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; 4458636SMark.Maybee@Sun.COM uint64_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); 4678636SMark.Maybee@Sun.COM static void arc_evict_ghost(arc_state_t *state, uint64_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 */ 6139215SGeorge.Wilson@Sun.COM uint64_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 6338636SMark.Maybee@Sun.COM buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 634789Sahrens { 635789Sahrens uint8_t *vdva = (uint8_t *)dva; 636789Sahrens uint64_t crc = -1ULL; 637789Sahrens int i; 638789Sahrens 639789Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 640789Sahrens 641789Sahrens for (i = 0; i < sizeof (dva_t); i++) 642789Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 643789Sahrens 6448636SMark.Maybee@Sun.COM crc ^= (spa>>8) ^ birth; 645789Sahrens 646789Sahrens return (crc); 647789Sahrens } 648789Sahrens 649789Sahrens #define BUF_EMPTY(buf) \ 650789Sahrens ((buf)->b_dva.dva_word[0] == 0 && \ 651789Sahrens (buf)->b_dva.dva_word[1] == 0 && \ 652789Sahrens (buf)->b_birth == 0) 653789Sahrens 654789Sahrens #define BUF_EQUAL(spa, dva, birth, buf) \ 655789Sahrens ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 656789Sahrens ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 657789Sahrens ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 658789Sahrens 659789Sahrens static arc_buf_hdr_t * 6608636SMark.Maybee@Sun.COM buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 661789Sahrens { 662789Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 663789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 664789Sahrens arc_buf_hdr_t *buf; 665789Sahrens 666789Sahrens mutex_enter(hash_lock); 667789Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 668789Sahrens buf = buf->b_hash_next) { 669789Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 670789Sahrens *lockp = hash_lock; 671789Sahrens return (buf); 672789Sahrens } 673789Sahrens } 674789Sahrens mutex_exit(hash_lock); 675789Sahrens *lockp = NULL; 676789Sahrens return (NULL); 677789Sahrens } 678789Sahrens 679789Sahrens /* 680789Sahrens * Insert an entry into the hash table. If there is already an element 681789Sahrens * equal to elem in the hash table, then the already existing element 682789Sahrens * will be returned and the new element will not be inserted. 683789Sahrens * Otherwise returns NULL. 684789Sahrens */ 685789Sahrens static arc_buf_hdr_t * 686789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 687789Sahrens { 688789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 689789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 690789Sahrens arc_buf_hdr_t *fbuf; 6913403Sbmc uint32_t i; 692789Sahrens 6931544Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 694789Sahrens *lockp = hash_lock; 695789Sahrens mutex_enter(hash_lock); 696789Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 697789Sahrens fbuf = fbuf->b_hash_next, i++) { 698789Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 699789Sahrens return (fbuf); 700789Sahrens } 701789Sahrens 702789Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 703789Sahrens buf_hash_table.ht_table[idx] = buf; 7041544Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 705789Sahrens 706789Sahrens /* collect some hash table performance data */ 707789Sahrens if (i > 0) { 7083403Sbmc ARCSTAT_BUMP(arcstat_hash_collisions); 709789Sahrens if (i == 1) 7103403Sbmc ARCSTAT_BUMP(arcstat_hash_chains); 7113403Sbmc 7123403Sbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 713789Sahrens } 7143403Sbmc 7153403Sbmc ARCSTAT_BUMP(arcstat_hash_elements); 7163403Sbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 717789Sahrens 718789Sahrens return (NULL); 719789Sahrens } 720789Sahrens 721789Sahrens static void 722789Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 723789Sahrens { 724789Sahrens arc_buf_hdr_t *fbuf, **bufp; 725789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 726789Sahrens 727789Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 7281544Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 729789Sahrens 730789Sahrens bufp = &buf_hash_table.ht_table[idx]; 731789Sahrens while ((fbuf = *bufp) != buf) { 732789Sahrens ASSERT(fbuf != NULL); 733789Sahrens bufp = &fbuf->b_hash_next; 734789Sahrens } 735789Sahrens *bufp = buf->b_hash_next; 736789Sahrens buf->b_hash_next = NULL; 7371544Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 738789Sahrens 739789Sahrens /* collect some hash table performance data */ 7403403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 7413403Sbmc 742789Sahrens if (buf_hash_table.ht_table[idx] && 743789Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 7443403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 745789Sahrens } 746789Sahrens 747789Sahrens /* 748789Sahrens * Global data structures and functions for the buf kmem cache. 749789Sahrens */ 750789Sahrens static kmem_cache_t *hdr_cache; 751789Sahrens static kmem_cache_t *buf_cache; 752789Sahrens 753789Sahrens static void 754789Sahrens buf_fini(void) 755789Sahrens { 756789Sahrens int i; 757789Sahrens 758789Sahrens kmem_free(buf_hash_table.ht_table, 759789Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 760789Sahrens for (i = 0; i < BUF_LOCKS; i++) 761789Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 762789Sahrens kmem_cache_destroy(hdr_cache); 763789Sahrens kmem_cache_destroy(buf_cache); 764789Sahrens } 765789Sahrens 766789Sahrens /* 767789Sahrens * Constructor callback - called when the cache is empty 768789Sahrens * and a new buf is requested. 769789Sahrens */ 770789Sahrens /* ARGSUSED */ 771789Sahrens static int 772789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 773789Sahrens { 774789Sahrens arc_buf_hdr_t *buf = vbuf; 775789Sahrens 776789Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 777789Sahrens refcount_create(&buf->b_refcnt); 778789Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 7794831Sgw25295 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 7808582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 7818582SBrendan.Gregg@Sun.COM 782789Sahrens return (0); 783789Sahrens } 784789Sahrens 7857545SMark.Maybee@Sun.COM /* ARGSUSED */ 7867545SMark.Maybee@Sun.COM static int 7877545SMark.Maybee@Sun.COM buf_cons(void *vbuf, void *unused, int kmflag) 7887545SMark.Maybee@Sun.COM { 7897545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 7907545SMark.Maybee@Sun.COM 7917545SMark.Maybee@Sun.COM bzero(buf, sizeof (arc_buf_t)); 7927545SMark.Maybee@Sun.COM rw_init(&buf->b_lock, NULL, RW_DEFAULT, NULL); 7938582SBrendan.Gregg@Sun.COM arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 7948582SBrendan.Gregg@Sun.COM 7957545SMark.Maybee@Sun.COM return (0); 7967545SMark.Maybee@Sun.COM } 7977545SMark.Maybee@Sun.COM 798789Sahrens /* 799789Sahrens * Destructor callback - called when a cached buf is 800789Sahrens * no longer required. 801789Sahrens */ 802789Sahrens /* ARGSUSED */ 803789Sahrens static void 804789Sahrens hdr_dest(void *vbuf, void *unused) 805789Sahrens { 806789Sahrens arc_buf_hdr_t *buf = vbuf; 807789Sahrens 808789Sahrens refcount_destroy(&buf->b_refcnt); 809789Sahrens cv_destroy(&buf->b_cv); 8104831Sgw25295 mutex_destroy(&buf->b_freeze_lock); 8118582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 812789Sahrens } 813789Sahrens 8147545SMark.Maybee@Sun.COM /* ARGSUSED */ 8157545SMark.Maybee@Sun.COM static void 8167545SMark.Maybee@Sun.COM buf_dest(void *vbuf, void *unused) 8177545SMark.Maybee@Sun.COM { 8187545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 8197545SMark.Maybee@Sun.COM 8207545SMark.Maybee@Sun.COM rw_destroy(&buf->b_lock); 8218582SBrendan.Gregg@Sun.COM arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 8227545SMark.Maybee@Sun.COM } 8237545SMark.Maybee@Sun.COM 824789Sahrens /* 825789Sahrens * Reclaim callback -- invoked when memory is low. 826789Sahrens */ 827789Sahrens /* ARGSUSED */ 828789Sahrens static void 829789Sahrens hdr_recl(void *unused) 830789Sahrens { 831789Sahrens dprintf("hdr_recl called\n"); 8323158Smaybee /* 8333158Smaybee * umem calls the reclaim func when we destroy the buf cache, 8343158Smaybee * which is after we do arc_fini(). 8353158Smaybee */ 8363158Smaybee if (!arc_dead) 8373158Smaybee cv_signal(&arc_reclaim_thr_cv); 838789Sahrens } 839789Sahrens 840789Sahrens static void 841789Sahrens buf_init(void) 842789Sahrens { 843789Sahrens uint64_t *ct; 8441544Seschrock uint64_t hsize = 1ULL << 12; 845789Sahrens int i, j; 846789Sahrens 847789Sahrens /* 848789Sahrens * The hash table is big enough to fill all of physical memory 8491544Seschrock * with an average 64K block size. The table will take up 8501544Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 851789Sahrens */ 8521544Seschrock while (hsize * 65536 < physmem * PAGESIZE) 853789Sahrens hsize <<= 1; 8541544Seschrock retry: 855789Sahrens buf_hash_table.ht_mask = hsize - 1; 8561544Seschrock buf_hash_table.ht_table = 8571544Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 8581544Seschrock if (buf_hash_table.ht_table == NULL) { 8591544Seschrock ASSERT(hsize > (1ULL << 8)); 8601544Seschrock hsize >>= 1; 8611544Seschrock goto retry; 8621544Seschrock } 863789Sahrens 864789Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 865789Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 866789Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 8677545SMark.Maybee@Sun.COM 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 868789Sahrens 869789Sahrens for (i = 0; i < 256; i++) 870789Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 871789Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 872789Sahrens 873789Sahrens for (i = 0; i < BUF_LOCKS; i++) { 874789Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 875789Sahrens NULL, MUTEX_DEFAULT, NULL); 876789Sahrens } 877789Sahrens } 878789Sahrens 879789Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 880789Sahrens 881789Sahrens static void 8823093Sahrens arc_cksum_verify(arc_buf_t *buf) 8833093Sahrens { 8843093Sahrens zio_cksum_t zc; 8853093Sahrens 8863312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 8873093Sahrens return; 8883093Sahrens 8893093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8903265Sahrens if (buf->b_hdr->b_freeze_cksum == NULL || 8913265Sahrens (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 8923093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8933093Sahrens return; 8943093Sahrens } 8953093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8963093Sahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 8973093Sahrens panic("buffer modified while frozen!"); 8983093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8993093Sahrens } 9003093Sahrens 9015450Sbrendan static int 9025450Sbrendan arc_cksum_equal(arc_buf_t *buf) 9035450Sbrendan { 9045450Sbrendan zio_cksum_t zc; 9055450Sbrendan int equal; 9065450Sbrendan 9075450Sbrendan mutex_enter(&buf->b_hdr->b_freeze_lock); 9085450Sbrendan fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 9095450Sbrendan equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 9105450Sbrendan mutex_exit(&buf->b_hdr->b_freeze_lock); 9115450Sbrendan 9125450Sbrendan return (equal); 9135450Sbrendan } 9145450Sbrendan 9153093Sahrens static void 9165450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force) 9173093Sahrens { 9185450Sbrendan if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 9193093Sahrens return; 9203093Sahrens 9213093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9223093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9233093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9243093Sahrens return; 9253093Sahrens } 9263093Sahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 9273093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 9283093Sahrens buf->b_hdr->b_freeze_cksum); 9293093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9303093Sahrens } 9313093Sahrens 9323093Sahrens void 9333093Sahrens arc_buf_thaw(arc_buf_t *buf) 9343093Sahrens { 9355450Sbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 9365450Sbrendan if (buf->b_hdr->b_state != arc_anon) 9375450Sbrendan panic("modifying non-anon buffer!"); 9385450Sbrendan if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 9395450Sbrendan panic("modifying buffer while i/o in progress!"); 9405450Sbrendan arc_cksum_verify(buf); 9415450Sbrendan } 9425450Sbrendan 9433093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9443093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9453093Sahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 9463093Sahrens buf->b_hdr->b_freeze_cksum = NULL; 9473093Sahrens } 9483093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9493093Sahrens } 9503093Sahrens 9513093Sahrens void 9523093Sahrens arc_buf_freeze(arc_buf_t *buf) 9533093Sahrens { 9543312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 9553312Sahrens return; 9563312Sahrens 9573093Sahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 9583403Sbmc buf->b_hdr->b_state == arc_anon); 9595450Sbrendan arc_cksum_compute(buf, B_FALSE); 9603093Sahrens } 9613093Sahrens 9623093Sahrens static void 963789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 964789Sahrens { 965789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 966789Sahrens 967789Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 9683403Sbmc (ab->b_state != arc_anon)) { 9693700Sek110237 uint64_t delta = ab->b_size * ab->b_datacnt; 9704309Smaybee list_t *list = &ab->b_state->arcs_list[ab->b_type]; 9714309Smaybee uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 972789Sahrens 9733403Sbmc ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx)); 9743403Sbmc mutex_enter(&ab->b_state->arcs_mtx); 975789Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 9764309Smaybee list_remove(list, ab); 9771544Seschrock if (GHOST_STATE(ab->b_state)) { 9781544Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 9791544Seschrock ASSERT3P(ab->b_buf, ==, NULL); 9801544Seschrock delta = ab->b_size; 9811544Seschrock } 9821544Seschrock ASSERT(delta > 0); 9834309Smaybee ASSERT3U(*size, >=, delta); 9844309Smaybee atomic_add_64(size, -delta); 9853403Sbmc mutex_exit(&ab->b_state->arcs_mtx); 9867046Sahrens /* remove the prefetch flag if we get a reference */ 9872391Smaybee if (ab->b_flags & ARC_PREFETCH) 9882391Smaybee ab->b_flags &= ~ARC_PREFETCH; 989789Sahrens } 990789Sahrens } 991789Sahrens 992789Sahrens static int 993789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 994789Sahrens { 995789Sahrens int cnt; 9963403Sbmc arc_state_t *state = ab->b_state; 997789Sahrens 9983403Sbmc ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 9993403Sbmc ASSERT(!GHOST_STATE(state)); 1000789Sahrens 1001789Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 10023403Sbmc (state != arc_anon)) { 10034309Smaybee uint64_t *size = &state->arcs_lsize[ab->b_type]; 10044309Smaybee 10053403Sbmc ASSERT(!MUTEX_HELD(&state->arcs_mtx)); 10063403Sbmc mutex_enter(&state->arcs_mtx); 1007789Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 10084309Smaybee list_insert_head(&state->arcs_list[ab->b_type], ab); 10091544Seschrock ASSERT(ab->b_datacnt > 0); 10104309Smaybee atomic_add_64(size, ab->b_size * ab->b_datacnt); 10113403Sbmc mutex_exit(&state->arcs_mtx); 1012789Sahrens } 1013789Sahrens return (cnt); 1014789Sahrens } 1015789Sahrens 1016789Sahrens /* 1017789Sahrens * Move the supplied buffer to the indicated state. The mutex 1018789Sahrens * for the buffer must be held by the caller. 1019789Sahrens */ 1020789Sahrens static void 10211544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1022789Sahrens { 10231544Seschrock arc_state_t *old_state = ab->b_state; 10243700Sek110237 int64_t refcnt = refcount_count(&ab->b_refcnt); 10253700Sek110237 uint64_t from_delta, to_delta; 1026789Sahrens 1027789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 10281544Seschrock ASSERT(new_state != old_state); 10291544Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 10301544Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 10311544Seschrock 10321544Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 1033789Sahrens 1034789Sahrens /* 1035789Sahrens * If this buffer is evictable, transfer it from the 1036789Sahrens * old state list to the new state list. 1037789Sahrens */ 10381544Seschrock if (refcnt == 0) { 10393403Sbmc if (old_state != arc_anon) { 10403403Sbmc int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx); 10414309Smaybee uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 10421544Seschrock 10431544Seschrock if (use_mutex) 10443403Sbmc mutex_enter(&old_state->arcs_mtx); 10451544Seschrock 10461544Seschrock ASSERT(list_link_active(&ab->b_arc_node)); 10474309Smaybee list_remove(&old_state->arcs_list[ab->b_type], ab); 1048789Sahrens 10492391Smaybee /* 10502391Smaybee * If prefetching out of the ghost cache, 10512391Smaybee * we will have a non-null datacnt. 10522391Smaybee */ 10532391Smaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 10542391Smaybee /* ghost elements have a ghost size */ 10551544Seschrock ASSERT(ab->b_buf == NULL); 10561544Seschrock from_delta = ab->b_size; 1057789Sahrens } 10584309Smaybee ASSERT3U(*size, >=, from_delta); 10594309Smaybee atomic_add_64(size, -from_delta); 10601544Seschrock 10611544Seschrock if (use_mutex) 10623403Sbmc mutex_exit(&old_state->arcs_mtx); 1063789Sahrens } 10643403Sbmc if (new_state != arc_anon) { 10653403Sbmc int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx); 10664309Smaybee uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1067789Sahrens 10681544Seschrock if (use_mutex) 10693403Sbmc mutex_enter(&new_state->arcs_mtx); 10701544Seschrock 10714309Smaybee list_insert_head(&new_state->arcs_list[ab->b_type], ab); 10721544Seschrock 10731544Seschrock /* ghost elements have a ghost size */ 10741544Seschrock if (GHOST_STATE(new_state)) { 10751544Seschrock ASSERT(ab->b_datacnt == 0); 10761544Seschrock ASSERT(ab->b_buf == NULL); 10771544Seschrock to_delta = ab->b_size; 10781544Seschrock } 10794309Smaybee atomic_add_64(size, to_delta); 10801544Seschrock 10811544Seschrock if (use_mutex) 10823403Sbmc mutex_exit(&new_state->arcs_mtx); 1083789Sahrens } 1084789Sahrens } 1085789Sahrens 1086789Sahrens ASSERT(!BUF_EMPTY(ab)); 10875450Sbrendan if (new_state == arc_anon) { 1088789Sahrens buf_hash_remove(ab); 1089789Sahrens } 1090789Sahrens 10911544Seschrock /* adjust state sizes */ 10921544Seschrock if (to_delta) 10933403Sbmc atomic_add_64(&new_state->arcs_size, to_delta); 10941544Seschrock if (from_delta) { 10953403Sbmc ASSERT3U(old_state->arcs_size, >=, from_delta); 10963403Sbmc atomic_add_64(&old_state->arcs_size, -from_delta); 1097789Sahrens } 1098789Sahrens ab->b_state = new_state; 10995450Sbrendan 11005450Sbrendan /* adjust l2arc hdr stats */ 11015450Sbrendan if (new_state == arc_l2c_only) 11025450Sbrendan l2arc_hdr_stat_add(); 11035450Sbrendan else if (old_state == arc_l2c_only) 11045450Sbrendan l2arc_hdr_stat_remove(); 1105789Sahrens } 1106789Sahrens 11074309Smaybee void 11088582SBrendan.Gregg@Sun.COM arc_space_consume(uint64_t space, arc_space_type_t type) 11094309Smaybee { 11108582SBrendan.Gregg@Sun.COM ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11118582SBrendan.Gregg@Sun.COM 11128582SBrendan.Gregg@Sun.COM switch (type) { 11138582SBrendan.Gregg@Sun.COM case ARC_SPACE_DATA: 11148582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, space); 11158582SBrendan.Gregg@Sun.COM break; 11168582SBrendan.Gregg@Sun.COM case ARC_SPACE_OTHER: 11178582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_other_size, space); 11188582SBrendan.Gregg@Sun.COM break; 11198582SBrendan.Gregg@Sun.COM case ARC_SPACE_HDRS: 11208582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_hdr_size, space); 11218582SBrendan.Gregg@Sun.COM break; 11228582SBrendan.Gregg@Sun.COM case ARC_SPACE_L2HDRS: 11238582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_hdr_size, space); 11248582SBrendan.Gregg@Sun.COM break; 11258582SBrendan.Gregg@Sun.COM } 11268582SBrendan.Gregg@Sun.COM 11274309Smaybee atomic_add_64(&arc_meta_used, space); 11284309Smaybee atomic_add_64(&arc_size, space); 11294309Smaybee } 11304309Smaybee 11314309Smaybee void 11328582SBrendan.Gregg@Sun.COM arc_space_return(uint64_t space, arc_space_type_t type) 11334309Smaybee { 11348582SBrendan.Gregg@Sun.COM ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11358582SBrendan.Gregg@Sun.COM 11368582SBrendan.Gregg@Sun.COM switch (type) { 11378582SBrendan.Gregg@Sun.COM case ARC_SPACE_DATA: 11388582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -space); 11398582SBrendan.Gregg@Sun.COM break; 11408582SBrendan.Gregg@Sun.COM case ARC_SPACE_OTHER: 11418582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_other_size, -space); 11428582SBrendan.Gregg@Sun.COM break; 11438582SBrendan.Gregg@Sun.COM case ARC_SPACE_HDRS: 11448582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_hdr_size, -space); 11458582SBrendan.Gregg@Sun.COM break; 11468582SBrendan.Gregg@Sun.COM case ARC_SPACE_L2HDRS: 11478582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 11488582SBrendan.Gregg@Sun.COM break; 11498582SBrendan.Gregg@Sun.COM } 11508582SBrendan.Gregg@Sun.COM 11514309Smaybee ASSERT(arc_meta_used >= space); 11524309Smaybee if (arc_meta_max < arc_meta_used) 11534309Smaybee arc_meta_max = arc_meta_used; 11544309Smaybee atomic_add_64(&arc_meta_used, -space); 11554309Smaybee ASSERT(arc_size >= space); 11564309Smaybee atomic_add_64(&arc_size, -space); 11574309Smaybee } 11584309Smaybee 11594309Smaybee void * 11604309Smaybee arc_data_buf_alloc(uint64_t size) 11614309Smaybee { 11624309Smaybee if (arc_evict_needed(ARC_BUFC_DATA)) 11634309Smaybee cv_signal(&arc_reclaim_thr_cv); 11644309Smaybee atomic_add_64(&arc_size, size); 11654309Smaybee return (zio_data_buf_alloc(size)); 11664309Smaybee } 11674309Smaybee 11684309Smaybee void 11694309Smaybee arc_data_buf_free(void *buf, uint64_t size) 11704309Smaybee { 11714309Smaybee zio_data_buf_free(buf, size); 11724309Smaybee ASSERT(arc_size >= size); 11734309Smaybee atomic_add_64(&arc_size, -size); 11744309Smaybee } 11754309Smaybee 1176789Sahrens arc_buf_t * 11773290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1178789Sahrens { 1179789Sahrens arc_buf_hdr_t *hdr; 1180789Sahrens arc_buf_t *buf; 1181789Sahrens 1182789Sahrens ASSERT3U(size, >, 0); 11836245Smaybee hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1184789Sahrens ASSERT(BUF_EMPTY(hdr)); 1185789Sahrens hdr->b_size = size; 11863290Sjohansen hdr->b_type = type; 11878636SMark.Maybee@Sun.COM hdr->b_spa = spa_guid(spa); 11883403Sbmc hdr->b_state = arc_anon; 1189789Sahrens hdr->b_arc_access = 0; 11906245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1191789Sahrens buf->b_hdr = hdr; 11922688Smaybee buf->b_data = NULL; 11931544Seschrock buf->b_efunc = NULL; 11941544Seschrock buf->b_private = NULL; 1195789Sahrens buf->b_next = NULL; 1196789Sahrens hdr->b_buf = buf; 11972688Smaybee arc_get_data_buf(buf); 11981544Seschrock hdr->b_datacnt = 1; 1199789Sahrens hdr->b_flags = 0; 1200789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1201789Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 1202789Sahrens 1203789Sahrens return (buf); 1204789Sahrens } 1205789Sahrens 12062688Smaybee static arc_buf_t * 12072688Smaybee arc_buf_clone(arc_buf_t *from) 12081544Seschrock { 12092688Smaybee arc_buf_t *buf; 12102688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 12112688Smaybee uint64_t size = hdr->b_size; 12121544Seschrock 12136245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 12142688Smaybee buf->b_hdr = hdr; 12152688Smaybee buf->b_data = NULL; 12162688Smaybee buf->b_efunc = NULL; 12172688Smaybee buf->b_private = NULL; 12182688Smaybee buf->b_next = hdr->b_buf; 12192688Smaybee hdr->b_buf = buf; 12202688Smaybee arc_get_data_buf(buf); 12212688Smaybee bcopy(from->b_data, buf->b_data, size); 12222688Smaybee hdr->b_datacnt += 1; 12232688Smaybee return (buf); 12241544Seschrock } 12251544Seschrock 12261544Seschrock void 12271544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 12281544Seschrock { 12292887Smaybee arc_buf_hdr_t *hdr; 12301544Seschrock kmutex_t *hash_lock; 12311544Seschrock 12322724Smaybee /* 12337545SMark.Maybee@Sun.COM * Check to see if this buffer is evicted. Callers 12347545SMark.Maybee@Sun.COM * must verify b_data != NULL to know if the add_ref 12357545SMark.Maybee@Sun.COM * was successful. 12362724Smaybee */ 12377545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 12387545SMark.Maybee@Sun.COM if (buf->b_data == NULL) { 12397545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 12402724Smaybee return; 12412887Smaybee } 12427545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 12437545SMark.Maybee@Sun.COM ASSERT(hdr != NULL); 12442887Smaybee hash_lock = HDR_LOCK(hdr); 12452724Smaybee mutex_enter(hash_lock); 12467545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 12477545SMark.Maybee@Sun.COM 12483403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 12491544Seschrock add_reference(hdr, hash_lock, tag); 12508582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 12512688Smaybee arc_access(hdr, hash_lock); 12522688Smaybee mutex_exit(hash_lock); 12533403Sbmc ARCSTAT_BUMP(arcstat_hits); 12543403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 12553403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 12563403Sbmc data, metadata, hits); 12571544Seschrock } 12581544Seschrock 12595450Sbrendan /* 12605450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 12615450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 12625450Sbrendan */ 12635450Sbrendan static void 12645450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 12655450Sbrendan void *data, size_t size) 12665450Sbrendan { 12675450Sbrendan if (HDR_L2_WRITING(hdr)) { 12685450Sbrendan l2arc_data_free_t *df; 12695450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 12705450Sbrendan df->l2df_data = data; 12715450Sbrendan df->l2df_size = size; 12725450Sbrendan df->l2df_func = free_func; 12735450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 12745450Sbrendan list_insert_head(l2arc_free_on_write, df); 12755450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 12765450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 12775450Sbrendan } else { 12785450Sbrendan free_func(data, size); 12795450Sbrendan } 12805450Sbrendan } 12815450Sbrendan 1282789Sahrens static void 12832688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 12841544Seschrock { 12851544Seschrock arc_buf_t **bufp; 12861544Seschrock 12871544Seschrock /* free up data associated with the buf */ 12881544Seschrock if (buf->b_data) { 12891544Seschrock arc_state_t *state = buf->b_hdr->b_state; 12901544Seschrock uint64_t size = buf->b_hdr->b_size; 12913290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 12921544Seschrock 12933093Sahrens arc_cksum_verify(buf); 12942688Smaybee if (!recycle) { 12953290Sjohansen if (type == ARC_BUFC_METADATA) { 12965450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 12975450Sbrendan buf->b_data, size); 12988582SBrendan.Gregg@Sun.COM arc_space_return(size, ARC_SPACE_DATA); 12993290Sjohansen } else { 13003290Sjohansen ASSERT(type == ARC_BUFC_DATA); 13015450Sbrendan arc_buf_data_free(buf->b_hdr, 13025450Sbrendan zio_data_buf_free, buf->b_data, size); 13038582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, -size); 13044309Smaybee atomic_add_64(&arc_size, -size); 13053290Sjohansen } 13062688Smaybee } 13071544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 13084309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 13094309Smaybee 13101544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 13113403Sbmc ASSERT(state != arc_anon); 13124309Smaybee 13134309Smaybee ASSERT3U(*cnt, >=, size); 13144309Smaybee atomic_add_64(cnt, -size); 13151544Seschrock } 13163403Sbmc ASSERT3U(state->arcs_size, >=, size); 13173403Sbmc atomic_add_64(&state->arcs_size, -size); 13181544Seschrock buf->b_data = NULL; 13191544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 13201544Seschrock buf->b_hdr->b_datacnt -= 1; 13211544Seschrock } 13221544Seschrock 13231544Seschrock /* only remove the buf if requested */ 13241544Seschrock if (!all) 13251544Seschrock return; 13261544Seschrock 13271544Seschrock /* remove the buf from the hdr list */ 13281544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 13291544Seschrock continue; 13301544Seschrock *bufp = buf->b_next; 13311544Seschrock 13321544Seschrock ASSERT(buf->b_efunc == NULL); 13331544Seschrock 13341544Seschrock /* clean up the buf */ 13351544Seschrock buf->b_hdr = NULL; 13361544Seschrock kmem_cache_free(buf_cache, buf); 13371544Seschrock } 13381544Seschrock 13391544Seschrock static void 13401544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1341789Sahrens { 1342789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 13433403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 13441544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 13457046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 1346789Sahrens 13475450Sbrendan if (hdr->b_l2hdr != NULL) { 13485450Sbrendan if (!MUTEX_HELD(&l2arc_buflist_mtx)) { 13495450Sbrendan /* 13505450Sbrendan * To prevent arc_free() and l2arc_evict() from 13515450Sbrendan * attempting to free the same buffer at the same time, 13525450Sbrendan * a FREE_IN_PROGRESS flag is given to arc_free() to 13535450Sbrendan * give it priority. l2arc_evict() can't destroy this 13545450Sbrendan * header while we are waiting on l2arc_buflist_mtx. 13557361SBrendan.Gregg@Sun.COM * 13567361SBrendan.Gregg@Sun.COM * The hdr may be removed from l2ad_buflist before we 13577361SBrendan.Gregg@Sun.COM * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 13585450Sbrendan */ 13595450Sbrendan mutex_enter(&l2arc_buflist_mtx); 13607361SBrendan.Gregg@Sun.COM if (hdr->b_l2hdr != NULL) { 13617361SBrendan.Gregg@Sun.COM list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, 13627361SBrendan.Gregg@Sun.COM hdr); 13637361SBrendan.Gregg@Sun.COM } 13645450Sbrendan mutex_exit(&l2arc_buflist_mtx); 13655450Sbrendan } else { 13665450Sbrendan list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr); 13675450Sbrendan } 13685450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 13695450Sbrendan kmem_free(hdr->b_l2hdr, sizeof (l2arc_buf_hdr_t)); 13705450Sbrendan if (hdr->b_state == arc_l2c_only) 13715450Sbrendan l2arc_hdr_stat_remove(); 13725450Sbrendan hdr->b_l2hdr = NULL; 13735450Sbrendan } 13745450Sbrendan 1375789Sahrens if (!BUF_EMPTY(hdr)) { 13761544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1377789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1378789Sahrens hdr->b_birth = 0; 1379789Sahrens hdr->b_cksum0 = 0; 1380789Sahrens } 13811544Seschrock while (hdr->b_buf) { 1382789Sahrens arc_buf_t *buf = hdr->b_buf; 1383789Sahrens 13841544Seschrock if (buf->b_efunc) { 13851544Seschrock mutex_enter(&arc_eviction_mtx); 13867545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 13871544Seschrock ASSERT(buf->b_hdr != NULL); 13882688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 13891544Seschrock hdr->b_buf = buf->b_next; 13902887Smaybee buf->b_hdr = &arc_eviction_hdr; 13911544Seschrock buf->b_next = arc_eviction_list; 13921544Seschrock arc_eviction_list = buf; 13937545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 13941544Seschrock mutex_exit(&arc_eviction_mtx); 13951544Seschrock } else { 13962688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 13971544Seschrock } 1398789Sahrens } 13993093Sahrens if (hdr->b_freeze_cksum != NULL) { 14003093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 14013093Sahrens hdr->b_freeze_cksum = NULL; 14023093Sahrens } 14031544Seschrock 1404789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1405789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1406789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1407789Sahrens kmem_cache_free(hdr_cache, hdr); 1408789Sahrens } 1409789Sahrens 1410789Sahrens void 1411789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1412789Sahrens { 1413789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 14143403Sbmc int hashed = hdr->b_state != arc_anon; 14151544Seschrock 14161544Seschrock ASSERT(buf->b_efunc == NULL); 14171544Seschrock ASSERT(buf->b_data != NULL); 14181544Seschrock 14191544Seschrock if (hashed) { 14201544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 14211544Seschrock 14221544Seschrock mutex_enter(hash_lock); 14231544Seschrock (void) remove_reference(hdr, hash_lock, tag); 14241544Seschrock if (hdr->b_datacnt > 1) 14252688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14261544Seschrock else 14271544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 14281544Seschrock mutex_exit(hash_lock); 14291544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 14301544Seschrock int destroy_hdr; 14311544Seschrock /* 14321544Seschrock * We are in the middle of an async write. Don't destroy 14331544Seschrock * this buffer unless the write completes before we finish 14341544Seschrock * decrementing the reference count. 14351544Seschrock */ 14361544Seschrock mutex_enter(&arc_eviction_mtx); 14371544Seschrock (void) remove_reference(hdr, NULL, tag); 14381544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 14391544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 14401544Seschrock mutex_exit(&arc_eviction_mtx); 14411544Seschrock if (destroy_hdr) 14421544Seschrock arc_hdr_destroy(hdr); 14431544Seschrock } else { 14441544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 14451544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 14462688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14471544Seschrock } else { 14481544Seschrock arc_hdr_destroy(hdr); 14491544Seschrock } 14501544Seschrock } 14511544Seschrock } 14521544Seschrock 14531544Seschrock int 14541544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 14551544Seschrock { 14561544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1457789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 14581544Seschrock int no_callback = (buf->b_efunc == NULL); 14591544Seschrock 14603403Sbmc if (hdr->b_state == arc_anon) { 14611544Seschrock arc_buf_free(buf, tag); 14621544Seschrock return (no_callback); 14631544Seschrock } 1464789Sahrens 1465789Sahrens mutex_enter(hash_lock); 14663403Sbmc ASSERT(hdr->b_state != arc_anon); 14671544Seschrock ASSERT(buf->b_data != NULL); 1468789Sahrens 14691544Seschrock (void) remove_reference(hdr, hash_lock, tag); 14701544Seschrock if (hdr->b_datacnt > 1) { 14711544Seschrock if (no_callback) 14722688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14731544Seschrock } else if (no_callback) { 14741544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 14751544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1476789Sahrens } 14771544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 14781544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1479789Sahrens mutex_exit(hash_lock); 14801544Seschrock return (no_callback); 1481789Sahrens } 1482789Sahrens 1483789Sahrens int 1484789Sahrens arc_buf_size(arc_buf_t *buf) 1485789Sahrens { 1486789Sahrens return (buf->b_hdr->b_size); 1487789Sahrens } 1488789Sahrens 1489789Sahrens /* 1490789Sahrens * Evict buffers from list until we've removed the specified number of 1491789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 14922688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 14932688Smaybee * - look for a buffer to evict that is `bytes' long. 14942688Smaybee * - return the data block from this buffer rather than freeing it. 14952688Smaybee * This flag is used by callers that are trying to make space for a 14962688Smaybee * new buffer in a full arc cache. 14975642Smaybee * 14985642Smaybee * This function makes a "best effort". It skips over any buffers 14995642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 15005642Smaybee * It may also return without evicting as much space as requested. 1501789Sahrens */ 15022688Smaybee static void * 15038636SMark.Maybee@Sun.COM arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 15043290Sjohansen arc_buf_contents_t type) 1505789Sahrens { 1506789Sahrens arc_state_t *evicted_state; 15072688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 15082918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 15094309Smaybee list_t *list = &state->arcs_list[type]; 1510789Sahrens kmutex_t *hash_lock; 15112688Smaybee boolean_t have_lock; 15122918Smaybee void *stolen = NULL; 1513789Sahrens 15143403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1515789Sahrens 15163403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1517789Sahrens 15183403Sbmc mutex_enter(&state->arcs_mtx); 15193403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1520789Sahrens 15214309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15224309Smaybee ab_prev = list_prev(list, ab); 15232391Smaybee /* prefetch buffers have a minimum lifespan */ 15242688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 15255642Smaybee (spa && ab->b_spa != spa) || 15262688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 15272688Smaybee lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) { 15282391Smaybee skipped++; 15292391Smaybee continue; 15302391Smaybee } 15312918Smaybee /* "lookahead" for better eviction candidate */ 15322918Smaybee if (recycle && ab->b_size != bytes && 15332918Smaybee ab_prev && ab_prev->b_size == bytes) 15342688Smaybee continue; 1535789Sahrens hash_lock = HDR_LOCK(ab); 15362688Smaybee have_lock = MUTEX_HELD(hash_lock); 15372688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1538789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 15391544Seschrock ASSERT(ab->b_datacnt > 0); 15401544Seschrock while (ab->b_buf) { 15411544Seschrock arc_buf_t *buf = ab->b_buf; 15427545SMark.Maybee@Sun.COM if (!rw_tryenter(&buf->b_lock, RW_WRITER)) { 15437545SMark.Maybee@Sun.COM missed += 1; 15447545SMark.Maybee@Sun.COM break; 15457545SMark.Maybee@Sun.COM } 15462688Smaybee if (buf->b_data) { 15471544Seschrock bytes_evicted += ab->b_size; 15483290Sjohansen if (recycle && ab->b_type == type && 15495450Sbrendan ab->b_size == bytes && 15505450Sbrendan !HDR_L2_WRITING(ab)) { 15512918Smaybee stolen = buf->b_data; 15522918Smaybee recycle = FALSE; 15532918Smaybee } 15542688Smaybee } 15551544Seschrock if (buf->b_efunc) { 15561544Seschrock mutex_enter(&arc_eviction_mtx); 15572918Smaybee arc_buf_destroy(buf, 15582918Smaybee buf->b_data == stolen, FALSE); 15591544Seschrock ab->b_buf = buf->b_next; 15602887Smaybee buf->b_hdr = &arc_eviction_hdr; 15611544Seschrock buf->b_next = arc_eviction_list; 15621544Seschrock arc_eviction_list = buf; 15631544Seschrock mutex_exit(&arc_eviction_mtx); 15647545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 15651544Seschrock } else { 15667545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 15672918Smaybee arc_buf_destroy(buf, 15682918Smaybee buf->b_data == stolen, TRUE); 15691544Seschrock } 15701544Seschrock } 15717545SMark.Maybee@Sun.COM if (ab->b_datacnt == 0) { 15727545SMark.Maybee@Sun.COM arc_change_state(evicted_state, ab, hash_lock); 15737545SMark.Maybee@Sun.COM ASSERT(HDR_IN_HASH_TABLE(ab)); 15747545SMark.Maybee@Sun.COM ab->b_flags |= ARC_IN_HASH_TABLE; 15757545SMark.Maybee@Sun.COM ab->b_flags &= ~ARC_BUF_AVAILABLE; 15767545SMark.Maybee@Sun.COM DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 15777545SMark.Maybee@Sun.COM } 15782688Smaybee if (!have_lock) 15792688Smaybee mutex_exit(hash_lock); 15801544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1581789Sahrens break; 1582789Sahrens } else { 15832688Smaybee missed += 1; 1584789Sahrens } 1585789Sahrens } 15863403Sbmc 15873403Sbmc mutex_exit(&evicted_state->arcs_mtx); 15883403Sbmc mutex_exit(&state->arcs_mtx); 1589789Sahrens 1590789Sahrens if (bytes_evicted < bytes) 1591789Sahrens dprintf("only evicted %lld bytes from %x", 1592789Sahrens (longlong_t)bytes_evicted, state); 1593789Sahrens 15942688Smaybee if (skipped) 15953403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 15963403Sbmc 15972688Smaybee if (missed) 15983403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 15993403Sbmc 16004709Smaybee /* 16014709Smaybee * We have just evicted some date into the ghost state, make 16024709Smaybee * sure we also adjust the ghost state size if necessary. 16034709Smaybee */ 16044709Smaybee if (arc_no_grow && 16054709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 16064709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 16074709Smaybee arc_mru_ghost->arcs_size - arc_c; 16084709Smaybee 16094709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 16104709Smaybee int64_t todelete = 16114709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 16125642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 16134709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 16144709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 16154709Smaybee arc_mru_ghost->arcs_size + 16164709Smaybee arc_mfu_ghost->arcs_size - arc_c); 16175642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 16184709Smaybee } 16194709Smaybee } 16204709Smaybee 16212918Smaybee return (stolen); 1622789Sahrens } 1623789Sahrens 1624789Sahrens /* 1625789Sahrens * Remove buffers from list until we've removed the specified number of 1626789Sahrens * bytes. Destroy the buffers that are removed. 1627789Sahrens */ 1628789Sahrens static void 16298636SMark.Maybee@Sun.COM arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 1630789Sahrens { 1631789Sahrens arc_buf_hdr_t *ab, *ab_prev; 16324309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1633789Sahrens kmutex_t *hash_lock; 16341544Seschrock uint64_t bytes_deleted = 0; 16353700Sek110237 uint64_t bufs_skipped = 0; 1636789Sahrens 16371544Seschrock ASSERT(GHOST_STATE(state)); 1638789Sahrens top: 16393403Sbmc mutex_enter(&state->arcs_mtx); 16404309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 16414309Smaybee ab_prev = list_prev(list, ab); 16425642Smaybee if (spa && ab->b_spa != spa) 16435642Smaybee continue; 1644789Sahrens hash_lock = HDR_LOCK(ab); 1645789Sahrens if (mutex_tryenter(hash_lock)) { 16462391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 16471544Seschrock ASSERT(ab->b_buf == NULL); 16483403Sbmc ARCSTAT_BUMP(arcstat_deleted); 16491544Seschrock bytes_deleted += ab->b_size; 16505450Sbrendan 16515450Sbrendan if (ab->b_l2hdr != NULL) { 16525450Sbrendan /* 16535450Sbrendan * This buffer is cached on the 2nd Level ARC; 16545450Sbrendan * don't destroy the header. 16555450Sbrendan */ 16565450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 16575450Sbrendan mutex_exit(hash_lock); 16585450Sbrendan } else { 16595450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 16605450Sbrendan mutex_exit(hash_lock); 16615450Sbrendan arc_hdr_destroy(ab); 16625450Sbrendan } 16635450Sbrendan 1664789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1665789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1666789Sahrens break; 1667789Sahrens } else { 1668789Sahrens if (bytes < 0) { 16693403Sbmc mutex_exit(&state->arcs_mtx); 1670789Sahrens mutex_enter(hash_lock); 1671789Sahrens mutex_exit(hash_lock); 1672789Sahrens goto top; 1673789Sahrens } 1674789Sahrens bufs_skipped += 1; 1675789Sahrens } 1676789Sahrens } 16773403Sbmc mutex_exit(&state->arcs_mtx); 1678789Sahrens 16794309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 16804309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 16814309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 16824309Smaybee goto top; 16834309Smaybee } 16844309Smaybee 1685789Sahrens if (bufs_skipped) { 16863403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1687789Sahrens ASSERT(bytes >= 0); 1688789Sahrens } 1689789Sahrens 1690789Sahrens if (bytes_deleted < bytes) 1691789Sahrens dprintf("only deleted %lld bytes from %p", 1692789Sahrens (longlong_t)bytes_deleted, state); 1693789Sahrens } 1694789Sahrens 1695789Sahrens static void 1696789Sahrens arc_adjust(void) 1697789Sahrens { 16988582SBrendan.Gregg@Sun.COM int64_t adjustment, delta; 16998582SBrendan.Gregg@Sun.COM 17008582SBrendan.Gregg@Sun.COM /* 17018582SBrendan.Gregg@Sun.COM * Adjust MRU size 17028582SBrendan.Gregg@Sun.COM */ 17038582SBrendan.Gregg@Sun.COM 17048582SBrendan.Gregg@Sun.COM adjustment = MIN(arc_size - arc_c, 17058582SBrendan.Gregg@Sun.COM arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - arc_p); 17068582SBrendan.Gregg@Sun.COM 17078582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 17088582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 17098582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA); 17108582SBrendan.Gregg@Sun.COM adjustment -= delta; 17114309Smaybee } 17124309Smaybee 17138582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 17148582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 17158582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mru, NULL, delta, FALSE, 17165642Smaybee ARC_BUFC_METADATA); 1717789Sahrens } 1718789Sahrens 17198582SBrendan.Gregg@Sun.COM /* 17208582SBrendan.Gregg@Sun.COM * Adjust MFU size 17218582SBrendan.Gregg@Sun.COM */ 17228582SBrendan.Gregg@Sun.COM 17238582SBrendan.Gregg@Sun.COM adjustment = arc_size - arc_c; 17248582SBrendan.Gregg@Sun.COM 17258582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 17268582SBrendan.Gregg@Sun.COM delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 17278582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA); 17288582SBrendan.Gregg@Sun.COM adjustment -= delta; 17298582SBrendan.Gregg@Sun.COM } 17308582SBrendan.Gregg@Sun.COM 17318582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 17328582SBrendan.Gregg@Sun.COM int64_t delta = MIN(adjustment, 17338582SBrendan.Gregg@Sun.COM arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 17348582SBrendan.Gregg@Sun.COM (void) arc_evict(arc_mfu, NULL, delta, FALSE, 17358582SBrendan.Gregg@Sun.COM ARC_BUFC_METADATA); 17368582SBrendan.Gregg@Sun.COM } 17378582SBrendan.Gregg@Sun.COM 17388582SBrendan.Gregg@Sun.COM /* 17398582SBrendan.Gregg@Sun.COM * Adjust ghost lists 17408582SBrendan.Gregg@Sun.COM */ 17418582SBrendan.Gregg@Sun.COM 17428582SBrendan.Gregg@Sun.COM adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 17438582SBrendan.Gregg@Sun.COM 17448582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 17458582SBrendan.Gregg@Sun.COM delta = MIN(arc_mru_ghost->arcs_size, adjustment); 17468582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mru_ghost, NULL, delta); 17478582SBrendan.Gregg@Sun.COM } 17488582SBrendan.Gregg@Sun.COM 17498582SBrendan.Gregg@Sun.COM adjustment = 17508582SBrendan.Gregg@Sun.COM arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 17518582SBrendan.Gregg@Sun.COM 17528582SBrendan.Gregg@Sun.COM if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 17538582SBrendan.Gregg@Sun.COM delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 17548582SBrendan.Gregg@Sun.COM arc_evict_ghost(arc_mfu_ghost, NULL, delta); 1755789Sahrens } 1756789Sahrens } 1757789Sahrens 17581544Seschrock static void 17591544Seschrock arc_do_user_evicts(void) 17601544Seschrock { 17611544Seschrock mutex_enter(&arc_eviction_mtx); 17621544Seschrock while (arc_eviction_list != NULL) { 17631544Seschrock arc_buf_t *buf = arc_eviction_list; 17641544Seschrock arc_eviction_list = buf->b_next; 17657545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 17661544Seschrock buf->b_hdr = NULL; 17677545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 17681544Seschrock mutex_exit(&arc_eviction_mtx); 17691544Seschrock 17701819Smaybee if (buf->b_efunc != NULL) 17711819Smaybee VERIFY(buf->b_efunc(buf) == 0); 17721544Seschrock 17731544Seschrock buf->b_efunc = NULL; 17741544Seschrock buf->b_private = NULL; 17751544Seschrock kmem_cache_free(buf_cache, buf); 17761544Seschrock mutex_enter(&arc_eviction_mtx); 17771544Seschrock } 17781544Seschrock mutex_exit(&arc_eviction_mtx); 17791544Seschrock } 17801544Seschrock 1781789Sahrens /* 17825642Smaybee * Flush all *evictable* data from the cache for the given spa. 1783789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1784789Sahrens */ 1785789Sahrens void 17865642Smaybee arc_flush(spa_t *spa) 1787789Sahrens { 17888636SMark.Maybee@Sun.COM uint64_t guid = 0; 17898636SMark.Maybee@Sun.COM 17908636SMark.Maybee@Sun.COM if (spa) 17918636SMark.Maybee@Sun.COM guid = spa_guid(spa); 17928636SMark.Maybee@Sun.COM 17935642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 17948636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 17955642Smaybee if (spa) 17965642Smaybee break; 17975642Smaybee } 17985642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 17998636SMark.Maybee@Sun.COM (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 18005642Smaybee if (spa) 18015642Smaybee break; 18025642Smaybee } 18035642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 18048636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 18055642Smaybee if (spa) 18065642Smaybee break; 18075642Smaybee } 18085642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 18098636SMark.Maybee@Sun.COM (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 18105642Smaybee if (spa) 18115642Smaybee break; 18125642Smaybee } 18135642Smaybee 18148636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mru_ghost, guid, -1); 18158636SMark.Maybee@Sun.COM arc_evict_ghost(arc_mfu_ghost, guid, -1); 18161544Seschrock 18171544Seschrock mutex_enter(&arc_reclaim_thr_lock); 18181544Seschrock arc_do_user_evicts(); 18191544Seschrock mutex_exit(&arc_reclaim_thr_lock); 18205642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1821789Sahrens } 1822789Sahrens 1823789Sahrens void 18243158Smaybee arc_shrink(void) 1825789Sahrens { 18263403Sbmc if (arc_c > arc_c_min) { 18273158Smaybee uint64_t to_free; 1828789Sahrens 18292048Sstans #ifdef _KERNEL 18303403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 18312048Sstans #else 18323403Sbmc to_free = arc_c >> arc_shrink_shift; 18332048Sstans #endif 18343403Sbmc if (arc_c > arc_c_min + to_free) 18353403Sbmc atomic_add_64(&arc_c, -to_free); 18363158Smaybee else 18373403Sbmc arc_c = arc_c_min; 18382048Sstans 18393403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 18403403Sbmc if (arc_c > arc_size) 18413403Sbmc arc_c = MAX(arc_size, arc_c_min); 18423403Sbmc if (arc_p > arc_c) 18433403Sbmc arc_p = (arc_c >> 1); 18443403Sbmc ASSERT(arc_c >= arc_c_min); 18453403Sbmc ASSERT((int64_t)arc_p >= 0); 18463158Smaybee } 1847789Sahrens 18483403Sbmc if (arc_size > arc_c) 18493158Smaybee arc_adjust(); 1850789Sahrens } 1851789Sahrens 1852789Sahrens static int 1853789Sahrens arc_reclaim_needed(void) 1854789Sahrens { 1855789Sahrens uint64_t extra; 1856789Sahrens 1857789Sahrens #ifdef _KERNEL 18582048Sstans 18592048Sstans if (needfree) 18602048Sstans return (1); 18612048Sstans 1862789Sahrens /* 1863789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1864789Sahrens */ 1865789Sahrens extra = desfree; 1866789Sahrens 1867789Sahrens /* 1868789Sahrens * check that we're out of range of the pageout scanner. It starts to 1869789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1870789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1871789Sahrens * number of needed free pages. We add extra pages here to make sure 1872789Sahrens * the scanner doesn't start up while we're freeing memory. 1873789Sahrens */ 1874789Sahrens if (freemem < lotsfree + needfree + extra) 1875789Sahrens return (1); 1876789Sahrens 1877789Sahrens /* 1878789Sahrens * check to make sure that swapfs has enough space so that anon 18795450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1880789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1881789Sahrens * swap pages. We also add a bit of extra here just to prevent 1882789Sahrens * circumstances from getting really dire. 1883789Sahrens */ 1884789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1885789Sahrens return (1); 1886789Sahrens 18871936Smaybee #if defined(__i386) 1888789Sahrens /* 1889789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1890789Sahrens * kernel heap space before we ever run out of available physical 1891789Sahrens * memory. Most checks of the size of the heap_area compare against 1892789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1893789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1894789Sahrens * which is so low that it's useless. In this comparison, we seek to 1895789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 18965450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1897789Sahrens * free) 1898789Sahrens */ 1899789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1900789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1901789Sahrens return (1); 1902789Sahrens #endif 1903789Sahrens 1904789Sahrens #else 1905789Sahrens if (spa_get_random(100) == 0) 1906789Sahrens return (1); 1907789Sahrens #endif 1908789Sahrens return (0); 1909789Sahrens } 1910789Sahrens 1911789Sahrens static void 1912789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1913789Sahrens { 1914789Sahrens size_t i; 1915789Sahrens kmem_cache_t *prev_cache = NULL; 19163290Sjohansen kmem_cache_t *prev_data_cache = NULL; 1917789Sahrens extern kmem_cache_t *zio_buf_cache[]; 19183290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 1919789Sahrens 19201484Sek110237 #ifdef _KERNEL 19214309Smaybee if (arc_meta_used >= arc_meta_limit) { 19224309Smaybee /* 19234309Smaybee * We are exceeding our meta-data cache limit. 19244309Smaybee * Purge some DNLC entries to release holds on meta-data. 19254309Smaybee */ 19264309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 19274309Smaybee } 19281936Smaybee #if defined(__i386) 19291936Smaybee /* 19301936Smaybee * Reclaim unused memory from all kmem caches. 19311936Smaybee */ 19321936Smaybee kmem_reap(); 19331936Smaybee #endif 19341484Sek110237 #endif 19351484Sek110237 1936789Sahrens /* 19375450Sbrendan * An aggressive reclamation will shrink the cache size as well as 19381544Seschrock * reap free buffers from the arc kmem caches. 1939789Sahrens */ 1940789Sahrens if (strat == ARC_RECLAIM_AGGR) 19413158Smaybee arc_shrink(); 1942789Sahrens 1943789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 1944789Sahrens if (zio_buf_cache[i] != prev_cache) { 1945789Sahrens prev_cache = zio_buf_cache[i]; 1946789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 1947789Sahrens } 19483290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 19493290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 19503290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 19513290Sjohansen } 1952789Sahrens } 19531544Seschrock kmem_cache_reap_now(buf_cache); 19541544Seschrock kmem_cache_reap_now(hdr_cache); 1955789Sahrens } 1956789Sahrens 1957789Sahrens static void 1958789Sahrens arc_reclaim_thread(void) 1959789Sahrens { 1960789Sahrens clock_t growtime = 0; 1961789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 1962789Sahrens callb_cpr_t cpr; 1963789Sahrens 1964789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 1965789Sahrens 1966789Sahrens mutex_enter(&arc_reclaim_thr_lock); 1967789Sahrens while (arc_thread_exit == 0) { 1968789Sahrens if (arc_reclaim_needed()) { 1969789Sahrens 19703403Sbmc if (arc_no_grow) { 1971789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 1972789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1973789Sahrens } else { 1974789Sahrens last_reclaim = ARC_RECLAIM_CONS; 1975789Sahrens } 1976789Sahrens } else { 19773403Sbmc arc_no_grow = TRUE; 1978789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1979789Sahrens membar_producer(); 1980789Sahrens } 1981789Sahrens 1982789Sahrens /* reset the growth delay for every reclaim */ 1983789Sahrens growtime = lbolt + (arc_grow_retry * hz); 1984789Sahrens 1985789Sahrens arc_kmem_reap_now(last_reclaim); 19866987Sbrendan arc_warm = B_TRUE; 1987789Sahrens 19884309Smaybee } else if (arc_no_grow && lbolt >= growtime) { 19893403Sbmc arc_no_grow = FALSE; 1990789Sahrens } 1991789Sahrens 19923403Sbmc if (2 * arc_c < arc_size + 19933403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 19943298Smaybee arc_adjust(); 19953298Smaybee 19961544Seschrock if (arc_eviction_list != NULL) 19971544Seschrock arc_do_user_evicts(); 19981544Seschrock 1999789Sahrens /* block until needed, or one second, whichever is shorter */ 2000789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 2001789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 2002789Sahrens &arc_reclaim_thr_lock, (lbolt + hz)); 2003789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2004789Sahrens } 2005789Sahrens 2006789Sahrens arc_thread_exit = 0; 2007789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 2008789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2009789Sahrens thread_exit(); 2010789Sahrens } 2011789Sahrens 20121544Seschrock /* 20131544Seschrock * Adapt arc info given the number of bytes we are trying to add and 20141544Seschrock * the state that we are comming from. This function is only called 20151544Seschrock * when we are adding new content to the cache. 20161544Seschrock */ 2017789Sahrens static void 20181544Seschrock arc_adapt(int bytes, arc_state_t *state) 2019789Sahrens { 20201544Seschrock int mult; 20218582SBrendan.Gregg@Sun.COM uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 20221544Seschrock 20235450Sbrendan if (state == arc_l2c_only) 20245450Sbrendan return; 20255450Sbrendan 20261544Seschrock ASSERT(bytes > 0); 2027789Sahrens /* 20281544Seschrock * Adapt the target size of the MRU list: 20291544Seschrock * - if we just hit in the MRU ghost list, then increase 20301544Seschrock * the target size of the MRU list. 20311544Seschrock * - if we just hit in the MFU ghost list, then increase 20321544Seschrock * the target size of the MFU list by decreasing the 20331544Seschrock * target size of the MRU list. 2034789Sahrens */ 20353403Sbmc if (state == arc_mru_ghost) { 20363403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 20373403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 20381544Seschrock 20398582SBrendan.Gregg@Sun.COM arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 20403403Sbmc } else if (state == arc_mfu_ghost) { 20418582SBrendan.Gregg@Sun.COM uint64_t delta; 20428582SBrendan.Gregg@Sun.COM 20433403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 20443403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 20451544Seschrock 20468582SBrendan.Gregg@Sun.COM delta = MIN(bytes * mult, arc_p); 20478582SBrendan.Gregg@Sun.COM arc_p = MAX(arc_p_min, arc_p - delta); 20481544Seschrock } 20493403Sbmc ASSERT((int64_t)arc_p >= 0); 2050789Sahrens 2051789Sahrens if (arc_reclaim_needed()) { 2052789Sahrens cv_signal(&arc_reclaim_thr_cv); 2053789Sahrens return; 2054789Sahrens } 2055789Sahrens 20563403Sbmc if (arc_no_grow) 2057789Sahrens return; 2058789Sahrens 20593403Sbmc if (arc_c >= arc_c_max) 20601544Seschrock return; 20611544Seschrock 2062789Sahrens /* 20631544Seschrock * If we're within (2 * maxblocksize) bytes of the target 20641544Seschrock * cache size, increment the target cache size 2065789Sahrens */ 20663403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 20673403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 20683403Sbmc if (arc_c > arc_c_max) 20693403Sbmc arc_c = arc_c_max; 20703403Sbmc else if (state == arc_anon) 20713403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 20723403Sbmc if (arc_p > arc_c) 20733403Sbmc arc_p = arc_c; 2074789Sahrens } 20753403Sbmc ASSERT((int64_t)arc_p >= 0); 2076789Sahrens } 2077789Sahrens 2078789Sahrens /* 20791544Seschrock * Check if the cache has reached its limits and eviction is required 20801544Seschrock * prior to insert. 2081789Sahrens */ 2082789Sahrens static int 20834309Smaybee arc_evict_needed(arc_buf_contents_t type) 2084789Sahrens { 20854309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 20864309Smaybee return (1); 20874309Smaybee 20884309Smaybee #ifdef _KERNEL 20894309Smaybee /* 20904309Smaybee * If zio data pages are being allocated out of a separate heap segment, 20914309Smaybee * then enforce that the size of available vmem for this area remains 20924309Smaybee * above about 1/32nd free. 20934309Smaybee */ 20944309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 20954309Smaybee vmem_size(zio_arena, VMEM_FREE) < 20964309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 20974309Smaybee return (1); 20984309Smaybee #endif 20994309Smaybee 2100789Sahrens if (arc_reclaim_needed()) 2101789Sahrens return (1); 2102789Sahrens 21033403Sbmc return (arc_size > arc_c); 2104789Sahrens } 2105789Sahrens 2106789Sahrens /* 21072688Smaybee * The buffer, supplied as the first argument, needs a data block. 21082688Smaybee * So, if we are at cache max, determine which cache should be victimized. 21092688Smaybee * We have the following cases: 2110789Sahrens * 21113403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2112789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2113789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2114789Sahrens * 21153403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2116789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2117789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2118789Sahrens * entries. 2119789Sahrens * 21203403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2121789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2122789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2123789Sahrens * the MFU side, so the MRU side needs to be victimized. 2124789Sahrens * 21253403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2126789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2127789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2128789Sahrens */ 2129789Sahrens static void 21302688Smaybee arc_get_data_buf(arc_buf_t *buf) 2131789Sahrens { 21323290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 21333290Sjohansen uint64_t size = buf->b_hdr->b_size; 21343290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 21352688Smaybee 21362688Smaybee arc_adapt(size, state); 2137789Sahrens 21382688Smaybee /* 21392688Smaybee * We have not yet reached cache maximum size, 21402688Smaybee * just allocate a new buffer. 21412688Smaybee */ 21424309Smaybee if (!arc_evict_needed(type)) { 21433290Sjohansen if (type == ARC_BUFC_METADATA) { 21443290Sjohansen buf->b_data = zio_buf_alloc(size); 21458582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 21463290Sjohansen } else { 21473290Sjohansen ASSERT(type == ARC_BUFC_DATA); 21483290Sjohansen buf->b_data = zio_data_buf_alloc(size); 21498582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 21504309Smaybee atomic_add_64(&arc_size, size); 21513290Sjohansen } 21522688Smaybee goto out; 21532688Smaybee } 21542688Smaybee 21552688Smaybee /* 21562688Smaybee * If we are prefetching from the mfu ghost list, this buffer 21572688Smaybee * will end up on the mru list; so steal space from there. 21582688Smaybee */ 21593403Sbmc if (state == arc_mfu_ghost) 21603403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 21613403Sbmc else if (state == arc_mru_ghost) 21623403Sbmc state = arc_mru; 2163789Sahrens 21643403Sbmc if (state == arc_mru || state == arc_anon) { 21653403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 21668582SBrendan.Gregg@Sun.COM state = (arc_mfu->arcs_lsize[type] >= size && 21674309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2168789Sahrens } else { 21692688Smaybee /* MFU cases */ 21703403Sbmc uint64_t mfu_space = arc_c - arc_p; 21718582SBrendan.Gregg@Sun.COM state = (arc_mru->arcs_lsize[type] >= size && 21724309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 21732688Smaybee } 21745642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 21753290Sjohansen if (type == ARC_BUFC_METADATA) { 21763290Sjohansen buf->b_data = zio_buf_alloc(size); 21778582SBrendan.Gregg@Sun.COM arc_space_consume(size, ARC_SPACE_DATA); 21783290Sjohansen } else { 21793290Sjohansen ASSERT(type == ARC_BUFC_DATA); 21803290Sjohansen buf->b_data = zio_data_buf_alloc(size); 21818582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_data_size, size); 21824309Smaybee atomic_add_64(&arc_size, size); 21833290Sjohansen } 21843403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 21852688Smaybee } 21862688Smaybee ASSERT(buf->b_data != NULL); 21872688Smaybee out: 21882688Smaybee /* 21892688Smaybee * Update the state size. Note that ghost states have a 21902688Smaybee * "ghost size" and so don't need to be updated. 21912688Smaybee */ 21922688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 21932688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 21942688Smaybee 21953403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 21962688Smaybee if (list_link_active(&hdr->b_arc_node)) { 21972688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 21984309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2199789Sahrens } 22003298Smaybee /* 22013298Smaybee * If we are growing the cache, and we are adding anonymous 22023403Sbmc * data, and we have outgrown arc_p, update arc_p 22033298Smaybee */ 22043403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 22053403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 22063403Sbmc arc_p = MIN(arc_c, arc_p + size); 2207789Sahrens } 2208789Sahrens } 2209789Sahrens 2210789Sahrens /* 2211789Sahrens * This routine is called whenever a buffer is accessed. 22121544Seschrock * NOTE: the hash lock is dropped in this function. 2213789Sahrens */ 2214789Sahrens static void 22152688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2216789Sahrens { 2217789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2218789Sahrens 22193403Sbmc if (buf->b_state == arc_anon) { 2220789Sahrens /* 2221789Sahrens * This buffer is not in the cache, and does not 2222789Sahrens * appear in our "ghost" list. Add the new buffer 2223789Sahrens * to the MRU state. 2224789Sahrens */ 2225789Sahrens 2226789Sahrens ASSERT(buf->b_arc_access == 0); 2227789Sahrens buf->b_arc_access = lbolt; 22281544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 22293403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2230789Sahrens 22313403Sbmc } else if (buf->b_state == arc_mru) { 2232789Sahrens /* 22332391Smaybee * If this buffer is here because of a prefetch, then either: 22342391Smaybee * - clear the flag if this is a "referencing" read 22352391Smaybee * (any subsequent access will bump this into the MFU state). 22362391Smaybee * or 22372391Smaybee * - move the buffer to the head of the list if this is 22382391Smaybee * another prefetch (to make it less likely to be evicted). 2239789Sahrens */ 2240789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 22412391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 22422391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 22432391Smaybee } else { 22442391Smaybee buf->b_flags &= ~ARC_PREFETCH; 22453403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 22462391Smaybee } 22472391Smaybee buf->b_arc_access = lbolt; 2248789Sahrens return; 2249789Sahrens } 2250789Sahrens 2251789Sahrens /* 2252789Sahrens * This buffer has been "accessed" only once so far, 2253789Sahrens * but it is still in the cache. Move it to the MFU 2254789Sahrens * state. 2255789Sahrens */ 2256789Sahrens if (lbolt > buf->b_arc_access + ARC_MINTIME) { 2257789Sahrens /* 2258789Sahrens * More than 125ms have passed since we 2259789Sahrens * instantiated this buffer. Move it to the 2260789Sahrens * most frequently used state. 2261789Sahrens */ 2262789Sahrens buf->b_arc_access = lbolt; 22631544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 22643403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2265789Sahrens } 22663403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 22673403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2268789Sahrens arc_state_t *new_state; 2269789Sahrens /* 2270789Sahrens * This buffer has been "accessed" recently, but 2271789Sahrens * was evicted from the cache. Move it to the 2272789Sahrens * MFU state. 2273789Sahrens */ 2274789Sahrens 2275789Sahrens if (buf->b_flags & ARC_PREFETCH) { 22763403Sbmc new_state = arc_mru; 22772391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 22782391Smaybee buf->b_flags &= ~ARC_PREFETCH; 22791544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2280789Sahrens } else { 22813403Sbmc new_state = arc_mfu; 22821544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2283789Sahrens } 2284789Sahrens 2285789Sahrens buf->b_arc_access = lbolt; 2286789Sahrens arc_change_state(new_state, buf, hash_lock); 2287789Sahrens 22883403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 22893403Sbmc } else if (buf->b_state == arc_mfu) { 2290789Sahrens /* 2291789Sahrens * This buffer has been accessed more than once and is 2292789Sahrens * still in the cache. Keep it in the MFU state. 2293789Sahrens * 22942391Smaybee * NOTE: an add_reference() that occurred when we did 22952391Smaybee * the arc_read() will have kicked this off the list. 22962391Smaybee * If it was a prefetch, we will explicitly move it to 22972391Smaybee * the head of the list now. 2298789Sahrens */ 22992391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 23002391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 23012391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 23022391Smaybee } 23033403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 23042391Smaybee buf->b_arc_access = lbolt; 23053403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 23063403Sbmc arc_state_t *new_state = arc_mfu; 2307789Sahrens /* 2308789Sahrens * This buffer has been accessed more than once but has 2309789Sahrens * been evicted from the cache. Move it back to the 2310789Sahrens * MFU state. 2311789Sahrens */ 2312789Sahrens 23132391Smaybee if (buf->b_flags & ARC_PREFETCH) { 23142391Smaybee /* 23152391Smaybee * This is a prefetch access... 23162391Smaybee * move this block back to the MRU state. 23172391Smaybee */ 23182391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 23193403Sbmc new_state = arc_mru; 23202391Smaybee } 23212391Smaybee 2322789Sahrens buf->b_arc_access = lbolt; 23231544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23242391Smaybee arc_change_state(new_state, buf, hash_lock); 2325789Sahrens 23263403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 23275450Sbrendan } else if (buf->b_state == arc_l2c_only) { 23285450Sbrendan /* 23295450Sbrendan * This buffer is on the 2nd Level ARC. 23305450Sbrendan */ 23315450Sbrendan 23325450Sbrendan buf->b_arc_access = lbolt; 23335450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 23345450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2335789Sahrens } else { 2336789Sahrens ASSERT(!"invalid arc state"); 2337789Sahrens } 2338789Sahrens } 2339789Sahrens 2340789Sahrens /* a generic arc_done_func_t which you can use */ 2341789Sahrens /* ARGSUSED */ 2342789Sahrens void 2343789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2344789Sahrens { 2345789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 23461544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2347789Sahrens } 2348789Sahrens 23494309Smaybee /* a generic arc_done_func_t */ 2350789Sahrens void 2351789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2352789Sahrens { 2353789Sahrens arc_buf_t **bufp = arg; 2354789Sahrens if (zio && zio->io_error) { 23551544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2356789Sahrens *bufp = NULL; 2357789Sahrens } else { 2358789Sahrens *bufp = buf; 2359789Sahrens } 2360789Sahrens } 2361789Sahrens 2362789Sahrens static void 2363789Sahrens arc_read_done(zio_t *zio) 2364789Sahrens { 23651589Smaybee arc_buf_hdr_t *hdr, *found; 2366789Sahrens arc_buf_t *buf; 2367789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2368789Sahrens kmutex_t *hash_lock; 2369789Sahrens arc_callback_t *callback_list, *acb; 2370789Sahrens int freeable = FALSE; 2371789Sahrens 2372789Sahrens buf = zio->io_private; 2373789Sahrens hdr = buf->b_hdr; 2374789Sahrens 23751589Smaybee /* 23761589Smaybee * The hdr was inserted into hash-table and removed from lists 23771589Smaybee * prior to starting I/O. We should find this header, since 23781589Smaybee * it's in the hash table, and it should be legit since it's 23791589Smaybee * not possible to evict it during the I/O. The only possible 23801589Smaybee * reason for it not to be found is if we were freed during the 23811589Smaybee * read. 23821589Smaybee */ 23838636SMark.Maybee@Sun.COM found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 23843093Sahrens &hash_lock); 2385789Sahrens 23861589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 23875450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 23885450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 23895450Sbrendan 23906987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 23915450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 23927237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2393789Sahrens 2394789Sahrens /* byteswap if necessary */ 2395789Sahrens callback_list = hdr->b_acb; 2396789Sahrens ASSERT(callback_list != NULL); 23977046Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 23987046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 23997046Sahrens byteswap_uint64_array : 24007046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 24017046Sahrens func(buf->b_data, hdr->b_size); 24027046Sahrens } 2403789Sahrens 24045450Sbrendan arc_cksum_compute(buf, B_FALSE); 24053093Sahrens 2406789Sahrens /* create copies of the data buffer for the callers */ 2407789Sahrens abuf = buf; 2408789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2409789Sahrens if (acb->acb_done) { 24102688Smaybee if (abuf == NULL) 24112688Smaybee abuf = arc_buf_clone(buf); 2412789Sahrens acb->acb_buf = abuf; 2413789Sahrens abuf = NULL; 2414789Sahrens } 2415789Sahrens } 2416789Sahrens hdr->b_acb = NULL; 2417789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 24181544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 24191544Seschrock if (abuf == buf) 24201544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 2421789Sahrens 2422789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2423789Sahrens 2424789Sahrens if (zio->io_error != 0) { 2425789Sahrens hdr->b_flags |= ARC_IO_ERROR; 24263403Sbmc if (hdr->b_state != arc_anon) 24273403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 24281544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 24291544Seschrock buf_hash_remove(hdr); 2430789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2431789Sahrens } 2432789Sahrens 24331544Seschrock /* 24342391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 24352391Smaybee * that the hdr (and hence the cv) might be freed before we get to 24362391Smaybee * the cv_broadcast(). 24371544Seschrock */ 24381544Seschrock cv_broadcast(&hdr->b_cv); 24391544Seschrock 24401589Smaybee if (hash_lock) { 2441789Sahrens /* 2442789Sahrens * Only call arc_access on anonymous buffers. This is because 2443789Sahrens * if we've issued an I/O for an evicted buffer, we've already 2444789Sahrens * called arc_access (to prevent any simultaneous readers from 2445789Sahrens * getting confused). 2446789Sahrens */ 24473403Sbmc if (zio->io_error == 0 && hdr->b_state == arc_anon) 24482688Smaybee arc_access(hdr, hash_lock); 24492688Smaybee mutex_exit(hash_lock); 2450789Sahrens } else { 2451789Sahrens /* 2452789Sahrens * This block was freed while we waited for the read to 2453789Sahrens * complete. It has been removed from the hash table and 2454789Sahrens * moved to the anonymous state (so that it won't show up 2455789Sahrens * in the cache). 2456789Sahrens */ 24573403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2458789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2459789Sahrens } 2460789Sahrens 2461789Sahrens /* execute each callback and free its structure */ 2462789Sahrens while ((acb = callback_list) != NULL) { 2463789Sahrens if (acb->acb_done) 2464789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2465789Sahrens 2466789Sahrens if (acb->acb_zio_dummy != NULL) { 2467789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2468789Sahrens zio_nowait(acb->acb_zio_dummy); 2469789Sahrens } 2470789Sahrens 2471789Sahrens callback_list = acb->acb_next; 2472789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2473789Sahrens } 2474789Sahrens 2475789Sahrens if (freeable) 24761544Seschrock arc_hdr_destroy(hdr); 2477789Sahrens } 2478789Sahrens 2479789Sahrens /* 2480789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2481789Sahrens * cache. If the block is found in the cache, invoke the provided 2482789Sahrens * callback immediately and return. Note that the `zio' parameter 2483789Sahrens * in the callback will be NULL in this case, since no IO was 2484789Sahrens * required. If the block is not in the cache pass the read request 2485789Sahrens * on to the spa with a substitute callback function, so that the 2486789Sahrens * requested block will be added to the cache. 2487789Sahrens * 2488789Sahrens * If a read request arrives for a block that has a read in-progress, 2489789Sahrens * either wait for the in-progress read to complete (and return the 2490789Sahrens * results); or, if this is a read with a "done" func, add a record 2491789Sahrens * to the read to invoke the "done" func when the read completes, 2492789Sahrens * and return; or just return. 2493789Sahrens * 2494789Sahrens * arc_read_done() will invoke all the requested "done" functions 2495789Sahrens * for readers of this block. 24967046Sahrens * 24977046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 24987046Sahrens * for the bp. But if you know you don't need locking, you can use 24998213SSuhasini.Peddada@Sun.COM * arc_read_bp. 2500789Sahrens */ 2501789Sahrens int 25027046Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_buf_t *pbuf, 25037237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 25047046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 25057046Sahrens { 25067046Sahrens int err; 25077265Sahrens arc_buf_hdr_t *hdr = pbuf->b_hdr; 25087046Sahrens 25097046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 25107046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 25117545SMark.Maybee@Sun.COM rw_enter(&pbuf->b_lock, RW_READER); 25127046Sahrens 25137046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 25147237Sek110237 zio_flags, arc_flags, zb); 25157046Sahrens 25167265Sahrens ASSERT3P(hdr, ==, pbuf->b_hdr); 25177545SMark.Maybee@Sun.COM rw_exit(&pbuf->b_lock); 25187046Sahrens return (err); 25197046Sahrens } 25207046Sahrens 25217046Sahrens int 25227046Sahrens arc_read_nolock(zio_t *pio, spa_t *spa, blkptr_t *bp, 25237237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 25247046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2525789Sahrens { 2526789Sahrens arc_buf_hdr_t *hdr; 2527789Sahrens arc_buf_t *buf; 2528789Sahrens kmutex_t *hash_lock; 25295450Sbrendan zio_t *rzio; 25308636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 2531789Sahrens 2532789Sahrens top: 25338636SMark.Maybee@Sun.COM hdr = buf_hash_find(guid, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 25341544Seschrock if (hdr && hdr->b_datacnt > 0) { 2535789Sahrens 25362391Smaybee *arc_flags |= ARC_CACHED; 25372391Smaybee 2538789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 25392391Smaybee 25402391Smaybee if (*arc_flags & ARC_WAIT) { 25412391Smaybee cv_wait(&hdr->b_cv, hash_lock); 25422391Smaybee mutex_exit(hash_lock); 25432391Smaybee goto top; 25442391Smaybee } 25452391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 25462391Smaybee 25472391Smaybee if (done) { 2548789Sahrens arc_callback_t *acb = NULL; 2549789Sahrens 2550789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2551789Sahrens KM_SLEEP); 2552789Sahrens acb->acb_done = done; 2553789Sahrens acb->acb_private = private; 2554789Sahrens if (pio != NULL) 2555789Sahrens acb->acb_zio_dummy = zio_null(pio, 25568632SBill.Moore@Sun.COM spa, NULL, NULL, NULL, zio_flags); 2557789Sahrens 2558789Sahrens ASSERT(acb->acb_done != NULL); 2559789Sahrens acb->acb_next = hdr->b_acb; 2560789Sahrens hdr->b_acb = acb; 2561789Sahrens add_reference(hdr, hash_lock, private); 2562789Sahrens mutex_exit(hash_lock); 2563789Sahrens return (0); 2564789Sahrens } 2565789Sahrens mutex_exit(hash_lock); 2566789Sahrens return (0); 2567789Sahrens } 2568789Sahrens 25693403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2570789Sahrens 25711544Seschrock if (done) { 25722688Smaybee add_reference(hdr, hash_lock, private); 25731544Seschrock /* 25741544Seschrock * If this block is already in use, create a new 25751544Seschrock * copy of the data so that we will be guaranteed 25761544Seschrock * that arc_release() will always succeed. 25771544Seschrock */ 25781544Seschrock buf = hdr->b_buf; 25791544Seschrock ASSERT(buf); 25801544Seschrock ASSERT(buf->b_data); 25812688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 25821544Seschrock ASSERT(buf->b_efunc == NULL); 25831544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 25842688Smaybee } else { 25852688Smaybee buf = arc_buf_clone(buf); 25861544Seschrock } 25872391Smaybee } else if (*arc_flags & ARC_PREFETCH && 25882391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 25892391Smaybee hdr->b_flags |= ARC_PREFETCH; 2590789Sahrens } 2591789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 25922688Smaybee arc_access(hdr, hash_lock); 25937237Sek110237 if (*arc_flags & ARC_L2CACHE) 25947237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25952688Smaybee mutex_exit(hash_lock); 25963403Sbmc ARCSTAT_BUMP(arcstat_hits); 25973403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 25983403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 25993403Sbmc data, metadata, hits); 26003403Sbmc 2601789Sahrens if (done) 2602789Sahrens done(NULL, buf, private); 2603789Sahrens } else { 2604789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2605789Sahrens arc_callback_t *acb; 26066987Sbrendan vdev_t *vd = NULL; 26079215SGeorge.Wilson@Sun.COM uint64_t addr; 26088582SBrendan.Gregg@Sun.COM boolean_t devw = B_FALSE; 2609789Sahrens 2610789Sahrens if (hdr == NULL) { 2611789Sahrens /* this block is not in the cache */ 2612789Sahrens arc_buf_hdr_t *exists; 26133290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 26143290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2615789Sahrens hdr = buf->b_hdr; 2616789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 2617789Sahrens hdr->b_birth = bp->blk_birth; 2618789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2619789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2620789Sahrens if (exists) { 2621789Sahrens /* somebody beat us to the hash insert */ 2622789Sahrens mutex_exit(hash_lock); 2623789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2624789Sahrens hdr->b_birth = 0; 2625789Sahrens hdr->b_cksum0 = 0; 26261544Seschrock (void) arc_buf_remove_ref(buf, private); 2627789Sahrens goto top; /* restart the IO request */ 2628789Sahrens } 26292391Smaybee /* if this is a prefetch, we don't have a reference */ 26302391Smaybee if (*arc_flags & ARC_PREFETCH) { 26312391Smaybee (void) remove_reference(hdr, hash_lock, 26322391Smaybee private); 26332391Smaybee hdr->b_flags |= ARC_PREFETCH; 26342391Smaybee } 26357237Sek110237 if (*arc_flags & ARC_L2CACHE) 26367237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26372391Smaybee if (BP_GET_LEVEL(bp) > 0) 26382391Smaybee hdr->b_flags |= ARC_INDIRECT; 2639789Sahrens } else { 2640789Sahrens /* this block is in the ghost cache */ 26411544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 26421544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 26432391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 26442391Smaybee ASSERT(hdr->b_buf == NULL); 2645789Sahrens 26462391Smaybee /* if this is a prefetch, we don't have a reference */ 26472391Smaybee if (*arc_flags & ARC_PREFETCH) 26482391Smaybee hdr->b_flags |= ARC_PREFETCH; 26492391Smaybee else 26502391Smaybee add_reference(hdr, hash_lock, private); 26517237Sek110237 if (*arc_flags & ARC_L2CACHE) 26527237Sek110237 hdr->b_flags |= ARC_L2CACHE; 26536245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 26541544Seschrock buf->b_hdr = hdr; 26552688Smaybee buf->b_data = NULL; 26561544Seschrock buf->b_efunc = NULL; 26571544Seschrock buf->b_private = NULL; 26581544Seschrock buf->b_next = NULL; 26591544Seschrock hdr->b_buf = buf; 26602688Smaybee arc_get_data_buf(buf); 26611544Seschrock ASSERT(hdr->b_datacnt == 0); 26621544Seschrock hdr->b_datacnt = 1; 26632391Smaybee 2664789Sahrens } 2665789Sahrens 2666789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2667789Sahrens acb->acb_done = done; 2668789Sahrens acb->acb_private = private; 2669789Sahrens 2670789Sahrens ASSERT(hdr->b_acb == NULL); 2671789Sahrens hdr->b_acb = acb; 2672789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2673789Sahrens 2674789Sahrens /* 2675789Sahrens * If the buffer has been evicted, migrate it to a present state 2676789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2677789Sahrens * the header will be marked as I/O in progress and have an 2678789Sahrens * attached buffer. At this point, anybody who finds this 2679789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2680789Sahrens */ 2681789Sahrens 26821544Seschrock if (GHOST_STATE(hdr->b_state)) 26832688Smaybee arc_access(hdr, hash_lock); 2684789Sahrens 26857754SJeff.Bonwick@Sun.COM if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 26867754SJeff.Bonwick@Sun.COM (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 26878582SBrendan.Gregg@Sun.COM devw = hdr->b_l2hdr->b_dev->l2ad_writing; 26886987Sbrendan addr = hdr->b_l2hdr->b_daddr; 26897754SJeff.Bonwick@Sun.COM /* 26907754SJeff.Bonwick@Sun.COM * Lock out device removal. 26917754SJeff.Bonwick@Sun.COM */ 26927754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || 26937754SJeff.Bonwick@Sun.COM !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 26947754SJeff.Bonwick@Sun.COM vd = NULL; 26956987Sbrendan } 26966987Sbrendan 26976987Sbrendan mutex_exit(hash_lock); 26986987Sbrendan 2699789Sahrens ASSERT3U(hdr->b_size, ==, size); 27001596Sahrens DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size, 27011596Sahrens zbookmark_t *, zb); 27023403Sbmc ARCSTAT_BUMP(arcstat_misses); 27033403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 27043403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 27053403Sbmc data, metadata, misses); 27061544Seschrock 27078582SBrendan.Gregg@Sun.COM if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 27086987Sbrendan /* 27096987Sbrendan * Read from the L2ARC if the following are true: 27106987Sbrendan * 1. The L2ARC vdev was previously cached. 27116987Sbrendan * 2. This buffer still has L2ARC metadata. 27126987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 27136987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 27146987Sbrendan * also have invalidated the vdev. 27158582SBrendan.Gregg@Sun.COM * 5. This isn't prefetch and l2arc_noprefetch is set. 27166987Sbrendan */ 27177754SJeff.Bonwick@Sun.COM if (hdr->b_l2hdr != NULL && 27188582SBrendan.Gregg@Sun.COM !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 27198582SBrendan.Gregg@Sun.COM !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 27205450Sbrendan l2arc_read_callback_t *cb; 27215450Sbrendan 27226643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 27236643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 27246643Seschrock 27255450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 27265450Sbrendan KM_SLEEP); 27275450Sbrendan cb->l2rcb_buf = buf; 27285450Sbrendan cb->l2rcb_spa = spa; 27295450Sbrendan cb->l2rcb_bp = *bp; 27305450Sbrendan cb->l2rcb_zb = *zb; 27317237Sek110237 cb->l2rcb_flags = zio_flags; 27325450Sbrendan 27335450Sbrendan /* 27347754SJeff.Bonwick@Sun.COM * l2arc read. The SCL_L2ARC lock will be 27357754SJeff.Bonwick@Sun.COM * released by l2arc_read_done(). 27365450Sbrendan */ 27375450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 27385450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 27397237Sek110237 l2arc_read_done, cb, priority, zio_flags | 27407361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 27417754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_PROPAGATE | 27427754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_RETRY, B_FALSE); 27435450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 27445450Sbrendan zio_t *, rzio); 27458582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_read_bytes, size); 27466987Sbrendan 27476987Sbrendan if (*arc_flags & ARC_NOWAIT) { 27486987Sbrendan zio_nowait(rzio); 27496987Sbrendan return (0); 27506987Sbrendan } 27516987Sbrendan 27526987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 27536987Sbrendan if (zio_wait(rzio) == 0) 27546987Sbrendan return (0); 27556987Sbrendan 27566987Sbrendan /* l2arc read error; goto zio_read() */ 27575450Sbrendan } else { 27585450Sbrendan DTRACE_PROBE1(l2arc__miss, 27595450Sbrendan arc_buf_hdr_t *, hdr); 27605450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 27615450Sbrendan if (HDR_L2_WRITING(hdr)) 27625450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 27637754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 27645450Sbrendan } 27658582SBrendan.Gregg@Sun.COM } else { 27668628SBill.Moore@Sun.COM if (vd != NULL) 27678628SBill.Moore@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 27688582SBrendan.Gregg@Sun.COM if (l2arc_ndev != 0) { 27698582SBrendan.Gregg@Sun.COM DTRACE_PROBE1(l2arc__miss, 27708582SBrendan.Gregg@Sun.COM arc_buf_hdr_t *, hdr); 27718582SBrendan.Gregg@Sun.COM ARCSTAT_BUMP(arcstat_l2_misses); 27728582SBrendan.Gregg@Sun.COM } 27735450Sbrendan } 27746643Seschrock 2775789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 27767237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2777789Sahrens 27782391Smaybee if (*arc_flags & ARC_WAIT) 2779789Sahrens return (zio_wait(rzio)); 2780789Sahrens 27812391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2782789Sahrens zio_nowait(rzio); 2783789Sahrens } 2784789Sahrens return (0); 2785789Sahrens } 2786789Sahrens 2787789Sahrens /* 2788789Sahrens * arc_read() variant to support pool traversal. If the block is already 2789789Sahrens * in the ARC, make a copy of it; otherwise, the caller will do the I/O. 2790789Sahrens * The idea is that we don't want pool traversal filling up memory, but 2791789Sahrens * if the ARC already has the data anyway, we shouldn't pay for the I/O. 2792789Sahrens */ 2793789Sahrens int 2794789Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data) 2795789Sahrens { 2796789Sahrens arc_buf_hdr_t *hdr; 2797789Sahrens kmutex_t *hash_mtx; 27988636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 2799789Sahrens int rc = 0; 2800789Sahrens 28018636SMark.Maybee@Sun.COM hdr = buf_hash_find(guid, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx); 2802789Sahrens 28031544Seschrock if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) { 28041544Seschrock arc_buf_t *buf = hdr->b_buf; 28051544Seschrock 28061544Seschrock ASSERT(buf); 28071544Seschrock while (buf->b_data == NULL) { 28081544Seschrock buf = buf->b_next; 28091544Seschrock ASSERT(buf); 28101544Seschrock } 28111544Seschrock bcopy(buf->b_data, data, hdr->b_size); 28121544Seschrock } else { 2813789Sahrens rc = ENOENT; 28141544Seschrock } 2815789Sahrens 2816789Sahrens if (hash_mtx) 2817789Sahrens mutex_exit(hash_mtx); 2818789Sahrens 2819789Sahrens return (rc); 2820789Sahrens } 2821789Sahrens 28221544Seschrock void 28231544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 28241544Seschrock { 28251544Seschrock ASSERT(buf->b_hdr != NULL); 28263403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 28271544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 28281544Seschrock buf->b_efunc = func; 28291544Seschrock buf->b_private = private; 28301544Seschrock } 28311544Seschrock 28321544Seschrock /* 28331544Seschrock * This is used by the DMU to let the ARC know that a buffer is 28341544Seschrock * being evicted, so the ARC should clean up. If this arc buf 28351544Seschrock * is not yet in the evicted state, it will be put there. 28361544Seschrock */ 28371544Seschrock int 28381544Seschrock arc_buf_evict(arc_buf_t *buf) 28391544Seschrock { 28402887Smaybee arc_buf_hdr_t *hdr; 28411544Seschrock kmutex_t *hash_lock; 28421544Seschrock arc_buf_t **bufp; 28431544Seschrock 28447545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 28452887Smaybee hdr = buf->b_hdr; 28461544Seschrock if (hdr == NULL) { 28471544Seschrock /* 28481544Seschrock * We are in arc_do_user_evicts(). 28491544Seschrock */ 28501544Seschrock ASSERT(buf->b_data == NULL); 28517545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 28521544Seschrock return (0); 28537545SMark.Maybee@Sun.COM } else if (buf->b_data == NULL) { 28547545SMark.Maybee@Sun.COM arc_buf_t copy = *buf; /* structure assignment */ 28557545SMark.Maybee@Sun.COM /* 28567545SMark.Maybee@Sun.COM * We are on the eviction list; process this buffer now 28577545SMark.Maybee@Sun.COM * but let arc_do_user_evicts() do the reaping. 28587545SMark.Maybee@Sun.COM */ 28597545SMark.Maybee@Sun.COM buf->b_efunc = NULL; 28607545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 28617545SMark.Maybee@Sun.COM VERIFY(copy.b_efunc(©) == 0); 28627545SMark.Maybee@Sun.COM return (1); 28631544Seschrock } 28642887Smaybee hash_lock = HDR_LOCK(hdr); 28651544Seschrock mutex_enter(hash_lock); 28661544Seschrock 28672724Smaybee ASSERT(buf->b_hdr == hdr); 28682724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 28693403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 28701544Seschrock 28711544Seschrock /* 28721544Seschrock * Pull this buffer off of the hdr 28731544Seschrock */ 28741544Seschrock bufp = &hdr->b_buf; 28751544Seschrock while (*bufp != buf) 28761544Seschrock bufp = &(*bufp)->b_next; 28771544Seschrock *bufp = buf->b_next; 28781544Seschrock 28791544Seschrock ASSERT(buf->b_data != NULL); 28802688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 28811544Seschrock 28821544Seschrock if (hdr->b_datacnt == 0) { 28831544Seschrock arc_state_t *old_state = hdr->b_state; 28841544Seschrock arc_state_t *evicted_state; 28851544Seschrock 28861544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 28871544Seschrock 28881544Seschrock evicted_state = 28893403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 28901544Seschrock 28913403Sbmc mutex_enter(&old_state->arcs_mtx); 28923403Sbmc mutex_enter(&evicted_state->arcs_mtx); 28931544Seschrock 28941544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 28951544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 28965450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 28975450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 28981544Seschrock 28993403Sbmc mutex_exit(&evicted_state->arcs_mtx); 29003403Sbmc mutex_exit(&old_state->arcs_mtx); 29011544Seschrock } 29021544Seschrock mutex_exit(hash_lock); 29037545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29041819Smaybee 29051544Seschrock VERIFY(buf->b_efunc(buf) == 0); 29061544Seschrock buf->b_efunc = NULL; 29071544Seschrock buf->b_private = NULL; 29081544Seschrock buf->b_hdr = NULL; 29091544Seschrock kmem_cache_free(buf_cache, buf); 29101544Seschrock return (1); 29111544Seschrock } 29121544Seschrock 2913789Sahrens /* 2914789Sahrens * Release this buffer from the cache. This must be done 2915789Sahrens * after a read and prior to modifying the buffer contents. 2916789Sahrens * If the buffer has more than one reference, we must make 29177046Sahrens * a new hdr for the buffer. 2918789Sahrens */ 2919789Sahrens void 2920789Sahrens arc_release(arc_buf_t *buf, void *tag) 2921789Sahrens { 29227545SMark.Maybee@Sun.COM arc_buf_hdr_t *hdr; 29237545SMark.Maybee@Sun.COM kmutex_t *hash_lock; 29247545SMark.Maybee@Sun.COM l2arc_buf_hdr_t *l2hdr; 29255450Sbrendan uint64_t buf_size; 2926*9274SBrendan.Gregg@Sun.COM boolean_t released = B_FALSE; 2927789Sahrens 29287545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 29297545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 29307545SMark.Maybee@Sun.COM 2931789Sahrens /* this buffer is not on any list */ 2932789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 29337046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 2934789Sahrens 29353403Sbmc if (hdr->b_state == arc_anon) { 2936789Sahrens /* this buffer is already released */ 2937789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2938789Sahrens ASSERT(BUF_EMPTY(hdr)); 29391544Seschrock ASSERT(buf->b_efunc == NULL); 29403093Sahrens arc_buf_thaw(buf); 29417545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 2942*9274SBrendan.Gregg@Sun.COM released = B_TRUE; 2943*9274SBrendan.Gregg@Sun.COM } else { 2944*9274SBrendan.Gregg@Sun.COM hash_lock = HDR_LOCK(hdr); 2945*9274SBrendan.Gregg@Sun.COM mutex_enter(hash_lock); 2946789Sahrens } 2947789Sahrens 29487545SMark.Maybee@Sun.COM l2hdr = hdr->b_l2hdr; 29497545SMark.Maybee@Sun.COM if (l2hdr) { 29507545SMark.Maybee@Sun.COM mutex_enter(&l2arc_buflist_mtx); 29517545SMark.Maybee@Sun.COM hdr->b_l2hdr = NULL; 29527545SMark.Maybee@Sun.COM buf_size = hdr->b_size; 29537545SMark.Maybee@Sun.COM } 29547545SMark.Maybee@Sun.COM 2955*9274SBrendan.Gregg@Sun.COM if (released) 2956*9274SBrendan.Gregg@Sun.COM goto out; 2957*9274SBrendan.Gregg@Sun.COM 29581544Seschrock /* 29591544Seschrock * Do we have more than one buf? 29601544Seschrock */ 29617545SMark.Maybee@Sun.COM if (hdr->b_datacnt > 1) { 2962789Sahrens arc_buf_hdr_t *nhdr; 2963789Sahrens arc_buf_t **bufp; 2964789Sahrens uint64_t blksz = hdr->b_size; 29658636SMark.Maybee@Sun.COM uint64_t spa = hdr->b_spa; 29663290Sjohansen arc_buf_contents_t type = hdr->b_type; 29675450Sbrendan uint32_t flags = hdr->b_flags; 2968789Sahrens 29697545SMark.Maybee@Sun.COM ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 2970789Sahrens /* 2971789Sahrens * Pull the data off of this buf and attach it to 2972789Sahrens * a new anonymous buf. 2973789Sahrens */ 29741544Seschrock (void) remove_reference(hdr, hash_lock, tag); 2975789Sahrens bufp = &hdr->b_buf; 29761544Seschrock while (*bufp != buf) 2977789Sahrens bufp = &(*bufp)->b_next; 2978789Sahrens *bufp = (*bufp)->b_next; 29793897Smaybee buf->b_next = NULL; 29801544Seschrock 29813403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 29823403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 29831544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 29844309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 29854309Smaybee ASSERT3U(*size, >=, hdr->b_size); 29864309Smaybee atomic_add_64(size, -hdr->b_size); 29871544Seschrock } 29881544Seschrock hdr->b_datacnt -= 1; 29893547Smaybee arc_cksum_verify(buf); 29901544Seschrock 2991789Sahrens mutex_exit(hash_lock); 2992789Sahrens 29936245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 2994789Sahrens nhdr->b_size = blksz; 2995789Sahrens nhdr->b_spa = spa; 29963290Sjohansen nhdr->b_type = type; 2997789Sahrens nhdr->b_buf = buf; 29983403Sbmc nhdr->b_state = arc_anon; 2999789Sahrens nhdr->b_arc_access = 0; 30005450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 30015450Sbrendan nhdr->b_l2hdr = NULL; 30021544Seschrock nhdr->b_datacnt = 1; 30033547Smaybee nhdr->b_freeze_cksum = NULL; 30043897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 3005789Sahrens buf->b_hdr = nhdr; 30067545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30073403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 3008789Sahrens } else { 30097545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30101544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3011789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 3012789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 30133403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 3014789Sahrens hdr->b_arc_access = 0; 3015789Sahrens mutex_exit(hash_lock); 30165450Sbrendan 3017789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 3018789Sahrens hdr->b_birth = 0; 3019789Sahrens hdr->b_cksum0 = 0; 30203547Smaybee arc_buf_thaw(buf); 3021789Sahrens } 30221544Seschrock buf->b_efunc = NULL; 30231544Seschrock buf->b_private = NULL; 30245450Sbrendan 3025*9274SBrendan.Gregg@Sun.COM out: 30265450Sbrendan if (l2hdr) { 30275450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 30285450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 30295450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 30307545SMark.Maybee@Sun.COM mutex_exit(&l2arc_buflist_mtx); 30315450Sbrendan } 3032789Sahrens } 3033789Sahrens 3034789Sahrens int 3035789Sahrens arc_released(arc_buf_t *buf) 3036789Sahrens { 30377545SMark.Maybee@Sun.COM int released; 30387545SMark.Maybee@Sun.COM 30397545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30407545SMark.Maybee@Sun.COM released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 30417545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30427545SMark.Maybee@Sun.COM return (released); 30431544Seschrock } 30441544Seschrock 30451544Seschrock int 30461544Seschrock arc_has_callback(arc_buf_t *buf) 30471544Seschrock { 30487545SMark.Maybee@Sun.COM int callback; 30497545SMark.Maybee@Sun.COM 30507545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30517545SMark.Maybee@Sun.COM callback = (buf->b_efunc != NULL); 30527545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30537545SMark.Maybee@Sun.COM return (callback); 3054789Sahrens } 3055789Sahrens 30561544Seschrock #ifdef ZFS_DEBUG 30571544Seschrock int 30581544Seschrock arc_referenced(arc_buf_t *buf) 30591544Seschrock { 30607545SMark.Maybee@Sun.COM int referenced; 30617545SMark.Maybee@Sun.COM 30627545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 30637545SMark.Maybee@Sun.COM referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 30647545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 30657545SMark.Maybee@Sun.COM return (referenced); 30661544Seschrock } 30671544Seschrock #endif 30681544Seschrock 3069789Sahrens static void 30703547Smaybee arc_write_ready(zio_t *zio) 30713547Smaybee { 30723547Smaybee arc_write_callback_t *callback = zio->io_private; 30733547Smaybee arc_buf_t *buf = callback->awcb_buf; 30745329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 30755329Sgw25295 30767754SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 30777754SJeff.Bonwick@Sun.COM callback->awcb_ready(zio, buf, callback->awcb_private); 30787754SJeff.Bonwick@Sun.COM 30795329Sgw25295 /* 30805329Sgw25295 * If the IO is already in progress, then this is a re-write 30817754SJeff.Bonwick@Sun.COM * attempt, so we need to thaw and re-compute the cksum. 30827754SJeff.Bonwick@Sun.COM * It is the responsibility of the callback to handle the 30837754SJeff.Bonwick@Sun.COM * accounting for any re-write attempt. 30845329Sgw25295 */ 30855329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 30865329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 30875329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 30885329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 30895329Sgw25295 hdr->b_freeze_cksum = NULL; 30905329Sgw25295 } 30915329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 30925329Sgw25295 } 30935450Sbrendan arc_cksum_compute(buf, B_FALSE); 30945329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 30953547Smaybee } 30963547Smaybee 30973547Smaybee static void 3098789Sahrens arc_write_done(zio_t *zio) 3099789Sahrens { 31003547Smaybee arc_write_callback_t *callback = zio->io_private; 31013547Smaybee arc_buf_t *buf = callback->awcb_buf; 31023547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3103789Sahrens 3104789Sahrens hdr->b_acb = NULL; 3105789Sahrens 3106789Sahrens hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3107789Sahrens hdr->b_birth = zio->io_bp->blk_birth; 3108789Sahrens hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 31091544Seschrock /* 31101544Seschrock * If the block to be written was all-zero, we may have 31111544Seschrock * compressed it away. In this case no write was performed 31121544Seschrock * so there will be no dva/birth-date/checksum. The buffer 31131544Seschrock * must therefor remain anonymous (and uncached). 31141544Seschrock */ 3115789Sahrens if (!BUF_EMPTY(hdr)) { 3116789Sahrens arc_buf_hdr_t *exists; 3117789Sahrens kmutex_t *hash_lock; 3118789Sahrens 31193093Sahrens arc_cksum_verify(buf); 31203093Sahrens 3121789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3122789Sahrens if (exists) { 3123789Sahrens /* 3124789Sahrens * This can only happen if we overwrite for 3125789Sahrens * sync-to-convergence, because we remove 3126789Sahrens * buffers from the hash table when we arc_free(). 3127789Sahrens */ 31287754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_IO_REWRITE); 3129789Sahrens ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig), 3130789Sahrens BP_IDENTITY(zio->io_bp))); 3131789Sahrens ASSERT3U(zio->io_bp_orig.blk_birth, ==, 3132789Sahrens zio->io_bp->blk_birth); 3133789Sahrens 3134789Sahrens ASSERT(refcount_is_zero(&exists->b_refcnt)); 31353403Sbmc arc_change_state(arc_anon, exists, hash_lock); 3136789Sahrens mutex_exit(hash_lock); 31371544Seschrock arc_hdr_destroy(exists); 3138789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3139789Sahrens ASSERT3P(exists, ==, NULL); 3140789Sahrens } 31411544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 31427046Sahrens /* if it's not anon, we are doing a scrub */ 31437046Sahrens if (hdr->b_state == arc_anon) 31447046Sahrens arc_access(hdr, hash_lock); 31452688Smaybee mutex_exit(hash_lock); 31463547Smaybee } else if (callback->awcb_done == NULL) { 31471544Seschrock int destroy_hdr; 31481544Seschrock /* 31491544Seschrock * This is an anonymous buffer with no user callback, 31501544Seschrock * destroy it if there are no active references. 31511544Seschrock */ 31521544Seschrock mutex_enter(&arc_eviction_mtx); 31531544Seschrock destroy_hdr = refcount_is_zero(&hdr->b_refcnt); 31541544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 31551544Seschrock mutex_exit(&arc_eviction_mtx); 31561544Seschrock if (destroy_hdr) 31571544Seschrock arc_hdr_destroy(hdr); 31581544Seschrock } else { 31591544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3160789Sahrens } 31617046Sahrens hdr->b_flags &= ~ARC_STORED; 31621544Seschrock 31633547Smaybee if (callback->awcb_done) { 3164789Sahrens ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 31653547Smaybee callback->awcb_done(zio, buf, callback->awcb_private); 3166789Sahrens } 3167789Sahrens 31683547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3169789Sahrens } 3170789Sahrens 31717872STim.Haley@Sun.COM void 31727754SJeff.Bonwick@Sun.COM write_policy(spa_t *spa, const writeprops_t *wp, zio_prop_t *zp) 31737046Sahrens { 31747046Sahrens boolean_t ismd = (wp->wp_level > 0 || dmu_ot[wp->wp_type].ot_metadata); 31757046Sahrens 31767046Sahrens /* Determine checksum setting */ 31777046Sahrens if (ismd) { 31787046Sahrens /* 31797046Sahrens * Metadata always gets checksummed. If the data 31807046Sahrens * checksum is multi-bit correctable, and it's not a 31817046Sahrens * ZBT-style checksum, then it's suitable for metadata 31827046Sahrens * as well. Otherwise, the metadata checksum defaults 31837046Sahrens * to fletcher4. 31847046Sahrens */ 31857046Sahrens if (zio_checksum_table[wp->wp_oschecksum].ci_correctable && 31867046Sahrens !zio_checksum_table[wp->wp_oschecksum].ci_zbt) 31877754SJeff.Bonwick@Sun.COM zp->zp_checksum = wp->wp_oschecksum; 31887046Sahrens else 31897754SJeff.Bonwick@Sun.COM zp->zp_checksum = ZIO_CHECKSUM_FLETCHER_4; 31907046Sahrens } else { 31917754SJeff.Bonwick@Sun.COM zp->zp_checksum = zio_checksum_select(wp->wp_dnchecksum, 31927046Sahrens wp->wp_oschecksum); 31937046Sahrens } 31947046Sahrens 31957046Sahrens /* Determine compression setting */ 31967046Sahrens if (ismd) { 31977046Sahrens /* 31987046Sahrens * XXX -- we should design a compression algorithm 31997046Sahrens * that specializes in arrays of bps. 32007046Sahrens */ 32017754SJeff.Bonwick@Sun.COM zp->zp_compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY : 32027046Sahrens ZIO_COMPRESS_LZJB; 32037046Sahrens } else { 32047754SJeff.Bonwick@Sun.COM zp->zp_compress = zio_compress_select(wp->wp_dncompress, 32057046Sahrens wp->wp_oscompress); 32067046Sahrens } 32077754SJeff.Bonwick@Sun.COM 32087754SJeff.Bonwick@Sun.COM zp->zp_type = wp->wp_type; 32097754SJeff.Bonwick@Sun.COM zp->zp_level = wp->wp_level; 32107754SJeff.Bonwick@Sun.COM zp->zp_ndvas = MIN(wp->wp_copies + ismd, spa_max_replication(spa)); 32117046Sahrens } 32127046Sahrens 32133547Smaybee zio_t * 32147046Sahrens arc_write(zio_t *pio, spa_t *spa, const writeprops_t *wp, 32157237Sek110237 boolean_t l2arc, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 32163547Smaybee arc_done_func_t *ready, arc_done_func_t *done, void *private, int priority, 32177237Sek110237 int zio_flags, const zbookmark_t *zb) 3218789Sahrens { 3219789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 32203547Smaybee arc_write_callback_t *callback; 32217754SJeff.Bonwick@Sun.COM zio_t *zio; 32227754SJeff.Bonwick@Sun.COM zio_prop_t zp; 32237754SJeff.Bonwick@Sun.COM 32247754SJeff.Bonwick@Sun.COM ASSERT(ready != NULL); 3225789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 32262237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 32272237Smaybee ASSERT(hdr->b_acb == 0); 32287237Sek110237 if (l2arc) 32297237Sek110237 hdr->b_flags |= ARC_L2CACHE; 32303547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 32313547Smaybee callback->awcb_ready = ready; 32323547Smaybee callback->awcb_done = done; 32333547Smaybee callback->awcb_private = private; 32343547Smaybee callback->awcb_buf = buf; 32357046Sahrens 32367754SJeff.Bonwick@Sun.COM write_policy(spa, wp, &zp); 32377754SJeff.Bonwick@Sun.COM zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, &zp, 32387754SJeff.Bonwick@Sun.COM arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3239789Sahrens 32403547Smaybee return (zio); 3241789Sahrens } 3242789Sahrens 3243789Sahrens int 3244789Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 3245789Sahrens zio_done_func_t *done, void *private, uint32_t arc_flags) 3246789Sahrens { 3247789Sahrens arc_buf_hdr_t *ab; 3248789Sahrens kmutex_t *hash_lock; 3249789Sahrens zio_t *zio; 32508636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 3251789Sahrens 3252789Sahrens /* 3253789Sahrens * If this buffer is in the cache, release it, so it 3254789Sahrens * can be re-used. 3255789Sahrens */ 32568636SMark.Maybee@Sun.COM ab = buf_hash_find(guid, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 3257789Sahrens if (ab != NULL) { 3258789Sahrens /* 3259789Sahrens * The checksum of blocks to free is not always 3260789Sahrens * preserved (eg. on the deadlist). However, if it is 3261789Sahrens * nonzero, it should match what we have in the cache. 3262789Sahrens */ 3263789Sahrens ASSERT(bp->blk_cksum.zc_word[0] == 0 || 32647754SJeff.Bonwick@Sun.COM bp->blk_cksum.zc_word[0] == ab->b_cksum0 || 32657754SJeff.Bonwick@Sun.COM bp->blk_fill == BLK_FILL_ALREADY_FREED); 32667754SJeff.Bonwick@Sun.COM 32673403Sbmc if (ab->b_state != arc_anon) 32683403Sbmc arc_change_state(arc_anon, ab, hash_lock); 32692391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 32702391Smaybee /* 32712391Smaybee * This should only happen when we prefetch. 32722391Smaybee */ 32732391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 32742391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 32752391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 32762391Smaybee if (HDR_IN_HASH_TABLE(ab)) 32772391Smaybee buf_hash_remove(ab); 32782391Smaybee ab->b_arc_access = 0; 32792391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 32802391Smaybee ab->b_birth = 0; 32812391Smaybee ab->b_cksum0 = 0; 32822391Smaybee ab->b_buf->b_efunc = NULL; 32832391Smaybee ab->b_buf->b_private = NULL; 32842391Smaybee mutex_exit(hash_lock); 32852391Smaybee } else if (refcount_is_zero(&ab->b_refcnt)) { 32865450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3287789Sahrens mutex_exit(hash_lock); 32881544Seschrock arc_hdr_destroy(ab); 32893403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3290789Sahrens } else { 32911589Smaybee /* 32922391Smaybee * We still have an active reference on this 32932391Smaybee * buffer. This can happen, e.g., from 32942391Smaybee * dbuf_unoverride(). 32951589Smaybee */ 32962391Smaybee ASSERT(!HDR_IN_HASH_TABLE(ab)); 3297789Sahrens ab->b_arc_access = 0; 3298789Sahrens bzero(&ab->b_dva, sizeof (dva_t)); 3299789Sahrens ab->b_birth = 0; 3300789Sahrens ab->b_cksum0 = 0; 33011544Seschrock ab->b_buf->b_efunc = NULL; 33021544Seschrock ab->b_buf->b_private = NULL; 3303789Sahrens mutex_exit(hash_lock); 3304789Sahrens } 3305789Sahrens } 3306789Sahrens 33077754SJeff.Bonwick@Sun.COM zio = zio_free(pio, spa, txg, bp, done, private, ZIO_FLAG_MUSTSUCCEED); 3308789Sahrens 3309789Sahrens if (arc_flags & ARC_WAIT) 3310789Sahrens return (zio_wait(zio)); 3311789Sahrens 3312789Sahrens ASSERT(arc_flags & ARC_NOWAIT); 3313789Sahrens zio_nowait(zio); 3314789Sahrens 3315789Sahrens return (0); 3316789Sahrens } 3317789Sahrens 33186245Smaybee static int 33196245Smaybee arc_memory_throttle(uint64_t reserve, uint64_t txg) 33206245Smaybee { 33216245Smaybee #ifdef _KERNEL 33226245Smaybee uint64_t inflight_data = arc_anon->arcs_size; 33236245Smaybee uint64_t available_memory = ptob(freemem); 33246245Smaybee static uint64_t page_load = 0; 33256245Smaybee static uint64_t last_txg = 0; 33266245Smaybee 33276245Smaybee #if defined(__i386) 33286245Smaybee available_memory = 33296245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 33306245Smaybee #endif 33316245Smaybee if (available_memory >= zfs_write_limit_max) 33326245Smaybee return (0); 33336245Smaybee 33346245Smaybee if (txg > last_txg) { 33356245Smaybee last_txg = txg; 33366245Smaybee page_load = 0; 33376245Smaybee } 33386245Smaybee /* 33396245Smaybee * If we are in pageout, we know that memory is already tight, 33406245Smaybee * the arc is already going to be evicting, so we just want to 33416245Smaybee * continue to let page writes occur as quickly as possible. 33426245Smaybee */ 33436245Smaybee if (curproc == proc_pageout) { 33446245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 33456245Smaybee return (ERESTART); 33466245Smaybee /* Note: reserve is inflated, so we deflate */ 33476245Smaybee page_load += reserve / 8; 33486245Smaybee return (0); 33496245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 33506245Smaybee /* memory is low, delay before restarting */ 33516245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33526245Smaybee return (EAGAIN); 33536245Smaybee } 33546245Smaybee page_load = 0; 33556245Smaybee 33566245Smaybee if (arc_size > arc_c_min) { 33576245Smaybee uint64_t evictable_memory = 33586245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 33596245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 33606245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 33616245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 33626245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 33636245Smaybee } 33646245Smaybee 33656245Smaybee if (inflight_data > available_memory / 4) { 33666245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 33676245Smaybee return (ERESTART); 33686245Smaybee } 33696245Smaybee #endif 33706245Smaybee return (0); 33716245Smaybee } 33726245Smaybee 3373789Sahrens void 33746245Smaybee arc_tempreserve_clear(uint64_t reserve) 3375789Sahrens { 33766245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3377789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3378789Sahrens } 3379789Sahrens 3380789Sahrens int 33816245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3382789Sahrens { 33836245Smaybee int error; 33846245Smaybee 3385789Sahrens #ifdef ZFS_DEBUG 3386789Sahrens /* 3387789Sahrens * Once in a while, fail for no reason. Everything should cope. 3388789Sahrens */ 3389789Sahrens if (spa_get_random(10000) == 0) { 3390789Sahrens dprintf("forcing random failure\n"); 3391789Sahrens return (ERESTART); 3392789Sahrens } 3393789Sahrens #endif 33946245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 33956245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 33966245Smaybee if (reserve > arc_c) 3397982Smaybee return (ENOMEM); 3398982Smaybee 3399789Sahrens /* 34006245Smaybee * Writes will, almost always, require additional memory allocations 34016245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 34026245Smaybee * make sure that there is sufficient available memory for this. 34036245Smaybee */ 34046245Smaybee if (error = arc_memory_throttle(reserve, txg)) 34056245Smaybee return (error); 34066245Smaybee 34076245Smaybee /* 3408982Smaybee * Throttle writes when the amount of dirty data in the cache 3409982Smaybee * gets too large. We try to keep the cache less than half full 3410982Smaybee * of dirty blocks so that our sync times don't grow too large. 3411982Smaybee * Note: if two requests come in concurrently, we might let them 3412982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3413789Sahrens */ 34146245Smaybee if (reserve + arc_tempreserve + arc_anon->arcs_size > arc_c / 2 && 34156245Smaybee arc_anon->arcs_size > arc_c / 4) { 34164309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 34174309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 34184309Smaybee arc_tempreserve>>10, 34194309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 34204309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 34216245Smaybee reserve>>10, arc_c>>10); 3422789Sahrens return (ERESTART); 3423789Sahrens } 34246245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3425789Sahrens return (0); 3426789Sahrens } 3427789Sahrens 3428789Sahrens void 3429789Sahrens arc_init(void) 3430789Sahrens { 3431789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3432789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3433789Sahrens 34342391Smaybee /* Convert seconds to clock ticks */ 34352638Sperrin arc_min_prefetch_lifespan = 1 * hz; 34362391Smaybee 3437789Sahrens /* Start out with 1/8 of all memory */ 34383403Sbmc arc_c = physmem * PAGESIZE / 8; 3439789Sahrens 3440789Sahrens #ifdef _KERNEL 3441789Sahrens /* 3442789Sahrens * On architectures where the physical memory can be larger 3443789Sahrens * than the addressable space (intel in 32-bit mode), we may 3444789Sahrens * need to limit the cache to 1/8 of VM size. 3445789Sahrens */ 34463403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3447789Sahrens #endif 3448789Sahrens 3449982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 34503403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3451982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 34523403Sbmc if (arc_c * 8 >= 1<<30) 34533403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3454789Sahrens else 34553403Sbmc arc_c_max = arc_c_min; 34563403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 34572885Sahrens 34582885Sahrens /* 34592885Sahrens * Allow the tunables to override our calculations if they are 34602885Sahrens * reasonable (ie. over 64MB) 34612885Sahrens */ 34622885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 34633403Sbmc arc_c_max = zfs_arc_max; 34643403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 34653403Sbmc arc_c_min = zfs_arc_min; 34662885Sahrens 34673403Sbmc arc_c = arc_c_max; 34683403Sbmc arc_p = (arc_c >> 1); 3469789Sahrens 34704309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 34714309Smaybee arc_meta_limit = arc_c_max / 4; 34724645Sek110237 34734645Sek110237 /* Allow the tunable to override if it is reasonable */ 34744645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 34754645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 34764645Sek110237 34774309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 34784309Smaybee arc_c_min = arc_meta_limit / 2; 34794309Smaybee 34808582SBrendan.Gregg@Sun.COM if (zfs_arc_grow_retry > 0) 34818582SBrendan.Gregg@Sun.COM arc_grow_retry = zfs_arc_grow_retry; 34828582SBrendan.Gregg@Sun.COM 34838582SBrendan.Gregg@Sun.COM if (zfs_arc_shrink_shift > 0) 34848582SBrendan.Gregg@Sun.COM arc_shrink_shift = zfs_arc_shrink_shift; 34858582SBrendan.Gregg@Sun.COM 34868582SBrendan.Gregg@Sun.COM if (zfs_arc_p_min_shift > 0) 34878582SBrendan.Gregg@Sun.COM arc_p_min_shift = zfs_arc_p_min_shift; 34888582SBrendan.Gregg@Sun.COM 3489789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3490789Sahrens if (kmem_debugging()) 34913403Sbmc arc_c = arc_c / 2; 34923403Sbmc if (arc_c < arc_c_min) 34933403Sbmc arc_c = arc_c_min; 3494789Sahrens 34953403Sbmc arc_anon = &ARC_anon; 34963403Sbmc arc_mru = &ARC_mru; 34973403Sbmc arc_mru_ghost = &ARC_mru_ghost; 34983403Sbmc arc_mfu = &ARC_mfu; 34993403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 35005450Sbrendan arc_l2c_only = &ARC_l2c_only; 35013403Sbmc arc_size = 0; 3502789Sahrens 35033403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35043403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35053403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35063403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35073403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35085450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 35092688Smaybee 35104309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 35114309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35124309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 35134309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35144309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 35154309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35164309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 35174309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35184309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 35194309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35204309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 35214309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35224309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 35234309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35244309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 35254309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35265450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 35275450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35285450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 35295450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3530789Sahrens 3531789Sahrens buf_init(); 3532789Sahrens 3533789Sahrens arc_thread_exit = 0; 35341544Seschrock arc_eviction_list = NULL; 35351544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 35362887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3537789Sahrens 35383403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 35393403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 35403403Sbmc 35413403Sbmc if (arc_ksp != NULL) { 35423403Sbmc arc_ksp->ks_data = &arc_stats; 35433403Sbmc kstat_install(arc_ksp); 35443403Sbmc } 35453403Sbmc 3546789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3547789Sahrens TS_RUN, minclsyspri); 35483158Smaybee 35493158Smaybee arc_dead = FALSE; 35506987Sbrendan arc_warm = B_FALSE; 35516245Smaybee 35526245Smaybee if (zfs_write_limit_max == 0) 35537468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 35546245Smaybee else 35556245Smaybee zfs_write_limit_shift = 0; 35567468SMark.Maybee@Sun.COM mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3557789Sahrens } 3558789Sahrens 3559789Sahrens void 3560789Sahrens arc_fini(void) 3561789Sahrens { 3562789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3563789Sahrens arc_thread_exit = 1; 3564789Sahrens while (arc_thread_exit != 0) 3565789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3566789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3567789Sahrens 35685642Smaybee arc_flush(NULL); 3569789Sahrens 3570789Sahrens arc_dead = TRUE; 3571789Sahrens 35723403Sbmc if (arc_ksp != NULL) { 35733403Sbmc kstat_delete(arc_ksp); 35743403Sbmc arc_ksp = NULL; 35753403Sbmc } 35763403Sbmc 35771544Seschrock mutex_destroy(&arc_eviction_mtx); 3578789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3579789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3580789Sahrens 35814309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 35824309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 35834309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 35844309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 35854309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 35864309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 35874309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 35884309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3589789Sahrens 35903403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 35913403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 35923403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 35933403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 35943403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 35958214SRicardo.M.Correia@Sun.COM mutex_destroy(&arc_l2c_only->arcs_mtx); 35962856Snd150628 35977468SMark.Maybee@Sun.COM mutex_destroy(&zfs_write_limit_lock); 35987468SMark.Maybee@Sun.COM 3599789Sahrens buf_fini(); 3600789Sahrens } 36015450Sbrendan 36025450Sbrendan /* 36035450Sbrendan * Level 2 ARC 36045450Sbrendan * 36055450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 36065450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 36075450Sbrendan * using large infrequent writes. The main role of this cache is to boost 36085450Sbrendan * the performance of random read workloads. The intended L2ARC devices 36095450Sbrendan * include short-stroked disks, solid state disks, and other media with 36105450Sbrendan * substantially faster read latency than disk. 36115450Sbrendan * 36125450Sbrendan * +-----------------------+ 36135450Sbrendan * | ARC | 36145450Sbrendan * +-----------------------+ 36155450Sbrendan * | ^ ^ 36165450Sbrendan * | | | 36175450Sbrendan * l2arc_feed_thread() arc_read() 36185450Sbrendan * | | | 36195450Sbrendan * | l2arc read | 36205450Sbrendan * V | | 36215450Sbrendan * +---------------+ | 36225450Sbrendan * | L2ARC | | 36235450Sbrendan * +---------------+ | 36245450Sbrendan * | ^ | 36255450Sbrendan * l2arc_write() | | 36265450Sbrendan * | | | 36275450Sbrendan * V | | 36285450Sbrendan * +-------+ +-------+ 36295450Sbrendan * | vdev | | vdev | 36305450Sbrendan * | cache | | cache | 36315450Sbrendan * +-------+ +-------+ 36325450Sbrendan * +=========+ .-----. 36335450Sbrendan * : L2ARC : |-_____-| 36345450Sbrendan * : devices : | Disks | 36355450Sbrendan * +=========+ `-_____-' 36365450Sbrendan * 36375450Sbrendan * Read requests are satisfied from the following sources, in order: 36385450Sbrendan * 36395450Sbrendan * 1) ARC 36405450Sbrendan * 2) vdev cache of L2ARC devices 36415450Sbrendan * 3) L2ARC devices 36425450Sbrendan * 4) vdev cache of disks 36435450Sbrendan * 5) disks 36445450Sbrendan * 36455450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 36465450Sbrendan * To accommodate for this there are some significant differences between 36475450Sbrendan * the L2ARC and traditional cache design: 36485450Sbrendan * 36495450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 36505450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 36515450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 36525450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 36535450Sbrendan * 36545450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 36555450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 36565450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 36575450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 36585450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 36595450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 36605450Sbrendan * provide a better sense of ratio than this diagram: 36615450Sbrendan * 36625450Sbrendan * head --> tail 36635450Sbrendan * +---------------------+----------+ 36645450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 36655450Sbrendan * +---------------------+----------+ | o L2ARC eligible 36665450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 36675450Sbrendan * +---------------------+----------+ | 36685450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 36695450Sbrendan * headroom | 36705450Sbrendan * l2arc_feed_thread() 36715450Sbrendan * | 36725450Sbrendan * l2arc write hand <--[oooo]--' 36735450Sbrendan * | 8 Mbyte 36745450Sbrendan * | write max 36755450Sbrendan * V 36765450Sbrendan * +==============================+ 36775450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 36785450Sbrendan * +==============================+ 36795450Sbrendan * 32 Gbytes 36805450Sbrendan * 36815450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 36825450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 36835450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 36845450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 36855450Sbrendan * the ARC lists have moved there due to inactivity. 36865450Sbrendan * 36875450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 36885450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 36895450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 36905450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 36915450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 36925450Sbrendan * quickly, such as during backups of the entire pool. 36935450Sbrendan * 36946987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 36956987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 36966987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 36976987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 36986987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 36996987Sbrendan * 37006987Sbrendan * The L2ARC device write speed is also boosted during this time so that 37016987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 37026987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 37036987Sbrendan * through increased writes. 37046987Sbrendan * 37056987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 37065450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 37075450Sbrendan * device is written to in a rotor fashion, sweeping writes through 37085450Sbrendan * available space then repeating. 37095450Sbrendan * 37106987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 37115450Sbrendan * write buffers back to disk based storage. 37125450Sbrendan * 37136987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 37145450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 37155450Sbrendan * 37165450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 37175450Sbrendan * may be necessary for different workloads: 37185450Sbrendan * 37195450Sbrendan * l2arc_write_max max write bytes per interval 37206987Sbrendan * l2arc_write_boost extra write bytes during device warmup 37215450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 37225450Sbrendan * l2arc_headroom number of max device writes to precache 37235450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 37245450Sbrendan * 37255450Sbrendan * Tunables may be removed or added as future performance improvements are 37265450Sbrendan * integrated, and also may become zpool properties. 37278582SBrendan.Gregg@Sun.COM * 37288582SBrendan.Gregg@Sun.COM * There are three key functions that control how the L2ARC warms up: 37298582SBrendan.Gregg@Sun.COM * 37308582SBrendan.Gregg@Sun.COM * l2arc_write_eligible() check if a buffer is eligible to cache 37318582SBrendan.Gregg@Sun.COM * l2arc_write_size() calculate how much to write 37328582SBrendan.Gregg@Sun.COM * l2arc_write_interval() calculate sleep delay between writes 37338582SBrendan.Gregg@Sun.COM * 37348582SBrendan.Gregg@Sun.COM * These three functions determine what to write, how much, and how quickly 37358582SBrendan.Gregg@Sun.COM * to send writes. 37365450Sbrendan */ 37375450Sbrendan 37388582SBrendan.Gregg@Sun.COM static boolean_t 37398636SMark.Maybee@Sun.COM l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 37408582SBrendan.Gregg@Sun.COM { 37418582SBrendan.Gregg@Sun.COM /* 37428582SBrendan.Gregg@Sun.COM * A buffer is *not* eligible for the L2ARC if it: 37438582SBrendan.Gregg@Sun.COM * 1. belongs to a different spa. 37448582SBrendan.Gregg@Sun.COM * 2. has no attached buffer. 37458582SBrendan.Gregg@Sun.COM * 3. is already cached on the L2ARC. 37468582SBrendan.Gregg@Sun.COM * 4. has an I/O in progress (it may be an incomplete read). 37478582SBrendan.Gregg@Sun.COM * 5. is flagged not eligible (zfs property). 37488582SBrendan.Gregg@Sun.COM */ 37498636SMark.Maybee@Sun.COM if (ab->b_spa != spa_guid || ab->b_buf == NULL || ab->b_l2hdr != NULL || 37508582SBrendan.Gregg@Sun.COM HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) 37518582SBrendan.Gregg@Sun.COM return (B_FALSE); 37528582SBrendan.Gregg@Sun.COM 37538582SBrendan.Gregg@Sun.COM return (B_TRUE); 37548582SBrendan.Gregg@Sun.COM } 37558582SBrendan.Gregg@Sun.COM 37568582SBrendan.Gregg@Sun.COM static uint64_t 37578582SBrendan.Gregg@Sun.COM l2arc_write_size(l2arc_dev_t *dev) 37588582SBrendan.Gregg@Sun.COM { 37598582SBrendan.Gregg@Sun.COM uint64_t size; 37608582SBrendan.Gregg@Sun.COM 37618582SBrendan.Gregg@Sun.COM size = dev->l2ad_write; 37628582SBrendan.Gregg@Sun.COM 37638582SBrendan.Gregg@Sun.COM if (arc_warm == B_FALSE) 37648582SBrendan.Gregg@Sun.COM size += dev->l2ad_boost; 37658582SBrendan.Gregg@Sun.COM 37668582SBrendan.Gregg@Sun.COM return (size); 37678582SBrendan.Gregg@Sun.COM 37688582SBrendan.Gregg@Sun.COM } 37698582SBrendan.Gregg@Sun.COM 37708582SBrendan.Gregg@Sun.COM static clock_t 37718582SBrendan.Gregg@Sun.COM l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 37728582SBrendan.Gregg@Sun.COM { 37738582SBrendan.Gregg@Sun.COM clock_t interval, next; 37748582SBrendan.Gregg@Sun.COM 37758582SBrendan.Gregg@Sun.COM /* 37768582SBrendan.Gregg@Sun.COM * If the ARC lists are busy, increase our write rate; if the 37778582SBrendan.Gregg@Sun.COM * lists are stale, idle back. This is achieved by checking 37788582SBrendan.Gregg@Sun.COM * how much we previously wrote - if it was more than half of 37798582SBrendan.Gregg@Sun.COM * what we wanted, schedule the next write much sooner. 37808582SBrendan.Gregg@Sun.COM */ 37818582SBrendan.Gregg@Sun.COM if (l2arc_feed_again && wrote > (wanted / 2)) 37828582SBrendan.Gregg@Sun.COM interval = (hz * l2arc_feed_min_ms) / 1000; 37838582SBrendan.Gregg@Sun.COM else 37848582SBrendan.Gregg@Sun.COM interval = hz * l2arc_feed_secs; 37858582SBrendan.Gregg@Sun.COM 37868582SBrendan.Gregg@Sun.COM next = MAX(lbolt, MIN(lbolt + interval, began + interval)); 37878582SBrendan.Gregg@Sun.COM 37888582SBrendan.Gregg@Sun.COM return (next); 37898582SBrendan.Gregg@Sun.COM } 37908582SBrendan.Gregg@Sun.COM 37915450Sbrendan static void 37925450Sbrendan l2arc_hdr_stat_add(void) 37935450Sbrendan { 37946018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 37956018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 37965450Sbrendan } 37975450Sbrendan 37985450Sbrendan static void 37995450Sbrendan l2arc_hdr_stat_remove(void) 38005450Sbrendan { 38016018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 38026018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 38035450Sbrendan } 38045450Sbrendan 38055450Sbrendan /* 38065450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 38076987Sbrendan * If a device is returned, this also returns holding the spa config lock. 38085450Sbrendan */ 38095450Sbrendan static l2arc_dev_t * 38105450Sbrendan l2arc_dev_get_next(void) 38115450Sbrendan { 38126987Sbrendan l2arc_dev_t *first, *next = NULL; 38136987Sbrendan 38146987Sbrendan /* 38156987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 38166987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 38176987Sbrendan * both locks will be dropped and a spa config lock held instead. 38186987Sbrendan */ 38196987Sbrendan mutex_enter(&spa_namespace_lock); 38206987Sbrendan mutex_enter(&l2arc_dev_mtx); 38216643Seschrock 38226643Seschrock /* if there are no vdevs, there is nothing to do */ 38236643Seschrock if (l2arc_ndev == 0) 38246987Sbrendan goto out; 38256643Seschrock 38266643Seschrock first = NULL; 38276643Seschrock next = l2arc_dev_last; 38286643Seschrock do { 38296643Seschrock /* loop around the list looking for a non-faulted vdev */ 38306643Seschrock if (next == NULL) { 38315450Sbrendan next = list_head(l2arc_dev_list); 38326643Seschrock } else { 38336643Seschrock next = list_next(l2arc_dev_list, next); 38346643Seschrock if (next == NULL) 38356643Seschrock next = list_head(l2arc_dev_list); 38366643Seschrock } 38376643Seschrock 38386643Seschrock /* if we have come back to the start, bail out */ 38396643Seschrock if (first == NULL) 38406643Seschrock first = next; 38416643Seschrock else if (next == first) 38426643Seschrock break; 38436643Seschrock 38446643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 38456643Seschrock 38466643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 38476643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 38486987Sbrendan next = NULL; 38495450Sbrendan 38505450Sbrendan l2arc_dev_last = next; 38515450Sbrendan 38526987Sbrendan out: 38536987Sbrendan mutex_exit(&l2arc_dev_mtx); 38546987Sbrendan 38556987Sbrendan /* 38566987Sbrendan * Grab the config lock to prevent the 'next' device from being 38576987Sbrendan * removed while we are writing to it. 38586987Sbrendan */ 38596987Sbrendan if (next != NULL) 38607754SJeff.Bonwick@Sun.COM spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 38616987Sbrendan mutex_exit(&spa_namespace_lock); 38626987Sbrendan 38635450Sbrendan return (next); 38645450Sbrendan } 38655450Sbrendan 38665450Sbrendan /* 38676987Sbrendan * Free buffers that were tagged for destruction. 38686987Sbrendan */ 38696987Sbrendan static void 38706987Sbrendan l2arc_do_free_on_write() 38716987Sbrendan { 38726987Sbrendan list_t *buflist; 38736987Sbrendan l2arc_data_free_t *df, *df_prev; 38746987Sbrendan 38756987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 38766987Sbrendan buflist = l2arc_free_on_write; 38776987Sbrendan 38786987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 38796987Sbrendan df_prev = list_prev(buflist, df); 38806987Sbrendan ASSERT(df->l2df_data != NULL); 38816987Sbrendan ASSERT(df->l2df_func != NULL); 38826987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 38836987Sbrendan list_remove(buflist, df); 38846987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 38856987Sbrendan } 38866987Sbrendan 38876987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 38886987Sbrendan } 38896987Sbrendan 38906987Sbrendan /* 38915450Sbrendan * A write to a cache device has completed. Update all headers to allow 38925450Sbrendan * reads from these buffers to begin. 38935450Sbrendan */ 38945450Sbrendan static void 38955450Sbrendan l2arc_write_done(zio_t *zio) 38965450Sbrendan { 38975450Sbrendan l2arc_write_callback_t *cb; 38985450Sbrendan l2arc_dev_t *dev; 38995450Sbrendan list_t *buflist; 39005450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 39016987Sbrendan l2arc_buf_hdr_t *abl2; 39025450Sbrendan kmutex_t *hash_lock; 39035450Sbrendan 39045450Sbrendan cb = zio->io_private; 39055450Sbrendan ASSERT(cb != NULL); 39065450Sbrendan dev = cb->l2wcb_dev; 39075450Sbrendan ASSERT(dev != NULL); 39085450Sbrendan head = cb->l2wcb_head; 39095450Sbrendan ASSERT(head != NULL); 39105450Sbrendan buflist = dev->l2ad_buflist; 39115450Sbrendan ASSERT(buflist != NULL); 39125450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 39135450Sbrendan l2arc_write_callback_t *, cb); 39145450Sbrendan 39155450Sbrendan if (zio->io_error != 0) 39165450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 39175450Sbrendan 39185450Sbrendan mutex_enter(&l2arc_buflist_mtx); 39195450Sbrendan 39205450Sbrendan /* 39215450Sbrendan * All writes completed, or an error was hit. 39225450Sbrendan */ 39235450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 39245450Sbrendan ab_prev = list_prev(buflist, ab); 39255450Sbrendan 39265450Sbrendan hash_lock = HDR_LOCK(ab); 39275450Sbrendan if (!mutex_tryenter(hash_lock)) { 39285450Sbrendan /* 39295450Sbrendan * This buffer misses out. It may be in a stage 39305450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 39315450Sbrendan * left set, denying reads to this buffer. 39325450Sbrendan */ 39335450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 39345450Sbrendan continue; 39355450Sbrendan } 39365450Sbrendan 39375450Sbrendan if (zio->io_error != 0) { 39385450Sbrendan /* 39396987Sbrendan * Error - drop L2ARC entry. 39405450Sbrendan */ 39416987Sbrendan list_remove(buflist, ab); 39426987Sbrendan abl2 = ab->b_l2hdr; 39435450Sbrendan ab->b_l2hdr = NULL; 39446987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 39456987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 39465450Sbrendan } 39475450Sbrendan 39485450Sbrendan /* 39495450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 39505450Sbrendan */ 39515450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 39525450Sbrendan 39535450Sbrendan mutex_exit(hash_lock); 39545450Sbrendan } 39555450Sbrendan 39565450Sbrendan atomic_inc_64(&l2arc_writes_done); 39575450Sbrendan list_remove(buflist, head); 39585450Sbrendan kmem_cache_free(hdr_cache, head); 39595450Sbrendan mutex_exit(&l2arc_buflist_mtx); 39605450Sbrendan 39616987Sbrendan l2arc_do_free_on_write(); 39625450Sbrendan 39635450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 39645450Sbrendan } 39655450Sbrendan 39665450Sbrendan /* 39675450Sbrendan * A read to a cache device completed. Validate buffer contents before 39685450Sbrendan * handing over to the regular ARC routines. 39695450Sbrendan */ 39705450Sbrendan static void 39715450Sbrendan l2arc_read_done(zio_t *zio) 39725450Sbrendan { 39735450Sbrendan l2arc_read_callback_t *cb; 39745450Sbrendan arc_buf_hdr_t *hdr; 39755450Sbrendan arc_buf_t *buf; 39765450Sbrendan kmutex_t *hash_lock; 39776987Sbrendan int equal; 39785450Sbrendan 39797754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd != NULL); 39807754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 39817754SJeff.Bonwick@Sun.COM 39827754SJeff.Bonwick@Sun.COM spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 39837754SJeff.Bonwick@Sun.COM 39845450Sbrendan cb = zio->io_private; 39855450Sbrendan ASSERT(cb != NULL); 39865450Sbrendan buf = cb->l2rcb_buf; 39875450Sbrendan ASSERT(buf != NULL); 39885450Sbrendan hdr = buf->b_hdr; 39895450Sbrendan ASSERT(hdr != NULL); 39905450Sbrendan 39915450Sbrendan hash_lock = HDR_LOCK(hdr); 39925450Sbrendan mutex_enter(hash_lock); 39935450Sbrendan 39945450Sbrendan /* 39955450Sbrendan * Check this survived the L2ARC journey. 39965450Sbrendan */ 39975450Sbrendan equal = arc_cksum_equal(buf); 39985450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 39995450Sbrendan mutex_exit(hash_lock); 40005450Sbrendan zio->io_private = buf; 40017754SJeff.Bonwick@Sun.COM zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 40027754SJeff.Bonwick@Sun.COM zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 40035450Sbrendan arc_read_done(zio); 40045450Sbrendan } else { 40055450Sbrendan mutex_exit(hash_lock); 40065450Sbrendan /* 40075450Sbrendan * Buffer didn't survive caching. Increment stats and 40085450Sbrendan * reissue to the original storage device. 40095450Sbrendan */ 40106987Sbrendan if (zio->io_error != 0) { 40115450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 40126987Sbrendan } else { 40136987Sbrendan zio->io_error = EIO; 40146987Sbrendan } 40155450Sbrendan if (!equal) 40165450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 40175450Sbrendan 40187754SJeff.Bonwick@Sun.COM /* 40197754SJeff.Bonwick@Sun.COM * If there's no waiter, issue an async i/o to the primary 40207754SJeff.Bonwick@Sun.COM * storage now. If there *is* a waiter, the caller must 40217754SJeff.Bonwick@Sun.COM * issue the i/o in a context where it's OK to block. 40227754SJeff.Bonwick@Sun.COM */ 40238632SBill.Moore@Sun.COM if (zio->io_waiter == NULL) { 40248632SBill.Moore@Sun.COM zio_t *pio = zio_unique_parent(zio); 40258632SBill.Moore@Sun.COM 40268632SBill.Moore@Sun.COM ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 40278632SBill.Moore@Sun.COM 40288632SBill.Moore@Sun.COM zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 40297754SJeff.Bonwick@Sun.COM buf->b_data, zio->io_size, arc_read_done, buf, 40307754SJeff.Bonwick@Sun.COM zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 40318632SBill.Moore@Sun.COM } 40325450Sbrendan } 40335450Sbrendan 40345450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 40355450Sbrendan } 40365450Sbrendan 40375450Sbrendan /* 40385450Sbrendan * This is the list priority from which the L2ARC will search for pages to 40395450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 40405450Sbrendan * desired order. This order can have a significant effect on cache 40415450Sbrendan * performance. 40425450Sbrendan * 40435450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 40445450Sbrendan * the data lists. This function returns a locked list, and also returns 40455450Sbrendan * the lock pointer. 40465450Sbrendan */ 40475450Sbrendan static list_t * 40485450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 40495450Sbrendan { 40505450Sbrendan list_t *list; 40515450Sbrendan 40525450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 40535450Sbrendan 40545450Sbrendan switch (list_num) { 40555450Sbrendan case 0: 40565450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 40575450Sbrendan *lock = &arc_mfu->arcs_mtx; 40585450Sbrendan break; 40595450Sbrendan case 1: 40605450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 40615450Sbrendan *lock = &arc_mru->arcs_mtx; 40625450Sbrendan break; 40635450Sbrendan case 2: 40645450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 40655450Sbrendan *lock = &arc_mfu->arcs_mtx; 40665450Sbrendan break; 40675450Sbrendan case 3: 40685450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 40695450Sbrendan *lock = &arc_mru->arcs_mtx; 40705450Sbrendan break; 40715450Sbrendan } 40725450Sbrendan 40735450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 40745450Sbrendan mutex_enter(*lock); 40755450Sbrendan return (list); 40765450Sbrendan } 40775450Sbrendan 40785450Sbrendan /* 40795450Sbrendan * Evict buffers from the device write hand to the distance specified in 40805450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 40815450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 40825450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 40835450Sbrendan */ 40845450Sbrendan static void 40855450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 40865450Sbrendan { 40875450Sbrendan list_t *buflist; 40885450Sbrendan l2arc_buf_hdr_t *abl2; 40895450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 40905450Sbrendan kmutex_t *hash_lock; 40915450Sbrendan uint64_t taddr; 40925450Sbrendan 40935450Sbrendan buflist = dev->l2ad_buflist; 40945450Sbrendan 40955450Sbrendan if (buflist == NULL) 40965450Sbrendan return; 40975450Sbrendan 40985450Sbrendan if (!all && dev->l2ad_first) { 40995450Sbrendan /* 41005450Sbrendan * This is the first sweep through the device. There is 41015450Sbrendan * nothing to evict. 41025450Sbrendan */ 41035450Sbrendan return; 41045450Sbrendan } 41055450Sbrendan 41066987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 41075450Sbrendan /* 41085450Sbrendan * When nearing the end of the device, evict to the end 41095450Sbrendan * before the device write hand jumps to the start. 41105450Sbrendan */ 41115450Sbrendan taddr = dev->l2ad_end; 41125450Sbrendan } else { 41135450Sbrendan taddr = dev->l2ad_hand + distance; 41145450Sbrendan } 41155450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 41165450Sbrendan uint64_t, taddr, boolean_t, all); 41175450Sbrendan 41185450Sbrendan top: 41195450Sbrendan mutex_enter(&l2arc_buflist_mtx); 41205450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 41215450Sbrendan ab_prev = list_prev(buflist, ab); 41225450Sbrendan 41235450Sbrendan hash_lock = HDR_LOCK(ab); 41245450Sbrendan if (!mutex_tryenter(hash_lock)) { 41255450Sbrendan /* 41265450Sbrendan * Missed the hash lock. Retry. 41275450Sbrendan */ 41285450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 41295450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41305450Sbrendan mutex_enter(hash_lock); 41315450Sbrendan mutex_exit(hash_lock); 41325450Sbrendan goto top; 41335450Sbrendan } 41345450Sbrendan 41355450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 41365450Sbrendan /* 41375450Sbrendan * We hit a write head node. Leave it for 41385450Sbrendan * l2arc_write_done(). 41395450Sbrendan */ 41405450Sbrendan list_remove(buflist, ab); 41415450Sbrendan mutex_exit(hash_lock); 41425450Sbrendan continue; 41435450Sbrendan } 41445450Sbrendan 41455450Sbrendan if (!all && ab->b_l2hdr != NULL && 41465450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 41475450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 41485450Sbrendan /* 41495450Sbrendan * We've evicted to the target address, 41505450Sbrendan * or the end of the device. 41515450Sbrendan */ 41525450Sbrendan mutex_exit(hash_lock); 41535450Sbrendan break; 41545450Sbrendan } 41555450Sbrendan 41565450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 41575450Sbrendan /* 41585450Sbrendan * Already on the path to destruction. 41595450Sbrendan */ 41605450Sbrendan mutex_exit(hash_lock); 41615450Sbrendan continue; 41625450Sbrendan } 41635450Sbrendan 41645450Sbrendan if (ab->b_state == arc_l2c_only) { 41655450Sbrendan ASSERT(!HDR_L2_READING(ab)); 41665450Sbrendan /* 41675450Sbrendan * This doesn't exist in the ARC. Destroy. 41685450Sbrendan * arc_hdr_destroy() will call list_remove() 41695450Sbrendan * and decrement arcstat_l2_size. 41705450Sbrendan */ 41715450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 41725450Sbrendan arc_hdr_destroy(ab); 41735450Sbrendan } else { 41745450Sbrendan /* 41756987Sbrendan * Invalidate issued or about to be issued 41766987Sbrendan * reads, since we may be about to write 41776987Sbrendan * over this location. 41786987Sbrendan */ 41796987Sbrendan if (HDR_L2_READING(ab)) { 41806987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 41816987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 41826987Sbrendan } 41836987Sbrendan 41846987Sbrendan /* 41855450Sbrendan * Tell ARC this no longer exists in L2ARC. 41865450Sbrendan */ 41875450Sbrendan if (ab->b_l2hdr != NULL) { 41885450Sbrendan abl2 = ab->b_l2hdr; 41895450Sbrendan ab->b_l2hdr = NULL; 41905450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 41915450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 41925450Sbrendan } 41935450Sbrendan list_remove(buflist, ab); 41945450Sbrendan 41955450Sbrendan /* 41965450Sbrendan * This may have been leftover after a 41975450Sbrendan * failed write. 41985450Sbrendan */ 41995450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 42005450Sbrendan } 42015450Sbrendan mutex_exit(hash_lock); 42025450Sbrendan } 42035450Sbrendan mutex_exit(&l2arc_buflist_mtx); 42045450Sbrendan 42055450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, -(taddr - dev->l2ad_evict)); 42065450Sbrendan dev->l2ad_evict = taddr; 42075450Sbrendan } 42085450Sbrendan 42095450Sbrendan /* 42105450Sbrendan * Find and write ARC buffers to the L2ARC device. 42115450Sbrendan * 42125450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 42135450Sbrendan * for reading until they have completed writing. 42145450Sbrendan */ 42158582SBrendan.Gregg@Sun.COM static uint64_t 42166987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 42175450Sbrendan { 42185450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 42195450Sbrendan l2arc_buf_hdr_t *hdrl2; 42205450Sbrendan list_t *list; 42216987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 42225450Sbrendan void *buf_data; 42235450Sbrendan kmutex_t *hash_lock, *list_lock; 42245450Sbrendan boolean_t have_lock, full; 42255450Sbrendan l2arc_write_callback_t *cb; 42265450Sbrendan zio_t *pio, *wzio; 42278636SMark.Maybee@Sun.COM uint64_t guid = spa_guid(spa); 42285450Sbrendan 42295450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 42305450Sbrendan 42315450Sbrendan pio = NULL; 42325450Sbrendan write_sz = 0; 42335450Sbrendan full = B_FALSE; 42346245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 42355450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 42365450Sbrendan 42375450Sbrendan /* 42385450Sbrendan * Copy buffers for L2ARC writing. 42395450Sbrendan */ 42405450Sbrendan mutex_enter(&l2arc_buflist_mtx); 42415450Sbrendan for (int try = 0; try <= 3; try++) { 42425450Sbrendan list = l2arc_list_locked(try, &list_lock); 42435450Sbrendan passed_sz = 0; 42445450Sbrendan 42456987Sbrendan /* 42466987Sbrendan * L2ARC fast warmup. 42476987Sbrendan * 42486987Sbrendan * Until the ARC is warm and starts to evict, read from the 42496987Sbrendan * head of the ARC lists rather than the tail. 42506987Sbrendan */ 42516987Sbrendan headroom = target_sz * l2arc_headroom; 42526987Sbrendan if (arc_warm == B_FALSE) 42536987Sbrendan ab = list_head(list); 42546987Sbrendan else 42556987Sbrendan ab = list_tail(list); 42566987Sbrendan 42576987Sbrendan for (; ab; ab = ab_prev) { 42586987Sbrendan if (arc_warm == B_FALSE) 42596987Sbrendan ab_prev = list_next(list, ab); 42606987Sbrendan else 42616987Sbrendan ab_prev = list_prev(list, ab); 42625450Sbrendan 42635450Sbrendan hash_lock = HDR_LOCK(ab); 42645450Sbrendan have_lock = MUTEX_HELD(hash_lock); 42655450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 42665450Sbrendan /* 42675450Sbrendan * Skip this buffer rather than waiting. 42685450Sbrendan */ 42695450Sbrendan continue; 42705450Sbrendan } 42715450Sbrendan 42725450Sbrendan passed_sz += ab->b_size; 42735450Sbrendan if (passed_sz > headroom) { 42745450Sbrendan /* 42755450Sbrendan * Searched too far. 42765450Sbrendan */ 42775450Sbrendan mutex_exit(hash_lock); 42785450Sbrendan break; 42795450Sbrendan } 42805450Sbrendan 42818636SMark.Maybee@Sun.COM if (!l2arc_write_eligible(guid, ab)) { 42825450Sbrendan mutex_exit(hash_lock); 42835450Sbrendan continue; 42845450Sbrendan } 42855450Sbrendan 42865450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 42875450Sbrendan full = B_TRUE; 42885450Sbrendan mutex_exit(hash_lock); 42895450Sbrendan break; 42905450Sbrendan } 42915450Sbrendan 42925450Sbrendan if (pio == NULL) { 42935450Sbrendan /* 42945450Sbrendan * Insert a dummy header on the buflist so 42955450Sbrendan * l2arc_write_done() can find where the 42965450Sbrendan * write buffers begin without searching. 42975450Sbrendan */ 42985450Sbrendan list_insert_head(dev->l2ad_buflist, head); 42995450Sbrendan 43005450Sbrendan cb = kmem_alloc( 43015450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 43025450Sbrendan cb->l2wcb_dev = dev; 43035450Sbrendan cb->l2wcb_head = head; 43045450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 43055450Sbrendan ZIO_FLAG_CANFAIL); 43065450Sbrendan } 43075450Sbrendan 43085450Sbrendan /* 43095450Sbrendan * Create and add a new L2ARC header. 43105450Sbrendan */ 43115450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 43125450Sbrendan hdrl2->b_dev = dev; 43135450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 43145450Sbrendan 43155450Sbrendan ab->b_flags |= ARC_L2_WRITING; 43165450Sbrendan ab->b_l2hdr = hdrl2; 43175450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 43185450Sbrendan buf_data = ab->b_buf->b_data; 43195450Sbrendan buf_sz = ab->b_size; 43205450Sbrendan 43215450Sbrendan /* 43225450Sbrendan * Compute and store the buffer cksum before 43235450Sbrendan * writing. On debug the cksum is verified first. 43245450Sbrendan */ 43255450Sbrendan arc_cksum_verify(ab->b_buf); 43265450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 43275450Sbrendan 43285450Sbrendan mutex_exit(hash_lock); 43295450Sbrendan 43305450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 43315450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 43325450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 43335450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 43345450Sbrendan 43355450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 43365450Sbrendan zio_t *, wzio); 43375450Sbrendan (void) zio_nowait(wzio); 43385450Sbrendan 43397754SJeff.Bonwick@Sun.COM /* 43407754SJeff.Bonwick@Sun.COM * Keep the clock hand suitably device-aligned. 43417754SJeff.Bonwick@Sun.COM */ 43427754SJeff.Bonwick@Sun.COM buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 43437754SJeff.Bonwick@Sun.COM 43445450Sbrendan write_sz += buf_sz; 43455450Sbrendan dev->l2ad_hand += buf_sz; 43465450Sbrendan } 43475450Sbrendan 43485450Sbrendan mutex_exit(list_lock); 43495450Sbrendan 43505450Sbrendan if (full == B_TRUE) 43515450Sbrendan break; 43525450Sbrendan } 43535450Sbrendan mutex_exit(&l2arc_buflist_mtx); 43545450Sbrendan 43555450Sbrendan if (pio == NULL) { 43565450Sbrendan ASSERT3U(write_sz, ==, 0); 43575450Sbrendan kmem_cache_free(hdr_cache, head); 43588582SBrendan.Gregg@Sun.COM return (0); 43595450Sbrendan } 43605450Sbrendan 43615450Sbrendan ASSERT3U(write_sz, <=, target_sz); 43625450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 43638582SBrendan.Gregg@Sun.COM ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 43645450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 43655450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, write_sz); 43665450Sbrendan 43675450Sbrendan /* 43685450Sbrendan * Bump device hand to the device start if it is approaching the end. 43695450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 43705450Sbrendan */ 43716987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 43725450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, 43735450Sbrendan dev->l2ad_end - dev->l2ad_hand); 43745450Sbrendan dev->l2ad_hand = dev->l2ad_start; 43755450Sbrendan dev->l2ad_evict = dev->l2ad_start; 43765450Sbrendan dev->l2ad_first = B_FALSE; 43775450Sbrendan } 43785450Sbrendan 43798582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_TRUE; 43805450Sbrendan (void) zio_wait(pio); 43818582SBrendan.Gregg@Sun.COM dev->l2ad_writing = B_FALSE; 43828582SBrendan.Gregg@Sun.COM 43838582SBrendan.Gregg@Sun.COM return (write_sz); 43845450Sbrendan } 43855450Sbrendan 43865450Sbrendan /* 43875450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 43885450Sbrendan * heart of the L2ARC. 43895450Sbrendan */ 43905450Sbrendan static void 43915450Sbrendan l2arc_feed_thread(void) 43925450Sbrendan { 43935450Sbrendan callb_cpr_t cpr; 43945450Sbrendan l2arc_dev_t *dev; 43955450Sbrendan spa_t *spa; 43968582SBrendan.Gregg@Sun.COM uint64_t size, wrote; 43978582SBrendan.Gregg@Sun.COM clock_t begin, next = lbolt; 43985450Sbrendan 43995450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 44005450Sbrendan 44015450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 44025450Sbrendan 44035450Sbrendan while (l2arc_thread_exit == 0) { 44045450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 44056987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 44068582SBrendan.Gregg@Sun.COM next); 44076987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 44088582SBrendan.Gregg@Sun.COM next = lbolt + hz; 44096987Sbrendan 44106987Sbrendan /* 44116987Sbrendan * Quick check for L2ARC devices. 44126987Sbrendan */ 44136987Sbrendan mutex_enter(&l2arc_dev_mtx); 44146987Sbrendan if (l2arc_ndev == 0) { 44156987Sbrendan mutex_exit(&l2arc_dev_mtx); 44166987Sbrendan continue; 44175450Sbrendan } 44186987Sbrendan mutex_exit(&l2arc_dev_mtx); 44198582SBrendan.Gregg@Sun.COM begin = lbolt; 44206643Seschrock 44215450Sbrendan /* 44226643Seschrock * This selects the next l2arc device to write to, and in 44236643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 44246987Sbrendan * will return NULL if there are now no l2arc devices or if 44256987Sbrendan * they are all faulted. 44266987Sbrendan * 44276987Sbrendan * If a device is returned, its spa's config lock is also 44286987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 44296987Sbrendan * will grab and release l2arc_dev_mtx. 44305450Sbrendan */ 44316987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 44325450Sbrendan continue; 44336987Sbrendan 44346987Sbrendan spa = dev->l2ad_spa; 44356987Sbrendan ASSERT(spa != NULL); 44365450Sbrendan 44375450Sbrendan /* 44385450Sbrendan * Avoid contributing to memory pressure. 44395450Sbrendan */ 44405450Sbrendan if (arc_reclaim_needed()) { 44415450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 44427754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44435450Sbrendan continue; 44445450Sbrendan } 44455450Sbrendan 44465450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 44475450Sbrendan 44488582SBrendan.Gregg@Sun.COM size = l2arc_write_size(dev); 44496987Sbrendan 44505450Sbrendan /* 44515450Sbrendan * Evict L2ARC buffers that will be overwritten. 44525450Sbrendan */ 44536987Sbrendan l2arc_evict(dev, size, B_FALSE); 44545450Sbrendan 44555450Sbrendan /* 44565450Sbrendan * Write ARC buffers. 44575450Sbrendan */ 44588582SBrendan.Gregg@Sun.COM wrote = l2arc_write_buffers(spa, dev, size); 44598582SBrendan.Gregg@Sun.COM 44608582SBrendan.Gregg@Sun.COM /* 44618582SBrendan.Gregg@Sun.COM * Calculate interval between writes. 44628582SBrendan.Gregg@Sun.COM */ 44638582SBrendan.Gregg@Sun.COM next = l2arc_write_interval(begin, size, wrote); 44647754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 44655450Sbrendan } 44665450Sbrendan 44675450Sbrendan l2arc_thread_exit = 0; 44685450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 44695450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 44705450Sbrendan thread_exit(); 44715450Sbrendan } 44725450Sbrendan 44736643Seschrock boolean_t 44746643Seschrock l2arc_vdev_present(vdev_t *vd) 44756643Seschrock { 44766643Seschrock l2arc_dev_t *dev; 44776643Seschrock 44786643Seschrock mutex_enter(&l2arc_dev_mtx); 44796643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 44806643Seschrock dev = list_next(l2arc_dev_list, dev)) { 44816643Seschrock if (dev->l2ad_vdev == vd) 44826643Seschrock break; 44836643Seschrock } 44846643Seschrock mutex_exit(&l2arc_dev_mtx); 44856643Seschrock 44866643Seschrock return (dev != NULL); 44876643Seschrock } 44886643Seschrock 44895450Sbrendan /* 44905450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 44915450Sbrendan * validated the vdev and opened it. 44925450Sbrendan */ 44935450Sbrendan void 44945450Sbrendan l2arc_add_vdev(spa_t *spa, vdev_t *vd, uint64_t start, uint64_t end) 44955450Sbrendan { 44965450Sbrendan l2arc_dev_t *adddev; 44975450Sbrendan 44986643Seschrock ASSERT(!l2arc_vdev_present(vd)); 44996643Seschrock 45005450Sbrendan /* 45015450Sbrendan * Create a new l2arc device entry. 45025450Sbrendan */ 45035450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 45045450Sbrendan adddev->l2ad_spa = spa; 45055450Sbrendan adddev->l2ad_vdev = vd; 45065450Sbrendan adddev->l2ad_write = l2arc_write_max; 45076987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 45085450Sbrendan adddev->l2ad_start = start; 45095450Sbrendan adddev->l2ad_end = end; 45105450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 45115450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 45125450Sbrendan adddev->l2ad_first = B_TRUE; 45138582SBrendan.Gregg@Sun.COM adddev->l2ad_writing = B_FALSE; 45145450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 45155450Sbrendan 45165450Sbrendan /* 45175450Sbrendan * This is a list of all ARC buffers that are still valid on the 45185450Sbrendan * device. 45195450Sbrendan */ 45205450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 45215450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 45225450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 45235450Sbrendan 45245450Sbrendan spa_l2cache_space_update(vd, adddev->l2ad_end - adddev->l2ad_hand, 0); 45255450Sbrendan 45265450Sbrendan /* 45275450Sbrendan * Add device to global list 45285450Sbrendan */ 45295450Sbrendan mutex_enter(&l2arc_dev_mtx); 45305450Sbrendan list_insert_head(l2arc_dev_list, adddev); 45315450Sbrendan atomic_inc_64(&l2arc_ndev); 45325450Sbrendan mutex_exit(&l2arc_dev_mtx); 45335450Sbrendan } 45345450Sbrendan 45355450Sbrendan /* 45365450Sbrendan * Remove a vdev from the L2ARC. 45375450Sbrendan */ 45385450Sbrendan void 45395450Sbrendan l2arc_remove_vdev(vdev_t *vd) 45405450Sbrendan { 45415450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 45425450Sbrendan 45435450Sbrendan /* 45445450Sbrendan * Find the device by vdev 45455450Sbrendan */ 45465450Sbrendan mutex_enter(&l2arc_dev_mtx); 45475450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 45485450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 45495450Sbrendan if (vd == dev->l2ad_vdev) { 45505450Sbrendan remdev = dev; 45515450Sbrendan break; 45525450Sbrendan } 45535450Sbrendan } 45545450Sbrendan ASSERT(remdev != NULL); 45555450Sbrendan 45565450Sbrendan /* 45575450Sbrendan * Remove device from global list 45585450Sbrendan */ 45595450Sbrendan list_remove(l2arc_dev_list, remdev); 45605450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 45616987Sbrendan atomic_dec_64(&l2arc_ndev); 45626987Sbrendan mutex_exit(&l2arc_dev_mtx); 45635450Sbrendan 45645450Sbrendan /* 45655450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 45665450Sbrendan */ 45675450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 45685450Sbrendan list_destroy(remdev->l2ad_buflist); 45695450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 45705450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 45715450Sbrendan } 45725450Sbrendan 45735450Sbrendan void 45747754SJeff.Bonwick@Sun.COM l2arc_init(void) 45755450Sbrendan { 45765450Sbrendan l2arc_thread_exit = 0; 45775450Sbrendan l2arc_ndev = 0; 45785450Sbrendan l2arc_writes_sent = 0; 45795450Sbrendan l2arc_writes_done = 0; 45805450Sbrendan 45815450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 45825450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 45835450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 45845450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 45855450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 45865450Sbrendan 45875450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 45885450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 45895450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 45905450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 45915450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 45925450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 45935450Sbrendan } 45945450Sbrendan 45955450Sbrendan void 45967754SJeff.Bonwick@Sun.COM l2arc_fini(void) 45975450Sbrendan { 45986987Sbrendan /* 45996987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 46006987Sbrendan * Because of this, we can assume that all l2arc devices have 46016987Sbrendan * already been removed when the pools themselves were removed. 46026987Sbrendan */ 46036987Sbrendan 46046987Sbrendan l2arc_do_free_on_write(); 46056987Sbrendan 46065450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 46075450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 46085450Sbrendan mutex_destroy(&l2arc_dev_mtx); 46095450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 46105450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 46115450Sbrendan 46125450Sbrendan list_destroy(l2arc_dev_list); 46135450Sbrendan list_destroy(l2arc_free_on_write); 46145450Sbrendan } 46157754SJeff.Bonwick@Sun.COM 46167754SJeff.Bonwick@Sun.COM void 46177754SJeff.Bonwick@Sun.COM l2arc_start(void) 46187754SJeff.Bonwick@Sun.COM { 46198241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46207754SJeff.Bonwick@Sun.COM return; 46217754SJeff.Bonwick@Sun.COM 46227754SJeff.Bonwick@Sun.COM (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 46237754SJeff.Bonwick@Sun.COM TS_RUN, minclsyspri); 46247754SJeff.Bonwick@Sun.COM } 46257754SJeff.Bonwick@Sun.COM 46267754SJeff.Bonwick@Sun.COM void 46277754SJeff.Bonwick@Sun.COM l2arc_stop(void) 46287754SJeff.Bonwick@Sun.COM { 46298241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 46307754SJeff.Bonwick@Sun.COM return; 46317754SJeff.Bonwick@Sun.COM 46327754SJeff.Bonwick@Sun.COM mutex_enter(&l2arc_feed_thr_lock); 46337754SJeff.Bonwick@Sun.COM cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 46347754SJeff.Bonwick@Sun.COM l2arc_thread_exit = 1; 46357754SJeff.Bonwick@Sun.COM while (l2arc_thread_exit != 0) 46367754SJeff.Bonwick@Sun.COM cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 46377754SJeff.Bonwick@Sun.COM mutex_exit(&l2arc_feed_thr_lock); 46387754SJeff.Bonwick@Sun.COM } 4639