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 /* 228582SBrendan.Gregg@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens /* 273403Sbmc * DVA-based Adjustable Replacement Cache 28789Sahrens * 291544Seschrock * While much of the theory of operation used here is 301544Seschrock * based on the self-tuning, low overhead replacement cache 31789Sahrens * presented by Megiddo and Modha at FAST 2003, there are some 32789Sahrens * significant differences: 33789Sahrens * 34789Sahrens * 1. The Megiddo and Modha model assumes any page is evictable. 35789Sahrens * Pages in its cache cannot be "locked" into memory. This makes 36789Sahrens * the eviction algorithm simple: evict the last page in the list. 37789Sahrens * This also make the performance characteristics easy to reason 38789Sahrens * about. Our cache is not so simple. At any given moment, some 39789Sahrens * subset of the blocks in the cache are un-evictable because we 40789Sahrens * have handed out a reference to them. Blocks are only evictable 41789Sahrens * when there are no external references active. This makes 42789Sahrens * eviction far more problematic: we choose to evict the evictable 43789Sahrens * blocks that are the "lowest" in the list. 44789Sahrens * 45789Sahrens * There are times when it is not possible to evict the requested 46789Sahrens * space. In these circumstances we are unable to adjust the cache 47789Sahrens * size. To prevent the cache growing unbounded at these times we 485450Sbrendan * implement a "cache throttle" that slows the flow of new data 495450Sbrendan * into the cache until we can make space available. 50789Sahrens * 51789Sahrens * 2. The Megiddo and Modha model assumes a fixed cache size. 52789Sahrens * Pages are evicted when the cache is full and there is a cache 53789Sahrens * miss. Our model has a variable sized cache. It grows with 545450Sbrendan * high use, but also tries to react to memory pressure from the 55789Sahrens * operating system: decreasing its size when system memory is 56789Sahrens * tight. 57789Sahrens * 58789Sahrens * 3. The Megiddo and Modha model assumes a fixed page size. All 59789Sahrens * elements of the cache are therefor exactly the same size. So 60789Sahrens * when adjusting the cache size following a cache miss, its simply 61789Sahrens * a matter of choosing a single page to evict. In our model, we 62789Sahrens * have variable sized cache blocks (rangeing from 512 bytes to 63789Sahrens * 128K bytes). We therefor choose a set of blocks to evict to make 64789Sahrens * space for a cache miss that approximates as closely as possible 65789Sahrens * the space used by the new block. 66789Sahrens * 67789Sahrens * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 68789Sahrens * by N. Megiddo & D. Modha, FAST 2003 69789Sahrens */ 70789Sahrens 71789Sahrens /* 72789Sahrens * The locking model: 73789Sahrens * 74789Sahrens * A new reference to a cache buffer can be obtained in two 75789Sahrens * ways: 1) via a hash table lookup using the DVA as a key, 765450Sbrendan * or 2) via one of the ARC lists. The arc_read() interface 77789Sahrens * uses method 1, while the internal arc algorithms for 78789Sahrens * adjusting the cache use method 2. We therefor provide two 79789Sahrens * types of locks: 1) the hash table lock array, and 2) the 80789Sahrens * arc list locks. 81789Sahrens * 82789Sahrens * Buffers do not have their own mutexs, rather they rely on the 83789Sahrens * hash table mutexs for the bulk of their protection (i.e. most 84789Sahrens * fields in the arc_buf_hdr_t are protected by these mutexs). 85789Sahrens * 86789Sahrens * buf_hash_find() returns the appropriate mutex (held) when it 87789Sahrens * locates the requested buffer in the hash table. It returns 88789Sahrens * NULL for the mutex if the buffer was not in the table. 89789Sahrens * 90789Sahrens * buf_hash_remove() expects the appropriate hash mutex to be 91789Sahrens * already held before it is invoked. 92789Sahrens * 93789Sahrens * Each arc state also has a mutex which is used to protect the 94789Sahrens * buffer list associated with the state. When attempting to 95789Sahrens * obtain a hash table lock while holding an arc list lock you 96789Sahrens * must use: mutex_tryenter() to avoid deadlock. Also note that 972688Smaybee * the active state mutex must be held before the ghost state mutex. 98789Sahrens * 991544Seschrock * Arc buffers may have an associated eviction callback function. 1001544Seschrock * This function will be invoked prior to removing the buffer (e.g. 1011544Seschrock * in arc_do_user_evicts()). Note however that the data associated 1021544Seschrock * with the buffer may be evicted prior to the callback. The callback 1031544Seschrock * must be made with *no locks held* (to prevent deadlock). Additionally, 1041544Seschrock * the users of callbacks must ensure that their private data is 1051544Seschrock * protected from simultaneous callbacks from arc_buf_evict() 1061544Seschrock * and arc_do_user_evicts(). 1071544Seschrock * 108789Sahrens * Note that the majority of the performance stats are manipulated 109789Sahrens * with atomic operations. 1105450Sbrendan * 1115450Sbrendan * The L2ARC uses the l2arc_buflist_mtx global mutex for the following: 1125450Sbrendan * 1135450Sbrendan * - L2ARC buflist creation 1145450Sbrendan * - L2ARC buflist eviction 1155450Sbrendan * - L2ARC write completion, which walks L2ARC buflists 1165450Sbrendan * - ARC header destruction, as it removes from L2ARC buflists 1175450Sbrendan * - ARC header release, as it removes from L2ARC buflists 118789Sahrens */ 119789Sahrens 120789Sahrens #include <sys/spa.h> 121789Sahrens #include <sys/zio.h> 122789Sahrens #include <sys/zfs_context.h> 123789Sahrens #include <sys/arc.h> 124789Sahrens #include <sys/refcount.h> 1256643Seschrock #include <sys/vdev.h> 1269816SGeorge.Wilson@Sun.COM #include <sys/vdev_impl.h> 127789Sahrens #ifdef _KERNEL 128789Sahrens #include <sys/vmsystm.h> 129789Sahrens #include <vm/anon.h> 130789Sahrens #include <sys/fs/swapnode.h> 1311484Sek110237 #include <sys/dnlc.h> 132789Sahrens #endif 133789Sahrens #include <sys/callb.h> 1343403Sbmc #include <sys/kstat.h> 135*10922SJeff.Bonwick@Sun.COM #include <zfs_fletcher.h> 136789Sahrens 137789Sahrens static kmutex_t arc_reclaim_thr_lock; 138789Sahrens static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 139789Sahrens static uint8_t arc_thread_exit; 140789Sahrens 1416245Smaybee extern int zfs_write_limit_shift; 1426245Smaybee extern uint64_t zfs_write_limit_max; 1437468SMark.Maybee@Sun.COM extern kmutex_t zfs_write_limit_lock; 1446245Smaybee 1451484Sek110237 #define ARC_REDUCE_DNLC_PERCENT 3 1461484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 1471484Sek110237 148789Sahrens typedef enum arc_reclaim_strategy { 149789Sahrens ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 150789Sahrens ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 151789Sahrens } arc_reclaim_strategy_t; 152789Sahrens 153789Sahrens /* number of seconds before growing cache again */ 154789Sahrens static int arc_grow_retry = 60; 155789Sahrens 1568582SBrendan.Gregg@Sun.COM /* shift of arc_c for calculating both min and max arc_p */ 1578582SBrendan.Gregg@Sun.COM static int arc_p_min_shift = 4; 1588582SBrendan.Gregg@Sun.COM 1598582SBrendan.Gregg@Sun.COM /* log2(fraction of arc to reclaim) */ 1608582SBrendan.Gregg@Sun.COM static int arc_shrink_shift = 5; 1618582SBrendan.Gregg@Sun.COM 1622391Smaybee /* 1632638Sperrin * minimum lifespan of a prefetch block in clock ticks 1642638Sperrin * (initialized in arc_init()) 1652391Smaybee */ 1662638Sperrin static int arc_min_prefetch_lifespan; 1672391Smaybee 168789Sahrens static int arc_dead; 169789Sahrens 170789Sahrens /* 1716987Sbrendan * The arc has filled available memory and has now warmed up. 1726987Sbrendan */ 1736987Sbrendan static boolean_t arc_warm; 1746987Sbrendan 1756987Sbrendan /* 1762885Sahrens * These tunables are for performance analysis. 1772885Sahrens */ 1782885Sahrens uint64_t zfs_arc_max; 1792885Sahrens uint64_t zfs_arc_min; 1804645Sek110237 uint64_t zfs_arc_meta_limit = 0; 1818582SBrendan.Gregg@Sun.COM int zfs_arc_grow_retry = 0; 1828582SBrendan.Gregg@Sun.COM int zfs_arc_shrink_shift = 0; 1838582SBrendan.Gregg@Sun.COM int zfs_arc_p_min_shift = 0; 1842885Sahrens 1852885Sahrens /* 1865450Sbrendan * Note that buffers can be in one of 6 states: 187789Sahrens * ARC_anon - anonymous (discussed below) 1881544Seschrock * ARC_mru - recently used, currently cached 1891544Seschrock * ARC_mru_ghost - recentely used, no longer in cache 1901544Seschrock * ARC_mfu - frequently used, currently cached 1911544Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 1925450Sbrendan * ARC_l2c_only - exists in L2ARC but not other states 1934309Smaybee * When there are no active references to the buffer, they are 1944309Smaybee * are linked onto a list in one of these arc states. These are 1954309Smaybee * the only buffers that can be evicted or deleted. Within each 1964309Smaybee * state there are multiple lists, one for meta-data and one for 1974309Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 1984309Smaybee * etc.) is tracked separately so that it can be managed more 1995450Sbrendan * explicitly: favored over data, limited explicitly. 200789Sahrens * 201789Sahrens * Anonymous buffers are buffers that are not associated with 202789Sahrens * a DVA. These are buffers that hold dirty block copies 203789Sahrens * before they are written to stable storage. By definition, 2041544Seschrock * they are "ref'd" and are considered part of arc_mru 205789Sahrens * that cannot be freed. Generally, they will aquire a DVA 2061544Seschrock * as they are written and migrate onto the arc_mru list. 2075450Sbrendan * 2085450Sbrendan * The ARC_l2c_only state is for buffers that are in the second 2095450Sbrendan * level ARC but no longer in any of the ARC_m* lists. The second 2105450Sbrendan * level ARC itself may also contain buffers that are in any of 2115450Sbrendan * the ARC_m* states - meaning that a buffer can exist in two 2125450Sbrendan * places. The reason for the ARC_l2c_only state is to keep the 2135450Sbrendan * buffer header in the hash table, so that reads that hit the 2145450Sbrendan * second level ARC benefit from these fast lookups. 215789Sahrens */ 216789Sahrens 217789Sahrens typedef struct arc_state { 2184309Smaybee list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */ 2194309Smaybee uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 2204309Smaybee uint64_t arcs_size; /* total amount of data in this state */ 2213403Sbmc kmutex_t arcs_mtx; 222789Sahrens } arc_state_t; 223789Sahrens 2245450Sbrendan /* The 6 states: */ 225789Sahrens static arc_state_t ARC_anon; 2261544Seschrock static arc_state_t ARC_mru; 2271544Seschrock static arc_state_t ARC_mru_ghost; 2281544Seschrock static arc_state_t ARC_mfu; 2291544Seschrock static arc_state_t ARC_mfu_ghost; 2305450Sbrendan static arc_state_t ARC_l2c_only; 231789Sahrens 2323403Sbmc typedef struct arc_stats { 2333403Sbmc kstat_named_t arcstat_hits; 2343403Sbmc kstat_named_t arcstat_misses; 2353403Sbmc kstat_named_t arcstat_demand_data_hits; 2363403Sbmc kstat_named_t arcstat_demand_data_misses; 2373403Sbmc kstat_named_t arcstat_demand_metadata_hits; 2383403Sbmc kstat_named_t arcstat_demand_metadata_misses; 2393403Sbmc kstat_named_t arcstat_prefetch_data_hits; 2403403Sbmc kstat_named_t arcstat_prefetch_data_misses; 2413403Sbmc kstat_named_t arcstat_prefetch_metadata_hits; 2423403Sbmc kstat_named_t arcstat_prefetch_metadata_misses; 2433403Sbmc kstat_named_t arcstat_mru_hits; 2443403Sbmc kstat_named_t arcstat_mru_ghost_hits; 2453403Sbmc kstat_named_t arcstat_mfu_hits; 2463403Sbmc kstat_named_t arcstat_mfu_ghost_hits; 2473403Sbmc kstat_named_t arcstat_deleted; 2483403Sbmc kstat_named_t arcstat_recycle_miss; 2493403Sbmc kstat_named_t arcstat_mutex_miss; 2503403Sbmc kstat_named_t arcstat_evict_skip; 25110357SBrendan.Gregg@Sun.COM kstat_named_t arcstat_evict_l2_cached; 25210357SBrendan.Gregg@Sun.COM kstat_named_t arcstat_evict_l2_eligible; 25310357SBrendan.Gregg@Sun.COM kstat_named_t arcstat_evict_l2_ineligible; 2543403Sbmc kstat_named_t arcstat_hash_elements; 2553403Sbmc kstat_named_t arcstat_hash_elements_max; 2563403Sbmc kstat_named_t arcstat_hash_collisions; 2573403Sbmc kstat_named_t arcstat_hash_chains; 2583403Sbmc kstat_named_t arcstat_hash_chain_max; 2593403Sbmc kstat_named_t arcstat_p; 2603403Sbmc kstat_named_t arcstat_c; 2613403Sbmc kstat_named_t arcstat_c_min; 2623403Sbmc kstat_named_t arcstat_c_max; 2633403Sbmc kstat_named_t arcstat_size; 2645450Sbrendan kstat_named_t arcstat_hdr_size; 2658582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_data_size; 2668582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_other_size; 2675450Sbrendan kstat_named_t arcstat_l2_hits; 2685450Sbrendan kstat_named_t arcstat_l2_misses; 2695450Sbrendan kstat_named_t arcstat_l2_feeds; 2705450Sbrendan kstat_named_t arcstat_l2_rw_clash; 2718582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_l2_read_bytes; 2728582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_l2_write_bytes; 2735450Sbrendan kstat_named_t arcstat_l2_writes_sent; 2745450Sbrendan kstat_named_t arcstat_l2_writes_done; 2755450Sbrendan kstat_named_t arcstat_l2_writes_error; 2765450Sbrendan kstat_named_t arcstat_l2_writes_hdr_miss; 2775450Sbrendan kstat_named_t arcstat_l2_evict_lock_retry; 2785450Sbrendan kstat_named_t arcstat_l2_evict_reading; 2795450Sbrendan kstat_named_t arcstat_l2_free_on_write; 2805450Sbrendan kstat_named_t arcstat_l2_abort_lowmem; 2815450Sbrendan kstat_named_t arcstat_l2_cksum_bad; 2825450Sbrendan kstat_named_t arcstat_l2_io_error; 2835450Sbrendan kstat_named_t arcstat_l2_size; 2845450Sbrendan kstat_named_t arcstat_l2_hdr_size; 2856245Smaybee kstat_named_t arcstat_memory_throttle_count; 2863403Sbmc } arc_stats_t; 2873403Sbmc 2883403Sbmc static arc_stats_t arc_stats = { 2893403Sbmc { "hits", KSTAT_DATA_UINT64 }, 2903403Sbmc { "misses", KSTAT_DATA_UINT64 }, 2913403Sbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 2923403Sbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 2933403Sbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 2943403Sbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 2953403Sbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 2963403Sbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 2973403Sbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 2983403Sbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 2993403Sbmc { "mru_hits", KSTAT_DATA_UINT64 }, 3003403Sbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 3013403Sbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 3023403Sbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 3033403Sbmc { "deleted", KSTAT_DATA_UINT64 }, 3043403Sbmc { "recycle_miss", KSTAT_DATA_UINT64 }, 3053403Sbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 3063403Sbmc { "evict_skip", KSTAT_DATA_UINT64 }, 30710357SBrendan.Gregg@Sun.COM { "evict_l2_cached", KSTAT_DATA_UINT64 }, 30810357SBrendan.Gregg@Sun.COM { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 30910357SBrendan.Gregg@Sun.COM { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 3103403Sbmc { "hash_elements", KSTAT_DATA_UINT64 }, 3113403Sbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 3123403Sbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 3133403Sbmc { "hash_chains", KSTAT_DATA_UINT64 }, 3143403Sbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 3153403Sbmc { "p", KSTAT_DATA_UINT64 }, 3163403Sbmc { "c", KSTAT_DATA_UINT64 }, 3173403Sbmc { "c_min", KSTAT_DATA_UINT64 }, 3183403Sbmc { "c_max", KSTAT_DATA_UINT64 }, 3195450Sbrendan { "size", KSTAT_DATA_UINT64 }, 3205450Sbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 3218582SBrendan.Gregg@Sun.COM { "data_size", KSTAT_DATA_UINT64 }, 3228582SBrendan.Gregg@Sun.COM { "other_size", KSTAT_DATA_UINT64 }, 3235450Sbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 3245450Sbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 3255450Sbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 3265450Sbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 3278582SBrendan.Gregg@Sun.COM { "l2_read_bytes", KSTAT_DATA_UINT64 }, 3288582SBrendan.Gregg@Sun.COM { "l2_write_bytes", KSTAT_DATA_UINT64 }, 3295450Sbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 3305450Sbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 3315450Sbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 3325450Sbrendan { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 3335450Sbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 3345450Sbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 3355450Sbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 3365450Sbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 3375450Sbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 3385450Sbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 3395450Sbrendan { "l2_size", KSTAT_DATA_UINT64 }, 3406245Smaybee { "l2_hdr_size", KSTAT_DATA_UINT64 }, 3416245Smaybee { "memory_throttle_count", KSTAT_DATA_UINT64 } 3423403Sbmc }; 343789Sahrens 3443403Sbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 3453403Sbmc 3463403Sbmc #define ARCSTAT_INCR(stat, val) \ 3473403Sbmc atomic_add_64(&arc_stats.stat.value.ui64, (val)); 3483403Sbmc 349*10922SJeff.Bonwick@Sun.COM #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 3503403Sbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 3513403Sbmc 3523403Sbmc #define ARCSTAT_MAX(stat, val) { \ 3533403Sbmc uint64_t m; \ 3543403Sbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 3553403Sbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 3563403Sbmc continue; \ 3573403Sbmc } 3583403Sbmc 3593403Sbmc #define ARCSTAT_MAXSTAT(stat) \ 3603403Sbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 361789Sahrens 3623403Sbmc /* 3633403Sbmc * We define a macro to allow ARC hits/misses to be easily broken down by 3643403Sbmc * two separate conditions, giving a total of four different subtypes for 3653403Sbmc * each of hits and misses (so eight statistics total). 3663403Sbmc */ 3673403Sbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 3683403Sbmc if (cond1) { \ 3693403Sbmc if (cond2) { \ 3703403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 3713403Sbmc } else { \ 3723403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 3733403Sbmc } \ 3743403Sbmc } else { \ 3753403Sbmc if (cond2) { \ 3763403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 3773403Sbmc } else { \ 3783403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 3793403Sbmc } \ 3803403Sbmc } 381789Sahrens 3823403Sbmc kstat_t *arc_ksp; 383*10922SJeff.Bonwick@Sun.COM static arc_state_t *arc_anon; 3843403Sbmc static arc_state_t *arc_mru; 3853403Sbmc static arc_state_t *arc_mru_ghost; 3863403Sbmc static arc_state_t *arc_mfu; 3873403Sbmc static arc_state_t *arc_mfu_ghost; 3885450Sbrendan static arc_state_t *arc_l2c_only; 3893403Sbmc 3903403Sbmc /* 3913403Sbmc * There are several ARC variables that are critical to export as kstats -- 3923403Sbmc * but we don't want to have to grovel around in the kstat whenever we wish to 3933403Sbmc * manipulate them. For these variables, we therefore define them to be in 3943403Sbmc * terms of the statistic variable. This assures that we are not introducing 3953403Sbmc * the possibility of inconsistency by having shadow copies of the variables, 3963403Sbmc * while still allowing the code to be readable. 3973403Sbmc */ 3983403Sbmc #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 3993403Sbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 4003403Sbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 4013403Sbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 4023403Sbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 4033403Sbmc 4043403Sbmc static int arc_no_grow; /* Don't try to grow cache size */ 4053403Sbmc static uint64_t arc_tempreserve; 4069412SAleksandr.Guzovskiy@Sun.COM static uint64_t arc_loaned_bytes; 4074309Smaybee static uint64_t arc_meta_used; 4084309Smaybee static uint64_t arc_meta_limit; 4094309Smaybee static uint64_t arc_meta_max = 0; 410789Sahrens 4115450Sbrendan typedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 4125450Sbrendan 413789Sahrens typedef struct arc_callback arc_callback_t; 414789Sahrens 415789Sahrens struct arc_callback { 4163547Smaybee void *acb_private; 417789Sahrens arc_done_func_t *acb_done; 418789Sahrens arc_buf_t *acb_buf; 419789Sahrens zio_t *acb_zio_dummy; 420789Sahrens arc_callback_t *acb_next; 421789Sahrens }; 422789Sahrens 4233547Smaybee typedef struct arc_write_callback arc_write_callback_t; 4243547Smaybee 4253547Smaybee struct arc_write_callback { 4263547Smaybee void *awcb_private; 4273547Smaybee arc_done_func_t *awcb_ready; 4283547Smaybee arc_done_func_t *awcb_done; 4293547Smaybee arc_buf_t *awcb_buf; 4303547Smaybee }; 4313547Smaybee 432789Sahrens struct arc_buf_hdr { 433789Sahrens /* protected by hash lock */ 434789Sahrens dva_t b_dva; 435789Sahrens uint64_t b_birth; 436789Sahrens uint64_t b_cksum0; 437789Sahrens 4383093Sahrens kmutex_t b_freeze_lock; 4393093Sahrens zio_cksum_t *b_freeze_cksum; 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)) 548789Sahrens #define HDR_LOCK(buf) \ 549789Sahrens (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->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 667789Sahrens static arc_buf_hdr_t * 6688636SMark.Maybee@Sun.COM buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 669789Sahrens { 670789Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 671789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 672789Sahrens arc_buf_hdr_t *buf; 673789Sahrens 674789Sahrens mutex_enter(hash_lock); 675789Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 676789Sahrens buf = buf->b_hash_next) { 677789Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 678789Sahrens *lockp = hash_lock; 679789Sahrens return (buf); 680789Sahrens } 681789Sahrens } 682789Sahrens mutex_exit(hash_lock); 683789Sahrens *lockp = NULL; 684789Sahrens return (NULL); 685789Sahrens } 686789Sahrens 687789Sahrens /* 688789Sahrens * Insert an entry into the hash table. If there is already an element 689789Sahrens * equal to elem in the hash table, then the already existing element 690789Sahrens * will be returned and the new element will not be inserted. 691789Sahrens * Otherwise returns NULL. 692789Sahrens */ 693789Sahrens static arc_buf_hdr_t * 694789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 695789Sahrens { 696789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 697789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 698789Sahrens arc_buf_hdr_t *fbuf; 6993403Sbmc uint32_t i; 700789Sahrens 7011544Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 702789Sahrens *lockp = hash_lock; 703789Sahrens mutex_enter(hash_lock); 704789Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 705789Sahrens fbuf = fbuf->b_hash_next, i++) { 706789Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 707789Sahrens return (fbuf); 708789Sahrens } 709789Sahrens 710789Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 711789Sahrens buf_hash_table.ht_table[idx] = buf; 7121544Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 713789Sahrens 714789Sahrens /* collect some hash table performance data */ 715789Sahrens if (i > 0) { 7163403Sbmc ARCSTAT_BUMP(arcstat_hash_collisions); 717789Sahrens if (i == 1) 7183403Sbmc ARCSTAT_BUMP(arcstat_hash_chains); 7193403Sbmc 7203403Sbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 721789Sahrens } 7223403Sbmc 7233403Sbmc ARCSTAT_BUMP(arcstat_hash_elements); 7243403Sbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 725789Sahrens 726789Sahrens return (NULL); 727789Sahrens } 728789Sahrens 729789Sahrens static void 730789Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 731789Sahrens { 732789Sahrens arc_buf_hdr_t *fbuf, **bufp; 733789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 734789Sahrens 735789Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 7361544Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 737789Sahrens 738789Sahrens bufp = &buf_hash_table.ht_table[idx]; 739789Sahrens while ((fbuf = *bufp) != buf) { 740789Sahrens ASSERT(fbuf != NULL); 741789Sahrens bufp = &fbuf->b_hash_next; 742789Sahrens } 743789Sahrens *bufp = buf->b_hash_next; 744789Sahrens buf->b_hash_next = NULL; 7451544Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 746789Sahrens 747789Sahrens /* collect some hash table performance data */ 7483403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 7493403Sbmc 750789Sahrens if (buf_hash_table.ht_table[idx] && 751789Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 7523403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 753789Sahrens } 754789Sahrens 755789Sahrens /* 756789Sahrens * Global data structures and functions for the buf kmem cache. 757789Sahrens */ 758789Sahrens static kmem_cache_t *hdr_cache; 759789Sahrens static kmem_cache_t *buf_cache; 760789Sahrens 761789Sahrens static void 762789Sahrens buf_fini(void) 763789Sahrens { 764789Sahrens int i; 765789Sahrens 766789Sahrens kmem_free(buf_hash_table.ht_table, 767789Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 768789Sahrens for (i = 0; i < BUF_LOCKS; i++) 769789Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 770789Sahrens kmem_cache_destroy(hdr_cache); 771789Sahrens kmem_cache_destroy(buf_cache); 772789Sahrens } 773789Sahrens 774789Sahrens /* 775789Sahrens * Constructor callback - called when the cache is empty 776789Sahrens * and a new buf is requested. 777789Sahrens */ 778789Sahrens /* ARGSUSED */ 779789Sahrens static int 780789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 781789Sahrens { 782789Sahrens arc_buf_hdr_t *buf = vbuf; 783789Sahrens 784789Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 785789Sahrens refcount_create(&buf->b_refcnt); 786789Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 7874831Sgw25295 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 7888582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 7898582SBrendan.Gregg@Sun.COM 790789Sahrens return (0); 791789Sahrens } 792789Sahrens 7937545SMark.Maybee@Sun.COM /* ARGSUSED */ 7947545SMark.Maybee@Sun.COM static int 7957545SMark.Maybee@Sun.COM buf_cons(void *vbuf, void *unused, int kmflag) 7967545SMark.Maybee@Sun.COM { 7977545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 7987545SMark.Maybee@Sun.COM 7997545SMark.Maybee@Sun.COM bzero(buf, sizeof (arc_buf_t)); 8007545SMark.Maybee@Sun.COM rw_init(&buf->b_lock, NULL, RW_DEFAULT, NULL); 8018582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 8028582SBrendan.Gregg@Sun.COM 8037545SMark.Maybee@Sun.COM return (0); 8047545SMark.Maybee@Sun.COM } 8057545SMark.Maybee@Sun.COM 806789Sahrens /* 807789Sahrens * Destructor callback - called when a cached buf is 808789Sahrens * no longer required. 809789Sahrens */ 810789Sahrens /* ARGSUSED */ 811789Sahrens static void 812789Sahrens hdr_dest(void *vbuf, void *unused) 813789Sahrens { 814789Sahrens arc_buf_hdr_t *buf = vbuf; 815789Sahrens 816*10922SJeff.Bonwick@Sun.COM ASSERT(BUF_EMPTY(buf)); 817789Sahrens refcount_destroy(&buf->b_refcnt); 818789Sahrens cv_destroy(&buf->b_cv); 8194831Sgw25295 mutex_destroy(&buf->b_freeze_lock); 8208582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 821789Sahrens } 822789Sahrens 8237545SMark.Maybee@Sun.COM /* ARGSUSED */ 8247545SMark.Maybee@Sun.COM static void 8257545SMark.Maybee@Sun.COM buf_dest(void *vbuf, void *unused) 8267545SMark.Maybee@Sun.COM { 8277545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 8287545SMark.Maybee@Sun.COM 8297545SMark.Maybee@Sun.COM rw_destroy(&buf->b_lock); 8308582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 8317545SMark.Maybee@Sun.COM } 8327545SMark.Maybee@Sun.COM 833789Sahrens /* 834789Sahrens * Reclaim callback -- invoked when memory is low. 835789Sahrens */ 836789Sahrens /* ARGSUSED */ 837789Sahrens static void 838789Sahrens hdr_recl(void *unused) 839789Sahrens { 840789Sahrens dprintf("hdr_recl called\n"); 8413158Smaybee /* 8423158Smaybee * umem calls the reclaim func when we destroy the buf cache, 8433158Smaybee * which is after we do arc_fini(). 8443158Smaybee */ 8453158Smaybee if (!arc_dead) 8463158Smaybee cv_signal(&arc_reclaim_thr_cv); 847789Sahrens } 848789Sahrens 849789Sahrens static void 850789Sahrens buf_init(void) 851789Sahrens { 852789Sahrens uint64_t *ct; 8531544Seschrock uint64_t hsize = 1ULL << 12; 854789Sahrens int i, j; 855789Sahrens 856789Sahrens /* 857789Sahrens * The hash table is big enough to fill all of physical memory 8581544Seschrock * with an average 64K block size. The table will take up 8591544Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 860789Sahrens */ 8611544Seschrock while (hsize * 65536 < physmem * PAGESIZE) 862789Sahrens hsize <<= 1; 8631544Seschrock retry: 864789Sahrens buf_hash_table.ht_mask = hsize - 1; 8651544Seschrock buf_hash_table.ht_table = 8661544Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 8671544Seschrock if (buf_hash_table.ht_table == NULL) { 8681544Seschrock ASSERT(hsize > (1ULL << 8)); 8691544Seschrock hsize >>= 1; 8701544Seschrock goto retry; 8711544Seschrock } 872789Sahrens 873789Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 874789Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 875789Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 8767545SMark.Maybee@Sun.COM 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 877789Sahrens 878789Sahrens for (i = 0; i < 256; i++) 879789Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 880789Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 881789Sahrens 882789Sahrens for (i = 0; i < BUF_LOCKS; i++) { 883789Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 884789Sahrens NULL, MUTEX_DEFAULT, NULL); 885789Sahrens } 886789Sahrens } 887789Sahrens 888789Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 889789Sahrens 890789Sahrens static void 8913093Sahrens arc_cksum_verify(arc_buf_t *buf) 8923093Sahrens { 8933093Sahrens zio_cksum_t zc; 8943093Sahrens 8953312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 8963093Sahrens return; 8973093Sahrens 8983093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8993265Sahrens if (buf->b_hdr->b_freeze_cksum == NULL || 9003265Sahrens (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 9013093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9023093Sahrens return; 9033093Sahrens } 9043093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 9053093Sahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 9063093Sahrens panic("buffer modified while frozen!"); 9073093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9083093Sahrens } 9093093Sahrens 9105450Sbrendan static int 9115450Sbrendan arc_cksum_equal(arc_buf_t *buf) 9125450Sbrendan { 9135450Sbrendan zio_cksum_t zc; 9145450Sbrendan int equal; 9155450Sbrendan 9165450Sbrendan mutex_enter(&buf->b_hdr->b_freeze_lock); 9175450Sbrendan fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 9185450Sbrendan equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 9195450Sbrendan mutex_exit(&buf->b_hdr->b_freeze_lock); 9205450Sbrendan 9215450Sbrendan return (equal); 9225450Sbrendan } 9235450Sbrendan 9243093Sahrens static void 9255450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force) 9263093Sahrens { 9275450Sbrendan if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 9283093Sahrens return; 9293093Sahrens 9303093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9313093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9323093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9333093Sahrens return; 9343093Sahrens } 9353093Sahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 9363093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 9373093Sahrens buf->b_hdr->b_freeze_cksum); 9383093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9393093Sahrens } 9403093Sahrens 9413093Sahrens void 9423093Sahrens arc_buf_thaw(arc_buf_t *buf) 9433093Sahrens { 9445450Sbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 9455450Sbrendan if (buf->b_hdr->b_state != arc_anon) 9465450Sbrendan panic("modifying non-anon buffer!"); 9475450Sbrendan if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 9485450Sbrendan panic("modifying buffer while i/o in progress!"); 9495450Sbrendan arc_cksum_verify(buf); 9505450Sbrendan } 9515450Sbrendan 9523093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9533093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9543093Sahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 9553093Sahrens buf->b_hdr->b_freeze_cksum = NULL; 9563093Sahrens } 9573093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9583093Sahrens } 9593093Sahrens 9603093Sahrens void 9613093Sahrens arc_buf_freeze(arc_buf_t *buf) 9623093Sahrens { 9633312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 9643312Sahrens return; 9653312Sahrens 9663093Sahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 9673403Sbmc buf->b_hdr->b_state == arc_anon); 9685450Sbrendan arc_cksum_compute(buf, B_FALSE); 9693093Sahrens } 9703093Sahrens 9713093Sahrens static void 972789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 973789Sahrens { 974789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 975789Sahrens 976789Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 9773403Sbmc (ab->b_state != arc_anon)) { 9783700Sek110237 uint64_t delta = ab->b_size * ab->b_datacnt; 9794309Smaybee list_t *list = &ab->b_state->arcs_list[ab->b_type]; 9804309Smaybee uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 981789Sahrens 9823403Sbmc ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx)); 9833403Sbmc mutex_enter(&ab->b_state->arcs_mtx); 984789Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 9854309Smaybee list_remove(list, ab); 9861544Seschrock if (GHOST_STATE(ab->b_state)) { 9871544Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 9881544Seschrock ASSERT3P(ab->b_buf, ==, NULL); 9891544Seschrock delta = ab->b_size; 9901544Seschrock } 9911544Seschrock ASSERT(delta > 0); 9924309Smaybee ASSERT3U(*size, >=, delta); 9934309Smaybee atomic_add_64(size, -delta); 9943403Sbmc mutex_exit(&ab->b_state->arcs_mtx); 9957046Sahrens /* remove the prefetch flag if we get a reference */ 9962391Smaybee if (ab->b_flags & ARC_PREFETCH) 9972391Smaybee ab->b_flags &= ~ARC_PREFETCH; 998789Sahrens } 999789Sahrens } 1000789Sahrens 1001789Sahrens static int 1002789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1003789Sahrens { 1004789Sahrens int cnt; 10053403Sbmc arc_state_t *state = ab->b_state; 1006789Sahrens 10073403Sbmc ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 10083403Sbmc ASSERT(!GHOST_STATE(state)); 1009789Sahrens 1010789Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 10113403Sbmc (state != arc_anon)) { 10124309Smaybee uint64_t *size = &state->arcs_lsize[ab->b_type]; 10134309Smaybee 10143403Sbmc ASSERT(!MUTEX_HELD(&state->arcs_mtx)); 10153403Sbmc mutex_enter(&state->arcs_mtx); 1016789Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 10174309Smaybee list_insert_head(&state->arcs_list[ab->b_type], ab); 10181544Seschrock ASSERT(ab->b_datacnt > 0); 10194309Smaybee atomic_add_64(size, ab->b_size * ab->b_datacnt); 10203403Sbmc mutex_exit(&state->arcs_mtx); 1021789Sahrens } 1022789Sahrens return (cnt); 1023789Sahrens } 1024789Sahrens 1025789Sahrens /* 1026789Sahrens * Move the supplied buffer to the indicated state. The mutex 1027789Sahrens * for the buffer must be held by the caller. 1028789Sahrens */ 1029789Sahrens static void 10301544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1031789Sahrens { 10321544Seschrock arc_state_t *old_state = ab->b_state; 10333700Sek110237 int64_t refcnt = refcount_count(&ab->b_refcnt); 10343700Sek110237 uint64_t from_delta, to_delta; 1035789Sahrens 1036789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 10371544Seschrock ASSERT(new_state != old_state); 10381544Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 10391544Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 1040*10922SJeff.Bonwick@Sun.COM ASSERT(ab->b_datacnt <= 1 || new_state != arc_anon); 1041*10922SJeff.Bonwick@Sun.COM ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon); 10421544Seschrock 10431544Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 1044789Sahrens 1045789Sahrens /* 1046789Sahrens * If this buffer is evictable, transfer it from the 1047789Sahrens * old state list to the new state list. 1048789Sahrens */ 10491544Seschrock if (refcnt == 0) { 10503403Sbmc if (old_state != arc_anon) { 10513403Sbmc int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx); 10524309Smaybee uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 10531544Seschrock 10541544Seschrock if (use_mutex) 10553403Sbmc mutex_enter(&old_state->arcs_mtx); 10561544Seschrock 10571544Seschrock ASSERT(list_link_active(&ab->b_arc_node)); 10584309Smaybee list_remove(&old_state->arcs_list[ab->b_type], ab); 1059789Sahrens 10602391Smaybee /* 10612391Smaybee * If prefetching out of the ghost cache, 10622391Smaybee * we will have a non-null datacnt. 10632391Smaybee */ 10642391Smaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 10652391Smaybee /* ghost elements have a ghost size */ 10661544Seschrock ASSERT(ab->b_buf == NULL); 10671544Seschrock from_delta = ab->b_size; 1068789Sahrens } 10694309Smaybee ASSERT3U(*size, >=, from_delta); 10704309Smaybee atomic_add_64(size, -from_delta); 10711544Seschrock 10721544Seschrock if (use_mutex) 10733403Sbmc mutex_exit(&old_state->arcs_mtx); 1074789Sahrens } 10753403Sbmc if (new_state != arc_anon) { 10763403Sbmc int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx); 10774309Smaybee uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1078789Sahrens 10791544Seschrock if (use_mutex) 10803403Sbmc mutex_enter(&new_state->arcs_mtx); 10811544Seschrock 10824309Smaybee list_insert_head(&new_state->arcs_list[ab->b_type], ab); 10831544Seschrock 10841544Seschrock /* ghost elements have a ghost size */ 10851544Seschrock if (GHOST_STATE(new_state)) { 10861544Seschrock ASSERT(ab->b_datacnt == 0); 10871544Seschrock ASSERT(ab->b_buf == NULL); 10881544Seschrock to_delta = ab->b_size; 10891544Seschrock } 10904309Smaybee atomic_add_64(size, to_delta); 10911544Seschrock 10921544Seschrock if (use_mutex) 10933403Sbmc mutex_exit(&new_state->arcs_mtx); 1094789Sahrens } 1095789Sahrens } 1096789Sahrens 1097789Sahrens ASSERT(!BUF_EMPTY(ab)); 10985450Sbrendan if (new_state == arc_anon) { 1099789Sahrens buf_hash_remove(ab); 1100789Sahrens } 1101789Sahrens 11021544Seschrock /* adjust state sizes */ 11031544Seschrock if (to_delta) 11043403Sbmc atomic_add_64(&new_state->arcs_size, to_delta); 11051544Seschrock if (from_delta) { 11063403Sbmc ASSERT3U(old_state->arcs_size, >=, from_delta); 11073403Sbmc atomic_add_64(&old_state->arcs_size, -from_delta); 1108789Sahrens } 1109789Sahrens ab->b_state = new_state; 11105450Sbrendan 11115450Sbrendan /* adjust l2arc hdr stats */ 11125450Sbrendan if (new_state == arc_l2c_only) 11135450Sbrendan l2arc_hdr_stat_add(); 11145450Sbrendan else if (old_state == arc_l2c_only) 11155450Sbrendan l2arc_hdr_stat_remove(); 1116789Sahrens } 1117789Sahrens 11184309Smaybee void 11198582SBrendan.Gregg@Sun.COM arc_space_consume(uint64_t space, arc_space_type_t type) 11204309Smaybee { 11218582SBrendan.Gregg@Sun.COM ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11228582SBrendan.Gregg@Sun.COM 11238582SBrendan.Gregg@Sun.COM switch (type) { 11248582SBrendan.Gregg@Sun.COM case ARC_SPACE_DATA: 11258582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, space); 11268582SBrendan.Gregg@Sun.COM break; 11278582SBrendan.Gregg@Sun.COM case ARC_SPACE_OTHER: 11288582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_other_size, space); 11298582SBrendan.Gregg@Sun.COM break; 11308582SBrendan.Gregg@Sun.COM case ARC_SPACE_HDRS: 11318582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_hdr_size, space); 11328582SBrendan.Gregg@Sun.COM break; 11338582SBrendan.Gregg@Sun.COM case ARC_SPACE_L2HDRS: 11348582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_hdr_size, space); 11358582SBrendan.Gregg@Sun.COM break; 11368582SBrendan.Gregg@Sun.COM } 11378582SBrendan.Gregg@Sun.COM 11384309Smaybee atomic_add_64(&arc_meta_used, space); 11394309Smaybee atomic_add_64(&arc_size, space); 11404309Smaybee } 11414309Smaybee 11424309Smaybee void 11438582SBrendan.Gregg@Sun.COM arc_space_return(uint64_t space, arc_space_type_t type) 11444309Smaybee { 11458582SBrendan.Gregg@Sun.COM ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11468582SBrendan.Gregg@Sun.COM 11478582SBrendan.Gregg@Sun.COM switch (type) { 11488582SBrendan.Gregg@Sun.COM case ARC_SPACE_DATA: 11498582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -space); 11508582SBrendan.Gregg@Sun.COM break; 11518582SBrendan.Gregg@Sun.COM case ARC_SPACE_OTHER: 11528582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_other_size, -space); 11538582SBrendan.Gregg@Sun.COM break; 11548582SBrendan.Gregg@Sun.COM case ARC_SPACE_HDRS: 11558582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_hdr_size, -space); 11568582SBrendan.Gregg@Sun.COM break; 11578582SBrendan.Gregg@Sun.COM case ARC_SPACE_L2HDRS: 11588582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 11598582SBrendan.Gregg@Sun.COM break; 11608582SBrendan.Gregg@Sun.COM } 11618582SBrendan.Gregg@Sun.COM 11624309Smaybee ASSERT(arc_meta_used >= space); 11634309Smaybee if (arc_meta_max < arc_meta_used) 11644309Smaybee arc_meta_max = arc_meta_used; 11654309Smaybee atomic_add_64(&arc_meta_used, -space); 11664309Smaybee ASSERT(arc_size >= space); 11674309Smaybee atomic_add_64(&arc_size, -space); 11684309Smaybee } 11694309Smaybee 11704309Smaybee void * 11714309Smaybee arc_data_buf_alloc(uint64_t size) 11724309Smaybee { 11734309Smaybee if (arc_evict_needed(ARC_BUFC_DATA)) 11744309Smaybee cv_signal(&arc_reclaim_thr_cv); 11754309Smaybee atomic_add_64(&arc_size, size); 11764309Smaybee return (zio_data_buf_alloc(size)); 11774309Smaybee } 11784309Smaybee 11794309Smaybee void 11804309Smaybee arc_data_buf_free(void *buf, uint64_t size) 11814309Smaybee { 11824309Smaybee zio_data_buf_free(buf, size); 11834309Smaybee ASSERT(arc_size >= size); 11844309Smaybee atomic_add_64(&arc_size, -size); 11854309Smaybee } 11864309Smaybee 1187789Sahrens arc_buf_t * 11883290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1189789Sahrens { 1190789Sahrens arc_buf_hdr_t *hdr; 1191789Sahrens arc_buf_t *buf; 1192789Sahrens 1193789Sahrens ASSERT3U(size, >, 0); 11946245Smaybee hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1195789Sahrens ASSERT(BUF_EMPTY(hdr)); 1196789Sahrens hdr->b_size = size; 11973290Sjohansen hdr->b_type = type; 11988636SMark.Maybee@Sun.COM hdr->b_spa = spa_guid(spa); 11993403Sbmc hdr->b_state = arc_anon; 1200789Sahrens hdr->b_arc_access = 0; 12016245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1202789Sahrens buf->b_hdr = hdr; 12032688Smaybee buf->b_data = NULL; 12041544Seschrock buf->b_efunc = NULL; 12051544Seschrock buf->b_private = NULL; 1206789Sahrens buf->b_next = NULL; 1207789Sahrens hdr->b_buf = buf; 12082688Smaybee arc_get_data_buf(buf); 12091544Seschrock hdr->b_datacnt = 1; 1210789Sahrens hdr->b_flags = 0; 1211789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1212789Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 1213789Sahrens 1214789Sahrens return (buf); 1215789Sahrens } 1216789Sahrens 12179412SAleksandr.Guzovskiy@Sun.COM static char *arc_onloan_tag = "onloan"; 12189412SAleksandr.Guzovskiy@Sun.COM 12199412SAleksandr.Guzovskiy@Sun.COM /* 12209412SAleksandr.Guzovskiy@Sun.COM * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 12219412SAleksandr.Guzovskiy@Sun.COM * flight data by arc_tempreserve_space() until they are "returned". Loaned 12229412SAleksandr.Guzovskiy@Sun.COM * buffers must be returned to the arc before they can be used by the DMU or 12239412SAleksandr.Guzovskiy@Sun.COM * freed. 12249412SAleksandr.Guzovskiy@Sun.COM */ 12259412SAleksandr.Guzovskiy@Sun.COM arc_buf_t * 12269412SAleksandr.Guzovskiy@Sun.COM arc_loan_buf(spa_t *spa, int size) 12279412SAleksandr.Guzovskiy@Sun.COM { 12289412SAleksandr.Guzovskiy@Sun.COM arc_buf_t *buf; 12299412SAleksandr.Guzovskiy@Sun.COM 12309412SAleksandr.Guzovskiy@Sun.COM buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 12319412SAleksandr.Guzovskiy@Sun.COM 12329412SAleksandr.Guzovskiy@Sun.COM atomic_add_64(&arc_loaned_bytes, size); 12339412SAleksandr.Guzovskiy@Sun.COM return (buf); 12349412SAleksandr.Guzovskiy@Sun.COM } 12359412SAleksandr.Guzovskiy@Sun.COM 12369412SAleksandr.Guzovskiy@Sun.COM /* 12379412SAleksandr.Guzovskiy@Sun.COM * Return a loaned arc buffer to the arc. 12389412SAleksandr.Guzovskiy@Sun.COM */ 12399412SAleksandr.Guzovskiy@Sun.COM void 12409412SAleksandr.Guzovskiy@Sun.COM arc_return_buf(arc_buf_t *buf, void *tag) 12419412SAleksandr.Guzovskiy@Sun.COM { 12429412SAleksandr.Guzovskiy@Sun.COM arc_buf_hdr_t *hdr = buf->b_hdr; 12439412SAleksandr.Guzovskiy@Sun.COM 12449412SAleksandr.Guzovskiy@Sun.COM ASSERT(hdr->b_state == arc_anon); 12459412SAleksandr.Guzovskiy@Sun.COM ASSERT(buf->b_data != NULL); 12469412SAleksandr.Guzovskiy@Sun.COM VERIFY(refcount_remove(&hdr->b_refcnt, arc_onloan_tag) == 0); 12479412SAleksandr.Guzovskiy@Sun.COM VERIFY(refcount_add(&hdr->b_refcnt, tag) == 1); 12489412SAleksandr.Guzovskiy@Sun.COM 12499412SAleksandr.Guzovskiy@Sun.COM atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 12509412SAleksandr.Guzovskiy@Sun.COM } 12519412SAleksandr.Guzovskiy@Sun.COM 12522688Smaybee static arc_buf_t * 12532688Smaybee arc_buf_clone(arc_buf_t *from) 12541544Seschrock { 12552688Smaybee arc_buf_t *buf; 12562688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 12572688Smaybee uint64_t size = hdr->b_size; 12581544Seschrock 1259*10922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_state != arc_anon); 1260*10922SJeff.Bonwick@Sun.COM 12616245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 12622688Smaybee buf->b_hdr = hdr; 12632688Smaybee buf->b_data = NULL; 12642688Smaybee buf->b_efunc = NULL; 12652688Smaybee buf->b_private = NULL; 12662688Smaybee buf->b_next = hdr->b_buf; 12672688Smaybee hdr->b_buf = buf; 12682688Smaybee arc_get_data_buf(buf); 12692688Smaybee bcopy(from->b_data, buf->b_data, size); 12702688Smaybee hdr->b_datacnt += 1; 12712688Smaybee return (buf); 12721544Seschrock } 12731544Seschrock 12741544Seschrock void 12751544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 12761544Seschrock { 12772887Smaybee arc_buf_hdr_t *hdr; 12781544Seschrock kmutex_t *hash_lock; 12791544Seschrock 12802724Smaybee /* 12817545SMark.Maybee@Sun.COM * Check to see if this buffer is evicted. Callers 12827545SMark.Maybee@Sun.COM * must verify b_data != NULL to know if the add_ref 12837545SMark.Maybee@Sun.COM * was successful. 12842724Smaybee */ 12857545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 12867545SMark.Maybee@Sun.COM if (buf->b_data == NULL) { 12877545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 12882724Smaybee return; 12892887Smaybee } 12907545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 12917545SMark.Maybee@Sun.COM ASSERT(hdr != NULL); 12922887Smaybee hash_lock = HDR_LOCK(hdr); 12932724Smaybee mutex_enter(hash_lock); 12947545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 12957545SMark.Maybee@Sun.COM 12963403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 12971544Seschrock add_reference(hdr, hash_lock, tag); 12988582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 12992688Smaybee arc_access(hdr, hash_lock); 13002688Smaybee mutex_exit(hash_lock); 13013403Sbmc ARCSTAT_BUMP(arcstat_hits); 13023403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 13033403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 13043403Sbmc data, metadata, hits); 13051544Seschrock } 13061544Seschrock 13075450Sbrendan /* 13085450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 13095450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 13105450Sbrendan */ 13115450Sbrendan static void 13125450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 13135450Sbrendan void *data, size_t size) 13145450Sbrendan { 13155450Sbrendan if (HDR_L2_WRITING(hdr)) { 13165450Sbrendan l2arc_data_free_t *df; 13175450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 13185450Sbrendan df->l2df_data = data; 13195450Sbrendan df->l2df_size = size; 13205450Sbrendan df->l2df_func = free_func; 13215450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 13225450Sbrendan list_insert_head(l2arc_free_on_write, df); 13235450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 13245450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 13255450Sbrendan } else { 13265450Sbrendan free_func(data, size); 13275450Sbrendan } 13285450Sbrendan } 13295450Sbrendan 1330789Sahrens static void 13312688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 13321544Seschrock { 13331544Seschrock arc_buf_t **bufp; 13341544Seschrock 13351544Seschrock /* free up data associated with the buf */ 13361544Seschrock if (buf->b_data) { 13371544Seschrock arc_state_t *state = buf->b_hdr->b_state; 13381544Seschrock uint64_t size = buf->b_hdr->b_size; 13393290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 13401544Seschrock 13413093Sahrens arc_cksum_verify(buf); 1342*10922SJeff.Bonwick@Sun.COM 13432688Smaybee if (!recycle) { 13443290Sjohansen if (type == ARC_BUFC_METADATA) { 13455450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 13465450Sbrendan buf->b_data, size); 13478582SBrendan.Gregg@Sun.COM arc_space_return(size, ARC_SPACE_DATA); 13483290Sjohansen } else { 13493290Sjohansen ASSERT(type == ARC_BUFC_DATA); 13505450Sbrendan arc_buf_data_free(buf->b_hdr, 13515450Sbrendan zio_data_buf_free, buf->b_data, size); 13528582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -size); 13534309Smaybee atomic_add_64(&arc_size, -size); 13543290Sjohansen } 13552688Smaybee } 13561544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 13574309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 13584309Smaybee 13591544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 13603403Sbmc ASSERT(state != arc_anon); 13614309Smaybee 13624309Smaybee ASSERT3U(*cnt, >=, size); 13634309Smaybee atomic_add_64(cnt, -size); 13641544Seschrock } 13653403Sbmc ASSERT3U(state->arcs_size, >=, size); 13663403Sbmc atomic_add_64(&state->arcs_size, -size); 13671544Seschrock buf->b_data = NULL; 13681544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 13691544Seschrock buf->b_hdr->b_datacnt -= 1; 13701544Seschrock } 13711544Seschrock 13721544Seschrock /* only remove the buf if requested */ 13731544Seschrock if (!all) 13741544Seschrock return; 13751544Seschrock 13761544Seschrock /* remove the buf from the hdr list */ 13771544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 13781544Seschrock continue; 13791544Seschrock *bufp = buf->b_next; 13801544Seschrock 13811544Seschrock ASSERT(buf->b_efunc == NULL); 13821544Seschrock 13831544Seschrock /* clean up the buf */ 13841544Seschrock buf->b_hdr = NULL; 13851544Seschrock kmem_cache_free(buf_cache, buf); 13861544Seschrock } 13871544Seschrock 13881544Seschrock static void 13891544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1390789Sahrens { 1391789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 13923403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 13931544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1394*10922SJeff.Bonwick@Sun.COM l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 1395*10922SJeff.Bonwick@Sun.COM 1396*10922SJeff.Bonwick@Sun.COM if (l2hdr != NULL) { 1397*10922SJeff.Bonwick@Sun.COM boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx); 1398*10922SJeff.Bonwick@Sun.COM /* 1399*10922SJeff.Bonwick@Sun.COM * To prevent arc_free() and l2arc_evict() from 1400*10922SJeff.Bonwick@Sun.COM * attempting to free the same buffer at the same time, 1401*10922SJeff.Bonwick@Sun.COM * a FREE_IN_PROGRESS flag is given to arc_free() to 1402*10922SJeff.Bonwick@Sun.COM * give it priority. l2arc_evict() can't destroy this 1403*10922SJeff.Bonwick@Sun.COM * header while we are waiting on l2arc_buflist_mtx. 1404*10922SJeff.Bonwick@Sun.COM * 1405*10922SJeff.Bonwick@Sun.COM * The hdr may be removed from l2ad_buflist before we 1406*10922SJeff.Bonwick@Sun.COM * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 1407*10922SJeff.Bonwick@Sun.COM */ 1408*10922SJeff.Bonwick@Sun.COM if (!buflist_held) { 14095450Sbrendan mutex_enter(&l2arc_buflist_mtx); 1410*10922SJeff.Bonwick@Sun.COM l2hdr = hdr->b_l2hdr; 1411*10922SJeff.Bonwick@Sun.COM } 1412*10922SJeff.Bonwick@Sun.COM 1413*10922SJeff.Bonwick@Sun.COM if (l2hdr != NULL) { 1414*10922SJeff.Bonwick@Sun.COM list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 1415*10922SJeff.Bonwick@Sun.COM ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 1416*10922SJeff.Bonwick@Sun.COM kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 1417*10922SJeff.Bonwick@Sun.COM if (hdr->b_state == arc_l2c_only) 1418*10922SJeff.Bonwick@Sun.COM l2arc_hdr_stat_remove(); 1419*10922SJeff.Bonwick@Sun.COM hdr->b_l2hdr = NULL; 1420*10922SJeff.Bonwick@Sun.COM } 1421*10922SJeff.Bonwick@Sun.COM 1422*10922SJeff.Bonwick@Sun.COM if (!buflist_held) 14235450Sbrendan mutex_exit(&l2arc_buflist_mtx); 14245450Sbrendan } 14255450Sbrendan 1426789Sahrens if (!BUF_EMPTY(hdr)) { 14271544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1428789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1429789Sahrens hdr->b_birth = 0; 1430789Sahrens hdr->b_cksum0 = 0; 1431789Sahrens } 14321544Seschrock while (hdr->b_buf) { 1433789Sahrens arc_buf_t *buf = hdr->b_buf; 1434789Sahrens 14351544Seschrock if (buf->b_efunc) { 14361544Seschrock mutex_enter(&arc_eviction_mtx); 14377545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 14381544Seschrock ASSERT(buf->b_hdr != NULL); 14392688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 14401544Seschrock hdr->b_buf = buf->b_next; 14412887Smaybee buf->b_hdr = &arc_eviction_hdr; 14421544Seschrock buf->b_next = arc_eviction_list; 14431544Seschrock arc_eviction_list = buf; 14447545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 14451544Seschrock mutex_exit(&arc_eviction_mtx); 14461544Seschrock } else { 14472688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 14481544Seschrock } 1449789Sahrens } 14503093Sahrens if (hdr->b_freeze_cksum != NULL) { 14513093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 14523093Sahrens hdr->b_freeze_cksum = NULL; 14533093Sahrens } 14541544Seschrock 1455789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1456789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1457789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1458789Sahrens kmem_cache_free(hdr_cache, hdr); 1459789Sahrens } 1460789Sahrens 1461789Sahrens void 1462789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1463789Sahrens { 1464789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 14653403Sbmc int hashed = hdr->b_state != arc_anon; 14661544Seschrock 14671544Seschrock ASSERT(buf->b_efunc == NULL); 14681544Seschrock ASSERT(buf->b_data != NULL); 14691544Seschrock 14701544Seschrock if (hashed) { 14711544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 14721544Seschrock 14731544Seschrock mutex_enter(hash_lock); 14741544Seschrock (void) remove_reference(hdr, hash_lock, tag); 1475*10922SJeff.Bonwick@Sun.COM if (hdr->b_datacnt > 1) { 14762688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 1477*10922SJeff.Bonwick@Sun.COM } else { 1478*10922SJeff.Bonwick@Sun.COM ASSERT(buf == hdr->b_buf); 1479*10922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 14801544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1481*10922SJeff.Bonwick@Sun.COM } 14821544Seschrock mutex_exit(hash_lock); 14831544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 14841544Seschrock int destroy_hdr; 14851544Seschrock /* 14861544Seschrock * We are in the middle of an async write. Don't destroy 14871544Seschrock * this buffer unless the write completes before we finish 14881544Seschrock * decrementing the reference count. 14891544Seschrock */ 14901544Seschrock mutex_enter(&arc_eviction_mtx); 14911544Seschrock (void) remove_reference(hdr, NULL, tag); 14921544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 14931544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 14941544Seschrock mutex_exit(&arc_eviction_mtx); 14951544Seschrock if (destroy_hdr) 14961544Seschrock arc_hdr_destroy(hdr); 14971544Seschrock } else { 14981544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 14991544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 15002688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 15011544Seschrock } else { 15021544Seschrock arc_hdr_destroy(hdr); 15031544Seschrock } 15041544Seschrock } 15051544Seschrock } 15061544Seschrock 15071544Seschrock int 15081544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 15091544Seschrock { 15101544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1511789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 15121544Seschrock int no_callback = (buf->b_efunc == NULL); 15131544Seschrock 15143403Sbmc if (hdr->b_state == arc_anon) { 1515*10922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 15161544Seschrock arc_buf_free(buf, tag); 15171544Seschrock return (no_callback); 15181544Seschrock } 1519789Sahrens 1520789Sahrens mutex_enter(hash_lock); 15213403Sbmc ASSERT(hdr->b_state != arc_anon); 15221544Seschrock ASSERT(buf->b_data != NULL); 1523789Sahrens 15241544Seschrock (void) remove_reference(hdr, hash_lock, tag); 15251544Seschrock if (hdr->b_datacnt > 1) { 15261544Seschrock if (no_callback) 15272688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 15281544Seschrock } else if (no_callback) { 15291544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 1530*10922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 15311544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1532789Sahrens } 15331544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 15341544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1535789Sahrens mutex_exit(hash_lock); 15361544Seschrock return (no_callback); 1537789Sahrens } 1538789Sahrens 1539789Sahrens int 1540789Sahrens arc_buf_size(arc_buf_t *buf) 1541789Sahrens { 1542789Sahrens return (buf->b_hdr->b_size); 1543789Sahrens } 1544789Sahrens 1545789Sahrens /* 1546789Sahrens * Evict buffers from list until we've removed the specified number of 1547789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 15482688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 15492688Smaybee * - look for a buffer to evict that is `bytes' long. 15502688Smaybee * - return the data block from this buffer rather than freeing it. 15512688Smaybee * This flag is used by callers that are trying to make space for a 15522688Smaybee * new buffer in a full arc cache. 15535642Smaybee * 15545642Smaybee * This function makes a "best effort". It skips over any buffers 15555642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 15565642Smaybee * It may also return without evicting as much space as requested. 1557789Sahrens */ 15582688Smaybee static void * 15598636SMark.Maybee@Sun.COM arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 15603290Sjohansen arc_buf_contents_t type) 1561789Sahrens { 1562789Sahrens arc_state_t *evicted_state; 15632688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 15642918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 15654309Smaybee list_t *list = &state->arcs_list[type]; 1566789Sahrens kmutex_t *hash_lock; 15672688Smaybee boolean_t have_lock; 15682918Smaybee void *stolen = NULL; 1569789Sahrens 15703403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1571789Sahrens 15723403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1573789Sahrens 15743403Sbmc mutex_enter(&state->arcs_mtx); 15753403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1576789Sahrens 15774309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15784309Smaybee ab_prev = list_prev(list, ab); 15792391Smaybee /* prefetch buffers have a minimum lifespan */ 15802688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 15815642Smaybee (spa && ab->b_spa != spa) || 15822688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 15832688Smaybee lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) { 15842391Smaybee skipped++; 15852391Smaybee continue; 15862391Smaybee } 15872918Smaybee /* "lookahead" for better eviction candidate */ 15882918Smaybee if (recycle && ab->b_size != bytes && 15892918Smaybee ab_prev && ab_prev->b_size == bytes) 15902688Smaybee continue; 1591789Sahrens hash_lock = HDR_LOCK(ab); 15922688Smaybee have_lock = MUTEX_HELD(hash_lock); 15932688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1594789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 15951544Seschrock ASSERT(ab->b_datacnt > 0); 15961544Seschrock while (ab->b_buf) { 15971544Seschrock arc_buf_t *buf = ab->b_buf; 15987545SMark.Maybee@Sun.COM if (!rw_tryenter(&buf->b_lock, RW_WRITER)) { 15997545SMark.Maybee@Sun.COM missed += 1; 16007545SMark.Maybee@Sun.COM break; 16017545SMark.Maybee@Sun.COM } 16022688Smaybee if (buf->b_data) { 16031544Seschrock bytes_evicted += ab->b_size; 16043290Sjohansen if (recycle && ab->b_type == type && 16055450Sbrendan ab->b_size == bytes && 16065450Sbrendan !HDR_L2_WRITING(ab)) { 16072918Smaybee stolen = buf->b_data; 16082918Smaybee recycle = FALSE; 16092918Smaybee } 16102688Smaybee } 16111544Seschrock if (buf->b_efunc) { 16121544Seschrock mutex_enter(&arc_eviction_mtx); 16132918Smaybee arc_buf_destroy(buf, 16142918Smaybee buf->b_data == stolen, FALSE); 16151544Seschrock ab->b_buf = buf->b_next; 16162887Smaybee buf->b_hdr = &arc_eviction_hdr; 16171544Seschrock buf->b_next = arc_eviction_list; 16181544Seschrock arc_eviction_list = buf; 16191544Seschrock mutex_exit(&arc_eviction_mtx); 16207545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 16211544Seschrock } else { 16227545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 16232918Smaybee arc_buf_destroy(buf, 16242918Smaybee buf->b_data == stolen, TRUE); 16251544Seschrock } 16261544Seschrock } 162710357SBrendan.Gregg@Sun.COM 162810357SBrendan.Gregg@Sun.COM if (ab->b_l2hdr) { 162910357SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_evict_l2_cached, 163010357SBrendan.Gregg@Sun.COM ab->b_size); 163110357SBrendan.Gregg@Sun.COM } else { 163210357SBrendan.Gregg@Sun.COM if (l2arc_write_eligible(ab->b_spa, ab)) { 163310357SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_evict_l2_eligible, 163410357SBrendan.Gregg@Sun.COM ab->b_size); 163510357SBrendan.Gregg@Sun.COM } else { 163610357SBrendan.Gregg@Sun.COM ARCSTAT_INCR( 163710357SBrendan.Gregg@Sun.COM arcstat_evict_l2_ineligible, 163810357SBrendan.Gregg@Sun.COM ab->b_size); 163910357SBrendan.Gregg@Sun.COM } 164010357SBrendan.Gregg@Sun.COM } 164110357SBrendan.Gregg@Sun.COM 16427545SMark.Maybee@Sun.COM if (ab->b_datacnt == 0) { 16437545SMark.Maybee@Sun.COM arc_change_state(evicted_state, ab, hash_lock); 16447545SMark.Maybee@Sun.COM ASSERT(HDR_IN_HASH_TABLE(ab)); 16457545SMark.Maybee@Sun.COM ab->b_flags |= ARC_IN_HASH_TABLE; 16467545SMark.Maybee@Sun.COM ab->b_flags &= ~ARC_BUF_AVAILABLE; 16477545SMark.Maybee@Sun.COM DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 16487545SMark.Maybee@Sun.COM } 16492688Smaybee if (!have_lock) 16502688Smaybee mutex_exit(hash_lock); 16511544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1652789Sahrens break; 1653789Sahrens } else { 16542688Smaybee missed += 1; 1655789Sahrens } 1656789Sahrens } 16573403Sbmc 16583403Sbmc mutex_exit(&evicted_state->arcs_mtx); 16593403Sbmc mutex_exit(&state->arcs_mtx); 1660789Sahrens 1661789Sahrens if (bytes_evicted < bytes) 1662789Sahrens dprintf("only evicted %lld bytes from %x", 1663789Sahrens (longlong_t)bytes_evicted, state); 1664789Sahrens 16652688Smaybee if (skipped) 16663403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 16673403Sbmc 16682688Smaybee if (missed) 16693403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 16703403Sbmc 16714709Smaybee /* 16724709Smaybee * We have just evicted some date into the ghost state, make 16734709Smaybee * sure we also adjust the ghost state size if necessary. 16744709Smaybee */ 16754709Smaybee if (arc_no_grow && 16764709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 16774709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 16784709Smaybee arc_mru_ghost->arcs_size - arc_c; 16794709Smaybee 16804709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 16814709Smaybee int64_t todelete = 16824709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 16835642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 16844709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 16854709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 16864709Smaybee arc_mru_ghost->arcs_size + 16874709Smaybee arc_mfu_ghost->arcs_size - arc_c); 16885642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 16894709Smaybee } 16904709Smaybee } 16914709Smaybee 16922918Smaybee return (stolen); 1693789Sahrens } 1694789Sahrens 1695789Sahrens /* 1696789Sahrens * Remove buffers from list until we've removed the specified number of 1697789Sahrens * bytes. Destroy the buffers that are removed. 1698789Sahrens */ 1699789Sahrens static void 17008636SMark.Maybee@Sun.COM arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 1701789Sahrens { 1702789Sahrens arc_buf_hdr_t *ab, *ab_prev; 17034309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1704789Sahrens kmutex_t *hash_lock; 17051544Seschrock uint64_t bytes_deleted = 0; 17063700Sek110237 uint64_t bufs_skipped = 0; 1707789Sahrens 17081544Seschrock ASSERT(GHOST_STATE(state)); 1709789Sahrens top: 17103403Sbmc mutex_enter(&state->arcs_mtx); 17114309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 17124309Smaybee ab_prev = list_prev(list, ab); 17135642Smaybee if (spa && ab->b_spa != spa) 17145642Smaybee continue; 1715789Sahrens hash_lock = HDR_LOCK(ab); 1716789Sahrens if (mutex_tryenter(hash_lock)) { 17172391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 17181544Seschrock ASSERT(ab->b_buf == NULL); 17193403Sbmc ARCSTAT_BUMP(arcstat_deleted); 17201544Seschrock bytes_deleted += ab->b_size; 17215450Sbrendan 17225450Sbrendan if (ab->b_l2hdr != NULL) { 17235450Sbrendan /* 17245450Sbrendan * This buffer is cached on the 2nd Level ARC; 17255450Sbrendan * don't destroy the header. 17265450Sbrendan */ 17275450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 17285450Sbrendan mutex_exit(hash_lock); 17295450Sbrendan } else { 17305450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 17315450Sbrendan mutex_exit(hash_lock); 17325450Sbrendan arc_hdr_destroy(ab); 17335450Sbrendan } 17345450Sbrendan 1735789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1736789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1737789Sahrens break; 1738789Sahrens } else { 1739789Sahrens if (bytes < 0) { 17403403Sbmc mutex_exit(&state->arcs_mtx); 1741789Sahrens mutex_enter(hash_lock); 1742789Sahrens mutex_exit(hash_lock); 1743789Sahrens goto top; 1744789Sahrens } 1745789Sahrens bufs_skipped += 1; 1746789Sahrens } 1747789Sahrens } 17483403Sbmc mutex_exit(&state->arcs_mtx); 1749789Sahrens 17504309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 17514309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 17524309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 17534309Smaybee goto top; 17544309Smaybee } 17554309Smaybee 1756789Sahrens if (bufs_skipped) { 17573403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1758789Sahrens ASSERT(bytes >= 0); 1759789Sahrens } 1760789Sahrens 1761789Sahrens if (bytes_deleted < bytes) 1762789Sahrens dprintf("only deleted %lld bytes from %p", 1763789Sahrens (longlong_t)bytes_deleted, state); 1764789Sahrens } 1765789Sahrens 1766789Sahrens static void 1767789Sahrens arc_adjust(void) 1768789Sahrens { 17698582SBrendan.Gregg@Sun.COM int64_t adjustment, delta; 17708582SBrendan.Gregg@Sun.COM 17718582SBrendan.Gregg@Sun.COM /* 17728582SBrendan.Gregg@Sun.COM * Adjust MRU size 17738582SBrendan.Gregg@Sun.COM */ 17748582SBrendan.Gregg@Sun.COM 17758582SBrendan.Gregg@Sun.COM adjustment = MIN(arc_size - arc_c, 17768582SBrendan.Gregg@Sun.COM arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - arc_p); 17778582SBrendan.Gregg@Sun.COM 17788582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 17798582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 17808582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA); 17818582SBrendan.Gregg@Sun.COM adjustment -= delta; 17824309Smaybee } 17834309Smaybee 17848582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 17858582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 17868582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, 17875642Smaybee ARC_BUFC_METADATA); 1788789Sahrens } 1789789Sahrens 17908582SBrendan.Gregg@Sun.COM /* 17918582SBrendan.Gregg@Sun.COM * Adjust MFU size 17928582SBrendan.Gregg@Sun.COM */ 17938582SBrendan.Gregg@Sun.COM 17948582SBrendan.Gregg@Sun.COM adjustment = arc_size - arc_c; 17958582SBrendan.Gregg@Sun.COM 17968582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 17978582SBrendan.Gregg@Sun.COM delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 17988582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA); 17998582SBrendan.Gregg@Sun.COM adjustment -= delta; 18008582SBrendan.Gregg@Sun.COM } 18018582SBrendan.Gregg@Sun.COM 18028582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 18038582SBrendan.Gregg@Sun.COM int64_t delta = MIN(adjustment, 18048582SBrendan.Gregg@Sun.COM arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 18058582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, 18068582SBrendan.Gregg@Sun.COM ARC_BUFC_METADATA); 18078582SBrendan.Gregg@Sun.COM } 18088582SBrendan.Gregg@Sun.COM 18098582SBrendan.Gregg@Sun.COM /* 18108582SBrendan.Gregg@Sun.COM * Adjust ghost lists 18118582SBrendan.Gregg@Sun.COM */ 18128582SBrendan.Gregg@Sun.COM 18138582SBrendan.Gregg@Sun.COM adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 18148582SBrendan.Gregg@Sun.COM 18158582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 18168582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru_ghost->arcs_size, adjustment); 18178582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mru_ghost, NULL, delta); 18188582SBrendan.Gregg@Sun.COM } 18198582SBrendan.Gregg@Sun.COM 18208582SBrendan.Gregg@Sun.COM adjustment = 18218582SBrendan.Gregg@Sun.COM arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 18228582SBrendan.Gregg@Sun.COM 18238582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 18248582SBrendan.Gregg@Sun.COM delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 18258582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mfu_ghost, NULL, delta); 1826789Sahrens } 1827789Sahrens } 1828789Sahrens 18291544Seschrock static void 18301544Seschrock arc_do_user_evicts(void) 18311544Seschrock { 18321544Seschrock mutex_enter(&arc_eviction_mtx); 18331544Seschrock while (arc_eviction_list != NULL) { 18341544Seschrock arc_buf_t *buf = arc_eviction_list; 18351544Seschrock arc_eviction_list = buf->b_next; 18367545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 18371544Seschrock buf->b_hdr = NULL; 18387545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 18391544Seschrock mutex_exit(&arc_eviction_mtx); 18401544Seschrock 18411819Smaybee if (buf->b_efunc != NULL) 18421819Smaybee VERIFY(buf->b_efunc(buf) == 0); 18431544Seschrock 18441544Seschrock buf->b_efunc = NULL; 18451544Seschrock buf->b_private = NULL; 18461544Seschrock kmem_cache_free(buf_cache, buf); 18471544Seschrock mutex_enter(&arc_eviction_mtx); 18481544Seschrock } 18491544Seschrock mutex_exit(&arc_eviction_mtx); 18501544Seschrock } 18511544Seschrock 1852789Sahrens /* 18535642Smaybee * Flush all *evictable* data from the cache for the given spa. 1854789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1855789Sahrens */ 1856789Sahrens void 18575642Smaybee arc_flush(spa_t *spa) 1858789Sahrens { 18598636SMark.Maybee@Sun.COM uint64_t guid = 0; 18608636SMark.Maybee@Sun.COM 18618636SMark.Maybee@Sun.COM if (spa) 18628636SMark.Maybee@Sun.COM guid = spa_guid(spa); 18638636SMark.Maybee@Sun.COM 18645642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 18658636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 18665642Smaybee if (spa) 18675642Smaybee break; 18685642Smaybee } 18695642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 18708636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 18715642Smaybee if (spa) 18725642Smaybee break; 18735642Smaybee } 18745642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 18758636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 18765642Smaybee if (spa) 18775642Smaybee break; 18785642Smaybee } 18795642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 18808636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 18815642Smaybee if (spa) 18825642Smaybee break; 18835642Smaybee } 18845642Smaybee 18858636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mru_ghost, guid, -1); 18868636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mfu_ghost, guid, -1); 18871544Seschrock 18881544Seschrock mutex_enter(&arc_reclaim_thr_lock); 18891544Seschrock arc_do_user_evicts(); 18901544Seschrock mutex_exit(&arc_reclaim_thr_lock); 18915642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1892789Sahrens } 1893789Sahrens 1894789Sahrens void 18953158Smaybee arc_shrink(void) 1896789Sahrens { 18973403Sbmc if (arc_c > arc_c_min) { 18983158Smaybee uint64_t to_free; 1899789Sahrens 19002048Sstans #ifdef _KERNEL 19013403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 19022048Sstans #else 19033403Sbmc to_free = arc_c >> arc_shrink_shift; 19042048Sstans #endif 19053403Sbmc if (arc_c > arc_c_min + to_free) 19063403Sbmc atomic_add_64(&arc_c, -to_free); 19073158Smaybee else 19083403Sbmc arc_c = arc_c_min; 19092048Sstans 19103403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 19113403Sbmc if (arc_c > arc_size) 19123403Sbmc arc_c = MAX(arc_size, arc_c_min); 19133403Sbmc if (arc_p > arc_c) 19143403Sbmc arc_p = (arc_c >> 1); 19153403Sbmc ASSERT(arc_c >= arc_c_min); 19163403Sbmc ASSERT((int64_t)arc_p >= 0); 19173158Smaybee } 1918789Sahrens 19193403Sbmc if (arc_size > arc_c) 19203158Smaybee arc_adjust(); 1921789Sahrens } 1922789Sahrens 1923789Sahrens static int 1924789Sahrens arc_reclaim_needed(void) 1925789Sahrens { 1926789Sahrens uint64_t extra; 1927789Sahrens 1928789Sahrens #ifdef _KERNEL 19292048Sstans 19302048Sstans if (needfree) 19312048Sstans return (1); 19322048Sstans 1933789Sahrens /* 1934789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1935789Sahrens */ 1936789Sahrens extra = desfree; 1937789Sahrens 1938789Sahrens /* 1939789Sahrens * check that we're out of range of the pageout scanner. It starts to 1940789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1941789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1942789Sahrens * number of needed free pages. We add extra pages here to make sure 1943789Sahrens * the scanner doesn't start up while we're freeing memory. 1944789Sahrens */ 1945789Sahrens if (freemem < lotsfree + needfree + extra) 1946789Sahrens return (1); 1947789Sahrens 1948789Sahrens /* 1949789Sahrens * check to make sure that swapfs has enough space so that anon 19505450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1951789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1952789Sahrens * swap pages. We also add a bit of extra here just to prevent 1953789Sahrens * circumstances from getting really dire. 1954789Sahrens */ 1955789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1956789Sahrens return (1); 1957789Sahrens 19581936Smaybee #if defined(__i386) 1959789Sahrens /* 1960789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1961789Sahrens * kernel heap space before we ever run out of available physical 1962789Sahrens * memory. Most checks of the size of the heap_area compare against 1963789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1964789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1965789Sahrens * which is so low that it's useless. In this comparison, we seek to 1966789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 19675450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1968789Sahrens * free) 1969789Sahrens */ 1970789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1971789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1972789Sahrens return (1); 1973789Sahrens #endif 1974789Sahrens 1975789Sahrens #else 1976789Sahrens if (spa_get_random(100) == 0) 1977789Sahrens return (1); 1978789Sahrens #endif 1979789Sahrens return (0); 1980789Sahrens } 1981789Sahrens 1982789Sahrens static void 1983789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1984789Sahrens { 1985789Sahrens size_t i; 1986789Sahrens kmem_cache_t *prev_cache = NULL; 19873290Sjohansen kmem_cache_t *prev_data_cache = NULL; 1988789Sahrens extern kmem_cache_t *zio_buf_cache[]; 19893290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 1990789Sahrens 19911484Sek110237 #ifdef _KERNEL 19924309Smaybee if (arc_meta_used >= arc_meta_limit) { 19934309Smaybee /* 19944309Smaybee * We are exceeding our meta-data cache limit. 19954309Smaybee * Purge some DNLC entries to release holds on meta-data. 19964309Smaybee */ 19974309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 19984309Smaybee } 19991936Smaybee #if defined(__i386) 20001936Smaybee /* 20011936Smaybee * Reclaim unused memory from all kmem caches. 20021936Smaybee */ 20031936Smaybee kmem_reap(); 20041936Smaybee #endif 20051484Sek110237 #endif 20061484Sek110237 2007789Sahrens /* 20085450Sbrendan * An aggressive reclamation will shrink the cache size as well as 20091544Seschrock * reap free buffers from the arc kmem caches. 2010789Sahrens */ 2011789Sahrens if (strat == ARC_RECLAIM_AGGR) 20123158Smaybee arc_shrink(); 2013789Sahrens 2014789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2015789Sahrens if (zio_buf_cache[i] != prev_cache) { 2016789Sahrens prev_cache = zio_buf_cache[i]; 2017789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 2018789Sahrens } 20193290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 20203290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 20213290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 20223290Sjohansen } 2023789Sahrens } 20241544Seschrock kmem_cache_reap_now(buf_cache); 20251544Seschrock kmem_cache_reap_now(hdr_cache); 2026789Sahrens } 2027789Sahrens 2028789Sahrens static void 2029789Sahrens arc_reclaim_thread(void) 2030789Sahrens { 2031789Sahrens clock_t growtime = 0; 2032789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2033789Sahrens callb_cpr_t cpr; 2034789Sahrens 2035789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2036789Sahrens 2037789Sahrens mutex_enter(&arc_reclaim_thr_lock); 2038789Sahrens while (arc_thread_exit == 0) { 2039789Sahrens if (arc_reclaim_needed()) { 2040789Sahrens 20413403Sbmc if (arc_no_grow) { 2042789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 2043789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2044789Sahrens } else { 2045789Sahrens last_reclaim = ARC_RECLAIM_CONS; 2046789Sahrens } 2047789Sahrens } else { 20483403Sbmc arc_no_grow = TRUE; 2049789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2050789Sahrens membar_producer(); 2051789Sahrens } 2052789Sahrens 2053789Sahrens /* reset the growth delay for every reclaim */ 2054789Sahrens growtime = lbolt + (arc_grow_retry * hz); 2055789Sahrens 2056789Sahrens arc_kmem_reap_now(last_reclaim); 20576987Sbrendan arc_warm = B_TRUE; 2058789Sahrens 20594309Smaybee } else if (arc_no_grow && lbolt >= growtime) { 20603403Sbmc arc_no_grow = FALSE; 2061789Sahrens } 2062789Sahrens 20633403Sbmc if (2 * arc_c < arc_size + 20643403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 20653298Smaybee arc_adjust(); 20663298Smaybee 20671544Seschrock if (arc_eviction_list != NULL) 20681544Seschrock arc_do_user_evicts(); 20691544Seschrock 2070789Sahrens /* block until needed, or one second, whichever is shorter */ 2071789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 2072789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 2073789Sahrens &arc_reclaim_thr_lock, (lbolt + hz)); 2074789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2075789Sahrens } 2076789Sahrens 2077789Sahrens arc_thread_exit = 0; 2078789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 2079789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2080789Sahrens thread_exit(); 2081789Sahrens } 2082789Sahrens 20831544Seschrock /* 20841544Seschrock * Adapt arc info given the number of bytes we are trying to add and 20851544Seschrock * the state that we are comming from. This function is only called 20861544Seschrock * when we are adding new content to the cache. 20871544Seschrock */ 2088789Sahrens static void 20891544Seschrock arc_adapt(int bytes, arc_state_t *state) 2090789Sahrens { 20911544Seschrock int mult; 20928582SBrendan.Gregg@Sun.COM uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 20931544Seschrock 20945450Sbrendan if (state == arc_l2c_only) 20955450Sbrendan return; 20965450Sbrendan 20971544Seschrock ASSERT(bytes > 0); 2098789Sahrens /* 20991544Seschrock * Adapt the target size of the MRU list: 21001544Seschrock * - if we just hit in the MRU ghost list, then increase 21011544Seschrock * the target size of the MRU list. 21021544Seschrock * - if we just hit in the MFU ghost list, then increase 21031544Seschrock * the target size of the MFU list by decreasing the 21041544Seschrock * target size of the MRU list. 2105789Sahrens */ 21063403Sbmc if (state == arc_mru_ghost) { 21073403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 21083403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 21091544Seschrock 21108582SBrendan.Gregg@Sun.COM arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 21113403Sbmc } else if (state == arc_mfu_ghost) { 21128582SBrendan.Gregg@Sun.COM uint64_t delta; 21138582SBrendan.Gregg@Sun.COM 21143403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 21153403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 21161544Seschrock 21178582SBrendan.Gregg@Sun.COM delta = MIN(bytes * mult, arc_p); 21188582SBrendan.Gregg@Sun.COM arc_p = MAX(arc_p_min, arc_p - delta); 21191544Seschrock } 21203403Sbmc ASSERT((int64_t)arc_p >= 0); 2121789Sahrens 2122789Sahrens if (arc_reclaim_needed()) { 2123789Sahrens cv_signal(&arc_reclaim_thr_cv); 2124789Sahrens return; 2125789Sahrens } 2126789Sahrens 21273403Sbmc if (arc_no_grow) 2128789Sahrens return; 2129789Sahrens 21303403Sbmc if (arc_c >= arc_c_max) 21311544Seschrock return; 21321544Seschrock 2133789Sahrens /* 21341544Seschrock * If we're within (2 * maxblocksize) bytes of the target 21351544Seschrock * cache size, increment the target cache size 2136789Sahrens */ 21373403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 21383403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 21393403Sbmc if (arc_c > arc_c_max) 21403403Sbmc arc_c = arc_c_max; 21413403Sbmc else if (state == arc_anon) 21423403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 21433403Sbmc if (arc_p > arc_c) 21443403Sbmc arc_p = arc_c; 2145789Sahrens } 21463403Sbmc ASSERT((int64_t)arc_p >= 0); 2147789Sahrens } 2148789Sahrens 2149789Sahrens /* 21501544Seschrock * Check if the cache has reached its limits and eviction is required 21511544Seschrock * prior to insert. 2152789Sahrens */ 2153789Sahrens static int 21544309Smaybee arc_evict_needed(arc_buf_contents_t type) 2155789Sahrens { 21564309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 21574309Smaybee return (1); 21584309Smaybee 21594309Smaybee #ifdef _KERNEL 21604309Smaybee /* 21614309Smaybee * If zio data pages are being allocated out of a separate heap segment, 21624309Smaybee * then enforce that the size of available vmem for this area remains 21634309Smaybee * above about 1/32nd free. 21644309Smaybee */ 21654309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 21664309Smaybee vmem_size(zio_arena, VMEM_FREE) < 21674309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 21684309Smaybee return (1); 21694309Smaybee #endif 21704309Smaybee 2171789Sahrens if (arc_reclaim_needed()) 2172789Sahrens return (1); 2173789Sahrens 21743403Sbmc return (arc_size > arc_c); 2175789Sahrens } 2176789Sahrens 2177789Sahrens /* 21782688Smaybee * The buffer, supplied as the first argument, needs a data block. 21792688Smaybee * So, if we are at cache max, determine which cache should be victimized. 21802688Smaybee * We have the following cases: 2181789Sahrens * 21823403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2183789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2184789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2185789Sahrens * 21863403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2187789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2188789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2189789Sahrens * entries. 2190789Sahrens * 21913403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2192789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2193789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2194789Sahrens * the MFU side, so the MRU side needs to be victimized. 2195789Sahrens * 21963403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2197789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2198789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2199789Sahrens */ 2200789Sahrens static void 22012688Smaybee arc_get_data_buf(arc_buf_t *buf) 2202789Sahrens { 22033290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 22043290Sjohansen uint64_t size = buf->b_hdr->b_size; 22053290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 22062688Smaybee 22072688Smaybee arc_adapt(size, state); 2208789Sahrens 22092688Smaybee /* 22102688Smaybee * We have not yet reached cache maximum size, 22112688Smaybee * just allocate a new buffer. 22122688Smaybee */ 22134309Smaybee if (!arc_evict_needed(type)) { 22143290Sjohansen if (type == ARC_BUFC_METADATA) { 22153290Sjohansen buf->b_data = zio_buf_alloc(size); 22168582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 22173290Sjohansen } else { 22183290Sjohansen ASSERT(type == ARC_BUFC_DATA); 22193290Sjohansen buf->b_data = zio_data_buf_alloc(size); 22208582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 22214309Smaybee atomic_add_64(&arc_size, size); 22223290Sjohansen } 22232688Smaybee goto out; 22242688Smaybee } 22252688Smaybee 22262688Smaybee /* 22272688Smaybee * If we are prefetching from the mfu ghost list, this buffer 22282688Smaybee * will end up on the mru list; so steal space from there. 22292688Smaybee */ 22303403Sbmc if (state == arc_mfu_ghost) 22313403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 22323403Sbmc else if (state == arc_mru_ghost) 22333403Sbmc state = arc_mru; 2234789Sahrens 22353403Sbmc if (state == arc_mru || state == arc_anon) { 22363403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 22378582SBrendan.Gregg@Sun.COM state = (arc_mfu->arcs_lsize[type] >= size && 22384309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2239789Sahrens } else { 22402688Smaybee /* MFU cases */ 22413403Sbmc uint64_t mfu_space = arc_c - arc_p; 22428582SBrendan.Gregg@Sun.COM state = (arc_mru->arcs_lsize[type] >= size && 22434309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 22442688Smaybee } 22455642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 22463290Sjohansen if (type == ARC_BUFC_METADATA) { 22473290Sjohansen buf->b_data = zio_buf_alloc(size); 22488582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 22493290Sjohansen } else { 22503290Sjohansen ASSERT(type == ARC_BUFC_DATA); 22513290Sjohansen buf->b_data = zio_data_buf_alloc(size); 22528582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 22534309Smaybee atomic_add_64(&arc_size, size); 22543290Sjohansen } 22553403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 22562688Smaybee } 22572688Smaybee ASSERT(buf->b_data != NULL); 22582688Smaybee out: 22592688Smaybee /* 22602688Smaybee * Update the state size. Note that ghost states have a 22612688Smaybee * "ghost size" and so don't need to be updated. 22622688Smaybee */ 22632688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 22642688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 22652688Smaybee 22663403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 22672688Smaybee if (list_link_active(&hdr->b_arc_node)) { 22682688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 22694309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2270789Sahrens } 22713298Smaybee /* 22723298Smaybee * If we are growing the cache, and we are adding anonymous 22733403Sbmc * data, and we have outgrown arc_p, update arc_p 22743298Smaybee */ 22753403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 22763403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 22773403Sbmc arc_p = MIN(arc_c, arc_p + size); 2278789Sahrens } 2279789Sahrens } 2280789Sahrens 2281789Sahrens /* 2282789Sahrens * This routine is called whenever a buffer is accessed. 22831544Seschrock * NOTE: the hash lock is dropped in this function. 2284789Sahrens */ 2285789Sahrens static void 22862688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2287789Sahrens { 2288789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2289789Sahrens 22903403Sbmc if (buf->b_state == arc_anon) { 2291789Sahrens /* 2292789Sahrens * This buffer is not in the cache, and does not 2293789Sahrens * appear in our "ghost" list. Add the new buffer 2294789Sahrens * to the MRU state. 2295789Sahrens */ 2296789Sahrens 2297789Sahrens ASSERT(buf->b_arc_access == 0); 2298789Sahrens buf->b_arc_access = lbolt; 22991544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 23003403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2301789Sahrens 23023403Sbmc } else if (buf->b_state == arc_mru) { 2303789Sahrens /* 23042391Smaybee * If this buffer is here because of a prefetch, then either: 23052391Smaybee * - clear the flag if this is a "referencing" read 23062391Smaybee * (any subsequent access will bump this into the MFU state). 23072391Smaybee * or 23082391Smaybee * - move the buffer to the head of the list if this is 23092391Smaybee * another prefetch (to make it less likely to be evicted). 2310789Sahrens */ 2311789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 23122391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 23132391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23142391Smaybee } else { 23152391Smaybee buf->b_flags &= ~ARC_PREFETCH; 23163403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23172391Smaybee } 23182391Smaybee buf->b_arc_access = lbolt; 2319789Sahrens return; 2320789Sahrens } 2321789Sahrens 2322789Sahrens /* 2323789Sahrens * This buffer has been "accessed" only once so far, 2324789Sahrens * but it is still in the cache. Move it to the MFU 2325789Sahrens * state. 2326789Sahrens */ 2327789Sahrens if (lbolt > buf->b_arc_access + ARC_MINTIME) { 2328789Sahrens /* 2329789Sahrens * More than 125ms have passed since we 2330789Sahrens * instantiated this buffer. Move it to the 2331789Sahrens * most frequently used state. 2332789Sahrens */ 2333789Sahrens buf->b_arc_access = lbolt; 23341544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23353403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2336789Sahrens } 23373403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23383403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2339789Sahrens arc_state_t *new_state; 2340789Sahrens /* 2341789Sahrens * This buffer has been "accessed" recently, but 2342789Sahrens * was evicted from the cache. Move it to the 2343789Sahrens * MFU state. 2344789Sahrens */ 2345789Sahrens 2346789Sahrens if (buf->b_flags & ARC_PREFETCH) { 23473403Sbmc new_state = arc_mru; 23482391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 23492391Smaybee buf->b_flags &= ~ARC_PREFETCH; 23501544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2351789Sahrens } else { 23523403Sbmc new_state = arc_mfu; 23531544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2354789Sahrens } 2355789Sahrens 2356789Sahrens buf->b_arc_access = lbolt; 2357789Sahrens arc_change_state(new_state, buf, hash_lock); 2358789Sahrens 23593403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 23603403Sbmc } else if (buf->b_state == arc_mfu) { 2361789Sahrens /* 2362789Sahrens * This buffer has been accessed more than once and is 2363789Sahrens * still in the cache. Keep it in the MFU state. 2364789Sahrens * 23652391Smaybee * NOTE: an add_reference() that occurred when we did 23662391Smaybee * the arc_read() will have kicked this off the list. 23672391Smaybee * If it was a prefetch, we will explicitly move it to 23682391Smaybee * the head of the list now. 2369789Sahrens */ 23702391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 23712391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 23722391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23732391Smaybee } 23743403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 23752391Smaybee buf->b_arc_access = lbolt; 23763403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 23773403Sbmc arc_state_t *new_state = arc_mfu; 2378789Sahrens /* 2379789Sahrens * This buffer has been accessed more than once but has 2380789Sahrens * been evicted from the cache. Move it back to the 2381789Sahrens * MFU state. 2382789Sahrens */ 2383789Sahrens 23842391Smaybee if (buf->b_flags & ARC_PREFETCH) { 23852391Smaybee /* 23862391Smaybee * This is a prefetch access... 23872391Smaybee * move this block back to the MRU state. 23882391Smaybee */ 23892391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 23903403Sbmc new_state = arc_mru; 23912391Smaybee } 23922391Smaybee 2393789Sahrens buf->b_arc_access = lbolt; 23941544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23952391Smaybee arc_change_state(new_state, buf, hash_lock); 2396789Sahrens 23973403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 23985450Sbrendan } else if (buf->b_state == arc_l2c_only) { 23995450Sbrendan /* 24005450Sbrendan * This buffer is on the 2nd Level ARC. 24015450Sbrendan */ 24025450Sbrendan 24035450Sbrendan buf->b_arc_access = lbolt; 24045450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 24055450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2406789Sahrens } else { 2407789Sahrens ASSERT(!"invalid arc state"); 2408789Sahrens } 2409789Sahrens } 2410789Sahrens 2411789Sahrens /* a generic arc_done_func_t which you can use */ 2412789Sahrens /* ARGSUSED */ 2413789Sahrens void 2414789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2415789Sahrens { 2416789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 24171544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2418789Sahrens } 2419789Sahrens 24204309Smaybee /* a generic arc_done_func_t */ 2421789Sahrens void 2422789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2423789Sahrens { 2424789Sahrens arc_buf_t **bufp = arg; 2425789Sahrens if (zio && zio->io_error) { 24261544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2427789Sahrens *bufp = NULL; 2428789Sahrens } else { 2429789Sahrens *bufp = buf; 2430789Sahrens } 2431789Sahrens } 2432789Sahrens 2433789Sahrens static void 2434789Sahrens arc_read_done(zio_t *zio) 2435789Sahrens { 24361589Smaybee arc_buf_hdr_t *hdr, *found; 2437789Sahrens arc_buf_t *buf; 2438789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2439789Sahrens kmutex_t *hash_lock; 2440789Sahrens arc_callback_t *callback_list, *acb; 2441789Sahrens int freeable = FALSE; 2442789Sahrens 2443789Sahrens buf = zio->io_private; 2444789Sahrens hdr = buf->b_hdr; 2445789Sahrens 24461589Smaybee /* 24471589Smaybee * The hdr was inserted into hash-table and removed from lists 24481589Smaybee * prior to starting I/O. We should find this header, since 24491589Smaybee * it's in the hash table, and it should be legit since it's 24501589Smaybee * not possible to evict it during the I/O. The only possible 24511589Smaybee * reason for it not to be found is if we were freed during the 24521589Smaybee * read. 24531589Smaybee */ 24548636SMark.Maybee@Sun.COM found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 24553093Sahrens &hash_lock); 2456789Sahrens 24571589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 24585450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 24595450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 24605450Sbrendan 24616987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 24625450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 24637237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2464789Sahrens 2465789Sahrens /* byteswap if necessary */ 2466789Sahrens callback_list = hdr->b_acb; 2467789Sahrens ASSERT(callback_list != NULL); 246810839Swilliam.gorrell@sun.com if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 24697046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 24707046Sahrens byteswap_uint64_array : 24717046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 24727046Sahrens func(buf->b_data, hdr->b_size); 24737046Sahrens } 2474789Sahrens 24755450Sbrendan arc_cksum_compute(buf, B_FALSE); 24763093Sahrens 2477*10922SJeff.Bonwick@Sun.COM if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 2478*10922SJeff.Bonwick@Sun.COM /* 2479*10922SJeff.Bonwick@Sun.COM * Only call arc_access on anonymous buffers. This is because 2480*10922SJeff.Bonwick@Sun.COM * if we've issued an I/O for an evicted buffer, we've already 2481*10922SJeff.Bonwick@Sun.COM * called arc_access (to prevent any simultaneous readers from 2482*10922SJeff.Bonwick@Sun.COM * getting confused). 2483*10922SJeff.Bonwick@Sun.COM */ 2484*10922SJeff.Bonwick@Sun.COM arc_access(hdr, hash_lock); 2485*10922SJeff.Bonwick@Sun.COM } 2486*10922SJeff.Bonwick@Sun.COM 2487789Sahrens /* create copies of the data buffer for the callers */ 2488789Sahrens abuf = buf; 2489789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2490789Sahrens if (acb->acb_done) { 24912688Smaybee if (abuf == NULL) 24922688Smaybee abuf = arc_buf_clone(buf); 2493789Sahrens acb->acb_buf = abuf; 2494789Sahrens abuf = NULL; 2495789Sahrens } 2496789Sahrens } 2497789Sahrens hdr->b_acb = NULL; 2498789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 24991544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 2500*10922SJeff.Bonwick@Sun.COM if (abuf == buf) { 2501*10922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 2502*10922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 25031544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 2504*10922SJeff.Bonwick@Sun.COM } 2505789Sahrens 2506789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2507789Sahrens 2508789Sahrens if (zio->io_error != 0) { 2509789Sahrens hdr->b_flags |= ARC_IO_ERROR; 25103403Sbmc if (hdr->b_state != arc_anon) 25113403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 25121544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 25131544Seschrock buf_hash_remove(hdr); 2514789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2515789Sahrens } 2516789Sahrens 25171544Seschrock /* 25182391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 25192391Smaybee * that the hdr (and hence the cv) might be freed before we get to 25202391Smaybee * the cv_broadcast(). 25211544Seschrock */ 25221544Seschrock cv_broadcast(&hdr->b_cv); 25231544Seschrock 25241589Smaybee if (hash_lock) { 25252688Smaybee mutex_exit(hash_lock); 2526789Sahrens } else { 2527789Sahrens /* 2528789Sahrens * This block was freed while we waited for the read to 2529789Sahrens * complete. It has been removed from the hash table and 2530789Sahrens * moved to the anonymous state (so that it won't show up 2531789Sahrens * in the cache). 2532789Sahrens */ 25333403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2534789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2535789Sahrens } 2536789Sahrens 2537789Sahrens /* execute each callback and free its structure */ 2538789Sahrens while ((acb = callback_list) != NULL) { 2539789Sahrens if (acb->acb_done) 2540789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2541789Sahrens 2542789Sahrens if (acb->acb_zio_dummy != NULL) { 2543789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2544789Sahrens zio_nowait(acb->acb_zio_dummy); 2545789Sahrens } 2546789Sahrens 2547789Sahrens callback_list = acb->acb_next; 2548789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2549789Sahrens } 2550789Sahrens 2551789Sahrens if (freeable) 25521544Seschrock arc_hdr_destroy(hdr); 2553789Sahrens } 2554789Sahrens 2555789Sahrens /* 2556789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2557789Sahrens * cache. If the block is found in the cache, invoke the provided 2558789Sahrens * callback immediately and return. Note that the `zio' parameter 2559789Sahrens * in the callback will be NULL in this case, since no IO was 2560789Sahrens * required. If the block is not in the cache pass the read request 2561789Sahrens * on to the spa with a substitute callback function, so that the 2562789Sahrens * requested block will be added to the cache. 2563789Sahrens * 2564789Sahrens * If a read request arrives for a block that has a read in-progress, 2565789Sahrens * either wait for the in-progress read to complete (and return the 2566789Sahrens * results); or, if this is a read with a "done" func, add a record 2567789Sahrens * to the read to invoke the "done" func when the read completes, 2568789Sahrens * and return; or just return. 2569789Sahrens * 2570789Sahrens * arc_read_done() will invoke all the requested "done" functions 2571789Sahrens * for readers of this block. 25727046Sahrens * 25737046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 25747046Sahrens * for the bp. But if you know you don't need locking, you can use 25758213SSuhasini.Peddada@Sun.COM * arc_read_bp. 2576789Sahrens */ 2577789Sahrens int 2578*10922SJeff.Bonwick@Sun.COM arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf, 25797237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 25807046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 25817046Sahrens { 25827046Sahrens int err; 25837046Sahrens 25847046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 25857046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 25867545SMark.Maybee@Sun.COM rw_enter(&pbuf->b_lock, RW_READER); 25877046Sahrens 25887046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 25897237Sek110237 zio_flags, arc_flags, zb); 25907545SMark.Maybee@Sun.COM rw_exit(&pbuf->b_lock); 25919396SMatthew.Ahrens@Sun.COM 25927046Sahrens return (err); 25937046Sahrens } 25947046Sahrens 25957046Sahrens int 2596*10922SJeff.Bonwick@Sun.COM arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp, 25977237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 25987046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2599789Sahrens { 2600789Sahrens arc_buf_hdr_t *hdr; 2601789Sahrens arc_buf_t *buf; 2602789Sahrens kmutex_t *hash_lock; 26035450Sbrendan zio_t *rzio; 26048636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 2605789Sahrens 2606789Sahrens top: 2607*10922SJeff.Bonwick@Sun.COM hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 2608*10922SJeff.Bonwick@Sun.COM &hash_lock); 26091544Seschrock if (hdr && hdr->b_datacnt > 0) { 2610789Sahrens 26112391Smaybee *arc_flags |= ARC_CACHED; 26122391Smaybee 2613789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 26142391Smaybee 26152391Smaybee if (*arc_flags & ARC_WAIT) { 26162391Smaybee cv_wait(&hdr->b_cv, hash_lock); 26172391Smaybee mutex_exit(hash_lock); 26182391Smaybee goto top; 26192391Smaybee } 26202391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 26212391Smaybee 26222391Smaybee if (done) { 2623789Sahrens arc_callback_t *acb = NULL; 2624789Sahrens 2625789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2626789Sahrens KM_SLEEP); 2627789Sahrens acb->acb_done = done; 2628789Sahrens acb->acb_private = private; 2629789Sahrens if (pio != NULL) 2630789Sahrens acb->acb_zio_dummy = zio_null(pio, 26318632SBill.Moore@Sun.COM spa, NULL, NULL, NULL, zio_flags); 2632789Sahrens 2633789Sahrens ASSERT(acb->acb_done != NULL); 2634789Sahrens acb->acb_next = hdr->b_acb; 2635789Sahrens hdr->b_acb = acb; 2636789Sahrens add_reference(hdr, hash_lock, private); 2637789Sahrens mutex_exit(hash_lock); 2638789Sahrens return (0); 2639789Sahrens } 2640789Sahrens mutex_exit(hash_lock); 2641789Sahrens return (0); 2642789Sahrens } 2643789Sahrens 26443403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2645789Sahrens 26461544Seschrock if (done) { 26472688Smaybee add_reference(hdr, hash_lock, private); 26481544Seschrock /* 26491544Seschrock * If this block is already in use, create a new 26501544Seschrock * copy of the data so that we will be guaranteed 26511544Seschrock * that arc_release() will always succeed. 26521544Seschrock */ 26531544Seschrock buf = hdr->b_buf; 26541544Seschrock ASSERT(buf); 26551544Seschrock ASSERT(buf->b_data); 26562688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 26571544Seschrock ASSERT(buf->b_efunc == NULL); 26581544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 26592688Smaybee } else { 26602688Smaybee buf = arc_buf_clone(buf); 26611544Seschrock } 2662*10922SJeff.Bonwick@Sun.COM 26632391Smaybee } else if (*arc_flags & ARC_PREFETCH && 26642391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 26652391Smaybee hdr->b_flags |= ARC_PREFETCH; 2666789Sahrens } 2667789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 26682688Smaybee arc_access(hdr, hash_lock); 26697237Sek110237 if (*arc_flags & ARC_L2CACHE) 26707237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26712688Smaybee mutex_exit(hash_lock); 26723403Sbmc ARCSTAT_BUMP(arcstat_hits); 26733403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 26743403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 26753403Sbmc data, metadata, hits); 26763403Sbmc 2677789Sahrens if (done) 2678789Sahrens done(NULL, buf, private); 2679789Sahrens } else { 2680789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2681789Sahrens arc_callback_t *acb; 26826987Sbrendan vdev_t *vd = NULL; 26839215SGeorge.Wilson@Sun.COM uint64_t addr; 26848582SBrendan.Gregg@Sun.COM boolean_t devw = B_FALSE; 2685789Sahrens 2686789Sahrens if (hdr == NULL) { 2687789Sahrens /* this block is not in the cache */ 2688789Sahrens arc_buf_hdr_t *exists; 26893290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 26903290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2691789Sahrens hdr = buf->b_hdr; 2692789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 2693*10922SJeff.Bonwick@Sun.COM hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 2694789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2695789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2696789Sahrens if (exists) { 2697789Sahrens /* somebody beat us to the hash insert */ 2698789Sahrens mutex_exit(hash_lock); 2699789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2700789Sahrens hdr->b_birth = 0; 2701789Sahrens hdr->b_cksum0 = 0; 27021544Seschrock (void) arc_buf_remove_ref(buf, private); 2703789Sahrens goto top; /* restart the IO request */ 2704789Sahrens } 27052391Smaybee /* if this is a prefetch, we don't have a reference */ 27062391Smaybee if (*arc_flags & ARC_PREFETCH) { 27072391Smaybee (void) remove_reference(hdr, hash_lock, 27082391Smaybee private); 27092391Smaybee hdr->b_flags |= ARC_PREFETCH; 27102391Smaybee } 27117237Sek110237 if (*arc_flags & ARC_L2CACHE) 27127237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27132391Smaybee if (BP_GET_LEVEL(bp) > 0) 27142391Smaybee hdr->b_flags |= ARC_INDIRECT; 2715789Sahrens } else { 2716789Sahrens /* this block is in the ghost cache */ 27171544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 27181544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 27192391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 27202391Smaybee ASSERT(hdr->b_buf == NULL); 2721789Sahrens 27222391Smaybee /* if this is a prefetch, we don't have a reference */ 27232391Smaybee if (*arc_flags & ARC_PREFETCH) 27242391Smaybee hdr->b_flags |= ARC_PREFETCH; 27252391Smaybee else 27262391Smaybee add_reference(hdr, hash_lock, private); 27277237Sek110237 if (*arc_flags & ARC_L2CACHE) 27287237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27296245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 27301544Seschrock buf->b_hdr = hdr; 27312688Smaybee buf->b_data = NULL; 27321544Seschrock buf->b_efunc = NULL; 27331544Seschrock buf->b_private = NULL; 27341544Seschrock buf->b_next = NULL; 27351544Seschrock hdr->b_buf = buf; 27362688Smaybee arc_get_data_buf(buf); 27371544Seschrock ASSERT(hdr->b_datacnt == 0); 27381544Seschrock hdr->b_datacnt = 1; 2739789Sahrens } 2740789Sahrens 2741789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2742789Sahrens acb->acb_done = done; 2743789Sahrens acb->acb_private = private; 2744789Sahrens 2745789Sahrens ASSERT(hdr->b_acb == NULL); 2746789Sahrens hdr->b_acb = acb; 2747789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2748789Sahrens 2749789Sahrens /* 2750789Sahrens * If the buffer has been evicted, migrate it to a present state 2751789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2752789Sahrens * the header will be marked as I/O in progress and have an 2753789Sahrens * attached buffer. At this point, anybody who finds this 2754789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2755789Sahrens */ 2756789Sahrens 27571544Seschrock if (GHOST_STATE(hdr->b_state)) 27582688Smaybee arc_access(hdr, hash_lock); 2759789Sahrens 27607754SJeff.Bonwick@Sun.COM if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 27617754SJeff.Bonwick@Sun.COM (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 27628582SBrendan.Gregg@Sun.COM devw = hdr->b_l2hdr->b_dev->l2ad_writing; 27636987Sbrendan addr = hdr->b_l2hdr->b_daddr; 27647754SJeff.Bonwick@Sun.COM /* 27657754SJeff.Bonwick@Sun.COM * Lock out device removal. 27667754SJeff.Bonwick@Sun.COM */ 27677754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || 27687754SJeff.Bonwick@Sun.COM !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 27697754SJeff.Bonwick@Sun.COM vd = NULL; 27706987Sbrendan } 27716987Sbrendan 27726987Sbrendan mutex_exit(hash_lock); 27736987Sbrendan 2774789Sahrens ASSERT3U(hdr->b_size, ==, size); 277510409SBrendan.Gregg@Sun.COM DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 277610409SBrendan.Gregg@Sun.COM uint64_t, size, zbookmark_t *, zb); 27773403Sbmc ARCSTAT_BUMP(arcstat_misses); 27783403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 27793403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 27803403Sbmc data, metadata, misses); 27811544Seschrock 27828582SBrendan.Gregg@Sun.COM if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 27836987Sbrendan /* 27846987Sbrendan * Read from the L2ARC if the following are true: 27856987Sbrendan * 1. The L2ARC vdev was previously cached. 27866987Sbrendan * 2. This buffer still has L2ARC metadata. 27876987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 27886987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 27896987Sbrendan * also have invalidated the vdev. 27908582SBrendan.Gregg@Sun.COM * 5. This isn't prefetch and l2arc_noprefetch is set. 27916987Sbrendan */ 27927754SJeff.Bonwick@Sun.COM if (hdr->b_l2hdr != NULL && 27938582SBrendan.Gregg@Sun.COM !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 27948582SBrendan.Gregg@Sun.COM !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 27955450Sbrendan l2arc_read_callback_t *cb; 27965450Sbrendan 27976643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 27986643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 27996643Seschrock 28005450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 28015450Sbrendan KM_SLEEP); 28025450Sbrendan cb->l2rcb_buf = buf; 28035450Sbrendan cb->l2rcb_spa = spa; 28045450Sbrendan cb->l2rcb_bp = *bp; 28055450Sbrendan cb->l2rcb_zb = *zb; 28067237Sek110237 cb->l2rcb_flags = zio_flags; 28075450Sbrendan 28085450Sbrendan /* 28097754SJeff.Bonwick@Sun.COM * l2arc read. The SCL_L2ARC lock will be 28107754SJeff.Bonwick@Sun.COM * released by l2arc_read_done(). 28115450Sbrendan */ 28125450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 28135450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 28147237Sek110237 l2arc_read_done, cb, priority, zio_flags | 28157361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 28167754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_PROPAGATE | 28177754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_RETRY, B_FALSE); 28185450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 28195450Sbrendan zio_t *, rzio); 28208582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_read_bytes, size); 28216987Sbrendan 28226987Sbrendan if (*arc_flags & ARC_NOWAIT) { 28236987Sbrendan zio_nowait(rzio); 28246987Sbrendan return (0); 28256987Sbrendan } 28266987Sbrendan 28276987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 28286987Sbrendan if (zio_wait(rzio) == 0) 28296987Sbrendan return (0); 28306987Sbrendan 28316987Sbrendan /* l2arc read error; goto zio_read() */ 28325450Sbrendan } else { 28335450Sbrendan DTRACE_PROBE1(l2arc__miss, 28345450Sbrendan arc_buf_hdr_t *, hdr); 28355450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 28365450Sbrendan if (HDR_L2_WRITING(hdr)) 28375450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 28387754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 28395450Sbrendan } 28408582SBrendan.Gregg@Sun.COM } else { 28418628SBill.Moore@Sun.COM if (vd != NULL) 28428628SBill.Moore@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 28438582SBrendan.Gregg@Sun.COM if (l2arc_ndev != 0) { 28448582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(l2arc__miss, 28458582SBrendan.Gregg@Sun.COM arc_buf_hdr_t *, hdr); 28468582SBrendan.Gregg@Sun.COM ARCSTAT_BUMP(arcstat_l2_misses); 28478582SBrendan.Gregg@Sun.COM } 28485450Sbrendan } 28496643Seschrock 2850789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 28517237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2852789Sahrens 28532391Smaybee if (*arc_flags & ARC_WAIT) 2854789Sahrens return (zio_wait(rzio)); 2855789Sahrens 28562391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2857789Sahrens zio_nowait(rzio); 2858789Sahrens } 2859789Sahrens return (0); 2860789Sahrens } 2861789Sahrens 28621544Seschrock void 28631544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 28641544Seschrock { 28651544Seschrock ASSERT(buf->b_hdr != NULL); 28663403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 28671544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 2868*10922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 2869*10922SJeff.Bonwick@Sun.COM ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 2870*10922SJeff.Bonwick@Sun.COM 28711544Seschrock buf->b_efunc = func; 28721544Seschrock buf->b_private = private; 28731544Seschrock } 28741544Seschrock 28751544Seschrock /* 28761544Seschrock * This is used by the DMU to let the ARC know that a buffer is 28771544Seschrock * being evicted, so the ARC should clean up. If this arc buf 28781544Seschrock * is not yet in the evicted state, it will be put there. 28791544Seschrock */ 28801544Seschrock int 28811544Seschrock arc_buf_evict(arc_buf_t *buf) 28821544Seschrock { 28832887Smaybee arc_buf_hdr_t *hdr; 28841544Seschrock kmutex_t *hash_lock; 28851544Seschrock arc_buf_t **bufp; 28861544Seschrock 28877545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 28882887Smaybee hdr = buf->b_hdr; 28891544Seschrock if (hdr == NULL) { 28901544Seschrock /* 28911544Seschrock * We are in arc_do_user_evicts(). 28921544Seschrock */ 28931544Seschrock ASSERT(buf->b_data == NULL); 28947545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 28951544Seschrock return (0); 28967545SMark.Maybee@Sun.COM } else if (buf->b_data == NULL) { 28977545SMark.Maybee@Sun.COM arc_buf_t copy = *buf; /* structure assignment */ 28987545SMark.Maybee@Sun.COM /* 28997545SMark.Maybee@Sun.COM * We are on the eviction list; process this buffer now 29007545SMark.Maybee@Sun.COM * but let arc_do_user_evicts() do the reaping. 29017545SMark.Maybee@Sun.COM */ 29027545SMark.Maybee@Sun.COM buf->b_efunc = NULL; 29037545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29047545SMark.Maybee@Sun.COM VERIFY(copy.b_efunc(©) == 0); 29057545SMark.Maybee@Sun.COM return (1); 29061544Seschrock } 29072887Smaybee hash_lock = HDR_LOCK(hdr); 29081544Seschrock mutex_enter(hash_lock); 29091544Seschrock 29102724Smaybee ASSERT(buf->b_hdr == hdr); 29112724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 29123403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 29131544Seschrock 29141544Seschrock /* 29151544Seschrock * Pull this buffer off of the hdr 29161544Seschrock */ 29171544Seschrock bufp = &hdr->b_buf; 29181544Seschrock while (*bufp != buf) 29191544Seschrock bufp = &(*bufp)->b_next; 29201544Seschrock *bufp = buf->b_next; 29211544Seschrock 29221544Seschrock ASSERT(buf->b_data != NULL); 29232688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 29241544Seschrock 29251544Seschrock if (hdr->b_datacnt == 0) { 29261544Seschrock arc_state_t *old_state = hdr->b_state; 29271544Seschrock arc_state_t *evicted_state; 29281544Seschrock 29291544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 29301544Seschrock 29311544Seschrock evicted_state = 29323403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 29331544Seschrock 29343403Sbmc mutex_enter(&old_state->arcs_mtx); 29353403Sbmc mutex_enter(&evicted_state->arcs_mtx); 29361544Seschrock 29371544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 29381544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 29395450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 29405450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 29411544Seschrock 29423403Sbmc mutex_exit(&evicted_state->arcs_mtx); 29433403Sbmc mutex_exit(&old_state->arcs_mtx); 29441544Seschrock } 29451544Seschrock mutex_exit(hash_lock); 29467545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29471819Smaybee 29481544Seschrock VERIFY(buf->b_efunc(buf) == 0); 29491544Seschrock buf->b_efunc = NULL; 29501544Seschrock buf->b_private = NULL; 29511544Seschrock buf->b_hdr = NULL; 29521544Seschrock kmem_cache_free(buf_cache, buf); 29531544Seschrock return (1); 29541544Seschrock } 29551544Seschrock 2956789Sahrens /* 2957789Sahrens * Release this buffer from the cache. This must be done 2958789Sahrens * after a read and prior to modifying the buffer contents. 2959789Sahrens * If the buffer has more than one reference, we must make 29607046Sahrens * a new hdr for the buffer. 2961789Sahrens */ 2962789Sahrens void 2963789Sahrens arc_release(arc_buf_t *buf, void *tag) 2964789Sahrens { 29657545SMark.Maybee@Sun.COM arc_buf_hdr_t *hdr; 29667545SMark.Maybee@Sun.COM kmutex_t *hash_lock; 29677545SMark.Maybee@Sun.COM l2arc_buf_hdr_t *l2hdr; 29685450Sbrendan uint64_t buf_size; 29699274SBrendan.Gregg@Sun.COM boolean_t released = B_FALSE; 2970789Sahrens 29717545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 29727545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 29737545SMark.Maybee@Sun.COM 2974789Sahrens /* this buffer is not on any list */ 2975789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 2976789Sahrens 29773403Sbmc if (hdr->b_state == arc_anon) { 2978789Sahrens /* this buffer is already released */ 2979789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2980789Sahrens ASSERT(BUF_EMPTY(hdr)); 29811544Seschrock ASSERT(buf->b_efunc == NULL); 29823093Sahrens arc_buf_thaw(buf); 29837545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29849274SBrendan.Gregg@Sun.COM released = B_TRUE; 29859274SBrendan.Gregg@Sun.COM } else { 29869274SBrendan.Gregg@Sun.COM hash_lock = HDR_LOCK(hdr); 29879274SBrendan.Gregg@Sun.COM mutex_enter(hash_lock); 2988789Sahrens } 2989789Sahrens 29907545SMark.Maybee@Sun.COM l2hdr = hdr->b_l2hdr; 29917545SMark.Maybee@Sun.COM if (l2hdr) { 29927545SMark.Maybee@Sun.COM mutex_enter(&l2arc_buflist_mtx); 29937545SMark.Maybee@Sun.COM hdr->b_l2hdr = NULL; 29947545SMark.Maybee@Sun.COM buf_size = hdr->b_size; 29957545SMark.Maybee@Sun.COM } 29967545SMark.Maybee@Sun.COM 29979274SBrendan.Gregg@Sun.COM if (released) 29989274SBrendan.Gregg@Sun.COM goto out; 29999274SBrendan.Gregg@Sun.COM 30001544Seschrock /* 30011544Seschrock * Do we have more than one buf? 30021544Seschrock */ 30037545SMark.Maybee@Sun.COM if (hdr->b_datacnt > 1) { 3004789Sahrens arc_buf_hdr_t *nhdr; 3005789Sahrens arc_buf_t **bufp; 3006789Sahrens uint64_t blksz = hdr->b_size; 30078636SMark.Maybee@Sun.COM uint64_t spa = hdr->b_spa; 30083290Sjohansen arc_buf_contents_t type = hdr->b_type; 30095450Sbrendan uint32_t flags = hdr->b_flags; 3010789Sahrens 30117545SMark.Maybee@Sun.COM ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3012789Sahrens /* 3013789Sahrens * Pull the data off of this buf and attach it to 3014789Sahrens * a new anonymous buf. 3015789Sahrens */ 30161544Seschrock (void) remove_reference(hdr, hash_lock, tag); 3017789Sahrens bufp = &hdr->b_buf; 30181544Seschrock while (*bufp != buf) 3019789Sahrens bufp = &(*bufp)->b_next; 3020789Sahrens *bufp = (*bufp)->b_next; 30213897Smaybee buf->b_next = NULL; 30221544Seschrock 30233403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 30243403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 30251544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 30264309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 30274309Smaybee ASSERT3U(*size, >=, hdr->b_size); 30284309Smaybee atomic_add_64(size, -hdr->b_size); 30291544Seschrock } 30301544Seschrock hdr->b_datacnt -= 1; 30313547Smaybee arc_cksum_verify(buf); 30321544Seschrock 3033789Sahrens mutex_exit(hash_lock); 3034789Sahrens 30356245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3036789Sahrens nhdr->b_size = blksz; 3037789Sahrens nhdr->b_spa = spa; 30383290Sjohansen nhdr->b_type = type; 3039789Sahrens nhdr->b_buf = buf; 30403403Sbmc nhdr->b_state = arc_anon; 3041789Sahrens nhdr->b_arc_access = 0; 30425450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 30435450Sbrendan nhdr->b_l2hdr = NULL; 30441544Seschrock nhdr->b_datacnt = 1; 30453547Smaybee nhdr->b_freeze_cksum = NULL; 30463897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 3047789Sahrens buf->b_hdr = nhdr; 30487545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30493403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 3050789Sahrens } else { 30517545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30521544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3053789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 3054789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 30553403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 3056789Sahrens hdr->b_arc_access = 0; 3057789Sahrens mutex_exit(hash_lock); 30585450Sbrendan 3059789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 3060789Sahrens hdr->b_birth = 0; 3061789Sahrens hdr->b_cksum0 = 0; 30623547Smaybee arc_buf_thaw(buf); 3063789Sahrens } 30641544Seschrock buf->b_efunc = NULL; 30651544Seschrock buf->b_private = NULL; 30665450Sbrendan 30679274SBrendan.Gregg@Sun.COM out: 30685450Sbrendan if (l2hdr) { 30695450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 30705450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 30715450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 30727545SMark.Maybee@Sun.COM mutex_exit(&l2arc_buflist_mtx); 30735450Sbrendan } 3074789Sahrens } 3075789Sahrens 3076789Sahrens int 3077789Sahrens arc_released(arc_buf_t *buf) 3078789Sahrens { 30797545SMark.Maybee@Sun.COM int released; 30807545SMark.Maybee@Sun.COM 30817545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30827545SMark.Maybee@Sun.COM released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 30837545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30847545SMark.Maybee@Sun.COM return (released); 30851544Seschrock } 30861544Seschrock 30871544Seschrock int 30881544Seschrock arc_has_callback(arc_buf_t *buf) 30891544Seschrock { 30907545SMark.Maybee@Sun.COM int callback; 30917545SMark.Maybee@Sun.COM 30927545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30937545SMark.Maybee@Sun.COM callback = (buf->b_efunc != NULL); 30947545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30957545SMark.Maybee@Sun.COM return (callback); 3096789Sahrens } 3097789Sahrens 30981544Seschrock #ifdef ZFS_DEBUG 30991544Seschrock int 31001544Seschrock arc_referenced(arc_buf_t *buf) 31011544Seschrock { 31027545SMark.Maybee@Sun.COM int referenced; 31037545SMark.Maybee@Sun.COM 31047545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 31057545SMark.Maybee@Sun.COM referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 31067545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 31077545SMark.Maybee@Sun.COM return (referenced); 31081544Seschrock } 31091544Seschrock #endif 31101544Seschrock 3111789Sahrens static void 31123547Smaybee arc_write_ready(zio_t *zio) 31133547Smaybee { 31143547Smaybee arc_write_callback_t *callback = zio->io_private; 31153547Smaybee arc_buf_t *buf = callback->awcb_buf; 31165329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 31175329Sgw25295 31187754SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 31197754SJeff.Bonwick@Sun.COM callback->awcb_ready(zio, buf, callback->awcb_private); 31207754SJeff.Bonwick@Sun.COM 31215329Sgw25295 /* 31225329Sgw25295 * If the IO is already in progress, then this is a re-write 31237754SJeff.Bonwick@Sun.COM * attempt, so we need to thaw and re-compute the cksum. 31247754SJeff.Bonwick@Sun.COM * It is the responsibility of the callback to handle the 31257754SJeff.Bonwick@Sun.COM * accounting for any re-write attempt. 31265329Sgw25295 */ 31275329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 31285329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 31295329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 31305329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 31315329Sgw25295 hdr->b_freeze_cksum = NULL; 31325329Sgw25295 } 31335329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 31345329Sgw25295 } 31355450Sbrendan arc_cksum_compute(buf, B_FALSE); 31365329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 31373547Smaybee } 31383547Smaybee 31393547Smaybee static void 3140789Sahrens arc_write_done(zio_t *zio) 3141789Sahrens { 31423547Smaybee arc_write_callback_t *callback = zio->io_private; 31433547Smaybee arc_buf_t *buf = callback->awcb_buf; 31443547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3145789Sahrens 3146*10922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_acb == NULL); 3147*10922SJeff.Bonwick@Sun.COM 3148*10922SJeff.Bonwick@Sun.COM if (zio->io_error == 0) { 3149*10922SJeff.Bonwick@Sun.COM hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3150*10922SJeff.Bonwick@Sun.COM hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 3151*10922SJeff.Bonwick@Sun.COM hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 3152*10922SJeff.Bonwick@Sun.COM } else { 3153*10922SJeff.Bonwick@Sun.COM ASSERT(BUF_EMPTY(hdr)); 3154*10922SJeff.Bonwick@Sun.COM } 3155*10922SJeff.Bonwick@Sun.COM 31561544Seschrock /* 31571544Seschrock * If the block to be written was all-zero, we may have 31581544Seschrock * compressed it away. In this case no write was performed 31591544Seschrock * so there will be no dva/birth-date/checksum. The buffer 31601544Seschrock * must therefor remain anonymous (and uncached). 31611544Seschrock */ 3162789Sahrens if (!BUF_EMPTY(hdr)) { 3163789Sahrens arc_buf_hdr_t *exists; 3164789Sahrens kmutex_t *hash_lock; 3165789Sahrens 3166*10922SJeff.Bonwick@Sun.COM ASSERT(zio->io_error == 0); 3167*10922SJeff.Bonwick@Sun.COM 31683093Sahrens arc_cksum_verify(buf); 31693093Sahrens 3170789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3171789Sahrens if (exists) { 3172789Sahrens /* 3173789Sahrens * This can only happen if we overwrite for 3174789Sahrens * sync-to-convergence, because we remove 3175789Sahrens * buffers from the hash table when we arc_free(). 3176789Sahrens */ 3177*10922SJeff.Bonwick@Sun.COM if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 3178*10922SJeff.Bonwick@Sun.COM if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3179*10922SJeff.Bonwick@Sun.COM panic("bad overwrite, hdr=%p exists=%p", 3180*10922SJeff.Bonwick@Sun.COM (void *)hdr, (void *)exists); 3181*10922SJeff.Bonwick@Sun.COM ASSERT(refcount_is_zero(&exists->b_refcnt)); 3182*10922SJeff.Bonwick@Sun.COM arc_change_state(arc_anon, exists, hash_lock); 3183*10922SJeff.Bonwick@Sun.COM mutex_exit(hash_lock); 3184*10922SJeff.Bonwick@Sun.COM arc_hdr_destroy(exists); 3185*10922SJeff.Bonwick@Sun.COM exists = buf_hash_insert(hdr, &hash_lock); 3186*10922SJeff.Bonwick@Sun.COM ASSERT3P(exists, ==, NULL); 3187*10922SJeff.Bonwick@Sun.COM } else { 3188*10922SJeff.Bonwick@Sun.COM /* Dedup */ 3189*10922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 3190*10922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_state == arc_anon); 3191*10922SJeff.Bonwick@Sun.COM ASSERT(BP_GET_DEDUP(zio->io_bp)); 3192*10922SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 319310272SMatthew.Ahrens@Sun.COM } 3194789Sahrens } 31951544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 31967046Sahrens /* if it's not anon, we are doing a scrub */ 3197*10922SJeff.Bonwick@Sun.COM if (!exists && hdr->b_state == arc_anon) 31987046Sahrens arc_access(hdr, hash_lock); 31992688Smaybee mutex_exit(hash_lock); 32001544Seschrock } else { 32011544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3202789Sahrens } 3203*10922SJeff.Bonwick@Sun.COM 3204*10922SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 3205*10922SJeff.Bonwick@Sun.COM callback->awcb_done(zio, buf, callback->awcb_private); 3206789Sahrens 32073547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3208789Sahrens } 3209789Sahrens 32103547Smaybee zio_t * 3211*10922SJeff.Bonwick@Sun.COM arc_write(zio_t *pio, spa_t *spa, uint64_t txg, 3212*10922SJeff.Bonwick@Sun.COM blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp, 3213*10922SJeff.Bonwick@Sun.COM arc_done_func_t *ready, arc_done_func_t *done, void *private, 3214*10922SJeff.Bonwick@Sun.COM int priority, int zio_flags, const zbookmark_t *zb) 3215789Sahrens { 3216789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 32173547Smaybee arc_write_callback_t *callback; 32187754SJeff.Bonwick@Sun.COM zio_t *zio; 32197754SJeff.Bonwick@Sun.COM 32207754SJeff.Bonwick@Sun.COM ASSERT(ready != NULL); 3221*10922SJeff.Bonwick@Sun.COM ASSERT(done != NULL); 3222789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 32232237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 3224*10922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_acb == NULL); 32257237Sek110237 if (l2arc) 32267237Sek110237 hdr->b_flags |= ARC_L2CACHE; 32273547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 32283547Smaybee callback->awcb_ready = ready; 32293547Smaybee callback->awcb_done = done; 32303547Smaybee callback->awcb_private = private; 32313547Smaybee callback->awcb_buf = buf; 32327046Sahrens 3233*10922SJeff.Bonwick@Sun.COM zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 32347754SJeff.Bonwick@Sun.COM arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3235789Sahrens 32363547Smaybee return (zio); 3237789Sahrens } 3238789Sahrens 3239*10922SJeff.Bonwick@Sun.COM void 3240*10922SJeff.Bonwick@Sun.COM arc_free(spa_t *spa, const blkptr_t *bp) 3241789Sahrens { 3242789Sahrens arc_buf_hdr_t *ab; 3243789Sahrens kmutex_t *hash_lock; 32448636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 3245789Sahrens 3246789Sahrens /* 3247*10922SJeff.Bonwick@Sun.COM * If this buffer is in the cache, release it, so it can be re-used. 3248789Sahrens */ 3249*10922SJeff.Bonwick@Sun.COM ab = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 3250*10922SJeff.Bonwick@Sun.COM &hash_lock); 3251789Sahrens if (ab != NULL) { 32523403Sbmc if (ab->b_state != arc_anon) 32533403Sbmc arc_change_state(arc_anon, ab, hash_lock); 32542391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 32552391Smaybee /* 32562391Smaybee * This should only happen when we prefetch. 32572391Smaybee */ 32582391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 32592391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 32602391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 32612391Smaybee if (HDR_IN_HASH_TABLE(ab)) 32622391Smaybee buf_hash_remove(ab); 32632391Smaybee ab->b_arc_access = 0; 32642391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 32652391Smaybee ab->b_birth = 0; 32662391Smaybee ab->b_cksum0 = 0; 32672391Smaybee ab->b_buf->b_efunc = NULL; 32682391Smaybee ab->b_buf->b_private = NULL; 32692391Smaybee mutex_exit(hash_lock); 3270*10922SJeff.Bonwick@Sun.COM } else { 3271*10922SJeff.Bonwick@Sun.COM ASSERT(refcount_is_zero(&ab->b_refcnt)); 32725450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3273789Sahrens mutex_exit(hash_lock); 32741544Seschrock arc_hdr_destroy(ab); 32753403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3276789Sahrens } 3277789Sahrens } 3278789Sahrens } 3279789Sahrens 32806245Smaybee static int 32819412SAleksandr.Guzovskiy@Sun.COM arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg) 32826245Smaybee { 32836245Smaybee #ifdef _KERNEL 32846245Smaybee uint64_t available_memory = ptob(freemem); 32856245Smaybee static uint64_t page_load = 0; 32866245Smaybee static uint64_t last_txg = 0; 32876245Smaybee 32886245Smaybee #if defined(__i386) 32896245Smaybee available_memory = 32906245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 32916245Smaybee #endif 32926245Smaybee if (available_memory >= zfs_write_limit_max) 32936245Smaybee return (0); 32946245Smaybee 32956245Smaybee if (txg > last_txg) { 32966245Smaybee last_txg = txg; 32976245Smaybee page_load = 0; 32986245Smaybee } 32996245Smaybee /* 33006245Smaybee * If we are in pageout, we know that memory is already tight, 33016245Smaybee * the arc is already going to be evicting, so we just want to 33026245Smaybee * continue to let page writes occur as quickly as possible. 33036245Smaybee */ 33046245Smaybee if (curproc == proc_pageout) { 33056245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 33066245Smaybee return (ERESTART); 33076245Smaybee /* Note: reserve is inflated, so we deflate */ 33086245Smaybee page_load += reserve / 8; 33096245Smaybee return (0); 33106245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 33116245Smaybee /* memory is low, delay before restarting */ 33126245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33136245Smaybee return (EAGAIN); 33146245Smaybee } 33156245Smaybee page_load = 0; 33166245Smaybee 33176245Smaybee if (arc_size > arc_c_min) { 33186245Smaybee uint64_t evictable_memory = 33196245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 33206245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 33216245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 33226245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 33236245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 33246245Smaybee } 33256245Smaybee 33266245Smaybee if (inflight_data > available_memory / 4) { 33276245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33286245Smaybee return (ERESTART); 33296245Smaybee } 33306245Smaybee #endif 33316245Smaybee return (0); 33326245Smaybee } 33336245Smaybee 3334789Sahrens void 33356245Smaybee arc_tempreserve_clear(uint64_t reserve) 3336789Sahrens { 33376245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3338789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3339789Sahrens } 3340789Sahrens 3341789Sahrens int 33426245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3343789Sahrens { 33446245Smaybee int error; 33459412SAleksandr.Guzovskiy@Sun.COM uint64_t anon_size; 33466245Smaybee 3347789Sahrens #ifdef ZFS_DEBUG 3348789Sahrens /* 3349789Sahrens * Once in a while, fail for no reason. Everything should cope. 3350789Sahrens */ 3351789Sahrens if (spa_get_random(10000) == 0) { 3352789Sahrens dprintf("forcing random failure\n"); 3353789Sahrens return (ERESTART); 3354789Sahrens } 3355789Sahrens #endif 33566245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 33576245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 33586245Smaybee if (reserve > arc_c) 3359982Smaybee return (ENOMEM); 3360982Smaybee 3361789Sahrens /* 33629412SAleksandr.Guzovskiy@Sun.COM * Don't count loaned bufs as in flight dirty data to prevent long 33639412SAleksandr.Guzovskiy@Sun.COM * network delays from blocking transactions that are ready to be 33649412SAleksandr.Guzovskiy@Sun.COM * assigned to a txg. 33659412SAleksandr.Guzovskiy@Sun.COM */ 33669412SAleksandr.Guzovskiy@Sun.COM anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 33679412SAleksandr.Guzovskiy@Sun.COM 33689412SAleksandr.Guzovskiy@Sun.COM /* 33696245Smaybee * Writes will, almost always, require additional memory allocations 33706245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 33716245Smaybee * make sure that there is sufficient available memory for this. 33726245Smaybee */ 33739412SAleksandr.Guzovskiy@Sun.COM if (error = arc_memory_throttle(reserve, anon_size, txg)) 33746245Smaybee return (error); 33756245Smaybee 33766245Smaybee /* 3377982Smaybee * Throttle writes when the amount of dirty data in the cache 3378982Smaybee * gets too large. We try to keep the cache less than half full 3379982Smaybee * of dirty blocks so that our sync times don't grow too large. 3380982Smaybee * Note: if two requests come in concurrently, we might let them 3381982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3382789Sahrens */ 33839412SAleksandr.Guzovskiy@Sun.COM 33849412SAleksandr.Guzovskiy@Sun.COM if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 33859412SAleksandr.Guzovskiy@Sun.COM anon_size > arc_c / 4) { 33864309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 33874309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 33884309Smaybee arc_tempreserve>>10, 33894309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 33904309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 33916245Smaybee reserve>>10, arc_c>>10); 3392789Sahrens return (ERESTART); 3393789Sahrens } 33946245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3395789Sahrens return (0); 3396789Sahrens } 3397789Sahrens 3398789Sahrens void 3399789Sahrens arc_init(void) 3400789Sahrens { 3401789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3402789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3403789Sahrens 34042391Smaybee /* Convert seconds to clock ticks */ 34052638Sperrin arc_min_prefetch_lifespan = 1 * hz; 34062391Smaybee 3407789Sahrens /* Start out with 1/8 of all memory */ 34083403Sbmc arc_c = physmem * PAGESIZE / 8; 3409789Sahrens 3410789Sahrens #ifdef _KERNEL 3411789Sahrens /* 3412789Sahrens * On architectures where the physical memory can be larger 3413789Sahrens * than the addressable space (intel in 32-bit mode), we may 3414789Sahrens * need to limit the cache to 1/8 of VM size. 3415789Sahrens */ 34163403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3417789Sahrens #endif 3418789Sahrens 3419982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 34203403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3421982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 34223403Sbmc if (arc_c * 8 >= 1<<30) 34233403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3424789Sahrens else 34253403Sbmc arc_c_max = arc_c_min; 34263403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 34272885Sahrens 34282885Sahrens /* 34292885Sahrens * Allow the tunables to override our calculations if they are 34302885Sahrens * reasonable (ie. over 64MB) 34312885Sahrens */ 34322885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 34333403Sbmc arc_c_max = zfs_arc_max; 34343403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 34353403Sbmc arc_c_min = zfs_arc_min; 34362885Sahrens 34373403Sbmc arc_c = arc_c_max; 34383403Sbmc arc_p = (arc_c >> 1); 3439789Sahrens 34404309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 34414309Smaybee arc_meta_limit = arc_c_max / 4; 34424645Sek110237 34434645Sek110237 /* Allow the tunable to override if it is reasonable */ 34444645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 34454645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 34464645Sek110237 34474309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 34484309Smaybee arc_c_min = arc_meta_limit / 2; 34494309Smaybee 34508582SBrendan.Gregg@Sun.COM if (zfs_arc_grow_retry > 0) 34518582SBrendan.Gregg@Sun.COM arc_grow_retry = zfs_arc_grow_retry; 34528582SBrendan.Gregg@Sun.COM 34538582SBrendan.Gregg@Sun.COM if (zfs_arc_shrink_shift > 0) 34548582SBrendan.Gregg@Sun.COM arc_shrink_shift = zfs_arc_shrink_shift; 34558582SBrendan.Gregg@Sun.COM 34568582SBrendan.Gregg@Sun.COM if (zfs_arc_p_min_shift > 0) 34578582SBrendan.Gregg@Sun.COM arc_p_min_shift = zfs_arc_p_min_shift; 34588582SBrendan.Gregg@Sun.COM 3459789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3460789Sahrens if (kmem_debugging()) 34613403Sbmc arc_c = arc_c / 2; 34623403Sbmc if (arc_c < arc_c_min) 34633403Sbmc arc_c = arc_c_min; 3464789Sahrens 34653403Sbmc arc_anon = &ARC_anon; 34663403Sbmc arc_mru = &ARC_mru; 34673403Sbmc arc_mru_ghost = &ARC_mru_ghost; 34683403Sbmc arc_mfu = &ARC_mfu; 34693403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 34705450Sbrendan arc_l2c_only = &ARC_l2c_only; 34713403Sbmc arc_size = 0; 3472789Sahrens 34733403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34743403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34753403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34763403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34773403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34785450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34792688Smaybee 34804309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 34814309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34824309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 34834309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34844309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 34854309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34864309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 34874309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34884309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 34894309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34904309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 34914309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34924309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 34934309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34944309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 34954309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34965450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 34975450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34985450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 34995450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3500789Sahrens 3501789Sahrens buf_init(); 3502789Sahrens 3503789Sahrens arc_thread_exit = 0; 35041544Seschrock arc_eviction_list = NULL; 35051544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 35062887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3507789Sahrens 35083403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 35093403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 35103403Sbmc 35113403Sbmc if (arc_ksp != NULL) { 35123403Sbmc arc_ksp->ks_data = &arc_stats; 35133403Sbmc kstat_install(arc_ksp); 35143403Sbmc } 35153403Sbmc 3516789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3517789Sahrens TS_RUN, minclsyspri); 35183158Smaybee 35193158Smaybee arc_dead = FALSE; 35206987Sbrendan arc_warm = B_FALSE; 35216245Smaybee 35226245Smaybee if (zfs_write_limit_max == 0) 35237468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 35246245Smaybee else 35256245Smaybee zfs_write_limit_shift = 0; 35267468SMark.Maybee@Sun.COM mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3527789Sahrens } 3528789Sahrens 3529789Sahrens void 3530789Sahrens arc_fini(void) 3531789Sahrens { 3532789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3533789Sahrens arc_thread_exit = 1; 3534789Sahrens while (arc_thread_exit != 0) 3535789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3536789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3537789Sahrens 35385642Smaybee arc_flush(NULL); 3539789Sahrens 3540789Sahrens arc_dead = TRUE; 3541789Sahrens 35423403Sbmc if (arc_ksp != NULL) { 35433403Sbmc kstat_delete(arc_ksp); 35443403Sbmc arc_ksp = NULL; 35453403Sbmc } 35463403Sbmc 35471544Seschrock mutex_destroy(&arc_eviction_mtx); 3548789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3549789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3550789Sahrens 35514309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 35524309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 35534309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 35544309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 35554309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 35564309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 35574309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 35584309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3559789Sahrens 35603403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 35613403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 35623403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 35633403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 35643403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 35658214SRicardo.M.Correia@Sun.COM mutex_destroy(&arc_l2c_only->arcs_mtx); 35662856Snd150628 35677468SMark.Maybee@Sun.COM mutex_destroy(&zfs_write_limit_lock); 35687468SMark.Maybee@Sun.COM 3569789Sahrens buf_fini(); 35709412SAleksandr.Guzovskiy@Sun.COM 35719412SAleksandr.Guzovskiy@Sun.COM ASSERT(arc_loaned_bytes == 0); 3572789Sahrens } 35735450Sbrendan 35745450Sbrendan /* 35755450Sbrendan * Level 2 ARC 35765450Sbrendan * 35775450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 35785450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 35795450Sbrendan * using large infrequent writes. The main role of this cache is to boost 35805450Sbrendan * the performance of random read workloads. The intended L2ARC devices 35815450Sbrendan * include short-stroked disks, solid state disks, and other media with 35825450Sbrendan * substantially faster read latency than disk. 35835450Sbrendan * 35845450Sbrendan * +-----------------------+ 35855450Sbrendan * | ARC | 35865450Sbrendan * +-----------------------+ 35875450Sbrendan * | ^ ^ 35885450Sbrendan * | | | 35895450Sbrendan * l2arc_feed_thread() arc_read() 35905450Sbrendan * | | | 35915450Sbrendan * | l2arc read | 35925450Sbrendan * V | | 35935450Sbrendan * +---------------+ | 35945450Sbrendan * | L2ARC | | 35955450Sbrendan * +---------------+ | 35965450Sbrendan * | ^ | 35975450Sbrendan * l2arc_write() | | 35985450Sbrendan * | | | 35995450Sbrendan * V | | 36005450Sbrendan * +-------+ +-------+ 36015450Sbrendan * | vdev | | vdev | 36025450Sbrendan * | cache | | cache | 36035450Sbrendan * +-------+ +-------+ 36045450Sbrendan * +=========+ .-----. 36055450Sbrendan * : L2ARC : |-_____-| 36065450Sbrendan * : devices : | Disks | 36075450Sbrendan * +=========+ `-_____-' 36085450Sbrendan * 36095450Sbrendan * Read requests are satisfied from the following sources, in order: 36105450Sbrendan * 36115450Sbrendan * 1) ARC 36125450Sbrendan * 2) vdev cache of L2ARC devices 36135450Sbrendan * 3) L2ARC devices 36145450Sbrendan * 4) vdev cache of disks 36155450Sbrendan * 5) disks 36165450Sbrendan * 36175450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 36185450Sbrendan * To accommodate for this there are some significant differences between 36195450Sbrendan * the L2ARC and traditional cache design: 36205450Sbrendan * 36215450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 36225450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 36235450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 36245450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 36255450Sbrendan * 36265450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 36275450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 36285450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 36295450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 36305450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 36315450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 36325450Sbrendan * provide a better sense of ratio than this diagram: 36335450Sbrendan * 36345450Sbrendan * head --> tail 36355450Sbrendan * +---------------------+----------+ 36365450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 36375450Sbrendan * +---------------------+----------+ | o L2ARC eligible 36385450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 36395450Sbrendan * +---------------------+----------+ | 36405450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 36415450Sbrendan * headroom | 36425450Sbrendan * l2arc_feed_thread() 36435450Sbrendan * | 36445450Sbrendan * l2arc write hand <--[oooo]--' 36455450Sbrendan * | 8 Mbyte 36465450Sbrendan * | write max 36475450Sbrendan * V 36485450Sbrendan * +==============================+ 36495450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 36505450Sbrendan * +==============================+ 36515450Sbrendan * 32 Gbytes 36525450Sbrendan * 36535450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 36545450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 36555450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 36565450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 36575450Sbrendan * the ARC lists have moved there due to inactivity. 36585450Sbrendan * 36595450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 36605450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 36615450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 36625450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 36635450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 36645450Sbrendan * quickly, such as during backups of the entire pool. 36655450Sbrendan * 36666987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 36676987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 36686987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 36696987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 36706987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 36716987Sbrendan * 36726987Sbrendan * The L2ARC device write speed is also boosted during this time so that 36736987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 36746987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 36756987Sbrendan * through increased writes. 36766987Sbrendan * 36776987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 36785450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 36795450Sbrendan * device is written to in a rotor fashion, sweeping writes through 36805450Sbrendan * available space then repeating. 36815450Sbrendan * 36826987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 36835450Sbrendan * write buffers back to disk based storage. 36845450Sbrendan * 36856987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 36865450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 36875450Sbrendan * 36885450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 36895450Sbrendan * may be necessary for different workloads: 36905450Sbrendan * 36915450Sbrendan * l2arc_write_max max write bytes per interval 36926987Sbrendan * l2arc_write_boost extra write bytes during device warmup 36935450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 36945450Sbrendan * l2arc_headroom number of max device writes to precache 36955450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 36965450Sbrendan * 36975450Sbrendan * Tunables may be removed or added as future performance improvements are 36985450Sbrendan * integrated, and also may become zpool properties. 36998582SBrendan.Gregg@Sun.COM * 37008582SBrendan.Gregg@Sun.COM * There are three key functions that control how the L2ARC warms up: 37018582SBrendan.Gregg@Sun.COM * 37028582SBrendan.Gregg@Sun.COM * l2arc_write_eligible() check if a buffer is eligible to cache 37038582SBrendan.Gregg@Sun.COM * l2arc_write_size() calculate how much to write 37048582SBrendan.Gregg@Sun.COM * l2arc_write_interval() calculate sleep delay between writes 37058582SBrendan.Gregg@Sun.COM * 37068582SBrendan.Gregg@Sun.COM * These three functions determine what to write, how much, and how quickly 37078582SBrendan.Gregg@Sun.COM * to send writes. 37085450Sbrendan */ 37095450Sbrendan 37108582SBrendan.Gregg@Sun.COM static boolean_t 37118636SMark.Maybee@Sun.COM l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 37128582SBrendan.Gregg@Sun.COM { 37138582SBrendan.Gregg@Sun.COM /* 37148582SBrendan.Gregg@Sun.COM * A buffer is *not* eligible for the L2ARC if it: 37158582SBrendan.Gregg@Sun.COM * 1. belongs to a different spa. 371610357SBrendan.Gregg@Sun.COM * 2. is already cached on the L2ARC. 371710357SBrendan.Gregg@Sun.COM * 3. has an I/O in progress (it may be an incomplete read). 371810357SBrendan.Gregg@Sun.COM * 4. is flagged not eligible (zfs property). 37198582SBrendan.Gregg@Sun.COM */ 372010357SBrendan.Gregg@Sun.COM if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL || 37218582SBrendan.Gregg@Sun.COM HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) 37228582SBrendan.Gregg@Sun.COM return (B_FALSE); 37238582SBrendan.Gregg@Sun.COM 37248582SBrendan.Gregg@Sun.COM return (B_TRUE); 37258582SBrendan.Gregg@Sun.COM } 37268582SBrendan.Gregg@Sun.COM 37278582SBrendan.Gregg@Sun.COM static uint64_t 37288582SBrendan.Gregg@Sun.COM l2arc_write_size(l2arc_dev_t *dev) 37298582SBrendan.Gregg@Sun.COM { 37308582SBrendan.Gregg@Sun.COM uint64_t size; 37318582SBrendan.Gregg@Sun.COM 37328582SBrendan.Gregg@Sun.COM size = dev->l2ad_write; 37338582SBrendan.Gregg@Sun.COM 37348582SBrendan.Gregg@Sun.COM if (arc_warm == B_FALSE) 37358582SBrendan.Gregg@Sun.COM size += dev->l2ad_boost; 37368582SBrendan.Gregg@Sun.COM 37378582SBrendan.Gregg@Sun.COM return (size); 37388582SBrendan.Gregg@Sun.COM 37398582SBrendan.Gregg@Sun.COM } 37408582SBrendan.Gregg@Sun.COM 37418582SBrendan.Gregg@Sun.COM static clock_t 37428582SBrendan.Gregg@Sun.COM l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 37438582SBrendan.Gregg@Sun.COM { 37448582SBrendan.Gregg@Sun.COM clock_t interval, next; 37458582SBrendan.Gregg@Sun.COM 37468582SBrendan.Gregg@Sun.COM /* 37478582SBrendan.Gregg@Sun.COM * If the ARC lists are busy, increase our write rate; if the 37488582SBrendan.Gregg@Sun.COM * lists are stale, idle back. This is achieved by checking 37498582SBrendan.Gregg@Sun.COM * how much we previously wrote - if it was more than half of 37508582SBrendan.Gregg@Sun.COM * what we wanted, schedule the next write much sooner. 37518582SBrendan.Gregg@Sun.COM */ 37528582SBrendan.Gregg@Sun.COM if (l2arc_feed_again && wrote > (wanted / 2)) 37538582SBrendan.Gregg@Sun.COM interval = (hz * l2arc_feed_min_ms) / 1000; 37548582SBrendan.Gregg@Sun.COM else 37558582SBrendan.Gregg@Sun.COM interval = hz * l2arc_feed_secs; 37568582SBrendan.Gregg@Sun.COM 37578582SBrendan.Gregg@Sun.COM next = MAX(lbolt, MIN(lbolt + interval, began + interval)); 37588582SBrendan.Gregg@Sun.COM 37598582SBrendan.Gregg@Sun.COM return (next); 37608582SBrendan.Gregg@Sun.COM } 37618582SBrendan.Gregg@Sun.COM 37625450Sbrendan static void 37635450Sbrendan l2arc_hdr_stat_add(void) 37645450Sbrendan { 37656018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 37666018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 37675450Sbrendan } 37685450Sbrendan 37695450Sbrendan static void 37705450Sbrendan l2arc_hdr_stat_remove(void) 37715450Sbrendan { 37726018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 37736018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 37745450Sbrendan } 37755450Sbrendan 37765450Sbrendan /* 37775450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 37786987Sbrendan * If a device is returned, this also returns holding the spa config lock. 37795450Sbrendan */ 37805450Sbrendan static l2arc_dev_t * 37815450Sbrendan l2arc_dev_get_next(void) 37825450Sbrendan { 37836987Sbrendan l2arc_dev_t *first, *next = NULL; 37846987Sbrendan 37856987Sbrendan /* 37866987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 37876987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 37886987Sbrendan * both locks will be dropped and a spa config lock held instead. 37896987Sbrendan */ 37906987Sbrendan mutex_enter(&spa_namespace_lock); 37916987Sbrendan mutex_enter(&l2arc_dev_mtx); 37926643Seschrock 37936643Seschrock /* if there are no vdevs, there is nothing to do */ 37946643Seschrock if (l2arc_ndev == 0) 37956987Sbrendan goto out; 37966643Seschrock 37976643Seschrock first = NULL; 37986643Seschrock next = l2arc_dev_last; 37996643Seschrock do { 38006643Seschrock /* loop around the list looking for a non-faulted vdev */ 38016643Seschrock if (next == NULL) { 38025450Sbrendan next = list_head(l2arc_dev_list); 38036643Seschrock } else { 38046643Seschrock next = list_next(l2arc_dev_list, next); 38056643Seschrock if (next == NULL) 38066643Seschrock next = list_head(l2arc_dev_list); 38076643Seschrock } 38086643Seschrock 38096643Seschrock /* if we have come back to the start, bail out */ 38106643Seschrock if (first == NULL) 38116643Seschrock first = next; 38126643Seschrock else if (next == first) 38136643Seschrock break; 38146643Seschrock 38156643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 38166643Seschrock 38176643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 38186643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 38196987Sbrendan next = NULL; 38205450Sbrendan 38215450Sbrendan l2arc_dev_last = next; 38225450Sbrendan 38236987Sbrendan out: 38246987Sbrendan mutex_exit(&l2arc_dev_mtx); 38256987Sbrendan 38266987Sbrendan /* 38276987Sbrendan * Grab the config lock to prevent the 'next' device from being 38286987Sbrendan * removed while we are writing to it. 38296987Sbrendan */ 38306987Sbrendan if (next != NULL) 38317754SJeff.Bonwick@Sun.COM spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 38326987Sbrendan mutex_exit(&spa_namespace_lock); 38336987Sbrendan 38345450Sbrendan return (next); 38355450Sbrendan } 38365450Sbrendan 38375450Sbrendan /* 38386987Sbrendan * Free buffers that were tagged for destruction. 38396987Sbrendan */ 38406987Sbrendan static void 38416987Sbrendan l2arc_do_free_on_write() 38426987Sbrendan { 38436987Sbrendan list_t *buflist; 38446987Sbrendan l2arc_data_free_t *df, *df_prev; 38456987Sbrendan 38466987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 38476987Sbrendan buflist = l2arc_free_on_write; 38486987Sbrendan 38496987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 38506987Sbrendan df_prev = list_prev(buflist, df); 38516987Sbrendan ASSERT(df->l2df_data != NULL); 38526987Sbrendan ASSERT(df->l2df_func != NULL); 38536987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 38546987Sbrendan list_remove(buflist, df); 38556987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 38566987Sbrendan } 38576987Sbrendan 38586987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 38596987Sbrendan } 38606987Sbrendan 38616987Sbrendan /* 38625450Sbrendan * A write to a cache device has completed. Update all headers to allow 38635450Sbrendan * reads from these buffers to begin. 38645450Sbrendan */ 38655450Sbrendan static void 38665450Sbrendan l2arc_write_done(zio_t *zio) 38675450Sbrendan { 38685450Sbrendan l2arc_write_callback_t *cb; 38695450Sbrendan l2arc_dev_t *dev; 38705450Sbrendan list_t *buflist; 38715450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 38726987Sbrendan l2arc_buf_hdr_t *abl2; 38735450Sbrendan kmutex_t *hash_lock; 38745450Sbrendan 38755450Sbrendan cb = zio->io_private; 38765450Sbrendan ASSERT(cb != NULL); 38775450Sbrendan dev = cb->l2wcb_dev; 38785450Sbrendan ASSERT(dev != NULL); 38795450Sbrendan head = cb->l2wcb_head; 38805450Sbrendan ASSERT(head != NULL); 38815450Sbrendan buflist = dev->l2ad_buflist; 38825450Sbrendan ASSERT(buflist != NULL); 38835450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 38845450Sbrendan l2arc_write_callback_t *, cb); 38855450Sbrendan 38865450Sbrendan if (zio->io_error != 0) 38875450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 38885450Sbrendan 38895450Sbrendan mutex_enter(&l2arc_buflist_mtx); 38905450Sbrendan 38915450Sbrendan /* 38925450Sbrendan * All writes completed, or an error was hit. 38935450Sbrendan */ 38945450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 38955450Sbrendan ab_prev = list_prev(buflist, ab); 38965450Sbrendan 38975450Sbrendan hash_lock = HDR_LOCK(ab); 38985450Sbrendan if (!mutex_tryenter(hash_lock)) { 38995450Sbrendan /* 39005450Sbrendan * This buffer misses out. It may be in a stage 39015450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 39025450Sbrendan * left set, denying reads to this buffer. 39035450Sbrendan */ 39045450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 39055450Sbrendan continue; 39065450Sbrendan } 39075450Sbrendan 39085450Sbrendan if (zio->io_error != 0) { 39095450Sbrendan /* 39106987Sbrendan * Error - drop L2ARC entry. 39115450Sbrendan */ 39126987Sbrendan list_remove(buflist, ab); 39136987Sbrendan abl2 = ab->b_l2hdr; 39145450Sbrendan ab->b_l2hdr = NULL; 39156987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 39166987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 39175450Sbrendan } 39185450Sbrendan 39195450Sbrendan /* 39205450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 39215450Sbrendan */ 39225450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 39235450Sbrendan 39245450Sbrendan mutex_exit(hash_lock); 39255450Sbrendan } 39265450Sbrendan 39275450Sbrendan atomic_inc_64(&l2arc_writes_done); 39285450Sbrendan list_remove(buflist, head); 39295450Sbrendan kmem_cache_free(hdr_cache, head); 39305450Sbrendan mutex_exit(&l2arc_buflist_mtx); 39315450Sbrendan 39326987Sbrendan l2arc_do_free_on_write(); 39335450Sbrendan 39345450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 39355450Sbrendan } 39365450Sbrendan 39375450Sbrendan /* 39385450Sbrendan * A read to a cache device completed. Validate buffer contents before 39395450Sbrendan * handing over to the regular ARC routines. 39405450Sbrendan */ 39415450Sbrendan static void 39425450Sbrendan l2arc_read_done(zio_t *zio) 39435450Sbrendan { 39445450Sbrendan l2arc_read_callback_t *cb; 39455450Sbrendan arc_buf_hdr_t *hdr; 39465450Sbrendan arc_buf_t *buf; 39475450Sbrendan kmutex_t *hash_lock; 39486987Sbrendan int equal; 39495450Sbrendan 39507754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd != NULL); 39517754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 39527754SJeff.Bonwick@Sun.COM 39537754SJeff.Bonwick@Sun.COM spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 39547754SJeff.Bonwick@Sun.COM 39555450Sbrendan cb = zio->io_private; 39565450Sbrendan ASSERT(cb != NULL); 39575450Sbrendan buf = cb->l2rcb_buf; 39585450Sbrendan ASSERT(buf != NULL); 39595450Sbrendan hdr = buf->b_hdr; 39605450Sbrendan ASSERT(hdr != NULL); 39615450Sbrendan 39625450Sbrendan hash_lock = HDR_LOCK(hdr); 39635450Sbrendan mutex_enter(hash_lock); 39645450Sbrendan 39655450Sbrendan /* 39665450Sbrendan * Check this survived the L2ARC journey. 39675450Sbrendan */ 39685450Sbrendan equal = arc_cksum_equal(buf); 39695450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 39705450Sbrendan mutex_exit(hash_lock); 39715450Sbrendan zio->io_private = buf; 39727754SJeff.Bonwick@Sun.COM zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 39737754SJeff.Bonwick@Sun.COM zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 39745450Sbrendan arc_read_done(zio); 39755450Sbrendan } else { 39765450Sbrendan mutex_exit(hash_lock); 39775450Sbrendan /* 39785450Sbrendan * Buffer didn't survive caching. Increment stats and 39795450Sbrendan * reissue to the original storage device. 39805450Sbrendan */ 39816987Sbrendan if (zio->io_error != 0) { 39825450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 39836987Sbrendan } else { 39846987Sbrendan zio->io_error = EIO; 39856987Sbrendan } 39865450Sbrendan if (!equal) 39875450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 39885450Sbrendan 39897754SJeff.Bonwick@Sun.COM /* 39907754SJeff.Bonwick@Sun.COM * If there's no waiter, issue an async i/o to the primary 39917754SJeff.Bonwick@Sun.COM * storage now. If there *is* a waiter, the caller must 39927754SJeff.Bonwick@Sun.COM * issue the i/o in a context where it's OK to block. 39937754SJeff.Bonwick@Sun.COM */ 39948632SBill.Moore@Sun.COM if (zio->io_waiter == NULL) { 39958632SBill.Moore@Sun.COM zio_t *pio = zio_unique_parent(zio); 39968632SBill.Moore@Sun.COM 39978632SBill.Moore@Sun.COM ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 39988632SBill.Moore@Sun.COM 39998632SBill.Moore@Sun.COM zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 40007754SJeff.Bonwick@Sun.COM buf->b_data, zio->io_size, arc_read_done, buf, 40017754SJeff.Bonwick@Sun.COM zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 40028632SBill.Moore@Sun.COM } 40035450Sbrendan } 40045450Sbrendan 40055450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 40065450Sbrendan } 40075450Sbrendan 40085450Sbrendan /* 40095450Sbrendan * This is the list priority from which the L2ARC will search for pages to 40105450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 40115450Sbrendan * desired order. This order can have a significant effect on cache 40125450Sbrendan * performance. 40135450Sbrendan * 40145450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 40155450Sbrendan * the data lists. This function returns a locked list, and also returns 40165450Sbrendan * the lock pointer. 40175450Sbrendan */ 40185450Sbrendan static list_t * 40195450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 40205450Sbrendan { 40215450Sbrendan list_t *list; 40225450Sbrendan 40235450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 40245450Sbrendan 40255450Sbrendan switch (list_num) { 40265450Sbrendan case 0: 40275450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 40285450Sbrendan *lock = &arc_mfu->arcs_mtx; 40295450Sbrendan break; 40305450Sbrendan case 1: 40315450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 40325450Sbrendan *lock = &arc_mru->arcs_mtx; 40335450Sbrendan break; 40345450Sbrendan case 2: 40355450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 40365450Sbrendan *lock = &arc_mfu->arcs_mtx; 40375450Sbrendan break; 40385450Sbrendan case 3: 40395450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 40405450Sbrendan *lock = &arc_mru->arcs_mtx; 40415450Sbrendan break; 40425450Sbrendan } 40435450Sbrendan 40445450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 40455450Sbrendan mutex_enter(*lock); 40465450Sbrendan return (list); 40475450Sbrendan } 40485450Sbrendan 40495450Sbrendan /* 40505450Sbrendan * Evict buffers from the device write hand to the distance specified in 40515450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 40525450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 40535450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 40545450Sbrendan */ 40555450Sbrendan static void 40565450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 40575450Sbrendan { 40585450Sbrendan list_t *buflist; 40595450Sbrendan l2arc_buf_hdr_t *abl2; 40605450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 40615450Sbrendan kmutex_t *hash_lock; 40625450Sbrendan uint64_t taddr; 40635450Sbrendan 40645450Sbrendan buflist = dev->l2ad_buflist; 40655450Sbrendan 40665450Sbrendan if (buflist == NULL) 40675450Sbrendan return; 40685450Sbrendan 40695450Sbrendan if (!all && dev->l2ad_first) { 40705450Sbrendan /* 40715450Sbrendan * This is the first sweep through the device. There is 40725450Sbrendan * nothing to evict. 40735450Sbrendan */ 40745450Sbrendan return; 40755450Sbrendan } 40765450Sbrendan 40776987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 40785450Sbrendan /* 40795450Sbrendan * When nearing the end of the device, evict to the end 40805450Sbrendan * before the device write hand jumps to the start. 40815450Sbrendan */ 40825450Sbrendan taddr = dev->l2ad_end; 40835450Sbrendan } else { 40845450Sbrendan taddr = dev->l2ad_hand + distance; 40855450Sbrendan } 40865450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 40875450Sbrendan uint64_t, taddr, boolean_t, all); 40885450Sbrendan 40895450Sbrendan top: 40905450Sbrendan mutex_enter(&l2arc_buflist_mtx); 40915450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 40925450Sbrendan ab_prev = list_prev(buflist, ab); 40935450Sbrendan 40945450Sbrendan hash_lock = HDR_LOCK(ab); 40955450Sbrendan if (!mutex_tryenter(hash_lock)) { 40965450Sbrendan /* 40975450Sbrendan * Missed the hash lock. Retry. 40985450Sbrendan */ 40995450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 41005450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41015450Sbrendan mutex_enter(hash_lock); 41025450Sbrendan mutex_exit(hash_lock); 41035450Sbrendan goto top; 41045450Sbrendan } 41055450Sbrendan 41065450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 41075450Sbrendan /* 41085450Sbrendan * We hit a write head node. Leave it for 41095450Sbrendan * l2arc_write_done(). 41105450Sbrendan */ 41115450Sbrendan list_remove(buflist, ab); 41125450Sbrendan mutex_exit(hash_lock); 41135450Sbrendan continue; 41145450Sbrendan } 41155450Sbrendan 41165450Sbrendan if (!all && ab->b_l2hdr != NULL && 41175450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 41185450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 41195450Sbrendan /* 41205450Sbrendan * We've evicted to the target address, 41215450Sbrendan * or the end of the device. 41225450Sbrendan */ 41235450Sbrendan mutex_exit(hash_lock); 41245450Sbrendan break; 41255450Sbrendan } 41265450Sbrendan 41275450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 41285450Sbrendan /* 41295450Sbrendan * Already on the path to destruction. 41305450Sbrendan */ 41315450Sbrendan mutex_exit(hash_lock); 41325450Sbrendan continue; 41335450Sbrendan } 41345450Sbrendan 41355450Sbrendan if (ab->b_state == arc_l2c_only) { 41365450Sbrendan ASSERT(!HDR_L2_READING(ab)); 41375450Sbrendan /* 41385450Sbrendan * This doesn't exist in the ARC. Destroy. 41395450Sbrendan * arc_hdr_destroy() will call list_remove() 41405450Sbrendan * and decrement arcstat_l2_size. 41415450Sbrendan */ 41425450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 41435450Sbrendan arc_hdr_destroy(ab); 41445450Sbrendan } else { 41455450Sbrendan /* 41466987Sbrendan * Invalidate issued or about to be issued 41476987Sbrendan * reads, since we may be about to write 41486987Sbrendan * over this location. 41496987Sbrendan */ 41506987Sbrendan if (HDR_L2_READING(ab)) { 41516987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 41526987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 41536987Sbrendan } 41546987Sbrendan 41556987Sbrendan /* 41565450Sbrendan * Tell ARC this no longer exists in L2ARC. 41575450Sbrendan */ 41585450Sbrendan if (ab->b_l2hdr != NULL) { 41595450Sbrendan abl2 = ab->b_l2hdr; 41605450Sbrendan ab->b_l2hdr = NULL; 41615450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 41625450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 41635450Sbrendan } 41645450Sbrendan list_remove(buflist, ab); 41655450Sbrendan 41665450Sbrendan /* 41675450Sbrendan * This may have been leftover after a 41685450Sbrendan * failed write. 41695450Sbrendan */ 41705450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 41715450Sbrendan } 41725450Sbrendan mutex_exit(hash_lock); 41735450Sbrendan } 41745450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41755450Sbrendan 4176*10922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0); 41775450Sbrendan dev->l2ad_evict = taddr; 41785450Sbrendan } 41795450Sbrendan 41805450Sbrendan /* 41815450Sbrendan * Find and write ARC buffers to the L2ARC device. 41825450Sbrendan * 41835450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 41845450Sbrendan * for reading until they have completed writing. 41855450Sbrendan */ 41868582SBrendan.Gregg@Sun.COM static uint64_t 41876987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 41885450Sbrendan { 41895450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 41905450Sbrendan l2arc_buf_hdr_t *hdrl2; 41915450Sbrendan list_t *list; 41926987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 41935450Sbrendan void *buf_data; 41945450Sbrendan kmutex_t *hash_lock, *list_lock; 41955450Sbrendan boolean_t have_lock, full; 41965450Sbrendan l2arc_write_callback_t *cb; 41975450Sbrendan zio_t *pio, *wzio; 41988636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 41995450Sbrendan 42005450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 42015450Sbrendan 42025450Sbrendan pio = NULL; 42035450Sbrendan write_sz = 0; 42045450Sbrendan full = B_FALSE; 42056245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 42065450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 42075450Sbrendan 42085450Sbrendan /* 42095450Sbrendan * Copy buffers for L2ARC writing. 42105450Sbrendan */ 42115450Sbrendan mutex_enter(&l2arc_buflist_mtx); 42125450Sbrendan for (int try = 0; try <= 3; try++) { 42135450Sbrendan list = l2arc_list_locked(try, &list_lock); 42145450Sbrendan passed_sz = 0; 42155450Sbrendan 42166987Sbrendan /* 42176987Sbrendan * L2ARC fast warmup. 42186987Sbrendan * 42196987Sbrendan * Until the ARC is warm and starts to evict, read from the 42206987Sbrendan * head of the ARC lists rather than the tail. 42216987Sbrendan */ 42226987Sbrendan headroom = target_sz * l2arc_headroom; 42236987Sbrendan if (arc_warm == B_FALSE) 42246987Sbrendan ab = list_head(list); 42256987Sbrendan else 42266987Sbrendan ab = list_tail(list); 42276987Sbrendan 42286987Sbrendan for (; ab; ab = ab_prev) { 42296987Sbrendan if (arc_warm == B_FALSE) 42306987Sbrendan ab_prev = list_next(list, ab); 42316987Sbrendan else 42326987Sbrendan ab_prev = list_prev(list, ab); 42335450Sbrendan 42345450Sbrendan hash_lock = HDR_LOCK(ab); 42355450Sbrendan have_lock = MUTEX_HELD(hash_lock); 42365450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 42375450Sbrendan /* 42385450Sbrendan * Skip this buffer rather than waiting. 42395450Sbrendan */ 42405450Sbrendan continue; 42415450Sbrendan } 42425450Sbrendan 42435450Sbrendan passed_sz += ab->b_size; 42445450Sbrendan if (passed_sz > headroom) { 42455450Sbrendan /* 42465450Sbrendan * Searched too far. 42475450Sbrendan */ 42485450Sbrendan mutex_exit(hash_lock); 42495450Sbrendan break; 42505450Sbrendan } 42515450Sbrendan 42528636SMark.Maybee@Sun.COM if (!l2arc_write_eligible(guid, ab)) { 42535450Sbrendan mutex_exit(hash_lock); 42545450Sbrendan continue; 42555450Sbrendan } 42565450Sbrendan 42575450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 42585450Sbrendan full = B_TRUE; 42595450Sbrendan mutex_exit(hash_lock); 42605450Sbrendan break; 42615450Sbrendan } 42625450Sbrendan 42635450Sbrendan if (pio == NULL) { 42645450Sbrendan /* 42655450Sbrendan * Insert a dummy header on the buflist so 42665450Sbrendan * l2arc_write_done() can find where the 42675450Sbrendan * write buffers begin without searching. 42685450Sbrendan */ 42695450Sbrendan list_insert_head(dev->l2ad_buflist, head); 42705450Sbrendan 42715450Sbrendan cb = kmem_alloc( 42725450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 42735450Sbrendan cb->l2wcb_dev = dev; 42745450Sbrendan cb->l2wcb_head = head; 42755450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 42765450Sbrendan ZIO_FLAG_CANFAIL); 42775450Sbrendan } 42785450Sbrendan 42795450Sbrendan /* 42805450Sbrendan * Create and add a new L2ARC header. 42815450Sbrendan */ 42825450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 42835450Sbrendan hdrl2->b_dev = dev; 42845450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 42855450Sbrendan 42865450Sbrendan ab->b_flags |= ARC_L2_WRITING; 42875450Sbrendan ab->b_l2hdr = hdrl2; 42885450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 42895450Sbrendan buf_data = ab->b_buf->b_data; 42905450Sbrendan buf_sz = ab->b_size; 42915450Sbrendan 42925450Sbrendan /* 42935450Sbrendan * Compute and store the buffer cksum before 42945450Sbrendan * writing. On debug the cksum is verified first. 42955450Sbrendan */ 42965450Sbrendan arc_cksum_verify(ab->b_buf); 42975450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 42985450Sbrendan 42995450Sbrendan mutex_exit(hash_lock); 43005450Sbrendan 43015450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 43025450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 43035450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 43045450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 43055450Sbrendan 43065450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 43075450Sbrendan zio_t *, wzio); 43085450Sbrendan (void) zio_nowait(wzio); 43095450Sbrendan 43107754SJeff.Bonwick@Sun.COM /* 43117754SJeff.Bonwick@Sun.COM * Keep the clock hand suitably device-aligned. 43127754SJeff.Bonwick@Sun.COM */ 43137754SJeff.Bonwick@Sun.COM buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 43147754SJeff.Bonwick@Sun.COM 43155450Sbrendan write_sz += buf_sz; 43165450Sbrendan dev->l2ad_hand += buf_sz; 43175450Sbrendan } 43185450Sbrendan 43195450Sbrendan mutex_exit(list_lock); 43205450Sbrendan 43215450Sbrendan if (full == B_TRUE) 43225450Sbrendan break; 43235450Sbrendan } 43245450Sbrendan mutex_exit(&l2arc_buflist_mtx); 43255450Sbrendan 43265450Sbrendan if (pio == NULL) { 43275450Sbrendan ASSERT3U(write_sz, ==, 0); 43285450Sbrendan kmem_cache_free(hdr_cache, head); 43298582SBrendan.Gregg@Sun.COM return (0); 43305450Sbrendan } 43315450Sbrendan 43325450Sbrendan ASSERT3U(write_sz, <=, target_sz); 43335450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 43348582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 43355450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 4336*10922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0); 43375450Sbrendan 43385450Sbrendan /* 43395450Sbrendan * Bump device hand to the device start if it is approaching the end. 43405450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 43415450Sbrendan */ 43426987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 4343*10922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, 4344*10922SJeff.Bonwick@Sun.COM dev->l2ad_end - dev->l2ad_hand, 0, 0); 43455450Sbrendan dev->l2ad_hand = dev->l2ad_start; 43465450Sbrendan dev->l2ad_evict = dev->l2ad_start; 43475450Sbrendan dev->l2ad_first = B_FALSE; 43485450Sbrendan } 43495450Sbrendan 43508582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_TRUE; 43515450Sbrendan (void) zio_wait(pio); 43528582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_FALSE; 43538582SBrendan.Gregg@Sun.COM 43548582SBrendan.Gregg@Sun.COM return (write_sz); 43555450Sbrendan } 43565450Sbrendan 43575450Sbrendan /* 43585450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 43595450Sbrendan * heart of the L2ARC. 43605450Sbrendan */ 43615450Sbrendan static void 43625450Sbrendan l2arc_feed_thread(void) 43635450Sbrendan { 43645450Sbrendan callb_cpr_t cpr; 43655450Sbrendan l2arc_dev_t *dev; 43665450Sbrendan spa_t *spa; 43678582SBrendan.Gregg@Sun.COM uint64_t size, wrote; 43688582SBrendan.Gregg@Sun.COM clock_t begin, next = lbolt; 43695450Sbrendan 43705450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 43715450Sbrendan 43725450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 43735450Sbrendan 43745450Sbrendan while (l2arc_thread_exit == 0) { 43755450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 43766987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 43778582SBrendan.Gregg@Sun.COM next); 43786987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 43798582SBrendan.Gregg@Sun.COM next = lbolt + hz; 43806987Sbrendan 43816987Sbrendan /* 43826987Sbrendan * Quick check for L2ARC devices. 43836987Sbrendan */ 43846987Sbrendan mutex_enter(&l2arc_dev_mtx); 43856987Sbrendan if (l2arc_ndev == 0) { 43866987Sbrendan mutex_exit(&l2arc_dev_mtx); 43876987Sbrendan continue; 43885450Sbrendan } 43896987Sbrendan mutex_exit(&l2arc_dev_mtx); 43908582SBrendan.Gregg@Sun.COM begin = lbolt; 43916643Seschrock 43925450Sbrendan /* 43936643Seschrock * This selects the next l2arc device to write to, and in 43946643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 43956987Sbrendan * will return NULL if there are now no l2arc devices or if 43966987Sbrendan * they are all faulted. 43976987Sbrendan * 43986987Sbrendan * If a device is returned, its spa's config lock is also 43996987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 44006987Sbrendan * will grab and release l2arc_dev_mtx. 44015450Sbrendan */ 44026987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 44035450Sbrendan continue; 44046987Sbrendan 44056987Sbrendan spa = dev->l2ad_spa; 44066987Sbrendan ASSERT(spa != NULL); 44075450Sbrendan 44085450Sbrendan /* 44095450Sbrendan * Avoid contributing to memory pressure. 44105450Sbrendan */ 44115450Sbrendan if (arc_reclaim_needed()) { 44125450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 44137754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44145450Sbrendan continue; 44155450Sbrendan } 44165450Sbrendan 44175450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 44185450Sbrendan 44198582SBrendan.Gregg@Sun.COM size = l2arc_write_size(dev); 44206987Sbrendan 44215450Sbrendan /* 44225450Sbrendan * Evict L2ARC buffers that will be overwritten. 44235450Sbrendan */ 44246987Sbrendan l2arc_evict(dev, size, B_FALSE); 44255450Sbrendan 44265450Sbrendan /* 44275450Sbrendan * Write ARC buffers. 44285450Sbrendan */ 44298582SBrendan.Gregg@Sun.COM wrote = l2arc_write_buffers(spa, dev, size); 44308582SBrendan.Gregg@Sun.COM 44318582SBrendan.Gregg@Sun.COM /* 44328582SBrendan.Gregg@Sun.COM * Calculate interval between writes. 44338582SBrendan.Gregg@Sun.COM */ 44348582SBrendan.Gregg@Sun.COM next = l2arc_write_interval(begin, size, wrote); 44357754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44365450Sbrendan } 44375450Sbrendan 44385450Sbrendan l2arc_thread_exit = 0; 44395450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 44405450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 44415450Sbrendan thread_exit(); 44425450Sbrendan } 44435450Sbrendan 44446643Seschrock boolean_t 44456643Seschrock l2arc_vdev_present(vdev_t *vd) 44466643Seschrock { 44476643Seschrock l2arc_dev_t *dev; 44486643Seschrock 44496643Seschrock mutex_enter(&l2arc_dev_mtx); 44506643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 44516643Seschrock dev = list_next(l2arc_dev_list, dev)) { 44526643Seschrock if (dev->l2ad_vdev == vd) 44536643Seschrock break; 44546643Seschrock } 44556643Seschrock mutex_exit(&l2arc_dev_mtx); 44566643Seschrock 44576643Seschrock return (dev != NULL); 44586643Seschrock } 44596643Seschrock 44605450Sbrendan /* 44615450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 44625450Sbrendan * validated the vdev and opened it. 44635450Sbrendan */ 44645450Sbrendan void 44659816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa_t *spa, vdev_t *vd) 44665450Sbrendan { 44675450Sbrendan l2arc_dev_t *adddev; 44685450Sbrendan 44696643Seschrock ASSERT(!l2arc_vdev_present(vd)); 44706643Seschrock 44715450Sbrendan /* 44725450Sbrendan * Create a new l2arc device entry. 44735450Sbrendan */ 44745450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 44755450Sbrendan adddev->l2ad_spa = spa; 44765450Sbrendan adddev->l2ad_vdev = vd; 44775450Sbrendan adddev->l2ad_write = l2arc_write_max; 44786987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 44799816SGeorge.Wilson@Sun.COM adddev->l2ad_start = VDEV_LABEL_START_SIZE; 44809816SGeorge.Wilson@Sun.COM adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 44815450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 44825450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 44835450Sbrendan adddev->l2ad_first = B_TRUE; 44848582SBrendan.Gregg@Sun.COM adddev->l2ad_writing = B_FALSE; 44855450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 44865450Sbrendan 44875450Sbrendan /* 44885450Sbrendan * This is a list of all ARC buffers that are still valid on the 44895450Sbrendan * device. 44905450Sbrendan */ 44915450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 44925450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 44935450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 44945450Sbrendan 4495*10922SJeff.Bonwick@Sun.COM vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 44965450Sbrendan 44975450Sbrendan /* 44985450Sbrendan * Add device to global list 44995450Sbrendan */ 45005450Sbrendan mutex_enter(&l2arc_dev_mtx); 45015450Sbrendan list_insert_head(l2arc_dev_list, adddev); 45025450Sbrendan atomic_inc_64(&l2arc_ndev); 45035450Sbrendan mutex_exit(&l2arc_dev_mtx); 45045450Sbrendan } 45055450Sbrendan 45065450Sbrendan /* 45075450Sbrendan * Remove a vdev from the L2ARC. 45085450Sbrendan */ 45095450Sbrendan void 45105450Sbrendan l2arc_remove_vdev(vdev_t *vd) 45115450Sbrendan { 45125450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 45135450Sbrendan 45145450Sbrendan /* 45155450Sbrendan * Find the device by vdev 45165450Sbrendan */ 45175450Sbrendan mutex_enter(&l2arc_dev_mtx); 45185450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 45195450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 45205450Sbrendan if (vd == dev->l2ad_vdev) { 45215450Sbrendan remdev = dev; 45225450Sbrendan break; 45235450Sbrendan } 45245450Sbrendan } 45255450Sbrendan ASSERT(remdev != NULL); 45265450Sbrendan 45275450Sbrendan /* 45285450Sbrendan * Remove device from global list 45295450Sbrendan */ 45305450Sbrendan list_remove(l2arc_dev_list, remdev); 45315450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 45326987Sbrendan atomic_dec_64(&l2arc_ndev); 45336987Sbrendan mutex_exit(&l2arc_dev_mtx); 45345450Sbrendan 45355450Sbrendan /* 45365450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 45375450Sbrendan */ 45385450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 45395450Sbrendan list_destroy(remdev->l2ad_buflist); 45405450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 45415450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 45425450Sbrendan } 45435450Sbrendan 45445450Sbrendan void 45457754SJeff.Bonwick@Sun.COM l2arc_init(void) 45465450Sbrendan { 45475450Sbrendan l2arc_thread_exit = 0; 45485450Sbrendan l2arc_ndev = 0; 45495450Sbrendan l2arc_writes_sent = 0; 45505450Sbrendan l2arc_writes_done = 0; 45515450Sbrendan 45525450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 45535450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 45545450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 45555450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 45565450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 45575450Sbrendan 45585450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 45595450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 45605450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 45615450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 45625450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 45635450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 45645450Sbrendan } 45655450Sbrendan 45665450Sbrendan void 45677754SJeff.Bonwick@Sun.COM l2arc_fini(void) 45685450Sbrendan { 45696987Sbrendan /* 45706987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 45716987Sbrendan * Because of this, we can assume that all l2arc devices have 45726987Sbrendan * already been removed when the pools themselves were removed. 45736987Sbrendan */ 45746987Sbrendan 45756987Sbrendan l2arc_do_free_on_write(); 45766987Sbrendan 45775450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 45785450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 45795450Sbrendan mutex_destroy(&l2arc_dev_mtx); 45805450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 45815450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 45825450Sbrendan 45835450Sbrendan list_destroy(l2arc_dev_list); 45845450Sbrendan list_destroy(l2arc_free_on_write); 45855450Sbrendan } 45867754SJeff.Bonwick@Sun.COM 45877754SJeff.Bonwick@Sun.COM void 45887754SJeff.Bonwick@Sun.COM l2arc_start(void) 45897754SJeff.Bonwick@Sun.COM { 45908241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 45917754SJeff.Bonwick@Sun.COM return; 45927754SJeff.Bonwick@Sun.COM 45937754SJeff.Bonwick@Sun.COM (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 45947754SJeff.Bonwick@Sun.COM TS_RUN, minclsyspri); 45957754SJeff.Bonwick@Sun.COM } 45967754SJeff.Bonwick@Sun.COM 45977754SJeff.Bonwick@Sun.COM void 45987754SJeff.Bonwick@Sun.COM l2arc_stop(void) 45997754SJeff.Bonwick@Sun.COM { 46008241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46017754SJeff.Bonwick@Sun.COM return; 46027754SJeff.Bonwick@Sun.COM 46037754SJeff.Bonwick@Sun.COM mutex_enter(&l2arc_feed_thr_lock); 46047754SJeff.Bonwick@Sun.COM cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 46057754SJeff.Bonwick@Sun.COM l2arc_thread_exit = 1; 46067754SJeff.Bonwick@Sun.COM while (l2arc_thread_exit != 0) 46077754SJeff.Bonwick@Sun.COM cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 46087754SJeff.Bonwick@Sun.COM mutex_exit(&l2arc_feed_thr_lock); 46097754SJeff.Bonwick@Sun.COM } 4610