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> 1223093Sahrens #include <sys/zio_checksum.h> 123789Sahrens #include <sys/zfs_context.h> 124789Sahrens #include <sys/arc.h> 125789Sahrens #include <sys/refcount.h> 1266643Seschrock #include <sys/vdev.h> 127*9816SGeorge.Wilson@Sun.COM #include <sys/vdev_impl.h> 128789Sahrens #ifdef _KERNEL 129789Sahrens #include <sys/vmsystm.h> 130789Sahrens #include <vm/anon.h> 131789Sahrens #include <sys/fs/swapnode.h> 1321484Sek110237 #include <sys/dnlc.h> 133789Sahrens #endif 134789Sahrens #include <sys/callb.h> 1353403Sbmc #include <sys/kstat.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; 1817046Sahrens int zfs_mdcomp_disable = 0; 1828582SBrendan.Gregg@Sun.COM int zfs_arc_grow_retry = 0; 1838582SBrendan.Gregg@Sun.COM int zfs_arc_shrink_shift = 0; 1848582SBrendan.Gregg@Sun.COM int zfs_arc_p_min_shift = 0; 1852885Sahrens 1862885Sahrens /* 1875450Sbrendan * Note that buffers can be in one of 6 states: 188789Sahrens * ARC_anon - anonymous (discussed below) 1891544Seschrock * ARC_mru - recently used, currently cached 1901544Seschrock * ARC_mru_ghost - recentely used, no longer in cache 1911544Seschrock * ARC_mfu - frequently used, currently cached 1921544Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 1935450Sbrendan * ARC_l2c_only - exists in L2ARC but not other states 1944309Smaybee * When there are no active references to the buffer, they are 1954309Smaybee * are linked onto a list in one of these arc states. These are 1964309Smaybee * the only buffers that can be evicted or deleted. Within each 1974309Smaybee * state there are multiple lists, one for meta-data and one for 1984309Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 1994309Smaybee * etc.) is tracked separately so that it can be managed more 2005450Sbrendan * explicitly: favored over data, limited explicitly. 201789Sahrens * 202789Sahrens * Anonymous buffers are buffers that are not associated with 203789Sahrens * a DVA. These are buffers that hold dirty block copies 204789Sahrens * before they are written to stable storage. By definition, 2051544Seschrock * they are "ref'd" and are considered part of arc_mru 206789Sahrens * that cannot be freed. Generally, they will aquire a DVA 2071544Seschrock * as they are written and migrate onto the arc_mru list. 2085450Sbrendan * 2095450Sbrendan * The ARC_l2c_only state is for buffers that are in the second 2105450Sbrendan * level ARC but no longer in any of the ARC_m* lists. The second 2115450Sbrendan * level ARC itself may also contain buffers that are in any of 2125450Sbrendan * the ARC_m* states - meaning that a buffer can exist in two 2135450Sbrendan * places. The reason for the ARC_l2c_only state is to keep the 2145450Sbrendan * buffer header in the hash table, so that reads that hit the 2155450Sbrendan * second level ARC benefit from these fast lookups. 216789Sahrens */ 217789Sahrens 218789Sahrens typedef struct arc_state { 2194309Smaybee list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */ 2204309Smaybee uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 2214309Smaybee uint64_t arcs_size; /* total amount of data in this state */ 2223403Sbmc kmutex_t arcs_mtx; 223789Sahrens } arc_state_t; 224789Sahrens 2255450Sbrendan /* The 6 states: */ 226789Sahrens static arc_state_t ARC_anon; 2271544Seschrock static arc_state_t ARC_mru; 2281544Seschrock static arc_state_t ARC_mru_ghost; 2291544Seschrock static arc_state_t ARC_mfu; 2301544Seschrock static arc_state_t ARC_mfu_ghost; 2315450Sbrendan static arc_state_t ARC_l2c_only; 232789Sahrens 2333403Sbmc typedef struct arc_stats { 2343403Sbmc kstat_named_t arcstat_hits; 2353403Sbmc kstat_named_t arcstat_misses; 2363403Sbmc kstat_named_t arcstat_demand_data_hits; 2373403Sbmc kstat_named_t arcstat_demand_data_misses; 2383403Sbmc kstat_named_t arcstat_demand_metadata_hits; 2393403Sbmc kstat_named_t arcstat_demand_metadata_misses; 2403403Sbmc kstat_named_t arcstat_prefetch_data_hits; 2413403Sbmc kstat_named_t arcstat_prefetch_data_misses; 2423403Sbmc kstat_named_t arcstat_prefetch_metadata_hits; 2433403Sbmc kstat_named_t arcstat_prefetch_metadata_misses; 2443403Sbmc kstat_named_t arcstat_mru_hits; 2453403Sbmc kstat_named_t arcstat_mru_ghost_hits; 2463403Sbmc kstat_named_t arcstat_mfu_hits; 2473403Sbmc kstat_named_t arcstat_mfu_ghost_hits; 2483403Sbmc kstat_named_t arcstat_deleted; 2493403Sbmc kstat_named_t arcstat_recycle_miss; 2503403Sbmc kstat_named_t arcstat_mutex_miss; 2513403Sbmc kstat_named_t arcstat_evict_skip; 2523403Sbmc kstat_named_t arcstat_hash_elements; 2533403Sbmc kstat_named_t arcstat_hash_elements_max; 2543403Sbmc kstat_named_t arcstat_hash_collisions; 2553403Sbmc kstat_named_t arcstat_hash_chains; 2563403Sbmc kstat_named_t arcstat_hash_chain_max; 2573403Sbmc kstat_named_t arcstat_p; 2583403Sbmc kstat_named_t arcstat_c; 2593403Sbmc kstat_named_t arcstat_c_min; 2603403Sbmc kstat_named_t arcstat_c_max; 2613403Sbmc kstat_named_t arcstat_size; 2625450Sbrendan kstat_named_t arcstat_hdr_size; 2638582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_data_size; 2648582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_other_size; 2655450Sbrendan kstat_named_t arcstat_l2_hits; 2665450Sbrendan kstat_named_t arcstat_l2_misses; 2675450Sbrendan kstat_named_t arcstat_l2_feeds; 2685450Sbrendan kstat_named_t arcstat_l2_rw_clash; 2698582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_l2_read_bytes; 2708582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_l2_write_bytes; 2715450Sbrendan kstat_named_t arcstat_l2_writes_sent; 2725450Sbrendan kstat_named_t arcstat_l2_writes_done; 2735450Sbrendan kstat_named_t arcstat_l2_writes_error; 2745450Sbrendan kstat_named_t arcstat_l2_writes_hdr_miss; 2755450Sbrendan kstat_named_t arcstat_l2_evict_lock_retry; 2765450Sbrendan kstat_named_t arcstat_l2_evict_reading; 2775450Sbrendan kstat_named_t arcstat_l2_free_on_write; 2785450Sbrendan kstat_named_t arcstat_l2_abort_lowmem; 2795450Sbrendan kstat_named_t arcstat_l2_cksum_bad; 2805450Sbrendan kstat_named_t arcstat_l2_io_error; 2815450Sbrendan kstat_named_t arcstat_l2_size; 2825450Sbrendan kstat_named_t arcstat_l2_hdr_size; 2836245Smaybee kstat_named_t arcstat_memory_throttle_count; 2843403Sbmc } arc_stats_t; 2853403Sbmc 2863403Sbmc static arc_stats_t arc_stats = { 2873403Sbmc { "hits", KSTAT_DATA_UINT64 }, 2883403Sbmc { "misses", KSTAT_DATA_UINT64 }, 2893403Sbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 2903403Sbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 2913403Sbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 2923403Sbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 2933403Sbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 2943403Sbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 2953403Sbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 2963403Sbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 2973403Sbmc { "mru_hits", KSTAT_DATA_UINT64 }, 2983403Sbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 2993403Sbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 3003403Sbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 3013403Sbmc { "deleted", KSTAT_DATA_UINT64 }, 3023403Sbmc { "recycle_miss", KSTAT_DATA_UINT64 }, 3033403Sbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 3043403Sbmc { "evict_skip", KSTAT_DATA_UINT64 }, 3053403Sbmc { "hash_elements", KSTAT_DATA_UINT64 }, 3063403Sbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 3073403Sbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 3083403Sbmc { "hash_chains", KSTAT_DATA_UINT64 }, 3093403Sbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 3103403Sbmc { "p", KSTAT_DATA_UINT64 }, 3113403Sbmc { "c", KSTAT_DATA_UINT64 }, 3123403Sbmc { "c_min", KSTAT_DATA_UINT64 }, 3133403Sbmc { "c_max", KSTAT_DATA_UINT64 }, 3145450Sbrendan { "size", KSTAT_DATA_UINT64 }, 3155450Sbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 3168582SBrendan.Gregg@Sun.COM { "data_size", KSTAT_DATA_UINT64 }, 3178582SBrendan.Gregg@Sun.COM { "other_size", KSTAT_DATA_UINT64 }, 3185450Sbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 3195450Sbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 3205450Sbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 3215450Sbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 3228582SBrendan.Gregg@Sun.COM { "l2_read_bytes", KSTAT_DATA_UINT64 }, 3238582SBrendan.Gregg@Sun.COM { "l2_write_bytes", KSTAT_DATA_UINT64 }, 3245450Sbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 3255450Sbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 3265450Sbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 3275450Sbrendan { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 3285450Sbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 3295450Sbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 3305450Sbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 3315450Sbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 3325450Sbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 3335450Sbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 3345450Sbrendan { "l2_size", KSTAT_DATA_UINT64 }, 3356245Smaybee { "l2_hdr_size", KSTAT_DATA_UINT64 }, 3366245Smaybee { "memory_throttle_count", KSTAT_DATA_UINT64 } 3373403Sbmc }; 338789Sahrens 3393403Sbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 3403403Sbmc 3413403Sbmc #define ARCSTAT_INCR(stat, val) \ 3423403Sbmc atomic_add_64(&arc_stats.stat.value.ui64, (val)); 3433403Sbmc 3443403Sbmc #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 3453403Sbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 3463403Sbmc 3473403Sbmc #define ARCSTAT_MAX(stat, val) { \ 3483403Sbmc uint64_t m; \ 3493403Sbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 3503403Sbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 3513403Sbmc continue; \ 3523403Sbmc } 3533403Sbmc 3543403Sbmc #define ARCSTAT_MAXSTAT(stat) \ 3553403Sbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 356789Sahrens 3573403Sbmc /* 3583403Sbmc * We define a macro to allow ARC hits/misses to be easily broken down by 3593403Sbmc * two separate conditions, giving a total of four different subtypes for 3603403Sbmc * each of hits and misses (so eight statistics total). 3613403Sbmc */ 3623403Sbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 3633403Sbmc if (cond1) { \ 3643403Sbmc if (cond2) { \ 3653403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 3663403Sbmc } else { \ 3673403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 3683403Sbmc } \ 3693403Sbmc } else { \ 3703403Sbmc if (cond2) { \ 3713403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 3723403Sbmc } else { \ 3733403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 3743403Sbmc } \ 3753403Sbmc } 376789Sahrens 3773403Sbmc kstat_t *arc_ksp; 3783403Sbmc static arc_state_t *arc_anon; 3793403Sbmc static arc_state_t *arc_mru; 3803403Sbmc static arc_state_t *arc_mru_ghost; 3813403Sbmc static arc_state_t *arc_mfu; 3823403Sbmc static arc_state_t *arc_mfu_ghost; 3835450Sbrendan static arc_state_t *arc_l2c_only; 3843403Sbmc 3853403Sbmc /* 3863403Sbmc * There are several ARC variables that are critical to export as kstats -- 3873403Sbmc * but we don't want to have to grovel around in the kstat whenever we wish to 3883403Sbmc * manipulate them. For these variables, we therefore define them to be in 3893403Sbmc * terms of the statistic variable. This assures that we are not introducing 3903403Sbmc * the possibility of inconsistency by having shadow copies of the variables, 3913403Sbmc * while still allowing the code to be readable. 3923403Sbmc */ 3933403Sbmc #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 3943403Sbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 3953403Sbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 3963403Sbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 3973403Sbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 3983403Sbmc 3993403Sbmc static int arc_no_grow; /* Don't try to grow cache size */ 4003403Sbmc static uint64_t arc_tempreserve; 4019412SAleksandr.Guzovskiy@Sun.COM static uint64_t arc_loaned_bytes; 4024309Smaybee static uint64_t arc_meta_used; 4034309Smaybee static uint64_t arc_meta_limit; 4044309Smaybee static uint64_t arc_meta_max = 0; 405789Sahrens 4065450Sbrendan typedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 4075450Sbrendan 408789Sahrens typedef struct arc_callback arc_callback_t; 409789Sahrens 410789Sahrens struct arc_callback { 4113547Smaybee void *acb_private; 412789Sahrens arc_done_func_t *acb_done; 413789Sahrens arc_buf_t *acb_buf; 414789Sahrens zio_t *acb_zio_dummy; 415789Sahrens arc_callback_t *acb_next; 416789Sahrens }; 417789Sahrens 4183547Smaybee typedef struct arc_write_callback arc_write_callback_t; 4193547Smaybee 4203547Smaybee struct arc_write_callback { 4213547Smaybee void *awcb_private; 4223547Smaybee arc_done_func_t *awcb_ready; 4233547Smaybee arc_done_func_t *awcb_done; 4243547Smaybee arc_buf_t *awcb_buf; 4253547Smaybee }; 4263547Smaybee 427789Sahrens struct arc_buf_hdr { 428789Sahrens /* protected by hash lock */ 429789Sahrens dva_t b_dva; 430789Sahrens uint64_t b_birth; 431789Sahrens uint64_t b_cksum0; 432789Sahrens 4333093Sahrens kmutex_t b_freeze_lock; 4343093Sahrens zio_cksum_t *b_freeze_cksum; 4353093Sahrens 436789Sahrens arc_buf_hdr_t *b_hash_next; 437789Sahrens arc_buf_t *b_buf; 438789Sahrens uint32_t b_flags; 4391544Seschrock uint32_t b_datacnt; 440789Sahrens 4413290Sjohansen arc_callback_t *b_acb; 442789Sahrens kcondvar_t b_cv; 4433290Sjohansen 4443290Sjohansen /* immutable */ 4453290Sjohansen arc_buf_contents_t b_type; 4463290Sjohansen uint64_t b_size; 4478636SMark.Maybee@Sun.COM uint64_t b_spa; 448789Sahrens 449789Sahrens /* protected by arc state mutex */ 450789Sahrens arc_state_t *b_state; 451789Sahrens list_node_t b_arc_node; 452789Sahrens 453789Sahrens /* updated atomically */ 454789Sahrens clock_t b_arc_access; 455789Sahrens 456789Sahrens /* self protecting */ 457789Sahrens refcount_t b_refcnt; 4585450Sbrendan 4595450Sbrendan l2arc_buf_hdr_t *b_l2hdr; 4605450Sbrendan list_node_t b_l2node; 461789Sahrens }; 462789Sahrens 4631544Seschrock static arc_buf_t *arc_eviction_list; 4641544Seschrock static kmutex_t arc_eviction_mtx; 4652887Smaybee static arc_buf_hdr_t arc_eviction_hdr; 4662688Smaybee static void arc_get_data_buf(arc_buf_t *buf); 4672688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 4684309Smaybee static int arc_evict_needed(arc_buf_contents_t type); 4698636SMark.Maybee@Sun.COM static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes); 4701544Seschrock 4711544Seschrock #define GHOST_STATE(state) \ 4725450Sbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 4735450Sbrendan (state) == arc_l2c_only) 4741544Seschrock 475789Sahrens /* 476789Sahrens * Private ARC flags. These flags are private ARC only flags that will show up 477789Sahrens * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 478789Sahrens * be passed in as arc_flags in things like arc_read. However, these flags 479789Sahrens * should never be passed and should only be set by ARC code. When adding new 480789Sahrens * public flags, make sure not to smash the private ones. 481789Sahrens */ 482789Sahrens 4831544Seschrock #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 484789Sahrens #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 485789Sahrens #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 486789Sahrens #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 4871544Seschrock #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 4882391Smaybee #define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 4895450Sbrendan #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 4907237Sek110237 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 4917237Sek110237 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 4927237Sek110237 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 4937237Sek110237 #define ARC_STORED (1 << 19) /* has been store()d to */ 494789Sahrens 4951544Seschrock #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 496789Sahrens #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 497789Sahrens #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 4988582SBrendan.Gregg@Sun.COM #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH) 499789Sahrens #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 5001544Seschrock #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 5015450Sbrendan #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 5027237Sek110237 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 5036987Sbrendan #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 5046987Sbrendan (hdr)->b_l2hdr != NULL) 5055450Sbrendan #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 5065450Sbrendan #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 5075450Sbrendan #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 508789Sahrens 509789Sahrens /* 5106018Sbrendan * Other sizes 5116018Sbrendan */ 5126018Sbrendan 5136018Sbrendan #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 5146018Sbrendan #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 5156018Sbrendan 5166018Sbrendan /* 517789Sahrens * Hash table routines 518789Sahrens */ 519789Sahrens 520789Sahrens #define HT_LOCK_PAD 64 521789Sahrens 522789Sahrens struct ht_lock { 523789Sahrens kmutex_t ht_lock; 524789Sahrens #ifdef _KERNEL 525789Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 526789Sahrens #endif 527789Sahrens }; 528789Sahrens 529789Sahrens #define BUF_LOCKS 256 530789Sahrens typedef struct buf_hash_table { 531789Sahrens uint64_t ht_mask; 532789Sahrens arc_buf_hdr_t **ht_table; 533789Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 534789Sahrens } buf_hash_table_t; 535789Sahrens 536789Sahrens static buf_hash_table_t buf_hash_table; 537789Sahrens 538789Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 539789Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 540789Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 541789Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 542789Sahrens #define HDR_LOCK(buf) \ 543789Sahrens (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth))) 544789Sahrens 545789Sahrens uint64_t zfs_crc64_table[256]; 546789Sahrens 5475450Sbrendan /* 5485450Sbrendan * Level 2 ARC 5495450Sbrendan */ 5505450Sbrendan 5515450Sbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 5528582SBrendan.Gregg@Sun.COM #define L2ARC_HEADROOM 2 /* num of writes */ 5538582SBrendan.Gregg@Sun.COM #define L2ARC_FEED_SECS 1 /* caching interval secs */ 5548582SBrendan.Gregg@Sun.COM #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 5555450Sbrendan 5565450Sbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 5575450Sbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 5585450Sbrendan 5595450Sbrendan /* 5605450Sbrendan * L2ARC Performance Tunables 5615450Sbrendan */ 5625450Sbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 5636987Sbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 5645450Sbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 5655450Sbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 5668582SBrendan.Gregg@Sun.COM uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 5675450Sbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 5688582SBrendan.Gregg@Sun.COM boolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 5698582SBrendan.Gregg@Sun.COM boolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 5705450Sbrendan 5715450Sbrendan /* 5725450Sbrendan * L2ARC Internals 5735450Sbrendan */ 5745450Sbrendan typedef struct l2arc_dev { 5755450Sbrendan vdev_t *l2ad_vdev; /* vdev */ 5765450Sbrendan spa_t *l2ad_spa; /* spa */ 5775450Sbrendan uint64_t l2ad_hand; /* next write location */ 5785450Sbrendan uint64_t l2ad_write; /* desired write size, bytes */ 5796987Sbrendan uint64_t l2ad_boost; /* warmup write boost, bytes */ 5805450Sbrendan uint64_t l2ad_start; /* first addr on device */ 5815450Sbrendan uint64_t l2ad_end; /* last addr on device */ 5825450Sbrendan uint64_t l2ad_evict; /* last addr eviction reached */ 5835450Sbrendan boolean_t l2ad_first; /* first sweep through */ 5848582SBrendan.Gregg@Sun.COM boolean_t l2ad_writing; /* currently writing */ 5855450Sbrendan list_t *l2ad_buflist; /* buffer list */ 5865450Sbrendan list_node_t l2ad_node; /* device list node */ 5875450Sbrendan } l2arc_dev_t; 5885450Sbrendan 5895450Sbrendan static list_t L2ARC_dev_list; /* device list */ 5905450Sbrendan static list_t *l2arc_dev_list; /* device list pointer */ 5915450Sbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 5925450Sbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 5935450Sbrendan static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 5945450Sbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 5955450Sbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 5965450Sbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 5975450Sbrendan static uint64_t l2arc_ndev; /* number of devices */ 5985450Sbrendan 5995450Sbrendan typedef struct l2arc_read_callback { 6005450Sbrendan arc_buf_t *l2rcb_buf; /* read buffer */ 6015450Sbrendan spa_t *l2rcb_spa; /* spa */ 6025450Sbrendan blkptr_t l2rcb_bp; /* original blkptr */ 6035450Sbrendan zbookmark_t l2rcb_zb; /* original bookmark */ 6045450Sbrendan int l2rcb_flags; /* original flags */ 6055450Sbrendan } l2arc_read_callback_t; 6065450Sbrendan 6075450Sbrendan typedef struct l2arc_write_callback { 6085450Sbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 6095450Sbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 6105450Sbrendan } l2arc_write_callback_t; 6115450Sbrendan 6125450Sbrendan struct l2arc_buf_hdr { 6135450Sbrendan /* protected by arc_buf_hdr mutex */ 6145450Sbrendan l2arc_dev_t *b_dev; /* L2ARC device */ 6159215SGeorge.Wilson@Sun.COM uint64_t b_daddr; /* disk address, offset byte */ 6165450Sbrendan }; 6175450Sbrendan 6185450Sbrendan typedef struct l2arc_data_free { 6195450Sbrendan /* protected by l2arc_free_on_write_mtx */ 6205450Sbrendan void *l2df_data; 6215450Sbrendan size_t l2df_size; 6225450Sbrendan void (*l2df_func)(void *, size_t); 6235450Sbrendan list_node_t l2df_list_node; 6245450Sbrendan } l2arc_data_free_t; 6255450Sbrendan 6265450Sbrendan static kmutex_t l2arc_feed_thr_lock; 6275450Sbrendan static kcondvar_t l2arc_feed_thr_cv; 6285450Sbrendan static uint8_t l2arc_thread_exit; 6295450Sbrendan 6305450Sbrendan static void l2arc_read_done(zio_t *zio); 6315450Sbrendan static void l2arc_hdr_stat_add(void); 6325450Sbrendan static void l2arc_hdr_stat_remove(void); 6335450Sbrendan 634789Sahrens static uint64_t 6358636SMark.Maybee@Sun.COM buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 636789Sahrens { 637789Sahrens uint8_t *vdva = (uint8_t *)dva; 638789Sahrens uint64_t crc = -1ULL; 639789Sahrens int i; 640789Sahrens 641789Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 642789Sahrens 643789Sahrens for (i = 0; i < sizeof (dva_t); i++) 644789Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 645789Sahrens 6468636SMark.Maybee@Sun.COM crc ^= (spa>>8) ^ birth; 647789Sahrens 648789Sahrens return (crc); 649789Sahrens } 650789Sahrens 651789Sahrens #define BUF_EMPTY(buf) \ 652789Sahrens ((buf)->b_dva.dva_word[0] == 0 && \ 653789Sahrens (buf)->b_dva.dva_word[1] == 0 && \ 654789Sahrens (buf)->b_birth == 0) 655789Sahrens 656789Sahrens #define BUF_EQUAL(spa, dva, birth, buf) \ 657789Sahrens ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 658789Sahrens ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 659789Sahrens ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 660789Sahrens 661789Sahrens static arc_buf_hdr_t * 6628636SMark.Maybee@Sun.COM buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 663789Sahrens { 664789Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 665789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 666789Sahrens arc_buf_hdr_t *buf; 667789Sahrens 668789Sahrens mutex_enter(hash_lock); 669789Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 670789Sahrens buf = buf->b_hash_next) { 671789Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 672789Sahrens *lockp = hash_lock; 673789Sahrens return (buf); 674789Sahrens } 675789Sahrens } 676789Sahrens mutex_exit(hash_lock); 677789Sahrens *lockp = NULL; 678789Sahrens return (NULL); 679789Sahrens } 680789Sahrens 681789Sahrens /* 682789Sahrens * Insert an entry into the hash table. If there is already an element 683789Sahrens * equal to elem in the hash table, then the already existing element 684789Sahrens * will be returned and the new element will not be inserted. 685789Sahrens * Otherwise returns NULL. 686789Sahrens */ 687789Sahrens static arc_buf_hdr_t * 688789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 689789Sahrens { 690789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 691789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 692789Sahrens arc_buf_hdr_t *fbuf; 6933403Sbmc uint32_t i; 694789Sahrens 6951544Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 696789Sahrens *lockp = hash_lock; 697789Sahrens mutex_enter(hash_lock); 698789Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 699789Sahrens fbuf = fbuf->b_hash_next, i++) { 700789Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 701789Sahrens return (fbuf); 702789Sahrens } 703789Sahrens 704789Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 705789Sahrens buf_hash_table.ht_table[idx] = buf; 7061544Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 707789Sahrens 708789Sahrens /* collect some hash table performance data */ 709789Sahrens if (i > 0) { 7103403Sbmc ARCSTAT_BUMP(arcstat_hash_collisions); 711789Sahrens if (i == 1) 7123403Sbmc ARCSTAT_BUMP(arcstat_hash_chains); 7133403Sbmc 7143403Sbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 715789Sahrens } 7163403Sbmc 7173403Sbmc ARCSTAT_BUMP(arcstat_hash_elements); 7183403Sbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 719789Sahrens 720789Sahrens return (NULL); 721789Sahrens } 722789Sahrens 723789Sahrens static void 724789Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 725789Sahrens { 726789Sahrens arc_buf_hdr_t *fbuf, **bufp; 727789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 728789Sahrens 729789Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 7301544Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 731789Sahrens 732789Sahrens bufp = &buf_hash_table.ht_table[idx]; 733789Sahrens while ((fbuf = *bufp) != buf) { 734789Sahrens ASSERT(fbuf != NULL); 735789Sahrens bufp = &fbuf->b_hash_next; 736789Sahrens } 737789Sahrens *bufp = buf->b_hash_next; 738789Sahrens buf->b_hash_next = NULL; 7391544Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 740789Sahrens 741789Sahrens /* collect some hash table performance data */ 7423403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 7433403Sbmc 744789Sahrens if (buf_hash_table.ht_table[idx] && 745789Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 7463403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 747789Sahrens } 748789Sahrens 749789Sahrens /* 750789Sahrens * Global data structures and functions for the buf kmem cache. 751789Sahrens */ 752789Sahrens static kmem_cache_t *hdr_cache; 753789Sahrens static kmem_cache_t *buf_cache; 754789Sahrens 755789Sahrens static void 756789Sahrens buf_fini(void) 757789Sahrens { 758789Sahrens int i; 759789Sahrens 760789Sahrens kmem_free(buf_hash_table.ht_table, 761789Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 762789Sahrens for (i = 0; i < BUF_LOCKS; i++) 763789Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 764789Sahrens kmem_cache_destroy(hdr_cache); 765789Sahrens kmem_cache_destroy(buf_cache); 766789Sahrens } 767789Sahrens 768789Sahrens /* 769789Sahrens * Constructor callback - called when the cache is empty 770789Sahrens * and a new buf is requested. 771789Sahrens */ 772789Sahrens /* ARGSUSED */ 773789Sahrens static int 774789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 775789Sahrens { 776789Sahrens arc_buf_hdr_t *buf = vbuf; 777789Sahrens 778789Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 779789Sahrens refcount_create(&buf->b_refcnt); 780789Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 7814831Sgw25295 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 7828582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 7838582SBrendan.Gregg@Sun.COM 784789Sahrens return (0); 785789Sahrens } 786789Sahrens 7877545SMark.Maybee@Sun.COM /* ARGSUSED */ 7887545SMark.Maybee@Sun.COM static int 7897545SMark.Maybee@Sun.COM buf_cons(void *vbuf, void *unused, int kmflag) 7907545SMark.Maybee@Sun.COM { 7917545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 7927545SMark.Maybee@Sun.COM 7937545SMark.Maybee@Sun.COM bzero(buf, sizeof (arc_buf_t)); 7947545SMark.Maybee@Sun.COM rw_init(&buf->b_lock, NULL, RW_DEFAULT, NULL); 7958582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 7968582SBrendan.Gregg@Sun.COM 7977545SMark.Maybee@Sun.COM return (0); 7987545SMark.Maybee@Sun.COM } 7997545SMark.Maybee@Sun.COM 800789Sahrens /* 801789Sahrens * Destructor callback - called when a cached buf is 802789Sahrens * no longer required. 803789Sahrens */ 804789Sahrens /* ARGSUSED */ 805789Sahrens static void 806789Sahrens hdr_dest(void *vbuf, void *unused) 807789Sahrens { 808789Sahrens arc_buf_hdr_t *buf = vbuf; 809789Sahrens 810789Sahrens refcount_destroy(&buf->b_refcnt); 811789Sahrens cv_destroy(&buf->b_cv); 8124831Sgw25295 mutex_destroy(&buf->b_freeze_lock); 8138582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 814789Sahrens } 815789Sahrens 8167545SMark.Maybee@Sun.COM /* ARGSUSED */ 8177545SMark.Maybee@Sun.COM static void 8187545SMark.Maybee@Sun.COM buf_dest(void *vbuf, void *unused) 8197545SMark.Maybee@Sun.COM { 8207545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 8217545SMark.Maybee@Sun.COM 8227545SMark.Maybee@Sun.COM rw_destroy(&buf->b_lock); 8238582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 8247545SMark.Maybee@Sun.COM } 8257545SMark.Maybee@Sun.COM 826789Sahrens /* 827789Sahrens * Reclaim callback -- invoked when memory is low. 828789Sahrens */ 829789Sahrens /* ARGSUSED */ 830789Sahrens static void 831789Sahrens hdr_recl(void *unused) 832789Sahrens { 833789Sahrens dprintf("hdr_recl called\n"); 8343158Smaybee /* 8353158Smaybee * umem calls the reclaim func when we destroy the buf cache, 8363158Smaybee * which is after we do arc_fini(). 8373158Smaybee */ 8383158Smaybee if (!arc_dead) 8393158Smaybee cv_signal(&arc_reclaim_thr_cv); 840789Sahrens } 841789Sahrens 842789Sahrens static void 843789Sahrens buf_init(void) 844789Sahrens { 845789Sahrens uint64_t *ct; 8461544Seschrock uint64_t hsize = 1ULL << 12; 847789Sahrens int i, j; 848789Sahrens 849789Sahrens /* 850789Sahrens * The hash table is big enough to fill all of physical memory 8511544Seschrock * with an average 64K block size. The table will take up 8521544Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 853789Sahrens */ 8541544Seschrock while (hsize * 65536 < physmem * PAGESIZE) 855789Sahrens hsize <<= 1; 8561544Seschrock retry: 857789Sahrens buf_hash_table.ht_mask = hsize - 1; 8581544Seschrock buf_hash_table.ht_table = 8591544Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 8601544Seschrock if (buf_hash_table.ht_table == NULL) { 8611544Seschrock ASSERT(hsize > (1ULL << 8)); 8621544Seschrock hsize >>= 1; 8631544Seschrock goto retry; 8641544Seschrock } 865789Sahrens 866789Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 867789Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 868789Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 8697545SMark.Maybee@Sun.COM 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 870789Sahrens 871789Sahrens for (i = 0; i < 256; i++) 872789Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 873789Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 874789Sahrens 875789Sahrens for (i = 0; i < BUF_LOCKS; i++) { 876789Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 877789Sahrens NULL, MUTEX_DEFAULT, NULL); 878789Sahrens } 879789Sahrens } 880789Sahrens 881789Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 882789Sahrens 883789Sahrens static void 8843093Sahrens arc_cksum_verify(arc_buf_t *buf) 8853093Sahrens { 8863093Sahrens zio_cksum_t zc; 8873093Sahrens 8883312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 8893093Sahrens return; 8903093Sahrens 8913093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8923265Sahrens if (buf->b_hdr->b_freeze_cksum == NULL || 8933265Sahrens (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 8943093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8953093Sahrens return; 8963093Sahrens } 8973093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8983093Sahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 8993093Sahrens panic("buffer modified while frozen!"); 9003093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9013093Sahrens } 9023093Sahrens 9035450Sbrendan static int 9045450Sbrendan arc_cksum_equal(arc_buf_t *buf) 9055450Sbrendan { 9065450Sbrendan zio_cksum_t zc; 9075450Sbrendan int equal; 9085450Sbrendan 9095450Sbrendan mutex_enter(&buf->b_hdr->b_freeze_lock); 9105450Sbrendan fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 9115450Sbrendan equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 9125450Sbrendan mutex_exit(&buf->b_hdr->b_freeze_lock); 9135450Sbrendan 9145450Sbrendan return (equal); 9155450Sbrendan } 9165450Sbrendan 9173093Sahrens static void 9185450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force) 9193093Sahrens { 9205450Sbrendan if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 9213093Sahrens return; 9223093Sahrens 9233093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9243093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9253093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9263093Sahrens return; 9273093Sahrens } 9283093Sahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 9293093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 9303093Sahrens buf->b_hdr->b_freeze_cksum); 9313093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9323093Sahrens } 9333093Sahrens 9343093Sahrens void 9353093Sahrens arc_buf_thaw(arc_buf_t *buf) 9363093Sahrens { 9375450Sbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 9385450Sbrendan if (buf->b_hdr->b_state != arc_anon) 9395450Sbrendan panic("modifying non-anon buffer!"); 9405450Sbrendan if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 9415450Sbrendan panic("modifying buffer while i/o in progress!"); 9425450Sbrendan arc_cksum_verify(buf); 9435450Sbrendan } 9445450Sbrendan 9453093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9463093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9473093Sahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 9483093Sahrens buf->b_hdr->b_freeze_cksum = NULL; 9493093Sahrens } 9503093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9513093Sahrens } 9523093Sahrens 9533093Sahrens void 9543093Sahrens arc_buf_freeze(arc_buf_t *buf) 9553093Sahrens { 9563312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 9573312Sahrens return; 9583312Sahrens 9593093Sahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 9603403Sbmc buf->b_hdr->b_state == arc_anon); 9615450Sbrendan arc_cksum_compute(buf, B_FALSE); 9623093Sahrens } 9633093Sahrens 9643093Sahrens static void 965789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 966789Sahrens { 967789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 968789Sahrens 969789Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 9703403Sbmc (ab->b_state != arc_anon)) { 9713700Sek110237 uint64_t delta = ab->b_size * ab->b_datacnt; 9724309Smaybee list_t *list = &ab->b_state->arcs_list[ab->b_type]; 9734309Smaybee uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 974789Sahrens 9753403Sbmc ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx)); 9763403Sbmc mutex_enter(&ab->b_state->arcs_mtx); 977789Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 9784309Smaybee list_remove(list, ab); 9791544Seschrock if (GHOST_STATE(ab->b_state)) { 9801544Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 9811544Seschrock ASSERT3P(ab->b_buf, ==, NULL); 9821544Seschrock delta = ab->b_size; 9831544Seschrock } 9841544Seschrock ASSERT(delta > 0); 9854309Smaybee ASSERT3U(*size, >=, delta); 9864309Smaybee atomic_add_64(size, -delta); 9873403Sbmc mutex_exit(&ab->b_state->arcs_mtx); 9887046Sahrens /* remove the prefetch flag if we get a reference */ 9892391Smaybee if (ab->b_flags & ARC_PREFETCH) 9902391Smaybee ab->b_flags &= ~ARC_PREFETCH; 991789Sahrens } 992789Sahrens } 993789Sahrens 994789Sahrens static int 995789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 996789Sahrens { 997789Sahrens int cnt; 9983403Sbmc arc_state_t *state = ab->b_state; 999789Sahrens 10003403Sbmc ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 10013403Sbmc ASSERT(!GHOST_STATE(state)); 1002789Sahrens 1003789Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 10043403Sbmc (state != arc_anon)) { 10054309Smaybee uint64_t *size = &state->arcs_lsize[ab->b_type]; 10064309Smaybee 10073403Sbmc ASSERT(!MUTEX_HELD(&state->arcs_mtx)); 10083403Sbmc mutex_enter(&state->arcs_mtx); 1009789Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 10104309Smaybee list_insert_head(&state->arcs_list[ab->b_type], ab); 10111544Seschrock ASSERT(ab->b_datacnt > 0); 10124309Smaybee atomic_add_64(size, ab->b_size * ab->b_datacnt); 10133403Sbmc mutex_exit(&state->arcs_mtx); 1014789Sahrens } 1015789Sahrens return (cnt); 1016789Sahrens } 1017789Sahrens 1018789Sahrens /* 1019789Sahrens * Move the supplied buffer to the indicated state. The mutex 1020789Sahrens * for the buffer must be held by the caller. 1021789Sahrens */ 1022789Sahrens static void 10231544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1024789Sahrens { 10251544Seschrock arc_state_t *old_state = ab->b_state; 10263700Sek110237 int64_t refcnt = refcount_count(&ab->b_refcnt); 10273700Sek110237 uint64_t from_delta, to_delta; 1028789Sahrens 1029789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 10301544Seschrock ASSERT(new_state != old_state); 10311544Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 10321544Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 10331544Seschrock 10341544Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 1035789Sahrens 1036789Sahrens /* 1037789Sahrens * If this buffer is evictable, transfer it from the 1038789Sahrens * old state list to the new state list. 1039789Sahrens */ 10401544Seschrock if (refcnt == 0) { 10413403Sbmc if (old_state != arc_anon) { 10423403Sbmc int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx); 10434309Smaybee uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 10441544Seschrock 10451544Seschrock if (use_mutex) 10463403Sbmc mutex_enter(&old_state->arcs_mtx); 10471544Seschrock 10481544Seschrock ASSERT(list_link_active(&ab->b_arc_node)); 10494309Smaybee list_remove(&old_state->arcs_list[ab->b_type], ab); 1050789Sahrens 10512391Smaybee /* 10522391Smaybee * If prefetching out of the ghost cache, 10532391Smaybee * we will have a non-null datacnt. 10542391Smaybee */ 10552391Smaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 10562391Smaybee /* ghost elements have a ghost size */ 10571544Seschrock ASSERT(ab->b_buf == NULL); 10581544Seschrock from_delta = ab->b_size; 1059789Sahrens } 10604309Smaybee ASSERT3U(*size, >=, from_delta); 10614309Smaybee atomic_add_64(size, -from_delta); 10621544Seschrock 10631544Seschrock if (use_mutex) 10643403Sbmc mutex_exit(&old_state->arcs_mtx); 1065789Sahrens } 10663403Sbmc if (new_state != arc_anon) { 10673403Sbmc int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx); 10684309Smaybee uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1069789Sahrens 10701544Seschrock if (use_mutex) 10713403Sbmc mutex_enter(&new_state->arcs_mtx); 10721544Seschrock 10734309Smaybee list_insert_head(&new_state->arcs_list[ab->b_type], ab); 10741544Seschrock 10751544Seschrock /* ghost elements have a ghost size */ 10761544Seschrock if (GHOST_STATE(new_state)) { 10771544Seschrock ASSERT(ab->b_datacnt == 0); 10781544Seschrock ASSERT(ab->b_buf == NULL); 10791544Seschrock to_delta = ab->b_size; 10801544Seschrock } 10814309Smaybee atomic_add_64(size, to_delta); 10821544Seschrock 10831544Seschrock if (use_mutex) 10843403Sbmc mutex_exit(&new_state->arcs_mtx); 1085789Sahrens } 1086789Sahrens } 1087789Sahrens 1088789Sahrens ASSERT(!BUF_EMPTY(ab)); 10895450Sbrendan if (new_state == arc_anon) { 1090789Sahrens buf_hash_remove(ab); 1091789Sahrens } 1092789Sahrens 10931544Seschrock /* adjust state sizes */ 10941544Seschrock if (to_delta) 10953403Sbmc atomic_add_64(&new_state->arcs_size, to_delta); 10961544Seschrock if (from_delta) { 10973403Sbmc ASSERT3U(old_state->arcs_size, >=, from_delta); 10983403Sbmc atomic_add_64(&old_state->arcs_size, -from_delta); 1099789Sahrens } 1100789Sahrens ab->b_state = new_state; 11015450Sbrendan 11025450Sbrendan /* adjust l2arc hdr stats */ 11035450Sbrendan if (new_state == arc_l2c_only) 11045450Sbrendan l2arc_hdr_stat_add(); 11055450Sbrendan else if (old_state == arc_l2c_only) 11065450Sbrendan l2arc_hdr_stat_remove(); 1107789Sahrens } 1108789Sahrens 11094309Smaybee void 11108582SBrendan.Gregg@Sun.COM arc_space_consume(uint64_t space, arc_space_type_t type) 11114309Smaybee { 11128582SBrendan.Gregg@Sun.COM ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11138582SBrendan.Gregg@Sun.COM 11148582SBrendan.Gregg@Sun.COM switch (type) { 11158582SBrendan.Gregg@Sun.COM case ARC_SPACE_DATA: 11168582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, space); 11178582SBrendan.Gregg@Sun.COM break; 11188582SBrendan.Gregg@Sun.COM case ARC_SPACE_OTHER: 11198582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_other_size, space); 11208582SBrendan.Gregg@Sun.COM break; 11218582SBrendan.Gregg@Sun.COM case ARC_SPACE_HDRS: 11228582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_hdr_size, space); 11238582SBrendan.Gregg@Sun.COM break; 11248582SBrendan.Gregg@Sun.COM case ARC_SPACE_L2HDRS: 11258582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_hdr_size, space); 11268582SBrendan.Gregg@Sun.COM break; 11278582SBrendan.Gregg@Sun.COM } 11288582SBrendan.Gregg@Sun.COM 11294309Smaybee atomic_add_64(&arc_meta_used, space); 11304309Smaybee atomic_add_64(&arc_size, space); 11314309Smaybee } 11324309Smaybee 11334309Smaybee void 11348582SBrendan.Gregg@Sun.COM arc_space_return(uint64_t space, arc_space_type_t type) 11354309Smaybee { 11368582SBrendan.Gregg@Sun.COM ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11378582SBrendan.Gregg@Sun.COM 11388582SBrendan.Gregg@Sun.COM switch (type) { 11398582SBrendan.Gregg@Sun.COM case ARC_SPACE_DATA: 11408582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -space); 11418582SBrendan.Gregg@Sun.COM break; 11428582SBrendan.Gregg@Sun.COM case ARC_SPACE_OTHER: 11438582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_other_size, -space); 11448582SBrendan.Gregg@Sun.COM break; 11458582SBrendan.Gregg@Sun.COM case ARC_SPACE_HDRS: 11468582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_hdr_size, -space); 11478582SBrendan.Gregg@Sun.COM break; 11488582SBrendan.Gregg@Sun.COM case ARC_SPACE_L2HDRS: 11498582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 11508582SBrendan.Gregg@Sun.COM break; 11518582SBrendan.Gregg@Sun.COM } 11528582SBrendan.Gregg@Sun.COM 11534309Smaybee ASSERT(arc_meta_used >= space); 11544309Smaybee if (arc_meta_max < arc_meta_used) 11554309Smaybee arc_meta_max = arc_meta_used; 11564309Smaybee atomic_add_64(&arc_meta_used, -space); 11574309Smaybee ASSERT(arc_size >= space); 11584309Smaybee atomic_add_64(&arc_size, -space); 11594309Smaybee } 11604309Smaybee 11614309Smaybee void * 11624309Smaybee arc_data_buf_alloc(uint64_t size) 11634309Smaybee { 11644309Smaybee if (arc_evict_needed(ARC_BUFC_DATA)) 11654309Smaybee cv_signal(&arc_reclaim_thr_cv); 11664309Smaybee atomic_add_64(&arc_size, size); 11674309Smaybee return (zio_data_buf_alloc(size)); 11684309Smaybee } 11694309Smaybee 11704309Smaybee void 11714309Smaybee arc_data_buf_free(void *buf, uint64_t size) 11724309Smaybee { 11734309Smaybee zio_data_buf_free(buf, size); 11744309Smaybee ASSERT(arc_size >= size); 11754309Smaybee atomic_add_64(&arc_size, -size); 11764309Smaybee } 11774309Smaybee 1178789Sahrens arc_buf_t * 11793290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1180789Sahrens { 1181789Sahrens arc_buf_hdr_t *hdr; 1182789Sahrens arc_buf_t *buf; 1183789Sahrens 1184789Sahrens ASSERT3U(size, >, 0); 11856245Smaybee hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1186789Sahrens ASSERT(BUF_EMPTY(hdr)); 1187789Sahrens hdr->b_size = size; 11883290Sjohansen hdr->b_type = type; 11898636SMark.Maybee@Sun.COM hdr->b_spa = spa_guid(spa); 11903403Sbmc hdr->b_state = arc_anon; 1191789Sahrens hdr->b_arc_access = 0; 11926245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1193789Sahrens buf->b_hdr = hdr; 11942688Smaybee buf->b_data = NULL; 11951544Seschrock buf->b_efunc = NULL; 11961544Seschrock buf->b_private = NULL; 1197789Sahrens buf->b_next = NULL; 1198789Sahrens hdr->b_buf = buf; 11992688Smaybee arc_get_data_buf(buf); 12001544Seschrock hdr->b_datacnt = 1; 1201789Sahrens hdr->b_flags = 0; 1202789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1203789Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 1204789Sahrens 1205789Sahrens return (buf); 1206789Sahrens } 1207789Sahrens 12089412SAleksandr.Guzovskiy@Sun.COM static char *arc_onloan_tag = "onloan"; 12099412SAleksandr.Guzovskiy@Sun.COM 12109412SAleksandr.Guzovskiy@Sun.COM /* 12119412SAleksandr.Guzovskiy@Sun.COM * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 12129412SAleksandr.Guzovskiy@Sun.COM * flight data by arc_tempreserve_space() until they are "returned". Loaned 12139412SAleksandr.Guzovskiy@Sun.COM * buffers must be returned to the arc before they can be used by the DMU or 12149412SAleksandr.Guzovskiy@Sun.COM * freed. 12159412SAleksandr.Guzovskiy@Sun.COM */ 12169412SAleksandr.Guzovskiy@Sun.COM arc_buf_t * 12179412SAleksandr.Guzovskiy@Sun.COM arc_loan_buf(spa_t *spa, int size) 12189412SAleksandr.Guzovskiy@Sun.COM { 12199412SAleksandr.Guzovskiy@Sun.COM arc_buf_t *buf; 12209412SAleksandr.Guzovskiy@Sun.COM 12219412SAleksandr.Guzovskiy@Sun.COM buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 12229412SAleksandr.Guzovskiy@Sun.COM 12239412SAleksandr.Guzovskiy@Sun.COM atomic_add_64(&arc_loaned_bytes, size); 12249412SAleksandr.Guzovskiy@Sun.COM return (buf); 12259412SAleksandr.Guzovskiy@Sun.COM } 12269412SAleksandr.Guzovskiy@Sun.COM 12279412SAleksandr.Guzovskiy@Sun.COM /* 12289412SAleksandr.Guzovskiy@Sun.COM * Return a loaned arc buffer to the arc. 12299412SAleksandr.Guzovskiy@Sun.COM */ 12309412SAleksandr.Guzovskiy@Sun.COM void 12319412SAleksandr.Guzovskiy@Sun.COM arc_return_buf(arc_buf_t *buf, void *tag) 12329412SAleksandr.Guzovskiy@Sun.COM { 12339412SAleksandr.Guzovskiy@Sun.COM arc_buf_hdr_t *hdr = buf->b_hdr; 12349412SAleksandr.Guzovskiy@Sun.COM 12359412SAleksandr.Guzovskiy@Sun.COM ASSERT(hdr->b_state == arc_anon); 12369412SAleksandr.Guzovskiy@Sun.COM ASSERT(buf->b_data != NULL); 12379412SAleksandr.Guzovskiy@Sun.COM VERIFY(refcount_remove(&hdr->b_refcnt, arc_onloan_tag) == 0); 12389412SAleksandr.Guzovskiy@Sun.COM VERIFY(refcount_add(&hdr->b_refcnt, tag) == 1); 12399412SAleksandr.Guzovskiy@Sun.COM 12409412SAleksandr.Guzovskiy@Sun.COM atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 12419412SAleksandr.Guzovskiy@Sun.COM } 12429412SAleksandr.Guzovskiy@Sun.COM 12432688Smaybee static arc_buf_t * 12442688Smaybee arc_buf_clone(arc_buf_t *from) 12451544Seschrock { 12462688Smaybee arc_buf_t *buf; 12472688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 12482688Smaybee uint64_t size = hdr->b_size; 12491544Seschrock 12506245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 12512688Smaybee buf->b_hdr = hdr; 12522688Smaybee buf->b_data = NULL; 12532688Smaybee buf->b_efunc = NULL; 12542688Smaybee buf->b_private = NULL; 12552688Smaybee buf->b_next = hdr->b_buf; 12562688Smaybee hdr->b_buf = buf; 12572688Smaybee arc_get_data_buf(buf); 12582688Smaybee bcopy(from->b_data, buf->b_data, size); 12592688Smaybee hdr->b_datacnt += 1; 12602688Smaybee return (buf); 12611544Seschrock } 12621544Seschrock 12631544Seschrock void 12641544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 12651544Seschrock { 12662887Smaybee arc_buf_hdr_t *hdr; 12671544Seschrock kmutex_t *hash_lock; 12681544Seschrock 12692724Smaybee /* 12707545SMark.Maybee@Sun.COM * Check to see if this buffer is evicted. Callers 12717545SMark.Maybee@Sun.COM * must verify b_data != NULL to know if the add_ref 12727545SMark.Maybee@Sun.COM * was successful. 12732724Smaybee */ 12747545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 12757545SMark.Maybee@Sun.COM if (buf->b_data == NULL) { 12767545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 12772724Smaybee return; 12782887Smaybee } 12797545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 12807545SMark.Maybee@Sun.COM ASSERT(hdr != NULL); 12812887Smaybee hash_lock = HDR_LOCK(hdr); 12822724Smaybee mutex_enter(hash_lock); 12837545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 12847545SMark.Maybee@Sun.COM 12853403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 12861544Seschrock add_reference(hdr, hash_lock, tag); 12878582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 12882688Smaybee arc_access(hdr, hash_lock); 12892688Smaybee mutex_exit(hash_lock); 12903403Sbmc ARCSTAT_BUMP(arcstat_hits); 12913403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 12923403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 12933403Sbmc data, metadata, hits); 12941544Seschrock } 12951544Seschrock 12965450Sbrendan /* 12975450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 12985450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 12995450Sbrendan */ 13005450Sbrendan static void 13015450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 13025450Sbrendan void *data, size_t size) 13035450Sbrendan { 13045450Sbrendan if (HDR_L2_WRITING(hdr)) { 13055450Sbrendan l2arc_data_free_t *df; 13065450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 13075450Sbrendan df->l2df_data = data; 13085450Sbrendan df->l2df_size = size; 13095450Sbrendan df->l2df_func = free_func; 13105450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 13115450Sbrendan list_insert_head(l2arc_free_on_write, df); 13125450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 13135450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 13145450Sbrendan } else { 13155450Sbrendan free_func(data, size); 13165450Sbrendan } 13175450Sbrendan } 13185450Sbrendan 1319789Sahrens static void 13202688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 13211544Seschrock { 13221544Seschrock arc_buf_t **bufp; 13231544Seschrock 13241544Seschrock /* free up data associated with the buf */ 13251544Seschrock if (buf->b_data) { 13261544Seschrock arc_state_t *state = buf->b_hdr->b_state; 13271544Seschrock uint64_t size = buf->b_hdr->b_size; 13283290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 13291544Seschrock 13303093Sahrens arc_cksum_verify(buf); 13312688Smaybee if (!recycle) { 13323290Sjohansen if (type == ARC_BUFC_METADATA) { 13335450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 13345450Sbrendan buf->b_data, size); 13358582SBrendan.Gregg@Sun.COM arc_space_return(size, ARC_SPACE_DATA); 13363290Sjohansen } else { 13373290Sjohansen ASSERT(type == ARC_BUFC_DATA); 13385450Sbrendan arc_buf_data_free(buf->b_hdr, 13395450Sbrendan zio_data_buf_free, buf->b_data, size); 13408582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -size); 13414309Smaybee atomic_add_64(&arc_size, -size); 13423290Sjohansen } 13432688Smaybee } 13441544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 13454309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 13464309Smaybee 13471544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 13483403Sbmc ASSERT(state != arc_anon); 13494309Smaybee 13504309Smaybee ASSERT3U(*cnt, >=, size); 13514309Smaybee atomic_add_64(cnt, -size); 13521544Seschrock } 13533403Sbmc ASSERT3U(state->arcs_size, >=, size); 13543403Sbmc atomic_add_64(&state->arcs_size, -size); 13551544Seschrock buf->b_data = NULL; 13561544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 13571544Seschrock buf->b_hdr->b_datacnt -= 1; 13581544Seschrock } 13591544Seschrock 13601544Seschrock /* only remove the buf if requested */ 13611544Seschrock if (!all) 13621544Seschrock return; 13631544Seschrock 13641544Seschrock /* remove the buf from the hdr list */ 13651544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 13661544Seschrock continue; 13671544Seschrock *bufp = buf->b_next; 13681544Seschrock 13691544Seschrock ASSERT(buf->b_efunc == NULL); 13701544Seschrock 13711544Seschrock /* clean up the buf */ 13721544Seschrock buf->b_hdr = NULL; 13731544Seschrock kmem_cache_free(buf_cache, buf); 13741544Seschrock } 13751544Seschrock 13761544Seschrock static void 13771544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1378789Sahrens { 1379789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 13803403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 13811544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 13827046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 1383789Sahrens 13845450Sbrendan if (hdr->b_l2hdr != NULL) { 13855450Sbrendan if (!MUTEX_HELD(&l2arc_buflist_mtx)) { 13865450Sbrendan /* 13875450Sbrendan * To prevent arc_free() and l2arc_evict() from 13885450Sbrendan * attempting to free the same buffer at the same time, 13895450Sbrendan * a FREE_IN_PROGRESS flag is given to arc_free() to 13905450Sbrendan * give it priority. l2arc_evict() can't destroy this 13915450Sbrendan * header while we are waiting on l2arc_buflist_mtx. 13927361SBrendan.Gregg@Sun.COM * 13937361SBrendan.Gregg@Sun.COM * The hdr may be removed from l2ad_buflist before we 13947361SBrendan.Gregg@Sun.COM * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 13955450Sbrendan */ 13965450Sbrendan mutex_enter(&l2arc_buflist_mtx); 13977361SBrendan.Gregg@Sun.COM if (hdr->b_l2hdr != NULL) { 13987361SBrendan.Gregg@Sun.COM list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, 13997361SBrendan.Gregg@Sun.COM hdr); 14007361SBrendan.Gregg@Sun.COM } 14015450Sbrendan mutex_exit(&l2arc_buflist_mtx); 14025450Sbrendan } else { 14035450Sbrendan list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr); 14045450Sbrendan } 14055450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 14065450Sbrendan kmem_free(hdr->b_l2hdr, sizeof (l2arc_buf_hdr_t)); 14075450Sbrendan if (hdr->b_state == arc_l2c_only) 14085450Sbrendan l2arc_hdr_stat_remove(); 14095450Sbrendan hdr->b_l2hdr = NULL; 14105450Sbrendan } 14115450Sbrendan 1412789Sahrens if (!BUF_EMPTY(hdr)) { 14131544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1414789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1415789Sahrens hdr->b_birth = 0; 1416789Sahrens hdr->b_cksum0 = 0; 1417789Sahrens } 14181544Seschrock while (hdr->b_buf) { 1419789Sahrens arc_buf_t *buf = hdr->b_buf; 1420789Sahrens 14211544Seschrock if (buf->b_efunc) { 14221544Seschrock mutex_enter(&arc_eviction_mtx); 14237545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 14241544Seschrock ASSERT(buf->b_hdr != NULL); 14252688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 14261544Seschrock hdr->b_buf = buf->b_next; 14272887Smaybee buf->b_hdr = &arc_eviction_hdr; 14281544Seschrock buf->b_next = arc_eviction_list; 14291544Seschrock arc_eviction_list = buf; 14307545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 14311544Seschrock mutex_exit(&arc_eviction_mtx); 14321544Seschrock } else { 14332688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 14341544Seschrock } 1435789Sahrens } 14363093Sahrens if (hdr->b_freeze_cksum != NULL) { 14373093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 14383093Sahrens hdr->b_freeze_cksum = NULL; 14393093Sahrens } 14401544Seschrock 1441789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1442789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1443789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1444789Sahrens kmem_cache_free(hdr_cache, hdr); 1445789Sahrens } 1446789Sahrens 1447789Sahrens void 1448789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1449789Sahrens { 1450789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 14513403Sbmc int hashed = hdr->b_state != arc_anon; 14521544Seschrock 14531544Seschrock ASSERT(buf->b_efunc == NULL); 14541544Seschrock ASSERT(buf->b_data != NULL); 14551544Seschrock 14561544Seschrock if (hashed) { 14571544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 14581544Seschrock 14591544Seschrock mutex_enter(hash_lock); 14601544Seschrock (void) remove_reference(hdr, hash_lock, tag); 14611544Seschrock if (hdr->b_datacnt > 1) 14622688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14631544Seschrock else 14641544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 14651544Seschrock mutex_exit(hash_lock); 14661544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 14671544Seschrock int destroy_hdr; 14681544Seschrock /* 14691544Seschrock * We are in the middle of an async write. Don't destroy 14701544Seschrock * this buffer unless the write completes before we finish 14711544Seschrock * decrementing the reference count. 14721544Seschrock */ 14731544Seschrock mutex_enter(&arc_eviction_mtx); 14741544Seschrock (void) remove_reference(hdr, NULL, tag); 14751544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 14761544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 14771544Seschrock mutex_exit(&arc_eviction_mtx); 14781544Seschrock if (destroy_hdr) 14791544Seschrock arc_hdr_destroy(hdr); 14801544Seschrock } else { 14811544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 14821544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 14832688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14841544Seschrock } else { 14851544Seschrock arc_hdr_destroy(hdr); 14861544Seschrock } 14871544Seschrock } 14881544Seschrock } 14891544Seschrock 14901544Seschrock int 14911544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 14921544Seschrock { 14931544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1494789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 14951544Seschrock int no_callback = (buf->b_efunc == NULL); 14961544Seschrock 14973403Sbmc if (hdr->b_state == arc_anon) { 14981544Seschrock arc_buf_free(buf, tag); 14991544Seschrock return (no_callback); 15001544Seschrock } 1501789Sahrens 1502789Sahrens mutex_enter(hash_lock); 15033403Sbmc ASSERT(hdr->b_state != arc_anon); 15041544Seschrock ASSERT(buf->b_data != NULL); 1505789Sahrens 15061544Seschrock (void) remove_reference(hdr, hash_lock, tag); 15071544Seschrock if (hdr->b_datacnt > 1) { 15081544Seschrock if (no_callback) 15092688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 15101544Seschrock } else if (no_callback) { 15111544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 15121544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1513789Sahrens } 15141544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 15151544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1516789Sahrens mutex_exit(hash_lock); 15171544Seschrock return (no_callback); 1518789Sahrens } 1519789Sahrens 1520789Sahrens int 1521789Sahrens arc_buf_size(arc_buf_t *buf) 1522789Sahrens { 1523789Sahrens return (buf->b_hdr->b_size); 1524789Sahrens } 1525789Sahrens 1526789Sahrens /* 1527789Sahrens * Evict buffers from list until we've removed the specified number of 1528789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 15292688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 15302688Smaybee * - look for a buffer to evict that is `bytes' long. 15312688Smaybee * - return the data block from this buffer rather than freeing it. 15322688Smaybee * This flag is used by callers that are trying to make space for a 15332688Smaybee * new buffer in a full arc cache. 15345642Smaybee * 15355642Smaybee * This function makes a "best effort". It skips over any buffers 15365642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 15375642Smaybee * It may also return without evicting as much space as requested. 1538789Sahrens */ 15392688Smaybee static void * 15408636SMark.Maybee@Sun.COM arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 15413290Sjohansen arc_buf_contents_t type) 1542789Sahrens { 1543789Sahrens arc_state_t *evicted_state; 15442688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 15452918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 15464309Smaybee list_t *list = &state->arcs_list[type]; 1547789Sahrens kmutex_t *hash_lock; 15482688Smaybee boolean_t have_lock; 15492918Smaybee void *stolen = NULL; 1550789Sahrens 15513403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1552789Sahrens 15533403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1554789Sahrens 15553403Sbmc mutex_enter(&state->arcs_mtx); 15563403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1557789Sahrens 15584309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15594309Smaybee ab_prev = list_prev(list, ab); 15602391Smaybee /* prefetch buffers have a minimum lifespan */ 15612688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 15625642Smaybee (spa && ab->b_spa != spa) || 15632688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 15642688Smaybee lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) { 15652391Smaybee skipped++; 15662391Smaybee continue; 15672391Smaybee } 15682918Smaybee /* "lookahead" for better eviction candidate */ 15692918Smaybee if (recycle && ab->b_size != bytes && 15702918Smaybee ab_prev && ab_prev->b_size == bytes) 15712688Smaybee continue; 1572789Sahrens hash_lock = HDR_LOCK(ab); 15732688Smaybee have_lock = MUTEX_HELD(hash_lock); 15742688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1575789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 15761544Seschrock ASSERT(ab->b_datacnt > 0); 15771544Seschrock while (ab->b_buf) { 15781544Seschrock arc_buf_t *buf = ab->b_buf; 15797545SMark.Maybee@Sun.COM if (!rw_tryenter(&buf->b_lock, RW_WRITER)) { 15807545SMark.Maybee@Sun.COM missed += 1; 15817545SMark.Maybee@Sun.COM break; 15827545SMark.Maybee@Sun.COM } 15832688Smaybee if (buf->b_data) { 15841544Seschrock bytes_evicted += ab->b_size; 15853290Sjohansen if (recycle && ab->b_type == type && 15865450Sbrendan ab->b_size == bytes && 15875450Sbrendan !HDR_L2_WRITING(ab)) { 15882918Smaybee stolen = buf->b_data; 15892918Smaybee recycle = FALSE; 15902918Smaybee } 15912688Smaybee } 15921544Seschrock if (buf->b_efunc) { 15931544Seschrock mutex_enter(&arc_eviction_mtx); 15942918Smaybee arc_buf_destroy(buf, 15952918Smaybee buf->b_data == stolen, FALSE); 15961544Seschrock ab->b_buf = buf->b_next; 15972887Smaybee buf->b_hdr = &arc_eviction_hdr; 15981544Seschrock buf->b_next = arc_eviction_list; 15991544Seschrock arc_eviction_list = buf; 16001544Seschrock mutex_exit(&arc_eviction_mtx); 16017545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 16021544Seschrock } else { 16037545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 16042918Smaybee arc_buf_destroy(buf, 16052918Smaybee buf->b_data == stolen, TRUE); 16061544Seschrock } 16071544Seschrock } 16087545SMark.Maybee@Sun.COM if (ab->b_datacnt == 0) { 16097545SMark.Maybee@Sun.COM arc_change_state(evicted_state, ab, hash_lock); 16107545SMark.Maybee@Sun.COM ASSERT(HDR_IN_HASH_TABLE(ab)); 16117545SMark.Maybee@Sun.COM ab->b_flags |= ARC_IN_HASH_TABLE; 16127545SMark.Maybee@Sun.COM ab->b_flags &= ~ARC_BUF_AVAILABLE; 16137545SMark.Maybee@Sun.COM DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 16147545SMark.Maybee@Sun.COM } 16152688Smaybee if (!have_lock) 16162688Smaybee mutex_exit(hash_lock); 16171544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1618789Sahrens break; 1619789Sahrens } else { 16202688Smaybee missed += 1; 1621789Sahrens } 1622789Sahrens } 16233403Sbmc 16243403Sbmc mutex_exit(&evicted_state->arcs_mtx); 16253403Sbmc mutex_exit(&state->arcs_mtx); 1626789Sahrens 1627789Sahrens if (bytes_evicted < bytes) 1628789Sahrens dprintf("only evicted %lld bytes from %x", 1629789Sahrens (longlong_t)bytes_evicted, state); 1630789Sahrens 16312688Smaybee if (skipped) 16323403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 16333403Sbmc 16342688Smaybee if (missed) 16353403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 16363403Sbmc 16374709Smaybee /* 16384709Smaybee * We have just evicted some date into the ghost state, make 16394709Smaybee * sure we also adjust the ghost state size if necessary. 16404709Smaybee */ 16414709Smaybee if (arc_no_grow && 16424709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 16434709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 16444709Smaybee arc_mru_ghost->arcs_size - arc_c; 16454709Smaybee 16464709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 16474709Smaybee int64_t todelete = 16484709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 16495642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 16504709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 16514709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 16524709Smaybee arc_mru_ghost->arcs_size + 16534709Smaybee arc_mfu_ghost->arcs_size - arc_c); 16545642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 16554709Smaybee } 16564709Smaybee } 16574709Smaybee 16582918Smaybee return (stolen); 1659789Sahrens } 1660789Sahrens 1661789Sahrens /* 1662789Sahrens * Remove buffers from list until we've removed the specified number of 1663789Sahrens * bytes. Destroy the buffers that are removed. 1664789Sahrens */ 1665789Sahrens static void 16668636SMark.Maybee@Sun.COM arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 1667789Sahrens { 1668789Sahrens arc_buf_hdr_t *ab, *ab_prev; 16694309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1670789Sahrens kmutex_t *hash_lock; 16711544Seschrock uint64_t bytes_deleted = 0; 16723700Sek110237 uint64_t bufs_skipped = 0; 1673789Sahrens 16741544Seschrock ASSERT(GHOST_STATE(state)); 1675789Sahrens top: 16763403Sbmc mutex_enter(&state->arcs_mtx); 16774309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 16784309Smaybee ab_prev = list_prev(list, ab); 16795642Smaybee if (spa && ab->b_spa != spa) 16805642Smaybee continue; 1681789Sahrens hash_lock = HDR_LOCK(ab); 1682789Sahrens if (mutex_tryenter(hash_lock)) { 16832391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 16841544Seschrock ASSERT(ab->b_buf == NULL); 16853403Sbmc ARCSTAT_BUMP(arcstat_deleted); 16861544Seschrock bytes_deleted += ab->b_size; 16875450Sbrendan 16885450Sbrendan if (ab->b_l2hdr != NULL) { 16895450Sbrendan /* 16905450Sbrendan * This buffer is cached on the 2nd Level ARC; 16915450Sbrendan * don't destroy the header. 16925450Sbrendan */ 16935450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 16945450Sbrendan mutex_exit(hash_lock); 16955450Sbrendan } else { 16965450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 16975450Sbrendan mutex_exit(hash_lock); 16985450Sbrendan arc_hdr_destroy(ab); 16995450Sbrendan } 17005450Sbrendan 1701789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1702789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1703789Sahrens break; 1704789Sahrens } else { 1705789Sahrens if (bytes < 0) { 17063403Sbmc mutex_exit(&state->arcs_mtx); 1707789Sahrens mutex_enter(hash_lock); 1708789Sahrens mutex_exit(hash_lock); 1709789Sahrens goto top; 1710789Sahrens } 1711789Sahrens bufs_skipped += 1; 1712789Sahrens } 1713789Sahrens } 17143403Sbmc mutex_exit(&state->arcs_mtx); 1715789Sahrens 17164309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 17174309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 17184309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 17194309Smaybee goto top; 17204309Smaybee } 17214309Smaybee 1722789Sahrens if (bufs_skipped) { 17233403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1724789Sahrens ASSERT(bytes >= 0); 1725789Sahrens } 1726789Sahrens 1727789Sahrens if (bytes_deleted < bytes) 1728789Sahrens dprintf("only deleted %lld bytes from %p", 1729789Sahrens (longlong_t)bytes_deleted, state); 1730789Sahrens } 1731789Sahrens 1732789Sahrens static void 1733789Sahrens arc_adjust(void) 1734789Sahrens { 17358582SBrendan.Gregg@Sun.COM int64_t adjustment, delta; 17368582SBrendan.Gregg@Sun.COM 17378582SBrendan.Gregg@Sun.COM /* 17388582SBrendan.Gregg@Sun.COM * Adjust MRU size 17398582SBrendan.Gregg@Sun.COM */ 17408582SBrendan.Gregg@Sun.COM 17418582SBrendan.Gregg@Sun.COM adjustment = MIN(arc_size - arc_c, 17428582SBrendan.Gregg@Sun.COM arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - arc_p); 17438582SBrendan.Gregg@Sun.COM 17448582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 17458582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 17468582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA); 17478582SBrendan.Gregg@Sun.COM adjustment -= delta; 17484309Smaybee } 17494309Smaybee 17508582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 17518582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 17528582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, 17535642Smaybee ARC_BUFC_METADATA); 1754789Sahrens } 1755789Sahrens 17568582SBrendan.Gregg@Sun.COM /* 17578582SBrendan.Gregg@Sun.COM * Adjust MFU size 17588582SBrendan.Gregg@Sun.COM */ 17598582SBrendan.Gregg@Sun.COM 17608582SBrendan.Gregg@Sun.COM adjustment = arc_size - arc_c; 17618582SBrendan.Gregg@Sun.COM 17628582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 17638582SBrendan.Gregg@Sun.COM delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 17648582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA); 17658582SBrendan.Gregg@Sun.COM adjustment -= delta; 17668582SBrendan.Gregg@Sun.COM } 17678582SBrendan.Gregg@Sun.COM 17688582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 17698582SBrendan.Gregg@Sun.COM int64_t delta = MIN(adjustment, 17708582SBrendan.Gregg@Sun.COM arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 17718582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, 17728582SBrendan.Gregg@Sun.COM ARC_BUFC_METADATA); 17738582SBrendan.Gregg@Sun.COM } 17748582SBrendan.Gregg@Sun.COM 17758582SBrendan.Gregg@Sun.COM /* 17768582SBrendan.Gregg@Sun.COM * Adjust ghost lists 17778582SBrendan.Gregg@Sun.COM */ 17788582SBrendan.Gregg@Sun.COM 17798582SBrendan.Gregg@Sun.COM adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 17808582SBrendan.Gregg@Sun.COM 17818582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 17828582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru_ghost->arcs_size, adjustment); 17838582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mru_ghost, NULL, delta); 17848582SBrendan.Gregg@Sun.COM } 17858582SBrendan.Gregg@Sun.COM 17868582SBrendan.Gregg@Sun.COM adjustment = 17878582SBrendan.Gregg@Sun.COM arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 17888582SBrendan.Gregg@Sun.COM 17898582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 17908582SBrendan.Gregg@Sun.COM delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 17918582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mfu_ghost, NULL, delta); 1792789Sahrens } 1793789Sahrens } 1794789Sahrens 17951544Seschrock static void 17961544Seschrock arc_do_user_evicts(void) 17971544Seschrock { 17981544Seschrock mutex_enter(&arc_eviction_mtx); 17991544Seschrock while (arc_eviction_list != NULL) { 18001544Seschrock arc_buf_t *buf = arc_eviction_list; 18011544Seschrock arc_eviction_list = buf->b_next; 18027545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 18031544Seschrock buf->b_hdr = NULL; 18047545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 18051544Seschrock mutex_exit(&arc_eviction_mtx); 18061544Seschrock 18071819Smaybee if (buf->b_efunc != NULL) 18081819Smaybee VERIFY(buf->b_efunc(buf) == 0); 18091544Seschrock 18101544Seschrock buf->b_efunc = NULL; 18111544Seschrock buf->b_private = NULL; 18121544Seschrock kmem_cache_free(buf_cache, buf); 18131544Seschrock mutex_enter(&arc_eviction_mtx); 18141544Seschrock } 18151544Seschrock mutex_exit(&arc_eviction_mtx); 18161544Seschrock } 18171544Seschrock 1818789Sahrens /* 18195642Smaybee * Flush all *evictable* data from the cache for the given spa. 1820789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1821789Sahrens */ 1822789Sahrens void 18235642Smaybee arc_flush(spa_t *spa) 1824789Sahrens { 18258636SMark.Maybee@Sun.COM uint64_t guid = 0; 18268636SMark.Maybee@Sun.COM 18278636SMark.Maybee@Sun.COM if (spa) 18288636SMark.Maybee@Sun.COM guid = spa_guid(spa); 18298636SMark.Maybee@Sun.COM 18305642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 18318636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 18325642Smaybee if (spa) 18335642Smaybee break; 18345642Smaybee } 18355642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 18368636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 18375642Smaybee if (spa) 18385642Smaybee break; 18395642Smaybee } 18405642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 18418636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 18425642Smaybee if (spa) 18435642Smaybee break; 18445642Smaybee } 18455642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 18468636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 18475642Smaybee if (spa) 18485642Smaybee break; 18495642Smaybee } 18505642Smaybee 18518636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mru_ghost, guid, -1); 18528636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mfu_ghost, guid, -1); 18531544Seschrock 18541544Seschrock mutex_enter(&arc_reclaim_thr_lock); 18551544Seschrock arc_do_user_evicts(); 18561544Seschrock mutex_exit(&arc_reclaim_thr_lock); 18575642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1858789Sahrens } 1859789Sahrens 1860789Sahrens void 18613158Smaybee arc_shrink(void) 1862789Sahrens { 18633403Sbmc if (arc_c > arc_c_min) { 18643158Smaybee uint64_t to_free; 1865789Sahrens 18662048Sstans #ifdef _KERNEL 18673403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 18682048Sstans #else 18693403Sbmc to_free = arc_c >> arc_shrink_shift; 18702048Sstans #endif 18713403Sbmc if (arc_c > arc_c_min + to_free) 18723403Sbmc atomic_add_64(&arc_c, -to_free); 18733158Smaybee else 18743403Sbmc arc_c = arc_c_min; 18752048Sstans 18763403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 18773403Sbmc if (arc_c > arc_size) 18783403Sbmc arc_c = MAX(arc_size, arc_c_min); 18793403Sbmc if (arc_p > arc_c) 18803403Sbmc arc_p = (arc_c >> 1); 18813403Sbmc ASSERT(arc_c >= arc_c_min); 18823403Sbmc ASSERT((int64_t)arc_p >= 0); 18833158Smaybee } 1884789Sahrens 18853403Sbmc if (arc_size > arc_c) 18863158Smaybee arc_adjust(); 1887789Sahrens } 1888789Sahrens 1889789Sahrens static int 1890789Sahrens arc_reclaim_needed(void) 1891789Sahrens { 1892789Sahrens uint64_t extra; 1893789Sahrens 1894789Sahrens #ifdef _KERNEL 18952048Sstans 18962048Sstans if (needfree) 18972048Sstans return (1); 18982048Sstans 1899789Sahrens /* 1900789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1901789Sahrens */ 1902789Sahrens extra = desfree; 1903789Sahrens 1904789Sahrens /* 1905789Sahrens * check that we're out of range of the pageout scanner. It starts to 1906789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1907789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1908789Sahrens * number of needed free pages. We add extra pages here to make sure 1909789Sahrens * the scanner doesn't start up while we're freeing memory. 1910789Sahrens */ 1911789Sahrens if (freemem < lotsfree + needfree + extra) 1912789Sahrens return (1); 1913789Sahrens 1914789Sahrens /* 1915789Sahrens * check to make sure that swapfs has enough space so that anon 19165450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1917789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1918789Sahrens * swap pages. We also add a bit of extra here just to prevent 1919789Sahrens * circumstances from getting really dire. 1920789Sahrens */ 1921789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1922789Sahrens return (1); 1923789Sahrens 19241936Smaybee #if defined(__i386) 1925789Sahrens /* 1926789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1927789Sahrens * kernel heap space before we ever run out of available physical 1928789Sahrens * memory. Most checks of the size of the heap_area compare against 1929789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1930789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1931789Sahrens * which is so low that it's useless. In this comparison, we seek to 1932789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 19335450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1934789Sahrens * free) 1935789Sahrens */ 1936789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1937789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1938789Sahrens return (1); 1939789Sahrens #endif 1940789Sahrens 1941789Sahrens #else 1942789Sahrens if (spa_get_random(100) == 0) 1943789Sahrens return (1); 1944789Sahrens #endif 1945789Sahrens return (0); 1946789Sahrens } 1947789Sahrens 1948789Sahrens static void 1949789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1950789Sahrens { 1951789Sahrens size_t i; 1952789Sahrens kmem_cache_t *prev_cache = NULL; 19533290Sjohansen kmem_cache_t *prev_data_cache = NULL; 1954789Sahrens extern kmem_cache_t *zio_buf_cache[]; 19553290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 1956789Sahrens 19571484Sek110237 #ifdef _KERNEL 19584309Smaybee if (arc_meta_used >= arc_meta_limit) { 19594309Smaybee /* 19604309Smaybee * We are exceeding our meta-data cache limit. 19614309Smaybee * Purge some DNLC entries to release holds on meta-data. 19624309Smaybee */ 19634309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 19644309Smaybee } 19651936Smaybee #if defined(__i386) 19661936Smaybee /* 19671936Smaybee * Reclaim unused memory from all kmem caches. 19681936Smaybee */ 19691936Smaybee kmem_reap(); 19701936Smaybee #endif 19711484Sek110237 #endif 19721484Sek110237 1973789Sahrens /* 19745450Sbrendan * An aggressive reclamation will shrink the cache size as well as 19751544Seschrock * reap free buffers from the arc kmem caches. 1976789Sahrens */ 1977789Sahrens if (strat == ARC_RECLAIM_AGGR) 19783158Smaybee arc_shrink(); 1979789Sahrens 1980789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 1981789Sahrens if (zio_buf_cache[i] != prev_cache) { 1982789Sahrens prev_cache = zio_buf_cache[i]; 1983789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 1984789Sahrens } 19853290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 19863290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 19873290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 19883290Sjohansen } 1989789Sahrens } 19901544Seschrock kmem_cache_reap_now(buf_cache); 19911544Seschrock kmem_cache_reap_now(hdr_cache); 1992789Sahrens } 1993789Sahrens 1994789Sahrens static void 1995789Sahrens arc_reclaim_thread(void) 1996789Sahrens { 1997789Sahrens clock_t growtime = 0; 1998789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 1999789Sahrens callb_cpr_t cpr; 2000789Sahrens 2001789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2002789Sahrens 2003789Sahrens mutex_enter(&arc_reclaim_thr_lock); 2004789Sahrens while (arc_thread_exit == 0) { 2005789Sahrens if (arc_reclaim_needed()) { 2006789Sahrens 20073403Sbmc if (arc_no_grow) { 2008789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 2009789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2010789Sahrens } else { 2011789Sahrens last_reclaim = ARC_RECLAIM_CONS; 2012789Sahrens } 2013789Sahrens } else { 20143403Sbmc arc_no_grow = TRUE; 2015789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2016789Sahrens membar_producer(); 2017789Sahrens } 2018789Sahrens 2019789Sahrens /* reset the growth delay for every reclaim */ 2020789Sahrens growtime = lbolt + (arc_grow_retry * hz); 2021789Sahrens 2022789Sahrens arc_kmem_reap_now(last_reclaim); 20236987Sbrendan arc_warm = B_TRUE; 2024789Sahrens 20254309Smaybee } else if (arc_no_grow && lbolt >= growtime) { 20263403Sbmc arc_no_grow = FALSE; 2027789Sahrens } 2028789Sahrens 20293403Sbmc if (2 * arc_c < arc_size + 20303403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 20313298Smaybee arc_adjust(); 20323298Smaybee 20331544Seschrock if (arc_eviction_list != NULL) 20341544Seschrock arc_do_user_evicts(); 20351544Seschrock 2036789Sahrens /* block until needed, or one second, whichever is shorter */ 2037789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 2038789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 2039789Sahrens &arc_reclaim_thr_lock, (lbolt + hz)); 2040789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2041789Sahrens } 2042789Sahrens 2043789Sahrens arc_thread_exit = 0; 2044789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 2045789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2046789Sahrens thread_exit(); 2047789Sahrens } 2048789Sahrens 20491544Seschrock /* 20501544Seschrock * Adapt arc info given the number of bytes we are trying to add and 20511544Seschrock * the state that we are comming from. This function is only called 20521544Seschrock * when we are adding new content to the cache. 20531544Seschrock */ 2054789Sahrens static void 20551544Seschrock arc_adapt(int bytes, arc_state_t *state) 2056789Sahrens { 20571544Seschrock int mult; 20588582SBrendan.Gregg@Sun.COM uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 20591544Seschrock 20605450Sbrendan if (state == arc_l2c_only) 20615450Sbrendan return; 20625450Sbrendan 20631544Seschrock ASSERT(bytes > 0); 2064789Sahrens /* 20651544Seschrock * Adapt the target size of the MRU list: 20661544Seschrock * - if we just hit in the MRU ghost list, then increase 20671544Seschrock * the target size of the MRU list. 20681544Seschrock * - if we just hit in the MFU ghost list, then increase 20691544Seschrock * the target size of the MFU list by decreasing the 20701544Seschrock * target size of the MRU list. 2071789Sahrens */ 20723403Sbmc if (state == arc_mru_ghost) { 20733403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 20743403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 20751544Seschrock 20768582SBrendan.Gregg@Sun.COM arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 20773403Sbmc } else if (state == arc_mfu_ghost) { 20788582SBrendan.Gregg@Sun.COM uint64_t delta; 20798582SBrendan.Gregg@Sun.COM 20803403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 20813403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 20821544Seschrock 20838582SBrendan.Gregg@Sun.COM delta = MIN(bytes * mult, arc_p); 20848582SBrendan.Gregg@Sun.COM arc_p = MAX(arc_p_min, arc_p - delta); 20851544Seschrock } 20863403Sbmc ASSERT((int64_t)arc_p >= 0); 2087789Sahrens 2088789Sahrens if (arc_reclaim_needed()) { 2089789Sahrens cv_signal(&arc_reclaim_thr_cv); 2090789Sahrens return; 2091789Sahrens } 2092789Sahrens 20933403Sbmc if (arc_no_grow) 2094789Sahrens return; 2095789Sahrens 20963403Sbmc if (arc_c >= arc_c_max) 20971544Seschrock return; 20981544Seschrock 2099789Sahrens /* 21001544Seschrock * If we're within (2 * maxblocksize) bytes of the target 21011544Seschrock * cache size, increment the target cache size 2102789Sahrens */ 21033403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 21043403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 21053403Sbmc if (arc_c > arc_c_max) 21063403Sbmc arc_c = arc_c_max; 21073403Sbmc else if (state == arc_anon) 21083403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 21093403Sbmc if (arc_p > arc_c) 21103403Sbmc arc_p = arc_c; 2111789Sahrens } 21123403Sbmc ASSERT((int64_t)arc_p >= 0); 2113789Sahrens } 2114789Sahrens 2115789Sahrens /* 21161544Seschrock * Check if the cache has reached its limits and eviction is required 21171544Seschrock * prior to insert. 2118789Sahrens */ 2119789Sahrens static int 21204309Smaybee arc_evict_needed(arc_buf_contents_t type) 2121789Sahrens { 21224309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 21234309Smaybee return (1); 21244309Smaybee 21254309Smaybee #ifdef _KERNEL 21264309Smaybee /* 21274309Smaybee * If zio data pages are being allocated out of a separate heap segment, 21284309Smaybee * then enforce that the size of available vmem for this area remains 21294309Smaybee * above about 1/32nd free. 21304309Smaybee */ 21314309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 21324309Smaybee vmem_size(zio_arena, VMEM_FREE) < 21334309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 21344309Smaybee return (1); 21354309Smaybee #endif 21364309Smaybee 2137789Sahrens if (arc_reclaim_needed()) 2138789Sahrens return (1); 2139789Sahrens 21403403Sbmc return (arc_size > arc_c); 2141789Sahrens } 2142789Sahrens 2143789Sahrens /* 21442688Smaybee * The buffer, supplied as the first argument, needs a data block. 21452688Smaybee * So, if we are at cache max, determine which cache should be victimized. 21462688Smaybee * We have the following cases: 2147789Sahrens * 21483403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2149789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2150789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2151789Sahrens * 21523403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2153789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2154789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2155789Sahrens * entries. 2156789Sahrens * 21573403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2158789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2159789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2160789Sahrens * the MFU side, so the MRU side needs to be victimized. 2161789Sahrens * 21623403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2163789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2164789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2165789Sahrens */ 2166789Sahrens static void 21672688Smaybee arc_get_data_buf(arc_buf_t *buf) 2168789Sahrens { 21693290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 21703290Sjohansen uint64_t size = buf->b_hdr->b_size; 21713290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 21722688Smaybee 21732688Smaybee arc_adapt(size, state); 2174789Sahrens 21752688Smaybee /* 21762688Smaybee * We have not yet reached cache maximum size, 21772688Smaybee * just allocate a new buffer. 21782688Smaybee */ 21794309Smaybee if (!arc_evict_needed(type)) { 21803290Sjohansen if (type == ARC_BUFC_METADATA) { 21813290Sjohansen buf->b_data = zio_buf_alloc(size); 21828582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 21833290Sjohansen } else { 21843290Sjohansen ASSERT(type == ARC_BUFC_DATA); 21853290Sjohansen buf->b_data = zio_data_buf_alloc(size); 21868582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 21874309Smaybee atomic_add_64(&arc_size, size); 21883290Sjohansen } 21892688Smaybee goto out; 21902688Smaybee } 21912688Smaybee 21922688Smaybee /* 21932688Smaybee * If we are prefetching from the mfu ghost list, this buffer 21942688Smaybee * will end up on the mru list; so steal space from there. 21952688Smaybee */ 21963403Sbmc if (state == arc_mfu_ghost) 21973403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 21983403Sbmc else if (state == arc_mru_ghost) 21993403Sbmc state = arc_mru; 2200789Sahrens 22013403Sbmc if (state == arc_mru || state == arc_anon) { 22023403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 22038582SBrendan.Gregg@Sun.COM state = (arc_mfu->arcs_lsize[type] >= size && 22044309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2205789Sahrens } else { 22062688Smaybee /* MFU cases */ 22073403Sbmc uint64_t mfu_space = arc_c - arc_p; 22088582SBrendan.Gregg@Sun.COM state = (arc_mru->arcs_lsize[type] >= size && 22094309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 22102688Smaybee } 22115642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 22123290Sjohansen if (type == ARC_BUFC_METADATA) { 22133290Sjohansen buf->b_data = zio_buf_alloc(size); 22148582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 22153290Sjohansen } else { 22163290Sjohansen ASSERT(type == ARC_BUFC_DATA); 22173290Sjohansen buf->b_data = zio_data_buf_alloc(size); 22188582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 22194309Smaybee atomic_add_64(&arc_size, size); 22203290Sjohansen } 22213403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 22222688Smaybee } 22232688Smaybee ASSERT(buf->b_data != NULL); 22242688Smaybee out: 22252688Smaybee /* 22262688Smaybee * Update the state size. Note that ghost states have a 22272688Smaybee * "ghost size" and so don't need to be updated. 22282688Smaybee */ 22292688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 22302688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 22312688Smaybee 22323403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 22332688Smaybee if (list_link_active(&hdr->b_arc_node)) { 22342688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 22354309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2236789Sahrens } 22373298Smaybee /* 22383298Smaybee * If we are growing the cache, and we are adding anonymous 22393403Sbmc * data, and we have outgrown arc_p, update arc_p 22403298Smaybee */ 22413403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 22423403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 22433403Sbmc arc_p = MIN(arc_c, arc_p + size); 2244789Sahrens } 2245789Sahrens } 2246789Sahrens 2247789Sahrens /* 2248789Sahrens * This routine is called whenever a buffer is accessed. 22491544Seschrock * NOTE: the hash lock is dropped in this function. 2250789Sahrens */ 2251789Sahrens static void 22522688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2253789Sahrens { 2254789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2255789Sahrens 22563403Sbmc if (buf->b_state == arc_anon) { 2257789Sahrens /* 2258789Sahrens * This buffer is not in the cache, and does not 2259789Sahrens * appear in our "ghost" list. Add the new buffer 2260789Sahrens * to the MRU state. 2261789Sahrens */ 2262789Sahrens 2263789Sahrens ASSERT(buf->b_arc_access == 0); 2264789Sahrens buf->b_arc_access = lbolt; 22651544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 22663403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2267789Sahrens 22683403Sbmc } else if (buf->b_state == arc_mru) { 2269789Sahrens /* 22702391Smaybee * If this buffer is here because of a prefetch, then either: 22712391Smaybee * - clear the flag if this is a "referencing" read 22722391Smaybee * (any subsequent access will bump this into the MFU state). 22732391Smaybee * or 22742391Smaybee * - move the buffer to the head of the list if this is 22752391Smaybee * another prefetch (to make it less likely to be evicted). 2276789Sahrens */ 2277789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 22782391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 22792391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 22802391Smaybee } else { 22812391Smaybee buf->b_flags &= ~ARC_PREFETCH; 22823403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 22832391Smaybee } 22842391Smaybee buf->b_arc_access = lbolt; 2285789Sahrens return; 2286789Sahrens } 2287789Sahrens 2288789Sahrens /* 2289789Sahrens * This buffer has been "accessed" only once so far, 2290789Sahrens * but it is still in the cache. Move it to the MFU 2291789Sahrens * state. 2292789Sahrens */ 2293789Sahrens if (lbolt > buf->b_arc_access + ARC_MINTIME) { 2294789Sahrens /* 2295789Sahrens * More than 125ms have passed since we 2296789Sahrens * instantiated this buffer. Move it to the 2297789Sahrens * most frequently used state. 2298789Sahrens */ 2299789Sahrens buf->b_arc_access = lbolt; 23001544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23013403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2302789Sahrens } 23033403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 23043403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2305789Sahrens arc_state_t *new_state; 2306789Sahrens /* 2307789Sahrens * This buffer has been "accessed" recently, but 2308789Sahrens * was evicted from the cache. Move it to the 2309789Sahrens * MFU state. 2310789Sahrens */ 2311789Sahrens 2312789Sahrens if (buf->b_flags & ARC_PREFETCH) { 23133403Sbmc new_state = arc_mru; 23142391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 23152391Smaybee buf->b_flags &= ~ARC_PREFETCH; 23161544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2317789Sahrens } else { 23183403Sbmc new_state = arc_mfu; 23191544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2320789Sahrens } 2321789Sahrens 2322789Sahrens buf->b_arc_access = lbolt; 2323789Sahrens arc_change_state(new_state, buf, hash_lock); 2324789Sahrens 23253403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 23263403Sbmc } else if (buf->b_state == arc_mfu) { 2327789Sahrens /* 2328789Sahrens * This buffer has been accessed more than once and is 2329789Sahrens * still in the cache. Keep it in the MFU state. 2330789Sahrens * 23312391Smaybee * NOTE: an add_reference() that occurred when we did 23322391Smaybee * the arc_read() will have kicked this off the list. 23332391Smaybee * If it was a prefetch, we will explicitly move it to 23342391Smaybee * the head of the list now. 2335789Sahrens */ 23362391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 23372391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 23382391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23392391Smaybee } 23403403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 23412391Smaybee buf->b_arc_access = lbolt; 23423403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 23433403Sbmc arc_state_t *new_state = arc_mfu; 2344789Sahrens /* 2345789Sahrens * This buffer has been accessed more than once but has 2346789Sahrens * been evicted from the cache. Move it back to the 2347789Sahrens * MFU state. 2348789Sahrens */ 2349789Sahrens 23502391Smaybee if (buf->b_flags & ARC_PREFETCH) { 23512391Smaybee /* 23522391Smaybee * This is a prefetch access... 23532391Smaybee * move this block back to the MRU state. 23542391Smaybee */ 23552391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 23563403Sbmc new_state = arc_mru; 23572391Smaybee } 23582391Smaybee 2359789Sahrens buf->b_arc_access = lbolt; 23601544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23612391Smaybee arc_change_state(new_state, buf, hash_lock); 2362789Sahrens 23633403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 23645450Sbrendan } else if (buf->b_state == arc_l2c_only) { 23655450Sbrendan /* 23665450Sbrendan * This buffer is on the 2nd Level ARC. 23675450Sbrendan */ 23685450Sbrendan 23695450Sbrendan buf->b_arc_access = lbolt; 23705450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23715450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2372789Sahrens } else { 2373789Sahrens ASSERT(!"invalid arc state"); 2374789Sahrens } 2375789Sahrens } 2376789Sahrens 2377789Sahrens /* a generic arc_done_func_t which you can use */ 2378789Sahrens /* ARGSUSED */ 2379789Sahrens void 2380789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2381789Sahrens { 2382789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 23831544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2384789Sahrens } 2385789Sahrens 23864309Smaybee /* a generic arc_done_func_t */ 2387789Sahrens void 2388789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2389789Sahrens { 2390789Sahrens arc_buf_t **bufp = arg; 2391789Sahrens if (zio && zio->io_error) { 23921544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2393789Sahrens *bufp = NULL; 2394789Sahrens } else { 2395789Sahrens *bufp = buf; 2396789Sahrens } 2397789Sahrens } 2398789Sahrens 2399789Sahrens static void 2400789Sahrens arc_read_done(zio_t *zio) 2401789Sahrens { 24021589Smaybee arc_buf_hdr_t *hdr, *found; 2403789Sahrens arc_buf_t *buf; 2404789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2405789Sahrens kmutex_t *hash_lock; 2406789Sahrens arc_callback_t *callback_list, *acb; 2407789Sahrens int freeable = FALSE; 2408789Sahrens 2409789Sahrens buf = zio->io_private; 2410789Sahrens hdr = buf->b_hdr; 2411789Sahrens 24121589Smaybee /* 24131589Smaybee * The hdr was inserted into hash-table and removed from lists 24141589Smaybee * prior to starting I/O. We should find this header, since 24151589Smaybee * it's in the hash table, and it should be legit since it's 24161589Smaybee * not possible to evict it during the I/O. The only possible 24171589Smaybee * reason for it not to be found is if we were freed during the 24181589Smaybee * read. 24191589Smaybee */ 24208636SMark.Maybee@Sun.COM found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 24213093Sahrens &hash_lock); 2422789Sahrens 24231589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 24245450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 24255450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 24265450Sbrendan 24276987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 24285450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 24297237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2430789Sahrens 2431789Sahrens /* byteswap if necessary */ 2432789Sahrens callback_list = hdr->b_acb; 2433789Sahrens ASSERT(callback_list != NULL); 24347046Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 24357046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 24367046Sahrens byteswap_uint64_array : 24377046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 24387046Sahrens func(buf->b_data, hdr->b_size); 24397046Sahrens } 2440789Sahrens 24415450Sbrendan arc_cksum_compute(buf, B_FALSE); 24423093Sahrens 2443789Sahrens /* create copies of the data buffer for the callers */ 2444789Sahrens abuf = buf; 2445789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2446789Sahrens if (acb->acb_done) { 24472688Smaybee if (abuf == NULL) 24482688Smaybee abuf = arc_buf_clone(buf); 2449789Sahrens acb->acb_buf = abuf; 2450789Sahrens abuf = NULL; 2451789Sahrens } 2452789Sahrens } 2453789Sahrens hdr->b_acb = NULL; 2454789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 24551544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 24561544Seschrock if (abuf == buf) 24571544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 2458789Sahrens 2459789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2460789Sahrens 2461789Sahrens if (zio->io_error != 0) { 2462789Sahrens hdr->b_flags |= ARC_IO_ERROR; 24633403Sbmc if (hdr->b_state != arc_anon) 24643403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 24651544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 24661544Seschrock buf_hash_remove(hdr); 2467789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2468789Sahrens } 2469789Sahrens 24701544Seschrock /* 24712391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 24722391Smaybee * that the hdr (and hence the cv) might be freed before we get to 24732391Smaybee * the cv_broadcast(). 24741544Seschrock */ 24751544Seschrock cv_broadcast(&hdr->b_cv); 24761544Seschrock 24771589Smaybee if (hash_lock) { 2478789Sahrens /* 2479789Sahrens * Only call arc_access on anonymous buffers. This is because 2480789Sahrens * if we've issued an I/O for an evicted buffer, we've already 2481789Sahrens * called arc_access (to prevent any simultaneous readers from 2482789Sahrens * getting confused). 2483789Sahrens */ 24843403Sbmc if (zio->io_error == 0 && hdr->b_state == arc_anon) 24852688Smaybee arc_access(hdr, hash_lock); 24862688Smaybee mutex_exit(hash_lock); 2487789Sahrens } else { 2488789Sahrens /* 2489789Sahrens * This block was freed while we waited for the read to 2490789Sahrens * complete. It has been removed from the hash table and 2491789Sahrens * moved to the anonymous state (so that it won't show up 2492789Sahrens * in the cache). 2493789Sahrens */ 24943403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2495789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2496789Sahrens } 2497789Sahrens 2498789Sahrens /* execute each callback and free its structure */ 2499789Sahrens while ((acb = callback_list) != NULL) { 2500789Sahrens if (acb->acb_done) 2501789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2502789Sahrens 2503789Sahrens if (acb->acb_zio_dummy != NULL) { 2504789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2505789Sahrens zio_nowait(acb->acb_zio_dummy); 2506789Sahrens } 2507789Sahrens 2508789Sahrens callback_list = acb->acb_next; 2509789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2510789Sahrens } 2511789Sahrens 2512789Sahrens if (freeable) 25131544Seschrock arc_hdr_destroy(hdr); 2514789Sahrens } 2515789Sahrens 2516789Sahrens /* 2517789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2518789Sahrens * cache. If the block is found in the cache, invoke the provided 2519789Sahrens * callback immediately and return. Note that the `zio' parameter 2520789Sahrens * in the callback will be NULL in this case, since no IO was 2521789Sahrens * required. If the block is not in the cache pass the read request 2522789Sahrens * on to the spa with a substitute callback function, so that the 2523789Sahrens * requested block will be added to the cache. 2524789Sahrens * 2525789Sahrens * If a read request arrives for a block that has a read in-progress, 2526789Sahrens * either wait for the in-progress read to complete (and return the 2527789Sahrens * results); or, if this is a read with a "done" func, add a record 2528789Sahrens * to the read to invoke the "done" func when the read completes, 2529789Sahrens * and return; or just return. 2530789Sahrens * 2531789Sahrens * arc_read_done() will invoke all the requested "done" functions 2532789Sahrens * for readers of this block. 25337046Sahrens * 25347046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 25357046Sahrens * for the bp. But if you know you don't need locking, you can use 25368213SSuhasini.Peddada@Sun.COM * arc_read_bp. 2537789Sahrens */ 2538789Sahrens int 25397046Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_buf_t *pbuf, 25407237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 25417046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 25427046Sahrens { 25437046Sahrens int err; 25447046Sahrens 25457046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 25467046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 25477545SMark.Maybee@Sun.COM rw_enter(&pbuf->b_lock, RW_READER); 25487046Sahrens 25497046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 25507237Sek110237 zio_flags, arc_flags, zb); 25517545SMark.Maybee@Sun.COM rw_exit(&pbuf->b_lock); 25529396SMatthew.Ahrens@Sun.COM 25537046Sahrens return (err); 25547046Sahrens } 25557046Sahrens 25567046Sahrens int 25577046Sahrens arc_read_nolock(zio_t *pio, spa_t *spa, blkptr_t *bp, 25587237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 25597046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2560789Sahrens { 2561789Sahrens arc_buf_hdr_t *hdr; 2562789Sahrens arc_buf_t *buf; 2563789Sahrens kmutex_t *hash_lock; 25645450Sbrendan zio_t *rzio; 25658636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 2566789Sahrens 2567789Sahrens top: 25688636SMark.Maybee@Sun.COM hdr = buf_hash_find(guid, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 25691544Seschrock if (hdr && hdr->b_datacnt > 0) { 2570789Sahrens 25712391Smaybee *arc_flags |= ARC_CACHED; 25722391Smaybee 2573789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 25742391Smaybee 25752391Smaybee if (*arc_flags & ARC_WAIT) { 25762391Smaybee cv_wait(&hdr->b_cv, hash_lock); 25772391Smaybee mutex_exit(hash_lock); 25782391Smaybee goto top; 25792391Smaybee } 25802391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 25812391Smaybee 25822391Smaybee if (done) { 2583789Sahrens arc_callback_t *acb = NULL; 2584789Sahrens 2585789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2586789Sahrens KM_SLEEP); 2587789Sahrens acb->acb_done = done; 2588789Sahrens acb->acb_private = private; 2589789Sahrens if (pio != NULL) 2590789Sahrens acb->acb_zio_dummy = zio_null(pio, 25918632SBill.Moore@Sun.COM spa, NULL, NULL, NULL, zio_flags); 2592789Sahrens 2593789Sahrens ASSERT(acb->acb_done != NULL); 2594789Sahrens acb->acb_next = hdr->b_acb; 2595789Sahrens hdr->b_acb = acb; 2596789Sahrens add_reference(hdr, hash_lock, private); 2597789Sahrens mutex_exit(hash_lock); 2598789Sahrens return (0); 2599789Sahrens } 2600789Sahrens mutex_exit(hash_lock); 2601789Sahrens return (0); 2602789Sahrens } 2603789Sahrens 26043403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2605789Sahrens 26061544Seschrock if (done) { 26072688Smaybee add_reference(hdr, hash_lock, private); 26081544Seschrock /* 26091544Seschrock * If this block is already in use, create a new 26101544Seschrock * copy of the data so that we will be guaranteed 26111544Seschrock * that arc_release() will always succeed. 26121544Seschrock */ 26131544Seschrock buf = hdr->b_buf; 26141544Seschrock ASSERT(buf); 26151544Seschrock ASSERT(buf->b_data); 26162688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 26171544Seschrock ASSERT(buf->b_efunc == NULL); 26181544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 26192688Smaybee } else { 26202688Smaybee buf = arc_buf_clone(buf); 26211544Seschrock } 26222391Smaybee } else if (*arc_flags & ARC_PREFETCH && 26232391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 26242391Smaybee hdr->b_flags |= ARC_PREFETCH; 2625789Sahrens } 2626789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 26272688Smaybee arc_access(hdr, hash_lock); 26287237Sek110237 if (*arc_flags & ARC_L2CACHE) 26297237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26302688Smaybee mutex_exit(hash_lock); 26313403Sbmc ARCSTAT_BUMP(arcstat_hits); 26323403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 26333403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 26343403Sbmc data, metadata, hits); 26353403Sbmc 2636789Sahrens if (done) 2637789Sahrens done(NULL, buf, private); 2638789Sahrens } else { 2639789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2640789Sahrens arc_callback_t *acb; 26416987Sbrendan vdev_t *vd = NULL; 26429215SGeorge.Wilson@Sun.COM uint64_t addr; 26438582SBrendan.Gregg@Sun.COM boolean_t devw = B_FALSE; 2644789Sahrens 2645789Sahrens if (hdr == NULL) { 2646789Sahrens /* this block is not in the cache */ 2647789Sahrens arc_buf_hdr_t *exists; 26483290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 26493290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2650789Sahrens hdr = buf->b_hdr; 2651789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 2652789Sahrens hdr->b_birth = bp->blk_birth; 2653789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2654789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2655789Sahrens if (exists) { 2656789Sahrens /* somebody beat us to the hash insert */ 2657789Sahrens mutex_exit(hash_lock); 2658789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2659789Sahrens hdr->b_birth = 0; 2660789Sahrens hdr->b_cksum0 = 0; 26611544Seschrock (void) arc_buf_remove_ref(buf, private); 2662789Sahrens goto top; /* restart the IO request */ 2663789Sahrens } 26642391Smaybee /* if this is a prefetch, we don't have a reference */ 26652391Smaybee if (*arc_flags & ARC_PREFETCH) { 26662391Smaybee (void) remove_reference(hdr, hash_lock, 26672391Smaybee private); 26682391Smaybee hdr->b_flags |= ARC_PREFETCH; 26692391Smaybee } 26707237Sek110237 if (*arc_flags & ARC_L2CACHE) 26717237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26722391Smaybee if (BP_GET_LEVEL(bp) > 0) 26732391Smaybee hdr->b_flags |= ARC_INDIRECT; 2674789Sahrens } else { 2675789Sahrens /* this block is in the ghost cache */ 26761544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 26771544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 26782391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 26792391Smaybee ASSERT(hdr->b_buf == NULL); 2680789Sahrens 26812391Smaybee /* if this is a prefetch, we don't have a reference */ 26822391Smaybee if (*arc_flags & ARC_PREFETCH) 26832391Smaybee hdr->b_flags |= ARC_PREFETCH; 26842391Smaybee else 26852391Smaybee add_reference(hdr, hash_lock, private); 26867237Sek110237 if (*arc_flags & ARC_L2CACHE) 26877237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26886245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 26891544Seschrock buf->b_hdr = hdr; 26902688Smaybee buf->b_data = NULL; 26911544Seschrock buf->b_efunc = NULL; 26921544Seschrock buf->b_private = NULL; 26931544Seschrock buf->b_next = NULL; 26941544Seschrock hdr->b_buf = buf; 26952688Smaybee arc_get_data_buf(buf); 26961544Seschrock ASSERT(hdr->b_datacnt == 0); 26971544Seschrock hdr->b_datacnt = 1; 26982391Smaybee 2699789Sahrens } 2700789Sahrens 2701789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2702789Sahrens acb->acb_done = done; 2703789Sahrens acb->acb_private = private; 2704789Sahrens 2705789Sahrens ASSERT(hdr->b_acb == NULL); 2706789Sahrens hdr->b_acb = acb; 2707789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2708789Sahrens 2709789Sahrens /* 2710789Sahrens * If the buffer has been evicted, migrate it to a present state 2711789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2712789Sahrens * the header will be marked as I/O in progress and have an 2713789Sahrens * attached buffer. At this point, anybody who finds this 2714789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2715789Sahrens */ 2716789Sahrens 27171544Seschrock if (GHOST_STATE(hdr->b_state)) 27182688Smaybee arc_access(hdr, hash_lock); 2719789Sahrens 27207754SJeff.Bonwick@Sun.COM if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 27217754SJeff.Bonwick@Sun.COM (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 27228582SBrendan.Gregg@Sun.COM devw = hdr->b_l2hdr->b_dev->l2ad_writing; 27236987Sbrendan addr = hdr->b_l2hdr->b_daddr; 27247754SJeff.Bonwick@Sun.COM /* 27257754SJeff.Bonwick@Sun.COM * Lock out device removal. 27267754SJeff.Bonwick@Sun.COM */ 27277754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || 27287754SJeff.Bonwick@Sun.COM !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 27297754SJeff.Bonwick@Sun.COM vd = NULL; 27306987Sbrendan } 27316987Sbrendan 27326987Sbrendan mutex_exit(hash_lock); 27336987Sbrendan 2734789Sahrens ASSERT3U(hdr->b_size, ==, size); 27351596Sahrens DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size, 27361596Sahrens zbookmark_t *, zb); 27373403Sbmc ARCSTAT_BUMP(arcstat_misses); 27383403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 27393403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 27403403Sbmc data, metadata, misses); 27411544Seschrock 27428582SBrendan.Gregg@Sun.COM if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 27436987Sbrendan /* 27446987Sbrendan * Read from the L2ARC if the following are true: 27456987Sbrendan * 1. The L2ARC vdev was previously cached. 27466987Sbrendan * 2. This buffer still has L2ARC metadata. 27476987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 27486987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 27496987Sbrendan * also have invalidated the vdev. 27508582SBrendan.Gregg@Sun.COM * 5. This isn't prefetch and l2arc_noprefetch is set. 27516987Sbrendan */ 27527754SJeff.Bonwick@Sun.COM if (hdr->b_l2hdr != NULL && 27538582SBrendan.Gregg@Sun.COM !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 27548582SBrendan.Gregg@Sun.COM !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 27555450Sbrendan l2arc_read_callback_t *cb; 27565450Sbrendan 27576643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 27586643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 27596643Seschrock 27605450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 27615450Sbrendan KM_SLEEP); 27625450Sbrendan cb->l2rcb_buf = buf; 27635450Sbrendan cb->l2rcb_spa = spa; 27645450Sbrendan cb->l2rcb_bp = *bp; 27655450Sbrendan cb->l2rcb_zb = *zb; 27667237Sek110237 cb->l2rcb_flags = zio_flags; 27675450Sbrendan 27685450Sbrendan /* 27697754SJeff.Bonwick@Sun.COM * l2arc read. The SCL_L2ARC lock will be 27707754SJeff.Bonwick@Sun.COM * released by l2arc_read_done(). 27715450Sbrendan */ 27725450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 27735450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 27747237Sek110237 l2arc_read_done, cb, priority, zio_flags | 27757361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 27767754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_PROPAGATE | 27777754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_RETRY, B_FALSE); 27785450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 27795450Sbrendan zio_t *, rzio); 27808582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_read_bytes, size); 27816987Sbrendan 27826987Sbrendan if (*arc_flags & ARC_NOWAIT) { 27836987Sbrendan zio_nowait(rzio); 27846987Sbrendan return (0); 27856987Sbrendan } 27866987Sbrendan 27876987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 27886987Sbrendan if (zio_wait(rzio) == 0) 27896987Sbrendan return (0); 27906987Sbrendan 27916987Sbrendan /* l2arc read error; goto zio_read() */ 27925450Sbrendan } else { 27935450Sbrendan DTRACE_PROBE1(l2arc__miss, 27945450Sbrendan arc_buf_hdr_t *, hdr); 27955450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 27965450Sbrendan if (HDR_L2_WRITING(hdr)) 27975450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 27987754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 27995450Sbrendan } 28008582SBrendan.Gregg@Sun.COM } else { 28018628SBill.Moore@Sun.COM if (vd != NULL) 28028628SBill.Moore@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 28038582SBrendan.Gregg@Sun.COM if (l2arc_ndev != 0) { 28048582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(l2arc__miss, 28058582SBrendan.Gregg@Sun.COM arc_buf_hdr_t *, hdr); 28068582SBrendan.Gregg@Sun.COM ARCSTAT_BUMP(arcstat_l2_misses); 28078582SBrendan.Gregg@Sun.COM } 28085450Sbrendan } 28096643Seschrock 2810789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 28117237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2812789Sahrens 28132391Smaybee if (*arc_flags & ARC_WAIT) 2814789Sahrens return (zio_wait(rzio)); 2815789Sahrens 28162391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2817789Sahrens zio_nowait(rzio); 2818789Sahrens } 2819789Sahrens return (0); 2820789Sahrens } 2821789Sahrens 2822789Sahrens /* 2823789Sahrens * arc_read() variant to support pool traversal. If the block is already 2824789Sahrens * in the ARC, make a copy of it; otherwise, the caller will do the I/O. 2825789Sahrens * The idea is that we don't want pool traversal filling up memory, but 2826789Sahrens * if the ARC already has the data anyway, we shouldn't pay for the I/O. 2827789Sahrens */ 2828789Sahrens int 2829789Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data) 2830789Sahrens { 2831789Sahrens arc_buf_hdr_t *hdr; 2832789Sahrens kmutex_t *hash_mtx; 28338636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 2834789Sahrens int rc = 0; 2835789Sahrens 28368636SMark.Maybee@Sun.COM hdr = buf_hash_find(guid, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx); 2837789Sahrens 28381544Seschrock if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) { 28391544Seschrock arc_buf_t *buf = hdr->b_buf; 28401544Seschrock 28411544Seschrock ASSERT(buf); 28421544Seschrock while (buf->b_data == NULL) { 28431544Seschrock buf = buf->b_next; 28441544Seschrock ASSERT(buf); 28451544Seschrock } 28461544Seschrock bcopy(buf->b_data, data, hdr->b_size); 28471544Seschrock } else { 2848789Sahrens rc = ENOENT; 28491544Seschrock } 2850789Sahrens 2851789Sahrens if (hash_mtx) 2852789Sahrens mutex_exit(hash_mtx); 2853789Sahrens 2854789Sahrens return (rc); 2855789Sahrens } 2856789Sahrens 28571544Seschrock void 28581544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 28591544Seschrock { 28601544Seschrock ASSERT(buf->b_hdr != NULL); 28613403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 28621544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 28631544Seschrock buf->b_efunc = func; 28641544Seschrock buf->b_private = private; 28651544Seschrock } 28661544Seschrock 28671544Seschrock /* 28681544Seschrock * This is used by the DMU to let the ARC know that a buffer is 28691544Seschrock * being evicted, so the ARC should clean up. If this arc buf 28701544Seschrock * is not yet in the evicted state, it will be put there. 28711544Seschrock */ 28721544Seschrock int 28731544Seschrock arc_buf_evict(arc_buf_t *buf) 28741544Seschrock { 28752887Smaybee arc_buf_hdr_t *hdr; 28761544Seschrock kmutex_t *hash_lock; 28771544Seschrock arc_buf_t **bufp; 28781544Seschrock 28797545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 28802887Smaybee hdr = buf->b_hdr; 28811544Seschrock if (hdr == NULL) { 28821544Seschrock /* 28831544Seschrock * We are in arc_do_user_evicts(). 28841544Seschrock */ 28851544Seschrock ASSERT(buf->b_data == NULL); 28867545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 28871544Seschrock return (0); 28887545SMark.Maybee@Sun.COM } else if (buf->b_data == NULL) { 28897545SMark.Maybee@Sun.COM arc_buf_t copy = *buf; /* structure assignment */ 28907545SMark.Maybee@Sun.COM /* 28917545SMark.Maybee@Sun.COM * We are on the eviction list; process this buffer now 28927545SMark.Maybee@Sun.COM * but let arc_do_user_evicts() do the reaping. 28937545SMark.Maybee@Sun.COM */ 28947545SMark.Maybee@Sun.COM buf->b_efunc = NULL; 28957545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 28967545SMark.Maybee@Sun.COM VERIFY(copy.b_efunc(©) == 0); 28977545SMark.Maybee@Sun.COM return (1); 28981544Seschrock } 28992887Smaybee hash_lock = HDR_LOCK(hdr); 29001544Seschrock mutex_enter(hash_lock); 29011544Seschrock 29022724Smaybee ASSERT(buf->b_hdr == hdr); 29032724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 29043403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 29051544Seschrock 29061544Seschrock /* 29071544Seschrock * Pull this buffer off of the hdr 29081544Seschrock */ 29091544Seschrock bufp = &hdr->b_buf; 29101544Seschrock while (*bufp != buf) 29111544Seschrock bufp = &(*bufp)->b_next; 29121544Seschrock *bufp = buf->b_next; 29131544Seschrock 29141544Seschrock ASSERT(buf->b_data != NULL); 29152688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 29161544Seschrock 29171544Seschrock if (hdr->b_datacnt == 0) { 29181544Seschrock arc_state_t *old_state = hdr->b_state; 29191544Seschrock arc_state_t *evicted_state; 29201544Seschrock 29211544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 29221544Seschrock 29231544Seschrock evicted_state = 29243403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 29251544Seschrock 29263403Sbmc mutex_enter(&old_state->arcs_mtx); 29273403Sbmc mutex_enter(&evicted_state->arcs_mtx); 29281544Seschrock 29291544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 29301544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 29315450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 29325450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 29331544Seschrock 29343403Sbmc mutex_exit(&evicted_state->arcs_mtx); 29353403Sbmc mutex_exit(&old_state->arcs_mtx); 29361544Seschrock } 29371544Seschrock mutex_exit(hash_lock); 29387545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29391819Smaybee 29401544Seschrock VERIFY(buf->b_efunc(buf) == 0); 29411544Seschrock buf->b_efunc = NULL; 29421544Seschrock buf->b_private = NULL; 29431544Seschrock buf->b_hdr = NULL; 29441544Seschrock kmem_cache_free(buf_cache, buf); 29451544Seschrock return (1); 29461544Seschrock } 29471544Seschrock 2948789Sahrens /* 2949789Sahrens * Release this buffer from the cache. This must be done 2950789Sahrens * after a read and prior to modifying the buffer contents. 2951789Sahrens * If the buffer has more than one reference, we must make 29527046Sahrens * a new hdr for the buffer. 2953789Sahrens */ 2954789Sahrens void 2955789Sahrens arc_release(arc_buf_t *buf, void *tag) 2956789Sahrens { 29577545SMark.Maybee@Sun.COM arc_buf_hdr_t *hdr; 29587545SMark.Maybee@Sun.COM kmutex_t *hash_lock; 29597545SMark.Maybee@Sun.COM l2arc_buf_hdr_t *l2hdr; 29605450Sbrendan uint64_t buf_size; 29619274SBrendan.Gregg@Sun.COM boolean_t released = B_FALSE; 2962789Sahrens 29637545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 29647545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 29657545SMark.Maybee@Sun.COM 2966789Sahrens /* this buffer is not on any list */ 2967789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 29687046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 2969789Sahrens 29703403Sbmc if (hdr->b_state == arc_anon) { 2971789Sahrens /* this buffer is already released */ 2972789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2973789Sahrens ASSERT(BUF_EMPTY(hdr)); 29741544Seschrock ASSERT(buf->b_efunc == NULL); 29753093Sahrens arc_buf_thaw(buf); 29767545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29779274SBrendan.Gregg@Sun.COM released = B_TRUE; 29789274SBrendan.Gregg@Sun.COM } else { 29799274SBrendan.Gregg@Sun.COM hash_lock = HDR_LOCK(hdr); 29809274SBrendan.Gregg@Sun.COM mutex_enter(hash_lock); 2981789Sahrens } 2982789Sahrens 29837545SMark.Maybee@Sun.COM l2hdr = hdr->b_l2hdr; 29847545SMark.Maybee@Sun.COM if (l2hdr) { 29857545SMark.Maybee@Sun.COM mutex_enter(&l2arc_buflist_mtx); 29867545SMark.Maybee@Sun.COM hdr->b_l2hdr = NULL; 29877545SMark.Maybee@Sun.COM buf_size = hdr->b_size; 29887545SMark.Maybee@Sun.COM } 29897545SMark.Maybee@Sun.COM 29909274SBrendan.Gregg@Sun.COM if (released) 29919274SBrendan.Gregg@Sun.COM goto out; 29929274SBrendan.Gregg@Sun.COM 29931544Seschrock /* 29941544Seschrock * Do we have more than one buf? 29951544Seschrock */ 29967545SMark.Maybee@Sun.COM if (hdr->b_datacnt > 1) { 2997789Sahrens arc_buf_hdr_t *nhdr; 2998789Sahrens arc_buf_t **bufp; 2999789Sahrens uint64_t blksz = hdr->b_size; 30008636SMark.Maybee@Sun.COM uint64_t spa = hdr->b_spa; 30013290Sjohansen arc_buf_contents_t type = hdr->b_type; 30025450Sbrendan uint32_t flags = hdr->b_flags; 3003789Sahrens 30047545SMark.Maybee@Sun.COM ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3005789Sahrens /* 3006789Sahrens * Pull the data off of this buf and attach it to 3007789Sahrens * a new anonymous buf. 3008789Sahrens */ 30091544Seschrock (void) remove_reference(hdr, hash_lock, tag); 3010789Sahrens bufp = &hdr->b_buf; 30111544Seschrock while (*bufp != buf) 3012789Sahrens bufp = &(*bufp)->b_next; 3013789Sahrens *bufp = (*bufp)->b_next; 30143897Smaybee buf->b_next = NULL; 30151544Seschrock 30163403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 30173403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 30181544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 30194309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 30204309Smaybee ASSERT3U(*size, >=, hdr->b_size); 30214309Smaybee atomic_add_64(size, -hdr->b_size); 30221544Seschrock } 30231544Seschrock hdr->b_datacnt -= 1; 30243547Smaybee arc_cksum_verify(buf); 30251544Seschrock 3026789Sahrens mutex_exit(hash_lock); 3027789Sahrens 30286245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3029789Sahrens nhdr->b_size = blksz; 3030789Sahrens nhdr->b_spa = spa; 30313290Sjohansen nhdr->b_type = type; 3032789Sahrens nhdr->b_buf = buf; 30333403Sbmc nhdr->b_state = arc_anon; 3034789Sahrens nhdr->b_arc_access = 0; 30355450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 30365450Sbrendan nhdr->b_l2hdr = NULL; 30371544Seschrock nhdr->b_datacnt = 1; 30383547Smaybee nhdr->b_freeze_cksum = NULL; 30393897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 3040789Sahrens buf->b_hdr = nhdr; 30417545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30423403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 3043789Sahrens } else { 30447545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30451544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3046789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 3047789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 30483403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 3049789Sahrens hdr->b_arc_access = 0; 3050789Sahrens mutex_exit(hash_lock); 30515450Sbrendan 3052789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 3053789Sahrens hdr->b_birth = 0; 3054789Sahrens hdr->b_cksum0 = 0; 30553547Smaybee arc_buf_thaw(buf); 3056789Sahrens } 30571544Seschrock buf->b_efunc = NULL; 30581544Seschrock buf->b_private = NULL; 30595450Sbrendan 30609274SBrendan.Gregg@Sun.COM out: 30615450Sbrendan if (l2hdr) { 30625450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 30635450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 30645450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 30657545SMark.Maybee@Sun.COM mutex_exit(&l2arc_buflist_mtx); 30665450Sbrendan } 3067789Sahrens } 3068789Sahrens 3069789Sahrens int 3070789Sahrens arc_released(arc_buf_t *buf) 3071789Sahrens { 30727545SMark.Maybee@Sun.COM int released; 30737545SMark.Maybee@Sun.COM 30747545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30757545SMark.Maybee@Sun.COM released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 30767545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30777545SMark.Maybee@Sun.COM return (released); 30781544Seschrock } 30791544Seschrock 30801544Seschrock int 30811544Seschrock arc_has_callback(arc_buf_t *buf) 30821544Seschrock { 30837545SMark.Maybee@Sun.COM int callback; 30847545SMark.Maybee@Sun.COM 30857545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30867545SMark.Maybee@Sun.COM callback = (buf->b_efunc != NULL); 30877545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30887545SMark.Maybee@Sun.COM return (callback); 3089789Sahrens } 3090789Sahrens 30911544Seschrock #ifdef ZFS_DEBUG 30921544Seschrock int 30931544Seschrock arc_referenced(arc_buf_t *buf) 30941544Seschrock { 30957545SMark.Maybee@Sun.COM int referenced; 30967545SMark.Maybee@Sun.COM 30977545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30987545SMark.Maybee@Sun.COM referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 30997545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 31007545SMark.Maybee@Sun.COM return (referenced); 31011544Seschrock } 31021544Seschrock #endif 31031544Seschrock 3104789Sahrens static void 31053547Smaybee arc_write_ready(zio_t *zio) 31063547Smaybee { 31073547Smaybee arc_write_callback_t *callback = zio->io_private; 31083547Smaybee arc_buf_t *buf = callback->awcb_buf; 31095329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 31105329Sgw25295 31117754SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 31127754SJeff.Bonwick@Sun.COM callback->awcb_ready(zio, buf, callback->awcb_private); 31137754SJeff.Bonwick@Sun.COM 31145329Sgw25295 /* 31155329Sgw25295 * If the IO is already in progress, then this is a re-write 31167754SJeff.Bonwick@Sun.COM * attempt, so we need to thaw and re-compute the cksum. 31177754SJeff.Bonwick@Sun.COM * It is the responsibility of the callback to handle the 31187754SJeff.Bonwick@Sun.COM * accounting for any re-write attempt. 31195329Sgw25295 */ 31205329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 31215329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 31225329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 31235329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 31245329Sgw25295 hdr->b_freeze_cksum = NULL; 31255329Sgw25295 } 31265329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 31275329Sgw25295 } 31285450Sbrendan arc_cksum_compute(buf, B_FALSE); 31295329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 31303547Smaybee } 31313547Smaybee 31323547Smaybee static void 3133789Sahrens arc_write_done(zio_t *zio) 3134789Sahrens { 31353547Smaybee arc_write_callback_t *callback = zio->io_private; 31363547Smaybee arc_buf_t *buf = callback->awcb_buf; 31373547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3138789Sahrens 3139789Sahrens hdr->b_acb = NULL; 3140789Sahrens 3141789Sahrens hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3142789Sahrens hdr->b_birth = zio->io_bp->blk_birth; 3143789Sahrens hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 31441544Seschrock /* 31451544Seschrock * If the block to be written was all-zero, we may have 31461544Seschrock * compressed it away. In this case no write was performed 31471544Seschrock * so there will be no dva/birth-date/checksum. The buffer 31481544Seschrock * must therefor remain anonymous (and uncached). 31491544Seschrock */ 3150789Sahrens if (!BUF_EMPTY(hdr)) { 3151789Sahrens arc_buf_hdr_t *exists; 3152789Sahrens kmutex_t *hash_lock; 3153789Sahrens 31543093Sahrens arc_cksum_verify(buf); 31553093Sahrens 3156789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3157789Sahrens if (exists) { 3158789Sahrens /* 3159789Sahrens * This can only happen if we overwrite for 3160789Sahrens * sync-to-convergence, because we remove 3161789Sahrens * buffers from the hash table when we arc_free(). 3162789Sahrens */ 31637754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_IO_REWRITE); 3164789Sahrens ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig), 3165789Sahrens BP_IDENTITY(zio->io_bp))); 3166789Sahrens ASSERT3U(zio->io_bp_orig.blk_birth, ==, 3167789Sahrens zio->io_bp->blk_birth); 3168789Sahrens 3169789Sahrens ASSERT(refcount_is_zero(&exists->b_refcnt)); 31703403Sbmc arc_change_state(arc_anon, exists, hash_lock); 3171789Sahrens mutex_exit(hash_lock); 31721544Seschrock arc_hdr_destroy(exists); 3173789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3174789Sahrens ASSERT3P(exists, ==, NULL); 3175789Sahrens } 31761544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 31777046Sahrens /* if it's not anon, we are doing a scrub */ 31787046Sahrens if (hdr->b_state == arc_anon) 31797046Sahrens arc_access(hdr, hash_lock); 31802688Smaybee mutex_exit(hash_lock); 31813547Smaybee } else if (callback->awcb_done == NULL) { 31821544Seschrock int destroy_hdr; 31831544Seschrock /* 31841544Seschrock * This is an anonymous buffer with no user callback, 31851544Seschrock * destroy it if there are no active references. 31861544Seschrock */ 31871544Seschrock mutex_enter(&arc_eviction_mtx); 31881544Seschrock destroy_hdr = refcount_is_zero(&hdr->b_refcnt); 31891544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 31901544Seschrock mutex_exit(&arc_eviction_mtx); 31911544Seschrock if (destroy_hdr) 31921544Seschrock arc_hdr_destroy(hdr); 31931544Seschrock } else { 31941544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3195789Sahrens } 31967046Sahrens hdr->b_flags &= ~ARC_STORED; 31971544Seschrock 31983547Smaybee if (callback->awcb_done) { 3199789Sahrens ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 32003547Smaybee callback->awcb_done(zio, buf, callback->awcb_private); 3201789Sahrens } 3202789Sahrens 32033547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3204789Sahrens } 3205789Sahrens 32067872STim.Haley@Sun.COM void 32077754SJeff.Bonwick@Sun.COM write_policy(spa_t *spa, const writeprops_t *wp, zio_prop_t *zp) 32087046Sahrens { 32097046Sahrens boolean_t ismd = (wp->wp_level > 0 || dmu_ot[wp->wp_type].ot_metadata); 32107046Sahrens 32117046Sahrens /* Determine checksum setting */ 32127046Sahrens if (ismd) { 32137046Sahrens /* 32147046Sahrens * Metadata always gets checksummed. If the data 32157046Sahrens * checksum is multi-bit correctable, and it's not a 32167046Sahrens * ZBT-style checksum, then it's suitable for metadata 32177046Sahrens * as well. Otherwise, the metadata checksum defaults 32187046Sahrens * to fletcher4. 32197046Sahrens */ 32207046Sahrens if (zio_checksum_table[wp->wp_oschecksum].ci_correctable && 32217046Sahrens !zio_checksum_table[wp->wp_oschecksum].ci_zbt) 32227754SJeff.Bonwick@Sun.COM zp->zp_checksum = wp->wp_oschecksum; 32237046Sahrens else 32247754SJeff.Bonwick@Sun.COM zp->zp_checksum = ZIO_CHECKSUM_FLETCHER_4; 32257046Sahrens } else { 32267754SJeff.Bonwick@Sun.COM zp->zp_checksum = zio_checksum_select(wp->wp_dnchecksum, 32277046Sahrens wp->wp_oschecksum); 32287046Sahrens } 32297046Sahrens 32307046Sahrens /* Determine compression setting */ 32317046Sahrens if (ismd) { 32327046Sahrens /* 32337046Sahrens * XXX -- we should design a compression algorithm 32347046Sahrens * that specializes in arrays of bps. 32357046Sahrens */ 32367754SJeff.Bonwick@Sun.COM zp->zp_compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY : 32377046Sahrens ZIO_COMPRESS_LZJB; 32387046Sahrens } else { 32397754SJeff.Bonwick@Sun.COM zp->zp_compress = zio_compress_select(wp->wp_dncompress, 32407046Sahrens wp->wp_oscompress); 32417046Sahrens } 32427754SJeff.Bonwick@Sun.COM 32437754SJeff.Bonwick@Sun.COM zp->zp_type = wp->wp_type; 32447754SJeff.Bonwick@Sun.COM zp->zp_level = wp->wp_level; 32457754SJeff.Bonwick@Sun.COM zp->zp_ndvas = MIN(wp->wp_copies + ismd, spa_max_replication(spa)); 32467046Sahrens } 32477046Sahrens 32483547Smaybee zio_t * 32497046Sahrens arc_write(zio_t *pio, spa_t *spa, const writeprops_t *wp, 32507237Sek110237 boolean_t l2arc, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 32513547Smaybee arc_done_func_t *ready, arc_done_func_t *done, void *private, int priority, 32527237Sek110237 int zio_flags, const zbookmark_t *zb) 3253789Sahrens { 3254789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 32553547Smaybee arc_write_callback_t *callback; 32567754SJeff.Bonwick@Sun.COM zio_t *zio; 32577754SJeff.Bonwick@Sun.COM zio_prop_t zp; 32587754SJeff.Bonwick@Sun.COM 32597754SJeff.Bonwick@Sun.COM ASSERT(ready != NULL); 3260789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 32612237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 32622237Smaybee ASSERT(hdr->b_acb == 0); 32637237Sek110237 if (l2arc) 32647237Sek110237 hdr->b_flags |= ARC_L2CACHE; 32653547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 32663547Smaybee callback->awcb_ready = ready; 32673547Smaybee callback->awcb_done = done; 32683547Smaybee callback->awcb_private = private; 32693547Smaybee callback->awcb_buf = buf; 32707046Sahrens 32717754SJeff.Bonwick@Sun.COM write_policy(spa, wp, &zp); 32727754SJeff.Bonwick@Sun.COM zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, &zp, 32737754SJeff.Bonwick@Sun.COM arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3274789Sahrens 32753547Smaybee return (zio); 3276789Sahrens } 3277789Sahrens 3278789Sahrens int 3279789Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 3280789Sahrens zio_done_func_t *done, void *private, uint32_t arc_flags) 3281789Sahrens { 3282789Sahrens arc_buf_hdr_t *ab; 3283789Sahrens kmutex_t *hash_lock; 3284789Sahrens zio_t *zio; 32858636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 3286789Sahrens 3287789Sahrens /* 3288789Sahrens * If this buffer is in the cache, release it, so it 3289789Sahrens * can be re-used. 3290789Sahrens */ 32918636SMark.Maybee@Sun.COM ab = buf_hash_find(guid, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 3292789Sahrens if (ab != NULL) { 3293789Sahrens /* 3294789Sahrens * The checksum of blocks to free is not always 3295789Sahrens * preserved (eg. on the deadlist). However, if it is 3296789Sahrens * nonzero, it should match what we have in the cache. 3297789Sahrens */ 3298789Sahrens ASSERT(bp->blk_cksum.zc_word[0] == 0 || 32997754SJeff.Bonwick@Sun.COM bp->blk_cksum.zc_word[0] == ab->b_cksum0 || 33007754SJeff.Bonwick@Sun.COM bp->blk_fill == BLK_FILL_ALREADY_FREED); 33017754SJeff.Bonwick@Sun.COM 33023403Sbmc if (ab->b_state != arc_anon) 33033403Sbmc arc_change_state(arc_anon, ab, hash_lock); 33042391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 33052391Smaybee /* 33062391Smaybee * This should only happen when we prefetch. 33072391Smaybee */ 33082391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 33092391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 33102391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 33112391Smaybee if (HDR_IN_HASH_TABLE(ab)) 33122391Smaybee buf_hash_remove(ab); 33132391Smaybee ab->b_arc_access = 0; 33142391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 33152391Smaybee ab->b_birth = 0; 33162391Smaybee ab->b_cksum0 = 0; 33172391Smaybee ab->b_buf->b_efunc = NULL; 33182391Smaybee ab->b_buf->b_private = NULL; 33192391Smaybee mutex_exit(hash_lock); 33202391Smaybee } else if (refcount_is_zero(&ab->b_refcnt)) { 33215450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3322789Sahrens mutex_exit(hash_lock); 33231544Seschrock arc_hdr_destroy(ab); 33243403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3325789Sahrens } else { 33261589Smaybee /* 33272391Smaybee * We still have an active reference on this 33282391Smaybee * buffer. This can happen, e.g., from 33292391Smaybee * dbuf_unoverride(). 33301589Smaybee */ 33312391Smaybee ASSERT(!HDR_IN_HASH_TABLE(ab)); 3332789Sahrens ab->b_arc_access = 0; 3333789Sahrens bzero(&ab->b_dva, sizeof (dva_t)); 3334789Sahrens ab->b_birth = 0; 3335789Sahrens ab->b_cksum0 = 0; 33361544Seschrock ab->b_buf->b_efunc = NULL; 33371544Seschrock ab->b_buf->b_private = NULL; 3338789Sahrens mutex_exit(hash_lock); 3339789Sahrens } 3340789Sahrens } 3341789Sahrens 33427754SJeff.Bonwick@Sun.COM zio = zio_free(pio, spa, txg, bp, done, private, ZIO_FLAG_MUSTSUCCEED); 3343789Sahrens 3344789Sahrens if (arc_flags & ARC_WAIT) 3345789Sahrens return (zio_wait(zio)); 3346789Sahrens 3347789Sahrens ASSERT(arc_flags & ARC_NOWAIT); 3348789Sahrens zio_nowait(zio); 3349789Sahrens 3350789Sahrens return (0); 3351789Sahrens } 3352789Sahrens 33536245Smaybee static int 33549412SAleksandr.Guzovskiy@Sun.COM arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg) 33556245Smaybee { 33566245Smaybee #ifdef _KERNEL 33576245Smaybee uint64_t available_memory = ptob(freemem); 33586245Smaybee static uint64_t page_load = 0; 33596245Smaybee static uint64_t last_txg = 0; 33606245Smaybee 33616245Smaybee #if defined(__i386) 33626245Smaybee available_memory = 33636245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 33646245Smaybee #endif 33656245Smaybee if (available_memory >= zfs_write_limit_max) 33666245Smaybee return (0); 33676245Smaybee 33686245Smaybee if (txg > last_txg) { 33696245Smaybee last_txg = txg; 33706245Smaybee page_load = 0; 33716245Smaybee } 33726245Smaybee /* 33736245Smaybee * If we are in pageout, we know that memory is already tight, 33746245Smaybee * the arc is already going to be evicting, so we just want to 33756245Smaybee * continue to let page writes occur as quickly as possible. 33766245Smaybee */ 33776245Smaybee if (curproc == proc_pageout) { 33786245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 33796245Smaybee return (ERESTART); 33806245Smaybee /* Note: reserve is inflated, so we deflate */ 33816245Smaybee page_load += reserve / 8; 33826245Smaybee return (0); 33836245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 33846245Smaybee /* memory is low, delay before restarting */ 33856245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33866245Smaybee return (EAGAIN); 33876245Smaybee } 33886245Smaybee page_load = 0; 33896245Smaybee 33906245Smaybee if (arc_size > arc_c_min) { 33916245Smaybee uint64_t evictable_memory = 33926245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 33936245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 33946245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 33956245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 33966245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 33976245Smaybee } 33986245Smaybee 33996245Smaybee if (inflight_data > available_memory / 4) { 34006245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 34016245Smaybee return (ERESTART); 34026245Smaybee } 34036245Smaybee #endif 34046245Smaybee return (0); 34056245Smaybee } 34066245Smaybee 3407789Sahrens void 34086245Smaybee arc_tempreserve_clear(uint64_t reserve) 3409789Sahrens { 34106245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3411789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3412789Sahrens } 3413789Sahrens 3414789Sahrens int 34156245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3416789Sahrens { 34176245Smaybee int error; 34189412SAleksandr.Guzovskiy@Sun.COM uint64_t anon_size; 34196245Smaybee 3420789Sahrens #ifdef ZFS_DEBUG 3421789Sahrens /* 3422789Sahrens * Once in a while, fail for no reason. Everything should cope. 3423789Sahrens */ 3424789Sahrens if (spa_get_random(10000) == 0) { 3425789Sahrens dprintf("forcing random failure\n"); 3426789Sahrens return (ERESTART); 3427789Sahrens } 3428789Sahrens #endif 34296245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 34306245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 34316245Smaybee if (reserve > arc_c) 3432982Smaybee return (ENOMEM); 3433982Smaybee 3434789Sahrens /* 34359412SAleksandr.Guzovskiy@Sun.COM * Don't count loaned bufs as in flight dirty data to prevent long 34369412SAleksandr.Guzovskiy@Sun.COM * network delays from blocking transactions that are ready to be 34379412SAleksandr.Guzovskiy@Sun.COM * assigned to a txg. 34389412SAleksandr.Guzovskiy@Sun.COM */ 34399412SAleksandr.Guzovskiy@Sun.COM anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 34409412SAleksandr.Guzovskiy@Sun.COM 34419412SAleksandr.Guzovskiy@Sun.COM /* 34426245Smaybee * Writes will, almost always, require additional memory allocations 34436245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 34446245Smaybee * make sure that there is sufficient available memory for this. 34456245Smaybee */ 34469412SAleksandr.Guzovskiy@Sun.COM if (error = arc_memory_throttle(reserve, anon_size, txg)) 34476245Smaybee return (error); 34486245Smaybee 34496245Smaybee /* 3450982Smaybee * Throttle writes when the amount of dirty data in the cache 3451982Smaybee * gets too large. We try to keep the cache less than half full 3452982Smaybee * of dirty blocks so that our sync times don't grow too large. 3453982Smaybee * Note: if two requests come in concurrently, we might let them 3454982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3455789Sahrens */ 34569412SAleksandr.Guzovskiy@Sun.COM 34579412SAleksandr.Guzovskiy@Sun.COM if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 34589412SAleksandr.Guzovskiy@Sun.COM anon_size > arc_c / 4) { 34594309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 34604309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 34614309Smaybee arc_tempreserve>>10, 34624309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 34634309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 34646245Smaybee reserve>>10, arc_c>>10); 3465789Sahrens return (ERESTART); 3466789Sahrens } 34676245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3468789Sahrens return (0); 3469789Sahrens } 3470789Sahrens 3471789Sahrens void 3472789Sahrens arc_init(void) 3473789Sahrens { 3474789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3475789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3476789Sahrens 34772391Smaybee /* Convert seconds to clock ticks */ 34782638Sperrin arc_min_prefetch_lifespan = 1 * hz; 34792391Smaybee 3480789Sahrens /* Start out with 1/8 of all memory */ 34813403Sbmc arc_c = physmem * PAGESIZE / 8; 3482789Sahrens 3483789Sahrens #ifdef _KERNEL 3484789Sahrens /* 3485789Sahrens * On architectures where the physical memory can be larger 3486789Sahrens * than the addressable space (intel in 32-bit mode), we may 3487789Sahrens * need to limit the cache to 1/8 of VM size. 3488789Sahrens */ 34893403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3490789Sahrens #endif 3491789Sahrens 3492982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 34933403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3494982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 34953403Sbmc if (arc_c * 8 >= 1<<30) 34963403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3497789Sahrens else 34983403Sbmc arc_c_max = arc_c_min; 34993403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 35002885Sahrens 35012885Sahrens /* 35022885Sahrens * Allow the tunables to override our calculations if they are 35032885Sahrens * reasonable (ie. over 64MB) 35042885Sahrens */ 35052885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 35063403Sbmc arc_c_max = zfs_arc_max; 35073403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 35083403Sbmc arc_c_min = zfs_arc_min; 35092885Sahrens 35103403Sbmc arc_c = arc_c_max; 35113403Sbmc arc_p = (arc_c >> 1); 3512789Sahrens 35134309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 35144309Smaybee arc_meta_limit = arc_c_max / 4; 35154645Sek110237 35164645Sek110237 /* Allow the tunable to override if it is reasonable */ 35174645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 35184645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 35194645Sek110237 35204309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 35214309Smaybee arc_c_min = arc_meta_limit / 2; 35224309Smaybee 35238582SBrendan.Gregg@Sun.COM if (zfs_arc_grow_retry > 0) 35248582SBrendan.Gregg@Sun.COM arc_grow_retry = zfs_arc_grow_retry; 35258582SBrendan.Gregg@Sun.COM 35268582SBrendan.Gregg@Sun.COM if (zfs_arc_shrink_shift > 0) 35278582SBrendan.Gregg@Sun.COM arc_shrink_shift = zfs_arc_shrink_shift; 35288582SBrendan.Gregg@Sun.COM 35298582SBrendan.Gregg@Sun.COM if (zfs_arc_p_min_shift > 0) 35308582SBrendan.Gregg@Sun.COM arc_p_min_shift = zfs_arc_p_min_shift; 35318582SBrendan.Gregg@Sun.COM 3532789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3533789Sahrens if (kmem_debugging()) 35343403Sbmc arc_c = arc_c / 2; 35353403Sbmc if (arc_c < arc_c_min) 35363403Sbmc arc_c = arc_c_min; 3537789Sahrens 35383403Sbmc arc_anon = &ARC_anon; 35393403Sbmc arc_mru = &ARC_mru; 35403403Sbmc arc_mru_ghost = &ARC_mru_ghost; 35413403Sbmc arc_mfu = &ARC_mfu; 35423403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 35435450Sbrendan arc_l2c_only = &ARC_l2c_only; 35443403Sbmc arc_size = 0; 3545789Sahrens 35463403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35473403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35483403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35493403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35503403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35515450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35522688Smaybee 35534309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 35544309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35554309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 35564309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35574309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 35584309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35594309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 35604309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35614309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 35624309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35634309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 35644309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35654309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 35664309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35674309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 35684309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35695450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 35705450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35715450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 35725450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3573789Sahrens 3574789Sahrens buf_init(); 3575789Sahrens 3576789Sahrens arc_thread_exit = 0; 35771544Seschrock arc_eviction_list = NULL; 35781544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 35792887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3580789Sahrens 35813403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 35823403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 35833403Sbmc 35843403Sbmc if (arc_ksp != NULL) { 35853403Sbmc arc_ksp->ks_data = &arc_stats; 35863403Sbmc kstat_install(arc_ksp); 35873403Sbmc } 35883403Sbmc 3589789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3590789Sahrens TS_RUN, minclsyspri); 35913158Smaybee 35923158Smaybee arc_dead = FALSE; 35936987Sbrendan arc_warm = B_FALSE; 35946245Smaybee 35956245Smaybee if (zfs_write_limit_max == 0) 35967468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 35976245Smaybee else 35986245Smaybee zfs_write_limit_shift = 0; 35997468SMark.Maybee@Sun.COM mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3600789Sahrens } 3601789Sahrens 3602789Sahrens void 3603789Sahrens arc_fini(void) 3604789Sahrens { 3605789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3606789Sahrens arc_thread_exit = 1; 3607789Sahrens while (arc_thread_exit != 0) 3608789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3609789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3610789Sahrens 36115642Smaybee arc_flush(NULL); 3612789Sahrens 3613789Sahrens arc_dead = TRUE; 3614789Sahrens 36153403Sbmc if (arc_ksp != NULL) { 36163403Sbmc kstat_delete(arc_ksp); 36173403Sbmc arc_ksp = NULL; 36183403Sbmc } 36193403Sbmc 36201544Seschrock mutex_destroy(&arc_eviction_mtx); 3621789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3622789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3623789Sahrens 36244309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 36254309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 36264309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 36274309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 36284309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 36294309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 36304309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 36314309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3632789Sahrens 36333403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 36343403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 36353403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 36363403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 36373403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 36388214SRicardo.M.Correia@Sun.COM mutex_destroy(&arc_l2c_only->arcs_mtx); 36392856Snd150628 36407468SMark.Maybee@Sun.COM mutex_destroy(&zfs_write_limit_lock); 36417468SMark.Maybee@Sun.COM 3642789Sahrens buf_fini(); 36439412SAleksandr.Guzovskiy@Sun.COM 36449412SAleksandr.Guzovskiy@Sun.COM ASSERT(arc_loaned_bytes == 0); 3645789Sahrens } 36465450Sbrendan 36475450Sbrendan /* 36485450Sbrendan * Level 2 ARC 36495450Sbrendan * 36505450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 36515450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 36525450Sbrendan * using large infrequent writes. The main role of this cache is to boost 36535450Sbrendan * the performance of random read workloads. The intended L2ARC devices 36545450Sbrendan * include short-stroked disks, solid state disks, and other media with 36555450Sbrendan * substantially faster read latency than disk. 36565450Sbrendan * 36575450Sbrendan * +-----------------------+ 36585450Sbrendan * | ARC | 36595450Sbrendan * +-----------------------+ 36605450Sbrendan * | ^ ^ 36615450Sbrendan * | | | 36625450Sbrendan * l2arc_feed_thread() arc_read() 36635450Sbrendan * | | | 36645450Sbrendan * | l2arc read | 36655450Sbrendan * V | | 36665450Sbrendan * +---------------+ | 36675450Sbrendan * | L2ARC | | 36685450Sbrendan * +---------------+ | 36695450Sbrendan * | ^ | 36705450Sbrendan * l2arc_write() | | 36715450Sbrendan * | | | 36725450Sbrendan * V | | 36735450Sbrendan * +-------+ +-------+ 36745450Sbrendan * | vdev | | vdev | 36755450Sbrendan * | cache | | cache | 36765450Sbrendan * +-------+ +-------+ 36775450Sbrendan * +=========+ .-----. 36785450Sbrendan * : L2ARC : |-_____-| 36795450Sbrendan * : devices : | Disks | 36805450Sbrendan * +=========+ `-_____-' 36815450Sbrendan * 36825450Sbrendan * Read requests are satisfied from the following sources, in order: 36835450Sbrendan * 36845450Sbrendan * 1) ARC 36855450Sbrendan * 2) vdev cache of L2ARC devices 36865450Sbrendan * 3) L2ARC devices 36875450Sbrendan * 4) vdev cache of disks 36885450Sbrendan * 5) disks 36895450Sbrendan * 36905450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 36915450Sbrendan * To accommodate for this there are some significant differences between 36925450Sbrendan * the L2ARC and traditional cache design: 36935450Sbrendan * 36945450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 36955450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 36965450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 36975450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 36985450Sbrendan * 36995450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 37005450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 37015450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 37025450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 37035450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 37045450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 37055450Sbrendan * provide a better sense of ratio than this diagram: 37065450Sbrendan * 37075450Sbrendan * head --> tail 37085450Sbrendan * +---------------------+----------+ 37095450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 37105450Sbrendan * +---------------------+----------+ | o L2ARC eligible 37115450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 37125450Sbrendan * +---------------------+----------+ | 37135450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 37145450Sbrendan * headroom | 37155450Sbrendan * l2arc_feed_thread() 37165450Sbrendan * | 37175450Sbrendan * l2arc write hand <--[oooo]--' 37185450Sbrendan * | 8 Mbyte 37195450Sbrendan * | write max 37205450Sbrendan * V 37215450Sbrendan * +==============================+ 37225450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 37235450Sbrendan * +==============================+ 37245450Sbrendan * 32 Gbytes 37255450Sbrendan * 37265450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 37275450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 37285450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 37295450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 37305450Sbrendan * the ARC lists have moved there due to inactivity. 37315450Sbrendan * 37325450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 37335450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 37345450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 37355450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 37365450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 37375450Sbrendan * quickly, such as during backups of the entire pool. 37385450Sbrendan * 37396987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 37406987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 37416987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 37426987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 37436987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 37446987Sbrendan * 37456987Sbrendan * The L2ARC device write speed is also boosted during this time so that 37466987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 37476987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 37486987Sbrendan * through increased writes. 37496987Sbrendan * 37506987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 37515450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 37525450Sbrendan * device is written to in a rotor fashion, sweeping writes through 37535450Sbrendan * available space then repeating. 37545450Sbrendan * 37556987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 37565450Sbrendan * write buffers back to disk based storage. 37575450Sbrendan * 37586987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 37595450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 37605450Sbrendan * 37615450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 37625450Sbrendan * may be necessary for different workloads: 37635450Sbrendan * 37645450Sbrendan * l2arc_write_max max write bytes per interval 37656987Sbrendan * l2arc_write_boost extra write bytes during device warmup 37665450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 37675450Sbrendan * l2arc_headroom number of max device writes to precache 37685450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 37695450Sbrendan * 37705450Sbrendan * Tunables may be removed or added as future performance improvements are 37715450Sbrendan * integrated, and also may become zpool properties. 37728582SBrendan.Gregg@Sun.COM * 37738582SBrendan.Gregg@Sun.COM * There are three key functions that control how the L2ARC warms up: 37748582SBrendan.Gregg@Sun.COM * 37758582SBrendan.Gregg@Sun.COM * l2arc_write_eligible() check if a buffer is eligible to cache 37768582SBrendan.Gregg@Sun.COM * l2arc_write_size() calculate how much to write 37778582SBrendan.Gregg@Sun.COM * l2arc_write_interval() calculate sleep delay between writes 37788582SBrendan.Gregg@Sun.COM * 37798582SBrendan.Gregg@Sun.COM * These three functions determine what to write, how much, and how quickly 37808582SBrendan.Gregg@Sun.COM * to send writes. 37815450Sbrendan */ 37825450Sbrendan 37838582SBrendan.Gregg@Sun.COM static boolean_t 37848636SMark.Maybee@Sun.COM l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 37858582SBrendan.Gregg@Sun.COM { 37868582SBrendan.Gregg@Sun.COM /* 37878582SBrendan.Gregg@Sun.COM * A buffer is *not* eligible for the L2ARC if it: 37888582SBrendan.Gregg@Sun.COM * 1. belongs to a different spa. 37898582SBrendan.Gregg@Sun.COM * 2. has no attached buffer. 37908582SBrendan.Gregg@Sun.COM * 3. is already cached on the L2ARC. 37918582SBrendan.Gregg@Sun.COM * 4. has an I/O in progress (it may be an incomplete read). 37928582SBrendan.Gregg@Sun.COM * 5. is flagged not eligible (zfs property). 37938582SBrendan.Gregg@Sun.COM */ 37948636SMark.Maybee@Sun.COM if (ab->b_spa != spa_guid || ab->b_buf == NULL || ab->b_l2hdr != NULL || 37958582SBrendan.Gregg@Sun.COM HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) 37968582SBrendan.Gregg@Sun.COM return (B_FALSE); 37978582SBrendan.Gregg@Sun.COM 37988582SBrendan.Gregg@Sun.COM return (B_TRUE); 37998582SBrendan.Gregg@Sun.COM } 38008582SBrendan.Gregg@Sun.COM 38018582SBrendan.Gregg@Sun.COM static uint64_t 38028582SBrendan.Gregg@Sun.COM l2arc_write_size(l2arc_dev_t *dev) 38038582SBrendan.Gregg@Sun.COM { 38048582SBrendan.Gregg@Sun.COM uint64_t size; 38058582SBrendan.Gregg@Sun.COM 38068582SBrendan.Gregg@Sun.COM size = dev->l2ad_write; 38078582SBrendan.Gregg@Sun.COM 38088582SBrendan.Gregg@Sun.COM if (arc_warm == B_FALSE) 38098582SBrendan.Gregg@Sun.COM size += dev->l2ad_boost; 38108582SBrendan.Gregg@Sun.COM 38118582SBrendan.Gregg@Sun.COM return (size); 38128582SBrendan.Gregg@Sun.COM 38138582SBrendan.Gregg@Sun.COM } 38148582SBrendan.Gregg@Sun.COM 38158582SBrendan.Gregg@Sun.COM static clock_t 38168582SBrendan.Gregg@Sun.COM l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 38178582SBrendan.Gregg@Sun.COM { 38188582SBrendan.Gregg@Sun.COM clock_t interval, next; 38198582SBrendan.Gregg@Sun.COM 38208582SBrendan.Gregg@Sun.COM /* 38218582SBrendan.Gregg@Sun.COM * If the ARC lists are busy, increase our write rate; if the 38228582SBrendan.Gregg@Sun.COM * lists are stale, idle back. This is achieved by checking 38238582SBrendan.Gregg@Sun.COM * how much we previously wrote - if it was more than half of 38248582SBrendan.Gregg@Sun.COM * what we wanted, schedule the next write much sooner. 38258582SBrendan.Gregg@Sun.COM */ 38268582SBrendan.Gregg@Sun.COM if (l2arc_feed_again && wrote > (wanted / 2)) 38278582SBrendan.Gregg@Sun.COM interval = (hz * l2arc_feed_min_ms) / 1000; 38288582SBrendan.Gregg@Sun.COM else 38298582SBrendan.Gregg@Sun.COM interval = hz * l2arc_feed_secs; 38308582SBrendan.Gregg@Sun.COM 38318582SBrendan.Gregg@Sun.COM next = MAX(lbolt, MIN(lbolt + interval, began + interval)); 38328582SBrendan.Gregg@Sun.COM 38338582SBrendan.Gregg@Sun.COM return (next); 38348582SBrendan.Gregg@Sun.COM } 38358582SBrendan.Gregg@Sun.COM 38365450Sbrendan static void 38375450Sbrendan l2arc_hdr_stat_add(void) 38385450Sbrendan { 38396018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 38406018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 38415450Sbrendan } 38425450Sbrendan 38435450Sbrendan static void 38445450Sbrendan l2arc_hdr_stat_remove(void) 38455450Sbrendan { 38466018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 38476018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 38485450Sbrendan } 38495450Sbrendan 38505450Sbrendan /* 38515450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 38526987Sbrendan * If a device is returned, this also returns holding the spa config lock. 38535450Sbrendan */ 38545450Sbrendan static l2arc_dev_t * 38555450Sbrendan l2arc_dev_get_next(void) 38565450Sbrendan { 38576987Sbrendan l2arc_dev_t *first, *next = NULL; 38586987Sbrendan 38596987Sbrendan /* 38606987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 38616987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 38626987Sbrendan * both locks will be dropped and a spa config lock held instead. 38636987Sbrendan */ 38646987Sbrendan mutex_enter(&spa_namespace_lock); 38656987Sbrendan mutex_enter(&l2arc_dev_mtx); 38666643Seschrock 38676643Seschrock /* if there are no vdevs, there is nothing to do */ 38686643Seschrock if (l2arc_ndev == 0) 38696987Sbrendan goto out; 38706643Seschrock 38716643Seschrock first = NULL; 38726643Seschrock next = l2arc_dev_last; 38736643Seschrock do { 38746643Seschrock /* loop around the list looking for a non-faulted vdev */ 38756643Seschrock if (next == NULL) { 38765450Sbrendan next = list_head(l2arc_dev_list); 38776643Seschrock } else { 38786643Seschrock next = list_next(l2arc_dev_list, next); 38796643Seschrock if (next == NULL) 38806643Seschrock next = list_head(l2arc_dev_list); 38816643Seschrock } 38826643Seschrock 38836643Seschrock /* if we have come back to the start, bail out */ 38846643Seschrock if (first == NULL) 38856643Seschrock first = next; 38866643Seschrock else if (next == first) 38876643Seschrock break; 38886643Seschrock 38896643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 38906643Seschrock 38916643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 38926643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 38936987Sbrendan next = NULL; 38945450Sbrendan 38955450Sbrendan l2arc_dev_last = next; 38965450Sbrendan 38976987Sbrendan out: 38986987Sbrendan mutex_exit(&l2arc_dev_mtx); 38996987Sbrendan 39006987Sbrendan /* 39016987Sbrendan * Grab the config lock to prevent the 'next' device from being 39026987Sbrendan * removed while we are writing to it. 39036987Sbrendan */ 39046987Sbrendan if (next != NULL) 39057754SJeff.Bonwick@Sun.COM spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 39066987Sbrendan mutex_exit(&spa_namespace_lock); 39076987Sbrendan 39085450Sbrendan return (next); 39095450Sbrendan } 39105450Sbrendan 39115450Sbrendan /* 39126987Sbrendan * Free buffers that were tagged for destruction. 39136987Sbrendan */ 39146987Sbrendan static void 39156987Sbrendan l2arc_do_free_on_write() 39166987Sbrendan { 39176987Sbrendan list_t *buflist; 39186987Sbrendan l2arc_data_free_t *df, *df_prev; 39196987Sbrendan 39206987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 39216987Sbrendan buflist = l2arc_free_on_write; 39226987Sbrendan 39236987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 39246987Sbrendan df_prev = list_prev(buflist, df); 39256987Sbrendan ASSERT(df->l2df_data != NULL); 39266987Sbrendan ASSERT(df->l2df_func != NULL); 39276987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 39286987Sbrendan list_remove(buflist, df); 39296987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 39306987Sbrendan } 39316987Sbrendan 39326987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 39336987Sbrendan } 39346987Sbrendan 39356987Sbrendan /* 39365450Sbrendan * A write to a cache device has completed. Update all headers to allow 39375450Sbrendan * reads from these buffers to begin. 39385450Sbrendan */ 39395450Sbrendan static void 39405450Sbrendan l2arc_write_done(zio_t *zio) 39415450Sbrendan { 39425450Sbrendan l2arc_write_callback_t *cb; 39435450Sbrendan l2arc_dev_t *dev; 39445450Sbrendan list_t *buflist; 39455450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 39466987Sbrendan l2arc_buf_hdr_t *abl2; 39475450Sbrendan kmutex_t *hash_lock; 39485450Sbrendan 39495450Sbrendan cb = zio->io_private; 39505450Sbrendan ASSERT(cb != NULL); 39515450Sbrendan dev = cb->l2wcb_dev; 39525450Sbrendan ASSERT(dev != NULL); 39535450Sbrendan head = cb->l2wcb_head; 39545450Sbrendan ASSERT(head != NULL); 39555450Sbrendan buflist = dev->l2ad_buflist; 39565450Sbrendan ASSERT(buflist != NULL); 39575450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 39585450Sbrendan l2arc_write_callback_t *, cb); 39595450Sbrendan 39605450Sbrendan if (zio->io_error != 0) 39615450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 39625450Sbrendan 39635450Sbrendan mutex_enter(&l2arc_buflist_mtx); 39645450Sbrendan 39655450Sbrendan /* 39665450Sbrendan * All writes completed, or an error was hit. 39675450Sbrendan */ 39685450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 39695450Sbrendan ab_prev = list_prev(buflist, ab); 39705450Sbrendan 39715450Sbrendan hash_lock = HDR_LOCK(ab); 39725450Sbrendan if (!mutex_tryenter(hash_lock)) { 39735450Sbrendan /* 39745450Sbrendan * This buffer misses out. It may be in a stage 39755450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 39765450Sbrendan * left set, denying reads to this buffer. 39775450Sbrendan */ 39785450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 39795450Sbrendan continue; 39805450Sbrendan } 39815450Sbrendan 39825450Sbrendan if (zio->io_error != 0) { 39835450Sbrendan /* 39846987Sbrendan * Error - drop L2ARC entry. 39855450Sbrendan */ 39866987Sbrendan list_remove(buflist, ab); 39876987Sbrendan abl2 = ab->b_l2hdr; 39885450Sbrendan ab->b_l2hdr = NULL; 39896987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 39906987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 39915450Sbrendan } 39925450Sbrendan 39935450Sbrendan /* 39945450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 39955450Sbrendan */ 39965450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 39975450Sbrendan 39985450Sbrendan mutex_exit(hash_lock); 39995450Sbrendan } 40005450Sbrendan 40015450Sbrendan atomic_inc_64(&l2arc_writes_done); 40025450Sbrendan list_remove(buflist, head); 40035450Sbrendan kmem_cache_free(hdr_cache, head); 40045450Sbrendan mutex_exit(&l2arc_buflist_mtx); 40055450Sbrendan 40066987Sbrendan l2arc_do_free_on_write(); 40075450Sbrendan 40085450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 40095450Sbrendan } 40105450Sbrendan 40115450Sbrendan /* 40125450Sbrendan * A read to a cache device completed. Validate buffer contents before 40135450Sbrendan * handing over to the regular ARC routines. 40145450Sbrendan */ 40155450Sbrendan static void 40165450Sbrendan l2arc_read_done(zio_t *zio) 40175450Sbrendan { 40185450Sbrendan l2arc_read_callback_t *cb; 40195450Sbrendan arc_buf_hdr_t *hdr; 40205450Sbrendan arc_buf_t *buf; 40215450Sbrendan kmutex_t *hash_lock; 40226987Sbrendan int equal; 40235450Sbrendan 40247754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd != NULL); 40257754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 40267754SJeff.Bonwick@Sun.COM 40277754SJeff.Bonwick@Sun.COM spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 40287754SJeff.Bonwick@Sun.COM 40295450Sbrendan cb = zio->io_private; 40305450Sbrendan ASSERT(cb != NULL); 40315450Sbrendan buf = cb->l2rcb_buf; 40325450Sbrendan ASSERT(buf != NULL); 40335450Sbrendan hdr = buf->b_hdr; 40345450Sbrendan ASSERT(hdr != NULL); 40355450Sbrendan 40365450Sbrendan hash_lock = HDR_LOCK(hdr); 40375450Sbrendan mutex_enter(hash_lock); 40385450Sbrendan 40395450Sbrendan /* 40405450Sbrendan * Check this survived the L2ARC journey. 40415450Sbrendan */ 40425450Sbrendan equal = arc_cksum_equal(buf); 40435450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 40445450Sbrendan mutex_exit(hash_lock); 40455450Sbrendan zio->io_private = buf; 40467754SJeff.Bonwick@Sun.COM zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 40477754SJeff.Bonwick@Sun.COM zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 40485450Sbrendan arc_read_done(zio); 40495450Sbrendan } else { 40505450Sbrendan mutex_exit(hash_lock); 40515450Sbrendan /* 40525450Sbrendan * Buffer didn't survive caching. Increment stats and 40535450Sbrendan * reissue to the original storage device. 40545450Sbrendan */ 40556987Sbrendan if (zio->io_error != 0) { 40565450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 40576987Sbrendan } else { 40586987Sbrendan zio->io_error = EIO; 40596987Sbrendan } 40605450Sbrendan if (!equal) 40615450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 40625450Sbrendan 40637754SJeff.Bonwick@Sun.COM /* 40647754SJeff.Bonwick@Sun.COM * If there's no waiter, issue an async i/o to the primary 40657754SJeff.Bonwick@Sun.COM * storage now. If there *is* a waiter, the caller must 40667754SJeff.Bonwick@Sun.COM * issue the i/o in a context where it's OK to block. 40677754SJeff.Bonwick@Sun.COM */ 40688632SBill.Moore@Sun.COM if (zio->io_waiter == NULL) { 40698632SBill.Moore@Sun.COM zio_t *pio = zio_unique_parent(zio); 40708632SBill.Moore@Sun.COM 40718632SBill.Moore@Sun.COM ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 40728632SBill.Moore@Sun.COM 40738632SBill.Moore@Sun.COM zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 40747754SJeff.Bonwick@Sun.COM buf->b_data, zio->io_size, arc_read_done, buf, 40757754SJeff.Bonwick@Sun.COM zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 40768632SBill.Moore@Sun.COM } 40775450Sbrendan } 40785450Sbrendan 40795450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 40805450Sbrendan } 40815450Sbrendan 40825450Sbrendan /* 40835450Sbrendan * This is the list priority from which the L2ARC will search for pages to 40845450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 40855450Sbrendan * desired order. This order can have a significant effect on cache 40865450Sbrendan * performance. 40875450Sbrendan * 40885450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 40895450Sbrendan * the data lists. This function returns a locked list, and also returns 40905450Sbrendan * the lock pointer. 40915450Sbrendan */ 40925450Sbrendan static list_t * 40935450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 40945450Sbrendan { 40955450Sbrendan list_t *list; 40965450Sbrendan 40975450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 40985450Sbrendan 40995450Sbrendan switch (list_num) { 41005450Sbrendan case 0: 41015450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 41025450Sbrendan *lock = &arc_mfu->arcs_mtx; 41035450Sbrendan break; 41045450Sbrendan case 1: 41055450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 41065450Sbrendan *lock = &arc_mru->arcs_mtx; 41075450Sbrendan break; 41085450Sbrendan case 2: 41095450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 41105450Sbrendan *lock = &arc_mfu->arcs_mtx; 41115450Sbrendan break; 41125450Sbrendan case 3: 41135450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 41145450Sbrendan *lock = &arc_mru->arcs_mtx; 41155450Sbrendan break; 41165450Sbrendan } 41175450Sbrendan 41185450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 41195450Sbrendan mutex_enter(*lock); 41205450Sbrendan return (list); 41215450Sbrendan } 41225450Sbrendan 41235450Sbrendan /* 41245450Sbrendan * Evict buffers from the device write hand to the distance specified in 41255450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 41265450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 41275450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 41285450Sbrendan */ 41295450Sbrendan static void 41305450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 41315450Sbrendan { 41325450Sbrendan list_t *buflist; 41335450Sbrendan l2arc_buf_hdr_t *abl2; 41345450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 41355450Sbrendan kmutex_t *hash_lock; 41365450Sbrendan uint64_t taddr; 41375450Sbrendan 41385450Sbrendan buflist = dev->l2ad_buflist; 41395450Sbrendan 41405450Sbrendan if (buflist == NULL) 41415450Sbrendan return; 41425450Sbrendan 41435450Sbrendan if (!all && dev->l2ad_first) { 41445450Sbrendan /* 41455450Sbrendan * This is the first sweep through the device. There is 41465450Sbrendan * nothing to evict. 41475450Sbrendan */ 41485450Sbrendan return; 41495450Sbrendan } 41505450Sbrendan 41516987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 41525450Sbrendan /* 41535450Sbrendan * When nearing the end of the device, evict to the end 41545450Sbrendan * before the device write hand jumps to the start. 41555450Sbrendan */ 41565450Sbrendan taddr = dev->l2ad_end; 41575450Sbrendan } else { 41585450Sbrendan taddr = dev->l2ad_hand + distance; 41595450Sbrendan } 41605450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 41615450Sbrendan uint64_t, taddr, boolean_t, all); 41625450Sbrendan 41635450Sbrendan top: 41645450Sbrendan mutex_enter(&l2arc_buflist_mtx); 41655450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 41665450Sbrendan ab_prev = list_prev(buflist, ab); 41675450Sbrendan 41685450Sbrendan hash_lock = HDR_LOCK(ab); 41695450Sbrendan if (!mutex_tryenter(hash_lock)) { 41705450Sbrendan /* 41715450Sbrendan * Missed the hash lock. Retry. 41725450Sbrendan */ 41735450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 41745450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41755450Sbrendan mutex_enter(hash_lock); 41765450Sbrendan mutex_exit(hash_lock); 41775450Sbrendan goto top; 41785450Sbrendan } 41795450Sbrendan 41805450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 41815450Sbrendan /* 41825450Sbrendan * We hit a write head node. Leave it for 41835450Sbrendan * l2arc_write_done(). 41845450Sbrendan */ 41855450Sbrendan list_remove(buflist, ab); 41865450Sbrendan mutex_exit(hash_lock); 41875450Sbrendan continue; 41885450Sbrendan } 41895450Sbrendan 41905450Sbrendan if (!all && ab->b_l2hdr != NULL && 41915450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 41925450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 41935450Sbrendan /* 41945450Sbrendan * We've evicted to the target address, 41955450Sbrendan * or the end of the device. 41965450Sbrendan */ 41975450Sbrendan mutex_exit(hash_lock); 41985450Sbrendan break; 41995450Sbrendan } 42005450Sbrendan 42015450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 42025450Sbrendan /* 42035450Sbrendan * Already on the path to destruction. 42045450Sbrendan */ 42055450Sbrendan mutex_exit(hash_lock); 42065450Sbrendan continue; 42075450Sbrendan } 42085450Sbrendan 42095450Sbrendan if (ab->b_state == arc_l2c_only) { 42105450Sbrendan ASSERT(!HDR_L2_READING(ab)); 42115450Sbrendan /* 42125450Sbrendan * This doesn't exist in the ARC. Destroy. 42135450Sbrendan * arc_hdr_destroy() will call list_remove() 42145450Sbrendan * and decrement arcstat_l2_size. 42155450Sbrendan */ 42165450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 42175450Sbrendan arc_hdr_destroy(ab); 42185450Sbrendan } else { 42195450Sbrendan /* 42206987Sbrendan * Invalidate issued or about to be issued 42216987Sbrendan * reads, since we may be about to write 42226987Sbrendan * over this location. 42236987Sbrendan */ 42246987Sbrendan if (HDR_L2_READING(ab)) { 42256987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 42266987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 42276987Sbrendan } 42286987Sbrendan 42296987Sbrendan /* 42305450Sbrendan * Tell ARC this no longer exists in L2ARC. 42315450Sbrendan */ 42325450Sbrendan if (ab->b_l2hdr != NULL) { 42335450Sbrendan abl2 = ab->b_l2hdr; 42345450Sbrendan ab->b_l2hdr = NULL; 42355450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 42365450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 42375450Sbrendan } 42385450Sbrendan list_remove(buflist, ab); 42395450Sbrendan 42405450Sbrendan /* 42415450Sbrendan * This may have been leftover after a 42425450Sbrendan * failed write. 42435450Sbrendan */ 42445450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 42455450Sbrendan } 42465450Sbrendan mutex_exit(hash_lock); 42475450Sbrendan } 42485450Sbrendan mutex_exit(&l2arc_buflist_mtx); 42495450Sbrendan 42505450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, -(taddr - dev->l2ad_evict)); 42515450Sbrendan dev->l2ad_evict = taddr; 42525450Sbrendan } 42535450Sbrendan 42545450Sbrendan /* 42555450Sbrendan * Find and write ARC buffers to the L2ARC device. 42565450Sbrendan * 42575450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 42585450Sbrendan * for reading until they have completed writing. 42595450Sbrendan */ 42608582SBrendan.Gregg@Sun.COM static uint64_t 42616987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 42625450Sbrendan { 42635450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 42645450Sbrendan l2arc_buf_hdr_t *hdrl2; 42655450Sbrendan list_t *list; 42666987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 42675450Sbrendan void *buf_data; 42685450Sbrendan kmutex_t *hash_lock, *list_lock; 42695450Sbrendan boolean_t have_lock, full; 42705450Sbrendan l2arc_write_callback_t *cb; 42715450Sbrendan zio_t *pio, *wzio; 42728636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 42735450Sbrendan 42745450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 42755450Sbrendan 42765450Sbrendan pio = NULL; 42775450Sbrendan write_sz = 0; 42785450Sbrendan full = B_FALSE; 42796245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 42805450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 42815450Sbrendan 42825450Sbrendan /* 42835450Sbrendan * Copy buffers for L2ARC writing. 42845450Sbrendan */ 42855450Sbrendan mutex_enter(&l2arc_buflist_mtx); 42865450Sbrendan for (int try = 0; try <= 3; try++) { 42875450Sbrendan list = l2arc_list_locked(try, &list_lock); 42885450Sbrendan passed_sz = 0; 42895450Sbrendan 42906987Sbrendan /* 42916987Sbrendan * L2ARC fast warmup. 42926987Sbrendan * 42936987Sbrendan * Until the ARC is warm and starts to evict, read from the 42946987Sbrendan * head of the ARC lists rather than the tail. 42956987Sbrendan */ 42966987Sbrendan headroom = target_sz * l2arc_headroom; 42976987Sbrendan if (arc_warm == B_FALSE) 42986987Sbrendan ab = list_head(list); 42996987Sbrendan else 43006987Sbrendan ab = list_tail(list); 43016987Sbrendan 43026987Sbrendan for (; ab; ab = ab_prev) { 43036987Sbrendan if (arc_warm == B_FALSE) 43046987Sbrendan ab_prev = list_next(list, ab); 43056987Sbrendan else 43066987Sbrendan ab_prev = list_prev(list, ab); 43075450Sbrendan 43085450Sbrendan hash_lock = HDR_LOCK(ab); 43095450Sbrendan have_lock = MUTEX_HELD(hash_lock); 43105450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 43115450Sbrendan /* 43125450Sbrendan * Skip this buffer rather than waiting. 43135450Sbrendan */ 43145450Sbrendan continue; 43155450Sbrendan } 43165450Sbrendan 43175450Sbrendan passed_sz += ab->b_size; 43185450Sbrendan if (passed_sz > headroom) { 43195450Sbrendan /* 43205450Sbrendan * Searched too far. 43215450Sbrendan */ 43225450Sbrendan mutex_exit(hash_lock); 43235450Sbrendan break; 43245450Sbrendan } 43255450Sbrendan 43268636SMark.Maybee@Sun.COM if (!l2arc_write_eligible(guid, ab)) { 43275450Sbrendan mutex_exit(hash_lock); 43285450Sbrendan continue; 43295450Sbrendan } 43305450Sbrendan 43315450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 43325450Sbrendan full = B_TRUE; 43335450Sbrendan mutex_exit(hash_lock); 43345450Sbrendan break; 43355450Sbrendan } 43365450Sbrendan 43375450Sbrendan if (pio == NULL) { 43385450Sbrendan /* 43395450Sbrendan * Insert a dummy header on the buflist so 43405450Sbrendan * l2arc_write_done() can find where the 43415450Sbrendan * write buffers begin without searching. 43425450Sbrendan */ 43435450Sbrendan list_insert_head(dev->l2ad_buflist, head); 43445450Sbrendan 43455450Sbrendan cb = kmem_alloc( 43465450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 43475450Sbrendan cb->l2wcb_dev = dev; 43485450Sbrendan cb->l2wcb_head = head; 43495450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 43505450Sbrendan ZIO_FLAG_CANFAIL); 43515450Sbrendan } 43525450Sbrendan 43535450Sbrendan /* 43545450Sbrendan * Create and add a new L2ARC header. 43555450Sbrendan */ 43565450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 43575450Sbrendan hdrl2->b_dev = dev; 43585450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 43595450Sbrendan 43605450Sbrendan ab->b_flags |= ARC_L2_WRITING; 43615450Sbrendan ab->b_l2hdr = hdrl2; 43625450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 43635450Sbrendan buf_data = ab->b_buf->b_data; 43645450Sbrendan buf_sz = ab->b_size; 43655450Sbrendan 43665450Sbrendan /* 43675450Sbrendan * Compute and store the buffer cksum before 43685450Sbrendan * writing. On debug the cksum is verified first. 43695450Sbrendan */ 43705450Sbrendan arc_cksum_verify(ab->b_buf); 43715450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 43725450Sbrendan 43735450Sbrendan mutex_exit(hash_lock); 43745450Sbrendan 43755450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 43765450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 43775450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 43785450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 43795450Sbrendan 43805450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 43815450Sbrendan zio_t *, wzio); 43825450Sbrendan (void) zio_nowait(wzio); 43835450Sbrendan 43847754SJeff.Bonwick@Sun.COM /* 43857754SJeff.Bonwick@Sun.COM * Keep the clock hand suitably device-aligned. 43867754SJeff.Bonwick@Sun.COM */ 43877754SJeff.Bonwick@Sun.COM buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 43887754SJeff.Bonwick@Sun.COM 43895450Sbrendan write_sz += buf_sz; 43905450Sbrendan dev->l2ad_hand += buf_sz; 43915450Sbrendan } 43925450Sbrendan 43935450Sbrendan mutex_exit(list_lock); 43945450Sbrendan 43955450Sbrendan if (full == B_TRUE) 43965450Sbrendan break; 43975450Sbrendan } 43985450Sbrendan mutex_exit(&l2arc_buflist_mtx); 43995450Sbrendan 44005450Sbrendan if (pio == NULL) { 44015450Sbrendan ASSERT3U(write_sz, ==, 0); 44025450Sbrendan kmem_cache_free(hdr_cache, head); 44038582SBrendan.Gregg@Sun.COM return (0); 44045450Sbrendan } 44055450Sbrendan 44065450Sbrendan ASSERT3U(write_sz, <=, target_sz); 44075450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 44088582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 44095450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 44105450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, write_sz); 44115450Sbrendan 44125450Sbrendan /* 44135450Sbrendan * Bump device hand to the device start if it is approaching the end. 44145450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 44155450Sbrendan */ 44166987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 44175450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, 44185450Sbrendan dev->l2ad_end - dev->l2ad_hand); 44195450Sbrendan dev->l2ad_hand = dev->l2ad_start; 44205450Sbrendan dev->l2ad_evict = dev->l2ad_start; 44215450Sbrendan dev->l2ad_first = B_FALSE; 44225450Sbrendan } 44235450Sbrendan 44248582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_TRUE; 44255450Sbrendan (void) zio_wait(pio); 44268582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_FALSE; 44278582SBrendan.Gregg@Sun.COM 44288582SBrendan.Gregg@Sun.COM return (write_sz); 44295450Sbrendan } 44305450Sbrendan 44315450Sbrendan /* 44325450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 44335450Sbrendan * heart of the L2ARC. 44345450Sbrendan */ 44355450Sbrendan static void 44365450Sbrendan l2arc_feed_thread(void) 44375450Sbrendan { 44385450Sbrendan callb_cpr_t cpr; 44395450Sbrendan l2arc_dev_t *dev; 44405450Sbrendan spa_t *spa; 44418582SBrendan.Gregg@Sun.COM uint64_t size, wrote; 44428582SBrendan.Gregg@Sun.COM clock_t begin, next = lbolt; 44435450Sbrendan 44445450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 44455450Sbrendan 44465450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 44475450Sbrendan 44485450Sbrendan while (l2arc_thread_exit == 0) { 44495450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 44506987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 44518582SBrendan.Gregg@Sun.COM next); 44526987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 44538582SBrendan.Gregg@Sun.COM next = lbolt + hz; 44546987Sbrendan 44556987Sbrendan /* 44566987Sbrendan * Quick check for L2ARC devices. 44576987Sbrendan */ 44586987Sbrendan mutex_enter(&l2arc_dev_mtx); 44596987Sbrendan if (l2arc_ndev == 0) { 44606987Sbrendan mutex_exit(&l2arc_dev_mtx); 44616987Sbrendan continue; 44625450Sbrendan } 44636987Sbrendan mutex_exit(&l2arc_dev_mtx); 44648582SBrendan.Gregg@Sun.COM begin = lbolt; 44656643Seschrock 44665450Sbrendan /* 44676643Seschrock * This selects the next l2arc device to write to, and in 44686643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 44696987Sbrendan * will return NULL if there are now no l2arc devices or if 44706987Sbrendan * they are all faulted. 44716987Sbrendan * 44726987Sbrendan * If a device is returned, its spa's config lock is also 44736987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 44746987Sbrendan * will grab and release l2arc_dev_mtx. 44755450Sbrendan */ 44766987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 44775450Sbrendan continue; 44786987Sbrendan 44796987Sbrendan spa = dev->l2ad_spa; 44806987Sbrendan ASSERT(spa != NULL); 44815450Sbrendan 44825450Sbrendan /* 44835450Sbrendan * Avoid contributing to memory pressure. 44845450Sbrendan */ 44855450Sbrendan if (arc_reclaim_needed()) { 44865450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 44877754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44885450Sbrendan continue; 44895450Sbrendan } 44905450Sbrendan 44915450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 44925450Sbrendan 44938582SBrendan.Gregg@Sun.COM size = l2arc_write_size(dev); 44946987Sbrendan 44955450Sbrendan /* 44965450Sbrendan * Evict L2ARC buffers that will be overwritten. 44975450Sbrendan */ 44986987Sbrendan l2arc_evict(dev, size, B_FALSE); 44995450Sbrendan 45005450Sbrendan /* 45015450Sbrendan * Write ARC buffers. 45025450Sbrendan */ 45038582SBrendan.Gregg@Sun.COM wrote = l2arc_write_buffers(spa, dev, size); 45048582SBrendan.Gregg@Sun.COM 45058582SBrendan.Gregg@Sun.COM /* 45068582SBrendan.Gregg@Sun.COM * Calculate interval between writes. 45078582SBrendan.Gregg@Sun.COM */ 45088582SBrendan.Gregg@Sun.COM next = l2arc_write_interval(begin, size, wrote); 45097754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 45105450Sbrendan } 45115450Sbrendan 45125450Sbrendan l2arc_thread_exit = 0; 45135450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 45145450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 45155450Sbrendan thread_exit(); 45165450Sbrendan } 45175450Sbrendan 45186643Seschrock boolean_t 45196643Seschrock l2arc_vdev_present(vdev_t *vd) 45206643Seschrock { 45216643Seschrock l2arc_dev_t *dev; 45226643Seschrock 45236643Seschrock mutex_enter(&l2arc_dev_mtx); 45246643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 45256643Seschrock dev = list_next(l2arc_dev_list, dev)) { 45266643Seschrock if (dev->l2ad_vdev == vd) 45276643Seschrock break; 45286643Seschrock } 45296643Seschrock mutex_exit(&l2arc_dev_mtx); 45306643Seschrock 45316643Seschrock return (dev != NULL); 45326643Seschrock } 45336643Seschrock 45345450Sbrendan /* 45355450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 45365450Sbrendan * validated the vdev and opened it. 45375450Sbrendan */ 45385450Sbrendan void 4539*9816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa_t *spa, vdev_t *vd) 45405450Sbrendan { 45415450Sbrendan l2arc_dev_t *adddev; 45425450Sbrendan 45436643Seschrock ASSERT(!l2arc_vdev_present(vd)); 45446643Seschrock 45455450Sbrendan /* 45465450Sbrendan * Create a new l2arc device entry. 45475450Sbrendan */ 45485450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 45495450Sbrendan adddev->l2ad_spa = spa; 45505450Sbrendan adddev->l2ad_vdev = vd; 45515450Sbrendan adddev->l2ad_write = l2arc_write_max; 45526987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 4553*9816SGeorge.Wilson@Sun.COM adddev->l2ad_start = VDEV_LABEL_START_SIZE; 4554*9816SGeorge.Wilson@Sun.COM adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 45555450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 45565450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 45575450Sbrendan adddev->l2ad_first = B_TRUE; 45588582SBrendan.Gregg@Sun.COM adddev->l2ad_writing = B_FALSE; 45595450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 45605450Sbrendan 45615450Sbrendan /* 45625450Sbrendan * This is a list of all ARC buffers that are still valid on the 45635450Sbrendan * device. 45645450Sbrendan */ 45655450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 45665450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 45675450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 45685450Sbrendan 45695450Sbrendan spa_l2cache_space_update(vd, adddev->l2ad_end - adddev->l2ad_hand, 0); 45705450Sbrendan 45715450Sbrendan /* 45725450Sbrendan * Add device to global list 45735450Sbrendan */ 45745450Sbrendan mutex_enter(&l2arc_dev_mtx); 45755450Sbrendan list_insert_head(l2arc_dev_list, adddev); 45765450Sbrendan atomic_inc_64(&l2arc_ndev); 45775450Sbrendan mutex_exit(&l2arc_dev_mtx); 45785450Sbrendan } 45795450Sbrendan 45805450Sbrendan /* 45815450Sbrendan * Remove a vdev from the L2ARC. 45825450Sbrendan */ 45835450Sbrendan void 45845450Sbrendan l2arc_remove_vdev(vdev_t *vd) 45855450Sbrendan { 45865450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 45875450Sbrendan 45885450Sbrendan /* 45895450Sbrendan * Find the device by vdev 45905450Sbrendan */ 45915450Sbrendan mutex_enter(&l2arc_dev_mtx); 45925450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 45935450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 45945450Sbrendan if (vd == dev->l2ad_vdev) { 45955450Sbrendan remdev = dev; 45965450Sbrendan break; 45975450Sbrendan } 45985450Sbrendan } 45995450Sbrendan ASSERT(remdev != NULL); 46005450Sbrendan 46015450Sbrendan /* 46025450Sbrendan * Remove device from global list 46035450Sbrendan */ 46045450Sbrendan list_remove(l2arc_dev_list, remdev); 46055450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 46066987Sbrendan atomic_dec_64(&l2arc_ndev); 46076987Sbrendan mutex_exit(&l2arc_dev_mtx); 46085450Sbrendan 46095450Sbrendan /* 46105450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 46115450Sbrendan */ 46125450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 46135450Sbrendan list_destroy(remdev->l2ad_buflist); 46145450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 46155450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 46165450Sbrendan } 46175450Sbrendan 46185450Sbrendan void 46197754SJeff.Bonwick@Sun.COM l2arc_init(void) 46205450Sbrendan { 46215450Sbrendan l2arc_thread_exit = 0; 46225450Sbrendan l2arc_ndev = 0; 46235450Sbrendan l2arc_writes_sent = 0; 46245450Sbrendan l2arc_writes_done = 0; 46255450Sbrendan 46265450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 46275450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 46285450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 46295450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 46305450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 46315450Sbrendan 46325450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 46335450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 46345450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 46355450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 46365450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 46375450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 46385450Sbrendan } 46395450Sbrendan 46405450Sbrendan void 46417754SJeff.Bonwick@Sun.COM l2arc_fini(void) 46425450Sbrendan { 46436987Sbrendan /* 46446987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 46456987Sbrendan * Because of this, we can assume that all l2arc devices have 46466987Sbrendan * already been removed when the pools themselves were removed. 46476987Sbrendan */ 46486987Sbrendan 46496987Sbrendan l2arc_do_free_on_write(); 46506987Sbrendan 46515450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 46525450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 46535450Sbrendan mutex_destroy(&l2arc_dev_mtx); 46545450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 46555450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 46565450Sbrendan 46575450Sbrendan list_destroy(l2arc_dev_list); 46585450Sbrendan list_destroy(l2arc_free_on_write); 46595450Sbrendan } 46607754SJeff.Bonwick@Sun.COM 46617754SJeff.Bonwick@Sun.COM void 46627754SJeff.Bonwick@Sun.COM l2arc_start(void) 46637754SJeff.Bonwick@Sun.COM { 46648241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46657754SJeff.Bonwick@Sun.COM return; 46667754SJeff.Bonwick@Sun.COM 46677754SJeff.Bonwick@Sun.COM (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 46687754SJeff.Bonwick@Sun.COM TS_RUN, minclsyspri); 46697754SJeff.Bonwick@Sun.COM } 46707754SJeff.Bonwick@Sun.COM 46717754SJeff.Bonwick@Sun.COM void 46727754SJeff.Bonwick@Sun.COM l2arc_stop(void) 46737754SJeff.Bonwick@Sun.COM { 46748241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46757754SJeff.Bonwick@Sun.COM return; 46767754SJeff.Bonwick@Sun.COM 46777754SJeff.Bonwick@Sun.COM mutex_enter(&l2arc_feed_thr_lock); 46787754SJeff.Bonwick@Sun.COM cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 46797754SJeff.Bonwick@Sun.COM l2arc_thread_exit = 1; 46807754SJeff.Bonwick@Sun.COM while (l2arc_thread_exit != 0) 46817754SJeff.Bonwick@Sun.COM cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 46827754SJeff.Bonwick@Sun.COM mutex_exit(&l2arc_feed_thr_lock); 46837754SJeff.Bonwick@Sun.COM } 4684