1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51484Sek110237 * Common Development and Distribution License (the "License"). 61484Sek110237 * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 226018Sbrendan * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens /* 273403Sbmc * DVA-based Adjustable Replacement Cache 28789Sahrens * 291544Seschrock * While much of the theory of operation used here is 301544Seschrock * based on the self-tuning, low overhead replacement cache 31789Sahrens * presented by Megiddo and Modha at FAST 2003, there are some 32789Sahrens * significant differences: 33789Sahrens * 34789Sahrens * 1. The Megiddo and Modha model assumes any page is evictable. 35789Sahrens * Pages in its cache cannot be "locked" into memory. This makes 36789Sahrens * the eviction algorithm simple: evict the last page in the list. 37789Sahrens * This also make the performance characteristics easy to reason 38789Sahrens * about. Our cache is not so simple. At any given moment, some 39789Sahrens * subset of the blocks in the cache are un-evictable because we 40789Sahrens * have handed out a reference to them. Blocks are only evictable 41789Sahrens * when there are no external references active. This makes 42789Sahrens * eviction far more problematic: we choose to evict the evictable 43789Sahrens * blocks that are the "lowest" in the list. 44789Sahrens * 45789Sahrens * There are times when it is not possible to evict the requested 46789Sahrens * space. In these circumstances we are unable to adjust the cache 47789Sahrens * size. To prevent the cache growing unbounded at these times we 485450Sbrendan * implement a "cache throttle" that slows the flow of new data 495450Sbrendan * into the cache until we can make space available. 50789Sahrens * 51789Sahrens * 2. The Megiddo and Modha model assumes a fixed cache size. 52789Sahrens * Pages are evicted when the cache is full and there is a cache 53789Sahrens * miss. Our model has a variable sized cache. It grows with 545450Sbrendan * high use, but also tries to react to memory pressure from the 55789Sahrens * operating system: decreasing its size when system memory is 56789Sahrens * tight. 57789Sahrens * 58789Sahrens * 3. The Megiddo and Modha model assumes a fixed page size. All 59789Sahrens * elements of the cache are therefor exactly the same size. So 60789Sahrens * when adjusting the cache size following a cache miss, its simply 61789Sahrens * a matter of choosing a single page to evict. In our model, we 62789Sahrens * have variable sized cache blocks (rangeing from 512 bytes to 63789Sahrens * 128K bytes). We therefor choose a set of blocks to evict to make 64789Sahrens * space for a cache miss that approximates as closely as possible 65789Sahrens * the space used by the new block. 66789Sahrens * 67789Sahrens * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 68789Sahrens * by N. Megiddo & D. Modha, FAST 2003 69789Sahrens */ 70789Sahrens 71789Sahrens /* 72789Sahrens * The locking model: 73789Sahrens * 74789Sahrens * A new reference to a cache buffer can be obtained in two 75789Sahrens * ways: 1) via a hash table lookup using the DVA as a key, 765450Sbrendan * or 2) via one of the ARC lists. The arc_read() interface 77789Sahrens * uses method 1, while the internal arc algorithms for 78789Sahrens * adjusting the cache use method 2. We therefor provide two 79789Sahrens * types of locks: 1) the hash table lock array, and 2) the 80789Sahrens * arc list locks. 81789Sahrens * 82789Sahrens * Buffers do not have their own mutexs, rather they rely on the 83789Sahrens * hash table mutexs for the bulk of their protection (i.e. most 84789Sahrens * fields in the arc_buf_hdr_t are protected by these mutexs). 85789Sahrens * 86789Sahrens * buf_hash_find() returns the appropriate mutex (held) when it 87789Sahrens * locates the requested buffer in the hash table. It returns 88789Sahrens * NULL for the mutex if the buffer was not in the table. 89789Sahrens * 90789Sahrens * buf_hash_remove() expects the appropriate hash mutex to be 91789Sahrens * already held before it is invoked. 92789Sahrens * 93789Sahrens * Each arc state also has a mutex which is used to protect the 94789Sahrens * buffer list associated with the state. When attempting to 95789Sahrens * obtain a hash table lock while holding an arc list lock you 96789Sahrens * must use: mutex_tryenter() to avoid deadlock. Also note that 972688Smaybee * the active state mutex must be held before the ghost state mutex. 98789Sahrens * 991544Seschrock * Arc buffers may have an associated eviction callback function. 1001544Seschrock * This function will be invoked prior to removing the buffer (e.g. 1011544Seschrock * in arc_do_user_evicts()). Note however that the data associated 1021544Seschrock * with the buffer may be evicted prior to the callback. The callback 1031544Seschrock * must be made with *no locks held* (to prevent deadlock). Additionally, 1041544Seschrock * the users of callbacks must ensure that their private data is 1051544Seschrock * protected from simultaneous callbacks from arc_buf_evict() 1061544Seschrock * and arc_do_user_evicts(). 1071544Seschrock * 108789Sahrens * Note that the majority of the performance stats are manipulated 109789Sahrens * with atomic operations. 1105450Sbrendan * 1115450Sbrendan * The L2ARC uses the l2arc_buflist_mtx global mutex for the following: 1125450Sbrendan * 1135450Sbrendan * - L2ARC buflist creation 1145450Sbrendan * - L2ARC buflist eviction 1155450Sbrendan * - L2ARC write completion, which walks L2ARC buflists 1165450Sbrendan * - ARC header destruction, as it removes from L2ARC buflists 1175450Sbrendan * - ARC header release, as it removes from L2ARC buflists 118789Sahrens */ 119789Sahrens 120789Sahrens #include <sys/spa.h> 121789Sahrens #include <sys/zio.h> 1223093Sahrens #include <sys/zio_checksum.h> 123789Sahrens #include <sys/zfs_context.h> 124789Sahrens #include <sys/arc.h> 125789Sahrens #include <sys/refcount.h> 1266643Seschrock #include <sys/vdev.h> 127789Sahrens #ifdef _KERNEL 128789Sahrens #include <sys/vmsystm.h> 129789Sahrens #include <vm/anon.h> 130789Sahrens #include <sys/fs/swapnode.h> 1311484Sek110237 #include <sys/dnlc.h> 132789Sahrens #endif 133789Sahrens #include <sys/callb.h> 1343403Sbmc #include <sys/kstat.h> 135789Sahrens 136789Sahrens static kmutex_t arc_reclaim_thr_lock; 137789Sahrens static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 138789Sahrens static uint8_t arc_thread_exit; 139789Sahrens 1406245Smaybee extern int zfs_write_limit_shift; 1416245Smaybee extern uint64_t zfs_write_limit_max; 142*7468SMark.Maybee@Sun.COM extern kmutex_t zfs_write_limit_lock; 1436245Smaybee 1441484Sek110237 #define ARC_REDUCE_DNLC_PERCENT 3 1451484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 1461484Sek110237 147789Sahrens typedef enum arc_reclaim_strategy { 148789Sahrens ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 149789Sahrens ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 150789Sahrens } arc_reclaim_strategy_t; 151789Sahrens 152789Sahrens /* number of seconds before growing cache again */ 153789Sahrens static int arc_grow_retry = 60; 154789Sahrens 1552391Smaybee /* 1562638Sperrin * minimum lifespan of a prefetch block in clock ticks 1572638Sperrin * (initialized in arc_init()) 1582391Smaybee */ 1592638Sperrin static int arc_min_prefetch_lifespan; 1602391Smaybee 161789Sahrens static int arc_dead; 162789Sahrens 163789Sahrens /* 1646987Sbrendan * The arc has filled available memory and has now warmed up. 1656987Sbrendan */ 1666987Sbrendan static boolean_t arc_warm; 1676987Sbrendan 1686987Sbrendan /* 1692885Sahrens * These tunables are for performance analysis. 1702885Sahrens */ 1712885Sahrens uint64_t zfs_arc_max; 1722885Sahrens uint64_t zfs_arc_min; 1734645Sek110237 uint64_t zfs_arc_meta_limit = 0; 1747046Sahrens int zfs_mdcomp_disable = 0; 1752885Sahrens 1762885Sahrens /* 1775450Sbrendan * Note that buffers can be in one of 6 states: 178789Sahrens * ARC_anon - anonymous (discussed below) 1791544Seschrock * ARC_mru - recently used, currently cached 1801544Seschrock * ARC_mru_ghost - recentely used, no longer in cache 1811544Seschrock * ARC_mfu - frequently used, currently cached 1821544Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 1835450Sbrendan * ARC_l2c_only - exists in L2ARC but not other states 1844309Smaybee * When there are no active references to the buffer, they are 1854309Smaybee * are linked onto a list in one of these arc states. These are 1864309Smaybee * the only buffers that can be evicted or deleted. Within each 1874309Smaybee * state there are multiple lists, one for meta-data and one for 1884309Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 1894309Smaybee * etc.) is tracked separately so that it can be managed more 1905450Sbrendan * explicitly: favored over data, limited explicitly. 191789Sahrens * 192789Sahrens * Anonymous buffers are buffers that are not associated with 193789Sahrens * a DVA. These are buffers that hold dirty block copies 194789Sahrens * before they are written to stable storage. By definition, 1951544Seschrock * they are "ref'd" and are considered part of arc_mru 196789Sahrens * that cannot be freed. Generally, they will aquire a DVA 1971544Seschrock * as they are written and migrate onto the arc_mru list. 1985450Sbrendan * 1995450Sbrendan * The ARC_l2c_only state is for buffers that are in the second 2005450Sbrendan * level ARC but no longer in any of the ARC_m* lists. The second 2015450Sbrendan * level ARC itself may also contain buffers that are in any of 2025450Sbrendan * the ARC_m* states - meaning that a buffer can exist in two 2035450Sbrendan * places. The reason for the ARC_l2c_only state is to keep the 2045450Sbrendan * buffer header in the hash table, so that reads that hit the 2055450Sbrendan * second level ARC benefit from these fast lookups. 206789Sahrens */ 207789Sahrens 208789Sahrens typedef struct arc_state { 2094309Smaybee list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */ 2104309Smaybee uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 2114309Smaybee uint64_t arcs_size; /* total amount of data in this state */ 2123403Sbmc kmutex_t arcs_mtx; 213789Sahrens } arc_state_t; 214789Sahrens 2155450Sbrendan /* The 6 states: */ 216789Sahrens static arc_state_t ARC_anon; 2171544Seschrock static arc_state_t ARC_mru; 2181544Seschrock static arc_state_t ARC_mru_ghost; 2191544Seschrock static arc_state_t ARC_mfu; 2201544Seschrock static arc_state_t ARC_mfu_ghost; 2215450Sbrendan static arc_state_t ARC_l2c_only; 222789Sahrens 2233403Sbmc typedef struct arc_stats { 2243403Sbmc kstat_named_t arcstat_hits; 2253403Sbmc kstat_named_t arcstat_misses; 2263403Sbmc kstat_named_t arcstat_demand_data_hits; 2273403Sbmc kstat_named_t arcstat_demand_data_misses; 2283403Sbmc kstat_named_t arcstat_demand_metadata_hits; 2293403Sbmc kstat_named_t arcstat_demand_metadata_misses; 2303403Sbmc kstat_named_t arcstat_prefetch_data_hits; 2313403Sbmc kstat_named_t arcstat_prefetch_data_misses; 2323403Sbmc kstat_named_t arcstat_prefetch_metadata_hits; 2333403Sbmc kstat_named_t arcstat_prefetch_metadata_misses; 2343403Sbmc kstat_named_t arcstat_mru_hits; 2353403Sbmc kstat_named_t arcstat_mru_ghost_hits; 2363403Sbmc kstat_named_t arcstat_mfu_hits; 2373403Sbmc kstat_named_t arcstat_mfu_ghost_hits; 2383403Sbmc kstat_named_t arcstat_deleted; 2393403Sbmc kstat_named_t arcstat_recycle_miss; 2403403Sbmc kstat_named_t arcstat_mutex_miss; 2413403Sbmc kstat_named_t arcstat_evict_skip; 2423403Sbmc kstat_named_t arcstat_hash_elements; 2433403Sbmc kstat_named_t arcstat_hash_elements_max; 2443403Sbmc kstat_named_t arcstat_hash_collisions; 2453403Sbmc kstat_named_t arcstat_hash_chains; 2463403Sbmc kstat_named_t arcstat_hash_chain_max; 2473403Sbmc kstat_named_t arcstat_p; 2483403Sbmc kstat_named_t arcstat_c; 2493403Sbmc kstat_named_t arcstat_c_min; 2503403Sbmc kstat_named_t arcstat_c_max; 2513403Sbmc kstat_named_t arcstat_size; 2525450Sbrendan kstat_named_t arcstat_hdr_size; 2535450Sbrendan kstat_named_t arcstat_l2_hits; 2545450Sbrendan kstat_named_t arcstat_l2_misses; 2555450Sbrendan kstat_named_t arcstat_l2_feeds; 2565450Sbrendan kstat_named_t arcstat_l2_rw_clash; 2575450Sbrendan kstat_named_t arcstat_l2_writes_sent; 2585450Sbrendan kstat_named_t arcstat_l2_writes_done; 2595450Sbrendan kstat_named_t arcstat_l2_writes_error; 2605450Sbrendan kstat_named_t arcstat_l2_writes_hdr_miss; 2615450Sbrendan kstat_named_t arcstat_l2_evict_lock_retry; 2625450Sbrendan kstat_named_t arcstat_l2_evict_reading; 2635450Sbrendan kstat_named_t arcstat_l2_free_on_write; 2645450Sbrendan kstat_named_t arcstat_l2_abort_lowmem; 2655450Sbrendan kstat_named_t arcstat_l2_cksum_bad; 2665450Sbrendan kstat_named_t arcstat_l2_io_error; 2675450Sbrendan kstat_named_t arcstat_l2_size; 2685450Sbrendan kstat_named_t arcstat_l2_hdr_size; 2696245Smaybee kstat_named_t arcstat_memory_throttle_count; 2703403Sbmc } arc_stats_t; 2713403Sbmc 2723403Sbmc static arc_stats_t arc_stats = { 2733403Sbmc { "hits", KSTAT_DATA_UINT64 }, 2743403Sbmc { "misses", KSTAT_DATA_UINT64 }, 2753403Sbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 2763403Sbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 2773403Sbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 2783403Sbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 2793403Sbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 2803403Sbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 2813403Sbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 2823403Sbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 2833403Sbmc { "mru_hits", KSTAT_DATA_UINT64 }, 2843403Sbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 2853403Sbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 2863403Sbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 2873403Sbmc { "deleted", KSTAT_DATA_UINT64 }, 2883403Sbmc { "recycle_miss", KSTAT_DATA_UINT64 }, 2893403Sbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 2903403Sbmc { "evict_skip", KSTAT_DATA_UINT64 }, 2913403Sbmc { "hash_elements", KSTAT_DATA_UINT64 }, 2923403Sbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 2933403Sbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 2943403Sbmc { "hash_chains", KSTAT_DATA_UINT64 }, 2953403Sbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 2963403Sbmc { "p", KSTAT_DATA_UINT64 }, 2973403Sbmc { "c", KSTAT_DATA_UINT64 }, 2983403Sbmc { "c_min", KSTAT_DATA_UINT64 }, 2993403Sbmc { "c_max", KSTAT_DATA_UINT64 }, 3005450Sbrendan { "size", KSTAT_DATA_UINT64 }, 3015450Sbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 3025450Sbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 3035450Sbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 3045450Sbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 3055450Sbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 3065450Sbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 3075450Sbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 3085450Sbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 3095450Sbrendan { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 3105450Sbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 3115450Sbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 3125450Sbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 3135450Sbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 3145450Sbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 3155450Sbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 3165450Sbrendan { "l2_size", KSTAT_DATA_UINT64 }, 3176245Smaybee { "l2_hdr_size", KSTAT_DATA_UINT64 }, 3186245Smaybee { "memory_throttle_count", KSTAT_DATA_UINT64 } 3193403Sbmc }; 320789Sahrens 3213403Sbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 3223403Sbmc 3233403Sbmc #define ARCSTAT_INCR(stat, val) \ 3243403Sbmc atomic_add_64(&arc_stats.stat.value.ui64, (val)); 3253403Sbmc 3263403Sbmc #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 3273403Sbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 3283403Sbmc 3293403Sbmc #define ARCSTAT_MAX(stat, val) { \ 3303403Sbmc uint64_t m; \ 3313403Sbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 3323403Sbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 3333403Sbmc continue; \ 3343403Sbmc } 3353403Sbmc 3363403Sbmc #define ARCSTAT_MAXSTAT(stat) \ 3373403Sbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 338789Sahrens 3393403Sbmc /* 3403403Sbmc * We define a macro to allow ARC hits/misses to be easily broken down by 3413403Sbmc * two separate conditions, giving a total of four different subtypes for 3423403Sbmc * each of hits and misses (so eight statistics total). 3433403Sbmc */ 3443403Sbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 3453403Sbmc if (cond1) { \ 3463403Sbmc if (cond2) { \ 3473403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 3483403Sbmc } else { \ 3493403Sbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 3503403Sbmc } \ 3513403Sbmc } else { \ 3523403Sbmc if (cond2) { \ 3533403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 3543403Sbmc } else { \ 3553403Sbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 3563403Sbmc } \ 3573403Sbmc } 358789Sahrens 3593403Sbmc kstat_t *arc_ksp; 3603403Sbmc static arc_state_t *arc_anon; 3613403Sbmc static arc_state_t *arc_mru; 3623403Sbmc static arc_state_t *arc_mru_ghost; 3633403Sbmc static arc_state_t *arc_mfu; 3643403Sbmc static arc_state_t *arc_mfu_ghost; 3655450Sbrendan static arc_state_t *arc_l2c_only; 3663403Sbmc 3673403Sbmc /* 3683403Sbmc * There are several ARC variables that are critical to export as kstats -- 3693403Sbmc * but we don't want to have to grovel around in the kstat whenever we wish to 3703403Sbmc * manipulate them. For these variables, we therefore define them to be in 3713403Sbmc * terms of the statistic variable. This assures that we are not introducing 3723403Sbmc * the possibility of inconsistency by having shadow copies of the variables, 3733403Sbmc * while still allowing the code to be readable. 3743403Sbmc */ 3753403Sbmc #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 3763403Sbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 3773403Sbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 3783403Sbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 3793403Sbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 3803403Sbmc 3813403Sbmc static int arc_no_grow; /* Don't try to grow cache size */ 3823403Sbmc static uint64_t arc_tempreserve; 3834309Smaybee static uint64_t arc_meta_used; 3844309Smaybee static uint64_t arc_meta_limit; 3854309Smaybee static uint64_t arc_meta_max = 0; 386789Sahrens 3875450Sbrendan typedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 3885450Sbrendan 389789Sahrens typedef struct arc_callback arc_callback_t; 390789Sahrens 391789Sahrens struct arc_callback { 3923547Smaybee void *acb_private; 393789Sahrens arc_done_func_t *acb_done; 394789Sahrens arc_buf_t *acb_buf; 395789Sahrens zio_t *acb_zio_dummy; 396789Sahrens arc_callback_t *acb_next; 397789Sahrens }; 398789Sahrens 3993547Smaybee typedef struct arc_write_callback arc_write_callback_t; 4003547Smaybee 4013547Smaybee struct arc_write_callback { 4023547Smaybee void *awcb_private; 4033547Smaybee arc_done_func_t *awcb_ready; 4043547Smaybee arc_done_func_t *awcb_done; 4053547Smaybee arc_buf_t *awcb_buf; 4063547Smaybee }; 4073547Smaybee 408789Sahrens struct arc_buf_hdr { 409789Sahrens /* protected by hash lock */ 410789Sahrens dva_t b_dva; 411789Sahrens uint64_t b_birth; 412789Sahrens uint64_t b_cksum0; 413789Sahrens 4143093Sahrens kmutex_t b_freeze_lock; 4153093Sahrens zio_cksum_t *b_freeze_cksum; 4163093Sahrens 417789Sahrens arc_buf_hdr_t *b_hash_next; 418789Sahrens arc_buf_t *b_buf; 419789Sahrens uint32_t b_flags; 4201544Seschrock uint32_t b_datacnt; 421789Sahrens 4223290Sjohansen arc_callback_t *b_acb; 423789Sahrens kcondvar_t b_cv; 4243290Sjohansen 4253290Sjohansen /* immutable */ 4263290Sjohansen arc_buf_contents_t b_type; 4273290Sjohansen uint64_t b_size; 4283290Sjohansen spa_t *b_spa; 429789Sahrens 430789Sahrens /* protected by arc state mutex */ 431789Sahrens arc_state_t *b_state; 432789Sahrens list_node_t b_arc_node; 433789Sahrens 434789Sahrens /* updated atomically */ 435789Sahrens clock_t b_arc_access; 436789Sahrens 437789Sahrens /* self protecting */ 438789Sahrens refcount_t b_refcnt; 4395450Sbrendan 4405450Sbrendan l2arc_buf_hdr_t *b_l2hdr; 4415450Sbrendan list_node_t b_l2node; 4427046Sahrens /* 4437046Sahrens * scrub code can lockout access to the buf while it changes 4447046Sahrens * bp's contained within it. 4457046Sahrens */ 4467046Sahrens krwlock_t b_datalock; 447789Sahrens }; 448789Sahrens 4491544Seschrock static arc_buf_t *arc_eviction_list; 4501544Seschrock static kmutex_t arc_eviction_mtx; 4512887Smaybee static arc_buf_hdr_t arc_eviction_hdr; 4522688Smaybee static void arc_get_data_buf(arc_buf_t *buf); 4532688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 4544309Smaybee static int arc_evict_needed(arc_buf_contents_t type); 4555642Smaybee static void arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes); 4561544Seschrock 4571544Seschrock #define GHOST_STATE(state) \ 4585450Sbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 4595450Sbrendan (state) == arc_l2c_only) 4601544Seschrock 461789Sahrens /* 462789Sahrens * Private ARC flags. These flags are private ARC only flags that will show up 463789Sahrens * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 464789Sahrens * be passed in as arc_flags in things like arc_read. However, these flags 465789Sahrens * should never be passed and should only be set by ARC code. When adding new 466789Sahrens * public flags, make sure not to smash the private ones. 467789Sahrens */ 468789Sahrens 4691544Seschrock #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 470789Sahrens #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 471789Sahrens #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 472789Sahrens #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 4731544Seschrock #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 4742391Smaybee #define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 4755450Sbrendan #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 4767237Sek110237 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 4777237Sek110237 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 4787237Sek110237 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 4797237Sek110237 #define ARC_STORED (1 << 19) /* has been store()d to */ 480789Sahrens 4811544Seschrock #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 482789Sahrens #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 483789Sahrens #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 484789Sahrens #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 4851544Seschrock #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 4865450Sbrendan #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 4877237Sek110237 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 4886987Sbrendan #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 4896987Sbrendan (hdr)->b_l2hdr != NULL) 4905450Sbrendan #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 4915450Sbrendan #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 4925450Sbrendan #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 493789Sahrens 494789Sahrens /* 4956018Sbrendan * Other sizes 4966018Sbrendan */ 4976018Sbrendan 4986018Sbrendan #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 4996018Sbrendan #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 5006018Sbrendan 5016018Sbrendan /* 502789Sahrens * Hash table routines 503789Sahrens */ 504789Sahrens 505789Sahrens #define HT_LOCK_PAD 64 506789Sahrens 507789Sahrens struct ht_lock { 508789Sahrens kmutex_t ht_lock; 509789Sahrens #ifdef _KERNEL 510789Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 511789Sahrens #endif 512789Sahrens }; 513789Sahrens 514789Sahrens #define BUF_LOCKS 256 515789Sahrens typedef struct buf_hash_table { 516789Sahrens uint64_t ht_mask; 517789Sahrens arc_buf_hdr_t **ht_table; 518789Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 519789Sahrens } buf_hash_table_t; 520789Sahrens 521789Sahrens static buf_hash_table_t buf_hash_table; 522789Sahrens 523789Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 524789Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 525789Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 526789Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 527789Sahrens #define HDR_LOCK(buf) \ 528789Sahrens (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth))) 529789Sahrens 530789Sahrens uint64_t zfs_crc64_table[256]; 531789Sahrens 5325450Sbrendan /* 5335450Sbrendan * Level 2 ARC 5345450Sbrendan */ 5355450Sbrendan 5365450Sbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 5375450Sbrendan #define L2ARC_HEADROOM 4 /* num of writes */ 5385450Sbrendan #define L2ARC_FEED_SECS 1 /* caching interval */ 5395450Sbrendan 5405450Sbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 5415450Sbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 5425450Sbrendan 5435450Sbrendan /* 5445450Sbrendan * L2ARC Performance Tunables 5455450Sbrendan */ 5465450Sbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 5476987Sbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 5485450Sbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 5495450Sbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 5505450Sbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 5515450Sbrendan 5525450Sbrendan /* 5535450Sbrendan * L2ARC Internals 5545450Sbrendan */ 5555450Sbrendan typedef struct l2arc_dev { 5565450Sbrendan vdev_t *l2ad_vdev; /* vdev */ 5575450Sbrendan spa_t *l2ad_spa; /* spa */ 5585450Sbrendan uint64_t l2ad_hand; /* next write location */ 5595450Sbrendan uint64_t l2ad_write; /* desired write size, bytes */ 5606987Sbrendan uint64_t l2ad_boost; /* warmup write boost, bytes */ 5615450Sbrendan uint64_t l2ad_start; /* first addr on device */ 5625450Sbrendan uint64_t l2ad_end; /* last addr on device */ 5635450Sbrendan uint64_t l2ad_evict; /* last addr eviction reached */ 5645450Sbrendan boolean_t l2ad_first; /* first sweep through */ 5655450Sbrendan list_t *l2ad_buflist; /* buffer list */ 5665450Sbrendan list_node_t l2ad_node; /* device list node */ 5675450Sbrendan } l2arc_dev_t; 5685450Sbrendan 5695450Sbrendan static list_t L2ARC_dev_list; /* device list */ 5705450Sbrendan static list_t *l2arc_dev_list; /* device list pointer */ 5715450Sbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 5725450Sbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 5735450Sbrendan static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 5745450Sbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 5755450Sbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 5765450Sbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 5775450Sbrendan static uint64_t l2arc_ndev; /* number of devices */ 5785450Sbrendan 5795450Sbrendan typedef struct l2arc_read_callback { 5805450Sbrendan arc_buf_t *l2rcb_buf; /* read buffer */ 5815450Sbrendan spa_t *l2rcb_spa; /* spa */ 5825450Sbrendan blkptr_t l2rcb_bp; /* original blkptr */ 5835450Sbrendan zbookmark_t l2rcb_zb; /* original bookmark */ 5845450Sbrendan int l2rcb_flags; /* original flags */ 5855450Sbrendan } l2arc_read_callback_t; 5865450Sbrendan 5875450Sbrendan typedef struct l2arc_write_callback { 5885450Sbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 5895450Sbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 5905450Sbrendan } l2arc_write_callback_t; 5915450Sbrendan 5925450Sbrendan struct l2arc_buf_hdr { 5935450Sbrendan /* protected by arc_buf_hdr mutex */ 5945450Sbrendan l2arc_dev_t *b_dev; /* L2ARC device */ 5955450Sbrendan daddr_t b_daddr; /* disk address, offset byte */ 5965450Sbrendan }; 5975450Sbrendan 5985450Sbrendan typedef struct l2arc_data_free { 5995450Sbrendan /* protected by l2arc_free_on_write_mtx */ 6005450Sbrendan void *l2df_data; 6015450Sbrendan size_t l2df_size; 6025450Sbrendan void (*l2df_func)(void *, size_t); 6035450Sbrendan list_node_t l2df_list_node; 6045450Sbrendan } l2arc_data_free_t; 6055450Sbrendan 6065450Sbrendan static kmutex_t l2arc_feed_thr_lock; 6075450Sbrendan static kcondvar_t l2arc_feed_thr_cv; 6085450Sbrendan static uint8_t l2arc_thread_exit; 6095450Sbrendan 6105450Sbrendan static void l2arc_read_done(zio_t *zio); 6115450Sbrendan static void l2arc_hdr_stat_add(void); 6125450Sbrendan static void l2arc_hdr_stat_remove(void); 6135450Sbrendan 614789Sahrens static uint64_t 6157046Sahrens buf_hash(spa_t *spa, const dva_t *dva, uint64_t birth) 616789Sahrens { 617789Sahrens uintptr_t spav = (uintptr_t)spa; 618789Sahrens uint8_t *vdva = (uint8_t *)dva; 619789Sahrens uint64_t crc = -1ULL; 620789Sahrens int i; 621789Sahrens 622789Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 623789Sahrens 624789Sahrens for (i = 0; i < sizeof (dva_t); i++) 625789Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 626789Sahrens 627789Sahrens crc ^= (spav>>8) ^ birth; 628789Sahrens 629789Sahrens return (crc); 630789Sahrens } 631789Sahrens 632789Sahrens #define BUF_EMPTY(buf) \ 633789Sahrens ((buf)->b_dva.dva_word[0] == 0 && \ 634789Sahrens (buf)->b_dva.dva_word[1] == 0 && \ 635789Sahrens (buf)->b_birth == 0) 636789Sahrens 637789Sahrens #define BUF_EQUAL(spa, dva, birth, buf) \ 638789Sahrens ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 639789Sahrens ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 640789Sahrens ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 641789Sahrens 642789Sahrens static arc_buf_hdr_t * 6437046Sahrens buf_hash_find(spa_t *spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 644789Sahrens { 645789Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 646789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 647789Sahrens arc_buf_hdr_t *buf; 648789Sahrens 649789Sahrens mutex_enter(hash_lock); 650789Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 651789Sahrens buf = buf->b_hash_next) { 652789Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 653789Sahrens *lockp = hash_lock; 654789Sahrens return (buf); 655789Sahrens } 656789Sahrens } 657789Sahrens mutex_exit(hash_lock); 658789Sahrens *lockp = NULL; 659789Sahrens return (NULL); 660789Sahrens } 661789Sahrens 662789Sahrens /* 663789Sahrens * Insert an entry into the hash table. If there is already an element 664789Sahrens * equal to elem in the hash table, then the already existing element 665789Sahrens * will be returned and the new element will not be inserted. 666789Sahrens * Otherwise returns NULL. 667789Sahrens */ 668789Sahrens static arc_buf_hdr_t * 669789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 670789Sahrens { 671789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 672789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 673789Sahrens arc_buf_hdr_t *fbuf; 6743403Sbmc uint32_t i; 675789Sahrens 6761544Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 677789Sahrens *lockp = hash_lock; 678789Sahrens mutex_enter(hash_lock); 679789Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 680789Sahrens fbuf = fbuf->b_hash_next, i++) { 681789Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 682789Sahrens return (fbuf); 683789Sahrens } 684789Sahrens 685789Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 686789Sahrens buf_hash_table.ht_table[idx] = buf; 6871544Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 688789Sahrens 689789Sahrens /* collect some hash table performance data */ 690789Sahrens if (i > 0) { 6913403Sbmc ARCSTAT_BUMP(arcstat_hash_collisions); 692789Sahrens if (i == 1) 6933403Sbmc ARCSTAT_BUMP(arcstat_hash_chains); 6943403Sbmc 6953403Sbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 696789Sahrens } 6973403Sbmc 6983403Sbmc ARCSTAT_BUMP(arcstat_hash_elements); 6993403Sbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 700789Sahrens 701789Sahrens return (NULL); 702789Sahrens } 703789Sahrens 704789Sahrens static void 705789Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 706789Sahrens { 707789Sahrens arc_buf_hdr_t *fbuf, **bufp; 708789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 709789Sahrens 710789Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 7111544Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 712789Sahrens 713789Sahrens bufp = &buf_hash_table.ht_table[idx]; 714789Sahrens while ((fbuf = *bufp) != buf) { 715789Sahrens ASSERT(fbuf != NULL); 716789Sahrens bufp = &fbuf->b_hash_next; 717789Sahrens } 718789Sahrens *bufp = buf->b_hash_next; 719789Sahrens buf->b_hash_next = NULL; 7201544Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 721789Sahrens 722789Sahrens /* collect some hash table performance data */ 7233403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 7243403Sbmc 725789Sahrens if (buf_hash_table.ht_table[idx] && 726789Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 7273403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 728789Sahrens } 729789Sahrens 730789Sahrens /* 731789Sahrens * Global data structures and functions for the buf kmem cache. 732789Sahrens */ 733789Sahrens static kmem_cache_t *hdr_cache; 734789Sahrens static kmem_cache_t *buf_cache; 735789Sahrens 736789Sahrens static void 737789Sahrens buf_fini(void) 738789Sahrens { 739789Sahrens int i; 740789Sahrens 741789Sahrens kmem_free(buf_hash_table.ht_table, 742789Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 743789Sahrens for (i = 0; i < BUF_LOCKS; i++) 744789Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 745789Sahrens kmem_cache_destroy(hdr_cache); 746789Sahrens kmem_cache_destroy(buf_cache); 747789Sahrens } 748789Sahrens 749789Sahrens /* 750789Sahrens * Constructor callback - called when the cache is empty 751789Sahrens * and a new buf is requested. 752789Sahrens */ 753789Sahrens /* ARGSUSED */ 754789Sahrens static int 755789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 756789Sahrens { 757789Sahrens arc_buf_hdr_t *buf = vbuf; 758789Sahrens 759789Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 760789Sahrens refcount_create(&buf->b_refcnt); 761789Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 7624831Sgw25295 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 7637046Sahrens rw_init(&buf->b_datalock, NULL, RW_DEFAULT, NULL); 7645450Sbrendan 7656018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 766789Sahrens return (0); 767789Sahrens } 768789Sahrens 769789Sahrens /* 770789Sahrens * Destructor callback - called when a cached buf is 771789Sahrens * no longer required. 772789Sahrens */ 773789Sahrens /* ARGSUSED */ 774789Sahrens static void 775789Sahrens hdr_dest(void *vbuf, void *unused) 776789Sahrens { 777789Sahrens arc_buf_hdr_t *buf = vbuf; 778789Sahrens 779789Sahrens refcount_destroy(&buf->b_refcnt); 780789Sahrens cv_destroy(&buf->b_cv); 7814831Sgw25295 mutex_destroy(&buf->b_freeze_lock); 7827046Sahrens rw_destroy(&buf->b_datalock); 7835450Sbrendan 7846018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 785789Sahrens } 786789Sahrens 787789Sahrens /* 788789Sahrens * Reclaim callback -- invoked when memory is low. 789789Sahrens */ 790789Sahrens /* ARGSUSED */ 791789Sahrens static void 792789Sahrens hdr_recl(void *unused) 793789Sahrens { 794789Sahrens dprintf("hdr_recl called\n"); 7953158Smaybee /* 7963158Smaybee * umem calls the reclaim func when we destroy the buf cache, 7973158Smaybee * which is after we do arc_fini(). 7983158Smaybee */ 7993158Smaybee if (!arc_dead) 8003158Smaybee cv_signal(&arc_reclaim_thr_cv); 801789Sahrens } 802789Sahrens 803789Sahrens static void 804789Sahrens buf_init(void) 805789Sahrens { 806789Sahrens uint64_t *ct; 8071544Seschrock uint64_t hsize = 1ULL << 12; 808789Sahrens int i, j; 809789Sahrens 810789Sahrens /* 811789Sahrens * The hash table is big enough to fill all of physical memory 8121544Seschrock * with an average 64K block size. The table will take up 8131544Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 814789Sahrens */ 8151544Seschrock while (hsize * 65536 < physmem * PAGESIZE) 816789Sahrens hsize <<= 1; 8171544Seschrock retry: 818789Sahrens buf_hash_table.ht_mask = hsize - 1; 8191544Seschrock buf_hash_table.ht_table = 8201544Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 8211544Seschrock if (buf_hash_table.ht_table == NULL) { 8221544Seschrock ASSERT(hsize > (1ULL << 8)); 8231544Seschrock hsize >>= 1; 8241544Seschrock goto retry; 8251544Seschrock } 826789Sahrens 827789Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 828789Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 829789Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 830789Sahrens 0, NULL, NULL, NULL, NULL, NULL, 0); 831789Sahrens 832789Sahrens for (i = 0; i < 256; i++) 833789Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 834789Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 835789Sahrens 836789Sahrens for (i = 0; i < BUF_LOCKS; i++) { 837789Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 838789Sahrens NULL, MUTEX_DEFAULT, NULL); 839789Sahrens } 840789Sahrens } 841789Sahrens 842789Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 843789Sahrens 844789Sahrens static void 8453093Sahrens arc_cksum_verify(arc_buf_t *buf) 8463093Sahrens { 8473093Sahrens zio_cksum_t zc; 8483093Sahrens 8493312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 8503093Sahrens return; 8513093Sahrens 8523093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8533265Sahrens if (buf->b_hdr->b_freeze_cksum == NULL || 8543265Sahrens (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 8553093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8563093Sahrens return; 8573093Sahrens } 8583093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8593093Sahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 8603093Sahrens panic("buffer modified while frozen!"); 8613093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8623093Sahrens } 8633093Sahrens 8645450Sbrendan static int 8655450Sbrendan arc_cksum_equal(arc_buf_t *buf) 8665450Sbrendan { 8675450Sbrendan zio_cksum_t zc; 8685450Sbrendan int equal; 8695450Sbrendan 8705450Sbrendan mutex_enter(&buf->b_hdr->b_freeze_lock); 8715450Sbrendan fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8725450Sbrendan equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 8735450Sbrendan mutex_exit(&buf->b_hdr->b_freeze_lock); 8745450Sbrendan 8755450Sbrendan return (equal); 8765450Sbrendan } 8775450Sbrendan 8783093Sahrens static void 8795450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force) 8803093Sahrens { 8815450Sbrendan if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 8823093Sahrens return; 8833093Sahrens 8843093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8853093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 8863093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8873093Sahrens return; 8883093Sahrens } 8893093Sahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 8903093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 8913093Sahrens buf->b_hdr->b_freeze_cksum); 8923093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8933093Sahrens } 8943093Sahrens 8953093Sahrens void 8963093Sahrens arc_buf_thaw(arc_buf_t *buf) 8973093Sahrens { 8985450Sbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 8995450Sbrendan if (buf->b_hdr->b_state != arc_anon) 9005450Sbrendan panic("modifying non-anon buffer!"); 9015450Sbrendan if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 9025450Sbrendan panic("modifying buffer while i/o in progress!"); 9035450Sbrendan arc_cksum_verify(buf); 9045450Sbrendan } 9055450Sbrendan 9063093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9073093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9083093Sahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 9093093Sahrens buf->b_hdr->b_freeze_cksum = NULL; 9103093Sahrens } 9113093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9123093Sahrens } 9133093Sahrens 9143093Sahrens void 9153093Sahrens arc_buf_freeze(arc_buf_t *buf) 9163093Sahrens { 9173312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 9183312Sahrens return; 9193312Sahrens 9203093Sahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 9213403Sbmc buf->b_hdr->b_state == arc_anon); 9225450Sbrendan arc_cksum_compute(buf, B_FALSE); 9233093Sahrens } 9243093Sahrens 9253093Sahrens static void 926789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 927789Sahrens { 928789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 929789Sahrens 930789Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 9313403Sbmc (ab->b_state != arc_anon)) { 9323700Sek110237 uint64_t delta = ab->b_size * ab->b_datacnt; 9334309Smaybee list_t *list = &ab->b_state->arcs_list[ab->b_type]; 9344309Smaybee uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 935789Sahrens 9363403Sbmc ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx)); 9373403Sbmc mutex_enter(&ab->b_state->arcs_mtx); 938789Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 9394309Smaybee list_remove(list, ab); 9401544Seschrock if (GHOST_STATE(ab->b_state)) { 9411544Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 9421544Seschrock ASSERT3P(ab->b_buf, ==, NULL); 9431544Seschrock delta = ab->b_size; 9441544Seschrock } 9451544Seschrock ASSERT(delta > 0); 9464309Smaybee ASSERT3U(*size, >=, delta); 9474309Smaybee atomic_add_64(size, -delta); 9483403Sbmc mutex_exit(&ab->b_state->arcs_mtx); 9497046Sahrens /* remove the prefetch flag if we get a reference */ 9502391Smaybee if (ab->b_flags & ARC_PREFETCH) 9512391Smaybee ab->b_flags &= ~ARC_PREFETCH; 952789Sahrens } 953789Sahrens } 954789Sahrens 955789Sahrens static int 956789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 957789Sahrens { 958789Sahrens int cnt; 9593403Sbmc arc_state_t *state = ab->b_state; 960789Sahrens 9613403Sbmc ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 9623403Sbmc ASSERT(!GHOST_STATE(state)); 963789Sahrens 964789Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 9653403Sbmc (state != arc_anon)) { 9664309Smaybee uint64_t *size = &state->arcs_lsize[ab->b_type]; 9674309Smaybee 9683403Sbmc ASSERT(!MUTEX_HELD(&state->arcs_mtx)); 9693403Sbmc mutex_enter(&state->arcs_mtx); 970789Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 9714309Smaybee list_insert_head(&state->arcs_list[ab->b_type], ab); 9721544Seschrock ASSERT(ab->b_datacnt > 0); 9734309Smaybee atomic_add_64(size, ab->b_size * ab->b_datacnt); 9743403Sbmc mutex_exit(&state->arcs_mtx); 975789Sahrens } 976789Sahrens return (cnt); 977789Sahrens } 978789Sahrens 979789Sahrens /* 980789Sahrens * Move the supplied buffer to the indicated state. The mutex 981789Sahrens * for the buffer must be held by the caller. 982789Sahrens */ 983789Sahrens static void 9841544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 985789Sahrens { 9861544Seschrock arc_state_t *old_state = ab->b_state; 9873700Sek110237 int64_t refcnt = refcount_count(&ab->b_refcnt); 9883700Sek110237 uint64_t from_delta, to_delta; 989789Sahrens 990789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 9911544Seschrock ASSERT(new_state != old_state); 9921544Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 9931544Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 9941544Seschrock 9951544Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 996789Sahrens 997789Sahrens /* 998789Sahrens * If this buffer is evictable, transfer it from the 999789Sahrens * old state list to the new state list. 1000789Sahrens */ 10011544Seschrock if (refcnt == 0) { 10023403Sbmc if (old_state != arc_anon) { 10033403Sbmc int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx); 10044309Smaybee uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 10051544Seschrock 10061544Seschrock if (use_mutex) 10073403Sbmc mutex_enter(&old_state->arcs_mtx); 10081544Seschrock 10091544Seschrock ASSERT(list_link_active(&ab->b_arc_node)); 10104309Smaybee list_remove(&old_state->arcs_list[ab->b_type], ab); 1011789Sahrens 10122391Smaybee /* 10132391Smaybee * If prefetching out of the ghost cache, 10142391Smaybee * we will have a non-null datacnt. 10152391Smaybee */ 10162391Smaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 10172391Smaybee /* ghost elements have a ghost size */ 10181544Seschrock ASSERT(ab->b_buf == NULL); 10191544Seschrock from_delta = ab->b_size; 1020789Sahrens } 10214309Smaybee ASSERT3U(*size, >=, from_delta); 10224309Smaybee atomic_add_64(size, -from_delta); 10231544Seschrock 10241544Seschrock if (use_mutex) 10253403Sbmc mutex_exit(&old_state->arcs_mtx); 1026789Sahrens } 10273403Sbmc if (new_state != arc_anon) { 10283403Sbmc int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx); 10294309Smaybee uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1030789Sahrens 10311544Seschrock if (use_mutex) 10323403Sbmc mutex_enter(&new_state->arcs_mtx); 10331544Seschrock 10344309Smaybee list_insert_head(&new_state->arcs_list[ab->b_type], ab); 10351544Seschrock 10361544Seschrock /* ghost elements have a ghost size */ 10371544Seschrock if (GHOST_STATE(new_state)) { 10381544Seschrock ASSERT(ab->b_datacnt == 0); 10391544Seschrock ASSERT(ab->b_buf == NULL); 10401544Seschrock to_delta = ab->b_size; 10411544Seschrock } 10424309Smaybee atomic_add_64(size, to_delta); 10431544Seschrock 10441544Seschrock if (use_mutex) 10453403Sbmc mutex_exit(&new_state->arcs_mtx); 1046789Sahrens } 1047789Sahrens } 1048789Sahrens 1049789Sahrens ASSERT(!BUF_EMPTY(ab)); 10505450Sbrendan if (new_state == arc_anon) { 1051789Sahrens buf_hash_remove(ab); 1052789Sahrens } 1053789Sahrens 10541544Seschrock /* adjust state sizes */ 10551544Seschrock if (to_delta) 10563403Sbmc atomic_add_64(&new_state->arcs_size, to_delta); 10571544Seschrock if (from_delta) { 10583403Sbmc ASSERT3U(old_state->arcs_size, >=, from_delta); 10593403Sbmc atomic_add_64(&old_state->arcs_size, -from_delta); 1060789Sahrens } 1061789Sahrens ab->b_state = new_state; 10625450Sbrendan 10635450Sbrendan /* adjust l2arc hdr stats */ 10645450Sbrendan if (new_state == arc_l2c_only) 10655450Sbrendan l2arc_hdr_stat_add(); 10665450Sbrendan else if (old_state == arc_l2c_only) 10675450Sbrendan l2arc_hdr_stat_remove(); 1068789Sahrens } 1069789Sahrens 10704309Smaybee void 10714309Smaybee arc_space_consume(uint64_t space) 10724309Smaybee { 10734309Smaybee atomic_add_64(&arc_meta_used, space); 10744309Smaybee atomic_add_64(&arc_size, space); 10754309Smaybee } 10764309Smaybee 10774309Smaybee void 10784309Smaybee arc_space_return(uint64_t space) 10794309Smaybee { 10804309Smaybee ASSERT(arc_meta_used >= space); 10814309Smaybee if (arc_meta_max < arc_meta_used) 10824309Smaybee arc_meta_max = arc_meta_used; 10834309Smaybee atomic_add_64(&arc_meta_used, -space); 10844309Smaybee ASSERT(arc_size >= space); 10854309Smaybee atomic_add_64(&arc_size, -space); 10864309Smaybee } 10874309Smaybee 10884309Smaybee void * 10894309Smaybee arc_data_buf_alloc(uint64_t size) 10904309Smaybee { 10914309Smaybee if (arc_evict_needed(ARC_BUFC_DATA)) 10924309Smaybee cv_signal(&arc_reclaim_thr_cv); 10934309Smaybee atomic_add_64(&arc_size, size); 10944309Smaybee return (zio_data_buf_alloc(size)); 10954309Smaybee } 10964309Smaybee 10974309Smaybee void 10984309Smaybee arc_data_buf_free(void *buf, uint64_t size) 10994309Smaybee { 11004309Smaybee zio_data_buf_free(buf, size); 11014309Smaybee ASSERT(arc_size >= size); 11024309Smaybee atomic_add_64(&arc_size, -size); 11034309Smaybee } 11044309Smaybee 1105789Sahrens arc_buf_t * 11063290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1107789Sahrens { 1108789Sahrens arc_buf_hdr_t *hdr; 1109789Sahrens arc_buf_t *buf; 1110789Sahrens 1111789Sahrens ASSERT3U(size, >, 0); 11126245Smaybee hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1113789Sahrens ASSERT(BUF_EMPTY(hdr)); 1114789Sahrens hdr->b_size = size; 11153290Sjohansen hdr->b_type = type; 1116789Sahrens hdr->b_spa = spa; 11173403Sbmc hdr->b_state = arc_anon; 1118789Sahrens hdr->b_arc_access = 0; 11196245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1120789Sahrens buf->b_hdr = hdr; 11212688Smaybee buf->b_data = NULL; 11221544Seschrock buf->b_efunc = NULL; 11231544Seschrock buf->b_private = NULL; 1124789Sahrens buf->b_next = NULL; 1125789Sahrens hdr->b_buf = buf; 11262688Smaybee arc_get_data_buf(buf); 11271544Seschrock hdr->b_datacnt = 1; 1128789Sahrens hdr->b_flags = 0; 1129789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1130789Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 1131789Sahrens 1132789Sahrens return (buf); 1133789Sahrens } 1134789Sahrens 11352688Smaybee static arc_buf_t * 11362688Smaybee arc_buf_clone(arc_buf_t *from) 11371544Seschrock { 11382688Smaybee arc_buf_t *buf; 11392688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 11402688Smaybee uint64_t size = hdr->b_size; 11411544Seschrock 11426245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 11432688Smaybee buf->b_hdr = hdr; 11442688Smaybee buf->b_data = NULL; 11452688Smaybee buf->b_efunc = NULL; 11462688Smaybee buf->b_private = NULL; 11472688Smaybee buf->b_next = hdr->b_buf; 11482688Smaybee hdr->b_buf = buf; 11492688Smaybee arc_get_data_buf(buf); 11502688Smaybee bcopy(from->b_data, buf->b_data, size); 11512688Smaybee hdr->b_datacnt += 1; 11522688Smaybee return (buf); 11531544Seschrock } 11541544Seschrock 11551544Seschrock void 11561544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 11571544Seschrock { 11582887Smaybee arc_buf_hdr_t *hdr; 11591544Seschrock kmutex_t *hash_lock; 11601544Seschrock 11612724Smaybee /* 11622724Smaybee * Check to see if this buffer is currently being evicted via 11632887Smaybee * arc_do_user_evicts(). 11642724Smaybee */ 11652887Smaybee mutex_enter(&arc_eviction_mtx); 11662887Smaybee hdr = buf->b_hdr; 11672887Smaybee if (hdr == NULL) { 11682887Smaybee mutex_exit(&arc_eviction_mtx); 11692724Smaybee return; 11702887Smaybee } 11712887Smaybee hash_lock = HDR_LOCK(hdr); 11722887Smaybee mutex_exit(&arc_eviction_mtx); 11732724Smaybee 11742724Smaybee mutex_enter(hash_lock); 11751544Seschrock if (buf->b_data == NULL) { 11761544Seschrock /* 11771544Seschrock * This buffer is evicted. 11781544Seschrock */ 11792724Smaybee mutex_exit(hash_lock); 11801544Seschrock return; 11811544Seschrock } 11821544Seschrock 11832724Smaybee ASSERT(buf->b_hdr == hdr); 11843403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 11851544Seschrock add_reference(hdr, hash_lock, tag); 11862688Smaybee arc_access(hdr, hash_lock); 11872688Smaybee mutex_exit(hash_lock); 11883403Sbmc ARCSTAT_BUMP(arcstat_hits); 11893403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 11903403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 11913403Sbmc data, metadata, hits); 11921544Seschrock } 11931544Seschrock 11945450Sbrendan /* 11955450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 11965450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 11975450Sbrendan */ 11985450Sbrendan static void 11995450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 12005450Sbrendan void *data, size_t size) 12015450Sbrendan { 12025450Sbrendan if (HDR_L2_WRITING(hdr)) { 12035450Sbrendan l2arc_data_free_t *df; 12045450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 12055450Sbrendan df->l2df_data = data; 12065450Sbrendan df->l2df_size = size; 12075450Sbrendan df->l2df_func = free_func; 12085450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 12095450Sbrendan list_insert_head(l2arc_free_on_write, df); 12105450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 12115450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 12125450Sbrendan } else { 12135450Sbrendan free_func(data, size); 12145450Sbrendan } 12155450Sbrendan } 12165450Sbrendan 1217789Sahrens static void 12182688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 12191544Seschrock { 12201544Seschrock arc_buf_t **bufp; 12211544Seschrock 12221544Seschrock /* free up data associated with the buf */ 12231544Seschrock if (buf->b_data) { 12241544Seschrock arc_state_t *state = buf->b_hdr->b_state; 12251544Seschrock uint64_t size = buf->b_hdr->b_size; 12263290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 12271544Seschrock 12283093Sahrens arc_cksum_verify(buf); 12292688Smaybee if (!recycle) { 12303290Sjohansen if (type == ARC_BUFC_METADATA) { 12315450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 12325450Sbrendan buf->b_data, size); 12334309Smaybee arc_space_return(size); 12343290Sjohansen } else { 12353290Sjohansen ASSERT(type == ARC_BUFC_DATA); 12365450Sbrendan arc_buf_data_free(buf->b_hdr, 12375450Sbrendan zio_data_buf_free, buf->b_data, size); 12384309Smaybee atomic_add_64(&arc_size, -size); 12393290Sjohansen } 12402688Smaybee } 12411544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 12424309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 12434309Smaybee 12441544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 12453403Sbmc ASSERT(state != arc_anon); 12464309Smaybee 12474309Smaybee ASSERT3U(*cnt, >=, size); 12484309Smaybee atomic_add_64(cnt, -size); 12491544Seschrock } 12503403Sbmc ASSERT3U(state->arcs_size, >=, size); 12513403Sbmc atomic_add_64(&state->arcs_size, -size); 12521544Seschrock buf->b_data = NULL; 12531544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 12541544Seschrock buf->b_hdr->b_datacnt -= 1; 12551544Seschrock } 12561544Seschrock 12571544Seschrock /* only remove the buf if requested */ 12581544Seschrock if (!all) 12591544Seschrock return; 12601544Seschrock 12611544Seschrock /* remove the buf from the hdr list */ 12621544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 12631544Seschrock continue; 12641544Seschrock *bufp = buf->b_next; 12651544Seschrock 12661544Seschrock ASSERT(buf->b_efunc == NULL); 12671544Seschrock 12681544Seschrock /* clean up the buf */ 12691544Seschrock buf->b_hdr = NULL; 12701544Seschrock kmem_cache_free(buf_cache, buf); 12711544Seschrock } 12721544Seschrock 12731544Seschrock static void 12741544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1275789Sahrens { 1276789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 12773403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 12781544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 12797046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 1280789Sahrens 12815450Sbrendan if (hdr->b_l2hdr != NULL) { 12825450Sbrendan if (!MUTEX_HELD(&l2arc_buflist_mtx)) { 12835450Sbrendan /* 12845450Sbrendan * To prevent arc_free() and l2arc_evict() from 12855450Sbrendan * attempting to free the same buffer at the same time, 12865450Sbrendan * a FREE_IN_PROGRESS flag is given to arc_free() to 12875450Sbrendan * give it priority. l2arc_evict() can't destroy this 12885450Sbrendan * header while we are waiting on l2arc_buflist_mtx. 12897361SBrendan.Gregg@Sun.COM * 12907361SBrendan.Gregg@Sun.COM * The hdr may be removed from l2ad_buflist before we 12917361SBrendan.Gregg@Sun.COM * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 12925450Sbrendan */ 12935450Sbrendan mutex_enter(&l2arc_buflist_mtx); 12947361SBrendan.Gregg@Sun.COM if (hdr->b_l2hdr != NULL) { 12957361SBrendan.Gregg@Sun.COM list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, 12967361SBrendan.Gregg@Sun.COM hdr); 12977361SBrendan.Gregg@Sun.COM } 12985450Sbrendan mutex_exit(&l2arc_buflist_mtx); 12995450Sbrendan } else { 13005450Sbrendan list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr); 13015450Sbrendan } 13025450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 13035450Sbrendan kmem_free(hdr->b_l2hdr, sizeof (l2arc_buf_hdr_t)); 13045450Sbrendan if (hdr->b_state == arc_l2c_only) 13055450Sbrendan l2arc_hdr_stat_remove(); 13065450Sbrendan hdr->b_l2hdr = NULL; 13075450Sbrendan } 13085450Sbrendan 1309789Sahrens if (!BUF_EMPTY(hdr)) { 13101544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1311789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1312789Sahrens hdr->b_birth = 0; 1313789Sahrens hdr->b_cksum0 = 0; 1314789Sahrens } 13151544Seschrock while (hdr->b_buf) { 1316789Sahrens arc_buf_t *buf = hdr->b_buf; 1317789Sahrens 13181544Seschrock if (buf->b_efunc) { 13191544Seschrock mutex_enter(&arc_eviction_mtx); 13201544Seschrock ASSERT(buf->b_hdr != NULL); 13212688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 13221544Seschrock hdr->b_buf = buf->b_next; 13232887Smaybee buf->b_hdr = &arc_eviction_hdr; 13241544Seschrock buf->b_next = arc_eviction_list; 13251544Seschrock arc_eviction_list = buf; 13261544Seschrock mutex_exit(&arc_eviction_mtx); 13271544Seschrock } else { 13282688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 13291544Seschrock } 1330789Sahrens } 13313093Sahrens if (hdr->b_freeze_cksum != NULL) { 13323093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 13333093Sahrens hdr->b_freeze_cksum = NULL; 13343093Sahrens } 13351544Seschrock 1336789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1337789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1338789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1339789Sahrens kmem_cache_free(hdr_cache, hdr); 1340789Sahrens } 1341789Sahrens 1342789Sahrens void 1343789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1344789Sahrens { 1345789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 13463403Sbmc int hashed = hdr->b_state != arc_anon; 13471544Seschrock 13481544Seschrock ASSERT(buf->b_efunc == NULL); 13491544Seschrock ASSERT(buf->b_data != NULL); 13501544Seschrock 13511544Seschrock if (hashed) { 13521544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 13531544Seschrock 13541544Seschrock mutex_enter(hash_lock); 13551544Seschrock (void) remove_reference(hdr, hash_lock, tag); 13561544Seschrock if (hdr->b_datacnt > 1) 13572688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 13581544Seschrock else 13591544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 13601544Seschrock mutex_exit(hash_lock); 13611544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 13621544Seschrock int destroy_hdr; 13631544Seschrock /* 13641544Seschrock * We are in the middle of an async write. Don't destroy 13651544Seschrock * this buffer unless the write completes before we finish 13661544Seschrock * decrementing the reference count. 13671544Seschrock */ 13681544Seschrock mutex_enter(&arc_eviction_mtx); 13691544Seschrock (void) remove_reference(hdr, NULL, tag); 13701544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 13711544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 13721544Seschrock mutex_exit(&arc_eviction_mtx); 13731544Seschrock if (destroy_hdr) 13741544Seschrock arc_hdr_destroy(hdr); 13751544Seschrock } else { 13761544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 13771544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 13782688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 13791544Seschrock } else { 13801544Seschrock arc_hdr_destroy(hdr); 13811544Seschrock } 13821544Seschrock } 13831544Seschrock } 13841544Seschrock 13851544Seschrock int 13861544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 13871544Seschrock { 13881544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1389789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 13901544Seschrock int no_callback = (buf->b_efunc == NULL); 13911544Seschrock 13923403Sbmc if (hdr->b_state == arc_anon) { 13931544Seschrock arc_buf_free(buf, tag); 13941544Seschrock return (no_callback); 13951544Seschrock } 1396789Sahrens 1397789Sahrens mutex_enter(hash_lock); 13983403Sbmc ASSERT(hdr->b_state != arc_anon); 13991544Seschrock ASSERT(buf->b_data != NULL); 1400789Sahrens 14011544Seschrock (void) remove_reference(hdr, hash_lock, tag); 14021544Seschrock if (hdr->b_datacnt > 1) { 14031544Seschrock if (no_callback) 14042688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14051544Seschrock } else if (no_callback) { 14061544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 14071544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1408789Sahrens } 14091544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 14101544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1411789Sahrens mutex_exit(hash_lock); 14121544Seschrock return (no_callback); 1413789Sahrens } 1414789Sahrens 1415789Sahrens int 1416789Sahrens arc_buf_size(arc_buf_t *buf) 1417789Sahrens { 1418789Sahrens return (buf->b_hdr->b_size); 1419789Sahrens } 1420789Sahrens 1421789Sahrens /* 1422789Sahrens * Evict buffers from list until we've removed the specified number of 1423789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 14242688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 14252688Smaybee * - look for a buffer to evict that is `bytes' long. 14262688Smaybee * - return the data block from this buffer rather than freeing it. 14272688Smaybee * This flag is used by callers that are trying to make space for a 14282688Smaybee * new buffer in a full arc cache. 14295642Smaybee * 14305642Smaybee * This function makes a "best effort". It skips over any buffers 14315642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 14325642Smaybee * It may also return without evicting as much space as requested. 1433789Sahrens */ 14342688Smaybee static void * 14355642Smaybee arc_evict(arc_state_t *state, spa_t *spa, int64_t bytes, boolean_t recycle, 14363290Sjohansen arc_buf_contents_t type) 1437789Sahrens { 1438789Sahrens arc_state_t *evicted_state; 14392688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 14402918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 14414309Smaybee list_t *list = &state->arcs_list[type]; 1442789Sahrens kmutex_t *hash_lock; 14432688Smaybee boolean_t have_lock; 14442918Smaybee void *stolen = NULL; 1445789Sahrens 14463403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1447789Sahrens 14483403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1449789Sahrens 14503403Sbmc mutex_enter(&state->arcs_mtx); 14513403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1452789Sahrens 14534309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 14544309Smaybee ab_prev = list_prev(list, ab); 14552391Smaybee /* prefetch buffers have a minimum lifespan */ 14562688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 14575642Smaybee (spa && ab->b_spa != spa) || 14582688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 14592688Smaybee lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) { 14602391Smaybee skipped++; 14612391Smaybee continue; 14622391Smaybee } 14632918Smaybee /* "lookahead" for better eviction candidate */ 14642918Smaybee if (recycle && ab->b_size != bytes && 14652918Smaybee ab_prev && ab_prev->b_size == bytes) 14662688Smaybee continue; 1467789Sahrens hash_lock = HDR_LOCK(ab); 14682688Smaybee have_lock = MUTEX_HELD(hash_lock); 14692688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1470789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 14711544Seschrock ASSERT(ab->b_datacnt > 0); 14721544Seschrock while (ab->b_buf) { 14731544Seschrock arc_buf_t *buf = ab->b_buf; 14742688Smaybee if (buf->b_data) { 14751544Seschrock bytes_evicted += ab->b_size; 14763290Sjohansen if (recycle && ab->b_type == type && 14775450Sbrendan ab->b_size == bytes && 14785450Sbrendan !HDR_L2_WRITING(ab)) { 14792918Smaybee stolen = buf->b_data; 14802918Smaybee recycle = FALSE; 14812918Smaybee } 14822688Smaybee } 14831544Seschrock if (buf->b_efunc) { 14841544Seschrock mutex_enter(&arc_eviction_mtx); 14852918Smaybee arc_buf_destroy(buf, 14862918Smaybee buf->b_data == stolen, FALSE); 14871544Seschrock ab->b_buf = buf->b_next; 14882887Smaybee buf->b_hdr = &arc_eviction_hdr; 14891544Seschrock buf->b_next = arc_eviction_list; 14901544Seschrock arc_eviction_list = buf; 14911544Seschrock mutex_exit(&arc_eviction_mtx); 14921544Seschrock } else { 14932918Smaybee arc_buf_destroy(buf, 14942918Smaybee buf->b_data == stolen, TRUE); 14951544Seschrock } 14961544Seschrock } 14971544Seschrock ASSERT(ab->b_datacnt == 0); 1498789Sahrens arc_change_state(evicted_state, ab, hash_lock); 14991544Seschrock ASSERT(HDR_IN_HASH_TABLE(ab)); 15005450Sbrendan ab->b_flags |= ARC_IN_HASH_TABLE; 15015450Sbrendan ab->b_flags &= ~ARC_BUF_AVAILABLE; 1502789Sahrens DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 15032688Smaybee if (!have_lock) 15042688Smaybee mutex_exit(hash_lock); 15051544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1506789Sahrens break; 1507789Sahrens } else { 15082688Smaybee missed += 1; 1509789Sahrens } 1510789Sahrens } 15113403Sbmc 15123403Sbmc mutex_exit(&evicted_state->arcs_mtx); 15133403Sbmc mutex_exit(&state->arcs_mtx); 1514789Sahrens 1515789Sahrens if (bytes_evicted < bytes) 1516789Sahrens dprintf("only evicted %lld bytes from %x", 1517789Sahrens (longlong_t)bytes_evicted, state); 1518789Sahrens 15192688Smaybee if (skipped) 15203403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 15213403Sbmc 15222688Smaybee if (missed) 15233403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 15243403Sbmc 15254709Smaybee /* 15264709Smaybee * We have just evicted some date into the ghost state, make 15274709Smaybee * sure we also adjust the ghost state size if necessary. 15284709Smaybee */ 15294709Smaybee if (arc_no_grow && 15304709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 15314709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 15324709Smaybee arc_mru_ghost->arcs_size - arc_c; 15334709Smaybee 15344709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 15354709Smaybee int64_t todelete = 15364709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 15375642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 15384709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 15394709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 15404709Smaybee arc_mru_ghost->arcs_size + 15414709Smaybee arc_mfu_ghost->arcs_size - arc_c); 15425642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 15434709Smaybee } 15444709Smaybee } 15454709Smaybee 15462918Smaybee return (stolen); 1547789Sahrens } 1548789Sahrens 1549789Sahrens /* 1550789Sahrens * Remove buffers from list until we've removed the specified number of 1551789Sahrens * bytes. Destroy the buffers that are removed. 1552789Sahrens */ 1553789Sahrens static void 15545642Smaybee arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes) 1555789Sahrens { 1556789Sahrens arc_buf_hdr_t *ab, *ab_prev; 15574309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1558789Sahrens kmutex_t *hash_lock; 15591544Seschrock uint64_t bytes_deleted = 0; 15603700Sek110237 uint64_t bufs_skipped = 0; 1561789Sahrens 15621544Seschrock ASSERT(GHOST_STATE(state)); 1563789Sahrens top: 15643403Sbmc mutex_enter(&state->arcs_mtx); 15654309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15664309Smaybee ab_prev = list_prev(list, ab); 15675642Smaybee if (spa && ab->b_spa != spa) 15685642Smaybee continue; 1569789Sahrens hash_lock = HDR_LOCK(ab); 1570789Sahrens if (mutex_tryenter(hash_lock)) { 15712391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 15721544Seschrock ASSERT(ab->b_buf == NULL); 15733403Sbmc ARCSTAT_BUMP(arcstat_deleted); 15741544Seschrock bytes_deleted += ab->b_size; 15755450Sbrendan 15765450Sbrendan if (ab->b_l2hdr != NULL) { 15775450Sbrendan /* 15785450Sbrendan * This buffer is cached on the 2nd Level ARC; 15795450Sbrendan * don't destroy the header. 15805450Sbrendan */ 15815450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 15825450Sbrendan mutex_exit(hash_lock); 15835450Sbrendan } else { 15845450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 15855450Sbrendan mutex_exit(hash_lock); 15865450Sbrendan arc_hdr_destroy(ab); 15875450Sbrendan } 15885450Sbrendan 1589789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1590789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1591789Sahrens break; 1592789Sahrens } else { 1593789Sahrens if (bytes < 0) { 15943403Sbmc mutex_exit(&state->arcs_mtx); 1595789Sahrens mutex_enter(hash_lock); 1596789Sahrens mutex_exit(hash_lock); 1597789Sahrens goto top; 1598789Sahrens } 1599789Sahrens bufs_skipped += 1; 1600789Sahrens } 1601789Sahrens } 16023403Sbmc mutex_exit(&state->arcs_mtx); 1603789Sahrens 16044309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 16054309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 16064309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 16074309Smaybee goto top; 16084309Smaybee } 16094309Smaybee 1610789Sahrens if (bufs_skipped) { 16113403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1612789Sahrens ASSERT(bytes >= 0); 1613789Sahrens } 1614789Sahrens 1615789Sahrens if (bytes_deleted < bytes) 1616789Sahrens dprintf("only deleted %lld bytes from %p", 1617789Sahrens (longlong_t)bytes_deleted, state); 1618789Sahrens } 1619789Sahrens 1620789Sahrens static void 1621789Sahrens arc_adjust(void) 1622789Sahrens { 16233403Sbmc int64_t top_sz, mru_over, arc_over, todelete; 1624789Sahrens 16255642Smaybee top_sz = arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used; 1626789Sahrens 16274309Smaybee if (top_sz > arc_p && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 16284309Smaybee int64_t toevict = 16294309Smaybee MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], top_sz - arc_p); 16305642Smaybee (void) arc_evict(arc_mru, NULL, toevict, FALSE, ARC_BUFC_DATA); 16314309Smaybee top_sz = arc_anon->arcs_size + arc_mru->arcs_size; 16324309Smaybee } 16334309Smaybee 16344309Smaybee if (top_sz > arc_p && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 16354309Smaybee int64_t toevict = 16364309Smaybee MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], top_sz - arc_p); 16375642Smaybee (void) arc_evict(arc_mru, NULL, toevict, FALSE, 16385642Smaybee ARC_BUFC_METADATA); 16393403Sbmc top_sz = arc_anon->arcs_size + arc_mru->arcs_size; 1640789Sahrens } 1641789Sahrens 16423403Sbmc mru_over = top_sz + arc_mru_ghost->arcs_size - arc_c; 1643789Sahrens 1644789Sahrens if (mru_over > 0) { 16454309Smaybee if (arc_mru_ghost->arcs_size > 0) { 16464309Smaybee todelete = MIN(arc_mru_ghost->arcs_size, mru_over); 16475642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 1648789Sahrens } 1649789Sahrens } 1650789Sahrens 16513403Sbmc if ((arc_over = arc_size - arc_c) > 0) { 16521544Seschrock int64_t tbl_over; 1653789Sahrens 16544309Smaybee if (arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 16554309Smaybee int64_t toevict = 16564309Smaybee MIN(arc_mfu->arcs_lsize[ARC_BUFC_DATA], arc_over); 16575642Smaybee (void) arc_evict(arc_mfu, NULL, toevict, FALSE, 16584309Smaybee ARC_BUFC_DATA); 16594309Smaybee arc_over = arc_size - arc_c; 1660789Sahrens } 1661789Sahrens 16624309Smaybee if (arc_over > 0 && 16634309Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 16644309Smaybee int64_t toevict = 16654309Smaybee MIN(arc_mfu->arcs_lsize[ARC_BUFC_METADATA], 16664309Smaybee arc_over); 16675642Smaybee (void) arc_evict(arc_mfu, NULL, toevict, FALSE, 16684309Smaybee ARC_BUFC_METADATA); 16694309Smaybee } 16704309Smaybee 16714309Smaybee tbl_over = arc_size + arc_mru_ghost->arcs_size + 16724309Smaybee arc_mfu_ghost->arcs_size - arc_c * 2; 16734309Smaybee 16744309Smaybee if (tbl_over > 0 && arc_mfu_ghost->arcs_size > 0) { 16754309Smaybee todelete = MIN(arc_mfu_ghost->arcs_size, tbl_over); 16765642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 1677789Sahrens } 1678789Sahrens } 1679789Sahrens } 1680789Sahrens 16811544Seschrock static void 16821544Seschrock arc_do_user_evicts(void) 16831544Seschrock { 16841544Seschrock mutex_enter(&arc_eviction_mtx); 16851544Seschrock while (arc_eviction_list != NULL) { 16861544Seschrock arc_buf_t *buf = arc_eviction_list; 16871544Seschrock arc_eviction_list = buf->b_next; 16881544Seschrock buf->b_hdr = NULL; 16891544Seschrock mutex_exit(&arc_eviction_mtx); 16901544Seschrock 16911819Smaybee if (buf->b_efunc != NULL) 16921819Smaybee VERIFY(buf->b_efunc(buf) == 0); 16931544Seschrock 16941544Seschrock buf->b_efunc = NULL; 16951544Seschrock buf->b_private = NULL; 16961544Seschrock kmem_cache_free(buf_cache, buf); 16971544Seschrock mutex_enter(&arc_eviction_mtx); 16981544Seschrock } 16991544Seschrock mutex_exit(&arc_eviction_mtx); 17001544Seschrock } 17011544Seschrock 1702789Sahrens /* 17035642Smaybee * Flush all *evictable* data from the cache for the given spa. 1704789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1705789Sahrens */ 1706789Sahrens void 17075642Smaybee arc_flush(spa_t *spa) 1708789Sahrens { 17095642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 17105642Smaybee (void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_DATA); 17115642Smaybee if (spa) 17125642Smaybee break; 17135642Smaybee } 17145642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 17155642Smaybee (void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_METADATA); 17165642Smaybee if (spa) 17175642Smaybee break; 17185642Smaybee } 17195642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 17205642Smaybee (void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_DATA); 17215642Smaybee if (spa) 17225642Smaybee break; 17235642Smaybee } 17245642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 17255642Smaybee (void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_METADATA); 17265642Smaybee if (spa) 17275642Smaybee break; 17285642Smaybee } 17295642Smaybee 17305642Smaybee arc_evict_ghost(arc_mru_ghost, spa, -1); 17315642Smaybee arc_evict_ghost(arc_mfu_ghost, spa, -1); 17321544Seschrock 17331544Seschrock mutex_enter(&arc_reclaim_thr_lock); 17341544Seschrock arc_do_user_evicts(); 17351544Seschrock mutex_exit(&arc_reclaim_thr_lock); 17365642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1737789Sahrens } 1738789Sahrens 17393158Smaybee int arc_shrink_shift = 5; /* log2(fraction of arc to reclaim) */ 17402391Smaybee 1741789Sahrens void 17423158Smaybee arc_shrink(void) 1743789Sahrens { 17443403Sbmc if (arc_c > arc_c_min) { 17453158Smaybee uint64_t to_free; 1746789Sahrens 17472048Sstans #ifdef _KERNEL 17483403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 17492048Sstans #else 17503403Sbmc to_free = arc_c >> arc_shrink_shift; 17512048Sstans #endif 17523403Sbmc if (arc_c > arc_c_min + to_free) 17533403Sbmc atomic_add_64(&arc_c, -to_free); 17543158Smaybee else 17553403Sbmc arc_c = arc_c_min; 17562048Sstans 17573403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 17583403Sbmc if (arc_c > arc_size) 17593403Sbmc arc_c = MAX(arc_size, arc_c_min); 17603403Sbmc if (arc_p > arc_c) 17613403Sbmc arc_p = (arc_c >> 1); 17623403Sbmc ASSERT(arc_c >= arc_c_min); 17633403Sbmc ASSERT((int64_t)arc_p >= 0); 17643158Smaybee } 1765789Sahrens 17663403Sbmc if (arc_size > arc_c) 17673158Smaybee arc_adjust(); 1768789Sahrens } 1769789Sahrens 1770789Sahrens static int 1771789Sahrens arc_reclaim_needed(void) 1772789Sahrens { 1773789Sahrens uint64_t extra; 1774789Sahrens 1775789Sahrens #ifdef _KERNEL 17762048Sstans 17772048Sstans if (needfree) 17782048Sstans return (1); 17792048Sstans 1780789Sahrens /* 1781789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1782789Sahrens */ 1783789Sahrens extra = desfree; 1784789Sahrens 1785789Sahrens /* 1786789Sahrens * check that we're out of range of the pageout scanner. It starts to 1787789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1788789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1789789Sahrens * number of needed free pages. We add extra pages here to make sure 1790789Sahrens * the scanner doesn't start up while we're freeing memory. 1791789Sahrens */ 1792789Sahrens if (freemem < lotsfree + needfree + extra) 1793789Sahrens return (1); 1794789Sahrens 1795789Sahrens /* 1796789Sahrens * check to make sure that swapfs has enough space so that anon 17975450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1798789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1799789Sahrens * swap pages. We also add a bit of extra here just to prevent 1800789Sahrens * circumstances from getting really dire. 1801789Sahrens */ 1802789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1803789Sahrens return (1); 1804789Sahrens 18051936Smaybee #if defined(__i386) 1806789Sahrens /* 1807789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1808789Sahrens * kernel heap space before we ever run out of available physical 1809789Sahrens * memory. Most checks of the size of the heap_area compare against 1810789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1811789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1812789Sahrens * which is so low that it's useless. In this comparison, we seek to 1813789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 18145450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1815789Sahrens * free) 1816789Sahrens */ 1817789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1818789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1819789Sahrens return (1); 1820789Sahrens #endif 1821789Sahrens 1822789Sahrens #else 1823789Sahrens if (spa_get_random(100) == 0) 1824789Sahrens return (1); 1825789Sahrens #endif 1826789Sahrens return (0); 1827789Sahrens } 1828789Sahrens 1829789Sahrens static void 1830789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1831789Sahrens { 1832789Sahrens size_t i; 1833789Sahrens kmem_cache_t *prev_cache = NULL; 18343290Sjohansen kmem_cache_t *prev_data_cache = NULL; 1835789Sahrens extern kmem_cache_t *zio_buf_cache[]; 18363290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 1837789Sahrens 18381484Sek110237 #ifdef _KERNEL 18394309Smaybee if (arc_meta_used >= arc_meta_limit) { 18404309Smaybee /* 18414309Smaybee * We are exceeding our meta-data cache limit. 18424309Smaybee * Purge some DNLC entries to release holds on meta-data. 18434309Smaybee */ 18444309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 18454309Smaybee } 18461936Smaybee #if defined(__i386) 18471936Smaybee /* 18481936Smaybee * Reclaim unused memory from all kmem caches. 18491936Smaybee */ 18501936Smaybee kmem_reap(); 18511936Smaybee #endif 18521484Sek110237 #endif 18531484Sek110237 1854789Sahrens /* 18555450Sbrendan * An aggressive reclamation will shrink the cache size as well as 18561544Seschrock * reap free buffers from the arc kmem caches. 1857789Sahrens */ 1858789Sahrens if (strat == ARC_RECLAIM_AGGR) 18593158Smaybee arc_shrink(); 1860789Sahrens 1861789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 1862789Sahrens if (zio_buf_cache[i] != prev_cache) { 1863789Sahrens prev_cache = zio_buf_cache[i]; 1864789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 1865789Sahrens } 18663290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 18673290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 18683290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 18693290Sjohansen } 1870789Sahrens } 18711544Seschrock kmem_cache_reap_now(buf_cache); 18721544Seschrock kmem_cache_reap_now(hdr_cache); 1873789Sahrens } 1874789Sahrens 1875789Sahrens static void 1876789Sahrens arc_reclaim_thread(void) 1877789Sahrens { 1878789Sahrens clock_t growtime = 0; 1879789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 1880789Sahrens callb_cpr_t cpr; 1881789Sahrens 1882789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 1883789Sahrens 1884789Sahrens mutex_enter(&arc_reclaim_thr_lock); 1885789Sahrens while (arc_thread_exit == 0) { 1886789Sahrens if (arc_reclaim_needed()) { 1887789Sahrens 18883403Sbmc if (arc_no_grow) { 1889789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 1890789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1891789Sahrens } else { 1892789Sahrens last_reclaim = ARC_RECLAIM_CONS; 1893789Sahrens } 1894789Sahrens } else { 18953403Sbmc arc_no_grow = TRUE; 1896789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1897789Sahrens membar_producer(); 1898789Sahrens } 1899789Sahrens 1900789Sahrens /* reset the growth delay for every reclaim */ 1901789Sahrens growtime = lbolt + (arc_grow_retry * hz); 1902789Sahrens 1903789Sahrens arc_kmem_reap_now(last_reclaim); 19046987Sbrendan arc_warm = B_TRUE; 1905789Sahrens 19064309Smaybee } else if (arc_no_grow && lbolt >= growtime) { 19073403Sbmc arc_no_grow = FALSE; 1908789Sahrens } 1909789Sahrens 19103403Sbmc if (2 * arc_c < arc_size + 19113403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 19123298Smaybee arc_adjust(); 19133298Smaybee 19141544Seschrock if (arc_eviction_list != NULL) 19151544Seschrock arc_do_user_evicts(); 19161544Seschrock 1917789Sahrens /* block until needed, or one second, whichever is shorter */ 1918789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 1919789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 1920789Sahrens &arc_reclaim_thr_lock, (lbolt + hz)); 1921789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 1922789Sahrens } 1923789Sahrens 1924789Sahrens arc_thread_exit = 0; 1925789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 1926789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 1927789Sahrens thread_exit(); 1928789Sahrens } 1929789Sahrens 19301544Seschrock /* 19311544Seschrock * Adapt arc info given the number of bytes we are trying to add and 19321544Seschrock * the state that we are comming from. This function is only called 19331544Seschrock * when we are adding new content to the cache. 19341544Seschrock */ 1935789Sahrens static void 19361544Seschrock arc_adapt(int bytes, arc_state_t *state) 1937789Sahrens { 19381544Seschrock int mult; 19391544Seschrock 19405450Sbrendan if (state == arc_l2c_only) 19415450Sbrendan return; 19425450Sbrendan 19431544Seschrock ASSERT(bytes > 0); 1944789Sahrens /* 19451544Seschrock * Adapt the target size of the MRU list: 19461544Seschrock * - if we just hit in the MRU ghost list, then increase 19471544Seschrock * the target size of the MRU list. 19481544Seschrock * - if we just hit in the MFU ghost list, then increase 19491544Seschrock * the target size of the MFU list by decreasing the 19501544Seschrock * target size of the MRU list. 1951789Sahrens */ 19523403Sbmc if (state == arc_mru_ghost) { 19533403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 19543403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 19551544Seschrock 19563403Sbmc arc_p = MIN(arc_c, arc_p + bytes * mult); 19573403Sbmc } else if (state == arc_mfu_ghost) { 19583403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 19593403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 19601544Seschrock 19613403Sbmc arc_p = MAX(0, (int64_t)arc_p - bytes * mult); 19621544Seschrock } 19633403Sbmc ASSERT((int64_t)arc_p >= 0); 1964789Sahrens 1965789Sahrens if (arc_reclaim_needed()) { 1966789Sahrens cv_signal(&arc_reclaim_thr_cv); 1967789Sahrens return; 1968789Sahrens } 1969789Sahrens 19703403Sbmc if (arc_no_grow) 1971789Sahrens return; 1972789Sahrens 19733403Sbmc if (arc_c >= arc_c_max) 19741544Seschrock return; 19751544Seschrock 1976789Sahrens /* 19771544Seschrock * If we're within (2 * maxblocksize) bytes of the target 19781544Seschrock * cache size, increment the target cache size 1979789Sahrens */ 19803403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 19813403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 19823403Sbmc if (arc_c > arc_c_max) 19833403Sbmc arc_c = arc_c_max; 19843403Sbmc else if (state == arc_anon) 19853403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 19863403Sbmc if (arc_p > arc_c) 19873403Sbmc arc_p = arc_c; 1988789Sahrens } 19893403Sbmc ASSERT((int64_t)arc_p >= 0); 1990789Sahrens } 1991789Sahrens 1992789Sahrens /* 19931544Seschrock * Check if the cache has reached its limits and eviction is required 19941544Seschrock * prior to insert. 1995789Sahrens */ 1996789Sahrens static int 19974309Smaybee arc_evict_needed(arc_buf_contents_t type) 1998789Sahrens { 19994309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 20004309Smaybee return (1); 20014309Smaybee 20024309Smaybee #ifdef _KERNEL 20034309Smaybee /* 20044309Smaybee * If zio data pages are being allocated out of a separate heap segment, 20054309Smaybee * then enforce that the size of available vmem for this area remains 20064309Smaybee * above about 1/32nd free. 20074309Smaybee */ 20084309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 20094309Smaybee vmem_size(zio_arena, VMEM_FREE) < 20104309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 20114309Smaybee return (1); 20124309Smaybee #endif 20134309Smaybee 2014789Sahrens if (arc_reclaim_needed()) 2015789Sahrens return (1); 2016789Sahrens 20173403Sbmc return (arc_size > arc_c); 2018789Sahrens } 2019789Sahrens 2020789Sahrens /* 20212688Smaybee * The buffer, supplied as the first argument, needs a data block. 20222688Smaybee * So, if we are at cache max, determine which cache should be victimized. 20232688Smaybee * We have the following cases: 2024789Sahrens * 20253403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2026789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2027789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2028789Sahrens * 20293403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2030789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2031789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2032789Sahrens * entries. 2033789Sahrens * 20343403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2035789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2036789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2037789Sahrens * the MFU side, so the MRU side needs to be victimized. 2038789Sahrens * 20393403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2040789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2041789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2042789Sahrens */ 2043789Sahrens static void 20442688Smaybee arc_get_data_buf(arc_buf_t *buf) 2045789Sahrens { 20463290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 20473290Sjohansen uint64_t size = buf->b_hdr->b_size; 20483290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 20492688Smaybee 20502688Smaybee arc_adapt(size, state); 2051789Sahrens 20522688Smaybee /* 20532688Smaybee * We have not yet reached cache maximum size, 20542688Smaybee * just allocate a new buffer. 20552688Smaybee */ 20564309Smaybee if (!arc_evict_needed(type)) { 20573290Sjohansen if (type == ARC_BUFC_METADATA) { 20583290Sjohansen buf->b_data = zio_buf_alloc(size); 20594309Smaybee arc_space_consume(size); 20603290Sjohansen } else { 20613290Sjohansen ASSERT(type == ARC_BUFC_DATA); 20623290Sjohansen buf->b_data = zio_data_buf_alloc(size); 20634309Smaybee atomic_add_64(&arc_size, size); 20643290Sjohansen } 20652688Smaybee goto out; 20662688Smaybee } 20672688Smaybee 20682688Smaybee /* 20692688Smaybee * If we are prefetching from the mfu ghost list, this buffer 20702688Smaybee * will end up on the mru list; so steal space from there. 20712688Smaybee */ 20723403Sbmc if (state == arc_mfu_ghost) 20733403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 20743403Sbmc else if (state == arc_mru_ghost) 20753403Sbmc state = arc_mru; 2076789Sahrens 20773403Sbmc if (state == arc_mru || state == arc_anon) { 20783403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 20794309Smaybee state = (arc_mfu->arcs_lsize[type] > 0 && 20804309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2081789Sahrens } else { 20822688Smaybee /* MFU cases */ 20833403Sbmc uint64_t mfu_space = arc_c - arc_p; 20844309Smaybee state = (arc_mru->arcs_lsize[type] > 0 && 20854309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 20862688Smaybee } 20875642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 20883290Sjohansen if (type == ARC_BUFC_METADATA) { 20893290Sjohansen buf->b_data = zio_buf_alloc(size); 20904309Smaybee arc_space_consume(size); 20913290Sjohansen } else { 20923290Sjohansen ASSERT(type == ARC_BUFC_DATA); 20933290Sjohansen buf->b_data = zio_data_buf_alloc(size); 20944309Smaybee atomic_add_64(&arc_size, size); 20953290Sjohansen } 20963403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 20972688Smaybee } 20982688Smaybee ASSERT(buf->b_data != NULL); 20992688Smaybee out: 21002688Smaybee /* 21012688Smaybee * Update the state size. Note that ghost states have a 21022688Smaybee * "ghost size" and so don't need to be updated. 21032688Smaybee */ 21042688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 21052688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 21062688Smaybee 21073403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 21082688Smaybee if (list_link_active(&hdr->b_arc_node)) { 21092688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 21104309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2111789Sahrens } 21123298Smaybee /* 21133298Smaybee * If we are growing the cache, and we are adding anonymous 21143403Sbmc * data, and we have outgrown arc_p, update arc_p 21153298Smaybee */ 21163403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 21173403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 21183403Sbmc arc_p = MIN(arc_c, arc_p + size); 2119789Sahrens } 2120789Sahrens } 2121789Sahrens 2122789Sahrens /* 2123789Sahrens * This routine is called whenever a buffer is accessed. 21241544Seschrock * NOTE: the hash lock is dropped in this function. 2125789Sahrens */ 2126789Sahrens static void 21272688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2128789Sahrens { 2129789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2130789Sahrens 21313403Sbmc if (buf->b_state == arc_anon) { 2132789Sahrens /* 2133789Sahrens * This buffer is not in the cache, and does not 2134789Sahrens * appear in our "ghost" list. Add the new buffer 2135789Sahrens * to the MRU state. 2136789Sahrens */ 2137789Sahrens 2138789Sahrens ASSERT(buf->b_arc_access == 0); 2139789Sahrens buf->b_arc_access = lbolt; 21401544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 21413403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2142789Sahrens 21433403Sbmc } else if (buf->b_state == arc_mru) { 2144789Sahrens /* 21452391Smaybee * If this buffer is here because of a prefetch, then either: 21462391Smaybee * - clear the flag if this is a "referencing" read 21472391Smaybee * (any subsequent access will bump this into the MFU state). 21482391Smaybee * or 21492391Smaybee * - move the buffer to the head of the list if this is 21502391Smaybee * another prefetch (to make it less likely to be evicted). 2151789Sahrens */ 2152789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 21532391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 21542391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 21552391Smaybee } else { 21562391Smaybee buf->b_flags &= ~ARC_PREFETCH; 21573403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 21582391Smaybee } 21592391Smaybee buf->b_arc_access = lbolt; 2160789Sahrens return; 2161789Sahrens } 2162789Sahrens 2163789Sahrens /* 2164789Sahrens * This buffer has been "accessed" only once so far, 2165789Sahrens * but it is still in the cache. Move it to the MFU 2166789Sahrens * state. 2167789Sahrens */ 2168789Sahrens if (lbolt > buf->b_arc_access + ARC_MINTIME) { 2169789Sahrens /* 2170789Sahrens * More than 125ms have passed since we 2171789Sahrens * instantiated this buffer. Move it to the 2172789Sahrens * most frequently used state. 2173789Sahrens */ 2174789Sahrens buf->b_arc_access = lbolt; 21751544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 21763403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2177789Sahrens } 21783403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 21793403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2180789Sahrens arc_state_t *new_state; 2181789Sahrens /* 2182789Sahrens * This buffer has been "accessed" recently, but 2183789Sahrens * was evicted from the cache. Move it to the 2184789Sahrens * MFU state. 2185789Sahrens */ 2186789Sahrens 2187789Sahrens if (buf->b_flags & ARC_PREFETCH) { 21883403Sbmc new_state = arc_mru; 21892391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 21902391Smaybee buf->b_flags &= ~ARC_PREFETCH; 21911544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2192789Sahrens } else { 21933403Sbmc new_state = arc_mfu; 21941544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2195789Sahrens } 2196789Sahrens 2197789Sahrens buf->b_arc_access = lbolt; 2198789Sahrens arc_change_state(new_state, buf, hash_lock); 2199789Sahrens 22003403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 22013403Sbmc } else if (buf->b_state == arc_mfu) { 2202789Sahrens /* 2203789Sahrens * This buffer has been accessed more than once and is 2204789Sahrens * still in the cache. Keep it in the MFU state. 2205789Sahrens * 22062391Smaybee * NOTE: an add_reference() that occurred when we did 22072391Smaybee * the arc_read() will have kicked this off the list. 22082391Smaybee * If it was a prefetch, we will explicitly move it to 22092391Smaybee * the head of the list now. 2210789Sahrens */ 22112391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 22122391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 22132391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 22142391Smaybee } 22153403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 22162391Smaybee buf->b_arc_access = lbolt; 22173403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 22183403Sbmc arc_state_t *new_state = arc_mfu; 2219789Sahrens /* 2220789Sahrens * This buffer has been accessed more than once but has 2221789Sahrens * been evicted from the cache. Move it back to the 2222789Sahrens * MFU state. 2223789Sahrens */ 2224789Sahrens 22252391Smaybee if (buf->b_flags & ARC_PREFETCH) { 22262391Smaybee /* 22272391Smaybee * This is a prefetch access... 22282391Smaybee * move this block back to the MRU state. 22292391Smaybee */ 22302391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 22313403Sbmc new_state = arc_mru; 22322391Smaybee } 22332391Smaybee 2234789Sahrens buf->b_arc_access = lbolt; 22351544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 22362391Smaybee arc_change_state(new_state, buf, hash_lock); 2237789Sahrens 22383403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 22395450Sbrendan } else if (buf->b_state == arc_l2c_only) { 22405450Sbrendan /* 22415450Sbrendan * This buffer is on the 2nd Level ARC. 22425450Sbrendan */ 22435450Sbrendan 22445450Sbrendan buf->b_arc_access = lbolt; 22455450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 22465450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2247789Sahrens } else { 2248789Sahrens ASSERT(!"invalid arc state"); 2249789Sahrens } 2250789Sahrens } 2251789Sahrens 2252789Sahrens /* a generic arc_done_func_t which you can use */ 2253789Sahrens /* ARGSUSED */ 2254789Sahrens void 2255789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2256789Sahrens { 2257789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 22581544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2259789Sahrens } 2260789Sahrens 22614309Smaybee /* a generic arc_done_func_t */ 2262789Sahrens void 2263789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2264789Sahrens { 2265789Sahrens arc_buf_t **bufp = arg; 2266789Sahrens if (zio && zio->io_error) { 22671544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2268789Sahrens *bufp = NULL; 2269789Sahrens } else { 2270789Sahrens *bufp = buf; 2271789Sahrens } 2272789Sahrens } 2273789Sahrens 2274789Sahrens static void 2275789Sahrens arc_read_done(zio_t *zio) 2276789Sahrens { 22771589Smaybee arc_buf_hdr_t *hdr, *found; 2278789Sahrens arc_buf_t *buf; 2279789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2280789Sahrens kmutex_t *hash_lock; 2281789Sahrens arc_callback_t *callback_list, *acb; 2282789Sahrens int freeable = FALSE; 2283789Sahrens 2284789Sahrens buf = zio->io_private; 2285789Sahrens hdr = buf->b_hdr; 2286789Sahrens 22871589Smaybee /* 22881589Smaybee * The hdr was inserted into hash-table and removed from lists 22891589Smaybee * prior to starting I/O. We should find this header, since 22901589Smaybee * it's in the hash table, and it should be legit since it's 22911589Smaybee * not possible to evict it during the I/O. The only possible 22921589Smaybee * reason for it not to be found is if we were freed during the 22931589Smaybee * read. 22941589Smaybee */ 22951589Smaybee found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth, 22963093Sahrens &hash_lock); 2297789Sahrens 22981589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 22995450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 23005450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 23015450Sbrendan 23026987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 23035450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 23047237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2305789Sahrens 2306789Sahrens /* byteswap if necessary */ 2307789Sahrens callback_list = hdr->b_acb; 2308789Sahrens ASSERT(callback_list != NULL); 23097046Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 23107046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 23117046Sahrens byteswap_uint64_array : 23127046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 23137046Sahrens func(buf->b_data, hdr->b_size); 23147046Sahrens } 2315789Sahrens 23165450Sbrendan arc_cksum_compute(buf, B_FALSE); 23173093Sahrens 2318789Sahrens /* create copies of the data buffer for the callers */ 2319789Sahrens abuf = buf; 2320789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2321789Sahrens if (acb->acb_done) { 23222688Smaybee if (abuf == NULL) 23232688Smaybee abuf = arc_buf_clone(buf); 2324789Sahrens acb->acb_buf = abuf; 2325789Sahrens abuf = NULL; 2326789Sahrens } 2327789Sahrens } 2328789Sahrens hdr->b_acb = NULL; 2329789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 23301544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 23311544Seschrock if (abuf == buf) 23321544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 2333789Sahrens 2334789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2335789Sahrens 2336789Sahrens if (zio->io_error != 0) { 2337789Sahrens hdr->b_flags |= ARC_IO_ERROR; 23383403Sbmc if (hdr->b_state != arc_anon) 23393403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 23401544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 23411544Seschrock buf_hash_remove(hdr); 2342789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2343789Sahrens } 2344789Sahrens 23451544Seschrock /* 23462391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 23472391Smaybee * that the hdr (and hence the cv) might be freed before we get to 23482391Smaybee * the cv_broadcast(). 23491544Seschrock */ 23501544Seschrock cv_broadcast(&hdr->b_cv); 23511544Seschrock 23521589Smaybee if (hash_lock) { 2353789Sahrens /* 2354789Sahrens * Only call arc_access on anonymous buffers. This is because 2355789Sahrens * if we've issued an I/O for an evicted buffer, we've already 2356789Sahrens * called arc_access (to prevent any simultaneous readers from 2357789Sahrens * getting confused). 2358789Sahrens */ 23593403Sbmc if (zio->io_error == 0 && hdr->b_state == arc_anon) 23602688Smaybee arc_access(hdr, hash_lock); 23612688Smaybee mutex_exit(hash_lock); 2362789Sahrens } else { 2363789Sahrens /* 2364789Sahrens * This block was freed while we waited for the read to 2365789Sahrens * complete. It has been removed from the hash table and 2366789Sahrens * moved to the anonymous state (so that it won't show up 2367789Sahrens * in the cache). 2368789Sahrens */ 23693403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2370789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2371789Sahrens } 2372789Sahrens 2373789Sahrens /* execute each callback and free its structure */ 2374789Sahrens while ((acb = callback_list) != NULL) { 2375789Sahrens if (acb->acb_done) 2376789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2377789Sahrens 2378789Sahrens if (acb->acb_zio_dummy != NULL) { 2379789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2380789Sahrens zio_nowait(acb->acb_zio_dummy); 2381789Sahrens } 2382789Sahrens 2383789Sahrens callback_list = acb->acb_next; 2384789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2385789Sahrens } 2386789Sahrens 2387789Sahrens if (freeable) 23881544Seschrock arc_hdr_destroy(hdr); 2389789Sahrens } 2390789Sahrens 2391789Sahrens /* 2392789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2393789Sahrens * cache. If the block is found in the cache, invoke the provided 2394789Sahrens * callback immediately and return. Note that the `zio' parameter 2395789Sahrens * in the callback will be NULL in this case, since no IO was 2396789Sahrens * required. If the block is not in the cache pass the read request 2397789Sahrens * on to the spa with a substitute callback function, so that the 2398789Sahrens * requested block will be added to the cache. 2399789Sahrens * 2400789Sahrens * If a read request arrives for a block that has a read in-progress, 2401789Sahrens * either wait for the in-progress read to complete (and return the 2402789Sahrens * results); or, if this is a read with a "done" func, add a record 2403789Sahrens * to the read to invoke the "done" func when the read completes, 2404789Sahrens * and return; or just return. 2405789Sahrens * 2406789Sahrens * arc_read_done() will invoke all the requested "done" functions 2407789Sahrens * for readers of this block. 24087046Sahrens * 24097046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 24107046Sahrens * for the bp. But if you know you don't need locking, you can use 24117046Sahrens * arc_read_bp. 2412789Sahrens */ 2413789Sahrens int 24147046Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_buf_t *pbuf, 24157237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 24167046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 24177046Sahrens { 24187046Sahrens int err; 24197265Sahrens arc_buf_hdr_t *hdr = pbuf->b_hdr; 24207046Sahrens 24217046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 24227046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 24237046Sahrens rw_enter(&pbuf->b_hdr->b_datalock, RW_READER); 24247046Sahrens 24257046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 24267237Sek110237 zio_flags, arc_flags, zb); 24277046Sahrens 24287265Sahrens ASSERT3P(hdr, ==, pbuf->b_hdr); 24297046Sahrens rw_exit(&pbuf->b_hdr->b_datalock); 24307046Sahrens return (err); 24317046Sahrens } 24327046Sahrens 24337046Sahrens int 24347046Sahrens arc_read_nolock(zio_t *pio, spa_t *spa, blkptr_t *bp, 24357237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 24367046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2437789Sahrens { 2438789Sahrens arc_buf_hdr_t *hdr; 2439789Sahrens arc_buf_t *buf; 2440789Sahrens kmutex_t *hash_lock; 24415450Sbrendan zio_t *rzio; 2442789Sahrens 2443789Sahrens top: 2444789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 24451544Seschrock if (hdr && hdr->b_datacnt > 0) { 2446789Sahrens 24472391Smaybee *arc_flags |= ARC_CACHED; 24482391Smaybee 2449789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 24502391Smaybee 24512391Smaybee if (*arc_flags & ARC_WAIT) { 24522391Smaybee cv_wait(&hdr->b_cv, hash_lock); 24532391Smaybee mutex_exit(hash_lock); 24542391Smaybee goto top; 24552391Smaybee } 24562391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 24572391Smaybee 24582391Smaybee if (done) { 2459789Sahrens arc_callback_t *acb = NULL; 2460789Sahrens 2461789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2462789Sahrens KM_SLEEP); 2463789Sahrens acb->acb_done = done; 2464789Sahrens acb->acb_private = private; 2465789Sahrens if (pio != NULL) 2466789Sahrens acb->acb_zio_dummy = zio_null(pio, 24677237Sek110237 spa, NULL, NULL, zio_flags); 2468789Sahrens 2469789Sahrens ASSERT(acb->acb_done != NULL); 2470789Sahrens acb->acb_next = hdr->b_acb; 2471789Sahrens hdr->b_acb = acb; 2472789Sahrens add_reference(hdr, hash_lock, private); 2473789Sahrens mutex_exit(hash_lock); 2474789Sahrens return (0); 2475789Sahrens } 2476789Sahrens mutex_exit(hash_lock); 2477789Sahrens return (0); 2478789Sahrens } 2479789Sahrens 24803403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2481789Sahrens 24821544Seschrock if (done) { 24832688Smaybee add_reference(hdr, hash_lock, private); 24841544Seschrock /* 24851544Seschrock * If this block is already in use, create a new 24861544Seschrock * copy of the data so that we will be guaranteed 24871544Seschrock * that arc_release() will always succeed. 24881544Seschrock */ 24891544Seschrock buf = hdr->b_buf; 24901544Seschrock ASSERT(buf); 24911544Seschrock ASSERT(buf->b_data); 24922688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 24931544Seschrock ASSERT(buf->b_efunc == NULL); 24941544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 24952688Smaybee } else { 24962688Smaybee buf = arc_buf_clone(buf); 24971544Seschrock } 24982391Smaybee } else if (*arc_flags & ARC_PREFETCH && 24992391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 25002391Smaybee hdr->b_flags |= ARC_PREFETCH; 2501789Sahrens } 2502789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 25032688Smaybee arc_access(hdr, hash_lock); 25047237Sek110237 if (*arc_flags & ARC_L2CACHE) 25057237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25062688Smaybee mutex_exit(hash_lock); 25073403Sbmc ARCSTAT_BUMP(arcstat_hits); 25083403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 25093403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 25103403Sbmc data, metadata, hits); 25113403Sbmc 2512789Sahrens if (done) 2513789Sahrens done(NULL, buf, private); 2514789Sahrens } else { 2515789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2516789Sahrens arc_callback_t *acb; 25176987Sbrendan vdev_t *vd = NULL; 25186987Sbrendan daddr_t addr; 2519789Sahrens 2520789Sahrens if (hdr == NULL) { 2521789Sahrens /* this block is not in the cache */ 2522789Sahrens arc_buf_hdr_t *exists; 25233290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 25243290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2525789Sahrens hdr = buf->b_hdr; 2526789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 2527789Sahrens hdr->b_birth = bp->blk_birth; 2528789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2529789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2530789Sahrens if (exists) { 2531789Sahrens /* somebody beat us to the hash insert */ 2532789Sahrens mutex_exit(hash_lock); 2533789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2534789Sahrens hdr->b_birth = 0; 2535789Sahrens hdr->b_cksum0 = 0; 25361544Seschrock (void) arc_buf_remove_ref(buf, private); 2537789Sahrens goto top; /* restart the IO request */ 2538789Sahrens } 25392391Smaybee /* if this is a prefetch, we don't have a reference */ 25402391Smaybee if (*arc_flags & ARC_PREFETCH) { 25412391Smaybee (void) remove_reference(hdr, hash_lock, 25422391Smaybee private); 25432391Smaybee hdr->b_flags |= ARC_PREFETCH; 25442391Smaybee } 25457237Sek110237 if (*arc_flags & ARC_L2CACHE) 25467237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25472391Smaybee if (BP_GET_LEVEL(bp) > 0) 25482391Smaybee hdr->b_flags |= ARC_INDIRECT; 2549789Sahrens } else { 2550789Sahrens /* this block is in the ghost cache */ 25511544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 25521544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 25532391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 25542391Smaybee ASSERT(hdr->b_buf == NULL); 2555789Sahrens 25562391Smaybee /* if this is a prefetch, we don't have a reference */ 25572391Smaybee if (*arc_flags & ARC_PREFETCH) 25582391Smaybee hdr->b_flags |= ARC_PREFETCH; 25592391Smaybee else 25602391Smaybee add_reference(hdr, hash_lock, private); 25617237Sek110237 if (*arc_flags & ARC_L2CACHE) 25627237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25636245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 25641544Seschrock buf->b_hdr = hdr; 25652688Smaybee buf->b_data = NULL; 25661544Seschrock buf->b_efunc = NULL; 25671544Seschrock buf->b_private = NULL; 25681544Seschrock buf->b_next = NULL; 25691544Seschrock hdr->b_buf = buf; 25702688Smaybee arc_get_data_buf(buf); 25711544Seschrock ASSERT(hdr->b_datacnt == 0); 25721544Seschrock hdr->b_datacnt = 1; 25732391Smaybee 2574789Sahrens } 2575789Sahrens 2576789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2577789Sahrens acb->acb_done = done; 2578789Sahrens acb->acb_private = private; 2579789Sahrens 2580789Sahrens ASSERT(hdr->b_acb == NULL); 2581789Sahrens hdr->b_acb = acb; 2582789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2583789Sahrens 2584789Sahrens /* 2585789Sahrens * If the buffer has been evicted, migrate it to a present state 2586789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2587789Sahrens * the header will be marked as I/O in progress and have an 2588789Sahrens * attached buffer. At this point, anybody who finds this 2589789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2590789Sahrens */ 2591789Sahrens 25921544Seschrock if (GHOST_STATE(hdr->b_state)) 25932688Smaybee arc_access(hdr, hash_lock); 2594789Sahrens 25956987Sbrendan if (hdr->b_l2hdr != NULL) { 25966987Sbrendan vd = hdr->b_l2hdr->b_dev->l2ad_vdev; 25976987Sbrendan addr = hdr->b_l2hdr->b_daddr; 25986987Sbrendan } 25996987Sbrendan 26006987Sbrendan mutex_exit(hash_lock); 26016987Sbrendan 2602789Sahrens ASSERT3U(hdr->b_size, ==, size); 26031596Sahrens DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size, 26041596Sahrens zbookmark_t *, zb); 26053403Sbmc ARCSTAT_BUMP(arcstat_misses); 26063403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 26073403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 26083403Sbmc data, metadata, misses); 26091544Seschrock 26107237Sek110237 if (l2arc_ndev != 0 && HDR_L2CACHE(hdr)) { 26115450Sbrendan /* 26126987Sbrendan * Lock out device removal. 26135450Sbrendan */ 26146987Sbrendan spa_config_enter(spa, RW_READER, FTAG); 26156987Sbrendan 26166987Sbrendan /* 26176987Sbrendan * Read from the L2ARC if the following are true: 26186987Sbrendan * 1. The L2ARC vdev was previously cached. 26196987Sbrendan * 2. This buffer still has L2ARC metadata. 26206987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 26216987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 26226987Sbrendan * also have invalidated the vdev. 26236987Sbrendan */ 26246987Sbrendan if (vd != NULL && hdr->b_l2hdr != NULL && 26256987Sbrendan !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr)) { 26265450Sbrendan l2arc_read_callback_t *cb; 26275450Sbrendan 26286643Seschrock if (vdev_is_dead(vd)) 26296987Sbrendan goto l2skip; 26305450Sbrendan 26316643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 26326643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 26336643Seschrock 26345450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 26355450Sbrendan KM_SLEEP); 26365450Sbrendan cb->l2rcb_buf = buf; 26375450Sbrendan cb->l2rcb_spa = spa; 26385450Sbrendan cb->l2rcb_bp = *bp; 26395450Sbrendan cb->l2rcb_zb = *zb; 26407237Sek110237 cb->l2rcb_flags = zio_flags; 26415450Sbrendan 26425450Sbrendan /* 26435450Sbrendan * l2arc read. 26445450Sbrendan */ 26455450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 26465450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 26477237Sek110237 l2arc_read_done, cb, priority, zio_flags | 26487361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 26497361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_PROPAGATE, B_FALSE); 26505450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 26515450Sbrendan zio_t *, rzio); 26526987Sbrendan spa_config_exit(spa, FTAG); 26536987Sbrendan 26546987Sbrendan if (*arc_flags & ARC_NOWAIT) { 26556987Sbrendan zio_nowait(rzio); 26566987Sbrendan return (0); 26576987Sbrendan } 26586987Sbrendan 26596987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 26606987Sbrendan if (zio_wait(rzio) == 0) 26616987Sbrendan return (0); 26626987Sbrendan 26636987Sbrendan /* l2arc read error; goto zio_read() */ 26645450Sbrendan } else { 26655450Sbrendan DTRACE_PROBE1(l2arc__miss, 26665450Sbrendan arc_buf_hdr_t *, hdr); 26675450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 26685450Sbrendan if (HDR_L2_WRITING(hdr)) 26695450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 26706987Sbrendan l2skip: 26716987Sbrendan spa_config_exit(spa, FTAG); 26725450Sbrendan } 26735450Sbrendan } 26746643Seschrock 2675789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 26767237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2677789Sahrens 26782391Smaybee if (*arc_flags & ARC_WAIT) 2679789Sahrens return (zio_wait(rzio)); 2680789Sahrens 26812391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2682789Sahrens zio_nowait(rzio); 2683789Sahrens } 2684789Sahrens return (0); 2685789Sahrens } 2686789Sahrens 2687789Sahrens /* 2688789Sahrens * arc_read() variant to support pool traversal. If the block is already 2689789Sahrens * in the ARC, make a copy of it; otherwise, the caller will do the I/O. 2690789Sahrens * The idea is that we don't want pool traversal filling up memory, but 2691789Sahrens * if the ARC already has the data anyway, we shouldn't pay for the I/O. 2692789Sahrens */ 2693789Sahrens int 2694789Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data) 2695789Sahrens { 2696789Sahrens arc_buf_hdr_t *hdr; 2697789Sahrens kmutex_t *hash_mtx; 2698789Sahrens int rc = 0; 2699789Sahrens 2700789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx); 2701789Sahrens 27021544Seschrock if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) { 27031544Seschrock arc_buf_t *buf = hdr->b_buf; 27041544Seschrock 27051544Seschrock ASSERT(buf); 27061544Seschrock while (buf->b_data == NULL) { 27071544Seschrock buf = buf->b_next; 27081544Seschrock ASSERT(buf); 27091544Seschrock } 27101544Seschrock bcopy(buf->b_data, data, hdr->b_size); 27111544Seschrock } else { 2712789Sahrens rc = ENOENT; 27131544Seschrock } 2714789Sahrens 2715789Sahrens if (hash_mtx) 2716789Sahrens mutex_exit(hash_mtx); 2717789Sahrens 2718789Sahrens return (rc); 2719789Sahrens } 2720789Sahrens 27211544Seschrock void 27221544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 27231544Seschrock { 27241544Seschrock ASSERT(buf->b_hdr != NULL); 27253403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 27261544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 27271544Seschrock buf->b_efunc = func; 27281544Seschrock buf->b_private = private; 27291544Seschrock } 27301544Seschrock 27311544Seschrock /* 27321544Seschrock * This is used by the DMU to let the ARC know that a buffer is 27331544Seschrock * being evicted, so the ARC should clean up. If this arc buf 27341544Seschrock * is not yet in the evicted state, it will be put there. 27351544Seschrock */ 27361544Seschrock int 27371544Seschrock arc_buf_evict(arc_buf_t *buf) 27381544Seschrock { 27392887Smaybee arc_buf_hdr_t *hdr; 27401544Seschrock kmutex_t *hash_lock; 27411544Seschrock arc_buf_t **bufp; 27421544Seschrock 27432887Smaybee mutex_enter(&arc_eviction_mtx); 27442887Smaybee hdr = buf->b_hdr; 27451544Seschrock if (hdr == NULL) { 27461544Seschrock /* 27471544Seschrock * We are in arc_do_user_evicts(). 27481544Seschrock */ 27491544Seschrock ASSERT(buf->b_data == NULL); 27502887Smaybee mutex_exit(&arc_eviction_mtx); 27511544Seschrock return (0); 27521544Seschrock } 27532887Smaybee hash_lock = HDR_LOCK(hdr); 27542887Smaybee mutex_exit(&arc_eviction_mtx); 27551544Seschrock 27561544Seschrock mutex_enter(hash_lock); 27571544Seschrock 27582724Smaybee if (buf->b_data == NULL) { 27592724Smaybee /* 27602724Smaybee * We are on the eviction list. 27612724Smaybee */ 27622724Smaybee mutex_exit(hash_lock); 27632724Smaybee mutex_enter(&arc_eviction_mtx); 27642724Smaybee if (buf->b_hdr == NULL) { 27652724Smaybee /* 27662724Smaybee * We are already in arc_do_user_evicts(). 27672724Smaybee */ 27682724Smaybee mutex_exit(&arc_eviction_mtx); 27692724Smaybee return (0); 27702724Smaybee } else { 27712724Smaybee arc_buf_t copy = *buf; /* structure assignment */ 27722724Smaybee /* 27732724Smaybee * Process this buffer now 27742724Smaybee * but let arc_do_user_evicts() do the reaping. 27752724Smaybee */ 27762724Smaybee buf->b_efunc = NULL; 27772724Smaybee mutex_exit(&arc_eviction_mtx); 27782724Smaybee VERIFY(copy.b_efunc(©) == 0); 27792724Smaybee return (1); 27802724Smaybee } 27812724Smaybee } 27822724Smaybee 27832724Smaybee ASSERT(buf->b_hdr == hdr); 27842724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 27853403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 27861544Seschrock 27871544Seschrock /* 27881544Seschrock * Pull this buffer off of the hdr 27891544Seschrock */ 27901544Seschrock bufp = &hdr->b_buf; 27911544Seschrock while (*bufp != buf) 27921544Seschrock bufp = &(*bufp)->b_next; 27931544Seschrock *bufp = buf->b_next; 27941544Seschrock 27951544Seschrock ASSERT(buf->b_data != NULL); 27962688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 27971544Seschrock 27981544Seschrock if (hdr->b_datacnt == 0) { 27991544Seschrock arc_state_t *old_state = hdr->b_state; 28001544Seschrock arc_state_t *evicted_state; 28011544Seschrock 28021544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 28031544Seschrock 28041544Seschrock evicted_state = 28053403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 28061544Seschrock 28073403Sbmc mutex_enter(&old_state->arcs_mtx); 28083403Sbmc mutex_enter(&evicted_state->arcs_mtx); 28091544Seschrock 28101544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 28111544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 28125450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 28135450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 28141544Seschrock 28153403Sbmc mutex_exit(&evicted_state->arcs_mtx); 28163403Sbmc mutex_exit(&old_state->arcs_mtx); 28171544Seschrock } 28181544Seschrock mutex_exit(hash_lock); 28191819Smaybee 28201544Seschrock VERIFY(buf->b_efunc(buf) == 0); 28211544Seschrock buf->b_efunc = NULL; 28221544Seschrock buf->b_private = NULL; 28231544Seschrock buf->b_hdr = NULL; 28241544Seschrock kmem_cache_free(buf_cache, buf); 28251544Seschrock return (1); 28261544Seschrock } 28271544Seschrock 2828789Sahrens /* 2829789Sahrens * Release this buffer from the cache. This must be done 2830789Sahrens * after a read and prior to modifying the buffer contents. 2831789Sahrens * If the buffer has more than one reference, we must make 28327046Sahrens * a new hdr for the buffer. 2833789Sahrens */ 2834789Sahrens void 2835789Sahrens arc_release(arc_buf_t *buf, void *tag) 2836789Sahrens { 2837789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 2838789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 28395450Sbrendan l2arc_buf_hdr_t *l2hdr = NULL; 28405450Sbrendan uint64_t buf_size; 2841789Sahrens 2842789Sahrens /* this buffer is not on any list */ 2843789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 28447046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 2845789Sahrens 28463403Sbmc if (hdr->b_state == arc_anon) { 2847789Sahrens /* this buffer is already released */ 2848789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2849789Sahrens ASSERT(BUF_EMPTY(hdr)); 28501544Seschrock ASSERT(buf->b_efunc == NULL); 28513093Sahrens arc_buf_thaw(buf); 2852789Sahrens return; 2853789Sahrens } 2854789Sahrens 2855789Sahrens mutex_enter(hash_lock); 2856789Sahrens 28571544Seschrock /* 28581544Seschrock * Do we have more than one buf? 28591544Seschrock */ 28601544Seschrock if (hdr->b_buf != buf || buf->b_next != NULL) { 2861789Sahrens arc_buf_hdr_t *nhdr; 2862789Sahrens arc_buf_t **bufp; 2863789Sahrens uint64_t blksz = hdr->b_size; 2864789Sahrens spa_t *spa = hdr->b_spa; 28653290Sjohansen arc_buf_contents_t type = hdr->b_type; 28665450Sbrendan uint32_t flags = hdr->b_flags; 2867789Sahrens 28681544Seschrock ASSERT(hdr->b_datacnt > 1); 2869789Sahrens /* 2870789Sahrens * Pull the data off of this buf and attach it to 2871789Sahrens * a new anonymous buf. 2872789Sahrens */ 28731544Seschrock (void) remove_reference(hdr, hash_lock, tag); 2874789Sahrens bufp = &hdr->b_buf; 28751544Seschrock while (*bufp != buf) 2876789Sahrens bufp = &(*bufp)->b_next; 2877789Sahrens *bufp = (*bufp)->b_next; 28783897Smaybee buf->b_next = NULL; 28791544Seschrock 28803403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 28813403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 28821544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 28834309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 28844309Smaybee ASSERT3U(*size, >=, hdr->b_size); 28854309Smaybee atomic_add_64(size, -hdr->b_size); 28861544Seschrock } 28871544Seschrock hdr->b_datacnt -= 1; 28885450Sbrendan if (hdr->b_l2hdr != NULL) { 28895450Sbrendan mutex_enter(&l2arc_buflist_mtx); 28905450Sbrendan l2hdr = hdr->b_l2hdr; 28915450Sbrendan hdr->b_l2hdr = NULL; 28925450Sbrendan buf_size = hdr->b_size; 28935450Sbrendan } 28943547Smaybee arc_cksum_verify(buf); 28951544Seschrock 2896789Sahrens mutex_exit(hash_lock); 2897789Sahrens 28986245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 2899789Sahrens nhdr->b_size = blksz; 2900789Sahrens nhdr->b_spa = spa; 29013290Sjohansen nhdr->b_type = type; 2902789Sahrens nhdr->b_buf = buf; 29033403Sbmc nhdr->b_state = arc_anon; 2904789Sahrens nhdr->b_arc_access = 0; 29055450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 29065450Sbrendan nhdr->b_l2hdr = NULL; 29071544Seschrock nhdr->b_datacnt = 1; 29083547Smaybee nhdr->b_freeze_cksum = NULL; 29093897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 2910789Sahrens buf->b_hdr = nhdr; 29113403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 2912789Sahrens } else { 29131544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 2914789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 2915789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 29163403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 2917789Sahrens hdr->b_arc_access = 0; 29185450Sbrendan if (hdr->b_l2hdr != NULL) { 29195450Sbrendan mutex_enter(&l2arc_buflist_mtx); 29205450Sbrendan l2hdr = hdr->b_l2hdr; 29215450Sbrendan hdr->b_l2hdr = NULL; 29225450Sbrendan buf_size = hdr->b_size; 29235450Sbrendan } 2924789Sahrens mutex_exit(hash_lock); 29255450Sbrendan 2926789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2927789Sahrens hdr->b_birth = 0; 2928789Sahrens hdr->b_cksum0 = 0; 29293547Smaybee arc_buf_thaw(buf); 2930789Sahrens } 29311544Seschrock buf->b_efunc = NULL; 29321544Seschrock buf->b_private = NULL; 29335450Sbrendan 29345450Sbrendan if (l2hdr) { 29355450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 29365450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 29375450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 29385450Sbrendan } 29395450Sbrendan if (MUTEX_HELD(&l2arc_buflist_mtx)) 29405450Sbrendan mutex_exit(&l2arc_buflist_mtx); 2941789Sahrens } 2942789Sahrens 2943789Sahrens int 2944789Sahrens arc_released(arc_buf_t *buf) 2945789Sahrens { 29463403Sbmc return (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 29471544Seschrock } 29481544Seschrock 29491544Seschrock int 29501544Seschrock arc_has_callback(arc_buf_t *buf) 29511544Seschrock { 29521544Seschrock return (buf->b_efunc != NULL); 2953789Sahrens } 2954789Sahrens 29551544Seschrock #ifdef ZFS_DEBUG 29561544Seschrock int 29571544Seschrock arc_referenced(arc_buf_t *buf) 29581544Seschrock { 29591544Seschrock return (refcount_count(&buf->b_hdr->b_refcnt)); 29601544Seschrock } 29611544Seschrock #endif 29621544Seschrock 2963789Sahrens static void 29643547Smaybee arc_write_ready(zio_t *zio) 29653547Smaybee { 29663547Smaybee arc_write_callback_t *callback = zio->io_private; 29673547Smaybee arc_buf_t *buf = callback->awcb_buf; 29685329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 29695329Sgw25295 29705329Sgw25295 if (zio->io_error == 0 && callback->awcb_ready) { 29713547Smaybee ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 29723547Smaybee callback->awcb_ready(zio, buf, callback->awcb_private); 29733547Smaybee } 29745329Sgw25295 /* 29755329Sgw25295 * If the IO is already in progress, then this is a re-write 29765329Sgw25295 * attempt, so we need to thaw and re-compute the cksum. It is 29775329Sgw25295 * the responsibility of the callback to handle the freeing 29785329Sgw25295 * and accounting for any re-write attempt. If we don't have a 29795329Sgw25295 * callback registered then simply free the block here. 29805329Sgw25295 */ 29815329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 29825329Sgw25295 if (!BP_IS_HOLE(&zio->io_bp_orig) && 29835329Sgw25295 callback->awcb_ready == NULL) { 29845329Sgw25295 zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg, 29855329Sgw25295 &zio->io_bp_orig, NULL, NULL)); 29865329Sgw25295 } 29875329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 29885329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 29895329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 29905329Sgw25295 hdr->b_freeze_cksum = NULL; 29915329Sgw25295 } 29925329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 29935329Sgw25295 } 29945450Sbrendan arc_cksum_compute(buf, B_FALSE); 29955329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 29963547Smaybee } 29973547Smaybee 29983547Smaybee static void 2999789Sahrens arc_write_done(zio_t *zio) 3000789Sahrens { 30013547Smaybee arc_write_callback_t *callback = zio->io_private; 30023547Smaybee arc_buf_t *buf = callback->awcb_buf; 30033547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3004789Sahrens 3005789Sahrens hdr->b_acb = NULL; 3006789Sahrens 3007789Sahrens hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3008789Sahrens hdr->b_birth = zio->io_bp->blk_birth; 3009789Sahrens hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 30101544Seschrock /* 30111544Seschrock * If the block to be written was all-zero, we may have 30121544Seschrock * compressed it away. In this case no write was performed 30131544Seschrock * so there will be no dva/birth-date/checksum. The buffer 30141544Seschrock * must therefor remain anonymous (and uncached). 30151544Seschrock */ 3016789Sahrens if (!BUF_EMPTY(hdr)) { 3017789Sahrens arc_buf_hdr_t *exists; 3018789Sahrens kmutex_t *hash_lock; 3019789Sahrens 30203093Sahrens arc_cksum_verify(buf); 30213093Sahrens 3022789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3023789Sahrens if (exists) { 3024789Sahrens /* 3025789Sahrens * This can only happen if we overwrite for 3026789Sahrens * sync-to-convergence, because we remove 3027789Sahrens * buffers from the hash table when we arc_free(). 3028789Sahrens */ 3029789Sahrens ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig), 3030789Sahrens BP_IDENTITY(zio->io_bp))); 3031789Sahrens ASSERT3U(zio->io_bp_orig.blk_birth, ==, 3032789Sahrens zio->io_bp->blk_birth); 3033789Sahrens 3034789Sahrens ASSERT(refcount_is_zero(&exists->b_refcnt)); 30353403Sbmc arc_change_state(arc_anon, exists, hash_lock); 3036789Sahrens mutex_exit(hash_lock); 30371544Seschrock arc_hdr_destroy(exists); 3038789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3039789Sahrens ASSERT3P(exists, ==, NULL); 3040789Sahrens } 30411544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 30427046Sahrens /* if it's not anon, we are doing a scrub */ 30437046Sahrens if (hdr->b_state == arc_anon) 30447046Sahrens arc_access(hdr, hash_lock); 30452688Smaybee mutex_exit(hash_lock); 30463547Smaybee } else if (callback->awcb_done == NULL) { 30471544Seschrock int destroy_hdr; 30481544Seschrock /* 30491544Seschrock * This is an anonymous buffer with no user callback, 30501544Seschrock * destroy it if there are no active references. 30511544Seschrock */ 30521544Seschrock mutex_enter(&arc_eviction_mtx); 30531544Seschrock destroy_hdr = refcount_is_zero(&hdr->b_refcnt); 30541544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 30551544Seschrock mutex_exit(&arc_eviction_mtx); 30561544Seschrock if (destroy_hdr) 30571544Seschrock arc_hdr_destroy(hdr); 30581544Seschrock } else { 30591544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3060789Sahrens } 30617046Sahrens hdr->b_flags &= ~ARC_STORED; 30621544Seschrock 30633547Smaybee if (callback->awcb_done) { 3064789Sahrens ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 30653547Smaybee callback->awcb_done(zio, buf, callback->awcb_private); 3066789Sahrens } 3067789Sahrens 30683547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3069789Sahrens } 3070789Sahrens 30717046Sahrens static void 30727046Sahrens write_policy(spa_t *spa, const writeprops_t *wp, 30737046Sahrens int *cksump, int *compp, int *copiesp) 30747046Sahrens { 30757046Sahrens int copies = wp->wp_copies; 30767046Sahrens boolean_t ismd = (wp->wp_level > 0 || dmu_ot[wp->wp_type].ot_metadata); 30777046Sahrens 30787046Sahrens /* Determine copies setting */ 30797046Sahrens if (ismd) 30807046Sahrens copies++; 30817046Sahrens *copiesp = MIN(copies, spa_max_replication(spa)); 30827046Sahrens 30837046Sahrens /* Determine checksum setting */ 30847046Sahrens if (ismd) { 30857046Sahrens /* 30867046Sahrens * Metadata always gets checksummed. If the data 30877046Sahrens * checksum is multi-bit correctable, and it's not a 30887046Sahrens * ZBT-style checksum, then it's suitable for metadata 30897046Sahrens * as well. Otherwise, the metadata checksum defaults 30907046Sahrens * to fletcher4. 30917046Sahrens */ 30927046Sahrens if (zio_checksum_table[wp->wp_oschecksum].ci_correctable && 30937046Sahrens !zio_checksum_table[wp->wp_oschecksum].ci_zbt) 30947046Sahrens *cksump = wp->wp_oschecksum; 30957046Sahrens else 30967046Sahrens *cksump = ZIO_CHECKSUM_FLETCHER_4; 30977046Sahrens } else { 30987046Sahrens *cksump = zio_checksum_select(wp->wp_dnchecksum, 30997046Sahrens wp->wp_oschecksum); 31007046Sahrens } 31017046Sahrens 31027046Sahrens /* Determine compression setting */ 31037046Sahrens if (ismd) { 31047046Sahrens /* 31057046Sahrens * XXX -- we should design a compression algorithm 31067046Sahrens * that specializes in arrays of bps. 31077046Sahrens */ 31087046Sahrens *compp = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY : 31097046Sahrens ZIO_COMPRESS_LZJB; 31107046Sahrens } else { 31117046Sahrens *compp = zio_compress_select(wp->wp_dncompress, 31127046Sahrens wp->wp_oscompress); 31137046Sahrens } 31147046Sahrens } 31157046Sahrens 31163547Smaybee zio_t * 31177046Sahrens arc_write(zio_t *pio, spa_t *spa, const writeprops_t *wp, 31187237Sek110237 boolean_t l2arc, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 31193547Smaybee arc_done_func_t *ready, arc_done_func_t *done, void *private, int priority, 31207237Sek110237 int zio_flags, const zbookmark_t *zb) 3121789Sahrens { 3122789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 31233547Smaybee arc_write_callback_t *callback; 31243547Smaybee zio_t *zio; 31257046Sahrens int cksum, comp, copies; 31267046Sahrens 3127789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 31282237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 31292237Smaybee ASSERT(hdr->b_acb == 0); 31307237Sek110237 if (l2arc) 31317237Sek110237 hdr->b_flags |= ARC_L2CACHE; 31323547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 31333547Smaybee callback->awcb_ready = ready; 31343547Smaybee callback->awcb_done = done; 31353547Smaybee callback->awcb_private = private; 31363547Smaybee callback->awcb_buf = buf; 31377046Sahrens 31387046Sahrens write_policy(spa, wp, &cksum, &comp, &copies); 31397046Sahrens zio = zio_write(pio, spa, cksum, comp, copies, txg, bp, 31407046Sahrens buf->b_data, hdr->b_size, arc_write_ready, arc_write_done, 31417237Sek110237 callback, priority, zio_flags, zb); 3142789Sahrens 31433547Smaybee return (zio); 3144789Sahrens } 3145789Sahrens 3146789Sahrens int 3147789Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 3148789Sahrens zio_done_func_t *done, void *private, uint32_t arc_flags) 3149789Sahrens { 3150789Sahrens arc_buf_hdr_t *ab; 3151789Sahrens kmutex_t *hash_lock; 3152789Sahrens zio_t *zio; 3153789Sahrens 3154789Sahrens /* 3155789Sahrens * If this buffer is in the cache, release it, so it 3156789Sahrens * can be re-used. 3157789Sahrens */ 3158789Sahrens ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 3159789Sahrens if (ab != NULL) { 3160789Sahrens /* 3161789Sahrens * The checksum of blocks to free is not always 3162789Sahrens * preserved (eg. on the deadlist). However, if it is 3163789Sahrens * nonzero, it should match what we have in the cache. 3164789Sahrens */ 3165789Sahrens ASSERT(bp->blk_cksum.zc_word[0] == 0 || 3166789Sahrens ab->b_cksum0 == bp->blk_cksum.zc_word[0]); 31673403Sbmc if (ab->b_state != arc_anon) 31683403Sbmc arc_change_state(arc_anon, ab, hash_lock); 31692391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 31702391Smaybee /* 31712391Smaybee * This should only happen when we prefetch. 31722391Smaybee */ 31732391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 31742391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 31752391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 31762391Smaybee if (HDR_IN_HASH_TABLE(ab)) 31772391Smaybee buf_hash_remove(ab); 31782391Smaybee ab->b_arc_access = 0; 31792391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 31802391Smaybee ab->b_birth = 0; 31812391Smaybee ab->b_cksum0 = 0; 31822391Smaybee ab->b_buf->b_efunc = NULL; 31832391Smaybee ab->b_buf->b_private = NULL; 31842391Smaybee mutex_exit(hash_lock); 31852391Smaybee } else if (refcount_is_zero(&ab->b_refcnt)) { 31865450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3187789Sahrens mutex_exit(hash_lock); 31881544Seschrock arc_hdr_destroy(ab); 31893403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3190789Sahrens } else { 31911589Smaybee /* 31922391Smaybee * We still have an active reference on this 31932391Smaybee * buffer. This can happen, e.g., from 31942391Smaybee * dbuf_unoverride(). 31951589Smaybee */ 31962391Smaybee ASSERT(!HDR_IN_HASH_TABLE(ab)); 3197789Sahrens ab->b_arc_access = 0; 3198789Sahrens bzero(&ab->b_dva, sizeof (dva_t)); 3199789Sahrens ab->b_birth = 0; 3200789Sahrens ab->b_cksum0 = 0; 32011544Seschrock ab->b_buf->b_efunc = NULL; 32021544Seschrock ab->b_buf->b_private = NULL; 3203789Sahrens mutex_exit(hash_lock); 3204789Sahrens } 3205789Sahrens } 3206789Sahrens 3207789Sahrens zio = zio_free(pio, spa, txg, bp, done, private); 3208789Sahrens 3209789Sahrens if (arc_flags & ARC_WAIT) 3210789Sahrens return (zio_wait(zio)); 3211789Sahrens 3212789Sahrens ASSERT(arc_flags & ARC_NOWAIT); 3213789Sahrens zio_nowait(zio); 3214789Sahrens 3215789Sahrens return (0); 3216789Sahrens } 3217789Sahrens 32186245Smaybee static int 32196245Smaybee arc_memory_throttle(uint64_t reserve, uint64_t txg) 32206245Smaybee { 32216245Smaybee #ifdef _KERNEL 32226245Smaybee uint64_t inflight_data = arc_anon->arcs_size; 32236245Smaybee uint64_t available_memory = ptob(freemem); 32246245Smaybee static uint64_t page_load = 0; 32256245Smaybee static uint64_t last_txg = 0; 32266245Smaybee 32276245Smaybee #if defined(__i386) 32286245Smaybee available_memory = 32296245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 32306245Smaybee #endif 32316245Smaybee if (available_memory >= zfs_write_limit_max) 32326245Smaybee return (0); 32336245Smaybee 32346245Smaybee if (txg > last_txg) { 32356245Smaybee last_txg = txg; 32366245Smaybee page_load = 0; 32376245Smaybee } 32386245Smaybee /* 32396245Smaybee * If we are in pageout, we know that memory is already tight, 32406245Smaybee * the arc is already going to be evicting, so we just want to 32416245Smaybee * continue to let page writes occur as quickly as possible. 32426245Smaybee */ 32436245Smaybee if (curproc == proc_pageout) { 32446245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 32456245Smaybee return (ERESTART); 32466245Smaybee /* Note: reserve is inflated, so we deflate */ 32476245Smaybee page_load += reserve / 8; 32486245Smaybee return (0); 32496245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 32506245Smaybee /* memory is low, delay before restarting */ 32516245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 32526245Smaybee return (EAGAIN); 32536245Smaybee } 32546245Smaybee page_load = 0; 32556245Smaybee 32566245Smaybee if (arc_size > arc_c_min) { 32576245Smaybee uint64_t evictable_memory = 32586245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 32596245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 32606245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 32616245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 32626245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 32636245Smaybee } 32646245Smaybee 32656245Smaybee if (inflight_data > available_memory / 4) { 32666245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 32676245Smaybee return (ERESTART); 32686245Smaybee } 32696245Smaybee #endif 32706245Smaybee return (0); 32716245Smaybee } 32726245Smaybee 3273789Sahrens void 32746245Smaybee arc_tempreserve_clear(uint64_t reserve) 3275789Sahrens { 32766245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3277789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3278789Sahrens } 3279789Sahrens 3280789Sahrens int 32816245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3282789Sahrens { 32836245Smaybee int error; 32846245Smaybee 3285789Sahrens #ifdef ZFS_DEBUG 3286789Sahrens /* 3287789Sahrens * Once in a while, fail for no reason. Everything should cope. 3288789Sahrens */ 3289789Sahrens if (spa_get_random(10000) == 0) { 3290789Sahrens dprintf("forcing random failure\n"); 3291789Sahrens return (ERESTART); 3292789Sahrens } 3293789Sahrens #endif 32946245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 32956245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 32966245Smaybee if (reserve > arc_c) 3297982Smaybee return (ENOMEM); 3298982Smaybee 3299789Sahrens /* 33006245Smaybee * Writes will, almost always, require additional memory allocations 33016245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 33026245Smaybee * make sure that there is sufficient available memory for this. 33036245Smaybee */ 33046245Smaybee if (error = arc_memory_throttle(reserve, txg)) 33056245Smaybee return (error); 33066245Smaybee 33076245Smaybee /* 3308982Smaybee * Throttle writes when the amount of dirty data in the cache 3309982Smaybee * gets too large. We try to keep the cache less than half full 3310982Smaybee * of dirty blocks so that our sync times don't grow too large. 3311982Smaybee * Note: if two requests come in concurrently, we might let them 3312982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3313789Sahrens */ 33146245Smaybee if (reserve + arc_tempreserve + arc_anon->arcs_size > arc_c / 2 && 33156245Smaybee arc_anon->arcs_size > arc_c / 4) { 33164309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 33174309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 33184309Smaybee arc_tempreserve>>10, 33194309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 33204309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 33216245Smaybee reserve>>10, arc_c>>10); 3322789Sahrens return (ERESTART); 3323789Sahrens } 33246245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3325789Sahrens return (0); 3326789Sahrens } 3327789Sahrens 3328789Sahrens void 3329789Sahrens arc_init(void) 3330789Sahrens { 3331789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3332789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3333789Sahrens 33342391Smaybee /* Convert seconds to clock ticks */ 33352638Sperrin arc_min_prefetch_lifespan = 1 * hz; 33362391Smaybee 3337789Sahrens /* Start out with 1/8 of all memory */ 33383403Sbmc arc_c = physmem * PAGESIZE / 8; 3339789Sahrens 3340789Sahrens #ifdef _KERNEL 3341789Sahrens /* 3342789Sahrens * On architectures where the physical memory can be larger 3343789Sahrens * than the addressable space (intel in 32-bit mode), we may 3344789Sahrens * need to limit the cache to 1/8 of VM size. 3345789Sahrens */ 33463403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3347789Sahrens #endif 3348789Sahrens 3349982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 33503403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3351982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 33523403Sbmc if (arc_c * 8 >= 1<<30) 33533403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3354789Sahrens else 33553403Sbmc arc_c_max = arc_c_min; 33563403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 33572885Sahrens 33582885Sahrens /* 33592885Sahrens * Allow the tunables to override our calculations if they are 33602885Sahrens * reasonable (ie. over 64MB) 33612885Sahrens */ 33622885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 33633403Sbmc arc_c_max = zfs_arc_max; 33643403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 33653403Sbmc arc_c_min = zfs_arc_min; 33662885Sahrens 33673403Sbmc arc_c = arc_c_max; 33683403Sbmc arc_p = (arc_c >> 1); 3369789Sahrens 33704309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 33714309Smaybee arc_meta_limit = arc_c_max / 4; 33724645Sek110237 33734645Sek110237 /* Allow the tunable to override if it is reasonable */ 33744645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 33754645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 33764645Sek110237 33774309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 33784309Smaybee arc_c_min = arc_meta_limit / 2; 33794309Smaybee 3380789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3381789Sahrens if (kmem_debugging()) 33823403Sbmc arc_c = arc_c / 2; 33833403Sbmc if (arc_c < arc_c_min) 33843403Sbmc arc_c = arc_c_min; 3385789Sahrens 33863403Sbmc arc_anon = &ARC_anon; 33873403Sbmc arc_mru = &ARC_mru; 33883403Sbmc arc_mru_ghost = &ARC_mru_ghost; 33893403Sbmc arc_mfu = &ARC_mfu; 33903403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 33915450Sbrendan arc_l2c_only = &ARC_l2c_only; 33923403Sbmc arc_size = 0; 3393789Sahrens 33943403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33953403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33963403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33973403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33983403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 33995450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34002688Smaybee 34014309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 34024309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34034309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 34044309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34054309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 34064309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34074309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 34084309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34094309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 34104309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34114309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 34124309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34134309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 34144309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34154309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 34164309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34175450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 34185450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34195450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 34205450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3421789Sahrens 3422789Sahrens buf_init(); 3423789Sahrens 3424789Sahrens arc_thread_exit = 0; 34251544Seschrock arc_eviction_list = NULL; 34261544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 34272887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3428789Sahrens 34293403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 34303403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 34313403Sbmc 34323403Sbmc if (arc_ksp != NULL) { 34333403Sbmc arc_ksp->ks_data = &arc_stats; 34343403Sbmc kstat_install(arc_ksp); 34353403Sbmc } 34363403Sbmc 3437789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3438789Sahrens TS_RUN, minclsyspri); 34393158Smaybee 34403158Smaybee arc_dead = FALSE; 34416987Sbrendan arc_warm = B_FALSE; 34426245Smaybee 34436245Smaybee if (zfs_write_limit_max == 0) 3444*7468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 34456245Smaybee else 34466245Smaybee zfs_write_limit_shift = 0; 3447*7468SMark.Maybee@Sun.COM mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3448789Sahrens } 3449789Sahrens 3450789Sahrens void 3451789Sahrens arc_fini(void) 3452789Sahrens { 3453789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3454789Sahrens arc_thread_exit = 1; 3455789Sahrens while (arc_thread_exit != 0) 3456789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3457789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3458789Sahrens 34595642Smaybee arc_flush(NULL); 3460789Sahrens 3461789Sahrens arc_dead = TRUE; 3462789Sahrens 34633403Sbmc if (arc_ksp != NULL) { 34643403Sbmc kstat_delete(arc_ksp); 34653403Sbmc arc_ksp = NULL; 34663403Sbmc } 34673403Sbmc 34681544Seschrock mutex_destroy(&arc_eviction_mtx); 3469789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3470789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3471789Sahrens 34724309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 34734309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 34744309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 34754309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 34764309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 34774309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 34784309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 34794309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3480789Sahrens 34813403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 34823403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 34833403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 34843403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 34853403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 34862856Snd150628 3487*7468SMark.Maybee@Sun.COM mutex_destroy(&zfs_write_limit_lock); 3488*7468SMark.Maybee@Sun.COM 3489789Sahrens buf_fini(); 3490789Sahrens } 34915450Sbrendan 34925450Sbrendan /* 34935450Sbrendan * Level 2 ARC 34945450Sbrendan * 34955450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 34965450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 34975450Sbrendan * using large infrequent writes. The main role of this cache is to boost 34985450Sbrendan * the performance of random read workloads. The intended L2ARC devices 34995450Sbrendan * include short-stroked disks, solid state disks, and other media with 35005450Sbrendan * substantially faster read latency than disk. 35015450Sbrendan * 35025450Sbrendan * +-----------------------+ 35035450Sbrendan * | ARC | 35045450Sbrendan * +-----------------------+ 35055450Sbrendan * | ^ ^ 35065450Sbrendan * | | | 35075450Sbrendan * l2arc_feed_thread() arc_read() 35085450Sbrendan * | | | 35095450Sbrendan * | l2arc read | 35105450Sbrendan * V | | 35115450Sbrendan * +---------------+ | 35125450Sbrendan * | L2ARC | | 35135450Sbrendan * +---------------+ | 35145450Sbrendan * | ^ | 35155450Sbrendan * l2arc_write() | | 35165450Sbrendan * | | | 35175450Sbrendan * V | | 35185450Sbrendan * +-------+ +-------+ 35195450Sbrendan * | vdev | | vdev | 35205450Sbrendan * | cache | | cache | 35215450Sbrendan * +-------+ +-------+ 35225450Sbrendan * +=========+ .-----. 35235450Sbrendan * : L2ARC : |-_____-| 35245450Sbrendan * : devices : | Disks | 35255450Sbrendan * +=========+ `-_____-' 35265450Sbrendan * 35275450Sbrendan * Read requests are satisfied from the following sources, in order: 35285450Sbrendan * 35295450Sbrendan * 1) ARC 35305450Sbrendan * 2) vdev cache of L2ARC devices 35315450Sbrendan * 3) L2ARC devices 35325450Sbrendan * 4) vdev cache of disks 35335450Sbrendan * 5) disks 35345450Sbrendan * 35355450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 35365450Sbrendan * To accommodate for this there are some significant differences between 35375450Sbrendan * the L2ARC and traditional cache design: 35385450Sbrendan * 35395450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 35405450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 35415450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 35425450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 35435450Sbrendan * 35445450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 35455450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 35465450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 35475450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 35485450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 35495450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 35505450Sbrendan * provide a better sense of ratio than this diagram: 35515450Sbrendan * 35525450Sbrendan * head --> tail 35535450Sbrendan * +---------------------+----------+ 35545450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 35555450Sbrendan * +---------------------+----------+ | o L2ARC eligible 35565450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 35575450Sbrendan * +---------------------+----------+ | 35585450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 35595450Sbrendan * headroom | 35605450Sbrendan * l2arc_feed_thread() 35615450Sbrendan * | 35625450Sbrendan * l2arc write hand <--[oooo]--' 35635450Sbrendan * | 8 Mbyte 35645450Sbrendan * | write max 35655450Sbrendan * V 35665450Sbrendan * +==============================+ 35675450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 35685450Sbrendan * +==============================+ 35695450Sbrendan * 32 Gbytes 35705450Sbrendan * 35715450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 35725450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 35735450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 35745450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 35755450Sbrendan * the ARC lists have moved there due to inactivity. 35765450Sbrendan * 35775450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 35785450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 35795450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 35805450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 35815450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 35825450Sbrendan * quickly, such as during backups of the entire pool. 35835450Sbrendan * 35846987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 35856987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 35866987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 35876987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 35886987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 35896987Sbrendan * 35906987Sbrendan * The L2ARC device write speed is also boosted during this time so that 35916987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 35926987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 35936987Sbrendan * through increased writes. 35946987Sbrendan * 35956987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 35965450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 35975450Sbrendan * device is written to in a rotor fashion, sweeping writes through 35985450Sbrendan * available space then repeating. 35995450Sbrendan * 36006987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 36015450Sbrendan * write buffers back to disk based storage. 36025450Sbrendan * 36036987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 36045450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 36055450Sbrendan * 36065450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 36075450Sbrendan * may be necessary for different workloads: 36085450Sbrendan * 36095450Sbrendan * l2arc_write_max max write bytes per interval 36106987Sbrendan * l2arc_write_boost extra write bytes during device warmup 36115450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 36125450Sbrendan * l2arc_headroom number of max device writes to precache 36135450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 36145450Sbrendan * 36155450Sbrendan * Tunables may be removed or added as future performance improvements are 36165450Sbrendan * integrated, and also may become zpool properties. 36175450Sbrendan */ 36185450Sbrendan 36195450Sbrendan static void 36205450Sbrendan l2arc_hdr_stat_add(void) 36215450Sbrendan { 36226018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 36236018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 36245450Sbrendan } 36255450Sbrendan 36265450Sbrendan static void 36275450Sbrendan l2arc_hdr_stat_remove(void) 36285450Sbrendan { 36296018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 36306018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 36315450Sbrendan } 36325450Sbrendan 36335450Sbrendan /* 36345450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 36356987Sbrendan * If a device is returned, this also returns holding the spa config lock. 36365450Sbrendan */ 36375450Sbrendan static l2arc_dev_t * 36385450Sbrendan l2arc_dev_get_next(void) 36395450Sbrendan { 36406987Sbrendan l2arc_dev_t *first, *next = NULL; 36416987Sbrendan 36426987Sbrendan /* 36436987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 36446987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 36456987Sbrendan * both locks will be dropped and a spa config lock held instead. 36466987Sbrendan */ 36476987Sbrendan mutex_enter(&spa_namespace_lock); 36486987Sbrendan mutex_enter(&l2arc_dev_mtx); 36496643Seschrock 36506643Seschrock /* if there are no vdevs, there is nothing to do */ 36516643Seschrock if (l2arc_ndev == 0) 36526987Sbrendan goto out; 36536643Seschrock 36546643Seschrock first = NULL; 36556643Seschrock next = l2arc_dev_last; 36566643Seschrock do { 36576643Seschrock /* loop around the list looking for a non-faulted vdev */ 36586643Seschrock if (next == NULL) { 36595450Sbrendan next = list_head(l2arc_dev_list); 36606643Seschrock } else { 36616643Seschrock next = list_next(l2arc_dev_list, next); 36626643Seschrock if (next == NULL) 36636643Seschrock next = list_head(l2arc_dev_list); 36646643Seschrock } 36656643Seschrock 36666643Seschrock /* if we have come back to the start, bail out */ 36676643Seschrock if (first == NULL) 36686643Seschrock first = next; 36696643Seschrock else if (next == first) 36706643Seschrock break; 36716643Seschrock 36726643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 36736643Seschrock 36746643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 36756643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 36766987Sbrendan next = NULL; 36775450Sbrendan 36785450Sbrendan l2arc_dev_last = next; 36795450Sbrendan 36806987Sbrendan out: 36816987Sbrendan mutex_exit(&l2arc_dev_mtx); 36826987Sbrendan 36836987Sbrendan /* 36846987Sbrendan * Grab the config lock to prevent the 'next' device from being 36856987Sbrendan * removed while we are writing to it. 36866987Sbrendan */ 36876987Sbrendan if (next != NULL) 36886987Sbrendan spa_config_enter(next->l2ad_spa, RW_READER, next); 36896987Sbrendan mutex_exit(&spa_namespace_lock); 36906987Sbrendan 36915450Sbrendan return (next); 36925450Sbrendan } 36935450Sbrendan 36945450Sbrendan /* 36956987Sbrendan * Free buffers that were tagged for destruction. 36966987Sbrendan */ 36976987Sbrendan static void 36986987Sbrendan l2arc_do_free_on_write() 36996987Sbrendan { 37006987Sbrendan list_t *buflist; 37016987Sbrendan l2arc_data_free_t *df, *df_prev; 37026987Sbrendan 37036987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 37046987Sbrendan buflist = l2arc_free_on_write; 37056987Sbrendan 37066987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 37076987Sbrendan df_prev = list_prev(buflist, df); 37086987Sbrendan ASSERT(df->l2df_data != NULL); 37096987Sbrendan ASSERT(df->l2df_func != NULL); 37106987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 37116987Sbrendan list_remove(buflist, df); 37126987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 37136987Sbrendan } 37146987Sbrendan 37156987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 37166987Sbrendan } 37176987Sbrendan 37186987Sbrendan /* 37195450Sbrendan * A write to a cache device has completed. Update all headers to allow 37205450Sbrendan * reads from these buffers to begin. 37215450Sbrendan */ 37225450Sbrendan static void 37235450Sbrendan l2arc_write_done(zio_t *zio) 37245450Sbrendan { 37255450Sbrendan l2arc_write_callback_t *cb; 37265450Sbrendan l2arc_dev_t *dev; 37275450Sbrendan list_t *buflist; 37285450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 37296987Sbrendan l2arc_buf_hdr_t *abl2; 37305450Sbrendan kmutex_t *hash_lock; 37315450Sbrendan 37325450Sbrendan cb = zio->io_private; 37335450Sbrendan ASSERT(cb != NULL); 37345450Sbrendan dev = cb->l2wcb_dev; 37355450Sbrendan ASSERT(dev != NULL); 37365450Sbrendan head = cb->l2wcb_head; 37375450Sbrendan ASSERT(head != NULL); 37385450Sbrendan buflist = dev->l2ad_buflist; 37395450Sbrendan ASSERT(buflist != NULL); 37405450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 37415450Sbrendan l2arc_write_callback_t *, cb); 37425450Sbrendan 37435450Sbrendan if (zio->io_error != 0) 37445450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 37455450Sbrendan 37465450Sbrendan mutex_enter(&l2arc_buflist_mtx); 37475450Sbrendan 37485450Sbrendan /* 37495450Sbrendan * All writes completed, or an error was hit. 37505450Sbrendan */ 37515450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 37525450Sbrendan ab_prev = list_prev(buflist, ab); 37535450Sbrendan 37545450Sbrendan hash_lock = HDR_LOCK(ab); 37555450Sbrendan if (!mutex_tryenter(hash_lock)) { 37565450Sbrendan /* 37575450Sbrendan * This buffer misses out. It may be in a stage 37585450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 37595450Sbrendan * left set, denying reads to this buffer. 37605450Sbrendan */ 37615450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 37625450Sbrendan continue; 37635450Sbrendan } 37645450Sbrendan 37655450Sbrendan if (zio->io_error != 0) { 37665450Sbrendan /* 37676987Sbrendan * Error - drop L2ARC entry. 37685450Sbrendan */ 37696987Sbrendan list_remove(buflist, ab); 37706987Sbrendan abl2 = ab->b_l2hdr; 37715450Sbrendan ab->b_l2hdr = NULL; 37726987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 37736987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 37745450Sbrendan } 37755450Sbrendan 37765450Sbrendan /* 37775450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 37785450Sbrendan */ 37795450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 37805450Sbrendan 37815450Sbrendan mutex_exit(hash_lock); 37825450Sbrendan } 37835450Sbrendan 37845450Sbrendan atomic_inc_64(&l2arc_writes_done); 37855450Sbrendan list_remove(buflist, head); 37865450Sbrendan kmem_cache_free(hdr_cache, head); 37875450Sbrendan mutex_exit(&l2arc_buflist_mtx); 37885450Sbrendan 37896987Sbrendan l2arc_do_free_on_write(); 37905450Sbrendan 37915450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 37925450Sbrendan } 37935450Sbrendan 37945450Sbrendan /* 37955450Sbrendan * A read to a cache device completed. Validate buffer contents before 37965450Sbrendan * handing over to the regular ARC routines. 37975450Sbrendan */ 37985450Sbrendan static void 37995450Sbrendan l2arc_read_done(zio_t *zio) 38005450Sbrendan { 38015450Sbrendan l2arc_read_callback_t *cb; 38025450Sbrendan arc_buf_hdr_t *hdr; 38035450Sbrendan arc_buf_t *buf; 38045450Sbrendan zio_t *rzio; 38055450Sbrendan kmutex_t *hash_lock; 38066987Sbrendan int equal; 38075450Sbrendan 38085450Sbrendan cb = zio->io_private; 38095450Sbrendan ASSERT(cb != NULL); 38105450Sbrendan buf = cb->l2rcb_buf; 38115450Sbrendan ASSERT(buf != NULL); 38125450Sbrendan hdr = buf->b_hdr; 38135450Sbrendan ASSERT(hdr != NULL); 38145450Sbrendan 38155450Sbrendan hash_lock = HDR_LOCK(hdr); 38165450Sbrendan mutex_enter(hash_lock); 38175450Sbrendan 38185450Sbrendan /* 38195450Sbrendan * Check this survived the L2ARC journey. 38205450Sbrendan */ 38215450Sbrendan equal = arc_cksum_equal(buf); 38225450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 38235450Sbrendan mutex_exit(hash_lock); 38245450Sbrendan zio->io_private = buf; 38255450Sbrendan arc_read_done(zio); 38265450Sbrendan } else { 38275450Sbrendan mutex_exit(hash_lock); 38285450Sbrendan /* 38295450Sbrendan * Buffer didn't survive caching. Increment stats and 38305450Sbrendan * reissue to the original storage device. 38315450Sbrendan */ 38326987Sbrendan if (zio->io_error != 0) { 38335450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 38346987Sbrendan } else { 38356987Sbrendan zio->io_error = EIO; 38366987Sbrendan } 38375450Sbrendan if (!equal) 38385450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 38395450Sbrendan 38406987Sbrendan if (zio->io_waiter == NULL) { 38416987Sbrendan /* 38426987Sbrendan * Let the resent I/O call arc_read_done() instead. 38436987Sbrendan */ 38446987Sbrendan zio->io_done = NULL; 38456987Sbrendan zio->io_flags &= ~ZIO_FLAG_DONT_CACHE; 38466987Sbrendan 38477361SBrendan.Gregg@Sun.COM rzio = zio_read(zio->io_parent, cb->l2rcb_spa, 38487361SBrendan.Gregg@Sun.COM &cb->l2rcb_bp, buf->b_data, zio->io_size, 38497361SBrendan.Gregg@Sun.COM arc_read_done, buf, zio->io_priority, 38507361SBrendan.Gregg@Sun.COM cb->l2rcb_flags, &cb->l2rcb_zb); 38516987Sbrendan 38526987Sbrendan (void) zio_nowait(rzio); 38536987Sbrendan } 38545450Sbrendan } 38555450Sbrendan 38565450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 38575450Sbrendan } 38585450Sbrendan 38595450Sbrendan /* 38605450Sbrendan * This is the list priority from which the L2ARC will search for pages to 38615450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 38625450Sbrendan * desired order. This order can have a significant effect on cache 38635450Sbrendan * performance. 38645450Sbrendan * 38655450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 38665450Sbrendan * the data lists. This function returns a locked list, and also returns 38675450Sbrendan * the lock pointer. 38685450Sbrendan */ 38695450Sbrendan static list_t * 38705450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 38715450Sbrendan { 38725450Sbrendan list_t *list; 38735450Sbrendan 38745450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 38755450Sbrendan 38765450Sbrendan switch (list_num) { 38775450Sbrendan case 0: 38785450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 38795450Sbrendan *lock = &arc_mfu->arcs_mtx; 38805450Sbrendan break; 38815450Sbrendan case 1: 38825450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 38835450Sbrendan *lock = &arc_mru->arcs_mtx; 38845450Sbrendan break; 38855450Sbrendan case 2: 38865450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 38875450Sbrendan *lock = &arc_mfu->arcs_mtx; 38885450Sbrendan break; 38895450Sbrendan case 3: 38905450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 38915450Sbrendan *lock = &arc_mru->arcs_mtx; 38925450Sbrendan break; 38935450Sbrendan } 38945450Sbrendan 38955450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 38965450Sbrendan mutex_enter(*lock); 38975450Sbrendan return (list); 38985450Sbrendan } 38995450Sbrendan 39005450Sbrendan /* 39015450Sbrendan * Evict buffers from the device write hand to the distance specified in 39025450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 39035450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 39045450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 39055450Sbrendan */ 39065450Sbrendan static void 39075450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 39085450Sbrendan { 39095450Sbrendan list_t *buflist; 39105450Sbrendan l2arc_buf_hdr_t *abl2; 39115450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 39125450Sbrendan kmutex_t *hash_lock; 39135450Sbrendan uint64_t taddr; 39145450Sbrendan 39155450Sbrendan buflist = dev->l2ad_buflist; 39165450Sbrendan 39175450Sbrendan if (buflist == NULL) 39185450Sbrendan return; 39195450Sbrendan 39205450Sbrendan if (!all && dev->l2ad_first) { 39215450Sbrendan /* 39225450Sbrendan * This is the first sweep through the device. There is 39235450Sbrendan * nothing to evict. 39245450Sbrendan */ 39255450Sbrendan return; 39265450Sbrendan } 39275450Sbrendan 39286987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 39295450Sbrendan /* 39305450Sbrendan * When nearing the end of the device, evict to the end 39315450Sbrendan * before the device write hand jumps to the start. 39325450Sbrendan */ 39335450Sbrendan taddr = dev->l2ad_end; 39345450Sbrendan } else { 39355450Sbrendan taddr = dev->l2ad_hand + distance; 39365450Sbrendan } 39375450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 39385450Sbrendan uint64_t, taddr, boolean_t, all); 39395450Sbrendan 39405450Sbrendan top: 39415450Sbrendan mutex_enter(&l2arc_buflist_mtx); 39425450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 39435450Sbrendan ab_prev = list_prev(buflist, ab); 39445450Sbrendan 39455450Sbrendan hash_lock = HDR_LOCK(ab); 39465450Sbrendan if (!mutex_tryenter(hash_lock)) { 39475450Sbrendan /* 39485450Sbrendan * Missed the hash lock. Retry. 39495450Sbrendan */ 39505450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 39515450Sbrendan mutex_exit(&l2arc_buflist_mtx); 39525450Sbrendan mutex_enter(hash_lock); 39535450Sbrendan mutex_exit(hash_lock); 39545450Sbrendan goto top; 39555450Sbrendan } 39565450Sbrendan 39575450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 39585450Sbrendan /* 39595450Sbrendan * We hit a write head node. Leave it for 39605450Sbrendan * l2arc_write_done(). 39615450Sbrendan */ 39625450Sbrendan list_remove(buflist, ab); 39635450Sbrendan mutex_exit(hash_lock); 39645450Sbrendan continue; 39655450Sbrendan } 39665450Sbrendan 39675450Sbrendan if (!all && ab->b_l2hdr != NULL && 39685450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 39695450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 39705450Sbrendan /* 39715450Sbrendan * We've evicted to the target address, 39725450Sbrendan * or the end of the device. 39735450Sbrendan */ 39745450Sbrendan mutex_exit(hash_lock); 39755450Sbrendan break; 39765450Sbrendan } 39775450Sbrendan 39785450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 39795450Sbrendan /* 39805450Sbrendan * Already on the path to destruction. 39815450Sbrendan */ 39825450Sbrendan mutex_exit(hash_lock); 39835450Sbrendan continue; 39845450Sbrendan } 39855450Sbrendan 39865450Sbrendan if (ab->b_state == arc_l2c_only) { 39875450Sbrendan ASSERT(!HDR_L2_READING(ab)); 39885450Sbrendan /* 39895450Sbrendan * This doesn't exist in the ARC. Destroy. 39905450Sbrendan * arc_hdr_destroy() will call list_remove() 39915450Sbrendan * and decrement arcstat_l2_size. 39925450Sbrendan */ 39935450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 39945450Sbrendan arc_hdr_destroy(ab); 39955450Sbrendan } else { 39965450Sbrendan /* 39976987Sbrendan * Invalidate issued or about to be issued 39986987Sbrendan * reads, since we may be about to write 39996987Sbrendan * over this location. 40006987Sbrendan */ 40016987Sbrendan if (HDR_L2_READING(ab)) { 40026987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 40036987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 40046987Sbrendan } 40056987Sbrendan 40066987Sbrendan /* 40075450Sbrendan * Tell ARC this no longer exists in L2ARC. 40085450Sbrendan */ 40095450Sbrendan if (ab->b_l2hdr != NULL) { 40105450Sbrendan abl2 = ab->b_l2hdr; 40115450Sbrendan ab->b_l2hdr = NULL; 40125450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 40135450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 40145450Sbrendan } 40155450Sbrendan list_remove(buflist, ab); 40165450Sbrendan 40175450Sbrendan /* 40185450Sbrendan * This may have been leftover after a 40195450Sbrendan * failed write. 40205450Sbrendan */ 40215450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 40225450Sbrendan } 40235450Sbrendan mutex_exit(hash_lock); 40245450Sbrendan } 40255450Sbrendan mutex_exit(&l2arc_buflist_mtx); 40265450Sbrendan 40275450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, -(taddr - dev->l2ad_evict)); 40285450Sbrendan dev->l2ad_evict = taddr; 40295450Sbrendan } 40305450Sbrendan 40315450Sbrendan /* 40325450Sbrendan * Find and write ARC buffers to the L2ARC device. 40335450Sbrendan * 40345450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 40355450Sbrendan * for reading until they have completed writing. 40365450Sbrendan */ 40375450Sbrendan static void 40386987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 40395450Sbrendan { 40405450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 40415450Sbrendan l2arc_buf_hdr_t *hdrl2; 40425450Sbrendan list_t *list; 40436987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 40445450Sbrendan void *buf_data; 40455450Sbrendan kmutex_t *hash_lock, *list_lock; 40465450Sbrendan boolean_t have_lock, full; 40475450Sbrendan l2arc_write_callback_t *cb; 40485450Sbrendan zio_t *pio, *wzio; 40495450Sbrendan 40505450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 40515450Sbrendan 40525450Sbrendan pio = NULL; 40535450Sbrendan write_sz = 0; 40545450Sbrendan full = B_FALSE; 40556245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 40565450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 40575450Sbrendan 40585450Sbrendan /* 40595450Sbrendan * Copy buffers for L2ARC writing. 40605450Sbrendan */ 40615450Sbrendan mutex_enter(&l2arc_buflist_mtx); 40625450Sbrendan for (int try = 0; try <= 3; try++) { 40635450Sbrendan list = l2arc_list_locked(try, &list_lock); 40645450Sbrendan passed_sz = 0; 40655450Sbrendan 40666987Sbrendan /* 40676987Sbrendan * L2ARC fast warmup. 40686987Sbrendan * 40696987Sbrendan * Until the ARC is warm and starts to evict, read from the 40706987Sbrendan * head of the ARC lists rather than the tail. 40716987Sbrendan */ 40726987Sbrendan headroom = target_sz * l2arc_headroom; 40736987Sbrendan if (arc_warm == B_FALSE) 40746987Sbrendan ab = list_head(list); 40756987Sbrendan else 40766987Sbrendan ab = list_tail(list); 40776987Sbrendan 40786987Sbrendan for (; ab; ab = ab_prev) { 40796987Sbrendan if (arc_warm == B_FALSE) 40806987Sbrendan ab_prev = list_next(list, ab); 40816987Sbrendan else 40826987Sbrendan ab_prev = list_prev(list, ab); 40835450Sbrendan 40845450Sbrendan hash_lock = HDR_LOCK(ab); 40855450Sbrendan have_lock = MUTEX_HELD(hash_lock); 40865450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 40875450Sbrendan /* 40885450Sbrendan * Skip this buffer rather than waiting. 40895450Sbrendan */ 40905450Sbrendan continue; 40915450Sbrendan } 40925450Sbrendan 40935450Sbrendan passed_sz += ab->b_size; 40945450Sbrendan if (passed_sz > headroom) { 40955450Sbrendan /* 40965450Sbrendan * Searched too far. 40975450Sbrendan */ 40985450Sbrendan mutex_exit(hash_lock); 40995450Sbrendan break; 41005450Sbrendan } 41015450Sbrendan 41025450Sbrendan if (ab->b_spa != spa) { 41035450Sbrendan mutex_exit(hash_lock); 41045450Sbrendan continue; 41055450Sbrendan } 41065450Sbrendan 41075450Sbrendan if (ab->b_l2hdr != NULL) { 41085450Sbrendan /* 41095450Sbrendan * Already in L2ARC. 41105450Sbrendan */ 41115450Sbrendan mutex_exit(hash_lock); 41125450Sbrendan continue; 41135450Sbrendan } 41145450Sbrendan 41157237Sek110237 if (HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) { 41165450Sbrendan mutex_exit(hash_lock); 41175450Sbrendan continue; 41185450Sbrendan } 41195450Sbrendan 41205450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 41215450Sbrendan full = B_TRUE; 41225450Sbrendan mutex_exit(hash_lock); 41235450Sbrendan break; 41245450Sbrendan } 41255450Sbrendan 41265450Sbrendan if (ab->b_buf == NULL) { 41275450Sbrendan DTRACE_PROBE1(l2arc__buf__null, void *, ab); 41285450Sbrendan mutex_exit(hash_lock); 41295450Sbrendan continue; 41305450Sbrendan } 41315450Sbrendan 41325450Sbrendan if (pio == NULL) { 41335450Sbrendan /* 41345450Sbrendan * Insert a dummy header on the buflist so 41355450Sbrendan * l2arc_write_done() can find where the 41365450Sbrendan * write buffers begin without searching. 41375450Sbrendan */ 41385450Sbrendan list_insert_head(dev->l2ad_buflist, head); 41395450Sbrendan 41405450Sbrendan cb = kmem_alloc( 41415450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 41425450Sbrendan cb->l2wcb_dev = dev; 41435450Sbrendan cb->l2wcb_head = head; 41445450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 41455450Sbrendan ZIO_FLAG_CANFAIL); 41465450Sbrendan } 41475450Sbrendan 41485450Sbrendan /* 41495450Sbrendan * Create and add a new L2ARC header. 41505450Sbrendan */ 41515450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 41525450Sbrendan hdrl2->b_dev = dev; 41535450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 41545450Sbrendan 41555450Sbrendan ab->b_flags |= ARC_L2_WRITING; 41565450Sbrendan ab->b_l2hdr = hdrl2; 41575450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 41585450Sbrendan buf_data = ab->b_buf->b_data; 41595450Sbrendan buf_sz = ab->b_size; 41605450Sbrendan 41615450Sbrendan /* 41625450Sbrendan * Compute and store the buffer cksum before 41635450Sbrendan * writing. On debug the cksum is verified first. 41645450Sbrendan */ 41655450Sbrendan arc_cksum_verify(ab->b_buf); 41665450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 41675450Sbrendan 41685450Sbrendan mutex_exit(hash_lock); 41695450Sbrendan 41705450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 41715450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 41725450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 41735450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 41745450Sbrendan 41755450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 41765450Sbrendan zio_t *, wzio); 41775450Sbrendan (void) zio_nowait(wzio); 41785450Sbrendan 41795450Sbrendan write_sz += buf_sz; 41805450Sbrendan dev->l2ad_hand += buf_sz; 41815450Sbrendan } 41825450Sbrendan 41835450Sbrendan mutex_exit(list_lock); 41845450Sbrendan 41855450Sbrendan if (full == B_TRUE) 41865450Sbrendan break; 41875450Sbrendan } 41885450Sbrendan mutex_exit(&l2arc_buflist_mtx); 41895450Sbrendan 41905450Sbrendan if (pio == NULL) { 41915450Sbrendan ASSERT3U(write_sz, ==, 0); 41925450Sbrendan kmem_cache_free(hdr_cache, head); 41935450Sbrendan return; 41945450Sbrendan } 41955450Sbrendan 41965450Sbrendan ASSERT3U(write_sz, <=, target_sz); 41975450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 41985450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 41995450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, write_sz); 42005450Sbrendan 42015450Sbrendan /* 42025450Sbrendan * Bump device hand to the device start if it is approaching the end. 42035450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 42045450Sbrendan */ 42056987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 42065450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, 42075450Sbrendan dev->l2ad_end - dev->l2ad_hand); 42085450Sbrendan dev->l2ad_hand = dev->l2ad_start; 42095450Sbrendan dev->l2ad_evict = dev->l2ad_start; 42105450Sbrendan dev->l2ad_first = B_FALSE; 42115450Sbrendan } 42125450Sbrendan 42135450Sbrendan (void) zio_wait(pio); 42145450Sbrendan } 42155450Sbrendan 42165450Sbrendan /* 42175450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 42185450Sbrendan * heart of the L2ARC. 42195450Sbrendan */ 42205450Sbrendan static void 42215450Sbrendan l2arc_feed_thread(void) 42225450Sbrendan { 42235450Sbrendan callb_cpr_t cpr; 42245450Sbrendan l2arc_dev_t *dev; 42255450Sbrendan spa_t *spa; 42266987Sbrendan uint64_t size; 42275450Sbrendan 42285450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 42295450Sbrendan 42305450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 42315450Sbrendan 42325450Sbrendan while (l2arc_thread_exit == 0) { 42335450Sbrendan /* 42346987Sbrendan * Pause for l2arc_feed_secs seconds between writes. 42355450Sbrendan */ 42365450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 42376987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 42386987Sbrendan lbolt + (hz * l2arc_feed_secs)); 42396987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 42406987Sbrendan 42416987Sbrendan /* 42426987Sbrendan * Quick check for L2ARC devices. 42436987Sbrendan */ 42446987Sbrendan mutex_enter(&l2arc_dev_mtx); 42456987Sbrendan if (l2arc_ndev == 0) { 42466987Sbrendan mutex_exit(&l2arc_dev_mtx); 42476987Sbrendan continue; 42485450Sbrendan } 42496987Sbrendan mutex_exit(&l2arc_dev_mtx); 42506643Seschrock 42515450Sbrendan /* 42526643Seschrock * This selects the next l2arc device to write to, and in 42536643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 42546987Sbrendan * will return NULL if there are now no l2arc devices or if 42556987Sbrendan * they are all faulted. 42566987Sbrendan * 42576987Sbrendan * If a device is returned, its spa's config lock is also 42586987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 42596987Sbrendan * will grab and release l2arc_dev_mtx. 42605450Sbrendan */ 42616987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 42625450Sbrendan continue; 42636987Sbrendan 42646987Sbrendan spa = dev->l2ad_spa; 42656987Sbrendan ASSERT(spa != NULL); 42665450Sbrendan 42675450Sbrendan /* 42685450Sbrendan * Avoid contributing to memory pressure. 42695450Sbrendan */ 42705450Sbrendan if (arc_reclaim_needed()) { 42715450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 42726987Sbrendan spa_config_exit(spa, dev); 42735450Sbrendan continue; 42745450Sbrendan } 42755450Sbrendan 42765450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 42775450Sbrendan 42786987Sbrendan size = dev->l2ad_write; 42796987Sbrendan if (arc_warm == B_FALSE) 42806987Sbrendan size += dev->l2ad_boost; 42816987Sbrendan 42825450Sbrendan /* 42835450Sbrendan * Evict L2ARC buffers that will be overwritten. 42845450Sbrendan */ 42856987Sbrendan l2arc_evict(dev, size, B_FALSE); 42865450Sbrendan 42875450Sbrendan /* 42885450Sbrendan * Write ARC buffers. 42895450Sbrendan */ 42906987Sbrendan l2arc_write_buffers(spa, dev, size); 42916987Sbrendan spa_config_exit(spa, dev); 42925450Sbrendan } 42935450Sbrendan 42945450Sbrendan l2arc_thread_exit = 0; 42955450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 42965450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 42975450Sbrendan thread_exit(); 42985450Sbrendan } 42995450Sbrendan 43006643Seschrock boolean_t 43016643Seschrock l2arc_vdev_present(vdev_t *vd) 43026643Seschrock { 43036643Seschrock l2arc_dev_t *dev; 43046643Seschrock 43056643Seschrock mutex_enter(&l2arc_dev_mtx); 43066643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 43076643Seschrock dev = list_next(l2arc_dev_list, dev)) { 43086643Seschrock if (dev->l2ad_vdev == vd) 43096643Seschrock break; 43106643Seschrock } 43116643Seschrock mutex_exit(&l2arc_dev_mtx); 43126643Seschrock 43136643Seschrock return (dev != NULL); 43146643Seschrock } 43156643Seschrock 43165450Sbrendan /* 43175450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 43185450Sbrendan * validated the vdev and opened it. 43195450Sbrendan */ 43205450Sbrendan void 43215450Sbrendan l2arc_add_vdev(spa_t *spa, vdev_t *vd, uint64_t start, uint64_t end) 43225450Sbrendan { 43235450Sbrendan l2arc_dev_t *adddev; 43245450Sbrendan 43256643Seschrock ASSERT(!l2arc_vdev_present(vd)); 43266643Seschrock 43275450Sbrendan /* 43285450Sbrendan * Create a new l2arc device entry. 43295450Sbrendan */ 43305450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 43315450Sbrendan adddev->l2ad_spa = spa; 43325450Sbrendan adddev->l2ad_vdev = vd; 43335450Sbrendan adddev->l2ad_write = l2arc_write_max; 43346987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 43355450Sbrendan adddev->l2ad_start = start; 43365450Sbrendan adddev->l2ad_end = end; 43375450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 43385450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 43395450Sbrendan adddev->l2ad_first = B_TRUE; 43405450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 43415450Sbrendan 43425450Sbrendan /* 43435450Sbrendan * This is a list of all ARC buffers that are still valid on the 43445450Sbrendan * device. 43455450Sbrendan */ 43465450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 43475450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 43485450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 43495450Sbrendan 43505450Sbrendan spa_l2cache_space_update(vd, adddev->l2ad_end - adddev->l2ad_hand, 0); 43515450Sbrendan 43525450Sbrendan /* 43535450Sbrendan * Add device to global list 43545450Sbrendan */ 43555450Sbrendan mutex_enter(&l2arc_dev_mtx); 43565450Sbrendan list_insert_head(l2arc_dev_list, adddev); 43575450Sbrendan atomic_inc_64(&l2arc_ndev); 43585450Sbrendan mutex_exit(&l2arc_dev_mtx); 43595450Sbrendan } 43605450Sbrendan 43615450Sbrendan /* 43625450Sbrendan * Remove a vdev from the L2ARC. 43635450Sbrendan */ 43645450Sbrendan void 43655450Sbrendan l2arc_remove_vdev(vdev_t *vd) 43665450Sbrendan { 43675450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 43685450Sbrendan 43695450Sbrendan /* 43705450Sbrendan * Find the device by vdev 43715450Sbrendan */ 43725450Sbrendan mutex_enter(&l2arc_dev_mtx); 43735450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 43745450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 43755450Sbrendan if (vd == dev->l2ad_vdev) { 43765450Sbrendan remdev = dev; 43775450Sbrendan break; 43785450Sbrendan } 43795450Sbrendan } 43805450Sbrendan ASSERT(remdev != NULL); 43815450Sbrendan 43825450Sbrendan /* 43835450Sbrendan * Remove device from global list 43845450Sbrendan */ 43855450Sbrendan list_remove(l2arc_dev_list, remdev); 43865450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 43876987Sbrendan atomic_dec_64(&l2arc_ndev); 43886987Sbrendan mutex_exit(&l2arc_dev_mtx); 43895450Sbrendan 43905450Sbrendan /* 43915450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 43925450Sbrendan */ 43935450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 43945450Sbrendan list_destroy(remdev->l2ad_buflist); 43955450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 43965450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 43975450Sbrendan } 43985450Sbrendan 43995450Sbrendan void 44005450Sbrendan l2arc_init() 44015450Sbrendan { 44025450Sbrendan l2arc_thread_exit = 0; 44035450Sbrendan l2arc_ndev = 0; 44045450Sbrendan l2arc_writes_sent = 0; 44055450Sbrendan l2arc_writes_done = 0; 44065450Sbrendan 44075450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 44085450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 44095450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 44105450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 44115450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 44125450Sbrendan 44135450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 44145450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 44155450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 44165450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 44175450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 44185450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 44195450Sbrendan 44205450Sbrendan (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 44215450Sbrendan TS_RUN, minclsyspri); 44225450Sbrendan } 44235450Sbrendan 44245450Sbrendan void 44255450Sbrendan l2arc_fini() 44265450Sbrendan { 44276987Sbrendan /* 44286987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 44296987Sbrendan * Because of this, we can assume that all l2arc devices have 44306987Sbrendan * already been removed when the pools themselves were removed. 44316987Sbrendan */ 44326987Sbrendan 44335450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 44345450Sbrendan cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 44355450Sbrendan l2arc_thread_exit = 1; 44365450Sbrendan while (l2arc_thread_exit != 0) 44375450Sbrendan cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 44385450Sbrendan mutex_exit(&l2arc_feed_thr_lock); 44395450Sbrendan 44406987Sbrendan l2arc_do_free_on_write(); 44416987Sbrendan 44425450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 44435450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 44445450Sbrendan mutex_destroy(&l2arc_dev_mtx); 44455450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 44465450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 44475450Sbrendan 44485450Sbrendan list_destroy(l2arc_dev_list); 44495450Sbrendan list_destroy(l2arc_free_on_write); 44505450Sbrendan } 4451