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> 127789Sahrens #ifdef _KERNEL 128789Sahrens #include <sys/vmsystm.h> 129789Sahrens #include <vm/anon.h> 130789Sahrens #include <sys/fs/swapnode.h> 1311484Sek110237 #include <sys/dnlc.h> 132789Sahrens #endif 133789Sahrens #include <sys/callb.h> 1343403Sbmc #include <sys/kstat.h> 135789Sahrens 136789Sahrens static kmutex_t arc_reclaim_thr_lock; 137789Sahrens static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 138789Sahrens static uint8_t arc_thread_exit; 139789Sahrens 1406245Smaybee extern int zfs_write_limit_shift; 1416245Smaybee extern uint64_t zfs_write_limit_max; 1427468SMark.Maybee@Sun.COM extern kmutex_t zfs_write_limit_lock; 1436245Smaybee 1441484Sek110237 #define ARC_REDUCE_DNLC_PERCENT 3 1451484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 1461484Sek110237 147789Sahrens typedef enum arc_reclaim_strategy { 148789Sahrens ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 149789Sahrens ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 150789Sahrens } arc_reclaim_strategy_t; 151789Sahrens 152789Sahrens /* number of seconds before growing cache again */ 153789Sahrens static int arc_grow_retry = 60; 154789Sahrens 1558582SBrendan.Gregg@Sun.COM /* shift of arc_c for calculating both min and max arc_p */ 1568582SBrendan.Gregg@Sun.COM static int arc_p_min_shift = 4; 1578582SBrendan.Gregg@Sun.COM 1588582SBrendan.Gregg@Sun.COM /* log2(fraction of arc to reclaim) */ 1598582SBrendan.Gregg@Sun.COM static int arc_shrink_shift = 5; 1608582SBrendan.Gregg@Sun.COM 1612391Smaybee /* 1622638Sperrin * minimum lifespan of a prefetch block in clock ticks 1632638Sperrin * (initialized in arc_init()) 1642391Smaybee */ 1652638Sperrin static int arc_min_prefetch_lifespan; 1662391Smaybee 167789Sahrens static int arc_dead; 168789Sahrens 169789Sahrens /* 1706987Sbrendan * The arc has filled available memory and has now warmed up. 1716987Sbrendan */ 1726987Sbrendan static boolean_t arc_warm; 1736987Sbrendan 1746987Sbrendan /* 1752885Sahrens * These tunables are for performance analysis. 1762885Sahrens */ 1772885Sahrens uint64_t zfs_arc_max; 1782885Sahrens uint64_t zfs_arc_min; 1794645Sek110237 uint64_t zfs_arc_meta_limit = 0; 1807046Sahrens int zfs_mdcomp_disable = 0; 1818582SBrendan.Gregg@Sun.COM int zfs_arc_grow_retry = 0; 1828582SBrendan.Gregg@Sun.COM int zfs_arc_shrink_shift = 0; 1838582SBrendan.Gregg@Sun.COM int zfs_arc_p_min_shift = 0; 1842885Sahrens 1852885Sahrens /* 1865450Sbrendan * Note that buffers can be in one of 6 states: 187789Sahrens * ARC_anon - anonymous (discussed below) 1881544Seschrock * ARC_mru - recently used, currently cached 1891544Seschrock * ARC_mru_ghost - recentely used, no longer in cache 1901544Seschrock * ARC_mfu - frequently used, currently cached 1911544Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 1925450Sbrendan * ARC_l2c_only - exists in L2ARC but not other states 1934309Smaybee * When there are no active references to the buffer, they are 1944309Smaybee * are linked onto a list in one of these arc states. These are 1954309Smaybee * the only buffers that can be evicted or deleted. Within each 1964309Smaybee * state there are multiple lists, one for meta-data and one for 1974309Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 1984309Smaybee * etc.) is tracked separately so that it can be managed more 1995450Sbrendan * explicitly: favored over data, limited explicitly. 200789Sahrens * 201789Sahrens * Anonymous buffers are buffers that are not associated with 202789Sahrens * a DVA. These are buffers that hold dirty block copies 203789Sahrens * before they are written to stable storage. By definition, 2041544Seschrock * they are "ref'd" and are considered part of arc_mru 205789Sahrens * that cannot be freed. Generally, they will aquire a DVA 2061544Seschrock * as they are written and migrate onto the arc_mru list. 2075450Sbrendan * 2085450Sbrendan * The ARC_l2c_only state is for buffers that are in the second 2095450Sbrendan * level ARC but no longer in any of the ARC_m* lists. The second 2105450Sbrendan * level ARC itself may also contain buffers that are in any of 2115450Sbrendan * the ARC_m* states - meaning that a buffer can exist in two 2125450Sbrendan * places. The reason for the ARC_l2c_only state is to keep the 2135450Sbrendan * buffer header in the hash table, so that reads that hit the 2145450Sbrendan * second level ARC benefit from these fast lookups. 215789Sahrens */ 216789Sahrens 217789Sahrens typedef struct arc_state { 2184309Smaybee list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */ 2194309Smaybee uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 2204309Smaybee uint64_t arcs_size; /* total amount of data in this state */ 2213403Sbmc kmutex_t arcs_mtx; 222789Sahrens } arc_state_t; 223789Sahrens 2245450Sbrendan /* The 6 states: */ 225789Sahrens static arc_state_t ARC_anon; 2261544Seschrock static arc_state_t ARC_mru; 2271544Seschrock static arc_state_t ARC_mru_ghost; 2281544Seschrock static arc_state_t ARC_mfu; 2291544Seschrock static arc_state_t ARC_mfu_ghost; 2305450Sbrendan static arc_state_t ARC_l2c_only; 231789Sahrens 2323403Sbmc typedef struct arc_stats { 2333403Sbmc kstat_named_t arcstat_hits; 2343403Sbmc kstat_named_t arcstat_misses; 2353403Sbmc kstat_named_t arcstat_demand_data_hits; 2363403Sbmc kstat_named_t arcstat_demand_data_misses; 2373403Sbmc kstat_named_t arcstat_demand_metadata_hits; 2383403Sbmc kstat_named_t arcstat_demand_metadata_misses; 2393403Sbmc kstat_named_t arcstat_prefetch_data_hits; 2403403Sbmc kstat_named_t arcstat_prefetch_data_misses; 2413403Sbmc kstat_named_t arcstat_prefetch_metadata_hits; 2423403Sbmc kstat_named_t arcstat_prefetch_metadata_misses; 2433403Sbmc kstat_named_t arcstat_mru_hits; 2443403Sbmc kstat_named_t arcstat_mru_ghost_hits; 2453403Sbmc kstat_named_t arcstat_mfu_hits; 2463403Sbmc kstat_named_t arcstat_mfu_ghost_hits; 2473403Sbmc kstat_named_t arcstat_deleted; 2483403Sbmc kstat_named_t arcstat_recycle_miss; 2493403Sbmc kstat_named_t arcstat_mutex_miss; 2503403Sbmc kstat_named_t arcstat_evict_skip; 2513403Sbmc kstat_named_t arcstat_hash_elements; 2523403Sbmc kstat_named_t arcstat_hash_elements_max; 2533403Sbmc kstat_named_t arcstat_hash_collisions; 2543403Sbmc kstat_named_t arcstat_hash_chains; 2553403Sbmc kstat_named_t arcstat_hash_chain_max; 2563403Sbmc kstat_named_t arcstat_p; 2573403Sbmc kstat_named_t arcstat_c; 2583403Sbmc kstat_named_t arcstat_c_min; 2593403Sbmc kstat_named_t arcstat_c_max; 2603403Sbmc kstat_named_t arcstat_size; 2615450Sbrendan kstat_named_t arcstat_hdr_size; 2628582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_data_size; 2638582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_other_size; 2645450Sbrendan kstat_named_t arcstat_l2_hits; 2655450Sbrendan kstat_named_t arcstat_l2_misses; 2665450Sbrendan kstat_named_t arcstat_l2_feeds; 2675450Sbrendan kstat_named_t arcstat_l2_rw_clash; 2688582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_l2_read_bytes; 2698582SBrendan.Gregg@Sun.COM kstat_named_t arcstat_l2_write_bytes; 2705450Sbrendan kstat_named_t arcstat_l2_writes_sent; 2715450Sbrendan kstat_named_t arcstat_l2_writes_done; 2725450Sbrendan kstat_named_t arcstat_l2_writes_error; 2735450Sbrendan kstat_named_t arcstat_l2_writes_hdr_miss; 2745450Sbrendan kstat_named_t arcstat_l2_evict_lock_retry; 2755450Sbrendan kstat_named_t arcstat_l2_evict_reading; 2765450Sbrendan kstat_named_t arcstat_l2_free_on_write; 2775450Sbrendan kstat_named_t arcstat_l2_abort_lowmem; 2785450Sbrendan kstat_named_t arcstat_l2_cksum_bad; 2795450Sbrendan kstat_named_t arcstat_l2_io_error; 2805450Sbrendan kstat_named_t arcstat_l2_size; 2815450Sbrendan kstat_named_t arcstat_l2_hdr_size; 2826245Smaybee kstat_named_t arcstat_memory_throttle_count; 2833403Sbmc } arc_stats_t; 2843403Sbmc 2853403Sbmc static arc_stats_t arc_stats = { 2863403Sbmc { "hits", KSTAT_DATA_UINT64 }, 2873403Sbmc { "misses", KSTAT_DATA_UINT64 }, 2883403Sbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 2893403Sbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 2903403Sbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 2913403Sbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 2923403Sbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 2933403Sbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 2943403Sbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 2953403Sbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 2963403Sbmc { "mru_hits", KSTAT_DATA_UINT64 }, 2973403Sbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 2983403Sbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 2993403Sbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 3003403Sbmc { "deleted", KSTAT_DATA_UINT64 }, 3013403Sbmc { "recycle_miss", KSTAT_DATA_UINT64 }, 3023403Sbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 3033403Sbmc { "evict_skip", KSTAT_DATA_UINT64 }, 3043403Sbmc { "hash_elements", KSTAT_DATA_UINT64 }, 3053403Sbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 3063403Sbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 3073403Sbmc { "hash_chains", KSTAT_DATA_UINT64 }, 3083403Sbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 3093403Sbmc { "p", KSTAT_DATA_UINT64 }, 3103403Sbmc { "c", KSTAT_DATA_UINT64 }, 3113403Sbmc { "c_min", KSTAT_DATA_UINT64 }, 3123403Sbmc { "c_max", KSTAT_DATA_UINT64 }, 3135450Sbrendan { "size", KSTAT_DATA_UINT64 }, 3145450Sbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 3158582SBrendan.Gregg@Sun.COM { "data_size", KSTAT_DATA_UINT64 }, 3168582SBrendan.Gregg@Sun.COM { "other_size", KSTAT_DATA_UINT64 }, 3175450Sbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 3185450Sbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 3195450Sbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 3205450Sbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 3218582SBrendan.Gregg@Sun.COM { "l2_read_bytes", KSTAT_DATA_UINT64 }, 3228582SBrendan.Gregg@Sun.COM { "l2_write_bytes", KSTAT_DATA_UINT64 }, 3235450Sbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 3245450Sbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 3255450Sbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 3265450Sbrendan { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 3275450Sbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 3285450Sbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 3295450Sbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 3305450Sbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 3315450Sbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 3325450Sbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 3335450Sbrendan { "l2_size", KSTAT_DATA_UINT64 }, 3346245Smaybee { "l2_hdr_size", KSTAT_DATA_UINT64 }, 3356245Smaybee { "memory_throttle_count", KSTAT_DATA_UINT64 } 3363403Sbmc }; 337789Sahrens 3383403Sbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 3393403Sbmc 3403403Sbmc #define ARCSTAT_INCR(stat, val) \ 3413403Sbmc atomic_add_64(&arc_stats.stat.value.ui64, (val)); 3423403Sbmc 3433403Sbmc #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 3443403Sbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 3453403Sbmc 3463403Sbmc #define ARCSTAT_MAX(stat, val) { \ 3473403Sbmc uint64_t m; \ 3483403Sbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 3493403Sbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 3503403Sbmc continue; \ 3513403Sbmc } 3523403Sbmc 3533403Sbmc #define ARCSTAT_MAXSTAT(stat) \ 3543403Sbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 355789Sahrens 3563403Sbmc /* 3573403Sbmc * We define a macro to allow ARC hits/misses to be easily broken down by 3583403Sbmc * two separate conditions, giving a total of four different subtypes for 3593403Sbmc * each of hits and misses (so eight statistics total). 3603403Sbmc */ 3613403Sbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 3623403Sbmc if (cond1) { \ 3633403Sbmc if (cond2) { \ 3643403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 3653403Sbmc } else { \ 3663403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 3673403Sbmc } \ 3683403Sbmc } else { \ 3693403Sbmc if (cond2) { \ 3703403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 3713403Sbmc } else { \ 3723403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 3733403Sbmc } \ 3743403Sbmc } 375789Sahrens 3763403Sbmc kstat_t *arc_ksp; 3773403Sbmc static arc_state_t *arc_anon; 3783403Sbmc static arc_state_t *arc_mru; 3793403Sbmc static arc_state_t *arc_mru_ghost; 3803403Sbmc static arc_state_t *arc_mfu; 3813403Sbmc static arc_state_t *arc_mfu_ghost; 3825450Sbrendan static arc_state_t *arc_l2c_only; 3833403Sbmc 3843403Sbmc /* 3853403Sbmc * There are several ARC variables that are critical to export as kstats -- 3863403Sbmc * but we don't want to have to grovel around in the kstat whenever we wish to 3873403Sbmc * manipulate them. For these variables, we therefore define them to be in 3883403Sbmc * terms of the statistic variable. This assures that we are not introducing 3893403Sbmc * the possibility of inconsistency by having shadow copies of the variables, 3903403Sbmc * while still allowing the code to be readable. 3913403Sbmc */ 3923403Sbmc #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 3933403Sbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 3943403Sbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 3953403Sbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 3963403Sbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 3973403Sbmc 3983403Sbmc static int arc_no_grow; /* Don't try to grow cache size */ 3993403Sbmc static uint64_t arc_tempreserve; 4004309Smaybee static uint64_t arc_meta_used; 4014309Smaybee static uint64_t arc_meta_limit; 4024309Smaybee static uint64_t arc_meta_max = 0; 403789Sahrens 4045450Sbrendan typedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 4055450Sbrendan 406789Sahrens typedef struct arc_callback arc_callback_t; 407789Sahrens 408789Sahrens struct arc_callback { 4093547Smaybee void *acb_private; 410789Sahrens arc_done_func_t *acb_done; 411789Sahrens arc_buf_t *acb_buf; 412789Sahrens zio_t *acb_zio_dummy; 413789Sahrens arc_callback_t *acb_next; 414789Sahrens }; 415789Sahrens 4163547Smaybee typedef struct arc_write_callback arc_write_callback_t; 4173547Smaybee 4183547Smaybee struct arc_write_callback { 4193547Smaybee void *awcb_private; 4203547Smaybee arc_done_func_t *awcb_ready; 4213547Smaybee arc_done_func_t *awcb_done; 4223547Smaybee arc_buf_t *awcb_buf; 4233547Smaybee }; 4243547Smaybee 425789Sahrens struct arc_buf_hdr { 426789Sahrens /* protected by hash lock */ 427789Sahrens dva_t b_dva; 428789Sahrens uint64_t b_birth; 429789Sahrens uint64_t b_cksum0; 430789Sahrens 4313093Sahrens kmutex_t b_freeze_lock; 4323093Sahrens zio_cksum_t *b_freeze_cksum; 4333093Sahrens 434789Sahrens arc_buf_hdr_t *b_hash_next; 435789Sahrens arc_buf_t *b_buf; 436789Sahrens uint32_t b_flags; 4371544Seschrock uint32_t b_datacnt; 438789Sahrens 4393290Sjohansen arc_callback_t *b_acb; 440789Sahrens kcondvar_t b_cv; 4413290Sjohansen 4423290Sjohansen /* immutable */ 4433290Sjohansen arc_buf_contents_t b_type; 4443290Sjohansen uint64_t b_size; 4453290Sjohansen spa_t *b_spa; 446789Sahrens 447789Sahrens /* protected by arc state mutex */ 448789Sahrens arc_state_t *b_state; 449789Sahrens list_node_t b_arc_node; 450789Sahrens 451789Sahrens /* updated atomically */ 452789Sahrens clock_t b_arc_access; 453789Sahrens 454789Sahrens /* self protecting */ 455789Sahrens refcount_t b_refcnt; 4565450Sbrendan 4575450Sbrendan l2arc_buf_hdr_t *b_l2hdr; 4585450Sbrendan list_node_t b_l2node; 459789Sahrens }; 460789Sahrens 4611544Seschrock static arc_buf_t *arc_eviction_list; 4621544Seschrock static kmutex_t arc_eviction_mtx; 4632887Smaybee static arc_buf_hdr_t arc_eviction_hdr; 4642688Smaybee static void arc_get_data_buf(arc_buf_t *buf); 4652688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 4664309Smaybee static int arc_evict_needed(arc_buf_contents_t type); 4675642Smaybee static void arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes); 4681544Seschrock 4691544Seschrock #define GHOST_STATE(state) \ 4705450Sbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 4715450Sbrendan (state) == arc_l2c_only) 4721544Seschrock 473789Sahrens /* 474789Sahrens * Private ARC flags. These flags are private ARC only flags that will show up 475789Sahrens * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 476789Sahrens * be passed in as arc_flags in things like arc_read. However, these flags 477789Sahrens * should never be passed and should only be set by ARC code. When adding new 478789Sahrens * public flags, make sure not to smash the private ones. 479789Sahrens */ 480789Sahrens 4811544Seschrock #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 482789Sahrens #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 483789Sahrens #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 484789Sahrens #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 4851544Seschrock #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 4862391Smaybee #define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 4875450Sbrendan #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 4887237Sek110237 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 4897237Sek110237 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 4907237Sek110237 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 4917237Sek110237 #define ARC_STORED (1 << 19) /* has been store()d to */ 492789Sahrens 4931544Seschrock #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 494789Sahrens #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 495789Sahrens #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 4968582SBrendan.Gregg@Sun.COM #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH) 497789Sahrens #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 4981544Seschrock #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 4995450Sbrendan #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 5007237Sek110237 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 5016987Sbrendan #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 5026987Sbrendan (hdr)->b_l2hdr != NULL) 5035450Sbrendan #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 5045450Sbrendan #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 5055450Sbrendan #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 506789Sahrens 507789Sahrens /* 5086018Sbrendan * Other sizes 5096018Sbrendan */ 5106018Sbrendan 5116018Sbrendan #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 5126018Sbrendan #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 5136018Sbrendan 5146018Sbrendan /* 515789Sahrens * Hash table routines 516789Sahrens */ 517789Sahrens 518789Sahrens #define HT_LOCK_PAD 64 519789Sahrens 520789Sahrens struct ht_lock { 521789Sahrens kmutex_t ht_lock; 522789Sahrens #ifdef _KERNEL 523789Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 524789Sahrens #endif 525789Sahrens }; 526789Sahrens 527789Sahrens #define BUF_LOCKS 256 528789Sahrens typedef struct buf_hash_table { 529789Sahrens uint64_t ht_mask; 530789Sahrens arc_buf_hdr_t **ht_table; 531789Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 532789Sahrens } buf_hash_table_t; 533789Sahrens 534789Sahrens static buf_hash_table_t buf_hash_table; 535789Sahrens 536789Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 537789Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 538789Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 539789Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 540789Sahrens #define HDR_LOCK(buf) \ 541789Sahrens (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth))) 542789Sahrens 543789Sahrens uint64_t zfs_crc64_table[256]; 544789Sahrens 5455450Sbrendan /* 5465450Sbrendan * Level 2 ARC 5475450Sbrendan */ 5485450Sbrendan 5495450Sbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 5508582SBrendan.Gregg@Sun.COM #define L2ARC_HEADROOM 2 /* num of writes */ 5518582SBrendan.Gregg@Sun.COM #define L2ARC_FEED_SECS 1 /* caching interval secs */ 5528582SBrendan.Gregg@Sun.COM #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 5535450Sbrendan 5545450Sbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 5555450Sbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 5565450Sbrendan 5575450Sbrendan /* 5585450Sbrendan * L2ARC Performance Tunables 5595450Sbrendan */ 5605450Sbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 5616987Sbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 5625450Sbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 5635450Sbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 5648582SBrendan.Gregg@Sun.COM uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 5655450Sbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 5668582SBrendan.Gregg@Sun.COM boolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 5678582SBrendan.Gregg@Sun.COM boolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 5685450Sbrendan 5695450Sbrendan /* 5705450Sbrendan * L2ARC Internals 5715450Sbrendan */ 5725450Sbrendan typedef struct l2arc_dev { 5735450Sbrendan vdev_t *l2ad_vdev; /* vdev */ 5745450Sbrendan spa_t *l2ad_spa; /* spa */ 5755450Sbrendan uint64_t l2ad_hand; /* next write location */ 5765450Sbrendan uint64_t l2ad_write; /* desired write size, bytes */ 5776987Sbrendan uint64_t l2ad_boost; /* warmup write boost, bytes */ 5785450Sbrendan uint64_t l2ad_start; /* first addr on device */ 5795450Sbrendan uint64_t l2ad_end; /* last addr on device */ 5805450Sbrendan uint64_t l2ad_evict; /* last addr eviction reached */ 5815450Sbrendan boolean_t l2ad_first; /* first sweep through */ 5828582SBrendan.Gregg@Sun.COM boolean_t l2ad_writing; /* currently writing */ 5835450Sbrendan list_t *l2ad_buflist; /* buffer list */ 5845450Sbrendan list_node_t l2ad_node; /* device list node */ 5855450Sbrendan } l2arc_dev_t; 5865450Sbrendan 5875450Sbrendan static list_t L2ARC_dev_list; /* device list */ 5885450Sbrendan static list_t *l2arc_dev_list; /* device list pointer */ 5895450Sbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 5905450Sbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 5915450Sbrendan static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 5925450Sbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 5935450Sbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 5945450Sbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 5955450Sbrendan static uint64_t l2arc_ndev; /* number of devices */ 5965450Sbrendan 5975450Sbrendan typedef struct l2arc_read_callback { 5985450Sbrendan arc_buf_t *l2rcb_buf; /* read buffer */ 5995450Sbrendan spa_t *l2rcb_spa; /* spa */ 6005450Sbrendan blkptr_t l2rcb_bp; /* original blkptr */ 6015450Sbrendan zbookmark_t l2rcb_zb; /* original bookmark */ 6025450Sbrendan int l2rcb_flags; /* original flags */ 6035450Sbrendan } l2arc_read_callback_t; 6045450Sbrendan 6055450Sbrendan typedef struct l2arc_write_callback { 6065450Sbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 6075450Sbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 6085450Sbrendan } l2arc_write_callback_t; 6095450Sbrendan 6105450Sbrendan struct l2arc_buf_hdr { 6115450Sbrendan /* protected by arc_buf_hdr mutex */ 6125450Sbrendan l2arc_dev_t *b_dev; /* L2ARC device */ 6135450Sbrendan daddr_t b_daddr; /* disk address, offset byte */ 6145450Sbrendan }; 6155450Sbrendan 6165450Sbrendan typedef struct l2arc_data_free { 6175450Sbrendan /* protected by l2arc_free_on_write_mtx */ 6185450Sbrendan void *l2df_data; 6195450Sbrendan size_t l2df_size; 6205450Sbrendan void (*l2df_func)(void *, size_t); 6215450Sbrendan list_node_t l2df_list_node; 6225450Sbrendan } l2arc_data_free_t; 6235450Sbrendan 6245450Sbrendan static kmutex_t l2arc_feed_thr_lock; 6255450Sbrendan static kcondvar_t l2arc_feed_thr_cv; 6265450Sbrendan static uint8_t l2arc_thread_exit; 6275450Sbrendan 6285450Sbrendan static void l2arc_read_done(zio_t *zio); 6295450Sbrendan static void l2arc_hdr_stat_add(void); 6305450Sbrendan static void l2arc_hdr_stat_remove(void); 6315450Sbrendan 632789Sahrens static uint64_t 6337046Sahrens buf_hash(spa_t *spa, const dva_t *dva, uint64_t birth) 634789Sahrens { 635789Sahrens uintptr_t spav = (uintptr_t)spa; 636789Sahrens uint8_t *vdva = (uint8_t *)dva; 637789Sahrens uint64_t crc = -1ULL; 638789Sahrens int i; 639789Sahrens 640789Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 641789Sahrens 642789Sahrens for (i = 0; i < sizeof (dva_t); i++) 643789Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 644789Sahrens 645789Sahrens crc ^= (spav>>8) ^ birth; 646789Sahrens 647789Sahrens return (crc); 648789Sahrens } 649789Sahrens 650789Sahrens #define BUF_EMPTY(buf) \ 651789Sahrens ((buf)->b_dva.dva_word[0] == 0 && \ 652789Sahrens (buf)->b_dva.dva_word[1] == 0 && \ 653789Sahrens (buf)->b_birth == 0) 654789Sahrens 655789Sahrens #define BUF_EQUAL(spa, dva, birth, buf) \ 656789Sahrens ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 657789Sahrens ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 658789Sahrens ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 659789Sahrens 660789Sahrens static arc_buf_hdr_t * 6617046Sahrens buf_hash_find(spa_t *spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 662789Sahrens { 663789Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 664789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 665789Sahrens arc_buf_hdr_t *buf; 666789Sahrens 667789Sahrens mutex_enter(hash_lock); 668789Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 669789Sahrens buf = buf->b_hash_next) { 670789Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 671789Sahrens *lockp = hash_lock; 672789Sahrens return (buf); 673789Sahrens } 674789Sahrens } 675789Sahrens mutex_exit(hash_lock); 676789Sahrens *lockp = NULL; 677789Sahrens return (NULL); 678789Sahrens } 679789Sahrens 680789Sahrens /* 681789Sahrens * Insert an entry into the hash table. If there is already an element 682789Sahrens * equal to elem in the hash table, then the already existing element 683789Sahrens * will be returned and the new element will not be inserted. 684789Sahrens * Otherwise returns NULL. 685789Sahrens */ 686789Sahrens static arc_buf_hdr_t * 687789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 688789Sahrens { 689789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 690789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 691789Sahrens arc_buf_hdr_t *fbuf; 6923403Sbmc uint32_t i; 693789Sahrens 6941544Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 695789Sahrens *lockp = hash_lock; 696789Sahrens mutex_enter(hash_lock); 697789Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 698789Sahrens fbuf = fbuf->b_hash_next, i++) { 699789Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 700789Sahrens return (fbuf); 701789Sahrens } 702789Sahrens 703789Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 704789Sahrens buf_hash_table.ht_table[idx] = buf; 7051544Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 706789Sahrens 707789Sahrens /* collect some hash table performance data */ 708789Sahrens if (i > 0) { 7093403Sbmc ARCSTAT_BUMP(arcstat_hash_collisions); 710789Sahrens if (i == 1) 7113403Sbmc ARCSTAT_BUMP(arcstat_hash_chains); 7123403Sbmc 7133403Sbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 714789Sahrens } 7153403Sbmc 7163403Sbmc ARCSTAT_BUMP(arcstat_hash_elements); 7173403Sbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 718789Sahrens 719789Sahrens return (NULL); 720789Sahrens } 721789Sahrens 722789Sahrens static void 723789Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 724789Sahrens { 725789Sahrens arc_buf_hdr_t *fbuf, **bufp; 726789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 727789Sahrens 728789Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 7291544Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 730789Sahrens 731789Sahrens bufp = &buf_hash_table.ht_table[idx]; 732789Sahrens while ((fbuf = *bufp) != buf) { 733789Sahrens ASSERT(fbuf != NULL); 734789Sahrens bufp = &fbuf->b_hash_next; 735789Sahrens } 736789Sahrens *bufp = buf->b_hash_next; 737789Sahrens buf->b_hash_next = NULL; 7381544Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 739789Sahrens 740789Sahrens /* collect some hash table performance data */ 7413403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 7423403Sbmc 743789Sahrens if (buf_hash_table.ht_table[idx] && 744789Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 7453403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 746789Sahrens } 747789Sahrens 748789Sahrens /* 749789Sahrens * Global data structures and functions for the buf kmem cache. 750789Sahrens */ 751789Sahrens static kmem_cache_t *hdr_cache; 752789Sahrens static kmem_cache_t *buf_cache; 753789Sahrens 754789Sahrens static void 755789Sahrens buf_fini(void) 756789Sahrens { 757789Sahrens int i; 758789Sahrens 759789Sahrens kmem_free(buf_hash_table.ht_table, 760789Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 761789Sahrens for (i = 0; i < BUF_LOCKS; i++) 762789Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 763789Sahrens kmem_cache_destroy(hdr_cache); 764789Sahrens kmem_cache_destroy(buf_cache); 765789Sahrens } 766789Sahrens 767789Sahrens /* 768789Sahrens * Constructor callback - called when the cache is empty 769789Sahrens * and a new buf is requested. 770789Sahrens */ 771789Sahrens /* ARGSUSED */ 772789Sahrens static int 773789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 774789Sahrens { 775789Sahrens arc_buf_hdr_t *buf = vbuf; 776789Sahrens 777789Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 778789Sahrens refcount_create(&buf->b_refcnt); 779789Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 7804831Sgw25295 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 7818582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 7828582SBrendan.Gregg@Sun.COM 783789Sahrens return (0); 784789Sahrens } 785789Sahrens 7867545SMark.Maybee@Sun.COM /* ARGSUSED */ 7877545SMark.Maybee@Sun.COM static int 7887545SMark.Maybee@Sun.COM buf_cons(void *vbuf, void *unused, int kmflag) 7897545SMark.Maybee@Sun.COM { 7907545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 7917545SMark.Maybee@Sun.COM 7927545SMark.Maybee@Sun.COM bzero(buf, sizeof (arc_buf_t)); 7937545SMark.Maybee@Sun.COM rw_init(&buf->b_lock, NULL, RW_DEFAULT, NULL); 7948582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 7958582SBrendan.Gregg@Sun.COM 7967545SMark.Maybee@Sun.COM return (0); 7977545SMark.Maybee@Sun.COM } 7987545SMark.Maybee@Sun.COM 799789Sahrens /* 800789Sahrens * Destructor callback - called when a cached buf is 801789Sahrens * no longer required. 802789Sahrens */ 803789Sahrens /* ARGSUSED */ 804789Sahrens static void 805789Sahrens hdr_dest(void *vbuf, void *unused) 806789Sahrens { 807789Sahrens arc_buf_hdr_t *buf = vbuf; 808789Sahrens 809789Sahrens refcount_destroy(&buf->b_refcnt); 810789Sahrens cv_destroy(&buf->b_cv); 8114831Sgw25295 mutex_destroy(&buf->b_freeze_lock); 8128582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 813789Sahrens } 814789Sahrens 8157545SMark.Maybee@Sun.COM /* ARGSUSED */ 8167545SMark.Maybee@Sun.COM static void 8177545SMark.Maybee@Sun.COM buf_dest(void *vbuf, void *unused) 8187545SMark.Maybee@Sun.COM { 8197545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 8207545SMark.Maybee@Sun.COM 8217545SMark.Maybee@Sun.COM rw_destroy(&buf->b_lock); 8228582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 8237545SMark.Maybee@Sun.COM } 8247545SMark.Maybee@Sun.COM 825789Sahrens /* 826789Sahrens * Reclaim callback -- invoked when memory is low. 827789Sahrens */ 828789Sahrens /* ARGSUSED */ 829789Sahrens static void 830789Sahrens hdr_recl(void *unused) 831789Sahrens { 832789Sahrens dprintf("hdr_recl called\n"); 8333158Smaybee /* 8343158Smaybee * umem calls the reclaim func when we destroy the buf cache, 8353158Smaybee * which is after we do arc_fini(). 8363158Smaybee */ 8373158Smaybee if (!arc_dead) 8383158Smaybee cv_signal(&arc_reclaim_thr_cv); 839789Sahrens } 840789Sahrens 841789Sahrens static void 842789Sahrens buf_init(void) 843789Sahrens { 844789Sahrens uint64_t *ct; 8451544Seschrock uint64_t hsize = 1ULL << 12; 846789Sahrens int i, j; 847789Sahrens 848789Sahrens /* 849789Sahrens * The hash table is big enough to fill all of physical memory 8501544Seschrock * with an average 64K block size. The table will take up 8511544Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 852789Sahrens */ 8531544Seschrock while (hsize * 65536 < physmem * PAGESIZE) 854789Sahrens hsize <<= 1; 8551544Seschrock retry: 856789Sahrens buf_hash_table.ht_mask = hsize - 1; 8571544Seschrock buf_hash_table.ht_table = 8581544Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 8591544Seschrock if (buf_hash_table.ht_table == NULL) { 8601544Seschrock ASSERT(hsize > (1ULL << 8)); 8611544Seschrock hsize >>= 1; 8621544Seschrock goto retry; 8631544Seschrock } 864789Sahrens 865789Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 866789Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 867789Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 8687545SMark.Maybee@Sun.COM 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 869789Sahrens 870789Sahrens for (i = 0; i < 256; i++) 871789Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 872789Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 873789Sahrens 874789Sahrens for (i = 0; i < BUF_LOCKS; i++) { 875789Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 876789Sahrens NULL, MUTEX_DEFAULT, NULL); 877789Sahrens } 878789Sahrens } 879789Sahrens 880789Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 881789Sahrens 882789Sahrens static void 8833093Sahrens arc_cksum_verify(arc_buf_t *buf) 8843093Sahrens { 8853093Sahrens zio_cksum_t zc; 8863093Sahrens 8873312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 8883093Sahrens return; 8893093Sahrens 8903093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8913265Sahrens if (buf->b_hdr->b_freeze_cksum == NULL || 8923265Sahrens (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 8933093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8943093Sahrens return; 8953093Sahrens } 8963093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8973093Sahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 8983093Sahrens panic("buffer modified while frozen!"); 8993093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9003093Sahrens } 9013093Sahrens 9025450Sbrendan static int 9035450Sbrendan arc_cksum_equal(arc_buf_t *buf) 9045450Sbrendan { 9055450Sbrendan zio_cksum_t zc; 9065450Sbrendan int equal; 9075450Sbrendan 9085450Sbrendan mutex_enter(&buf->b_hdr->b_freeze_lock); 9095450Sbrendan fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 9105450Sbrendan equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 9115450Sbrendan mutex_exit(&buf->b_hdr->b_freeze_lock); 9125450Sbrendan 9135450Sbrendan return (equal); 9145450Sbrendan } 9155450Sbrendan 9163093Sahrens static void 9175450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force) 9183093Sahrens { 9195450Sbrendan if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 9203093Sahrens return; 9213093Sahrens 9223093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9233093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9243093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9253093Sahrens return; 9263093Sahrens } 9273093Sahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 9283093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 9293093Sahrens buf->b_hdr->b_freeze_cksum); 9303093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9313093Sahrens } 9323093Sahrens 9333093Sahrens void 9343093Sahrens arc_buf_thaw(arc_buf_t *buf) 9353093Sahrens { 9365450Sbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 9375450Sbrendan if (buf->b_hdr->b_state != arc_anon) 9385450Sbrendan panic("modifying non-anon buffer!"); 9395450Sbrendan if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 9405450Sbrendan panic("modifying buffer while i/o in progress!"); 9415450Sbrendan arc_cksum_verify(buf); 9425450Sbrendan } 9435450Sbrendan 9443093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9453093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9463093Sahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 9473093Sahrens buf->b_hdr->b_freeze_cksum = NULL; 9483093Sahrens } 9493093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9503093Sahrens } 9513093Sahrens 9523093Sahrens void 9533093Sahrens arc_buf_freeze(arc_buf_t *buf) 9543093Sahrens { 9553312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 9563312Sahrens return; 9573312Sahrens 9583093Sahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 9593403Sbmc buf->b_hdr->b_state == arc_anon); 9605450Sbrendan arc_cksum_compute(buf, B_FALSE); 9613093Sahrens } 9623093Sahrens 9633093Sahrens static void 964789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 965789Sahrens { 966789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 967789Sahrens 968789Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 9693403Sbmc (ab->b_state != arc_anon)) { 9703700Sek110237 uint64_t delta = ab->b_size * ab->b_datacnt; 9714309Smaybee list_t *list = &ab->b_state->arcs_list[ab->b_type]; 9724309Smaybee uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 973789Sahrens 9743403Sbmc ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx)); 9753403Sbmc mutex_enter(&ab->b_state->arcs_mtx); 976789Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 9774309Smaybee list_remove(list, ab); 9781544Seschrock if (GHOST_STATE(ab->b_state)) { 9791544Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 9801544Seschrock ASSERT3P(ab->b_buf, ==, NULL); 9811544Seschrock delta = ab->b_size; 9821544Seschrock } 9831544Seschrock ASSERT(delta > 0); 9844309Smaybee ASSERT3U(*size, >=, delta); 9854309Smaybee atomic_add_64(size, -delta); 9863403Sbmc mutex_exit(&ab->b_state->arcs_mtx); 9877046Sahrens /* remove the prefetch flag if we get a reference */ 9882391Smaybee if (ab->b_flags & ARC_PREFETCH) 9892391Smaybee ab->b_flags &= ~ARC_PREFETCH; 990789Sahrens } 991789Sahrens } 992789Sahrens 993789Sahrens static int 994789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 995789Sahrens { 996789Sahrens int cnt; 9973403Sbmc arc_state_t *state = ab->b_state; 998789Sahrens 9993403Sbmc ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 10003403Sbmc ASSERT(!GHOST_STATE(state)); 1001789Sahrens 1002789Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 10033403Sbmc (state != arc_anon)) { 10044309Smaybee uint64_t *size = &state->arcs_lsize[ab->b_type]; 10054309Smaybee 10063403Sbmc ASSERT(!MUTEX_HELD(&state->arcs_mtx)); 10073403Sbmc mutex_enter(&state->arcs_mtx); 1008789Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 10094309Smaybee list_insert_head(&state->arcs_list[ab->b_type], ab); 10101544Seschrock ASSERT(ab->b_datacnt > 0); 10114309Smaybee atomic_add_64(size, ab->b_size * ab->b_datacnt); 10123403Sbmc mutex_exit(&state->arcs_mtx); 1013789Sahrens } 1014789Sahrens return (cnt); 1015789Sahrens } 1016789Sahrens 1017789Sahrens /* 1018789Sahrens * Move the supplied buffer to the indicated state. The mutex 1019789Sahrens * for the buffer must be held by the caller. 1020789Sahrens */ 1021789Sahrens static void 10221544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1023789Sahrens { 10241544Seschrock arc_state_t *old_state = ab->b_state; 10253700Sek110237 int64_t refcnt = refcount_count(&ab->b_refcnt); 10263700Sek110237 uint64_t from_delta, to_delta; 1027789Sahrens 1028789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 10291544Seschrock ASSERT(new_state != old_state); 10301544Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 10311544Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 10321544Seschrock 10331544Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 1034789Sahrens 1035789Sahrens /* 1036789Sahrens * If this buffer is evictable, transfer it from the 1037789Sahrens * old state list to the new state list. 1038789Sahrens */ 10391544Seschrock if (refcnt == 0) { 10403403Sbmc if (old_state != arc_anon) { 10413403Sbmc int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx); 10424309Smaybee uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 10431544Seschrock 10441544Seschrock if (use_mutex) 10453403Sbmc mutex_enter(&old_state->arcs_mtx); 10461544Seschrock 10471544Seschrock ASSERT(list_link_active(&ab->b_arc_node)); 10484309Smaybee list_remove(&old_state->arcs_list[ab->b_type], ab); 1049789Sahrens 10502391Smaybee /* 10512391Smaybee * If prefetching out of the ghost cache, 10522391Smaybee * we will have a non-null datacnt. 10532391Smaybee */ 10542391Smaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 10552391Smaybee /* ghost elements have a ghost size */ 10561544Seschrock ASSERT(ab->b_buf == NULL); 10571544Seschrock from_delta = ab->b_size; 1058789Sahrens } 10594309Smaybee ASSERT3U(*size, >=, from_delta); 10604309Smaybee atomic_add_64(size, -from_delta); 10611544Seschrock 10621544Seschrock if (use_mutex) 10633403Sbmc mutex_exit(&old_state->arcs_mtx); 1064789Sahrens } 10653403Sbmc if (new_state != arc_anon) { 10663403Sbmc int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx); 10674309Smaybee uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1068789Sahrens 10691544Seschrock if (use_mutex) 10703403Sbmc mutex_enter(&new_state->arcs_mtx); 10711544Seschrock 10724309Smaybee list_insert_head(&new_state->arcs_list[ab->b_type], ab); 10731544Seschrock 10741544Seschrock /* ghost elements have a ghost size */ 10751544Seschrock if (GHOST_STATE(new_state)) { 10761544Seschrock ASSERT(ab->b_datacnt == 0); 10771544Seschrock ASSERT(ab->b_buf == NULL); 10781544Seschrock to_delta = ab->b_size; 10791544Seschrock } 10804309Smaybee atomic_add_64(size, to_delta); 10811544Seschrock 10821544Seschrock if (use_mutex) 10833403Sbmc mutex_exit(&new_state->arcs_mtx); 1084789Sahrens } 1085789Sahrens } 1086789Sahrens 1087789Sahrens ASSERT(!BUF_EMPTY(ab)); 10885450Sbrendan if (new_state == arc_anon) { 1089789Sahrens buf_hash_remove(ab); 1090789Sahrens } 1091789Sahrens 10921544Seschrock /* adjust state sizes */ 10931544Seschrock if (to_delta) 10943403Sbmc atomic_add_64(&new_state->arcs_size, to_delta); 10951544Seschrock if (from_delta) { 10963403Sbmc ASSERT3U(old_state->arcs_size, >=, from_delta); 10973403Sbmc atomic_add_64(&old_state->arcs_size, -from_delta); 1098789Sahrens } 1099789Sahrens ab->b_state = new_state; 11005450Sbrendan 11015450Sbrendan /* adjust l2arc hdr stats */ 11025450Sbrendan if (new_state == arc_l2c_only) 11035450Sbrendan l2arc_hdr_stat_add(); 11045450Sbrendan else if (old_state == arc_l2c_only) 11055450Sbrendan l2arc_hdr_stat_remove(); 1106789Sahrens } 1107789Sahrens 11084309Smaybee void 11098582SBrendan.Gregg@Sun.COM arc_space_consume(uint64_t space, arc_space_type_t type) 11104309Smaybee { 11118582SBrendan.Gregg@Sun.COM ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11128582SBrendan.Gregg@Sun.COM 11138582SBrendan.Gregg@Sun.COM switch (type) { 11148582SBrendan.Gregg@Sun.COM case ARC_SPACE_DATA: 11158582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, space); 11168582SBrendan.Gregg@Sun.COM break; 11178582SBrendan.Gregg@Sun.COM case ARC_SPACE_OTHER: 11188582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_other_size, space); 11198582SBrendan.Gregg@Sun.COM break; 11208582SBrendan.Gregg@Sun.COM case ARC_SPACE_HDRS: 11218582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_hdr_size, space); 11228582SBrendan.Gregg@Sun.COM break; 11238582SBrendan.Gregg@Sun.COM case ARC_SPACE_L2HDRS: 11248582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_hdr_size, space); 11258582SBrendan.Gregg@Sun.COM break; 11268582SBrendan.Gregg@Sun.COM } 11278582SBrendan.Gregg@Sun.COM 11284309Smaybee atomic_add_64(&arc_meta_used, space); 11294309Smaybee atomic_add_64(&arc_size, space); 11304309Smaybee } 11314309Smaybee 11324309Smaybee void 11338582SBrendan.Gregg@Sun.COM arc_space_return(uint64_t space, arc_space_type_t type) 11344309Smaybee { 11358582SBrendan.Gregg@Sun.COM ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11368582SBrendan.Gregg@Sun.COM 11378582SBrendan.Gregg@Sun.COM switch (type) { 11388582SBrendan.Gregg@Sun.COM case ARC_SPACE_DATA: 11398582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -space); 11408582SBrendan.Gregg@Sun.COM break; 11418582SBrendan.Gregg@Sun.COM case ARC_SPACE_OTHER: 11428582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_other_size, -space); 11438582SBrendan.Gregg@Sun.COM break; 11448582SBrendan.Gregg@Sun.COM case ARC_SPACE_HDRS: 11458582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_hdr_size, -space); 11468582SBrendan.Gregg@Sun.COM break; 11478582SBrendan.Gregg@Sun.COM case ARC_SPACE_L2HDRS: 11488582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 11498582SBrendan.Gregg@Sun.COM break; 11508582SBrendan.Gregg@Sun.COM } 11518582SBrendan.Gregg@Sun.COM 11524309Smaybee ASSERT(arc_meta_used >= space); 11534309Smaybee if (arc_meta_max < arc_meta_used) 11544309Smaybee arc_meta_max = arc_meta_used; 11554309Smaybee atomic_add_64(&arc_meta_used, -space); 11564309Smaybee ASSERT(arc_size >= space); 11574309Smaybee atomic_add_64(&arc_size, -space); 11584309Smaybee } 11594309Smaybee 11604309Smaybee void * 11614309Smaybee arc_data_buf_alloc(uint64_t size) 11624309Smaybee { 11634309Smaybee if (arc_evict_needed(ARC_BUFC_DATA)) 11644309Smaybee cv_signal(&arc_reclaim_thr_cv); 11654309Smaybee atomic_add_64(&arc_size, size); 11664309Smaybee return (zio_data_buf_alloc(size)); 11674309Smaybee } 11684309Smaybee 11694309Smaybee void 11704309Smaybee arc_data_buf_free(void *buf, uint64_t size) 11714309Smaybee { 11724309Smaybee zio_data_buf_free(buf, size); 11734309Smaybee ASSERT(arc_size >= size); 11744309Smaybee atomic_add_64(&arc_size, -size); 11754309Smaybee } 11764309Smaybee 1177789Sahrens arc_buf_t * 11783290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1179789Sahrens { 1180789Sahrens arc_buf_hdr_t *hdr; 1181789Sahrens arc_buf_t *buf; 1182789Sahrens 1183789Sahrens ASSERT3U(size, >, 0); 11846245Smaybee hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1185789Sahrens ASSERT(BUF_EMPTY(hdr)); 1186789Sahrens hdr->b_size = size; 11873290Sjohansen hdr->b_type = type; 1188789Sahrens hdr->b_spa = spa; 11893403Sbmc hdr->b_state = arc_anon; 1190789Sahrens hdr->b_arc_access = 0; 11916245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1192789Sahrens buf->b_hdr = hdr; 11932688Smaybee buf->b_data = NULL; 11941544Seschrock buf->b_efunc = NULL; 11951544Seschrock buf->b_private = NULL; 1196789Sahrens buf->b_next = NULL; 1197789Sahrens hdr->b_buf = buf; 11982688Smaybee arc_get_data_buf(buf); 11991544Seschrock hdr->b_datacnt = 1; 1200789Sahrens hdr->b_flags = 0; 1201789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1202789Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 1203789Sahrens 1204789Sahrens return (buf); 1205789Sahrens } 1206789Sahrens 12072688Smaybee static arc_buf_t * 12082688Smaybee arc_buf_clone(arc_buf_t *from) 12091544Seschrock { 12102688Smaybee arc_buf_t *buf; 12112688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 12122688Smaybee uint64_t size = hdr->b_size; 12131544Seschrock 12146245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 12152688Smaybee buf->b_hdr = hdr; 12162688Smaybee buf->b_data = NULL; 12172688Smaybee buf->b_efunc = NULL; 12182688Smaybee buf->b_private = NULL; 12192688Smaybee buf->b_next = hdr->b_buf; 12202688Smaybee hdr->b_buf = buf; 12212688Smaybee arc_get_data_buf(buf); 12222688Smaybee bcopy(from->b_data, buf->b_data, size); 12232688Smaybee hdr->b_datacnt += 1; 12242688Smaybee return (buf); 12251544Seschrock } 12261544Seschrock 12271544Seschrock void 12281544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 12291544Seschrock { 12302887Smaybee arc_buf_hdr_t *hdr; 12311544Seschrock kmutex_t *hash_lock; 12321544Seschrock 12332724Smaybee /* 12347545SMark.Maybee@Sun.COM * Check to see if this buffer is evicted. Callers 12357545SMark.Maybee@Sun.COM * must verify b_data != NULL to know if the add_ref 12367545SMark.Maybee@Sun.COM * was successful. 12372724Smaybee */ 12387545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 12397545SMark.Maybee@Sun.COM if (buf->b_data == NULL) { 12407545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 12412724Smaybee return; 12422887Smaybee } 12437545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 12447545SMark.Maybee@Sun.COM ASSERT(hdr != NULL); 12452887Smaybee hash_lock = HDR_LOCK(hdr); 12462724Smaybee mutex_enter(hash_lock); 12477545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 12487545SMark.Maybee@Sun.COM 12493403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 12501544Seschrock add_reference(hdr, hash_lock, tag); 12518582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 12522688Smaybee arc_access(hdr, hash_lock); 12532688Smaybee mutex_exit(hash_lock); 12543403Sbmc ARCSTAT_BUMP(arcstat_hits); 12553403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 12563403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 12573403Sbmc data, metadata, hits); 12581544Seschrock } 12591544Seschrock 12605450Sbrendan /* 12615450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 12625450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 12635450Sbrendan */ 12645450Sbrendan static void 12655450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 12665450Sbrendan void *data, size_t size) 12675450Sbrendan { 12685450Sbrendan if (HDR_L2_WRITING(hdr)) { 12695450Sbrendan l2arc_data_free_t *df; 12705450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 12715450Sbrendan df->l2df_data = data; 12725450Sbrendan df->l2df_size = size; 12735450Sbrendan df->l2df_func = free_func; 12745450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 12755450Sbrendan list_insert_head(l2arc_free_on_write, df); 12765450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 12775450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 12785450Sbrendan } else { 12795450Sbrendan free_func(data, size); 12805450Sbrendan } 12815450Sbrendan } 12825450Sbrendan 1283789Sahrens static void 12842688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 12851544Seschrock { 12861544Seschrock arc_buf_t **bufp; 12871544Seschrock 12881544Seschrock /* free up data associated with the buf */ 12891544Seschrock if (buf->b_data) { 12901544Seschrock arc_state_t *state = buf->b_hdr->b_state; 12911544Seschrock uint64_t size = buf->b_hdr->b_size; 12923290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 12931544Seschrock 12943093Sahrens arc_cksum_verify(buf); 12952688Smaybee if (!recycle) { 12963290Sjohansen if (type == ARC_BUFC_METADATA) { 12975450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 12985450Sbrendan buf->b_data, size); 12998582SBrendan.Gregg@Sun.COM arc_space_return(size, ARC_SPACE_DATA); 13003290Sjohansen } else { 13013290Sjohansen ASSERT(type == ARC_BUFC_DATA); 13025450Sbrendan arc_buf_data_free(buf->b_hdr, 13035450Sbrendan zio_data_buf_free, buf->b_data, size); 13048582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -size); 13054309Smaybee atomic_add_64(&arc_size, -size); 13063290Sjohansen } 13072688Smaybee } 13081544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 13094309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 13104309Smaybee 13111544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 13123403Sbmc ASSERT(state != arc_anon); 13134309Smaybee 13144309Smaybee ASSERT3U(*cnt, >=, size); 13154309Smaybee atomic_add_64(cnt, -size); 13161544Seschrock } 13173403Sbmc ASSERT3U(state->arcs_size, >=, size); 13183403Sbmc atomic_add_64(&state->arcs_size, -size); 13191544Seschrock buf->b_data = NULL; 13201544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 13211544Seschrock buf->b_hdr->b_datacnt -= 1; 13221544Seschrock } 13231544Seschrock 13241544Seschrock /* only remove the buf if requested */ 13251544Seschrock if (!all) 13261544Seschrock return; 13271544Seschrock 13281544Seschrock /* remove the buf from the hdr list */ 13291544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 13301544Seschrock continue; 13311544Seschrock *bufp = buf->b_next; 13321544Seschrock 13331544Seschrock ASSERT(buf->b_efunc == NULL); 13341544Seschrock 13351544Seschrock /* clean up the buf */ 13361544Seschrock buf->b_hdr = NULL; 13371544Seschrock kmem_cache_free(buf_cache, buf); 13381544Seschrock } 13391544Seschrock 13401544Seschrock static void 13411544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1342789Sahrens { 1343789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 13443403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 13451544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 13467046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 1347789Sahrens 13485450Sbrendan if (hdr->b_l2hdr != NULL) { 13495450Sbrendan if (!MUTEX_HELD(&l2arc_buflist_mtx)) { 13505450Sbrendan /* 13515450Sbrendan * To prevent arc_free() and l2arc_evict() from 13525450Sbrendan * attempting to free the same buffer at the same time, 13535450Sbrendan * a FREE_IN_PROGRESS flag is given to arc_free() to 13545450Sbrendan * give it priority. l2arc_evict() can't destroy this 13555450Sbrendan * header while we are waiting on l2arc_buflist_mtx. 13567361SBrendan.Gregg@Sun.COM * 13577361SBrendan.Gregg@Sun.COM * The hdr may be removed from l2ad_buflist before we 13587361SBrendan.Gregg@Sun.COM * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 13595450Sbrendan */ 13605450Sbrendan mutex_enter(&l2arc_buflist_mtx); 13617361SBrendan.Gregg@Sun.COM if (hdr->b_l2hdr != NULL) { 13627361SBrendan.Gregg@Sun.COM list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, 13637361SBrendan.Gregg@Sun.COM hdr); 13647361SBrendan.Gregg@Sun.COM } 13655450Sbrendan mutex_exit(&l2arc_buflist_mtx); 13665450Sbrendan } else { 13675450Sbrendan list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr); 13685450Sbrendan } 13695450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 13705450Sbrendan kmem_free(hdr->b_l2hdr, sizeof (l2arc_buf_hdr_t)); 13715450Sbrendan if (hdr->b_state == arc_l2c_only) 13725450Sbrendan l2arc_hdr_stat_remove(); 13735450Sbrendan hdr->b_l2hdr = NULL; 13745450Sbrendan } 13755450Sbrendan 1376789Sahrens if (!BUF_EMPTY(hdr)) { 13771544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1378789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1379789Sahrens hdr->b_birth = 0; 1380789Sahrens hdr->b_cksum0 = 0; 1381789Sahrens } 13821544Seschrock while (hdr->b_buf) { 1383789Sahrens arc_buf_t *buf = hdr->b_buf; 1384789Sahrens 13851544Seschrock if (buf->b_efunc) { 13861544Seschrock mutex_enter(&arc_eviction_mtx); 13877545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 13881544Seschrock ASSERT(buf->b_hdr != NULL); 13892688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 13901544Seschrock hdr->b_buf = buf->b_next; 13912887Smaybee buf->b_hdr = &arc_eviction_hdr; 13921544Seschrock buf->b_next = arc_eviction_list; 13931544Seschrock arc_eviction_list = buf; 13947545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 13951544Seschrock mutex_exit(&arc_eviction_mtx); 13961544Seschrock } else { 13972688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 13981544Seschrock } 1399789Sahrens } 14003093Sahrens if (hdr->b_freeze_cksum != NULL) { 14013093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 14023093Sahrens hdr->b_freeze_cksum = NULL; 14033093Sahrens } 14041544Seschrock 1405789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1406789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1407789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1408789Sahrens kmem_cache_free(hdr_cache, hdr); 1409789Sahrens } 1410789Sahrens 1411789Sahrens void 1412789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1413789Sahrens { 1414789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 14153403Sbmc int hashed = hdr->b_state != arc_anon; 14161544Seschrock 14171544Seschrock ASSERT(buf->b_efunc == NULL); 14181544Seschrock ASSERT(buf->b_data != NULL); 14191544Seschrock 14201544Seschrock if (hashed) { 14211544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 14221544Seschrock 14231544Seschrock mutex_enter(hash_lock); 14241544Seschrock (void) remove_reference(hdr, hash_lock, tag); 14251544Seschrock if (hdr->b_datacnt > 1) 14262688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14271544Seschrock else 14281544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 14291544Seschrock mutex_exit(hash_lock); 14301544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 14311544Seschrock int destroy_hdr; 14321544Seschrock /* 14331544Seschrock * We are in the middle of an async write. Don't destroy 14341544Seschrock * this buffer unless the write completes before we finish 14351544Seschrock * decrementing the reference count. 14361544Seschrock */ 14371544Seschrock mutex_enter(&arc_eviction_mtx); 14381544Seschrock (void) remove_reference(hdr, NULL, tag); 14391544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 14401544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 14411544Seschrock mutex_exit(&arc_eviction_mtx); 14421544Seschrock if (destroy_hdr) 14431544Seschrock arc_hdr_destroy(hdr); 14441544Seschrock } else { 14451544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 14461544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 14472688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14481544Seschrock } else { 14491544Seschrock arc_hdr_destroy(hdr); 14501544Seschrock } 14511544Seschrock } 14521544Seschrock } 14531544Seschrock 14541544Seschrock int 14551544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 14561544Seschrock { 14571544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1458789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 14591544Seschrock int no_callback = (buf->b_efunc == NULL); 14601544Seschrock 14613403Sbmc if (hdr->b_state == arc_anon) { 14621544Seschrock arc_buf_free(buf, tag); 14631544Seschrock return (no_callback); 14641544Seschrock } 1465789Sahrens 1466789Sahrens mutex_enter(hash_lock); 14673403Sbmc ASSERT(hdr->b_state != arc_anon); 14681544Seschrock ASSERT(buf->b_data != NULL); 1469789Sahrens 14701544Seschrock (void) remove_reference(hdr, hash_lock, tag); 14711544Seschrock if (hdr->b_datacnt > 1) { 14721544Seschrock if (no_callback) 14732688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14741544Seschrock } else if (no_callback) { 14751544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 14761544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1477789Sahrens } 14781544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 14791544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1480789Sahrens mutex_exit(hash_lock); 14811544Seschrock return (no_callback); 1482789Sahrens } 1483789Sahrens 1484789Sahrens int 1485789Sahrens arc_buf_size(arc_buf_t *buf) 1486789Sahrens { 1487789Sahrens return (buf->b_hdr->b_size); 1488789Sahrens } 1489789Sahrens 1490789Sahrens /* 1491789Sahrens * Evict buffers from list until we've removed the specified number of 1492789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 14932688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 14942688Smaybee * - look for a buffer to evict that is `bytes' long. 14952688Smaybee * - return the data block from this buffer rather than freeing it. 14962688Smaybee * This flag is used by callers that are trying to make space for a 14972688Smaybee * new buffer in a full arc cache. 14985642Smaybee * 14995642Smaybee * This function makes a "best effort". It skips over any buffers 15005642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 15015642Smaybee * It may also return without evicting as much space as requested. 1502789Sahrens */ 15032688Smaybee static void * 15045642Smaybee arc_evict(arc_state_t *state, spa_t *spa, int64_t bytes, boolean_t recycle, 15053290Sjohansen arc_buf_contents_t type) 1506789Sahrens { 1507789Sahrens arc_state_t *evicted_state; 15082688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 15092918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 15104309Smaybee list_t *list = &state->arcs_list[type]; 1511789Sahrens kmutex_t *hash_lock; 15122688Smaybee boolean_t have_lock; 15132918Smaybee void *stolen = NULL; 1514789Sahrens 15153403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1516789Sahrens 15173403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1518789Sahrens 15193403Sbmc mutex_enter(&state->arcs_mtx); 15203403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1521789Sahrens 15224309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15234309Smaybee ab_prev = list_prev(list, ab); 15242391Smaybee /* prefetch buffers have a minimum lifespan */ 15252688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 15265642Smaybee (spa && ab->b_spa != spa) || 15272688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 15282688Smaybee lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) { 15292391Smaybee skipped++; 15302391Smaybee continue; 15312391Smaybee } 15322918Smaybee /* "lookahead" for better eviction candidate */ 15332918Smaybee if (recycle && ab->b_size != bytes && 15342918Smaybee ab_prev && ab_prev->b_size == bytes) 15352688Smaybee continue; 1536789Sahrens hash_lock = HDR_LOCK(ab); 15372688Smaybee have_lock = MUTEX_HELD(hash_lock); 15382688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1539789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 15401544Seschrock ASSERT(ab->b_datacnt > 0); 15411544Seschrock while (ab->b_buf) { 15421544Seschrock arc_buf_t *buf = ab->b_buf; 15437545SMark.Maybee@Sun.COM if (!rw_tryenter(&buf->b_lock, RW_WRITER)) { 15447545SMark.Maybee@Sun.COM missed += 1; 15457545SMark.Maybee@Sun.COM break; 15467545SMark.Maybee@Sun.COM } 15472688Smaybee if (buf->b_data) { 15481544Seschrock bytes_evicted += ab->b_size; 15493290Sjohansen if (recycle && ab->b_type == type && 15505450Sbrendan ab->b_size == bytes && 15515450Sbrendan !HDR_L2_WRITING(ab)) { 15522918Smaybee stolen = buf->b_data; 15532918Smaybee recycle = FALSE; 15542918Smaybee } 15552688Smaybee } 15561544Seschrock if (buf->b_efunc) { 15571544Seschrock mutex_enter(&arc_eviction_mtx); 15582918Smaybee arc_buf_destroy(buf, 15592918Smaybee buf->b_data == stolen, FALSE); 15601544Seschrock ab->b_buf = buf->b_next; 15612887Smaybee buf->b_hdr = &arc_eviction_hdr; 15621544Seschrock buf->b_next = arc_eviction_list; 15631544Seschrock arc_eviction_list = buf; 15641544Seschrock mutex_exit(&arc_eviction_mtx); 15657545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 15661544Seschrock } else { 15677545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 15682918Smaybee arc_buf_destroy(buf, 15692918Smaybee buf->b_data == stolen, TRUE); 15701544Seschrock } 15711544Seschrock } 15727545SMark.Maybee@Sun.COM if (ab->b_datacnt == 0) { 15737545SMark.Maybee@Sun.COM arc_change_state(evicted_state, ab, hash_lock); 15747545SMark.Maybee@Sun.COM ASSERT(HDR_IN_HASH_TABLE(ab)); 15757545SMark.Maybee@Sun.COM ab->b_flags |= ARC_IN_HASH_TABLE; 15767545SMark.Maybee@Sun.COM ab->b_flags &= ~ARC_BUF_AVAILABLE; 15777545SMark.Maybee@Sun.COM DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 15787545SMark.Maybee@Sun.COM } 15792688Smaybee if (!have_lock) 15802688Smaybee mutex_exit(hash_lock); 15811544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1582789Sahrens break; 1583789Sahrens } else { 15842688Smaybee missed += 1; 1585789Sahrens } 1586789Sahrens } 15873403Sbmc 15883403Sbmc mutex_exit(&evicted_state->arcs_mtx); 15893403Sbmc mutex_exit(&state->arcs_mtx); 1590789Sahrens 1591789Sahrens if (bytes_evicted < bytes) 1592789Sahrens dprintf("only evicted %lld bytes from %x", 1593789Sahrens (longlong_t)bytes_evicted, state); 1594789Sahrens 15952688Smaybee if (skipped) 15963403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 15973403Sbmc 15982688Smaybee if (missed) 15993403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 16003403Sbmc 16014709Smaybee /* 16024709Smaybee * We have just evicted some date into the ghost state, make 16034709Smaybee * sure we also adjust the ghost state size if necessary. 16044709Smaybee */ 16054709Smaybee if (arc_no_grow && 16064709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 16074709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 16084709Smaybee arc_mru_ghost->arcs_size - arc_c; 16094709Smaybee 16104709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 16114709Smaybee int64_t todelete = 16124709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 16135642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 16144709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 16154709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 16164709Smaybee arc_mru_ghost->arcs_size + 16174709Smaybee arc_mfu_ghost->arcs_size - arc_c); 16185642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 16194709Smaybee } 16204709Smaybee } 16214709Smaybee 16222918Smaybee return (stolen); 1623789Sahrens } 1624789Sahrens 1625789Sahrens /* 1626789Sahrens * Remove buffers from list until we've removed the specified number of 1627789Sahrens * bytes. Destroy the buffers that are removed. 1628789Sahrens */ 1629789Sahrens static void 16305642Smaybee arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes) 1631789Sahrens { 1632789Sahrens arc_buf_hdr_t *ab, *ab_prev; 16334309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1634789Sahrens kmutex_t *hash_lock; 16351544Seschrock uint64_t bytes_deleted = 0; 16363700Sek110237 uint64_t bufs_skipped = 0; 1637789Sahrens 16381544Seschrock ASSERT(GHOST_STATE(state)); 1639789Sahrens top: 16403403Sbmc mutex_enter(&state->arcs_mtx); 16414309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 16424309Smaybee ab_prev = list_prev(list, ab); 16435642Smaybee if (spa && ab->b_spa != spa) 16445642Smaybee continue; 1645789Sahrens hash_lock = HDR_LOCK(ab); 1646789Sahrens if (mutex_tryenter(hash_lock)) { 16472391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 16481544Seschrock ASSERT(ab->b_buf == NULL); 16493403Sbmc ARCSTAT_BUMP(arcstat_deleted); 16501544Seschrock bytes_deleted += ab->b_size; 16515450Sbrendan 16525450Sbrendan if (ab->b_l2hdr != NULL) { 16535450Sbrendan /* 16545450Sbrendan * This buffer is cached on the 2nd Level ARC; 16555450Sbrendan * don't destroy the header. 16565450Sbrendan */ 16575450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 16585450Sbrendan mutex_exit(hash_lock); 16595450Sbrendan } else { 16605450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 16615450Sbrendan mutex_exit(hash_lock); 16625450Sbrendan arc_hdr_destroy(ab); 16635450Sbrendan } 16645450Sbrendan 1665789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1666789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1667789Sahrens break; 1668789Sahrens } else { 1669789Sahrens if (bytes < 0) { 16703403Sbmc mutex_exit(&state->arcs_mtx); 1671789Sahrens mutex_enter(hash_lock); 1672789Sahrens mutex_exit(hash_lock); 1673789Sahrens goto top; 1674789Sahrens } 1675789Sahrens bufs_skipped += 1; 1676789Sahrens } 1677789Sahrens } 16783403Sbmc mutex_exit(&state->arcs_mtx); 1679789Sahrens 16804309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 16814309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 16824309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 16834309Smaybee goto top; 16844309Smaybee } 16854309Smaybee 1686789Sahrens if (bufs_skipped) { 16873403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1688789Sahrens ASSERT(bytes >= 0); 1689789Sahrens } 1690789Sahrens 1691789Sahrens if (bytes_deleted < bytes) 1692789Sahrens dprintf("only deleted %lld bytes from %p", 1693789Sahrens (longlong_t)bytes_deleted, state); 1694789Sahrens } 1695789Sahrens 1696789Sahrens static void 1697789Sahrens arc_adjust(void) 1698789Sahrens { 16998582SBrendan.Gregg@Sun.COM int64_t adjustment, delta; 17008582SBrendan.Gregg@Sun.COM 17018582SBrendan.Gregg@Sun.COM /* 17028582SBrendan.Gregg@Sun.COM * Adjust MRU size 17038582SBrendan.Gregg@Sun.COM */ 17048582SBrendan.Gregg@Sun.COM 17058582SBrendan.Gregg@Sun.COM adjustment = MIN(arc_size - arc_c, 17068582SBrendan.Gregg@Sun.COM arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - arc_p); 17078582SBrendan.Gregg@Sun.COM 17088582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 17098582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 17108582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA); 17118582SBrendan.Gregg@Sun.COM adjustment -= delta; 17124309Smaybee } 17134309Smaybee 17148582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 17158582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 17168582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, 17175642Smaybee ARC_BUFC_METADATA); 1718789Sahrens } 1719789Sahrens 17208582SBrendan.Gregg@Sun.COM /* 17218582SBrendan.Gregg@Sun.COM * Adjust MFU size 17228582SBrendan.Gregg@Sun.COM */ 17238582SBrendan.Gregg@Sun.COM 17248582SBrendan.Gregg@Sun.COM adjustment = arc_size - arc_c; 17258582SBrendan.Gregg@Sun.COM 17268582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 17278582SBrendan.Gregg@Sun.COM delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 17288582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA); 17298582SBrendan.Gregg@Sun.COM adjustment -= delta; 17308582SBrendan.Gregg@Sun.COM } 17318582SBrendan.Gregg@Sun.COM 17328582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 17338582SBrendan.Gregg@Sun.COM int64_t delta = MIN(adjustment, 17348582SBrendan.Gregg@Sun.COM arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 17358582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, 17368582SBrendan.Gregg@Sun.COM ARC_BUFC_METADATA); 17378582SBrendan.Gregg@Sun.COM } 17388582SBrendan.Gregg@Sun.COM 17398582SBrendan.Gregg@Sun.COM /* 17408582SBrendan.Gregg@Sun.COM * Adjust ghost lists 17418582SBrendan.Gregg@Sun.COM */ 17428582SBrendan.Gregg@Sun.COM 17438582SBrendan.Gregg@Sun.COM adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 17448582SBrendan.Gregg@Sun.COM 17458582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 17468582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru_ghost->arcs_size, adjustment); 17478582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mru_ghost, NULL, delta); 17488582SBrendan.Gregg@Sun.COM } 17498582SBrendan.Gregg@Sun.COM 17508582SBrendan.Gregg@Sun.COM adjustment = 17518582SBrendan.Gregg@Sun.COM arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 17528582SBrendan.Gregg@Sun.COM 17538582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 17548582SBrendan.Gregg@Sun.COM delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 17558582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mfu_ghost, NULL, delta); 1756789Sahrens } 1757789Sahrens } 1758789Sahrens 17591544Seschrock static void 17601544Seschrock arc_do_user_evicts(void) 17611544Seschrock { 17621544Seschrock mutex_enter(&arc_eviction_mtx); 17631544Seschrock while (arc_eviction_list != NULL) { 17641544Seschrock arc_buf_t *buf = arc_eviction_list; 17651544Seschrock arc_eviction_list = buf->b_next; 17667545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 17671544Seschrock buf->b_hdr = NULL; 17687545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 17691544Seschrock mutex_exit(&arc_eviction_mtx); 17701544Seschrock 17711819Smaybee if (buf->b_efunc != NULL) 17721819Smaybee VERIFY(buf->b_efunc(buf) == 0); 17731544Seschrock 17741544Seschrock buf->b_efunc = NULL; 17751544Seschrock buf->b_private = NULL; 17761544Seschrock kmem_cache_free(buf_cache, buf); 17771544Seschrock mutex_enter(&arc_eviction_mtx); 17781544Seschrock } 17791544Seschrock mutex_exit(&arc_eviction_mtx); 17801544Seschrock } 17811544Seschrock 1782789Sahrens /* 17835642Smaybee * Flush all *evictable* data from the cache for the given spa. 1784789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1785789Sahrens */ 1786789Sahrens void 17875642Smaybee arc_flush(spa_t *spa) 1788789Sahrens { 17895642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 17905642Smaybee (void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_DATA); 17915642Smaybee if (spa) 17925642Smaybee break; 17935642Smaybee } 17945642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 17955642Smaybee (void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_METADATA); 17965642Smaybee if (spa) 17975642Smaybee break; 17985642Smaybee } 17995642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 18005642Smaybee (void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_DATA); 18015642Smaybee if (spa) 18025642Smaybee break; 18035642Smaybee } 18045642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 18055642Smaybee (void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_METADATA); 18065642Smaybee if (spa) 18075642Smaybee break; 18085642Smaybee } 18095642Smaybee 18105642Smaybee arc_evict_ghost(arc_mru_ghost, spa, -1); 18115642Smaybee arc_evict_ghost(arc_mfu_ghost, spa, -1); 18121544Seschrock 18131544Seschrock mutex_enter(&arc_reclaim_thr_lock); 18141544Seschrock arc_do_user_evicts(); 18151544Seschrock mutex_exit(&arc_reclaim_thr_lock); 18165642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1817789Sahrens } 1818789Sahrens 1819789Sahrens void 18203158Smaybee arc_shrink(void) 1821789Sahrens { 18223403Sbmc if (arc_c > arc_c_min) { 18233158Smaybee uint64_t to_free; 1824789Sahrens 18252048Sstans #ifdef _KERNEL 18263403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 18272048Sstans #else 18283403Sbmc to_free = arc_c >> arc_shrink_shift; 18292048Sstans #endif 18303403Sbmc if (arc_c > arc_c_min + to_free) 18313403Sbmc atomic_add_64(&arc_c, -to_free); 18323158Smaybee else 18333403Sbmc arc_c = arc_c_min; 18342048Sstans 18353403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 18363403Sbmc if (arc_c > arc_size) 18373403Sbmc arc_c = MAX(arc_size, arc_c_min); 18383403Sbmc if (arc_p > arc_c) 18393403Sbmc arc_p = (arc_c >> 1); 18403403Sbmc ASSERT(arc_c >= arc_c_min); 18413403Sbmc ASSERT((int64_t)arc_p >= 0); 18423158Smaybee } 1843789Sahrens 18443403Sbmc if (arc_size > arc_c) 18453158Smaybee arc_adjust(); 1846789Sahrens } 1847789Sahrens 1848789Sahrens static int 1849789Sahrens arc_reclaim_needed(void) 1850789Sahrens { 1851789Sahrens uint64_t extra; 1852789Sahrens 1853789Sahrens #ifdef _KERNEL 18542048Sstans 18552048Sstans if (needfree) 18562048Sstans return (1); 18572048Sstans 1858789Sahrens /* 1859789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1860789Sahrens */ 1861789Sahrens extra = desfree; 1862789Sahrens 1863789Sahrens /* 1864789Sahrens * check that we're out of range of the pageout scanner. It starts to 1865789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1866789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1867789Sahrens * number of needed free pages. We add extra pages here to make sure 1868789Sahrens * the scanner doesn't start up while we're freeing memory. 1869789Sahrens */ 1870789Sahrens if (freemem < lotsfree + needfree + extra) 1871789Sahrens return (1); 1872789Sahrens 1873789Sahrens /* 1874789Sahrens * check to make sure that swapfs has enough space so that anon 18755450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1876789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1877789Sahrens * swap pages. We also add a bit of extra here just to prevent 1878789Sahrens * circumstances from getting really dire. 1879789Sahrens */ 1880789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1881789Sahrens return (1); 1882789Sahrens 18831936Smaybee #if defined(__i386) 1884789Sahrens /* 1885789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1886789Sahrens * kernel heap space before we ever run out of available physical 1887789Sahrens * memory. Most checks of the size of the heap_area compare against 1888789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1889789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1890789Sahrens * which is so low that it's useless. In this comparison, we seek to 1891789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 18925450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1893789Sahrens * free) 1894789Sahrens */ 1895789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1896789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1897789Sahrens return (1); 1898789Sahrens #endif 1899789Sahrens 1900789Sahrens #else 1901789Sahrens if (spa_get_random(100) == 0) 1902789Sahrens return (1); 1903789Sahrens #endif 1904789Sahrens return (0); 1905789Sahrens } 1906789Sahrens 1907789Sahrens static void 1908789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1909789Sahrens { 1910789Sahrens size_t i; 1911789Sahrens kmem_cache_t *prev_cache = NULL; 19123290Sjohansen kmem_cache_t *prev_data_cache = NULL; 1913789Sahrens extern kmem_cache_t *zio_buf_cache[]; 19143290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 1915789Sahrens 19161484Sek110237 #ifdef _KERNEL 19174309Smaybee if (arc_meta_used >= arc_meta_limit) { 19184309Smaybee /* 19194309Smaybee * We are exceeding our meta-data cache limit. 19204309Smaybee * Purge some DNLC entries to release holds on meta-data. 19214309Smaybee */ 19224309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 19234309Smaybee } 19241936Smaybee #if defined(__i386) 19251936Smaybee /* 19261936Smaybee * Reclaim unused memory from all kmem caches. 19271936Smaybee */ 19281936Smaybee kmem_reap(); 19291936Smaybee #endif 19301484Sek110237 #endif 19311484Sek110237 1932789Sahrens /* 19335450Sbrendan * An aggressive reclamation will shrink the cache size as well as 19341544Seschrock * reap free buffers from the arc kmem caches. 1935789Sahrens */ 1936789Sahrens if (strat == ARC_RECLAIM_AGGR) 19373158Smaybee arc_shrink(); 1938789Sahrens 1939789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 1940789Sahrens if (zio_buf_cache[i] != prev_cache) { 1941789Sahrens prev_cache = zio_buf_cache[i]; 1942789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 1943789Sahrens } 19443290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 19453290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 19463290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 19473290Sjohansen } 1948789Sahrens } 19491544Seschrock kmem_cache_reap_now(buf_cache); 19501544Seschrock kmem_cache_reap_now(hdr_cache); 1951789Sahrens } 1952789Sahrens 1953789Sahrens static void 1954789Sahrens arc_reclaim_thread(void) 1955789Sahrens { 1956789Sahrens clock_t growtime = 0; 1957789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 1958789Sahrens callb_cpr_t cpr; 1959789Sahrens 1960789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 1961789Sahrens 1962789Sahrens mutex_enter(&arc_reclaim_thr_lock); 1963789Sahrens while (arc_thread_exit == 0) { 1964789Sahrens if (arc_reclaim_needed()) { 1965789Sahrens 19663403Sbmc if (arc_no_grow) { 1967789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 1968789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1969789Sahrens } else { 1970789Sahrens last_reclaim = ARC_RECLAIM_CONS; 1971789Sahrens } 1972789Sahrens } else { 19733403Sbmc arc_no_grow = TRUE; 1974789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1975789Sahrens membar_producer(); 1976789Sahrens } 1977789Sahrens 1978789Sahrens /* reset the growth delay for every reclaim */ 1979789Sahrens growtime = lbolt + (arc_grow_retry * hz); 1980789Sahrens 1981789Sahrens arc_kmem_reap_now(last_reclaim); 19826987Sbrendan arc_warm = B_TRUE; 1983789Sahrens 19844309Smaybee } else if (arc_no_grow && lbolt >= growtime) { 19853403Sbmc arc_no_grow = FALSE; 1986789Sahrens } 1987789Sahrens 19883403Sbmc if (2 * arc_c < arc_size + 19893403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 19903298Smaybee arc_adjust(); 19913298Smaybee 19921544Seschrock if (arc_eviction_list != NULL) 19931544Seschrock arc_do_user_evicts(); 19941544Seschrock 1995789Sahrens /* block until needed, or one second, whichever is shorter */ 1996789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 1997789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 1998789Sahrens &arc_reclaim_thr_lock, (lbolt + hz)); 1999789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2000789Sahrens } 2001789Sahrens 2002789Sahrens arc_thread_exit = 0; 2003789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 2004789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2005789Sahrens thread_exit(); 2006789Sahrens } 2007789Sahrens 20081544Seschrock /* 20091544Seschrock * Adapt arc info given the number of bytes we are trying to add and 20101544Seschrock * the state that we are comming from. This function is only called 20111544Seschrock * when we are adding new content to the cache. 20121544Seschrock */ 2013789Sahrens static void 20141544Seschrock arc_adapt(int bytes, arc_state_t *state) 2015789Sahrens { 20161544Seschrock int mult; 20178582SBrendan.Gregg@Sun.COM uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 20181544Seschrock 20195450Sbrendan if (state == arc_l2c_only) 20205450Sbrendan return; 20215450Sbrendan 20221544Seschrock ASSERT(bytes > 0); 2023789Sahrens /* 20241544Seschrock * Adapt the target size of the MRU list: 20251544Seschrock * - if we just hit in the MRU ghost list, then increase 20261544Seschrock * the target size of the MRU list. 20271544Seschrock * - if we just hit in the MFU ghost list, then increase 20281544Seschrock * the target size of the MFU list by decreasing the 20291544Seschrock * target size of the MRU list. 2030789Sahrens */ 20313403Sbmc if (state == arc_mru_ghost) { 20323403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 20333403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 20341544Seschrock 20358582SBrendan.Gregg@Sun.COM arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 20363403Sbmc } else if (state == arc_mfu_ghost) { 20378582SBrendan.Gregg@Sun.COM uint64_t delta; 20388582SBrendan.Gregg@Sun.COM 20393403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 20403403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 20411544Seschrock 20428582SBrendan.Gregg@Sun.COM delta = MIN(bytes * mult, arc_p); 20438582SBrendan.Gregg@Sun.COM arc_p = MAX(arc_p_min, arc_p - delta); 20441544Seschrock } 20453403Sbmc ASSERT((int64_t)arc_p >= 0); 2046789Sahrens 2047789Sahrens if (arc_reclaim_needed()) { 2048789Sahrens cv_signal(&arc_reclaim_thr_cv); 2049789Sahrens return; 2050789Sahrens } 2051789Sahrens 20523403Sbmc if (arc_no_grow) 2053789Sahrens return; 2054789Sahrens 20553403Sbmc if (arc_c >= arc_c_max) 20561544Seschrock return; 20571544Seschrock 2058789Sahrens /* 20591544Seschrock * If we're within (2 * maxblocksize) bytes of the target 20601544Seschrock * cache size, increment the target cache size 2061789Sahrens */ 20623403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 20633403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 20643403Sbmc if (arc_c > arc_c_max) 20653403Sbmc arc_c = arc_c_max; 20663403Sbmc else if (state == arc_anon) 20673403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 20683403Sbmc if (arc_p > arc_c) 20693403Sbmc arc_p = arc_c; 2070789Sahrens } 20713403Sbmc ASSERT((int64_t)arc_p >= 0); 2072789Sahrens } 2073789Sahrens 2074789Sahrens /* 20751544Seschrock * Check if the cache has reached its limits and eviction is required 20761544Seschrock * prior to insert. 2077789Sahrens */ 2078789Sahrens static int 20794309Smaybee arc_evict_needed(arc_buf_contents_t type) 2080789Sahrens { 20814309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 20824309Smaybee return (1); 20834309Smaybee 20844309Smaybee #ifdef _KERNEL 20854309Smaybee /* 20864309Smaybee * If zio data pages are being allocated out of a separate heap segment, 20874309Smaybee * then enforce that the size of available vmem for this area remains 20884309Smaybee * above about 1/32nd free. 20894309Smaybee */ 20904309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 20914309Smaybee vmem_size(zio_arena, VMEM_FREE) < 20924309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 20934309Smaybee return (1); 20944309Smaybee #endif 20954309Smaybee 2096789Sahrens if (arc_reclaim_needed()) 2097789Sahrens return (1); 2098789Sahrens 20993403Sbmc return (arc_size > arc_c); 2100789Sahrens } 2101789Sahrens 2102789Sahrens /* 21032688Smaybee * The buffer, supplied as the first argument, needs a data block. 21042688Smaybee * So, if we are at cache max, determine which cache should be victimized. 21052688Smaybee * We have the following cases: 2106789Sahrens * 21073403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2108789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2109789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2110789Sahrens * 21113403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2112789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2113789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2114789Sahrens * entries. 2115789Sahrens * 21163403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2117789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2118789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2119789Sahrens * the MFU side, so the MRU side needs to be victimized. 2120789Sahrens * 21213403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2122789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2123789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2124789Sahrens */ 2125789Sahrens static void 21262688Smaybee arc_get_data_buf(arc_buf_t *buf) 2127789Sahrens { 21283290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 21293290Sjohansen uint64_t size = buf->b_hdr->b_size; 21303290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 21312688Smaybee 21322688Smaybee arc_adapt(size, state); 2133789Sahrens 21342688Smaybee /* 21352688Smaybee * We have not yet reached cache maximum size, 21362688Smaybee * just allocate a new buffer. 21372688Smaybee */ 21384309Smaybee if (!arc_evict_needed(type)) { 21393290Sjohansen if (type == ARC_BUFC_METADATA) { 21403290Sjohansen buf->b_data = zio_buf_alloc(size); 21418582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 21423290Sjohansen } else { 21433290Sjohansen ASSERT(type == ARC_BUFC_DATA); 21443290Sjohansen buf->b_data = zio_data_buf_alloc(size); 21458582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 21464309Smaybee atomic_add_64(&arc_size, size); 21473290Sjohansen } 21482688Smaybee goto out; 21492688Smaybee } 21502688Smaybee 21512688Smaybee /* 21522688Smaybee * If we are prefetching from the mfu ghost list, this buffer 21532688Smaybee * will end up on the mru list; so steal space from there. 21542688Smaybee */ 21553403Sbmc if (state == arc_mfu_ghost) 21563403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 21573403Sbmc else if (state == arc_mru_ghost) 21583403Sbmc state = arc_mru; 2159789Sahrens 21603403Sbmc if (state == arc_mru || state == arc_anon) { 21613403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 21628582SBrendan.Gregg@Sun.COM state = (arc_mfu->arcs_lsize[type] >= size && 21634309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2164789Sahrens } else { 21652688Smaybee /* MFU cases */ 21663403Sbmc uint64_t mfu_space = arc_c - arc_p; 21678582SBrendan.Gregg@Sun.COM state = (arc_mru->arcs_lsize[type] >= size && 21684309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 21692688Smaybee } 21705642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 21713290Sjohansen if (type == ARC_BUFC_METADATA) { 21723290Sjohansen buf->b_data = zio_buf_alloc(size); 21738582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 21743290Sjohansen } else { 21753290Sjohansen ASSERT(type == ARC_BUFC_DATA); 21763290Sjohansen buf->b_data = zio_data_buf_alloc(size); 21778582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 21784309Smaybee atomic_add_64(&arc_size, size); 21793290Sjohansen } 21803403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 21812688Smaybee } 21822688Smaybee ASSERT(buf->b_data != NULL); 21832688Smaybee out: 21842688Smaybee /* 21852688Smaybee * Update the state size. Note that ghost states have a 21862688Smaybee * "ghost size" and so don't need to be updated. 21872688Smaybee */ 21882688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 21892688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 21902688Smaybee 21913403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 21922688Smaybee if (list_link_active(&hdr->b_arc_node)) { 21932688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 21944309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2195789Sahrens } 21963298Smaybee /* 21973298Smaybee * If we are growing the cache, and we are adding anonymous 21983403Sbmc * data, and we have outgrown arc_p, update arc_p 21993298Smaybee */ 22003403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 22013403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 22023403Sbmc arc_p = MIN(arc_c, arc_p + size); 2203789Sahrens } 2204789Sahrens } 2205789Sahrens 2206789Sahrens /* 2207789Sahrens * This routine is called whenever a buffer is accessed. 22081544Seschrock * NOTE: the hash lock is dropped in this function. 2209789Sahrens */ 2210789Sahrens static void 22112688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2212789Sahrens { 2213789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2214789Sahrens 22153403Sbmc if (buf->b_state == arc_anon) { 2216789Sahrens /* 2217789Sahrens * This buffer is not in the cache, and does not 2218789Sahrens * appear in our "ghost" list. Add the new buffer 2219789Sahrens * to the MRU state. 2220789Sahrens */ 2221789Sahrens 2222789Sahrens ASSERT(buf->b_arc_access == 0); 2223789Sahrens buf->b_arc_access = lbolt; 22241544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 22253403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2226789Sahrens 22273403Sbmc } else if (buf->b_state == arc_mru) { 2228789Sahrens /* 22292391Smaybee * If this buffer is here because of a prefetch, then either: 22302391Smaybee * - clear the flag if this is a "referencing" read 22312391Smaybee * (any subsequent access will bump this into the MFU state). 22322391Smaybee * or 22332391Smaybee * - move the buffer to the head of the list if this is 22342391Smaybee * another prefetch (to make it less likely to be evicted). 2235789Sahrens */ 2236789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 22372391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 22382391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 22392391Smaybee } else { 22402391Smaybee buf->b_flags &= ~ARC_PREFETCH; 22413403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 22422391Smaybee } 22432391Smaybee buf->b_arc_access = lbolt; 2244789Sahrens return; 2245789Sahrens } 2246789Sahrens 2247789Sahrens /* 2248789Sahrens * This buffer has been "accessed" only once so far, 2249789Sahrens * but it is still in the cache. Move it to the MFU 2250789Sahrens * state. 2251789Sahrens */ 2252789Sahrens if (lbolt > buf->b_arc_access + ARC_MINTIME) { 2253789Sahrens /* 2254789Sahrens * More than 125ms have passed since we 2255789Sahrens * instantiated this buffer. Move it to the 2256789Sahrens * most frequently used state. 2257789Sahrens */ 2258789Sahrens buf->b_arc_access = lbolt; 22591544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 22603403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2261789Sahrens } 22623403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 22633403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2264789Sahrens arc_state_t *new_state; 2265789Sahrens /* 2266789Sahrens * This buffer has been "accessed" recently, but 2267789Sahrens * was evicted from the cache. Move it to the 2268789Sahrens * MFU state. 2269789Sahrens */ 2270789Sahrens 2271789Sahrens if (buf->b_flags & ARC_PREFETCH) { 22723403Sbmc new_state = arc_mru; 22732391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 22742391Smaybee buf->b_flags &= ~ARC_PREFETCH; 22751544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2276789Sahrens } else { 22773403Sbmc new_state = arc_mfu; 22781544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2279789Sahrens } 2280789Sahrens 2281789Sahrens buf->b_arc_access = lbolt; 2282789Sahrens arc_change_state(new_state, buf, hash_lock); 2283789Sahrens 22843403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 22853403Sbmc } else if (buf->b_state == arc_mfu) { 2286789Sahrens /* 2287789Sahrens * This buffer has been accessed more than once and is 2288789Sahrens * still in the cache. Keep it in the MFU state. 2289789Sahrens * 22902391Smaybee * NOTE: an add_reference() that occurred when we did 22912391Smaybee * the arc_read() will have kicked this off the list. 22922391Smaybee * If it was a prefetch, we will explicitly move it to 22932391Smaybee * the head of the list now. 2294789Sahrens */ 22952391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 22962391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 22972391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 22982391Smaybee } 22993403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 23002391Smaybee buf->b_arc_access = lbolt; 23013403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 23023403Sbmc arc_state_t *new_state = arc_mfu; 2303789Sahrens /* 2304789Sahrens * This buffer has been accessed more than once but has 2305789Sahrens * been evicted from the cache. Move it back to the 2306789Sahrens * MFU state. 2307789Sahrens */ 2308789Sahrens 23092391Smaybee if (buf->b_flags & ARC_PREFETCH) { 23102391Smaybee /* 23112391Smaybee * This is a prefetch access... 23122391Smaybee * move this block back to the MRU state. 23132391Smaybee */ 23142391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 23153403Sbmc new_state = arc_mru; 23162391Smaybee } 23172391Smaybee 2318789Sahrens buf->b_arc_access = lbolt; 23191544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23202391Smaybee arc_change_state(new_state, buf, hash_lock); 2321789Sahrens 23223403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 23235450Sbrendan } else if (buf->b_state == arc_l2c_only) { 23245450Sbrendan /* 23255450Sbrendan * This buffer is on the 2nd Level ARC. 23265450Sbrendan */ 23275450Sbrendan 23285450Sbrendan buf->b_arc_access = lbolt; 23295450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23305450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2331789Sahrens } else { 2332789Sahrens ASSERT(!"invalid arc state"); 2333789Sahrens } 2334789Sahrens } 2335789Sahrens 2336789Sahrens /* a generic arc_done_func_t which you can use */ 2337789Sahrens /* ARGSUSED */ 2338789Sahrens void 2339789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2340789Sahrens { 2341789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 23421544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2343789Sahrens } 2344789Sahrens 23454309Smaybee /* a generic arc_done_func_t */ 2346789Sahrens void 2347789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2348789Sahrens { 2349789Sahrens arc_buf_t **bufp = arg; 2350789Sahrens if (zio && zio->io_error) { 23511544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2352789Sahrens *bufp = NULL; 2353789Sahrens } else { 2354789Sahrens *bufp = buf; 2355789Sahrens } 2356789Sahrens } 2357789Sahrens 2358789Sahrens static void 2359789Sahrens arc_read_done(zio_t *zio) 2360789Sahrens { 23611589Smaybee arc_buf_hdr_t *hdr, *found; 2362789Sahrens arc_buf_t *buf; 2363789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2364789Sahrens kmutex_t *hash_lock; 2365789Sahrens arc_callback_t *callback_list, *acb; 2366789Sahrens int freeable = FALSE; 2367789Sahrens 2368789Sahrens buf = zio->io_private; 2369789Sahrens hdr = buf->b_hdr; 2370789Sahrens 23711589Smaybee /* 23721589Smaybee * The hdr was inserted into hash-table and removed from lists 23731589Smaybee * prior to starting I/O. We should find this header, since 23741589Smaybee * it's in the hash table, and it should be legit since it's 23751589Smaybee * not possible to evict it during the I/O. The only possible 23761589Smaybee * reason for it not to be found is if we were freed during the 23771589Smaybee * read. 23781589Smaybee */ 23791589Smaybee found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth, 23803093Sahrens &hash_lock); 2381789Sahrens 23821589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 23835450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 23845450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 23855450Sbrendan 23866987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 23875450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 23887237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2389789Sahrens 2390789Sahrens /* byteswap if necessary */ 2391789Sahrens callback_list = hdr->b_acb; 2392789Sahrens ASSERT(callback_list != NULL); 23937046Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 23947046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 23957046Sahrens byteswap_uint64_array : 23967046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 23977046Sahrens func(buf->b_data, hdr->b_size); 23987046Sahrens } 2399789Sahrens 24005450Sbrendan arc_cksum_compute(buf, B_FALSE); 24013093Sahrens 2402789Sahrens /* create copies of the data buffer for the callers */ 2403789Sahrens abuf = buf; 2404789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2405789Sahrens if (acb->acb_done) { 24062688Smaybee if (abuf == NULL) 24072688Smaybee abuf = arc_buf_clone(buf); 2408789Sahrens acb->acb_buf = abuf; 2409789Sahrens abuf = NULL; 2410789Sahrens } 2411789Sahrens } 2412789Sahrens hdr->b_acb = NULL; 2413789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 24141544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 24151544Seschrock if (abuf == buf) 24161544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 2417789Sahrens 2418789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2419789Sahrens 2420789Sahrens if (zio->io_error != 0) { 2421789Sahrens hdr->b_flags |= ARC_IO_ERROR; 24223403Sbmc if (hdr->b_state != arc_anon) 24233403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 24241544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 24251544Seschrock buf_hash_remove(hdr); 2426789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2427789Sahrens } 2428789Sahrens 24291544Seschrock /* 24302391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 24312391Smaybee * that the hdr (and hence the cv) might be freed before we get to 24322391Smaybee * the cv_broadcast(). 24331544Seschrock */ 24341544Seschrock cv_broadcast(&hdr->b_cv); 24351544Seschrock 24361589Smaybee if (hash_lock) { 2437789Sahrens /* 2438789Sahrens * Only call arc_access on anonymous buffers. This is because 2439789Sahrens * if we've issued an I/O for an evicted buffer, we've already 2440789Sahrens * called arc_access (to prevent any simultaneous readers from 2441789Sahrens * getting confused). 2442789Sahrens */ 24433403Sbmc if (zio->io_error == 0 && hdr->b_state == arc_anon) 24442688Smaybee arc_access(hdr, hash_lock); 24452688Smaybee mutex_exit(hash_lock); 2446789Sahrens } else { 2447789Sahrens /* 2448789Sahrens * This block was freed while we waited for the read to 2449789Sahrens * complete. It has been removed from the hash table and 2450789Sahrens * moved to the anonymous state (so that it won't show up 2451789Sahrens * in the cache). 2452789Sahrens */ 24533403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2454789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2455789Sahrens } 2456789Sahrens 2457789Sahrens /* execute each callback and free its structure */ 2458789Sahrens while ((acb = callback_list) != NULL) { 2459789Sahrens if (acb->acb_done) 2460789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2461789Sahrens 2462789Sahrens if (acb->acb_zio_dummy != NULL) { 2463789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2464789Sahrens zio_nowait(acb->acb_zio_dummy); 2465789Sahrens } 2466789Sahrens 2467789Sahrens callback_list = acb->acb_next; 2468789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2469789Sahrens } 2470789Sahrens 2471789Sahrens if (freeable) 24721544Seschrock arc_hdr_destroy(hdr); 2473789Sahrens } 2474789Sahrens 2475789Sahrens /* 2476789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2477789Sahrens * cache. If the block is found in the cache, invoke the provided 2478789Sahrens * callback immediately and return. Note that the `zio' parameter 2479789Sahrens * in the callback will be NULL in this case, since no IO was 2480789Sahrens * required. If the block is not in the cache pass the read request 2481789Sahrens * on to the spa with a substitute callback function, so that the 2482789Sahrens * requested block will be added to the cache. 2483789Sahrens * 2484789Sahrens * If a read request arrives for a block that has a read in-progress, 2485789Sahrens * either wait for the in-progress read to complete (and return the 2486789Sahrens * results); or, if this is a read with a "done" func, add a record 2487789Sahrens * to the read to invoke the "done" func when the read completes, 2488789Sahrens * and return; or just return. 2489789Sahrens * 2490789Sahrens * arc_read_done() will invoke all the requested "done" functions 2491789Sahrens * for readers of this block. 24927046Sahrens * 24937046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 24947046Sahrens * for the bp. But if you know you don't need locking, you can use 24958213SSuhasini.Peddada@Sun.COM * arc_read_bp. 2496789Sahrens */ 2497789Sahrens int 24987046Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_buf_t *pbuf, 24997237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 25007046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 25017046Sahrens { 25027046Sahrens int err; 25037265Sahrens arc_buf_hdr_t *hdr = pbuf->b_hdr; 25047046Sahrens 25057046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 25067046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 25077545SMark.Maybee@Sun.COM rw_enter(&pbuf->b_lock, RW_READER); 25087046Sahrens 25097046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 25107237Sek110237 zio_flags, arc_flags, zb); 25117046Sahrens 25127265Sahrens ASSERT3P(hdr, ==, pbuf->b_hdr); 25137545SMark.Maybee@Sun.COM rw_exit(&pbuf->b_lock); 25147046Sahrens return (err); 25157046Sahrens } 25167046Sahrens 25177046Sahrens int 25187046Sahrens arc_read_nolock(zio_t *pio, spa_t *spa, blkptr_t *bp, 25197237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 25207046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2521789Sahrens { 2522789Sahrens arc_buf_hdr_t *hdr; 2523789Sahrens arc_buf_t *buf; 2524789Sahrens kmutex_t *hash_lock; 25255450Sbrendan zio_t *rzio; 2526789Sahrens 2527789Sahrens top: 2528789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 25291544Seschrock if (hdr && hdr->b_datacnt > 0) { 2530789Sahrens 25312391Smaybee *arc_flags |= ARC_CACHED; 25322391Smaybee 2533789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 25342391Smaybee 25352391Smaybee if (*arc_flags & ARC_WAIT) { 25362391Smaybee cv_wait(&hdr->b_cv, hash_lock); 25372391Smaybee mutex_exit(hash_lock); 25382391Smaybee goto top; 25392391Smaybee } 25402391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 25412391Smaybee 25422391Smaybee if (done) { 2543789Sahrens arc_callback_t *acb = NULL; 2544789Sahrens 2545789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2546789Sahrens KM_SLEEP); 2547789Sahrens acb->acb_done = done; 2548789Sahrens acb->acb_private = private; 2549789Sahrens if (pio != NULL) 2550789Sahrens acb->acb_zio_dummy = zio_null(pio, 2551*8632SBill.Moore@Sun.COM spa, NULL, NULL, NULL, zio_flags); 2552789Sahrens 2553789Sahrens ASSERT(acb->acb_done != NULL); 2554789Sahrens acb->acb_next = hdr->b_acb; 2555789Sahrens hdr->b_acb = acb; 2556789Sahrens add_reference(hdr, hash_lock, private); 2557789Sahrens mutex_exit(hash_lock); 2558789Sahrens return (0); 2559789Sahrens } 2560789Sahrens mutex_exit(hash_lock); 2561789Sahrens return (0); 2562789Sahrens } 2563789Sahrens 25643403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2565789Sahrens 25661544Seschrock if (done) { 25672688Smaybee add_reference(hdr, hash_lock, private); 25681544Seschrock /* 25691544Seschrock * If this block is already in use, create a new 25701544Seschrock * copy of the data so that we will be guaranteed 25711544Seschrock * that arc_release() will always succeed. 25721544Seschrock */ 25731544Seschrock buf = hdr->b_buf; 25741544Seschrock ASSERT(buf); 25751544Seschrock ASSERT(buf->b_data); 25762688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 25771544Seschrock ASSERT(buf->b_efunc == NULL); 25781544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 25792688Smaybee } else { 25802688Smaybee buf = arc_buf_clone(buf); 25811544Seschrock } 25822391Smaybee } else if (*arc_flags & ARC_PREFETCH && 25832391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 25842391Smaybee hdr->b_flags |= ARC_PREFETCH; 2585789Sahrens } 2586789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 25872688Smaybee arc_access(hdr, hash_lock); 25887237Sek110237 if (*arc_flags & ARC_L2CACHE) 25897237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25902688Smaybee mutex_exit(hash_lock); 25913403Sbmc ARCSTAT_BUMP(arcstat_hits); 25923403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 25933403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 25943403Sbmc data, metadata, hits); 25953403Sbmc 2596789Sahrens if (done) 2597789Sahrens done(NULL, buf, private); 2598789Sahrens } else { 2599789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2600789Sahrens arc_callback_t *acb; 26016987Sbrendan vdev_t *vd = NULL; 26026987Sbrendan daddr_t addr; 26038582SBrendan.Gregg@Sun.COM boolean_t devw = B_FALSE; 2604789Sahrens 2605789Sahrens if (hdr == NULL) { 2606789Sahrens /* this block is not in the cache */ 2607789Sahrens arc_buf_hdr_t *exists; 26083290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 26093290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2610789Sahrens hdr = buf->b_hdr; 2611789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 2612789Sahrens hdr->b_birth = bp->blk_birth; 2613789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2614789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2615789Sahrens if (exists) { 2616789Sahrens /* somebody beat us to the hash insert */ 2617789Sahrens mutex_exit(hash_lock); 2618789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2619789Sahrens hdr->b_birth = 0; 2620789Sahrens hdr->b_cksum0 = 0; 26211544Seschrock (void) arc_buf_remove_ref(buf, private); 2622789Sahrens goto top; /* restart the IO request */ 2623789Sahrens } 26242391Smaybee /* if this is a prefetch, we don't have a reference */ 26252391Smaybee if (*arc_flags & ARC_PREFETCH) { 26262391Smaybee (void) remove_reference(hdr, hash_lock, 26272391Smaybee private); 26282391Smaybee hdr->b_flags |= ARC_PREFETCH; 26292391Smaybee } 26307237Sek110237 if (*arc_flags & ARC_L2CACHE) 26317237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26322391Smaybee if (BP_GET_LEVEL(bp) > 0) 26332391Smaybee hdr->b_flags |= ARC_INDIRECT; 2634789Sahrens } else { 2635789Sahrens /* this block is in the ghost cache */ 26361544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 26371544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 26382391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 26392391Smaybee ASSERT(hdr->b_buf == NULL); 2640789Sahrens 26412391Smaybee /* if this is a prefetch, we don't have a reference */ 26422391Smaybee if (*arc_flags & ARC_PREFETCH) 26432391Smaybee hdr->b_flags |= ARC_PREFETCH; 26442391Smaybee else 26452391Smaybee add_reference(hdr, hash_lock, private); 26467237Sek110237 if (*arc_flags & ARC_L2CACHE) 26477237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26486245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 26491544Seschrock buf->b_hdr = hdr; 26502688Smaybee buf->b_data = NULL; 26511544Seschrock buf->b_efunc = NULL; 26521544Seschrock buf->b_private = NULL; 26531544Seschrock buf->b_next = NULL; 26541544Seschrock hdr->b_buf = buf; 26552688Smaybee arc_get_data_buf(buf); 26561544Seschrock ASSERT(hdr->b_datacnt == 0); 26571544Seschrock hdr->b_datacnt = 1; 26582391Smaybee 2659789Sahrens } 2660789Sahrens 2661789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2662789Sahrens acb->acb_done = done; 2663789Sahrens acb->acb_private = private; 2664789Sahrens 2665789Sahrens ASSERT(hdr->b_acb == NULL); 2666789Sahrens hdr->b_acb = acb; 2667789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2668789Sahrens 2669789Sahrens /* 2670789Sahrens * If the buffer has been evicted, migrate it to a present state 2671789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2672789Sahrens * the header will be marked as I/O in progress and have an 2673789Sahrens * attached buffer. At this point, anybody who finds this 2674789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2675789Sahrens */ 2676789Sahrens 26771544Seschrock if (GHOST_STATE(hdr->b_state)) 26782688Smaybee arc_access(hdr, hash_lock); 2679789Sahrens 26807754SJeff.Bonwick@Sun.COM if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 26817754SJeff.Bonwick@Sun.COM (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 26828582SBrendan.Gregg@Sun.COM devw = hdr->b_l2hdr->b_dev->l2ad_writing; 26836987Sbrendan addr = hdr->b_l2hdr->b_daddr; 26847754SJeff.Bonwick@Sun.COM /* 26857754SJeff.Bonwick@Sun.COM * Lock out device removal. 26867754SJeff.Bonwick@Sun.COM */ 26877754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || 26887754SJeff.Bonwick@Sun.COM !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 26897754SJeff.Bonwick@Sun.COM vd = NULL; 26906987Sbrendan } 26916987Sbrendan 26926987Sbrendan mutex_exit(hash_lock); 26936987Sbrendan 2694789Sahrens ASSERT3U(hdr->b_size, ==, size); 26951596Sahrens DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size, 26961596Sahrens zbookmark_t *, zb); 26973403Sbmc ARCSTAT_BUMP(arcstat_misses); 26983403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 26993403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 27003403Sbmc data, metadata, misses); 27011544Seschrock 27028582SBrendan.Gregg@Sun.COM if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 27036987Sbrendan /* 27046987Sbrendan * Read from the L2ARC if the following are true: 27056987Sbrendan * 1. The L2ARC vdev was previously cached. 27066987Sbrendan * 2. This buffer still has L2ARC metadata. 27076987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 27086987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 27096987Sbrendan * also have invalidated the vdev. 27108582SBrendan.Gregg@Sun.COM * 5. This isn't prefetch and l2arc_noprefetch is set. 27116987Sbrendan */ 27127754SJeff.Bonwick@Sun.COM if (hdr->b_l2hdr != NULL && 27138582SBrendan.Gregg@Sun.COM !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 27148582SBrendan.Gregg@Sun.COM !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 27155450Sbrendan l2arc_read_callback_t *cb; 27165450Sbrendan 27176643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 27186643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 27196643Seschrock 27205450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 27215450Sbrendan KM_SLEEP); 27225450Sbrendan cb->l2rcb_buf = buf; 27235450Sbrendan cb->l2rcb_spa = spa; 27245450Sbrendan cb->l2rcb_bp = *bp; 27255450Sbrendan cb->l2rcb_zb = *zb; 27267237Sek110237 cb->l2rcb_flags = zio_flags; 27275450Sbrendan 27285450Sbrendan /* 27297754SJeff.Bonwick@Sun.COM * l2arc read. The SCL_L2ARC lock will be 27307754SJeff.Bonwick@Sun.COM * released by l2arc_read_done(). 27315450Sbrendan */ 27325450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 27335450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 27347237Sek110237 l2arc_read_done, cb, priority, zio_flags | 27357361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 27367754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_PROPAGATE | 27377754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_RETRY, B_FALSE); 27385450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 27395450Sbrendan zio_t *, rzio); 27408582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_read_bytes, size); 27416987Sbrendan 27426987Sbrendan if (*arc_flags & ARC_NOWAIT) { 27436987Sbrendan zio_nowait(rzio); 27446987Sbrendan return (0); 27456987Sbrendan } 27466987Sbrendan 27476987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 27486987Sbrendan if (zio_wait(rzio) == 0) 27496987Sbrendan return (0); 27506987Sbrendan 27516987Sbrendan /* l2arc read error; goto zio_read() */ 27525450Sbrendan } else { 27535450Sbrendan DTRACE_PROBE1(l2arc__miss, 27545450Sbrendan arc_buf_hdr_t *, hdr); 27555450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 27565450Sbrendan if (HDR_L2_WRITING(hdr)) 27575450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 27587754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 27595450Sbrendan } 27608582SBrendan.Gregg@Sun.COM } else { 27618628SBill.Moore@Sun.COM if (vd != NULL) 27628628SBill.Moore@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 27638582SBrendan.Gregg@Sun.COM if (l2arc_ndev != 0) { 27648582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(l2arc__miss, 27658582SBrendan.Gregg@Sun.COM arc_buf_hdr_t *, hdr); 27668582SBrendan.Gregg@Sun.COM ARCSTAT_BUMP(arcstat_l2_misses); 27678582SBrendan.Gregg@Sun.COM } 27685450Sbrendan } 27696643Seschrock 2770789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 27717237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2772789Sahrens 27732391Smaybee if (*arc_flags & ARC_WAIT) 2774789Sahrens return (zio_wait(rzio)); 2775789Sahrens 27762391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2777789Sahrens zio_nowait(rzio); 2778789Sahrens } 2779789Sahrens return (0); 2780789Sahrens } 2781789Sahrens 2782789Sahrens /* 2783789Sahrens * arc_read() variant to support pool traversal. If the block is already 2784789Sahrens * in the ARC, make a copy of it; otherwise, the caller will do the I/O. 2785789Sahrens * The idea is that we don't want pool traversal filling up memory, but 2786789Sahrens * if the ARC already has the data anyway, we shouldn't pay for the I/O. 2787789Sahrens */ 2788789Sahrens int 2789789Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data) 2790789Sahrens { 2791789Sahrens arc_buf_hdr_t *hdr; 2792789Sahrens kmutex_t *hash_mtx; 2793789Sahrens int rc = 0; 2794789Sahrens 2795789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx); 2796789Sahrens 27971544Seschrock if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) { 27981544Seschrock arc_buf_t *buf = hdr->b_buf; 27991544Seschrock 28001544Seschrock ASSERT(buf); 28011544Seschrock while (buf->b_data == NULL) { 28021544Seschrock buf = buf->b_next; 28031544Seschrock ASSERT(buf); 28041544Seschrock } 28051544Seschrock bcopy(buf->b_data, data, hdr->b_size); 28061544Seschrock } else { 2807789Sahrens rc = ENOENT; 28081544Seschrock } 2809789Sahrens 2810789Sahrens if (hash_mtx) 2811789Sahrens mutex_exit(hash_mtx); 2812789Sahrens 2813789Sahrens return (rc); 2814789Sahrens } 2815789Sahrens 28161544Seschrock void 28171544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 28181544Seschrock { 28191544Seschrock ASSERT(buf->b_hdr != NULL); 28203403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 28211544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 28221544Seschrock buf->b_efunc = func; 28231544Seschrock buf->b_private = private; 28241544Seschrock } 28251544Seschrock 28261544Seschrock /* 28271544Seschrock * This is used by the DMU to let the ARC know that a buffer is 28281544Seschrock * being evicted, so the ARC should clean up. If this arc buf 28291544Seschrock * is not yet in the evicted state, it will be put there. 28301544Seschrock */ 28311544Seschrock int 28321544Seschrock arc_buf_evict(arc_buf_t *buf) 28331544Seschrock { 28342887Smaybee arc_buf_hdr_t *hdr; 28351544Seschrock kmutex_t *hash_lock; 28361544Seschrock arc_buf_t **bufp; 28371544Seschrock 28387545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 28392887Smaybee hdr = buf->b_hdr; 28401544Seschrock if (hdr == NULL) { 28411544Seschrock /* 28421544Seschrock * We are in arc_do_user_evicts(). 28431544Seschrock */ 28441544Seschrock ASSERT(buf->b_data == NULL); 28457545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 28461544Seschrock return (0); 28477545SMark.Maybee@Sun.COM } else if (buf->b_data == NULL) { 28487545SMark.Maybee@Sun.COM arc_buf_t copy = *buf; /* structure assignment */ 28497545SMark.Maybee@Sun.COM /* 28507545SMark.Maybee@Sun.COM * We are on the eviction list; process this buffer now 28517545SMark.Maybee@Sun.COM * but let arc_do_user_evicts() do the reaping. 28527545SMark.Maybee@Sun.COM */ 28537545SMark.Maybee@Sun.COM buf->b_efunc = NULL; 28547545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 28557545SMark.Maybee@Sun.COM VERIFY(copy.b_efunc(©) == 0); 28567545SMark.Maybee@Sun.COM return (1); 28571544Seschrock } 28582887Smaybee hash_lock = HDR_LOCK(hdr); 28591544Seschrock mutex_enter(hash_lock); 28601544Seschrock 28612724Smaybee ASSERT(buf->b_hdr == hdr); 28622724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 28633403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 28641544Seschrock 28651544Seschrock /* 28661544Seschrock * Pull this buffer off of the hdr 28671544Seschrock */ 28681544Seschrock bufp = &hdr->b_buf; 28691544Seschrock while (*bufp != buf) 28701544Seschrock bufp = &(*bufp)->b_next; 28711544Seschrock *bufp = buf->b_next; 28721544Seschrock 28731544Seschrock ASSERT(buf->b_data != NULL); 28742688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 28751544Seschrock 28761544Seschrock if (hdr->b_datacnt == 0) { 28771544Seschrock arc_state_t *old_state = hdr->b_state; 28781544Seschrock arc_state_t *evicted_state; 28791544Seschrock 28801544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 28811544Seschrock 28821544Seschrock evicted_state = 28833403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 28841544Seschrock 28853403Sbmc mutex_enter(&old_state->arcs_mtx); 28863403Sbmc mutex_enter(&evicted_state->arcs_mtx); 28871544Seschrock 28881544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 28891544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 28905450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 28915450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 28921544Seschrock 28933403Sbmc mutex_exit(&evicted_state->arcs_mtx); 28943403Sbmc mutex_exit(&old_state->arcs_mtx); 28951544Seschrock } 28961544Seschrock mutex_exit(hash_lock); 28977545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 28981819Smaybee 28991544Seschrock VERIFY(buf->b_efunc(buf) == 0); 29001544Seschrock buf->b_efunc = NULL; 29011544Seschrock buf->b_private = NULL; 29021544Seschrock buf->b_hdr = NULL; 29031544Seschrock kmem_cache_free(buf_cache, buf); 29041544Seschrock return (1); 29051544Seschrock } 29061544Seschrock 2907789Sahrens /* 2908789Sahrens * Release this buffer from the cache. This must be done 2909789Sahrens * after a read and prior to modifying the buffer contents. 2910789Sahrens * If the buffer has more than one reference, we must make 29117046Sahrens * a new hdr for the buffer. 2912789Sahrens */ 2913789Sahrens void 2914789Sahrens arc_release(arc_buf_t *buf, void *tag) 2915789Sahrens { 29167545SMark.Maybee@Sun.COM arc_buf_hdr_t *hdr; 29177545SMark.Maybee@Sun.COM kmutex_t *hash_lock; 29187545SMark.Maybee@Sun.COM l2arc_buf_hdr_t *l2hdr; 29195450Sbrendan uint64_t buf_size; 2920789Sahrens 29217545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 29227545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 29237545SMark.Maybee@Sun.COM 2924789Sahrens /* this buffer is not on any list */ 2925789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 29267046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 2927789Sahrens 29283403Sbmc if (hdr->b_state == arc_anon) { 2929789Sahrens /* this buffer is already released */ 2930789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2931789Sahrens ASSERT(BUF_EMPTY(hdr)); 29321544Seschrock ASSERT(buf->b_efunc == NULL); 29333093Sahrens arc_buf_thaw(buf); 29347545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 2935789Sahrens return; 2936789Sahrens } 2937789Sahrens 29387545SMark.Maybee@Sun.COM hash_lock = HDR_LOCK(hdr); 2939789Sahrens mutex_enter(hash_lock); 2940789Sahrens 29417545SMark.Maybee@Sun.COM l2hdr = hdr->b_l2hdr; 29427545SMark.Maybee@Sun.COM if (l2hdr) { 29437545SMark.Maybee@Sun.COM mutex_enter(&l2arc_buflist_mtx); 29447545SMark.Maybee@Sun.COM hdr->b_l2hdr = NULL; 29457545SMark.Maybee@Sun.COM buf_size = hdr->b_size; 29467545SMark.Maybee@Sun.COM } 29477545SMark.Maybee@Sun.COM 29481544Seschrock /* 29491544Seschrock * Do we have more than one buf? 29501544Seschrock */ 29517545SMark.Maybee@Sun.COM if (hdr->b_datacnt > 1) { 2952789Sahrens arc_buf_hdr_t *nhdr; 2953789Sahrens arc_buf_t **bufp; 2954789Sahrens uint64_t blksz = hdr->b_size; 2955789Sahrens spa_t *spa = hdr->b_spa; 29563290Sjohansen arc_buf_contents_t type = hdr->b_type; 29575450Sbrendan uint32_t flags = hdr->b_flags; 2958789Sahrens 29597545SMark.Maybee@Sun.COM ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 2960789Sahrens /* 2961789Sahrens * Pull the data off of this buf and attach it to 2962789Sahrens * a new anonymous buf. 2963789Sahrens */ 29641544Seschrock (void) remove_reference(hdr, hash_lock, tag); 2965789Sahrens bufp = &hdr->b_buf; 29661544Seschrock while (*bufp != buf) 2967789Sahrens bufp = &(*bufp)->b_next; 2968789Sahrens *bufp = (*bufp)->b_next; 29693897Smaybee buf->b_next = NULL; 29701544Seschrock 29713403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 29723403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 29731544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 29744309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 29754309Smaybee ASSERT3U(*size, >=, hdr->b_size); 29764309Smaybee atomic_add_64(size, -hdr->b_size); 29771544Seschrock } 29781544Seschrock hdr->b_datacnt -= 1; 29793547Smaybee arc_cksum_verify(buf); 29801544Seschrock 2981789Sahrens mutex_exit(hash_lock); 2982789Sahrens 29836245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 2984789Sahrens nhdr->b_size = blksz; 2985789Sahrens nhdr->b_spa = spa; 29863290Sjohansen nhdr->b_type = type; 2987789Sahrens nhdr->b_buf = buf; 29883403Sbmc nhdr->b_state = arc_anon; 2989789Sahrens nhdr->b_arc_access = 0; 29905450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 29915450Sbrendan nhdr->b_l2hdr = NULL; 29921544Seschrock nhdr->b_datacnt = 1; 29933547Smaybee nhdr->b_freeze_cksum = NULL; 29943897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 2995789Sahrens buf->b_hdr = nhdr; 29967545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29973403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 2998789Sahrens } else { 29997545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30001544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3001789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 3002789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 30033403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 3004789Sahrens hdr->b_arc_access = 0; 3005789Sahrens mutex_exit(hash_lock); 30065450Sbrendan 3007789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 3008789Sahrens hdr->b_birth = 0; 3009789Sahrens hdr->b_cksum0 = 0; 30103547Smaybee arc_buf_thaw(buf); 3011789Sahrens } 30121544Seschrock buf->b_efunc = NULL; 30131544Seschrock buf->b_private = NULL; 30145450Sbrendan 30155450Sbrendan if (l2hdr) { 30165450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 30175450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 30185450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 30197545SMark.Maybee@Sun.COM mutex_exit(&l2arc_buflist_mtx); 30205450Sbrendan } 3021789Sahrens } 3022789Sahrens 3023789Sahrens int 3024789Sahrens arc_released(arc_buf_t *buf) 3025789Sahrens { 30267545SMark.Maybee@Sun.COM int released; 30277545SMark.Maybee@Sun.COM 30287545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30297545SMark.Maybee@Sun.COM released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 30307545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30317545SMark.Maybee@Sun.COM return (released); 30321544Seschrock } 30331544Seschrock 30341544Seschrock int 30351544Seschrock arc_has_callback(arc_buf_t *buf) 30361544Seschrock { 30377545SMark.Maybee@Sun.COM int callback; 30387545SMark.Maybee@Sun.COM 30397545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30407545SMark.Maybee@Sun.COM callback = (buf->b_efunc != NULL); 30417545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30427545SMark.Maybee@Sun.COM return (callback); 3043789Sahrens } 3044789Sahrens 30451544Seschrock #ifdef ZFS_DEBUG 30461544Seschrock int 30471544Seschrock arc_referenced(arc_buf_t *buf) 30481544Seschrock { 30497545SMark.Maybee@Sun.COM int referenced; 30507545SMark.Maybee@Sun.COM 30517545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30527545SMark.Maybee@Sun.COM referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 30537545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30547545SMark.Maybee@Sun.COM return (referenced); 30551544Seschrock } 30561544Seschrock #endif 30571544Seschrock 3058789Sahrens static void 30593547Smaybee arc_write_ready(zio_t *zio) 30603547Smaybee { 30613547Smaybee arc_write_callback_t *callback = zio->io_private; 30623547Smaybee arc_buf_t *buf = callback->awcb_buf; 30635329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 30645329Sgw25295 30657754SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 30667754SJeff.Bonwick@Sun.COM callback->awcb_ready(zio, buf, callback->awcb_private); 30677754SJeff.Bonwick@Sun.COM 30685329Sgw25295 /* 30695329Sgw25295 * If the IO is already in progress, then this is a re-write 30707754SJeff.Bonwick@Sun.COM * attempt, so we need to thaw and re-compute the cksum. 30717754SJeff.Bonwick@Sun.COM * It is the responsibility of the callback to handle the 30727754SJeff.Bonwick@Sun.COM * accounting for any re-write attempt. 30735329Sgw25295 */ 30745329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 30755329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 30765329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 30775329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 30785329Sgw25295 hdr->b_freeze_cksum = NULL; 30795329Sgw25295 } 30805329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 30815329Sgw25295 } 30825450Sbrendan arc_cksum_compute(buf, B_FALSE); 30835329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 30843547Smaybee } 30853547Smaybee 30863547Smaybee static void 3087789Sahrens arc_write_done(zio_t *zio) 3088789Sahrens { 30893547Smaybee arc_write_callback_t *callback = zio->io_private; 30903547Smaybee arc_buf_t *buf = callback->awcb_buf; 30913547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3092789Sahrens 3093789Sahrens hdr->b_acb = NULL; 3094789Sahrens 3095789Sahrens hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3096789Sahrens hdr->b_birth = zio->io_bp->blk_birth; 3097789Sahrens hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 30981544Seschrock /* 30991544Seschrock * If the block to be written was all-zero, we may have 31001544Seschrock * compressed it away. In this case no write was performed 31011544Seschrock * so there will be no dva/birth-date/checksum. The buffer 31021544Seschrock * must therefor remain anonymous (and uncached). 31031544Seschrock */ 3104789Sahrens if (!BUF_EMPTY(hdr)) { 3105789Sahrens arc_buf_hdr_t *exists; 3106789Sahrens kmutex_t *hash_lock; 3107789Sahrens 31083093Sahrens arc_cksum_verify(buf); 31093093Sahrens 3110789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3111789Sahrens if (exists) { 3112789Sahrens /* 3113789Sahrens * This can only happen if we overwrite for 3114789Sahrens * sync-to-convergence, because we remove 3115789Sahrens * buffers from the hash table when we arc_free(). 3116789Sahrens */ 31177754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_IO_REWRITE); 3118789Sahrens ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig), 3119789Sahrens BP_IDENTITY(zio->io_bp))); 3120789Sahrens ASSERT3U(zio->io_bp_orig.blk_birth, ==, 3121789Sahrens zio->io_bp->blk_birth); 3122789Sahrens 3123789Sahrens ASSERT(refcount_is_zero(&exists->b_refcnt)); 31243403Sbmc arc_change_state(arc_anon, exists, hash_lock); 3125789Sahrens mutex_exit(hash_lock); 31261544Seschrock arc_hdr_destroy(exists); 3127789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3128789Sahrens ASSERT3P(exists, ==, NULL); 3129789Sahrens } 31301544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 31317046Sahrens /* if it's not anon, we are doing a scrub */ 31327046Sahrens if (hdr->b_state == arc_anon) 31337046Sahrens arc_access(hdr, hash_lock); 31342688Smaybee mutex_exit(hash_lock); 31353547Smaybee } else if (callback->awcb_done == NULL) { 31361544Seschrock int destroy_hdr; 31371544Seschrock /* 31381544Seschrock * This is an anonymous buffer with no user callback, 31391544Seschrock * destroy it if there are no active references. 31401544Seschrock */ 31411544Seschrock mutex_enter(&arc_eviction_mtx); 31421544Seschrock destroy_hdr = refcount_is_zero(&hdr->b_refcnt); 31431544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 31441544Seschrock mutex_exit(&arc_eviction_mtx); 31451544Seschrock if (destroy_hdr) 31461544Seschrock arc_hdr_destroy(hdr); 31471544Seschrock } else { 31481544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3149789Sahrens } 31507046Sahrens hdr->b_flags &= ~ARC_STORED; 31511544Seschrock 31523547Smaybee if (callback->awcb_done) { 3153789Sahrens ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 31543547Smaybee callback->awcb_done(zio, buf, callback->awcb_private); 3155789Sahrens } 3156789Sahrens 31573547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3158789Sahrens } 3159789Sahrens 31607872STim.Haley@Sun.COM void 31617754SJeff.Bonwick@Sun.COM write_policy(spa_t *spa, const writeprops_t *wp, zio_prop_t *zp) 31627046Sahrens { 31637046Sahrens boolean_t ismd = (wp->wp_level > 0 || dmu_ot[wp->wp_type].ot_metadata); 31647046Sahrens 31657046Sahrens /* Determine checksum setting */ 31667046Sahrens if (ismd) { 31677046Sahrens /* 31687046Sahrens * Metadata always gets checksummed. If the data 31697046Sahrens * checksum is multi-bit correctable, and it's not a 31707046Sahrens * ZBT-style checksum, then it's suitable for metadata 31717046Sahrens * as well. Otherwise, the metadata checksum defaults 31727046Sahrens * to fletcher4. 31737046Sahrens */ 31747046Sahrens if (zio_checksum_table[wp->wp_oschecksum].ci_correctable && 31757046Sahrens !zio_checksum_table[wp->wp_oschecksum].ci_zbt) 31767754SJeff.Bonwick@Sun.COM zp->zp_checksum = wp->wp_oschecksum; 31777046Sahrens else 31787754SJeff.Bonwick@Sun.COM zp->zp_checksum = ZIO_CHECKSUM_FLETCHER_4; 31797046Sahrens } else { 31807754SJeff.Bonwick@Sun.COM zp->zp_checksum = zio_checksum_select(wp->wp_dnchecksum, 31817046Sahrens wp->wp_oschecksum); 31827046Sahrens } 31837046Sahrens 31847046Sahrens /* Determine compression setting */ 31857046Sahrens if (ismd) { 31867046Sahrens /* 31877046Sahrens * XXX -- we should design a compression algorithm 31887046Sahrens * that specializes in arrays of bps. 31897046Sahrens */ 31907754SJeff.Bonwick@Sun.COM zp->zp_compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY : 31917046Sahrens ZIO_COMPRESS_LZJB; 31927046Sahrens } else { 31937754SJeff.Bonwick@Sun.COM zp->zp_compress = zio_compress_select(wp->wp_dncompress, 31947046Sahrens wp->wp_oscompress); 31957046Sahrens } 31967754SJeff.Bonwick@Sun.COM 31977754SJeff.Bonwick@Sun.COM zp->zp_type = wp->wp_type; 31987754SJeff.Bonwick@Sun.COM zp->zp_level = wp->wp_level; 31997754SJeff.Bonwick@Sun.COM zp->zp_ndvas = MIN(wp->wp_copies + ismd, spa_max_replication(spa)); 32007046Sahrens } 32017046Sahrens 32023547Smaybee zio_t * 32037046Sahrens arc_write(zio_t *pio, spa_t *spa, const writeprops_t *wp, 32047237Sek110237 boolean_t l2arc, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 32053547Smaybee arc_done_func_t *ready, arc_done_func_t *done, void *private, int priority, 32067237Sek110237 int zio_flags, const zbookmark_t *zb) 3207789Sahrens { 3208789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 32093547Smaybee arc_write_callback_t *callback; 32107754SJeff.Bonwick@Sun.COM zio_t *zio; 32117754SJeff.Bonwick@Sun.COM zio_prop_t zp; 32127754SJeff.Bonwick@Sun.COM 32137754SJeff.Bonwick@Sun.COM ASSERT(ready != NULL); 3214789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 32152237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 32162237Smaybee ASSERT(hdr->b_acb == 0); 32177237Sek110237 if (l2arc) 32187237Sek110237 hdr->b_flags |= ARC_L2CACHE; 32193547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 32203547Smaybee callback->awcb_ready = ready; 32213547Smaybee callback->awcb_done = done; 32223547Smaybee callback->awcb_private = private; 32233547Smaybee callback->awcb_buf = buf; 32247046Sahrens 32257754SJeff.Bonwick@Sun.COM write_policy(spa, wp, &zp); 32267754SJeff.Bonwick@Sun.COM zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, &zp, 32277754SJeff.Bonwick@Sun.COM arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3228789Sahrens 32293547Smaybee return (zio); 3230789Sahrens } 3231789Sahrens 3232789Sahrens int 3233789Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 3234789Sahrens zio_done_func_t *done, void *private, uint32_t arc_flags) 3235789Sahrens { 3236789Sahrens arc_buf_hdr_t *ab; 3237789Sahrens kmutex_t *hash_lock; 3238789Sahrens zio_t *zio; 3239789Sahrens 3240789Sahrens /* 3241789Sahrens * If this buffer is in the cache, release it, so it 3242789Sahrens * can be re-used. 3243789Sahrens */ 3244789Sahrens ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 3245789Sahrens if (ab != NULL) { 3246789Sahrens /* 3247789Sahrens * The checksum of blocks to free is not always 3248789Sahrens * preserved (eg. on the deadlist). However, if it is 3249789Sahrens * nonzero, it should match what we have in the cache. 3250789Sahrens */ 3251789Sahrens ASSERT(bp->blk_cksum.zc_word[0] == 0 || 32527754SJeff.Bonwick@Sun.COM bp->blk_cksum.zc_word[0] == ab->b_cksum0 || 32537754SJeff.Bonwick@Sun.COM bp->blk_fill == BLK_FILL_ALREADY_FREED); 32547754SJeff.Bonwick@Sun.COM 32553403Sbmc if (ab->b_state != arc_anon) 32563403Sbmc arc_change_state(arc_anon, ab, hash_lock); 32572391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 32582391Smaybee /* 32592391Smaybee * This should only happen when we prefetch. 32602391Smaybee */ 32612391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 32622391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 32632391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 32642391Smaybee if (HDR_IN_HASH_TABLE(ab)) 32652391Smaybee buf_hash_remove(ab); 32662391Smaybee ab->b_arc_access = 0; 32672391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 32682391Smaybee ab->b_birth = 0; 32692391Smaybee ab->b_cksum0 = 0; 32702391Smaybee ab->b_buf->b_efunc = NULL; 32712391Smaybee ab->b_buf->b_private = NULL; 32722391Smaybee mutex_exit(hash_lock); 32732391Smaybee } else if (refcount_is_zero(&ab->b_refcnt)) { 32745450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3275789Sahrens mutex_exit(hash_lock); 32761544Seschrock arc_hdr_destroy(ab); 32773403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3278789Sahrens } else { 32791589Smaybee /* 32802391Smaybee * We still have an active reference on this 32812391Smaybee * buffer. This can happen, e.g., from 32822391Smaybee * dbuf_unoverride(). 32831589Smaybee */ 32842391Smaybee ASSERT(!HDR_IN_HASH_TABLE(ab)); 3285789Sahrens ab->b_arc_access = 0; 3286789Sahrens bzero(&ab->b_dva, sizeof (dva_t)); 3287789Sahrens ab->b_birth = 0; 3288789Sahrens ab->b_cksum0 = 0; 32891544Seschrock ab->b_buf->b_efunc = NULL; 32901544Seschrock ab->b_buf->b_private = NULL; 3291789Sahrens mutex_exit(hash_lock); 3292789Sahrens } 3293789Sahrens } 3294789Sahrens 32957754SJeff.Bonwick@Sun.COM zio = zio_free(pio, spa, txg, bp, done, private, ZIO_FLAG_MUSTSUCCEED); 3296789Sahrens 3297789Sahrens if (arc_flags & ARC_WAIT) 3298789Sahrens return (zio_wait(zio)); 3299789Sahrens 3300789Sahrens ASSERT(arc_flags & ARC_NOWAIT); 3301789Sahrens zio_nowait(zio); 3302789Sahrens 3303789Sahrens return (0); 3304789Sahrens } 3305789Sahrens 33066245Smaybee static int 33076245Smaybee arc_memory_throttle(uint64_t reserve, uint64_t txg) 33086245Smaybee { 33096245Smaybee #ifdef _KERNEL 33106245Smaybee uint64_t inflight_data = arc_anon->arcs_size; 33116245Smaybee uint64_t available_memory = ptob(freemem); 33126245Smaybee static uint64_t page_load = 0; 33136245Smaybee static uint64_t last_txg = 0; 33146245Smaybee 33156245Smaybee #if defined(__i386) 33166245Smaybee available_memory = 33176245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 33186245Smaybee #endif 33196245Smaybee if (available_memory >= zfs_write_limit_max) 33206245Smaybee return (0); 33216245Smaybee 33226245Smaybee if (txg > last_txg) { 33236245Smaybee last_txg = txg; 33246245Smaybee page_load = 0; 33256245Smaybee } 33266245Smaybee /* 33276245Smaybee * If we are in pageout, we know that memory is already tight, 33286245Smaybee * the arc is already going to be evicting, so we just want to 33296245Smaybee * continue to let page writes occur as quickly as possible. 33306245Smaybee */ 33316245Smaybee if (curproc == proc_pageout) { 33326245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 33336245Smaybee return (ERESTART); 33346245Smaybee /* Note: reserve is inflated, so we deflate */ 33356245Smaybee page_load += reserve / 8; 33366245Smaybee return (0); 33376245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 33386245Smaybee /* memory is low, delay before restarting */ 33396245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33406245Smaybee return (EAGAIN); 33416245Smaybee } 33426245Smaybee page_load = 0; 33436245Smaybee 33446245Smaybee if (arc_size > arc_c_min) { 33456245Smaybee uint64_t evictable_memory = 33466245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 33476245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 33486245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 33496245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 33506245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 33516245Smaybee } 33526245Smaybee 33536245Smaybee if (inflight_data > available_memory / 4) { 33546245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33556245Smaybee return (ERESTART); 33566245Smaybee } 33576245Smaybee #endif 33586245Smaybee return (0); 33596245Smaybee } 33606245Smaybee 3361789Sahrens void 33626245Smaybee arc_tempreserve_clear(uint64_t reserve) 3363789Sahrens { 33646245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3365789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3366789Sahrens } 3367789Sahrens 3368789Sahrens int 33696245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3370789Sahrens { 33716245Smaybee int error; 33726245Smaybee 3373789Sahrens #ifdef ZFS_DEBUG 3374789Sahrens /* 3375789Sahrens * Once in a while, fail for no reason. Everything should cope. 3376789Sahrens */ 3377789Sahrens if (spa_get_random(10000) == 0) { 3378789Sahrens dprintf("forcing random failure\n"); 3379789Sahrens return (ERESTART); 3380789Sahrens } 3381789Sahrens #endif 33826245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 33836245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 33846245Smaybee if (reserve > arc_c) 3385982Smaybee return (ENOMEM); 3386982Smaybee 3387789Sahrens /* 33886245Smaybee * Writes will, almost always, require additional memory allocations 33896245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 33906245Smaybee * make sure that there is sufficient available memory for this. 33916245Smaybee */ 33926245Smaybee if (error = arc_memory_throttle(reserve, txg)) 33936245Smaybee return (error); 33946245Smaybee 33956245Smaybee /* 3396982Smaybee * Throttle writes when the amount of dirty data in the cache 3397982Smaybee * gets too large. We try to keep the cache less than half full 3398982Smaybee * of dirty blocks so that our sync times don't grow too large. 3399982Smaybee * Note: if two requests come in concurrently, we might let them 3400982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3401789Sahrens */ 34026245Smaybee if (reserve + arc_tempreserve + arc_anon->arcs_size > arc_c / 2 && 34036245Smaybee arc_anon->arcs_size > arc_c / 4) { 34044309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 34054309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 34064309Smaybee arc_tempreserve>>10, 34074309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 34084309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 34096245Smaybee reserve>>10, arc_c>>10); 3410789Sahrens return (ERESTART); 3411789Sahrens } 34126245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3413789Sahrens return (0); 3414789Sahrens } 3415789Sahrens 3416789Sahrens void 3417789Sahrens arc_init(void) 3418789Sahrens { 3419789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3420789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3421789Sahrens 34222391Smaybee /* Convert seconds to clock ticks */ 34232638Sperrin arc_min_prefetch_lifespan = 1 * hz; 34242391Smaybee 3425789Sahrens /* Start out with 1/8 of all memory */ 34263403Sbmc arc_c = physmem * PAGESIZE / 8; 3427789Sahrens 3428789Sahrens #ifdef _KERNEL 3429789Sahrens /* 3430789Sahrens * On architectures where the physical memory can be larger 3431789Sahrens * than the addressable space (intel in 32-bit mode), we may 3432789Sahrens * need to limit the cache to 1/8 of VM size. 3433789Sahrens */ 34343403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3435789Sahrens #endif 3436789Sahrens 3437982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 34383403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3439982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 34403403Sbmc if (arc_c * 8 >= 1<<30) 34413403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3442789Sahrens else 34433403Sbmc arc_c_max = arc_c_min; 34443403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 34452885Sahrens 34462885Sahrens /* 34472885Sahrens * Allow the tunables to override our calculations if they are 34482885Sahrens * reasonable (ie. over 64MB) 34492885Sahrens */ 34502885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 34513403Sbmc arc_c_max = zfs_arc_max; 34523403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 34533403Sbmc arc_c_min = zfs_arc_min; 34542885Sahrens 34553403Sbmc arc_c = arc_c_max; 34563403Sbmc arc_p = (arc_c >> 1); 3457789Sahrens 34584309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 34594309Smaybee arc_meta_limit = arc_c_max / 4; 34604645Sek110237 34614645Sek110237 /* Allow the tunable to override if it is reasonable */ 34624645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 34634645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 34644645Sek110237 34654309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 34664309Smaybee arc_c_min = arc_meta_limit / 2; 34674309Smaybee 34688582SBrendan.Gregg@Sun.COM if (zfs_arc_grow_retry > 0) 34698582SBrendan.Gregg@Sun.COM arc_grow_retry = zfs_arc_grow_retry; 34708582SBrendan.Gregg@Sun.COM 34718582SBrendan.Gregg@Sun.COM if (zfs_arc_shrink_shift > 0) 34728582SBrendan.Gregg@Sun.COM arc_shrink_shift = zfs_arc_shrink_shift; 34738582SBrendan.Gregg@Sun.COM 34748582SBrendan.Gregg@Sun.COM if (zfs_arc_p_min_shift > 0) 34758582SBrendan.Gregg@Sun.COM arc_p_min_shift = zfs_arc_p_min_shift; 34768582SBrendan.Gregg@Sun.COM 3477789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3478789Sahrens if (kmem_debugging()) 34793403Sbmc arc_c = arc_c / 2; 34803403Sbmc if (arc_c < arc_c_min) 34813403Sbmc arc_c = arc_c_min; 3482789Sahrens 34833403Sbmc arc_anon = &ARC_anon; 34843403Sbmc arc_mru = &ARC_mru; 34853403Sbmc arc_mru_ghost = &ARC_mru_ghost; 34863403Sbmc arc_mfu = &ARC_mfu; 34873403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 34885450Sbrendan arc_l2c_only = &ARC_l2c_only; 34893403Sbmc arc_size = 0; 3490789Sahrens 34913403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34923403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34933403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34943403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34953403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34965450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34972688Smaybee 34984309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 34994309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35004309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 35014309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35024309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 35034309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35044309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 35054309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35064309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 35074309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35084309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 35094309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35104309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 35114309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35124309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 35134309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35145450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 35155450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35165450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 35175450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3518789Sahrens 3519789Sahrens buf_init(); 3520789Sahrens 3521789Sahrens arc_thread_exit = 0; 35221544Seschrock arc_eviction_list = NULL; 35231544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 35242887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3525789Sahrens 35263403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 35273403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 35283403Sbmc 35293403Sbmc if (arc_ksp != NULL) { 35303403Sbmc arc_ksp->ks_data = &arc_stats; 35313403Sbmc kstat_install(arc_ksp); 35323403Sbmc } 35333403Sbmc 3534789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3535789Sahrens TS_RUN, minclsyspri); 35363158Smaybee 35373158Smaybee arc_dead = FALSE; 35386987Sbrendan arc_warm = B_FALSE; 35396245Smaybee 35406245Smaybee if (zfs_write_limit_max == 0) 35417468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 35426245Smaybee else 35436245Smaybee zfs_write_limit_shift = 0; 35447468SMark.Maybee@Sun.COM mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3545789Sahrens } 3546789Sahrens 3547789Sahrens void 3548789Sahrens arc_fini(void) 3549789Sahrens { 3550789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3551789Sahrens arc_thread_exit = 1; 3552789Sahrens while (arc_thread_exit != 0) 3553789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3554789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3555789Sahrens 35565642Smaybee arc_flush(NULL); 3557789Sahrens 3558789Sahrens arc_dead = TRUE; 3559789Sahrens 35603403Sbmc if (arc_ksp != NULL) { 35613403Sbmc kstat_delete(arc_ksp); 35623403Sbmc arc_ksp = NULL; 35633403Sbmc } 35643403Sbmc 35651544Seschrock mutex_destroy(&arc_eviction_mtx); 3566789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3567789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3568789Sahrens 35694309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 35704309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 35714309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 35724309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 35734309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 35744309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 35754309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 35764309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3577789Sahrens 35783403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 35793403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 35803403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 35813403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 35823403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 35838214SRicardo.M.Correia@Sun.COM mutex_destroy(&arc_l2c_only->arcs_mtx); 35842856Snd150628 35857468SMark.Maybee@Sun.COM mutex_destroy(&zfs_write_limit_lock); 35867468SMark.Maybee@Sun.COM 3587789Sahrens buf_fini(); 3588789Sahrens } 35895450Sbrendan 35905450Sbrendan /* 35915450Sbrendan * Level 2 ARC 35925450Sbrendan * 35935450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 35945450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 35955450Sbrendan * using large infrequent writes. The main role of this cache is to boost 35965450Sbrendan * the performance of random read workloads. The intended L2ARC devices 35975450Sbrendan * include short-stroked disks, solid state disks, and other media with 35985450Sbrendan * substantially faster read latency than disk. 35995450Sbrendan * 36005450Sbrendan * +-----------------------+ 36015450Sbrendan * | ARC | 36025450Sbrendan * +-----------------------+ 36035450Sbrendan * | ^ ^ 36045450Sbrendan * | | | 36055450Sbrendan * l2arc_feed_thread() arc_read() 36065450Sbrendan * | | | 36075450Sbrendan * | l2arc read | 36085450Sbrendan * V | | 36095450Sbrendan * +---------------+ | 36105450Sbrendan * | L2ARC | | 36115450Sbrendan * +---------------+ | 36125450Sbrendan * | ^ | 36135450Sbrendan * l2arc_write() | | 36145450Sbrendan * | | | 36155450Sbrendan * V | | 36165450Sbrendan * +-------+ +-------+ 36175450Sbrendan * | vdev | | vdev | 36185450Sbrendan * | cache | | cache | 36195450Sbrendan * +-------+ +-------+ 36205450Sbrendan * +=========+ .-----. 36215450Sbrendan * : L2ARC : |-_____-| 36225450Sbrendan * : devices : | Disks | 36235450Sbrendan * +=========+ `-_____-' 36245450Sbrendan * 36255450Sbrendan * Read requests are satisfied from the following sources, in order: 36265450Sbrendan * 36275450Sbrendan * 1) ARC 36285450Sbrendan * 2) vdev cache of L2ARC devices 36295450Sbrendan * 3) L2ARC devices 36305450Sbrendan * 4) vdev cache of disks 36315450Sbrendan * 5) disks 36325450Sbrendan * 36335450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 36345450Sbrendan * To accommodate for this there are some significant differences between 36355450Sbrendan * the L2ARC and traditional cache design: 36365450Sbrendan * 36375450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 36385450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 36395450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 36405450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 36415450Sbrendan * 36425450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 36435450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 36445450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 36455450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 36465450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 36475450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 36485450Sbrendan * provide a better sense of ratio than this diagram: 36495450Sbrendan * 36505450Sbrendan * head --> tail 36515450Sbrendan * +---------------------+----------+ 36525450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 36535450Sbrendan * +---------------------+----------+ | o L2ARC eligible 36545450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 36555450Sbrendan * +---------------------+----------+ | 36565450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 36575450Sbrendan * headroom | 36585450Sbrendan * l2arc_feed_thread() 36595450Sbrendan * | 36605450Sbrendan * l2arc write hand <--[oooo]--' 36615450Sbrendan * | 8 Mbyte 36625450Sbrendan * | write max 36635450Sbrendan * V 36645450Sbrendan * +==============================+ 36655450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 36665450Sbrendan * +==============================+ 36675450Sbrendan * 32 Gbytes 36685450Sbrendan * 36695450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 36705450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 36715450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 36725450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 36735450Sbrendan * the ARC lists have moved there due to inactivity. 36745450Sbrendan * 36755450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 36765450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 36775450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 36785450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 36795450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 36805450Sbrendan * quickly, such as during backups of the entire pool. 36815450Sbrendan * 36826987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 36836987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 36846987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 36856987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 36866987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 36876987Sbrendan * 36886987Sbrendan * The L2ARC device write speed is also boosted during this time so that 36896987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 36906987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 36916987Sbrendan * through increased writes. 36926987Sbrendan * 36936987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 36945450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 36955450Sbrendan * device is written to in a rotor fashion, sweeping writes through 36965450Sbrendan * available space then repeating. 36975450Sbrendan * 36986987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 36995450Sbrendan * write buffers back to disk based storage. 37005450Sbrendan * 37016987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 37025450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 37035450Sbrendan * 37045450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 37055450Sbrendan * may be necessary for different workloads: 37065450Sbrendan * 37075450Sbrendan * l2arc_write_max max write bytes per interval 37086987Sbrendan * l2arc_write_boost extra write bytes during device warmup 37095450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 37105450Sbrendan * l2arc_headroom number of max device writes to precache 37115450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 37125450Sbrendan * 37135450Sbrendan * Tunables may be removed or added as future performance improvements are 37145450Sbrendan * integrated, and also may become zpool properties. 37158582SBrendan.Gregg@Sun.COM * 37168582SBrendan.Gregg@Sun.COM * There are three key functions that control how the L2ARC warms up: 37178582SBrendan.Gregg@Sun.COM * 37188582SBrendan.Gregg@Sun.COM * l2arc_write_eligible() check if a buffer is eligible to cache 37198582SBrendan.Gregg@Sun.COM * l2arc_write_size() calculate how much to write 37208582SBrendan.Gregg@Sun.COM * l2arc_write_interval() calculate sleep delay between writes 37218582SBrendan.Gregg@Sun.COM * 37228582SBrendan.Gregg@Sun.COM * These three functions determine what to write, how much, and how quickly 37238582SBrendan.Gregg@Sun.COM * to send writes. 37245450Sbrendan */ 37255450Sbrendan 37268582SBrendan.Gregg@Sun.COM static boolean_t 37278582SBrendan.Gregg@Sun.COM l2arc_write_eligible(spa_t *spa, arc_buf_hdr_t *ab) 37288582SBrendan.Gregg@Sun.COM { 37298582SBrendan.Gregg@Sun.COM /* 37308582SBrendan.Gregg@Sun.COM * A buffer is *not* eligible for the L2ARC if it: 37318582SBrendan.Gregg@Sun.COM * 1. belongs to a different spa. 37328582SBrendan.Gregg@Sun.COM * 2. has no attached buffer. 37338582SBrendan.Gregg@Sun.COM * 3. is already cached on the L2ARC. 37348582SBrendan.Gregg@Sun.COM * 4. has an I/O in progress (it may be an incomplete read). 37358582SBrendan.Gregg@Sun.COM * 5. is flagged not eligible (zfs property). 37368582SBrendan.Gregg@Sun.COM */ 37378582SBrendan.Gregg@Sun.COM if (ab->b_spa != spa || ab->b_buf == NULL || ab->b_l2hdr != NULL || 37388582SBrendan.Gregg@Sun.COM HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) 37398582SBrendan.Gregg@Sun.COM return (B_FALSE); 37408582SBrendan.Gregg@Sun.COM 37418582SBrendan.Gregg@Sun.COM return (B_TRUE); 37428582SBrendan.Gregg@Sun.COM } 37438582SBrendan.Gregg@Sun.COM 37448582SBrendan.Gregg@Sun.COM static uint64_t 37458582SBrendan.Gregg@Sun.COM l2arc_write_size(l2arc_dev_t *dev) 37468582SBrendan.Gregg@Sun.COM { 37478582SBrendan.Gregg@Sun.COM uint64_t size; 37488582SBrendan.Gregg@Sun.COM 37498582SBrendan.Gregg@Sun.COM size = dev->l2ad_write; 37508582SBrendan.Gregg@Sun.COM 37518582SBrendan.Gregg@Sun.COM if (arc_warm == B_FALSE) 37528582SBrendan.Gregg@Sun.COM size += dev->l2ad_boost; 37538582SBrendan.Gregg@Sun.COM 37548582SBrendan.Gregg@Sun.COM return (size); 37558582SBrendan.Gregg@Sun.COM 37568582SBrendan.Gregg@Sun.COM } 37578582SBrendan.Gregg@Sun.COM 37588582SBrendan.Gregg@Sun.COM static clock_t 37598582SBrendan.Gregg@Sun.COM l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 37608582SBrendan.Gregg@Sun.COM { 37618582SBrendan.Gregg@Sun.COM clock_t interval, next; 37628582SBrendan.Gregg@Sun.COM 37638582SBrendan.Gregg@Sun.COM /* 37648582SBrendan.Gregg@Sun.COM * If the ARC lists are busy, increase our write rate; if the 37658582SBrendan.Gregg@Sun.COM * lists are stale, idle back. This is achieved by checking 37668582SBrendan.Gregg@Sun.COM * how much we previously wrote - if it was more than half of 37678582SBrendan.Gregg@Sun.COM * what we wanted, schedule the next write much sooner. 37688582SBrendan.Gregg@Sun.COM */ 37698582SBrendan.Gregg@Sun.COM if (l2arc_feed_again && wrote > (wanted / 2)) 37708582SBrendan.Gregg@Sun.COM interval = (hz * l2arc_feed_min_ms) / 1000; 37718582SBrendan.Gregg@Sun.COM else 37728582SBrendan.Gregg@Sun.COM interval = hz * l2arc_feed_secs; 37738582SBrendan.Gregg@Sun.COM 37748582SBrendan.Gregg@Sun.COM next = MAX(lbolt, MIN(lbolt + interval, began + interval)); 37758582SBrendan.Gregg@Sun.COM 37768582SBrendan.Gregg@Sun.COM return (next); 37778582SBrendan.Gregg@Sun.COM } 37788582SBrendan.Gregg@Sun.COM 37795450Sbrendan static void 37805450Sbrendan l2arc_hdr_stat_add(void) 37815450Sbrendan { 37826018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 37836018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 37845450Sbrendan } 37855450Sbrendan 37865450Sbrendan static void 37875450Sbrendan l2arc_hdr_stat_remove(void) 37885450Sbrendan { 37896018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 37906018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 37915450Sbrendan } 37925450Sbrendan 37935450Sbrendan /* 37945450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 37956987Sbrendan * If a device is returned, this also returns holding the spa config lock. 37965450Sbrendan */ 37975450Sbrendan static l2arc_dev_t * 37985450Sbrendan l2arc_dev_get_next(void) 37995450Sbrendan { 38006987Sbrendan l2arc_dev_t *first, *next = NULL; 38016987Sbrendan 38026987Sbrendan /* 38036987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 38046987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 38056987Sbrendan * both locks will be dropped and a spa config lock held instead. 38066987Sbrendan */ 38076987Sbrendan mutex_enter(&spa_namespace_lock); 38086987Sbrendan mutex_enter(&l2arc_dev_mtx); 38096643Seschrock 38106643Seschrock /* if there are no vdevs, there is nothing to do */ 38116643Seschrock if (l2arc_ndev == 0) 38126987Sbrendan goto out; 38136643Seschrock 38146643Seschrock first = NULL; 38156643Seschrock next = l2arc_dev_last; 38166643Seschrock do { 38176643Seschrock /* loop around the list looking for a non-faulted vdev */ 38186643Seschrock if (next == NULL) { 38195450Sbrendan next = list_head(l2arc_dev_list); 38206643Seschrock } else { 38216643Seschrock next = list_next(l2arc_dev_list, next); 38226643Seschrock if (next == NULL) 38236643Seschrock next = list_head(l2arc_dev_list); 38246643Seschrock } 38256643Seschrock 38266643Seschrock /* if we have come back to the start, bail out */ 38276643Seschrock if (first == NULL) 38286643Seschrock first = next; 38296643Seschrock else if (next == first) 38306643Seschrock break; 38316643Seschrock 38326643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 38336643Seschrock 38346643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 38356643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 38366987Sbrendan next = NULL; 38375450Sbrendan 38385450Sbrendan l2arc_dev_last = next; 38395450Sbrendan 38406987Sbrendan out: 38416987Sbrendan mutex_exit(&l2arc_dev_mtx); 38426987Sbrendan 38436987Sbrendan /* 38446987Sbrendan * Grab the config lock to prevent the 'next' device from being 38456987Sbrendan * removed while we are writing to it. 38466987Sbrendan */ 38476987Sbrendan if (next != NULL) 38487754SJeff.Bonwick@Sun.COM spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 38496987Sbrendan mutex_exit(&spa_namespace_lock); 38506987Sbrendan 38515450Sbrendan return (next); 38525450Sbrendan } 38535450Sbrendan 38545450Sbrendan /* 38556987Sbrendan * Free buffers that were tagged for destruction. 38566987Sbrendan */ 38576987Sbrendan static void 38586987Sbrendan l2arc_do_free_on_write() 38596987Sbrendan { 38606987Sbrendan list_t *buflist; 38616987Sbrendan l2arc_data_free_t *df, *df_prev; 38626987Sbrendan 38636987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 38646987Sbrendan buflist = l2arc_free_on_write; 38656987Sbrendan 38666987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 38676987Sbrendan df_prev = list_prev(buflist, df); 38686987Sbrendan ASSERT(df->l2df_data != NULL); 38696987Sbrendan ASSERT(df->l2df_func != NULL); 38706987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 38716987Sbrendan list_remove(buflist, df); 38726987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 38736987Sbrendan } 38746987Sbrendan 38756987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 38766987Sbrendan } 38776987Sbrendan 38786987Sbrendan /* 38795450Sbrendan * A write to a cache device has completed. Update all headers to allow 38805450Sbrendan * reads from these buffers to begin. 38815450Sbrendan */ 38825450Sbrendan static void 38835450Sbrendan l2arc_write_done(zio_t *zio) 38845450Sbrendan { 38855450Sbrendan l2arc_write_callback_t *cb; 38865450Sbrendan l2arc_dev_t *dev; 38875450Sbrendan list_t *buflist; 38885450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 38896987Sbrendan l2arc_buf_hdr_t *abl2; 38905450Sbrendan kmutex_t *hash_lock; 38915450Sbrendan 38925450Sbrendan cb = zio->io_private; 38935450Sbrendan ASSERT(cb != NULL); 38945450Sbrendan dev = cb->l2wcb_dev; 38955450Sbrendan ASSERT(dev != NULL); 38965450Sbrendan head = cb->l2wcb_head; 38975450Sbrendan ASSERT(head != NULL); 38985450Sbrendan buflist = dev->l2ad_buflist; 38995450Sbrendan ASSERT(buflist != NULL); 39005450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 39015450Sbrendan l2arc_write_callback_t *, cb); 39025450Sbrendan 39035450Sbrendan if (zio->io_error != 0) 39045450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 39055450Sbrendan 39065450Sbrendan mutex_enter(&l2arc_buflist_mtx); 39075450Sbrendan 39085450Sbrendan /* 39095450Sbrendan * All writes completed, or an error was hit. 39105450Sbrendan */ 39115450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 39125450Sbrendan ab_prev = list_prev(buflist, ab); 39135450Sbrendan 39145450Sbrendan hash_lock = HDR_LOCK(ab); 39155450Sbrendan if (!mutex_tryenter(hash_lock)) { 39165450Sbrendan /* 39175450Sbrendan * This buffer misses out. It may be in a stage 39185450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 39195450Sbrendan * left set, denying reads to this buffer. 39205450Sbrendan */ 39215450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 39225450Sbrendan continue; 39235450Sbrendan } 39245450Sbrendan 39255450Sbrendan if (zio->io_error != 0) { 39265450Sbrendan /* 39276987Sbrendan * Error - drop L2ARC entry. 39285450Sbrendan */ 39296987Sbrendan list_remove(buflist, ab); 39306987Sbrendan abl2 = ab->b_l2hdr; 39315450Sbrendan ab->b_l2hdr = NULL; 39326987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 39336987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 39345450Sbrendan } 39355450Sbrendan 39365450Sbrendan /* 39375450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 39385450Sbrendan */ 39395450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 39405450Sbrendan 39415450Sbrendan mutex_exit(hash_lock); 39425450Sbrendan } 39435450Sbrendan 39445450Sbrendan atomic_inc_64(&l2arc_writes_done); 39455450Sbrendan list_remove(buflist, head); 39465450Sbrendan kmem_cache_free(hdr_cache, head); 39475450Sbrendan mutex_exit(&l2arc_buflist_mtx); 39485450Sbrendan 39496987Sbrendan l2arc_do_free_on_write(); 39505450Sbrendan 39515450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 39525450Sbrendan } 39535450Sbrendan 39545450Sbrendan /* 39555450Sbrendan * A read to a cache device completed. Validate buffer contents before 39565450Sbrendan * handing over to the regular ARC routines. 39575450Sbrendan */ 39585450Sbrendan static void 39595450Sbrendan l2arc_read_done(zio_t *zio) 39605450Sbrendan { 39615450Sbrendan l2arc_read_callback_t *cb; 39625450Sbrendan arc_buf_hdr_t *hdr; 39635450Sbrendan arc_buf_t *buf; 39645450Sbrendan kmutex_t *hash_lock; 39656987Sbrendan int equal; 39665450Sbrendan 39677754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd != NULL); 39687754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 39697754SJeff.Bonwick@Sun.COM 39707754SJeff.Bonwick@Sun.COM spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 39717754SJeff.Bonwick@Sun.COM 39725450Sbrendan cb = zio->io_private; 39735450Sbrendan ASSERT(cb != NULL); 39745450Sbrendan buf = cb->l2rcb_buf; 39755450Sbrendan ASSERT(buf != NULL); 39765450Sbrendan hdr = buf->b_hdr; 39775450Sbrendan ASSERT(hdr != NULL); 39785450Sbrendan 39795450Sbrendan hash_lock = HDR_LOCK(hdr); 39805450Sbrendan mutex_enter(hash_lock); 39815450Sbrendan 39825450Sbrendan /* 39835450Sbrendan * Check this survived the L2ARC journey. 39845450Sbrendan */ 39855450Sbrendan equal = arc_cksum_equal(buf); 39865450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 39875450Sbrendan mutex_exit(hash_lock); 39885450Sbrendan zio->io_private = buf; 39897754SJeff.Bonwick@Sun.COM zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 39907754SJeff.Bonwick@Sun.COM zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 39915450Sbrendan arc_read_done(zio); 39925450Sbrendan } else { 39935450Sbrendan mutex_exit(hash_lock); 39945450Sbrendan /* 39955450Sbrendan * Buffer didn't survive caching. Increment stats and 39965450Sbrendan * reissue to the original storage device. 39975450Sbrendan */ 39986987Sbrendan if (zio->io_error != 0) { 39995450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 40006987Sbrendan } else { 40016987Sbrendan zio->io_error = EIO; 40026987Sbrendan } 40035450Sbrendan if (!equal) 40045450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 40055450Sbrendan 40067754SJeff.Bonwick@Sun.COM /* 40077754SJeff.Bonwick@Sun.COM * If there's no waiter, issue an async i/o to the primary 40087754SJeff.Bonwick@Sun.COM * storage now. If there *is* a waiter, the caller must 40097754SJeff.Bonwick@Sun.COM * issue the i/o in a context where it's OK to block. 40107754SJeff.Bonwick@Sun.COM */ 4011*8632SBill.Moore@Sun.COM if (zio->io_waiter == NULL) { 4012*8632SBill.Moore@Sun.COM zio_t *pio = zio_unique_parent(zio); 4013*8632SBill.Moore@Sun.COM 4014*8632SBill.Moore@Sun.COM ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 4015*8632SBill.Moore@Sun.COM 4016*8632SBill.Moore@Sun.COM zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 40177754SJeff.Bonwick@Sun.COM buf->b_data, zio->io_size, arc_read_done, buf, 40187754SJeff.Bonwick@Sun.COM zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 4019*8632SBill.Moore@Sun.COM } 40205450Sbrendan } 40215450Sbrendan 40225450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 40235450Sbrendan } 40245450Sbrendan 40255450Sbrendan /* 40265450Sbrendan * This is the list priority from which the L2ARC will search for pages to 40275450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 40285450Sbrendan * desired order. This order can have a significant effect on cache 40295450Sbrendan * performance. 40305450Sbrendan * 40315450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 40325450Sbrendan * the data lists. This function returns a locked list, and also returns 40335450Sbrendan * the lock pointer. 40345450Sbrendan */ 40355450Sbrendan static list_t * 40365450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 40375450Sbrendan { 40385450Sbrendan list_t *list; 40395450Sbrendan 40405450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 40415450Sbrendan 40425450Sbrendan switch (list_num) { 40435450Sbrendan case 0: 40445450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 40455450Sbrendan *lock = &arc_mfu->arcs_mtx; 40465450Sbrendan break; 40475450Sbrendan case 1: 40485450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 40495450Sbrendan *lock = &arc_mru->arcs_mtx; 40505450Sbrendan break; 40515450Sbrendan case 2: 40525450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 40535450Sbrendan *lock = &arc_mfu->arcs_mtx; 40545450Sbrendan break; 40555450Sbrendan case 3: 40565450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 40575450Sbrendan *lock = &arc_mru->arcs_mtx; 40585450Sbrendan break; 40595450Sbrendan } 40605450Sbrendan 40615450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 40625450Sbrendan mutex_enter(*lock); 40635450Sbrendan return (list); 40645450Sbrendan } 40655450Sbrendan 40665450Sbrendan /* 40675450Sbrendan * Evict buffers from the device write hand to the distance specified in 40685450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 40695450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 40705450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 40715450Sbrendan */ 40725450Sbrendan static void 40735450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 40745450Sbrendan { 40755450Sbrendan list_t *buflist; 40765450Sbrendan l2arc_buf_hdr_t *abl2; 40775450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 40785450Sbrendan kmutex_t *hash_lock; 40795450Sbrendan uint64_t taddr; 40805450Sbrendan 40815450Sbrendan buflist = dev->l2ad_buflist; 40825450Sbrendan 40835450Sbrendan if (buflist == NULL) 40845450Sbrendan return; 40855450Sbrendan 40865450Sbrendan if (!all && dev->l2ad_first) { 40875450Sbrendan /* 40885450Sbrendan * This is the first sweep through the device. There is 40895450Sbrendan * nothing to evict. 40905450Sbrendan */ 40915450Sbrendan return; 40925450Sbrendan } 40935450Sbrendan 40946987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 40955450Sbrendan /* 40965450Sbrendan * When nearing the end of the device, evict to the end 40975450Sbrendan * before the device write hand jumps to the start. 40985450Sbrendan */ 40995450Sbrendan taddr = dev->l2ad_end; 41005450Sbrendan } else { 41015450Sbrendan taddr = dev->l2ad_hand + distance; 41025450Sbrendan } 41035450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 41045450Sbrendan uint64_t, taddr, boolean_t, all); 41055450Sbrendan 41065450Sbrendan top: 41075450Sbrendan mutex_enter(&l2arc_buflist_mtx); 41085450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 41095450Sbrendan ab_prev = list_prev(buflist, ab); 41105450Sbrendan 41115450Sbrendan hash_lock = HDR_LOCK(ab); 41125450Sbrendan if (!mutex_tryenter(hash_lock)) { 41135450Sbrendan /* 41145450Sbrendan * Missed the hash lock. Retry. 41155450Sbrendan */ 41165450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 41175450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41185450Sbrendan mutex_enter(hash_lock); 41195450Sbrendan mutex_exit(hash_lock); 41205450Sbrendan goto top; 41215450Sbrendan } 41225450Sbrendan 41235450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 41245450Sbrendan /* 41255450Sbrendan * We hit a write head node. Leave it for 41265450Sbrendan * l2arc_write_done(). 41275450Sbrendan */ 41285450Sbrendan list_remove(buflist, ab); 41295450Sbrendan mutex_exit(hash_lock); 41305450Sbrendan continue; 41315450Sbrendan } 41325450Sbrendan 41335450Sbrendan if (!all && ab->b_l2hdr != NULL && 41345450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 41355450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 41365450Sbrendan /* 41375450Sbrendan * We've evicted to the target address, 41385450Sbrendan * or the end of the device. 41395450Sbrendan */ 41405450Sbrendan mutex_exit(hash_lock); 41415450Sbrendan break; 41425450Sbrendan } 41435450Sbrendan 41445450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 41455450Sbrendan /* 41465450Sbrendan * Already on the path to destruction. 41475450Sbrendan */ 41485450Sbrendan mutex_exit(hash_lock); 41495450Sbrendan continue; 41505450Sbrendan } 41515450Sbrendan 41525450Sbrendan if (ab->b_state == arc_l2c_only) { 41535450Sbrendan ASSERT(!HDR_L2_READING(ab)); 41545450Sbrendan /* 41555450Sbrendan * This doesn't exist in the ARC. Destroy. 41565450Sbrendan * arc_hdr_destroy() will call list_remove() 41575450Sbrendan * and decrement arcstat_l2_size. 41585450Sbrendan */ 41595450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 41605450Sbrendan arc_hdr_destroy(ab); 41615450Sbrendan } else { 41625450Sbrendan /* 41636987Sbrendan * Invalidate issued or about to be issued 41646987Sbrendan * reads, since we may be about to write 41656987Sbrendan * over this location. 41666987Sbrendan */ 41676987Sbrendan if (HDR_L2_READING(ab)) { 41686987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 41696987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 41706987Sbrendan } 41716987Sbrendan 41726987Sbrendan /* 41735450Sbrendan * Tell ARC this no longer exists in L2ARC. 41745450Sbrendan */ 41755450Sbrendan if (ab->b_l2hdr != NULL) { 41765450Sbrendan abl2 = ab->b_l2hdr; 41775450Sbrendan ab->b_l2hdr = NULL; 41785450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 41795450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 41805450Sbrendan } 41815450Sbrendan list_remove(buflist, ab); 41825450Sbrendan 41835450Sbrendan /* 41845450Sbrendan * This may have been leftover after a 41855450Sbrendan * failed write. 41865450Sbrendan */ 41875450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 41885450Sbrendan } 41895450Sbrendan mutex_exit(hash_lock); 41905450Sbrendan } 41915450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41925450Sbrendan 41935450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, -(taddr - dev->l2ad_evict)); 41945450Sbrendan dev->l2ad_evict = taddr; 41955450Sbrendan } 41965450Sbrendan 41975450Sbrendan /* 41985450Sbrendan * Find and write ARC buffers to the L2ARC device. 41995450Sbrendan * 42005450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 42015450Sbrendan * for reading until they have completed writing. 42025450Sbrendan */ 42038582SBrendan.Gregg@Sun.COM static uint64_t 42046987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 42055450Sbrendan { 42065450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 42075450Sbrendan l2arc_buf_hdr_t *hdrl2; 42085450Sbrendan list_t *list; 42096987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 42105450Sbrendan void *buf_data; 42115450Sbrendan kmutex_t *hash_lock, *list_lock; 42125450Sbrendan boolean_t have_lock, full; 42135450Sbrendan l2arc_write_callback_t *cb; 42145450Sbrendan zio_t *pio, *wzio; 42155450Sbrendan 42165450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 42175450Sbrendan 42185450Sbrendan pio = NULL; 42195450Sbrendan write_sz = 0; 42205450Sbrendan full = B_FALSE; 42216245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 42225450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 42235450Sbrendan 42245450Sbrendan /* 42255450Sbrendan * Copy buffers for L2ARC writing. 42265450Sbrendan */ 42275450Sbrendan mutex_enter(&l2arc_buflist_mtx); 42285450Sbrendan for (int try = 0; try <= 3; try++) { 42295450Sbrendan list = l2arc_list_locked(try, &list_lock); 42305450Sbrendan passed_sz = 0; 42315450Sbrendan 42326987Sbrendan /* 42336987Sbrendan * L2ARC fast warmup. 42346987Sbrendan * 42356987Sbrendan * Until the ARC is warm and starts to evict, read from the 42366987Sbrendan * head of the ARC lists rather than the tail. 42376987Sbrendan */ 42386987Sbrendan headroom = target_sz * l2arc_headroom; 42396987Sbrendan if (arc_warm == B_FALSE) 42406987Sbrendan ab = list_head(list); 42416987Sbrendan else 42426987Sbrendan ab = list_tail(list); 42436987Sbrendan 42446987Sbrendan for (; ab; ab = ab_prev) { 42456987Sbrendan if (arc_warm == B_FALSE) 42466987Sbrendan ab_prev = list_next(list, ab); 42476987Sbrendan else 42486987Sbrendan ab_prev = list_prev(list, ab); 42495450Sbrendan 42505450Sbrendan hash_lock = HDR_LOCK(ab); 42515450Sbrendan have_lock = MUTEX_HELD(hash_lock); 42525450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 42535450Sbrendan /* 42545450Sbrendan * Skip this buffer rather than waiting. 42555450Sbrendan */ 42565450Sbrendan continue; 42575450Sbrendan } 42585450Sbrendan 42595450Sbrendan passed_sz += ab->b_size; 42605450Sbrendan if (passed_sz > headroom) { 42615450Sbrendan /* 42625450Sbrendan * Searched too far. 42635450Sbrendan */ 42645450Sbrendan mutex_exit(hash_lock); 42655450Sbrendan break; 42665450Sbrendan } 42675450Sbrendan 42688582SBrendan.Gregg@Sun.COM if (!l2arc_write_eligible(spa, ab)) { 42695450Sbrendan mutex_exit(hash_lock); 42705450Sbrendan continue; 42715450Sbrendan } 42725450Sbrendan 42735450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 42745450Sbrendan full = B_TRUE; 42755450Sbrendan mutex_exit(hash_lock); 42765450Sbrendan break; 42775450Sbrendan } 42785450Sbrendan 42795450Sbrendan if (pio == NULL) { 42805450Sbrendan /* 42815450Sbrendan * Insert a dummy header on the buflist so 42825450Sbrendan * l2arc_write_done() can find where the 42835450Sbrendan * write buffers begin without searching. 42845450Sbrendan */ 42855450Sbrendan list_insert_head(dev->l2ad_buflist, head); 42865450Sbrendan 42875450Sbrendan cb = kmem_alloc( 42885450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 42895450Sbrendan cb->l2wcb_dev = dev; 42905450Sbrendan cb->l2wcb_head = head; 42915450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 42925450Sbrendan ZIO_FLAG_CANFAIL); 42935450Sbrendan } 42945450Sbrendan 42955450Sbrendan /* 42965450Sbrendan * Create and add a new L2ARC header. 42975450Sbrendan */ 42985450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 42995450Sbrendan hdrl2->b_dev = dev; 43005450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 43015450Sbrendan 43025450Sbrendan ab->b_flags |= ARC_L2_WRITING; 43035450Sbrendan ab->b_l2hdr = hdrl2; 43045450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 43055450Sbrendan buf_data = ab->b_buf->b_data; 43065450Sbrendan buf_sz = ab->b_size; 43075450Sbrendan 43085450Sbrendan /* 43095450Sbrendan * Compute and store the buffer cksum before 43105450Sbrendan * writing. On debug the cksum is verified first. 43115450Sbrendan */ 43125450Sbrendan arc_cksum_verify(ab->b_buf); 43135450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 43145450Sbrendan 43155450Sbrendan mutex_exit(hash_lock); 43165450Sbrendan 43175450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 43185450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 43195450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 43205450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 43215450Sbrendan 43225450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 43235450Sbrendan zio_t *, wzio); 43245450Sbrendan (void) zio_nowait(wzio); 43255450Sbrendan 43267754SJeff.Bonwick@Sun.COM /* 43277754SJeff.Bonwick@Sun.COM * Keep the clock hand suitably device-aligned. 43287754SJeff.Bonwick@Sun.COM */ 43297754SJeff.Bonwick@Sun.COM buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 43307754SJeff.Bonwick@Sun.COM 43315450Sbrendan write_sz += buf_sz; 43325450Sbrendan dev->l2ad_hand += buf_sz; 43335450Sbrendan } 43345450Sbrendan 43355450Sbrendan mutex_exit(list_lock); 43365450Sbrendan 43375450Sbrendan if (full == B_TRUE) 43385450Sbrendan break; 43395450Sbrendan } 43405450Sbrendan mutex_exit(&l2arc_buflist_mtx); 43415450Sbrendan 43425450Sbrendan if (pio == NULL) { 43435450Sbrendan ASSERT3U(write_sz, ==, 0); 43445450Sbrendan kmem_cache_free(hdr_cache, head); 43458582SBrendan.Gregg@Sun.COM return (0); 43465450Sbrendan } 43475450Sbrendan 43485450Sbrendan ASSERT3U(write_sz, <=, target_sz); 43495450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 43508582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 43515450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 43525450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, write_sz); 43535450Sbrendan 43545450Sbrendan /* 43555450Sbrendan * Bump device hand to the device start if it is approaching the end. 43565450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 43575450Sbrendan */ 43586987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 43595450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, 43605450Sbrendan dev->l2ad_end - dev->l2ad_hand); 43615450Sbrendan dev->l2ad_hand = dev->l2ad_start; 43625450Sbrendan dev->l2ad_evict = dev->l2ad_start; 43635450Sbrendan dev->l2ad_first = B_FALSE; 43645450Sbrendan } 43655450Sbrendan 43668582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_TRUE; 43675450Sbrendan (void) zio_wait(pio); 43688582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_FALSE; 43698582SBrendan.Gregg@Sun.COM 43708582SBrendan.Gregg@Sun.COM return (write_sz); 43715450Sbrendan } 43725450Sbrendan 43735450Sbrendan /* 43745450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 43755450Sbrendan * heart of the L2ARC. 43765450Sbrendan */ 43775450Sbrendan static void 43785450Sbrendan l2arc_feed_thread(void) 43795450Sbrendan { 43805450Sbrendan callb_cpr_t cpr; 43815450Sbrendan l2arc_dev_t *dev; 43825450Sbrendan spa_t *spa; 43838582SBrendan.Gregg@Sun.COM uint64_t size, wrote; 43848582SBrendan.Gregg@Sun.COM clock_t begin, next = lbolt; 43855450Sbrendan 43865450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 43875450Sbrendan 43885450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 43895450Sbrendan 43905450Sbrendan while (l2arc_thread_exit == 0) { 43915450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 43926987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 43938582SBrendan.Gregg@Sun.COM next); 43946987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 43958582SBrendan.Gregg@Sun.COM next = lbolt + hz; 43966987Sbrendan 43976987Sbrendan /* 43986987Sbrendan * Quick check for L2ARC devices. 43996987Sbrendan */ 44006987Sbrendan mutex_enter(&l2arc_dev_mtx); 44016987Sbrendan if (l2arc_ndev == 0) { 44026987Sbrendan mutex_exit(&l2arc_dev_mtx); 44036987Sbrendan continue; 44045450Sbrendan } 44056987Sbrendan mutex_exit(&l2arc_dev_mtx); 44068582SBrendan.Gregg@Sun.COM begin = lbolt; 44076643Seschrock 44085450Sbrendan /* 44096643Seschrock * This selects the next l2arc device to write to, and in 44106643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 44116987Sbrendan * will return NULL if there are now no l2arc devices or if 44126987Sbrendan * they are all faulted. 44136987Sbrendan * 44146987Sbrendan * If a device is returned, its spa's config lock is also 44156987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 44166987Sbrendan * will grab and release l2arc_dev_mtx. 44175450Sbrendan */ 44186987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 44195450Sbrendan continue; 44206987Sbrendan 44216987Sbrendan spa = dev->l2ad_spa; 44226987Sbrendan ASSERT(spa != NULL); 44235450Sbrendan 44245450Sbrendan /* 44255450Sbrendan * Avoid contributing to memory pressure. 44265450Sbrendan */ 44275450Sbrendan if (arc_reclaim_needed()) { 44285450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 44297754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44305450Sbrendan continue; 44315450Sbrendan } 44325450Sbrendan 44335450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 44345450Sbrendan 44358582SBrendan.Gregg@Sun.COM size = l2arc_write_size(dev); 44366987Sbrendan 44375450Sbrendan /* 44385450Sbrendan * Evict L2ARC buffers that will be overwritten. 44395450Sbrendan */ 44406987Sbrendan l2arc_evict(dev, size, B_FALSE); 44415450Sbrendan 44425450Sbrendan /* 44435450Sbrendan * Write ARC buffers. 44445450Sbrendan */ 44458582SBrendan.Gregg@Sun.COM wrote = l2arc_write_buffers(spa, dev, size); 44468582SBrendan.Gregg@Sun.COM 44478582SBrendan.Gregg@Sun.COM /* 44488582SBrendan.Gregg@Sun.COM * Calculate interval between writes. 44498582SBrendan.Gregg@Sun.COM */ 44508582SBrendan.Gregg@Sun.COM next = l2arc_write_interval(begin, size, wrote); 44517754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44525450Sbrendan } 44535450Sbrendan 44545450Sbrendan l2arc_thread_exit = 0; 44555450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 44565450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 44575450Sbrendan thread_exit(); 44585450Sbrendan } 44595450Sbrendan 44606643Seschrock boolean_t 44616643Seschrock l2arc_vdev_present(vdev_t *vd) 44626643Seschrock { 44636643Seschrock l2arc_dev_t *dev; 44646643Seschrock 44656643Seschrock mutex_enter(&l2arc_dev_mtx); 44666643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 44676643Seschrock dev = list_next(l2arc_dev_list, dev)) { 44686643Seschrock if (dev->l2ad_vdev == vd) 44696643Seschrock break; 44706643Seschrock } 44716643Seschrock mutex_exit(&l2arc_dev_mtx); 44726643Seschrock 44736643Seschrock return (dev != NULL); 44746643Seschrock } 44756643Seschrock 44765450Sbrendan /* 44775450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 44785450Sbrendan * validated the vdev and opened it. 44795450Sbrendan */ 44805450Sbrendan void 44815450Sbrendan l2arc_add_vdev(spa_t *spa, vdev_t *vd, uint64_t start, uint64_t end) 44825450Sbrendan { 44835450Sbrendan l2arc_dev_t *adddev; 44845450Sbrendan 44856643Seschrock ASSERT(!l2arc_vdev_present(vd)); 44866643Seschrock 44875450Sbrendan /* 44885450Sbrendan * Create a new l2arc device entry. 44895450Sbrendan */ 44905450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 44915450Sbrendan adddev->l2ad_spa = spa; 44925450Sbrendan adddev->l2ad_vdev = vd; 44935450Sbrendan adddev->l2ad_write = l2arc_write_max; 44946987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 44955450Sbrendan adddev->l2ad_start = start; 44965450Sbrendan adddev->l2ad_end = end; 44975450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 44985450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 44995450Sbrendan adddev->l2ad_first = B_TRUE; 45008582SBrendan.Gregg@Sun.COM adddev->l2ad_writing = B_FALSE; 45015450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 45025450Sbrendan 45035450Sbrendan /* 45045450Sbrendan * This is a list of all ARC buffers that are still valid on the 45055450Sbrendan * device. 45065450Sbrendan */ 45075450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 45085450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 45095450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 45105450Sbrendan 45115450Sbrendan spa_l2cache_space_update(vd, adddev->l2ad_end - adddev->l2ad_hand, 0); 45125450Sbrendan 45135450Sbrendan /* 45145450Sbrendan * Add device to global list 45155450Sbrendan */ 45165450Sbrendan mutex_enter(&l2arc_dev_mtx); 45175450Sbrendan list_insert_head(l2arc_dev_list, adddev); 45185450Sbrendan atomic_inc_64(&l2arc_ndev); 45195450Sbrendan mutex_exit(&l2arc_dev_mtx); 45205450Sbrendan } 45215450Sbrendan 45225450Sbrendan /* 45235450Sbrendan * Remove a vdev from the L2ARC. 45245450Sbrendan */ 45255450Sbrendan void 45265450Sbrendan l2arc_remove_vdev(vdev_t *vd) 45275450Sbrendan { 45285450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 45295450Sbrendan 45305450Sbrendan /* 45315450Sbrendan * Find the device by vdev 45325450Sbrendan */ 45335450Sbrendan mutex_enter(&l2arc_dev_mtx); 45345450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 45355450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 45365450Sbrendan if (vd == dev->l2ad_vdev) { 45375450Sbrendan remdev = dev; 45385450Sbrendan break; 45395450Sbrendan } 45405450Sbrendan } 45415450Sbrendan ASSERT(remdev != NULL); 45425450Sbrendan 45435450Sbrendan /* 45445450Sbrendan * Remove device from global list 45455450Sbrendan */ 45465450Sbrendan list_remove(l2arc_dev_list, remdev); 45475450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 45486987Sbrendan atomic_dec_64(&l2arc_ndev); 45496987Sbrendan mutex_exit(&l2arc_dev_mtx); 45505450Sbrendan 45515450Sbrendan /* 45525450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 45535450Sbrendan */ 45545450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 45555450Sbrendan list_destroy(remdev->l2ad_buflist); 45565450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 45575450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 45585450Sbrendan } 45595450Sbrendan 45605450Sbrendan void 45617754SJeff.Bonwick@Sun.COM l2arc_init(void) 45625450Sbrendan { 45635450Sbrendan l2arc_thread_exit = 0; 45645450Sbrendan l2arc_ndev = 0; 45655450Sbrendan l2arc_writes_sent = 0; 45665450Sbrendan l2arc_writes_done = 0; 45675450Sbrendan 45685450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 45695450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 45705450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 45715450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 45725450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 45735450Sbrendan 45745450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 45755450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 45765450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 45775450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 45785450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 45795450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 45805450Sbrendan } 45815450Sbrendan 45825450Sbrendan void 45837754SJeff.Bonwick@Sun.COM l2arc_fini(void) 45845450Sbrendan { 45856987Sbrendan /* 45866987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 45876987Sbrendan * Because of this, we can assume that all l2arc devices have 45886987Sbrendan * already been removed when the pools themselves were removed. 45896987Sbrendan */ 45906987Sbrendan 45916987Sbrendan l2arc_do_free_on_write(); 45926987Sbrendan 45935450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 45945450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 45955450Sbrendan mutex_destroy(&l2arc_dev_mtx); 45965450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 45975450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 45985450Sbrendan 45995450Sbrendan list_destroy(l2arc_dev_list); 46005450Sbrendan list_destroy(l2arc_free_on_write); 46015450Sbrendan } 46027754SJeff.Bonwick@Sun.COM 46037754SJeff.Bonwick@Sun.COM void 46047754SJeff.Bonwick@Sun.COM l2arc_start(void) 46057754SJeff.Bonwick@Sun.COM { 46068241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46077754SJeff.Bonwick@Sun.COM return; 46087754SJeff.Bonwick@Sun.COM 46097754SJeff.Bonwick@Sun.COM (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 46107754SJeff.Bonwick@Sun.COM TS_RUN, minclsyspri); 46117754SJeff.Bonwick@Sun.COM } 46127754SJeff.Bonwick@Sun.COM 46137754SJeff.Bonwick@Sun.COM void 46147754SJeff.Bonwick@Sun.COM l2arc_stop(void) 46157754SJeff.Bonwick@Sun.COM { 46168241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46177754SJeff.Bonwick@Sun.COM return; 46187754SJeff.Bonwick@Sun.COM 46197754SJeff.Bonwick@Sun.COM mutex_enter(&l2arc_feed_thr_lock); 46207754SJeff.Bonwick@Sun.COM cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 46217754SJeff.Bonwick@Sun.COM l2arc_thread_exit = 1; 46227754SJeff.Bonwick@Sun.COM while (l2arc_thread_exit != 0) 46237754SJeff.Bonwick@Sun.COM cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 46247754SJeff.Bonwick@Sun.COM mutex_exit(&l2arc_feed_thr_lock); 46257754SJeff.Bonwick@Sun.COM } 4626