1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51484Sek110237 * Common Development and Distribution License (the "License"). 61484Sek110237 * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 228582SBrendan.Gregg@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens /* 273403Sbmc * DVA-based Adjustable Replacement Cache 28789Sahrens * 291544Seschrock * While much of the theory of operation used here is 301544Seschrock * based on the self-tuning, low overhead replacement cache 31789Sahrens * presented by Megiddo and Modha at FAST 2003, there are some 32789Sahrens * significant differences: 33789Sahrens * 34789Sahrens * 1. The Megiddo and Modha model assumes any page is evictable. 35789Sahrens * Pages in its cache cannot be "locked" into memory. This makes 36789Sahrens * the eviction algorithm simple: evict the last page in the list. 37789Sahrens * This also make the performance characteristics easy to reason 38789Sahrens * about. Our cache is not so simple. At any given moment, some 39789Sahrens * subset of the blocks in the cache are un-evictable because we 40789Sahrens * have handed out a reference to them. Blocks are only evictable 41789Sahrens * when there are no external references active. This makes 42789Sahrens * eviction far more problematic: we choose to evict the evictable 43789Sahrens * blocks that are the "lowest" in the list. 44789Sahrens * 45789Sahrens * There are times when it is not possible to evict the requested 46789Sahrens * space. In these circumstances we are unable to adjust the cache 47789Sahrens * size. To prevent the cache growing unbounded at these times we 485450Sbrendan * implement a "cache throttle" that slows the flow of new data 495450Sbrendan * into the cache until we can make space available. 50789Sahrens * 51789Sahrens * 2. The Megiddo and Modha model assumes a fixed cache size. 52789Sahrens * Pages are evicted when the cache is full and there is a cache 53789Sahrens * miss. Our model has a variable sized cache. It grows with 545450Sbrendan * high use, but also tries to react to memory pressure from the 55789Sahrens * operating system: decreasing its size when system memory is 56789Sahrens * tight. 57789Sahrens * 58789Sahrens * 3. The Megiddo and Modha model assumes a fixed page size. All 59789Sahrens * elements of the cache are therefor exactly the same size. So 60789Sahrens * when adjusting the cache size following a cache miss, its simply 61789Sahrens * a matter of choosing a single page to evict. In our model, we 62789Sahrens * have variable sized cache blocks (rangeing from 512 bytes to 63789Sahrens * 128K bytes). We therefor choose a set of blocks to evict to make 64789Sahrens * space for a cache miss that approximates as closely as possible 65789Sahrens * the space used by the new block. 66789Sahrens * 67789Sahrens * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 68789Sahrens * by N. Megiddo & D. Modha, FAST 2003 69789Sahrens */ 70789Sahrens 71789Sahrens /* 72789Sahrens * The locking model: 73789Sahrens * 74789Sahrens * A new reference to a cache buffer can be obtained in two 75789Sahrens * ways: 1) via a hash table lookup using the DVA as a key, 765450Sbrendan * or 2) via one of the ARC lists. The arc_read() interface 77789Sahrens * uses method 1, while the internal arc algorithms for 78789Sahrens * adjusting the cache use method 2. We therefor provide two 79789Sahrens * types of locks: 1) the hash table lock array, and 2) the 80789Sahrens * arc list locks. 81789Sahrens * 82789Sahrens * Buffers do not have their own mutexs, rather they rely on the 83789Sahrens * hash table mutexs for the bulk of their protection (i.e. most 84789Sahrens * fields in the arc_buf_hdr_t are protected by these mutexs). 85789Sahrens * 86789Sahrens * buf_hash_find() returns the appropriate mutex (held) when it 87789Sahrens * locates the requested buffer in the hash table. It returns 88789Sahrens * NULL for the mutex if the buffer was not in the table. 89789Sahrens * 90789Sahrens * buf_hash_remove() expects the appropriate hash mutex to be 91789Sahrens * already held before it is invoked. 92789Sahrens * 93789Sahrens * Each arc state also has a mutex which is used to protect the 94789Sahrens * buffer list associated with the state. When attempting to 95789Sahrens * obtain a hash table lock while holding an arc list lock you 96789Sahrens * must use: mutex_tryenter() to avoid deadlock. Also note that 972688Smaybee * the active state mutex must be held before the ghost state mutex. 98789Sahrens * 991544Seschrock * Arc buffers may have an associated eviction callback function. 1001544Seschrock * This function will be invoked prior to removing the buffer (e.g. 1011544Seschrock * in arc_do_user_evicts()). Note however that the data associated 1021544Seschrock * with the buffer may be evicted prior to the callback. The callback 1031544Seschrock * must be made with *no locks held* (to prevent deadlock). Additionally, 1041544Seschrock * the users of callbacks must ensure that their private data is 1051544Seschrock * protected from simultaneous callbacks from arc_buf_evict() 1061544Seschrock * and arc_do_user_evicts(). 1071544Seschrock * 108789Sahrens * Note that the majority of the performance stats are manipulated 109789Sahrens * with atomic operations. 1105450Sbrendan * 1115450Sbrendan * The L2ARC uses the l2arc_buflist_mtx global mutex for the following: 1125450Sbrendan * 1135450Sbrendan * - L2ARC buflist creation 1145450Sbrendan * - L2ARC buflist eviction 1155450Sbrendan * - L2ARC write completion, which walks L2ARC buflists 1165450Sbrendan * - ARC header destruction, as it removes from L2ARC buflists 1175450Sbrendan * - ARC header release, as it removes from L2ARC buflists 118789Sahrens */ 119789Sahrens 120789Sahrens #include <sys/spa.h> 121789Sahrens #include <sys/zio.h> 122789Sahrens #include <sys/zfs_context.h> 123789Sahrens #include <sys/arc.h> 124789Sahrens #include <sys/refcount.h> 1256643Seschrock #include <sys/vdev.h> 1269816SGeorge.Wilson@Sun.COM #include <sys/vdev_impl.h> 127789Sahrens #ifdef _KERNEL 128789Sahrens #include <sys/vmsystm.h> 129789Sahrens #include <vm/anon.h> 130789Sahrens #include <sys/fs/swapnode.h> 1311484Sek110237 #include <sys/dnlc.h> 132789Sahrens #endif 133789Sahrens #include <sys/callb.h> 1343403Sbmc #include <sys/kstat.h> 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(hdr->b_state == arc_anon); 12459412SAleksandr.Guzovskiy@Sun.COM ASSERT(buf->b_data != NULL); 12469412SAleksandr.Guzovskiy@Sun.COM VERIFY(refcount_remove(&hdr->b_refcnt, arc_onloan_tag) == 0); 12479412SAleksandr.Guzovskiy@Sun.COM VERIFY(refcount_add(&hdr->b_refcnt, tag) == 1); 12489412SAleksandr.Guzovskiy@Sun.COM 12499412SAleksandr.Guzovskiy@Sun.COM atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 12509412SAleksandr.Guzovskiy@Sun.COM } 12519412SAleksandr.Guzovskiy@Sun.COM 12522688Smaybee static arc_buf_t * 12532688Smaybee arc_buf_clone(arc_buf_t *from) 12541544Seschrock { 12552688Smaybee arc_buf_t *buf; 12562688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 12572688Smaybee uint64_t size = hdr->b_size; 12581544Seschrock 125910922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_state != arc_anon); 126010922SJeff.Bonwick@Sun.COM 12616245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 12622688Smaybee buf->b_hdr = hdr; 12632688Smaybee buf->b_data = NULL; 12642688Smaybee buf->b_efunc = NULL; 12652688Smaybee buf->b_private = NULL; 12662688Smaybee buf->b_next = hdr->b_buf; 12672688Smaybee hdr->b_buf = buf; 12682688Smaybee arc_get_data_buf(buf); 12692688Smaybee bcopy(from->b_data, buf->b_data, size); 12702688Smaybee hdr->b_datacnt += 1; 12712688Smaybee return (buf); 12721544Seschrock } 12731544Seschrock 12741544Seschrock void 12751544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 12761544Seschrock { 12772887Smaybee arc_buf_hdr_t *hdr; 12781544Seschrock kmutex_t *hash_lock; 12791544Seschrock 12802724Smaybee /* 12817545SMark.Maybee@Sun.COM * Check to see if this buffer is evicted. Callers 12827545SMark.Maybee@Sun.COM * must verify b_data != NULL to know if the add_ref 12837545SMark.Maybee@Sun.COM * was successful. 12842724Smaybee */ 12857545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 12867545SMark.Maybee@Sun.COM if (buf->b_data == NULL) { 12877545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 12882724Smaybee return; 12892887Smaybee } 12907545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 12917545SMark.Maybee@Sun.COM ASSERT(hdr != NULL); 12922887Smaybee hash_lock = HDR_LOCK(hdr); 12932724Smaybee mutex_enter(hash_lock); 12947545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 12957545SMark.Maybee@Sun.COM 12963403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 12971544Seschrock add_reference(hdr, hash_lock, tag); 12988582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 12992688Smaybee arc_access(hdr, hash_lock); 13002688Smaybee mutex_exit(hash_lock); 13013403Sbmc ARCSTAT_BUMP(arcstat_hits); 13023403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 13033403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 13043403Sbmc data, metadata, hits); 13051544Seschrock } 13061544Seschrock 13075450Sbrendan /* 13085450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 13095450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 13105450Sbrendan */ 13115450Sbrendan static void 13125450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 13135450Sbrendan void *data, size_t size) 13145450Sbrendan { 13155450Sbrendan if (HDR_L2_WRITING(hdr)) { 13165450Sbrendan l2arc_data_free_t *df; 13175450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 13185450Sbrendan df->l2df_data = data; 13195450Sbrendan df->l2df_size = size; 13205450Sbrendan df->l2df_func = free_func; 13215450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 13225450Sbrendan list_insert_head(l2arc_free_on_write, df); 13235450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 13245450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 13255450Sbrendan } else { 13265450Sbrendan free_func(data, size); 13275450Sbrendan } 13285450Sbrendan } 13295450Sbrendan 1330789Sahrens static void 13312688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 13321544Seschrock { 13331544Seschrock arc_buf_t **bufp; 13341544Seschrock 13351544Seschrock /* free up data associated with the buf */ 13361544Seschrock if (buf->b_data) { 13371544Seschrock arc_state_t *state = buf->b_hdr->b_state; 13381544Seschrock uint64_t size = buf->b_hdr->b_size; 13393290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 13401544Seschrock 13413093Sahrens arc_cksum_verify(buf); 134210922SJeff.Bonwick@Sun.COM 13432688Smaybee if (!recycle) { 13443290Sjohansen if (type == ARC_BUFC_METADATA) { 13455450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 13465450Sbrendan buf->b_data, size); 13478582SBrendan.Gregg@Sun.COM arc_space_return(size, ARC_SPACE_DATA); 13483290Sjohansen } else { 13493290Sjohansen ASSERT(type == ARC_BUFC_DATA); 13505450Sbrendan arc_buf_data_free(buf->b_hdr, 13515450Sbrendan zio_data_buf_free, buf->b_data, size); 13528582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -size); 13534309Smaybee atomic_add_64(&arc_size, -size); 13543290Sjohansen } 13552688Smaybee } 13561544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 13574309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 13584309Smaybee 13591544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 13603403Sbmc ASSERT(state != arc_anon); 13614309Smaybee 13624309Smaybee ASSERT3U(*cnt, >=, size); 13634309Smaybee atomic_add_64(cnt, -size); 13641544Seschrock } 13653403Sbmc ASSERT3U(state->arcs_size, >=, size); 13663403Sbmc atomic_add_64(&state->arcs_size, -size); 13671544Seschrock buf->b_data = NULL; 13681544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 13691544Seschrock buf->b_hdr->b_datacnt -= 1; 13701544Seschrock } 13711544Seschrock 13721544Seschrock /* only remove the buf if requested */ 13731544Seschrock if (!all) 13741544Seschrock return; 13751544Seschrock 13761544Seschrock /* remove the buf from the hdr list */ 13771544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 13781544Seschrock continue; 13791544Seschrock *bufp = buf->b_next; 13801544Seschrock 13811544Seschrock ASSERT(buf->b_efunc == NULL); 13821544Seschrock 13831544Seschrock /* clean up the buf */ 13841544Seschrock buf->b_hdr = NULL; 13851544Seschrock kmem_cache_free(buf_cache, buf); 13861544Seschrock } 13871544Seschrock 13881544Seschrock static void 13891544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1390789Sahrens { 1391789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 13923403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 13931544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 139410922SJeff.Bonwick@Sun.COM l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 139510922SJeff.Bonwick@Sun.COM 139610922SJeff.Bonwick@Sun.COM if (l2hdr != NULL) { 139710922SJeff.Bonwick@Sun.COM boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx); 139810922SJeff.Bonwick@Sun.COM /* 139910922SJeff.Bonwick@Sun.COM * To prevent arc_free() and l2arc_evict() from 140010922SJeff.Bonwick@Sun.COM * attempting to free the same buffer at the same time, 140110922SJeff.Bonwick@Sun.COM * a FREE_IN_PROGRESS flag is given to arc_free() to 140210922SJeff.Bonwick@Sun.COM * give it priority. l2arc_evict() can't destroy this 140310922SJeff.Bonwick@Sun.COM * header while we are waiting on l2arc_buflist_mtx. 140410922SJeff.Bonwick@Sun.COM * 140510922SJeff.Bonwick@Sun.COM * The hdr may be removed from l2ad_buflist before we 140610922SJeff.Bonwick@Sun.COM * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 140710922SJeff.Bonwick@Sun.COM */ 140810922SJeff.Bonwick@Sun.COM if (!buflist_held) { 14095450Sbrendan mutex_enter(&l2arc_buflist_mtx); 141010922SJeff.Bonwick@Sun.COM l2hdr = hdr->b_l2hdr; 141110922SJeff.Bonwick@Sun.COM } 141210922SJeff.Bonwick@Sun.COM 141310922SJeff.Bonwick@Sun.COM if (l2hdr != NULL) { 141410922SJeff.Bonwick@Sun.COM list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 141510922SJeff.Bonwick@Sun.COM ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 141610922SJeff.Bonwick@Sun.COM kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 141710922SJeff.Bonwick@Sun.COM if (hdr->b_state == arc_l2c_only) 141810922SJeff.Bonwick@Sun.COM l2arc_hdr_stat_remove(); 141910922SJeff.Bonwick@Sun.COM hdr->b_l2hdr = NULL; 142010922SJeff.Bonwick@Sun.COM } 142110922SJeff.Bonwick@Sun.COM 142210922SJeff.Bonwick@Sun.COM if (!buflist_held) 14235450Sbrendan mutex_exit(&l2arc_buflist_mtx); 14245450Sbrendan } 14255450Sbrendan 1426789Sahrens if (!BUF_EMPTY(hdr)) { 14271544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1428789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1429789Sahrens hdr->b_birth = 0; 1430789Sahrens hdr->b_cksum0 = 0; 1431789Sahrens } 14321544Seschrock while (hdr->b_buf) { 1433789Sahrens arc_buf_t *buf = hdr->b_buf; 1434789Sahrens 14351544Seschrock if (buf->b_efunc) { 14361544Seschrock mutex_enter(&arc_eviction_mtx); 14377545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 14381544Seschrock ASSERT(buf->b_hdr != NULL); 14392688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 14401544Seschrock hdr->b_buf = buf->b_next; 14412887Smaybee buf->b_hdr = &arc_eviction_hdr; 14421544Seschrock buf->b_next = arc_eviction_list; 14431544Seschrock arc_eviction_list = buf; 14447545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 14451544Seschrock mutex_exit(&arc_eviction_mtx); 14461544Seschrock } else { 14472688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 14481544Seschrock } 1449789Sahrens } 14503093Sahrens if (hdr->b_freeze_cksum != NULL) { 14513093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 14523093Sahrens hdr->b_freeze_cksum = NULL; 14533093Sahrens } 14541544Seschrock 1455789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1456789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1457789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1458789Sahrens kmem_cache_free(hdr_cache, hdr); 1459789Sahrens } 1460789Sahrens 1461789Sahrens void 1462789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1463789Sahrens { 1464789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 14653403Sbmc int hashed = hdr->b_state != arc_anon; 14661544Seschrock 14671544Seschrock ASSERT(buf->b_efunc == NULL); 14681544Seschrock ASSERT(buf->b_data != NULL); 14691544Seschrock 14701544Seschrock if (hashed) { 14711544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 14721544Seschrock 14731544Seschrock mutex_enter(hash_lock); 14741544Seschrock (void) remove_reference(hdr, hash_lock, tag); 147510922SJeff.Bonwick@Sun.COM if (hdr->b_datacnt > 1) { 14762688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 147710922SJeff.Bonwick@Sun.COM } else { 147810922SJeff.Bonwick@Sun.COM ASSERT(buf == hdr->b_buf); 147910922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 14801544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 148110922SJeff.Bonwick@Sun.COM } 14821544Seschrock mutex_exit(hash_lock); 14831544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 14841544Seschrock int destroy_hdr; 14851544Seschrock /* 14861544Seschrock * We are in the middle of an async write. Don't destroy 14871544Seschrock * this buffer unless the write completes before we finish 14881544Seschrock * decrementing the reference count. 14891544Seschrock */ 14901544Seschrock mutex_enter(&arc_eviction_mtx); 14911544Seschrock (void) remove_reference(hdr, NULL, tag); 14921544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 14931544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 14941544Seschrock mutex_exit(&arc_eviction_mtx); 14951544Seschrock if (destroy_hdr) 14961544Seschrock arc_hdr_destroy(hdr); 14971544Seschrock } else { 14981544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 14991544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 15002688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 15011544Seschrock } else { 15021544Seschrock arc_hdr_destroy(hdr); 15031544Seschrock } 15041544Seschrock } 15051544Seschrock } 15061544Seschrock 15071544Seschrock int 15081544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 15091544Seschrock { 15101544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1511789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 15121544Seschrock int no_callback = (buf->b_efunc == NULL); 15131544Seschrock 15143403Sbmc if (hdr->b_state == arc_anon) { 151510922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 15161544Seschrock arc_buf_free(buf, tag); 15171544Seschrock return (no_callback); 15181544Seschrock } 1519789Sahrens 1520789Sahrens mutex_enter(hash_lock); 15213403Sbmc ASSERT(hdr->b_state != arc_anon); 15221544Seschrock ASSERT(buf->b_data != NULL); 1523789Sahrens 15241544Seschrock (void) remove_reference(hdr, hash_lock, tag); 15251544Seschrock if (hdr->b_datacnt > 1) { 15261544Seschrock if (no_callback) 15272688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 15281544Seschrock } else if (no_callback) { 15291544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 153010922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 15311544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1532789Sahrens } 15331544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 15341544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1535789Sahrens mutex_exit(hash_lock); 15361544Seschrock return (no_callback); 1537789Sahrens } 1538789Sahrens 1539789Sahrens int 1540789Sahrens arc_buf_size(arc_buf_t *buf) 1541789Sahrens { 1542789Sahrens return (buf->b_hdr->b_size); 1543789Sahrens } 1544789Sahrens 1545789Sahrens /* 1546789Sahrens * Evict buffers from list until we've removed the specified number of 1547789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 15482688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 15492688Smaybee * - look for a buffer to evict that is `bytes' long. 15502688Smaybee * - return the data block from this buffer rather than freeing it. 15512688Smaybee * This flag is used by callers that are trying to make space for a 15522688Smaybee * new buffer in a full arc cache. 15535642Smaybee * 15545642Smaybee * This function makes a "best effort". It skips over any buffers 15555642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 15565642Smaybee * It may also return without evicting as much space as requested. 1557789Sahrens */ 15582688Smaybee static void * 15598636SMark.Maybee@Sun.COM arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 15603290Sjohansen arc_buf_contents_t type) 1561789Sahrens { 1562789Sahrens arc_state_t *evicted_state; 15632688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 15642918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 15654309Smaybee list_t *list = &state->arcs_list[type]; 1566789Sahrens kmutex_t *hash_lock; 15672688Smaybee boolean_t have_lock; 15682918Smaybee void *stolen = NULL; 1569789Sahrens 15703403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1571789Sahrens 15723403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1573789Sahrens 15743403Sbmc mutex_enter(&state->arcs_mtx); 15753403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1576789Sahrens 15774309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15784309Smaybee ab_prev = list_prev(list, ab); 15792391Smaybee /* prefetch buffers have a minimum lifespan */ 15802688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 15815642Smaybee (spa && ab->b_spa != spa) || 15822688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 1583*11066Srafael.vanoni@sun.com ddi_get_lbolt() - ab->b_arc_access < 1584*11066Srafael.vanoni@sun.com arc_min_prefetch_lifespan)) { 15852391Smaybee skipped++; 15862391Smaybee continue; 15872391Smaybee } 15882918Smaybee /* "lookahead" for better eviction candidate */ 15892918Smaybee if (recycle && ab->b_size != bytes && 15902918Smaybee ab_prev && ab_prev->b_size == bytes) 15912688Smaybee continue; 1592789Sahrens hash_lock = HDR_LOCK(ab); 15932688Smaybee have_lock = MUTEX_HELD(hash_lock); 15942688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1595789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 15961544Seschrock ASSERT(ab->b_datacnt > 0); 15971544Seschrock while (ab->b_buf) { 15981544Seschrock arc_buf_t *buf = ab->b_buf; 15997545SMark.Maybee@Sun.COM if (!rw_tryenter(&buf->b_lock, RW_WRITER)) { 16007545SMark.Maybee@Sun.COM missed += 1; 16017545SMark.Maybee@Sun.COM break; 16027545SMark.Maybee@Sun.COM } 16032688Smaybee if (buf->b_data) { 16041544Seschrock bytes_evicted += ab->b_size; 16053290Sjohansen if (recycle && ab->b_type == type && 16065450Sbrendan ab->b_size == bytes && 16075450Sbrendan !HDR_L2_WRITING(ab)) { 16082918Smaybee stolen = buf->b_data; 16092918Smaybee recycle = FALSE; 16102918Smaybee } 16112688Smaybee } 16121544Seschrock if (buf->b_efunc) { 16131544Seschrock mutex_enter(&arc_eviction_mtx); 16142918Smaybee arc_buf_destroy(buf, 16152918Smaybee buf->b_data == stolen, FALSE); 16161544Seschrock ab->b_buf = buf->b_next; 16172887Smaybee buf->b_hdr = &arc_eviction_hdr; 16181544Seschrock buf->b_next = arc_eviction_list; 16191544Seschrock arc_eviction_list = buf; 16201544Seschrock mutex_exit(&arc_eviction_mtx); 16217545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 16221544Seschrock } else { 16237545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 16242918Smaybee arc_buf_destroy(buf, 16252918Smaybee buf->b_data == stolen, TRUE); 16261544Seschrock } 16271544Seschrock } 162810357SBrendan.Gregg@Sun.COM 162910357SBrendan.Gregg@Sun.COM if (ab->b_l2hdr) { 163010357SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_evict_l2_cached, 163110357SBrendan.Gregg@Sun.COM ab->b_size); 163210357SBrendan.Gregg@Sun.COM } else { 163310357SBrendan.Gregg@Sun.COM if (l2arc_write_eligible(ab->b_spa, ab)) { 163410357SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_evict_l2_eligible, 163510357SBrendan.Gregg@Sun.COM ab->b_size); 163610357SBrendan.Gregg@Sun.COM } else { 163710357SBrendan.Gregg@Sun.COM ARCSTAT_INCR( 163810357SBrendan.Gregg@Sun.COM arcstat_evict_l2_ineligible, 163910357SBrendan.Gregg@Sun.COM ab->b_size); 164010357SBrendan.Gregg@Sun.COM } 164110357SBrendan.Gregg@Sun.COM } 164210357SBrendan.Gregg@Sun.COM 16437545SMark.Maybee@Sun.COM if (ab->b_datacnt == 0) { 16447545SMark.Maybee@Sun.COM arc_change_state(evicted_state, ab, hash_lock); 16457545SMark.Maybee@Sun.COM ASSERT(HDR_IN_HASH_TABLE(ab)); 16467545SMark.Maybee@Sun.COM ab->b_flags |= ARC_IN_HASH_TABLE; 16477545SMark.Maybee@Sun.COM ab->b_flags &= ~ARC_BUF_AVAILABLE; 16487545SMark.Maybee@Sun.COM DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 16497545SMark.Maybee@Sun.COM } 16502688Smaybee if (!have_lock) 16512688Smaybee mutex_exit(hash_lock); 16521544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1653789Sahrens break; 1654789Sahrens } else { 16552688Smaybee missed += 1; 1656789Sahrens } 1657789Sahrens } 16583403Sbmc 16593403Sbmc mutex_exit(&evicted_state->arcs_mtx); 16603403Sbmc mutex_exit(&state->arcs_mtx); 1661789Sahrens 1662789Sahrens if (bytes_evicted < bytes) 1663789Sahrens dprintf("only evicted %lld bytes from %x", 1664789Sahrens (longlong_t)bytes_evicted, state); 1665789Sahrens 16662688Smaybee if (skipped) 16673403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 16683403Sbmc 16692688Smaybee if (missed) 16703403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 16713403Sbmc 16724709Smaybee /* 16734709Smaybee * We have just evicted some date into the ghost state, make 16744709Smaybee * sure we also adjust the ghost state size if necessary. 16754709Smaybee */ 16764709Smaybee if (arc_no_grow && 16774709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 16784709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 16794709Smaybee arc_mru_ghost->arcs_size - arc_c; 16804709Smaybee 16814709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 16824709Smaybee int64_t todelete = 16834709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 16845642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 16854709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 16864709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 16874709Smaybee arc_mru_ghost->arcs_size + 16884709Smaybee arc_mfu_ghost->arcs_size - arc_c); 16895642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 16904709Smaybee } 16914709Smaybee } 16924709Smaybee 16932918Smaybee return (stolen); 1694789Sahrens } 1695789Sahrens 1696789Sahrens /* 1697789Sahrens * Remove buffers from list until we've removed the specified number of 1698789Sahrens * bytes. Destroy the buffers that are removed. 1699789Sahrens */ 1700789Sahrens static void 17018636SMark.Maybee@Sun.COM arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 1702789Sahrens { 1703789Sahrens arc_buf_hdr_t *ab, *ab_prev; 17044309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1705789Sahrens kmutex_t *hash_lock; 17061544Seschrock uint64_t bytes_deleted = 0; 17073700Sek110237 uint64_t bufs_skipped = 0; 1708789Sahrens 17091544Seschrock ASSERT(GHOST_STATE(state)); 1710789Sahrens top: 17113403Sbmc mutex_enter(&state->arcs_mtx); 17124309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 17134309Smaybee ab_prev = list_prev(list, ab); 17145642Smaybee if (spa && ab->b_spa != spa) 17155642Smaybee continue; 1716789Sahrens hash_lock = HDR_LOCK(ab); 1717789Sahrens if (mutex_tryenter(hash_lock)) { 17182391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 17191544Seschrock ASSERT(ab->b_buf == NULL); 17203403Sbmc ARCSTAT_BUMP(arcstat_deleted); 17211544Seschrock bytes_deleted += ab->b_size; 17225450Sbrendan 17235450Sbrendan if (ab->b_l2hdr != NULL) { 17245450Sbrendan /* 17255450Sbrendan * This buffer is cached on the 2nd Level ARC; 17265450Sbrendan * don't destroy the header. 17275450Sbrendan */ 17285450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 17295450Sbrendan mutex_exit(hash_lock); 17305450Sbrendan } else { 17315450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 17325450Sbrendan mutex_exit(hash_lock); 17335450Sbrendan arc_hdr_destroy(ab); 17345450Sbrendan } 17355450Sbrendan 1736789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1737789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1738789Sahrens break; 1739789Sahrens } else { 1740789Sahrens if (bytes < 0) { 17413403Sbmc mutex_exit(&state->arcs_mtx); 1742789Sahrens mutex_enter(hash_lock); 1743789Sahrens mutex_exit(hash_lock); 1744789Sahrens goto top; 1745789Sahrens } 1746789Sahrens bufs_skipped += 1; 1747789Sahrens } 1748789Sahrens } 17493403Sbmc mutex_exit(&state->arcs_mtx); 1750789Sahrens 17514309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 17524309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 17534309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 17544309Smaybee goto top; 17554309Smaybee } 17564309Smaybee 1757789Sahrens if (bufs_skipped) { 17583403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1759789Sahrens ASSERT(bytes >= 0); 1760789Sahrens } 1761789Sahrens 1762789Sahrens if (bytes_deleted < bytes) 1763789Sahrens dprintf("only deleted %lld bytes from %p", 1764789Sahrens (longlong_t)bytes_deleted, state); 1765789Sahrens } 1766789Sahrens 1767789Sahrens static void 1768789Sahrens arc_adjust(void) 1769789Sahrens { 17708582SBrendan.Gregg@Sun.COM int64_t adjustment, delta; 17718582SBrendan.Gregg@Sun.COM 17728582SBrendan.Gregg@Sun.COM /* 17738582SBrendan.Gregg@Sun.COM * Adjust MRU size 17748582SBrendan.Gregg@Sun.COM */ 17758582SBrendan.Gregg@Sun.COM 17768582SBrendan.Gregg@Sun.COM adjustment = MIN(arc_size - arc_c, 17778582SBrendan.Gregg@Sun.COM arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - arc_p); 17788582SBrendan.Gregg@Sun.COM 17798582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 17808582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 17818582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA); 17828582SBrendan.Gregg@Sun.COM adjustment -= delta; 17834309Smaybee } 17844309Smaybee 17858582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 17868582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 17878582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, 17885642Smaybee ARC_BUFC_METADATA); 1789789Sahrens } 1790789Sahrens 17918582SBrendan.Gregg@Sun.COM /* 17928582SBrendan.Gregg@Sun.COM * Adjust MFU size 17938582SBrendan.Gregg@Sun.COM */ 17948582SBrendan.Gregg@Sun.COM 17958582SBrendan.Gregg@Sun.COM adjustment = arc_size - arc_c; 17968582SBrendan.Gregg@Sun.COM 17978582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 17988582SBrendan.Gregg@Sun.COM delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 17998582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA); 18008582SBrendan.Gregg@Sun.COM adjustment -= delta; 18018582SBrendan.Gregg@Sun.COM } 18028582SBrendan.Gregg@Sun.COM 18038582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 18048582SBrendan.Gregg@Sun.COM int64_t delta = MIN(adjustment, 18058582SBrendan.Gregg@Sun.COM arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 18068582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, 18078582SBrendan.Gregg@Sun.COM ARC_BUFC_METADATA); 18088582SBrendan.Gregg@Sun.COM } 18098582SBrendan.Gregg@Sun.COM 18108582SBrendan.Gregg@Sun.COM /* 18118582SBrendan.Gregg@Sun.COM * Adjust ghost lists 18128582SBrendan.Gregg@Sun.COM */ 18138582SBrendan.Gregg@Sun.COM 18148582SBrendan.Gregg@Sun.COM adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 18158582SBrendan.Gregg@Sun.COM 18168582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 18178582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru_ghost->arcs_size, adjustment); 18188582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mru_ghost, NULL, delta); 18198582SBrendan.Gregg@Sun.COM } 18208582SBrendan.Gregg@Sun.COM 18218582SBrendan.Gregg@Sun.COM adjustment = 18228582SBrendan.Gregg@Sun.COM arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 18238582SBrendan.Gregg@Sun.COM 18248582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 18258582SBrendan.Gregg@Sun.COM delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 18268582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mfu_ghost, NULL, delta); 1827789Sahrens } 1828789Sahrens } 1829789Sahrens 18301544Seschrock static void 18311544Seschrock arc_do_user_evicts(void) 18321544Seschrock { 18331544Seschrock mutex_enter(&arc_eviction_mtx); 18341544Seschrock while (arc_eviction_list != NULL) { 18351544Seschrock arc_buf_t *buf = arc_eviction_list; 18361544Seschrock arc_eviction_list = buf->b_next; 18377545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 18381544Seschrock buf->b_hdr = NULL; 18397545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 18401544Seschrock mutex_exit(&arc_eviction_mtx); 18411544Seschrock 18421819Smaybee if (buf->b_efunc != NULL) 18431819Smaybee VERIFY(buf->b_efunc(buf) == 0); 18441544Seschrock 18451544Seschrock buf->b_efunc = NULL; 18461544Seschrock buf->b_private = NULL; 18471544Seschrock kmem_cache_free(buf_cache, buf); 18481544Seschrock mutex_enter(&arc_eviction_mtx); 18491544Seschrock } 18501544Seschrock mutex_exit(&arc_eviction_mtx); 18511544Seschrock } 18521544Seschrock 1853789Sahrens /* 18545642Smaybee * Flush all *evictable* data from the cache for the given spa. 1855789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1856789Sahrens */ 1857789Sahrens void 18585642Smaybee arc_flush(spa_t *spa) 1859789Sahrens { 18608636SMark.Maybee@Sun.COM uint64_t guid = 0; 18618636SMark.Maybee@Sun.COM 18628636SMark.Maybee@Sun.COM if (spa) 18638636SMark.Maybee@Sun.COM guid = spa_guid(spa); 18648636SMark.Maybee@Sun.COM 18655642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 18668636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 18675642Smaybee if (spa) 18685642Smaybee break; 18695642Smaybee } 18705642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 18718636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 18725642Smaybee if (spa) 18735642Smaybee break; 18745642Smaybee } 18755642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 18768636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 18775642Smaybee if (spa) 18785642Smaybee break; 18795642Smaybee } 18805642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 18818636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 18825642Smaybee if (spa) 18835642Smaybee break; 18845642Smaybee } 18855642Smaybee 18868636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mru_ghost, guid, -1); 18878636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mfu_ghost, guid, -1); 18881544Seschrock 18891544Seschrock mutex_enter(&arc_reclaim_thr_lock); 18901544Seschrock arc_do_user_evicts(); 18911544Seschrock mutex_exit(&arc_reclaim_thr_lock); 18925642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1893789Sahrens } 1894789Sahrens 1895789Sahrens void 18963158Smaybee arc_shrink(void) 1897789Sahrens { 18983403Sbmc if (arc_c > arc_c_min) { 18993158Smaybee uint64_t to_free; 1900789Sahrens 19012048Sstans #ifdef _KERNEL 19023403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 19032048Sstans #else 19043403Sbmc to_free = arc_c >> arc_shrink_shift; 19052048Sstans #endif 19063403Sbmc if (arc_c > arc_c_min + to_free) 19073403Sbmc atomic_add_64(&arc_c, -to_free); 19083158Smaybee else 19093403Sbmc arc_c = arc_c_min; 19102048Sstans 19113403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 19123403Sbmc if (arc_c > arc_size) 19133403Sbmc arc_c = MAX(arc_size, arc_c_min); 19143403Sbmc if (arc_p > arc_c) 19153403Sbmc arc_p = (arc_c >> 1); 19163403Sbmc ASSERT(arc_c >= arc_c_min); 19173403Sbmc ASSERT((int64_t)arc_p >= 0); 19183158Smaybee } 1919789Sahrens 19203403Sbmc if (arc_size > arc_c) 19213158Smaybee arc_adjust(); 1922789Sahrens } 1923789Sahrens 1924789Sahrens static int 1925789Sahrens arc_reclaim_needed(void) 1926789Sahrens { 1927789Sahrens uint64_t extra; 1928789Sahrens 1929789Sahrens #ifdef _KERNEL 19302048Sstans 19312048Sstans if (needfree) 19322048Sstans return (1); 19332048Sstans 1934789Sahrens /* 1935789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1936789Sahrens */ 1937789Sahrens extra = desfree; 1938789Sahrens 1939789Sahrens /* 1940789Sahrens * check that we're out of range of the pageout scanner. It starts to 1941789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1942789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1943789Sahrens * number of needed free pages. We add extra pages here to make sure 1944789Sahrens * the scanner doesn't start up while we're freeing memory. 1945789Sahrens */ 1946789Sahrens if (freemem < lotsfree + needfree + extra) 1947789Sahrens return (1); 1948789Sahrens 1949789Sahrens /* 1950789Sahrens * check to make sure that swapfs has enough space so that anon 19515450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1952789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1953789Sahrens * swap pages. We also add a bit of extra here just to prevent 1954789Sahrens * circumstances from getting really dire. 1955789Sahrens */ 1956789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1957789Sahrens return (1); 1958789Sahrens 19591936Smaybee #if defined(__i386) 1960789Sahrens /* 1961789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1962789Sahrens * kernel heap space before we ever run out of available physical 1963789Sahrens * memory. Most checks of the size of the heap_area compare against 1964789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1965789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1966789Sahrens * which is so low that it's useless. In this comparison, we seek to 1967789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 19685450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1969789Sahrens * free) 1970789Sahrens */ 1971789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1972789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1973789Sahrens return (1); 1974789Sahrens #endif 1975789Sahrens 1976789Sahrens #else 1977789Sahrens if (spa_get_random(100) == 0) 1978789Sahrens return (1); 1979789Sahrens #endif 1980789Sahrens return (0); 1981789Sahrens } 1982789Sahrens 1983789Sahrens static void 1984789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1985789Sahrens { 1986789Sahrens size_t i; 1987789Sahrens kmem_cache_t *prev_cache = NULL; 19883290Sjohansen kmem_cache_t *prev_data_cache = NULL; 1989789Sahrens extern kmem_cache_t *zio_buf_cache[]; 19903290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 1991789Sahrens 19921484Sek110237 #ifdef _KERNEL 19934309Smaybee if (arc_meta_used >= arc_meta_limit) { 19944309Smaybee /* 19954309Smaybee * We are exceeding our meta-data cache limit. 19964309Smaybee * Purge some DNLC entries to release holds on meta-data. 19974309Smaybee */ 19984309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 19994309Smaybee } 20001936Smaybee #if defined(__i386) 20011936Smaybee /* 20021936Smaybee * Reclaim unused memory from all kmem caches. 20031936Smaybee */ 20041936Smaybee kmem_reap(); 20051936Smaybee #endif 20061484Sek110237 #endif 20071484Sek110237 2008789Sahrens /* 20095450Sbrendan * An aggressive reclamation will shrink the cache size as well as 20101544Seschrock * reap free buffers from the arc kmem caches. 2011789Sahrens */ 2012789Sahrens if (strat == ARC_RECLAIM_AGGR) 20133158Smaybee arc_shrink(); 2014789Sahrens 2015789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2016789Sahrens if (zio_buf_cache[i] != prev_cache) { 2017789Sahrens prev_cache = zio_buf_cache[i]; 2018789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 2019789Sahrens } 20203290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 20213290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 20223290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 20233290Sjohansen } 2024789Sahrens } 20251544Seschrock kmem_cache_reap_now(buf_cache); 20261544Seschrock kmem_cache_reap_now(hdr_cache); 2027789Sahrens } 2028789Sahrens 2029789Sahrens static void 2030789Sahrens arc_reclaim_thread(void) 2031789Sahrens { 2032789Sahrens clock_t growtime = 0; 2033789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2034789Sahrens callb_cpr_t cpr; 2035789Sahrens 2036789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2037789Sahrens 2038789Sahrens mutex_enter(&arc_reclaim_thr_lock); 2039789Sahrens while (arc_thread_exit == 0) { 2040789Sahrens if (arc_reclaim_needed()) { 2041789Sahrens 20423403Sbmc if (arc_no_grow) { 2043789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 2044789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2045789Sahrens } else { 2046789Sahrens last_reclaim = ARC_RECLAIM_CONS; 2047789Sahrens } 2048789Sahrens } else { 20493403Sbmc arc_no_grow = TRUE; 2050789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2051789Sahrens membar_producer(); 2052789Sahrens } 2053789Sahrens 2054789Sahrens /* reset the growth delay for every reclaim */ 2055*11066Srafael.vanoni@sun.com growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 2056789Sahrens 2057789Sahrens arc_kmem_reap_now(last_reclaim); 20586987Sbrendan arc_warm = B_TRUE; 2059789Sahrens 2060*11066Srafael.vanoni@sun.com } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 20613403Sbmc arc_no_grow = FALSE; 2062789Sahrens } 2063789Sahrens 20643403Sbmc if (2 * arc_c < arc_size + 20653403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 20663298Smaybee arc_adjust(); 20673298Smaybee 20681544Seschrock if (arc_eviction_list != NULL) 20691544Seschrock arc_do_user_evicts(); 20701544Seschrock 2071789Sahrens /* block until needed, or one second, whichever is shorter */ 2072789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 2073789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 2074*11066Srafael.vanoni@sun.com &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz)); 2075789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2076789Sahrens } 2077789Sahrens 2078789Sahrens arc_thread_exit = 0; 2079789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 2080789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2081789Sahrens thread_exit(); 2082789Sahrens } 2083789Sahrens 20841544Seschrock /* 20851544Seschrock * Adapt arc info given the number of bytes we are trying to add and 20861544Seschrock * the state that we are comming from. This function is only called 20871544Seschrock * when we are adding new content to the cache. 20881544Seschrock */ 2089789Sahrens static void 20901544Seschrock arc_adapt(int bytes, arc_state_t *state) 2091789Sahrens { 20921544Seschrock int mult; 20938582SBrendan.Gregg@Sun.COM uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 20941544Seschrock 20955450Sbrendan if (state == arc_l2c_only) 20965450Sbrendan return; 20975450Sbrendan 20981544Seschrock ASSERT(bytes > 0); 2099789Sahrens /* 21001544Seschrock * Adapt the target size of the MRU list: 21011544Seschrock * - if we just hit in the MRU ghost list, then increase 21021544Seschrock * the target size of the MRU list. 21031544Seschrock * - if we just hit in the MFU ghost list, then increase 21041544Seschrock * the target size of the MFU list by decreasing the 21051544Seschrock * target size of the MRU list. 2106789Sahrens */ 21073403Sbmc if (state == arc_mru_ghost) { 21083403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 21093403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 21101544Seschrock 21118582SBrendan.Gregg@Sun.COM arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 21123403Sbmc } else if (state == arc_mfu_ghost) { 21138582SBrendan.Gregg@Sun.COM uint64_t delta; 21148582SBrendan.Gregg@Sun.COM 21153403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 21163403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 21171544Seschrock 21188582SBrendan.Gregg@Sun.COM delta = MIN(bytes * mult, arc_p); 21198582SBrendan.Gregg@Sun.COM arc_p = MAX(arc_p_min, arc_p - delta); 21201544Seschrock } 21213403Sbmc ASSERT((int64_t)arc_p >= 0); 2122789Sahrens 2123789Sahrens if (arc_reclaim_needed()) { 2124789Sahrens cv_signal(&arc_reclaim_thr_cv); 2125789Sahrens return; 2126789Sahrens } 2127789Sahrens 21283403Sbmc if (arc_no_grow) 2129789Sahrens return; 2130789Sahrens 21313403Sbmc if (arc_c >= arc_c_max) 21321544Seschrock return; 21331544Seschrock 2134789Sahrens /* 21351544Seschrock * If we're within (2 * maxblocksize) bytes of the target 21361544Seschrock * cache size, increment the target cache size 2137789Sahrens */ 21383403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 21393403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 21403403Sbmc if (arc_c > arc_c_max) 21413403Sbmc arc_c = arc_c_max; 21423403Sbmc else if (state == arc_anon) 21433403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 21443403Sbmc if (arc_p > arc_c) 21453403Sbmc arc_p = arc_c; 2146789Sahrens } 21473403Sbmc ASSERT((int64_t)arc_p >= 0); 2148789Sahrens } 2149789Sahrens 2150789Sahrens /* 21511544Seschrock * Check if the cache has reached its limits and eviction is required 21521544Seschrock * prior to insert. 2153789Sahrens */ 2154789Sahrens static int 21554309Smaybee arc_evict_needed(arc_buf_contents_t type) 2156789Sahrens { 21574309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 21584309Smaybee return (1); 21594309Smaybee 21604309Smaybee #ifdef _KERNEL 21614309Smaybee /* 21624309Smaybee * If zio data pages are being allocated out of a separate heap segment, 21634309Smaybee * then enforce that the size of available vmem for this area remains 21644309Smaybee * above about 1/32nd free. 21654309Smaybee */ 21664309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 21674309Smaybee vmem_size(zio_arena, VMEM_FREE) < 21684309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 21694309Smaybee return (1); 21704309Smaybee #endif 21714309Smaybee 2172789Sahrens if (arc_reclaim_needed()) 2173789Sahrens return (1); 2174789Sahrens 21753403Sbmc return (arc_size > arc_c); 2176789Sahrens } 2177789Sahrens 2178789Sahrens /* 21792688Smaybee * The buffer, supplied as the first argument, needs a data block. 21802688Smaybee * So, if we are at cache max, determine which cache should be victimized. 21812688Smaybee * We have the following cases: 2182789Sahrens * 21833403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2184789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2185789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2186789Sahrens * 21873403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2188789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2189789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2190789Sahrens * entries. 2191789Sahrens * 21923403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2193789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2194789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2195789Sahrens * the MFU side, so the MRU side needs to be victimized. 2196789Sahrens * 21973403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2198789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2199789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2200789Sahrens */ 2201789Sahrens static void 22022688Smaybee arc_get_data_buf(arc_buf_t *buf) 2203789Sahrens { 22043290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 22053290Sjohansen uint64_t size = buf->b_hdr->b_size; 22063290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 22072688Smaybee 22082688Smaybee arc_adapt(size, state); 2209789Sahrens 22102688Smaybee /* 22112688Smaybee * We have not yet reached cache maximum size, 22122688Smaybee * just allocate a new buffer. 22132688Smaybee */ 22144309Smaybee if (!arc_evict_needed(type)) { 22153290Sjohansen if (type == ARC_BUFC_METADATA) { 22163290Sjohansen buf->b_data = zio_buf_alloc(size); 22178582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 22183290Sjohansen } else { 22193290Sjohansen ASSERT(type == ARC_BUFC_DATA); 22203290Sjohansen buf->b_data = zio_data_buf_alloc(size); 22218582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 22224309Smaybee atomic_add_64(&arc_size, size); 22233290Sjohansen } 22242688Smaybee goto out; 22252688Smaybee } 22262688Smaybee 22272688Smaybee /* 22282688Smaybee * If we are prefetching from the mfu ghost list, this buffer 22292688Smaybee * will end up on the mru list; so steal space from there. 22302688Smaybee */ 22313403Sbmc if (state == arc_mfu_ghost) 22323403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 22333403Sbmc else if (state == arc_mru_ghost) 22343403Sbmc state = arc_mru; 2235789Sahrens 22363403Sbmc if (state == arc_mru || state == arc_anon) { 22373403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 22388582SBrendan.Gregg@Sun.COM state = (arc_mfu->arcs_lsize[type] >= size && 22394309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2240789Sahrens } else { 22412688Smaybee /* MFU cases */ 22423403Sbmc uint64_t mfu_space = arc_c - arc_p; 22438582SBrendan.Gregg@Sun.COM state = (arc_mru->arcs_lsize[type] >= size && 22444309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 22452688Smaybee } 22465642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 22473290Sjohansen if (type == ARC_BUFC_METADATA) { 22483290Sjohansen buf->b_data = zio_buf_alloc(size); 22498582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 22503290Sjohansen } else { 22513290Sjohansen ASSERT(type == ARC_BUFC_DATA); 22523290Sjohansen buf->b_data = zio_data_buf_alloc(size); 22538582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 22544309Smaybee atomic_add_64(&arc_size, size); 22553290Sjohansen } 22563403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 22572688Smaybee } 22582688Smaybee ASSERT(buf->b_data != NULL); 22592688Smaybee out: 22602688Smaybee /* 22612688Smaybee * Update the state size. Note that ghost states have a 22622688Smaybee * "ghost size" and so don't need to be updated. 22632688Smaybee */ 22642688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 22652688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 22662688Smaybee 22673403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 22682688Smaybee if (list_link_active(&hdr->b_arc_node)) { 22692688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 22704309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2271789Sahrens } 22723298Smaybee /* 22733298Smaybee * If we are growing the cache, and we are adding anonymous 22743403Sbmc * data, and we have outgrown arc_p, update arc_p 22753298Smaybee */ 22763403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 22773403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 22783403Sbmc arc_p = MIN(arc_c, arc_p + size); 2279789Sahrens } 2280789Sahrens } 2281789Sahrens 2282789Sahrens /* 2283789Sahrens * This routine is called whenever a buffer is accessed. 22841544Seschrock * NOTE: the hash lock is dropped in this function. 2285789Sahrens */ 2286789Sahrens static void 22872688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2288789Sahrens { 2289*11066Srafael.vanoni@sun.com clock_t now; 2290*11066Srafael.vanoni@sun.com 2291789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2292789Sahrens 22933403Sbmc if (buf->b_state == arc_anon) { 2294789Sahrens /* 2295789Sahrens * This buffer is not in the cache, and does not 2296789Sahrens * appear in our "ghost" list. Add the new buffer 2297789Sahrens * to the MRU state. 2298789Sahrens */ 2299789Sahrens 2300789Sahrens ASSERT(buf->b_arc_access == 0); 2301*11066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 23021544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 23033403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2304789Sahrens 23053403Sbmc } else if (buf->b_state == arc_mru) { 2306*11066Srafael.vanoni@sun.com now = ddi_get_lbolt(); 2307*11066Srafael.vanoni@sun.com 2308789Sahrens /* 23092391Smaybee * If this buffer is here because of a prefetch, then either: 23102391Smaybee * - clear the flag if this is a "referencing" read 23112391Smaybee * (any subsequent access will bump this into the MFU state). 23122391Smaybee * or 23132391Smaybee * - move the buffer to the head of the list if this is 23142391Smaybee * another prefetch (to make it less likely to be evicted). 2315789Sahrens */ 2316789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 23172391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 23182391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23192391Smaybee } else { 23202391Smaybee buf->b_flags &= ~ARC_PREFETCH; 23213403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23222391Smaybee } 2323*11066Srafael.vanoni@sun.com buf->b_arc_access = now; 2324789Sahrens return; 2325789Sahrens } 2326789Sahrens 2327789Sahrens /* 2328789Sahrens * This buffer has been "accessed" only once so far, 2329789Sahrens * but it is still in the cache. Move it to the MFU 2330789Sahrens * state. 2331789Sahrens */ 2332*11066Srafael.vanoni@sun.com if (now > buf->b_arc_access + ARC_MINTIME) { 2333789Sahrens /* 2334789Sahrens * More than 125ms have passed since we 2335789Sahrens * instantiated this buffer. Move it to the 2336789Sahrens * most frequently used state. 2337789Sahrens */ 2338*11066Srafael.vanoni@sun.com buf->b_arc_access = now; 23391544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23403403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2341789Sahrens } 23423403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23433403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2344789Sahrens arc_state_t *new_state; 2345789Sahrens /* 2346789Sahrens * This buffer has been "accessed" recently, but 2347789Sahrens * was evicted from the cache. Move it to the 2348789Sahrens * MFU state. 2349789Sahrens */ 2350789Sahrens 2351789Sahrens if (buf->b_flags & ARC_PREFETCH) { 23523403Sbmc new_state = arc_mru; 23532391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 23542391Smaybee buf->b_flags &= ~ARC_PREFETCH; 23551544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2356789Sahrens } else { 23573403Sbmc new_state = arc_mfu; 23581544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2359789Sahrens } 2360789Sahrens 2361*11066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 2362789Sahrens arc_change_state(new_state, buf, hash_lock); 2363789Sahrens 23643403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 23653403Sbmc } else if (buf->b_state == arc_mfu) { 2366789Sahrens /* 2367789Sahrens * This buffer has been accessed more than once and is 2368789Sahrens * still in the cache. Keep it in the MFU state. 2369789Sahrens * 23702391Smaybee * NOTE: an add_reference() that occurred when we did 23712391Smaybee * the arc_read() will have kicked this off the list. 23722391Smaybee * If it was a prefetch, we will explicitly move it to 23732391Smaybee * the head of the list now. 2374789Sahrens */ 23752391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 23762391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 23772391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23782391Smaybee } 23793403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 2380*11066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 23813403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 23823403Sbmc arc_state_t *new_state = arc_mfu; 2383789Sahrens /* 2384789Sahrens * This buffer has been accessed more than once but has 2385789Sahrens * been evicted from the cache. Move it back to the 2386789Sahrens * MFU state. 2387789Sahrens */ 2388789Sahrens 23892391Smaybee if (buf->b_flags & ARC_PREFETCH) { 23902391Smaybee /* 23912391Smaybee * This is a prefetch access... 23922391Smaybee * move this block back to the MRU state. 23932391Smaybee */ 23942391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 23953403Sbmc new_state = arc_mru; 23962391Smaybee } 23972391Smaybee 2398*11066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 23991544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 24002391Smaybee arc_change_state(new_state, buf, hash_lock); 2401789Sahrens 24023403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 24035450Sbrendan } else if (buf->b_state == arc_l2c_only) { 24045450Sbrendan /* 24055450Sbrendan * This buffer is on the 2nd Level ARC. 24065450Sbrendan */ 24075450Sbrendan 2408*11066Srafael.vanoni@sun.com buf->b_arc_access = ddi_get_lbolt(); 24095450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 24105450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2411789Sahrens } else { 2412789Sahrens ASSERT(!"invalid arc state"); 2413789Sahrens } 2414789Sahrens } 2415789Sahrens 2416789Sahrens /* a generic arc_done_func_t which you can use */ 2417789Sahrens /* ARGSUSED */ 2418789Sahrens void 2419789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2420789Sahrens { 2421789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 24221544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2423789Sahrens } 2424789Sahrens 24254309Smaybee /* a generic arc_done_func_t */ 2426789Sahrens void 2427789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2428789Sahrens { 2429789Sahrens arc_buf_t **bufp = arg; 2430789Sahrens if (zio && zio->io_error) { 24311544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2432789Sahrens *bufp = NULL; 2433789Sahrens } else { 2434789Sahrens *bufp = buf; 2435789Sahrens } 2436789Sahrens } 2437789Sahrens 2438789Sahrens static void 2439789Sahrens arc_read_done(zio_t *zio) 2440789Sahrens { 24411589Smaybee arc_buf_hdr_t *hdr, *found; 2442789Sahrens arc_buf_t *buf; 2443789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2444789Sahrens kmutex_t *hash_lock; 2445789Sahrens arc_callback_t *callback_list, *acb; 2446789Sahrens int freeable = FALSE; 2447789Sahrens 2448789Sahrens buf = zio->io_private; 2449789Sahrens hdr = buf->b_hdr; 2450789Sahrens 24511589Smaybee /* 24521589Smaybee * The hdr was inserted into hash-table and removed from lists 24531589Smaybee * prior to starting I/O. We should find this header, since 24541589Smaybee * it's in the hash table, and it should be legit since it's 24551589Smaybee * not possible to evict it during the I/O. The only possible 24561589Smaybee * reason for it not to be found is if we were freed during the 24571589Smaybee * read. 24581589Smaybee */ 24598636SMark.Maybee@Sun.COM found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 24603093Sahrens &hash_lock); 2461789Sahrens 24621589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 24635450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 24645450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 24655450Sbrendan 24666987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 24675450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 24687237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2469789Sahrens 2470789Sahrens /* byteswap if necessary */ 2471789Sahrens callback_list = hdr->b_acb; 2472789Sahrens ASSERT(callback_list != NULL); 247310839Swilliam.gorrell@sun.com if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 24747046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 24757046Sahrens byteswap_uint64_array : 24767046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 24777046Sahrens func(buf->b_data, hdr->b_size); 24787046Sahrens } 2479789Sahrens 24805450Sbrendan arc_cksum_compute(buf, B_FALSE); 24813093Sahrens 248210922SJeff.Bonwick@Sun.COM if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 248310922SJeff.Bonwick@Sun.COM /* 248410922SJeff.Bonwick@Sun.COM * Only call arc_access on anonymous buffers. This is because 248510922SJeff.Bonwick@Sun.COM * if we've issued an I/O for an evicted buffer, we've already 248610922SJeff.Bonwick@Sun.COM * called arc_access (to prevent any simultaneous readers from 248710922SJeff.Bonwick@Sun.COM * getting confused). 248810922SJeff.Bonwick@Sun.COM */ 248910922SJeff.Bonwick@Sun.COM arc_access(hdr, hash_lock); 249010922SJeff.Bonwick@Sun.COM } 249110922SJeff.Bonwick@Sun.COM 2492789Sahrens /* create copies of the data buffer for the callers */ 2493789Sahrens abuf = buf; 2494789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2495789Sahrens if (acb->acb_done) { 24962688Smaybee if (abuf == NULL) 24972688Smaybee abuf = arc_buf_clone(buf); 2498789Sahrens acb->acb_buf = abuf; 2499789Sahrens abuf = NULL; 2500789Sahrens } 2501789Sahrens } 2502789Sahrens hdr->b_acb = NULL; 2503789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 25041544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 250510922SJeff.Bonwick@Sun.COM if (abuf == buf) { 250610922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 250710922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 25081544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 250910922SJeff.Bonwick@Sun.COM } 2510789Sahrens 2511789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2512789Sahrens 2513789Sahrens if (zio->io_error != 0) { 2514789Sahrens hdr->b_flags |= ARC_IO_ERROR; 25153403Sbmc if (hdr->b_state != arc_anon) 25163403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 25171544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 25181544Seschrock buf_hash_remove(hdr); 2519789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2520789Sahrens } 2521789Sahrens 25221544Seschrock /* 25232391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 25242391Smaybee * that the hdr (and hence the cv) might be freed before we get to 25252391Smaybee * the cv_broadcast(). 25261544Seschrock */ 25271544Seschrock cv_broadcast(&hdr->b_cv); 25281544Seschrock 25291589Smaybee if (hash_lock) { 25302688Smaybee mutex_exit(hash_lock); 2531789Sahrens } else { 2532789Sahrens /* 2533789Sahrens * This block was freed while we waited for the read to 2534789Sahrens * complete. It has been removed from the hash table and 2535789Sahrens * moved to the anonymous state (so that it won't show up 2536789Sahrens * in the cache). 2537789Sahrens */ 25383403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2539789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2540789Sahrens } 2541789Sahrens 2542789Sahrens /* execute each callback and free its structure */ 2543789Sahrens while ((acb = callback_list) != NULL) { 2544789Sahrens if (acb->acb_done) 2545789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2546789Sahrens 2547789Sahrens if (acb->acb_zio_dummy != NULL) { 2548789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2549789Sahrens zio_nowait(acb->acb_zio_dummy); 2550789Sahrens } 2551789Sahrens 2552789Sahrens callback_list = acb->acb_next; 2553789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2554789Sahrens } 2555789Sahrens 2556789Sahrens if (freeable) 25571544Seschrock arc_hdr_destroy(hdr); 2558789Sahrens } 2559789Sahrens 2560789Sahrens /* 2561789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2562789Sahrens * cache. If the block is found in the cache, invoke the provided 2563789Sahrens * callback immediately and return. Note that the `zio' parameter 2564789Sahrens * in the callback will be NULL in this case, since no IO was 2565789Sahrens * required. If the block is not in the cache pass the read request 2566789Sahrens * on to the spa with a substitute callback function, so that the 2567789Sahrens * requested block will be added to the cache. 2568789Sahrens * 2569789Sahrens * If a read request arrives for a block that has a read in-progress, 2570789Sahrens * either wait for the in-progress read to complete (and return the 2571789Sahrens * results); or, if this is a read with a "done" func, add a record 2572789Sahrens * to the read to invoke the "done" func when the read completes, 2573789Sahrens * and return; or just return. 2574789Sahrens * 2575789Sahrens * arc_read_done() will invoke all the requested "done" functions 2576789Sahrens * for readers of this block. 25777046Sahrens * 25787046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 25797046Sahrens * for the bp. But if you know you don't need locking, you can use 25808213SSuhasini.Peddada@Sun.COM * arc_read_bp. 2581789Sahrens */ 2582789Sahrens int 258310922SJeff.Bonwick@Sun.COM arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf, 25847237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 25857046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 25867046Sahrens { 25877046Sahrens int err; 25887046Sahrens 25897046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 25907046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 25917545SMark.Maybee@Sun.COM rw_enter(&pbuf->b_lock, RW_READER); 25927046Sahrens 25937046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 25947237Sek110237 zio_flags, arc_flags, zb); 25957545SMark.Maybee@Sun.COM rw_exit(&pbuf->b_lock); 25969396SMatthew.Ahrens@Sun.COM 25977046Sahrens return (err); 25987046Sahrens } 25997046Sahrens 26007046Sahrens int 260110922SJeff.Bonwick@Sun.COM arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp, 26027237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 26037046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2604789Sahrens { 2605789Sahrens arc_buf_hdr_t *hdr; 2606789Sahrens arc_buf_t *buf; 2607789Sahrens kmutex_t *hash_lock; 26085450Sbrendan zio_t *rzio; 26098636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 2610789Sahrens 2611789Sahrens top: 261210922SJeff.Bonwick@Sun.COM hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 261310922SJeff.Bonwick@Sun.COM &hash_lock); 26141544Seschrock if (hdr && hdr->b_datacnt > 0) { 2615789Sahrens 26162391Smaybee *arc_flags |= ARC_CACHED; 26172391Smaybee 2618789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 26192391Smaybee 26202391Smaybee if (*arc_flags & ARC_WAIT) { 26212391Smaybee cv_wait(&hdr->b_cv, hash_lock); 26222391Smaybee mutex_exit(hash_lock); 26232391Smaybee goto top; 26242391Smaybee } 26252391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 26262391Smaybee 26272391Smaybee if (done) { 2628789Sahrens arc_callback_t *acb = NULL; 2629789Sahrens 2630789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2631789Sahrens KM_SLEEP); 2632789Sahrens acb->acb_done = done; 2633789Sahrens acb->acb_private = private; 2634789Sahrens if (pio != NULL) 2635789Sahrens acb->acb_zio_dummy = zio_null(pio, 26368632SBill.Moore@Sun.COM spa, NULL, NULL, NULL, zio_flags); 2637789Sahrens 2638789Sahrens ASSERT(acb->acb_done != NULL); 2639789Sahrens acb->acb_next = hdr->b_acb; 2640789Sahrens hdr->b_acb = acb; 2641789Sahrens add_reference(hdr, hash_lock, private); 2642789Sahrens mutex_exit(hash_lock); 2643789Sahrens return (0); 2644789Sahrens } 2645789Sahrens mutex_exit(hash_lock); 2646789Sahrens return (0); 2647789Sahrens } 2648789Sahrens 26493403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2650789Sahrens 26511544Seschrock if (done) { 26522688Smaybee add_reference(hdr, hash_lock, private); 26531544Seschrock /* 26541544Seschrock * If this block is already in use, create a new 26551544Seschrock * copy of the data so that we will be guaranteed 26561544Seschrock * that arc_release() will always succeed. 26571544Seschrock */ 26581544Seschrock buf = hdr->b_buf; 26591544Seschrock ASSERT(buf); 26601544Seschrock ASSERT(buf->b_data); 26612688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 26621544Seschrock ASSERT(buf->b_efunc == NULL); 26631544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 26642688Smaybee } else { 26652688Smaybee buf = arc_buf_clone(buf); 26661544Seschrock } 266710922SJeff.Bonwick@Sun.COM 26682391Smaybee } else if (*arc_flags & ARC_PREFETCH && 26692391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 26702391Smaybee hdr->b_flags |= ARC_PREFETCH; 2671789Sahrens } 2672789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 26732688Smaybee arc_access(hdr, hash_lock); 26747237Sek110237 if (*arc_flags & ARC_L2CACHE) 26757237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26762688Smaybee mutex_exit(hash_lock); 26773403Sbmc ARCSTAT_BUMP(arcstat_hits); 26783403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 26793403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 26803403Sbmc data, metadata, hits); 26813403Sbmc 2682789Sahrens if (done) 2683789Sahrens done(NULL, buf, private); 2684789Sahrens } else { 2685789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2686789Sahrens arc_callback_t *acb; 26876987Sbrendan vdev_t *vd = NULL; 26889215SGeorge.Wilson@Sun.COM uint64_t addr; 26898582SBrendan.Gregg@Sun.COM boolean_t devw = B_FALSE; 2690789Sahrens 2691789Sahrens if (hdr == NULL) { 2692789Sahrens /* this block is not in the cache */ 2693789Sahrens arc_buf_hdr_t *exists; 26943290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 26953290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2696789Sahrens hdr = buf->b_hdr; 2697789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 269810922SJeff.Bonwick@Sun.COM hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 2699789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2700789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2701789Sahrens if (exists) { 2702789Sahrens /* somebody beat us to the hash insert */ 2703789Sahrens mutex_exit(hash_lock); 2704789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2705789Sahrens hdr->b_birth = 0; 2706789Sahrens hdr->b_cksum0 = 0; 27071544Seschrock (void) arc_buf_remove_ref(buf, private); 2708789Sahrens goto top; /* restart the IO request */ 2709789Sahrens } 27102391Smaybee /* if this is a prefetch, we don't have a reference */ 27112391Smaybee if (*arc_flags & ARC_PREFETCH) { 27122391Smaybee (void) remove_reference(hdr, hash_lock, 27132391Smaybee private); 27142391Smaybee hdr->b_flags |= ARC_PREFETCH; 27152391Smaybee } 27167237Sek110237 if (*arc_flags & ARC_L2CACHE) 27177237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27182391Smaybee if (BP_GET_LEVEL(bp) > 0) 27192391Smaybee hdr->b_flags |= ARC_INDIRECT; 2720789Sahrens } else { 2721789Sahrens /* this block is in the ghost cache */ 27221544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 27231544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 27242391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 27252391Smaybee ASSERT(hdr->b_buf == NULL); 2726789Sahrens 27272391Smaybee /* if this is a prefetch, we don't have a reference */ 27282391Smaybee if (*arc_flags & ARC_PREFETCH) 27292391Smaybee hdr->b_flags |= ARC_PREFETCH; 27302391Smaybee else 27312391Smaybee add_reference(hdr, hash_lock, private); 27327237Sek110237 if (*arc_flags & ARC_L2CACHE) 27337237Sek110237 hdr->b_flags |= ARC_L2CACHE; 27346245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 27351544Seschrock buf->b_hdr = hdr; 27362688Smaybee buf->b_data = NULL; 27371544Seschrock buf->b_efunc = NULL; 27381544Seschrock buf->b_private = NULL; 27391544Seschrock buf->b_next = NULL; 27401544Seschrock hdr->b_buf = buf; 27412688Smaybee arc_get_data_buf(buf); 27421544Seschrock ASSERT(hdr->b_datacnt == 0); 27431544Seschrock hdr->b_datacnt = 1; 2744789Sahrens } 2745789Sahrens 2746789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2747789Sahrens acb->acb_done = done; 2748789Sahrens acb->acb_private = private; 2749789Sahrens 2750789Sahrens ASSERT(hdr->b_acb == NULL); 2751789Sahrens hdr->b_acb = acb; 2752789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2753789Sahrens 2754789Sahrens /* 2755789Sahrens * If the buffer has been evicted, migrate it to a present state 2756789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2757789Sahrens * the header will be marked as I/O in progress and have an 2758789Sahrens * attached buffer. At this point, anybody who finds this 2759789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2760789Sahrens */ 2761789Sahrens 27621544Seschrock if (GHOST_STATE(hdr->b_state)) 27632688Smaybee arc_access(hdr, hash_lock); 2764789Sahrens 27657754SJeff.Bonwick@Sun.COM if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 27667754SJeff.Bonwick@Sun.COM (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 27678582SBrendan.Gregg@Sun.COM devw = hdr->b_l2hdr->b_dev->l2ad_writing; 27686987Sbrendan addr = hdr->b_l2hdr->b_daddr; 27697754SJeff.Bonwick@Sun.COM /* 27707754SJeff.Bonwick@Sun.COM * Lock out device removal. 27717754SJeff.Bonwick@Sun.COM */ 27727754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || 27737754SJeff.Bonwick@Sun.COM !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 27747754SJeff.Bonwick@Sun.COM vd = NULL; 27756987Sbrendan } 27766987Sbrendan 27776987Sbrendan mutex_exit(hash_lock); 27786987Sbrendan 2779789Sahrens ASSERT3U(hdr->b_size, ==, size); 278010409SBrendan.Gregg@Sun.COM DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 278110409SBrendan.Gregg@Sun.COM uint64_t, size, zbookmark_t *, zb); 27823403Sbmc ARCSTAT_BUMP(arcstat_misses); 27833403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 27843403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 27853403Sbmc data, metadata, misses); 27861544Seschrock 27878582SBrendan.Gregg@Sun.COM if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 27886987Sbrendan /* 27896987Sbrendan * Read from the L2ARC if the following are true: 27906987Sbrendan * 1. The L2ARC vdev was previously cached. 27916987Sbrendan * 2. This buffer still has L2ARC metadata. 27926987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 27936987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 27946987Sbrendan * also have invalidated the vdev. 27958582SBrendan.Gregg@Sun.COM * 5. This isn't prefetch and l2arc_noprefetch is set. 27966987Sbrendan */ 27977754SJeff.Bonwick@Sun.COM if (hdr->b_l2hdr != NULL && 27988582SBrendan.Gregg@Sun.COM !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 27998582SBrendan.Gregg@Sun.COM !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 28005450Sbrendan l2arc_read_callback_t *cb; 28015450Sbrendan 28026643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 28036643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 28046643Seschrock 28055450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 28065450Sbrendan KM_SLEEP); 28075450Sbrendan cb->l2rcb_buf = buf; 28085450Sbrendan cb->l2rcb_spa = spa; 28095450Sbrendan cb->l2rcb_bp = *bp; 28105450Sbrendan cb->l2rcb_zb = *zb; 28117237Sek110237 cb->l2rcb_flags = zio_flags; 28125450Sbrendan 28135450Sbrendan /* 28147754SJeff.Bonwick@Sun.COM * l2arc read. The SCL_L2ARC lock will be 28157754SJeff.Bonwick@Sun.COM * released by l2arc_read_done(). 28165450Sbrendan */ 28175450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 28185450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 28197237Sek110237 l2arc_read_done, cb, priority, zio_flags | 28207361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 28217754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_PROPAGATE | 28227754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_RETRY, B_FALSE); 28235450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 28245450Sbrendan zio_t *, rzio); 28258582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_read_bytes, size); 28266987Sbrendan 28276987Sbrendan if (*arc_flags & ARC_NOWAIT) { 28286987Sbrendan zio_nowait(rzio); 28296987Sbrendan return (0); 28306987Sbrendan } 28316987Sbrendan 28326987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 28336987Sbrendan if (zio_wait(rzio) == 0) 28346987Sbrendan return (0); 28356987Sbrendan 28366987Sbrendan /* l2arc read error; goto zio_read() */ 28375450Sbrendan } else { 28385450Sbrendan DTRACE_PROBE1(l2arc__miss, 28395450Sbrendan arc_buf_hdr_t *, hdr); 28405450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 28415450Sbrendan if (HDR_L2_WRITING(hdr)) 28425450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 28437754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 28445450Sbrendan } 28458582SBrendan.Gregg@Sun.COM } else { 28468628SBill.Moore@Sun.COM if (vd != NULL) 28478628SBill.Moore@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 28488582SBrendan.Gregg@Sun.COM if (l2arc_ndev != 0) { 28498582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(l2arc__miss, 28508582SBrendan.Gregg@Sun.COM arc_buf_hdr_t *, hdr); 28518582SBrendan.Gregg@Sun.COM ARCSTAT_BUMP(arcstat_l2_misses); 28528582SBrendan.Gregg@Sun.COM } 28535450Sbrendan } 28546643Seschrock 2855789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 28567237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2857789Sahrens 28582391Smaybee if (*arc_flags & ARC_WAIT) 2859789Sahrens return (zio_wait(rzio)); 2860789Sahrens 28612391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2862789Sahrens zio_nowait(rzio); 2863789Sahrens } 2864789Sahrens return (0); 2865789Sahrens } 2866789Sahrens 28671544Seschrock void 28681544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 28691544Seschrock { 28701544Seschrock ASSERT(buf->b_hdr != NULL); 28713403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 28721544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 287310922SJeff.Bonwick@Sun.COM ASSERT(buf->b_efunc == NULL); 287410922SJeff.Bonwick@Sun.COM ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 287510922SJeff.Bonwick@Sun.COM 28761544Seschrock buf->b_efunc = func; 28771544Seschrock buf->b_private = private; 28781544Seschrock } 28791544Seschrock 28801544Seschrock /* 28811544Seschrock * This is used by the DMU to let the ARC know that a buffer is 28821544Seschrock * being evicted, so the ARC should clean up. If this arc buf 28831544Seschrock * is not yet in the evicted state, it will be put there. 28841544Seschrock */ 28851544Seschrock int 28861544Seschrock arc_buf_evict(arc_buf_t *buf) 28871544Seschrock { 28882887Smaybee arc_buf_hdr_t *hdr; 28891544Seschrock kmutex_t *hash_lock; 28901544Seschrock arc_buf_t **bufp; 28911544Seschrock 28927545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 28932887Smaybee hdr = buf->b_hdr; 28941544Seschrock if (hdr == NULL) { 28951544Seschrock /* 28961544Seschrock * We are in arc_do_user_evicts(). 28971544Seschrock */ 28981544Seschrock ASSERT(buf->b_data == NULL); 28997545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29001544Seschrock return (0); 29017545SMark.Maybee@Sun.COM } else if (buf->b_data == NULL) { 29027545SMark.Maybee@Sun.COM arc_buf_t copy = *buf; /* structure assignment */ 29037545SMark.Maybee@Sun.COM /* 29047545SMark.Maybee@Sun.COM * We are on the eviction list; process this buffer now 29057545SMark.Maybee@Sun.COM * but let arc_do_user_evicts() do the reaping. 29067545SMark.Maybee@Sun.COM */ 29077545SMark.Maybee@Sun.COM buf->b_efunc = NULL; 29087545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29097545SMark.Maybee@Sun.COM VERIFY(copy.b_efunc(©) == 0); 29107545SMark.Maybee@Sun.COM return (1); 29111544Seschrock } 29122887Smaybee hash_lock = HDR_LOCK(hdr); 29131544Seschrock mutex_enter(hash_lock); 29141544Seschrock 29152724Smaybee ASSERT(buf->b_hdr == hdr); 29162724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 29173403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 29181544Seschrock 29191544Seschrock /* 29201544Seschrock * Pull this buffer off of the hdr 29211544Seschrock */ 29221544Seschrock bufp = &hdr->b_buf; 29231544Seschrock while (*bufp != buf) 29241544Seschrock bufp = &(*bufp)->b_next; 29251544Seschrock *bufp = buf->b_next; 29261544Seschrock 29271544Seschrock ASSERT(buf->b_data != NULL); 29282688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 29291544Seschrock 29301544Seschrock if (hdr->b_datacnt == 0) { 29311544Seschrock arc_state_t *old_state = hdr->b_state; 29321544Seschrock arc_state_t *evicted_state; 29331544Seschrock 29341544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 29351544Seschrock 29361544Seschrock evicted_state = 29373403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 29381544Seschrock 29393403Sbmc mutex_enter(&old_state->arcs_mtx); 29403403Sbmc mutex_enter(&evicted_state->arcs_mtx); 29411544Seschrock 29421544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 29431544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 29445450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 29455450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 29461544Seschrock 29473403Sbmc mutex_exit(&evicted_state->arcs_mtx); 29483403Sbmc mutex_exit(&old_state->arcs_mtx); 29491544Seschrock } 29501544Seschrock mutex_exit(hash_lock); 29517545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29521819Smaybee 29531544Seschrock VERIFY(buf->b_efunc(buf) == 0); 29541544Seschrock buf->b_efunc = NULL; 29551544Seschrock buf->b_private = NULL; 29561544Seschrock buf->b_hdr = NULL; 29571544Seschrock kmem_cache_free(buf_cache, buf); 29581544Seschrock return (1); 29591544Seschrock } 29601544Seschrock 2961789Sahrens /* 2962789Sahrens * Release this buffer from the cache. This must be done 2963789Sahrens * after a read and prior to modifying the buffer contents. 2964789Sahrens * If the buffer has more than one reference, we must make 29657046Sahrens * a new hdr for the buffer. 2966789Sahrens */ 2967789Sahrens void 2968789Sahrens arc_release(arc_buf_t *buf, void *tag) 2969789Sahrens { 29707545SMark.Maybee@Sun.COM arc_buf_hdr_t *hdr; 29717545SMark.Maybee@Sun.COM kmutex_t *hash_lock; 29727545SMark.Maybee@Sun.COM l2arc_buf_hdr_t *l2hdr; 29735450Sbrendan uint64_t buf_size; 29749274SBrendan.Gregg@Sun.COM boolean_t released = B_FALSE; 2975789Sahrens 29767545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 29777545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 29787545SMark.Maybee@Sun.COM 2979789Sahrens /* this buffer is not on any list */ 2980789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 2981789Sahrens 29823403Sbmc if (hdr->b_state == arc_anon) { 2983789Sahrens /* this buffer is already released */ 2984789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2985789Sahrens ASSERT(BUF_EMPTY(hdr)); 29861544Seschrock ASSERT(buf->b_efunc == NULL); 29873093Sahrens arc_buf_thaw(buf); 29887545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29899274SBrendan.Gregg@Sun.COM released = B_TRUE; 29909274SBrendan.Gregg@Sun.COM } else { 29919274SBrendan.Gregg@Sun.COM hash_lock = HDR_LOCK(hdr); 29929274SBrendan.Gregg@Sun.COM mutex_enter(hash_lock); 2993789Sahrens } 2994789Sahrens 29957545SMark.Maybee@Sun.COM l2hdr = hdr->b_l2hdr; 29967545SMark.Maybee@Sun.COM if (l2hdr) { 29977545SMark.Maybee@Sun.COM mutex_enter(&l2arc_buflist_mtx); 29987545SMark.Maybee@Sun.COM hdr->b_l2hdr = NULL; 29997545SMark.Maybee@Sun.COM buf_size = hdr->b_size; 30007545SMark.Maybee@Sun.COM } 30017545SMark.Maybee@Sun.COM 30029274SBrendan.Gregg@Sun.COM if (released) 30039274SBrendan.Gregg@Sun.COM goto out; 30049274SBrendan.Gregg@Sun.COM 30051544Seschrock /* 30061544Seschrock * Do we have more than one buf? 30071544Seschrock */ 30087545SMark.Maybee@Sun.COM if (hdr->b_datacnt > 1) { 3009789Sahrens arc_buf_hdr_t *nhdr; 3010789Sahrens arc_buf_t **bufp; 3011789Sahrens uint64_t blksz = hdr->b_size; 30128636SMark.Maybee@Sun.COM uint64_t spa = hdr->b_spa; 30133290Sjohansen arc_buf_contents_t type = hdr->b_type; 30145450Sbrendan uint32_t flags = hdr->b_flags; 3015789Sahrens 30167545SMark.Maybee@Sun.COM ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3017789Sahrens /* 3018789Sahrens * Pull the data off of this buf and attach it to 3019789Sahrens * a new anonymous buf. 3020789Sahrens */ 30211544Seschrock (void) remove_reference(hdr, hash_lock, tag); 3022789Sahrens bufp = &hdr->b_buf; 30231544Seschrock while (*bufp != buf) 3024789Sahrens bufp = &(*bufp)->b_next; 3025789Sahrens *bufp = (*bufp)->b_next; 30263897Smaybee buf->b_next = NULL; 30271544Seschrock 30283403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 30293403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 30301544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 30314309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 30324309Smaybee ASSERT3U(*size, >=, hdr->b_size); 30334309Smaybee atomic_add_64(size, -hdr->b_size); 30341544Seschrock } 30351544Seschrock hdr->b_datacnt -= 1; 30363547Smaybee arc_cksum_verify(buf); 30371544Seschrock 3038789Sahrens mutex_exit(hash_lock); 3039789Sahrens 30406245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3041789Sahrens nhdr->b_size = blksz; 3042789Sahrens nhdr->b_spa = spa; 30433290Sjohansen nhdr->b_type = type; 3044789Sahrens nhdr->b_buf = buf; 30453403Sbmc nhdr->b_state = arc_anon; 3046789Sahrens nhdr->b_arc_access = 0; 30475450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 30485450Sbrendan nhdr->b_l2hdr = NULL; 30491544Seschrock nhdr->b_datacnt = 1; 30503547Smaybee nhdr->b_freeze_cksum = NULL; 30513897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 3052789Sahrens buf->b_hdr = nhdr; 30537545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30543403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 3055789Sahrens } else { 30567545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30571544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3058789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 3059789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 30603403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 3061789Sahrens hdr->b_arc_access = 0; 3062789Sahrens mutex_exit(hash_lock); 30635450Sbrendan 3064789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 3065789Sahrens hdr->b_birth = 0; 3066789Sahrens hdr->b_cksum0 = 0; 30673547Smaybee arc_buf_thaw(buf); 3068789Sahrens } 30691544Seschrock buf->b_efunc = NULL; 30701544Seschrock buf->b_private = NULL; 30715450Sbrendan 30729274SBrendan.Gregg@Sun.COM out: 30735450Sbrendan if (l2hdr) { 30745450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 30755450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 30765450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 30777545SMark.Maybee@Sun.COM mutex_exit(&l2arc_buflist_mtx); 30785450Sbrendan } 3079789Sahrens } 3080789Sahrens 3081789Sahrens int 3082789Sahrens arc_released(arc_buf_t *buf) 3083789Sahrens { 30847545SMark.Maybee@Sun.COM int released; 30857545SMark.Maybee@Sun.COM 30867545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30877545SMark.Maybee@Sun.COM released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 30887545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30897545SMark.Maybee@Sun.COM return (released); 30901544Seschrock } 30911544Seschrock 30921544Seschrock int 30931544Seschrock arc_has_callback(arc_buf_t *buf) 30941544Seschrock { 30957545SMark.Maybee@Sun.COM int callback; 30967545SMark.Maybee@Sun.COM 30977545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30987545SMark.Maybee@Sun.COM callback = (buf->b_efunc != NULL); 30997545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 31007545SMark.Maybee@Sun.COM return (callback); 3101789Sahrens } 3102789Sahrens 31031544Seschrock #ifdef ZFS_DEBUG 31041544Seschrock int 31051544Seschrock arc_referenced(arc_buf_t *buf) 31061544Seschrock { 31077545SMark.Maybee@Sun.COM int referenced; 31087545SMark.Maybee@Sun.COM 31097545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 31107545SMark.Maybee@Sun.COM referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 31117545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 31127545SMark.Maybee@Sun.COM return (referenced); 31131544Seschrock } 31141544Seschrock #endif 31151544Seschrock 3116789Sahrens static void 31173547Smaybee arc_write_ready(zio_t *zio) 31183547Smaybee { 31193547Smaybee arc_write_callback_t *callback = zio->io_private; 31203547Smaybee arc_buf_t *buf = callback->awcb_buf; 31215329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 31225329Sgw25295 31237754SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 31247754SJeff.Bonwick@Sun.COM callback->awcb_ready(zio, buf, callback->awcb_private); 31257754SJeff.Bonwick@Sun.COM 31265329Sgw25295 /* 31275329Sgw25295 * If the IO is already in progress, then this is a re-write 31287754SJeff.Bonwick@Sun.COM * attempt, so we need to thaw and re-compute the cksum. 31297754SJeff.Bonwick@Sun.COM * It is the responsibility of the callback to handle the 31307754SJeff.Bonwick@Sun.COM * accounting for any re-write attempt. 31315329Sgw25295 */ 31325329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 31335329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 31345329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 31355329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 31365329Sgw25295 hdr->b_freeze_cksum = NULL; 31375329Sgw25295 } 31385329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 31395329Sgw25295 } 31405450Sbrendan arc_cksum_compute(buf, B_FALSE); 31415329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 31423547Smaybee } 31433547Smaybee 31443547Smaybee static void 3145789Sahrens arc_write_done(zio_t *zio) 3146789Sahrens { 31473547Smaybee arc_write_callback_t *callback = zio->io_private; 31483547Smaybee arc_buf_t *buf = callback->awcb_buf; 31493547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3150789Sahrens 315110922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_acb == NULL); 315210922SJeff.Bonwick@Sun.COM 315310922SJeff.Bonwick@Sun.COM if (zio->io_error == 0) { 315410922SJeff.Bonwick@Sun.COM hdr->b_dva = *BP_IDENTITY(zio->io_bp); 315510922SJeff.Bonwick@Sun.COM hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 315610922SJeff.Bonwick@Sun.COM hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 315710922SJeff.Bonwick@Sun.COM } else { 315810922SJeff.Bonwick@Sun.COM ASSERT(BUF_EMPTY(hdr)); 315910922SJeff.Bonwick@Sun.COM } 316010922SJeff.Bonwick@Sun.COM 31611544Seschrock /* 31621544Seschrock * If the block to be written was all-zero, we may have 31631544Seschrock * compressed it away. In this case no write was performed 31641544Seschrock * so there will be no dva/birth-date/checksum. The buffer 31651544Seschrock * must therefor remain anonymous (and uncached). 31661544Seschrock */ 3167789Sahrens if (!BUF_EMPTY(hdr)) { 3168789Sahrens arc_buf_hdr_t *exists; 3169789Sahrens kmutex_t *hash_lock; 3170789Sahrens 317110922SJeff.Bonwick@Sun.COM ASSERT(zio->io_error == 0); 317210922SJeff.Bonwick@Sun.COM 31733093Sahrens arc_cksum_verify(buf); 31743093Sahrens 3175789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3176789Sahrens if (exists) { 3177789Sahrens /* 3178789Sahrens * This can only happen if we overwrite for 3179789Sahrens * sync-to-convergence, because we remove 3180789Sahrens * buffers from the hash table when we arc_free(). 3181789Sahrens */ 318210922SJeff.Bonwick@Sun.COM if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 318310922SJeff.Bonwick@Sun.COM if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 318410922SJeff.Bonwick@Sun.COM panic("bad overwrite, hdr=%p exists=%p", 318510922SJeff.Bonwick@Sun.COM (void *)hdr, (void *)exists); 318610922SJeff.Bonwick@Sun.COM ASSERT(refcount_is_zero(&exists->b_refcnt)); 318710922SJeff.Bonwick@Sun.COM arc_change_state(arc_anon, exists, hash_lock); 318810922SJeff.Bonwick@Sun.COM mutex_exit(hash_lock); 318910922SJeff.Bonwick@Sun.COM arc_hdr_destroy(exists); 319010922SJeff.Bonwick@Sun.COM exists = buf_hash_insert(hdr, &hash_lock); 319110922SJeff.Bonwick@Sun.COM ASSERT3P(exists, ==, NULL); 319210922SJeff.Bonwick@Sun.COM } else { 319310922SJeff.Bonwick@Sun.COM /* Dedup */ 319410922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_datacnt == 1); 319510922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_state == arc_anon); 319610922SJeff.Bonwick@Sun.COM ASSERT(BP_GET_DEDUP(zio->io_bp)); 319710922SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 319810272SMatthew.Ahrens@Sun.COM } 3199789Sahrens } 32001544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 32017046Sahrens /* if it's not anon, we are doing a scrub */ 320210922SJeff.Bonwick@Sun.COM if (!exists && hdr->b_state == arc_anon) 32037046Sahrens arc_access(hdr, hash_lock); 32042688Smaybee mutex_exit(hash_lock); 32051544Seschrock } else { 32061544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3207789Sahrens } 320810922SJeff.Bonwick@Sun.COM 320910922SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 321010922SJeff.Bonwick@Sun.COM callback->awcb_done(zio, buf, callback->awcb_private); 3211789Sahrens 32123547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3213789Sahrens } 3214789Sahrens 32153547Smaybee zio_t * 321610922SJeff.Bonwick@Sun.COM arc_write(zio_t *pio, spa_t *spa, uint64_t txg, 321710922SJeff.Bonwick@Sun.COM blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp, 321810922SJeff.Bonwick@Sun.COM arc_done_func_t *ready, arc_done_func_t *done, void *private, 321910922SJeff.Bonwick@Sun.COM int priority, int zio_flags, const zbookmark_t *zb) 3220789Sahrens { 3221789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 32223547Smaybee arc_write_callback_t *callback; 32237754SJeff.Bonwick@Sun.COM zio_t *zio; 32247754SJeff.Bonwick@Sun.COM 32257754SJeff.Bonwick@Sun.COM ASSERT(ready != NULL); 322610922SJeff.Bonwick@Sun.COM ASSERT(done != NULL); 3227789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 32282237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 322910922SJeff.Bonwick@Sun.COM ASSERT(hdr->b_acb == NULL); 32307237Sek110237 if (l2arc) 32317237Sek110237 hdr->b_flags |= ARC_L2CACHE; 32323547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 32333547Smaybee callback->awcb_ready = ready; 32343547Smaybee callback->awcb_done = done; 32353547Smaybee callback->awcb_private = private; 32363547Smaybee callback->awcb_buf = buf; 32377046Sahrens 323810922SJeff.Bonwick@Sun.COM zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 32397754SJeff.Bonwick@Sun.COM arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3240789Sahrens 32413547Smaybee return (zio); 3242789Sahrens } 3243789Sahrens 324410922SJeff.Bonwick@Sun.COM void 324510922SJeff.Bonwick@Sun.COM arc_free(spa_t *spa, const blkptr_t *bp) 3246789Sahrens { 3247789Sahrens arc_buf_hdr_t *ab; 3248789Sahrens kmutex_t *hash_lock; 32498636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 3250789Sahrens 3251789Sahrens /* 325210922SJeff.Bonwick@Sun.COM * If this buffer is in the cache, release it, so it can be re-used. 3253789Sahrens */ 325410922SJeff.Bonwick@Sun.COM ab = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 325510922SJeff.Bonwick@Sun.COM &hash_lock); 3256789Sahrens if (ab != NULL) { 32573403Sbmc if (ab->b_state != arc_anon) 32583403Sbmc arc_change_state(arc_anon, ab, hash_lock); 32592391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 32602391Smaybee /* 32612391Smaybee * This should only happen when we prefetch. 32622391Smaybee */ 32632391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 32642391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 32652391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 32662391Smaybee if (HDR_IN_HASH_TABLE(ab)) 32672391Smaybee buf_hash_remove(ab); 32682391Smaybee ab->b_arc_access = 0; 32692391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 32702391Smaybee ab->b_birth = 0; 32712391Smaybee ab->b_cksum0 = 0; 32722391Smaybee ab->b_buf->b_efunc = NULL; 32732391Smaybee ab->b_buf->b_private = NULL; 32742391Smaybee mutex_exit(hash_lock); 327510922SJeff.Bonwick@Sun.COM } else { 327610922SJeff.Bonwick@Sun.COM ASSERT(refcount_is_zero(&ab->b_refcnt)); 32775450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3278789Sahrens mutex_exit(hash_lock); 32791544Seschrock arc_hdr_destroy(ab); 32803403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3281789Sahrens } 3282789Sahrens } 3283789Sahrens } 3284789Sahrens 32856245Smaybee static int 32869412SAleksandr.Guzovskiy@Sun.COM arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg) 32876245Smaybee { 32886245Smaybee #ifdef _KERNEL 32896245Smaybee uint64_t available_memory = ptob(freemem); 32906245Smaybee static uint64_t page_load = 0; 32916245Smaybee static uint64_t last_txg = 0; 32926245Smaybee 32936245Smaybee #if defined(__i386) 32946245Smaybee available_memory = 32956245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 32966245Smaybee #endif 32976245Smaybee if (available_memory >= zfs_write_limit_max) 32986245Smaybee return (0); 32996245Smaybee 33006245Smaybee if (txg > last_txg) { 33016245Smaybee last_txg = txg; 33026245Smaybee page_load = 0; 33036245Smaybee } 33046245Smaybee /* 33056245Smaybee * If we are in pageout, we know that memory is already tight, 33066245Smaybee * the arc is already going to be evicting, so we just want to 33076245Smaybee * continue to let page writes occur as quickly as possible. 33086245Smaybee */ 33096245Smaybee if (curproc == proc_pageout) { 33106245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 33116245Smaybee return (ERESTART); 33126245Smaybee /* Note: reserve is inflated, so we deflate */ 33136245Smaybee page_load += reserve / 8; 33146245Smaybee return (0); 33156245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 33166245Smaybee /* memory is low, delay before restarting */ 33176245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33186245Smaybee return (EAGAIN); 33196245Smaybee } 33206245Smaybee page_load = 0; 33216245Smaybee 33226245Smaybee if (arc_size > arc_c_min) { 33236245Smaybee uint64_t evictable_memory = 33246245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 33256245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 33266245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 33276245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 33286245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 33296245Smaybee } 33306245Smaybee 33316245Smaybee if (inflight_data > available_memory / 4) { 33326245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33336245Smaybee return (ERESTART); 33346245Smaybee } 33356245Smaybee #endif 33366245Smaybee return (0); 33376245Smaybee } 33386245Smaybee 3339789Sahrens void 33406245Smaybee arc_tempreserve_clear(uint64_t reserve) 3341789Sahrens { 33426245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3343789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3344789Sahrens } 3345789Sahrens 3346789Sahrens int 33476245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3348789Sahrens { 33496245Smaybee int error; 33509412SAleksandr.Guzovskiy@Sun.COM uint64_t anon_size; 33516245Smaybee 3352789Sahrens #ifdef ZFS_DEBUG 3353789Sahrens /* 3354789Sahrens * Once in a while, fail for no reason. Everything should cope. 3355789Sahrens */ 3356789Sahrens if (spa_get_random(10000) == 0) { 3357789Sahrens dprintf("forcing random failure\n"); 3358789Sahrens return (ERESTART); 3359789Sahrens } 3360789Sahrens #endif 33616245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 33626245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 33636245Smaybee if (reserve > arc_c) 3364982Smaybee return (ENOMEM); 3365982Smaybee 3366789Sahrens /* 33679412SAleksandr.Guzovskiy@Sun.COM * Don't count loaned bufs as in flight dirty data to prevent long 33689412SAleksandr.Guzovskiy@Sun.COM * network delays from blocking transactions that are ready to be 33699412SAleksandr.Guzovskiy@Sun.COM * assigned to a txg. 33709412SAleksandr.Guzovskiy@Sun.COM */ 33719412SAleksandr.Guzovskiy@Sun.COM anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 33729412SAleksandr.Guzovskiy@Sun.COM 33739412SAleksandr.Guzovskiy@Sun.COM /* 33746245Smaybee * Writes will, almost always, require additional memory allocations 33756245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 33766245Smaybee * make sure that there is sufficient available memory for this. 33776245Smaybee */ 33789412SAleksandr.Guzovskiy@Sun.COM if (error = arc_memory_throttle(reserve, anon_size, txg)) 33796245Smaybee return (error); 33806245Smaybee 33816245Smaybee /* 3382982Smaybee * Throttle writes when the amount of dirty data in the cache 3383982Smaybee * gets too large. We try to keep the cache less than half full 3384982Smaybee * of dirty blocks so that our sync times don't grow too large. 3385982Smaybee * Note: if two requests come in concurrently, we might let them 3386982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3387789Sahrens */ 33889412SAleksandr.Guzovskiy@Sun.COM 33899412SAleksandr.Guzovskiy@Sun.COM if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 33909412SAleksandr.Guzovskiy@Sun.COM anon_size > arc_c / 4) { 33914309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 33924309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 33934309Smaybee arc_tempreserve>>10, 33944309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 33954309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 33966245Smaybee reserve>>10, arc_c>>10); 3397789Sahrens return (ERESTART); 3398789Sahrens } 33996245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3400789Sahrens return (0); 3401789Sahrens } 3402789Sahrens 3403789Sahrens void 3404789Sahrens arc_init(void) 3405789Sahrens { 3406789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3407789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3408789Sahrens 34092391Smaybee /* Convert seconds to clock ticks */ 34102638Sperrin arc_min_prefetch_lifespan = 1 * hz; 34112391Smaybee 3412789Sahrens /* Start out with 1/8 of all memory */ 34133403Sbmc arc_c = physmem * PAGESIZE / 8; 3414789Sahrens 3415789Sahrens #ifdef _KERNEL 3416789Sahrens /* 3417789Sahrens * On architectures where the physical memory can be larger 3418789Sahrens * than the addressable space (intel in 32-bit mode), we may 3419789Sahrens * need to limit the cache to 1/8 of VM size. 3420789Sahrens */ 34213403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3422789Sahrens #endif 3423789Sahrens 3424982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 34253403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3426982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 34273403Sbmc if (arc_c * 8 >= 1<<30) 34283403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3429789Sahrens else 34303403Sbmc arc_c_max = arc_c_min; 34313403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 34322885Sahrens 34332885Sahrens /* 34342885Sahrens * Allow the tunables to override our calculations if they are 34352885Sahrens * reasonable (ie. over 64MB) 34362885Sahrens */ 34372885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 34383403Sbmc arc_c_max = zfs_arc_max; 34393403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 34403403Sbmc arc_c_min = zfs_arc_min; 34412885Sahrens 34423403Sbmc arc_c = arc_c_max; 34433403Sbmc arc_p = (arc_c >> 1); 3444789Sahrens 34454309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 34464309Smaybee arc_meta_limit = arc_c_max / 4; 34474645Sek110237 34484645Sek110237 /* Allow the tunable to override if it is reasonable */ 34494645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 34504645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 34514645Sek110237 34524309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 34534309Smaybee arc_c_min = arc_meta_limit / 2; 34544309Smaybee 34558582SBrendan.Gregg@Sun.COM if (zfs_arc_grow_retry > 0) 34568582SBrendan.Gregg@Sun.COM arc_grow_retry = zfs_arc_grow_retry; 34578582SBrendan.Gregg@Sun.COM 34588582SBrendan.Gregg@Sun.COM if (zfs_arc_shrink_shift > 0) 34598582SBrendan.Gregg@Sun.COM arc_shrink_shift = zfs_arc_shrink_shift; 34608582SBrendan.Gregg@Sun.COM 34618582SBrendan.Gregg@Sun.COM if (zfs_arc_p_min_shift > 0) 34628582SBrendan.Gregg@Sun.COM arc_p_min_shift = zfs_arc_p_min_shift; 34638582SBrendan.Gregg@Sun.COM 3464789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3465789Sahrens if (kmem_debugging()) 34663403Sbmc arc_c = arc_c / 2; 34673403Sbmc if (arc_c < arc_c_min) 34683403Sbmc arc_c = arc_c_min; 3469789Sahrens 34703403Sbmc arc_anon = &ARC_anon; 34713403Sbmc arc_mru = &ARC_mru; 34723403Sbmc arc_mru_ghost = &ARC_mru_ghost; 34733403Sbmc arc_mfu = &ARC_mfu; 34743403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 34755450Sbrendan arc_l2c_only = &ARC_l2c_only; 34763403Sbmc arc_size = 0; 3477789Sahrens 34783403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34793403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34803403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34813403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34823403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34835450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34842688Smaybee 34854309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 34864309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34874309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 34884309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34894309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 34904309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34914309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 34924309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34934309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 34944309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34954309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 34964309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34974309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 34984309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34994309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 35004309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35015450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 35025450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35035450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 35045450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3505789Sahrens 3506789Sahrens buf_init(); 3507789Sahrens 3508789Sahrens arc_thread_exit = 0; 35091544Seschrock arc_eviction_list = NULL; 35101544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 35112887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3512789Sahrens 35133403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 35143403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 35153403Sbmc 35163403Sbmc if (arc_ksp != NULL) { 35173403Sbmc arc_ksp->ks_data = &arc_stats; 35183403Sbmc kstat_install(arc_ksp); 35193403Sbmc } 35203403Sbmc 3521789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3522789Sahrens TS_RUN, minclsyspri); 35233158Smaybee 35243158Smaybee arc_dead = FALSE; 35256987Sbrendan arc_warm = B_FALSE; 35266245Smaybee 35276245Smaybee if (zfs_write_limit_max == 0) 35287468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 35296245Smaybee else 35306245Smaybee zfs_write_limit_shift = 0; 35317468SMark.Maybee@Sun.COM mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3532789Sahrens } 3533789Sahrens 3534789Sahrens void 3535789Sahrens arc_fini(void) 3536789Sahrens { 3537789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3538789Sahrens arc_thread_exit = 1; 3539789Sahrens while (arc_thread_exit != 0) 3540789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3541789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3542789Sahrens 35435642Smaybee arc_flush(NULL); 3544789Sahrens 3545789Sahrens arc_dead = TRUE; 3546789Sahrens 35473403Sbmc if (arc_ksp != NULL) { 35483403Sbmc kstat_delete(arc_ksp); 35493403Sbmc arc_ksp = NULL; 35503403Sbmc } 35513403Sbmc 35521544Seschrock mutex_destroy(&arc_eviction_mtx); 3553789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3554789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3555789Sahrens 35564309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 35574309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 35584309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 35594309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 35604309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 35614309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 35624309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 35634309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3564789Sahrens 35653403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 35663403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 35673403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 35683403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 35693403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 35708214SRicardo.M.Correia@Sun.COM mutex_destroy(&arc_l2c_only->arcs_mtx); 35712856Snd150628 35727468SMark.Maybee@Sun.COM mutex_destroy(&zfs_write_limit_lock); 35737468SMark.Maybee@Sun.COM 3574789Sahrens buf_fini(); 35759412SAleksandr.Guzovskiy@Sun.COM 35769412SAleksandr.Guzovskiy@Sun.COM ASSERT(arc_loaned_bytes == 0); 3577789Sahrens } 35785450Sbrendan 35795450Sbrendan /* 35805450Sbrendan * Level 2 ARC 35815450Sbrendan * 35825450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 35835450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 35845450Sbrendan * using large infrequent writes. The main role of this cache is to boost 35855450Sbrendan * the performance of random read workloads. The intended L2ARC devices 35865450Sbrendan * include short-stroked disks, solid state disks, and other media with 35875450Sbrendan * substantially faster read latency than disk. 35885450Sbrendan * 35895450Sbrendan * +-----------------------+ 35905450Sbrendan * | ARC | 35915450Sbrendan * +-----------------------+ 35925450Sbrendan * | ^ ^ 35935450Sbrendan * | | | 35945450Sbrendan * l2arc_feed_thread() arc_read() 35955450Sbrendan * | | | 35965450Sbrendan * | l2arc read | 35975450Sbrendan * V | | 35985450Sbrendan * +---------------+ | 35995450Sbrendan * | L2ARC | | 36005450Sbrendan * +---------------+ | 36015450Sbrendan * | ^ | 36025450Sbrendan * l2arc_write() | | 36035450Sbrendan * | | | 36045450Sbrendan * V | | 36055450Sbrendan * +-------+ +-------+ 36065450Sbrendan * | vdev | | vdev | 36075450Sbrendan * | cache | | cache | 36085450Sbrendan * +-------+ +-------+ 36095450Sbrendan * +=========+ .-----. 36105450Sbrendan * : L2ARC : |-_____-| 36115450Sbrendan * : devices : | Disks | 36125450Sbrendan * +=========+ `-_____-' 36135450Sbrendan * 36145450Sbrendan * Read requests are satisfied from the following sources, in order: 36155450Sbrendan * 36165450Sbrendan * 1) ARC 36175450Sbrendan * 2) vdev cache of L2ARC devices 36185450Sbrendan * 3) L2ARC devices 36195450Sbrendan * 4) vdev cache of disks 36205450Sbrendan * 5) disks 36215450Sbrendan * 36225450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 36235450Sbrendan * To accommodate for this there are some significant differences between 36245450Sbrendan * the L2ARC and traditional cache design: 36255450Sbrendan * 36265450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 36275450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 36285450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 36295450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 36305450Sbrendan * 36315450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 36325450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 36335450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 36345450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 36355450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 36365450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 36375450Sbrendan * provide a better sense of ratio than this diagram: 36385450Sbrendan * 36395450Sbrendan * head --> tail 36405450Sbrendan * +---------------------+----------+ 36415450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 36425450Sbrendan * +---------------------+----------+ | o L2ARC eligible 36435450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 36445450Sbrendan * +---------------------+----------+ | 36455450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 36465450Sbrendan * headroom | 36475450Sbrendan * l2arc_feed_thread() 36485450Sbrendan * | 36495450Sbrendan * l2arc write hand <--[oooo]--' 36505450Sbrendan * | 8 Mbyte 36515450Sbrendan * | write max 36525450Sbrendan * V 36535450Sbrendan * +==============================+ 36545450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 36555450Sbrendan * +==============================+ 36565450Sbrendan * 32 Gbytes 36575450Sbrendan * 36585450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 36595450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 36605450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 36615450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 36625450Sbrendan * the ARC lists have moved there due to inactivity. 36635450Sbrendan * 36645450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 36655450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 36665450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 36675450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 36685450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 36695450Sbrendan * quickly, such as during backups of the entire pool. 36705450Sbrendan * 36716987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 36726987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 36736987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 36746987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 36756987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 36766987Sbrendan * 36776987Sbrendan * The L2ARC device write speed is also boosted during this time so that 36786987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 36796987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 36806987Sbrendan * through increased writes. 36816987Sbrendan * 36826987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 36835450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 36845450Sbrendan * device is written to in a rotor fashion, sweeping writes through 36855450Sbrendan * available space then repeating. 36865450Sbrendan * 36876987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 36885450Sbrendan * write buffers back to disk based storage. 36895450Sbrendan * 36906987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 36915450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 36925450Sbrendan * 36935450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 36945450Sbrendan * may be necessary for different workloads: 36955450Sbrendan * 36965450Sbrendan * l2arc_write_max max write bytes per interval 36976987Sbrendan * l2arc_write_boost extra write bytes during device warmup 36985450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 36995450Sbrendan * l2arc_headroom number of max device writes to precache 37005450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 37015450Sbrendan * 37025450Sbrendan * Tunables may be removed or added as future performance improvements are 37035450Sbrendan * integrated, and also may become zpool properties. 37048582SBrendan.Gregg@Sun.COM * 37058582SBrendan.Gregg@Sun.COM * There are three key functions that control how the L2ARC warms up: 37068582SBrendan.Gregg@Sun.COM * 37078582SBrendan.Gregg@Sun.COM * l2arc_write_eligible() check if a buffer is eligible to cache 37088582SBrendan.Gregg@Sun.COM * l2arc_write_size() calculate how much to write 37098582SBrendan.Gregg@Sun.COM * l2arc_write_interval() calculate sleep delay between writes 37108582SBrendan.Gregg@Sun.COM * 37118582SBrendan.Gregg@Sun.COM * These three functions determine what to write, how much, and how quickly 37128582SBrendan.Gregg@Sun.COM * to send writes. 37135450Sbrendan */ 37145450Sbrendan 37158582SBrendan.Gregg@Sun.COM static boolean_t 37168636SMark.Maybee@Sun.COM l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 37178582SBrendan.Gregg@Sun.COM { 37188582SBrendan.Gregg@Sun.COM /* 37198582SBrendan.Gregg@Sun.COM * A buffer is *not* eligible for the L2ARC if it: 37208582SBrendan.Gregg@Sun.COM * 1. belongs to a different spa. 372110357SBrendan.Gregg@Sun.COM * 2. is already cached on the L2ARC. 372210357SBrendan.Gregg@Sun.COM * 3. has an I/O in progress (it may be an incomplete read). 372310357SBrendan.Gregg@Sun.COM * 4. is flagged not eligible (zfs property). 37248582SBrendan.Gregg@Sun.COM */ 372510357SBrendan.Gregg@Sun.COM if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL || 37268582SBrendan.Gregg@Sun.COM HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) 37278582SBrendan.Gregg@Sun.COM return (B_FALSE); 37288582SBrendan.Gregg@Sun.COM 37298582SBrendan.Gregg@Sun.COM return (B_TRUE); 37308582SBrendan.Gregg@Sun.COM } 37318582SBrendan.Gregg@Sun.COM 37328582SBrendan.Gregg@Sun.COM static uint64_t 37338582SBrendan.Gregg@Sun.COM l2arc_write_size(l2arc_dev_t *dev) 37348582SBrendan.Gregg@Sun.COM { 37358582SBrendan.Gregg@Sun.COM uint64_t size; 37368582SBrendan.Gregg@Sun.COM 37378582SBrendan.Gregg@Sun.COM size = dev->l2ad_write; 37388582SBrendan.Gregg@Sun.COM 37398582SBrendan.Gregg@Sun.COM if (arc_warm == B_FALSE) 37408582SBrendan.Gregg@Sun.COM size += dev->l2ad_boost; 37418582SBrendan.Gregg@Sun.COM 37428582SBrendan.Gregg@Sun.COM return (size); 37438582SBrendan.Gregg@Sun.COM 37448582SBrendan.Gregg@Sun.COM } 37458582SBrendan.Gregg@Sun.COM 37468582SBrendan.Gregg@Sun.COM static clock_t 37478582SBrendan.Gregg@Sun.COM l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 37488582SBrendan.Gregg@Sun.COM { 3749*11066Srafael.vanoni@sun.com clock_t interval, next, now; 37508582SBrendan.Gregg@Sun.COM 37518582SBrendan.Gregg@Sun.COM /* 37528582SBrendan.Gregg@Sun.COM * If the ARC lists are busy, increase our write rate; if the 37538582SBrendan.Gregg@Sun.COM * lists are stale, idle back. This is achieved by checking 37548582SBrendan.Gregg@Sun.COM * how much we previously wrote - if it was more than half of 37558582SBrendan.Gregg@Sun.COM * what we wanted, schedule the next write much sooner. 37568582SBrendan.Gregg@Sun.COM */ 37578582SBrendan.Gregg@Sun.COM if (l2arc_feed_again && wrote > (wanted / 2)) 37588582SBrendan.Gregg@Sun.COM interval = (hz * l2arc_feed_min_ms) / 1000; 37598582SBrendan.Gregg@Sun.COM else 37608582SBrendan.Gregg@Sun.COM interval = hz * l2arc_feed_secs; 37618582SBrendan.Gregg@Sun.COM 3762*11066Srafael.vanoni@sun.com now = ddi_get_lbolt(); 3763*11066Srafael.vanoni@sun.com next = MAX(now, MIN(now + interval, began + interval)); 37648582SBrendan.Gregg@Sun.COM 37658582SBrendan.Gregg@Sun.COM return (next); 37668582SBrendan.Gregg@Sun.COM } 37678582SBrendan.Gregg@Sun.COM 37685450Sbrendan static void 37695450Sbrendan l2arc_hdr_stat_add(void) 37705450Sbrendan { 37716018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 37726018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 37735450Sbrendan } 37745450Sbrendan 37755450Sbrendan static void 37765450Sbrendan l2arc_hdr_stat_remove(void) 37775450Sbrendan { 37786018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 37796018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 37805450Sbrendan } 37815450Sbrendan 37825450Sbrendan /* 37835450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 37846987Sbrendan * If a device is returned, this also returns holding the spa config lock. 37855450Sbrendan */ 37865450Sbrendan static l2arc_dev_t * 37875450Sbrendan l2arc_dev_get_next(void) 37885450Sbrendan { 37896987Sbrendan l2arc_dev_t *first, *next = NULL; 37906987Sbrendan 37916987Sbrendan /* 37926987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 37936987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 37946987Sbrendan * both locks will be dropped and a spa config lock held instead. 37956987Sbrendan */ 37966987Sbrendan mutex_enter(&spa_namespace_lock); 37976987Sbrendan mutex_enter(&l2arc_dev_mtx); 37986643Seschrock 37996643Seschrock /* if there are no vdevs, there is nothing to do */ 38006643Seschrock if (l2arc_ndev == 0) 38016987Sbrendan goto out; 38026643Seschrock 38036643Seschrock first = NULL; 38046643Seschrock next = l2arc_dev_last; 38056643Seschrock do { 38066643Seschrock /* loop around the list looking for a non-faulted vdev */ 38076643Seschrock if (next == NULL) { 38085450Sbrendan next = list_head(l2arc_dev_list); 38096643Seschrock } else { 38106643Seschrock next = list_next(l2arc_dev_list, next); 38116643Seschrock if (next == NULL) 38126643Seschrock next = list_head(l2arc_dev_list); 38136643Seschrock } 38146643Seschrock 38156643Seschrock /* if we have come back to the start, bail out */ 38166643Seschrock if (first == NULL) 38176643Seschrock first = next; 38186643Seschrock else if (next == first) 38196643Seschrock break; 38206643Seschrock 38216643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 38226643Seschrock 38236643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 38246643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 38256987Sbrendan next = NULL; 38265450Sbrendan 38275450Sbrendan l2arc_dev_last = next; 38285450Sbrendan 38296987Sbrendan out: 38306987Sbrendan mutex_exit(&l2arc_dev_mtx); 38316987Sbrendan 38326987Sbrendan /* 38336987Sbrendan * Grab the config lock to prevent the 'next' device from being 38346987Sbrendan * removed while we are writing to it. 38356987Sbrendan */ 38366987Sbrendan if (next != NULL) 38377754SJeff.Bonwick@Sun.COM spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 38386987Sbrendan mutex_exit(&spa_namespace_lock); 38396987Sbrendan 38405450Sbrendan return (next); 38415450Sbrendan } 38425450Sbrendan 38435450Sbrendan /* 38446987Sbrendan * Free buffers that were tagged for destruction. 38456987Sbrendan */ 38466987Sbrendan static void 38476987Sbrendan l2arc_do_free_on_write() 38486987Sbrendan { 38496987Sbrendan list_t *buflist; 38506987Sbrendan l2arc_data_free_t *df, *df_prev; 38516987Sbrendan 38526987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 38536987Sbrendan buflist = l2arc_free_on_write; 38546987Sbrendan 38556987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 38566987Sbrendan df_prev = list_prev(buflist, df); 38576987Sbrendan ASSERT(df->l2df_data != NULL); 38586987Sbrendan ASSERT(df->l2df_func != NULL); 38596987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 38606987Sbrendan list_remove(buflist, df); 38616987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 38626987Sbrendan } 38636987Sbrendan 38646987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 38656987Sbrendan } 38666987Sbrendan 38676987Sbrendan /* 38685450Sbrendan * A write to a cache device has completed. Update all headers to allow 38695450Sbrendan * reads from these buffers to begin. 38705450Sbrendan */ 38715450Sbrendan static void 38725450Sbrendan l2arc_write_done(zio_t *zio) 38735450Sbrendan { 38745450Sbrendan l2arc_write_callback_t *cb; 38755450Sbrendan l2arc_dev_t *dev; 38765450Sbrendan list_t *buflist; 38775450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 38786987Sbrendan l2arc_buf_hdr_t *abl2; 38795450Sbrendan kmutex_t *hash_lock; 38805450Sbrendan 38815450Sbrendan cb = zio->io_private; 38825450Sbrendan ASSERT(cb != NULL); 38835450Sbrendan dev = cb->l2wcb_dev; 38845450Sbrendan ASSERT(dev != NULL); 38855450Sbrendan head = cb->l2wcb_head; 38865450Sbrendan ASSERT(head != NULL); 38875450Sbrendan buflist = dev->l2ad_buflist; 38885450Sbrendan ASSERT(buflist != NULL); 38895450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 38905450Sbrendan l2arc_write_callback_t *, cb); 38915450Sbrendan 38925450Sbrendan if (zio->io_error != 0) 38935450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 38945450Sbrendan 38955450Sbrendan mutex_enter(&l2arc_buflist_mtx); 38965450Sbrendan 38975450Sbrendan /* 38985450Sbrendan * All writes completed, or an error was hit. 38995450Sbrendan */ 39005450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 39015450Sbrendan ab_prev = list_prev(buflist, ab); 39025450Sbrendan 39035450Sbrendan hash_lock = HDR_LOCK(ab); 39045450Sbrendan if (!mutex_tryenter(hash_lock)) { 39055450Sbrendan /* 39065450Sbrendan * This buffer misses out. It may be in a stage 39075450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 39085450Sbrendan * left set, denying reads to this buffer. 39095450Sbrendan */ 39105450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 39115450Sbrendan continue; 39125450Sbrendan } 39135450Sbrendan 39145450Sbrendan if (zio->io_error != 0) { 39155450Sbrendan /* 39166987Sbrendan * Error - drop L2ARC entry. 39175450Sbrendan */ 39186987Sbrendan list_remove(buflist, ab); 39196987Sbrendan abl2 = ab->b_l2hdr; 39205450Sbrendan ab->b_l2hdr = NULL; 39216987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 39226987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 39235450Sbrendan } 39245450Sbrendan 39255450Sbrendan /* 39265450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 39275450Sbrendan */ 39285450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 39295450Sbrendan 39305450Sbrendan mutex_exit(hash_lock); 39315450Sbrendan } 39325450Sbrendan 39335450Sbrendan atomic_inc_64(&l2arc_writes_done); 39345450Sbrendan list_remove(buflist, head); 39355450Sbrendan kmem_cache_free(hdr_cache, head); 39365450Sbrendan mutex_exit(&l2arc_buflist_mtx); 39375450Sbrendan 39386987Sbrendan l2arc_do_free_on_write(); 39395450Sbrendan 39405450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 39415450Sbrendan } 39425450Sbrendan 39435450Sbrendan /* 39445450Sbrendan * A read to a cache device completed. Validate buffer contents before 39455450Sbrendan * handing over to the regular ARC routines. 39465450Sbrendan */ 39475450Sbrendan static void 39485450Sbrendan l2arc_read_done(zio_t *zio) 39495450Sbrendan { 39505450Sbrendan l2arc_read_callback_t *cb; 39515450Sbrendan arc_buf_hdr_t *hdr; 39525450Sbrendan arc_buf_t *buf; 39535450Sbrendan kmutex_t *hash_lock; 39546987Sbrendan int equal; 39555450Sbrendan 39567754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd != NULL); 39577754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 39587754SJeff.Bonwick@Sun.COM 39597754SJeff.Bonwick@Sun.COM spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 39607754SJeff.Bonwick@Sun.COM 39615450Sbrendan cb = zio->io_private; 39625450Sbrendan ASSERT(cb != NULL); 39635450Sbrendan buf = cb->l2rcb_buf; 39645450Sbrendan ASSERT(buf != NULL); 39655450Sbrendan hdr = buf->b_hdr; 39665450Sbrendan ASSERT(hdr != NULL); 39675450Sbrendan 39685450Sbrendan hash_lock = HDR_LOCK(hdr); 39695450Sbrendan mutex_enter(hash_lock); 39705450Sbrendan 39715450Sbrendan /* 39725450Sbrendan * Check this survived the L2ARC journey. 39735450Sbrendan */ 39745450Sbrendan equal = arc_cksum_equal(buf); 39755450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 39765450Sbrendan mutex_exit(hash_lock); 39775450Sbrendan zio->io_private = buf; 39787754SJeff.Bonwick@Sun.COM zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 39797754SJeff.Bonwick@Sun.COM zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 39805450Sbrendan arc_read_done(zio); 39815450Sbrendan } else { 39825450Sbrendan mutex_exit(hash_lock); 39835450Sbrendan /* 39845450Sbrendan * Buffer didn't survive caching. Increment stats and 39855450Sbrendan * reissue to the original storage device. 39865450Sbrendan */ 39876987Sbrendan if (zio->io_error != 0) { 39885450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 39896987Sbrendan } else { 39906987Sbrendan zio->io_error = EIO; 39916987Sbrendan } 39925450Sbrendan if (!equal) 39935450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 39945450Sbrendan 39957754SJeff.Bonwick@Sun.COM /* 39967754SJeff.Bonwick@Sun.COM * If there's no waiter, issue an async i/o to the primary 39977754SJeff.Bonwick@Sun.COM * storage now. If there *is* a waiter, the caller must 39987754SJeff.Bonwick@Sun.COM * issue the i/o in a context where it's OK to block. 39997754SJeff.Bonwick@Sun.COM */ 40008632SBill.Moore@Sun.COM if (zio->io_waiter == NULL) { 40018632SBill.Moore@Sun.COM zio_t *pio = zio_unique_parent(zio); 40028632SBill.Moore@Sun.COM 40038632SBill.Moore@Sun.COM ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 40048632SBill.Moore@Sun.COM 40058632SBill.Moore@Sun.COM zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 40067754SJeff.Bonwick@Sun.COM buf->b_data, zio->io_size, arc_read_done, buf, 40077754SJeff.Bonwick@Sun.COM zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 40088632SBill.Moore@Sun.COM } 40095450Sbrendan } 40105450Sbrendan 40115450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 40125450Sbrendan } 40135450Sbrendan 40145450Sbrendan /* 40155450Sbrendan * This is the list priority from which the L2ARC will search for pages to 40165450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 40175450Sbrendan * desired order. This order can have a significant effect on cache 40185450Sbrendan * performance. 40195450Sbrendan * 40205450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 40215450Sbrendan * the data lists. This function returns a locked list, and also returns 40225450Sbrendan * the lock pointer. 40235450Sbrendan */ 40245450Sbrendan static list_t * 40255450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 40265450Sbrendan { 40275450Sbrendan list_t *list; 40285450Sbrendan 40295450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 40305450Sbrendan 40315450Sbrendan switch (list_num) { 40325450Sbrendan case 0: 40335450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 40345450Sbrendan *lock = &arc_mfu->arcs_mtx; 40355450Sbrendan break; 40365450Sbrendan case 1: 40375450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 40385450Sbrendan *lock = &arc_mru->arcs_mtx; 40395450Sbrendan break; 40405450Sbrendan case 2: 40415450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 40425450Sbrendan *lock = &arc_mfu->arcs_mtx; 40435450Sbrendan break; 40445450Sbrendan case 3: 40455450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 40465450Sbrendan *lock = &arc_mru->arcs_mtx; 40475450Sbrendan break; 40485450Sbrendan } 40495450Sbrendan 40505450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 40515450Sbrendan mutex_enter(*lock); 40525450Sbrendan return (list); 40535450Sbrendan } 40545450Sbrendan 40555450Sbrendan /* 40565450Sbrendan * Evict buffers from the device write hand to the distance specified in 40575450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 40585450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 40595450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 40605450Sbrendan */ 40615450Sbrendan static void 40625450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 40635450Sbrendan { 40645450Sbrendan list_t *buflist; 40655450Sbrendan l2arc_buf_hdr_t *abl2; 40665450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 40675450Sbrendan kmutex_t *hash_lock; 40685450Sbrendan uint64_t taddr; 40695450Sbrendan 40705450Sbrendan buflist = dev->l2ad_buflist; 40715450Sbrendan 40725450Sbrendan if (buflist == NULL) 40735450Sbrendan return; 40745450Sbrendan 40755450Sbrendan if (!all && dev->l2ad_first) { 40765450Sbrendan /* 40775450Sbrendan * This is the first sweep through the device. There is 40785450Sbrendan * nothing to evict. 40795450Sbrendan */ 40805450Sbrendan return; 40815450Sbrendan } 40825450Sbrendan 40836987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 40845450Sbrendan /* 40855450Sbrendan * When nearing the end of the device, evict to the end 40865450Sbrendan * before the device write hand jumps to the start. 40875450Sbrendan */ 40885450Sbrendan taddr = dev->l2ad_end; 40895450Sbrendan } else { 40905450Sbrendan taddr = dev->l2ad_hand + distance; 40915450Sbrendan } 40925450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 40935450Sbrendan uint64_t, taddr, boolean_t, all); 40945450Sbrendan 40955450Sbrendan top: 40965450Sbrendan mutex_enter(&l2arc_buflist_mtx); 40975450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 40985450Sbrendan ab_prev = list_prev(buflist, ab); 40995450Sbrendan 41005450Sbrendan hash_lock = HDR_LOCK(ab); 41015450Sbrendan if (!mutex_tryenter(hash_lock)) { 41025450Sbrendan /* 41035450Sbrendan * Missed the hash lock. Retry. 41045450Sbrendan */ 41055450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 41065450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41075450Sbrendan mutex_enter(hash_lock); 41085450Sbrendan mutex_exit(hash_lock); 41095450Sbrendan goto top; 41105450Sbrendan } 41115450Sbrendan 41125450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 41135450Sbrendan /* 41145450Sbrendan * We hit a write head node. Leave it for 41155450Sbrendan * l2arc_write_done(). 41165450Sbrendan */ 41175450Sbrendan list_remove(buflist, ab); 41185450Sbrendan mutex_exit(hash_lock); 41195450Sbrendan continue; 41205450Sbrendan } 41215450Sbrendan 41225450Sbrendan if (!all && ab->b_l2hdr != NULL && 41235450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 41245450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 41255450Sbrendan /* 41265450Sbrendan * We've evicted to the target address, 41275450Sbrendan * or the end of the device. 41285450Sbrendan */ 41295450Sbrendan mutex_exit(hash_lock); 41305450Sbrendan break; 41315450Sbrendan } 41325450Sbrendan 41335450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 41345450Sbrendan /* 41355450Sbrendan * Already on the path to destruction. 41365450Sbrendan */ 41375450Sbrendan mutex_exit(hash_lock); 41385450Sbrendan continue; 41395450Sbrendan } 41405450Sbrendan 41415450Sbrendan if (ab->b_state == arc_l2c_only) { 41425450Sbrendan ASSERT(!HDR_L2_READING(ab)); 41435450Sbrendan /* 41445450Sbrendan * This doesn't exist in the ARC. Destroy. 41455450Sbrendan * arc_hdr_destroy() will call list_remove() 41465450Sbrendan * and decrement arcstat_l2_size. 41475450Sbrendan */ 41485450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 41495450Sbrendan arc_hdr_destroy(ab); 41505450Sbrendan } else { 41515450Sbrendan /* 41526987Sbrendan * Invalidate issued or about to be issued 41536987Sbrendan * reads, since we may be about to write 41546987Sbrendan * over this location. 41556987Sbrendan */ 41566987Sbrendan if (HDR_L2_READING(ab)) { 41576987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 41586987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 41596987Sbrendan } 41606987Sbrendan 41616987Sbrendan /* 41625450Sbrendan * Tell ARC this no longer exists in L2ARC. 41635450Sbrendan */ 41645450Sbrendan if (ab->b_l2hdr != NULL) { 41655450Sbrendan abl2 = ab->b_l2hdr; 41665450Sbrendan ab->b_l2hdr = NULL; 41675450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 41685450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 41695450Sbrendan } 41705450Sbrendan list_remove(buflist, ab); 41715450Sbrendan 41725450Sbrendan /* 41735450Sbrendan * This may have been leftover after a 41745450Sbrendan * failed write. 41755450Sbrendan */ 41765450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 41775450Sbrendan } 41785450Sbrendan mutex_exit(hash_lock); 41795450Sbrendan } 41805450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41815450Sbrendan 418210922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0); 41835450Sbrendan dev->l2ad_evict = taddr; 41845450Sbrendan } 41855450Sbrendan 41865450Sbrendan /* 41875450Sbrendan * Find and write ARC buffers to the L2ARC device. 41885450Sbrendan * 41895450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 41905450Sbrendan * for reading until they have completed writing. 41915450Sbrendan */ 41928582SBrendan.Gregg@Sun.COM static uint64_t 41936987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 41945450Sbrendan { 41955450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 41965450Sbrendan l2arc_buf_hdr_t *hdrl2; 41975450Sbrendan list_t *list; 41986987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 41995450Sbrendan void *buf_data; 42005450Sbrendan kmutex_t *hash_lock, *list_lock; 42015450Sbrendan boolean_t have_lock, full; 42025450Sbrendan l2arc_write_callback_t *cb; 42035450Sbrendan zio_t *pio, *wzio; 42048636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 42055450Sbrendan 42065450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 42075450Sbrendan 42085450Sbrendan pio = NULL; 42095450Sbrendan write_sz = 0; 42105450Sbrendan full = B_FALSE; 42116245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 42125450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 42135450Sbrendan 42145450Sbrendan /* 42155450Sbrendan * Copy buffers for L2ARC writing. 42165450Sbrendan */ 42175450Sbrendan mutex_enter(&l2arc_buflist_mtx); 42185450Sbrendan for (int try = 0; try <= 3; try++) { 42195450Sbrendan list = l2arc_list_locked(try, &list_lock); 42205450Sbrendan passed_sz = 0; 42215450Sbrendan 42226987Sbrendan /* 42236987Sbrendan * L2ARC fast warmup. 42246987Sbrendan * 42256987Sbrendan * Until the ARC is warm and starts to evict, read from the 42266987Sbrendan * head of the ARC lists rather than the tail. 42276987Sbrendan */ 42286987Sbrendan headroom = target_sz * l2arc_headroom; 42296987Sbrendan if (arc_warm == B_FALSE) 42306987Sbrendan ab = list_head(list); 42316987Sbrendan else 42326987Sbrendan ab = list_tail(list); 42336987Sbrendan 42346987Sbrendan for (; ab; ab = ab_prev) { 42356987Sbrendan if (arc_warm == B_FALSE) 42366987Sbrendan ab_prev = list_next(list, ab); 42376987Sbrendan else 42386987Sbrendan ab_prev = list_prev(list, ab); 42395450Sbrendan 42405450Sbrendan hash_lock = HDR_LOCK(ab); 42415450Sbrendan have_lock = MUTEX_HELD(hash_lock); 42425450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 42435450Sbrendan /* 42445450Sbrendan * Skip this buffer rather than waiting. 42455450Sbrendan */ 42465450Sbrendan continue; 42475450Sbrendan } 42485450Sbrendan 42495450Sbrendan passed_sz += ab->b_size; 42505450Sbrendan if (passed_sz > headroom) { 42515450Sbrendan /* 42525450Sbrendan * Searched too far. 42535450Sbrendan */ 42545450Sbrendan mutex_exit(hash_lock); 42555450Sbrendan break; 42565450Sbrendan } 42575450Sbrendan 42588636SMark.Maybee@Sun.COM if (!l2arc_write_eligible(guid, ab)) { 42595450Sbrendan mutex_exit(hash_lock); 42605450Sbrendan continue; 42615450Sbrendan } 42625450Sbrendan 42635450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 42645450Sbrendan full = B_TRUE; 42655450Sbrendan mutex_exit(hash_lock); 42665450Sbrendan break; 42675450Sbrendan } 42685450Sbrendan 42695450Sbrendan if (pio == NULL) { 42705450Sbrendan /* 42715450Sbrendan * Insert a dummy header on the buflist so 42725450Sbrendan * l2arc_write_done() can find where the 42735450Sbrendan * write buffers begin without searching. 42745450Sbrendan */ 42755450Sbrendan list_insert_head(dev->l2ad_buflist, head); 42765450Sbrendan 42775450Sbrendan cb = kmem_alloc( 42785450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 42795450Sbrendan cb->l2wcb_dev = dev; 42805450Sbrendan cb->l2wcb_head = head; 42815450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 42825450Sbrendan ZIO_FLAG_CANFAIL); 42835450Sbrendan } 42845450Sbrendan 42855450Sbrendan /* 42865450Sbrendan * Create and add a new L2ARC header. 42875450Sbrendan */ 42885450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 42895450Sbrendan hdrl2->b_dev = dev; 42905450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 42915450Sbrendan 42925450Sbrendan ab->b_flags |= ARC_L2_WRITING; 42935450Sbrendan ab->b_l2hdr = hdrl2; 42945450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 42955450Sbrendan buf_data = ab->b_buf->b_data; 42965450Sbrendan buf_sz = ab->b_size; 42975450Sbrendan 42985450Sbrendan /* 42995450Sbrendan * Compute and store the buffer cksum before 43005450Sbrendan * writing. On debug the cksum is verified first. 43015450Sbrendan */ 43025450Sbrendan arc_cksum_verify(ab->b_buf); 43035450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 43045450Sbrendan 43055450Sbrendan mutex_exit(hash_lock); 43065450Sbrendan 43075450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 43085450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 43095450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 43105450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 43115450Sbrendan 43125450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 43135450Sbrendan zio_t *, wzio); 43145450Sbrendan (void) zio_nowait(wzio); 43155450Sbrendan 43167754SJeff.Bonwick@Sun.COM /* 43177754SJeff.Bonwick@Sun.COM * Keep the clock hand suitably device-aligned. 43187754SJeff.Bonwick@Sun.COM */ 43197754SJeff.Bonwick@Sun.COM buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 43207754SJeff.Bonwick@Sun.COM 43215450Sbrendan write_sz += buf_sz; 43225450Sbrendan dev->l2ad_hand += buf_sz; 43235450Sbrendan } 43245450Sbrendan 43255450Sbrendan mutex_exit(list_lock); 43265450Sbrendan 43275450Sbrendan if (full == B_TRUE) 43285450Sbrendan break; 43295450Sbrendan } 43305450Sbrendan mutex_exit(&l2arc_buflist_mtx); 43315450Sbrendan 43325450Sbrendan if (pio == NULL) { 43335450Sbrendan ASSERT3U(write_sz, ==, 0); 43345450Sbrendan kmem_cache_free(hdr_cache, head); 43358582SBrendan.Gregg@Sun.COM return (0); 43365450Sbrendan } 43375450Sbrendan 43385450Sbrendan ASSERT3U(write_sz, <=, target_sz); 43395450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 43408582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 43415450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 434210922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0); 43435450Sbrendan 43445450Sbrendan /* 43455450Sbrendan * Bump device hand to the device start if it is approaching the end. 43465450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 43475450Sbrendan */ 43486987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 434910922SJeff.Bonwick@Sun.COM vdev_space_update(dev->l2ad_vdev, 435010922SJeff.Bonwick@Sun.COM dev->l2ad_end - dev->l2ad_hand, 0, 0); 43515450Sbrendan dev->l2ad_hand = dev->l2ad_start; 43525450Sbrendan dev->l2ad_evict = dev->l2ad_start; 43535450Sbrendan dev->l2ad_first = B_FALSE; 43545450Sbrendan } 43555450Sbrendan 43568582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_TRUE; 43575450Sbrendan (void) zio_wait(pio); 43588582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_FALSE; 43598582SBrendan.Gregg@Sun.COM 43608582SBrendan.Gregg@Sun.COM return (write_sz); 43615450Sbrendan } 43625450Sbrendan 43635450Sbrendan /* 43645450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 43655450Sbrendan * heart of the L2ARC. 43665450Sbrendan */ 43675450Sbrendan static void 43685450Sbrendan l2arc_feed_thread(void) 43695450Sbrendan { 43705450Sbrendan callb_cpr_t cpr; 43715450Sbrendan l2arc_dev_t *dev; 43725450Sbrendan spa_t *spa; 43738582SBrendan.Gregg@Sun.COM uint64_t size, wrote; 4374*11066Srafael.vanoni@sun.com clock_t begin, next = ddi_get_lbolt(); 43755450Sbrendan 43765450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 43775450Sbrendan 43785450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 43795450Sbrendan 43805450Sbrendan while (l2arc_thread_exit == 0) { 43815450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 43826987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 43838582SBrendan.Gregg@Sun.COM next); 43846987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 4385*11066Srafael.vanoni@sun.com next = ddi_get_lbolt() + hz; 43866987Sbrendan 43876987Sbrendan /* 43886987Sbrendan * Quick check for L2ARC devices. 43896987Sbrendan */ 43906987Sbrendan mutex_enter(&l2arc_dev_mtx); 43916987Sbrendan if (l2arc_ndev == 0) { 43926987Sbrendan mutex_exit(&l2arc_dev_mtx); 43936987Sbrendan continue; 43945450Sbrendan } 43956987Sbrendan mutex_exit(&l2arc_dev_mtx); 4396*11066Srafael.vanoni@sun.com begin = ddi_get_lbolt(); 43976643Seschrock 43985450Sbrendan /* 43996643Seschrock * This selects the next l2arc device to write to, and in 44006643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 44016987Sbrendan * will return NULL if there are now no l2arc devices or if 44026987Sbrendan * they are all faulted. 44036987Sbrendan * 44046987Sbrendan * If a device is returned, its spa's config lock is also 44056987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 44066987Sbrendan * will grab and release l2arc_dev_mtx. 44075450Sbrendan */ 44086987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 44095450Sbrendan continue; 44106987Sbrendan 44116987Sbrendan spa = dev->l2ad_spa; 44126987Sbrendan ASSERT(spa != NULL); 44135450Sbrendan 44145450Sbrendan /* 44155450Sbrendan * Avoid contributing to memory pressure. 44165450Sbrendan */ 44175450Sbrendan if (arc_reclaim_needed()) { 44185450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 44197754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44205450Sbrendan continue; 44215450Sbrendan } 44225450Sbrendan 44235450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 44245450Sbrendan 44258582SBrendan.Gregg@Sun.COM size = l2arc_write_size(dev); 44266987Sbrendan 44275450Sbrendan /* 44285450Sbrendan * Evict L2ARC buffers that will be overwritten. 44295450Sbrendan */ 44306987Sbrendan l2arc_evict(dev, size, B_FALSE); 44315450Sbrendan 44325450Sbrendan /* 44335450Sbrendan * Write ARC buffers. 44345450Sbrendan */ 44358582SBrendan.Gregg@Sun.COM wrote = l2arc_write_buffers(spa, dev, size); 44368582SBrendan.Gregg@Sun.COM 44378582SBrendan.Gregg@Sun.COM /* 44388582SBrendan.Gregg@Sun.COM * Calculate interval between writes. 44398582SBrendan.Gregg@Sun.COM */ 44408582SBrendan.Gregg@Sun.COM next = l2arc_write_interval(begin, size, wrote); 44417754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44425450Sbrendan } 44435450Sbrendan 44445450Sbrendan l2arc_thread_exit = 0; 44455450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 44465450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 44475450Sbrendan thread_exit(); 44485450Sbrendan } 44495450Sbrendan 44506643Seschrock boolean_t 44516643Seschrock l2arc_vdev_present(vdev_t *vd) 44526643Seschrock { 44536643Seschrock l2arc_dev_t *dev; 44546643Seschrock 44556643Seschrock mutex_enter(&l2arc_dev_mtx); 44566643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 44576643Seschrock dev = list_next(l2arc_dev_list, dev)) { 44586643Seschrock if (dev->l2ad_vdev == vd) 44596643Seschrock break; 44606643Seschrock } 44616643Seschrock mutex_exit(&l2arc_dev_mtx); 44626643Seschrock 44636643Seschrock return (dev != NULL); 44646643Seschrock } 44656643Seschrock 44665450Sbrendan /* 44675450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 44685450Sbrendan * validated the vdev and opened it. 44695450Sbrendan */ 44705450Sbrendan void 44719816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa_t *spa, vdev_t *vd) 44725450Sbrendan { 44735450Sbrendan l2arc_dev_t *adddev; 44745450Sbrendan 44756643Seschrock ASSERT(!l2arc_vdev_present(vd)); 44766643Seschrock 44775450Sbrendan /* 44785450Sbrendan * Create a new l2arc device entry. 44795450Sbrendan */ 44805450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 44815450Sbrendan adddev->l2ad_spa = spa; 44825450Sbrendan adddev->l2ad_vdev = vd; 44835450Sbrendan adddev->l2ad_write = l2arc_write_max; 44846987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 44859816SGeorge.Wilson@Sun.COM adddev->l2ad_start = VDEV_LABEL_START_SIZE; 44869816SGeorge.Wilson@Sun.COM adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 44875450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 44885450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 44895450Sbrendan adddev->l2ad_first = B_TRUE; 44908582SBrendan.Gregg@Sun.COM adddev->l2ad_writing = B_FALSE; 44915450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 44925450Sbrendan 44935450Sbrendan /* 44945450Sbrendan * This is a list of all ARC buffers that are still valid on the 44955450Sbrendan * device. 44965450Sbrendan */ 44975450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 44985450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 44995450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 45005450Sbrendan 450110922SJeff.Bonwick@Sun.COM vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 45025450Sbrendan 45035450Sbrendan /* 45045450Sbrendan * Add device to global list 45055450Sbrendan */ 45065450Sbrendan mutex_enter(&l2arc_dev_mtx); 45075450Sbrendan list_insert_head(l2arc_dev_list, adddev); 45085450Sbrendan atomic_inc_64(&l2arc_ndev); 45095450Sbrendan mutex_exit(&l2arc_dev_mtx); 45105450Sbrendan } 45115450Sbrendan 45125450Sbrendan /* 45135450Sbrendan * Remove a vdev from the L2ARC. 45145450Sbrendan */ 45155450Sbrendan void 45165450Sbrendan l2arc_remove_vdev(vdev_t *vd) 45175450Sbrendan { 45185450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 45195450Sbrendan 45205450Sbrendan /* 45215450Sbrendan * Find the device by vdev 45225450Sbrendan */ 45235450Sbrendan mutex_enter(&l2arc_dev_mtx); 45245450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 45255450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 45265450Sbrendan if (vd == dev->l2ad_vdev) { 45275450Sbrendan remdev = dev; 45285450Sbrendan break; 45295450Sbrendan } 45305450Sbrendan } 45315450Sbrendan ASSERT(remdev != NULL); 45325450Sbrendan 45335450Sbrendan /* 45345450Sbrendan * Remove device from global list 45355450Sbrendan */ 45365450Sbrendan list_remove(l2arc_dev_list, remdev); 45375450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 45386987Sbrendan atomic_dec_64(&l2arc_ndev); 45396987Sbrendan mutex_exit(&l2arc_dev_mtx); 45405450Sbrendan 45415450Sbrendan /* 45425450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 45435450Sbrendan */ 45445450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 45455450Sbrendan list_destroy(remdev->l2ad_buflist); 45465450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 45475450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 45485450Sbrendan } 45495450Sbrendan 45505450Sbrendan void 45517754SJeff.Bonwick@Sun.COM l2arc_init(void) 45525450Sbrendan { 45535450Sbrendan l2arc_thread_exit = 0; 45545450Sbrendan l2arc_ndev = 0; 45555450Sbrendan l2arc_writes_sent = 0; 45565450Sbrendan l2arc_writes_done = 0; 45575450Sbrendan 45585450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 45595450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 45605450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 45615450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 45625450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 45635450Sbrendan 45645450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 45655450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 45665450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 45675450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 45685450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 45695450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 45705450Sbrendan } 45715450Sbrendan 45725450Sbrendan void 45737754SJeff.Bonwick@Sun.COM l2arc_fini(void) 45745450Sbrendan { 45756987Sbrendan /* 45766987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 45776987Sbrendan * Because of this, we can assume that all l2arc devices have 45786987Sbrendan * already been removed when the pools themselves were removed. 45796987Sbrendan */ 45806987Sbrendan 45816987Sbrendan l2arc_do_free_on_write(); 45826987Sbrendan 45835450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 45845450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 45855450Sbrendan mutex_destroy(&l2arc_dev_mtx); 45865450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 45875450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 45885450Sbrendan 45895450Sbrendan list_destroy(l2arc_dev_list); 45905450Sbrendan list_destroy(l2arc_free_on_write); 45915450Sbrendan } 45927754SJeff.Bonwick@Sun.COM 45937754SJeff.Bonwick@Sun.COM void 45947754SJeff.Bonwick@Sun.COM l2arc_start(void) 45957754SJeff.Bonwick@Sun.COM { 45968241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 45977754SJeff.Bonwick@Sun.COM return; 45987754SJeff.Bonwick@Sun.COM 45997754SJeff.Bonwick@Sun.COM (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 46007754SJeff.Bonwick@Sun.COM TS_RUN, minclsyspri); 46017754SJeff.Bonwick@Sun.COM } 46027754SJeff.Bonwick@Sun.COM 46037754SJeff.Bonwick@Sun.COM void 46047754SJeff.Bonwick@Sun.COM l2arc_stop(void) 46057754SJeff.Bonwick@Sun.COM { 46068241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46077754SJeff.Bonwick@Sun.COM return; 46087754SJeff.Bonwick@Sun.COM 46097754SJeff.Bonwick@Sun.COM mutex_enter(&l2arc_feed_thr_lock); 46107754SJeff.Bonwick@Sun.COM cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 46117754SJeff.Bonwick@Sun.COM l2arc_thread_exit = 1; 46127754SJeff.Bonwick@Sun.COM while (l2arc_thread_exit != 0) 46137754SJeff.Bonwick@Sun.COM cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 46147754SJeff.Bonwick@Sun.COM mutex_exit(&l2arc_feed_thr_lock); 46157754SJeff.Bonwick@Sun.COM } 4616