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 /* 2211539SChunli.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); 124511539SChunli.Zhang@Sun.COM (void) refcount_add(&hdr->b_refcnt, tag); 124611539SChunli.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 125111539SChunli.Zhang@Sun.COM /* Detach an arc_buf from a dbuf (tag) */ 125211539SChunli.Zhang@Sun.COM void 125311539SChunli.Zhang@Sun.COM arc_loan_inuse_buf(arc_buf_t *buf, void *tag) 125411539SChunli.Zhang@Sun.COM { 125511539SChunli.Zhang@Sun.COM arc_buf_hdr_t *hdr; 125611539SChunli.Zhang@Sun.COM 125711539SChunli.Zhang@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 125811539SChunli.Zhang@Sun.COM ASSERT(buf->b_data != NULL); 125911539SChunli.Zhang@Sun.COM hdr = buf->b_hdr; 126011539SChunli.Zhang@Sun.COM (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag); 126111539SChunli.Zhang@Sun.COM (void) refcount_remove(&hdr->b_refcnt, tag); 126211539SChunli.Zhang@Sun.COM buf->b_efunc = NULL; 126311539SChunli.Zhang@Sun.COM buf->b_private = NULL; 126411539SChunli.Zhang@Sun.COM 126511539SChunli.Zhang@Sun.COM atomic_add_64(&arc_loaned_bytes, hdr->b_size); 126611539SChunli.Zhang@Sun.COM rw_exit(&buf->b_lock); 126711539SChunli.Zhang@Sun.COM } 126811539SChunli.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); 1734*12033Swilliam.gorrell@sun.com /* caller may be trying to modify this buffer, skip it */ 1735*12033Swilliam.gorrell@sun.com if (MUTEX_HELD(hash_lock)) 1736*12033Swilliam.gorrell@sun.com continue; 1737*12033Swilliam.gorrell@sun.com if (mutex_tryenter(hash_lock)) { 17382391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 17391544Seschrock ASSERT(ab->b_buf == NULL); 17403403Sbmc ARCSTAT_BUMP(arcstat_deleted); 17411544Seschrock bytes_deleted += ab->b_size; 17425450Sbrendan 17435450Sbrendan if (ab->b_l2hdr != NULL) { 17445450Sbrendan /* 17455450Sbrendan * This buffer is cached on the 2nd Level ARC; 17465450Sbrendan * don't destroy the header. 17475450Sbrendan */ 17485450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 1749*12033Swilliam.gorrell@sun.com mutex_exit(hash_lock); 17505450Sbrendan } else { 17515450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 1752*12033Swilliam.gorrell@sun.com mutex_exit(hash_lock); 17535450Sbrendan arc_hdr_destroy(ab); 17545450Sbrendan } 17555450Sbrendan 1756789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1757789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1758789Sahrens break; 1759789Sahrens } else { 1760789Sahrens if (bytes < 0) { 17613403Sbmc mutex_exit(&state->arcs_mtx); 1762789Sahrens mutex_enter(hash_lock); 1763789Sahrens mutex_exit(hash_lock); 1764789Sahrens goto top; 1765789Sahrens } 1766789Sahrens bufs_skipped += 1; 1767789Sahrens } 1768789Sahrens } 17693403Sbmc mutex_exit(&state->arcs_mtx); 1770789Sahrens 17714309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 17724309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 17734309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 17744309Smaybee goto top; 17754309Smaybee } 17764309Smaybee 1777789Sahrens if (bufs_skipped) { 17783403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1779789Sahrens ASSERT(bytes >= 0); 1780789Sahrens } 1781789Sahrens 1782789Sahrens if (bytes_deleted < bytes) 1783789Sahrens dprintf("only deleted %lld bytes from %p", 1784789Sahrens (longlong_t)bytes_deleted, state); 1785789Sahrens } 1786789Sahrens 1787789Sahrens static void 1788789Sahrens arc_adjust(void) 1789789Sahrens { 17908582SBrendan.Gregg@Sun.COM int64_t adjustment, delta; 17918582SBrendan.Gregg@Sun.COM 17928582SBrendan.Gregg@Sun.COM /* 17938582SBrendan.Gregg@Sun.COM * Adjust MRU size 17948582SBrendan.Gregg@Sun.COM */ 17958582SBrendan.Gregg@Sun.COM 17968582SBrendan.Gregg@Sun.COM adjustment = MIN(arc_size - arc_c, 17978582SBrendan.Gregg@Sun.COM arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - arc_p); 17988582SBrendan.Gregg@Sun.COM 17998582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 18008582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 18018582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA); 18028582SBrendan.Gregg@Sun.COM adjustment -= delta; 18034309Smaybee } 18044309Smaybee 18058582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 18068582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 18078582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, 18085642Smaybee ARC_BUFC_METADATA); 1809789Sahrens } 1810789Sahrens 18118582SBrendan.Gregg@Sun.COM /* 18128582SBrendan.Gregg@Sun.COM * Adjust MFU size 18138582SBrendan.Gregg@Sun.COM */ 18148582SBrendan.Gregg@Sun.COM 18158582SBrendan.Gregg@Sun.COM adjustment = arc_size - arc_c; 18168582SBrendan.Gregg@Sun.COM 18178582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 18188582SBrendan.Gregg@Sun.COM delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 18198582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA); 18208582SBrendan.Gregg@Sun.COM adjustment -= delta; 18218582SBrendan.Gregg@Sun.COM } 18228582SBrendan.Gregg@Sun.COM 18238582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 18248582SBrendan.Gregg@Sun.COM int64_t delta = MIN(adjustment, 18258582SBrendan.Gregg@Sun.COM arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 18268582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, 18278582SBrendan.Gregg@Sun.COM ARC_BUFC_METADATA); 18288582SBrendan.Gregg@Sun.COM } 18298582SBrendan.Gregg@Sun.COM 18308582SBrendan.Gregg@Sun.COM /* 18318582SBrendan.Gregg@Sun.COM * Adjust ghost lists 18328582SBrendan.Gregg@Sun.COM */ 18338582SBrendan.Gregg@Sun.COM 18348582SBrendan.Gregg@Sun.COM adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 18358582SBrendan.Gregg@Sun.COM 18368582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 18378582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru_ghost->arcs_size, adjustment); 18388582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mru_ghost, NULL, delta); 18398582SBrendan.Gregg@Sun.COM } 18408582SBrendan.Gregg@Sun.COM 18418582SBrendan.Gregg@Sun.COM adjustment = 18428582SBrendan.Gregg@Sun.COM arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 18438582SBrendan.Gregg@Sun.COM 18448582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 18458582SBrendan.Gregg@Sun.COM delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 18468582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mfu_ghost, NULL, delta); 1847789Sahrens } 1848789Sahrens } 1849789Sahrens 18501544Seschrock static void 18511544Seschrock arc_do_user_evicts(void) 18521544Seschrock { 18531544Seschrock mutex_enter(&arc_eviction_mtx); 18541544Seschrock while (arc_eviction_list != NULL) { 18551544Seschrock arc_buf_t *buf = arc_eviction_list; 18561544Seschrock arc_eviction_list = buf->b_next; 18577545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 18581544Seschrock buf->b_hdr = NULL; 18597545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 18601544Seschrock mutex_exit(&arc_eviction_mtx); 18611544Seschrock 18621819Smaybee if (buf->b_efunc != NULL) 18631819Smaybee VERIFY(buf->b_efunc(buf) == 0); 18641544Seschrock 18651544Seschrock buf->b_efunc = NULL; 18661544Seschrock buf->b_private = NULL; 18671544Seschrock kmem_cache_free(buf_cache, buf); 18681544Seschrock mutex_enter(&arc_eviction_mtx); 18691544Seschrock } 18701544Seschrock mutex_exit(&arc_eviction_mtx); 18711544Seschrock } 18721544Seschrock 1873789Sahrens /* 18745642Smaybee * Flush all *evictable* data from the cache for the given spa. 1875789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1876789Sahrens */ 1877789Sahrens void 18785642Smaybee arc_flush(spa_t *spa) 1879789Sahrens { 18808636SMark.Maybee@Sun.COM uint64_t guid = 0; 18818636SMark.Maybee@Sun.COM 18828636SMark.Maybee@Sun.COM if (spa) 18838636SMark.Maybee@Sun.COM guid = spa_guid(spa); 18848636SMark.Maybee@Sun.COM 18855642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 18868636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 18875642Smaybee if (spa) 18885642Smaybee break; 18895642Smaybee } 18905642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 18918636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 18925642Smaybee if (spa) 18935642Smaybee break; 18945642Smaybee } 18955642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 18968636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 18975642Smaybee if (spa) 18985642Smaybee break; 18995642Smaybee } 19005642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 19018636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 19025642Smaybee if (spa) 19035642Smaybee break; 19045642Smaybee } 19055642Smaybee 19068636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mru_ghost, guid, -1); 19078636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mfu_ghost, guid, -1); 19081544Seschrock 19091544Seschrock mutex_enter(&arc_reclaim_thr_lock); 19101544Seschrock arc_do_user_evicts(); 19111544Seschrock mutex_exit(&arc_reclaim_thr_lock); 19125642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1913789Sahrens } 1914789Sahrens 1915789Sahrens void 19163158Smaybee arc_shrink(void) 1917789Sahrens { 19183403Sbmc if (arc_c > arc_c_min) { 19193158Smaybee uint64_t to_free; 1920789Sahrens 19212048Sstans #ifdef _KERNEL 19223403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 19232048Sstans #else 19243403Sbmc to_free = arc_c >> arc_shrink_shift; 19252048Sstans #endif 19263403Sbmc if (arc_c > arc_c_min + to_free) 19273403Sbmc atomic_add_64(&arc_c, -to_free); 19283158Smaybee else 19293403Sbmc arc_c = arc_c_min; 19302048Sstans 19313403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 19323403Sbmc if (arc_c > arc_size) 19333403Sbmc arc_c = MAX(arc_size, arc_c_min); 19343403Sbmc if (arc_p > arc_c) 19353403Sbmc arc_p = (arc_c >> 1); 19363403Sbmc ASSERT(arc_c >= arc_c_min); 19373403Sbmc ASSERT((int64_t)arc_p >= 0); 19383158Smaybee } 1939789Sahrens 19403403Sbmc if (arc_size > arc_c) 19413158Smaybee arc_adjust(); 1942789Sahrens } 1943789Sahrens 1944789Sahrens static int 1945789Sahrens arc_reclaim_needed(void) 1946789Sahrens { 1947789Sahrens uint64_t extra; 1948789Sahrens 1949789Sahrens #ifdef _KERNEL 19502048Sstans 19512048Sstans if (needfree) 19522048Sstans return (1); 19532048Sstans 1954789Sahrens /* 1955789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1956789Sahrens */ 1957789Sahrens extra = desfree; 1958789Sahrens 1959789Sahrens /* 1960789Sahrens * check that we're out of range of the pageout scanner. It starts to 1961789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1962789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1963789Sahrens * number of needed free pages. We add extra pages here to make sure 1964789Sahrens * the scanner doesn't start up while we're freeing memory. 1965789Sahrens */ 1966789Sahrens if (freemem < lotsfree + needfree + extra) 1967789Sahrens return (1); 1968789Sahrens 1969789Sahrens /* 1970789Sahrens * check to make sure that swapfs has enough space so that anon 19715450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1972789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1973789Sahrens * swap pages. We also add a bit of extra here just to prevent 1974789Sahrens * circumstances from getting really dire. 1975789Sahrens */ 1976789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1977789Sahrens return (1); 1978789Sahrens 19791936Smaybee #if defined(__i386) 1980789Sahrens /* 1981789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1982789Sahrens * kernel heap space before we ever run out of available physical 1983789Sahrens * memory. Most checks of the size of the heap_area compare against 1984789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1985789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1986789Sahrens * which is so low that it's useless. In this comparison, we seek to 1987789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 19885450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1989789Sahrens * free) 1990789Sahrens */ 1991789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1992789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1993789Sahrens return (1); 1994789Sahrens #endif 1995789Sahrens 1996789Sahrens #else 1997789Sahrens if (spa_get_random(100) == 0) 1998789Sahrens return (1); 1999789Sahrens #endif 2000789Sahrens return (0); 2001789Sahrens } 2002789Sahrens 2003789Sahrens static void 2004789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 2005789Sahrens { 2006789Sahrens size_t i; 2007789Sahrens kmem_cache_t *prev_cache = NULL; 20083290Sjohansen kmem_cache_t *prev_data_cache = NULL; 2009789Sahrens extern kmem_cache_t *zio_buf_cache[]; 20103290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 2011789Sahrens 20121484Sek110237 #ifdef _KERNEL 20134309Smaybee if (arc_meta_used >= arc_meta_limit) { 20144309Smaybee /* 20154309Smaybee * We are exceeding our meta-data cache limit. 20164309Smaybee * Purge some DNLC entries to release holds on meta-data. 20174309Smaybee */ 20184309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 20194309Smaybee } 20201936Smaybee #if defined(__i386) 20211936Smaybee /* 20221936Smaybee * Reclaim unused memory from all kmem caches. 20231936Smaybee */ 20241936Smaybee kmem_reap(); 20251936Smaybee #endif 20261484Sek110237 #endif 20271484Sek110237 2028789Sahrens /* 20295450Sbrendan * An aggressive reclamation will shrink the cache size as well as 20301544Seschrock * reap free buffers from the arc kmem caches. 2031789Sahrens */ 2032789Sahrens if (strat == ARC_RECLAIM_AGGR) 20333158Smaybee arc_shrink(); 2034789Sahrens 2035789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2036789Sahrens if (zio_buf_cache[i] != prev_cache) { 2037789Sahrens prev_cache = zio_buf_cache[i]; 2038789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 2039789Sahrens } 20403290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 20413290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 20423290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 20433290Sjohansen } 2044789Sahrens } 20451544Seschrock kmem_cache_reap_now(buf_cache); 20461544Seschrock kmem_cache_reap_now(hdr_cache); 2047789Sahrens } 2048789Sahrens 2049789Sahrens static void 2050789Sahrens arc_reclaim_thread(void) 2051789Sahrens { 2052789Sahrens clock_t growtime = 0; 2053789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2054789Sahrens callb_cpr_t cpr; 2055789Sahrens 2056789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2057789Sahrens 2058789Sahrens mutex_enter(&arc_reclaim_thr_lock); 2059789Sahrens while (arc_thread_exit == 0) { 2060789Sahrens if (arc_reclaim_needed()) { 2061789Sahrens 20623403Sbmc if (arc_no_grow) { 2063789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 2064789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2065789Sahrens } else { 2066789Sahrens last_reclaim = ARC_RECLAIM_CONS; 2067789Sahrens } 2068789Sahrens } else { 20693403Sbmc arc_no_grow = TRUE; 2070789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2071789Sahrens membar_producer(); 2072789Sahrens } 2073789Sahrens 2074789Sahrens /* reset the growth delay for every reclaim */ 207511066Srafael.vanoni@sun.com growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 2076789Sahrens 2077789Sahrens arc_kmem_reap_now(last_reclaim); 20786987Sbrendan arc_warm = B_TRUE; 2079789Sahrens 208011066Srafael.vanoni@sun.com } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 20813403Sbmc arc_no_grow = FALSE; 2082789Sahrens } 2083789Sahrens 20843403Sbmc if (2 * arc_c < arc_size + 20853403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 20863298Smaybee arc_adjust(); 20873298Smaybee 20881544Seschrock if (arc_eviction_list != NULL) 20891544Seschrock arc_do_user_evicts(); 20901544Seschrock 2091789Sahrens /* block until needed, or one second, whichever is shorter */ 2092789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 2093789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 209411066Srafael.vanoni@sun.com &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz)); 2095789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2096789Sahrens } 2097789Sahrens 2098789Sahrens arc_thread_exit = 0; 2099789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 2100789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2101789Sahrens thread_exit(); 2102789Sahrens } 2103789Sahrens 21041544Seschrock /* 21051544Seschrock * Adapt arc info given the number of bytes we are trying to add and 21061544Seschrock * the state that we are comming from. This function is only called 21071544Seschrock * when we are adding new content to the cache. 21081544Seschrock */ 2109789Sahrens static void 21101544Seschrock arc_adapt(int bytes, arc_state_t *state) 2111789Sahrens { 21121544Seschrock int mult; 21138582SBrendan.Gregg@Sun.COM uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 21141544Seschrock 21155450Sbrendan if (state == arc_l2c_only) 21165450Sbrendan return; 21175450Sbrendan 21181544Seschrock ASSERT(bytes > 0); 2119789Sahrens /* 21201544Seschrock * Adapt the target size of the MRU list: 21211544Seschrock * - if we just hit in the MRU ghost list, then increase 21221544Seschrock * the target size of the MRU list. 21231544Seschrock * - if we just hit in the MFU ghost list, then increase 21241544Seschrock * the target size of the MFU list by decreasing the 21251544Seschrock * target size of the MRU list. 2126789Sahrens */ 21273403Sbmc if (state == arc_mru_ghost) { 21283403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 21293403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 21301544Seschrock 21318582SBrendan.Gregg@Sun.COM arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 21323403Sbmc } else if (state == arc_mfu_ghost) { 21338582SBrendan.Gregg@Sun.COM uint64_t delta; 21348582SBrendan.Gregg@Sun.COM 21353403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 21363403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 21371544Seschrock 21388582SBrendan.Gregg@Sun.COM delta = MIN(bytes * mult, arc_p); 21398582SBrendan.Gregg@Sun.COM arc_p = MAX(arc_p_min, arc_p - delta); 21401544Seschrock } 21413403Sbmc ASSERT((int64_t)arc_p >= 0); 2142789Sahrens 2143789Sahrens if (arc_reclaim_needed()) { 2144789Sahrens cv_signal(&arc_reclaim_thr_cv); 2145789Sahrens return; 2146789Sahrens } 2147789Sahrens 21483403Sbmc if (arc_no_grow) 2149789Sahrens return; 2150789Sahrens 21513403Sbmc if (arc_c >= arc_c_max) 21521544Seschrock return; 21531544Seschrock 2154789Sahrens /* 21551544Seschrock * If we're within (2 * maxblocksize) bytes of the target 21561544Seschrock * cache size, increment the target cache size 2157789Sahrens */ 21583403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 21593403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 21603403Sbmc if (arc_c > arc_c_max) 21613403Sbmc arc_c = arc_c_max; 21623403Sbmc else if (state == arc_anon) 21633403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 21643403Sbmc if (arc_p > arc_c) 21653403Sbmc arc_p = arc_c; 2166789Sahrens } 21673403Sbmc ASSERT((int64_t)arc_p >= 0); 2168789Sahrens } 2169789Sahrens 2170789Sahrens /* 21711544Seschrock * Check if the cache has reached its limits and eviction is required 21721544Seschrock * prior to insert. 2173789Sahrens */ 2174789Sahrens static int 21754309Smaybee arc_evict_needed(arc_buf_contents_t type) 2176789Sahrens { 21774309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 21784309Smaybee return (1); 21794309Smaybee 21804309Smaybee #ifdef _KERNEL 21814309Smaybee /* 21824309Smaybee * If zio data pages are being allocated out of a separate heap segment, 21834309Smaybee * then enforce that the size of available vmem for this area remains 21844309Smaybee * above about 1/32nd free. 21854309Smaybee */ 21864309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 21874309Smaybee vmem_size(zio_arena, VMEM_FREE) < 21884309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 21894309Smaybee return (1); 21904309Smaybee #endif 21914309Smaybee 2192789Sahrens if (arc_reclaim_needed()) 2193789Sahrens return (1); 2194789Sahrens 21953403Sbmc return (arc_size > arc_c); 2196789Sahrens } 2197789Sahrens 2198789Sahrens /* 21992688Smaybee * The buffer, supplied as the first argument, needs a data block. 22002688Smaybee * So, if we are at cache max, determine which cache should be victimized. 22012688Smaybee * We have the following cases: 2202789Sahrens * 22033403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2204789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2205789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2206789Sahrens * 22073403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2208789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2209789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2210789Sahrens * entries. 2211789Sahrens * 22123403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2213789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2214789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2215789Sahrens * the MFU side, so the MRU side needs to be victimized. 2216789Sahrens * 22173403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2218789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2219789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2220789Sahrens */ 2221789Sahrens static void 22222688Smaybee arc_get_data_buf(arc_buf_t *buf) 2223789Sahrens { 22243290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 22253290Sjohansen uint64_t size = buf->b_hdr->b_size; 22263290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 22272688Smaybee 22282688Smaybee arc_adapt(size, state); 2229789Sahrens 22302688Smaybee /* 22312688Smaybee * We have not yet reached cache maximum size, 22322688Smaybee * just allocate a new buffer. 22332688Smaybee */ 22344309Smaybee if (!arc_evict_needed(type)) { 22353290Sjohansen if (type == ARC_BUFC_METADATA) { 22363290Sjohansen buf->b_data = zio_buf_alloc(size); 22378582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 22383290Sjohansen } else { 22393290Sjohansen ASSERT(type == ARC_BUFC_DATA); 22403290Sjohansen buf->b_data = zio_data_buf_alloc(size); 22418582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 22424309Smaybee atomic_add_64(&arc_size, size); 22433290Sjohansen } 22442688Smaybee goto out; 22452688Smaybee } 22462688Smaybee 22472688Smaybee /* 22482688Smaybee * If we are prefetching from the mfu ghost list, this buffer 22492688Smaybee * will end up on the mru list; so steal space from there. 22502688Smaybee */ 22513403Sbmc if (state == arc_mfu_ghost) 22523403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 22533403Sbmc else if (state == arc_mru_ghost) 22543403Sbmc state = arc_mru; 2255789Sahrens 22563403Sbmc if (state == arc_mru || state == arc_anon) { 22573403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 22588582SBrendan.Gregg@Sun.COM state = (arc_mfu->arcs_lsize[type] >= size && 22594309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2260789Sahrens } else { 22612688Smaybee /* MFU cases */ 22623403Sbmc uint64_t mfu_space = arc_c - arc_p; 22638582SBrendan.Gregg@Sun.COM state = (arc_mru->arcs_lsize[type] >= size && 22644309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 22652688Smaybee } 22665642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 22673290Sjohansen if (type == ARC_BUFC_METADATA) { 22683290Sjohansen buf->b_data = zio_buf_alloc(size); 22698582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 22703290Sjohansen } else { 22713290Sjohansen ASSERT(type == ARC_BUFC_DATA); 22723290Sjohansen buf->b_data = zio_data_buf_alloc(size); 22738582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 22744309Smaybee atomic_add_64(&arc_size, size); 22753290Sjohansen } 22763403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 22772688Smaybee } 22782688Smaybee ASSERT(buf->b_data != NULL); 22792688Smaybee out: 22802688Smaybee /* 22812688Smaybee * Update the state size. Note that ghost states have a 22822688Smaybee * "ghost size" and so don't need to be updated. 22832688Smaybee */ 22842688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 22852688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 22862688Smaybee 22873403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 22882688Smaybee if (list_link_active(&hdr->b_arc_node)) { 22892688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 22904309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2291789Sahrens } 22923298Smaybee /* 22933298Smaybee * If we are growing the cache, and we are adding anonymous 22943403Sbmc * data, and we have outgrown arc_p, update arc_p 22953298Smaybee */ 22963403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 22973403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 22983403Sbmc arc_p = MIN(arc_c, arc_p + size); 2299789Sahrens } 2300789Sahrens } 2301789Sahrens 2302789Sahrens /* 2303789Sahrens * This routine is called whenever a buffer is accessed. 23041544Seschrock * NOTE: the hash lock is dropped in this function. 2305789Sahrens */ 2306789Sahrens static void 23072688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2308789Sahrens { 230911066Srafael.vanoni@sun.com clock_t now; 231011066Srafael.vanoni@sun.com 2311789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2312789Sahrens 23133403Sbmc if (buf->b_state == arc_anon) { 2314789Sahrens /* 2315789Sahrens * This buffer is not in the cache, and does not 2316789Sahrens * appear in our "ghost" list. Add the new buffer 2317789Sahrens * to the MRU state. 2318789Sahrens */ 2319789Sahrens 2320789Sahrens ASSERT(buf->b_arc_access == 0); 232111066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 23221544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 23233403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2324789Sahrens 23253403Sbmc } else if (buf->b_state == arc_mru) { 232611066Srafael.vanoni@sun.com now = ddi_get_lbolt(); 232711066Srafael.vanoni@sun.com 2328789Sahrens /* 23292391Smaybee * If this buffer is here because of a prefetch, then either: 23302391Smaybee * - clear the flag if this is a "referencing" read 23312391Smaybee * (any subsequent access will bump this into the MFU state). 23322391Smaybee * or 23332391Smaybee * - move the buffer to the head of the list if this is 23342391Smaybee * another prefetch (to make it less likely to be evicted). 2335789Sahrens */ 2336789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 23372391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 23382391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23392391Smaybee } else { 23402391Smaybee buf->b_flags &= ~ARC_PREFETCH; 23413403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23422391Smaybee } 234311066Srafael.vanoni@sun.com buf->b_arc_access = now; 2344789Sahrens return; 2345789Sahrens } 2346789Sahrens 2347789Sahrens /* 2348789Sahrens * This buffer has been "accessed" only once so far, 2349789Sahrens * but it is still in the cache. Move it to the MFU 2350789Sahrens * state. 2351789Sahrens */ 235211066Srafael.vanoni@sun.com if (now > buf->b_arc_access + ARC_MINTIME) { 2353789Sahrens /* 2354789Sahrens * More than 125ms have passed since we 2355789Sahrens * instantiated this buffer. Move it to the 2356789Sahrens * most frequently used state. 2357789Sahrens */ 235811066Srafael.vanoni@sun.com buf->b_arc_access = now; 23591544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23603403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2361789Sahrens } 23623403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23633403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2364789Sahrens arc_state_t *new_state; 2365789Sahrens /* 2366789Sahrens * This buffer has been "accessed" recently, but 2367789Sahrens * was evicted from the cache. Move it to the 2368789Sahrens * MFU state. 2369789Sahrens */ 2370789Sahrens 2371789Sahrens if (buf->b_flags & ARC_PREFETCH) { 23723403Sbmc new_state = arc_mru; 23732391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 23742391Smaybee buf->b_flags &= ~ARC_PREFETCH; 23751544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2376789Sahrens } else { 23773403Sbmc new_state = arc_mfu; 23781544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2379789Sahrens } 2380789Sahrens 238111066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 2382789Sahrens arc_change_state(new_state, buf, hash_lock); 2383789Sahrens 23843403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 23853403Sbmc } else if (buf->b_state == arc_mfu) { 2386789Sahrens /* 2387789Sahrens * This buffer has been accessed more than once and is 2388789Sahrens * still in the cache. Keep it in the MFU state. 2389789Sahrens * 23902391Smaybee * NOTE: an add_reference() that occurred when we did 23912391Smaybee * the arc_read() will have kicked this off the list. 23922391Smaybee * If it was a prefetch, we will explicitly move it to 23932391Smaybee * the head of the list now. 2394789Sahrens */ 23952391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 23962391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 23972391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23982391Smaybee } 23993403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 240011066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 24013403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 24023403Sbmc arc_state_t *new_state = arc_mfu; 2403789Sahrens /* 2404789Sahrens * This buffer has been accessed more than once but has 2405789Sahrens * been evicted from the cache. Move it back to the 2406789Sahrens * MFU state. 2407789Sahrens */ 2408789Sahrens 24092391Smaybee if (buf->b_flags & ARC_PREFETCH) { 24102391Smaybee /* 24112391Smaybee * This is a prefetch access... 24122391Smaybee * move this block back to the MRU state. 24132391Smaybee */ 24142391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 24153403Sbmc new_state = arc_mru; 24162391Smaybee } 24172391Smaybee 241811066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 24191544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 24202391Smaybee arc_change_state(new_state, buf, hash_lock); 2421789Sahrens 24223403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 24235450Sbrendan } else if (buf->b_state == arc_l2c_only) { 24245450Sbrendan /* 24255450Sbrendan * This buffer is on the 2nd Level ARC. 24265450Sbrendan */ 24275450Sbrendan 242811066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 24295450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 24305450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2431789Sahrens } else { 2432789Sahrens ASSERT(!"invalid arc state"); 2433789Sahrens } 2434789Sahrens } 2435789Sahrens 2436789Sahrens /* a generic arc_done_func_t which you can use */ 2437789Sahrens /* ARGSUSED */ 2438789Sahrens void 2439789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2440789Sahrens { 2441789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 24421544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2443789Sahrens } 2444789Sahrens 24454309Smaybee /* a generic arc_done_func_t */ 2446789Sahrens void 2447789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2448789Sahrens { 2449789Sahrens arc_buf_t **bufp = arg; 2450789Sahrens if (zio && zio->io_error) { 24511544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2452789Sahrens *bufp = NULL; 2453789Sahrens } else { 2454789Sahrens *bufp = buf; 2455789Sahrens } 2456789Sahrens } 2457789Sahrens 2458789Sahrens static void 2459789Sahrens arc_read_done(zio_t *zio) 2460789Sahrens { 24611589Smaybee arc_buf_hdr_t *hdr, *found; 2462789Sahrens arc_buf_t *buf; 2463789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2464789Sahrens kmutex_t *hash_lock; 2465789Sahrens arc_callback_t *callback_list, *acb; 2466789Sahrens int freeable = FALSE; 2467789Sahrens 2468789Sahrens buf = zio->io_private; 2469789Sahrens hdr = buf->b_hdr; 2470789Sahrens 24711589Smaybee /* 24721589Smaybee * The hdr was inserted into hash-table and removed from lists 24731589Smaybee * prior to starting I/O. We should find this header, since 24741589Smaybee * it's in the hash table, and it should be legit since it's 24751589Smaybee * not possible to evict it during the I/O. The only possible 24761589Smaybee * reason for it not to be found is if we were freed during the 24771589Smaybee * read. 24781589Smaybee */ 24798636SMark.Maybee@Sun.COM found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 24803093Sahrens &hash_lock); 2481789Sahrens 24821589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 24835450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 24845450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 24855450Sbrendan 24866987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 24875450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 24887237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2489789Sahrens 2490789Sahrens /* byteswap if necessary */ 2491789Sahrens callback_list = hdr->b_acb; 2492789Sahrens ASSERT(callback_list != NULL); 249310839Swilliam.gorrell@sun.com if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 24947046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 24957046Sahrens byteswap_uint64_array : 24967046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 24977046Sahrens func(buf->b_data, hdr->b_size); 24987046Sahrens } 2499789Sahrens 25005450Sbrendan arc_cksum_compute(buf, B_FALSE); 25013093Sahrens 250210922SJeff.Bonwick@Sun.COM if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 250310922SJeff.Bonwick@Sun.COM /* 250410922SJeff.Bonwick@Sun.COM * Only call arc_access on anonymous buffers. This is because 250510922SJeff.Bonwick@Sun.COM * if we've issued an I/O for an evicted buffer, we've already 250610922SJeff.Bonwick@Sun.COM * called arc_access (to prevent any simultaneous readers from 250710922SJeff.Bonwick@Sun.COM * getting confused). 250810922SJeff.Bonwick@Sun.COM */ 250910922SJeff.Bonwick@Sun.COM arc_access(hdr, hash_lock); 251010922SJeff.Bonwick@Sun.COM } 251110922SJeff.Bonwick@Sun.COM 2512789Sahrens /* create copies of the data buffer for the callers */ 2513789Sahrens abuf = buf; 2514789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2515789Sahrens if (acb->acb_done) { 25162688Smaybee if (abuf == NULL) 25172688Smaybee abuf = arc_buf_clone(buf); 2518789Sahrens acb->acb_buf = abuf; 2519789Sahrens abuf = NULL; 2520789Sahrens } 2521789Sahrens } 2522789Sahrens hdr->b_acb = NULL; 2523789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 25241544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 252510922SJeff.Bonwick@Sun.COM if (abuf == buf) { 252610922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 252710922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 25281544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 252910922SJeff.Bonwick@Sun.COM } 2530789Sahrens 2531789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2532789Sahrens 2533789Sahrens if (zio->io_error != 0) { 2534789Sahrens hdr->b_flags |= ARC_IO_ERROR; 25353403Sbmc if (hdr->b_state != arc_anon) 25363403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 25371544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 25381544Seschrock buf_hash_remove(hdr); 2539789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2540789Sahrens } 2541789Sahrens 25421544Seschrock /* 25432391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 25442391Smaybee * that the hdr (and hence the cv) might be freed before we get to 25452391Smaybee * the cv_broadcast(). 25461544Seschrock */ 25471544Seschrock cv_broadcast(&hdr->b_cv); 25481544Seschrock 25491589Smaybee if (hash_lock) { 25502688Smaybee mutex_exit(hash_lock); 2551789Sahrens } else { 2552789Sahrens /* 2553789Sahrens * This block was freed while we waited for the read to 2554789Sahrens * complete. It has been removed from the hash table and 2555789Sahrens * moved to the anonymous state (so that it won't show up 2556789Sahrens * in the cache). 2557789Sahrens */ 25583403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2559789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2560789Sahrens } 2561789Sahrens 2562789Sahrens /* execute each callback and free its structure */ 2563789Sahrens while ((acb = callback_list) != NULL) { 2564789Sahrens if (acb->acb_done) 2565789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2566789Sahrens 2567789Sahrens if (acb->acb_zio_dummy != NULL) { 2568789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2569789Sahrens zio_nowait(acb->acb_zio_dummy); 2570789Sahrens } 2571789Sahrens 2572789Sahrens callback_list = acb->acb_next; 2573789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2574789Sahrens } 2575789Sahrens 2576789Sahrens if (freeable) 25771544Seschrock arc_hdr_destroy(hdr); 2578789Sahrens } 2579789Sahrens 2580789Sahrens /* 2581789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2582789Sahrens * cache. If the block is found in the cache, invoke the provided 2583789Sahrens * callback immediately and return. Note that the `zio' parameter 2584789Sahrens * in the callback will be NULL in this case, since no IO was 2585789Sahrens * required. If the block is not in the cache pass the read request 2586789Sahrens * on to the spa with a substitute callback function, so that the 2587789Sahrens * requested block will be added to the cache. 2588789Sahrens * 2589789Sahrens * If a read request arrives for a block that has a read in-progress, 2590789Sahrens * either wait for the in-progress read to complete (and return the 2591789Sahrens * results); or, if this is a read with a "done" func, add a record 2592789Sahrens * to the read to invoke the "done" func when the read completes, 2593789Sahrens * and return; or just return. 2594789Sahrens * 2595789Sahrens * arc_read_done() will invoke all the requested "done" functions 2596789Sahrens * for readers of this block. 25977046Sahrens * 25987046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 25997046Sahrens * for the bp. But if you know you don't need locking, you can use 26008213SSuhasini.Peddada@Sun.COM * arc_read_bp. 2601789Sahrens */ 2602789Sahrens int 260310922SJeff.Bonwick@Sun.COM arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf, 26047237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 26057046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 26067046Sahrens { 26077046Sahrens int err; 26087046Sahrens 26097046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 26107046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 26117545SMark.Maybee@Sun.COM rw_enter(&pbuf->b_lock, RW_READER); 26127046Sahrens 26137046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 26147237Sek110237 zio_flags, arc_flags, zb); 26157545SMark.Maybee@Sun.COM rw_exit(&pbuf->b_lock); 26169396SMatthew.Ahrens@Sun.COM 26177046Sahrens return (err); 26187046Sahrens } 26197046Sahrens 26207046Sahrens int 262110922SJeff.Bonwick@Sun.COM arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp, 26227237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 26237046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2624789Sahrens { 2625789Sahrens arc_buf_hdr_t *hdr; 2626789Sahrens arc_buf_t *buf; 2627789Sahrens kmutex_t *hash_lock; 26285450Sbrendan zio_t *rzio; 26298636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 2630789Sahrens 2631789Sahrens top: 263210922SJeff.Bonwick@Sun.COM hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 263310922SJeff.Bonwick@Sun.COM &hash_lock); 26341544Seschrock if (hdr && hdr->b_datacnt > 0) { 2635789Sahrens 26362391Smaybee *arc_flags |= ARC_CACHED; 26372391Smaybee 2638789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 26392391Smaybee 26402391Smaybee if (*arc_flags & ARC_WAIT) { 26412391Smaybee cv_wait(&hdr->b_cv, hash_lock); 26422391Smaybee mutex_exit(hash_lock); 26432391Smaybee goto top; 26442391Smaybee } 26452391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 26462391Smaybee 26472391Smaybee if (done) { 2648789Sahrens arc_callback_t *acb = NULL; 2649789Sahrens 2650789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2651789Sahrens KM_SLEEP); 2652789Sahrens acb->acb_done = done; 2653789Sahrens acb->acb_private = private; 2654789Sahrens if (pio != NULL) 2655789Sahrens acb->acb_zio_dummy = zio_null(pio, 26568632SBill.Moore@Sun.COM spa, NULL, NULL, NULL, zio_flags); 2657789Sahrens 2658789Sahrens ASSERT(acb->acb_done != NULL); 2659789Sahrens acb->acb_next = hdr->b_acb; 2660789Sahrens hdr->b_acb = acb; 2661789Sahrens add_reference(hdr, hash_lock, private); 2662789Sahrens mutex_exit(hash_lock); 2663789Sahrens return (0); 2664789Sahrens } 2665789Sahrens mutex_exit(hash_lock); 2666789Sahrens return (0); 2667789Sahrens } 2668789Sahrens 26693403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2670789Sahrens 26711544Seschrock if (done) { 26722688Smaybee add_reference(hdr, hash_lock, private); 26731544Seschrock /* 26741544Seschrock * If this block is already in use, create a new 26751544Seschrock * copy of the data so that we will be guaranteed 26761544Seschrock * that arc_release() will always succeed. 26771544Seschrock */ 26781544Seschrock buf = hdr->b_buf; 26791544Seschrock ASSERT(buf); 26801544Seschrock ASSERT(buf->b_data); 26812688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 26821544Seschrock ASSERT(buf->b_efunc == NULL); 26831544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 26842688Smaybee } else { 26852688Smaybee buf = arc_buf_clone(buf); 26861544Seschrock } 268710922SJeff.Bonwick@Sun.COM 26882391Smaybee } else if (*arc_flags & ARC_PREFETCH && 26892391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 26902391Smaybee hdr->b_flags |= ARC_PREFETCH; 2691789Sahrens } 2692789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 26932688Smaybee arc_access(hdr, hash_lock); 26947237Sek110237 if (*arc_flags & ARC_L2CACHE) 26957237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26962688Smaybee mutex_exit(hash_lock); 26973403Sbmc ARCSTAT_BUMP(arcstat_hits); 26983403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 26993403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 27003403Sbmc data, metadata, hits); 27013403Sbmc 2702789Sahrens if (done) 2703789Sahrens done(NULL, buf, private); 2704789Sahrens } else { 2705789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2706789Sahrens arc_callback_t *acb; 27076987Sbrendan vdev_t *vd = NULL; 27089215SGeorge.Wilson@Sun.COM uint64_t addr; 27098582SBrendan.Gregg@Sun.COM boolean_t devw = B_FALSE; 2710789Sahrens 2711789Sahrens if (hdr == NULL) { 2712789Sahrens /* this block is not in the cache */ 2713789Sahrens arc_buf_hdr_t *exists; 27143290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 27153290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2716789Sahrens hdr = buf->b_hdr; 2717789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 271810922SJeff.Bonwick@Sun.COM hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 2719789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2720789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2721789Sahrens if (exists) { 2722789Sahrens /* somebody beat us to the hash insert */ 2723789Sahrens mutex_exit(hash_lock); 2724789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2725789Sahrens hdr->b_birth = 0; 2726789Sahrens hdr->b_cksum0 = 0; 27271544Seschrock (void) arc_buf_remove_ref(buf, private); 2728789Sahrens goto top; /* restart the IO request */ 2729789Sahrens } 27302391Smaybee /* if this is a prefetch, we don't have a reference */ 27312391Smaybee if (*arc_flags & ARC_PREFETCH) { 27322391Smaybee (void) remove_reference(hdr, hash_lock, 27332391Smaybee private); 27342391Smaybee hdr->b_flags |= ARC_PREFETCH; 27352391Smaybee } 27367237Sek110237 if (*arc_flags & ARC_L2CACHE) 27377237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27382391Smaybee if (BP_GET_LEVEL(bp) > 0) 27392391Smaybee hdr->b_flags |= ARC_INDIRECT; 2740789Sahrens } else { 2741789Sahrens /* this block is in the ghost cache */ 27421544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 27431544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 27442391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 27452391Smaybee ASSERT(hdr->b_buf == NULL); 2746789Sahrens 27472391Smaybee /* if this is a prefetch, we don't have a reference */ 27482391Smaybee if (*arc_flags & ARC_PREFETCH) 27492391Smaybee hdr->b_flags |= ARC_PREFETCH; 27502391Smaybee else 27512391Smaybee add_reference(hdr, hash_lock, private); 27527237Sek110237 if (*arc_flags & ARC_L2CACHE) 27537237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27546245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 27551544Seschrock buf->b_hdr = hdr; 27562688Smaybee buf->b_data = NULL; 27571544Seschrock buf->b_efunc = NULL; 27581544Seschrock buf->b_private = NULL; 27591544Seschrock buf->b_next = NULL; 27601544Seschrock hdr->b_buf = buf; 27611544Seschrock ASSERT(hdr->b_datacnt == 0); 27621544Seschrock hdr->b_datacnt = 1; 2763*12033Swilliam.gorrell@sun.com arc_get_data_buf(buf); 276411805Swilliam.gorrell@sun.com arc_access(hdr, hash_lock); 2765789Sahrens } 2766789Sahrens 276711805Swilliam.gorrell@sun.com ASSERT(!GHOST_STATE(hdr->b_state)); 276811805Swilliam.gorrell@sun.com 2769789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2770789Sahrens acb->acb_done = done; 2771789Sahrens acb->acb_private = private; 2772789Sahrens 2773789Sahrens ASSERT(hdr->b_acb == NULL); 2774789Sahrens hdr->b_acb = acb; 2775789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2776789Sahrens 27777754SJeff.Bonwick@Sun.COM if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 27787754SJeff.Bonwick@Sun.COM (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 27798582SBrendan.Gregg@Sun.COM devw = hdr->b_l2hdr->b_dev->l2ad_writing; 27806987Sbrendan addr = hdr->b_l2hdr->b_daddr; 27817754SJeff.Bonwick@Sun.COM /* 27827754SJeff.Bonwick@Sun.COM * Lock out device removal. 27837754SJeff.Bonwick@Sun.COM */ 27847754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || 27857754SJeff.Bonwick@Sun.COM !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 27867754SJeff.Bonwick@Sun.COM vd = NULL; 27876987Sbrendan } 27886987Sbrendan 27896987Sbrendan mutex_exit(hash_lock); 27906987Sbrendan 2791789Sahrens ASSERT3U(hdr->b_size, ==, size); 279210409SBrendan.Gregg@Sun.COM DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 279310409SBrendan.Gregg@Sun.COM uint64_t, size, zbookmark_t *, zb); 27943403Sbmc ARCSTAT_BUMP(arcstat_misses); 27953403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 27963403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 27973403Sbmc data, metadata, misses); 27981544Seschrock 27998582SBrendan.Gregg@Sun.COM if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 28006987Sbrendan /* 28016987Sbrendan * Read from the L2ARC if the following are true: 28026987Sbrendan * 1. The L2ARC vdev was previously cached. 28036987Sbrendan * 2. This buffer still has L2ARC metadata. 28046987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 28056987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 28066987Sbrendan * also have invalidated the vdev. 28078582SBrendan.Gregg@Sun.COM * 5. This isn't prefetch and l2arc_noprefetch is set. 28086987Sbrendan */ 28097754SJeff.Bonwick@Sun.COM if (hdr->b_l2hdr != NULL && 28108582SBrendan.Gregg@Sun.COM !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 28118582SBrendan.Gregg@Sun.COM !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 28125450Sbrendan l2arc_read_callback_t *cb; 28135450Sbrendan 28146643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 28156643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 28166643Seschrock 28175450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 28185450Sbrendan KM_SLEEP); 28195450Sbrendan cb->l2rcb_buf = buf; 28205450Sbrendan cb->l2rcb_spa = spa; 28215450Sbrendan cb->l2rcb_bp = *bp; 28225450Sbrendan cb->l2rcb_zb = *zb; 28237237Sek110237 cb->l2rcb_flags = zio_flags; 28245450Sbrendan 28255450Sbrendan /* 28267754SJeff.Bonwick@Sun.COM * l2arc read. The SCL_L2ARC lock will be 28277754SJeff.Bonwick@Sun.COM * released by l2arc_read_done(). 28285450Sbrendan */ 28295450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 28305450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 28317237Sek110237 l2arc_read_done, cb, priority, zio_flags | 28327361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 28337754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_PROPAGATE | 28347754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_RETRY, B_FALSE); 28355450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 28365450Sbrendan zio_t *, rzio); 28378582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_read_bytes, size); 28386987Sbrendan 28396987Sbrendan if (*arc_flags & ARC_NOWAIT) { 28406987Sbrendan zio_nowait(rzio); 28416987Sbrendan return (0); 28426987Sbrendan } 28436987Sbrendan 28446987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 28456987Sbrendan if (zio_wait(rzio) == 0) 28466987Sbrendan return (0); 28476987Sbrendan 28486987Sbrendan /* l2arc read error; goto zio_read() */ 28495450Sbrendan } else { 28505450Sbrendan DTRACE_PROBE1(l2arc__miss, 28515450Sbrendan arc_buf_hdr_t *, hdr); 28525450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 28535450Sbrendan if (HDR_L2_WRITING(hdr)) 28545450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 28557754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 28565450Sbrendan } 28578582SBrendan.Gregg@Sun.COM } else { 28588628SBill.Moore@Sun.COM if (vd != NULL) 28598628SBill.Moore@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 28608582SBrendan.Gregg@Sun.COM if (l2arc_ndev != 0) { 28618582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(l2arc__miss, 28628582SBrendan.Gregg@Sun.COM arc_buf_hdr_t *, hdr); 28638582SBrendan.Gregg@Sun.COM ARCSTAT_BUMP(arcstat_l2_misses); 28648582SBrendan.Gregg@Sun.COM } 28655450Sbrendan } 28666643Seschrock 2867789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 28687237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2869789Sahrens 28702391Smaybee if (*arc_flags & ARC_WAIT) 2871789Sahrens return (zio_wait(rzio)); 2872789Sahrens 28732391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2874789Sahrens zio_nowait(rzio); 2875789Sahrens } 2876789Sahrens return (0); 2877789Sahrens } 2878789Sahrens 28791544Seschrock void 28801544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 28811544Seschrock { 28821544Seschrock ASSERT(buf->b_hdr != NULL); 28833403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 28841544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 288510922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 288610922SJeff.Bonwick@Sun.COM ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 288710922SJeff.Bonwick@Sun.COM 28881544Seschrock buf->b_efunc = func; 28891544Seschrock buf->b_private = private; 28901544Seschrock } 28911544Seschrock 28921544Seschrock /* 28931544Seschrock * This is used by the DMU to let the ARC know that a buffer is 28941544Seschrock * being evicted, so the ARC should clean up. If this arc buf 28951544Seschrock * is not yet in the evicted state, it will be put there. 28961544Seschrock */ 28971544Seschrock int 28981544Seschrock arc_buf_evict(arc_buf_t *buf) 28991544Seschrock { 29002887Smaybee arc_buf_hdr_t *hdr; 29011544Seschrock kmutex_t *hash_lock; 29021544Seschrock arc_buf_t **bufp; 29031544Seschrock 29047545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 29052887Smaybee hdr = buf->b_hdr; 29061544Seschrock if (hdr == NULL) { 29071544Seschrock /* 29081544Seschrock * We are in arc_do_user_evicts(). 29091544Seschrock */ 29101544Seschrock ASSERT(buf->b_data == NULL); 29117545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29121544Seschrock return (0); 29137545SMark.Maybee@Sun.COM } else if (buf->b_data == NULL) { 29147545SMark.Maybee@Sun.COM arc_buf_t copy = *buf; /* structure assignment */ 29157545SMark.Maybee@Sun.COM /* 29167545SMark.Maybee@Sun.COM * We are on the eviction list; process this buffer now 29177545SMark.Maybee@Sun.COM * but let arc_do_user_evicts() do the reaping. 29187545SMark.Maybee@Sun.COM */ 29197545SMark.Maybee@Sun.COM buf->b_efunc = NULL; 29207545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29217545SMark.Maybee@Sun.COM VERIFY(copy.b_efunc(©) == 0); 29227545SMark.Maybee@Sun.COM return (1); 29231544Seschrock } 29242887Smaybee hash_lock = HDR_LOCK(hdr); 29251544Seschrock mutex_enter(hash_lock); 29261544Seschrock 29272724Smaybee ASSERT(buf->b_hdr == hdr); 29282724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 29293403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 29301544Seschrock 29311544Seschrock /* 29321544Seschrock * Pull this buffer off of the hdr 29331544Seschrock */ 29341544Seschrock bufp = &hdr->b_buf; 29351544Seschrock while (*bufp != buf) 29361544Seschrock bufp = &(*bufp)->b_next; 29371544Seschrock *bufp = buf->b_next; 29381544Seschrock 29391544Seschrock ASSERT(buf->b_data != NULL); 29402688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 29411544Seschrock 29421544Seschrock if (hdr->b_datacnt == 0) { 29431544Seschrock arc_state_t *old_state = hdr->b_state; 29441544Seschrock arc_state_t *evicted_state; 29451544Seschrock 29461544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 29471544Seschrock 29481544Seschrock evicted_state = 29493403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 29501544Seschrock 29513403Sbmc mutex_enter(&old_state->arcs_mtx); 29523403Sbmc mutex_enter(&evicted_state->arcs_mtx); 29531544Seschrock 29541544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 29551544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 29565450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 29575450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 29581544Seschrock 29593403Sbmc mutex_exit(&evicted_state->arcs_mtx); 29603403Sbmc mutex_exit(&old_state->arcs_mtx); 29611544Seschrock } 29621544Seschrock mutex_exit(hash_lock); 29637545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29641819Smaybee 29651544Seschrock VERIFY(buf->b_efunc(buf) == 0); 29661544Seschrock buf->b_efunc = NULL; 29671544Seschrock buf->b_private = NULL; 29681544Seschrock buf->b_hdr = NULL; 29691544Seschrock kmem_cache_free(buf_cache, buf); 29701544Seschrock return (1); 29711544Seschrock } 29721544Seschrock 2973789Sahrens /* 2974789Sahrens * Release this buffer from the cache. This must be done 2975789Sahrens * after a read and prior to modifying the buffer contents. 2976789Sahrens * If the buffer has more than one reference, we must make 29777046Sahrens * a new hdr for the buffer. 2978789Sahrens */ 2979789Sahrens void 2980789Sahrens arc_release(arc_buf_t *buf, void *tag) 2981789Sahrens { 29827545SMark.Maybee@Sun.COM arc_buf_hdr_t *hdr; 29837545SMark.Maybee@Sun.COM kmutex_t *hash_lock; 29847545SMark.Maybee@Sun.COM l2arc_buf_hdr_t *l2hdr; 29855450Sbrendan uint64_t buf_size; 29869274SBrendan.Gregg@Sun.COM boolean_t released = B_FALSE; 2987789Sahrens 29887545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 29897545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 29907545SMark.Maybee@Sun.COM 2991789Sahrens /* this buffer is not on any list */ 2992789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 2993789Sahrens 29943403Sbmc if (hdr->b_state == arc_anon) { 2995789Sahrens /* this buffer is already released */ 2996789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2997789Sahrens ASSERT(BUF_EMPTY(hdr)); 29981544Seschrock ASSERT(buf->b_efunc == NULL); 29993093Sahrens arc_buf_thaw(buf); 30007545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30019274SBrendan.Gregg@Sun.COM released = B_TRUE; 30029274SBrendan.Gregg@Sun.COM } else { 30039274SBrendan.Gregg@Sun.COM hash_lock = HDR_LOCK(hdr); 30049274SBrendan.Gregg@Sun.COM mutex_enter(hash_lock); 3005789Sahrens } 3006789Sahrens 30077545SMark.Maybee@Sun.COM l2hdr = hdr->b_l2hdr; 30087545SMark.Maybee@Sun.COM if (l2hdr) { 30097545SMark.Maybee@Sun.COM mutex_enter(&l2arc_buflist_mtx); 30107545SMark.Maybee@Sun.COM hdr->b_l2hdr = NULL; 30117545SMark.Maybee@Sun.COM buf_size = hdr->b_size; 30127545SMark.Maybee@Sun.COM } 30137545SMark.Maybee@Sun.COM 30149274SBrendan.Gregg@Sun.COM if (released) 30159274SBrendan.Gregg@Sun.COM goto out; 30169274SBrendan.Gregg@Sun.COM 30171544Seschrock /* 30181544Seschrock * Do we have more than one buf? 30191544Seschrock */ 30207545SMark.Maybee@Sun.COM if (hdr->b_datacnt > 1) { 3021789Sahrens arc_buf_hdr_t *nhdr; 3022789Sahrens arc_buf_t **bufp; 3023789Sahrens uint64_t blksz = hdr->b_size; 30248636SMark.Maybee@Sun.COM uint64_t spa = hdr->b_spa; 30253290Sjohansen arc_buf_contents_t type = hdr->b_type; 30265450Sbrendan uint32_t flags = hdr->b_flags; 3027789Sahrens 30287545SMark.Maybee@Sun.COM ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3029789Sahrens /* 3030789Sahrens * Pull the data off of this buf and attach it to 3031789Sahrens * a new anonymous buf. 3032789Sahrens */ 30331544Seschrock (void) remove_reference(hdr, hash_lock, tag); 3034789Sahrens bufp = &hdr->b_buf; 30351544Seschrock while (*bufp != buf) 3036789Sahrens bufp = &(*bufp)->b_next; 3037789Sahrens *bufp = (*bufp)->b_next; 30383897Smaybee buf->b_next = NULL; 30391544Seschrock 30403403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 30413403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 30421544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 30434309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 30444309Smaybee ASSERT3U(*size, >=, hdr->b_size); 30454309Smaybee atomic_add_64(size, -hdr->b_size); 30461544Seschrock } 30471544Seschrock hdr->b_datacnt -= 1; 30483547Smaybee arc_cksum_verify(buf); 30491544Seschrock 3050789Sahrens mutex_exit(hash_lock); 3051789Sahrens 30526245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3053789Sahrens nhdr->b_size = blksz; 3054789Sahrens nhdr->b_spa = spa; 30553290Sjohansen nhdr->b_type = type; 3056789Sahrens nhdr->b_buf = buf; 30573403Sbmc nhdr->b_state = arc_anon; 3058789Sahrens nhdr->b_arc_access = 0; 30595450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 30605450Sbrendan nhdr->b_l2hdr = NULL; 30611544Seschrock nhdr->b_datacnt = 1; 30623547Smaybee nhdr->b_freeze_cksum = NULL; 30633897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 3064789Sahrens buf->b_hdr = nhdr; 30657545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30663403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 3067789Sahrens } else { 30687545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30691544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3070789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 3071789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 30723403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 3073789Sahrens hdr->b_arc_access = 0; 3074789Sahrens mutex_exit(hash_lock); 30755450Sbrendan 3076789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 3077789Sahrens hdr->b_birth = 0; 3078789Sahrens hdr->b_cksum0 = 0; 30793547Smaybee arc_buf_thaw(buf); 3080789Sahrens } 30811544Seschrock buf->b_efunc = NULL; 30821544Seschrock buf->b_private = NULL; 30835450Sbrendan 30849274SBrendan.Gregg@Sun.COM out: 30855450Sbrendan if (l2hdr) { 30865450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 30875450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 30885450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 30897545SMark.Maybee@Sun.COM mutex_exit(&l2arc_buflist_mtx); 30905450Sbrendan } 3091789Sahrens } 3092789Sahrens 3093789Sahrens int 3094789Sahrens arc_released(arc_buf_t *buf) 3095789Sahrens { 30967545SMark.Maybee@Sun.COM int released; 30977545SMark.Maybee@Sun.COM 30987545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30997545SMark.Maybee@Sun.COM released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 31007545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 31017545SMark.Maybee@Sun.COM return (released); 31021544Seschrock } 31031544Seschrock 31041544Seschrock int 31051544Seschrock arc_has_callback(arc_buf_t *buf) 31061544Seschrock { 31077545SMark.Maybee@Sun.COM int callback; 31087545SMark.Maybee@Sun.COM 31097545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 31107545SMark.Maybee@Sun.COM callback = (buf->b_efunc != NULL); 31117545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 31127545SMark.Maybee@Sun.COM return (callback); 3113789Sahrens } 3114789Sahrens 31151544Seschrock #ifdef ZFS_DEBUG 31161544Seschrock int 31171544Seschrock arc_referenced(arc_buf_t *buf) 31181544Seschrock { 31197545SMark.Maybee@Sun.COM int referenced; 31207545SMark.Maybee@Sun.COM 31217545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 31227545SMark.Maybee@Sun.COM referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 31237545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 31247545SMark.Maybee@Sun.COM return (referenced); 31251544Seschrock } 31261544Seschrock #endif 31271544Seschrock 3128789Sahrens static void 31293547Smaybee arc_write_ready(zio_t *zio) 31303547Smaybee { 31313547Smaybee arc_write_callback_t *callback = zio->io_private; 31323547Smaybee arc_buf_t *buf = callback->awcb_buf; 31335329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 31345329Sgw25295 31357754SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 31367754SJeff.Bonwick@Sun.COM callback->awcb_ready(zio, buf, callback->awcb_private); 31377754SJeff.Bonwick@Sun.COM 31385329Sgw25295 /* 31395329Sgw25295 * If the IO is already in progress, then this is a re-write 31407754SJeff.Bonwick@Sun.COM * attempt, so we need to thaw and re-compute the cksum. 31417754SJeff.Bonwick@Sun.COM * It is the responsibility of the callback to handle the 31427754SJeff.Bonwick@Sun.COM * accounting for any re-write attempt. 31435329Sgw25295 */ 31445329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 31455329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 31465329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 31475329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 31485329Sgw25295 hdr->b_freeze_cksum = NULL; 31495329Sgw25295 } 31505329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 31515329Sgw25295 } 31525450Sbrendan arc_cksum_compute(buf, B_FALSE); 31535329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 31543547Smaybee } 31553547Smaybee 31563547Smaybee static void 3157789Sahrens arc_write_done(zio_t *zio) 3158789Sahrens { 31593547Smaybee arc_write_callback_t *callback = zio->io_private; 31603547Smaybee arc_buf_t *buf = callback->awcb_buf; 31613547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3162789Sahrens 316310922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_acb == NULL); 316410922SJeff.Bonwick@Sun.COM 316510922SJeff.Bonwick@Sun.COM if (zio->io_error == 0) { 316610922SJeff.Bonwick@Sun.COM hdr->b_dva = *BP_IDENTITY(zio->io_bp); 316710922SJeff.Bonwick@Sun.COM hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 316810922SJeff.Bonwick@Sun.COM hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 316910922SJeff.Bonwick@Sun.COM } else { 317010922SJeff.Bonwick@Sun.COM ASSERT(BUF_EMPTY(hdr)); 317110922SJeff.Bonwick@Sun.COM } 317210922SJeff.Bonwick@Sun.COM 31731544Seschrock /* 31741544Seschrock * If the block to be written was all-zero, we may have 31751544Seschrock * compressed it away. In this case no write was performed 31761544Seschrock * so there will be no dva/birth-date/checksum. The buffer 31771544Seschrock * must therefor remain anonymous (and uncached). 31781544Seschrock */ 3179789Sahrens if (!BUF_EMPTY(hdr)) { 3180789Sahrens arc_buf_hdr_t *exists; 3181789Sahrens kmutex_t *hash_lock; 3182789Sahrens 318310922SJeff.Bonwick@Sun.COM ASSERT(zio->io_error == 0); 318410922SJeff.Bonwick@Sun.COM 31853093Sahrens arc_cksum_verify(buf); 31863093Sahrens 3187789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3188789Sahrens if (exists) { 3189789Sahrens /* 3190789Sahrens * This can only happen if we overwrite for 3191789Sahrens * sync-to-convergence, because we remove 3192789Sahrens * buffers from the hash table when we arc_free(). 3193789Sahrens */ 319410922SJeff.Bonwick@Sun.COM if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 319510922SJeff.Bonwick@Sun.COM if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 319610922SJeff.Bonwick@Sun.COM panic("bad overwrite, hdr=%p exists=%p", 319710922SJeff.Bonwick@Sun.COM (void *)hdr, (void *)exists); 319810922SJeff.Bonwick@Sun.COM ASSERT(refcount_is_zero(&exists->b_refcnt)); 319910922SJeff.Bonwick@Sun.COM arc_change_state(arc_anon, exists, hash_lock); 320010922SJeff.Bonwick@Sun.COM mutex_exit(hash_lock); 320110922SJeff.Bonwick@Sun.COM arc_hdr_destroy(exists); 320210922SJeff.Bonwick@Sun.COM exists = buf_hash_insert(hdr, &hash_lock); 320310922SJeff.Bonwick@Sun.COM ASSERT3P(exists, ==, NULL); 320410922SJeff.Bonwick@Sun.COM } else { 320510922SJeff.Bonwick@Sun.COM /* Dedup */ 320610922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 320710922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_state == arc_anon); 320810922SJeff.Bonwick@Sun.COM ASSERT(BP_GET_DEDUP(zio->io_bp)); 320910922SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 321010272SMatthew.Ahrens@Sun.COM } 3211789Sahrens } 32121544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 32137046Sahrens /* if it's not anon, we are doing a scrub */ 321410922SJeff.Bonwick@Sun.COM if (!exists && hdr->b_state == arc_anon) 32157046Sahrens arc_access(hdr, hash_lock); 32162688Smaybee mutex_exit(hash_lock); 32171544Seschrock } else { 32181544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3219789Sahrens } 322010922SJeff.Bonwick@Sun.COM 322110922SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 322210922SJeff.Bonwick@Sun.COM callback->awcb_done(zio, buf, callback->awcb_private); 3223789Sahrens 32243547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3225789Sahrens } 3226789Sahrens 32273547Smaybee zio_t * 322810922SJeff.Bonwick@Sun.COM arc_write(zio_t *pio, spa_t *spa, uint64_t txg, 322910922SJeff.Bonwick@Sun.COM blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp, 323010922SJeff.Bonwick@Sun.COM arc_done_func_t *ready, arc_done_func_t *done, void *private, 323110922SJeff.Bonwick@Sun.COM int priority, int zio_flags, const zbookmark_t *zb) 3232789Sahrens { 3233789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 32343547Smaybee arc_write_callback_t *callback; 32357754SJeff.Bonwick@Sun.COM zio_t *zio; 32367754SJeff.Bonwick@Sun.COM 32377754SJeff.Bonwick@Sun.COM ASSERT(ready != NULL); 323810922SJeff.Bonwick@Sun.COM ASSERT(done != NULL); 3239789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 32402237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 324110922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_acb == NULL); 32427237Sek110237 if (l2arc) 32437237Sek110237 hdr->b_flags |= ARC_L2CACHE; 32443547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 32453547Smaybee callback->awcb_ready = ready; 32463547Smaybee callback->awcb_done = done; 32473547Smaybee callback->awcb_private = private; 32483547Smaybee callback->awcb_buf = buf; 32497046Sahrens 325010922SJeff.Bonwick@Sun.COM zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 32517754SJeff.Bonwick@Sun.COM arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3252789Sahrens 32533547Smaybee return (zio); 3254789Sahrens } 3255789Sahrens 325610922SJeff.Bonwick@Sun.COM void 325710922SJeff.Bonwick@Sun.COM arc_free(spa_t *spa, const blkptr_t *bp) 3258789Sahrens { 3259789Sahrens arc_buf_hdr_t *ab; 3260789Sahrens kmutex_t *hash_lock; 32618636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 3262789Sahrens 3263789Sahrens /* 326410922SJeff.Bonwick@Sun.COM * If this buffer is in the cache, release it, so it can be re-used. 3265789Sahrens */ 326610922SJeff.Bonwick@Sun.COM ab = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 326710922SJeff.Bonwick@Sun.COM &hash_lock); 3268789Sahrens if (ab != NULL) { 32693403Sbmc if (ab->b_state != arc_anon) 32703403Sbmc arc_change_state(arc_anon, ab, hash_lock); 32712391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 32722391Smaybee /* 32732391Smaybee * This should only happen when we prefetch. 32742391Smaybee */ 32752391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 32762391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 32772391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 32782391Smaybee if (HDR_IN_HASH_TABLE(ab)) 32792391Smaybee buf_hash_remove(ab); 32802391Smaybee ab->b_arc_access = 0; 32812391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 32822391Smaybee ab->b_birth = 0; 32832391Smaybee ab->b_cksum0 = 0; 32842391Smaybee ab->b_buf->b_efunc = NULL; 32852391Smaybee ab->b_buf->b_private = NULL; 32862391Smaybee mutex_exit(hash_lock); 328710922SJeff.Bonwick@Sun.COM } else { 328810922SJeff.Bonwick@Sun.COM ASSERT(refcount_is_zero(&ab->b_refcnt)); 32895450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3290789Sahrens mutex_exit(hash_lock); 32911544Seschrock arc_hdr_destroy(ab); 32923403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3293789Sahrens } 3294789Sahrens } 3295789Sahrens } 3296789Sahrens 32976245Smaybee static int 32989412SAleksandr.Guzovskiy@Sun.COM arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg) 32996245Smaybee { 33006245Smaybee #ifdef _KERNEL 33016245Smaybee uint64_t available_memory = ptob(freemem); 33026245Smaybee static uint64_t page_load = 0; 33036245Smaybee static uint64_t last_txg = 0; 33046245Smaybee 33056245Smaybee #if defined(__i386) 33066245Smaybee available_memory = 33076245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 33086245Smaybee #endif 33096245Smaybee if (available_memory >= zfs_write_limit_max) 33106245Smaybee return (0); 33116245Smaybee 33126245Smaybee if (txg > last_txg) { 33136245Smaybee last_txg = txg; 33146245Smaybee page_load = 0; 33156245Smaybee } 33166245Smaybee /* 33176245Smaybee * If we are in pageout, we know that memory is already tight, 33186245Smaybee * the arc is already going to be evicting, so we just want to 33196245Smaybee * continue to let page writes occur as quickly as possible. 33206245Smaybee */ 33216245Smaybee if (curproc == proc_pageout) { 33226245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 33236245Smaybee return (ERESTART); 33246245Smaybee /* Note: reserve is inflated, so we deflate */ 33256245Smaybee page_load += reserve / 8; 33266245Smaybee return (0); 33276245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 33286245Smaybee /* memory is low, delay before restarting */ 33296245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33306245Smaybee return (EAGAIN); 33316245Smaybee } 33326245Smaybee page_load = 0; 33336245Smaybee 33346245Smaybee if (arc_size > arc_c_min) { 33356245Smaybee uint64_t evictable_memory = 33366245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 33376245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 33386245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 33396245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 33406245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 33416245Smaybee } 33426245Smaybee 33436245Smaybee if (inflight_data > available_memory / 4) { 33446245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33456245Smaybee return (ERESTART); 33466245Smaybee } 33476245Smaybee #endif 33486245Smaybee return (0); 33496245Smaybee } 33506245Smaybee 3351789Sahrens void 33526245Smaybee arc_tempreserve_clear(uint64_t reserve) 3353789Sahrens { 33546245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3355789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3356789Sahrens } 3357789Sahrens 3358789Sahrens int 33596245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3360789Sahrens { 33616245Smaybee int error; 33629412SAleksandr.Guzovskiy@Sun.COM uint64_t anon_size; 33636245Smaybee 3364789Sahrens #ifdef ZFS_DEBUG 3365789Sahrens /* 3366789Sahrens * Once in a while, fail for no reason. Everything should cope. 3367789Sahrens */ 3368789Sahrens if (spa_get_random(10000) == 0) { 3369789Sahrens dprintf("forcing random failure\n"); 3370789Sahrens return (ERESTART); 3371789Sahrens } 3372789Sahrens #endif 33736245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 33746245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 33756245Smaybee if (reserve > arc_c) 3376982Smaybee return (ENOMEM); 3377982Smaybee 3378789Sahrens /* 33799412SAleksandr.Guzovskiy@Sun.COM * Don't count loaned bufs as in flight dirty data to prevent long 33809412SAleksandr.Guzovskiy@Sun.COM * network delays from blocking transactions that are ready to be 33819412SAleksandr.Guzovskiy@Sun.COM * assigned to a txg. 33829412SAleksandr.Guzovskiy@Sun.COM */ 33839412SAleksandr.Guzovskiy@Sun.COM anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 33849412SAleksandr.Guzovskiy@Sun.COM 33859412SAleksandr.Guzovskiy@Sun.COM /* 33866245Smaybee * Writes will, almost always, require additional memory allocations 33876245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 33886245Smaybee * make sure that there is sufficient available memory for this. 33896245Smaybee */ 33909412SAleksandr.Guzovskiy@Sun.COM if (error = arc_memory_throttle(reserve, anon_size, txg)) 33916245Smaybee return (error); 33926245Smaybee 33936245Smaybee /* 3394982Smaybee * Throttle writes when the amount of dirty data in the cache 3395982Smaybee * gets too large. We try to keep the cache less than half full 3396982Smaybee * of dirty blocks so that our sync times don't grow too large. 3397982Smaybee * Note: if two requests come in concurrently, we might let them 3398982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3399789Sahrens */ 34009412SAleksandr.Guzovskiy@Sun.COM 34019412SAleksandr.Guzovskiy@Sun.COM if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 34029412SAleksandr.Guzovskiy@Sun.COM anon_size > arc_c / 4) { 34034309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 34044309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 34054309Smaybee arc_tempreserve>>10, 34064309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 34074309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 34086245Smaybee reserve>>10, arc_c>>10); 3409789Sahrens return (ERESTART); 3410789Sahrens } 34116245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3412789Sahrens return (0); 3413789Sahrens } 3414789Sahrens 3415789Sahrens void 3416789Sahrens arc_init(void) 3417789Sahrens { 3418789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3419789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3420789Sahrens 34212391Smaybee /* Convert seconds to clock ticks */ 34222638Sperrin arc_min_prefetch_lifespan = 1 * hz; 34232391Smaybee 3424789Sahrens /* Start out with 1/8 of all memory */ 34253403Sbmc arc_c = physmem * PAGESIZE / 8; 3426789Sahrens 3427789Sahrens #ifdef _KERNEL 3428789Sahrens /* 3429789Sahrens * On architectures where the physical memory can be larger 3430789Sahrens * than the addressable space (intel in 32-bit mode), we may 3431789Sahrens * need to limit the cache to 1/8 of VM size. 3432789Sahrens */ 34333403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3434789Sahrens #endif 3435789Sahrens 3436982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 34373403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3438982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 34393403Sbmc if (arc_c * 8 >= 1<<30) 34403403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3441789Sahrens else 34423403Sbmc arc_c_max = arc_c_min; 34433403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 34442885Sahrens 34452885Sahrens /* 34462885Sahrens * Allow the tunables to override our calculations if they are 34472885Sahrens * reasonable (ie. over 64MB) 34482885Sahrens */ 34492885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 34503403Sbmc arc_c_max = zfs_arc_max; 34513403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 34523403Sbmc arc_c_min = zfs_arc_min; 34532885Sahrens 34543403Sbmc arc_c = arc_c_max; 34553403Sbmc arc_p = (arc_c >> 1); 3456789Sahrens 34574309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 34584309Smaybee arc_meta_limit = arc_c_max / 4; 34594645Sek110237 34604645Sek110237 /* Allow the tunable to override if it is reasonable */ 34614645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 34624645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 34634645Sek110237 34644309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 34654309Smaybee arc_c_min = arc_meta_limit / 2; 34664309Smaybee 34678582SBrendan.Gregg@Sun.COM if (zfs_arc_grow_retry > 0) 34688582SBrendan.Gregg@Sun.COM arc_grow_retry = zfs_arc_grow_retry; 34698582SBrendan.Gregg@Sun.COM 34708582SBrendan.Gregg@Sun.COM if (zfs_arc_shrink_shift > 0) 34718582SBrendan.Gregg@Sun.COM arc_shrink_shift = zfs_arc_shrink_shift; 34728582SBrendan.Gregg@Sun.COM 34738582SBrendan.Gregg@Sun.COM if (zfs_arc_p_min_shift > 0) 34748582SBrendan.Gregg@Sun.COM arc_p_min_shift = zfs_arc_p_min_shift; 34758582SBrendan.Gregg@Sun.COM 3476789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3477789Sahrens if (kmem_debugging()) 34783403Sbmc arc_c = arc_c / 2; 34793403Sbmc if (arc_c < arc_c_min) 34803403Sbmc arc_c = arc_c_min; 3481789Sahrens 34823403Sbmc arc_anon = &ARC_anon; 34833403Sbmc arc_mru = &ARC_mru; 34843403Sbmc arc_mru_ghost = &ARC_mru_ghost; 34853403Sbmc arc_mfu = &ARC_mfu; 34863403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 34875450Sbrendan arc_l2c_only = &ARC_l2c_only; 34883403Sbmc arc_size = 0; 3489789Sahrens 34903403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34913403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34923403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34933403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34943403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34955450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34962688Smaybee 34974309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 34984309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34994309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 35004309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35014309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 35024309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35034309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 35044309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35054309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 35064309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35074309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 35084309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35094309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 35104309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35114309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 35124309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35135450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 35145450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35155450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 35165450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3517789Sahrens 3518789Sahrens buf_init(); 3519789Sahrens 3520789Sahrens arc_thread_exit = 0; 35211544Seschrock arc_eviction_list = NULL; 35221544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 35232887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3524789Sahrens 35253403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 35263403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 35273403Sbmc 35283403Sbmc if (arc_ksp != NULL) { 35293403Sbmc arc_ksp->ks_data = &arc_stats; 35303403Sbmc kstat_install(arc_ksp); 35313403Sbmc } 35323403Sbmc 3533789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3534789Sahrens TS_RUN, minclsyspri); 35353158Smaybee 35363158Smaybee arc_dead = FALSE; 35376987Sbrendan arc_warm = B_FALSE; 35386245Smaybee 35396245Smaybee if (zfs_write_limit_max == 0) 35407468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 35416245Smaybee else 35426245Smaybee zfs_write_limit_shift = 0; 35437468SMark.Maybee@Sun.COM mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3544789Sahrens } 3545789Sahrens 3546789Sahrens void 3547789Sahrens arc_fini(void) 3548789Sahrens { 3549789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3550789Sahrens arc_thread_exit = 1; 3551789Sahrens while (arc_thread_exit != 0) 3552789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3553789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3554789Sahrens 35555642Smaybee arc_flush(NULL); 3556789Sahrens 3557789Sahrens arc_dead = TRUE; 3558789Sahrens 35593403Sbmc if (arc_ksp != NULL) { 35603403Sbmc kstat_delete(arc_ksp); 35613403Sbmc arc_ksp = NULL; 35623403Sbmc } 35633403Sbmc 35641544Seschrock mutex_destroy(&arc_eviction_mtx); 3565789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3566789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3567789Sahrens 35684309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 35694309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 35704309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 35714309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 35724309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 35734309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 35744309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 35754309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3576789Sahrens 35773403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 35783403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 35793403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 35803403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 35813403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 35828214SRicardo.M.Correia@Sun.COM mutex_destroy(&arc_l2c_only->arcs_mtx); 35832856Snd150628 35847468SMark.Maybee@Sun.COM mutex_destroy(&zfs_write_limit_lock); 35857468SMark.Maybee@Sun.COM 3586789Sahrens buf_fini(); 35879412SAleksandr.Guzovskiy@Sun.COM 35889412SAleksandr.Guzovskiy@Sun.COM ASSERT(arc_loaned_bytes == 0); 3589789Sahrens } 35905450Sbrendan 35915450Sbrendan /* 35925450Sbrendan * Level 2 ARC 35935450Sbrendan * 35945450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 35955450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 35965450Sbrendan * using large infrequent writes. The main role of this cache is to boost 35975450Sbrendan * the performance of random read workloads. The intended L2ARC devices 35985450Sbrendan * include short-stroked disks, solid state disks, and other media with 35995450Sbrendan * substantially faster read latency than disk. 36005450Sbrendan * 36015450Sbrendan * +-----------------------+ 36025450Sbrendan * | ARC | 36035450Sbrendan * +-----------------------+ 36045450Sbrendan * | ^ ^ 36055450Sbrendan * | | | 36065450Sbrendan * l2arc_feed_thread() arc_read() 36075450Sbrendan * | | | 36085450Sbrendan * | l2arc read | 36095450Sbrendan * V | | 36105450Sbrendan * +---------------+ | 36115450Sbrendan * | L2ARC | | 36125450Sbrendan * +---------------+ | 36135450Sbrendan * | ^ | 36145450Sbrendan * l2arc_write() | | 36155450Sbrendan * | | | 36165450Sbrendan * V | | 36175450Sbrendan * +-------+ +-------+ 36185450Sbrendan * | vdev | | vdev | 36195450Sbrendan * | cache | | cache | 36205450Sbrendan * +-------+ +-------+ 36215450Sbrendan * +=========+ .-----. 36225450Sbrendan * : L2ARC : |-_____-| 36235450Sbrendan * : devices : | Disks | 36245450Sbrendan * +=========+ `-_____-' 36255450Sbrendan * 36265450Sbrendan * Read requests are satisfied from the following sources, in order: 36275450Sbrendan * 36285450Sbrendan * 1) ARC 36295450Sbrendan * 2) vdev cache of L2ARC devices 36305450Sbrendan * 3) L2ARC devices 36315450Sbrendan * 4) vdev cache of disks 36325450Sbrendan * 5) disks 36335450Sbrendan * 36345450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 36355450Sbrendan * To accommodate for this there are some significant differences between 36365450Sbrendan * the L2ARC and traditional cache design: 36375450Sbrendan * 36385450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 36395450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 36405450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 36415450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 36425450Sbrendan * 36435450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 36445450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 36455450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 36465450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 36475450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 36485450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 36495450Sbrendan * provide a better sense of ratio than this diagram: 36505450Sbrendan * 36515450Sbrendan * head --> tail 36525450Sbrendan * +---------------------+----------+ 36535450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 36545450Sbrendan * +---------------------+----------+ | o L2ARC eligible 36555450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 36565450Sbrendan * +---------------------+----------+ | 36575450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 36585450Sbrendan * headroom | 36595450Sbrendan * l2arc_feed_thread() 36605450Sbrendan * | 36615450Sbrendan * l2arc write hand <--[oooo]--' 36625450Sbrendan * | 8 Mbyte 36635450Sbrendan * | write max 36645450Sbrendan * V 36655450Sbrendan * +==============================+ 36665450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 36675450Sbrendan * +==============================+ 36685450Sbrendan * 32 Gbytes 36695450Sbrendan * 36705450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 36715450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 36725450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 36735450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 36745450Sbrendan * the ARC lists have moved there due to inactivity. 36755450Sbrendan * 36765450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 36775450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 36785450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 36795450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 36805450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 36815450Sbrendan * quickly, such as during backups of the entire pool. 36825450Sbrendan * 36836987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 36846987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 36856987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 36866987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 36876987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 36886987Sbrendan * 36896987Sbrendan * The L2ARC device write speed is also boosted during this time so that 36906987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 36916987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 36926987Sbrendan * through increased writes. 36936987Sbrendan * 36946987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 36955450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 36965450Sbrendan * device is written to in a rotor fashion, sweeping writes through 36975450Sbrendan * available space then repeating. 36985450Sbrendan * 36996987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 37005450Sbrendan * write buffers back to disk based storage. 37015450Sbrendan * 37026987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 37035450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 37045450Sbrendan * 37055450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 37065450Sbrendan * may be necessary for different workloads: 37075450Sbrendan * 37085450Sbrendan * l2arc_write_max max write bytes per interval 37096987Sbrendan * l2arc_write_boost extra write bytes during device warmup 37105450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 37115450Sbrendan * l2arc_headroom number of max device writes to precache 37125450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 37135450Sbrendan * 37145450Sbrendan * Tunables may be removed or added as future performance improvements are 37155450Sbrendan * integrated, and also may become zpool properties. 37168582SBrendan.Gregg@Sun.COM * 37178582SBrendan.Gregg@Sun.COM * There are three key functions that control how the L2ARC warms up: 37188582SBrendan.Gregg@Sun.COM * 37198582SBrendan.Gregg@Sun.COM * l2arc_write_eligible() check if a buffer is eligible to cache 37208582SBrendan.Gregg@Sun.COM * l2arc_write_size() calculate how much to write 37218582SBrendan.Gregg@Sun.COM * l2arc_write_interval() calculate sleep delay between writes 37228582SBrendan.Gregg@Sun.COM * 37238582SBrendan.Gregg@Sun.COM * These three functions determine what to write, how much, and how quickly 37248582SBrendan.Gregg@Sun.COM * to send writes. 37255450Sbrendan */ 37265450Sbrendan 37278582SBrendan.Gregg@Sun.COM static boolean_t 37288636SMark.Maybee@Sun.COM l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 37298582SBrendan.Gregg@Sun.COM { 37308582SBrendan.Gregg@Sun.COM /* 37318582SBrendan.Gregg@Sun.COM * A buffer is *not* eligible for the L2ARC if it: 37328582SBrendan.Gregg@Sun.COM * 1. belongs to a different spa. 373310357SBrendan.Gregg@Sun.COM * 2. is already cached on the L2ARC. 373410357SBrendan.Gregg@Sun.COM * 3. has an I/O in progress (it may be an incomplete read). 373510357SBrendan.Gregg@Sun.COM * 4. is flagged not eligible (zfs property). 37368582SBrendan.Gregg@Sun.COM */ 373710357SBrendan.Gregg@Sun.COM if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL || 37388582SBrendan.Gregg@Sun.COM HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) 37398582SBrendan.Gregg@Sun.COM return (B_FALSE); 37408582SBrendan.Gregg@Sun.COM 37418582SBrendan.Gregg@Sun.COM return (B_TRUE); 37428582SBrendan.Gregg@Sun.COM } 37438582SBrendan.Gregg@Sun.COM 37448582SBrendan.Gregg@Sun.COM static uint64_t 37458582SBrendan.Gregg@Sun.COM l2arc_write_size(l2arc_dev_t *dev) 37468582SBrendan.Gregg@Sun.COM { 37478582SBrendan.Gregg@Sun.COM uint64_t size; 37488582SBrendan.Gregg@Sun.COM 37498582SBrendan.Gregg@Sun.COM size = dev->l2ad_write; 37508582SBrendan.Gregg@Sun.COM 37518582SBrendan.Gregg@Sun.COM if (arc_warm == B_FALSE) 37528582SBrendan.Gregg@Sun.COM size += dev->l2ad_boost; 37538582SBrendan.Gregg@Sun.COM 37548582SBrendan.Gregg@Sun.COM return (size); 37558582SBrendan.Gregg@Sun.COM 37568582SBrendan.Gregg@Sun.COM } 37578582SBrendan.Gregg@Sun.COM 37588582SBrendan.Gregg@Sun.COM static clock_t 37598582SBrendan.Gregg@Sun.COM l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 37608582SBrendan.Gregg@Sun.COM { 376111066Srafael.vanoni@sun.com clock_t interval, next, now; 37628582SBrendan.Gregg@Sun.COM 37638582SBrendan.Gregg@Sun.COM /* 37648582SBrendan.Gregg@Sun.COM * If the ARC lists are busy, increase our write rate; if the 37658582SBrendan.Gregg@Sun.COM * lists are stale, idle back. This is achieved by checking 37668582SBrendan.Gregg@Sun.COM * how much we previously wrote - if it was more than half of 37678582SBrendan.Gregg@Sun.COM * what we wanted, schedule the next write much sooner. 37688582SBrendan.Gregg@Sun.COM */ 37698582SBrendan.Gregg@Sun.COM if (l2arc_feed_again && wrote > (wanted / 2)) 37708582SBrendan.Gregg@Sun.COM interval = (hz * l2arc_feed_min_ms) / 1000; 37718582SBrendan.Gregg@Sun.COM else 37728582SBrendan.Gregg@Sun.COM interval = hz * l2arc_feed_secs; 37738582SBrendan.Gregg@Sun.COM 377411066Srafael.vanoni@sun.com now = ddi_get_lbolt(); 377511066Srafael.vanoni@sun.com next = MAX(now, MIN(now + interval, began + interval)); 37768582SBrendan.Gregg@Sun.COM 37778582SBrendan.Gregg@Sun.COM return (next); 37788582SBrendan.Gregg@Sun.COM } 37798582SBrendan.Gregg@Sun.COM 37805450Sbrendan static void 37815450Sbrendan l2arc_hdr_stat_add(void) 37825450Sbrendan { 37836018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 37846018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 37855450Sbrendan } 37865450Sbrendan 37875450Sbrendan static void 37885450Sbrendan l2arc_hdr_stat_remove(void) 37895450Sbrendan { 37906018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 37916018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 37925450Sbrendan } 37935450Sbrendan 37945450Sbrendan /* 37955450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 37966987Sbrendan * If a device is returned, this also returns holding the spa config lock. 37975450Sbrendan */ 37985450Sbrendan static l2arc_dev_t * 37995450Sbrendan l2arc_dev_get_next(void) 38005450Sbrendan { 38016987Sbrendan l2arc_dev_t *first, *next = NULL; 38026987Sbrendan 38036987Sbrendan /* 38046987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 38056987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 38066987Sbrendan * both locks will be dropped and a spa config lock held instead. 38076987Sbrendan */ 38086987Sbrendan mutex_enter(&spa_namespace_lock); 38096987Sbrendan mutex_enter(&l2arc_dev_mtx); 38106643Seschrock 38116643Seschrock /* if there are no vdevs, there is nothing to do */ 38126643Seschrock if (l2arc_ndev == 0) 38136987Sbrendan goto out; 38146643Seschrock 38156643Seschrock first = NULL; 38166643Seschrock next = l2arc_dev_last; 38176643Seschrock do { 38186643Seschrock /* loop around the list looking for a non-faulted vdev */ 38196643Seschrock if (next == NULL) { 38205450Sbrendan next = list_head(l2arc_dev_list); 38216643Seschrock } else { 38226643Seschrock next = list_next(l2arc_dev_list, next); 38236643Seschrock if (next == NULL) 38246643Seschrock next = list_head(l2arc_dev_list); 38256643Seschrock } 38266643Seschrock 38276643Seschrock /* if we have come back to the start, bail out */ 38286643Seschrock if (first == NULL) 38296643Seschrock first = next; 38306643Seschrock else if (next == first) 38316643Seschrock break; 38326643Seschrock 38336643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 38346643Seschrock 38356643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 38366643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 38376987Sbrendan next = NULL; 38385450Sbrendan 38395450Sbrendan l2arc_dev_last = next; 38405450Sbrendan 38416987Sbrendan out: 38426987Sbrendan mutex_exit(&l2arc_dev_mtx); 38436987Sbrendan 38446987Sbrendan /* 38456987Sbrendan * Grab the config lock to prevent the 'next' device from being 38466987Sbrendan * removed while we are writing to it. 38476987Sbrendan */ 38486987Sbrendan if (next != NULL) 38497754SJeff.Bonwick@Sun.COM spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 38506987Sbrendan mutex_exit(&spa_namespace_lock); 38516987Sbrendan 38525450Sbrendan return (next); 38535450Sbrendan } 38545450Sbrendan 38555450Sbrendan /* 38566987Sbrendan * Free buffers that were tagged for destruction. 38576987Sbrendan */ 38586987Sbrendan static void 38596987Sbrendan l2arc_do_free_on_write() 38606987Sbrendan { 38616987Sbrendan list_t *buflist; 38626987Sbrendan l2arc_data_free_t *df, *df_prev; 38636987Sbrendan 38646987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 38656987Sbrendan buflist = l2arc_free_on_write; 38666987Sbrendan 38676987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 38686987Sbrendan df_prev = list_prev(buflist, df); 38696987Sbrendan ASSERT(df->l2df_data != NULL); 38706987Sbrendan ASSERT(df->l2df_func != NULL); 38716987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 38726987Sbrendan list_remove(buflist, df); 38736987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 38746987Sbrendan } 38756987Sbrendan 38766987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 38776987Sbrendan } 38786987Sbrendan 38796987Sbrendan /* 38805450Sbrendan * A write to a cache device has completed. Update all headers to allow 38815450Sbrendan * reads from these buffers to begin. 38825450Sbrendan */ 38835450Sbrendan static void 38845450Sbrendan l2arc_write_done(zio_t *zio) 38855450Sbrendan { 38865450Sbrendan l2arc_write_callback_t *cb; 38875450Sbrendan l2arc_dev_t *dev; 38885450Sbrendan list_t *buflist; 38895450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 38906987Sbrendan l2arc_buf_hdr_t *abl2; 38915450Sbrendan kmutex_t *hash_lock; 38925450Sbrendan 38935450Sbrendan cb = zio->io_private; 38945450Sbrendan ASSERT(cb != NULL); 38955450Sbrendan dev = cb->l2wcb_dev; 38965450Sbrendan ASSERT(dev != NULL); 38975450Sbrendan head = cb->l2wcb_head; 38985450Sbrendan ASSERT(head != NULL); 38995450Sbrendan buflist = dev->l2ad_buflist; 39005450Sbrendan ASSERT(buflist != NULL); 39015450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 39025450Sbrendan l2arc_write_callback_t *, cb); 39035450Sbrendan 39045450Sbrendan if (zio->io_error != 0) 39055450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 39065450Sbrendan 39075450Sbrendan mutex_enter(&l2arc_buflist_mtx); 39085450Sbrendan 39095450Sbrendan /* 39105450Sbrendan * All writes completed, or an error was hit. 39115450Sbrendan */ 39125450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 39135450Sbrendan ab_prev = list_prev(buflist, ab); 39145450Sbrendan 39155450Sbrendan hash_lock = HDR_LOCK(ab); 39165450Sbrendan if (!mutex_tryenter(hash_lock)) { 39175450Sbrendan /* 39185450Sbrendan * This buffer misses out. It may be in a stage 39195450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 39205450Sbrendan * left set, denying reads to this buffer. 39215450Sbrendan */ 39225450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 39235450Sbrendan continue; 39245450Sbrendan } 39255450Sbrendan 39265450Sbrendan if (zio->io_error != 0) { 39275450Sbrendan /* 39286987Sbrendan * Error - drop L2ARC entry. 39295450Sbrendan */ 39306987Sbrendan list_remove(buflist, ab); 39316987Sbrendan abl2 = ab->b_l2hdr; 39325450Sbrendan ab->b_l2hdr = NULL; 39336987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 39346987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 39355450Sbrendan } 39365450Sbrendan 39375450Sbrendan /* 39385450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 39395450Sbrendan */ 39405450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 39415450Sbrendan 39425450Sbrendan mutex_exit(hash_lock); 39435450Sbrendan } 39445450Sbrendan 39455450Sbrendan atomic_inc_64(&l2arc_writes_done); 39465450Sbrendan list_remove(buflist, head); 39475450Sbrendan kmem_cache_free(hdr_cache, head); 39485450Sbrendan mutex_exit(&l2arc_buflist_mtx); 39495450Sbrendan 39506987Sbrendan l2arc_do_free_on_write(); 39515450Sbrendan 39525450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 39535450Sbrendan } 39545450Sbrendan 39555450Sbrendan /* 39565450Sbrendan * A read to a cache device completed. Validate buffer contents before 39575450Sbrendan * handing over to the regular ARC routines. 39585450Sbrendan */ 39595450Sbrendan static void 39605450Sbrendan l2arc_read_done(zio_t *zio) 39615450Sbrendan { 39625450Sbrendan l2arc_read_callback_t *cb; 39635450Sbrendan arc_buf_hdr_t *hdr; 39645450Sbrendan arc_buf_t *buf; 39655450Sbrendan kmutex_t *hash_lock; 39666987Sbrendan int equal; 39675450Sbrendan 39687754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd != NULL); 39697754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 39707754SJeff.Bonwick@Sun.COM 39717754SJeff.Bonwick@Sun.COM spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 39727754SJeff.Bonwick@Sun.COM 39735450Sbrendan cb = zio->io_private; 39745450Sbrendan ASSERT(cb != NULL); 39755450Sbrendan buf = cb->l2rcb_buf; 39765450Sbrendan ASSERT(buf != NULL); 39775450Sbrendan hdr = buf->b_hdr; 39785450Sbrendan ASSERT(hdr != NULL); 39795450Sbrendan 39805450Sbrendan hash_lock = HDR_LOCK(hdr); 39815450Sbrendan mutex_enter(hash_lock); 39825450Sbrendan 39835450Sbrendan /* 39845450Sbrendan * Check this survived the L2ARC journey. 39855450Sbrendan */ 39865450Sbrendan equal = arc_cksum_equal(buf); 39875450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 39885450Sbrendan mutex_exit(hash_lock); 39895450Sbrendan zio->io_private = buf; 39907754SJeff.Bonwick@Sun.COM zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 39917754SJeff.Bonwick@Sun.COM zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 39925450Sbrendan arc_read_done(zio); 39935450Sbrendan } else { 39945450Sbrendan mutex_exit(hash_lock); 39955450Sbrendan /* 39965450Sbrendan * Buffer didn't survive caching. Increment stats and 39975450Sbrendan * reissue to the original storage device. 39985450Sbrendan */ 39996987Sbrendan if (zio->io_error != 0) { 40005450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 40016987Sbrendan } else { 40026987Sbrendan zio->io_error = EIO; 40036987Sbrendan } 40045450Sbrendan if (!equal) 40055450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 40065450Sbrendan 40077754SJeff.Bonwick@Sun.COM /* 40087754SJeff.Bonwick@Sun.COM * If there's no waiter, issue an async i/o to the primary 40097754SJeff.Bonwick@Sun.COM * storage now. If there *is* a waiter, the caller must 40107754SJeff.Bonwick@Sun.COM * issue the i/o in a context where it's OK to block. 40117754SJeff.Bonwick@Sun.COM */ 40128632SBill.Moore@Sun.COM if (zio->io_waiter == NULL) { 40138632SBill.Moore@Sun.COM zio_t *pio = zio_unique_parent(zio); 40148632SBill.Moore@Sun.COM 40158632SBill.Moore@Sun.COM ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 40168632SBill.Moore@Sun.COM 40178632SBill.Moore@Sun.COM zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 40187754SJeff.Bonwick@Sun.COM buf->b_data, zio->io_size, arc_read_done, buf, 40197754SJeff.Bonwick@Sun.COM zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 40208632SBill.Moore@Sun.COM } 40215450Sbrendan } 40225450Sbrendan 40235450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 40245450Sbrendan } 40255450Sbrendan 40265450Sbrendan /* 40275450Sbrendan * This is the list priority from which the L2ARC will search for pages to 40285450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 40295450Sbrendan * desired order. This order can have a significant effect on cache 40305450Sbrendan * performance. 40315450Sbrendan * 40325450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 40335450Sbrendan * the data lists. This function returns a locked list, and also returns 40345450Sbrendan * the lock pointer. 40355450Sbrendan */ 40365450Sbrendan static list_t * 40375450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 40385450Sbrendan { 40395450Sbrendan list_t *list; 40405450Sbrendan 40415450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 40425450Sbrendan 40435450Sbrendan switch (list_num) { 40445450Sbrendan case 0: 40455450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 40465450Sbrendan *lock = &arc_mfu->arcs_mtx; 40475450Sbrendan break; 40485450Sbrendan case 1: 40495450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 40505450Sbrendan *lock = &arc_mru->arcs_mtx; 40515450Sbrendan break; 40525450Sbrendan case 2: 40535450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 40545450Sbrendan *lock = &arc_mfu->arcs_mtx; 40555450Sbrendan break; 40565450Sbrendan case 3: 40575450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 40585450Sbrendan *lock = &arc_mru->arcs_mtx; 40595450Sbrendan break; 40605450Sbrendan } 40615450Sbrendan 40625450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 40635450Sbrendan mutex_enter(*lock); 40645450Sbrendan return (list); 40655450Sbrendan } 40665450Sbrendan 40675450Sbrendan /* 40685450Sbrendan * Evict buffers from the device write hand to the distance specified in 40695450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 40705450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 40715450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 40725450Sbrendan */ 40735450Sbrendan static void 40745450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 40755450Sbrendan { 40765450Sbrendan list_t *buflist; 40775450Sbrendan l2arc_buf_hdr_t *abl2; 40785450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 40795450Sbrendan kmutex_t *hash_lock; 40805450Sbrendan uint64_t taddr; 40815450Sbrendan 40825450Sbrendan buflist = dev->l2ad_buflist; 40835450Sbrendan 40845450Sbrendan if (buflist == NULL) 40855450Sbrendan return; 40865450Sbrendan 40875450Sbrendan if (!all && dev->l2ad_first) { 40885450Sbrendan /* 40895450Sbrendan * This is the first sweep through the device. There is 40905450Sbrendan * nothing to evict. 40915450Sbrendan */ 40925450Sbrendan return; 40935450Sbrendan } 40945450Sbrendan 40956987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 40965450Sbrendan /* 40975450Sbrendan * When nearing the end of the device, evict to the end 40985450Sbrendan * before the device write hand jumps to the start. 40995450Sbrendan */ 41005450Sbrendan taddr = dev->l2ad_end; 41015450Sbrendan } else { 41025450Sbrendan taddr = dev->l2ad_hand + distance; 41035450Sbrendan } 41045450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 41055450Sbrendan uint64_t, taddr, boolean_t, all); 41065450Sbrendan 41075450Sbrendan top: 41085450Sbrendan mutex_enter(&l2arc_buflist_mtx); 41095450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 41105450Sbrendan ab_prev = list_prev(buflist, ab); 41115450Sbrendan 41125450Sbrendan hash_lock = HDR_LOCK(ab); 41135450Sbrendan if (!mutex_tryenter(hash_lock)) { 41145450Sbrendan /* 41155450Sbrendan * Missed the hash lock. Retry. 41165450Sbrendan */ 41175450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 41185450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41195450Sbrendan mutex_enter(hash_lock); 41205450Sbrendan mutex_exit(hash_lock); 41215450Sbrendan goto top; 41225450Sbrendan } 41235450Sbrendan 41245450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 41255450Sbrendan /* 41265450Sbrendan * We hit a write head node. Leave it for 41275450Sbrendan * l2arc_write_done(). 41285450Sbrendan */ 41295450Sbrendan list_remove(buflist, ab); 41305450Sbrendan mutex_exit(hash_lock); 41315450Sbrendan continue; 41325450Sbrendan } 41335450Sbrendan 41345450Sbrendan if (!all && ab->b_l2hdr != NULL && 41355450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 41365450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 41375450Sbrendan /* 41385450Sbrendan * We've evicted to the target address, 41395450Sbrendan * or the end of the device. 41405450Sbrendan */ 41415450Sbrendan mutex_exit(hash_lock); 41425450Sbrendan break; 41435450Sbrendan } 41445450Sbrendan 41455450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 41465450Sbrendan /* 41475450Sbrendan * Already on the path to destruction. 41485450Sbrendan */ 41495450Sbrendan mutex_exit(hash_lock); 41505450Sbrendan continue; 41515450Sbrendan } 41525450Sbrendan 41535450Sbrendan if (ab->b_state == arc_l2c_only) { 41545450Sbrendan ASSERT(!HDR_L2_READING(ab)); 41555450Sbrendan /* 41565450Sbrendan * This doesn't exist in the ARC. Destroy. 41575450Sbrendan * arc_hdr_destroy() will call list_remove() 41585450Sbrendan * and decrement arcstat_l2_size. 41595450Sbrendan */ 41605450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 41615450Sbrendan arc_hdr_destroy(ab); 41625450Sbrendan } else { 41635450Sbrendan /* 41646987Sbrendan * Invalidate issued or about to be issued 41656987Sbrendan * reads, since we may be about to write 41666987Sbrendan * over this location. 41676987Sbrendan */ 41686987Sbrendan if (HDR_L2_READING(ab)) { 41696987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 41706987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 41716987Sbrendan } 41726987Sbrendan 41736987Sbrendan /* 41745450Sbrendan * Tell ARC this no longer exists in L2ARC. 41755450Sbrendan */ 41765450Sbrendan if (ab->b_l2hdr != NULL) { 41775450Sbrendan abl2 = ab->b_l2hdr; 41785450Sbrendan ab->b_l2hdr = NULL; 41795450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 41805450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 41815450Sbrendan } 41825450Sbrendan list_remove(buflist, ab); 41835450Sbrendan 41845450Sbrendan /* 41855450Sbrendan * This may have been leftover after a 41865450Sbrendan * failed write. 41875450Sbrendan */ 41885450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 41895450Sbrendan } 41905450Sbrendan mutex_exit(hash_lock); 41915450Sbrendan } 41925450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41935450Sbrendan 419410922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0); 41955450Sbrendan dev->l2ad_evict = taddr; 41965450Sbrendan } 41975450Sbrendan 41985450Sbrendan /* 41995450Sbrendan * Find and write ARC buffers to the L2ARC device. 42005450Sbrendan * 42015450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 42025450Sbrendan * for reading until they have completed writing. 42035450Sbrendan */ 42048582SBrendan.Gregg@Sun.COM static uint64_t 42056987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 42065450Sbrendan { 42075450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 42085450Sbrendan l2arc_buf_hdr_t *hdrl2; 42095450Sbrendan list_t *list; 42106987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 42115450Sbrendan void *buf_data; 42125450Sbrendan kmutex_t *hash_lock, *list_lock; 42135450Sbrendan boolean_t have_lock, full; 42145450Sbrendan l2arc_write_callback_t *cb; 42155450Sbrendan zio_t *pio, *wzio; 42168636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 42175450Sbrendan 42185450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 42195450Sbrendan 42205450Sbrendan pio = NULL; 42215450Sbrendan write_sz = 0; 42225450Sbrendan full = B_FALSE; 42236245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 42245450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 42255450Sbrendan 42265450Sbrendan /* 42275450Sbrendan * Copy buffers for L2ARC writing. 42285450Sbrendan */ 42295450Sbrendan mutex_enter(&l2arc_buflist_mtx); 42305450Sbrendan for (int try = 0; try <= 3; try++) { 42315450Sbrendan list = l2arc_list_locked(try, &list_lock); 42325450Sbrendan passed_sz = 0; 42335450Sbrendan 42346987Sbrendan /* 42356987Sbrendan * L2ARC fast warmup. 42366987Sbrendan * 42376987Sbrendan * Until the ARC is warm and starts to evict, read from the 42386987Sbrendan * head of the ARC lists rather than the tail. 42396987Sbrendan */ 42406987Sbrendan headroom = target_sz * l2arc_headroom; 42416987Sbrendan if (arc_warm == B_FALSE) 42426987Sbrendan ab = list_head(list); 42436987Sbrendan else 42446987Sbrendan ab = list_tail(list); 42456987Sbrendan 42466987Sbrendan for (; ab; ab = ab_prev) { 42476987Sbrendan if (arc_warm == B_FALSE) 42486987Sbrendan ab_prev = list_next(list, ab); 42496987Sbrendan else 42506987Sbrendan ab_prev = list_prev(list, ab); 42515450Sbrendan 42525450Sbrendan hash_lock = HDR_LOCK(ab); 42535450Sbrendan have_lock = MUTEX_HELD(hash_lock); 42545450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 42555450Sbrendan /* 42565450Sbrendan * Skip this buffer rather than waiting. 42575450Sbrendan */ 42585450Sbrendan continue; 42595450Sbrendan } 42605450Sbrendan 42615450Sbrendan passed_sz += ab->b_size; 42625450Sbrendan if (passed_sz > headroom) { 42635450Sbrendan /* 42645450Sbrendan * Searched too far. 42655450Sbrendan */ 42665450Sbrendan mutex_exit(hash_lock); 42675450Sbrendan break; 42685450Sbrendan } 42695450Sbrendan 42708636SMark.Maybee@Sun.COM if (!l2arc_write_eligible(guid, ab)) { 42715450Sbrendan mutex_exit(hash_lock); 42725450Sbrendan continue; 42735450Sbrendan } 42745450Sbrendan 42755450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 42765450Sbrendan full = B_TRUE; 42775450Sbrendan mutex_exit(hash_lock); 42785450Sbrendan break; 42795450Sbrendan } 42805450Sbrendan 42815450Sbrendan if (pio == NULL) { 42825450Sbrendan /* 42835450Sbrendan * Insert a dummy header on the buflist so 42845450Sbrendan * l2arc_write_done() can find where the 42855450Sbrendan * write buffers begin without searching. 42865450Sbrendan */ 42875450Sbrendan list_insert_head(dev->l2ad_buflist, head); 42885450Sbrendan 42895450Sbrendan cb = kmem_alloc( 42905450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 42915450Sbrendan cb->l2wcb_dev = dev; 42925450Sbrendan cb->l2wcb_head = head; 42935450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 42945450Sbrendan ZIO_FLAG_CANFAIL); 42955450Sbrendan } 42965450Sbrendan 42975450Sbrendan /* 42985450Sbrendan * Create and add a new L2ARC header. 42995450Sbrendan */ 43005450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 43015450Sbrendan hdrl2->b_dev = dev; 43025450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 43035450Sbrendan 43045450Sbrendan ab->b_flags |= ARC_L2_WRITING; 43055450Sbrendan ab->b_l2hdr = hdrl2; 43065450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 43075450Sbrendan buf_data = ab->b_buf->b_data; 43085450Sbrendan buf_sz = ab->b_size; 43095450Sbrendan 43105450Sbrendan /* 43115450Sbrendan * Compute and store the buffer cksum before 43125450Sbrendan * writing. On debug the cksum is verified first. 43135450Sbrendan */ 43145450Sbrendan arc_cksum_verify(ab->b_buf); 43155450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 43165450Sbrendan 43175450Sbrendan mutex_exit(hash_lock); 43185450Sbrendan 43195450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 43205450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 43215450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 43225450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 43235450Sbrendan 43245450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 43255450Sbrendan zio_t *, wzio); 43265450Sbrendan (void) zio_nowait(wzio); 43275450Sbrendan 43287754SJeff.Bonwick@Sun.COM /* 43297754SJeff.Bonwick@Sun.COM * Keep the clock hand suitably device-aligned. 43307754SJeff.Bonwick@Sun.COM */ 43317754SJeff.Bonwick@Sun.COM buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 43327754SJeff.Bonwick@Sun.COM 43335450Sbrendan write_sz += buf_sz; 43345450Sbrendan dev->l2ad_hand += buf_sz; 43355450Sbrendan } 43365450Sbrendan 43375450Sbrendan mutex_exit(list_lock); 43385450Sbrendan 43395450Sbrendan if (full == B_TRUE) 43405450Sbrendan break; 43415450Sbrendan } 43425450Sbrendan mutex_exit(&l2arc_buflist_mtx); 43435450Sbrendan 43445450Sbrendan if (pio == NULL) { 43455450Sbrendan ASSERT3U(write_sz, ==, 0); 43465450Sbrendan kmem_cache_free(hdr_cache, head); 43478582SBrendan.Gregg@Sun.COM return (0); 43485450Sbrendan } 43495450Sbrendan 43505450Sbrendan ASSERT3U(write_sz, <=, target_sz); 43515450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 43528582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 43535450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 435410922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0); 43555450Sbrendan 43565450Sbrendan /* 43575450Sbrendan * Bump device hand to the device start if it is approaching the end. 43585450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 43595450Sbrendan */ 43606987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 436110922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, 436210922SJeff.Bonwick@Sun.COM dev->l2ad_end - dev->l2ad_hand, 0, 0); 43635450Sbrendan dev->l2ad_hand = dev->l2ad_start; 43645450Sbrendan dev->l2ad_evict = dev->l2ad_start; 43655450Sbrendan dev->l2ad_first = B_FALSE; 43665450Sbrendan } 43675450Sbrendan 43688582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_TRUE; 43695450Sbrendan (void) zio_wait(pio); 43708582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_FALSE; 43718582SBrendan.Gregg@Sun.COM 43728582SBrendan.Gregg@Sun.COM return (write_sz); 43735450Sbrendan } 43745450Sbrendan 43755450Sbrendan /* 43765450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 43775450Sbrendan * heart of the L2ARC. 43785450Sbrendan */ 43795450Sbrendan static void 43805450Sbrendan l2arc_feed_thread(void) 43815450Sbrendan { 43825450Sbrendan callb_cpr_t cpr; 43835450Sbrendan l2arc_dev_t *dev; 43845450Sbrendan spa_t *spa; 43858582SBrendan.Gregg@Sun.COM uint64_t size, wrote; 438611066Srafael.vanoni@sun.com clock_t begin, next = ddi_get_lbolt(); 43875450Sbrendan 43885450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 43895450Sbrendan 43905450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 43915450Sbrendan 43925450Sbrendan while (l2arc_thread_exit == 0) { 43935450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 43946987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 43958582SBrendan.Gregg@Sun.COM next); 43966987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 439711066Srafael.vanoni@sun.com next = ddi_get_lbolt() + hz; 43986987Sbrendan 43996987Sbrendan /* 44006987Sbrendan * Quick check for L2ARC devices. 44016987Sbrendan */ 44026987Sbrendan mutex_enter(&l2arc_dev_mtx); 44036987Sbrendan if (l2arc_ndev == 0) { 44046987Sbrendan mutex_exit(&l2arc_dev_mtx); 44056987Sbrendan continue; 44065450Sbrendan } 44076987Sbrendan mutex_exit(&l2arc_dev_mtx); 440811066Srafael.vanoni@sun.com begin = ddi_get_lbolt(); 44096643Seschrock 44105450Sbrendan /* 44116643Seschrock * This selects the next l2arc device to write to, and in 44126643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 44136987Sbrendan * will return NULL if there are now no l2arc devices or if 44146987Sbrendan * they are all faulted. 44156987Sbrendan * 44166987Sbrendan * If a device is returned, its spa's config lock is also 44176987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 44186987Sbrendan * will grab and release l2arc_dev_mtx. 44195450Sbrendan */ 44206987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 44215450Sbrendan continue; 44226987Sbrendan 44236987Sbrendan spa = dev->l2ad_spa; 44246987Sbrendan ASSERT(spa != NULL); 44255450Sbrendan 44265450Sbrendan /* 44275450Sbrendan * Avoid contributing to memory pressure. 44285450Sbrendan */ 44295450Sbrendan if (arc_reclaim_needed()) { 44305450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 44317754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44325450Sbrendan continue; 44335450Sbrendan } 44345450Sbrendan 44355450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 44365450Sbrendan 44378582SBrendan.Gregg@Sun.COM size = l2arc_write_size(dev); 44386987Sbrendan 44395450Sbrendan /* 44405450Sbrendan * Evict L2ARC buffers that will be overwritten. 44415450Sbrendan */ 44426987Sbrendan l2arc_evict(dev, size, B_FALSE); 44435450Sbrendan 44445450Sbrendan /* 44455450Sbrendan * Write ARC buffers. 44465450Sbrendan */ 44478582SBrendan.Gregg@Sun.COM wrote = l2arc_write_buffers(spa, dev, size); 44488582SBrendan.Gregg@Sun.COM 44498582SBrendan.Gregg@Sun.COM /* 44508582SBrendan.Gregg@Sun.COM * Calculate interval between writes. 44518582SBrendan.Gregg@Sun.COM */ 44528582SBrendan.Gregg@Sun.COM next = l2arc_write_interval(begin, size, wrote); 44537754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44545450Sbrendan } 44555450Sbrendan 44565450Sbrendan l2arc_thread_exit = 0; 44575450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 44585450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 44595450Sbrendan thread_exit(); 44605450Sbrendan } 44615450Sbrendan 44626643Seschrock boolean_t 44636643Seschrock l2arc_vdev_present(vdev_t *vd) 44646643Seschrock { 44656643Seschrock l2arc_dev_t *dev; 44666643Seschrock 44676643Seschrock mutex_enter(&l2arc_dev_mtx); 44686643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 44696643Seschrock dev = list_next(l2arc_dev_list, dev)) { 44706643Seschrock if (dev->l2ad_vdev == vd) 44716643Seschrock break; 44726643Seschrock } 44736643Seschrock mutex_exit(&l2arc_dev_mtx); 44746643Seschrock 44756643Seschrock return (dev != NULL); 44766643Seschrock } 44776643Seschrock 44785450Sbrendan /* 44795450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 44805450Sbrendan * validated the vdev and opened it. 44815450Sbrendan */ 44825450Sbrendan void 44839816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa_t *spa, vdev_t *vd) 44845450Sbrendan { 44855450Sbrendan l2arc_dev_t *adddev; 44865450Sbrendan 44876643Seschrock ASSERT(!l2arc_vdev_present(vd)); 44886643Seschrock 44895450Sbrendan /* 44905450Sbrendan * Create a new l2arc device entry. 44915450Sbrendan */ 44925450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 44935450Sbrendan adddev->l2ad_spa = spa; 44945450Sbrendan adddev->l2ad_vdev = vd; 44955450Sbrendan adddev->l2ad_write = l2arc_write_max; 44966987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 44979816SGeorge.Wilson@Sun.COM adddev->l2ad_start = VDEV_LABEL_START_SIZE; 44989816SGeorge.Wilson@Sun.COM adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 44995450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 45005450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 45015450Sbrendan adddev->l2ad_first = B_TRUE; 45028582SBrendan.Gregg@Sun.COM adddev->l2ad_writing = B_FALSE; 45035450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 45045450Sbrendan 45055450Sbrendan /* 45065450Sbrendan * This is a list of all ARC buffers that are still valid on the 45075450Sbrendan * device. 45085450Sbrendan */ 45095450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 45105450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 45115450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 45125450Sbrendan 451310922SJeff.Bonwick@Sun.COM vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 45145450Sbrendan 45155450Sbrendan /* 45165450Sbrendan * Add device to global list 45175450Sbrendan */ 45185450Sbrendan mutex_enter(&l2arc_dev_mtx); 45195450Sbrendan list_insert_head(l2arc_dev_list, adddev); 45205450Sbrendan atomic_inc_64(&l2arc_ndev); 45215450Sbrendan mutex_exit(&l2arc_dev_mtx); 45225450Sbrendan } 45235450Sbrendan 45245450Sbrendan /* 45255450Sbrendan * Remove a vdev from the L2ARC. 45265450Sbrendan */ 45275450Sbrendan void 45285450Sbrendan l2arc_remove_vdev(vdev_t *vd) 45295450Sbrendan { 45305450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 45315450Sbrendan 45325450Sbrendan /* 45335450Sbrendan * Find the device by vdev 45345450Sbrendan */ 45355450Sbrendan mutex_enter(&l2arc_dev_mtx); 45365450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 45375450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 45385450Sbrendan if (vd == dev->l2ad_vdev) { 45395450Sbrendan remdev = dev; 45405450Sbrendan break; 45415450Sbrendan } 45425450Sbrendan } 45435450Sbrendan ASSERT(remdev != NULL); 45445450Sbrendan 45455450Sbrendan /* 45465450Sbrendan * Remove device from global list 45475450Sbrendan */ 45485450Sbrendan list_remove(l2arc_dev_list, remdev); 45495450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 45506987Sbrendan atomic_dec_64(&l2arc_ndev); 45516987Sbrendan mutex_exit(&l2arc_dev_mtx); 45525450Sbrendan 45535450Sbrendan /* 45545450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 45555450Sbrendan */ 45565450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 45575450Sbrendan list_destroy(remdev->l2ad_buflist); 45585450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 45595450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 45605450Sbrendan } 45615450Sbrendan 45625450Sbrendan void 45637754SJeff.Bonwick@Sun.COM l2arc_init(void) 45645450Sbrendan { 45655450Sbrendan l2arc_thread_exit = 0; 45665450Sbrendan l2arc_ndev = 0; 45675450Sbrendan l2arc_writes_sent = 0; 45685450Sbrendan l2arc_writes_done = 0; 45695450Sbrendan 45705450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 45715450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 45725450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 45735450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 45745450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 45755450Sbrendan 45765450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 45775450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 45785450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 45795450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 45805450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 45815450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 45825450Sbrendan } 45835450Sbrendan 45845450Sbrendan void 45857754SJeff.Bonwick@Sun.COM l2arc_fini(void) 45865450Sbrendan { 45876987Sbrendan /* 45886987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 45896987Sbrendan * Because of this, we can assume that all l2arc devices have 45906987Sbrendan * already been removed when the pools themselves were removed. 45916987Sbrendan */ 45926987Sbrendan 45936987Sbrendan l2arc_do_free_on_write(); 45946987Sbrendan 45955450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 45965450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 45975450Sbrendan mutex_destroy(&l2arc_dev_mtx); 45985450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 45995450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 46005450Sbrendan 46015450Sbrendan list_destroy(l2arc_dev_list); 46025450Sbrendan list_destroy(l2arc_free_on_write); 46035450Sbrendan } 46047754SJeff.Bonwick@Sun.COM 46057754SJeff.Bonwick@Sun.COM void 46067754SJeff.Bonwick@Sun.COM l2arc_start(void) 46077754SJeff.Bonwick@Sun.COM { 46088241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46097754SJeff.Bonwick@Sun.COM return; 46107754SJeff.Bonwick@Sun.COM 46117754SJeff.Bonwick@Sun.COM (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 46127754SJeff.Bonwick@Sun.COM TS_RUN, minclsyspri); 46137754SJeff.Bonwick@Sun.COM } 46147754SJeff.Bonwick@Sun.COM 46157754SJeff.Bonwick@Sun.COM void 46167754SJeff.Bonwick@Sun.COM l2arc_stop(void) 46177754SJeff.Bonwick@Sun.COM { 46188241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46197754SJeff.Bonwick@Sun.COM return; 46207754SJeff.Bonwick@Sun.COM 46217754SJeff.Bonwick@Sun.COM mutex_enter(&l2arc_feed_thr_lock); 46227754SJeff.Bonwick@Sun.COM cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 46237754SJeff.Bonwick@Sun.COM l2arc_thread_exit = 1; 46247754SJeff.Bonwick@Sun.COM while (l2arc_thread_exit != 0) 46257754SJeff.Bonwick@Sun.COM cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 46267754SJeff.Bonwick@Sun.COM mutex_exit(&l2arc_feed_thr_lock); 46277754SJeff.Bonwick@Sun.COM } 4628