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 /* 223403Sbmc * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens /* 293403Sbmc * DVA-based Adjustable Replacement Cache 30789Sahrens * 311544Seschrock * While much of the theory of operation used here is 321544Seschrock * based on the self-tuning, low overhead replacement cache 33789Sahrens * presented by Megiddo and Modha at FAST 2003, there are some 34789Sahrens * significant differences: 35789Sahrens * 36789Sahrens * 1. The Megiddo and Modha model assumes any page is evictable. 37789Sahrens * Pages in its cache cannot be "locked" into memory. This makes 38789Sahrens * the eviction algorithm simple: evict the last page in the list. 39789Sahrens * This also make the performance characteristics easy to reason 40789Sahrens * about. Our cache is not so simple. At any given moment, some 41789Sahrens * subset of the blocks in the cache are un-evictable because we 42789Sahrens * have handed out a reference to them. Blocks are only evictable 43789Sahrens * when there are no external references active. This makes 44789Sahrens * eviction far more problematic: we choose to evict the evictable 45789Sahrens * blocks that are the "lowest" in the list. 46789Sahrens * 47789Sahrens * There are times when it is not possible to evict the requested 48789Sahrens * space. In these circumstances we are unable to adjust the cache 49789Sahrens * size. To prevent the cache growing unbounded at these times we 505450Sbrendan * implement a "cache throttle" that slows the flow of new data 515450Sbrendan * into the cache until we can make space available. 52789Sahrens * 53789Sahrens * 2. The Megiddo and Modha model assumes a fixed cache size. 54789Sahrens * Pages are evicted when the cache is full and there is a cache 55789Sahrens * miss. Our model has a variable sized cache. It grows with 565450Sbrendan * high use, but also tries to react to memory pressure from the 57789Sahrens * operating system: decreasing its size when system memory is 58789Sahrens * tight. 59789Sahrens * 60789Sahrens * 3. The Megiddo and Modha model assumes a fixed page size. All 61789Sahrens * elements of the cache are therefor exactly the same size. So 62789Sahrens * when adjusting the cache size following a cache miss, its simply 63789Sahrens * a matter of choosing a single page to evict. In our model, we 64789Sahrens * have variable sized cache blocks (rangeing from 512 bytes to 65789Sahrens * 128K bytes). We therefor choose a set of blocks to evict to make 66789Sahrens * space for a cache miss that approximates as closely as possible 67789Sahrens * the space used by the new block. 68789Sahrens * 69789Sahrens * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 70789Sahrens * by N. Megiddo & D. Modha, FAST 2003 71789Sahrens */ 72789Sahrens 73789Sahrens /* 74789Sahrens * The locking model: 75789Sahrens * 76789Sahrens * A new reference to a cache buffer can be obtained in two 77789Sahrens * ways: 1) via a hash table lookup using the DVA as a key, 785450Sbrendan * or 2) via one of the ARC lists. The arc_read() interface 79789Sahrens * uses method 1, while the internal arc algorithms for 80789Sahrens * adjusting the cache use method 2. We therefor provide two 81789Sahrens * types of locks: 1) the hash table lock array, and 2) the 82789Sahrens * arc list locks. 83789Sahrens * 84789Sahrens * Buffers do not have their own mutexs, rather they rely on the 85789Sahrens * hash table mutexs for the bulk of their protection (i.e. most 86789Sahrens * fields in the arc_buf_hdr_t are protected by these mutexs). 87789Sahrens * 88789Sahrens * buf_hash_find() returns the appropriate mutex (held) when it 89789Sahrens * locates the requested buffer in the hash table. It returns 90789Sahrens * NULL for the mutex if the buffer was not in the table. 91789Sahrens * 92789Sahrens * buf_hash_remove() expects the appropriate hash mutex to be 93789Sahrens * already held before it is invoked. 94789Sahrens * 95789Sahrens * Each arc state also has a mutex which is used to protect the 96789Sahrens * buffer list associated with the state. When attempting to 97789Sahrens * obtain a hash table lock while holding an arc list lock you 98789Sahrens * must use: mutex_tryenter() to avoid deadlock. Also note that 992688Smaybee * the active state mutex must be held before the ghost state mutex. 100789Sahrens * 1011544Seschrock * Arc buffers may have an associated eviction callback function. 1021544Seschrock * This function will be invoked prior to removing the buffer (e.g. 1031544Seschrock * in arc_do_user_evicts()). Note however that the data associated 1041544Seschrock * with the buffer may be evicted prior to the callback. The callback 1051544Seschrock * must be made with *no locks held* (to prevent deadlock). Additionally, 1061544Seschrock * the users of callbacks must ensure that their private data is 1071544Seschrock * protected from simultaneous callbacks from arc_buf_evict() 1081544Seschrock * and arc_do_user_evicts(). 1091544Seschrock * 110789Sahrens * Note that the majority of the performance stats are manipulated 111789Sahrens * with atomic operations. 1125450Sbrendan * 1135450Sbrendan * The L2ARC uses the l2arc_buflist_mtx global mutex for the following: 1145450Sbrendan * 1155450Sbrendan * - L2ARC buflist creation 1165450Sbrendan * - L2ARC buflist eviction 1175450Sbrendan * - L2ARC write completion, which walks L2ARC buflists 1185450Sbrendan * - ARC header destruction, as it removes from L2ARC buflists 1195450Sbrendan * - ARC header release, as it removes from L2ARC buflists 120789Sahrens */ 121789Sahrens 122789Sahrens #include <sys/spa.h> 123789Sahrens #include <sys/zio.h> 1243093Sahrens #include <sys/zio_checksum.h> 125789Sahrens #include <sys/zfs_context.h> 126789Sahrens #include <sys/arc.h> 127789Sahrens #include <sys/refcount.h> 128789Sahrens #ifdef _KERNEL 129789Sahrens #include <sys/vmsystm.h> 130789Sahrens #include <vm/anon.h> 131789Sahrens #include <sys/fs/swapnode.h> 1321484Sek110237 #include <sys/dnlc.h> 133789Sahrens #endif 134789Sahrens #include <sys/callb.h> 1353403Sbmc #include <sys/kstat.h> 136789Sahrens 137789Sahrens static kmutex_t arc_reclaim_thr_lock; 138789Sahrens static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 139789Sahrens static uint8_t arc_thread_exit; 140789Sahrens 1411484Sek110237 #define ARC_REDUCE_DNLC_PERCENT 3 1421484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 1431484Sek110237 144789Sahrens typedef enum arc_reclaim_strategy { 145789Sahrens ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 146789Sahrens ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 147789Sahrens } arc_reclaim_strategy_t; 148789Sahrens 149789Sahrens /* number of seconds before growing cache again */ 150789Sahrens static int arc_grow_retry = 60; 151789Sahrens 1522391Smaybee /* 1532638Sperrin * minimum lifespan of a prefetch block in clock ticks 1542638Sperrin * (initialized in arc_init()) 1552391Smaybee */ 1562638Sperrin static int arc_min_prefetch_lifespan; 1572391Smaybee 158789Sahrens static int arc_dead; 159789Sahrens 160789Sahrens /* 1612885Sahrens * These tunables are for performance analysis. 1622885Sahrens */ 1632885Sahrens uint64_t zfs_arc_max; 1642885Sahrens uint64_t zfs_arc_min; 1654645Sek110237 uint64_t zfs_arc_meta_limit = 0; 1662885Sahrens 1672885Sahrens /* 1685450Sbrendan * Note that buffers can be in one of 6 states: 169789Sahrens * ARC_anon - anonymous (discussed below) 1701544Seschrock * ARC_mru - recently used, currently cached 1711544Seschrock * ARC_mru_ghost - recentely used, no longer in cache 1721544Seschrock * ARC_mfu - frequently used, currently cached 1731544Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 1745450Sbrendan * ARC_l2c_only - exists in L2ARC but not other states 1754309Smaybee * When there are no active references to the buffer, they are 1764309Smaybee * are linked onto a list in one of these arc states. These are 1774309Smaybee * the only buffers that can be evicted or deleted. Within each 1784309Smaybee * state there are multiple lists, one for meta-data and one for 1794309Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 1804309Smaybee * etc.) is tracked separately so that it can be managed more 1815450Sbrendan * explicitly: favored over data, limited explicitly. 182789Sahrens * 183789Sahrens * Anonymous buffers are buffers that are not associated with 184789Sahrens * a DVA. These are buffers that hold dirty block copies 185789Sahrens * before they are written to stable storage. By definition, 1861544Seschrock * they are "ref'd" and are considered part of arc_mru 187789Sahrens * that cannot be freed. Generally, they will aquire a DVA 1881544Seschrock * as they are written and migrate onto the arc_mru list. 1895450Sbrendan * 1905450Sbrendan * The ARC_l2c_only state is for buffers that are in the second 1915450Sbrendan * level ARC but no longer in any of the ARC_m* lists. The second 1925450Sbrendan * level ARC itself may also contain buffers that are in any of 1935450Sbrendan * the ARC_m* states - meaning that a buffer can exist in two 1945450Sbrendan * places. The reason for the ARC_l2c_only state is to keep the 1955450Sbrendan * buffer header in the hash table, so that reads that hit the 1965450Sbrendan * second level ARC benefit from these fast lookups. 197789Sahrens */ 198789Sahrens 199789Sahrens typedef struct arc_state { 2004309Smaybee list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */ 2014309Smaybee uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 2024309Smaybee uint64_t arcs_size; /* total amount of data in this state */ 2033403Sbmc kmutex_t arcs_mtx; 204789Sahrens } arc_state_t; 205789Sahrens 2065450Sbrendan /* The 6 states: */ 207789Sahrens static arc_state_t ARC_anon; 2081544Seschrock static arc_state_t ARC_mru; 2091544Seschrock static arc_state_t ARC_mru_ghost; 2101544Seschrock static arc_state_t ARC_mfu; 2111544Seschrock static arc_state_t ARC_mfu_ghost; 2125450Sbrendan static arc_state_t ARC_l2c_only; 213789Sahrens 2143403Sbmc typedef struct arc_stats { 2153403Sbmc kstat_named_t arcstat_hits; 2163403Sbmc kstat_named_t arcstat_misses; 2173403Sbmc kstat_named_t arcstat_demand_data_hits; 2183403Sbmc kstat_named_t arcstat_demand_data_misses; 2193403Sbmc kstat_named_t arcstat_demand_metadata_hits; 2203403Sbmc kstat_named_t arcstat_demand_metadata_misses; 2213403Sbmc kstat_named_t arcstat_prefetch_data_hits; 2223403Sbmc kstat_named_t arcstat_prefetch_data_misses; 2233403Sbmc kstat_named_t arcstat_prefetch_metadata_hits; 2243403Sbmc kstat_named_t arcstat_prefetch_metadata_misses; 2253403Sbmc kstat_named_t arcstat_mru_hits; 2263403Sbmc kstat_named_t arcstat_mru_ghost_hits; 2273403Sbmc kstat_named_t arcstat_mfu_hits; 2283403Sbmc kstat_named_t arcstat_mfu_ghost_hits; 2293403Sbmc kstat_named_t arcstat_deleted; 2303403Sbmc kstat_named_t arcstat_recycle_miss; 2313403Sbmc kstat_named_t arcstat_mutex_miss; 2323403Sbmc kstat_named_t arcstat_evict_skip; 2333403Sbmc kstat_named_t arcstat_hash_elements; 2343403Sbmc kstat_named_t arcstat_hash_elements_max; 2353403Sbmc kstat_named_t arcstat_hash_collisions; 2363403Sbmc kstat_named_t arcstat_hash_chains; 2373403Sbmc kstat_named_t arcstat_hash_chain_max; 2383403Sbmc kstat_named_t arcstat_p; 2393403Sbmc kstat_named_t arcstat_c; 2403403Sbmc kstat_named_t arcstat_c_min; 2413403Sbmc kstat_named_t arcstat_c_max; 2423403Sbmc kstat_named_t arcstat_size; 2435450Sbrendan kstat_named_t arcstat_hdr_size; 2445450Sbrendan kstat_named_t arcstat_l2_hits; 2455450Sbrendan kstat_named_t arcstat_l2_misses; 2465450Sbrendan kstat_named_t arcstat_l2_feeds; 2475450Sbrendan kstat_named_t arcstat_l2_rw_clash; 2485450Sbrendan kstat_named_t arcstat_l2_writes_sent; 2495450Sbrendan kstat_named_t arcstat_l2_writes_done; 2505450Sbrendan kstat_named_t arcstat_l2_writes_error; 2515450Sbrendan kstat_named_t arcstat_l2_writes_hdr_miss; 2525450Sbrendan kstat_named_t arcstat_l2_evict_lock_retry; 2535450Sbrendan kstat_named_t arcstat_l2_evict_reading; 2545450Sbrendan kstat_named_t arcstat_l2_free_on_write; 2555450Sbrendan kstat_named_t arcstat_l2_abort_lowmem; 2565450Sbrendan kstat_named_t arcstat_l2_cksum_bad; 2575450Sbrendan kstat_named_t arcstat_l2_io_error; 2585450Sbrendan kstat_named_t arcstat_l2_size; 2595450Sbrendan kstat_named_t arcstat_l2_hdr_size; 2603403Sbmc } arc_stats_t; 2613403Sbmc 2623403Sbmc static arc_stats_t arc_stats = { 2633403Sbmc { "hits", KSTAT_DATA_UINT64 }, 2643403Sbmc { "misses", KSTAT_DATA_UINT64 }, 2653403Sbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 2663403Sbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 2673403Sbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 2683403Sbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 2693403Sbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 2703403Sbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 2713403Sbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 2723403Sbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 2733403Sbmc { "mru_hits", KSTAT_DATA_UINT64 }, 2743403Sbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 2753403Sbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 2763403Sbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 2773403Sbmc { "deleted", KSTAT_DATA_UINT64 }, 2783403Sbmc { "recycle_miss", KSTAT_DATA_UINT64 }, 2793403Sbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 2803403Sbmc { "evict_skip", KSTAT_DATA_UINT64 }, 2813403Sbmc { "hash_elements", KSTAT_DATA_UINT64 }, 2823403Sbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 2833403Sbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 2843403Sbmc { "hash_chains", KSTAT_DATA_UINT64 }, 2853403Sbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 2863403Sbmc { "p", KSTAT_DATA_UINT64 }, 2873403Sbmc { "c", KSTAT_DATA_UINT64 }, 2883403Sbmc { "c_min", KSTAT_DATA_UINT64 }, 2893403Sbmc { "c_max", KSTAT_DATA_UINT64 }, 2905450Sbrendan { "size", KSTAT_DATA_UINT64 }, 2915450Sbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 2925450Sbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 2935450Sbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 2945450Sbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 2955450Sbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 2965450Sbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 2975450Sbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 2985450Sbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 2995450Sbrendan { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 3005450Sbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 3015450Sbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 3025450Sbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 3035450Sbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 3045450Sbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 3055450Sbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 3065450Sbrendan { "l2_size", KSTAT_DATA_UINT64 }, 3075450Sbrendan { "l2_hdr_size", KSTAT_DATA_UINT64 } 3083403Sbmc }; 309789Sahrens 3103403Sbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 3113403Sbmc 3123403Sbmc #define ARCSTAT_INCR(stat, val) \ 3133403Sbmc atomic_add_64(&arc_stats.stat.value.ui64, (val)); 3143403Sbmc 3153403Sbmc #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 3163403Sbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 3173403Sbmc 3183403Sbmc #define ARCSTAT_MAX(stat, val) { \ 3193403Sbmc uint64_t m; \ 3203403Sbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 3213403Sbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 3223403Sbmc continue; \ 3233403Sbmc } 3243403Sbmc 3253403Sbmc #define ARCSTAT_MAXSTAT(stat) \ 3263403Sbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 327789Sahrens 3283403Sbmc /* 3293403Sbmc * We define a macro to allow ARC hits/misses to be easily broken down by 3303403Sbmc * two separate conditions, giving a total of four different subtypes for 3313403Sbmc * each of hits and misses (so eight statistics total). 3323403Sbmc */ 3333403Sbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 3343403Sbmc if (cond1) { \ 3353403Sbmc if (cond2) { \ 3363403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 3373403Sbmc } else { \ 3383403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 3393403Sbmc } \ 3403403Sbmc } else { \ 3413403Sbmc if (cond2) { \ 3423403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 3433403Sbmc } else { \ 3443403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 3453403Sbmc } \ 3463403Sbmc } 347789Sahrens 3483403Sbmc kstat_t *arc_ksp; 3493403Sbmc static arc_state_t *arc_anon; 3503403Sbmc static arc_state_t *arc_mru; 3513403Sbmc static arc_state_t *arc_mru_ghost; 3523403Sbmc static arc_state_t *arc_mfu; 3533403Sbmc static arc_state_t *arc_mfu_ghost; 3545450Sbrendan static arc_state_t *arc_l2c_only; 3553403Sbmc 3563403Sbmc /* 3573403Sbmc * There are several ARC variables that are critical to export as kstats -- 3583403Sbmc * but we don't want to have to grovel around in the kstat whenever we wish to 3593403Sbmc * manipulate them. For these variables, we therefore define them to be in 3603403Sbmc * terms of the statistic variable. This assures that we are not introducing 3613403Sbmc * the possibility of inconsistency by having shadow copies of the variables, 3623403Sbmc * while still allowing the code to be readable. 3633403Sbmc */ 3643403Sbmc #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 3653403Sbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 3663403Sbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 3673403Sbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 3683403Sbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 3693403Sbmc 3703403Sbmc static int arc_no_grow; /* Don't try to grow cache size */ 3713403Sbmc static uint64_t arc_tempreserve; 3724309Smaybee static uint64_t arc_meta_used; 3734309Smaybee static uint64_t arc_meta_limit; 3744309Smaybee static uint64_t arc_meta_max = 0; 375789Sahrens 3765450Sbrendan typedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 3775450Sbrendan 378789Sahrens typedef struct arc_callback arc_callback_t; 379789Sahrens 380789Sahrens struct arc_callback { 3813547Smaybee void *acb_private; 382789Sahrens arc_done_func_t *acb_done; 383789Sahrens arc_byteswap_func_t *acb_byteswap; 384789Sahrens arc_buf_t *acb_buf; 385789Sahrens zio_t *acb_zio_dummy; 386789Sahrens arc_callback_t *acb_next; 387789Sahrens }; 388789Sahrens 3893547Smaybee typedef struct arc_write_callback arc_write_callback_t; 3903547Smaybee 3913547Smaybee struct arc_write_callback { 3923547Smaybee void *awcb_private; 3933547Smaybee arc_done_func_t *awcb_ready; 3943547Smaybee arc_done_func_t *awcb_done; 3953547Smaybee arc_buf_t *awcb_buf; 3963547Smaybee }; 3973547Smaybee 398789Sahrens struct arc_buf_hdr { 399789Sahrens /* protected by hash lock */ 400789Sahrens dva_t b_dva; 401789Sahrens uint64_t b_birth; 402789Sahrens uint64_t b_cksum0; 403789Sahrens 4043093Sahrens kmutex_t b_freeze_lock; 4053093Sahrens zio_cksum_t *b_freeze_cksum; 4063093Sahrens 407789Sahrens arc_buf_hdr_t *b_hash_next; 408789Sahrens arc_buf_t *b_buf; 409789Sahrens uint32_t b_flags; 4101544Seschrock uint32_t b_datacnt; 411789Sahrens 4123290Sjohansen arc_callback_t *b_acb; 413789Sahrens kcondvar_t b_cv; 4143290Sjohansen 4153290Sjohansen /* immutable */ 4163290Sjohansen arc_buf_contents_t b_type; 4173290Sjohansen uint64_t b_size; 4183290Sjohansen spa_t *b_spa; 419789Sahrens 420789Sahrens /* protected by arc state mutex */ 421789Sahrens arc_state_t *b_state; 422789Sahrens list_node_t b_arc_node; 423789Sahrens 424789Sahrens /* updated atomically */ 425789Sahrens clock_t b_arc_access; 426789Sahrens 427789Sahrens /* self protecting */ 428789Sahrens refcount_t b_refcnt; 4295450Sbrendan 4305450Sbrendan l2arc_buf_hdr_t *b_l2hdr; 4315450Sbrendan list_node_t b_l2node; 432789Sahrens }; 433789Sahrens 4341544Seschrock static arc_buf_t *arc_eviction_list; 4351544Seschrock static kmutex_t arc_eviction_mtx; 4362887Smaybee static arc_buf_hdr_t arc_eviction_hdr; 4372688Smaybee static void arc_get_data_buf(arc_buf_t *buf); 4382688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 4394309Smaybee static int arc_evict_needed(arc_buf_contents_t type); 440*5642Smaybee static void arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes); 4411544Seschrock 4421544Seschrock #define GHOST_STATE(state) \ 4435450Sbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 4445450Sbrendan (state) == arc_l2c_only) 4451544Seschrock 446789Sahrens /* 447789Sahrens * Private ARC flags. These flags are private ARC only flags that will show up 448789Sahrens * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 449789Sahrens * be passed in as arc_flags in things like arc_read. However, these flags 450789Sahrens * should never be passed and should only be set by ARC code. When adding new 451789Sahrens * public flags, make sure not to smash the private ones. 452789Sahrens */ 453789Sahrens 4541544Seschrock #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 455789Sahrens #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 456789Sahrens #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 457789Sahrens #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 4581544Seschrock #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 4592391Smaybee #define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 4605450Sbrendan #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 4615450Sbrendan #define ARC_DONT_L2CACHE (1 << 16) /* originated by prefetch */ 4625450Sbrendan #define ARC_L2_READING (1 << 17) /* L2ARC read in progress */ 4635450Sbrendan #define ARC_L2_WRITING (1 << 18) /* L2ARC write in progress */ 4645450Sbrendan #define ARC_L2_EVICTED (1 << 19) /* evicted during I/O */ 4655450Sbrendan #define ARC_L2_WRITE_HEAD (1 << 20) /* head of write list */ 466789Sahrens 4671544Seschrock #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 468789Sahrens #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 469789Sahrens #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 470789Sahrens #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 4711544Seschrock #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 4725450Sbrendan #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 4735450Sbrendan #define HDR_DONT_L2CACHE(hdr) ((hdr)->b_flags & ARC_DONT_L2CACHE) 4745450Sbrendan #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_L2_READING) 4755450Sbrendan #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 4765450Sbrendan #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 4775450Sbrendan #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 478789Sahrens 479789Sahrens /* 480789Sahrens * Hash table routines 481789Sahrens */ 482789Sahrens 483789Sahrens #define HT_LOCK_PAD 64 484789Sahrens 485789Sahrens struct ht_lock { 486789Sahrens kmutex_t ht_lock; 487789Sahrens #ifdef _KERNEL 488789Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 489789Sahrens #endif 490789Sahrens }; 491789Sahrens 492789Sahrens #define BUF_LOCKS 256 493789Sahrens typedef struct buf_hash_table { 494789Sahrens uint64_t ht_mask; 495789Sahrens arc_buf_hdr_t **ht_table; 496789Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 497789Sahrens } buf_hash_table_t; 498789Sahrens 499789Sahrens static buf_hash_table_t buf_hash_table; 500789Sahrens 501789Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 502789Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 503789Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 504789Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 505789Sahrens #define HDR_LOCK(buf) \ 506789Sahrens (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth))) 507789Sahrens 508789Sahrens uint64_t zfs_crc64_table[256]; 509789Sahrens 5105450Sbrendan /* 5115450Sbrendan * Level 2 ARC 5125450Sbrendan */ 5135450Sbrendan 5145450Sbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 5155450Sbrendan #define L2ARC_HEADROOM 4 /* num of writes */ 5165450Sbrendan #define L2ARC_FEED_DELAY 180 /* starting grace */ 5175450Sbrendan #define L2ARC_FEED_SECS 1 /* caching interval */ 5185450Sbrendan 5195450Sbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 5205450Sbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 5215450Sbrendan 5225450Sbrendan /* 5235450Sbrendan * L2ARC Performance Tunables 5245450Sbrendan */ 5255450Sbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 5265450Sbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 5275450Sbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 5285450Sbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 5295450Sbrendan 5305450Sbrendan /* 5315450Sbrendan * L2ARC Internals 5325450Sbrendan */ 5335450Sbrendan typedef struct l2arc_dev { 5345450Sbrendan vdev_t *l2ad_vdev; /* vdev */ 5355450Sbrendan spa_t *l2ad_spa; /* spa */ 5365450Sbrendan uint64_t l2ad_hand; /* next write location */ 5375450Sbrendan uint64_t l2ad_write; /* desired write size, bytes */ 5385450Sbrendan uint64_t l2ad_start; /* first addr on device */ 5395450Sbrendan uint64_t l2ad_end; /* last addr on device */ 5405450Sbrendan uint64_t l2ad_evict; /* last addr eviction reached */ 5415450Sbrendan boolean_t l2ad_first; /* first sweep through */ 5425450Sbrendan list_t *l2ad_buflist; /* buffer list */ 5435450Sbrendan list_node_t l2ad_node; /* device list node */ 5445450Sbrendan } l2arc_dev_t; 5455450Sbrendan 5465450Sbrendan static list_t L2ARC_dev_list; /* device list */ 5475450Sbrendan static list_t *l2arc_dev_list; /* device list pointer */ 5485450Sbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 5495450Sbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 5505450Sbrendan static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 5515450Sbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 5525450Sbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 5535450Sbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 5545450Sbrendan static uint64_t l2arc_ndev; /* number of devices */ 5555450Sbrendan 5565450Sbrendan typedef struct l2arc_read_callback { 5575450Sbrendan arc_buf_t *l2rcb_buf; /* read buffer */ 5585450Sbrendan spa_t *l2rcb_spa; /* spa */ 5595450Sbrendan blkptr_t l2rcb_bp; /* original blkptr */ 5605450Sbrendan zbookmark_t l2rcb_zb; /* original bookmark */ 5615450Sbrendan int l2rcb_flags; /* original flags */ 5625450Sbrendan } l2arc_read_callback_t; 5635450Sbrendan 5645450Sbrendan typedef struct l2arc_write_callback { 5655450Sbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 5665450Sbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 5675450Sbrendan } l2arc_write_callback_t; 5685450Sbrendan 5695450Sbrendan struct l2arc_buf_hdr { 5705450Sbrendan /* protected by arc_buf_hdr mutex */ 5715450Sbrendan l2arc_dev_t *b_dev; /* L2ARC device */ 5725450Sbrendan daddr_t b_daddr; /* disk address, offset byte */ 5735450Sbrendan }; 5745450Sbrendan 5755450Sbrendan typedef struct l2arc_data_free { 5765450Sbrendan /* protected by l2arc_free_on_write_mtx */ 5775450Sbrendan void *l2df_data; 5785450Sbrendan size_t l2df_size; 5795450Sbrendan void (*l2df_func)(void *, size_t); 5805450Sbrendan list_node_t l2df_list_node; 5815450Sbrendan } l2arc_data_free_t; 5825450Sbrendan 5835450Sbrendan static kmutex_t l2arc_feed_thr_lock; 5845450Sbrendan static kcondvar_t l2arc_feed_thr_cv; 5855450Sbrendan static uint8_t l2arc_thread_exit; 5865450Sbrendan 5875450Sbrendan static void l2arc_read_done(zio_t *zio); 5885450Sbrendan static void l2arc_hdr_stat_add(void); 5895450Sbrendan static void l2arc_hdr_stat_remove(void); 5905450Sbrendan 591789Sahrens static uint64_t 592789Sahrens buf_hash(spa_t *spa, dva_t *dva, uint64_t birth) 593789Sahrens { 594789Sahrens uintptr_t spav = (uintptr_t)spa; 595789Sahrens uint8_t *vdva = (uint8_t *)dva; 596789Sahrens uint64_t crc = -1ULL; 597789Sahrens int i; 598789Sahrens 599789Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 600789Sahrens 601789Sahrens for (i = 0; i < sizeof (dva_t); i++) 602789Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 603789Sahrens 604789Sahrens crc ^= (spav>>8) ^ birth; 605789Sahrens 606789Sahrens return (crc); 607789Sahrens } 608789Sahrens 609789Sahrens #define BUF_EMPTY(buf) \ 610789Sahrens ((buf)->b_dva.dva_word[0] == 0 && \ 611789Sahrens (buf)->b_dva.dva_word[1] == 0 && \ 612789Sahrens (buf)->b_birth == 0) 613789Sahrens 614789Sahrens #define BUF_EQUAL(spa, dva, birth, buf) \ 615789Sahrens ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 616789Sahrens ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 617789Sahrens ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 618789Sahrens 619789Sahrens static arc_buf_hdr_t * 620789Sahrens buf_hash_find(spa_t *spa, dva_t *dva, uint64_t birth, kmutex_t **lockp) 621789Sahrens { 622789Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 623789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 624789Sahrens arc_buf_hdr_t *buf; 625789Sahrens 626789Sahrens mutex_enter(hash_lock); 627789Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 628789Sahrens buf = buf->b_hash_next) { 629789Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 630789Sahrens *lockp = hash_lock; 631789Sahrens return (buf); 632789Sahrens } 633789Sahrens } 634789Sahrens mutex_exit(hash_lock); 635789Sahrens *lockp = NULL; 636789Sahrens return (NULL); 637789Sahrens } 638789Sahrens 639789Sahrens /* 640789Sahrens * Insert an entry into the hash table. If there is already an element 641789Sahrens * equal to elem in the hash table, then the already existing element 642789Sahrens * will be returned and the new element will not be inserted. 643789Sahrens * Otherwise returns NULL. 644789Sahrens */ 645789Sahrens static arc_buf_hdr_t * 646789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 647789Sahrens { 648789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 649789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 650789Sahrens arc_buf_hdr_t *fbuf; 6513403Sbmc uint32_t i; 652789Sahrens 6531544Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 654789Sahrens *lockp = hash_lock; 655789Sahrens mutex_enter(hash_lock); 656789Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 657789Sahrens fbuf = fbuf->b_hash_next, i++) { 658789Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 659789Sahrens return (fbuf); 660789Sahrens } 661789Sahrens 662789Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 663789Sahrens buf_hash_table.ht_table[idx] = buf; 6641544Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 665789Sahrens 666789Sahrens /* collect some hash table performance data */ 667789Sahrens if (i > 0) { 6683403Sbmc ARCSTAT_BUMP(arcstat_hash_collisions); 669789Sahrens if (i == 1) 6703403Sbmc ARCSTAT_BUMP(arcstat_hash_chains); 6713403Sbmc 6723403Sbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 673789Sahrens } 6743403Sbmc 6753403Sbmc ARCSTAT_BUMP(arcstat_hash_elements); 6763403Sbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 677789Sahrens 678789Sahrens return (NULL); 679789Sahrens } 680789Sahrens 681789Sahrens static void 682789Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 683789Sahrens { 684789Sahrens arc_buf_hdr_t *fbuf, **bufp; 685789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 686789Sahrens 687789Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 6881544Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 689789Sahrens 690789Sahrens bufp = &buf_hash_table.ht_table[idx]; 691789Sahrens while ((fbuf = *bufp) != buf) { 692789Sahrens ASSERT(fbuf != NULL); 693789Sahrens bufp = &fbuf->b_hash_next; 694789Sahrens } 695789Sahrens *bufp = buf->b_hash_next; 696789Sahrens buf->b_hash_next = NULL; 6971544Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 698789Sahrens 699789Sahrens /* collect some hash table performance data */ 7003403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 7013403Sbmc 702789Sahrens if (buf_hash_table.ht_table[idx] && 703789Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 7043403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 705789Sahrens } 706789Sahrens 707789Sahrens /* 708789Sahrens * Global data structures and functions for the buf kmem cache. 709789Sahrens */ 710789Sahrens static kmem_cache_t *hdr_cache; 711789Sahrens static kmem_cache_t *buf_cache; 712789Sahrens 713789Sahrens static void 714789Sahrens buf_fini(void) 715789Sahrens { 716789Sahrens int i; 717789Sahrens 718789Sahrens kmem_free(buf_hash_table.ht_table, 719789Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 720789Sahrens for (i = 0; i < BUF_LOCKS; i++) 721789Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 722789Sahrens kmem_cache_destroy(hdr_cache); 723789Sahrens kmem_cache_destroy(buf_cache); 724789Sahrens } 725789Sahrens 726789Sahrens /* 727789Sahrens * Constructor callback - called when the cache is empty 728789Sahrens * and a new buf is requested. 729789Sahrens */ 730789Sahrens /* ARGSUSED */ 731789Sahrens static int 732789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 733789Sahrens { 734789Sahrens arc_buf_hdr_t *buf = vbuf; 735789Sahrens 736789Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 737789Sahrens refcount_create(&buf->b_refcnt); 738789Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 7394831Sgw25295 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 7405450Sbrendan 7415450Sbrendan ARCSTAT_INCR(arcstat_hdr_size, sizeof (arc_buf_hdr_t)); 742789Sahrens return (0); 743789Sahrens } 744789Sahrens 745789Sahrens /* 746789Sahrens * Destructor callback - called when a cached buf is 747789Sahrens * no longer required. 748789Sahrens */ 749789Sahrens /* ARGSUSED */ 750789Sahrens static void 751789Sahrens hdr_dest(void *vbuf, void *unused) 752789Sahrens { 753789Sahrens arc_buf_hdr_t *buf = vbuf; 754789Sahrens 755789Sahrens refcount_destroy(&buf->b_refcnt); 756789Sahrens cv_destroy(&buf->b_cv); 7574831Sgw25295 mutex_destroy(&buf->b_freeze_lock); 7585450Sbrendan 7595450Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -sizeof (arc_buf_hdr_t)); 760789Sahrens } 761789Sahrens 762789Sahrens /* 763789Sahrens * Reclaim callback -- invoked when memory is low. 764789Sahrens */ 765789Sahrens /* ARGSUSED */ 766789Sahrens static void 767789Sahrens hdr_recl(void *unused) 768789Sahrens { 769789Sahrens dprintf("hdr_recl called\n"); 7703158Smaybee /* 7713158Smaybee * umem calls the reclaim func when we destroy the buf cache, 7723158Smaybee * which is after we do arc_fini(). 7733158Smaybee */ 7743158Smaybee if (!arc_dead) 7753158Smaybee cv_signal(&arc_reclaim_thr_cv); 776789Sahrens } 777789Sahrens 778789Sahrens static void 779789Sahrens buf_init(void) 780789Sahrens { 781789Sahrens uint64_t *ct; 7821544Seschrock uint64_t hsize = 1ULL << 12; 783789Sahrens int i, j; 784789Sahrens 785789Sahrens /* 786789Sahrens * The hash table is big enough to fill all of physical memory 7871544Seschrock * with an average 64K block size. The table will take up 7881544Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 789789Sahrens */ 7901544Seschrock while (hsize * 65536 < physmem * PAGESIZE) 791789Sahrens hsize <<= 1; 7921544Seschrock retry: 793789Sahrens buf_hash_table.ht_mask = hsize - 1; 7941544Seschrock buf_hash_table.ht_table = 7951544Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 7961544Seschrock if (buf_hash_table.ht_table == NULL) { 7971544Seschrock ASSERT(hsize > (1ULL << 8)); 7981544Seschrock hsize >>= 1; 7991544Seschrock goto retry; 8001544Seschrock } 801789Sahrens 802789Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 803789Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 804789Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 805789Sahrens 0, NULL, NULL, NULL, NULL, NULL, 0); 806789Sahrens 807789Sahrens for (i = 0; i < 256; i++) 808789Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 809789Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 810789Sahrens 811789Sahrens for (i = 0; i < BUF_LOCKS; i++) { 812789Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 813789Sahrens NULL, MUTEX_DEFAULT, NULL); 814789Sahrens } 815789Sahrens } 816789Sahrens 817789Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 818789Sahrens 819789Sahrens static void 8203093Sahrens arc_cksum_verify(arc_buf_t *buf) 8213093Sahrens { 8223093Sahrens zio_cksum_t zc; 8233093Sahrens 8243312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 8253093Sahrens return; 8263093Sahrens 8273093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8283265Sahrens if (buf->b_hdr->b_freeze_cksum == NULL || 8293265Sahrens (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 8303093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8313093Sahrens return; 8323093Sahrens } 8333093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8343093Sahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 8353093Sahrens panic("buffer modified while frozen!"); 8363093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8373093Sahrens } 8383093Sahrens 8395450Sbrendan static int 8405450Sbrendan arc_cksum_equal(arc_buf_t *buf) 8415450Sbrendan { 8425450Sbrendan zio_cksum_t zc; 8435450Sbrendan int equal; 8445450Sbrendan 8455450Sbrendan mutex_enter(&buf->b_hdr->b_freeze_lock); 8465450Sbrendan fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8475450Sbrendan equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 8485450Sbrendan mutex_exit(&buf->b_hdr->b_freeze_lock); 8495450Sbrendan 8505450Sbrendan return (equal); 8515450Sbrendan } 8525450Sbrendan 8533093Sahrens static void 8545450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force) 8553093Sahrens { 8565450Sbrendan if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 8573093Sahrens return; 8583093Sahrens 8593093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8603093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 8613093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8623093Sahrens return; 8633093Sahrens } 8643093Sahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 8653093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 8663093Sahrens buf->b_hdr->b_freeze_cksum); 8673093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8683093Sahrens } 8693093Sahrens 8703093Sahrens void 8713093Sahrens arc_buf_thaw(arc_buf_t *buf) 8723093Sahrens { 8735450Sbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 8745450Sbrendan if (buf->b_hdr->b_state != arc_anon) 8755450Sbrendan panic("modifying non-anon buffer!"); 8765450Sbrendan if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 8775450Sbrendan panic("modifying buffer while i/o in progress!"); 8785450Sbrendan arc_cksum_verify(buf); 8795450Sbrendan } 8805450Sbrendan 8813093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8823093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 8833093Sahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 8843093Sahrens buf->b_hdr->b_freeze_cksum = NULL; 8853093Sahrens } 8863093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8873093Sahrens } 8883093Sahrens 8893093Sahrens void 8903093Sahrens arc_buf_freeze(arc_buf_t *buf) 8913093Sahrens { 8923312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 8933312Sahrens return; 8943312Sahrens 8953093Sahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 8963403Sbmc buf->b_hdr->b_state == arc_anon); 8975450Sbrendan arc_cksum_compute(buf, B_FALSE); 8983093Sahrens } 8993093Sahrens 9003093Sahrens static void 901789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 902789Sahrens { 903789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 904789Sahrens 905789Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 9063403Sbmc (ab->b_state != arc_anon)) { 9073700Sek110237 uint64_t delta = ab->b_size * ab->b_datacnt; 9084309Smaybee list_t *list = &ab->b_state->arcs_list[ab->b_type]; 9094309Smaybee uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 910789Sahrens 9113403Sbmc ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx)); 9123403Sbmc mutex_enter(&ab->b_state->arcs_mtx); 913789Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 9144309Smaybee list_remove(list, ab); 9151544Seschrock if (GHOST_STATE(ab->b_state)) { 9161544Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 9171544Seschrock ASSERT3P(ab->b_buf, ==, NULL); 9181544Seschrock delta = ab->b_size; 9191544Seschrock } 9201544Seschrock ASSERT(delta > 0); 9214309Smaybee ASSERT3U(*size, >=, delta); 9224309Smaybee atomic_add_64(size, -delta); 9233403Sbmc mutex_exit(&ab->b_state->arcs_mtx); 9242391Smaybee /* remove the prefetch flag is we get a reference */ 9252391Smaybee if (ab->b_flags & ARC_PREFETCH) 9262391Smaybee ab->b_flags &= ~ARC_PREFETCH; 927789Sahrens } 928789Sahrens } 929789Sahrens 930789Sahrens static int 931789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 932789Sahrens { 933789Sahrens int cnt; 9343403Sbmc arc_state_t *state = ab->b_state; 935789Sahrens 9363403Sbmc ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 9373403Sbmc ASSERT(!GHOST_STATE(state)); 938789Sahrens 939789Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 9403403Sbmc (state != arc_anon)) { 9414309Smaybee uint64_t *size = &state->arcs_lsize[ab->b_type]; 9424309Smaybee 9433403Sbmc ASSERT(!MUTEX_HELD(&state->arcs_mtx)); 9443403Sbmc mutex_enter(&state->arcs_mtx); 945789Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 9464309Smaybee list_insert_head(&state->arcs_list[ab->b_type], ab); 9471544Seschrock ASSERT(ab->b_datacnt > 0); 9484309Smaybee atomic_add_64(size, ab->b_size * ab->b_datacnt); 9493403Sbmc mutex_exit(&state->arcs_mtx); 950789Sahrens } 951789Sahrens return (cnt); 952789Sahrens } 953789Sahrens 954789Sahrens /* 955789Sahrens * Move the supplied buffer to the indicated state. The mutex 956789Sahrens * for the buffer must be held by the caller. 957789Sahrens */ 958789Sahrens static void 9591544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 960789Sahrens { 9611544Seschrock arc_state_t *old_state = ab->b_state; 9623700Sek110237 int64_t refcnt = refcount_count(&ab->b_refcnt); 9633700Sek110237 uint64_t from_delta, to_delta; 964789Sahrens 965789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 9661544Seschrock ASSERT(new_state != old_state); 9671544Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 9681544Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 9691544Seschrock 9701544Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 971789Sahrens 972789Sahrens /* 973789Sahrens * If this buffer is evictable, transfer it from the 974789Sahrens * old state list to the new state list. 975789Sahrens */ 9761544Seschrock if (refcnt == 0) { 9773403Sbmc if (old_state != arc_anon) { 9783403Sbmc int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx); 9794309Smaybee uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 9801544Seschrock 9811544Seschrock if (use_mutex) 9823403Sbmc mutex_enter(&old_state->arcs_mtx); 9831544Seschrock 9841544Seschrock ASSERT(list_link_active(&ab->b_arc_node)); 9854309Smaybee list_remove(&old_state->arcs_list[ab->b_type], ab); 986789Sahrens 9872391Smaybee /* 9882391Smaybee * If prefetching out of the ghost cache, 9892391Smaybee * we will have a non-null datacnt. 9902391Smaybee */ 9912391Smaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 9922391Smaybee /* ghost elements have a ghost size */ 9931544Seschrock ASSERT(ab->b_buf == NULL); 9941544Seschrock from_delta = ab->b_size; 995789Sahrens } 9964309Smaybee ASSERT3U(*size, >=, from_delta); 9974309Smaybee atomic_add_64(size, -from_delta); 9981544Seschrock 9991544Seschrock if (use_mutex) 10003403Sbmc mutex_exit(&old_state->arcs_mtx); 1001789Sahrens } 10023403Sbmc if (new_state != arc_anon) { 10033403Sbmc int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx); 10044309Smaybee uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1005789Sahrens 10061544Seschrock if (use_mutex) 10073403Sbmc mutex_enter(&new_state->arcs_mtx); 10081544Seschrock 10094309Smaybee list_insert_head(&new_state->arcs_list[ab->b_type], ab); 10101544Seschrock 10111544Seschrock /* ghost elements have a ghost size */ 10121544Seschrock if (GHOST_STATE(new_state)) { 10131544Seschrock ASSERT(ab->b_datacnt == 0); 10141544Seschrock ASSERT(ab->b_buf == NULL); 10151544Seschrock to_delta = ab->b_size; 10161544Seschrock } 10174309Smaybee atomic_add_64(size, to_delta); 10181544Seschrock 10191544Seschrock if (use_mutex) 10203403Sbmc mutex_exit(&new_state->arcs_mtx); 1021789Sahrens } 1022789Sahrens } 1023789Sahrens 1024789Sahrens ASSERT(!BUF_EMPTY(ab)); 10255450Sbrendan if (new_state == arc_anon) { 1026789Sahrens buf_hash_remove(ab); 1027789Sahrens } 1028789Sahrens 10291544Seschrock /* adjust state sizes */ 10301544Seschrock if (to_delta) 10313403Sbmc atomic_add_64(&new_state->arcs_size, to_delta); 10321544Seschrock if (from_delta) { 10333403Sbmc ASSERT3U(old_state->arcs_size, >=, from_delta); 10343403Sbmc atomic_add_64(&old_state->arcs_size, -from_delta); 1035789Sahrens } 1036789Sahrens ab->b_state = new_state; 10375450Sbrendan 10385450Sbrendan /* adjust l2arc hdr stats */ 10395450Sbrendan if (new_state == arc_l2c_only) 10405450Sbrendan l2arc_hdr_stat_add(); 10415450Sbrendan else if (old_state == arc_l2c_only) 10425450Sbrendan l2arc_hdr_stat_remove(); 1043789Sahrens } 1044789Sahrens 10454309Smaybee void 10464309Smaybee arc_space_consume(uint64_t space) 10474309Smaybee { 10484309Smaybee atomic_add_64(&arc_meta_used, space); 10494309Smaybee atomic_add_64(&arc_size, space); 10504309Smaybee } 10514309Smaybee 10524309Smaybee void 10534309Smaybee arc_space_return(uint64_t space) 10544309Smaybee { 10554309Smaybee ASSERT(arc_meta_used >= space); 10564309Smaybee if (arc_meta_max < arc_meta_used) 10574309Smaybee arc_meta_max = arc_meta_used; 10584309Smaybee atomic_add_64(&arc_meta_used, -space); 10594309Smaybee ASSERT(arc_size >= space); 10604309Smaybee atomic_add_64(&arc_size, -space); 10614309Smaybee } 10624309Smaybee 10634309Smaybee void * 10644309Smaybee arc_data_buf_alloc(uint64_t size) 10654309Smaybee { 10664309Smaybee if (arc_evict_needed(ARC_BUFC_DATA)) 10674309Smaybee cv_signal(&arc_reclaim_thr_cv); 10684309Smaybee atomic_add_64(&arc_size, size); 10694309Smaybee return (zio_data_buf_alloc(size)); 10704309Smaybee } 10714309Smaybee 10724309Smaybee void 10734309Smaybee arc_data_buf_free(void *buf, uint64_t size) 10744309Smaybee { 10754309Smaybee zio_data_buf_free(buf, size); 10764309Smaybee ASSERT(arc_size >= size); 10774309Smaybee atomic_add_64(&arc_size, -size); 10784309Smaybee } 10794309Smaybee 1080789Sahrens arc_buf_t * 10813290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1082789Sahrens { 1083789Sahrens arc_buf_hdr_t *hdr; 1084789Sahrens arc_buf_t *buf; 1085789Sahrens 1086789Sahrens ASSERT3U(size, >, 0); 1087789Sahrens hdr = kmem_cache_alloc(hdr_cache, KM_SLEEP); 1088789Sahrens ASSERT(BUF_EMPTY(hdr)); 1089789Sahrens hdr->b_size = size; 10903290Sjohansen hdr->b_type = type; 1091789Sahrens hdr->b_spa = spa; 10923403Sbmc hdr->b_state = arc_anon; 1093789Sahrens hdr->b_arc_access = 0; 1094789Sahrens buf = kmem_cache_alloc(buf_cache, KM_SLEEP); 1095789Sahrens buf->b_hdr = hdr; 10962688Smaybee buf->b_data = NULL; 10971544Seschrock buf->b_efunc = NULL; 10981544Seschrock buf->b_private = NULL; 1099789Sahrens buf->b_next = NULL; 1100789Sahrens hdr->b_buf = buf; 11012688Smaybee arc_get_data_buf(buf); 11021544Seschrock hdr->b_datacnt = 1; 1103789Sahrens hdr->b_flags = 0; 1104789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1105789Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 1106789Sahrens 1107789Sahrens return (buf); 1108789Sahrens } 1109789Sahrens 11102688Smaybee static arc_buf_t * 11112688Smaybee arc_buf_clone(arc_buf_t *from) 11121544Seschrock { 11132688Smaybee arc_buf_t *buf; 11142688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 11152688Smaybee uint64_t size = hdr->b_size; 11161544Seschrock 11172688Smaybee buf = kmem_cache_alloc(buf_cache, KM_SLEEP); 11182688Smaybee buf->b_hdr = hdr; 11192688Smaybee buf->b_data = NULL; 11202688Smaybee buf->b_efunc = NULL; 11212688Smaybee buf->b_private = NULL; 11222688Smaybee buf->b_next = hdr->b_buf; 11232688Smaybee hdr->b_buf = buf; 11242688Smaybee arc_get_data_buf(buf); 11252688Smaybee bcopy(from->b_data, buf->b_data, size); 11262688Smaybee hdr->b_datacnt += 1; 11272688Smaybee return (buf); 11281544Seschrock } 11291544Seschrock 11301544Seschrock void 11311544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 11321544Seschrock { 11332887Smaybee arc_buf_hdr_t *hdr; 11341544Seschrock kmutex_t *hash_lock; 11351544Seschrock 11362724Smaybee /* 11372724Smaybee * Check to see if this buffer is currently being evicted via 11382887Smaybee * arc_do_user_evicts(). 11392724Smaybee */ 11402887Smaybee mutex_enter(&arc_eviction_mtx); 11412887Smaybee hdr = buf->b_hdr; 11422887Smaybee if (hdr == NULL) { 11432887Smaybee mutex_exit(&arc_eviction_mtx); 11442724Smaybee return; 11452887Smaybee } 11462887Smaybee hash_lock = HDR_LOCK(hdr); 11472887Smaybee mutex_exit(&arc_eviction_mtx); 11482724Smaybee 11492724Smaybee mutex_enter(hash_lock); 11501544Seschrock if (buf->b_data == NULL) { 11511544Seschrock /* 11521544Seschrock * This buffer is evicted. 11531544Seschrock */ 11542724Smaybee mutex_exit(hash_lock); 11551544Seschrock return; 11561544Seschrock } 11571544Seschrock 11582724Smaybee ASSERT(buf->b_hdr == hdr); 11593403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 11601544Seschrock add_reference(hdr, hash_lock, tag); 11612688Smaybee arc_access(hdr, hash_lock); 11622688Smaybee mutex_exit(hash_lock); 11633403Sbmc ARCSTAT_BUMP(arcstat_hits); 11643403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 11653403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 11663403Sbmc data, metadata, hits); 11671544Seschrock } 11681544Seschrock 11695450Sbrendan /* 11705450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 11715450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 11725450Sbrendan */ 11735450Sbrendan static void 11745450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 11755450Sbrendan void *data, size_t size) 11765450Sbrendan { 11775450Sbrendan if (HDR_L2_WRITING(hdr)) { 11785450Sbrendan l2arc_data_free_t *df; 11795450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 11805450Sbrendan df->l2df_data = data; 11815450Sbrendan df->l2df_size = size; 11825450Sbrendan df->l2df_func = free_func; 11835450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 11845450Sbrendan list_insert_head(l2arc_free_on_write, df); 11855450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 11865450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 11875450Sbrendan } else { 11885450Sbrendan free_func(data, size); 11895450Sbrendan } 11905450Sbrendan } 11915450Sbrendan 1192789Sahrens static void 11932688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 11941544Seschrock { 11951544Seschrock arc_buf_t **bufp; 11961544Seschrock 11971544Seschrock /* free up data associated with the buf */ 11981544Seschrock if (buf->b_data) { 11991544Seschrock arc_state_t *state = buf->b_hdr->b_state; 12001544Seschrock uint64_t size = buf->b_hdr->b_size; 12013290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 12021544Seschrock 12033093Sahrens arc_cksum_verify(buf); 12042688Smaybee if (!recycle) { 12053290Sjohansen if (type == ARC_BUFC_METADATA) { 12065450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 12075450Sbrendan buf->b_data, size); 12084309Smaybee arc_space_return(size); 12093290Sjohansen } else { 12103290Sjohansen ASSERT(type == ARC_BUFC_DATA); 12115450Sbrendan arc_buf_data_free(buf->b_hdr, 12125450Sbrendan zio_data_buf_free, buf->b_data, size); 12134309Smaybee atomic_add_64(&arc_size, -size); 12143290Sjohansen } 12152688Smaybee } 12161544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 12174309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 12184309Smaybee 12191544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 12203403Sbmc ASSERT(state != arc_anon); 12214309Smaybee 12224309Smaybee ASSERT3U(*cnt, >=, size); 12234309Smaybee atomic_add_64(cnt, -size); 12241544Seschrock } 12253403Sbmc ASSERT3U(state->arcs_size, >=, size); 12263403Sbmc atomic_add_64(&state->arcs_size, -size); 12271544Seschrock buf->b_data = NULL; 12281544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 12291544Seschrock buf->b_hdr->b_datacnt -= 1; 12301544Seschrock } 12311544Seschrock 12321544Seschrock /* only remove the buf if requested */ 12331544Seschrock if (!all) 12341544Seschrock return; 12351544Seschrock 12361544Seschrock /* remove the buf from the hdr list */ 12371544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 12381544Seschrock continue; 12391544Seschrock *bufp = buf->b_next; 12401544Seschrock 12411544Seschrock ASSERT(buf->b_efunc == NULL); 12421544Seschrock 12431544Seschrock /* clean up the buf */ 12441544Seschrock buf->b_hdr = NULL; 12451544Seschrock kmem_cache_free(buf_cache, buf); 12461544Seschrock } 12471544Seschrock 12481544Seschrock static void 12491544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1250789Sahrens { 1251789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 12523403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 12531544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1254789Sahrens 12555450Sbrendan if (hdr->b_l2hdr != NULL) { 12565450Sbrendan if (!MUTEX_HELD(&l2arc_buflist_mtx)) { 12575450Sbrendan /* 12585450Sbrendan * To prevent arc_free() and l2arc_evict() from 12595450Sbrendan * attempting to free the same buffer at the same time, 12605450Sbrendan * a FREE_IN_PROGRESS flag is given to arc_free() to 12615450Sbrendan * give it priority. l2arc_evict() can't destroy this 12625450Sbrendan * header while we are waiting on l2arc_buflist_mtx. 12635450Sbrendan */ 12645450Sbrendan mutex_enter(&l2arc_buflist_mtx); 12655450Sbrendan ASSERT(hdr->b_l2hdr != NULL); 12665450Sbrendan 12675450Sbrendan list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr); 12685450Sbrendan mutex_exit(&l2arc_buflist_mtx); 12695450Sbrendan } else { 12705450Sbrendan list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr); 12715450Sbrendan } 12725450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 12735450Sbrendan kmem_free(hdr->b_l2hdr, sizeof (l2arc_buf_hdr_t)); 12745450Sbrendan if (hdr->b_state == arc_l2c_only) 12755450Sbrendan l2arc_hdr_stat_remove(); 12765450Sbrendan hdr->b_l2hdr = NULL; 12775450Sbrendan } 12785450Sbrendan 1279789Sahrens if (!BUF_EMPTY(hdr)) { 12801544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1281789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1282789Sahrens hdr->b_birth = 0; 1283789Sahrens hdr->b_cksum0 = 0; 1284789Sahrens } 12851544Seschrock while (hdr->b_buf) { 1286789Sahrens arc_buf_t *buf = hdr->b_buf; 1287789Sahrens 12881544Seschrock if (buf->b_efunc) { 12891544Seschrock mutex_enter(&arc_eviction_mtx); 12901544Seschrock ASSERT(buf->b_hdr != NULL); 12912688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 12921544Seschrock hdr->b_buf = buf->b_next; 12932887Smaybee buf->b_hdr = &arc_eviction_hdr; 12941544Seschrock buf->b_next = arc_eviction_list; 12951544Seschrock arc_eviction_list = buf; 12961544Seschrock mutex_exit(&arc_eviction_mtx); 12971544Seschrock } else { 12982688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 12991544Seschrock } 1300789Sahrens } 13013093Sahrens if (hdr->b_freeze_cksum != NULL) { 13023093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 13033093Sahrens hdr->b_freeze_cksum = NULL; 13043093Sahrens } 13051544Seschrock 1306789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1307789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1308789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1309789Sahrens kmem_cache_free(hdr_cache, hdr); 1310789Sahrens } 1311789Sahrens 1312789Sahrens void 1313789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1314789Sahrens { 1315789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 13163403Sbmc int hashed = hdr->b_state != arc_anon; 13171544Seschrock 13181544Seschrock ASSERT(buf->b_efunc == NULL); 13191544Seschrock ASSERT(buf->b_data != NULL); 13201544Seschrock 13211544Seschrock if (hashed) { 13221544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 13231544Seschrock 13241544Seschrock mutex_enter(hash_lock); 13251544Seschrock (void) remove_reference(hdr, hash_lock, tag); 13261544Seschrock if (hdr->b_datacnt > 1) 13272688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 13281544Seschrock else 13291544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 13301544Seschrock mutex_exit(hash_lock); 13311544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 13321544Seschrock int destroy_hdr; 13331544Seschrock /* 13341544Seschrock * We are in the middle of an async write. Don't destroy 13351544Seschrock * this buffer unless the write completes before we finish 13361544Seschrock * decrementing the reference count. 13371544Seschrock */ 13381544Seschrock mutex_enter(&arc_eviction_mtx); 13391544Seschrock (void) remove_reference(hdr, NULL, tag); 13401544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 13411544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 13421544Seschrock mutex_exit(&arc_eviction_mtx); 13431544Seschrock if (destroy_hdr) 13441544Seschrock arc_hdr_destroy(hdr); 13451544Seschrock } else { 13461544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 13471544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 13482688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 13491544Seschrock } else { 13501544Seschrock arc_hdr_destroy(hdr); 13511544Seschrock } 13521544Seschrock } 13531544Seschrock } 13541544Seschrock 13551544Seschrock int 13561544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 13571544Seschrock { 13581544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1359789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 13601544Seschrock int no_callback = (buf->b_efunc == NULL); 13611544Seschrock 13623403Sbmc if (hdr->b_state == arc_anon) { 13631544Seschrock arc_buf_free(buf, tag); 13641544Seschrock return (no_callback); 13651544Seschrock } 1366789Sahrens 1367789Sahrens mutex_enter(hash_lock); 13683403Sbmc ASSERT(hdr->b_state != arc_anon); 13691544Seschrock ASSERT(buf->b_data != NULL); 1370789Sahrens 13711544Seschrock (void) remove_reference(hdr, hash_lock, tag); 13721544Seschrock if (hdr->b_datacnt > 1) { 13731544Seschrock if (no_callback) 13742688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 13751544Seschrock } else if (no_callback) { 13761544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 13771544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1378789Sahrens } 13791544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 13801544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1381789Sahrens mutex_exit(hash_lock); 13821544Seschrock return (no_callback); 1383789Sahrens } 1384789Sahrens 1385789Sahrens int 1386789Sahrens arc_buf_size(arc_buf_t *buf) 1387789Sahrens { 1388789Sahrens return (buf->b_hdr->b_size); 1389789Sahrens } 1390789Sahrens 1391789Sahrens /* 1392789Sahrens * Evict buffers from list until we've removed the specified number of 1393789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 13942688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 13952688Smaybee * - look for a buffer to evict that is `bytes' long. 13962688Smaybee * - return the data block from this buffer rather than freeing it. 13972688Smaybee * This flag is used by callers that are trying to make space for a 13982688Smaybee * new buffer in a full arc cache. 1399*5642Smaybee * 1400*5642Smaybee * This function makes a "best effort". It skips over any buffers 1401*5642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 1402*5642Smaybee * It may also return without evicting as much space as requested. 1403789Sahrens */ 14042688Smaybee static void * 1405*5642Smaybee arc_evict(arc_state_t *state, spa_t *spa, int64_t bytes, boolean_t recycle, 14063290Sjohansen arc_buf_contents_t type) 1407789Sahrens { 1408789Sahrens arc_state_t *evicted_state; 14092688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 14102918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 14114309Smaybee list_t *list = &state->arcs_list[type]; 1412789Sahrens kmutex_t *hash_lock; 14132688Smaybee boolean_t have_lock; 14142918Smaybee void *stolen = NULL; 1415789Sahrens 14163403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1417789Sahrens 14183403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1419789Sahrens 14203403Sbmc mutex_enter(&state->arcs_mtx); 14213403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1422789Sahrens 14234309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 14244309Smaybee ab_prev = list_prev(list, ab); 14252391Smaybee /* prefetch buffers have a minimum lifespan */ 14262688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 1427*5642Smaybee (spa && ab->b_spa != spa) || 14282688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 14292688Smaybee lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) { 14302391Smaybee skipped++; 14312391Smaybee continue; 14322391Smaybee } 14332918Smaybee /* "lookahead" for better eviction candidate */ 14342918Smaybee if (recycle && ab->b_size != bytes && 14352918Smaybee ab_prev && ab_prev->b_size == bytes) 14362688Smaybee continue; 1437789Sahrens hash_lock = HDR_LOCK(ab); 14382688Smaybee have_lock = MUTEX_HELD(hash_lock); 14392688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1440789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 14411544Seschrock ASSERT(ab->b_datacnt > 0); 14421544Seschrock while (ab->b_buf) { 14431544Seschrock arc_buf_t *buf = ab->b_buf; 14442688Smaybee if (buf->b_data) { 14451544Seschrock bytes_evicted += ab->b_size; 14463290Sjohansen if (recycle && ab->b_type == type && 14475450Sbrendan ab->b_size == bytes && 14485450Sbrendan !HDR_L2_WRITING(ab)) { 14492918Smaybee stolen = buf->b_data; 14502918Smaybee recycle = FALSE; 14512918Smaybee } 14522688Smaybee } 14531544Seschrock if (buf->b_efunc) { 14541544Seschrock mutex_enter(&arc_eviction_mtx); 14552918Smaybee arc_buf_destroy(buf, 14562918Smaybee buf->b_data == stolen, FALSE); 14571544Seschrock ab->b_buf = buf->b_next; 14582887Smaybee buf->b_hdr = &arc_eviction_hdr; 14591544Seschrock buf->b_next = arc_eviction_list; 14601544Seschrock arc_eviction_list = buf; 14611544Seschrock mutex_exit(&arc_eviction_mtx); 14621544Seschrock } else { 14632918Smaybee arc_buf_destroy(buf, 14642918Smaybee buf->b_data == stolen, TRUE); 14651544Seschrock } 14661544Seschrock } 14671544Seschrock ASSERT(ab->b_datacnt == 0); 1468789Sahrens arc_change_state(evicted_state, ab, hash_lock); 14691544Seschrock ASSERT(HDR_IN_HASH_TABLE(ab)); 14705450Sbrendan ab->b_flags |= ARC_IN_HASH_TABLE; 14715450Sbrendan ab->b_flags &= ~ARC_BUF_AVAILABLE; 1472789Sahrens DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 14732688Smaybee if (!have_lock) 14742688Smaybee mutex_exit(hash_lock); 14751544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1476789Sahrens break; 1477789Sahrens } else { 14782688Smaybee missed += 1; 1479789Sahrens } 1480789Sahrens } 14813403Sbmc 14823403Sbmc mutex_exit(&evicted_state->arcs_mtx); 14833403Sbmc mutex_exit(&state->arcs_mtx); 1484789Sahrens 1485789Sahrens if (bytes_evicted < bytes) 1486789Sahrens dprintf("only evicted %lld bytes from %x", 1487789Sahrens (longlong_t)bytes_evicted, state); 1488789Sahrens 14892688Smaybee if (skipped) 14903403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 14913403Sbmc 14922688Smaybee if (missed) 14933403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 14943403Sbmc 14954709Smaybee /* 14964709Smaybee * We have just evicted some date into the ghost state, make 14974709Smaybee * sure we also adjust the ghost state size if necessary. 14984709Smaybee */ 14994709Smaybee if (arc_no_grow && 15004709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 15014709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 15024709Smaybee arc_mru_ghost->arcs_size - arc_c; 15034709Smaybee 15044709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 15054709Smaybee int64_t todelete = 15064709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 1507*5642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 15084709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 15094709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 15104709Smaybee arc_mru_ghost->arcs_size + 15114709Smaybee arc_mfu_ghost->arcs_size - arc_c); 1512*5642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 15134709Smaybee } 15144709Smaybee } 15154709Smaybee 15162918Smaybee return (stolen); 1517789Sahrens } 1518789Sahrens 1519789Sahrens /* 1520789Sahrens * Remove buffers from list until we've removed the specified number of 1521789Sahrens * bytes. Destroy the buffers that are removed. 1522789Sahrens */ 1523789Sahrens static void 1524*5642Smaybee arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes) 1525789Sahrens { 1526789Sahrens arc_buf_hdr_t *ab, *ab_prev; 15274309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1528789Sahrens kmutex_t *hash_lock; 15291544Seschrock uint64_t bytes_deleted = 0; 15303700Sek110237 uint64_t bufs_skipped = 0; 1531789Sahrens 15321544Seschrock ASSERT(GHOST_STATE(state)); 1533789Sahrens top: 15343403Sbmc mutex_enter(&state->arcs_mtx); 15354309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15364309Smaybee ab_prev = list_prev(list, ab); 1537*5642Smaybee if (spa && ab->b_spa != spa) 1538*5642Smaybee continue; 1539789Sahrens hash_lock = HDR_LOCK(ab); 1540789Sahrens if (mutex_tryenter(hash_lock)) { 15412391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 15421544Seschrock ASSERT(ab->b_buf == NULL); 15433403Sbmc ARCSTAT_BUMP(arcstat_deleted); 15441544Seschrock bytes_deleted += ab->b_size; 15455450Sbrendan 15465450Sbrendan if (ab->b_l2hdr != NULL) { 15475450Sbrendan /* 15485450Sbrendan * This buffer is cached on the 2nd Level ARC; 15495450Sbrendan * don't destroy the header. 15505450Sbrendan */ 15515450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 15525450Sbrendan mutex_exit(hash_lock); 15535450Sbrendan } else { 15545450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 15555450Sbrendan mutex_exit(hash_lock); 15565450Sbrendan arc_hdr_destroy(ab); 15575450Sbrendan } 15585450Sbrendan 1559789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1560789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1561789Sahrens break; 1562789Sahrens } else { 1563789Sahrens if (bytes < 0) { 15643403Sbmc mutex_exit(&state->arcs_mtx); 1565789Sahrens mutex_enter(hash_lock); 1566789Sahrens mutex_exit(hash_lock); 1567789Sahrens goto top; 1568789Sahrens } 1569789Sahrens bufs_skipped += 1; 1570789Sahrens } 1571789Sahrens } 15723403Sbmc mutex_exit(&state->arcs_mtx); 1573789Sahrens 15744309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 15754309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 15764309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 15774309Smaybee goto top; 15784309Smaybee } 15794309Smaybee 1580789Sahrens if (bufs_skipped) { 15813403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1582789Sahrens ASSERT(bytes >= 0); 1583789Sahrens } 1584789Sahrens 1585789Sahrens if (bytes_deleted < bytes) 1586789Sahrens dprintf("only deleted %lld bytes from %p", 1587789Sahrens (longlong_t)bytes_deleted, state); 1588789Sahrens } 1589789Sahrens 1590789Sahrens static void 1591789Sahrens arc_adjust(void) 1592789Sahrens { 15933403Sbmc int64_t top_sz, mru_over, arc_over, todelete; 1594789Sahrens 1595*5642Smaybee top_sz = arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used; 1596789Sahrens 15974309Smaybee if (top_sz > arc_p && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 15984309Smaybee int64_t toevict = 15994309Smaybee MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], top_sz - arc_p); 1600*5642Smaybee (void) arc_evict(arc_mru, NULL, toevict, FALSE, ARC_BUFC_DATA); 16014309Smaybee top_sz = arc_anon->arcs_size + arc_mru->arcs_size; 16024309Smaybee } 16034309Smaybee 16044309Smaybee if (top_sz > arc_p && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 16054309Smaybee int64_t toevict = 16064309Smaybee MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], top_sz - arc_p); 1607*5642Smaybee (void) arc_evict(arc_mru, NULL, toevict, FALSE, 1608*5642Smaybee ARC_BUFC_METADATA); 16093403Sbmc top_sz = arc_anon->arcs_size + arc_mru->arcs_size; 1610789Sahrens } 1611789Sahrens 16123403Sbmc mru_over = top_sz + arc_mru_ghost->arcs_size - arc_c; 1613789Sahrens 1614789Sahrens if (mru_over > 0) { 16154309Smaybee if (arc_mru_ghost->arcs_size > 0) { 16164309Smaybee todelete = MIN(arc_mru_ghost->arcs_size, mru_over); 1617*5642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 1618789Sahrens } 1619789Sahrens } 1620789Sahrens 16213403Sbmc if ((arc_over = arc_size - arc_c) > 0) { 16221544Seschrock int64_t tbl_over; 1623789Sahrens 16244309Smaybee if (arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 16254309Smaybee int64_t toevict = 16264309Smaybee MIN(arc_mfu->arcs_lsize[ARC_BUFC_DATA], arc_over); 1627*5642Smaybee (void) arc_evict(arc_mfu, NULL, toevict, FALSE, 16284309Smaybee ARC_BUFC_DATA); 16294309Smaybee arc_over = arc_size - arc_c; 1630789Sahrens } 1631789Sahrens 16324309Smaybee if (arc_over > 0 && 16334309Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 16344309Smaybee int64_t toevict = 16354309Smaybee MIN(arc_mfu->arcs_lsize[ARC_BUFC_METADATA], 16364309Smaybee arc_over); 1637*5642Smaybee (void) arc_evict(arc_mfu, NULL, toevict, FALSE, 16384309Smaybee ARC_BUFC_METADATA); 16394309Smaybee } 16404309Smaybee 16414309Smaybee tbl_over = arc_size + arc_mru_ghost->arcs_size + 16424309Smaybee arc_mfu_ghost->arcs_size - arc_c * 2; 16434309Smaybee 16444309Smaybee if (tbl_over > 0 && arc_mfu_ghost->arcs_size > 0) { 16454309Smaybee todelete = MIN(arc_mfu_ghost->arcs_size, tbl_over); 1646*5642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 1647789Sahrens } 1648789Sahrens } 1649789Sahrens } 1650789Sahrens 16511544Seschrock static void 16521544Seschrock arc_do_user_evicts(void) 16531544Seschrock { 16541544Seschrock mutex_enter(&arc_eviction_mtx); 16551544Seschrock while (arc_eviction_list != NULL) { 16561544Seschrock arc_buf_t *buf = arc_eviction_list; 16571544Seschrock arc_eviction_list = buf->b_next; 16581544Seschrock buf->b_hdr = NULL; 16591544Seschrock mutex_exit(&arc_eviction_mtx); 16601544Seschrock 16611819Smaybee if (buf->b_efunc != NULL) 16621819Smaybee VERIFY(buf->b_efunc(buf) == 0); 16631544Seschrock 16641544Seschrock buf->b_efunc = NULL; 16651544Seschrock buf->b_private = NULL; 16661544Seschrock kmem_cache_free(buf_cache, buf); 16671544Seschrock mutex_enter(&arc_eviction_mtx); 16681544Seschrock } 16691544Seschrock mutex_exit(&arc_eviction_mtx); 16701544Seschrock } 16711544Seschrock 1672789Sahrens /* 1673*5642Smaybee * Flush all *evictable* data from the cache for the given spa. 1674789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1675789Sahrens */ 1676789Sahrens void 1677*5642Smaybee arc_flush(spa_t *spa) 1678789Sahrens { 1679*5642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 1680*5642Smaybee (void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_DATA); 1681*5642Smaybee if (spa) 1682*5642Smaybee break; 1683*5642Smaybee } 1684*5642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 1685*5642Smaybee (void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_METADATA); 1686*5642Smaybee if (spa) 1687*5642Smaybee break; 1688*5642Smaybee } 1689*5642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 1690*5642Smaybee (void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_DATA); 1691*5642Smaybee if (spa) 1692*5642Smaybee break; 1693*5642Smaybee } 1694*5642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 1695*5642Smaybee (void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_METADATA); 1696*5642Smaybee if (spa) 1697*5642Smaybee break; 1698*5642Smaybee } 1699*5642Smaybee 1700*5642Smaybee arc_evict_ghost(arc_mru_ghost, spa, -1); 1701*5642Smaybee arc_evict_ghost(arc_mfu_ghost, spa, -1); 17021544Seschrock 17031544Seschrock mutex_enter(&arc_reclaim_thr_lock); 17041544Seschrock arc_do_user_evicts(); 17051544Seschrock mutex_exit(&arc_reclaim_thr_lock); 1706*5642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1707789Sahrens } 1708789Sahrens 17093158Smaybee int arc_shrink_shift = 5; /* log2(fraction of arc to reclaim) */ 17102391Smaybee 1711789Sahrens void 17123158Smaybee arc_shrink(void) 1713789Sahrens { 17143403Sbmc if (arc_c > arc_c_min) { 17153158Smaybee uint64_t to_free; 1716789Sahrens 17172048Sstans #ifdef _KERNEL 17183403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 17192048Sstans #else 17203403Sbmc to_free = arc_c >> arc_shrink_shift; 17212048Sstans #endif 17223403Sbmc if (arc_c > arc_c_min + to_free) 17233403Sbmc atomic_add_64(&arc_c, -to_free); 17243158Smaybee else 17253403Sbmc arc_c = arc_c_min; 17262048Sstans 17273403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 17283403Sbmc if (arc_c > arc_size) 17293403Sbmc arc_c = MAX(arc_size, arc_c_min); 17303403Sbmc if (arc_p > arc_c) 17313403Sbmc arc_p = (arc_c >> 1); 17323403Sbmc ASSERT(arc_c >= arc_c_min); 17333403Sbmc ASSERT((int64_t)arc_p >= 0); 17343158Smaybee } 1735789Sahrens 17363403Sbmc if (arc_size > arc_c) 17373158Smaybee arc_adjust(); 1738789Sahrens } 1739789Sahrens 1740789Sahrens static int 1741789Sahrens arc_reclaim_needed(void) 1742789Sahrens { 1743789Sahrens uint64_t extra; 1744789Sahrens 1745789Sahrens #ifdef _KERNEL 17462048Sstans 17472048Sstans if (needfree) 17482048Sstans return (1); 17492048Sstans 1750789Sahrens /* 1751789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1752789Sahrens */ 1753789Sahrens extra = desfree; 1754789Sahrens 1755789Sahrens /* 1756789Sahrens * check that we're out of range of the pageout scanner. It starts to 1757789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1758789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1759789Sahrens * number of needed free pages. We add extra pages here to make sure 1760789Sahrens * the scanner doesn't start up while we're freeing memory. 1761789Sahrens */ 1762789Sahrens if (freemem < lotsfree + needfree + extra) 1763789Sahrens return (1); 1764789Sahrens 1765789Sahrens /* 1766789Sahrens * check to make sure that swapfs has enough space so that anon 17675450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1768789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1769789Sahrens * swap pages. We also add a bit of extra here just to prevent 1770789Sahrens * circumstances from getting really dire. 1771789Sahrens */ 1772789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1773789Sahrens return (1); 1774789Sahrens 17751936Smaybee #if defined(__i386) 1776789Sahrens /* 1777789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1778789Sahrens * kernel heap space before we ever run out of available physical 1779789Sahrens * memory. Most checks of the size of the heap_area compare against 1780789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1781789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1782789Sahrens * which is so low that it's useless. In this comparison, we seek to 1783789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 17845450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1785789Sahrens * free) 1786789Sahrens */ 1787789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1788789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1789789Sahrens return (1); 1790789Sahrens #endif 1791789Sahrens 1792789Sahrens #else 1793789Sahrens if (spa_get_random(100) == 0) 1794789Sahrens return (1); 1795789Sahrens #endif 1796789Sahrens return (0); 1797789Sahrens } 1798789Sahrens 1799789Sahrens static void 1800789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1801789Sahrens { 1802789Sahrens size_t i; 1803789Sahrens kmem_cache_t *prev_cache = NULL; 18043290Sjohansen kmem_cache_t *prev_data_cache = NULL; 1805789Sahrens extern kmem_cache_t *zio_buf_cache[]; 18063290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 1807789Sahrens 18081484Sek110237 #ifdef _KERNEL 18094309Smaybee if (arc_meta_used >= arc_meta_limit) { 18104309Smaybee /* 18114309Smaybee * We are exceeding our meta-data cache limit. 18124309Smaybee * Purge some DNLC entries to release holds on meta-data. 18134309Smaybee */ 18144309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 18154309Smaybee } 18161936Smaybee #if defined(__i386) 18171936Smaybee /* 18181936Smaybee * Reclaim unused memory from all kmem caches. 18191936Smaybee */ 18201936Smaybee kmem_reap(); 18211936Smaybee #endif 18221484Sek110237 #endif 18231484Sek110237 1824789Sahrens /* 18255450Sbrendan * An aggressive reclamation will shrink the cache size as well as 18261544Seschrock * reap free buffers from the arc kmem caches. 1827789Sahrens */ 1828789Sahrens if (strat == ARC_RECLAIM_AGGR) 18293158Smaybee arc_shrink(); 1830789Sahrens 1831789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 1832789Sahrens if (zio_buf_cache[i] != prev_cache) { 1833789Sahrens prev_cache = zio_buf_cache[i]; 1834789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 1835789Sahrens } 18363290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 18373290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 18383290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 18393290Sjohansen } 1840789Sahrens } 18411544Seschrock kmem_cache_reap_now(buf_cache); 18421544Seschrock kmem_cache_reap_now(hdr_cache); 1843789Sahrens } 1844789Sahrens 1845789Sahrens static void 1846789Sahrens arc_reclaim_thread(void) 1847789Sahrens { 1848789Sahrens clock_t growtime = 0; 1849789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 1850789Sahrens callb_cpr_t cpr; 1851789Sahrens 1852789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 1853789Sahrens 1854789Sahrens mutex_enter(&arc_reclaim_thr_lock); 1855789Sahrens while (arc_thread_exit == 0) { 1856789Sahrens if (arc_reclaim_needed()) { 1857789Sahrens 18583403Sbmc if (arc_no_grow) { 1859789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 1860789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1861789Sahrens } else { 1862789Sahrens last_reclaim = ARC_RECLAIM_CONS; 1863789Sahrens } 1864789Sahrens } else { 18653403Sbmc arc_no_grow = TRUE; 1866789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1867789Sahrens membar_producer(); 1868789Sahrens } 1869789Sahrens 1870789Sahrens /* reset the growth delay for every reclaim */ 1871789Sahrens growtime = lbolt + (arc_grow_retry * hz); 1872789Sahrens 1873789Sahrens arc_kmem_reap_now(last_reclaim); 1874789Sahrens 18754309Smaybee } else if (arc_no_grow && lbolt >= growtime) { 18763403Sbmc arc_no_grow = FALSE; 1877789Sahrens } 1878789Sahrens 18793403Sbmc if (2 * arc_c < arc_size + 18803403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 18813298Smaybee arc_adjust(); 18823298Smaybee 18831544Seschrock if (arc_eviction_list != NULL) 18841544Seschrock arc_do_user_evicts(); 18851544Seschrock 1886789Sahrens /* block until needed, or one second, whichever is shorter */ 1887789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 1888789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 1889789Sahrens &arc_reclaim_thr_lock, (lbolt + hz)); 1890789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 1891789Sahrens } 1892789Sahrens 1893789Sahrens arc_thread_exit = 0; 1894789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 1895789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 1896789Sahrens thread_exit(); 1897789Sahrens } 1898789Sahrens 18991544Seschrock /* 19001544Seschrock * Adapt arc info given the number of bytes we are trying to add and 19011544Seschrock * the state that we are comming from. This function is only called 19021544Seschrock * when we are adding new content to the cache. 19031544Seschrock */ 1904789Sahrens static void 19051544Seschrock arc_adapt(int bytes, arc_state_t *state) 1906789Sahrens { 19071544Seschrock int mult; 19081544Seschrock 19095450Sbrendan if (state == arc_l2c_only) 19105450Sbrendan return; 19115450Sbrendan 19121544Seschrock ASSERT(bytes > 0); 1913789Sahrens /* 19141544Seschrock * Adapt the target size of the MRU list: 19151544Seschrock * - if we just hit in the MRU ghost list, then increase 19161544Seschrock * the target size of the MRU list. 19171544Seschrock * - if we just hit in the MFU ghost list, then increase 19181544Seschrock * the target size of the MFU list by decreasing the 19191544Seschrock * target size of the MRU list. 1920789Sahrens */ 19213403Sbmc if (state == arc_mru_ghost) { 19223403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 19233403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 19241544Seschrock 19253403Sbmc arc_p = MIN(arc_c, arc_p + bytes * mult); 19263403Sbmc } else if (state == arc_mfu_ghost) { 19273403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 19283403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 19291544Seschrock 19303403Sbmc arc_p = MAX(0, (int64_t)arc_p - bytes * mult); 19311544Seschrock } 19323403Sbmc ASSERT((int64_t)arc_p >= 0); 1933789Sahrens 1934789Sahrens if (arc_reclaim_needed()) { 1935789Sahrens cv_signal(&arc_reclaim_thr_cv); 1936789Sahrens return; 1937789Sahrens } 1938789Sahrens 19393403Sbmc if (arc_no_grow) 1940789Sahrens return; 1941789Sahrens 19423403Sbmc if (arc_c >= arc_c_max) 19431544Seschrock return; 19441544Seschrock 1945789Sahrens /* 19461544Seschrock * If we're within (2 * maxblocksize) bytes of the target 19471544Seschrock * cache size, increment the target cache size 1948789Sahrens */ 19493403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 19503403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 19513403Sbmc if (arc_c > arc_c_max) 19523403Sbmc arc_c = arc_c_max; 19533403Sbmc else if (state == arc_anon) 19543403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 19553403Sbmc if (arc_p > arc_c) 19563403Sbmc arc_p = arc_c; 1957789Sahrens } 19583403Sbmc ASSERT((int64_t)arc_p >= 0); 1959789Sahrens } 1960789Sahrens 1961789Sahrens /* 19621544Seschrock * Check if the cache has reached its limits and eviction is required 19631544Seschrock * prior to insert. 1964789Sahrens */ 1965789Sahrens static int 19664309Smaybee arc_evict_needed(arc_buf_contents_t type) 1967789Sahrens { 19684309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 19694309Smaybee return (1); 19704309Smaybee 19714309Smaybee #ifdef _KERNEL 19724309Smaybee /* 19734309Smaybee * If zio data pages are being allocated out of a separate heap segment, 19744309Smaybee * then enforce that the size of available vmem for this area remains 19754309Smaybee * above about 1/32nd free. 19764309Smaybee */ 19774309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 19784309Smaybee vmem_size(zio_arena, VMEM_FREE) < 19794309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 19804309Smaybee return (1); 19814309Smaybee #endif 19824309Smaybee 1983789Sahrens if (arc_reclaim_needed()) 1984789Sahrens return (1); 1985789Sahrens 19863403Sbmc return (arc_size > arc_c); 1987789Sahrens } 1988789Sahrens 1989789Sahrens /* 19902688Smaybee * The buffer, supplied as the first argument, needs a data block. 19912688Smaybee * So, if we are at cache max, determine which cache should be victimized. 19922688Smaybee * We have the following cases: 1993789Sahrens * 19943403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 1995789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 1996789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 1997789Sahrens * 19983403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 1999789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2000789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2001789Sahrens * entries. 2002789Sahrens * 20033403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2004789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2005789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2006789Sahrens * the MFU side, so the MRU side needs to be victimized. 2007789Sahrens * 20083403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2009789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2010789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2011789Sahrens */ 2012789Sahrens static void 20132688Smaybee arc_get_data_buf(arc_buf_t *buf) 2014789Sahrens { 20153290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 20163290Sjohansen uint64_t size = buf->b_hdr->b_size; 20173290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 20182688Smaybee 20192688Smaybee arc_adapt(size, state); 2020789Sahrens 20212688Smaybee /* 20222688Smaybee * We have not yet reached cache maximum size, 20232688Smaybee * just allocate a new buffer. 20242688Smaybee */ 20254309Smaybee if (!arc_evict_needed(type)) { 20263290Sjohansen if (type == ARC_BUFC_METADATA) { 20273290Sjohansen buf->b_data = zio_buf_alloc(size); 20284309Smaybee arc_space_consume(size); 20293290Sjohansen } else { 20303290Sjohansen ASSERT(type == ARC_BUFC_DATA); 20313290Sjohansen buf->b_data = zio_data_buf_alloc(size); 20324309Smaybee atomic_add_64(&arc_size, size); 20333290Sjohansen } 20342688Smaybee goto out; 20352688Smaybee } 20362688Smaybee 20372688Smaybee /* 20382688Smaybee * If we are prefetching from the mfu ghost list, this buffer 20392688Smaybee * will end up on the mru list; so steal space from there. 20402688Smaybee */ 20413403Sbmc if (state == arc_mfu_ghost) 20423403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 20433403Sbmc else if (state == arc_mru_ghost) 20443403Sbmc state = arc_mru; 2045789Sahrens 20463403Sbmc if (state == arc_mru || state == arc_anon) { 20473403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 20484309Smaybee state = (arc_mfu->arcs_lsize[type] > 0 && 20494309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2050789Sahrens } else { 20512688Smaybee /* MFU cases */ 20523403Sbmc uint64_t mfu_space = arc_c - arc_p; 20534309Smaybee state = (arc_mru->arcs_lsize[type] > 0 && 20544309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 20552688Smaybee } 2056*5642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 20573290Sjohansen if (type == ARC_BUFC_METADATA) { 20583290Sjohansen buf->b_data = zio_buf_alloc(size); 20594309Smaybee arc_space_consume(size); 20603290Sjohansen } else { 20613290Sjohansen ASSERT(type == ARC_BUFC_DATA); 20623290Sjohansen buf->b_data = zio_data_buf_alloc(size); 20634309Smaybee atomic_add_64(&arc_size, size); 20643290Sjohansen } 20653403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 20662688Smaybee } 20672688Smaybee ASSERT(buf->b_data != NULL); 20682688Smaybee out: 20692688Smaybee /* 20702688Smaybee * Update the state size. Note that ghost states have a 20712688Smaybee * "ghost size" and so don't need to be updated. 20722688Smaybee */ 20732688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 20742688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 20752688Smaybee 20763403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 20772688Smaybee if (list_link_active(&hdr->b_arc_node)) { 20782688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 20794309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2080789Sahrens } 20813298Smaybee /* 20823298Smaybee * If we are growing the cache, and we are adding anonymous 20833403Sbmc * data, and we have outgrown arc_p, update arc_p 20843298Smaybee */ 20853403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 20863403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 20873403Sbmc arc_p = MIN(arc_c, arc_p + size); 2088789Sahrens } 2089789Sahrens } 2090789Sahrens 2091789Sahrens /* 2092789Sahrens * This routine is called whenever a buffer is accessed. 20931544Seschrock * NOTE: the hash lock is dropped in this function. 2094789Sahrens */ 2095789Sahrens static void 20962688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2097789Sahrens { 2098789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2099789Sahrens 21003403Sbmc if (buf->b_state == arc_anon) { 2101789Sahrens /* 2102789Sahrens * This buffer is not in the cache, and does not 2103789Sahrens * appear in our "ghost" list. Add the new buffer 2104789Sahrens * to the MRU state. 2105789Sahrens */ 2106789Sahrens 2107789Sahrens ASSERT(buf->b_arc_access == 0); 2108789Sahrens buf->b_arc_access = lbolt; 21091544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 21103403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2111789Sahrens 21123403Sbmc } else if (buf->b_state == arc_mru) { 2113789Sahrens /* 21142391Smaybee * If this buffer is here because of a prefetch, then either: 21152391Smaybee * - clear the flag if this is a "referencing" read 21162391Smaybee * (any subsequent access will bump this into the MFU state). 21172391Smaybee * or 21182391Smaybee * - move the buffer to the head of the list if this is 21192391Smaybee * another prefetch (to make it less likely to be evicted). 2120789Sahrens */ 2121789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 21222391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 21232391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 21242391Smaybee } else { 21252391Smaybee buf->b_flags &= ~ARC_PREFETCH; 21263403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 21272391Smaybee } 21282391Smaybee buf->b_arc_access = lbolt; 2129789Sahrens return; 2130789Sahrens } 2131789Sahrens 2132789Sahrens /* 2133789Sahrens * This buffer has been "accessed" only once so far, 2134789Sahrens * but it is still in the cache. Move it to the MFU 2135789Sahrens * state. 2136789Sahrens */ 2137789Sahrens if (lbolt > buf->b_arc_access + ARC_MINTIME) { 2138789Sahrens /* 2139789Sahrens * More than 125ms have passed since we 2140789Sahrens * instantiated this buffer. Move it to the 2141789Sahrens * most frequently used state. 2142789Sahrens */ 2143789Sahrens buf->b_arc_access = lbolt; 21441544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 21453403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2146789Sahrens } 21473403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 21483403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2149789Sahrens arc_state_t *new_state; 2150789Sahrens /* 2151789Sahrens * This buffer has been "accessed" recently, but 2152789Sahrens * was evicted from the cache. Move it to the 2153789Sahrens * MFU state. 2154789Sahrens */ 2155789Sahrens 2156789Sahrens if (buf->b_flags & ARC_PREFETCH) { 21573403Sbmc new_state = arc_mru; 21582391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 21592391Smaybee buf->b_flags &= ~ARC_PREFETCH; 21601544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2161789Sahrens } else { 21623403Sbmc new_state = arc_mfu; 21631544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2164789Sahrens } 2165789Sahrens 2166789Sahrens buf->b_arc_access = lbolt; 2167789Sahrens arc_change_state(new_state, buf, hash_lock); 2168789Sahrens 21693403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 21703403Sbmc } else if (buf->b_state == arc_mfu) { 2171789Sahrens /* 2172789Sahrens * This buffer has been accessed more than once and is 2173789Sahrens * still in the cache. Keep it in the MFU state. 2174789Sahrens * 21752391Smaybee * NOTE: an add_reference() that occurred when we did 21762391Smaybee * the arc_read() will have kicked this off the list. 21772391Smaybee * If it was a prefetch, we will explicitly move it to 21782391Smaybee * the head of the list now. 2179789Sahrens */ 21802391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 21812391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 21822391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 21832391Smaybee } 21843403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 21852391Smaybee buf->b_arc_access = lbolt; 21863403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 21873403Sbmc arc_state_t *new_state = arc_mfu; 2188789Sahrens /* 2189789Sahrens * This buffer has been accessed more than once but has 2190789Sahrens * been evicted from the cache. Move it back to the 2191789Sahrens * MFU state. 2192789Sahrens */ 2193789Sahrens 21942391Smaybee if (buf->b_flags & ARC_PREFETCH) { 21952391Smaybee /* 21962391Smaybee * This is a prefetch access... 21972391Smaybee * move this block back to the MRU state. 21982391Smaybee */ 21992391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 22003403Sbmc new_state = arc_mru; 22012391Smaybee } 22022391Smaybee 2203789Sahrens buf->b_arc_access = lbolt; 22041544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 22052391Smaybee arc_change_state(new_state, buf, hash_lock); 2206789Sahrens 22073403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 22085450Sbrendan } else if (buf->b_state == arc_l2c_only) { 22095450Sbrendan /* 22105450Sbrendan * This buffer is on the 2nd Level ARC. 22115450Sbrendan */ 22125450Sbrendan 22135450Sbrendan buf->b_arc_access = lbolt; 22145450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 22155450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2216789Sahrens } else { 2217789Sahrens ASSERT(!"invalid arc state"); 2218789Sahrens } 2219789Sahrens } 2220789Sahrens 2221789Sahrens /* a generic arc_done_func_t which you can use */ 2222789Sahrens /* ARGSUSED */ 2223789Sahrens void 2224789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2225789Sahrens { 2226789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 22271544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2228789Sahrens } 2229789Sahrens 22304309Smaybee /* a generic arc_done_func_t */ 2231789Sahrens void 2232789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2233789Sahrens { 2234789Sahrens arc_buf_t **bufp = arg; 2235789Sahrens if (zio && zio->io_error) { 22361544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2237789Sahrens *bufp = NULL; 2238789Sahrens } else { 2239789Sahrens *bufp = buf; 2240789Sahrens } 2241789Sahrens } 2242789Sahrens 2243789Sahrens static void 2244789Sahrens arc_read_done(zio_t *zio) 2245789Sahrens { 22461589Smaybee arc_buf_hdr_t *hdr, *found; 2247789Sahrens arc_buf_t *buf; 2248789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2249789Sahrens kmutex_t *hash_lock; 2250789Sahrens arc_callback_t *callback_list, *acb; 2251789Sahrens int freeable = FALSE; 2252789Sahrens 2253789Sahrens buf = zio->io_private; 2254789Sahrens hdr = buf->b_hdr; 2255789Sahrens 22561589Smaybee /* 22571589Smaybee * The hdr was inserted into hash-table and removed from lists 22581589Smaybee * prior to starting I/O. We should find this header, since 22591589Smaybee * it's in the hash table, and it should be legit since it's 22601589Smaybee * not possible to evict it during the I/O. The only possible 22611589Smaybee * reason for it not to be found is if we were freed during the 22621589Smaybee * read. 22631589Smaybee */ 22641589Smaybee found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth, 22653093Sahrens &hash_lock); 2266789Sahrens 22671589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 22685450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 22695450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 22705450Sbrendan 22715450Sbrendan hdr->b_flags &= ~(ARC_L2_READING|ARC_L2_EVICTED); 22725450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 22735450Sbrendan hdr->b_flags |= ARC_DONT_L2CACHE; 2274789Sahrens 2275789Sahrens /* byteswap if necessary */ 2276789Sahrens callback_list = hdr->b_acb; 2277789Sahrens ASSERT(callback_list != NULL); 2278789Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp) && callback_list->acb_byteswap) 2279789Sahrens callback_list->acb_byteswap(buf->b_data, hdr->b_size); 2280789Sahrens 22815450Sbrendan arc_cksum_compute(buf, B_FALSE); 22823093Sahrens 2283789Sahrens /* create copies of the data buffer for the callers */ 2284789Sahrens abuf = buf; 2285789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2286789Sahrens if (acb->acb_done) { 22872688Smaybee if (abuf == NULL) 22882688Smaybee abuf = arc_buf_clone(buf); 2289789Sahrens acb->acb_buf = abuf; 2290789Sahrens abuf = NULL; 2291789Sahrens } 2292789Sahrens } 2293789Sahrens hdr->b_acb = NULL; 2294789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 22951544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 22961544Seschrock if (abuf == buf) 22971544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 2298789Sahrens 2299789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2300789Sahrens 2301789Sahrens if (zio->io_error != 0) { 2302789Sahrens hdr->b_flags |= ARC_IO_ERROR; 23033403Sbmc if (hdr->b_state != arc_anon) 23043403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 23051544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 23061544Seschrock buf_hash_remove(hdr); 2307789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 23082391Smaybee /* convert checksum errors into IO errors */ 23091544Seschrock if (zio->io_error == ECKSUM) 23101544Seschrock zio->io_error = EIO; 2311789Sahrens } 2312789Sahrens 23131544Seschrock /* 23142391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 23152391Smaybee * that the hdr (and hence the cv) might be freed before we get to 23162391Smaybee * the cv_broadcast(). 23171544Seschrock */ 23181544Seschrock cv_broadcast(&hdr->b_cv); 23191544Seschrock 23201589Smaybee if (hash_lock) { 2321789Sahrens /* 2322789Sahrens * Only call arc_access on anonymous buffers. This is because 2323789Sahrens * if we've issued an I/O for an evicted buffer, we've already 2324789Sahrens * called arc_access (to prevent any simultaneous readers from 2325789Sahrens * getting confused). 2326789Sahrens */ 23273403Sbmc if (zio->io_error == 0 && hdr->b_state == arc_anon) 23282688Smaybee arc_access(hdr, hash_lock); 23292688Smaybee mutex_exit(hash_lock); 2330789Sahrens } else { 2331789Sahrens /* 2332789Sahrens * This block was freed while we waited for the read to 2333789Sahrens * complete. It has been removed from the hash table and 2334789Sahrens * moved to the anonymous state (so that it won't show up 2335789Sahrens * in the cache). 2336789Sahrens */ 23373403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2338789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2339789Sahrens } 2340789Sahrens 2341789Sahrens /* execute each callback and free its structure */ 2342789Sahrens while ((acb = callback_list) != NULL) { 2343789Sahrens if (acb->acb_done) 2344789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2345789Sahrens 2346789Sahrens if (acb->acb_zio_dummy != NULL) { 2347789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2348789Sahrens zio_nowait(acb->acb_zio_dummy); 2349789Sahrens } 2350789Sahrens 2351789Sahrens callback_list = acb->acb_next; 2352789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2353789Sahrens } 2354789Sahrens 2355789Sahrens if (freeable) 23561544Seschrock arc_hdr_destroy(hdr); 2357789Sahrens } 2358789Sahrens 2359789Sahrens /* 2360789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2361789Sahrens * cache. If the block is found in the cache, invoke the provided 2362789Sahrens * callback immediately and return. Note that the `zio' parameter 2363789Sahrens * in the callback will be NULL in this case, since no IO was 2364789Sahrens * required. If the block is not in the cache pass the read request 2365789Sahrens * on to the spa with a substitute callback function, so that the 2366789Sahrens * requested block will be added to the cache. 2367789Sahrens * 2368789Sahrens * If a read request arrives for a block that has a read in-progress, 2369789Sahrens * either wait for the in-progress read to complete (and return the 2370789Sahrens * results); or, if this is a read with a "done" func, add a record 2371789Sahrens * to the read to invoke the "done" func when the read completes, 2372789Sahrens * and return; or just return. 2373789Sahrens * 2374789Sahrens * arc_read_done() will invoke all the requested "done" functions 2375789Sahrens * for readers of this block. 2376789Sahrens */ 2377789Sahrens int 2378789Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_byteswap_func_t *swap, 2379789Sahrens arc_done_func_t *done, void *private, int priority, int flags, 23802391Smaybee uint32_t *arc_flags, zbookmark_t *zb) 2381789Sahrens { 2382789Sahrens arc_buf_hdr_t *hdr; 2383789Sahrens arc_buf_t *buf; 2384789Sahrens kmutex_t *hash_lock; 23855450Sbrendan zio_t *rzio; 2386789Sahrens 2387789Sahrens top: 2388789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 23891544Seschrock if (hdr && hdr->b_datacnt > 0) { 2390789Sahrens 23912391Smaybee *arc_flags |= ARC_CACHED; 23922391Smaybee 2393789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 23942391Smaybee 23952391Smaybee if (*arc_flags & ARC_WAIT) { 23962391Smaybee cv_wait(&hdr->b_cv, hash_lock); 23972391Smaybee mutex_exit(hash_lock); 23982391Smaybee goto top; 23992391Smaybee } 24002391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 24012391Smaybee 24022391Smaybee if (done) { 2403789Sahrens arc_callback_t *acb = NULL; 2404789Sahrens 2405789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2406789Sahrens KM_SLEEP); 2407789Sahrens acb->acb_done = done; 2408789Sahrens acb->acb_private = private; 2409789Sahrens acb->acb_byteswap = swap; 2410789Sahrens if (pio != NULL) 2411789Sahrens acb->acb_zio_dummy = zio_null(pio, 2412789Sahrens spa, NULL, NULL, flags); 2413789Sahrens 2414789Sahrens ASSERT(acb->acb_done != NULL); 2415789Sahrens acb->acb_next = hdr->b_acb; 2416789Sahrens hdr->b_acb = acb; 2417789Sahrens add_reference(hdr, hash_lock, private); 2418789Sahrens mutex_exit(hash_lock); 2419789Sahrens return (0); 2420789Sahrens } 2421789Sahrens mutex_exit(hash_lock); 2422789Sahrens return (0); 2423789Sahrens } 2424789Sahrens 24253403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2426789Sahrens 24271544Seschrock if (done) { 24282688Smaybee add_reference(hdr, hash_lock, private); 24291544Seschrock /* 24301544Seschrock * If this block is already in use, create a new 24311544Seschrock * copy of the data so that we will be guaranteed 24321544Seschrock * that arc_release() will always succeed. 24331544Seschrock */ 24341544Seschrock buf = hdr->b_buf; 24351544Seschrock ASSERT(buf); 24361544Seschrock ASSERT(buf->b_data); 24372688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 24381544Seschrock ASSERT(buf->b_efunc == NULL); 24391544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 24402688Smaybee } else { 24412688Smaybee buf = arc_buf_clone(buf); 24421544Seschrock } 24432391Smaybee } else if (*arc_flags & ARC_PREFETCH && 24442391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 24452391Smaybee hdr->b_flags |= ARC_PREFETCH; 2446789Sahrens } 2447789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 24482688Smaybee arc_access(hdr, hash_lock); 24492688Smaybee mutex_exit(hash_lock); 24503403Sbmc ARCSTAT_BUMP(arcstat_hits); 24513403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 24523403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 24533403Sbmc data, metadata, hits); 24543403Sbmc 2455789Sahrens if (done) 2456789Sahrens done(NULL, buf, private); 2457789Sahrens } else { 2458789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2459789Sahrens arc_callback_t *acb; 2460789Sahrens 2461789Sahrens if (hdr == NULL) { 2462789Sahrens /* this block is not in the cache */ 2463789Sahrens arc_buf_hdr_t *exists; 24643290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 24653290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2466789Sahrens hdr = buf->b_hdr; 2467789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 2468789Sahrens hdr->b_birth = bp->blk_birth; 2469789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2470789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2471789Sahrens if (exists) { 2472789Sahrens /* somebody beat us to the hash insert */ 2473789Sahrens mutex_exit(hash_lock); 2474789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2475789Sahrens hdr->b_birth = 0; 2476789Sahrens hdr->b_cksum0 = 0; 24771544Seschrock (void) arc_buf_remove_ref(buf, private); 2478789Sahrens goto top; /* restart the IO request */ 2479789Sahrens } 24802391Smaybee /* if this is a prefetch, we don't have a reference */ 24812391Smaybee if (*arc_flags & ARC_PREFETCH) { 24822391Smaybee (void) remove_reference(hdr, hash_lock, 24832391Smaybee private); 24842391Smaybee hdr->b_flags |= ARC_PREFETCH; 24852391Smaybee } 24862391Smaybee if (BP_GET_LEVEL(bp) > 0) 24872391Smaybee hdr->b_flags |= ARC_INDIRECT; 2488789Sahrens } else { 2489789Sahrens /* this block is in the ghost cache */ 24901544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 24911544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 24922391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 24932391Smaybee ASSERT(hdr->b_buf == NULL); 2494789Sahrens 24952391Smaybee /* if this is a prefetch, we don't have a reference */ 24962391Smaybee if (*arc_flags & ARC_PREFETCH) 24972391Smaybee hdr->b_flags |= ARC_PREFETCH; 24982391Smaybee else 24992391Smaybee add_reference(hdr, hash_lock, private); 2500789Sahrens buf = kmem_cache_alloc(buf_cache, KM_SLEEP); 25011544Seschrock buf->b_hdr = hdr; 25022688Smaybee buf->b_data = NULL; 25031544Seschrock buf->b_efunc = NULL; 25041544Seschrock buf->b_private = NULL; 25051544Seschrock buf->b_next = NULL; 25061544Seschrock hdr->b_buf = buf; 25072688Smaybee arc_get_data_buf(buf); 25081544Seschrock ASSERT(hdr->b_datacnt == 0); 25091544Seschrock hdr->b_datacnt = 1; 25102391Smaybee 2511789Sahrens } 2512789Sahrens 2513789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2514789Sahrens acb->acb_done = done; 2515789Sahrens acb->acb_private = private; 2516789Sahrens acb->acb_byteswap = swap; 2517789Sahrens 2518789Sahrens ASSERT(hdr->b_acb == NULL); 2519789Sahrens hdr->b_acb = acb; 2520789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2521789Sahrens 2522789Sahrens /* 2523789Sahrens * If the buffer has been evicted, migrate it to a present state 2524789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2525789Sahrens * the header will be marked as I/O in progress and have an 2526789Sahrens * attached buffer. At this point, anybody who finds this 2527789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2528789Sahrens */ 2529789Sahrens 25301544Seschrock if (GHOST_STATE(hdr->b_state)) 25312688Smaybee arc_access(hdr, hash_lock); 2532789Sahrens 2533789Sahrens ASSERT3U(hdr->b_size, ==, size); 25341596Sahrens DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size, 25351596Sahrens zbookmark_t *, zb); 25363403Sbmc ARCSTAT_BUMP(arcstat_misses); 25373403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 25383403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 25393403Sbmc data, metadata, misses); 25401544Seschrock 25415450Sbrendan if (l2arc_ndev != 0) { 25425450Sbrendan /* 25435450Sbrendan * Read from the L2ARC if the following are true: 25445450Sbrendan * 1. This buffer has L2ARC metadata. 25455450Sbrendan * 2. This buffer isn't currently writing to the L2ARC. 25465450Sbrendan */ 25475450Sbrendan if (hdr->b_l2hdr != NULL && !HDR_L2_WRITING(hdr)) { 25485450Sbrendan vdev_t *vd = hdr->b_l2hdr->b_dev->l2ad_vdev; 25495450Sbrendan daddr_t addr = hdr->b_l2hdr->b_daddr; 25505450Sbrendan l2arc_read_callback_t *cb; 25515450Sbrendan 25525450Sbrendan DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 25535450Sbrendan ARCSTAT_BUMP(arcstat_l2_hits); 25545450Sbrendan 25555450Sbrendan hdr->b_flags |= ARC_L2_READING; 25565450Sbrendan mutex_exit(hash_lock); 25575450Sbrendan 25585450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 25595450Sbrendan KM_SLEEP); 25605450Sbrendan cb->l2rcb_buf = buf; 25615450Sbrendan cb->l2rcb_spa = spa; 25625450Sbrendan cb->l2rcb_bp = *bp; 25635450Sbrendan cb->l2rcb_zb = *zb; 25645450Sbrendan cb->l2rcb_flags = flags; 25655450Sbrendan 25665450Sbrendan /* 25675450Sbrendan * l2arc read. 25685450Sbrendan */ 25695450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 25705450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 25715450Sbrendan l2arc_read_done, cb, priority, 25725450Sbrendan flags | ZIO_FLAG_DONT_CACHE, B_FALSE); 25735450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 25745450Sbrendan zio_t *, rzio); 25755450Sbrendan 25765450Sbrendan if (*arc_flags & ARC_WAIT) 25775450Sbrendan return (zio_wait(rzio)); 25785450Sbrendan 25795450Sbrendan ASSERT(*arc_flags & ARC_NOWAIT); 25805450Sbrendan zio_nowait(rzio); 25815450Sbrendan return (0); 25825450Sbrendan } else { 25835450Sbrendan DTRACE_PROBE1(l2arc__miss, 25845450Sbrendan arc_buf_hdr_t *, hdr); 25855450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 25865450Sbrendan if (HDR_L2_WRITING(hdr)) 25875450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 25885450Sbrendan } 25895450Sbrendan } 25905450Sbrendan mutex_exit(hash_lock); 25915450Sbrendan 2592789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 25931544Seschrock arc_read_done, buf, priority, flags, zb); 2594789Sahrens 25952391Smaybee if (*arc_flags & ARC_WAIT) 2596789Sahrens return (zio_wait(rzio)); 2597789Sahrens 25982391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2599789Sahrens zio_nowait(rzio); 2600789Sahrens } 2601789Sahrens return (0); 2602789Sahrens } 2603789Sahrens 2604789Sahrens /* 2605789Sahrens * arc_read() variant to support pool traversal. If the block is already 2606789Sahrens * in the ARC, make a copy of it; otherwise, the caller will do the I/O. 2607789Sahrens * The idea is that we don't want pool traversal filling up memory, but 2608789Sahrens * if the ARC already has the data anyway, we shouldn't pay for the I/O. 2609789Sahrens */ 2610789Sahrens int 2611789Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data) 2612789Sahrens { 2613789Sahrens arc_buf_hdr_t *hdr; 2614789Sahrens kmutex_t *hash_mtx; 2615789Sahrens int rc = 0; 2616789Sahrens 2617789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx); 2618789Sahrens 26191544Seschrock if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) { 26201544Seschrock arc_buf_t *buf = hdr->b_buf; 26211544Seschrock 26221544Seschrock ASSERT(buf); 26231544Seschrock while (buf->b_data == NULL) { 26241544Seschrock buf = buf->b_next; 26251544Seschrock ASSERT(buf); 26261544Seschrock } 26271544Seschrock bcopy(buf->b_data, data, hdr->b_size); 26281544Seschrock } else { 2629789Sahrens rc = ENOENT; 26301544Seschrock } 2631789Sahrens 2632789Sahrens if (hash_mtx) 2633789Sahrens mutex_exit(hash_mtx); 2634789Sahrens 2635789Sahrens return (rc); 2636789Sahrens } 2637789Sahrens 26381544Seschrock void 26391544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 26401544Seschrock { 26411544Seschrock ASSERT(buf->b_hdr != NULL); 26423403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 26431544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 26441544Seschrock buf->b_efunc = func; 26451544Seschrock buf->b_private = private; 26461544Seschrock } 26471544Seschrock 26481544Seschrock /* 26491544Seschrock * This is used by the DMU to let the ARC know that a buffer is 26501544Seschrock * being evicted, so the ARC should clean up. If this arc buf 26511544Seschrock * is not yet in the evicted state, it will be put there. 26521544Seschrock */ 26531544Seschrock int 26541544Seschrock arc_buf_evict(arc_buf_t *buf) 26551544Seschrock { 26562887Smaybee arc_buf_hdr_t *hdr; 26571544Seschrock kmutex_t *hash_lock; 26581544Seschrock arc_buf_t **bufp; 26591544Seschrock 26602887Smaybee mutex_enter(&arc_eviction_mtx); 26612887Smaybee hdr = buf->b_hdr; 26621544Seschrock if (hdr == NULL) { 26631544Seschrock /* 26641544Seschrock * We are in arc_do_user_evicts(). 26651544Seschrock */ 26661544Seschrock ASSERT(buf->b_data == NULL); 26672887Smaybee mutex_exit(&arc_eviction_mtx); 26681544Seschrock return (0); 26691544Seschrock } 26702887Smaybee hash_lock = HDR_LOCK(hdr); 26712887Smaybee mutex_exit(&arc_eviction_mtx); 26721544Seschrock 26731544Seschrock mutex_enter(hash_lock); 26741544Seschrock 26752724Smaybee if (buf->b_data == NULL) { 26762724Smaybee /* 26772724Smaybee * We are on the eviction list. 26782724Smaybee */ 26792724Smaybee mutex_exit(hash_lock); 26802724Smaybee mutex_enter(&arc_eviction_mtx); 26812724Smaybee if (buf->b_hdr == NULL) { 26822724Smaybee /* 26832724Smaybee * We are already in arc_do_user_evicts(). 26842724Smaybee */ 26852724Smaybee mutex_exit(&arc_eviction_mtx); 26862724Smaybee return (0); 26872724Smaybee } else { 26882724Smaybee arc_buf_t copy = *buf; /* structure assignment */ 26892724Smaybee /* 26902724Smaybee * Process this buffer now 26912724Smaybee * but let arc_do_user_evicts() do the reaping. 26922724Smaybee */ 26932724Smaybee buf->b_efunc = NULL; 26942724Smaybee mutex_exit(&arc_eviction_mtx); 26952724Smaybee VERIFY(copy.b_efunc(©) == 0); 26962724Smaybee return (1); 26972724Smaybee } 26982724Smaybee } 26992724Smaybee 27002724Smaybee ASSERT(buf->b_hdr == hdr); 27012724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 27023403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 27031544Seschrock 27041544Seschrock /* 27051544Seschrock * Pull this buffer off of the hdr 27061544Seschrock */ 27071544Seschrock bufp = &hdr->b_buf; 27081544Seschrock while (*bufp != buf) 27091544Seschrock bufp = &(*bufp)->b_next; 27101544Seschrock *bufp = buf->b_next; 27111544Seschrock 27121544Seschrock ASSERT(buf->b_data != NULL); 27132688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 27141544Seschrock 27151544Seschrock if (hdr->b_datacnt == 0) { 27161544Seschrock arc_state_t *old_state = hdr->b_state; 27171544Seschrock arc_state_t *evicted_state; 27181544Seschrock 27191544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 27201544Seschrock 27211544Seschrock evicted_state = 27223403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 27231544Seschrock 27243403Sbmc mutex_enter(&old_state->arcs_mtx); 27253403Sbmc mutex_enter(&evicted_state->arcs_mtx); 27261544Seschrock 27271544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 27281544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 27295450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 27305450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 27311544Seschrock 27323403Sbmc mutex_exit(&evicted_state->arcs_mtx); 27333403Sbmc mutex_exit(&old_state->arcs_mtx); 27341544Seschrock } 27351544Seschrock mutex_exit(hash_lock); 27361819Smaybee 27371544Seschrock VERIFY(buf->b_efunc(buf) == 0); 27381544Seschrock buf->b_efunc = NULL; 27391544Seschrock buf->b_private = NULL; 27401544Seschrock buf->b_hdr = NULL; 27411544Seschrock kmem_cache_free(buf_cache, buf); 27421544Seschrock return (1); 27431544Seschrock } 27441544Seschrock 2745789Sahrens /* 2746789Sahrens * Release this buffer from the cache. This must be done 2747789Sahrens * after a read and prior to modifying the buffer contents. 2748789Sahrens * If the buffer has more than one reference, we must make 2749789Sahrens * make a new hdr for the buffer. 2750789Sahrens */ 2751789Sahrens void 2752789Sahrens arc_release(arc_buf_t *buf, void *tag) 2753789Sahrens { 2754789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 2755789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 27565450Sbrendan l2arc_buf_hdr_t *l2hdr = NULL; 27575450Sbrendan uint64_t buf_size; 2758789Sahrens 2759789Sahrens /* this buffer is not on any list */ 2760789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 2761789Sahrens 27623403Sbmc if (hdr->b_state == arc_anon) { 2763789Sahrens /* this buffer is already released */ 2764789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2765789Sahrens ASSERT(BUF_EMPTY(hdr)); 27661544Seschrock ASSERT(buf->b_efunc == NULL); 27673093Sahrens arc_buf_thaw(buf); 2768789Sahrens return; 2769789Sahrens } 2770789Sahrens 2771789Sahrens mutex_enter(hash_lock); 2772789Sahrens 27731544Seschrock /* 27741544Seschrock * Do we have more than one buf? 27751544Seschrock */ 27761544Seschrock if (hdr->b_buf != buf || buf->b_next != NULL) { 2777789Sahrens arc_buf_hdr_t *nhdr; 2778789Sahrens arc_buf_t **bufp; 2779789Sahrens uint64_t blksz = hdr->b_size; 2780789Sahrens spa_t *spa = hdr->b_spa; 27813290Sjohansen arc_buf_contents_t type = hdr->b_type; 27825450Sbrendan uint32_t flags = hdr->b_flags; 2783789Sahrens 27841544Seschrock ASSERT(hdr->b_datacnt > 1); 2785789Sahrens /* 2786789Sahrens * Pull the data off of this buf and attach it to 2787789Sahrens * a new anonymous buf. 2788789Sahrens */ 27891544Seschrock (void) remove_reference(hdr, hash_lock, tag); 2790789Sahrens bufp = &hdr->b_buf; 27911544Seschrock while (*bufp != buf) 2792789Sahrens bufp = &(*bufp)->b_next; 2793789Sahrens *bufp = (*bufp)->b_next; 27943897Smaybee buf->b_next = NULL; 27951544Seschrock 27963403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 27973403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 27981544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 27994309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 28004309Smaybee ASSERT3U(*size, >=, hdr->b_size); 28014309Smaybee atomic_add_64(size, -hdr->b_size); 28021544Seschrock } 28031544Seschrock hdr->b_datacnt -= 1; 28045450Sbrendan if (hdr->b_l2hdr != NULL) { 28055450Sbrendan mutex_enter(&l2arc_buflist_mtx); 28065450Sbrendan l2hdr = hdr->b_l2hdr; 28075450Sbrendan hdr->b_l2hdr = NULL; 28085450Sbrendan buf_size = hdr->b_size; 28095450Sbrendan } 28103547Smaybee arc_cksum_verify(buf); 28111544Seschrock 2812789Sahrens mutex_exit(hash_lock); 2813789Sahrens 2814789Sahrens nhdr = kmem_cache_alloc(hdr_cache, KM_SLEEP); 2815789Sahrens nhdr->b_size = blksz; 2816789Sahrens nhdr->b_spa = spa; 28173290Sjohansen nhdr->b_type = type; 2818789Sahrens nhdr->b_buf = buf; 28193403Sbmc nhdr->b_state = arc_anon; 2820789Sahrens nhdr->b_arc_access = 0; 28215450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 28225450Sbrendan nhdr->b_l2hdr = NULL; 28231544Seschrock nhdr->b_datacnt = 1; 28243547Smaybee nhdr->b_freeze_cksum = NULL; 28253897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 2826789Sahrens buf->b_hdr = nhdr; 28273403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 2828789Sahrens } else { 28291544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 2830789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 2831789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 28323403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 2833789Sahrens hdr->b_arc_access = 0; 28345450Sbrendan if (hdr->b_l2hdr != NULL) { 28355450Sbrendan mutex_enter(&l2arc_buflist_mtx); 28365450Sbrendan l2hdr = hdr->b_l2hdr; 28375450Sbrendan hdr->b_l2hdr = NULL; 28385450Sbrendan buf_size = hdr->b_size; 28395450Sbrendan } 2840789Sahrens mutex_exit(hash_lock); 28415450Sbrendan 2842789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2843789Sahrens hdr->b_birth = 0; 2844789Sahrens hdr->b_cksum0 = 0; 28453547Smaybee arc_buf_thaw(buf); 2846789Sahrens } 28471544Seschrock buf->b_efunc = NULL; 28481544Seschrock buf->b_private = NULL; 28495450Sbrendan 28505450Sbrendan if (l2hdr) { 28515450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 28525450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 28535450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 28545450Sbrendan } 28555450Sbrendan if (MUTEX_HELD(&l2arc_buflist_mtx)) 28565450Sbrendan mutex_exit(&l2arc_buflist_mtx); 2857789Sahrens } 2858789Sahrens 2859789Sahrens int 2860789Sahrens arc_released(arc_buf_t *buf) 2861789Sahrens { 28623403Sbmc return (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 28631544Seschrock } 28641544Seschrock 28651544Seschrock int 28661544Seschrock arc_has_callback(arc_buf_t *buf) 28671544Seschrock { 28681544Seschrock return (buf->b_efunc != NULL); 2869789Sahrens } 2870789Sahrens 28711544Seschrock #ifdef ZFS_DEBUG 28721544Seschrock int 28731544Seschrock arc_referenced(arc_buf_t *buf) 28741544Seschrock { 28751544Seschrock return (refcount_count(&buf->b_hdr->b_refcnt)); 28761544Seschrock } 28771544Seschrock #endif 28781544Seschrock 2879789Sahrens static void 28803547Smaybee arc_write_ready(zio_t *zio) 28813547Smaybee { 28823547Smaybee arc_write_callback_t *callback = zio->io_private; 28833547Smaybee arc_buf_t *buf = callback->awcb_buf; 28845329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 28855329Sgw25295 28865329Sgw25295 if (zio->io_error == 0 && callback->awcb_ready) { 28873547Smaybee ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 28883547Smaybee callback->awcb_ready(zio, buf, callback->awcb_private); 28893547Smaybee } 28905329Sgw25295 /* 28915329Sgw25295 * If the IO is already in progress, then this is a re-write 28925329Sgw25295 * attempt, so we need to thaw and re-compute the cksum. It is 28935329Sgw25295 * the responsibility of the callback to handle the freeing 28945329Sgw25295 * and accounting for any re-write attempt. If we don't have a 28955329Sgw25295 * callback registered then simply free the block here. 28965329Sgw25295 */ 28975329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 28985329Sgw25295 if (!BP_IS_HOLE(&zio->io_bp_orig) && 28995329Sgw25295 callback->awcb_ready == NULL) { 29005329Sgw25295 zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg, 29015329Sgw25295 &zio->io_bp_orig, NULL, NULL)); 29025329Sgw25295 } 29035329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 29045329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 29055329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 29065329Sgw25295 hdr->b_freeze_cksum = NULL; 29075329Sgw25295 } 29085329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 29095329Sgw25295 } 29105450Sbrendan arc_cksum_compute(buf, B_FALSE); 29115329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 29123547Smaybee } 29133547Smaybee 29143547Smaybee static void 2915789Sahrens arc_write_done(zio_t *zio) 2916789Sahrens { 29173547Smaybee arc_write_callback_t *callback = zio->io_private; 29183547Smaybee arc_buf_t *buf = callback->awcb_buf; 29193547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 2920789Sahrens 2921789Sahrens hdr->b_acb = NULL; 2922789Sahrens 2923789Sahrens /* this buffer is on no lists and is not in the hash table */ 29243403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2925789Sahrens 2926789Sahrens hdr->b_dva = *BP_IDENTITY(zio->io_bp); 2927789Sahrens hdr->b_birth = zio->io_bp->blk_birth; 2928789Sahrens hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 29291544Seschrock /* 29301544Seschrock * If the block to be written was all-zero, we may have 29311544Seschrock * compressed it away. In this case no write was performed 29321544Seschrock * so there will be no dva/birth-date/checksum. The buffer 29331544Seschrock * must therefor remain anonymous (and uncached). 29341544Seschrock */ 2935789Sahrens if (!BUF_EMPTY(hdr)) { 2936789Sahrens arc_buf_hdr_t *exists; 2937789Sahrens kmutex_t *hash_lock; 2938789Sahrens 29393093Sahrens arc_cksum_verify(buf); 29403093Sahrens 2941789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2942789Sahrens if (exists) { 2943789Sahrens /* 2944789Sahrens * This can only happen if we overwrite for 2945789Sahrens * sync-to-convergence, because we remove 2946789Sahrens * buffers from the hash table when we arc_free(). 2947789Sahrens */ 2948789Sahrens ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig), 2949789Sahrens BP_IDENTITY(zio->io_bp))); 2950789Sahrens ASSERT3U(zio->io_bp_orig.blk_birth, ==, 2951789Sahrens zio->io_bp->blk_birth); 2952789Sahrens 2953789Sahrens ASSERT(refcount_is_zero(&exists->b_refcnt)); 29543403Sbmc arc_change_state(arc_anon, exists, hash_lock); 2955789Sahrens mutex_exit(hash_lock); 29561544Seschrock arc_hdr_destroy(exists); 2957789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2958789Sahrens ASSERT3P(exists, ==, NULL); 2959789Sahrens } 29601544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 29612688Smaybee arc_access(hdr, hash_lock); 29622688Smaybee mutex_exit(hash_lock); 29633547Smaybee } else if (callback->awcb_done == NULL) { 29641544Seschrock int destroy_hdr; 29651544Seschrock /* 29661544Seschrock * This is an anonymous buffer with no user callback, 29671544Seschrock * destroy it if there are no active references. 29681544Seschrock */ 29691544Seschrock mutex_enter(&arc_eviction_mtx); 29701544Seschrock destroy_hdr = refcount_is_zero(&hdr->b_refcnt); 29711544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 29721544Seschrock mutex_exit(&arc_eviction_mtx); 29731544Seschrock if (destroy_hdr) 29741544Seschrock arc_hdr_destroy(hdr); 29751544Seschrock } else { 29761544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 2977789Sahrens } 29781544Seschrock 29793547Smaybee if (callback->awcb_done) { 2980789Sahrens ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 29813547Smaybee callback->awcb_done(zio, buf, callback->awcb_private); 2982789Sahrens } 2983789Sahrens 29843547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 2985789Sahrens } 2986789Sahrens 29873547Smaybee zio_t * 29881775Sbillm arc_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies, 2989789Sahrens uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 29903547Smaybee arc_done_func_t *ready, arc_done_func_t *done, void *private, int priority, 29913547Smaybee int flags, zbookmark_t *zb) 2992789Sahrens { 2993789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 29943547Smaybee arc_write_callback_t *callback; 29953547Smaybee zio_t *zio; 2996789Sahrens 2997789Sahrens /* this is a private buffer - no locking required */ 29983403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2999789Sahrens ASSERT(BUF_EMPTY(hdr)); 3000789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 30012237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 30022237Smaybee ASSERT(hdr->b_acb == 0); 30033547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 30043547Smaybee callback->awcb_ready = ready; 30053547Smaybee callback->awcb_done = done; 30063547Smaybee callback->awcb_private = private; 30073547Smaybee callback->awcb_buf = buf; 30083547Smaybee zio = zio_write(pio, spa, checksum, compress, ncopies, txg, bp, 30093547Smaybee buf->b_data, hdr->b_size, arc_write_ready, arc_write_done, callback, 30103547Smaybee priority, flags, zb); 3011789Sahrens 30123547Smaybee return (zio); 3013789Sahrens } 3014789Sahrens 3015789Sahrens int 3016789Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 3017789Sahrens zio_done_func_t *done, void *private, uint32_t arc_flags) 3018789Sahrens { 3019789Sahrens arc_buf_hdr_t *ab; 3020789Sahrens kmutex_t *hash_lock; 3021789Sahrens zio_t *zio; 3022789Sahrens 3023789Sahrens /* 3024789Sahrens * If this buffer is in the cache, release it, so it 3025789Sahrens * can be re-used. 3026789Sahrens */ 3027789Sahrens ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 3028789Sahrens if (ab != NULL) { 3029789Sahrens /* 3030789Sahrens * The checksum of blocks to free is not always 3031789Sahrens * preserved (eg. on the deadlist). However, if it is 3032789Sahrens * nonzero, it should match what we have in the cache. 3033789Sahrens */ 3034789Sahrens ASSERT(bp->blk_cksum.zc_word[0] == 0 || 3035789Sahrens ab->b_cksum0 == bp->blk_cksum.zc_word[0]); 30363403Sbmc if (ab->b_state != arc_anon) 30373403Sbmc arc_change_state(arc_anon, ab, hash_lock); 30382391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 30392391Smaybee /* 30402391Smaybee * This should only happen when we prefetch. 30412391Smaybee */ 30422391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 30432391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 30442391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 30452391Smaybee if (HDR_IN_HASH_TABLE(ab)) 30462391Smaybee buf_hash_remove(ab); 30472391Smaybee ab->b_arc_access = 0; 30482391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 30492391Smaybee ab->b_birth = 0; 30502391Smaybee ab->b_cksum0 = 0; 30512391Smaybee ab->b_buf->b_efunc = NULL; 30522391Smaybee ab->b_buf->b_private = NULL; 30532391Smaybee mutex_exit(hash_lock); 30542391Smaybee } else if (refcount_is_zero(&ab->b_refcnt)) { 30555450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3056789Sahrens mutex_exit(hash_lock); 30571544Seschrock arc_hdr_destroy(ab); 30583403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3059789Sahrens } else { 30601589Smaybee /* 30612391Smaybee * We still have an active reference on this 30622391Smaybee * buffer. This can happen, e.g., from 30632391Smaybee * dbuf_unoverride(). 30641589Smaybee */ 30652391Smaybee ASSERT(!HDR_IN_HASH_TABLE(ab)); 3066789Sahrens ab->b_arc_access = 0; 3067789Sahrens bzero(&ab->b_dva, sizeof (dva_t)); 3068789Sahrens ab->b_birth = 0; 3069789Sahrens ab->b_cksum0 = 0; 30701544Seschrock ab->b_buf->b_efunc = NULL; 30711544Seschrock ab->b_buf->b_private = NULL; 3072789Sahrens mutex_exit(hash_lock); 3073789Sahrens } 3074789Sahrens } 3075789Sahrens 3076789Sahrens zio = zio_free(pio, spa, txg, bp, done, private); 3077789Sahrens 3078789Sahrens if (arc_flags & ARC_WAIT) 3079789Sahrens return (zio_wait(zio)); 3080789Sahrens 3081789Sahrens ASSERT(arc_flags & ARC_NOWAIT); 3082789Sahrens zio_nowait(zio); 3083789Sahrens 3084789Sahrens return (0); 3085789Sahrens } 3086789Sahrens 3087789Sahrens void 3088789Sahrens arc_tempreserve_clear(uint64_t tempreserve) 3089789Sahrens { 3090789Sahrens atomic_add_64(&arc_tempreserve, -tempreserve); 3091789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3092789Sahrens } 3093789Sahrens 3094789Sahrens int 3095789Sahrens arc_tempreserve_space(uint64_t tempreserve) 3096789Sahrens { 3097789Sahrens #ifdef ZFS_DEBUG 3098789Sahrens /* 3099789Sahrens * Once in a while, fail for no reason. Everything should cope. 3100789Sahrens */ 3101789Sahrens if (spa_get_random(10000) == 0) { 3102789Sahrens dprintf("forcing random failure\n"); 3103789Sahrens return (ERESTART); 3104789Sahrens } 3105789Sahrens #endif 31063403Sbmc if (tempreserve > arc_c/4 && !arc_no_grow) 31073403Sbmc arc_c = MIN(arc_c_max, tempreserve * 4); 31083403Sbmc if (tempreserve > arc_c) 3109982Smaybee return (ENOMEM); 3110982Smaybee 3111789Sahrens /* 3112982Smaybee * Throttle writes when the amount of dirty data in the cache 3113982Smaybee * gets too large. We try to keep the cache less than half full 3114982Smaybee * of dirty blocks so that our sync times don't grow too large. 3115982Smaybee * Note: if two requests come in concurrently, we might let them 3116982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3117982Smaybee * 3118982Smaybee * XXX The limit should be adjusted dynamically to keep the time 3119982Smaybee * to sync a dataset fixed (around 1-5 seconds?). 3120789Sahrens */ 3121789Sahrens 31223403Sbmc if (tempreserve + arc_tempreserve + arc_anon->arcs_size > arc_c / 2 && 31233403Sbmc arc_tempreserve + arc_anon->arcs_size > arc_c / 4) { 31244309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 31254309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 31264309Smaybee arc_tempreserve>>10, 31274309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 31284309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 31293403Sbmc tempreserve>>10, arc_c>>10); 3130789Sahrens return (ERESTART); 3131789Sahrens } 3132789Sahrens atomic_add_64(&arc_tempreserve, tempreserve); 3133789Sahrens return (0); 3134789Sahrens } 3135789Sahrens 3136789Sahrens void 3137789Sahrens arc_init(void) 3138789Sahrens { 3139789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3140789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3141789Sahrens 31422391Smaybee /* Convert seconds to clock ticks */ 31432638Sperrin arc_min_prefetch_lifespan = 1 * hz; 31442391Smaybee 3145789Sahrens /* Start out with 1/8 of all memory */ 31463403Sbmc arc_c = physmem * PAGESIZE / 8; 3147789Sahrens 3148789Sahrens #ifdef _KERNEL 3149789Sahrens /* 3150789Sahrens * On architectures where the physical memory can be larger 3151789Sahrens * than the addressable space (intel in 32-bit mode), we may 3152789Sahrens * need to limit the cache to 1/8 of VM size. 3153789Sahrens */ 31543403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3155789Sahrens #endif 3156789Sahrens 3157982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 31583403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3159982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 31603403Sbmc if (arc_c * 8 >= 1<<30) 31613403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3162789Sahrens else 31633403Sbmc arc_c_max = arc_c_min; 31643403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 31652885Sahrens 31662885Sahrens /* 31672885Sahrens * Allow the tunables to override our calculations if they are 31682885Sahrens * reasonable (ie. over 64MB) 31692885Sahrens */ 31702885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 31713403Sbmc arc_c_max = zfs_arc_max; 31723403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 31733403Sbmc arc_c_min = zfs_arc_min; 31742885Sahrens 31753403Sbmc arc_c = arc_c_max; 31763403Sbmc arc_p = (arc_c >> 1); 3177789Sahrens 31784309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 31794309Smaybee arc_meta_limit = arc_c_max / 4; 31804645Sek110237 31814645Sek110237 /* Allow the tunable to override if it is reasonable */ 31824645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 31834645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 31844645Sek110237 31854309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 31864309Smaybee arc_c_min = arc_meta_limit / 2; 31874309Smaybee 3188789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3189789Sahrens if (kmem_debugging()) 31903403Sbmc arc_c = arc_c / 2; 31913403Sbmc if (arc_c < arc_c_min) 31923403Sbmc arc_c = arc_c_min; 3193789Sahrens 31943403Sbmc arc_anon = &ARC_anon; 31953403Sbmc arc_mru = &ARC_mru; 31963403Sbmc arc_mru_ghost = &ARC_mru_ghost; 31973403Sbmc arc_mfu = &ARC_mfu; 31983403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 31995450Sbrendan arc_l2c_only = &ARC_l2c_only; 32003403Sbmc arc_size = 0; 3201789Sahrens 32023403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 32033403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 32043403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 32053403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 32063403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 32075450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 32082688Smaybee 32094309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 32104309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 32114309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 32124309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 32134309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 32144309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 32154309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 32164309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 32174309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 32184309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 32194309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 32204309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 32214309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 32224309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 32234309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 32244309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 32255450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 32265450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 32275450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 32285450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3229789Sahrens 3230789Sahrens buf_init(); 3231789Sahrens 3232789Sahrens arc_thread_exit = 0; 32331544Seschrock arc_eviction_list = NULL; 32341544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 32352887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3236789Sahrens 32373403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 32383403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 32393403Sbmc 32403403Sbmc if (arc_ksp != NULL) { 32413403Sbmc arc_ksp->ks_data = &arc_stats; 32423403Sbmc kstat_install(arc_ksp); 32433403Sbmc } 32443403Sbmc 3245789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3246789Sahrens TS_RUN, minclsyspri); 32473158Smaybee 32483158Smaybee arc_dead = FALSE; 3249789Sahrens } 3250789Sahrens 3251789Sahrens void 3252789Sahrens arc_fini(void) 3253789Sahrens { 3254789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3255789Sahrens arc_thread_exit = 1; 3256789Sahrens while (arc_thread_exit != 0) 3257789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3258789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3259789Sahrens 3260*5642Smaybee arc_flush(NULL); 3261789Sahrens 3262789Sahrens arc_dead = TRUE; 3263789Sahrens 32643403Sbmc if (arc_ksp != NULL) { 32653403Sbmc kstat_delete(arc_ksp); 32663403Sbmc arc_ksp = NULL; 32673403Sbmc } 32683403Sbmc 32691544Seschrock mutex_destroy(&arc_eviction_mtx); 3270789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3271789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3272789Sahrens 32734309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 32744309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 32754309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 32764309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 32774309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 32784309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 32794309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 32804309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3281789Sahrens 32823403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 32833403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 32843403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 32853403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 32863403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 32872856Snd150628 3288789Sahrens buf_fini(); 3289789Sahrens } 32905450Sbrendan 32915450Sbrendan /* 32925450Sbrendan * Level 2 ARC 32935450Sbrendan * 32945450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 32955450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 32965450Sbrendan * using large infrequent writes. The main role of this cache is to boost 32975450Sbrendan * the performance of random read workloads. The intended L2ARC devices 32985450Sbrendan * include short-stroked disks, solid state disks, and other media with 32995450Sbrendan * substantially faster read latency than disk. 33005450Sbrendan * 33015450Sbrendan * +-----------------------+ 33025450Sbrendan * | ARC | 33035450Sbrendan * +-----------------------+ 33045450Sbrendan * | ^ ^ 33055450Sbrendan * | | | 33065450Sbrendan * l2arc_feed_thread() arc_read() 33075450Sbrendan * | | | 33085450Sbrendan * | l2arc read | 33095450Sbrendan * V | | 33105450Sbrendan * +---------------+ | 33115450Sbrendan * | L2ARC | | 33125450Sbrendan * +---------------+ | 33135450Sbrendan * | ^ | 33145450Sbrendan * l2arc_write() | | 33155450Sbrendan * | | | 33165450Sbrendan * V | | 33175450Sbrendan * +-------+ +-------+ 33185450Sbrendan * | vdev | | vdev | 33195450Sbrendan * | cache | | cache | 33205450Sbrendan * +-------+ +-------+ 33215450Sbrendan * +=========+ .-----. 33225450Sbrendan * : L2ARC : |-_____-| 33235450Sbrendan * : devices : | Disks | 33245450Sbrendan * +=========+ `-_____-' 33255450Sbrendan * 33265450Sbrendan * Read requests are satisfied from the following sources, in order: 33275450Sbrendan * 33285450Sbrendan * 1) ARC 33295450Sbrendan * 2) vdev cache of L2ARC devices 33305450Sbrendan * 3) L2ARC devices 33315450Sbrendan * 4) vdev cache of disks 33325450Sbrendan * 5) disks 33335450Sbrendan * 33345450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 33355450Sbrendan * To accommodate for this there are some significant differences between 33365450Sbrendan * the L2ARC and traditional cache design: 33375450Sbrendan * 33385450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 33395450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 33405450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 33415450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 33425450Sbrendan * 33435450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 33445450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 33455450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 33465450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 33475450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 33485450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 33495450Sbrendan * provide a better sense of ratio than this diagram: 33505450Sbrendan * 33515450Sbrendan * head --> tail 33525450Sbrendan * +---------------------+----------+ 33535450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 33545450Sbrendan * +---------------------+----------+ | o L2ARC eligible 33555450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 33565450Sbrendan * +---------------------+----------+ | 33575450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 33585450Sbrendan * headroom | 33595450Sbrendan * l2arc_feed_thread() 33605450Sbrendan * | 33615450Sbrendan * l2arc write hand <--[oooo]--' 33625450Sbrendan * | 8 Mbyte 33635450Sbrendan * | write max 33645450Sbrendan * V 33655450Sbrendan * +==============================+ 33665450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 33675450Sbrendan * +==============================+ 33685450Sbrendan * 32 Gbytes 33695450Sbrendan * 33705450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 33715450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 33725450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 33735450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 33745450Sbrendan * the ARC lists have moved there due to inactivity. 33755450Sbrendan * 33765450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 33775450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 33785450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 33795450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 33805450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 33815450Sbrendan * quickly, such as during backups of the entire pool. 33825450Sbrendan * 33835450Sbrendan * 5. Writes to the L2ARC devices are grouped and sent in-sequence, so that 33845450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 33855450Sbrendan * device is written to in a rotor fashion, sweeping writes through 33865450Sbrendan * available space then repeating. 33875450Sbrendan * 33885450Sbrendan * 6. The L2ARC does not store dirty content. It never needs to flush 33895450Sbrendan * write buffers back to disk based storage. 33905450Sbrendan * 33915450Sbrendan * 7. If an ARC buffer is written (and dirtied) which also exists in the 33925450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 33935450Sbrendan * 33945450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 33955450Sbrendan * may be necessary for different workloads: 33965450Sbrendan * 33975450Sbrendan * l2arc_write_max max write bytes per interval 33985450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 33995450Sbrendan * l2arc_headroom number of max device writes to precache 34005450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 34015450Sbrendan * 34025450Sbrendan * Tunables may be removed or added as future performance improvements are 34035450Sbrendan * integrated, and also may become zpool properties. 34045450Sbrendan */ 34055450Sbrendan 34065450Sbrendan static void 34075450Sbrendan l2arc_hdr_stat_add(void) 34085450Sbrendan { 34095450Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, sizeof (arc_buf_hdr_t) + 34105450Sbrendan sizeof (l2arc_buf_hdr_t)); 34115450Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -sizeof (arc_buf_hdr_t)); 34125450Sbrendan } 34135450Sbrendan 34145450Sbrendan static void 34155450Sbrendan l2arc_hdr_stat_remove(void) 34165450Sbrendan { 34175450Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -sizeof (arc_buf_hdr_t) - 34185450Sbrendan sizeof (l2arc_buf_hdr_t)); 34195450Sbrendan ARCSTAT_INCR(arcstat_hdr_size, sizeof (arc_buf_hdr_t)); 34205450Sbrendan } 34215450Sbrendan 34225450Sbrendan /* 34235450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 34245450Sbrendan * This is called with l2arc_dev_mtx held, which also locks out spa removal. 34255450Sbrendan */ 34265450Sbrendan static l2arc_dev_t * 34275450Sbrendan l2arc_dev_get_next(void) 34285450Sbrendan { 34295450Sbrendan l2arc_dev_t *next; 34305450Sbrendan 34315450Sbrendan if (l2arc_dev_last == NULL) { 34325450Sbrendan next = list_head(l2arc_dev_list); 34335450Sbrendan } else { 34345450Sbrendan next = list_next(l2arc_dev_list, l2arc_dev_last); 34355450Sbrendan if (next == NULL) 34365450Sbrendan next = list_head(l2arc_dev_list); 34375450Sbrendan } 34385450Sbrendan 34395450Sbrendan l2arc_dev_last = next; 34405450Sbrendan 34415450Sbrendan return (next); 34425450Sbrendan } 34435450Sbrendan 34445450Sbrendan /* 34455450Sbrendan * A write to a cache device has completed. Update all headers to allow 34465450Sbrendan * reads from these buffers to begin. 34475450Sbrendan */ 34485450Sbrendan static void 34495450Sbrendan l2arc_write_done(zio_t *zio) 34505450Sbrendan { 34515450Sbrendan l2arc_write_callback_t *cb; 34525450Sbrendan l2arc_dev_t *dev; 34535450Sbrendan list_t *buflist; 34545450Sbrendan l2arc_data_free_t *df, *df_prev; 34555450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 34565450Sbrendan kmutex_t *hash_lock; 34575450Sbrendan 34585450Sbrendan cb = zio->io_private; 34595450Sbrendan ASSERT(cb != NULL); 34605450Sbrendan dev = cb->l2wcb_dev; 34615450Sbrendan ASSERT(dev != NULL); 34625450Sbrendan head = cb->l2wcb_head; 34635450Sbrendan ASSERT(head != NULL); 34645450Sbrendan buflist = dev->l2ad_buflist; 34655450Sbrendan ASSERT(buflist != NULL); 34665450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 34675450Sbrendan l2arc_write_callback_t *, cb); 34685450Sbrendan 34695450Sbrendan if (zio->io_error != 0) 34705450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 34715450Sbrendan 34725450Sbrendan mutex_enter(&l2arc_buflist_mtx); 34735450Sbrendan 34745450Sbrendan /* 34755450Sbrendan * All writes completed, or an error was hit. 34765450Sbrendan */ 34775450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 34785450Sbrendan ab_prev = list_prev(buflist, ab); 34795450Sbrendan 34805450Sbrendan hash_lock = HDR_LOCK(ab); 34815450Sbrendan if (!mutex_tryenter(hash_lock)) { 34825450Sbrendan /* 34835450Sbrendan * This buffer misses out. It may be in a stage 34845450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 34855450Sbrendan * left set, denying reads to this buffer. 34865450Sbrendan */ 34875450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 34885450Sbrendan continue; 34895450Sbrendan } 34905450Sbrendan 34915450Sbrendan if (zio->io_error != 0) { 34925450Sbrendan /* 34935450Sbrendan * Error - invalidate L2ARC entry. 34945450Sbrendan */ 34955450Sbrendan ab->b_l2hdr = NULL; 34965450Sbrendan } 34975450Sbrendan 34985450Sbrendan /* 34995450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 35005450Sbrendan */ 35015450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 35025450Sbrendan 35035450Sbrendan mutex_exit(hash_lock); 35045450Sbrendan } 35055450Sbrendan 35065450Sbrendan atomic_inc_64(&l2arc_writes_done); 35075450Sbrendan list_remove(buflist, head); 35085450Sbrendan kmem_cache_free(hdr_cache, head); 35095450Sbrendan mutex_exit(&l2arc_buflist_mtx); 35105450Sbrendan 35115450Sbrendan /* 35125450Sbrendan * Free buffers that were tagged for destruction. 35135450Sbrendan */ 35145450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 35155450Sbrendan buflist = l2arc_free_on_write; 35165450Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 35175450Sbrendan df_prev = list_prev(buflist, df); 35185450Sbrendan ASSERT(df->l2df_data != NULL); 35195450Sbrendan ASSERT(df->l2df_func != NULL); 35205450Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 35215450Sbrendan list_remove(buflist, df); 35225450Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 35235450Sbrendan } 35245450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 35255450Sbrendan 35265450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 35275450Sbrendan } 35285450Sbrendan 35295450Sbrendan /* 35305450Sbrendan * A read to a cache device completed. Validate buffer contents before 35315450Sbrendan * handing over to the regular ARC routines. 35325450Sbrendan */ 35335450Sbrendan static void 35345450Sbrendan l2arc_read_done(zio_t *zio) 35355450Sbrendan { 35365450Sbrendan l2arc_read_callback_t *cb; 35375450Sbrendan arc_buf_hdr_t *hdr; 35385450Sbrendan arc_buf_t *buf; 35395450Sbrendan zio_t *rzio; 35405450Sbrendan kmutex_t *hash_lock; 35415450Sbrendan int equal, err = 0; 35425450Sbrendan 35435450Sbrendan cb = zio->io_private; 35445450Sbrendan ASSERT(cb != NULL); 35455450Sbrendan buf = cb->l2rcb_buf; 35465450Sbrendan ASSERT(buf != NULL); 35475450Sbrendan hdr = buf->b_hdr; 35485450Sbrendan ASSERT(hdr != NULL); 35495450Sbrendan 35505450Sbrendan hash_lock = HDR_LOCK(hdr); 35515450Sbrendan mutex_enter(hash_lock); 35525450Sbrendan 35535450Sbrendan /* 35545450Sbrendan * Check this survived the L2ARC journey. 35555450Sbrendan */ 35565450Sbrendan equal = arc_cksum_equal(buf); 35575450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 35585450Sbrendan mutex_exit(hash_lock); 35595450Sbrendan zio->io_private = buf; 35605450Sbrendan arc_read_done(zio); 35615450Sbrendan } else { 35625450Sbrendan mutex_exit(hash_lock); 35635450Sbrendan /* 35645450Sbrendan * Buffer didn't survive caching. Increment stats and 35655450Sbrendan * reissue to the original storage device. 35665450Sbrendan */ 35675450Sbrendan if (zio->io_error != 0) 35685450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 35695450Sbrendan if (!equal) 35705450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 35715450Sbrendan 35725450Sbrendan zio->io_flags &= ~ZIO_FLAG_DONT_CACHE; 35735450Sbrendan rzio = zio_read(NULL, cb->l2rcb_spa, &cb->l2rcb_bp, 35745450Sbrendan buf->b_data, zio->io_size, arc_read_done, buf, 35755450Sbrendan zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb); 35765450Sbrendan 35775450Sbrendan /* 35785450Sbrendan * Since this is a seperate thread, we can wait on this 35795450Sbrendan * I/O whether there is an io_waiter or not. 35805450Sbrendan */ 35815450Sbrendan err = zio_wait(rzio); 35825450Sbrendan 35835450Sbrendan /* 35845450Sbrendan * Let the resent I/O call arc_read_done() instead. 35855450Sbrendan * io_error is set to the reissued I/O error status. 35865450Sbrendan */ 35875450Sbrendan zio->io_done = NULL; 35885450Sbrendan zio->io_waiter = NULL; 35895450Sbrendan zio->io_error = err; 35905450Sbrendan } 35915450Sbrendan 35925450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 35935450Sbrendan } 35945450Sbrendan 35955450Sbrendan /* 35965450Sbrendan * This is the list priority from which the L2ARC will search for pages to 35975450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 35985450Sbrendan * desired order. This order can have a significant effect on cache 35995450Sbrendan * performance. 36005450Sbrendan * 36015450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 36025450Sbrendan * the data lists. This function returns a locked list, and also returns 36035450Sbrendan * the lock pointer. 36045450Sbrendan */ 36055450Sbrendan static list_t * 36065450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 36075450Sbrendan { 36085450Sbrendan list_t *list; 36095450Sbrendan 36105450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 36115450Sbrendan 36125450Sbrendan switch (list_num) { 36135450Sbrendan case 0: 36145450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 36155450Sbrendan *lock = &arc_mfu->arcs_mtx; 36165450Sbrendan break; 36175450Sbrendan case 1: 36185450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 36195450Sbrendan *lock = &arc_mru->arcs_mtx; 36205450Sbrendan break; 36215450Sbrendan case 2: 36225450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 36235450Sbrendan *lock = &arc_mfu->arcs_mtx; 36245450Sbrendan break; 36255450Sbrendan case 3: 36265450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 36275450Sbrendan *lock = &arc_mru->arcs_mtx; 36285450Sbrendan break; 36295450Sbrendan } 36305450Sbrendan 36315450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 36325450Sbrendan mutex_enter(*lock); 36335450Sbrendan return (list); 36345450Sbrendan } 36355450Sbrendan 36365450Sbrendan /* 36375450Sbrendan * Evict buffers from the device write hand to the distance specified in 36385450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 36395450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 36405450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 36415450Sbrendan */ 36425450Sbrendan static void 36435450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 36445450Sbrendan { 36455450Sbrendan list_t *buflist; 36465450Sbrendan l2arc_buf_hdr_t *abl2; 36475450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 36485450Sbrendan kmutex_t *hash_lock; 36495450Sbrendan uint64_t taddr; 36505450Sbrendan 36515450Sbrendan ASSERT(MUTEX_HELD(&l2arc_dev_mtx)); 36525450Sbrendan 36535450Sbrendan buflist = dev->l2ad_buflist; 36545450Sbrendan 36555450Sbrendan if (buflist == NULL) 36565450Sbrendan return; 36575450Sbrendan 36585450Sbrendan if (!all && dev->l2ad_first) { 36595450Sbrendan /* 36605450Sbrendan * This is the first sweep through the device. There is 36615450Sbrendan * nothing to evict. 36625450Sbrendan */ 36635450Sbrendan return; 36645450Sbrendan } 36655450Sbrendan 36665450Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * dev->l2ad_write))) { 36675450Sbrendan /* 36685450Sbrendan * When nearing the end of the device, evict to the end 36695450Sbrendan * before the device write hand jumps to the start. 36705450Sbrendan */ 36715450Sbrendan taddr = dev->l2ad_end; 36725450Sbrendan } else { 36735450Sbrendan taddr = dev->l2ad_hand + distance; 36745450Sbrendan } 36755450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 36765450Sbrendan uint64_t, taddr, boolean_t, all); 36775450Sbrendan 36785450Sbrendan top: 36795450Sbrendan mutex_enter(&l2arc_buflist_mtx); 36805450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 36815450Sbrendan ab_prev = list_prev(buflist, ab); 36825450Sbrendan 36835450Sbrendan hash_lock = HDR_LOCK(ab); 36845450Sbrendan if (!mutex_tryenter(hash_lock)) { 36855450Sbrendan /* 36865450Sbrendan * Missed the hash lock. Retry. 36875450Sbrendan */ 36885450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 36895450Sbrendan mutex_exit(&l2arc_buflist_mtx); 36905450Sbrendan mutex_enter(hash_lock); 36915450Sbrendan mutex_exit(hash_lock); 36925450Sbrendan goto top; 36935450Sbrendan } 36945450Sbrendan 36955450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 36965450Sbrendan /* 36975450Sbrendan * We hit a write head node. Leave it for 36985450Sbrendan * l2arc_write_done(). 36995450Sbrendan */ 37005450Sbrendan list_remove(buflist, ab); 37015450Sbrendan mutex_exit(hash_lock); 37025450Sbrendan continue; 37035450Sbrendan } 37045450Sbrendan 37055450Sbrendan if (!all && ab->b_l2hdr != NULL && 37065450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 37075450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 37085450Sbrendan /* 37095450Sbrendan * We've evicted to the target address, 37105450Sbrendan * or the end of the device. 37115450Sbrendan */ 37125450Sbrendan mutex_exit(hash_lock); 37135450Sbrendan break; 37145450Sbrendan } 37155450Sbrendan 37165450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 37175450Sbrendan /* 37185450Sbrendan * Already on the path to destruction. 37195450Sbrendan */ 37205450Sbrendan mutex_exit(hash_lock); 37215450Sbrendan continue; 37225450Sbrendan } 37235450Sbrendan 37245450Sbrendan if (ab->b_state == arc_l2c_only) { 37255450Sbrendan ASSERT(!HDR_L2_READING(ab)); 37265450Sbrendan /* 37275450Sbrendan * This doesn't exist in the ARC. Destroy. 37285450Sbrendan * arc_hdr_destroy() will call list_remove() 37295450Sbrendan * and decrement arcstat_l2_size. 37305450Sbrendan */ 37315450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 37325450Sbrendan arc_hdr_destroy(ab); 37335450Sbrendan } else { 37345450Sbrendan /* 37355450Sbrendan * Tell ARC this no longer exists in L2ARC. 37365450Sbrendan */ 37375450Sbrendan if (ab->b_l2hdr != NULL) { 37385450Sbrendan abl2 = ab->b_l2hdr; 37395450Sbrendan ab->b_l2hdr = NULL; 37405450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 37415450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 37425450Sbrendan } 37435450Sbrendan list_remove(buflist, ab); 37445450Sbrendan 37455450Sbrendan /* 37465450Sbrendan * This may have been leftover after a 37475450Sbrendan * failed write. 37485450Sbrendan */ 37495450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 37505450Sbrendan 37515450Sbrendan /* 37525450Sbrendan * Invalidate issued or about to be issued 37535450Sbrendan * reads, since we may be about to write 37545450Sbrendan * over this location. 37555450Sbrendan */ 37565450Sbrendan if (HDR_L2_READING(ab)) { 37575450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 37585450Sbrendan ab->b_flags |= ARC_L2_EVICTED; 37595450Sbrendan } 37605450Sbrendan } 37615450Sbrendan mutex_exit(hash_lock); 37625450Sbrendan } 37635450Sbrendan mutex_exit(&l2arc_buflist_mtx); 37645450Sbrendan 37655450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, -(taddr - dev->l2ad_evict)); 37665450Sbrendan dev->l2ad_evict = taddr; 37675450Sbrendan } 37685450Sbrendan 37695450Sbrendan /* 37705450Sbrendan * Find and write ARC buffers to the L2ARC device. 37715450Sbrendan * 37725450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 37735450Sbrendan * for reading until they have completed writing. 37745450Sbrendan */ 37755450Sbrendan static void 37765450Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev) 37775450Sbrendan { 37785450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 37795450Sbrendan l2arc_buf_hdr_t *hdrl2; 37805450Sbrendan list_t *list; 37815450Sbrendan uint64_t passed_sz, write_sz, buf_sz; 37825450Sbrendan uint64_t target_sz = dev->l2ad_write; 37835450Sbrendan uint64_t headroom = dev->l2ad_write * l2arc_headroom; 37845450Sbrendan void *buf_data; 37855450Sbrendan kmutex_t *hash_lock, *list_lock; 37865450Sbrendan boolean_t have_lock, full; 37875450Sbrendan l2arc_write_callback_t *cb; 37885450Sbrendan zio_t *pio, *wzio; 37895450Sbrendan 37905450Sbrendan ASSERT(MUTEX_HELD(&l2arc_dev_mtx)); 37915450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 37925450Sbrendan 37935450Sbrendan pio = NULL; 37945450Sbrendan write_sz = 0; 37955450Sbrendan full = B_FALSE; 37965450Sbrendan head = kmem_cache_alloc(hdr_cache, KM_SLEEP); 37975450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 37985450Sbrendan 37995450Sbrendan /* 38005450Sbrendan * Copy buffers for L2ARC writing. 38015450Sbrendan */ 38025450Sbrendan mutex_enter(&l2arc_buflist_mtx); 38035450Sbrendan for (int try = 0; try <= 3; try++) { 38045450Sbrendan list = l2arc_list_locked(try, &list_lock); 38055450Sbrendan passed_sz = 0; 38065450Sbrendan 38075450Sbrendan for (ab = list_tail(list); ab; ab = ab_prev) { 38085450Sbrendan ab_prev = list_prev(list, ab); 38095450Sbrendan 38105450Sbrendan hash_lock = HDR_LOCK(ab); 38115450Sbrendan have_lock = MUTEX_HELD(hash_lock); 38125450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 38135450Sbrendan /* 38145450Sbrendan * Skip this buffer rather than waiting. 38155450Sbrendan */ 38165450Sbrendan continue; 38175450Sbrendan } 38185450Sbrendan 38195450Sbrendan passed_sz += ab->b_size; 38205450Sbrendan if (passed_sz > headroom) { 38215450Sbrendan /* 38225450Sbrendan * Searched too far. 38235450Sbrendan */ 38245450Sbrendan mutex_exit(hash_lock); 38255450Sbrendan break; 38265450Sbrendan } 38275450Sbrendan 38285450Sbrendan if (ab->b_spa != spa) { 38295450Sbrendan mutex_exit(hash_lock); 38305450Sbrendan continue; 38315450Sbrendan } 38325450Sbrendan 38335450Sbrendan if (ab->b_l2hdr != NULL) { 38345450Sbrendan /* 38355450Sbrendan * Already in L2ARC. 38365450Sbrendan */ 38375450Sbrendan mutex_exit(hash_lock); 38385450Sbrendan continue; 38395450Sbrendan } 38405450Sbrendan 38415450Sbrendan if (HDR_IO_IN_PROGRESS(ab) || HDR_DONT_L2CACHE(ab)) { 38425450Sbrendan mutex_exit(hash_lock); 38435450Sbrendan continue; 38445450Sbrendan } 38455450Sbrendan 38465450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 38475450Sbrendan full = B_TRUE; 38485450Sbrendan mutex_exit(hash_lock); 38495450Sbrendan break; 38505450Sbrendan } 38515450Sbrendan 38525450Sbrendan if (ab->b_buf == NULL) { 38535450Sbrendan DTRACE_PROBE1(l2arc__buf__null, void *, ab); 38545450Sbrendan mutex_exit(hash_lock); 38555450Sbrendan continue; 38565450Sbrendan } 38575450Sbrendan 38585450Sbrendan if (pio == NULL) { 38595450Sbrendan /* 38605450Sbrendan * Insert a dummy header on the buflist so 38615450Sbrendan * l2arc_write_done() can find where the 38625450Sbrendan * write buffers begin without searching. 38635450Sbrendan */ 38645450Sbrendan list_insert_head(dev->l2ad_buflist, head); 38655450Sbrendan 38665450Sbrendan cb = kmem_alloc( 38675450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 38685450Sbrendan cb->l2wcb_dev = dev; 38695450Sbrendan cb->l2wcb_head = head; 38705450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 38715450Sbrendan ZIO_FLAG_CANFAIL); 38725450Sbrendan } 38735450Sbrendan 38745450Sbrendan /* 38755450Sbrendan * Create and add a new L2ARC header. 38765450Sbrendan */ 38775450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 38785450Sbrendan hdrl2->b_dev = dev; 38795450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 38805450Sbrendan 38815450Sbrendan ab->b_flags |= ARC_L2_WRITING; 38825450Sbrendan ab->b_l2hdr = hdrl2; 38835450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 38845450Sbrendan buf_data = ab->b_buf->b_data; 38855450Sbrendan buf_sz = ab->b_size; 38865450Sbrendan 38875450Sbrendan /* 38885450Sbrendan * Compute and store the buffer cksum before 38895450Sbrendan * writing. On debug the cksum is verified first. 38905450Sbrendan */ 38915450Sbrendan arc_cksum_verify(ab->b_buf); 38925450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 38935450Sbrendan 38945450Sbrendan mutex_exit(hash_lock); 38955450Sbrendan 38965450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 38975450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 38985450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 38995450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 39005450Sbrendan 39015450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 39025450Sbrendan zio_t *, wzio); 39035450Sbrendan (void) zio_nowait(wzio); 39045450Sbrendan 39055450Sbrendan write_sz += buf_sz; 39065450Sbrendan dev->l2ad_hand += buf_sz; 39075450Sbrendan } 39085450Sbrendan 39095450Sbrendan mutex_exit(list_lock); 39105450Sbrendan 39115450Sbrendan if (full == B_TRUE) 39125450Sbrendan break; 39135450Sbrendan } 39145450Sbrendan mutex_exit(&l2arc_buflist_mtx); 39155450Sbrendan 39165450Sbrendan if (pio == NULL) { 39175450Sbrendan ASSERT3U(write_sz, ==, 0); 39185450Sbrendan kmem_cache_free(hdr_cache, head); 39195450Sbrendan return; 39205450Sbrendan } 39215450Sbrendan 39225450Sbrendan ASSERT3U(write_sz, <=, target_sz); 39235450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 39245450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 39255450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, write_sz); 39265450Sbrendan 39275450Sbrendan /* 39285450Sbrendan * Bump device hand to the device start if it is approaching the end. 39295450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 39305450Sbrendan */ 39315450Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - dev->l2ad_write)) { 39325450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, 39335450Sbrendan dev->l2ad_end - dev->l2ad_hand); 39345450Sbrendan dev->l2ad_hand = dev->l2ad_start; 39355450Sbrendan dev->l2ad_evict = dev->l2ad_start; 39365450Sbrendan dev->l2ad_first = B_FALSE; 39375450Sbrendan } 39385450Sbrendan 39395450Sbrendan (void) zio_wait(pio); 39405450Sbrendan } 39415450Sbrendan 39425450Sbrendan /* 39435450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 39445450Sbrendan * heart of the L2ARC. 39455450Sbrendan */ 39465450Sbrendan static void 39475450Sbrendan l2arc_feed_thread(void) 39485450Sbrendan { 39495450Sbrendan callb_cpr_t cpr; 39505450Sbrendan l2arc_dev_t *dev; 39515450Sbrendan spa_t *spa; 39525450Sbrendan int interval; 39535450Sbrendan boolean_t startup = B_TRUE; 39545450Sbrendan 39555450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 39565450Sbrendan 39575450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 39585450Sbrendan 39595450Sbrendan while (l2arc_thread_exit == 0) { 39605450Sbrendan /* 39615450Sbrendan * Initially pause for L2ARC_FEED_DELAY seconds as a grace 39625450Sbrendan * interval during boot, followed by l2arc_feed_secs seconds 39635450Sbrendan * thereafter. 39645450Sbrendan */ 39655450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 39665450Sbrendan if (startup) { 39675450Sbrendan interval = L2ARC_FEED_DELAY; 39685450Sbrendan startup = B_FALSE; 39695450Sbrendan } else { 39705450Sbrendan interval = l2arc_feed_secs; 39715450Sbrendan } 39725450Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 39735450Sbrendan lbolt + (hz * interval)); 39745450Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 39755450Sbrendan 39765450Sbrendan /* 39775450Sbrendan * Do nothing until L2ARC devices exist. 39785450Sbrendan */ 39795450Sbrendan mutex_enter(&l2arc_dev_mtx); 39805450Sbrendan if (l2arc_ndev == 0) { 39815450Sbrendan mutex_exit(&l2arc_dev_mtx); 39825450Sbrendan continue; 39835450Sbrendan } 39845450Sbrendan 39855450Sbrendan /* 39865450Sbrendan * Avoid contributing to memory pressure. 39875450Sbrendan */ 39885450Sbrendan if (arc_reclaim_needed()) { 39895450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 39905450Sbrendan mutex_exit(&l2arc_dev_mtx); 39915450Sbrendan continue; 39925450Sbrendan } 39935450Sbrendan 39945450Sbrendan /* 39955450Sbrendan * This selects the next l2arc device to write to, and in 39965450Sbrendan * doing so the next spa to feed from: dev->l2ad_spa. 39975450Sbrendan */ 39985450Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) { 39995450Sbrendan mutex_exit(&l2arc_dev_mtx); 40005450Sbrendan continue; 40015450Sbrendan } 40025450Sbrendan spa = dev->l2ad_spa; 40035450Sbrendan ASSERT(spa != NULL); 40045450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 40055450Sbrendan 40065450Sbrendan /* 40075450Sbrendan * Evict L2ARC buffers that will be overwritten. 40085450Sbrendan */ 40095450Sbrendan l2arc_evict(dev, dev->l2ad_write, B_FALSE); 40105450Sbrendan 40115450Sbrendan /* 40125450Sbrendan * Write ARC buffers. 40135450Sbrendan */ 40145450Sbrendan l2arc_write_buffers(spa, dev); 40155450Sbrendan mutex_exit(&l2arc_dev_mtx); 40165450Sbrendan } 40175450Sbrendan 40185450Sbrendan l2arc_thread_exit = 0; 40195450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 40205450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 40215450Sbrendan thread_exit(); 40225450Sbrendan } 40235450Sbrendan 40245450Sbrendan /* 40255450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 40265450Sbrendan * validated the vdev and opened it. 40275450Sbrendan */ 40285450Sbrendan void 40295450Sbrendan l2arc_add_vdev(spa_t *spa, vdev_t *vd, uint64_t start, uint64_t end) 40305450Sbrendan { 40315450Sbrendan l2arc_dev_t *adddev; 40325450Sbrendan 40335450Sbrendan /* 40345450Sbrendan * Create a new l2arc device entry. 40355450Sbrendan */ 40365450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 40375450Sbrendan adddev->l2ad_spa = spa; 40385450Sbrendan adddev->l2ad_vdev = vd; 40395450Sbrendan adddev->l2ad_write = l2arc_write_max; 40405450Sbrendan adddev->l2ad_start = start; 40415450Sbrendan adddev->l2ad_end = end; 40425450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 40435450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 40445450Sbrendan adddev->l2ad_first = B_TRUE; 40455450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 40465450Sbrendan 40475450Sbrendan /* 40485450Sbrendan * This is a list of all ARC buffers that are still valid on the 40495450Sbrendan * device. 40505450Sbrendan */ 40515450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 40525450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 40535450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 40545450Sbrendan 40555450Sbrendan spa_l2cache_space_update(vd, adddev->l2ad_end - adddev->l2ad_hand, 0); 40565450Sbrendan 40575450Sbrendan /* 40585450Sbrendan * Add device to global list 40595450Sbrendan */ 40605450Sbrendan mutex_enter(&l2arc_dev_mtx); 40615450Sbrendan list_insert_head(l2arc_dev_list, adddev); 40625450Sbrendan atomic_inc_64(&l2arc_ndev); 40635450Sbrendan mutex_exit(&l2arc_dev_mtx); 40645450Sbrendan } 40655450Sbrendan 40665450Sbrendan /* 40675450Sbrendan * Remove a vdev from the L2ARC. 40685450Sbrendan */ 40695450Sbrendan void 40705450Sbrendan l2arc_remove_vdev(vdev_t *vd) 40715450Sbrendan { 40725450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 40735450Sbrendan 40745450Sbrendan /* 40755450Sbrendan * We can only grab the spa config lock when cache device writes 40765450Sbrendan * complete. 40775450Sbrendan */ 40785450Sbrendan ASSERT3U(l2arc_writes_sent, ==, l2arc_writes_done); 40795450Sbrendan 40805450Sbrendan /* 40815450Sbrendan * Find the device by vdev 40825450Sbrendan */ 40835450Sbrendan mutex_enter(&l2arc_dev_mtx); 40845450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 40855450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 40865450Sbrendan if (vd == dev->l2ad_vdev) { 40875450Sbrendan remdev = dev; 40885450Sbrendan break; 40895450Sbrendan } 40905450Sbrendan } 40915450Sbrendan ASSERT(remdev != NULL); 40925450Sbrendan 40935450Sbrendan /* 40945450Sbrendan * Remove device from global list 40955450Sbrendan */ 40965450Sbrendan list_remove(l2arc_dev_list, remdev); 40975450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 40985450Sbrendan 40995450Sbrendan /* 41005450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 41015450Sbrendan */ 41025450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 41035450Sbrendan list_destroy(remdev->l2ad_buflist); 41045450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 41055450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 41065450Sbrendan 41075450Sbrendan atomic_dec_64(&l2arc_ndev); 41085450Sbrendan mutex_exit(&l2arc_dev_mtx); 41095450Sbrendan } 41105450Sbrendan 41115450Sbrendan void 41125450Sbrendan l2arc_init() 41135450Sbrendan { 41145450Sbrendan l2arc_thread_exit = 0; 41155450Sbrendan l2arc_ndev = 0; 41165450Sbrendan l2arc_writes_sent = 0; 41175450Sbrendan l2arc_writes_done = 0; 41185450Sbrendan 41195450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 41205450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 41215450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 41225450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 41235450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 41245450Sbrendan 41255450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 41265450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 41275450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 41285450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 41295450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 41305450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 41315450Sbrendan 41325450Sbrendan (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 41335450Sbrendan TS_RUN, minclsyspri); 41345450Sbrendan } 41355450Sbrendan 41365450Sbrendan void 41375450Sbrendan l2arc_fini() 41385450Sbrendan { 41395450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 41405450Sbrendan cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 41415450Sbrendan l2arc_thread_exit = 1; 41425450Sbrendan while (l2arc_thread_exit != 0) 41435450Sbrendan cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 41445450Sbrendan mutex_exit(&l2arc_feed_thr_lock); 41455450Sbrendan 41465450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 41475450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 41485450Sbrendan mutex_destroy(&l2arc_dev_mtx); 41495450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 41505450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 41515450Sbrendan 41525450Sbrendan list_destroy(l2arc_dev_list); 41535450Sbrendan list_destroy(l2arc_free_on_write); 41545450Sbrendan } 4155