1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51484Sek110237 * Common Development and Distribution License (the "License"). 61484Sek110237 * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 22*11539SChunli.Zhang@Sun.COM * Copyright 2010 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> 13510922SJeff.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 34910922SJeff.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; 38310922SJeff.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 81610922SJeff.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)); 104010922SJeff.Bonwick@Sun.COM ASSERT(ab->b_datacnt <= 1 || new_state != arc_anon); 104110922SJeff.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(buf->b_data != NULL); 1245*11539SChunli.Zhang@Sun.COM (void) refcount_add(&hdr->b_refcnt, tag); 1246*11539SChunli.Zhang@Sun.COM (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag); 12479412SAleksandr.Guzovskiy@Sun.COM 12489412SAleksandr.Guzovskiy@Sun.COM atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 12499412SAleksandr.Guzovskiy@Sun.COM } 12509412SAleksandr.Guzovskiy@Sun.COM 1251*11539SChunli.Zhang@Sun.COM /* Detach an arc_buf from a dbuf (tag) */ 1252*11539SChunli.Zhang@Sun.COM void 1253*11539SChunli.Zhang@Sun.COM arc_loan_inuse_buf(arc_buf_t *buf, void *tag) 1254*11539SChunli.Zhang@Sun.COM { 1255*11539SChunli.Zhang@Sun.COM arc_buf_hdr_t *hdr; 1256*11539SChunli.Zhang@Sun.COM 1257*11539SChunli.Zhang@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 1258*11539SChunli.Zhang@Sun.COM ASSERT(buf->b_data != NULL); 1259*11539SChunli.Zhang@Sun.COM hdr = buf->b_hdr; 1260*11539SChunli.Zhang@Sun.COM (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag); 1261*11539SChunli.Zhang@Sun.COM (void) refcount_remove(&hdr->b_refcnt, tag); 1262*11539SChunli.Zhang@Sun.COM buf->b_efunc = NULL; 1263*11539SChunli.Zhang@Sun.COM buf->b_private = NULL; 1264*11539SChunli.Zhang@Sun.COM 1265*11539SChunli.Zhang@Sun.COM atomic_add_64(&arc_loaned_bytes, hdr->b_size); 1266*11539SChunli.Zhang@Sun.COM rw_exit(&buf->b_lock); 1267*11539SChunli.Zhang@Sun.COM } 1268*11539SChunli.Zhang@Sun.COM 12692688Smaybee static arc_buf_t * 12702688Smaybee arc_buf_clone(arc_buf_t *from) 12711544Seschrock { 12722688Smaybee arc_buf_t *buf; 12732688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 12742688Smaybee uint64_t size = hdr->b_size; 12751544Seschrock 127610922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_state != arc_anon); 127710922SJeff.Bonwick@Sun.COM 12786245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 12792688Smaybee buf->b_hdr = hdr; 12802688Smaybee buf->b_data = NULL; 12812688Smaybee buf->b_efunc = NULL; 12822688Smaybee buf->b_private = NULL; 12832688Smaybee buf->b_next = hdr->b_buf; 12842688Smaybee hdr->b_buf = buf; 12852688Smaybee arc_get_data_buf(buf); 12862688Smaybee bcopy(from->b_data, buf->b_data, size); 12872688Smaybee hdr->b_datacnt += 1; 12882688Smaybee return (buf); 12891544Seschrock } 12901544Seschrock 12911544Seschrock void 12921544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 12931544Seschrock { 12942887Smaybee arc_buf_hdr_t *hdr; 12951544Seschrock kmutex_t *hash_lock; 12961544Seschrock 12972724Smaybee /* 12987545SMark.Maybee@Sun.COM * Check to see if this buffer is evicted. Callers 12997545SMark.Maybee@Sun.COM * must verify b_data != NULL to know if the add_ref 13007545SMark.Maybee@Sun.COM * was successful. 13012724Smaybee */ 13027545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 13037545SMark.Maybee@Sun.COM if (buf->b_data == NULL) { 13047545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 13052724Smaybee return; 13062887Smaybee } 13077545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 13087545SMark.Maybee@Sun.COM ASSERT(hdr != NULL); 13092887Smaybee hash_lock = HDR_LOCK(hdr); 13102724Smaybee mutex_enter(hash_lock); 13117545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 13127545SMark.Maybee@Sun.COM 13133403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 13141544Seschrock add_reference(hdr, hash_lock, tag); 13158582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 13162688Smaybee arc_access(hdr, hash_lock); 13172688Smaybee mutex_exit(hash_lock); 13183403Sbmc ARCSTAT_BUMP(arcstat_hits); 13193403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 13203403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 13213403Sbmc data, metadata, hits); 13221544Seschrock } 13231544Seschrock 13245450Sbrendan /* 13255450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 13265450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 13275450Sbrendan */ 13285450Sbrendan static void 13295450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 13305450Sbrendan void *data, size_t size) 13315450Sbrendan { 13325450Sbrendan if (HDR_L2_WRITING(hdr)) { 13335450Sbrendan l2arc_data_free_t *df; 13345450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 13355450Sbrendan df->l2df_data = data; 13365450Sbrendan df->l2df_size = size; 13375450Sbrendan df->l2df_func = free_func; 13385450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 13395450Sbrendan list_insert_head(l2arc_free_on_write, df); 13405450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 13415450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 13425450Sbrendan } else { 13435450Sbrendan free_func(data, size); 13445450Sbrendan } 13455450Sbrendan } 13465450Sbrendan 1347789Sahrens static void 13482688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 13491544Seschrock { 13501544Seschrock arc_buf_t **bufp; 13511544Seschrock 13521544Seschrock /* free up data associated with the buf */ 13531544Seschrock if (buf->b_data) { 13541544Seschrock arc_state_t *state = buf->b_hdr->b_state; 13551544Seschrock uint64_t size = buf->b_hdr->b_size; 13563290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 13571544Seschrock 13583093Sahrens arc_cksum_verify(buf); 135910922SJeff.Bonwick@Sun.COM 13602688Smaybee if (!recycle) { 13613290Sjohansen if (type == ARC_BUFC_METADATA) { 13625450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 13635450Sbrendan buf->b_data, size); 13648582SBrendan.Gregg@Sun.COM arc_space_return(size, ARC_SPACE_DATA); 13653290Sjohansen } else { 13663290Sjohansen ASSERT(type == ARC_BUFC_DATA); 13675450Sbrendan arc_buf_data_free(buf->b_hdr, 13685450Sbrendan zio_data_buf_free, buf->b_data, size); 13698582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -size); 13704309Smaybee atomic_add_64(&arc_size, -size); 13713290Sjohansen } 13722688Smaybee } 13731544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 13744309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 13754309Smaybee 13761544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 13773403Sbmc ASSERT(state != arc_anon); 13784309Smaybee 13794309Smaybee ASSERT3U(*cnt, >=, size); 13804309Smaybee atomic_add_64(cnt, -size); 13811544Seschrock } 13823403Sbmc ASSERT3U(state->arcs_size, >=, size); 13833403Sbmc atomic_add_64(&state->arcs_size, -size); 13841544Seschrock buf->b_data = NULL; 13851544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 13861544Seschrock buf->b_hdr->b_datacnt -= 1; 13871544Seschrock } 13881544Seschrock 13891544Seschrock /* only remove the buf if requested */ 13901544Seschrock if (!all) 13911544Seschrock return; 13921544Seschrock 13931544Seschrock /* remove the buf from the hdr list */ 13941544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 13951544Seschrock continue; 13961544Seschrock *bufp = buf->b_next; 13971544Seschrock 13981544Seschrock ASSERT(buf->b_efunc == NULL); 13991544Seschrock 14001544Seschrock /* clean up the buf */ 14011544Seschrock buf->b_hdr = NULL; 14021544Seschrock kmem_cache_free(buf_cache, buf); 14031544Seschrock } 14041544Seschrock 14051544Seschrock static void 14061544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1407789Sahrens { 1408789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 14093403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 14101544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 141110922SJeff.Bonwick@Sun.COM l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 141210922SJeff.Bonwick@Sun.COM 141310922SJeff.Bonwick@Sun.COM if (l2hdr != NULL) { 141410922SJeff.Bonwick@Sun.COM boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx); 141510922SJeff.Bonwick@Sun.COM /* 141610922SJeff.Bonwick@Sun.COM * To prevent arc_free() and l2arc_evict() from 141710922SJeff.Bonwick@Sun.COM * attempting to free the same buffer at the same time, 141810922SJeff.Bonwick@Sun.COM * a FREE_IN_PROGRESS flag is given to arc_free() to 141910922SJeff.Bonwick@Sun.COM * give it priority. l2arc_evict() can't destroy this 142010922SJeff.Bonwick@Sun.COM * header while we are waiting on l2arc_buflist_mtx. 142110922SJeff.Bonwick@Sun.COM * 142210922SJeff.Bonwick@Sun.COM * The hdr may be removed from l2ad_buflist before we 142310922SJeff.Bonwick@Sun.COM * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 142410922SJeff.Bonwick@Sun.COM */ 142510922SJeff.Bonwick@Sun.COM if (!buflist_held) { 14265450Sbrendan mutex_enter(&l2arc_buflist_mtx); 142710922SJeff.Bonwick@Sun.COM l2hdr = hdr->b_l2hdr; 142810922SJeff.Bonwick@Sun.COM } 142910922SJeff.Bonwick@Sun.COM 143010922SJeff.Bonwick@Sun.COM if (l2hdr != NULL) { 143110922SJeff.Bonwick@Sun.COM list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 143210922SJeff.Bonwick@Sun.COM ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 143310922SJeff.Bonwick@Sun.COM kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 143410922SJeff.Bonwick@Sun.COM if (hdr->b_state == arc_l2c_only) 143510922SJeff.Bonwick@Sun.COM l2arc_hdr_stat_remove(); 143610922SJeff.Bonwick@Sun.COM hdr->b_l2hdr = NULL; 143710922SJeff.Bonwick@Sun.COM } 143810922SJeff.Bonwick@Sun.COM 143910922SJeff.Bonwick@Sun.COM if (!buflist_held) 14405450Sbrendan mutex_exit(&l2arc_buflist_mtx); 14415450Sbrendan } 14425450Sbrendan 1443789Sahrens if (!BUF_EMPTY(hdr)) { 14441544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1445789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1446789Sahrens hdr->b_birth = 0; 1447789Sahrens hdr->b_cksum0 = 0; 1448789Sahrens } 14491544Seschrock while (hdr->b_buf) { 1450789Sahrens arc_buf_t *buf = hdr->b_buf; 1451789Sahrens 14521544Seschrock if (buf->b_efunc) { 14531544Seschrock mutex_enter(&arc_eviction_mtx); 14547545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 14551544Seschrock ASSERT(buf->b_hdr != NULL); 14562688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 14571544Seschrock hdr->b_buf = buf->b_next; 14582887Smaybee buf->b_hdr = &arc_eviction_hdr; 14591544Seschrock buf->b_next = arc_eviction_list; 14601544Seschrock arc_eviction_list = buf; 14617545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 14621544Seschrock mutex_exit(&arc_eviction_mtx); 14631544Seschrock } else { 14642688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 14651544Seschrock } 1466789Sahrens } 14673093Sahrens if (hdr->b_freeze_cksum != NULL) { 14683093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 14693093Sahrens hdr->b_freeze_cksum = NULL; 14703093Sahrens } 14711544Seschrock 1472789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1473789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1474789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1475789Sahrens kmem_cache_free(hdr_cache, hdr); 1476789Sahrens } 1477789Sahrens 1478789Sahrens void 1479789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1480789Sahrens { 1481789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 14823403Sbmc int hashed = hdr->b_state != arc_anon; 14831544Seschrock 14841544Seschrock ASSERT(buf->b_efunc == NULL); 14851544Seschrock ASSERT(buf->b_data != NULL); 14861544Seschrock 14871544Seschrock if (hashed) { 14881544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 14891544Seschrock 14901544Seschrock mutex_enter(hash_lock); 14911544Seschrock (void) remove_reference(hdr, hash_lock, tag); 149210922SJeff.Bonwick@Sun.COM if (hdr->b_datacnt > 1) { 14932688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 149410922SJeff.Bonwick@Sun.COM } else { 149510922SJeff.Bonwick@Sun.COM ASSERT(buf == hdr->b_buf); 149610922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 14971544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 149810922SJeff.Bonwick@Sun.COM } 14991544Seschrock mutex_exit(hash_lock); 15001544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 15011544Seschrock int destroy_hdr; 15021544Seschrock /* 15031544Seschrock * We are in the middle of an async write. Don't destroy 15041544Seschrock * this buffer unless the write completes before we finish 15051544Seschrock * decrementing the reference count. 15061544Seschrock */ 15071544Seschrock mutex_enter(&arc_eviction_mtx); 15081544Seschrock (void) remove_reference(hdr, NULL, tag); 15091544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 15101544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 15111544Seschrock mutex_exit(&arc_eviction_mtx); 15121544Seschrock if (destroy_hdr) 15131544Seschrock arc_hdr_destroy(hdr); 15141544Seschrock } else { 15151544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 15161544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 15172688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 15181544Seschrock } else { 15191544Seschrock arc_hdr_destroy(hdr); 15201544Seschrock } 15211544Seschrock } 15221544Seschrock } 15231544Seschrock 15241544Seschrock int 15251544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 15261544Seschrock { 15271544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1528789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 15291544Seschrock int no_callback = (buf->b_efunc == NULL); 15301544Seschrock 15313403Sbmc if (hdr->b_state == arc_anon) { 153210922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 15331544Seschrock arc_buf_free(buf, tag); 15341544Seschrock return (no_callback); 15351544Seschrock } 1536789Sahrens 1537789Sahrens mutex_enter(hash_lock); 15383403Sbmc ASSERT(hdr->b_state != arc_anon); 15391544Seschrock ASSERT(buf->b_data != NULL); 1540789Sahrens 15411544Seschrock (void) remove_reference(hdr, hash_lock, tag); 15421544Seschrock if (hdr->b_datacnt > 1) { 15431544Seschrock if (no_callback) 15442688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 15451544Seschrock } else if (no_callback) { 15461544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 154710922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 15481544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1549789Sahrens } 15501544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 15511544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1552789Sahrens mutex_exit(hash_lock); 15531544Seschrock return (no_callback); 1554789Sahrens } 1555789Sahrens 1556789Sahrens int 1557789Sahrens arc_buf_size(arc_buf_t *buf) 1558789Sahrens { 1559789Sahrens return (buf->b_hdr->b_size); 1560789Sahrens } 1561789Sahrens 1562789Sahrens /* 1563789Sahrens * Evict buffers from list until we've removed the specified number of 1564789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 15652688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 15662688Smaybee * - look for a buffer to evict that is `bytes' long. 15672688Smaybee * - return the data block from this buffer rather than freeing it. 15682688Smaybee * This flag is used by callers that are trying to make space for a 15692688Smaybee * new buffer in a full arc cache. 15705642Smaybee * 15715642Smaybee * This function makes a "best effort". It skips over any buffers 15725642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 15735642Smaybee * It may also return without evicting as much space as requested. 1574789Sahrens */ 15752688Smaybee static void * 15768636SMark.Maybee@Sun.COM arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 15773290Sjohansen arc_buf_contents_t type) 1578789Sahrens { 1579789Sahrens arc_state_t *evicted_state; 15802688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 15812918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 15824309Smaybee list_t *list = &state->arcs_list[type]; 1583789Sahrens kmutex_t *hash_lock; 15842688Smaybee boolean_t have_lock; 15852918Smaybee void *stolen = NULL; 1586789Sahrens 15873403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1588789Sahrens 15893403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1590789Sahrens 15913403Sbmc mutex_enter(&state->arcs_mtx); 15923403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1593789Sahrens 15944309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15954309Smaybee ab_prev = list_prev(list, ab); 15962391Smaybee /* prefetch buffers have a minimum lifespan */ 15972688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 15985642Smaybee (spa && ab->b_spa != spa) || 15992688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 160011066Srafael.vanoni@sun.com ddi_get_lbolt() - ab->b_arc_access < 160111066Srafael.vanoni@sun.com arc_min_prefetch_lifespan)) { 16022391Smaybee skipped++; 16032391Smaybee continue; 16042391Smaybee } 16052918Smaybee /* "lookahead" for better eviction candidate */ 16062918Smaybee if (recycle && ab->b_size != bytes && 16072918Smaybee ab_prev && ab_prev->b_size == bytes) 16082688Smaybee continue; 1609789Sahrens hash_lock = HDR_LOCK(ab); 16102688Smaybee have_lock = MUTEX_HELD(hash_lock); 16112688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1612789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 16131544Seschrock ASSERT(ab->b_datacnt > 0); 16141544Seschrock while (ab->b_buf) { 16151544Seschrock arc_buf_t *buf = ab->b_buf; 16167545SMark.Maybee@Sun.COM if (!rw_tryenter(&buf->b_lock, RW_WRITER)) { 16177545SMark.Maybee@Sun.COM missed += 1; 16187545SMark.Maybee@Sun.COM break; 16197545SMark.Maybee@Sun.COM } 16202688Smaybee if (buf->b_data) { 16211544Seschrock bytes_evicted += ab->b_size; 16223290Sjohansen if (recycle && ab->b_type == type && 16235450Sbrendan ab->b_size == bytes && 16245450Sbrendan !HDR_L2_WRITING(ab)) { 16252918Smaybee stolen = buf->b_data; 16262918Smaybee recycle = FALSE; 16272918Smaybee } 16282688Smaybee } 16291544Seschrock if (buf->b_efunc) { 16301544Seschrock mutex_enter(&arc_eviction_mtx); 16312918Smaybee arc_buf_destroy(buf, 16322918Smaybee buf->b_data == stolen, FALSE); 16331544Seschrock ab->b_buf = buf->b_next; 16342887Smaybee buf->b_hdr = &arc_eviction_hdr; 16351544Seschrock buf->b_next = arc_eviction_list; 16361544Seschrock arc_eviction_list = buf; 16371544Seschrock mutex_exit(&arc_eviction_mtx); 16387545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 16391544Seschrock } else { 16407545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 16412918Smaybee arc_buf_destroy(buf, 16422918Smaybee buf->b_data == stolen, TRUE); 16431544Seschrock } 16441544Seschrock } 164510357SBrendan.Gregg@Sun.COM 164610357SBrendan.Gregg@Sun.COM if (ab->b_l2hdr) { 164710357SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_evict_l2_cached, 164810357SBrendan.Gregg@Sun.COM ab->b_size); 164910357SBrendan.Gregg@Sun.COM } else { 165010357SBrendan.Gregg@Sun.COM if (l2arc_write_eligible(ab->b_spa, ab)) { 165110357SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_evict_l2_eligible, 165210357SBrendan.Gregg@Sun.COM ab->b_size); 165310357SBrendan.Gregg@Sun.COM } else { 165410357SBrendan.Gregg@Sun.COM ARCSTAT_INCR( 165510357SBrendan.Gregg@Sun.COM arcstat_evict_l2_ineligible, 165610357SBrendan.Gregg@Sun.COM ab->b_size); 165710357SBrendan.Gregg@Sun.COM } 165810357SBrendan.Gregg@Sun.COM } 165910357SBrendan.Gregg@Sun.COM 16607545SMark.Maybee@Sun.COM if (ab->b_datacnt == 0) { 16617545SMark.Maybee@Sun.COM arc_change_state(evicted_state, ab, hash_lock); 16627545SMark.Maybee@Sun.COM ASSERT(HDR_IN_HASH_TABLE(ab)); 16637545SMark.Maybee@Sun.COM ab->b_flags |= ARC_IN_HASH_TABLE; 16647545SMark.Maybee@Sun.COM ab->b_flags &= ~ARC_BUF_AVAILABLE; 16657545SMark.Maybee@Sun.COM DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 16667545SMark.Maybee@Sun.COM } 16672688Smaybee if (!have_lock) 16682688Smaybee mutex_exit(hash_lock); 16691544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1670789Sahrens break; 1671789Sahrens } else { 16722688Smaybee missed += 1; 1673789Sahrens } 1674789Sahrens } 16753403Sbmc 16763403Sbmc mutex_exit(&evicted_state->arcs_mtx); 16773403Sbmc mutex_exit(&state->arcs_mtx); 1678789Sahrens 1679789Sahrens if (bytes_evicted < bytes) 1680789Sahrens dprintf("only evicted %lld bytes from %x", 1681789Sahrens (longlong_t)bytes_evicted, state); 1682789Sahrens 16832688Smaybee if (skipped) 16843403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 16853403Sbmc 16862688Smaybee if (missed) 16873403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 16883403Sbmc 16894709Smaybee /* 16904709Smaybee * We have just evicted some date into the ghost state, make 16914709Smaybee * sure we also adjust the ghost state size if necessary. 16924709Smaybee */ 16934709Smaybee if (arc_no_grow && 16944709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 16954709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 16964709Smaybee arc_mru_ghost->arcs_size - arc_c; 16974709Smaybee 16984709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 16994709Smaybee int64_t todelete = 17004709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 17015642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 17024709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 17034709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 17044709Smaybee arc_mru_ghost->arcs_size + 17054709Smaybee arc_mfu_ghost->arcs_size - arc_c); 17065642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 17074709Smaybee } 17084709Smaybee } 17094709Smaybee 17102918Smaybee return (stolen); 1711789Sahrens } 1712789Sahrens 1713789Sahrens /* 1714789Sahrens * Remove buffers from list until we've removed the specified number of 1715789Sahrens * bytes. Destroy the buffers that are removed. 1716789Sahrens */ 1717789Sahrens static void 17188636SMark.Maybee@Sun.COM arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 1719789Sahrens { 1720789Sahrens arc_buf_hdr_t *ab, *ab_prev; 17214309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1722789Sahrens kmutex_t *hash_lock; 17231544Seschrock uint64_t bytes_deleted = 0; 17243700Sek110237 uint64_t bufs_skipped = 0; 1725789Sahrens 17261544Seschrock ASSERT(GHOST_STATE(state)); 1727789Sahrens top: 17283403Sbmc mutex_enter(&state->arcs_mtx); 17294309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 17304309Smaybee ab_prev = list_prev(list, ab); 17315642Smaybee if (spa && ab->b_spa != spa) 17325642Smaybee continue; 1733789Sahrens hash_lock = HDR_LOCK(ab); 1734789Sahrens if (mutex_tryenter(hash_lock)) { 17352391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 17361544Seschrock ASSERT(ab->b_buf == NULL); 17373403Sbmc ARCSTAT_BUMP(arcstat_deleted); 17381544Seschrock bytes_deleted += ab->b_size; 17395450Sbrendan 17405450Sbrendan if (ab->b_l2hdr != NULL) { 17415450Sbrendan /* 17425450Sbrendan * This buffer is cached on the 2nd Level ARC; 17435450Sbrendan * don't destroy the header. 17445450Sbrendan */ 17455450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 17465450Sbrendan mutex_exit(hash_lock); 17475450Sbrendan } else { 17485450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 17495450Sbrendan mutex_exit(hash_lock); 17505450Sbrendan arc_hdr_destroy(ab); 17515450Sbrendan } 17525450Sbrendan 1753789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1754789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1755789Sahrens break; 1756789Sahrens } else { 1757789Sahrens if (bytes < 0) { 17583403Sbmc mutex_exit(&state->arcs_mtx); 1759789Sahrens mutex_enter(hash_lock); 1760789Sahrens mutex_exit(hash_lock); 1761789Sahrens goto top; 1762789Sahrens } 1763789Sahrens bufs_skipped += 1; 1764789Sahrens } 1765789Sahrens } 17663403Sbmc mutex_exit(&state->arcs_mtx); 1767789Sahrens 17684309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 17694309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 17704309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 17714309Smaybee goto top; 17724309Smaybee } 17734309Smaybee 1774789Sahrens if (bufs_skipped) { 17753403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1776789Sahrens ASSERT(bytes >= 0); 1777789Sahrens } 1778789Sahrens 1779789Sahrens if (bytes_deleted < bytes) 1780789Sahrens dprintf("only deleted %lld bytes from %p", 1781789Sahrens (longlong_t)bytes_deleted, state); 1782789Sahrens } 1783789Sahrens 1784789Sahrens static void 1785789Sahrens arc_adjust(void) 1786789Sahrens { 17878582SBrendan.Gregg@Sun.COM int64_t adjustment, delta; 17888582SBrendan.Gregg@Sun.COM 17898582SBrendan.Gregg@Sun.COM /* 17908582SBrendan.Gregg@Sun.COM * Adjust MRU size 17918582SBrendan.Gregg@Sun.COM */ 17928582SBrendan.Gregg@Sun.COM 17938582SBrendan.Gregg@Sun.COM adjustment = MIN(arc_size - arc_c, 17948582SBrendan.Gregg@Sun.COM arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - arc_p); 17958582SBrendan.Gregg@Sun.COM 17968582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 17978582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 17988582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA); 17998582SBrendan.Gregg@Sun.COM adjustment -= delta; 18004309Smaybee } 18014309Smaybee 18028582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 18038582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 18048582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, 18055642Smaybee ARC_BUFC_METADATA); 1806789Sahrens } 1807789Sahrens 18088582SBrendan.Gregg@Sun.COM /* 18098582SBrendan.Gregg@Sun.COM * Adjust MFU size 18108582SBrendan.Gregg@Sun.COM */ 18118582SBrendan.Gregg@Sun.COM 18128582SBrendan.Gregg@Sun.COM adjustment = arc_size - arc_c; 18138582SBrendan.Gregg@Sun.COM 18148582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 18158582SBrendan.Gregg@Sun.COM delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 18168582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA); 18178582SBrendan.Gregg@Sun.COM adjustment -= delta; 18188582SBrendan.Gregg@Sun.COM } 18198582SBrendan.Gregg@Sun.COM 18208582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 18218582SBrendan.Gregg@Sun.COM int64_t delta = MIN(adjustment, 18228582SBrendan.Gregg@Sun.COM arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 18238582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, 18248582SBrendan.Gregg@Sun.COM ARC_BUFC_METADATA); 18258582SBrendan.Gregg@Sun.COM } 18268582SBrendan.Gregg@Sun.COM 18278582SBrendan.Gregg@Sun.COM /* 18288582SBrendan.Gregg@Sun.COM * Adjust ghost lists 18298582SBrendan.Gregg@Sun.COM */ 18308582SBrendan.Gregg@Sun.COM 18318582SBrendan.Gregg@Sun.COM adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 18328582SBrendan.Gregg@Sun.COM 18338582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 18348582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru_ghost->arcs_size, adjustment); 18358582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mru_ghost, NULL, delta); 18368582SBrendan.Gregg@Sun.COM } 18378582SBrendan.Gregg@Sun.COM 18388582SBrendan.Gregg@Sun.COM adjustment = 18398582SBrendan.Gregg@Sun.COM arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 18408582SBrendan.Gregg@Sun.COM 18418582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 18428582SBrendan.Gregg@Sun.COM delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 18438582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mfu_ghost, NULL, delta); 1844789Sahrens } 1845789Sahrens } 1846789Sahrens 18471544Seschrock static void 18481544Seschrock arc_do_user_evicts(void) 18491544Seschrock { 18501544Seschrock mutex_enter(&arc_eviction_mtx); 18511544Seschrock while (arc_eviction_list != NULL) { 18521544Seschrock arc_buf_t *buf = arc_eviction_list; 18531544Seschrock arc_eviction_list = buf->b_next; 18547545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 18551544Seschrock buf->b_hdr = NULL; 18567545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 18571544Seschrock mutex_exit(&arc_eviction_mtx); 18581544Seschrock 18591819Smaybee if (buf->b_efunc != NULL) 18601819Smaybee VERIFY(buf->b_efunc(buf) == 0); 18611544Seschrock 18621544Seschrock buf->b_efunc = NULL; 18631544Seschrock buf->b_private = NULL; 18641544Seschrock kmem_cache_free(buf_cache, buf); 18651544Seschrock mutex_enter(&arc_eviction_mtx); 18661544Seschrock } 18671544Seschrock mutex_exit(&arc_eviction_mtx); 18681544Seschrock } 18691544Seschrock 1870789Sahrens /* 18715642Smaybee * Flush all *evictable* data from the cache for the given spa. 1872789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1873789Sahrens */ 1874789Sahrens void 18755642Smaybee arc_flush(spa_t *spa) 1876789Sahrens { 18778636SMark.Maybee@Sun.COM uint64_t guid = 0; 18788636SMark.Maybee@Sun.COM 18798636SMark.Maybee@Sun.COM if (spa) 18808636SMark.Maybee@Sun.COM guid = spa_guid(spa); 18818636SMark.Maybee@Sun.COM 18825642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 18838636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 18845642Smaybee if (spa) 18855642Smaybee break; 18865642Smaybee } 18875642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 18888636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 18895642Smaybee if (spa) 18905642Smaybee break; 18915642Smaybee } 18925642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 18938636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 18945642Smaybee if (spa) 18955642Smaybee break; 18965642Smaybee } 18975642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 18988636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 18995642Smaybee if (spa) 19005642Smaybee break; 19015642Smaybee } 19025642Smaybee 19038636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mru_ghost, guid, -1); 19048636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mfu_ghost, guid, -1); 19051544Seschrock 19061544Seschrock mutex_enter(&arc_reclaim_thr_lock); 19071544Seschrock arc_do_user_evicts(); 19081544Seschrock mutex_exit(&arc_reclaim_thr_lock); 19095642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1910789Sahrens } 1911789Sahrens 1912789Sahrens void 19133158Smaybee arc_shrink(void) 1914789Sahrens { 19153403Sbmc if (arc_c > arc_c_min) { 19163158Smaybee uint64_t to_free; 1917789Sahrens 19182048Sstans #ifdef _KERNEL 19193403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 19202048Sstans #else 19213403Sbmc to_free = arc_c >> arc_shrink_shift; 19222048Sstans #endif 19233403Sbmc if (arc_c > arc_c_min + to_free) 19243403Sbmc atomic_add_64(&arc_c, -to_free); 19253158Smaybee else 19263403Sbmc arc_c = arc_c_min; 19272048Sstans 19283403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 19293403Sbmc if (arc_c > arc_size) 19303403Sbmc arc_c = MAX(arc_size, arc_c_min); 19313403Sbmc if (arc_p > arc_c) 19323403Sbmc arc_p = (arc_c >> 1); 19333403Sbmc ASSERT(arc_c >= arc_c_min); 19343403Sbmc ASSERT((int64_t)arc_p >= 0); 19353158Smaybee } 1936789Sahrens 19373403Sbmc if (arc_size > arc_c) 19383158Smaybee arc_adjust(); 1939789Sahrens } 1940789Sahrens 1941789Sahrens static int 1942789Sahrens arc_reclaim_needed(void) 1943789Sahrens { 1944789Sahrens uint64_t extra; 1945789Sahrens 1946789Sahrens #ifdef _KERNEL 19472048Sstans 19482048Sstans if (needfree) 19492048Sstans return (1); 19502048Sstans 1951789Sahrens /* 1952789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1953789Sahrens */ 1954789Sahrens extra = desfree; 1955789Sahrens 1956789Sahrens /* 1957789Sahrens * check that we're out of range of the pageout scanner. It starts to 1958789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1959789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1960789Sahrens * number of needed free pages. We add extra pages here to make sure 1961789Sahrens * the scanner doesn't start up while we're freeing memory. 1962789Sahrens */ 1963789Sahrens if (freemem < lotsfree + needfree + extra) 1964789Sahrens return (1); 1965789Sahrens 1966789Sahrens /* 1967789Sahrens * check to make sure that swapfs has enough space so that anon 19685450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1969789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1970789Sahrens * swap pages. We also add a bit of extra here just to prevent 1971789Sahrens * circumstances from getting really dire. 1972789Sahrens */ 1973789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1974789Sahrens return (1); 1975789Sahrens 19761936Smaybee #if defined(__i386) 1977789Sahrens /* 1978789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1979789Sahrens * kernel heap space before we ever run out of available physical 1980789Sahrens * memory. Most checks of the size of the heap_area compare against 1981789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1982789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1983789Sahrens * which is so low that it's useless. In this comparison, we seek to 1984789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 19855450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1986789Sahrens * free) 1987789Sahrens */ 1988789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1989789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1990789Sahrens return (1); 1991789Sahrens #endif 1992789Sahrens 1993789Sahrens #else 1994789Sahrens if (spa_get_random(100) == 0) 1995789Sahrens return (1); 1996789Sahrens #endif 1997789Sahrens return (0); 1998789Sahrens } 1999789Sahrens 2000789Sahrens static void 2001789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 2002789Sahrens { 2003789Sahrens size_t i; 2004789Sahrens kmem_cache_t *prev_cache = NULL; 20053290Sjohansen kmem_cache_t *prev_data_cache = NULL; 2006789Sahrens extern kmem_cache_t *zio_buf_cache[]; 20073290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 2008789Sahrens 20091484Sek110237 #ifdef _KERNEL 20104309Smaybee if (arc_meta_used >= arc_meta_limit) { 20114309Smaybee /* 20124309Smaybee * We are exceeding our meta-data cache limit. 20134309Smaybee * Purge some DNLC entries to release holds on meta-data. 20144309Smaybee */ 20154309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 20164309Smaybee } 20171936Smaybee #if defined(__i386) 20181936Smaybee /* 20191936Smaybee * Reclaim unused memory from all kmem caches. 20201936Smaybee */ 20211936Smaybee kmem_reap(); 20221936Smaybee #endif 20231484Sek110237 #endif 20241484Sek110237 2025789Sahrens /* 20265450Sbrendan * An aggressive reclamation will shrink the cache size as well as 20271544Seschrock * reap free buffers from the arc kmem caches. 2028789Sahrens */ 2029789Sahrens if (strat == ARC_RECLAIM_AGGR) 20303158Smaybee arc_shrink(); 2031789Sahrens 2032789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2033789Sahrens if (zio_buf_cache[i] != prev_cache) { 2034789Sahrens prev_cache = zio_buf_cache[i]; 2035789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 2036789Sahrens } 20373290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 20383290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 20393290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 20403290Sjohansen } 2041789Sahrens } 20421544Seschrock kmem_cache_reap_now(buf_cache); 20431544Seschrock kmem_cache_reap_now(hdr_cache); 2044789Sahrens } 2045789Sahrens 2046789Sahrens static void 2047789Sahrens arc_reclaim_thread(void) 2048789Sahrens { 2049789Sahrens clock_t growtime = 0; 2050789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2051789Sahrens callb_cpr_t cpr; 2052789Sahrens 2053789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2054789Sahrens 2055789Sahrens mutex_enter(&arc_reclaim_thr_lock); 2056789Sahrens while (arc_thread_exit == 0) { 2057789Sahrens if (arc_reclaim_needed()) { 2058789Sahrens 20593403Sbmc if (arc_no_grow) { 2060789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 2061789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2062789Sahrens } else { 2063789Sahrens last_reclaim = ARC_RECLAIM_CONS; 2064789Sahrens } 2065789Sahrens } else { 20663403Sbmc arc_no_grow = TRUE; 2067789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2068789Sahrens membar_producer(); 2069789Sahrens } 2070789Sahrens 2071789Sahrens /* reset the growth delay for every reclaim */ 207211066Srafael.vanoni@sun.com growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 2073789Sahrens 2074789Sahrens arc_kmem_reap_now(last_reclaim); 20756987Sbrendan arc_warm = B_TRUE; 2076789Sahrens 207711066Srafael.vanoni@sun.com } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 20783403Sbmc arc_no_grow = FALSE; 2079789Sahrens } 2080789Sahrens 20813403Sbmc if (2 * arc_c < arc_size + 20823403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 20833298Smaybee arc_adjust(); 20843298Smaybee 20851544Seschrock if (arc_eviction_list != NULL) 20861544Seschrock arc_do_user_evicts(); 20871544Seschrock 2088789Sahrens /* block until needed, or one second, whichever is shorter */ 2089789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 2090789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 209111066Srafael.vanoni@sun.com &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz)); 2092789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2093789Sahrens } 2094789Sahrens 2095789Sahrens arc_thread_exit = 0; 2096789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 2097789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2098789Sahrens thread_exit(); 2099789Sahrens } 2100789Sahrens 21011544Seschrock /* 21021544Seschrock * Adapt arc info given the number of bytes we are trying to add and 21031544Seschrock * the state that we are comming from. This function is only called 21041544Seschrock * when we are adding new content to the cache. 21051544Seschrock */ 2106789Sahrens static void 21071544Seschrock arc_adapt(int bytes, arc_state_t *state) 2108789Sahrens { 21091544Seschrock int mult; 21108582SBrendan.Gregg@Sun.COM uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 21111544Seschrock 21125450Sbrendan if (state == arc_l2c_only) 21135450Sbrendan return; 21145450Sbrendan 21151544Seschrock ASSERT(bytes > 0); 2116789Sahrens /* 21171544Seschrock * Adapt the target size of the MRU list: 21181544Seschrock * - if we just hit in the MRU ghost list, then increase 21191544Seschrock * the target size of the MRU list. 21201544Seschrock * - if we just hit in the MFU ghost list, then increase 21211544Seschrock * the target size of the MFU list by decreasing the 21221544Seschrock * target size of the MRU list. 2123789Sahrens */ 21243403Sbmc if (state == arc_mru_ghost) { 21253403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 21263403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 21271544Seschrock 21288582SBrendan.Gregg@Sun.COM arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 21293403Sbmc } else if (state == arc_mfu_ghost) { 21308582SBrendan.Gregg@Sun.COM uint64_t delta; 21318582SBrendan.Gregg@Sun.COM 21323403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 21333403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 21341544Seschrock 21358582SBrendan.Gregg@Sun.COM delta = MIN(bytes * mult, arc_p); 21368582SBrendan.Gregg@Sun.COM arc_p = MAX(arc_p_min, arc_p - delta); 21371544Seschrock } 21383403Sbmc ASSERT((int64_t)arc_p >= 0); 2139789Sahrens 2140789Sahrens if (arc_reclaim_needed()) { 2141789Sahrens cv_signal(&arc_reclaim_thr_cv); 2142789Sahrens return; 2143789Sahrens } 2144789Sahrens 21453403Sbmc if (arc_no_grow) 2146789Sahrens return; 2147789Sahrens 21483403Sbmc if (arc_c >= arc_c_max) 21491544Seschrock return; 21501544Seschrock 2151789Sahrens /* 21521544Seschrock * If we're within (2 * maxblocksize) bytes of the target 21531544Seschrock * cache size, increment the target cache size 2154789Sahrens */ 21553403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 21563403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 21573403Sbmc if (arc_c > arc_c_max) 21583403Sbmc arc_c = arc_c_max; 21593403Sbmc else if (state == arc_anon) 21603403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 21613403Sbmc if (arc_p > arc_c) 21623403Sbmc arc_p = arc_c; 2163789Sahrens } 21643403Sbmc ASSERT((int64_t)arc_p >= 0); 2165789Sahrens } 2166789Sahrens 2167789Sahrens /* 21681544Seschrock * Check if the cache has reached its limits and eviction is required 21691544Seschrock * prior to insert. 2170789Sahrens */ 2171789Sahrens static int 21724309Smaybee arc_evict_needed(arc_buf_contents_t type) 2173789Sahrens { 21744309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 21754309Smaybee return (1); 21764309Smaybee 21774309Smaybee #ifdef _KERNEL 21784309Smaybee /* 21794309Smaybee * If zio data pages are being allocated out of a separate heap segment, 21804309Smaybee * then enforce that the size of available vmem for this area remains 21814309Smaybee * above about 1/32nd free. 21824309Smaybee */ 21834309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 21844309Smaybee vmem_size(zio_arena, VMEM_FREE) < 21854309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 21864309Smaybee return (1); 21874309Smaybee #endif 21884309Smaybee 2189789Sahrens if (arc_reclaim_needed()) 2190789Sahrens return (1); 2191789Sahrens 21923403Sbmc return (arc_size > arc_c); 2193789Sahrens } 2194789Sahrens 2195789Sahrens /* 21962688Smaybee * The buffer, supplied as the first argument, needs a data block. 21972688Smaybee * So, if we are at cache max, determine which cache should be victimized. 21982688Smaybee * We have the following cases: 2199789Sahrens * 22003403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2201789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2202789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2203789Sahrens * 22043403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2205789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2206789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2207789Sahrens * entries. 2208789Sahrens * 22093403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2210789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2211789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2212789Sahrens * the MFU side, so the MRU side needs to be victimized. 2213789Sahrens * 22143403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2215789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2216789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2217789Sahrens */ 2218789Sahrens static void 22192688Smaybee arc_get_data_buf(arc_buf_t *buf) 2220789Sahrens { 22213290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 22223290Sjohansen uint64_t size = buf->b_hdr->b_size; 22233290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 22242688Smaybee 22252688Smaybee arc_adapt(size, state); 2226789Sahrens 22272688Smaybee /* 22282688Smaybee * We have not yet reached cache maximum size, 22292688Smaybee * just allocate a new buffer. 22302688Smaybee */ 22314309Smaybee if (!arc_evict_needed(type)) { 22323290Sjohansen if (type == ARC_BUFC_METADATA) { 22333290Sjohansen buf->b_data = zio_buf_alloc(size); 22348582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 22353290Sjohansen } else { 22363290Sjohansen ASSERT(type == ARC_BUFC_DATA); 22373290Sjohansen buf->b_data = zio_data_buf_alloc(size); 22388582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 22394309Smaybee atomic_add_64(&arc_size, size); 22403290Sjohansen } 22412688Smaybee goto out; 22422688Smaybee } 22432688Smaybee 22442688Smaybee /* 22452688Smaybee * If we are prefetching from the mfu ghost list, this buffer 22462688Smaybee * will end up on the mru list; so steal space from there. 22472688Smaybee */ 22483403Sbmc if (state == arc_mfu_ghost) 22493403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 22503403Sbmc else if (state == arc_mru_ghost) 22513403Sbmc state = arc_mru; 2252789Sahrens 22533403Sbmc if (state == arc_mru || state == arc_anon) { 22543403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 22558582SBrendan.Gregg@Sun.COM state = (arc_mfu->arcs_lsize[type] >= size && 22564309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2257789Sahrens } else { 22582688Smaybee /* MFU cases */ 22593403Sbmc uint64_t mfu_space = arc_c - arc_p; 22608582SBrendan.Gregg@Sun.COM state = (arc_mru->arcs_lsize[type] >= size && 22614309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 22622688Smaybee } 22635642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 22643290Sjohansen if (type == ARC_BUFC_METADATA) { 22653290Sjohansen buf->b_data = zio_buf_alloc(size); 22668582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 22673290Sjohansen } else { 22683290Sjohansen ASSERT(type == ARC_BUFC_DATA); 22693290Sjohansen buf->b_data = zio_data_buf_alloc(size); 22708582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 22714309Smaybee atomic_add_64(&arc_size, size); 22723290Sjohansen } 22733403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 22742688Smaybee } 22752688Smaybee ASSERT(buf->b_data != NULL); 22762688Smaybee out: 22772688Smaybee /* 22782688Smaybee * Update the state size. Note that ghost states have a 22792688Smaybee * "ghost size" and so don't need to be updated. 22802688Smaybee */ 22812688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 22822688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 22832688Smaybee 22843403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 22852688Smaybee if (list_link_active(&hdr->b_arc_node)) { 22862688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 22874309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2288789Sahrens } 22893298Smaybee /* 22903298Smaybee * If we are growing the cache, and we are adding anonymous 22913403Sbmc * data, and we have outgrown arc_p, update arc_p 22923298Smaybee */ 22933403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 22943403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 22953403Sbmc arc_p = MIN(arc_c, arc_p + size); 2296789Sahrens } 2297789Sahrens } 2298789Sahrens 2299789Sahrens /* 2300789Sahrens * This routine is called whenever a buffer is accessed. 23011544Seschrock * NOTE: the hash lock is dropped in this function. 2302789Sahrens */ 2303789Sahrens static void 23042688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2305789Sahrens { 230611066Srafael.vanoni@sun.com clock_t now; 230711066Srafael.vanoni@sun.com 2308789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2309789Sahrens 23103403Sbmc if (buf->b_state == arc_anon) { 2311789Sahrens /* 2312789Sahrens * This buffer is not in the cache, and does not 2313789Sahrens * appear in our "ghost" list. Add the new buffer 2314789Sahrens * to the MRU state. 2315789Sahrens */ 2316789Sahrens 2317789Sahrens ASSERT(buf->b_arc_access == 0); 231811066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 23191544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 23203403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2321789Sahrens 23223403Sbmc } else if (buf->b_state == arc_mru) { 232311066Srafael.vanoni@sun.com now = ddi_get_lbolt(); 232411066Srafael.vanoni@sun.com 2325789Sahrens /* 23262391Smaybee * If this buffer is here because of a prefetch, then either: 23272391Smaybee * - clear the flag if this is a "referencing" read 23282391Smaybee * (any subsequent access will bump this into the MFU state). 23292391Smaybee * or 23302391Smaybee * - move the buffer to the head of the list if this is 23312391Smaybee * another prefetch (to make it less likely to be evicted). 2332789Sahrens */ 2333789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 23342391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 23352391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23362391Smaybee } else { 23372391Smaybee buf->b_flags &= ~ARC_PREFETCH; 23383403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23392391Smaybee } 234011066Srafael.vanoni@sun.com buf->b_arc_access = now; 2341789Sahrens return; 2342789Sahrens } 2343789Sahrens 2344789Sahrens /* 2345789Sahrens * This buffer has been "accessed" only once so far, 2346789Sahrens * but it is still in the cache. Move it to the MFU 2347789Sahrens * state. 2348789Sahrens */ 234911066Srafael.vanoni@sun.com if (now > buf->b_arc_access + ARC_MINTIME) { 2350789Sahrens /* 2351789Sahrens * More than 125ms have passed since we 2352789Sahrens * instantiated this buffer. Move it to the 2353789Sahrens * most frequently used state. 2354789Sahrens */ 235511066Srafael.vanoni@sun.com buf->b_arc_access = now; 23561544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23573403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2358789Sahrens } 23593403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23603403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2361789Sahrens arc_state_t *new_state; 2362789Sahrens /* 2363789Sahrens * This buffer has been "accessed" recently, but 2364789Sahrens * was evicted from the cache. Move it to the 2365789Sahrens * MFU state. 2366789Sahrens */ 2367789Sahrens 2368789Sahrens if (buf->b_flags & ARC_PREFETCH) { 23693403Sbmc new_state = arc_mru; 23702391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 23712391Smaybee buf->b_flags &= ~ARC_PREFETCH; 23721544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2373789Sahrens } else { 23743403Sbmc new_state = arc_mfu; 23751544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2376789Sahrens } 2377789Sahrens 237811066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 2379789Sahrens arc_change_state(new_state, buf, hash_lock); 2380789Sahrens 23813403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 23823403Sbmc } else if (buf->b_state == arc_mfu) { 2383789Sahrens /* 2384789Sahrens * This buffer has been accessed more than once and is 2385789Sahrens * still in the cache. Keep it in the MFU state. 2386789Sahrens * 23872391Smaybee * NOTE: an add_reference() that occurred when we did 23882391Smaybee * the arc_read() will have kicked this off the list. 23892391Smaybee * If it was a prefetch, we will explicitly move it to 23902391Smaybee * the head of the list now. 2391789Sahrens */ 23922391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 23932391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 23942391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23952391Smaybee } 23963403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 239711066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 23983403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 23993403Sbmc arc_state_t *new_state = arc_mfu; 2400789Sahrens /* 2401789Sahrens * This buffer has been accessed more than once but has 2402789Sahrens * been evicted from the cache. Move it back to the 2403789Sahrens * MFU state. 2404789Sahrens */ 2405789Sahrens 24062391Smaybee if (buf->b_flags & ARC_PREFETCH) { 24072391Smaybee /* 24082391Smaybee * This is a prefetch access... 24092391Smaybee * move this block back to the MRU state. 24102391Smaybee */ 24112391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 24123403Sbmc new_state = arc_mru; 24132391Smaybee } 24142391Smaybee 241511066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 24161544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 24172391Smaybee arc_change_state(new_state, buf, hash_lock); 2418789Sahrens 24193403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 24205450Sbrendan } else if (buf->b_state == arc_l2c_only) { 24215450Sbrendan /* 24225450Sbrendan * This buffer is on the 2nd Level ARC. 24235450Sbrendan */ 24245450Sbrendan 242511066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 24265450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 24275450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2428789Sahrens } else { 2429789Sahrens ASSERT(!"invalid arc state"); 2430789Sahrens } 2431789Sahrens } 2432789Sahrens 2433789Sahrens /* a generic arc_done_func_t which you can use */ 2434789Sahrens /* ARGSUSED */ 2435789Sahrens void 2436789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2437789Sahrens { 2438789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 24391544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2440789Sahrens } 2441789Sahrens 24424309Smaybee /* a generic arc_done_func_t */ 2443789Sahrens void 2444789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2445789Sahrens { 2446789Sahrens arc_buf_t **bufp = arg; 2447789Sahrens if (zio && zio->io_error) { 24481544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2449789Sahrens *bufp = NULL; 2450789Sahrens } else { 2451789Sahrens *bufp = buf; 2452789Sahrens } 2453789Sahrens } 2454789Sahrens 2455789Sahrens static void 2456789Sahrens arc_read_done(zio_t *zio) 2457789Sahrens { 24581589Smaybee arc_buf_hdr_t *hdr, *found; 2459789Sahrens arc_buf_t *buf; 2460789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2461789Sahrens kmutex_t *hash_lock; 2462789Sahrens arc_callback_t *callback_list, *acb; 2463789Sahrens int freeable = FALSE; 2464789Sahrens 2465789Sahrens buf = zio->io_private; 2466789Sahrens hdr = buf->b_hdr; 2467789Sahrens 24681589Smaybee /* 24691589Smaybee * The hdr was inserted into hash-table and removed from lists 24701589Smaybee * prior to starting I/O. We should find this header, since 24711589Smaybee * it's in the hash table, and it should be legit since it's 24721589Smaybee * not possible to evict it during the I/O. The only possible 24731589Smaybee * reason for it not to be found is if we were freed during the 24741589Smaybee * read. 24751589Smaybee */ 24768636SMark.Maybee@Sun.COM found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 24773093Sahrens &hash_lock); 2478789Sahrens 24791589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 24805450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 24815450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 24825450Sbrendan 24836987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 24845450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 24857237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2486789Sahrens 2487789Sahrens /* byteswap if necessary */ 2488789Sahrens callback_list = hdr->b_acb; 2489789Sahrens ASSERT(callback_list != NULL); 249010839Swilliam.gorrell@sun.com if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 24917046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 24927046Sahrens byteswap_uint64_array : 24937046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 24947046Sahrens func(buf->b_data, hdr->b_size); 24957046Sahrens } 2496789Sahrens 24975450Sbrendan arc_cksum_compute(buf, B_FALSE); 24983093Sahrens 249910922SJeff.Bonwick@Sun.COM if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 250010922SJeff.Bonwick@Sun.COM /* 250110922SJeff.Bonwick@Sun.COM * Only call arc_access on anonymous buffers. This is because 250210922SJeff.Bonwick@Sun.COM * if we've issued an I/O for an evicted buffer, we've already 250310922SJeff.Bonwick@Sun.COM * called arc_access (to prevent any simultaneous readers from 250410922SJeff.Bonwick@Sun.COM * getting confused). 250510922SJeff.Bonwick@Sun.COM */ 250610922SJeff.Bonwick@Sun.COM arc_access(hdr, hash_lock); 250710922SJeff.Bonwick@Sun.COM } 250810922SJeff.Bonwick@Sun.COM 2509789Sahrens /* create copies of the data buffer for the callers */ 2510789Sahrens abuf = buf; 2511789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2512789Sahrens if (acb->acb_done) { 25132688Smaybee if (abuf == NULL) 25142688Smaybee abuf = arc_buf_clone(buf); 2515789Sahrens acb->acb_buf = abuf; 2516789Sahrens abuf = NULL; 2517789Sahrens } 2518789Sahrens } 2519789Sahrens hdr->b_acb = NULL; 2520789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 25211544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 252210922SJeff.Bonwick@Sun.COM if (abuf == buf) { 252310922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 252410922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 25251544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 252610922SJeff.Bonwick@Sun.COM } 2527789Sahrens 2528789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2529789Sahrens 2530789Sahrens if (zio->io_error != 0) { 2531789Sahrens hdr->b_flags |= ARC_IO_ERROR; 25323403Sbmc if (hdr->b_state != arc_anon) 25333403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 25341544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 25351544Seschrock buf_hash_remove(hdr); 2536789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2537789Sahrens } 2538789Sahrens 25391544Seschrock /* 25402391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 25412391Smaybee * that the hdr (and hence the cv) might be freed before we get to 25422391Smaybee * the cv_broadcast(). 25431544Seschrock */ 25441544Seschrock cv_broadcast(&hdr->b_cv); 25451544Seschrock 25461589Smaybee if (hash_lock) { 25472688Smaybee mutex_exit(hash_lock); 2548789Sahrens } else { 2549789Sahrens /* 2550789Sahrens * This block was freed while we waited for the read to 2551789Sahrens * complete. It has been removed from the hash table and 2552789Sahrens * moved to the anonymous state (so that it won't show up 2553789Sahrens * in the cache). 2554789Sahrens */ 25553403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2556789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2557789Sahrens } 2558789Sahrens 2559789Sahrens /* execute each callback and free its structure */ 2560789Sahrens while ((acb = callback_list) != NULL) { 2561789Sahrens if (acb->acb_done) 2562789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2563789Sahrens 2564789Sahrens if (acb->acb_zio_dummy != NULL) { 2565789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2566789Sahrens zio_nowait(acb->acb_zio_dummy); 2567789Sahrens } 2568789Sahrens 2569789Sahrens callback_list = acb->acb_next; 2570789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2571789Sahrens } 2572789Sahrens 2573789Sahrens if (freeable) 25741544Seschrock arc_hdr_destroy(hdr); 2575789Sahrens } 2576789Sahrens 2577789Sahrens /* 2578789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2579789Sahrens * cache. If the block is found in the cache, invoke the provided 2580789Sahrens * callback immediately and return. Note that the `zio' parameter 2581789Sahrens * in the callback will be NULL in this case, since no IO was 2582789Sahrens * required. If the block is not in the cache pass the read request 2583789Sahrens * on to the spa with a substitute callback function, so that the 2584789Sahrens * requested block will be added to the cache. 2585789Sahrens * 2586789Sahrens * If a read request arrives for a block that has a read in-progress, 2587789Sahrens * either wait for the in-progress read to complete (and return the 2588789Sahrens * results); or, if this is a read with a "done" func, add a record 2589789Sahrens * to the read to invoke the "done" func when the read completes, 2590789Sahrens * and return; or just return. 2591789Sahrens * 2592789Sahrens * arc_read_done() will invoke all the requested "done" functions 2593789Sahrens * for readers of this block. 25947046Sahrens * 25957046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 25967046Sahrens * for the bp. But if you know you don't need locking, you can use 25978213SSuhasini.Peddada@Sun.COM * arc_read_bp. 2598789Sahrens */ 2599789Sahrens int 260010922SJeff.Bonwick@Sun.COM arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf, 26017237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 26027046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 26037046Sahrens { 26047046Sahrens int err; 26057046Sahrens 26067046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 26077046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 26087545SMark.Maybee@Sun.COM rw_enter(&pbuf->b_lock, RW_READER); 26097046Sahrens 26107046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 26117237Sek110237 zio_flags, arc_flags, zb); 26127545SMark.Maybee@Sun.COM rw_exit(&pbuf->b_lock); 26139396SMatthew.Ahrens@Sun.COM 26147046Sahrens return (err); 26157046Sahrens } 26167046Sahrens 26177046Sahrens int 261810922SJeff.Bonwick@Sun.COM arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp, 26197237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 26207046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2621789Sahrens { 2622789Sahrens arc_buf_hdr_t *hdr; 2623789Sahrens arc_buf_t *buf; 2624789Sahrens kmutex_t *hash_lock; 26255450Sbrendan zio_t *rzio; 26268636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 2627789Sahrens 2628789Sahrens top: 262910922SJeff.Bonwick@Sun.COM hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 263010922SJeff.Bonwick@Sun.COM &hash_lock); 26311544Seschrock if (hdr && hdr->b_datacnt > 0) { 2632789Sahrens 26332391Smaybee *arc_flags |= ARC_CACHED; 26342391Smaybee 2635789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 26362391Smaybee 26372391Smaybee if (*arc_flags & ARC_WAIT) { 26382391Smaybee cv_wait(&hdr->b_cv, hash_lock); 26392391Smaybee mutex_exit(hash_lock); 26402391Smaybee goto top; 26412391Smaybee } 26422391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 26432391Smaybee 26442391Smaybee if (done) { 2645789Sahrens arc_callback_t *acb = NULL; 2646789Sahrens 2647789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2648789Sahrens KM_SLEEP); 2649789Sahrens acb->acb_done = done; 2650789Sahrens acb->acb_private = private; 2651789Sahrens if (pio != NULL) 2652789Sahrens acb->acb_zio_dummy = zio_null(pio, 26538632SBill.Moore@Sun.COM spa, NULL, NULL, NULL, zio_flags); 2654789Sahrens 2655789Sahrens ASSERT(acb->acb_done != NULL); 2656789Sahrens acb->acb_next = hdr->b_acb; 2657789Sahrens hdr->b_acb = acb; 2658789Sahrens add_reference(hdr, hash_lock, private); 2659789Sahrens mutex_exit(hash_lock); 2660789Sahrens return (0); 2661789Sahrens } 2662789Sahrens mutex_exit(hash_lock); 2663789Sahrens return (0); 2664789Sahrens } 2665789Sahrens 26663403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2667789Sahrens 26681544Seschrock if (done) { 26692688Smaybee add_reference(hdr, hash_lock, private); 26701544Seschrock /* 26711544Seschrock * If this block is already in use, create a new 26721544Seschrock * copy of the data so that we will be guaranteed 26731544Seschrock * that arc_release() will always succeed. 26741544Seschrock */ 26751544Seschrock buf = hdr->b_buf; 26761544Seschrock ASSERT(buf); 26771544Seschrock ASSERT(buf->b_data); 26782688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 26791544Seschrock ASSERT(buf->b_efunc == NULL); 26801544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 26812688Smaybee } else { 26822688Smaybee buf = arc_buf_clone(buf); 26831544Seschrock } 268410922SJeff.Bonwick@Sun.COM 26852391Smaybee } else if (*arc_flags & ARC_PREFETCH && 26862391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 26872391Smaybee hdr->b_flags |= ARC_PREFETCH; 2688789Sahrens } 2689789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 26902688Smaybee arc_access(hdr, hash_lock); 26917237Sek110237 if (*arc_flags & ARC_L2CACHE) 26927237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26932688Smaybee mutex_exit(hash_lock); 26943403Sbmc ARCSTAT_BUMP(arcstat_hits); 26953403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 26963403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 26973403Sbmc data, metadata, hits); 26983403Sbmc 2699789Sahrens if (done) 2700789Sahrens done(NULL, buf, private); 2701789Sahrens } else { 2702789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2703789Sahrens arc_callback_t *acb; 27046987Sbrendan vdev_t *vd = NULL; 27059215SGeorge.Wilson@Sun.COM uint64_t addr; 27068582SBrendan.Gregg@Sun.COM boolean_t devw = B_FALSE; 2707789Sahrens 2708789Sahrens if (hdr == NULL) { 2709789Sahrens /* this block is not in the cache */ 2710789Sahrens arc_buf_hdr_t *exists; 27113290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 27123290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2713789Sahrens hdr = buf->b_hdr; 2714789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 271510922SJeff.Bonwick@Sun.COM hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 2716789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2717789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2718789Sahrens if (exists) { 2719789Sahrens /* somebody beat us to the hash insert */ 2720789Sahrens mutex_exit(hash_lock); 2721789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2722789Sahrens hdr->b_birth = 0; 2723789Sahrens hdr->b_cksum0 = 0; 27241544Seschrock (void) arc_buf_remove_ref(buf, private); 2725789Sahrens goto top; /* restart the IO request */ 2726789Sahrens } 27272391Smaybee /* if this is a prefetch, we don't have a reference */ 27282391Smaybee if (*arc_flags & ARC_PREFETCH) { 27292391Smaybee (void) remove_reference(hdr, hash_lock, 27302391Smaybee private); 27312391Smaybee hdr->b_flags |= ARC_PREFETCH; 27322391Smaybee } 27337237Sek110237 if (*arc_flags & ARC_L2CACHE) 27347237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27352391Smaybee if (BP_GET_LEVEL(bp) > 0) 27362391Smaybee hdr->b_flags |= ARC_INDIRECT; 2737789Sahrens } else { 2738789Sahrens /* this block is in the ghost cache */ 27391544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 27401544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 27412391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 27422391Smaybee ASSERT(hdr->b_buf == NULL); 2743789Sahrens 27442391Smaybee /* if this is a prefetch, we don't have a reference */ 27452391Smaybee if (*arc_flags & ARC_PREFETCH) 27462391Smaybee hdr->b_flags |= ARC_PREFETCH; 27472391Smaybee else 27482391Smaybee add_reference(hdr, hash_lock, private); 27497237Sek110237 if (*arc_flags & ARC_L2CACHE) 27507237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27516245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 27521544Seschrock buf->b_hdr = hdr; 27532688Smaybee buf->b_data = NULL; 27541544Seschrock buf->b_efunc = NULL; 27551544Seschrock buf->b_private = NULL; 27561544Seschrock buf->b_next = NULL; 27571544Seschrock hdr->b_buf = buf; 27582688Smaybee arc_get_data_buf(buf); 27591544Seschrock ASSERT(hdr->b_datacnt == 0); 27601544Seschrock hdr->b_datacnt = 1; 2761789Sahrens } 2762789Sahrens 2763789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2764789Sahrens acb->acb_done = done; 2765789Sahrens acb->acb_private = private; 2766789Sahrens 2767789Sahrens ASSERT(hdr->b_acb == NULL); 2768789Sahrens hdr->b_acb = acb; 2769789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2770789Sahrens 2771789Sahrens /* 2772789Sahrens * If the buffer has been evicted, migrate it to a present state 2773789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2774789Sahrens * the header will be marked as I/O in progress and have an 2775789Sahrens * attached buffer. At this point, anybody who finds this 2776789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2777789Sahrens */ 2778789Sahrens 27791544Seschrock if (GHOST_STATE(hdr->b_state)) 27802688Smaybee arc_access(hdr, hash_lock); 2781789Sahrens 27827754SJeff.Bonwick@Sun.COM if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 27837754SJeff.Bonwick@Sun.COM (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 27848582SBrendan.Gregg@Sun.COM devw = hdr->b_l2hdr->b_dev->l2ad_writing; 27856987Sbrendan addr = hdr->b_l2hdr->b_daddr; 27867754SJeff.Bonwick@Sun.COM /* 27877754SJeff.Bonwick@Sun.COM * Lock out device removal. 27887754SJeff.Bonwick@Sun.COM */ 27897754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || 27907754SJeff.Bonwick@Sun.COM !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 27917754SJeff.Bonwick@Sun.COM vd = NULL; 27926987Sbrendan } 27936987Sbrendan 27946987Sbrendan mutex_exit(hash_lock); 27956987Sbrendan 2796789Sahrens ASSERT3U(hdr->b_size, ==, size); 279710409SBrendan.Gregg@Sun.COM DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 279810409SBrendan.Gregg@Sun.COM uint64_t, size, zbookmark_t *, zb); 27993403Sbmc ARCSTAT_BUMP(arcstat_misses); 28003403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 28013403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 28023403Sbmc data, metadata, misses); 28031544Seschrock 28048582SBrendan.Gregg@Sun.COM if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 28056987Sbrendan /* 28066987Sbrendan * Read from the L2ARC if the following are true: 28076987Sbrendan * 1. The L2ARC vdev was previously cached. 28086987Sbrendan * 2. This buffer still has L2ARC metadata. 28096987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 28106987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 28116987Sbrendan * also have invalidated the vdev. 28128582SBrendan.Gregg@Sun.COM * 5. This isn't prefetch and l2arc_noprefetch is set. 28136987Sbrendan */ 28147754SJeff.Bonwick@Sun.COM if (hdr->b_l2hdr != NULL && 28158582SBrendan.Gregg@Sun.COM !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 28168582SBrendan.Gregg@Sun.COM !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 28175450Sbrendan l2arc_read_callback_t *cb; 28185450Sbrendan 28196643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 28206643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 28216643Seschrock 28225450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 28235450Sbrendan KM_SLEEP); 28245450Sbrendan cb->l2rcb_buf = buf; 28255450Sbrendan cb->l2rcb_spa = spa; 28265450Sbrendan cb->l2rcb_bp = *bp; 28275450Sbrendan cb->l2rcb_zb = *zb; 28287237Sek110237 cb->l2rcb_flags = zio_flags; 28295450Sbrendan 28305450Sbrendan /* 28317754SJeff.Bonwick@Sun.COM * l2arc read. The SCL_L2ARC lock will be 28327754SJeff.Bonwick@Sun.COM * released by l2arc_read_done(). 28335450Sbrendan */ 28345450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 28355450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 28367237Sek110237 l2arc_read_done, cb, priority, zio_flags | 28377361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 28387754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_PROPAGATE | 28397754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_RETRY, B_FALSE); 28405450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 28415450Sbrendan zio_t *, rzio); 28428582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_read_bytes, size); 28436987Sbrendan 28446987Sbrendan if (*arc_flags & ARC_NOWAIT) { 28456987Sbrendan zio_nowait(rzio); 28466987Sbrendan return (0); 28476987Sbrendan } 28486987Sbrendan 28496987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 28506987Sbrendan if (zio_wait(rzio) == 0) 28516987Sbrendan return (0); 28526987Sbrendan 28536987Sbrendan /* l2arc read error; goto zio_read() */ 28545450Sbrendan } else { 28555450Sbrendan DTRACE_PROBE1(l2arc__miss, 28565450Sbrendan arc_buf_hdr_t *, hdr); 28575450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 28585450Sbrendan if (HDR_L2_WRITING(hdr)) 28595450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 28607754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 28615450Sbrendan } 28628582SBrendan.Gregg@Sun.COM } else { 28638628SBill.Moore@Sun.COM if (vd != NULL) 28648628SBill.Moore@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 28658582SBrendan.Gregg@Sun.COM if (l2arc_ndev != 0) { 28668582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(l2arc__miss, 28678582SBrendan.Gregg@Sun.COM arc_buf_hdr_t *, hdr); 28688582SBrendan.Gregg@Sun.COM ARCSTAT_BUMP(arcstat_l2_misses); 28698582SBrendan.Gregg@Sun.COM } 28705450Sbrendan } 28716643Seschrock 2872789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 28737237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2874789Sahrens 28752391Smaybee if (*arc_flags & ARC_WAIT) 2876789Sahrens return (zio_wait(rzio)); 2877789Sahrens 28782391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2879789Sahrens zio_nowait(rzio); 2880789Sahrens } 2881789Sahrens return (0); 2882789Sahrens } 2883789Sahrens 28841544Seschrock void 28851544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 28861544Seschrock { 28871544Seschrock ASSERT(buf->b_hdr != NULL); 28883403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 28891544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 289010922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 289110922SJeff.Bonwick@Sun.COM ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 289210922SJeff.Bonwick@Sun.COM 28931544Seschrock buf->b_efunc = func; 28941544Seschrock buf->b_private = private; 28951544Seschrock } 28961544Seschrock 28971544Seschrock /* 28981544Seschrock * This is used by the DMU to let the ARC know that a buffer is 28991544Seschrock * being evicted, so the ARC should clean up. If this arc buf 29001544Seschrock * is not yet in the evicted state, it will be put there. 29011544Seschrock */ 29021544Seschrock int 29031544Seschrock arc_buf_evict(arc_buf_t *buf) 29041544Seschrock { 29052887Smaybee arc_buf_hdr_t *hdr; 29061544Seschrock kmutex_t *hash_lock; 29071544Seschrock arc_buf_t **bufp; 29081544Seschrock 29097545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 29102887Smaybee hdr = buf->b_hdr; 29111544Seschrock if (hdr == NULL) { 29121544Seschrock /* 29131544Seschrock * We are in arc_do_user_evicts(). 29141544Seschrock */ 29151544Seschrock ASSERT(buf->b_data == NULL); 29167545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29171544Seschrock return (0); 29187545SMark.Maybee@Sun.COM } else if (buf->b_data == NULL) { 29197545SMark.Maybee@Sun.COM arc_buf_t copy = *buf; /* structure assignment */ 29207545SMark.Maybee@Sun.COM /* 29217545SMark.Maybee@Sun.COM * We are on the eviction list; process this buffer now 29227545SMark.Maybee@Sun.COM * but let arc_do_user_evicts() do the reaping. 29237545SMark.Maybee@Sun.COM */ 29247545SMark.Maybee@Sun.COM buf->b_efunc = NULL; 29257545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29267545SMark.Maybee@Sun.COM VERIFY(copy.b_efunc(©) == 0); 29277545SMark.Maybee@Sun.COM return (1); 29281544Seschrock } 29292887Smaybee hash_lock = HDR_LOCK(hdr); 29301544Seschrock mutex_enter(hash_lock); 29311544Seschrock 29322724Smaybee ASSERT(buf->b_hdr == hdr); 29332724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 29343403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 29351544Seschrock 29361544Seschrock /* 29371544Seschrock * Pull this buffer off of the hdr 29381544Seschrock */ 29391544Seschrock bufp = &hdr->b_buf; 29401544Seschrock while (*bufp != buf) 29411544Seschrock bufp = &(*bufp)->b_next; 29421544Seschrock *bufp = buf->b_next; 29431544Seschrock 29441544Seschrock ASSERT(buf->b_data != NULL); 29452688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 29461544Seschrock 29471544Seschrock if (hdr->b_datacnt == 0) { 29481544Seschrock arc_state_t *old_state = hdr->b_state; 29491544Seschrock arc_state_t *evicted_state; 29501544Seschrock 29511544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 29521544Seschrock 29531544Seschrock evicted_state = 29543403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 29551544Seschrock 29563403Sbmc mutex_enter(&old_state->arcs_mtx); 29573403Sbmc mutex_enter(&evicted_state->arcs_mtx); 29581544Seschrock 29591544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 29601544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 29615450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 29625450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 29631544Seschrock 29643403Sbmc mutex_exit(&evicted_state->arcs_mtx); 29653403Sbmc mutex_exit(&old_state->arcs_mtx); 29661544Seschrock } 29671544Seschrock mutex_exit(hash_lock); 29687545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29691819Smaybee 29701544Seschrock VERIFY(buf->b_efunc(buf) == 0); 29711544Seschrock buf->b_efunc = NULL; 29721544Seschrock buf->b_private = NULL; 29731544Seschrock buf->b_hdr = NULL; 29741544Seschrock kmem_cache_free(buf_cache, buf); 29751544Seschrock return (1); 29761544Seschrock } 29771544Seschrock 2978789Sahrens /* 2979789Sahrens * Release this buffer from the cache. This must be done 2980789Sahrens * after a read and prior to modifying the buffer contents. 2981789Sahrens * If the buffer has more than one reference, we must make 29827046Sahrens * a new hdr for the buffer. 2983789Sahrens */ 2984789Sahrens void 2985789Sahrens arc_release(arc_buf_t *buf, void *tag) 2986789Sahrens { 29877545SMark.Maybee@Sun.COM arc_buf_hdr_t *hdr; 29887545SMark.Maybee@Sun.COM kmutex_t *hash_lock; 29897545SMark.Maybee@Sun.COM l2arc_buf_hdr_t *l2hdr; 29905450Sbrendan uint64_t buf_size; 29919274SBrendan.Gregg@Sun.COM boolean_t released = B_FALSE; 2992789Sahrens 29937545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 29947545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 29957545SMark.Maybee@Sun.COM 2996789Sahrens /* this buffer is not on any list */ 2997789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 2998789Sahrens 29993403Sbmc if (hdr->b_state == arc_anon) { 3000789Sahrens /* this buffer is already released */ 3001789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 3002789Sahrens ASSERT(BUF_EMPTY(hdr)); 30031544Seschrock ASSERT(buf->b_efunc == NULL); 30043093Sahrens arc_buf_thaw(buf); 30057545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30069274SBrendan.Gregg@Sun.COM released = B_TRUE; 30079274SBrendan.Gregg@Sun.COM } else { 30089274SBrendan.Gregg@Sun.COM hash_lock = HDR_LOCK(hdr); 30099274SBrendan.Gregg@Sun.COM mutex_enter(hash_lock); 3010789Sahrens } 3011789Sahrens 30127545SMark.Maybee@Sun.COM l2hdr = hdr->b_l2hdr; 30137545SMark.Maybee@Sun.COM if (l2hdr) { 30147545SMark.Maybee@Sun.COM mutex_enter(&l2arc_buflist_mtx); 30157545SMark.Maybee@Sun.COM hdr->b_l2hdr = NULL; 30167545SMark.Maybee@Sun.COM buf_size = hdr->b_size; 30177545SMark.Maybee@Sun.COM } 30187545SMark.Maybee@Sun.COM 30199274SBrendan.Gregg@Sun.COM if (released) 30209274SBrendan.Gregg@Sun.COM goto out; 30219274SBrendan.Gregg@Sun.COM 30221544Seschrock /* 30231544Seschrock * Do we have more than one buf? 30241544Seschrock */ 30257545SMark.Maybee@Sun.COM if (hdr->b_datacnt > 1) { 3026789Sahrens arc_buf_hdr_t *nhdr; 3027789Sahrens arc_buf_t **bufp; 3028789Sahrens uint64_t blksz = hdr->b_size; 30298636SMark.Maybee@Sun.COM uint64_t spa = hdr->b_spa; 30303290Sjohansen arc_buf_contents_t type = hdr->b_type; 30315450Sbrendan uint32_t flags = hdr->b_flags; 3032789Sahrens 30337545SMark.Maybee@Sun.COM ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3034789Sahrens /* 3035789Sahrens * Pull the data off of this buf and attach it to 3036789Sahrens * a new anonymous buf. 3037789Sahrens */ 30381544Seschrock (void) remove_reference(hdr, hash_lock, tag); 3039789Sahrens bufp = &hdr->b_buf; 30401544Seschrock while (*bufp != buf) 3041789Sahrens bufp = &(*bufp)->b_next; 3042789Sahrens *bufp = (*bufp)->b_next; 30433897Smaybee buf->b_next = NULL; 30441544Seschrock 30453403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 30463403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 30471544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 30484309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 30494309Smaybee ASSERT3U(*size, >=, hdr->b_size); 30504309Smaybee atomic_add_64(size, -hdr->b_size); 30511544Seschrock } 30521544Seschrock hdr->b_datacnt -= 1; 30533547Smaybee arc_cksum_verify(buf); 30541544Seschrock 3055789Sahrens mutex_exit(hash_lock); 3056789Sahrens 30576245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3058789Sahrens nhdr->b_size = blksz; 3059789Sahrens nhdr->b_spa = spa; 30603290Sjohansen nhdr->b_type = type; 3061789Sahrens nhdr->b_buf = buf; 30623403Sbmc nhdr->b_state = arc_anon; 3063789Sahrens nhdr->b_arc_access = 0; 30645450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 30655450Sbrendan nhdr->b_l2hdr = NULL; 30661544Seschrock nhdr->b_datacnt = 1; 30673547Smaybee nhdr->b_freeze_cksum = NULL; 30683897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 3069789Sahrens buf->b_hdr = nhdr; 30707545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30713403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 3072789Sahrens } else { 30737545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30741544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3075789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 3076789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 30773403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 3078789Sahrens hdr->b_arc_access = 0; 3079789Sahrens mutex_exit(hash_lock); 30805450Sbrendan 3081789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 3082789Sahrens hdr->b_birth = 0; 3083789Sahrens hdr->b_cksum0 = 0; 30843547Smaybee arc_buf_thaw(buf); 3085789Sahrens } 30861544Seschrock buf->b_efunc = NULL; 30871544Seschrock buf->b_private = NULL; 30885450Sbrendan 30899274SBrendan.Gregg@Sun.COM out: 30905450Sbrendan if (l2hdr) { 30915450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 30925450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 30935450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 30947545SMark.Maybee@Sun.COM mutex_exit(&l2arc_buflist_mtx); 30955450Sbrendan } 3096789Sahrens } 3097789Sahrens 3098789Sahrens int 3099789Sahrens arc_released(arc_buf_t *buf) 3100789Sahrens { 31017545SMark.Maybee@Sun.COM int released; 31027545SMark.Maybee@Sun.COM 31037545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 31047545SMark.Maybee@Sun.COM released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 31057545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 31067545SMark.Maybee@Sun.COM return (released); 31071544Seschrock } 31081544Seschrock 31091544Seschrock int 31101544Seschrock arc_has_callback(arc_buf_t *buf) 31111544Seschrock { 31127545SMark.Maybee@Sun.COM int callback; 31137545SMark.Maybee@Sun.COM 31147545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 31157545SMark.Maybee@Sun.COM callback = (buf->b_efunc != NULL); 31167545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 31177545SMark.Maybee@Sun.COM return (callback); 3118789Sahrens } 3119789Sahrens 31201544Seschrock #ifdef ZFS_DEBUG 31211544Seschrock int 31221544Seschrock arc_referenced(arc_buf_t *buf) 31231544Seschrock { 31247545SMark.Maybee@Sun.COM int referenced; 31257545SMark.Maybee@Sun.COM 31267545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 31277545SMark.Maybee@Sun.COM referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 31287545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 31297545SMark.Maybee@Sun.COM return (referenced); 31301544Seschrock } 31311544Seschrock #endif 31321544Seschrock 3133789Sahrens static void 31343547Smaybee arc_write_ready(zio_t *zio) 31353547Smaybee { 31363547Smaybee arc_write_callback_t *callback = zio->io_private; 31373547Smaybee arc_buf_t *buf = callback->awcb_buf; 31385329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 31395329Sgw25295 31407754SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 31417754SJeff.Bonwick@Sun.COM callback->awcb_ready(zio, buf, callback->awcb_private); 31427754SJeff.Bonwick@Sun.COM 31435329Sgw25295 /* 31445329Sgw25295 * If the IO is already in progress, then this is a re-write 31457754SJeff.Bonwick@Sun.COM * attempt, so we need to thaw and re-compute the cksum. 31467754SJeff.Bonwick@Sun.COM * It is the responsibility of the callback to handle the 31477754SJeff.Bonwick@Sun.COM * accounting for any re-write attempt. 31485329Sgw25295 */ 31495329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 31505329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 31515329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 31525329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 31535329Sgw25295 hdr->b_freeze_cksum = NULL; 31545329Sgw25295 } 31555329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 31565329Sgw25295 } 31575450Sbrendan arc_cksum_compute(buf, B_FALSE); 31585329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 31593547Smaybee } 31603547Smaybee 31613547Smaybee static void 3162789Sahrens arc_write_done(zio_t *zio) 3163789Sahrens { 31643547Smaybee arc_write_callback_t *callback = zio->io_private; 31653547Smaybee arc_buf_t *buf = callback->awcb_buf; 31663547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3167789Sahrens 316810922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_acb == NULL); 316910922SJeff.Bonwick@Sun.COM 317010922SJeff.Bonwick@Sun.COM if (zio->io_error == 0) { 317110922SJeff.Bonwick@Sun.COM hdr->b_dva = *BP_IDENTITY(zio->io_bp); 317210922SJeff.Bonwick@Sun.COM hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 317310922SJeff.Bonwick@Sun.COM hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 317410922SJeff.Bonwick@Sun.COM } else { 317510922SJeff.Bonwick@Sun.COM ASSERT(BUF_EMPTY(hdr)); 317610922SJeff.Bonwick@Sun.COM } 317710922SJeff.Bonwick@Sun.COM 31781544Seschrock /* 31791544Seschrock * If the block to be written was all-zero, we may have 31801544Seschrock * compressed it away. In this case no write was performed 31811544Seschrock * so there will be no dva/birth-date/checksum. The buffer 31821544Seschrock * must therefor remain anonymous (and uncached). 31831544Seschrock */ 3184789Sahrens if (!BUF_EMPTY(hdr)) { 3185789Sahrens arc_buf_hdr_t *exists; 3186789Sahrens kmutex_t *hash_lock; 3187789Sahrens 318810922SJeff.Bonwick@Sun.COM ASSERT(zio->io_error == 0); 318910922SJeff.Bonwick@Sun.COM 31903093Sahrens arc_cksum_verify(buf); 31913093Sahrens 3192789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3193789Sahrens if (exists) { 3194789Sahrens /* 3195789Sahrens * This can only happen if we overwrite for 3196789Sahrens * sync-to-convergence, because we remove 3197789Sahrens * buffers from the hash table when we arc_free(). 3198789Sahrens */ 319910922SJeff.Bonwick@Sun.COM if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 320010922SJeff.Bonwick@Sun.COM if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 320110922SJeff.Bonwick@Sun.COM panic("bad overwrite, hdr=%p exists=%p", 320210922SJeff.Bonwick@Sun.COM (void *)hdr, (void *)exists); 320310922SJeff.Bonwick@Sun.COM ASSERT(refcount_is_zero(&exists->b_refcnt)); 320410922SJeff.Bonwick@Sun.COM arc_change_state(arc_anon, exists, hash_lock); 320510922SJeff.Bonwick@Sun.COM mutex_exit(hash_lock); 320610922SJeff.Bonwick@Sun.COM arc_hdr_destroy(exists); 320710922SJeff.Bonwick@Sun.COM exists = buf_hash_insert(hdr, &hash_lock); 320810922SJeff.Bonwick@Sun.COM ASSERT3P(exists, ==, NULL); 320910922SJeff.Bonwick@Sun.COM } else { 321010922SJeff.Bonwick@Sun.COM /* Dedup */ 321110922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 321210922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_state == arc_anon); 321310922SJeff.Bonwick@Sun.COM ASSERT(BP_GET_DEDUP(zio->io_bp)); 321410922SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 321510272SMatthew.Ahrens@Sun.COM } 3216789Sahrens } 32171544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 32187046Sahrens /* if it's not anon, we are doing a scrub */ 321910922SJeff.Bonwick@Sun.COM if (!exists && hdr->b_state == arc_anon) 32207046Sahrens arc_access(hdr, hash_lock); 32212688Smaybee mutex_exit(hash_lock); 32221544Seschrock } else { 32231544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3224789Sahrens } 322510922SJeff.Bonwick@Sun.COM 322610922SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 322710922SJeff.Bonwick@Sun.COM callback->awcb_done(zio, buf, callback->awcb_private); 3228789Sahrens 32293547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3230789Sahrens } 3231789Sahrens 32323547Smaybee zio_t * 323310922SJeff.Bonwick@Sun.COM arc_write(zio_t *pio, spa_t *spa, uint64_t txg, 323410922SJeff.Bonwick@Sun.COM blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp, 323510922SJeff.Bonwick@Sun.COM arc_done_func_t *ready, arc_done_func_t *done, void *private, 323610922SJeff.Bonwick@Sun.COM int priority, int zio_flags, const zbookmark_t *zb) 3237789Sahrens { 3238789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 32393547Smaybee arc_write_callback_t *callback; 32407754SJeff.Bonwick@Sun.COM zio_t *zio; 32417754SJeff.Bonwick@Sun.COM 32427754SJeff.Bonwick@Sun.COM ASSERT(ready != NULL); 324310922SJeff.Bonwick@Sun.COM ASSERT(done != NULL); 3244789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 32452237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 324610922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_acb == NULL); 32477237Sek110237 if (l2arc) 32487237Sek110237 hdr->b_flags |= ARC_L2CACHE; 32493547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 32503547Smaybee callback->awcb_ready = ready; 32513547Smaybee callback->awcb_done = done; 32523547Smaybee callback->awcb_private = private; 32533547Smaybee callback->awcb_buf = buf; 32547046Sahrens 325510922SJeff.Bonwick@Sun.COM zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 32567754SJeff.Bonwick@Sun.COM arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3257789Sahrens 32583547Smaybee return (zio); 3259789Sahrens } 3260789Sahrens 326110922SJeff.Bonwick@Sun.COM void 326210922SJeff.Bonwick@Sun.COM arc_free(spa_t *spa, const blkptr_t *bp) 3263789Sahrens { 3264789Sahrens arc_buf_hdr_t *ab; 3265789Sahrens kmutex_t *hash_lock; 32668636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 3267789Sahrens 3268789Sahrens /* 326910922SJeff.Bonwick@Sun.COM * If this buffer is in the cache, release it, so it can be re-used. 3270789Sahrens */ 327110922SJeff.Bonwick@Sun.COM ab = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 327210922SJeff.Bonwick@Sun.COM &hash_lock); 3273789Sahrens if (ab != NULL) { 32743403Sbmc if (ab->b_state != arc_anon) 32753403Sbmc arc_change_state(arc_anon, ab, hash_lock); 32762391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 32772391Smaybee /* 32782391Smaybee * This should only happen when we prefetch. 32792391Smaybee */ 32802391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 32812391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 32822391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 32832391Smaybee if (HDR_IN_HASH_TABLE(ab)) 32842391Smaybee buf_hash_remove(ab); 32852391Smaybee ab->b_arc_access = 0; 32862391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 32872391Smaybee ab->b_birth = 0; 32882391Smaybee ab->b_cksum0 = 0; 32892391Smaybee ab->b_buf->b_efunc = NULL; 32902391Smaybee ab->b_buf->b_private = NULL; 32912391Smaybee mutex_exit(hash_lock); 329210922SJeff.Bonwick@Sun.COM } else { 329310922SJeff.Bonwick@Sun.COM ASSERT(refcount_is_zero(&ab->b_refcnt)); 32945450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3295789Sahrens mutex_exit(hash_lock); 32961544Seschrock arc_hdr_destroy(ab); 32973403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3298789Sahrens } 3299789Sahrens } 3300789Sahrens } 3301789Sahrens 33026245Smaybee static int 33039412SAleksandr.Guzovskiy@Sun.COM arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg) 33046245Smaybee { 33056245Smaybee #ifdef _KERNEL 33066245Smaybee uint64_t available_memory = ptob(freemem); 33076245Smaybee static uint64_t page_load = 0; 33086245Smaybee static uint64_t last_txg = 0; 33096245Smaybee 33106245Smaybee #if defined(__i386) 33116245Smaybee available_memory = 33126245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 33136245Smaybee #endif 33146245Smaybee if (available_memory >= zfs_write_limit_max) 33156245Smaybee return (0); 33166245Smaybee 33176245Smaybee if (txg > last_txg) { 33186245Smaybee last_txg = txg; 33196245Smaybee page_load = 0; 33206245Smaybee } 33216245Smaybee /* 33226245Smaybee * If we are in pageout, we know that memory is already tight, 33236245Smaybee * the arc is already going to be evicting, so we just want to 33246245Smaybee * continue to let page writes occur as quickly as possible. 33256245Smaybee */ 33266245Smaybee if (curproc == proc_pageout) { 33276245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 33286245Smaybee return (ERESTART); 33296245Smaybee /* Note: reserve is inflated, so we deflate */ 33306245Smaybee page_load += reserve / 8; 33316245Smaybee return (0); 33326245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 33336245Smaybee /* memory is low, delay before restarting */ 33346245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33356245Smaybee return (EAGAIN); 33366245Smaybee } 33376245Smaybee page_load = 0; 33386245Smaybee 33396245Smaybee if (arc_size > arc_c_min) { 33406245Smaybee uint64_t evictable_memory = 33416245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 33426245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 33436245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 33446245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 33456245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 33466245Smaybee } 33476245Smaybee 33486245Smaybee if (inflight_data > available_memory / 4) { 33496245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33506245Smaybee return (ERESTART); 33516245Smaybee } 33526245Smaybee #endif 33536245Smaybee return (0); 33546245Smaybee } 33556245Smaybee 3356789Sahrens void 33576245Smaybee arc_tempreserve_clear(uint64_t reserve) 3358789Sahrens { 33596245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3360789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3361789Sahrens } 3362789Sahrens 3363789Sahrens int 33646245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3365789Sahrens { 33666245Smaybee int error; 33679412SAleksandr.Guzovskiy@Sun.COM uint64_t anon_size; 33686245Smaybee 3369789Sahrens #ifdef ZFS_DEBUG 3370789Sahrens /* 3371789Sahrens * Once in a while, fail for no reason. Everything should cope. 3372789Sahrens */ 3373789Sahrens if (spa_get_random(10000) == 0) { 3374789Sahrens dprintf("forcing random failure\n"); 3375789Sahrens return (ERESTART); 3376789Sahrens } 3377789Sahrens #endif 33786245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 33796245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 33806245Smaybee if (reserve > arc_c) 3381982Smaybee return (ENOMEM); 3382982Smaybee 3383789Sahrens /* 33849412SAleksandr.Guzovskiy@Sun.COM * Don't count loaned bufs as in flight dirty data to prevent long 33859412SAleksandr.Guzovskiy@Sun.COM * network delays from blocking transactions that are ready to be 33869412SAleksandr.Guzovskiy@Sun.COM * assigned to a txg. 33879412SAleksandr.Guzovskiy@Sun.COM */ 33889412SAleksandr.Guzovskiy@Sun.COM anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 33899412SAleksandr.Guzovskiy@Sun.COM 33909412SAleksandr.Guzovskiy@Sun.COM /* 33916245Smaybee * Writes will, almost always, require additional memory allocations 33926245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 33936245Smaybee * make sure that there is sufficient available memory for this. 33946245Smaybee */ 33959412SAleksandr.Guzovskiy@Sun.COM if (error = arc_memory_throttle(reserve, anon_size, txg)) 33966245Smaybee return (error); 33976245Smaybee 33986245Smaybee /* 3399982Smaybee * Throttle writes when the amount of dirty data in the cache 3400982Smaybee * gets too large. We try to keep the cache less than half full 3401982Smaybee * of dirty blocks so that our sync times don't grow too large. 3402982Smaybee * Note: if two requests come in concurrently, we might let them 3403982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3404789Sahrens */ 34059412SAleksandr.Guzovskiy@Sun.COM 34069412SAleksandr.Guzovskiy@Sun.COM if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 34079412SAleksandr.Guzovskiy@Sun.COM anon_size > arc_c / 4) { 34084309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 34094309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 34104309Smaybee arc_tempreserve>>10, 34114309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 34124309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 34136245Smaybee reserve>>10, arc_c>>10); 3414789Sahrens return (ERESTART); 3415789Sahrens } 34166245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3417789Sahrens return (0); 3418789Sahrens } 3419789Sahrens 3420789Sahrens void 3421789Sahrens arc_init(void) 3422789Sahrens { 3423789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3424789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3425789Sahrens 34262391Smaybee /* Convert seconds to clock ticks */ 34272638Sperrin arc_min_prefetch_lifespan = 1 * hz; 34282391Smaybee 3429789Sahrens /* Start out with 1/8 of all memory */ 34303403Sbmc arc_c = physmem * PAGESIZE / 8; 3431789Sahrens 3432789Sahrens #ifdef _KERNEL 3433789Sahrens /* 3434789Sahrens * On architectures where the physical memory can be larger 3435789Sahrens * than the addressable space (intel in 32-bit mode), we may 3436789Sahrens * need to limit the cache to 1/8 of VM size. 3437789Sahrens */ 34383403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3439789Sahrens #endif 3440789Sahrens 3441982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 34423403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3443982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 34443403Sbmc if (arc_c * 8 >= 1<<30) 34453403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3446789Sahrens else 34473403Sbmc arc_c_max = arc_c_min; 34483403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 34492885Sahrens 34502885Sahrens /* 34512885Sahrens * Allow the tunables to override our calculations if they are 34522885Sahrens * reasonable (ie. over 64MB) 34532885Sahrens */ 34542885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 34553403Sbmc arc_c_max = zfs_arc_max; 34563403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 34573403Sbmc arc_c_min = zfs_arc_min; 34582885Sahrens 34593403Sbmc arc_c = arc_c_max; 34603403Sbmc arc_p = (arc_c >> 1); 3461789Sahrens 34624309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 34634309Smaybee arc_meta_limit = arc_c_max / 4; 34644645Sek110237 34654645Sek110237 /* Allow the tunable to override if it is reasonable */ 34664645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 34674645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 34684645Sek110237 34694309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 34704309Smaybee arc_c_min = arc_meta_limit / 2; 34714309Smaybee 34728582SBrendan.Gregg@Sun.COM if (zfs_arc_grow_retry > 0) 34738582SBrendan.Gregg@Sun.COM arc_grow_retry = zfs_arc_grow_retry; 34748582SBrendan.Gregg@Sun.COM 34758582SBrendan.Gregg@Sun.COM if (zfs_arc_shrink_shift > 0) 34768582SBrendan.Gregg@Sun.COM arc_shrink_shift = zfs_arc_shrink_shift; 34778582SBrendan.Gregg@Sun.COM 34788582SBrendan.Gregg@Sun.COM if (zfs_arc_p_min_shift > 0) 34798582SBrendan.Gregg@Sun.COM arc_p_min_shift = zfs_arc_p_min_shift; 34808582SBrendan.Gregg@Sun.COM 3481789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3482789Sahrens if (kmem_debugging()) 34833403Sbmc arc_c = arc_c / 2; 34843403Sbmc if (arc_c < arc_c_min) 34853403Sbmc arc_c = arc_c_min; 3486789Sahrens 34873403Sbmc arc_anon = &ARC_anon; 34883403Sbmc arc_mru = &ARC_mru; 34893403Sbmc arc_mru_ghost = &ARC_mru_ghost; 34903403Sbmc arc_mfu = &ARC_mfu; 34913403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 34925450Sbrendan arc_l2c_only = &ARC_l2c_only; 34933403Sbmc arc_size = 0; 3494789Sahrens 34953403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34963403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34973403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34983403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34993403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35005450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35012688Smaybee 35024309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 35034309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35044309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 35054309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35064309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 35074309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35084309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 35094309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35104309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 35114309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35124309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 35134309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35144309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 35154309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35164309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 35174309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35185450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 35195450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35205450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 35215450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3522789Sahrens 3523789Sahrens buf_init(); 3524789Sahrens 3525789Sahrens arc_thread_exit = 0; 35261544Seschrock arc_eviction_list = NULL; 35271544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 35282887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3529789Sahrens 35303403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 35313403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 35323403Sbmc 35333403Sbmc if (arc_ksp != NULL) { 35343403Sbmc arc_ksp->ks_data = &arc_stats; 35353403Sbmc kstat_install(arc_ksp); 35363403Sbmc } 35373403Sbmc 3538789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3539789Sahrens TS_RUN, minclsyspri); 35403158Smaybee 35413158Smaybee arc_dead = FALSE; 35426987Sbrendan arc_warm = B_FALSE; 35436245Smaybee 35446245Smaybee if (zfs_write_limit_max == 0) 35457468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 35466245Smaybee else 35476245Smaybee zfs_write_limit_shift = 0; 35487468SMark.Maybee@Sun.COM mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3549789Sahrens } 3550789Sahrens 3551789Sahrens void 3552789Sahrens arc_fini(void) 3553789Sahrens { 3554789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3555789Sahrens arc_thread_exit = 1; 3556789Sahrens while (arc_thread_exit != 0) 3557789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3558789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3559789Sahrens 35605642Smaybee arc_flush(NULL); 3561789Sahrens 3562789Sahrens arc_dead = TRUE; 3563789Sahrens 35643403Sbmc if (arc_ksp != NULL) { 35653403Sbmc kstat_delete(arc_ksp); 35663403Sbmc arc_ksp = NULL; 35673403Sbmc } 35683403Sbmc 35691544Seschrock mutex_destroy(&arc_eviction_mtx); 3570789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3571789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3572789Sahrens 35734309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 35744309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 35754309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 35764309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 35774309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 35784309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 35794309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 35804309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3581789Sahrens 35823403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 35833403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 35843403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 35853403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 35863403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 35878214SRicardo.M.Correia@Sun.COM mutex_destroy(&arc_l2c_only->arcs_mtx); 35882856Snd150628 35897468SMark.Maybee@Sun.COM mutex_destroy(&zfs_write_limit_lock); 35907468SMark.Maybee@Sun.COM 3591789Sahrens buf_fini(); 35929412SAleksandr.Guzovskiy@Sun.COM 35939412SAleksandr.Guzovskiy@Sun.COM ASSERT(arc_loaned_bytes == 0); 3594789Sahrens } 35955450Sbrendan 35965450Sbrendan /* 35975450Sbrendan * Level 2 ARC 35985450Sbrendan * 35995450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 36005450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 36015450Sbrendan * using large infrequent writes. The main role of this cache is to boost 36025450Sbrendan * the performance of random read workloads. The intended L2ARC devices 36035450Sbrendan * include short-stroked disks, solid state disks, and other media with 36045450Sbrendan * substantially faster read latency than disk. 36055450Sbrendan * 36065450Sbrendan * +-----------------------+ 36075450Sbrendan * | ARC | 36085450Sbrendan * +-----------------------+ 36095450Sbrendan * | ^ ^ 36105450Sbrendan * | | | 36115450Sbrendan * l2arc_feed_thread() arc_read() 36125450Sbrendan * | | | 36135450Sbrendan * | l2arc read | 36145450Sbrendan * V | | 36155450Sbrendan * +---------------+ | 36165450Sbrendan * | L2ARC | | 36175450Sbrendan * +---------------+ | 36185450Sbrendan * | ^ | 36195450Sbrendan * l2arc_write() | | 36205450Sbrendan * | | | 36215450Sbrendan * V | | 36225450Sbrendan * +-------+ +-------+ 36235450Sbrendan * | vdev | | vdev | 36245450Sbrendan * | cache | | cache | 36255450Sbrendan * +-------+ +-------+ 36265450Sbrendan * +=========+ .-----. 36275450Sbrendan * : L2ARC : |-_____-| 36285450Sbrendan * : devices : | Disks | 36295450Sbrendan * +=========+ `-_____-' 36305450Sbrendan * 36315450Sbrendan * Read requests are satisfied from the following sources, in order: 36325450Sbrendan * 36335450Sbrendan * 1) ARC 36345450Sbrendan * 2) vdev cache of L2ARC devices 36355450Sbrendan * 3) L2ARC devices 36365450Sbrendan * 4) vdev cache of disks 36375450Sbrendan * 5) disks 36385450Sbrendan * 36395450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 36405450Sbrendan * To accommodate for this there are some significant differences between 36415450Sbrendan * the L2ARC and traditional cache design: 36425450Sbrendan * 36435450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 36445450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 36455450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 36465450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 36475450Sbrendan * 36485450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 36495450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 36505450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 36515450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 36525450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 36535450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 36545450Sbrendan * provide a better sense of ratio than this diagram: 36555450Sbrendan * 36565450Sbrendan * head --> tail 36575450Sbrendan * +---------------------+----------+ 36585450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 36595450Sbrendan * +---------------------+----------+ | o L2ARC eligible 36605450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 36615450Sbrendan * +---------------------+----------+ | 36625450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 36635450Sbrendan * headroom | 36645450Sbrendan * l2arc_feed_thread() 36655450Sbrendan * | 36665450Sbrendan * l2arc write hand <--[oooo]--' 36675450Sbrendan * | 8 Mbyte 36685450Sbrendan * | write max 36695450Sbrendan * V 36705450Sbrendan * +==============================+ 36715450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 36725450Sbrendan * +==============================+ 36735450Sbrendan * 32 Gbytes 36745450Sbrendan * 36755450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 36765450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 36775450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 36785450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 36795450Sbrendan * the ARC lists have moved there due to inactivity. 36805450Sbrendan * 36815450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 36825450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 36835450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 36845450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 36855450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 36865450Sbrendan * quickly, such as during backups of the entire pool. 36875450Sbrendan * 36886987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 36896987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 36906987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 36916987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 36926987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 36936987Sbrendan * 36946987Sbrendan * The L2ARC device write speed is also boosted during this time so that 36956987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 36966987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 36976987Sbrendan * through increased writes. 36986987Sbrendan * 36996987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 37005450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 37015450Sbrendan * device is written to in a rotor fashion, sweeping writes through 37025450Sbrendan * available space then repeating. 37035450Sbrendan * 37046987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 37055450Sbrendan * write buffers back to disk based storage. 37065450Sbrendan * 37076987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 37085450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 37095450Sbrendan * 37105450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 37115450Sbrendan * may be necessary for different workloads: 37125450Sbrendan * 37135450Sbrendan * l2arc_write_max max write bytes per interval 37146987Sbrendan * l2arc_write_boost extra write bytes during device warmup 37155450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 37165450Sbrendan * l2arc_headroom number of max device writes to precache 37175450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 37185450Sbrendan * 37195450Sbrendan * Tunables may be removed or added as future performance improvements are 37205450Sbrendan * integrated, and also may become zpool properties. 37218582SBrendan.Gregg@Sun.COM * 37228582SBrendan.Gregg@Sun.COM * There are three key functions that control how the L2ARC warms up: 37238582SBrendan.Gregg@Sun.COM * 37248582SBrendan.Gregg@Sun.COM * l2arc_write_eligible() check if a buffer is eligible to cache 37258582SBrendan.Gregg@Sun.COM * l2arc_write_size() calculate how much to write 37268582SBrendan.Gregg@Sun.COM * l2arc_write_interval() calculate sleep delay between writes 37278582SBrendan.Gregg@Sun.COM * 37288582SBrendan.Gregg@Sun.COM * These three functions determine what to write, how much, and how quickly 37298582SBrendan.Gregg@Sun.COM * to send writes. 37305450Sbrendan */ 37315450Sbrendan 37328582SBrendan.Gregg@Sun.COM static boolean_t 37338636SMark.Maybee@Sun.COM l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 37348582SBrendan.Gregg@Sun.COM { 37358582SBrendan.Gregg@Sun.COM /* 37368582SBrendan.Gregg@Sun.COM * A buffer is *not* eligible for the L2ARC if it: 37378582SBrendan.Gregg@Sun.COM * 1. belongs to a different spa. 373810357SBrendan.Gregg@Sun.COM * 2. is already cached on the L2ARC. 373910357SBrendan.Gregg@Sun.COM * 3. has an I/O in progress (it may be an incomplete read). 374010357SBrendan.Gregg@Sun.COM * 4. is flagged not eligible (zfs property). 37418582SBrendan.Gregg@Sun.COM */ 374210357SBrendan.Gregg@Sun.COM if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL || 37438582SBrendan.Gregg@Sun.COM HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) 37448582SBrendan.Gregg@Sun.COM return (B_FALSE); 37458582SBrendan.Gregg@Sun.COM 37468582SBrendan.Gregg@Sun.COM return (B_TRUE); 37478582SBrendan.Gregg@Sun.COM } 37488582SBrendan.Gregg@Sun.COM 37498582SBrendan.Gregg@Sun.COM static uint64_t 37508582SBrendan.Gregg@Sun.COM l2arc_write_size(l2arc_dev_t *dev) 37518582SBrendan.Gregg@Sun.COM { 37528582SBrendan.Gregg@Sun.COM uint64_t size; 37538582SBrendan.Gregg@Sun.COM 37548582SBrendan.Gregg@Sun.COM size = dev->l2ad_write; 37558582SBrendan.Gregg@Sun.COM 37568582SBrendan.Gregg@Sun.COM if (arc_warm == B_FALSE) 37578582SBrendan.Gregg@Sun.COM size += dev->l2ad_boost; 37588582SBrendan.Gregg@Sun.COM 37598582SBrendan.Gregg@Sun.COM return (size); 37608582SBrendan.Gregg@Sun.COM 37618582SBrendan.Gregg@Sun.COM } 37628582SBrendan.Gregg@Sun.COM 37638582SBrendan.Gregg@Sun.COM static clock_t 37648582SBrendan.Gregg@Sun.COM l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 37658582SBrendan.Gregg@Sun.COM { 376611066Srafael.vanoni@sun.com clock_t interval, next, now; 37678582SBrendan.Gregg@Sun.COM 37688582SBrendan.Gregg@Sun.COM /* 37698582SBrendan.Gregg@Sun.COM * If the ARC lists are busy, increase our write rate; if the 37708582SBrendan.Gregg@Sun.COM * lists are stale, idle back. This is achieved by checking 37718582SBrendan.Gregg@Sun.COM * how much we previously wrote - if it was more than half of 37728582SBrendan.Gregg@Sun.COM * what we wanted, schedule the next write much sooner. 37738582SBrendan.Gregg@Sun.COM */ 37748582SBrendan.Gregg@Sun.COM if (l2arc_feed_again && wrote > (wanted / 2)) 37758582SBrendan.Gregg@Sun.COM interval = (hz * l2arc_feed_min_ms) / 1000; 37768582SBrendan.Gregg@Sun.COM else 37778582SBrendan.Gregg@Sun.COM interval = hz * l2arc_feed_secs; 37788582SBrendan.Gregg@Sun.COM 377911066Srafael.vanoni@sun.com now = ddi_get_lbolt(); 378011066Srafael.vanoni@sun.com next = MAX(now, MIN(now + interval, began + interval)); 37818582SBrendan.Gregg@Sun.COM 37828582SBrendan.Gregg@Sun.COM return (next); 37838582SBrendan.Gregg@Sun.COM } 37848582SBrendan.Gregg@Sun.COM 37855450Sbrendan static void 37865450Sbrendan l2arc_hdr_stat_add(void) 37875450Sbrendan { 37886018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 37896018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 37905450Sbrendan } 37915450Sbrendan 37925450Sbrendan static void 37935450Sbrendan l2arc_hdr_stat_remove(void) 37945450Sbrendan { 37956018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 37966018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 37975450Sbrendan } 37985450Sbrendan 37995450Sbrendan /* 38005450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 38016987Sbrendan * If a device is returned, this also returns holding the spa config lock. 38025450Sbrendan */ 38035450Sbrendan static l2arc_dev_t * 38045450Sbrendan l2arc_dev_get_next(void) 38055450Sbrendan { 38066987Sbrendan l2arc_dev_t *first, *next = NULL; 38076987Sbrendan 38086987Sbrendan /* 38096987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 38106987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 38116987Sbrendan * both locks will be dropped and a spa config lock held instead. 38126987Sbrendan */ 38136987Sbrendan mutex_enter(&spa_namespace_lock); 38146987Sbrendan mutex_enter(&l2arc_dev_mtx); 38156643Seschrock 38166643Seschrock /* if there are no vdevs, there is nothing to do */ 38176643Seschrock if (l2arc_ndev == 0) 38186987Sbrendan goto out; 38196643Seschrock 38206643Seschrock first = NULL; 38216643Seschrock next = l2arc_dev_last; 38226643Seschrock do { 38236643Seschrock /* loop around the list looking for a non-faulted vdev */ 38246643Seschrock if (next == NULL) { 38255450Sbrendan next = list_head(l2arc_dev_list); 38266643Seschrock } else { 38276643Seschrock next = list_next(l2arc_dev_list, next); 38286643Seschrock if (next == NULL) 38296643Seschrock next = list_head(l2arc_dev_list); 38306643Seschrock } 38316643Seschrock 38326643Seschrock /* if we have come back to the start, bail out */ 38336643Seschrock if (first == NULL) 38346643Seschrock first = next; 38356643Seschrock else if (next == first) 38366643Seschrock break; 38376643Seschrock 38386643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 38396643Seschrock 38406643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 38416643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 38426987Sbrendan next = NULL; 38435450Sbrendan 38445450Sbrendan l2arc_dev_last = next; 38455450Sbrendan 38466987Sbrendan out: 38476987Sbrendan mutex_exit(&l2arc_dev_mtx); 38486987Sbrendan 38496987Sbrendan /* 38506987Sbrendan * Grab the config lock to prevent the 'next' device from being 38516987Sbrendan * removed while we are writing to it. 38526987Sbrendan */ 38536987Sbrendan if (next != NULL) 38547754SJeff.Bonwick@Sun.COM spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 38556987Sbrendan mutex_exit(&spa_namespace_lock); 38566987Sbrendan 38575450Sbrendan return (next); 38585450Sbrendan } 38595450Sbrendan 38605450Sbrendan /* 38616987Sbrendan * Free buffers that were tagged for destruction. 38626987Sbrendan */ 38636987Sbrendan static void 38646987Sbrendan l2arc_do_free_on_write() 38656987Sbrendan { 38666987Sbrendan list_t *buflist; 38676987Sbrendan l2arc_data_free_t *df, *df_prev; 38686987Sbrendan 38696987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 38706987Sbrendan buflist = l2arc_free_on_write; 38716987Sbrendan 38726987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 38736987Sbrendan df_prev = list_prev(buflist, df); 38746987Sbrendan ASSERT(df->l2df_data != NULL); 38756987Sbrendan ASSERT(df->l2df_func != NULL); 38766987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 38776987Sbrendan list_remove(buflist, df); 38786987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 38796987Sbrendan } 38806987Sbrendan 38816987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 38826987Sbrendan } 38836987Sbrendan 38846987Sbrendan /* 38855450Sbrendan * A write to a cache device has completed. Update all headers to allow 38865450Sbrendan * reads from these buffers to begin. 38875450Sbrendan */ 38885450Sbrendan static void 38895450Sbrendan l2arc_write_done(zio_t *zio) 38905450Sbrendan { 38915450Sbrendan l2arc_write_callback_t *cb; 38925450Sbrendan l2arc_dev_t *dev; 38935450Sbrendan list_t *buflist; 38945450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 38956987Sbrendan l2arc_buf_hdr_t *abl2; 38965450Sbrendan kmutex_t *hash_lock; 38975450Sbrendan 38985450Sbrendan cb = zio->io_private; 38995450Sbrendan ASSERT(cb != NULL); 39005450Sbrendan dev = cb->l2wcb_dev; 39015450Sbrendan ASSERT(dev != NULL); 39025450Sbrendan head = cb->l2wcb_head; 39035450Sbrendan ASSERT(head != NULL); 39045450Sbrendan buflist = dev->l2ad_buflist; 39055450Sbrendan ASSERT(buflist != NULL); 39065450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 39075450Sbrendan l2arc_write_callback_t *, cb); 39085450Sbrendan 39095450Sbrendan if (zio->io_error != 0) 39105450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 39115450Sbrendan 39125450Sbrendan mutex_enter(&l2arc_buflist_mtx); 39135450Sbrendan 39145450Sbrendan /* 39155450Sbrendan * All writes completed, or an error was hit. 39165450Sbrendan */ 39175450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 39185450Sbrendan ab_prev = list_prev(buflist, ab); 39195450Sbrendan 39205450Sbrendan hash_lock = HDR_LOCK(ab); 39215450Sbrendan if (!mutex_tryenter(hash_lock)) { 39225450Sbrendan /* 39235450Sbrendan * This buffer misses out. It may be in a stage 39245450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 39255450Sbrendan * left set, denying reads to this buffer. 39265450Sbrendan */ 39275450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 39285450Sbrendan continue; 39295450Sbrendan } 39305450Sbrendan 39315450Sbrendan if (zio->io_error != 0) { 39325450Sbrendan /* 39336987Sbrendan * Error - drop L2ARC entry. 39345450Sbrendan */ 39356987Sbrendan list_remove(buflist, ab); 39366987Sbrendan abl2 = ab->b_l2hdr; 39375450Sbrendan ab->b_l2hdr = NULL; 39386987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 39396987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 39405450Sbrendan } 39415450Sbrendan 39425450Sbrendan /* 39435450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 39445450Sbrendan */ 39455450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 39465450Sbrendan 39475450Sbrendan mutex_exit(hash_lock); 39485450Sbrendan } 39495450Sbrendan 39505450Sbrendan atomic_inc_64(&l2arc_writes_done); 39515450Sbrendan list_remove(buflist, head); 39525450Sbrendan kmem_cache_free(hdr_cache, head); 39535450Sbrendan mutex_exit(&l2arc_buflist_mtx); 39545450Sbrendan 39556987Sbrendan l2arc_do_free_on_write(); 39565450Sbrendan 39575450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 39585450Sbrendan } 39595450Sbrendan 39605450Sbrendan /* 39615450Sbrendan * A read to a cache device completed. Validate buffer contents before 39625450Sbrendan * handing over to the regular ARC routines. 39635450Sbrendan */ 39645450Sbrendan static void 39655450Sbrendan l2arc_read_done(zio_t *zio) 39665450Sbrendan { 39675450Sbrendan l2arc_read_callback_t *cb; 39685450Sbrendan arc_buf_hdr_t *hdr; 39695450Sbrendan arc_buf_t *buf; 39705450Sbrendan kmutex_t *hash_lock; 39716987Sbrendan int equal; 39725450Sbrendan 39737754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd != NULL); 39747754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 39757754SJeff.Bonwick@Sun.COM 39767754SJeff.Bonwick@Sun.COM spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 39777754SJeff.Bonwick@Sun.COM 39785450Sbrendan cb = zio->io_private; 39795450Sbrendan ASSERT(cb != NULL); 39805450Sbrendan buf = cb->l2rcb_buf; 39815450Sbrendan ASSERT(buf != NULL); 39825450Sbrendan hdr = buf->b_hdr; 39835450Sbrendan ASSERT(hdr != NULL); 39845450Sbrendan 39855450Sbrendan hash_lock = HDR_LOCK(hdr); 39865450Sbrendan mutex_enter(hash_lock); 39875450Sbrendan 39885450Sbrendan /* 39895450Sbrendan * Check this survived the L2ARC journey. 39905450Sbrendan */ 39915450Sbrendan equal = arc_cksum_equal(buf); 39925450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 39935450Sbrendan mutex_exit(hash_lock); 39945450Sbrendan zio->io_private = buf; 39957754SJeff.Bonwick@Sun.COM zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 39967754SJeff.Bonwick@Sun.COM zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 39975450Sbrendan arc_read_done(zio); 39985450Sbrendan } else { 39995450Sbrendan mutex_exit(hash_lock); 40005450Sbrendan /* 40015450Sbrendan * Buffer didn't survive caching. Increment stats and 40025450Sbrendan * reissue to the original storage device. 40035450Sbrendan */ 40046987Sbrendan if (zio->io_error != 0) { 40055450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 40066987Sbrendan } else { 40076987Sbrendan zio->io_error = EIO; 40086987Sbrendan } 40095450Sbrendan if (!equal) 40105450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 40115450Sbrendan 40127754SJeff.Bonwick@Sun.COM /* 40137754SJeff.Bonwick@Sun.COM * If there's no waiter, issue an async i/o to the primary 40147754SJeff.Bonwick@Sun.COM * storage now. If there *is* a waiter, the caller must 40157754SJeff.Bonwick@Sun.COM * issue the i/o in a context where it's OK to block. 40167754SJeff.Bonwick@Sun.COM */ 40178632SBill.Moore@Sun.COM if (zio->io_waiter == NULL) { 40188632SBill.Moore@Sun.COM zio_t *pio = zio_unique_parent(zio); 40198632SBill.Moore@Sun.COM 40208632SBill.Moore@Sun.COM ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 40218632SBill.Moore@Sun.COM 40228632SBill.Moore@Sun.COM zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 40237754SJeff.Bonwick@Sun.COM buf->b_data, zio->io_size, arc_read_done, buf, 40247754SJeff.Bonwick@Sun.COM zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 40258632SBill.Moore@Sun.COM } 40265450Sbrendan } 40275450Sbrendan 40285450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 40295450Sbrendan } 40305450Sbrendan 40315450Sbrendan /* 40325450Sbrendan * This is the list priority from which the L2ARC will search for pages to 40335450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 40345450Sbrendan * desired order. This order can have a significant effect on cache 40355450Sbrendan * performance. 40365450Sbrendan * 40375450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 40385450Sbrendan * the data lists. This function returns a locked list, and also returns 40395450Sbrendan * the lock pointer. 40405450Sbrendan */ 40415450Sbrendan static list_t * 40425450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 40435450Sbrendan { 40445450Sbrendan list_t *list; 40455450Sbrendan 40465450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 40475450Sbrendan 40485450Sbrendan switch (list_num) { 40495450Sbrendan case 0: 40505450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 40515450Sbrendan *lock = &arc_mfu->arcs_mtx; 40525450Sbrendan break; 40535450Sbrendan case 1: 40545450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 40555450Sbrendan *lock = &arc_mru->arcs_mtx; 40565450Sbrendan break; 40575450Sbrendan case 2: 40585450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 40595450Sbrendan *lock = &arc_mfu->arcs_mtx; 40605450Sbrendan break; 40615450Sbrendan case 3: 40625450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 40635450Sbrendan *lock = &arc_mru->arcs_mtx; 40645450Sbrendan break; 40655450Sbrendan } 40665450Sbrendan 40675450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 40685450Sbrendan mutex_enter(*lock); 40695450Sbrendan return (list); 40705450Sbrendan } 40715450Sbrendan 40725450Sbrendan /* 40735450Sbrendan * Evict buffers from the device write hand to the distance specified in 40745450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 40755450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 40765450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 40775450Sbrendan */ 40785450Sbrendan static void 40795450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 40805450Sbrendan { 40815450Sbrendan list_t *buflist; 40825450Sbrendan l2arc_buf_hdr_t *abl2; 40835450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 40845450Sbrendan kmutex_t *hash_lock; 40855450Sbrendan uint64_t taddr; 40865450Sbrendan 40875450Sbrendan buflist = dev->l2ad_buflist; 40885450Sbrendan 40895450Sbrendan if (buflist == NULL) 40905450Sbrendan return; 40915450Sbrendan 40925450Sbrendan if (!all && dev->l2ad_first) { 40935450Sbrendan /* 40945450Sbrendan * This is the first sweep through the device. There is 40955450Sbrendan * nothing to evict. 40965450Sbrendan */ 40975450Sbrendan return; 40985450Sbrendan } 40995450Sbrendan 41006987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 41015450Sbrendan /* 41025450Sbrendan * When nearing the end of the device, evict to the end 41035450Sbrendan * before the device write hand jumps to the start. 41045450Sbrendan */ 41055450Sbrendan taddr = dev->l2ad_end; 41065450Sbrendan } else { 41075450Sbrendan taddr = dev->l2ad_hand + distance; 41085450Sbrendan } 41095450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 41105450Sbrendan uint64_t, taddr, boolean_t, all); 41115450Sbrendan 41125450Sbrendan top: 41135450Sbrendan mutex_enter(&l2arc_buflist_mtx); 41145450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 41155450Sbrendan ab_prev = list_prev(buflist, ab); 41165450Sbrendan 41175450Sbrendan hash_lock = HDR_LOCK(ab); 41185450Sbrendan if (!mutex_tryenter(hash_lock)) { 41195450Sbrendan /* 41205450Sbrendan * Missed the hash lock. Retry. 41215450Sbrendan */ 41225450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 41235450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41245450Sbrendan mutex_enter(hash_lock); 41255450Sbrendan mutex_exit(hash_lock); 41265450Sbrendan goto top; 41275450Sbrendan } 41285450Sbrendan 41295450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 41305450Sbrendan /* 41315450Sbrendan * We hit a write head node. Leave it for 41325450Sbrendan * l2arc_write_done(). 41335450Sbrendan */ 41345450Sbrendan list_remove(buflist, ab); 41355450Sbrendan mutex_exit(hash_lock); 41365450Sbrendan continue; 41375450Sbrendan } 41385450Sbrendan 41395450Sbrendan if (!all && ab->b_l2hdr != NULL && 41405450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 41415450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 41425450Sbrendan /* 41435450Sbrendan * We've evicted to the target address, 41445450Sbrendan * or the end of the device. 41455450Sbrendan */ 41465450Sbrendan mutex_exit(hash_lock); 41475450Sbrendan break; 41485450Sbrendan } 41495450Sbrendan 41505450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 41515450Sbrendan /* 41525450Sbrendan * Already on the path to destruction. 41535450Sbrendan */ 41545450Sbrendan mutex_exit(hash_lock); 41555450Sbrendan continue; 41565450Sbrendan } 41575450Sbrendan 41585450Sbrendan if (ab->b_state == arc_l2c_only) { 41595450Sbrendan ASSERT(!HDR_L2_READING(ab)); 41605450Sbrendan /* 41615450Sbrendan * This doesn't exist in the ARC. Destroy. 41625450Sbrendan * arc_hdr_destroy() will call list_remove() 41635450Sbrendan * and decrement arcstat_l2_size. 41645450Sbrendan */ 41655450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 41665450Sbrendan arc_hdr_destroy(ab); 41675450Sbrendan } else { 41685450Sbrendan /* 41696987Sbrendan * Invalidate issued or about to be issued 41706987Sbrendan * reads, since we may be about to write 41716987Sbrendan * over this location. 41726987Sbrendan */ 41736987Sbrendan if (HDR_L2_READING(ab)) { 41746987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 41756987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 41766987Sbrendan } 41776987Sbrendan 41786987Sbrendan /* 41795450Sbrendan * Tell ARC this no longer exists in L2ARC. 41805450Sbrendan */ 41815450Sbrendan if (ab->b_l2hdr != NULL) { 41825450Sbrendan abl2 = ab->b_l2hdr; 41835450Sbrendan ab->b_l2hdr = NULL; 41845450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 41855450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 41865450Sbrendan } 41875450Sbrendan list_remove(buflist, ab); 41885450Sbrendan 41895450Sbrendan /* 41905450Sbrendan * This may have been leftover after a 41915450Sbrendan * failed write. 41925450Sbrendan */ 41935450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 41945450Sbrendan } 41955450Sbrendan mutex_exit(hash_lock); 41965450Sbrendan } 41975450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41985450Sbrendan 419910922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0); 42005450Sbrendan dev->l2ad_evict = taddr; 42015450Sbrendan } 42025450Sbrendan 42035450Sbrendan /* 42045450Sbrendan * Find and write ARC buffers to the L2ARC device. 42055450Sbrendan * 42065450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 42075450Sbrendan * for reading until they have completed writing. 42085450Sbrendan */ 42098582SBrendan.Gregg@Sun.COM static uint64_t 42106987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 42115450Sbrendan { 42125450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 42135450Sbrendan l2arc_buf_hdr_t *hdrl2; 42145450Sbrendan list_t *list; 42156987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 42165450Sbrendan void *buf_data; 42175450Sbrendan kmutex_t *hash_lock, *list_lock; 42185450Sbrendan boolean_t have_lock, full; 42195450Sbrendan l2arc_write_callback_t *cb; 42205450Sbrendan zio_t *pio, *wzio; 42218636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 42225450Sbrendan 42235450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 42245450Sbrendan 42255450Sbrendan pio = NULL; 42265450Sbrendan write_sz = 0; 42275450Sbrendan full = B_FALSE; 42286245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 42295450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 42305450Sbrendan 42315450Sbrendan /* 42325450Sbrendan * Copy buffers for L2ARC writing. 42335450Sbrendan */ 42345450Sbrendan mutex_enter(&l2arc_buflist_mtx); 42355450Sbrendan for (int try = 0; try <= 3; try++) { 42365450Sbrendan list = l2arc_list_locked(try, &list_lock); 42375450Sbrendan passed_sz = 0; 42385450Sbrendan 42396987Sbrendan /* 42406987Sbrendan * L2ARC fast warmup. 42416987Sbrendan * 42426987Sbrendan * Until the ARC is warm and starts to evict, read from the 42436987Sbrendan * head of the ARC lists rather than the tail. 42446987Sbrendan */ 42456987Sbrendan headroom = target_sz * l2arc_headroom; 42466987Sbrendan if (arc_warm == B_FALSE) 42476987Sbrendan ab = list_head(list); 42486987Sbrendan else 42496987Sbrendan ab = list_tail(list); 42506987Sbrendan 42516987Sbrendan for (; ab; ab = ab_prev) { 42526987Sbrendan if (arc_warm == B_FALSE) 42536987Sbrendan ab_prev = list_next(list, ab); 42546987Sbrendan else 42556987Sbrendan ab_prev = list_prev(list, ab); 42565450Sbrendan 42575450Sbrendan hash_lock = HDR_LOCK(ab); 42585450Sbrendan have_lock = MUTEX_HELD(hash_lock); 42595450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 42605450Sbrendan /* 42615450Sbrendan * Skip this buffer rather than waiting. 42625450Sbrendan */ 42635450Sbrendan continue; 42645450Sbrendan } 42655450Sbrendan 42665450Sbrendan passed_sz += ab->b_size; 42675450Sbrendan if (passed_sz > headroom) { 42685450Sbrendan /* 42695450Sbrendan * Searched too far. 42705450Sbrendan */ 42715450Sbrendan mutex_exit(hash_lock); 42725450Sbrendan break; 42735450Sbrendan } 42745450Sbrendan 42758636SMark.Maybee@Sun.COM if (!l2arc_write_eligible(guid, ab)) { 42765450Sbrendan mutex_exit(hash_lock); 42775450Sbrendan continue; 42785450Sbrendan } 42795450Sbrendan 42805450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 42815450Sbrendan full = B_TRUE; 42825450Sbrendan mutex_exit(hash_lock); 42835450Sbrendan break; 42845450Sbrendan } 42855450Sbrendan 42865450Sbrendan if (pio == NULL) { 42875450Sbrendan /* 42885450Sbrendan * Insert a dummy header on the buflist so 42895450Sbrendan * l2arc_write_done() can find where the 42905450Sbrendan * write buffers begin without searching. 42915450Sbrendan */ 42925450Sbrendan list_insert_head(dev->l2ad_buflist, head); 42935450Sbrendan 42945450Sbrendan cb = kmem_alloc( 42955450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 42965450Sbrendan cb->l2wcb_dev = dev; 42975450Sbrendan cb->l2wcb_head = head; 42985450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 42995450Sbrendan ZIO_FLAG_CANFAIL); 43005450Sbrendan } 43015450Sbrendan 43025450Sbrendan /* 43035450Sbrendan * Create and add a new L2ARC header. 43045450Sbrendan */ 43055450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 43065450Sbrendan hdrl2->b_dev = dev; 43075450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 43085450Sbrendan 43095450Sbrendan ab->b_flags |= ARC_L2_WRITING; 43105450Sbrendan ab->b_l2hdr = hdrl2; 43115450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 43125450Sbrendan buf_data = ab->b_buf->b_data; 43135450Sbrendan buf_sz = ab->b_size; 43145450Sbrendan 43155450Sbrendan /* 43165450Sbrendan * Compute and store the buffer cksum before 43175450Sbrendan * writing. On debug the cksum is verified first. 43185450Sbrendan */ 43195450Sbrendan arc_cksum_verify(ab->b_buf); 43205450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 43215450Sbrendan 43225450Sbrendan mutex_exit(hash_lock); 43235450Sbrendan 43245450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 43255450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 43265450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 43275450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 43285450Sbrendan 43295450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 43305450Sbrendan zio_t *, wzio); 43315450Sbrendan (void) zio_nowait(wzio); 43325450Sbrendan 43337754SJeff.Bonwick@Sun.COM /* 43347754SJeff.Bonwick@Sun.COM * Keep the clock hand suitably device-aligned. 43357754SJeff.Bonwick@Sun.COM */ 43367754SJeff.Bonwick@Sun.COM buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 43377754SJeff.Bonwick@Sun.COM 43385450Sbrendan write_sz += buf_sz; 43395450Sbrendan dev->l2ad_hand += buf_sz; 43405450Sbrendan } 43415450Sbrendan 43425450Sbrendan mutex_exit(list_lock); 43435450Sbrendan 43445450Sbrendan if (full == B_TRUE) 43455450Sbrendan break; 43465450Sbrendan } 43475450Sbrendan mutex_exit(&l2arc_buflist_mtx); 43485450Sbrendan 43495450Sbrendan if (pio == NULL) { 43505450Sbrendan ASSERT3U(write_sz, ==, 0); 43515450Sbrendan kmem_cache_free(hdr_cache, head); 43528582SBrendan.Gregg@Sun.COM return (0); 43535450Sbrendan } 43545450Sbrendan 43555450Sbrendan ASSERT3U(write_sz, <=, target_sz); 43565450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 43578582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 43585450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 435910922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0); 43605450Sbrendan 43615450Sbrendan /* 43625450Sbrendan * Bump device hand to the device start if it is approaching the end. 43635450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 43645450Sbrendan */ 43656987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 436610922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, 436710922SJeff.Bonwick@Sun.COM dev->l2ad_end - dev->l2ad_hand, 0, 0); 43685450Sbrendan dev->l2ad_hand = dev->l2ad_start; 43695450Sbrendan dev->l2ad_evict = dev->l2ad_start; 43705450Sbrendan dev->l2ad_first = B_FALSE; 43715450Sbrendan } 43725450Sbrendan 43738582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_TRUE; 43745450Sbrendan (void) zio_wait(pio); 43758582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_FALSE; 43768582SBrendan.Gregg@Sun.COM 43778582SBrendan.Gregg@Sun.COM return (write_sz); 43785450Sbrendan } 43795450Sbrendan 43805450Sbrendan /* 43815450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 43825450Sbrendan * heart of the L2ARC. 43835450Sbrendan */ 43845450Sbrendan static void 43855450Sbrendan l2arc_feed_thread(void) 43865450Sbrendan { 43875450Sbrendan callb_cpr_t cpr; 43885450Sbrendan l2arc_dev_t *dev; 43895450Sbrendan spa_t *spa; 43908582SBrendan.Gregg@Sun.COM uint64_t size, wrote; 439111066Srafael.vanoni@sun.com clock_t begin, next = ddi_get_lbolt(); 43925450Sbrendan 43935450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 43945450Sbrendan 43955450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 43965450Sbrendan 43975450Sbrendan while (l2arc_thread_exit == 0) { 43985450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 43996987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 44008582SBrendan.Gregg@Sun.COM next); 44016987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 440211066Srafael.vanoni@sun.com next = ddi_get_lbolt() + hz; 44036987Sbrendan 44046987Sbrendan /* 44056987Sbrendan * Quick check for L2ARC devices. 44066987Sbrendan */ 44076987Sbrendan mutex_enter(&l2arc_dev_mtx); 44086987Sbrendan if (l2arc_ndev == 0) { 44096987Sbrendan mutex_exit(&l2arc_dev_mtx); 44106987Sbrendan continue; 44115450Sbrendan } 44126987Sbrendan mutex_exit(&l2arc_dev_mtx); 441311066Srafael.vanoni@sun.com begin = ddi_get_lbolt(); 44146643Seschrock 44155450Sbrendan /* 44166643Seschrock * This selects the next l2arc device to write to, and in 44176643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 44186987Sbrendan * will return NULL if there are now no l2arc devices or if 44196987Sbrendan * they are all faulted. 44206987Sbrendan * 44216987Sbrendan * If a device is returned, its spa's config lock is also 44226987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 44236987Sbrendan * will grab and release l2arc_dev_mtx. 44245450Sbrendan */ 44256987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 44265450Sbrendan continue; 44276987Sbrendan 44286987Sbrendan spa = dev->l2ad_spa; 44296987Sbrendan ASSERT(spa != NULL); 44305450Sbrendan 44315450Sbrendan /* 44325450Sbrendan * Avoid contributing to memory pressure. 44335450Sbrendan */ 44345450Sbrendan if (arc_reclaim_needed()) { 44355450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 44367754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44375450Sbrendan continue; 44385450Sbrendan } 44395450Sbrendan 44405450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 44415450Sbrendan 44428582SBrendan.Gregg@Sun.COM size = l2arc_write_size(dev); 44436987Sbrendan 44445450Sbrendan /* 44455450Sbrendan * Evict L2ARC buffers that will be overwritten. 44465450Sbrendan */ 44476987Sbrendan l2arc_evict(dev, size, B_FALSE); 44485450Sbrendan 44495450Sbrendan /* 44505450Sbrendan * Write ARC buffers. 44515450Sbrendan */ 44528582SBrendan.Gregg@Sun.COM wrote = l2arc_write_buffers(spa, dev, size); 44538582SBrendan.Gregg@Sun.COM 44548582SBrendan.Gregg@Sun.COM /* 44558582SBrendan.Gregg@Sun.COM * Calculate interval between writes. 44568582SBrendan.Gregg@Sun.COM */ 44578582SBrendan.Gregg@Sun.COM next = l2arc_write_interval(begin, size, wrote); 44587754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44595450Sbrendan } 44605450Sbrendan 44615450Sbrendan l2arc_thread_exit = 0; 44625450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 44635450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 44645450Sbrendan thread_exit(); 44655450Sbrendan } 44665450Sbrendan 44676643Seschrock boolean_t 44686643Seschrock l2arc_vdev_present(vdev_t *vd) 44696643Seschrock { 44706643Seschrock l2arc_dev_t *dev; 44716643Seschrock 44726643Seschrock mutex_enter(&l2arc_dev_mtx); 44736643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 44746643Seschrock dev = list_next(l2arc_dev_list, dev)) { 44756643Seschrock if (dev->l2ad_vdev == vd) 44766643Seschrock break; 44776643Seschrock } 44786643Seschrock mutex_exit(&l2arc_dev_mtx); 44796643Seschrock 44806643Seschrock return (dev != NULL); 44816643Seschrock } 44826643Seschrock 44835450Sbrendan /* 44845450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 44855450Sbrendan * validated the vdev and opened it. 44865450Sbrendan */ 44875450Sbrendan void 44889816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa_t *spa, vdev_t *vd) 44895450Sbrendan { 44905450Sbrendan l2arc_dev_t *adddev; 44915450Sbrendan 44926643Seschrock ASSERT(!l2arc_vdev_present(vd)); 44936643Seschrock 44945450Sbrendan /* 44955450Sbrendan * Create a new l2arc device entry. 44965450Sbrendan */ 44975450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 44985450Sbrendan adddev->l2ad_spa = spa; 44995450Sbrendan adddev->l2ad_vdev = vd; 45005450Sbrendan adddev->l2ad_write = l2arc_write_max; 45016987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 45029816SGeorge.Wilson@Sun.COM adddev->l2ad_start = VDEV_LABEL_START_SIZE; 45039816SGeorge.Wilson@Sun.COM adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 45045450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 45055450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 45065450Sbrendan adddev->l2ad_first = B_TRUE; 45078582SBrendan.Gregg@Sun.COM adddev->l2ad_writing = B_FALSE; 45085450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 45095450Sbrendan 45105450Sbrendan /* 45115450Sbrendan * This is a list of all ARC buffers that are still valid on the 45125450Sbrendan * device. 45135450Sbrendan */ 45145450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 45155450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 45165450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 45175450Sbrendan 451810922SJeff.Bonwick@Sun.COM vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 45195450Sbrendan 45205450Sbrendan /* 45215450Sbrendan * Add device to global list 45225450Sbrendan */ 45235450Sbrendan mutex_enter(&l2arc_dev_mtx); 45245450Sbrendan list_insert_head(l2arc_dev_list, adddev); 45255450Sbrendan atomic_inc_64(&l2arc_ndev); 45265450Sbrendan mutex_exit(&l2arc_dev_mtx); 45275450Sbrendan } 45285450Sbrendan 45295450Sbrendan /* 45305450Sbrendan * Remove a vdev from the L2ARC. 45315450Sbrendan */ 45325450Sbrendan void 45335450Sbrendan l2arc_remove_vdev(vdev_t *vd) 45345450Sbrendan { 45355450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 45365450Sbrendan 45375450Sbrendan /* 45385450Sbrendan * Find the device by vdev 45395450Sbrendan */ 45405450Sbrendan mutex_enter(&l2arc_dev_mtx); 45415450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 45425450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 45435450Sbrendan if (vd == dev->l2ad_vdev) { 45445450Sbrendan remdev = dev; 45455450Sbrendan break; 45465450Sbrendan } 45475450Sbrendan } 45485450Sbrendan ASSERT(remdev != NULL); 45495450Sbrendan 45505450Sbrendan /* 45515450Sbrendan * Remove device from global list 45525450Sbrendan */ 45535450Sbrendan list_remove(l2arc_dev_list, remdev); 45545450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 45556987Sbrendan atomic_dec_64(&l2arc_ndev); 45566987Sbrendan mutex_exit(&l2arc_dev_mtx); 45575450Sbrendan 45585450Sbrendan /* 45595450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 45605450Sbrendan */ 45615450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 45625450Sbrendan list_destroy(remdev->l2ad_buflist); 45635450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 45645450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 45655450Sbrendan } 45665450Sbrendan 45675450Sbrendan void 45687754SJeff.Bonwick@Sun.COM l2arc_init(void) 45695450Sbrendan { 45705450Sbrendan l2arc_thread_exit = 0; 45715450Sbrendan l2arc_ndev = 0; 45725450Sbrendan l2arc_writes_sent = 0; 45735450Sbrendan l2arc_writes_done = 0; 45745450Sbrendan 45755450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 45765450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 45775450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 45785450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 45795450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 45805450Sbrendan 45815450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 45825450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 45835450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 45845450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 45855450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 45865450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 45875450Sbrendan } 45885450Sbrendan 45895450Sbrendan void 45907754SJeff.Bonwick@Sun.COM l2arc_fini(void) 45915450Sbrendan { 45926987Sbrendan /* 45936987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 45946987Sbrendan * Because of this, we can assume that all l2arc devices have 45956987Sbrendan * already been removed when the pools themselves were removed. 45966987Sbrendan */ 45976987Sbrendan 45986987Sbrendan l2arc_do_free_on_write(); 45996987Sbrendan 46005450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 46015450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 46025450Sbrendan mutex_destroy(&l2arc_dev_mtx); 46035450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 46045450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 46055450Sbrendan 46065450Sbrendan list_destroy(l2arc_dev_list); 46075450Sbrendan list_destroy(l2arc_free_on_write); 46085450Sbrendan } 46097754SJeff.Bonwick@Sun.COM 46107754SJeff.Bonwick@Sun.COM void 46117754SJeff.Bonwick@Sun.COM l2arc_start(void) 46127754SJeff.Bonwick@Sun.COM { 46138241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46147754SJeff.Bonwick@Sun.COM return; 46157754SJeff.Bonwick@Sun.COM 46167754SJeff.Bonwick@Sun.COM (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 46177754SJeff.Bonwick@Sun.COM TS_RUN, minclsyspri); 46187754SJeff.Bonwick@Sun.COM } 46197754SJeff.Bonwick@Sun.COM 46207754SJeff.Bonwick@Sun.COM void 46217754SJeff.Bonwick@Sun.COM l2arc_stop(void) 46227754SJeff.Bonwick@Sun.COM { 46238241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46247754SJeff.Bonwick@Sun.COM return; 46257754SJeff.Bonwick@Sun.COM 46267754SJeff.Bonwick@Sun.COM mutex_enter(&l2arc_feed_thr_lock); 46277754SJeff.Bonwick@Sun.COM cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 46287754SJeff.Bonwick@Sun.COM l2arc_thread_exit = 1; 46297754SJeff.Bonwick@Sun.COM while (l2arc_thread_exit != 0) 46307754SJeff.Bonwick@Sun.COM cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 46317754SJeff.Bonwick@Sun.COM mutex_exit(&l2arc_feed_thr_lock); 46327754SJeff.Bonwick@Sun.COM } 4633