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; 1427468SMark.Maybee@Sun.COM extern kmutex_t zfs_write_limit_lock; 1436245Smaybee 1441484Sek110237 #define ARC_REDUCE_DNLC_PERCENT 3 1451484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 1461484Sek110237 147789Sahrens typedef enum arc_reclaim_strategy { 148789Sahrens ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 149789Sahrens ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 150789Sahrens } arc_reclaim_strategy_t; 151789Sahrens 152789Sahrens /* number of seconds before growing cache again */ 153789Sahrens static int arc_grow_retry = 60; 154789Sahrens 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; 442789Sahrens }; 443789Sahrens 4441544Seschrock static arc_buf_t *arc_eviction_list; 4451544Seschrock static kmutex_t arc_eviction_mtx; 4462887Smaybee static arc_buf_hdr_t arc_eviction_hdr; 4472688Smaybee static void arc_get_data_buf(arc_buf_t *buf); 4482688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 4494309Smaybee static int arc_evict_needed(arc_buf_contents_t type); 4505642Smaybee static void arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes); 4511544Seschrock 4521544Seschrock #define GHOST_STATE(state) \ 4535450Sbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 4545450Sbrendan (state) == arc_l2c_only) 4551544Seschrock 456789Sahrens /* 457789Sahrens * Private ARC flags. These flags are private ARC only flags that will show up 458789Sahrens * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 459789Sahrens * be passed in as arc_flags in things like arc_read. However, these flags 460789Sahrens * should never be passed and should only be set by ARC code. When adding new 461789Sahrens * public flags, make sure not to smash the private ones. 462789Sahrens */ 463789Sahrens 4641544Seschrock #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 465789Sahrens #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 466789Sahrens #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 467789Sahrens #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 4681544Seschrock #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 4692391Smaybee #define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 4705450Sbrendan #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 4717237Sek110237 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 4727237Sek110237 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 4737237Sek110237 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 4747237Sek110237 #define ARC_STORED (1 << 19) /* has been store()d to */ 475789Sahrens 4761544Seschrock #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 477789Sahrens #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 478789Sahrens #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 479789Sahrens #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 4801544Seschrock #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 4815450Sbrendan #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 4827237Sek110237 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 4836987Sbrendan #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 4846987Sbrendan (hdr)->b_l2hdr != NULL) 4855450Sbrendan #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 4865450Sbrendan #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 4875450Sbrendan #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 488789Sahrens 489789Sahrens /* 4906018Sbrendan * Other sizes 4916018Sbrendan */ 4926018Sbrendan 4936018Sbrendan #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 4946018Sbrendan #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 4956018Sbrendan 4966018Sbrendan /* 497789Sahrens * Hash table routines 498789Sahrens */ 499789Sahrens 500789Sahrens #define HT_LOCK_PAD 64 501789Sahrens 502789Sahrens struct ht_lock { 503789Sahrens kmutex_t ht_lock; 504789Sahrens #ifdef _KERNEL 505789Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 506789Sahrens #endif 507789Sahrens }; 508789Sahrens 509789Sahrens #define BUF_LOCKS 256 510789Sahrens typedef struct buf_hash_table { 511789Sahrens uint64_t ht_mask; 512789Sahrens arc_buf_hdr_t **ht_table; 513789Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 514789Sahrens } buf_hash_table_t; 515789Sahrens 516789Sahrens static buf_hash_table_t buf_hash_table; 517789Sahrens 518789Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 519789Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 520789Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 521789Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 522789Sahrens #define HDR_LOCK(buf) \ 523789Sahrens (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth))) 524789Sahrens 525789Sahrens uint64_t zfs_crc64_table[256]; 526789Sahrens 5275450Sbrendan /* 5285450Sbrendan * Level 2 ARC 5295450Sbrendan */ 5305450Sbrendan 5315450Sbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 5325450Sbrendan #define L2ARC_HEADROOM 4 /* num of writes */ 5335450Sbrendan #define L2ARC_FEED_SECS 1 /* caching interval */ 5345450Sbrendan 5355450Sbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 5365450Sbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 5375450Sbrendan 5385450Sbrendan /* 5395450Sbrendan * L2ARC Performance Tunables 5405450Sbrendan */ 5415450Sbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 5426987Sbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 5435450Sbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 5445450Sbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 5455450Sbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 5465450Sbrendan 5475450Sbrendan /* 5485450Sbrendan * L2ARC Internals 5495450Sbrendan */ 5505450Sbrendan typedef struct l2arc_dev { 5515450Sbrendan vdev_t *l2ad_vdev; /* vdev */ 5525450Sbrendan spa_t *l2ad_spa; /* spa */ 5535450Sbrendan uint64_t l2ad_hand; /* next write location */ 5545450Sbrendan uint64_t l2ad_write; /* desired write size, bytes */ 5556987Sbrendan uint64_t l2ad_boost; /* warmup write boost, bytes */ 5565450Sbrendan uint64_t l2ad_start; /* first addr on device */ 5575450Sbrendan uint64_t l2ad_end; /* last addr on device */ 5585450Sbrendan uint64_t l2ad_evict; /* last addr eviction reached */ 5595450Sbrendan boolean_t l2ad_first; /* first sweep through */ 5605450Sbrendan list_t *l2ad_buflist; /* buffer list */ 5615450Sbrendan list_node_t l2ad_node; /* device list node */ 5625450Sbrendan } l2arc_dev_t; 5635450Sbrendan 5645450Sbrendan static list_t L2ARC_dev_list; /* device list */ 5655450Sbrendan static list_t *l2arc_dev_list; /* device list pointer */ 5665450Sbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 5675450Sbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 5685450Sbrendan static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 5695450Sbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 5705450Sbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 5715450Sbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 5725450Sbrendan static uint64_t l2arc_ndev; /* number of devices */ 5735450Sbrendan 5745450Sbrendan typedef struct l2arc_read_callback { 5755450Sbrendan arc_buf_t *l2rcb_buf; /* read buffer */ 5765450Sbrendan spa_t *l2rcb_spa; /* spa */ 5775450Sbrendan blkptr_t l2rcb_bp; /* original blkptr */ 5785450Sbrendan zbookmark_t l2rcb_zb; /* original bookmark */ 5795450Sbrendan int l2rcb_flags; /* original flags */ 5805450Sbrendan } l2arc_read_callback_t; 5815450Sbrendan 5825450Sbrendan typedef struct l2arc_write_callback { 5835450Sbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 5845450Sbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 5855450Sbrendan } l2arc_write_callback_t; 5865450Sbrendan 5875450Sbrendan struct l2arc_buf_hdr { 5885450Sbrendan /* protected by arc_buf_hdr mutex */ 5895450Sbrendan l2arc_dev_t *b_dev; /* L2ARC device */ 5905450Sbrendan daddr_t b_daddr; /* disk address, offset byte */ 5915450Sbrendan }; 5925450Sbrendan 5935450Sbrendan typedef struct l2arc_data_free { 5945450Sbrendan /* protected by l2arc_free_on_write_mtx */ 5955450Sbrendan void *l2df_data; 5965450Sbrendan size_t l2df_size; 5975450Sbrendan void (*l2df_func)(void *, size_t); 5985450Sbrendan list_node_t l2df_list_node; 5995450Sbrendan } l2arc_data_free_t; 6005450Sbrendan 6015450Sbrendan static kmutex_t l2arc_feed_thr_lock; 6025450Sbrendan static kcondvar_t l2arc_feed_thr_cv; 6035450Sbrendan static uint8_t l2arc_thread_exit; 6045450Sbrendan 6055450Sbrendan static void l2arc_read_done(zio_t *zio); 6065450Sbrendan static void l2arc_hdr_stat_add(void); 6075450Sbrendan static void l2arc_hdr_stat_remove(void); 6085450Sbrendan 609789Sahrens static uint64_t 6107046Sahrens buf_hash(spa_t *spa, const dva_t *dva, uint64_t birth) 611789Sahrens { 612789Sahrens uintptr_t spav = (uintptr_t)spa; 613789Sahrens uint8_t *vdva = (uint8_t *)dva; 614789Sahrens uint64_t crc = -1ULL; 615789Sahrens int i; 616789Sahrens 617789Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 618789Sahrens 619789Sahrens for (i = 0; i < sizeof (dva_t); i++) 620789Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 621789Sahrens 622789Sahrens crc ^= (spav>>8) ^ birth; 623789Sahrens 624789Sahrens return (crc); 625789Sahrens } 626789Sahrens 627789Sahrens #define BUF_EMPTY(buf) \ 628789Sahrens ((buf)->b_dva.dva_word[0] == 0 && \ 629789Sahrens (buf)->b_dva.dva_word[1] == 0 && \ 630789Sahrens (buf)->b_birth == 0) 631789Sahrens 632789Sahrens #define BUF_EQUAL(spa, dva, birth, buf) \ 633789Sahrens ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 634789Sahrens ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 635789Sahrens ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 636789Sahrens 637789Sahrens static arc_buf_hdr_t * 6387046Sahrens buf_hash_find(spa_t *spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 639789Sahrens { 640789Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 641789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 642789Sahrens arc_buf_hdr_t *buf; 643789Sahrens 644789Sahrens mutex_enter(hash_lock); 645789Sahrens for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 646789Sahrens buf = buf->b_hash_next) { 647789Sahrens if (BUF_EQUAL(spa, dva, birth, buf)) { 648789Sahrens *lockp = hash_lock; 649789Sahrens return (buf); 650789Sahrens } 651789Sahrens } 652789Sahrens mutex_exit(hash_lock); 653789Sahrens *lockp = NULL; 654789Sahrens return (NULL); 655789Sahrens } 656789Sahrens 657789Sahrens /* 658789Sahrens * Insert an entry into the hash table. If there is already an element 659789Sahrens * equal to elem in the hash table, then the already existing element 660789Sahrens * will be returned and the new element will not be inserted. 661789Sahrens * Otherwise returns NULL. 662789Sahrens */ 663789Sahrens static arc_buf_hdr_t * 664789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 665789Sahrens { 666789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 667789Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 668789Sahrens arc_buf_hdr_t *fbuf; 6693403Sbmc uint32_t i; 670789Sahrens 6711544Seschrock ASSERT(!HDR_IN_HASH_TABLE(buf)); 672789Sahrens *lockp = hash_lock; 673789Sahrens mutex_enter(hash_lock); 674789Sahrens for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 675789Sahrens fbuf = fbuf->b_hash_next, i++) { 676789Sahrens if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 677789Sahrens return (fbuf); 678789Sahrens } 679789Sahrens 680789Sahrens buf->b_hash_next = buf_hash_table.ht_table[idx]; 681789Sahrens buf_hash_table.ht_table[idx] = buf; 6821544Seschrock buf->b_flags |= ARC_IN_HASH_TABLE; 683789Sahrens 684789Sahrens /* collect some hash table performance data */ 685789Sahrens if (i > 0) { 6863403Sbmc ARCSTAT_BUMP(arcstat_hash_collisions); 687789Sahrens if (i == 1) 6883403Sbmc ARCSTAT_BUMP(arcstat_hash_chains); 6893403Sbmc 6903403Sbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 691789Sahrens } 6923403Sbmc 6933403Sbmc ARCSTAT_BUMP(arcstat_hash_elements); 6943403Sbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 695789Sahrens 696789Sahrens return (NULL); 697789Sahrens } 698789Sahrens 699789Sahrens static void 700789Sahrens buf_hash_remove(arc_buf_hdr_t *buf) 701789Sahrens { 702789Sahrens arc_buf_hdr_t *fbuf, **bufp; 703789Sahrens uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 704789Sahrens 705789Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 7061544Seschrock ASSERT(HDR_IN_HASH_TABLE(buf)); 707789Sahrens 708789Sahrens bufp = &buf_hash_table.ht_table[idx]; 709789Sahrens while ((fbuf = *bufp) != buf) { 710789Sahrens ASSERT(fbuf != NULL); 711789Sahrens bufp = &fbuf->b_hash_next; 712789Sahrens } 713789Sahrens *bufp = buf->b_hash_next; 714789Sahrens buf->b_hash_next = NULL; 7151544Seschrock buf->b_flags &= ~ARC_IN_HASH_TABLE; 716789Sahrens 717789Sahrens /* collect some hash table performance data */ 7183403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 7193403Sbmc 720789Sahrens if (buf_hash_table.ht_table[idx] && 721789Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 7223403Sbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 723789Sahrens } 724789Sahrens 725789Sahrens /* 726789Sahrens * Global data structures and functions for the buf kmem cache. 727789Sahrens */ 728789Sahrens static kmem_cache_t *hdr_cache; 729789Sahrens static kmem_cache_t *buf_cache; 730789Sahrens 731789Sahrens static void 732789Sahrens buf_fini(void) 733789Sahrens { 734789Sahrens int i; 735789Sahrens 736789Sahrens kmem_free(buf_hash_table.ht_table, 737789Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 738789Sahrens for (i = 0; i < BUF_LOCKS; i++) 739789Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 740789Sahrens kmem_cache_destroy(hdr_cache); 741789Sahrens kmem_cache_destroy(buf_cache); 742789Sahrens } 743789Sahrens 744789Sahrens /* 745789Sahrens * Constructor callback - called when the cache is empty 746789Sahrens * and a new buf is requested. 747789Sahrens */ 748789Sahrens /* ARGSUSED */ 749789Sahrens static int 750789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag) 751789Sahrens { 752789Sahrens arc_buf_hdr_t *buf = vbuf; 753789Sahrens 754789Sahrens bzero(buf, sizeof (arc_buf_hdr_t)); 755789Sahrens refcount_create(&buf->b_refcnt); 756789Sahrens cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 7574831Sgw25295 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 7585450Sbrendan 7596018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 760789Sahrens return (0); 761789Sahrens } 762789Sahrens 7637545SMark.Maybee@Sun.COM /* ARGSUSED */ 7647545SMark.Maybee@Sun.COM static int 7657545SMark.Maybee@Sun.COM buf_cons(void *vbuf, void *unused, int kmflag) 7667545SMark.Maybee@Sun.COM { 7677545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 7687545SMark.Maybee@Sun.COM 7697545SMark.Maybee@Sun.COM bzero(buf, sizeof (arc_buf_t)); 7707545SMark.Maybee@Sun.COM rw_init(&buf->b_lock, NULL, RW_DEFAULT, NULL); 7717545SMark.Maybee@Sun.COM return (0); 7727545SMark.Maybee@Sun.COM } 7737545SMark.Maybee@Sun.COM 774789Sahrens /* 775789Sahrens * Destructor callback - called when a cached buf is 776789Sahrens * no longer required. 777789Sahrens */ 778789Sahrens /* ARGSUSED */ 779789Sahrens static void 780789Sahrens hdr_dest(void *vbuf, void *unused) 781789Sahrens { 782789Sahrens arc_buf_hdr_t *buf = vbuf; 783789Sahrens 784789Sahrens refcount_destroy(&buf->b_refcnt); 785789Sahrens cv_destroy(&buf->b_cv); 7864831Sgw25295 mutex_destroy(&buf->b_freeze_lock); 7875450Sbrendan 7886018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 789789Sahrens } 790789Sahrens 7917545SMark.Maybee@Sun.COM /* ARGSUSED */ 7927545SMark.Maybee@Sun.COM static void 7937545SMark.Maybee@Sun.COM buf_dest(void *vbuf, void *unused) 7947545SMark.Maybee@Sun.COM { 7957545SMark.Maybee@Sun.COM arc_buf_t *buf = vbuf; 7967545SMark.Maybee@Sun.COM 7977545SMark.Maybee@Sun.COM rw_destroy(&buf->b_lock); 7987545SMark.Maybee@Sun.COM } 7997545SMark.Maybee@Sun.COM 800789Sahrens /* 801789Sahrens * Reclaim callback -- invoked when memory is low. 802789Sahrens */ 803789Sahrens /* ARGSUSED */ 804789Sahrens static void 805789Sahrens hdr_recl(void *unused) 806789Sahrens { 807789Sahrens dprintf("hdr_recl called\n"); 8083158Smaybee /* 8093158Smaybee * umem calls the reclaim func when we destroy the buf cache, 8103158Smaybee * which is after we do arc_fini(). 8113158Smaybee */ 8123158Smaybee if (!arc_dead) 8133158Smaybee cv_signal(&arc_reclaim_thr_cv); 814789Sahrens } 815789Sahrens 816789Sahrens static void 817789Sahrens buf_init(void) 818789Sahrens { 819789Sahrens uint64_t *ct; 8201544Seschrock uint64_t hsize = 1ULL << 12; 821789Sahrens int i, j; 822789Sahrens 823789Sahrens /* 824789Sahrens * The hash table is big enough to fill all of physical memory 8251544Seschrock * with an average 64K block size. The table will take up 8261544Seschrock * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 827789Sahrens */ 8281544Seschrock while (hsize * 65536 < physmem * PAGESIZE) 829789Sahrens hsize <<= 1; 8301544Seschrock retry: 831789Sahrens buf_hash_table.ht_mask = hsize - 1; 8321544Seschrock buf_hash_table.ht_table = 8331544Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 8341544Seschrock if (buf_hash_table.ht_table == NULL) { 8351544Seschrock ASSERT(hsize > (1ULL << 8)); 8361544Seschrock hsize >>= 1; 8371544Seschrock goto retry; 8381544Seschrock } 839789Sahrens 840789Sahrens hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 841789Sahrens 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 842789Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 8437545SMark.Maybee@Sun.COM 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 844789Sahrens 845789Sahrens for (i = 0; i < 256; i++) 846789Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 847789Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 848789Sahrens 849789Sahrens for (i = 0; i < BUF_LOCKS; i++) { 850789Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 851789Sahrens NULL, MUTEX_DEFAULT, NULL); 852789Sahrens } 853789Sahrens } 854789Sahrens 855789Sahrens #define ARC_MINTIME (hz>>4) /* 62 ms */ 856789Sahrens 857789Sahrens static void 8583093Sahrens arc_cksum_verify(arc_buf_t *buf) 8593093Sahrens { 8603093Sahrens zio_cksum_t zc; 8613093Sahrens 8623312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 8633093Sahrens return; 8643093Sahrens 8653093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8663265Sahrens if (buf->b_hdr->b_freeze_cksum == NULL || 8673265Sahrens (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 8683093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8693093Sahrens return; 8703093Sahrens } 8713093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8723093Sahrens if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 8733093Sahrens panic("buffer modified while frozen!"); 8743093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 8753093Sahrens } 8763093Sahrens 8775450Sbrendan static int 8785450Sbrendan arc_cksum_equal(arc_buf_t *buf) 8795450Sbrendan { 8805450Sbrendan zio_cksum_t zc; 8815450Sbrendan int equal; 8825450Sbrendan 8835450Sbrendan mutex_enter(&buf->b_hdr->b_freeze_lock); 8845450Sbrendan fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 8855450Sbrendan equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 8865450Sbrendan mutex_exit(&buf->b_hdr->b_freeze_lock); 8875450Sbrendan 8885450Sbrendan return (equal); 8895450Sbrendan } 8905450Sbrendan 8913093Sahrens static void 8925450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force) 8933093Sahrens { 8945450Sbrendan if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 8953093Sahrens return; 8963093Sahrens 8973093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 8983093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 8993093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9003093Sahrens return; 9013093Sahrens } 9023093Sahrens buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 9033093Sahrens fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 9043093Sahrens buf->b_hdr->b_freeze_cksum); 9053093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9063093Sahrens } 9073093Sahrens 9083093Sahrens void 9093093Sahrens arc_buf_thaw(arc_buf_t *buf) 9103093Sahrens { 9115450Sbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 9125450Sbrendan if (buf->b_hdr->b_state != arc_anon) 9135450Sbrendan panic("modifying non-anon buffer!"); 9145450Sbrendan if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 9155450Sbrendan panic("modifying buffer while i/o in progress!"); 9165450Sbrendan arc_cksum_verify(buf); 9175450Sbrendan } 9185450Sbrendan 9193093Sahrens mutex_enter(&buf->b_hdr->b_freeze_lock); 9203093Sahrens if (buf->b_hdr->b_freeze_cksum != NULL) { 9213093Sahrens kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 9223093Sahrens buf->b_hdr->b_freeze_cksum = NULL; 9233093Sahrens } 9243093Sahrens mutex_exit(&buf->b_hdr->b_freeze_lock); 9253093Sahrens } 9263093Sahrens 9273093Sahrens void 9283093Sahrens arc_buf_freeze(arc_buf_t *buf) 9293093Sahrens { 9303312Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 9313312Sahrens return; 9323312Sahrens 9333093Sahrens ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 9343403Sbmc buf->b_hdr->b_state == arc_anon); 9355450Sbrendan arc_cksum_compute(buf, B_FALSE); 9363093Sahrens } 9373093Sahrens 9383093Sahrens static void 939789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 940789Sahrens { 941789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 942789Sahrens 943789Sahrens if ((refcount_add(&ab->b_refcnt, tag) == 1) && 9443403Sbmc (ab->b_state != arc_anon)) { 9453700Sek110237 uint64_t delta = ab->b_size * ab->b_datacnt; 9464309Smaybee list_t *list = &ab->b_state->arcs_list[ab->b_type]; 9474309Smaybee uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 948789Sahrens 9493403Sbmc ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx)); 9503403Sbmc mutex_enter(&ab->b_state->arcs_mtx); 951789Sahrens ASSERT(list_link_active(&ab->b_arc_node)); 9524309Smaybee list_remove(list, ab); 9531544Seschrock if (GHOST_STATE(ab->b_state)) { 9541544Seschrock ASSERT3U(ab->b_datacnt, ==, 0); 9551544Seschrock ASSERT3P(ab->b_buf, ==, NULL); 9561544Seschrock delta = ab->b_size; 9571544Seschrock } 9581544Seschrock ASSERT(delta > 0); 9594309Smaybee ASSERT3U(*size, >=, delta); 9604309Smaybee atomic_add_64(size, -delta); 9613403Sbmc mutex_exit(&ab->b_state->arcs_mtx); 9627046Sahrens /* remove the prefetch flag if we get a reference */ 9632391Smaybee if (ab->b_flags & ARC_PREFETCH) 9642391Smaybee ab->b_flags &= ~ARC_PREFETCH; 965789Sahrens } 966789Sahrens } 967789Sahrens 968789Sahrens static int 969789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 970789Sahrens { 971789Sahrens int cnt; 9723403Sbmc arc_state_t *state = ab->b_state; 973789Sahrens 9743403Sbmc ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 9753403Sbmc ASSERT(!GHOST_STATE(state)); 976789Sahrens 977789Sahrens if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 9783403Sbmc (state != arc_anon)) { 9794309Smaybee uint64_t *size = &state->arcs_lsize[ab->b_type]; 9804309Smaybee 9813403Sbmc ASSERT(!MUTEX_HELD(&state->arcs_mtx)); 9823403Sbmc mutex_enter(&state->arcs_mtx); 983789Sahrens ASSERT(!list_link_active(&ab->b_arc_node)); 9844309Smaybee list_insert_head(&state->arcs_list[ab->b_type], ab); 9851544Seschrock ASSERT(ab->b_datacnt > 0); 9864309Smaybee atomic_add_64(size, ab->b_size * ab->b_datacnt); 9873403Sbmc mutex_exit(&state->arcs_mtx); 988789Sahrens } 989789Sahrens return (cnt); 990789Sahrens } 991789Sahrens 992789Sahrens /* 993789Sahrens * Move the supplied buffer to the indicated state. The mutex 994789Sahrens * for the buffer must be held by the caller. 995789Sahrens */ 996789Sahrens static void 9971544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 998789Sahrens { 9991544Seschrock arc_state_t *old_state = ab->b_state; 10003700Sek110237 int64_t refcnt = refcount_count(&ab->b_refcnt); 10013700Sek110237 uint64_t from_delta, to_delta; 1002789Sahrens 1003789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 10041544Seschrock ASSERT(new_state != old_state); 10051544Seschrock ASSERT(refcnt == 0 || ab->b_datacnt > 0); 10061544Seschrock ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 10071544Seschrock 10081544Seschrock from_delta = to_delta = ab->b_datacnt * ab->b_size; 1009789Sahrens 1010789Sahrens /* 1011789Sahrens * If this buffer is evictable, transfer it from the 1012789Sahrens * old state list to the new state list. 1013789Sahrens */ 10141544Seschrock if (refcnt == 0) { 10153403Sbmc if (old_state != arc_anon) { 10163403Sbmc int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx); 10174309Smaybee uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 10181544Seschrock 10191544Seschrock if (use_mutex) 10203403Sbmc mutex_enter(&old_state->arcs_mtx); 10211544Seschrock 10221544Seschrock ASSERT(list_link_active(&ab->b_arc_node)); 10234309Smaybee list_remove(&old_state->arcs_list[ab->b_type], ab); 1024789Sahrens 10252391Smaybee /* 10262391Smaybee * If prefetching out of the ghost cache, 10272391Smaybee * we will have a non-null datacnt. 10282391Smaybee */ 10292391Smaybee if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 10302391Smaybee /* ghost elements have a ghost size */ 10311544Seschrock ASSERT(ab->b_buf == NULL); 10321544Seschrock from_delta = ab->b_size; 1033789Sahrens } 10344309Smaybee ASSERT3U(*size, >=, from_delta); 10354309Smaybee atomic_add_64(size, -from_delta); 10361544Seschrock 10371544Seschrock if (use_mutex) 10383403Sbmc mutex_exit(&old_state->arcs_mtx); 1039789Sahrens } 10403403Sbmc if (new_state != arc_anon) { 10413403Sbmc int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx); 10424309Smaybee uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1043789Sahrens 10441544Seschrock if (use_mutex) 10453403Sbmc mutex_enter(&new_state->arcs_mtx); 10461544Seschrock 10474309Smaybee list_insert_head(&new_state->arcs_list[ab->b_type], ab); 10481544Seschrock 10491544Seschrock /* ghost elements have a ghost size */ 10501544Seschrock if (GHOST_STATE(new_state)) { 10511544Seschrock ASSERT(ab->b_datacnt == 0); 10521544Seschrock ASSERT(ab->b_buf == NULL); 10531544Seschrock to_delta = ab->b_size; 10541544Seschrock } 10554309Smaybee atomic_add_64(size, to_delta); 10561544Seschrock 10571544Seschrock if (use_mutex) 10583403Sbmc mutex_exit(&new_state->arcs_mtx); 1059789Sahrens } 1060789Sahrens } 1061789Sahrens 1062789Sahrens ASSERT(!BUF_EMPTY(ab)); 10635450Sbrendan if (new_state == arc_anon) { 1064789Sahrens buf_hash_remove(ab); 1065789Sahrens } 1066789Sahrens 10671544Seschrock /* adjust state sizes */ 10681544Seschrock if (to_delta) 10693403Sbmc atomic_add_64(&new_state->arcs_size, to_delta); 10701544Seschrock if (from_delta) { 10713403Sbmc ASSERT3U(old_state->arcs_size, >=, from_delta); 10723403Sbmc atomic_add_64(&old_state->arcs_size, -from_delta); 1073789Sahrens } 1074789Sahrens ab->b_state = new_state; 10755450Sbrendan 10765450Sbrendan /* adjust l2arc hdr stats */ 10775450Sbrendan if (new_state == arc_l2c_only) 10785450Sbrendan l2arc_hdr_stat_add(); 10795450Sbrendan else if (old_state == arc_l2c_only) 10805450Sbrendan l2arc_hdr_stat_remove(); 1081789Sahrens } 1082789Sahrens 10834309Smaybee void 10844309Smaybee arc_space_consume(uint64_t space) 10854309Smaybee { 10864309Smaybee atomic_add_64(&arc_meta_used, space); 10874309Smaybee atomic_add_64(&arc_size, space); 10884309Smaybee } 10894309Smaybee 10904309Smaybee void 10914309Smaybee arc_space_return(uint64_t space) 10924309Smaybee { 10934309Smaybee ASSERT(arc_meta_used >= space); 10944309Smaybee if (arc_meta_max < arc_meta_used) 10954309Smaybee arc_meta_max = arc_meta_used; 10964309Smaybee atomic_add_64(&arc_meta_used, -space); 10974309Smaybee ASSERT(arc_size >= space); 10984309Smaybee atomic_add_64(&arc_size, -space); 10994309Smaybee } 11004309Smaybee 11014309Smaybee void * 11024309Smaybee arc_data_buf_alloc(uint64_t size) 11034309Smaybee { 11044309Smaybee if (arc_evict_needed(ARC_BUFC_DATA)) 11054309Smaybee cv_signal(&arc_reclaim_thr_cv); 11064309Smaybee atomic_add_64(&arc_size, size); 11074309Smaybee return (zio_data_buf_alloc(size)); 11084309Smaybee } 11094309Smaybee 11104309Smaybee void 11114309Smaybee arc_data_buf_free(void *buf, uint64_t size) 11124309Smaybee { 11134309Smaybee zio_data_buf_free(buf, size); 11144309Smaybee ASSERT(arc_size >= size); 11154309Smaybee atomic_add_64(&arc_size, -size); 11164309Smaybee } 11174309Smaybee 1118789Sahrens arc_buf_t * 11193290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1120789Sahrens { 1121789Sahrens arc_buf_hdr_t *hdr; 1122789Sahrens arc_buf_t *buf; 1123789Sahrens 1124789Sahrens ASSERT3U(size, >, 0); 11256245Smaybee hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1126789Sahrens ASSERT(BUF_EMPTY(hdr)); 1127789Sahrens hdr->b_size = size; 11283290Sjohansen hdr->b_type = type; 1129789Sahrens hdr->b_spa = spa; 11303403Sbmc hdr->b_state = arc_anon; 1131789Sahrens hdr->b_arc_access = 0; 11326245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1133789Sahrens buf->b_hdr = hdr; 11342688Smaybee buf->b_data = NULL; 11351544Seschrock buf->b_efunc = NULL; 11361544Seschrock buf->b_private = NULL; 1137789Sahrens buf->b_next = NULL; 1138789Sahrens hdr->b_buf = buf; 11392688Smaybee arc_get_data_buf(buf); 11401544Seschrock hdr->b_datacnt = 1; 1141789Sahrens hdr->b_flags = 0; 1142789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1143789Sahrens (void) refcount_add(&hdr->b_refcnt, tag); 1144789Sahrens 1145789Sahrens return (buf); 1146789Sahrens } 1147789Sahrens 11482688Smaybee static arc_buf_t * 11492688Smaybee arc_buf_clone(arc_buf_t *from) 11501544Seschrock { 11512688Smaybee arc_buf_t *buf; 11522688Smaybee arc_buf_hdr_t *hdr = from->b_hdr; 11532688Smaybee uint64_t size = hdr->b_size; 11541544Seschrock 11556245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 11562688Smaybee buf->b_hdr = hdr; 11572688Smaybee buf->b_data = NULL; 11582688Smaybee buf->b_efunc = NULL; 11592688Smaybee buf->b_private = NULL; 11602688Smaybee buf->b_next = hdr->b_buf; 11612688Smaybee hdr->b_buf = buf; 11622688Smaybee arc_get_data_buf(buf); 11632688Smaybee bcopy(from->b_data, buf->b_data, size); 11642688Smaybee hdr->b_datacnt += 1; 11652688Smaybee return (buf); 11661544Seschrock } 11671544Seschrock 11681544Seschrock void 11691544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag) 11701544Seschrock { 11712887Smaybee arc_buf_hdr_t *hdr; 11721544Seschrock kmutex_t *hash_lock; 11731544Seschrock 11742724Smaybee /* 11757545SMark.Maybee@Sun.COM * Check to see if this buffer is evicted. Callers 11767545SMark.Maybee@Sun.COM * must verify b_data != NULL to know if the add_ref 11777545SMark.Maybee@Sun.COM * was successful. 11782724Smaybee */ 11797545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 11807545SMark.Maybee@Sun.COM if (buf->b_data == NULL) { 11817545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 11822724Smaybee return; 11832887Smaybee } 11847545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 11857545SMark.Maybee@Sun.COM ASSERT(hdr != NULL); 11862887Smaybee hash_lock = HDR_LOCK(hdr); 11872724Smaybee mutex_enter(hash_lock); 11887545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 11897545SMark.Maybee@Sun.COM 11903403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 11911544Seschrock add_reference(hdr, hash_lock, tag); 11922688Smaybee arc_access(hdr, hash_lock); 11932688Smaybee mutex_exit(hash_lock); 11943403Sbmc ARCSTAT_BUMP(arcstat_hits); 11953403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 11963403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 11973403Sbmc data, metadata, hits); 11981544Seschrock } 11991544Seschrock 12005450Sbrendan /* 12015450Sbrendan * Free the arc data buffer. If it is an l2arc write in progress, 12025450Sbrendan * the buffer is placed on l2arc_free_on_write to be freed later. 12035450Sbrendan */ 12045450Sbrendan static void 12055450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t), 12065450Sbrendan void *data, size_t size) 12075450Sbrendan { 12085450Sbrendan if (HDR_L2_WRITING(hdr)) { 12095450Sbrendan l2arc_data_free_t *df; 12105450Sbrendan df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 12115450Sbrendan df->l2df_data = data; 12125450Sbrendan df->l2df_size = size; 12135450Sbrendan df->l2df_func = free_func; 12145450Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 12155450Sbrendan list_insert_head(l2arc_free_on_write, df); 12165450Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 12175450Sbrendan ARCSTAT_BUMP(arcstat_l2_free_on_write); 12185450Sbrendan } else { 12195450Sbrendan free_func(data, size); 12205450Sbrendan } 12215450Sbrendan } 12225450Sbrendan 1223789Sahrens static void 12242688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 12251544Seschrock { 12261544Seschrock arc_buf_t **bufp; 12271544Seschrock 12281544Seschrock /* free up data associated with the buf */ 12291544Seschrock if (buf->b_data) { 12301544Seschrock arc_state_t *state = buf->b_hdr->b_state; 12311544Seschrock uint64_t size = buf->b_hdr->b_size; 12323290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 12331544Seschrock 12343093Sahrens arc_cksum_verify(buf); 12352688Smaybee if (!recycle) { 12363290Sjohansen if (type == ARC_BUFC_METADATA) { 12375450Sbrendan arc_buf_data_free(buf->b_hdr, zio_buf_free, 12385450Sbrendan buf->b_data, size); 12394309Smaybee arc_space_return(size); 12403290Sjohansen } else { 12413290Sjohansen ASSERT(type == ARC_BUFC_DATA); 12425450Sbrendan arc_buf_data_free(buf->b_hdr, 12435450Sbrendan zio_data_buf_free, buf->b_data, size); 12444309Smaybee atomic_add_64(&arc_size, -size); 12453290Sjohansen } 12462688Smaybee } 12471544Seschrock if (list_link_active(&buf->b_hdr->b_arc_node)) { 12484309Smaybee uint64_t *cnt = &state->arcs_lsize[type]; 12494309Smaybee 12501544Seschrock ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 12513403Sbmc ASSERT(state != arc_anon); 12524309Smaybee 12534309Smaybee ASSERT3U(*cnt, >=, size); 12544309Smaybee atomic_add_64(cnt, -size); 12551544Seschrock } 12563403Sbmc ASSERT3U(state->arcs_size, >=, size); 12573403Sbmc atomic_add_64(&state->arcs_size, -size); 12581544Seschrock buf->b_data = NULL; 12591544Seschrock ASSERT(buf->b_hdr->b_datacnt > 0); 12601544Seschrock buf->b_hdr->b_datacnt -= 1; 12611544Seschrock } 12621544Seschrock 12631544Seschrock /* only remove the buf if requested */ 12641544Seschrock if (!all) 12651544Seschrock return; 12661544Seschrock 12671544Seschrock /* remove the buf from the hdr list */ 12681544Seschrock for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 12691544Seschrock continue; 12701544Seschrock *bufp = buf->b_next; 12711544Seschrock 12721544Seschrock ASSERT(buf->b_efunc == NULL); 12731544Seschrock 12741544Seschrock /* clean up the buf */ 12751544Seschrock buf->b_hdr = NULL; 12761544Seschrock kmem_cache_free(buf_cache, buf); 12771544Seschrock } 12781544Seschrock 12791544Seschrock static void 12801544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 1281789Sahrens { 1282789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt)); 12833403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 12841544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 12857046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 1286789Sahrens 12875450Sbrendan if (hdr->b_l2hdr != NULL) { 12885450Sbrendan if (!MUTEX_HELD(&l2arc_buflist_mtx)) { 12895450Sbrendan /* 12905450Sbrendan * To prevent arc_free() and l2arc_evict() from 12915450Sbrendan * attempting to free the same buffer at the same time, 12925450Sbrendan * a FREE_IN_PROGRESS flag is given to arc_free() to 12935450Sbrendan * give it priority. l2arc_evict() can't destroy this 12945450Sbrendan * header while we are waiting on l2arc_buflist_mtx. 12957361SBrendan.Gregg@Sun.COM * 12967361SBrendan.Gregg@Sun.COM * The hdr may be removed from l2ad_buflist before we 12977361SBrendan.Gregg@Sun.COM * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 12985450Sbrendan */ 12995450Sbrendan mutex_enter(&l2arc_buflist_mtx); 13007361SBrendan.Gregg@Sun.COM if (hdr->b_l2hdr != NULL) { 13017361SBrendan.Gregg@Sun.COM list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, 13027361SBrendan.Gregg@Sun.COM hdr); 13037361SBrendan.Gregg@Sun.COM } 13045450Sbrendan mutex_exit(&l2arc_buflist_mtx); 13055450Sbrendan } else { 13065450Sbrendan list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr); 13075450Sbrendan } 13085450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 13095450Sbrendan kmem_free(hdr->b_l2hdr, sizeof (l2arc_buf_hdr_t)); 13105450Sbrendan if (hdr->b_state == arc_l2c_only) 13115450Sbrendan l2arc_hdr_stat_remove(); 13125450Sbrendan hdr->b_l2hdr = NULL; 13135450Sbrendan } 13145450Sbrendan 1315789Sahrens if (!BUF_EMPTY(hdr)) { 13161544Seschrock ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1317789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 1318789Sahrens hdr->b_birth = 0; 1319789Sahrens hdr->b_cksum0 = 0; 1320789Sahrens } 13211544Seschrock while (hdr->b_buf) { 1322789Sahrens arc_buf_t *buf = hdr->b_buf; 1323789Sahrens 13241544Seschrock if (buf->b_efunc) { 13251544Seschrock mutex_enter(&arc_eviction_mtx); 13267545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 13271544Seschrock ASSERT(buf->b_hdr != NULL); 13282688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 13291544Seschrock hdr->b_buf = buf->b_next; 13302887Smaybee buf->b_hdr = &arc_eviction_hdr; 13311544Seschrock buf->b_next = arc_eviction_list; 13321544Seschrock arc_eviction_list = buf; 13337545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 13341544Seschrock mutex_exit(&arc_eviction_mtx); 13351544Seschrock } else { 13362688Smaybee arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 13371544Seschrock } 1338789Sahrens } 13393093Sahrens if (hdr->b_freeze_cksum != NULL) { 13403093Sahrens kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 13413093Sahrens hdr->b_freeze_cksum = NULL; 13423093Sahrens } 13431544Seschrock 1344789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 1345789Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 1346789Sahrens ASSERT3P(hdr->b_acb, ==, NULL); 1347789Sahrens kmem_cache_free(hdr_cache, hdr); 1348789Sahrens } 1349789Sahrens 1350789Sahrens void 1351789Sahrens arc_buf_free(arc_buf_t *buf, void *tag) 1352789Sahrens { 1353789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 13543403Sbmc int hashed = hdr->b_state != arc_anon; 13551544Seschrock 13561544Seschrock ASSERT(buf->b_efunc == NULL); 13571544Seschrock ASSERT(buf->b_data != NULL); 13581544Seschrock 13591544Seschrock if (hashed) { 13601544Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 13611544Seschrock 13621544Seschrock mutex_enter(hash_lock); 13631544Seschrock (void) remove_reference(hdr, hash_lock, tag); 13641544Seschrock if (hdr->b_datacnt > 1) 13652688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 13661544Seschrock else 13671544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 13681544Seschrock mutex_exit(hash_lock); 13691544Seschrock } else if (HDR_IO_IN_PROGRESS(hdr)) { 13701544Seschrock int destroy_hdr; 13711544Seschrock /* 13721544Seschrock * We are in the middle of an async write. Don't destroy 13731544Seschrock * this buffer unless the write completes before we finish 13741544Seschrock * decrementing the reference count. 13751544Seschrock */ 13761544Seschrock mutex_enter(&arc_eviction_mtx); 13771544Seschrock (void) remove_reference(hdr, NULL, tag); 13781544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 13791544Seschrock destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 13801544Seschrock mutex_exit(&arc_eviction_mtx); 13811544Seschrock if (destroy_hdr) 13821544Seschrock arc_hdr_destroy(hdr); 13831544Seschrock } else { 13841544Seschrock if (remove_reference(hdr, NULL, tag) > 0) { 13851544Seschrock ASSERT(HDR_IO_ERROR(hdr)); 13862688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 13871544Seschrock } else { 13881544Seschrock arc_hdr_destroy(hdr); 13891544Seschrock } 13901544Seschrock } 13911544Seschrock } 13921544Seschrock 13931544Seschrock int 13941544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag) 13951544Seschrock { 13961544Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 1397789Sahrens kmutex_t *hash_lock = HDR_LOCK(hdr); 13981544Seschrock int no_callback = (buf->b_efunc == NULL); 13991544Seschrock 14003403Sbmc if (hdr->b_state == arc_anon) { 14011544Seschrock arc_buf_free(buf, tag); 14021544Seschrock return (no_callback); 14031544Seschrock } 1404789Sahrens 1405789Sahrens mutex_enter(hash_lock); 14063403Sbmc ASSERT(hdr->b_state != arc_anon); 14071544Seschrock ASSERT(buf->b_data != NULL); 1408789Sahrens 14091544Seschrock (void) remove_reference(hdr, hash_lock, tag); 14101544Seschrock if (hdr->b_datacnt > 1) { 14111544Seschrock if (no_callback) 14122688Smaybee arc_buf_destroy(buf, FALSE, TRUE); 14131544Seschrock } else if (no_callback) { 14141544Seschrock ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 14151544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 1416789Sahrens } 14171544Seschrock ASSERT(no_callback || hdr->b_datacnt > 1 || 14181544Seschrock refcount_is_zero(&hdr->b_refcnt)); 1419789Sahrens mutex_exit(hash_lock); 14201544Seschrock return (no_callback); 1421789Sahrens } 1422789Sahrens 1423789Sahrens int 1424789Sahrens arc_buf_size(arc_buf_t *buf) 1425789Sahrens { 1426789Sahrens return (buf->b_hdr->b_size); 1427789Sahrens } 1428789Sahrens 1429789Sahrens /* 1430789Sahrens * Evict buffers from list until we've removed the specified number of 1431789Sahrens * bytes. Move the removed buffers to the appropriate evict state. 14322688Smaybee * If the recycle flag is set, then attempt to "recycle" a buffer: 14332688Smaybee * - look for a buffer to evict that is `bytes' long. 14342688Smaybee * - return the data block from this buffer rather than freeing it. 14352688Smaybee * This flag is used by callers that are trying to make space for a 14362688Smaybee * new buffer in a full arc cache. 14375642Smaybee * 14385642Smaybee * This function makes a "best effort". It skips over any buffers 14395642Smaybee * it can't get a hash_lock on, and so may not catch all candidates. 14405642Smaybee * It may also return without evicting as much space as requested. 1441789Sahrens */ 14422688Smaybee static void * 14435642Smaybee arc_evict(arc_state_t *state, spa_t *spa, int64_t bytes, boolean_t recycle, 14443290Sjohansen arc_buf_contents_t type) 1445789Sahrens { 1446789Sahrens arc_state_t *evicted_state; 14472688Smaybee uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 14482918Smaybee arc_buf_hdr_t *ab, *ab_prev = NULL; 14494309Smaybee list_t *list = &state->arcs_list[type]; 1450789Sahrens kmutex_t *hash_lock; 14512688Smaybee boolean_t have_lock; 14522918Smaybee void *stolen = NULL; 1453789Sahrens 14543403Sbmc ASSERT(state == arc_mru || state == arc_mfu); 1455789Sahrens 14563403Sbmc evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1457789Sahrens 14583403Sbmc mutex_enter(&state->arcs_mtx); 14593403Sbmc mutex_enter(&evicted_state->arcs_mtx); 1460789Sahrens 14614309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 14624309Smaybee ab_prev = list_prev(list, ab); 14632391Smaybee /* prefetch buffers have a minimum lifespan */ 14642688Smaybee if (HDR_IO_IN_PROGRESS(ab) || 14655642Smaybee (spa && ab->b_spa != spa) || 14662688Smaybee (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 14672688Smaybee lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) { 14682391Smaybee skipped++; 14692391Smaybee continue; 14702391Smaybee } 14712918Smaybee /* "lookahead" for better eviction candidate */ 14722918Smaybee if (recycle && ab->b_size != bytes && 14732918Smaybee ab_prev && ab_prev->b_size == bytes) 14742688Smaybee continue; 1475789Sahrens hash_lock = HDR_LOCK(ab); 14762688Smaybee have_lock = MUTEX_HELD(hash_lock); 14772688Smaybee if (have_lock || mutex_tryenter(hash_lock)) { 1478789Sahrens ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 14791544Seschrock ASSERT(ab->b_datacnt > 0); 14801544Seschrock while (ab->b_buf) { 14811544Seschrock arc_buf_t *buf = ab->b_buf; 14827545SMark.Maybee@Sun.COM if (!rw_tryenter(&buf->b_lock, RW_WRITER)) { 14837545SMark.Maybee@Sun.COM missed += 1; 14847545SMark.Maybee@Sun.COM break; 14857545SMark.Maybee@Sun.COM } 14862688Smaybee if (buf->b_data) { 14871544Seschrock bytes_evicted += ab->b_size; 14883290Sjohansen if (recycle && ab->b_type == type && 14895450Sbrendan ab->b_size == bytes && 14905450Sbrendan !HDR_L2_WRITING(ab)) { 14912918Smaybee stolen = buf->b_data; 14922918Smaybee recycle = FALSE; 14932918Smaybee } 14942688Smaybee } 14951544Seschrock if (buf->b_efunc) { 14961544Seschrock mutex_enter(&arc_eviction_mtx); 14972918Smaybee arc_buf_destroy(buf, 14982918Smaybee buf->b_data == stolen, FALSE); 14991544Seschrock ab->b_buf = buf->b_next; 15002887Smaybee buf->b_hdr = &arc_eviction_hdr; 15011544Seschrock buf->b_next = arc_eviction_list; 15021544Seschrock arc_eviction_list = buf; 15031544Seschrock mutex_exit(&arc_eviction_mtx); 15047545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 15051544Seschrock } else { 15067545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 15072918Smaybee arc_buf_destroy(buf, 15082918Smaybee buf->b_data == stolen, TRUE); 15091544Seschrock } 15101544Seschrock } 15117545SMark.Maybee@Sun.COM if (ab->b_datacnt == 0) { 15127545SMark.Maybee@Sun.COM arc_change_state(evicted_state, ab, hash_lock); 15137545SMark.Maybee@Sun.COM ASSERT(HDR_IN_HASH_TABLE(ab)); 15147545SMark.Maybee@Sun.COM ab->b_flags |= ARC_IN_HASH_TABLE; 15157545SMark.Maybee@Sun.COM ab->b_flags &= ~ARC_BUF_AVAILABLE; 15167545SMark.Maybee@Sun.COM DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 15177545SMark.Maybee@Sun.COM } 15182688Smaybee if (!have_lock) 15192688Smaybee mutex_exit(hash_lock); 15201544Seschrock if (bytes >= 0 && bytes_evicted >= bytes) 1521789Sahrens break; 1522789Sahrens } else { 15232688Smaybee missed += 1; 1524789Sahrens } 1525789Sahrens } 15263403Sbmc 15273403Sbmc mutex_exit(&evicted_state->arcs_mtx); 15283403Sbmc mutex_exit(&state->arcs_mtx); 1529789Sahrens 1530789Sahrens if (bytes_evicted < bytes) 1531789Sahrens dprintf("only evicted %lld bytes from %x", 1532789Sahrens (longlong_t)bytes_evicted, state); 1533789Sahrens 15342688Smaybee if (skipped) 15353403Sbmc ARCSTAT_INCR(arcstat_evict_skip, skipped); 15363403Sbmc 15372688Smaybee if (missed) 15383403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, missed); 15393403Sbmc 15404709Smaybee /* 15414709Smaybee * We have just evicted some date into the ghost state, make 15424709Smaybee * sure we also adjust the ghost state size if necessary. 15434709Smaybee */ 15444709Smaybee if (arc_no_grow && 15454709Smaybee arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 15464709Smaybee int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 15474709Smaybee arc_mru_ghost->arcs_size - arc_c; 15484709Smaybee 15494709Smaybee if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 15504709Smaybee int64_t todelete = 15514709Smaybee MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 15525642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 15534709Smaybee } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 15544709Smaybee int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 15554709Smaybee arc_mru_ghost->arcs_size + 15564709Smaybee arc_mfu_ghost->arcs_size - arc_c); 15575642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 15584709Smaybee } 15594709Smaybee } 15604709Smaybee 15612918Smaybee return (stolen); 1562789Sahrens } 1563789Sahrens 1564789Sahrens /* 1565789Sahrens * Remove buffers from list until we've removed the specified number of 1566789Sahrens * bytes. Destroy the buffers that are removed. 1567789Sahrens */ 1568789Sahrens static void 15695642Smaybee arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes) 1570789Sahrens { 1571789Sahrens arc_buf_hdr_t *ab, *ab_prev; 15724309Smaybee list_t *list = &state->arcs_list[ARC_BUFC_DATA]; 1573789Sahrens kmutex_t *hash_lock; 15741544Seschrock uint64_t bytes_deleted = 0; 15753700Sek110237 uint64_t bufs_skipped = 0; 1576789Sahrens 15771544Seschrock ASSERT(GHOST_STATE(state)); 1578789Sahrens top: 15793403Sbmc mutex_enter(&state->arcs_mtx); 15804309Smaybee for (ab = list_tail(list); ab; ab = ab_prev) { 15814309Smaybee ab_prev = list_prev(list, ab); 15825642Smaybee if (spa && ab->b_spa != spa) 15835642Smaybee continue; 1584789Sahrens hash_lock = HDR_LOCK(ab); 1585789Sahrens if (mutex_tryenter(hash_lock)) { 15862391Smaybee ASSERT(!HDR_IO_IN_PROGRESS(ab)); 15871544Seschrock ASSERT(ab->b_buf == NULL); 15883403Sbmc ARCSTAT_BUMP(arcstat_deleted); 15891544Seschrock bytes_deleted += ab->b_size; 15905450Sbrendan 15915450Sbrendan if (ab->b_l2hdr != NULL) { 15925450Sbrendan /* 15935450Sbrendan * This buffer is cached on the 2nd Level ARC; 15945450Sbrendan * don't destroy the header. 15955450Sbrendan */ 15965450Sbrendan arc_change_state(arc_l2c_only, ab, hash_lock); 15975450Sbrendan mutex_exit(hash_lock); 15985450Sbrendan } else { 15995450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 16005450Sbrendan mutex_exit(hash_lock); 16015450Sbrendan arc_hdr_destroy(ab); 16025450Sbrendan } 16035450Sbrendan 1604789Sahrens DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1605789Sahrens if (bytes >= 0 && bytes_deleted >= bytes) 1606789Sahrens break; 1607789Sahrens } else { 1608789Sahrens if (bytes < 0) { 16093403Sbmc mutex_exit(&state->arcs_mtx); 1610789Sahrens mutex_enter(hash_lock); 1611789Sahrens mutex_exit(hash_lock); 1612789Sahrens goto top; 1613789Sahrens } 1614789Sahrens bufs_skipped += 1; 1615789Sahrens } 1616789Sahrens } 16173403Sbmc mutex_exit(&state->arcs_mtx); 1618789Sahrens 16194309Smaybee if (list == &state->arcs_list[ARC_BUFC_DATA] && 16204309Smaybee (bytes < 0 || bytes_deleted < bytes)) { 16214309Smaybee list = &state->arcs_list[ARC_BUFC_METADATA]; 16224309Smaybee goto top; 16234309Smaybee } 16244309Smaybee 1625789Sahrens if (bufs_skipped) { 16263403Sbmc ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 1627789Sahrens ASSERT(bytes >= 0); 1628789Sahrens } 1629789Sahrens 1630789Sahrens if (bytes_deleted < bytes) 1631789Sahrens dprintf("only deleted %lld bytes from %p", 1632789Sahrens (longlong_t)bytes_deleted, state); 1633789Sahrens } 1634789Sahrens 1635789Sahrens static void 1636789Sahrens arc_adjust(void) 1637789Sahrens { 16383403Sbmc int64_t top_sz, mru_over, arc_over, todelete; 1639789Sahrens 16405642Smaybee top_sz = arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used; 1641789Sahrens 16424309Smaybee if (top_sz > arc_p && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 16434309Smaybee int64_t toevict = 16444309Smaybee MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], top_sz - arc_p); 16455642Smaybee (void) arc_evict(arc_mru, NULL, toevict, FALSE, ARC_BUFC_DATA); 16464309Smaybee top_sz = arc_anon->arcs_size + arc_mru->arcs_size; 16474309Smaybee } 16484309Smaybee 16494309Smaybee if (top_sz > arc_p && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 16504309Smaybee int64_t toevict = 16514309Smaybee MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], top_sz - arc_p); 16525642Smaybee (void) arc_evict(arc_mru, NULL, toevict, FALSE, 16535642Smaybee ARC_BUFC_METADATA); 16543403Sbmc top_sz = arc_anon->arcs_size + arc_mru->arcs_size; 1655789Sahrens } 1656789Sahrens 16573403Sbmc mru_over = top_sz + arc_mru_ghost->arcs_size - arc_c; 1658789Sahrens 1659789Sahrens if (mru_over > 0) { 16604309Smaybee if (arc_mru_ghost->arcs_size > 0) { 16614309Smaybee todelete = MIN(arc_mru_ghost->arcs_size, mru_over); 16625642Smaybee arc_evict_ghost(arc_mru_ghost, NULL, todelete); 1663789Sahrens } 1664789Sahrens } 1665789Sahrens 16663403Sbmc if ((arc_over = arc_size - arc_c) > 0) { 16671544Seschrock int64_t tbl_over; 1668789Sahrens 16694309Smaybee if (arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 16704309Smaybee int64_t toevict = 16714309Smaybee MIN(arc_mfu->arcs_lsize[ARC_BUFC_DATA], arc_over); 16725642Smaybee (void) arc_evict(arc_mfu, NULL, toevict, FALSE, 16734309Smaybee ARC_BUFC_DATA); 16744309Smaybee arc_over = arc_size - arc_c; 1675789Sahrens } 1676789Sahrens 16774309Smaybee if (arc_over > 0 && 16784309Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 16794309Smaybee int64_t toevict = 16804309Smaybee MIN(arc_mfu->arcs_lsize[ARC_BUFC_METADATA], 16814309Smaybee arc_over); 16825642Smaybee (void) arc_evict(arc_mfu, NULL, toevict, FALSE, 16834309Smaybee ARC_BUFC_METADATA); 16844309Smaybee } 16854309Smaybee 16864309Smaybee tbl_over = arc_size + arc_mru_ghost->arcs_size + 16874309Smaybee arc_mfu_ghost->arcs_size - arc_c * 2; 16884309Smaybee 16894309Smaybee if (tbl_over > 0 && arc_mfu_ghost->arcs_size > 0) { 16904309Smaybee todelete = MIN(arc_mfu_ghost->arcs_size, tbl_over); 16915642Smaybee arc_evict_ghost(arc_mfu_ghost, NULL, todelete); 1692789Sahrens } 1693789Sahrens } 1694789Sahrens } 1695789Sahrens 16961544Seschrock static void 16971544Seschrock arc_do_user_evicts(void) 16981544Seschrock { 16991544Seschrock mutex_enter(&arc_eviction_mtx); 17001544Seschrock while (arc_eviction_list != NULL) { 17011544Seschrock arc_buf_t *buf = arc_eviction_list; 17021544Seschrock arc_eviction_list = buf->b_next; 17037545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 17041544Seschrock buf->b_hdr = NULL; 17057545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 17061544Seschrock mutex_exit(&arc_eviction_mtx); 17071544Seschrock 17081819Smaybee if (buf->b_efunc != NULL) 17091819Smaybee VERIFY(buf->b_efunc(buf) == 0); 17101544Seschrock 17111544Seschrock buf->b_efunc = NULL; 17121544Seschrock buf->b_private = NULL; 17131544Seschrock kmem_cache_free(buf_cache, buf); 17141544Seschrock mutex_enter(&arc_eviction_mtx); 17151544Seschrock } 17161544Seschrock mutex_exit(&arc_eviction_mtx); 17171544Seschrock } 17181544Seschrock 1719789Sahrens /* 17205642Smaybee * Flush all *evictable* data from the cache for the given spa. 1721789Sahrens * NOTE: this will not touch "active" (i.e. referenced) data. 1722789Sahrens */ 1723789Sahrens void 17245642Smaybee arc_flush(spa_t *spa) 1725789Sahrens { 17265642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) { 17275642Smaybee (void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_DATA); 17285642Smaybee if (spa) 17295642Smaybee break; 17305642Smaybee } 17315642Smaybee while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) { 17325642Smaybee (void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_METADATA); 17335642Smaybee if (spa) 17345642Smaybee break; 17355642Smaybee } 17365642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) { 17375642Smaybee (void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_DATA); 17385642Smaybee if (spa) 17395642Smaybee break; 17405642Smaybee } 17415642Smaybee while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) { 17425642Smaybee (void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_METADATA); 17435642Smaybee if (spa) 17445642Smaybee break; 17455642Smaybee } 17465642Smaybee 17475642Smaybee arc_evict_ghost(arc_mru_ghost, spa, -1); 17485642Smaybee arc_evict_ghost(arc_mfu_ghost, spa, -1); 17491544Seschrock 17501544Seschrock mutex_enter(&arc_reclaim_thr_lock); 17511544Seschrock arc_do_user_evicts(); 17521544Seschrock mutex_exit(&arc_reclaim_thr_lock); 17535642Smaybee ASSERT(spa || arc_eviction_list == NULL); 1754789Sahrens } 1755789Sahrens 17563158Smaybee int arc_shrink_shift = 5; /* log2(fraction of arc to reclaim) */ 17572391Smaybee 1758789Sahrens void 17593158Smaybee arc_shrink(void) 1760789Sahrens { 17613403Sbmc if (arc_c > arc_c_min) { 17623158Smaybee uint64_t to_free; 1763789Sahrens 17642048Sstans #ifdef _KERNEL 17653403Sbmc to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree)); 17662048Sstans #else 17673403Sbmc to_free = arc_c >> arc_shrink_shift; 17682048Sstans #endif 17693403Sbmc if (arc_c > arc_c_min + to_free) 17703403Sbmc atomic_add_64(&arc_c, -to_free); 17713158Smaybee else 17723403Sbmc arc_c = arc_c_min; 17732048Sstans 17743403Sbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 17753403Sbmc if (arc_c > arc_size) 17763403Sbmc arc_c = MAX(arc_size, arc_c_min); 17773403Sbmc if (arc_p > arc_c) 17783403Sbmc arc_p = (arc_c >> 1); 17793403Sbmc ASSERT(arc_c >= arc_c_min); 17803403Sbmc ASSERT((int64_t)arc_p >= 0); 17813158Smaybee } 1782789Sahrens 17833403Sbmc if (arc_size > arc_c) 17843158Smaybee arc_adjust(); 1785789Sahrens } 1786789Sahrens 1787789Sahrens static int 1788789Sahrens arc_reclaim_needed(void) 1789789Sahrens { 1790789Sahrens uint64_t extra; 1791789Sahrens 1792789Sahrens #ifdef _KERNEL 17932048Sstans 17942048Sstans if (needfree) 17952048Sstans return (1); 17962048Sstans 1797789Sahrens /* 1798789Sahrens * take 'desfree' extra pages, so we reclaim sooner, rather than later 1799789Sahrens */ 1800789Sahrens extra = desfree; 1801789Sahrens 1802789Sahrens /* 1803789Sahrens * check that we're out of range of the pageout scanner. It starts to 1804789Sahrens * schedule paging if freemem is less than lotsfree and needfree. 1805789Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 1806789Sahrens * number of needed free pages. We add extra pages here to make sure 1807789Sahrens * the scanner doesn't start up while we're freeing memory. 1808789Sahrens */ 1809789Sahrens if (freemem < lotsfree + needfree + extra) 1810789Sahrens return (1); 1811789Sahrens 1812789Sahrens /* 1813789Sahrens * check to make sure that swapfs has enough space so that anon 18145450Sbrendan * reservations can still succeed. anon_resvmem() checks that the 1815789Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 1816789Sahrens * swap pages. We also add a bit of extra here just to prevent 1817789Sahrens * circumstances from getting really dire. 1818789Sahrens */ 1819789Sahrens if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1820789Sahrens return (1); 1821789Sahrens 18221936Smaybee #if defined(__i386) 1823789Sahrens /* 1824789Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 1825789Sahrens * kernel heap space before we ever run out of available physical 1826789Sahrens * memory. Most checks of the size of the heap_area compare against 1827789Sahrens * tune.t_minarmem, which is the minimum available real memory that we 1828789Sahrens * can have in the system. However, this is generally fixed at 25 pages 1829789Sahrens * which is so low that it's useless. In this comparison, we seek to 1830789Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 18315450Sbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 1832789Sahrens * free) 1833789Sahrens */ 1834789Sahrens if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1835789Sahrens (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1836789Sahrens return (1); 1837789Sahrens #endif 1838789Sahrens 1839789Sahrens #else 1840789Sahrens if (spa_get_random(100) == 0) 1841789Sahrens return (1); 1842789Sahrens #endif 1843789Sahrens return (0); 1844789Sahrens } 1845789Sahrens 1846789Sahrens static void 1847789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1848789Sahrens { 1849789Sahrens size_t i; 1850789Sahrens kmem_cache_t *prev_cache = NULL; 18513290Sjohansen kmem_cache_t *prev_data_cache = NULL; 1852789Sahrens extern kmem_cache_t *zio_buf_cache[]; 18533290Sjohansen extern kmem_cache_t *zio_data_buf_cache[]; 1854789Sahrens 18551484Sek110237 #ifdef _KERNEL 18564309Smaybee if (arc_meta_used >= arc_meta_limit) { 18574309Smaybee /* 18584309Smaybee * We are exceeding our meta-data cache limit. 18594309Smaybee * Purge some DNLC entries to release holds on meta-data. 18604309Smaybee */ 18614309Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 18624309Smaybee } 18631936Smaybee #if defined(__i386) 18641936Smaybee /* 18651936Smaybee * Reclaim unused memory from all kmem caches. 18661936Smaybee */ 18671936Smaybee kmem_reap(); 18681936Smaybee #endif 18691484Sek110237 #endif 18701484Sek110237 1871789Sahrens /* 18725450Sbrendan * An aggressive reclamation will shrink the cache size as well as 18731544Seschrock * reap free buffers from the arc kmem caches. 1874789Sahrens */ 1875789Sahrens if (strat == ARC_RECLAIM_AGGR) 18763158Smaybee arc_shrink(); 1877789Sahrens 1878789Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 1879789Sahrens if (zio_buf_cache[i] != prev_cache) { 1880789Sahrens prev_cache = zio_buf_cache[i]; 1881789Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 1882789Sahrens } 18833290Sjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 18843290Sjohansen prev_data_cache = zio_data_buf_cache[i]; 18853290Sjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 18863290Sjohansen } 1887789Sahrens } 18881544Seschrock kmem_cache_reap_now(buf_cache); 18891544Seschrock kmem_cache_reap_now(hdr_cache); 1890789Sahrens } 1891789Sahrens 1892789Sahrens static void 1893789Sahrens arc_reclaim_thread(void) 1894789Sahrens { 1895789Sahrens clock_t growtime = 0; 1896789Sahrens arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 1897789Sahrens callb_cpr_t cpr; 1898789Sahrens 1899789Sahrens CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 1900789Sahrens 1901789Sahrens mutex_enter(&arc_reclaim_thr_lock); 1902789Sahrens while (arc_thread_exit == 0) { 1903789Sahrens if (arc_reclaim_needed()) { 1904789Sahrens 19053403Sbmc if (arc_no_grow) { 1906789Sahrens if (last_reclaim == ARC_RECLAIM_CONS) { 1907789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1908789Sahrens } else { 1909789Sahrens last_reclaim = ARC_RECLAIM_CONS; 1910789Sahrens } 1911789Sahrens } else { 19123403Sbmc arc_no_grow = TRUE; 1913789Sahrens last_reclaim = ARC_RECLAIM_AGGR; 1914789Sahrens membar_producer(); 1915789Sahrens } 1916789Sahrens 1917789Sahrens /* reset the growth delay for every reclaim */ 1918789Sahrens growtime = lbolt + (arc_grow_retry * hz); 1919789Sahrens 1920789Sahrens arc_kmem_reap_now(last_reclaim); 19216987Sbrendan arc_warm = B_TRUE; 1922789Sahrens 19234309Smaybee } else if (arc_no_grow && lbolt >= growtime) { 19243403Sbmc arc_no_grow = FALSE; 1925789Sahrens } 1926789Sahrens 19273403Sbmc if (2 * arc_c < arc_size + 19283403Sbmc arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size) 19293298Smaybee arc_adjust(); 19303298Smaybee 19311544Seschrock if (arc_eviction_list != NULL) 19321544Seschrock arc_do_user_evicts(); 19331544Seschrock 1934789Sahrens /* block until needed, or one second, whichever is shorter */ 1935789Sahrens CALLB_CPR_SAFE_BEGIN(&cpr); 1936789Sahrens (void) cv_timedwait(&arc_reclaim_thr_cv, 1937789Sahrens &arc_reclaim_thr_lock, (lbolt + hz)); 1938789Sahrens CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 1939789Sahrens } 1940789Sahrens 1941789Sahrens arc_thread_exit = 0; 1942789Sahrens cv_broadcast(&arc_reclaim_thr_cv); 1943789Sahrens CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 1944789Sahrens thread_exit(); 1945789Sahrens } 1946789Sahrens 19471544Seschrock /* 19481544Seschrock * Adapt arc info given the number of bytes we are trying to add and 19491544Seschrock * the state that we are comming from. This function is only called 19501544Seschrock * when we are adding new content to the cache. 19511544Seschrock */ 1952789Sahrens static void 19531544Seschrock arc_adapt(int bytes, arc_state_t *state) 1954789Sahrens { 19551544Seschrock int mult; 19561544Seschrock 19575450Sbrendan if (state == arc_l2c_only) 19585450Sbrendan return; 19595450Sbrendan 19601544Seschrock ASSERT(bytes > 0); 1961789Sahrens /* 19621544Seschrock * Adapt the target size of the MRU list: 19631544Seschrock * - if we just hit in the MRU ghost list, then increase 19641544Seschrock * the target size of the MRU list. 19651544Seschrock * - if we just hit in the MFU ghost list, then increase 19661544Seschrock * the target size of the MFU list by decreasing the 19671544Seschrock * target size of the MRU list. 1968789Sahrens */ 19693403Sbmc if (state == arc_mru_ghost) { 19703403Sbmc mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 19713403Sbmc 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 19721544Seschrock 19733403Sbmc arc_p = MIN(arc_c, arc_p + bytes * mult); 19743403Sbmc } else if (state == arc_mfu_ghost) { 19753403Sbmc mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 19763403Sbmc 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 19771544Seschrock 19783403Sbmc arc_p = MAX(0, (int64_t)arc_p - bytes * mult); 19791544Seschrock } 19803403Sbmc ASSERT((int64_t)arc_p >= 0); 1981789Sahrens 1982789Sahrens if (arc_reclaim_needed()) { 1983789Sahrens cv_signal(&arc_reclaim_thr_cv); 1984789Sahrens return; 1985789Sahrens } 1986789Sahrens 19873403Sbmc if (arc_no_grow) 1988789Sahrens return; 1989789Sahrens 19903403Sbmc if (arc_c >= arc_c_max) 19911544Seschrock return; 19921544Seschrock 1993789Sahrens /* 19941544Seschrock * If we're within (2 * maxblocksize) bytes of the target 19951544Seschrock * cache size, increment the target cache size 1996789Sahrens */ 19973403Sbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 19983403Sbmc atomic_add_64(&arc_c, (int64_t)bytes); 19993403Sbmc if (arc_c > arc_c_max) 20003403Sbmc arc_c = arc_c_max; 20013403Sbmc else if (state == arc_anon) 20023403Sbmc atomic_add_64(&arc_p, (int64_t)bytes); 20033403Sbmc if (arc_p > arc_c) 20043403Sbmc arc_p = arc_c; 2005789Sahrens } 20063403Sbmc ASSERT((int64_t)arc_p >= 0); 2007789Sahrens } 2008789Sahrens 2009789Sahrens /* 20101544Seschrock * Check if the cache has reached its limits and eviction is required 20111544Seschrock * prior to insert. 2012789Sahrens */ 2013789Sahrens static int 20144309Smaybee arc_evict_needed(arc_buf_contents_t type) 2015789Sahrens { 20164309Smaybee if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 20174309Smaybee return (1); 20184309Smaybee 20194309Smaybee #ifdef _KERNEL 20204309Smaybee /* 20214309Smaybee * If zio data pages are being allocated out of a separate heap segment, 20224309Smaybee * then enforce that the size of available vmem for this area remains 20234309Smaybee * above about 1/32nd free. 20244309Smaybee */ 20254309Smaybee if (type == ARC_BUFC_DATA && zio_arena != NULL && 20264309Smaybee vmem_size(zio_arena, VMEM_FREE) < 20274309Smaybee (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 20284309Smaybee return (1); 20294309Smaybee #endif 20304309Smaybee 2031789Sahrens if (arc_reclaim_needed()) 2032789Sahrens return (1); 2033789Sahrens 20343403Sbmc return (arc_size > arc_c); 2035789Sahrens } 2036789Sahrens 2037789Sahrens /* 20382688Smaybee * The buffer, supplied as the first argument, needs a data block. 20392688Smaybee * So, if we are at cache max, determine which cache should be victimized. 20402688Smaybee * We have the following cases: 2041789Sahrens * 20423403Sbmc * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2043789Sahrens * In this situation if we're out of space, but the resident size of the MFU is 2044789Sahrens * under the limit, victimize the MFU cache to satisfy this insertion request. 2045789Sahrens * 20463403Sbmc * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2047789Sahrens * Here, we've used up all of the available space for the MRU, so we need to 2048789Sahrens * evict from our own cache instead. Evict from the set of resident MRU 2049789Sahrens * entries. 2050789Sahrens * 20513403Sbmc * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2052789Sahrens * c minus p represents the MFU space in the cache, since p is the size of the 2053789Sahrens * cache that is dedicated to the MRU. In this situation there's still space on 2054789Sahrens * the MFU side, so the MRU side needs to be victimized. 2055789Sahrens * 20563403Sbmc * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2057789Sahrens * MFU's resident set is consuming more space than it has been allotted. In 2058789Sahrens * this situation, we must victimize our own cache, the MFU, for this insertion. 2059789Sahrens */ 2060789Sahrens static void 20612688Smaybee arc_get_data_buf(arc_buf_t *buf) 2062789Sahrens { 20633290Sjohansen arc_state_t *state = buf->b_hdr->b_state; 20643290Sjohansen uint64_t size = buf->b_hdr->b_size; 20653290Sjohansen arc_buf_contents_t type = buf->b_hdr->b_type; 20662688Smaybee 20672688Smaybee arc_adapt(size, state); 2068789Sahrens 20692688Smaybee /* 20702688Smaybee * We have not yet reached cache maximum size, 20712688Smaybee * just allocate a new buffer. 20722688Smaybee */ 20734309Smaybee if (!arc_evict_needed(type)) { 20743290Sjohansen if (type == ARC_BUFC_METADATA) { 20753290Sjohansen buf->b_data = zio_buf_alloc(size); 20764309Smaybee arc_space_consume(size); 20773290Sjohansen } else { 20783290Sjohansen ASSERT(type == ARC_BUFC_DATA); 20793290Sjohansen buf->b_data = zio_data_buf_alloc(size); 20804309Smaybee atomic_add_64(&arc_size, size); 20813290Sjohansen } 20822688Smaybee goto out; 20832688Smaybee } 20842688Smaybee 20852688Smaybee /* 20862688Smaybee * If we are prefetching from the mfu ghost list, this buffer 20872688Smaybee * will end up on the mru list; so steal space from there. 20882688Smaybee */ 20893403Sbmc if (state == arc_mfu_ghost) 20903403Sbmc state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 20913403Sbmc else if (state == arc_mru_ghost) 20923403Sbmc state = arc_mru; 2093789Sahrens 20943403Sbmc if (state == arc_mru || state == arc_anon) { 20953403Sbmc uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 20964309Smaybee state = (arc_mfu->arcs_lsize[type] > 0 && 20974309Smaybee arc_p > mru_used) ? arc_mfu : arc_mru; 2098789Sahrens } else { 20992688Smaybee /* MFU cases */ 21003403Sbmc uint64_t mfu_space = arc_c - arc_p; 21014309Smaybee state = (arc_mru->arcs_lsize[type] > 0 && 21024309Smaybee mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 21032688Smaybee } 21045642Smaybee if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) { 21053290Sjohansen if (type == ARC_BUFC_METADATA) { 21063290Sjohansen buf->b_data = zio_buf_alloc(size); 21074309Smaybee arc_space_consume(size); 21083290Sjohansen } else { 21093290Sjohansen ASSERT(type == ARC_BUFC_DATA); 21103290Sjohansen buf->b_data = zio_data_buf_alloc(size); 21114309Smaybee atomic_add_64(&arc_size, size); 21123290Sjohansen } 21133403Sbmc ARCSTAT_BUMP(arcstat_recycle_miss); 21142688Smaybee } 21152688Smaybee ASSERT(buf->b_data != NULL); 21162688Smaybee out: 21172688Smaybee /* 21182688Smaybee * Update the state size. Note that ghost states have a 21192688Smaybee * "ghost size" and so don't need to be updated. 21202688Smaybee */ 21212688Smaybee if (!GHOST_STATE(buf->b_hdr->b_state)) { 21222688Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 21232688Smaybee 21243403Sbmc atomic_add_64(&hdr->b_state->arcs_size, size); 21252688Smaybee if (list_link_active(&hdr->b_arc_node)) { 21262688Smaybee ASSERT(refcount_is_zero(&hdr->b_refcnt)); 21274309Smaybee atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2128789Sahrens } 21293298Smaybee /* 21303298Smaybee * If we are growing the cache, and we are adding anonymous 21313403Sbmc * data, and we have outgrown arc_p, update arc_p 21323298Smaybee */ 21333403Sbmc if (arc_size < arc_c && hdr->b_state == arc_anon && 21343403Sbmc arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 21353403Sbmc arc_p = MIN(arc_c, arc_p + size); 2136789Sahrens } 2137789Sahrens } 2138789Sahrens 2139789Sahrens /* 2140789Sahrens * This routine is called whenever a buffer is accessed. 21411544Seschrock * NOTE: the hash lock is dropped in this function. 2142789Sahrens */ 2143789Sahrens static void 21442688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2145789Sahrens { 2146789Sahrens ASSERT(MUTEX_HELD(hash_lock)); 2147789Sahrens 21483403Sbmc if (buf->b_state == arc_anon) { 2149789Sahrens /* 2150789Sahrens * This buffer is not in the cache, and does not 2151789Sahrens * appear in our "ghost" list. Add the new buffer 2152789Sahrens * to the MRU state. 2153789Sahrens */ 2154789Sahrens 2155789Sahrens ASSERT(buf->b_arc_access == 0); 2156789Sahrens buf->b_arc_access = lbolt; 21571544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 21583403Sbmc arc_change_state(arc_mru, buf, hash_lock); 2159789Sahrens 21603403Sbmc } else if (buf->b_state == arc_mru) { 2161789Sahrens /* 21622391Smaybee * If this buffer is here because of a prefetch, then either: 21632391Smaybee * - clear the flag if this is a "referencing" read 21642391Smaybee * (any subsequent access will bump this into the MFU state). 21652391Smaybee * or 21662391Smaybee * - move the buffer to the head of the list if this is 21672391Smaybee * another prefetch (to make it less likely to be evicted). 2168789Sahrens */ 2169789Sahrens if ((buf->b_flags & ARC_PREFETCH) != 0) { 21702391Smaybee if (refcount_count(&buf->b_refcnt) == 0) { 21712391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 21722391Smaybee } else { 21732391Smaybee buf->b_flags &= ~ARC_PREFETCH; 21743403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 21752391Smaybee } 21762391Smaybee buf->b_arc_access = lbolt; 2177789Sahrens return; 2178789Sahrens } 2179789Sahrens 2180789Sahrens /* 2181789Sahrens * This buffer has been "accessed" only once so far, 2182789Sahrens * but it is still in the cache. Move it to the MFU 2183789Sahrens * state. 2184789Sahrens */ 2185789Sahrens if (lbolt > buf->b_arc_access + ARC_MINTIME) { 2186789Sahrens /* 2187789Sahrens * More than 125ms have passed since we 2188789Sahrens * instantiated this buffer. Move it to the 2189789Sahrens * most frequently used state. 2190789Sahrens */ 2191789Sahrens buf->b_arc_access = lbolt; 21921544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 21933403Sbmc arc_change_state(arc_mfu, buf, hash_lock); 2194789Sahrens } 21953403Sbmc ARCSTAT_BUMP(arcstat_mru_hits); 21963403Sbmc } else if (buf->b_state == arc_mru_ghost) { 2197789Sahrens arc_state_t *new_state; 2198789Sahrens /* 2199789Sahrens * This buffer has been "accessed" recently, but 2200789Sahrens * was evicted from the cache. Move it to the 2201789Sahrens * MFU state. 2202789Sahrens */ 2203789Sahrens 2204789Sahrens if (buf->b_flags & ARC_PREFETCH) { 22053403Sbmc new_state = arc_mru; 22062391Smaybee if (refcount_count(&buf->b_refcnt) > 0) 22072391Smaybee buf->b_flags &= ~ARC_PREFETCH; 22081544Seschrock DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2209789Sahrens } else { 22103403Sbmc new_state = arc_mfu; 22111544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2212789Sahrens } 2213789Sahrens 2214789Sahrens buf->b_arc_access = lbolt; 2215789Sahrens arc_change_state(new_state, buf, hash_lock); 2216789Sahrens 22173403Sbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 22183403Sbmc } else if (buf->b_state == arc_mfu) { 2219789Sahrens /* 2220789Sahrens * This buffer has been accessed more than once and is 2221789Sahrens * still in the cache. Keep it in the MFU state. 2222789Sahrens * 22232391Smaybee * NOTE: an add_reference() that occurred when we did 22242391Smaybee * the arc_read() will have kicked this off the list. 22252391Smaybee * If it was a prefetch, we will explicitly move it to 22262391Smaybee * the head of the list now. 2227789Sahrens */ 22282391Smaybee if ((buf->b_flags & ARC_PREFETCH) != 0) { 22292391Smaybee ASSERT(refcount_count(&buf->b_refcnt) == 0); 22302391Smaybee ASSERT(list_link_active(&buf->b_arc_node)); 22312391Smaybee } 22323403Sbmc ARCSTAT_BUMP(arcstat_mfu_hits); 22332391Smaybee buf->b_arc_access = lbolt; 22343403Sbmc } else if (buf->b_state == arc_mfu_ghost) { 22353403Sbmc arc_state_t *new_state = arc_mfu; 2236789Sahrens /* 2237789Sahrens * This buffer has been accessed more than once but has 2238789Sahrens * been evicted from the cache. Move it back to the 2239789Sahrens * MFU state. 2240789Sahrens */ 2241789Sahrens 22422391Smaybee if (buf->b_flags & ARC_PREFETCH) { 22432391Smaybee /* 22442391Smaybee * This is a prefetch access... 22452391Smaybee * move this block back to the MRU state. 22462391Smaybee */ 22472391Smaybee ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 22483403Sbmc new_state = arc_mru; 22492391Smaybee } 22502391Smaybee 2251789Sahrens buf->b_arc_access = lbolt; 22521544Seschrock DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 22532391Smaybee arc_change_state(new_state, buf, hash_lock); 2254789Sahrens 22553403Sbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 22565450Sbrendan } else if (buf->b_state == arc_l2c_only) { 22575450Sbrendan /* 22585450Sbrendan * This buffer is on the 2nd Level ARC. 22595450Sbrendan */ 22605450Sbrendan 22615450Sbrendan buf->b_arc_access = lbolt; 22625450Sbrendan DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 22635450Sbrendan arc_change_state(arc_mfu, buf, hash_lock); 2264789Sahrens } else { 2265789Sahrens ASSERT(!"invalid arc state"); 2266789Sahrens } 2267789Sahrens } 2268789Sahrens 2269789Sahrens /* a generic arc_done_func_t which you can use */ 2270789Sahrens /* ARGSUSED */ 2271789Sahrens void 2272789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2273789Sahrens { 2274789Sahrens bcopy(buf->b_data, arg, buf->b_hdr->b_size); 22751544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2276789Sahrens } 2277789Sahrens 22784309Smaybee /* a generic arc_done_func_t */ 2279789Sahrens void 2280789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2281789Sahrens { 2282789Sahrens arc_buf_t **bufp = arg; 2283789Sahrens if (zio && zio->io_error) { 22841544Seschrock VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2285789Sahrens *bufp = NULL; 2286789Sahrens } else { 2287789Sahrens *bufp = buf; 2288789Sahrens } 2289789Sahrens } 2290789Sahrens 2291789Sahrens static void 2292789Sahrens arc_read_done(zio_t *zio) 2293789Sahrens { 22941589Smaybee arc_buf_hdr_t *hdr, *found; 2295789Sahrens arc_buf_t *buf; 2296789Sahrens arc_buf_t *abuf; /* buffer we're assigning to callback */ 2297789Sahrens kmutex_t *hash_lock; 2298789Sahrens arc_callback_t *callback_list, *acb; 2299789Sahrens int freeable = FALSE; 2300789Sahrens 2301789Sahrens buf = zio->io_private; 2302789Sahrens hdr = buf->b_hdr; 2303789Sahrens 23041589Smaybee /* 23051589Smaybee * The hdr was inserted into hash-table and removed from lists 23061589Smaybee * prior to starting I/O. We should find this header, since 23071589Smaybee * it's in the hash table, and it should be legit since it's 23081589Smaybee * not possible to evict it during the I/O. The only possible 23091589Smaybee * reason for it not to be found is if we were freed during the 23101589Smaybee * read. 23111589Smaybee */ 23121589Smaybee found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth, 23133093Sahrens &hash_lock); 2314789Sahrens 23151589Smaybee ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 23165450Sbrendan (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 23175450Sbrendan (found == hdr && HDR_L2_READING(hdr))); 23185450Sbrendan 23196987Sbrendan hdr->b_flags &= ~ARC_L2_EVICTED; 23205450Sbrendan if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 23217237Sek110237 hdr->b_flags &= ~ARC_L2CACHE; 2322789Sahrens 2323789Sahrens /* byteswap if necessary */ 2324789Sahrens callback_list = hdr->b_acb; 2325789Sahrens ASSERT(callback_list != NULL); 23267046Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 23277046Sahrens arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 23287046Sahrens byteswap_uint64_array : 23297046Sahrens dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap; 23307046Sahrens func(buf->b_data, hdr->b_size); 23317046Sahrens } 2332789Sahrens 23335450Sbrendan arc_cksum_compute(buf, B_FALSE); 23343093Sahrens 2335789Sahrens /* create copies of the data buffer for the callers */ 2336789Sahrens abuf = buf; 2337789Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 2338789Sahrens if (acb->acb_done) { 23392688Smaybee if (abuf == NULL) 23402688Smaybee abuf = arc_buf_clone(buf); 2341789Sahrens acb->acb_buf = abuf; 2342789Sahrens abuf = NULL; 2343789Sahrens } 2344789Sahrens } 2345789Sahrens hdr->b_acb = NULL; 2346789Sahrens hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 23471544Seschrock ASSERT(!HDR_BUF_AVAILABLE(hdr)); 23481544Seschrock if (abuf == buf) 23491544Seschrock hdr->b_flags |= ARC_BUF_AVAILABLE; 2350789Sahrens 2351789Sahrens ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2352789Sahrens 2353789Sahrens if (zio->io_error != 0) { 2354789Sahrens hdr->b_flags |= ARC_IO_ERROR; 23553403Sbmc if (hdr->b_state != arc_anon) 23563403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 23571544Seschrock if (HDR_IN_HASH_TABLE(hdr)) 23581544Seschrock buf_hash_remove(hdr); 2359789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2360789Sahrens } 2361789Sahrens 23621544Seschrock /* 23632391Smaybee * Broadcast before we drop the hash_lock to avoid the possibility 23642391Smaybee * that the hdr (and hence the cv) might be freed before we get to 23652391Smaybee * the cv_broadcast(). 23661544Seschrock */ 23671544Seschrock cv_broadcast(&hdr->b_cv); 23681544Seschrock 23691589Smaybee if (hash_lock) { 2370789Sahrens /* 2371789Sahrens * Only call arc_access on anonymous buffers. This is because 2372789Sahrens * if we've issued an I/O for an evicted buffer, we've already 2373789Sahrens * called arc_access (to prevent any simultaneous readers from 2374789Sahrens * getting confused). 2375789Sahrens */ 23763403Sbmc if (zio->io_error == 0 && hdr->b_state == arc_anon) 23772688Smaybee arc_access(hdr, hash_lock); 23782688Smaybee mutex_exit(hash_lock); 2379789Sahrens } else { 2380789Sahrens /* 2381789Sahrens * This block was freed while we waited for the read to 2382789Sahrens * complete. It has been removed from the hash table and 2383789Sahrens * moved to the anonymous state (so that it won't show up 2384789Sahrens * in the cache). 2385789Sahrens */ 23863403Sbmc ASSERT3P(hdr->b_state, ==, arc_anon); 2387789Sahrens freeable = refcount_is_zero(&hdr->b_refcnt); 2388789Sahrens } 2389789Sahrens 2390789Sahrens /* execute each callback and free its structure */ 2391789Sahrens while ((acb = callback_list) != NULL) { 2392789Sahrens if (acb->acb_done) 2393789Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2394789Sahrens 2395789Sahrens if (acb->acb_zio_dummy != NULL) { 2396789Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 2397789Sahrens zio_nowait(acb->acb_zio_dummy); 2398789Sahrens } 2399789Sahrens 2400789Sahrens callback_list = acb->acb_next; 2401789Sahrens kmem_free(acb, sizeof (arc_callback_t)); 2402789Sahrens } 2403789Sahrens 2404789Sahrens if (freeable) 24051544Seschrock arc_hdr_destroy(hdr); 2406789Sahrens } 2407789Sahrens 2408789Sahrens /* 2409789Sahrens * "Read" the block block at the specified DVA (in bp) via the 2410789Sahrens * cache. If the block is found in the cache, invoke the provided 2411789Sahrens * callback immediately and return. Note that the `zio' parameter 2412789Sahrens * in the callback will be NULL in this case, since no IO was 2413789Sahrens * required. If the block is not in the cache pass the read request 2414789Sahrens * on to the spa with a substitute callback function, so that the 2415789Sahrens * requested block will be added to the cache. 2416789Sahrens * 2417789Sahrens * If a read request arrives for a block that has a read in-progress, 2418789Sahrens * either wait for the in-progress read to complete (and return the 2419789Sahrens * results); or, if this is a read with a "done" func, add a record 2420789Sahrens * to the read to invoke the "done" func when the read completes, 2421789Sahrens * and return; or just return. 2422789Sahrens * 2423789Sahrens * arc_read_done() will invoke all the requested "done" functions 2424789Sahrens * for readers of this block. 24257046Sahrens * 24267046Sahrens * Normal callers should use arc_read and pass the arc buffer and offset 24277046Sahrens * for the bp. But if you know you don't need locking, you can use 24288213SSuhasini.Peddada@Sun.COM * arc_read_bp. 2429789Sahrens */ 2430789Sahrens int 24317046Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_buf_t *pbuf, 24327237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 24337046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 24347046Sahrens { 24357046Sahrens int err; 24367265Sahrens arc_buf_hdr_t *hdr = pbuf->b_hdr; 24377046Sahrens 24387046Sahrens ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 24397046Sahrens ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 24407545SMark.Maybee@Sun.COM rw_enter(&pbuf->b_lock, RW_READER); 24417046Sahrens 24427046Sahrens err = arc_read_nolock(pio, spa, bp, done, private, priority, 24437237Sek110237 zio_flags, arc_flags, zb); 24447046Sahrens 24457265Sahrens ASSERT3P(hdr, ==, pbuf->b_hdr); 24467545SMark.Maybee@Sun.COM rw_exit(&pbuf->b_lock); 24477046Sahrens return (err); 24487046Sahrens } 24497046Sahrens 24507046Sahrens int 24517046Sahrens arc_read_nolock(zio_t *pio, spa_t *spa, blkptr_t *bp, 24527237Sek110237 arc_done_func_t *done, void *private, int priority, int zio_flags, 24537046Sahrens uint32_t *arc_flags, const zbookmark_t *zb) 2454789Sahrens { 2455789Sahrens arc_buf_hdr_t *hdr; 2456789Sahrens arc_buf_t *buf; 2457789Sahrens kmutex_t *hash_lock; 24585450Sbrendan zio_t *rzio; 2459789Sahrens 2460789Sahrens top: 2461789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 24621544Seschrock if (hdr && hdr->b_datacnt > 0) { 2463789Sahrens 24642391Smaybee *arc_flags |= ARC_CACHED; 24652391Smaybee 2466789Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 24672391Smaybee 24682391Smaybee if (*arc_flags & ARC_WAIT) { 24692391Smaybee cv_wait(&hdr->b_cv, hash_lock); 24702391Smaybee mutex_exit(hash_lock); 24712391Smaybee goto top; 24722391Smaybee } 24732391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 24742391Smaybee 24752391Smaybee if (done) { 2476789Sahrens arc_callback_t *acb = NULL; 2477789Sahrens 2478789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 2479789Sahrens KM_SLEEP); 2480789Sahrens acb->acb_done = done; 2481789Sahrens acb->acb_private = private; 2482789Sahrens if (pio != NULL) 2483789Sahrens acb->acb_zio_dummy = zio_null(pio, 24847237Sek110237 spa, NULL, NULL, zio_flags); 2485789Sahrens 2486789Sahrens ASSERT(acb->acb_done != NULL); 2487789Sahrens acb->acb_next = hdr->b_acb; 2488789Sahrens hdr->b_acb = acb; 2489789Sahrens add_reference(hdr, hash_lock, private); 2490789Sahrens mutex_exit(hash_lock); 2491789Sahrens return (0); 2492789Sahrens } 2493789Sahrens mutex_exit(hash_lock); 2494789Sahrens return (0); 2495789Sahrens } 2496789Sahrens 24973403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 2498789Sahrens 24991544Seschrock if (done) { 25002688Smaybee add_reference(hdr, hash_lock, private); 25011544Seschrock /* 25021544Seschrock * If this block is already in use, create a new 25031544Seschrock * copy of the data so that we will be guaranteed 25041544Seschrock * that arc_release() will always succeed. 25051544Seschrock */ 25061544Seschrock buf = hdr->b_buf; 25071544Seschrock ASSERT(buf); 25081544Seschrock ASSERT(buf->b_data); 25092688Smaybee if (HDR_BUF_AVAILABLE(hdr)) { 25101544Seschrock ASSERT(buf->b_efunc == NULL); 25111544Seschrock hdr->b_flags &= ~ARC_BUF_AVAILABLE; 25122688Smaybee } else { 25132688Smaybee buf = arc_buf_clone(buf); 25141544Seschrock } 25152391Smaybee } else if (*arc_flags & ARC_PREFETCH && 25162391Smaybee refcount_count(&hdr->b_refcnt) == 0) { 25172391Smaybee hdr->b_flags |= ARC_PREFETCH; 2518789Sahrens } 2519789Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 25202688Smaybee arc_access(hdr, hash_lock); 25217237Sek110237 if (*arc_flags & ARC_L2CACHE) 25227237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25232688Smaybee mutex_exit(hash_lock); 25243403Sbmc ARCSTAT_BUMP(arcstat_hits); 25253403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 25263403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 25273403Sbmc data, metadata, hits); 25283403Sbmc 2529789Sahrens if (done) 2530789Sahrens done(NULL, buf, private); 2531789Sahrens } else { 2532789Sahrens uint64_t size = BP_GET_LSIZE(bp); 2533789Sahrens arc_callback_t *acb; 25346987Sbrendan vdev_t *vd = NULL; 25356987Sbrendan daddr_t addr; 2536789Sahrens 2537789Sahrens if (hdr == NULL) { 2538789Sahrens /* this block is not in the cache */ 2539789Sahrens arc_buf_hdr_t *exists; 25403290Sjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 25413290Sjohansen buf = arc_buf_alloc(spa, size, private, type); 2542789Sahrens hdr = buf->b_hdr; 2543789Sahrens hdr->b_dva = *BP_IDENTITY(bp); 2544789Sahrens hdr->b_birth = bp->blk_birth; 2545789Sahrens hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 2546789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 2547789Sahrens if (exists) { 2548789Sahrens /* somebody beat us to the hash insert */ 2549789Sahrens mutex_exit(hash_lock); 2550789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2551789Sahrens hdr->b_birth = 0; 2552789Sahrens hdr->b_cksum0 = 0; 25531544Seschrock (void) arc_buf_remove_ref(buf, private); 2554789Sahrens goto top; /* restart the IO request */ 2555789Sahrens } 25562391Smaybee /* if this is a prefetch, we don't have a reference */ 25572391Smaybee if (*arc_flags & ARC_PREFETCH) { 25582391Smaybee (void) remove_reference(hdr, hash_lock, 25592391Smaybee private); 25602391Smaybee hdr->b_flags |= ARC_PREFETCH; 25612391Smaybee } 25627237Sek110237 if (*arc_flags & ARC_L2CACHE) 25637237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25642391Smaybee if (BP_GET_LEVEL(bp) > 0) 25652391Smaybee hdr->b_flags |= ARC_INDIRECT; 2566789Sahrens } else { 2567789Sahrens /* this block is in the ghost cache */ 25681544Seschrock ASSERT(GHOST_STATE(hdr->b_state)); 25691544Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 25702391Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 25712391Smaybee ASSERT(hdr->b_buf == NULL); 2572789Sahrens 25732391Smaybee /* if this is a prefetch, we don't have a reference */ 25742391Smaybee if (*arc_flags & ARC_PREFETCH) 25752391Smaybee hdr->b_flags |= ARC_PREFETCH; 25762391Smaybee else 25772391Smaybee add_reference(hdr, hash_lock, private); 25787237Sek110237 if (*arc_flags & ARC_L2CACHE) 25797237Sek110237 hdr->b_flags |= ARC_L2CACHE; 25806245Smaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 25811544Seschrock buf->b_hdr = hdr; 25822688Smaybee buf->b_data = NULL; 25831544Seschrock buf->b_efunc = NULL; 25841544Seschrock buf->b_private = NULL; 25851544Seschrock buf->b_next = NULL; 25861544Seschrock hdr->b_buf = buf; 25872688Smaybee arc_get_data_buf(buf); 25881544Seschrock ASSERT(hdr->b_datacnt == 0); 25891544Seschrock hdr->b_datacnt = 1; 25902391Smaybee 2591789Sahrens } 2592789Sahrens 2593789Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2594789Sahrens acb->acb_done = done; 2595789Sahrens acb->acb_private = private; 2596789Sahrens 2597789Sahrens ASSERT(hdr->b_acb == NULL); 2598789Sahrens hdr->b_acb = acb; 2599789Sahrens hdr->b_flags |= ARC_IO_IN_PROGRESS; 2600789Sahrens 2601789Sahrens /* 2602789Sahrens * If the buffer has been evicted, migrate it to a present state 2603789Sahrens * before issuing the I/O. Once we drop the hash-table lock, 2604789Sahrens * the header will be marked as I/O in progress and have an 2605789Sahrens * attached buffer. At this point, anybody who finds this 2606789Sahrens * buffer ought to notice that it's legit but has a pending I/O. 2607789Sahrens */ 2608789Sahrens 26091544Seschrock if (GHOST_STATE(hdr->b_state)) 26102688Smaybee arc_access(hdr, hash_lock); 2611789Sahrens 26127754SJeff.Bonwick@Sun.COM if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 26137754SJeff.Bonwick@Sun.COM (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 26146987Sbrendan addr = hdr->b_l2hdr->b_daddr; 26157754SJeff.Bonwick@Sun.COM /* 26167754SJeff.Bonwick@Sun.COM * Lock out device removal. 26177754SJeff.Bonwick@Sun.COM */ 26187754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || 26197754SJeff.Bonwick@Sun.COM !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 26207754SJeff.Bonwick@Sun.COM vd = NULL; 26216987Sbrendan } 26226987Sbrendan 26236987Sbrendan mutex_exit(hash_lock); 26246987Sbrendan 2625789Sahrens ASSERT3U(hdr->b_size, ==, size); 26261596Sahrens DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size, 26271596Sahrens zbookmark_t *, zb); 26283403Sbmc ARCSTAT_BUMP(arcstat_misses); 26293403Sbmc ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 26303403Sbmc demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 26313403Sbmc data, metadata, misses); 26321544Seschrock 26337754SJeff.Bonwick@Sun.COM if (vd != NULL) { 26346987Sbrendan /* 26356987Sbrendan * Read from the L2ARC if the following are true: 26366987Sbrendan * 1. The L2ARC vdev was previously cached. 26376987Sbrendan * 2. This buffer still has L2ARC metadata. 26386987Sbrendan * 3. This buffer isn't currently writing to the L2ARC. 26396987Sbrendan * 4. The L2ARC entry wasn't evicted, which may 26406987Sbrendan * also have invalidated the vdev. 26416987Sbrendan */ 26427754SJeff.Bonwick@Sun.COM if (hdr->b_l2hdr != NULL && 26436987Sbrendan !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr)) { 26445450Sbrendan l2arc_read_callback_t *cb; 26455450Sbrendan 26466643Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 26476643Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 26486643Seschrock 26495450Sbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 26505450Sbrendan KM_SLEEP); 26515450Sbrendan cb->l2rcb_buf = buf; 26525450Sbrendan cb->l2rcb_spa = spa; 26535450Sbrendan cb->l2rcb_bp = *bp; 26545450Sbrendan cb->l2rcb_zb = *zb; 26557237Sek110237 cb->l2rcb_flags = zio_flags; 26565450Sbrendan 26575450Sbrendan /* 26587754SJeff.Bonwick@Sun.COM * l2arc read. The SCL_L2ARC lock will be 26597754SJeff.Bonwick@Sun.COM * released by l2arc_read_done(). 26605450Sbrendan */ 26615450Sbrendan rzio = zio_read_phys(pio, vd, addr, size, 26625450Sbrendan buf->b_data, ZIO_CHECKSUM_OFF, 26637237Sek110237 l2arc_read_done, cb, priority, zio_flags | 26647361SBrendan.Gregg@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 26657754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_PROPAGATE | 26667754SJeff.Bonwick@Sun.COM ZIO_FLAG_DONT_RETRY, B_FALSE); 26675450Sbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 26685450Sbrendan zio_t *, rzio); 26696987Sbrendan 26706987Sbrendan if (*arc_flags & ARC_NOWAIT) { 26716987Sbrendan zio_nowait(rzio); 26726987Sbrendan return (0); 26736987Sbrendan } 26746987Sbrendan 26756987Sbrendan ASSERT(*arc_flags & ARC_WAIT); 26766987Sbrendan if (zio_wait(rzio) == 0) 26776987Sbrendan return (0); 26786987Sbrendan 26796987Sbrendan /* l2arc read error; goto zio_read() */ 26805450Sbrendan } else { 26815450Sbrendan DTRACE_PROBE1(l2arc__miss, 26825450Sbrendan arc_buf_hdr_t *, hdr); 26835450Sbrendan ARCSTAT_BUMP(arcstat_l2_misses); 26845450Sbrendan if (HDR_L2_WRITING(hdr)) 26855450Sbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 26867754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, vd); 26875450Sbrendan } 26885450Sbrendan } 26896643Seschrock 2690789Sahrens rzio = zio_read(pio, spa, bp, buf->b_data, size, 26917237Sek110237 arc_read_done, buf, priority, zio_flags, zb); 2692789Sahrens 26932391Smaybee if (*arc_flags & ARC_WAIT) 2694789Sahrens return (zio_wait(rzio)); 2695789Sahrens 26962391Smaybee ASSERT(*arc_flags & ARC_NOWAIT); 2697789Sahrens zio_nowait(rzio); 2698789Sahrens } 2699789Sahrens return (0); 2700789Sahrens } 2701789Sahrens 2702789Sahrens /* 2703789Sahrens * arc_read() variant to support pool traversal. If the block is already 2704789Sahrens * in the ARC, make a copy of it; otherwise, the caller will do the I/O. 2705789Sahrens * The idea is that we don't want pool traversal filling up memory, but 2706789Sahrens * if the ARC already has the data anyway, we shouldn't pay for the I/O. 2707789Sahrens */ 2708789Sahrens int 2709789Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data) 2710789Sahrens { 2711789Sahrens arc_buf_hdr_t *hdr; 2712789Sahrens kmutex_t *hash_mtx; 2713789Sahrens int rc = 0; 2714789Sahrens 2715789Sahrens hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx); 2716789Sahrens 27171544Seschrock if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) { 27181544Seschrock arc_buf_t *buf = hdr->b_buf; 27191544Seschrock 27201544Seschrock ASSERT(buf); 27211544Seschrock while (buf->b_data == NULL) { 27221544Seschrock buf = buf->b_next; 27231544Seschrock ASSERT(buf); 27241544Seschrock } 27251544Seschrock bcopy(buf->b_data, data, hdr->b_size); 27261544Seschrock } else { 2727789Sahrens rc = ENOENT; 27281544Seschrock } 2729789Sahrens 2730789Sahrens if (hash_mtx) 2731789Sahrens mutex_exit(hash_mtx); 2732789Sahrens 2733789Sahrens return (rc); 2734789Sahrens } 2735789Sahrens 27361544Seschrock void 27371544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 27381544Seschrock { 27391544Seschrock ASSERT(buf->b_hdr != NULL); 27403403Sbmc ASSERT(buf->b_hdr->b_state != arc_anon); 27411544Seschrock ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 27421544Seschrock buf->b_efunc = func; 27431544Seschrock buf->b_private = private; 27441544Seschrock } 27451544Seschrock 27461544Seschrock /* 27471544Seschrock * This is used by the DMU to let the ARC know that a buffer is 27481544Seschrock * being evicted, so the ARC should clean up. If this arc buf 27491544Seschrock * is not yet in the evicted state, it will be put there. 27501544Seschrock */ 27511544Seschrock int 27521544Seschrock arc_buf_evict(arc_buf_t *buf) 27531544Seschrock { 27542887Smaybee arc_buf_hdr_t *hdr; 27551544Seschrock kmutex_t *hash_lock; 27561544Seschrock arc_buf_t **bufp; 27571544Seschrock 27587545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 27592887Smaybee hdr = buf->b_hdr; 27601544Seschrock if (hdr == NULL) { 27611544Seschrock /* 27621544Seschrock * We are in arc_do_user_evicts(). 27631544Seschrock */ 27641544Seschrock ASSERT(buf->b_data == NULL); 27657545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 27661544Seschrock return (0); 27677545SMark.Maybee@Sun.COM } else if (buf->b_data == NULL) { 27687545SMark.Maybee@Sun.COM arc_buf_t copy = *buf; /* structure assignment */ 27697545SMark.Maybee@Sun.COM /* 27707545SMark.Maybee@Sun.COM * We are on the eviction list; process this buffer now 27717545SMark.Maybee@Sun.COM * but let arc_do_user_evicts() do the reaping. 27727545SMark.Maybee@Sun.COM */ 27737545SMark.Maybee@Sun.COM buf->b_efunc = NULL; 27747545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 27757545SMark.Maybee@Sun.COM VERIFY(copy.b_efunc(©) == 0); 27767545SMark.Maybee@Sun.COM return (1); 27771544Seschrock } 27782887Smaybee hash_lock = HDR_LOCK(hdr); 27791544Seschrock mutex_enter(hash_lock); 27801544Seschrock 27812724Smaybee ASSERT(buf->b_hdr == hdr); 27822724Smaybee ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 27833403Sbmc ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 27841544Seschrock 27851544Seschrock /* 27861544Seschrock * Pull this buffer off of the hdr 27871544Seschrock */ 27881544Seschrock bufp = &hdr->b_buf; 27891544Seschrock while (*bufp != buf) 27901544Seschrock bufp = &(*bufp)->b_next; 27911544Seschrock *bufp = buf->b_next; 27921544Seschrock 27931544Seschrock ASSERT(buf->b_data != NULL); 27942688Smaybee arc_buf_destroy(buf, FALSE, FALSE); 27951544Seschrock 27961544Seschrock if (hdr->b_datacnt == 0) { 27971544Seschrock arc_state_t *old_state = hdr->b_state; 27981544Seschrock arc_state_t *evicted_state; 27991544Seschrock 28001544Seschrock ASSERT(refcount_is_zero(&hdr->b_refcnt)); 28011544Seschrock 28021544Seschrock evicted_state = 28033403Sbmc (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 28041544Seschrock 28053403Sbmc mutex_enter(&old_state->arcs_mtx); 28063403Sbmc mutex_enter(&evicted_state->arcs_mtx); 28071544Seschrock 28081544Seschrock arc_change_state(evicted_state, hdr, hash_lock); 28091544Seschrock ASSERT(HDR_IN_HASH_TABLE(hdr)); 28105450Sbrendan hdr->b_flags |= ARC_IN_HASH_TABLE; 28115450Sbrendan hdr->b_flags &= ~ARC_BUF_AVAILABLE; 28121544Seschrock 28133403Sbmc mutex_exit(&evicted_state->arcs_mtx); 28143403Sbmc mutex_exit(&old_state->arcs_mtx); 28151544Seschrock } 28161544Seschrock mutex_exit(hash_lock); 28177545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 28181819Smaybee 28191544Seschrock VERIFY(buf->b_efunc(buf) == 0); 28201544Seschrock buf->b_efunc = NULL; 28211544Seschrock buf->b_private = NULL; 28221544Seschrock buf->b_hdr = NULL; 28231544Seschrock kmem_cache_free(buf_cache, buf); 28241544Seschrock return (1); 28251544Seschrock } 28261544Seschrock 2827789Sahrens /* 2828789Sahrens * Release this buffer from the cache. This must be done 2829789Sahrens * after a read and prior to modifying the buffer contents. 2830789Sahrens * If the buffer has more than one reference, we must make 28317046Sahrens * a new hdr for the buffer. 2832789Sahrens */ 2833789Sahrens void 2834789Sahrens arc_release(arc_buf_t *buf, void *tag) 2835789Sahrens { 28367545SMark.Maybee@Sun.COM arc_buf_hdr_t *hdr; 28377545SMark.Maybee@Sun.COM kmutex_t *hash_lock; 28387545SMark.Maybee@Sun.COM l2arc_buf_hdr_t *l2hdr; 28395450Sbrendan uint64_t buf_size; 2840789Sahrens 28417545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_WRITER); 28427545SMark.Maybee@Sun.COM hdr = buf->b_hdr; 28437545SMark.Maybee@Sun.COM 2844789Sahrens /* this buffer is not on any list */ 2845789Sahrens ASSERT(refcount_count(&hdr->b_refcnt) > 0); 28467046Sahrens ASSERT(!(hdr->b_flags & ARC_STORED)); 2847789Sahrens 28483403Sbmc if (hdr->b_state == arc_anon) { 2849789Sahrens /* this buffer is already released */ 2850789Sahrens ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2851789Sahrens ASSERT(BUF_EMPTY(hdr)); 28521544Seschrock ASSERT(buf->b_efunc == NULL); 28533093Sahrens arc_buf_thaw(buf); 28547545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 2855789Sahrens return; 2856789Sahrens } 2857789Sahrens 28587545SMark.Maybee@Sun.COM hash_lock = HDR_LOCK(hdr); 2859789Sahrens mutex_enter(hash_lock); 2860789Sahrens 28617545SMark.Maybee@Sun.COM l2hdr = hdr->b_l2hdr; 28627545SMark.Maybee@Sun.COM if (l2hdr) { 28637545SMark.Maybee@Sun.COM mutex_enter(&l2arc_buflist_mtx); 28647545SMark.Maybee@Sun.COM hdr->b_l2hdr = NULL; 28657545SMark.Maybee@Sun.COM buf_size = hdr->b_size; 28667545SMark.Maybee@Sun.COM } 28677545SMark.Maybee@Sun.COM 28681544Seschrock /* 28691544Seschrock * Do we have more than one buf? 28701544Seschrock */ 28717545SMark.Maybee@Sun.COM if (hdr->b_datacnt > 1) { 2872789Sahrens arc_buf_hdr_t *nhdr; 2873789Sahrens arc_buf_t **bufp; 2874789Sahrens uint64_t blksz = hdr->b_size; 2875789Sahrens spa_t *spa = hdr->b_spa; 28763290Sjohansen arc_buf_contents_t type = hdr->b_type; 28775450Sbrendan uint32_t flags = hdr->b_flags; 2878789Sahrens 28797545SMark.Maybee@Sun.COM ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 2880789Sahrens /* 2881789Sahrens * Pull the data off of this buf and attach it to 2882789Sahrens * a new anonymous buf. 2883789Sahrens */ 28841544Seschrock (void) remove_reference(hdr, hash_lock, tag); 2885789Sahrens bufp = &hdr->b_buf; 28861544Seschrock while (*bufp != buf) 2887789Sahrens bufp = &(*bufp)->b_next; 2888789Sahrens *bufp = (*bufp)->b_next; 28893897Smaybee buf->b_next = NULL; 28901544Seschrock 28913403Sbmc ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 28923403Sbmc atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 28931544Seschrock if (refcount_is_zero(&hdr->b_refcnt)) { 28944309Smaybee uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 28954309Smaybee ASSERT3U(*size, >=, hdr->b_size); 28964309Smaybee atomic_add_64(size, -hdr->b_size); 28971544Seschrock } 28981544Seschrock hdr->b_datacnt -= 1; 28993547Smaybee arc_cksum_verify(buf); 29001544Seschrock 2901789Sahrens mutex_exit(hash_lock); 2902789Sahrens 29036245Smaybee nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 2904789Sahrens nhdr->b_size = blksz; 2905789Sahrens nhdr->b_spa = spa; 29063290Sjohansen nhdr->b_type = type; 2907789Sahrens nhdr->b_buf = buf; 29083403Sbmc nhdr->b_state = arc_anon; 2909789Sahrens nhdr->b_arc_access = 0; 29105450Sbrendan nhdr->b_flags = flags & ARC_L2_WRITING; 29115450Sbrendan nhdr->b_l2hdr = NULL; 29121544Seschrock nhdr->b_datacnt = 1; 29133547Smaybee nhdr->b_freeze_cksum = NULL; 29143897Smaybee (void) refcount_add(&nhdr->b_refcnt, tag); 2915789Sahrens buf->b_hdr = nhdr; 29167545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29173403Sbmc atomic_add_64(&arc_anon->arcs_size, blksz); 2918789Sahrens } else { 29197545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29201544Seschrock ASSERT(refcount_count(&hdr->b_refcnt) == 1); 2921789Sahrens ASSERT(!list_link_active(&hdr->b_arc_node)); 2922789Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 29233403Sbmc arc_change_state(arc_anon, hdr, hash_lock); 2924789Sahrens hdr->b_arc_access = 0; 2925789Sahrens mutex_exit(hash_lock); 29265450Sbrendan 2927789Sahrens bzero(&hdr->b_dva, sizeof (dva_t)); 2928789Sahrens hdr->b_birth = 0; 2929789Sahrens hdr->b_cksum0 = 0; 29303547Smaybee arc_buf_thaw(buf); 2931789Sahrens } 29321544Seschrock buf->b_efunc = NULL; 29331544Seschrock buf->b_private = NULL; 29345450Sbrendan 29355450Sbrendan if (l2hdr) { 29365450Sbrendan list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 29375450Sbrendan kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 29385450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -buf_size); 29397545SMark.Maybee@Sun.COM mutex_exit(&l2arc_buflist_mtx); 29405450Sbrendan } 2941789Sahrens } 2942789Sahrens 2943789Sahrens int 2944789Sahrens arc_released(arc_buf_t *buf) 2945789Sahrens { 29467545SMark.Maybee@Sun.COM int released; 29477545SMark.Maybee@Sun.COM 29487545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 29497545SMark.Maybee@Sun.COM released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 29507545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29517545SMark.Maybee@Sun.COM return (released); 29521544Seschrock } 29531544Seschrock 29541544Seschrock int 29551544Seschrock arc_has_callback(arc_buf_t *buf) 29561544Seschrock { 29577545SMark.Maybee@Sun.COM int callback; 29587545SMark.Maybee@Sun.COM 29597545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 29607545SMark.Maybee@Sun.COM callback = (buf->b_efunc != NULL); 29617545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29627545SMark.Maybee@Sun.COM return (callback); 2963789Sahrens } 2964789Sahrens 29651544Seschrock #ifdef ZFS_DEBUG 29661544Seschrock int 29671544Seschrock arc_referenced(arc_buf_t *buf) 29681544Seschrock { 29697545SMark.Maybee@Sun.COM int referenced; 29707545SMark.Maybee@Sun.COM 29717545SMark.Maybee@Sun.COM rw_enter(&buf->b_lock, RW_READER); 29727545SMark.Maybee@Sun.COM referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 29737545SMark.Maybee@Sun.COM rw_exit(&buf->b_lock); 29747545SMark.Maybee@Sun.COM return (referenced); 29751544Seschrock } 29761544Seschrock #endif 29771544Seschrock 2978789Sahrens static void 29793547Smaybee arc_write_ready(zio_t *zio) 29803547Smaybee { 29813547Smaybee arc_write_callback_t *callback = zio->io_private; 29823547Smaybee arc_buf_t *buf = callback->awcb_buf; 29835329Sgw25295 arc_buf_hdr_t *hdr = buf->b_hdr; 29845329Sgw25295 29857754SJeff.Bonwick@Sun.COM ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 29867754SJeff.Bonwick@Sun.COM callback->awcb_ready(zio, buf, callback->awcb_private); 29877754SJeff.Bonwick@Sun.COM 29885329Sgw25295 /* 29895329Sgw25295 * If the IO is already in progress, then this is a re-write 29907754SJeff.Bonwick@Sun.COM * attempt, so we need to thaw and re-compute the cksum. 29917754SJeff.Bonwick@Sun.COM * It is the responsibility of the callback to handle the 29927754SJeff.Bonwick@Sun.COM * accounting for any re-write attempt. 29935329Sgw25295 */ 29945329Sgw25295 if (HDR_IO_IN_PROGRESS(hdr)) { 29955329Sgw25295 mutex_enter(&hdr->b_freeze_lock); 29965329Sgw25295 if (hdr->b_freeze_cksum != NULL) { 29975329Sgw25295 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 29985329Sgw25295 hdr->b_freeze_cksum = NULL; 29995329Sgw25295 } 30005329Sgw25295 mutex_exit(&hdr->b_freeze_lock); 30015329Sgw25295 } 30025450Sbrendan arc_cksum_compute(buf, B_FALSE); 30035329Sgw25295 hdr->b_flags |= ARC_IO_IN_PROGRESS; 30043547Smaybee } 30053547Smaybee 30063547Smaybee static void 3007789Sahrens arc_write_done(zio_t *zio) 3008789Sahrens { 30093547Smaybee arc_write_callback_t *callback = zio->io_private; 30103547Smaybee arc_buf_t *buf = callback->awcb_buf; 30113547Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 3012789Sahrens 3013789Sahrens hdr->b_acb = NULL; 3014789Sahrens 3015789Sahrens hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3016789Sahrens hdr->b_birth = zio->io_bp->blk_birth; 3017789Sahrens hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 30181544Seschrock /* 30191544Seschrock * If the block to be written was all-zero, we may have 30201544Seschrock * compressed it away. In this case no write was performed 30211544Seschrock * so there will be no dva/birth-date/checksum. The buffer 30221544Seschrock * must therefor remain anonymous (and uncached). 30231544Seschrock */ 3024789Sahrens if (!BUF_EMPTY(hdr)) { 3025789Sahrens arc_buf_hdr_t *exists; 3026789Sahrens kmutex_t *hash_lock; 3027789Sahrens 30283093Sahrens arc_cksum_verify(buf); 30293093Sahrens 3030789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3031789Sahrens if (exists) { 3032789Sahrens /* 3033789Sahrens * This can only happen if we overwrite for 3034789Sahrens * sync-to-convergence, because we remove 3035789Sahrens * buffers from the hash table when we arc_free(). 3036789Sahrens */ 30377754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_IO_REWRITE); 3038789Sahrens ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig), 3039789Sahrens BP_IDENTITY(zio->io_bp))); 3040789Sahrens ASSERT3U(zio->io_bp_orig.blk_birth, ==, 3041789Sahrens zio->io_bp->blk_birth); 3042789Sahrens 3043789Sahrens ASSERT(refcount_is_zero(&exists->b_refcnt)); 30443403Sbmc arc_change_state(arc_anon, exists, hash_lock); 3045789Sahrens mutex_exit(hash_lock); 30461544Seschrock arc_hdr_destroy(exists); 3047789Sahrens exists = buf_hash_insert(hdr, &hash_lock); 3048789Sahrens ASSERT3P(exists, ==, NULL); 3049789Sahrens } 30501544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 30517046Sahrens /* if it's not anon, we are doing a scrub */ 30527046Sahrens if (hdr->b_state == arc_anon) 30537046Sahrens arc_access(hdr, hash_lock); 30542688Smaybee mutex_exit(hash_lock); 30553547Smaybee } else if (callback->awcb_done == NULL) { 30561544Seschrock int destroy_hdr; 30571544Seschrock /* 30581544Seschrock * This is an anonymous buffer with no user callback, 30591544Seschrock * destroy it if there are no active references. 30601544Seschrock */ 30611544Seschrock mutex_enter(&arc_eviction_mtx); 30621544Seschrock destroy_hdr = refcount_is_zero(&hdr->b_refcnt); 30631544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 30641544Seschrock mutex_exit(&arc_eviction_mtx); 30651544Seschrock if (destroy_hdr) 30661544Seschrock arc_hdr_destroy(hdr); 30671544Seschrock } else { 30681544Seschrock hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3069789Sahrens } 30707046Sahrens hdr->b_flags &= ~ARC_STORED; 30711544Seschrock 30723547Smaybee if (callback->awcb_done) { 3073789Sahrens ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 30743547Smaybee callback->awcb_done(zio, buf, callback->awcb_private); 3075789Sahrens } 3076789Sahrens 30773547Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 3078789Sahrens } 3079789Sahrens 30807872STim.Haley@Sun.COM void 30817754SJeff.Bonwick@Sun.COM write_policy(spa_t *spa, const writeprops_t *wp, zio_prop_t *zp) 30827046Sahrens { 30837046Sahrens boolean_t ismd = (wp->wp_level > 0 || dmu_ot[wp->wp_type].ot_metadata); 30847046Sahrens 30857046Sahrens /* Determine checksum setting */ 30867046Sahrens if (ismd) { 30877046Sahrens /* 30887046Sahrens * Metadata always gets checksummed. If the data 30897046Sahrens * checksum is multi-bit correctable, and it's not a 30907046Sahrens * ZBT-style checksum, then it's suitable for metadata 30917046Sahrens * as well. Otherwise, the metadata checksum defaults 30927046Sahrens * to fletcher4. 30937046Sahrens */ 30947046Sahrens if (zio_checksum_table[wp->wp_oschecksum].ci_correctable && 30957046Sahrens !zio_checksum_table[wp->wp_oschecksum].ci_zbt) 30967754SJeff.Bonwick@Sun.COM zp->zp_checksum = wp->wp_oschecksum; 30977046Sahrens else 30987754SJeff.Bonwick@Sun.COM zp->zp_checksum = ZIO_CHECKSUM_FLETCHER_4; 30997046Sahrens } else { 31007754SJeff.Bonwick@Sun.COM zp->zp_checksum = zio_checksum_select(wp->wp_dnchecksum, 31017046Sahrens wp->wp_oschecksum); 31027046Sahrens } 31037046Sahrens 31047046Sahrens /* Determine compression setting */ 31057046Sahrens if (ismd) { 31067046Sahrens /* 31077046Sahrens * XXX -- we should design a compression algorithm 31087046Sahrens * that specializes in arrays of bps. 31097046Sahrens */ 31107754SJeff.Bonwick@Sun.COM zp->zp_compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY : 31117046Sahrens ZIO_COMPRESS_LZJB; 31127046Sahrens } else { 31137754SJeff.Bonwick@Sun.COM zp->zp_compress = zio_compress_select(wp->wp_dncompress, 31147046Sahrens wp->wp_oscompress); 31157046Sahrens } 31167754SJeff.Bonwick@Sun.COM 31177754SJeff.Bonwick@Sun.COM zp->zp_type = wp->wp_type; 31187754SJeff.Bonwick@Sun.COM zp->zp_level = wp->wp_level; 31197754SJeff.Bonwick@Sun.COM zp->zp_ndvas = MIN(wp->wp_copies + ismd, spa_max_replication(spa)); 31207046Sahrens } 31217046Sahrens 31223547Smaybee zio_t * 31237046Sahrens arc_write(zio_t *pio, spa_t *spa, const writeprops_t *wp, 31247237Sek110237 boolean_t l2arc, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 31253547Smaybee arc_done_func_t *ready, arc_done_func_t *done, void *private, int priority, 31267237Sek110237 int zio_flags, const zbookmark_t *zb) 3127789Sahrens { 3128789Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 31293547Smaybee arc_write_callback_t *callback; 31307754SJeff.Bonwick@Sun.COM zio_t *zio; 31317754SJeff.Bonwick@Sun.COM zio_prop_t zp; 31327754SJeff.Bonwick@Sun.COM 31337754SJeff.Bonwick@Sun.COM ASSERT(ready != NULL); 3134789Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 31352237Smaybee ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 31362237Smaybee ASSERT(hdr->b_acb == 0); 31377237Sek110237 if (l2arc) 31387237Sek110237 hdr->b_flags |= ARC_L2CACHE; 31393547Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 31403547Smaybee callback->awcb_ready = ready; 31413547Smaybee callback->awcb_done = done; 31423547Smaybee callback->awcb_private = private; 31433547Smaybee callback->awcb_buf = buf; 31447046Sahrens 31457754SJeff.Bonwick@Sun.COM write_policy(spa, wp, &zp); 31467754SJeff.Bonwick@Sun.COM zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, &zp, 31477754SJeff.Bonwick@Sun.COM arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3148789Sahrens 31493547Smaybee return (zio); 3150789Sahrens } 3151789Sahrens 3152789Sahrens int 3153789Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 3154789Sahrens zio_done_func_t *done, void *private, uint32_t arc_flags) 3155789Sahrens { 3156789Sahrens arc_buf_hdr_t *ab; 3157789Sahrens kmutex_t *hash_lock; 3158789Sahrens zio_t *zio; 3159789Sahrens 3160789Sahrens /* 3161789Sahrens * If this buffer is in the cache, release it, so it 3162789Sahrens * can be re-used. 3163789Sahrens */ 3164789Sahrens ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 3165789Sahrens if (ab != NULL) { 3166789Sahrens /* 3167789Sahrens * The checksum of blocks to free is not always 3168789Sahrens * preserved (eg. on the deadlist). However, if it is 3169789Sahrens * nonzero, it should match what we have in the cache. 3170789Sahrens */ 3171789Sahrens ASSERT(bp->blk_cksum.zc_word[0] == 0 || 31727754SJeff.Bonwick@Sun.COM bp->blk_cksum.zc_word[0] == ab->b_cksum0 || 31737754SJeff.Bonwick@Sun.COM bp->blk_fill == BLK_FILL_ALREADY_FREED); 31747754SJeff.Bonwick@Sun.COM 31753403Sbmc if (ab->b_state != arc_anon) 31763403Sbmc arc_change_state(arc_anon, ab, hash_lock); 31772391Smaybee if (HDR_IO_IN_PROGRESS(ab)) { 31782391Smaybee /* 31792391Smaybee * This should only happen when we prefetch. 31802391Smaybee */ 31812391Smaybee ASSERT(ab->b_flags & ARC_PREFETCH); 31822391Smaybee ASSERT3U(ab->b_datacnt, ==, 1); 31832391Smaybee ab->b_flags |= ARC_FREED_IN_READ; 31842391Smaybee if (HDR_IN_HASH_TABLE(ab)) 31852391Smaybee buf_hash_remove(ab); 31862391Smaybee ab->b_arc_access = 0; 31872391Smaybee bzero(&ab->b_dva, sizeof (dva_t)); 31882391Smaybee ab->b_birth = 0; 31892391Smaybee ab->b_cksum0 = 0; 31902391Smaybee ab->b_buf->b_efunc = NULL; 31912391Smaybee ab->b_buf->b_private = NULL; 31922391Smaybee mutex_exit(hash_lock); 31932391Smaybee } else if (refcount_is_zero(&ab->b_refcnt)) { 31945450Sbrendan ab->b_flags |= ARC_FREE_IN_PROGRESS; 3195789Sahrens mutex_exit(hash_lock); 31961544Seschrock arc_hdr_destroy(ab); 31973403Sbmc ARCSTAT_BUMP(arcstat_deleted); 3198789Sahrens } else { 31991589Smaybee /* 32002391Smaybee * We still have an active reference on this 32012391Smaybee * buffer. This can happen, e.g., from 32022391Smaybee * dbuf_unoverride(). 32031589Smaybee */ 32042391Smaybee ASSERT(!HDR_IN_HASH_TABLE(ab)); 3205789Sahrens ab->b_arc_access = 0; 3206789Sahrens bzero(&ab->b_dva, sizeof (dva_t)); 3207789Sahrens ab->b_birth = 0; 3208789Sahrens ab->b_cksum0 = 0; 32091544Seschrock ab->b_buf->b_efunc = NULL; 32101544Seschrock ab->b_buf->b_private = NULL; 3211789Sahrens mutex_exit(hash_lock); 3212789Sahrens } 3213789Sahrens } 3214789Sahrens 32157754SJeff.Bonwick@Sun.COM zio = zio_free(pio, spa, txg, bp, done, private, ZIO_FLAG_MUSTSUCCEED); 3216789Sahrens 3217789Sahrens if (arc_flags & ARC_WAIT) 3218789Sahrens return (zio_wait(zio)); 3219789Sahrens 3220789Sahrens ASSERT(arc_flags & ARC_NOWAIT); 3221789Sahrens zio_nowait(zio); 3222789Sahrens 3223789Sahrens return (0); 3224789Sahrens } 3225789Sahrens 32266245Smaybee static int 32276245Smaybee arc_memory_throttle(uint64_t reserve, uint64_t txg) 32286245Smaybee { 32296245Smaybee #ifdef _KERNEL 32306245Smaybee uint64_t inflight_data = arc_anon->arcs_size; 32316245Smaybee uint64_t available_memory = ptob(freemem); 32326245Smaybee static uint64_t page_load = 0; 32336245Smaybee static uint64_t last_txg = 0; 32346245Smaybee 32356245Smaybee #if defined(__i386) 32366245Smaybee available_memory = 32376245Smaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 32386245Smaybee #endif 32396245Smaybee if (available_memory >= zfs_write_limit_max) 32406245Smaybee return (0); 32416245Smaybee 32426245Smaybee if (txg > last_txg) { 32436245Smaybee last_txg = txg; 32446245Smaybee page_load = 0; 32456245Smaybee } 32466245Smaybee /* 32476245Smaybee * If we are in pageout, we know that memory is already tight, 32486245Smaybee * the arc is already going to be evicting, so we just want to 32496245Smaybee * continue to let page writes occur as quickly as possible. 32506245Smaybee */ 32516245Smaybee if (curproc == proc_pageout) { 32526245Smaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 32536245Smaybee return (ERESTART); 32546245Smaybee /* Note: reserve is inflated, so we deflate */ 32556245Smaybee page_load += reserve / 8; 32566245Smaybee return (0); 32576245Smaybee } else if (page_load > 0 && arc_reclaim_needed()) { 32586245Smaybee /* memory is low, delay before restarting */ 32596245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 32606245Smaybee return (EAGAIN); 32616245Smaybee } 32626245Smaybee page_load = 0; 32636245Smaybee 32646245Smaybee if (arc_size > arc_c_min) { 32656245Smaybee uint64_t evictable_memory = 32666245Smaybee arc_mru->arcs_lsize[ARC_BUFC_DATA] + 32676245Smaybee arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 32686245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 32696245Smaybee arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 32706245Smaybee available_memory += MIN(evictable_memory, arc_size - arc_c_min); 32716245Smaybee } 32726245Smaybee 32736245Smaybee if (inflight_data > available_memory / 4) { 32746245Smaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 32756245Smaybee return (ERESTART); 32766245Smaybee } 32776245Smaybee #endif 32786245Smaybee return (0); 32796245Smaybee } 32806245Smaybee 3281789Sahrens void 32826245Smaybee arc_tempreserve_clear(uint64_t reserve) 3283789Sahrens { 32846245Smaybee atomic_add_64(&arc_tempreserve, -reserve); 3285789Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 3286789Sahrens } 3287789Sahrens 3288789Sahrens int 32896245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 3290789Sahrens { 32916245Smaybee int error; 32926245Smaybee 3293789Sahrens #ifdef ZFS_DEBUG 3294789Sahrens /* 3295789Sahrens * Once in a while, fail for no reason. Everything should cope. 3296789Sahrens */ 3297789Sahrens if (spa_get_random(10000) == 0) { 3298789Sahrens dprintf("forcing random failure\n"); 3299789Sahrens return (ERESTART); 3300789Sahrens } 3301789Sahrens #endif 33026245Smaybee if (reserve > arc_c/4 && !arc_no_grow) 33036245Smaybee arc_c = MIN(arc_c_max, reserve * 4); 33046245Smaybee if (reserve > arc_c) 3305982Smaybee return (ENOMEM); 3306982Smaybee 3307789Sahrens /* 33086245Smaybee * Writes will, almost always, require additional memory allocations 33096245Smaybee * in order to compress/encrypt/etc the data. We therefor need to 33106245Smaybee * make sure that there is sufficient available memory for this. 33116245Smaybee */ 33126245Smaybee if (error = arc_memory_throttle(reserve, txg)) 33136245Smaybee return (error); 33146245Smaybee 33156245Smaybee /* 3316982Smaybee * Throttle writes when the amount of dirty data in the cache 3317982Smaybee * gets too large. We try to keep the cache less than half full 3318982Smaybee * of dirty blocks so that our sync times don't grow too large. 3319982Smaybee * Note: if two requests come in concurrently, we might let them 3320982Smaybee * both succeed, when one of them should fail. Not a huge deal. 3321789Sahrens */ 33226245Smaybee if (reserve + arc_tempreserve + arc_anon->arcs_size > arc_c / 2 && 33236245Smaybee arc_anon->arcs_size > arc_c / 4) { 33244309Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 33254309Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 33264309Smaybee arc_tempreserve>>10, 33274309Smaybee arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 33284309Smaybee arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 33296245Smaybee reserve>>10, arc_c>>10); 3330789Sahrens return (ERESTART); 3331789Sahrens } 33326245Smaybee atomic_add_64(&arc_tempreserve, reserve); 3333789Sahrens return (0); 3334789Sahrens } 3335789Sahrens 3336789Sahrens void 3337789Sahrens arc_init(void) 3338789Sahrens { 3339789Sahrens mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3340789Sahrens cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3341789Sahrens 33422391Smaybee /* Convert seconds to clock ticks */ 33432638Sperrin arc_min_prefetch_lifespan = 1 * hz; 33442391Smaybee 3345789Sahrens /* Start out with 1/8 of all memory */ 33463403Sbmc arc_c = physmem * PAGESIZE / 8; 3347789Sahrens 3348789Sahrens #ifdef _KERNEL 3349789Sahrens /* 3350789Sahrens * On architectures where the physical memory can be larger 3351789Sahrens * than the addressable space (intel in 32-bit mode), we may 3352789Sahrens * need to limit the cache to 1/8 of VM size. 3353789Sahrens */ 33543403Sbmc arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3355789Sahrens #endif 3356789Sahrens 3357982Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 33583403Sbmc arc_c_min = MAX(arc_c / 4, 64<<20); 3359982Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 33603403Sbmc if (arc_c * 8 >= 1<<30) 33613403Sbmc arc_c_max = (arc_c * 8) - (1<<30); 3362789Sahrens else 33633403Sbmc arc_c_max = arc_c_min; 33643403Sbmc arc_c_max = MAX(arc_c * 6, arc_c_max); 33652885Sahrens 33662885Sahrens /* 33672885Sahrens * Allow the tunables to override our calculations if they are 33682885Sahrens * reasonable (ie. over 64MB) 33692885Sahrens */ 33702885Sahrens if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE) 33713403Sbmc arc_c_max = zfs_arc_max; 33723403Sbmc if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max) 33733403Sbmc arc_c_min = zfs_arc_min; 33742885Sahrens 33753403Sbmc arc_c = arc_c_max; 33763403Sbmc arc_p = (arc_c >> 1); 3377789Sahrens 33784309Smaybee /* limit meta-data to 1/4 of the arc capacity */ 33794309Smaybee arc_meta_limit = arc_c_max / 4; 33804645Sek110237 33814645Sek110237 /* Allow the tunable to override if it is reasonable */ 33824645Sek110237 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 33834645Sek110237 arc_meta_limit = zfs_arc_meta_limit; 33844645Sek110237 33854309Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 33864309Smaybee arc_c_min = arc_meta_limit / 2; 33874309Smaybee 3388789Sahrens /* if kmem_flags are set, lets try to use less memory */ 3389789Sahrens if (kmem_debugging()) 33903403Sbmc arc_c = arc_c / 2; 33913403Sbmc if (arc_c < arc_c_min) 33923403Sbmc arc_c = arc_c_min; 3393789Sahrens 33943403Sbmc arc_anon = &ARC_anon; 33953403Sbmc arc_mru = &ARC_mru; 33963403Sbmc arc_mru_ghost = &ARC_mru_ghost; 33973403Sbmc arc_mfu = &ARC_mfu; 33983403Sbmc arc_mfu_ghost = &ARC_mfu_ghost; 33995450Sbrendan arc_l2c_only = &ARC_l2c_only; 34003403Sbmc arc_size = 0; 3401789Sahrens 34023403Sbmc mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34033403Sbmc mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34043403Sbmc mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34053403Sbmc mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34063403Sbmc mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34075450Sbrendan mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL); 34082688Smaybee 34094309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 34104309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34114309Smaybee list_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 34124309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34134309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 34144309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34154309Smaybee list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 34164309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34174309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 34184309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34194309Smaybee list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 34204309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34214309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 34224309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34234309Smaybee list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 34244309Smaybee sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34255450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 34265450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 34275450Sbrendan list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 34285450Sbrendan sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3429789Sahrens 3430789Sahrens buf_init(); 3431789Sahrens 3432789Sahrens arc_thread_exit = 0; 34331544Seschrock arc_eviction_list = NULL; 34341544Seschrock mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 34352887Smaybee bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3436789Sahrens 34373403Sbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 34383403Sbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 34393403Sbmc 34403403Sbmc if (arc_ksp != NULL) { 34413403Sbmc arc_ksp->ks_data = &arc_stats; 34423403Sbmc kstat_install(arc_ksp); 34433403Sbmc } 34443403Sbmc 3445789Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3446789Sahrens TS_RUN, minclsyspri); 34473158Smaybee 34483158Smaybee arc_dead = FALSE; 34496987Sbrendan arc_warm = B_FALSE; 34506245Smaybee 34516245Smaybee if (zfs_write_limit_max == 0) 34527468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 34536245Smaybee else 34546245Smaybee zfs_write_limit_shift = 0; 34557468SMark.Maybee@Sun.COM mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3456789Sahrens } 3457789Sahrens 3458789Sahrens void 3459789Sahrens arc_fini(void) 3460789Sahrens { 3461789Sahrens mutex_enter(&arc_reclaim_thr_lock); 3462789Sahrens arc_thread_exit = 1; 3463789Sahrens while (arc_thread_exit != 0) 3464789Sahrens cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3465789Sahrens mutex_exit(&arc_reclaim_thr_lock); 3466789Sahrens 34675642Smaybee arc_flush(NULL); 3468789Sahrens 3469789Sahrens arc_dead = TRUE; 3470789Sahrens 34713403Sbmc if (arc_ksp != NULL) { 34723403Sbmc kstat_delete(arc_ksp); 34733403Sbmc arc_ksp = NULL; 34743403Sbmc } 34753403Sbmc 34761544Seschrock mutex_destroy(&arc_eviction_mtx); 3477789Sahrens mutex_destroy(&arc_reclaim_thr_lock); 3478789Sahrens cv_destroy(&arc_reclaim_thr_cv); 3479789Sahrens 34804309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 34814309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 34824309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 34834309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 34844309Smaybee list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 34854309Smaybee list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 34864309Smaybee list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 34874309Smaybee list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 3488789Sahrens 34893403Sbmc mutex_destroy(&arc_anon->arcs_mtx); 34903403Sbmc mutex_destroy(&arc_mru->arcs_mtx); 34913403Sbmc mutex_destroy(&arc_mru_ghost->arcs_mtx); 34923403Sbmc mutex_destroy(&arc_mfu->arcs_mtx); 34933403Sbmc mutex_destroy(&arc_mfu_ghost->arcs_mtx); 34948214SRicardo.M.Correia@Sun.COM mutex_destroy(&arc_l2c_only->arcs_mtx); 34952856Snd150628 34967468SMark.Maybee@Sun.COM mutex_destroy(&zfs_write_limit_lock); 34977468SMark.Maybee@Sun.COM 3498789Sahrens buf_fini(); 3499789Sahrens } 35005450Sbrendan 35015450Sbrendan /* 35025450Sbrendan * Level 2 ARC 35035450Sbrendan * 35045450Sbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 35055450Sbrendan * It uses dedicated storage devices to hold cached data, which are populated 35065450Sbrendan * using large infrequent writes. The main role of this cache is to boost 35075450Sbrendan * the performance of random read workloads. The intended L2ARC devices 35085450Sbrendan * include short-stroked disks, solid state disks, and other media with 35095450Sbrendan * substantially faster read latency than disk. 35105450Sbrendan * 35115450Sbrendan * +-----------------------+ 35125450Sbrendan * | ARC | 35135450Sbrendan * +-----------------------+ 35145450Sbrendan * | ^ ^ 35155450Sbrendan * | | | 35165450Sbrendan * l2arc_feed_thread() arc_read() 35175450Sbrendan * | | | 35185450Sbrendan * | l2arc read | 35195450Sbrendan * V | | 35205450Sbrendan * +---------------+ | 35215450Sbrendan * | L2ARC | | 35225450Sbrendan * +---------------+ | 35235450Sbrendan * | ^ | 35245450Sbrendan * l2arc_write() | | 35255450Sbrendan * | | | 35265450Sbrendan * V | | 35275450Sbrendan * +-------+ +-------+ 35285450Sbrendan * | vdev | | vdev | 35295450Sbrendan * | cache | | cache | 35305450Sbrendan * +-------+ +-------+ 35315450Sbrendan * +=========+ .-----. 35325450Sbrendan * : L2ARC : |-_____-| 35335450Sbrendan * : devices : | Disks | 35345450Sbrendan * +=========+ `-_____-' 35355450Sbrendan * 35365450Sbrendan * Read requests are satisfied from the following sources, in order: 35375450Sbrendan * 35385450Sbrendan * 1) ARC 35395450Sbrendan * 2) vdev cache of L2ARC devices 35405450Sbrendan * 3) L2ARC devices 35415450Sbrendan * 4) vdev cache of disks 35425450Sbrendan * 5) disks 35435450Sbrendan * 35445450Sbrendan * Some L2ARC device types exhibit extremely slow write performance. 35455450Sbrendan * To accommodate for this there are some significant differences between 35465450Sbrendan * the L2ARC and traditional cache design: 35475450Sbrendan * 35485450Sbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 35495450Sbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 35505450Sbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 35515450Sbrendan * this would add inflated write latencies for all ARC memory pressure. 35525450Sbrendan * 35535450Sbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 35545450Sbrendan * It does this by periodically scanning buffers from the eviction-end of 35555450Sbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 35565450Sbrendan * not already there. It scans until a headroom of buffers is satisfied, 35575450Sbrendan * which itself is a buffer for ARC eviction. The thread that does this is 35585450Sbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 35595450Sbrendan * provide a better sense of ratio than this diagram: 35605450Sbrendan * 35615450Sbrendan * head --> tail 35625450Sbrendan * +---------------------+----------+ 35635450Sbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 35645450Sbrendan * +---------------------+----------+ | o L2ARC eligible 35655450Sbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 35665450Sbrendan * +---------------------+----------+ | 35675450Sbrendan * 15.9 Gbytes ^ 32 Mbytes | 35685450Sbrendan * headroom | 35695450Sbrendan * l2arc_feed_thread() 35705450Sbrendan * | 35715450Sbrendan * l2arc write hand <--[oooo]--' 35725450Sbrendan * | 8 Mbyte 35735450Sbrendan * | write max 35745450Sbrendan * V 35755450Sbrendan * +==============================+ 35765450Sbrendan * L2ARC dev |####|#|###|###| |####| ... | 35775450Sbrendan * +==============================+ 35785450Sbrendan * 32 Gbytes 35795450Sbrendan * 35805450Sbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 35815450Sbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 35825450Sbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 35835450Sbrendan * safe to say that this is an uncommon case, since buffers at the end of 35845450Sbrendan * the ARC lists have moved there due to inactivity. 35855450Sbrendan * 35865450Sbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 35875450Sbrendan * then the L2ARC simply misses copying some buffers. This serves as a 35885450Sbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 35895450Sbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 35905450Sbrendan * the potential for the L2ARC to churn if it attempts to cache content too 35915450Sbrendan * quickly, such as during backups of the entire pool. 35925450Sbrendan * 35936987Sbrendan * 5. After system boot and before the ARC has filled main memory, there are 35946987Sbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 35956987Sbrendan * lists can remain mostly static. Instead of searching from tail of these 35966987Sbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 35976987Sbrendan * for eligible buffers, greatly increasing its chance of finding them. 35986987Sbrendan * 35996987Sbrendan * The L2ARC device write speed is also boosted during this time so that 36006987Sbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 36016987Sbrendan * there are no L2ARC reads, and no fear of degrading read performance 36026987Sbrendan * through increased writes. 36036987Sbrendan * 36046987Sbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 36055450Sbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 36065450Sbrendan * device is written to in a rotor fashion, sweeping writes through 36075450Sbrendan * available space then repeating. 36085450Sbrendan * 36096987Sbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 36105450Sbrendan * write buffers back to disk based storage. 36115450Sbrendan * 36126987Sbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 36135450Sbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 36145450Sbrendan * 36155450Sbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 36165450Sbrendan * may be necessary for different workloads: 36175450Sbrendan * 36185450Sbrendan * l2arc_write_max max write bytes per interval 36196987Sbrendan * l2arc_write_boost extra write bytes during device warmup 36205450Sbrendan * l2arc_noprefetch skip caching prefetched buffers 36215450Sbrendan * l2arc_headroom number of max device writes to precache 36225450Sbrendan * l2arc_feed_secs seconds between L2ARC writing 36235450Sbrendan * 36245450Sbrendan * Tunables may be removed or added as future performance improvements are 36255450Sbrendan * integrated, and also may become zpool properties. 36265450Sbrendan */ 36275450Sbrendan 36285450Sbrendan static void 36295450Sbrendan l2arc_hdr_stat_add(void) 36305450Sbrendan { 36316018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 36326018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 36335450Sbrendan } 36345450Sbrendan 36355450Sbrendan static void 36365450Sbrendan l2arc_hdr_stat_remove(void) 36375450Sbrendan { 36386018Sbrendan ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 36396018Sbrendan ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 36405450Sbrendan } 36415450Sbrendan 36425450Sbrendan /* 36435450Sbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 36446987Sbrendan * If a device is returned, this also returns holding the spa config lock. 36455450Sbrendan */ 36465450Sbrendan static l2arc_dev_t * 36475450Sbrendan l2arc_dev_get_next(void) 36485450Sbrendan { 36496987Sbrendan l2arc_dev_t *first, *next = NULL; 36506987Sbrendan 36516987Sbrendan /* 36526987Sbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 36536987Sbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 36546987Sbrendan * both locks will be dropped and a spa config lock held instead. 36556987Sbrendan */ 36566987Sbrendan mutex_enter(&spa_namespace_lock); 36576987Sbrendan mutex_enter(&l2arc_dev_mtx); 36586643Seschrock 36596643Seschrock /* if there are no vdevs, there is nothing to do */ 36606643Seschrock if (l2arc_ndev == 0) 36616987Sbrendan goto out; 36626643Seschrock 36636643Seschrock first = NULL; 36646643Seschrock next = l2arc_dev_last; 36656643Seschrock do { 36666643Seschrock /* loop around the list looking for a non-faulted vdev */ 36676643Seschrock if (next == NULL) { 36685450Sbrendan next = list_head(l2arc_dev_list); 36696643Seschrock } else { 36706643Seschrock next = list_next(l2arc_dev_list, next); 36716643Seschrock if (next == NULL) 36726643Seschrock next = list_head(l2arc_dev_list); 36736643Seschrock } 36746643Seschrock 36756643Seschrock /* if we have come back to the start, bail out */ 36766643Seschrock if (first == NULL) 36776643Seschrock first = next; 36786643Seschrock else if (next == first) 36796643Seschrock break; 36806643Seschrock 36816643Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 36826643Seschrock 36836643Seschrock /* if we were unable to find any usable vdevs, return NULL */ 36846643Seschrock if (vdev_is_dead(next->l2ad_vdev)) 36856987Sbrendan next = NULL; 36865450Sbrendan 36875450Sbrendan l2arc_dev_last = next; 36885450Sbrendan 36896987Sbrendan out: 36906987Sbrendan mutex_exit(&l2arc_dev_mtx); 36916987Sbrendan 36926987Sbrendan /* 36936987Sbrendan * Grab the config lock to prevent the 'next' device from being 36946987Sbrendan * removed while we are writing to it. 36956987Sbrendan */ 36966987Sbrendan if (next != NULL) 36977754SJeff.Bonwick@Sun.COM spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 36986987Sbrendan mutex_exit(&spa_namespace_lock); 36996987Sbrendan 37005450Sbrendan return (next); 37015450Sbrendan } 37025450Sbrendan 37035450Sbrendan /* 37046987Sbrendan * Free buffers that were tagged for destruction. 37056987Sbrendan */ 37066987Sbrendan static void 37076987Sbrendan l2arc_do_free_on_write() 37086987Sbrendan { 37096987Sbrendan list_t *buflist; 37106987Sbrendan l2arc_data_free_t *df, *df_prev; 37116987Sbrendan 37126987Sbrendan mutex_enter(&l2arc_free_on_write_mtx); 37136987Sbrendan buflist = l2arc_free_on_write; 37146987Sbrendan 37156987Sbrendan for (df = list_tail(buflist); df; df = df_prev) { 37166987Sbrendan df_prev = list_prev(buflist, df); 37176987Sbrendan ASSERT(df->l2df_data != NULL); 37186987Sbrendan ASSERT(df->l2df_func != NULL); 37196987Sbrendan df->l2df_func(df->l2df_data, df->l2df_size); 37206987Sbrendan list_remove(buflist, df); 37216987Sbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 37226987Sbrendan } 37236987Sbrendan 37246987Sbrendan mutex_exit(&l2arc_free_on_write_mtx); 37256987Sbrendan } 37266987Sbrendan 37276987Sbrendan /* 37285450Sbrendan * A write to a cache device has completed. Update all headers to allow 37295450Sbrendan * reads from these buffers to begin. 37305450Sbrendan */ 37315450Sbrendan static void 37325450Sbrendan l2arc_write_done(zio_t *zio) 37335450Sbrendan { 37345450Sbrendan l2arc_write_callback_t *cb; 37355450Sbrendan l2arc_dev_t *dev; 37365450Sbrendan list_t *buflist; 37375450Sbrendan arc_buf_hdr_t *head, *ab, *ab_prev; 37386987Sbrendan l2arc_buf_hdr_t *abl2; 37395450Sbrendan kmutex_t *hash_lock; 37405450Sbrendan 37415450Sbrendan cb = zio->io_private; 37425450Sbrendan ASSERT(cb != NULL); 37435450Sbrendan dev = cb->l2wcb_dev; 37445450Sbrendan ASSERT(dev != NULL); 37455450Sbrendan head = cb->l2wcb_head; 37465450Sbrendan ASSERT(head != NULL); 37475450Sbrendan buflist = dev->l2ad_buflist; 37485450Sbrendan ASSERT(buflist != NULL); 37495450Sbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 37505450Sbrendan l2arc_write_callback_t *, cb); 37515450Sbrendan 37525450Sbrendan if (zio->io_error != 0) 37535450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 37545450Sbrendan 37555450Sbrendan mutex_enter(&l2arc_buflist_mtx); 37565450Sbrendan 37575450Sbrendan /* 37585450Sbrendan * All writes completed, or an error was hit. 37595450Sbrendan */ 37605450Sbrendan for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 37615450Sbrendan ab_prev = list_prev(buflist, ab); 37625450Sbrendan 37635450Sbrendan hash_lock = HDR_LOCK(ab); 37645450Sbrendan if (!mutex_tryenter(hash_lock)) { 37655450Sbrendan /* 37665450Sbrendan * This buffer misses out. It may be in a stage 37675450Sbrendan * of eviction. Its ARC_L2_WRITING flag will be 37685450Sbrendan * left set, denying reads to this buffer. 37695450Sbrendan */ 37705450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 37715450Sbrendan continue; 37725450Sbrendan } 37735450Sbrendan 37745450Sbrendan if (zio->io_error != 0) { 37755450Sbrendan /* 37766987Sbrendan * Error - drop L2ARC entry. 37775450Sbrendan */ 37786987Sbrendan list_remove(buflist, ab); 37796987Sbrendan abl2 = ab->b_l2hdr; 37805450Sbrendan ab->b_l2hdr = NULL; 37816987Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 37826987Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 37835450Sbrendan } 37845450Sbrendan 37855450Sbrendan /* 37865450Sbrendan * Allow ARC to begin reads to this L2ARC entry. 37875450Sbrendan */ 37885450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 37895450Sbrendan 37905450Sbrendan mutex_exit(hash_lock); 37915450Sbrendan } 37925450Sbrendan 37935450Sbrendan atomic_inc_64(&l2arc_writes_done); 37945450Sbrendan list_remove(buflist, head); 37955450Sbrendan kmem_cache_free(hdr_cache, head); 37965450Sbrendan mutex_exit(&l2arc_buflist_mtx); 37975450Sbrendan 37986987Sbrendan l2arc_do_free_on_write(); 37995450Sbrendan 38005450Sbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 38015450Sbrendan } 38025450Sbrendan 38035450Sbrendan /* 38045450Sbrendan * A read to a cache device completed. Validate buffer contents before 38055450Sbrendan * handing over to the regular ARC routines. 38065450Sbrendan */ 38075450Sbrendan static void 38085450Sbrendan l2arc_read_done(zio_t *zio) 38095450Sbrendan { 38105450Sbrendan l2arc_read_callback_t *cb; 38115450Sbrendan arc_buf_hdr_t *hdr; 38125450Sbrendan arc_buf_t *buf; 38135450Sbrendan kmutex_t *hash_lock; 38146987Sbrendan int equal; 38155450Sbrendan 38167754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd != NULL); 38177754SJeff.Bonwick@Sun.COM ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 38187754SJeff.Bonwick@Sun.COM 38197754SJeff.Bonwick@Sun.COM spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 38207754SJeff.Bonwick@Sun.COM 38215450Sbrendan cb = zio->io_private; 38225450Sbrendan ASSERT(cb != NULL); 38235450Sbrendan buf = cb->l2rcb_buf; 38245450Sbrendan ASSERT(buf != NULL); 38255450Sbrendan hdr = buf->b_hdr; 38265450Sbrendan ASSERT(hdr != NULL); 38275450Sbrendan 38285450Sbrendan hash_lock = HDR_LOCK(hdr); 38295450Sbrendan mutex_enter(hash_lock); 38305450Sbrendan 38315450Sbrendan /* 38325450Sbrendan * Check this survived the L2ARC journey. 38335450Sbrendan */ 38345450Sbrendan equal = arc_cksum_equal(buf); 38355450Sbrendan if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 38365450Sbrendan mutex_exit(hash_lock); 38375450Sbrendan zio->io_private = buf; 38387754SJeff.Bonwick@Sun.COM zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 38397754SJeff.Bonwick@Sun.COM zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 38405450Sbrendan arc_read_done(zio); 38415450Sbrendan } else { 38425450Sbrendan mutex_exit(hash_lock); 38435450Sbrendan /* 38445450Sbrendan * Buffer didn't survive caching. Increment stats and 38455450Sbrendan * reissue to the original storage device. 38465450Sbrendan */ 38476987Sbrendan if (zio->io_error != 0) { 38485450Sbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 38496987Sbrendan } else { 38506987Sbrendan zio->io_error = EIO; 38516987Sbrendan } 38525450Sbrendan if (!equal) 38535450Sbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 38545450Sbrendan 38557754SJeff.Bonwick@Sun.COM /* 38567754SJeff.Bonwick@Sun.COM * If there's no waiter, issue an async i/o to the primary 38577754SJeff.Bonwick@Sun.COM * storage now. If there *is* a waiter, the caller must 38587754SJeff.Bonwick@Sun.COM * issue the i/o in a context where it's OK to block. 38597754SJeff.Bonwick@Sun.COM */ 38607754SJeff.Bonwick@Sun.COM if (zio->io_waiter == NULL) 38617754SJeff.Bonwick@Sun.COM zio_nowait(zio_read(zio->io_parent, 38627754SJeff.Bonwick@Sun.COM cb->l2rcb_spa, &cb->l2rcb_bp, 38637754SJeff.Bonwick@Sun.COM buf->b_data, zio->io_size, arc_read_done, buf, 38647754SJeff.Bonwick@Sun.COM zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 38655450Sbrendan } 38665450Sbrendan 38675450Sbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 38685450Sbrendan } 38695450Sbrendan 38705450Sbrendan /* 38715450Sbrendan * This is the list priority from which the L2ARC will search for pages to 38725450Sbrendan * cache. This is used within loops (0..3) to cycle through lists in the 38735450Sbrendan * desired order. This order can have a significant effect on cache 38745450Sbrendan * performance. 38755450Sbrendan * 38765450Sbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 38775450Sbrendan * the data lists. This function returns a locked list, and also returns 38785450Sbrendan * the lock pointer. 38795450Sbrendan */ 38805450Sbrendan static list_t * 38815450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock) 38825450Sbrendan { 38835450Sbrendan list_t *list; 38845450Sbrendan 38855450Sbrendan ASSERT(list_num >= 0 && list_num <= 3); 38865450Sbrendan 38875450Sbrendan switch (list_num) { 38885450Sbrendan case 0: 38895450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 38905450Sbrendan *lock = &arc_mfu->arcs_mtx; 38915450Sbrendan break; 38925450Sbrendan case 1: 38935450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 38945450Sbrendan *lock = &arc_mru->arcs_mtx; 38955450Sbrendan break; 38965450Sbrendan case 2: 38975450Sbrendan list = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 38985450Sbrendan *lock = &arc_mfu->arcs_mtx; 38995450Sbrendan break; 39005450Sbrendan case 3: 39015450Sbrendan list = &arc_mru->arcs_list[ARC_BUFC_DATA]; 39025450Sbrendan *lock = &arc_mru->arcs_mtx; 39035450Sbrendan break; 39045450Sbrendan } 39055450Sbrendan 39065450Sbrendan ASSERT(!(MUTEX_HELD(*lock))); 39075450Sbrendan mutex_enter(*lock); 39085450Sbrendan return (list); 39095450Sbrendan } 39105450Sbrendan 39115450Sbrendan /* 39125450Sbrendan * Evict buffers from the device write hand to the distance specified in 39135450Sbrendan * bytes. This distance may span populated buffers, it may span nothing. 39145450Sbrendan * This is clearing a region on the L2ARC device ready for writing. 39155450Sbrendan * If the 'all' boolean is set, every buffer is evicted. 39165450Sbrendan */ 39175450Sbrendan static void 39185450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 39195450Sbrendan { 39205450Sbrendan list_t *buflist; 39215450Sbrendan l2arc_buf_hdr_t *abl2; 39225450Sbrendan arc_buf_hdr_t *ab, *ab_prev; 39235450Sbrendan kmutex_t *hash_lock; 39245450Sbrendan uint64_t taddr; 39255450Sbrendan 39265450Sbrendan buflist = dev->l2ad_buflist; 39275450Sbrendan 39285450Sbrendan if (buflist == NULL) 39295450Sbrendan return; 39305450Sbrendan 39315450Sbrendan if (!all && dev->l2ad_first) { 39325450Sbrendan /* 39335450Sbrendan * This is the first sweep through the device. There is 39345450Sbrendan * nothing to evict. 39355450Sbrendan */ 39365450Sbrendan return; 39375450Sbrendan } 39385450Sbrendan 39396987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 39405450Sbrendan /* 39415450Sbrendan * When nearing the end of the device, evict to the end 39425450Sbrendan * before the device write hand jumps to the start. 39435450Sbrendan */ 39445450Sbrendan taddr = dev->l2ad_end; 39455450Sbrendan } else { 39465450Sbrendan taddr = dev->l2ad_hand + distance; 39475450Sbrendan } 39485450Sbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 39495450Sbrendan uint64_t, taddr, boolean_t, all); 39505450Sbrendan 39515450Sbrendan top: 39525450Sbrendan mutex_enter(&l2arc_buflist_mtx); 39535450Sbrendan for (ab = list_tail(buflist); ab; ab = ab_prev) { 39545450Sbrendan ab_prev = list_prev(buflist, ab); 39555450Sbrendan 39565450Sbrendan hash_lock = HDR_LOCK(ab); 39575450Sbrendan if (!mutex_tryenter(hash_lock)) { 39585450Sbrendan /* 39595450Sbrendan * Missed the hash lock. Retry. 39605450Sbrendan */ 39615450Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 39625450Sbrendan mutex_exit(&l2arc_buflist_mtx); 39635450Sbrendan mutex_enter(hash_lock); 39645450Sbrendan mutex_exit(hash_lock); 39655450Sbrendan goto top; 39665450Sbrendan } 39675450Sbrendan 39685450Sbrendan if (HDR_L2_WRITE_HEAD(ab)) { 39695450Sbrendan /* 39705450Sbrendan * We hit a write head node. Leave it for 39715450Sbrendan * l2arc_write_done(). 39725450Sbrendan */ 39735450Sbrendan list_remove(buflist, ab); 39745450Sbrendan mutex_exit(hash_lock); 39755450Sbrendan continue; 39765450Sbrendan } 39775450Sbrendan 39785450Sbrendan if (!all && ab->b_l2hdr != NULL && 39795450Sbrendan (ab->b_l2hdr->b_daddr > taddr || 39805450Sbrendan ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 39815450Sbrendan /* 39825450Sbrendan * We've evicted to the target address, 39835450Sbrendan * or the end of the device. 39845450Sbrendan */ 39855450Sbrendan mutex_exit(hash_lock); 39865450Sbrendan break; 39875450Sbrendan } 39885450Sbrendan 39895450Sbrendan if (HDR_FREE_IN_PROGRESS(ab)) { 39905450Sbrendan /* 39915450Sbrendan * Already on the path to destruction. 39925450Sbrendan */ 39935450Sbrendan mutex_exit(hash_lock); 39945450Sbrendan continue; 39955450Sbrendan } 39965450Sbrendan 39975450Sbrendan if (ab->b_state == arc_l2c_only) { 39985450Sbrendan ASSERT(!HDR_L2_READING(ab)); 39995450Sbrendan /* 40005450Sbrendan * This doesn't exist in the ARC. Destroy. 40015450Sbrendan * arc_hdr_destroy() will call list_remove() 40025450Sbrendan * and decrement arcstat_l2_size. 40035450Sbrendan */ 40045450Sbrendan arc_change_state(arc_anon, ab, hash_lock); 40055450Sbrendan arc_hdr_destroy(ab); 40065450Sbrendan } else { 40075450Sbrendan /* 40086987Sbrendan * Invalidate issued or about to be issued 40096987Sbrendan * reads, since we may be about to write 40106987Sbrendan * over this location. 40116987Sbrendan */ 40126987Sbrendan if (HDR_L2_READING(ab)) { 40136987Sbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 40146987Sbrendan ab->b_flags |= ARC_L2_EVICTED; 40156987Sbrendan } 40166987Sbrendan 40176987Sbrendan /* 40185450Sbrendan * Tell ARC this no longer exists in L2ARC. 40195450Sbrendan */ 40205450Sbrendan if (ab->b_l2hdr != NULL) { 40215450Sbrendan abl2 = ab->b_l2hdr; 40225450Sbrendan ab->b_l2hdr = NULL; 40235450Sbrendan kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 40245450Sbrendan ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 40255450Sbrendan } 40265450Sbrendan list_remove(buflist, ab); 40275450Sbrendan 40285450Sbrendan /* 40295450Sbrendan * This may have been leftover after a 40305450Sbrendan * failed write. 40315450Sbrendan */ 40325450Sbrendan ab->b_flags &= ~ARC_L2_WRITING; 40335450Sbrendan } 40345450Sbrendan mutex_exit(hash_lock); 40355450Sbrendan } 40365450Sbrendan mutex_exit(&l2arc_buflist_mtx); 40375450Sbrendan 40385450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, -(taddr - dev->l2ad_evict)); 40395450Sbrendan dev->l2ad_evict = taddr; 40405450Sbrendan } 40415450Sbrendan 40425450Sbrendan /* 40435450Sbrendan * Find and write ARC buffers to the L2ARC device. 40445450Sbrendan * 40455450Sbrendan * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 40465450Sbrendan * for reading until they have completed writing. 40475450Sbrendan */ 40485450Sbrendan static void 40496987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 40505450Sbrendan { 40515450Sbrendan arc_buf_hdr_t *ab, *ab_prev, *head; 40525450Sbrendan l2arc_buf_hdr_t *hdrl2; 40535450Sbrendan list_t *list; 40546987Sbrendan uint64_t passed_sz, write_sz, buf_sz, headroom; 40555450Sbrendan void *buf_data; 40565450Sbrendan kmutex_t *hash_lock, *list_lock; 40575450Sbrendan boolean_t have_lock, full; 40585450Sbrendan l2arc_write_callback_t *cb; 40595450Sbrendan zio_t *pio, *wzio; 40605450Sbrendan 40615450Sbrendan ASSERT(dev->l2ad_vdev != NULL); 40625450Sbrendan 40635450Sbrendan pio = NULL; 40645450Sbrendan write_sz = 0; 40655450Sbrendan full = B_FALSE; 40666245Smaybee head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 40675450Sbrendan head->b_flags |= ARC_L2_WRITE_HEAD; 40685450Sbrendan 40695450Sbrendan /* 40705450Sbrendan * Copy buffers for L2ARC writing. 40715450Sbrendan */ 40725450Sbrendan mutex_enter(&l2arc_buflist_mtx); 40735450Sbrendan for (int try = 0; try <= 3; try++) { 40745450Sbrendan list = l2arc_list_locked(try, &list_lock); 40755450Sbrendan passed_sz = 0; 40765450Sbrendan 40776987Sbrendan /* 40786987Sbrendan * L2ARC fast warmup. 40796987Sbrendan * 40806987Sbrendan * Until the ARC is warm and starts to evict, read from the 40816987Sbrendan * head of the ARC lists rather than the tail. 40826987Sbrendan */ 40836987Sbrendan headroom = target_sz * l2arc_headroom; 40846987Sbrendan if (arc_warm == B_FALSE) 40856987Sbrendan ab = list_head(list); 40866987Sbrendan else 40876987Sbrendan ab = list_tail(list); 40886987Sbrendan 40896987Sbrendan for (; ab; ab = ab_prev) { 40906987Sbrendan if (arc_warm == B_FALSE) 40916987Sbrendan ab_prev = list_next(list, ab); 40926987Sbrendan else 40936987Sbrendan ab_prev = list_prev(list, ab); 40945450Sbrendan 40955450Sbrendan hash_lock = HDR_LOCK(ab); 40965450Sbrendan have_lock = MUTEX_HELD(hash_lock); 40975450Sbrendan if (!have_lock && !mutex_tryenter(hash_lock)) { 40985450Sbrendan /* 40995450Sbrendan * Skip this buffer rather than waiting. 41005450Sbrendan */ 41015450Sbrendan continue; 41025450Sbrendan } 41035450Sbrendan 41045450Sbrendan passed_sz += ab->b_size; 41055450Sbrendan if (passed_sz > headroom) { 41065450Sbrendan /* 41075450Sbrendan * Searched too far. 41085450Sbrendan */ 41095450Sbrendan mutex_exit(hash_lock); 41105450Sbrendan break; 41115450Sbrendan } 41125450Sbrendan 41135450Sbrendan if (ab->b_spa != spa) { 41145450Sbrendan mutex_exit(hash_lock); 41155450Sbrendan continue; 41165450Sbrendan } 41175450Sbrendan 41185450Sbrendan if (ab->b_l2hdr != NULL) { 41195450Sbrendan /* 41205450Sbrendan * Already in L2ARC. 41215450Sbrendan */ 41225450Sbrendan mutex_exit(hash_lock); 41235450Sbrendan continue; 41245450Sbrendan } 41255450Sbrendan 41267237Sek110237 if (HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab)) { 41275450Sbrendan mutex_exit(hash_lock); 41285450Sbrendan continue; 41295450Sbrendan } 41305450Sbrendan 41315450Sbrendan if ((write_sz + ab->b_size) > target_sz) { 41325450Sbrendan full = B_TRUE; 41335450Sbrendan mutex_exit(hash_lock); 41345450Sbrendan break; 41355450Sbrendan } 41365450Sbrendan 41375450Sbrendan if (ab->b_buf == NULL) { 41385450Sbrendan DTRACE_PROBE1(l2arc__buf__null, void *, ab); 41395450Sbrendan mutex_exit(hash_lock); 41405450Sbrendan continue; 41415450Sbrendan } 41425450Sbrendan 41435450Sbrendan if (pio == NULL) { 41445450Sbrendan /* 41455450Sbrendan * Insert a dummy header on the buflist so 41465450Sbrendan * l2arc_write_done() can find where the 41475450Sbrendan * write buffers begin without searching. 41485450Sbrendan */ 41495450Sbrendan list_insert_head(dev->l2ad_buflist, head); 41505450Sbrendan 41515450Sbrendan cb = kmem_alloc( 41525450Sbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 41535450Sbrendan cb->l2wcb_dev = dev; 41545450Sbrendan cb->l2wcb_head = head; 41555450Sbrendan pio = zio_root(spa, l2arc_write_done, cb, 41565450Sbrendan ZIO_FLAG_CANFAIL); 41575450Sbrendan } 41585450Sbrendan 41595450Sbrendan /* 41605450Sbrendan * Create and add a new L2ARC header. 41615450Sbrendan */ 41625450Sbrendan hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 41635450Sbrendan hdrl2->b_dev = dev; 41645450Sbrendan hdrl2->b_daddr = dev->l2ad_hand; 41655450Sbrendan 41665450Sbrendan ab->b_flags |= ARC_L2_WRITING; 41675450Sbrendan ab->b_l2hdr = hdrl2; 41685450Sbrendan list_insert_head(dev->l2ad_buflist, ab); 41695450Sbrendan buf_data = ab->b_buf->b_data; 41705450Sbrendan buf_sz = ab->b_size; 41715450Sbrendan 41725450Sbrendan /* 41735450Sbrendan * Compute and store the buffer cksum before 41745450Sbrendan * writing. On debug the cksum is verified first. 41755450Sbrendan */ 41765450Sbrendan arc_cksum_verify(ab->b_buf); 41775450Sbrendan arc_cksum_compute(ab->b_buf, B_TRUE); 41785450Sbrendan 41795450Sbrendan mutex_exit(hash_lock); 41805450Sbrendan 41815450Sbrendan wzio = zio_write_phys(pio, dev->l2ad_vdev, 41825450Sbrendan dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 41835450Sbrendan NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 41845450Sbrendan ZIO_FLAG_CANFAIL, B_FALSE); 41855450Sbrendan 41865450Sbrendan DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 41875450Sbrendan zio_t *, wzio); 41885450Sbrendan (void) zio_nowait(wzio); 41895450Sbrendan 41907754SJeff.Bonwick@Sun.COM /* 41917754SJeff.Bonwick@Sun.COM * Keep the clock hand suitably device-aligned. 41927754SJeff.Bonwick@Sun.COM */ 41937754SJeff.Bonwick@Sun.COM buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 41947754SJeff.Bonwick@Sun.COM 41955450Sbrendan write_sz += buf_sz; 41965450Sbrendan dev->l2ad_hand += buf_sz; 41975450Sbrendan } 41985450Sbrendan 41995450Sbrendan mutex_exit(list_lock); 42005450Sbrendan 42015450Sbrendan if (full == B_TRUE) 42025450Sbrendan break; 42035450Sbrendan } 42045450Sbrendan mutex_exit(&l2arc_buflist_mtx); 42055450Sbrendan 42065450Sbrendan if (pio == NULL) { 42075450Sbrendan ASSERT3U(write_sz, ==, 0); 42085450Sbrendan kmem_cache_free(hdr_cache, head); 42095450Sbrendan return; 42105450Sbrendan } 42115450Sbrendan 42125450Sbrendan ASSERT3U(write_sz, <=, target_sz); 42135450Sbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 42145450Sbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 42155450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, write_sz); 42165450Sbrendan 42175450Sbrendan /* 42185450Sbrendan * Bump device hand to the device start if it is approaching the end. 42195450Sbrendan * l2arc_evict() will already have evicted ahead for this case. 42205450Sbrendan */ 42216987Sbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 42225450Sbrendan spa_l2cache_space_update(dev->l2ad_vdev, 0, 42235450Sbrendan dev->l2ad_end - dev->l2ad_hand); 42245450Sbrendan dev->l2ad_hand = dev->l2ad_start; 42255450Sbrendan dev->l2ad_evict = dev->l2ad_start; 42265450Sbrendan dev->l2ad_first = B_FALSE; 42275450Sbrendan } 42285450Sbrendan 42295450Sbrendan (void) zio_wait(pio); 42305450Sbrendan } 42315450Sbrendan 42325450Sbrendan /* 42335450Sbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 42345450Sbrendan * heart of the L2ARC. 42355450Sbrendan */ 42365450Sbrendan static void 42375450Sbrendan l2arc_feed_thread(void) 42385450Sbrendan { 42395450Sbrendan callb_cpr_t cpr; 42405450Sbrendan l2arc_dev_t *dev; 42415450Sbrendan spa_t *spa; 42426987Sbrendan uint64_t size; 42435450Sbrendan 42445450Sbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 42455450Sbrendan 42465450Sbrendan mutex_enter(&l2arc_feed_thr_lock); 42475450Sbrendan 42485450Sbrendan while (l2arc_thread_exit == 0) { 42495450Sbrendan /* 42506987Sbrendan * Pause for l2arc_feed_secs seconds between writes. 42515450Sbrendan */ 42525450Sbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 42536987Sbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 42546987Sbrendan lbolt + (hz * l2arc_feed_secs)); 42556987Sbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 42566987Sbrendan 42576987Sbrendan /* 42586987Sbrendan * Quick check for L2ARC devices. 42596987Sbrendan */ 42606987Sbrendan mutex_enter(&l2arc_dev_mtx); 42616987Sbrendan if (l2arc_ndev == 0) { 42626987Sbrendan mutex_exit(&l2arc_dev_mtx); 42636987Sbrendan continue; 42645450Sbrendan } 42656987Sbrendan mutex_exit(&l2arc_dev_mtx); 42666643Seschrock 42675450Sbrendan /* 42686643Seschrock * This selects the next l2arc device to write to, and in 42696643Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 42706987Sbrendan * will return NULL if there are now no l2arc devices or if 42716987Sbrendan * they are all faulted. 42726987Sbrendan * 42736987Sbrendan * If a device is returned, its spa's config lock is also 42746987Sbrendan * held to prevent device removal. l2arc_dev_get_next() 42756987Sbrendan * will grab and release l2arc_dev_mtx. 42765450Sbrendan */ 42776987Sbrendan if ((dev = l2arc_dev_get_next()) == NULL) 42785450Sbrendan continue; 42796987Sbrendan 42806987Sbrendan spa = dev->l2ad_spa; 42816987Sbrendan ASSERT(spa != NULL); 42825450Sbrendan 42835450Sbrendan /* 42845450Sbrendan * Avoid contributing to memory pressure. 42855450Sbrendan */ 42865450Sbrendan if (arc_reclaim_needed()) { 42875450Sbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 42887754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 42895450Sbrendan continue; 42905450Sbrendan } 42915450Sbrendan 42925450Sbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 42935450Sbrendan 42946987Sbrendan size = dev->l2ad_write; 42956987Sbrendan if (arc_warm == B_FALSE) 42966987Sbrendan size += dev->l2ad_boost; 42976987Sbrendan 42985450Sbrendan /* 42995450Sbrendan * Evict L2ARC buffers that will be overwritten. 43005450Sbrendan */ 43016987Sbrendan l2arc_evict(dev, size, B_FALSE); 43025450Sbrendan 43035450Sbrendan /* 43045450Sbrendan * Write ARC buffers. 43055450Sbrendan */ 43066987Sbrendan l2arc_write_buffers(spa, dev, size); 43077754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_L2ARC, dev); 43085450Sbrendan } 43095450Sbrendan 43105450Sbrendan l2arc_thread_exit = 0; 43115450Sbrendan cv_broadcast(&l2arc_feed_thr_cv); 43125450Sbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 43135450Sbrendan thread_exit(); 43145450Sbrendan } 43155450Sbrendan 43166643Seschrock boolean_t 43176643Seschrock l2arc_vdev_present(vdev_t *vd) 43186643Seschrock { 43196643Seschrock l2arc_dev_t *dev; 43206643Seschrock 43216643Seschrock mutex_enter(&l2arc_dev_mtx); 43226643Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 43236643Seschrock dev = list_next(l2arc_dev_list, dev)) { 43246643Seschrock if (dev->l2ad_vdev == vd) 43256643Seschrock break; 43266643Seschrock } 43276643Seschrock mutex_exit(&l2arc_dev_mtx); 43286643Seschrock 43296643Seschrock return (dev != NULL); 43306643Seschrock } 43316643Seschrock 43325450Sbrendan /* 43335450Sbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 43345450Sbrendan * validated the vdev and opened it. 43355450Sbrendan */ 43365450Sbrendan void 43375450Sbrendan l2arc_add_vdev(spa_t *spa, vdev_t *vd, uint64_t start, uint64_t end) 43385450Sbrendan { 43395450Sbrendan l2arc_dev_t *adddev; 43405450Sbrendan 43416643Seschrock ASSERT(!l2arc_vdev_present(vd)); 43426643Seschrock 43435450Sbrendan /* 43445450Sbrendan * Create a new l2arc device entry. 43455450Sbrendan */ 43465450Sbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 43475450Sbrendan adddev->l2ad_spa = spa; 43485450Sbrendan adddev->l2ad_vdev = vd; 43495450Sbrendan adddev->l2ad_write = l2arc_write_max; 43506987Sbrendan adddev->l2ad_boost = l2arc_write_boost; 43515450Sbrendan adddev->l2ad_start = start; 43525450Sbrendan adddev->l2ad_end = end; 43535450Sbrendan adddev->l2ad_hand = adddev->l2ad_start; 43545450Sbrendan adddev->l2ad_evict = adddev->l2ad_start; 43555450Sbrendan adddev->l2ad_first = B_TRUE; 43565450Sbrendan ASSERT3U(adddev->l2ad_write, >, 0); 43575450Sbrendan 43585450Sbrendan /* 43595450Sbrendan * This is a list of all ARC buffers that are still valid on the 43605450Sbrendan * device. 43615450Sbrendan */ 43625450Sbrendan adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 43635450Sbrendan list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 43645450Sbrendan offsetof(arc_buf_hdr_t, b_l2node)); 43655450Sbrendan 43665450Sbrendan spa_l2cache_space_update(vd, adddev->l2ad_end - adddev->l2ad_hand, 0); 43675450Sbrendan 43685450Sbrendan /* 43695450Sbrendan * Add device to global list 43705450Sbrendan */ 43715450Sbrendan mutex_enter(&l2arc_dev_mtx); 43725450Sbrendan list_insert_head(l2arc_dev_list, adddev); 43735450Sbrendan atomic_inc_64(&l2arc_ndev); 43745450Sbrendan mutex_exit(&l2arc_dev_mtx); 43755450Sbrendan } 43765450Sbrendan 43775450Sbrendan /* 43785450Sbrendan * Remove a vdev from the L2ARC. 43795450Sbrendan */ 43805450Sbrendan void 43815450Sbrendan l2arc_remove_vdev(vdev_t *vd) 43825450Sbrendan { 43835450Sbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 43845450Sbrendan 43855450Sbrendan /* 43865450Sbrendan * Find the device by vdev 43875450Sbrendan */ 43885450Sbrendan mutex_enter(&l2arc_dev_mtx); 43895450Sbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 43905450Sbrendan nextdev = list_next(l2arc_dev_list, dev); 43915450Sbrendan if (vd == dev->l2ad_vdev) { 43925450Sbrendan remdev = dev; 43935450Sbrendan break; 43945450Sbrendan } 43955450Sbrendan } 43965450Sbrendan ASSERT(remdev != NULL); 43975450Sbrendan 43985450Sbrendan /* 43995450Sbrendan * Remove device from global list 44005450Sbrendan */ 44015450Sbrendan list_remove(l2arc_dev_list, remdev); 44025450Sbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 44036987Sbrendan atomic_dec_64(&l2arc_ndev); 44046987Sbrendan mutex_exit(&l2arc_dev_mtx); 44055450Sbrendan 44065450Sbrendan /* 44075450Sbrendan * Clear all buflists and ARC references. L2ARC device flush. 44085450Sbrendan */ 44095450Sbrendan l2arc_evict(remdev, 0, B_TRUE); 44105450Sbrendan list_destroy(remdev->l2ad_buflist); 44115450Sbrendan kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 44125450Sbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 44135450Sbrendan } 44145450Sbrendan 44155450Sbrendan void 44167754SJeff.Bonwick@Sun.COM l2arc_init(void) 44175450Sbrendan { 44185450Sbrendan l2arc_thread_exit = 0; 44195450Sbrendan l2arc_ndev = 0; 44205450Sbrendan l2arc_writes_sent = 0; 44215450Sbrendan l2arc_writes_done = 0; 44225450Sbrendan 44235450Sbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 44245450Sbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 44255450Sbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 44265450Sbrendan mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 44275450Sbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 44285450Sbrendan 44295450Sbrendan l2arc_dev_list = &L2ARC_dev_list; 44305450Sbrendan l2arc_free_on_write = &L2ARC_free_on_write; 44315450Sbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 44325450Sbrendan offsetof(l2arc_dev_t, l2ad_node)); 44335450Sbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 44345450Sbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 44355450Sbrendan } 44365450Sbrendan 44375450Sbrendan void 44387754SJeff.Bonwick@Sun.COM l2arc_fini(void) 44395450Sbrendan { 44406987Sbrendan /* 44416987Sbrendan * This is called from dmu_fini(), which is called from spa_fini(); 44426987Sbrendan * Because of this, we can assume that all l2arc devices have 44436987Sbrendan * already been removed when the pools themselves were removed. 44446987Sbrendan */ 44456987Sbrendan 44466987Sbrendan l2arc_do_free_on_write(); 44476987Sbrendan 44485450Sbrendan mutex_destroy(&l2arc_feed_thr_lock); 44495450Sbrendan cv_destroy(&l2arc_feed_thr_cv); 44505450Sbrendan mutex_destroy(&l2arc_dev_mtx); 44515450Sbrendan mutex_destroy(&l2arc_buflist_mtx); 44525450Sbrendan mutex_destroy(&l2arc_free_on_write_mtx); 44535450Sbrendan 44545450Sbrendan list_destroy(l2arc_dev_list); 44555450Sbrendan list_destroy(l2arc_free_on_write); 44565450Sbrendan } 44577754SJeff.Bonwick@Sun.COM 44587754SJeff.Bonwick@Sun.COM void 44597754SJeff.Bonwick@Sun.COM l2arc_start(void) 44607754SJeff.Bonwick@Sun.COM { 4461*8241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 44627754SJeff.Bonwick@Sun.COM return; 44637754SJeff.Bonwick@Sun.COM 44647754SJeff.Bonwick@Sun.COM (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 44657754SJeff.Bonwick@Sun.COM TS_RUN, minclsyspri); 44667754SJeff.Bonwick@Sun.COM } 44677754SJeff.Bonwick@Sun.COM 44687754SJeff.Bonwick@Sun.COM void 44697754SJeff.Bonwick@Sun.COM l2arc_stop(void) 44707754SJeff.Bonwick@Sun.COM { 4471*8241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 44727754SJeff.Bonwick@Sun.COM return; 44737754SJeff.Bonwick@Sun.COM 44747754SJeff.Bonwick@Sun.COM mutex_enter(&l2arc_feed_thr_lock); 44757754SJeff.Bonwick@Sun.COM cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 44767754SJeff.Bonwick@Sun.COM l2arc_thread_exit = 1; 44777754SJeff.Bonwick@Sun.COM while (l2arc_thread_exit != 0) 44787754SJeff.Bonwick@Sun.COM cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 44797754SJeff.Bonwick@Sun.COM mutex_exit(&l2arc_feed_thr_lock); 44807754SJeff.Bonwick@Sun.COM } 4481