1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5033f9833Sek * Common Development and Distribution License (the "License"). 6033f9833Sek * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 2336a64e62STim Kordas * Copyright (c) 2018, Joyent, Inc. 2410fbdecbSMatthew Ahrens * Copyright (c) 2011, 2017 by Delphix. All rights reserved. 2571cb1b74SSaso Kiselkov * Copyright (c) 2014 by Saso Kiselkov. All rights reserved. 2601a059eeSRoman Strashkin * Copyright 2017 Nexenta Systems, Inc. All rights reserved. 27fa9e4066Sahrens */ 28fa9e4066Sahrens 29fa9e4066Sahrens /* 3044cb6abcSbmc * DVA-based Adjustable Replacement Cache 31fa9e4066Sahrens * 32ea8dc4b6Seschrock * While much of the theory of operation used here is 33ea8dc4b6Seschrock * based on the self-tuning, low overhead replacement cache 34fa9e4066Sahrens * presented by Megiddo and Modha at FAST 2003, there are some 35fa9e4066Sahrens * significant differences: 36fa9e4066Sahrens * 37fa9e4066Sahrens * 1. The Megiddo and Modha model assumes any page is evictable. 38fa9e4066Sahrens * Pages in its cache cannot be "locked" into memory. This makes 39fa9e4066Sahrens * the eviction algorithm simple: evict the last page in the list. 40fa9e4066Sahrens * This also make the performance characteristics easy to reason 41fa9e4066Sahrens * about. Our cache is not so simple. At any given moment, some 42fa9e4066Sahrens * subset of the blocks in the cache are un-evictable because we 43fa9e4066Sahrens * have handed out a reference to them. Blocks are only evictable 44fa9e4066Sahrens * when there are no external references active. This makes 45fa9e4066Sahrens * eviction far more problematic: we choose to evict the evictable 46fa9e4066Sahrens * blocks that are the "lowest" in the list. 47fa9e4066Sahrens * 48fa9e4066Sahrens * There are times when it is not possible to evict the requested 49fa9e4066Sahrens * space. In these circumstances we are unable to adjust the cache 50fa9e4066Sahrens * size. To prevent the cache growing unbounded at these times we 51fa94a07fSbrendan * implement a "cache throttle" that slows the flow of new data 52fa94a07fSbrendan * into the cache until we can make space available. 53fa9e4066Sahrens * 54fa9e4066Sahrens * 2. The Megiddo and Modha model assumes a fixed cache size. 55fa9e4066Sahrens * Pages are evicted when the cache is full and there is a cache 56fa9e4066Sahrens * miss. Our model has a variable sized cache. It grows with 57fa94a07fSbrendan * high use, but also tries to react to memory pressure from the 58fa9e4066Sahrens * operating system: decreasing its size when system memory is 59fa9e4066Sahrens * tight. 60fa9e4066Sahrens * 61fa9e4066Sahrens * 3. The Megiddo and Modha model assumes a fixed page size. All 62f7170741SWill Andrews * elements of the cache are therefore exactly the same size. So 63fa9e4066Sahrens * when adjusting the cache size following a cache miss, its simply 64fa9e4066Sahrens * a matter of choosing a single page to evict. In our model, we 65fa9e4066Sahrens * have variable sized cache blocks (rangeing from 512 bytes to 66f7170741SWill Andrews * 128K bytes). We therefore choose a set of blocks to evict to make 67fa9e4066Sahrens * space for a cache miss that approximates as closely as possible 68fa9e4066Sahrens * the space used by the new block. 69fa9e4066Sahrens * 70fa9e4066Sahrens * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 71fa9e4066Sahrens * by N. Megiddo & D. Modha, FAST 2003 72fa9e4066Sahrens */ 73fa9e4066Sahrens 74fa9e4066Sahrens /* 75fa9e4066Sahrens * The locking model: 76fa9e4066Sahrens * 77fa9e4066Sahrens * A new reference to a cache buffer can be obtained in two 78fa9e4066Sahrens * ways: 1) via a hash table lookup using the DVA as a key, 79fa94a07fSbrendan * or 2) via one of the ARC lists. The arc_read() interface 805602294fSDan Kimmel * uses method 1, while the internal ARC algorithms for 81f7170741SWill Andrews * adjusting the cache use method 2. We therefore provide two 82fa9e4066Sahrens * types of locks: 1) the hash table lock array, and 2) the 835602294fSDan Kimmel * ARC list locks. 84fa9e4066Sahrens * 85fc98fea5SBart Coddens * Buffers do not have their own mutexes, rather they rely on the 86fc98fea5SBart Coddens * hash table mutexes for the bulk of their protection (i.e. most 87fc98fea5SBart Coddens * fields in the arc_buf_hdr_t are protected by these mutexes). 88fa9e4066Sahrens * 89fa9e4066Sahrens * buf_hash_find() returns the appropriate mutex (held) when it 90fa9e4066Sahrens * locates the requested buffer in the hash table. It returns 91fa9e4066Sahrens * NULL for the mutex if the buffer was not in the table. 92fa9e4066Sahrens * 93fa9e4066Sahrens * buf_hash_remove() expects the appropriate hash mutex to be 94fa9e4066Sahrens * already held before it is invoked. 95fa9e4066Sahrens * 965602294fSDan Kimmel * Each ARC state also has a mutex which is used to protect the 97fa9e4066Sahrens * buffer list associated with the state. When attempting to 985602294fSDan Kimmel * obtain a hash table lock while holding an ARC list lock you 99fa9e4066Sahrens * must use: mutex_tryenter() to avoid deadlock. Also note that 10044eda4d7Smaybee * the active state mutex must be held before the ghost state mutex. 101fa9e4066Sahrens * 102fa9e4066Sahrens * Note that the majority of the performance stats are manipulated 103fa9e4066Sahrens * with atomic operations. 104fa94a07fSbrendan * 10589c86e32SChris Williamson * The L2ARC uses the l2ad_mtx on each vdev for the following: 106fa94a07fSbrendan * 107fa94a07fSbrendan * - L2ARC buflist creation 108fa94a07fSbrendan * - L2ARC buflist eviction 109fa94a07fSbrendan * - L2ARC write completion, which walks L2ARC buflists 110fa94a07fSbrendan * - ARC header destruction, as it removes from L2ARC buflists 111fa94a07fSbrendan * - ARC header release, as it removes from L2ARC buflists 112fa9e4066Sahrens */ 113fa9e4066Sahrens 114dcbf3bd6SGeorge Wilson /* 115dcbf3bd6SGeorge Wilson * ARC operation: 116dcbf3bd6SGeorge Wilson * 117dcbf3bd6SGeorge Wilson * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure. 118dcbf3bd6SGeorge Wilson * This structure can point either to a block that is still in the cache or to 119dcbf3bd6SGeorge Wilson * one that is only accessible in an L2 ARC device, or it can provide 120dcbf3bd6SGeorge Wilson * information about a block that was recently evicted. If a block is 121dcbf3bd6SGeorge Wilson * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough 122dcbf3bd6SGeorge Wilson * information to retrieve it from the L2ARC device. This information is 123dcbf3bd6SGeorge Wilson * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block 124dcbf3bd6SGeorge Wilson * that is in this state cannot access the data directly. 125dcbf3bd6SGeorge Wilson * 126dcbf3bd6SGeorge Wilson * Blocks that are actively being referenced or have not been evicted 127dcbf3bd6SGeorge Wilson * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within 128dcbf3bd6SGeorge Wilson * the arc_buf_hdr_t that will point to the data block in memory. A block can 129dcbf3bd6SGeorge Wilson * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC 1305602294fSDan Kimmel * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and 131770499e1SDan Kimmel * also in the arc_buf_hdr_t's private physical data block pointer (b_pabd). 1325602294fSDan Kimmel * 1335602294fSDan Kimmel * The L1ARC's data pointer may or may not be uncompressed. The ARC has the 134770499e1SDan Kimmel * ability to store the physical data (b_pabd) associated with the DVA of the 135770499e1SDan Kimmel * arc_buf_hdr_t. Since the b_pabd is a copy of the on-disk physical block, 1365602294fSDan Kimmel * it will match its on-disk compression characteristics. This behavior can be 1375602294fSDan Kimmel * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the 138770499e1SDan Kimmel * compressed ARC functionality is disabled, the b_pabd will point to an 1395602294fSDan Kimmel * uncompressed version of the on-disk data. 1405602294fSDan Kimmel * 1415602294fSDan Kimmel * Data in the L1ARC is not accessed by consumers of the ARC directly. Each 1425602294fSDan Kimmel * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it. 1435602294fSDan Kimmel * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC 1445602294fSDan Kimmel * consumer. The ARC will provide references to this data and will keep it 1455602294fSDan Kimmel * cached until it is no longer in use. The ARC caches only the L1ARC's physical 1465602294fSDan Kimmel * data block and will evict any arc_buf_t that is no longer referenced. The 1475602294fSDan Kimmel * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the 148dcbf3bd6SGeorge Wilson * "overhead_size" kstat. 149dcbf3bd6SGeorge Wilson * 1505602294fSDan Kimmel * Depending on the consumer, an arc_buf_t can be requested in uncompressed or 1515602294fSDan Kimmel * compressed form. The typical case is that consumers will want uncompressed 1525602294fSDan Kimmel * data, and when that happens a new data buffer is allocated where the data is 1535602294fSDan Kimmel * decompressed for them to use. Currently the only consumer who wants 1545602294fSDan Kimmel * compressed arc_buf_t's is "zfs send", when it streams data exactly as it 1555602294fSDan Kimmel * exists on disk. When this happens, the arc_buf_t's data buffer is shared 1565602294fSDan Kimmel * with the arc_buf_hdr_t. 157dcbf3bd6SGeorge Wilson * 1585602294fSDan Kimmel * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The 1595602294fSDan Kimmel * first one is owned by a compressed send consumer (and therefore references 1605602294fSDan Kimmel * the same compressed data buffer as the arc_buf_hdr_t) and the second could be 1615602294fSDan Kimmel * used by any other consumer (and has its own uncompressed copy of the data 1625602294fSDan Kimmel * buffer). 163dcbf3bd6SGeorge Wilson * 1645602294fSDan Kimmel * arc_buf_hdr_t 1655602294fSDan Kimmel * +-----------+ 1665602294fSDan Kimmel * | fields | 1675602294fSDan Kimmel * | common to | 1685602294fSDan Kimmel * | L1- and | 1695602294fSDan Kimmel * | L2ARC | 1705602294fSDan Kimmel * +-----------+ 1715602294fSDan Kimmel * | l2arc_buf_hdr_t 1725602294fSDan Kimmel * | | 1735602294fSDan Kimmel * +-----------+ 1745602294fSDan Kimmel * | l1arc_buf_hdr_t 1755602294fSDan Kimmel * | | arc_buf_t 1765602294fSDan Kimmel * | b_buf +------------>+-----------+ arc_buf_t 177770499e1SDan Kimmel * | b_pabd +-+ |b_next +---->+-----------+ 1785602294fSDan Kimmel * +-----------+ | |-----------| |b_next +-->NULL 1795602294fSDan Kimmel * | |b_comp = T | +-----------+ 1805602294fSDan Kimmel * | |b_data +-+ |b_comp = F | 1815602294fSDan Kimmel * | +-----------+ | |b_data +-+ 1825602294fSDan Kimmel * +->+------+ | +-----------+ | 1835602294fSDan Kimmel * compressed | | | | 1845602294fSDan Kimmel * data | |<--------------+ | uncompressed 1855602294fSDan Kimmel * +------+ compressed, | data 1865602294fSDan Kimmel * shared +-->+------+ 1875602294fSDan Kimmel * data | | 1885602294fSDan Kimmel * | | 1895602294fSDan Kimmel * +------+ 190dcbf3bd6SGeorge Wilson * 191dcbf3bd6SGeorge Wilson * When a consumer reads a block, the ARC must first look to see if the 1925602294fSDan Kimmel * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new 1935602294fSDan Kimmel * arc_buf_t and either copies uncompressed data into a new data buffer from an 194770499e1SDan Kimmel * existing uncompressed arc_buf_t, decompresses the hdr's b_pabd buffer into a 195770499e1SDan Kimmel * new data buffer, or shares the hdr's b_pabd buffer, depending on whether the 1965602294fSDan Kimmel * hdr is compressed and the desired compression characteristics of the 1975602294fSDan Kimmel * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the 1985602294fSDan Kimmel * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be 1995602294fSDan Kimmel * the last buffer in the hdr's b_buf list, however a shared compressed buf can 2005602294fSDan Kimmel * be anywhere in the hdr's list. 201dcbf3bd6SGeorge Wilson * 202dcbf3bd6SGeorge Wilson * The diagram below shows an example of an uncompressed ARC hdr that is 2035602294fSDan Kimmel * sharing its data with an arc_buf_t (note that the shared uncompressed buf is 2045602294fSDan Kimmel * the last element in the buf list): 205dcbf3bd6SGeorge Wilson * 206dcbf3bd6SGeorge Wilson * arc_buf_hdr_t 207dcbf3bd6SGeorge Wilson * +-----------+ 208dcbf3bd6SGeorge Wilson * | | 209dcbf3bd6SGeorge Wilson * | | 210dcbf3bd6SGeorge Wilson * | | 211dcbf3bd6SGeorge Wilson * +-----------+ 212dcbf3bd6SGeorge Wilson * l2arc_buf_hdr_t| | 213dcbf3bd6SGeorge Wilson * | | 214dcbf3bd6SGeorge Wilson * +-----------+ 215dcbf3bd6SGeorge Wilson * l1arc_buf_hdr_t| | 216dcbf3bd6SGeorge Wilson * | | arc_buf_t (shared) 217dcbf3bd6SGeorge Wilson * | b_buf +------------>+---------+ arc_buf_t 218dcbf3bd6SGeorge Wilson * | | |b_next +---->+---------+ 219770499e1SDan Kimmel * | b_pabd +-+ |---------| |b_next +-->NULL 220dcbf3bd6SGeorge Wilson * +-----------+ | | | +---------+ 221dcbf3bd6SGeorge Wilson * | |b_data +-+ | | 222dcbf3bd6SGeorge Wilson * | +---------+ | |b_data +-+ 223dcbf3bd6SGeorge Wilson * +->+------+ | +---------+ | 224dcbf3bd6SGeorge Wilson * | | | | 225dcbf3bd6SGeorge Wilson * uncompressed | | | | 226dcbf3bd6SGeorge Wilson * data +------+ | | 227dcbf3bd6SGeorge Wilson * ^ +->+------+ | 228dcbf3bd6SGeorge Wilson * | uncompressed | | | 229dcbf3bd6SGeorge Wilson * | data | | | 230dcbf3bd6SGeorge Wilson * | +------+ | 231dcbf3bd6SGeorge Wilson * +---------------------------------+ 232dcbf3bd6SGeorge Wilson * 233770499e1SDan Kimmel * Writing to the ARC requires that the ARC first discard the hdr's b_pabd 234dcbf3bd6SGeorge Wilson * since the physical block is about to be rewritten. The new data contents 2355602294fSDan Kimmel * will be contained in the arc_buf_t. As the I/O pipeline performs the write, 2365602294fSDan Kimmel * it may compress the data before writing it to disk. The ARC will be called 2375602294fSDan Kimmel * with the transformed data and will bcopy the transformed on-disk block into 238770499e1SDan Kimmel * a newly allocated b_pabd. Writes are always done into buffers which have 2395602294fSDan Kimmel * either been loaned (and hence are new and don't have other readers) or 2405602294fSDan Kimmel * buffers which have been released (and hence have their own hdr, if there 2415602294fSDan Kimmel * were originally other readers of the buf's original hdr). This ensures that 2425602294fSDan Kimmel * the ARC only needs to update a single buf and its hdr after a write occurs. 243dcbf3bd6SGeorge Wilson * 244770499e1SDan Kimmel * When the L2ARC is in use, it will also take advantage of the b_pabd. The 245770499e1SDan Kimmel * L2ARC will always write the contents of b_pabd to the L2ARC. This means 2465602294fSDan Kimmel * that when compressed ARC is enabled that the L2ARC blocks are identical 247dcbf3bd6SGeorge Wilson * to the on-disk block in the main data pool. This provides a significant 248dcbf3bd6SGeorge Wilson * advantage since the ARC can leverage the bp's checksum when reading from the 249dcbf3bd6SGeorge Wilson * L2ARC to determine if the contents are valid. However, if the compressed 2505602294fSDan Kimmel * ARC is disabled, then the L2ARC's block must be transformed to look 251dcbf3bd6SGeorge Wilson * like the physical block in the main data pool before comparing the 252dcbf3bd6SGeorge Wilson * checksum and determining its validity. 253dcbf3bd6SGeorge Wilson */ 254dcbf3bd6SGeorge Wilson 255fa9e4066Sahrens #include <sys/spa.h> 256fa9e4066Sahrens #include <sys/zio.h> 257dcbf3bd6SGeorge Wilson #include <sys/spa_impl.h> 258aad02571SSaso Kiselkov #include <sys/zio_compress.h> 259dcbf3bd6SGeorge Wilson #include <sys/zio_checksum.h> 260fa9e4066Sahrens #include <sys/zfs_context.h> 261fa9e4066Sahrens #include <sys/arc.h> 262fa9e4066Sahrens #include <sys/refcount.h> 263c5904d13Seschrock #include <sys/vdev.h> 264573ca77eSGeorge Wilson #include <sys/vdev_impl.h> 26569962b56SMatthew Ahrens #include <sys/dsl_pool.h> 266770499e1SDan Kimmel #include <sys/zio_checksum.h> 267244781f1SPrakash Surya #include <sys/multilist.h> 268770499e1SDan Kimmel #include <sys/abd.h> 269fa9e4066Sahrens #ifdef _KERNEL 270fa9e4066Sahrens #include <sys/vmsystm.h> 271fa9e4066Sahrens #include <vm/anon.h> 272fa9e4066Sahrens #include <sys/fs/swapnode.h> 273033f9833Sek #include <sys/dnlc.h> 274fa9e4066Sahrens #endif 275fa9e4066Sahrens #include <sys/callb.h> 27644cb6abcSbmc #include <sys/kstat.h> 277b24ab676SJeff Bonwick #include <zfs_fletcher.h> 278*3a2d8a1bSPaul Dagnelie #include <sys/aggsum.h> 279*3a2d8a1bSPaul Dagnelie #include <sys/cityhash.h> 280fa9e4066Sahrens 281cd1c8b85SMatthew Ahrens #ifndef _KERNEL 282cd1c8b85SMatthew Ahrens /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ 283cd1c8b85SMatthew Ahrens boolean_t arc_watch = B_FALSE; 284cd1c8b85SMatthew Ahrens int arc_procfd; 285cd1c8b85SMatthew Ahrens #endif 286cd1c8b85SMatthew Ahrens 287244781f1SPrakash Surya static kmutex_t arc_reclaim_lock; 288244781f1SPrakash Surya static kcondvar_t arc_reclaim_thread_cv; 289244781f1SPrakash Surya static boolean_t arc_reclaim_thread_exit; 290244781f1SPrakash Surya static kcondvar_t arc_reclaim_waiters_cv; 291244781f1SPrakash Surya 2922ec99e3eSMatthew Ahrens uint_t arc_reduce_dnlc_percent = 3; 293fa9e4066Sahrens 29469962b56SMatthew Ahrens /* 295244781f1SPrakash Surya * The number of headers to evict in arc_evict_state_impl() before 296244781f1SPrakash Surya * dropping the sublist lock and evicting from another sublist. A lower 297244781f1SPrakash Surya * value means we're more likely to evict the "correct" header (i.e. the 298244781f1SPrakash Surya * oldest header in the arc state), but comes with higher overhead 299244781f1SPrakash Surya * (i.e. more invocations of arc_evict_state_impl()). 300244781f1SPrakash Surya */ 301244781f1SPrakash Surya int zfs_arc_evict_batch_limit = 10; 302244781f1SPrakash Surya 303fa9e4066Sahrens /* number of seconds before growing cache again */ 304fa9e4066Sahrens static int arc_grow_retry = 60; 305fa9e4066Sahrens 30636a64e62STim Kordas /* number of milliseconds before attempting a kmem-cache-reap */ 30736a64e62STim Kordas static int arc_kmem_cache_reap_retry_ms = 1000; 30836a64e62STim Kordas 309770499e1SDan Kimmel /* shift of arc_c for calculating overflow limit in arc_get_data_impl */ 310244781f1SPrakash Surya int zfs_arc_overflow_shift = 8; 311244781f1SPrakash Surya 3125a98e54bSBrendan Gregg - Sun Microsystems /* shift of arc_c for calculating both min and max arc_p */ 3135a98e54bSBrendan Gregg - Sun Microsystems static int arc_p_min_shift = 4; 3145a98e54bSBrendan Gregg - Sun Microsystems 3155a98e54bSBrendan Gregg - Sun Microsystems /* log2(fraction of arc to reclaim) */ 3162ec99e3eSMatthew Ahrens static int arc_shrink_shift = 7; 3172ec99e3eSMatthew Ahrens 3182ec99e3eSMatthew Ahrens /* 3192ec99e3eSMatthew Ahrens * log2(fraction of ARC which must be free to allow growing). 3202ec99e3eSMatthew Ahrens * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, 3212ec99e3eSMatthew Ahrens * when reading a new block into the ARC, we will evict an equal-sized block 3222ec99e3eSMatthew Ahrens * from the ARC. 3232ec99e3eSMatthew Ahrens * 3242ec99e3eSMatthew Ahrens * This must be less than arc_shrink_shift, so that when we shrink the ARC, 3252ec99e3eSMatthew Ahrens * we will still not allow it to grow. 3262ec99e3eSMatthew Ahrens */ 3272ec99e3eSMatthew Ahrens int arc_no_grow_shift = 5; 3282ec99e3eSMatthew Ahrens 3295a98e54bSBrendan Gregg - Sun Microsystems 33013506d1eSmaybee /* 331b19a79ecSperrin * minimum lifespan of a prefetch block in clock ticks 332b19a79ecSperrin * (initialized in arc_init()) 33313506d1eSmaybee */ 334b19a79ecSperrin static int arc_min_prefetch_lifespan; 33513506d1eSmaybee 33669962b56SMatthew Ahrens /* 33769962b56SMatthew Ahrens * If this percent of memory is free, don't throttle. 33869962b56SMatthew Ahrens */ 33969962b56SMatthew Ahrens int arc_lotsfree_percent = 10; 34069962b56SMatthew Ahrens 341fa9e4066Sahrens static int arc_dead; 342fa9e4066Sahrens 3433a737e0dSbrendan /* 3443a737e0dSbrendan * The arc has filled available memory and has now warmed up. 3453a737e0dSbrendan */ 3463a737e0dSbrendan static boolean_t arc_warm; 3473a737e0dSbrendan 3480dd053d7SPrakash Surya /* 3490dd053d7SPrakash Surya * log2 fraction of the zio arena to keep free. 3500dd053d7SPrakash Surya */ 3510dd053d7SPrakash Surya int arc_zio_arena_free_shift = 2; 3520dd053d7SPrakash Surya 353a2eea2e1Sahrens /* 354a2eea2e1Sahrens * These tunables are for performance analysis. 355a2eea2e1Sahrens */ 356a2eea2e1Sahrens uint64_t zfs_arc_max; 357a2eea2e1Sahrens uint64_t zfs_arc_min; 3581116048bSek uint64_t zfs_arc_meta_limit = 0; 3593a5286a1SMatthew Ahrens uint64_t zfs_arc_meta_min = 0; 3605a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_grow_retry = 0; 3615a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_shrink_shift = 0; 3625a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_p_min_shift = 0; 36363e911b6SMatthew Ahrens int zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ 364a2eea2e1Sahrens 365dcbf3bd6SGeorge Wilson boolean_t zfs_compressed_arc_enabled = B_TRUE; 366dcbf3bd6SGeorge Wilson 367fa9e4066Sahrens /* 368fa94a07fSbrendan * Note that buffers can be in one of 6 states: 369fa9e4066Sahrens * ARC_anon - anonymous (discussed below) 370ea8dc4b6Seschrock * ARC_mru - recently used, currently cached 371ea8dc4b6Seschrock * ARC_mru_ghost - recentely used, no longer in cache 372ea8dc4b6Seschrock * ARC_mfu - frequently used, currently cached 373ea8dc4b6Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 374fa94a07fSbrendan * ARC_l2c_only - exists in L2ARC but not other states 3750e8c6158Smaybee * When there are no active references to the buffer, they are 3760e8c6158Smaybee * are linked onto a list in one of these arc states. These are 3770e8c6158Smaybee * the only buffers that can be evicted or deleted. Within each 3780e8c6158Smaybee * state there are multiple lists, one for meta-data and one for 3790e8c6158Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 3800e8c6158Smaybee * etc.) is tracked separately so that it can be managed more 381fa94a07fSbrendan * explicitly: favored over data, limited explicitly. 382fa9e4066Sahrens * 383fa9e4066Sahrens * Anonymous buffers are buffers that are not associated with 384fa9e4066Sahrens * a DVA. These are buffers that hold dirty block copies 385fa9e4066Sahrens * before they are written to stable storage. By definition, 386ea8dc4b6Seschrock * they are "ref'd" and are considered part of arc_mru 387fa9e4066Sahrens * that cannot be freed. Generally, they will aquire a DVA 388ea8dc4b6Seschrock * as they are written and migrate onto the arc_mru list. 389fa94a07fSbrendan * 390fa94a07fSbrendan * The ARC_l2c_only state is for buffers that are in the second 391fa94a07fSbrendan * level ARC but no longer in any of the ARC_m* lists. The second 392fa94a07fSbrendan * level ARC itself may also contain buffers that are in any of 393fa94a07fSbrendan * the ARC_m* states - meaning that a buffer can exist in two 394fa94a07fSbrendan * places. The reason for the ARC_l2c_only state is to keep the 395fa94a07fSbrendan * buffer header in the hash table, so that reads that hit the 396fa94a07fSbrendan * second level ARC benefit from these fast lookups. 397fa9e4066Sahrens */ 398fa9e4066Sahrens 399fa9e4066Sahrens typedef struct arc_state { 400244781f1SPrakash Surya /* 401244781f1SPrakash Surya * list of evictable buffers 402244781f1SPrakash Surya */ 40394c2d0ebSMatthew Ahrens multilist_t *arcs_list[ARC_BUFC_NUMTYPES]; 404244781f1SPrakash Surya /* 405244781f1SPrakash Surya * total amount of evictable data in this state 406244781f1SPrakash Surya */ 407dcbf3bd6SGeorge Wilson refcount_t arcs_esize[ARC_BUFC_NUMTYPES]; 408244781f1SPrakash Surya /* 409244781f1SPrakash Surya * total amount of data in this state; this includes: evictable, 410244781f1SPrakash Surya * non-evictable, ARC_BUFC_DATA, and ARC_BUFC_METADATA. 411244781f1SPrakash Surya */ 4122fd872a7SPrakash Surya refcount_t arcs_size; 413fa9e4066Sahrens } arc_state_t; 414fa9e4066Sahrens 415fa94a07fSbrendan /* The 6 states: */ 416fa9e4066Sahrens static arc_state_t ARC_anon; 417ea8dc4b6Seschrock static arc_state_t ARC_mru; 418ea8dc4b6Seschrock static arc_state_t ARC_mru_ghost; 419ea8dc4b6Seschrock static arc_state_t ARC_mfu; 420ea8dc4b6Seschrock static arc_state_t ARC_mfu_ghost; 421fa94a07fSbrendan static arc_state_t ARC_l2c_only; 422fa9e4066Sahrens 42344cb6abcSbmc typedef struct arc_stats { 42444cb6abcSbmc kstat_named_t arcstat_hits; 42544cb6abcSbmc kstat_named_t arcstat_misses; 42644cb6abcSbmc kstat_named_t arcstat_demand_data_hits; 42744cb6abcSbmc kstat_named_t arcstat_demand_data_misses; 42844cb6abcSbmc kstat_named_t arcstat_demand_metadata_hits; 42944cb6abcSbmc kstat_named_t arcstat_demand_metadata_misses; 43044cb6abcSbmc kstat_named_t arcstat_prefetch_data_hits; 43144cb6abcSbmc kstat_named_t arcstat_prefetch_data_misses; 43244cb6abcSbmc kstat_named_t arcstat_prefetch_metadata_hits; 43344cb6abcSbmc kstat_named_t arcstat_prefetch_metadata_misses; 43444cb6abcSbmc kstat_named_t arcstat_mru_hits; 43544cb6abcSbmc kstat_named_t arcstat_mru_ghost_hits; 43644cb6abcSbmc kstat_named_t arcstat_mfu_hits; 43744cb6abcSbmc kstat_named_t arcstat_mfu_ghost_hits; 43844cb6abcSbmc kstat_named_t arcstat_deleted; 4393e30c24aSWill Andrews /* 4403e30c24aSWill Andrews * Number of buffers that could not be evicted because the hash lock 4413e30c24aSWill Andrews * was held by another thread. The lock may not necessarily be held 4423e30c24aSWill Andrews * by something using the same buffer, since hash locks are shared 4433e30c24aSWill Andrews * by multiple buffers. 4443e30c24aSWill Andrews */ 44544cb6abcSbmc kstat_named_t arcstat_mutex_miss; 4463e30c24aSWill Andrews /* 4473e30c24aSWill Andrews * Number of buffers skipped because they have I/O in progress, are 4483e30c24aSWill Andrews * indrect prefetch buffers that have not lived long enough, or are 4493e30c24aSWill Andrews * not from the spa we're trying to evict from. 4503e30c24aSWill Andrews */ 45144cb6abcSbmc kstat_named_t arcstat_evict_skip; 452244781f1SPrakash Surya /* 453244781f1SPrakash Surya * Number of times arc_evict_state() was unable to evict enough 454244781f1SPrakash Surya * buffers to reach it's target amount. 455244781f1SPrakash Surya */ 456244781f1SPrakash Surya kstat_named_t arcstat_evict_not_enough; 4575ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_cached; 4585ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_eligible; 4595ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_ineligible; 460244781f1SPrakash Surya kstat_named_t arcstat_evict_l2_skip; 46144cb6abcSbmc kstat_named_t arcstat_hash_elements; 46244cb6abcSbmc kstat_named_t arcstat_hash_elements_max; 46344cb6abcSbmc kstat_named_t arcstat_hash_collisions; 46444cb6abcSbmc kstat_named_t arcstat_hash_chains; 46544cb6abcSbmc kstat_named_t arcstat_hash_chain_max; 46644cb6abcSbmc kstat_named_t arcstat_p; 46744cb6abcSbmc kstat_named_t arcstat_c; 46844cb6abcSbmc kstat_named_t arcstat_c_min; 46944cb6abcSbmc kstat_named_t arcstat_c_max; 470*3a2d8a1bSPaul Dagnelie /* Not updated directly; only synced in arc_kstat_update. */ 47144cb6abcSbmc kstat_named_t arcstat_size; 472dcbf3bd6SGeorge Wilson /* 473770499e1SDan Kimmel * Number of compressed bytes stored in the arc_buf_hdr_t's b_pabd. 474dcbf3bd6SGeorge Wilson * Note that the compressed bytes may match the uncompressed bytes 475dcbf3bd6SGeorge Wilson * if the block is either not compressed or compressed arc is disabled. 476dcbf3bd6SGeorge Wilson */ 477dcbf3bd6SGeorge Wilson kstat_named_t arcstat_compressed_size; 478dcbf3bd6SGeorge Wilson /* 479770499e1SDan Kimmel * Uncompressed size of the data stored in b_pabd. If compressed 480dcbf3bd6SGeorge Wilson * arc is disabled then this value will be identical to the stat 481dcbf3bd6SGeorge Wilson * above. 482dcbf3bd6SGeorge Wilson */ 483dcbf3bd6SGeorge Wilson kstat_named_t arcstat_uncompressed_size; 484dcbf3bd6SGeorge Wilson /* 485dcbf3bd6SGeorge Wilson * Number of bytes stored in all the arc_buf_t's. This is classified 486dcbf3bd6SGeorge Wilson * as "overhead" since this data is typically short-lived and will 487dcbf3bd6SGeorge Wilson * be evicted from the arc when it becomes unreferenced unless the 488dcbf3bd6SGeorge Wilson * zfs_keep_uncompressed_metadata or zfs_keep_uncompressed_level 489dcbf3bd6SGeorge Wilson * values have been set (see comment in dbuf.c for more information). 490dcbf3bd6SGeorge Wilson */ 491dcbf3bd6SGeorge Wilson kstat_named_t arcstat_overhead_size; 4924076b1bfSPrakash Surya /* 4934076b1bfSPrakash Surya * Number of bytes consumed by internal ARC structures necessary 4944076b1bfSPrakash Surya * for tracking purposes; these structures are not actually 4954076b1bfSPrakash Surya * backed by ARC buffers. This includes arc_buf_hdr_t structures 4964076b1bfSPrakash Surya * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only 4974076b1bfSPrakash Surya * caches), and arc_buf_t structures (allocated via arc_buf_t 4984076b1bfSPrakash Surya * cache). 499*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5004076b1bfSPrakash Surya */ 501fa94a07fSbrendan kstat_named_t arcstat_hdr_size; 5024076b1bfSPrakash Surya /* 5034076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers of type equal to 5044076b1bfSPrakash Surya * ARC_BUFC_DATA. This is generally consumed by buffers backing 5054076b1bfSPrakash Surya * on disk user data (e.g. plain file contents). 506*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5074076b1bfSPrakash Surya */ 5085a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_data_size; 5094076b1bfSPrakash Surya /* 5104076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers of type equal to 5114076b1bfSPrakash Surya * ARC_BUFC_METADATA. This is generally consumed by buffers 5124076b1bfSPrakash Surya * backing on disk data that is used for internal ZFS 5134076b1bfSPrakash Surya * structures (e.g. ZAP, dnode, indirect blocks, etc). 514*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5154076b1bfSPrakash Surya */ 5164076b1bfSPrakash Surya kstat_named_t arcstat_metadata_size; 5174076b1bfSPrakash Surya /* 5184076b1bfSPrakash Surya * Number of bytes consumed by various buffers and structures 5194076b1bfSPrakash Surya * not actually backed with ARC buffers. This includes bonus 5204076b1bfSPrakash Surya * buffers (allocated directly via zio_buf_* functions), 5214076b1bfSPrakash Surya * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t 5224076b1bfSPrakash Surya * cache), and dnode_t structures (allocated via dnode_t cache). 523*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5244076b1bfSPrakash Surya */ 5255a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_other_size; 5264076b1bfSPrakash Surya /* 5274076b1bfSPrakash Surya * Total number of bytes consumed by ARC buffers residing in the 5284076b1bfSPrakash Surya * arc_anon state. This includes *all* buffers in the arc_anon 5294076b1bfSPrakash Surya * state; e.g. data, metadata, evictable, and unevictable buffers 5304076b1bfSPrakash Surya * are all included in this value. 531*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5324076b1bfSPrakash Surya */ 5334076b1bfSPrakash Surya kstat_named_t arcstat_anon_size; 5344076b1bfSPrakash Surya /* 5354076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5364076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_DATA, 5374076b1bfSPrakash Surya * residing in the arc_anon state, and are eligible for eviction 5384076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 539*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5404076b1bfSPrakash Surya */ 5414076b1bfSPrakash Surya kstat_named_t arcstat_anon_evictable_data; 5424076b1bfSPrakash Surya /* 5434076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5444076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_METADATA, 5454076b1bfSPrakash Surya * residing in the arc_anon state, and are eligible for eviction 5464076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 547*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5484076b1bfSPrakash Surya */ 5494076b1bfSPrakash Surya kstat_named_t arcstat_anon_evictable_metadata; 5504076b1bfSPrakash Surya /* 5514076b1bfSPrakash Surya * Total number of bytes consumed by ARC buffers residing in the 5524076b1bfSPrakash Surya * arc_mru state. This includes *all* buffers in the arc_mru 5534076b1bfSPrakash Surya * state; e.g. data, metadata, evictable, and unevictable buffers 5544076b1bfSPrakash Surya * are all included in this value. 555*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5564076b1bfSPrakash Surya */ 5574076b1bfSPrakash Surya kstat_named_t arcstat_mru_size; 5584076b1bfSPrakash Surya /* 5594076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5604076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_DATA, 5614076b1bfSPrakash Surya * residing in the arc_mru state, and are eligible for eviction 5624076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 563*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5644076b1bfSPrakash Surya */ 5654076b1bfSPrakash Surya kstat_named_t arcstat_mru_evictable_data; 5664076b1bfSPrakash Surya /* 5674076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5684076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_METADATA, 5694076b1bfSPrakash Surya * residing in the arc_mru state, and are eligible for eviction 5704076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 571*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5724076b1bfSPrakash Surya */ 5734076b1bfSPrakash Surya kstat_named_t arcstat_mru_evictable_metadata; 5744076b1bfSPrakash Surya /* 5754076b1bfSPrakash Surya * Total number of bytes that *would have been* consumed by ARC 5764076b1bfSPrakash Surya * buffers in the arc_mru_ghost state. The key thing to note 5774076b1bfSPrakash Surya * here, is the fact that this size doesn't actually indicate 5784076b1bfSPrakash Surya * RAM consumption. The ghost lists only consist of headers and 5794076b1bfSPrakash Surya * don't actually have ARC buffers linked off of these headers. 5804076b1bfSPrakash Surya * Thus, *if* the headers had associated ARC buffers, these 5814076b1bfSPrakash Surya * buffers *would have* consumed this number of bytes. 582*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5834076b1bfSPrakash Surya */ 5844076b1bfSPrakash Surya kstat_named_t arcstat_mru_ghost_size; 5854076b1bfSPrakash Surya /* 5864076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 5874076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 5884076b1bfSPrakash Surya * ARC_BUFC_DATA, and linked off the arc_mru_ghost state. 589*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5904076b1bfSPrakash Surya */ 5914076b1bfSPrakash Surya kstat_named_t arcstat_mru_ghost_evictable_data; 5924076b1bfSPrakash Surya /* 5934076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 5944076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 5954076b1bfSPrakash Surya * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 596*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5974076b1bfSPrakash Surya */ 5984076b1bfSPrakash Surya kstat_named_t arcstat_mru_ghost_evictable_metadata; 5994076b1bfSPrakash Surya /* 6004076b1bfSPrakash Surya * Total number of bytes consumed by ARC buffers residing in the 6014076b1bfSPrakash Surya * arc_mfu state. This includes *all* buffers in the arc_mfu 6024076b1bfSPrakash Surya * state; e.g. data, metadata, evictable, and unevictable buffers 6034076b1bfSPrakash Surya * are all included in this value. 604*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6054076b1bfSPrakash Surya */ 6064076b1bfSPrakash Surya kstat_named_t arcstat_mfu_size; 6074076b1bfSPrakash Surya /* 6084076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that are eligible for 6094076b1bfSPrakash Surya * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu 6104076b1bfSPrakash Surya * state. 611*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6124076b1bfSPrakash Surya */ 6134076b1bfSPrakash Surya kstat_named_t arcstat_mfu_evictable_data; 6144076b1bfSPrakash Surya /* 6154076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that are eligible for 6164076b1bfSPrakash Surya * eviction, of type ARC_BUFC_METADATA, and reside in the 6174076b1bfSPrakash Surya * arc_mfu state. 618*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6194076b1bfSPrakash Surya */ 6204076b1bfSPrakash Surya kstat_named_t arcstat_mfu_evictable_metadata; 6214076b1bfSPrakash Surya /* 6224076b1bfSPrakash Surya * Total number of bytes that *would have been* consumed by ARC 6234076b1bfSPrakash Surya * buffers in the arc_mfu_ghost state. See the comment above 6244076b1bfSPrakash Surya * arcstat_mru_ghost_size for more details. 625*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6264076b1bfSPrakash Surya */ 6274076b1bfSPrakash Surya kstat_named_t arcstat_mfu_ghost_size; 6284076b1bfSPrakash Surya /* 6294076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 6304076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 6314076b1bfSPrakash Surya * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state. 632*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6334076b1bfSPrakash Surya */ 6344076b1bfSPrakash Surya kstat_named_t arcstat_mfu_ghost_evictable_data; 6354076b1bfSPrakash Surya /* 6364076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 6374076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 6384076b1bfSPrakash Surya * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 639*3a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6404076b1bfSPrakash Surya */ 6414076b1bfSPrakash Surya kstat_named_t arcstat_mfu_ghost_evictable_metadata; 642fa94a07fSbrendan kstat_named_t arcstat_l2_hits; 643fa94a07fSbrendan kstat_named_t arcstat_l2_misses; 644fa94a07fSbrendan kstat_named_t arcstat_l2_feeds; 645fa94a07fSbrendan kstat_named_t arcstat_l2_rw_clash; 6465a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_l2_read_bytes; 6475a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_l2_write_bytes; 648fa94a07fSbrendan kstat_named_t arcstat_l2_writes_sent; 649fa94a07fSbrendan kstat_named_t arcstat_l2_writes_done; 650fa94a07fSbrendan kstat_named_t arcstat_l2_writes_error; 651244781f1SPrakash Surya kstat_named_t arcstat_l2_writes_lock_retry; 652fa94a07fSbrendan kstat_named_t arcstat_l2_evict_lock_retry; 653fa94a07fSbrendan kstat_named_t arcstat_l2_evict_reading; 65489c86e32SChris Williamson kstat_named_t arcstat_l2_evict_l1cached; 655fa94a07fSbrendan kstat_named_t arcstat_l2_free_on_write; 656fa94a07fSbrendan kstat_named_t arcstat_l2_abort_lowmem; 657fa94a07fSbrendan kstat_named_t arcstat_l2_cksum_bad; 658fa94a07fSbrendan kstat_named_t arcstat_l2_io_error; 65916a7e5acSAndriy Gapon kstat_named_t arcstat_l2_lsize; 66016a7e5acSAndriy Gapon kstat_named_t arcstat_l2_psize; 661*3a2d8a1bSPaul Dagnelie /* Not updated directly; only synced in arc_kstat_update. */ 662fa94a07fSbrendan kstat_named_t arcstat_l2_hdr_size; 6631ab7f2deSmaybee kstat_named_t arcstat_memory_throttle_count; 664*3a2d8a1bSPaul Dagnelie /* Not updated directly; only synced in arc_kstat_update. */ 66520128a08SGeorge Wilson kstat_named_t arcstat_meta_used; 66620128a08SGeorge Wilson kstat_named_t arcstat_meta_limit; 66720128a08SGeorge Wilson kstat_named_t arcstat_meta_max; 6683a5286a1SMatthew Ahrens kstat_named_t arcstat_meta_min; 669cf6106c8SMatthew Ahrens kstat_named_t arcstat_sync_wait_for_async; 670cf6106c8SMatthew Ahrens kstat_named_t arcstat_demand_hit_predictive_prefetch; 67144cb6abcSbmc } arc_stats_t; 67244cb6abcSbmc 67344cb6abcSbmc static arc_stats_t arc_stats = { 67444cb6abcSbmc { "hits", KSTAT_DATA_UINT64 }, 67544cb6abcSbmc { "misses", KSTAT_DATA_UINT64 }, 67644cb6abcSbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 67744cb6abcSbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 67844cb6abcSbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 67944cb6abcSbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 68044cb6abcSbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 68144cb6abcSbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 68244cb6abcSbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 68344cb6abcSbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 68444cb6abcSbmc { "mru_hits", KSTAT_DATA_UINT64 }, 68544cb6abcSbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 68644cb6abcSbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 68744cb6abcSbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 68844cb6abcSbmc { "deleted", KSTAT_DATA_UINT64 }, 68944cb6abcSbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 69044cb6abcSbmc { "evict_skip", KSTAT_DATA_UINT64 }, 691244781f1SPrakash Surya { "evict_not_enough", KSTAT_DATA_UINT64 }, 6925ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_cached", KSTAT_DATA_UINT64 }, 6935ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 6945ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 695244781f1SPrakash Surya { "evict_l2_skip", KSTAT_DATA_UINT64 }, 69644cb6abcSbmc { "hash_elements", KSTAT_DATA_UINT64 }, 69744cb6abcSbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 69844cb6abcSbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 69944cb6abcSbmc { "hash_chains", KSTAT_DATA_UINT64 }, 70044cb6abcSbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 70144cb6abcSbmc { "p", KSTAT_DATA_UINT64 }, 70244cb6abcSbmc { "c", KSTAT_DATA_UINT64 }, 70344cb6abcSbmc { "c_min", KSTAT_DATA_UINT64 }, 70444cb6abcSbmc { "c_max", KSTAT_DATA_UINT64 }, 705fa94a07fSbrendan { "size", KSTAT_DATA_UINT64 }, 706dcbf3bd6SGeorge Wilson { "compressed_size", KSTAT_DATA_UINT64 }, 707dcbf3bd6SGeorge Wilson { "uncompressed_size", KSTAT_DATA_UINT64 }, 708dcbf3bd6SGeorge Wilson { "overhead_size", KSTAT_DATA_UINT64 }, 709fa94a07fSbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 7105a98e54bSBrendan Gregg - Sun Microsystems { "data_size", KSTAT_DATA_UINT64 }, 7114076b1bfSPrakash Surya { "metadata_size", KSTAT_DATA_UINT64 }, 7125a98e54bSBrendan Gregg - Sun Microsystems { "other_size", KSTAT_DATA_UINT64 }, 7134076b1bfSPrakash Surya { "anon_size", KSTAT_DATA_UINT64 }, 7144076b1bfSPrakash Surya { "anon_evictable_data", KSTAT_DATA_UINT64 }, 7154076b1bfSPrakash Surya { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, 7164076b1bfSPrakash Surya { "mru_size", KSTAT_DATA_UINT64 }, 7174076b1bfSPrakash Surya { "mru_evictable_data", KSTAT_DATA_UINT64 }, 7184076b1bfSPrakash Surya { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, 7194076b1bfSPrakash Surya { "mru_ghost_size", KSTAT_DATA_UINT64 }, 7204076b1bfSPrakash Surya { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, 7214076b1bfSPrakash Surya { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 7224076b1bfSPrakash Surya { "mfu_size", KSTAT_DATA_UINT64 }, 7234076b1bfSPrakash Surya { "mfu_evictable_data", KSTAT_DATA_UINT64 }, 7244076b1bfSPrakash Surya { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, 7254076b1bfSPrakash Surya { "mfu_ghost_size", KSTAT_DATA_UINT64 }, 7264076b1bfSPrakash Surya { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, 7274076b1bfSPrakash Surya { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 728fa94a07fSbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 729fa94a07fSbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 730fa94a07fSbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 731fa94a07fSbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 7325a98e54bSBrendan Gregg - Sun Microsystems { "l2_read_bytes", KSTAT_DATA_UINT64 }, 7335a98e54bSBrendan Gregg - Sun Microsystems { "l2_write_bytes", KSTAT_DATA_UINT64 }, 734fa94a07fSbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 735fa94a07fSbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 736fa94a07fSbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 737244781f1SPrakash Surya { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, 738fa94a07fSbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 739fa94a07fSbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 74089c86e32SChris Williamson { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, 741fa94a07fSbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 742fa94a07fSbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 743fa94a07fSbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 744fa94a07fSbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 745fa94a07fSbrendan { "l2_size", KSTAT_DATA_UINT64 }, 746aad02571SSaso Kiselkov { "l2_asize", KSTAT_DATA_UINT64 }, 7471ab7f2deSmaybee { "l2_hdr_size", KSTAT_DATA_UINT64 }, 7489253d63dSGeorge Wilson { "memory_throttle_count", KSTAT_DATA_UINT64 }, 74920128a08SGeorge Wilson { "arc_meta_used", KSTAT_DATA_UINT64 }, 75020128a08SGeorge Wilson { "arc_meta_limit", KSTAT_DATA_UINT64 }, 7513a5286a1SMatthew Ahrens { "arc_meta_max", KSTAT_DATA_UINT64 }, 752cf6106c8SMatthew Ahrens { "arc_meta_min", KSTAT_DATA_UINT64 }, 753cf6106c8SMatthew Ahrens { "sync_wait_for_async", KSTAT_DATA_UINT64 }, 754cf6106c8SMatthew Ahrens { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 }, 75544cb6abcSbmc }; 75644cb6abcSbmc 75744cb6abcSbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 75844cb6abcSbmc 75944cb6abcSbmc #define ARCSTAT_INCR(stat, val) \ 760f7170741SWill Andrews atomic_add_64(&arc_stats.stat.value.ui64, (val)) 76144cb6abcSbmc 762b24ab676SJeff Bonwick #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 76344cb6abcSbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 76444cb6abcSbmc 76544cb6abcSbmc #define ARCSTAT_MAX(stat, val) { \ 76644cb6abcSbmc uint64_t m; \ 76744cb6abcSbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 76844cb6abcSbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 76944cb6abcSbmc continue; \ 77044cb6abcSbmc } 77144cb6abcSbmc 77244cb6abcSbmc #define ARCSTAT_MAXSTAT(stat) \ 77344cb6abcSbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 77444cb6abcSbmc 77544cb6abcSbmc /* 77644cb6abcSbmc * We define a macro to allow ARC hits/misses to be easily broken down by 77744cb6abcSbmc * two separate conditions, giving a total of four different subtypes for 77844cb6abcSbmc * each of hits and misses (so eight statistics total). 77944cb6abcSbmc */ 78044cb6abcSbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 78144cb6abcSbmc if (cond1) { \ 78244cb6abcSbmc if (cond2) { \ 78344cb6abcSbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 78444cb6abcSbmc } else { \ 78544cb6abcSbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 78644cb6abcSbmc } \ 78744cb6abcSbmc } else { \ 78844cb6abcSbmc if (cond2) { \ 78944cb6abcSbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 79044cb6abcSbmc } else { \ 79144cb6abcSbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 79244cb6abcSbmc } \ 79344cb6abcSbmc } 79444cb6abcSbmc 79544cb6abcSbmc kstat_t *arc_ksp; 796b24ab676SJeff Bonwick static arc_state_t *arc_anon; 79744cb6abcSbmc static arc_state_t *arc_mru; 79844cb6abcSbmc static arc_state_t *arc_mru_ghost; 79944cb6abcSbmc static arc_state_t *arc_mfu; 80044cb6abcSbmc static arc_state_t *arc_mfu_ghost; 801fa94a07fSbrendan static arc_state_t *arc_l2c_only; 80244cb6abcSbmc 80344cb6abcSbmc /* 80444cb6abcSbmc * There are several ARC variables that are critical to export as kstats -- 80544cb6abcSbmc * but we don't want to have to grovel around in the kstat whenever we wish to 80644cb6abcSbmc * manipulate them. For these variables, we therefore define them to be in 80744cb6abcSbmc * terms of the statistic variable. This assures that we are not introducing 80844cb6abcSbmc * the possibility of inconsistency by having shadow copies of the variables, 80944cb6abcSbmc * while still allowing the code to be readable. 81044cb6abcSbmc */ 81144cb6abcSbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 81244cb6abcSbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 81344cb6abcSbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 81444cb6abcSbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 81520128a08SGeorge Wilson #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ 8163a5286a1SMatthew Ahrens #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ 81720128a08SGeorge Wilson #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ 81844cb6abcSbmc 819dcbf3bd6SGeorge Wilson /* compressed size of entire arc */ 820dcbf3bd6SGeorge Wilson #define arc_compressed_size ARCSTAT(arcstat_compressed_size) 821dcbf3bd6SGeorge Wilson /* uncompressed size of entire arc */ 822dcbf3bd6SGeorge Wilson #define arc_uncompressed_size ARCSTAT(arcstat_uncompressed_size) 823dcbf3bd6SGeorge Wilson /* number of bytes in the arc from arc_buf_t's */ 824dcbf3bd6SGeorge Wilson #define arc_overhead_size ARCSTAT(arcstat_overhead_size) 825aad02571SSaso Kiselkov 826*3a2d8a1bSPaul Dagnelie /* 827*3a2d8a1bSPaul Dagnelie * There are also some ARC variables that we want to export, but that are 828*3a2d8a1bSPaul Dagnelie * updated so often that having the canonical representation be the statistic 829*3a2d8a1bSPaul Dagnelie * variable causes a performance bottleneck. We want to use aggsum_t's for these 830*3a2d8a1bSPaul Dagnelie * instead, but still be able to export the kstat in the same way as before. 831*3a2d8a1bSPaul Dagnelie * The solution is to always use the aggsum version, except in the kstat update 832*3a2d8a1bSPaul Dagnelie * callback. 833*3a2d8a1bSPaul Dagnelie */ 834*3a2d8a1bSPaul Dagnelie aggsum_t arc_size; 835*3a2d8a1bSPaul Dagnelie aggsum_t arc_meta_used; 836*3a2d8a1bSPaul Dagnelie aggsum_t astat_data_size; 837*3a2d8a1bSPaul Dagnelie aggsum_t astat_metadata_size; 838*3a2d8a1bSPaul Dagnelie aggsum_t astat_hdr_size; 839*3a2d8a1bSPaul Dagnelie aggsum_t astat_other_size; 840*3a2d8a1bSPaul Dagnelie aggsum_t astat_l2_hdr_size; 841*3a2d8a1bSPaul Dagnelie 84244cb6abcSbmc static int arc_no_grow; /* Don't try to grow cache size */ 84344cb6abcSbmc static uint64_t arc_tempreserve; 8442fdbea25SAleksandr Guzovskiy static uint64_t arc_loaned_bytes; 845fa9e4066Sahrens 846fa9e4066Sahrens typedef struct arc_callback arc_callback_t; 847fa9e4066Sahrens 848fa9e4066Sahrens struct arc_callback { 849fa9e4066Sahrens void *acb_private; 850c717a561Smaybee arc_done_func_t *acb_done; 851fa9e4066Sahrens arc_buf_t *acb_buf; 8525602294fSDan Kimmel boolean_t acb_compressed; 853fa9e4066Sahrens zio_t *acb_zio_dummy; 854fa9e4066Sahrens arc_callback_t *acb_next; 855fa9e4066Sahrens }; 856fa9e4066Sahrens 857c717a561Smaybee typedef struct arc_write_callback arc_write_callback_t; 858c717a561Smaybee 859c717a561Smaybee struct arc_write_callback { 860c717a561Smaybee void *awcb_private; 861c717a561Smaybee arc_done_func_t *awcb_ready; 8628df0bcf0SPaul Dagnelie arc_done_func_t *awcb_children_ready; 86369962b56SMatthew Ahrens arc_done_func_t *awcb_physdone; 864c717a561Smaybee arc_done_func_t *awcb_done; 865c717a561Smaybee arc_buf_t *awcb_buf; 866c717a561Smaybee }; 867c717a561Smaybee 86889c86e32SChris Williamson /* 86989c86e32SChris Williamson * ARC buffers are separated into multiple structs as a memory saving measure: 87089c86e32SChris Williamson * - Common fields struct, always defined, and embedded within it: 87189c86e32SChris Williamson * - L2-only fields, always allocated but undefined when not in L2ARC 87289c86e32SChris Williamson * - L1-only fields, only allocated when in L1ARC 87389c86e32SChris Williamson * 87489c86e32SChris Williamson * Buffer in L1 Buffer only in L2 87589c86e32SChris Williamson * +------------------------+ +------------------------+ 87689c86e32SChris Williamson * | arc_buf_hdr_t | | arc_buf_hdr_t | 87789c86e32SChris Williamson * | | | | 87889c86e32SChris Williamson * | | | | 87989c86e32SChris Williamson * | | | | 88089c86e32SChris Williamson * +------------------------+ +------------------------+ 88189c86e32SChris Williamson * | l2arc_buf_hdr_t | | l2arc_buf_hdr_t | 88289c86e32SChris Williamson * | (undefined if L1-only) | | | 88389c86e32SChris Williamson * +------------------------+ +------------------------+ 88489c86e32SChris Williamson * | l1arc_buf_hdr_t | 88589c86e32SChris Williamson * | | 88689c86e32SChris Williamson * | | 88789c86e32SChris Williamson * | | 88889c86e32SChris Williamson * | | 88989c86e32SChris Williamson * +------------------------+ 89089c86e32SChris Williamson * 89189c86e32SChris Williamson * Because it's possible for the L2ARC to become extremely large, we can wind 89289c86e32SChris Williamson * up eating a lot of memory in L2ARC buffer headers, so the size of a header 89389c86e32SChris Williamson * is minimized by only allocating the fields necessary for an L1-cached buffer 89489c86e32SChris Williamson * when a header is actually in the L1 cache. The sub-headers (l1arc_buf_hdr and 89589c86e32SChris Williamson * l2arc_buf_hdr) are embedded rather than allocated separately to save a couple 89689c86e32SChris Williamson * words in pointers. arc_hdr_realloc() is used to switch a header between 89789c86e32SChris Williamson * these two allocation states. 89889c86e32SChris Williamson */ 89989c86e32SChris Williamson typedef struct l1arc_buf_hdr { 9006b4acc8bSahrens kmutex_t b_freeze_lock; 901dcbf3bd6SGeorge Wilson zio_cksum_t *b_freeze_cksum; 90289c86e32SChris Williamson #ifdef ZFS_DEBUG 90389c86e32SChris Williamson /* 9045602294fSDan Kimmel * Used for debugging with kmem_flags - by allocating and freeing 90589c86e32SChris Williamson * b_thawed when the buffer is thawed, we get a record of the stack 90689c86e32SChris Williamson * trace that thawed it. 90789c86e32SChris Williamson */ 9083f9d6ad7SLin Ling void *b_thawed; 90989c86e32SChris Williamson #endif 9106b4acc8bSahrens 911fa9e4066Sahrens arc_buf_t *b_buf; 912dcbf3bd6SGeorge Wilson uint32_t b_bufcnt; 91389c86e32SChris Williamson /* for waiting on writes to complete */ 914ad23a2dbSjohansen kcondvar_t b_cv; 915dcbf3bd6SGeorge Wilson uint8_t b_byteswap; 916ad23a2dbSjohansen 917fa9e4066Sahrens /* protected by arc state mutex */ 918fa9e4066Sahrens arc_state_t *b_state; 919244781f1SPrakash Surya multilist_node_t b_arc_node; 920fa9e4066Sahrens 921fa9e4066Sahrens /* updated atomically */ 922fa9e4066Sahrens clock_t b_arc_access; 923fa9e4066Sahrens 924fa9e4066Sahrens /* self protecting */ 925fa9e4066Sahrens refcount_t b_refcnt; 926fa94a07fSbrendan 92789c86e32SChris Williamson arc_callback_t *b_acb; 928770499e1SDan Kimmel abd_t *b_pabd; 92989c86e32SChris Williamson } l1arc_buf_hdr_t; 93089c86e32SChris Williamson 93189c86e32SChris Williamson typedef struct l2arc_dev l2arc_dev_t; 93289c86e32SChris Williamson 93389c86e32SChris Williamson typedef struct l2arc_buf_hdr { 93489c86e32SChris Williamson /* protected by arc_buf_hdr mutex */ 93589c86e32SChris Williamson l2arc_dev_t *b_dev; /* L2ARC device */ 93689c86e32SChris Williamson uint64_t b_daddr; /* disk address, offset byte */ 93789c86e32SChris Williamson 938fa94a07fSbrendan list_node_t b_l2node; 93989c86e32SChris Williamson } l2arc_buf_hdr_t; 94089c86e32SChris Williamson 94189c86e32SChris Williamson struct arc_buf_hdr { 94289c86e32SChris Williamson /* protected by hash lock */ 94389c86e32SChris Williamson dva_t b_dva; 94489c86e32SChris Williamson uint64_t b_birth; 94589c86e32SChris Williamson 946dcbf3bd6SGeorge Wilson arc_buf_contents_t b_type; 94789c86e32SChris Williamson arc_buf_hdr_t *b_hash_next; 94889c86e32SChris Williamson arc_flags_t b_flags; 94989c86e32SChris Williamson 950dcbf3bd6SGeorge Wilson /* 951dcbf3bd6SGeorge Wilson * This field stores the size of the data buffer after 952dcbf3bd6SGeorge Wilson * compression, and is set in the arc's zio completion handlers. 953dcbf3bd6SGeorge Wilson * It is in units of SPA_MINBLOCKSIZE (e.g. 1 == 512 bytes). 954dcbf3bd6SGeorge Wilson * 955dcbf3bd6SGeorge Wilson * While the block pointers can store up to 32MB in their psize 956dcbf3bd6SGeorge Wilson * field, we can only store up to 32MB minus 512B. This is due 957dcbf3bd6SGeorge Wilson * to the bp using a bias of 1, whereas we use a bias of 0 (i.e. 958dcbf3bd6SGeorge Wilson * a field of zeros represents 512B in the bp). We can't use a 959dcbf3bd6SGeorge Wilson * bias of 1 since we need to reserve a psize of zero, here, to 960dcbf3bd6SGeorge Wilson * represent holes and embedded blocks. 961dcbf3bd6SGeorge Wilson * 962dcbf3bd6SGeorge Wilson * This isn't a problem in practice, since the maximum size of a 963dcbf3bd6SGeorge Wilson * buffer is limited to 16MB, so we never need to store 32MB in 964dcbf3bd6SGeorge Wilson * this field. Even in the upstream illumos code base, the 965dcbf3bd6SGeorge Wilson * maximum size of a buffer is limited to 16MB. 966dcbf3bd6SGeorge Wilson */ 967dcbf3bd6SGeorge Wilson uint16_t b_psize; 968dcbf3bd6SGeorge Wilson 969dcbf3bd6SGeorge Wilson /* 970dcbf3bd6SGeorge Wilson * This field stores the size of the data buffer before 971dcbf3bd6SGeorge Wilson * compression, and cannot change once set. It is in units 972dcbf3bd6SGeorge Wilson * of SPA_MINBLOCKSIZE (e.g. 2 == 1024 bytes) 973dcbf3bd6SGeorge Wilson */ 974dcbf3bd6SGeorge Wilson uint16_t b_lsize; /* immutable */ 975dcbf3bd6SGeorge Wilson uint64_t b_spa; /* immutable */ 97689c86e32SChris Williamson 97789c86e32SChris Williamson /* L2ARC fields. Undefined when not in L2ARC. */ 97889c86e32SChris Williamson l2arc_buf_hdr_t b_l2hdr; 97989c86e32SChris Williamson /* L1ARC fields. Undefined when in l2arc_only state */ 98089c86e32SChris Williamson l1arc_buf_hdr_t b_l1hdr; 981fa9e4066Sahrens }; 982fa9e4066Sahrens 983ea8dc4b6Seschrock #define GHOST_STATE(state) \ 984fa94a07fSbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 985fa94a07fSbrendan (state) == arc_l2c_only) 986ea8dc4b6Seschrock 9877adb730bSGeorge Wilson #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) 9887adb730bSGeorge Wilson #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) 9897adb730bSGeorge Wilson #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) 9907adb730bSGeorge Wilson #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) 991dcbf3bd6SGeorge Wilson #define HDR_COMPRESSION_ENABLED(hdr) \ 992dcbf3bd6SGeorge Wilson ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC) 99389c86e32SChris Williamson 9947adb730bSGeorge Wilson #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) 9957adb730bSGeorge Wilson #define HDR_L2_READING(hdr) \ 996dcbf3bd6SGeorge Wilson (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ 997dcbf3bd6SGeorge Wilson ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) 9987adb730bSGeorge Wilson #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) 9997adb730bSGeorge Wilson #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) 10007adb730bSGeorge Wilson #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) 1001dcbf3bd6SGeorge Wilson #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA) 1002fa9e4066Sahrens 100389c86e32SChris Williamson #define HDR_ISTYPE_METADATA(hdr) \ 1004dcbf3bd6SGeorge Wilson ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) 100589c86e32SChris Williamson #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) 100689c86e32SChris Williamson 100789c86e32SChris Williamson #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) 100889c86e32SChris Williamson #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) 100989c86e32SChris Williamson 1010dcbf3bd6SGeorge Wilson /* For storing compression mode in b_flags */ 1011dcbf3bd6SGeorge Wilson #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1) 1012dcbf3bd6SGeorge Wilson 1013dcbf3bd6SGeorge Wilson #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \ 1014dcbf3bd6SGeorge Wilson HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS)) 1015dcbf3bd6SGeorge Wilson #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \ 1016dcbf3bd6SGeorge Wilson HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp)); 1017dcbf3bd6SGeorge Wilson 1018dcbf3bd6SGeorge Wilson #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL) 10195602294fSDan Kimmel #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED) 10205602294fSDan Kimmel #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED) 1021dcbf3bd6SGeorge Wilson 1022e6c728e1Sbrendan /* 1023e6c728e1Sbrendan * Other sizes 1024e6c728e1Sbrendan */ 1025e6c728e1Sbrendan 102689c86e32SChris Williamson #define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 102789c86e32SChris Williamson #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) 1028e6c728e1Sbrendan 1029fa9e4066Sahrens /* 1030fa9e4066Sahrens * Hash table routines 1031fa9e4066Sahrens */ 1032fa9e4066Sahrens 1033fa9e4066Sahrens #define HT_LOCK_PAD 64 1034fa9e4066Sahrens 1035fa9e4066Sahrens struct ht_lock { 1036fa9e4066Sahrens kmutex_t ht_lock; 1037fa9e4066Sahrens #ifdef _KERNEL 1038fa9e4066Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 1039fa9e4066Sahrens #endif 1040fa9e4066Sahrens }; 1041fa9e4066Sahrens 1042fa9e4066Sahrens #define BUF_LOCKS 256 1043fa9e4066Sahrens typedef struct buf_hash_table { 1044fa9e4066Sahrens uint64_t ht_mask; 1045fa9e4066Sahrens arc_buf_hdr_t **ht_table; 1046fa9e4066Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 1047fa9e4066Sahrens } buf_hash_table_t; 1048fa9e4066Sahrens 1049fa9e4066Sahrens static buf_hash_table_t buf_hash_table; 1050fa9e4066Sahrens 1051fa9e4066Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 1052fa9e4066Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 1053fa9e4066Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 1054fa9e4066Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 10553f9d6ad7SLin Ling #define HDR_LOCK(hdr) \ 10563f9d6ad7SLin Ling (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 1057fa9e4066Sahrens 1058fa9e4066Sahrens uint64_t zfs_crc64_table[256]; 1059fa9e4066Sahrens 1060fa94a07fSbrendan /* 1061fa94a07fSbrendan * Level 2 ARC 1062fa94a07fSbrendan */ 1063fa94a07fSbrendan 1064fa94a07fSbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 1065aad02571SSaso Kiselkov #define L2ARC_HEADROOM 2 /* num of writes */ 1066aad02571SSaso Kiselkov /* 1067aad02571SSaso Kiselkov * If we discover during ARC scan any buffers to be compressed, we boost 1068aad02571SSaso Kiselkov * our headroom for the next scanning cycle by this percentage multiple. 1069aad02571SSaso Kiselkov */ 1070aad02571SSaso Kiselkov #define L2ARC_HEADROOM_BOOST 200 10715a98e54bSBrendan Gregg - Sun Microsystems #define L2ARC_FEED_SECS 1 /* caching interval secs */ 10725a98e54bSBrendan Gregg - Sun Microsystems #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 1073fa94a07fSbrendan 1074fa94a07fSbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 1075fa94a07fSbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 1076fa94a07fSbrendan 1077f7170741SWill Andrews /* L2ARC Performance Tunables */ 1078fa94a07fSbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 10793a737e0dSbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 1080fa94a07fSbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 1081aad02571SSaso Kiselkov uint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 1082fa94a07fSbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 10835a98e54bSBrendan Gregg - Sun Microsystems uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 1084fa94a07fSbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 10855a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 10865a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 1087fa94a07fSbrendan 1088fa94a07fSbrendan /* 1089fa94a07fSbrendan * L2ARC Internals 1090fa94a07fSbrendan */ 109189c86e32SChris Williamson struct l2arc_dev { 1092fa94a07fSbrendan vdev_t *l2ad_vdev; /* vdev */ 1093fa94a07fSbrendan spa_t *l2ad_spa; /* spa */ 1094fa94a07fSbrendan uint64_t l2ad_hand; /* next write location */ 1095fa94a07fSbrendan uint64_t l2ad_start; /* first addr on device */ 1096fa94a07fSbrendan uint64_t l2ad_end; /* last addr on device */ 1097fa94a07fSbrendan boolean_t l2ad_first; /* first sweep through */ 10985a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2ad_writing; /* currently writing */ 109989c86e32SChris Williamson kmutex_t l2ad_mtx; /* lock for buffer list */ 110089c86e32SChris Williamson list_t l2ad_buflist; /* buffer list */ 1101fa94a07fSbrendan list_node_t l2ad_node; /* device list node */ 1102a52fc310SPrakash Surya refcount_t l2ad_alloc; /* allocated bytes */ 110389c86e32SChris Williamson }; 1104fa94a07fSbrendan 1105fa94a07fSbrendan static list_t L2ARC_dev_list; /* device list */ 1106fa94a07fSbrendan static list_t *l2arc_dev_list; /* device list pointer */ 1107fa94a07fSbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 1108fa94a07fSbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 1109fa94a07fSbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 1110fa94a07fSbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 1111fa94a07fSbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 1112fa94a07fSbrendan static uint64_t l2arc_ndev; /* number of devices */ 1113fa94a07fSbrendan 1114fa94a07fSbrendan typedef struct l2arc_read_callback { 11155602294fSDan Kimmel arc_buf_hdr_t *l2rcb_hdr; /* read header */ 1116aad02571SSaso Kiselkov blkptr_t l2rcb_bp; /* original blkptr */ 11177802d7bfSMatthew Ahrens zbookmark_phys_t l2rcb_zb; /* original bookmark */ 1118aad02571SSaso Kiselkov int l2rcb_flags; /* original flags */ 1119403a8da7SAndriy Gapon abd_t *l2rcb_abd; /* temporary buffer */ 1120fa94a07fSbrendan } l2arc_read_callback_t; 1121fa94a07fSbrendan 1122fa94a07fSbrendan typedef struct l2arc_write_callback { 1123fa94a07fSbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 1124fa94a07fSbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 1125fa94a07fSbrendan } l2arc_write_callback_t; 1126fa94a07fSbrendan 1127fa94a07fSbrendan typedef struct l2arc_data_free { 1128fa94a07fSbrendan /* protected by l2arc_free_on_write_mtx */ 1129770499e1SDan Kimmel abd_t *l2df_abd; 1130fa94a07fSbrendan size_t l2df_size; 1131dcbf3bd6SGeorge Wilson arc_buf_contents_t l2df_type; 1132fa94a07fSbrendan list_node_t l2df_list_node; 1133fa94a07fSbrendan } l2arc_data_free_t; 1134fa94a07fSbrendan 1135fa94a07fSbrendan static kmutex_t l2arc_feed_thr_lock; 1136fa94a07fSbrendan static kcondvar_t l2arc_feed_thr_cv; 1137fa94a07fSbrendan static uint8_t l2arc_thread_exit; 1138fa94a07fSbrendan 1139770499e1SDan Kimmel static abd_t *arc_get_data_abd(arc_buf_hdr_t *, uint64_t, void *); 1140dcbf3bd6SGeorge Wilson static void *arc_get_data_buf(arc_buf_hdr_t *, uint64_t, void *); 1141770499e1SDan Kimmel static void arc_get_data_impl(arc_buf_hdr_t *, uint64_t, void *); 1142770499e1SDan Kimmel static void arc_free_data_abd(arc_buf_hdr_t *, abd_t *, uint64_t, void *); 1143dcbf3bd6SGeorge Wilson static void arc_free_data_buf(arc_buf_hdr_t *, void *, uint64_t, void *); 1144770499e1SDan Kimmel static void arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag); 1145770499e1SDan Kimmel static void arc_hdr_free_pabd(arc_buf_hdr_t *); 1146770499e1SDan Kimmel static void arc_hdr_alloc_pabd(arc_buf_hdr_t *); 11477adb730bSGeorge Wilson static void arc_access(arc_buf_hdr_t *, kmutex_t *); 1148244781f1SPrakash Surya static boolean_t arc_is_overflowing(); 11497adb730bSGeorge Wilson static void arc_buf_watch(arc_buf_t *); 11507adb730bSGeorge Wilson 115189c86e32SChris Williamson static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); 115289c86e32SChris Williamson static uint32_t arc_bufc_to_flags(arc_buf_contents_t); 1153dcbf3bd6SGeorge Wilson static inline void arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); 1154dcbf3bd6SGeorge Wilson static inline void arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); 115589c86e32SChris Williamson 11567adb730bSGeorge Wilson static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); 11577adb730bSGeorge Wilson static void l2arc_read_done(zio_t *); 1158fa94a07fSbrendan 1159*3a2d8a1bSPaul Dagnelie 1160*3a2d8a1bSPaul Dagnelie /* 1161*3a2d8a1bSPaul Dagnelie * We use Cityhash for this. It's fast, and has good hash properties without 1162*3a2d8a1bSPaul Dagnelie * requiring any large static buffers. 1163*3a2d8a1bSPaul Dagnelie */ 1164fa9e4066Sahrens static uint64_t 1165ac05c741SMark Maybee buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 1166fa9e4066Sahrens { 1167*3a2d8a1bSPaul Dagnelie return (cityhash4(spa, dva->dva_word[0], dva->dva_word[1], birth)); 1168fa9e4066Sahrens } 1169fa9e4066Sahrens 1170dcbf3bd6SGeorge Wilson #define HDR_EMPTY(hdr) \ 1171dcbf3bd6SGeorge Wilson ((hdr)->b_dva.dva_word[0] == 0 && \ 1172dcbf3bd6SGeorge Wilson (hdr)->b_dva.dva_word[1] == 0) 1173fa9e4066Sahrens 1174dcbf3bd6SGeorge Wilson #define HDR_EQUAL(spa, dva, birth, hdr) \ 1175dcbf3bd6SGeorge Wilson ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 1176dcbf3bd6SGeorge Wilson ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 1177dcbf3bd6SGeorge Wilson ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa) 1178fa9e4066Sahrens 11793f9d6ad7SLin Ling static void 11803f9d6ad7SLin Ling buf_discard_identity(arc_buf_hdr_t *hdr) 11813f9d6ad7SLin Ling { 11823f9d6ad7SLin Ling hdr->b_dva.dva_word[0] = 0; 11833f9d6ad7SLin Ling hdr->b_dva.dva_word[1] = 0; 11843f9d6ad7SLin Ling hdr->b_birth = 0; 11853f9d6ad7SLin Ling } 11863f9d6ad7SLin Ling 1187fa9e4066Sahrens static arc_buf_hdr_t * 11885d7b4d43SMatthew Ahrens buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 1189fa9e4066Sahrens { 11905d7b4d43SMatthew Ahrens const dva_t *dva = BP_IDENTITY(bp); 11915d7b4d43SMatthew Ahrens uint64_t birth = BP_PHYSICAL_BIRTH(bp); 1192fa9e4066Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 1193fa9e4066Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 11947adb730bSGeorge Wilson arc_buf_hdr_t *hdr; 1195fa9e4066Sahrens 1196fa9e4066Sahrens mutex_enter(hash_lock); 11977adb730bSGeorge Wilson for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; 11987adb730bSGeorge Wilson hdr = hdr->b_hash_next) { 1199dcbf3bd6SGeorge Wilson if (HDR_EQUAL(spa, dva, birth, hdr)) { 1200fa9e4066Sahrens *lockp = hash_lock; 12017adb730bSGeorge Wilson return (hdr); 1202fa9e4066Sahrens } 1203fa9e4066Sahrens } 1204fa9e4066Sahrens mutex_exit(hash_lock); 1205fa9e4066Sahrens *lockp = NULL; 1206fa9e4066Sahrens return (NULL); 1207fa9e4066Sahrens } 1208fa9e4066Sahrens 1209fa9e4066Sahrens /* 1210fa9e4066Sahrens * Insert an entry into the hash table. If there is already an element 1211fa9e4066Sahrens * equal to elem in the hash table, then the already existing element 1212fa9e4066Sahrens * will be returned and the new element will not be inserted. 1213fa9e4066Sahrens * Otherwise returns NULL. 121489c86e32SChris Williamson * If lockp == NULL, the caller is assumed to already hold the hash lock. 1215fa9e4066Sahrens */ 1216fa9e4066Sahrens static arc_buf_hdr_t * 12177adb730bSGeorge Wilson buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) 1218fa9e4066Sahrens { 12197adb730bSGeorge Wilson uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1220fa9e4066Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 12217adb730bSGeorge Wilson arc_buf_hdr_t *fhdr; 122244cb6abcSbmc uint32_t i; 1223fa9e4066Sahrens 12247adb730bSGeorge Wilson ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); 12257adb730bSGeorge Wilson ASSERT(hdr->b_birth != 0); 12267adb730bSGeorge Wilson ASSERT(!HDR_IN_HASH_TABLE(hdr)); 122789c86e32SChris Williamson 122889c86e32SChris Williamson if (lockp != NULL) { 122989c86e32SChris Williamson *lockp = hash_lock; 123089c86e32SChris Williamson mutex_enter(hash_lock); 123189c86e32SChris Williamson } else { 123289c86e32SChris Williamson ASSERT(MUTEX_HELD(hash_lock)); 123389c86e32SChris Williamson } 123489c86e32SChris Williamson 12357adb730bSGeorge Wilson for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; 12367adb730bSGeorge Wilson fhdr = fhdr->b_hash_next, i++) { 1237dcbf3bd6SGeorge Wilson if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) 12387adb730bSGeorge Wilson return (fhdr); 1239fa9e4066Sahrens } 1240fa9e4066Sahrens 12417adb730bSGeorge Wilson hdr->b_hash_next = buf_hash_table.ht_table[idx]; 12427adb730bSGeorge Wilson buf_hash_table.ht_table[idx] = hdr; 1243dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 1244fa9e4066Sahrens 1245fa9e4066Sahrens /* collect some hash table performance data */ 1246fa9e4066Sahrens if (i > 0) { 124744cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_collisions); 1248fa9e4066Sahrens if (i == 1) 124944cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_chains); 125044cb6abcSbmc 125144cb6abcSbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 1252fa9e4066Sahrens } 125344cb6abcSbmc 125444cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_elements); 125544cb6abcSbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 1256fa9e4066Sahrens 1257fa9e4066Sahrens return (NULL); 1258fa9e4066Sahrens } 1259fa9e4066Sahrens 1260fa9e4066Sahrens static void 12617adb730bSGeorge Wilson buf_hash_remove(arc_buf_hdr_t *hdr) 1262fa9e4066Sahrens { 12637adb730bSGeorge Wilson arc_buf_hdr_t *fhdr, **hdrp; 12647adb730bSGeorge Wilson uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1265fa9e4066Sahrens 1266fa9e4066Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 12677adb730bSGeorge Wilson ASSERT(HDR_IN_HASH_TABLE(hdr)); 1268fa9e4066Sahrens 12697adb730bSGeorge Wilson hdrp = &buf_hash_table.ht_table[idx]; 12707adb730bSGeorge Wilson while ((fhdr = *hdrp) != hdr) { 1271dcbf3bd6SGeorge Wilson ASSERT3P(fhdr, !=, NULL); 12727adb730bSGeorge Wilson hdrp = &fhdr->b_hash_next; 1273fa9e4066Sahrens } 12747adb730bSGeorge Wilson *hdrp = hdr->b_hash_next; 12757adb730bSGeorge Wilson hdr->b_hash_next = NULL; 1276dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 1277fa9e4066Sahrens 1278fa9e4066Sahrens /* collect some hash table performance data */ 127944cb6abcSbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 128044cb6abcSbmc 1281fa9e4066Sahrens if (buf_hash_table.ht_table[idx] && 1282fa9e4066Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 128344cb6abcSbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1284fa9e4066Sahrens } 1285fa9e4066Sahrens 1286fa9e4066Sahrens /* 1287fa9e4066Sahrens * Global data structures and functions for the buf kmem cache. 1288fa9e4066Sahrens */ 128989c86e32SChris Williamson static kmem_cache_t *hdr_full_cache; 129089c86e32SChris Williamson static kmem_cache_t *hdr_l2only_cache; 1291fa9e4066Sahrens static kmem_cache_t *buf_cache; 1292fa9e4066Sahrens 1293fa9e4066Sahrens static void 1294fa9e4066Sahrens buf_fini(void) 1295fa9e4066Sahrens { 1296fa9e4066Sahrens int i; 1297fa9e4066Sahrens 1298fa9e4066Sahrens kmem_free(buf_hash_table.ht_table, 1299fa9e4066Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1300fa9e4066Sahrens for (i = 0; i < BUF_LOCKS; i++) 1301fa9e4066Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 130289c86e32SChris Williamson kmem_cache_destroy(hdr_full_cache); 130389c86e32SChris Williamson kmem_cache_destroy(hdr_l2only_cache); 1304fa9e4066Sahrens kmem_cache_destroy(buf_cache); 1305fa9e4066Sahrens } 1306fa9e4066Sahrens 1307fa9e4066Sahrens /* 1308fa9e4066Sahrens * Constructor callback - called when the cache is empty 1309fa9e4066Sahrens * and a new buf is requested. 1310fa9e4066Sahrens */ 1311fa9e4066Sahrens /* ARGSUSED */ 1312fa9e4066Sahrens static int 131389c86e32SChris Williamson hdr_full_cons(void *vbuf, void *unused, int kmflag) 1314fa9e4066Sahrens { 13157adb730bSGeorge Wilson arc_buf_hdr_t *hdr = vbuf; 1316fa9e4066Sahrens 131789c86e32SChris Williamson bzero(hdr, HDR_FULL_SIZE); 131889c86e32SChris Williamson cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); 131989c86e32SChris Williamson refcount_create(&hdr->b_l1hdr.b_refcnt); 132089c86e32SChris Williamson mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1321244781f1SPrakash Surya multilist_link_init(&hdr->b_l1hdr.b_arc_node); 132289c86e32SChris Williamson arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); 132389c86e32SChris Williamson 132489c86e32SChris Williamson return (0); 132589c86e32SChris Williamson } 132689c86e32SChris Williamson 132789c86e32SChris Williamson /* ARGSUSED */ 132889c86e32SChris Williamson static int 132989c86e32SChris Williamson hdr_l2only_cons(void *vbuf, void *unused, int kmflag) 133089c86e32SChris Williamson { 133189c86e32SChris Williamson arc_buf_hdr_t *hdr = vbuf; 133289c86e32SChris Williamson 133389c86e32SChris Williamson bzero(hdr, HDR_L2ONLY_SIZE); 133489c86e32SChris Williamson arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1335fa94a07fSbrendan 1336fa9e4066Sahrens return (0); 1337fa9e4066Sahrens } 1338fa9e4066Sahrens 13396f83844dSMark Maybee /* ARGSUSED */ 13406f83844dSMark Maybee static int 13416f83844dSMark Maybee buf_cons(void *vbuf, void *unused, int kmflag) 13426f83844dSMark Maybee { 13436f83844dSMark Maybee arc_buf_t *buf = vbuf; 13446f83844dSMark Maybee 13456f83844dSMark Maybee bzero(buf, sizeof (arc_buf_t)); 13463f9d6ad7SLin Ling mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 13475a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 13485a98e54bSBrendan Gregg - Sun Microsystems 13496f83844dSMark Maybee return (0); 13506f83844dSMark Maybee } 13516f83844dSMark Maybee 1352fa9e4066Sahrens /* 1353fa9e4066Sahrens * Destructor callback - called when a cached buf is 1354fa9e4066Sahrens * no longer required. 1355fa9e4066Sahrens */ 1356fa9e4066Sahrens /* ARGSUSED */ 1357fa9e4066Sahrens static void 135889c86e32SChris Williamson hdr_full_dest(void *vbuf, void *unused) 135989c86e32SChris Williamson { 136089c86e32SChris Williamson arc_buf_hdr_t *hdr = vbuf; 136189c86e32SChris Williamson 1362dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 136389c86e32SChris Williamson cv_destroy(&hdr->b_l1hdr.b_cv); 136489c86e32SChris Williamson refcount_destroy(&hdr->b_l1hdr.b_refcnt); 136589c86e32SChris Williamson mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); 1366244781f1SPrakash Surya ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 136789c86e32SChris Williamson arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); 136889c86e32SChris Williamson } 136989c86e32SChris Williamson 137089c86e32SChris Williamson /* ARGSUSED */ 137189c86e32SChris Williamson static void 137289c86e32SChris Williamson hdr_l2only_dest(void *vbuf, void *unused) 1373fa9e4066Sahrens { 13747adb730bSGeorge Wilson arc_buf_hdr_t *hdr = vbuf; 1375fa9e4066Sahrens 1376dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 137789c86e32SChris Williamson arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1378fa9e4066Sahrens } 1379fa9e4066Sahrens 13806f83844dSMark Maybee /* ARGSUSED */ 13816f83844dSMark Maybee static void 13826f83844dSMark Maybee buf_dest(void *vbuf, void *unused) 13836f83844dSMark Maybee { 13846f83844dSMark Maybee arc_buf_t *buf = vbuf; 13856f83844dSMark Maybee 13863f9d6ad7SLin Ling mutex_destroy(&buf->b_evict_lock); 13875a98e54bSBrendan Gregg - Sun Microsystems arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 13886f83844dSMark Maybee } 13896f83844dSMark Maybee 1390fa9e4066Sahrens /* 1391fa9e4066Sahrens * Reclaim callback -- invoked when memory is low. 1392fa9e4066Sahrens */ 1393fa9e4066Sahrens /* ARGSUSED */ 1394fa9e4066Sahrens static void 1395fa9e4066Sahrens hdr_recl(void *unused) 1396fa9e4066Sahrens { 1397fa9e4066Sahrens dprintf("hdr_recl called\n"); 139849e3519aSmaybee /* 139949e3519aSmaybee * umem calls the reclaim func when we destroy the buf cache, 140049e3519aSmaybee * which is after we do arc_fini(). 140149e3519aSmaybee */ 140249e3519aSmaybee if (!arc_dead) 1403244781f1SPrakash Surya cv_signal(&arc_reclaim_thread_cv); 1404fa9e4066Sahrens } 1405fa9e4066Sahrens 1406fa9e4066Sahrens static void 1407fa9e4066Sahrens buf_init(void) 1408fa9e4066Sahrens { 1409fa9e4066Sahrens uint64_t *ct; 1410ea8dc4b6Seschrock uint64_t hsize = 1ULL << 12; 1411fa9e4066Sahrens int i, j; 1412fa9e4066Sahrens 1413fa9e4066Sahrens /* 1414fa9e4066Sahrens * The hash table is big enough to fill all of physical memory 141563e911b6SMatthew Ahrens * with an average block size of zfs_arc_average_blocksize (default 8K). 141663e911b6SMatthew Ahrens * By default, the table will take up 141763e911b6SMatthew Ahrens * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1418fa9e4066Sahrens */ 141963e911b6SMatthew Ahrens while (hsize * zfs_arc_average_blocksize < physmem * PAGESIZE) 1420fa9e4066Sahrens hsize <<= 1; 1421ea8dc4b6Seschrock retry: 1422fa9e4066Sahrens buf_hash_table.ht_mask = hsize - 1; 1423ea8dc4b6Seschrock buf_hash_table.ht_table = 1424ea8dc4b6Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1425ea8dc4b6Seschrock if (buf_hash_table.ht_table == NULL) { 1426ea8dc4b6Seschrock ASSERT(hsize > (1ULL << 8)); 1427ea8dc4b6Seschrock hsize >>= 1; 1428ea8dc4b6Seschrock goto retry; 1429ea8dc4b6Seschrock } 1430fa9e4066Sahrens 143189c86e32SChris Williamson hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, 143289c86e32SChris Williamson 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0); 143389c86e32SChris Williamson hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", 143489c86e32SChris Williamson HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl, 143589c86e32SChris Williamson NULL, NULL, 0); 1436fa9e4066Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 14376f83844dSMark Maybee 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1438fa9e4066Sahrens 1439fa9e4066Sahrens for (i = 0; i < 256; i++) 1440fa9e4066Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1441fa9e4066Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1442fa9e4066Sahrens 1443fa9e4066Sahrens for (i = 0; i < BUF_LOCKS; i++) { 1444fa9e4066Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1445fa9e4066Sahrens NULL, MUTEX_DEFAULT, NULL); 1446fa9e4066Sahrens } 1447fa9e4066Sahrens } 1448fa9e4066Sahrens 14495602294fSDan Kimmel /* 14505602294fSDan Kimmel * This is the size that the buf occupies in memory. If the buf is compressed, 14515602294fSDan Kimmel * it will correspond to the compressed size. You should use this method of 14525602294fSDan Kimmel * getting the buf size unless you explicitly need the logical size. 14535602294fSDan Kimmel */ 14545602294fSDan Kimmel int32_t 14555602294fSDan Kimmel arc_buf_size(arc_buf_t *buf) 14565602294fSDan Kimmel { 14575602294fSDan Kimmel return (ARC_BUF_COMPRESSED(buf) ? 14585602294fSDan Kimmel HDR_GET_PSIZE(buf->b_hdr) : HDR_GET_LSIZE(buf->b_hdr)); 14595602294fSDan Kimmel } 14605602294fSDan Kimmel 14615602294fSDan Kimmel int32_t 14625602294fSDan Kimmel arc_buf_lsize(arc_buf_t *buf) 14635602294fSDan Kimmel { 14645602294fSDan Kimmel return (HDR_GET_LSIZE(buf->b_hdr)); 14655602294fSDan Kimmel } 14665602294fSDan Kimmel 14675602294fSDan Kimmel enum zio_compress 14685602294fSDan Kimmel arc_get_compression(arc_buf_t *buf) 14695602294fSDan Kimmel { 14705602294fSDan Kimmel return (ARC_BUF_COMPRESSED(buf) ? 14715602294fSDan Kimmel HDR_GET_COMPRESS(buf->b_hdr) : ZIO_COMPRESS_OFF); 14725602294fSDan Kimmel } 14735602294fSDan Kimmel 1474dcbf3bd6SGeorge Wilson #define ARC_MINTIME (hz>>4) /* 62 ms */ 1475244781f1SPrakash Surya 1476dcbf3bd6SGeorge Wilson static inline boolean_t 1477dcbf3bd6SGeorge Wilson arc_buf_is_shared(arc_buf_t *buf) 1478dcbf3bd6SGeorge Wilson { 1479dcbf3bd6SGeorge Wilson boolean_t shared = (buf->b_data != NULL && 1480770499e1SDan Kimmel buf->b_hdr->b_l1hdr.b_pabd != NULL && 1481770499e1SDan Kimmel abd_is_linear(buf->b_hdr->b_l1hdr.b_pabd) && 1482770499e1SDan Kimmel buf->b_data == abd_to_buf(buf->b_hdr->b_l1hdr.b_pabd)); 1483dcbf3bd6SGeorge Wilson IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr)); 14845602294fSDan Kimmel IMPLY(shared, ARC_BUF_SHARED(buf)); 14855602294fSDan Kimmel IMPLY(shared, ARC_BUF_COMPRESSED(buf) || ARC_BUF_LAST(buf)); 14865602294fSDan Kimmel 14875602294fSDan Kimmel /* 14885602294fSDan Kimmel * It would be nice to assert arc_can_share() too, but the "hdr isn't 14895602294fSDan Kimmel * already being shared" requirement prevents us from doing that. 14905602294fSDan Kimmel */ 14915602294fSDan Kimmel 1492dcbf3bd6SGeorge Wilson return (shared); 1493dcbf3bd6SGeorge Wilson } 1494c546f36aSArne Jansen 14955602294fSDan Kimmel /* 14965602294fSDan Kimmel * Free the checksum associated with this header. If there is no checksum, this 14975602294fSDan Kimmel * is a no-op. 14985602294fSDan Kimmel */ 1499dcbf3bd6SGeorge Wilson static inline void 1500dcbf3bd6SGeorge Wilson arc_cksum_free(arc_buf_hdr_t *hdr) 1501dcbf3bd6SGeorge Wilson { 1502dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1503dcbf3bd6SGeorge Wilson mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1504dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_freeze_cksum != NULL) { 1505dcbf3bd6SGeorge Wilson kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t)); 1506dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_freeze_cksum = NULL; 150789c86e32SChris Williamson } 1508dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 150989c86e32SChris Williamson } 151089c86e32SChris Williamson 15115602294fSDan Kimmel /* 15125602294fSDan Kimmel * Return true iff at least one of the bufs on hdr is not compressed. 15135602294fSDan Kimmel */ 15145602294fSDan Kimmel static boolean_t 15155602294fSDan Kimmel arc_hdr_has_uncompressed_buf(arc_buf_hdr_t *hdr) 15165602294fSDan Kimmel { 15175602294fSDan Kimmel for (arc_buf_t *b = hdr->b_l1hdr.b_buf; b != NULL; b = b->b_next) { 15185602294fSDan Kimmel if (!ARC_BUF_COMPRESSED(b)) { 15195602294fSDan Kimmel return (B_TRUE); 15205602294fSDan Kimmel } 15215602294fSDan Kimmel } 15225602294fSDan Kimmel return (B_FALSE); 15235602294fSDan Kimmel } 15245602294fSDan Kimmel 15255602294fSDan Kimmel /* 15265602294fSDan Kimmel * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data 15275602294fSDan Kimmel * matches the checksum that is stored in the hdr. If there is no checksum, 15285602294fSDan Kimmel * or if the buf is compressed, this is a no-op. 15295602294fSDan Kimmel */ 15306b4acc8bSahrens static void 15316b4acc8bSahrens arc_cksum_verify(arc_buf_t *buf) 15326b4acc8bSahrens { 1533dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 15346b4acc8bSahrens zio_cksum_t zc; 15356b4acc8bSahrens 1536cc60fd72Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 15376b4acc8bSahrens return; 15386b4acc8bSahrens 15395602294fSDan Kimmel if (ARC_BUF_COMPRESSED(buf)) { 15405602294fSDan Kimmel ASSERT(hdr->b_l1hdr.b_freeze_cksum == NULL || 15415602294fSDan Kimmel arc_hdr_has_uncompressed_buf(hdr)); 15425602294fSDan Kimmel return; 15435602294fSDan Kimmel } 15445602294fSDan Kimmel 1545dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1546dcbf3bd6SGeorge Wilson 1547dcbf3bd6SGeorge Wilson mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1548dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) { 1549dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 15506b4acc8bSahrens return; 15516b4acc8bSahrens } 15525602294fSDan Kimmel 15535602294fSDan Kimmel fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, &zc); 1554dcbf3bd6SGeorge Wilson if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc)) 15556b4acc8bSahrens panic("buffer modified while frozen!"); 1556dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 15576b4acc8bSahrens } 15586b4acc8bSahrens 1559dcbf3bd6SGeorge Wilson static boolean_t 1560dcbf3bd6SGeorge Wilson arc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio) 1561fa94a07fSbrendan { 1562dcbf3bd6SGeorge Wilson enum zio_compress compress = BP_GET_COMPRESS(zio->io_bp); 1563dcbf3bd6SGeorge Wilson boolean_t valid_cksum; 1564fa94a07fSbrendan 1565dcbf3bd6SGeorge Wilson ASSERT(!BP_IS_EMBEDDED(zio->io_bp)); 1566dcbf3bd6SGeorge Wilson VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr)); 1567dcbf3bd6SGeorge Wilson 1568dcbf3bd6SGeorge Wilson /* 1569dcbf3bd6SGeorge Wilson * We rely on the blkptr's checksum to determine if the block 1570dcbf3bd6SGeorge Wilson * is valid or not. When compressed arc is enabled, the l2arc 1571dcbf3bd6SGeorge Wilson * writes the block to the l2arc just as it appears in the pool. 1572dcbf3bd6SGeorge Wilson * This allows us to use the blkptr's checksum to validate the 1573dcbf3bd6SGeorge Wilson * data that we just read off of the l2arc without having to store 1574dcbf3bd6SGeorge Wilson * a separate checksum in the arc_buf_hdr_t. However, if compressed 1575dcbf3bd6SGeorge Wilson * arc is disabled, then the data written to the l2arc is always 1576dcbf3bd6SGeorge Wilson * uncompressed and won't match the block as it exists in the main 1577dcbf3bd6SGeorge Wilson * pool. When this is the case, we must first compress it if it is 1578dcbf3bd6SGeorge Wilson * compressed on the main pool before we can validate the checksum. 1579dcbf3bd6SGeorge Wilson */ 1580dcbf3bd6SGeorge Wilson if (!HDR_COMPRESSION_ENABLED(hdr) && compress != ZIO_COMPRESS_OFF) { 1581dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 1582dcbf3bd6SGeorge Wilson uint64_t lsize = HDR_GET_LSIZE(hdr); 1583dcbf3bd6SGeorge Wilson uint64_t csize; 1584dcbf3bd6SGeorge Wilson 158501a059eeSRoman Strashkin abd_t *cdata = abd_alloc_linear(HDR_GET_PSIZE(hdr), B_TRUE); 158601a059eeSRoman Strashkin csize = zio_compress_data(compress, zio->io_abd, 158701a059eeSRoman Strashkin abd_to_buf(cdata), lsize); 1588770499e1SDan Kimmel 1589dcbf3bd6SGeorge Wilson ASSERT3U(csize, <=, HDR_GET_PSIZE(hdr)); 1590dcbf3bd6SGeorge Wilson if (csize < HDR_GET_PSIZE(hdr)) { 1591dcbf3bd6SGeorge Wilson /* 1592dcbf3bd6SGeorge Wilson * Compressed blocks are always a multiple of the 1593dcbf3bd6SGeorge Wilson * smallest ashift in the pool. Ideally, we would 1594dcbf3bd6SGeorge Wilson * like to round up the csize to the next 1595dcbf3bd6SGeorge Wilson * spa_min_ashift but that value may have changed 1596dcbf3bd6SGeorge Wilson * since the block was last written. Instead, 1597dcbf3bd6SGeorge Wilson * we rely on the fact that the hdr's psize 1598dcbf3bd6SGeorge Wilson * was set to the psize of the block when it was 1599dcbf3bd6SGeorge Wilson * last written. We set the csize to that value 1600dcbf3bd6SGeorge Wilson * and zero out any part that should not contain 1601dcbf3bd6SGeorge Wilson * data. 1602dcbf3bd6SGeorge Wilson */ 160301a059eeSRoman Strashkin abd_zero_off(cdata, csize, HDR_GET_PSIZE(hdr) - csize); 1604dcbf3bd6SGeorge Wilson csize = HDR_GET_PSIZE(hdr); 1605dcbf3bd6SGeorge Wilson } 160601a059eeSRoman Strashkin zio_push_transform(zio, cdata, csize, HDR_GET_PSIZE(hdr), NULL); 1607dcbf3bd6SGeorge Wilson } 1608fa94a07fSbrendan 1609dcbf3bd6SGeorge Wilson /* 1610dcbf3bd6SGeorge Wilson * Block pointers always store the checksum for the logical data. 1611dcbf3bd6SGeorge Wilson * If the block pointer has the gang bit set, then the checksum 1612dcbf3bd6SGeorge Wilson * it represents is for the reconstituted data and not for an 1613dcbf3bd6SGeorge Wilson * individual gang member. The zio pipeline, however, must be able to 1614dcbf3bd6SGeorge Wilson * determine the checksum of each of the gang constituents so it 1615dcbf3bd6SGeorge Wilson * treats the checksum comparison differently than what we need 1616dcbf3bd6SGeorge Wilson * for l2arc blocks. This prevents us from using the 1617dcbf3bd6SGeorge Wilson * zio_checksum_error() interface directly. Instead we must call the 1618dcbf3bd6SGeorge Wilson * zio_checksum_error_impl() so that we can ensure the checksum is 1619dcbf3bd6SGeorge Wilson * generated using the correct checksum algorithm and accounts for the 1620dcbf3bd6SGeorge Wilson * logical I/O size and not just a gang fragment. 1621dcbf3bd6SGeorge Wilson */ 1622dcbf3bd6SGeorge Wilson valid_cksum = (zio_checksum_error_impl(zio->io_spa, zio->io_bp, 1623770499e1SDan Kimmel BP_GET_CHECKSUM(zio->io_bp), zio->io_abd, zio->io_size, 1624dcbf3bd6SGeorge Wilson zio->io_offset, NULL) == 0); 1625dcbf3bd6SGeorge Wilson zio_pop_transforms(zio); 1626dcbf3bd6SGeorge Wilson return (valid_cksum); 1627fa94a07fSbrendan } 1628fa94a07fSbrendan 16295602294fSDan Kimmel /* 16305602294fSDan Kimmel * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a 16315602294fSDan Kimmel * checksum and attaches it to the buf's hdr so that we can ensure that the buf 16325602294fSDan Kimmel * isn't modified later on. If buf is compressed or there is already a checksum 16335602294fSDan Kimmel * on the hdr, this is a no-op (we only checksum uncompressed bufs). 16345602294fSDan Kimmel */ 16356b4acc8bSahrens static void 1636dcbf3bd6SGeorge Wilson arc_cksum_compute(arc_buf_t *buf) 16376b4acc8bSahrens { 1638dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 1639dcbf3bd6SGeorge Wilson 1640dcbf3bd6SGeorge Wilson if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 16416b4acc8bSahrens return; 16426b4acc8bSahrens 1643dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 16445602294fSDan Kimmel 164589c86e32SChris Williamson mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1646dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_freeze_cksum != NULL) { 16475602294fSDan Kimmel ASSERT(arc_hdr_has_uncompressed_buf(hdr)); 16485602294fSDan Kimmel mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 16495602294fSDan Kimmel return; 16505602294fSDan Kimmel } else if (ARC_BUF_COMPRESSED(buf)) { 1651dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 16526b4acc8bSahrens return; 16536b4acc8bSahrens } 16545602294fSDan Kimmel 16555602294fSDan Kimmel ASSERT(!ARC_BUF_COMPRESSED(buf)); 1656dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), 1657dcbf3bd6SGeorge Wilson KM_SLEEP); 16585602294fSDan Kimmel fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, 1659dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_freeze_cksum); 1660dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1661cd1c8b85SMatthew Ahrens arc_buf_watch(buf); 1662cd1c8b85SMatthew Ahrens } 1663cd1c8b85SMatthew Ahrens 1664cd1c8b85SMatthew Ahrens #ifndef _KERNEL 1665cd1c8b85SMatthew Ahrens typedef struct procctl { 1666cd1c8b85SMatthew Ahrens long cmd; 1667cd1c8b85SMatthew Ahrens prwatch_t prwatch; 1668cd1c8b85SMatthew Ahrens } procctl_t; 1669cd1c8b85SMatthew Ahrens #endif 1670cd1c8b85SMatthew Ahrens 1671cd1c8b85SMatthew Ahrens /* ARGSUSED */ 1672cd1c8b85SMatthew Ahrens static void 1673cd1c8b85SMatthew Ahrens arc_buf_unwatch(arc_buf_t *buf) 1674cd1c8b85SMatthew Ahrens { 1675cd1c8b85SMatthew Ahrens #ifndef _KERNEL 1676cd1c8b85SMatthew Ahrens if (arc_watch) { 1677cd1c8b85SMatthew Ahrens int result; 1678cd1c8b85SMatthew Ahrens procctl_t ctl; 1679cd1c8b85SMatthew Ahrens ctl.cmd = PCWATCH; 1680cd1c8b85SMatthew Ahrens ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1681cd1c8b85SMatthew Ahrens ctl.prwatch.pr_size = 0; 1682cd1c8b85SMatthew Ahrens ctl.prwatch.pr_wflags = 0; 1683cd1c8b85SMatthew Ahrens result = write(arc_procfd, &ctl, sizeof (ctl)); 1684cd1c8b85SMatthew Ahrens ASSERT3U(result, ==, sizeof (ctl)); 1685cd1c8b85SMatthew Ahrens } 1686cd1c8b85SMatthew Ahrens #endif 1687cd1c8b85SMatthew Ahrens } 1688cd1c8b85SMatthew Ahrens 1689cd1c8b85SMatthew Ahrens /* ARGSUSED */ 1690cd1c8b85SMatthew Ahrens static void 1691cd1c8b85SMatthew Ahrens arc_buf_watch(arc_buf_t *buf) 1692cd1c8b85SMatthew Ahrens { 1693cd1c8b85SMatthew Ahrens #ifndef _KERNEL 1694cd1c8b85SMatthew Ahrens if (arc_watch) { 1695cd1c8b85SMatthew Ahrens int result; 1696cd1c8b85SMatthew Ahrens procctl_t ctl; 1697cd1c8b85SMatthew Ahrens ctl.cmd = PCWATCH; 1698cd1c8b85SMatthew Ahrens ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 16995602294fSDan Kimmel ctl.prwatch.pr_size = arc_buf_size(buf); 1700cd1c8b85SMatthew Ahrens ctl.prwatch.pr_wflags = WA_WRITE; 1701cd1c8b85SMatthew Ahrens result = write(arc_procfd, &ctl, sizeof (ctl)); 1702cd1c8b85SMatthew Ahrens ASSERT3U(result, ==, sizeof (ctl)); 1703cd1c8b85SMatthew Ahrens } 1704cd1c8b85SMatthew Ahrens #endif 17056b4acc8bSahrens } 17066b4acc8bSahrens 170789c86e32SChris Williamson static arc_buf_contents_t 170889c86e32SChris Williamson arc_buf_type(arc_buf_hdr_t *hdr) 170989c86e32SChris Williamson { 1710dcbf3bd6SGeorge Wilson arc_buf_contents_t type; 171189c86e32SChris Williamson if (HDR_ISTYPE_METADATA(hdr)) { 1712dcbf3bd6SGeorge Wilson type = ARC_BUFC_METADATA; 171389c86e32SChris Williamson } else { 1714dcbf3bd6SGeorge Wilson type = ARC_BUFC_DATA; 171589c86e32SChris Williamson } 1716dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 1717dcbf3bd6SGeorge Wilson return (type); 171889c86e32SChris Williamson } 171989c86e32SChris Williamson 17205602294fSDan Kimmel boolean_t 17215602294fSDan Kimmel arc_is_metadata(arc_buf_t *buf) 17225602294fSDan Kimmel { 17235602294fSDan Kimmel return (HDR_ISTYPE_METADATA(buf->b_hdr) != 0); 17245602294fSDan Kimmel } 17255602294fSDan Kimmel 172689c86e32SChris Williamson static uint32_t 172789c86e32SChris Williamson arc_bufc_to_flags(arc_buf_contents_t type) 172889c86e32SChris Williamson { 172989c86e32SChris Williamson switch (type) { 173089c86e32SChris Williamson case ARC_BUFC_DATA: 173189c86e32SChris Williamson /* metadata field is 0 if buffer contains normal data */ 173289c86e32SChris Williamson return (0); 173389c86e32SChris Williamson case ARC_BUFC_METADATA: 173489c86e32SChris Williamson return (ARC_FLAG_BUFC_METADATA); 173589c86e32SChris Williamson default: 173689c86e32SChris Williamson break; 173789c86e32SChris Williamson } 173889c86e32SChris Williamson panic("undefined ARC buffer type!"); 173989c86e32SChris Williamson return ((uint32_t)-1); 174089c86e32SChris Williamson } 174189c86e32SChris Williamson 17426b4acc8bSahrens void 17436b4acc8bSahrens arc_buf_thaw(arc_buf_t *buf) 17446b4acc8bSahrens { 1745dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 1746dcbf3bd6SGeorge Wilson 17475602294fSDan Kimmel ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 17485602294fSDan Kimmel ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 17495602294fSDan Kimmel 17505602294fSDan Kimmel arc_cksum_verify(buf); 17515602294fSDan Kimmel 17525602294fSDan Kimmel /* 17535602294fSDan Kimmel * Compressed buffers do not manipulate the b_freeze_cksum or 17545602294fSDan Kimmel * allocate b_thawed. 17555602294fSDan Kimmel */ 17565602294fSDan Kimmel if (ARC_BUF_COMPRESSED(buf)) { 17575602294fSDan Kimmel ASSERT(hdr->b_l1hdr.b_freeze_cksum == NULL || 17585602294fSDan Kimmel arc_hdr_has_uncompressed_buf(hdr)); 17595602294fSDan Kimmel return; 1760fa94a07fSbrendan } 17616b4acc8bSahrens 1762dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1763dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 17643f9d6ad7SLin Ling 1765dcbf3bd6SGeorge Wilson mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 176689c86e32SChris Williamson #ifdef ZFS_DEBUG 17673f9d6ad7SLin Ling if (zfs_flags & ZFS_DEBUG_MODIFY) { 1768dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_thawed != NULL) 1769dcbf3bd6SGeorge Wilson kmem_free(hdr->b_l1hdr.b_thawed, 1); 1770dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_thawed = kmem_alloc(1, KM_SLEEP); 17713f9d6ad7SLin Ling } 177289c86e32SChris Williamson #endif 17733f9d6ad7SLin Ling 1774dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1775cd1c8b85SMatthew Ahrens 1776cd1c8b85SMatthew Ahrens arc_buf_unwatch(buf); 17776b4acc8bSahrens } 17786b4acc8bSahrens 17796b4acc8bSahrens void 17806b4acc8bSahrens arc_buf_freeze(arc_buf_t *buf) 17816b4acc8bSahrens { 1782dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 17833f9d6ad7SLin Ling kmutex_t *hash_lock; 17843f9d6ad7SLin Ling 1785cc60fd72Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1786cc60fd72Sahrens return; 1787cc60fd72Sahrens 17885602294fSDan Kimmel if (ARC_BUF_COMPRESSED(buf)) { 17895602294fSDan Kimmel ASSERT(hdr->b_l1hdr.b_freeze_cksum == NULL || 17905602294fSDan Kimmel arc_hdr_has_uncompressed_buf(hdr)); 17915602294fSDan Kimmel return; 17925602294fSDan Kimmel } 17935602294fSDan Kimmel 1794dcbf3bd6SGeorge Wilson hash_lock = HDR_LOCK(hdr); 17953f9d6ad7SLin Ling mutex_enter(hash_lock); 17963f9d6ad7SLin Ling 1797dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1798dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_freeze_cksum != NULL || 1799dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_state == arc_anon); 1800dcbf3bd6SGeorge Wilson arc_cksum_compute(buf); 18013f9d6ad7SLin Ling mutex_exit(hash_lock); 18026b4acc8bSahrens } 18036b4acc8bSahrens 1804dcbf3bd6SGeorge Wilson /* 1805dcbf3bd6SGeorge Wilson * The arc_buf_hdr_t's b_flags should never be modified directly. Instead, 1806dcbf3bd6SGeorge Wilson * the following functions should be used to ensure that the flags are 1807dcbf3bd6SGeorge Wilson * updated in a thread-safe way. When manipulating the flags either 1808dcbf3bd6SGeorge Wilson * the hash_lock must be held or the hdr must be undiscoverable. This 1809dcbf3bd6SGeorge Wilson * ensures that we're not racing with any other threads when updating 1810dcbf3bd6SGeorge Wilson * the flags. 1811dcbf3bd6SGeorge Wilson */ 1812dcbf3bd6SGeorge Wilson static inline void 1813dcbf3bd6SGeorge Wilson arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) 1814dcbf3bd6SGeorge Wilson { 1815dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 1816dcbf3bd6SGeorge Wilson hdr->b_flags |= flags; 1817dcbf3bd6SGeorge Wilson } 1818dcbf3bd6SGeorge Wilson 1819dcbf3bd6SGeorge Wilson static inline void 1820dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) 1821dcbf3bd6SGeorge Wilson { 1822dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 1823dcbf3bd6SGeorge Wilson hdr->b_flags &= ~flags; 1824dcbf3bd6SGeorge Wilson } 1825dcbf3bd6SGeorge Wilson 1826dcbf3bd6SGeorge Wilson /* 1827dcbf3bd6SGeorge Wilson * Setting the compression bits in the arc_buf_hdr_t's b_flags is 1828dcbf3bd6SGeorge Wilson * done in a special way since we have to clear and set bits 1829dcbf3bd6SGeorge Wilson * at the same time. Consumers that wish to set the compression bits 1830dcbf3bd6SGeorge Wilson * must use this function to ensure that the flags are updated in 1831dcbf3bd6SGeorge Wilson * thread-safe manner. 1832dcbf3bd6SGeorge Wilson */ 1833fa9e4066Sahrens static void 1834dcbf3bd6SGeorge Wilson arc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp) 1835fa9e4066Sahrens { 1836dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 1837fa9e4066Sahrens 1838dcbf3bd6SGeorge Wilson /* 1839dcbf3bd6SGeorge Wilson * Holes and embedded blocks will always have a psize = 0 so 1840dcbf3bd6SGeorge Wilson * we ignore the compression of the blkptr and set the 1841dcbf3bd6SGeorge Wilson * arc_buf_hdr_t's compression to ZIO_COMPRESS_OFF. 1842dcbf3bd6SGeorge Wilson * Holes and embedded blocks remain anonymous so we don't 1843dcbf3bd6SGeorge Wilson * want to uncompress them. Mark them as uncompressed. 1844dcbf3bd6SGeorge Wilson */ 1845dcbf3bd6SGeorge Wilson if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) { 1846dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC); 1847dcbf3bd6SGeorge Wilson HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); 1848dcbf3bd6SGeorge Wilson ASSERT(!HDR_COMPRESSION_ENABLED(hdr)); 1849dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 1850dcbf3bd6SGeorge Wilson } else { 1851dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC); 1852dcbf3bd6SGeorge Wilson HDR_SET_COMPRESS(hdr, cmp); 1853dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp); 1854dcbf3bd6SGeorge Wilson ASSERT(HDR_COMPRESSION_ENABLED(hdr)); 1855dcbf3bd6SGeorge Wilson } 1856dcbf3bd6SGeorge Wilson } 1857244781f1SPrakash Surya 18585602294fSDan Kimmel /* 18595602294fSDan Kimmel * Looks for another buf on the same hdr which has the data decompressed, copies 18605602294fSDan Kimmel * from it, and returns true. If no such buf exists, returns false. 18615602294fSDan Kimmel */ 18625602294fSDan Kimmel static boolean_t 18635602294fSDan Kimmel arc_buf_try_copy_decompressed_data(arc_buf_t *buf) 18645602294fSDan Kimmel { 18655602294fSDan Kimmel arc_buf_hdr_t *hdr = buf->b_hdr; 18665602294fSDan Kimmel boolean_t copied = B_FALSE; 18675602294fSDan Kimmel 18685602294fSDan Kimmel ASSERT(HDR_HAS_L1HDR(hdr)); 18695602294fSDan Kimmel ASSERT3P(buf->b_data, !=, NULL); 18705602294fSDan Kimmel ASSERT(!ARC_BUF_COMPRESSED(buf)); 18715602294fSDan Kimmel 18725602294fSDan Kimmel for (arc_buf_t *from = hdr->b_l1hdr.b_buf; from != NULL; 18735602294fSDan Kimmel from = from->b_next) { 18745602294fSDan Kimmel /* can't use our own data buffer */ 18755602294fSDan Kimmel if (from == buf) { 18765602294fSDan Kimmel continue; 18775602294fSDan Kimmel } 18785602294fSDan Kimmel 18795602294fSDan Kimmel if (!ARC_BUF_COMPRESSED(from)) { 18805602294fSDan Kimmel bcopy(from->b_data, buf->b_data, arc_buf_size(buf)); 18815602294fSDan Kimmel copied = B_TRUE; 18825602294fSDan Kimmel break; 18835602294fSDan Kimmel } 18845602294fSDan Kimmel } 18855602294fSDan Kimmel 18865602294fSDan Kimmel /* 18875602294fSDan Kimmel * There were no decompressed bufs, so there should not be a 18885602294fSDan Kimmel * checksum on the hdr either. 18895602294fSDan Kimmel */ 18905602294fSDan Kimmel EQUIV(!copied, hdr->b_l1hdr.b_freeze_cksum == NULL); 18915602294fSDan Kimmel 18925602294fSDan Kimmel return (copied); 18935602294fSDan Kimmel } 18945602294fSDan Kimmel 18955602294fSDan Kimmel /* 18965602294fSDan Kimmel * Given a buf that has a data buffer attached to it, this function will 18975602294fSDan Kimmel * efficiently fill the buf with data of the specified compression setting from 18985602294fSDan Kimmel * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr 18995602294fSDan Kimmel * are already sharing a data buf, no copy is performed. 19005602294fSDan Kimmel * 19015602294fSDan Kimmel * If the buf is marked as compressed but uncompressed data was requested, this 19025602294fSDan Kimmel * will allocate a new data buffer for the buf, remove that flag, and fill the 19035602294fSDan Kimmel * buf with uncompressed data. You can't request a compressed buf on a hdr with 19045602294fSDan Kimmel * uncompressed data, and (since we haven't added support for it yet) if you 19055602294fSDan Kimmel * want compressed data your buf must already be marked as compressed and have 19065602294fSDan Kimmel * the correct-sized data buffer. 19075602294fSDan Kimmel */ 1908dcbf3bd6SGeorge Wilson static int 19095602294fSDan Kimmel arc_buf_fill(arc_buf_t *buf, boolean_t compressed) 1910dcbf3bd6SGeorge Wilson { 1911dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 19125602294fSDan Kimmel boolean_t hdr_compressed = (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF); 1913dcbf3bd6SGeorge Wilson dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap; 191489c86e32SChris Williamson 19155602294fSDan Kimmel ASSERT3P(buf->b_data, !=, NULL); 19165602294fSDan Kimmel IMPLY(compressed, hdr_compressed); 19175602294fSDan Kimmel IMPLY(compressed, ARC_BUF_COMPRESSED(buf)); 19185602294fSDan Kimmel 19195602294fSDan Kimmel if (hdr_compressed == compressed) { 19205602294fSDan Kimmel if (!arc_buf_is_shared(buf)) { 1921770499e1SDan Kimmel abd_copy_to_buf(buf->b_data, hdr->b_l1hdr.b_pabd, 19225602294fSDan Kimmel arc_buf_size(buf)); 19235602294fSDan Kimmel } 1924dcbf3bd6SGeorge Wilson } else { 19255602294fSDan Kimmel ASSERT(hdr_compressed); 19265602294fSDan Kimmel ASSERT(!compressed); 1927dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), !=, HDR_GET_PSIZE(hdr)); 19285602294fSDan Kimmel 19295602294fSDan Kimmel /* 19305602294fSDan Kimmel * If the buf is sharing its data with the hdr, unlink it and 19315602294fSDan Kimmel * allocate a new data buffer for the buf. 19325602294fSDan Kimmel */ 19335602294fSDan Kimmel if (arc_buf_is_shared(buf)) { 19345602294fSDan Kimmel ASSERT(ARC_BUF_COMPRESSED(buf)); 19355602294fSDan Kimmel 19365602294fSDan Kimmel /* We need to give the buf it's own b_data */ 19375602294fSDan Kimmel buf->b_flags &= ~ARC_BUF_FLAG_SHARED; 19385602294fSDan Kimmel buf->b_data = 19395602294fSDan Kimmel arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); 19405602294fSDan Kimmel arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 19415602294fSDan Kimmel 19425602294fSDan Kimmel /* Previously overhead was 0; just add new overhead */ 19435602294fSDan Kimmel ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); 19445602294fSDan Kimmel } else if (ARC_BUF_COMPRESSED(buf)) { 19455602294fSDan Kimmel /* We need to reallocate the buf's b_data */ 19465602294fSDan Kimmel arc_free_data_buf(hdr, buf->b_data, HDR_GET_PSIZE(hdr), 19475602294fSDan Kimmel buf); 19485602294fSDan Kimmel buf->b_data = 19495602294fSDan Kimmel arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); 19505602294fSDan Kimmel 19515602294fSDan Kimmel /* We increased the size of b_data; update overhead */ 19525602294fSDan Kimmel ARCSTAT_INCR(arcstat_overhead_size, 19535602294fSDan Kimmel HDR_GET_LSIZE(hdr) - HDR_GET_PSIZE(hdr)); 19545602294fSDan Kimmel } 19555602294fSDan Kimmel 19565602294fSDan Kimmel /* 19575602294fSDan Kimmel * Regardless of the buf's previous compression settings, it 19585602294fSDan Kimmel * should not be compressed at the end of this function. 19595602294fSDan Kimmel */ 19605602294fSDan Kimmel buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; 19615602294fSDan Kimmel 19625602294fSDan Kimmel /* 19635602294fSDan Kimmel * Try copying the data from another buf which already has a 19645602294fSDan Kimmel * decompressed version. If that's not possible, it's time to 19655602294fSDan Kimmel * bite the bullet and decompress the data from the hdr. 19665602294fSDan Kimmel */ 19675602294fSDan Kimmel if (arc_buf_try_copy_decompressed_data(buf)) { 19685602294fSDan Kimmel /* Skip byteswapping and checksumming (already done) */ 19695602294fSDan Kimmel ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, !=, NULL); 19705602294fSDan Kimmel return (0); 19715602294fSDan Kimmel } else { 19725602294fSDan Kimmel int error = zio_decompress_data(HDR_GET_COMPRESS(hdr), 1973770499e1SDan Kimmel hdr->b_l1hdr.b_pabd, buf->b_data, 19745602294fSDan Kimmel HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); 19755602294fSDan Kimmel 19765602294fSDan Kimmel /* 19775602294fSDan Kimmel * Absent hardware errors or software bugs, this should 19785602294fSDan Kimmel * be impossible, but log it anyway so we can debug it. 19795602294fSDan Kimmel */ 19805602294fSDan Kimmel if (error != 0) { 19815602294fSDan Kimmel zfs_dbgmsg( 19825602294fSDan Kimmel "hdr %p, compress %d, psize %d, lsize %d", 19835602294fSDan Kimmel hdr, HDR_GET_COMPRESS(hdr), 19845602294fSDan Kimmel HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); 19855602294fSDan Kimmel return (SET_ERROR(EIO)); 19865602294fSDan Kimmel } 1987ea8dc4b6Seschrock } 1988fa9e4066Sahrens } 19895602294fSDan Kimmel 19905602294fSDan Kimmel /* Byteswap the buf's data if necessary */ 1991dcbf3bd6SGeorge Wilson if (bswap != DMU_BSWAP_NUMFUNCS) { 1992dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 1993dcbf3bd6SGeorge Wilson ASSERT3U(bswap, <, DMU_BSWAP_NUMFUNCS); 1994dcbf3bd6SGeorge Wilson dmu_ot_byteswap[bswap].ob_func(buf->b_data, HDR_GET_LSIZE(hdr)); 1995dcbf3bd6SGeorge Wilson } 19965602294fSDan Kimmel 19975602294fSDan Kimmel /* Compute the hdr's checksum if necessary */ 1998dcbf3bd6SGeorge Wilson arc_cksum_compute(buf); 19995602294fSDan Kimmel 2000dcbf3bd6SGeorge Wilson return (0); 2001fa9e4066Sahrens } 2002fa9e4066Sahrens 20035602294fSDan Kimmel int 20045602294fSDan Kimmel arc_decompress(arc_buf_t *buf) 20055602294fSDan Kimmel { 20065602294fSDan Kimmel return (arc_buf_fill(buf, B_FALSE)); 20075602294fSDan Kimmel } 20085602294fSDan Kimmel 2009dcbf3bd6SGeorge Wilson /* 2010770499e1SDan Kimmel * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t. 2011dcbf3bd6SGeorge Wilson */ 2012dcbf3bd6SGeorge Wilson static uint64_t 2013dcbf3bd6SGeorge Wilson arc_hdr_size(arc_buf_hdr_t *hdr) 2014fa9e4066Sahrens { 2015dcbf3bd6SGeorge Wilson uint64_t size; 2016fa9e4066Sahrens 2017dcbf3bd6SGeorge Wilson if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && 2018dcbf3bd6SGeorge Wilson HDR_GET_PSIZE(hdr) > 0) { 2019dcbf3bd6SGeorge Wilson size = HDR_GET_PSIZE(hdr); 2020dcbf3bd6SGeorge Wilson } else { 2021dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0); 2022dcbf3bd6SGeorge Wilson size = HDR_GET_LSIZE(hdr); 2023dcbf3bd6SGeorge Wilson } 2024dcbf3bd6SGeorge Wilson return (size); 2025dcbf3bd6SGeorge Wilson } 2026fa9e4066Sahrens 2027dcbf3bd6SGeorge Wilson /* 2028dcbf3bd6SGeorge Wilson * Increment the amount of evictable space in the arc_state_t's refcount. 2029dcbf3bd6SGeorge Wilson * We account for the space used by the hdr and the arc buf individually 2030dcbf3bd6SGeorge Wilson * so that we can add and remove them from the refcount individually. 2031dcbf3bd6SGeorge Wilson */ 2032dcbf3bd6SGeorge Wilson static void 2033dcbf3bd6SGeorge Wilson arc_evictable_space_increment(arc_buf_hdr_t *hdr, arc_state_t *state) 2034dcbf3bd6SGeorge Wilson { 2035dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 2036244781f1SPrakash Surya 2037dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 20380e8c6158Smaybee 2039dcbf3bd6SGeorge Wilson if (GHOST_STATE(state)) { 2040dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 2041dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2042770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 20435602294fSDan Kimmel (void) refcount_add_many(&state->arcs_esize[type], 20445602294fSDan Kimmel HDR_GET_LSIZE(hdr), hdr); 2045dcbf3bd6SGeorge Wilson return; 2046dcbf3bd6SGeorge Wilson } 2047dcbf3bd6SGeorge Wilson 2048dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(state)); 2049770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 2050dcbf3bd6SGeorge Wilson (void) refcount_add_many(&state->arcs_esize[type], 2051dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 2052dcbf3bd6SGeorge Wilson } 2053dcbf3bd6SGeorge Wilson for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2054dcbf3bd6SGeorge Wilson buf = buf->b_next) { 20555602294fSDan Kimmel if (arc_buf_is_shared(buf)) 2056dcbf3bd6SGeorge Wilson continue; 20575602294fSDan Kimmel (void) refcount_add_many(&state->arcs_esize[type], 20585602294fSDan Kimmel arc_buf_size(buf), buf); 2059fa9e4066Sahrens } 2060fa9e4066Sahrens } 2061fa9e4066Sahrens 2062fa9e4066Sahrens /* 2063dcbf3bd6SGeorge Wilson * Decrement the amount of evictable space in the arc_state_t's refcount. 2064dcbf3bd6SGeorge Wilson * We account for the space used by the hdr and the arc buf individually 2065dcbf3bd6SGeorge Wilson * so that we can add and remove them from the refcount individually. 2066fa9e4066Sahrens */ 2067fa9e4066Sahrens static void 20685602294fSDan Kimmel arc_evictable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state) 2069fa9e4066Sahrens { 2070dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 2071dcbf3bd6SGeorge Wilson 2072dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2073dcbf3bd6SGeorge Wilson 2074dcbf3bd6SGeorge Wilson if (GHOST_STATE(state)) { 2075dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 2076dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2077770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2078dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 20795602294fSDan Kimmel HDR_GET_LSIZE(hdr), hdr); 2080dcbf3bd6SGeorge Wilson return; 2081dcbf3bd6SGeorge Wilson } 2082dcbf3bd6SGeorge Wilson 2083dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(state)); 2084770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 2085dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 2086dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 2087dcbf3bd6SGeorge Wilson } 2088dcbf3bd6SGeorge Wilson for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2089dcbf3bd6SGeorge Wilson buf = buf->b_next) { 20905602294fSDan Kimmel if (arc_buf_is_shared(buf)) 2091dcbf3bd6SGeorge Wilson continue; 2092dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 20935602294fSDan Kimmel arc_buf_size(buf), buf); 2094dcbf3bd6SGeorge Wilson } 2095dcbf3bd6SGeorge Wilson } 2096dcbf3bd6SGeorge Wilson 2097dcbf3bd6SGeorge Wilson /* 2098dcbf3bd6SGeorge Wilson * Add a reference to this hdr indicating that someone is actively 2099dcbf3bd6SGeorge Wilson * referencing that memory. When the refcount transitions from 0 to 1, 2100dcbf3bd6SGeorge Wilson * we remove it from the respective arc_state_t list to indicate that 2101dcbf3bd6SGeorge Wilson * it is not evictable. 2102dcbf3bd6SGeorge Wilson */ 2103dcbf3bd6SGeorge Wilson static void 2104dcbf3bd6SGeorge Wilson add_reference(arc_buf_hdr_t *hdr, void *tag) 2105dcbf3bd6SGeorge Wilson { 2106dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2107dcbf3bd6SGeorge Wilson if (!MUTEX_HELD(HDR_LOCK(hdr))) { 2108dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_state == arc_anon); 2109dcbf3bd6SGeorge Wilson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2110dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2111dcbf3bd6SGeorge Wilson } 2112dcbf3bd6SGeorge Wilson 2113dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2114dcbf3bd6SGeorge Wilson 2115dcbf3bd6SGeorge Wilson if ((refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && 2116dcbf3bd6SGeorge Wilson (state != arc_anon)) { 2117dcbf3bd6SGeorge Wilson /* We don't use the L2-only state list. */ 2118dcbf3bd6SGeorge Wilson if (state != arc_l2c_only) { 211994c2d0ebSMatthew Ahrens multilist_remove(state->arcs_list[arc_buf_type(hdr)], 2120dcbf3bd6SGeorge Wilson hdr); 21215602294fSDan Kimmel arc_evictable_space_decrement(hdr, state); 2122dcbf3bd6SGeorge Wilson } 2123dcbf3bd6SGeorge Wilson /* remove the prefetch flag if we get a reference */ 2124dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); 2125dcbf3bd6SGeorge Wilson } 2126dcbf3bd6SGeorge Wilson } 2127dcbf3bd6SGeorge Wilson 2128dcbf3bd6SGeorge Wilson /* 2129dcbf3bd6SGeorge Wilson * Remove a reference from this hdr. When the reference transitions from 2130dcbf3bd6SGeorge Wilson * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's 2131dcbf3bd6SGeorge Wilson * list making it eligible for eviction. 2132dcbf3bd6SGeorge Wilson */ 2133dcbf3bd6SGeorge Wilson static int 2134dcbf3bd6SGeorge Wilson remove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 2135dcbf3bd6SGeorge Wilson { 2136dcbf3bd6SGeorge Wilson int cnt; 2137dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2138dcbf3bd6SGeorge Wilson 2139dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2140dcbf3bd6SGeorge Wilson ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 2141dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(state)); 2142dcbf3bd6SGeorge Wilson 2143dcbf3bd6SGeorge Wilson /* 2144dcbf3bd6SGeorge Wilson * arc_l2c_only counts as a ghost state so we don't need to explicitly 2145dcbf3bd6SGeorge Wilson * check to prevent usage of the arc_l2c_only list. 2146dcbf3bd6SGeorge Wilson */ 2147dcbf3bd6SGeorge Wilson if (((cnt = refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && 2148dcbf3bd6SGeorge Wilson (state != arc_anon)) { 214994c2d0ebSMatthew Ahrens multilist_insert(state->arcs_list[arc_buf_type(hdr)], hdr); 2150dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); 2151dcbf3bd6SGeorge Wilson arc_evictable_space_increment(hdr, state); 2152dcbf3bd6SGeorge Wilson } 2153dcbf3bd6SGeorge Wilson return (cnt); 2154dcbf3bd6SGeorge Wilson } 2155dcbf3bd6SGeorge Wilson 2156dcbf3bd6SGeorge Wilson /* 2157dcbf3bd6SGeorge Wilson * Move the supplied buffer to the indicated state. The hash lock 2158dcbf3bd6SGeorge Wilson * for the buffer must be held by the caller. 2159dcbf3bd6SGeorge Wilson */ 2160dcbf3bd6SGeorge Wilson static void 2161dcbf3bd6SGeorge Wilson arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, 2162dcbf3bd6SGeorge Wilson kmutex_t *hash_lock) 2163dcbf3bd6SGeorge Wilson { 2164dcbf3bd6SGeorge Wilson arc_state_t *old_state; 2165dcbf3bd6SGeorge Wilson int64_t refcnt; 2166dcbf3bd6SGeorge Wilson uint32_t bufcnt; 2167dcbf3bd6SGeorge Wilson boolean_t update_old, update_new; 2168dcbf3bd6SGeorge Wilson arc_buf_contents_t buftype = arc_buf_type(hdr); 216989c86e32SChris Williamson 217089c86e32SChris Williamson /* 217189c86e32SChris Williamson * We almost always have an L1 hdr here, since we call arc_hdr_realloc() 217289c86e32SChris Williamson * in arc_read() when bringing a buffer out of the L2ARC. However, the 217389c86e32SChris Williamson * L1 hdr doesn't always exist when we change state to arc_anon before 217489c86e32SChris Williamson * destroying a header, in which case reallocating to add the L1 hdr is 217589c86e32SChris Williamson * pointless. 217689c86e32SChris Williamson */ 217789c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) { 217889c86e32SChris Williamson old_state = hdr->b_l1hdr.b_state; 217989c86e32SChris Williamson refcnt = refcount_count(&hdr->b_l1hdr.b_refcnt); 2180dcbf3bd6SGeorge Wilson bufcnt = hdr->b_l1hdr.b_bufcnt; 2181770499e1SDan Kimmel update_old = (bufcnt > 0 || hdr->b_l1hdr.b_pabd != NULL); 218289c86e32SChris Williamson } else { 218389c86e32SChris Williamson old_state = arc_l2c_only; 218489c86e32SChris Williamson refcnt = 0; 2185dcbf3bd6SGeorge Wilson bufcnt = 0; 2186dcbf3bd6SGeorge Wilson update_old = B_FALSE; 218789c86e32SChris Williamson } 2188dcbf3bd6SGeorge Wilson update_new = update_old; 2189fa9e4066Sahrens 2190fa9e4066Sahrens ASSERT(MUTEX_HELD(hash_lock)); 219169962b56SMatthew Ahrens ASSERT3P(new_state, !=, old_state); 2192dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(new_state) || bufcnt == 0); 2193dcbf3bd6SGeorge Wilson ASSERT(old_state != arc_anon || bufcnt <= 1); 2194fa9e4066Sahrens 2195fa9e4066Sahrens /* 2196fa9e4066Sahrens * If this buffer is evictable, transfer it from the 2197fa9e4066Sahrens * old state list to the new state list. 2198fa9e4066Sahrens */ 2199ea8dc4b6Seschrock if (refcnt == 0) { 220089c86e32SChris Williamson if (old_state != arc_anon && old_state != arc_l2c_only) { 220189c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 220294c2d0ebSMatthew Ahrens multilist_remove(old_state->arcs_list[buftype], hdr); 2203ea8dc4b6Seschrock 2204dcbf3bd6SGeorge Wilson if (GHOST_STATE(old_state)) { 2205dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 2206dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2207dcbf3bd6SGeorge Wilson update_old = B_TRUE; 2208ea8dc4b6Seschrock } 22095602294fSDan Kimmel arc_evictable_space_decrement(hdr, old_state); 2210fa9e4066Sahrens } 221189c86e32SChris Williamson if (new_state != arc_anon && new_state != arc_l2c_only) { 2212fa9e4066Sahrens 221389c86e32SChris Williamson /* 221489c86e32SChris Williamson * An L1 header always exists here, since if we're 221589c86e32SChris Williamson * moving to some L1-cached state (i.e. not l2c_only or 221689c86e32SChris Williamson * anonymous), we realloc the header to add an L1hdr 221789c86e32SChris Williamson * beforehand. 221889c86e32SChris Williamson */ 221989c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 222094c2d0ebSMatthew Ahrens multilist_insert(new_state->arcs_list[buftype], hdr); 2221ea8dc4b6Seschrock 2222ea8dc4b6Seschrock if (GHOST_STATE(new_state)) { 2223dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 2224dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2225dcbf3bd6SGeorge Wilson update_new = B_TRUE; 2226ea8dc4b6Seschrock } 2227dcbf3bd6SGeorge Wilson arc_evictable_space_increment(hdr, new_state); 2228fa9e4066Sahrens } 2229fa9e4066Sahrens } 2230fa9e4066Sahrens 2231dcbf3bd6SGeorge Wilson ASSERT(!HDR_EMPTY(hdr)); 22327adb730bSGeorge Wilson if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) 22337adb730bSGeorge Wilson buf_hash_remove(hdr); 2234fa9e4066Sahrens 223589c86e32SChris Williamson /* adjust state sizes (ignore arc_l2c_only) */ 22362fd872a7SPrakash Surya 2237dcbf3bd6SGeorge Wilson if (update_new && new_state != arc_l2c_only) { 22382fd872a7SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 22392fd872a7SPrakash Surya if (GHOST_STATE(new_state)) { 2240dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 22412fd872a7SPrakash Surya 22422fd872a7SPrakash Surya /* 2243dcbf3bd6SGeorge Wilson * When moving a header to a ghost state, we first 22442fd872a7SPrakash Surya * remove all arc buffers. Thus, we'll have a 2245dcbf3bd6SGeorge Wilson * bufcnt of zero, and no arc buffer to use for 22462fd872a7SPrakash Surya * the reference. As a result, we use the arc 22472fd872a7SPrakash Surya * header pointer for the reference. 22482fd872a7SPrakash Surya */ 22492fd872a7SPrakash Surya (void) refcount_add_many(&new_state->arcs_size, 2250dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr), hdr); 2251770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 22522fd872a7SPrakash Surya } else { 2253dcbf3bd6SGeorge Wilson uint32_t buffers = 0; 22542fd872a7SPrakash Surya 22552fd872a7SPrakash Surya /* 22562fd872a7SPrakash Surya * Each individual buffer holds a unique reference, 22572fd872a7SPrakash Surya * thus we must remove each of these references one 22582fd872a7SPrakash Surya * at a time. 22592fd872a7SPrakash Surya */ 22602fd872a7SPrakash Surya for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 22612fd872a7SPrakash Surya buf = buf->b_next) { 2262dcbf3bd6SGeorge Wilson ASSERT3U(bufcnt, !=, 0); 2263dcbf3bd6SGeorge Wilson buffers++; 2264dcbf3bd6SGeorge Wilson 2265dcbf3bd6SGeorge Wilson /* 2266dcbf3bd6SGeorge Wilson * When the arc_buf_t is sharing the data 2267dcbf3bd6SGeorge Wilson * block with the hdr, the owner of the 2268dcbf3bd6SGeorge Wilson * reference belongs to the hdr. Only 2269dcbf3bd6SGeorge Wilson * add to the refcount if the arc_buf_t is 2270dcbf3bd6SGeorge Wilson * not shared. 2271dcbf3bd6SGeorge Wilson */ 22725602294fSDan Kimmel if (arc_buf_is_shared(buf)) 2273dcbf3bd6SGeorge Wilson continue; 2274dcbf3bd6SGeorge Wilson 22752fd872a7SPrakash Surya (void) refcount_add_many(&new_state->arcs_size, 22765602294fSDan Kimmel arc_buf_size(buf), buf); 2277dcbf3bd6SGeorge Wilson } 2278dcbf3bd6SGeorge Wilson ASSERT3U(bufcnt, ==, buffers); 2279dcbf3bd6SGeorge Wilson 2280770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 2281dcbf3bd6SGeorge Wilson (void) refcount_add_many(&new_state->arcs_size, 2282dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 2283dcbf3bd6SGeorge Wilson } else { 2284dcbf3bd6SGeorge Wilson ASSERT(GHOST_STATE(old_state)); 22852fd872a7SPrakash Surya } 22862fd872a7SPrakash Surya } 22872fd872a7SPrakash Surya } 22882fd872a7SPrakash Surya 2289dcbf3bd6SGeorge Wilson if (update_old && old_state != arc_l2c_only) { 22902fd872a7SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 22912fd872a7SPrakash Surya if (GHOST_STATE(old_state)) { 2292dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 2293770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2294dcbf3bd6SGeorge Wilson 22952fd872a7SPrakash Surya /* 22962fd872a7SPrakash Surya * When moving a header off of a ghost state, 2297dcbf3bd6SGeorge Wilson * the header will not contain any arc buffers. 2298dcbf3bd6SGeorge Wilson * We use the arc header pointer for the reference 2299dcbf3bd6SGeorge Wilson * which is exactly what we did when we put the 2300dcbf3bd6SGeorge Wilson * header on the ghost state. 23012fd872a7SPrakash Surya */ 23022fd872a7SPrakash Surya 23032fd872a7SPrakash Surya (void) refcount_remove_many(&old_state->arcs_size, 2304dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr), hdr); 23052fd872a7SPrakash Surya } else { 2306dcbf3bd6SGeorge Wilson uint32_t buffers = 0; 23072fd872a7SPrakash Surya 23082fd872a7SPrakash Surya /* 23092fd872a7SPrakash Surya * Each individual buffer holds a unique reference, 23102fd872a7SPrakash Surya * thus we must remove each of these references one 23112fd872a7SPrakash Surya * at a time. 23122fd872a7SPrakash Surya */ 23132fd872a7SPrakash Surya for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 23142fd872a7SPrakash Surya buf = buf->b_next) { 23155602294fSDan Kimmel ASSERT3U(bufcnt, !=, 0); 2316dcbf3bd6SGeorge Wilson buffers++; 2317dcbf3bd6SGeorge Wilson 2318dcbf3bd6SGeorge Wilson /* 2319dcbf3bd6SGeorge Wilson * When the arc_buf_t is sharing the data 2320dcbf3bd6SGeorge Wilson * block with the hdr, the owner of the 2321dcbf3bd6SGeorge Wilson * reference belongs to the hdr. Only 2322dcbf3bd6SGeorge Wilson * add to the refcount if the arc_buf_t is 2323dcbf3bd6SGeorge Wilson * not shared. 2324dcbf3bd6SGeorge Wilson */ 23255602294fSDan Kimmel if (arc_buf_is_shared(buf)) 2326dcbf3bd6SGeorge Wilson continue; 2327dcbf3bd6SGeorge Wilson 23282fd872a7SPrakash Surya (void) refcount_remove_many( 23295602294fSDan Kimmel &old_state->arcs_size, arc_buf_size(buf), 2330dcbf3bd6SGeorge Wilson buf); 23312fd872a7SPrakash Surya } 2332dcbf3bd6SGeorge Wilson ASSERT3U(bufcnt, ==, buffers); 2333770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 2334dcbf3bd6SGeorge Wilson (void) refcount_remove_many( 2335dcbf3bd6SGeorge Wilson &old_state->arcs_size, arc_hdr_size(hdr), hdr); 23362fd872a7SPrakash Surya } 2337fa9e4066Sahrens } 23382fd872a7SPrakash Surya 233989c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) 234089c86e32SChris Williamson hdr->b_l1hdr.b_state = new_state; 2341fa94a07fSbrendan 234289c86e32SChris Williamson /* 234389c86e32SChris Williamson * L2 headers should never be on the L2 state list since they don't 234489c86e32SChris Williamson * have L1 headers allocated. 234589c86e32SChris Williamson */ 234694c2d0ebSMatthew Ahrens ASSERT(multilist_is_empty(arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && 234794c2d0ebSMatthew Ahrens multilist_is_empty(arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); 2348fa9e4066Sahrens } 2349fa9e4066Sahrens 23500e8c6158Smaybee void 23515a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(uint64_t space, arc_space_type_t type) 23520e8c6158Smaybee { 23535a98e54bSBrendan Gregg - Sun Microsystems ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 23545a98e54bSBrendan Gregg - Sun Microsystems 23555a98e54bSBrendan Gregg - Sun Microsystems switch (type) { 23565a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_DATA: 2357*3a2d8a1bSPaul Dagnelie aggsum_add(&astat_data_size, space); 23585a98e54bSBrendan Gregg - Sun Microsystems break; 23594076b1bfSPrakash Surya case ARC_SPACE_META: 2360*3a2d8a1bSPaul Dagnelie aggsum_add(&astat_metadata_size, space); 23614076b1bfSPrakash Surya break; 23625a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_OTHER: 2363*3a2d8a1bSPaul Dagnelie aggsum_add(&astat_other_size, space); 23645a98e54bSBrendan Gregg - Sun Microsystems break; 23655a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_HDRS: 2366*3a2d8a1bSPaul Dagnelie aggsum_add(&astat_hdr_size, space); 23675a98e54bSBrendan Gregg - Sun Microsystems break; 23685a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_L2HDRS: 2369*3a2d8a1bSPaul Dagnelie aggsum_add(&astat_l2_hdr_size, space); 23705a98e54bSBrendan Gregg - Sun Microsystems break; 23715a98e54bSBrendan Gregg - Sun Microsystems } 23725a98e54bSBrendan Gregg - Sun Microsystems 23734076b1bfSPrakash Surya if (type != ARC_SPACE_DATA) 2374*3a2d8a1bSPaul Dagnelie aggsum_add(&arc_meta_used, space); 23754076b1bfSPrakash Surya 2376*3a2d8a1bSPaul Dagnelie aggsum_add(&arc_size, space); 23770e8c6158Smaybee } 23780e8c6158Smaybee 23790e8c6158Smaybee void 23805a98e54bSBrendan Gregg - Sun Microsystems arc_space_return(uint64_t space, arc_space_type_t type) 23810e8c6158Smaybee { 23825a98e54bSBrendan Gregg - Sun Microsystems ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 23835a98e54bSBrendan Gregg - Sun Microsystems 23845a98e54bSBrendan Gregg - Sun Microsystems switch (type) { 23855a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_DATA: 2386*3a2d8a1bSPaul Dagnelie aggsum_add(&astat_data_size, -space); 23875a98e54bSBrendan Gregg - Sun Microsystems break; 23884076b1bfSPrakash Surya case ARC_SPACE_META: 2389*3a2d8a1bSPaul Dagnelie aggsum_add(&astat_metadata_size, -space); 23904076b1bfSPrakash Surya break; 23915a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_OTHER: 2392*3a2d8a1bSPaul Dagnelie aggsum_add(&astat_other_size, -space); 23935a98e54bSBrendan Gregg - Sun Microsystems break; 23945a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_HDRS: 2395*3a2d8a1bSPaul Dagnelie aggsum_add(&astat_hdr_size, -space); 23965a98e54bSBrendan Gregg - Sun Microsystems break; 23975a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_L2HDRS: 2398*3a2d8a1bSPaul Dagnelie aggsum_add(&astat_l2_hdr_size, -space); 23995a98e54bSBrendan Gregg - Sun Microsystems break; 24005a98e54bSBrendan Gregg - Sun Microsystems } 24015a98e54bSBrendan Gregg - Sun Microsystems 24024076b1bfSPrakash Surya if (type != ARC_SPACE_DATA) { 2403*3a2d8a1bSPaul Dagnelie ASSERT(aggsum_compare(&arc_meta_used, space) >= 0); 2404*3a2d8a1bSPaul Dagnelie /* 2405*3a2d8a1bSPaul Dagnelie * We use the upper bound here rather than the precise value 2406*3a2d8a1bSPaul Dagnelie * because the arc_meta_max value doesn't need to be 2407*3a2d8a1bSPaul Dagnelie * precise. It's only consumed by humans via arcstats. 2408*3a2d8a1bSPaul Dagnelie */ 2409*3a2d8a1bSPaul Dagnelie if (arc_meta_max < aggsum_upper_bound(&arc_meta_used)) 2410*3a2d8a1bSPaul Dagnelie arc_meta_max = aggsum_upper_bound(&arc_meta_used); 2411*3a2d8a1bSPaul Dagnelie aggsum_add(&arc_meta_used, -space); 24124076b1bfSPrakash Surya } 24134076b1bfSPrakash Surya 2414*3a2d8a1bSPaul Dagnelie ASSERT(aggsum_compare(&arc_size, space) >= 0); 2415*3a2d8a1bSPaul Dagnelie aggsum_add(&arc_size, -space); 24160e8c6158Smaybee } 24170e8c6158Smaybee 2418dcbf3bd6SGeorge Wilson /* 24195602294fSDan Kimmel * Given a hdr and a buf, returns whether that buf can share its b_data buffer 2420770499e1SDan Kimmel * with the hdr's b_pabd. 2421dcbf3bd6SGeorge Wilson */ 24225602294fSDan Kimmel static boolean_t 24235602294fSDan Kimmel arc_can_share(arc_buf_hdr_t *hdr, arc_buf_t *buf) 24245602294fSDan Kimmel { 24255602294fSDan Kimmel /* 24265602294fSDan Kimmel * The criteria for sharing a hdr's data are: 24275602294fSDan Kimmel * 1. the hdr's compression matches the buf's compression 24285602294fSDan Kimmel * 2. the hdr doesn't need to be byteswapped 24295602294fSDan Kimmel * 3. the hdr isn't already being shared 24305602294fSDan Kimmel * 4. the buf is either compressed or it is the last buf in the hdr list 24315602294fSDan Kimmel * 24325602294fSDan Kimmel * Criterion #4 maintains the invariant that shared uncompressed 24335602294fSDan Kimmel * bufs must be the final buf in the hdr's b_buf list. Reading this, you 24345602294fSDan Kimmel * might ask, "if a compressed buf is allocated first, won't that be the 24355602294fSDan Kimmel * last thing in the list?", but in that case it's impossible to create 24365602294fSDan Kimmel * a shared uncompressed buf anyway (because the hdr must be compressed 24375602294fSDan Kimmel * to have the compressed buf). You might also think that #3 is 24385602294fSDan Kimmel * sufficient to make this guarantee, however it's possible 24395602294fSDan Kimmel * (specifically in the rare L2ARC write race mentioned in 24405602294fSDan Kimmel * arc_buf_alloc_impl()) there will be an existing uncompressed buf that 24415602294fSDan Kimmel * is sharable, but wasn't at the time of its allocation. Rather than 24425602294fSDan Kimmel * allow a new shared uncompressed buf to be created and then shuffle 24435602294fSDan Kimmel * the list around to make it the last element, this simply disallows 24445602294fSDan Kimmel * sharing if the new buf isn't the first to be added. 24455602294fSDan Kimmel */ 24465602294fSDan Kimmel ASSERT3P(buf->b_hdr, ==, hdr); 24475602294fSDan Kimmel boolean_t hdr_compressed = HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF; 24485602294fSDan Kimmel boolean_t buf_compressed = ARC_BUF_COMPRESSED(buf) != 0; 24495602294fSDan Kimmel return (buf_compressed == hdr_compressed && 24505602294fSDan Kimmel hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS && 24515602294fSDan Kimmel !HDR_SHARED_DATA(hdr) && 24525602294fSDan Kimmel (ARC_BUF_LAST(buf) || ARC_BUF_COMPRESSED(buf))); 24535602294fSDan Kimmel } 24545602294fSDan Kimmel 24555602294fSDan Kimmel /* 24565602294fSDan Kimmel * Allocate a buf for this hdr. If you care about the data that's in the hdr, 24575602294fSDan Kimmel * or if you want a compressed buffer, pass those flags in. Returns 0 if the 24585602294fSDan Kimmel * copy was made successfully, or an error code otherwise. 24595602294fSDan Kimmel */ 24605602294fSDan Kimmel static int 24615602294fSDan Kimmel arc_buf_alloc_impl(arc_buf_hdr_t *hdr, void *tag, boolean_t compressed, 24625602294fSDan Kimmel boolean_t fill, arc_buf_t **ret) 2463fa9e4066Sahrens { 2464fa9e4066Sahrens arc_buf_t *buf; 2465fa9e4066Sahrens 2466dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2467dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); 2468dcbf3bd6SGeorge Wilson VERIFY(hdr->b_type == ARC_BUFC_DATA || 2469dcbf3bd6SGeorge Wilson hdr->b_type == ARC_BUFC_METADATA); 24705602294fSDan Kimmel ASSERT3P(ret, !=, NULL); 24715602294fSDan Kimmel ASSERT3P(*ret, ==, NULL); 2472dcbf3bd6SGeorge Wilson 24735602294fSDan Kimmel buf = *ret = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2474fa9e4066Sahrens buf->b_hdr = hdr; 247544eda4d7Smaybee buf->b_data = NULL; 24765602294fSDan Kimmel buf->b_next = hdr->b_l1hdr.b_buf; 24775602294fSDan Kimmel buf->b_flags = 0; 247889c86e32SChris Williamson 2479dcbf3bd6SGeorge Wilson add_reference(hdr, tag); 2480dcbf3bd6SGeorge Wilson 2481dcbf3bd6SGeorge Wilson /* 2482dcbf3bd6SGeorge Wilson * We're about to change the hdr's b_flags. We must either 2483dcbf3bd6SGeorge Wilson * hold the hash_lock or be undiscoverable. 2484dcbf3bd6SGeorge Wilson */ 2485dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2486dcbf3bd6SGeorge Wilson 2487dcbf3bd6SGeorge Wilson /* 24885602294fSDan Kimmel * Only honor requests for compressed bufs if the hdr is actually 24895602294fSDan Kimmel * compressed. 24905602294fSDan Kimmel */ 24915602294fSDan Kimmel if (compressed && HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) 24925602294fSDan Kimmel buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; 24935602294fSDan Kimmel 24945602294fSDan Kimmel /* 24955602294fSDan Kimmel * If the hdr's data can be shared then we share the data buffer and 24965602294fSDan Kimmel * set the appropriate bit in the hdr's b_flags to indicate the hdr is 2497770499e1SDan Kimmel * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new 24985602294fSDan Kimmel * buffer to store the buf's data. 24995602294fSDan Kimmel * 2500770499e1SDan Kimmel * There are two additional restrictions here because we're sharing 2501770499e1SDan Kimmel * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be 2502770499e1SDan Kimmel * actively involved in an L2ARC write, because if this buf is used by 2503770499e1SDan Kimmel * an arc_write() then the hdr's data buffer will be released when the 25045602294fSDan Kimmel * write completes, even though the L2ARC write might still be using it. 2505770499e1SDan Kimmel * Second, the hdr's ABD must be linear so that the buf's user doesn't 2506770499e1SDan Kimmel * need to be ABD-aware. 2507dcbf3bd6SGeorge Wilson */ 2508770499e1SDan Kimmel boolean_t can_share = arc_can_share(hdr, buf) && !HDR_L2_WRITING(hdr) && 2509770499e1SDan Kimmel abd_is_linear(hdr->b_l1hdr.b_pabd); 25105602294fSDan Kimmel 25115602294fSDan Kimmel /* Set up b_data and sharing */ 25125602294fSDan Kimmel if (can_share) { 2513770499e1SDan Kimmel buf->b_data = abd_to_buf(hdr->b_l1hdr.b_pabd); 25145602294fSDan Kimmel buf->b_flags |= ARC_BUF_FLAG_SHARED; 2515dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); 2516dcbf3bd6SGeorge Wilson } else { 25175602294fSDan Kimmel buf->b_data = 25185602294fSDan Kimmel arc_get_data_buf(hdr, arc_buf_size(buf), buf); 25195602294fSDan Kimmel ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); 2520dcbf3bd6SGeorge Wilson } 2521dcbf3bd6SGeorge Wilson VERIFY3P(buf->b_data, !=, NULL); 252289c86e32SChris Williamson 252389c86e32SChris Williamson hdr->b_l1hdr.b_buf = buf; 2524dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt += 1; 252589c86e32SChris Williamson 25265602294fSDan Kimmel /* 25275602294fSDan Kimmel * If the user wants the data from the hdr, we need to either copy or 25285602294fSDan Kimmel * decompress the data. 25295602294fSDan Kimmel */ 25305602294fSDan Kimmel if (fill) { 25315602294fSDan Kimmel return (arc_buf_fill(buf, ARC_BUF_COMPRESSED(buf) != 0)); 25325602294fSDan Kimmel } 2533dcbf3bd6SGeorge Wilson 25345602294fSDan Kimmel return (0); 25355602294fSDan Kimmel } 2536dcbf3bd6SGeorge Wilson 25375602294fSDan Kimmel static char *arc_onloan_tag = "onloan"; 2538fa9e4066Sahrens 25395602294fSDan Kimmel static inline void 25405602294fSDan Kimmel arc_loaned_bytes_update(int64_t delta) 25415602294fSDan Kimmel { 25425602294fSDan Kimmel atomic_add_64(&arc_loaned_bytes, delta); 2543dcbf3bd6SGeorge Wilson 25445602294fSDan Kimmel /* assert that it did not wrap around */ 25455602294fSDan Kimmel ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); 2546fa9e4066Sahrens } 2547fa9e4066Sahrens 25482fdbea25SAleksandr Guzovskiy /* 25492fdbea25SAleksandr Guzovskiy * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 25502fdbea25SAleksandr Guzovskiy * flight data by arc_tempreserve_space() until they are "returned". Loaned 25512fdbea25SAleksandr Guzovskiy * buffers must be returned to the arc before they can be used by the DMU or 25522fdbea25SAleksandr Guzovskiy * freed. 25532fdbea25SAleksandr Guzovskiy */ 25542fdbea25SAleksandr Guzovskiy arc_buf_t * 25555602294fSDan Kimmel arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size) 25562fdbea25SAleksandr Guzovskiy { 25575602294fSDan Kimmel arc_buf_t *buf = arc_alloc_buf(spa, arc_onloan_tag, 25585602294fSDan Kimmel is_metadata ? ARC_BUFC_METADATA : ARC_BUFC_DATA, size); 25595602294fSDan Kimmel 25605602294fSDan Kimmel arc_loaned_bytes_update(size); 25615602294fSDan Kimmel 25625602294fSDan Kimmel return (buf); 25635602294fSDan Kimmel } 25642fdbea25SAleksandr Guzovskiy 25655602294fSDan Kimmel arc_buf_t * 25665602294fSDan Kimmel arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize, 25675602294fSDan Kimmel enum zio_compress compression_type) 25685602294fSDan Kimmel { 25695602294fSDan Kimmel arc_buf_t *buf = arc_alloc_compressed_buf(spa, arc_onloan_tag, 25705602294fSDan Kimmel psize, lsize, compression_type); 25715602294fSDan Kimmel 25725602294fSDan Kimmel arc_loaned_bytes_update(psize); 25732fdbea25SAleksandr Guzovskiy 25742fdbea25SAleksandr Guzovskiy return (buf); 25752fdbea25SAleksandr Guzovskiy } 25762fdbea25SAleksandr Guzovskiy 25775602294fSDan Kimmel 25782fdbea25SAleksandr Guzovskiy /* 25792fdbea25SAleksandr Guzovskiy * Return a loaned arc buffer to the arc. 25802fdbea25SAleksandr Guzovskiy */ 25812fdbea25SAleksandr Guzovskiy void 25822fdbea25SAleksandr Guzovskiy arc_return_buf(arc_buf_t *buf, void *tag) 25832fdbea25SAleksandr Guzovskiy { 25842fdbea25SAleksandr Guzovskiy arc_buf_hdr_t *hdr = buf->b_hdr; 25852fdbea25SAleksandr Guzovskiy 2586dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, !=, NULL); 258789c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 258889c86e32SChris Williamson (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 258989c86e32SChris Williamson (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 25902fdbea25SAleksandr Guzovskiy 25915602294fSDan Kimmel arc_loaned_bytes_update(-arc_buf_size(buf)); 25922fdbea25SAleksandr Guzovskiy } 25932fdbea25SAleksandr Guzovskiy 2594c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /* Detach an arc_buf from a dbuf (tag) */ 2595c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void 2596c242f9a0Schunli zhang - Sun Microsystems - Irvine United States arc_loan_inuse_buf(arc_buf_t *buf, void *tag) 2597c242f9a0Schunli zhang - Sun Microsystems - Irvine United States { 259889c86e32SChris Williamson arc_buf_hdr_t *hdr = buf->b_hdr; 2599c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 2600dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, !=, NULL); 260189c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 260289c86e32SChris Williamson (void) refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 260389c86e32SChris Williamson (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); 2604c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 26055602294fSDan Kimmel arc_loaned_bytes_update(arc_buf_size(buf)); 2606c242f9a0Schunli zhang - Sun Microsystems - Irvine United States } 2607c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 2608dcbf3bd6SGeorge Wilson static void 2609770499e1SDan Kimmel l2arc_free_abd_on_write(abd_t *abd, size_t size, arc_buf_contents_t type) 2610ea8dc4b6Seschrock { 2611dcbf3bd6SGeorge Wilson l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP); 2612ea8dc4b6Seschrock 2613770499e1SDan Kimmel df->l2df_abd = abd; 2614dcbf3bd6SGeorge Wilson df->l2df_size = size; 2615dcbf3bd6SGeorge Wilson df->l2df_type = type; 2616dcbf3bd6SGeorge Wilson mutex_enter(&l2arc_free_on_write_mtx); 2617dcbf3bd6SGeorge Wilson list_insert_head(l2arc_free_on_write, df); 2618dcbf3bd6SGeorge Wilson mutex_exit(&l2arc_free_on_write_mtx); 2619dcbf3bd6SGeorge Wilson } 2620b24ab676SJeff Bonwick 2621dcbf3bd6SGeorge Wilson static void 2622dcbf3bd6SGeorge Wilson arc_hdr_free_on_write(arc_buf_hdr_t *hdr) 2623dcbf3bd6SGeorge Wilson { 2624dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2625dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 2626dcbf3bd6SGeorge Wilson uint64_t size = arc_hdr_size(hdr); 2627dcbf3bd6SGeorge Wilson 2628dcbf3bd6SGeorge Wilson /* protected by hash lock, if in the hash table */ 2629dcbf3bd6SGeorge Wilson if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 2630dcbf3bd6SGeorge Wilson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2631dcbf3bd6SGeorge Wilson ASSERT(state != arc_anon && state != arc_l2c_only); 2632dcbf3bd6SGeorge Wilson 2633dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 2634dcbf3bd6SGeorge Wilson size, hdr); 2635dcbf3bd6SGeorge Wilson } 2636dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_size, size, hdr); 26376de76ce2SAndriy Gapon if (type == ARC_BUFC_METADATA) { 26386de76ce2SAndriy Gapon arc_space_return(size, ARC_SPACE_META); 26396de76ce2SAndriy Gapon } else { 26406de76ce2SAndriy Gapon ASSERT(type == ARC_BUFC_DATA); 26416de76ce2SAndriy Gapon arc_space_return(size, ARC_SPACE_DATA); 26426de76ce2SAndriy Gapon } 2643dcbf3bd6SGeorge Wilson 2644770499e1SDan Kimmel l2arc_free_abd_on_write(hdr->b_l1hdr.b_pabd, size, type); 2645dcbf3bd6SGeorge Wilson } 2646dcbf3bd6SGeorge Wilson 2647dcbf3bd6SGeorge Wilson /* 2648dcbf3bd6SGeorge Wilson * Share the arc_buf_t's data with the hdr. Whenever we are sharing the 2649dcbf3bd6SGeorge Wilson * data buffer, we transfer the refcount ownership to the hdr and update 2650dcbf3bd6SGeorge Wilson * the appropriate kstats. 2651dcbf3bd6SGeorge Wilson */ 2652dcbf3bd6SGeorge Wilson static void 2653dcbf3bd6SGeorge Wilson arc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2654dcbf3bd6SGeorge Wilson { 2655dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2656dcbf3bd6SGeorge Wilson 26575602294fSDan Kimmel ASSERT(arc_can_share(hdr, buf)); 2658770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2659dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 26609253d63dSGeorge Wilson 26619253d63dSGeorge Wilson /* 2662dcbf3bd6SGeorge Wilson * Start sharing the data buffer. We transfer the 2663dcbf3bd6SGeorge Wilson * refcount ownership to the hdr since it always owns 2664dcbf3bd6SGeorge Wilson * the refcount whenever an arc_buf_t is shared. 26659253d63dSGeorge Wilson */ 2666dcbf3bd6SGeorge Wilson refcount_transfer_ownership(&state->arcs_size, buf, hdr); 2667770499e1SDan Kimmel hdr->b_l1hdr.b_pabd = abd_get_from_buf(buf->b_data, arc_buf_size(buf)); 2668770499e1SDan Kimmel abd_take_ownership_of_buf(hdr->b_l1hdr.b_pabd, 2669770499e1SDan Kimmel HDR_ISTYPE_METADATA(hdr)); 2670dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); 26715602294fSDan Kimmel buf->b_flags |= ARC_BUF_FLAG_SHARED; 2672dcbf3bd6SGeorge Wilson 2673dcbf3bd6SGeorge Wilson /* 2674dcbf3bd6SGeorge Wilson * Since we've transferred ownership to the hdr we need 2675dcbf3bd6SGeorge Wilson * to increment its compressed and uncompressed kstats and 2676dcbf3bd6SGeorge Wilson * decrement the overhead size. 2677dcbf3bd6SGeorge Wilson */ 2678dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); 2679dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); 26805602294fSDan Kimmel ARCSTAT_INCR(arcstat_overhead_size, -arc_buf_size(buf)); 2681ea8dc4b6Seschrock } 2682ea8dc4b6Seschrock 2683dcbf3bd6SGeorge Wilson static void 2684dcbf3bd6SGeorge Wilson arc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2685ea8dc4b6Seschrock { 2686dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2687dcbf3bd6SGeorge Wilson 2688dcbf3bd6SGeorge Wilson ASSERT(arc_buf_is_shared(buf)); 2689770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 2690dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2691ea8dc4b6Seschrock 26929b23f181Smaybee /* 2693dcbf3bd6SGeorge Wilson * We are no longer sharing this buffer so we need 2694dcbf3bd6SGeorge Wilson * to transfer its ownership to the rightful owner. 26959b23f181Smaybee */ 2696dcbf3bd6SGeorge Wilson refcount_transfer_ownership(&state->arcs_size, hdr, buf); 2697dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 2698770499e1SDan Kimmel abd_release_ownership_of_buf(hdr->b_l1hdr.b_pabd); 2699770499e1SDan Kimmel abd_put(hdr->b_l1hdr.b_pabd); 2700770499e1SDan Kimmel hdr->b_l1hdr.b_pabd = NULL; 27015602294fSDan Kimmel buf->b_flags &= ~ARC_BUF_FLAG_SHARED; 2702dcbf3bd6SGeorge Wilson 2703dcbf3bd6SGeorge Wilson /* 2704dcbf3bd6SGeorge Wilson * Since the buffer is no longer shared between 2705dcbf3bd6SGeorge Wilson * the arc buf and the hdr, count it as overhead. 2706dcbf3bd6SGeorge Wilson */ 2707dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); 2708dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); 27095602294fSDan Kimmel ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); 2710dcbf3bd6SGeorge Wilson } 2711dcbf3bd6SGeorge Wilson 2712dcbf3bd6SGeorge Wilson /* 27135602294fSDan Kimmel * Remove an arc_buf_t from the hdr's buf list and return the last 27145602294fSDan Kimmel * arc_buf_t on the list. If no buffers remain on the list then return 27155602294fSDan Kimmel * NULL. 27165602294fSDan Kimmel */ 27175602294fSDan Kimmel static arc_buf_t * 27185602294fSDan Kimmel arc_buf_remove(arc_buf_hdr_t *hdr, arc_buf_t *buf) 27195602294fSDan Kimmel { 27205602294fSDan Kimmel ASSERT(HDR_HAS_L1HDR(hdr)); 27215602294fSDan Kimmel ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 27225602294fSDan Kimmel 27235602294fSDan Kimmel arc_buf_t **bufp = &hdr->b_l1hdr.b_buf; 27245602294fSDan Kimmel arc_buf_t *lastbuf = NULL; 27255602294fSDan Kimmel 27265602294fSDan Kimmel /* 27275602294fSDan Kimmel * Remove the buf from the hdr list and locate the last 27285602294fSDan Kimmel * remaining buffer on the list. 27295602294fSDan Kimmel */ 27305602294fSDan Kimmel while (*bufp != NULL) { 27315602294fSDan Kimmel if (*bufp == buf) 27325602294fSDan Kimmel *bufp = buf->b_next; 27335602294fSDan Kimmel 27345602294fSDan Kimmel /* 27355602294fSDan Kimmel * If we've removed a buffer in the middle of 27365602294fSDan Kimmel * the list then update the lastbuf and update 27375602294fSDan Kimmel * bufp. 27385602294fSDan Kimmel */ 27395602294fSDan Kimmel if (*bufp != NULL) { 27405602294fSDan Kimmel lastbuf = *bufp; 27415602294fSDan Kimmel bufp = &(*bufp)->b_next; 27425602294fSDan Kimmel } 27435602294fSDan Kimmel } 27445602294fSDan Kimmel buf->b_next = NULL; 27455602294fSDan Kimmel ASSERT3P(lastbuf, !=, buf); 27465602294fSDan Kimmel IMPLY(hdr->b_l1hdr.b_bufcnt > 0, lastbuf != NULL); 27475602294fSDan Kimmel IMPLY(hdr->b_l1hdr.b_bufcnt > 0, hdr->b_l1hdr.b_buf != NULL); 27485602294fSDan Kimmel IMPLY(lastbuf != NULL, ARC_BUF_LAST(lastbuf)); 27495602294fSDan Kimmel 27505602294fSDan Kimmel return (lastbuf); 27515602294fSDan Kimmel } 27525602294fSDan Kimmel 27535602294fSDan Kimmel /* 27545602294fSDan Kimmel * Free up buf->b_data and pull the arc_buf_t off of the the arc_buf_hdr_t's 27555602294fSDan Kimmel * list and free it. 2756dcbf3bd6SGeorge Wilson */ 2757dcbf3bd6SGeorge Wilson static void 27585602294fSDan Kimmel arc_buf_destroy_impl(arc_buf_t *buf) 2759dcbf3bd6SGeorge Wilson { 2760dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 2761dcbf3bd6SGeorge Wilson 2762dcbf3bd6SGeorge Wilson /* 27635602294fSDan Kimmel * Free up the data associated with the buf but only if we're not 27645602294fSDan Kimmel * sharing this with the hdr. If we are sharing it with the hdr, the 27655602294fSDan Kimmel * hdr is responsible for doing the free. 2766dcbf3bd6SGeorge Wilson */ 2767dcbf3bd6SGeorge Wilson if (buf->b_data != NULL) { 2768dcbf3bd6SGeorge Wilson /* 2769dcbf3bd6SGeorge Wilson * We're about to change the hdr's b_flags. We must either 2770dcbf3bd6SGeorge Wilson * hold the hash_lock or be undiscoverable. 2771dcbf3bd6SGeorge Wilson */ 2772dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2773dcbf3bd6SGeorge Wilson 2774dcbf3bd6SGeorge Wilson arc_cksum_verify(buf); 2775dcbf3bd6SGeorge Wilson arc_buf_unwatch(buf); 2776dcbf3bd6SGeorge Wilson 27775602294fSDan Kimmel if (arc_buf_is_shared(buf)) { 2778dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 2779dcbf3bd6SGeorge Wilson } else { 27805602294fSDan Kimmel uint64_t size = arc_buf_size(buf); 2781dcbf3bd6SGeorge Wilson arc_free_data_buf(hdr, buf->b_data, size, buf); 2782dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_overhead_size, -size); 2783dcbf3bd6SGeorge Wilson } 2784dcbf3bd6SGeorge Wilson buf->b_data = NULL; 2785dcbf3bd6SGeorge Wilson 2786dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 2787dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt -= 1; 2788dcbf3bd6SGeorge Wilson } 2789dcbf3bd6SGeorge Wilson 27905602294fSDan Kimmel arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); 2791dcbf3bd6SGeorge Wilson 27925602294fSDan Kimmel if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) { 2793dcbf3bd6SGeorge Wilson /* 27945602294fSDan Kimmel * If the current arc_buf_t is sharing its data buffer with the 2795770499e1SDan Kimmel * hdr, then reassign the hdr's b_pabd to share it with the new 27965602294fSDan Kimmel * buffer at the end of the list. The shared buffer is always 27975602294fSDan Kimmel * the last one on the hdr's buffer list. 27985602294fSDan Kimmel * 27995602294fSDan Kimmel * There is an equivalent case for compressed bufs, but since 28005602294fSDan Kimmel * they aren't guaranteed to be the last buf in the list and 28015602294fSDan Kimmel * that is an exceedingly rare case, we just allow that space be 28025602294fSDan Kimmel * wasted temporarily. 2803dcbf3bd6SGeorge Wilson */ 28045602294fSDan Kimmel if (lastbuf != NULL) { 28055602294fSDan Kimmel /* Only one buf can be shared at once */ 28065602294fSDan Kimmel VERIFY(!arc_buf_is_shared(lastbuf)); 28075602294fSDan Kimmel /* hdr is uncompressed so can't have compressed buf */ 28085602294fSDan Kimmel VERIFY(!ARC_BUF_COMPRESSED(lastbuf)); 2809dcbf3bd6SGeorge Wilson 2810770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 2811770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 2812dcbf3bd6SGeorge Wilson 28135602294fSDan Kimmel /* 28145602294fSDan Kimmel * We must setup a new shared block between the 28155602294fSDan Kimmel * last buffer and the hdr. The data would have 28165602294fSDan Kimmel * been allocated by the arc buf so we need to transfer 28175602294fSDan Kimmel * ownership to the hdr since it's now being shared. 28185602294fSDan Kimmel */ 28195602294fSDan Kimmel arc_share_buf(hdr, lastbuf); 28205602294fSDan Kimmel } 28215602294fSDan Kimmel } else if (HDR_SHARED_DATA(hdr)) { 2822dcbf3bd6SGeorge Wilson /* 28235602294fSDan Kimmel * Uncompressed shared buffers are always at the end 28245602294fSDan Kimmel * of the list. Compressed buffers don't have the 28255602294fSDan Kimmel * same requirements. This makes it hard to 28265602294fSDan Kimmel * simply assert that the lastbuf is shared so 28275602294fSDan Kimmel * we rely on the hdr's compression flags to determine 28285602294fSDan Kimmel * if we have a compressed, shared buffer. 2829dcbf3bd6SGeorge Wilson */ 28305602294fSDan Kimmel ASSERT3P(lastbuf, !=, NULL); 28315602294fSDan Kimmel ASSERT(arc_buf_is_shared(lastbuf) || 28325602294fSDan Kimmel HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF); 2833dcbf3bd6SGeorge Wilson } 2834dcbf3bd6SGeorge Wilson 28355602294fSDan Kimmel /* 28365602294fSDan Kimmel * Free the checksum if we're removing the last uncompressed buf from 28375602294fSDan Kimmel * this hdr. 28385602294fSDan Kimmel */ 28395602294fSDan Kimmel if (!arc_hdr_has_uncompressed_buf(hdr)) { 2840dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 28415602294fSDan Kimmel } 2842dcbf3bd6SGeorge Wilson 2843dcbf3bd6SGeorge Wilson /* clean up the buf */ 2844dcbf3bd6SGeorge Wilson buf->b_hdr = NULL; 2845dcbf3bd6SGeorge Wilson kmem_cache_free(buf_cache, buf); 2846dcbf3bd6SGeorge Wilson } 2847dcbf3bd6SGeorge Wilson 2848dcbf3bd6SGeorge Wilson static void 2849770499e1SDan Kimmel arc_hdr_alloc_pabd(arc_buf_hdr_t *hdr) 2850dcbf3bd6SGeorge Wilson { 2851dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); 285289c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 2853dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 2854ea8dc4b6Seschrock 2855770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2856770499e1SDan Kimmel hdr->b_l1hdr.b_pabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr); 2857dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 2858770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 285989c86e32SChris Williamson 2860dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); 2861dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); 2862ea8dc4b6Seschrock } 2863ea8dc4b6Seschrock 2864244781f1SPrakash Surya static void 2865770499e1SDan Kimmel arc_hdr_free_pabd(arc_buf_hdr_t *hdr) 2866244781f1SPrakash Surya { 2867dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2868770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 2869244781f1SPrakash Surya 2870dcbf3bd6SGeorge Wilson /* 2871dcbf3bd6SGeorge Wilson * If the hdr is currently being written to the l2arc then 2872dcbf3bd6SGeorge Wilson * we defer freeing the data by adding it to the l2arc_free_on_write 2873dcbf3bd6SGeorge Wilson * list. The l2arc will free the data once it's finished 2874dcbf3bd6SGeorge Wilson * writing it to the l2arc device. 2875dcbf3bd6SGeorge Wilson */ 2876dcbf3bd6SGeorge Wilson if (HDR_L2_WRITING(hdr)) { 2877dcbf3bd6SGeorge Wilson arc_hdr_free_on_write(hdr); 2878dcbf3bd6SGeorge Wilson ARCSTAT_BUMP(arcstat_l2_free_on_write); 2879dcbf3bd6SGeorge Wilson } else { 2880770499e1SDan Kimmel arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, 2881dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 2882dcbf3bd6SGeorge Wilson } 2883770499e1SDan Kimmel hdr->b_l1hdr.b_pabd = NULL; 2884dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 2885dcbf3bd6SGeorge Wilson 2886dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); 2887dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); 2888dcbf3bd6SGeorge Wilson } 2889dcbf3bd6SGeorge Wilson 2890dcbf3bd6SGeorge Wilson static arc_buf_hdr_t * 2891dcbf3bd6SGeorge Wilson arc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize, 28925602294fSDan Kimmel enum zio_compress compression_type, arc_buf_contents_t type) 2893dcbf3bd6SGeorge Wilson { 2894dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr; 2895dcbf3bd6SGeorge Wilson 2896dcbf3bd6SGeorge Wilson VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA); 2897dcbf3bd6SGeorge Wilson 2898dcbf3bd6SGeorge Wilson hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 2899dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 2900dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 2901dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_thawed, ==, NULL); 2902dcbf3bd6SGeorge Wilson HDR_SET_PSIZE(hdr, psize); 2903dcbf3bd6SGeorge Wilson HDR_SET_LSIZE(hdr, lsize); 2904dcbf3bd6SGeorge Wilson hdr->b_spa = spa; 2905dcbf3bd6SGeorge Wilson hdr->b_type = type; 2906dcbf3bd6SGeorge Wilson hdr->b_flags = 0; 2907dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR); 29085602294fSDan Kimmel arc_hdr_set_compress(hdr, compression_type); 2909dcbf3bd6SGeorge Wilson 2910dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_state = arc_anon; 2911dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_arc_access = 0; 2912dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt = 0; 2913dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_buf = NULL; 2914dcbf3bd6SGeorge Wilson 2915dcbf3bd6SGeorge Wilson /* 2916dcbf3bd6SGeorge Wilson * Allocate the hdr's buffer. This will contain either 2917dcbf3bd6SGeorge Wilson * the compressed or uncompressed data depending on the block 2918dcbf3bd6SGeorge Wilson * it references and compressed arc enablement. 2919dcbf3bd6SGeorge Wilson */ 2920770499e1SDan Kimmel arc_hdr_alloc_pabd(hdr); 2921dcbf3bd6SGeorge Wilson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2922dcbf3bd6SGeorge Wilson 2923dcbf3bd6SGeorge Wilson return (hdr); 2924244781f1SPrakash Surya } 2925244781f1SPrakash Surya 2926fa94a07fSbrendan /* 2927dcbf3bd6SGeorge Wilson * Transition between the two allocation states for the arc_buf_hdr struct. 2928dcbf3bd6SGeorge Wilson * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without 2929dcbf3bd6SGeorge Wilson * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller 2930dcbf3bd6SGeorge Wilson * version is used when a cache buffer is only in the L2ARC in order to reduce 2931dcbf3bd6SGeorge Wilson * memory usage. 2932fa94a07fSbrendan */ 2933dcbf3bd6SGeorge Wilson static arc_buf_hdr_t * 2934dcbf3bd6SGeorge Wilson arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) 2935fa94a07fSbrendan { 2936dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L2HDR(hdr)); 2937dcbf3bd6SGeorge Wilson 2938dcbf3bd6SGeorge Wilson arc_buf_hdr_t *nhdr; 2939dcbf3bd6SGeorge Wilson l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 2940dcbf3bd6SGeorge Wilson 2941dcbf3bd6SGeorge Wilson ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || 2942dcbf3bd6SGeorge Wilson (old == hdr_l2only_cache && new == hdr_full_cache)); 2943dcbf3bd6SGeorge Wilson 2944dcbf3bd6SGeorge Wilson nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); 2945dcbf3bd6SGeorge Wilson 2946dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 2947dcbf3bd6SGeorge Wilson buf_hash_remove(hdr); 2948dcbf3bd6SGeorge Wilson 2949dcbf3bd6SGeorge Wilson bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); 2950dcbf3bd6SGeorge Wilson 2951dcbf3bd6SGeorge Wilson if (new == hdr_full_cache) { 2952dcbf3bd6SGeorge Wilson arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR); 2953dcbf3bd6SGeorge Wilson /* 2954dcbf3bd6SGeorge Wilson * arc_access and arc_change_state need to be aware that a 2955dcbf3bd6SGeorge Wilson * header has just come out of L2ARC, so we set its state to 2956dcbf3bd6SGeorge Wilson * l2c_only even though it's about to change. 2957dcbf3bd6SGeorge Wilson */ 2958dcbf3bd6SGeorge Wilson nhdr->b_l1hdr.b_state = arc_l2c_only; 2959dcbf3bd6SGeorge Wilson 2960dcbf3bd6SGeorge Wilson /* Verify previous threads set to NULL before freeing */ 2961770499e1SDan Kimmel ASSERT3P(nhdr->b_l1hdr.b_pabd, ==, NULL); 2962dcbf3bd6SGeorge Wilson } else { 2963dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2964dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 2965dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 2966dcbf3bd6SGeorge Wilson 2967dcbf3bd6SGeorge Wilson /* 2968dcbf3bd6SGeorge Wilson * If we've reached here, We must have been called from 2969dcbf3bd6SGeorge Wilson * arc_evict_hdr(), as such we should have already been 2970dcbf3bd6SGeorge Wilson * removed from any ghost list we were previously on 2971dcbf3bd6SGeorge Wilson * (which protects us from racing with arc_evict_state), 2972dcbf3bd6SGeorge Wilson * thus no locking is needed during this check. 2973dcbf3bd6SGeorge Wilson */ 2974dcbf3bd6SGeorge Wilson ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 2975cd1c8b85SMatthew Ahrens 2976dcbf3bd6SGeorge Wilson /* 2977dcbf3bd6SGeorge Wilson * A buffer must not be moved into the arc_l2c_only 2978dcbf3bd6SGeorge Wilson * state if it's not finished being written out to the 2979770499e1SDan Kimmel * l2arc device. Otherwise, the b_l1hdr.b_pabd field 2980dcbf3bd6SGeorge Wilson * might try to be accessed, even though it was removed. 2981dcbf3bd6SGeorge Wilson */ 2982dcbf3bd6SGeorge Wilson VERIFY(!HDR_L2_WRITING(hdr)); 2983770499e1SDan Kimmel VERIFY3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2984fa94a07fSbrendan 2985dcbf3bd6SGeorge Wilson #ifdef ZFS_DEBUG 2986dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_thawed != NULL) { 2987dcbf3bd6SGeorge Wilson kmem_free(hdr->b_l1hdr.b_thawed, 1); 2988dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_thawed = NULL; 2989dcbf3bd6SGeorge Wilson } 2990dcbf3bd6SGeorge Wilson #endif 2991244781f1SPrakash Surya 2992dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR); 2993dcbf3bd6SGeorge Wilson } 2994244781f1SPrakash Surya /* 2995dcbf3bd6SGeorge Wilson * The header has been reallocated so we need to re-insert it into any 2996dcbf3bd6SGeorge Wilson * lists it was on. 2997244781f1SPrakash Surya */ 2998dcbf3bd6SGeorge Wilson (void) buf_hash_insert(nhdr, NULL); 2999244781f1SPrakash Surya 3000dcbf3bd6SGeorge Wilson ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); 3001dcbf3bd6SGeorge Wilson 3002dcbf3bd6SGeorge Wilson mutex_enter(&dev->l2ad_mtx); 3003244781f1SPrakash Surya 3004244781f1SPrakash Surya /* 3005dcbf3bd6SGeorge Wilson * We must place the realloc'ed header back into the list at 3006dcbf3bd6SGeorge Wilson * the same spot. Otherwise, if it's placed earlier in the list, 3007dcbf3bd6SGeorge Wilson * l2arc_write_buffers() could find it during the function's 3008dcbf3bd6SGeorge Wilson * write phase, and try to write it out to the l2arc. 3009244781f1SPrakash Surya */ 3010dcbf3bd6SGeorge Wilson list_insert_after(&dev->l2ad_buflist, hdr, nhdr); 3011dcbf3bd6SGeorge Wilson list_remove(&dev->l2ad_buflist, hdr); 3012dcbf3bd6SGeorge Wilson 3013dcbf3bd6SGeorge Wilson mutex_exit(&dev->l2ad_mtx); 3014244781f1SPrakash Surya 3015244781f1SPrakash Surya /* 3016dcbf3bd6SGeorge Wilson * Since we're using the pointer address as the tag when 3017dcbf3bd6SGeorge Wilson * incrementing and decrementing the l2ad_alloc refcount, we 3018dcbf3bd6SGeorge Wilson * must remove the old pointer (that we're about to destroy) and 3019dcbf3bd6SGeorge Wilson * add the new pointer to the refcount. Otherwise we'd remove 3020dcbf3bd6SGeorge Wilson * the wrong pointer address when calling arc_hdr_destroy() later. 3021244781f1SPrakash Surya */ 3022244781f1SPrakash Surya 3023dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr), hdr); 3024dcbf3bd6SGeorge Wilson (void) refcount_add_many(&dev->l2ad_alloc, arc_hdr_size(nhdr), nhdr); 3025244781f1SPrakash Surya 3026dcbf3bd6SGeorge Wilson buf_discard_identity(hdr); 3027dcbf3bd6SGeorge Wilson kmem_cache_free(old, hdr); 3028244781f1SPrakash Surya 3029dcbf3bd6SGeorge Wilson return (nhdr); 3030244781f1SPrakash Surya } 3031244781f1SPrakash Surya 3032bbfa8ea8SMatthew Ahrens /* 3033dcbf3bd6SGeorge Wilson * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller. 3034dcbf3bd6SGeorge Wilson * The buf is returned thawed since we expect the consumer to modify it. 3035bbfa8ea8SMatthew Ahrens */ 3036dcbf3bd6SGeorge Wilson arc_buf_t * 30375602294fSDan Kimmel arc_alloc_buf(spa_t *spa, void *tag, arc_buf_contents_t type, int32_t size) 3038ea8dc4b6Seschrock { 3039dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size, 3040dcbf3bd6SGeorge Wilson ZIO_COMPRESS_OFF, type); 3041dcbf3bd6SGeorge Wilson ASSERT(!MUTEX_HELD(HDR_LOCK(hdr))); 30425602294fSDan Kimmel 30435602294fSDan Kimmel arc_buf_t *buf = NULL; 30445602294fSDan Kimmel VERIFY0(arc_buf_alloc_impl(hdr, tag, B_FALSE, B_FALSE, &buf)); 3045dcbf3bd6SGeorge Wilson arc_buf_thaw(buf); 30465602294fSDan Kimmel 30475602294fSDan Kimmel return (buf); 30485602294fSDan Kimmel } 30495602294fSDan Kimmel 30505602294fSDan Kimmel /* 30515602294fSDan Kimmel * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this 30525602294fSDan Kimmel * for bufs containing metadata. 30535602294fSDan Kimmel */ 30545602294fSDan Kimmel arc_buf_t * 30555602294fSDan Kimmel arc_alloc_compressed_buf(spa_t *spa, void *tag, uint64_t psize, uint64_t lsize, 30565602294fSDan Kimmel enum zio_compress compression_type) 30575602294fSDan Kimmel { 30585602294fSDan Kimmel ASSERT3U(lsize, >, 0); 30595602294fSDan Kimmel ASSERT3U(lsize, >=, psize); 30605602294fSDan Kimmel ASSERT(compression_type > ZIO_COMPRESS_OFF); 30615602294fSDan Kimmel ASSERT(compression_type < ZIO_COMPRESS_FUNCTIONS); 30625602294fSDan Kimmel 30635602294fSDan Kimmel arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, 30645602294fSDan Kimmel compression_type, ARC_BUFC_DATA); 30655602294fSDan Kimmel ASSERT(!MUTEX_HELD(HDR_LOCK(hdr))); 30665602294fSDan Kimmel 30675602294fSDan Kimmel arc_buf_t *buf = NULL; 30685602294fSDan Kimmel VERIFY0(arc_buf_alloc_impl(hdr, tag, B_TRUE, B_FALSE, &buf)); 30695602294fSDan Kimmel arc_buf_thaw(buf); 30705602294fSDan Kimmel ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 30715602294fSDan Kimmel 3072770499e1SDan Kimmel if (!arc_buf_is_shared(buf)) { 3073770499e1SDan Kimmel /* 3074770499e1SDan Kimmel * To ensure that the hdr has the correct data in it if we call 3075770499e1SDan Kimmel * arc_decompress() on this buf before it's been written to 3076770499e1SDan Kimmel * disk, it's easiest if we just set up sharing between the 3077770499e1SDan Kimmel * buf and the hdr. 3078770499e1SDan Kimmel */ 3079770499e1SDan Kimmel ASSERT(!abd_is_linear(hdr->b_l1hdr.b_pabd)); 3080770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 3081770499e1SDan Kimmel arc_share_buf(hdr, buf); 3082770499e1SDan Kimmel } 3083770499e1SDan Kimmel 3084dcbf3bd6SGeorge Wilson return (buf); 3085ea8dc4b6Seschrock } 3086ea8dc4b6Seschrock 3087a52fc310SPrakash Surya static void 3088a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) 3089a52fc310SPrakash Surya { 3090a52fc310SPrakash Surya l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 3091a52fc310SPrakash Surya l2arc_dev_t *dev = l2hdr->b_dev; 309216a7e5acSAndriy Gapon uint64_t psize = arc_hdr_size(hdr); 3093a52fc310SPrakash Surya 3094a52fc310SPrakash Surya ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); 3095a52fc310SPrakash Surya ASSERT(HDR_HAS_L2HDR(hdr)); 3096a52fc310SPrakash Surya 3097a52fc310SPrakash Surya list_remove(&dev->l2ad_buflist, hdr); 3098a52fc310SPrakash Surya 309916a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_psize, -psize); 310016a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_lsize, -HDR_GET_LSIZE(hdr)); 3101a52fc310SPrakash Surya 310216a7e5acSAndriy Gapon vdev_space_update(dev->l2ad_vdev, -psize, 0, 0); 3103a52fc310SPrakash Surya 310416a7e5acSAndriy Gapon (void) refcount_remove_many(&dev->l2ad_alloc, psize, hdr); 3105dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); 3106a52fc310SPrakash Surya } 3107a52fc310SPrakash Surya 3108fa9e4066Sahrens static void 3109ea8dc4b6Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 3110fa9e4066Sahrens { 311189c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) { 311289c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_buf == NULL || 3113dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt > 0); 311489c86e32SChris Williamson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 311589c86e32SChris Williamson ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 311689c86e32SChris Williamson } 3117ea8dc4b6Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 311889c86e32SChris Williamson ASSERT(!HDR_IN_HASH_TABLE(hdr)); 311989c86e32SChris Williamson 3120dcbf3bd6SGeorge Wilson if (!HDR_EMPTY(hdr)) 3121dcbf3bd6SGeorge Wilson buf_discard_identity(hdr); 3122dcbf3bd6SGeorge Wilson 312389c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr)) { 3124a52fc310SPrakash Surya l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 3125a52fc310SPrakash Surya boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); 3126b24ab676SJeff Bonwick 3127a52fc310SPrakash Surya if (!buflist_held) 3128a52fc310SPrakash Surya mutex_enter(&dev->l2ad_mtx); 312989c86e32SChris Williamson 3130244781f1SPrakash Surya /* 3131a52fc310SPrakash Surya * Even though we checked this conditional above, we 3132a52fc310SPrakash Surya * need to check this again now that we have the 3133a52fc310SPrakash Surya * l2ad_mtx. This is because we could be racing with 3134a52fc310SPrakash Surya * another thread calling l2arc_evict() which might have 3135a52fc310SPrakash Surya * destroyed this header's L2 portion as we were waiting 3136a52fc310SPrakash Surya * to acquire the l2ad_mtx. If that happens, we don't 3137a52fc310SPrakash Surya * want to re-destroy the header's L2 portion. 3138244781f1SPrakash Surya */ 3139a52fc310SPrakash Surya if (HDR_HAS_L2HDR(hdr)) 3140a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(hdr); 3141b24ab676SJeff Bonwick 3142b24ab676SJeff Bonwick if (!buflist_held) 3143a52fc310SPrakash Surya mutex_exit(&dev->l2ad_mtx); 3144fa94a07fSbrendan } 3145fa94a07fSbrendan 3146dcbf3bd6SGeorge Wilson if (HDR_HAS_L1HDR(hdr)) { 3147dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 314889c86e32SChris Williamson 3149dcbf3bd6SGeorge Wilson while (hdr->b_l1hdr.b_buf != NULL) 31505602294fSDan Kimmel arc_buf_destroy_impl(hdr->b_l1hdr.b_buf); 315189c86e32SChris Williamson 315289c86e32SChris Williamson #ifdef ZFS_DEBUG 315389c86e32SChris Williamson if (hdr->b_l1hdr.b_thawed != NULL) { 315489c86e32SChris Williamson kmem_free(hdr->b_l1hdr.b_thawed, 1); 315589c86e32SChris Williamson hdr->b_l1hdr.b_thawed = NULL; 315689c86e32SChris Williamson } 315789c86e32SChris Williamson #endif 3158dcbf3bd6SGeorge Wilson 3159770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 3160770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 3161dcbf3bd6SGeorge Wilson } 31623f9d6ad7SLin Ling } 3163ea8dc4b6Seschrock 3164fa9e4066Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 316589c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) { 3166244781f1SPrakash Surya ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 316789c86e32SChris Williamson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 316889c86e32SChris Williamson kmem_cache_free(hdr_full_cache, hdr); 316989c86e32SChris Williamson } else { 317089c86e32SChris Williamson kmem_cache_free(hdr_l2only_cache, hdr); 317189c86e32SChris Williamson } 3172fa9e4066Sahrens } 3173fa9e4066Sahrens 3174fa9e4066Sahrens void 3175dcbf3bd6SGeorge Wilson arc_buf_destroy(arc_buf_t *buf, void* tag) 3176ea8dc4b6Seschrock { 3177ea8dc4b6Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 3178ea8dc4b6Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 3179fa9e4066Sahrens 318089c86e32SChris Williamson if (hdr->b_l1hdr.b_state == arc_anon) { 3181dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 3182dcbf3bd6SGeorge Wilson ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3183dcbf3bd6SGeorge Wilson VERIFY0(remove_reference(hdr, NULL, tag)); 3184dcbf3bd6SGeorge Wilson arc_hdr_destroy(hdr); 3185dcbf3bd6SGeorge Wilson return; 3186ea8dc4b6Seschrock } 3187ea8dc4b6Seschrock 3188ea8dc4b6Seschrock mutex_enter(hash_lock); 3189dcbf3bd6SGeorge Wilson ASSERT3P(hdr, ==, buf->b_hdr); 3190dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 31913f9d6ad7SLin Ling ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3192dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon); 3193dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, !=, NULL); 3194ea8dc4b6Seschrock 3195ea8dc4b6Seschrock (void) remove_reference(hdr, hash_lock, tag); 31965602294fSDan Kimmel arc_buf_destroy_impl(buf); 3197ea8dc4b6Seschrock mutex_exit(hash_lock); 3198fa9e4066Sahrens } 3199fa9e4066Sahrens 3200fa9e4066Sahrens /* 3201244781f1SPrakash Surya * Evict the arc_buf_hdr that is provided as a parameter. The resultant 3202244781f1SPrakash Surya * state of the header is dependent on it's state prior to entering this 3203244781f1SPrakash Surya * function. The following transitions are possible: 3204874395d5Smaybee * 3205244781f1SPrakash Surya * - arc_mru -> arc_mru_ghost 3206244781f1SPrakash Surya * - arc_mfu -> arc_mfu_ghost 3207244781f1SPrakash Surya * - arc_mru_ghost -> arc_l2c_only 3208244781f1SPrakash Surya * - arc_mru_ghost -> deleted 3209244781f1SPrakash Surya * - arc_mfu_ghost -> arc_l2c_only 3210244781f1SPrakash Surya * - arc_mfu_ghost -> deleted 3211fa9e4066Sahrens */ 3212244781f1SPrakash Surya static int64_t 3213244781f1SPrakash Surya arc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 3214fa9e4066Sahrens { 3215244781f1SPrakash Surya arc_state_t *evicted_state, *state; 3216244781f1SPrakash Surya int64_t bytes_evicted = 0; 3217fa9e4066Sahrens 3218244781f1SPrakash Surya ASSERT(MUTEX_HELD(hash_lock)); 3219244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 3220fa9e4066Sahrens 3221244781f1SPrakash Surya state = hdr->b_l1hdr.b_state; 3222244781f1SPrakash Surya if (GHOST_STATE(state)) { 3223244781f1SPrakash Surya ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3224dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 3225fa9e4066Sahrens 3226244781f1SPrakash Surya /* 3227244781f1SPrakash Surya * l2arc_write_buffers() relies on a header's L1 portion 3228770499e1SDan Kimmel * (i.e. its b_pabd field) during it's write phase. 3229244781f1SPrakash Surya * Thus, we cannot push a header onto the arc_l2c_only 3230244781f1SPrakash Surya * state (removing it's L1 piece) until the header is 3231244781f1SPrakash Surya * done being written to the l2arc. 3232244781f1SPrakash Surya */ 3233244781f1SPrakash Surya if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { 3234244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_l2_skip); 3235244781f1SPrakash Surya return (bytes_evicted); 32363a5286a1SMatthew Ahrens } 3237244781f1SPrakash Surya 3238244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_deleted); 3239dcbf3bd6SGeorge Wilson bytes_evicted += HDR_GET_LSIZE(hdr); 3240244781f1SPrakash Surya 3241244781f1SPrakash Surya DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); 3242244781f1SPrakash Surya 3243770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 3244244781f1SPrakash Surya if (HDR_HAS_L2HDR(hdr)) { 3245244781f1SPrakash Surya /* 3246244781f1SPrakash Surya * This buffer is cached on the 2nd Level ARC; 3247244781f1SPrakash Surya * don't destroy the header. 3248244781f1SPrakash Surya */ 3249244781f1SPrakash Surya arc_change_state(arc_l2c_only, hdr, hash_lock); 32503a5286a1SMatthew Ahrens /* 3251244781f1SPrakash Surya * dropping from L1+L2 cached to L2-only, 3252244781f1SPrakash Surya * realloc to remove the L1 header. 32533a5286a1SMatthew Ahrens */ 3254244781f1SPrakash Surya hdr = arc_hdr_realloc(hdr, hdr_full_cache, 3255244781f1SPrakash Surya hdr_l2only_cache); 3256244781f1SPrakash Surya } else { 3257244781f1SPrakash Surya arc_change_state(arc_anon, hdr, hash_lock); 3258244781f1SPrakash Surya arc_hdr_destroy(hdr); 32593a5286a1SMatthew Ahrens } 3260244781f1SPrakash Surya return (bytes_evicted); 32613a5286a1SMatthew Ahrens } 32623a5286a1SMatthew Ahrens 3263244781f1SPrakash Surya ASSERT(state == arc_mru || state == arc_mfu); 3264244781f1SPrakash Surya evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 3265244781f1SPrakash Surya 3266244781f1SPrakash Surya /* prefetch buffers have a minimum lifespan */ 3267244781f1SPrakash Surya if (HDR_IO_IN_PROGRESS(hdr) || 3268244781f1SPrakash Surya ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && 3269244781f1SPrakash Surya ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < 3270244781f1SPrakash Surya arc_min_prefetch_lifespan)) { 3271244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_skip); 3272244781f1SPrakash Surya return (bytes_evicted); 3273244781f1SPrakash Surya } 3274244781f1SPrakash Surya 3275244781f1SPrakash Surya ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 3276244781f1SPrakash Surya while (hdr->b_l1hdr.b_buf) { 3277244781f1SPrakash Surya arc_buf_t *buf = hdr->b_l1hdr.b_buf; 3278244781f1SPrakash Surya if (!mutex_tryenter(&buf->b_evict_lock)) { 3279244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_mutex_miss); 3280244781f1SPrakash Surya break; 328113506d1eSmaybee } 3282244781f1SPrakash Surya if (buf->b_data != NULL) 3283dcbf3bd6SGeorge Wilson bytes_evicted += HDR_GET_LSIZE(hdr); 3284dcbf3bd6SGeorge Wilson mutex_exit(&buf->b_evict_lock); 32855602294fSDan Kimmel arc_buf_destroy_impl(buf); 3286244781f1SPrakash Surya } 328769962b56SMatthew Ahrens 3288244781f1SPrakash Surya if (HDR_HAS_L2HDR(hdr)) { 3289dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr)); 3290244781f1SPrakash Surya } else { 3291dcbf3bd6SGeorge Wilson if (l2arc_write_eligible(hdr->b_spa, hdr)) { 3292dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_evict_l2_eligible, 3293dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr)); 3294dcbf3bd6SGeorge Wilson } else { 3295dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_evict_l2_ineligible, 3296dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr)); 3297dcbf3bd6SGeorge Wilson } 3298244781f1SPrakash Surya } 3299244781f1SPrakash Surya 3300dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_bufcnt == 0) { 3301dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 3302dcbf3bd6SGeorge Wilson 3303dcbf3bd6SGeorge Wilson bytes_evicted += arc_hdr_size(hdr); 3304dcbf3bd6SGeorge Wilson 3305dcbf3bd6SGeorge Wilson /* 3306dcbf3bd6SGeorge Wilson * If this hdr is being evicted and has a compressed 3307dcbf3bd6SGeorge Wilson * buffer then we discard it here before we change states. 3308dcbf3bd6SGeorge Wilson * This ensures that the accounting is updated correctly 3309770499e1SDan Kimmel * in arc_free_data_impl(). 3310dcbf3bd6SGeorge Wilson */ 3311770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 3312dcbf3bd6SGeorge Wilson 3313244781f1SPrakash Surya arc_change_state(evicted_state, hdr, hash_lock); 3314244781f1SPrakash Surya ASSERT(HDR_IN_HASH_TABLE(hdr)); 3315dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 3316244781f1SPrakash Surya DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); 3317244781f1SPrakash Surya } 3318244781f1SPrakash Surya 3319244781f1SPrakash Surya return (bytes_evicted); 3320244781f1SPrakash Surya } 3321244781f1SPrakash Surya 3322244781f1SPrakash Surya static uint64_t 3323244781f1SPrakash Surya arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, 3324244781f1SPrakash Surya uint64_t spa, int64_t bytes) 3325244781f1SPrakash Surya { 3326244781f1SPrakash Surya multilist_sublist_t *mls; 3327244781f1SPrakash Surya uint64_t bytes_evicted = 0; 3328244781f1SPrakash Surya arc_buf_hdr_t *hdr; 3329244781f1SPrakash Surya kmutex_t *hash_lock; 3330244781f1SPrakash Surya int evict_count = 0; 3331244781f1SPrakash Surya 3332244781f1SPrakash Surya ASSERT3P(marker, !=, NULL); 3333244781f1SPrakash Surya IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 3334244781f1SPrakash Surya 3335244781f1SPrakash Surya mls = multilist_sublist_lock(ml, idx); 3336244781f1SPrakash Surya 3337244781f1SPrakash Surya for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL; 3338244781f1SPrakash Surya hdr = multilist_sublist_prev(mls, marker)) { 3339244781f1SPrakash Surya if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) || 3340244781f1SPrakash Surya (evict_count >= zfs_arc_evict_batch_limit)) 3341244781f1SPrakash Surya break; 334269962b56SMatthew Ahrens 334369962b56SMatthew Ahrens /* 3344244781f1SPrakash Surya * To keep our iteration location, move the marker 3345244781f1SPrakash Surya * forward. Since we're not holding hdr's hash lock, we 3346244781f1SPrakash Surya * must be very careful and not remove 'hdr' from the 3347244781f1SPrakash Surya * sublist. Otherwise, other consumers might mistake the 3348244781f1SPrakash Surya * 'hdr' as not being on a sublist when they call the 3349244781f1SPrakash Surya * multilist_link_active() function (they all rely on 3350244781f1SPrakash Surya * the hash lock protecting concurrent insertions and 3351244781f1SPrakash Surya * removals). multilist_sublist_move_forward() was 3352244781f1SPrakash Surya * specifically implemented to ensure this is the case 3353244781f1SPrakash Surya * (only 'marker' will be removed and re-inserted). 3354244781f1SPrakash Surya */ 3355244781f1SPrakash Surya multilist_sublist_move_forward(mls, marker); 3356244781f1SPrakash Surya 3357244781f1SPrakash Surya /* 3358244781f1SPrakash Surya * The only case where the b_spa field should ever be 3359244781f1SPrakash Surya * zero, is the marker headers inserted by 3360244781f1SPrakash Surya * arc_evict_state(). It's possible for multiple threads 3361244781f1SPrakash Surya * to be calling arc_evict_state() concurrently (e.g. 3362244781f1SPrakash Surya * dsl_pool_close() and zio_inject_fault()), so we must 3363244781f1SPrakash Surya * skip any markers we see from these other threads. 336469962b56SMatthew Ahrens */ 3365244781f1SPrakash Surya if (hdr->b_spa == 0) 3366244781f1SPrakash Surya continue; 3367244781f1SPrakash Surya 3368244781f1SPrakash Surya /* we're only interested in evicting buffers of a certain spa */ 3369244781f1SPrakash Surya if (spa != 0 && hdr->b_spa != spa) { 3370244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_skip); 337169962b56SMatthew Ahrens continue; 337269962b56SMatthew Ahrens } 337369962b56SMatthew Ahrens 33747adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 33755ea40c06SBrendan Gregg - Sun Microsystems 3376244781f1SPrakash Surya /* 3377244781f1SPrakash Surya * We aren't calling this function from any code path 3378244781f1SPrakash Surya * that would already be holding a hash lock, so we're 3379244781f1SPrakash Surya * asserting on this assumption to be defensive in case 3380244781f1SPrakash Surya * this ever changes. Without this check, it would be 3381244781f1SPrakash Surya * possible to incorrectly increment arcstat_mutex_miss 3382244781f1SPrakash Surya * below (e.g. if the code changed such that we called 3383244781f1SPrakash Surya * this function with a hash lock held). 3384244781f1SPrakash Surya */ 3385244781f1SPrakash Surya ASSERT(!MUTEX_HELD(hash_lock)); 338644cb6abcSbmc 3387244781f1SPrakash Surya if (mutex_tryenter(hash_lock)) { 3388244781f1SPrakash Surya uint64_t evicted = arc_evict_hdr(hdr, hash_lock); 3389244781f1SPrakash Surya mutex_exit(hash_lock); 3390fa9e4066Sahrens 3391244781f1SPrakash Surya bytes_evicted += evicted; 3392fa9e4066Sahrens 3393244781f1SPrakash Surya /* 3394244781f1SPrakash Surya * If evicted is zero, arc_evict_hdr() must have 3395244781f1SPrakash Surya * decided to skip this header, don't increment 3396244781f1SPrakash Surya * evict_count in this case. 3397244781f1SPrakash Surya */ 3398244781f1SPrakash Surya if (evicted != 0) 3399244781f1SPrakash Surya evict_count++; 340044cb6abcSbmc 3401244781f1SPrakash Surya /* 3402244781f1SPrakash Surya * If arc_size isn't overflowing, signal any 3403244781f1SPrakash Surya * threads that might happen to be waiting. 3404244781f1SPrakash Surya * 3405244781f1SPrakash Surya * For each header evicted, we wake up a single 3406244781f1SPrakash Surya * thread. If we used cv_broadcast, we could 3407244781f1SPrakash Surya * wake up "too many" threads causing arc_size 3408244781f1SPrakash Surya * to significantly overflow arc_c; since 3409770499e1SDan Kimmel * arc_get_data_impl() doesn't check for overflow 3410244781f1SPrakash Surya * when it's woken up (it doesn't because it's 3411244781f1SPrakash Surya * possible for the ARC to be overflowing while 3412244781f1SPrakash Surya * full of un-evictable buffers, and the 3413244781f1SPrakash Surya * function should proceed in this case). 3414244781f1SPrakash Surya * 3415244781f1SPrakash Surya * If threads are left sleeping, due to not 3416244781f1SPrakash Surya * using cv_broadcast, they will be woken up 3417244781f1SPrakash Surya * just before arc_reclaim_thread() sleeps. 3418244781f1SPrakash Surya */ 3419244781f1SPrakash Surya mutex_enter(&arc_reclaim_lock); 3420244781f1SPrakash Surya if (!arc_is_overflowing()) 3421244781f1SPrakash Surya cv_signal(&arc_reclaim_waiters_cv); 3422244781f1SPrakash Surya mutex_exit(&arc_reclaim_lock); 3423244781f1SPrakash Surya } else { 3424244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_mutex_miss); 3425244781f1SPrakash Surya } 3426244781f1SPrakash Surya } 3427f4d2e9e6Smaybee 3428244781f1SPrakash Surya multilist_sublist_unlock(mls); 342944cb6abcSbmc 3430244781f1SPrakash Surya return (bytes_evicted); 3431fa9e4066Sahrens } 3432fa9e4066Sahrens 3433fa9e4066Sahrens /* 3434244781f1SPrakash Surya * Evict buffers from the given arc state, until we've removed the 3435244781f1SPrakash Surya * specified number of bytes. Move the removed buffers to the 3436244781f1SPrakash Surya * appropriate evict state. 3437244781f1SPrakash Surya * 3438244781f1SPrakash Surya * This function makes a "best effort". It skips over any buffers 3439244781f1SPrakash Surya * it can't get a hash_lock on, and so, may not catch all candidates. 3440244781f1SPrakash Surya * It may also return without evicting as much space as requested. 3441244781f1SPrakash Surya * 3442244781f1SPrakash Surya * If bytes is specified using the special value ARC_EVICT_ALL, this 3443244781f1SPrakash Surya * will evict all available (i.e. unlocked and evictable) buffers from 3444244781f1SPrakash Surya * the given arc state; which is used by arc_flush(). 3445fa9e4066Sahrens */ 3446244781f1SPrakash Surya static uint64_t 3447244781f1SPrakash Surya arc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes, 3448244781f1SPrakash Surya arc_buf_contents_t type) 3449fa9e4066Sahrens { 3450244781f1SPrakash Surya uint64_t total_evicted = 0; 345194c2d0ebSMatthew Ahrens multilist_t *ml = state->arcs_list[type]; 3452244781f1SPrakash Surya int num_sublists; 3453244781f1SPrakash Surya arc_buf_hdr_t **markers; 3454fa9e4066Sahrens 3455244781f1SPrakash Surya IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 3456b802aa8cSSanjeev Bagewadi 3457244781f1SPrakash Surya num_sublists = multilist_get_num_sublists(ml); 3458b802aa8cSSanjeev Bagewadi 3459244781f1SPrakash Surya /* 3460244781f1SPrakash Surya * If we've tried to evict from each sublist, made some 3461244781f1SPrakash Surya * progress, but still have not hit the target number of bytes 3462244781f1SPrakash Surya * to evict, we want to keep trying. The markers allow us to 3463244781f1SPrakash Surya * pick up where we left off for each individual sublist, rather 3464244781f1SPrakash Surya * than starting from the tail each time. 3465244781f1SPrakash Surya */ 3466244781f1SPrakash Surya markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); 3467244781f1SPrakash Surya for (int i = 0; i < num_sublists; i++) { 3468244781f1SPrakash Surya markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); 346969962b56SMatthew Ahrens 347069962b56SMatthew Ahrens /* 3471244781f1SPrakash Surya * A b_spa of 0 is used to indicate that this header is 3472244781f1SPrakash Surya * a marker. This fact is used in arc_adjust_type() and 3473244781f1SPrakash Surya * arc_evict_state_impl(). 347469962b56SMatthew Ahrens */ 3475244781f1SPrakash Surya markers[i]->b_spa = 0; 3476fa94a07fSbrendan 3477244781f1SPrakash Surya multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 3478244781f1SPrakash Surya multilist_sublist_insert_tail(mls, markers[i]); 3479244781f1SPrakash Surya multilist_sublist_unlock(mls); 3480244781f1SPrakash Surya } 3481fa94a07fSbrendan 3482244781f1SPrakash Surya /* 3483244781f1SPrakash Surya * While we haven't hit our target number of bytes to evict, or 3484244781f1SPrakash Surya * we're evicting all available buffers. 3485244781f1SPrakash Surya */ 3486244781f1SPrakash Surya while (total_evicted < bytes || bytes == ARC_EVICT_ALL) { 3487244781f1SPrakash Surya /* 3488244781f1SPrakash Surya * Start eviction using a randomly selected sublist, 3489244781f1SPrakash Surya * this is to try and evenly balance eviction across all 3490244781f1SPrakash Surya * sublists. Always starting at the same sublist 3491244781f1SPrakash Surya * (e.g. index 0) would cause evictions to favor certain 3492244781f1SPrakash Surya * sublists over others. 3493244781f1SPrakash Surya */ 3494244781f1SPrakash Surya int sublist_idx = multilist_get_random_index(ml); 3495244781f1SPrakash Surya uint64_t scan_evicted = 0; 3496244781f1SPrakash Surya 3497244781f1SPrakash Surya for (int i = 0; i < num_sublists; i++) { 3498244781f1SPrakash Surya uint64_t bytes_remaining; 3499244781f1SPrakash Surya uint64_t bytes_evicted; 3500244781f1SPrakash Surya 3501244781f1SPrakash Surya if (bytes == ARC_EVICT_ALL) 3502244781f1SPrakash Surya bytes_remaining = ARC_EVICT_ALL; 3503244781f1SPrakash Surya else if (total_evicted < bytes) 3504244781f1SPrakash Surya bytes_remaining = bytes - total_evicted; 3505244781f1SPrakash Surya else 3506fa9e4066Sahrens break; 3507244781f1SPrakash Surya 3508244781f1SPrakash Surya bytes_evicted = arc_evict_state_impl(ml, sublist_idx, 3509244781f1SPrakash Surya markers[sublist_idx], spa, bytes_remaining); 3510244781f1SPrakash Surya 3511244781f1SPrakash Surya scan_evicted += bytes_evicted; 3512244781f1SPrakash Surya total_evicted += bytes_evicted; 3513244781f1SPrakash Surya 3514244781f1SPrakash Surya /* we've reached the end, wrap to the beginning */ 3515244781f1SPrakash Surya if (++sublist_idx >= num_sublists) 3516244781f1SPrakash Surya sublist_idx = 0; 3517244781f1SPrakash Surya } 3518244781f1SPrakash Surya 3519244781f1SPrakash Surya /* 3520244781f1SPrakash Surya * If we didn't evict anything during this scan, we have 3521244781f1SPrakash Surya * no reason to believe we'll evict more during another 3522244781f1SPrakash Surya * scan, so break the loop. 3523244781f1SPrakash Surya */ 3524244781f1SPrakash Surya if (scan_evicted == 0) { 3525244781f1SPrakash Surya /* This isn't possible, let's make that obvious */ 3526244781f1SPrakash Surya ASSERT3S(bytes, !=, 0); 3527244781f1SPrakash Surya 3528b802aa8cSSanjeev Bagewadi /* 3529244781f1SPrakash Surya * When bytes is ARC_EVICT_ALL, the only way to 3530244781f1SPrakash Surya * break the loop is when scan_evicted is zero. 3531244781f1SPrakash Surya * In that case, we actually have evicted enough, 3532244781f1SPrakash Surya * so we don't want to increment the kstat. 3533b802aa8cSSanjeev Bagewadi */ 3534244781f1SPrakash Surya if (bytes != ARC_EVICT_ALL) { 3535244781f1SPrakash Surya ASSERT3S(total_evicted, <, bytes); 3536244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_not_enough); 3537244781f1SPrakash Surya } 3538244781f1SPrakash Surya 3539244781f1SPrakash Surya break; 354069962b56SMatthew Ahrens } 3541244781f1SPrakash Surya } 3542244781f1SPrakash Surya 3543244781f1SPrakash Surya for (int i = 0; i < num_sublists; i++) { 3544244781f1SPrakash Surya multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 3545244781f1SPrakash Surya multilist_sublist_remove(mls, markers[i]); 3546244781f1SPrakash Surya multilist_sublist_unlock(mls); 354769962b56SMatthew Ahrens 3548244781f1SPrakash Surya kmem_cache_free(hdr_full_cache, markers[i]); 3549fa9e4066Sahrens } 3550244781f1SPrakash Surya kmem_free(markers, sizeof (*markers) * num_sublists); 3551fa9e4066Sahrens 3552244781f1SPrakash Surya return (total_evicted); 3553244781f1SPrakash Surya } 3554244781f1SPrakash Surya 3555244781f1SPrakash Surya /* 3556244781f1SPrakash Surya * Flush all "evictable" data of the given type from the arc state 3557244781f1SPrakash Surya * specified. This will not evict any "active" buffers (i.e. referenced). 3558244781f1SPrakash Surya * 3559dcbf3bd6SGeorge Wilson * When 'retry' is set to B_FALSE, the function will make a single pass 3560244781f1SPrakash Surya * over the state and evict any buffers that it can. Since it doesn't 3561244781f1SPrakash Surya * continually retry the eviction, it might end up leaving some buffers 3562244781f1SPrakash Surya * in the ARC due to lock misses. 3563244781f1SPrakash Surya * 3564dcbf3bd6SGeorge Wilson * When 'retry' is set to B_TRUE, the function will continually retry the 3565244781f1SPrakash Surya * eviction until *all* evictable buffers have been removed from the 3566244781f1SPrakash Surya * state. As a result, if concurrent insertions into the state are 3567244781f1SPrakash Surya * allowed (e.g. if the ARC isn't shutting down), this function might 3568244781f1SPrakash Surya * wind up in an infinite loop, continually trying to evict buffers. 3569244781f1SPrakash Surya */ 3570244781f1SPrakash Surya static uint64_t 3571244781f1SPrakash Surya arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, 3572244781f1SPrakash Surya boolean_t retry) 3573244781f1SPrakash Surya { 3574244781f1SPrakash Surya uint64_t evicted = 0; 3575244781f1SPrakash Surya 3576dcbf3bd6SGeorge Wilson while (refcount_count(&state->arcs_esize[type]) != 0) { 3577244781f1SPrakash Surya evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); 3578244781f1SPrakash Surya 3579244781f1SPrakash Surya if (!retry) 3580244781f1SPrakash Surya break; 35810e8c6158Smaybee } 35820e8c6158Smaybee 3583244781f1SPrakash Surya return (evicted); 3584244781f1SPrakash Surya } 3585244781f1SPrakash Surya 3586244781f1SPrakash Surya /* 3587244781f1SPrakash Surya * Evict the specified number of bytes from the state specified, 3588244781f1SPrakash Surya * restricting eviction to the spa and type given. This function 3589244781f1SPrakash Surya * prevents us from trying to evict more from a state's list than 3590244781f1SPrakash Surya * is "evictable", and to skip evicting altogether when passed a 3591244781f1SPrakash Surya * negative value for "bytes". In contrast, arc_evict_state() will 3592244781f1SPrakash Surya * evict everything it can, when passed a negative value for "bytes". 3593244781f1SPrakash Surya */ 3594244781f1SPrakash Surya static uint64_t 3595244781f1SPrakash Surya arc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes, 3596244781f1SPrakash Surya arc_buf_contents_t type) 3597244781f1SPrakash Surya { 3598244781f1SPrakash Surya int64_t delta; 3599244781f1SPrakash Surya 3600dcbf3bd6SGeorge Wilson if (bytes > 0 && refcount_count(&state->arcs_esize[type]) > 0) { 3601dcbf3bd6SGeorge Wilson delta = MIN(refcount_count(&state->arcs_esize[type]), bytes); 3602244781f1SPrakash Surya return (arc_evict_state(state, spa, delta, type)); 3603fa9e4066Sahrens } 3604fa9e4066Sahrens 3605244781f1SPrakash Surya return (0); 3606fa9e4066Sahrens } 3607fa9e4066Sahrens 3608244781f1SPrakash Surya /* 3609244781f1SPrakash Surya * Evict metadata buffers from the cache, such that arc_meta_used is 3610244781f1SPrakash Surya * capped by the arc_meta_limit tunable. 3611244781f1SPrakash Surya */ 3612244781f1SPrakash Surya static uint64_t 3613*3a2d8a1bSPaul Dagnelie arc_adjust_meta(uint64_t meta_used) 3614244781f1SPrakash Surya { 3615244781f1SPrakash Surya uint64_t total_evicted = 0; 3616244781f1SPrakash Surya int64_t target; 3617244781f1SPrakash Surya 3618244781f1SPrakash Surya /* 3619244781f1SPrakash Surya * If we're over the meta limit, we want to evict enough 3620244781f1SPrakash Surya * metadata to get back under the meta limit. We don't want to 3621244781f1SPrakash Surya * evict so much that we drop the MRU below arc_p, though. If 3622244781f1SPrakash Surya * we're over the meta limit more than we're over arc_p, we 3623244781f1SPrakash Surya * evict some from the MRU here, and some from the MFU below. 3624244781f1SPrakash Surya */ 3625*3a2d8a1bSPaul Dagnelie target = MIN((int64_t)(meta_used - arc_meta_limit), 36262fd872a7SPrakash Surya (int64_t)(refcount_count(&arc_anon->arcs_size) + 36272fd872a7SPrakash Surya refcount_count(&arc_mru->arcs_size) - arc_p)); 3628244781f1SPrakash Surya 3629244781f1SPrakash Surya total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3630244781f1SPrakash Surya 3631244781f1SPrakash Surya /* 3632244781f1SPrakash Surya * Similar to the above, we want to evict enough bytes to get us 3633244781f1SPrakash Surya * below the meta limit, but not so much as to drop us below the 36345602294fSDan Kimmel * space allotted to the MFU (which is defined as arc_c - arc_p). 3635244781f1SPrakash Surya */ 3636*3a2d8a1bSPaul Dagnelie target = MIN((int64_t)(meta_used - arc_meta_limit), 3637*3a2d8a1bSPaul Dagnelie (int64_t)(refcount_count(&arc_mfu->arcs_size) - 3638*3a2d8a1bSPaul Dagnelie (arc_c - arc_p))); 3639244781f1SPrakash Surya 3640244781f1SPrakash Surya total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3641244781f1SPrakash Surya 3642244781f1SPrakash Surya return (total_evicted); 3643244781f1SPrakash Surya } 3644244781f1SPrakash Surya 3645244781f1SPrakash Surya /* 3646244781f1SPrakash Surya * Return the type of the oldest buffer in the given arc state 3647244781f1SPrakash Surya * 3648244781f1SPrakash Surya * This function will select a random sublist of type ARC_BUFC_DATA and 3649244781f1SPrakash Surya * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist 3650244781f1SPrakash Surya * is compared, and the type which contains the "older" buffer will be 3651244781f1SPrakash Surya * returned. 3652244781f1SPrakash Surya */ 3653244781f1SPrakash Surya static arc_buf_contents_t 3654244781f1SPrakash Surya arc_adjust_type(arc_state_t *state) 3655244781f1SPrakash Surya { 365694c2d0ebSMatthew Ahrens multilist_t *data_ml = state->arcs_list[ARC_BUFC_DATA]; 365794c2d0ebSMatthew Ahrens multilist_t *meta_ml = state->arcs_list[ARC_BUFC_METADATA]; 3658244781f1SPrakash Surya int data_idx = multilist_get_random_index(data_ml); 3659244781f1SPrakash Surya int meta_idx = multilist_get_random_index(meta_ml); 3660244781f1SPrakash Surya multilist_sublist_t *data_mls; 3661244781f1SPrakash Surya multilist_sublist_t *meta_mls; 3662244781f1SPrakash Surya arc_buf_contents_t type; 3663244781f1SPrakash Surya arc_buf_hdr_t *data_hdr; 3664244781f1SPrakash Surya arc_buf_hdr_t *meta_hdr; 3665244781f1SPrakash Surya 3666244781f1SPrakash Surya /* 3667244781f1SPrakash Surya * We keep the sublist lock until we're finished, to prevent 3668244781f1SPrakash Surya * the headers from being destroyed via arc_evict_state(). 3669244781f1SPrakash Surya */ 3670244781f1SPrakash Surya data_mls = multilist_sublist_lock(data_ml, data_idx); 3671244781f1SPrakash Surya meta_mls = multilist_sublist_lock(meta_ml, meta_idx); 3672244781f1SPrakash Surya 3673244781f1SPrakash Surya /* 3674244781f1SPrakash Surya * These two loops are to ensure we skip any markers that 3675244781f1SPrakash Surya * might be at the tail of the lists due to arc_evict_state(). 3676244781f1SPrakash Surya */ 3677244781f1SPrakash Surya 3678244781f1SPrakash Surya for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; 3679244781f1SPrakash Surya data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { 3680244781f1SPrakash Surya if (data_hdr->b_spa != 0) 3681244781f1SPrakash Surya break; 3682244781f1SPrakash Surya } 3683244781f1SPrakash Surya 3684244781f1SPrakash Surya for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; 3685244781f1SPrakash Surya meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { 3686244781f1SPrakash Surya if (meta_hdr->b_spa != 0) 3687244781f1SPrakash Surya break; 3688244781f1SPrakash Surya } 3689244781f1SPrakash Surya 3690244781f1SPrakash Surya if (data_hdr == NULL && meta_hdr == NULL) { 3691244781f1SPrakash Surya type = ARC_BUFC_DATA; 3692244781f1SPrakash Surya } else if (data_hdr == NULL) { 3693244781f1SPrakash Surya ASSERT3P(meta_hdr, !=, NULL); 3694244781f1SPrakash Surya type = ARC_BUFC_METADATA; 3695244781f1SPrakash Surya } else if (meta_hdr == NULL) { 3696244781f1SPrakash Surya ASSERT3P(data_hdr, !=, NULL); 3697244781f1SPrakash Surya type = ARC_BUFC_DATA; 3698244781f1SPrakash Surya } else { 3699244781f1SPrakash Surya ASSERT3P(data_hdr, !=, NULL); 3700244781f1SPrakash Surya ASSERT3P(meta_hdr, !=, NULL); 3701244781f1SPrakash Surya 3702244781f1SPrakash Surya /* The headers can't be on the sublist without an L1 header */ 3703244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(data_hdr)); 3704244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(meta_hdr)); 3705244781f1SPrakash Surya 3706244781f1SPrakash Surya if (data_hdr->b_l1hdr.b_arc_access < 3707244781f1SPrakash Surya meta_hdr->b_l1hdr.b_arc_access) { 3708244781f1SPrakash Surya type = ARC_BUFC_DATA; 3709244781f1SPrakash Surya } else { 3710244781f1SPrakash Surya type = ARC_BUFC_METADATA; 3711244781f1SPrakash Surya } 3712244781f1SPrakash Surya } 3713244781f1SPrakash Surya 3714244781f1SPrakash Surya multilist_sublist_unlock(meta_mls); 3715244781f1SPrakash Surya multilist_sublist_unlock(data_mls); 3716244781f1SPrakash Surya 3717244781f1SPrakash Surya return (type); 3718244781f1SPrakash Surya } 3719244781f1SPrakash Surya 3720244781f1SPrakash Surya /* 3721244781f1SPrakash Surya * Evict buffers from the cache, such that arc_size is capped by arc_c. 3722244781f1SPrakash Surya */ 3723244781f1SPrakash Surya static uint64_t 3724fa9e4066Sahrens arc_adjust(void) 3725fa9e4066Sahrens { 3726244781f1SPrakash Surya uint64_t total_evicted = 0; 3727244781f1SPrakash Surya uint64_t bytes; 3728244781f1SPrakash Surya int64_t target; 3729*3a2d8a1bSPaul Dagnelie uint64_t asize = aggsum_value(&arc_size); 3730*3a2d8a1bSPaul Dagnelie uint64_t ameta = aggsum_value(&arc_meta_used); 3731fa9e4066Sahrens 37325a98e54bSBrendan Gregg - Sun Microsystems /* 3733244781f1SPrakash Surya * If we're over arc_meta_limit, we want to correct that before 3734244781f1SPrakash Surya * potentially evicting data buffers below. 37355a98e54bSBrendan Gregg - Sun Microsystems */ 3736*3a2d8a1bSPaul Dagnelie total_evicted += arc_adjust_meta(ameta); 37375a98e54bSBrendan Gregg - Sun Microsystems 3738244781f1SPrakash Surya /* 3739244781f1SPrakash Surya * Adjust MRU size 3740244781f1SPrakash Surya * 3741244781f1SPrakash Surya * If we're over the target cache size, we want to evict enough 3742244781f1SPrakash Surya * from the list to get back to our target size. We don't want 3743244781f1SPrakash Surya * to evict too much from the MRU, such that it drops below 3744244781f1SPrakash Surya * arc_p. So, if we're over our target cache size more than 3745244781f1SPrakash Surya * the MRU is over arc_p, we'll evict enough to get back to 3746244781f1SPrakash Surya * arc_p here, and then evict more from the MFU below. 3747244781f1SPrakash Surya */ 3748*3a2d8a1bSPaul Dagnelie target = MIN((int64_t)(asize - arc_c), 37492fd872a7SPrakash Surya (int64_t)(refcount_count(&arc_anon->arcs_size) + 3750*3a2d8a1bSPaul Dagnelie refcount_count(&arc_mru->arcs_size) + ameta - arc_p)); 3751fa9e4066Sahrens 3752244781f1SPrakash Surya /* 3753244781f1SPrakash Surya * If we're below arc_meta_min, always prefer to evict data. 3754244781f1SPrakash Surya * Otherwise, try to satisfy the requested number of bytes to 3755244781f1SPrakash Surya * evict from the type which contains older buffers; in an 3756244781f1SPrakash Surya * effort to keep newer buffers in the cache regardless of their 3757244781f1SPrakash Surya * type. If we cannot satisfy the number of bytes from this 3758244781f1SPrakash Surya * type, spill over into the next type. 3759244781f1SPrakash Surya */ 3760244781f1SPrakash Surya if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA && 3761*3a2d8a1bSPaul Dagnelie ameta > arc_meta_min) { 3762244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3763244781f1SPrakash Surya total_evicted += bytes; 37640e8c6158Smaybee 3765244781f1SPrakash Surya /* 3766244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3767244781f1SPrakash Surya * metadata, we try to get the rest from data. 3768244781f1SPrakash Surya */ 3769244781f1SPrakash Surya target -= bytes; 3770244781f1SPrakash Surya 3771244781f1SPrakash Surya total_evicted += 3772244781f1SPrakash Surya arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3773244781f1SPrakash Surya } else { 3774244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3775244781f1SPrakash Surya total_evicted += bytes; 3776244781f1SPrakash Surya 3777244781f1SPrakash Surya /* 3778244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3779244781f1SPrakash Surya * data, we try to get the rest from metadata. 3780244781f1SPrakash Surya */ 3781244781f1SPrakash Surya target -= bytes; 3782244781f1SPrakash Surya 3783244781f1SPrakash Surya total_evicted += 3784244781f1SPrakash Surya arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3785fa9e4066Sahrens } 3786fa9e4066Sahrens 37875a98e54bSBrendan Gregg - Sun Microsystems /* 37885a98e54bSBrendan Gregg - Sun Microsystems * Adjust MFU size 3789244781f1SPrakash Surya * 3790244781f1SPrakash Surya * Now that we've tried to evict enough from the MRU to get its 3791244781f1SPrakash Surya * size back to arc_p, if we're still above the target cache 3792244781f1SPrakash Surya * size, we evict the rest from the MFU. 37935a98e54bSBrendan Gregg - Sun Microsystems */ 3794*3a2d8a1bSPaul Dagnelie target = asize - arc_c; 3795fa9e4066Sahrens 379631c46cf2SAlek Pinchuk if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA && 3797*3a2d8a1bSPaul Dagnelie ameta > arc_meta_min) { 3798244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3799244781f1SPrakash Surya total_evicted += bytes; 38005a98e54bSBrendan Gregg - Sun Microsystems 3801244781f1SPrakash Surya /* 3802244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3803244781f1SPrakash Surya * metadata, we try to get the rest from data. 3804244781f1SPrakash Surya */ 3805244781f1SPrakash Surya target -= bytes; 3806244781f1SPrakash Surya 3807244781f1SPrakash Surya total_evicted += 3808244781f1SPrakash Surya arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3809244781f1SPrakash Surya } else { 3810244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3811244781f1SPrakash Surya total_evicted += bytes; 3812244781f1SPrakash Surya 3813244781f1SPrakash Surya /* 3814244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3815244781f1SPrakash Surya * data, we try to get the rest from data. 3816244781f1SPrakash Surya */ 3817244781f1SPrakash Surya target -= bytes; 3818fa9e4066Sahrens 3819244781f1SPrakash Surya total_evicted += 3820244781f1SPrakash Surya arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 38215a98e54bSBrendan Gregg - Sun Microsystems } 3822fa9e4066Sahrens 38235a98e54bSBrendan Gregg - Sun Microsystems /* 38245a98e54bSBrendan Gregg - Sun Microsystems * Adjust ghost lists 3825244781f1SPrakash Surya * 3826244781f1SPrakash Surya * In addition to the above, the ARC also defines target values 3827244781f1SPrakash Surya * for the ghost lists. The sum of the mru list and mru ghost 3828244781f1SPrakash Surya * list should never exceed the target size of the cache, and 3829244781f1SPrakash Surya * the sum of the mru list, mfu list, mru ghost list, and mfu 3830244781f1SPrakash Surya * ghost list should never exceed twice the target size of the 3831244781f1SPrakash Surya * cache. The following logic enforces these limits on the ghost 3832244781f1SPrakash Surya * caches, and evicts from them as needed. 38335a98e54bSBrendan Gregg - Sun Microsystems */ 38342fd872a7SPrakash Surya target = refcount_count(&arc_mru->arcs_size) + 38352fd872a7SPrakash Surya refcount_count(&arc_mru_ghost->arcs_size) - arc_c; 3836fa9e4066Sahrens 3837244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); 3838244781f1SPrakash Surya total_evicted += bytes; 3839fa9e4066Sahrens 3840244781f1SPrakash Surya target -= bytes; 38410e8c6158Smaybee 3842244781f1SPrakash Surya total_evicted += 3843244781f1SPrakash Surya arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); 38445a98e54bSBrendan Gregg - Sun Microsystems 3845244781f1SPrakash Surya /* 3846244781f1SPrakash Surya * We assume the sum of the mru list and mfu list is less than 3847244781f1SPrakash Surya * or equal to arc_c (we enforced this above), which means we 3848244781f1SPrakash Surya * can use the simpler of the two equations below: 3849244781f1SPrakash Surya * 3850244781f1SPrakash Surya * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c 3851244781f1SPrakash Surya * mru ghost + mfu ghost <= arc_c 3852244781f1SPrakash Surya */ 38532fd872a7SPrakash Surya target = refcount_count(&arc_mru_ghost->arcs_size) + 38542fd872a7SPrakash Surya refcount_count(&arc_mfu_ghost->arcs_size) - arc_c; 3855244781f1SPrakash Surya 3856244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); 3857244781f1SPrakash Surya total_evicted += bytes; 3858244781f1SPrakash Surya 3859244781f1SPrakash Surya target -= bytes; 3860244781f1SPrakash Surya 3861244781f1SPrakash Surya total_evicted += 3862244781f1SPrakash Surya arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); 3863244781f1SPrakash Surya 3864244781f1SPrakash Surya return (total_evicted); 3865fa9e4066Sahrens } 3866fa9e4066Sahrens 3867fa9e4066Sahrens void 3868244781f1SPrakash Surya arc_flush(spa_t *spa, boolean_t retry) 3869fa9e4066Sahrens { 3870ac05c741SMark Maybee uint64_t guid = 0; 3871ac05c741SMark Maybee 3872244781f1SPrakash Surya /* 3873dcbf3bd6SGeorge Wilson * If retry is B_TRUE, a spa must not be specified since we have 3874244781f1SPrakash Surya * no good way to determine if all of a spa's buffers have been 3875244781f1SPrakash Surya * evicted from an arc state. 3876244781f1SPrakash Surya */ 3877244781f1SPrakash Surya ASSERT(!retry || spa == 0); 3878244781f1SPrakash Surya 387989c86e32SChris Williamson if (spa != NULL) 3880e9103aaeSGarrett D'Amore guid = spa_load_guid(spa); 3881ac05c741SMark Maybee 3882244781f1SPrakash Surya (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); 3883244781f1SPrakash Surya (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); 3884244781f1SPrakash Surya 3885244781f1SPrakash Surya (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); 3886244781f1SPrakash Surya (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); 3887874395d5Smaybee 3888244781f1SPrakash Surya (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); 3889244781f1SPrakash Surya (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); 3890244781f1SPrakash Surya 3891244781f1SPrakash Surya (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); 3892244781f1SPrakash Surya (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); 3893fa9e4066Sahrens } 3894fa9e4066Sahrens 3895fa9e4066Sahrens void 38962ec99e3eSMatthew Ahrens arc_shrink(int64_t to_free) 3897fa9e4066Sahrens { 3898*3a2d8a1bSPaul Dagnelie uint64_t asize = aggsum_value(&arc_size); 389944cb6abcSbmc if (arc_c > arc_c_min) { 3900fa9e4066Sahrens 390144cb6abcSbmc if (arc_c > arc_c_min + to_free) 390244cb6abcSbmc atomic_add_64(&arc_c, -to_free); 390349e3519aSmaybee else 390444cb6abcSbmc arc_c = arc_c_min; 390544cb6abcSbmc 390644cb6abcSbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 3907*3a2d8a1bSPaul Dagnelie if (asize < arc_c) 3908*3a2d8a1bSPaul Dagnelie arc_c = MAX(asize, arc_c_min); 390944cb6abcSbmc if (arc_p > arc_c) 391044cb6abcSbmc arc_p = (arc_c >> 1); 391144cb6abcSbmc ASSERT(arc_c >= arc_c_min); 391244cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 391349e3519aSmaybee } 3914fa9e4066Sahrens 3915*3a2d8a1bSPaul Dagnelie if (asize > arc_c) 3916244781f1SPrakash Surya (void) arc_adjust(); 3917fa9e4066Sahrens } 3918fa9e4066Sahrens 39192ec99e3eSMatthew Ahrens typedef enum free_memory_reason_t { 39202ec99e3eSMatthew Ahrens FMR_UNKNOWN, 39212ec99e3eSMatthew Ahrens FMR_NEEDFREE, 39222ec99e3eSMatthew Ahrens FMR_LOTSFREE, 39232ec99e3eSMatthew Ahrens FMR_SWAPFS_MINFREE, 39242ec99e3eSMatthew Ahrens FMR_PAGES_PP_MAXIMUM, 39252ec99e3eSMatthew Ahrens FMR_HEAP_ARENA, 39262ec99e3eSMatthew Ahrens FMR_ZIO_ARENA, 39272ec99e3eSMatthew Ahrens } free_memory_reason_t; 39282ec99e3eSMatthew Ahrens 39292ec99e3eSMatthew Ahrens int64_t last_free_memory; 39302ec99e3eSMatthew Ahrens free_memory_reason_t last_free_reason; 39312ec99e3eSMatthew Ahrens 393294dd93aeSGeorge Wilson /* 39332ec99e3eSMatthew Ahrens * Additional reserve of pages for pp_reserve. 393494dd93aeSGeorge Wilson */ 39352ec99e3eSMatthew Ahrens int64_t arc_pages_pp_reserve = 64; 3936fa9e4066Sahrens 39372ec99e3eSMatthew Ahrens /* 39382ec99e3eSMatthew Ahrens * Additional reserve of pages for swapfs. 39392ec99e3eSMatthew Ahrens */ 39402ec99e3eSMatthew Ahrens int64_t arc_swapfs_reserve = 64; 39413cff2f43Sstans 39422ec99e3eSMatthew Ahrens /* 39432ec99e3eSMatthew Ahrens * Return the amount of memory that can be consumed before reclaim will be 39442ec99e3eSMatthew Ahrens * needed. Positive if there is sufficient free memory, negative indicates 39452ec99e3eSMatthew Ahrens * the amount of memory that needs to be freed up. 39462ec99e3eSMatthew Ahrens */ 39472ec99e3eSMatthew Ahrens static int64_t 39482ec99e3eSMatthew Ahrens arc_available_memory(void) 39492ec99e3eSMatthew Ahrens { 39502ec99e3eSMatthew Ahrens int64_t lowest = INT64_MAX; 39512ec99e3eSMatthew Ahrens int64_t n; 39522ec99e3eSMatthew Ahrens free_memory_reason_t r = FMR_UNKNOWN; 39533cff2f43Sstans 39542ec99e3eSMatthew Ahrens #ifdef _KERNEL 39552ec99e3eSMatthew Ahrens if (needfree > 0) { 39562ec99e3eSMatthew Ahrens n = PAGESIZE * (-needfree); 39572ec99e3eSMatthew Ahrens if (n < lowest) { 39582ec99e3eSMatthew Ahrens lowest = n; 39592ec99e3eSMatthew Ahrens r = FMR_NEEDFREE; 39602ec99e3eSMatthew Ahrens } 39612ec99e3eSMatthew Ahrens } 3962fa9e4066Sahrens 3963fa9e4066Sahrens /* 3964fa9e4066Sahrens * check that we're out of range of the pageout scanner. It starts to 3965fa9e4066Sahrens * schedule paging if freemem is less than lotsfree and needfree. 3966fa9e4066Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 3967fa9e4066Sahrens * number of needed free pages. We add extra pages here to make sure 3968fa9e4066Sahrens * the scanner doesn't start up while we're freeing memory. 3969fa9e4066Sahrens */ 39702ec99e3eSMatthew Ahrens n = PAGESIZE * (freemem - lotsfree - needfree - desfree); 39712ec99e3eSMatthew Ahrens if (n < lowest) { 39722ec99e3eSMatthew Ahrens lowest = n; 39732ec99e3eSMatthew Ahrens r = FMR_LOTSFREE; 39742ec99e3eSMatthew Ahrens } 3975fa9e4066Sahrens 3976fa9e4066Sahrens /* 3977fa9e4066Sahrens * check to make sure that swapfs has enough space so that anon 3978fa94a07fSbrendan * reservations can still succeed. anon_resvmem() checks that the 3979fa9e4066Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 3980fa9e4066Sahrens * swap pages. We also add a bit of extra here just to prevent 3981fa9e4066Sahrens * circumstances from getting really dire. 3982fa9e4066Sahrens */ 39832ec99e3eSMatthew Ahrens n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve - 39842ec99e3eSMatthew Ahrens desfree - arc_swapfs_reserve); 39852ec99e3eSMatthew Ahrens if (n < lowest) { 39862ec99e3eSMatthew Ahrens lowest = n; 39872ec99e3eSMatthew Ahrens r = FMR_SWAPFS_MINFREE; 39882ec99e3eSMatthew Ahrens } 39892ec99e3eSMatthew Ahrens 3990fa9e4066Sahrens 3991cf746768SBryan Cantrill /* 3992cf746768SBryan Cantrill * Check that we have enough availrmem that memory locking (e.g., via 3993cf746768SBryan Cantrill * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 3994cf746768SBryan Cantrill * stores the number of pages that cannot be locked; when availrmem 3995cf746768SBryan Cantrill * drops below pages_pp_maximum, page locking mechanisms such as 3996cf746768SBryan Cantrill * page_pp_lock() will fail.) 3997cf746768SBryan Cantrill */ 39982ec99e3eSMatthew Ahrens n = PAGESIZE * (availrmem - pages_pp_maximum - 39992ec99e3eSMatthew Ahrens arc_pages_pp_reserve); 40002ec99e3eSMatthew Ahrens if (n < lowest) { 40012ec99e3eSMatthew Ahrens lowest = n; 40022ec99e3eSMatthew Ahrens r = FMR_PAGES_PP_MAXIMUM; 40032ec99e3eSMatthew Ahrens } 4004cf746768SBryan Cantrill 40055dc8af33Smaybee #if defined(__i386) 4006fa9e4066Sahrens /* 4007fa9e4066Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 4008fa9e4066Sahrens * kernel heap space before we ever run out of available physical 4009fa9e4066Sahrens * memory. Most checks of the size of the heap_area compare against 4010fa9e4066Sahrens * tune.t_minarmem, which is the minimum available real memory that we 4011fa9e4066Sahrens * can have in the system. However, this is generally fixed at 25 pages 4012fa9e4066Sahrens * which is so low that it's useless. In this comparison, we seek to 4013fa9e4066Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 4014fa94a07fSbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 4015fa9e4066Sahrens * free) 4016fa9e4066Sahrens */ 40170dd053d7SPrakash Surya n = (int64_t)vmem_size(heap_arena, VMEM_FREE) - 40182ec99e3eSMatthew Ahrens (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2); 40192ec99e3eSMatthew Ahrens if (n < lowest) { 40202ec99e3eSMatthew Ahrens lowest = n; 40212ec99e3eSMatthew Ahrens r = FMR_HEAP_ARENA; 40222ec99e3eSMatthew Ahrens } 4023fa9e4066Sahrens #endif 4024fa9e4066Sahrens 402594dd93aeSGeorge Wilson /* 402694dd93aeSGeorge Wilson * If zio data pages are being allocated out of a separate heap segment, 402794dd93aeSGeorge Wilson * then enforce that the size of available vmem for this arena remains 40280dd053d7SPrakash Surya * above about 1/4th (1/(2^arc_zio_arena_free_shift)) free. 402994dd93aeSGeorge Wilson * 40300dd053d7SPrakash Surya * Note that reducing the arc_zio_arena_free_shift keeps more virtual 40310dd053d7SPrakash Surya * memory (in the zio_arena) free, which can avoid memory 40320dd053d7SPrakash Surya * fragmentation issues. 403394dd93aeSGeorge Wilson */ 40342ec99e3eSMatthew Ahrens if (zio_arena != NULL) { 40350dd053d7SPrakash Surya n = (int64_t)vmem_size(zio_arena, VMEM_FREE) - 40360dd053d7SPrakash Surya (vmem_size(zio_arena, VMEM_ALLOC) >> 40370dd053d7SPrakash Surya arc_zio_arena_free_shift); 40382ec99e3eSMatthew Ahrens if (n < lowest) { 40392ec99e3eSMatthew Ahrens lowest = n; 40402ec99e3eSMatthew Ahrens r = FMR_ZIO_ARENA; 40412ec99e3eSMatthew Ahrens } 40422ec99e3eSMatthew Ahrens } 4043fa9e4066Sahrens #else 40442ec99e3eSMatthew Ahrens /* Every 100 calls, free a small amount */ 4045fa9e4066Sahrens if (spa_get_random(100) == 0) 40462ec99e3eSMatthew Ahrens lowest = -1024; 4047fa9e4066Sahrens #endif 40482ec99e3eSMatthew Ahrens 40492ec99e3eSMatthew Ahrens last_free_memory = lowest; 40502ec99e3eSMatthew Ahrens last_free_reason = r; 40512ec99e3eSMatthew Ahrens 40522ec99e3eSMatthew Ahrens return (lowest); 40532ec99e3eSMatthew Ahrens } 40542ec99e3eSMatthew Ahrens 40552ec99e3eSMatthew Ahrens 40562ec99e3eSMatthew Ahrens /* 40572ec99e3eSMatthew Ahrens * Determine if the system is under memory pressure and is asking 4058dcbf3bd6SGeorge Wilson * to reclaim memory. A return value of B_TRUE indicates that the system 40592ec99e3eSMatthew Ahrens * is under memory pressure and that the arc should adjust accordingly. 40602ec99e3eSMatthew Ahrens */ 40612ec99e3eSMatthew Ahrens static boolean_t 40622ec99e3eSMatthew Ahrens arc_reclaim_needed(void) 40632ec99e3eSMatthew Ahrens { 40642ec99e3eSMatthew Ahrens return (arc_available_memory() < 0); 4065fa9e4066Sahrens } 4066fa9e4066Sahrens 4067fa9e4066Sahrens static void 40682ec99e3eSMatthew Ahrens arc_kmem_reap_now(void) 4069fa9e4066Sahrens { 4070fa9e4066Sahrens size_t i; 4071fa9e4066Sahrens kmem_cache_t *prev_cache = NULL; 4072ad23a2dbSjohansen kmem_cache_t *prev_data_cache = NULL; 4073fa9e4066Sahrens extern kmem_cache_t *zio_buf_cache[]; 4074ad23a2dbSjohansen extern kmem_cache_t *zio_data_buf_cache[]; 407583803b51SGeorge Wilson extern kmem_cache_t *range_seg_cache; 4076770499e1SDan Kimmel extern kmem_cache_t *abd_chunk_cache; 4077fa9e4066Sahrens 4078033f9833Sek #ifdef _KERNEL 4079*3a2d8a1bSPaul Dagnelie if (aggsum_compare(&arc_meta_used, arc_meta_limit) >= 0) { 40800e8c6158Smaybee /* 40810e8c6158Smaybee * We are exceeding our meta-data cache limit. 40820e8c6158Smaybee * Purge some DNLC entries to release holds on meta-data. 40830e8c6158Smaybee */ 40840e8c6158Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 40850e8c6158Smaybee } 40865dc8af33Smaybee #if defined(__i386) 40875dc8af33Smaybee /* 40885dc8af33Smaybee * Reclaim unused memory from all kmem caches. 40895dc8af33Smaybee */ 40905dc8af33Smaybee kmem_reap(); 40915dc8af33Smaybee #endif 4092033f9833Sek #endif 4093033f9833Sek 409436a64e62STim Kordas /* 409536a64e62STim Kordas * If a kmem reap is already active, don't schedule more. We must 409636a64e62STim Kordas * check for this because kmem_cache_reap_soon() won't actually 409736a64e62STim Kordas * block on the cache being reaped (this is to prevent callers from 409836a64e62STim Kordas * becoming implicitly blocked by a system-wide kmem reap -- which, 409936a64e62STim Kordas * on a system with many, many full magazines, can take minutes). 410036a64e62STim Kordas */ 410136a64e62STim Kordas if (kmem_cache_reap_active()) 410236a64e62STim Kordas return; 410336a64e62STim Kordas 4104fa9e4066Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 4105fa9e4066Sahrens if (zio_buf_cache[i] != prev_cache) { 4106fa9e4066Sahrens prev_cache = zio_buf_cache[i]; 410736a64e62STim Kordas kmem_cache_reap_soon(zio_buf_cache[i]); 4108fa9e4066Sahrens } 4109ad23a2dbSjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 4110ad23a2dbSjohansen prev_data_cache = zio_data_buf_cache[i]; 411136a64e62STim Kordas kmem_cache_reap_soon(zio_data_buf_cache[i]); 4112ad23a2dbSjohansen } 4113fa9e4066Sahrens } 411436a64e62STim Kordas kmem_cache_reap_soon(abd_chunk_cache); 411536a64e62STim Kordas kmem_cache_reap_soon(buf_cache); 411636a64e62STim Kordas kmem_cache_reap_soon(hdr_full_cache); 411736a64e62STim Kordas kmem_cache_reap_soon(hdr_l2only_cache); 411836a64e62STim Kordas kmem_cache_reap_soon(range_seg_cache); 411994dd93aeSGeorge Wilson 41202ec99e3eSMatthew Ahrens if (zio_arena != NULL) { 41212ec99e3eSMatthew Ahrens /* 41222ec99e3eSMatthew Ahrens * Ask the vmem arena to reclaim unused memory from its 41232ec99e3eSMatthew Ahrens * quantum caches. 41242ec99e3eSMatthew Ahrens */ 412594dd93aeSGeorge Wilson vmem_qcache_reap(zio_arena); 41262ec99e3eSMatthew Ahrens } 4127fa9e4066Sahrens } 4128fa9e4066Sahrens 4129244781f1SPrakash Surya /* 4130770499e1SDan Kimmel * Threads can block in arc_get_data_impl() waiting for this thread to evict 4131244781f1SPrakash Surya * enough data and signal them to proceed. When this happens, the threads in 4132770499e1SDan Kimmel * arc_get_data_impl() are sleeping while holding the hash lock for their 4133244781f1SPrakash Surya * particular arc header. Thus, we must be careful to never sleep on a 4134244781f1SPrakash Surya * hash lock in this thread. This is to prevent the following deadlock: 4135244781f1SPrakash Surya * 4136770499e1SDan Kimmel * - Thread A sleeps on CV in arc_get_data_impl() holding hash lock "L", 4137244781f1SPrakash Surya * waiting for the reclaim thread to signal it. 4138244781f1SPrakash Surya * 4139244781f1SPrakash Surya * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter, 4140244781f1SPrakash Surya * fails, and goes to sleep forever. 4141244781f1SPrakash Surya * 4142244781f1SPrakash Surya * This possible deadlock is avoided by always acquiring a hash lock 4143244781f1SPrakash Surya * using mutex_tryenter() from arc_reclaim_thread(). 4144244781f1SPrakash Surya */ 41453f7978d0SAlan Somers /* ARGSUSED */ 4146fa9e4066Sahrens static void 41473f7978d0SAlan Somers arc_reclaim_thread(void *unused) 4148fa9e4066Sahrens { 4149a8f6344fSEli Rosenthal hrtime_t growtime = 0; 415036a64e62STim Kordas hrtime_t kmem_reap_time = 0; 4151fa9e4066Sahrens callb_cpr_t cpr; 4152fa9e4066Sahrens 4153244781f1SPrakash Surya CALLB_CPR_INIT(&cpr, &arc_reclaim_lock, callb_generic_cpr, FTAG); 4154fa9e4066Sahrens 4155244781f1SPrakash Surya mutex_enter(&arc_reclaim_lock); 4156244781f1SPrakash Surya while (!arc_reclaim_thread_exit) { 4157244781f1SPrakash Surya uint64_t evicted = 0; 4158244781f1SPrakash Surya 4159dcbf3bd6SGeorge Wilson /* 4160dcbf3bd6SGeorge Wilson * This is necessary in order for the mdb ::arc dcmd to 4161dcbf3bd6SGeorge Wilson * show up to date information. Since the ::arc command 4162dcbf3bd6SGeorge Wilson * does not call the kstat's update function, without 4163dcbf3bd6SGeorge Wilson * this call, the command may show stale stats for the 4164dcbf3bd6SGeorge Wilson * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even 4165dcbf3bd6SGeorge Wilson * with this change, the data might be up to 1 second 4166dcbf3bd6SGeorge Wilson * out of date; but that should suffice. The arc_state_t 4167dcbf3bd6SGeorge Wilson * structures can be queried directly if more accurate 4168dcbf3bd6SGeorge Wilson * information is needed. 4169dcbf3bd6SGeorge Wilson */ 4170dcbf3bd6SGeorge Wilson if (arc_ksp != NULL) 4171dcbf3bd6SGeorge Wilson arc_ksp->ks_update(arc_ksp, KSTAT_READ); 4172dcbf3bd6SGeorge Wilson 4173244781f1SPrakash Surya mutex_exit(&arc_reclaim_lock); 4174244781f1SPrakash Surya 4175405a5a0fSMatthew Ahrens /* 4176405a5a0fSMatthew Ahrens * We call arc_adjust() before (possibly) calling 4177405a5a0fSMatthew Ahrens * arc_kmem_reap_now(), so that we can wake up 4178770499e1SDan Kimmel * arc_get_data_impl() sooner. 4179405a5a0fSMatthew Ahrens */ 4180405a5a0fSMatthew Ahrens evicted = arc_adjust(); 4181405a5a0fSMatthew Ahrens 4182405a5a0fSMatthew Ahrens int64_t free_memory = arc_available_memory(); 41832ec99e3eSMatthew Ahrens if (free_memory < 0) { 418436a64e62STim Kordas hrtime_t curtime = gethrtime(); 41852ec99e3eSMatthew Ahrens arc_no_grow = B_TRUE; 41862ec99e3eSMatthew Ahrens arc_warm = B_TRUE; 4187fa9e4066Sahrens 41882ec99e3eSMatthew Ahrens /* 41892ec99e3eSMatthew Ahrens * Wait at least zfs_grow_retry (default 60) seconds 41902ec99e3eSMatthew Ahrens * before considering growing. 41912ec99e3eSMatthew Ahrens */ 419236a64e62STim Kordas growtime = curtime + SEC2NSEC(arc_grow_retry); 4193fa9e4066Sahrens 419436a64e62STim Kordas /* 419536a64e62STim Kordas * Wait at least arc_kmem_cache_reap_retry_ms 419636a64e62STim Kordas * between arc_kmem_reap_now() calls. Without 419736a64e62STim Kordas * this check it is possible to end up in a 419836a64e62STim Kordas * situation where we spend lots of time 419936a64e62STim Kordas * reaping caches, while we're near arc_c_min. 420036a64e62STim Kordas */ 420136a64e62STim Kordas if (curtime >= kmem_reap_time) { 420236a64e62STim Kordas arc_kmem_reap_now(); 420336a64e62STim Kordas kmem_reap_time = gethrtime() + 420436a64e62STim Kordas MSEC2NSEC(arc_kmem_cache_reap_retry_ms); 420536a64e62STim Kordas } 4206fa9e4066Sahrens 42072ec99e3eSMatthew Ahrens /* 42082ec99e3eSMatthew Ahrens * If we are still low on memory, shrink the ARC 42092ec99e3eSMatthew Ahrens * so that we have arc_shrink_min free space. 42102ec99e3eSMatthew Ahrens */ 42112ec99e3eSMatthew Ahrens free_memory = arc_available_memory(); 42122ec99e3eSMatthew Ahrens 42132ec99e3eSMatthew Ahrens int64_t to_free = 42142ec99e3eSMatthew Ahrens (arc_c >> arc_shrink_shift) - free_memory; 42152ec99e3eSMatthew Ahrens if (to_free > 0) { 42162ec99e3eSMatthew Ahrens #ifdef _KERNEL 42172ec99e3eSMatthew Ahrens to_free = MAX(to_free, ptob(needfree)); 42182ec99e3eSMatthew Ahrens #endif 42192ec99e3eSMatthew Ahrens arc_shrink(to_free); 42202ec99e3eSMatthew Ahrens } 42212ec99e3eSMatthew Ahrens } else if (free_memory < arc_c >> arc_no_grow_shift) { 42222ec99e3eSMatthew Ahrens arc_no_grow = B_TRUE; 4223a8f6344fSEli Rosenthal } else if (gethrtime() >= growtime) { 42242ec99e3eSMatthew Ahrens arc_no_grow = B_FALSE; 4225fa9e4066Sahrens } 4226fa9e4066Sahrens 4227244781f1SPrakash Surya mutex_enter(&arc_reclaim_lock); 4228244781f1SPrakash Surya 4229244781f1SPrakash Surya /* 4230244781f1SPrakash Surya * If evicted is zero, we couldn't evict anything via 4231244781f1SPrakash Surya * arc_adjust(). This could be due to hash lock 4232244781f1SPrakash Surya * collisions, but more likely due to the majority of 4233244781f1SPrakash Surya * arc buffers being unevictable. Therefore, even if 4234244781f1SPrakash Surya * arc_size is above arc_c, another pass is unlikely to 4235244781f1SPrakash Surya * be helpful and could potentially cause us to enter an 4236244781f1SPrakash Surya * infinite loop. 4237244781f1SPrakash Surya */ 4238*3a2d8a1bSPaul Dagnelie if (aggsum_compare(&arc_size, arc_c) <= 0|| evicted == 0) { 4239244781f1SPrakash Surya /* 4240244781f1SPrakash Surya * We're either no longer overflowing, or we 4241244781f1SPrakash Surya * can't evict anything more, so we should wake 4242244781f1SPrakash Surya * up any threads before we go to sleep. 4243244781f1SPrakash Surya */ 4244244781f1SPrakash Surya cv_broadcast(&arc_reclaim_waiters_cv); 4245244781f1SPrakash Surya 4246244781f1SPrakash Surya /* 4247244781f1SPrakash Surya * Block until signaled, or after one second (we 4248244781f1SPrakash Surya * might need to perform arc_kmem_reap_now() 4249244781f1SPrakash Surya * even if we aren't being signalled) 4250244781f1SPrakash Surya */ 4251244781f1SPrakash Surya CALLB_CPR_SAFE_BEGIN(&cpr); 4252a8f6344fSEli Rosenthal (void) cv_timedwait_hires(&arc_reclaim_thread_cv, 4253a8f6344fSEli Rosenthal &arc_reclaim_lock, SEC2NSEC(1), MSEC2NSEC(1), 0); 4254244781f1SPrakash Surya CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_lock); 4255244781f1SPrakash Surya } 4256244781f1SPrakash Surya } 4257244781f1SPrakash Surya 4258dcbf3bd6SGeorge Wilson arc_reclaim_thread_exit = B_FALSE; 4259244781f1SPrakash Surya cv_broadcast(&arc_reclaim_thread_cv); 4260244781f1SPrakash Surya CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_lock */ 4261244781f1SPrakash Surya thread_exit(); 4262244781f1SPrakash Surya } 4263244781f1SPrakash Surya 4264ea8dc4b6Seschrock /* 4265ea8dc4b6Seschrock * Adapt arc info given the number of bytes we are trying to add and 4266ea8dc4b6Seschrock * the state that we are comming from. This function is only called 4267ea8dc4b6Seschrock * when we are adding new content to the cache. 4268ea8dc4b6Seschrock */ 4269fa9e4066Sahrens static void 4270ea8dc4b6Seschrock arc_adapt(int bytes, arc_state_t *state) 4271fa9e4066Sahrens { 4272ea8dc4b6Seschrock int mult; 42735a98e54bSBrendan Gregg - Sun Microsystems uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 42742fd872a7SPrakash Surya int64_t mrug_size = refcount_count(&arc_mru_ghost->arcs_size); 42752fd872a7SPrakash Surya int64_t mfug_size = refcount_count(&arc_mfu_ghost->arcs_size); 4276ea8dc4b6Seschrock 4277fa94a07fSbrendan if (state == arc_l2c_only) 4278fa94a07fSbrendan return; 4279fa94a07fSbrendan 4280ea8dc4b6Seschrock ASSERT(bytes > 0); 4281fa9e4066Sahrens /* 4282ea8dc4b6Seschrock * Adapt the target size of the MRU list: 4283ea8dc4b6Seschrock * - if we just hit in the MRU ghost list, then increase 4284ea8dc4b6Seschrock * the target size of the MRU list. 4285ea8dc4b6Seschrock * - if we just hit in the MFU ghost list, then increase 4286ea8dc4b6Seschrock * the target size of the MFU list by decreasing the 4287ea8dc4b6Seschrock * target size of the MRU list. 4288fa9e4066Sahrens */ 428944cb6abcSbmc if (state == arc_mru_ghost) { 42902fd872a7SPrakash Surya mult = (mrug_size >= mfug_size) ? 1 : (mfug_size / mrug_size); 42913e4e8481STom Erickson mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 4292ea8dc4b6Seschrock 42935a98e54bSBrendan Gregg - Sun Microsystems arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 429444cb6abcSbmc } else if (state == arc_mfu_ghost) { 42955a98e54bSBrendan Gregg - Sun Microsystems uint64_t delta; 42965a98e54bSBrendan Gregg - Sun Microsystems 42972fd872a7SPrakash Surya mult = (mfug_size >= mrug_size) ? 1 : (mrug_size / mfug_size); 42983e4e8481STom Erickson mult = MIN(mult, 10); 4299ea8dc4b6Seschrock 43005a98e54bSBrendan Gregg - Sun Microsystems delta = MIN(bytes * mult, arc_p); 43015a98e54bSBrendan Gregg - Sun Microsystems arc_p = MAX(arc_p_min, arc_p - delta); 4302ea8dc4b6Seschrock } 430344cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 4304fa9e4066Sahrens 4305fa9e4066Sahrens if (arc_reclaim_needed()) { 4306244781f1SPrakash Surya cv_signal(&arc_reclaim_thread_cv); 4307fa9e4066Sahrens return; 4308fa9e4066Sahrens } 4309fa9e4066Sahrens 431044cb6abcSbmc if (arc_no_grow) 4311fa9e4066Sahrens return; 4312fa9e4066Sahrens 431344cb6abcSbmc if (arc_c >= arc_c_max) 4314ea8dc4b6Seschrock return; 4315ea8dc4b6Seschrock 4316fa9e4066Sahrens /* 4317ea8dc4b6Seschrock * If we're within (2 * maxblocksize) bytes of the target 4318ea8dc4b6Seschrock * cache size, increment the target cache size 4319fa9e4066Sahrens */ 4320*3a2d8a1bSPaul Dagnelie if (aggsum_compare(&arc_size, arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) > 4321*3a2d8a1bSPaul Dagnelie 0) { 432244cb6abcSbmc atomic_add_64(&arc_c, (int64_t)bytes); 432344cb6abcSbmc if (arc_c > arc_c_max) 432444cb6abcSbmc arc_c = arc_c_max; 432544cb6abcSbmc else if (state == arc_anon) 432644cb6abcSbmc atomic_add_64(&arc_p, (int64_t)bytes); 432744cb6abcSbmc if (arc_p > arc_c) 432844cb6abcSbmc arc_p = arc_c; 4329fa9e4066Sahrens } 433044cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 4331fa9e4066Sahrens } 4332fa9e4066Sahrens 4333fa9e4066Sahrens /* 4334244781f1SPrakash Surya * Check if arc_size has grown past our upper threshold, determined by 4335244781f1SPrakash Surya * zfs_arc_overflow_shift. 4336fa9e4066Sahrens */ 4337244781f1SPrakash Surya static boolean_t 4338244781f1SPrakash Surya arc_is_overflowing(void) 4339fa9e4066Sahrens { 4340244781f1SPrakash Surya /* Always allow at least one block of overflow */ 4341244781f1SPrakash Surya uint64_t overflow = MAX(SPA_MAXBLOCKSIZE, 4342244781f1SPrakash Surya arc_c >> zfs_arc_overflow_shift); 4343fa9e4066Sahrens 4344*3a2d8a1bSPaul Dagnelie /* 4345*3a2d8a1bSPaul Dagnelie * We just compare the lower bound here for performance reasons. Our 4346*3a2d8a1bSPaul Dagnelie * primary goals are to make sure that the arc never grows without 4347*3a2d8a1bSPaul Dagnelie * bound, and that it can reach its maximum size. This check 4348*3a2d8a1bSPaul Dagnelie * accomplishes both goals. The maximum amount we could run over by is 4349*3a2d8a1bSPaul Dagnelie * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block 4350*3a2d8a1bSPaul Dagnelie * in the ARC. In practice, that's in the tens of MB, which is low 4351*3a2d8a1bSPaul Dagnelie * enough to be safe. 4352*3a2d8a1bSPaul Dagnelie */ 4353*3a2d8a1bSPaul Dagnelie return (aggsum_lower_bound(&arc_size) >= arc_c + overflow); 4354fa9e4066Sahrens } 4355fa9e4066Sahrens 4356770499e1SDan Kimmel static abd_t * 4357770499e1SDan Kimmel arc_get_data_abd(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4358770499e1SDan Kimmel { 4359770499e1SDan Kimmel arc_buf_contents_t type = arc_buf_type(hdr); 4360770499e1SDan Kimmel 4361770499e1SDan Kimmel arc_get_data_impl(hdr, size, tag); 4362770499e1SDan Kimmel if (type == ARC_BUFC_METADATA) { 4363770499e1SDan Kimmel return (abd_alloc(size, B_TRUE)); 4364770499e1SDan Kimmel } else { 4365770499e1SDan Kimmel ASSERT(type == ARC_BUFC_DATA); 4366770499e1SDan Kimmel return (abd_alloc(size, B_FALSE)); 4367770499e1SDan Kimmel } 4368770499e1SDan Kimmel } 4369770499e1SDan Kimmel 4370770499e1SDan Kimmel static void * 4371770499e1SDan Kimmel arc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4372770499e1SDan Kimmel { 4373770499e1SDan Kimmel arc_buf_contents_t type = arc_buf_type(hdr); 4374770499e1SDan Kimmel 4375770499e1SDan Kimmel arc_get_data_impl(hdr, size, tag); 4376770499e1SDan Kimmel if (type == ARC_BUFC_METADATA) { 4377770499e1SDan Kimmel return (zio_buf_alloc(size)); 4378770499e1SDan Kimmel } else { 4379770499e1SDan Kimmel ASSERT(type == ARC_BUFC_DATA); 4380770499e1SDan Kimmel return (zio_data_buf_alloc(size)); 4381770499e1SDan Kimmel } 4382770499e1SDan Kimmel } 4383770499e1SDan Kimmel 4384fa9e4066Sahrens /* 4385dcbf3bd6SGeorge Wilson * Allocate a block and return it to the caller. If we are hitting the 4386dcbf3bd6SGeorge Wilson * hard limit for the cache size, we must sleep, waiting for the eviction 4387dcbf3bd6SGeorge Wilson * thread to catch up. If we're past the target size but below the hard 4388dcbf3bd6SGeorge Wilson * limit, we'll only signal the reclaim thread and continue on. 4389fa9e4066Sahrens */ 4390770499e1SDan Kimmel static void 4391770499e1SDan Kimmel arc_get_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4392fa9e4066Sahrens { 4393770499e1SDan Kimmel arc_state_t *state = hdr->b_l1hdr.b_state; 4394770499e1SDan Kimmel arc_buf_contents_t type = arc_buf_type(hdr); 4395fa9e4066Sahrens 439644eda4d7Smaybee arc_adapt(size, state); 4397fa9e4066Sahrens 439844eda4d7Smaybee /* 4399244781f1SPrakash Surya * If arc_size is currently overflowing, and has grown past our 4400244781f1SPrakash Surya * upper limit, we must be adding data faster than the evict 4401244781f1SPrakash Surya * thread can evict. Thus, to ensure we don't compound the 4402244781f1SPrakash Surya * problem by adding more data and forcing arc_size to grow even 4403244781f1SPrakash Surya * further past it's target size, we halt and wait for the 4404244781f1SPrakash Surya * eviction thread to catch up. 4405244781f1SPrakash Surya * 4406244781f1SPrakash Surya * It's also possible that the reclaim thread is unable to evict 4407244781f1SPrakash Surya * enough buffers to get arc_size below the overflow limit (e.g. 4408244781f1SPrakash Surya * due to buffers being un-evictable, or hash lock collisions). 4409244781f1SPrakash Surya * In this case, we want to proceed regardless if we're 4410244781f1SPrakash Surya * overflowing; thus we don't use a while loop here. 441144eda4d7Smaybee */ 4412244781f1SPrakash Surya if (arc_is_overflowing()) { 4413244781f1SPrakash Surya mutex_enter(&arc_reclaim_lock); 4414244781f1SPrakash Surya 4415244781f1SPrakash Surya /* 4416244781f1SPrakash Surya * Now that we've acquired the lock, we may no longer be 4417244781f1SPrakash Surya * over the overflow limit, lets check. 4418244781f1SPrakash Surya * 4419244781f1SPrakash Surya * We're ignoring the case of spurious wake ups. If that 4420244781f1SPrakash Surya * were to happen, it'd let this thread consume an ARC 4421244781f1SPrakash Surya * buffer before it should have (i.e. before we're under 4422244781f1SPrakash Surya * the overflow limit and were signalled by the reclaim 4423244781f1SPrakash Surya * thread). As long as that is a rare occurrence, it 4424244781f1SPrakash Surya * shouldn't cause any harm. 4425244781f1SPrakash Surya */ 4426244781f1SPrakash Surya if (arc_is_overflowing()) { 4427244781f1SPrakash Surya cv_signal(&arc_reclaim_thread_cv); 4428244781f1SPrakash Surya cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); 4429ad23a2dbSjohansen } 4430244781f1SPrakash Surya 4431244781f1SPrakash Surya mutex_exit(&arc_reclaim_lock); 443244eda4d7Smaybee } 443344eda4d7Smaybee 4434dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 4435244781f1SPrakash Surya if (type == ARC_BUFC_METADATA) { 4436244781f1SPrakash Surya arc_space_consume(size, ARC_SPACE_META); 4437fa9e4066Sahrens } else { 4438244781f1SPrakash Surya arc_space_consume(size, ARC_SPACE_DATA); 443944eda4d7Smaybee } 4440244781f1SPrakash Surya 444144eda4d7Smaybee /* 444244eda4d7Smaybee * Update the state size. Note that ghost states have a 444344eda4d7Smaybee * "ghost size" and so don't need to be updated. 444444eda4d7Smaybee */ 4445dcbf3bd6SGeorge Wilson if (!GHOST_STATE(state)) { 444644eda4d7Smaybee 4447dcbf3bd6SGeorge Wilson (void) refcount_add_many(&state->arcs_size, size, tag); 4448244781f1SPrakash Surya 4449244781f1SPrakash Surya /* 4450244781f1SPrakash Surya * If this is reached via arc_read, the link is 4451244781f1SPrakash Surya * protected by the hash lock. If reached via 4452244781f1SPrakash Surya * arc_buf_alloc, the header should not be accessed by 4453244781f1SPrakash Surya * any other thread. And, if reached via arc_read_done, 4454244781f1SPrakash Surya * the hash lock will protect it if it's found in the 4455244781f1SPrakash Surya * hash table; otherwise no other thread should be 4456244781f1SPrakash Surya * trying to [add|remove]_reference it. 4457244781f1SPrakash Surya */ 4458244781f1SPrakash Surya if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 445989c86e32SChris Williamson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4460dcbf3bd6SGeorge Wilson (void) refcount_add_many(&state->arcs_esize[type], 4461dcbf3bd6SGeorge Wilson size, tag); 4462fa9e4066Sahrens } 4463dcbf3bd6SGeorge Wilson 4464641fbdaeSmaybee /* 4465641fbdaeSmaybee * If we are growing the cache, and we are adding anonymous 446644cb6abcSbmc * data, and we have outgrown arc_p, update arc_p 4467641fbdaeSmaybee */ 4468*3a2d8a1bSPaul Dagnelie if (aggsum_compare(&arc_size, arc_c) < 0 && 4469*3a2d8a1bSPaul Dagnelie hdr->b_l1hdr.b_state == arc_anon && 44702fd872a7SPrakash Surya (refcount_count(&arc_anon->arcs_size) + 44712fd872a7SPrakash Surya refcount_count(&arc_mru->arcs_size) > arc_p)) 447244cb6abcSbmc arc_p = MIN(arc_c, arc_p + size); 4473fa9e4066Sahrens } 4474770499e1SDan Kimmel } 4475770499e1SDan Kimmel 4476770499e1SDan Kimmel static void 4477770499e1SDan Kimmel arc_free_data_abd(arc_buf_hdr_t *hdr, abd_t *abd, uint64_t size, void *tag) 4478770499e1SDan Kimmel { 4479770499e1SDan Kimmel arc_free_data_impl(hdr, size, tag); 4480770499e1SDan Kimmel abd_free(abd); 4481770499e1SDan Kimmel } 4482770499e1SDan Kimmel 4483770499e1SDan Kimmel static void 4484770499e1SDan Kimmel arc_free_data_buf(arc_buf_hdr_t *hdr, void *buf, uint64_t size, void *tag) 4485770499e1SDan Kimmel { 4486770499e1SDan Kimmel arc_buf_contents_t type = arc_buf_type(hdr); 4487770499e1SDan Kimmel 4488770499e1SDan Kimmel arc_free_data_impl(hdr, size, tag); 4489770499e1SDan Kimmel if (type == ARC_BUFC_METADATA) { 4490770499e1SDan Kimmel zio_buf_free(buf, size); 4491770499e1SDan Kimmel } else { 4492770499e1SDan Kimmel ASSERT(type == ARC_BUFC_DATA); 4493770499e1SDan Kimmel zio_data_buf_free(buf, size); 4494770499e1SDan Kimmel } 4495dcbf3bd6SGeorge Wilson } 4496dcbf3bd6SGeorge Wilson 4497dcbf3bd6SGeorge Wilson /* 4498dcbf3bd6SGeorge Wilson * Free the arc data buffer. 4499dcbf3bd6SGeorge Wilson */ 4500dcbf3bd6SGeorge Wilson static void 4501770499e1SDan Kimmel arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4502dcbf3bd6SGeorge Wilson { 4503dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 4504dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 4505dcbf3bd6SGeorge Wilson 4506dcbf3bd6SGeorge Wilson /* protected by hash lock, if in the hash table */ 4507dcbf3bd6SGeorge Wilson if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 4508dcbf3bd6SGeorge Wilson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4509dcbf3bd6SGeorge Wilson ASSERT(state != arc_anon && state != arc_l2c_only); 4510dcbf3bd6SGeorge Wilson 4511dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 4512dcbf3bd6SGeorge Wilson size, tag); 4513dcbf3bd6SGeorge Wilson } 4514dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_size, size, tag); 4515dcbf3bd6SGeorge Wilson 4516dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 4517dcbf3bd6SGeorge Wilson if (type == ARC_BUFC_METADATA) { 4518dcbf3bd6SGeorge Wilson arc_space_return(size, ARC_SPACE_META); 4519dcbf3bd6SGeorge Wilson } else { 4520dcbf3bd6SGeorge Wilson ASSERT(type == ARC_BUFC_DATA); 4521dcbf3bd6SGeorge Wilson arc_space_return(size, ARC_SPACE_DATA); 4522dcbf3bd6SGeorge Wilson } 4523fa9e4066Sahrens } 4524fa9e4066Sahrens 4525fa9e4066Sahrens /* 4526fa9e4066Sahrens * This routine is called whenever a buffer is accessed. 4527ea8dc4b6Seschrock * NOTE: the hash lock is dropped in this function. 4528fa9e4066Sahrens */ 4529fa9e4066Sahrens static void 45307adb730bSGeorge Wilson arc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 4531fa9e4066Sahrens { 4532d3d50737SRafael Vanoni clock_t now; 4533d3d50737SRafael Vanoni 4534fa9e4066Sahrens ASSERT(MUTEX_HELD(hash_lock)); 453589c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 4536fa9e4066Sahrens 453789c86e32SChris Williamson if (hdr->b_l1hdr.b_state == arc_anon) { 4538fa9e4066Sahrens /* 4539fa9e4066Sahrens * This buffer is not in the cache, and does not 4540fa9e4066Sahrens * appear in our "ghost" list. Add the new buffer 4541fa9e4066Sahrens * to the MRU state. 4542fa9e4066Sahrens */ 4543fa9e4066Sahrens 454489c86e32SChris Williamson ASSERT0(hdr->b_l1hdr.b_arc_access); 454589c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 45467adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 45477adb730bSGeorge Wilson arc_change_state(arc_mru, hdr, hash_lock); 4548fa9e4066Sahrens 454989c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mru) { 4550d3d50737SRafael Vanoni now = ddi_get_lbolt(); 4551d3d50737SRafael Vanoni 4552fa9e4066Sahrens /* 455313506d1eSmaybee * If this buffer is here because of a prefetch, then either: 455413506d1eSmaybee * - clear the flag if this is a "referencing" read 455513506d1eSmaybee * (any subsequent access will bump this into the MFU state). 455613506d1eSmaybee * or 455713506d1eSmaybee * - move the buffer to the head of the list if this is 455813506d1eSmaybee * another prefetch (to make it less likely to be evicted). 4559fa9e4066Sahrens */ 456089c86e32SChris Williamson if (HDR_PREFETCH(hdr)) { 456189c86e32SChris Williamson if (refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 4562244781f1SPrakash Surya /* link protected by hash lock */ 4563244781f1SPrakash Surya ASSERT(multilist_link_active( 456489c86e32SChris Williamson &hdr->b_l1hdr.b_arc_node)); 456513506d1eSmaybee } else { 4566dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); 456744cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_hits); 456813506d1eSmaybee } 456989c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = now; 4570fa9e4066Sahrens return; 4571fa9e4066Sahrens } 4572fa9e4066Sahrens 4573fa9e4066Sahrens /* 4574fa9e4066Sahrens * This buffer has been "accessed" only once so far, 4575fa9e4066Sahrens * but it is still in the cache. Move it to the MFU 4576fa9e4066Sahrens * state. 4577fa9e4066Sahrens */ 457889c86e32SChris Williamson if (now > hdr->b_l1hdr.b_arc_access + ARC_MINTIME) { 4579fa9e4066Sahrens /* 4580fa9e4066Sahrens * More than 125ms have passed since we 4581fa9e4066Sahrens * instantiated this buffer. Move it to the 4582fa9e4066Sahrens * most frequently used state. 4583fa9e4066Sahrens */ 458489c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = now; 45857adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 45867adb730bSGeorge Wilson arc_change_state(arc_mfu, hdr, hash_lock); 4587fa9e4066Sahrens } 458844cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_hits); 458989c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { 4590fa9e4066Sahrens arc_state_t *new_state; 4591fa9e4066Sahrens /* 4592fa9e4066Sahrens * This buffer has been "accessed" recently, but 4593fa9e4066Sahrens * was evicted from the cache. Move it to the 4594fa9e4066Sahrens * MFU state. 4595fa9e4066Sahrens */ 4596fa9e4066Sahrens 459789c86e32SChris Williamson if (HDR_PREFETCH(hdr)) { 459844cb6abcSbmc new_state = arc_mru; 459989c86e32SChris Williamson if (refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) 4600dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); 46017adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 4602fa9e4066Sahrens } else { 460344cb6abcSbmc new_state = arc_mfu; 46047adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4605fa9e4066Sahrens } 4606fa9e4066Sahrens 460789c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 46087adb730bSGeorge Wilson arc_change_state(new_state, hdr, hash_lock); 4609fa9e4066Sahrens 461044cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 461189c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mfu) { 4612fa9e4066Sahrens /* 4613fa9e4066Sahrens * This buffer has been accessed more than once and is 4614fa9e4066Sahrens * still in the cache. Keep it in the MFU state. 4615fa9e4066Sahrens * 461613506d1eSmaybee * NOTE: an add_reference() that occurred when we did 461713506d1eSmaybee * the arc_read() will have kicked this off the list. 461813506d1eSmaybee * If it was a prefetch, we will explicitly move it to 461913506d1eSmaybee * the head of the list now. 4620fa9e4066Sahrens */ 462189c86e32SChris Williamson if ((HDR_PREFETCH(hdr)) != 0) { 462289c86e32SChris Williamson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4623244781f1SPrakash Surya /* link protected by hash_lock */ 4624244781f1SPrakash Surya ASSERT(multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 462513506d1eSmaybee } 462644cb6abcSbmc ARCSTAT_BUMP(arcstat_mfu_hits); 462789c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 462889c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { 462944cb6abcSbmc arc_state_t *new_state = arc_mfu; 4630fa9e4066Sahrens /* 4631fa9e4066Sahrens * This buffer has been accessed more than once but has 4632fa9e4066Sahrens * been evicted from the cache. Move it back to the 4633fa9e4066Sahrens * MFU state. 4634fa9e4066Sahrens */ 4635fa9e4066Sahrens 463689c86e32SChris Williamson if (HDR_PREFETCH(hdr)) { 463713506d1eSmaybee /* 463813506d1eSmaybee * This is a prefetch access... 463913506d1eSmaybee * move this block back to the MRU state. 464013506d1eSmaybee */ 464189c86e32SChris Williamson ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 464244cb6abcSbmc new_state = arc_mru; 464313506d1eSmaybee } 464413506d1eSmaybee 464589c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 46467adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 46477adb730bSGeorge Wilson arc_change_state(new_state, hdr, hash_lock); 4648fa9e4066Sahrens 464944cb6abcSbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 465089c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { 4651fa94a07fSbrendan /* 4652fa94a07fSbrendan * This buffer is on the 2nd Level ARC. 4653fa94a07fSbrendan */ 4654fa94a07fSbrendan 465589c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 46567adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 46577adb730bSGeorge Wilson arc_change_state(arc_mfu, hdr, hash_lock); 4658fa9e4066Sahrens } else { 4659fa9e4066Sahrens ASSERT(!"invalid arc state"); 4660fa9e4066Sahrens } 4661fa9e4066Sahrens } 4662fa9e4066Sahrens 4663fa9e4066Sahrens /* a generic arc_done_func_t which you can use */ 4664fa9e4066Sahrens /* ARGSUSED */ 4665fa9e4066Sahrens void 4666fa9e4066Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 4667fa9e4066Sahrens { 46683f9d6ad7SLin Ling if (zio == NULL || zio->io_error == 0) 46695602294fSDan Kimmel bcopy(buf->b_data, arg, arc_buf_size(buf)); 4670dcbf3bd6SGeorge Wilson arc_buf_destroy(buf, arg); 4671fa9e4066Sahrens } 4672fa9e4066Sahrens 46730e8c6158Smaybee /* a generic arc_done_func_t */ 4674fa9e4066Sahrens void 4675fa9e4066Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 4676fa9e4066Sahrens { 4677fa9e4066Sahrens arc_buf_t **bufp = arg; 4678fa9e4066Sahrens if (zio && zio->io_error) { 4679dcbf3bd6SGeorge Wilson arc_buf_destroy(buf, arg); 4680fa9e4066Sahrens *bufp = NULL; 4681fa9e4066Sahrens } else { 4682fa9e4066Sahrens *bufp = buf; 46833f9d6ad7SLin Ling ASSERT(buf->b_data); 4684fa9e4066Sahrens } 4685fa9e4066Sahrens } 4686fa9e4066Sahrens 4687dcbf3bd6SGeorge Wilson static void 4688dcbf3bd6SGeorge Wilson arc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp) 4689dcbf3bd6SGeorge Wilson { 4690dcbf3bd6SGeorge Wilson if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { 4691dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0); 4692dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 4693dcbf3bd6SGeorge Wilson } else { 4694dcbf3bd6SGeorge Wilson if (HDR_COMPRESSION_ENABLED(hdr)) { 4695dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, 4696dcbf3bd6SGeorge Wilson BP_GET_COMPRESS(bp)); 4697dcbf3bd6SGeorge Wilson } 4698dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); 4699dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp)); 4700dcbf3bd6SGeorge Wilson } 4701dcbf3bd6SGeorge Wilson } 4702dcbf3bd6SGeorge Wilson 4703fa9e4066Sahrens static void 4704fa9e4066Sahrens arc_read_done(zio_t *zio) 4705fa9e4066Sahrens { 4706dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = zio->io_private; 47075d7b4d43SMatthew Ahrens kmutex_t *hash_lock = NULL; 47085602294fSDan Kimmel arc_callback_t *callback_list; 47095602294fSDan Kimmel arc_callback_t *acb; 47105602294fSDan Kimmel boolean_t freeable = B_FALSE; 47115602294fSDan Kimmel boolean_t no_zio_error = (zio->io_error == 0); 4712fa9e4066Sahrens 4713bbf4a8dfSmaybee /* 4714bbf4a8dfSmaybee * The hdr was inserted into hash-table and removed from lists 4715bbf4a8dfSmaybee * prior to starting I/O. We should find this header, since 4716bbf4a8dfSmaybee * it's in the hash table, and it should be legit since it's 4717bbf4a8dfSmaybee * not possible to evict it during the I/O. The only possible 4718bbf4a8dfSmaybee * reason for it not to be found is if we were freed during the 4719bbf4a8dfSmaybee * read. 4720bbf4a8dfSmaybee */ 47215d7b4d43SMatthew Ahrens if (HDR_IN_HASH_TABLE(hdr)) { 47225d7b4d43SMatthew Ahrens ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 47235d7b4d43SMatthew Ahrens ASSERT3U(hdr->b_dva.dva_word[0], ==, 47245d7b4d43SMatthew Ahrens BP_IDENTITY(zio->io_bp)->dva_word[0]); 47255d7b4d43SMatthew Ahrens ASSERT3U(hdr->b_dva.dva_word[1], ==, 47265d7b4d43SMatthew Ahrens BP_IDENTITY(zio->io_bp)->dva_word[1]); 47275d7b4d43SMatthew Ahrens 47285d7b4d43SMatthew Ahrens arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 47295d7b4d43SMatthew Ahrens &hash_lock); 47305d7b4d43SMatthew Ahrens 4731dcbf3bd6SGeorge Wilson ASSERT((found == hdr && 47325d7b4d43SMatthew Ahrens DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 47335d7b4d43SMatthew Ahrens (found == hdr && HDR_L2_READING(hdr))); 4734dcbf3bd6SGeorge Wilson ASSERT3P(hash_lock, !=, NULL); 4735dcbf3bd6SGeorge Wilson } 4736dcbf3bd6SGeorge Wilson 47375602294fSDan Kimmel if (no_zio_error) { 4738dcbf3bd6SGeorge Wilson /* byteswap if necessary */ 4739dcbf3bd6SGeorge Wilson if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 4740dcbf3bd6SGeorge Wilson if (BP_GET_LEVEL(zio->io_bp) > 0) { 4741dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; 4742dcbf3bd6SGeorge Wilson } else { 4743dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = 4744dcbf3bd6SGeorge Wilson DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 4745dcbf3bd6SGeorge Wilson } 4746dcbf3bd6SGeorge Wilson } else { 4747dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 4748dcbf3bd6SGeorge Wilson } 47495d7b4d43SMatthew Ahrens } 4750fa94a07fSbrendan 4751dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED); 475289c86e32SChris Williamson if (l2arc_noprefetch && HDR_PREFETCH(hdr)) 4753dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE); 4754fa9e4066Sahrens 475589c86e32SChris Williamson callback_list = hdr->b_l1hdr.b_acb; 4756dcbf3bd6SGeorge Wilson ASSERT3P(callback_list, !=, NULL); 47576b4acc8bSahrens 47585602294fSDan Kimmel if (hash_lock && no_zio_error && hdr->b_l1hdr.b_state == arc_anon) { 4759b24ab676SJeff Bonwick /* 4760b24ab676SJeff Bonwick * Only call arc_access on anonymous buffers. This is because 4761b24ab676SJeff Bonwick * if we've issued an I/O for an evicted buffer, we've already 4762b24ab676SJeff Bonwick * called arc_access (to prevent any simultaneous readers from 4763b24ab676SJeff Bonwick * getting confused). 4764b24ab676SJeff Bonwick */ 4765b24ab676SJeff Bonwick arc_access(hdr, hash_lock); 4766b24ab676SJeff Bonwick } 4767b24ab676SJeff Bonwick 47685602294fSDan Kimmel /* 47695602294fSDan Kimmel * If a read request has a callback (i.e. acb_done is not NULL), then we 47705602294fSDan Kimmel * make a buf containing the data according to the parameters which were 47715602294fSDan Kimmel * passed in. The implementation of arc_buf_alloc_impl() ensures that we 47725602294fSDan Kimmel * aren't needlessly decompressing the data multiple times. 47735602294fSDan Kimmel */ 47745602294fSDan Kimmel int callback_cnt = 0; 47755602294fSDan Kimmel for (acb = callback_list; acb != NULL; acb = acb->acb_next) { 47765602294fSDan Kimmel if (!acb->acb_done) 47775602294fSDan Kimmel continue; 47785602294fSDan Kimmel 47795602294fSDan Kimmel /* This is a demand read since prefetches don't use callbacks */ 47805602294fSDan Kimmel callback_cnt++; 47815602294fSDan Kimmel 47825602294fSDan Kimmel int error = arc_buf_alloc_impl(hdr, acb->acb_private, 47835602294fSDan Kimmel acb->acb_compressed, no_zio_error, &acb->acb_buf); 47845602294fSDan Kimmel if (no_zio_error) { 47855602294fSDan Kimmel zio->io_error = error; 4786fa9e4066Sahrens } 4787fa9e4066Sahrens } 478889c86e32SChris Williamson hdr->b_l1hdr.b_acb = NULL; 4789dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 47905602294fSDan Kimmel if (callback_cnt == 0) { 4791dcbf3bd6SGeorge Wilson ASSERT(HDR_PREFETCH(hdr)); 4792dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 4793770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 4794b24ab676SJeff Bonwick } 4795fa9e4066Sahrens 479689c86e32SChris Williamson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || 479789c86e32SChris Williamson callback_list != NULL); 4798fa9e4066Sahrens 47995602294fSDan Kimmel if (no_zio_error) { 4800dcbf3bd6SGeorge Wilson arc_hdr_verify(hdr, zio->io_bp); 4801dcbf3bd6SGeorge Wilson } else { 4802dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); 480389c86e32SChris Williamson if (hdr->b_l1hdr.b_state != arc_anon) 480444cb6abcSbmc arc_change_state(arc_anon, hdr, hash_lock); 4805ea8dc4b6Seschrock if (HDR_IN_HASH_TABLE(hdr)) 4806ea8dc4b6Seschrock buf_hash_remove(hdr); 480789c86e32SChris Williamson freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4808fa9e4066Sahrens } 4809fa9e4066Sahrens 4810ea8dc4b6Seschrock /* 481113506d1eSmaybee * Broadcast before we drop the hash_lock to avoid the possibility 481213506d1eSmaybee * that the hdr (and hence the cv) might be freed before we get to 481313506d1eSmaybee * the cv_broadcast(). 4814ea8dc4b6Seschrock */ 481589c86e32SChris Williamson cv_broadcast(&hdr->b_l1hdr.b_cv); 4816ea8dc4b6Seschrock 481789c86e32SChris Williamson if (hash_lock != NULL) { 481844eda4d7Smaybee mutex_exit(hash_lock); 4819fa9e4066Sahrens } else { 4820fa9e4066Sahrens /* 4821fa9e4066Sahrens * This block was freed while we waited for the read to 4822fa9e4066Sahrens * complete. It has been removed from the hash table and 4823fa9e4066Sahrens * moved to the anonymous state (so that it won't show up 4824fa9e4066Sahrens * in the cache). 4825fa9e4066Sahrens */ 482689c86e32SChris Williamson ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 482789c86e32SChris Williamson freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4828fa9e4066Sahrens } 4829fa9e4066Sahrens 4830fa9e4066Sahrens /* execute each callback and free its structure */ 4831fa9e4066Sahrens while ((acb = callback_list) != NULL) { 4832fa9e4066Sahrens if (acb->acb_done) 4833fa9e4066Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 4834fa9e4066Sahrens 4835fa9e4066Sahrens if (acb->acb_zio_dummy != NULL) { 4836fa9e4066Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 4837fa9e4066Sahrens zio_nowait(acb->acb_zio_dummy); 4838fa9e4066Sahrens } 4839fa9e4066Sahrens 4840fa9e4066Sahrens callback_list = acb->acb_next; 4841fa9e4066Sahrens kmem_free(acb, sizeof (arc_callback_t)); 4842fa9e4066Sahrens } 4843fa9e4066Sahrens 4844fa9e4066Sahrens if (freeable) 4845ea8dc4b6Seschrock arc_hdr_destroy(hdr); 4846fa9e4066Sahrens } 4847fa9e4066Sahrens 4848fa9e4066Sahrens /* 4849fc98fea5SBart Coddens * "Read" the block at the specified DVA (in bp) via the 4850fa9e4066Sahrens * cache. If the block is found in the cache, invoke the provided 4851fa9e4066Sahrens * callback immediately and return. Note that the `zio' parameter 4852fa9e4066Sahrens * in the callback will be NULL in this case, since no IO was 4853fa9e4066Sahrens * required. If the block is not in the cache pass the read request 4854fa9e4066Sahrens * on to the spa with a substitute callback function, so that the 4855fa9e4066Sahrens * requested block will be added to the cache. 4856fa9e4066Sahrens * 4857fa9e4066Sahrens * If a read request arrives for a block that has a read in-progress, 4858fa9e4066Sahrens * either wait for the in-progress read to complete (and return the 4859fa9e4066Sahrens * results); or, if this is a read with a "done" func, add a record 4860fa9e4066Sahrens * to the read to invoke the "done" func when the read completes, 4861fa9e4066Sahrens * and return; or just return. 4862fa9e4066Sahrens * 4863fa9e4066Sahrens * arc_read_done() will invoke all the requested "done" functions 4864fa9e4066Sahrens * for readers of this block. 4865fa9e4066Sahrens */ 4866fa9e4066Sahrens int 48671b912ec7SGeorge Wilson arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 48687adb730bSGeorge Wilson void *private, zio_priority_t priority, int zio_flags, 48697adb730bSGeorge Wilson arc_flags_t *arc_flags, const zbookmark_phys_t *zb) 4870fa9e4066Sahrens { 48715d7b4d43SMatthew Ahrens arc_buf_hdr_t *hdr = NULL; 48725d7b4d43SMatthew Ahrens kmutex_t *hash_lock = NULL; 4873fa94a07fSbrendan zio_t *rzio; 4874e9103aaeSGarrett D'Amore uint64_t guid = spa_load_guid(spa); 48755602294fSDan Kimmel boolean_t compressed_read = (zio_flags & ZIO_FLAG_RAW) != 0; 4876fa9e4066Sahrens 48775d7b4d43SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp) || 48785d7b4d43SMatthew Ahrens BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 48795d7b4d43SMatthew Ahrens 4880fa9e4066Sahrens top: 48815d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) { 48825d7b4d43SMatthew Ahrens /* 48835d7b4d43SMatthew Ahrens * Embedded BP's have no DVA and require no I/O to "read". 48845d7b4d43SMatthew Ahrens * Create an anonymous arc buf to back it. 48855d7b4d43SMatthew Ahrens */ 48865d7b4d43SMatthew Ahrens hdr = buf_hash_find(guid, bp, &hash_lock); 48875d7b4d43SMatthew Ahrens } 48885d7b4d43SMatthew Ahrens 4889770499e1SDan Kimmel if (hdr != NULL && HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_pabd != NULL) { 4890dcbf3bd6SGeorge Wilson arc_buf_t *buf = NULL; 48917adb730bSGeorge Wilson *arc_flags |= ARC_FLAG_CACHED; 489213506d1eSmaybee 4893fa9e4066Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 489413506d1eSmaybee 4895cf6106c8SMatthew Ahrens if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) && 4896cf6106c8SMatthew Ahrens priority == ZIO_PRIORITY_SYNC_READ) { 4897cf6106c8SMatthew Ahrens /* 4898cf6106c8SMatthew Ahrens * This sync read must wait for an 4899cf6106c8SMatthew Ahrens * in-progress async read (e.g. a predictive 4900cf6106c8SMatthew Ahrens * prefetch). Async reads are queued 4901cf6106c8SMatthew Ahrens * separately at the vdev_queue layer, so 4902cf6106c8SMatthew Ahrens * this is a form of priority inversion. 4903cf6106c8SMatthew Ahrens * Ideally, we would "inherit" the demand 4904cf6106c8SMatthew Ahrens * i/o's priority by moving the i/o from 4905cf6106c8SMatthew Ahrens * the async queue to the synchronous queue, 4906cf6106c8SMatthew Ahrens * but there is currently no mechanism to do 4907cf6106c8SMatthew Ahrens * so. Track this so that we can evaluate 4908cf6106c8SMatthew Ahrens * the magnitude of this potential performance 4909cf6106c8SMatthew Ahrens * problem. 4910cf6106c8SMatthew Ahrens * 4911cf6106c8SMatthew Ahrens * Note that if the prefetch i/o is already 4912cf6106c8SMatthew Ahrens * active (has been issued to the device), 4913cf6106c8SMatthew Ahrens * the prefetch improved performance, because 4914cf6106c8SMatthew Ahrens * we issued it sooner than we would have 4915cf6106c8SMatthew Ahrens * without the prefetch. 4916cf6106c8SMatthew Ahrens */ 4917cf6106c8SMatthew Ahrens DTRACE_PROBE1(arc__sync__wait__for__async, 4918cf6106c8SMatthew Ahrens arc_buf_hdr_t *, hdr); 4919cf6106c8SMatthew Ahrens ARCSTAT_BUMP(arcstat_sync_wait_for_async); 4920cf6106c8SMatthew Ahrens } 4921cf6106c8SMatthew Ahrens if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { 4922dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, 4923dcbf3bd6SGeorge Wilson ARC_FLAG_PREDICTIVE_PREFETCH); 4924cf6106c8SMatthew Ahrens } 4925cf6106c8SMatthew Ahrens 49267adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_WAIT) { 492789c86e32SChris Williamson cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); 492813506d1eSmaybee mutex_exit(hash_lock); 492913506d1eSmaybee goto top; 493013506d1eSmaybee } 49317adb730bSGeorge Wilson ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 493213506d1eSmaybee 493313506d1eSmaybee if (done) { 4934cf6106c8SMatthew Ahrens arc_callback_t *acb = NULL; 4935fa9e4066Sahrens 4936fa9e4066Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 4937fa9e4066Sahrens KM_SLEEP); 4938fa9e4066Sahrens acb->acb_done = done; 4939fa9e4066Sahrens acb->acb_private = private; 49405602294fSDan Kimmel acb->acb_compressed = compressed_read; 4941fa9e4066Sahrens if (pio != NULL) 4942fa9e4066Sahrens acb->acb_zio_dummy = zio_null(pio, 4943a3f829aeSBill Moore spa, NULL, NULL, NULL, zio_flags); 4944fa9e4066Sahrens 4945dcbf3bd6SGeorge Wilson ASSERT3P(acb->acb_done, !=, NULL); 494689c86e32SChris Williamson acb->acb_next = hdr->b_l1hdr.b_acb; 494789c86e32SChris Williamson hdr->b_l1hdr.b_acb = acb; 4948fa9e4066Sahrens mutex_exit(hash_lock); 4949fa9e4066Sahrens return (0); 4950fa9e4066Sahrens } 4951fa9e4066Sahrens mutex_exit(hash_lock); 4952fa9e4066Sahrens return (0); 4953fa9e4066Sahrens } 4954fa9e4066Sahrens 495589c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_state == arc_mru || 495689c86e32SChris Williamson hdr->b_l1hdr.b_state == arc_mfu); 4957fa9e4066Sahrens 4958ea8dc4b6Seschrock if (done) { 4959cf6106c8SMatthew Ahrens if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { 4960cf6106c8SMatthew Ahrens /* 4961cf6106c8SMatthew Ahrens * This is a demand read which does not have to 4962cf6106c8SMatthew Ahrens * wait for i/o because we did a predictive 4963cf6106c8SMatthew Ahrens * prefetch i/o for it, which has completed. 4964cf6106c8SMatthew Ahrens */ 4965cf6106c8SMatthew Ahrens DTRACE_PROBE1( 4966cf6106c8SMatthew Ahrens arc__demand__hit__predictive__prefetch, 4967cf6106c8SMatthew Ahrens arc_buf_hdr_t *, hdr); 4968cf6106c8SMatthew Ahrens ARCSTAT_BUMP( 4969cf6106c8SMatthew Ahrens arcstat_demand_hit_predictive_prefetch); 4970dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, 4971dcbf3bd6SGeorge Wilson ARC_FLAG_PREDICTIVE_PREFETCH); 4972cf6106c8SMatthew Ahrens } 4973dcbf3bd6SGeorge Wilson ASSERT(!BP_IS_EMBEDDED(bp) || !BP_IS_HOLE(bp)); 4974dcbf3bd6SGeorge Wilson 49755602294fSDan Kimmel /* Get a buf with the desired data in it. */ 49765602294fSDan Kimmel VERIFY0(arc_buf_alloc_impl(hdr, private, 49775602294fSDan Kimmel compressed_read, B_TRUE, &buf)); 49787adb730bSGeorge Wilson } else if (*arc_flags & ARC_FLAG_PREFETCH && 497989c86e32SChris Williamson refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 4980dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); 4981fa9e4066Sahrens } 4982fa9e4066Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 498344eda4d7Smaybee arc_access(hdr, hash_lock); 49847adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_L2CACHE) 4985dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 498644eda4d7Smaybee mutex_exit(hash_lock); 498744cb6abcSbmc ARCSTAT_BUMP(arcstat_hits); 498889c86e32SChris Williamson ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 498989c86e32SChris Williamson demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 499044cb6abcSbmc data, metadata, hits); 499144cb6abcSbmc 4992fa9e4066Sahrens if (done) 4993fa9e4066Sahrens done(NULL, buf, private); 4994fa9e4066Sahrens } else { 4995dcbf3bd6SGeorge Wilson uint64_t lsize = BP_GET_LSIZE(bp); 4996dcbf3bd6SGeorge Wilson uint64_t psize = BP_GET_PSIZE(bp); 49975d7b4d43SMatthew Ahrens arc_callback_t *acb; 49983a737e0dSbrendan vdev_t *vd = NULL; 4999d5285caeSGeorge Wilson uint64_t addr = 0; 50005a98e54bSBrendan Gregg - Sun Microsystems boolean_t devw = B_FALSE; 5001dcbf3bd6SGeorge Wilson uint64_t size; 5002fa9e4066Sahrens 5003fa9e4066Sahrens if (hdr == NULL) { 5004fa9e4066Sahrens /* this block is not in the cache */ 50055d7b4d43SMatthew Ahrens arc_buf_hdr_t *exists = NULL; 5006ad23a2dbSjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 5007dcbf3bd6SGeorge Wilson hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, 5008dcbf3bd6SGeorge Wilson BP_GET_COMPRESS(bp), type); 5009dcbf3bd6SGeorge Wilson 50105d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) { 50115d7b4d43SMatthew Ahrens hdr->b_dva = *BP_IDENTITY(bp); 50125d7b4d43SMatthew Ahrens hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 50135d7b4d43SMatthew Ahrens exists = buf_hash_insert(hdr, &hash_lock); 50145d7b4d43SMatthew Ahrens } 50155d7b4d43SMatthew Ahrens if (exists != NULL) { 5016fa9e4066Sahrens /* somebody beat us to the hash insert */ 5017fa9e4066Sahrens mutex_exit(hash_lock); 50183f9d6ad7SLin Ling buf_discard_identity(hdr); 5019dcbf3bd6SGeorge Wilson arc_hdr_destroy(hdr); 5020fa9e4066Sahrens goto top; /* restart the IO request */ 5021fa9e4066Sahrens } 5022fa9e4066Sahrens } else { 502389c86e32SChris Williamson /* 502489c86e32SChris Williamson * This block is in the ghost cache. If it was L2-only 502589c86e32SChris Williamson * (and thus didn't have an L1 hdr), we realloc the 502689c86e32SChris Williamson * header to add an L1 hdr. 502789c86e32SChris Williamson */ 502889c86e32SChris Williamson if (!HDR_HAS_L1HDR(hdr)) { 502989c86e32SChris Williamson hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, 503089c86e32SChris Williamson hdr_full_cache); 503189c86e32SChris Williamson } 5032770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 503389c86e32SChris Williamson ASSERT(GHOST_STATE(hdr->b_l1hdr.b_state)); 5034ea8dc4b6Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 503589c86e32SChris Williamson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 5036244781f1SPrakash Surya ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 50375602294fSDan Kimmel ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 503813506d1eSmaybee 5039cf6106c8SMatthew Ahrens /* 5040dcbf3bd6SGeorge Wilson * This is a delicate dance that we play here. 5041dcbf3bd6SGeorge Wilson * This hdr is in the ghost list so we access it 5042dcbf3bd6SGeorge Wilson * to move it out of the ghost list before we 5043dcbf3bd6SGeorge Wilson * initiate the read. If it's a prefetch then 5044dcbf3bd6SGeorge Wilson * it won't have a callback so we'll remove the 5045dcbf3bd6SGeorge Wilson * reference that arc_buf_alloc_impl() created. We 5046dcbf3bd6SGeorge Wilson * do this after we've called arc_access() to 5047dcbf3bd6SGeorge Wilson * avoid hitting an assert in remove_reference(). 5048cf6106c8SMatthew Ahrens */ 50497e453561SWilliam Gorrell arc_access(hdr, hash_lock); 5050770499e1SDan Kimmel arc_hdr_alloc_pabd(hdr); 5051dcbf3bd6SGeorge Wilson } 5052770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 5053dcbf3bd6SGeorge Wilson size = arc_hdr_size(hdr); 5054dcbf3bd6SGeorge Wilson 5055dcbf3bd6SGeorge Wilson /* 5056dcbf3bd6SGeorge Wilson * If compression is enabled on the hdr, then will do 5057dcbf3bd6SGeorge Wilson * RAW I/O and will store the compressed data in the hdr's 5058dcbf3bd6SGeorge Wilson * data block. Otherwise, the hdr's data block will contain 5059dcbf3bd6SGeorge Wilson * the uncompressed data. 5060dcbf3bd6SGeorge Wilson */ 5061dcbf3bd6SGeorge Wilson if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) { 5062dcbf3bd6SGeorge Wilson zio_flags |= ZIO_FLAG_RAW; 5063fa9e4066Sahrens } 5064fa9e4066Sahrens 5065dcbf3bd6SGeorge Wilson if (*arc_flags & ARC_FLAG_PREFETCH) 5066dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); 5067dcbf3bd6SGeorge Wilson if (*arc_flags & ARC_FLAG_L2CACHE) 5068dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 5069dcbf3bd6SGeorge Wilson if (BP_GET_LEVEL(bp) > 0) 5070dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT); 5071cf6106c8SMatthew Ahrens if (*arc_flags & ARC_FLAG_PREDICTIVE_PREFETCH) 5072dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PREDICTIVE_PREFETCH); 507389c86e32SChris Williamson ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); 50745614b00aSWilliam Gorrell 5075fa9e4066Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 5076fa9e4066Sahrens acb->acb_done = done; 5077fa9e4066Sahrens acb->acb_private = private; 50785602294fSDan Kimmel acb->acb_compressed = compressed_read; 5079fa9e4066Sahrens 5080dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 508189c86e32SChris Williamson hdr->b_l1hdr.b_acb = acb; 5082dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5083fa9e4066Sahrens 508489c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr) && 508589c86e32SChris Williamson (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { 508689c86e32SChris Williamson devw = hdr->b_l2hdr.b_dev->l2ad_writing; 508789c86e32SChris Williamson addr = hdr->b_l2hdr.b_daddr; 5088e14bb325SJeff Bonwick /* 50895cabbc6bSPrashanth Sreenivasa * Lock out L2ARC device removal. 5090e14bb325SJeff Bonwick */ 5091e14bb325SJeff Bonwick if (vdev_is_dead(vd) || 5092e14bb325SJeff Bonwick !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 5093e14bb325SJeff Bonwick vd = NULL; 50943a737e0dSbrendan } 50953a737e0dSbrendan 5096dcbf3bd6SGeorge Wilson if (priority == ZIO_PRIORITY_ASYNC_READ) 5097dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); 5098dcbf3bd6SGeorge Wilson else 5099dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); 5100dcbf3bd6SGeorge Wilson 51015d7b4d43SMatthew Ahrens if (hash_lock != NULL) 51025d7b4d43SMatthew Ahrens mutex_exit(hash_lock); 51033a737e0dSbrendan 51043e30c24aSWill Andrews /* 51053e30c24aSWill Andrews * At this point, we have a level 1 cache miss. Try again in 51063e30c24aSWill Andrews * L2ARC if possible. 51073e30c24aSWill Andrews */ 5108dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize); 5109dcbf3bd6SGeorge Wilson 51105c28183bSBrendan Gregg - Sun Microsystems DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 5111dcbf3bd6SGeorge Wilson uint64_t, lsize, zbookmark_phys_t *, zb); 511244cb6abcSbmc ARCSTAT_BUMP(arcstat_misses); 511389c86e32SChris Williamson ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 511489c86e32SChris Williamson demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 511544cb6abcSbmc data, metadata, misses); 5116ea8dc4b6Seschrock 51175a98e54bSBrendan Gregg - Sun Microsystems if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 5118fa94a07fSbrendan /* 5119fa94a07fSbrendan * Read from the L2ARC if the following are true: 51203a737e0dSbrendan * 1. The L2ARC vdev was previously cached. 51213a737e0dSbrendan * 2. This buffer still has L2ARC metadata. 51223a737e0dSbrendan * 3. This buffer isn't currently writing to the L2ARC. 51233a737e0dSbrendan * 4. The L2ARC entry wasn't evicted, which may 51243a737e0dSbrendan * also have invalidated the vdev. 51255a98e54bSBrendan Gregg - Sun Microsystems * 5. This isn't prefetch and l2arc_noprefetch is set. 5126fa94a07fSbrendan */ 512789c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr) && 51285a98e54bSBrendan Gregg - Sun Microsystems !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 51295a98e54bSBrendan Gregg - Sun Microsystems !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 5130fa94a07fSbrendan l2arc_read_callback_t *cb; 5131403a8da7SAndriy Gapon abd_t *abd; 5132403a8da7SAndriy Gapon uint64_t asize; 5133fa94a07fSbrendan 5134c5904d13Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 5135c5904d13Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 5136c5904d13Seschrock 5137fa94a07fSbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 5138fa94a07fSbrendan KM_SLEEP); 5139dcbf3bd6SGeorge Wilson cb->l2rcb_hdr = hdr; 5140fa94a07fSbrendan cb->l2rcb_bp = *bp; 5141fa94a07fSbrendan cb->l2rcb_zb = *zb; 51423baa08fcSek cb->l2rcb_flags = zio_flags; 5143fa94a07fSbrendan 5144403a8da7SAndriy Gapon asize = vdev_psize_to_asize(vd, size); 5145403a8da7SAndriy Gapon if (asize != size) { 5146403a8da7SAndriy Gapon abd = abd_alloc_for_io(asize, 5147403a8da7SAndriy Gapon HDR_ISTYPE_METADATA(hdr)); 5148403a8da7SAndriy Gapon cb->l2rcb_abd = abd; 5149403a8da7SAndriy Gapon } else { 5150403a8da7SAndriy Gapon abd = hdr->b_l1hdr.b_pabd; 5151403a8da7SAndriy Gapon } 5152403a8da7SAndriy Gapon 5153d5285caeSGeorge Wilson ASSERT(addr >= VDEV_LABEL_START_SIZE && 5154403a8da7SAndriy Gapon addr + asize <= vd->vdev_psize - 5155d5285caeSGeorge Wilson VDEV_LABEL_END_SIZE); 5156d5285caeSGeorge Wilson 5157fa94a07fSbrendan /* 5158e14bb325SJeff Bonwick * l2arc read. The SCL_L2ARC lock will be 5159e14bb325SJeff Bonwick * released by l2arc_read_done(). 5160aad02571SSaso Kiselkov * Issue a null zio if the underlying buffer 5161aad02571SSaso Kiselkov * was squashed to zero size by compression. 5162fa94a07fSbrendan */ 5163dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), !=, 5164dcbf3bd6SGeorge Wilson ZIO_COMPRESS_EMPTY); 5165dcbf3bd6SGeorge Wilson rzio = zio_read_phys(pio, vd, addr, 5166403a8da7SAndriy Gapon asize, abd, 5167dcbf3bd6SGeorge Wilson ZIO_CHECKSUM_OFF, 5168dcbf3bd6SGeorge Wilson l2arc_read_done, cb, priority, 5169dcbf3bd6SGeorge Wilson zio_flags | ZIO_FLAG_DONT_CACHE | 5170dcbf3bd6SGeorge Wilson ZIO_FLAG_CANFAIL | 5171dcbf3bd6SGeorge Wilson ZIO_FLAG_DONT_PROPAGATE | 5172dcbf3bd6SGeorge Wilson ZIO_FLAG_DONT_RETRY, B_FALSE); 5173fa94a07fSbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 5174fa94a07fSbrendan zio_t *, rzio); 5175dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_l2_read_bytes, size); 5176fa94a07fSbrendan 51777adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_NOWAIT) { 51783a737e0dSbrendan zio_nowait(rzio); 51793a737e0dSbrendan return (0); 51803a737e0dSbrendan } 5181fa94a07fSbrendan 51827adb730bSGeorge Wilson ASSERT(*arc_flags & ARC_FLAG_WAIT); 51833a737e0dSbrendan if (zio_wait(rzio) == 0) 51843a737e0dSbrendan return (0); 51853a737e0dSbrendan 51863a737e0dSbrendan /* l2arc read error; goto zio_read() */ 5187fa94a07fSbrendan } else { 5188fa94a07fSbrendan DTRACE_PROBE1(l2arc__miss, 5189fa94a07fSbrendan arc_buf_hdr_t *, hdr); 5190fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_misses); 5191fa94a07fSbrendan if (HDR_L2_WRITING(hdr)) 5192fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 5193e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, vd); 5194fa94a07fSbrendan } 51955a98e54bSBrendan Gregg - Sun Microsystems } else { 519676a25fafSBill Moore if (vd != NULL) 519776a25fafSBill Moore spa_config_exit(spa, SCL_L2ARC, vd); 51985a98e54bSBrendan Gregg - Sun Microsystems if (l2arc_ndev != 0) { 51995a98e54bSBrendan Gregg - Sun Microsystems DTRACE_PROBE1(l2arc__miss, 52005a98e54bSBrendan Gregg - Sun Microsystems arc_buf_hdr_t *, hdr); 52015a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_BUMP(arcstat_l2_misses); 52025a98e54bSBrendan Gregg - Sun Microsystems } 5203fa94a07fSbrendan } 5204c5904d13Seschrock 5205770499e1SDan Kimmel rzio = zio_read(pio, spa, bp, hdr->b_l1hdr.b_pabd, size, 5206dcbf3bd6SGeorge Wilson arc_read_done, hdr, priority, zio_flags, zb); 5207fa9e4066Sahrens 52087adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_WAIT) 5209fa9e4066Sahrens return (zio_wait(rzio)); 5210fa9e4066Sahrens 52117adb730bSGeorge Wilson ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 5212fa9e4066Sahrens zio_nowait(rzio); 5213fa9e4066Sahrens } 5214fa9e4066Sahrens return (0); 5215fa9e4066Sahrens } 5216fa9e4066Sahrens 52176e6d5868SMatthew Ahrens /* 52186e6d5868SMatthew Ahrens * Notify the arc that a block was freed, and thus will never be used again. 52196e6d5868SMatthew Ahrens */ 52206e6d5868SMatthew Ahrens void 52216e6d5868SMatthew Ahrens arc_freed(spa_t *spa, const blkptr_t *bp) 52226e6d5868SMatthew Ahrens { 52236e6d5868SMatthew Ahrens arc_buf_hdr_t *hdr; 52246e6d5868SMatthew Ahrens kmutex_t *hash_lock; 52256e6d5868SMatthew Ahrens uint64_t guid = spa_load_guid(spa); 52266e6d5868SMatthew Ahrens 52275d7b4d43SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp)); 52285d7b4d43SMatthew Ahrens 52295d7b4d43SMatthew Ahrens hdr = buf_hash_find(guid, bp, &hash_lock); 52306e6d5868SMatthew Ahrens if (hdr == NULL) 52316e6d5868SMatthew Ahrens return; 52326e6d5868SMatthew Ahrens 5233dcbf3bd6SGeorge Wilson /* 5234dcbf3bd6SGeorge Wilson * We might be trying to free a block that is still doing I/O 5235dcbf3bd6SGeorge Wilson * (i.e. prefetch) or has a reference (i.e. a dedup-ed, 5236dcbf3bd6SGeorge Wilson * dmu_sync-ed block). If this block is being prefetched, then it 5237dcbf3bd6SGeorge Wilson * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr 5238dcbf3bd6SGeorge Wilson * until the I/O completes. A block may also have a reference if it is 5239dcbf3bd6SGeorge Wilson * part of a dedup-ed, dmu_synced write. The dmu_sync() function would 5240dcbf3bd6SGeorge Wilson * have written the new block to its final resting place on disk but 5241dcbf3bd6SGeorge Wilson * without the dedup flag set. This would have left the hdr in the MRU 5242dcbf3bd6SGeorge Wilson * state and discoverable. When the txg finally syncs it detects that 5243dcbf3bd6SGeorge Wilson * the block was overridden in open context and issues an override I/O. 5244dcbf3bd6SGeorge Wilson * Since this is a dedup block, the override I/O will determine if the 5245dcbf3bd6SGeorge Wilson * block is already in the DDT. If so, then it will replace the io_bp 5246dcbf3bd6SGeorge Wilson * with the bp from the DDT and allow the I/O to finish. When the I/O 5247dcbf3bd6SGeorge Wilson * reaches the done callback, dbuf_write_override_done, it will 5248dcbf3bd6SGeorge Wilson * check to see if the io_bp and io_bp_override are identical. 5249dcbf3bd6SGeorge Wilson * If they are not, then it indicates that the bp was replaced with 5250dcbf3bd6SGeorge Wilson * the bp in the DDT and the override bp is freed. This allows 5251dcbf3bd6SGeorge Wilson * us to arrive here with a reference on a block that is being 5252dcbf3bd6SGeorge Wilson * freed. So if we have an I/O in progress, or a reference to 5253dcbf3bd6SGeorge Wilson * this hdr, then we don't destroy the hdr. 5254dcbf3bd6SGeorge Wilson */ 5255dcbf3bd6SGeorge Wilson if (!HDR_HAS_L1HDR(hdr) || (!HDR_IO_IN_PROGRESS(hdr) && 5256dcbf3bd6SGeorge Wilson refcount_is_zero(&hdr->b_l1hdr.b_refcnt))) { 5257dcbf3bd6SGeorge Wilson arc_change_state(arc_anon, hdr, hash_lock); 5258dcbf3bd6SGeorge Wilson arc_hdr_destroy(hdr); 52596e6d5868SMatthew Ahrens mutex_exit(hash_lock); 5260bbfa8ea8SMatthew Ahrens } else { 5261dcbf3bd6SGeorge Wilson mutex_exit(hash_lock); 5262ea8dc4b6Seschrock } 5263dd6ef538Smaybee 5264ea8dc4b6Seschrock } 5265ea8dc4b6Seschrock 5266fa9e4066Sahrens /* 52673e30c24aSWill Andrews * Release this buffer from the cache, making it an anonymous buffer. This 52683e30c24aSWill Andrews * must be done after a read and prior to modifying the buffer contents. 5269fa9e4066Sahrens * If the buffer has more than one reference, we must make 5270088f3894Sahrens * a new hdr for the buffer. 5271fa9e4066Sahrens */ 5272fa9e4066Sahrens void 5273fa9e4066Sahrens arc_release(arc_buf_t *buf, void *tag) 5274fa9e4066Sahrens { 527589c86e32SChris Williamson arc_buf_hdr_t *hdr = buf->b_hdr; 5276fa9e4066Sahrens 52773f9d6ad7SLin Ling /* 52783f9d6ad7SLin Ling * It would be nice to assert that if it's DMU metadata (level > 52793f9d6ad7SLin Ling * 0 || it's the dnode file), then it must be syncing context. 52803f9d6ad7SLin Ling * But we don't know that information at this level. 52813f9d6ad7SLin Ling */ 52823f9d6ad7SLin Ling 52833f9d6ad7SLin Ling mutex_enter(&buf->b_evict_lock); 52846f83844dSMark Maybee 5285244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 5286244781f1SPrakash Surya 528789c86e32SChris Williamson /* 528889c86e32SChris Williamson * We don't grab the hash lock prior to this check, because if 528989c86e32SChris Williamson * the buffer's header is in the arc_anon state, it won't be 529089c86e32SChris Williamson * linked into the hash table. 529189c86e32SChris Williamson */ 529289c86e32SChris Williamson if (hdr->b_l1hdr.b_state == arc_anon) { 529389c86e32SChris Williamson mutex_exit(&buf->b_evict_lock); 529489c86e32SChris Williamson ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 529589c86e32SChris Williamson ASSERT(!HDR_IN_HASH_TABLE(hdr)); 529689c86e32SChris Williamson ASSERT(!HDR_HAS_L2HDR(hdr)); 5297dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 5298fa9e4066Sahrens 5299dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 530089c86e32SChris Williamson ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); 530189c86e32SChris Williamson ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 530289c86e32SChris Williamson 530389c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = 0; 5304dcbf3bd6SGeorge Wilson 5305dcbf3bd6SGeorge Wilson /* 5306dcbf3bd6SGeorge Wilson * If the buf is being overridden then it may already 5307dcbf3bd6SGeorge Wilson * have a hdr that is not empty. 5308dcbf3bd6SGeorge Wilson */ 5309dcbf3bd6SGeorge Wilson buf_discard_identity(hdr); 531089c86e32SChris Williamson arc_buf_thaw(buf); 531189c86e32SChris Williamson 531289c86e32SChris Williamson return; 5313fa9e4066Sahrens } 5314fa9e4066Sahrens 531589c86e32SChris Williamson kmutex_t *hash_lock = HDR_LOCK(hdr); 531689c86e32SChris Williamson mutex_enter(hash_lock); 531789c86e32SChris Williamson 531889c86e32SChris Williamson /* 531989c86e32SChris Williamson * This assignment is only valid as long as the hash_lock is 532089c86e32SChris Williamson * held, we must be careful not to reference state or the 532189c86e32SChris Williamson * b_state field after dropping the lock. 532289c86e32SChris Williamson */ 532389c86e32SChris Williamson arc_state_t *state = hdr->b_l1hdr.b_state; 532489c86e32SChris Williamson ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 532589c86e32SChris Williamson ASSERT3P(state, !=, arc_anon); 532689c86e32SChris Williamson 532789c86e32SChris Williamson /* this buffer is not on any list */ 53285602294fSDan Kimmel ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), >, 0); 532989c86e32SChris Williamson 533089c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr)) { 533189c86e32SChris Williamson mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); 5332244781f1SPrakash Surya 5333244781f1SPrakash Surya /* 5334a52fc310SPrakash Surya * We have to recheck this conditional again now that 5335a52fc310SPrakash Surya * we're holding the l2ad_mtx to prevent a race with 5336a52fc310SPrakash Surya * another thread which might be concurrently calling 5337a52fc310SPrakash Surya * l2arc_evict(). In that case, l2arc_evict() might have 5338a52fc310SPrakash Surya * destroyed the header's L2 portion as we were waiting 5339a52fc310SPrakash Surya * to acquire the l2ad_mtx. 5340244781f1SPrakash Surya */ 5341a52fc310SPrakash Surya if (HDR_HAS_L2HDR(hdr)) 5342a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(hdr); 5343244781f1SPrakash Surya 534489c86e32SChris Williamson mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); 53456f83844dSMark Maybee } 53466f83844dSMark Maybee 5347ea8dc4b6Seschrock /* 5348ea8dc4b6Seschrock * Do we have more than one buf? 5349ea8dc4b6Seschrock */ 5350dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_bufcnt > 1) { 5351fa9e4066Sahrens arc_buf_hdr_t *nhdr; 5352ac05c741SMark Maybee uint64_t spa = hdr->b_spa; 5353dcbf3bd6SGeorge Wilson uint64_t psize = HDR_GET_PSIZE(hdr); 5354dcbf3bd6SGeorge Wilson uint64_t lsize = HDR_GET_LSIZE(hdr); 5355dcbf3bd6SGeorge Wilson enum zio_compress compress = HDR_GET_COMPRESS(hdr); 535689c86e32SChris Williamson arc_buf_contents_t type = arc_buf_type(hdr); 5357dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 5358fa9e4066Sahrens 535989c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); 5360dcbf3bd6SGeorge Wilson (void) remove_reference(hdr, hash_lock, tag); 5361dcbf3bd6SGeorge Wilson 53625602294fSDan Kimmel if (arc_buf_is_shared(buf) && !ARC_BUF_COMPRESSED(buf)) { 5363dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); 5364dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(buf)); 5365dcbf3bd6SGeorge Wilson } 5366dcbf3bd6SGeorge Wilson 5367fa9e4066Sahrens /* 53683f9d6ad7SLin Ling * Pull the data off of this hdr and attach it to 5369dcbf3bd6SGeorge Wilson * a new anonymous hdr. Also find the last buffer 5370dcbf3bd6SGeorge Wilson * in the hdr's buffer list. 5371fa9e4066Sahrens */ 53725602294fSDan Kimmel arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); 5373dcbf3bd6SGeorge Wilson ASSERT3P(lastbuf, !=, NULL); 5374dcbf3bd6SGeorge Wilson 5375dcbf3bd6SGeorge Wilson /* 5376dcbf3bd6SGeorge Wilson * If the current arc_buf_t and the hdr are sharing their data 53775602294fSDan Kimmel * buffer, then we must stop sharing that block. 5378dcbf3bd6SGeorge Wilson */ 5379dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 5380dcbf3bd6SGeorge Wilson VERIFY(!arc_buf_is_shared(lastbuf)); 5381ea8dc4b6Seschrock 5382dcbf3bd6SGeorge Wilson /* 5383dcbf3bd6SGeorge Wilson * First, sever the block sharing relationship between 53845602294fSDan Kimmel * buf and the arc_buf_hdr_t. 5385dcbf3bd6SGeorge Wilson */ 5386dcbf3bd6SGeorge Wilson arc_unshare_buf(hdr, buf); 53875602294fSDan Kimmel 53885602294fSDan Kimmel /* 5389770499e1SDan Kimmel * Now we need to recreate the hdr's b_pabd. Since we 53905602294fSDan Kimmel * have lastbuf handy, we try to share with it, but if 5391770499e1SDan Kimmel * we can't then we allocate a new b_pabd and copy the 53925602294fSDan Kimmel * data from buf into it. 53935602294fSDan Kimmel */ 53945602294fSDan Kimmel if (arc_can_share(hdr, lastbuf)) { 53955602294fSDan Kimmel arc_share_buf(hdr, lastbuf); 53965602294fSDan Kimmel } else { 5397770499e1SDan Kimmel arc_hdr_alloc_pabd(hdr); 5398770499e1SDan Kimmel abd_copy_from_buf(hdr->b_l1hdr.b_pabd, 5399770499e1SDan Kimmel buf->b_data, psize); 54005602294fSDan Kimmel } 5401dcbf3bd6SGeorge Wilson VERIFY3P(lastbuf->b_data, !=, NULL); 5402dcbf3bd6SGeorge Wilson } else if (HDR_SHARED_DATA(hdr)) { 54035602294fSDan Kimmel /* 54045602294fSDan Kimmel * Uncompressed shared buffers are always at the end 54055602294fSDan Kimmel * of the list. Compressed buffers don't have the 54065602294fSDan Kimmel * same requirements. This makes it hard to 54075602294fSDan Kimmel * simply assert that the lastbuf is shared so 54085602294fSDan Kimmel * we rely on the hdr's compression flags to determine 54095602294fSDan Kimmel * if we have a compressed, shared buffer. 54105602294fSDan Kimmel */ 54115602294fSDan Kimmel ASSERT(arc_buf_is_shared(lastbuf) || 54125602294fSDan Kimmel HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF); 54135602294fSDan Kimmel ASSERT(!ARC_BUF_SHARED(buf)); 5414dcbf3bd6SGeorge Wilson } 5415770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 541689c86e32SChris Williamson ASSERT3P(state, !=, arc_l2c_only); 54172fd872a7SPrakash Surya 5418dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_size, 54195602294fSDan Kimmel arc_buf_size(buf), buf); 54202fd872a7SPrakash Surya 542189c86e32SChris Williamson if (refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { 542289c86e32SChris Williamson ASSERT3P(state, !=, arc_l2c_only); 5423dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 54245602294fSDan Kimmel arc_buf_size(buf), buf); 5425ea8dc4b6Seschrock } 54269253d63dSGeorge Wilson 5427dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt -= 1; 5428c717a561Smaybee arc_cksum_verify(buf); 5429cd1c8b85SMatthew Ahrens arc_buf_unwatch(buf); 5430ea8dc4b6Seschrock 5431fa9e4066Sahrens mutex_exit(hash_lock); 5432fa9e4066Sahrens 5433dcbf3bd6SGeorge Wilson /* 5434770499e1SDan Kimmel * Allocate a new hdr. The new hdr will contain a b_pabd 5435dcbf3bd6SGeorge Wilson * buffer which will be freed in arc_write(). 5436dcbf3bd6SGeorge Wilson */ 5437dcbf3bd6SGeorge Wilson nhdr = arc_hdr_alloc(spa, psize, lsize, compress, type); 5438dcbf3bd6SGeorge Wilson ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL); 5439dcbf3bd6SGeorge Wilson ASSERT0(nhdr->b_l1hdr.b_bufcnt); 5440dcbf3bd6SGeorge Wilson ASSERT0(refcount_count(&nhdr->b_l1hdr.b_refcnt)); 5441dcbf3bd6SGeorge Wilson VERIFY3U(nhdr->b_type, ==, type); 5442dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(nhdr)); 544389c86e32SChris Williamson 544489c86e32SChris Williamson nhdr->b_l1hdr.b_buf = buf; 5445dcbf3bd6SGeorge Wilson nhdr->b_l1hdr.b_bufcnt = 1; 544689c86e32SChris Williamson (void) refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); 5447af2c4821Smaybee buf->b_hdr = nhdr; 5448dcbf3bd6SGeorge Wilson 54493f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 5450dcbf3bd6SGeorge Wilson (void) refcount_add_many(&arc_anon->arcs_size, 54515602294fSDan Kimmel arc_buf_size(buf), buf); 5452fa9e4066Sahrens } else { 54533f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 545489c86e32SChris Williamson ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); 5455244781f1SPrakash Surya /* protected by hash lock, or hdr is on arc_anon */ 5456244781f1SPrakash Surya ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 5457fa9e4066Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 545889c86e32SChris Williamson arc_change_state(arc_anon, hdr, hash_lock); 545989c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = 0; 546089c86e32SChris Williamson mutex_exit(hash_lock); 5461fa94a07fSbrendan 54623f9d6ad7SLin Ling buf_discard_identity(hdr); 5463c717a561Smaybee arc_buf_thaw(buf); 5464fa9e4066Sahrens } 5465fa9e4066Sahrens } 5466fa9e4066Sahrens 5467fa9e4066Sahrens int 5468fa9e4066Sahrens arc_released(arc_buf_t *buf) 5469fa9e4066Sahrens { 54706f83844dSMark Maybee int released; 54716f83844dSMark Maybee 54723f9d6ad7SLin Ling mutex_enter(&buf->b_evict_lock); 547389c86e32SChris Williamson released = (buf->b_data != NULL && 547489c86e32SChris Williamson buf->b_hdr->b_l1hdr.b_state == arc_anon); 54753f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 54766f83844dSMark Maybee return (released); 5477ea8dc4b6Seschrock } 5478ea8dc4b6Seschrock 5479ea8dc4b6Seschrock #ifdef ZFS_DEBUG 5480ea8dc4b6Seschrock int 5481ea8dc4b6Seschrock arc_referenced(arc_buf_t *buf) 5482ea8dc4b6Seschrock { 54836f83844dSMark Maybee int referenced; 54846f83844dSMark Maybee 54853f9d6ad7SLin Ling mutex_enter(&buf->b_evict_lock); 548689c86e32SChris Williamson referenced = (refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); 54873f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 54886f83844dSMark Maybee return (referenced); 5489ea8dc4b6Seschrock } 5490ea8dc4b6Seschrock #endif 5491ea8dc4b6Seschrock 5492c717a561Smaybee static void 5493c717a561Smaybee arc_write_ready(zio_t *zio) 5494c717a561Smaybee { 5495c717a561Smaybee arc_write_callback_t *callback = zio->io_private; 5496c717a561Smaybee arc_buf_t *buf = callback->awcb_buf; 54970a4e9518Sgw arc_buf_hdr_t *hdr = buf->b_hdr; 5498dcbf3bd6SGeorge Wilson uint64_t psize = BP_IS_HOLE(zio->io_bp) ? 0 : BP_GET_PSIZE(zio->io_bp); 5499c717a561Smaybee 550089c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 550189c86e32SChris Williamson ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); 5502dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 5503e14bb325SJeff Bonwick 55040a4e9518Sgw /* 5505dcbf3bd6SGeorge Wilson * If we're reexecuting this zio because the pool suspended, then 5506dcbf3bd6SGeorge Wilson * cleanup any state that was previously set the first time the 55075602294fSDan Kimmel * callback was invoked. 55080a4e9518Sgw */ 5509dcbf3bd6SGeorge Wilson if (zio->io_flags & ZIO_FLAG_REEXECUTED) { 5510dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 5511dcbf3bd6SGeorge Wilson arc_buf_unwatch(buf); 5512770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 5513dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 5514dcbf3bd6SGeorge Wilson arc_unshare_buf(hdr, buf); 5515dcbf3bd6SGeorge Wilson } else { 5516770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 5517dcbf3bd6SGeorge Wilson } 55180a4e9518Sgw } 55190a4e9518Sgw } 5520770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 5521dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 5522dcbf3bd6SGeorge Wilson ASSERT(!arc_buf_is_shared(buf)); 5523dcbf3bd6SGeorge Wilson 5524dcbf3bd6SGeorge Wilson callback->awcb_ready(zio, buf, callback->awcb_private); 5525dcbf3bd6SGeorge Wilson 5526dcbf3bd6SGeorge Wilson if (HDR_IO_IN_PROGRESS(hdr)) 5527dcbf3bd6SGeorge Wilson ASSERT(zio->io_flags & ZIO_FLAG_REEXECUTED); 5528dcbf3bd6SGeorge Wilson 5529dcbf3bd6SGeorge Wilson arc_cksum_compute(buf); 5530dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5531dcbf3bd6SGeorge Wilson 5532dcbf3bd6SGeorge Wilson enum zio_compress compress; 5533dcbf3bd6SGeorge Wilson if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 5534dcbf3bd6SGeorge Wilson compress = ZIO_COMPRESS_OFF; 5535dcbf3bd6SGeorge Wilson } else { 5536dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(zio->io_bp)); 5537dcbf3bd6SGeorge Wilson compress = BP_GET_COMPRESS(zio->io_bp); 5538dcbf3bd6SGeorge Wilson } 5539dcbf3bd6SGeorge Wilson HDR_SET_PSIZE(hdr, psize); 5540dcbf3bd6SGeorge Wilson arc_hdr_set_compress(hdr, compress); 5541dcbf3bd6SGeorge Wilson 5542770499e1SDan Kimmel 5543dcbf3bd6SGeorge Wilson /* 5544770499e1SDan Kimmel * Fill the hdr with data. If the hdr is compressed, the data we want 5545770499e1SDan Kimmel * is available from the zio, otherwise we can take it from the buf. 5546770499e1SDan Kimmel * 5547770499e1SDan Kimmel * We might be able to share the buf's data with the hdr here. However, 5548770499e1SDan Kimmel * doing so would cause the ARC to be full of linear ABDs if we write a 5549770499e1SDan Kimmel * lot of shareable data. As a compromise, we check whether scattered 5550770499e1SDan Kimmel * ABDs are allowed, and assume that if they are then the user wants 5551770499e1SDan Kimmel * the ARC to be primarily filled with them regardless of the data being 5552770499e1SDan Kimmel * written. Therefore, if they're allowed then we allocate one and copy 5553770499e1SDan Kimmel * the data into it; otherwise, we share the data directly if we can. 5554dcbf3bd6SGeorge Wilson */ 5555770499e1SDan Kimmel if (zfs_abd_scatter_enabled || !arc_can_share(hdr, buf)) { 5556770499e1SDan Kimmel arc_hdr_alloc_pabd(hdr); 5557770499e1SDan Kimmel 5558770499e1SDan Kimmel /* 5559770499e1SDan Kimmel * Ideally, we would always copy the io_abd into b_pabd, but the 5560770499e1SDan Kimmel * user may have disabled compressed ARC, thus we must check the 5561770499e1SDan Kimmel * hdr's compression setting rather than the io_bp's. 5562770499e1SDan Kimmel */ 5563770499e1SDan Kimmel if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) { 5564770499e1SDan Kimmel ASSERT3U(BP_GET_COMPRESS(zio->io_bp), !=, 5565770499e1SDan Kimmel ZIO_COMPRESS_OFF); 5566770499e1SDan Kimmel ASSERT3U(psize, >, 0); 5567770499e1SDan Kimmel 5568770499e1SDan Kimmel abd_copy(hdr->b_l1hdr.b_pabd, zio->io_abd, psize); 5569770499e1SDan Kimmel } else { 5570770499e1SDan Kimmel ASSERT3U(zio->io_orig_size, ==, arc_hdr_size(hdr)); 5571770499e1SDan Kimmel 5572770499e1SDan Kimmel abd_copy_from_buf(hdr->b_l1hdr.b_pabd, buf->b_data, 5573770499e1SDan Kimmel arc_buf_size(buf)); 5574770499e1SDan Kimmel } 5575dcbf3bd6SGeorge Wilson } else { 5576770499e1SDan Kimmel ASSERT3P(buf->b_data, ==, abd_to_buf(zio->io_orig_abd)); 55775602294fSDan Kimmel ASSERT3U(zio->io_orig_size, ==, arc_buf_size(buf)); 5578dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 5579dcbf3bd6SGeorge Wilson 5580dcbf3bd6SGeorge Wilson arc_share_buf(hdr, buf); 5581dcbf3bd6SGeorge Wilson } 5582770499e1SDan Kimmel 5583dcbf3bd6SGeorge Wilson arc_hdr_verify(hdr, zio->io_bp); 5584c717a561Smaybee } 5585c717a561Smaybee 55868df0bcf0SPaul Dagnelie static void 55878df0bcf0SPaul Dagnelie arc_write_children_ready(zio_t *zio) 55888df0bcf0SPaul Dagnelie { 55898df0bcf0SPaul Dagnelie arc_write_callback_t *callback = zio->io_private; 55908df0bcf0SPaul Dagnelie arc_buf_t *buf = callback->awcb_buf; 55918df0bcf0SPaul Dagnelie 55928df0bcf0SPaul Dagnelie callback->awcb_children_ready(zio, buf, callback->awcb_private); 55938df0bcf0SPaul Dagnelie } 55948df0bcf0SPaul Dagnelie 559569962b56SMatthew Ahrens /* 559669962b56SMatthew Ahrens * The SPA calls this callback for each physical write that happens on behalf 559769962b56SMatthew Ahrens * of a logical write. See the comment in dbuf_write_physdone() for details. 559869962b56SMatthew Ahrens */ 559969962b56SMatthew Ahrens static void 560069962b56SMatthew Ahrens arc_write_physdone(zio_t *zio) 560169962b56SMatthew Ahrens { 560269962b56SMatthew Ahrens arc_write_callback_t *cb = zio->io_private; 560369962b56SMatthew Ahrens if (cb->awcb_physdone != NULL) 560469962b56SMatthew Ahrens cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 560569962b56SMatthew Ahrens } 560669962b56SMatthew Ahrens 5607fa9e4066Sahrens static void 5608fa9e4066Sahrens arc_write_done(zio_t *zio) 5609fa9e4066Sahrens { 5610c717a561Smaybee arc_write_callback_t *callback = zio->io_private; 5611c717a561Smaybee arc_buf_t *buf = callback->awcb_buf; 5612c717a561Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 5613fa9e4066Sahrens 5614dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 5615b24ab676SJeff Bonwick 5616b24ab676SJeff Bonwick if (zio->io_error == 0) { 5617dcbf3bd6SGeorge Wilson arc_hdr_verify(hdr, zio->io_bp); 5618dcbf3bd6SGeorge Wilson 56195d7b4d43SMatthew Ahrens if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 562043466aaeSMax Grossman buf_discard_identity(hdr); 562143466aaeSMax Grossman } else { 562243466aaeSMax Grossman hdr->b_dva = *BP_IDENTITY(zio->io_bp); 562343466aaeSMax Grossman hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 562443466aaeSMax Grossman } 5625b24ab676SJeff Bonwick } else { 5626dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 5627b24ab676SJeff Bonwick } 5628fa9e4066Sahrens 5629ea8dc4b6Seschrock /* 56305d7b4d43SMatthew Ahrens * If the block to be written was all-zero or compressed enough to be 56315d7b4d43SMatthew Ahrens * embedded in the BP, no write was performed so there will be no 56325d7b4d43SMatthew Ahrens * dva/birth/checksum. The buffer must therefore remain anonymous 56335d7b4d43SMatthew Ahrens * (and uncached). 5634ea8dc4b6Seschrock */ 5635dcbf3bd6SGeorge Wilson if (!HDR_EMPTY(hdr)) { 5636fa9e4066Sahrens arc_buf_hdr_t *exists; 5637fa9e4066Sahrens kmutex_t *hash_lock; 5638fa9e4066Sahrens 56395602294fSDan Kimmel ASSERT3U(zio->io_error, ==, 0); 5640b24ab676SJeff Bonwick 56416b4acc8bSahrens arc_cksum_verify(buf); 56426b4acc8bSahrens 5643fa9e4066Sahrens exists = buf_hash_insert(hdr, &hash_lock); 564489c86e32SChris Williamson if (exists != NULL) { 5645fa9e4066Sahrens /* 5646fa9e4066Sahrens * This can only happen if we overwrite for 5647fa9e4066Sahrens * sync-to-convergence, because we remove 5648fa9e4066Sahrens * buffers from the hash table when we arc_free(). 5649fa9e4066Sahrens */ 5650b24ab676SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 5651b24ab676SJeff Bonwick if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 5652b24ab676SJeff Bonwick panic("bad overwrite, hdr=%p exists=%p", 5653b24ab676SJeff Bonwick (void *)hdr, (void *)exists); 565489c86e32SChris Williamson ASSERT(refcount_is_zero( 565589c86e32SChris Williamson &exists->b_l1hdr.b_refcnt)); 5656b24ab676SJeff Bonwick arc_change_state(arc_anon, exists, hash_lock); 5657b24ab676SJeff Bonwick mutex_exit(hash_lock); 5658b24ab676SJeff Bonwick arc_hdr_destroy(exists); 5659b24ab676SJeff Bonwick exists = buf_hash_insert(hdr, &hash_lock); 5660b24ab676SJeff Bonwick ASSERT3P(exists, ==, NULL); 566180901aeaSGeorge Wilson } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 566280901aeaSGeorge Wilson /* nopwrite */ 566380901aeaSGeorge Wilson ASSERT(zio->io_prop.zp_nopwrite); 566480901aeaSGeorge Wilson if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 566580901aeaSGeorge Wilson panic("bad nopwrite, hdr=%p exists=%p", 566680901aeaSGeorge Wilson (void *)hdr, (void *)exists); 5667b24ab676SJeff Bonwick } else { 5668b24ab676SJeff Bonwick /* Dedup */ 5669dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt == 1); 567089c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_state == arc_anon); 5671b24ab676SJeff Bonwick ASSERT(BP_GET_DEDUP(zio->io_bp)); 5672b24ab676SJeff Bonwick ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 5673ae46e4c7SMatthew Ahrens } 5674fa9e4066Sahrens } 5675dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5676088f3894Sahrens /* if it's not anon, we are doing a scrub */ 567789c86e32SChris Williamson if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) 5678088f3894Sahrens arc_access(hdr, hash_lock); 567944eda4d7Smaybee mutex_exit(hash_lock); 5680ea8dc4b6Seschrock } else { 5681dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5682fa9e4066Sahrens } 5683ea8dc4b6Seschrock 568489c86e32SChris Williamson ASSERT(!refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 5685b24ab676SJeff Bonwick callback->awcb_done(zio, buf, callback->awcb_private); 5686fa9e4066Sahrens 5687770499e1SDan Kimmel abd_put(zio->io_abd); 5688c717a561Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 5689fa9e4066Sahrens } 5690fa9e4066Sahrens 5691c717a561Smaybee zio_t * 5692dcbf3bd6SGeorge Wilson arc_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 5693dcbf3bd6SGeorge Wilson boolean_t l2arc, const zio_prop_t *zp, arc_done_func_t *ready, 56948df0bcf0SPaul Dagnelie arc_done_func_t *children_ready, arc_done_func_t *physdone, 569569962b56SMatthew Ahrens arc_done_func_t *done, void *private, zio_priority_t priority, 56967802d7bfSMatthew Ahrens int zio_flags, const zbookmark_phys_t *zb) 5697fa9e4066Sahrens { 5698fa9e4066Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 5699c717a561Smaybee arc_write_callback_t *callback; 5700e14bb325SJeff Bonwick zio_t *zio; 5701adaec86aSMatthew Ahrens zio_prop_t localprop = *zp; 5702fa9e4066Sahrens 5703dcbf3bd6SGeorge Wilson ASSERT3P(ready, !=, NULL); 5704dcbf3bd6SGeorge Wilson ASSERT3P(done, !=, NULL); 5705fa9e4066Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 570689c86e32SChris Williamson ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 5707dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 5708dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); 57093baa08fcSek if (l2arc) 5710dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 57115602294fSDan Kimmel if (ARC_BUF_COMPRESSED(buf)) { 5712adaec86aSMatthew Ahrens /* 5713adaec86aSMatthew Ahrens * We're writing a pre-compressed buffer. Make the 5714adaec86aSMatthew Ahrens * compression algorithm requested by the zio_prop_t match 5715adaec86aSMatthew Ahrens * the pre-compressed buffer's compression algorithm. 5716adaec86aSMatthew Ahrens */ 5717adaec86aSMatthew Ahrens localprop.zp_compress = HDR_GET_COMPRESS(hdr); 5718adaec86aSMatthew Ahrens 57195602294fSDan Kimmel ASSERT3U(HDR_GET_LSIZE(hdr), !=, arc_buf_size(buf)); 57205602294fSDan Kimmel zio_flags |= ZIO_FLAG_RAW; 57215602294fSDan Kimmel } 5722c717a561Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 5723c717a561Smaybee callback->awcb_ready = ready; 57248df0bcf0SPaul Dagnelie callback->awcb_children_ready = children_ready; 572569962b56SMatthew Ahrens callback->awcb_physdone = physdone; 5726c717a561Smaybee callback->awcb_done = done; 5727c717a561Smaybee callback->awcb_private = private; 5728c717a561Smaybee callback->awcb_buf = buf; 5729088f3894Sahrens 5730dcbf3bd6SGeorge Wilson /* 5731770499e1SDan Kimmel * The hdr's b_pabd is now stale, free it now. A new data block 5732dcbf3bd6SGeorge Wilson * will be allocated when the zio pipeline calls arc_write_ready(). 5733dcbf3bd6SGeorge Wilson */ 5734770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 5735dcbf3bd6SGeorge Wilson /* 5736dcbf3bd6SGeorge Wilson * If the buf is currently sharing the data block with 5737dcbf3bd6SGeorge Wilson * the hdr then we need to break that relationship here. 5738dcbf3bd6SGeorge Wilson * The hdr will remain with a NULL data pointer and the 5739dcbf3bd6SGeorge Wilson * buf will take sole ownership of the block. 5740dcbf3bd6SGeorge Wilson */ 5741dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 5742dcbf3bd6SGeorge Wilson arc_unshare_buf(hdr, buf); 5743dcbf3bd6SGeorge Wilson } else { 5744770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 5745dcbf3bd6SGeorge Wilson } 5746dcbf3bd6SGeorge Wilson VERIFY3P(buf->b_data, !=, NULL); 5747dcbf3bd6SGeorge Wilson arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF); 5748dcbf3bd6SGeorge Wilson } 5749dcbf3bd6SGeorge Wilson ASSERT(!arc_buf_is_shared(buf)); 5750770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 5751dcbf3bd6SGeorge Wilson 5752770499e1SDan Kimmel zio = zio_write(pio, spa, txg, bp, 5753770499e1SDan Kimmel abd_get_from_buf(buf->b_data, HDR_GET_LSIZE(hdr)), 5754adaec86aSMatthew Ahrens HDR_GET_LSIZE(hdr), arc_buf_size(buf), &localprop, arc_write_ready, 57558df0bcf0SPaul Dagnelie (children_ready != NULL) ? arc_write_children_ready : NULL, 57568df0bcf0SPaul Dagnelie arc_write_physdone, arc_write_done, callback, 575769962b56SMatthew Ahrens priority, zio_flags, zb); 5758fa9e4066Sahrens 5759c717a561Smaybee return (zio); 5760fa9e4066Sahrens } 5761fa9e4066Sahrens 57621ab7f2deSmaybee static int 576369962b56SMatthew Ahrens arc_memory_throttle(uint64_t reserve, uint64_t txg) 57641ab7f2deSmaybee { 57651ab7f2deSmaybee #ifdef _KERNEL 57661ab7f2deSmaybee uint64_t available_memory = ptob(freemem); 57671ab7f2deSmaybee static uint64_t page_load = 0; 57681ab7f2deSmaybee static uint64_t last_txg = 0; 57691ab7f2deSmaybee 57701ab7f2deSmaybee #if defined(__i386) 57711ab7f2deSmaybee available_memory = 57721ab7f2deSmaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 57731ab7f2deSmaybee #endif 577469962b56SMatthew Ahrens 577569962b56SMatthew Ahrens if (freemem > physmem * arc_lotsfree_percent / 100) 57761ab7f2deSmaybee return (0); 57771ab7f2deSmaybee 57781ab7f2deSmaybee if (txg > last_txg) { 57791ab7f2deSmaybee last_txg = txg; 57801ab7f2deSmaybee page_load = 0; 57811ab7f2deSmaybee } 57821ab7f2deSmaybee /* 57831ab7f2deSmaybee * If we are in pageout, we know that memory is already tight, 57841ab7f2deSmaybee * the arc is already going to be evicting, so we just want to 57851ab7f2deSmaybee * continue to let page writes occur as quickly as possible. 57861ab7f2deSmaybee */ 57871ab7f2deSmaybee if (curproc == proc_pageout) { 57881ab7f2deSmaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 5789be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART)); 57901ab7f2deSmaybee /* Note: reserve is inflated, so we deflate */ 57911ab7f2deSmaybee page_load += reserve / 8; 57921ab7f2deSmaybee return (0); 57931ab7f2deSmaybee } else if (page_load > 0 && arc_reclaim_needed()) { 57941ab7f2deSmaybee /* memory is low, delay before restarting */ 57951ab7f2deSmaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 5796be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 57971ab7f2deSmaybee } 57981ab7f2deSmaybee page_load = 0; 57991ab7f2deSmaybee #endif 58001ab7f2deSmaybee return (0); 58011ab7f2deSmaybee } 58021ab7f2deSmaybee 5803fa9e4066Sahrens void 58041ab7f2deSmaybee arc_tempreserve_clear(uint64_t reserve) 5805fa9e4066Sahrens { 58061ab7f2deSmaybee atomic_add_64(&arc_tempreserve, -reserve); 5807fa9e4066Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 5808fa9e4066Sahrens } 5809fa9e4066Sahrens 5810fa9e4066Sahrens int 58111ab7f2deSmaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 5812fa9e4066Sahrens { 58131ab7f2deSmaybee int error; 58142fdbea25SAleksandr Guzovskiy uint64_t anon_size; 58151ab7f2deSmaybee 58161ab7f2deSmaybee if (reserve > arc_c/4 && !arc_no_grow) 58171ab7f2deSmaybee arc_c = MIN(arc_c_max, reserve * 4); 58181ab7f2deSmaybee if (reserve > arc_c) 5819be6fd75aSMatthew Ahrens return (SET_ERROR(ENOMEM)); 5820112fe045Smaybee 58212fdbea25SAleksandr Guzovskiy /* 58222fdbea25SAleksandr Guzovskiy * Don't count loaned bufs as in flight dirty data to prevent long 58232fdbea25SAleksandr Guzovskiy * network delays from blocking transactions that are ready to be 58242fdbea25SAleksandr Guzovskiy * assigned to a txg. 58252fdbea25SAleksandr Guzovskiy */ 58265602294fSDan Kimmel 58275602294fSDan Kimmel /* assert that it has not wrapped around */ 58285602294fSDan Kimmel ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); 58295602294fSDan Kimmel 58302fd872a7SPrakash Surya anon_size = MAX((int64_t)(refcount_count(&arc_anon->arcs_size) - 58312fd872a7SPrakash Surya arc_loaned_bytes), 0); 58322fdbea25SAleksandr Guzovskiy 58331ab7f2deSmaybee /* 58341ab7f2deSmaybee * Writes will, almost always, require additional memory allocations 5835f7170741SWill Andrews * in order to compress/encrypt/etc the data. We therefore need to 58361ab7f2deSmaybee * make sure that there is sufficient available memory for this. 58371ab7f2deSmaybee */ 583869962b56SMatthew Ahrens error = arc_memory_throttle(reserve, txg); 583969962b56SMatthew Ahrens if (error != 0) 58401ab7f2deSmaybee return (error); 58411ab7f2deSmaybee 5842fa9e4066Sahrens /* 5843112fe045Smaybee * Throttle writes when the amount of dirty data in the cache 5844112fe045Smaybee * gets too large. We try to keep the cache less than half full 5845112fe045Smaybee * of dirty blocks so that our sync times don't grow too large. 5846112fe045Smaybee * Note: if two requests come in concurrently, we might let them 5847112fe045Smaybee * both succeed, when one of them should fail. Not a huge deal. 5848fa9e4066Sahrens */ 58492fdbea25SAleksandr Guzovskiy 58502fdbea25SAleksandr Guzovskiy if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 58512fdbea25SAleksandr Guzovskiy anon_size > arc_c / 4) { 5852dcbf3bd6SGeorge Wilson uint64_t meta_esize = 5853dcbf3bd6SGeorge Wilson refcount_count(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 5854dcbf3bd6SGeorge Wilson uint64_t data_esize = 5855dcbf3bd6SGeorge Wilson refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 58560e8c6158Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 58570e8c6158Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 5858dcbf3bd6SGeorge Wilson arc_tempreserve >> 10, meta_esize >> 10, 5859dcbf3bd6SGeorge Wilson data_esize >> 10, reserve >> 10, arc_c >> 10); 5860be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART)); 5861fa9e4066Sahrens } 58621ab7f2deSmaybee atomic_add_64(&arc_tempreserve, reserve); 5863fa9e4066Sahrens return (0); 5864fa9e4066Sahrens } 5865fa9e4066Sahrens 58664076b1bfSPrakash Surya static void 58674076b1bfSPrakash Surya arc_kstat_update_state(arc_state_t *state, kstat_named_t *size, 58684076b1bfSPrakash Surya kstat_named_t *evict_data, kstat_named_t *evict_metadata) 58694076b1bfSPrakash Surya { 58702fd872a7SPrakash Surya size->value.ui64 = refcount_count(&state->arcs_size); 5871dcbf3bd6SGeorge Wilson evict_data->value.ui64 = 5872dcbf3bd6SGeorge Wilson refcount_count(&state->arcs_esize[ARC_BUFC_DATA]); 5873dcbf3bd6SGeorge Wilson evict_metadata->value.ui64 = 5874dcbf3bd6SGeorge Wilson refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]); 58754076b1bfSPrakash Surya } 58764076b1bfSPrakash Surya 58774076b1bfSPrakash Surya static int 58784076b1bfSPrakash Surya arc_kstat_update(kstat_t *ksp, int rw) 58794076b1bfSPrakash Surya { 58804076b1bfSPrakash Surya arc_stats_t *as = ksp->ks_data; 58814076b1bfSPrakash Surya 58824076b1bfSPrakash Surya if (rw == KSTAT_WRITE) { 58834076b1bfSPrakash Surya return (EACCES); 58844076b1bfSPrakash Surya } else { 58854076b1bfSPrakash Surya arc_kstat_update_state(arc_anon, 58864076b1bfSPrakash Surya &as->arcstat_anon_size, 58874076b1bfSPrakash Surya &as->arcstat_anon_evictable_data, 58884076b1bfSPrakash Surya &as->arcstat_anon_evictable_metadata); 58894076b1bfSPrakash Surya arc_kstat_update_state(arc_mru, 58904076b1bfSPrakash Surya &as->arcstat_mru_size, 58914076b1bfSPrakash Surya &as->arcstat_mru_evictable_data, 58924076b1bfSPrakash Surya &as->arcstat_mru_evictable_metadata); 58934076b1bfSPrakash Surya arc_kstat_update_state(arc_mru_ghost, 58944076b1bfSPrakash Surya &as->arcstat_mru_ghost_size, 58954076b1bfSPrakash Surya &as->arcstat_mru_ghost_evictable_data, 58964076b1bfSPrakash Surya &as->arcstat_mru_ghost_evictable_metadata); 58974076b1bfSPrakash Surya arc_kstat_update_state(arc_mfu, 58984076b1bfSPrakash Surya &as->arcstat_mfu_size, 58994076b1bfSPrakash Surya &as->arcstat_mfu_evictable_data, 59004076b1bfSPrakash Surya &as->arcstat_mfu_evictable_metadata); 59014076b1bfSPrakash Surya arc_kstat_update_state(arc_mfu_ghost, 59024076b1bfSPrakash Surya &as->arcstat_mfu_ghost_size, 59034076b1bfSPrakash Surya &as->arcstat_mfu_ghost_evictable_data, 59044076b1bfSPrakash Surya &as->arcstat_mfu_ghost_evictable_metadata); 5905*3a2d8a1bSPaul Dagnelie 5906*3a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_size) = aggsum_value(&arc_size); 5907*3a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_meta_used) = aggsum_value(&arc_meta_used); 5908*3a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_data_size) = aggsum_value(&astat_data_size); 5909*3a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_metadata_size) = 5910*3a2d8a1bSPaul Dagnelie aggsum_value(&astat_metadata_size); 5911*3a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_hdr_size) = aggsum_value(&astat_hdr_size); 5912*3a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_other_size) = aggsum_value(&astat_other_size); 5913*3a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_l2_hdr_size) = aggsum_value(&astat_l2_hdr_size); 59144076b1bfSPrakash Surya } 59154076b1bfSPrakash Surya 59164076b1bfSPrakash Surya return (0); 59174076b1bfSPrakash Surya } 59184076b1bfSPrakash Surya 5919dcbf3bd6SGeorge Wilson /* 5920dcbf3bd6SGeorge Wilson * This function *must* return indices evenly distributed between all 5921dcbf3bd6SGeorge Wilson * sublists of the multilist. This is needed due to how the ARC eviction 5922dcbf3bd6SGeorge Wilson * code is laid out; arc_evict_state() assumes ARC buffers are evenly 5923dcbf3bd6SGeorge Wilson * distributed between all sublists and uses this assumption when 5924dcbf3bd6SGeorge Wilson * deciding which sublist to evict from and how much to evict from it. 5925dcbf3bd6SGeorge Wilson */ 5926dcbf3bd6SGeorge Wilson unsigned int 5927dcbf3bd6SGeorge Wilson arc_state_multilist_index_func(multilist_t *ml, void *obj) 5928dcbf3bd6SGeorge Wilson { 5929dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = obj; 5930dcbf3bd6SGeorge Wilson 5931dcbf3bd6SGeorge Wilson /* 5932dcbf3bd6SGeorge Wilson * We rely on b_dva to generate evenly distributed index 5933dcbf3bd6SGeorge Wilson * numbers using buf_hash below. So, as an added precaution, 5934dcbf3bd6SGeorge Wilson * let's make sure we never add empty buffers to the arc lists. 5935dcbf3bd6SGeorge Wilson */ 5936dcbf3bd6SGeorge Wilson ASSERT(!HDR_EMPTY(hdr)); 5937dcbf3bd6SGeorge Wilson 5938dcbf3bd6SGeorge Wilson /* 5939dcbf3bd6SGeorge Wilson * The assumption here, is the hash value for a given 5940dcbf3bd6SGeorge Wilson * arc_buf_hdr_t will remain constant throughout it's lifetime 5941dcbf3bd6SGeorge Wilson * (i.e. it's b_spa, b_dva, and b_birth fields don't change). 5942dcbf3bd6SGeorge Wilson * Thus, we don't need to store the header's sublist index 5943dcbf3bd6SGeorge Wilson * on insertion, as this index can be recalculated on removal. 5944dcbf3bd6SGeorge Wilson * 5945dcbf3bd6SGeorge Wilson * Also, the low order bits of the hash value are thought to be 5946dcbf3bd6SGeorge Wilson * distributed evenly. Otherwise, in the case that the multilist 5947dcbf3bd6SGeorge Wilson * has a power of two number of sublists, each sublists' usage 5948dcbf3bd6SGeorge Wilson * would not be evenly distributed. 5949dcbf3bd6SGeorge Wilson */ 5950dcbf3bd6SGeorge Wilson return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % 5951dcbf3bd6SGeorge Wilson multilist_get_num_sublists(ml)); 5952dcbf3bd6SGeorge Wilson } 5953dcbf3bd6SGeorge Wilson 5954dcbf3bd6SGeorge Wilson static void 5955dcbf3bd6SGeorge Wilson arc_state_init(void) 5956dcbf3bd6SGeorge Wilson { 5957dcbf3bd6SGeorge Wilson arc_anon = &ARC_anon; 5958dcbf3bd6SGeorge Wilson arc_mru = &ARC_mru; 5959dcbf3bd6SGeorge Wilson arc_mru_ghost = &ARC_mru_ghost; 5960dcbf3bd6SGeorge Wilson arc_mfu = &ARC_mfu; 5961dcbf3bd6SGeorge Wilson arc_mfu_ghost = &ARC_mfu_ghost; 5962dcbf3bd6SGeorge Wilson arc_l2c_only = &ARC_l2c_only; 5963dcbf3bd6SGeorge Wilson 596494c2d0ebSMatthew Ahrens arc_mru->arcs_list[ARC_BUFC_METADATA] = 596594c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 5966dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 596710fbdecbSMatthew Ahrens arc_state_multilist_index_func); 596894c2d0ebSMatthew Ahrens arc_mru->arcs_list[ARC_BUFC_DATA] = 596994c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 5970dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 597110fbdecbSMatthew Ahrens arc_state_multilist_index_func); 597294c2d0ebSMatthew Ahrens arc_mru_ghost->arcs_list[ARC_BUFC_METADATA] = 597394c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 5974dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 597510fbdecbSMatthew Ahrens arc_state_multilist_index_func); 597694c2d0ebSMatthew Ahrens arc_mru_ghost->arcs_list[ARC_BUFC_DATA] = 597794c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 5978dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 597910fbdecbSMatthew Ahrens arc_state_multilist_index_func); 598094c2d0ebSMatthew Ahrens arc_mfu->arcs_list[ARC_BUFC_METADATA] = 598194c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 5982dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 598310fbdecbSMatthew Ahrens arc_state_multilist_index_func); 598494c2d0ebSMatthew Ahrens arc_mfu->arcs_list[ARC_BUFC_DATA] = 598594c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 5986dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 598710fbdecbSMatthew Ahrens arc_state_multilist_index_func); 598894c2d0ebSMatthew Ahrens arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA] = 598994c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 5990dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 599110fbdecbSMatthew Ahrens arc_state_multilist_index_func); 599294c2d0ebSMatthew Ahrens arc_mfu_ghost->arcs_list[ARC_BUFC_DATA] = 599394c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 5994dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 599510fbdecbSMatthew Ahrens arc_state_multilist_index_func); 599694c2d0ebSMatthew Ahrens arc_l2c_only->arcs_list[ARC_BUFC_METADATA] = 599794c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 5998dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 599910fbdecbSMatthew Ahrens arc_state_multilist_index_func); 600094c2d0ebSMatthew Ahrens arc_l2c_only->arcs_list[ARC_BUFC_DATA] = 600194c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6002dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 600310fbdecbSMatthew Ahrens arc_state_multilist_index_func); 6004dcbf3bd6SGeorge Wilson 6005dcbf3bd6SGeorge Wilson refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 6006dcbf3bd6SGeorge Wilson refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 6007dcbf3bd6SGeorge Wilson refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); 6008dcbf3bd6SGeorge Wilson refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]); 6009dcbf3bd6SGeorge Wilson refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); 6010dcbf3bd6SGeorge Wilson refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); 6011dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); 6012dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); 6013dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); 6014dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); 6015dcbf3bd6SGeorge Wilson refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); 6016dcbf3bd6SGeorge Wilson refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); 6017dcbf3bd6SGeorge Wilson 6018dcbf3bd6SGeorge Wilson refcount_create(&arc_anon->arcs_size); 6019dcbf3bd6SGeorge Wilson refcount_create(&arc_mru->arcs_size); 6020dcbf3bd6SGeorge Wilson refcount_create(&arc_mru_ghost->arcs_size); 6021dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu->arcs_size); 6022dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu_ghost->arcs_size); 6023dcbf3bd6SGeorge Wilson refcount_create(&arc_l2c_only->arcs_size); 6024*3a2d8a1bSPaul Dagnelie 6025*3a2d8a1bSPaul Dagnelie aggsum_init(&arc_meta_used, 0); 6026*3a2d8a1bSPaul Dagnelie aggsum_init(&arc_size, 0); 6027*3a2d8a1bSPaul Dagnelie aggsum_init(&astat_data_size, 0); 6028*3a2d8a1bSPaul Dagnelie aggsum_init(&astat_metadata_size, 0); 6029*3a2d8a1bSPaul Dagnelie aggsum_init(&astat_hdr_size, 0); 6030*3a2d8a1bSPaul Dagnelie aggsum_init(&astat_other_size, 0); 6031*3a2d8a1bSPaul Dagnelie aggsum_init(&astat_l2_hdr_size, 0); 6032dcbf3bd6SGeorge Wilson } 6033dcbf3bd6SGeorge Wilson 6034dcbf3bd6SGeorge Wilson static void 6035dcbf3bd6SGeorge Wilson arc_state_fini(void) 6036dcbf3bd6SGeorge Wilson { 6037dcbf3bd6SGeorge Wilson refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 6038dcbf3bd6SGeorge Wilson refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 6039dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); 6040dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]); 6041dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); 6042dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); 6043dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); 6044dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); 6045dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); 6046dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); 6047dcbf3bd6SGeorge Wilson refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); 6048dcbf3bd6SGeorge Wilson refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); 6049dcbf3bd6SGeorge Wilson 6050dcbf3bd6SGeorge Wilson refcount_destroy(&arc_anon->arcs_size); 6051dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru->arcs_size); 6052dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru_ghost->arcs_size); 6053dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu->arcs_size); 6054dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu_ghost->arcs_size); 6055dcbf3bd6SGeorge Wilson refcount_destroy(&arc_l2c_only->arcs_size); 6056dcbf3bd6SGeorge Wilson 605794c2d0ebSMatthew Ahrens multilist_destroy(arc_mru->arcs_list[ARC_BUFC_METADATA]); 605894c2d0ebSMatthew Ahrens multilist_destroy(arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 605994c2d0ebSMatthew Ahrens multilist_destroy(arc_mfu->arcs_list[ARC_BUFC_METADATA]); 606094c2d0ebSMatthew Ahrens multilist_destroy(arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 606194c2d0ebSMatthew Ahrens multilist_destroy(arc_mru->arcs_list[ARC_BUFC_DATA]); 606294c2d0ebSMatthew Ahrens multilist_destroy(arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 606394c2d0ebSMatthew Ahrens multilist_destroy(arc_mfu->arcs_list[ARC_BUFC_DATA]); 606494c2d0ebSMatthew Ahrens multilist_destroy(arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 6065dcbf3bd6SGeorge Wilson } 6066dcbf3bd6SGeorge Wilson 6067dcbf3bd6SGeorge Wilson uint64_t 6068dcbf3bd6SGeorge Wilson arc_max_bytes(void) 6069244781f1SPrakash Surya { 6070dcbf3bd6SGeorge Wilson return (arc_c_max); 6071244781f1SPrakash Surya } 6072244781f1SPrakash Surya 6073fa9e4066Sahrens void 6074fa9e4066Sahrens arc_init(void) 6075fa9e4066Sahrens { 60762ec99e3eSMatthew Ahrens /* 60772ec99e3eSMatthew Ahrens * allmem is "all memory that we could possibly use". 60782ec99e3eSMatthew Ahrens */ 60792ec99e3eSMatthew Ahrens #ifdef _KERNEL 60802ec99e3eSMatthew Ahrens uint64_t allmem = ptob(physmem - swapfs_minfree); 60812ec99e3eSMatthew Ahrens #else 60822ec99e3eSMatthew Ahrens uint64_t allmem = (physmem * PAGESIZE) / 2; 60832ec99e3eSMatthew Ahrens #endif 60842ec99e3eSMatthew Ahrens 6085244781f1SPrakash Surya mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL); 6086244781f1SPrakash Surya cv_init(&arc_reclaim_thread_cv, NULL, CV_DEFAULT, NULL); 6087244781f1SPrakash Surya cv_init(&arc_reclaim_waiters_cv, NULL, CV_DEFAULT, NULL); 6088244781f1SPrakash Surya 608913506d1eSmaybee /* Convert seconds to clock ticks */ 6090b19a79ecSperrin arc_min_prefetch_lifespan = 1 * hz; 609113506d1eSmaybee 6092112fe045Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 60932ec99e3eSMatthew Ahrens arc_c_min = MAX(allmem / 32, 64 << 20); 6094112fe045Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 60952ec99e3eSMatthew Ahrens if (allmem >= 1 << 30) 60962ec99e3eSMatthew Ahrens arc_c_max = allmem - (1 << 30); 6097fa9e4066Sahrens else 609844cb6abcSbmc arc_c_max = arc_c_min; 60992ec99e3eSMatthew Ahrens arc_c_max = MAX(allmem * 3 / 4, arc_c_max); 6100a2eea2e1Sahrens 61018fe00bfbSMatthew Ahrens /* 61028fe00bfbSMatthew Ahrens * In userland, there's only the memory pressure that we artificially 61038fe00bfbSMatthew Ahrens * create (see arc_available_memory()). Don't let arc_c get too 61048fe00bfbSMatthew Ahrens * small, because it can cause transactions to be larger than 61058fe00bfbSMatthew Ahrens * arc_c, causing arc_tempreserve_space() to fail. 61068fe00bfbSMatthew Ahrens */ 61078fe00bfbSMatthew Ahrens #ifndef _KERNEL 61088fe00bfbSMatthew Ahrens arc_c_min = arc_c_max / 2; 61098fe00bfbSMatthew Ahrens #endif 61108fe00bfbSMatthew Ahrens 6111a2eea2e1Sahrens /* 6112a2eea2e1Sahrens * Allow the tunables to override our calculations if they are 6113a2eea2e1Sahrens * reasonable (ie. over 64MB) 6114a2eea2e1Sahrens */ 6115e5961710SMatthew Ahrens if (zfs_arc_max > 64 << 20 && zfs_arc_max < allmem) { 611644cb6abcSbmc arc_c_max = zfs_arc_max; 6117e5961710SMatthew Ahrens arc_c_min = MIN(arc_c_min, arc_c_max); 6118e5961710SMatthew Ahrens } 61192ec99e3eSMatthew Ahrens if (zfs_arc_min > 64 << 20 && zfs_arc_min <= arc_c_max) 612044cb6abcSbmc arc_c_min = zfs_arc_min; 6121a2eea2e1Sahrens 612244cb6abcSbmc arc_c = arc_c_max; 612344cb6abcSbmc arc_p = (arc_c >> 1); 6124fa9e4066Sahrens 61250e8c6158Smaybee /* limit meta-data to 1/4 of the arc capacity */ 61260e8c6158Smaybee arc_meta_limit = arc_c_max / 4; 61271116048bSek 6128af868f46SMatthew Ahrens #ifdef _KERNEL 6129af868f46SMatthew Ahrens /* 6130af868f46SMatthew Ahrens * Metadata is stored in the kernel's heap. Don't let us 6131af868f46SMatthew Ahrens * use more than half the heap for the ARC. 6132af868f46SMatthew Ahrens */ 6133af868f46SMatthew Ahrens arc_meta_limit = MIN(arc_meta_limit, 6134af868f46SMatthew Ahrens vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 2); 6135af868f46SMatthew Ahrens #endif 6136af868f46SMatthew Ahrens 61371116048bSek /* Allow the tunable to override if it is reasonable */ 61381116048bSek if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 61391116048bSek arc_meta_limit = zfs_arc_meta_limit; 61401116048bSek 61410e8c6158Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 61420e8c6158Smaybee arc_c_min = arc_meta_limit / 2; 61430e8c6158Smaybee 61443a5286a1SMatthew Ahrens if (zfs_arc_meta_min > 0) { 61453a5286a1SMatthew Ahrens arc_meta_min = zfs_arc_meta_min; 61463a5286a1SMatthew Ahrens } else { 61473a5286a1SMatthew Ahrens arc_meta_min = arc_c_min / 2; 61483a5286a1SMatthew Ahrens } 61493a5286a1SMatthew Ahrens 61505a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_grow_retry > 0) 61515a98e54bSBrendan Gregg - Sun Microsystems arc_grow_retry = zfs_arc_grow_retry; 61525a98e54bSBrendan Gregg - Sun Microsystems 61535a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_shrink_shift > 0) 61545a98e54bSBrendan Gregg - Sun Microsystems arc_shrink_shift = zfs_arc_shrink_shift; 61555a98e54bSBrendan Gregg - Sun Microsystems 61562ec99e3eSMatthew Ahrens /* 61572ec99e3eSMatthew Ahrens * Ensure that arc_no_grow_shift is less than arc_shrink_shift. 61582ec99e3eSMatthew Ahrens */ 61592ec99e3eSMatthew Ahrens if (arc_no_grow_shift >= arc_shrink_shift) 61602ec99e3eSMatthew Ahrens arc_no_grow_shift = arc_shrink_shift - 1; 61612ec99e3eSMatthew Ahrens 61625a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_p_min_shift > 0) 61635a98e54bSBrendan Gregg - Sun Microsystems arc_p_min_shift = zfs_arc_p_min_shift; 61645a98e54bSBrendan Gregg - Sun Microsystems 6165fa9e4066Sahrens /* if kmem_flags are set, lets try to use less memory */ 6166fa9e4066Sahrens if (kmem_debugging()) 616744cb6abcSbmc arc_c = arc_c / 2; 616844cb6abcSbmc if (arc_c < arc_c_min) 616944cb6abcSbmc arc_c = arc_c_min; 617044cb6abcSbmc 6171dcbf3bd6SGeorge Wilson arc_state_init(); 6172fa9e4066Sahrens buf_init(); 6173fa9e4066Sahrens 6174dcbf3bd6SGeorge Wilson arc_reclaim_thread_exit = B_FALSE; 6175fa9e4066Sahrens 617644cb6abcSbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 617744cb6abcSbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 617844cb6abcSbmc 617944cb6abcSbmc if (arc_ksp != NULL) { 618044cb6abcSbmc arc_ksp->ks_data = &arc_stats; 61814076b1bfSPrakash Surya arc_ksp->ks_update = arc_kstat_update; 618244cb6abcSbmc kstat_install(arc_ksp); 618344cb6abcSbmc } 618444cb6abcSbmc 6185fa9e4066Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 6186fa9e4066Sahrens TS_RUN, minclsyspri); 618749e3519aSmaybee 6188dcbf3bd6SGeorge Wilson arc_dead = B_FALSE; 61893a737e0dSbrendan arc_warm = B_FALSE; 61901ab7f2deSmaybee 619169962b56SMatthew Ahrens /* 619269962b56SMatthew Ahrens * Calculate maximum amount of dirty data per pool. 619369962b56SMatthew Ahrens * 619469962b56SMatthew Ahrens * If it has been set by /etc/system, take that. 619569962b56SMatthew Ahrens * Otherwise, use a percentage of physical memory defined by 619669962b56SMatthew Ahrens * zfs_dirty_data_max_percent (default 10%) with a cap at 619769962b56SMatthew Ahrens * zfs_dirty_data_max_max (default 4GB). 619869962b56SMatthew Ahrens */ 619969962b56SMatthew Ahrens if (zfs_dirty_data_max == 0) { 620069962b56SMatthew Ahrens zfs_dirty_data_max = physmem * PAGESIZE * 620169962b56SMatthew Ahrens zfs_dirty_data_max_percent / 100; 620269962b56SMatthew Ahrens zfs_dirty_data_max = MIN(zfs_dirty_data_max, 620369962b56SMatthew Ahrens zfs_dirty_data_max_max); 620469962b56SMatthew Ahrens } 6205fa9e4066Sahrens } 6206fa9e4066Sahrens 6207fa9e4066Sahrens void 6208fa9e4066Sahrens arc_fini(void) 6209fa9e4066Sahrens { 6210244781f1SPrakash Surya mutex_enter(&arc_reclaim_lock); 6211dcbf3bd6SGeorge Wilson arc_reclaim_thread_exit = B_TRUE; 6212244781f1SPrakash Surya /* 6213244781f1SPrakash Surya * The reclaim thread will set arc_reclaim_thread_exit back to 6214dcbf3bd6SGeorge Wilson * B_FALSE when it is finished exiting; we're waiting for that. 6215244781f1SPrakash Surya */ 6216244781f1SPrakash Surya while (arc_reclaim_thread_exit) { 6217244781f1SPrakash Surya cv_signal(&arc_reclaim_thread_cv); 6218244781f1SPrakash Surya cv_wait(&arc_reclaim_thread_cv, &arc_reclaim_lock); 6219244781f1SPrakash Surya } 6220244781f1SPrakash Surya mutex_exit(&arc_reclaim_lock); 6221fa9e4066Sahrens 6222dcbf3bd6SGeorge Wilson /* Use B_TRUE to ensure *all* buffers are evicted */ 6223dcbf3bd6SGeorge Wilson arc_flush(NULL, B_TRUE); 6224fa9e4066Sahrens 6225dcbf3bd6SGeorge Wilson arc_dead = B_TRUE; 6226fa9e4066Sahrens 622744cb6abcSbmc if (arc_ksp != NULL) { 622844cb6abcSbmc kstat_delete(arc_ksp); 622944cb6abcSbmc arc_ksp = NULL; 623044cb6abcSbmc } 623144cb6abcSbmc 6232244781f1SPrakash Surya mutex_destroy(&arc_reclaim_lock); 6233244781f1SPrakash Surya cv_destroy(&arc_reclaim_thread_cv); 6234244781f1SPrakash Surya cv_destroy(&arc_reclaim_waiters_cv); 6235244781f1SPrakash Surya 6236dcbf3bd6SGeorge Wilson arc_state_fini(); 6237fa9e4066Sahrens buf_fini(); 62382fdbea25SAleksandr Guzovskiy 623989c86e32SChris Williamson ASSERT0(arc_loaned_bytes); 6240fa9e4066Sahrens } 6241fa94a07fSbrendan 6242fa94a07fSbrendan /* 6243fa94a07fSbrendan * Level 2 ARC 6244fa94a07fSbrendan * 6245fa94a07fSbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 6246fa94a07fSbrendan * It uses dedicated storage devices to hold cached data, which are populated 6247fa94a07fSbrendan * using large infrequent writes. The main role of this cache is to boost 6248fa94a07fSbrendan * the performance of random read workloads. The intended L2ARC devices 6249fa94a07fSbrendan * include short-stroked disks, solid state disks, and other media with 6250fa94a07fSbrendan * substantially faster read latency than disk. 6251fa94a07fSbrendan * 6252fa94a07fSbrendan * +-----------------------+ 6253fa94a07fSbrendan * | ARC | 6254fa94a07fSbrendan * +-----------------------+ 6255fa94a07fSbrendan * | ^ ^ 6256fa94a07fSbrendan * | | | 6257fa94a07fSbrendan * l2arc_feed_thread() arc_read() 6258fa94a07fSbrendan * | | | 6259fa94a07fSbrendan * | l2arc read | 6260fa94a07fSbrendan * V | | 6261fa94a07fSbrendan * +---------------+ | 6262fa94a07fSbrendan * | L2ARC | | 6263fa94a07fSbrendan * +---------------+ | 6264fa94a07fSbrendan * | ^ | 6265fa94a07fSbrendan * l2arc_write() | | 6266fa94a07fSbrendan * | | | 6267fa94a07fSbrendan * V | | 6268fa94a07fSbrendan * +-------+ +-------+ 6269fa94a07fSbrendan * | vdev | | vdev | 6270fa94a07fSbrendan * | cache | | cache | 6271fa94a07fSbrendan * +-------+ +-------+ 6272fa94a07fSbrendan * +=========+ .-----. 6273fa94a07fSbrendan * : L2ARC : |-_____-| 6274fa94a07fSbrendan * : devices : | Disks | 6275fa94a07fSbrendan * +=========+ `-_____-' 6276fa94a07fSbrendan * 6277fa94a07fSbrendan * Read requests are satisfied from the following sources, in order: 6278fa94a07fSbrendan * 6279fa94a07fSbrendan * 1) ARC 6280fa94a07fSbrendan * 2) vdev cache of L2ARC devices 6281fa94a07fSbrendan * 3) L2ARC devices 6282fa94a07fSbrendan * 4) vdev cache of disks 6283fa94a07fSbrendan * 5) disks 6284fa94a07fSbrendan * 6285fa94a07fSbrendan * Some L2ARC device types exhibit extremely slow write performance. 6286fa94a07fSbrendan * To accommodate for this there are some significant differences between 6287fa94a07fSbrendan * the L2ARC and traditional cache design: 6288fa94a07fSbrendan * 6289fa94a07fSbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 6290fa94a07fSbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 6291fa94a07fSbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 6292fa94a07fSbrendan * this would add inflated write latencies for all ARC memory pressure. 6293fa94a07fSbrendan * 6294fa94a07fSbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 6295fa94a07fSbrendan * It does this by periodically scanning buffers from the eviction-end of 6296fa94a07fSbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 6297aad02571SSaso Kiselkov * not already there. It scans until a headroom of buffers is satisfied, 6298aad02571SSaso Kiselkov * which itself is a buffer for ARC eviction. If a compressible buffer is 6299aad02571SSaso Kiselkov * found during scanning and selected for writing to an L2ARC device, we 6300aad02571SSaso Kiselkov * temporarily boost scanning headroom during the next scan cycle to make 6301aad02571SSaso Kiselkov * sure we adapt to compression effects (which might significantly reduce 6302aad02571SSaso Kiselkov * the data volume we write to L2ARC). The thread that does this is 6303fa94a07fSbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 6304fa94a07fSbrendan * provide a better sense of ratio than this diagram: 6305fa94a07fSbrendan * 6306fa94a07fSbrendan * head --> tail 6307fa94a07fSbrendan * +---------------------+----------+ 6308fa94a07fSbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 6309fa94a07fSbrendan * +---------------------+----------+ | o L2ARC eligible 6310fa94a07fSbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 6311fa94a07fSbrendan * +---------------------+----------+ | 6312fa94a07fSbrendan * 15.9 Gbytes ^ 32 Mbytes | 6313fa94a07fSbrendan * headroom | 6314fa94a07fSbrendan * l2arc_feed_thread() 6315fa94a07fSbrendan * | 6316fa94a07fSbrendan * l2arc write hand <--[oooo]--' 6317fa94a07fSbrendan * | 8 Mbyte 6318fa94a07fSbrendan * | write max 6319fa94a07fSbrendan * V 6320fa94a07fSbrendan * +==============================+ 6321fa94a07fSbrendan * L2ARC dev |####|#|###|###| |####| ... | 6322fa94a07fSbrendan * +==============================+ 6323fa94a07fSbrendan * 32 Gbytes 6324fa94a07fSbrendan * 6325fa94a07fSbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 6326fa94a07fSbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 6327fa94a07fSbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 6328fa94a07fSbrendan * safe to say that this is an uncommon case, since buffers at the end of 6329fa94a07fSbrendan * the ARC lists have moved there due to inactivity. 6330fa94a07fSbrendan * 6331fa94a07fSbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 6332fa94a07fSbrendan * then the L2ARC simply misses copying some buffers. This serves as a 6333fa94a07fSbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 6334fa94a07fSbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 6335fa94a07fSbrendan * the potential for the L2ARC to churn if it attempts to cache content too 6336fa94a07fSbrendan * quickly, such as during backups of the entire pool. 6337fa94a07fSbrendan * 63383a737e0dSbrendan * 5. After system boot and before the ARC has filled main memory, there are 63393a737e0dSbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 63403a737e0dSbrendan * lists can remain mostly static. Instead of searching from tail of these 63413a737e0dSbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 63423a737e0dSbrendan * for eligible buffers, greatly increasing its chance of finding them. 63433a737e0dSbrendan * 63443a737e0dSbrendan * The L2ARC device write speed is also boosted during this time so that 63453a737e0dSbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 63463a737e0dSbrendan * there are no L2ARC reads, and no fear of degrading read performance 63473a737e0dSbrendan * through increased writes. 63483a737e0dSbrendan * 63493a737e0dSbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 6350fa94a07fSbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 6351fa94a07fSbrendan * device is written to in a rotor fashion, sweeping writes through 6352fa94a07fSbrendan * available space then repeating. 6353fa94a07fSbrendan * 63543a737e0dSbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 6355fa94a07fSbrendan * write buffers back to disk based storage. 6356fa94a07fSbrendan * 63573a737e0dSbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 6358fa94a07fSbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 6359fa94a07fSbrendan * 6360fa94a07fSbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 6361fa94a07fSbrendan * may be necessary for different workloads: 6362fa94a07fSbrendan * 6363fa94a07fSbrendan * l2arc_write_max max write bytes per interval 63643a737e0dSbrendan * l2arc_write_boost extra write bytes during device warmup 6365fa94a07fSbrendan * l2arc_noprefetch skip caching prefetched buffers 6366fa94a07fSbrendan * l2arc_headroom number of max device writes to precache 6367aad02571SSaso Kiselkov * l2arc_headroom_boost when we find compressed buffers during ARC 6368aad02571SSaso Kiselkov * scanning, we multiply headroom by this 6369aad02571SSaso Kiselkov * percentage factor for the next scan cycle, 6370aad02571SSaso Kiselkov * since more compressed buffers are likely to 6371aad02571SSaso Kiselkov * be present 6372fa94a07fSbrendan * l2arc_feed_secs seconds between L2ARC writing 6373fa94a07fSbrendan * 6374fa94a07fSbrendan * Tunables may be removed or added as future performance improvements are 6375fa94a07fSbrendan * integrated, and also may become zpool properties. 63765a98e54bSBrendan Gregg - Sun Microsystems * 63775a98e54bSBrendan Gregg - Sun Microsystems * There are three key functions that control how the L2ARC warms up: 63785a98e54bSBrendan Gregg - Sun Microsystems * 63795a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_eligible() check if a buffer is eligible to cache 63805a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_size() calculate how much to write 63815a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_interval() calculate sleep delay between writes 63825a98e54bSBrendan Gregg - Sun Microsystems * 63835a98e54bSBrendan Gregg - Sun Microsystems * These three functions determine what to write, how much, and how quickly 63845a98e54bSBrendan Gregg - Sun Microsystems * to send writes. 6385fa94a07fSbrendan */ 6386fa94a07fSbrendan 63875a98e54bSBrendan Gregg - Sun Microsystems static boolean_t 63887adb730bSGeorge Wilson l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) 63895a98e54bSBrendan Gregg - Sun Microsystems { 63905a98e54bSBrendan Gregg - Sun Microsystems /* 63915a98e54bSBrendan Gregg - Sun Microsystems * A buffer is *not* eligible for the L2ARC if it: 63925a98e54bSBrendan Gregg - Sun Microsystems * 1. belongs to a different spa. 63935ea40c06SBrendan Gregg - Sun Microsystems * 2. is already cached on the L2ARC. 63945ea40c06SBrendan Gregg - Sun Microsystems * 3. has an I/O in progress (it may be an incomplete read). 63955ea40c06SBrendan Gregg - Sun Microsystems * 4. is flagged not eligible (zfs property). 63965a98e54bSBrendan Gregg - Sun Microsystems */ 639789c86e32SChris Williamson if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) || 63987adb730bSGeorge Wilson HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr)) 63995a98e54bSBrendan Gregg - Sun Microsystems return (B_FALSE); 64005a98e54bSBrendan Gregg - Sun Microsystems 64015a98e54bSBrendan Gregg - Sun Microsystems return (B_TRUE); 64025a98e54bSBrendan Gregg - Sun Microsystems } 64035a98e54bSBrendan Gregg - Sun Microsystems 64045a98e54bSBrendan Gregg - Sun Microsystems static uint64_t 6405aad02571SSaso Kiselkov l2arc_write_size(void) 64065a98e54bSBrendan Gregg - Sun Microsystems { 64075a98e54bSBrendan Gregg - Sun Microsystems uint64_t size; 64085a98e54bSBrendan Gregg - Sun Microsystems 6409aad02571SSaso Kiselkov /* 6410aad02571SSaso Kiselkov * Make sure our globals have meaningful values in case the user 6411aad02571SSaso Kiselkov * altered them. 6412aad02571SSaso Kiselkov */ 6413aad02571SSaso Kiselkov size = l2arc_write_max; 6414aad02571SSaso Kiselkov if (size == 0) { 6415aad02571SSaso Kiselkov cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 6416aad02571SSaso Kiselkov "be greater than zero, resetting it to the default (%d)", 6417aad02571SSaso Kiselkov L2ARC_WRITE_SIZE); 6418aad02571SSaso Kiselkov size = l2arc_write_max = L2ARC_WRITE_SIZE; 6419aad02571SSaso Kiselkov } 64205a98e54bSBrendan Gregg - Sun Microsystems 64215a98e54bSBrendan Gregg - Sun Microsystems if (arc_warm == B_FALSE) 6422aad02571SSaso Kiselkov size += l2arc_write_boost; 64235a98e54bSBrendan Gregg - Sun Microsystems 64245a98e54bSBrendan Gregg - Sun Microsystems return (size); 64255a98e54bSBrendan Gregg - Sun Microsystems 64265a98e54bSBrendan Gregg - Sun Microsystems } 64275a98e54bSBrendan Gregg - Sun Microsystems 64285a98e54bSBrendan Gregg - Sun Microsystems static clock_t 64295a98e54bSBrendan Gregg - Sun Microsystems l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 64305a98e54bSBrendan Gregg - Sun Microsystems { 6431d3d50737SRafael Vanoni clock_t interval, next, now; 64325a98e54bSBrendan Gregg - Sun Microsystems 64335a98e54bSBrendan Gregg - Sun Microsystems /* 64345a98e54bSBrendan Gregg - Sun Microsystems * If the ARC lists are busy, increase our write rate; if the 64355a98e54bSBrendan Gregg - Sun Microsystems * lists are stale, idle back. This is achieved by checking 64365a98e54bSBrendan Gregg - Sun Microsystems * how much we previously wrote - if it was more than half of 64375a98e54bSBrendan Gregg - Sun Microsystems * what we wanted, schedule the next write much sooner. 64385a98e54bSBrendan Gregg - Sun Microsystems */ 64395a98e54bSBrendan Gregg - Sun Microsystems if (l2arc_feed_again && wrote > (wanted / 2)) 64405a98e54bSBrendan Gregg - Sun Microsystems interval = (hz * l2arc_feed_min_ms) / 1000; 64415a98e54bSBrendan Gregg - Sun Microsystems else 64425a98e54bSBrendan Gregg - Sun Microsystems interval = hz * l2arc_feed_secs; 64435a98e54bSBrendan Gregg - Sun Microsystems 6444d3d50737SRafael Vanoni now = ddi_get_lbolt(); 6445d3d50737SRafael Vanoni next = MAX(now, MIN(now + interval, began + interval)); 64465a98e54bSBrendan Gregg - Sun Microsystems 64475a98e54bSBrendan Gregg - Sun Microsystems return (next); 64485a98e54bSBrendan Gregg - Sun Microsystems } 64495a98e54bSBrendan Gregg - Sun Microsystems 6450fa94a07fSbrendan /* 6451fa94a07fSbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 64523a737e0dSbrendan * If a device is returned, this also returns holding the spa config lock. 6453fa94a07fSbrendan */ 6454fa94a07fSbrendan static l2arc_dev_t * 6455fa94a07fSbrendan l2arc_dev_get_next(void) 6456fa94a07fSbrendan { 64573a737e0dSbrendan l2arc_dev_t *first, *next = NULL; 64583a737e0dSbrendan 64593a737e0dSbrendan /* 64603a737e0dSbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 64613a737e0dSbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 64623a737e0dSbrendan * both locks will be dropped and a spa config lock held instead. 64633a737e0dSbrendan */ 64643a737e0dSbrendan mutex_enter(&spa_namespace_lock); 64653a737e0dSbrendan mutex_enter(&l2arc_dev_mtx); 6466fa94a07fSbrendan 6467c5904d13Seschrock /* if there are no vdevs, there is nothing to do */ 6468c5904d13Seschrock if (l2arc_ndev == 0) 64693a737e0dSbrendan goto out; 6470c5904d13Seschrock 6471c5904d13Seschrock first = NULL; 6472c5904d13Seschrock next = l2arc_dev_last; 6473c5904d13Seschrock do { 6474c5904d13Seschrock /* loop around the list looking for a non-faulted vdev */ 6475c5904d13Seschrock if (next == NULL) { 6476fa94a07fSbrendan next = list_head(l2arc_dev_list); 6477c5904d13Seschrock } else { 6478c5904d13Seschrock next = list_next(l2arc_dev_list, next); 6479c5904d13Seschrock if (next == NULL) 6480c5904d13Seschrock next = list_head(l2arc_dev_list); 6481c5904d13Seschrock } 6482c5904d13Seschrock 6483c5904d13Seschrock /* if we have come back to the start, bail out */ 6484c5904d13Seschrock if (first == NULL) 6485c5904d13Seschrock first = next; 6486c5904d13Seschrock else if (next == first) 6487c5904d13Seschrock break; 6488c5904d13Seschrock 6489c5904d13Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 6490c5904d13Seschrock 6491c5904d13Seschrock /* if we were unable to find any usable vdevs, return NULL */ 6492c5904d13Seschrock if (vdev_is_dead(next->l2ad_vdev)) 64933a737e0dSbrendan next = NULL; 6494fa94a07fSbrendan 6495fa94a07fSbrendan l2arc_dev_last = next; 6496fa94a07fSbrendan 64973a737e0dSbrendan out: 64983a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 64993a737e0dSbrendan 65003a737e0dSbrendan /* 65013a737e0dSbrendan * Grab the config lock to prevent the 'next' device from being 65023a737e0dSbrendan * removed while we are writing to it. 65033a737e0dSbrendan */ 65043a737e0dSbrendan if (next != NULL) 6505e14bb325SJeff Bonwick spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 65063a737e0dSbrendan mutex_exit(&spa_namespace_lock); 65073a737e0dSbrendan 6508fa94a07fSbrendan return (next); 6509fa94a07fSbrendan } 6510fa94a07fSbrendan 65113a737e0dSbrendan /* 65123a737e0dSbrendan * Free buffers that were tagged for destruction. 65133a737e0dSbrendan */ 65143a737e0dSbrendan static void 65153a737e0dSbrendan l2arc_do_free_on_write() 65163a737e0dSbrendan { 65173a737e0dSbrendan list_t *buflist; 65183a737e0dSbrendan l2arc_data_free_t *df, *df_prev; 65193a737e0dSbrendan 65203a737e0dSbrendan mutex_enter(&l2arc_free_on_write_mtx); 65213a737e0dSbrendan buflist = l2arc_free_on_write; 65223a737e0dSbrendan 65233a737e0dSbrendan for (df = list_tail(buflist); df; df = df_prev) { 65243a737e0dSbrendan df_prev = list_prev(buflist, df); 6525770499e1SDan Kimmel ASSERT3P(df->l2df_abd, !=, NULL); 6526770499e1SDan Kimmel abd_free(df->l2df_abd); 65273a737e0dSbrendan list_remove(buflist, df); 65283a737e0dSbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 65293a737e0dSbrendan } 65303a737e0dSbrendan 65313a737e0dSbrendan mutex_exit(&l2arc_free_on_write_mtx); 65323a737e0dSbrendan } 65333a737e0dSbrendan 6534fa94a07fSbrendan /* 6535fa94a07fSbrendan * A write to a cache device has completed. Update all headers to allow 6536fa94a07fSbrendan * reads from these buffers to begin. 6537fa94a07fSbrendan */ 6538fa94a07fSbrendan static void 6539fa94a07fSbrendan l2arc_write_done(zio_t *zio) 6540fa94a07fSbrendan { 6541fa94a07fSbrendan l2arc_write_callback_t *cb; 6542fa94a07fSbrendan l2arc_dev_t *dev; 6543fa94a07fSbrendan list_t *buflist; 65447adb730bSGeorge Wilson arc_buf_hdr_t *head, *hdr, *hdr_prev; 6545fa94a07fSbrendan kmutex_t *hash_lock; 65463038a2b4SSaso Kiselkov int64_t bytes_dropped = 0; 6547fa94a07fSbrendan 6548fa94a07fSbrendan cb = zio->io_private; 6549dcbf3bd6SGeorge Wilson ASSERT3P(cb, !=, NULL); 6550fa94a07fSbrendan dev = cb->l2wcb_dev; 6551dcbf3bd6SGeorge Wilson ASSERT3P(dev, !=, NULL); 6552fa94a07fSbrendan head = cb->l2wcb_head; 6553dcbf3bd6SGeorge Wilson ASSERT3P(head, !=, NULL); 655489c86e32SChris Williamson buflist = &dev->l2ad_buflist; 6555dcbf3bd6SGeorge Wilson ASSERT3P(buflist, !=, NULL); 6556fa94a07fSbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 6557fa94a07fSbrendan l2arc_write_callback_t *, cb); 6558fa94a07fSbrendan 6559fa94a07fSbrendan if (zio->io_error != 0) 6560fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 6561fa94a07fSbrendan 6562fa94a07fSbrendan /* 6563fa94a07fSbrendan * All writes completed, or an error was hit. 6564fa94a07fSbrendan */ 6565244781f1SPrakash Surya top: 6566244781f1SPrakash Surya mutex_enter(&dev->l2ad_mtx); 65677adb730bSGeorge Wilson for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { 65687adb730bSGeorge Wilson hdr_prev = list_prev(buflist, hdr); 6569fa94a07fSbrendan 65707adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 6571244781f1SPrakash Surya 6572244781f1SPrakash Surya /* 6573244781f1SPrakash Surya * We cannot use mutex_enter or else we can deadlock 6574244781f1SPrakash Surya * with l2arc_write_buffers (due to swapping the order 6575244781f1SPrakash Surya * the hash lock and l2ad_mtx are taken). 6576244781f1SPrakash Surya */ 6577fa94a07fSbrendan if (!mutex_tryenter(hash_lock)) { 6578fa94a07fSbrendan /* 6579244781f1SPrakash Surya * Missed the hash lock. We must retry so we 6580244781f1SPrakash Surya * don't leave the ARC_FLAG_L2_WRITING bit set. 6581fa94a07fSbrendan */ 6582244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); 6583244781f1SPrakash Surya 6584244781f1SPrakash Surya /* 6585244781f1SPrakash Surya * We don't want to rescan the headers we've 6586244781f1SPrakash Surya * already marked as having been written out, so 6587244781f1SPrakash Surya * we reinsert the head node so we can pick up 6588244781f1SPrakash Surya * where we left off. 6589244781f1SPrakash Surya */ 6590244781f1SPrakash Surya list_remove(buflist, head); 6591244781f1SPrakash Surya list_insert_after(buflist, hdr, head); 6592244781f1SPrakash Surya 6593244781f1SPrakash Surya mutex_exit(&dev->l2ad_mtx); 6594244781f1SPrakash Surya 6595244781f1SPrakash Surya /* 6596244781f1SPrakash Surya * We wait for the hash lock to become available 6597244781f1SPrakash Surya * to try and prevent busy waiting, and increase 6598244781f1SPrakash Surya * the chance we'll be able to acquire the lock 6599244781f1SPrakash Surya * the next time around. 6600244781f1SPrakash Surya */ 6601244781f1SPrakash Surya mutex_enter(hash_lock); 6602244781f1SPrakash Surya mutex_exit(hash_lock); 6603244781f1SPrakash Surya goto top; 6604fa94a07fSbrendan } 6605fa94a07fSbrendan 660689c86e32SChris Williamson /* 6607244781f1SPrakash Surya * We could not have been moved into the arc_l2c_only 6608244781f1SPrakash Surya * state while in-flight due to our ARC_FLAG_L2_WRITING 6609244781f1SPrakash Surya * bit being set. Let's just ensure that's being enforced. 6610244781f1SPrakash Surya */ 6611244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 6612244781f1SPrakash Surya 6613fa94a07fSbrendan if (zio->io_error != 0) { 6614fa94a07fSbrendan /* 66153a737e0dSbrendan * Error - drop L2ARC entry. 6616fa94a07fSbrendan */ 66177adb730bSGeorge Wilson list_remove(buflist, hdr); 6618dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); 661989c86e32SChris Williamson 662016a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_psize, -arc_hdr_size(hdr)); 662116a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_lsize, -HDR_GET_LSIZE(hdr)); 6622a52fc310SPrakash Surya 6623dcbf3bd6SGeorge Wilson bytes_dropped += arc_hdr_size(hdr); 6624a52fc310SPrakash Surya (void) refcount_remove_many(&dev->l2ad_alloc, 6625dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 6626fa94a07fSbrendan } 6627fa94a07fSbrendan 6628fa94a07fSbrendan /* 6629244781f1SPrakash Surya * Allow ARC to begin reads and ghost list evictions to 6630244781f1SPrakash Surya * this L2ARC entry. 6631fa94a07fSbrendan */ 6632dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING); 6633fa94a07fSbrendan 6634fa94a07fSbrendan mutex_exit(hash_lock); 6635fa94a07fSbrendan } 6636fa94a07fSbrendan 6637fa94a07fSbrendan atomic_inc_64(&l2arc_writes_done); 6638fa94a07fSbrendan list_remove(buflist, head); 663989c86e32SChris Williamson ASSERT(!HDR_HAS_L1HDR(head)); 664089c86e32SChris Williamson kmem_cache_free(hdr_l2only_cache, head); 664189c86e32SChris Williamson mutex_exit(&dev->l2ad_mtx); 6642fa94a07fSbrendan 66433038a2b4SSaso Kiselkov vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 66443038a2b4SSaso Kiselkov 66453a737e0dSbrendan l2arc_do_free_on_write(); 6646fa94a07fSbrendan 6647fa94a07fSbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 6648fa94a07fSbrendan } 6649fa94a07fSbrendan 6650fa94a07fSbrendan /* 6651fa94a07fSbrendan * A read to a cache device completed. Validate buffer contents before 6652fa94a07fSbrendan * handing over to the regular ARC routines. 6653fa94a07fSbrendan */ 6654fa94a07fSbrendan static void 6655fa94a07fSbrendan l2arc_read_done(zio_t *zio) 6656fa94a07fSbrendan { 6657fa94a07fSbrendan l2arc_read_callback_t *cb; 6658fa94a07fSbrendan arc_buf_hdr_t *hdr; 6659fa94a07fSbrendan kmutex_t *hash_lock; 6660dcbf3bd6SGeorge Wilson boolean_t valid_cksum; 6661fa94a07fSbrendan 6662dcbf3bd6SGeorge Wilson ASSERT3P(zio->io_vd, !=, NULL); 6663e14bb325SJeff Bonwick ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 6664e14bb325SJeff Bonwick 6665e14bb325SJeff Bonwick spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 6666e14bb325SJeff Bonwick 6667fa94a07fSbrendan cb = zio->io_private; 6668dcbf3bd6SGeorge Wilson ASSERT3P(cb, !=, NULL); 6669dcbf3bd6SGeorge Wilson hdr = cb->l2rcb_hdr; 6670dcbf3bd6SGeorge Wilson ASSERT3P(hdr, !=, NULL); 6671fa94a07fSbrendan 6672dcbf3bd6SGeorge Wilson hash_lock = HDR_LOCK(hdr); 6673fa94a07fSbrendan mutex_enter(hash_lock); 66743f9d6ad7SLin Ling ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 6675fa94a07fSbrendan 6676403a8da7SAndriy Gapon /* 6677403a8da7SAndriy Gapon * If the data was read into a temporary buffer, 6678403a8da7SAndriy Gapon * move it and free the buffer. 6679403a8da7SAndriy Gapon */ 6680403a8da7SAndriy Gapon if (cb->l2rcb_abd != NULL) { 6681403a8da7SAndriy Gapon ASSERT3U(arc_hdr_size(hdr), <, zio->io_size); 6682403a8da7SAndriy Gapon if (zio->io_error == 0) { 6683403a8da7SAndriy Gapon abd_copy(hdr->b_l1hdr.b_pabd, cb->l2rcb_abd, 6684403a8da7SAndriy Gapon arc_hdr_size(hdr)); 6685403a8da7SAndriy Gapon } 6686403a8da7SAndriy Gapon 6687403a8da7SAndriy Gapon /* 6688403a8da7SAndriy Gapon * The following must be done regardless of whether 6689403a8da7SAndriy Gapon * there was an error: 6690403a8da7SAndriy Gapon * - free the temporary buffer 6691403a8da7SAndriy Gapon * - point zio to the real ARC buffer 6692403a8da7SAndriy Gapon * - set zio size accordingly 6693403a8da7SAndriy Gapon * These are required because zio is either re-used for 6694403a8da7SAndriy Gapon * an I/O of the block in the case of the error 6695403a8da7SAndriy Gapon * or the zio is passed to arc_read_done() and it 6696403a8da7SAndriy Gapon * needs real data. 6697403a8da7SAndriy Gapon */ 6698403a8da7SAndriy Gapon abd_free(cb->l2rcb_abd); 6699403a8da7SAndriy Gapon zio->io_size = zio->io_orig_size = arc_hdr_size(hdr); 6700403a8da7SAndriy Gapon zio->io_abd = zio->io_orig_abd = hdr->b_l1hdr.b_pabd; 6701403a8da7SAndriy Gapon } 6702403a8da7SAndriy Gapon 6703770499e1SDan Kimmel ASSERT3P(zio->io_abd, !=, NULL); 6704aad02571SSaso Kiselkov 6705fa94a07fSbrendan /* 6706fa94a07fSbrendan * Check this survived the L2ARC journey. 6707fa94a07fSbrendan */ 6708770499e1SDan Kimmel ASSERT3P(zio->io_abd, ==, hdr->b_l1hdr.b_pabd); 6709dcbf3bd6SGeorge Wilson zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 6710dcbf3bd6SGeorge Wilson zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 6711dcbf3bd6SGeorge Wilson 6712dcbf3bd6SGeorge Wilson valid_cksum = arc_cksum_is_equal(hdr, zio); 6713dcbf3bd6SGeorge Wilson if (valid_cksum && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 6714fa94a07fSbrendan mutex_exit(hash_lock); 6715dcbf3bd6SGeorge Wilson zio->io_private = hdr; 6716fa94a07fSbrendan arc_read_done(zio); 6717fa94a07fSbrendan } else { 6718fa94a07fSbrendan mutex_exit(hash_lock); 6719fa94a07fSbrendan /* 6720fa94a07fSbrendan * Buffer didn't survive caching. Increment stats and 6721fa94a07fSbrendan * reissue to the original storage device. 6722fa94a07fSbrendan */ 67233a737e0dSbrendan if (zio->io_error != 0) { 6724fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 67253a737e0dSbrendan } else { 6726be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(EIO); 67273a737e0dSbrendan } 6728dcbf3bd6SGeorge Wilson if (!valid_cksum) 6729fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 6730fa94a07fSbrendan 6731e14bb325SJeff Bonwick /* 6732e14bb325SJeff Bonwick * If there's no waiter, issue an async i/o to the primary 6733e14bb325SJeff Bonwick * storage now. If there *is* a waiter, the caller must 6734e14bb325SJeff Bonwick * issue the i/o in a context where it's OK to block. 6735e14bb325SJeff Bonwick */ 6736a3f829aeSBill Moore if (zio->io_waiter == NULL) { 6737a3f829aeSBill Moore zio_t *pio = zio_unique_parent(zio); 6738a3f829aeSBill Moore 6739a3f829aeSBill Moore ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 6740a3f829aeSBill Moore 6741dcbf3bd6SGeorge Wilson zio_nowait(zio_read(pio, zio->io_spa, zio->io_bp, 6742770499e1SDan Kimmel hdr->b_l1hdr.b_pabd, zio->io_size, arc_read_done, 6743dcbf3bd6SGeorge Wilson hdr, zio->io_priority, cb->l2rcb_flags, 6744dcbf3bd6SGeorge Wilson &cb->l2rcb_zb)); 6745a3f829aeSBill Moore } 6746fa94a07fSbrendan } 6747fa94a07fSbrendan 6748fa94a07fSbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 6749fa94a07fSbrendan } 6750fa94a07fSbrendan 6751fa94a07fSbrendan /* 6752fa94a07fSbrendan * This is the list priority from which the L2ARC will search for pages to 6753fa94a07fSbrendan * cache. This is used within loops (0..3) to cycle through lists in the 6754fa94a07fSbrendan * desired order. This order can have a significant effect on cache 6755fa94a07fSbrendan * performance. 6756fa94a07fSbrendan * 6757fa94a07fSbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 6758fa94a07fSbrendan * the data lists. This function returns a locked list, and also returns 6759fa94a07fSbrendan * the lock pointer. 6760fa94a07fSbrendan */ 6761244781f1SPrakash Surya static multilist_sublist_t * 6762244781f1SPrakash Surya l2arc_sublist_lock(int list_num) 6763fa94a07fSbrendan { 6764244781f1SPrakash Surya multilist_t *ml = NULL; 6765244781f1SPrakash Surya unsigned int idx; 6766fa94a07fSbrendan 6767fa94a07fSbrendan ASSERT(list_num >= 0 && list_num <= 3); 6768fa94a07fSbrendan 6769fa94a07fSbrendan switch (list_num) { 6770fa94a07fSbrendan case 0: 677194c2d0ebSMatthew Ahrens ml = arc_mfu->arcs_list[ARC_BUFC_METADATA]; 6772fa94a07fSbrendan break; 6773fa94a07fSbrendan case 1: 677494c2d0ebSMatthew Ahrens ml = arc_mru->arcs_list[ARC_BUFC_METADATA]; 6775fa94a07fSbrendan break; 6776fa94a07fSbrendan case 2: 677794c2d0ebSMatthew Ahrens ml = arc_mfu->arcs_list[ARC_BUFC_DATA]; 6778fa94a07fSbrendan break; 6779fa94a07fSbrendan case 3: 678094c2d0ebSMatthew Ahrens ml = arc_mru->arcs_list[ARC_BUFC_DATA]; 6781fa94a07fSbrendan break; 6782fa94a07fSbrendan } 6783fa94a07fSbrendan 6784244781f1SPrakash Surya /* 6785244781f1SPrakash Surya * Return a randomly-selected sublist. This is acceptable 6786244781f1SPrakash Surya * because the caller feeds only a little bit of data for each 6787244781f1SPrakash Surya * call (8MB). Subsequent calls will result in different 6788244781f1SPrakash Surya * sublists being selected. 6789244781f1SPrakash Surya */ 6790244781f1SPrakash Surya idx = multilist_get_random_index(ml); 6791244781f1SPrakash Surya return (multilist_sublist_lock(ml, idx)); 6792fa94a07fSbrendan } 6793fa94a07fSbrendan 6794fa94a07fSbrendan /* 6795fa94a07fSbrendan * Evict buffers from the device write hand to the distance specified in 6796fa94a07fSbrendan * bytes. This distance may span populated buffers, it may span nothing. 6797fa94a07fSbrendan * This is clearing a region on the L2ARC device ready for writing. 6798fa94a07fSbrendan * If the 'all' boolean is set, every buffer is evicted. 6799fa94a07fSbrendan */ 6800fa94a07fSbrendan static void 6801fa94a07fSbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 6802fa94a07fSbrendan { 6803fa94a07fSbrendan list_t *buflist; 68047adb730bSGeorge Wilson arc_buf_hdr_t *hdr, *hdr_prev; 6805fa94a07fSbrendan kmutex_t *hash_lock; 6806fa94a07fSbrendan uint64_t taddr; 6807fa94a07fSbrendan 680889c86e32SChris Williamson buflist = &dev->l2ad_buflist; 6809fa94a07fSbrendan 6810fa94a07fSbrendan if (!all && dev->l2ad_first) { 6811fa94a07fSbrendan /* 6812fa94a07fSbrendan * This is the first sweep through the device. There is 6813fa94a07fSbrendan * nothing to evict. 6814fa94a07fSbrendan */ 6815fa94a07fSbrendan return; 6816fa94a07fSbrendan } 6817fa94a07fSbrendan 68183a737e0dSbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 6819fa94a07fSbrendan /* 6820fa94a07fSbrendan * When nearing the end of the device, evict to the end 6821fa94a07fSbrendan * before the device write hand jumps to the start. 6822fa94a07fSbrendan */ 6823fa94a07fSbrendan taddr = dev->l2ad_end; 6824fa94a07fSbrendan } else { 6825fa94a07fSbrendan taddr = dev->l2ad_hand + distance; 6826fa94a07fSbrendan } 6827fa94a07fSbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 6828fa94a07fSbrendan uint64_t, taddr, boolean_t, all); 6829fa94a07fSbrendan 6830fa94a07fSbrendan top: 683189c86e32SChris Williamson mutex_enter(&dev->l2ad_mtx); 68327adb730bSGeorge Wilson for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { 68337adb730bSGeorge Wilson hdr_prev = list_prev(buflist, hdr); 6834fa94a07fSbrendan 68357adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 6836244781f1SPrakash Surya 6837244781f1SPrakash Surya /* 6838244781f1SPrakash Surya * We cannot use mutex_enter or else we can deadlock 6839244781f1SPrakash Surya * with l2arc_write_buffers (due to swapping the order 6840244781f1SPrakash Surya * the hash lock and l2ad_mtx are taken). 6841244781f1SPrakash Surya */ 6842fa94a07fSbrendan if (!mutex_tryenter(hash_lock)) { 6843fa94a07fSbrendan /* 6844fa94a07fSbrendan * Missed the hash lock. Retry. 6845fa94a07fSbrendan */ 6846fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 684789c86e32SChris Williamson mutex_exit(&dev->l2ad_mtx); 6848fa94a07fSbrendan mutex_enter(hash_lock); 6849fa94a07fSbrendan mutex_exit(hash_lock); 6850fa94a07fSbrendan goto top; 6851fa94a07fSbrendan } 6852fa94a07fSbrendan 6853267ae6c3SAndriy Gapon /* 6854267ae6c3SAndriy Gapon * A header can't be on this list if it doesn't have L2 header. 6855267ae6c3SAndriy Gapon */ 6856267ae6c3SAndriy Gapon ASSERT(HDR_HAS_L2HDR(hdr)); 6857fa94a07fSbrendan 6858267ae6c3SAndriy Gapon /* Ensure this header has finished being written. */ 6859267ae6c3SAndriy Gapon ASSERT(!HDR_L2_WRITING(hdr)); 6860267ae6c3SAndriy Gapon ASSERT(!HDR_L2_WRITE_HEAD(hdr)); 6861267ae6c3SAndriy Gapon 6862267ae6c3SAndriy Gapon if (!all && (hdr->b_l2hdr.b_daddr >= taddr || 686389c86e32SChris Williamson hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { 6864fa94a07fSbrendan /* 6865fa94a07fSbrendan * We've evicted to the target address, 6866fa94a07fSbrendan * or the end of the device. 6867fa94a07fSbrendan */ 6868fa94a07fSbrendan mutex_exit(hash_lock); 6869fa94a07fSbrendan break; 6870fa94a07fSbrendan } 6871fa94a07fSbrendan 687289c86e32SChris Williamson if (!HDR_HAS_L1HDR(hdr)) { 68737adb730bSGeorge Wilson ASSERT(!HDR_L2_READING(hdr)); 6874fa94a07fSbrendan /* 6875fa94a07fSbrendan * This doesn't exist in the ARC. Destroy. 6876fa94a07fSbrendan * arc_hdr_destroy() will call list_remove() 687716a7e5acSAndriy Gapon * and decrement arcstat_l2_lsize. 6878fa94a07fSbrendan */ 68797adb730bSGeorge Wilson arc_change_state(arc_anon, hdr, hash_lock); 68807adb730bSGeorge Wilson arc_hdr_destroy(hdr); 6881fa94a07fSbrendan } else { 688289c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); 688389c86e32SChris Williamson ARCSTAT_BUMP(arcstat_l2_evict_l1cached); 68843a737e0dSbrendan /* 68853a737e0dSbrendan * Invalidate issued or about to be issued 68863a737e0dSbrendan * reads, since we may be about to write 68873a737e0dSbrendan * over this location. 68883a737e0dSbrendan */ 68897adb730bSGeorge Wilson if (HDR_L2_READING(hdr)) { 68903a737e0dSbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 6891dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED); 68923a737e0dSbrendan } 68933a737e0dSbrendan 6894a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(hdr); 6895fa94a07fSbrendan } 6896fa94a07fSbrendan mutex_exit(hash_lock); 6897fa94a07fSbrendan } 689889c86e32SChris Williamson mutex_exit(&dev->l2ad_mtx); 6899fa94a07fSbrendan } 6900fa94a07fSbrendan 6901fa94a07fSbrendan /* 6902fa94a07fSbrendan * Find and write ARC buffers to the L2ARC device. 6903fa94a07fSbrendan * 69047adb730bSGeorge Wilson * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid 6905fa94a07fSbrendan * for reading until they have completed writing. 6906aad02571SSaso Kiselkov * The headroom_boost is an in-out parameter used to maintain headroom boost 6907aad02571SSaso Kiselkov * state between calls to this function. 6908aad02571SSaso Kiselkov * 6909aad02571SSaso Kiselkov * Returns the number of bytes actually written (which may be smaller than 6910aad02571SSaso Kiselkov * the delta by which the device hand has changed due to alignment). 6911fa94a07fSbrendan */ 69125a98e54bSBrendan Gregg - Sun Microsystems static uint64_t 6913dcbf3bd6SGeorge Wilson l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 6914fa94a07fSbrendan { 69157adb730bSGeorge Wilson arc_buf_hdr_t *hdr, *hdr_prev, *head; 691616a7e5acSAndriy Gapon uint64_t write_asize, write_psize, write_lsize, headroom; 6917aad02571SSaso Kiselkov boolean_t full; 6918fa94a07fSbrendan l2arc_write_callback_t *cb; 6919fa94a07fSbrendan zio_t *pio, *wzio; 6920e9103aaeSGarrett D'Amore uint64_t guid = spa_load_guid(spa); 6921fa94a07fSbrendan 6922dcbf3bd6SGeorge Wilson ASSERT3P(dev->l2ad_vdev, !=, NULL); 6923aad02571SSaso Kiselkov 6924fa94a07fSbrendan pio = NULL; 692516a7e5acSAndriy Gapon write_lsize = write_asize = write_psize = 0; 6926fa94a07fSbrendan full = B_FALSE; 692789c86e32SChris Williamson head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); 6928dcbf3bd6SGeorge Wilson arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR); 6929aad02571SSaso Kiselkov 6930fa94a07fSbrendan /* 6931fa94a07fSbrendan * Copy buffers for L2ARC writing. 6932fa94a07fSbrendan */ 6933fa94a07fSbrendan for (int try = 0; try <= 3; try++) { 6934244781f1SPrakash Surya multilist_sublist_t *mls = l2arc_sublist_lock(try); 6935aad02571SSaso Kiselkov uint64_t passed_sz = 0; 6936aad02571SSaso Kiselkov 69373a737e0dSbrendan /* 69383a737e0dSbrendan * L2ARC fast warmup. 69393a737e0dSbrendan * 69403a737e0dSbrendan * Until the ARC is warm and starts to evict, read from the 69413a737e0dSbrendan * head of the ARC lists rather than the tail. 69423a737e0dSbrendan */ 69433a737e0dSbrendan if (arc_warm == B_FALSE) 6944244781f1SPrakash Surya hdr = multilist_sublist_head(mls); 69453a737e0dSbrendan else 6946244781f1SPrakash Surya hdr = multilist_sublist_tail(mls); 69473a737e0dSbrendan 6948aad02571SSaso Kiselkov headroom = target_sz * l2arc_headroom; 6949dcbf3bd6SGeorge Wilson if (zfs_compressed_arc_enabled) 6950aad02571SSaso Kiselkov headroom = (headroom * l2arc_headroom_boost) / 100; 6951aad02571SSaso Kiselkov 69527adb730bSGeorge Wilson for (; hdr; hdr = hdr_prev) { 6953aad02571SSaso Kiselkov kmutex_t *hash_lock; 6954aad02571SSaso Kiselkov 69553a737e0dSbrendan if (arc_warm == B_FALSE) 6956244781f1SPrakash Surya hdr_prev = multilist_sublist_next(mls, hdr); 69573a737e0dSbrendan else 6958244781f1SPrakash Surya hdr_prev = multilist_sublist_prev(mls, hdr); 6959fa94a07fSbrendan 69607adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 6961aad02571SSaso Kiselkov if (!mutex_tryenter(hash_lock)) { 6962fa94a07fSbrendan /* 6963fa94a07fSbrendan * Skip this buffer rather than waiting. 6964fa94a07fSbrendan */ 6965fa94a07fSbrendan continue; 6966fa94a07fSbrendan } 6967fa94a07fSbrendan 6968dcbf3bd6SGeorge Wilson passed_sz += HDR_GET_LSIZE(hdr); 6969fa94a07fSbrendan if (passed_sz > headroom) { 6970fa94a07fSbrendan /* 6971fa94a07fSbrendan * Searched too far. 6972fa94a07fSbrendan */ 6973fa94a07fSbrendan mutex_exit(hash_lock); 6974fa94a07fSbrendan break; 6975fa94a07fSbrendan } 6976fa94a07fSbrendan 69777adb730bSGeorge Wilson if (!l2arc_write_eligible(guid, hdr)) { 6978fa94a07fSbrendan mutex_exit(hash_lock); 6979fa94a07fSbrendan continue; 6980fa94a07fSbrendan } 6981fa94a07fSbrendan 698216a7e5acSAndriy Gapon /* 698316a7e5acSAndriy Gapon * We rely on the L1 portion of the header below, so 698416a7e5acSAndriy Gapon * it's invalid for this header to have been evicted out 698516a7e5acSAndriy Gapon * of the ghost cache, prior to being written out. The 698616a7e5acSAndriy Gapon * ARC_FLAG_L2_WRITING bit ensures this won't happen. 698716a7e5acSAndriy Gapon */ 698816a7e5acSAndriy Gapon ASSERT(HDR_HAS_L1HDR(hdr)); 698916a7e5acSAndriy Gapon 699016a7e5acSAndriy Gapon ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); 699116a7e5acSAndriy Gapon ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 699216a7e5acSAndriy Gapon ASSERT3U(arc_hdr_size(hdr), >, 0); 699316a7e5acSAndriy Gapon uint64_t psize = arc_hdr_size(hdr); 699416a7e5acSAndriy Gapon uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, 699516a7e5acSAndriy Gapon psize); 699616a7e5acSAndriy Gapon 699716a7e5acSAndriy Gapon if ((write_asize + asize) > target_sz) { 6998fa94a07fSbrendan full = B_TRUE; 6999fa94a07fSbrendan mutex_exit(hash_lock); 7000fa94a07fSbrendan break; 7001fa94a07fSbrendan } 7002fa94a07fSbrendan 7003fa94a07fSbrendan if (pio == NULL) { 7004fa94a07fSbrendan /* 7005fa94a07fSbrendan * Insert a dummy header on the buflist so 7006fa94a07fSbrendan * l2arc_write_done() can find where the 7007fa94a07fSbrendan * write buffers begin without searching. 7008fa94a07fSbrendan */ 7009244781f1SPrakash Surya mutex_enter(&dev->l2ad_mtx); 701089c86e32SChris Williamson list_insert_head(&dev->l2ad_buflist, head); 7011244781f1SPrakash Surya mutex_exit(&dev->l2ad_mtx); 7012fa94a07fSbrendan 7013fa94a07fSbrendan cb = kmem_alloc( 7014fa94a07fSbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 7015fa94a07fSbrendan cb->l2wcb_dev = dev; 7016fa94a07fSbrendan cb->l2wcb_head = head; 7017fa94a07fSbrendan pio = zio_root(spa, l2arc_write_done, cb, 7018fa94a07fSbrendan ZIO_FLAG_CANFAIL); 7019fa94a07fSbrendan } 7020fa94a07fSbrendan 702189c86e32SChris Williamson hdr->b_l2hdr.b_dev = dev; 7022dcbf3bd6SGeorge Wilson hdr->b_l2hdr.b_daddr = dev->l2ad_hand; 7023dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, 7024dcbf3bd6SGeorge Wilson ARC_FLAG_L2_WRITING | ARC_FLAG_HAS_L2HDR); 7025dcbf3bd6SGeorge Wilson 7026dcbf3bd6SGeorge Wilson mutex_enter(&dev->l2ad_mtx); 7027dcbf3bd6SGeorge Wilson list_insert_head(&dev->l2ad_buflist, hdr); 7028dcbf3bd6SGeorge Wilson mutex_exit(&dev->l2ad_mtx); 7029dcbf3bd6SGeorge Wilson 703016a7e5acSAndriy Gapon (void) refcount_add_many(&dev->l2ad_alloc, psize, hdr); 7031aad02571SSaso Kiselkov 7032a52fc310SPrakash Surya /* 7033dcbf3bd6SGeorge Wilson * Normally the L2ARC can use the hdr's data, but if 7034dcbf3bd6SGeorge Wilson * we're sharing data between the hdr and one of its 7035dcbf3bd6SGeorge Wilson * bufs, L2ARC needs its own copy of the data so that 7036403a8da7SAndriy Gapon * the ZIO below can't race with the buf consumer. 7037403a8da7SAndriy Gapon * Another case where we need to create a copy of the 7038403a8da7SAndriy Gapon * data is when the buffer size is not device-aligned 7039403a8da7SAndriy Gapon * and we need to pad the block to make it such. 7040403a8da7SAndriy Gapon * That also keeps the clock hand suitably aligned. 7041403a8da7SAndriy Gapon * 7042403a8da7SAndriy Gapon * To ensure that the copy will be available for the 7043dcbf3bd6SGeorge Wilson * lifetime of the ZIO and be cleaned up afterwards, we 7044dcbf3bd6SGeorge Wilson * add it to the l2arc_free_on_write queue. 7045a52fc310SPrakash Surya */ 7046770499e1SDan Kimmel abd_t *to_write; 704716a7e5acSAndriy Gapon if (!HDR_SHARED_DATA(hdr) && psize == asize) { 7048770499e1SDan Kimmel to_write = hdr->b_l1hdr.b_pabd; 7049dcbf3bd6SGeorge Wilson } else { 7050403a8da7SAndriy Gapon to_write = abd_alloc_for_io(asize, 7051770499e1SDan Kimmel HDR_ISTYPE_METADATA(hdr)); 705216a7e5acSAndriy Gapon abd_copy(to_write, hdr->b_l1hdr.b_pabd, psize); 705316a7e5acSAndriy Gapon if (asize != psize) { 705416a7e5acSAndriy Gapon abd_zero_off(to_write, psize, 705516a7e5acSAndriy Gapon asize - psize); 7056403a8da7SAndriy Gapon } 705716a7e5acSAndriy Gapon l2arc_free_abd_on_write(to_write, asize, 7058770499e1SDan Kimmel arc_buf_type(hdr)); 7059dcbf3bd6SGeorge Wilson } 7060dcbf3bd6SGeorge Wilson wzio = zio_write_phys(pio, dev->l2ad_vdev, 7061403a8da7SAndriy Gapon hdr->b_l2hdr.b_daddr, asize, to_write, 7062dcbf3bd6SGeorge Wilson ZIO_CHECKSUM_OFF, NULL, hdr, 7063dcbf3bd6SGeorge Wilson ZIO_PRIORITY_ASYNC_WRITE, 7064dcbf3bd6SGeorge Wilson ZIO_FLAG_CANFAIL, B_FALSE); 7065aad02571SSaso Kiselkov 706616a7e5acSAndriy Gapon write_lsize += HDR_GET_LSIZE(hdr); 7067dcbf3bd6SGeorge Wilson DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 7068dcbf3bd6SGeorge Wilson zio_t *, wzio); 7069fa94a07fSbrendan 707016a7e5acSAndriy Gapon write_psize += psize; 707116a7e5acSAndriy Gapon write_asize += asize; 7072dcbf3bd6SGeorge Wilson dev->l2ad_hand += asize; 7073fa94a07fSbrendan 7074fa94a07fSbrendan mutex_exit(hash_lock); 7075fa94a07fSbrendan 7076dcbf3bd6SGeorge Wilson (void) zio_nowait(wzio); 7077aad02571SSaso Kiselkov } 7078aad02571SSaso Kiselkov 7079244781f1SPrakash Surya multilist_sublist_unlock(mls); 7080aad02571SSaso Kiselkov 7081aad02571SSaso Kiselkov if (full == B_TRUE) 7082aad02571SSaso Kiselkov break; 7083aad02571SSaso Kiselkov } 7084aad02571SSaso Kiselkov 7085aad02571SSaso Kiselkov /* No buffers selected for writing? */ 7086aad02571SSaso Kiselkov if (pio == NULL) { 708716a7e5acSAndriy Gapon ASSERT0(write_lsize); 708889c86e32SChris Williamson ASSERT(!HDR_HAS_L1HDR(head)); 708989c86e32SChris Williamson kmem_cache_free(hdr_l2only_cache, head); 7090aad02571SSaso Kiselkov return (0); 7091aad02571SSaso Kiselkov } 7092aad02571SSaso Kiselkov 7093aad02571SSaso Kiselkov ASSERT3U(write_asize, <=, target_sz); 7094fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 709516a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_write_bytes, write_psize); 709616a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_lsize, write_lsize); 709716a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_psize, write_psize); 709816a7e5acSAndriy Gapon vdev_space_update(dev->l2ad_vdev, write_psize, 0, 0); 7099fa94a07fSbrendan 7100fa94a07fSbrendan /* 7101fa94a07fSbrendan * Bump device hand to the device start if it is approaching the end. 7102fa94a07fSbrendan * l2arc_evict() will already have evicted ahead for this case. 7103fa94a07fSbrendan */ 71043a737e0dSbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 7105fa94a07fSbrendan dev->l2ad_hand = dev->l2ad_start; 7106fa94a07fSbrendan dev->l2ad_first = B_FALSE; 7107fa94a07fSbrendan } 7108fa94a07fSbrendan 71095a98e54bSBrendan Gregg - Sun Microsystems dev->l2ad_writing = B_TRUE; 7110fa94a07fSbrendan (void) zio_wait(pio); 71115a98e54bSBrendan Gregg - Sun Microsystems dev->l2ad_writing = B_FALSE; 71125a98e54bSBrendan Gregg - Sun Microsystems 7113aad02571SSaso Kiselkov return (write_asize); 7114aad02571SSaso Kiselkov } 7115aad02571SSaso Kiselkov 7116fa94a07fSbrendan /* 7117fa94a07fSbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 7118fa94a07fSbrendan * heart of the L2ARC. 7119fa94a07fSbrendan */ 71203f7978d0SAlan Somers /* ARGSUSED */ 7121fa94a07fSbrendan static void 71223f7978d0SAlan Somers l2arc_feed_thread(void *unused) 7123fa94a07fSbrendan { 7124fa94a07fSbrendan callb_cpr_t cpr; 7125fa94a07fSbrendan l2arc_dev_t *dev; 7126fa94a07fSbrendan spa_t *spa; 71275a98e54bSBrendan Gregg - Sun Microsystems uint64_t size, wrote; 7128d3d50737SRafael Vanoni clock_t begin, next = ddi_get_lbolt(); 7129fa94a07fSbrendan 7130fa94a07fSbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 7131fa94a07fSbrendan 7132fa94a07fSbrendan mutex_enter(&l2arc_feed_thr_lock); 7133fa94a07fSbrendan 7134fa94a07fSbrendan while (l2arc_thread_exit == 0) { 7135fa94a07fSbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 7136fa94a07fSbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 71375a98e54bSBrendan Gregg - Sun Microsystems next); 7138fa94a07fSbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 7139d3d50737SRafael Vanoni next = ddi_get_lbolt() + hz; 7140fa94a07fSbrendan 71413a737e0dSbrendan /* 71423a737e0dSbrendan * Quick check for L2ARC devices. 71433a737e0dSbrendan */ 7144c5904d13Seschrock mutex_enter(&l2arc_dev_mtx); 71453a737e0dSbrendan if (l2arc_ndev == 0) { 71463a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 71473a737e0dSbrendan continue; 71483a737e0dSbrendan } 71493a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 7150d3d50737SRafael Vanoni begin = ddi_get_lbolt(); 7151c5904d13Seschrock 7152fa94a07fSbrendan /* 7153c5904d13Seschrock * This selects the next l2arc device to write to, and in 7154c5904d13Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 71553a737e0dSbrendan * will return NULL if there are now no l2arc devices or if 71563a737e0dSbrendan * they are all faulted. 71573a737e0dSbrendan * 71583a737e0dSbrendan * If a device is returned, its spa's config lock is also 71593a737e0dSbrendan * held to prevent device removal. l2arc_dev_get_next() 71603a737e0dSbrendan * will grab and release l2arc_dev_mtx. 7161fa94a07fSbrendan */ 71623a737e0dSbrendan if ((dev = l2arc_dev_get_next()) == NULL) 7163fa94a07fSbrendan continue; 71643a737e0dSbrendan 71653a737e0dSbrendan spa = dev->l2ad_spa; 7166dcbf3bd6SGeorge Wilson ASSERT3P(spa, !=, NULL); 7167fa94a07fSbrendan 7168f9af39baSGeorge Wilson /* 7169f9af39baSGeorge Wilson * If the pool is read-only then force the feed thread to 7170f9af39baSGeorge Wilson * sleep a little longer. 7171f9af39baSGeorge Wilson */ 7172f9af39baSGeorge Wilson if (!spa_writeable(spa)) { 7173f9af39baSGeorge Wilson next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 7174f9af39baSGeorge Wilson spa_config_exit(spa, SCL_L2ARC, dev); 7175f9af39baSGeorge Wilson continue; 7176f9af39baSGeorge Wilson } 7177f9af39baSGeorge Wilson 7178fa94a07fSbrendan /* 7179fa94a07fSbrendan * Avoid contributing to memory pressure. 7180fa94a07fSbrendan */ 7181fa94a07fSbrendan if (arc_reclaim_needed()) { 7182fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 7183e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, dev); 7184fa94a07fSbrendan continue; 7185fa94a07fSbrendan } 7186fa94a07fSbrendan 7187fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 7188fa94a07fSbrendan 7189aad02571SSaso Kiselkov size = l2arc_write_size(); 71903a737e0dSbrendan 7191fa94a07fSbrendan /* 7192fa94a07fSbrendan * Evict L2ARC buffers that will be overwritten. 7193fa94a07fSbrendan */ 71943a737e0dSbrendan l2arc_evict(dev, size, B_FALSE); 7195fa94a07fSbrendan 7196fa94a07fSbrendan /* 7197fa94a07fSbrendan * Write ARC buffers. 7198fa94a07fSbrendan */ 7199dcbf3bd6SGeorge Wilson wrote = l2arc_write_buffers(spa, dev, size); 72005a98e54bSBrendan Gregg - Sun Microsystems 72015a98e54bSBrendan Gregg - Sun Microsystems /* 72025a98e54bSBrendan Gregg - Sun Microsystems * Calculate interval between writes. 72035a98e54bSBrendan Gregg - Sun Microsystems */ 72045a98e54bSBrendan Gregg - Sun Microsystems next = l2arc_write_interval(begin, size, wrote); 7205e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, dev); 7206fa94a07fSbrendan } 7207fa94a07fSbrendan 7208fa94a07fSbrendan l2arc_thread_exit = 0; 7209fa94a07fSbrendan cv_broadcast(&l2arc_feed_thr_cv); 7210fa94a07fSbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 7211fa94a07fSbrendan thread_exit(); 7212fa94a07fSbrendan } 7213fa94a07fSbrendan 7214c5904d13Seschrock boolean_t 7215c5904d13Seschrock l2arc_vdev_present(vdev_t *vd) 7216c5904d13Seschrock { 7217c5904d13Seschrock l2arc_dev_t *dev; 7218c5904d13Seschrock 7219c5904d13Seschrock mutex_enter(&l2arc_dev_mtx); 7220c5904d13Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 7221c5904d13Seschrock dev = list_next(l2arc_dev_list, dev)) { 7222c5904d13Seschrock if (dev->l2ad_vdev == vd) 7223c5904d13Seschrock break; 7224c5904d13Seschrock } 7225c5904d13Seschrock mutex_exit(&l2arc_dev_mtx); 7226c5904d13Seschrock 7227c5904d13Seschrock return (dev != NULL); 7228c5904d13Seschrock } 7229c5904d13Seschrock 7230fa94a07fSbrendan /* 7231fa94a07fSbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 7232fa94a07fSbrendan * validated the vdev and opened it. 7233fa94a07fSbrendan */ 7234fa94a07fSbrendan void 7235573ca77eSGeorge Wilson l2arc_add_vdev(spa_t *spa, vdev_t *vd) 7236fa94a07fSbrendan { 7237fa94a07fSbrendan l2arc_dev_t *adddev; 7238fa94a07fSbrendan 7239c5904d13Seschrock ASSERT(!l2arc_vdev_present(vd)); 7240c5904d13Seschrock 7241fa94a07fSbrendan /* 7242fa94a07fSbrendan * Create a new l2arc device entry. 7243fa94a07fSbrendan */ 7244fa94a07fSbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 7245fa94a07fSbrendan adddev->l2ad_spa = spa; 7246fa94a07fSbrendan adddev->l2ad_vdev = vd; 7247573ca77eSGeorge Wilson adddev->l2ad_start = VDEV_LABEL_START_SIZE; 7248573ca77eSGeorge Wilson adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 7249fa94a07fSbrendan adddev->l2ad_hand = adddev->l2ad_start; 7250fa94a07fSbrendan adddev->l2ad_first = B_TRUE; 72515a98e54bSBrendan Gregg - Sun Microsystems adddev->l2ad_writing = B_FALSE; 7252fa94a07fSbrendan 725389c86e32SChris Williamson mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); 7254fa94a07fSbrendan /* 7255fa94a07fSbrendan * This is a list of all ARC buffers that are still valid on the 7256fa94a07fSbrendan * device. 7257fa94a07fSbrendan */ 725889c86e32SChris Williamson list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 725989c86e32SChris Williamson offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); 7260fa94a07fSbrendan 7261b24ab676SJeff Bonwick vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 7262a52fc310SPrakash Surya refcount_create(&adddev->l2ad_alloc); 7263fa94a07fSbrendan 7264fa94a07fSbrendan /* 7265fa94a07fSbrendan * Add device to global list 7266fa94a07fSbrendan */ 7267fa94a07fSbrendan mutex_enter(&l2arc_dev_mtx); 7268fa94a07fSbrendan list_insert_head(l2arc_dev_list, adddev); 7269fa94a07fSbrendan atomic_inc_64(&l2arc_ndev); 7270fa94a07fSbrendan mutex_exit(&l2arc_dev_mtx); 7271fa94a07fSbrendan } 7272fa94a07fSbrendan 7273fa94a07fSbrendan /* 7274fa94a07fSbrendan * Remove a vdev from the L2ARC. 7275fa94a07fSbrendan */ 7276fa94a07fSbrendan void 7277fa94a07fSbrendan l2arc_remove_vdev(vdev_t *vd) 7278fa94a07fSbrendan { 7279fa94a07fSbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 7280fa94a07fSbrendan 7281fa94a07fSbrendan /* 7282fa94a07fSbrendan * Find the device by vdev 7283fa94a07fSbrendan */ 7284fa94a07fSbrendan mutex_enter(&l2arc_dev_mtx); 7285fa94a07fSbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 7286fa94a07fSbrendan nextdev = list_next(l2arc_dev_list, dev); 7287fa94a07fSbrendan if (vd == dev->l2ad_vdev) { 7288fa94a07fSbrendan remdev = dev; 7289fa94a07fSbrendan break; 7290fa94a07fSbrendan } 7291fa94a07fSbrendan } 7292dcbf3bd6SGeorge Wilson ASSERT3P(remdev, !=, NULL); 7293fa94a07fSbrendan 7294fa94a07fSbrendan /* 7295fa94a07fSbrendan * Remove device from global list 7296fa94a07fSbrendan */ 7297fa94a07fSbrendan list_remove(l2arc_dev_list, remdev); 7298fa94a07fSbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 72993a737e0dSbrendan atomic_dec_64(&l2arc_ndev); 73003a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 7301fa94a07fSbrendan 7302fa94a07fSbrendan /* 7303fa94a07fSbrendan * Clear all buflists and ARC references. L2ARC device flush. 7304fa94a07fSbrendan */ 7305fa94a07fSbrendan l2arc_evict(remdev, 0, B_TRUE); 730689c86e32SChris Williamson list_destroy(&remdev->l2ad_buflist); 730789c86e32SChris Williamson mutex_destroy(&remdev->l2ad_mtx); 7308a52fc310SPrakash Surya refcount_destroy(&remdev->l2ad_alloc); 7309fa94a07fSbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 7310fa94a07fSbrendan } 7311fa94a07fSbrendan 7312fa94a07fSbrendan void 7313e14bb325SJeff Bonwick l2arc_init(void) 7314fa94a07fSbrendan { 7315fa94a07fSbrendan l2arc_thread_exit = 0; 7316fa94a07fSbrendan l2arc_ndev = 0; 7317fa94a07fSbrendan l2arc_writes_sent = 0; 7318fa94a07fSbrendan l2arc_writes_done = 0; 7319fa94a07fSbrendan 7320fa94a07fSbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 7321fa94a07fSbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 7322fa94a07fSbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 7323fa94a07fSbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 7324fa94a07fSbrendan 7325fa94a07fSbrendan l2arc_dev_list = &L2ARC_dev_list; 7326fa94a07fSbrendan l2arc_free_on_write = &L2ARC_free_on_write; 7327fa94a07fSbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 7328fa94a07fSbrendan offsetof(l2arc_dev_t, l2ad_node)); 7329fa94a07fSbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 7330fa94a07fSbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 7331fa94a07fSbrendan } 7332fa94a07fSbrendan 7333fa94a07fSbrendan void 7334e14bb325SJeff Bonwick l2arc_fini(void) 7335fa94a07fSbrendan { 73363a737e0dSbrendan /* 73373a737e0dSbrendan * This is called from dmu_fini(), which is called from spa_fini(); 73383a737e0dSbrendan * Because of this, we can assume that all l2arc devices have 73393a737e0dSbrendan * already been removed when the pools themselves were removed. 73403a737e0dSbrendan */ 73413a737e0dSbrendan 73423a737e0dSbrendan l2arc_do_free_on_write(); 73433a737e0dSbrendan 7344fa94a07fSbrendan mutex_destroy(&l2arc_feed_thr_lock); 7345fa94a07fSbrendan cv_destroy(&l2arc_feed_thr_cv); 7346fa94a07fSbrendan mutex_destroy(&l2arc_dev_mtx); 7347fa94a07fSbrendan mutex_destroy(&l2arc_free_on_write_mtx); 7348fa94a07fSbrendan 7349fa94a07fSbrendan list_destroy(l2arc_dev_list); 7350fa94a07fSbrendan list_destroy(l2arc_free_on_write); 7351fa94a07fSbrendan } 7352e14bb325SJeff Bonwick 7353e14bb325SJeff Bonwick void 7354e14bb325SJeff Bonwick l2arc_start(void) 7355e14bb325SJeff Bonwick { 73568ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 7357e14bb325SJeff Bonwick return; 7358e14bb325SJeff Bonwick 7359e14bb325SJeff Bonwick (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 7360e14bb325SJeff Bonwick TS_RUN, minclsyspri); 7361e14bb325SJeff Bonwick } 7362e14bb325SJeff Bonwick 7363e14bb325SJeff Bonwick void 7364e14bb325SJeff Bonwick l2arc_stop(void) 7365e14bb325SJeff Bonwick { 73668ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 7367e14bb325SJeff Bonwick return; 7368e14bb325SJeff Bonwick 7369e14bb325SJeff Bonwick mutex_enter(&l2arc_feed_thr_lock); 7370e14bb325SJeff Bonwick cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 7371e14bb325SJeff Bonwick l2arc_thread_exit = 1; 7372e14bb325SJeff Bonwick while (l2arc_thread_exit != 0) 7373e14bb325SJeff Bonwick cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 7374e14bb325SJeff Bonwick mutex_exit(&l2arc_feed_thr_lock); 7375e14bb325SJeff Bonwick } 7376