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 /* 22*12296SLin.Ling@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23789Sahrens */ 24789Sahrens 25789Sahrens /* 263403Sbmc * DVA-based Adjustable Replacement Cache 27789Sahrens * 281544Seschrock * While much of the theory of operation used here is 291544Seschrock * based on the self-tuning, low overhead replacement cache 30789Sahrens * presented by Megiddo and Modha at FAST 2003, there are some 31789Sahrens * significant differences: 32789Sahrens * 33789Sahrens * 1. The Megiddo and Modha model assumes any page is evictable. 34789Sahrens * Pages in its cache cannot be "locked" into memory. This makes 35789Sahrens * the eviction algorithm simple: evict the last page in the list. 36789Sahrens * This also make the performance characteristics easy to reason 37789Sahrens * about. Our cache is not so simple. At any given moment, some 38789Sahrens * subset of the blocks in the cache are un-evictable because we 39789Sahrens * have handed out a reference to them. Blocks are only evictable 40789Sahrens * when there are no external references active. This makes 41789Sahrens * eviction far more problematic: we choose to evict the evictable 42789Sahrens * blocks that are the "lowest" in the list. 43789Sahrens * 44789Sahrens * There are times when it is not possible to evict the requested 45789Sahrens * space. In these circumstances we are unable to adjust the cache 46789Sahrens * size. To prevent the cache growing unbounded at these times we 475450Sbrendan * implement a "cache throttle" that slows the flow of new data 485450Sbrendan * into the cache until we can make space available. 49789Sahrens * 50789Sahrens * 2. The Megiddo and Modha model assumes a fixed cache size. 51789Sahrens * Pages are evicted when the cache is full and there is a cache 52789Sahrens * miss. Our model has a variable sized cache. It grows with 535450Sbrendan * high use, but also tries to react to memory pressure from the 54789Sahrens * operating system: decreasing its size when system memory is 55789Sahrens * tight. 56789Sahrens * 57789Sahrens * 3. The Megiddo and Modha model assumes a fixed page size. All 58789Sahrens * elements of the cache are therefor exactly the same size. So 59789Sahrens * when adjusting the cache size following a cache miss, its simply 60789Sahrens * a matter of choosing a single page to evict. In our model, we 61789Sahrens * have variable sized cache blocks (rangeing from 512 bytes to 62789Sahrens * 128K bytes). We therefor choose a set of blocks to evict to make 63789Sahrens * space for a cache miss that approximates as closely as possible 64789Sahrens * the space used by the new block. 65789Sahrens * 66789Sahrens * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 67789Sahrens * by N. Megiddo & D. Modha, FAST 2003 68789Sahrens */ 69789Sahrens 70789Sahrens /* 71789Sahrens * The locking model: 72789Sahrens * 73789Sahrens * A new reference to a cache buffer can be obtained in two 74789Sahrens * ways: 1) via a hash table lookup using the DVA as a key, 755450Sbrendan * or 2) via one of the ARC lists. The arc_read() interface 76789Sahrens * uses method 1, while the internal arc algorithms for 77789Sahrens * adjusting the cache use method 2. We therefor provide two 78789Sahrens * types of locks: 1) the hash table lock array, and 2) the 79789Sahrens * arc list locks. 80789Sahrens * 81789Sahrens * Buffers do not have their own mutexs, rather they rely on the 82789Sahrens * hash table mutexs for the bulk of their protection (i.e. most 83789Sahrens * fields in the arc_buf_hdr_t are protected by these mutexs). 84789Sahrens * 85789Sahrens * buf_hash_find() returns the appropriate mutex (held) when it 86789Sahrens * locates the requested buffer in the hash table. It returns 87789Sahrens * NULL for the mutex if the buffer was not in the table. 88789Sahrens * 89789Sahrens * buf_hash_remove() expects the appropriate hash mutex to be 90789Sahrens * already held before it is invoked. 91789Sahrens * 92789Sahrens * Each arc state also has a mutex which is used to protect the 93789Sahrens * buffer list associated with the state. When attempting to 94789Sahrens * obtain a hash table lock while holding an arc list lock you 95789Sahrens * must use: mutex_tryenter() to avoid deadlock. Also note that 962688Smaybee * the active state mutex must be held before the ghost state mutex. 97789Sahrens * 981544Seschrock * Arc buffers may have an associated eviction callback function. 991544Seschrock * This function will be invoked prior to removing the buffer (e.g. 1001544Seschrock * in arc_do_user_evicts()). Note however that the data associated 1011544Seschrock * with the buffer may be evicted prior to the callback. The callback 1021544Seschrock * must be made with *no locks held* (to prevent deadlock). Additionally, 1031544Seschrock * the users of callbacks must ensure that their private data is 1041544Seschrock * protected from simultaneous callbacks from arc_buf_evict() 1051544Seschrock * and arc_do_user_evicts(). 1061544Seschrock * 107789Sahrens * Note that the majority of the performance stats are manipulated 108789Sahrens * with atomic operations. 1095450Sbrendan * 1105450Sbrendan * The L2ARC uses the l2arc_buflist_mtx global mutex for the following: 1115450Sbrendan * 1125450Sbrendan * - L2ARC buflist creation 1135450Sbrendan * - L2ARC buflist eviction 1145450Sbrendan * - L2ARC write completion, which walks L2ARC buflists 1155450Sbrendan * - ARC header destruction, as it removes from L2ARC buflists 1165450Sbrendan * - ARC header release, as it removes from L2ARC buflists 117789Sahrens */ 118789Sahrens 119789Sahrens #include <sys/spa.h> 120789Sahrens #include <sys/zio.h> 121789Sahrens #include <sys/zfs_context.h> 122789Sahrens #include <sys/arc.h> 123789Sahrens #include <sys/refcount.h> 1246643Seschrock #include <sys/vdev.h> 1259816SGeorge.Wilson@Sun.COM #include <sys/vdev_impl.h> 126789Sahrens #ifdef _KERNEL 127789Sahrens #include <sys/vmsystm.h> 128789Sahrens #include <vm/anon.h> 129789Sahrens #include <sys/fs/swapnode.h> 1301484Sek110237 #include <sys/dnlc.h> 131789Sahrens #endif 132789Sahrens #include <sys/callb.h> 1333403Sbmc #include <sys/kstat.h> 13410922SJeff.Bonwick@Sun.COM #include <zfs_fletcher.h> 135789Sahrens 136789Sahrens static kmutex_t arc_reclaim_thr_lock; 137789Sahrens static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 138789Sahrens static uint8_t arc_thread_exit; 139789Sahrens 1406245Smaybee extern int zfs_write_limit_shift; 1416245Smaybee extern uint64_t zfs_write_limit_max; 1427468SMark.Maybee@Sun.COM extern kmutex_t zfs_write_limit_lock; 1436245Smaybee 1441484Sek110237 #define ARC_REDUCE_DNLC_PERCENT 3 1451484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 1461484Sek110237 147789Sahrens typedef enum arc_reclaim_strategy { 148789Sahrens ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 149789Sahrens ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 150789Sahrens } arc_reclaim_strategy_t; 151789Sahrens 152789Sahrens /* number of seconds before growing cache again */ 153789Sahrens static int arc_grow_retry = 60; 154789Sahrens 1558582SBrendan.Gregg@Sun.COM /* shift of arc_c for calculating both min and max arc_p */ 1568582SBrendan.Gregg@Sun.COM static int arc_p_min_shift = 4; 1578582SBrendan.Gregg@Sun.COM 1588582SBrendan.Gregg@Sun.COM /* log2(fraction of arc to reclaim) */ 1598582SBrendan.Gregg@Sun.COM static int arc_shrink_shift = 5; 1608582SBrendan.Gregg@Sun.COM 1612391Smaybee /* 1622638Sperrin * minimum lifespan of a prefetch block in clock ticks 1632638Sperrin * (initialized in arc_init()) 1642391Smaybee */ 1652638Sperrin static int arc_min_prefetch_lifespan; 1662391Smaybee 167789Sahrens static int arc_dead; 168789Sahrens 169789Sahrens /* 1706987Sbrendan * The arc has filled available memory and has now warmed up. 1716987Sbrendan */ 1726987Sbrendan static boolean_t arc_warm; 1736987Sbrendan 1746987Sbrendan /* 1752885Sahrens * These tunables are for performance analysis. 1762885Sahrens */ 1772885Sahrens uint64_t zfs_arc_max; 1782885Sahrens uint64_t zfs_arc_min; 1794645Sek110237 uint64_t zfs_arc_meta_limit = 0; 1808582SBrendan.Gregg@Sun.COM int zfs_arc_grow_retry = 0; 1818582SBrendan.Gregg@Sun.COM int zfs_arc_shrink_shift = 0; 1828582SBrendan.Gregg@Sun.COM int zfs_arc_p_min_shift = 0; 1832885Sahrens 1842885Sahrens /* 1855450Sbrendan * Note that buffers can be in one of 6 states: 186789Sahrens * ARC_anon - anonymous (discussed below) 1871544Seschrock * ARC_mru - recently used, currently cached 1881544Seschrock * ARC_mru_ghost - recentely used, no longer in cache 1891544Seschrock * ARC_mfu - frequently used, currently cached 1901544Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 1915450Sbrendan * ARC_l2c_only - exists in L2ARC but not other states 1924309Smaybee * When there are no active references to the buffer, they are 1934309Smaybee * are linked onto a list in one of these arc states. These are 1944309Smaybee * the only buffers that can be evicted or deleted. Within each 1954309Smaybee * state there are multiple lists, one for meta-data and one for 1964309Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 1974309Smaybee * etc.) is tracked separately so that it can be managed more 1985450Sbrendan * explicitly: favored over data, limited explicitly. 199789Sahrens * 200789Sahrens * Anonymous buffers are buffers that are not associated with 201789Sahrens * a DVA. These are buffers that hold dirty block copies 202789Sahrens * before they are written to stable storage. By definition, 2031544Seschrock * they are "ref'd" and are considered part of arc_mru 204789Sahrens * that cannot be freed. Generally, they will aquire a DVA 2051544Seschrock * as they are written and migrate onto the arc_mru list. 2065450Sbrendan * 2075450Sbrendan * The ARC_l2c_only state is for buffers that are in the second 2085450Sbrendan * level ARC but no longer in any of the ARC_m* lists. The second 2095450Sbrendan * level ARC itself may also contain buffers that are in any of 2105450Sbrendan * the ARC_m* states - meaning that a buffer can exist in two 2115450Sbrendan * places. The reason for the ARC_l2c_only state is to keep the 2125450Sbrendan * buffer header in the hash table, so that reads that hit the 2135450Sbrendan * second level ARC benefit from these fast lookups. 214789Sahrens */ 215789Sahrens 216789Sahrens typedef struct arc_state { 2174309Smaybee list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */ 2184309Smaybee uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 2194309Smaybee uint64_t arcs_size; /* total amount of data in this state */ 2203403Sbmc kmutex_t arcs_mtx; 221789Sahrens } arc_state_t; 222789Sahrens 2235450Sbrendan /* The 6 states: */ 224789Sahrens static arc_state_t ARC_anon; 2251544Seschrock static arc_state_t ARC_mru; 2261544Seschrock static arc_state_t ARC_mru_ghost; 2271544Seschrock static arc_state_t ARC_mfu; 2281544Seschrock static arc_state_t ARC_mfu_ghost; 2295450Sbrendan static arc_state_t ARC_l2c_only; 230789Sahrens 2313403Sbmc typedef struct arc_stats { 2323403Sbmc kstat_named_t arcstat_hits; 2333403Sbmc kstat_named_t arcstat_misses; 2343403Sbmc kstat_named_t arcstat_demand_data_hits; 2353403Sbmc kstat_named_t arcstat_demand_data_misses; 2363403Sbmc kstat_named_t arcstat_demand_metadata_hits; 2373403Sbmc kstat_named_t arcstat_demand_metadata_misses; 2383403Sbmc kstat_named_t arcstat_prefetch_data_hits; 2393403Sbmc kstat_named_t arcstat_prefetch_data_misses; 2403403Sbmc kstat_named_t arcstat_prefetch_metadata_hits; 2413403Sbmc kstat_named_t arcstat_prefetch_metadata_misses; 2423403Sbmc kstat_named_t arcstat_mru_hits; 2433403Sbmc kstat_named_t arcstat_mru_ghost_hits; 2443403Sbmc kstat_named_t arcstat_mfu_hits; 2453403Sbmc kstat_named_t arcstat_mfu_ghost_hits; 2463403Sbmc kstat_named_t arcstat_deleted; 2473403Sbmc kstat_named_t arcstat_recycle_miss; 2483403Sbmc kstat_named_t arcstat_mutex_miss; 2493403Sbmc kstat_named_t arcstat_evict_skip; 25010357SBrendan.Gregg@Sun.COM kstat_named_t arcstat_evict_l2_cached; 25110357SBrendan.Gregg@Sun.COM kstat_named_t arcstat_evict_l2_eligible; 25210357SBrendan.Gregg@Sun.COM kstat_named_t arcstat_evict_l2_ineligible; 2533403Sbmc kstat_named_t arcstat_hash_elements; 2543403Sbmc kstat_named_t arcstat_hash_elements_max; 2553403Sbmc kstat_named_t arcstat_hash_collisions; 2563403Sbmc kstat_named_t arcstat_hash_chains; 2573403Sbmc kstat_named_t arcstat_hash_chain_max; 2583403Sbmc kstat_named_t arcstat_p; 2593403Sbmc kstat_named_t arcstat_c; 2603403Sbmc kstat_named_t arcstat_c_min; 2613403Sbmc kstat_named_t arcstat_c_max; 2623403Sbmc kstat_named_t arcstat_size; 2635450Sbrendan kstat_named_t arcstat_hdr_size; 2648582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_data_size; 2658582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_other_size; 2665450Sbrendan kstat_named_t arcstat_l2_hits; 2675450Sbrendan kstat_named_t arcstat_l2_misses; 2685450Sbrendan kstat_named_t arcstat_l2_feeds; 2695450Sbrendan kstat_named_t arcstat_l2_rw_clash; 2708582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_l2_read_bytes; 2718582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_l2_write_bytes; 2725450Sbrendan kstat_named_t arcstat_l2_writes_sent; 2735450Sbrendan kstat_named_t arcstat_l2_writes_done; 2745450Sbrendan kstat_named_t arcstat_l2_writes_error; 2755450Sbrendan kstat_named_t arcstat_l2_writes_hdr_miss; 2765450Sbrendan kstat_named_t arcstat_l2_evict_lock_retry; 2775450Sbrendan kstat_named_t arcstat_l2_evict_reading; 2785450Sbrendan kstat_named_t arcstat_l2_free_on_write; 2795450Sbrendan kstat_named_t arcstat_l2_abort_lowmem; 2805450Sbrendan kstat_named_t arcstat_l2_cksum_bad; 2815450Sbrendan kstat_named_t arcstat_l2_io_error; 2825450Sbrendan kstat_named_t arcstat_l2_size; 2835450Sbrendan kstat_named_t arcstat_l2_hdr_size; 2846245Smaybee kstat_named_t arcstat_memory_throttle_count; 2853403Sbmc } arc_stats_t; 2863403Sbmc 2873403Sbmc static arc_stats_t arc_stats = { 2883403Sbmc { "hits", KSTAT_DATA_UINT64 }, 2893403Sbmc { "misses", KSTAT_DATA_UINT64 }, 2903403Sbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 2913403Sbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 2923403Sbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 2933403Sbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 2943403Sbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 2953403Sbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 2963403Sbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 2973403Sbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 2983403Sbmc { "mru_hits", KSTAT_DATA_UINT64 }, 2993403Sbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 3003403Sbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 3013403Sbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 3023403Sbmc { "deleted", KSTAT_DATA_UINT64 }, 3033403Sbmc { "recycle_miss", KSTAT_DATA_UINT64 }, 3043403Sbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 3053403Sbmc { "evict_skip", KSTAT_DATA_UINT64 }, 30610357SBrendan.Gregg@Sun.COM { "evict_l2_cached", KSTAT_DATA_UINT64 }, 30710357SBrendan.Gregg@Sun.COM { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 30810357SBrendan.Gregg@Sun.COM { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 3093403Sbmc { "hash_elements", KSTAT_DATA_UINT64 }, 3103403Sbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 3113403Sbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 3123403Sbmc { "hash_chains", KSTAT_DATA_UINT64 }, 3133403Sbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 3143403Sbmc { "p", KSTAT_DATA_UINT64 }, 3153403Sbmc { "c", KSTAT_DATA_UINT64 }, 3163403Sbmc { "c_min", KSTAT_DATA_UINT64 }, 3173403Sbmc { "c_max", KSTAT_DATA_UINT64 }, 3185450Sbrendan { "size", KSTAT_DATA_UINT64 }, 3195450Sbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 3208582SBrendan.Gregg@Sun.COM { "data_size", KSTAT_DATA_UINT64 }, 3218582SBrendan.Gregg@Sun.COM { "other_size", KSTAT_DATA_UINT64 }, 3225450Sbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 3235450Sbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 3245450Sbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 3255450Sbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 3268582SBrendan.Gregg@Sun.COM { "l2_read_bytes", KSTAT_DATA_UINT64 }, 3278582SBrendan.Gregg@Sun.COM { "l2_write_bytes", KSTAT_DATA_UINT64 }, 3285450Sbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 3295450Sbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 3305450Sbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 3315450Sbrendan { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 3325450Sbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 3335450Sbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 3345450Sbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 3355450Sbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 3365450Sbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 3375450Sbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 3385450Sbrendan { "l2_size", KSTAT_DATA_UINT64 }, 3396245Smaybee { "l2_hdr_size", KSTAT_DATA_UINT64 }, 3406245Smaybee { "memory_throttle_count", KSTAT_DATA_UINT64 } 3413403Sbmc }; 342789Sahrens 3433403Sbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 3443403Sbmc 3453403Sbmc #define ARCSTAT_INCR(stat, val) \ 3463403Sbmc atomic_add_64(&arc_stats.stat.value.ui64, (val)); 3473403Sbmc 34810922SJeff.Bonwick@Sun.COM #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 3493403Sbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 3503403Sbmc 3513403Sbmc #define ARCSTAT_MAX(stat, val) { \ 3523403Sbmc uint64_t m; \ 3533403Sbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 3543403Sbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 3553403Sbmc continue; \ 3563403Sbmc } 3573403Sbmc 3583403Sbmc #define ARCSTAT_MAXSTAT(stat) \ 3593403Sbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 360789Sahrens 3613403Sbmc /* 3623403Sbmc * We define a macro to allow ARC hits/misses to be easily broken down by 3633403Sbmc * two separate conditions, giving a total of four different subtypes for 3643403Sbmc * each of hits and misses (so eight statistics total). 3653403Sbmc */ 3663403Sbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 3673403Sbmc if (cond1) { \ 3683403Sbmc if (cond2) { \ 3693403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 3703403Sbmc } else { \ 3713403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 3723403Sbmc } \ 3733403Sbmc } else { \ 3743403Sbmc if (cond2) { \ 3753403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 3763403Sbmc } else { \ 3773403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 3783403Sbmc } \ 3793403Sbmc } 380789Sahrens 3813403Sbmc kstat_t *arc_ksp; 38210922SJeff.Bonwick@Sun.COM static arc_state_t *arc_anon; 3833403Sbmc static arc_state_t *arc_mru; 3843403Sbmc static arc_state_t *arc_mru_ghost; 3853403Sbmc static arc_state_t *arc_mfu; 3863403Sbmc static arc_state_t *arc_mfu_ghost; 3875450Sbrendan static arc_state_t *arc_l2c_only; 3883403Sbmc 3893403Sbmc /* 3903403Sbmc * There are several ARC variables that are critical to export as kstats -- 3913403Sbmc * but we don't want to have to grovel around in the kstat whenever we wish to 3923403Sbmc * manipulate them. For these variables, we therefore define them to be in 3933403Sbmc * terms of the statistic variable. This assures that we are not introducing 3943403Sbmc * the possibility of inconsistency by having shadow copies of the variables, 3953403Sbmc * while still allowing the code to be readable. 3963403Sbmc */ 3973403Sbmc #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 3983403Sbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 3993403Sbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 4003403Sbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 4013403Sbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 4023403Sbmc 4033403Sbmc static int arc_no_grow; /* Don't try to grow cache size */ 4043403Sbmc static uint64_t arc_tempreserve; 4059412SAleksandr.Guzovskiy@Sun.COM static uint64_t arc_loaned_bytes; 4064309Smaybee static uint64_t arc_meta_used; 4074309Smaybee static uint64_t arc_meta_limit; 4084309Smaybee static uint64_t arc_meta_max = 0; 409789Sahrens 4105450Sbrendan typedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 4115450Sbrendan 412789Sahrens typedef struct arc_callback arc_callback_t; 413789Sahrens 414789Sahrens struct arc_callback { 4153547Smaybee void *acb_private; 416789Sahrens arc_done_func_t *acb_done; 417789Sahrens arc_buf_t *acb_buf; 418789Sahrens zio_t *acb_zio_dummy; 419789Sahrens arc_callback_t *acb_next; 420789Sahrens }; 421789Sahrens 4223547Smaybee typedef struct arc_write_callback arc_write_callback_t; 4233547Smaybee 4243547Smaybee struct arc_write_callback { 4253547Smaybee void *awcb_private; 4263547Smaybee arc_done_func_t *awcb_ready; 4273547Smaybee arc_done_func_t *awcb_done; 4283547Smaybee arc_buf_t *awcb_buf; 4293547Smaybee }; 4303547Smaybee 431789Sahrens struct arc_buf_hdr { 432789Sahrens /* protected by hash lock */ 433789Sahrens dva_t b_dva; 434789Sahrens uint64_t b_birth; 435789Sahrens uint64_t b_cksum0; 436789Sahrens 4373093Sahrens kmutex_t b_freeze_lock; 4383093Sahrens zio_cksum_t *b_freeze_cksum; 439*12296SLin.Ling@Sun.COM void *b_thawed; 4403093Sahrens 441789Sahrens arc_buf_hdr_t *b_hash_next; 442789Sahrens arc_buf_t *b_buf; 443789Sahrens uint32_t b_flags; 4441544Seschrock uint32_t b_datacnt; 445789Sahrens 4463290Sjohansen arc_callback_t *b_acb; 447789Sahrens kcondvar_t b_cv; 4483290Sjohansen 4493290Sjohansen /* immutable */ 4503290Sjohansen arc_buf_contents_t b_type; 4513290Sjohansen uint64_t b_size; 4528636SMark.Maybee@Sun.COM uint64_t b_spa; 453789Sahrens 454789Sahrens /* protected by arc state mutex */ 455789Sahrens arc_state_t *b_state; 456789Sahrens list_node_t b_arc_node; 457789Sahrens 458789Sahrens /* updated atomically */ 459789Sahrens clock_t b_arc_access; 460789Sahrens 461789Sahrens /* self protecting */ 462789Sahrens refcount_t b_refcnt; 4635450Sbrendan 4645450Sbrendan l2arc_buf_hdr_t *b_l2hdr; 4655450Sbrendan list_node_t b_l2node; 466789Sahrens }; 467789Sahrens 4681544Seschrock static arc_buf_t *arc_eviction_list; 4691544Seschrock static kmutex_t arc_eviction_mtx; 4702887Smaybee static arc_buf_hdr_t arc_eviction_hdr; 4712688Smaybee static void arc_get_data_buf(arc_buf_t *buf); 4722688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 4734309Smaybee static int arc_evict_needed(arc_buf_contents_t type); 4748636SMark.Maybee@Sun.COM static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes); 4751544Seschrock 47610357SBrendan.Gregg@Sun.COM static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab); 47710357SBrendan.Gregg@Sun.COM 4781544Seschrock #define GHOST_STATE(state) \ 4795450Sbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 4805450Sbrendan (state) == arc_l2c_only) 4811544Seschrock 482789Sahrens /* 483789Sahrens * Private ARC flags. These flags are private ARC only flags that will show up 484789Sahrens * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 485789Sahrens * be passed in as arc_flags in things like arc_read. However, these flags 486789Sahrens * should never be passed and should only be set by ARC code. When adding new 487789Sahrens * public flags, make sure not to smash the private ones. 488789Sahrens */ 489789Sahrens 4901544Seschrock #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 491789Sahrens #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 492789Sahrens #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 493789Sahrens #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 4941544Seschrock #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 4952391Smaybee #define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 4965450Sbrendan #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 4977237Sek110237 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 4987237Sek110237 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 4997237Sek110237 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 500789Sahrens 5011544Seschrock #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 502789Sahrens #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 503789Sahrens #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 5048582SBrendan.Gregg@Sun.COM #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH) 505789Sahrens #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 5061544Seschrock #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 5075450Sbrendan #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 5087237Sek110237 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 5096987Sbrendan #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 5106987Sbrendan (hdr)->b_l2hdr != NULL) 5115450Sbrendan #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 5125450Sbrendan #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 5135450Sbrendan #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 514789Sahrens 515789Sahrens /* 5166018Sbrendan * Other sizes 5176018Sbrendan */ 5186018Sbrendan 5196018Sbrendan #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 5206018Sbrendan #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 5216018Sbrendan 5226018Sbrendan /* 523789Sahrens * Hash table routines 524789Sahrens */ 525789Sahrens 526789Sahrens #define HT_LOCK_PAD 64 527789Sahrens 528789Sahrens struct ht_lock { 529789Sahrens kmutex_t ht_lock; 530789Sahrens #ifdef _KERNEL 531789Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 532789Sahrens #endif 533789Sahrens }; 534789Sahrens 535789Sahrens #define BUF_LOCKS 256 536789Sahrens typedef struct buf_hash_table { 537789Sahrens uint64_t ht_mask; 538789Sahrens arc_buf_hdr_t **ht_table; 539789Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 540789Sahrens } buf_hash_table_t; 541789Sahrens 542789Sahrens static buf_hash_table_t buf_hash_table; 543789Sahrens 544789Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 545789Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 546789Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 547789Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 548*12296SLin.Ling@Sun.COM #define HDR_LOCK(hdr) \ 549*12296SLin.Ling@Sun.COM (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 550789Sahrens 551789Sahrens uint64_t zfs_crc64_table[256]; 552789Sahrens 5535450Sbrendan /* 5545450Sbrendan * Level 2 ARC 5555450Sbrendan */ 5565450Sbrendan 5575450Sbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 5588582SBrendan.Gregg@Sun.COM #define L2ARC_HEADROOM 2 /* num of writes */ 5598582SBrendan.Gregg@Sun.COM #define L2ARC_FEED_SECS 1 /* caching interval secs */ 5608582SBrendan.Gregg@Sun.COM #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 5615450Sbrendan 5625450Sbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 5635450Sbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 5645450Sbrendan 5655450Sbrendan /* 5665450Sbrendan * L2ARC Performance Tunables 5675450Sbrendan */ 5685450Sbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 5696987Sbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 5705450Sbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 5715450Sbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 5728582SBrendan.Gregg@Sun.COM uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 5735450Sbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 5748582SBrendan.Gregg@Sun.COM boolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 5758582SBrendan.Gregg@Sun.COM boolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 5765450Sbrendan 5775450Sbrendan /* 5785450Sbrendan * L2ARC Internals 5795450Sbrendan */ 5805450Sbrendan typedef struct l2arc_dev { 5815450Sbrendan vdev_t *l2ad_vdev; /* vdev */ 5825450Sbrendan spa_t *l2ad_spa; /* spa */ 5835450Sbrendan uint64_t l2ad_hand; /* next write location */ 5845450Sbrendan uint64_t l2ad_write; /* desired write size, bytes */ 5856987Sbrendan uint64_t l2ad_boost; /* warmup write boost, bytes */ 5865450Sbrendan uint64_t l2ad_start; /* first addr on device */ 5875450Sbrendan uint64_t l2ad_end; /* last addr on device */ 5885450Sbrendan uint64_t l2ad_evict; /* last addr eviction reached */ 5895450Sbrendan boolean_t l2ad_first; /* first sweep through */ 5908582SBrendan.Gregg@Sun.COM boolean_t l2ad_writing; /* currently writing */ 5915450Sbrendan list_t *l2ad_buflist; /* buffer list */ 5925450Sbrendan list_node_t l2ad_node; /* device list node */ 5935450Sbrendan } l2arc_dev_t; 5945450Sbrendan 5955450Sbrendan static list_t L2ARC_dev_list; /* device list */ 5965450Sbrendan static list_t *l2arc_dev_list; /* device list pointer */ 5975450Sbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 5985450Sbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 5995450Sbrendan static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 6005450Sbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 6015450Sbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 6025450Sbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 6035450Sbrendan static uint64_t l2arc_ndev; /* number of devices */ 6045450Sbrendan 6055450Sbrendan typedef struct l2arc_read_callback { 6065450Sbrendan arc_buf_t *l2rcb_buf; /* read buffer */ 6075450Sbrendan spa_t *l2rcb_spa; /* spa */ 6085450Sbrendan blkptr_t l2rcb_bp; /* original blkptr */ 6095450Sbrendan zbookmark_t l2rcb_zb; /* original bookmark */ 6105450Sbrendan int l2rcb_flags; /* original flags */ 6115450Sbrendan } l2arc_read_callback_t; 6125450Sbrendan 6135450Sbrendan typedef struct l2arc_write_callback { 6145450Sbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 6155450Sbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 6165450Sbrendan } l2arc_write_callback_t; 6175450Sbrendan 6185450Sbrendan struct l2arc_buf_hdr { 6195450Sbrendan /* protected by arc_buf_hdr mutex */ 6205450Sbrendan l2arc_dev_t *b_dev; /* L2ARC device */ 6219215SGeorge.Wilson@Sun.COM uint64_t b_daddr; /* disk address, offset byte */ 6225450Sbrendan }; 6235450Sbrendan 6245450Sbrendan typedef struct l2arc_data_free { 6255450Sbrendan /* protected by l2arc_free_on_write_mtx */ 6265450Sbrendan void *l2df_data; 6275450Sbrendan size_t l2df_size; 6285450Sbrendan void (*l2df_func)(void *, size_t); 6295450Sbrendan list_node_t l2df_list_node; 6305450Sbrendan } l2arc_data_free_t; 6315450Sbrendan 6325450Sbrendan static kmutex_t l2arc_feed_thr_lock; 6335450Sbrendan static kcondvar_t l2arc_feed_thr_cv; 6345450Sbrendan static uint8_t l2arc_thread_exit; 6355450Sbrendan 6365450Sbrendan static void l2arc_read_done(zio_t *zio); 6375450Sbrendan static void l2arc_hdr_stat_add(void); 6385450Sbrendan static void l2arc_hdr_stat_remove(void); 6395450Sbrendan 640789Sahrens static uint64_t 6418636SMark.Maybee@Sun.COM buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 642789Sahrens { 643789Sahrens uint8_t *vdva = (uint8_t *)dva; 644789Sahrens uint64_t crc = -1ULL; 645789Sahrens int i; 646789Sahrens 647789Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 648789Sahrens 649789Sahrens for (i = 0; i < sizeof (dva_t); i++) 650789Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 651789Sahrens 6528636SMark.Maybee@Sun.COM crc ^= (spa>>8) ^ birth; 653789Sahrens 654789Sahrens return (crc); 655789Sahrens } 656789Sahrens 657789Sahrens #define BUF_EMPTY(buf) \ 658789Sahrens ((buf)->b_dva.dva_word[0] == 0 && \ 659789Sahrens (buf)->b_dva.dva_word[1] == 0 && \ 660789Sahrens (buf)->b_birth == 0) 661789Sahrens 662789Sahrens #define BUF_EQUAL(spa, dva, birth, buf) \ 663789Sahrens ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 664789Sahrens ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 665789Sahrens ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 666789Sahrens 667*12296SLin.Ling@Sun.COM static void 668*12296SLin.Ling@Sun.COM buf_discard_identity(arc_buf_hdr_t *hdr) 669*12296SLin.Ling@Sun.COM { 670*12296SLin.Ling@Sun.COM hdr->b_dva.dva_word[0] = 0; 671*12296SLin.Ling@Sun.COM hdr->b_dva.dva_word[1] = 0; 672*12296SLin.Ling@Sun.COM hdr->b_birth = 0; 673*12296SLin.Ling@Sun.COM hdr->b_cksum0 = 0; 674*12296SLin.Ling@Sun.COM } 675*12296SLin.Ling@Sun.COM 676789Sahrens static arc_buf_hdr_t * 6778636SMark.Maybee@Sun.COM buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 678789Sahrens { 679789Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 680789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 681789Sahrens arc_buf_hdr_t *buf; 682789Sahrens 683789Sahrens mutex_enter(hash_lock); 684789Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 685789Sahrens buf = buf->b_hash_next) { 686789Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 687789Sahrens *lockp = hash_lock; 688789Sahrens return (buf); 689789Sahrens } 690789Sahrens } 691789Sahrens mutex_exit(hash_lock); 692789Sahrens *lockp = NULL; 693789Sahrens return (NULL); 694789Sahrens } 695789Sahrens 696789Sahrens /* 697789Sahrens * Insert an entry into the hash table. If there is already an element 698789Sahrens * equal to elem in the hash table, then the already existing element 699789Sahrens * will be returned and the new element will not be inserted. 700789Sahrens * Otherwise returns NULL. 701789Sahrens */ 702789Sahrens static arc_buf_hdr_t * 703789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 704789Sahrens { 705789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 706789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 707789Sahrens arc_buf_hdr_t *fbuf; 7083403Sbmc uint32_t i; 709789Sahrens 7101544Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 711789Sahrens *lockp = hash_lock; 712789Sahrens mutex_enter(hash_lock); 713789Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 714789Sahrens fbuf = fbuf->b_hash_next, i++) { 715789Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 716789Sahrens return (fbuf); 717789Sahrens } 718789Sahrens 719789Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 720789Sahrens buf_hash_table.ht_table[idx] = buf; 7211544Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 722789Sahrens 723789Sahrens /* collect some hash table performance data */ 724789Sahrens if (i > 0) { 7253403Sbmc ARCSTAT_BUMP(arcstat_hash_collisions); 726789Sahrens if (i == 1) 7273403Sbmc ARCSTAT_BUMP(arcstat_hash_chains); 7283403Sbmc 7293403Sbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 730789Sahrens } 7313403Sbmc 7323403Sbmc ARCSTAT_BUMP(arcstat_hash_elements); 7333403Sbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 734789Sahrens 735789Sahrens return (NULL); 736789Sahrens } 737789Sahrens 738789Sahrens static void 739789Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 740789Sahrens { 741789Sahrens arc_buf_hdr_t *fbuf, **bufp; 742789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 743789Sahrens 744789Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 7451544Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 746789Sahrens 747789Sahrens bufp = &buf_hash_table.ht_table[idx]; 748789Sahrens while ((fbuf = *bufp) != buf) { 749789Sahrens ASSERT(fbuf != NULL); 750789Sahrens bufp = &fbuf->b_hash_next; 751789Sahrens } 752789Sahrens *bufp = buf->b_hash_next; 753789Sahrens buf->b_hash_next = NULL; 7541544Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 755789Sahrens 756789Sahrens /* collect some hash table performance data */ 7573403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 7583403Sbmc 759789Sahrens if (buf_hash_table.ht_table[idx] && 760789Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 7613403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 762789Sahrens } 763789Sahrens 764789Sahrens /* 765789Sahrens * Global data structures and functions for the buf kmem cache. 766789Sahrens */ 767789Sahrens static kmem_cache_t *hdr_cache; 768789Sahrens static kmem_cache_t *buf_cache; 769789Sahrens 770789Sahrens static void 771789Sahrens buf_fini(void) 772789Sahrens { 773789Sahrens int i; 774789Sahrens 775789Sahrens kmem_free(buf_hash_table.ht_table, 776789Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 777789Sahrens for (i = 0; i < BUF_LOCKS; i++) 778789Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 779789Sahrens kmem_cache_destroy(hdr_cache); 780789Sahrens kmem_cache_destroy(buf_cache); 781789Sahrens } 782789Sahrens 783789Sahrens /* 784789Sahrens * Constructor callback - called when the cache is empty 785789Sahrens * and a new buf is requested. 786789Sahrens */ 787789Sahrens /* ARGSUSED */ 788789Sahrens static int 789789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 790789Sahrens { 791789Sahrens arc_buf_hdr_t *buf = vbuf; 792789Sahrens 793789Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 794789Sahrens refcount_create(&buf->b_refcnt); 795789Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 7964831Sgw25295 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 7978582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 7988582SBrendan.Gregg@Sun.COM 799789Sahrens return (0); 800789Sahrens } 801789Sahrens 8027545SMark.Maybee@Sun.COM /* ARGSUSED */ 8037545SMark.Maybee@Sun.COM static int 8047545SMark.Maybee@Sun.COM buf_cons(void *vbuf, void *unused, int kmflag) 8057545SMark.Maybee@Sun.COM { 8067545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 8077545SMark.Maybee@Sun.COM 8087545SMark.Maybee@Sun.COM bzero(buf, sizeof (arc_buf_t)); 809*12296SLin.Ling@Sun.COM mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 810*12296SLin.Ling@Sun.COM rw_init(&buf->b_data_lock, NULL, RW_DEFAULT, NULL); 8118582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 8128582SBrendan.Gregg@Sun.COM 8137545SMark.Maybee@Sun.COM return (0); 8147545SMark.Maybee@Sun.COM } 8157545SMark.Maybee@Sun.COM 816789Sahrens /* 817789Sahrens * Destructor callback - called when a cached buf is 818789Sahrens * no longer required. 819789Sahrens */ 820789Sahrens /* ARGSUSED */ 821789Sahrens static void 822789Sahrens hdr_dest(void *vbuf, void *unused) 823789Sahrens { 824789Sahrens arc_buf_hdr_t *buf = vbuf; 825789Sahrens 82610922SJeff.Bonwick@Sun.COM ASSERT(BUF_EMPTY(buf)); 827789Sahrens refcount_destroy(&buf->b_refcnt); 828789Sahrens cv_destroy(&buf->b_cv); 8294831Sgw25295 mutex_destroy(&buf->b_freeze_lock); 8308582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 831789Sahrens } 832789Sahrens 8337545SMark.Maybee@Sun.COM /* ARGSUSED */ 8347545SMark.Maybee@Sun.COM static void 8357545SMark.Maybee@Sun.COM buf_dest(void *vbuf, void *unused) 8367545SMark.Maybee@Sun.COM { 8377545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 8387545SMark.Maybee@Sun.COM 839*12296SLin.Ling@Sun.COM mutex_destroy(&buf->b_evict_lock); 840*12296SLin.Ling@Sun.COM rw_destroy(&buf->b_data_lock); 8418582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 8427545SMark.Maybee@Sun.COM } 8437545SMark.Maybee@Sun.COM 844789Sahrens /* 845789Sahrens * Reclaim callback -- invoked when memory is low. 846789Sahrens */ 847789Sahrens /* ARGSUSED */ 848789Sahrens static void 849789Sahrens hdr_recl(void *unused) 850789Sahrens { 851789Sahrens dprintf("hdr_recl called\n"); 8523158Smaybee /* 8533158Smaybee * umem calls the reclaim func when we destroy the buf cache, 8543158Smaybee * which is after we do arc_fini(). 8553158Smaybee */ 8563158Smaybee if (!arc_dead) 8573158Smaybee cv_signal(&arc_reclaim_thr_cv); 858789Sahrens } 859789Sahrens 860789Sahrens static void 861789Sahrens buf_init(void) 862789Sahrens { 863789Sahrens uint64_t *ct; 8641544Seschrock uint64_t hsize = 1ULL << 12; 865789Sahrens int i, j; 866789Sahrens 867789Sahrens /* 868789Sahrens * The hash table is big enough to fill all of physical memory 8691544Seschrock * with an average 64K block size. The table will take up 8701544Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 871789Sahrens */ 8721544Seschrock while (hsize * 65536 < physmem * PAGESIZE) 873789Sahrens hsize <<= 1; 8741544Seschrock retry: 875789Sahrens buf_hash_table.ht_mask = hsize - 1; 8761544Seschrock buf_hash_table.ht_table = 8771544Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 8781544Seschrock if (buf_hash_table.ht_table == NULL) { 8791544Seschrock ASSERT(hsize > (1ULL << 8)); 8801544Seschrock hsize >>= 1; 8811544Seschrock goto retry; 8821544Seschrock } 883789Sahrens 884789Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 885789Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 886789Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 8877545SMark.Maybee@Sun.COM 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 888789Sahrens 889789Sahrens for (i = 0; i < 256; i++) 890789Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 891789Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 892789Sahrens 893789Sahrens for (i = 0; i < BUF_LOCKS; i++) { 894789Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 895789Sahrens NULL, MUTEX_DEFAULT, NULL); 896789Sahrens } 897789Sahrens } 898789Sahrens 899789Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 900789Sahrens 901789Sahrens static void 9023093Sahrens arc_cksum_verify(arc_buf_t *buf) 9033093Sahrens { 9043093Sahrens zio_cksum_t zc; 9053093Sahrens 9063312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 9073093Sahrens return; 9083093Sahrens 9093093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9103265Sahrens if (buf->b_hdr->b_freeze_cksum == NULL || 9113265Sahrens (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 9123093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9133093Sahrens return; 9143093Sahrens } 9153093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 9163093Sahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 9173093Sahrens panic("buffer modified while frozen!"); 9183093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9193093Sahrens } 9203093Sahrens 9215450Sbrendan static int 9225450Sbrendan arc_cksum_equal(arc_buf_t *buf) 9235450Sbrendan { 9245450Sbrendan zio_cksum_t zc; 9255450Sbrendan int equal; 9265450Sbrendan 9275450Sbrendan mutex_enter(&buf->b_hdr->b_freeze_lock); 9285450Sbrendan fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 9295450Sbrendan equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 9305450Sbrendan mutex_exit(&buf->b_hdr->b_freeze_lock); 9315450Sbrendan 9325450Sbrendan return (equal); 9335450Sbrendan } 9345450Sbrendan 9353093Sahrens static void 9365450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force) 9373093Sahrens { 9385450Sbrendan if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 9393093Sahrens return; 9403093Sahrens 9413093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9423093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9433093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9443093Sahrens return; 9453093Sahrens } 9463093Sahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 9473093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 9483093Sahrens buf->b_hdr->b_freeze_cksum); 9493093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9503093Sahrens } 9513093Sahrens 9523093Sahrens void 9533093Sahrens arc_buf_thaw(arc_buf_t *buf) 9543093Sahrens { 955*12296SLin.Ling@Sun.COM kmutex_t *hash_lock; 956*12296SLin.Ling@Sun.COM 957*12296SLin.Ling@Sun.COM hash_lock = HDR_LOCK(buf->b_hdr); 958*12296SLin.Ling@Sun.COM mutex_enter(hash_lock); 959*12296SLin.Ling@Sun.COM 9605450Sbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 9615450Sbrendan if (buf->b_hdr->b_state != arc_anon) 9625450Sbrendan panic("modifying non-anon buffer!"); 9635450Sbrendan if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 9645450Sbrendan panic("modifying buffer while i/o in progress!"); 9655450Sbrendan arc_cksum_verify(buf); 9665450Sbrendan } 9675450Sbrendan 9683093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9693093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9703093Sahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 9713093Sahrens buf->b_hdr->b_freeze_cksum = NULL; 9723093Sahrens } 973*12296SLin.Ling@Sun.COM 974*12296SLin.Ling@Sun.COM if (zfs_flags & ZFS_DEBUG_MODIFY) { 975*12296SLin.Ling@Sun.COM if (buf->b_hdr->b_thawed) 976*12296SLin.Ling@Sun.COM kmem_free(buf->b_hdr->b_thawed, 1); 977*12296SLin.Ling@Sun.COM buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP); 978*12296SLin.Ling@Sun.COM } 979*12296SLin.Ling@Sun.COM 9803093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 981*12296SLin.Ling@Sun.COM mutex_exit(hash_lock); 9823093Sahrens } 9833093Sahrens 9843093Sahrens void 9853093Sahrens arc_buf_freeze(arc_buf_t *buf) 9863093Sahrens { 987*12296SLin.Ling@Sun.COM kmutex_t *hash_lock; 988*12296SLin.Ling@Sun.COM 9893312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 9903312Sahrens return; 9913312Sahrens 992*12296SLin.Ling@Sun.COM hash_lock = HDR_LOCK(buf->b_hdr); 993*12296SLin.Ling@Sun.COM mutex_enter(hash_lock); 994*12296SLin.Ling@Sun.COM 9953093Sahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 9963403Sbmc buf->b_hdr->b_state == arc_anon); 9975450Sbrendan arc_cksum_compute(buf, B_FALSE); 998*12296SLin.Ling@Sun.COM mutex_exit(hash_lock); 9993093Sahrens } 10003093Sahrens 10013093Sahrens static void 1002789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1003789Sahrens { 1004789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 1005789Sahrens 1006789Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 10073403Sbmc (ab->b_state != arc_anon)) { 10083700Sek110237 uint64_t delta = ab->b_size * ab->b_datacnt; 10094309Smaybee list_t *list = &ab->b_state->arcs_list[ab->b_type]; 10104309Smaybee uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 1011789Sahrens 10123403Sbmc ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx)); 10133403Sbmc mutex_enter(&ab->b_state->arcs_mtx); 1014789Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 10154309Smaybee list_remove(list, ab); 10161544Seschrock if (GHOST_STATE(ab->b_state)) { 10171544Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 10181544Seschrock ASSERT3P(ab->b_buf, ==, NULL); 10191544Seschrock delta = ab->b_size; 10201544Seschrock } 10211544Seschrock ASSERT(delta > 0); 10224309Smaybee ASSERT3U(*size, >=, delta); 10234309Smaybee atomic_add_64(size, -delta); 10243403Sbmc mutex_exit(&ab->b_state->arcs_mtx); 10257046Sahrens /* remove the prefetch flag if we get a reference */ 10262391Smaybee if (ab->b_flags & ARC_PREFETCH) 10272391Smaybee ab->b_flags &= ~ARC_PREFETCH; 1028789Sahrens } 1029789Sahrens } 1030789Sahrens 1031789Sahrens static int 1032789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1033789Sahrens { 1034789Sahrens int cnt; 10353403Sbmc arc_state_t *state = ab->b_state; 1036789Sahrens 10373403Sbmc ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 10383403Sbmc ASSERT(!GHOST_STATE(state)); 1039789Sahrens 1040789Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 10413403Sbmc (state != arc_anon)) { 10424309Smaybee uint64_t *size = &state->arcs_lsize[ab->b_type]; 10434309Smaybee 10443403Sbmc ASSERT(!MUTEX_HELD(&state->arcs_mtx)); 10453403Sbmc mutex_enter(&state->arcs_mtx); 1046789Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 10474309Smaybee list_insert_head(&state->arcs_list[ab->b_type], ab); 10481544Seschrock ASSERT(ab->b_datacnt > 0); 10494309Smaybee atomic_add_64(size, ab->b_size * ab->b_datacnt); 10503403Sbmc mutex_exit(&state->arcs_mtx); 1051789Sahrens } 1052789Sahrens return (cnt); 1053789Sahrens } 1054789Sahrens 1055789Sahrens /* 1056789Sahrens * Move the supplied buffer to the indicated state. The mutex 1057789Sahrens * for the buffer must be held by the caller. 1058789Sahrens */ 1059789Sahrens static void 10601544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1061789Sahrens { 10621544Seschrock arc_state_t *old_state = ab->b_state; 10633700Sek110237 int64_t refcnt = refcount_count(&ab->b_refcnt); 10643700Sek110237 uint64_t from_delta, to_delta; 1065789Sahrens 1066789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 10671544Seschrock ASSERT(new_state != old_state); 10681544Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 10691544Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 107010922SJeff.Bonwick@Sun.COM ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon); 10711544Seschrock 10721544Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 1073789Sahrens 1074789Sahrens /* 1075789Sahrens * If this buffer is evictable, transfer it from the 1076789Sahrens * old state list to the new state list. 1077789Sahrens */ 10781544Seschrock if (refcnt == 0) { 10793403Sbmc if (old_state != arc_anon) { 10803403Sbmc int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx); 10814309Smaybee uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 10821544Seschrock 10831544Seschrock if (use_mutex) 10843403Sbmc mutex_enter(&old_state->arcs_mtx); 10851544Seschrock 10861544Seschrock ASSERT(list_link_active(&ab->b_arc_node)); 10874309Smaybee list_remove(&old_state->arcs_list[ab->b_type], ab); 1088789Sahrens 10892391Smaybee /* 10902391Smaybee * If prefetching out of the ghost cache, 1091*12296SLin.Ling@Sun.COM * we will have a non-zero datacnt. 10922391Smaybee */ 10932391Smaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 10942391Smaybee /* ghost elements have a ghost size */ 10951544Seschrock ASSERT(ab->b_buf == NULL); 10961544Seschrock from_delta = ab->b_size; 1097789Sahrens } 10984309Smaybee ASSERT3U(*size, >=, from_delta); 10994309Smaybee atomic_add_64(size, -from_delta); 11001544Seschrock 11011544Seschrock if (use_mutex) 11023403Sbmc mutex_exit(&old_state->arcs_mtx); 1103789Sahrens } 11043403Sbmc if (new_state != arc_anon) { 11053403Sbmc int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx); 11064309Smaybee uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1107789Sahrens 11081544Seschrock if (use_mutex) 11093403Sbmc mutex_enter(&new_state->arcs_mtx); 11101544Seschrock 11114309Smaybee list_insert_head(&new_state->arcs_list[ab->b_type], ab); 11121544Seschrock 11131544Seschrock /* ghost elements have a ghost size */ 11141544Seschrock if (GHOST_STATE(new_state)) { 11151544Seschrock ASSERT(ab->b_datacnt == 0); 11161544Seschrock ASSERT(ab->b_buf == NULL); 11171544Seschrock to_delta = ab->b_size; 11181544Seschrock } 11194309Smaybee atomic_add_64(size, to_delta); 11201544Seschrock 11211544Seschrock if (use_mutex) 11223403Sbmc mutex_exit(&new_state->arcs_mtx); 1123789Sahrens } 1124789Sahrens } 1125789Sahrens 1126789Sahrens ASSERT(!BUF_EMPTY(ab)); 1127*12296SLin.Ling@Sun.COM if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab)) 1128789Sahrens buf_hash_remove(ab); 1129789Sahrens 11301544Seschrock /* adjust state sizes */ 11311544Seschrock if (to_delta) 11323403Sbmc atomic_add_64(&new_state->arcs_size, to_delta); 11331544Seschrock if (from_delta) { 11343403Sbmc ASSERT3U(old_state->arcs_size, >=, from_delta); 11353403Sbmc atomic_add_64(&old_state->arcs_size, -from_delta); 1136789Sahrens } 1137789Sahrens ab->b_state = new_state; 11385450Sbrendan 11395450Sbrendan /* adjust l2arc hdr stats */ 11405450Sbrendan if (new_state == arc_l2c_only) 11415450Sbrendan l2arc_hdr_stat_add(); 11425450Sbrendan else if (old_state == arc_l2c_only) 11435450Sbrendan l2arc_hdr_stat_remove(); 1144789Sahrens } 1145789Sahrens 11464309Smaybee void 11478582SBrendan.Gregg@Sun.COM arc_space_consume(uint64_t space, arc_space_type_t type) 11484309Smaybee { 11498582SBrendan.Gregg@Sun.COM ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11508582SBrendan.Gregg@Sun.COM 11518582SBrendan.Gregg@Sun.COM switch (type) { 11528582SBrendan.Gregg@Sun.COM case ARC_SPACE_DATA: 11538582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, space); 11548582SBrendan.Gregg@Sun.COM break; 11558582SBrendan.Gregg@Sun.COM case ARC_SPACE_OTHER: 11568582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_other_size, space); 11578582SBrendan.Gregg@Sun.COM break; 11588582SBrendan.Gregg@Sun.COM case ARC_SPACE_HDRS: 11598582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_hdr_size, space); 11608582SBrendan.Gregg@Sun.COM break; 11618582SBrendan.Gregg@Sun.COM case ARC_SPACE_L2HDRS: 11628582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_hdr_size, space); 11638582SBrendan.Gregg@Sun.COM break; 11648582SBrendan.Gregg@Sun.COM } 11658582SBrendan.Gregg@Sun.COM 11664309Smaybee atomic_add_64(&arc_meta_used, space); 11674309Smaybee atomic_add_64(&arc_size, space); 11684309Smaybee } 11694309Smaybee 11704309Smaybee void 11718582SBrendan.Gregg@Sun.COM arc_space_return(uint64_t space, arc_space_type_t type) 11724309Smaybee { 11738582SBrendan.Gregg@Sun.COM ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11748582SBrendan.Gregg@Sun.COM 11758582SBrendan.Gregg@Sun.COM switch (type) { 11768582SBrendan.Gregg@Sun.COM case ARC_SPACE_DATA: 11778582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -space); 11788582SBrendan.Gregg@Sun.COM break; 11798582SBrendan.Gregg@Sun.COM case ARC_SPACE_OTHER: 11808582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_other_size, -space); 11818582SBrendan.Gregg@Sun.COM break; 11828582SBrendan.Gregg@Sun.COM case ARC_SPACE_HDRS: 11838582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_hdr_size, -space); 11848582SBrendan.Gregg@Sun.COM break; 11858582SBrendan.Gregg@Sun.COM case ARC_SPACE_L2HDRS: 11868582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 11878582SBrendan.Gregg@Sun.COM break; 11888582SBrendan.Gregg@Sun.COM } 11898582SBrendan.Gregg@Sun.COM 11904309Smaybee ASSERT(arc_meta_used >= space); 11914309Smaybee if (arc_meta_max < arc_meta_used) 11924309Smaybee arc_meta_max = arc_meta_used; 11934309Smaybee atomic_add_64(&arc_meta_used, -space); 11944309Smaybee ASSERT(arc_size >= space); 11954309Smaybee atomic_add_64(&arc_size, -space); 11964309Smaybee } 11974309Smaybee 11984309Smaybee void * 11994309Smaybee arc_data_buf_alloc(uint64_t size) 12004309Smaybee { 12014309Smaybee if (arc_evict_needed(ARC_BUFC_DATA)) 12024309Smaybee cv_signal(&arc_reclaim_thr_cv); 12034309Smaybee atomic_add_64(&arc_size, size); 12044309Smaybee return (zio_data_buf_alloc(size)); 12054309Smaybee } 12064309Smaybee 12074309Smaybee void 12084309Smaybee arc_data_buf_free(void *buf, uint64_t size) 12094309Smaybee { 12104309Smaybee zio_data_buf_free(buf, size); 12114309Smaybee ASSERT(arc_size >= size); 12124309Smaybee atomic_add_64(&arc_size, -size); 12134309Smaybee } 12144309Smaybee 1215789Sahrens arc_buf_t * 12163290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1217789Sahrens { 1218789Sahrens arc_buf_hdr_t *hdr; 1219789Sahrens arc_buf_t *buf; 1220789Sahrens 1221789Sahrens ASSERT3U(size, >, 0); 12226245Smaybee hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1223789Sahrens ASSERT(BUF_EMPTY(hdr)); 1224789Sahrens hdr->b_size = size; 12253290Sjohansen hdr->b_type = type; 12268636SMark.Maybee@Sun.COM hdr->b_spa = spa_guid(spa); 12273403Sbmc hdr->b_state = arc_anon; 1228789Sahrens hdr->b_arc_access = 0; 12296245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1230789Sahrens buf->b_hdr = hdr; 12312688Smaybee buf->b_data = NULL; 12321544Seschrock buf->b_efunc = NULL; 12331544Seschrock buf->b_private = NULL; 1234789Sahrens buf->b_next = NULL; 1235789Sahrens hdr->b_buf = buf; 12362688Smaybee arc_get_data_buf(buf); 12371544Seschrock hdr->b_datacnt = 1; 1238789Sahrens hdr->b_flags = 0; 1239789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1240789Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 1241789Sahrens 1242789Sahrens return (buf); 1243789Sahrens } 1244789Sahrens 12459412SAleksandr.Guzovskiy@Sun.COM static char *arc_onloan_tag = "onloan"; 12469412SAleksandr.Guzovskiy@Sun.COM 12479412SAleksandr.Guzovskiy@Sun.COM /* 12489412SAleksandr.Guzovskiy@Sun.COM * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 12499412SAleksandr.Guzovskiy@Sun.COM * flight data by arc_tempreserve_space() until they are "returned". Loaned 12509412SAleksandr.Guzovskiy@Sun.COM * buffers must be returned to the arc before they can be used by the DMU or 12519412SAleksandr.Guzovskiy@Sun.COM * freed. 12529412SAleksandr.Guzovskiy@Sun.COM */ 12539412SAleksandr.Guzovskiy@Sun.COM arc_buf_t * 12549412SAleksandr.Guzovskiy@Sun.COM arc_loan_buf(spa_t *spa, int size) 12559412SAleksandr.Guzovskiy@Sun.COM { 12569412SAleksandr.Guzovskiy@Sun.COM arc_buf_t *buf; 12579412SAleksandr.Guzovskiy@Sun.COM 12589412SAleksandr.Guzovskiy@Sun.COM buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 12599412SAleksandr.Guzovskiy@Sun.COM 12609412SAleksandr.Guzovskiy@Sun.COM atomic_add_64(&arc_loaned_bytes, size); 12619412SAleksandr.Guzovskiy@Sun.COM return (buf); 12629412SAleksandr.Guzovskiy@Sun.COM } 12639412SAleksandr.Guzovskiy@Sun.COM 12649412SAleksandr.Guzovskiy@Sun.COM /* 12659412SAleksandr.Guzovskiy@Sun.COM * Return a loaned arc buffer to the arc. 12669412SAleksandr.Guzovskiy@Sun.COM */ 12679412SAleksandr.Guzovskiy@Sun.COM void 12689412SAleksandr.Guzovskiy@Sun.COM arc_return_buf(arc_buf_t *buf, void *tag) 12699412SAleksandr.Guzovskiy@Sun.COM { 12709412SAleksandr.Guzovskiy@Sun.COM arc_buf_hdr_t *hdr = buf->b_hdr; 12719412SAleksandr.Guzovskiy@Sun.COM 12729412SAleksandr.Guzovskiy@Sun.COM ASSERT(buf->b_data != NULL); 127311539SChunli.Zhang@Sun.COM (void) refcount_add(&hdr->b_refcnt, tag); 127411539SChunli.Zhang@Sun.COM (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag); 12759412SAleksandr.Guzovskiy@Sun.COM 12769412SAleksandr.Guzovskiy@Sun.COM atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 12779412SAleksandr.Guzovskiy@Sun.COM } 12789412SAleksandr.Guzovskiy@Sun.COM 127911539SChunli.Zhang@Sun.COM /* Detach an arc_buf from a dbuf (tag) */ 128011539SChunli.Zhang@Sun.COM void 128111539SChunli.Zhang@Sun.COM arc_loan_inuse_buf(arc_buf_t *buf, void *tag) 128211539SChunli.Zhang@Sun.COM { 128311539SChunli.Zhang@Sun.COM arc_buf_hdr_t *hdr; 128411539SChunli.Zhang@Sun.COM 128511539SChunli.Zhang@Sun.COM ASSERT(buf->b_data != NULL); 128611539SChunli.Zhang@Sun.COM hdr = buf->b_hdr; 128711539SChunli.Zhang@Sun.COM (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag); 128811539SChunli.Zhang@Sun.COM (void) refcount_remove(&hdr->b_refcnt, tag); 128911539SChunli.Zhang@Sun.COM buf->b_efunc = NULL; 129011539SChunli.Zhang@Sun.COM buf->b_private = NULL; 129111539SChunli.Zhang@Sun.COM 129211539SChunli.Zhang@Sun.COM atomic_add_64(&arc_loaned_bytes, hdr->b_size); 129311539SChunli.Zhang@Sun.COM } 129411539SChunli.Zhang@Sun.COM 12952688Smaybee static arc_buf_t * 12962688Smaybee arc_buf_clone(arc_buf_t *from) 12971544Seschrock { 12982688Smaybee arc_buf_t *buf; 12992688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 13002688Smaybee uint64_t size = hdr->b_size; 13011544Seschrock 130210922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_state != arc_anon); 130310922SJeff.Bonwick@Sun.COM 13046245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 13052688Smaybee buf->b_hdr = hdr; 13062688Smaybee buf->b_data = NULL; 13072688Smaybee buf->b_efunc = NULL; 13082688Smaybee buf->b_private = NULL; 13092688Smaybee buf->b_next = hdr->b_buf; 13102688Smaybee hdr->b_buf = buf; 13112688Smaybee arc_get_data_buf(buf); 13122688Smaybee bcopy(from->b_data, buf->b_data, size); 13132688Smaybee hdr->b_datacnt += 1; 13142688Smaybee return (buf); 13151544Seschrock } 13161544Seschrock 13171544Seschrock void 13181544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 13191544Seschrock { 13202887Smaybee arc_buf_hdr_t *hdr; 13211544Seschrock kmutex_t *hash_lock; 13221544Seschrock 13232724Smaybee /* 13247545SMark.Maybee@Sun.COM * Check to see if this buffer is evicted. Callers 13257545SMark.Maybee@Sun.COM * must verify b_data != NULL to know if the add_ref 13267545SMark.Maybee@Sun.COM * was successful. 13272724Smaybee */ 1328*12296SLin.Ling@Sun.COM mutex_enter(&buf->b_evict_lock); 13297545SMark.Maybee@Sun.COM if (buf->b_data == NULL) { 1330*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 13312724Smaybee return; 13322887Smaybee } 1333*12296SLin.Ling@Sun.COM hash_lock = HDR_LOCK(buf->b_hdr); 1334*12296SLin.Ling@Sun.COM mutex_enter(hash_lock); 13357545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 1336*12296SLin.Ling@Sun.COM ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1337*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 13387545SMark.Maybee@Sun.COM 13393403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 13401544Seschrock add_reference(hdr, hash_lock, tag); 13418582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 13422688Smaybee arc_access(hdr, hash_lock); 13432688Smaybee mutex_exit(hash_lock); 13443403Sbmc ARCSTAT_BUMP(arcstat_hits); 13453403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 13463403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 13473403Sbmc data, metadata, hits); 13481544Seschrock } 13491544Seschrock 13505450Sbrendan /* 13515450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 13525450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 13535450Sbrendan */ 13545450Sbrendan static void 13555450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 13565450Sbrendan void *data, size_t size) 13575450Sbrendan { 13585450Sbrendan if (HDR_L2_WRITING(hdr)) { 13595450Sbrendan l2arc_data_free_t *df; 13605450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 13615450Sbrendan df->l2df_data = data; 13625450Sbrendan df->l2df_size = size; 13635450Sbrendan df->l2df_func = free_func; 13645450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 13655450Sbrendan list_insert_head(l2arc_free_on_write, df); 13665450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 13675450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 13685450Sbrendan } else { 13695450Sbrendan free_func(data, size); 13705450Sbrendan } 13715450Sbrendan } 13725450Sbrendan 1373789Sahrens static void 13742688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 13751544Seschrock { 13761544Seschrock arc_buf_t **bufp; 13771544Seschrock 13781544Seschrock /* free up data associated with the buf */ 13791544Seschrock if (buf->b_data) { 13801544Seschrock arc_state_t *state = buf->b_hdr->b_state; 13811544Seschrock uint64_t size = buf->b_hdr->b_size; 13823290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 13831544Seschrock 13843093Sahrens arc_cksum_verify(buf); 138510922SJeff.Bonwick@Sun.COM 13862688Smaybee if (!recycle) { 13873290Sjohansen if (type == ARC_BUFC_METADATA) { 13885450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 13895450Sbrendan buf->b_data, size); 13908582SBrendan.Gregg@Sun.COM arc_space_return(size, ARC_SPACE_DATA); 13913290Sjohansen } else { 13923290Sjohansen ASSERT(type == ARC_BUFC_DATA); 13935450Sbrendan arc_buf_data_free(buf->b_hdr, 13945450Sbrendan zio_data_buf_free, buf->b_data, size); 13958582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -size); 13964309Smaybee atomic_add_64(&arc_size, -size); 13973290Sjohansen } 13982688Smaybee } 13991544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 14004309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 14014309Smaybee 14021544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 14033403Sbmc ASSERT(state != arc_anon); 14044309Smaybee 14054309Smaybee ASSERT3U(*cnt, >=, size); 14064309Smaybee atomic_add_64(cnt, -size); 14071544Seschrock } 14083403Sbmc ASSERT3U(state->arcs_size, >=, size); 14093403Sbmc atomic_add_64(&state->arcs_size, -size); 14101544Seschrock buf->b_data = NULL; 14111544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 14121544Seschrock buf->b_hdr->b_datacnt -= 1; 14131544Seschrock } 14141544Seschrock 14151544Seschrock /* only remove the buf if requested */ 14161544Seschrock if (!all) 14171544Seschrock return; 14181544Seschrock 14191544Seschrock /* remove the buf from the hdr list */ 14201544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 14211544Seschrock continue; 14221544Seschrock *bufp = buf->b_next; 1423*12296SLin.Ling@Sun.COM buf->b_next = NULL; 14241544Seschrock 14251544Seschrock ASSERT(buf->b_efunc == NULL); 14261544Seschrock 14271544Seschrock /* clean up the buf */ 14281544Seschrock buf->b_hdr = NULL; 14291544Seschrock kmem_cache_free(buf_cache, buf); 14301544Seschrock } 14311544Seschrock 14321544Seschrock static void 14331544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1434789Sahrens { 1435789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 14363403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 14371544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 143810922SJeff.Bonwick@Sun.COM l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 143910922SJeff.Bonwick@Sun.COM 144010922SJeff.Bonwick@Sun.COM if (l2hdr != NULL) { 144110922SJeff.Bonwick@Sun.COM boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx); 144210922SJeff.Bonwick@Sun.COM /* 144310922SJeff.Bonwick@Sun.COM * To prevent arc_free() and l2arc_evict() from 144410922SJeff.Bonwick@Sun.COM * attempting to free the same buffer at the same time, 144510922SJeff.Bonwick@Sun.COM * a FREE_IN_PROGRESS flag is given to arc_free() to 144610922SJeff.Bonwick@Sun.COM * give it priority. l2arc_evict() can't destroy this 144710922SJeff.Bonwick@Sun.COM * header while we are waiting on l2arc_buflist_mtx. 144810922SJeff.Bonwick@Sun.COM * 144910922SJeff.Bonwick@Sun.COM * The hdr may be removed from l2ad_buflist before we 145010922SJeff.Bonwick@Sun.COM * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 145110922SJeff.Bonwick@Sun.COM */ 145210922SJeff.Bonwick@Sun.COM if (!buflist_held) { 14535450Sbrendan mutex_enter(&l2arc_buflist_mtx); 145410922SJeff.Bonwick@Sun.COM l2hdr = hdr->b_l2hdr; 145510922SJeff.Bonwick@Sun.COM } 145610922SJeff.Bonwick@Sun.COM 145710922SJeff.Bonwick@Sun.COM if (l2hdr != NULL) { 145810922SJeff.Bonwick@Sun.COM list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 145910922SJeff.Bonwick@Sun.COM ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 146010922SJeff.Bonwick@Sun.COM kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 146110922SJeff.Bonwick@Sun.COM if (hdr->b_state == arc_l2c_only) 146210922SJeff.Bonwick@Sun.COM l2arc_hdr_stat_remove(); 146310922SJeff.Bonwick@Sun.COM hdr->b_l2hdr = NULL; 146410922SJeff.Bonwick@Sun.COM } 146510922SJeff.Bonwick@Sun.COM 146610922SJeff.Bonwick@Sun.COM if (!buflist_held) 14675450Sbrendan mutex_exit(&l2arc_buflist_mtx); 14685450Sbrendan } 14695450Sbrendan 1470789Sahrens if (!BUF_EMPTY(hdr)) { 14711544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1472*12296SLin.Ling@Sun.COM buf_discard_identity(hdr); 1473789Sahrens } 14741544Seschrock while (hdr->b_buf) { 1475789Sahrens arc_buf_t *buf = hdr->b_buf; 1476789Sahrens 14771544Seschrock if (buf->b_efunc) { 14781544Seschrock mutex_enter(&arc_eviction_mtx); 1479*12296SLin.Ling@Sun.COM mutex_enter(&buf->b_evict_lock); 14801544Seschrock ASSERT(buf->b_hdr != NULL); 14812688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 14821544Seschrock hdr->b_buf = buf->b_next; 14832887Smaybee buf->b_hdr = &arc_eviction_hdr; 14841544Seschrock buf->b_next = arc_eviction_list; 14851544Seschrock arc_eviction_list = buf; 1486*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 14871544Seschrock mutex_exit(&arc_eviction_mtx); 14881544Seschrock } else { 14892688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 14901544Seschrock } 1491789Sahrens } 14923093Sahrens if (hdr->b_freeze_cksum != NULL) { 14933093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 14943093Sahrens hdr->b_freeze_cksum = NULL; 14953093Sahrens } 1496*12296SLin.Ling@Sun.COM if (hdr->b_thawed) { 1497*12296SLin.Ling@Sun.COM kmem_free(hdr->b_thawed, 1); 1498*12296SLin.Ling@Sun.COM hdr->b_thawed = NULL; 1499*12296SLin.Ling@Sun.COM } 15001544Seschrock 1501789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1502789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1503789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1504789Sahrens kmem_cache_free(hdr_cache, hdr); 1505789Sahrens } 1506789Sahrens 1507789Sahrens void 1508789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1509789Sahrens { 1510789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 15113403Sbmc int hashed = hdr->b_state != arc_anon; 15121544Seschrock 15131544Seschrock ASSERT(buf->b_efunc == NULL); 15141544Seschrock ASSERT(buf->b_data != NULL); 15151544Seschrock 15161544Seschrock if (hashed) { 15171544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 15181544Seschrock 15191544Seschrock mutex_enter(hash_lock); 1520*12296SLin.Ling@Sun.COM hdr = buf->b_hdr; 1521*12296SLin.Ling@Sun.COM ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1522*12296SLin.Ling@Sun.COM 15231544Seschrock (void) remove_reference(hdr, hash_lock, tag); 152410922SJeff.Bonwick@Sun.COM if (hdr->b_datacnt > 1) { 15252688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 152610922SJeff.Bonwick@Sun.COM } else { 152710922SJeff.Bonwick@Sun.COM ASSERT(buf == hdr->b_buf); 152810922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 15291544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 153010922SJeff.Bonwick@Sun.COM } 15311544Seschrock mutex_exit(hash_lock); 15321544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 15331544Seschrock int destroy_hdr; 15341544Seschrock /* 15351544Seschrock * We are in the middle of an async write. Don't destroy 15361544Seschrock * this buffer unless the write completes before we finish 15371544Seschrock * decrementing the reference count. 15381544Seschrock */ 15391544Seschrock mutex_enter(&arc_eviction_mtx); 15401544Seschrock (void) remove_reference(hdr, NULL, tag); 15411544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 15421544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 15431544Seschrock mutex_exit(&arc_eviction_mtx); 15441544Seschrock if (destroy_hdr) 15451544Seschrock arc_hdr_destroy(hdr); 15461544Seschrock } else { 1547*12296SLin.Ling@Sun.COM if (remove_reference(hdr, NULL, tag) > 0) 15482688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 1549*12296SLin.Ling@Sun.COM else 15501544Seschrock arc_hdr_destroy(hdr); 15511544Seschrock } 15521544Seschrock } 15531544Seschrock 15541544Seschrock int 15551544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 15561544Seschrock { 15571544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1558789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 15591544Seschrock int no_callback = (buf->b_efunc == NULL); 15601544Seschrock 15613403Sbmc if (hdr->b_state == arc_anon) { 156210922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 15631544Seschrock arc_buf_free(buf, tag); 15641544Seschrock return (no_callback); 15651544Seschrock } 1566789Sahrens 1567789Sahrens mutex_enter(hash_lock); 1568*12296SLin.Ling@Sun.COM hdr = buf->b_hdr; 1569*12296SLin.Ling@Sun.COM ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 15703403Sbmc ASSERT(hdr->b_state != arc_anon); 15711544Seschrock ASSERT(buf->b_data != NULL); 1572789Sahrens 15731544Seschrock (void) remove_reference(hdr, hash_lock, tag); 15741544Seschrock if (hdr->b_datacnt > 1) { 15751544Seschrock if (no_callback) 15762688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 15771544Seschrock } else if (no_callback) { 15781544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 157910922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 15801544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1581789Sahrens } 15821544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 15831544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1584789Sahrens mutex_exit(hash_lock); 15851544Seschrock return (no_callback); 1586789Sahrens } 1587789Sahrens 1588789Sahrens int 1589789Sahrens arc_buf_size(arc_buf_t *buf) 1590789Sahrens { 1591789Sahrens return (buf->b_hdr->b_size); 1592789Sahrens } 1593789Sahrens 1594789Sahrens /* 1595789Sahrens * Evict buffers from list until we've removed the specified number of 1596789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 15972688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 15982688Smaybee * - look for a buffer to evict that is `bytes' long. 15992688Smaybee * - return the data block from this buffer rather than freeing it. 16002688Smaybee * This flag is used by callers that are trying to make space for a 16012688Smaybee * new buffer in a full arc cache. 16025642Smaybee * 16035642Smaybee * This function makes a "best effort". It skips over any buffers 16045642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 16055642Smaybee * It may also return without evicting as much space as requested. 1606789Sahrens */ 16072688Smaybee static void * 16088636SMark.Maybee@Sun.COM arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 16093290Sjohansen arc_buf_contents_t type) 1610789Sahrens { 1611789Sahrens arc_state_t *evicted_state; 16122688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 16132918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 16144309Smaybee list_t *list = &state->arcs_list[type]; 1615789Sahrens kmutex_t *hash_lock; 16162688Smaybee boolean_t have_lock; 16172918Smaybee void *stolen = NULL; 1618789Sahrens 16193403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1620789Sahrens 16213403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1622789Sahrens 16233403Sbmc mutex_enter(&state->arcs_mtx); 16243403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1625789Sahrens 16264309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 16274309Smaybee ab_prev = list_prev(list, ab); 16282391Smaybee /* prefetch buffers have a minimum lifespan */ 16292688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 16305642Smaybee (spa && ab->b_spa != spa) || 16312688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 163211066Srafael.vanoni@sun.com ddi_get_lbolt() - ab->b_arc_access < 163311066Srafael.vanoni@sun.com arc_min_prefetch_lifespan)) { 16342391Smaybee skipped++; 16352391Smaybee continue; 16362391Smaybee } 16372918Smaybee /* "lookahead" for better eviction candidate */ 16382918Smaybee if (recycle && ab->b_size != bytes && 16392918Smaybee ab_prev && ab_prev->b_size == bytes) 16402688Smaybee continue; 1641789Sahrens hash_lock = HDR_LOCK(ab); 16422688Smaybee have_lock = MUTEX_HELD(hash_lock); 16432688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1644789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 16451544Seschrock ASSERT(ab->b_datacnt > 0); 16461544Seschrock while (ab->b_buf) { 16471544Seschrock arc_buf_t *buf = ab->b_buf; 1648*12296SLin.Ling@Sun.COM if (!mutex_tryenter(&buf->b_evict_lock)) { 16497545SMark.Maybee@Sun.COM missed += 1; 16507545SMark.Maybee@Sun.COM break; 16517545SMark.Maybee@Sun.COM } 16522688Smaybee if (buf->b_data) { 16531544Seschrock bytes_evicted += ab->b_size; 16543290Sjohansen if (recycle && ab->b_type == type && 16555450Sbrendan ab->b_size == bytes && 16565450Sbrendan !HDR_L2_WRITING(ab)) { 16572918Smaybee stolen = buf->b_data; 16582918Smaybee recycle = FALSE; 16592918Smaybee } 16602688Smaybee } 16611544Seschrock if (buf->b_efunc) { 16621544Seschrock mutex_enter(&arc_eviction_mtx); 16632918Smaybee arc_buf_destroy(buf, 16642918Smaybee buf->b_data == stolen, FALSE); 16651544Seschrock ab->b_buf = buf->b_next; 16662887Smaybee buf->b_hdr = &arc_eviction_hdr; 16671544Seschrock buf->b_next = arc_eviction_list; 16681544Seschrock arc_eviction_list = buf; 16691544Seschrock mutex_exit(&arc_eviction_mtx); 1670*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 16711544Seschrock } else { 1672*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 16732918Smaybee arc_buf_destroy(buf, 16742918Smaybee buf->b_data == stolen, TRUE); 16751544Seschrock } 16761544Seschrock } 167710357SBrendan.Gregg@Sun.COM 167810357SBrendan.Gregg@Sun.COM if (ab->b_l2hdr) { 167910357SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_evict_l2_cached, 168010357SBrendan.Gregg@Sun.COM ab->b_size); 168110357SBrendan.Gregg@Sun.COM } else { 168210357SBrendan.Gregg@Sun.COM if (l2arc_write_eligible(ab->b_spa, ab)) { 168310357SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_evict_l2_eligible, 168410357SBrendan.Gregg@Sun.COM ab->b_size); 168510357SBrendan.Gregg@Sun.COM } else { 168610357SBrendan.Gregg@Sun.COM ARCSTAT_INCR( 168710357SBrendan.Gregg@Sun.COM arcstat_evict_l2_ineligible, 168810357SBrendan.Gregg@Sun.COM ab->b_size); 168910357SBrendan.Gregg@Sun.COM } 169010357SBrendan.Gregg@Sun.COM } 169110357SBrendan.Gregg@Sun.COM 16927545SMark.Maybee@Sun.COM if (ab->b_datacnt == 0) { 16937545SMark.Maybee@Sun.COM arc_change_state(evicted_state, ab, hash_lock); 16947545SMark.Maybee@Sun.COM ASSERT(HDR_IN_HASH_TABLE(ab)); 16957545SMark.Maybee@Sun.COM ab->b_flags |= ARC_IN_HASH_TABLE; 16967545SMark.Maybee@Sun.COM ab->b_flags &= ~ARC_BUF_AVAILABLE; 16977545SMark.Maybee@Sun.COM DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 16987545SMark.Maybee@Sun.COM } 16992688Smaybee if (!have_lock) 17002688Smaybee mutex_exit(hash_lock); 17011544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1702789Sahrens break; 1703789Sahrens } else { 17042688Smaybee missed += 1; 1705789Sahrens } 1706789Sahrens } 17073403Sbmc 17083403Sbmc mutex_exit(&evicted_state->arcs_mtx); 17093403Sbmc mutex_exit(&state->arcs_mtx); 1710789Sahrens 1711789Sahrens if (bytes_evicted < bytes) 1712789Sahrens dprintf("only evicted %lld bytes from %x", 1713789Sahrens (longlong_t)bytes_evicted, state); 1714789Sahrens 17152688Smaybee if (skipped) 17163403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 17173403Sbmc 17182688Smaybee if (missed) 17193403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 17203403Sbmc 17214709Smaybee /* 17224709Smaybee * We have just evicted some date into the ghost state, make 17234709Smaybee * sure we also adjust the ghost state size if necessary. 17244709Smaybee */ 17254709Smaybee if (arc_no_grow && 17264709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 17274709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 17284709Smaybee arc_mru_ghost->arcs_size - arc_c; 17294709Smaybee 17304709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 17314709Smaybee int64_t todelete = 17324709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 17335642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 17344709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 17354709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 17364709Smaybee arc_mru_ghost->arcs_size + 17374709Smaybee arc_mfu_ghost->arcs_size - arc_c); 17385642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 17394709Smaybee } 17404709Smaybee } 17414709Smaybee 17422918Smaybee return (stolen); 1743789Sahrens } 1744789Sahrens 1745789Sahrens /* 1746789Sahrens * Remove buffers from list until we've removed the specified number of 1747789Sahrens * bytes. Destroy the buffers that are removed. 1748789Sahrens */ 1749789Sahrens static void 17508636SMark.Maybee@Sun.COM arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 1751789Sahrens { 1752789Sahrens arc_buf_hdr_t *ab, *ab_prev; 17534309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1754789Sahrens kmutex_t *hash_lock; 17551544Seschrock uint64_t bytes_deleted = 0; 17563700Sek110237 uint64_t bufs_skipped = 0; 1757789Sahrens 17581544Seschrock ASSERT(GHOST_STATE(state)); 1759789Sahrens top: 17603403Sbmc mutex_enter(&state->arcs_mtx); 17614309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 17624309Smaybee ab_prev = list_prev(list, ab); 17635642Smaybee if (spa && ab->b_spa != spa) 17645642Smaybee continue; 1765789Sahrens hash_lock = HDR_LOCK(ab); 176612033Swilliam.gorrell@sun.com /* caller may be trying to modify this buffer, skip it */ 176712033Swilliam.gorrell@sun.com if (MUTEX_HELD(hash_lock)) 176812033Swilliam.gorrell@sun.com continue; 176912033Swilliam.gorrell@sun.com if (mutex_tryenter(hash_lock)) { 17702391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 17711544Seschrock ASSERT(ab->b_buf == NULL); 17723403Sbmc ARCSTAT_BUMP(arcstat_deleted); 17731544Seschrock bytes_deleted += ab->b_size; 17745450Sbrendan 17755450Sbrendan if (ab->b_l2hdr != NULL) { 17765450Sbrendan /* 17775450Sbrendan * This buffer is cached on the 2nd Level ARC; 17785450Sbrendan * don't destroy the header. 17795450Sbrendan */ 17805450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 178112033Swilliam.gorrell@sun.com mutex_exit(hash_lock); 17825450Sbrendan } else { 17835450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 178412033Swilliam.gorrell@sun.com mutex_exit(hash_lock); 17855450Sbrendan arc_hdr_destroy(ab); 17865450Sbrendan } 17875450Sbrendan 1788789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1789789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1790789Sahrens break; 1791789Sahrens } else { 1792789Sahrens if (bytes < 0) { 17933403Sbmc mutex_exit(&state->arcs_mtx); 1794789Sahrens mutex_enter(hash_lock); 1795789Sahrens mutex_exit(hash_lock); 1796789Sahrens goto top; 1797789Sahrens } 1798789Sahrens bufs_skipped += 1; 1799789Sahrens } 1800789Sahrens } 18013403Sbmc mutex_exit(&state->arcs_mtx); 1802789Sahrens 18034309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 18044309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 18054309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 18064309Smaybee goto top; 18074309Smaybee } 18084309Smaybee 1809789Sahrens if (bufs_skipped) { 18103403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1811789Sahrens ASSERT(bytes >= 0); 1812789Sahrens } 1813789Sahrens 1814789Sahrens if (bytes_deleted < bytes) 1815789Sahrens dprintf("only deleted %lld bytes from %p", 1816789Sahrens (longlong_t)bytes_deleted, state); 1817789Sahrens } 1818789Sahrens 1819789Sahrens static void 1820789Sahrens arc_adjust(void) 1821789Sahrens { 18228582SBrendan.Gregg@Sun.COM int64_t adjustment, delta; 18238582SBrendan.Gregg@Sun.COM 18248582SBrendan.Gregg@Sun.COM /* 18258582SBrendan.Gregg@Sun.COM * Adjust MRU size 18268582SBrendan.Gregg@Sun.COM */ 18278582SBrendan.Gregg@Sun.COM 18288582SBrendan.Gregg@Sun.COM adjustment = MIN(arc_size - arc_c, 18298582SBrendan.Gregg@Sun.COM arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - arc_p); 18308582SBrendan.Gregg@Sun.COM 18318582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 18328582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 18338582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA); 18348582SBrendan.Gregg@Sun.COM adjustment -= delta; 18354309Smaybee } 18364309Smaybee 18378582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 18388582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 18398582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, 18405642Smaybee ARC_BUFC_METADATA); 1841789Sahrens } 1842789Sahrens 18438582SBrendan.Gregg@Sun.COM /* 18448582SBrendan.Gregg@Sun.COM * Adjust MFU size 18458582SBrendan.Gregg@Sun.COM */ 18468582SBrendan.Gregg@Sun.COM 18478582SBrendan.Gregg@Sun.COM adjustment = arc_size - arc_c; 18488582SBrendan.Gregg@Sun.COM 18498582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 18508582SBrendan.Gregg@Sun.COM delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 18518582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA); 18528582SBrendan.Gregg@Sun.COM adjustment -= delta; 18538582SBrendan.Gregg@Sun.COM } 18548582SBrendan.Gregg@Sun.COM 18558582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 18568582SBrendan.Gregg@Sun.COM int64_t delta = MIN(adjustment, 18578582SBrendan.Gregg@Sun.COM arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 18588582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, 18598582SBrendan.Gregg@Sun.COM ARC_BUFC_METADATA); 18608582SBrendan.Gregg@Sun.COM } 18618582SBrendan.Gregg@Sun.COM 18628582SBrendan.Gregg@Sun.COM /* 18638582SBrendan.Gregg@Sun.COM * Adjust ghost lists 18648582SBrendan.Gregg@Sun.COM */ 18658582SBrendan.Gregg@Sun.COM 18668582SBrendan.Gregg@Sun.COM adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 18678582SBrendan.Gregg@Sun.COM 18688582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 18698582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru_ghost->arcs_size, adjustment); 18708582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mru_ghost, NULL, delta); 18718582SBrendan.Gregg@Sun.COM } 18728582SBrendan.Gregg@Sun.COM 18738582SBrendan.Gregg@Sun.COM adjustment = 18748582SBrendan.Gregg@Sun.COM arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 18758582SBrendan.Gregg@Sun.COM 18768582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 18778582SBrendan.Gregg@Sun.COM delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 18788582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mfu_ghost, NULL, delta); 1879789Sahrens } 1880789Sahrens } 1881789Sahrens 18821544Seschrock static void 18831544Seschrock arc_do_user_evicts(void) 18841544Seschrock { 18851544Seschrock mutex_enter(&arc_eviction_mtx); 18861544Seschrock while (arc_eviction_list != NULL) { 18871544Seschrock arc_buf_t *buf = arc_eviction_list; 18881544Seschrock arc_eviction_list = buf->b_next; 1889*12296SLin.Ling@Sun.COM mutex_enter(&buf->b_evict_lock); 18901544Seschrock buf->b_hdr = NULL; 1891*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 18921544Seschrock mutex_exit(&arc_eviction_mtx); 18931544Seschrock 18941819Smaybee if (buf->b_efunc != NULL) 18951819Smaybee VERIFY(buf->b_efunc(buf) == 0); 18961544Seschrock 18971544Seschrock buf->b_efunc = NULL; 18981544Seschrock buf->b_private = NULL; 18991544Seschrock kmem_cache_free(buf_cache, buf); 19001544Seschrock mutex_enter(&arc_eviction_mtx); 19011544Seschrock } 19021544Seschrock mutex_exit(&arc_eviction_mtx); 19031544Seschrock } 19041544Seschrock 1905789Sahrens /* 19065642Smaybee * Flush all *evictable* data from the cache for the given spa. 1907789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1908789Sahrens */ 1909789Sahrens void 19105642Smaybee arc_flush(spa_t *spa) 1911789Sahrens { 19128636SMark.Maybee@Sun.COM uint64_t guid = 0; 19138636SMark.Maybee@Sun.COM 19148636SMark.Maybee@Sun.COM if (spa) 19158636SMark.Maybee@Sun.COM guid = spa_guid(spa); 19168636SMark.Maybee@Sun.COM 19175642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 19188636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 19195642Smaybee if (spa) 19205642Smaybee break; 19215642Smaybee } 19225642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 19238636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 19245642Smaybee if (spa) 19255642Smaybee break; 19265642Smaybee } 19275642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 19288636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 19295642Smaybee if (spa) 19305642Smaybee break; 19315642Smaybee } 19325642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 19338636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 19345642Smaybee if (spa) 19355642Smaybee break; 19365642Smaybee } 19375642Smaybee 19388636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mru_ghost, guid, -1); 19398636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mfu_ghost, guid, -1); 19401544Seschrock 19411544Seschrock mutex_enter(&arc_reclaim_thr_lock); 19421544Seschrock arc_do_user_evicts(); 19431544Seschrock mutex_exit(&arc_reclaim_thr_lock); 19445642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1945789Sahrens } 1946789Sahrens 1947789Sahrens void 19483158Smaybee arc_shrink(void) 1949789Sahrens { 19503403Sbmc if (arc_c > arc_c_min) { 19513158Smaybee uint64_t to_free; 1952789Sahrens 19532048Sstans #ifdef _KERNEL 19543403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 19552048Sstans #else 19563403Sbmc to_free = arc_c >> arc_shrink_shift; 19572048Sstans #endif 19583403Sbmc if (arc_c > arc_c_min + to_free) 19593403Sbmc atomic_add_64(&arc_c, -to_free); 19603158Smaybee else 19613403Sbmc arc_c = arc_c_min; 19622048Sstans 19633403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 19643403Sbmc if (arc_c > arc_size) 19653403Sbmc arc_c = MAX(arc_size, arc_c_min); 19663403Sbmc if (arc_p > arc_c) 19673403Sbmc arc_p = (arc_c >> 1); 19683403Sbmc ASSERT(arc_c >= arc_c_min); 19693403Sbmc ASSERT((int64_t)arc_p >= 0); 19703158Smaybee } 1971789Sahrens 19723403Sbmc if (arc_size > arc_c) 19733158Smaybee arc_adjust(); 1974789Sahrens } 1975789Sahrens 1976789Sahrens static int 1977789Sahrens arc_reclaim_needed(void) 1978789Sahrens { 1979789Sahrens uint64_t extra; 1980789Sahrens 1981789Sahrens #ifdef _KERNEL 19822048Sstans 19832048Sstans if (needfree) 19842048Sstans return (1); 19852048Sstans 1986789Sahrens /* 1987789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1988789Sahrens */ 1989789Sahrens extra = desfree; 1990789Sahrens 1991789Sahrens /* 1992789Sahrens * check that we're out of range of the pageout scanner. It starts to 1993789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1994789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1995789Sahrens * number of needed free pages. We add extra pages here to make sure 1996789Sahrens * the scanner doesn't start up while we're freeing memory. 1997789Sahrens */ 1998789Sahrens if (freemem < lotsfree + needfree + extra) 1999789Sahrens return (1); 2000789Sahrens 2001789Sahrens /* 2002789Sahrens * check to make sure that swapfs has enough space so that anon 20035450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 2004789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 2005789Sahrens * swap pages. We also add a bit of extra here just to prevent 2006789Sahrens * circumstances from getting really dire. 2007789Sahrens */ 2008789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 2009789Sahrens return (1); 2010789Sahrens 20111936Smaybee #if defined(__i386) 2012789Sahrens /* 2013789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 2014789Sahrens * kernel heap space before we ever run out of available physical 2015789Sahrens * memory. Most checks of the size of the heap_area compare against 2016789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 2017789Sahrens * can have in the system. However, this is generally fixed at 25 pages 2018789Sahrens * which is so low that it's useless. In this comparison, we seek to 2019789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 20205450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 2021789Sahrens * free) 2022789Sahrens */ 2023789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 2024789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 2025789Sahrens return (1); 2026789Sahrens #endif 2027789Sahrens 2028789Sahrens #else 2029789Sahrens if (spa_get_random(100) == 0) 2030789Sahrens return (1); 2031789Sahrens #endif 2032789Sahrens return (0); 2033789Sahrens } 2034789Sahrens 2035789Sahrens static void 2036789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 2037789Sahrens { 2038789Sahrens size_t i; 2039789Sahrens kmem_cache_t *prev_cache = NULL; 20403290Sjohansen kmem_cache_t *prev_data_cache = NULL; 2041789Sahrens extern kmem_cache_t *zio_buf_cache[]; 20423290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 2043789Sahrens 20441484Sek110237 #ifdef _KERNEL 20454309Smaybee if (arc_meta_used >= arc_meta_limit) { 20464309Smaybee /* 20474309Smaybee * We are exceeding our meta-data cache limit. 20484309Smaybee * Purge some DNLC entries to release holds on meta-data. 20494309Smaybee */ 20504309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 20514309Smaybee } 20521936Smaybee #if defined(__i386) 20531936Smaybee /* 20541936Smaybee * Reclaim unused memory from all kmem caches. 20551936Smaybee */ 20561936Smaybee kmem_reap(); 20571936Smaybee #endif 20581484Sek110237 #endif 20591484Sek110237 2060789Sahrens /* 20615450Sbrendan * An aggressive reclamation will shrink the cache size as well as 20621544Seschrock * reap free buffers from the arc kmem caches. 2063789Sahrens */ 2064789Sahrens if (strat == ARC_RECLAIM_AGGR) 20653158Smaybee arc_shrink(); 2066789Sahrens 2067789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2068789Sahrens if (zio_buf_cache[i] != prev_cache) { 2069789Sahrens prev_cache = zio_buf_cache[i]; 2070789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 2071789Sahrens } 20723290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 20733290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 20743290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 20753290Sjohansen } 2076789Sahrens } 20771544Seschrock kmem_cache_reap_now(buf_cache); 20781544Seschrock kmem_cache_reap_now(hdr_cache); 2079789Sahrens } 2080789Sahrens 2081789Sahrens static void 2082789Sahrens arc_reclaim_thread(void) 2083789Sahrens { 2084789Sahrens clock_t growtime = 0; 2085789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2086789Sahrens callb_cpr_t cpr; 2087789Sahrens 2088789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2089789Sahrens 2090789Sahrens mutex_enter(&arc_reclaim_thr_lock); 2091789Sahrens while (arc_thread_exit == 0) { 2092789Sahrens if (arc_reclaim_needed()) { 2093789Sahrens 20943403Sbmc if (arc_no_grow) { 2095789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 2096789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2097789Sahrens } else { 2098789Sahrens last_reclaim = ARC_RECLAIM_CONS; 2099789Sahrens } 2100789Sahrens } else { 21013403Sbmc arc_no_grow = TRUE; 2102789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2103789Sahrens membar_producer(); 2104789Sahrens } 2105789Sahrens 2106789Sahrens /* reset the growth delay for every reclaim */ 210711066Srafael.vanoni@sun.com growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 2108789Sahrens 2109789Sahrens arc_kmem_reap_now(last_reclaim); 21106987Sbrendan arc_warm = B_TRUE; 2111789Sahrens 211211066Srafael.vanoni@sun.com } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 21133403Sbmc arc_no_grow = FALSE; 2114789Sahrens } 2115789Sahrens 21163403Sbmc if (2 * arc_c < arc_size + 21173403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 21183298Smaybee arc_adjust(); 21193298Smaybee 21201544Seschrock if (arc_eviction_list != NULL) 21211544Seschrock arc_do_user_evicts(); 21221544Seschrock 2123789Sahrens /* block until needed, or one second, whichever is shorter */ 2124789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 2125789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 212611066Srafael.vanoni@sun.com &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz)); 2127789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2128789Sahrens } 2129789Sahrens 2130789Sahrens arc_thread_exit = 0; 2131789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 2132789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2133789Sahrens thread_exit(); 2134789Sahrens } 2135789Sahrens 21361544Seschrock /* 21371544Seschrock * Adapt arc info given the number of bytes we are trying to add and 21381544Seschrock * the state that we are comming from. This function is only called 21391544Seschrock * when we are adding new content to the cache. 21401544Seschrock */ 2141789Sahrens static void 21421544Seschrock arc_adapt(int bytes, arc_state_t *state) 2143789Sahrens { 21441544Seschrock int mult; 21458582SBrendan.Gregg@Sun.COM uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 21461544Seschrock 21475450Sbrendan if (state == arc_l2c_only) 21485450Sbrendan return; 21495450Sbrendan 21501544Seschrock ASSERT(bytes > 0); 2151789Sahrens /* 21521544Seschrock * Adapt the target size of the MRU list: 21531544Seschrock * - if we just hit in the MRU ghost list, then increase 21541544Seschrock * the target size of the MRU list. 21551544Seschrock * - if we just hit in the MFU ghost list, then increase 21561544Seschrock * the target size of the MFU list by decreasing the 21571544Seschrock * target size of the MRU list. 2158789Sahrens */ 21593403Sbmc if (state == arc_mru_ghost) { 21603403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 21613403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 21621544Seschrock 21638582SBrendan.Gregg@Sun.COM arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 21643403Sbmc } else if (state == arc_mfu_ghost) { 21658582SBrendan.Gregg@Sun.COM uint64_t delta; 21668582SBrendan.Gregg@Sun.COM 21673403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 21683403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 21691544Seschrock 21708582SBrendan.Gregg@Sun.COM delta = MIN(bytes * mult, arc_p); 21718582SBrendan.Gregg@Sun.COM arc_p = MAX(arc_p_min, arc_p - delta); 21721544Seschrock } 21733403Sbmc ASSERT((int64_t)arc_p >= 0); 2174789Sahrens 2175789Sahrens if (arc_reclaim_needed()) { 2176789Sahrens cv_signal(&arc_reclaim_thr_cv); 2177789Sahrens return; 2178789Sahrens } 2179789Sahrens 21803403Sbmc if (arc_no_grow) 2181789Sahrens return; 2182789Sahrens 21833403Sbmc if (arc_c >= arc_c_max) 21841544Seschrock return; 21851544Seschrock 2186789Sahrens /* 21871544Seschrock * If we're within (2 * maxblocksize) bytes of the target 21881544Seschrock * cache size, increment the target cache size 2189789Sahrens */ 21903403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 21913403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 21923403Sbmc if (arc_c > arc_c_max) 21933403Sbmc arc_c = arc_c_max; 21943403Sbmc else if (state == arc_anon) 21953403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 21963403Sbmc if (arc_p > arc_c) 21973403Sbmc arc_p = arc_c; 2198789Sahrens } 21993403Sbmc ASSERT((int64_t)arc_p >= 0); 2200789Sahrens } 2201789Sahrens 2202789Sahrens /* 22031544Seschrock * Check if the cache has reached its limits and eviction is required 22041544Seschrock * prior to insert. 2205789Sahrens */ 2206789Sahrens static int 22074309Smaybee arc_evict_needed(arc_buf_contents_t type) 2208789Sahrens { 22094309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 22104309Smaybee return (1); 22114309Smaybee 22124309Smaybee #ifdef _KERNEL 22134309Smaybee /* 22144309Smaybee * If zio data pages are being allocated out of a separate heap segment, 22154309Smaybee * then enforce that the size of available vmem for this area remains 22164309Smaybee * above about 1/32nd free. 22174309Smaybee */ 22184309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 22194309Smaybee vmem_size(zio_arena, VMEM_FREE) < 22204309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 22214309Smaybee return (1); 22224309Smaybee #endif 22234309Smaybee 2224789Sahrens if (arc_reclaim_needed()) 2225789Sahrens return (1); 2226789Sahrens 22273403Sbmc return (arc_size > arc_c); 2228789Sahrens } 2229789Sahrens 2230789Sahrens /* 22312688Smaybee * The buffer, supplied as the first argument, needs a data block. 22322688Smaybee * So, if we are at cache max, determine which cache should be victimized. 22332688Smaybee * We have the following cases: 2234789Sahrens * 22353403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2236789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2237789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2238789Sahrens * 22393403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2240789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2241789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2242789Sahrens * entries. 2243789Sahrens * 22443403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2245789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2246789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2247789Sahrens * the MFU side, so the MRU side needs to be victimized. 2248789Sahrens * 22493403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2250789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2251789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2252789Sahrens */ 2253789Sahrens static void 22542688Smaybee arc_get_data_buf(arc_buf_t *buf) 2255789Sahrens { 22563290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 22573290Sjohansen uint64_t size = buf->b_hdr->b_size; 22583290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 22592688Smaybee 22602688Smaybee arc_adapt(size, state); 2261789Sahrens 22622688Smaybee /* 22632688Smaybee * We have not yet reached cache maximum size, 22642688Smaybee * just allocate a new buffer. 22652688Smaybee */ 22664309Smaybee if (!arc_evict_needed(type)) { 22673290Sjohansen if (type == ARC_BUFC_METADATA) { 22683290Sjohansen buf->b_data = zio_buf_alloc(size); 22698582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 22703290Sjohansen } else { 22713290Sjohansen ASSERT(type == ARC_BUFC_DATA); 22723290Sjohansen buf->b_data = zio_data_buf_alloc(size); 22738582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 22744309Smaybee atomic_add_64(&arc_size, size); 22753290Sjohansen } 22762688Smaybee goto out; 22772688Smaybee } 22782688Smaybee 22792688Smaybee /* 22802688Smaybee * If we are prefetching from the mfu ghost list, this buffer 22812688Smaybee * will end up on the mru list; so steal space from there. 22822688Smaybee */ 22833403Sbmc if (state == arc_mfu_ghost) 22843403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 22853403Sbmc else if (state == arc_mru_ghost) 22863403Sbmc state = arc_mru; 2287789Sahrens 22883403Sbmc if (state == arc_mru || state == arc_anon) { 22893403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 22908582SBrendan.Gregg@Sun.COM state = (arc_mfu->arcs_lsize[type] >= size && 22914309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2292789Sahrens } else { 22932688Smaybee /* MFU cases */ 22943403Sbmc uint64_t mfu_space = arc_c - arc_p; 22958582SBrendan.Gregg@Sun.COM state = (arc_mru->arcs_lsize[type] >= size && 22964309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 22972688Smaybee } 22985642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 22993290Sjohansen if (type == ARC_BUFC_METADATA) { 23003290Sjohansen buf->b_data = zio_buf_alloc(size); 23018582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 23023290Sjohansen } else { 23033290Sjohansen ASSERT(type == ARC_BUFC_DATA); 23043290Sjohansen buf->b_data = zio_data_buf_alloc(size); 23058582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 23064309Smaybee atomic_add_64(&arc_size, size); 23073290Sjohansen } 23083403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 23092688Smaybee } 23102688Smaybee ASSERT(buf->b_data != NULL); 23112688Smaybee out: 23122688Smaybee /* 23132688Smaybee * Update the state size. Note that ghost states have a 23142688Smaybee * "ghost size" and so don't need to be updated. 23152688Smaybee */ 23162688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 23172688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 23182688Smaybee 23193403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 23202688Smaybee if (list_link_active(&hdr->b_arc_node)) { 23212688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 23224309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2323789Sahrens } 23243298Smaybee /* 23253298Smaybee * If we are growing the cache, and we are adding anonymous 23263403Sbmc * data, and we have outgrown arc_p, update arc_p 23273298Smaybee */ 23283403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 23293403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 23303403Sbmc arc_p = MIN(arc_c, arc_p + size); 2331789Sahrens } 2332789Sahrens } 2333789Sahrens 2334789Sahrens /* 2335789Sahrens * This routine is called whenever a buffer is accessed. 23361544Seschrock * NOTE: the hash lock is dropped in this function. 2337789Sahrens */ 2338789Sahrens static void 23392688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2340789Sahrens { 234111066Srafael.vanoni@sun.com clock_t now; 234211066Srafael.vanoni@sun.com 2343789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2344789Sahrens 23453403Sbmc if (buf->b_state == arc_anon) { 2346789Sahrens /* 2347789Sahrens * This buffer is not in the cache, and does not 2348789Sahrens * appear in our "ghost" list. Add the new buffer 2349789Sahrens * to the MRU state. 2350789Sahrens */ 2351789Sahrens 2352789Sahrens ASSERT(buf->b_arc_access == 0); 235311066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 23541544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 23553403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2356789Sahrens 23573403Sbmc } else if (buf->b_state == arc_mru) { 235811066Srafael.vanoni@sun.com now = ddi_get_lbolt(); 235911066Srafael.vanoni@sun.com 2360789Sahrens /* 23612391Smaybee * If this buffer is here because of a prefetch, then either: 23622391Smaybee * - clear the flag if this is a "referencing" read 23632391Smaybee * (any subsequent access will bump this into the MFU state). 23642391Smaybee * or 23652391Smaybee * - move the buffer to the head of the list if this is 23662391Smaybee * another prefetch (to make it less likely to be evicted). 2367789Sahrens */ 2368789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 23692391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 23702391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23712391Smaybee } else { 23722391Smaybee buf->b_flags &= ~ARC_PREFETCH; 23733403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23742391Smaybee } 237511066Srafael.vanoni@sun.com buf->b_arc_access = now; 2376789Sahrens return; 2377789Sahrens } 2378789Sahrens 2379789Sahrens /* 2380789Sahrens * This buffer has been "accessed" only once so far, 2381789Sahrens * but it is still in the cache. Move it to the MFU 2382789Sahrens * state. 2383789Sahrens */ 238411066Srafael.vanoni@sun.com if (now > buf->b_arc_access + ARC_MINTIME) { 2385789Sahrens /* 2386789Sahrens * More than 125ms have passed since we 2387789Sahrens * instantiated this buffer. Move it to the 2388789Sahrens * most frequently used state. 2389789Sahrens */ 239011066Srafael.vanoni@sun.com buf->b_arc_access = now; 23911544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23923403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2393789Sahrens } 23943403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23953403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2396789Sahrens arc_state_t *new_state; 2397789Sahrens /* 2398789Sahrens * This buffer has been "accessed" recently, but 2399789Sahrens * was evicted from the cache. Move it to the 2400789Sahrens * MFU state. 2401789Sahrens */ 2402789Sahrens 2403789Sahrens if (buf->b_flags & ARC_PREFETCH) { 24043403Sbmc new_state = arc_mru; 24052391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 24062391Smaybee buf->b_flags &= ~ARC_PREFETCH; 24071544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2408789Sahrens } else { 24093403Sbmc new_state = arc_mfu; 24101544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2411789Sahrens } 2412789Sahrens 241311066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 2414789Sahrens arc_change_state(new_state, buf, hash_lock); 2415789Sahrens 24163403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 24173403Sbmc } else if (buf->b_state == arc_mfu) { 2418789Sahrens /* 2419789Sahrens * This buffer has been accessed more than once and is 2420789Sahrens * still in the cache. Keep it in the MFU state. 2421789Sahrens * 24222391Smaybee * NOTE: an add_reference() that occurred when we did 24232391Smaybee * the arc_read() will have kicked this off the list. 24242391Smaybee * If it was a prefetch, we will explicitly move it to 24252391Smaybee * the head of the list now. 2426789Sahrens */ 24272391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 24282391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 24292391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 24302391Smaybee } 24313403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 243211066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 24333403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 24343403Sbmc arc_state_t *new_state = arc_mfu; 2435789Sahrens /* 2436789Sahrens * This buffer has been accessed more than once but has 2437789Sahrens * been evicted from the cache. Move it back to the 2438789Sahrens * MFU state. 2439789Sahrens */ 2440789Sahrens 24412391Smaybee if (buf->b_flags & ARC_PREFETCH) { 24422391Smaybee /* 24432391Smaybee * This is a prefetch access... 24442391Smaybee * move this block back to the MRU state. 24452391Smaybee */ 24462391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 24473403Sbmc new_state = arc_mru; 24482391Smaybee } 24492391Smaybee 245011066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 24511544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 24522391Smaybee arc_change_state(new_state, buf, hash_lock); 2453789Sahrens 24543403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 24555450Sbrendan } else if (buf->b_state == arc_l2c_only) { 24565450Sbrendan /* 24575450Sbrendan * This buffer is on the 2nd Level ARC. 24585450Sbrendan */ 24595450Sbrendan 246011066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 24615450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 24625450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2463789Sahrens } else { 2464789Sahrens ASSERT(!"invalid arc state"); 2465789Sahrens } 2466789Sahrens } 2467789Sahrens 2468789Sahrens /* a generic arc_done_func_t which you can use */ 2469789Sahrens /* ARGSUSED */ 2470789Sahrens void 2471789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2472789Sahrens { 2473*12296SLin.Ling@Sun.COM if (zio == NULL || zio->io_error == 0) 2474*12296SLin.Ling@Sun.COM bcopy(buf->b_data, arg, buf->b_hdr->b_size); 24751544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2476789Sahrens } 2477789Sahrens 24784309Smaybee /* a generic arc_done_func_t */ 2479789Sahrens void 2480789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2481789Sahrens { 2482789Sahrens arc_buf_t **bufp = arg; 2483789Sahrens if (zio && zio->io_error) { 24841544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2485789Sahrens *bufp = NULL; 2486789Sahrens } else { 2487789Sahrens *bufp = buf; 2488*12296SLin.Ling@Sun.COM ASSERT(buf->b_data); 2489789Sahrens } 2490789Sahrens } 2491789Sahrens 2492789Sahrens static void 2493789Sahrens arc_read_done(zio_t *zio) 2494789Sahrens { 24951589Smaybee arc_buf_hdr_t *hdr, *found; 2496789Sahrens arc_buf_t *buf; 2497789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2498789Sahrens kmutex_t *hash_lock; 2499789Sahrens arc_callback_t *callback_list, *acb; 2500789Sahrens int freeable = FALSE; 2501789Sahrens 2502789Sahrens buf = zio->io_private; 2503789Sahrens hdr = buf->b_hdr; 2504789Sahrens 25051589Smaybee /* 25061589Smaybee * The hdr was inserted into hash-table and removed from lists 25071589Smaybee * prior to starting I/O. We should find this header, since 25081589Smaybee * it's in the hash table, and it should be legit since it's 25091589Smaybee * not possible to evict it during the I/O. The only possible 25101589Smaybee * reason for it not to be found is if we were freed during the 25111589Smaybee * read. 25121589Smaybee */ 25138636SMark.Maybee@Sun.COM found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 25143093Sahrens &hash_lock); 2515789Sahrens 25161589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 25175450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 25185450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 25195450Sbrendan 25206987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 25215450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 25227237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2523789Sahrens 2524789Sahrens /* byteswap if necessary */ 2525789Sahrens callback_list = hdr->b_acb; 2526789Sahrens ASSERT(callback_list != NULL); 252710839Swilliam.gorrell@sun.com if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 25287046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 25297046Sahrens byteswap_uint64_array : 25307046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 25317046Sahrens func(buf->b_data, hdr->b_size); 25327046Sahrens } 2533789Sahrens 25345450Sbrendan arc_cksum_compute(buf, B_FALSE); 25353093Sahrens 253610922SJeff.Bonwick@Sun.COM if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 253710922SJeff.Bonwick@Sun.COM /* 253810922SJeff.Bonwick@Sun.COM * Only call arc_access on anonymous buffers. This is because 253910922SJeff.Bonwick@Sun.COM * if we've issued an I/O for an evicted buffer, we've already 254010922SJeff.Bonwick@Sun.COM * called arc_access (to prevent any simultaneous readers from 254110922SJeff.Bonwick@Sun.COM * getting confused). 254210922SJeff.Bonwick@Sun.COM */ 254310922SJeff.Bonwick@Sun.COM arc_access(hdr, hash_lock); 254410922SJeff.Bonwick@Sun.COM } 254510922SJeff.Bonwick@Sun.COM 2546789Sahrens /* create copies of the data buffer for the callers */ 2547789Sahrens abuf = buf; 2548789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2549789Sahrens if (acb->acb_done) { 25502688Smaybee if (abuf == NULL) 25512688Smaybee abuf = arc_buf_clone(buf); 2552789Sahrens acb->acb_buf = abuf; 2553789Sahrens abuf = NULL; 2554789Sahrens } 2555789Sahrens } 2556789Sahrens hdr->b_acb = NULL; 2557789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 25581544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 255910922SJeff.Bonwick@Sun.COM if (abuf == buf) { 256010922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 256110922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 25621544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 256310922SJeff.Bonwick@Sun.COM } 2564789Sahrens 2565789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2566789Sahrens 2567789Sahrens if (zio->io_error != 0) { 2568789Sahrens hdr->b_flags |= ARC_IO_ERROR; 25693403Sbmc if (hdr->b_state != arc_anon) 25703403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 25711544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 25721544Seschrock buf_hash_remove(hdr); 2573789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2574789Sahrens } 2575789Sahrens 25761544Seschrock /* 25772391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 25782391Smaybee * that the hdr (and hence the cv) might be freed before we get to 25792391Smaybee * the cv_broadcast(). 25801544Seschrock */ 25811544Seschrock cv_broadcast(&hdr->b_cv); 25821544Seschrock 25831589Smaybee if (hash_lock) { 25842688Smaybee mutex_exit(hash_lock); 2585789Sahrens } else { 2586789Sahrens /* 2587789Sahrens * This block was freed while we waited for the read to 2588789Sahrens * complete. It has been removed from the hash table and 2589789Sahrens * moved to the anonymous state (so that it won't show up 2590789Sahrens * in the cache). 2591789Sahrens */ 25923403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2593789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2594789Sahrens } 2595789Sahrens 2596789Sahrens /* execute each callback and free its structure */ 2597789Sahrens while ((acb = callback_list) != NULL) { 2598789Sahrens if (acb->acb_done) 2599789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2600789Sahrens 2601789Sahrens if (acb->acb_zio_dummy != NULL) { 2602789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2603789Sahrens zio_nowait(acb->acb_zio_dummy); 2604789Sahrens } 2605789Sahrens 2606789Sahrens callback_list = acb->acb_next; 2607789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2608789Sahrens } 2609789Sahrens 2610789Sahrens if (freeable) 26111544Seschrock arc_hdr_destroy(hdr); 2612789Sahrens } 2613789Sahrens 2614789Sahrens /* 2615789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2616789Sahrens * cache. If the block is found in the cache, invoke the provided 2617789Sahrens * callback immediately and return. Note that the `zio' parameter 2618789Sahrens * in the callback will be NULL in this case, since no IO was 2619789Sahrens * required. If the block is not in the cache pass the read request 2620789Sahrens * on to the spa with a substitute callback function, so that the 2621789Sahrens * requested block will be added to the cache. 2622789Sahrens * 2623789Sahrens * If a read request arrives for a block that has a read in-progress, 2624789Sahrens * either wait for the in-progress read to complete (and return the 2625789Sahrens * results); or, if this is a read with a "done" func, add a record 2626789Sahrens * to the read to invoke the "done" func when the read completes, 2627789Sahrens * and return; or just return. 2628789Sahrens * 2629789Sahrens * arc_read_done() will invoke all the requested "done" functions 2630789Sahrens * for readers of this block. 26317046Sahrens * 26327046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 26337046Sahrens * for the bp. But if you know you don't need locking, you can use 26348213SSuhasini.Peddada@Sun.COM * arc_read_bp. 2635789Sahrens */ 2636789Sahrens int 263710922SJeff.Bonwick@Sun.COM arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf, 26387237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 26397046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 26407046Sahrens { 26417046Sahrens int err; 26427046Sahrens 2643*12296SLin.Ling@Sun.COM if (pbuf == NULL) { 2644*12296SLin.Ling@Sun.COM /* 2645*12296SLin.Ling@Sun.COM * XXX This happens from traverse callback funcs, for 2646*12296SLin.Ling@Sun.COM * the objset_phys_t block. 2647*12296SLin.Ling@Sun.COM */ 2648*12296SLin.Ling@Sun.COM return (arc_read_nolock(pio, spa, bp, done, private, priority, 2649*12296SLin.Ling@Sun.COM zio_flags, arc_flags, zb)); 2650*12296SLin.Ling@Sun.COM } 2651*12296SLin.Ling@Sun.COM 26527046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 26537046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 2654*12296SLin.Ling@Sun.COM rw_enter(&pbuf->b_data_lock, RW_READER); 26557046Sahrens 26567046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 26577237Sek110237 zio_flags, arc_flags, zb); 2658*12296SLin.Ling@Sun.COM rw_exit(&pbuf->b_data_lock); 26599396SMatthew.Ahrens@Sun.COM 26607046Sahrens return (err); 26617046Sahrens } 26627046Sahrens 26637046Sahrens int 266410922SJeff.Bonwick@Sun.COM arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp, 26657237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 26667046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2667789Sahrens { 2668789Sahrens arc_buf_hdr_t *hdr; 2669789Sahrens arc_buf_t *buf; 2670789Sahrens kmutex_t *hash_lock; 26715450Sbrendan zio_t *rzio; 26728636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 2673789Sahrens 2674789Sahrens top: 267510922SJeff.Bonwick@Sun.COM hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 267610922SJeff.Bonwick@Sun.COM &hash_lock); 26771544Seschrock if (hdr && hdr->b_datacnt > 0) { 2678789Sahrens 26792391Smaybee *arc_flags |= ARC_CACHED; 26802391Smaybee 2681789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 26822391Smaybee 26832391Smaybee if (*arc_flags & ARC_WAIT) { 26842391Smaybee cv_wait(&hdr->b_cv, hash_lock); 26852391Smaybee mutex_exit(hash_lock); 26862391Smaybee goto top; 26872391Smaybee } 26882391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 26892391Smaybee 26902391Smaybee if (done) { 2691789Sahrens arc_callback_t *acb = NULL; 2692789Sahrens 2693789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2694789Sahrens KM_SLEEP); 2695789Sahrens acb->acb_done = done; 2696789Sahrens acb->acb_private = private; 2697789Sahrens if (pio != NULL) 2698789Sahrens acb->acb_zio_dummy = zio_null(pio, 26998632SBill.Moore@Sun.COM spa, NULL, NULL, NULL, zio_flags); 2700789Sahrens 2701789Sahrens ASSERT(acb->acb_done != NULL); 2702789Sahrens acb->acb_next = hdr->b_acb; 2703789Sahrens hdr->b_acb = acb; 2704789Sahrens add_reference(hdr, hash_lock, private); 2705789Sahrens mutex_exit(hash_lock); 2706789Sahrens return (0); 2707789Sahrens } 2708789Sahrens mutex_exit(hash_lock); 2709789Sahrens return (0); 2710789Sahrens } 2711789Sahrens 27123403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2713789Sahrens 27141544Seschrock if (done) { 27152688Smaybee add_reference(hdr, hash_lock, private); 27161544Seschrock /* 27171544Seschrock * If this block is already in use, create a new 27181544Seschrock * copy of the data so that we will be guaranteed 27191544Seschrock * that arc_release() will always succeed. 27201544Seschrock */ 27211544Seschrock buf = hdr->b_buf; 27221544Seschrock ASSERT(buf); 27231544Seschrock ASSERT(buf->b_data); 27242688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 27251544Seschrock ASSERT(buf->b_efunc == NULL); 27261544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 27272688Smaybee } else { 27282688Smaybee buf = arc_buf_clone(buf); 27291544Seschrock } 273010922SJeff.Bonwick@Sun.COM 27312391Smaybee } else if (*arc_flags & ARC_PREFETCH && 27322391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 27332391Smaybee hdr->b_flags |= ARC_PREFETCH; 2734789Sahrens } 2735789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 27362688Smaybee arc_access(hdr, hash_lock); 27377237Sek110237 if (*arc_flags & ARC_L2CACHE) 27387237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27392688Smaybee mutex_exit(hash_lock); 27403403Sbmc ARCSTAT_BUMP(arcstat_hits); 27413403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 27423403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 27433403Sbmc data, metadata, hits); 27443403Sbmc 2745789Sahrens if (done) 2746789Sahrens done(NULL, buf, private); 2747789Sahrens } else { 2748789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2749789Sahrens arc_callback_t *acb; 27506987Sbrendan vdev_t *vd = NULL; 27519215SGeorge.Wilson@Sun.COM uint64_t addr; 27528582SBrendan.Gregg@Sun.COM boolean_t devw = B_FALSE; 2753789Sahrens 2754789Sahrens if (hdr == NULL) { 2755789Sahrens /* this block is not in the cache */ 2756789Sahrens arc_buf_hdr_t *exists; 27573290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 27583290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2759789Sahrens hdr = buf->b_hdr; 2760789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 276110922SJeff.Bonwick@Sun.COM hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 2762789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2763789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2764789Sahrens if (exists) { 2765789Sahrens /* somebody beat us to the hash insert */ 2766789Sahrens mutex_exit(hash_lock); 2767*12296SLin.Ling@Sun.COM buf_discard_identity(hdr); 27681544Seschrock (void) arc_buf_remove_ref(buf, private); 2769789Sahrens goto top; /* restart the IO request */ 2770789Sahrens } 27712391Smaybee /* if this is a prefetch, we don't have a reference */ 27722391Smaybee if (*arc_flags & ARC_PREFETCH) { 27732391Smaybee (void) remove_reference(hdr, hash_lock, 27742391Smaybee private); 27752391Smaybee hdr->b_flags |= ARC_PREFETCH; 27762391Smaybee } 27777237Sek110237 if (*arc_flags & ARC_L2CACHE) 27787237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27792391Smaybee if (BP_GET_LEVEL(bp) > 0) 27802391Smaybee hdr->b_flags |= ARC_INDIRECT; 2781789Sahrens } else { 2782789Sahrens /* this block is in the ghost cache */ 27831544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 27841544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 27852391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 27862391Smaybee ASSERT(hdr->b_buf == NULL); 2787789Sahrens 27882391Smaybee /* if this is a prefetch, we don't have a reference */ 27892391Smaybee if (*arc_flags & ARC_PREFETCH) 27902391Smaybee hdr->b_flags |= ARC_PREFETCH; 27912391Smaybee else 27922391Smaybee add_reference(hdr, hash_lock, private); 27937237Sek110237 if (*arc_flags & ARC_L2CACHE) 27947237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27956245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 27961544Seschrock buf->b_hdr = hdr; 27972688Smaybee buf->b_data = NULL; 27981544Seschrock buf->b_efunc = NULL; 27991544Seschrock buf->b_private = NULL; 28001544Seschrock buf->b_next = NULL; 28011544Seschrock hdr->b_buf = buf; 28021544Seschrock ASSERT(hdr->b_datacnt == 0); 28031544Seschrock hdr->b_datacnt = 1; 280412033Swilliam.gorrell@sun.com arc_get_data_buf(buf); 280511805Swilliam.gorrell@sun.com arc_access(hdr, hash_lock); 2806789Sahrens } 2807789Sahrens 280811805Swilliam.gorrell@sun.com ASSERT(!GHOST_STATE(hdr->b_state)); 280911805Swilliam.gorrell@sun.com 2810789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2811789Sahrens acb->acb_done = done; 2812789Sahrens acb->acb_private = private; 2813789Sahrens 2814789Sahrens ASSERT(hdr->b_acb == NULL); 2815789Sahrens hdr->b_acb = acb; 2816789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2817789Sahrens 28187754SJeff.Bonwick@Sun.COM if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 28197754SJeff.Bonwick@Sun.COM (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 28208582SBrendan.Gregg@Sun.COM devw = hdr->b_l2hdr->b_dev->l2ad_writing; 28216987Sbrendan addr = hdr->b_l2hdr->b_daddr; 28227754SJeff.Bonwick@Sun.COM /* 28237754SJeff.Bonwick@Sun.COM * Lock out device removal. 28247754SJeff.Bonwick@Sun.COM */ 28257754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || 28267754SJeff.Bonwick@Sun.COM !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 28277754SJeff.Bonwick@Sun.COM vd = NULL; 28286987Sbrendan } 28296987Sbrendan 28306987Sbrendan mutex_exit(hash_lock); 28316987Sbrendan 2832789Sahrens ASSERT3U(hdr->b_size, ==, size); 283310409SBrendan.Gregg@Sun.COM DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 283410409SBrendan.Gregg@Sun.COM uint64_t, size, zbookmark_t *, zb); 28353403Sbmc ARCSTAT_BUMP(arcstat_misses); 28363403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 28373403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 28383403Sbmc data, metadata, misses); 28391544Seschrock 28408582SBrendan.Gregg@Sun.COM if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 28416987Sbrendan /* 28426987Sbrendan * Read from the L2ARC if the following are true: 28436987Sbrendan * 1. The L2ARC vdev was previously cached. 28446987Sbrendan * 2. This buffer still has L2ARC metadata. 28456987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 28466987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 28476987Sbrendan * also have invalidated the vdev. 28488582SBrendan.Gregg@Sun.COM * 5. This isn't prefetch and l2arc_noprefetch is set. 28496987Sbrendan */ 28507754SJeff.Bonwick@Sun.COM if (hdr->b_l2hdr != NULL && 28518582SBrendan.Gregg@Sun.COM !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 28528582SBrendan.Gregg@Sun.COM !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 28535450Sbrendan l2arc_read_callback_t *cb; 28545450Sbrendan 28556643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 28566643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 28576643Seschrock 28585450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 28595450Sbrendan KM_SLEEP); 28605450Sbrendan cb->l2rcb_buf = buf; 28615450Sbrendan cb->l2rcb_spa = spa; 28625450Sbrendan cb->l2rcb_bp = *bp; 28635450Sbrendan cb->l2rcb_zb = *zb; 28647237Sek110237 cb->l2rcb_flags = zio_flags; 28655450Sbrendan 28665450Sbrendan /* 28677754SJeff.Bonwick@Sun.COM * l2arc read. The SCL_L2ARC lock will be 28687754SJeff.Bonwick@Sun.COM * released by l2arc_read_done(). 28695450Sbrendan */ 28705450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 28715450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 28727237Sek110237 l2arc_read_done, cb, priority, zio_flags | 28737361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 28747754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_PROPAGATE | 28757754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_RETRY, B_FALSE); 28765450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 28775450Sbrendan zio_t *, rzio); 28788582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_read_bytes, size); 28796987Sbrendan 28806987Sbrendan if (*arc_flags & ARC_NOWAIT) { 28816987Sbrendan zio_nowait(rzio); 28826987Sbrendan return (0); 28836987Sbrendan } 28846987Sbrendan 28856987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 28866987Sbrendan if (zio_wait(rzio) == 0) 28876987Sbrendan return (0); 28886987Sbrendan 28896987Sbrendan /* l2arc read error; goto zio_read() */ 28905450Sbrendan } else { 28915450Sbrendan DTRACE_PROBE1(l2arc__miss, 28925450Sbrendan arc_buf_hdr_t *, hdr); 28935450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 28945450Sbrendan if (HDR_L2_WRITING(hdr)) 28955450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 28967754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 28975450Sbrendan } 28988582SBrendan.Gregg@Sun.COM } else { 28998628SBill.Moore@Sun.COM if (vd != NULL) 29008628SBill.Moore@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 29018582SBrendan.Gregg@Sun.COM if (l2arc_ndev != 0) { 29028582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(l2arc__miss, 29038582SBrendan.Gregg@Sun.COM arc_buf_hdr_t *, hdr); 29048582SBrendan.Gregg@Sun.COM ARCSTAT_BUMP(arcstat_l2_misses); 29058582SBrendan.Gregg@Sun.COM } 29065450Sbrendan } 29076643Seschrock 2908789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 29097237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2910789Sahrens 29112391Smaybee if (*arc_flags & ARC_WAIT) 2912789Sahrens return (zio_wait(rzio)); 2913789Sahrens 29142391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2915789Sahrens zio_nowait(rzio); 2916789Sahrens } 2917789Sahrens return (0); 2918789Sahrens } 2919789Sahrens 29201544Seschrock void 29211544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 29221544Seschrock { 29231544Seschrock ASSERT(buf->b_hdr != NULL); 29243403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 29251544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 292610922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 292710922SJeff.Bonwick@Sun.COM ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 292810922SJeff.Bonwick@Sun.COM 29291544Seschrock buf->b_efunc = func; 29301544Seschrock buf->b_private = private; 29311544Seschrock } 29321544Seschrock 29331544Seschrock /* 29341544Seschrock * This is used by the DMU to let the ARC know that a buffer is 29351544Seschrock * being evicted, so the ARC should clean up. If this arc buf 29361544Seschrock * is not yet in the evicted state, it will be put there. 29371544Seschrock */ 29381544Seschrock int 29391544Seschrock arc_buf_evict(arc_buf_t *buf) 29401544Seschrock { 29412887Smaybee arc_buf_hdr_t *hdr; 29421544Seschrock kmutex_t *hash_lock; 29431544Seschrock arc_buf_t **bufp; 29441544Seschrock 2945*12296SLin.Ling@Sun.COM mutex_enter(&buf->b_evict_lock); 29462887Smaybee hdr = buf->b_hdr; 29471544Seschrock if (hdr == NULL) { 29481544Seschrock /* 29491544Seschrock * We are in arc_do_user_evicts(). 29501544Seschrock */ 29511544Seschrock ASSERT(buf->b_data == NULL); 2952*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 29531544Seschrock return (0); 29547545SMark.Maybee@Sun.COM } else if (buf->b_data == NULL) { 29557545SMark.Maybee@Sun.COM arc_buf_t copy = *buf; /* structure assignment */ 29567545SMark.Maybee@Sun.COM /* 29577545SMark.Maybee@Sun.COM * We are on the eviction list; process this buffer now 29587545SMark.Maybee@Sun.COM * but let arc_do_user_evicts() do the reaping. 29597545SMark.Maybee@Sun.COM */ 29607545SMark.Maybee@Sun.COM buf->b_efunc = NULL; 2961*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 29627545SMark.Maybee@Sun.COM VERIFY(copy.b_efunc(©) == 0); 29637545SMark.Maybee@Sun.COM return (1); 29641544Seschrock } 29652887Smaybee hash_lock = HDR_LOCK(hdr); 29661544Seschrock mutex_enter(hash_lock); 2967*12296SLin.Ling@Sun.COM hdr = buf->b_hdr; 2968*12296SLin.Ling@Sun.COM ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2969*12296SLin.Ling@Sun.COM 29702724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 29713403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 29721544Seschrock 29731544Seschrock /* 29741544Seschrock * Pull this buffer off of the hdr 29751544Seschrock */ 29761544Seschrock bufp = &hdr->b_buf; 29771544Seschrock while (*bufp != buf) 29781544Seschrock bufp = &(*bufp)->b_next; 29791544Seschrock *bufp = buf->b_next; 29801544Seschrock 29811544Seschrock ASSERT(buf->b_data != NULL); 29822688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 29831544Seschrock 29841544Seschrock if (hdr->b_datacnt == 0) { 29851544Seschrock arc_state_t *old_state = hdr->b_state; 29861544Seschrock arc_state_t *evicted_state; 29871544Seschrock 2988*12296SLin.Ling@Sun.COM ASSERT(hdr->b_buf == NULL); 29891544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 29901544Seschrock 29911544Seschrock evicted_state = 29923403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 29931544Seschrock 29943403Sbmc mutex_enter(&old_state->arcs_mtx); 29953403Sbmc mutex_enter(&evicted_state->arcs_mtx); 29961544Seschrock 29971544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 29981544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 29995450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 30005450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 30011544Seschrock 30023403Sbmc mutex_exit(&evicted_state->arcs_mtx); 30033403Sbmc mutex_exit(&old_state->arcs_mtx); 30041544Seschrock } 30051544Seschrock mutex_exit(hash_lock); 3006*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 30071819Smaybee 30081544Seschrock VERIFY(buf->b_efunc(buf) == 0); 30091544Seschrock buf->b_efunc = NULL; 30101544Seschrock buf->b_private = NULL; 30111544Seschrock buf->b_hdr = NULL; 3012*12296SLin.Ling@Sun.COM buf->b_next = NULL; 30131544Seschrock kmem_cache_free(buf_cache, buf); 30141544Seschrock return (1); 30151544Seschrock } 30161544Seschrock 3017789Sahrens /* 3018789Sahrens * Release this buffer from the cache. This must be done 3019789Sahrens * after a read and prior to modifying the buffer contents. 3020789Sahrens * If the buffer has more than one reference, we must make 30217046Sahrens * a new hdr for the buffer. 3022789Sahrens */ 3023789Sahrens void 3024789Sahrens arc_release(arc_buf_t *buf, void *tag) 3025789Sahrens { 30267545SMark.Maybee@Sun.COM arc_buf_hdr_t *hdr; 3027*12296SLin.Ling@Sun.COM kmutex_t *hash_lock = NULL; 30287545SMark.Maybee@Sun.COM l2arc_buf_hdr_t *l2hdr; 30295450Sbrendan uint64_t buf_size; 3030*12296SLin.Ling@Sun.COM 3031*12296SLin.Ling@Sun.COM /* 3032*12296SLin.Ling@Sun.COM * It would be nice to assert that if it's DMU metadata (level > 3033*12296SLin.Ling@Sun.COM * 0 || it's the dnode file), then it must be syncing context. 3034*12296SLin.Ling@Sun.COM * But we don't know that information at this level. 3035*12296SLin.Ling@Sun.COM */ 3036*12296SLin.Ling@Sun.COM 3037*12296SLin.Ling@Sun.COM mutex_enter(&buf->b_evict_lock); 30387545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 30397545SMark.Maybee@Sun.COM 3040789Sahrens /* this buffer is not on any list */ 3041789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 3042789Sahrens 30433403Sbmc if (hdr->b_state == arc_anon) { 3044789Sahrens /* this buffer is already released */ 30451544Seschrock ASSERT(buf->b_efunc == NULL); 30469274SBrendan.Gregg@Sun.COM } else { 30479274SBrendan.Gregg@Sun.COM hash_lock = HDR_LOCK(hdr); 30489274SBrendan.Gregg@Sun.COM mutex_enter(hash_lock); 3049*12296SLin.Ling@Sun.COM hdr = buf->b_hdr; 3050*12296SLin.Ling@Sun.COM ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3051789Sahrens } 3052789Sahrens 30537545SMark.Maybee@Sun.COM l2hdr = hdr->b_l2hdr; 30547545SMark.Maybee@Sun.COM if (l2hdr) { 30557545SMark.Maybee@Sun.COM mutex_enter(&l2arc_buflist_mtx); 30567545SMark.Maybee@Sun.COM hdr->b_l2hdr = NULL; 30577545SMark.Maybee@Sun.COM buf_size = hdr->b_size; 30587545SMark.Maybee@Sun.COM } 30597545SMark.Maybee@Sun.COM 30601544Seschrock /* 30611544Seschrock * Do we have more than one buf? 30621544Seschrock */ 30637545SMark.Maybee@Sun.COM if (hdr->b_datacnt > 1) { 3064789Sahrens arc_buf_hdr_t *nhdr; 3065789Sahrens arc_buf_t **bufp; 3066789Sahrens uint64_t blksz = hdr->b_size; 30678636SMark.Maybee@Sun.COM uint64_t spa = hdr->b_spa; 30683290Sjohansen arc_buf_contents_t type = hdr->b_type; 30695450Sbrendan uint32_t flags = hdr->b_flags; 3070789Sahrens 30717545SMark.Maybee@Sun.COM ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3072789Sahrens /* 3073*12296SLin.Ling@Sun.COM * Pull the data off of this hdr and attach it to 3074*12296SLin.Ling@Sun.COM * a new anonymous hdr. 3075789Sahrens */ 30761544Seschrock (void) remove_reference(hdr, hash_lock, tag); 3077789Sahrens bufp = &hdr->b_buf; 30781544Seschrock while (*bufp != buf) 3079789Sahrens bufp = &(*bufp)->b_next; 3080*12296SLin.Ling@Sun.COM *bufp = buf->b_next; 30813897Smaybee buf->b_next = NULL; 30821544Seschrock 30833403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 30843403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 30851544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 30864309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 30874309Smaybee ASSERT3U(*size, >=, hdr->b_size); 30884309Smaybee atomic_add_64(size, -hdr->b_size); 30891544Seschrock } 30901544Seschrock hdr->b_datacnt -= 1; 30913547Smaybee arc_cksum_verify(buf); 30921544Seschrock 3093789Sahrens mutex_exit(hash_lock); 3094789Sahrens 30956245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3096789Sahrens nhdr->b_size = blksz; 3097789Sahrens nhdr->b_spa = spa; 30983290Sjohansen nhdr->b_type = type; 3099789Sahrens nhdr->b_buf = buf; 31003403Sbmc nhdr->b_state = arc_anon; 3101789Sahrens nhdr->b_arc_access = 0; 31025450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 31035450Sbrendan nhdr->b_l2hdr = NULL; 31041544Seschrock nhdr->b_datacnt = 1; 31053547Smaybee nhdr->b_freeze_cksum = NULL; 31063897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 3107789Sahrens buf->b_hdr = nhdr; 3108*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 31093403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 3110789Sahrens } else { 3111*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 31121544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3113789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 3114789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3115*12296SLin.Ling@Sun.COM if (hdr->b_state != arc_anon) 3116*12296SLin.Ling@Sun.COM arc_change_state(arc_anon, hdr, hash_lock); 3117789Sahrens hdr->b_arc_access = 0; 3118*12296SLin.Ling@Sun.COM if (hash_lock) 3119*12296SLin.Ling@Sun.COM mutex_exit(hash_lock); 3120*12296SLin.Ling@Sun.COM 3121*12296SLin.Ling@Sun.COM buf_discard_identity(hdr); 31223547Smaybee arc_buf_thaw(buf); 3123789Sahrens } 31241544Seschrock buf->b_efunc = NULL; 31251544Seschrock buf->b_private = NULL; 31265450Sbrendan 31275450Sbrendan if (l2hdr) { 31285450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 31295450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 31305450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 31317545SMark.Maybee@Sun.COM mutex_exit(&l2arc_buflist_mtx); 31325450Sbrendan } 3133789Sahrens } 3134789Sahrens 3135*12296SLin.Ling@Sun.COM /* 3136*12296SLin.Ling@Sun.COM * Release this buffer. If it does not match the provided BP, fill it 3137*12296SLin.Ling@Sun.COM * with that block's contents. 3138*12296SLin.Ling@Sun.COM */ 3139*12296SLin.Ling@Sun.COM /* ARGSUSED */ 3140*12296SLin.Ling@Sun.COM int 3141*12296SLin.Ling@Sun.COM arc_release_bp(arc_buf_t *buf, void *tag, blkptr_t *bp, spa_t *spa, 3142*12296SLin.Ling@Sun.COM zbookmark_t *zb) 3143*12296SLin.Ling@Sun.COM { 3144*12296SLin.Ling@Sun.COM arc_release(buf, tag); 3145*12296SLin.Ling@Sun.COM return (0); 3146*12296SLin.Ling@Sun.COM } 3147*12296SLin.Ling@Sun.COM 3148789Sahrens int 3149789Sahrens arc_released(arc_buf_t *buf) 3150789Sahrens { 31517545SMark.Maybee@Sun.COM int released; 31527545SMark.Maybee@Sun.COM 3153*12296SLin.Ling@Sun.COM mutex_enter(&buf->b_evict_lock); 31547545SMark.Maybee@Sun.COM released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 3155*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 31567545SMark.Maybee@Sun.COM return (released); 31571544Seschrock } 31581544Seschrock 31591544Seschrock int 31601544Seschrock arc_has_callback(arc_buf_t *buf) 31611544Seschrock { 31627545SMark.Maybee@Sun.COM int callback; 31637545SMark.Maybee@Sun.COM 3164*12296SLin.Ling@Sun.COM mutex_enter(&buf->b_evict_lock); 31657545SMark.Maybee@Sun.COM callback = (buf->b_efunc != NULL); 3166*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 31677545SMark.Maybee@Sun.COM return (callback); 3168789Sahrens } 3169789Sahrens 31701544Seschrock #ifdef ZFS_DEBUG 31711544Seschrock int 31721544Seschrock arc_referenced(arc_buf_t *buf) 31731544Seschrock { 31747545SMark.Maybee@Sun.COM int referenced; 31757545SMark.Maybee@Sun.COM 3176*12296SLin.Ling@Sun.COM mutex_enter(&buf->b_evict_lock); 31777545SMark.Maybee@Sun.COM referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 3178*12296SLin.Ling@Sun.COM mutex_exit(&buf->b_evict_lock); 31797545SMark.Maybee@Sun.COM return (referenced); 31801544Seschrock } 31811544Seschrock #endif 31821544Seschrock 3183789Sahrens static void 31843547Smaybee arc_write_ready(zio_t *zio) 31853547Smaybee { 31863547Smaybee arc_write_callback_t *callback = zio->io_private; 31873547Smaybee arc_buf_t *buf = callback->awcb_buf; 31885329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 31895329Sgw25295 31907754SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 31917754SJeff.Bonwick@Sun.COM callback->awcb_ready(zio, buf, callback->awcb_private); 31927754SJeff.Bonwick@Sun.COM 31935329Sgw25295 /* 31945329Sgw25295 * If the IO is already in progress, then this is a re-write 31957754SJeff.Bonwick@Sun.COM * attempt, so we need to thaw and re-compute the cksum. 31967754SJeff.Bonwick@Sun.COM * It is the responsibility of the callback to handle the 31977754SJeff.Bonwick@Sun.COM * accounting for any re-write attempt. 31985329Sgw25295 */ 31995329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 32005329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 32015329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 32025329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 32035329Sgw25295 hdr->b_freeze_cksum = NULL; 32045329Sgw25295 } 32055329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 32065329Sgw25295 } 32075450Sbrendan arc_cksum_compute(buf, B_FALSE); 32085329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 32093547Smaybee } 32103547Smaybee 32113547Smaybee static void 3212789Sahrens arc_write_done(zio_t *zio) 3213789Sahrens { 32143547Smaybee arc_write_callback_t *callback = zio->io_private; 32153547Smaybee arc_buf_t *buf = callback->awcb_buf; 32163547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3217789Sahrens 321810922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_acb == NULL); 321910922SJeff.Bonwick@Sun.COM 322010922SJeff.Bonwick@Sun.COM if (zio->io_error == 0) { 322110922SJeff.Bonwick@Sun.COM hdr->b_dva = *BP_IDENTITY(zio->io_bp); 322210922SJeff.Bonwick@Sun.COM hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 322310922SJeff.Bonwick@Sun.COM hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 322410922SJeff.Bonwick@Sun.COM } else { 322510922SJeff.Bonwick@Sun.COM ASSERT(BUF_EMPTY(hdr)); 322610922SJeff.Bonwick@Sun.COM } 322710922SJeff.Bonwick@Sun.COM 32281544Seschrock /* 32291544Seschrock * If the block to be written was all-zero, we may have 32301544Seschrock * compressed it away. In this case no write was performed 3231*12296SLin.Ling@Sun.COM * so there will be no dva/birth/checksum. The buffer must 3232*12296SLin.Ling@Sun.COM * therefore remain anonymous (and uncached). 32331544Seschrock */ 3234789Sahrens if (!BUF_EMPTY(hdr)) { 3235789Sahrens arc_buf_hdr_t *exists; 3236789Sahrens kmutex_t *hash_lock; 3237789Sahrens 323810922SJeff.Bonwick@Sun.COM ASSERT(zio->io_error == 0); 323910922SJeff.Bonwick@Sun.COM 32403093Sahrens arc_cksum_verify(buf); 32413093Sahrens 3242789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3243789Sahrens if (exists) { 3244789Sahrens /* 3245789Sahrens * This can only happen if we overwrite for 3246789Sahrens * sync-to-convergence, because we remove 3247789Sahrens * buffers from the hash table when we arc_free(). 3248789Sahrens */ 324910922SJeff.Bonwick@Sun.COM if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 325010922SJeff.Bonwick@Sun.COM if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 325110922SJeff.Bonwick@Sun.COM panic("bad overwrite, hdr=%p exists=%p", 325210922SJeff.Bonwick@Sun.COM (void *)hdr, (void *)exists); 325310922SJeff.Bonwick@Sun.COM ASSERT(refcount_is_zero(&exists->b_refcnt)); 325410922SJeff.Bonwick@Sun.COM arc_change_state(arc_anon, exists, hash_lock); 325510922SJeff.Bonwick@Sun.COM mutex_exit(hash_lock); 325610922SJeff.Bonwick@Sun.COM arc_hdr_destroy(exists); 325710922SJeff.Bonwick@Sun.COM exists = buf_hash_insert(hdr, &hash_lock); 325810922SJeff.Bonwick@Sun.COM ASSERT3P(exists, ==, NULL); 325910922SJeff.Bonwick@Sun.COM } else { 326010922SJeff.Bonwick@Sun.COM /* Dedup */ 326110922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 326210922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_state == arc_anon); 326310922SJeff.Bonwick@Sun.COM ASSERT(BP_GET_DEDUP(zio->io_bp)); 326410922SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 326510272SMatthew.Ahrens@Sun.COM } 3266789Sahrens } 32671544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 32687046Sahrens /* if it's not anon, we are doing a scrub */ 326910922SJeff.Bonwick@Sun.COM if (!exists && hdr->b_state == arc_anon) 32707046Sahrens arc_access(hdr, hash_lock); 32712688Smaybee mutex_exit(hash_lock); 32721544Seschrock } else { 32731544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3274789Sahrens } 327510922SJeff.Bonwick@Sun.COM 327610922SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 327710922SJeff.Bonwick@Sun.COM callback->awcb_done(zio, buf, callback->awcb_private); 3278789Sahrens 32793547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3280789Sahrens } 3281789Sahrens 32823547Smaybee zio_t * 328310922SJeff.Bonwick@Sun.COM arc_write(zio_t *pio, spa_t *spa, uint64_t txg, 328410922SJeff.Bonwick@Sun.COM blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp, 328510922SJeff.Bonwick@Sun.COM arc_done_func_t *ready, arc_done_func_t *done, void *private, 328610922SJeff.Bonwick@Sun.COM int priority, int zio_flags, const zbookmark_t *zb) 3287789Sahrens { 3288789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 32893547Smaybee arc_write_callback_t *callback; 32907754SJeff.Bonwick@Sun.COM zio_t *zio; 32917754SJeff.Bonwick@Sun.COM 32927754SJeff.Bonwick@Sun.COM ASSERT(ready != NULL); 329310922SJeff.Bonwick@Sun.COM ASSERT(done != NULL); 3294789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 32952237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 329610922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_acb == NULL); 32977237Sek110237 if (l2arc) 32987237Sek110237 hdr->b_flags |= ARC_L2CACHE; 32993547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 33003547Smaybee callback->awcb_ready = ready; 33013547Smaybee callback->awcb_done = done; 33023547Smaybee callback->awcb_private = private; 33033547Smaybee callback->awcb_buf = buf; 33047046Sahrens 330510922SJeff.Bonwick@Sun.COM zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 33067754SJeff.Bonwick@Sun.COM arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3307789Sahrens 33083547Smaybee return (zio); 3309789Sahrens } 3310789Sahrens 331110922SJeff.Bonwick@Sun.COM void 331210922SJeff.Bonwick@Sun.COM arc_free(spa_t *spa, const blkptr_t *bp) 3313789Sahrens { 3314789Sahrens arc_buf_hdr_t *ab; 3315789Sahrens kmutex_t *hash_lock; 33168636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 3317789Sahrens 3318789Sahrens /* 331910922SJeff.Bonwick@Sun.COM * If this buffer is in the cache, release it, so it can be re-used. 3320789Sahrens */ 332110922SJeff.Bonwick@Sun.COM ab = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 332210922SJeff.Bonwick@Sun.COM &hash_lock); 3323789Sahrens if (ab != NULL) { 33243403Sbmc if (ab->b_state != arc_anon) 33253403Sbmc arc_change_state(arc_anon, ab, hash_lock); 33262391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 33272391Smaybee /* 33282391Smaybee * This should only happen when we prefetch. 33292391Smaybee */ 33302391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 33312391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 33322391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 33332391Smaybee if (HDR_IN_HASH_TABLE(ab)) 33342391Smaybee buf_hash_remove(ab); 33352391Smaybee ab->b_arc_access = 0; 3336*12296SLin.Ling@Sun.COM buf_discard_identity(ab); 33372391Smaybee ab->b_buf->b_efunc = NULL; 33382391Smaybee ab->b_buf->b_private = NULL; 33392391Smaybee mutex_exit(hash_lock); 334010922SJeff.Bonwick@Sun.COM } else { 334110922SJeff.Bonwick@Sun.COM ASSERT(refcount_is_zero(&ab->b_refcnt)); 33425450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3343789Sahrens mutex_exit(hash_lock); 33441544Seschrock arc_hdr_destroy(ab); 33453403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3346789Sahrens } 3347789Sahrens } 3348789Sahrens } 3349789Sahrens 33506245Smaybee static int 33519412SAleksandr.Guzovskiy@Sun.COM arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg) 33526245Smaybee { 33536245Smaybee #ifdef _KERNEL 33546245Smaybee uint64_t available_memory = ptob(freemem); 33556245Smaybee static uint64_t page_load = 0; 33566245Smaybee static uint64_t last_txg = 0; 33576245Smaybee 33586245Smaybee #if defined(__i386) 33596245Smaybee available_memory = 33606245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 33616245Smaybee #endif 33626245Smaybee if (available_memory >= zfs_write_limit_max) 33636245Smaybee return (0); 33646245Smaybee 33656245Smaybee if (txg > last_txg) { 33666245Smaybee last_txg = txg; 33676245Smaybee page_load = 0; 33686245Smaybee } 33696245Smaybee /* 33706245Smaybee * If we are in pageout, we know that memory is already tight, 33716245Smaybee * the arc is already going to be evicting, so we just want to 33726245Smaybee * continue to let page writes occur as quickly as possible. 33736245Smaybee */ 33746245Smaybee if (curproc == proc_pageout) { 33756245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 33766245Smaybee return (ERESTART); 33776245Smaybee /* Note: reserve is inflated, so we deflate */ 33786245Smaybee page_load += reserve / 8; 33796245Smaybee return (0); 33806245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 33816245Smaybee /* memory is low, delay before restarting */ 33826245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33836245Smaybee return (EAGAIN); 33846245Smaybee } 33856245Smaybee page_load = 0; 33866245Smaybee 33876245Smaybee if (arc_size > arc_c_min) { 33886245Smaybee uint64_t evictable_memory = 33896245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 33906245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 33916245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 33926245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 33936245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 33946245Smaybee } 33956245Smaybee 33966245Smaybee if (inflight_data > available_memory / 4) { 33976245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33986245Smaybee return (ERESTART); 33996245Smaybee } 34006245Smaybee #endif 34016245Smaybee return (0); 34026245Smaybee } 34036245Smaybee 3404789Sahrens void 34056245Smaybee arc_tempreserve_clear(uint64_t reserve) 3406789Sahrens { 34076245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3408789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3409789Sahrens } 3410789Sahrens 3411789Sahrens int 34126245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3413789Sahrens { 34146245Smaybee int error; 34159412SAleksandr.Guzovskiy@Sun.COM uint64_t anon_size; 34166245Smaybee 3417789Sahrens #ifdef ZFS_DEBUG 3418789Sahrens /* 3419789Sahrens * Once in a while, fail for no reason. Everything should cope. 3420789Sahrens */ 3421789Sahrens if (spa_get_random(10000) == 0) { 3422789Sahrens dprintf("forcing random failure\n"); 3423789Sahrens return (ERESTART); 3424789Sahrens } 3425789Sahrens #endif 34266245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 34276245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 34286245Smaybee if (reserve > arc_c) 3429982Smaybee return (ENOMEM); 3430982Smaybee 3431789Sahrens /* 34329412SAleksandr.Guzovskiy@Sun.COM * Don't count loaned bufs as in flight dirty data to prevent long 34339412SAleksandr.Guzovskiy@Sun.COM * network delays from blocking transactions that are ready to be 34349412SAleksandr.Guzovskiy@Sun.COM * assigned to a txg. 34359412SAleksandr.Guzovskiy@Sun.COM */ 34369412SAleksandr.Guzovskiy@Sun.COM anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 34379412SAleksandr.Guzovskiy@Sun.COM 34389412SAleksandr.Guzovskiy@Sun.COM /* 34396245Smaybee * Writes will, almost always, require additional memory allocations 34406245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 34416245Smaybee * make sure that there is sufficient available memory for this. 34426245Smaybee */ 34439412SAleksandr.Guzovskiy@Sun.COM if (error = arc_memory_throttle(reserve, anon_size, txg)) 34446245Smaybee return (error); 34456245Smaybee 34466245Smaybee /* 3447982Smaybee * Throttle writes when the amount of dirty data in the cache 3448982Smaybee * gets too large. We try to keep the cache less than half full 3449982Smaybee * of dirty blocks so that our sync times don't grow too large. 3450982Smaybee * Note: if two requests come in concurrently, we might let them 3451982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3452789Sahrens */ 34539412SAleksandr.Guzovskiy@Sun.COM 34549412SAleksandr.Guzovskiy@Sun.COM if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 34559412SAleksandr.Guzovskiy@Sun.COM anon_size > arc_c / 4) { 34564309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 34574309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 34584309Smaybee arc_tempreserve>>10, 34594309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 34604309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 34616245Smaybee reserve>>10, arc_c>>10); 3462789Sahrens return (ERESTART); 3463789Sahrens } 34646245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3465789Sahrens return (0); 3466789Sahrens } 3467789Sahrens 3468789Sahrens void 3469789Sahrens arc_init(void) 3470789Sahrens { 3471789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3472789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3473789Sahrens 34742391Smaybee /* Convert seconds to clock ticks */ 34752638Sperrin arc_min_prefetch_lifespan = 1 * hz; 34762391Smaybee 3477789Sahrens /* Start out with 1/8 of all memory */ 34783403Sbmc arc_c = physmem * PAGESIZE / 8; 3479789Sahrens 3480789Sahrens #ifdef _KERNEL 3481789Sahrens /* 3482789Sahrens * On architectures where the physical memory can be larger 3483789Sahrens * than the addressable space (intel in 32-bit mode), we may 3484789Sahrens * need to limit the cache to 1/8 of VM size. 3485789Sahrens */ 34863403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3487789Sahrens #endif 3488789Sahrens 3489982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 34903403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3491982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 34923403Sbmc if (arc_c * 8 >= 1<<30) 34933403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3494789Sahrens else 34953403Sbmc arc_c_max = arc_c_min; 34963403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 34972885Sahrens 34982885Sahrens /* 34992885Sahrens * Allow the tunables to override our calculations if they are 35002885Sahrens * reasonable (ie. over 64MB) 35012885Sahrens */ 35022885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 35033403Sbmc arc_c_max = zfs_arc_max; 35043403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 35053403Sbmc arc_c_min = zfs_arc_min; 35062885Sahrens 35073403Sbmc arc_c = arc_c_max; 35083403Sbmc arc_p = (arc_c >> 1); 3509789Sahrens 35104309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 35114309Smaybee arc_meta_limit = arc_c_max / 4; 35124645Sek110237 35134645Sek110237 /* Allow the tunable to override if it is reasonable */ 35144645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 35154645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 35164645Sek110237 35174309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 35184309Smaybee arc_c_min = arc_meta_limit / 2; 35194309Smaybee 35208582SBrendan.Gregg@Sun.COM if (zfs_arc_grow_retry > 0) 35218582SBrendan.Gregg@Sun.COM arc_grow_retry = zfs_arc_grow_retry; 35228582SBrendan.Gregg@Sun.COM 35238582SBrendan.Gregg@Sun.COM if (zfs_arc_shrink_shift > 0) 35248582SBrendan.Gregg@Sun.COM arc_shrink_shift = zfs_arc_shrink_shift; 35258582SBrendan.Gregg@Sun.COM 35268582SBrendan.Gregg@Sun.COM if (zfs_arc_p_min_shift > 0) 35278582SBrendan.Gregg@Sun.COM arc_p_min_shift = zfs_arc_p_min_shift; 35288582SBrendan.Gregg@Sun.COM 3529789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3530789Sahrens if (kmem_debugging()) 35313403Sbmc arc_c = arc_c / 2; 35323403Sbmc if (arc_c < arc_c_min) 35333403Sbmc arc_c = arc_c_min; 3534789Sahrens 35353403Sbmc arc_anon = &ARC_anon; 35363403Sbmc arc_mru = &ARC_mru; 35373403Sbmc arc_mru_ghost = &ARC_mru_ghost; 35383403Sbmc arc_mfu = &ARC_mfu; 35393403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 35405450Sbrendan arc_l2c_only = &ARC_l2c_only; 35413403Sbmc arc_size = 0; 3542789Sahrens 35433403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35443403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35453403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35463403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35473403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35485450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35492688Smaybee 35504309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 35514309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35524309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 35534309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35544309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 35554309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35564309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 35574309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35584309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 35594309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35604309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 35614309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35624309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 35634309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35644309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 35654309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35665450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 35675450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35685450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 35695450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3570789Sahrens 3571789Sahrens buf_init(); 3572789Sahrens 3573789Sahrens arc_thread_exit = 0; 35741544Seschrock arc_eviction_list = NULL; 35751544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 35762887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3577789Sahrens 35783403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 35793403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 35803403Sbmc 35813403Sbmc if (arc_ksp != NULL) { 35823403Sbmc arc_ksp->ks_data = &arc_stats; 35833403Sbmc kstat_install(arc_ksp); 35843403Sbmc } 35853403Sbmc 3586789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3587789Sahrens TS_RUN, minclsyspri); 35883158Smaybee 35893158Smaybee arc_dead = FALSE; 35906987Sbrendan arc_warm = B_FALSE; 35916245Smaybee 35926245Smaybee if (zfs_write_limit_max == 0) 35937468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 35946245Smaybee else 35956245Smaybee zfs_write_limit_shift = 0; 35967468SMark.Maybee@Sun.COM mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3597789Sahrens } 3598789Sahrens 3599789Sahrens void 3600789Sahrens arc_fini(void) 3601789Sahrens { 3602789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3603789Sahrens arc_thread_exit = 1; 3604789Sahrens while (arc_thread_exit != 0) 3605789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3606789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3607789Sahrens 36085642Smaybee arc_flush(NULL); 3609789Sahrens 3610789Sahrens arc_dead = TRUE; 3611789Sahrens 36123403Sbmc if (arc_ksp != NULL) { 36133403Sbmc kstat_delete(arc_ksp); 36143403Sbmc arc_ksp = NULL; 36153403Sbmc } 36163403Sbmc 36171544Seschrock mutex_destroy(&arc_eviction_mtx); 3618789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3619789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3620789Sahrens 36214309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 36224309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 36234309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 36244309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 36254309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 36264309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 36274309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 36284309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3629789Sahrens 36303403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 36313403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 36323403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 36333403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 36343403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 36358214SRicardo.M.Correia@Sun.COM mutex_destroy(&arc_l2c_only->arcs_mtx); 36362856Snd150628 36377468SMark.Maybee@Sun.COM mutex_destroy(&zfs_write_limit_lock); 36387468SMark.Maybee@Sun.COM 3639789Sahrens buf_fini(); 36409412SAleksandr.Guzovskiy@Sun.COM 36419412SAleksandr.Guzovskiy@Sun.COM ASSERT(arc_loaned_bytes == 0); 3642789Sahrens } 36435450Sbrendan 36445450Sbrendan /* 36455450Sbrendan * Level 2 ARC 36465450Sbrendan * 36475450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 36485450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 36495450Sbrendan * using large infrequent writes. The main role of this cache is to boost 36505450Sbrendan * the performance of random read workloads. The intended L2ARC devices 36515450Sbrendan * include short-stroked disks, solid state disks, and other media with 36525450Sbrendan * substantially faster read latency than disk. 36535450Sbrendan * 36545450Sbrendan * +-----------------------+ 36555450Sbrendan * | ARC | 36565450Sbrendan * +-----------------------+ 36575450Sbrendan * | ^ ^ 36585450Sbrendan * | | | 36595450Sbrendan * l2arc_feed_thread() arc_read() 36605450Sbrendan * | | | 36615450Sbrendan * | l2arc read | 36625450Sbrendan * V | | 36635450Sbrendan * +---------------+ | 36645450Sbrendan * | L2ARC | | 36655450Sbrendan * +---------------+ | 36665450Sbrendan * | ^ | 36675450Sbrendan * l2arc_write() | | 36685450Sbrendan * | | | 36695450Sbrendan * V | | 36705450Sbrendan * +-------+ +-------+ 36715450Sbrendan * | vdev | | vdev | 36725450Sbrendan * | cache | | cache | 36735450Sbrendan * +-------+ +-------+ 36745450Sbrendan * +=========+ .-----. 36755450Sbrendan * : L2ARC : |-_____-| 36765450Sbrendan * : devices : | Disks | 36775450Sbrendan * +=========+ `-_____-' 36785450Sbrendan * 36795450Sbrendan * Read requests are satisfied from the following sources, in order: 36805450Sbrendan * 36815450Sbrendan * 1) ARC 36825450Sbrendan * 2) vdev cache of L2ARC devices 36835450Sbrendan * 3) L2ARC devices 36845450Sbrendan * 4) vdev cache of disks 36855450Sbrendan * 5) disks 36865450Sbrendan * 36875450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 36885450Sbrendan * To accommodate for this there are some significant differences between 36895450Sbrendan * the L2ARC and traditional cache design: 36905450Sbrendan * 36915450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 36925450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 36935450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 36945450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 36955450Sbrendan * 36965450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 36975450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 36985450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 36995450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 37005450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 37015450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 37025450Sbrendan * provide a better sense of ratio than this diagram: 37035450Sbrendan * 37045450Sbrendan * head --> tail 37055450Sbrendan * +---------------------+----------+ 37065450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 37075450Sbrendan * +---------------------+----------+ | o L2ARC eligible 37085450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 37095450Sbrendan * +---------------------+----------+ | 37105450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 37115450Sbrendan * headroom | 37125450Sbrendan * l2arc_feed_thread() 37135450Sbrendan * | 37145450Sbrendan * l2arc write hand <--[oooo]--' 37155450Sbrendan * | 8 Mbyte 37165450Sbrendan * | write max 37175450Sbrendan * V 37185450Sbrendan * +==============================+ 37195450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 37205450Sbrendan * +==============================+ 37215450Sbrendan * 32 Gbytes 37225450Sbrendan * 37235450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 37245450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 37255450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 37265450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 37275450Sbrendan * the ARC lists have moved there due to inactivity. 37285450Sbrendan * 37295450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 37305450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 37315450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 37325450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 37335450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 37345450Sbrendan * quickly, such as during backups of the entire pool. 37355450Sbrendan * 37366987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 37376987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 37386987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 37396987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 37406987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 37416987Sbrendan * 37426987Sbrendan * The L2ARC device write speed is also boosted during this time so that 37436987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 37446987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 37456987Sbrendan * through increased writes. 37466987Sbrendan * 37476987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 37485450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 37495450Sbrendan * device is written to in a rotor fashion, sweeping writes through 37505450Sbrendan * available space then repeating. 37515450Sbrendan * 37526987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 37535450Sbrendan * write buffers back to disk based storage. 37545450Sbrendan * 37556987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 37565450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 37575450Sbrendan * 37585450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 37595450Sbrendan * may be necessary for different workloads: 37605450Sbrendan * 37615450Sbrendan * l2arc_write_max max write bytes per interval 37626987Sbrendan * l2arc_write_boost extra write bytes during device warmup 37635450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 37645450Sbrendan * l2arc_headroom number of max device writes to precache 37655450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 37665450Sbrendan * 37675450Sbrendan * Tunables may be removed or added as future performance improvements are 37685450Sbrendan * integrated, and also may become zpool properties. 37698582SBrendan.Gregg@Sun.COM * 37708582SBrendan.Gregg@Sun.COM * There are three key functions that control how the L2ARC warms up: 37718582SBrendan.Gregg@Sun.COM * 37728582SBrendan.Gregg@Sun.COM * l2arc_write_eligible() check if a buffer is eligible to cache 37738582SBrendan.Gregg@Sun.COM * l2arc_write_size() calculate how much to write 37748582SBrendan.Gregg@Sun.COM * l2arc_write_interval() calculate sleep delay between writes 37758582SBrendan.Gregg@Sun.COM * 37768582SBrendan.Gregg@Sun.COM * These three functions determine what to write, how much, and how quickly 37778582SBrendan.Gregg@Sun.COM * to send writes. 37785450Sbrendan */ 37795450Sbrendan 37808582SBrendan.Gregg@Sun.COM static boolean_t 37818636SMark.Maybee@Sun.COM l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 37828582SBrendan.Gregg@Sun.COM { 37838582SBrendan.Gregg@Sun.COM /* 37848582SBrendan.Gregg@Sun.COM * A buffer is *not* eligible for the L2ARC if it: 37858582SBrendan.Gregg@Sun.COM * 1. belongs to a different spa. 378610357SBrendan.Gregg@Sun.COM * 2. is already cached on the L2ARC. 378710357SBrendan.Gregg@Sun.COM * 3. has an I/O in progress (it may be an incomplete read). 378810357SBrendan.Gregg@Sun.COM * 4. is flagged not eligible (zfs property). 37898582SBrendan.Gregg@Sun.COM */ 379010357SBrendan.Gregg@Sun.COM if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL || 37918582SBrendan.Gregg@Sun.COM HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) 37928582SBrendan.Gregg@Sun.COM return (B_FALSE); 37938582SBrendan.Gregg@Sun.COM 37948582SBrendan.Gregg@Sun.COM return (B_TRUE); 37958582SBrendan.Gregg@Sun.COM } 37968582SBrendan.Gregg@Sun.COM 37978582SBrendan.Gregg@Sun.COM static uint64_t 37988582SBrendan.Gregg@Sun.COM l2arc_write_size(l2arc_dev_t *dev) 37998582SBrendan.Gregg@Sun.COM { 38008582SBrendan.Gregg@Sun.COM uint64_t size; 38018582SBrendan.Gregg@Sun.COM 38028582SBrendan.Gregg@Sun.COM size = dev->l2ad_write; 38038582SBrendan.Gregg@Sun.COM 38048582SBrendan.Gregg@Sun.COM if (arc_warm == B_FALSE) 38058582SBrendan.Gregg@Sun.COM size += dev->l2ad_boost; 38068582SBrendan.Gregg@Sun.COM 38078582SBrendan.Gregg@Sun.COM return (size); 38088582SBrendan.Gregg@Sun.COM 38098582SBrendan.Gregg@Sun.COM } 38108582SBrendan.Gregg@Sun.COM 38118582SBrendan.Gregg@Sun.COM static clock_t 38128582SBrendan.Gregg@Sun.COM l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 38138582SBrendan.Gregg@Sun.COM { 381411066Srafael.vanoni@sun.com clock_t interval, next, now; 38158582SBrendan.Gregg@Sun.COM 38168582SBrendan.Gregg@Sun.COM /* 38178582SBrendan.Gregg@Sun.COM * If the ARC lists are busy, increase our write rate; if the 38188582SBrendan.Gregg@Sun.COM * lists are stale, idle back. This is achieved by checking 38198582SBrendan.Gregg@Sun.COM * how much we previously wrote - if it was more than half of 38208582SBrendan.Gregg@Sun.COM * what we wanted, schedule the next write much sooner. 38218582SBrendan.Gregg@Sun.COM */ 38228582SBrendan.Gregg@Sun.COM if (l2arc_feed_again && wrote > (wanted / 2)) 38238582SBrendan.Gregg@Sun.COM interval = (hz * l2arc_feed_min_ms) / 1000; 38248582SBrendan.Gregg@Sun.COM else 38258582SBrendan.Gregg@Sun.COM interval = hz * l2arc_feed_secs; 38268582SBrendan.Gregg@Sun.COM 382711066Srafael.vanoni@sun.com now = ddi_get_lbolt(); 382811066Srafael.vanoni@sun.com next = MAX(now, MIN(now + interval, began + interval)); 38298582SBrendan.Gregg@Sun.COM 38308582SBrendan.Gregg@Sun.COM return (next); 38318582SBrendan.Gregg@Sun.COM } 38328582SBrendan.Gregg@Sun.COM 38335450Sbrendan static void 38345450Sbrendan l2arc_hdr_stat_add(void) 38355450Sbrendan { 38366018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 38376018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 38385450Sbrendan } 38395450Sbrendan 38405450Sbrendan static void 38415450Sbrendan l2arc_hdr_stat_remove(void) 38425450Sbrendan { 38436018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 38446018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 38455450Sbrendan } 38465450Sbrendan 38475450Sbrendan /* 38485450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 38496987Sbrendan * If a device is returned, this also returns holding the spa config lock. 38505450Sbrendan */ 38515450Sbrendan static l2arc_dev_t * 38525450Sbrendan l2arc_dev_get_next(void) 38535450Sbrendan { 38546987Sbrendan l2arc_dev_t *first, *next = NULL; 38556987Sbrendan 38566987Sbrendan /* 38576987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 38586987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 38596987Sbrendan * both locks will be dropped and a spa config lock held instead. 38606987Sbrendan */ 38616987Sbrendan mutex_enter(&spa_namespace_lock); 38626987Sbrendan mutex_enter(&l2arc_dev_mtx); 38636643Seschrock 38646643Seschrock /* if there are no vdevs, there is nothing to do */ 38656643Seschrock if (l2arc_ndev == 0) 38666987Sbrendan goto out; 38676643Seschrock 38686643Seschrock first = NULL; 38696643Seschrock next = l2arc_dev_last; 38706643Seschrock do { 38716643Seschrock /* loop around the list looking for a non-faulted vdev */ 38726643Seschrock if (next == NULL) { 38735450Sbrendan next = list_head(l2arc_dev_list); 38746643Seschrock } else { 38756643Seschrock next = list_next(l2arc_dev_list, next); 38766643Seschrock if (next == NULL) 38776643Seschrock next = list_head(l2arc_dev_list); 38786643Seschrock } 38796643Seschrock 38806643Seschrock /* if we have come back to the start, bail out */ 38816643Seschrock if (first == NULL) 38826643Seschrock first = next; 38836643Seschrock else if (next == first) 38846643Seschrock break; 38856643Seschrock 38866643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 38876643Seschrock 38886643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 38896643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 38906987Sbrendan next = NULL; 38915450Sbrendan 38925450Sbrendan l2arc_dev_last = next; 38935450Sbrendan 38946987Sbrendan out: 38956987Sbrendan mutex_exit(&l2arc_dev_mtx); 38966987Sbrendan 38976987Sbrendan /* 38986987Sbrendan * Grab the config lock to prevent the 'next' device from being 38996987Sbrendan * removed while we are writing to it. 39006987Sbrendan */ 39016987Sbrendan if (next != NULL) 39027754SJeff.Bonwick@Sun.COM spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 39036987Sbrendan mutex_exit(&spa_namespace_lock); 39046987Sbrendan 39055450Sbrendan return (next); 39065450Sbrendan } 39075450Sbrendan 39085450Sbrendan /* 39096987Sbrendan * Free buffers that were tagged for destruction. 39106987Sbrendan */ 39116987Sbrendan static void 39126987Sbrendan l2arc_do_free_on_write() 39136987Sbrendan { 39146987Sbrendan list_t *buflist; 39156987Sbrendan l2arc_data_free_t *df, *df_prev; 39166987Sbrendan 39176987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 39186987Sbrendan buflist = l2arc_free_on_write; 39196987Sbrendan 39206987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 39216987Sbrendan df_prev = list_prev(buflist, df); 39226987Sbrendan ASSERT(df->l2df_data != NULL); 39236987Sbrendan ASSERT(df->l2df_func != NULL); 39246987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 39256987Sbrendan list_remove(buflist, df); 39266987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 39276987Sbrendan } 39286987Sbrendan 39296987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 39306987Sbrendan } 39316987Sbrendan 39326987Sbrendan /* 39335450Sbrendan * A write to a cache device has completed. Update all headers to allow 39345450Sbrendan * reads from these buffers to begin. 39355450Sbrendan */ 39365450Sbrendan static void 39375450Sbrendan l2arc_write_done(zio_t *zio) 39385450Sbrendan { 39395450Sbrendan l2arc_write_callback_t *cb; 39405450Sbrendan l2arc_dev_t *dev; 39415450Sbrendan list_t *buflist; 39425450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 39436987Sbrendan l2arc_buf_hdr_t *abl2; 39445450Sbrendan kmutex_t *hash_lock; 39455450Sbrendan 39465450Sbrendan cb = zio->io_private; 39475450Sbrendan ASSERT(cb != NULL); 39485450Sbrendan dev = cb->l2wcb_dev; 39495450Sbrendan ASSERT(dev != NULL); 39505450Sbrendan head = cb->l2wcb_head; 39515450Sbrendan ASSERT(head != NULL); 39525450Sbrendan buflist = dev->l2ad_buflist; 39535450Sbrendan ASSERT(buflist != NULL); 39545450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 39555450Sbrendan l2arc_write_callback_t *, cb); 39565450Sbrendan 39575450Sbrendan if (zio->io_error != 0) 39585450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 39595450Sbrendan 39605450Sbrendan mutex_enter(&l2arc_buflist_mtx); 39615450Sbrendan 39625450Sbrendan /* 39635450Sbrendan * All writes completed, or an error was hit. 39645450Sbrendan */ 39655450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 39665450Sbrendan ab_prev = list_prev(buflist, ab); 39675450Sbrendan 39685450Sbrendan hash_lock = HDR_LOCK(ab); 39695450Sbrendan if (!mutex_tryenter(hash_lock)) { 39705450Sbrendan /* 39715450Sbrendan * This buffer misses out. It may be in a stage 39725450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 39735450Sbrendan * left set, denying reads to this buffer. 39745450Sbrendan */ 39755450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 39765450Sbrendan continue; 39775450Sbrendan } 39785450Sbrendan 39795450Sbrendan if (zio->io_error != 0) { 39805450Sbrendan /* 39816987Sbrendan * Error - drop L2ARC entry. 39825450Sbrendan */ 39836987Sbrendan list_remove(buflist, ab); 39846987Sbrendan abl2 = ab->b_l2hdr; 39855450Sbrendan ab->b_l2hdr = NULL; 39866987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 39876987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 39885450Sbrendan } 39895450Sbrendan 39905450Sbrendan /* 39915450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 39925450Sbrendan */ 39935450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 39945450Sbrendan 39955450Sbrendan mutex_exit(hash_lock); 39965450Sbrendan } 39975450Sbrendan 39985450Sbrendan atomic_inc_64(&l2arc_writes_done); 39995450Sbrendan list_remove(buflist, head); 40005450Sbrendan kmem_cache_free(hdr_cache, head); 40015450Sbrendan mutex_exit(&l2arc_buflist_mtx); 40025450Sbrendan 40036987Sbrendan l2arc_do_free_on_write(); 40045450Sbrendan 40055450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 40065450Sbrendan } 40075450Sbrendan 40085450Sbrendan /* 40095450Sbrendan * A read to a cache device completed. Validate buffer contents before 40105450Sbrendan * handing over to the regular ARC routines. 40115450Sbrendan */ 40125450Sbrendan static void 40135450Sbrendan l2arc_read_done(zio_t *zio) 40145450Sbrendan { 40155450Sbrendan l2arc_read_callback_t *cb; 40165450Sbrendan arc_buf_hdr_t *hdr; 40175450Sbrendan arc_buf_t *buf; 40185450Sbrendan kmutex_t *hash_lock; 40196987Sbrendan int equal; 40205450Sbrendan 40217754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd != NULL); 40227754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 40237754SJeff.Bonwick@Sun.COM 40247754SJeff.Bonwick@Sun.COM spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 40257754SJeff.Bonwick@Sun.COM 40265450Sbrendan cb = zio->io_private; 40275450Sbrendan ASSERT(cb != NULL); 40285450Sbrendan buf = cb->l2rcb_buf; 40295450Sbrendan ASSERT(buf != NULL); 4030*12296SLin.Ling@Sun.COM 4031*12296SLin.Ling@Sun.COM hash_lock = HDR_LOCK(buf->b_hdr); 4032*12296SLin.Ling@Sun.COM mutex_enter(hash_lock); 40335450Sbrendan hdr = buf->b_hdr; 4034*12296SLin.Ling@Sun.COM ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 40355450Sbrendan 40365450Sbrendan /* 40375450Sbrendan * Check this survived the L2ARC journey. 40385450Sbrendan */ 40395450Sbrendan equal = arc_cksum_equal(buf); 40405450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 40415450Sbrendan mutex_exit(hash_lock); 40425450Sbrendan zio->io_private = buf; 40437754SJeff.Bonwick@Sun.COM zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 40447754SJeff.Bonwick@Sun.COM zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 40455450Sbrendan arc_read_done(zio); 40465450Sbrendan } else { 40475450Sbrendan mutex_exit(hash_lock); 40485450Sbrendan /* 40495450Sbrendan * Buffer didn't survive caching. Increment stats and 40505450Sbrendan * reissue to the original storage device. 40515450Sbrendan */ 40526987Sbrendan if (zio->io_error != 0) { 40535450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 40546987Sbrendan } else { 40556987Sbrendan zio->io_error = EIO; 40566987Sbrendan } 40575450Sbrendan if (!equal) 40585450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 40595450Sbrendan 40607754SJeff.Bonwick@Sun.COM /* 40617754SJeff.Bonwick@Sun.COM * If there's no waiter, issue an async i/o to the primary 40627754SJeff.Bonwick@Sun.COM * storage now. If there *is* a waiter, the caller must 40637754SJeff.Bonwick@Sun.COM * issue the i/o in a context where it's OK to block. 40647754SJeff.Bonwick@Sun.COM */ 40658632SBill.Moore@Sun.COM if (zio->io_waiter == NULL) { 40668632SBill.Moore@Sun.COM zio_t *pio = zio_unique_parent(zio); 40678632SBill.Moore@Sun.COM 40688632SBill.Moore@Sun.COM ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 40698632SBill.Moore@Sun.COM 40708632SBill.Moore@Sun.COM zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 40717754SJeff.Bonwick@Sun.COM buf->b_data, zio->io_size, arc_read_done, buf, 40727754SJeff.Bonwick@Sun.COM zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 40738632SBill.Moore@Sun.COM } 40745450Sbrendan } 40755450Sbrendan 40765450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 40775450Sbrendan } 40785450Sbrendan 40795450Sbrendan /* 40805450Sbrendan * This is the list priority from which the L2ARC will search for pages to 40815450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 40825450Sbrendan * desired order. This order can have a significant effect on cache 40835450Sbrendan * performance. 40845450Sbrendan * 40855450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 40865450Sbrendan * the data lists. This function returns a locked list, and also returns 40875450Sbrendan * the lock pointer. 40885450Sbrendan */ 40895450Sbrendan static list_t * 40905450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 40915450Sbrendan { 40925450Sbrendan list_t *list; 40935450Sbrendan 40945450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 40955450Sbrendan 40965450Sbrendan switch (list_num) { 40975450Sbrendan case 0: 40985450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 40995450Sbrendan *lock = &arc_mfu->arcs_mtx; 41005450Sbrendan break; 41015450Sbrendan case 1: 41025450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 41035450Sbrendan *lock = &arc_mru->arcs_mtx; 41045450Sbrendan break; 41055450Sbrendan case 2: 41065450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 41075450Sbrendan *lock = &arc_mfu->arcs_mtx; 41085450Sbrendan break; 41095450Sbrendan case 3: 41105450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 41115450Sbrendan *lock = &arc_mru->arcs_mtx; 41125450Sbrendan break; 41135450Sbrendan } 41145450Sbrendan 41155450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 41165450Sbrendan mutex_enter(*lock); 41175450Sbrendan return (list); 41185450Sbrendan } 41195450Sbrendan 41205450Sbrendan /* 41215450Sbrendan * Evict buffers from the device write hand to the distance specified in 41225450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 41235450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 41245450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 41255450Sbrendan */ 41265450Sbrendan static void 41275450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 41285450Sbrendan { 41295450Sbrendan list_t *buflist; 41305450Sbrendan l2arc_buf_hdr_t *abl2; 41315450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 41325450Sbrendan kmutex_t *hash_lock; 41335450Sbrendan uint64_t taddr; 41345450Sbrendan 41355450Sbrendan buflist = dev->l2ad_buflist; 41365450Sbrendan 41375450Sbrendan if (buflist == NULL) 41385450Sbrendan return; 41395450Sbrendan 41405450Sbrendan if (!all && dev->l2ad_first) { 41415450Sbrendan /* 41425450Sbrendan * This is the first sweep through the device. There is 41435450Sbrendan * nothing to evict. 41445450Sbrendan */ 41455450Sbrendan return; 41465450Sbrendan } 41475450Sbrendan 41486987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 41495450Sbrendan /* 41505450Sbrendan * When nearing the end of the device, evict to the end 41515450Sbrendan * before the device write hand jumps to the start. 41525450Sbrendan */ 41535450Sbrendan taddr = dev->l2ad_end; 41545450Sbrendan } else { 41555450Sbrendan taddr = dev->l2ad_hand + distance; 41565450Sbrendan } 41575450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 41585450Sbrendan uint64_t, taddr, boolean_t, all); 41595450Sbrendan 41605450Sbrendan top: 41615450Sbrendan mutex_enter(&l2arc_buflist_mtx); 41625450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 41635450Sbrendan ab_prev = list_prev(buflist, ab); 41645450Sbrendan 41655450Sbrendan hash_lock = HDR_LOCK(ab); 41665450Sbrendan if (!mutex_tryenter(hash_lock)) { 41675450Sbrendan /* 41685450Sbrendan * Missed the hash lock. Retry. 41695450Sbrendan */ 41705450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 41715450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41725450Sbrendan mutex_enter(hash_lock); 41735450Sbrendan mutex_exit(hash_lock); 41745450Sbrendan goto top; 41755450Sbrendan } 41765450Sbrendan 41775450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 41785450Sbrendan /* 41795450Sbrendan * We hit a write head node. Leave it for 41805450Sbrendan * l2arc_write_done(). 41815450Sbrendan */ 41825450Sbrendan list_remove(buflist, ab); 41835450Sbrendan mutex_exit(hash_lock); 41845450Sbrendan continue; 41855450Sbrendan } 41865450Sbrendan 41875450Sbrendan if (!all && ab->b_l2hdr != NULL && 41885450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 41895450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 41905450Sbrendan /* 41915450Sbrendan * We've evicted to the target address, 41925450Sbrendan * or the end of the device. 41935450Sbrendan */ 41945450Sbrendan mutex_exit(hash_lock); 41955450Sbrendan break; 41965450Sbrendan } 41975450Sbrendan 41985450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 41995450Sbrendan /* 42005450Sbrendan * Already on the path to destruction. 42015450Sbrendan */ 42025450Sbrendan mutex_exit(hash_lock); 42035450Sbrendan continue; 42045450Sbrendan } 42055450Sbrendan 42065450Sbrendan if (ab->b_state == arc_l2c_only) { 42075450Sbrendan ASSERT(!HDR_L2_READING(ab)); 42085450Sbrendan /* 42095450Sbrendan * This doesn't exist in the ARC. Destroy. 42105450Sbrendan * arc_hdr_destroy() will call list_remove() 42115450Sbrendan * and decrement arcstat_l2_size. 42125450Sbrendan */ 42135450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 42145450Sbrendan arc_hdr_destroy(ab); 42155450Sbrendan } else { 42165450Sbrendan /* 42176987Sbrendan * Invalidate issued or about to be issued 42186987Sbrendan * reads, since we may be about to write 42196987Sbrendan * over this location. 42206987Sbrendan */ 42216987Sbrendan if (HDR_L2_READING(ab)) { 42226987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 42236987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 42246987Sbrendan } 42256987Sbrendan 42266987Sbrendan /* 42275450Sbrendan * Tell ARC this no longer exists in L2ARC. 42285450Sbrendan */ 42295450Sbrendan if (ab->b_l2hdr != NULL) { 42305450Sbrendan abl2 = ab->b_l2hdr; 42315450Sbrendan ab->b_l2hdr = NULL; 42325450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 42335450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 42345450Sbrendan } 42355450Sbrendan list_remove(buflist, ab); 42365450Sbrendan 42375450Sbrendan /* 42385450Sbrendan * This may have been leftover after a 42395450Sbrendan * failed write. 42405450Sbrendan */ 42415450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 42425450Sbrendan } 42435450Sbrendan mutex_exit(hash_lock); 42445450Sbrendan } 42455450Sbrendan mutex_exit(&l2arc_buflist_mtx); 42465450Sbrendan 424710922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0); 42485450Sbrendan dev->l2ad_evict = taddr; 42495450Sbrendan } 42505450Sbrendan 42515450Sbrendan /* 42525450Sbrendan * Find and write ARC buffers to the L2ARC device. 42535450Sbrendan * 42545450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 42555450Sbrendan * for reading until they have completed writing. 42565450Sbrendan */ 42578582SBrendan.Gregg@Sun.COM static uint64_t 42586987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 42595450Sbrendan { 42605450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 42615450Sbrendan l2arc_buf_hdr_t *hdrl2; 42625450Sbrendan list_t *list; 42636987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 42645450Sbrendan void *buf_data; 42655450Sbrendan kmutex_t *hash_lock, *list_lock; 42665450Sbrendan boolean_t have_lock, full; 42675450Sbrendan l2arc_write_callback_t *cb; 42685450Sbrendan zio_t *pio, *wzio; 42698636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 42705450Sbrendan 42715450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 42725450Sbrendan 42735450Sbrendan pio = NULL; 42745450Sbrendan write_sz = 0; 42755450Sbrendan full = B_FALSE; 42766245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 42775450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 42785450Sbrendan 42795450Sbrendan /* 42805450Sbrendan * Copy buffers for L2ARC writing. 42815450Sbrendan */ 42825450Sbrendan mutex_enter(&l2arc_buflist_mtx); 42835450Sbrendan for (int try = 0; try <= 3; try++) { 42845450Sbrendan list = l2arc_list_locked(try, &list_lock); 42855450Sbrendan passed_sz = 0; 42865450Sbrendan 42876987Sbrendan /* 42886987Sbrendan * L2ARC fast warmup. 42896987Sbrendan * 42906987Sbrendan * Until the ARC is warm and starts to evict, read from the 42916987Sbrendan * head of the ARC lists rather than the tail. 42926987Sbrendan */ 42936987Sbrendan headroom = target_sz * l2arc_headroom; 42946987Sbrendan if (arc_warm == B_FALSE) 42956987Sbrendan ab = list_head(list); 42966987Sbrendan else 42976987Sbrendan ab = list_tail(list); 42986987Sbrendan 42996987Sbrendan for (; ab; ab = ab_prev) { 43006987Sbrendan if (arc_warm == B_FALSE) 43016987Sbrendan ab_prev = list_next(list, ab); 43026987Sbrendan else 43036987Sbrendan ab_prev = list_prev(list, ab); 43045450Sbrendan 43055450Sbrendan hash_lock = HDR_LOCK(ab); 43065450Sbrendan have_lock = MUTEX_HELD(hash_lock); 43075450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 43085450Sbrendan /* 43095450Sbrendan * Skip this buffer rather than waiting. 43105450Sbrendan */ 43115450Sbrendan continue; 43125450Sbrendan } 43135450Sbrendan 43145450Sbrendan passed_sz += ab->b_size; 43155450Sbrendan if (passed_sz > headroom) { 43165450Sbrendan /* 43175450Sbrendan * Searched too far. 43185450Sbrendan */ 43195450Sbrendan mutex_exit(hash_lock); 43205450Sbrendan break; 43215450Sbrendan } 43225450Sbrendan 43238636SMark.Maybee@Sun.COM if (!l2arc_write_eligible(guid, ab)) { 43245450Sbrendan mutex_exit(hash_lock); 43255450Sbrendan continue; 43265450Sbrendan } 43275450Sbrendan 43285450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 43295450Sbrendan full = B_TRUE; 43305450Sbrendan mutex_exit(hash_lock); 43315450Sbrendan break; 43325450Sbrendan } 43335450Sbrendan 43345450Sbrendan if (pio == NULL) { 43355450Sbrendan /* 43365450Sbrendan * Insert a dummy header on the buflist so 43375450Sbrendan * l2arc_write_done() can find where the 43385450Sbrendan * write buffers begin without searching. 43395450Sbrendan */ 43405450Sbrendan list_insert_head(dev->l2ad_buflist, head); 43415450Sbrendan 43425450Sbrendan cb = kmem_alloc( 43435450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 43445450Sbrendan cb->l2wcb_dev = dev; 43455450Sbrendan cb->l2wcb_head = head; 43465450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 43475450Sbrendan ZIO_FLAG_CANFAIL); 43485450Sbrendan } 43495450Sbrendan 43505450Sbrendan /* 43515450Sbrendan * Create and add a new L2ARC header. 43525450Sbrendan */ 43535450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 43545450Sbrendan hdrl2->b_dev = dev; 43555450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 43565450Sbrendan 43575450Sbrendan ab->b_flags |= ARC_L2_WRITING; 43585450Sbrendan ab->b_l2hdr = hdrl2; 43595450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 43605450Sbrendan buf_data = ab->b_buf->b_data; 43615450Sbrendan buf_sz = ab->b_size; 43625450Sbrendan 43635450Sbrendan /* 43645450Sbrendan * Compute and store the buffer cksum before 43655450Sbrendan * writing. On debug the cksum is verified first. 43665450Sbrendan */ 43675450Sbrendan arc_cksum_verify(ab->b_buf); 43685450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 43695450Sbrendan 43705450Sbrendan mutex_exit(hash_lock); 43715450Sbrendan 43725450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 43735450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 43745450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 43755450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 43765450Sbrendan 43775450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 43785450Sbrendan zio_t *, wzio); 43795450Sbrendan (void) zio_nowait(wzio); 43805450Sbrendan 43817754SJeff.Bonwick@Sun.COM /* 43827754SJeff.Bonwick@Sun.COM * Keep the clock hand suitably device-aligned. 43837754SJeff.Bonwick@Sun.COM */ 43847754SJeff.Bonwick@Sun.COM buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 43857754SJeff.Bonwick@Sun.COM 43865450Sbrendan write_sz += buf_sz; 43875450Sbrendan dev->l2ad_hand += buf_sz; 43885450Sbrendan } 43895450Sbrendan 43905450Sbrendan mutex_exit(list_lock); 43915450Sbrendan 43925450Sbrendan if (full == B_TRUE) 43935450Sbrendan break; 43945450Sbrendan } 43955450Sbrendan mutex_exit(&l2arc_buflist_mtx); 43965450Sbrendan 43975450Sbrendan if (pio == NULL) { 43985450Sbrendan ASSERT3U(write_sz, ==, 0); 43995450Sbrendan kmem_cache_free(hdr_cache, head); 44008582SBrendan.Gregg@Sun.COM return (0); 44015450Sbrendan } 44025450Sbrendan 44035450Sbrendan ASSERT3U(write_sz, <=, target_sz); 44045450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 44058582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 44065450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 440710922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0); 44085450Sbrendan 44095450Sbrendan /* 44105450Sbrendan * Bump device hand to the device start if it is approaching the end. 44115450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 44125450Sbrendan */ 44136987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 441410922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, 441510922SJeff.Bonwick@Sun.COM dev->l2ad_end - dev->l2ad_hand, 0, 0); 44165450Sbrendan dev->l2ad_hand = dev->l2ad_start; 44175450Sbrendan dev->l2ad_evict = dev->l2ad_start; 44185450Sbrendan dev->l2ad_first = B_FALSE; 44195450Sbrendan } 44205450Sbrendan 44218582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_TRUE; 44225450Sbrendan (void) zio_wait(pio); 44238582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_FALSE; 44248582SBrendan.Gregg@Sun.COM 44258582SBrendan.Gregg@Sun.COM return (write_sz); 44265450Sbrendan } 44275450Sbrendan 44285450Sbrendan /* 44295450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 44305450Sbrendan * heart of the L2ARC. 44315450Sbrendan */ 44325450Sbrendan static void 44335450Sbrendan l2arc_feed_thread(void) 44345450Sbrendan { 44355450Sbrendan callb_cpr_t cpr; 44365450Sbrendan l2arc_dev_t *dev; 44375450Sbrendan spa_t *spa; 44388582SBrendan.Gregg@Sun.COM uint64_t size, wrote; 443911066Srafael.vanoni@sun.com clock_t begin, next = ddi_get_lbolt(); 44405450Sbrendan 44415450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 44425450Sbrendan 44435450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 44445450Sbrendan 44455450Sbrendan while (l2arc_thread_exit == 0) { 44465450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 44476987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 44488582SBrendan.Gregg@Sun.COM next); 44496987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 445011066Srafael.vanoni@sun.com next = ddi_get_lbolt() + hz; 44516987Sbrendan 44526987Sbrendan /* 44536987Sbrendan * Quick check for L2ARC devices. 44546987Sbrendan */ 44556987Sbrendan mutex_enter(&l2arc_dev_mtx); 44566987Sbrendan if (l2arc_ndev == 0) { 44576987Sbrendan mutex_exit(&l2arc_dev_mtx); 44586987Sbrendan continue; 44595450Sbrendan } 44606987Sbrendan mutex_exit(&l2arc_dev_mtx); 446111066Srafael.vanoni@sun.com begin = ddi_get_lbolt(); 44626643Seschrock 44635450Sbrendan /* 44646643Seschrock * This selects the next l2arc device to write to, and in 44656643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 44666987Sbrendan * will return NULL if there are now no l2arc devices or if 44676987Sbrendan * they are all faulted. 44686987Sbrendan * 44696987Sbrendan * If a device is returned, its spa's config lock is also 44706987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 44716987Sbrendan * will grab and release l2arc_dev_mtx. 44725450Sbrendan */ 44736987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 44745450Sbrendan continue; 44756987Sbrendan 44766987Sbrendan spa = dev->l2ad_spa; 44776987Sbrendan ASSERT(spa != NULL); 44785450Sbrendan 44795450Sbrendan /* 44805450Sbrendan * Avoid contributing to memory pressure. 44815450Sbrendan */ 44825450Sbrendan if (arc_reclaim_needed()) { 44835450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 44847754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44855450Sbrendan continue; 44865450Sbrendan } 44875450Sbrendan 44885450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 44895450Sbrendan 44908582SBrendan.Gregg@Sun.COM size = l2arc_write_size(dev); 44916987Sbrendan 44925450Sbrendan /* 44935450Sbrendan * Evict L2ARC buffers that will be overwritten. 44945450Sbrendan */ 44956987Sbrendan l2arc_evict(dev, size, B_FALSE); 44965450Sbrendan 44975450Sbrendan /* 44985450Sbrendan * Write ARC buffers. 44995450Sbrendan */ 45008582SBrendan.Gregg@Sun.COM wrote = l2arc_write_buffers(spa, dev, size); 45018582SBrendan.Gregg@Sun.COM 45028582SBrendan.Gregg@Sun.COM /* 45038582SBrendan.Gregg@Sun.COM * Calculate interval between writes. 45048582SBrendan.Gregg@Sun.COM */ 45058582SBrendan.Gregg@Sun.COM next = l2arc_write_interval(begin, size, wrote); 45067754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 45075450Sbrendan } 45085450Sbrendan 45095450Sbrendan l2arc_thread_exit = 0; 45105450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 45115450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 45125450Sbrendan thread_exit(); 45135450Sbrendan } 45145450Sbrendan 45156643Seschrock boolean_t 45166643Seschrock l2arc_vdev_present(vdev_t *vd) 45176643Seschrock { 45186643Seschrock l2arc_dev_t *dev; 45196643Seschrock 45206643Seschrock mutex_enter(&l2arc_dev_mtx); 45216643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 45226643Seschrock dev = list_next(l2arc_dev_list, dev)) { 45236643Seschrock if (dev->l2ad_vdev == vd) 45246643Seschrock break; 45256643Seschrock } 45266643Seschrock mutex_exit(&l2arc_dev_mtx); 45276643Seschrock 45286643Seschrock return (dev != NULL); 45296643Seschrock } 45306643Seschrock 45315450Sbrendan /* 45325450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 45335450Sbrendan * validated the vdev and opened it. 45345450Sbrendan */ 45355450Sbrendan void 45369816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa_t *spa, vdev_t *vd) 45375450Sbrendan { 45385450Sbrendan l2arc_dev_t *adddev; 45395450Sbrendan 45406643Seschrock ASSERT(!l2arc_vdev_present(vd)); 45416643Seschrock 45425450Sbrendan /* 45435450Sbrendan * Create a new l2arc device entry. 45445450Sbrendan */ 45455450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 45465450Sbrendan adddev->l2ad_spa = spa; 45475450Sbrendan adddev->l2ad_vdev = vd; 45485450Sbrendan adddev->l2ad_write = l2arc_write_max; 45496987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 45509816SGeorge.Wilson@Sun.COM adddev->l2ad_start = VDEV_LABEL_START_SIZE; 45519816SGeorge.Wilson@Sun.COM adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 45525450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 45535450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 45545450Sbrendan adddev->l2ad_first = B_TRUE; 45558582SBrendan.Gregg@Sun.COM adddev->l2ad_writing = B_FALSE; 45565450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 45575450Sbrendan 45585450Sbrendan /* 45595450Sbrendan * This is a list of all ARC buffers that are still valid on the 45605450Sbrendan * device. 45615450Sbrendan */ 45625450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 45635450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 45645450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 45655450Sbrendan 456610922SJeff.Bonwick@Sun.COM vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 45675450Sbrendan 45685450Sbrendan /* 45695450Sbrendan * Add device to global list 45705450Sbrendan */ 45715450Sbrendan mutex_enter(&l2arc_dev_mtx); 45725450Sbrendan list_insert_head(l2arc_dev_list, adddev); 45735450Sbrendan atomic_inc_64(&l2arc_ndev); 45745450Sbrendan mutex_exit(&l2arc_dev_mtx); 45755450Sbrendan } 45765450Sbrendan 45775450Sbrendan /* 45785450Sbrendan * Remove a vdev from the L2ARC. 45795450Sbrendan */ 45805450Sbrendan void 45815450Sbrendan l2arc_remove_vdev(vdev_t *vd) 45825450Sbrendan { 45835450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 45845450Sbrendan 45855450Sbrendan /* 45865450Sbrendan * Find the device by vdev 45875450Sbrendan */ 45885450Sbrendan mutex_enter(&l2arc_dev_mtx); 45895450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 45905450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 45915450Sbrendan if (vd == dev->l2ad_vdev) { 45925450Sbrendan remdev = dev; 45935450Sbrendan break; 45945450Sbrendan } 45955450Sbrendan } 45965450Sbrendan ASSERT(remdev != NULL); 45975450Sbrendan 45985450Sbrendan /* 45995450Sbrendan * Remove device from global list 46005450Sbrendan */ 46015450Sbrendan list_remove(l2arc_dev_list, remdev); 46025450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 46036987Sbrendan atomic_dec_64(&l2arc_ndev); 46046987Sbrendan mutex_exit(&l2arc_dev_mtx); 46055450Sbrendan 46065450Sbrendan /* 46075450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 46085450Sbrendan */ 46095450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 46105450Sbrendan list_destroy(remdev->l2ad_buflist); 46115450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 46125450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 46135450Sbrendan } 46145450Sbrendan 46155450Sbrendan void 46167754SJeff.Bonwick@Sun.COM l2arc_init(void) 46175450Sbrendan { 46185450Sbrendan l2arc_thread_exit = 0; 46195450Sbrendan l2arc_ndev = 0; 46205450Sbrendan l2arc_writes_sent = 0; 46215450Sbrendan l2arc_writes_done = 0; 46225450Sbrendan 46235450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 46245450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 46255450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 46265450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 46275450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 46285450Sbrendan 46295450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 46305450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 46315450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 46325450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 46335450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 46345450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 46355450Sbrendan } 46365450Sbrendan 46375450Sbrendan void 46387754SJeff.Bonwick@Sun.COM l2arc_fini(void) 46395450Sbrendan { 46406987Sbrendan /* 46416987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 46426987Sbrendan * Because of this, we can assume that all l2arc devices have 46436987Sbrendan * already been removed when the pools themselves were removed. 46446987Sbrendan */ 46456987Sbrendan 46466987Sbrendan l2arc_do_free_on_write(); 46476987Sbrendan 46485450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 46495450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 46505450Sbrendan mutex_destroy(&l2arc_dev_mtx); 46515450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 46525450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 46535450Sbrendan 46545450Sbrendan list_destroy(l2arc_dev_list); 46555450Sbrendan list_destroy(l2arc_free_on_write); 46565450Sbrendan } 46577754SJeff.Bonwick@Sun.COM 46587754SJeff.Bonwick@Sun.COM void 46597754SJeff.Bonwick@Sun.COM l2arc_start(void) 46607754SJeff.Bonwick@Sun.COM { 46618241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46627754SJeff.Bonwick@Sun.COM return; 46637754SJeff.Bonwick@Sun.COM 46647754SJeff.Bonwick@Sun.COM (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 46657754SJeff.Bonwick@Sun.COM TS_RUN, minclsyspri); 46667754SJeff.Bonwick@Sun.COM } 46677754SJeff.Bonwick@Sun.COM 46687754SJeff.Bonwick@Sun.COM void 46697754SJeff.Bonwick@Sun.COM l2arc_stop(void) 46707754SJeff.Bonwick@Sun.COM { 46718241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46727754SJeff.Bonwick@Sun.COM return; 46737754SJeff.Bonwick@Sun.COM 46747754SJeff.Bonwick@Sun.COM mutex_enter(&l2arc_feed_thr_lock); 46757754SJeff.Bonwick@Sun.COM cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 46767754SJeff.Bonwick@Sun.COM l2arc_thread_exit = 1; 46777754SJeff.Bonwick@Sun.COM while (l2arc_thread_exit != 0) 46787754SJeff.Bonwick@Sun.COM cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 46797754SJeff.Bonwick@Sun.COM mutex_exit(&l2arc_feed_thr_lock); 46807754SJeff.Bonwick@Sun.COM } 4681