1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5033f9833Sek * Common Development and Distribution License (the "License"). 6033f9833Sek * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 225a98e54bSBrendan Gregg - Sun Microsystems * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens /* 2744cb6abcSbmc * DVA-based Adjustable Replacement Cache 28fa9e4066Sahrens * 29ea8dc4b6Seschrock * While much of the theory of operation used here is 30ea8dc4b6Seschrock * based on the self-tuning, low overhead replacement cache 31fa9e4066Sahrens * presented by Megiddo and Modha at FAST 2003, there are some 32fa9e4066Sahrens * significant differences: 33fa9e4066Sahrens * 34fa9e4066Sahrens * 1. The Megiddo and Modha model assumes any page is evictable. 35fa9e4066Sahrens * Pages in its cache cannot be "locked" into memory. This makes 36fa9e4066Sahrens * the eviction algorithm simple: evict the last page in the list. 37fa9e4066Sahrens * This also make the performance characteristics easy to reason 38fa9e4066Sahrens * about. Our cache is not so simple. At any given moment, some 39fa9e4066Sahrens * subset of the blocks in the cache are un-evictable because we 40fa9e4066Sahrens * have handed out a reference to them. Blocks are only evictable 41fa9e4066Sahrens * when there are no external references active. This makes 42fa9e4066Sahrens * eviction far more problematic: we choose to evict the evictable 43fa9e4066Sahrens * blocks that are the "lowest" in the list. 44fa9e4066Sahrens * 45fa9e4066Sahrens * There are times when it is not possible to evict the requested 46fa9e4066Sahrens * space. In these circumstances we are unable to adjust the cache 47fa9e4066Sahrens * size. To prevent the cache growing unbounded at these times we 48fa94a07fSbrendan * implement a "cache throttle" that slows the flow of new data 49fa94a07fSbrendan * into the cache until we can make space available. 50fa9e4066Sahrens * 51fa9e4066Sahrens * 2. The Megiddo and Modha model assumes a fixed cache size. 52fa9e4066Sahrens * Pages are evicted when the cache is full and there is a cache 53fa9e4066Sahrens * miss. Our model has a variable sized cache. It grows with 54fa94a07fSbrendan * high use, but also tries to react to memory pressure from the 55fa9e4066Sahrens * operating system: decreasing its size when system memory is 56fa9e4066Sahrens * tight. 57fa9e4066Sahrens * 58fa9e4066Sahrens * 3. The Megiddo and Modha model assumes a fixed page size. All 59fa9e4066Sahrens * elements of the cache are therefor exactly the same size. So 60fa9e4066Sahrens * when adjusting the cache size following a cache miss, its simply 61fa9e4066Sahrens * a matter of choosing a single page to evict. In our model, we 62fa9e4066Sahrens * have variable sized cache blocks (rangeing from 512 bytes to 63fa9e4066Sahrens * 128K bytes). We therefor choose a set of blocks to evict to make 64fa9e4066Sahrens * space for a cache miss that approximates as closely as possible 65fa9e4066Sahrens * the space used by the new block. 66fa9e4066Sahrens * 67fa9e4066Sahrens * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 68fa9e4066Sahrens * by N. Megiddo & D. Modha, FAST 2003 69fa9e4066Sahrens */ 70fa9e4066Sahrens 71fa9e4066Sahrens /* 72fa9e4066Sahrens * The locking model: 73fa9e4066Sahrens * 74fa9e4066Sahrens * A new reference to a cache buffer can be obtained in two 75fa9e4066Sahrens * ways: 1) via a hash table lookup using the DVA as a key, 76fa94a07fSbrendan * or 2) via one of the ARC lists. The arc_read() interface 77fa9e4066Sahrens * uses method 1, while the internal arc algorithms for 78fa9e4066Sahrens * adjusting the cache use method 2. We therefor provide two 79fa9e4066Sahrens * types of locks: 1) the hash table lock array, and 2) the 80fa9e4066Sahrens * arc list locks. 81fa9e4066Sahrens * 82fa9e4066Sahrens * Buffers do not have their own mutexs, rather they rely on the 83fa9e4066Sahrens * hash table mutexs for the bulk of their protection (i.e. most 84fa9e4066Sahrens * fields in the arc_buf_hdr_t are protected by these mutexs). 85fa9e4066Sahrens * 86fa9e4066Sahrens * buf_hash_find() returns the appropriate mutex (held) when it 87fa9e4066Sahrens * locates the requested buffer in the hash table. It returns 88fa9e4066Sahrens * NULL for the mutex if the buffer was not in the table. 89fa9e4066Sahrens * 90fa9e4066Sahrens * buf_hash_remove() expects the appropriate hash mutex to be 91fa9e4066Sahrens * already held before it is invoked. 92fa9e4066Sahrens * 93fa9e4066Sahrens * Each arc state also has a mutex which is used to protect the 94fa9e4066Sahrens * buffer list associated with the state. When attempting to 95fa9e4066Sahrens * obtain a hash table lock while holding an arc list lock you 96fa9e4066Sahrens * must use: mutex_tryenter() to avoid deadlock. Also note that 9744eda4d7Smaybee * the active state mutex must be held before the ghost state mutex. 98fa9e4066Sahrens * 99ea8dc4b6Seschrock * Arc buffers may have an associated eviction callback function. 100ea8dc4b6Seschrock * This function will be invoked prior to removing the buffer (e.g. 101ea8dc4b6Seschrock * in arc_do_user_evicts()). Note however that the data associated 102ea8dc4b6Seschrock * with the buffer may be evicted prior to the callback. The callback 103ea8dc4b6Seschrock * must be made with *no locks held* (to prevent deadlock). Additionally, 104ea8dc4b6Seschrock * the users of callbacks must ensure that their private data is 105ea8dc4b6Seschrock * protected from simultaneous callbacks from arc_buf_evict() 106ea8dc4b6Seschrock * and arc_do_user_evicts(). 107ea8dc4b6Seschrock * 108fa9e4066Sahrens * Note that the majority of the performance stats are manipulated 109fa9e4066Sahrens * with atomic operations. 110fa94a07fSbrendan * 111fa94a07fSbrendan * The L2ARC uses the l2arc_buflist_mtx global mutex for the following: 112fa94a07fSbrendan * 113fa94a07fSbrendan * - L2ARC buflist creation 114fa94a07fSbrendan * - L2ARC buflist eviction 115fa94a07fSbrendan * - L2ARC write completion, which walks L2ARC buflists 116fa94a07fSbrendan * - ARC header destruction, as it removes from L2ARC buflists 117fa94a07fSbrendan * - ARC header release, as it removes from L2ARC buflists 118fa9e4066Sahrens */ 119fa9e4066Sahrens 120fa9e4066Sahrens #include <sys/spa.h> 121fa9e4066Sahrens #include <sys/zio.h> 1226b4acc8bSahrens #include <sys/zio_checksum.h> 123fa9e4066Sahrens #include <sys/zfs_context.h> 124fa9e4066Sahrens #include <sys/arc.h> 125fa9e4066Sahrens #include <sys/refcount.h> 126c5904d13Seschrock #include <sys/vdev.h> 127573ca77eSGeorge Wilson #include <sys/vdev_impl.h> 128fa9e4066Sahrens #ifdef _KERNEL 129fa9e4066Sahrens #include <sys/vmsystm.h> 130fa9e4066Sahrens #include <vm/anon.h> 131fa9e4066Sahrens #include <sys/fs/swapnode.h> 132033f9833Sek #include <sys/dnlc.h> 133fa9e4066Sahrens #endif 134fa9e4066Sahrens #include <sys/callb.h> 13544cb6abcSbmc #include <sys/kstat.h> 136fa9e4066Sahrens 137fa9e4066Sahrens static kmutex_t arc_reclaim_thr_lock; 138fa9e4066Sahrens static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 139fa9e4066Sahrens static uint8_t arc_thread_exit; 140fa9e4066Sahrens 1411ab7f2deSmaybee extern int zfs_write_limit_shift; 1421ab7f2deSmaybee extern uint64_t zfs_write_limit_max; 14305715f94SMark Maybee extern kmutex_t zfs_write_limit_lock; 1441ab7f2deSmaybee 145033f9833Sek #define ARC_REDUCE_DNLC_PERCENT 3 146033f9833Sek uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 147033f9833Sek 148fa9e4066Sahrens typedef enum arc_reclaim_strategy { 149fa9e4066Sahrens ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 150fa9e4066Sahrens ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 151fa9e4066Sahrens } arc_reclaim_strategy_t; 152fa9e4066Sahrens 153fa9e4066Sahrens /* number of seconds before growing cache again */ 154fa9e4066Sahrens static int arc_grow_retry = 60; 155fa9e4066Sahrens 1565a98e54bSBrendan Gregg - Sun Microsystems /* shift of arc_c for calculating both min and max arc_p */ 1575a98e54bSBrendan Gregg - Sun Microsystems static int arc_p_min_shift = 4; 1585a98e54bSBrendan Gregg - Sun Microsystems 1595a98e54bSBrendan Gregg - Sun Microsystems /* log2(fraction of arc to reclaim) */ 1605a98e54bSBrendan Gregg - Sun Microsystems static int arc_shrink_shift = 5; 1615a98e54bSBrendan Gregg - Sun Microsystems 16213506d1eSmaybee /* 163b19a79ecSperrin * minimum lifespan of a prefetch block in clock ticks 164b19a79ecSperrin * (initialized in arc_init()) 16513506d1eSmaybee */ 166b19a79ecSperrin static int arc_min_prefetch_lifespan; 16713506d1eSmaybee 168fa9e4066Sahrens static int arc_dead; 169fa9e4066Sahrens 1703a737e0dSbrendan /* 1713a737e0dSbrendan * The arc has filled available memory and has now warmed up. 1723a737e0dSbrendan */ 1733a737e0dSbrendan static boolean_t arc_warm; 1743a737e0dSbrendan 175a2eea2e1Sahrens /* 176a2eea2e1Sahrens * These tunables are for performance analysis. 177a2eea2e1Sahrens */ 178a2eea2e1Sahrens uint64_t zfs_arc_max; 179a2eea2e1Sahrens uint64_t zfs_arc_min; 1801116048bSek uint64_t zfs_arc_meta_limit = 0; 181088f3894Sahrens int zfs_mdcomp_disable = 0; 1825a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_grow_retry = 0; 1835a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_shrink_shift = 0; 1845a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_p_min_shift = 0; 185a2eea2e1Sahrens 186fa9e4066Sahrens /* 187fa94a07fSbrendan * Note that buffers can be in one of 6 states: 188fa9e4066Sahrens * ARC_anon - anonymous (discussed below) 189ea8dc4b6Seschrock * ARC_mru - recently used, currently cached 190ea8dc4b6Seschrock * ARC_mru_ghost - recentely used, no longer in cache 191ea8dc4b6Seschrock * ARC_mfu - frequently used, currently cached 192ea8dc4b6Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 193fa94a07fSbrendan * ARC_l2c_only - exists in L2ARC but not other states 1940e8c6158Smaybee * When there are no active references to the buffer, they are 1950e8c6158Smaybee * are linked onto a list in one of these arc states. These are 1960e8c6158Smaybee * the only buffers that can be evicted or deleted. Within each 1970e8c6158Smaybee * state there are multiple lists, one for meta-data and one for 1980e8c6158Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 1990e8c6158Smaybee * etc.) is tracked separately so that it can be managed more 200fa94a07fSbrendan * explicitly: favored over data, limited explicitly. 201fa9e4066Sahrens * 202fa9e4066Sahrens * Anonymous buffers are buffers that are not associated with 203fa9e4066Sahrens * a DVA. These are buffers that hold dirty block copies 204fa9e4066Sahrens * before they are written to stable storage. By definition, 205ea8dc4b6Seschrock * they are "ref'd" and are considered part of arc_mru 206fa9e4066Sahrens * that cannot be freed. Generally, they will aquire a DVA 207ea8dc4b6Seschrock * as they are written and migrate onto the arc_mru list. 208fa94a07fSbrendan * 209fa94a07fSbrendan * The ARC_l2c_only state is for buffers that are in the second 210fa94a07fSbrendan * level ARC but no longer in any of the ARC_m* lists. The second 211fa94a07fSbrendan * level ARC itself may also contain buffers that are in any of 212fa94a07fSbrendan * the ARC_m* states - meaning that a buffer can exist in two 213fa94a07fSbrendan * places. The reason for the ARC_l2c_only state is to keep the 214fa94a07fSbrendan * buffer header in the hash table, so that reads that hit the 215fa94a07fSbrendan * second level ARC benefit from these fast lookups. 216fa9e4066Sahrens */ 217fa9e4066Sahrens 218fa9e4066Sahrens typedef struct arc_state { 2190e8c6158Smaybee list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */ 2200e8c6158Smaybee uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 2210e8c6158Smaybee uint64_t arcs_size; /* total amount of data in this state */ 22244cb6abcSbmc kmutex_t arcs_mtx; 223fa9e4066Sahrens } arc_state_t; 224fa9e4066Sahrens 225fa94a07fSbrendan /* The 6 states: */ 226fa9e4066Sahrens static arc_state_t ARC_anon; 227ea8dc4b6Seschrock static arc_state_t ARC_mru; 228ea8dc4b6Seschrock static arc_state_t ARC_mru_ghost; 229ea8dc4b6Seschrock static arc_state_t ARC_mfu; 230ea8dc4b6Seschrock static arc_state_t ARC_mfu_ghost; 231fa94a07fSbrendan static arc_state_t ARC_l2c_only; 232fa9e4066Sahrens 23344cb6abcSbmc typedef struct arc_stats { 23444cb6abcSbmc kstat_named_t arcstat_hits; 23544cb6abcSbmc kstat_named_t arcstat_misses; 23644cb6abcSbmc kstat_named_t arcstat_demand_data_hits; 23744cb6abcSbmc kstat_named_t arcstat_demand_data_misses; 23844cb6abcSbmc kstat_named_t arcstat_demand_metadata_hits; 23944cb6abcSbmc kstat_named_t arcstat_demand_metadata_misses; 24044cb6abcSbmc kstat_named_t arcstat_prefetch_data_hits; 24144cb6abcSbmc kstat_named_t arcstat_prefetch_data_misses; 24244cb6abcSbmc kstat_named_t arcstat_prefetch_metadata_hits; 24344cb6abcSbmc kstat_named_t arcstat_prefetch_metadata_misses; 24444cb6abcSbmc kstat_named_t arcstat_mru_hits; 24544cb6abcSbmc kstat_named_t arcstat_mru_ghost_hits; 24644cb6abcSbmc kstat_named_t arcstat_mfu_hits; 24744cb6abcSbmc kstat_named_t arcstat_mfu_ghost_hits; 24844cb6abcSbmc kstat_named_t arcstat_deleted; 24944cb6abcSbmc kstat_named_t arcstat_recycle_miss; 25044cb6abcSbmc kstat_named_t arcstat_mutex_miss; 25144cb6abcSbmc kstat_named_t arcstat_evict_skip; 252*5ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_cached; 253*5ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_eligible; 254*5ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_ineligible; 25544cb6abcSbmc kstat_named_t arcstat_hash_elements; 25644cb6abcSbmc kstat_named_t arcstat_hash_elements_max; 25744cb6abcSbmc kstat_named_t arcstat_hash_collisions; 25844cb6abcSbmc kstat_named_t arcstat_hash_chains; 25944cb6abcSbmc kstat_named_t arcstat_hash_chain_max; 26044cb6abcSbmc kstat_named_t arcstat_p; 26144cb6abcSbmc kstat_named_t arcstat_c; 26244cb6abcSbmc kstat_named_t arcstat_c_min; 26344cb6abcSbmc kstat_named_t arcstat_c_max; 26444cb6abcSbmc kstat_named_t arcstat_size; 265fa94a07fSbrendan kstat_named_t arcstat_hdr_size; 2665a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_data_size; 2675a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_other_size; 268fa94a07fSbrendan kstat_named_t arcstat_l2_hits; 269fa94a07fSbrendan kstat_named_t arcstat_l2_misses; 270fa94a07fSbrendan kstat_named_t arcstat_l2_feeds; 271fa94a07fSbrendan kstat_named_t arcstat_l2_rw_clash; 2725a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_l2_read_bytes; 2735a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_l2_write_bytes; 274fa94a07fSbrendan kstat_named_t arcstat_l2_writes_sent; 275fa94a07fSbrendan kstat_named_t arcstat_l2_writes_done; 276fa94a07fSbrendan kstat_named_t arcstat_l2_writes_error; 277fa94a07fSbrendan kstat_named_t arcstat_l2_writes_hdr_miss; 278fa94a07fSbrendan kstat_named_t arcstat_l2_evict_lock_retry; 279fa94a07fSbrendan kstat_named_t arcstat_l2_evict_reading; 280fa94a07fSbrendan kstat_named_t arcstat_l2_free_on_write; 281fa94a07fSbrendan kstat_named_t arcstat_l2_abort_lowmem; 282fa94a07fSbrendan kstat_named_t arcstat_l2_cksum_bad; 283fa94a07fSbrendan kstat_named_t arcstat_l2_io_error; 284fa94a07fSbrendan kstat_named_t arcstat_l2_size; 285fa94a07fSbrendan kstat_named_t arcstat_l2_hdr_size; 2861ab7f2deSmaybee kstat_named_t arcstat_memory_throttle_count; 28744cb6abcSbmc } arc_stats_t; 28844cb6abcSbmc 28944cb6abcSbmc static arc_stats_t arc_stats = { 29044cb6abcSbmc { "hits", KSTAT_DATA_UINT64 }, 29144cb6abcSbmc { "misses", KSTAT_DATA_UINT64 }, 29244cb6abcSbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 29344cb6abcSbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 29444cb6abcSbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 29544cb6abcSbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 29644cb6abcSbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 29744cb6abcSbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 29844cb6abcSbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 29944cb6abcSbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 30044cb6abcSbmc { "mru_hits", KSTAT_DATA_UINT64 }, 30144cb6abcSbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 30244cb6abcSbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 30344cb6abcSbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 30444cb6abcSbmc { "deleted", KSTAT_DATA_UINT64 }, 30544cb6abcSbmc { "recycle_miss", KSTAT_DATA_UINT64 }, 30644cb6abcSbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 30744cb6abcSbmc { "evict_skip", KSTAT_DATA_UINT64 }, 308*5ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_cached", KSTAT_DATA_UINT64 }, 309*5ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 310*5ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 31144cb6abcSbmc { "hash_elements", KSTAT_DATA_UINT64 }, 31244cb6abcSbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 31344cb6abcSbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 31444cb6abcSbmc { "hash_chains", KSTAT_DATA_UINT64 }, 31544cb6abcSbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 31644cb6abcSbmc { "p", KSTAT_DATA_UINT64 }, 31744cb6abcSbmc { "c", KSTAT_DATA_UINT64 }, 31844cb6abcSbmc { "c_min", KSTAT_DATA_UINT64 }, 31944cb6abcSbmc { "c_max", KSTAT_DATA_UINT64 }, 320fa94a07fSbrendan { "size", KSTAT_DATA_UINT64 }, 321fa94a07fSbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 3225a98e54bSBrendan Gregg - Sun Microsystems { "data_size", KSTAT_DATA_UINT64 }, 3235a98e54bSBrendan Gregg - Sun Microsystems { "other_size", KSTAT_DATA_UINT64 }, 324fa94a07fSbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 325fa94a07fSbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 326fa94a07fSbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 327fa94a07fSbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 3285a98e54bSBrendan Gregg - Sun Microsystems { "l2_read_bytes", KSTAT_DATA_UINT64 }, 3295a98e54bSBrendan Gregg - Sun Microsystems { "l2_write_bytes", KSTAT_DATA_UINT64 }, 330fa94a07fSbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 331fa94a07fSbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 332fa94a07fSbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 333fa94a07fSbrendan { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 334fa94a07fSbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 335fa94a07fSbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 336fa94a07fSbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 337fa94a07fSbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 338fa94a07fSbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 339fa94a07fSbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 340fa94a07fSbrendan { "l2_size", KSTAT_DATA_UINT64 }, 3411ab7f2deSmaybee { "l2_hdr_size", KSTAT_DATA_UINT64 }, 3421ab7f2deSmaybee { "memory_throttle_count", KSTAT_DATA_UINT64 } 34344cb6abcSbmc }; 34444cb6abcSbmc 34544cb6abcSbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 34644cb6abcSbmc 34744cb6abcSbmc #define ARCSTAT_INCR(stat, val) \ 34844cb6abcSbmc atomic_add_64(&arc_stats.stat.value.ui64, (val)); 34944cb6abcSbmc 35044cb6abcSbmc #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 35144cb6abcSbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 35244cb6abcSbmc 35344cb6abcSbmc #define ARCSTAT_MAX(stat, val) { \ 35444cb6abcSbmc uint64_t m; \ 35544cb6abcSbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 35644cb6abcSbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 35744cb6abcSbmc continue; \ 35844cb6abcSbmc } 35944cb6abcSbmc 36044cb6abcSbmc #define ARCSTAT_MAXSTAT(stat) \ 36144cb6abcSbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 36244cb6abcSbmc 36344cb6abcSbmc /* 36444cb6abcSbmc * We define a macro to allow ARC hits/misses to be easily broken down by 36544cb6abcSbmc * two separate conditions, giving a total of four different subtypes for 36644cb6abcSbmc * each of hits and misses (so eight statistics total). 36744cb6abcSbmc */ 36844cb6abcSbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 36944cb6abcSbmc if (cond1) { \ 37044cb6abcSbmc if (cond2) { \ 37144cb6abcSbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 37244cb6abcSbmc } else { \ 37344cb6abcSbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 37444cb6abcSbmc } \ 37544cb6abcSbmc } else { \ 37644cb6abcSbmc if (cond2) { \ 37744cb6abcSbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 37844cb6abcSbmc } else { \ 37944cb6abcSbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 38044cb6abcSbmc } \ 38144cb6abcSbmc } 38244cb6abcSbmc 38344cb6abcSbmc kstat_t *arc_ksp; 38444cb6abcSbmc static arc_state_t *arc_anon; 38544cb6abcSbmc static arc_state_t *arc_mru; 38644cb6abcSbmc static arc_state_t *arc_mru_ghost; 38744cb6abcSbmc static arc_state_t *arc_mfu; 38844cb6abcSbmc static arc_state_t *arc_mfu_ghost; 389fa94a07fSbrendan static arc_state_t *arc_l2c_only; 39044cb6abcSbmc 39144cb6abcSbmc /* 39244cb6abcSbmc * There are several ARC variables that are critical to export as kstats -- 39344cb6abcSbmc * but we don't want to have to grovel around in the kstat whenever we wish to 39444cb6abcSbmc * manipulate them. For these variables, we therefore define them to be in 39544cb6abcSbmc * terms of the statistic variable. This assures that we are not introducing 39644cb6abcSbmc * the possibility of inconsistency by having shadow copies of the variables, 39744cb6abcSbmc * while still allowing the code to be readable. 39844cb6abcSbmc */ 39944cb6abcSbmc #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 40044cb6abcSbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 40144cb6abcSbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 40244cb6abcSbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 40344cb6abcSbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 40444cb6abcSbmc 40544cb6abcSbmc static int arc_no_grow; /* Don't try to grow cache size */ 40644cb6abcSbmc static uint64_t arc_tempreserve; 4072fdbea25SAleksandr Guzovskiy static uint64_t arc_loaned_bytes; 4080e8c6158Smaybee static uint64_t arc_meta_used; 4090e8c6158Smaybee static uint64_t arc_meta_limit; 4100e8c6158Smaybee static uint64_t arc_meta_max = 0; 411fa9e4066Sahrens 412fa94a07fSbrendan typedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 413fa94a07fSbrendan 414fa9e4066Sahrens typedef struct arc_callback arc_callback_t; 415fa9e4066Sahrens 416fa9e4066Sahrens struct arc_callback { 417fa9e4066Sahrens void *acb_private; 418c717a561Smaybee arc_done_func_t *acb_done; 419fa9e4066Sahrens arc_buf_t *acb_buf; 420fa9e4066Sahrens zio_t *acb_zio_dummy; 421fa9e4066Sahrens arc_callback_t *acb_next; 422fa9e4066Sahrens }; 423fa9e4066Sahrens 424c717a561Smaybee typedef struct arc_write_callback arc_write_callback_t; 425c717a561Smaybee 426c717a561Smaybee struct arc_write_callback { 427c717a561Smaybee void *awcb_private; 428c717a561Smaybee arc_done_func_t *awcb_ready; 429c717a561Smaybee arc_done_func_t *awcb_done; 430c717a561Smaybee arc_buf_t *awcb_buf; 431c717a561Smaybee }; 432c717a561Smaybee 433fa9e4066Sahrens struct arc_buf_hdr { 434fa9e4066Sahrens /* protected by hash lock */ 435fa9e4066Sahrens dva_t b_dva; 436fa9e4066Sahrens uint64_t b_birth; 437fa9e4066Sahrens uint64_t b_cksum0; 438fa9e4066Sahrens 4396b4acc8bSahrens kmutex_t b_freeze_lock; 4406b4acc8bSahrens zio_cksum_t *b_freeze_cksum; 4416b4acc8bSahrens 442fa9e4066Sahrens arc_buf_hdr_t *b_hash_next; 443fa9e4066Sahrens arc_buf_t *b_buf; 444fa9e4066Sahrens uint32_t b_flags; 445ea8dc4b6Seschrock uint32_t b_datacnt; 446fa9e4066Sahrens 447fa9e4066Sahrens arc_callback_t *b_acb; 448ad23a2dbSjohansen kcondvar_t b_cv; 449ad23a2dbSjohansen 450ad23a2dbSjohansen /* immutable */ 451ad23a2dbSjohansen arc_buf_contents_t b_type; 452ad23a2dbSjohansen uint64_t b_size; 453ac05c741SMark Maybee uint64_t b_spa; 454fa9e4066Sahrens 455fa9e4066Sahrens /* protected by arc state mutex */ 456fa9e4066Sahrens arc_state_t *b_state; 457fa9e4066Sahrens list_node_t b_arc_node; 458fa9e4066Sahrens 459fa9e4066Sahrens /* updated atomically */ 460fa9e4066Sahrens clock_t b_arc_access; 461fa9e4066Sahrens 462fa9e4066Sahrens /* self protecting */ 463fa9e4066Sahrens refcount_t b_refcnt; 464fa94a07fSbrendan 465fa94a07fSbrendan l2arc_buf_hdr_t *b_l2hdr; 466fa94a07fSbrendan list_node_t b_l2node; 467fa9e4066Sahrens }; 468fa9e4066Sahrens 469ea8dc4b6Seschrock static arc_buf_t *arc_eviction_list; 470ea8dc4b6Seschrock static kmutex_t arc_eviction_mtx; 47140d7d650Smaybee static arc_buf_hdr_t arc_eviction_hdr; 47244eda4d7Smaybee static void arc_get_data_buf(arc_buf_t *buf); 47344eda4d7Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 4740e8c6158Smaybee static int arc_evict_needed(arc_buf_contents_t type); 475ac05c741SMark Maybee static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes); 476ea8dc4b6Seschrock 477*5ea40c06SBrendan Gregg - Sun Microsystems static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab); 478*5ea40c06SBrendan Gregg - Sun Microsystems 479ea8dc4b6Seschrock #define GHOST_STATE(state) \ 480fa94a07fSbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 481fa94a07fSbrendan (state) == arc_l2c_only) 482ea8dc4b6Seschrock 483fa9e4066Sahrens /* 484fa9e4066Sahrens * Private ARC flags. These flags are private ARC only flags that will show up 485fa9e4066Sahrens * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 486fa9e4066Sahrens * be passed in as arc_flags in things like arc_read. However, these flags 487fa9e4066Sahrens * should never be passed and should only be set by ARC code. When adding new 488fa9e4066Sahrens * public flags, make sure not to smash the private ones. 489fa9e4066Sahrens */ 490fa9e4066Sahrens 491ea8dc4b6Seschrock #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 492fa9e4066Sahrens #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 493fa9e4066Sahrens #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 494fa9e4066Sahrens #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 495ea8dc4b6Seschrock #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 49613506d1eSmaybee #define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 497fa94a07fSbrendan #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 4983baa08fcSek #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 4993baa08fcSek #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 5003baa08fcSek #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 5013baa08fcSek #define ARC_STORED (1 << 19) /* has been store()d to */ 502fa9e4066Sahrens 503ea8dc4b6Seschrock #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 504fa9e4066Sahrens #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 505fa9e4066Sahrens #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 5065a98e54bSBrendan Gregg - Sun Microsystems #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH) 507fa9e4066Sahrens #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 508ea8dc4b6Seschrock #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 509fa94a07fSbrendan #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 5103baa08fcSek #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 5113a737e0dSbrendan #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 5123a737e0dSbrendan (hdr)->b_l2hdr != NULL) 513fa94a07fSbrendan #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 514fa94a07fSbrendan #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 515fa94a07fSbrendan #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 516fa9e4066Sahrens 517e6c728e1Sbrendan /* 518e6c728e1Sbrendan * Other sizes 519e6c728e1Sbrendan */ 520e6c728e1Sbrendan 521e6c728e1Sbrendan #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 522e6c728e1Sbrendan #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 523e6c728e1Sbrendan 524fa9e4066Sahrens /* 525fa9e4066Sahrens * Hash table routines 526fa9e4066Sahrens */ 527fa9e4066Sahrens 528fa9e4066Sahrens #define HT_LOCK_PAD 64 529fa9e4066Sahrens 530fa9e4066Sahrens struct ht_lock { 531fa9e4066Sahrens kmutex_t ht_lock; 532fa9e4066Sahrens #ifdef _KERNEL 533fa9e4066Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 534fa9e4066Sahrens #endif 535fa9e4066Sahrens }; 536fa9e4066Sahrens 537fa9e4066Sahrens #define BUF_LOCKS 256 538fa9e4066Sahrens typedef struct buf_hash_table { 539fa9e4066Sahrens uint64_t ht_mask; 540fa9e4066Sahrens arc_buf_hdr_t **ht_table; 541fa9e4066Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 542fa9e4066Sahrens } buf_hash_table_t; 543fa9e4066Sahrens 544fa9e4066Sahrens static buf_hash_table_t buf_hash_table; 545fa9e4066Sahrens 546fa9e4066Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 547fa9e4066Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 548fa9e4066Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 549fa9e4066Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 550fa9e4066Sahrens #define HDR_LOCK(buf) \ 551fa9e4066Sahrens (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth))) 552fa9e4066Sahrens 553fa9e4066Sahrens uint64_t zfs_crc64_table[256]; 554fa9e4066Sahrens 555fa94a07fSbrendan /* 556fa94a07fSbrendan * Level 2 ARC 557fa94a07fSbrendan */ 558fa94a07fSbrendan 559fa94a07fSbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 5605a98e54bSBrendan Gregg - Sun Microsystems #define L2ARC_HEADROOM 2 /* num of writes */ 5615a98e54bSBrendan Gregg - Sun Microsystems #define L2ARC_FEED_SECS 1 /* caching interval secs */ 5625a98e54bSBrendan Gregg - Sun Microsystems #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 563fa94a07fSbrendan 564fa94a07fSbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 565fa94a07fSbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 566fa94a07fSbrendan 567fa94a07fSbrendan /* 568fa94a07fSbrendan * L2ARC Performance Tunables 569fa94a07fSbrendan */ 570fa94a07fSbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 5713a737e0dSbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 572fa94a07fSbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 573fa94a07fSbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 5745a98e54bSBrendan Gregg - Sun Microsystems uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 575fa94a07fSbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 5765a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 5775a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 578fa94a07fSbrendan 579fa94a07fSbrendan /* 580fa94a07fSbrendan * L2ARC Internals 581fa94a07fSbrendan */ 582fa94a07fSbrendan typedef struct l2arc_dev { 583fa94a07fSbrendan vdev_t *l2ad_vdev; /* vdev */ 584fa94a07fSbrendan spa_t *l2ad_spa; /* spa */ 585fa94a07fSbrendan uint64_t l2ad_hand; /* next write location */ 586fa94a07fSbrendan uint64_t l2ad_write; /* desired write size, bytes */ 5873a737e0dSbrendan uint64_t l2ad_boost; /* warmup write boost, bytes */ 588fa94a07fSbrendan uint64_t l2ad_start; /* first addr on device */ 589fa94a07fSbrendan uint64_t l2ad_end; /* last addr on device */ 590fa94a07fSbrendan uint64_t l2ad_evict; /* last addr eviction reached */ 591fa94a07fSbrendan boolean_t l2ad_first; /* first sweep through */ 5925a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2ad_writing; /* currently writing */ 593fa94a07fSbrendan list_t *l2ad_buflist; /* buffer list */ 594fa94a07fSbrendan list_node_t l2ad_node; /* device list node */ 595fa94a07fSbrendan } l2arc_dev_t; 596fa94a07fSbrendan 597fa94a07fSbrendan static list_t L2ARC_dev_list; /* device list */ 598fa94a07fSbrendan static list_t *l2arc_dev_list; /* device list pointer */ 599fa94a07fSbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 600fa94a07fSbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 601fa94a07fSbrendan static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 602fa94a07fSbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 603fa94a07fSbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 604fa94a07fSbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 605fa94a07fSbrendan static uint64_t l2arc_ndev; /* number of devices */ 606fa94a07fSbrendan 607fa94a07fSbrendan typedef struct l2arc_read_callback { 608fa94a07fSbrendan arc_buf_t *l2rcb_buf; /* read buffer */ 609fa94a07fSbrendan spa_t *l2rcb_spa; /* spa */ 610fa94a07fSbrendan blkptr_t l2rcb_bp; /* original blkptr */ 611fa94a07fSbrendan zbookmark_t l2rcb_zb; /* original bookmark */ 612fa94a07fSbrendan int l2rcb_flags; /* original flags */ 613fa94a07fSbrendan } l2arc_read_callback_t; 614fa94a07fSbrendan 615fa94a07fSbrendan typedef struct l2arc_write_callback { 616fa94a07fSbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 617fa94a07fSbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 618fa94a07fSbrendan } l2arc_write_callback_t; 619fa94a07fSbrendan 620fa94a07fSbrendan struct l2arc_buf_hdr { 621fa94a07fSbrendan /* protected by arc_buf_hdr mutex */ 622fa94a07fSbrendan l2arc_dev_t *b_dev; /* L2ARC device */ 623f9d8334fSGeorge Wilson uint64_t b_daddr; /* disk address, offset byte */ 624fa94a07fSbrendan }; 625fa94a07fSbrendan 626fa94a07fSbrendan typedef struct l2arc_data_free { 627fa94a07fSbrendan /* protected by l2arc_free_on_write_mtx */ 628fa94a07fSbrendan void *l2df_data; 629fa94a07fSbrendan size_t l2df_size; 630fa94a07fSbrendan void (*l2df_func)(void *, size_t); 631fa94a07fSbrendan list_node_t l2df_list_node; 632fa94a07fSbrendan } l2arc_data_free_t; 633fa94a07fSbrendan 634fa94a07fSbrendan static kmutex_t l2arc_feed_thr_lock; 635fa94a07fSbrendan static kcondvar_t l2arc_feed_thr_cv; 636fa94a07fSbrendan static uint8_t l2arc_thread_exit; 637fa94a07fSbrendan 638fa94a07fSbrendan static void l2arc_read_done(zio_t *zio); 639fa94a07fSbrendan static void l2arc_hdr_stat_add(void); 640fa94a07fSbrendan static void l2arc_hdr_stat_remove(void); 641fa94a07fSbrendan 642fa9e4066Sahrens static uint64_t 643ac05c741SMark Maybee buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 644fa9e4066Sahrens { 645fa9e4066Sahrens uint8_t *vdva = (uint8_t *)dva; 646fa9e4066Sahrens uint64_t crc = -1ULL; 647fa9e4066Sahrens int i; 648fa9e4066Sahrens 649fa9e4066Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 650fa9e4066Sahrens 651fa9e4066Sahrens for (i = 0; i < sizeof (dva_t); i++) 652fa9e4066Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 653fa9e4066Sahrens 654ac05c741SMark Maybee crc ^= (spa>>8) ^ birth; 655fa9e4066Sahrens 656fa9e4066Sahrens return (crc); 657fa9e4066Sahrens } 658fa9e4066Sahrens 659fa9e4066Sahrens #define BUF_EMPTY(buf) \ 660fa9e4066Sahrens ((buf)->b_dva.dva_word[0] == 0 && \ 661fa9e4066Sahrens (buf)->b_dva.dva_word[1] == 0 && \ 662fa9e4066Sahrens (buf)->b_birth == 0) 663fa9e4066Sahrens 664fa9e4066Sahrens #define BUF_EQUAL(spa, dva, birth, buf) \ 665fa9e4066Sahrens ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 666fa9e4066Sahrens ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 667fa9e4066Sahrens ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 668fa9e4066Sahrens 669fa9e4066Sahrens static arc_buf_hdr_t * 670ac05c741SMark Maybee buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 671fa9e4066Sahrens { 672fa9e4066Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 673fa9e4066Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 674fa9e4066Sahrens arc_buf_hdr_t *buf; 675fa9e4066Sahrens 676fa9e4066Sahrens mutex_enter(hash_lock); 677fa9e4066Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 678fa9e4066Sahrens buf = buf->b_hash_next) { 679fa9e4066Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 680fa9e4066Sahrens *lockp = hash_lock; 681fa9e4066Sahrens return (buf); 682fa9e4066Sahrens } 683fa9e4066Sahrens } 684fa9e4066Sahrens mutex_exit(hash_lock); 685fa9e4066Sahrens *lockp = NULL; 686fa9e4066Sahrens return (NULL); 687fa9e4066Sahrens } 688fa9e4066Sahrens 689fa9e4066Sahrens /* 690fa9e4066Sahrens * Insert an entry into the hash table. If there is already an element 691fa9e4066Sahrens * equal to elem in the hash table, then the already existing element 692fa9e4066Sahrens * will be returned and the new element will not be inserted. 693fa9e4066Sahrens * Otherwise returns NULL. 694fa9e4066Sahrens */ 695fa9e4066Sahrens static arc_buf_hdr_t * 696fa9e4066Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 697fa9e4066Sahrens { 698fa9e4066Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 699fa9e4066Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 700fa9e4066Sahrens arc_buf_hdr_t *fbuf; 70144cb6abcSbmc uint32_t i; 702fa9e4066Sahrens 703ea8dc4b6Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 704fa9e4066Sahrens *lockp = hash_lock; 705fa9e4066Sahrens mutex_enter(hash_lock); 706fa9e4066Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 707fa9e4066Sahrens fbuf = fbuf->b_hash_next, i++) { 708fa9e4066Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 709fa9e4066Sahrens return (fbuf); 710fa9e4066Sahrens } 711fa9e4066Sahrens 712fa9e4066Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 713fa9e4066Sahrens buf_hash_table.ht_table[idx] = buf; 714ea8dc4b6Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 715fa9e4066Sahrens 716fa9e4066Sahrens /* collect some hash table performance data */ 717fa9e4066Sahrens if (i > 0) { 71844cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_collisions); 719fa9e4066Sahrens if (i == 1) 72044cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_chains); 72144cb6abcSbmc 72244cb6abcSbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 723fa9e4066Sahrens } 72444cb6abcSbmc 72544cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_elements); 72644cb6abcSbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 727fa9e4066Sahrens 728fa9e4066Sahrens return (NULL); 729fa9e4066Sahrens } 730fa9e4066Sahrens 731fa9e4066Sahrens static void 732fa9e4066Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 733fa9e4066Sahrens { 734fa9e4066Sahrens arc_buf_hdr_t *fbuf, **bufp; 735fa9e4066Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 736fa9e4066Sahrens 737fa9e4066Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 738ea8dc4b6Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 739fa9e4066Sahrens 740fa9e4066Sahrens bufp = &buf_hash_table.ht_table[idx]; 741fa9e4066Sahrens while ((fbuf = *bufp) != buf) { 742fa9e4066Sahrens ASSERT(fbuf != NULL); 743fa9e4066Sahrens bufp = &fbuf->b_hash_next; 744fa9e4066Sahrens } 745fa9e4066Sahrens *bufp = buf->b_hash_next; 746fa9e4066Sahrens buf->b_hash_next = NULL; 747ea8dc4b6Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 748fa9e4066Sahrens 749fa9e4066Sahrens /* collect some hash table performance data */ 75044cb6abcSbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 75144cb6abcSbmc 752fa9e4066Sahrens if (buf_hash_table.ht_table[idx] && 753fa9e4066Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 75444cb6abcSbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 755fa9e4066Sahrens } 756fa9e4066Sahrens 757fa9e4066Sahrens /* 758fa9e4066Sahrens * Global data structures and functions for the buf kmem cache. 759fa9e4066Sahrens */ 760fa9e4066Sahrens static kmem_cache_t *hdr_cache; 761fa9e4066Sahrens static kmem_cache_t *buf_cache; 762fa9e4066Sahrens 763fa9e4066Sahrens static void 764fa9e4066Sahrens buf_fini(void) 765fa9e4066Sahrens { 766fa9e4066Sahrens int i; 767fa9e4066Sahrens 768fa9e4066Sahrens kmem_free(buf_hash_table.ht_table, 769fa9e4066Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 770fa9e4066Sahrens for (i = 0; i < BUF_LOCKS; i++) 771fa9e4066Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 772fa9e4066Sahrens kmem_cache_destroy(hdr_cache); 773fa9e4066Sahrens kmem_cache_destroy(buf_cache); 774fa9e4066Sahrens } 775fa9e4066Sahrens 776fa9e4066Sahrens /* 777fa9e4066Sahrens * Constructor callback - called when the cache is empty 778fa9e4066Sahrens * and a new buf is requested. 779fa9e4066Sahrens */ 780fa9e4066Sahrens /* ARGSUSED */ 781fa9e4066Sahrens static int 782fa9e4066Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 783fa9e4066Sahrens { 784fa9e4066Sahrens arc_buf_hdr_t *buf = vbuf; 785fa9e4066Sahrens 786fa9e4066Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 787fa9e4066Sahrens refcount_create(&buf->b_refcnt); 788fa9e4066Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 789c25056deSgw mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 7905a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 791fa94a07fSbrendan 792fa9e4066Sahrens return (0); 793fa9e4066Sahrens } 794fa9e4066Sahrens 7956f83844dSMark Maybee /* ARGSUSED */ 7966f83844dSMark Maybee static int 7976f83844dSMark Maybee buf_cons(void *vbuf, void *unused, int kmflag) 7986f83844dSMark Maybee { 7996f83844dSMark Maybee arc_buf_t *buf = vbuf; 8006f83844dSMark Maybee 8016f83844dSMark Maybee bzero(buf, sizeof (arc_buf_t)); 8026f83844dSMark Maybee rw_init(&buf->b_lock, NULL, RW_DEFAULT, NULL); 8035a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 8045a98e54bSBrendan Gregg - Sun Microsystems 8056f83844dSMark Maybee return (0); 8066f83844dSMark Maybee } 8076f83844dSMark Maybee 808fa9e4066Sahrens /* 809fa9e4066Sahrens * Destructor callback - called when a cached buf is 810fa9e4066Sahrens * no longer required. 811fa9e4066Sahrens */ 812fa9e4066Sahrens /* ARGSUSED */ 813fa9e4066Sahrens static void 814fa9e4066Sahrens hdr_dest(void *vbuf, void *unused) 815fa9e4066Sahrens { 816fa9e4066Sahrens arc_buf_hdr_t *buf = vbuf; 817fa9e4066Sahrens 818fa9e4066Sahrens refcount_destroy(&buf->b_refcnt); 819fa9e4066Sahrens cv_destroy(&buf->b_cv); 820c25056deSgw mutex_destroy(&buf->b_freeze_lock); 8215a98e54bSBrendan Gregg - Sun Microsystems arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 822fa9e4066Sahrens } 823fa9e4066Sahrens 8246f83844dSMark Maybee /* ARGSUSED */ 8256f83844dSMark Maybee static void 8266f83844dSMark Maybee buf_dest(void *vbuf, void *unused) 8276f83844dSMark Maybee { 8286f83844dSMark Maybee arc_buf_t *buf = vbuf; 8296f83844dSMark Maybee 8306f83844dSMark Maybee rw_destroy(&buf->b_lock); 8315a98e54bSBrendan Gregg - Sun Microsystems arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 8326f83844dSMark Maybee } 8336f83844dSMark Maybee 834fa9e4066Sahrens /* 835fa9e4066Sahrens * Reclaim callback -- invoked when memory is low. 836fa9e4066Sahrens */ 837fa9e4066Sahrens /* ARGSUSED */ 838fa9e4066Sahrens static void 839fa9e4066Sahrens hdr_recl(void *unused) 840fa9e4066Sahrens { 841fa9e4066Sahrens dprintf("hdr_recl called\n"); 84249e3519aSmaybee /* 84349e3519aSmaybee * umem calls the reclaim func when we destroy the buf cache, 84449e3519aSmaybee * which is after we do arc_fini(). 84549e3519aSmaybee */ 84649e3519aSmaybee if (!arc_dead) 84749e3519aSmaybee cv_signal(&arc_reclaim_thr_cv); 848fa9e4066Sahrens } 849fa9e4066Sahrens 850fa9e4066Sahrens static void 851fa9e4066Sahrens buf_init(void) 852fa9e4066Sahrens { 853fa9e4066Sahrens uint64_t *ct; 854ea8dc4b6Seschrock uint64_t hsize = 1ULL << 12; 855fa9e4066Sahrens int i, j; 856fa9e4066Sahrens 857fa9e4066Sahrens /* 858fa9e4066Sahrens * The hash table is big enough to fill all of physical memory 859ea8dc4b6Seschrock * with an average 64K block size. The table will take up 860ea8dc4b6Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 861fa9e4066Sahrens */ 862ea8dc4b6Seschrock while (hsize * 65536 < physmem * PAGESIZE) 863fa9e4066Sahrens hsize <<= 1; 864ea8dc4b6Seschrock retry: 865fa9e4066Sahrens buf_hash_table.ht_mask = hsize - 1; 866ea8dc4b6Seschrock buf_hash_table.ht_table = 867ea8dc4b6Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 868ea8dc4b6Seschrock if (buf_hash_table.ht_table == NULL) { 869ea8dc4b6Seschrock ASSERT(hsize > (1ULL << 8)); 870ea8dc4b6Seschrock hsize >>= 1; 871ea8dc4b6Seschrock goto retry; 872ea8dc4b6Seschrock } 873fa9e4066Sahrens 874fa9e4066Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 875fa9e4066Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 876fa9e4066Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 8776f83844dSMark Maybee 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 878fa9e4066Sahrens 879fa9e4066Sahrens for (i = 0; i < 256; i++) 880fa9e4066Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 881fa9e4066Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 882fa9e4066Sahrens 883fa9e4066Sahrens for (i = 0; i < BUF_LOCKS; i++) { 884fa9e4066Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 885fa9e4066Sahrens NULL, MUTEX_DEFAULT, NULL); 886fa9e4066Sahrens } 887fa9e4066Sahrens } 888fa9e4066Sahrens 889fa9e4066Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 890fa9e4066Sahrens 8916b4acc8bSahrens static void 8926b4acc8bSahrens arc_cksum_verify(arc_buf_t *buf) 8936b4acc8bSahrens { 8946b4acc8bSahrens zio_cksum_t zc; 8956b4acc8bSahrens 896cc60fd72Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 8976b4acc8bSahrens return; 8986b4acc8bSahrens 8996b4acc8bSahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9003ccfa83cSahrens if (buf->b_hdr->b_freeze_cksum == NULL || 9013ccfa83cSahrens (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 9026b4acc8bSahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9036b4acc8bSahrens return; 9046b4acc8bSahrens } 9056b4acc8bSahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 9066b4acc8bSahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 9076b4acc8bSahrens panic("buffer modified while frozen!"); 9086b4acc8bSahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9096b4acc8bSahrens } 9106b4acc8bSahrens 911fa94a07fSbrendan static int 912fa94a07fSbrendan arc_cksum_equal(arc_buf_t *buf) 913fa94a07fSbrendan { 914fa94a07fSbrendan zio_cksum_t zc; 915fa94a07fSbrendan int equal; 916fa94a07fSbrendan 917fa94a07fSbrendan mutex_enter(&buf->b_hdr->b_freeze_lock); 918fa94a07fSbrendan fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 919fa94a07fSbrendan equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 920fa94a07fSbrendan mutex_exit(&buf->b_hdr->b_freeze_lock); 921fa94a07fSbrendan 922fa94a07fSbrendan return (equal); 923fa94a07fSbrendan } 924fa94a07fSbrendan 9256b4acc8bSahrens static void 926fa94a07fSbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force) 9276b4acc8bSahrens { 928fa94a07fSbrendan if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 9296b4acc8bSahrens return; 9306b4acc8bSahrens 9316b4acc8bSahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9326b4acc8bSahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9336b4acc8bSahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9346b4acc8bSahrens return; 9356b4acc8bSahrens } 9366b4acc8bSahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 9376b4acc8bSahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 9386b4acc8bSahrens buf->b_hdr->b_freeze_cksum); 9396b4acc8bSahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9406b4acc8bSahrens } 9416b4acc8bSahrens 9426b4acc8bSahrens void 9436b4acc8bSahrens arc_buf_thaw(arc_buf_t *buf) 9446b4acc8bSahrens { 945fa94a07fSbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 946fa94a07fSbrendan if (buf->b_hdr->b_state != arc_anon) 947fa94a07fSbrendan panic("modifying non-anon buffer!"); 948fa94a07fSbrendan if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 949fa94a07fSbrendan panic("modifying buffer while i/o in progress!"); 950fa94a07fSbrendan arc_cksum_verify(buf); 951fa94a07fSbrendan } 9526b4acc8bSahrens 9536b4acc8bSahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9546b4acc8bSahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9556b4acc8bSahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 9566b4acc8bSahrens buf->b_hdr->b_freeze_cksum = NULL; 9576b4acc8bSahrens } 9586b4acc8bSahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9596b4acc8bSahrens } 9606b4acc8bSahrens 9616b4acc8bSahrens void 9626b4acc8bSahrens arc_buf_freeze(arc_buf_t *buf) 9636b4acc8bSahrens { 964cc60fd72Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 965cc60fd72Sahrens return; 966cc60fd72Sahrens 9676b4acc8bSahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 96844cb6abcSbmc buf->b_hdr->b_state == arc_anon); 969fa94a07fSbrendan arc_cksum_compute(buf, B_FALSE); 9706b4acc8bSahrens } 9716b4acc8bSahrens 972fa9e4066Sahrens static void 973fa9e4066Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 974fa9e4066Sahrens { 975fa9e4066Sahrens ASSERT(MUTEX_HELD(hash_lock)); 976fa9e4066Sahrens 977fa9e4066Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 97844cb6abcSbmc (ab->b_state != arc_anon)) { 979c0a81264Sek uint64_t delta = ab->b_size * ab->b_datacnt; 9800e8c6158Smaybee list_t *list = &ab->b_state->arcs_list[ab->b_type]; 9810e8c6158Smaybee uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 982fa9e4066Sahrens 98344cb6abcSbmc ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx)); 98444cb6abcSbmc mutex_enter(&ab->b_state->arcs_mtx); 985fa9e4066Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 9860e8c6158Smaybee list_remove(list, ab); 987ea8dc4b6Seschrock if (GHOST_STATE(ab->b_state)) { 988ea8dc4b6Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 989ea8dc4b6Seschrock ASSERT3P(ab->b_buf, ==, NULL); 990ea8dc4b6Seschrock delta = ab->b_size; 991ea8dc4b6Seschrock } 992ea8dc4b6Seschrock ASSERT(delta > 0); 9930e8c6158Smaybee ASSERT3U(*size, >=, delta); 9940e8c6158Smaybee atomic_add_64(size, -delta); 99544cb6abcSbmc mutex_exit(&ab->b_state->arcs_mtx); 996088f3894Sahrens /* remove the prefetch flag if we get a reference */ 99713506d1eSmaybee if (ab->b_flags & ARC_PREFETCH) 99813506d1eSmaybee ab->b_flags &= ~ARC_PREFETCH; 999fa9e4066Sahrens } 1000fa9e4066Sahrens } 1001fa9e4066Sahrens 1002fa9e4066Sahrens static int 1003fa9e4066Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1004fa9e4066Sahrens { 1005fa9e4066Sahrens int cnt; 100644cb6abcSbmc arc_state_t *state = ab->b_state; 1007fa9e4066Sahrens 100844cb6abcSbmc ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 100944cb6abcSbmc ASSERT(!GHOST_STATE(state)); 1010fa9e4066Sahrens 1011fa9e4066Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 101244cb6abcSbmc (state != arc_anon)) { 10130e8c6158Smaybee uint64_t *size = &state->arcs_lsize[ab->b_type]; 10140e8c6158Smaybee 101544cb6abcSbmc ASSERT(!MUTEX_HELD(&state->arcs_mtx)); 101644cb6abcSbmc mutex_enter(&state->arcs_mtx); 1017fa9e4066Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 10180e8c6158Smaybee list_insert_head(&state->arcs_list[ab->b_type], ab); 1019ea8dc4b6Seschrock ASSERT(ab->b_datacnt > 0); 10200e8c6158Smaybee atomic_add_64(size, ab->b_size * ab->b_datacnt); 102144cb6abcSbmc mutex_exit(&state->arcs_mtx); 1022fa9e4066Sahrens } 1023fa9e4066Sahrens return (cnt); 1024fa9e4066Sahrens } 1025fa9e4066Sahrens 1026fa9e4066Sahrens /* 1027fa9e4066Sahrens * Move the supplied buffer to the indicated state. The mutex 1028fa9e4066Sahrens * for the buffer must be held by the caller. 1029fa9e4066Sahrens */ 1030fa9e4066Sahrens static void 1031ea8dc4b6Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1032fa9e4066Sahrens { 1033ea8dc4b6Seschrock arc_state_t *old_state = ab->b_state; 1034c0a81264Sek int64_t refcnt = refcount_count(&ab->b_refcnt); 1035c0a81264Sek uint64_t from_delta, to_delta; 1036fa9e4066Sahrens 1037fa9e4066Sahrens ASSERT(MUTEX_HELD(hash_lock)); 1038ea8dc4b6Seschrock ASSERT(new_state != old_state); 1039ea8dc4b6Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 1040ea8dc4b6Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 1041ea8dc4b6Seschrock 1042ea8dc4b6Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 1043fa9e4066Sahrens 1044fa9e4066Sahrens /* 1045fa9e4066Sahrens * If this buffer is evictable, transfer it from the 1046fa9e4066Sahrens * old state list to the new state list. 1047fa9e4066Sahrens */ 1048ea8dc4b6Seschrock if (refcnt == 0) { 104944cb6abcSbmc if (old_state != arc_anon) { 105044cb6abcSbmc int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx); 10510e8c6158Smaybee uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 1052ea8dc4b6Seschrock 1053ea8dc4b6Seschrock if (use_mutex) 105444cb6abcSbmc mutex_enter(&old_state->arcs_mtx); 1055fa9e4066Sahrens 1056fa9e4066Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 10570e8c6158Smaybee list_remove(&old_state->arcs_list[ab->b_type], ab); 1058ea8dc4b6Seschrock 105913506d1eSmaybee /* 106013506d1eSmaybee * If prefetching out of the ghost cache, 106113506d1eSmaybee * we will have a non-null datacnt. 106213506d1eSmaybee */ 106313506d1eSmaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 106413506d1eSmaybee /* ghost elements have a ghost size */ 1065ea8dc4b6Seschrock ASSERT(ab->b_buf == NULL); 1066ea8dc4b6Seschrock from_delta = ab->b_size; 1067ea8dc4b6Seschrock } 10680e8c6158Smaybee ASSERT3U(*size, >=, from_delta); 10690e8c6158Smaybee atomic_add_64(size, -from_delta); 1070ea8dc4b6Seschrock 1071ea8dc4b6Seschrock if (use_mutex) 107244cb6abcSbmc mutex_exit(&old_state->arcs_mtx); 1073fa9e4066Sahrens } 107444cb6abcSbmc if (new_state != arc_anon) { 107544cb6abcSbmc int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx); 10760e8c6158Smaybee uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1077fa9e4066Sahrens 1078ea8dc4b6Seschrock if (use_mutex) 107944cb6abcSbmc mutex_enter(&new_state->arcs_mtx); 1080ea8dc4b6Seschrock 10810e8c6158Smaybee list_insert_head(&new_state->arcs_list[ab->b_type], ab); 1082ea8dc4b6Seschrock 1083ea8dc4b6Seschrock /* ghost elements have a ghost size */ 1084ea8dc4b6Seschrock if (GHOST_STATE(new_state)) { 1085ea8dc4b6Seschrock ASSERT(ab->b_datacnt == 0); 1086ea8dc4b6Seschrock ASSERT(ab->b_buf == NULL); 1087ea8dc4b6Seschrock to_delta = ab->b_size; 1088ea8dc4b6Seschrock } 10890e8c6158Smaybee atomic_add_64(size, to_delta); 1090ea8dc4b6Seschrock 1091ea8dc4b6Seschrock if (use_mutex) 109244cb6abcSbmc mutex_exit(&new_state->arcs_mtx); 1093fa9e4066Sahrens } 1094fa9e4066Sahrens } 1095fa9e4066Sahrens 1096fa9e4066Sahrens ASSERT(!BUF_EMPTY(ab)); 1097fa94a07fSbrendan if (new_state == arc_anon) { 1098fa9e4066Sahrens buf_hash_remove(ab); 1099fa9e4066Sahrens } 1100fa9e4066Sahrens 1101ea8dc4b6Seschrock /* adjust state sizes */ 1102ea8dc4b6Seschrock if (to_delta) 110344cb6abcSbmc atomic_add_64(&new_state->arcs_size, to_delta); 1104ea8dc4b6Seschrock if (from_delta) { 110544cb6abcSbmc ASSERT3U(old_state->arcs_size, >=, from_delta); 110644cb6abcSbmc atomic_add_64(&old_state->arcs_size, -from_delta); 1107fa9e4066Sahrens } 1108fa9e4066Sahrens ab->b_state = new_state; 1109fa94a07fSbrendan 1110fa94a07fSbrendan /* adjust l2arc hdr stats */ 1111fa94a07fSbrendan if (new_state == arc_l2c_only) 1112fa94a07fSbrendan l2arc_hdr_stat_add(); 1113fa94a07fSbrendan else if (old_state == arc_l2c_only) 1114fa94a07fSbrendan l2arc_hdr_stat_remove(); 1115fa9e4066Sahrens } 1116fa9e4066Sahrens 11170e8c6158Smaybee void 11185a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(uint64_t space, arc_space_type_t type) 11190e8c6158Smaybee { 11205a98e54bSBrendan Gregg - Sun Microsystems ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11215a98e54bSBrendan Gregg - Sun Microsystems 11225a98e54bSBrendan Gregg - Sun Microsystems switch (type) { 11235a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_DATA: 11245a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_data_size, space); 11255a98e54bSBrendan Gregg - Sun Microsystems break; 11265a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_OTHER: 11275a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_other_size, space); 11285a98e54bSBrendan Gregg - Sun Microsystems break; 11295a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_HDRS: 11305a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_hdr_size, space); 11315a98e54bSBrendan Gregg - Sun Microsystems break; 11325a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_L2HDRS: 11335a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_l2_hdr_size, space); 11345a98e54bSBrendan Gregg - Sun Microsystems break; 11355a98e54bSBrendan Gregg - Sun Microsystems } 11365a98e54bSBrendan Gregg - Sun Microsystems 11370e8c6158Smaybee atomic_add_64(&arc_meta_used, space); 11380e8c6158Smaybee atomic_add_64(&arc_size, space); 11390e8c6158Smaybee } 11400e8c6158Smaybee 11410e8c6158Smaybee void 11425a98e54bSBrendan Gregg - Sun Microsystems arc_space_return(uint64_t space, arc_space_type_t type) 11430e8c6158Smaybee { 11445a98e54bSBrendan Gregg - Sun Microsystems ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 11455a98e54bSBrendan Gregg - Sun Microsystems 11465a98e54bSBrendan Gregg - Sun Microsystems switch (type) { 11475a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_DATA: 11485a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_data_size, -space); 11495a98e54bSBrendan Gregg - Sun Microsystems break; 11505a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_OTHER: 11515a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_other_size, -space); 11525a98e54bSBrendan Gregg - Sun Microsystems break; 11535a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_HDRS: 11545a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_hdr_size, -space); 11555a98e54bSBrendan Gregg - Sun Microsystems break; 11565a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_L2HDRS: 11575a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 11585a98e54bSBrendan Gregg - Sun Microsystems break; 11595a98e54bSBrendan Gregg - Sun Microsystems } 11605a98e54bSBrendan Gregg - Sun Microsystems 11610e8c6158Smaybee ASSERT(arc_meta_used >= space); 11620e8c6158Smaybee if (arc_meta_max < arc_meta_used) 11630e8c6158Smaybee arc_meta_max = arc_meta_used; 11640e8c6158Smaybee atomic_add_64(&arc_meta_used, -space); 11650e8c6158Smaybee ASSERT(arc_size >= space); 11660e8c6158Smaybee atomic_add_64(&arc_size, -space); 11670e8c6158Smaybee } 11680e8c6158Smaybee 11690e8c6158Smaybee void * 11700e8c6158Smaybee arc_data_buf_alloc(uint64_t size) 11710e8c6158Smaybee { 11720e8c6158Smaybee if (arc_evict_needed(ARC_BUFC_DATA)) 11730e8c6158Smaybee cv_signal(&arc_reclaim_thr_cv); 11740e8c6158Smaybee atomic_add_64(&arc_size, size); 11750e8c6158Smaybee return (zio_data_buf_alloc(size)); 11760e8c6158Smaybee } 11770e8c6158Smaybee 11780e8c6158Smaybee void 11790e8c6158Smaybee arc_data_buf_free(void *buf, uint64_t size) 11800e8c6158Smaybee { 11810e8c6158Smaybee zio_data_buf_free(buf, size); 11820e8c6158Smaybee ASSERT(arc_size >= size); 11830e8c6158Smaybee atomic_add_64(&arc_size, -size); 11840e8c6158Smaybee } 11850e8c6158Smaybee 1186fa9e4066Sahrens arc_buf_t * 1187ad23a2dbSjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1188fa9e4066Sahrens { 1189fa9e4066Sahrens arc_buf_hdr_t *hdr; 1190fa9e4066Sahrens arc_buf_t *buf; 1191fa9e4066Sahrens 1192fa9e4066Sahrens ASSERT3U(size, >, 0); 11931ab7f2deSmaybee hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1194fa9e4066Sahrens ASSERT(BUF_EMPTY(hdr)); 1195fa9e4066Sahrens hdr->b_size = size; 1196ad23a2dbSjohansen hdr->b_type = type; 1197ac05c741SMark Maybee hdr->b_spa = spa_guid(spa); 119844cb6abcSbmc hdr->b_state = arc_anon; 1199fa9e4066Sahrens hdr->b_arc_access = 0; 12001ab7f2deSmaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1201fa9e4066Sahrens buf->b_hdr = hdr; 120244eda4d7Smaybee buf->b_data = NULL; 1203ea8dc4b6Seschrock buf->b_efunc = NULL; 1204ea8dc4b6Seschrock buf->b_private = NULL; 1205fa9e4066Sahrens buf->b_next = NULL; 1206fa9e4066Sahrens hdr->b_buf = buf; 120744eda4d7Smaybee arc_get_data_buf(buf); 1208ea8dc4b6Seschrock hdr->b_datacnt = 1; 1209fa9e4066Sahrens hdr->b_flags = 0; 1210fa9e4066Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1211fa9e4066Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 1212fa9e4066Sahrens 1213fa9e4066Sahrens return (buf); 1214fa9e4066Sahrens } 1215fa9e4066Sahrens 12162fdbea25SAleksandr Guzovskiy static char *arc_onloan_tag = "onloan"; 12172fdbea25SAleksandr Guzovskiy 12182fdbea25SAleksandr Guzovskiy /* 12192fdbea25SAleksandr Guzovskiy * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 12202fdbea25SAleksandr Guzovskiy * flight data by arc_tempreserve_space() until they are "returned". Loaned 12212fdbea25SAleksandr Guzovskiy * buffers must be returned to the arc before they can be used by the DMU or 12222fdbea25SAleksandr Guzovskiy * freed. 12232fdbea25SAleksandr Guzovskiy */ 12242fdbea25SAleksandr Guzovskiy arc_buf_t * 12252fdbea25SAleksandr Guzovskiy arc_loan_buf(spa_t *spa, int size) 12262fdbea25SAleksandr Guzovskiy { 12272fdbea25SAleksandr Guzovskiy arc_buf_t *buf; 12282fdbea25SAleksandr Guzovskiy 12292fdbea25SAleksandr Guzovskiy buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 12302fdbea25SAleksandr Guzovskiy 12312fdbea25SAleksandr Guzovskiy atomic_add_64(&arc_loaned_bytes, size); 12322fdbea25SAleksandr Guzovskiy return (buf); 12332fdbea25SAleksandr Guzovskiy } 12342fdbea25SAleksandr Guzovskiy 12352fdbea25SAleksandr Guzovskiy /* 12362fdbea25SAleksandr Guzovskiy * Return a loaned arc buffer to the arc. 12372fdbea25SAleksandr Guzovskiy */ 12382fdbea25SAleksandr Guzovskiy void 12392fdbea25SAleksandr Guzovskiy arc_return_buf(arc_buf_t *buf, void *tag) 12402fdbea25SAleksandr Guzovskiy { 12412fdbea25SAleksandr Guzovskiy arc_buf_hdr_t *hdr = buf->b_hdr; 12422fdbea25SAleksandr Guzovskiy 12432fdbea25SAleksandr Guzovskiy ASSERT(hdr->b_state == arc_anon); 12442fdbea25SAleksandr Guzovskiy ASSERT(buf->b_data != NULL); 12452fdbea25SAleksandr Guzovskiy VERIFY(refcount_remove(&hdr->b_refcnt, arc_onloan_tag) == 0); 12462fdbea25SAleksandr Guzovskiy VERIFY(refcount_add(&hdr->b_refcnt, tag) == 1); 12472fdbea25SAleksandr Guzovskiy 12482fdbea25SAleksandr Guzovskiy atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 12492fdbea25SAleksandr Guzovskiy } 12502fdbea25SAleksandr Guzovskiy 125144eda4d7Smaybee static arc_buf_t * 125244eda4d7Smaybee arc_buf_clone(arc_buf_t *from) 1253ea8dc4b6Seschrock { 125444eda4d7Smaybee arc_buf_t *buf; 125544eda4d7Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 125644eda4d7Smaybee uint64_t size = hdr->b_size; 1257ea8dc4b6Seschrock 12581ab7f2deSmaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 125944eda4d7Smaybee buf->b_hdr = hdr; 126044eda4d7Smaybee buf->b_data = NULL; 126144eda4d7Smaybee buf->b_efunc = NULL; 126244eda4d7Smaybee buf->b_private = NULL; 126344eda4d7Smaybee buf->b_next = hdr->b_buf; 126444eda4d7Smaybee hdr->b_buf = buf; 126544eda4d7Smaybee arc_get_data_buf(buf); 126644eda4d7Smaybee bcopy(from->b_data, buf->b_data, size); 126744eda4d7Smaybee hdr->b_datacnt += 1; 126844eda4d7Smaybee return (buf); 1269ea8dc4b6Seschrock } 1270ea8dc4b6Seschrock 1271ea8dc4b6Seschrock void 1272ea8dc4b6Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 1273ea8dc4b6Seschrock { 127440d7d650Smaybee arc_buf_hdr_t *hdr; 1275ea8dc4b6Seschrock kmutex_t *hash_lock; 1276ea8dc4b6Seschrock 12779b23f181Smaybee /* 12786f83844dSMark Maybee * Check to see if this buffer is evicted. Callers 12796f83844dSMark Maybee * must verify b_data != NULL to know if the add_ref 12806f83844dSMark Maybee * was successful. 12819b23f181Smaybee */ 12826f83844dSMark Maybee rw_enter(&buf->b_lock, RW_READER); 12836f83844dSMark Maybee if (buf->b_data == NULL) { 12846f83844dSMark Maybee rw_exit(&buf->b_lock); 12859b23f181Smaybee return; 128640d7d650Smaybee } 12876f83844dSMark Maybee hdr = buf->b_hdr; 12886f83844dSMark Maybee ASSERT(hdr != NULL); 12899b23f181Smaybee hash_lock = HDR_LOCK(hdr); 12909b23f181Smaybee mutex_enter(hash_lock); 12916f83844dSMark Maybee rw_exit(&buf->b_lock); 1292ea8dc4b6Seschrock 129344cb6abcSbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 1294ea8dc4b6Seschrock add_reference(hdr, hash_lock, tag); 12955a98e54bSBrendan Gregg - Sun Microsystems DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 129644eda4d7Smaybee arc_access(hdr, hash_lock); 129744eda4d7Smaybee mutex_exit(hash_lock); 129844cb6abcSbmc ARCSTAT_BUMP(arcstat_hits); 129944cb6abcSbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 130044cb6abcSbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 130144cb6abcSbmc data, metadata, hits); 1302ea8dc4b6Seschrock } 1303ea8dc4b6Seschrock 1304fa94a07fSbrendan /* 1305fa94a07fSbrendan * Free the arc data buffer. If it is an l2arc write in progress, 1306fa94a07fSbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 1307fa94a07fSbrendan */ 1308fa94a07fSbrendan static void 1309fa94a07fSbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 1310fa94a07fSbrendan void *data, size_t size) 1311fa94a07fSbrendan { 1312fa94a07fSbrendan if (HDR_L2_WRITING(hdr)) { 1313fa94a07fSbrendan l2arc_data_free_t *df; 1314fa94a07fSbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 1315fa94a07fSbrendan df->l2df_data = data; 1316fa94a07fSbrendan df->l2df_size = size; 1317fa94a07fSbrendan df->l2df_func = free_func; 1318fa94a07fSbrendan mutex_enter(&l2arc_free_on_write_mtx); 1319fa94a07fSbrendan list_insert_head(l2arc_free_on_write, df); 1320fa94a07fSbrendan mutex_exit(&l2arc_free_on_write_mtx); 1321fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 1322fa94a07fSbrendan } else { 1323fa94a07fSbrendan free_func(data, size); 1324fa94a07fSbrendan } 1325fa94a07fSbrendan } 1326fa94a07fSbrendan 1327ea8dc4b6Seschrock static void 132844eda4d7Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 1329ea8dc4b6Seschrock { 1330ea8dc4b6Seschrock arc_buf_t **bufp; 1331ea8dc4b6Seschrock 1332ea8dc4b6Seschrock /* free up data associated with the buf */ 1333ea8dc4b6Seschrock if (buf->b_data) { 1334ea8dc4b6Seschrock arc_state_t *state = buf->b_hdr->b_state; 1335ea8dc4b6Seschrock uint64_t size = buf->b_hdr->b_size; 1336ad23a2dbSjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 1337ea8dc4b6Seschrock 13386b4acc8bSahrens arc_cksum_verify(buf); 133944eda4d7Smaybee if (!recycle) { 1340ad23a2dbSjohansen if (type == ARC_BUFC_METADATA) { 1341fa94a07fSbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 1342fa94a07fSbrendan buf->b_data, size); 13435a98e54bSBrendan Gregg - Sun Microsystems arc_space_return(size, ARC_SPACE_DATA); 1344ad23a2dbSjohansen } else { 1345ad23a2dbSjohansen ASSERT(type == ARC_BUFC_DATA); 1346fa94a07fSbrendan arc_buf_data_free(buf->b_hdr, 1347fa94a07fSbrendan zio_data_buf_free, buf->b_data, size); 13485a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_data_size, -size); 13490e8c6158Smaybee atomic_add_64(&arc_size, -size); 1350ad23a2dbSjohansen } 135144eda4d7Smaybee } 1352ea8dc4b6Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 13530e8c6158Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 13540e8c6158Smaybee 1355ea8dc4b6Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 135644cb6abcSbmc ASSERT(state != arc_anon); 13570e8c6158Smaybee 13580e8c6158Smaybee ASSERT3U(*cnt, >=, size); 13590e8c6158Smaybee atomic_add_64(cnt, -size); 1360ea8dc4b6Seschrock } 136144cb6abcSbmc ASSERT3U(state->arcs_size, >=, size); 136244cb6abcSbmc atomic_add_64(&state->arcs_size, -size); 1363ea8dc4b6Seschrock buf->b_data = NULL; 1364ea8dc4b6Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 1365ea8dc4b6Seschrock buf->b_hdr->b_datacnt -= 1; 1366ea8dc4b6Seschrock } 1367ea8dc4b6Seschrock 1368ea8dc4b6Seschrock /* only remove the buf if requested */ 1369ea8dc4b6Seschrock if (!all) 1370ea8dc4b6Seschrock return; 1371ea8dc4b6Seschrock 1372ea8dc4b6Seschrock /* remove the buf from the hdr list */ 1373ea8dc4b6Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 1374ea8dc4b6Seschrock continue; 1375ea8dc4b6Seschrock *bufp = buf->b_next; 1376ea8dc4b6Seschrock 1377ea8dc4b6Seschrock ASSERT(buf->b_efunc == NULL); 1378ea8dc4b6Seschrock 1379ea8dc4b6Seschrock /* clean up the buf */ 1380ea8dc4b6Seschrock buf->b_hdr = NULL; 1381ea8dc4b6Seschrock kmem_cache_free(buf_cache, buf); 1382ea8dc4b6Seschrock } 1383ea8dc4b6Seschrock 1384fa9e4066Sahrens static void 1385ea8dc4b6Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1386fa9e4066Sahrens { 1387fa9e4066Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 138844cb6abcSbmc ASSERT3P(hdr->b_state, ==, arc_anon); 1389ea8dc4b6Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1390088f3894Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 1391fa9e4066Sahrens 1392fa94a07fSbrendan if (hdr->b_l2hdr != NULL) { 1393fa94a07fSbrendan if (!MUTEX_HELD(&l2arc_buflist_mtx)) { 1394fa94a07fSbrendan /* 1395fa94a07fSbrendan * To prevent arc_free() and l2arc_evict() from 1396fa94a07fSbrendan * attempting to free the same buffer at the same time, 1397fa94a07fSbrendan * a FREE_IN_PROGRESS flag is given to arc_free() to 1398fa94a07fSbrendan * give it priority. l2arc_evict() can't destroy this 1399fa94a07fSbrendan * header while we are waiting on l2arc_buflist_mtx. 140049cf58c0SBrendan Gregg - Sun Microsystems * 140149cf58c0SBrendan Gregg - Sun Microsystems * The hdr may be removed from l2ad_buflist before we 140249cf58c0SBrendan Gregg - Sun Microsystems * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 1403fa94a07fSbrendan */ 1404fa94a07fSbrendan mutex_enter(&l2arc_buflist_mtx); 140549cf58c0SBrendan Gregg - Sun Microsystems if (hdr->b_l2hdr != NULL) { 140649cf58c0SBrendan Gregg - Sun Microsystems list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, 140749cf58c0SBrendan Gregg - Sun Microsystems hdr); 140849cf58c0SBrendan Gregg - Sun Microsystems } 1409fa94a07fSbrendan mutex_exit(&l2arc_buflist_mtx); 1410fa94a07fSbrendan } else { 1411fa94a07fSbrendan list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr); 1412fa94a07fSbrendan } 1413fa94a07fSbrendan ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 1414fa94a07fSbrendan kmem_free(hdr->b_l2hdr, sizeof (l2arc_buf_hdr_t)); 1415fa94a07fSbrendan if (hdr->b_state == arc_l2c_only) 1416fa94a07fSbrendan l2arc_hdr_stat_remove(); 1417fa94a07fSbrendan hdr->b_l2hdr = NULL; 1418fa94a07fSbrendan } 1419fa94a07fSbrendan 1420fa9e4066Sahrens if (!BUF_EMPTY(hdr)) { 1421ea8dc4b6Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1422fa9e4066Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1423fa9e4066Sahrens hdr->b_birth = 0; 1424fa9e4066Sahrens hdr->b_cksum0 = 0; 1425fa9e4066Sahrens } 1426ea8dc4b6Seschrock while (hdr->b_buf) { 1427fa9e4066Sahrens arc_buf_t *buf = hdr->b_buf; 1428fa9e4066Sahrens 1429ea8dc4b6Seschrock if (buf->b_efunc) { 1430ea8dc4b6Seschrock mutex_enter(&arc_eviction_mtx); 14316f83844dSMark Maybee rw_enter(&buf->b_lock, RW_WRITER); 1432ea8dc4b6Seschrock ASSERT(buf->b_hdr != NULL); 143344eda4d7Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 1434ea8dc4b6Seschrock hdr->b_buf = buf->b_next; 143540d7d650Smaybee buf->b_hdr = &arc_eviction_hdr; 1436ea8dc4b6Seschrock buf->b_next = arc_eviction_list; 1437ea8dc4b6Seschrock arc_eviction_list = buf; 14386f83844dSMark Maybee rw_exit(&buf->b_lock); 1439ea8dc4b6Seschrock mutex_exit(&arc_eviction_mtx); 1440ea8dc4b6Seschrock } else { 144144eda4d7Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 1442ea8dc4b6Seschrock } 1443fa9e4066Sahrens } 14446b4acc8bSahrens if (hdr->b_freeze_cksum != NULL) { 14456b4acc8bSahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 14466b4acc8bSahrens hdr->b_freeze_cksum = NULL; 14476b4acc8bSahrens } 1448ea8dc4b6Seschrock 1449fa9e4066Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1450fa9e4066Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1451fa9e4066Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1452fa9e4066Sahrens kmem_cache_free(hdr_cache, hdr); 1453fa9e4066Sahrens } 1454fa9e4066Sahrens 1455fa9e4066Sahrens void 1456fa9e4066Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1457fa9e4066Sahrens { 1458fa9e4066Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 145944cb6abcSbmc int hashed = hdr->b_state != arc_anon; 1460fa9e4066Sahrens 1461ea8dc4b6Seschrock ASSERT(buf->b_efunc == NULL); 1462ea8dc4b6Seschrock ASSERT(buf->b_data != NULL); 1463ea8dc4b6Seschrock 1464ea8dc4b6Seschrock if (hashed) { 1465ea8dc4b6Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 1466ea8dc4b6Seschrock 1467ea8dc4b6Seschrock mutex_enter(hash_lock); 1468ea8dc4b6Seschrock (void) remove_reference(hdr, hash_lock, tag); 1469ea8dc4b6Seschrock if (hdr->b_datacnt > 1) 147044eda4d7Smaybee arc_buf_destroy(buf, FALSE, TRUE); 1471ea8dc4b6Seschrock else 1472ea8dc4b6Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1473fa9e4066Sahrens mutex_exit(hash_lock); 1474ea8dc4b6Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 1475ea8dc4b6Seschrock int destroy_hdr; 1476ea8dc4b6Seschrock /* 1477ea8dc4b6Seschrock * We are in the middle of an async write. Don't destroy 1478ea8dc4b6Seschrock * this buffer unless the write completes before we finish 1479ea8dc4b6Seschrock * decrementing the reference count. 1480ea8dc4b6Seschrock */ 1481ea8dc4b6Seschrock mutex_enter(&arc_eviction_mtx); 1482ea8dc4b6Seschrock (void) remove_reference(hdr, NULL, tag); 1483ea8dc4b6Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1484ea8dc4b6Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 1485ea8dc4b6Seschrock mutex_exit(&arc_eviction_mtx); 1486ea8dc4b6Seschrock if (destroy_hdr) 1487ea8dc4b6Seschrock arc_hdr_destroy(hdr); 1488ea8dc4b6Seschrock } else { 1489ea8dc4b6Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 1490ea8dc4b6Seschrock ASSERT(HDR_IO_ERROR(hdr)); 149144eda4d7Smaybee arc_buf_destroy(buf, FALSE, TRUE); 1492ea8dc4b6Seschrock } else { 1493ea8dc4b6Seschrock arc_hdr_destroy(hdr); 1494ea8dc4b6Seschrock } 1495fa9e4066Sahrens } 1496ea8dc4b6Seschrock } 1497fa9e4066Sahrens 1498ea8dc4b6Seschrock int 1499ea8dc4b6Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 1500ea8dc4b6Seschrock { 1501ea8dc4b6Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1502ea8dc4b6Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 1503ea8dc4b6Seschrock int no_callback = (buf->b_efunc == NULL); 1504fa9e4066Sahrens 150544cb6abcSbmc if (hdr->b_state == arc_anon) { 1506ea8dc4b6Seschrock arc_buf_free(buf, tag); 1507ea8dc4b6Seschrock return (no_callback); 1508ea8dc4b6Seschrock } 1509ea8dc4b6Seschrock 1510ea8dc4b6Seschrock mutex_enter(hash_lock); 151144cb6abcSbmc ASSERT(hdr->b_state != arc_anon); 1512ea8dc4b6Seschrock ASSERT(buf->b_data != NULL); 1513ea8dc4b6Seschrock 1514ea8dc4b6Seschrock (void) remove_reference(hdr, hash_lock, tag); 1515ea8dc4b6Seschrock if (hdr->b_datacnt > 1) { 1516ea8dc4b6Seschrock if (no_callback) 151744eda4d7Smaybee arc_buf_destroy(buf, FALSE, TRUE); 1518ea8dc4b6Seschrock } else if (no_callback) { 1519ea8dc4b6Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 1520ea8dc4b6Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1521ea8dc4b6Seschrock } 1522ea8dc4b6Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 1523ea8dc4b6Seschrock refcount_is_zero(&hdr->b_refcnt)); 1524ea8dc4b6Seschrock mutex_exit(hash_lock); 1525ea8dc4b6Seschrock return (no_callback); 1526fa9e4066Sahrens } 1527fa9e4066Sahrens 1528fa9e4066Sahrens int 1529fa9e4066Sahrens arc_buf_size(arc_buf_t *buf) 1530fa9e4066Sahrens { 1531fa9e4066Sahrens return (buf->b_hdr->b_size); 1532fa9e4066Sahrens } 1533fa9e4066Sahrens 1534fa9e4066Sahrens /* 1535fa9e4066Sahrens * Evict buffers from list until we've removed the specified number of 1536fa9e4066Sahrens * bytes. Move the removed buffers to the appropriate evict state. 153744eda4d7Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 153844eda4d7Smaybee * - look for a buffer to evict that is `bytes' long. 153944eda4d7Smaybee * - return the data block from this buffer rather than freeing it. 154044eda4d7Smaybee * This flag is used by callers that are trying to make space for a 154144eda4d7Smaybee * new buffer in a full arc cache. 1542874395d5Smaybee * 1543874395d5Smaybee * This function makes a "best effort". It skips over any buffers 1544874395d5Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 1545874395d5Smaybee * It may also return without evicting as much space as requested. 1546fa9e4066Sahrens */ 154744eda4d7Smaybee static void * 1548ac05c741SMark Maybee arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 1549ad23a2dbSjohansen arc_buf_contents_t type) 1550fa9e4066Sahrens { 1551fa9e4066Sahrens arc_state_t *evicted_state; 155244eda4d7Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 15533fa51506Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 15540e8c6158Smaybee list_t *list = &state->arcs_list[type]; 1555fa9e4066Sahrens kmutex_t *hash_lock; 155644eda4d7Smaybee boolean_t have_lock; 15573fa51506Smaybee void *stolen = NULL; 1558fa9e4066Sahrens 155944cb6abcSbmc ASSERT(state == arc_mru || state == arc_mfu); 1560fa9e4066Sahrens 156144cb6abcSbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1562fa9e4066Sahrens 156344cb6abcSbmc mutex_enter(&state->arcs_mtx); 156444cb6abcSbmc mutex_enter(&evicted_state->arcs_mtx); 1565fa9e4066Sahrens 15660e8c6158Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15670e8c6158Smaybee ab_prev = list_prev(list, ab); 156813506d1eSmaybee /* prefetch buffers have a minimum lifespan */ 156944eda4d7Smaybee if (HDR_IO_IN_PROGRESS(ab) || 1570874395d5Smaybee (spa && ab->b_spa != spa) || 157144eda4d7Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 157244eda4d7Smaybee lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) { 157313506d1eSmaybee skipped++; 157413506d1eSmaybee continue; 157513506d1eSmaybee } 15763fa51506Smaybee /* "lookahead" for better eviction candidate */ 15773fa51506Smaybee if (recycle && ab->b_size != bytes && 15783fa51506Smaybee ab_prev && ab_prev->b_size == bytes) 157944eda4d7Smaybee continue; 1580fa9e4066Sahrens hash_lock = HDR_LOCK(ab); 158144eda4d7Smaybee have_lock = MUTEX_HELD(hash_lock); 158244eda4d7Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1583fa9e4066Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 1584ea8dc4b6Seschrock ASSERT(ab->b_datacnt > 0); 1585ea8dc4b6Seschrock while (ab->b_buf) { 1586ea8dc4b6Seschrock arc_buf_t *buf = ab->b_buf; 15876f83844dSMark Maybee if (!rw_tryenter(&buf->b_lock, RW_WRITER)) { 15886f83844dSMark Maybee missed += 1; 15896f83844dSMark Maybee break; 15906f83844dSMark Maybee } 159144eda4d7Smaybee if (buf->b_data) { 1592ea8dc4b6Seschrock bytes_evicted += ab->b_size; 1593ad23a2dbSjohansen if (recycle && ab->b_type == type && 1594fa94a07fSbrendan ab->b_size == bytes && 1595fa94a07fSbrendan !HDR_L2_WRITING(ab)) { 15963fa51506Smaybee stolen = buf->b_data; 15973fa51506Smaybee recycle = FALSE; 15983fa51506Smaybee } 159944eda4d7Smaybee } 1600ea8dc4b6Seschrock if (buf->b_efunc) { 1601ea8dc4b6Seschrock mutex_enter(&arc_eviction_mtx); 16023fa51506Smaybee arc_buf_destroy(buf, 16033fa51506Smaybee buf->b_data == stolen, FALSE); 1604ea8dc4b6Seschrock ab->b_buf = buf->b_next; 160540d7d650Smaybee buf->b_hdr = &arc_eviction_hdr; 1606ea8dc4b6Seschrock buf->b_next = arc_eviction_list; 1607ea8dc4b6Seschrock arc_eviction_list = buf; 1608ea8dc4b6Seschrock mutex_exit(&arc_eviction_mtx); 16096f83844dSMark Maybee rw_exit(&buf->b_lock); 1610ea8dc4b6Seschrock } else { 16116f83844dSMark Maybee rw_exit(&buf->b_lock); 16123fa51506Smaybee arc_buf_destroy(buf, 16133fa51506Smaybee buf->b_data == stolen, TRUE); 1614ea8dc4b6Seschrock } 1615ea8dc4b6Seschrock } 1616*5ea40c06SBrendan Gregg - Sun Microsystems 1617*5ea40c06SBrendan Gregg - Sun Microsystems if (ab->b_l2hdr) { 1618*5ea40c06SBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_evict_l2_cached, 1619*5ea40c06SBrendan Gregg - Sun Microsystems ab->b_size); 1620*5ea40c06SBrendan Gregg - Sun Microsystems } else { 1621*5ea40c06SBrendan Gregg - Sun Microsystems if (l2arc_write_eligible(ab->b_spa, ab)) { 1622*5ea40c06SBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_evict_l2_eligible, 1623*5ea40c06SBrendan Gregg - Sun Microsystems ab->b_size); 1624*5ea40c06SBrendan Gregg - Sun Microsystems } else { 1625*5ea40c06SBrendan Gregg - Sun Microsystems ARCSTAT_INCR( 1626*5ea40c06SBrendan Gregg - Sun Microsystems arcstat_evict_l2_ineligible, 1627*5ea40c06SBrendan Gregg - Sun Microsystems ab->b_size); 1628*5ea40c06SBrendan Gregg - Sun Microsystems } 1629*5ea40c06SBrendan Gregg - Sun Microsystems } 1630*5ea40c06SBrendan Gregg - Sun Microsystems 16316f83844dSMark Maybee if (ab->b_datacnt == 0) { 16326f83844dSMark Maybee arc_change_state(evicted_state, ab, hash_lock); 16336f83844dSMark Maybee ASSERT(HDR_IN_HASH_TABLE(ab)); 16346f83844dSMark Maybee ab->b_flags |= ARC_IN_HASH_TABLE; 16356f83844dSMark Maybee ab->b_flags &= ~ARC_BUF_AVAILABLE; 16366f83844dSMark Maybee DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 16376f83844dSMark Maybee } 163844eda4d7Smaybee if (!have_lock) 163944eda4d7Smaybee mutex_exit(hash_lock); 1640ea8dc4b6Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1641fa9e4066Sahrens break; 1642fa9e4066Sahrens } else { 164344eda4d7Smaybee missed += 1; 1644fa9e4066Sahrens } 1645fa9e4066Sahrens } 164644cb6abcSbmc 164744cb6abcSbmc mutex_exit(&evicted_state->arcs_mtx); 164844cb6abcSbmc mutex_exit(&state->arcs_mtx); 1649fa9e4066Sahrens 1650fa9e4066Sahrens if (bytes_evicted < bytes) 1651fa9e4066Sahrens dprintf("only evicted %lld bytes from %x", 1652fa9e4066Sahrens (longlong_t)bytes_evicted, state); 1653fa9e4066Sahrens 165444eda4d7Smaybee if (skipped) 165544cb6abcSbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 165644cb6abcSbmc 165744eda4d7Smaybee if (missed) 165844cb6abcSbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 1659f4d2e9e6Smaybee 1660f4d2e9e6Smaybee /* 1661f4d2e9e6Smaybee * We have just evicted some date into the ghost state, make 1662f4d2e9e6Smaybee * sure we also adjust the ghost state size if necessary. 1663f4d2e9e6Smaybee */ 1664f4d2e9e6Smaybee if (arc_no_grow && 1665f4d2e9e6Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 1666f4d2e9e6Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 1667f4d2e9e6Smaybee arc_mru_ghost->arcs_size - arc_c; 1668f4d2e9e6Smaybee 1669f4d2e9e6Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 1670f4d2e9e6Smaybee int64_t todelete = 1671f4d2e9e6Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 1672874395d5Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 1673f4d2e9e6Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 1674f4d2e9e6Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 1675f4d2e9e6Smaybee arc_mru_ghost->arcs_size + 1676f4d2e9e6Smaybee arc_mfu_ghost->arcs_size - arc_c); 1677874395d5Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 1678f4d2e9e6Smaybee } 1679f4d2e9e6Smaybee } 168044cb6abcSbmc 16813fa51506Smaybee return (stolen); 1682fa9e4066Sahrens } 1683fa9e4066Sahrens 1684fa9e4066Sahrens /* 1685fa9e4066Sahrens * Remove buffers from list until we've removed the specified number of 1686fa9e4066Sahrens * bytes. Destroy the buffers that are removed. 1687fa9e4066Sahrens */ 1688fa9e4066Sahrens static void 1689ac05c741SMark Maybee arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 1690fa9e4066Sahrens { 1691fa9e4066Sahrens arc_buf_hdr_t *ab, *ab_prev; 16920e8c6158Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1693fa9e4066Sahrens kmutex_t *hash_lock; 1694ea8dc4b6Seschrock uint64_t bytes_deleted = 0; 1695c0a81264Sek uint64_t bufs_skipped = 0; 1696fa9e4066Sahrens 1697ea8dc4b6Seschrock ASSERT(GHOST_STATE(state)); 1698fa9e4066Sahrens top: 169944cb6abcSbmc mutex_enter(&state->arcs_mtx); 17000e8c6158Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 17010e8c6158Smaybee ab_prev = list_prev(list, ab); 1702874395d5Smaybee if (spa && ab->b_spa != spa) 1703874395d5Smaybee continue; 1704fa9e4066Sahrens hash_lock = HDR_LOCK(ab); 1705fa9e4066Sahrens if (mutex_tryenter(hash_lock)) { 170613506d1eSmaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 1707ea8dc4b6Seschrock ASSERT(ab->b_buf == NULL); 170844cb6abcSbmc ARCSTAT_BUMP(arcstat_deleted); 1709fa9e4066Sahrens bytes_deleted += ab->b_size; 1710fa94a07fSbrendan 1711fa94a07fSbrendan if (ab->b_l2hdr != NULL) { 1712fa94a07fSbrendan /* 1713fa94a07fSbrendan * This buffer is cached on the 2nd Level ARC; 1714fa94a07fSbrendan * don't destroy the header. 1715fa94a07fSbrendan */ 1716fa94a07fSbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 1717fa94a07fSbrendan mutex_exit(hash_lock); 1718fa94a07fSbrendan } else { 1719fa94a07fSbrendan arc_change_state(arc_anon, ab, hash_lock); 1720fa94a07fSbrendan mutex_exit(hash_lock); 1721fa94a07fSbrendan arc_hdr_destroy(ab); 1722fa94a07fSbrendan } 1723fa94a07fSbrendan 1724ea8dc4b6Seschrock DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1725fa9e4066Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1726fa9e4066Sahrens break; 1727fa9e4066Sahrens } else { 1728fa9e4066Sahrens if (bytes < 0) { 172944cb6abcSbmc mutex_exit(&state->arcs_mtx); 1730fa9e4066Sahrens mutex_enter(hash_lock); 1731fa9e4066Sahrens mutex_exit(hash_lock); 1732fa9e4066Sahrens goto top; 1733fa9e4066Sahrens } 1734fa9e4066Sahrens bufs_skipped += 1; 1735fa9e4066Sahrens } 1736fa9e4066Sahrens } 173744cb6abcSbmc mutex_exit(&state->arcs_mtx); 1738fa9e4066Sahrens 17390e8c6158Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 17400e8c6158Smaybee (bytes < 0 || bytes_deleted < bytes)) { 17410e8c6158Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 17420e8c6158Smaybee goto top; 17430e8c6158Smaybee } 17440e8c6158Smaybee 1745fa9e4066Sahrens if (bufs_skipped) { 174644cb6abcSbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1747fa9e4066Sahrens ASSERT(bytes >= 0); 1748fa9e4066Sahrens } 1749fa9e4066Sahrens 1750fa9e4066Sahrens if (bytes_deleted < bytes) 1751fa9e4066Sahrens dprintf("only deleted %lld bytes from %p", 1752fa9e4066Sahrens (longlong_t)bytes_deleted, state); 1753fa9e4066Sahrens } 1754fa9e4066Sahrens 1755fa9e4066Sahrens static void 1756fa9e4066Sahrens arc_adjust(void) 1757fa9e4066Sahrens { 17585a98e54bSBrendan Gregg - Sun Microsystems int64_t adjustment, delta; 1759fa9e4066Sahrens 17605a98e54bSBrendan Gregg - Sun Microsystems /* 17615a98e54bSBrendan Gregg - Sun Microsystems * Adjust MRU size 17625a98e54bSBrendan Gregg - Sun Microsystems */ 17635a98e54bSBrendan Gregg - Sun Microsystems 17645a98e54bSBrendan Gregg - Sun Microsystems adjustment = MIN(arc_size - arc_c, 17655a98e54bSBrendan Gregg - Sun Microsystems arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - arc_p); 1766fa9e4066Sahrens 17675a98e54bSBrendan Gregg - Sun Microsystems if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 17685a98e54bSBrendan Gregg - Sun Microsystems delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 17695a98e54bSBrendan Gregg - Sun Microsystems (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA); 17705a98e54bSBrendan Gregg - Sun Microsystems adjustment -= delta; 17710e8c6158Smaybee } 17720e8c6158Smaybee 17735a98e54bSBrendan Gregg - Sun Microsystems if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 17745a98e54bSBrendan Gregg - Sun Microsystems delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 17755a98e54bSBrendan Gregg - Sun Microsystems (void) arc_evict(arc_mru, NULL, delta, FALSE, 1776874395d5Smaybee ARC_BUFC_METADATA); 1777fa9e4066Sahrens } 1778fa9e4066Sahrens 17795a98e54bSBrendan Gregg - Sun Microsystems /* 17805a98e54bSBrendan Gregg - Sun Microsystems * Adjust MFU size 17815a98e54bSBrendan Gregg - Sun Microsystems */ 1782fa9e4066Sahrens 17835a98e54bSBrendan Gregg - Sun Microsystems adjustment = arc_size - arc_c; 17845a98e54bSBrendan Gregg - Sun Microsystems 17855a98e54bSBrendan Gregg - Sun Microsystems if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 17865a98e54bSBrendan Gregg - Sun Microsystems delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 17875a98e54bSBrendan Gregg - Sun Microsystems (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA); 17885a98e54bSBrendan Gregg - Sun Microsystems adjustment -= delta; 1789fa9e4066Sahrens } 1790fa9e4066Sahrens 17915a98e54bSBrendan Gregg - Sun Microsystems if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 17925a98e54bSBrendan Gregg - Sun Microsystems int64_t delta = MIN(adjustment, 17935a98e54bSBrendan Gregg - Sun Microsystems arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 17945a98e54bSBrendan Gregg - Sun Microsystems (void) arc_evict(arc_mfu, NULL, delta, FALSE, 17955a98e54bSBrendan Gregg - Sun Microsystems ARC_BUFC_METADATA); 17965a98e54bSBrendan Gregg - Sun Microsystems } 1797fa9e4066Sahrens 17985a98e54bSBrendan Gregg - Sun Microsystems /* 17995a98e54bSBrendan Gregg - Sun Microsystems * Adjust ghost lists 18005a98e54bSBrendan Gregg - Sun Microsystems */ 1801fa9e4066Sahrens 18025a98e54bSBrendan Gregg - Sun Microsystems adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 1803fa9e4066Sahrens 18045a98e54bSBrendan Gregg - Sun Microsystems if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 18055a98e54bSBrendan Gregg - Sun Microsystems delta = MIN(arc_mru_ghost->arcs_size, adjustment); 18065a98e54bSBrendan Gregg - Sun Microsystems arc_evict_ghost(arc_mru_ghost, NULL, delta); 18075a98e54bSBrendan Gregg - Sun Microsystems } 18080e8c6158Smaybee 18095a98e54bSBrendan Gregg - Sun Microsystems adjustment = 18105a98e54bSBrendan Gregg - Sun Microsystems arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 18115a98e54bSBrendan Gregg - Sun Microsystems 18125a98e54bSBrendan Gregg - Sun Microsystems if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 18135a98e54bSBrendan Gregg - Sun Microsystems delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 18145a98e54bSBrendan Gregg - Sun Microsystems arc_evict_ghost(arc_mfu_ghost, NULL, delta); 1815fa9e4066Sahrens } 1816fa9e4066Sahrens } 1817fa9e4066Sahrens 1818ea8dc4b6Seschrock static void 1819ea8dc4b6Seschrock arc_do_user_evicts(void) 1820ea8dc4b6Seschrock { 1821ea8dc4b6Seschrock mutex_enter(&arc_eviction_mtx); 1822ea8dc4b6Seschrock while (arc_eviction_list != NULL) { 1823ea8dc4b6Seschrock arc_buf_t *buf = arc_eviction_list; 1824ea8dc4b6Seschrock arc_eviction_list = buf->b_next; 18256f83844dSMark Maybee rw_enter(&buf->b_lock, RW_WRITER); 1826ea8dc4b6Seschrock buf->b_hdr = NULL; 18276f83844dSMark Maybee rw_exit(&buf->b_lock); 1828ea8dc4b6Seschrock mutex_exit(&arc_eviction_mtx); 1829ea8dc4b6Seschrock 1830dd6ef538Smaybee if (buf->b_efunc != NULL) 1831dd6ef538Smaybee VERIFY(buf->b_efunc(buf) == 0); 1832ea8dc4b6Seschrock 1833ea8dc4b6Seschrock buf->b_efunc = NULL; 1834ea8dc4b6Seschrock buf->b_private = NULL; 1835ea8dc4b6Seschrock kmem_cache_free(buf_cache, buf); 1836ea8dc4b6Seschrock mutex_enter(&arc_eviction_mtx); 1837ea8dc4b6Seschrock } 1838ea8dc4b6Seschrock mutex_exit(&arc_eviction_mtx); 1839ea8dc4b6Seschrock } 1840ea8dc4b6Seschrock 1841fa9e4066Sahrens /* 1842874395d5Smaybee * Flush all *evictable* data from the cache for the given spa. 1843fa9e4066Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1844fa9e4066Sahrens */ 1845fa9e4066Sahrens void 1846874395d5Smaybee arc_flush(spa_t *spa) 1847fa9e4066Sahrens { 1848ac05c741SMark Maybee uint64_t guid = 0; 1849ac05c741SMark Maybee 1850ac05c741SMark Maybee if (spa) 1851ac05c741SMark Maybee guid = spa_guid(spa); 1852ac05c741SMark Maybee 1853874395d5Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 1854ac05c741SMark Maybee (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 1855874395d5Smaybee if (spa) 1856874395d5Smaybee break; 1857874395d5Smaybee } 1858874395d5Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 1859ac05c741SMark Maybee (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 1860874395d5Smaybee if (spa) 1861874395d5Smaybee break; 1862874395d5Smaybee } 1863874395d5Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 1864ac05c741SMark Maybee (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 1865874395d5Smaybee if (spa) 1866874395d5Smaybee break; 1867874395d5Smaybee } 1868874395d5Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 1869ac05c741SMark Maybee (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 1870874395d5Smaybee if (spa) 1871874395d5Smaybee break; 1872874395d5Smaybee } 1873874395d5Smaybee 1874ac05c741SMark Maybee arc_evict_ghost(arc_mru_ghost, guid, -1); 1875ac05c741SMark Maybee arc_evict_ghost(arc_mfu_ghost, guid, -1); 1876ea8dc4b6Seschrock 1877ea8dc4b6Seschrock mutex_enter(&arc_reclaim_thr_lock); 1878ea8dc4b6Seschrock arc_do_user_evicts(); 1879ea8dc4b6Seschrock mutex_exit(&arc_reclaim_thr_lock); 1880874395d5Smaybee ASSERT(spa || arc_eviction_list == NULL); 1881fa9e4066Sahrens } 1882fa9e4066Sahrens 1883fa9e4066Sahrens void 188449e3519aSmaybee arc_shrink(void) 1885fa9e4066Sahrens { 188644cb6abcSbmc if (arc_c > arc_c_min) { 188749e3519aSmaybee uint64_t to_free; 1888fa9e4066Sahrens 18893cff2f43Sstans #ifdef _KERNEL 189044cb6abcSbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 18913cff2f43Sstans #else 189244cb6abcSbmc to_free = arc_c >> arc_shrink_shift; 18933cff2f43Sstans #endif 189444cb6abcSbmc if (arc_c > arc_c_min + to_free) 189544cb6abcSbmc atomic_add_64(&arc_c, -to_free); 189649e3519aSmaybee else 189744cb6abcSbmc arc_c = arc_c_min; 189844cb6abcSbmc 189944cb6abcSbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 190044cb6abcSbmc if (arc_c > arc_size) 190144cb6abcSbmc arc_c = MAX(arc_size, arc_c_min); 190244cb6abcSbmc if (arc_p > arc_c) 190344cb6abcSbmc arc_p = (arc_c >> 1); 190444cb6abcSbmc ASSERT(arc_c >= arc_c_min); 190544cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 190649e3519aSmaybee } 1907fa9e4066Sahrens 190844cb6abcSbmc if (arc_size > arc_c) 190949e3519aSmaybee arc_adjust(); 1910fa9e4066Sahrens } 1911fa9e4066Sahrens 1912fa9e4066Sahrens static int 1913fa9e4066Sahrens arc_reclaim_needed(void) 1914fa9e4066Sahrens { 1915fa9e4066Sahrens uint64_t extra; 1916fa9e4066Sahrens 1917fa9e4066Sahrens #ifdef _KERNEL 19183cff2f43Sstans 19193cff2f43Sstans if (needfree) 19203cff2f43Sstans return (1); 19213cff2f43Sstans 1922fa9e4066Sahrens /* 1923fa9e4066Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1924fa9e4066Sahrens */ 1925fa9e4066Sahrens extra = desfree; 1926fa9e4066Sahrens 1927fa9e4066Sahrens /* 1928fa9e4066Sahrens * check that we're out of range of the pageout scanner. It starts to 1929fa9e4066Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1930fa9e4066Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1931fa9e4066Sahrens * number of needed free pages. We add extra pages here to make sure 1932fa9e4066Sahrens * the scanner doesn't start up while we're freeing memory. 1933fa9e4066Sahrens */ 1934fa9e4066Sahrens if (freemem < lotsfree + needfree + extra) 1935fa9e4066Sahrens return (1); 1936fa9e4066Sahrens 1937fa9e4066Sahrens /* 1938fa9e4066Sahrens * check to make sure that swapfs has enough space so that anon 1939fa94a07fSbrendan * reservations can still succeed. anon_resvmem() checks that the 1940fa9e4066Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1941fa9e4066Sahrens * swap pages. We also add a bit of extra here just to prevent 1942fa9e4066Sahrens * circumstances from getting really dire. 1943fa9e4066Sahrens */ 1944fa9e4066Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1945fa9e4066Sahrens return (1); 1946fa9e4066Sahrens 19475dc8af33Smaybee #if defined(__i386) 1948fa9e4066Sahrens /* 1949fa9e4066Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1950fa9e4066Sahrens * kernel heap space before we ever run out of available physical 1951fa9e4066Sahrens * memory. Most checks of the size of the heap_area compare against 1952fa9e4066Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1953fa9e4066Sahrens * can have in the system. However, this is generally fixed at 25 pages 1954fa9e4066Sahrens * which is so low that it's useless. In this comparison, we seek to 1955fa9e4066Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 1956fa94a07fSbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1957fa9e4066Sahrens * free) 1958fa9e4066Sahrens */ 1959fa9e4066Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1960fa9e4066Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1961fa9e4066Sahrens return (1); 1962fa9e4066Sahrens #endif 1963fa9e4066Sahrens 1964fa9e4066Sahrens #else 1965fa9e4066Sahrens if (spa_get_random(100) == 0) 1966fa9e4066Sahrens return (1); 1967fa9e4066Sahrens #endif 1968fa9e4066Sahrens return (0); 1969fa9e4066Sahrens } 1970fa9e4066Sahrens 1971fa9e4066Sahrens static void 1972fa9e4066Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1973fa9e4066Sahrens { 1974fa9e4066Sahrens size_t i; 1975fa9e4066Sahrens kmem_cache_t *prev_cache = NULL; 1976ad23a2dbSjohansen kmem_cache_t *prev_data_cache = NULL; 1977fa9e4066Sahrens extern kmem_cache_t *zio_buf_cache[]; 1978ad23a2dbSjohansen extern kmem_cache_t *zio_data_buf_cache[]; 1979fa9e4066Sahrens 1980033f9833Sek #ifdef _KERNEL 19810e8c6158Smaybee if (arc_meta_used >= arc_meta_limit) { 19820e8c6158Smaybee /* 19830e8c6158Smaybee * We are exceeding our meta-data cache limit. 19840e8c6158Smaybee * Purge some DNLC entries to release holds on meta-data. 19850e8c6158Smaybee */ 19860e8c6158Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 19870e8c6158Smaybee } 19885dc8af33Smaybee #if defined(__i386) 19895dc8af33Smaybee /* 19905dc8af33Smaybee * Reclaim unused memory from all kmem caches. 19915dc8af33Smaybee */ 19925dc8af33Smaybee kmem_reap(); 19935dc8af33Smaybee #endif 1994033f9833Sek #endif 1995033f9833Sek 1996fa9e4066Sahrens /* 1997fa94a07fSbrendan * An aggressive reclamation will shrink the cache size as well as 1998ea8dc4b6Seschrock * reap free buffers from the arc kmem caches. 1999fa9e4066Sahrens */ 2000fa9e4066Sahrens if (strat == ARC_RECLAIM_AGGR) 200149e3519aSmaybee arc_shrink(); 2002fa9e4066Sahrens 2003fa9e4066Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2004fa9e4066Sahrens if (zio_buf_cache[i] != prev_cache) { 2005fa9e4066Sahrens prev_cache = zio_buf_cache[i]; 2006fa9e4066Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 2007fa9e4066Sahrens } 2008ad23a2dbSjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 2009ad23a2dbSjohansen prev_data_cache = zio_data_buf_cache[i]; 2010ad23a2dbSjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 2011ad23a2dbSjohansen } 2012fa9e4066Sahrens } 2013ea8dc4b6Seschrock kmem_cache_reap_now(buf_cache); 2014ea8dc4b6Seschrock kmem_cache_reap_now(hdr_cache); 2015fa9e4066Sahrens } 2016fa9e4066Sahrens 2017fa9e4066Sahrens static void 2018fa9e4066Sahrens arc_reclaim_thread(void) 2019fa9e4066Sahrens { 2020fa9e4066Sahrens clock_t growtime = 0; 2021fa9e4066Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2022fa9e4066Sahrens callb_cpr_t cpr; 2023fa9e4066Sahrens 2024fa9e4066Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2025fa9e4066Sahrens 2026fa9e4066Sahrens mutex_enter(&arc_reclaim_thr_lock); 2027fa9e4066Sahrens while (arc_thread_exit == 0) { 2028fa9e4066Sahrens if (arc_reclaim_needed()) { 2029fa9e4066Sahrens 203044cb6abcSbmc if (arc_no_grow) { 2031fa9e4066Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 2032fa9e4066Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2033fa9e4066Sahrens } else { 2034fa9e4066Sahrens last_reclaim = ARC_RECLAIM_CONS; 2035fa9e4066Sahrens } 2036fa9e4066Sahrens } else { 203744cb6abcSbmc arc_no_grow = TRUE; 2038fa9e4066Sahrens last_reclaim = ARC_RECLAIM_AGGR; 2039fa9e4066Sahrens membar_producer(); 2040fa9e4066Sahrens } 2041fa9e4066Sahrens 2042fa9e4066Sahrens /* reset the growth delay for every reclaim */ 2043fa9e4066Sahrens growtime = lbolt + (arc_grow_retry * hz); 2044fa9e4066Sahrens 2045fa9e4066Sahrens arc_kmem_reap_now(last_reclaim); 20463a737e0dSbrendan arc_warm = B_TRUE; 2047fa9e4066Sahrens 20480e8c6158Smaybee } else if (arc_no_grow && lbolt >= growtime) { 204944cb6abcSbmc arc_no_grow = FALSE; 2050fa9e4066Sahrens } 2051fa9e4066Sahrens 205244cb6abcSbmc if (2 * arc_c < arc_size + 205344cb6abcSbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 2054641fbdaeSmaybee arc_adjust(); 2055641fbdaeSmaybee 2056ea8dc4b6Seschrock if (arc_eviction_list != NULL) 2057ea8dc4b6Seschrock arc_do_user_evicts(); 2058ea8dc4b6Seschrock 2059fa9e4066Sahrens /* block until needed, or one second, whichever is shorter */ 2060fa9e4066Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 2061fa9e4066Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 2062fa9e4066Sahrens &arc_reclaim_thr_lock, (lbolt + hz)); 2063fa9e4066Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2064fa9e4066Sahrens } 2065fa9e4066Sahrens 2066fa9e4066Sahrens arc_thread_exit = 0; 2067fa9e4066Sahrens cv_broadcast(&arc_reclaim_thr_cv); 2068fa9e4066Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2069fa9e4066Sahrens thread_exit(); 2070fa9e4066Sahrens } 2071fa9e4066Sahrens 2072ea8dc4b6Seschrock /* 2073ea8dc4b6Seschrock * Adapt arc info given the number of bytes we are trying to add and 2074ea8dc4b6Seschrock * the state that we are comming from. This function is only called 2075ea8dc4b6Seschrock * when we are adding new content to the cache. 2076ea8dc4b6Seschrock */ 2077fa9e4066Sahrens static void 2078ea8dc4b6Seschrock arc_adapt(int bytes, arc_state_t *state) 2079fa9e4066Sahrens { 2080ea8dc4b6Seschrock int mult; 20815a98e54bSBrendan Gregg - Sun Microsystems uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 2082ea8dc4b6Seschrock 2083fa94a07fSbrendan if (state == arc_l2c_only) 2084fa94a07fSbrendan return; 2085fa94a07fSbrendan 2086ea8dc4b6Seschrock ASSERT(bytes > 0); 2087fa9e4066Sahrens /* 2088ea8dc4b6Seschrock * Adapt the target size of the MRU list: 2089ea8dc4b6Seschrock * - if we just hit in the MRU ghost list, then increase 2090ea8dc4b6Seschrock * the target size of the MRU list. 2091ea8dc4b6Seschrock * - if we just hit in the MFU ghost list, then increase 2092ea8dc4b6Seschrock * the target size of the MFU list by decreasing the 2093ea8dc4b6Seschrock * target size of the MRU list. 2094fa9e4066Sahrens */ 209544cb6abcSbmc if (state == arc_mru_ghost) { 209644cb6abcSbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 209744cb6abcSbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 2098ea8dc4b6Seschrock 20995a98e54bSBrendan Gregg - Sun Microsystems arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 210044cb6abcSbmc } else if (state == arc_mfu_ghost) { 21015a98e54bSBrendan Gregg - Sun Microsystems uint64_t delta; 21025a98e54bSBrendan Gregg - Sun Microsystems 210344cb6abcSbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 210444cb6abcSbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 2105ea8dc4b6Seschrock 21065a98e54bSBrendan Gregg - Sun Microsystems delta = MIN(bytes * mult, arc_p); 21075a98e54bSBrendan Gregg - Sun Microsystems arc_p = MAX(arc_p_min, arc_p - delta); 2108ea8dc4b6Seschrock } 210944cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 2110fa9e4066Sahrens 2111fa9e4066Sahrens if (arc_reclaim_needed()) { 2112fa9e4066Sahrens cv_signal(&arc_reclaim_thr_cv); 2113fa9e4066Sahrens return; 2114fa9e4066Sahrens } 2115fa9e4066Sahrens 211644cb6abcSbmc if (arc_no_grow) 2117fa9e4066Sahrens return; 2118fa9e4066Sahrens 211944cb6abcSbmc if (arc_c >= arc_c_max) 2120ea8dc4b6Seschrock return; 2121ea8dc4b6Seschrock 2122fa9e4066Sahrens /* 2123ea8dc4b6Seschrock * If we're within (2 * maxblocksize) bytes of the target 2124ea8dc4b6Seschrock * cache size, increment the target cache size 2125fa9e4066Sahrens */ 212644cb6abcSbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 212744cb6abcSbmc atomic_add_64(&arc_c, (int64_t)bytes); 212844cb6abcSbmc if (arc_c > arc_c_max) 212944cb6abcSbmc arc_c = arc_c_max; 213044cb6abcSbmc else if (state == arc_anon) 213144cb6abcSbmc atomic_add_64(&arc_p, (int64_t)bytes); 213244cb6abcSbmc if (arc_p > arc_c) 213344cb6abcSbmc arc_p = arc_c; 2134fa9e4066Sahrens } 213544cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 2136fa9e4066Sahrens } 2137fa9e4066Sahrens 2138fa9e4066Sahrens /* 2139ea8dc4b6Seschrock * Check if the cache has reached its limits and eviction is required 2140ea8dc4b6Seschrock * prior to insert. 2141fa9e4066Sahrens */ 2142fa9e4066Sahrens static int 21430e8c6158Smaybee arc_evict_needed(arc_buf_contents_t type) 2144fa9e4066Sahrens { 21450e8c6158Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 21460e8c6158Smaybee return (1); 21470e8c6158Smaybee 21480e8c6158Smaybee #ifdef _KERNEL 21490e8c6158Smaybee /* 21500e8c6158Smaybee * If zio data pages are being allocated out of a separate heap segment, 21510e8c6158Smaybee * then enforce that the size of available vmem for this area remains 21520e8c6158Smaybee * above about 1/32nd free. 21530e8c6158Smaybee */ 21540e8c6158Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 21550e8c6158Smaybee vmem_size(zio_arena, VMEM_FREE) < 21560e8c6158Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 21570e8c6158Smaybee return (1); 21580e8c6158Smaybee #endif 21590e8c6158Smaybee 2160fa9e4066Sahrens if (arc_reclaim_needed()) 2161fa9e4066Sahrens return (1); 2162fa9e4066Sahrens 216344cb6abcSbmc return (arc_size > arc_c); 2164fa9e4066Sahrens } 2165fa9e4066Sahrens 2166fa9e4066Sahrens /* 216744eda4d7Smaybee * The buffer, supplied as the first argument, needs a data block. 216844eda4d7Smaybee * So, if we are at cache max, determine which cache should be victimized. 216944eda4d7Smaybee * We have the following cases: 2170fa9e4066Sahrens * 217144cb6abcSbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2172fa9e4066Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2173fa9e4066Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2174fa9e4066Sahrens * 217544cb6abcSbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2176fa9e4066Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2177fa9e4066Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2178fa9e4066Sahrens * entries. 2179fa9e4066Sahrens * 218044cb6abcSbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2181fa9e4066Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2182fa9e4066Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2183fa9e4066Sahrens * the MFU side, so the MRU side needs to be victimized. 2184fa9e4066Sahrens * 218544cb6abcSbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2186fa9e4066Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2187fa9e4066Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2188fa9e4066Sahrens */ 2189fa9e4066Sahrens static void 219044eda4d7Smaybee arc_get_data_buf(arc_buf_t *buf) 2191fa9e4066Sahrens { 2192ad23a2dbSjohansen arc_state_t *state = buf->b_hdr->b_state; 2193ad23a2dbSjohansen uint64_t size = buf->b_hdr->b_size; 2194ad23a2dbSjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 2195fa9e4066Sahrens 219644eda4d7Smaybee arc_adapt(size, state); 2197fa9e4066Sahrens 219844eda4d7Smaybee /* 219944eda4d7Smaybee * We have not yet reached cache maximum size, 220044eda4d7Smaybee * just allocate a new buffer. 220144eda4d7Smaybee */ 22020e8c6158Smaybee if (!arc_evict_needed(type)) { 2203ad23a2dbSjohansen if (type == ARC_BUFC_METADATA) { 2204ad23a2dbSjohansen buf->b_data = zio_buf_alloc(size); 22055a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(size, ARC_SPACE_DATA); 2206ad23a2dbSjohansen } else { 2207ad23a2dbSjohansen ASSERT(type == ARC_BUFC_DATA); 2208ad23a2dbSjohansen buf->b_data = zio_data_buf_alloc(size); 22095a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_data_size, size); 22100e8c6158Smaybee atomic_add_64(&arc_size, size); 2211ad23a2dbSjohansen } 221244eda4d7Smaybee goto out; 221344eda4d7Smaybee } 221444eda4d7Smaybee 221544eda4d7Smaybee /* 221644eda4d7Smaybee * If we are prefetching from the mfu ghost list, this buffer 221744eda4d7Smaybee * will end up on the mru list; so steal space from there. 221844eda4d7Smaybee */ 221944cb6abcSbmc if (state == arc_mfu_ghost) 222044cb6abcSbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 222144cb6abcSbmc else if (state == arc_mru_ghost) 222244cb6abcSbmc state = arc_mru; 222344cb6abcSbmc 222444cb6abcSbmc if (state == arc_mru || state == arc_anon) { 222544cb6abcSbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 22265a98e54bSBrendan Gregg - Sun Microsystems state = (arc_mfu->arcs_lsize[type] >= size && 22270e8c6158Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2228fa9e4066Sahrens } else { 222944eda4d7Smaybee /* MFU cases */ 223044cb6abcSbmc uint64_t mfu_space = arc_c - arc_p; 22315a98e54bSBrendan Gregg - Sun Microsystems state = (arc_mru->arcs_lsize[type] >= size && 22320e8c6158Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 223344eda4d7Smaybee } 2234874395d5Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 2235ad23a2dbSjohansen if (type == ARC_BUFC_METADATA) { 2236ad23a2dbSjohansen buf->b_data = zio_buf_alloc(size); 22375a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(size, ARC_SPACE_DATA); 2238ad23a2dbSjohansen } else { 2239ad23a2dbSjohansen ASSERT(type == ARC_BUFC_DATA); 2240ad23a2dbSjohansen buf->b_data = zio_data_buf_alloc(size); 22415a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_data_size, size); 22420e8c6158Smaybee atomic_add_64(&arc_size, size); 2243ad23a2dbSjohansen } 224444cb6abcSbmc ARCSTAT_BUMP(arcstat_recycle_miss); 224544eda4d7Smaybee } 224644eda4d7Smaybee ASSERT(buf->b_data != NULL); 224744eda4d7Smaybee out: 224844eda4d7Smaybee /* 224944eda4d7Smaybee * Update the state size. Note that ghost states have a 225044eda4d7Smaybee * "ghost size" and so don't need to be updated. 225144eda4d7Smaybee */ 225244eda4d7Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 225344eda4d7Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 225444eda4d7Smaybee 225544cb6abcSbmc atomic_add_64(&hdr->b_state->arcs_size, size); 225644eda4d7Smaybee if (list_link_active(&hdr->b_arc_node)) { 225744eda4d7Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 22580e8c6158Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2259fa9e4066Sahrens } 2260641fbdaeSmaybee /* 2261641fbdaeSmaybee * If we are growing the cache, and we are adding anonymous 226244cb6abcSbmc * data, and we have outgrown arc_p, update arc_p 2263641fbdaeSmaybee */ 226444cb6abcSbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 226544cb6abcSbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 226644cb6abcSbmc arc_p = MIN(arc_c, arc_p + size); 2267fa9e4066Sahrens } 2268fa9e4066Sahrens } 2269fa9e4066Sahrens 2270fa9e4066Sahrens /* 2271fa9e4066Sahrens * This routine is called whenever a buffer is accessed. 2272ea8dc4b6Seschrock * NOTE: the hash lock is dropped in this function. 2273fa9e4066Sahrens */ 2274fa9e4066Sahrens static void 227544eda4d7Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2276fa9e4066Sahrens { 2277fa9e4066Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2278fa9e4066Sahrens 227944cb6abcSbmc if (buf->b_state == arc_anon) { 2280fa9e4066Sahrens /* 2281fa9e4066Sahrens * This buffer is not in the cache, and does not 2282fa9e4066Sahrens * appear in our "ghost" list. Add the new buffer 2283fa9e4066Sahrens * to the MRU state. 2284fa9e4066Sahrens */ 2285fa9e4066Sahrens 2286fa9e4066Sahrens ASSERT(buf->b_arc_access == 0); 2287fa9e4066Sahrens buf->b_arc_access = lbolt; 2288ea8dc4b6Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 228944cb6abcSbmc arc_change_state(arc_mru, buf, hash_lock); 2290fa9e4066Sahrens 229144cb6abcSbmc } else if (buf->b_state == arc_mru) { 2292fa9e4066Sahrens /* 229313506d1eSmaybee * If this buffer is here because of a prefetch, then either: 229413506d1eSmaybee * - clear the flag if this is a "referencing" read 229513506d1eSmaybee * (any subsequent access will bump this into the MFU state). 229613506d1eSmaybee * or 229713506d1eSmaybee * - move the buffer to the head of the list if this is 229813506d1eSmaybee * another prefetch (to make it less likely to be evicted). 2299fa9e4066Sahrens */ 2300fa9e4066Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 230113506d1eSmaybee if (refcount_count(&buf->b_refcnt) == 0) { 230213506d1eSmaybee ASSERT(list_link_active(&buf->b_arc_node)); 230313506d1eSmaybee } else { 230413506d1eSmaybee buf->b_flags &= ~ARC_PREFETCH; 230544cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_hits); 230613506d1eSmaybee } 230713506d1eSmaybee buf->b_arc_access = lbolt; 2308fa9e4066Sahrens return; 2309fa9e4066Sahrens } 2310fa9e4066Sahrens 2311fa9e4066Sahrens /* 2312fa9e4066Sahrens * This buffer has been "accessed" only once so far, 2313fa9e4066Sahrens * but it is still in the cache. Move it to the MFU 2314fa9e4066Sahrens * state. 2315fa9e4066Sahrens */ 2316fa9e4066Sahrens if (lbolt > buf->b_arc_access + ARC_MINTIME) { 2317fa9e4066Sahrens /* 2318fa9e4066Sahrens * More than 125ms have passed since we 2319fa9e4066Sahrens * instantiated this buffer. Move it to the 2320fa9e4066Sahrens * most frequently used state. 2321fa9e4066Sahrens */ 2322fa9e4066Sahrens buf->b_arc_access = lbolt; 2323ea8dc4b6Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 232444cb6abcSbmc arc_change_state(arc_mfu, buf, hash_lock); 2325fa9e4066Sahrens } 232644cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_hits); 232744cb6abcSbmc } else if (buf->b_state == arc_mru_ghost) { 2328fa9e4066Sahrens arc_state_t *new_state; 2329fa9e4066Sahrens /* 2330fa9e4066Sahrens * This buffer has been "accessed" recently, but 2331fa9e4066Sahrens * was evicted from the cache. Move it to the 2332fa9e4066Sahrens * MFU state. 2333fa9e4066Sahrens */ 2334fa9e4066Sahrens 2335fa9e4066Sahrens if (buf->b_flags & ARC_PREFETCH) { 233644cb6abcSbmc new_state = arc_mru; 233713506d1eSmaybee if (refcount_count(&buf->b_refcnt) > 0) 233813506d1eSmaybee buf->b_flags &= ~ARC_PREFETCH; 2339ea8dc4b6Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2340fa9e4066Sahrens } else { 234144cb6abcSbmc new_state = arc_mfu; 2342ea8dc4b6Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2343fa9e4066Sahrens } 2344fa9e4066Sahrens 2345fa9e4066Sahrens buf->b_arc_access = lbolt; 2346fa9e4066Sahrens arc_change_state(new_state, buf, hash_lock); 2347fa9e4066Sahrens 234844cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 234944cb6abcSbmc } else if (buf->b_state == arc_mfu) { 2350fa9e4066Sahrens /* 2351fa9e4066Sahrens * This buffer has been accessed more than once and is 2352fa9e4066Sahrens * still in the cache. Keep it in the MFU state. 2353fa9e4066Sahrens * 235413506d1eSmaybee * NOTE: an add_reference() that occurred when we did 235513506d1eSmaybee * the arc_read() will have kicked this off the list. 235613506d1eSmaybee * If it was a prefetch, we will explicitly move it to 235713506d1eSmaybee * the head of the list now. 2358fa9e4066Sahrens */ 235913506d1eSmaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 236013506d1eSmaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 236113506d1eSmaybee ASSERT(list_link_active(&buf->b_arc_node)); 236213506d1eSmaybee } 236344cb6abcSbmc ARCSTAT_BUMP(arcstat_mfu_hits); 236413506d1eSmaybee buf->b_arc_access = lbolt; 236544cb6abcSbmc } else if (buf->b_state == arc_mfu_ghost) { 236644cb6abcSbmc arc_state_t *new_state = arc_mfu; 2367fa9e4066Sahrens /* 2368fa9e4066Sahrens * This buffer has been accessed more than once but has 2369fa9e4066Sahrens * been evicted from the cache. Move it back to the 2370fa9e4066Sahrens * MFU state. 2371fa9e4066Sahrens */ 2372fa9e4066Sahrens 237313506d1eSmaybee if (buf->b_flags & ARC_PREFETCH) { 237413506d1eSmaybee /* 237513506d1eSmaybee * This is a prefetch access... 237613506d1eSmaybee * move this block back to the MRU state. 237713506d1eSmaybee */ 237813506d1eSmaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 237944cb6abcSbmc new_state = arc_mru; 238013506d1eSmaybee } 238113506d1eSmaybee 2382fa9e4066Sahrens buf->b_arc_access = lbolt; 2383ea8dc4b6Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 238413506d1eSmaybee arc_change_state(new_state, buf, hash_lock); 2385fa9e4066Sahrens 238644cb6abcSbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 2387fa94a07fSbrendan } else if (buf->b_state == arc_l2c_only) { 2388fa94a07fSbrendan /* 2389fa94a07fSbrendan * This buffer is on the 2nd Level ARC. 2390fa94a07fSbrendan */ 2391fa94a07fSbrendan 2392fa94a07fSbrendan buf->b_arc_access = lbolt; 2393fa94a07fSbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2394fa94a07fSbrendan arc_change_state(arc_mfu, buf, hash_lock); 2395fa9e4066Sahrens } else { 2396fa9e4066Sahrens ASSERT(!"invalid arc state"); 2397fa9e4066Sahrens } 2398fa9e4066Sahrens } 2399fa9e4066Sahrens 2400fa9e4066Sahrens /* a generic arc_done_func_t which you can use */ 2401fa9e4066Sahrens /* ARGSUSED */ 2402fa9e4066Sahrens void 2403fa9e4066Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2404fa9e4066Sahrens { 2405fa9e4066Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 2406ea8dc4b6Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2407fa9e4066Sahrens } 2408fa9e4066Sahrens 24090e8c6158Smaybee /* a generic arc_done_func_t */ 2410fa9e4066Sahrens void 2411fa9e4066Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2412fa9e4066Sahrens { 2413fa9e4066Sahrens arc_buf_t **bufp = arg; 2414fa9e4066Sahrens if (zio && zio->io_error) { 2415ea8dc4b6Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2416fa9e4066Sahrens *bufp = NULL; 2417fa9e4066Sahrens } else { 2418fa9e4066Sahrens *bufp = buf; 2419fa9e4066Sahrens } 2420fa9e4066Sahrens } 2421fa9e4066Sahrens 2422fa9e4066Sahrens static void 2423fa9e4066Sahrens arc_read_done(zio_t *zio) 2424fa9e4066Sahrens { 2425bbf4a8dfSmaybee arc_buf_hdr_t *hdr, *found; 2426fa9e4066Sahrens arc_buf_t *buf; 2427fa9e4066Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2428fa9e4066Sahrens kmutex_t *hash_lock; 2429fa9e4066Sahrens arc_callback_t *callback_list, *acb; 2430fa9e4066Sahrens int freeable = FALSE; 2431fa9e4066Sahrens 2432fa9e4066Sahrens buf = zio->io_private; 2433fa9e4066Sahrens hdr = buf->b_hdr; 2434fa9e4066Sahrens 2435bbf4a8dfSmaybee /* 2436bbf4a8dfSmaybee * The hdr was inserted into hash-table and removed from lists 2437bbf4a8dfSmaybee * prior to starting I/O. We should find this header, since 2438bbf4a8dfSmaybee * it's in the hash table, and it should be legit since it's 2439bbf4a8dfSmaybee * not possible to evict it during the I/O. The only possible 2440bbf4a8dfSmaybee * reason for it not to be found is if we were freed during the 2441bbf4a8dfSmaybee * read. 2442bbf4a8dfSmaybee */ 2443ac05c741SMark Maybee found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 24446b4acc8bSahrens &hash_lock); 2445fa9e4066Sahrens 2446bbf4a8dfSmaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 2447fa94a07fSbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 2448fa94a07fSbrendan (found == hdr && HDR_L2_READING(hdr))); 2449fa94a07fSbrendan 24503a737e0dSbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 2451fa94a07fSbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 24523baa08fcSek hdr->b_flags &= ~ARC_L2CACHE; 2453fa9e4066Sahrens 2454fa9e4066Sahrens /* byteswap if necessary */ 2455fa9e4066Sahrens callback_list = hdr->b_acb; 2456fa9e4066Sahrens ASSERT(callback_list != NULL); 2457088f3894Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 2458088f3894Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 2459088f3894Sahrens byteswap_uint64_array : 2460088f3894Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 2461088f3894Sahrens func(buf->b_data, hdr->b_size); 2462088f3894Sahrens } 2463fa9e4066Sahrens 2464fa94a07fSbrendan arc_cksum_compute(buf, B_FALSE); 24656b4acc8bSahrens 2466fa9e4066Sahrens /* create copies of the data buffer for the callers */ 2467fa9e4066Sahrens abuf = buf; 2468fa9e4066Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2469fa9e4066Sahrens if (acb->acb_done) { 247044eda4d7Smaybee if (abuf == NULL) 247144eda4d7Smaybee abuf = arc_buf_clone(buf); 2472fa9e4066Sahrens acb->acb_buf = abuf; 2473fa9e4066Sahrens abuf = NULL; 2474fa9e4066Sahrens } 2475fa9e4066Sahrens } 2476fa9e4066Sahrens hdr->b_acb = NULL; 2477fa9e4066Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 2478ea8dc4b6Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 2479ea8dc4b6Seschrock if (abuf == buf) 2480ea8dc4b6Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 2481fa9e4066Sahrens 2482fa9e4066Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2483fa9e4066Sahrens 2484fa9e4066Sahrens if (zio->io_error != 0) { 2485fa9e4066Sahrens hdr->b_flags |= ARC_IO_ERROR; 248644cb6abcSbmc if (hdr->b_state != arc_anon) 248744cb6abcSbmc arc_change_state(arc_anon, hdr, hash_lock); 2488ea8dc4b6Seschrock if (HDR_IN_HASH_TABLE(hdr)) 2489ea8dc4b6Seschrock buf_hash_remove(hdr); 2490fa9e4066Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2491fa9e4066Sahrens } 2492fa9e4066Sahrens 2493ea8dc4b6Seschrock /* 249413506d1eSmaybee * Broadcast before we drop the hash_lock to avoid the possibility 249513506d1eSmaybee * that the hdr (and hence the cv) might be freed before we get to 249613506d1eSmaybee * the cv_broadcast(). 2497ea8dc4b6Seschrock */ 2498ea8dc4b6Seschrock cv_broadcast(&hdr->b_cv); 2499ea8dc4b6Seschrock 2500bbf4a8dfSmaybee if (hash_lock) { 2501fa9e4066Sahrens /* 2502fa9e4066Sahrens * Only call arc_access on anonymous buffers. This is because 2503fa9e4066Sahrens * if we've issued an I/O for an evicted buffer, we've already 2504fa9e4066Sahrens * called arc_access (to prevent any simultaneous readers from 2505fa9e4066Sahrens * getting confused). 2506fa9e4066Sahrens */ 250744cb6abcSbmc if (zio->io_error == 0 && hdr->b_state == arc_anon) 250844eda4d7Smaybee arc_access(hdr, hash_lock); 250944eda4d7Smaybee mutex_exit(hash_lock); 2510fa9e4066Sahrens } else { 2511fa9e4066Sahrens /* 2512fa9e4066Sahrens * This block was freed while we waited for the read to 2513fa9e4066Sahrens * complete. It has been removed from the hash table and 2514fa9e4066Sahrens * moved to the anonymous state (so that it won't show up 2515fa9e4066Sahrens * in the cache). 2516fa9e4066Sahrens */ 251744cb6abcSbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2518fa9e4066Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2519fa9e4066Sahrens } 2520fa9e4066Sahrens 2521fa9e4066Sahrens /* execute each callback and free its structure */ 2522fa9e4066Sahrens while ((acb = callback_list) != NULL) { 2523fa9e4066Sahrens if (acb->acb_done) 2524fa9e4066Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2525fa9e4066Sahrens 2526fa9e4066Sahrens if (acb->acb_zio_dummy != NULL) { 2527fa9e4066Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2528fa9e4066Sahrens zio_nowait(acb->acb_zio_dummy); 2529fa9e4066Sahrens } 2530fa9e4066Sahrens 2531fa9e4066Sahrens callback_list = acb->acb_next; 2532fa9e4066Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2533fa9e4066Sahrens } 2534fa9e4066Sahrens 2535fa9e4066Sahrens if (freeable) 2536ea8dc4b6Seschrock arc_hdr_destroy(hdr); 2537fa9e4066Sahrens } 2538fa9e4066Sahrens 2539fa9e4066Sahrens /* 2540fa9e4066Sahrens * "Read" the block block at the specified DVA (in bp) via the 2541fa9e4066Sahrens * cache. If the block is found in the cache, invoke the provided 2542fa9e4066Sahrens * callback immediately and return. Note that the `zio' parameter 2543fa9e4066Sahrens * in the callback will be NULL in this case, since no IO was 2544fa9e4066Sahrens * required. If the block is not in the cache pass the read request 2545fa9e4066Sahrens * on to the spa with a substitute callback function, so that the 2546fa9e4066Sahrens * requested block will be added to the cache. 2547fa9e4066Sahrens * 2548fa9e4066Sahrens * If a read request arrives for a block that has a read in-progress, 2549fa9e4066Sahrens * either wait for the in-progress read to complete (and return the 2550fa9e4066Sahrens * results); or, if this is a read with a "done" func, add a record 2551fa9e4066Sahrens * to the read to invoke the "done" func when the read completes, 2552fa9e4066Sahrens * and return; or just return. 2553fa9e4066Sahrens * 2554fa9e4066Sahrens * arc_read_done() will invoke all the requested "done" functions 2555fa9e4066Sahrens * for readers of this block. 2556088f3894Sahrens * 2557088f3894Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 2558088f3894Sahrens * for the bp. But if you know you don't need locking, you can use 25598ad10dceSSuhasini Peddada * arc_read_bp. 2560fa9e4066Sahrens */ 2561fa9e4066Sahrens int 2562088f3894Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_buf_t *pbuf, 25633baa08fcSek arc_done_func_t *done, void *private, int priority, int zio_flags, 2564088f3894Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2565088f3894Sahrens { 2566088f3894Sahrens int err; 2567088f3894Sahrens 2568088f3894Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 2569088f3894Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 25706f83844dSMark Maybee rw_enter(&pbuf->b_lock, RW_READER); 2571088f3894Sahrens 2572088f3894Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 25733baa08fcSek zio_flags, arc_flags, zb); 25746f83844dSMark Maybee rw_exit(&pbuf->b_lock); 257514843421SMatthew Ahrens 2576088f3894Sahrens return (err); 2577088f3894Sahrens } 2578088f3894Sahrens 2579088f3894Sahrens int 2580088f3894Sahrens arc_read_nolock(zio_t *pio, spa_t *spa, blkptr_t *bp, 25813baa08fcSek arc_done_func_t *done, void *private, int priority, int zio_flags, 2582088f3894Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2583fa9e4066Sahrens { 2584fa9e4066Sahrens arc_buf_hdr_t *hdr; 2585fa9e4066Sahrens arc_buf_t *buf; 2586fa9e4066Sahrens kmutex_t *hash_lock; 2587fa94a07fSbrendan zio_t *rzio; 2588ac05c741SMark Maybee uint64_t guid = spa_guid(spa); 2589fa9e4066Sahrens 2590fa9e4066Sahrens top: 2591ac05c741SMark Maybee hdr = buf_hash_find(guid, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 2592ea8dc4b6Seschrock if (hdr && hdr->b_datacnt > 0) { 2593fa9e4066Sahrens 259413506d1eSmaybee *arc_flags |= ARC_CACHED; 259513506d1eSmaybee 2596fa9e4066Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 259713506d1eSmaybee 259813506d1eSmaybee if (*arc_flags & ARC_WAIT) { 259913506d1eSmaybee cv_wait(&hdr->b_cv, hash_lock); 260013506d1eSmaybee mutex_exit(hash_lock); 260113506d1eSmaybee goto top; 260213506d1eSmaybee } 260313506d1eSmaybee ASSERT(*arc_flags & ARC_NOWAIT); 260413506d1eSmaybee 260513506d1eSmaybee if (done) { 2606fa9e4066Sahrens arc_callback_t *acb = NULL; 2607fa9e4066Sahrens 2608fa9e4066Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2609fa9e4066Sahrens KM_SLEEP); 2610fa9e4066Sahrens acb->acb_done = done; 2611fa9e4066Sahrens acb->acb_private = private; 2612fa9e4066Sahrens if (pio != NULL) 2613fa9e4066Sahrens acb->acb_zio_dummy = zio_null(pio, 2614a3f829aeSBill Moore spa, NULL, NULL, NULL, zio_flags); 2615fa9e4066Sahrens 2616fa9e4066Sahrens ASSERT(acb->acb_done != NULL); 2617fa9e4066Sahrens acb->acb_next = hdr->b_acb; 2618fa9e4066Sahrens hdr->b_acb = acb; 2619fa9e4066Sahrens add_reference(hdr, hash_lock, private); 2620fa9e4066Sahrens mutex_exit(hash_lock); 2621fa9e4066Sahrens return (0); 2622fa9e4066Sahrens } 2623fa9e4066Sahrens mutex_exit(hash_lock); 2624fa9e4066Sahrens return (0); 2625fa9e4066Sahrens } 2626fa9e4066Sahrens 262744cb6abcSbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2628fa9e4066Sahrens 2629ea8dc4b6Seschrock if (done) { 263044eda4d7Smaybee add_reference(hdr, hash_lock, private); 2631ea8dc4b6Seschrock /* 2632ea8dc4b6Seschrock * If this block is already in use, create a new 2633ea8dc4b6Seschrock * copy of the data so that we will be guaranteed 2634ea8dc4b6Seschrock * that arc_release() will always succeed. 2635ea8dc4b6Seschrock */ 2636fa9e4066Sahrens buf = hdr->b_buf; 2637ea8dc4b6Seschrock ASSERT(buf); 2638ea8dc4b6Seschrock ASSERT(buf->b_data); 263944eda4d7Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 2640ea8dc4b6Seschrock ASSERT(buf->b_efunc == NULL); 2641ea8dc4b6Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 264244eda4d7Smaybee } else { 264344eda4d7Smaybee buf = arc_buf_clone(buf); 2644ea8dc4b6Seschrock } 264513506d1eSmaybee } else if (*arc_flags & ARC_PREFETCH && 264613506d1eSmaybee refcount_count(&hdr->b_refcnt) == 0) { 264713506d1eSmaybee hdr->b_flags |= ARC_PREFETCH; 2648fa9e4066Sahrens } 2649fa9e4066Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 265044eda4d7Smaybee arc_access(hdr, hash_lock); 26513baa08fcSek if (*arc_flags & ARC_L2CACHE) 26523baa08fcSek hdr->b_flags |= ARC_L2CACHE; 265344eda4d7Smaybee mutex_exit(hash_lock); 265444cb6abcSbmc ARCSTAT_BUMP(arcstat_hits); 265544cb6abcSbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 265644cb6abcSbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 265744cb6abcSbmc data, metadata, hits); 265844cb6abcSbmc 2659fa9e4066Sahrens if (done) 2660fa9e4066Sahrens done(NULL, buf, private); 2661fa9e4066Sahrens } else { 2662fa9e4066Sahrens uint64_t size = BP_GET_LSIZE(bp); 2663fa9e4066Sahrens arc_callback_t *acb; 26643a737e0dSbrendan vdev_t *vd = NULL; 2665f9d8334fSGeorge Wilson uint64_t addr; 26665a98e54bSBrendan Gregg - Sun Microsystems boolean_t devw = B_FALSE; 2667fa9e4066Sahrens 2668fa9e4066Sahrens if (hdr == NULL) { 2669fa9e4066Sahrens /* this block is not in the cache */ 2670fa9e4066Sahrens arc_buf_hdr_t *exists; 2671ad23a2dbSjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 2672ad23a2dbSjohansen buf = arc_buf_alloc(spa, size, private, type); 2673fa9e4066Sahrens hdr = buf->b_hdr; 2674fa9e4066Sahrens hdr->b_dva = *BP_IDENTITY(bp); 2675fa9e4066Sahrens hdr->b_birth = bp->blk_birth; 2676fa9e4066Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2677fa9e4066Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2678fa9e4066Sahrens if (exists) { 2679fa9e4066Sahrens /* somebody beat us to the hash insert */ 2680fa9e4066Sahrens mutex_exit(hash_lock); 2681fa9e4066Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2682fa9e4066Sahrens hdr->b_birth = 0; 2683fa9e4066Sahrens hdr->b_cksum0 = 0; 2684ea8dc4b6Seschrock (void) arc_buf_remove_ref(buf, private); 2685fa9e4066Sahrens goto top; /* restart the IO request */ 2686fa9e4066Sahrens } 268713506d1eSmaybee /* if this is a prefetch, we don't have a reference */ 268813506d1eSmaybee if (*arc_flags & ARC_PREFETCH) { 268913506d1eSmaybee (void) remove_reference(hdr, hash_lock, 269013506d1eSmaybee private); 269113506d1eSmaybee hdr->b_flags |= ARC_PREFETCH; 269213506d1eSmaybee } 26933baa08fcSek if (*arc_flags & ARC_L2CACHE) 26943baa08fcSek hdr->b_flags |= ARC_L2CACHE; 269513506d1eSmaybee if (BP_GET_LEVEL(bp) > 0) 269613506d1eSmaybee hdr->b_flags |= ARC_INDIRECT; 2697fa9e4066Sahrens } else { 2698fa9e4066Sahrens /* this block is in the ghost cache */ 2699ea8dc4b6Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 2700ea8dc4b6Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 270113506d1eSmaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 2702ea8dc4b6Seschrock ASSERT(hdr->b_buf == NULL); 270313506d1eSmaybee 270413506d1eSmaybee /* if this is a prefetch, we don't have a reference */ 270513506d1eSmaybee if (*arc_flags & ARC_PREFETCH) 270613506d1eSmaybee hdr->b_flags |= ARC_PREFETCH; 270713506d1eSmaybee else 270813506d1eSmaybee add_reference(hdr, hash_lock, private); 27093baa08fcSek if (*arc_flags & ARC_L2CACHE) 27103baa08fcSek hdr->b_flags |= ARC_L2CACHE; 27111ab7f2deSmaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2712fa9e4066Sahrens buf->b_hdr = hdr; 271344eda4d7Smaybee buf->b_data = NULL; 2714ea8dc4b6Seschrock buf->b_efunc = NULL; 2715ea8dc4b6Seschrock buf->b_private = NULL; 2716fa9e4066Sahrens buf->b_next = NULL; 2717fa9e4066Sahrens hdr->b_buf = buf; 271844eda4d7Smaybee arc_get_data_buf(buf); 2719ea8dc4b6Seschrock ASSERT(hdr->b_datacnt == 0); 2720ea8dc4b6Seschrock hdr->b_datacnt = 1; 272113506d1eSmaybee 2722fa9e4066Sahrens } 2723fa9e4066Sahrens 2724fa9e4066Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2725fa9e4066Sahrens acb->acb_done = done; 2726fa9e4066Sahrens acb->acb_private = private; 2727fa9e4066Sahrens 2728fa9e4066Sahrens ASSERT(hdr->b_acb == NULL); 2729fa9e4066Sahrens hdr->b_acb = acb; 2730fa9e4066Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2731fa9e4066Sahrens 2732fa9e4066Sahrens /* 2733fa9e4066Sahrens * If the buffer has been evicted, migrate it to a present state 2734fa9e4066Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2735fa9e4066Sahrens * the header will be marked as I/O in progress and have an 2736fa9e4066Sahrens * attached buffer. At this point, anybody who finds this 2737fa9e4066Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2738fa9e4066Sahrens */ 2739fa9e4066Sahrens 2740ea8dc4b6Seschrock if (GHOST_STATE(hdr->b_state)) 274144eda4d7Smaybee arc_access(hdr, hash_lock); 2742fa9e4066Sahrens 2743e14bb325SJeff Bonwick if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 2744e14bb325SJeff Bonwick (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 27455a98e54bSBrendan Gregg - Sun Microsystems devw = hdr->b_l2hdr->b_dev->l2ad_writing; 27463a737e0dSbrendan addr = hdr->b_l2hdr->b_daddr; 2747e14bb325SJeff Bonwick /* 2748e14bb325SJeff Bonwick * Lock out device removal. 2749e14bb325SJeff Bonwick */ 2750e14bb325SJeff Bonwick if (vdev_is_dead(vd) || 2751e14bb325SJeff Bonwick !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 2752e14bb325SJeff Bonwick vd = NULL; 27533a737e0dSbrendan } 27543a737e0dSbrendan 27553a737e0dSbrendan mutex_exit(hash_lock); 27563a737e0dSbrendan 2757fa9e4066Sahrens ASSERT3U(hdr->b_size, ==, size); 2758c543ec06Sahrens DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size, 2759c543ec06Sahrens zbookmark_t *, zb); 276044cb6abcSbmc ARCSTAT_BUMP(arcstat_misses); 276144cb6abcSbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 276244cb6abcSbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 276344cb6abcSbmc data, metadata, misses); 2764ea8dc4b6Seschrock 27655a98e54bSBrendan Gregg - Sun Microsystems if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 2766fa94a07fSbrendan /* 2767fa94a07fSbrendan * Read from the L2ARC if the following are true: 27683a737e0dSbrendan * 1. The L2ARC vdev was previously cached. 27693a737e0dSbrendan * 2. This buffer still has L2ARC metadata. 27703a737e0dSbrendan * 3. This buffer isn't currently writing to the L2ARC. 27713a737e0dSbrendan * 4. The L2ARC entry wasn't evicted, which may 27723a737e0dSbrendan * also have invalidated the vdev. 27735a98e54bSBrendan Gregg - Sun Microsystems * 5. This isn't prefetch and l2arc_noprefetch is set. 2774fa94a07fSbrendan */ 2775e14bb325SJeff Bonwick if (hdr->b_l2hdr != NULL && 27765a98e54bSBrendan Gregg - Sun Microsystems !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 27775a98e54bSBrendan Gregg - Sun Microsystems !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 2778fa94a07fSbrendan l2arc_read_callback_t *cb; 2779fa94a07fSbrendan 2780c5904d13Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 2781c5904d13Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 2782c5904d13Seschrock 2783fa94a07fSbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 2784fa94a07fSbrendan KM_SLEEP); 2785fa94a07fSbrendan cb->l2rcb_buf = buf; 2786fa94a07fSbrendan cb->l2rcb_spa = spa; 2787fa94a07fSbrendan cb->l2rcb_bp = *bp; 2788fa94a07fSbrendan cb->l2rcb_zb = *zb; 27893baa08fcSek cb->l2rcb_flags = zio_flags; 2790fa94a07fSbrendan 2791fa94a07fSbrendan /* 2792e14bb325SJeff Bonwick * l2arc read. The SCL_L2ARC lock will be 2793e14bb325SJeff Bonwick * released by l2arc_read_done(). 2794fa94a07fSbrendan */ 2795fa94a07fSbrendan rzio = zio_read_phys(pio, vd, addr, size, 2796fa94a07fSbrendan buf->b_data, ZIO_CHECKSUM_OFF, 27973baa08fcSek l2arc_read_done, cb, priority, zio_flags | 279849cf58c0SBrendan Gregg - Sun Microsystems ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 2799e14bb325SJeff Bonwick ZIO_FLAG_DONT_PROPAGATE | 2800e14bb325SJeff Bonwick ZIO_FLAG_DONT_RETRY, B_FALSE); 2801fa94a07fSbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 2802fa94a07fSbrendan zio_t *, rzio); 28035a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_l2_read_bytes, size); 2804fa94a07fSbrendan 28053a737e0dSbrendan if (*arc_flags & ARC_NOWAIT) { 28063a737e0dSbrendan zio_nowait(rzio); 28073a737e0dSbrendan return (0); 28083a737e0dSbrendan } 2809fa94a07fSbrendan 28103a737e0dSbrendan ASSERT(*arc_flags & ARC_WAIT); 28113a737e0dSbrendan if (zio_wait(rzio) == 0) 28123a737e0dSbrendan return (0); 28133a737e0dSbrendan 28143a737e0dSbrendan /* l2arc read error; goto zio_read() */ 2815fa94a07fSbrendan } else { 2816fa94a07fSbrendan DTRACE_PROBE1(l2arc__miss, 2817fa94a07fSbrendan arc_buf_hdr_t *, hdr); 2818fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_misses); 2819fa94a07fSbrendan if (HDR_L2_WRITING(hdr)) 2820fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 2821e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, vd); 2822fa94a07fSbrendan } 28235a98e54bSBrendan Gregg - Sun Microsystems } else { 282476a25fafSBill Moore if (vd != NULL) 282576a25fafSBill Moore spa_config_exit(spa, SCL_L2ARC, vd); 28265a98e54bSBrendan Gregg - Sun Microsystems if (l2arc_ndev != 0) { 28275a98e54bSBrendan Gregg - Sun Microsystems DTRACE_PROBE1(l2arc__miss, 28285a98e54bSBrendan Gregg - Sun Microsystems arc_buf_hdr_t *, hdr); 28295a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_BUMP(arcstat_l2_misses); 28305a98e54bSBrendan Gregg - Sun Microsystems } 2831fa94a07fSbrendan } 2832c5904d13Seschrock 2833fa9e4066Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 28343baa08fcSek arc_read_done, buf, priority, zio_flags, zb); 2835fa9e4066Sahrens 283613506d1eSmaybee if (*arc_flags & ARC_WAIT) 2837fa9e4066Sahrens return (zio_wait(rzio)); 2838fa9e4066Sahrens 283913506d1eSmaybee ASSERT(*arc_flags & ARC_NOWAIT); 2840fa9e4066Sahrens zio_nowait(rzio); 2841fa9e4066Sahrens } 2842fa9e4066Sahrens return (0); 2843fa9e4066Sahrens } 2844fa9e4066Sahrens 2845fa9e4066Sahrens /* 2846fa9e4066Sahrens * arc_read() variant to support pool traversal. If the block is already 2847fa9e4066Sahrens * in the ARC, make a copy of it; otherwise, the caller will do the I/O. 2848fa9e4066Sahrens * The idea is that we don't want pool traversal filling up memory, but 2849fa9e4066Sahrens * if the ARC already has the data anyway, we shouldn't pay for the I/O. 2850fa9e4066Sahrens */ 2851fa9e4066Sahrens int 2852fa9e4066Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data) 2853fa9e4066Sahrens { 2854fa9e4066Sahrens arc_buf_hdr_t *hdr; 2855fa9e4066Sahrens kmutex_t *hash_mtx; 2856ac05c741SMark Maybee uint64_t guid = spa_guid(spa); 2857fa9e4066Sahrens int rc = 0; 2858fa9e4066Sahrens 2859ac05c741SMark Maybee hdr = buf_hash_find(guid, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx); 2860fa9e4066Sahrens 2861ea8dc4b6Seschrock if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) { 2862ea8dc4b6Seschrock arc_buf_t *buf = hdr->b_buf; 2863ea8dc4b6Seschrock 2864ea8dc4b6Seschrock ASSERT(buf); 2865ea8dc4b6Seschrock while (buf->b_data == NULL) { 2866ea8dc4b6Seschrock buf = buf->b_next; 2867ea8dc4b6Seschrock ASSERT(buf); 2868ea8dc4b6Seschrock } 2869ea8dc4b6Seschrock bcopy(buf->b_data, data, hdr->b_size); 2870ea8dc4b6Seschrock } else { 2871fa9e4066Sahrens rc = ENOENT; 2872ea8dc4b6Seschrock } 2873fa9e4066Sahrens 2874fa9e4066Sahrens if (hash_mtx) 2875fa9e4066Sahrens mutex_exit(hash_mtx); 2876fa9e4066Sahrens 2877fa9e4066Sahrens return (rc); 2878fa9e4066Sahrens } 2879fa9e4066Sahrens 2880ea8dc4b6Seschrock void 2881ea8dc4b6Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 2882ea8dc4b6Seschrock { 2883ea8dc4b6Seschrock ASSERT(buf->b_hdr != NULL); 288444cb6abcSbmc ASSERT(buf->b_hdr->b_state != arc_anon); 2885ea8dc4b6Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 2886ea8dc4b6Seschrock buf->b_efunc = func; 2887ea8dc4b6Seschrock buf->b_private = private; 2888ea8dc4b6Seschrock } 2889ea8dc4b6Seschrock 2890ea8dc4b6Seschrock /* 2891ea8dc4b6Seschrock * This is used by the DMU to let the ARC know that a buffer is 2892ea8dc4b6Seschrock * being evicted, so the ARC should clean up. If this arc buf 2893ea8dc4b6Seschrock * is not yet in the evicted state, it will be put there. 2894ea8dc4b6Seschrock */ 2895ea8dc4b6Seschrock int 2896ea8dc4b6Seschrock arc_buf_evict(arc_buf_t *buf) 2897ea8dc4b6Seschrock { 289840d7d650Smaybee arc_buf_hdr_t *hdr; 2899ea8dc4b6Seschrock kmutex_t *hash_lock; 2900ea8dc4b6Seschrock arc_buf_t **bufp; 2901ea8dc4b6Seschrock 29026f83844dSMark Maybee rw_enter(&buf->b_lock, RW_WRITER); 290340d7d650Smaybee hdr = buf->b_hdr; 2904ea8dc4b6Seschrock if (hdr == NULL) { 2905ea8dc4b6Seschrock /* 2906ea8dc4b6Seschrock * We are in arc_do_user_evicts(). 2907ea8dc4b6Seschrock */ 2908ea8dc4b6Seschrock ASSERT(buf->b_data == NULL); 29096f83844dSMark Maybee rw_exit(&buf->b_lock); 2910ea8dc4b6Seschrock return (0); 29116f83844dSMark Maybee } else if (buf->b_data == NULL) { 29126f83844dSMark Maybee arc_buf_t copy = *buf; /* structure assignment */ 29139b23f181Smaybee /* 29146f83844dSMark Maybee * We are on the eviction list; process this buffer now 29156f83844dSMark Maybee * but let arc_do_user_evicts() do the reaping. 29169b23f181Smaybee */ 29176f83844dSMark Maybee buf->b_efunc = NULL; 29186f83844dSMark Maybee rw_exit(&buf->b_lock); 29196f83844dSMark Maybee VERIFY(copy.b_efunc(©) == 0); 29206f83844dSMark Maybee return (1); 29219b23f181Smaybee } 29226f83844dSMark Maybee hash_lock = HDR_LOCK(hdr); 29236f83844dSMark Maybee mutex_enter(hash_lock); 29249b23f181Smaybee 29259b23f181Smaybee ASSERT(buf->b_hdr == hdr); 29269b23f181Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 292744cb6abcSbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2928ea8dc4b6Seschrock 2929ea8dc4b6Seschrock /* 2930ea8dc4b6Seschrock * Pull this buffer off of the hdr 2931ea8dc4b6Seschrock */ 2932ea8dc4b6Seschrock bufp = &hdr->b_buf; 2933ea8dc4b6Seschrock while (*bufp != buf) 2934ea8dc4b6Seschrock bufp = &(*bufp)->b_next; 2935ea8dc4b6Seschrock *bufp = buf->b_next; 2936ea8dc4b6Seschrock 2937ea8dc4b6Seschrock ASSERT(buf->b_data != NULL); 293844eda4d7Smaybee arc_buf_destroy(buf, FALSE, FALSE); 2939ea8dc4b6Seschrock 2940ea8dc4b6Seschrock if (hdr->b_datacnt == 0) { 2941ea8dc4b6Seschrock arc_state_t *old_state = hdr->b_state; 2942ea8dc4b6Seschrock arc_state_t *evicted_state; 2943ea8dc4b6Seschrock 2944ea8dc4b6Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 2945ea8dc4b6Seschrock 2946ea8dc4b6Seschrock evicted_state = 294744cb6abcSbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 2948ea8dc4b6Seschrock 294944cb6abcSbmc mutex_enter(&old_state->arcs_mtx); 295044cb6abcSbmc mutex_enter(&evicted_state->arcs_mtx); 2951ea8dc4b6Seschrock 2952ea8dc4b6Seschrock arc_change_state(evicted_state, hdr, hash_lock); 2953ea8dc4b6Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 2954fa94a07fSbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 2955fa94a07fSbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 2956ea8dc4b6Seschrock 295744cb6abcSbmc mutex_exit(&evicted_state->arcs_mtx); 295844cb6abcSbmc mutex_exit(&old_state->arcs_mtx); 2959ea8dc4b6Seschrock } 2960ea8dc4b6Seschrock mutex_exit(hash_lock); 29616f83844dSMark Maybee rw_exit(&buf->b_lock); 2962dd6ef538Smaybee 2963ea8dc4b6Seschrock VERIFY(buf->b_efunc(buf) == 0); 2964ea8dc4b6Seschrock buf->b_efunc = NULL; 2965ea8dc4b6Seschrock buf->b_private = NULL; 2966ea8dc4b6Seschrock buf->b_hdr = NULL; 2967ea8dc4b6Seschrock kmem_cache_free(buf_cache, buf); 2968ea8dc4b6Seschrock return (1); 2969ea8dc4b6Seschrock } 2970ea8dc4b6Seschrock 2971fa9e4066Sahrens /* 2972fa9e4066Sahrens * Release this buffer from the cache. This must be done 2973fa9e4066Sahrens * after a read and prior to modifying the buffer contents. 2974fa9e4066Sahrens * If the buffer has more than one reference, we must make 2975088f3894Sahrens * a new hdr for the buffer. 2976fa9e4066Sahrens */ 2977fa9e4066Sahrens void 2978fa9e4066Sahrens arc_release(arc_buf_t *buf, void *tag) 2979fa9e4066Sahrens { 29806f83844dSMark Maybee arc_buf_hdr_t *hdr; 29816f83844dSMark Maybee kmutex_t *hash_lock; 29826f83844dSMark Maybee l2arc_buf_hdr_t *l2hdr; 2983fa94a07fSbrendan uint64_t buf_size; 29840a95608cSBrendan Gregg - Sun Microsystems boolean_t released = B_FALSE; 2985fa9e4066Sahrens 29866f83844dSMark Maybee rw_enter(&buf->b_lock, RW_WRITER); 29876f83844dSMark Maybee hdr = buf->b_hdr; 29886f83844dSMark Maybee 2989fa9e4066Sahrens /* this buffer is not on any list */ 2990fa9e4066Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 2991088f3894Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 2992fa9e4066Sahrens 299344cb6abcSbmc if (hdr->b_state == arc_anon) { 2994fa9e4066Sahrens /* this buffer is already released */ 2995fa9e4066Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2996fa9e4066Sahrens ASSERT(BUF_EMPTY(hdr)); 2997ea8dc4b6Seschrock ASSERT(buf->b_efunc == NULL); 29986b4acc8bSahrens arc_buf_thaw(buf); 29996f83844dSMark Maybee rw_exit(&buf->b_lock); 30000a95608cSBrendan Gregg - Sun Microsystems released = B_TRUE; 30010a95608cSBrendan Gregg - Sun Microsystems } else { 30020a95608cSBrendan Gregg - Sun Microsystems hash_lock = HDR_LOCK(hdr); 30030a95608cSBrendan Gregg - Sun Microsystems mutex_enter(hash_lock); 3004fa9e4066Sahrens } 3005fa9e4066Sahrens 30066f83844dSMark Maybee l2hdr = hdr->b_l2hdr; 30076f83844dSMark Maybee if (l2hdr) { 30086f83844dSMark Maybee mutex_enter(&l2arc_buflist_mtx); 30096f83844dSMark Maybee hdr->b_l2hdr = NULL; 30106f83844dSMark Maybee buf_size = hdr->b_size; 30116f83844dSMark Maybee } 30126f83844dSMark Maybee 30130a95608cSBrendan Gregg - Sun Microsystems if (released) 30140a95608cSBrendan Gregg - Sun Microsystems goto out; 30150a95608cSBrendan Gregg - Sun Microsystems 3016ea8dc4b6Seschrock /* 3017ea8dc4b6Seschrock * Do we have more than one buf? 3018ea8dc4b6Seschrock */ 30196f83844dSMark Maybee if (hdr->b_datacnt > 1) { 3020fa9e4066Sahrens arc_buf_hdr_t *nhdr; 3021fa9e4066Sahrens arc_buf_t **bufp; 3022fa9e4066Sahrens uint64_t blksz = hdr->b_size; 3023ac05c741SMark Maybee uint64_t spa = hdr->b_spa; 3024ad23a2dbSjohansen arc_buf_contents_t type = hdr->b_type; 3025fa94a07fSbrendan uint32_t flags = hdr->b_flags; 3026fa9e4066Sahrens 30276f83844dSMark Maybee ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3028fa9e4066Sahrens /* 3029fa9e4066Sahrens * Pull the data off of this buf and attach it to 3030fa9e4066Sahrens * a new anonymous buf. 3031fa9e4066Sahrens */ 3032ea8dc4b6Seschrock (void) remove_reference(hdr, hash_lock, tag); 3033fa9e4066Sahrens bufp = &hdr->b_buf; 3034ea8dc4b6Seschrock while (*bufp != buf) 3035fa9e4066Sahrens bufp = &(*bufp)->b_next; 3036fa9e4066Sahrens *bufp = (*bufp)->b_next; 3037af2c4821Smaybee buf->b_next = NULL; 3038ea8dc4b6Seschrock 303944cb6abcSbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 304044cb6abcSbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 3041ea8dc4b6Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 30420e8c6158Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 30430e8c6158Smaybee ASSERT3U(*size, >=, hdr->b_size); 30440e8c6158Smaybee atomic_add_64(size, -hdr->b_size); 3045ea8dc4b6Seschrock } 3046ea8dc4b6Seschrock hdr->b_datacnt -= 1; 3047c717a561Smaybee arc_cksum_verify(buf); 3048ea8dc4b6Seschrock 3049fa9e4066Sahrens mutex_exit(hash_lock); 3050fa9e4066Sahrens 30511ab7f2deSmaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3052fa9e4066Sahrens nhdr->b_size = blksz; 3053fa9e4066Sahrens nhdr->b_spa = spa; 3054ad23a2dbSjohansen nhdr->b_type = type; 3055fa9e4066Sahrens nhdr->b_buf = buf; 305644cb6abcSbmc nhdr->b_state = arc_anon; 3057fa9e4066Sahrens nhdr->b_arc_access = 0; 3058fa94a07fSbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 3059fa94a07fSbrendan nhdr->b_l2hdr = NULL; 3060ea8dc4b6Seschrock nhdr->b_datacnt = 1; 3061c717a561Smaybee nhdr->b_freeze_cksum = NULL; 3062fa9e4066Sahrens (void) refcount_add(&nhdr->b_refcnt, tag); 3063af2c4821Smaybee buf->b_hdr = nhdr; 30646f83844dSMark Maybee rw_exit(&buf->b_lock); 306544cb6abcSbmc atomic_add_64(&arc_anon->arcs_size, blksz); 3066fa9e4066Sahrens } else { 30676f83844dSMark Maybee rw_exit(&buf->b_lock); 3068ea8dc4b6Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3069fa9e4066Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 3070fa9e4066Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 307144cb6abcSbmc arc_change_state(arc_anon, hdr, hash_lock); 3072fa9e4066Sahrens hdr->b_arc_access = 0; 3073fa9e4066Sahrens mutex_exit(hash_lock); 3074fa94a07fSbrendan 3075fa9e4066Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 3076fa9e4066Sahrens hdr->b_birth = 0; 3077fa9e4066Sahrens hdr->b_cksum0 = 0; 3078c717a561Smaybee arc_buf_thaw(buf); 3079fa9e4066Sahrens } 3080ea8dc4b6Seschrock buf->b_efunc = NULL; 3081ea8dc4b6Seschrock buf->b_private = NULL; 3082fa94a07fSbrendan 30830a95608cSBrendan Gregg - Sun Microsystems out: 3084fa94a07fSbrendan if (l2hdr) { 3085fa94a07fSbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 3086fa94a07fSbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 3087fa94a07fSbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 3088fa94a07fSbrendan mutex_exit(&l2arc_buflist_mtx); 30896f83844dSMark Maybee } 3090fa9e4066Sahrens } 3091fa9e4066Sahrens 3092fa9e4066Sahrens int 3093fa9e4066Sahrens arc_released(arc_buf_t *buf) 3094fa9e4066Sahrens { 30956f83844dSMark Maybee int released; 30966f83844dSMark Maybee 30976f83844dSMark Maybee rw_enter(&buf->b_lock, RW_READER); 30986f83844dSMark Maybee released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 30996f83844dSMark Maybee rw_exit(&buf->b_lock); 31006f83844dSMark Maybee return (released); 3101ea8dc4b6Seschrock } 3102ea8dc4b6Seschrock 3103ea8dc4b6Seschrock int 3104ea8dc4b6Seschrock arc_has_callback(arc_buf_t *buf) 3105ea8dc4b6Seschrock { 31066f83844dSMark Maybee int callback; 31076f83844dSMark Maybee 31086f83844dSMark Maybee rw_enter(&buf->b_lock, RW_READER); 31096f83844dSMark Maybee callback = (buf->b_efunc != NULL); 31106f83844dSMark Maybee rw_exit(&buf->b_lock); 31116f83844dSMark Maybee return (callback); 3112fa9e4066Sahrens } 3113fa9e4066Sahrens 3114ea8dc4b6Seschrock #ifdef ZFS_DEBUG 3115ea8dc4b6Seschrock int 3116ea8dc4b6Seschrock arc_referenced(arc_buf_t *buf) 3117ea8dc4b6Seschrock { 31186f83844dSMark Maybee int referenced; 31196f83844dSMark Maybee 31206f83844dSMark Maybee rw_enter(&buf->b_lock, RW_READER); 31216f83844dSMark Maybee referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 31226f83844dSMark Maybee rw_exit(&buf->b_lock); 31236f83844dSMark Maybee return (referenced); 3124ea8dc4b6Seschrock } 3125ea8dc4b6Seschrock #endif 3126ea8dc4b6Seschrock 3127c717a561Smaybee static void 3128c717a561Smaybee arc_write_ready(zio_t *zio) 3129c717a561Smaybee { 3130c717a561Smaybee arc_write_callback_t *callback = zio->io_private; 3131c717a561Smaybee arc_buf_t *buf = callback->awcb_buf; 31320a4e9518Sgw arc_buf_hdr_t *hdr = buf->b_hdr; 3133c717a561Smaybee 3134e14bb325SJeff Bonwick ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 3135e14bb325SJeff Bonwick callback->awcb_ready(zio, buf, callback->awcb_private); 3136e14bb325SJeff Bonwick 31370a4e9518Sgw /* 31380a4e9518Sgw * If the IO is already in progress, then this is a re-write 3139e14bb325SJeff Bonwick * attempt, so we need to thaw and re-compute the cksum. 3140e14bb325SJeff Bonwick * It is the responsibility of the callback to handle the 3141e14bb325SJeff Bonwick * accounting for any re-write attempt. 31420a4e9518Sgw */ 31430a4e9518Sgw if (HDR_IO_IN_PROGRESS(hdr)) { 31440a4e9518Sgw mutex_enter(&hdr->b_freeze_lock); 31450a4e9518Sgw if (hdr->b_freeze_cksum != NULL) { 31460a4e9518Sgw kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 31470a4e9518Sgw hdr->b_freeze_cksum = NULL; 31480a4e9518Sgw } 31490a4e9518Sgw mutex_exit(&hdr->b_freeze_lock); 31500a4e9518Sgw } 3151fa94a07fSbrendan arc_cksum_compute(buf, B_FALSE); 31520a4e9518Sgw hdr->b_flags |= ARC_IO_IN_PROGRESS; 3153c717a561Smaybee } 3154c717a561Smaybee 3155fa9e4066Sahrens static void 3156fa9e4066Sahrens arc_write_done(zio_t *zio) 3157fa9e4066Sahrens { 3158c717a561Smaybee arc_write_callback_t *callback = zio->io_private; 3159c717a561Smaybee arc_buf_t *buf = callback->awcb_buf; 3160c717a561Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3161fa9e4066Sahrens 3162fa9e4066Sahrens hdr->b_acb = NULL; 3163fa9e4066Sahrens 3164fa9e4066Sahrens hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3165fa9e4066Sahrens hdr->b_birth = zio->io_bp->blk_birth; 3166fa9e4066Sahrens hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 3167ea8dc4b6Seschrock /* 3168ea8dc4b6Seschrock * If the block to be written was all-zero, we may have 3169ea8dc4b6Seschrock * compressed it away. In this case no write was performed 3170ea8dc4b6Seschrock * so there will be no dva/birth-date/checksum. The buffer 3171ea8dc4b6Seschrock * must therefor remain anonymous (and uncached). 3172ea8dc4b6Seschrock */ 3173fa9e4066Sahrens if (!BUF_EMPTY(hdr)) { 3174fa9e4066Sahrens arc_buf_hdr_t *exists; 3175fa9e4066Sahrens kmutex_t *hash_lock; 3176fa9e4066Sahrens 31776b4acc8bSahrens arc_cksum_verify(buf); 31786b4acc8bSahrens 3179fa9e4066Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3180fa9e4066Sahrens if (exists) { 3181fa9e4066Sahrens /* 3182fa9e4066Sahrens * This can only happen if we overwrite for 3183fa9e4066Sahrens * sync-to-convergence, because we remove 3184fa9e4066Sahrens * buffers from the hash table when we arc_free(). 3185fa9e4066Sahrens */ 3186ae46e4c7SMatthew Ahrens if (!(zio->io_flags & ZIO_FLAG_IO_REWRITE) || 3187ae46e4c7SMatthew Ahrens !DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig), 3188ae46e4c7SMatthew Ahrens BP_IDENTITY(zio->io_bp)) || 3189ae46e4c7SMatthew Ahrens zio->io_bp_orig.blk_birth != 3190ae46e4c7SMatthew Ahrens zio->io_bp->blk_birth) { 3191ae46e4c7SMatthew Ahrens panic("bad overwrite, hdr=%p exists=%p", 3192ae46e4c7SMatthew Ahrens (void *)hdr, (void *)exists); 3193ae46e4c7SMatthew Ahrens } 3194fa9e4066Sahrens 3195fa9e4066Sahrens ASSERT(refcount_is_zero(&exists->b_refcnt)); 319644cb6abcSbmc arc_change_state(arc_anon, exists, hash_lock); 3197fa9e4066Sahrens mutex_exit(hash_lock); 3198ea8dc4b6Seschrock arc_hdr_destroy(exists); 3199fa9e4066Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3200fa9e4066Sahrens ASSERT3P(exists, ==, NULL); 3201fa9e4066Sahrens } 3202ea8dc4b6Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3203088f3894Sahrens /* if it's not anon, we are doing a scrub */ 3204088f3894Sahrens if (hdr->b_state == arc_anon) 3205088f3894Sahrens arc_access(hdr, hash_lock); 320644eda4d7Smaybee mutex_exit(hash_lock); 3207c717a561Smaybee } else if (callback->awcb_done == NULL) { 3208ea8dc4b6Seschrock int destroy_hdr; 3209ea8dc4b6Seschrock /* 3210ea8dc4b6Seschrock * This is an anonymous buffer with no user callback, 3211ea8dc4b6Seschrock * destroy it if there are no active references. 3212ea8dc4b6Seschrock */ 3213ea8dc4b6Seschrock mutex_enter(&arc_eviction_mtx); 3214ea8dc4b6Seschrock destroy_hdr = refcount_is_zero(&hdr->b_refcnt); 3215ea8dc4b6Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3216ea8dc4b6Seschrock mutex_exit(&arc_eviction_mtx); 3217ea8dc4b6Seschrock if (destroy_hdr) 3218ea8dc4b6Seschrock arc_hdr_destroy(hdr); 3219ea8dc4b6Seschrock } else { 3220ea8dc4b6Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3221fa9e4066Sahrens } 3222088f3894Sahrens hdr->b_flags &= ~ARC_STORED; 3223ea8dc4b6Seschrock 3224c717a561Smaybee if (callback->awcb_done) { 3225fa9e4066Sahrens ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 3226c717a561Smaybee callback->awcb_done(zio, buf, callback->awcb_private); 3227fa9e4066Sahrens } 3228fa9e4066Sahrens 3229c717a561Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3230fa9e4066Sahrens } 3231fa9e4066Sahrens 323282c9918fSTim Haley void 3233e14bb325SJeff Bonwick write_policy(spa_t *spa, const writeprops_t *wp, zio_prop_t *zp) 3234088f3894Sahrens { 3235088f3894Sahrens boolean_t ismd = (wp->wp_level > 0 || dmu_ot[wp->wp_type].ot_metadata); 3236088f3894Sahrens 3237088f3894Sahrens /* Determine checksum setting */ 3238088f3894Sahrens if (ismd) { 3239088f3894Sahrens /* 3240088f3894Sahrens * Metadata always gets checksummed. If the data 3241088f3894Sahrens * checksum is multi-bit correctable, and it's not a 3242088f3894Sahrens * ZBT-style checksum, then it's suitable for metadata 3243088f3894Sahrens * as well. Otherwise, the metadata checksum defaults 3244088f3894Sahrens * to fletcher4. 3245088f3894Sahrens */ 3246088f3894Sahrens if (zio_checksum_table[wp->wp_oschecksum].ci_correctable && 3247088f3894Sahrens !zio_checksum_table[wp->wp_oschecksum].ci_zbt) 3248e14bb325SJeff Bonwick zp->zp_checksum = wp->wp_oschecksum; 3249088f3894Sahrens else 3250e14bb325SJeff Bonwick zp->zp_checksum = ZIO_CHECKSUM_FLETCHER_4; 3251088f3894Sahrens } else { 3252e14bb325SJeff Bonwick zp->zp_checksum = zio_checksum_select(wp->wp_dnchecksum, 3253088f3894Sahrens wp->wp_oschecksum); 3254088f3894Sahrens } 3255088f3894Sahrens 3256088f3894Sahrens /* Determine compression setting */ 3257088f3894Sahrens if (ismd) { 3258088f3894Sahrens /* 3259088f3894Sahrens * XXX -- we should design a compression algorithm 3260088f3894Sahrens * that specializes in arrays of bps. 3261088f3894Sahrens */ 3262e14bb325SJeff Bonwick zp->zp_compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY : 3263088f3894Sahrens ZIO_COMPRESS_LZJB; 3264088f3894Sahrens } else { 3265e14bb325SJeff Bonwick zp->zp_compress = zio_compress_select(wp->wp_dncompress, 3266088f3894Sahrens wp->wp_oscompress); 3267088f3894Sahrens } 3268e14bb325SJeff Bonwick 3269e14bb325SJeff Bonwick zp->zp_type = wp->wp_type; 3270e14bb325SJeff Bonwick zp->zp_level = wp->wp_level; 3271e14bb325SJeff Bonwick zp->zp_ndvas = MIN(wp->wp_copies + ismd, spa_max_replication(spa)); 3272088f3894Sahrens } 3273088f3894Sahrens 3274c717a561Smaybee zio_t * 3275088f3894Sahrens arc_write(zio_t *pio, spa_t *spa, const writeprops_t *wp, 32763baa08fcSek boolean_t l2arc, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 3277c717a561Smaybee arc_done_func_t *ready, arc_done_func_t *done, void *private, int priority, 32783baa08fcSek int zio_flags, const zbookmark_t *zb) 3279fa9e4066Sahrens { 3280fa9e4066Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 3281c717a561Smaybee arc_write_callback_t *callback; 3282e14bb325SJeff Bonwick zio_t *zio; 3283e14bb325SJeff Bonwick zio_prop_t zp; 3284fa9e4066Sahrens 3285e14bb325SJeff Bonwick ASSERT(ready != NULL); 3286fa9e4066Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 3287c5c6ffa0Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 3288c5c6ffa0Smaybee ASSERT(hdr->b_acb == 0); 32893baa08fcSek if (l2arc) 32903baa08fcSek hdr->b_flags |= ARC_L2CACHE; 3291c717a561Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 3292c717a561Smaybee callback->awcb_ready = ready; 3293c717a561Smaybee callback->awcb_done = done; 3294c717a561Smaybee callback->awcb_private = private; 3295c717a561Smaybee callback->awcb_buf = buf; 3296088f3894Sahrens 3297e14bb325SJeff Bonwick write_policy(spa, wp, &zp); 3298e14bb325SJeff Bonwick zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, &zp, 3299e14bb325SJeff Bonwick arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3300fa9e4066Sahrens 3301c717a561Smaybee return (zio); 3302fa9e4066Sahrens } 3303fa9e4066Sahrens 3304fa9e4066Sahrens int 3305fa9e4066Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 3306fa9e4066Sahrens zio_done_func_t *done, void *private, uint32_t arc_flags) 3307fa9e4066Sahrens { 3308fa9e4066Sahrens arc_buf_hdr_t *ab; 3309fa9e4066Sahrens kmutex_t *hash_lock; 3310fa9e4066Sahrens zio_t *zio; 3311ac05c741SMark Maybee uint64_t guid = spa_guid(spa); 3312fa9e4066Sahrens 3313fa9e4066Sahrens /* 3314fa9e4066Sahrens * If this buffer is in the cache, release it, so it 3315fa9e4066Sahrens * can be re-used. 3316fa9e4066Sahrens */ 3317ac05c741SMark Maybee ab = buf_hash_find(guid, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 3318fa9e4066Sahrens if (ab != NULL) { 3319fa9e4066Sahrens /* 3320fa9e4066Sahrens * The checksum of blocks to free is not always 3321fa9e4066Sahrens * preserved (eg. on the deadlist). However, if it is 3322fa9e4066Sahrens * nonzero, it should match what we have in the cache. 3323fa9e4066Sahrens */ 3324fa9e4066Sahrens ASSERT(bp->blk_cksum.zc_word[0] == 0 || 3325e14bb325SJeff Bonwick bp->blk_cksum.zc_word[0] == ab->b_cksum0 || 3326e14bb325SJeff Bonwick bp->blk_fill == BLK_FILL_ALREADY_FREED); 3327e14bb325SJeff Bonwick 332844cb6abcSbmc if (ab->b_state != arc_anon) 332944cb6abcSbmc arc_change_state(arc_anon, ab, hash_lock); 333013506d1eSmaybee if (HDR_IO_IN_PROGRESS(ab)) { 333113506d1eSmaybee /* 333213506d1eSmaybee * This should only happen when we prefetch. 333313506d1eSmaybee */ 333413506d1eSmaybee ASSERT(ab->b_flags & ARC_PREFETCH); 333513506d1eSmaybee ASSERT3U(ab->b_datacnt, ==, 1); 333613506d1eSmaybee ab->b_flags |= ARC_FREED_IN_READ; 333713506d1eSmaybee if (HDR_IN_HASH_TABLE(ab)) 333813506d1eSmaybee buf_hash_remove(ab); 333913506d1eSmaybee ab->b_arc_access = 0; 334013506d1eSmaybee bzero(&ab->b_dva, sizeof (dva_t)); 334113506d1eSmaybee ab->b_birth = 0; 334213506d1eSmaybee ab->b_cksum0 = 0; 334313506d1eSmaybee ab->b_buf->b_efunc = NULL; 334413506d1eSmaybee ab->b_buf->b_private = NULL; 334513506d1eSmaybee mutex_exit(hash_lock); 334613506d1eSmaybee } else if (refcount_is_zero(&ab->b_refcnt)) { 3347fa94a07fSbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3348fa9e4066Sahrens mutex_exit(hash_lock); 3349ea8dc4b6Seschrock arc_hdr_destroy(ab); 335044cb6abcSbmc ARCSTAT_BUMP(arcstat_deleted); 3351fa9e4066Sahrens } else { 3352bbf4a8dfSmaybee /* 335313506d1eSmaybee * We still have an active reference on this 335413506d1eSmaybee * buffer. This can happen, e.g., from 335513506d1eSmaybee * dbuf_unoverride(). 3356bbf4a8dfSmaybee */ 335713506d1eSmaybee ASSERT(!HDR_IN_HASH_TABLE(ab)); 3358fa9e4066Sahrens ab->b_arc_access = 0; 3359fa9e4066Sahrens bzero(&ab->b_dva, sizeof (dva_t)); 3360fa9e4066Sahrens ab->b_birth = 0; 3361fa9e4066Sahrens ab->b_cksum0 = 0; 3362ea8dc4b6Seschrock ab->b_buf->b_efunc = NULL; 3363ea8dc4b6Seschrock ab->b_buf->b_private = NULL; 3364fa9e4066Sahrens mutex_exit(hash_lock); 3365fa9e4066Sahrens } 3366fa9e4066Sahrens } 3367fa9e4066Sahrens 3368e14bb325SJeff Bonwick zio = zio_free(pio, spa, txg, bp, done, private, ZIO_FLAG_MUSTSUCCEED); 3369fa9e4066Sahrens 3370fa9e4066Sahrens if (arc_flags & ARC_WAIT) 3371fa9e4066Sahrens return (zio_wait(zio)); 3372fa9e4066Sahrens 3373fa9e4066Sahrens ASSERT(arc_flags & ARC_NOWAIT); 3374fa9e4066Sahrens zio_nowait(zio); 3375fa9e4066Sahrens 3376fa9e4066Sahrens return (0); 3377fa9e4066Sahrens } 3378fa9e4066Sahrens 33791ab7f2deSmaybee static int 33802fdbea25SAleksandr Guzovskiy arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg) 33811ab7f2deSmaybee { 33821ab7f2deSmaybee #ifdef _KERNEL 33831ab7f2deSmaybee uint64_t available_memory = ptob(freemem); 33841ab7f2deSmaybee static uint64_t page_load = 0; 33851ab7f2deSmaybee static uint64_t last_txg = 0; 33861ab7f2deSmaybee 33871ab7f2deSmaybee #if defined(__i386) 33881ab7f2deSmaybee available_memory = 33891ab7f2deSmaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 33901ab7f2deSmaybee #endif 33911ab7f2deSmaybee if (available_memory >= zfs_write_limit_max) 33921ab7f2deSmaybee return (0); 33931ab7f2deSmaybee 33941ab7f2deSmaybee if (txg > last_txg) { 33951ab7f2deSmaybee last_txg = txg; 33961ab7f2deSmaybee page_load = 0; 33971ab7f2deSmaybee } 33981ab7f2deSmaybee /* 33991ab7f2deSmaybee * If we are in pageout, we know that memory is already tight, 34001ab7f2deSmaybee * the arc is already going to be evicting, so we just want to 34011ab7f2deSmaybee * continue to let page writes occur as quickly as possible. 34021ab7f2deSmaybee */ 34031ab7f2deSmaybee if (curproc == proc_pageout) { 34041ab7f2deSmaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 34051ab7f2deSmaybee return (ERESTART); 34061ab7f2deSmaybee /* Note: reserve is inflated, so we deflate */ 34071ab7f2deSmaybee page_load += reserve / 8; 34081ab7f2deSmaybee return (0); 34091ab7f2deSmaybee } else if (page_load > 0 && arc_reclaim_needed()) { 34101ab7f2deSmaybee /* memory is low, delay before restarting */ 34111ab7f2deSmaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 34121ab7f2deSmaybee return (EAGAIN); 34131ab7f2deSmaybee } 34141ab7f2deSmaybee page_load = 0; 34151ab7f2deSmaybee 34161ab7f2deSmaybee if (arc_size > arc_c_min) { 34171ab7f2deSmaybee uint64_t evictable_memory = 34181ab7f2deSmaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 34191ab7f2deSmaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 34201ab7f2deSmaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 34211ab7f2deSmaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 34221ab7f2deSmaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 34231ab7f2deSmaybee } 34241ab7f2deSmaybee 34251ab7f2deSmaybee if (inflight_data > available_memory / 4) { 34261ab7f2deSmaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 34271ab7f2deSmaybee return (ERESTART); 34281ab7f2deSmaybee } 34291ab7f2deSmaybee #endif 34301ab7f2deSmaybee return (0); 34311ab7f2deSmaybee } 34321ab7f2deSmaybee 3433fa9e4066Sahrens void 34341ab7f2deSmaybee arc_tempreserve_clear(uint64_t reserve) 3435fa9e4066Sahrens { 34361ab7f2deSmaybee atomic_add_64(&arc_tempreserve, -reserve); 3437fa9e4066Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3438fa9e4066Sahrens } 3439fa9e4066Sahrens 3440fa9e4066Sahrens int 34411ab7f2deSmaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3442fa9e4066Sahrens { 34431ab7f2deSmaybee int error; 34442fdbea25SAleksandr Guzovskiy uint64_t anon_size; 34451ab7f2deSmaybee 3446fa9e4066Sahrens #ifdef ZFS_DEBUG 3447fa9e4066Sahrens /* 3448fa9e4066Sahrens * Once in a while, fail for no reason. Everything should cope. 3449fa9e4066Sahrens */ 3450fa9e4066Sahrens if (spa_get_random(10000) == 0) { 3451fa9e4066Sahrens dprintf("forcing random failure\n"); 3452fa9e4066Sahrens return (ERESTART); 3453fa9e4066Sahrens } 3454fa9e4066Sahrens #endif 34551ab7f2deSmaybee if (reserve > arc_c/4 && !arc_no_grow) 34561ab7f2deSmaybee arc_c = MIN(arc_c_max, reserve * 4); 34571ab7f2deSmaybee if (reserve > arc_c) 3458112fe045Smaybee return (ENOMEM); 3459112fe045Smaybee 34602fdbea25SAleksandr Guzovskiy /* 34612fdbea25SAleksandr Guzovskiy * Don't count loaned bufs as in flight dirty data to prevent long 34622fdbea25SAleksandr Guzovskiy * network delays from blocking transactions that are ready to be 34632fdbea25SAleksandr Guzovskiy * assigned to a txg. 34642fdbea25SAleksandr Guzovskiy */ 34652fdbea25SAleksandr Guzovskiy anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 34662fdbea25SAleksandr Guzovskiy 34671ab7f2deSmaybee /* 34681ab7f2deSmaybee * Writes will, almost always, require additional memory allocations 34691ab7f2deSmaybee * in order to compress/encrypt/etc the data. We therefor need to 34701ab7f2deSmaybee * make sure that there is sufficient available memory for this. 34711ab7f2deSmaybee */ 34722fdbea25SAleksandr Guzovskiy if (error = arc_memory_throttle(reserve, anon_size, txg)) 34731ab7f2deSmaybee return (error); 34741ab7f2deSmaybee 3475fa9e4066Sahrens /* 3476112fe045Smaybee * Throttle writes when the amount of dirty data in the cache 3477112fe045Smaybee * gets too large. We try to keep the cache less than half full 3478112fe045Smaybee * of dirty blocks so that our sync times don't grow too large. 3479112fe045Smaybee * Note: if two requests come in concurrently, we might let them 3480112fe045Smaybee * both succeed, when one of them should fail. Not a huge deal. 3481fa9e4066Sahrens */ 34822fdbea25SAleksandr Guzovskiy 34832fdbea25SAleksandr Guzovskiy if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 34842fdbea25SAleksandr Guzovskiy anon_size > arc_c / 4) { 34850e8c6158Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 34860e8c6158Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 34870e8c6158Smaybee arc_tempreserve>>10, 34880e8c6158Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 34890e8c6158Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 34901ab7f2deSmaybee reserve>>10, arc_c>>10); 3491fa9e4066Sahrens return (ERESTART); 3492fa9e4066Sahrens } 34931ab7f2deSmaybee atomic_add_64(&arc_tempreserve, reserve); 3494fa9e4066Sahrens return (0); 3495fa9e4066Sahrens } 3496fa9e4066Sahrens 3497fa9e4066Sahrens void 3498fa9e4066Sahrens arc_init(void) 3499fa9e4066Sahrens { 3500fa9e4066Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3501fa9e4066Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3502fa9e4066Sahrens 350313506d1eSmaybee /* Convert seconds to clock ticks */ 3504b19a79ecSperrin arc_min_prefetch_lifespan = 1 * hz; 350513506d1eSmaybee 3506fa9e4066Sahrens /* Start out with 1/8 of all memory */ 350744cb6abcSbmc arc_c = physmem * PAGESIZE / 8; 3508fa9e4066Sahrens 3509fa9e4066Sahrens #ifdef _KERNEL 3510fa9e4066Sahrens /* 3511fa9e4066Sahrens * On architectures where the physical memory can be larger 3512fa9e4066Sahrens * than the addressable space (intel in 32-bit mode), we may 3513fa9e4066Sahrens * need to limit the cache to 1/8 of VM size. 3514fa9e4066Sahrens */ 351544cb6abcSbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3516fa9e4066Sahrens #endif 3517fa9e4066Sahrens 3518112fe045Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 351944cb6abcSbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3520112fe045Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 352144cb6abcSbmc if (arc_c * 8 >= 1<<30) 352244cb6abcSbmc arc_c_max = (arc_c * 8) - (1<<30); 3523fa9e4066Sahrens else 352444cb6abcSbmc arc_c_max = arc_c_min; 352544cb6abcSbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 3526a2eea2e1Sahrens 3527a2eea2e1Sahrens /* 3528a2eea2e1Sahrens * Allow the tunables to override our calculations if they are 3529a2eea2e1Sahrens * reasonable (ie. over 64MB) 3530a2eea2e1Sahrens */ 3531a2eea2e1Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 353244cb6abcSbmc arc_c_max = zfs_arc_max; 353344cb6abcSbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 353444cb6abcSbmc arc_c_min = zfs_arc_min; 3535a2eea2e1Sahrens 353644cb6abcSbmc arc_c = arc_c_max; 353744cb6abcSbmc arc_p = (arc_c >> 1); 3538fa9e4066Sahrens 35390e8c6158Smaybee /* limit meta-data to 1/4 of the arc capacity */ 35400e8c6158Smaybee arc_meta_limit = arc_c_max / 4; 35411116048bSek 35421116048bSek /* Allow the tunable to override if it is reasonable */ 35431116048bSek if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 35441116048bSek arc_meta_limit = zfs_arc_meta_limit; 35451116048bSek 35460e8c6158Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 35470e8c6158Smaybee arc_c_min = arc_meta_limit / 2; 35480e8c6158Smaybee 35495a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_grow_retry > 0) 35505a98e54bSBrendan Gregg - Sun Microsystems arc_grow_retry = zfs_arc_grow_retry; 35515a98e54bSBrendan Gregg - Sun Microsystems 35525a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_shrink_shift > 0) 35535a98e54bSBrendan Gregg - Sun Microsystems arc_shrink_shift = zfs_arc_shrink_shift; 35545a98e54bSBrendan Gregg - Sun Microsystems 35555a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_p_min_shift > 0) 35565a98e54bSBrendan Gregg - Sun Microsystems arc_p_min_shift = zfs_arc_p_min_shift; 35575a98e54bSBrendan Gregg - Sun Microsystems 3558fa9e4066Sahrens /* if kmem_flags are set, lets try to use less memory */ 3559fa9e4066Sahrens if (kmem_debugging()) 356044cb6abcSbmc arc_c = arc_c / 2; 356144cb6abcSbmc if (arc_c < arc_c_min) 356244cb6abcSbmc arc_c = arc_c_min; 356344cb6abcSbmc 356444cb6abcSbmc arc_anon = &ARC_anon; 356544cb6abcSbmc arc_mru = &ARC_mru; 356644cb6abcSbmc arc_mru_ghost = &ARC_mru_ghost; 356744cb6abcSbmc arc_mfu = &ARC_mfu; 356844cb6abcSbmc arc_mfu_ghost = &ARC_mfu_ghost; 3569fa94a07fSbrendan arc_l2c_only = &ARC_l2c_only; 357044cb6abcSbmc arc_size = 0; 357144cb6abcSbmc 357244cb6abcSbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 357344cb6abcSbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 357444cb6abcSbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 357544cb6abcSbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 357644cb6abcSbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 3577fa94a07fSbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 357844cb6abcSbmc 35790e8c6158Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 35800e8c6158Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35810e8c6158Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 35820e8c6158Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35830e8c6158Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 35840e8c6158Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35850e8c6158Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 35860e8c6158Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35870e8c6158Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 35880e8c6158Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35890e8c6158Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 35900e8c6158Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35910e8c6158Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 35920e8c6158Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 35930e8c6158Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 35940e8c6158Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3595fa94a07fSbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 3596fa94a07fSbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3597fa94a07fSbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 3598fa94a07fSbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3599fa9e4066Sahrens 3600fa9e4066Sahrens buf_init(); 3601fa9e4066Sahrens 3602fa9e4066Sahrens arc_thread_exit = 0; 3603ea8dc4b6Seschrock arc_eviction_list = NULL; 3604ea8dc4b6Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 360540d7d650Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3606fa9e4066Sahrens 360744cb6abcSbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 360844cb6abcSbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 360944cb6abcSbmc 361044cb6abcSbmc if (arc_ksp != NULL) { 361144cb6abcSbmc arc_ksp->ks_data = &arc_stats; 361244cb6abcSbmc kstat_install(arc_ksp); 361344cb6abcSbmc } 361444cb6abcSbmc 3615fa9e4066Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3616fa9e4066Sahrens TS_RUN, minclsyspri); 361749e3519aSmaybee 361849e3519aSmaybee arc_dead = FALSE; 36193a737e0dSbrendan arc_warm = B_FALSE; 36201ab7f2deSmaybee 36211ab7f2deSmaybee if (zfs_write_limit_max == 0) 362205715f94SMark Maybee zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 36231ab7f2deSmaybee else 36241ab7f2deSmaybee zfs_write_limit_shift = 0; 362505715f94SMark Maybee mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3626fa9e4066Sahrens } 3627fa9e4066Sahrens 3628fa9e4066Sahrens void 3629fa9e4066Sahrens arc_fini(void) 3630fa9e4066Sahrens { 3631fa9e4066Sahrens mutex_enter(&arc_reclaim_thr_lock); 3632fa9e4066Sahrens arc_thread_exit = 1; 3633fa9e4066Sahrens while (arc_thread_exit != 0) 3634fa9e4066Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3635fa9e4066Sahrens mutex_exit(&arc_reclaim_thr_lock); 3636fa9e4066Sahrens 3637874395d5Smaybee arc_flush(NULL); 3638fa9e4066Sahrens 3639fa9e4066Sahrens arc_dead = TRUE; 3640fa9e4066Sahrens 364144cb6abcSbmc if (arc_ksp != NULL) { 364244cb6abcSbmc kstat_delete(arc_ksp); 364344cb6abcSbmc arc_ksp = NULL; 364444cb6abcSbmc } 364544cb6abcSbmc 3646ea8dc4b6Seschrock mutex_destroy(&arc_eviction_mtx); 3647fa9e4066Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3648fa9e4066Sahrens cv_destroy(&arc_reclaim_thr_cv); 3649fa9e4066Sahrens 36500e8c6158Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 36510e8c6158Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 36520e8c6158Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 36530e8c6158Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 36540e8c6158Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 36550e8c6158Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 36560e8c6158Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 36570e8c6158Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3658fa9e4066Sahrens 365944cb6abcSbmc mutex_destroy(&arc_anon->arcs_mtx); 366044cb6abcSbmc mutex_destroy(&arc_mru->arcs_mtx); 366144cb6abcSbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 366244cb6abcSbmc mutex_destroy(&arc_mfu->arcs_mtx); 366344cb6abcSbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 3664b5e70f97SRicardo M. Correia mutex_destroy(&arc_l2c_only->arcs_mtx); 36655ad82045Snd 366605715f94SMark Maybee mutex_destroy(&zfs_write_limit_lock); 366705715f94SMark Maybee 3668fa9e4066Sahrens buf_fini(); 36692fdbea25SAleksandr Guzovskiy 36702fdbea25SAleksandr Guzovskiy ASSERT(arc_loaned_bytes == 0); 3671fa9e4066Sahrens } 3672fa94a07fSbrendan 3673fa94a07fSbrendan /* 3674fa94a07fSbrendan * Level 2 ARC 3675fa94a07fSbrendan * 3676fa94a07fSbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 3677fa94a07fSbrendan * It uses dedicated storage devices to hold cached data, which are populated 3678fa94a07fSbrendan * using large infrequent writes. The main role of this cache is to boost 3679fa94a07fSbrendan * the performance of random read workloads. The intended L2ARC devices 3680fa94a07fSbrendan * include short-stroked disks, solid state disks, and other media with 3681fa94a07fSbrendan * substantially faster read latency than disk. 3682fa94a07fSbrendan * 3683fa94a07fSbrendan * +-----------------------+ 3684fa94a07fSbrendan * | ARC | 3685fa94a07fSbrendan * +-----------------------+ 3686fa94a07fSbrendan * | ^ ^ 3687fa94a07fSbrendan * | | | 3688fa94a07fSbrendan * l2arc_feed_thread() arc_read() 3689fa94a07fSbrendan * | | | 3690fa94a07fSbrendan * | l2arc read | 3691fa94a07fSbrendan * V | | 3692fa94a07fSbrendan * +---------------+ | 3693fa94a07fSbrendan * | L2ARC | | 3694fa94a07fSbrendan * +---------------+ | 3695fa94a07fSbrendan * | ^ | 3696fa94a07fSbrendan * l2arc_write() | | 3697fa94a07fSbrendan * | | | 3698fa94a07fSbrendan * V | | 3699fa94a07fSbrendan * +-------+ +-------+ 3700fa94a07fSbrendan * | vdev | | vdev | 3701fa94a07fSbrendan * | cache | | cache | 3702fa94a07fSbrendan * +-------+ +-------+ 3703fa94a07fSbrendan * +=========+ .-----. 3704fa94a07fSbrendan * : L2ARC : |-_____-| 3705fa94a07fSbrendan * : devices : | Disks | 3706fa94a07fSbrendan * +=========+ `-_____-' 3707fa94a07fSbrendan * 3708fa94a07fSbrendan * Read requests are satisfied from the following sources, in order: 3709fa94a07fSbrendan * 3710fa94a07fSbrendan * 1) ARC 3711fa94a07fSbrendan * 2) vdev cache of L2ARC devices 3712fa94a07fSbrendan * 3) L2ARC devices 3713fa94a07fSbrendan * 4) vdev cache of disks 3714fa94a07fSbrendan * 5) disks 3715fa94a07fSbrendan * 3716fa94a07fSbrendan * Some L2ARC device types exhibit extremely slow write performance. 3717fa94a07fSbrendan * To accommodate for this there are some significant differences between 3718fa94a07fSbrendan * the L2ARC and traditional cache design: 3719fa94a07fSbrendan * 3720fa94a07fSbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 3721fa94a07fSbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 3722fa94a07fSbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 3723fa94a07fSbrendan * this would add inflated write latencies for all ARC memory pressure. 3724fa94a07fSbrendan * 3725fa94a07fSbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 3726fa94a07fSbrendan * It does this by periodically scanning buffers from the eviction-end of 3727fa94a07fSbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 3728fa94a07fSbrendan * not already there. It scans until a headroom of buffers is satisfied, 3729fa94a07fSbrendan * which itself is a buffer for ARC eviction. The thread that does this is 3730fa94a07fSbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 3731fa94a07fSbrendan * provide a better sense of ratio than this diagram: 3732fa94a07fSbrendan * 3733fa94a07fSbrendan * head --> tail 3734fa94a07fSbrendan * +---------------------+----------+ 3735fa94a07fSbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 3736fa94a07fSbrendan * +---------------------+----------+ | o L2ARC eligible 3737fa94a07fSbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 3738fa94a07fSbrendan * +---------------------+----------+ | 3739fa94a07fSbrendan * 15.9 Gbytes ^ 32 Mbytes | 3740fa94a07fSbrendan * headroom | 3741fa94a07fSbrendan * l2arc_feed_thread() 3742fa94a07fSbrendan * | 3743fa94a07fSbrendan * l2arc write hand <--[oooo]--' 3744fa94a07fSbrendan * | 8 Mbyte 3745fa94a07fSbrendan * | write max 3746fa94a07fSbrendan * V 3747fa94a07fSbrendan * +==============================+ 3748fa94a07fSbrendan * L2ARC dev |####|#|###|###| |####| ... | 3749fa94a07fSbrendan * +==============================+ 3750fa94a07fSbrendan * 32 Gbytes 3751fa94a07fSbrendan * 3752fa94a07fSbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 3753fa94a07fSbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 3754fa94a07fSbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 3755fa94a07fSbrendan * safe to say that this is an uncommon case, since buffers at the end of 3756fa94a07fSbrendan * the ARC lists have moved there due to inactivity. 3757fa94a07fSbrendan * 3758fa94a07fSbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 3759fa94a07fSbrendan * then the L2ARC simply misses copying some buffers. This serves as a 3760fa94a07fSbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 3761fa94a07fSbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 3762fa94a07fSbrendan * the potential for the L2ARC to churn if it attempts to cache content too 3763fa94a07fSbrendan * quickly, such as during backups of the entire pool. 3764fa94a07fSbrendan * 37653a737e0dSbrendan * 5. After system boot and before the ARC has filled main memory, there are 37663a737e0dSbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 37673a737e0dSbrendan * lists can remain mostly static. Instead of searching from tail of these 37683a737e0dSbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 37693a737e0dSbrendan * for eligible buffers, greatly increasing its chance of finding them. 37703a737e0dSbrendan * 37713a737e0dSbrendan * The L2ARC device write speed is also boosted during this time so that 37723a737e0dSbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 37733a737e0dSbrendan * there are no L2ARC reads, and no fear of degrading read performance 37743a737e0dSbrendan * through increased writes. 37753a737e0dSbrendan * 37763a737e0dSbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 3777fa94a07fSbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 3778fa94a07fSbrendan * device is written to in a rotor fashion, sweeping writes through 3779fa94a07fSbrendan * available space then repeating. 3780fa94a07fSbrendan * 37813a737e0dSbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 3782fa94a07fSbrendan * write buffers back to disk based storage. 3783fa94a07fSbrendan * 37843a737e0dSbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 3785fa94a07fSbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 3786fa94a07fSbrendan * 3787fa94a07fSbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 3788fa94a07fSbrendan * may be necessary for different workloads: 3789fa94a07fSbrendan * 3790fa94a07fSbrendan * l2arc_write_max max write bytes per interval 37913a737e0dSbrendan * l2arc_write_boost extra write bytes during device warmup 3792fa94a07fSbrendan * l2arc_noprefetch skip caching prefetched buffers 3793fa94a07fSbrendan * l2arc_headroom number of max device writes to precache 3794fa94a07fSbrendan * l2arc_feed_secs seconds between L2ARC writing 3795fa94a07fSbrendan * 3796fa94a07fSbrendan * Tunables may be removed or added as future performance improvements are 3797fa94a07fSbrendan * integrated, and also may become zpool properties. 37985a98e54bSBrendan Gregg - Sun Microsystems * 37995a98e54bSBrendan Gregg - Sun Microsystems * There are three key functions that control how the L2ARC warms up: 38005a98e54bSBrendan Gregg - Sun Microsystems * 38015a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_eligible() check if a buffer is eligible to cache 38025a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_size() calculate how much to write 38035a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_interval() calculate sleep delay between writes 38045a98e54bSBrendan Gregg - Sun Microsystems * 38055a98e54bSBrendan Gregg - Sun Microsystems * These three functions determine what to write, how much, and how quickly 38065a98e54bSBrendan Gregg - Sun Microsystems * to send writes. 3807fa94a07fSbrendan */ 3808fa94a07fSbrendan 38095a98e54bSBrendan Gregg - Sun Microsystems static boolean_t 3810ac05c741SMark Maybee l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 38115a98e54bSBrendan Gregg - Sun Microsystems { 38125a98e54bSBrendan Gregg - Sun Microsystems /* 38135a98e54bSBrendan Gregg - Sun Microsystems * A buffer is *not* eligible for the L2ARC if it: 38145a98e54bSBrendan Gregg - Sun Microsystems * 1. belongs to a different spa. 3815*5ea40c06SBrendan Gregg - Sun Microsystems * 2. is already cached on the L2ARC. 3816*5ea40c06SBrendan Gregg - Sun Microsystems * 3. has an I/O in progress (it may be an incomplete read). 3817*5ea40c06SBrendan Gregg - Sun Microsystems * 4. is flagged not eligible (zfs property). 38185a98e54bSBrendan Gregg - Sun Microsystems */ 3819*5ea40c06SBrendan Gregg - Sun Microsystems if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL || 38205a98e54bSBrendan Gregg - Sun Microsystems HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) 38215a98e54bSBrendan Gregg - Sun Microsystems return (B_FALSE); 38225a98e54bSBrendan Gregg - Sun Microsystems 38235a98e54bSBrendan Gregg - Sun Microsystems return (B_TRUE); 38245a98e54bSBrendan Gregg - Sun Microsystems } 38255a98e54bSBrendan Gregg - Sun Microsystems 38265a98e54bSBrendan Gregg - Sun Microsystems static uint64_t 38275a98e54bSBrendan Gregg - Sun Microsystems l2arc_write_size(l2arc_dev_t *dev) 38285a98e54bSBrendan Gregg - Sun Microsystems { 38295a98e54bSBrendan Gregg - Sun Microsystems uint64_t size; 38305a98e54bSBrendan Gregg - Sun Microsystems 38315a98e54bSBrendan Gregg - Sun Microsystems size = dev->l2ad_write; 38325a98e54bSBrendan Gregg - Sun Microsystems 38335a98e54bSBrendan Gregg - Sun Microsystems if (arc_warm == B_FALSE) 38345a98e54bSBrendan Gregg - Sun Microsystems size += dev->l2ad_boost; 38355a98e54bSBrendan Gregg - Sun Microsystems 38365a98e54bSBrendan Gregg - Sun Microsystems return (size); 38375a98e54bSBrendan Gregg - Sun Microsystems 38385a98e54bSBrendan Gregg - Sun Microsystems } 38395a98e54bSBrendan Gregg - Sun Microsystems 38405a98e54bSBrendan Gregg - Sun Microsystems static clock_t 38415a98e54bSBrendan Gregg - Sun Microsystems l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 38425a98e54bSBrendan Gregg - Sun Microsystems { 38435a98e54bSBrendan Gregg - Sun Microsystems clock_t interval, next; 38445a98e54bSBrendan Gregg - Sun Microsystems 38455a98e54bSBrendan Gregg - Sun Microsystems /* 38465a98e54bSBrendan Gregg - Sun Microsystems * If the ARC lists are busy, increase our write rate; if the 38475a98e54bSBrendan Gregg - Sun Microsystems * lists are stale, idle back. This is achieved by checking 38485a98e54bSBrendan Gregg - Sun Microsystems * how much we previously wrote - if it was more than half of 38495a98e54bSBrendan Gregg - Sun Microsystems * what we wanted, schedule the next write much sooner. 38505a98e54bSBrendan Gregg - Sun Microsystems */ 38515a98e54bSBrendan Gregg - Sun Microsystems if (l2arc_feed_again && wrote > (wanted / 2)) 38525a98e54bSBrendan Gregg - Sun Microsystems interval = (hz * l2arc_feed_min_ms) / 1000; 38535a98e54bSBrendan Gregg - Sun Microsystems else 38545a98e54bSBrendan Gregg - Sun Microsystems interval = hz * l2arc_feed_secs; 38555a98e54bSBrendan Gregg - Sun Microsystems 38565a98e54bSBrendan Gregg - Sun Microsystems next = MAX(lbolt, MIN(lbolt + interval, began + interval)); 38575a98e54bSBrendan Gregg - Sun Microsystems 38585a98e54bSBrendan Gregg - Sun Microsystems return (next); 38595a98e54bSBrendan Gregg - Sun Microsystems } 38605a98e54bSBrendan Gregg - Sun Microsystems 3861fa94a07fSbrendan static void 3862fa94a07fSbrendan l2arc_hdr_stat_add(void) 3863fa94a07fSbrendan { 3864e6c728e1Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 3865e6c728e1Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 3866fa94a07fSbrendan } 3867fa94a07fSbrendan 3868fa94a07fSbrendan static void 3869fa94a07fSbrendan l2arc_hdr_stat_remove(void) 3870fa94a07fSbrendan { 3871e6c728e1Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 3872e6c728e1Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 3873fa94a07fSbrendan } 3874fa94a07fSbrendan 3875fa94a07fSbrendan /* 3876fa94a07fSbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 38773a737e0dSbrendan * If a device is returned, this also returns holding the spa config lock. 3878fa94a07fSbrendan */ 3879fa94a07fSbrendan static l2arc_dev_t * 3880fa94a07fSbrendan l2arc_dev_get_next(void) 3881fa94a07fSbrendan { 38823a737e0dSbrendan l2arc_dev_t *first, *next = NULL; 38833a737e0dSbrendan 38843a737e0dSbrendan /* 38853a737e0dSbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 38863a737e0dSbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 38873a737e0dSbrendan * both locks will be dropped and a spa config lock held instead. 38883a737e0dSbrendan */ 38893a737e0dSbrendan mutex_enter(&spa_namespace_lock); 38903a737e0dSbrendan mutex_enter(&l2arc_dev_mtx); 3891fa94a07fSbrendan 3892c5904d13Seschrock /* if there are no vdevs, there is nothing to do */ 3893c5904d13Seschrock if (l2arc_ndev == 0) 38943a737e0dSbrendan goto out; 3895c5904d13Seschrock 3896c5904d13Seschrock first = NULL; 3897c5904d13Seschrock next = l2arc_dev_last; 3898c5904d13Seschrock do { 3899c5904d13Seschrock /* loop around the list looking for a non-faulted vdev */ 3900c5904d13Seschrock if (next == NULL) { 3901fa94a07fSbrendan next = list_head(l2arc_dev_list); 3902c5904d13Seschrock } else { 3903c5904d13Seschrock next = list_next(l2arc_dev_list, next); 3904c5904d13Seschrock if (next == NULL) 3905c5904d13Seschrock next = list_head(l2arc_dev_list); 3906c5904d13Seschrock } 3907c5904d13Seschrock 3908c5904d13Seschrock /* if we have come back to the start, bail out */ 3909c5904d13Seschrock if (first == NULL) 3910c5904d13Seschrock first = next; 3911c5904d13Seschrock else if (next == first) 3912c5904d13Seschrock break; 3913c5904d13Seschrock 3914c5904d13Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 3915c5904d13Seschrock 3916c5904d13Seschrock /* if we were unable to find any usable vdevs, return NULL */ 3917c5904d13Seschrock if (vdev_is_dead(next->l2ad_vdev)) 39183a737e0dSbrendan next = NULL; 3919fa94a07fSbrendan 3920fa94a07fSbrendan l2arc_dev_last = next; 3921fa94a07fSbrendan 39223a737e0dSbrendan out: 39233a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 39243a737e0dSbrendan 39253a737e0dSbrendan /* 39263a737e0dSbrendan * Grab the config lock to prevent the 'next' device from being 39273a737e0dSbrendan * removed while we are writing to it. 39283a737e0dSbrendan */ 39293a737e0dSbrendan if (next != NULL) 3930e14bb325SJeff Bonwick spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 39313a737e0dSbrendan mutex_exit(&spa_namespace_lock); 39323a737e0dSbrendan 3933fa94a07fSbrendan return (next); 3934fa94a07fSbrendan } 3935fa94a07fSbrendan 39363a737e0dSbrendan /* 39373a737e0dSbrendan * Free buffers that were tagged for destruction. 39383a737e0dSbrendan */ 39393a737e0dSbrendan static void 39403a737e0dSbrendan l2arc_do_free_on_write() 39413a737e0dSbrendan { 39423a737e0dSbrendan list_t *buflist; 39433a737e0dSbrendan l2arc_data_free_t *df, *df_prev; 39443a737e0dSbrendan 39453a737e0dSbrendan mutex_enter(&l2arc_free_on_write_mtx); 39463a737e0dSbrendan buflist = l2arc_free_on_write; 39473a737e0dSbrendan 39483a737e0dSbrendan for (df = list_tail(buflist); df; df = df_prev) { 39493a737e0dSbrendan df_prev = list_prev(buflist, df); 39503a737e0dSbrendan ASSERT(df->l2df_data != NULL); 39513a737e0dSbrendan ASSERT(df->l2df_func != NULL); 39523a737e0dSbrendan df->l2df_func(df->l2df_data, df->l2df_size); 39533a737e0dSbrendan list_remove(buflist, df); 39543a737e0dSbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 39553a737e0dSbrendan } 39563a737e0dSbrendan 39573a737e0dSbrendan mutex_exit(&l2arc_free_on_write_mtx); 39583a737e0dSbrendan } 39593a737e0dSbrendan 3960fa94a07fSbrendan /* 3961fa94a07fSbrendan * A write to a cache device has completed. Update all headers to allow 3962fa94a07fSbrendan * reads from these buffers to begin. 3963fa94a07fSbrendan */ 3964fa94a07fSbrendan static void 3965fa94a07fSbrendan l2arc_write_done(zio_t *zio) 3966fa94a07fSbrendan { 3967fa94a07fSbrendan l2arc_write_callback_t *cb; 3968fa94a07fSbrendan l2arc_dev_t *dev; 3969fa94a07fSbrendan list_t *buflist; 3970fa94a07fSbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 39713a737e0dSbrendan l2arc_buf_hdr_t *abl2; 3972fa94a07fSbrendan kmutex_t *hash_lock; 3973fa94a07fSbrendan 3974fa94a07fSbrendan cb = zio->io_private; 3975fa94a07fSbrendan ASSERT(cb != NULL); 3976fa94a07fSbrendan dev = cb->l2wcb_dev; 3977fa94a07fSbrendan ASSERT(dev != NULL); 3978fa94a07fSbrendan head = cb->l2wcb_head; 3979fa94a07fSbrendan ASSERT(head != NULL); 3980fa94a07fSbrendan buflist = dev->l2ad_buflist; 3981fa94a07fSbrendan ASSERT(buflist != NULL); 3982fa94a07fSbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 3983fa94a07fSbrendan l2arc_write_callback_t *, cb); 3984fa94a07fSbrendan 3985fa94a07fSbrendan if (zio->io_error != 0) 3986fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 3987fa94a07fSbrendan 3988fa94a07fSbrendan mutex_enter(&l2arc_buflist_mtx); 3989fa94a07fSbrendan 3990fa94a07fSbrendan /* 3991fa94a07fSbrendan * All writes completed, or an error was hit. 3992fa94a07fSbrendan */ 3993fa94a07fSbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 3994fa94a07fSbrendan ab_prev = list_prev(buflist, ab); 3995fa94a07fSbrendan 3996fa94a07fSbrendan hash_lock = HDR_LOCK(ab); 3997fa94a07fSbrendan if (!mutex_tryenter(hash_lock)) { 3998fa94a07fSbrendan /* 3999fa94a07fSbrendan * This buffer misses out. It may be in a stage 4000fa94a07fSbrendan * of eviction. Its ARC_L2_WRITING flag will be 4001fa94a07fSbrendan * left set, denying reads to this buffer. 4002fa94a07fSbrendan */ 4003fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 4004fa94a07fSbrendan continue; 4005fa94a07fSbrendan } 4006fa94a07fSbrendan 4007fa94a07fSbrendan if (zio->io_error != 0) { 4008fa94a07fSbrendan /* 40093a737e0dSbrendan * Error - drop L2ARC entry. 4010fa94a07fSbrendan */ 40113a737e0dSbrendan list_remove(buflist, ab); 40123a737e0dSbrendan abl2 = ab->b_l2hdr; 4013fa94a07fSbrendan ab->b_l2hdr = NULL; 40143a737e0dSbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 40153a737e0dSbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4016fa94a07fSbrendan } 4017fa94a07fSbrendan 4018fa94a07fSbrendan /* 4019fa94a07fSbrendan * Allow ARC to begin reads to this L2ARC entry. 4020fa94a07fSbrendan */ 4021fa94a07fSbrendan ab->b_flags &= ~ARC_L2_WRITING; 4022fa94a07fSbrendan 4023fa94a07fSbrendan mutex_exit(hash_lock); 4024fa94a07fSbrendan } 4025fa94a07fSbrendan 4026fa94a07fSbrendan atomic_inc_64(&l2arc_writes_done); 4027fa94a07fSbrendan list_remove(buflist, head); 4028fa94a07fSbrendan kmem_cache_free(hdr_cache, head); 4029fa94a07fSbrendan mutex_exit(&l2arc_buflist_mtx); 4030fa94a07fSbrendan 40313a737e0dSbrendan l2arc_do_free_on_write(); 4032fa94a07fSbrendan 4033fa94a07fSbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 4034fa94a07fSbrendan } 4035fa94a07fSbrendan 4036fa94a07fSbrendan /* 4037fa94a07fSbrendan * A read to a cache device completed. Validate buffer contents before 4038fa94a07fSbrendan * handing over to the regular ARC routines. 4039fa94a07fSbrendan */ 4040fa94a07fSbrendan static void 4041fa94a07fSbrendan l2arc_read_done(zio_t *zio) 4042fa94a07fSbrendan { 4043fa94a07fSbrendan l2arc_read_callback_t *cb; 4044fa94a07fSbrendan arc_buf_hdr_t *hdr; 4045fa94a07fSbrendan arc_buf_t *buf; 4046fa94a07fSbrendan kmutex_t *hash_lock; 40473a737e0dSbrendan int equal; 4048fa94a07fSbrendan 4049e14bb325SJeff Bonwick ASSERT(zio->io_vd != NULL); 4050e14bb325SJeff Bonwick ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 4051e14bb325SJeff Bonwick 4052e14bb325SJeff Bonwick spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 4053e14bb325SJeff Bonwick 4054fa94a07fSbrendan cb = zio->io_private; 4055fa94a07fSbrendan ASSERT(cb != NULL); 4056fa94a07fSbrendan buf = cb->l2rcb_buf; 4057fa94a07fSbrendan ASSERT(buf != NULL); 4058fa94a07fSbrendan hdr = buf->b_hdr; 4059fa94a07fSbrendan ASSERT(hdr != NULL); 4060fa94a07fSbrendan 4061fa94a07fSbrendan hash_lock = HDR_LOCK(hdr); 4062fa94a07fSbrendan mutex_enter(hash_lock); 4063fa94a07fSbrendan 4064fa94a07fSbrendan /* 4065fa94a07fSbrendan * Check this survived the L2ARC journey. 4066fa94a07fSbrendan */ 4067fa94a07fSbrendan equal = arc_cksum_equal(buf); 4068fa94a07fSbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 4069fa94a07fSbrendan mutex_exit(hash_lock); 4070fa94a07fSbrendan zio->io_private = buf; 4071e14bb325SJeff Bonwick zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 4072e14bb325SJeff Bonwick zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 4073fa94a07fSbrendan arc_read_done(zio); 4074fa94a07fSbrendan } else { 4075fa94a07fSbrendan mutex_exit(hash_lock); 4076fa94a07fSbrendan /* 4077fa94a07fSbrendan * Buffer didn't survive caching. Increment stats and 4078fa94a07fSbrendan * reissue to the original storage device. 4079fa94a07fSbrendan */ 40803a737e0dSbrendan if (zio->io_error != 0) { 4081fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 40823a737e0dSbrendan } else { 40833a737e0dSbrendan zio->io_error = EIO; 40843a737e0dSbrendan } 4085fa94a07fSbrendan if (!equal) 4086fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 4087fa94a07fSbrendan 4088e14bb325SJeff Bonwick /* 4089e14bb325SJeff Bonwick * If there's no waiter, issue an async i/o to the primary 4090e14bb325SJeff Bonwick * storage now. If there *is* a waiter, the caller must 4091e14bb325SJeff Bonwick * issue the i/o in a context where it's OK to block. 4092e14bb325SJeff Bonwick */ 4093a3f829aeSBill Moore if (zio->io_waiter == NULL) { 4094a3f829aeSBill Moore zio_t *pio = zio_unique_parent(zio); 4095a3f829aeSBill Moore 4096a3f829aeSBill Moore ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 4097a3f829aeSBill Moore 4098a3f829aeSBill Moore zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 4099e14bb325SJeff Bonwick buf->b_data, zio->io_size, arc_read_done, buf, 4100e14bb325SJeff Bonwick zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 4101a3f829aeSBill Moore } 4102fa94a07fSbrendan } 4103fa94a07fSbrendan 4104fa94a07fSbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 4105fa94a07fSbrendan } 4106fa94a07fSbrendan 4107fa94a07fSbrendan /* 4108fa94a07fSbrendan * This is the list priority from which the L2ARC will search for pages to 4109fa94a07fSbrendan * cache. This is used within loops (0..3) to cycle through lists in the 4110fa94a07fSbrendan * desired order. This order can have a significant effect on cache 4111fa94a07fSbrendan * performance. 4112fa94a07fSbrendan * 4113fa94a07fSbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 4114fa94a07fSbrendan * the data lists. This function returns a locked list, and also returns 4115fa94a07fSbrendan * the lock pointer. 4116fa94a07fSbrendan */ 4117fa94a07fSbrendan static list_t * 4118fa94a07fSbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 4119fa94a07fSbrendan { 4120fa94a07fSbrendan list_t *list; 4121fa94a07fSbrendan 4122fa94a07fSbrendan ASSERT(list_num >= 0 && list_num <= 3); 4123fa94a07fSbrendan 4124fa94a07fSbrendan switch (list_num) { 4125fa94a07fSbrendan case 0: 4126fa94a07fSbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 4127fa94a07fSbrendan *lock = &arc_mfu->arcs_mtx; 4128fa94a07fSbrendan break; 4129fa94a07fSbrendan case 1: 4130fa94a07fSbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 4131fa94a07fSbrendan *lock = &arc_mru->arcs_mtx; 4132fa94a07fSbrendan break; 4133fa94a07fSbrendan case 2: 4134fa94a07fSbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 4135fa94a07fSbrendan *lock = &arc_mfu->arcs_mtx; 4136fa94a07fSbrendan break; 4137fa94a07fSbrendan case 3: 4138fa94a07fSbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 4139fa94a07fSbrendan *lock = &arc_mru->arcs_mtx; 4140fa94a07fSbrendan break; 4141fa94a07fSbrendan } 4142fa94a07fSbrendan 4143fa94a07fSbrendan ASSERT(!(MUTEX_HELD(*lock))); 4144fa94a07fSbrendan mutex_enter(*lock); 4145fa94a07fSbrendan return (list); 4146fa94a07fSbrendan } 4147fa94a07fSbrendan 4148fa94a07fSbrendan /* 4149fa94a07fSbrendan * Evict buffers from the device write hand to the distance specified in 4150fa94a07fSbrendan * bytes. This distance may span populated buffers, it may span nothing. 4151fa94a07fSbrendan * This is clearing a region on the L2ARC device ready for writing. 4152fa94a07fSbrendan * If the 'all' boolean is set, every buffer is evicted. 4153fa94a07fSbrendan */ 4154fa94a07fSbrendan static void 4155fa94a07fSbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 4156fa94a07fSbrendan { 4157fa94a07fSbrendan list_t *buflist; 4158fa94a07fSbrendan l2arc_buf_hdr_t *abl2; 4159fa94a07fSbrendan arc_buf_hdr_t *ab, *ab_prev; 4160fa94a07fSbrendan kmutex_t *hash_lock; 4161fa94a07fSbrendan uint64_t taddr; 4162fa94a07fSbrendan 4163fa94a07fSbrendan buflist = dev->l2ad_buflist; 4164fa94a07fSbrendan 4165fa94a07fSbrendan if (buflist == NULL) 4166fa94a07fSbrendan return; 4167fa94a07fSbrendan 4168fa94a07fSbrendan if (!all && dev->l2ad_first) { 4169fa94a07fSbrendan /* 4170fa94a07fSbrendan * This is the first sweep through the device. There is 4171fa94a07fSbrendan * nothing to evict. 4172fa94a07fSbrendan */ 4173fa94a07fSbrendan return; 4174fa94a07fSbrendan } 4175fa94a07fSbrendan 41763a737e0dSbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 4177fa94a07fSbrendan /* 4178fa94a07fSbrendan * When nearing the end of the device, evict to the end 4179fa94a07fSbrendan * before the device write hand jumps to the start. 4180fa94a07fSbrendan */ 4181fa94a07fSbrendan taddr = dev->l2ad_end; 4182fa94a07fSbrendan } else { 4183fa94a07fSbrendan taddr = dev->l2ad_hand + distance; 4184fa94a07fSbrendan } 4185fa94a07fSbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 4186fa94a07fSbrendan uint64_t, taddr, boolean_t, all); 4187fa94a07fSbrendan 4188fa94a07fSbrendan top: 4189fa94a07fSbrendan mutex_enter(&l2arc_buflist_mtx); 4190fa94a07fSbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 4191fa94a07fSbrendan ab_prev = list_prev(buflist, ab); 4192fa94a07fSbrendan 4193fa94a07fSbrendan hash_lock = HDR_LOCK(ab); 4194fa94a07fSbrendan if (!mutex_tryenter(hash_lock)) { 4195fa94a07fSbrendan /* 4196fa94a07fSbrendan * Missed the hash lock. Retry. 4197fa94a07fSbrendan */ 4198fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 4199fa94a07fSbrendan mutex_exit(&l2arc_buflist_mtx); 4200fa94a07fSbrendan mutex_enter(hash_lock); 4201fa94a07fSbrendan mutex_exit(hash_lock); 4202fa94a07fSbrendan goto top; 4203fa94a07fSbrendan } 4204fa94a07fSbrendan 4205fa94a07fSbrendan if (HDR_L2_WRITE_HEAD(ab)) { 4206fa94a07fSbrendan /* 4207fa94a07fSbrendan * We hit a write head node. Leave it for 4208fa94a07fSbrendan * l2arc_write_done(). 4209fa94a07fSbrendan */ 4210fa94a07fSbrendan list_remove(buflist, ab); 4211fa94a07fSbrendan mutex_exit(hash_lock); 4212fa94a07fSbrendan continue; 4213fa94a07fSbrendan } 4214fa94a07fSbrendan 4215fa94a07fSbrendan if (!all && ab->b_l2hdr != NULL && 4216fa94a07fSbrendan (ab->b_l2hdr->b_daddr > taddr || 4217fa94a07fSbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 4218fa94a07fSbrendan /* 4219fa94a07fSbrendan * We've evicted to the target address, 4220fa94a07fSbrendan * or the end of the device. 4221fa94a07fSbrendan */ 4222fa94a07fSbrendan mutex_exit(hash_lock); 4223fa94a07fSbrendan break; 4224fa94a07fSbrendan } 4225fa94a07fSbrendan 4226fa94a07fSbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 4227fa94a07fSbrendan /* 4228fa94a07fSbrendan * Already on the path to destruction. 4229fa94a07fSbrendan */ 4230fa94a07fSbrendan mutex_exit(hash_lock); 4231fa94a07fSbrendan continue; 4232fa94a07fSbrendan } 4233fa94a07fSbrendan 4234fa94a07fSbrendan if (ab->b_state == arc_l2c_only) { 4235fa94a07fSbrendan ASSERT(!HDR_L2_READING(ab)); 4236fa94a07fSbrendan /* 4237fa94a07fSbrendan * This doesn't exist in the ARC. Destroy. 4238fa94a07fSbrendan * arc_hdr_destroy() will call list_remove() 4239fa94a07fSbrendan * and decrement arcstat_l2_size. 4240fa94a07fSbrendan */ 4241fa94a07fSbrendan arc_change_state(arc_anon, ab, hash_lock); 4242fa94a07fSbrendan arc_hdr_destroy(ab); 4243fa94a07fSbrendan } else { 42443a737e0dSbrendan /* 42453a737e0dSbrendan * Invalidate issued or about to be issued 42463a737e0dSbrendan * reads, since we may be about to write 42473a737e0dSbrendan * over this location. 42483a737e0dSbrendan */ 42493a737e0dSbrendan if (HDR_L2_READING(ab)) { 42503a737e0dSbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 42513a737e0dSbrendan ab->b_flags |= ARC_L2_EVICTED; 42523a737e0dSbrendan } 42533a737e0dSbrendan 4254fa94a07fSbrendan /* 4255fa94a07fSbrendan * Tell ARC this no longer exists in L2ARC. 4256fa94a07fSbrendan */ 4257fa94a07fSbrendan if (ab->b_l2hdr != NULL) { 4258fa94a07fSbrendan abl2 = ab->b_l2hdr; 4259fa94a07fSbrendan ab->b_l2hdr = NULL; 4260fa94a07fSbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4261fa94a07fSbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4262fa94a07fSbrendan } 4263fa94a07fSbrendan list_remove(buflist, ab); 4264fa94a07fSbrendan 4265fa94a07fSbrendan /* 4266fa94a07fSbrendan * This may have been leftover after a 4267fa94a07fSbrendan * failed write. 4268fa94a07fSbrendan */ 4269fa94a07fSbrendan ab->b_flags &= ~ARC_L2_WRITING; 4270fa94a07fSbrendan } 4271fa94a07fSbrendan mutex_exit(hash_lock); 4272fa94a07fSbrendan } 4273fa94a07fSbrendan mutex_exit(&l2arc_buflist_mtx); 4274fa94a07fSbrendan 4275fa94a07fSbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, -(taddr - dev->l2ad_evict)); 4276fa94a07fSbrendan dev->l2ad_evict = taddr; 4277fa94a07fSbrendan } 4278fa94a07fSbrendan 4279fa94a07fSbrendan /* 4280fa94a07fSbrendan * Find and write ARC buffers to the L2ARC device. 4281fa94a07fSbrendan * 4282fa94a07fSbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 4283fa94a07fSbrendan * for reading until they have completed writing. 4284fa94a07fSbrendan */ 42855a98e54bSBrendan Gregg - Sun Microsystems static uint64_t 42863a737e0dSbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 4287fa94a07fSbrendan { 4288fa94a07fSbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 4289fa94a07fSbrendan l2arc_buf_hdr_t *hdrl2; 4290fa94a07fSbrendan list_t *list; 42913a737e0dSbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 4292fa94a07fSbrendan void *buf_data; 4293fa94a07fSbrendan kmutex_t *hash_lock, *list_lock; 4294fa94a07fSbrendan boolean_t have_lock, full; 4295fa94a07fSbrendan l2arc_write_callback_t *cb; 4296fa94a07fSbrendan zio_t *pio, *wzio; 4297ac05c741SMark Maybee uint64_t guid = spa_guid(spa); 4298fa94a07fSbrendan 4299fa94a07fSbrendan ASSERT(dev->l2ad_vdev != NULL); 4300fa94a07fSbrendan 4301fa94a07fSbrendan pio = NULL; 4302fa94a07fSbrendan write_sz = 0; 4303fa94a07fSbrendan full = B_FALSE; 43041ab7f2deSmaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 4305fa94a07fSbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 4306fa94a07fSbrendan 4307fa94a07fSbrendan /* 4308fa94a07fSbrendan * Copy buffers for L2ARC writing. 4309fa94a07fSbrendan */ 4310fa94a07fSbrendan mutex_enter(&l2arc_buflist_mtx); 4311fa94a07fSbrendan for (int try = 0; try <= 3; try++) { 4312fa94a07fSbrendan list = l2arc_list_locked(try, &list_lock); 4313fa94a07fSbrendan passed_sz = 0; 4314fa94a07fSbrendan 43153a737e0dSbrendan /* 43163a737e0dSbrendan * L2ARC fast warmup. 43173a737e0dSbrendan * 43183a737e0dSbrendan * Until the ARC is warm and starts to evict, read from the 43193a737e0dSbrendan * head of the ARC lists rather than the tail. 43203a737e0dSbrendan */ 43213a737e0dSbrendan headroom = target_sz * l2arc_headroom; 43223a737e0dSbrendan if (arc_warm == B_FALSE) 43233a737e0dSbrendan ab = list_head(list); 43243a737e0dSbrendan else 43253a737e0dSbrendan ab = list_tail(list); 43263a737e0dSbrendan 43273a737e0dSbrendan for (; ab; ab = ab_prev) { 43283a737e0dSbrendan if (arc_warm == B_FALSE) 43293a737e0dSbrendan ab_prev = list_next(list, ab); 43303a737e0dSbrendan else 43313a737e0dSbrendan ab_prev = list_prev(list, ab); 4332fa94a07fSbrendan 4333fa94a07fSbrendan hash_lock = HDR_LOCK(ab); 4334fa94a07fSbrendan have_lock = MUTEX_HELD(hash_lock); 4335fa94a07fSbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 4336fa94a07fSbrendan /* 4337fa94a07fSbrendan * Skip this buffer rather than waiting. 4338fa94a07fSbrendan */ 4339fa94a07fSbrendan continue; 4340fa94a07fSbrendan } 4341fa94a07fSbrendan 4342fa94a07fSbrendan passed_sz += ab->b_size; 4343fa94a07fSbrendan if (passed_sz > headroom) { 4344fa94a07fSbrendan /* 4345fa94a07fSbrendan * Searched too far. 4346fa94a07fSbrendan */ 4347fa94a07fSbrendan mutex_exit(hash_lock); 4348fa94a07fSbrendan break; 4349fa94a07fSbrendan } 4350fa94a07fSbrendan 4351ac05c741SMark Maybee if (!l2arc_write_eligible(guid, ab)) { 4352fa94a07fSbrendan mutex_exit(hash_lock); 4353fa94a07fSbrendan continue; 4354fa94a07fSbrendan } 4355fa94a07fSbrendan 4356fa94a07fSbrendan if ((write_sz + ab->b_size) > target_sz) { 4357fa94a07fSbrendan full = B_TRUE; 4358fa94a07fSbrendan mutex_exit(hash_lock); 4359fa94a07fSbrendan break; 4360fa94a07fSbrendan } 4361fa94a07fSbrendan 4362fa94a07fSbrendan if (pio == NULL) { 4363fa94a07fSbrendan /* 4364fa94a07fSbrendan * Insert a dummy header on the buflist so 4365fa94a07fSbrendan * l2arc_write_done() can find where the 4366fa94a07fSbrendan * write buffers begin without searching. 4367fa94a07fSbrendan */ 4368fa94a07fSbrendan list_insert_head(dev->l2ad_buflist, head); 4369fa94a07fSbrendan 4370fa94a07fSbrendan cb = kmem_alloc( 4371fa94a07fSbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 4372fa94a07fSbrendan cb->l2wcb_dev = dev; 4373fa94a07fSbrendan cb->l2wcb_head = head; 4374fa94a07fSbrendan pio = zio_root(spa, l2arc_write_done, cb, 4375fa94a07fSbrendan ZIO_FLAG_CANFAIL); 4376fa94a07fSbrendan } 4377fa94a07fSbrendan 4378fa94a07fSbrendan /* 4379fa94a07fSbrendan * Create and add a new L2ARC header. 4380fa94a07fSbrendan */ 4381fa94a07fSbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 4382fa94a07fSbrendan hdrl2->b_dev = dev; 4383fa94a07fSbrendan hdrl2->b_daddr = dev->l2ad_hand; 4384fa94a07fSbrendan 4385fa94a07fSbrendan ab->b_flags |= ARC_L2_WRITING; 4386fa94a07fSbrendan ab->b_l2hdr = hdrl2; 4387fa94a07fSbrendan list_insert_head(dev->l2ad_buflist, ab); 4388fa94a07fSbrendan buf_data = ab->b_buf->b_data; 4389fa94a07fSbrendan buf_sz = ab->b_size; 4390fa94a07fSbrendan 4391fa94a07fSbrendan /* 4392fa94a07fSbrendan * Compute and store the buffer cksum before 4393fa94a07fSbrendan * writing. On debug the cksum is verified first. 4394fa94a07fSbrendan */ 4395fa94a07fSbrendan arc_cksum_verify(ab->b_buf); 4396fa94a07fSbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 4397fa94a07fSbrendan 4398fa94a07fSbrendan mutex_exit(hash_lock); 4399fa94a07fSbrendan 4400fa94a07fSbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 4401fa94a07fSbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 4402fa94a07fSbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 4403fa94a07fSbrendan ZIO_FLAG_CANFAIL, B_FALSE); 4404fa94a07fSbrendan 4405fa94a07fSbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 4406fa94a07fSbrendan zio_t *, wzio); 4407fa94a07fSbrendan (void) zio_nowait(wzio); 4408fa94a07fSbrendan 4409e14bb325SJeff Bonwick /* 4410e14bb325SJeff Bonwick * Keep the clock hand suitably device-aligned. 4411e14bb325SJeff Bonwick */ 4412e14bb325SJeff Bonwick buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 4413e14bb325SJeff Bonwick 4414fa94a07fSbrendan write_sz += buf_sz; 4415fa94a07fSbrendan dev->l2ad_hand += buf_sz; 4416fa94a07fSbrendan } 4417fa94a07fSbrendan 4418fa94a07fSbrendan mutex_exit(list_lock); 4419fa94a07fSbrendan 4420fa94a07fSbrendan if (full == B_TRUE) 4421fa94a07fSbrendan break; 4422fa94a07fSbrendan } 4423fa94a07fSbrendan mutex_exit(&l2arc_buflist_mtx); 4424fa94a07fSbrendan 4425fa94a07fSbrendan if (pio == NULL) { 4426fa94a07fSbrendan ASSERT3U(write_sz, ==, 0); 4427fa94a07fSbrendan kmem_cache_free(hdr_cache, head); 44285a98e54bSBrendan Gregg - Sun Microsystems return (0); 4429fa94a07fSbrendan } 4430fa94a07fSbrendan 4431fa94a07fSbrendan ASSERT3U(write_sz, <=, target_sz); 4432fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 44335a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 4434fa94a07fSbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 4435fa94a07fSbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, write_sz); 4436fa94a07fSbrendan 4437fa94a07fSbrendan /* 4438fa94a07fSbrendan * Bump device hand to the device start if it is approaching the end. 4439fa94a07fSbrendan * l2arc_evict() will already have evicted ahead for this case. 4440fa94a07fSbrendan */ 44413a737e0dSbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 4442fa94a07fSbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, 4443fa94a07fSbrendan dev->l2ad_end - dev->l2ad_hand); 4444fa94a07fSbrendan dev->l2ad_hand = dev->l2ad_start; 4445fa94a07fSbrendan dev->l2ad_evict = dev->l2ad_start; 4446fa94a07fSbrendan dev->l2ad_first = B_FALSE; 4447fa94a07fSbrendan } 4448fa94a07fSbrendan 44495a98e54bSBrendan Gregg - Sun Microsystems dev->l2ad_writing = B_TRUE; 4450fa94a07fSbrendan (void) zio_wait(pio); 44515a98e54bSBrendan Gregg - Sun Microsystems dev->l2ad_writing = B_FALSE; 44525a98e54bSBrendan Gregg - Sun Microsystems 44535a98e54bSBrendan Gregg - Sun Microsystems return (write_sz); 4454fa94a07fSbrendan } 4455fa94a07fSbrendan 4456fa94a07fSbrendan /* 4457fa94a07fSbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 4458fa94a07fSbrendan * heart of the L2ARC. 4459fa94a07fSbrendan */ 4460fa94a07fSbrendan static void 4461fa94a07fSbrendan l2arc_feed_thread(void) 4462fa94a07fSbrendan { 4463fa94a07fSbrendan callb_cpr_t cpr; 4464fa94a07fSbrendan l2arc_dev_t *dev; 4465fa94a07fSbrendan spa_t *spa; 44665a98e54bSBrendan Gregg - Sun Microsystems uint64_t size, wrote; 44675a98e54bSBrendan Gregg - Sun Microsystems clock_t begin, next = lbolt; 4468fa94a07fSbrendan 4469fa94a07fSbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 4470fa94a07fSbrendan 4471fa94a07fSbrendan mutex_enter(&l2arc_feed_thr_lock); 4472fa94a07fSbrendan 4473fa94a07fSbrendan while (l2arc_thread_exit == 0) { 4474fa94a07fSbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 4475fa94a07fSbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 44765a98e54bSBrendan Gregg - Sun Microsystems next); 4477fa94a07fSbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 44785a98e54bSBrendan Gregg - Sun Microsystems next = lbolt + hz; 4479fa94a07fSbrendan 44803a737e0dSbrendan /* 44813a737e0dSbrendan * Quick check for L2ARC devices. 44823a737e0dSbrendan */ 4483c5904d13Seschrock mutex_enter(&l2arc_dev_mtx); 44843a737e0dSbrendan if (l2arc_ndev == 0) { 44853a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 44863a737e0dSbrendan continue; 44873a737e0dSbrendan } 44883a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 44895a98e54bSBrendan Gregg - Sun Microsystems begin = lbolt; 4490c5904d13Seschrock 4491fa94a07fSbrendan /* 4492c5904d13Seschrock * This selects the next l2arc device to write to, and in 4493c5904d13Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 44943a737e0dSbrendan * will return NULL if there are now no l2arc devices or if 44953a737e0dSbrendan * they are all faulted. 44963a737e0dSbrendan * 44973a737e0dSbrendan * If a device is returned, its spa's config lock is also 44983a737e0dSbrendan * held to prevent device removal. l2arc_dev_get_next() 44993a737e0dSbrendan * will grab and release l2arc_dev_mtx. 4500fa94a07fSbrendan */ 45013a737e0dSbrendan if ((dev = l2arc_dev_get_next()) == NULL) 4502fa94a07fSbrendan continue; 45033a737e0dSbrendan 45043a737e0dSbrendan spa = dev->l2ad_spa; 45053a737e0dSbrendan ASSERT(spa != NULL); 4506fa94a07fSbrendan 4507fa94a07fSbrendan /* 4508fa94a07fSbrendan * Avoid contributing to memory pressure. 4509fa94a07fSbrendan */ 4510fa94a07fSbrendan if (arc_reclaim_needed()) { 4511fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 4512e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, dev); 4513fa94a07fSbrendan continue; 4514fa94a07fSbrendan } 4515fa94a07fSbrendan 4516fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 4517fa94a07fSbrendan 45185a98e54bSBrendan Gregg - Sun Microsystems size = l2arc_write_size(dev); 45193a737e0dSbrendan 4520fa94a07fSbrendan /* 4521fa94a07fSbrendan * Evict L2ARC buffers that will be overwritten. 4522fa94a07fSbrendan */ 45233a737e0dSbrendan l2arc_evict(dev, size, B_FALSE); 4524fa94a07fSbrendan 4525fa94a07fSbrendan /* 4526fa94a07fSbrendan * Write ARC buffers. 4527fa94a07fSbrendan */ 45285a98e54bSBrendan Gregg - Sun Microsystems wrote = l2arc_write_buffers(spa, dev, size); 45295a98e54bSBrendan Gregg - Sun Microsystems 45305a98e54bSBrendan Gregg - Sun Microsystems /* 45315a98e54bSBrendan Gregg - Sun Microsystems * Calculate interval between writes. 45325a98e54bSBrendan Gregg - Sun Microsystems */ 45335a98e54bSBrendan Gregg - Sun Microsystems next = l2arc_write_interval(begin, size, wrote); 4534e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, dev); 4535fa94a07fSbrendan } 4536fa94a07fSbrendan 4537fa94a07fSbrendan l2arc_thread_exit = 0; 4538fa94a07fSbrendan cv_broadcast(&l2arc_feed_thr_cv); 4539fa94a07fSbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 4540fa94a07fSbrendan thread_exit(); 4541fa94a07fSbrendan } 4542fa94a07fSbrendan 4543c5904d13Seschrock boolean_t 4544c5904d13Seschrock l2arc_vdev_present(vdev_t *vd) 4545c5904d13Seschrock { 4546c5904d13Seschrock l2arc_dev_t *dev; 4547c5904d13Seschrock 4548c5904d13Seschrock mutex_enter(&l2arc_dev_mtx); 4549c5904d13Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 4550c5904d13Seschrock dev = list_next(l2arc_dev_list, dev)) { 4551c5904d13Seschrock if (dev->l2ad_vdev == vd) 4552c5904d13Seschrock break; 4553c5904d13Seschrock } 4554c5904d13Seschrock mutex_exit(&l2arc_dev_mtx); 4555c5904d13Seschrock 4556c5904d13Seschrock return (dev != NULL); 4557c5904d13Seschrock } 4558c5904d13Seschrock 4559fa94a07fSbrendan /* 4560fa94a07fSbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 4561fa94a07fSbrendan * validated the vdev and opened it. 4562fa94a07fSbrendan */ 4563fa94a07fSbrendan void 4564573ca77eSGeorge Wilson l2arc_add_vdev(spa_t *spa, vdev_t *vd) 4565fa94a07fSbrendan { 4566fa94a07fSbrendan l2arc_dev_t *adddev; 4567fa94a07fSbrendan 4568c5904d13Seschrock ASSERT(!l2arc_vdev_present(vd)); 4569c5904d13Seschrock 4570fa94a07fSbrendan /* 4571fa94a07fSbrendan * Create a new l2arc device entry. 4572fa94a07fSbrendan */ 4573fa94a07fSbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 4574fa94a07fSbrendan adddev->l2ad_spa = spa; 4575fa94a07fSbrendan adddev->l2ad_vdev = vd; 4576fa94a07fSbrendan adddev->l2ad_write = l2arc_write_max; 45773a737e0dSbrendan adddev->l2ad_boost = l2arc_write_boost; 4578573ca77eSGeorge Wilson adddev->l2ad_start = VDEV_LABEL_START_SIZE; 4579573ca77eSGeorge Wilson adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 4580fa94a07fSbrendan adddev->l2ad_hand = adddev->l2ad_start; 4581fa94a07fSbrendan adddev->l2ad_evict = adddev->l2ad_start; 4582fa94a07fSbrendan adddev->l2ad_first = B_TRUE; 45835a98e54bSBrendan Gregg - Sun Microsystems adddev->l2ad_writing = B_FALSE; 4584fa94a07fSbrendan ASSERT3U(adddev->l2ad_write, >, 0); 4585fa94a07fSbrendan 4586fa94a07fSbrendan /* 4587fa94a07fSbrendan * This is a list of all ARC buffers that are still valid on the 4588fa94a07fSbrendan * device. 4589fa94a07fSbrendan */ 4590fa94a07fSbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 4591fa94a07fSbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 4592fa94a07fSbrendan offsetof(arc_buf_hdr_t, b_l2node)); 4593fa94a07fSbrendan 4594fa94a07fSbrendan spa_l2cache_space_update(vd, adddev->l2ad_end - adddev->l2ad_hand, 0); 4595fa94a07fSbrendan 4596fa94a07fSbrendan /* 4597fa94a07fSbrendan * Add device to global list 4598fa94a07fSbrendan */ 4599fa94a07fSbrendan mutex_enter(&l2arc_dev_mtx); 4600fa94a07fSbrendan list_insert_head(l2arc_dev_list, adddev); 4601fa94a07fSbrendan atomic_inc_64(&l2arc_ndev); 4602fa94a07fSbrendan mutex_exit(&l2arc_dev_mtx); 4603fa94a07fSbrendan } 4604fa94a07fSbrendan 4605fa94a07fSbrendan /* 4606fa94a07fSbrendan * Remove a vdev from the L2ARC. 4607fa94a07fSbrendan */ 4608fa94a07fSbrendan void 4609fa94a07fSbrendan l2arc_remove_vdev(vdev_t *vd) 4610fa94a07fSbrendan { 4611fa94a07fSbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 4612fa94a07fSbrendan 4613fa94a07fSbrendan /* 4614fa94a07fSbrendan * Find the device by vdev 4615fa94a07fSbrendan */ 4616fa94a07fSbrendan mutex_enter(&l2arc_dev_mtx); 4617fa94a07fSbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 4618fa94a07fSbrendan nextdev = list_next(l2arc_dev_list, dev); 4619fa94a07fSbrendan if (vd == dev->l2ad_vdev) { 4620fa94a07fSbrendan remdev = dev; 4621fa94a07fSbrendan break; 4622fa94a07fSbrendan } 4623fa94a07fSbrendan } 4624fa94a07fSbrendan ASSERT(remdev != NULL); 4625fa94a07fSbrendan 4626fa94a07fSbrendan /* 4627fa94a07fSbrendan * Remove device from global list 4628fa94a07fSbrendan */ 4629fa94a07fSbrendan list_remove(l2arc_dev_list, remdev); 4630fa94a07fSbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 46313a737e0dSbrendan atomic_dec_64(&l2arc_ndev); 46323a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 4633fa94a07fSbrendan 4634fa94a07fSbrendan /* 4635fa94a07fSbrendan * Clear all buflists and ARC references. L2ARC device flush. 4636fa94a07fSbrendan */ 4637fa94a07fSbrendan l2arc_evict(remdev, 0, B_TRUE); 4638fa94a07fSbrendan list_destroy(remdev->l2ad_buflist); 4639fa94a07fSbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 4640fa94a07fSbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 4641fa94a07fSbrendan } 4642fa94a07fSbrendan 4643fa94a07fSbrendan void 4644e14bb325SJeff Bonwick l2arc_init(void) 4645fa94a07fSbrendan { 4646fa94a07fSbrendan l2arc_thread_exit = 0; 4647fa94a07fSbrendan l2arc_ndev = 0; 4648fa94a07fSbrendan l2arc_writes_sent = 0; 4649fa94a07fSbrendan l2arc_writes_done = 0; 4650fa94a07fSbrendan 4651fa94a07fSbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 4652fa94a07fSbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 4653fa94a07fSbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 4654fa94a07fSbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 4655fa94a07fSbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 4656fa94a07fSbrendan 4657fa94a07fSbrendan l2arc_dev_list = &L2ARC_dev_list; 4658fa94a07fSbrendan l2arc_free_on_write = &L2ARC_free_on_write; 4659fa94a07fSbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 4660fa94a07fSbrendan offsetof(l2arc_dev_t, l2ad_node)); 4661fa94a07fSbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 4662fa94a07fSbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 4663fa94a07fSbrendan } 4664fa94a07fSbrendan 4665fa94a07fSbrendan void 4666e14bb325SJeff Bonwick l2arc_fini(void) 4667fa94a07fSbrendan { 46683a737e0dSbrendan /* 46693a737e0dSbrendan * This is called from dmu_fini(), which is called from spa_fini(); 46703a737e0dSbrendan * Because of this, we can assume that all l2arc devices have 46713a737e0dSbrendan * already been removed when the pools themselves were removed. 46723a737e0dSbrendan */ 46733a737e0dSbrendan 46743a737e0dSbrendan l2arc_do_free_on_write(); 46753a737e0dSbrendan 4676fa94a07fSbrendan mutex_destroy(&l2arc_feed_thr_lock); 4677fa94a07fSbrendan cv_destroy(&l2arc_feed_thr_cv); 4678fa94a07fSbrendan mutex_destroy(&l2arc_dev_mtx); 4679fa94a07fSbrendan mutex_destroy(&l2arc_buflist_mtx); 4680fa94a07fSbrendan mutex_destroy(&l2arc_free_on_write_mtx); 4681fa94a07fSbrendan 4682fa94a07fSbrendan list_destroy(l2arc_dev_list); 4683fa94a07fSbrendan list_destroy(l2arc_free_on_write); 4684fa94a07fSbrendan } 4685e14bb325SJeff Bonwick 4686e14bb325SJeff Bonwick void 4687e14bb325SJeff Bonwick l2arc_start(void) 4688e14bb325SJeff Bonwick { 46898ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4690e14bb325SJeff Bonwick return; 4691e14bb325SJeff Bonwick 4692e14bb325SJeff Bonwick (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 4693e14bb325SJeff Bonwick TS_RUN, minclsyspri); 4694e14bb325SJeff Bonwick } 4695e14bb325SJeff Bonwick 4696e14bb325SJeff Bonwick void 4697e14bb325SJeff Bonwick l2arc_stop(void) 4698e14bb325SJeff Bonwick { 46998ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4700e14bb325SJeff Bonwick return; 4701e14bb325SJeff Bonwick 4702e14bb325SJeff Bonwick mutex_enter(&l2arc_feed_thr_lock); 4703e14bb325SJeff Bonwick cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 4704e14bb325SJeff Bonwick l2arc_thread_exit = 1; 4705e14bb325SJeff Bonwick while (l2arc_thread_exit != 0) 4706e14bb325SJeff Bonwick cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 4707e14bb325SJeff Bonwick mutex_exit(&l2arc_feed_thr_lock); 4708e14bb325SJeff Bonwick } 4709