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. 24fa98e487SMatthew Ahrens * Copyright (c) 2011, 2018 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> 277de753e34SBrad Lewis #include <sys/zthr.h> 278b24ab676SJeff Bonwick #include <zfs_fletcher.h> 2793a2d8a1bSPaul Dagnelie #include <sys/aggsum.h> 2803a2d8a1bSPaul Dagnelie #include <sys/cityhash.h> 281fa9e4066Sahrens 282cd1c8b85SMatthew Ahrens #ifndef _KERNEL 283cd1c8b85SMatthew Ahrens /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ 284cd1c8b85SMatthew Ahrens boolean_t arc_watch = B_FALSE; 285cd1c8b85SMatthew Ahrens int arc_procfd; 286cd1c8b85SMatthew Ahrens #endif 287cd1c8b85SMatthew Ahrens 288de753e34SBrad Lewis /* 289de753e34SBrad Lewis * This thread's job is to keep enough free memory in the system, by 290de753e34SBrad Lewis * calling arc_kmem_reap_now() plus arc_shrink(), which improves 291de753e34SBrad Lewis * arc_available_memory(). 292de753e34SBrad Lewis */ 293de753e34SBrad Lewis static zthr_t *arc_reap_zthr; 294de753e34SBrad Lewis 295de753e34SBrad Lewis /* 296de753e34SBrad Lewis * This thread's job is to keep arc_size under arc_c, by calling 297de753e34SBrad Lewis * arc_adjust(), which improves arc_is_overflowing(). 298de753e34SBrad Lewis */ 299de753e34SBrad Lewis static zthr_t *arc_adjust_zthr; 300de753e34SBrad Lewis 301de753e34SBrad Lewis static kmutex_t arc_adjust_lock; 302de753e34SBrad Lewis static kcondvar_t arc_adjust_waiters_cv; 303de753e34SBrad Lewis static boolean_t arc_adjust_needed = B_FALSE; 304244781f1SPrakash Surya 3052ec99e3eSMatthew Ahrens uint_t arc_reduce_dnlc_percent = 3; 306fa9e4066Sahrens 30769962b56SMatthew Ahrens /* 308244781f1SPrakash Surya * The number of headers to evict in arc_evict_state_impl() before 309244781f1SPrakash Surya * dropping the sublist lock and evicting from another sublist. A lower 310244781f1SPrakash Surya * value means we're more likely to evict the "correct" header (i.e. the 311244781f1SPrakash Surya * oldest header in the arc state), but comes with higher overhead 312244781f1SPrakash Surya * (i.e. more invocations of arc_evict_state_impl()). 313244781f1SPrakash Surya */ 314244781f1SPrakash Surya int zfs_arc_evict_batch_limit = 10; 315244781f1SPrakash Surya 316fa9e4066Sahrens /* number of seconds before growing cache again */ 317de753e34SBrad Lewis int arc_grow_retry = 60; 318fa9e4066Sahrens 319de753e34SBrad Lewis /* 320de753e34SBrad Lewis * Minimum time between calls to arc_kmem_reap_soon(). Note that this will 321de753e34SBrad Lewis * be converted to ticks, so with the default hz=100, a setting of 15 ms 322de753e34SBrad Lewis * will actually wait 2 ticks, or 20ms. 323de753e34SBrad Lewis */ 324de753e34SBrad Lewis int arc_kmem_cache_reap_retry_ms = 1000; 32536a64e62STim Kordas 326770499e1SDan Kimmel /* shift of arc_c for calculating overflow limit in arc_get_data_impl */ 327de753e34SBrad Lewis int zfs_arc_overflow_shift = 8; 328244781f1SPrakash Surya 3295a98e54bSBrendan Gregg - Sun Microsystems /* shift of arc_c for calculating both min and max arc_p */ 330de753e34SBrad Lewis int arc_p_min_shift = 4; 3315a98e54bSBrendan Gregg - Sun Microsystems 3325a98e54bSBrendan Gregg - Sun Microsystems /* log2(fraction of arc to reclaim) */ 333de753e34SBrad Lewis int arc_shrink_shift = 7; 3342ec99e3eSMatthew Ahrens 3352ec99e3eSMatthew Ahrens /* 3362ec99e3eSMatthew Ahrens * log2(fraction of ARC which must be free to allow growing). 3372ec99e3eSMatthew Ahrens * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, 3382ec99e3eSMatthew Ahrens * when reading a new block into the ARC, we will evict an equal-sized block 3392ec99e3eSMatthew Ahrens * from the ARC. 3402ec99e3eSMatthew Ahrens * 3412ec99e3eSMatthew Ahrens * This must be less than arc_shrink_shift, so that when we shrink the ARC, 3422ec99e3eSMatthew Ahrens * we will still not allow it to grow. 3432ec99e3eSMatthew Ahrens */ 3442ec99e3eSMatthew Ahrens int arc_no_grow_shift = 5; 3452ec99e3eSMatthew Ahrens 3465a98e54bSBrendan Gregg - Sun Microsystems 34713506d1eSmaybee /* 348b19a79ecSperrin * minimum lifespan of a prefetch block in clock ticks 349b19a79ecSperrin * (initialized in arc_init()) 35013506d1eSmaybee */ 351*a3874b8bSToomas Soome static int zfs_arc_min_prefetch_ms = 1; 352*a3874b8bSToomas Soome static int zfs_arc_min_prescient_prefetch_ms = 6; 35313506d1eSmaybee 35469962b56SMatthew Ahrens /* 35569962b56SMatthew Ahrens * If this percent of memory is free, don't throttle. 35669962b56SMatthew Ahrens */ 35769962b56SMatthew Ahrens int arc_lotsfree_percent = 10; 35869962b56SMatthew Ahrens 359de753e34SBrad Lewis static boolean_t arc_initialized; 360fa9e4066Sahrens 3613a737e0dSbrendan /* 3623a737e0dSbrendan * The arc has filled available memory and has now warmed up. 3633a737e0dSbrendan */ 3643a737e0dSbrendan static boolean_t arc_warm; 3653a737e0dSbrendan 3660dd053d7SPrakash Surya /* 3670dd053d7SPrakash Surya * log2 fraction of the zio arena to keep free. 3680dd053d7SPrakash Surya */ 3690dd053d7SPrakash Surya int arc_zio_arena_free_shift = 2; 3700dd053d7SPrakash Surya 371a2eea2e1Sahrens /* 372a2eea2e1Sahrens * These tunables are for performance analysis. 373a2eea2e1Sahrens */ 374a2eea2e1Sahrens uint64_t zfs_arc_max; 375a2eea2e1Sahrens uint64_t zfs_arc_min; 3761116048bSek uint64_t zfs_arc_meta_limit = 0; 3773a5286a1SMatthew Ahrens uint64_t zfs_arc_meta_min = 0; 3785a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_grow_retry = 0; 3795a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_shrink_shift = 0; 3805a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_p_min_shift = 0; 38163e911b6SMatthew Ahrens int zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ 382a2eea2e1Sahrens 383abe1fd01SDon Brady /* 384abe1fd01SDon Brady * ARC dirty data constraints for arc_tempreserve_space() throttle 385abe1fd01SDon Brady */ 386abe1fd01SDon Brady uint_t zfs_arc_dirty_limit_percent = 50; /* total dirty data limit */ 387abe1fd01SDon Brady uint_t zfs_arc_anon_limit_percent = 25; /* anon block dirty limit */ 388abe1fd01SDon Brady uint_t zfs_arc_pool_dirty_percent = 20; /* each pool's anon allowance */ 389abe1fd01SDon Brady 390dcbf3bd6SGeorge Wilson boolean_t zfs_compressed_arc_enabled = B_TRUE; 391dcbf3bd6SGeorge Wilson 392fa9e4066Sahrens /* 393fa94a07fSbrendan * Note that buffers can be in one of 6 states: 394fa9e4066Sahrens * ARC_anon - anonymous (discussed below) 395ea8dc4b6Seschrock * ARC_mru - recently used, currently cached 396ea8dc4b6Seschrock * ARC_mru_ghost - recentely used, no longer in cache 397ea8dc4b6Seschrock * ARC_mfu - frequently used, currently cached 398ea8dc4b6Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 399fa94a07fSbrendan * ARC_l2c_only - exists in L2ARC but not other states 4000e8c6158Smaybee * When there are no active references to the buffer, they are 4010e8c6158Smaybee * are linked onto a list in one of these arc states. These are 4020e8c6158Smaybee * the only buffers that can be evicted or deleted. Within each 4030e8c6158Smaybee * state there are multiple lists, one for meta-data and one for 4040e8c6158Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 4050e8c6158Smaybee * etc.) is tracked separately so that it can be managed more 406fa94a07fSbrendan * explicitly: favored over data, limited explicitly. 407fa9e4066Sahrens * 408fa9e4066Sahrens * Anonymous buffers are buffers that are not associated with 409fa9e4066Sahrens * a DVA. These are buffers that hold dirty block copies 410fa9e4066Sahrens * before they are written to stable storage. By definition, 411ea8dc4b6Seschrock * they are "ref'd" and are considered part of arc_mru 412fa9e4066Sahrens * that cannot be freed. Generally, they will aquire a DVA 413ea8dc4b6Seschrock * as they are written and migrate onto the arc_mru list. 414fa94a07fSbrendan * 415fa94a07fSbrendan * The ARC_l2c_only state is for buffers that are in the second 416fa94a07fSbrendan * level ARC but no longer in any of the ARC_m* lists. The second 417fa94a07fSbrendan * level ARC itself may also contain buffers that are in any of 418fa94a07fSbrendan * the ARC_m* states - meaning that a buffer can exist in two 419fa94a07fSbrendan * places. The reason for the ARC_l2c_only state is to keep the 420fa94a07fSbrendan * buffer header in the hash table, so that reads that hit the 421fa94a07fSbrendan * second level ARC benefit from these fast lookups. 422fa9e4066Sahrens */ 423fa9e4066Sahrens 424fa9e4066Sahrens typedef struct arc_state { 425244781f1SPrakash Surya /* 426244781f1SPrakash Surya * list of evictable buffers 427244781f1SPrakash Surya */ 42894c2d0ebSMatthew Ahrens multilist_t *arcs_list[ARC_BUFC_NUMTYPES]; 429244781f1SPrakash Surya /* 430244781f1SPrakash Surya * total amount of evictable data in this state 431244781f1SPrakash Surya */ 432e914ace2STim Schumacher zfs_refcount_t arcs_esize[ARC_BUFC_NUMTYPES]; 433244781f1SPrakash Surya /* 434244781f1SPrakash Surya * total amount of data in this state; this includes: evictable, 435244781f1SPrakash Surya * non-evictable, ARC_BUFC_DATA, and ARC_BUFC_METADATA. 436244781f1SPrakash Surya */ 437e914ace2STim Schumacher zfs_refcount_t arcs_size; 438fa9e4066Sahrens } arc_state_t; 439fa9e4066Sahrens 440fa94a07fSbrendan /* The 6 states: */ 441fa9e4066Sahrens static arc_state_t ARC_anon; 442ea8dc4b6Seschrock static arc_state_t ARC_mru; 443ea8dc4b6Seschrock static arc_state_t ARC_mru_ghost; 444ea8dc4b6Seschrock static arc_state_t ARC_mfu; 445ea8dc4b6Seschrock static arc_state_t ARC_mfu_ghost; 446fa94a07fSbrendan static arc_state_t ARC_l2c_only; 447fa9e4066Sahrens 44844cb6abcSbmc typedef struct arc_stats { 44944cb6abcSbmc kstat_named_t arcstat_hits; 45044cb6abcSbmc kstat_named_t arcstat_misses; 45144cb6abcSbmc kstat_named_t arcstat_demand_data_hits; 45244cb6abcSbmc kstat_named_t arcstat_demand_data_misses; 45344cb6abcSbmc kstat_named_t arcstat_demand_metadata_hits; 45444cb6abcSbmc kstat_named_t arcstat_demand_metadata_misses; 45544cb6abcSbmc kstat_named_t arcstat_prefetch_data_hits; 45644cb6abcSbmc kstat_named_t arcstat_prefetch_data_misses; 45744cb6abcSbmc kstat_named_t arcstat_prefetch_metadata_hits; 45844cb6abcSbmc kstat_named_t arcstat_prefetch_metadata_misses; 45944cb6abcSbmc kstat_named_t arcstat_mru_hits; 46044cb6abcSbmc kstat_named_t arcstat_mru_ghost_hits; 46144cb6abcSbmc kstat_named_t arcstat_mfu_hits; 46244cb6abcSbmc kstat_named_t arcstat_mfu_ghost_hits; 46344cb6abcSbmc kstat_named_t arcstat_deleted; 4643e30c24aSWill Andrews /* 4653e30c24aSWill Andrews * Number of buffers that could not be evicted because the hash lock 4663e30c24aSWill Andrews * was held by another thread. The lock may not necessarily be held 4673e30c24aSWill Andrews * by something using the same buffer, since hash locks are shared 4683e30c24aSWill Andrews * by multiple buffers. 4693e30c24aSWill Andrews */ 47044cb6abcSbmc kstat_named_t arcstat_mutex_miss; 4717b38fab6SAlexander Motin /* 4727b38fab6SAlexander Motin * Number of buffers skipped when updating the access state due to the 4737b38fab6SAlexander Motin * header having already been released after acquiring the hash lock. 4747b38fab6SAlexander Motin */ 4757b38fab6SAlexander Motin kstat_named_t arcstat_access_skip; 4763e30c24aSWill Andrews /* 4773e30c24aSWill Andrews * Number of buffers skipped because they have I/O in progress, are 4787b38fab6SAlexander Motin * indirect prefetch buffers that have not lived long enough, or are 4793e30c24aSWill Andrews * not from the spa we're trying to evict from. 4803e30c24aSWill Andrews */ 48144cb6abcSbmc kstat_named_t arcstat_evict_skip; 482244781f1SPrakash Surya /* 483244781f1SPrakash Surya * Number of times arc_evict_state() was unable to evict enough 484244781f1SPrakash Surya * buffers to reach it's target amount. 485244781f1SPrakash Surya */ 486244781f1SPrakash Surya kstat_named_t arcstat_evict_not_enough; 4875ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_cached; 4885ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_eligible; 4895ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_ineligible; 490244781f1SPrakash Surya kstat_named_t arcstat_evict_l2_skip; 49144cb6abcSbmc kstat_named_t arcstat_hash_elements; 49244cb6abcSbmc kstat_named_t arcstat_hash_elements_max; 49344cb6abcSbmc kstat_named_t arcstat_hash_collisions; 49444cb6abcSbmc kstat_named_t arcstat_hash_chains; 49544cb6abcSbmc kstat_named_t arcstat_hash_chain_max; 49644cb6abcSbmc kstat_named_t arcstat_p; 49744cb6abcSbmc kstat_named_t arcstat_c; 49844cb6abcSbmc kstat_named_t arcstat_c_min; 49944cb6abcSbmc kstat_named_t arcstat_c_max; 5003a2d8a1bSPaul Dagnelie /* Not updated directly; only synced in arc_kstat_update. */ 50144cb6abcSbmc kstat_named_t arcstat_size; 502dcbf3bd6SGeorge Wilson /* 503770499e1SDan Kimmel * Number of compressed bytes stored in the arc_buf_hdr_t's b_pabd. 504dcbf3bd6SGeorge Wilson * Note that the compressed bytes may match the uncompressed bytes 505dcbf3bd6SGeorge Wilson * if the block is either not compressed or compressed arc is disabled. 506dcbf3bd6SGeorge Wilson */ 507dcbf3bd6SGeorge Wilson kstat_named_t arcstat_compressed_size; 508dcbf3bd6SGeorge Wilson /* 509770499e1SDan Kimmel * Uncompressed size of the data stored in b_pabd. If compressed 510dcbf3bd6SGeorge Wilson * arc is disabled then this value will be identical to the stat 511dcbf3bd6SGeorge Wilson * above. 512dcbf3bd6SGeorge Wilson */ 513dcbf3bd6SGeorge Wilson kstat_named_t arcstat_uncompressed_size; 514dcbf3bd6SGeorge Wilson /* 515dcbf3bd6SGeorge Wilson * Number of bytes stored in all the arc_buf_t's. This is classified 516dcbf3bd6SGeorge Wilson * as "overhead" since this data is typically short-lived and will 517dcbf3bd6SGeorge Wilson * be evicted from the arc when it becomes unreferenced unless the 518dcbf3bd6SGeorge Wilson * zfs_keep_uncompressed_metadata or zfs_keep_uncompressed_level 519dcbf3bd6SGeorge Wilson * values have been set (see comment in dbuf.c for more information). 520dcbf3bd6SGeorge Wilson */ 521dcbf3bd6SGeorge Wilson kstat_named_t arcstat_overhead_size; 5224076b1bfSPrakash Surya /* 5234076b1bfSPrakash Surya * Number of bytes consumed by internal ARC structures necessary 5244076b1bfSPrakash Surya * for tracking purposes; these structures are not actually 5254076b1bfSPrakash Surya * backed by ARC buffers. This includes arc_buf_hdr_t structures 5264076b1bfSPrakash Surya * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only 5274076b1bfSPrakash Surya * caches), and arc_buf_t structures (allocated via arc_buf_t 5284076b1bfSPrakash Surya * cache). 5293a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5304076b1bfSPrakash Surya */ 531fa94a07fSbrendan kstat_named_t arcstat_hdr_size; 5324076b1bfSPrakash Surya /* 5334076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers of type equal to 5344076b1bfSPrakash Surya * ARC_BUFC_DATA. This is generally consumed by buffers backing 5354076b1bfSPrakash Surya * on disk user data (e.g. plain file contents). 5363a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5374076b1bfSPrakash Surya */ 5385a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_data_size; 5394076b1bfSPrakash Surya /* 5404076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers of type equal to 5414076b1bfSPrakash Surya * ARC_BUFC_METADATA. This is generally consumed by buffers 5424076b1bfSPrakash Surya * backing on disk data that is used for internal ZFS 5434076b1bfSPrakash Surya * structures (e.g. ZAP, dnode, indirect blocks, etc). 5443a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5454076b1bfSPrakash Surya */ 5464076b1bfSPrakash Surya kstat_named_t arcstat_metadata_size; 5474076b1bfSPrakash Surya /* 5484076b1bfSPrakash Surya * Number of bytes consumed by various buffers and structures 5494076b1bfSPrakash Surya * not actually backed with ARC buffers. This includes bonus 5504076b1bfSPrakash Surya * buffers (allocated directly via zio_buf_* functions), 5514076b1bfSPrakash Surya * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t 5524076b1bfSPrakash Surya * cache), and dnode_t structures (allocated via dnode_t cache). 5533a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5544076b1bfSPrakash Surya */ 5555a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_other_size; 5564076b1bfSPrakash Surya /* 5574076b1bfSPrakash Surya * Total number of bytes consumed by ARC buffers residing in the 5584076b1bfSPrakash Surya * arc_anon state. This includes *all* buffers in the arc_anon 5594076b1bfSPrakash Surya * state; e.g. data, metadata, evictable, and unevictable buffers 5604076b1bfSPrakash Surya * are all included in this value. 5613a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5624076b1bfSPrakash Surya */ 5634076b1bfSPrakash Surya kstat_named_t arcstat_anon_size; 5644076b1bfSPrakash Surya /* 5654076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5664076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_DATA, 5674076b1bfSPrakash Surya * residing in the arc_anon state, and are eligible for eviction 5684076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 5693a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5704076b1bfSPrakash Surya */ 5714076b1bfSPrakash Surya kstat_named_t arcstat_anon_evictable_data; 5724076b1bfSPrakash Surya /* 5734076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5744076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_METADATA, 5754076b1bfSPrakash Surya * residing in the arc_anon state, and are eligible for eviction 5764076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 5773a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5784076b1bfSPrakash Surya */ 5794076b1bfSPrakash Surya kstat_named_t arcstat_anon_evictable_metadata; 5804076b1bfSPrakash Surya /* 5814076b1bfSPrakash Surya * Total number of bytes consumed by ARC buffers residing in the 5824076b1bfSPrakash Surya * arc_mru state. This includes *all* buffers in the arc_mru 5834076b1bfSPrakash Surya * state; e.g. data, metadata, evictable, and unevictable buffers 5844076b1bfSPrakash Surya * are all included in this value. 5853a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5864076b1bfSPrakash Surya */ 5874076b1bfSPrakash Surya kstat_named_t arcstat_mru_size; 5884076b1bfSPrakash Surya /* 5894076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5904076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_DATA, 5914076b1bfSPrakash Surya * residing in the arc_mru state, and are eligible for eviction 5924076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 5933a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 5944076b1bfSPrakash Surya */ 5954076b1bfSPrakash Surya kstat_named_t arcstat_mru_evictable_data; 5964076b1bfSPrakash Surya /* 5974076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5984076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_METADATA, 5994076b1bfSPrakash Surya * residing in the arc_mru state, and are eligible for eviction 6004076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 6013a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6024076b1bfSPrakash Surya */ 6034076b1bfSPrakash Surya kstat_named_t arcstat_mru_evictable_metadata; 6044076b1bfSPrakash Surya /* 6054076b1bfSPrakash Surya * Total number of bytes that *would have been* consumed by ARC 6064076b1bfSPrakash Surya * buffers in the arc_mru_ghost state. The key thing to note 6074076b1bfSPrakash Surya * here, is the fact that this size doesn't actually indicate 6084076b1bfSPrakash Surya * RAM consumption. The ghost lists only consist of headers and 6094076b1bfSPrakash Surya * don't actually have ARC buffers linked off of these headers. 6104076b1bfSPrakash Surya * Thus, *if* the headers had associated ARC buffers, these 6114076b1bfSPrakash Surya * buffers *would have* consumed this number of bytes. 6123a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6134076b1bfSPrakash Surya */ 6144076b1bfSPrakash Surya kstat_named_t arcstat_mru_ghost_size; 6154076b1bfSPrakash Surya /* 6164076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 6174076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 6184076b1bfSPrakash Surya * ARC_BUFC_DATA, and linked off the arc_mru_ghost state. 6193a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6204076b1bfSPrakash Surya */ 6214076b1bfSPrakash Surya kstat_named_t arcstat_mru_ghost_evictable_data; 6224076b1bfSPrakash Surya /* 6234076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 6244076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 6254076b1bfSPrakash Surya * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 6263a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6274076b1bfSPrakash Surya */ 6284076b1bfSPrakash Surya kstat_named_t arcstat_mru_ghost_evictable_metadata; 6294076b1bfSPrakash Surya /* 6304076b1bfSPrakash Surya * Total number of bytes consumed by ARC buffers residing in the 6314076b1bfSPrakash Surya * arc_mfu state. This includes *all* buffers in the arc_mfu 6324076b1bfSPrakash Surya * state; e.g. data, metadata, evictable, and unevictable buffers 6334076b1bfSPrakash Surya * are all included in this value. 6343a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6354076b1bfSPrakash Surya */ 6364076b1bfSPrakash Surya kstat_named_t arcstat_mfu_size; 6374076b1bfSPrakash Surya /* 6384076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that are eligible for 6394076b1bfSPrakash Surya * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu 6404076b1bfSPrakash Surya * state. 6413a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6424076b1bfSPrakash Surya */ 6434076b1bfSPrakash Surya kstat_named_t arcstat_mfu_evictable_data; 6444076b1bfSPrakash Surya /* 6454076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that are eligible for 6464076b1bfSPrakash Surya * eviction, of type ARC_BUFC_METADATA, and reside in the 6474076b1bfSPrakash Surya * arc_mfu state. 6483a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6494076b1bfSPrakash Surya */ 6504076b1bfSPrakash Surya kstat_named_t arcstat_mfu_evictable_metadata; 6514076b1bfSPrakash Surya /* 6524076b1bfSPrakash Surya * Total number of bytes that *would have been* consumed by ARC 6534076b1bfSPrakash Surya * buffers in the arc_mfu_ghost state. See the comment above 6544076b1bfSPrakash Surya * arcstat_mru_ghost_size for more details. 6553a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6564076b1bfSPrakash Surya */ 6574076b1bfSPrakash Surya kstat_named_t arcstat_mfu_ghost_size; 6584076b1bfSPrakash Surya /* 6594076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 6604076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 6614076b1bfSPrakash Surya * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state. 6623a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6634076b1bfSPrakash Surya */ 6644076b1bfSPrakash Surya kstat_named_t arcstat_mfu_ghost_evictable_data; 6654076b1bfSPrakash Surya /* 6664076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 6674076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 6684076b1bfSPrakash Surya * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 6693a2d8a1bSPaul Dagnelie * Not updated directly; only synced in arc_kstat_update. 6704076b1bfSPrakash Surya */ 6714076b1bfSPrakash Surya kstat_named_t arcstat_mfu_ghost_evictable_metadata; 672fa94a07fSbrendan kstat_named_t arcstat_l2_hits; 673fa94a07fSbrendan kstat_named_t arcstat_l2_misses; 674fa94a07fSbrendan kstat_named_t arcstat_l2_feeds; 675fa94a07fSbrendan kstat_named_t arcstat_l2_rw_clash; 6765a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_l2_read_bytes; 6775a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_l2_write_bytes; 678fa94a07fSbrendan kstat_named_t arcstat_l2_writes_sent; 679fa94a07fSbrendan kstat_named_t arcstat_l2_writes_done; 680fa94a07fSbrendan kstat_named_t arcstat_l2_writes_error; 681244781f1SPrakash Surya kstat_named_t arcstat_l2_writes_lock_retry; 682fa94a07fSbrendan kstat_named_t arcstat_l2_evict_lock_retry; 683fa94a07fSbrendan kstat_named_t arcstat_l2_evict_reading; 68489c86e32SChris Williamson kstat_named_t arcstat_l2_evict_l1cached; 685fa94a07fSbrendan kstat_named_t arcstat_l2_free_on_write; 686fa94a07fSbrendan kstat_named_t arcstat_l2_abort_lowmem; 687fa94a07fSbrendan kstat_named_t arcstat_l2_cksum_bad; 688fa94a07fSbrendan kstat_named_t arcstat_l2_io_error; 68916a7e5acSAndriy Gapon kstat_named_t arcstat_l2_lsize; 69016a7e5acSAndriy Gapon kstat_named_t arcstat_l2_psize; 6913a2d8a1bSPaul Dagnelie /* Not updated directly; only synced in arc_kstat_update. */ 692fa94a07fSbrendan kstat_named_t arcstat_l2_hdr_size; 6931ab7f2deSmaybee kstat_named_t arcstat_memory_throttle_count; 6943a2d8a1bSPaul Dagnelie /* Not updated directly; only synced in arc_kstat_update. */ 69520128a08SGeorge Wilson kstat_named_t arcstat_meta_used; 69620128a08SGeorge Wilson kstat_named_t arcstat_meta_limit; 69720128a08SGeorge Wilson kstat_named_t arcstat_meta_max; 6983a5286a1SMatthew Ahrens kstat_named_t arcstat_meta_min; 699*a3874b8bSToomas Soome kstat_named_t arcstat_async_upgrade_sync; 700cf6106c8SMatthew Ahrens kstat_named_t arcstat_demand_hit_predictive_prefetch; 701*a3874b8bSToomas Soome kstat_named_t arcstat_demand_hit_prescient_prefetch; 70244cb6abcSbmc } arc_stats_t; 70344cb6abcSbmc 70444cb6abcSbmc static arc_stats_t arc_stats = { 70544cb6abcSbmc { "hits", KSTAT_DATA_UINT64 }, 70644cb6abcSbmc { "misses", KSTAT_DATA_UINT64 }, 70744cb6abcSbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 70844cb6abcSbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 70944cb6abcSbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 71044cb6abcSbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 71144cb6abcSbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 71244cb6abcSbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 71344cb6abcSbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 71444cb6abcSbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 71544cb6abcSbmc { "mru_hits", KSTAT_DATA_UINT64 }, 71644cb6abcSbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 71744cb6abcSbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 71844cb6abcSbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 71944cb6abcSbmc { "deleted", KSTAT_DATA_UINT64 }, 72044cb6abcSbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 7217b38fab6SAlexander Motin { "access_skip", KSTAT_DATA_UINT64 }, 72244cb6abcSbmc { "evict_skip", KSTAT_DATA_UINT64 }, 723244781f1SPrakash Surya { "evict_not_enough", KSTAT_DATA_UINT64 }, 7245ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_cached", KSTAT_DATA_UINT64 }, 7255ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 7265ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 727244781f1SPrakash Surya { "evict_l2_skip", KSTAT_DATA_UINT64 }, 72844cb6abcSbmc { "hash_elements", KSTAT_DATA_UINT64 }, 72944cb6abcSbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 73044cb6abcSbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 73144cb6abcSbmc { "hash_chains", KSTAT_DATA_UINT64 }, 73244cb6abcSbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 73344cb6abcSbmc { "p", KSTAT_DATA_UINT64 }, 73444cb6abcSbmc { "c", KSTAT_DATA_UINT64 }, 73544cb6abcSbmc { "c_min", KSTAT_DATA_UINT64 }, 73644cb6abcSbmc { "c_max", KSTAT_DATA_UINT64 }, 737fa94a07fSbrendan { "size", KSTAT_DATA_UINT64 }, 738dcbf3bd6SGeorge Wilson { "compressed_size", KSTAT_DATA_UINT64 }, 739dcbf3bd6SGeorge Wilson { "uncompressed_size", KSTAT_DATA_UINT64 }, 740dcbf3bd6SGeorge Wilson { "overhead_size", KSTAT_DATA_UINT64 }, 741fa94a07fSbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 7425a98e54bSBrendan Gregg - Sun Microsystems { "data_size", KSTAT_DATA_UINT64 }, 7434076b1bfSPrakash Surya { "metadata_size", KSTAT_DATA_UINT64 }, 7445a98e54bSBrendan Gregg - Sun Microsystems { "other_size", KSTAT_DATA_UINT64 }, 7454076b1bfSPrakash Surya { "anon_size", KSTAT_DATA_UINT64 }, 7464076b1bfSPrakash Surya { "anon_evictable_data", KSTAT_DATA_UINT64 }, 7474076b1bfSPrakash Surya { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, 7484076b1bfSPrakash Surya { "mru_size", KSTAT_DATA_UINT64 }, 7494076b1bfSPrakash Surya { "mru_evictable_data", KSTAT_DATA_UINT64 }, 7504076b1bfSPrakash Surya { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, 7514076b1bfSPrakash Surya { "mru_ghost_size", KSTAT_DATA_UINT64 }, 7524076b1bfSPrakash Surya { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, 7534076b1bfSPrakash Surya { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 7544076b1bfSPrakash Surya { "mfu_size", KSTAT_DATA_UINT64 }, 7554076b1bfSPrakash Surya { "mfu_evictable_data", KSTAT_DATA_UINT64 }, 7564076b1bfSPrakash Surya { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, 7574076b1bfSPrakash Surya { "mfu_ghost_size", KSTAT_DATA_UINT64 }, 7584076b1bfSPrakash Surya { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, 7594076b1bfSPrakash Surya { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 760fa94a07fSbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 761fa94a07fSbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 762fa94a07fSbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 763fa94a07fSbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 7645a98e54bSBrendan Gregg - Sun Microsystems { "l2_read_bytes", KSTAT_DATA_UINT64 }, 7655a98e54bSBrendan Gregg - Sun Microsystems { "l2_write_bytes", KSTAT_DATA_UINT64 }, 766fa94a07fSbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 767fa94a07fSbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 768fa94a07fSbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 769244781f1SPrakash Surya { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, 770fa94a07fSbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 771fa94a07fSbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 77289c86e32SChris Williamson { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, 773fa94a07fSbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 774fa94a07fSbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 775fa94a07fSbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 776fa94a07fSbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 777fa94a07fSbrendan { "l2_size", KSTAT_DATA_UINT64 }, 778aad02571SSaso Kiselkov { "l2_asize", KSTAT_DATA_UINT64 }, 7791ab7f2deSmaybee { "l2_hdr_size", KSTAT_DATA_UINT64 }, 7809253d63dSGeorge Wilson { "memory_throttle_count", KSTAT_DATA_UINT64 }, 78120128a08SGeorge Wilson { "arc_meta_used", KSTAT_DATA_UINT64 }, 78220128a08SGeorge Wilson { "arc_meta_limit", KSTAT_DATA_UINT64 }, 7833a5286a1SMatthew Ahrens { "arc_meta_max", KSTAT_DATA_UINT64 }, 784cf6106c8SMatthew Ahrens { "arc_meta_min", KSTAT_DATA_UINT64 }, 785*a3874b8bSToomas Soome { "async_upgrade_sync", KSTAT_DATA_UINT64 }, 786cf6106c8SMatthew Ahrens { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 }, 787*a3874b8bSToomas Soome { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64 }, 78844cb6abcSbmc }; 78944cb6abcSbmc 79044cb6abcSbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 79144cb6abcSbmc 79244cb6abcSbmc #define ARCSTAT_INCR(stat, val) \ 793f7170741SWill Andrews atomic_add_64(&arc_stats.stat.value.ui64, (val)) 79444cb6abcSbmc 795b24ab676SJeff Bonwick #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 79644cb6abcSbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 79744cb6abcSbmc 79844cb6abcSbmc #define ARCSTAT_MAX(stat, val) { \ 79944cb6abcSbmc uint64_t m; \ 80044cb6abcSbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 80144cb6abcSbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 80244cb6abcSbmc continue; \ 80344cb6abcSbmc } 80444cb6abcSbmc 80544cb6abcSbmc #define ARCSTAT_MAXSTAT(stat) \ 80644cb6abcSbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 80744cb6abcSbmc 80844cb6abcSbmc /* 80944cb6abcSbmc * We define a macro to allow ARC hits/misses to be easily broken down by 81044cb6abcSbmc * two separate conditions, giving a total of four different subtypes for 81144cb6abcSbmc * each of hits and misses (so eight statistics total). 81244cb6abcSbmc */ 81344cb6abcSbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 81444cb6abcSbmc if (cond1) { \ 81544cb6abcSbmc if (cond2) { \ 81644cb6abcSbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 81744cb6abcSbmc } else { \ 81844cb6abcSbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 81944cb6abcSbmc } \ 82044cb6abcSbmc } else { \ 82144cb6abcSbmc if (cond2) { \ 82244cb6abcSbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 82344cb6abcSbmc } else { \ 82444cb6abcSbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 82544cb6abcSbmc } \ 82644cb6abcSbmc } 82744cb6abcSbmc 82844cb6abcSbmc kstat_t *arc_ksp; 829b24ab676SJeff Bonwick static arc_state_t *arc_anon; 83044cb6abcSbmc static arc_state_t *arc_mru; 83144cb6abcSbmc static arc_state_t *arc_mru_ghost; 83244cb6abcSbmc static arc_state_t *arc_mfu; 83344cb6abcSbmc static arc_state_t *arc_mfu_ghost; 834fa94a07fSbrendan static arc_state_t *arc_l2c_only; 83544cb6abcSbmc 83644cb6abcSbmc /* 83744cb6abcSbmc * There are several ARC variables that are critical to export as kstats -- 83844cb6abcSbmc * but we don't want to have to grovel around in the kstat whenever we wish to 83944cb6abcSbmc * manipulate them. For these variables, we therefore define them to be in 84044cb6abcSbmc * terms of the statistic variable. This assures that we are not introducing 84144cb6abcSbmc * the possibility of inconsistency by having shadow copies of the variables, 84244cb6abcSbmc * while still allowing the code to be readable. 84344cb6abcSbmc */ 84444cb6abcSbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 84544cb6abcSbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 84644cb6abcSbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 84744cb6abcSbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 84820128a08SGeorge Wilson #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ 8493a5286a1SMatthew Ahrens #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ 85020128a08SGeorge Wilson #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ 85144cb6abcSbmc 852dcbf3bd6SGeorge Wilson /* compressed size of entire arc */ 853dcbf3bd6SGeorge Wilson #define arc_compressed_size ARCSTAT(arcstat_compressed_size) 854dcbf3bd6SGeorge Wilson /* uncompressed size of entire arc */ 855dcbf3bd6SGeorge Wilson #define arc_uncompressed_size ARCSTAT(arcstat_uncompressed_size) 856dcbf3bd6SGeorge Wilson /* number of bytes in the arc from arc_buf_t's */ 857dcbf3bd6SGeorge Wilson #define arc_overhead_size ARCSTAT(arcstat_overhead_size) 858aad02571SSaso Kiselkov 8593a2d8a1bSPaul Dagnelie /* 8603a2d8a1bSPaul Dagnelie * There are also some ARC variables that we want to export, but that are 8613a2d8a1bSPaul Dagnelie * updated so often that having the canonical representation be the statistic 8623a2d8a1bSPaul Dagnelie * variable causes a performance bottleneck. We want to use aggsum_t's for these 8633a2d8a1bSPaul Dagnelie * instead, but still be able to export the kstat in the same way as before. 8643a2d8a1bSPaul Dagnelie * The solution is to always use the aggsum version, except in the kstat update 8653a2d8a1bSPaul Dagnelie * callback. 8663a2d8a1bSPaul Dagnelie */ 8673a2d8a1bSPaul Dagnelie aggsum_t arc_size; 8683a2d8a1bSPaul Dagnelie aggsum_t arc_meta_used; 8693a2d8a1bSPaul Dagnelie aggsum_t astat_data_size; 8703a2d8a1bSPaul Dagnelie aggsum_t astat_metadata_size; 8713a2d8a1bSPaul Dagnelie aggsum_t astat_hdr_size; 8723a2d8a1bSPaul Dagnelie aggsum_t astat_other_size; 8733a2d8a1bSPaul Dagnelie aggsum_t astat_l2_hdr_size; 8743a2d8a1bSPaul Dagnelie 87544cb6abcSbmc static int arc_no_grow; /* Don't try to grow cache size */ 876de753e34SBrad Lewis static hrtime_t arc_growtime; 87744cb6abcSbmc static uint64_t arc_tempreserve; 8782fdbea25SAleksandr Guzovskiy static uint64_t arc_loaned_bytes; 879fa9e4066Sahrens 880fa9e4066Sahrens typedef struct arc_callback arc_callback_t; 881fa9e4066Sahrens 882fa9e4066Sahrens struct arc_callback { 883fa9e4066Sahrens void *acb_private; 884*a3874b8bSToomas Soome arc_read_done_func_t *acb_done; 885fa9e4066Sahrens arc_buf_t *acb_buf; 8865602294fSDan Kimmel boolean_t acb_compressed; 887fa9e4066Sahrens zio_t *acb_zio_dummy; 888*a3874b8bSToomas Soome zio_t *acb_zio_head; 889fa9e4066Sahrens arc_callback_t *acb_next; 890fa9e4066Sahrens }; 891fa9e4066Sahrens 892c717a561Smaybee typedef struct arc_write_callback arc_write_callback_t; 893c717a561Smaybee 894c717a561Smaybee struct arc_write_callback { 895*a3874b8bSToomas Soome void *awcb_private; 896*a3874b8bSToomas Soome arc_write_done_func_t *awcb_ready; 897*a3874b8bSToomas Soome arc_write_done_func_t *awcb_children_ready; 898*a3874b8bSToomas Soome arc_write_done_func_t *awcb_physdone; 899*a3874b8bSToomas Soome arc_write_done_func_t *awcb_done; 900*a3874b8bSToomas Soome arc_buf_t *awcb_buf; 901c717a561Smaybee }; 902c717a561Smaybee 90389c86e32SChris Williamson /* 90489c86e32SChris Williamson * ARC buffers are separated into multiple structs as a memory saving measure: 90589c86e32SChris Williamson * - Common fields struct, always defined, and embedded within it: 90689c86e32SChris Williamson * - L2-only fields, always allocated but undefined when not in L2ARC 90789c86e32SChris Williamson * - L1-only fields, only allocated when in L1ARC 90889c86e32SChris Williamson * 90989c86e32SChris Williamson * Buffer in L1 Buffer only in L2 91089c86e32SChris Williamson * +------------------------+ +------------------------+ 91189c86e32SChris Williamson * | arc_buf_hdr_t | | arc_buf_hdr_t | 91289c86e32SChris Williamson * | | | | 91389c86e32SChris Williamson * | | | | 91489c86e32SChris Williamson * | | | | 91589c86e32SChris Williamson * +------------------------+ +------------------------+ 91689c86e32SChris Williamson * | l2arc_buf_hdr_t | | l2arc_buf_hdr_t | 91789c86e32SChris Williamson * | (undefined if L1-only) | | | 91889c86e32SChris Williamson * +------------------------+ +------------------------+ 91989c86e32SChris Williamson * | l1arc_buf_hdr_t | 92089c86e32SChris Williamson * | | 92189c86e32SChris Williamson * | | 92289c86e32SChris Williamson * | | 92389c86e32SChris Williamson * | | 92489c86e32SChris Williamson * +------------------------+ 92589c86e32SChris Williamson * 92689c86e32SChris Williamson * Because it's possible for the L2ARC to become extremely large, we can wind 92789c86e32SChris Williamson * up eating a lot of memory in L2ARC buffer headers, so the size of a header 92889c86e32SChris Williamson * is minimized by only allocating the fields necessary for an L1-cached buffer 92989c86e32SChris Williamson * when a header is actually in the L1 cache. The sub-headers (l1arc_buf_hdr and 93089c86e32SChris Williamson * l2arc_buf_hdr) are embedded rather than allocated separately to save a couple 93189c86e32SChris Williamson * words in pointers. arc_hdr_realloc() is used to switch a header between 93289c86e32SChris Williamson * these two allocation states. 93389c86e32SChris Williamson */ 93489c86e32SChris Williamson typedef struct l1arc_buf_hdr { 9356b4acc8bSahrens kmutex_t b_freeze_lock; 936dcbf3bd6SGeorge Wilson zio_cksum_t *b_freeze_cksum; 93789c86e32SChris Williamson #ifdef ZFS_DEBUG 93889c86e32SChris Williamson /* 9395602294fSDan Kimmel * Used for debugging with kmem_flags - by allocating and freeing 94089c86e32SChris Williamson * b_thawed when the buffer is thawed, we get a record of the stack 94189c86e32SChris Williamson * trace that thawed it. 94289c86e32SChris Williamson */ 9433f9d6ad7SLin Ling void *b_thawed; 94489c86e32SChris Williamson #endif 9456b4acc8bSahrens 946fa9e4066Sahrens arc_buf_t *b_buf; 947dcbf3bd6SGeorge Wilson uint32_t b_bufcnt; 94889c86e32SChris Williamson /* for waiting on writes to complete */ 949ad23a2dbSjohansen kcondvar_t b_cv; 950dcbf3bd6SGeorge Wilson uint8_t b_byteswap; 951ad23a2dbSjohansen 952fa9e4066Sahrens /* protected by arc state mutex */ 953fa9e4066Sahrens arc_state_t *b_state; 954244781f1SPrakash Surya multilist_node_t b_arc_node; 955fa9e4066Sahrens 956fa9e4066Sahrens /* updated atomically */ 957fa9e4066Sahrens clock_t b_arc_access; 958fa9e4066Sahrens 959fa9e4066Sahrens /* self protecting */ 960e914ace2STim Schumacher zfs_refcount_t b_refcnt; 961fa94a07fSbrendan 96289c86e32SChris Williamson arc_callback_t *b_acb; 963770499e1SDan Kimmel abd_t *b_pabd; 96489c86e32SChris Williamson } l1arc_buf_hdr_t; 96589c86e32SChris Williamson 96689c86e32SChris Williamson typedef struct l2arc_dev l2arc_dev_t; 96789c86e32SChris Williamson 96889c86e32SChris Williamson typedef struct l2arc_buf_hdr { 96989c86e32SChris Williamson /* protected by arc_buf_hdr mutex */ 97089c86e32SChris Williamson l2arc_dev_t *b_dev; /* L2ARC device */ 97189c86e32SChris Williamson uint64_t b_daddr; /* disk address, offset byte */ 97289c86e32SChris Williamson 973fa94a07fSbrendan list_node_t b_l2node; 97489c86e32SChris Williamson } l2arc_buf_hdr_t; 97589c86e32SChris Williamson 97689c86e32SChris Williamson struct arc_buf_hdr { 97789c86e32SChris Williamson /* protected by hash lock */ 97889c86e32SChris Williamson dva_t b_dva; 97989c86e32SChris Williamson uint64_t b_birth; 98089c86e32SChris Williamson 981dcbf3bd6SGeorge Wilson arc_buf_contents_t b_type; 98289c86e32SChris Williamson arc_buf_hdr_t *b_hash_next; 98389c86e32SChris Williamson arc_flags_t b_flags; 98489c86e32SChris Williamson 985dcbf3bd6SGeorge Wilson /* 986dcbf3bd6SGeorge Wilson * This field stores the size of the data buffer after 987dcbf3bd6SGeorge Wilson * compression, and is set in the arc's zio completion handlers. 988dcbf3bd6SGeorge Wilson * It is in units of SPA_MINBLOCKSIZE (e.g. 1 == 512 bytes). 989dcbf3bd6SGeorge Wilson * 990dcbf3bd6SGeorge Wilson * While the block pointers can store up to 32MB in their psize 991dcbf3bd6SGeorge Wilson * field, we can only store up to 32MB minus 512B. This is due 992dcbf3bd6SGeorge Wilson * to the bp using a bias of 1, whereas we use a bias of 0 (i.e. 993dcbf3bd6SGeorge Wilson * a field of zeros represents 512B in the bp). We can't use a 994dcbf3bd6SGeorge Wilson * bias of 1 since we need to reserve a psize of zero, here, to 995dcbf3bd6SGeorge Wilson * represent holes and embedded blocks. 996dcbf3bd6SGeorge Wilson * 997dcbf3bd6SGeorge Wilson * This isn't a problem in practice, since the maximum size of a 998dcbf3bd6SGeorge Wilson * buffer is limited to 16MB, so we never need to store 32MB in 999dcbf3bd6SGeorge Wilson * this field. Even in the upstream illumos code base, the 1000dcbf3bd6SGeorge Wilson * maximum size of a buffer is limited to 16MB. 1001dcbf3bd6SGeorge Wilson */ 1002dcbf3bd6SGeorge Wilson uint16_t b_psize; 1003dcbf3bd6SGeorge Wilson 1004dcbf3bd6SGeorge Wilson /* 1005dcbf3bd6SGeorge Wilson * This field stores the size of the data buffer before 1006dcbf3bd6SGeorge Wilson * compression, and cannot change once set. It is in units 1007dcbf3bd6SGeorge Wilson * of SPA_MINBLOCKSIZE (e.g. 2 == 1024 bytes) 1008dcbf3bd6SGeorge Wilson */ 1009dcbf3bd6SGeorge Wilson uint16_t b_lsize; /* immutable */ 1010dcbf3bd6SGeorge Wilson uint64_t b_spa; /* immutable */ 101189c86e32SChris Williamson 101289c86e32SChris Williamson /* L2ARC fields. Undefined when not in L2ARC. */ 101389c86e32SChris Williamson l2arc_buf_hdr_t b_l2hdr; 101489c86e32SChris Williamson /* L1ARC fields. Undefined when in l2arc_only state */ 101589c86e32SChris Williamson l1arc_buf_hdr_t b_l1hdr; 1016fa9e4066Sahrens }; 1017fa9e4066Sahrens 1018ea8dc4b6Seschrock #define GHOST_STATE(state) \ 1019fa94a07fSbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 1020fa94a07fSbrendan (state) == arc_l2c_only) 1021ea8dc4b6Seschrock 10227adb730bSGeorge Wilson #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) 10237adb730bSGeorge Wilson #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) 10247adb730bSGeorge Wilson #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) 10257adb730bSGeorge Wilson #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) 1026*a3874b8bSToomas Soome #define HDR_PRESCIENT_PREFETCH(hdr) \ 1027*a3874b8bSToomas Soome ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH) 1028dcbf3bd6SGeorge Wilson #define HDR_COMPRESSION_ENABLED(hdr) \ 1029dcbf3bd6SGeorge Wilson ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC) 103089c86e32SChris Williamson 10317adb730bSGeorge Wilson #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) 10327adb730bSGeorge Wilson #define HDR_L2_READING(hdr) \ 1033dcbf3bd6SGeorge Wilson (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ 1034dcbf3bd6SGeorge Wilson ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) 10357adb730bSGeorge Wilson #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) 10367adb730bSGeorge Wilson #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) 10377adb730bSGeorge Wilson #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) 1038dcbf3bd6SGeorge Wilson #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA) 1039fa9e4066Sahrens 104089c86e32SChris Williamson #define HDR_ISTYPE_METADATA(hdr) \ 1041dcbf3bd6SGeorge Wilson ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) 104289c86e32SChris Williamson #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) 104389c86e32SChris Williamson 104489c86e32SChris Williamson #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) 104589c86e32SChris Williamson #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) 104689c86e32SChris Williamson 1047dcbf3bd6SGeorge Wilson /* For storing compression mode in b_flags */ 1048dcbf3bd6SGeorge Wilson #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1) 1049dcbf3bd6SGeorge Wilson 1050dcbf3bd6SGeorge Wilson #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \ 1051dcbf3bd6SGeorge Wilson HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS)) 1052dcbf3bd6SGeorge Wilson #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \ 1053dcbf3bd6SGeorge Wilson HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp)); 1054dcbf3bd6SGeorge Wilson 1055dcbf3bd6SGeorge Wilson #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL) 10565602294fSDan Kimmel #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED) 10575602294fSDan Kimmel #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED) 1058dcbf3bd6SGeorge Wilson 1059e6c728e1Sbrendan /* 1060e6c728e1Sbrendan * Other sizes 1061e6c728e1Sbrendan */ 1062e6c728e1Sbrendan 106389c86e32SChris Williamson #define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 106489c86e32SChris Williamson #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) 1065e6c728e1Sbrendan 1066fa9e4066Sahrens /* 1067fa9e4066Sahrens * Hash table routines 1068fa9e4066Sahrens */ 1069fa9e4066Sahrens 1070fa9e4066Sahrens #define HT_LOCK_PAD 64 1071fa9e4066Sahrens 1072fa9e4066Sahrens struct ht_lock { 1073fa9e4066Sahrens kmutex_t ht_lock; 1074fa9e4066Sahrens #ifdef _KERNEL 1075fa9e4066Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 1076fa9e4066Sahrens #endif 1077fa9e4066Sahrens }; 1078fa9e4066Sahrens 1079fa9e4066Sahrens #define BUF_LOCKS 256 1080fa9e4066Sahrens typedef struct buf_hash_table { 1081fa9e4066Sahrens uint64_t ht_mask; 1082fa9e4066Sahrens arc_buf_hdr_t **ht_table; 1083fa9e4066Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 1084fa9e4066Sahrens } buf_hash_table_t; 1085fa9e4066Sahrens 1086fa9e4066Sahrens static buf_hash_table_t buf_hash_table; 1087fa9e4066Sahrens 1088fa9e4066Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 1089fa9e4066Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 1090fa9e4066Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 1091fa9e4066Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 10923f9d6ad7SLin Ling #define HDR_LOCK(hdr) \ 10933f9d6ad7SLin Ling (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 1094fa9e4066Sahrens 1095fa9e4066Sahrens uint64_t zfs_crc64_table[256]; 1096fa9e4066Sahrens 1097fa94a07fSbrendan /* 1098fa94a07fSbrendan * Level 2 ARC 1099fa94a07fSbrendan */ 1100fa94a07fSbrendan 1101fa94a07fSbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 1102aad02571SSaso Kiselkov #define L2ARC_HEADROOM 2 /* num of writes */ 1103aad02571SSaso Kiselkov /* 1104aad02571SSaso Kiselkov * If we discover during ARC scan any buffers to be compressed, we boost 1105aad02571SSaso Kiselkov * our headroom for the next scanning cycle by this percentage multiple. 1106aad02571SSaso Kiselkov */ 1107aad02571SSaso Kiselkov #define L2ARC_HEADROOM_BOOST 200 11085a98e54bSBrendan Gregg - Sun Microsystems #define L2ARC_FEED_SECS 1 /* caching interval secs */ 11095a98e54bSBrendan Gregg - Sun Microsystems #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 1110fa94a07fSbrendan 1111fa94a07fSbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 1112fa94a07fSbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 1113fa94a07fSbrendan 1114f7170741SWill Andrews /* L2ARC Performance Tunables */ 1115fa94a07fSbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 11163a737e0dSbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 1117fa94a07fSbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 1118aad02571SSaso Kiselkov uint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 1119fa94a07fSbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 11205a98e54bSBrendan Gregg - Sun Microsystems uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 1121fa94a07fSbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 11225a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 11235a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 1124fa94a07fSbrendan 1125fa94a07fSbrendan /* 1126fa94a07fSbrendan * L2ARC Internals 1127fa94a07fSbrendan */ 112889c86e32SChris Williamson struct l2arc_dev { 1129fa94a07fSbrendan vdev_t *l2ad_vdev; /* vdev */ 1130fa94a07fSbrendan spa_t *l2ad_spa; /* spa */ 1131fa94a07fSbrendan uint64_t l2ad_hand; /* next write location */ 1132fa94a07fSbrendan uint64_t l2ad_start; /* first addr on device */ 1133fa94a07fSbrendan uint64_t l2ad_end; /* last addr on device */ 1134fa94a07fSbrendan boolean_t l2ad_first; /* first sweep through */ 11355a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2ad_writing; /* currently writing */ 113689c86e32SChris Williamson kmutex_t l2ad_mtx; /* lock for buffer list */ 113789c86e32SChris Williamson list_t l2ad_buflist; /* buffer list */ 1138fa94a07fSbrendan list_node_t l2ad_node; /* device list node */ 1139e914ace2STim Schumacher zfs_refcount_t l2ad_alloc; /* allocated bytes */ 114089c86e32SChris Williamson }; 1141fa94a07fSbrendan 1142fa94a07fSbrendan static list_t L2ARC_dev_list; /* device list */ 1143fa94a07fSbrendan static list_t *l2arc_dev_list; /* device list pointer */ 1144fa94a07fSbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 1145fa94a07fSbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 1146fa94a07fSbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 1147fa94a07fSbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 1148fa94a07fSbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 1149fa94a07fSbrendan static uint64_t l2arc_ndev; /* number of devices */ 1150fa94a07fSbrendan 1151fa94a07fSbrendan typedef struct l2arc_read_callback { 11525602294fSDan Kimmel arc_buf_hdr_t *l2rcb_hdr; /* read header */ 1153aad02571SSaso Kiselkov blkptr_t l2rcb_bp; /* original blkptr */ 11547802d7bfSMatthew Ahrens zbookmark_phys_t l2rcb_zb; /* original bookmark */ 1155aad02571SSaso Kiselkov int l2rcb_flags; /* original flags */ 1156403a8da7SAndriy Gapon abd_t *l2rcb_abd; /* temporary buffer */ 1157fa94a07fSbrendan } l2arc_read_callback_t; 1158fa94a07fSbrendan 1159fa94a07fSbrendan typedef struct l2arc_write_callback { 1160fa94a07fSbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 1161fa94a07fSbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 1162fa94a07fSbrendan } l2arc_write_callback_t; 1163fa94a07fSbrendan 1164fa94a07fSbrendan typedef struct l2arc_data_free { 1165fa94a07fSbrendan /* protected by l2arc_free_on_write_mtx */ 1166770499e1SDan Kimmel abd_t *l2df_abd; 1167fa94a07fSbrendan size_t l2df_size; 1168dcbf3bd6SGeorge Wilson arc_buf_contents_t l2df_type; 1169fa94a07fSbrendan list_node_t l2df_list_node; 1170fa94a07fSbrendan } l2arc_data_free_t; 1171fa94a07fSbrendan 1172fa94a07fSbrendan static kmutex_t l2arc_feed_thr_lock; 1173fa94a07fSbrendan static kcondvar_t l2arc_feed_thr_cv; 1174fa94a07fSbrendan static uint8_t l2arc_thread_exit; 1175fa94a07fSbrendan 1176770499e1SDan Kimmel static abd_t *arc_get_data_abd(arc_buf_hdr_t *, uint64_t, void *); 1177dcbf3bd6SGeorge Wilson static void *arc_get_data_buf(arc_buf_hdr_t *, uint64_t, void *); 1178770499e1SDan Kimmel static void arc_get_data_impl(arc_buf_hdr_t *, uint64_t, void *); 1179770499e1SDan Kimmel static void arc_free_data_abd(arc_buf_hdr_t *, abd_t *, uint64_t, void *); 1180dcbf3bd6SGeorge Wilson static void arc_free_data_buf(arc_buf_hdr_t *, void *, uint64_t, void *); 1181770499e1SDan Kimmel static void arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag); 1182770499e1SDan Kimmel static void arc_hdr_free_pabd(arc_buf_hdr_t *); 1183770499e1SDan Kimmel static void arc_hdr_alloc_pabd(arc_buf_hdr_t *); 11847adb730bSGeorge Wilson static void arc_access(arc_buf_hdr_t *, kmutex_t *); 1185244781f1SPrakash Surya static boolean_t arc_is_overflowing(); 11867adb730bSGeorge Wilson static void arc_buf_watch(arc_buf_t *); 11877adb730bSGeorge Wilson 118889c86e32SChris Williamson static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); 118989c86e32SChris Williamson static uint32_t arc_bufc_to_flags(arc_buf_contents_t); 1190dcbf3bd6SGeorge Wilson static inline void arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); 1191dcbf3bd6SGeorge Wilson static inline void arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); 119289c86e32SChris Williamson 11937adb730bSGeorge Wilson static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); 11947adb730bSGeorge Wilson static void l2arc_read_done(zio_t *); 1195fa94a07fSbrendan 11963a2d8a1bSPaul Dagnelie 11973a2d8a1bSPaul Dagnelie /* 11983a2d8a1bSPaul Dagnelie * We use Cityhash for this. It's fast, and has good hash properties without 11993a2d8a1bSPaul Dagnelie * requiring any large static buffers. 12003a2d8a1bSPaul Dagnelie */ 1201fa9e4066Sahrens static uint64_t 1202ac05c741SMark Maybee buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 1203fa9e4066Sahrens { 12043a2d8a1bSPaul Dagnelie return (cityhash4(spa, dva->dva_word[0], dva->dva_word[1], birth)); 1205fa9e4066Sahrens } 1206fa9e4066Sahrens 1207dcbf3bd6SGeorge Wilson #define HDR_EMPTY(hdr) \ 1208dcbf3bd6SGeorge Wilson ((hdr)->b_dva.dva_word[0] == 0 && \ 1209dcbf3bd6SGeorge Wilson (hdr)->b_dva.dva_word[1] == 0) 1210fa9e4066Sahrens 1211dcbf3bd6SGeorge Wilson #define HDR_EQUAL(spa, dva, birth, hdr) \ 1212dcbf3bd6SGeorge Wilson ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 1213dcbf3bd6SGeorge Wilson ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 1214dcbf3bd6SGeorge Wilson ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa) 1215fa9e4066Sahrens 12163f9d6ad7SLin Ling static void 12173f9d6ad7SLin Ling buf_discard_identity(arc_buf_hdr_t *hdr) 12183f9d6ad7SLin Ling { 12193f9d6ad7SLin Ling hdr->b_dva.dva_word[0] = 0; 12203f9d6ad7SLin Ling hdr->b_dva.dva_word[1] = 0; 12213f9d6ad7SLin Ling hdr->b_birth = 0; 12223f9d6ad7SLin Ling } 12233f9d6ad7SLin Ling 1224fa9e4066Sahrens static arc_buf_hdr_t * 12255d7b4d43SMatthew Ahrens buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 1226fa9e4066Sahrens { 12275d7b4d43SMatthew Ahrens const dva_t *dva = BP_IDENTITY(bp); 12285d7b4d43SMatthew Ahrens uint64_t birth = BP_PHYSICAL_BIRTH(bp); 1229fa9e4066Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 1230fa9e4066Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 12317adb730bSGeorge Wilson arc_buf_hdr_t *hdr; 1232fa9e4066Sahrens 1233fa9e4066Sahrens mutex_enter(hash_lock); 12347adb730bSGeorge Wilson for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; 12357adb730bSGeorge Wilson hdr = hdr->b_hash_next) { 1236dcbf3bd6SGeorge Wilson if (HDR_EQUAL(spa, dva, birth, hdr)) { 1237fa9e4066Sahrens *lockp = hash_lock; 12387adb730bSGeorge Wilson return (hdr); 1239fa9e4066Sahrens } 1240fa9e4066Sahrens } 1241fa9e4066Sahrens mutex_exit(hash_lock); 1242fa9e4066Sahrens *lockp = NULL; 1243fa9e4066Sahrens return (NULL); 1244fa9e4066Sahrens } 1245fa9e4066Sahrens 1246fa9e4066Sahrens /* 1247fa9e4066Sahrens * Insert an entry into the hash table. If there is already an element 1248fa9e4066Sahrens * equal to elem in the hash table, then the already existing element 1249fa9e4066Sahrens * will be returned and the new element will not be inserted. 1250fa9e4066Sahrens * Otherwise returns NULL. 125189c86e32SChris Williamson * If lockp == NULL, the caller is assumed to already hold the hash lock. 1252fa9e4066Sahrens */ 1253fa9e4066Sahrens static arc_buf_hdr_t * 12547adb730bSGeorge Wilson buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) 1255fa9e4066Sahrens { 12567adb730bSGeorge Wilson uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1257fa9e4066Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 12587adb730bSGeorge Wilson arc_buf_hdr_t *fhdr; 125944cb6abcSbmc uint32_t i; 1260fa9e4066Sahrens 12617adb730bSGeorge Wilson ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); 12627adb730bSGeorge Wilson ASSERT(hdr->b_birth != 0); 12637adb730bSGeorge Wilson ASSERT(!HDR_IN_HASH_TABLE(hdr)); 126489c86e32SChris Williamson 126589c86e32SChris Williamson if (lockp != NULL) { 126689c86e32SChris Williamson *lockp = hash_lock; 126789c86e32SChris Williamson mutex_enter(hash_lock); 126889c86e32SChris Williamson } else { 126989c86e32SChris Williamson ASSERT(MUTEX_HELD(hash_lock)); 127089c86e32SChris Williamson } 127189c86e32SChris Williamson 12727adb730bSGeorge Wilson for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; 12737adb730bSGeorge Wilson fhdr = fhdr->b_hash_next, i++) { 1274dcbf3bd6SGeorge Wilson if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) 12757adb730bSGeorge Wilson return (fhdr); 1276fa9e4066Sahrens } 1277fa9e4066Sahrens 12787adb730bSGeorge Wilson hdr->b_hash_next = buf_hash_table.ht_table[idx]; 12797adb730bSGeorge Wilson buf_hash_table.ht_table[idx] = hdr; 1280dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 1281fa9e4066Sahrens 1282fa9e4066Sahrens /* collect some hash table performance data */ 1283fa9e4066Sahrens if (i > 0) { 128444cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_collisions); 1285fa9e4066Sahrens if (i == 1) 128644cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_chains); 128744cb6abcSbmc 128844cb6abcSbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 1289fa9e4066Sahrens } 129044cb6abcSbmc 129144cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_elements); 129244cb6abcSbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 1293fa9e4066Sahrens 1294fa9e4066Sahrens return (NULL); 1295fa9e4066Sahrens } 1296fa9e4066Sahrens 1297fa9e4066Sahrens static void 12987adb730bSGeorge Wilson buf_hash_remove(arc_buf_hdr_t *hdr) 1299fa9e4066Sahrens { 13007adb730bSGeorge Wilson arc_buf_hdr_t *fhdr, **hdrp; 13017adb730bSGeorge Wilson uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1302fa9e4066Sahrens 1303fa9e4066Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 13047adb730bSGeorge Wilson ASSERT(HDR_IN_HASH_TABLE(hdr)); 1305fa9e4066Sahrens 13067adb730bSGeorge Wilson hdrp = &buf_hash_table.ht_table[idx]; 13077adb730bSGeorge Wilson while ((fhdr = *hdrp) != hdr) { 1308dcbf3bd6SGeorge Wilson ASSERT3P(fhdr, !=, NULL); 13097adb730bSGeorge Wilson hdrp = &fhdr->b_hash_next; 1310fa9e4066Sahrens } 13117adb730bSGeorge Wilson *hdrp = hdr->b_hash_next; 13127adb730bSGeorge Wilson hdr->b_hash_next = NULL; 1313dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 1314fa9e4066Sahrens 1315fa9e4066Sahrens /* collect some hash table performance data */ 131644cb6abcSbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 131744cb6abcSbmc 1318fa9e4066Sahrens if (buf_hash_table.ht_table[idx] && 1319fa9e4066Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 132044cb6abcSbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1321fa9e4066Sahrens } 1322fa9e4066Sahrens 1323fa9e4066Sahrens /* 1324fa9e4066Sahrens * Global data structures and functions for the buf kmem cache. 1325fa9e4066Sahrens */ 132689c86e32SChris Williamson static kmem_cache_t *hdr_full_cache; 132789c86e32SChris Williamson static kmem_cache_t *hdr_l2only_cache; 1328fa9e4066Sahrens static kmem_cache_t *buf_cache; 1329fa9e4066Sahrens 1330fa9e4066Sahrens static void 1331fa9e4066Sahrens buf_fini(void) 1332fa9e4066Sahrens { 1333fa9e4066Sahrens int i; 1334fa9e4066Sahrens 1335fa9e4066Sahrens kmem_free(buf_hash_table.ht_table, 1336fa9e4066Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1337fa9e4066Sahrens for (i = 0; i < BUF_LOCKS; i++) 1338fa9e4066Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 133989c86e32SChris Williamson kmem_cache_destroy(hdr_full_cache); 134089c86e32SChris Williamson kmem_cache_destroy(hdr_l2only_cache); 1341fa9e4066Sahrens kmem_cache_destroy(buf_cache); 1342fa9e4066Sahrens } 1343fa9e4066Sahrens 1344fa9e4066Sahrens /* 1345fa9e4066Sahrens * Constructor callback - called when the cache is empty 1346fa9e4066Sahrens * and a new buf is requested. 1347fa9e4066Sahrens */ 1348fa9e4066Sahrens /* ARGSUSED */ 1349fa9e4066Sahrens static int 135089c86e32SChris Williamson hdr_full_cons(void *vbuf, void *unused, int kmflag) 1351fa9e4066Sahrens { 13527adb730bSGeorge Wilson arc_buf_hdr_t *hdr = vbuf; 1353fa9e4066Sahrens 135489c86e32SChris Williamson bzero(hdr, HDR_FULL_SIZE); 135589c86e32SChris Williamson cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); 1356e914ace2STim Schumacher zfs_refcount_create(&hdr->b_l1hdr.b_refcnt); 135789c86e32SChris Williamson mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1358244781f1SPrakash Surya multilist_link_init(&hdr->b_l1hdr.b_arc_node); 135989c86e32SChris Williamson arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); 136089c86e32SChris Williamson 136189c86e32SChris Williamson return (0); 136289c86e32SChris Williamson } 136389c86e32SChris Williamson 136489c86e32SChris Williamson /* ARGSUSED */ 136589c86e32SChris Williamson static int 136689c86e32SChris Williamson hdr_l2only_cons(void *vbuf, void *unused, int kmflag) 136789c86e32SChris Williamson { 136889c86e32SChris Williamson arc_buf_hdr_t *hdr = vbuf; 136989c86e32SChris Williamson 137089c86e32SChris Williamson bzero(hdr, HDR_L2ONLY_SIZE); 137189c86e32SChris Williamson arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1372fa94a07fSbrendan 1373fa9e4066Sahrens return (0); 1374fa9e4066Sahrens } 1375fa9e4066Sahrens 13766f83844dSMark Maybee /* ARGSUSED */ 13776f83844dSMark Maybee static int 13786f83844dSMark Maybee buf_cons(void *vbuf, void *unused, int kmflag) 13796f83844dSMark Maybee { 13806f83844dSMark Maybee arc_buf_t *buf = vbuf; 13816f83844dSMark Maybee 13826f83844dSMark Maybee bzero(buf, sizeof (arc_buf_t)); 13833f9d6ad7SLin Ling mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 13845a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 13855a98e54bSBrendan Gregg - Sun Microsystems 13866f83844dSMark Maybee return (0); 13876f83844dSMark Maybee } 13886f83844dSMark Maybee 1389fa9e4066Sahrens /* 1390fa9e4066Sahrens * Destructor callback - called when a cached buf is 1391fa9e4066Sahrens * no longer required. 1392fa9e4066Sahrens */ 1393fa9e4066Sahrens /* ARGSUSED */ 1394fa9e4066Sahrens static void 139589c86e32SChris Williamson hdr_full_dest(void *vbuf, void *unused) 139689c86e32SChris Williamson { 139789c86e32SChris Williamson arc_buf_hdr_t *hdr = vbuf; 139889c86e32SChris Williamson 1399dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 140089c86e32SChris Williamson cv_destroy(&hdr->b_l1hdr.b_cv); 1401e914ace2STim Schumacher zfs_refcount_destroy(&hdr->b_l1hdr.b_refcnt); 140289c86e32SChris Williamson mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); 1403244781f1SPrakash Surya ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 140489c86e32SChris Williamson arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); 140589c86e32SChris Williamson } 140689c86e32SChris Williamson 140789c86e32SChris Williamson /* ARGSUSED */ 140889c86e32SChris Williamson static void 140989c86e32SChris Williamson hdr_l2only_dest(void *vbuf, void *unused) 1410fa9e4066Sahrens { 14117adb730bSGeorge Wilson arc_buf_hdr_t *hdr = vbuf; 1412fa9e4066Sahrens 1413dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 141489c86e32SChris Williamson arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1415fa9e4066Sahrens } 1416fa9e4066Sahrens 14176f83844dSMark Maybee /* ARGSUSED */ 14186f83844dSMark Maybee static void 14196f83844dSMark Maybee buf_dest(void *vbuf, void *unused) 14206f83844dSMark Maybee { 14216f83844dSMark Maybee arc_buf_t *buf = vbuf; 14226f83844dSMark Maybee 14233f9d6ad7SLin Ling mutex_destroy(&buf->b_evict_lock); 14245a98e54bSBrendan Gregg - Sun Microsystems arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 14256f83844dSMark Maybee } 14266f83844dSMark Maybee 1427fa9e4066Sahrens /* 1428fa9e4066Sahrens * Reclaim callback -- invoked when memory is low. 1429fa9e4066Sahrens */ 1430fa9e4066Sahrens /* ARGSUSED */ 1431fa9e4066Sahrens static void 1432fa9e4066Sahrens hdr_recl(void *unused) 1433fa9e4066Sahrens { 1434fa9e4066Sahrens dprintf("hdr_recl called\n"); 143549e3519aSmaybee /* 143649e3519aSmaybee * umem calls the reclaim func when we destroy the buf cache, 143749e3519aSmaybee * which is after we do arc_fini(). 143849e3519aSmaybee */ 1439de753e34SBrad Lewis if (arc_initialized) 1440de753e34SBrad Lewis zthr_wakeup(arc_reap_zthr); 1441fa9e4066Sahrens } 1442fa9e4066Sahrens 1443fa9e4066Sahrens static void 1444fa9e4066Sahrens buf_init(void) 1445fa9e4066Sahrens { 1446fa9e4066Sahrens uint64_t *ct; 1447ea8dc4b6Seschrock uint64_t hsize = 1ULL << 12; 1448fa9e4066Sahrens int i, j; 1449fa9e4066Sahrens 1450fa9e4066Sahrens /* 1451fa9e4066Sahrens * The hash table is big enough to fill all of physical memory 145263e911b6SMatthew Ahrens * with an average block size of zfs_arc_average_blocksize (default 8K). 145363e911b6SMatthew Ahrens * By default, the table will take up 145463e911b6SMatthew Ahrens * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1455fa9e4066Sahrens */ 145663e911b6SMatthew Ahrens while (hsize * zfs_arc_average_blocksize < physmem * PAGESIZE) 1457fa9e4066Sahrens hsize <<= 1; 1458ea8dc4b6Seschrock retry: 1459fa9e4066Sahrens buf_hash_table.ht_mask = hsize - 1; 1460ea8dc4b6Seschrock buf_hash_table.ht_table = 1461ea8dc4b6Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1462ea8dc4b6Seschrock if (buf_hash_table.ht_table == NULL) { 1463ea8dc4b6Seschrock ASSERT(hsize > (1ULL << 8)); 1464ea8dc4b6Seschrock hsize >>= 1; 1465ea8dc4b6Seschrock goto retry; 1466ea8dc4b6Seschrock } 1467fa9e4066Sahrens 146889c86e32SChris Williamson hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, 146989c86e32SChris Williamson 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0); 147089c86e32SChris Williamson hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", 147189c86e32SChris Williamson HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl, 147289c86e32SChris Williamson NULL, NULL, 0); 1473fa9e4066Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 14746f83844dSMark Maybee 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1475fa9e4066Sahrens 1476fa9e4066Sahrens for (i = 0; i < 256; i++) 1477fa9e4066Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1478fa9e4066Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1479fa9e4066Sahrens 1480fa9e4066Sahrens for (i = 0; i < BUF_LOCKS; i++) { 1481fa9e4066Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1482fa9e4066Sahrens NULL, MUTEX_DEFAULT, NULL); 1483fa9e4066Sahrens } 1484fa9e4066Sahrens } 1485fa9e4066Sahrens 14865602294fSDan Kimmel /* 14875602294fSDan Kimmel * This is the size that the buf occupies in memory. If the buf is compressed, 14885602294fSDan Kimmel * it will correspond to the compressed size. You should use this method of 14895602294fSDan Kimmel * getting the buf size unless you explicitly need the logical size. 14905602294fSDan Kimmel */ 14915602294fSDan Kimmel int32_t 14925602294fSDan Kimmel arc_buf_size(arc_buf_t *buf) 14935602294fSDan Kimmel { 14945602294fSDan Kimmel return (ARC_BUF_COMPRESSED(buf) ? 14955602294fSDan Kimmel HDR_GET_PSIZE(buf->b_hdr) : HDR_GET_LSIZE(buf->b_hdr)); 14965602294fSDan Kimmel } 14975602294fSDan Kimmel 14985602294fSDan Kimmel int32_t 14995602294fSDan Kimmel arc_buf_lsize(arc_buf_t *buf) 15005602294fSDan Kimmel { 15015602294fSDan Kimmel return (HDR_GET_LSIZE(buf->b_hdr)); 15025602294fSDan Kimmel } 15035602294fSDan Kimmel 15045602294fSDan Kimmel enum zio_compress 15055602294fSDan Kimmel arc_get_compression(arc_buf_t *buf) 15065602294fSDan Kimmel { 15075602294fSDan Kimmel return (ARC_BUF_COMPRESSED(buf) ? 15085602294fSDan Kimmel HDR_GET_COMPRESS(buf->b_hdr) : ZIO_COMPRESS_OFF); 15095602294fSDan Kimmel } 15105602294fSDan Kimmel 1511dcbf3bd6SGeorge Wilson #define ARC_MINTIME (hz>>4) /* 62 ms */ 1512244781f1SPrakash Surya 1513dcbf3bd6SGeorge Wilson static inline boolean_t 1514dcbf3bd6SGeorge Wilson arc_buf_is_shared(arc_buf_t *buf) 1515dcbf3bd6SGeorge Wilson { 1516dcbf3bd6SGeorge Wilson boolean_t shared = (buf->b_data != NULL && 1517770499e1SDan Kimmel buf->b_hdr->b_l1hdr.b_pabd != NULL && 1518770499e1SDan Kimmel abd_is_linear(buf->b_hdr->b_l1hdr.b_pabd) && 1519770499e1SDan Kimmel buf->b_data == abd_to_buf(buf->b_hdr->b_l1hdr.b_pabd)); 1520dcbf3bd6SGeorge Wilson IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr)); 15215602294fSDan Kimmel IMPLY(shared, ARC_BUF_SHARED(buf)); 15225602294fSDan Kimmel IMPLY(shared, ARC_BUF_COMPRESSED(buf) || ARC_BUF_LAST(buf)); 15235602294fSDan Kimmel 15245602294fSDan Kimmel /* 15255602294fSDan Kimmel * It would be nice to assert arc_can_share() too, but the "hdr isn't 15265602294fSDan Kimmel * already being shared" requirement prevents us from doing that. 15275602294fSDan Kimmel */ 15285602294fSDan Kimmel 1529dcbf3bd6SGeorge Wilson return (shared); 1530dcbf3bd6SGeorge Wilson } 1531c546f36aSArne Jansen 15325602294fSDan Kimmel /* 15335602294fSDan Kimmel * Free the checksum associated with this header. If there is no checksum, this 15345602294fSDan Kimmel * is a no-op. 15355602294fSDan Kimmel */ 1536dcbf3bd6SGeorge Wilson static inline void 1537dcbf3bd6SGeorge Wilson arc_cksum_free(arc_buf_hdr_t *hdr) 1538dcbf3bd6SGeorge Wilson { 1539dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1540dcbf3bd6SGeorge Wilson mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1541dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_freeze_cksum != NULL) { 1542dcbf3bd6SGeorge Wilson kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t)); 1543dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_freeze_cksum = NULL; 154489c86e32SChris Williamson } 1545dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 154689c86e32SChris Williamson } 154789c86e32SChris Williamson 15485602294fSDan Kimmel /* 15495602294fSDan Kimmel * Return true iff at least one of the bufs on hdr is not compressed. 15505602294fSDan Kimmel */ 15515602294fSDan Kimmel static boolean_t 15525602294fSDan Kimmel arc_hdr_has_uncompressed_buf(arc_buf_hdr_t *hdr) 15535602294fSDan Kimmel { 15545602294fSDan Kimmel for (arc_buf_t *b = hdr->b_l1hdr.b_buf; b != NULL; b = b->b_next) { 15555602294fSDan Kimmel if (!ARC_BUF_COMPRESSED(b)) { 15565602294fSDan Kimmel return (B_TRUE); 15575602294fSDan Kimmel } 15585602294fSDan Kimmel } 15595602294fSDan Kimmel return (B_FALSE); 15605602294fSDan Kimmel } 15615602294fSDan Kimmel 15625602294fSDan Kimmel /* 15635602294fSDan Kimmel * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data 15645602294fSDan Kimmel * matches the checksum that is stored in the hdr. If there is no checksum, 15655602294fSDan Kimmel * or if the buf is compressed, this is a no-op. 15665602294fSDan Kimmel */ 15676b4acc8bSahrens static void 15686b4acc8bSahrens arc_cksum_verify(arc_buf_t *buf) 15696b4acc8bSahrens { 1570dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 15716b4acc8bSahrens zio_cksum_t zc; 15726b4acc8bSahrens 1573cc60fd72Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 15746b4acc8bSahrens return; 15756b4acc8bSahrens 15765602294fSDan Kimmel if (ARC_BUF_COMPRESSED(buf)) { 15775602294fSDan Kimmel ASSERT(hdr->b_l1hdr.b_freeze_cksum == NULL || 15785602294fSDan Kimmel arc_hdr_has_uncompressed_buf(hdr)); 15795602294fSDan Kimmel return; 15805602294fSDan Kimmel } 15815602294fSDan Kimmel 1582dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1583dcbf3bd6SGeorge Wilson 1584dcbf3bd6SGeorge Wilson mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1585dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) { 1586dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 15876b4acc8bSahrens return; 15886b4acc8bSahrens } 15895602294fSDan Kimmel 15905602294fSDan Kimmel fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, &zc); 1591dcbf3bd6SGeorge Wilson if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc)) 15926b4acc8bSahrens panic("buffer modified while frozen!"); 1593dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 15946b4acc8bSahrens } 15956b4acc8bSahrens 1596dcbf3bd6SGeorge Wilson static boolean_t 1597dcbf3bd6SGeorge Wilson arc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio) 1598fa94a07fSbrendan { 1599dcbf3bd6SGeorge Wilson enum zio_compress compress = BP_GET_COMPRESS(zio->io_bp); 1600dcbf3bd6SGeorge Wilson boolean_t valid_cksum; 1601fa94a07fSbrendan 1602dcbf3bd6SGeorge Wilson ASSERT(!BP_IS_EMBEDDED(zio->io_bp)); 1603dcbf3bd6SGeorge Wilson VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr)); 1604dcbf3bd6SGeorge Wilson 1605dcbf3bd6SGeorge Wilson /* 1606dcbf3bd6SGeorge Wilson * We rely on the blkptr's checksum to determine if the block 1607dcbf3bd6SGeorge Wilson * is valid or not. When compressed arc is enabled, the l2arc 1608dcbf3bd6SGeorge Wilson * writes the block to the l2arc just as it appears in the pool. 1609dcbf3bd6SGeorge Wilson * This allows us to use the blkptr's checksum to validate the 1610dcbf3bd6SGeorge Wilson * data that we just read off of the l2arc without having to store 1611dcbf3bd6SGeorge Wilson * a separate checksum in the arc_buf_hdr_t. However, if compressed 1612dcbf3bd6SGeorge Wilson * arc is disabled, then the data written to the l2arc is always 1613dcbf3bd6SGeorge Wilson * uncompressed and won't match the block as it exists in the main 1614dcbf3bd6SGeorge Wilson * pool. When this is the case, we must first compress it if it is 1615dcbf3bd6SGeorge Wilson * compressed on the main pool before we can validate the checksum. 1616dcbf3bd6SGeorge Wilson */ 1617dcbf3bd6SGeorge Wilson if (!HDR_COMPRESSION_ENABLED(hdr) && compress != ZIO_COMPRESS_OFF) { 1618dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 1619dcbf3bd6SGeorge Wilson uint64_t lsize = HDR_GET_LSIZE(hdr); 1620dcbf3bd6SGeorge Wilson uint64_t csize; 1621dcbf3bd6SGeorge Wilson 162201a059eeSRoman Strashkin abd_t *cdata = abd_alloc_linear(HDR_GET_PSIZE(hdr), B_TRUE); 162301a059eeSRoman Strashkin csize = zio_compress_data(compress, zio->io_abd, 162401a059eeSRoman Strashkin abd_to_buf(cdata), lsize); 1625770499e1SDan Kimmel 1626dcbf3bd6SGeorge Wilson ASSERT3U(csize, <=, HDR_GET_PSIZE(hdr)); 1627dcbf3bd6SGeorge Wilson if (csize < HDR_GET_PSIZE(hdr)) { 1628dcbf3bd6SGeorge Wilson /* 1629dcbf3bd6SGeorge Wilson * Compressed blocks are always a multiple of the 1630dcbf3bd6SGeorge Wilson * smallest ashift in the pool. Ideally, we would 1631dcbf3bd6SGeorge Wilson * like to round up the csize to the next 1632dcbf3bd6SGeorge Wilson * spa_min_ashift but that value may have changed 1633dcbf3bd6SGeorge Wilson * since the block was last written. Instead, 1634dcbf3bd6SGeorge Wilson * we rely on the fact that the hdr's psize 1635dcbf3bd6SGeorge Wilson * was set to the psize of the block when it was 1636dcbf3bd6SGeorge Wilson * last written. We set the csize to that value 1637dcbf3bd6SGeorge Wilson * and zero out any part that should not contain 1638dcbf3bd6SGeorge Wilson * data. 1639dcbf3bd6SGeorge Wilson */ 164001a059eeSRoman Strashkin abd_zero_off(cdata, csize, HDR_GET_PSIZE(hdr) - csize); 1641dcbf3bd6SGeorge Wilson csize = HDR_GET_PSIZE(hdr); 1642dcbf3bd6SGeorge Wilson } 164301a059eeSRoman Strashkin zio_push_transform(zio, cdata, csize, HDR_GET_PSIZE(hdr), NULL); 1644dcbf3bd6SGeorge Wilson } 1645fa94a07fSbrendan 1646dcbf3bd6SGeorge Wilson /* 1647dcbf3bd6SGeorge Wilson * Block pointers always store the checksum for the logical data. 1648dcbf3bd6SGeorge Wilson * If the block pointer has the gang bit set, then the checksum 1649dcbf3bd6SGeorge Wilson * it represents is for the reconstituted data and not for an 1650dcbf3bd6SGeorge Wilson * individual gang member. The zio pipeline, however, must be able to 1651dcbf3bd6SGeorge Wilson * determine the checksum of each of the gang constituents so it 1652dcbf3bd6SGeorge Wilson * treats the checksum comparison differently than what we need 1653dcbf3bd6SGeorge Wilson * for l2arc blocks. This prevents us from using the 1654dcbf3bd6SGeorge Wilson * zio_checksum_error() interface directly. Instead we must call the 1655dcbf3bd6SGeorge Wilson * zio_checksum_error_impl() so that we can ensure the checksum is 1656dcbf3bd6SGeorge Wilson * generated using the correct checksum algorithm and accounts for the 1657dcbf3bd6SGeorge Wilson * logical I/O size and not just a gang fragment. 1658dcbf3bd6SGeorge Wilson */ 1659dcbf3bd6SGeorge Wilson valid_cksum = (zio_checksum_error_impl(zio->io_spa, zio->io_bp, 1660770499e1SDan Kimmel BP_GET_CHECKSUM(zio->io_bp), zio->io_abd, zio->io_size, 1661dcbf3bd6SGeorge Wilson zio->io_offset, NULL) == 0); 1662dcbf3bd6SGeorge Wilson zio_pop_transforms(zio); 1663dcbf3bd6SGeorge Wilson return (valid_cksum); 1664fa94a07fSbrendan } 1665fa94a07fSbrendan 16665602294fSDan Kimmel /* 16675602294fSDan Kimmel * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a 16685602294fSDan Kimmel * checksum and attaches it to the buf's hdr so that we can ensure that the buf 16695602294fSDan Kimmel * isn't modified later on. If buf is compressed or there is already a checksum 16705602294fSDan Kimmel * on the hdr, this is a no-op (we only checksum uncompressed bufs). 16715602294fSDan Kimmel */ 16726b4acc8bSahrens static void 1673dcbf3bd6SGeorge Wilson arc_cksum_compute(arc_buf_t *buf) 16746b4acc8bSahrens { 1675dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 1676dcbf3bd6SGeorge Wilson 1677dcbf3bd6SGeorge Wilson if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 16786b4acc8bSahrens return; 16796b4acc8bSahrens 1680dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 16815602294fSDan Kimmel 168289c86e32SChris Williamson mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1683dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_freeze_cksum != NULL) { 16845602294fSDan Kimmel ASSERT(arc_hdr_has_uncompressed_buf(hdr)); 16855602294fSDan Kimmel mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 16865602294fSDan Kimmel return; 16875602294fSDan Kimmel } else if (ARC_BUF_COMPRESSED(buf)) { 1688dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 16896b4acc8bSahrens return; 16906b4acc8bSahrens } 16915602294fSDan Kimmel 16925602294fSDan Kimmel ASSERT(!ARC_BUF_COMPRESSED(buf)); 1693dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), 1694dcbf3bd6SGeorge Wilson KM_SLEEP); 16955602294fSDan Kimmel fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, 1696dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_freeze_cksum); 1697dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1698cd1c8b85SMatthew Ahrens arc_buf_watch(buf); 1699cd1c8b85SMatthew Ahrens } 1700cd1c8b85SMatthew Ahrens 1701cd1c8b85SMatthew Ahrens #ifndef _KERNEL 1702cd1c8b85SMatthew Ahrens typedef struct procctl { 1703cd1c8b85SMatthew Ahrens long cmd; 1704cd1c8b85SMatthew Ahrens prwatch_t prwatch; 1705cd1c8b85SMatthew Ahrens } procctl_t; 1706cd1c8b85SMatthew Ahrens #endif 1707cd1c8b85SMatthew Ahrens 1708cd1c8b85SMatthew Ahrens /* ARGSUSED */ 1709cd1c8b85SMatthew Ahrens static void 1710cd1c8b85SMatthew Ahrens arc_buf_unwatch(arc_buf_t *buf) 1711cd1c8b85SMatthew Ahrens { 1712cd1c8b85SMatthew Ahrens #ifndef _KERNEL 1713cd1c8b85SMatthew Ahrens if (arc_watch) { 1714cd1c8b85SMatthew Ahrens int result; 1715cd1c8b85SMatthew Ahrens procctl_t ctl; 1716cd1c8b85SMatthew Ahrens ctl.cmd = PCWATCH; 1717cd1c8b85SMatthew Ahrens ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1718cd1c8b85SMatthew Ahrens ctl.prwatch.pr_size = 0; 1719cd1c8b85SMatthew Ahrens ctl.prwatch.pr_wflags = 0; 1720cd1c8b85SMatthew Ahrens result = write(arc_procfd, &ctl, sizeof (ctl)); 1721cd1c8b85SMatthew Ahrens ASSERT3U(result, ==, sizeof (ctl)); 1722cd1c8b85SMatthew Ahrens } 1723cd1c8b85SMatthew Ahrens #endif 1724cd1c8b85SMatthew Ahrens } 1725cd1c8b85SMatthew Ahrens 1726cd1c8b85SMatthew Ahrens /* ARGSUSED */ 1727cd1c8b85SMatthew Ahrens static void 1728cd1c8b85SMatthew Ahrens arc_buf_watch(arc_buf_t *buf) 1729cd1c8b85SMatthew Ahrens { 1730cd1c8b85SMatthew Ahrens #ifndef _KERNEL 1731cd1c8b85SMatthew Ahrens if (arc_watch) { 1732cd1c8b85SMatthew Ahrens int result; 1733cd1c8b85SMatthew Ahrens procctl_t ctl; 1734cd1c8b85SMatthew Ahrens ctl.cmd = PCWATCH; 1735cd1c8b85SMatthew Ahrens ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 17365602294fSDan Kimmel ctl.prwatch.pr_size = arc_buf_size(buf); 1737cd1c8b85SMatthew Ahrens ctl.prwatch.pr_wflags = WA_WRITE; 1738cd1c8b85SMatthew Ahrens result = write(arc_procfd, &ctl, sizeof (ctl)); 1739cd1c8b85SMatthew Ahrens ASSERT3U(result, ==, sizeof (ctl)); 1740cd1c8b85SMatthew Ahrens } 1741cd1c8b85SMatthew Ahrens #endif 17426b4acc8bSahrens } 17436b4acc8bSahrens 174489c86e32SChris Williamson static arc_buf_contents_t 174589c86e32SChris Williamson arc_buf_type(arc_buf_hdr_t *hdr) 174689c86e32SChris Williamson { 1747dcbf3bd6SGeorge Wilson arc_buf_contents_t type; 174889c86e32SChris Williamson if (HDR_ISTYPE_METADATA(hdr)) { 1749dcbf3bd6SGeorge Wilson type = ARC_BUFC_METADATA; 175089c86e32SChris Williamson } else { 1751dcbf3bd6SGeorge Wilson type = ARC_BUFC_DATA; 175289c86e32SChris Williamson } 1753dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 1754dcbf3bd6SGeorge Wilson return (type); 175589c86e32SChris Williamson } 175689c86e32SChris Williamson 17575602294fSDan Kimmel boolean_t 17585602294fSDan Kimmel arc_is_metadata(arc_buf_t *buf) 17595602294fSDan Kimmel { 17605602294fSDan Kimmel return (HDR_ISTYPE_METADATA(buf->b_hdr) != 0); 17615602294fSDan Kimmel } 17625602294fSDan Kimmel 176389c86e32SChris Williamson static uint32_t 176489c86e32SChris Williamson arc_bufc_to_flags(arc_buf_contents_t type) 176589c86e32SChris Williamson { 176689c86e32SChris Williamson switch (type) { 176789c86e32SChris Williamson case ARC_BUFC_DATA: 176889c86e32SChris Williamson /* metadata field is 0 if buffer contains normal data */ 176989c86e32SChris Williamson return (0); 177089c86e32SChris Williamson case ARC_BUFC_METADATA: 177189c86e32SChris Williamson return (ARC_FLAG_BUFC_METADATA); 177289c86e32SChris Williamson default: 177389c86e32SChris Williamson break; 177489c86e32SChris Williamson } 177589c86e32SChris Williamson panic("undefined ARC buffer type!"); 177689c86e32SChris Williamson return ((uint32_t)-1); 177789c86e32SChris Williamson } 177889c86e32SChris Williamson 17796b4acc8bSahrens void 17806b4acc8bSahrens arc_buf_thaw(arc_buf_t *buf) 17816b4acc8bSahrens { 1782dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 1783dcbf3bd6SGeorge Wilson 17845602294fSDan Kimmel ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 17855602294fSDan Kimmel ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 17865602294fSDan Kimmel 17875602294fSDan Kimmel arc_cksum_verify(buf); 17885602294fSDan Kimmel 17895602294fSDan Kimmel /* 17905602294fSDan Kimmel * Compressed buffers do not manipulate the b_freeze_cksum or 17915602294fSDan Kimmel * allocate b_thawed. 17925602294fSDan Kimmel */ 17935602294fSDan Kimmel if (ARC_BUF_COMPRESSED(buf)) { 17945602294fSDan Kimmel ASSERT(hdr->b_l1hdr.b_freeze_cksum == NULL || 17955602294fSDan Kimmel arc_hdr_has_uncompressed_buf(hdr)); 17965602294fSDan Kimmel return; 1797fa94a07fSbrendan } 17986b4acc8bSahrens 1799dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1800dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 18013f9d6ad7SLin Ling 1802dcbf3bd6SGeorge Wilson mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 180389c86e32SChris Williamson #ifdef ZFS_DEBUG 18043f9d6ad7SLin Ling if (zfs_flags & ZFS_DEBUG_MODIFY) { 1805dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_thawed != NULL) 1806dcbf3bd6SGeorge Wilson kmem_free(hdr->b_l1hdr.b_thawed, 1); 1807dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_thawed = kmem_alloc(1, KM_SLEEP); 18083f9d6ad7SLin Ling } 180989c86e32SChris Williamson #endif 18103f9d6ad7SLin Ling 1811dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1812cd1c8b85SMatthew Ahrens 1813cd1c8b85SMatthew Ahrens arc_buf_unwatch(buf); 18146b4acc8bSahrens } 18156b4acc8bSahrens 18166b4acc8bSahrens void 18176b4acc8bSahrens arc_buf_freeze(arc_buf_t *buf) 18186b4acc8bSahrens { 1819dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 18203f9d6ad7SLin Ling kmutex_t *hash_lock; 18213f9d6ad7SLin Ling 1822cc60fd72Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1823cc60fd72Sahrens return; 1824cc60fd72Sahrens 18255602294fSDan Kimmel if (ARC_BUF_COMPRESSED(buf)) { 18265602294fSDan Kimmel ASSERT(hdr->b_l1hdr.b_freeze_cksum == NULL || 18275602294fSDan Kimmel arc_hdr_has_uncompressed_buf(hdr)); 18285602294fSDan Kimmel return; 18295602294fSDan Kimmel } 18305602294fSDan Kimmel 1831dcbf3bd6SGeorge Wilson hash_lock = HDR_LOCK(hdr); 18323f9d6ad7SLin Ling mutex_enter(hash_lock); 18333f9d6ad7SLin Ling 1834dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1835dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_freeze_cksum != NULL || 1836dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_state == arc_anon); 1837dcbf3bd6SGeorge Wilson arc_cksum_compute(buf); 18383f9d6ad7SLin Ling mutex_exit(hash_lock); 18396b4acc8bSahrens } 18406b4acc8bSahrens 1841dcbf3bd6SGeorge Wilson /* 1842dcbf3bd6SGeorge Wilson * The arc_buf_hdr_t's b_flags should never be modified directly. Instead, 1843dcbf3bd6SGeorge Wilson * the following functions should be used to ensure that the flags are 1844dcbf3bd6SGeorge Wilson * updated in a thread-safe way. When manipulating the flags either 1845dcbf3bd6SGeorge Wilson * the hash_lock must be held or the hdr must be undiscoverable. This 1846dcbf3bd6SGeorge Wilson * ensures that we're not racing with any other threads when updating 1847dcbf3bd6SGeorge Wilson * the flags. 1848dcbf3bd6SGeorge Wilson */ 1849dcbf3bd6SGeorge Wilson static inline void 1850dcbf3bd6SGeorge Wilson arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) 1851dcbf3bd6SGeorge Wilson { 1852dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 1853dcbf3bd6SGeorge Wilson hdr->b_flags |= flags; 1854dcbf3bd6SGeorge Wilson } 1855dcbf3bd6SGeorge Wilson 1856dcbf3bd6SGeorge Wilson static inline void 1857dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) 1858dcbf3bd6SGeorge Wilson { 1859dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 1860dcbf3bd6SGeorge Wilson hdr->b_flags &= ~flags; 1861dcbf3bd6SGeorge Wilson } 1862dcbf3bd6SGeorge Wilson 1863dcbf3bd6SGeorge Wilson /* 1864dcbf3bd6SGeorge Wilson * Setting the compression bits in the arc_buf_hdr_t's b_flags is 1865dcbf3bd6SGeorge Wilson * done in a special way since we have to clear and set bits 1866dcbf3bd6SGeorge Wilson * at the same time. Consumers that wish to set the compression bits 1867dcbf3bd6SGeorge Wilson * must use this function to ensure that the flags are updated in 1868dcbf3bd6SGeorge Wilson * thread-safe manner. 1869dcbf3bd6SGeorge Wilson */ 1870fa9e4066Sahrens static void 1871dcbf3bd6SGeorge Wilson arc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp) 1872fa9e4066Sahrens { 1873dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 1874fa9e4066Sahrens 1875dcbf3bd6SGeorge Wilson /* 1876dcbf3bd6SGeorge Wilson * Holes and embedded blocks will always have a psize = 0 so 1877dcbf3bd6SGeorge Wilson * we ignore the compression of the blkptr and set the 1878dcbf3bd6SGeorge Wilson * arc_buf_hdr_t's compression to ZIO_COMPRESS_OFF. 1879dcbf3bd6SGeorge Wilson * Holes and embedded blocks remain anonymous so we don't 1880dcbf3bd6SGeorge Wilson * want to uncompress them. Mark them as uncompressed. 1881dcbf3bd6SGeorge Wilson */ 1882dcbf3bd6SGeorge Wilson if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) { 1883dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC); 1884dcbf3bd6SGeorge Wilson HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); 1885dcbf3bd6SGeorge Wilson ASSERT(!HDR_COMPRESSION_ENABLED(hdr)); 1886dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 1887dcbf3bd6SGeorge Wilson } else { 1888dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC); 1889dcbf3bd6SGeorge Wilson HDR_SET_COMPRESS(hdr, cmp); 1890dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp); 1891dcbf3bd6SGeorge Wilson ASSERT(HDR_COMPRESSION_ENABLED(hdr)); 1892dcbf3bd6SGeorge Wilson } 1893dcbf3bd6SGeorge Wilson } 1894244781f1SPrakash Surya 18955602294fSDan Kimmel /* 18965602294fSDan Kimmel * Looks for another buf on the same hdr which has the data decompressed, copies 18975602294fSDan Kimmel * from it, and returns true. If no such buf exists, returns false. 18985602294fSDan Kimmel */ 18995602294fSDan Kimmel static boolean_t 19005602294fSDan Kimmel arc_buf_try_copy_decompressed_data(arc_buf_t *buf) 19015602294fSDan Kimmel { 19025602294fSDan Kimmel arc_buf_hdr_t *hdr = buf->b_hdr; 19035602294fSDan Kimmel boolean_t copied = B_FALSE; 19045602294fSDan Kimmel 19055602294fSDan Kimmel ASSERT(HDR_HAS_L1HDR(hdr)); 19065602294fSDan Kimmel ASSERT3P(buf->b_data, !=, NULL); 19075602294fSDan Kimmel ASSERT(!ARC_BUF_COMPRESSED(buf)); 19085602294fSDan Kimmel 19095602294fSDan Kimmel for (arc_buf_t *from = hdr->b_l1hdr.b_buf; from != NULL; 19105602294fSDan Kimmel from = from->b_next) { 19115602294fSDan Kimmel /* can't use our own data buffer */ 19125602294fSDan Kimmel if (from == buf) { 19135602294fSDan Kimmel continue; 19145602294fSDan Kimmel } 19155602294fSDan Kimmel 19165602294fSDan Kimmel if (!ARC_BUF_COMPRESSED(from)) { 19175602294fSDan Kimmel bcopy(from->b_data, buf->b_data, arc_buf_size(buf)); 19185602294fSDan Kimmel copied = B_TRUE; 19195602294fSDan Kimmel break; 19205602294fSDan Kimmel } 19215602294fSDan Kimmel } 19225602294fSDan Kimmel 19235602294fSDan Kimmel /* 19245602294fSDan Kimmel * There were no decompressed bufs, so there should not be a 19255602294fSDan Kimmel * checksum on the hdr either. 19265602294fSDan Kimmel */ 19275602294fSDan Kimmel EQUIV(!copied, hdr->b_l1hdr.b_freeze_cksum == NULL); 19285602294fSDan Kimmel 19295602294fSDan Kimmel return (copied); 19305602294fSDan Kimmel } 19315602294fSDan Kimmel 19325602294fSDan Kimmel /* 19335602294fSDan Kimmel * Given a buf that has a data buffer attached to it, this function will 19345602294fSDan Kimmel * efficiently fill the buf with data of the specified compression setting from 19355602294fSDan Kimmel * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr 19365602294fSDan Kimmel * are already sharing a data buf, no copy is performed. 19375602294fSDan Kimmel * 19385602294fSDan Kimmel * If the buf is marked as compressed but uncompressed data was requested, this 19395602294fSDan Kimmel * will allocate a new data buffer for the buf, remove that flag, and fill the 19405602294fSDan Kimmel * buf with uncompressed data. You can't request a compressed buf on a hdr with 19415602294fSDan Kimmel * uncompressed data, and (since we haven't added support for it yet) if you 19425602294fSDan Kimmel * want compressed data your buf must already be marked as compressed and have 19435602294fSDan Kimmel * the correct-sized data buffer. 19445602294fSDan Kimmel */ 1945dcbf3bd6SGeorge Wilson static int 19465602294fSDan Kimmel arc_buf_fill(arc_buf_t *buf, boolean_t compressed) 1947dcbf3bd6SGeorge Wilson { 1948dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 19495602294fSDan Kimmel boolean_t hdr_compressed = (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF); 1950dcbf3bd6SGeorge Wilson dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap; 195189c86e32SChris Williamson 19525602294fSDan Kimmel ASSERT3P(buf->b_data, !=, NULL); 19535602294fSDan Kimmel IMPLY(compressed, hdr_compressed); 19545602294fSDan Kimmel IMPLY(compressed, ARC_BUF_COMPRESSED(buf)); 19555602294fSDan Kimmel 19565602294fSDan Kimmel if (hdr_compressed == compressed) { 19575602294fSDan Kimmel if (!arc_buf_is_shared(buf)) { 1958770499e1SDan Kimmel abd_copy_to_buf(buf->b_data, hdr->b_l1hdr.b_pabd, 19595602294fSDan Kimmel arc_buf_size(buf)); 19605602294fSDan Kimmel } 1961dcbf3bd6SGeorge Wilson } else { 19625602294fSDan Kimmel ASSERT(hdr_compressed); 19635602294fSDan Kimmel ASSERT(!compressed); 1964dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), !=, HDR_GET_PSIZE(hdr)); 19655602294fSDan Kimmel 19665602294fSDan Kimmel /* 19675602294fSDan Kimmel * If the buf is sharing its data with the hdr, unlink it and 19685602294fSDan Kimmel * allocate a new data buffer for the buf. 19695602294fSDan Kimmel */ 19705602294fSDan Kimmel if (arc_buf_is_shared(buf)) { 19715602294fSDan Kimmel ASSERT(ARC_BUF_COMPRESSED(buf)); 19725602294fSDan Kimmel 19735602294fSDan Kimmel /* We need to give the buf it's own b_data */ 19745602294fSDan Kimmel buf->b_flags &= ~ARC_BUF_FLAG_SHARED; 19755602294fSDan Kimmel buf->b_data = 19765602294fSDan Kimmel arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); 19775602294fSDan Kimmel arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 19785602294fSDan Kimmel 19795602294fSDan Kimmel /* Previously overhead was 0; just add new overhead */ 19805602294fSDan Kimmel ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); 19815602294fSDan Kimmel } else if (ARC_BUF_COMPRESSED(buf)) { 19825602294fSDan Kimmel /* We need to reallocate the buf's b_data */ 19835602294fSDan Kimmel arc_free_data_buf(hdr, buf->b_data, HDR_GET_PSIZE(hdr), 19845602294fSDan Kimmel buf); 19855602294fSDan Kimmel buf->b_data = 19865602294fSDan Kimmel arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); 19875602294fSDan Kimmel 19885602294fSDan Kimmel /* We increased the size of b_data; update overhead */ 19895602294fSDan Kimmel ARCSTAT_INCR(arcstat_overhead_size, 19905602294fSDan Kimmel HDR_GET_LSIZE(hdr) - HDR_GET_PSIZE(hdr)); 19915602294fSDan Kimmel } 19925602294fSDan Kimmel 19935602294fSDan Kimmel /* 19945602294fSDan Kimmel * Regardless of the buf's previous compression settings, it 19955602294fSDan Kimmel * should not be compressed at the end of this function. 19965602294fSDan Kimmel */ 19975602294fSDan Kimmel buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; 19985602294fSDan Kimmel 19995602294fSDan Kimmel /* 20005602294fSDan Kimmel * Try copying the data from another buf which already has a 20015602294fSDan Kimmel * decompressed version. If that's not possible, it's time to 20025602294fSDan Kimmel * bite the bullet and decompress the data from the hdr. 20035602294fSDan Kimmel */ 20045602294fSDan Kimmel if (arc_buf_try_copy_decompressed_data(buf)) { 20055602294fSDan Kimmel /* Skip byteswapping and checksumming (already done) */ 20065602294fSDan Kimmel ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, !=, NULL); 20075602294fSDan Kimmel return (0); 20085602294fSDan Kimmel } else { 20095602294fSDan Kimmel int error = zio_decompress_data(HDR_GET_COMPRESS(hdr), 2010770499e1SDan Kimmel hdr->b_l1hdr.b_pabd, buf->b_data, 20115602294fSDan Kimmel HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); 20125602294fSDan Kimmel 20135602294fSDan Kimmel /* 20145602294fSDan Kimmel * Absent hardware errors or software bugs, this should 20155602294fSDan Kimmel * be impossible, but log it anyway so we can debug it. 20165602294fSDan Kimmel */ 20175602294fSDan Kimmel if (error != 0) { 20185602294fSDan Kimmel zfs_dbgmsg( 20195602294fSDan Kimmel "hdr %p, compress %d, psize %d, lsize %d", 20205602294fSDan Kimmel hdr, HDR_GET_COMPRESS(hdr), 20215602294fSDan Kimmel HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); 20225602294fSDan Kimmel return (SET_ERROR(EIO)); 20235602294fSDan Kimmel } 2024ea8dc4b6Seschrock } 2025fa9e4066Sahrens } 20265602294fSDan Kimmel 20275602294fSDan Kimmel /* Byteswap the buf's data if necessary */ 2028dcbf3bd6SGeorge Wilson if (bswap != DMU_BSWAP_NUMFUNCS) { 2029dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 2030dcbf3bd6SGeorge Wilson ASSERT3U(bswap, <, DMU_BSWAP_NUMFUNCS); 2031dcbf3bd6SGeorge Wilson dmu_ot_byteswap[bswap].ob_func(buf->b_data, HDR_GET_LSIZE(hdr)); 2032dcbf3bd6SGeorge Wilson } 20335602294fSDan Kimmel 20345602294fSDan Kimmel /* Compute the hdr's checksum if necessary */ 2035dcbf3bd6SGeorge Wilson arc_cksum_compute(buf); 20365602294fSDan Kimmel 2037dcbf3bd6SGeorge Wilson return (0); 2038fa9e4066Sahrens } 2039fa9e4066Sahrens 20405602294fSDan Kimmel int 20415602294fSDan Kimmel arc_decompress(arc_buf_t *buf) 20425602294fSDan Kimmel { 20435602294fSDan Kimmel return (arc_buf_fill(buf, B_FALSE)); 20445602294fSDan Kimmel } 20455602294fSDan Kimmel 2046dcbf3bd6SGeorge Wilson /* 2047770499e1SDan Kimmel * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t. 2048dcbf3bd6SGeorge Wilson */ 2049dcbf3bd6SGeorge Wilson static uint64_t 2050dcbf3bd6SGeorge Wilson arc_hdr_size(arc_buf_hdr_t *hdr) 2051fa9e4066Sahrens { 2052dcbf3bd6SGeorge Wilson uint64_t size; 2053fa9e4066Sahrens 2054dcbf3bd6SGeorge Wilson if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && 2055dcbf3bd6SGeorge Wilson HDR_GET_PSIZE(hdr) > 0) { 2056dcbf3bd6SGeorge Wilson size = HDR_GET_PSIZE(hdr); 2057dcbf3bd6SGeorge Wilson } else { 2058dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0); 2059dcbf3bd6SGeorge Wilson size = HDR_GET_LSIZE(hdr); 2060dcbf3bd6SGeorge Wilson } 2061dcbf3bd6SGeorge Wilson return (size); 2062dcbf3bd6SGeorge Wilson } 2063fa9e4066Sahrens 2064dcbf3bd6SGeorge Wilson /* 2065dcbf3bd6SGeorge Wilson * Increment the amount of evictable space in the arc_state_t's refcount. 2066dcbf3bd6SGeorge Wilson * We account for the space used by the hdr and the arc buf individually 2067dcbf3bd6SGeorge Wilson * so that we can add and remove them from the refcount individually. 2068dcbf3bd6SGeorge Wilson */ 2069dcbf3bd6SGeorge Wilson static void 2070dcbf3bd6SGeorge Wilson arc_evictable_space_increment(arc_buf_hdr_t *hdr, arc_state_t *state) 2071dcbf3bd6SGeorge Wilson { 2072dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 2073244781f1SPrakash Surya 2074dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 20750e8c6158Smaybee 2076dcbf3bd6SGeorge Wilson if (GHOST_STATE(state)) { 2077dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 2078dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2079770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2080e914ace2STim Schumacher (void) zfs_refcount_add_many(&state->arcs_esize[type], 20815602294fSDan Kimmel HDR_GET_LSIZE(hdr), hdr); 2082dcbf3bd6SGeorge Wilson return; 2083dcbf3bd6SGeorge Wilson } 2084dcbf3bd6SGeorge Wilson 2085dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(state)); 2086770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 2087e914ace2STim Schumacher (void) zfs_refcount_add_many(&state->arcs_esize[type], 2088dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 2089dcbf3bd6SGeorge Wilson } 2090dcbf3bd6SGeorge Wilson for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2091dcbf3bd6SGeorge Wilson buf = buf->b_next) { 20925602294fSDan Kimmel if (arc_buf_is_shared(buf)) 2093dcbf3bd6SGeorge Wilson continue; 2094e914ace2STim Schumacher (void) zfs_refcount_add_many(&state->arcs_esize[type], 20955602294fSDan Kimmel arc_buf_size(buf), buf); 2096fa9e4066Sahrens } 2097fa9e4066Sahrens } 2098fa9e4066Sahrens 2099fa9e4066Sahrens /* 2100dcbf3bd6SGeorge Wilson * Decrement the amount of evictable space in the arc_state_t's refcount. 2101dcbf3bd6SGeorge Wilson * We account for the space used by the hdr and the arc buf individually 2102dcbf3bd6SGeorge Wilson * so that we can add and remove them from the refcount individually. 2103fa9e4066Sahrens */ 2104fa9e4066Sahrens static void 21055602294fSDan Kimmel arc_evictable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state) 2106fa9e4066Sahrens { 2107dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 2108dcbf3bd6SGeorge Wilson 2109dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2110dcbf3bd6SGeorge Wilson 2111dcbf3bd6SGeorge Wilson if (GHOST_STATE(state)) { 2112dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 2113dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2114770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2115e914ace2STim Schumacher (void) zfs_refcount_remove_many(&state->arcs_esize[type], 21165602294fSDan Kimmel HDR_GET_LSIZE(hdr), hdr); 2117dcbf3bd6SGeorge Wilson return; 2118dcbf3bd6SGeorge Wilson } 2119dcbf3bd6SGeorge Wilson 2120dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(state)); 2121770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 2122e914ace2STim Schumacher (void) zfs_refcount_remove_many(&state->arcs_esize[type], 2123dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 2124dcbf3bd6SGeorge Wilson } 2125dcbf3bd6SGeorge Wilson for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2126dcbf3bd6SGeorge Wilson buf = buf->b_next) { 21275602294fSDan Kimmel if (arc_buf_is_shared(buf)) 2128dcbf3bd6SGeorge Wilson continue; 2129e914ace2STim Schumacher (void) zfs_refcount_remove_many(&state->arcs_esize[type], 21305602294fSDan Kimmel arc_buf_size(buf), buf); 2131dcbf3bd6SGeorge Wilson } 2132dcbf3bd6SGeorge Wilson } 2133dcbf3bd6SGeorge Wilson 2134dcbf3bd6SGeorge Wilson /* 2135dcbf3bd6SGeorge Wilson * Add a reference to this hdr indicating that someone is actively 2136dcbf3bd6SGeorge Wilson * referencing that memory. When the refcount transitions from 0 to 1, 2137dcbf3bd6SGeorge Wilson * we remove it from the respective arc_state_t list to indicate that 2138dcbf3bd6SGeorge Wilson * it is not evictable. 2139dcbf3bd6SGeorge Wilson */ 2140dcbf3bd6SGeorge Wilson static void 2141dcbf3bd6SGeorge Wilson add_reference(arc_buf_hdr_t *hdr, void *tag) 2142dcbf3bd6SGeorge Wilson { 2143dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2144dcbf3bd6SGeorge Wilson if (!MUTEX_HELD(HDR_LOCK(hdr))) { 2145dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_state == arc_anon); 2146e914ace2STim Schumacher ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2147dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2148dcbf3bd6SGeorge Wilson } 2149dcbf3bd6SGeorge Wilson 2150dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2151dcbf3bd6SGeorge Wilson 2152e914ace2STim Schumacher if ((zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && 2153dcbf3bd6SGeorge Wilson (state != arc_anon)) { 2154dcbf3bd6SGeorge Wilson /* We don't use the L2-only state list. */ 2155dcbf3bd6SGeorge Wilson if (state != arc_l2c_only) { 215694c2d0ebSMatthew Ahrens multilist_remove(state->arcs_list[arc_buf_type(hdr)], 2157dcbf3bd6SGeorge Wilson hdr); 21585602294fSDan Kimmel arc_evictable_space_decrement(hdr, state); 2159dcbf3bd6SGeorge Wilson } 2160dcbf3bd6SGeorge Wilson /* remove the prefetch flag if we get a reference */ 2161dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); 2162dcbf3bd6SGeorge Wilson } 2163dcbf3bd6SGeorge Wilson } 2164dcbf3bd6SGeorge Wilson 2165dcbf3bd6SGeorge Wilson /* 2166dcbf3bd6SGeorge Wilson * Remove a reference from this hdr. When the reference transitions from 2167dcbf3bd6SGeorge Wilson * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's 2168dcbf3bd6SGeorge Wilson * list making it eligible for eviction. 2169dcbf3bd6SGeorge Wilson */ 2170dcbf3bd6SGeorge Wilson static int 2171dcbf3bd6SGeorge Wilson remove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 2172dcbf3bd6SGeorge Wilson { 2173dcbf3bd6SGeorge Wilson int cnt; 2174dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2175dcbf3bd6SGeorge Wilson 2176dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2177dcbf3bd6SGeorge Wilson ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 2178dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(state)); 2179dcbf3bd6SGeorge Wilson 2180dcbf3bd6SGeorge Wilson /* 2181dcbf3bd6SGeorge Wilson * arc_l2c_only counts as a ghost state so we don't need to explicitly 2182dcbf3bd6SGeorge Wilson * check to prevent usage of the arc_l2c_only list. 2183dcbf3bd6SGeorge Wilson */ 2184e914ace2STim Schumacher if (((cnt = zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && 2185dcbf3bd6SGeorge Wilson (state != arc_anon)) { 218694c2d0ebSMatthew Ahrens multilist_insert(state->arcs_list[arc_buf_type(hdr)], hdr); 2187dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); 2188dcbf3bd6SGeorge Wilson arc_evictable_space_increment(hdr, state); 2189dcbf3bd6SGeorge Wilson } 2190dcbf3bd6SGeorge Wilson return (cnt); 2191dcbf3bd6SGeorge Wilson } 2192dcbf3bd6SGeorge Wilson 2193dcbf3bd6SGeorge Wilson /* 2194dcbf3bd6SGeorge Wilson * Move the supplied buffer to the indicated state. The hash lock 2195dcbf3bd6SGeorge Wilson * for the buffer must be held by the caller. 2196dcbf3bd6SGeorge Wilson */ 2197dcbf3bd6SGeorge Wilson static void 2198dcbf3bd6SGeorge Wilson arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, 2199dcbf3bd6SGeorge Wilson kmutex_t *hash_lock) 2200dcbf3bd6SGeorge Wilson { 2201dcbf3bd6SGeorge Wilson arc_state_t *old_state; 2202dcbf3bd6SGeorge Wilson int64_t refcnt; 2203dcbf3bd6SGeorge Wilson uint32_t bufcnt; 2204dcbf3bd6SGeorge Wilson boolean_t update_old, update_new; 2205dcbf3bd6SGeorge Wilson arc_buf_contents_t buftype = arc_buf_type(hdr); 220689c86e32SChris Williamson 220789c86e32SChris Williamson /* 220889c86e32SChris Williamson * We almost always have an L1 hdr here, since we call arc_hdr_realloc() 220989c86e32SChris Williamson * in arc_read() when bringing a buffer out of the L2ARC. However, the 221089c86e32SChris Williamson * L1 hdr doesn't always exist when we change state to arc_anon before 221189c86e32SChris Williamson * destroying a header, in which case reallocating to add the L1 hdr is 221289c86e32SChris Williamson * pointless. 221389c86e32SChris Williamson */ 221489c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) { 221589c86e32SChris Williamson old_state = hdr->b_l1hdr.b_state; 2216e914ace2STim Schumacher refcnt = zfs_refcount_count(&hdr->b_l1hdr.b_refcnt); 2217dcbf3bd6SGeorge Wilson bufcnt = hdr->b_l1hdr.b_bufcnt; 2218770499e1SDan Kimmel update_old = (bufcnt > 0 || hdr->b_l1hdr.b_pabd != NULL); 221989c86e32SChris Williamson } else { 222089c86e32SChris Williamson old_state = arc_l2c_only; 222189c86e32SChris Williamson refcnt = 0; 2222dcbf3bd6SGeorge Wilson bufcnt = 0; 2223dcbf3bd6SGeorge Wilson update_old = B_FALSE; 222489c86e32SChris Williamson } 2225dcbf3bd6SGeorge Wilson update_new = update_old; 2226fa9e4066Sahrens 2227fa9e4066Sahrens ASSERT(MUTEX_HELD(hash_lock)); 222869962b56SMatthew Ahrens ASSERT3P(new_state, !=, old_state); 2229dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(new_state) || bufcnt == 0); 2230dcbf3bd6SGeorge Wilson ASSERT(old_state != arc_anon || bufcnt <= 1); 2231fa9e4066Sahrens 2232fa9e4066Sahrens /* 2233fa9e4066Sahrens * If this buffer is evictable, transfer it from the 2234fa9e4066Sahrens * old state list to the new state list. 2235fa9e4066Sahrens */ 2236ea8dc4b6Seschrock if (refcnt == 0) { 223789c86e32SChris Williamson if (old_state != arc_anon && old_state != arc_l2c_only) { 223889c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 223994c2d0ebSMatthew Ahrens multilist_remove(old_state->arcs_list[buftype], hdr); 2240ea8dc4b6Seschrock 2241dcbf3bd6SGeorge Wilson if (GHOST_STATE(old_state)) { 2242dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 2243dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2244dcbf3bd6SGeorge Wilson update_old = B_TRUE; 2245ea8dc4b6Seschrock } 22465602294fSDan Kimmel arc_evictable_space_decrement(hdr, old_state); 2247fa9e4066Sahrens } 224889c86e32SChris Williamson if (new_state != arc_anon && new_state != arc_l2c_only) { 2249fa9e4066Sahrens 225089c86e32SChris Williamson /* 225189c86e32SChris Williamson * An L1 header always exists here, since if we're 225289c86e32SChris Williamson * moving to some L1-cached state (i.e. not l2c_only or 225389c86e32SChris Williamson * anonymous), we realloc the header to add an L1hdr 225489c86e32SChris Williamson * beforehand. 225589c86e32SChris Williamson */ 225689c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 225794c2d0ebSMatthew Ahrens multilist_insert(new_state->arcs_list[buftype], hdr); 2258ea8dc4b6Seschrock 2259ea8dc4b6Seschrock if (GHOST_STATE(new_state)) { 2260dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 2261dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2262dcbf3bd6SGeorge Wilson update_new = B_TRUE; 2263ea8dc4b6Seschrock } 2264dcbf3bd6SGeorge Wilson arc_evictable_space_increment(hdr, new_state); 2265fa9e4066Sahrens } 2266fa9e4066Sahrens } 2267fa9e4066Sahrens 2268dcbf3bd6SGeorge Wilson ASSERT(!HDR_EMPTY(hdr)); 22697adb730bSGeorge Wilson if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) 22707adb730bSGeorge Wilson buf_hash_remove(hdr); 2271fa9e4066Sahrens 227289c86e32SChris Williamson /* adjust state sizes (ignore arc_l2c_only) */ 22732fd872a7SPrakash Surya 2274dcbf3bd6SGeorge Wilson if (update_new && new_state != arc_l2c_only) { 22752fd872a7SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 22762fd872a7SPrakash Surya if (GHOST_STATE(new_state)) { 2277dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 22782fd872a7SPrakash Surya 22792fd872a7SPrakash Surya /* 2280dcbf3bd6SGeorge Wilson * When moving a header to a ghost state, we first 22812fd872a7SPrakash Surya * remove all arc buffers. Thus, we'll have a 2282dcbf3bd6SGeorge Wilson * bufcnt of zero, and no arc buffer to use for 22832fd872a7SPrakash Surya * the reference. As a result, we use the arc 22842fd872a7SPrakash Surya * header pointer for the reference. 22852fd872a7SPrakash Surya */ 2286e914ace2STim Schumacher (void) zfs_refcount_add_many(&new_state->arcs_size, 2287dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr), hdr); 2288770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 22892fd872a7SPrakash Surya } else { 2290dcbf3bd6SGeorge Wilson uint32_t buffers = 0; 22912fd872a7SPrakash Surya 22922fd872a7SPrakash Surya /* 22932fd872a7SPrakash Surya * Each individual buffer holds a unique reference, 22942fd872a7SPrakash Surya * thus we must remove each of these references one 22952fd872a7SPrakash Surya * at a time. 22962fd872a7SPrakash Surya */ 22972fd872a7SPrakash Surya for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 22982fd872a7SPrakash Surya buf = buf->b_next) { 2299dcbf3bd6SGeorge Wilson ASSERT3U(bufcnt, !=, 0); 2300dcbf3bd6SGeorge Wilson buffers++; 2301dcbf3bd6SGeorge Wilson 2302dcbf3bd6SGeorge Wilson /* 2303dcbf3bd6SGeorge Wilson * When the arc_buf_t is sharing the data 2304dcbf3bd6SGeorge Wilson * block with the hdr, the owner of the 2305dcbf3bd6SGeorge Wilson * reference belongs to the hdr. Only 2306dcbf3bd6SGeorge Wilson * add to the refcount if the arc_buf_t is 2307dcbf3bd6SGeorge Wilson * not shared. 2308dcbf3bd6SGeorge Wilson */ 23095602294fSDan Kimmel if (arc_buf_is_shared(buf)) 2310dcbf3bd6SGeorge Wilson continue; 2311dcbf3bd6SGeorge Wilson 2312e914ace2STim Schumacher (void) zfs_refcount_add_many( 2313e914ace2STim Schumacher &new_state->arcs_size, 23145602294fSDan Kimmel arc_buf_size(buf), buf); 2315dcbf3bd6SGeorge Wilson } 2316dcbf3bd6SGeorge Wilson ASSERT3U(bufcnt, ==, buffers); 2317dcbf3bd6SGeorge Wilson 2318770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 2319e914ace2STim Schumacher (void) zfs_refcount_add_many( 2320e914ace2STim Schumacher &new_state->arcs_size, 2321dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 2322dcbf3bd6SGeorge Wilson } else { 2323dcbf3bd6SGeorge Wilson ASSERT(GHOST_STATE(old_state)); 23242fd872a7SPrakash Surya } 23252fd872a7SPrakash Surya } 23262fd872a7SPrakash Surya } 23272fd872a7SPrakash Surya 2328dcbf3bd6SGeorge Wilson if (update_old && old_state != arc_l2c_only) { 23292fd872a7SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 23302fd872a7SPrakash Surya if (GHOST_STATE(old_state)) { 2331dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 2332770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2333dcbf3bd6SGeorge Wilson 23342fd872a7SPrakash Surya /* 23352fd872a7SPrakash Surya * When moving a header off of a ghost state, 2336dcbf3bd6SGeorge Wilson * the header will not contain any arc buffers. 2337dcbf3bd6SGeorge Wilson * We use the arc header pointer for the reference 2338dcbf3bd6SGeorge Wilson * which is exactly what we did when we put the 2339dcbf3bd6SGeorge Wilson * header on the ghost state. 23402fd872a7SPrakash Surya */ 23412fd872a7SPrakash Surya 2342e914ace2STim Schumacher (void) zfs_refcount_remove_many(&old_state->arcs_size, 2343dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr), hdr); 23442fd872a7SPrakash Surya } else { 2345dcbf3bd6SGeorge Wilson uint32_t buffers = 0; 23462fd872a7SPrakash Surya 23472fd872a7SPrakash Surya /* 23482fd872a7SPrakash Surya * Each individual buffer holds a unique reference, 23492fd872a7SPrakash Surya * thus we must remove each of these references one 23502fd872a7SPrakash Surya * at a time. 23512fd872a7SPrakash Surya */ 23522fd872a7SPrakash Surya for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 23532fd872a7SPrakash Surya buf = buf->b_next) { 23545602294fSDan Kimmel ASSERT3U(bufcnt, !=, 0); 2355dcbf3bd6SGeorge Wilson buffers++; 2356dcbf3bd6SGeorge Wilson 2357dcbf3bd6SGeorge Wilson /* 2358dcbf3bd6SGeorge Wilson * When the arc_buf_t is sharing the data 2359dcbf3bd6SGeorge Wilson * block with the hdr, the owner of the 2360dcbf3bd6SGeorge Wilson * reference belongs to the hdr. Only 2361dcbf3bd6SGeorge Wilson * add to the refcount if the arc_buf_t is 2362dcbf3bd6SGeorge Wilson * not shared. 2363dcbf3bd6SGeorge Wilson */ 23645602294fSDan Kimmel if (arc_buf_is_shared(buf)) 2365dcbf3bd6SGeorge Wilson continue; 2366dcbf3bd6SGeorge Wilson 2367e914ace2STim Schumacher (void) zfs_refcount_remove_many( 23685602294fSDan Kimmel &old_state->arcs_size, arc_buf_size(buf), 2369dcbf3bd6SGeorge Wilson buf); 23702fd872a7SPrakash Surya } 2371dcbf3bd6SGeorge Wilson ASSERT3U(bufcnt, ==, buffers); 2372770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 2373e914ace2STim Schumacher (void) zfs_refcount_remove_many( 2374dcbf3bd6SGeorge Wilson &old_state->arcs_size, arc_hdr_size(hdr), hdr); 23752fd872a7SPrakash Surya } 2376fa9e4066Sahrens } 23772fd872a7SPrakash Surya 237889c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) 237989c86e32SChris Williamson hdr->b_l1hdr.b_state = new_state; 2380fa94a07fSbrendan 238189c86e32SChris Williamson /* 238289c86e32SChris Williamson * L2 headers should never be on the L2 state list since they don't 238389c86e32SChris Williamson * have L1 headers allocated. 238489c86e32SChris Williamson */ 238594c2d0ebSMatthew Ahrens ASSERT(multilist_is_empty(arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && 238694c2d0ebSMatthew Ahrens multilist_is_empty(arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); 2387fa9e4066Sahrens } 2388fa9e4066Sahrens 23890e8c6158Smaybee void 23905a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(uint64_t space, arc_space_type_t type) 23910e8c6158Smaybee { 23925a98e54bSBrendan Gregg - Sun Microsystems ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 23935a98e54bSBrendan Gregg - Sun Microsystems 23945a98e54bSBrendan Gregg - Sun Microsystems switch (type) { 23955a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_DATA: 23963a2d8a1bSPaul Dagnelie aggsum_add(&astat_data_size, space); 23975a98e54bSBrendan Gregg - Sun Microsystems break; 23984076b1bfSPrakash Surya case ARC_SPACE_META: 23993a2d8a1bSPaul Dagnelie aggsum_add(&astat_metadata_size, space); 24004076b1bfSPrakash Surya break; 24015a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_OTHER: 24023a2d8a1bSPaul Dagnelie aggsum_add(&astat_other_size, space); 24035a98e54bSBrendan Gregg - Sun Microsystems break; 24045a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_HDRS: 24053a2d8a1bSPaul Dagnelie aggsum_add(&astat_hdr_size, space); 24065a98e54bSBrendan Gregg - Sun Microsystems break; 24075a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_L2HDRS: 24083a2d8a1bSPaul Dagnelie aggsum_add(&astat_l2_hdr_size, space); 24095a98e54bSBrendan Gregg - Sun Microsystems break; 24105a98e54bSBrendan Gregg - Sun Microsystems } 24115a98e54bSBrendan Gregg - Sun Microsystems 24124076b1bfSPrakash Surya if (type != ARC_SPACE_DATA) 24133a2d8a1bSPaul Dagnelie aggsum_add(&arc_meta_used, space); 24144076b1bfSPrakash Surya 24153a2d8a1bSPaul Dagnelie aggsum_add(&arc_size, space); 24160e8c6158Smaybee } 24170e8c6158Smaybee 24180e8c6158Smaybee void 24195a98e54bSBrendan Gregg - Sun Microsystems arc_space_return(uint64_t space, arc_space_type_t type) 24200e8c6158Smaybee { 24215a98e54bSBrendan Gregg - Sun Microsystems ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 24225a98e54bSBrendan Gregg - Sun Microsystems 24235a98e54bSBrendan Gregg - Sun Microsystems switch (type) { 24245a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_DATA: 24253a2d8a1bSPaul Dagnelie aggsum_add(&astat_data_size, -space); 24265a98e54bSBrendan Gregg - Sun Microsystems break; 24274076b1bfSPrakash Surya case ARC_SPACE_META: 24283a2d8a1bSPaul Dagnelie aggsum_add(&astat_metadata_size, -space); 24294076b1bfSPrakash Surya break; 24305a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_OTHER: 24313a2d8a1bSPaul Dagnelie aggsum_add(&astat_other_size, -space); 24325a98e54bSBrendan Gregg - Sun Microsystems break; 24335a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_HDRS: 24343a2d8a1bSPaul Dagnelie aggsum_add(&astat_hdr_size, -space); 24355a98e54bSBrendan Gregg - Sun Microsystems break; 24365a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_L2HDRS: 24373a2d8a1bSPaul Dagnelie aggsum_add(&astat_l2_hdr_size, -space); 24385a98e54bSBrendan Gregg - Sun Microsystems break; 24395a98e54bSBrendan Gregg - Sun Microsystems } 24405a98e54bSBrendan Gregg - Sun Microsystems 24414076b1bfSPrakash Surya if (type != ARC_SPACE_DATA) { 24423a2d8a1bSPaul Dagnelie ASSERT(aggsum_compare(&arc_meta_used, space) >= 0); 24433a2d8a1bSPaul Dagnelie /* 24443a2d8a1bSPaul Dagnelie * We use the upper bound here rather than the precise value 24453a2d8a1bSPaul Dagnelie * because the arc_meta_max value doesn't need to be 24463a2d8a1bSPaul Dagnelie * precise. It's only consumed by humans via arcstats. 24473a2d8a1bSPaul Dagnelie */ 24483a2d8a1bSPaul Dagnelie if (arc_meta_max < aggsum_upper_bound(&arc_meta_used)) 24493a2d8a1bSPaul Dagnelie arc_meta_max = aggsum_upper_bound(&arc_meta_used); 24503a2d8a1bSPaul Dagnelie aggsum_add(&arc_meta_used, -space); 24514076b1bfSPrakash Surya } 24524076b1bfSPrakash Surya 24533a2d8a1bSPaul Dagnelie ASSERT(aggsum_compare(&arc_size, space) >= 0); 24543a2d8a1bSPaul Dagnelie aggsum_add(&arc_size, -space); 24550e8c6158Smaybee } 24560e8c6158Smaybee 2457dcbf3bd6SGeorge Wilson /* 24585602294fSDan Kimmel * Given a hdr and a buf, returns whether that buf can share its b_data buffer 2459770499e1SDan Kimmel * with the hdr's b_pabd. 2460dcbf3bd6SGeorge Wilson */ 24615602294fSDan Kimmel static boolean_t 24625602294fSDan Kimmel arc_can_share(arc_buf_hdr_t *hdr, arc_buf_t *buf) 24635602294fSDan Kimmel { 24645602294fSDan Kimmel /* 24655602294fSDan Kimmel * The criteria for sharing a hdr's data are: 24665602294fSDan Kimmel * 1. the hdr's compression matches the buf's compression 24675602294fSDan Kimmel * 2. the hdr doesn't need to be byteswapped 24685602294fSDan Kimmel * 3. the hdr isn't already being shared 24695602294fSDan Kimmel * 4. the buf is either compressed or it is the last buf in the hdr list 24705602294fSDan Kimmel * 24715602294fSDan Kimmel * Criterion #4 maintains the invariant that shared uncompressed 24725602294fSDan Kimmel * bufs must be the final buf in the hdr's b_buf list. Reading this, you 24735602294fSDan Kimmel * might ask, "if a compressed buf is allocated first, won't that be the 24745602294fSDan Kimmel * last thing in the list?", but in that case it's impossible to create 24755602294fSDan Kimmel * a shared uncompressed buf anyway (because the hdr must be compressed 24765602294fSDan Kimmel * to have the compressed buf). You might also think that #3 is 24775602294fSDan Kimmel * sufficient to make this guarantee, however it's possible 24785602294fSDan Kimmel * (specifically in the rare L2ARC write race mentioned in 24795602294fSDan Kimmel * arc_buf_alloc_impl()) there will be an existing uncompressed buf that 24805602294fSDan Kimmel * is sharable, but wasn't at the time of its allocation. Rather than 24815602294fSDan Kimmel * allow a new shared uncompressed buf to be created and then shuffle 24825602294fSDan Kimmel * the list around to make it the last element, this simply disallows 24835602294fSDan Kimmel * sharing if the new buf isn't the first to be added. 24845602294fSDan Kimmel */ 24855602294fSDan Kimmel ASSERT3P(buf->b_hdr, ==, hdr); 24865602294fSDan Kimmel boolean_t hdr_compressed = HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF; 24875602294fSDan Kimmel boolean_t buf_compressed = ARC_BUF_COMPRESSED(buf) != 0; 24885602294fSDan Kimmel return (buf_compressed == hdr_compressed && 24895602294fSDan Kimmel hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS && 24905602294fSDan Kimmel !HDR_SHARED_DATA(hdr) && 24915602294fSDan Kimmel (ARC_BUF_LAST(buf) || ARC_BUF_COMPRESSED(buf))); 24925602294fSDan Kimmel } 24935602294fSDan Kimmel 24945602294fSDan Kimmel /* 24955602294fSDan Kimmel * Allocate a buf for this hdr. If you care about the data that's in the hdr, 24965602294fSDan Kimmel * or if you want a compressed buffer, pass those flags in. Returns 0 if the 24975602294fSDan Kimmel * copy was made successfully, or an error code otherwise. 24985602294fSDan Kimmel */ 24995602294fSDan Kimmel static int 25005602294fSDan Kimmel arc_buf_alloc_impl(arc_buf_hdr_t *hdr, void *tag, boolean_t compressed, 25015602294fSDan Kimmel boolean_t fill, arc_buf_t **ret) 2502fa9e4066Sahrens { 2503fa9e4066Sahrens arc_buf_t *buf; 2504fa9e4066Sahrens 2505dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2506dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); 2507dcbf3bd6SGeorge Wilson VERIFY(hdr->b_type == ARC_BUFC_DATA || 2508dcbf3bd6SGeorge Wilson hdr->b_type == ARC_BUFC_METADATA); 25095602294fSDan Kimmel ASSERT3P(ret, !=, NULL); 25105602294fSDan Kimmel ASSERT3P(*ret, ==, NULL); 2511dcbf3bd6SGeorge Wilson 25125602294fSDan Kimmel buf = *ret = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2513fa9e4066Sahrens buf->b_hdr = hdr; 251444eda4d7Smaybee buf->b_data = NULL; 25155602294fSDan Kimmel buf->b_next = hdr->b_l1hdr.b_buf; 25165602294fSDan Kimmel buf->b_flags = 0; 251789c86e32SChris Williamson 2518dcbf3bd6SGeorge Wilson add_reference(hdr, tag); 2519dcbf3bd6SGeorge Wilson 2520dcbf3bd6SGeorge Wilson /* 2521dcbf3bd6SGeorge Wilson * We're about to change the hdr's b_flags. We must either 2522dcbf3bd6SGeorge Wilson * hold the hash_lock or be undiscoverable. 2523dcbf3bd6SGeorge Wilson */ 2524dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2525dcbf3bd6SGeorge Wilson 2526dcbf3bd6SGeorge Wilson /* 25275602294fSDan Kimmel * Only honor requests for compressed bufs if the hdr is actually 25285602294fSDan Kimmel * compressed. 25295602294fSDan Kimmel */ 25305602294fSDan Kimmel if (compressed && HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) 25315602294fSDan Kimmel buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; 25325602294fSDan Kimmel 25335602294fSDan Kimmel /* 25345602294fSDan Kimmel * If the hdr's data can be shared then we share the data buffer and 25355602294fSDan Kimmel * set the appropriate bit in the hdr's b_flags to indicate the hdr is 2536770499e1SDan Kimmel * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new 25375602294fSDan Kimmel * buffer to store the buf's data. 25385602294fSDan Kimmel * 2539770499e1SDan Kimmel * There are two additional restrictions here because we're sharing 2540770499e1SDan Kimmel * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be 2541770499e1SDan Kimmel * actively involved in an L2ARC write, because if this buf is used by 2542770499e1SDan Kimmel * an arc_write() then the hdr's data buffer will be released when the 25435602294fSDan Kimmel * write completes, even though the L2ARC write might still be using it. 2544770499e1SDan Kimmel * Second, the hdr's ABD must be linear so that the buf's user doesn't 2545770499e1SDan Kimmel * need to be ABD-aware. 2546dcbf3bd6SGeorge Wilson */ 2547770499e1SDan Kimmel boolean_t can_share = arc_can_share(hdr, buf) && !HDR_L2_WRITING(hdr) && 2548770499e1SDan Kimmel abd_is_linear(hdr->b_l1hdr.b_pabd); 25495602294fSDan Kimmel 25505602294fSDan Kimmel /* Set up b_data and sharing */ 25515602294fSDan Kimmel if (can_share) { 2552770499e1SDan Kimmel buf->b_data = abd_to_buf(hdr->b_l1hdr.b_pabd); 25535602294fSDan Kimmel buf->b_flags |= ARC_BUF_FLAG_SHARED; 2554dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); 2555dcbf3bd6SGeorge Wilson } else { 25565602294fSDan Kimmel buf->b_data = 25575602294fSDan Kimmel arc_get_data_buf(hdr, arc_buf_size(buf), buf); 25585602294fSDan Kimmel ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); 2559dcbf3bd6SGeorge Wilson } 2560dcbf3bd6SGeorge Wilson VERIFY3P(buf->b_data, !=, NULL); 256189c86e32SChris Williamson 256289c86e32SChris Williamson hdr->b_l1hdr.b_buf = buf; 2563dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt += 1; 256489c86e32SChris Williamson 25655602294fSDan Kimmel /* 25665602294fSDan Kimmel * If the user wants the data from the hdr, we need to either copy or 25675602294fSDan Kimmel * decompress the data. 25685602294fSDan Kimmel */ 25695602294fSDan Kimmel if (fill) { 25705602294fSDan Kimmel return (arc_buf_fill(buf, ARC_BUF_COMPRESSED(buf) != 0)); 25715602294fSDan Kimmel } 2572dcbf3bd6SGeorge Wilson 25735602294fSDan Kimmel return (0); 25745602294fSDan Kimmel } 2575dcbf3bd6SGeorge Wilson 25765602294fSDan Kimmel static char *arc_onloan_tag = "onloan"; 2577fa9e4066Sahrens 25785602294fSDan Kimmel static inline void 25795602294fSDan Kimmel arc_loaned_bytes_update(int64_t delta) 25805602294fSDan Kimmel { 25815602294fSDan Kimmel atomic_add_64(&arc_loaned_bytes, delta); 2582dcbf3bd6SGeorge Wilson 25835602294fSDan Kimmel /* assert that it did not wrap around */ 25845602294fSDan Kimmel ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); 2585fa9e4066Sahrens } 2586fa9e4066Sahrens 25872fdbea25SAleksandr Guzovskiy /* 25882fdbea25SAleksandr Guzovskiy * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 25892fdbea25SAleksandr Guzovskiy * flight data by arc_tempreserve_space() until they are "returned". Loaned 25902fdbea25SAleksandr Guzovskiy * buffers must be returned to the arc before they can be used by the DMU or 25912fdbea25SAleksandr Guzovskiy * freed. 25922fdbea25SAleksandr Guzovskiy */ 25932fdbea25SAleksandr Guzovskiy arc_buf_t * 25945602294fSDan Kimmel arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size) 25952fdbea25SAleksandr Guzovskiy { 25965602294fSDan Kimmel arc_buf_t *buf = arc_alloc_buf(spa, arc_onloan_tag, 25975602294fSDan Kimmel is_metadata ? ARC_BUFC_METADATA : ARC_BUFC_DATA, size); 25985602294fSDan Kimmel 25999be12bd7SAllan Jude arc_loaned_bytes_update(arc_buf_size(buf)); 26005602294fSDan Kimmel 26015602294fSDan Kimmel return (buf); 26025602294fSDan Kimmel } 26032fdbea25SAleksandr Guzovskiy 26045602294fSDan Kimmel arc_buf_t * 26055602294fSDan Kimmel arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize, 26065602294fSDan Kimmel enum zio_compress compression_type) 26075602294fSDan Kimmel { 26085602294fSDan Kimmel arc_buf_t *buf = arc_alloc_compressed_buf(spa, arc_onloan_tag, 26095602294fSDan Kimmel psize, lsize, compression_type); 26105602294fSDan Kimmel 26119be12bd7SAllan Jude arc_loaned_bytes_update(arc_buf_size(buf)); 26122fdbea25SAleksandr Guzovskiy 26132fdbea25SAleksandr Guzovskiy return (buf); 26142fdbea25SAleksandr Guzovskiy } 26152fdbea25SAleksandr Guzovskiy 26165602294fSDan Kimmel 26172fdbea25SAleksandr Guzovskiy /* 26182fdbea25SAleksandr Guzovskiy * Return a loaned arc buffer to the arc. 26192fdbea25SAleksandr Guzovskiy */ 26202fdbea25SAleksandr Guzovskiy void 26212fdbea25SAleksandr Guzovskiy arc_return_buf(arc_buf_t *buf, void *tag) 26222fdbea25SAleksandr Guzovskiy { 26232fdbea25SAleksandr Guzovskiy arc_buf_hdr_t *hdr = buf->b_hdr; 26242fdbea25SAleksandr Guzovskiy 2625dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, !=, NULL); 262689c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 2627e914ace2STim Schumacher (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 2628e914ace2STim Schumacher (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 26292fdbea25SAleksandr Guzovskiy 26305602294fSDan Kimmel arc_loaned_bytes_update(-arc_buf_size(buf)); 26312fdbea25SAleksandr Guzovskiy } 26322fdbea25SAleksandr Guzovskiy 2633c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /* Detach an arc_buf from a dbuf (tag) */ 2634c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void 2635c242f9a0Schunli zhang - Sun Microsystems - Irvine United States arc_loan_inuse_buf(arc_buf_t *buf, void *tag) 2636c242f9a0Schunli zhang - Sun Microsystems - Irvine United States { 263789c86e32SChris Williamson arc_buf_hdr_t *hdr = buf->b_hdr; 2638c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 2639dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, !=, NULL); 264089c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 2641e914ace2STim Schumacher (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2642e914ace2STim Schumacher (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); 2643c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 26445602294fSDan Kimmel arc_loaned_bytes_update(arc_buf_size(buf)); 2645c242f9a0Schunli zhang - Sun Microsystems - Irvine United States } 2646c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 2647dcbf3bd6SGeorge Wilson static void 2648770499e1SDan Kimmel l2arc_free_abd_on_write(abd_t *abd, size_t size, arc_buf_contents_t type) 2649ea8dc4b6Seschrock { 2650dcbf3bd6SGeorge Wilson l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP); 2651ea8dc4b6Seschrock 2652770499e1SDan Kimmel df->l2df_abd = abd; 2653dcbf3bd6SGeorge Wilson df->l2df_size = size; 2654dcbf3bd6SGeorge Wilson df->l2df_type = type; 2655dcbf3bd6SGeorge Wilson mutex_enter(&l2arc_free_on_write_mtx); 2656dcbf3bd6SGeorge Wilson list_insert_head(l2arc_free_on_write, df); 2657dcbf3bd6SGeorge Wilson mutex_exit(&l2arc_free_on_write_mtx); 2658dcbf3bd6SGeorge Wilson } 2659b24ab676SJeff Bonwick 2660dcbf3bd6SGeorge Wilson static void 2661dcbf3bd6SGeorge Wilson arc_hdr_free_on_write(arc_buf_hdr_t *hdr) 2662dcbf3bd6SGeorge Wilson { 2663dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2664dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 2665dcbf3bd6SGeorge Wilson uint64_t size = arc_hdr_size(hdr); 2666dcbf3bd6SGeorge Wilson 2667dcbf3bd6SGeorge Wilson /* protected by hash lock, if in the hash table */ 2668dcbf3bd6SGeorge Wilson if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 2669e914ace2STim Schumacher ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2670dcbf3bd6SGeorge Wilson ASSERT(state != arc_anon && state != arc_l2c_only); 2671dcbf3bd6SGeorge Wilson 2672e914ace2STim Schumacher (void) zfs_refcount_remove_many(&state->arcs_esize[type], 2673dcbf3bd6SGeorge Wilson size, hdr); 2674dcbf3bd6SGeorge Wilson } 2675e914ace2STim Schumacher (void) zfs_refcount_remove_many(&state->arcs_size, size, hdr); 26766de76ce2SAndriy Gapon if (type == ARC_BUFC_METADATA) { 26776de76ce2SAndriy Gapon arc_space_return(size, ARC_SPACE_META); 26786de76ce2SAndriy Gapon } else { 26796de76ce2SAndriy Gapon ASSERT(type == ARC_BUFC_DATA); 26806de76ce2SAndriy Gapon arc_space_return(size, ARC_SPACE_DATA); 26816de76ce2SAndriy Gapon } 2682dcbf3bd6SGeorge Wilson 2683770499e1SDan Kimmel l2arc_free_abd_on_write(hdr->b_l1hdr.b_pabd, size, type); 2684dcbf3bd6SGeorge Wilson } 2685dcbf3bd6SGeorge Wilson 2686dcbf3bd6SGeorge Wilson /* 2687dcbf3bd6SGeorge Wilson * Share the arc_buf_t's data with the hdr. Whenever we are sharing the 2688dcbf3bd6SGeorge Wilson * data buffer, we transfer the refcount ownership to the hdr and update 2689dcbf3bd6SGeorge Wilson * the appropriate kstats. 2690dcbf3bd6SGeorge Wilson */ 2691dcbf3bd6SGeorge Wilson static void 2692dcbf3bd6SGeorge Wilson arc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2693dcbf3bd6SGeorge Wilson { 2694dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2695dcbf3bd6SGeorge Wilson 26965602294fSDan Kimmel ASSERT(arc_can_share(hdr, buf)); 2697770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2698dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 26999253d63dSGeorge Wilson 27009253d63dSGeorge Wilson /* 2701dcbf3bd6SGeorge Wilson * Start sharing the data buffer. We transfer the 2702dcbf3bd6SGeorge Wilson * refcount ownership to the hdr since it always owns 2703dcbf3bd6SGeorge Wilson * the refcount whenever an arc_buf_t is shared. 27049253d63dSGeorge Wilson */ 2705e914ace2STim Schumacher zfs_refcount_transfer_ownership(&state->arcs_size, buf, hdr); 2706770499e1SDan Kimmel hdr->b_l1hdr.b_pabd = abd_get_from_buf(buf->b_data, arc_buf_size(buf)); 2707770499e1SDan Kimmel abd_take_ownership_of_buf(hdr->b_l1hdr.b_pabd, 2708770499e1SDan Kimmel HDR_ISTYPE_METADATA(hdr)); 2709dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); 27105602294fSDan Kimmel buf->b_flags |= ARC_BUF_FLAG_SHARED; 2711dcbf3bd6SGeorge Wilson 2712dcbf3bd6SGeorge Wilson /* 2713dcbf3bd6SGeorge Wilson * Since we've transferred ownership to the hdr we need 2714dcbf3bd6SGeorge Wilson * to increment its compressed and uncompressed kstats and 2715dcbf3bd6SGeorge Wilson * decrement the overhead size. 2716dcbf3bd6SGeorge Wilson */ 2717dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); 2718dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); 27195602294fSDan Kimmel ARCSTAT_INCR(arcstat_overhead_size, -arc_buf_size(buf)); 2720ea8dc4b6Seschrock } 2721ea8dc4b6Seschrock 2722dcbf3bd6SGeorge Wilson static void 2723dcbf3bd6SGeorge Wilson arc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2724ea8dc4b6Seschrock { 2725dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2726dcbf3bd6SGeorge Wilson 2727dcbf3bd6SGeorge Wilson ASSERT(arc_buf_is_shared(buf)); 2728770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 2729dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2730ea8dc4b6Seschrock 27319b23f181Smaybee /* 2732dcbf3bd6SGeorge Wilson * We are no longer sharing this buffer so we need 2733dcbf3bd6SGeorge Wilson * to transfer its ownership to the rightful owner. 27349b23f181Smaybee */ 2735e914ace2STim Schumacher zfs_refcount_transfer_ownership(&state->arcs_size, hdr, buf); 2736dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 2737770499e1SDan Kimmel abd_release_ownership_of_buf(hdr->b_l1hdr.b_pabd); 2738770499e1SDan Kimmel abd_put(hdr->b_l1hdr.b_pabd); 2739770499e1SDan Kimmel hdr->b_l1hdr.b_pabd = NULL; 27405602294fSDan Kimmel buf->b_flags &= ~ARC_BUF_FLAG_SHARED; 2741dcbf3bd6SGeorge Wilson 2742dcbf3bd6SGeorge Wilson /* 2743dcbf3bd6SGeorge Wilson * Since the buffer is no longer shared between 2744dcbf3bd6SGeorge Wilson * the arc buf and the hdr, count it as overhead. 2745dcbf3bd6SGeorge Wilson */ 2746dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); 2747dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); 27485602294fSDan Kimmel ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); 2749dcbf3bd6SGeorge Wilson } 2750dcbf3bd6SGeorge Wilson 2751dcbf3bd6SGeorge Wilson /* 27525602294fSDan Kimmel * Remove an arc_buf_t from the hdr's buf list and return the last 27535602294fSDan Kimmel * arc_buf_t on the list. If no buffers remain on the list then return 27545602294fSDan Kimmel * NULL. 27555602294fSDan Kimmel */ 27565602294fSDan Kimmel static arc_buf_t * 27575602294fSDan Kimmel arc_buf_remove(arc_buf_hdr_t *hdr, arc_buf_t *buf) 27585602294fSDan Kimmel { 27595602294fSDan Kimmel ASSERT(HDR_HAS_L1HDR(hdr)); 27605602294fSDan Kimmel ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 27615602294fSDan Kimmel 27625602294fSDan Kimmel arc_buf_t **bufp = &hdr->b_l1hdr.b_buf; 27635602294fSDan Kimmel arc_buf_t *lastbuf = NULL; 27645602294fSDan Kimmel 27655602294fSDan Kimmel /* 27665602294fSDan Kimmel * Remove the buf from the hdr list and locate the last 27675602294fSDan Kimmel * remaining buffer on the list. 27685602294fSDan Kimmel */ 27695602294fSDan Kimmel while (*bufp != NULL) { 27705602294fSDan Kimmel if (*bufp == buf) 27715602294fSDan Kimmel *bufp = buf->b_next; 27725602294fSDan Kimmel 27735602294fSDan Kimmel /* 27745602294fSDan Kimmel * If we've removed a buffer in the middle of 27755602294fSDan Kimmel * the list then update the lastbuf and update 27765602294fSDan Kimmel * bufp. 27775602294fSDan Kimmel */ 27785602294fSDan Kimmel if (*bufp != NULL) { 27795602294fSDan Kimmel lastbuf = *bufp; 27805602294fSDan Kimmel bufp = &(*bufp)->b_next; 27815602294fSDan Kimmel } 27825602294fSDan Kimmel } 27835602294fSDan Kimmel buf->b_next = NULL; 27845602294fSDan Kimmel ASSERT3P(lastbuf, !=, buf); 27855602294fSDan Kimmel IMPLY(hdr->b_l1hdr.b_bufcnt > 0, lastbuf != NULL); 27865602294fSDan Kimmel IMPLY(hdr->b_l1hdr.b_bufcnt > 0, hdr->b_l1hdr.b_buf != NULL); 27875602294fSDan Kimmel IMPLY(lastbuf != NULL, ARC_BUF_LAST(lastbuf)); 27885602294fSDan Kimmel 27895602294fSDan Kimmel return (lastbuf); 27905602294fSDan Kimmel } 27915602294fSDan Kimmel 27925602294fSDan Kimmel /* 27935602294fSDan Kimmel * Free up buf->b_data and pull the arc_buf_t off of the the arc_buf_hdr_t's 27945602294fSDan Kimmel * list and free it. 2795dcbf3bd6SGeorge Wilson */ 2796dcbf3bd6SGeorge Wilson static void 27975602294fSDan Kimmel arc_buf_destroy_impl(arc_buf_t *buf) 2798dcbf3bd6SGeorge Wilson { 2799dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 2800dcbf3bd6SGeorge Wilson 2801dcbf3bd6SGeorge Wilson /* 28025602294fSDan Kimmel * Free up the data associated with the buf but only if we're not 28035602294fSDan Kimmel * sharing this with the hdr. If we are sharing it with the hdr, the 28045602294fSDan Kimmel * hdr is responsible for doing the free. 2805dcbf3bd6SGeorge Wilson */ 2806dcbf3bd6SGeorge Wilson if (buf->b_data != NULL) { 2807dcbf3bd6SGeorge Wilson /* 2808dcbf3bd6SGeorge Wilson * We're about to change the hdr's b_flags. We must either 2809dcbf3bd6SGeorge Wilson * hold the hash_lock or be undiscoverable. 2810dcbf3bd6SGeorge Wilson */ 2811dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2812dcbf3bd6SGeorge Wilson 2813dcbf3bd6SGeorge Wilson arc_cksum_verify(buf); 2814dcbf3bd6SGeorge Wilson arc_buf_unwatch(buf); 2815dcbf3bd6SGeorge Wilson 28165602294fSDan Kimmel if (arc_buf_is_shared(buf)) { 2817dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 2818dcbf3bd6SGeorge Wilson } else { 28195602294fSDan Kimmel uint64_t size = arc_buf_size(buf); 2820dcbf3bd6SGeorge Wilson arc_free_data_buf(hdr, buf->b_data, size, buf); 2821dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_overhead_size, -size); 2822dcbf3bd6SGeorge Wilson } 2823dcbf3bd6SGeorge Wilson buf->b_data = NULL; 2824dcbf3bd6SGeorge Wilson 2825dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 2826dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt -= 1; 2827dcbf3bd6SGeorge Wilson } 2828dcbf3bd6SGeorge Wilson 28295602294fSDan Kimmel arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); 2830dcbf3bd6SGeorge Wilson 28315602294fSDan Kimmel if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) { 2832dcbf3bd6SGeorge Wilson /* 28335602294fSDan Kimmel * If the current arc_buf_t is sharing its data buffer with the 2834770499e1SDan Kimmel * hdr, then reassign the hdr's b_pabd to share it with the new 28355602294fSDan Kimmel * buffer at the end of the list. The shared buffer is always 28365602294fSDan Kimmel * the last one on the hdr's buffer list. 28375602294fSDan Kimmel * 28385602294fSDan Kimmel * There is an equivalent case for compressed bufs, but since 28395602294fSDan Kimmel * they aren't guaranteed to be the last buf in the list and 28405602294fSDan Kimmel * that is an exceedingly rare case, we just allow that space be 28415602294fSDan Kimmel * wasted temporarily. 2842dcbf3bd6SGeorge Wilson */ 28435602294fSDan Kimmel if (lastbuf != NULL) { 28445602294fSDan Kimmel /* Only one buf can be shared at once */ 28455602294fSDan Kimmel VERIFY(!arc_buf_is_shared(lastbuf)); 28465602294fSDan Kimmel /* hdr is uncompressed so can't have compressed buf */ 28475602294fSDan Kimmel VERIFY(!ARC_BUF_COMPRESSED(lastbuf)); 2848dcbf3bd6SGeorge Wilson 2849770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 2850770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 2851dcbf3bd6SGeorge Wilson 28525602294fSDan Kimmel /* 28535602294fSDan Kimmel * We must setup a new shared block between the 28545602294fSDan Kimmel * last buffer and the hdr. The data would have 28555602294fSDan Kimmel * been allocated by the arc buf so we need to transfer 28565602294fSDan Kimmel * ownership to the hdr since it's now being shared. 28575602294fSDan Kimmel */ 28585602294fSDan Kimmel arc_share_buf(hdr, lastbuf); 28595602294fSDan Kimmel } 28605602294fSDan Kimmel } else if (HDR_SHARED_DATA(hdr)) { 2861dcbf3bd6SGeorge Wilson /* 28625602294fSDan Kimmel * Uncompressed shared buffers are always at the end 28635602294fSDan Kimmel * of the list. Compressed buffers don't have the 28645602294fSDan Kimmel * same requirements. This makes it hard to 28655602294fSDan Kimmel * simply assert that the lastbuf is shared so 28665602294fSDan Kimmel * we rely on the hdr's compression flags to determine 28675602294fSDan Kimmel * if we have a compressed, shared buffer. 2868dcbf3bd6SGeorge Wilson */ 28695602294fSDan Kimmel ASSERT3P(lastbuf, !=, NULL); 28705602294fSDan Kimmel ASSERT(arc_buf_is_shared(lastbuf) || 28715602294fSDan Kimmel HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF); 2872dcbf3bd6SGeorge Wilson } 2873dcbf3bd6SGeorge Wilson 28745602294fSDan Kimmel /* 28755602294fSDan Kimmel * Free the checksum if we're removing the last uncompressed buf from 28765602294fSDan Kimmel * this hdr. 28775602294fSDan Kimmel */ 28785602294fSDan Kimmel if (!arc_hdr_has_uncompressed_buf(hdr)) { 2879dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 28805602294fSDan Kimmel } 2881dcbf3bd6SGeorge Wilson 2882dcbf3bd6SGeorge Wilson /* clean up the buf */ 2883dcbf3bd6SGeorge Wilson buf->b_hdr = NULL; 2884dcbf3bd6SGeorge Wilson kmem_cache_free(buf_cache, buf); 2885dcbf3bd6SGeorge Wilson } 2886dcbf3bd6SGeorge Wilson 2887dcbf3bd6SGeorge Wilson static void 2888770499e1SDan Kimmel arc_hdr_alloc_pabd(arc_buf_hdr_t *hdr) 2889dcbf3bd6SGeorge Wilson { 2890dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); 289189c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 2892dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 2893ea8dc4b6Seschrock 2894770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2895770499e1SDan Kimmel hdr->b_l1hdr.b_pabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr); 2896dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 2897770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 289889c86e32SChris Williamson 2899dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); 2900dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); 2901ea8dc4b6Seschrock } 2902ea8dc4b6Seschrock 2903244781f1SPrakash Surya static void 2904770499e1SDan Kimmel arc_hdr_free_pabd(arc_buf_hdr_t *hdr) 2905244781f1SPrakash Surya { 2906dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2907770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 2908244781f1SPrakash Surya 2909dcbf3bd6SGeorge Wilson /* 2910dcbf3bd6SGeorge Wilson * If the hdr is currently being written to the l2arc then 2911dcbf3bd6SGeorge Wilson * we defer freeing the data by adding it to the l2arc_free_on_write 2912dcbf3bd6SGeorge Wilson * list. The l2arc will free the data once it's finished 2913dcbf3bd6SGeorge Wilson * writing it to the l2arc device. 2914dcbf3bd6SGeorge Wilson */ 2915dcbf3bd6SGeorge Wilson if (HDR_L2_WRITING(hdr)) { 2916dcbf3bd6SGeorge Wilson arc_hdr_free_on_write(hdr); 2917dcbf3bd6SGeorge Wilson ARCSTAT_BUMP(arcstat_l2_free_on_write); 2918dcbf3bd6SGeorge Wilson } else { 2919770499e1SDan Kimmel arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, 2920dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 2921dcbf3bd6SGeorge Wilson } 2922770499e1SDan Kimmel hdr->b_l1hdr.b_pabd = NULL; 2923dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 2924dcbf3bd6SGeorge Wilson 2925dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); 2926dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); 2927dcbf3bd6SGeorge Wilson } 2928dcbf3bd6SGeorge Wilson 2929dcbf3bd6SGeorge Wilson static arc_buf_hdr_t * 2930dcbf3bd6SGeorge Wilson arc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize, 29315602294fSDan Kimmel enum zio_compress compression_type, arc_buf_contents_t type) 2932dcbf3bd6SGeorge Wilson { 2933dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr; 2934dcbf3bd6SGeorge Wilson 2935dcbf3bd6SGeorge Wilson VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA); 2936dcbf3bd6SGeorge Wilson 2937dcbf3bd6SGeorge Wilson hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 2938dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 2939dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 2940dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_thawed, ==, NULL); 2941dcbf3bd6SGeorge Wilson HDR_SET_PSIZE(hdr, psize); 2942dcbf3bd6SGeorge Wilson HDR_SET_LSIZE(hdr, lsize); 2943dcbf3bd6SGeorge Wilson hdr->b_spa = spa; 2944dcbf3bd6SGeorge Wilson hdr->b_type = type; 2945dcbf3bd6SGeorge Wilson hdr->b_flags = 0; 2946dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR); 29475602294fSDan Kimmel arc_hdr_set_compress(hdr, compression_type); 2948dcbf3bd6SGeorge Wilson 2949dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_state = arc_anon; 2950dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_arc_access = 0; 2951dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt = 0; 2952dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_buf = NULL; 2953dcbf3bd6SGeorge Wilson 2954dcbf3bd6SGeorge Wilson /* 2955dcbf3bd6SGeorge Wilson * Allocate the hdr's buffer. This will contain either 2956dcbf3bd6SGeorge Wilson * the compressed or uncompressed data depending on the block 2957dcbf3bd6SGeorge Wilson * it references and compressed arc enablement. 2958dcbf3bd6SGeorge Wilson */ 2959770499e1SDan Kimmel arc_hdr_alloc_pabd(hdr); 2960e914ace2STim Schumacher ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2961dcbf3bd6SGeorge Wilson 2962dcbf3bd6SGeorge Wilson return (hdr); 2963244781f1SPrakash Surya } 2964244781f1SPrakash Surya 2965fa94a07fSbrendan /* 2966dcbf3bd6SGeorge Wilson * Transition between the two allocation states for the arc_buf_hdr struct. 2967dcbf3bd6SGeorge Wilson * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without 2968dcbf3bd6SGeorge Wilson * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller 2969dcbf3bd6SGeorge Wilson * version is used when a cache buffer is only in the L2ARC in order to reduce 2970dcbf3bd6SGeorge Wilson * memory usage. 2971fa94a07fSbrendan */ 2972dcbf3bd6SGeorge Wilson static arc_buf_hdr_t * 2973dcbf3bd6SGeorge Wilson arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) 2974fa94a07fSbrendan { 2975dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L2HDR(hdr)); 2976dcbf3bd6SGeorge Wilson 2977dcbf3bd6SGeorge Wilson arc_buf_hdr_t *nhdr; 2978dcbf3bd6SGeorge Wilson l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 2979dcbf3bd6SGeorge Wilson 2980dcbf3bd6SGeorge Wilson ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || 2981dcbf3bd6SGeorge Wilson (old == hdr_l2only_cache && new == hdr_full_cache)); 2982dcbf3bd6SGeorge Wilson 2983dcbf3bd6SGeorge Wilson nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); 2984dcbf3bd6SGeorge Wilson 2985dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 2986dcbf3bd6SGeorge Wilson buf_hash_remove(hdr); 2987dcbf3bd6SGeorge Wilson 2988dcbf3bd6SGeorge Wilson bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); 2989dcbf3bd6SGeorge Wilson 2990dcbf3bd6SGeorge Wilson if (new == hdr_full_cache) { 2991dcbf3bd6SGeorge Wilson arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR); 2992dcbf3bd6SGeorge Wilson /* 2993dcbf3bd6SGeorge Wilson * arc_access and arc_change_state need to be aware that a 2994dcbf3bd6SGeorge Wilson * header has just come out of L2ARC, so we set its state to 2995dcbf3bd6SGeorge Wilson * l2c_only even though it's about to change. 2996dcbf3bd6SGeorge Wilson */ 2997dcbf3bd6SGeorge Wilson nhdr->b_l1hdr.b_state = arc_l2c_only; 2998dcbf3bd6SGeorge Wilson 2999dcbf3bd6SGeorge Wilson /* Verify previous threads set to NULL before freeing */ 3000770499e1SDan Kimmel ASSERT3P(nhdr->b_l1hdr.b_pabd, ==, NULL); 3001dcbf3bd6SGeorge Wilson } else { 3002dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 3003dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 3004dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 3005dcbf3bd6SGeorge Wilson 3006dcbf3bd6SGeorge Wilson /* 3007dcbf3bd6SGeorge Wilson * If we've reached here, We must have been called from 3008dcbf3bd6SGeorge Wilson * arc_evict_hdr(), as such we should have already been 3009dcbf3bd6SGeorge Wilson * removed from any ghost list we were previously on 3010dcbf3bd6SGeorge Wilson * (which protects us from racing with arc_evict_state), 3011dcbf3bd6SGeorge Wilson * thus no locking is needed during this check. 3012dcbf3bd6SGeorge Wilson */ 3013dcbf3bd6SGeorge Wilson ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 3014cd1c8b85SMatthew Ahrens 3015dcbf3bd6SGeorge Wilson /* 3016dcbf3bd6SGeorge Wilson * A buffer must not be moved into the arc_l2c_only 3017dcbf3bd6SGeorge Wilson * state if it's not finished being written out to the 3018770499e1SDan Kimmel * l2arc device. Otherwise, the b_l1hdr.b_pabd field 3019dcbf3bd6SGeorge Wilson * might try to be accessed, even though it was removed. 3020dcbf3bd6SGeorge Wilson */ 3021dcbf3bd6SGeorge Wilson VERIFY(!HDR_L2_WRITING(hdr)); 3022770499e1SDan Kimmel VERIFY3P(hdr->b_l1hdr.b_pabd, ==, NULL); 3023fa94a07fSbrendan 3024dcbf3bd6SGeorge Wilson #ifdef ZFS_DEBUG 3025dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_thawed != NULL) { 3026dcbf3bd6SGeorge Wilson kmem_free(hdr->b_l1hdr.b_thawed, 1); 3027dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_thawed = NULL; 3028dcbf3bd6SGeorge Wilson } 3029dcbf3bd6SGeorge Wilson #endif 3030244781f1SPrakash Surya 3031dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR); 3032dcbf3bd6SGeorge Wilson } 3033244781f1SPrakash Surya /* 3034dcbf3bd6SGeorge Wilson * The header has been reallocated so we need to re-insert it into any 3035dcbf3bd6SGeorge Wilson * lists it was on. 3036244781f1SPrakash Surya */ 3037dcbf3bd6SGeorge Wilson (void) buf_hash_insert(nhdr, NULL); 3038244781f1SPrakash Surya 3039dcbf3bd6SGeorge Wilson ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); 3040dcbf3bd6SGeorge Wilson 3041dcbf3bd6SGeorge Wilson mutex_enter(&dev->l2ad_mtx); 3042244781f1SPrakash Surya 3043244781f1SPrakash Surya /* 3044dcbf3bd6SGeorge Wilson * We must place the realloc'ed header back into the list at 3045dcbf3bd6SGeorge Wilson * the same spot. Otherwise, if it's placed earlier in the list, 3046dcbf3bd6SGeorge Wilson * l2arc_write_buffers() could find it during the function's 3047dcbf3bd6SGeorge Wilson * write phase, and try to write it out to the l2arc. 3048244781f1SPrakash Surya */ 3049dcbf3bd6SGeorge Wilson list_insert_after(&dev->l2ad_buflist, hdr, nhdr); 3050dcbf3bd6SGeorge Wilson list_remove(&dev->l2ad_buflist, hdr); 3051dcbf3bd6SGeorge Wilson 3052dcbf3bd6SGeorge Wilson mutex_exit(&dev->l2ad_mtx); 3053244781f1SPrakash Surya 3054244781f1SPrakash Surya /* 3055dcbf3bd6SGeorge Wilson * Since we're using the pointer address as the tag when 3056dcbf3bd6SGeorge Wilson * incrementing and decrementing the l2ad_alloc refcount, we 3057dcbf3bd6SGeorge Wilson * must remove the old pointer (that we're about to destroy) and 3058dcbf3bd6SGeorge Wilson * add the new pointer to the refcount. Otherwise we'd remove 3059dcbf3bd6SGeorge Wilson * the wrong pointer address when calling arc_hdr_destroy() later. 3060244781f1SPrakash Surya */ 3061244781f1SPrakash Surya 3062e914ace2STim Schumacher (void) zfs_refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr), 3063e914ace2STim Schumacher hdr); 3064e914ace2STim Schumacher (void) zfs_refcount_add_many(&dev->l2ad_alloc, arc_hdr_size(nhdr), 3065e914ace2STim Schumacher nhdr); 3066244781f1SPrakash Surya 3067dcbf3bd6SGeorge Wilson buf_discard_identity(hdr); 3068dcbf3bd6SGeorge Wilson kmem_cache_free(old, hdr); 3069244781f1SPrakash Surya 3070dcbf3bd6SGeorge Wilson return (nhdr); 3071244781f1SPrakash Surya } 3072244781f1SPrakash Surya 3073bbfa8ea8SMatthew Ahrens /* 3074dcbf3bd6SGeorge Wilson * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller. 3075dcbf3bd6SGeorge Wilson * The buf is returned thawed since we expect the consumer to modify it. 3076bbfa8ea8SMatthew Ahrens */ 3077dcbf3bd6SGeorge Wilson arc_buf_t * 30785602294fSDan Kimmel arc_alloc_buf(spa_t *spa, void *tag, arc_buf_contents_t type, int32_t size) 3079ea8dc4b6Seschrock { 3080dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size, 3081dcbf3bd6SGeorge Wilson ZIO_COMPRESS_OFF, type); 3082dcbf3bd6SGeorge Wilson ASSERT(!MUTEX_HELD(HDR_LOCK(hdr))); 30835602294fSDan Kimmel 30845602294fSDan Kimmel arc_buf_t *buf = NULL; 30855602294fSDan Kimmel VERIFY0(arc_buf_alloc_impl(hdr, tag, B_FALSE, B_FALSE, &buf)); 3086dcbf3bd6SGeorge Wilson arc_buf_thaw(buf); 30875602294fSDan Kimmel 30885602294fSDan Kimmel return (buf); 30895602294fSDan Kimmel } 30905602294fSDan Kimmel 30915602294fSDan Kimmel /* 30925602294fSDan Kimmel * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this 30935602294fSDan Kimmel * for bufs containing metadata. 30945602294fSDan Kimmel */ 30955602294fSDan Kimmel arc_buf_t * 30965602294fSDan Kimmel arc_alloc_compressed_buf(spa_t *spa, void *tag, uint64_t psize, uint64_t lsize, 30975602294fSDan Kimmel enum zio_compress compression_type) 30985602294fSDan Kimmel { 30995602294fSDan Kimmel ASSERT3U(lsize, >, 0); 31005602294fSDan Kimmel ASSERT3U(lsize, >=, psize); 31015602294fSDan Kimmel ASSERT(compression_type > ZIO_COMPRESS_OFF); 31025602294fSDan Kimmel ASSERT(compression_type < ZIO_COMPRESS_FUNCTIONS); 31035602294fSDan Kimmel 31045602294fSDan Kimmel arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, 31055602294fSDan Kimmel compression_type, ARC_BUFC_DATA); 31065602294fSDan Kimmel ASSERT(!MUTEX_HELD(HDR_LOCK(hdr))); 31075602294fSDan Kimmel 31085602294fSDan Kimmel arc_buf_t *buf = NULL; 31095602294fSDan Kimmel VERIFY0(arc_buf_alloc_impl(hdr, tag, B_TRUE, B_FALSE, &buf)); 31105602294fSDan Kimmel arc_buf_thaw(buf); 31115602294fSDan Kimmel ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 31125602294fSDan Kimmel 3113770499e1SDan Kimmel if (!arc_buf_is_shared(buf)) { 3114770499e1SDan Kimmel /* 3115770499e1SDan Kimmel * To ensure that the hdr has the correct data in it if we call 3116770499e1SDan Kimmel * arc_decompress() on this buf before it's been written to 3117770499e1SDan Kimmel * disk, it's easiest if we just set up sharing between the 3118770499e1SDan Kimmel * buf and the hdr. 3119770499e1SDan Kimmel */ 3120770499e1SDan Kimmel ASSERT(!abd_is_linear(hdr->b_l1hdr.b_pabd)); 3121770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 3122770499e1SDan Kimmel arc_share_buf(hdr, buf); 3123770499e1SDan Kimmel } 3124770499e1SDan Kimmel 3125dcbf3bd6SGeorge Wilson return (buf); 3126ea8dc4b6Seschrock } 3127ea8dc4b6Seschrock 3128a52fc310SPrakash Surya static void 3129a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) 3130a52fc310SPrakash Surya { 3131a52fc310SPrakash Surya l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 3132a52fc310SPrakash Surya l2arc_dev_t *dev = l2hdr->b_dev; 313316a7e5acSAndriy Gapon uint64_t psize = arc_hdr_size(hdr); 3134a52fc310SPrakash Surya 3135a52fc310SPrakash Surya ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); 3136a52fc310SPrakash Surya ASSERT(HDR_HAS_L2HDR(hdr)); 3137a52fc310SPrakash Surya 3138a52fc310SPrakash Surya list_remove(&dev->l2ad_buflist, hdr); 3139a52fc310SPrakash Surya 314016a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_psize, -psize); 314116a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_lsize, -HDR_GET_LSIZE(hdr)); 3142a52fc310SPrakash Surya 314316a7e5acSAndriy Gapon vdev_space_update(dev->l2ad_vdev, -psize, 0, 0); 3144a52fc310SPrakash Surya 3145e914ace2STim Schumacher (void) zfs_refcount_remove_many(&dev->l2ad_alloc, psize, hdr); 3146dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); 3147a52fc310SPrakash Surya } 3148a52fc310SPrakash Surya 3149fa9e4066Sahrens static void 3150ea8dc4b6Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 3151fa9e4066Sahrens { 315289c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) { 315389c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_buf == NULL || 3154dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt > 0); 3155e914ace2STim Schumacher ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 315689c86e32SChris Williamson ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 315789c86e32SChris Williamson } 3158ea8dc4b6Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 315989c86e32SChris Williamson ASSERT(!HDR_IN_HASH_TABLE(hdr)); 316089c86e32SChris Williamson 3161dcbf3bd6SGeorge Wilson if (!HDR_EMPTY(hdr)) 3162dcbf3bd6SGeorge Wilson buf_discard_identity(hdr); 3163dcbf3bd6SGeorge Wilson 316489c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr)) { 3165a52fc310SPrakash Surya l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 3166a52fc310SPrakash Surya boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); 3167b24ab676SJeff Bonwick 3168a52fc310SPrakash Surya if (!buflist_held) 3169a52fc310SPrakash Surya mutex_enter(&dev->l2ad_mtx); 317089c86e32SChris Williamson 3171244781f1SPrakash Surya /* 3172a52fc310SPrakash Surya * Even though we checked this conditional above, we 3173a52fc310SPrakash Surya * need to check this again now that we have the 3174a52fc310SPrakash Surya * l2ad_mtx. This is because we could be racing with 3175a52fc310SPrakash Surya * another thread calling l2arc_evict() which might have 3176a52fc310SPrakash Surya * destroyed this header's L2 portion as we were waiting 3177a52fc310SPrakash Surya * to acquire the l2ad_mtx. If that happens, we don't 3178a52fc310SPrakash Surya * want to re-destroy the header's L2 portion. 3179244781f1SPrakash Surya */ 3180a52fc310SPrakash Surya if (HDR_HAS_L2HDR(hdr)) 3181a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(hdr); 3182b24ab676SJeff Bonwick 3183b24ab676SJeff Bonwick if (!buflist_held) 3184a52fc310SPrakash Surya mutex_exit(&dev->l2ad_mtx); 3185fa94a07fSbrendan } 3186fa94a07fSbrendan 3187dcbf3bd6SGeorge Wilson if (HDR_HAS_L1HDR(hdr)) { 3188dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 318989c86e32SChris Williamson 3190dcbf3bd6SGeorge Wilson while (hdr->b_l1hdr.b_buf != NULL) 31915602294fSDan Kimmel arc_buf_destroy_impl(hdr->b_l1hdr.b_buf); 319289c86e32SChris Williamson 319389c86e32SChris Williamson #ifdef ZFS_DEBUG 319489c86e32SChris Williamson if (hdr->b_l1hdr.b_thawed != NULL) { 319589c86e32SChris Williamson kmem_free(hdr->b_l1hdr.b_thawed, 1); 319689c86e32SChris Williamson hdr->b_l1hdr.b_thawed = NULL; 319789c86e32SChris Williamson } 319889c86e32SChris Williamson #endif 3199dcbf3bd6SGeorge Wilson 3200770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 3201770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 3202dcbf3bd6SGeorge Wilson } 32033f9d6ad7SLin Ling } 3204ea8dc4b6Seschrock 3205fa9e4066Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 320689c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) { 3207244781f1SPrakash Surya ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 320889c86e32SChris Williamson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 320989c86e32SChris Williamson kmem_cache_free(hdr_full_cache, hdr); 321089c86e32SChris Williamson } else { 321189c86e32SChris Williamson kmem_cache_free(hdr_l2only_cache, hdr); 321289c86e32SChris Williamson } 3213fa9e4066Sahrens } 3214fa9e4066Sahrens 3215fa9e4066Sahrens void 3216dcbf3bd6SGeorge Wilson arc_buf_destroy(arc_buf_t *buf, void* tag) 3217ea8dc4b6Seschrock { 3218ea8dc4b6Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 3219ea8dc4b6Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 3220fa9e4066Sahrens 322189c86e32SChris Williamson if (hdr->b_l1hdr.b_state == arc_anon) { 3222dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 3223dcbf3bd6SGeorge Wilson ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3224dcbf3bd6SGeorge Wilson VERIFY0(remove_reference(hdr, NULL, tag)); 3225dcbf3bd6SGeorge Wilson arc_hdr_destroy(hdr); 3226dcbf3bd6SGeorge Wilson return; 3227ea8dc4b6Seschrock } 3228ea8dc4b6Seschrock 3229ea8dc4b6Seschrock mutex_enter(hash_lock); 3230dcbf3bd6SGeorge Wilson ASSERT3P(hdr, ==, buf->b_hdr); 3231dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 32323f9d6ad7SLin Ling ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3233dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon); 3234dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, !=, NULL); 3235ea8dc4b6Seschrock 3236ea8dc4b6Seschrock (void) remove_reference(hdr, hash_lock, tag); 32375602294fSDan Kimmel arc_buf_destroy_impl(buf); 3238ea8dc4b6Seschrock mutex_exit(hash_lock); 3239fa9e4066Sahrens } 3240fa9e4066Sahrens 3241fa9e4066Sahrens /* 3242244781f1SPrakash Surya * Evict the arc_buf_hdr that is provided as a parameter. The resultant 3243244781f1SPrakash Surya * state of the header is dependent on it's state prior to entering this 3244244781f1SPrakash Surya * function. The following transitions are possible: 3245874395d5Smaybee * 3246244781f1SPrakash Surya * - arc_mru -> arc_mru_ghost 3247244781f1SPrakash Surya * - arc_mfu -> arc_mfu_ghost 3248244781f1SPrakash Surya * - arc_mru_ghost -> arc_l2c_only 3249244781f1SPrakash Surya * - arc_mru_ghost -> deleted 3250244781f1SPrakash Surya * - arc_mfu_ghost -> arc_l2c_only 3251244781f1SPrakash Surya * - arc_mfu_ghost -> deleted 3252fa9e4066Sahrens */ 3253244781f1SPrakash Surya static int64_t 3254244781f1SPrakash Surya arc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 3255fa9e4066Sahrens { 3256244781f1SPrakash Surya arc_state_t *evicted_state, *state; 3257244781f1SPrakash Surya int64_t bytes_evicted = 0; 3258*a3874b8bSToomas Soome int min_lifetime = HDR_PRESCIENT_PREFETCH(hdr) ? 3259*a3874b8bSToomas Soome zfs_arc_min_prescient_prefetch_ms : zfs_arc_min_prefetch_ms; 3260fa9e4066Sahrens 3261244781f1SPrakash Surya ASSERT(MUTEX_HELD(hash_lock)); 3262244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 3263fa9e4066Sahrens 3264244781f1SPrakash Surya state = hdr->b_l1hdr.b_state; 3265244781f1SPrakash Surya if (GHOST_STATE(state)) { 3266244781f1SPrakash Surya ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3267dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 3268fa9e4066Sahrens 3269244781f1SPrakash Surya /* 3270244781f1SPrakash Surya * l2arc_write_buffers() relies on a header's L1 portion 3271770499e1SDan Kimmel * (i.e. its b_pabd field) during it's write phase. 3272244781f1SPrakash Surya * Thus, we cannot push a header onto the arc_l2c_only 3273244781f1SPrakash Surya * state (removing it's L1 piece) until the header is 3274244781f1SPrakash Surya * done being written to the l2arc. 3275244781f1SPrakash Surya */ 3276244781f1SPrakash Surya if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { 3277244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_l2_skip); 3278244781f1SPrakash Surya return (bytes_evicted); 32793a5286a1SMatthew Ahrens } 3280244781f1SPrakash Surya 3281244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_deleted); 3282dcbf3bd6SGeorge Wilson bytes_evicted += HDR_GET_LSIZE(hdr); 3283244781f1SPrakash Surya 3284244781f1SPrakash Surya DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); 3285244781f1SPrakash Surya 3286770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 3287244781f1SPrakash Surya if (HDR_HAS_L2HDR(hdr)) { 3288244781f1SPrakash Surya /* 3289244781f1SPrakash Surya * This buffer is cached on the 2nd Level ARC; 3290244781f1SPrakash Surya * don't destroy the header. 3291244781f1SPrakash Surya */ 3292244781f1SPrakash Surya arc_change_state(arc_l2c_only, hdr, hash_lock); 32933a5286a1SMatthew Ahrens /* 3294244781f1SPrakash Surya * dropping from L1+L2 cached to L2-only, 3295244781f1SPrakash Surya * realloc to remove the L1 header. 32963a5286a1SMatthew Ahrens */ 3297244781f1SPrakash Surya hdr = arc_hdr_realloc(hdr, hdr_full_cache, 3298244781f1SPrakash Surya hdr_l2only_cache); 3299244781f1SPrakash Surya } else { 3300244781f1SPrakash Surya arc_change_state(arc_anon, hdr, hash_lock); 3301244781f1SPrakash Surya arc_hdr_destroy(hdr); 33023a5286a1SMatthew Ahrens } 3303244781f1SPrakash Surya return (bytes_evicted); 33043a5286a1SMatthew Ahrens } 33053a5286a1SMatthew Ahrens 3306244781f1SPrakash Surya ASSERT(state == arc_mru || state == arc_mfu); 3307244781f1SPrakash Surya evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 3308244781f1SPrakash Surya 3309244781f1SPrakash Surya /* prefetch buffers have a minimum lifespan */ 3310244781f1SPrakash Surya if (HDR_IO_IN_PROGRESS(hdr) || 3311244781f1SPrakash Surya ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && 3312*a3874b8bSToomas Soome ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < min_lifetime * hz)) { 3313244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_skip); 3314244781f1SPrakash Surya return (bytes_evicted); 3315244781f1SPrakash Surya } 3316244781f1SPrakash Surya 3317e914ace2STim Schumacher ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt)); 3318244781f1SPrakash Surya while (hdr->b_l1hdr.b_buf) { 3319244781f1SPrakash Surya arc_buf_t *buf = hdr->b_l1hdr.b_buf; 3320244781f1SPrakash Surya if (!mutex_tryenter(&buf->b_evict_lock)) { 3321244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_mutex_miss); 3322244781f1SPrakash Surya break; 332313506d1eSmaybee } 3324244781f1SPrakash Surya if (buf->b_data != NULL) 3325dcbf3bd6SGeorge Wilson bytes_evicted += HDR_GET_LSIZE(hdr); 3326dcbf3bd6SGeorge Wilson mutex_exit(&buf->b_evict_lock); 33275602294fSDan Kimmel arc_buf_destroy_impl(buf); 3328244781f1SPrakash Surya } 332969962b56SMatthew Ahrens 3330244781f1SPrakash Surya if (HDR_HAS_L2HDR(hdr)) { 3331dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr)); 3332244781f1SPrakash Surya } else { 3333dcbf3bd6SGeorge Wilson if (l2arc_write_eligible(hdr->b_spa, hdr)) { 3334dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_evict_l2_eligible, 3335dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr)); 3336dcbf3bd6SGeorge Wilson } else { 3337dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_evict_l2_ineligible, 3338dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr)); 3339dcbf3bd6SGeorge Wilson } 3340244781f1SPrakash Surya } 3341244781f1SPrakash Surya 3342dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_bufcnt == 0) { 3343dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 3344dcbf3bd6SGeorge Wilson 3345dcbf3bd6SGeorge Wilson bytes_evicted += arc_hdr_size(hdr); 3346dcbf3bd6SGeorge Wilson 3347dcbf3bd6SGeorge Wilson /* 3348dcbf3bd6SGeorge Wilson * If this hdr is being evicted and has a compressed 3349dcbf3bd6SGeorge Wilson * buffer then we discard it here before we change states. 3350dcbf3bd6SGeorge Wilson * This ensures that the accounting is updated correctly 3351770499e1SDan Kimmel * in arc_free_data_impl(). 3352dcbf3bd6SGeorge Wilson */ 3353770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 3354dcbf3bd6SGeorge Wilson 3355244781f1SPrakash Surya arc_change_state(evicted_state, hdr, hash_lock); 3356244781f1SPrakash Surya ASSERT(HDR_IN_HASH_TABLE(hdr)); 3357dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 3358244781f1SPrakash Surya DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); 3359244781f1SPrakash Surya } 3360244781f1SPrakash Surya 3361244781f1SPrakash Surya return (bytes_evicted); 3362244781f1SPrakash Surya } 3363244781f1SPrakash Surya 3364244781f1SPrakash Surya static uint64_t 3365244781f1SPrakash Surya arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, 3366244781f1SPrakash Surya uint64_t spa, int64_t bytes) 3367244781f1SPrakash Surya { 3368244781f1SPrakash Surya multilist_sublist_t *mls; 3369244781f1SPrakash Surya uint64_t bytes_evicted = 0; 3370244781f1SPrakash Surya arc_buf_hdr_t *hdr; 3371244781f1SPrakash Surya kmutex_t *hash_lock; 3372244781f1SPrakash Surya int evict_count = 0; 3373244781f1SPrakash Surya 3374244781f1SPrakash Surya ASSERT3P(marker, !=, NULL); 3375244781f1SPrakash Surya IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 3376244781f1SPrakash Surya 3377244781f1SPrakash Surya mls = multilist_sublist_lock(ml, idx); 3378244781f1SPrakash Surya 3379244781f1SPrakash Surya for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL; 3380244781f1SPrakash Surya hdr = multilist_sublist_prev(mls, marker)) { 3381244781f1SPrakash Surya if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) || 3382244781f1SPrakash Surya (evict_count >= zfs_arc_evict_batch_limit)) 3383244781f1SPrakash Surya break; 338469962b56SMatthew Ahrens 338569962b56SMatthew Ahrens /* 3386244781f1SPrakash Surya * To keep our iteration location, move the marker 3387244781f1SPrakash Surya * forward. Since we're not holding hdr's hash lock, we 3388244781f1SPrakash Surya * must be very careful and not remove 'hdr' from the 3389244781f1SPrakash Surya * sublist. Otherwise, other consumers might mistake the 3390244781f1SPrakash Surya * 'hdr' as not being on a sublist when they call the 3391244781f1SPrakash Surya * multilist_link_active() function (they all rely on 3392244781f1SPrakash Surya * the hash lock protecting concurrent insertions and 3393244781f1SPrakash Surya * removals). multilist_sublist_move_forward() was 3394244781f1SPrakash Surya * specifically implemented to ensure this is the case 3395244781f1SPrakash Surya * (only 'marker' will be removed and re-inserted). 3396244781f1SPrakash Surya */ 3397244781f1SPrakash Surya multilist_sublist_move_forward(mls, marker); 3398244781f1SPrakash Surya 3399244781f1SPrakash Surya /* 3400244781f1SPrakash Surya * The only case where the b_spa field should ever be 3401244781f1SPrakash Surya * zero, is the marker headers inserted by 3402244781f1SPrakash Surya * arc_evict_state(). It's possible for multiple threads 3403244781f1SPrakash Surya * to be calling arc_evict_state() concurrently (e.g. 3404244781f1SPrakash Surya * dsl_pool_close() and zio_inject_fault()), so we must 3405244781f1SPrakash Surya * skip any markers we see from these other threads. 340669962b56SMatthew Ahrens */ 3407244781f1SPrakash Surya if (hdr->b_spa == 0) 3408244781f1SPrakash Surya continue; 3409244781f1SPrakash Surya 3410244781f1SPrakash Surya /* we're only interested in evicting buffers of a certain spa */ 3411244781f1SPrakash Surya if (spa != 0 && hdr->b_spa != spa) { 3412244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_skip); 341369962b56SMatthew Ahrens continue; 341469962b56SMatthew Ahrens } 341569962b56SMatthew Ahrens 34167adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 34175ea40c06SBrendan Gregg - Sun Microsystems 3418244781f1SPrakash Surya /* 3419244781f1SPrakash Surya * We aren't calling this function from any code path 3420244781f1SPrakash Surya * that would already be holding a hash lock, so we're 3421244781f1SPrakash Surya * asserting on this assumption to be defensive in case 3422244781f1SPrakash Surya * this ever changes. Without this check, it would be 3423244781f1SPrakash Surya * possible to incorrectly increment arcstat_mutex_miss 3424244781f1SPrakash Surya * below (e.g. if the code changed such that we called 3425244781f1SPrakash Surya * this function with a hash lock held). 3426244781f1SPrakash Surya */ 3427244781f1SPrakash Surya ASSERT(!MUTEX_HELD(hash_lock)); 342844cb6abcSbmc 3429244781f1SPrakash Surya if (mutex_tryenter(hash_lock)) { 3430244781f1SPrakash Surya uint64_t evicted = arc_evict_hdr(hdr, hash_lock); 3431244781f1SPrakash Surya mutex_exit(hash_lock); 3432fa9e4066Sahrens 3433244781f1SPrakash Surya bytes_evicted += evicted; 3434fa9e4066Sahrens 3435244781f1SPrakash Surya /* 3436244781f1SPrakash Surya * If evicted is zero, arc_evict_hdr() must have 3437244781f1SPrakash Surya * decided to skip this header, don't increment 3438244781f1SPrakash Surya * evict_count in this case. 3439244781f1SPrakash Surya */ 3440244781f1SPrakash Surya if (evicted != 0) 3441244781f1SPrakash Surya evict_count++; 344244cb6abcSbmc 3443244781f1SPrakash Surya /* 3444244781f1SPrakash Surya * If arc_size isn't overflowing, signal any 3445244781f1SPrakash Surya * threads that might happen to be waiting. 3446244781f1SPrakash Surya * 3447244781f1SPrakash Surya * For each header evicted, we wake up a single 3448244781f1SPrakash Surya * thread. If we used cv_broadcast, we could 3449244781f1SPrakash Surya * wake up "too many" threads causing arc_size 3450244781f1SPrakash Surya * to significantly overflow arc_c; since 3451770499e1SDan Kimmel * arc_get_data_impl() doesn't check for overflow 3452244781f1SPrakash Surya * when it's woken up (it doesn't because it's 3453244781f1SPrakash Surya * possible for the ARC to be overflowing while 3454244781f1SPrakash Surya * full of un-evictable buffers, and the 3455244781f1SPrakash Surya * function should proceed in this case). 3456244781f1SPrakash Surya * 3457244781f1SPrakash Surya * If threads are left sleeping, due to not 3458de753e34SBrad Lewis * using cv_broadcast here, they will be woken 3459de753e34SBrad Lewis * up via cv_broadcast in arc_adjust_cb() just 3460de753e34SBrad Lewis * before arc_adjust_zthr sleeps. 3461244781f1SPrakash Surya */ 3462de753e34SBrad Lewis mutex_enter(&arc_adjust_lock); 3463244781f1SPrakash Surya if (!arc_is_overflowing()) 3464de753e34SBrad Lewis cv_signal(&arc_adjust_waiters_cv); 3465de753e34SBrad Lewis mutex_exit(&arc_adjust_lock); 3466244781f1SPrakash Surya } else { 3467244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_mutex_miss); 3468244781f1SPrakash Surya } 3469244781f1SPrakash Surya } 3470f4d2e9e6Smaybee 3471244781f1SPrakash Surya multilist_sublist_unlock(mls); 347244cb6abcSbmc 3473244781f1SPrakash Surya return (bytes_evicted); 3474fa9e4066Sahrens } 3475fa9e4066Sahrens 3476fa9e4066Sahrens /* 3477244781f1SPrakash Surya * Evict buffers from the given arc state, until we've removed the 3478244781f1SPrakash Surya * specified number of bytes. Move the removed buffers to the 3479244781f1SPrakash Surya * appropriate evict state. 3480244781f1SPrakash Surya * 3481244781f1SPrakash Surya * This function makes a "best effort". It skips over any buffers 3482244781f1SPrakash Surya * it can't get a hash_lock on, and so, may not catch all candidates. 3483244781f1SPrakash Surya * It may also return without evicting as much space as requested. 3484244781f1SPrakash Surya * 3485244781f1SPrakash Surya * If bytes is specified using the special value ARC_EVICT_ALL, this 3486244781f1SPrakash Surya * will evict all available (i.e. unlocked and evictable) buffers from 3487244781f1SPrakash Surya * the given arc state; which is used by arc_flush(). 3488fa9e4066Sahrens */ 3489244781f1SPrakash Surya static uint64_t 3490244781f1SPrakash Surya arc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes, 3491244781f1SPrakash Surya arc_buf_contents_t type) 3492fa9e4066Sahrens { 3493244781f1SPrakash Surya uint64_t total_evicted = 0; 349494c2d0ebSMatthew Ahrens multilist_t *ml = state->arcs_list[type]; 3495244781f1SPrakash Surya int num_sublists; 3496244781f1SPrakash Surya arc_buf_hdr_t **markers; 3497fa9e4066Sahrens 3498244781f1SPrakash Surya IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 3499b802aa8cSSanjeev Bagewadi 3500244781f1SPrakash Surya num_sublists = multilist_get_num_sublists(ml); 3501b802aa8cSSanjeev Bagewadi 3502244781f1SPrakash Surya /* 3503244781f1SPrakash Surya * If we've tried to evict from each sublist, made some 3504244781f1SPrakash Surya * progress, but still have not hit the target number of bytes 3505244781f1SPrakash Surya * to evict, we want to keep trying. The markers allow us to 3506244781f1SPrakash Surya * pick up where we left off for each individual sublist, rather 3507244781f1SPrakash Surya * than starting from the tail each time. 3508244781f1SPrakash Surya */ 3509244781f1SPrakash Surya markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); 3510244781f1SPrakash Surya for (int i = 0; i < num_sublists; i++) { 3511244781f1SPrakash Surya markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); 351269962b56SMatthew Ahrens 351369962b56SMatthew Ahrens /* 3514244781f1SPrakash Surya * A b_spa of 0 is used to indicate that this header is 3515244781f1SPrakash Surya * a marker. This fact is used in arc_adjust_type() and 3516244781f1SPrakash Surya * arc_evict_state_impl(). 351769962b56SMatthew Ahrens */ 3518244781f1SPrakash Surya markers[i]->b_spa = 0; 3519fa94a07fSbrendan 3520244781f1SPrakash Surya multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 3521244781f1SPrakash Surya multilist_sublist_insert_tail(mls, markers[i]); 3522244781f1SPrakash Surya multilist_sublist_unlock(mls); 3523244781f1SPrakash Surya } 3524fa94a07fSbrendan 3525244781f1SPrakash Surya /* 3526244781f1SPrakash Surya * While we haven't hit our target number of bytes to evict, or 3527244781f1SPrakash Surya * we're evicting all available buffers. 3528244781f1SPrakash Surya */ 3529244781f1SPrakash Surya while (total_evicted < bytes || bytes == ARC_EVICT_ALL) { 3530244781f1SPrakash Surya /* 3531244781f1SPrakash Surya * Start eviction using a randomly selected sublist, 3532244781f1SPrakash Surya * this is to try and evenly balance eviction across all 3533244781f1SPrakash Surya * sublists. Always starting at the same sublist 3534244781f1SPrakash Surya * (e.g. index 0) would cause evictions to favor certain 3535244781f1SPrakash Surya * sublists over others. 3536244781f1SPrakash Surya */ 3537244781f1SPrakash Surya int sublist_idx = multilist_get_random_index(ml); 3538244781f1SPrakash Surya uint64_t scan_evicted = 0; 3539244781f1SPrakash Surya 3540244781f1SPrakash Surya for (int i = 0; i < num_sublists; i++) { 3541244781f1SPrakash Surya uint64_t bytes_remaining; 3542244781f1SPrakash Surya uint64_t bytes_evicted; 3543244781f1SPrakash Surya 3544244781f1SPrakash Surya if (bytes == ARC_EVICT_ALL) 3545244781f1SPrakash Surya bytes_remaining = ARC_EVICT_ALL; 3546244781f1SPrakash Surya else if (total_evicted < bytes) 3547244781f1SPrakash Surya bytes_remaining = bytes - total_evicted; 3548244781f1SPrakash Surya else 3549fa9e4066Sahrens break; 3550244781f1SPrakash Surya 3551244781f1SPrakash Surya bytes_evicted = arc_evict_state_impl(ml, sublist_idx, 3552244781f1SPrakash Surya markers[sublist_idx], spa, bytes_remaining); 3553244781f1SPrakash Surya 3554244781f1SPrakash Surya scan_evicted += bytes_evicted; 3555244781f1SPrakash Surya total_evicted += bytes_evicted; 3556244781f1SPrakash Surya 3557244781f1SPrakash Surya /* we've reached the end, wrap to the beginning */ 3558244781f1SPrakash Surya if (++sublist_idx >= num_sublists) 3559244781f1SPrakash Surya sublist_idx = 0; 3560244781f1SPrakash Surya } 3561244781f1SPrakash Surya 3562244781f1SPrakash Surya /* 3563244781f1SPrakash Surya * If we didn't evict anything during this scan, we have 3564244781f1SPrakash Surya * no reason to believe we'll evict more during another 3565244781f1SPrakash Surya * scan, so break the loop. 3566244781f1SPrakash Surya */ 3567244781f1SPrakash Surya if (scan_evicted == 0) { 3568244781f1SPrakash Surya /* This isn't possible, let's make that obvious */ 3569244781f1SPrakash Surya ASSERT3S(bytes, !=, 0); 3570244781f1SPrakash Surya 3571b802aa8cSSanjeev Bagewadi /* 3572244781f1SPrakash Surya * When bytes is ARC_EVICT_ALL, the only way to 3573244781f1SPrakash Surya * break the loop is when scan_evicted is zero. 3574244781f1SPrakash Surya * In that case, we actually have evicted enough, 3575244781f1SPrakash Surya * so we don't want to increment the kstat. 3576b802aa8cSSanjeev Bagewadi */ 3577244781f1SPrakash Surya if (bytes != ARC_EVICT_ALL) { 3578244781f1SPrakash Surya ASSERT3S(total_evicted, <, bytes); 3579244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_not_enough); 3580244781f1SPrakash Surya } 3581244781f1SPrakash Surya 3582244781f1SPrakash Surya break; 358369962b56SMatthew Ahrens } 3584244781f1SPrakash Surya } 3585244781f1SPrakash Surya 3586244781f1SPrakash Surya for (int i = 0; i < num_sublists; i++) { 3587244781f1SPrakash Surya multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 3588244781f1SPrakash Surya multilist_sublist_remove(mls, markers[i]); 3589244781f1SPrakash Surya multilist_sublist_unlock(mls); 359069962b56SMatthew Ahrens 3591244781f1SPrakash Surya kmem_cache_free(hdr_full_cache, markers[i]); 3592fa9e4066Sahrens } 3593244781f1SPrakash Surya kmem_free(markers, sizeof (*markers) * num_sublists); 3594fa9e4066Sahrens 3595244781f1SPrakash Surya return (total_evicted); 3596244781f1SPrakash Surya } 3597244781f1SPrakash Surya 3598244781f1SPrakash Surya /* 3599244781f1SPrakash Surya * Flush all "evictable" data of the given type from the arc state 3600244781f1SPrakash Surya * specified. This will not evict any "active" buffers (i.e. referenced). 3601244781f1SPrakash Surya * 3602dcbf3bd6SGeorge Wilson * When 'retry' is set to B_FALSE, the function will make a single pass 3603244781f1SPrakash Surya * over the state and evict any buffers that it can. Since it doesn't 3604244781f1SPrakash Surya * continually retry the eviction, it might end up leaving some buffers 3605244781f1SPrakash Surya * in the ARC due to lock misses. 3606244781f1SPrakash Surya * 3607dcbf3bd6SGeorge Wilson * When 'retry' is set to B_TRUE, the function will continually retry the 3608244781f1SPrakash Surya * eviction until *all* evictable buffers have been removed from the 3609244781f1SPrakash Surya * state. As a result, if concurrent insertions into the state are 3610244781f1SPrakash Surya * allowed (e.g. if the ARC isn't shutting down), this function might 3611244781f1SPrakash Surya * wind up in an infinite loop, continually trying to evict buffers. 3612244781f1SPrakash Surya */ 3613244781f1SPrakash Surya static uint64_t 3614244781f1SPrakash Surya arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, 3615244781f1SPrakash Surya boolean_t retry) 3616244781f1SPrakash Surya { 3617244781f1SPrakash Surya uint64_t evicted = 0; 3618244781f1SPrakash Surya 3619e914ace2STim Schumacher while (zfs_refcount_count(&state->arcs_esize[type]) != 0) { 3620244781f1SPrakash Surya evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); 3621244781f1SPrakash Surya 3622244781f1SPrakash Surya if (!retry) 3623244781f1SPrakash Surya break; 36240e8c6158Smaybee } 36250e8c6158Smaybee 3626244781f1SPrakash Surya return (evicted); 3627244781f1SPrakash Surya } 3628244781f1SPrakash Surya 3629244781f1SPrakash Surya /* 3630244781f1SPrakash Surya * Evict the specified number of bytes from the state specified, 3631244781f1SPrakash Surya * restricting eviction to the spa and type given. This function 3632244781f1SPrakash Surya * prevents us from trying to evict more from a state's list than 3633244781f1SPrakash Surya * is "evictable", and to skip evicting altogether when passed a 3634244781f1SPrakash Surya * negative value for "bytes". In contrast, arc_evict_state() will 3635244781f1SPrakash Surya * evict everything it can, when passed a negative value for "bytes". 3636244781f1SPrakash Surya */ 3637244781f1SPrakash Surya static uint64_t 3638244781f1SPrakash Surya arc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes, 3639244781f1SPrakash Surya arc_buf_contents_t type) 3640244781f1SPrakash Surya { 3641244781f1SPrakash Surya int64_t delta; 3642244781f1SPrakash Surya 3643e914ace2STim Schumacher if (bytes > 0 && zfs_refcount_count(&state->arcs_esize[type]) > 0) { 3644e914ace2STim Schumacher delta = MIN(zfs_refcount_count(&state->arcs_esize[type]), 3645e914ace2STim Schumacher bytes); 3646244781f1SPrakash Surya return (arc_evict_state(state, spa, delta, type)); 3647fa9e4066Sahrens } 3648fa9e4066Sahrens 3649244781f1SPrakash Surya return (0); 3650fa9e4066Sahrens } 3651fa9e4066Sahrens 3652244781f1SPrakash Surya /* 3653244781f1SPrakash Surya * Evict metadata buffers from the cache, such that arc_meta_used is 3654244781f1SPrakash Surya * capped by the arc_meta_limit tunable. 3655244781f1SPrakash Surya */ 3656244781f1SPrakash Surya static uint64_t 36573a2d8a1bSPaul Dagnelie arc_adjust_meta(uint64_t meta_used) 3658244781f1SPrakash Surya { 3659244781f1SPrakash Surya uint64_t total_evicted = 0; 3660244781f1SPrakash Surya int64_t target; 3661244781f1SPrakash Surya 3662244781f1SPrakash Surya /* 3663244781f1SPrakash Surya * If we're over the meta limit, we want to evict enough 3664244781f1SPrakash Surya * metadata to get back under the meta limit. We don't want to 3665244781f1SPrakash Surya * evict so much that we drop the MRU below arc_p, though. If 3666244781f1SPrakash Surya * we're over the meta limit more than we're over arc_p, we 3667244781f1SPrakash Surya * evict some from the MRU here, and some from the MFU below. 3668244781f1SPrakash Surya */ 36693a2d8a1bSPaul Dagnelie target = MIN((int64_t)(meta_used - arc_meta_limit), 3670e914ace2STim Schumacher (int64_t)(zfs_refcount_count(&arc_anon->arcs_size) + 3671e914ace2STim Schumacher zfs_refcount_count(&arc_mru->arcs_size) - arc_p)); 3672244781f1SPrakash Surya 3673244781f1SPrakash Surya total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3674244781f1SPrakash Surya 3675244781f1SPrakash Surya /* 3676244781f1SPrakash Surya * Similar to the above, we want to evict enough bytes to get us 3677244781f1SPrakash Surya * below the meta limit, but not so much as to drop us below the 36785602294fSDan Kimmel * space allotted to the MFU (which is defined as arc_c - arc_p). 3679244781f1SPrakash Surya */ 36803a2d8a1bSPaul Dagnelie target = MIN((int64_t)(meta_used - arc_meta_limit), 3681e914ace2STim Schumacher (int64_t)(zfs_refcount_count(&arc_mfu->arcs_size) - 36823a2d8a1bSPaul Dagnelie (arc_c - arc_p))); 3683244781f1SPrakash Surya 3684244781f1SPrakash Surya total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3685244781f1SPrakash Surya 3686244781f1SPrakash Surya return (total_evicted); 3687244781f1SPrakash Surya } 3688244781f1SPrakash Surya 3689244781f1SPrakash Surya /* 3690244781f1SPrakash Surya * Return the type of the oldest buffer in the given arc state 3691244781f1SPrakash Surya * 3692244781f1SPrakash Surya * This function will select a random sublist of type ARC_BUFC_DATA and 3693244781f1SPrakash Surya * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist 3694244781f1SPrakash Surya * is compared, and the type which contains the "older" buffer will be 3695244781f1SPrakash Surya * returned. 3696244781f1SPrakash Surya */ 3697244781f1SPrakash Surya static arc_buf_contents_t 3698244781f1SPrakash Surya arc_adjust_type(arc_state_t *state) 3699244781f1SPrakash Surya { 370094c2d0ebSMatthew Ahrens multilist_t *data_ml = state->arcs_list[ARC_BUFC_DATA]; 370194c2d0ebSMatthew Ahrens multilist_t *meta_ml = state->arcs_list[ARC_BUFC_METADATA]; 3702244781f1SPrakash Surya int data_idx = multilist_get_random_index(data_ml); 3703244781f1SPrakash Surya int meta_idx = multilist_get_random_index(meta_ml); 3704244781f1SPrakash Surya multilist_sublist_t *data_mls; 3705244781f1SPrakash Surya multilist_sublist_t *meta_mls; 3706244781f1SPrakash Surya arc_buf_contents_t type; 3707244781f1SPrakash Surya arc_buf_hdr_t *data_hdr; 3708244781f1SPrakash Surya arc_buf_hdr_t *meta_hdr; 3709244781f1SPrakash Surya 3710244781f1SPrakash Surya /* 3711244781f1SPrakash Surya * We keep the sublist lock until we're finished, to prevent 3712244781f1SPrakash Surya * the headers from being destroyed via arc_evict_state(). 3713244781f1SPrakash Surya */ 3714244781f1SPrakash Surya data_mls = multilist_sublist_lock(data_ml, data_idx); 3715244781f1SPrakash Surya meta_mls = multilist_sublist_lock(meta_ml, meta_idx); 3716244781f1SPrakash Surya 3717244781f1SPrakash Surya /* 3718244781f1SPrakash Surya * These two loops are to ensure we skip any markers that 3719244781f1SPrakash Surya * might be at the tail of the lists due to arc_evict_state(). 3720244781f1SPrakash Surya */ 3721244781f1SPrakash Surya 3722244781f1SPrakash Surya for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; 3723244781f1SPrakash Surya data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { 3724244781f1SPrakash Surya if (data_hdr->b_spa != 0) 3725244781f1SPrakash Surya break; 3726244781f1SPrakash Surya } 3727244781f1SPrakash Surya 3728244781f1SPrakash Surya for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; 3729244781f1SPrakash Surya meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { 3730244781f1SPrakash Surya if (meta_hdr->b_spa != 0) 3731244781f1SPrakash Surya break; 3732244781f1SPrakash Surya } 3733244781f1SPrakash Surya 3734244781f1SPrakash Surya if (data_hdr == NULL && meta_hdr == NULL) { 3735244781f1SPrakash Surya type = ARC_BUFC_DATA; 3736244781f1SPrakash Surya } else if (data_hdr == NULL) { 3737244781f1SPrakash Surya ASSERT3P(meta_hdr, !=, NULL); 3738244781f1SPrakash Surya type = ARC_BUFC_METADATA; 3739244781f1SPrakash Surya } else if (meta_hdr == NULL) { 3740244781f1SPrakash Surya ASSERT3P(data_hdr, !=, NULL); 3741244781f1SPrakash Surya type = ARC_BUFC_DATA; 3742244781f1SPrakash Surya } else { 3743244781f1SPrakash Surya ASSERT3P(data_hdr, !=, NULL); 3744244781f1SPrakash Surya ASSERT3P(meta_hdr, !=, NULL); 3745244781f1SPrakash Surya 3746244781f1SPrakash Surya /* The headers can't be on the sublist without an L1 header */ 3747244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(data_hdr)); 3748244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(meta_hdr)); 3749244781f1SPrakash Surya 3750244781f1SPrakash Surya if (data_hdr->b_l1hdr.b_arc_access < 3751244781f1SPrakash Surya meta_hdr->b_l1hdr.b_arc_access) { 3752244781f1SPrakash Surya type = ARC_BUFC_DATA; 3753244781f1SPrakash Surya } else { 3754244781f1SPrakash Surya type = ARC_BUFC_METADATA; 3755244781f1SPrakash Surya } 3756244781f1SPrakash Surya } 3757244781f1SPrakash Surya 3758244781f1SPrakash Surya multilist_sublist_unlock(meta_mls); 3759244781f1SPrakash Surya multilist_sublist_unlock(data_mls); 3760244781f1SPrakash Surya 3761244781f1SPrakash Surya return (type); 3762244781f1SPrakash Surya } 3763244781f1SPrakash Surya 3764244781f1SPrakash Surya /* 3765244781f1SPrakash Surya * Evict buffers from the cache, such that arc_size is capped by arc_c. 3766244781f1SPrakash Surya */ 3767244781f1SPrakash Surya static uint64_t 3768fa9e4066Sahrens arc_adjust(void) 3769fa9e4066Sahrens { 3770244781f1SPrakash Surya uint64_t total_evicted = 0; 3771244781f1SPrakash Surya uint64_t bytes; 3772244781f1SPrakash Surya int64_t target; 37733a2d8a1bSPaul Dagnelie uint64_t asize = aggsum_value(&arc_size); 37743a2d8a1bSPaul Dagnelie uint64_t ameta = aggsum_value(&arc_meta_used); 3775fa9e4066Sahrens 37765a98e54bSBrendan Gregg - Sun Microsystems /* 3777244781f1SPrakash Surya * If we're over arc_meta_limit, we want to correct that before 3778244781f1SPrakash Surya * potentially evicting data buffers below. 37795a98e54bSBrendan Gregg - Sun Microsystems */ 37803a2d8a1bSPaul Dagnelie total_evicted += arc_adjust_meta(ameta); 37815a98e54bSBrendan Gregg - Sun Microsystems 3782244781f1SPrakash Surya /* 3783244781f1SPrakash Surya * Adjust MRU size 3784244781f1SPrakash Surya * 3785244781f1SPrakash Surya * If we're over the target cache size, we want to evict enough 3786244781f1SPrakash Surya * from the list to get back to our target size. We don't want 3787244781f1SPrakash Surya * to evict too much from the MRU, such that it drops below 3788244781f1SPrakash Surya * arc_p. So, if we're over our target cache size more than 3789244781f1SPrakash Surya * the MRU is over arc_p, we'll evict enough to get back to 3790244781f1SPrakash Surya * arc_p here, and then evict more from the MFU below. 3791244781f1SPrakash Surya */ 37923a2d8a1bSPaul Dagnelie target = MIN((int64_t)(asize - arc_c), 3793e914ace2STim Schumacher (int64_t)(zfs_refcount_count(&arc_anon->arcs_size) + 3794e914ace2STim Schumacher zfs_refcount_count(&arc_mru->arcs_size) + ameta - arc_p)); 3795fa9e4066Sahrens 3796244781f1SPrakash Surya /* 3797244781f1SPrakash Surya * If we're below arc_meta_min, always prefer to evict data. 3798244781f1SPrakash Surya * Otherwise, try to satisfy the requested number of bytes to 3799244781f1SPrakash Surya * evict from the type which contains older buffers; in an 3800244781f1SPrakash Surya * effort to keep newer buffers in the cache regardless of their 3801244781f1SPrakash Surya * type. If we cannot satisfy the number of bytes from this 3802244781f1SPrakash Surya * type, spill over into the next type. 3803244781f1SPrakash Surya */ 3804244781f1SPrakash Surya if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA && 38053a2d8a1bSPaul Dagnelie ameta > arc_meta_min) { 3806244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3807244781f1SPrakash Surya total_evicted += bytes; 38080e8c6158Smaybee 3809244781f1SPrakash Surya /* 3810244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3811244781f1SPrakash Surya * metadata, we try to get the rest from data. 3812244781f1SPrakash Surya */ 3813244781f1SPrakash Surya target -= bytes; 3814244781f1SPrakash Surya 3815244781f1SPrakash Surya total_evicted += 3816244781f1SPrakash Surya arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3817244781f1SPrakash Surya } else { 3818244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3819244781f1SPrakash Surya total_evicted += bytes; 3820244781f1SPrakash Surya 3821244781f1SPrakash Surya /* 3822244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3823244781f1SPrakash Surya * data, we try to get the rest from metadata. 3824244781f1SPrakash Surya */ 3825244781f1SPrakash Surya target -= bytes; 3826244781f1SPrakash Surya 3827244781f1SPrakash Surya total_evicted += 3828244781f1SPrakash Surya arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3829fa9e4066Sahrens } 3830fa9e4066Sahrens 38315a98e54bSBrendan Gregg - Sun Microsystems /* 38325a98e54bSBrendan Gregg - Sun Microsystems * Adjust MFU size 3833244781f1SPrakash Surya * 3834244781f1SPrakash Surya * Now that we've tried to evict enough from the MRU to get its 3835244781f1SPrakash Surya * size back to arc_p, if we're still above the target cache 3836244781f1SPrakash Surya * size, we evict the rest from the MFU. 38375a98e54bSBrendan Gregg - Sun Microsystems */ 38383a2d8a1bSPaul Dagnelie target = asize - arc_c; 3839fa9e4066Sahrens 384031c46cf2SAlek Pinchuk if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA && 38413a2d8a1bSPaul Dagnelie ameta > arc_meta_min) { 3842244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3843244781f1SPrakash Surya total_evicted += bytes; 38445a98e54bSBrendan Gregg - Sun Microsystems 3845244781f1SPrakash Surya /* 3846244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3847244781f1SPrakash Surya * metadata, we try to get the rest from data. 3848244781f1SPrakash Surya */ 3849244781f1SPrakash Surya target -= bytes; 3850244781f1SPrakash Surya 3851244781f1SPrakash Surya total_evicted += 3852244781f1SPrakash Surya arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3853244781f1SPrakash Surya } else { 3854244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3855244781f1SPrakash Surya total_evicted += bytes; 3856244781f1SPrakash Surya 3857244781f1SPrakash Surya /* 3858244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3859244781f1SPrakash Surya * data, we try to get the rest from data. 3860244781f1SPrakash Surya */ 3861244781f1SPrakash Surya target -= bytes; 3862fa9e4066Sahrens 3863244781f1SPrakash Surya total_evicted += 3864244781f1SPrakash Surya arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 38655a98e54bSBrendan Gregg - Sun Microsystems } 3866fa9e4066Sahrens 38675a98e54bSBrendan Gregg - Sun Microsystems /* 38685a98e54bSBrendan Gregg - Sun Microsystems * Adjust ghost lists 3869244781f1SPrakash Surya * 3870244781f1SPrakash Surya * In addition to the above, the ARC also defines target values 3871244781f1SPrakash Surya * for the ghost lists. The sum of the mru list and mru ghost 3872244781f1SPrakash Surya * list should never exceed the target size of the cache, and 3873244781f1SPrakash Surya * the sum of the mru list, mfu list, mru ghost list, and mfu 3874244781f1SPrakash Surya * ghost list should never exceed twice the target size of the 3875244781f1SPrakash Surya * cache. The following logic enforces these limits on the ghost 3876244781f1SPrakash Surya * caches, and evicts from them as needed. 38775a98e54bSBrendan Gregg - Sun Microsystems */ 3878e914ace2STim Schumacher target = zfs_refcount_count(&arc_mru->arcs_size) + 3879e914ace2STim Schumacher zfs_refcount_count(&arc_mru_ghost->arcs_size) - arc_c; 3880fa9e4066Sahrens 3881244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); 3882244781f1SPrakash Surya total_evicted += bytes; 3883fa9e4066Sahrens 3884244781f1SPrakash Surya target -= bytes; 38850e8c6158Smaybee 3886244781f1SPrakash Surya total_evicted += 3887244781f1SPrakash Surya arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); 38885a98e54bSBrendan Gregg - Sun Microsystems 3889244781f1SPrakash Surya /* 3890244781f1SPrakash Surya * We assume the sum of the mru list and mfu list is less than 3891244781f1SPrakash Surya * or equal to arc_c (we enforced this above), which means we 3892244781f1SPrakash Surya * can use the simpler of the two equations below: 3893244781f1SPrakash Surya * 3894244781f1SPrakash Surya * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c 3895244781f1SPrakash Surya * mru ghost + mfu ghost <= arc_c 3896244781f1SPrakash Surya */ 3897e914ace2STim Schumacher target = zfs_refcount_count(&arc_mru_ghost->arcs_size) + 3898e914ace2STim Schumacher zfs_refcount_count(&arc_mfu_ghost->arcs_size) - arc_c; 3899244781f1SPrakash Surya 3900244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); 3901244781f1SPrakash Surya total_evicted += bytes; 3902244781f1SPrakash Surya 3903244781f1SPrakash Surya target -= bytes; 3904244781f1SPrakash Surya 3905244781f1SPrakash Surya total_evicted += 3906244781f1SPrakash Surya arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); 3907244781f1SPrakash Surya 3908244781f1SPrakash Surya return (total_evicted); 3909fa9e4066Sahrens } 3910fa9e4066Sahrens 3911fa9e4066Sahrens void 3912244781f1SPrakash Surya arc_flush(spa_t *spa, boolean_t retry) 3913fa9e4066Sahrens { 3914ac05c741SMark Maybee uint64_t guid = 0; 3915ac05c741SMark Maybee 3916244781f1SPrakash Surya /* 3917dcbf3bd6SGeorge Wilson * If retry is B_TRUE, a spa must not be specified since we have 3918244781f1SPrakash Surya * no good way to determine if all of a spa's buffers have been 3919244781f1SPrakash Surya * evicted from an arc state. 3920244781f1SPrakash Surya */ 3921244781f1SPrakash Surya ASSERT(!retry || spa == 0); 3922244781f1SPrakash Surya 392389c86e32SChris Williamson if (spa != NULL) 3924e9103aaeSGarrett D'Amore guid = spa_load_guid(spa); 3925ac05c741SMark Maybee 3926244781f1SPrakash Surya (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); 3927244781f1SPrakash Surya (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); 3928244781f1SPrakash Surya 3929244781f1SPrakash Surya (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); 3930244781f1SPrakash Surya (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); 3931874395d5Smaybee 3932244781f1SPrakash Surya (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); 3933244781f1SPrakash Surya (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); 3934244781f1SPrakash Surya 3935244781f1SPrakash Surya (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); 3936244781f1SPrakash Surya (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); 3937fa9e4066Sahrens } 3938fa9e4066Sahrens 3939de753e34SBrad Lewis static void 3940de753e34SBrad Lewis arc_reduce_target_size(int64_t to_free) 3941fa9e4066Sahrens { 39423a2d8a1bSPaul Dagnelie uint64_t asize = aggsum_value(&arc_size); 394344cb6abcSbmc if (arc_c > arc_c_min) { 3944fa9e4066Sahrens 394544cb6abcSbmc if (arc_c > arc_c_min + to_free) 394644cb6abcSbmc atomic_add_64(&arc_c, -to_free); 394749e3519aSmaybee else 394844cb6abcSbmc arc_c = arc_c_min; 394944cb6abcSbmc 395044cb6abcSbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 39513a2d8a1bSPaul Dagnelie if (asize < arc_c) 39523a2d8a1bSPaul Dagnelie arc_c = MAX(asize, arc_c_min); 395344cb6abcSbmc if (arc_p > arc_c) 395444cb6abcSbmc arc_p = (arc_c >> 1); 395544cb6abcSbmc ASSERT(arc_c >= arc_c_min); 395644cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 395749e3519aSmaybee } 3958fa9e4066Sahrens 3959de753e34SBrad Lewis if (asize > arc_c) { 3960de753e34SBrad Lewis /* See comment in arc_adjust_cb_check() on why lock+flag */ 3961de753e34SBrad Lewis mutex_enter(&arc_adjust_lock); 3962de753e34SBrad Lewis arc_adjust_needed = B_TRUE; 3963de753e34SBrad Lewis mutex_exit(&arc_adjust_lock); 3964de753e34SBrad Lewis zthr_wakeup(arc_adjust_zthr); 3965de753e34SBrad Lewis } 3966fa9e4066Sahrens } 3967fa9e4066Sahrens 39682ec99e3eSMatthew Ahrens typedef enum free_memory_reason_t { 39692ec99e3eSMatthew Ahrens FMR_UNKNOWN, 39702ec99e3eSMatthew Ahrens FMR_NEEDFREE, 39712ec99e3eSMatthew Ahrens FMR_LOTSFREE, 39722ec99e3eSMatthew Ahrens FMR_SWAPFS_MINFREE, 39732ec99e3eSMatthew Ahrens FMR_PAGES_PP_MAXIMUM, 39742ec99e3eSMatthew Ahrens FMR_HEAP_ARENA, 39752ec99e3eSMatthew Ahrens FMR_ZIO_ARENA, 39762ec99e3eSMatthew Ahrens } free_memory_reason_t; 39772ec99e3eSMatthew Ahrens 39782ec99e3eSMatthew Ahrens int64_t last_free_memory; 39792ec99e3eSMatthew Ahrens free_memory_reason_t last_free_reason; 39802ec99e3eSMatthew Ahrens 398194dd93aeSGeorge Wilson /* 39822ec99e3eSMatthew Ahrens * Additional reserve of pages for pp_reserve. 398394dd93aeSGeorge Wilson */ 39842ec99e3eSMatthew Ahrens int64_t arc_pages_pp_reserve = 64; 3985fa9e4066Sahrens 39862ec99e3eSMatthew Ahrens /* 39872ec99e3eSMatthew Ahrens * Additional reserve of pages for swapfs. 39882ec99e3eSMatthew Ahrens */ 39892ec99e3eSMatthew Ahrens int64_t arc_swapfs_reserve = 64; 39903cff2f43Sstans 39912ec99e3eSMatthew Ahrens /* 39922ec99e3eSMatthew Ahrens * Return the amount of memory that can be consumed before reclaim will be 39932ec99e3eSMatthew Ahrens * needed. Positive if there is sufficient free memory, negative indicates 39942ec99e3eSMatthew Ahrens * the amount of memory that needs to be freed up. 39952ec99e3eSMatthew Ahrens */ 39962ec99e3eSMatthew Ahrens static int64_t 39972ec99e3eSMatthew Ahrens arc_available_memory(void) 39982ec99e3eSMatthew Ahrens { 39992ec99e3eSMatthew Ahrens int64_t lowest = INT64_MAX; 40002ec99e3eSMatthew Ahrens int64_t n; 40012ec99e3eSMatthew Ahrens free_memory_reason_t r = FMR_UNKNOWN; 40023cff2f43Sstans 40032ec99e3eSMatthew Ahrens #ifdef _KERNEL 40042ec99e3eSMatthew Ahrens if (needfree > 0) { 40052ec99e3eSMatthew Ahrens n = PAGESIZE * (-needfree); 40062ec99e3eSMatthew Ahrens if (n < lowest) { 40072ec99e3eSMatthew Ahrens lowest = n; 40082ec99e3eSMatthew Ahrens r = FMR_NEEDFREE; 40092ec99e3eSMatthew Ahrens } 40102ec99e3eSMatthew Ahrens } 4011fa9e4066Sahrens 4012fa9e4066Sahrens /* 4013fa9e4066Sahrens * check that we're out of range of the pageout scanner. It starts to 4014fa9e4066Sahrens * schedule paging if freemem is less than lotsfree and needfree. 4015fa9e4066Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 4016fa9e4066Sahrens * number of needed free pages. We add extra pages here to make sure 4017fa9e4066Sahrens * the scanner doesn't start up while we're freeing memory. 4018fa9e4066Sahrens */ 40192ec99e3eSMatthew Ahrens n = PAGESIZE * (freemem - lotsfree - needfree - desfree); 40202ec99e3eSMatthew Ahrens if (n < lowest) { 40212ec99e3eSMatthew Ahrens lowest = n; 40222ec99e3eSMatthew Ahrens r = FMR_LOTSFREE; 40232ec99e3eSMatthew Ahrens } 4024fa9e4066Sahrens 4025fa9e4066Sahrens /* 4026fa9e4066Sahrens * check to make sure that swapfs has enough space so that anon 4027fa94a07fSbrendan * reservations can still succeed. anon_resvmem() checks that the 4028fa9e4066Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 4029fa9e4066Sahrens * swap pages. We also add a bit of extra here just to prevent 4030fa9e4066Sahrens * circumstances from getting really dire. 4031fa9e4066Sahrens */ 40322ec99e3eSMatthew Ahrens n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve - 40332ec99e3eSMatthew Ahrens desfree - arc_swapfs_reserve); 40342ec99e3eSMatthew Ahrens if (n < lowest) { 40352ec99e3eSMatthew Ahrens lowest = n; 40362ec99e3eSMatthew Ahrens r = FMR_SWAPFS_MINFREE; 40372ec99e3eSMatthew Ahrens } 40382ec99e3eSMatthew Ahrens 4039fa9e4066Sahrens 4040cf746768SBryan Cantrill /* 4041cf746768SBryan Cantrill * Check that we have enough availrmem that memory locking (e.g., via 4042cf746768SBryan Cantrill * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 4043cf746768SBryan Cantrill * stores the number of pages that cannot be locked; when availrmem 4044cf746768SBryan Cantrill * drops below pages_pp_maximum, page locking mechanisms such as 4045cf746768SBryan Cantrill * page_pp_lock() will fail.) 4046cf746768SBryan Cantrill */ 40472ec99e3eSMatthew Ahrens n = PAGESIZE * (availrmem - pages_pp_maximum - 40482ec99e3eSMatthew Ahrens arc_pages_pp_reserve); 40492ec99e3eSMatthew Ahrens if (n < lowest) { 40502ec99e3eSMatthew Ahrens lowest = n; 40512ec99e3eSMatthew Ahrens r = FMR_PAGES_PP_MAXIMUM; 40522ec99e3eSMatthew Ahrens } 4053cf746768SBryan Cantrill 40545dc8af33Smaybee #if defined(__i386) 4055fa9e4066Sahrens /* 4056fa9e4066Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 4057fa9e4066Sahrens * kernel heap space before we ever run out of available physical 4058fa9e4066Sahrens * memory. Most checks of the size of the heap_area compare against 4059fa9e4066Sahrens * tune.t_minarmem, which is the minimum available real memory that we 4060fa9e4066Sahrens * can have in the system. However, this is generally fixed at 25 pages 4061fa9e4066Sahrens * which is so low that it's useless. In this comparison, we seek to 4062fa9e4066Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 4063fa94a07fSbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 4064fa9e4066Sahrens * free) 4065fa9e4066Sahrens */ 40660dd053d7SPrakash Surya n = (int64_t)vmem_size(heap_arena, VMEM_FREE) - 40672ec99e3eSMatthew Ahrens (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2); 40682ec99e3eSMatthew Ahrens if (n < lowest) { 40692ec99e3eSMatthew Ahrens lowest = n; 40702ec99e3eSMatthew Ahrens r = FMR_HEAP_ARENA; 40712ec99e3eSMatthew Ahrens } 4072fa9e4066Sahrens #endif 4073fa9e4066Sahrens 407494dd93aeSGeorge Wilson /* 407594dd93aeSGeorge Wilson * If zio data pages are being allocated out of a separate heap segment, 407694dd93aeSGeorge Wilson * then enforce that the size of available vmem for this arena remains 40770dd053d7SPrakash Surya * above about 1/4th (1/(2^arc_zio_arena_free_shift)) free. 407894dd93aeSGeorge Wilson * 40790dd053d7SPrakash Surya * Note that reducing the arc_zio_arena_free_shift keeps more virtual 40800dd053d7SPrakash Surya * memory (in the zio_arena) free, which can avoid memory 40810dd053d7SPrakash Surya * fragmentation issues. 408294dd93aeSGeorge Wilson */ 40832ec99e3eSMatthew Ahrens if (zio_arena != NULL) { 40840dd053d7SPrakash Surya n = (int64_t)vmem_size(zio_arena, VMEM_FREE) - 40850dd053d7SPrakash Surya (vmem_size(zio_arena, VMEM_ALLOC) >> 40860dd053d7SPrakash Surya arc_zio_arena_free_shift); 40872ec99e3eSMatthew Ahrens if (n < lowest) { 40882ec99e3eSMatthew Ahrens lowest = n; 40892ec99e3eSMatthew Ahrens r = FMR_ZIO_ARENA; 40902ec99e3eSMatthew Ahrens } 40912ec99e3eSMatthew Ahrens } 4092fa9e4066Sahrens #else 40932ec99e3eSMatthew Ahrens /* Every 100 calls, free a small amount */ 4094fa9e4066Sahrens if (spa_get_random(100) == 0) 40952ec99e3eSMatthew Ahrens lowest = -1024; 4096fa9e4066Sahrens #endif 40972ec99e3eSMatthew Ahrens 40982ec99e3eSMatthew Ahrens last_free_memory = lowest; 40992ec99e3eSMatthew Ahrens last_free_reason = r; 41002ec99e3eSMatthew Ahrens 41012ec99e3eSMatthew Ahrens return (lowest); 41022ec99e3eSMatthew Ahrens } 41032ec99e3eSMatthew Ahrens 41042ec99e3eSMatthew Ahrens 41052ec99e3eSMatthew Ahrens /* 41062ec99e3eSMatthew Ahrens * Determine if the system is under memory pressure and is asking 4107dcbf3bd6SGeorge Wilson * to reclaim memory. A return value of B_TRUE indicates that the system 41082ec99e3eSMatthew Ahrens * is under memory pressure and that the arc should adjust accordingly. 41092ec99e3eSMatthew Ahrens */ 41102ec99e3eSMatthew Ahrens static boolean_t 41112ec99e3eSMatthew Ahrens arc_reclaim_needed(void) 41122ec99e3eSMatthew Ahrens { 41132ec99e3eSMatthew Ahrens return (arc_available_memory() < 0); 4114fa9e4066Sahrens } 4115fa9e4066Sahrens 4116fa9e4066Sahrens static void 4117de753e34SBrad Lewis arc_kmem_reap_soon(void) 4118fa9e4066Sahrens { 4119fa9e4066Sahrens size_t i; 4120fa9e4066Sahrens kmem_cache_t *prev_cache = NULL; 4121ad23a2dbSjohansen kmem_cache_t *prev_data_cache = NULL; 4122fa9e4066Sahrens extern kmem_cache_t *zio_buf_cache[]; 4123ad23a2dbSjohansen extern kmem_cache_t *zio_data_buf_cache[]; 412483803b51SGeorge Wilson extern kmem_cache_t *range_seg_cache; 4125770499e1SDan Kimmel extern kmem_cache_t *abd_chunk_cache; 4126fa9e4066Sahrens 4127033f9833Sek #ifdef _KERNEL 41283a2d8a1bSPaul Dagnelie if (aggsum_compare(&arc_meta_used, arc_meta_limit) >= 0) { 41290e8c6158Smaybee /* 41300e8c6158Smaybee * We are exceeding our meta-data cache limit. 41310e8c6158Smaybee * Purge some DNLC entries to release holds on meta-data. 41320e8c6158Smaybee */ 41330e8c6158Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 41340e8c6158Smaybee } 41355dc8af33Smaybee #if defined(__i386) 41365dc8af33Smaybee /* 41375dc8af33Smaybee * Reclaim unused memory from all kmem caches. 41385dc8af33Smaybee */ 41395dc8af33Smaybee kmem_reap(); 41405dc8af33Smaybee #endif 4141033f9833Sek #endif 4142033f9833Sek 4143fa9e4066Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 4144fa9e4066Sahrens if (zio_buf_cache[i] != prev_cache) { 4145fa9e4066Sahrens prev_cache = zio_buf_cache[i]; 414636a64e62STim Kordas kmem_cache_reap_soon(zio_buf_cache[i]); 4147fa9e4066Sahrens } 4148ad23a2dbSjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 4149ad23a2dbSjohansen prev_data_cache = zio_data_buf_cache[i]; 415036a64e62STim Kordas kmem_cache_reap_soon(zio_data_buf_cache[i]); 4151ad23a2dbSjohansen } 4152fa9e4066Sahrens } 415336a64e62STim Kordas kmem_cache_reap_soon(abd_chunk_cache); 415436a64e62STim Kordas kmem_cache_reap_soon(buf_cache); 415536a64e62STim Kordas kmem_cache_reap_soon(hdr_full_cache); 415636a64e62STim Kordas kmem_cache_reap_soon(hdr_l2only_cache); 415736a64e62STim Kordas kmem_cache_reap_soon(range_seg_cache); 415894dd93aeSGeorge Wilson 41592ec99e3eSMatthew Ahrens if (zio_arena != NULL) { 41602ec99e3eSMatthew Ahrens /* 41612ec99e3eSMatthew Ahrens * Ask the vmem arena to reclaim unused memory from its 41622ec99e3eSMatthew Ahrens * quantum caches. 41632ec99e3eSMatthew Ahrens */ 416494dd93aeSGeorge Wilson vmem_qcache_reap(zio_arena); 41652ec99e3eSMatthew Ahrens } 4166fa9e4066Sahrens } 4167fa9e4066Sahrens 4168de753e34SBrad Lewis /* ARGSUSED */ 4169de753e34SBrad Lewis static boolean_t 4170de753e34SBrad Lewis arc_adjust_cb_check(void *arg, zthr_t *zthr) 4171de753e34SBrad Lewis { 4172de753e34SBrad Lewis /* 4173de753e34SBrad Lewis * This is necessary in order for the mdb ::arc dcmd to 4174de753e34SBrad Lewis * show up to date information. Since the ::arc command 4175de753e34SBrad Lewis * does not call the kstat's update function, without 4176de753e34SBrad Lewis * this call, the command may show stale stats for the 4177de753e34SBrad Lewis * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even 4178de753e34SBrad Lewis * with this change, the data might be up to 1 second 4179de753e34SBrad Lewis * out of date(the arc_adjust_zthr has a maximum sleep 4180de753e34SBrad Lewis * time of 1 second); but that should suffice. The 4181de753e34SBrad Lewis * arc_state_t structures can be queried directly if more 4182de753e34SBrad Lewis * accurate information is needed. 4183de753e34SBrad Lewis */ 4184de753e34SBrad Lewis if (arc_ksp != NULL) 4185de753e34SBrad Lewis arc_ksp->ks_update(arc_ksp, KSTAT_READ); 4186de753e34SBrad Lewis 4187de753e34SBrad Lewis /* 4188de753e34SBrad Lewis * We have to rely on arc_get_data_impl() to tell us when to adjust, 4189de753e34SBrad Lewis * rather than checking if we are overflowing here, so that we are 4190de753e34SBrad Lewis * sure to not leave arc_get_data_impl() waiting on 4191de753e34SBrad Lewis * arc_adjust_waiters_cv. If we have become "not overflowing" since 4192de753e34SBrad Lewis * arc_get_data_impl() checked, we need to wake it up. We could 4193de753e34SBrad Lewis * broadcast the CV here, but arc_get_data_impl() may have not yet 4194de753e34SBrad Lewis * gone to sleep. We would need to use a mutex to ensure that this 4195de753e34SBrad Lewis * function doesn't broadcast until arc_get_data_impl() has gone to 4196de753e34SBrad Lewis * sleep (e.g. the arc_adjust_lock). However, the lock ordering of 4197de753e34SBrad Lewis * such a lock would necessarily be incorrect with respect to the 4198de753e34SBrad Lewis * zthr_lock, which is held before this function is called, and is 4199de753e34SBrad Lewis * held by arc_get_data_impl() when it calls zthr_wakeup(). 4200de753e34SBrad Lewis */ 4201de753e34SBrad Lewis return (arc_adjust_needed); 4202de753e34SBrad Lewis } 4203de753e34SBrad Lewis 4204244781f1SPrakash Surya /* 4205de753e34SBrad Lewis * Keep arc_size under arc_c by running arc_adjust which evicts data 4206de753e34SBrad Lewis * from the ARC. 4207244781f1SPrakash Surya */ 42083f7978d0SAlan Somers /* ARGSUSED */ 42096a316e1fSSerapheim Dimitropoulos static void 4210de753e34SBrad Lewis arc_adjust_cb(void *arg, zthr_t *zthr) 4211fa9e4066Sahrens { 4212de753e34SBrad Lewis uint64_t evicted = 0; 4213fa9e4066Sahrens 4214de753e34SBrad Lewis /* Evict from cache */ 4215de753e34SBrad Lewis evicted = arc_adjust(); 4216244781f1SPrakash Surya 4217de753e34SBrad Lewis /* 4218de753e34SBrad Lewis * If evicted is zero, we couldn't evict anything 4219de753e34SBrad Lewis * via arc_adjust(). This could be due to hash lock 4220de753e34SBrad Lewis * collisions, but more likely due to the majority of 4221de753e34SBrad Lewis * arc buffers being unevictable. Therefore, even if 4222de753e34SBrad Lewis * arc_size is above arc_c, another pass is unlikely to 4223de753e34SBrad Lewis * be helpful and could potentially cause us to enter an 4224de753e34SBrad Lewis * infinite loop. Additionally, zthr_iscancelled() is 4225de753e34SBrad Lewis * checked here so that if the arc is shutting down, the 4226de753e34SBrad Lewis * broadcast will wake any remaining arc adjust waiters. 4227de753e34SBrad Lewis */ 4228de753e34SBrad Lewis mutex_enter(&arc_adjust_lock); 4229de753e34SBrad Lewis arc_adjust_needed = !zthr_iscancelled(arc_adjust_zthr) && 4230de753e34SBrad Lewis evicted > 0 && aggsum_compare(&arc_size, arc_c) > 0; 4231de753e34SBrad Lewis if (!arc_adjust_needed) { 4232dcbf3bd6SGeorge Wilson /* 4233de753e34SBrad Lewis * We're either no longer overflowing, or we 4234de753e34SBrad Lewis * can't evict anything more, so we should wake 4235de753e34SBrad Lewis * up any waiters. 4236dcbf3bd6SGeorge Wilson */ 4237de753e34SBrad Lewis cv_broadcast(&arc_adjust_waiters_cv); 4238de753e34SBrad Lewis } 4239de753e34SBrad Lewis mutex_exit(&arc_adjust_lock); 4240de753e34SBrad Lewis } 4241244781f1SPrakash Surya 4242de753e34SBrad Lewis /* ARGSUSED */ 4243de753e34SBrad Lewis static boolean_t 4244de753e34SBrad Lewis arc_reap_cb_check(void *arg, zthr_t *zthr) 4245de753e34SBrad Lewis { 4246de753e34SBrad Lewis int64_t free_memory = arc_available_memory(); 4247de753e34SBrad Lewis 4248de753e34SBrad Lewis /* 4249de753e34SBrad Lewis * If a kmem reap is already active, don't schedule more. We must 4250de753e34SBrad Lewis * check for this because kmem_cache_reap_soon() won't actually 4251de753e34SBrad Lewis * block on the cache being reaped (this is to prevent callers from 4252de753e34SBrad Lewis * becoming implicitly blocked by a system-wide kmem reap -- which, 4253de753e34SBrad Lewis * on a system with many, many full magazines, can take minutes). 4254de753e34SBrad Lewis */ 4255de753e34SBrad Lewis if (!kmem_cache_reap_active() && 4256de753e34SBrad Lewis free_memory < 0) { 4257de753e34SBrad Lewis arc_no_grow = B_TRUE; 4258de753e34SBrad Lewis arc_warm = B_TRUE; 4259405a5a0fSMatthew Ahrens /* 4260de753e34SBrad Lewis * Wait at least zfs_grow_retry (default 60) seconds 4261de753e34SBrad Lewis * before considering growing. 4262405a5a0fSMatthew Ahrens */ 4263de753e34SBrad Lewis arc_growtime = gethrtime() + SEC2NSEC(arc_grow_retry); 4264de753e34SBrad Lewis return (B_TRUE); 4265de753e34SBrad Lewis } else if (free_memory < arc_c >> arc_no_grow_shift) { 4266de753e34SBrad Lewis arc_no_grow = B_TRUE; 4267de753e34SBrad Lewis } else if (gethrtime() >= arc_growtime) { 4268de753e34SBrad Lewis arc_no_grow = B_FALSE; 4269de753e34SBrad Lewis } 4270405a5a0fSMatthew Ahrens 4271de753e34SBrad Lewis return (B_FALSE); 4272de753e34SBrad Lewis } 4273fa9e4066Sahrens 4274de753e34SBrad Lewis /* 4275de753e34SBrad Lewis * Keep enough free memory in the system by reaping the ARC's kmem 4276de753e34SBrad Lewis * caches. To cause more slabs to be reapable, we may reduce the 4277de753e34SBrad Lewis * target size of the cache (arc_c), causing the arc_adjust_cb() 4278de753e34SBrad Lewis * to free more buffers. 4279de753e34SBrad Lewis */ 4280de753e34SBrad Lewis /* ARGSUSED */ 42816a316e1fSSerapheim Dimitropoulos static void 4282de753e34SBrad Lewis arc_reap_cb(void *arg, zthr_t *zthr) 4283de753e34SBrad Lewis { 4284de753e34SBrad Lewis int64_t free_memory; 4285fa9e4066Sahrens 4286de753e34SBrad Lewis /* 4287de753e34SBrad Lewis * Kick off asynchronous kmem_reap()'s of all our caches. 4288de753e34SBrad Lewis */ 4289de753e34SBrad Lewis arc_kmem_reap_soon(); 4290fa9e4066Sahrens 4291de753e34SBrad Lewis /* 4292de753e34SBrad Lewis * Wait at least arc_kmem_cache_reap_retry_ms between 4293de753e34SBrad Lewis * arc_kmem_reap_soon() calls. Without this check it is possible to 4294de753e34SBrad Lewis * end up in a situation where we spend lots of time reaping 4295de753e34SBrad Lewis * caches, while we're near arc_c_min. Waiting here also gives the 4296de753e34SBrad Lewis * subsequent free memory check a chance of finding that the 4297de753e34SBrad Lewis * asynchronous reap has already freed enough memory, and we don't 4298de753e34SBrad Lewis * need to call arc_reduce_target_size(). 4299de753e34SBrad Lewis */ 4300de753e34SBrad Lewis delay((hz * arc_kmem_cache_reap_retry_ms + 999) / 1000); 4301de753e34SBrad Lewis 4302de753e34SBrad Lewis /* 4303de753e34SBrad Lewis * Reduce the target size as needed to maintain the amount of free 4304de753e34SBrad Lewis * memory in the system at a fraction of the arc_size (1/128th by 4305de753e34SBrad Lewis * default). If oversubscribed (free_memory < 0) then reduce the 4306de753e34SBrad Lewis * target arc_size by the deficit amount plus the fractional 4307de753e34SBrad Lewis * amount. If free memory is positive but less then the fractional 4308de753e34SBrad Lewis * amount, reduce by what is needed to hit the fractional amount. 4309de753e34SBrad Lewis */ 4310de753e34SBrad Lewis free_memory = arc_available_memory(); 43112ec99e3eSMatthew Ahrens 4312de753e34SBrad Lewis int64_t to_free = 4313de753e34SBrad Lewis (arc_c >> arc_shrink_shift) - free_memory; 4314de753e34SBrad Lewis if (to_free > 0) { 43152ec99e3eSMatthew Ahrens #ifdef _KERNEL 4316de753e34SBrad Lewis to_free = MAX(to_free, ptob(needfree)); 43172ec99e3eSMatthew Ahrens #endif 4318de753e34SBrad Lewis arc_reduce_target_size(to_free); 4319244781f1SPrakash Surya } 4320244781f1SPrakash Surya } 4321244781f1SPrakash Surya 4322ea8dc4b6Seschrock /* 4323ea8dc4b6Seschrock * Adapt arc info given the number of bytes we are trying to add and 4324ea8dc4b6Seschrock * the state that we are comming from. This function is only called 4325ea8dc4b6Seschrock * when we are adding new content to the cache. 4326ea8dc4b6Seschrock */ 4327fa9e4066Sahrens static void 4328ea8dc4b6Seschrock arc_adapt(int bytes, arc_state_t *state) 4329fa9e4066Sahrens { 4330ea8dc4b6Seschrock int mult; 43315a98e54bSBrendan Gregg - Sun Microsystems uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 4332e914ace2STim Schumacher int64_t mrug_size = zfs_refcount_count(&arc_mru_ghost->arcs_size); 4333e914ace2STim Schumacher int64_t mfug_size = zfs_refcount_count(&arc_mfu_ghost->arcs_size); 4334ea8dc4b6Seschrock 4335fa94a07fSbrendan if (state == arc_l2c_only) 4336fa94a07fSbrendan return; 4337fa94a07fSbrendan 4338ea8dc4b6Seschrock ASSERT(bytes > 0); 4339fa9e4066Sahrens /* 4340ea8dc4b6Seschrock * Adapt the target size of the MRU list: 4341ea8dc4b6Seschrock * - if we just hit in the MRU ghost list, then increase 4342ea8dc4b6Seschrock * the target size of the MRU list. 4343ea8dc4b6Seschrock * - if we just hit in the MFU ghost list, then increase 4344ea8dc4b6Seschrock * the target size of the MFU list by decreasing the 4345ea8dc4b6Seschrock * target size of the MRU list. 4346fa9e4066Sahrens */ 434744cb6abcSbmc if (state == arc_mru_ghost) { 43482fd872a7SPrakash Surya mult = (mrug_size >= mfug_size) ? 1 : (mfug_size / mrug_size); 43493e4e8481STom Erickson mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 4350ea8dc4b6Seschrock 43515a98e54bSBrendan Gregg - Sun Microsystems arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 435244cb6abcSbmc } else if (state == arc_mfu_ghost) { 43535a98e54bSBrendan Gregg - Sun Microsystems uint64_t delta; 43545a98e54bSBrendan Gregg - Sun Microsystems 43552fd872a7SPrakash Surya mult = (mfug_size >= mrug_size) ? 1 : (mrug_size / mfug_size); 43563e4e8481STom Erickson mult = MIN(mult, 10); 4357ea8dc4b6Seschrock 43585a98e54bSBrendan Gregg - Sun Microsystems delta = MIN(bytes * mult, arc_p); 43595a98e54bSBrendan Gregg - Sun Microsystems arc_p = MAX(arc_p_min, arc_p - delta); 4360ea8dc4b6Seschrock } 436144cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 4362fa9e4066Sahrens 4363de753e34SBrad Lewis /* 4364de753e34SBrad Lewis * Wake reap thread if we do not have any available memory 4365de753e34SBrad Lewis */ 4366fa9e4066Sahrens if (arc_reclaim_needed()) { 4367de753e34SBrad Lewis zthr_wakeup(arc_reap_zthr); 4368fa9e4066Sahrens return; 4369fa9e4066Sahrens } 4370fa9e4066Sahrens 4371de753e34SBrad Lewis 437244cb6abcSbmc if (arc_no_grow) 4373fa9e4066Sahrens return; 4374fa9e4066Sahrens 437544cb6abcSbmc if (arc_c >= arc_c_max) 4376ea8dc4b6Seschrock return; 4377ea8dc4b6Seschrock 4378fa9e4066Sahrens /* 4379ea8dc4b6Seschrock * If we're within (2 * maxblocksize) bytes of the target 4380ea8dc4b6Seschrock * cache size, increment the target cache size 4381fa9e4066Sahrens */ 43823a2d8a1bSPaul Dagnelie if (aggsum_compare(&arc_size, arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) > 43833a2d8a1bSPaul Dagnelie 0) { 438444cb6abcSbmc atomic_add_64(&arc_c, (int64_t)bytes); 438544cb6abcSbmc if (arc_c > arc_c_max) 438644cb6abcSbmc arc_c = arc_c_max; 438744cb6abcSbmc else if (state == arc_anon) 438844cb6abcSbmc atomic_add_64(&arc_p, (int64_t)bytes); 438944cb6abcSbmc if (arc_p > arc_c) 439044cb6abcSbmc arc_p = arc_c; 4391fa9e4066Sahrens } 439244cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 4393fa9e4066Sahrens } 4394fa9e4066Sahrens 4395fa9e4066Sahrens /* 4396244781f1SPrakash Surya * Check if arc_size has grown past our upper threshold, determined by 4397244781f1SPrakash Surya * zfs_arc_overflow_shift. 4398fa9e4066Sahrens */ 4399244781f1SPrakash Surya static boolean_t 4400244781f1SPrakash Surya arc_is_overflowing(void) 4401fa9e4066Sahrens { 4402244781f1SPrakash Surya /* Always allow at least one block of overflow */ 4403244781f1SPrakash Surya uint64_t overflow = MAX(SPA_MAXBLOCKSIZE, 4404244781f1SPrakash Surya arc_c >> zfs_arc_overflow_shift); 4405fa9e4066Sahrens 44063a2d8a1bSPaul Dagnelie /* 44073a2d8a1bSPaul Dagnelie * We just compare the lower bound here for performance reasons. Our 44083a2d8a1bSPaul Dagnelie * primary goals are to make sure that the arc never grows without 44093a2d8a1bSPaul Dagnelie * bound, and that it can reach its maximum size. This check 44103a2d8a1bSPaul Dagnelie * accomplishes both goals. The maximum amount we could run over by is 44113a2d8a1bSPaul Dagnelie * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block 44123a2d8a1bSPaul Dagnelie * in the ARC. In practice, that's in the tens of MB, which is low 44133a2d8a1bSPaul Dagnelie * enough to be safe. 44143a2d8a1bSPaul Dagnelie */ 44153a2d8a1bSPaul Dagnelie return (aggsum_lower_bound(&arc_size) >= arc_c + overflow); 4416fa9e4066Sahrens } 4417fa9e4066Sahrens 4418770499e1SDan Kimmel static abd_t * 4419770499e1SDan Kimmel arc_get_data_abd(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4420770499e1SDan Kimmel { 4421770499e1SDan Kimmel arc_buf_contents_t type = arc_buf_type(hdr); 4422770499e1SDan Kimmel 4423770499e1SDan Kimmel arc_get_data_impl(hdr, size, tag); 4424770499e1SDan Kimmel if (type == ARC_BUFC_METADATA) { 4425770499e1SDan Kimmel return (abd_alloc(size, B_TRUE)); 4426770499e1SDan Kimmel } else { 4427770499e1SDan Kimmel ASSERT(type == ARC_BUFC_DATA); 4428770499e1SDan Kimmel return (abd_alloc(size, B_FALSE)); 4429770499e1SDan Kimmel } 4430770499e1SDan Kimmel } 4431770499e1SDan Kimmel 4432770499e1SDan Kimmel static void * 4433770499e1SDan Kimmel arc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4434770499e1SDan Kimmel { 4435770499e1SDan Kimmel arc_buf_contents_t type = arc_buf_type(hdr); 4436770499e1SDan Kimmel 4437770499e1SDan Kimmel arc_get_data_impl(hdr, size, tag); 4438770499e1SDan Kimmel if (type == ARC_BUFC_METADATA) { 4439770499e1SDan Kimmel return (zio_buf_alloc(size)); 4440770499e1SDan Kimmel } else { 4441770499e1SDan Kimmel ASSERT(type == ARC_BUFC_DATA); 4442770499e1SDan Kimmel return (zio_data_buf_alloc(size)); 4443770499e1SDan Kimmel } 4444770499e1SDan Kimmel } 4445770499e1SDan Kimmel 4446fa9e4066Sahrens /* 4447dcbf3bd6SGeorge Wilson * Allocate a block and return it to the caller. If we are hitting the 4448dcbf3bd6SGeorge Wilson * hard limit for the cache size, we must sleep, waiting for the eviction 4449dcbf3bd6SGeorge Wilson * thread to catch up. If we're past the target size but below the hard 4450dcbf3bd6SGeorge Wilson * limit, we'll only signal the reclaim thread and continue on. 4451fa9e4066Sahrens */ 4452770499e1SDan Kimmel static void 4453770499e1SDan Kimmel arc_get_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4454fa9e4066Sahrens { 4455770499e1SDan Kimmel arc_state_t *state = hdr->b_l1hdr.b_state; 4456770499e1SDan Kimmel arc_buf_contents_t type = arc_buf_type(hdr); 4457fa9e4066Sahrens 445844eda4d7Smaybee arc_adapt(size, state); 4459fa9e4066Sahrens 446044eda4d7Smaybee /* 4461244781f1SPrakash Surya * If arc_size is currently overflowing, and has grown past our 4462244781f1SPrakash Surya * upper limit, we must be adding data faster than the evict 4463244781f1SPrakash Surya * thread can evict. Thus, to ensure we don't compound the 4464244781f1SPrakash Surya * problem by adding more data and forcing arc_size to grow even 4465244781f1SPrakash Surya * further past it's target size, we halt and wait for the 4466244781f1SPrakash Surya * eviction thread to catch up. 4467244781f1SPrakash Surya * 4468244781f1SPrakash Surya * It's also possible that the reclaim thread is unable to evict 4469244781f1SPrakash Surya * enough buffers to get arc_size below the overflow limit (e.g. 4470244781f1SPrakash Surya * due to buffers being un-evictable, or hash lock collisions). 4471244781f1SPrakash Surya * In this case, we want to proceed regardless if we're 4472244781f1SPrakash Surya * overflowing; thus we don't use a while loop here. 447344eda4d7Smaybee */ 4474244781f1SPrakash Surya if (arc_is_overflowing()) { 4475de753e34SBrad Lewis mutex_enter(&arc_adjust_lock); 4476244781f1SPrakash Surya 4477244781f1SPrakash Surya /* 4478244781f1SPrakash Surya * Now that we've acquired the lock, we may no longer be 4479244781f1SPrakash Surya * over the overflow limit, lets check. 4480244781f1SPrakash Surya * 4481244781f1SPrakash Surya * We're ignoring the case of spurious wake ups. If that 4482244781f1SPrakash Surya * were to happen, it'd let this thread consume an ARC 4483244781f1SPrakash Surya * buffer before it should have (i.e. before we're under 4484244781f1SPrakash Surya * the overflow limit and were signalled by the reclaim 4485244781f1SPrakash Surya * thread). As long as that is a rare occurrence, it 4486244781f1SPrakash Surya * shouldn't cause any harm. 4487244781f1SPrakash Surya */ 4488244781f1SPrakash Surya if (arc_is_overflowing()) { 4489de753e34SBrad Lewis arc_adjust_needed = B_TRUE; 4490de753e34SBrad Lewis zthr_wakeup(arc_adjust_zthr); 4491de753e34SBrad Lewis (void) cv_wait(&arc_adjust_waiters_cv, 4492de753e34SBrad Lewis &arc_adjust_lock); 4493ad23a2dbSjohansen } 4494de753e34SBrad Lewis mutex_exit(&arc_adjust_lock); 449544eda4d7Smaybee } 449644eda4d7Smaybee 4497dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 4498244781f1SPrakash Surya if (type == ARC_BUFC_METADATA) { 4499244781f1SPrakash Surya arc_space_consume(size, ARC_SPACE_META); 4500fa9e4066Sahrens } else { 4501244781f1SPrakash Surya arc_space_consume(size, ARC_SPACE_DATA); 450244eda4d7Smaybee } 4503244781f1SPrakash Surya 450444eda4d7Smaybee /* 450544eda4d7Smaybee * Update the state size. Note that ghost states have a 450644eda4d7Smaybee * "ghost size" and so don't need to be updated. 450744eda4d7Smaybee */ 4508dcbf3bd6SGeorge Wilson if (!GHOST_STATE(state)) { 450944eda4d7Smaybee 4510e914ace2STim Schumacher (void) zfs_refcount_add_many(&state->arcs_size, size, tag); 4511244781f1SPrakash Surya 4512244781f1SPrakash Surya /* 4513244781f1SPrakash Surya * If this is reached via arc_read, the link is 4514244781f1SPrakash Surya * protected by the hash lock. If reached via 4515244781f1SPrakash Surya * arc_buf_alloc, the header should not be accessed by 4516244781f1SPrakash Surya * any other thread. And, if reached via arc_read_done, 4517244781f1SPrakash Surya * the hash lock will protect it if it's found in the 4518244781f1SPrakash Surya * hash table; otherwise no other thread should be 4519244781f1SPrakash Surya * trying to [add|remove]_reference it. 4520244781f1SPrakash Surya */ 4521244781f1SPrakash Surya if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 4522e914ace2STim Schumacher ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4523e914ace2STim Schumacher (void) zfs_refcount_add_many(&state->arcs_esize[type], 4524dcbf3bd6SGeorge Wilson size, tag); 4525fa9e4066Sahrens } 4526dcbf3bd6SGeorge Wilson 4527641fbdaeSmaybee /* 4528641fbdaeSmaybee * If we are growing the cache, and we are adding anonymous 452944cb6abcSbmc * data, and we have outgrown arc_p, update arc_p 4530641fbdaeSmaybee */ 45313a2d8a1bSPaul Dagnelie if (aggsum_compare(&arc_size, arc_c) < 0 && 45323a2d8a1bSPaul Dagnelie hdr->b_l1hdr.b_state == arc_anon && 4533e914ace2STim Schumacher (zfs_refcount_count(&arc_anon->arcs_size) + 4534e914ace2STim Schumacher zfs_refcount_count(&arc_mru->arcs_size) > arc_p)) 453544cb6abcSbmc arc_p = MIN(arc_c, arc_p + size); 4536fa9e4066Sahrens } 4537770499e1SDan Kimmel } 4538770499e1SDan Kimmel 4539770499e1SDan Kimmel static void 4540770499e1SDan Kimmel arc_free_data_abd(arc_buf_hdr_t *hdr, abd_t *abd, uint64_t size, void *tag) 4541770499e1SDan Kimmel { 4542770499e1SDan Kimmel arc_free_data_impl(hdr, size, tag); 4543770499e1SDan Kimmel abd_free(abd); 4544770499e1SDan Kimmel } 4545770499e1SDan Kimmel 4546770499e1SDan Kimmel static void 4547770499e1SDan Kimmel arc_free_data_buf(arc_buf_hdr_t *hdr, void *buf, uint64_t size, void *tag) 4548770499e1SDan Kimmel { 4549770499e1SDan Kimmel arc_buf_contents_t type = arc_buf_type(hdr); 4550770499e1SDan Kimmel 4551770499e1SDan Kimmel arc_free_data_impl(hdr, size, tag); 4552770499e1SDan Kimmel if (type == ARC_BUFC_METADATA) { 4553770499e1SDan Kimmel zio_buf_free(buf, size); 4554770499e1SDan Kimmel } else { 4555770499e1SDan Kimmel ASSERT(type == ARC_BUFC_DATA); 4556770499e1SDan Kimmel zio_data_buf_free(buf, size); 4557770499e1SDan Kimmel } 4558dcbf3bd6SGeorge Wilson } 4559dcbf3bd6SGeorge Wilson 4560dcbf3bd6SGeorge Wilson /* 4561dcbf3bd6SGeorge Wilson * Free the arc data buffer. 4562dcbf3bd6SGeorge Wilson */ 4563dcbf3bd6SGeorge Wilson static void 4564770499e1SDan Kimmel arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4565dcbf3bd6SGeorge Wilson { 4566dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 4567dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 4568dcbf3bd6SGeorge Wilson 4569dcbf3bd6SGeorge Wilson /* protected by hash lock, if in the hash table */ 4570dcbf3bd6SGeorge Wilson if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 4571e914ace2STim Schumacher ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4572dcbf3bd6SGeorge Wilson ASSERT(state != arc_anon && state != arc_l2c_only); 4573dcbf3bd6SGeorge Wilson 4574e914ace2STim Schumacher (void) zfs_refcount_remove_many(&state->arcs_esize[type], 4575dcbf3bd6SGeorge Wilson size, tag); 4576dcbf3bd6SGeorge Wilson } 4577e914ace2STim Schumacher (void) zfs_refcount_remove_many(&state->arcs_size, size, tag); 4578dcbf3bd6SGeorge Wilson 4579dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 4580dcbf3bd6SGeorge Wilson if (type == ARC_BUFC_METADATA) { 4581dcbf3bd6SGeorge Wilson arc_space_return(size, ARC_SPACE_META); 4582dcbf3bd6SGeorge Wilson } else { 4583dcbf3bd6SGeorge Wilson ASSERT(type == ARC_BUFC_DATA); 4584dcbf3bd6SGeorge Wilson arc_space_return(size, ARC_SPACE_DATA); 4585dcbf3bd6SGeorge Wilson } 4586fa9e4066Sahrens } 4587fa9e4066Sahrens 4588fa9e4066Sahrens /* 4589fa9e4066Sahrens * This routine is called whenever a buffer is accessed. 4590ea8dc4b6Seschrock * NOTE: the hash lock is dropped in this function. 4591fa9e4066Sahrens */ 4592fa9e4066Sahrens static void 45937adb730bSGeorge Wilson arc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 4594fa9e4066Sahrens { 4595d3d50737SRafael Vanoni clock_t now; 4596d3d50737SRafael Vanoni 4597fa9e4066Sahrens ASSERT(MUTEX_HELD(hash_lock)); 459889c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 4599fa9e4066Sahrens 460089c86e32SChris Williamson if (hdr->b_l1hdr.b_state == arc_anon) { 4601fa9e4066Sahrens /* 4602fa9e4066Sahrens * This buffer is not in the cache, and does not 4603fa9e4066Sahrens * appear in our "ghost" list. Add the new buffer 4604fa9e4066Sahrens * to the MRU state. 4605fa9e4066Sahrens */ 4606fa9e4066Sahrens 460789c86e32SChris Williamson ASSERT0(hdr->b_l1hdr.b_arc_access); 460889c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 46097adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 46107adb730bSGeorge Wilson arc_change_state(arc_mru, hdr, hash_lock); 4611fa9e4066Sahrens 461289c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mru) { 4613d3d50737SRafael Vanoni now = ddi_get_lbolt(); 4614d3d50737SRafael Vanoni 4615fa9e4066Sahrens /* 461613506d1eSmaybee * If this buffer is here because of a prefetch, then either: 461713506d1eSmaybee * - clear the flag if this is a "referencing" read 461813506d1eSmaybee * (any subsequent access will bump this into the MFU state). 461913506d1eSmaybee * or 462013506d1eSmaybee * - move the buffer to the head of the list if this is 462113506d1eSmaybee * another prefetch (to make it less likely to be evicted). 4622fa9e4066Sahrens */ 4623*a3874b8bSToomas Soome if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { 4624e914ace2STim Schumacher if (zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 4625244781f1SPrakash Surya /* link protected by hash lock */ 4626244781f1SPrakash Surya ASSERT(multilist_link_active( 462789c86e32SChris Williamson &hdr->b_l1hdr.b_arc_node)); 462813506d1eSmaybee } else { 4629*a3874b8bSToomas Soome arc_hdr_clear_flags(hdr, 4630*a3874b8bSToomas Soome ARC_FLAG_PREFETCH | 4631*a3874b8bSToomas Soome ARC_FLAG_PRESCIENT_PREFETCH); 463244cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_hits); 463313506d1eSmaybee } 463489c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = now; 4635fa9e4066Sahrens return; 4636fa9e4066Sahrens } 4637fa9e4066Sahrens 4638fa9e4066Sahrens /* 4639fa9e4066Sahrens * This buffer has been "accessed" only once so far, 4640fa9e4066Sahrens * but it is still in the cache. Move it to the MFU 4641fa9e4066Sahrens * state. 4642fa9e4066Sahrens */ 464389c86e32SChris Williamson if (now > hdr->b_l1hdr.b_arc_access + ARC_MINTIME) { 4644fa9e4066Sahrens /* 4645fa9e4066Sahrens * More than 125ms have passed since we 4646fa9e4066Sahrens * instantiated this buffer. Move it to the 4647fa9e4066Sahrens * most frequently used state. 4648fa9e4066Sahrens */ 464989c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = now; 46507adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 46517adb730bSGeorge Wilson arc_change_state(arc_mfu, hdr, hash_lock); 4652fa9e4066Sahrens } 465344cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_hits); 465489c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { 4655fa9e4066Sahrens arc_state_t *new_state; 4656fa9e4066Sahrens /* 4657fa9e4066Sahrens * This buffer has been "accessed" recently, but 4658fa9e4066Sahrens * was evicted from the cache. Move it to the 4659fa9e4066Sahrens * MFU state. 4660fa9e4066Sahrens */ 4661fa9e4066Sahrens 4662*a3874b8bSToomas Soome if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { 466344cb6abcSbmc new_state = arc_mru; 4664*a3874b8bSToomas Soome if (zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) { 4665*a3874b8bSToomas Soome arc_hdr_clear_flags(hdr, 4666*a3874b8bSToomas Soome ARC_FLAG_PREFETCH | 4667*a3874b8bSToomas Soome ARC_FLAG_PRESCIENT_PREFETCH); 4668*a3874b8bSToomas Soome } 46697adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 4670fa9e4066Sahrens } else { 467144cb6abcSbmc new_state = arc_mfu; 46727adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4673fa9e4066Sahrens } 4674fa9e4066Sahrens 467589c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 46767adb730bSGeorge Wilson arc_change_state(new_state, hdr, hash_lock); 4677fa9e4066Sahrens 467844cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 467989c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mfu) { 4680fa9e4066Sahrens /* 4681fa9e4066Sahrens * This buffer has been accessed more than once and is 4682fa9e4066Sahrens * still in the cache. Keep it in the MFU state. 4683fa9e4066Sahrens * 468413506d1eSmaybee * NOTE: an add_reference() that occurred when we did 468513506d1eSmaybee * the arc_read() will have kicked this off the list. 468613506d1eSmaybee * If it was a prefetch, we will explicitly move it to 468713506d1eSmaybee * the head of the list now. 4688fa9e4066Sahrens */ 468944cb6abcSbmc ARCSTAT_BUMP(arcstat_mfu_hits); 469089c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 469189c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { 469244cb6abcSbmc arc_state_t *new_state = arc_mfu; 4693fa9e4066Sahrens /* 4694fa9e4066Sahrens * This buffer has been accessed more than once but has 4695fa9e4066Sahrens * been evicted from the cache. Move it back to the 4696fa9e4066Sahrens * MFU state. 4697fa9e4066Sahrens */ 4698fa9e4066Sahrens 4699*a3874b8bSToomas Soome if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { 470013506d1eSmaybee /* 470113506d1eSmaybee * This is a prefetch access... 470213506d1eSmaybee * move this block back to the MRU state. 470313506d1eSmaybee */ 470444cb6abcSbmc new_state = arc_mru; 470513506d1eSmaybee } 470613506d1eSmaybee 470789c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 47087adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 47097adb730bSGeorge Wilson arc_change_state(new_state, hdr, hash_lock); 4710fa9e4066Sahrens 471144cb6abcSbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 471289c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { 4713fa94a07fSbrendan /* 4714fa94a07fSbrendan * This buffer is on the 2nd Level ARC. 4715fa94a07fSbrendan */ 4716fa94a07fSbrendan 471789c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 47187adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 47197adb730bSGeorge Wilson arc_change_state(arc_mfu, hdr, hash_lock); 4720fa9e4066Sahrens } else { 4721fa9e4066Sahrens ASSERT(!"invalid arc state"); 4722fa9e4066Sahrens } 4723fa9e4066Sahrens } 4724fa9e4066Sahrens 47257b38fab6SAlexander Motin /* 47267b38fab6SAlexander Motin * This routine is called by dbuf_hold() to update the arc_access() state 47277b38fab6SAlexander Motin * which otherwise would be skipped for entries in the dbuf cache. 47287b38fab6SAlexander Motin */ 47297b38fab6SAlexander Motin void 47307b38fab6SAlexander Motin arc_buf_access(arc_buf_t *buf) 47317b38fab6SAlexander Motin { 47327b38fab6SAlexander Motin mutex_enter(&buf->b_evict_lock); 47337b38fab6SAlexander Motin arc_buf_hdr_t *hdr = buf->b_hdr; 47347b38fab6SAlexander Motin 47357b38fab6SAlexander Motin /* 47367b38fab6SAlexander Motin * Avoid taking the hash_lock when possible as an optimization. 47377b38fab6SAlexander Motin * The header must be checked again under the hash_lock in order 47387b38fab6SAlexander Motin * to handle the case where it is concurrently being released. 47397b38fab6SAlexander Motin */ 47407b38fab6SAlexander Motin if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) { 47417b38fab6SAlexander Motin mutex_exit(&buf->b_evict_lock); 47427b38fab6SAlexander Motin return; 47437b38fab6SAlexander Motin } 47447b38fab6SAlexander Motin 47457b38fab6SAlexander Motin kmutex_t *hash_lock = HDR_LOCK(hdr); 47467b38fab6SAlexander Motin mutex_enter(hash_lock); 47477b38fab6SAlexander Motin 47487b38fab6SAlexander Motin if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) { 47497b38fab6SAlexander Motin mutex_exit(hash_lock); 47507b38fab6SAlexander Motin mutex_exit(&buf->b_evict_lock); 47517b38fab6SAlexander Motin ARCSTAT_BUMP(arcstat_access_skip); 47527b38fab6SAlexander Motin return; 47537b38fab6SAlexander Motin } 47547b38fab6SAlexander Motin 47557b38fab6SAlexander Motin mutex_exit(&buf->b_evict_lock); 47567b38fab6SAlexander Motin 47577b38fab6SAlexander Motin ASSERT(hdr->b_l1hdr.b_state == arc_mru || 47587b38fab6SAlexander Motin hdr->b_l1hdr.b_state == arc_mfu); 47597b38fab6SAlexander Motin 47607b38fab6SAlexander Motin DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 47617b38fab6SAlexander Motin arc_access(hdr, hash_lock); 47627b38fab6SAlexander Motin mutex_exit(hash_lock); 47637b38fab6SAlexander Motin 47647b38fab6SAlexander Motin ARCSTAT_BUMP(arcstat_hits); 47657b38fab6SAlexander Motin ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 47667b38fab6SAlexander Motin demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data, metadata, hits); 47677b38fab6SAlexander Motin } 47687b38fab6SAlexander Motin 4769*a3874b8bSToomas Soome /* a generic arc_read_done_func_t which you can use */ 4770fa9e4066Sahrens /* ARGSUSED */ 4771fa9e4066Sahrens void 4772*a3874b8bSToomas Soome arc_bcopy_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, 4773*a3874b8bSToomas Soome arc_buf_t *buf, void *arg) 4774fa9e4066Sahrens { 4775*a3874b8bSToomas Soome if (buf == NULL) 4776*a3874b8bSToomas Soome return; 4777*a3874b8bSToomas Soome 4778*a3874b8bSToomas Soome bcopy(buf->b_data, arg, arc_buf_size(buf)); 4779dcbf3bd6SGeorge Wilson arc_buf_destroy(buf, arg); 4780fa9e4066Sahrens } 4781fa9e4066Sahrens 4782*a3874b8bSToomas Soome /* a generic arc_read_done_func_t */ 4783fa9e4066Sahrens void 4784*a3874b8bSToomas Soome arc_getbuf_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, 4785*a3874b8bSToomas Soome arc_buf_t *buf, void *arg) 4786fa9e4066Sahrens { 4787fa9e4066Sahrens arc_buf_t **bufp = arg; 4788*a3874b8bSToomas Soome 4789fa98e487SMatthew Ahrens if (buf == NULL) { 4790fa98e487SMatthew Ahrens ASSERT(zio == NULL || zio->io_error != 0); 4791fa9e4066Sahrens *bufp = NULL; 4792fa9e4066Sahrens } else { 4793fa98e487SMatthew Ahrens ASSERT(zio == NULL || zio->io_error == 0); 4794fa9e4066Sahrens *bufp = buf; 4795fa98e487SMatthew Ahrens ASSERT(buf->b_data != NULL); 4796fa9e4066Sahrens } 4797fa9e4066Sahrens } 4798fa9e4066Sahrens 4799dcbf3bd6SGeorge Wilson static void 4800dcbf3bd6SGeorge Wilson arc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp) 4801dcbf3bd6SGeorge Wilson { 4802dcbf3bd6SGeorge Wilson if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { 4803dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0); 4804dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 4805dcbf3bd6SGeorge Wilson } else { 4806dcbf3bd6SGeorge Wilson if (HDR_COMPRESSION_ENABLED(hdr)) { 4807dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, 4808dcbf3bd6SGeorge Wilson BP_GET_COMPRESS(bp)); 4809dcbf3bd6SGeorge Wilson } 4810dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); 4811dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp)); 4812dcbf3bd6SGeorge Wilson } 4813dcbf3bd6SGeorge Wilson } 4814dcbf3bd6SGeorge Wilson 4815fa9e4066Sahrens static void 4816fa9e4066Sahrens arc_read_done(zio_t *zio) 4817fa9e4066Sahrens { 4818dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = zio->io_private; 48195d7b4d43SMatthew Ahrens kmutex_t *hash_lock = NULL; 48205602294fSDan Kimmel arc_callback_t *callback_list; 48215602294fSDan Kimmel arc_callback_t *acb; 48225602294fSDan Kimmel boolean_t freeable = B_FALSE; 4823fa9e4066Sahrens 4824bbf4a8dfSmaybee /* 4825bbf4a8dfSmaybee * The hdr was inserted into hash-table and removed from lists 4826bbf4a8dfSmaybee * prior to starting I/O. We should find this header, since 4827bbf4a8dfSmaybee * it's in the hash table, and it should be legit since it's 4828bbf4a8dfSmaybee * not possible to evict it during the I/O. The only possible 4829bbf4a8dfSmaybee * reason for it not to be found is if we were freed during the 4830bbf4a8dfSmaybee * read. 4831bbf4a8dfSmaybee */ 48325d7b4d43SMatthew Ahrens if (HDR_IN_HASH_TABLE(hdr)) { 48335d7b4d43SMatthew Ahrens ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 48345d7b4d43SMatthew Ahrens ASSERT3U(hdr->b_dva.dva_word[0], ==, 48355d7b4d43SMatthew Ahrens BP_IDENTITY(zio->io_bp)->dva_word[0]); 48365d7b4d43SMatthew Ahrens ASSERT3U(hdr->b_dva.dva_word[1], ==, 48375d7b4d43SMatthew Ahrens BP_IDENTITY(zio->io_bp)->dva_word[1]); 48385d7b4d43SMatthew Ahrens 48395d7b4d43SMatthew Ahrens arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 48405d7b4d43SMatthew Ahrens &hash_lock); 48415d7b4d43SMatthew Ahrens 4842dcbf3bd6SGeorge Wilson ASSERT((found == hdr && 48435d7b4d43SMatthew Ahrens DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 48445d7b4d43SMatthew Ahrens (found == hdr && HDR_L2_READING(hdr))); 4845dcbf3bd6SGeorge Wilson ASSERT3P(hash_lock, !=, NULL); 4846dcbf3bd6SGeorge Wilson } 4847dcbf3bd6SGeorge Wilson 4848*a3874b8bSToomas Soome if (zio->io_error == 0) { 4849dcbf3bd6SGeorge Wilson /* byteswap if necessary */ 4850dcbf3bd6SGeorge Wilson if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 4851dcbf3bd6SGeorge Wilson if (BP_GET_LEVEL(zio->io_bp) > 0) { 4852dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; 4853dcbf3bd6SGeorge Wilson } else { 4854dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = 4855dcbf3bd6SGeorge Wilson DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 4856dcbf3bd6SGeorge Wilson } 4857dcbf3bd6SGeorge Wilson } else { 4858dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 4859dcbf3bd6SGeorge Wilson } 48605d7b4d43SMatthew Ahrens } 4861fa94a07fSbrendan 4862dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED); 486389c86e32SChris Williamson if (l2arc_noprefetch && HDR_PREFETCH(hdr)) 4864dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE); 4865fa9e4066Sahrens 486689c86e32SChris Williamson callback_list = hdr->b_l1hdr.b_acb; 4867dcbf3bd6SGeorge Wilson ASSERT3P(callback_list, !=, NULL); 48686b4acc8bSahrens 4869*a3874b8bSToomas Soome if (hash_lock && zio->io_error == 0 && 4870*a3874b8bSToomas Soome hdr->b_l1hdr.b_state == arc_anon) { 4871b24ab676SJeff Bonwick /* 4872b24ab676SJeff Bonwick * Only call arc_access on anonymous buffers. This is because 4873b24ab676SJeff Bonwick * if we've issued an I/O for an evicted buffer, we've already 4874b24ab676SJeff Bonwick * called arc_access (to prevent any simultaneous readers from 4875b24ab676SJeff Bonwick * getting confused). 4876b24ab676SJeff Bonwick */ 4877b24ab676SJeff Bonwick arc_access(hdr, hash_lock); 4878b24ab676SJeff Bonwick } 4879b24ab676SJeff Bonwick 48805602294fSDan Kimmel /* 48815602294fSDan Kimmel * If a read request has a callback (i.e. acb_done is not NULL), then we 48825602294fSDan Kimmel * make a buf containing the data according to the parameters which were 48835602294fSDan Kimmel * passed in. The implementation of arc_buf_alloc_impl() ensures that we 48845602294fSDan Kimmel * aren't needlessly decompressing the data multiple times. 48855602294fSDan Kimmel */ 48865602294fSDan Kimmel int callback_cnt = 0; 48875602294fSDan Kimmel for (acb = callback_list; acb != NULL; acb = acb->acb_next) { 48885602294fSDan Kimmel if (!acb->acb_done) 48895602294fSDan Kimmel continue; 48905602294fSDan Kimmel 48915602294fSDan Kimmel callback_cnt++; 48925602294fSDan Kimmel 4893*a3874b8bSToomas Soome if (zio->io_error != 0) 4894*a3874b8bSToomas Soome continue; 4895*a3874b8bSToomas Soome 4896*a3874b8bSToomas Soome int error = arc_buf_alloc_impl(hdr, acb->acb_private, 4897*a3874b8bSToomas Soome acb->acb_compressed, B_TRUE, &acb->acb_buf); 4898*a3874b8bSToomas Soome if (error != 0) { 4899*a3874b8bSToomas Soome /* 4900*a3874b8bSToomas Soome * Decompression failed. Set io_error 4901*a3874b8bSToomas Soome * so that when we call acb_done (below), 4902*a3874b8bSToomas Soome * we will indicate that the read failed. 4903*a3874b8bSToomas Soome * Note that in the unusual case where one 4904*a3874b8bSToomas Soome * callback is compressed and another 4905*a3874b8bSToomas Soome * uncompressed, we will mark all of them 4906*a3874b8bSToomas Soome * as failed, even though the uncompressed 4907*a3874b8bSToomas Soome * one can't actually fail. In this case, 4908*a3874b8bSToomas Soome * the hdr will not be anonymous, because 4909*a3874b8bSToomas Soome * if there are multiple callbacks, it's 4910*a3874b8bSToomas Soome * because multiple threads found the same 4911*a3874b8bSToomas Soome * arc buf in the hash table. 4912*a3874b8bSToomas Soome */ 4913*a3874b8bSToomas Soome zio->io_error = error; 4914fa9e4066Sahrens } 4915fa9e4066Sahrens } 4916fa98e487SMatthew Ahrens /* 4917fa98e487SMatthew Ahrens * If there are multiple callbacks, we must have the hash lock, 4918fa98e487SMatthew Ahrens * because the only way for multiple threads to find this hdr is 4919fa98e487SMatthew Ahrens * in the hash table. This ensures that if there are multiple 4920fa98e487SMatthew Ahrens * callbacks, the hdr is not anonymous. If it were anonymous, 4921fa98e487SMatthew Ahrens * we couldn't use arc_buf_destroy() in the error case below. 4922fa98e487SMatthew Ahrens */ 4923fa98e487SMatthew Ahrens ASSERT(callback_cnt < 2 || hash_lock != NULL); 4924fa98e487SMatthew Ahrens 492589c86e32SChris Williamson hdr->b_l1hdr.b_acb = NULL; 4926dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 49275602294fSDan Kimmel if (callback_cnt == 0) { 4928dcbf3bd6SGeorge Wilson ASSERT(HDR_PREFETCH(hdr)); 4929dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 4930770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 4931b24ab676SJeff Bonwick } 4932fa9e4066Sahrens 4933e914ace2STim Schumacher ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || 493489c86e32SChris Williamson callback_list != NULL); 4935fa9e4066Sahrens 4936*a3874b8bSToomas Soome if (zio->io_error == 0) { 4937dcbf3bd6SGeorge Wilson arc_hdr_verify(hdr, zio->io_bp); 4938dcbf3bd6SGeorge Wilson } else { 4939dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); 494089c86e32SChris Williamson if (hdr->b_l1hdr.b_state != arc_anon) 494144cb6abcSbmc arc_change_state(arc_anon, hdr, hash_lock); 4942ea8dc4b6Seschrock if (HDR_IN_HASH_TABLE(hdr)) 4943ea8dc4b6Seschrock buf_hash_remove(hdr); 4944e914ace2STim Schumacher freeable = zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4945fa9e4066Sahrens } 4946fa9e4066Sahrens 4947ea8dc4b6Seschrock /* 494813506d1eSmaybee * Broadcast before we drop the hash_lock to avoid the possibility 494913506d1eSmaybee * that the hdr (and hence the cv) might be freed before we get to 495013506d1eSmaybee * the cv_broadcast(). 4951ea8dc4b6Seschrock */ 495289c86e32SChris Williamson cv_broadcast(&hdr->b_l1hdr.b_cv); 4953ea8dc4b6Seschrock 495489c86e32SChris Williamson if (hash_lock != NULL) { 495544eda4d7Smaybee mutex_exit(hash_lock); 4956fa9e4066Sahrens } else { 4957fa9e4066Sahrens /* 4958fa9e4066Sahrens * This block was freed while we waited for the read to 4959fa9e4066Sahrens * complete. It has been removed from the hash table and 4960fa9e4066Sahrens * moved to the anonymous state (so that it won't show up 4961fa9e4066Sahrens * in the cache). 4962fa9e4066Sahrens */ 496389c86e32SChris Williamson ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 4964e914ace2STim Schumacher freeable = zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4965fa9e4066Sahrens } 4966fa9e4066Sahrens 4967fa9e4066Sahrens /* execute each callback and free its structure */ 4968fa9e4066Sahrens while ((acb = callback_list) != NULL) { 4969fa98e487SMatthew Ahrens if (acb->acb_done != NULL) { 4970fa98e487SMatthew Ahrens if (zio->io_error != 0 && acb->acb_buf != NULL) { 4971fa98e487SMatthew Ahrens /* 4972fa98e487SMatthew Ahrens * If arc_buf_alloc_impl() fails during 4973fa98e487SMatthew Ahrens * decompression, the buf will still be 4974fa98e487SMatthew Ahrens * allocated, and needs to be freed here. 4975fa98e487SMatthew Ahrens */ 4976fa98e487SMatthew Ahrens arc_buf_destroy(acb->acb_buf, acb->acb_private); 4977fa98e487SMatthew Ahrens acb->acb_buf = NULL; 4978fa98e487SMatthew Ahrens } 4979*a3874b8bSToomas Soome acb->acb_done(zio, &zio->io_bookmark, zio->io_bp, 4980*a3874b8bSToomas Soome acb->acb_buf, acb->acb_private); 4981fa98e487SMatthew Ahrens } 4982fa9e4066Sahrens 4983fa9e4066Sahrens if (acb->acb_zio_dummy != NULL) { 4984fa9e4066Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 4985fa9e4066Sahrens zio_nowait(acb->acb_zio_dummy); 4986fa9e4066Sahrens } 4987fa9e4066Sahrens 4988fa9e4066Sahrens callback_list = acb->acb_next; 4989fa9e4066Sahrens kmem_free(acb, sizeof (arc_callback_t)); 4990fa9e4066Sahrens } 4991fa9e4066Sahrens 4992fa9e4066Sahrens if (freeable) 4993ea8dc4b6Seschrock arc_hdr_destroy(hdr); 4994fa9e4066Sahrens } 4995fa9e4066Sahrens 4996fa9e4066Sahrens /* 4997fc98fea5SBart Coddens * "Read" the block at the specified DVA (in bp) via the 4998fa9e4066Sahrens * cache. If the block is found in the cache, invoke the provided 4999fa9e4066Sahrens * callback immediately and return. Note that the `zio' parameter 5000fa9e4066Sahrens * in the callback will be NULL in this case, since no IO was 5001fa9e4066Sahrens * required. If the block is not in the cache pass the read request 5002fa9e4066Sahrens * on to the spa with a substitute callback function, so that the 5003fa9e4066Sahrens * requested block will be added to the cache. 5004fa9e4066Sahrens * 5005fa9e4066Sahrens * If a read request arrives for a block that has a read in-progress, 5006fa9e4066Sahrens * either wait for the in-progress read to complete (and return the 5007fa9e4066Sahrens * results); or, if this is a read with a "done" func, add a record 5008fa9e4066Sahrens * to the read to invoke the "done" func when the read completes, 5009fa9e4066Sahrens * and return; or just return. 5010fa9e4066Sahrens * 5011fa9e4066Sahrens * arc_read_done() will invoke all the requested "done" functions 5012fa9e4066Sahrens * for readers of this block. 5013fa9e4066Sahrens */ 5014fa9e4066Sahrens int 5015*a3874b8bSToomas Soome arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_read_done_func_t *done, 50167adb730bSGeorge Wilson void *private, zio_priority_t priority, int zio_flags, 50177adb730bSGeorge Wilson arc_flags_t *arc_flags, const zbookmark_phys_t *zb) 5018fa9e4066Sahrens { 50195d7b4d43SMatthew Ahrens arc_buf_hdr_t *hdr = NULL; 50205d7b4d43SMatthew Ahrens kmutex_t *hash_lock = NULL; 5021fa94a07fSbrendan zio_t *rzio; 5022e9103aaeSGarrett D'Amore uint64_t guid = spa_load_guid(spa); 50235602294fSDan Kimmel boolean_t compressed_read = (zio_flags & ZIO_FLAG_RAW) != 0; 5024*a3874b8bSToomas Soome int rc = 0; 5025fa9e4066Sahrens 50265d7b4d43SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp) || 50275d7b4d43SMatthew Ahrens BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 50285d7b4d43SMatthew Ahrens 5029fa9e4066Sahrens top: 50305d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) { 50315d7b4d43SMatthew Ahrens /* 50325d7b4d43SMatthew Ahrens * Embedded BP's have no DVA and require no I/O to "read". 50335d7b4d43SMatthew Ahrens * Create an anonymous arc buf to back it. 50345d7b4d43SMatthew Ahrens */ 50355d7b4d43SMatthew Ahrens hdr = buf_hash_find(guid, bp, &hash_lock); 50365d7b4d43SMatthew Ahrens } 50375d7b4d43SMatthew Ahrens 5038770499e1SDan Kimmel if (hdr != NULL && HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_pabd != NULL) { 5039dcbf3bd6SGeorge Wilson arc_buf_t *buf = NULL; 50407adb730bSGeorge Wilson *arc_flags |= ARC_FLAG_CACHED; 504113506d1eSmaybee 5042fa9e4066Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 5043*a3874b8bSToomas Soome zio_t *head_zio = hdr->b_l1hdr.b_acb->acb_zio_head; 504413506d1eSmaybee 5045*a3874b8bSToomas Soome ASSERT3P(head_zio, !=, NULL); 5046cf6106c8SMatthew Ahrens if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) && 5047cf6106c8SMatthew Ahrens priority == ZIO_PRIORITY_SYNC_READ) { 5048cf6106c8SMatthew Ahrens /* 5049*a3874b8bSToomas Soome * This is a sync read that needs to wait for 5050*a3874b8bSToomas Soome * an in-flight async read. Request that the 5051*a3874b8bSToomas Soome * zio have its priority upgraded. 5052cf6106c8SMatthew Ahrens */ 5053*a3874b8bSToomas Soome zio_change_priority(head_zio, priority); 5054*a3874b8bSToomas Soome DTRACE_PROBE1(arc__async__upgrade__sync, 5055cf6106c8SMatthew Ahrens arc_buf_hdr_t *, hdr); 5056*a3874b8bSToomas Soome ARCSTAT_BUMP(arcstat_async_upgrade_sync); 5057cf6106c8SMatthew Ahrens } 5058cf6106c8SMatthew Ahrens if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { 5059dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, 5060dcbf3bd6SGeorge Wilson ARC_FLAG_PREDICTIVE_PREFETCH); 5061cf6106c8SMatthew Ahrens } 5062cf6106c8SMatthew Ahrens 50637adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_WAIT) { 506489c86e32SChris Williamson cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); 506513506d1eSmaybee mutex_exit(hash_lock); 506613506d1eSmaybee goto top; 506713506d1eSmaybee } 50687adb730bSGeorge Wilson ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 506913506d1eSmaybee 507013506d1eSmaybee if (done) { 5071cf6106c8SMatthew Ahrens arc_callback_t *acb = NULL; 5072fa9e4066Sahrens 5073fa9e4066Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 5074fa9e4066Sahrens KM_SLEEP); 5075fa9e4066Sahrens acb->acb_done = done; 5076fa9e4066Sahrens acb->acb_private = private; 50775602294fSDan Kimmel acb->acb_compressed = compressed_read; 5078fa9e4066Sahrens if (pio != NULL) 5079fa9e4066Sahrens acb->acb_zio_dummy = zio_null(pio, 5080a3f829aeSBill Moore spa, NULL, NULL, NULL, zio_flags); 5081fa9e4066Sahrens 5082dcbf3bd6SGeorge Wilson ASSERT3P(acb->acb_done, !=, NULL); 5083*a3874b8bSToomas Soome acb->acb_zio_head = head_zio; 508489c86e32SChris Williamson acb->acb_next = hdr->b_l1hdr.b_acb; 508589c86e32SChris Williamson hdr->b_l1hdr.b_acb = acb; 5086fa9e4066Sahrens mutex_exit(hash_lock); 5087fa9e4066Sahrens return (0); 5088fa9e4066Sahrens } 5089fa9e4066Sahrens mutex_exit(hash_lock); 5090fa9e4066Sahrens return (0); 5091fa9e4066Sahrens } 5092fa9e4066Sahrens 509389c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_state == arc_mru || 509489c86e32SChris Williamson hdr->b_l1hdr.b_state == arc_mfu); 5095fa9e4066Sahrens 5096ea8dc4b6Seschrock if (done) { 5097cf6106c8SMatthew Ahrens if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { 5098cf6106c8SMatthew Ahrens /* 5099cf6106c8SMatthew Ahrens * This is a demand read which does not have to 5100cf6106c8SMatthew Ahrens * wait for i/o because we did a predictive 5101cf6106c8SMatthew Ahrens * prefetch i/o for it, which has completed. 5102cf6106c8SMatthew Ahrens */ 5103cf6106c8SMatthew Ahrens DTRACE_PROBE1( 5104cf6106c8SMatthew Ahrens arc__demand__hit__predictive__prefetch, 5105cf6106c8SMatthew Ahrens arc_buf_hdr_t *, hdr); 5106cf6106c8SMatthew Ahrens ARCSTAT_BUMP( 5107cf6106c8SMatthew Ahrens arcstat_demand_hit_predictive_prefetch); 5108dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, 5109dcbf3bd6SGeorge Wilson ARC_FLAG_PREDICTIVE_PREFETCH); 5110cf6106c8SMatthew Ahrens } 5111*a3874b8bSToomas Soome 5112*a3874b8bSToomas Soome if (hdr->b_flags & ARC_FLAG_PRESCIENT_PREFETCH) { 5113*a3874b8bSToomas Soome ARCSTAT_BUMP( 5114*a3874b8bSToomas Soome arcstat_demand_hit_prescient_prefetch); 5115*a3874b8bSToomas Soome arc_hdr_clear_flags(hdr, 5116*a3874b8bSToomas Soome ARC_FLAG_PRESCIENT_PREFETCH); 5117*a3874b8bSToomas Soome } 5118*a3874b8bSToomas Soome 5119dcbf3bd6SGeorge Wilson ASSERT(!BP_IS_EMBEDDED(bp) || !BP_IS_HOLE(bp)); 5120dcbf3bd6SGeorge Wilson 51215602294fSDan Kimmel /* Get a buf with the desired data in it. */ 5122*a3874b8bSToomas Soome rc = arc_buf_alloc_impl(hdr, private, 5123*a3874b8bSToomas Soome compressed_read, B_TRUE, &buf); 5124*a3874b8bSToomas Soome if (rc != 0) { 5125*a3874b8bSToomas Soome arc_buf_destroy(buf, private); 5126*a3874b8bSToomas Soome buf = NULL; 5127*a3874b8bSToomas Soome } 5128*a3874b8bSToomas Soome ASSERT((zio_flags & ZIO_FLAG_SPECULATIVE) || 5129*a3874b8bSToomas Soome rc == 0 || rc != ENOENT); 51307adb730bSGeorge Wilson } else if (*arc_flags & ARC_FLAG_PREFETCH && 5131e914ace2STim Schumacher zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 5132dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); 5133fa9e4066Sahrens } 5134fa9e4066Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 513544eda4d7Smaybee arc_access(hdr, hash_lock); 5136*a3874b8bSToomas Soome if (*arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) 5137*a3874b8bSToomas Soome arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH); 51387adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_L2CACHE) 5139dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 514044eda4d7Smaybee mutex_exit(hash_lock); 514144cb6abcSbmc ARCSTAT_BUMP(arcstat_hits); 514289c86e32SChris Williamson ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 514389c86e32SChris Williamson demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 514444cb6abcSbmc data, metadata, hits); 514544cb6abcSbmc 5146fa9e4066Sahrens if (done) 5147*a3874b8bSToomas Soome done(NULL, zb, bp, buf, private); 5148fa9e4066Sahrens } else { 5149dcbf3bd6SGeorge Wilson uint64_t lsize = BP_GET_LSIZE(bp); 5150dcbf3bd6SGeorge Wilson uint64_t psize = BP_GET_PSIZE(bp); 51515d7b4d43SMatthew Ahrens arc_callback_t *acb; 51523a737e0dSbrendan vdev_t *vd = NULL; 5153d5285caeSGeorge Wilson uint64_t addr = 0; 51545a98e54bSBrendan Gregg - Sun Microsystems boolean_t devw = B_FALSE; 5155dcbf3bd6SGeorge Wilson uint64_t size; 5156fa9e4066Sahrens 5157fa9e4066Sahrens if (hdr == NULL) { 5158fa9e4066Sahrens /* this block is not in the cache */ 51595d7b4d43SMatthew Ahrens arc_buf_hdr_t *exists = NULL; 5160ad23a2dbSjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 5161dcbf3bd6SGeorge Wilson hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, 5162dcbf3bd6SGeorge Wilson BP_GET_COMPRESS(bp), type); 5163dcbf3bd6SGeorge Wilson 51645d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) { 51655d7b4d43SMatthew Ahrens hdr->b_dva = *BP_IDENTITY(bp); 51665d7b4d43SMatthew Ahrens hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 51675d7b4d43SMatthew Ahrens exists = buf_hash_insert(hdr, &hash_lock); 51685d7b4d43SMatthew Ahrens } 51695d7b4d43SMatthew Ahrens if (exists != NULL) { 5170fa9e4066Sahrens /* somebody beat us to the hash insert */ 5171fa9e4066Sahrens mutex_exit(hash_lock); 51723f9d6ad7SLin Ling buf_discard_identity(hdr); 5173dcbf3bd6SGeorge Wilson arc_hdr_destroy(hdr); 5174fa9e4066Sahrens goto top; /* restart the IO request */ 5175fa9e4066Sahrens } 5176fa9e4066Sahrens } else { 517789c86e32SChris Williamson /* 517889c86e32SChris Williamson * This block is in the ghost cache. If it was L2-only 517989c86e32SChris Williamson * (and thus didn't have an L1 hdr), we realloc the 518089c86e32SChris Williamson * header to add an L1 hdr. 518189c86e32SChris Williamson */ 518289c86e32SChris Williamson if (!HDR_HAS_L1HDR(hdr)) { 518389c86e32SChris Williamson hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, 518489c86e32SChris Williamson hdr_full_cache); 518589c86e32SChris Williamson } 5186770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 518789c86e32SChris Williamson ASSERT(GHOST_STATE(hdr->b_l1hdr.b_state)); 5188ea8dc4b6Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 5189e914ace2STim Schumacher ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 5190244781f1SPrakash Surya ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 51915602294fSDan Kimmel ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 519213506d1eSmaybee 5193cf6106c8SMatthew Ahrens /* 5194dcbf3bd6SGeorge Wilson * This is a delicate dance that we play here. 5195dcbf3bd6SGeorge Wilson * This hdr is in the ghost list so we access it 5196dcbf3bd6SGeorge Wilson * to move it out of the ghost list before we 5197dcbf3bd6SGeorge Wilson * initiate the read. If it's a prefetch then 5198dcbf3bd6SGeorge Wilson * it won't have a callback so we'll remove the 5199dcbf3bd6SGeorge Wilson * reference that arc_buf_alloc_impl() created. We 5200dcbf3bd6SGeorge Wilson * do this after we've called arc_access() to 5201dcbf3bd6SGeorge Wilson * avoid hitting an assert in remove_reference(). 5202cf6106c8SMatthew Ahrens */ 52037e453561SWilliam Gorrell arc_access(hdr, hash_lock); 5204770499e1SDan Kimmel arc_hdr_alloc_pabd(hdr); 5205dcbf3bd6SGeorge Wilson } 5206770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 5207dcbf3bd6SGeorge Wilson size = arc_hdr_size(hdr); 5208dcbf3bd6SGeorge Wilson 5209dcbf3bd6SGeorge Wilson /* 5210dcbf3bd6SGeorge Wilson * If compression is enabled on the hdr, then will do 5211dcbf3bd6SGeorge Wilson * RAW I/O and will store the compressed data in the hdr's 5212dcbf3bd6SGeorge Wilson * data block. Otherwise, the hdr's data block will contain 5213dcbf3bd6SGeorge Wilson * the uncompressed data. 5214dcbf3bd6SGeorge Wilson */ 5215dcbf3bd6SGeorge Wilson if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) { 5216dcbf3bd6SGeorge Wilson zio_flags |= ZIO_FLAG_RAW; 5217fa9e4066Sahrens } 5218fa9e4066Sahrens 5219dcbf3bd6SGeorge Wilson if (*arc_flags & ARC_FLAG_PREFETCH) 5220dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); 5221*a3874b8bSToomas Soome if (*arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) 5222*a3874b8bSToomas Soome arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH); 5223*a3874b8bSToomas Soome 5224dcbf3bd6SGeorge Wilson if (*arc_flags & ARC_FLAG_L2CACHE) 5225dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 5226dcbf3bd6SGeorge Wilson if (BP_GET_LEVEL(bp) > 0) 5227dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT); 5228cf6106c8SMatthew Ahrens if (*arc_flags & ARC_FLAG_PREDICTIVE_PREFETCH) 5229dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PREDICTIVE_PREFETCH); 523089c86e32SChris Williamson ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); 52315614b00aSWilliam Gorrell 5232fa9e4066Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 5233fa9e4066Sahrens acb->acb_done = done; 5234fa9e4066Sahrens acb->acb_private = private; 52355602294fSDan Kimmel acb->acb_compressed = compressed_read; 5236fa9e4066Sahrens 5237dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 523889c86e32SChris Williamson hdr->b_l1hdr.b_acb = acb; 5239dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5240fa9e4066Sahrens 524189c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr) && 524289c86e32SChris Williamson (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { 524389c86e32SChris Williamson devw = hdr->b_l2hdr.b_dev->l2ad_writing; 524489c86e32SChris Williamson addr = hdr->b_l2hdr.b_daddr; 5245e14bb325SJeff Bonwick /* 52465cabbc6bSPrashanth Sreenivasa * Lock out L2ARC device removal. 5247e14bb325SJeff Bonwick */ 5248e14bb325SJeff Bonwick if (vdev_is_dead(vd) || 5249e14bb325SJeff Bonwick !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 5250e14bb325SJeff Bonwick vd = NULL; 52513a737e0dSbrendan } 52523a737e0dSbrendan 5253*a3874b8bSToomas Soome /* 5254*a3874b8bSToomas Soome * We count both async reads and scrub IOs as asynchronous so 5255*a3874b8bSToomas Soome * that both can be upgraded in the event of a cache hit while 5256*a3874b8bSToomas Soome * the read IO is still in-flight. 5257*a3874b8bSToomas Soome */ 5258*a3874b8bSToomas Soome if (priority == ZIO_PRIORITY_ASYNC_READ || 5259*a3874b8bSToomas Soome priority == ZIO_PRIORITY_SCRUB) 5260dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); 5261dcbf3bd6SGeorge Wilson else 5262dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); 5263dcbf3bd6SGeorge Wilson 52643e30c24aSWill Andrews /* 52653e30c24aSWill Andrews * At this point, we have a level 1 cache miss. Try again in 52663e30c24aSWill Andrews * L2ARC if possible. 52673e30c24aSWill Andrews */ 5268dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize); 5269dcbf3bd6SGeorge Wilson 52705c28183bSBrendan Gregg - Sun Microsystems DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 5271dcbf3bd6SGeorge Wilson uint64_t, lsize, zbookmark_phys_t *, zb); 527244cb6abcSbmc ARCSTAT_BUMP(arcstat_misses); 527389c86e32SChris Williamson ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 527489c86e32SChris Williamson demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 527544cb6abcSbmc data, metadata, misses); 5276ea8dc4b6Seschrock 52775a98e54bSBrendan Gregg - Sun Microsystems if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 5278fa94a07fSbrendan /* 5279fa94a07fSbrendan * Read from the L2ARC if the following are true: 52803a737e0dSbrendan * 1. The L2ARC vdev was previously cached. 52813a737e0dSbrendan * 2. This buffer still has L2ARC metadata. 52823a737e0dSbrendan * 3. This buffer isn't currently writing to the L2ARC. 52833a737e0dSbrendan * 4. The L2ARC entry wasn't evicted, which may 52843a737e0dSbrendan * also have invalidated the vdev. 52855a98e54bSBrendan Gregg - Sun Microsystems * 5. This isn't prefetch and l2arc_noprefetch is set. 5286fa94a07fSbrendan */ 528789c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr) && 52885a98e54bSBrendan Gregg - Sun Microsystems !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 52895a98e54bSBrendan Gregg - Sun Microsystems !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 5290fa94a07fSbrendan l2arc_read_callback_t *cb; 5291403a8da7SAndriy Gapon abd_t *abd; 5292403a8da7SAndriy Gapon uint64_t asize; 5293fa94a07fSbrendan 5294c5904d13Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 5295c5904d13Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 5296c5904d13Seschrock 5297fa94a07fSbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 5298fa94a07fSbrendan KM_SLEEP); 5299dcbf3bd6SGeorge Wilson cb->l2rcb_hdr = hdr; 5300fa94a07fSbrendan cb->l2rcb_bp = *bp; 5301fa94a07fSbrendan cb->l2rcb_zb = *zb; 53023baa08fcSek cb->l2rcb_flags = zio_flags; 5303fa94a07fSbrendan 5304403a8da7SAndriy Gapon asize = vdev_psize_to_asize(vd, size); 5305403a8da7SAndriy Gapon if (asize != size) { 5306403a8da7SAndriy Gapon abd = abd_alloc_for_io(asize, 5307403a8da7SAndriy Gapon HDR_ISTYPE_METADATA(hdr)); 5308403a8da7SAndriy Gapon cb->l2rcb_abd = abd; 5309403a8da7SAndriy Gapon } else { 5310403a8da7SAndriy Gapon abd = hdr->b_l1hdr.b_pabd; 5311403a8da7SAndriy Gapon } 5312403a8da7SAndriy Gapon 5313d5285caeSGeorge Wilson ASSERT(addr >= VDEV_LABEL_START_SIZE && 5314403a8da7SAndriy Gapon addr + asize <= vd->vdev_psize - 5315d5285caeSGeorge Wilson VDEV_LABEL_END_SIZE); 5316d5285caeSGeorge Wilson 5317fa94a07fSbrendan /* 5318e14bb325SJeff Bonwick * l2arc read. The SCL_L2ARC lock will be 5319e14bb325SJeff Bonwick * released by l2arc_read_done(). 5320aad02571SSaso Kiselkov * Issue a null zio if the underlying buffer 5321aad02571SSaso Kiselkov * was squashed to zero size by compression. 5322fa94a07fSbrendan */ 5323dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), !=, 5324dcbf3bd6SGeorge Wilson ZIO_COMPRESS_EMPTY); 5325dcbf3bd6SGeorge Wilson rzio = zio_read_phys(pio, vd, addr, 5326403a8da7SAndriy Gapon asize, abd, 5327dcbf3bd6SGeorge Wilson ZIO_CHECKSUM_OFF, 5328dcbf3bd6SGeorge Wilson l2arc_read_done, cb, priority, 5329dcbf3bd6SGeorge Wilson zio_flags | ZIO_FLAG_DONT_CACHE | 5330dcbf3bd6SGeorge Wilson ZIO_FLAG_CANFAIL | 5331dcbf3bd6SGeorge Wilson ZIO_FLAG_DONT_PROPAGATE | 5332dcbf3bd6SGeorge Wilson ZIO_FLAG_DONT_RETRY, B_FALSE); 5333*a3874b8bSToomas Soome acb->acb_zio_head = rzio; 5334*a3874b8bSToomas Soome 5335*a3874b8bSToomas Soome if (hash_lock != NULL) 5336*a3874b8bSToomas Soome mutex_exit(hash_lock); 5337*a3874b8bSToomas Soome 5338fa94a07fSbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 5339fa94a07fSbrendan zio_t *, rzio); 5340dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_l2_read_bytes, size); 5341fa94a07fSbrendan 53427adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_NOWAIT) { 53433a737e0dSbrendan zio_nowait(rzio); 53443a737e0dSbrendan return (0); 53453a737e0dSbrendan } 5346fa94a07fSbrendan 53477adb730bSGeorge Wilson ASSERT(*arc_flags & ARC_FLAG_WAIT); 53483a737e0dSbrendan if (zio_wait(rzio) == 0) 53493a737e0dSbrendan return (0); 53503a737e0dSbrendan 53513a737e0dSbrendan /* l2arc read error; goto zio_read() */ 5352*a3874b8bSToomas Soome if (hash_lock != NULL) 5353*a3874b8bSToomas Soome mutex_enter(hash_lock); 5354fa94a07fSbrendan } else { 5355fa94a07fSbrendan DTRACE_PROBE1(l2arc__miss, 5356fa94a07fSbrendan arc_buf_hdr_t *, hdr); 5357fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_misses); 5358fa94a07fSbrendan if (HDR_L2_WRITING(hdr)) 5359fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 5360e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, vd); 5361fa94a07fSbrendan } 53625a98e54bSBrendan Gregg - Sun Microsystems } else { 536376a25fafSBill Moore if (vd != NULL) 536476a25fafSBill Moore spa_config_exit(spa, SCL_L2ARC, vd); 53655a98e54bSBrendan Gregg - Sun Microsystems if (l2arc_ndev != 0) { 53665a98e54bSBrendan Gregg - Sun Microsystems DTRACE_PROBE1(l2arc__miss, 53675a98e54bSBrendan Gregg - Sun Microsystems arc_buf_hdr_t *, hdr); 53685a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_BUMP(arcstat_l2_misses); 53695a98e54bSBrendan Gregg - Sun Microsystems } 5370fa94a07fSbrendan } 5371c5904d13Seschrock 5372770499e1SDan Kimmel rzio = zio_read(pio, spa, bp, hdr->b_l1hdr.b_pabd, size, 5373dcbf3bd6SGeorge Wilson arc_read_done, hdr, priority, zio_flags, zb); 5374*a3874b8bSToomas Soome acb->acb_zio_head = rzio; 5375*a3874b8bSToomas Soome 5376*a3874b8bSToomas Soome if (hash_lock != NULL) 5377*a3874b8bSToomas Soome mutex_exit(hash_lock); 5378fa9e4066Sahrens 53797adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_WAIT) 5380fa9e4066Sahrens return (zio_wait(rzio)); 5381fa9e4066Sahrens 53827adb730bSGeorge Wilson ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 5383fa9e4066Sahrens zio_nowait(rzio); 5384fa9e4066Sahrens } 5385fa9e4066Sahrens return (0); 5386fa9e4066Sahrens } 5387fa9e4066Sahrens 53886e6d5868SMatthew Ahrens /* 53896e6d5868SMatthew Ahrens * Notify the arc that a block was freed, and thus will never be used again. 53906e6d5868SMatthew Ahrens */ 53916e6d5868SMatthew Ahrens void 53926e6d5868SMatthew Ahrens arc_freed(spa_t *spa, const blkptr_t *bp) 53936e6d5868SMatthew Ahrens { 53946e6d5868SMatthew Ahrens arc_buf_hdr_t *hdr; 53956e6d5868SMatthew Ahrens kmutex_t *hash_lock; 53966e6d5868SMatthew Ahrens uint64_t guid = spa_load_guid(spa); 53976e6d5868SMatthew Ahrens 53985d7b4d43SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp)); 53995d7b4d43SMatthew Ahrens 54005d7b4d43SMatthew Ahrens hdr = buf_hash_find(guid, bp, &hash_lock); 54016e6d5868SMatthew Ahrens if (hdr == NULL) 54026e6d5868SMatthew Ahrens return; 54036e6d5868SMatthew Ahrens 5404dcbf3bd6SGeorge Wilson /* 5405dcbf3bd6SGeorge Wilson * We might be trying to free a block that is still doing I/O 5406dcbf3bd6SGeorge Wilson * (i.e. prefetch) or has a reference (i.e. a dedup-ed, 5407dcbf3bd6SGeorge Wilson * dmu_sync-ed block). If this block is being prefetched, then it 5408dcbf3bd6SGeorge Wilson * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr 5409dcbf3bd6SGeorge Wilson * until the I/O completes. A block may also have a reference if it is 5410dcbf3bd6SGeorge Wilson * part of a dedup-ed, dmu_synced write. The dmu_sync() function would 5411dcbf3bd6SGeorge Wilson * have written the new block to its final resting place on disk but 5412dcbf3bd6SGeorge Wilson * without the dedup flag set. This would have left the hdr in the MRU 5413dcbf3bd6SGeorge Wilson * state and discoverable. When the txg finally syncs it detects that 5414dcbf3bd6SGeorge Wilson * the block was overridden in open context and issues an override I/O. 5415dcbf3bd6SGeorge Wilson * Since this is a dedup block, the override I/O will determine if the 5416dcbf3bd6SGeorge Wilson * block is already in the DDT. If so, then it will replace the io_bp 5417dcbf3bd6SGeorge Wilson * with the bp from the DDT and allow the I/O to finish. When the I/O 5418dcbf3bd6SGeorge Wilson * reaches the done callback, dbuf_write_override_done, it will 5419dcbf3bd6SGeorge Wilson * check to see if the io_bp and io_bp_override are identical. 5420dcbf3bd6SGeorge Wilson * If they are not, then it indicates that the bp was replaced with 5421dcbf3bd6SGeorge Wilson * the bp in the DDT and the override bp is freed. This allows 5422dcbf3bd6SGeorge Wilson * us to arrive here with a reference on a block that is being 5423dcbf3bd6SGeorge Wilson * freed. So if we have an I/O in progress, or a reference to 5424dcbf3bd6SGeorge Wilson * this hdr, then we don't destroy the hdr. 5425dcbf3bd6SGeorge Wilson */ 5426dcbf3bd6SGeorge Wilson if (!HDR_HAS_L1HDR(hdr) || (!HDR_IO_IN_PROGRESS(hdr) && 5427e914ace2STim Schumacher zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt))) { 5428dcbf3bd6SGeorge Wilson arc_change_state(arc_anon, hdr, hash_lock); 5429dcbf3bd6SGeorge Wilson arc_hdr_destroy(hdr); 54306e6d5868SMatthew Ahrens mutex_exit(hash_lock); 5431bbfa8ea8SMatthew Ahrens } else { 5432dcbf3bd6SGeorge Wilson mutex_exit(hash_lock); 5433ea8dc4b6Seschrock } 5434dd6ef538Smaybee 5435ea8dc4b6Seschrock } 5436ea8dc4b6Seschrock 5437fa9e4066Sahrens /* 54383e30c24aSWill Andrews * Release this buffer from the cache, making it an anonymous buffer. This 54393e30c24aSWill Andrews * must be done after a read and prior to modifying the buffer contents. 5440fa9e4066Sahrens * If the buffer has more than one reference, we must make 5441088f3894Sahrens * a new hdr for the buffer. 5442fa9e4066Sahrens */ 5443fa9e4066Sahrens void 5444fa9e4066Sahrens arc_release(arc_buf_t *buf, void *tag) 5445fa9e4066Sahrens { 544689c86e32SChris Williamson arc_buf_hdr_t *hdr = buf->b_hdr; 5447fa9e4066Sahrens 54483f9d6ad7SLin Ling /* 54493f9d6ad7SLin Ling * It would be nice to assert that if it's DMU metadata (level > 54503f9d6ad7SLin Ling * 0 || it's the dnode file), then it must be syncing context. 54513f9d6ad7SLin Ling * But we don't know that information at this level. 54523f9d6ad7SLin Ling */ 54533f9d6ad7SLin Ling 54543f9d6ad7SLin Ling mutex_enter(&buf->b_evict_lock); 54556f83844dSMark Maybee 5456244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 5457244781f1SPrakash Surya 545889c86e32SChris Williamson /* 545989c86e32SChris Williamson * We don't grab the hash lock prior to this check, because if 546089c86e32SChris Williamson * the buffer's header is in the arc_anon state, it won't be 546189c86e32SChris Williamson * linked into the hash table. 546289c86e32SChris Williamson */ 546389c86e32SChris Williamson if (hdr->b_l1hdr.b_state == arc_anon) { 546489c86e32SChris Williamson mutex_exit(&buf->b_evict_lock); 546589c86e32SChris Williamson ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 546689c86e32SChris Williamson ASSERT(!HDR_IN_HASH_TABLE(hdr)); 546789c86e32SChris Williamson ASSERT(!HDR_HAS_L2HDR(hdr)); 5468dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 5469fa9e4066Sahrens 5470dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 5471e914ace2STim Schumacher ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); 547289c86e32SChris Williamson ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 547389c86e32SChris Williamson 547489c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = 0; 5475dcbf3bd6SGeorge Wilson 5476dcbf3bd6SGeorge Wilson /* 5477dcbf3bd6SGeorge Wilson * If the buf is being overridden then it may already 5478dcbf3bd6SGeorge Wilson * have a hdr that is not empty. 5479dcbf3bd6SGeorge Wilson */ 5480dcbf3bd6SGeorge Wilson buf_discard_identity(hdr); 548189c86e32SChris Williamson arc_buf_thaw(buf); 548289c86e32SChris Williamson 548389c86e32SChris Williamson return; 5484fa9e4066Sahrens } 5485fa9e4066Sahrens 548689c86e32SChris Williamson kmutex_t *hash_lock = HDR_LOCK(hdr); 548789c86e32SChris Williamson mutex_enter(hash_lock); 548889c86e32SChris Williamson 548989c86e32SChris Williamson /* 549089c86e32SChris Williamson * This assignment is only valid as long as the hash_lock is 549189c86e32SChris Williamson * held, we must be careful not to reference state or the 549289c86e32SChris Williamson * b_state field after dropping the lock. 549389c86e32SChris Williamson */ 549489c86e32SChris Williamson arc_state_t *state = hdr->b_l1hdr.b_state; 549589c86e32SChris Williamson ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 549689c86e32SChris Williamson ASSERT3P(state, !=, arc_anon); 549789c86e32SChris Williamson 549889c86e32SChris Williamson /* this buffer is not on any list */ 5499e914ace2STim Schumacher ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), >, 0); 550089c86e32SChris Williamson 550189c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr)) { 550289c86e32SChris Williamson mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); 5503244781f1SPrakash Surya 5504244781f1SPrakash Surya /* 5505a52fc310SPrakash Surya * We have to recheck this conditional again now that 5506a52fc310SPrakash Surya * we're holding the l2ad_mtx to prevent a race with 5507a52fc310SPrakash Surya * another thread which might be concurrently calling 5508a52fc310SPrakash Surya * l2arc_evict(). In that case, l2arc_evict() might have 5509a52fc310SPrakash Surya * destroyed the header's L2 portion as we were waiting 5510a52fc310SPrakash Surya * to acquire the l2ad_mtx. 5511244781f1SPrakash Surya */ 5512a52fc310SPrakash Surya if (HDR_HAS_L2HDR(hdr)) 5513a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(hdr); 5514244781f1SPrakash Surya 551589c86e32SChris Williamson mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); 55166f83844dSMark Maybee } 55176f83844dSMark Maybee 5518ea8dc4b6Seschrock /* 5519ea8dc4b6Seschrock * Do we have more than one buf? 5520ea8dc4b6Seschrock */ 5521dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_bufcnt > 1) { 5522fa9e4066Sahrens arc_buf_hdr_t *nhdr; 5523ac05c741SMark Maybee uint64_t spa = hdr->b_spa; 5524dcbf3bd6SGeorge Wilson uint64_t psize = HDR_GET_PSIZE(hdr); 5525dcbf3bd6SGeorge Wilson uint64_t lsize = HDR_GET_LSIZE(hdr); 5526dcbf3bd6SGeorge Wilson enum zio_compress compress = HDR_GET_COMPRESS(hdr); 552789c86e32SChris Williamson arc_buf_contents_t type = arc_buf_type(hdr); 5528dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 5529fa9e4066Sahrens 553089c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); 5531dcbf3bd6SGeorge Wilson (void) remove_reference(hdr, hash_lock, tag); 5532dcbf3bd6SGeorge Wilson 55335602294fSDan Kimmel if (arc_buf_is_shared(buf) && !ARC_BUF_COMPRESSED(buf)) { 5534dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); 5535dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(buf)); 5536dcbf3bd6SGeorge Wilson } 5537dcbf3bd6SGeorge Wilson 5538fa9e4066Sahrens /* 55393f9d6ad7SLin Ling * Pull the data off of this hdr and attach it to 5540dcbf3bd6SGeorge Wilson * a new anonymous hdr. Also find the last buffer 5541dcbf3bd6SGeorge Wilson * in the hdr's buffer list. 5542fa9e4066Sahrens */ 55435602294fSDan Kimmel arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); 5544dcbf3bd6SGeorge Wilson ASSERT3P(lastbuf, !=, NULL); 5545dcbf3bd6SGeorge Wilson 5546dcbf3bd6SGeorge Wilson /* 5547dcbf3bd6SGeorge Wilson * If the current arc_buf_t and the hdr are sharing their data 55485602294fSDan Kimmel * buffer, then we must stop sharing that block. 5549dcbf3bd6SGeorge Wilson */ 5550dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 5551dcbf3bd6SGeorge Wilson VERIFY(!arc_buf_is_shared(lastbuf)); 5552ea8dc4b6Seschrock 5553dcbf3bd6SGeorge Wilson /* 5554dcbf3bd6SGeorge Wilson * First, sever the block sharing relationship between 55555602294fSDan Kimmel * buf and the arc_buf_hdr_t. 5556dcbf3bd6SGeorge Wilson */ 5557dcbf3bd6SGeorge Wilson arc_unshare_buf(hdr, buf); 55585602294fSDan Kimmel 55595602294fSDan Kimmel /* 5560770499e1SDan Kimmel * Now we need to recreate the hdr's b_pabd. Since we 55615602294fSDan Kimmel * have lastbuf handy, we try to share with it, but if 5562770499e1SDan Kimmel * we can't then we allocate a new b_pabd and copy the 55635602294fSDan Kimmel * data from buf into it. 55645602294fSDan Kimmel */ 55655602294fSDan Kimmel if (arc_can_share(hdr, lastbuf)) { 55665602294fSDan Kimmel arc_share_buf(hdr, lastbuf); 55675602294fSDan Kimmel } else { 5568770499e1SDan Kimmel arc_hdr_alloc_pabd(hdr); 5569770499e1SDan Kimmel abd_copy_from_buf(hdr->b_l1hdr.b_pabd, 5570770499e1SDan Kimmel buf->b_data, psize); 55715602294fSDan Kimmel } 5572dcbf3bd6SGeorge Wilson VERIFY3P(lastbuf->b_data, !=, NULL); 5573dcbf3bd6SGeorge Wilson } else if (HDR_SHARED_DATA(hdr)) { 55745602294fSDan Kimmel /* 55755602294fSDan Kimmel * Uncompressed shared buffers are always at the end 55765602294fSDan Kimmel * of the list. Compressed buffers don't have the 55775602294fSDan Kimmel * same requirements. This makes it hard to 55785602294fSDan Kimmel * simply assert that the lastbuf is shared so 55795602294fSDan Kimmel * we rely on the hdr's compression flags to determine 55805602294fSDan Kimmel * if we have a compressed, shared buffer. 55815602294fSDan Kimmel */ 55825602294fSDan Kimmel ASSERT(arc_buf_is_shared(lastbuf) || 55835602294fSDan Kimmel HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF); 55845602294fSDan Kimmel ASSERT(!ARC_BUF_SHARED(buf)); 5585dcbf3bd6SGeorge Wilson } 5586770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 558789c86e32SChris Williamson ASSERT3P(state, !=, arc_l2c_only); 55882fd872a7SPrakash Surya 5589e914ace2STim Schumacher (void) zfs_refcount_remove_many(&state->arcs_size, 55905602294fSDan Kimmel arc_buf_size(buf), buf); 55912fd872a7SPrakash Surya 5592e914ace2STim Schumacher if (zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { 559389c86e32SChris Williamson ASSERT3P(state, !=, arc_l2c_only); 5594e914ace2STim Schumacher (void) zfs_refcount_remove_many( 5595e914ace2STim Schumacher &state->arcs_esize[type], 55965602294fSDan Kimmel arc_buf_size(buf), buf); 5597ea8dc4b6Seschrock } 55989253d63dSGeorge Wilson 5599dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt -= 1; 5600c717a561Smaybee arc_cksum_verify(buf); 5601cd1c8b85SMatthew Ahrens arc_buf_unwatch(buf); 5602ea8dc4b6Seschrock 5603fa9e4066Sahrens mutex_exit(hash_lock); 5604fa9e4066Sahrens 5605dcbf3bd6SGeorge Wilson /* 5606770499e1SDan Kimmel * Allocate a new hdr. The new hdr will contain a b_pabd 5607dcbf3bd6SGeorge Wilson * buffer which will be freed in arc_write(). 5608dcbf3bd6SGeorge Wilson */ 5609dcbf3bd6SGeorge Wilson nhdr = arc_hdr_alloc(spa, psize, lsize, compress, type); 5610dcbf3bd6SGeorge Wilson ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL); 5611dcbf3bd6SGeorge Wilson ASSERT0(nhdr->b_l1hdr.b_bufcnt); 5612e914ace2STim Schumacher ASSERT0(zfs_refcount_count(&nhdr->b_l1hdr.b_refcnt)); 5613dcbf3bd6SGeorge Wilson VERIFY3U(nhdr->b_type, ==, type); 5614dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(nhdr)); 561589c86e32SChris Williamson 561689c86e32SChris Williamson nhdr->b_l1hdr.b_buf = buf; 5617dcbf3bd6SGeorge Wilson nhdr->b_l1hdr.b_bufcnt = 1; 5618e914ace2STim Schumacher (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); 5619af2c4821Smaybee buf->b_hdr = nhdr; 5620dcbf3bd6SGeorge Wilson 56213f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 5622e914ace2STim Schumacher (void) zfs_refcount_add_many(&arc_anon->arcs_size, 56235602294fSDan Kimmel arc_buf_size(buf), buf); 5624fa9e4066Sahrens } else { 56253f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 5626e914ace2STim Schumacher ASSERT(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); 5627244781f1SPrakash Surya /* protected by hash lock, or hdr is on arc_anon */ 5628244781f1SPrakash Surya ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 5629fa9e4066Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 563089c86e32SChris Williamson arc_change_state(arc_anon, hdr, hash_lock); 563189c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = 0; 563289c86e32SChris Williamson mutex_exit(hash_lock); 5633fa94a07fSbrendan 56343f9d6ad7SLin Ling buf_discard_identity(hdr); 5635c717a561Smaybee arc_buf_thaw(buf); 5636fa9e4066Sahrens } 5637fa9e4066Sahrens } 5638fa9e4066Sahrens 5639fa9e4066Sahrens int 5640fa9e4066Sahrens arc_released(arc_buf_t *buf) 5641fa9e4066Sahrens { 56426f83844dSMark Maybee int released; 56436f83844dSMark Maybee 56443f9d6ad7SLin Ling mutex_enter(&buf->b_evict_lock); 564589c86e32SChris Williamson released = (buf->b_data != NULL && 564689c86e32SChris Williamson buf->b_hdr->b_l1hdr.b_state == arc_anon); 56473f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 56486f83844dSMark Maybee return (released); 5649ea8dc4b6Seschrock } 5650ea8dc4b6Seschrock 5651ea8dc4b6Seschrock #ifdef ZFS_DEBUG 5652ea8dc4b6Seschrock int 5653ea8dc4b6Seschrock arc_referenced(arc_buf_t *buf) 5654ea8dc4b6Seschrock { 56556f83844dSMark Maybee int referenced; 56566f83844dSMark Maybee 56573f9d6ad7SLin Ling mutex_enter(&buf->b_evict_lock); 5658e914ace2STim Schumacher referenced = (zfs_refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); 56593f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 56606f83844dSMark Maybee return (referenced); 5661ea8dc4b6Seschrock } 5662ea8dc4b6Seschrock #endif 5663ea8dc4b6Seschrock 5664c717a561Smaybee static void 5665c717a561Smaybee arc_write_ready(zio_t *zio) 5666c717a561Smaybee { 5667c717a561Smaybee arc_write_callback_t *callback = zio->io_private; 5668c717a561Smaybee arc_buf_t *buf = callback->awcb_buf; 56690a4e9518Sgw arc_buf_hdr_t *hdr = buf->b_hdr; 5670dcbf3bd6SGeorge Wilson uint64_t psize = BP_IS_HOLE(zio->io_bp) ? 0 : BP_GET_PSIZE(zio->io_bp); 5671c717a561Smaybee 567289c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 5673e914ace2STim Schumacher ASSERT(!zfs_refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); 5674dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 5675e14bb325SJeff Bonwick 56760a4e9518Sgw /* 5677dcbf3bd6SGeorge Wilson * If we're reexecuting this zio because the pool suspended, then 5678dcbf3bd6SGeorge Wilson * cleanup any state that was previously set the first time the 56795602294fSDan Kimmel * callback was invoked. 56800a4e9518Sgw */ 5681dcbf3bd6SGeorge Wilson if (zio->io_flags & ZIO_FLAG_REEXECUTED) { 5682dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 5683dcbf3bd6SGeorge Wilson arc_buf_unwatch(buf); 5684770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 5685dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 5686dcbf3bd6SGeorge Wilson arc_unshare_buf(hdr, buf); 5687dcbf3bd6SGeorge Wilson } else { 5688770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 5689dcbf3bd6SGeorge Wilson } 56900a4e9518Sgw } 56910a4e9518Sgw } 5692770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 5693dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 5694dcbf3bd6SGeorge Wilson ASSERT(!arc_buf_is_shared(buf)); 5695dcbf3bd6SGeorge Wilson 5696dcbf3bd6SGeorge Wilson callback->awcb_ready(zio, buf, callback->awcb_private); 5697dcbf3bd6SGeorge Wilson 5698dcbf3bd6SGeorge Wilson if (HDR_IO_IN_PROGRESS(hdr)) 5699dcbf3bd6SGeorge Wilson ASSERT(zio->io_flags & ZIO_FLAG_REEXECUTED); 5700dcbf3bd6SGeorge Wilson 5701dcbf3bd6SGeorge Wilson arc_cksum_compute(buf); 5702dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5703dcbf3bd6SGeorge Wilson 5704dcbf3bd6SGeorge Wilson enum zio_compress compress; 5705dcbf3bd6SGeorge Wilson if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 5706dcbf3bd6SGeorge Wilson compress = ZIO_COMPRESS_OFF; 5707dcbf3bd6SGeorge Wilson } else { 5708dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(zio->io_bp)); 5709dcbf3bd6SGeorge Wilson compress = BP_GET_COMPRESS(zio->io_bp); 5710dcbf3bd6SGeorge Wilson } 5711dcbf3bd6SGeorge Wilson HDR_SET_PSIZE(hdr, psize); 5712dcbf3bd6SGeorge Wilson arc_hdr_set_compress(hdr, compress); 5713dcbf3bd6SGeorge Wilson 5714770499e1SDan Kimmel 5715dcbf3bd6SGeorge Wilson /* 5716770499e1SDan Kimmel * Fill the hdr with data. If the hdr is compressed, the data we want 5717770499e1SDan Kimmel * is available from the zio, otherwise we can take it from the buf. 5718770499e1SDan Kimmel * 5719770499e1SDan Kimmel * We might be able to share the buf's data with the hdr here. However, 5720770499e1SDan Kimmel * doing so would cause the ARC to be full of linear ABDs if we write a 5721770499e1SDan Kimmel * lot of shareable data. As a compromise, we check whether scattered 5722770499e1SDan Kimmel * ABDs are allowed, and assume that if they are then the user wants 5723770499e1SDan Kimmel * the ARC to be primarily filled with them regardless of the data being 5724770499e1SDan Kimmel * written. Therefore, if they're allowed then we allocate one and copy 5725770499e1SDan Kimmel * the data into it; otherwise, we share the data directly if we can. 5726dcbf3bd6SGeorge Wilson */ 5727770499e1SDan Kimmel if (zfs_abd_scatter_enabled || !arc_can_share(hdr, buf)) { 5728770499e1SDan Kimmel arc_hdr_alloc_pabd(hdr); 5729770499e1SDan Kimmel 5730770499e1SDan Kimmel /* 5731770499e1SDan Kimmel * Ideally, we would always copy the io_abd into b_pabd, but the 5732770499e1SDan Kimmel * user may have disabled compressed ARC, thus we must check the 5733770499e1SDan Kimmel * hdr's compression setting rather than the io_bp's. 5734770499e1SDan Kimmel */ 5735770499e1SDan Kimmel if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) { 5736770499e1SDan Kimmel ASSERT3U(BP_GET_COMPRESS(zio->io_bp), !=, 5737770499e1SDan Kimmel ZIO_COMPRESS_OFF); 5738770499e1SDan Kimmel ASSERT3U(psize, >, 0); 5739770499e1SDan Kimmel 5740770499e1SDan Kimmel abd_copy(hdr->b_l1hdr.b_pabd, zio->io_abd, psize); 5741770499e1SDan Kimmel } else { 5742770499e1SDan Kimmel ASSERT3U(zio->io_orig_size, ==, arc_hdr_size(hdr)); 5743770499e1SDan Kimmel 5744770499e1SDan Kimmel abd_copy_from_buf(hdr->b_l1hdr.b_pabd, buf->b_data, 5745770499e1SDan Kimmel arc_buf_size(buf)); 5746770499e1SDan Kimmel } 5747dcbf3bd6SGeorge Wilson } else { 5748770499e1SDan Kimmel ASSERT3P(buf->b_data, ==, abd_to_buf(zio->io_orig_abd)); 57495602294fSDan Kimmel ASSERT3U(zio->io_orig_size, ==, arc_buf_size(buf)); 5750dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 5751dcbf3bd6SGeorge Wilson 5752dcbf3bd6SGeorge Wilson arc_share_buf(hdr, buf); 5753dcbf3bd6SGeorge Wilson } 5754770499e1SDan Kimmel 5755dcbf3bd6SGeorge Wilson arc_hdr_verify(hdr, zio->io_bp); 5756c717a561Smaybee } 5757c717a561Smaybee 57588df0bcf0SPaul Dagnelie static void 57598df0bcf0SPaul Dagnelie arc_write_children_ready(zio_t *zio) 57608df0bcf0SPaul Dagnelie { 57618df0bcf0SPaul Dagnelie arc_write_callback_t *callback = zio->io_private; 57628df0bcf0SPaul Dagnelie arc_buf_t *buf = callback->awcb_buf; 57638df0bcf0SPaul Dagnelie 57648df0bcf0SPaul Dagnelie callback->awcb_children_ready(zio, buf, callback->awcb_private); 57658df0bcf0SPaul Dagnelie } 57668df0bcf0SPaul Dagnelie 576769962b56SMatthew Ahrens /* 576869962b56SMatthew Ahrens * The SPA calls this callback for each physical write that happens on behalf 576969962b56SMatthew Ahrens * of a logical write. See the comment in dbuf_write_physdone() for details. 577069962b56SMatthew Ahrens */ 577169962b56SMatthew Ahrens static void 577269962b56SMatthew Ahrens arc_write_physdone(zio_t *zio) 577369962b56SMatthew Ahrens { 577469962b56SMatthew Ahrens arc_write_callback_t *cb = zio->io_private; 577569962b56SMatthew Ahrens if (cb->awcb_physdone != NULL) 577669962b56SMatthew Ahrens cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 577769962b56SMatthew Ahrens } 577869962b56SMatthew Ahrens 5779fa9e4066Sahrens static void 5780fa9e4066Sahrens arc_write_done(zio_t *zio) 5781fa9e4066Sahrens { 5782c717a561Smaybee arc_write_callback_t *callback = zio->io_private; 5783c717a561Smaybee arc_buf_t *buf = callback->awcb_buf; 5784c717a561Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 5785fa9e4066Sahrens 5786dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 5787b24ab676SJeff Bonwick 5788b24ab676SJeff Bonwick if (zio->io_error == 0) { 5789dcbf3bd6SGeorge Wilson arc_hdr_verify(hdr, zio->io_bp); 5790dcbf3bd6SGeorge Wilson 57915d7b4d43SMatthew Ahrens if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 579243466aaeSMax Grossman buf_discard_identity(hdr); 579343466aaeSMax Grossman } else { 579443466aaeSMax Grossman hdr->b_dva = *BP_IDENTITY(zio->io_bp); 579543466aaeSMax Grossman hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 579643466aaeSMax Grossman } 5797b24ab676SJeff Bonwick } else { 5798dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 5799b24ab676SJeff Bonwick } 5800fa9e4066Sahrens 5801ea8dc4b6Seschrock /* 58025d7b4d43SMatthew Ahrens * If the block to be written was all-zero or compressed enough to be 58035d7b4d43SMatthew Ahrens * embedded in the BP, no write was performed so there will be no 58045d7b4d43SMatthew Ahrens * dva/birth/checksum. The buffer must therefore remain anonymous 58055d7b4d43SMatthew Ahrens * (and uncached). 5806ea8dc4b6Seschrock */ 5807dcbf3bd6SGeorge Wilson if (!HDR_EMPTY(hdr)) { 5808fa9e4066Sahrens arc_buf_hdr_t *exists; 5809fa9e4066Sahrens kmutex_t *hash_lock; 5810fa9e4066Sahrens 58115602294fSDan Kimmel ASSERT3U(zio->io_error, ==, 0); 5812b24ab676SJeff Bonwick 58136b4acc8bSahrens arc_cksum_verify(buf); 58146b4acc8bSahrens 5815fa9e4066Sahrens exists = buf_hash_insert(hdr, &hash_lock); 581689c86e32SChris Williamson if (exists != NULL) { 5817fa9e4066Sahrens /* 5818fa9e4066Sahrens * This can only happen if we overwrite for 5819fa9e4066Sahrens * sync-to-convergence, because we remove 5820fa9e4066Sahrens * buffers from the hash table when we arc_free(). 5821fa9e4066Sahrens */ 5822b24ab676SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 5823b24ab676SJeff Bonwick if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 5824b24ab676SJeff Bonwick panic("bad overwrite, hdr=%p exists=%p", 5825b24ab676SJeff Bonwick (void *)hdr, (void *)exists); 5826e914ace2STim Schumacher ASSERT(zfs_refcount_is_zero( 582789c86e32SChris Williamson &exists->b_l1hdr.b_refcnt)); 5828b24ab676SJeff Bonwick arc_change_state(arc_anon, exists, hash_lock); 5829b24ab676SJeff Bonwick mutex_exit(hash_lock); 5830b24ab676SJeff Bonwick arc_hdr_destroy(exists); 5831b24ab676SJeff Bonwick exists = buf_hash_insert(hdr, &hash_lock); 5832b24ab676SJeff Bonwick ASSERT3P(exists, ==, NULL); 583380901aeaSGeorge Wilson } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 583480901aeaSGeorge Wilson /* nopwrite */ 583580901aeaSGeorge Wilson ASSERT(zio->io_prop.zp_nopwrite); 583680901aeaSGeorge Wilson if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 583780901aeaSGeorge Wilson panic("bad nopwrite, hdr=%p exists=%p", 583880901aeaSGeorge Wilson (void *)hdr, (void *)exists); 5839b24ab676SJeff Bonwick } else { 5840b24ab676SJeff Bonwick /* Dedup */ 5841dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt == 1); 584289c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_state == arc_anon); 5843b24ab676SJeff Bonwick ASSERT(BP_GET_DEDUP(zio->io_bp)); 5844b24ab676SJeff Bonwick ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 5845ae46e4c7SMatthew Ahrens } 5846fa9e4066Sahrens } 5847dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5848088f3894Sahrens /* if it's not anon, we are doing a scrub */ 584989c86e32SChris Williamson if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) 5850088f3894Sahrens arc_access(hdr, hash_lock); 585144eda4d7Smaybee mutex_exit(hash_lock); 5852ea8dc4b6Seschrock } else { 5853dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5854fa9e4066Sahrens } 5855ea8dc4b6Seschrock 5856e914ace2STim Schumacher ASSERT(!zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 5857b24ab676SJeff Bonwick callback->awcb_done(zio, buf, callback->awcb_private); 5858fa9e4066Sahrens 5859770499e1SDan Kimmel abd_put(zio->io_abd); 5860c717a561Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 5861fa9e4066Sahrens } 5862fa9e4066Sahrens 5863c717a561Smaybee zio_t * 5864dcbf3bd6SGeorge Wilson arc_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 5865*a3874b8bSToomas Soome boolean_t l2arc, const zio_prop_t *zp, arc_write_done_func_t *ready, 5866*a3874b8bSToomas Soome arc_write_done_func_t *children_ready, arc_write_done_func_t *physdone, 5867*a3874b8bSToomas Soome arc_write_done_func_t *done, void *private, zio_priority_t priority, 58687802d7bfSMatthew Ahrens int zio_flags, const zbookmark_phys_t *zb) 5869fa9e4066Sahrens { 5870fa9e4066Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 5871c717a561Smaybee arc_write_callback_t *callback; 5872e14bb325SJeff Bonwick zio_t *zio; 5873adaec86aSMatthew Ahrens zio_prop_t localprop = *zp; 5874fa9e4066Sahrens 5875dcbf3bd6SGeorge Wilson ASSERT3P(ready, !=, NULL); 5876dcbf3bd6SGeorge Wilson ASSERT3P(done, !=, NULL); 5877fa9e4066Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 587889c86e32SChris Williamson ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 5879dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 5880dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); 58813baa08fcSek if (l2arc) 5882dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 58835602294fSDan Kimmel if (ARC_BUF_COMPRESSED(buf)) { 5884adaec86aSMatthew Ahrens /* 5885adaec86aSMatthew Ahrens * We're writing a pre-compressed buffer. Make the 5886adaec86aSMatthew Ahrens * compression algorithm requested by the zio_prop_t match 5887adaec86aSMatthew Ahrens * the pre-compressed buffer's compression algorithm. 5888adaec86aSMatthew Ahrens */ 5889adaec86aSMatthew Ahrens localprop.zp_compress = HDR_GET_COMPRESS(hdr); 5890adaec86aSMatthew Ahrens 58915602294fSDan Kimmel ASSERT3U(HDR_GET_LSIZE(hdr), !=, arc_buf_size(buf)); 58925602294fSDan Kimmel zio_flags |= ZIO_FLAG_RAW; 58935602294fSDan Kimmel } 5894c717a561Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 5895c717a561Smaybee callback->awcb_ready = ready; 58968df0bcf0SPaul Dagnelie callback->awcb_children_ready = children_ready; 589769962b56SMatthew Ahrens callback->awcb_physdone = physdone; 5898c717a561Smaybee callback->awcb_done = done; 5899c717a561Smaybee callback->awcb_private = private; 5900c717a561Smaybee callback->awcb_buf = buf; 5901088f3894Sahrens 5902dcbf3bd6SGeorge Wilson /* 5903770499e1SDan Kimmel * The hdr's b_pabd is now stale, free it now. A new data block 5904dcbf3bd6SGeorge Wilson * will be allocated when the zio pipeline calls arc_write_ready(). 5905dcbf3bd6SGeorge Wilson */ 5906770499e1SDan Kimmel if (hdr->b_l1hdr.b_pabd != NULL) { 5907dcbf3bd6SGeorge Wilson /* 5908dcbf3bd6SGeorge Wilson * If the buf is currently sharing the data block with 5909dcbf3bd6SGeorge Wilson * the hdr then we need to break that relationship here. 5910dcbf3bd6SGeorge Wilson * The hdr will remain with a NULL data pointer and the 5911dcbf3bd6SGeorge Wilson * buf will take sole ownership of the block. 5912dcbf3bd6SGeorge Wilson */ 5913dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 5914dcbf3bd6SGeorge Wilson arc_unshare_buf(hdr, buf); 5915dcbf3bd6SGeorge Wilson } else { 5916770499e1SDan Kimmel arc_hdr_free_pabd(hdr); 5917dcbf3bd6SGeorge Wilson } 5918dcbf3bd6SGeorge Wilson VERIFY3P(buf->b_data, !=, NULL); 5919dcbf3bd6SGeorge Wilson arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF); 5920dcbf3bd6SGeorge Wilson } 5921dcbf3bd6SGeorge Wilson ASSERT(!arc_buf_is_shared(buf)); 5922770499e1SDan Kimmel ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 5923dcbf3bd6SGeorge Wilson 5924770499e1SDan Kimmel zio = zio_write(pio, spa, txg, bp, 5925770499e1SDan Kimmel abd_get_from_buf(buf->b_data, HDR_GET_LSIZE(hdr)), 5926adaec86aSMatthew Ahrens HDR_GET_LSIZE(hdr), arc_buf_size(buf), &localprop, arc_write_ready, 59278df0bcf0SPaul Dagnelie (children_ready != NULL) ? arc_write_children_ready : NULL, 59288df0bcf0SPaul Dagnelie arc_write_physdone, arc_write_done, callback, 592969962b56SMatthew Ahrens priority, zio_flags, zb); 5930fa9e4066Sahrens 5931c717a561Smaybee return (zio); 5932fa9e4066Sahrens } 5933fa9e4066Sahrens 59341ab7f2deSmaybee static int 5935abe1fd01SDon Brady arc_memory_throttle(spa_t *spa, uint64_t reserve, uint64_t txg) 59361ab7f2deSmaybee { 59371ab7f2deSmaybee #ifdef _KERNEL 59381ab7f2deSmaybee uint64_t available_memory = ptob(freemem); 59391ab7f2deSmaybee 59401ab7f2deSmaybee #if defined(__i386) 59411ab7f2deSmaybee available_memory = 59421ab7f2deSmaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 59431ab7f2deSmaybee #endif 594469962b56SMatthew Ahrens 594569962b56SMatthew Ahrens if (freemem > physmem * arc_lotsfree_percent / 100) 59461ab7f2deSmaybee return (0); 59471ab7f2deSmaybee 5948abe1fd01SDon Brady if (txg > spa->spa_lowmem_last_txg) { 5949abe1fd01SDon Brady spa->spa_lowmem_last_txg = txg; 5950abe1fd01SDon Brady spa->spa_lowmem_page_load = 0; 59511ab7f2deSmaybee } 59521ab7f2deSmaybee /* 59531ab7f2deSmaybee * If we are in pageout, we know that memory is already tight, 59541ab7f2deSmaybee * the arc is already going to be evicting, so we just want to 59551ab7f2deSmaybee * continue to let page writes occur as quickly as possible. 59561ab7f2deSmaybee */ 59571ab7f2deSmaybee if (curproc == proc_pageout) { 5958abe1fd01SDon Brady if (spa->spa_lowmem_page_load > 5959abe1fd01SDon Brady MAX(ptob(minfree), available_memory) / 4) 5960be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART)); 59611ab7f2deSmaybee /* Note: reserve is inflated, so we deflate */ 5962abe1fd01SDon Brady atomic_add_64(&spa->spa_lowmem_page_load, reserve / 8); 59631ab7f2deSmaybee return (0); 5964abe1fd01SDon Brady } else if (spa->spa_lowmem_page_load > 0 && arc_reclaim_needed()) { 59651ab7f2deSmaybee /* memory is low, delay before restarting */ 59661ab7f2deSmaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 5967be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 59681ab7f2deSmaybee } 5969abe1fd01SDon Brady spa->spa_lowmem_page_load = 0; 5970abe1fd01SDon Brady #endif /* _KERNEL */ 59711ab7f2deSmaybee return (0); 59721ab7f2deSmaybee } 59731ab7f2deSmaybee 5974fa9e4066Sahrens void 59751ab7f2deSmaybee arc_tempreserve_clear(uint64_t reserve) 5976fa9e4066Sahrens { 59771ab7f2deSmaybee atomic_add_64(&arc_tempreserve, -reserve); 5978fa9e4066Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 5979fa9e4066Sahrens } 5980fa9e4066Sahrens 5981fa9e4066Sahrens int 5982abe1fd01SDon Brady arc_tempreserve_space(spa_t *spa, uint64_t reserve, uint64_t txg) 5983fa9e4066Sahrens { 59841ab7f2deSmaybee int error; 59852fdbea25SAleksandr Guzovskiy uint64_t anon_size; 59861ab7f2deSmaybee 59871ab7f2deSmaybee if (reserve > arc_c/4 && !arc_no_grow) 59881ab7f2deSmaybee arc_c = MIN(arc_c_max, reserve * 4); 59891ab7f2deSmaybee if (reserve > arc_c) 5990be6fd75aSMatthew Ahrens return (SET_ERROR(ENOMEM)); 5991112fe045Smaybee 59922fdbea25SAleksandr Guzovskiy /* 59932fdbea25SAleksandr Guzovskiy * Don't count loaned bufs as in flight dirty data to prevent long 59942fdbea25SAleksandr Guzovskiy * network delays from blocking transactions that are ready to be 59952fdbea25SAleksandr Guzovskiy * assigned to a txg. 59962fdbea25SAleksandr Guzovskiy */ 59975602294fSDan Kimmel 59985602294fSDan Kimmel /* assert that it has not wrapped around */ 59995602294fSDan Kimmel ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); 60005602294fSDan Kimmel 6001e914ace2STim Schumacher anon_size = MAX((int64_t)(zfs_refcount_count(&arc_anon->arcs_size) - 60022fd872a7SPrakash Surya arc_loaned_bytes), 0); 60032fdbea25SAleksandr Guzovskiy 60041ab7f2deSmaybee /* 60051ab7f2deSmaybee * Writes will, almost always, require additional memory allocations 6006f7170741SWill Andrews * in order to compress/encrypt/etc the data. We therefore need to 60071ab7f2deSmaybee * make sure that there is sufficient available memory for this. 60081ab7f2deSmaybee */ 6009abe1fd01SDon Brady error = arc_memory_throttle(spa, reserve, txg); 601069962b56SMatthew Ahrens if (error != 0) 60111ab7f2deSmaybee return (error); 60121ab7f2deSmaybee 6013fa9e4066Sahrens /* 6014112fe045Smaybee * Throttle writes when the amount of dirty data in the cache 6015112fe045Smaybee * gets too large. We try to keep the cache less than half full 6016112fe045Smaybee * of dirty blocks so that our sync times don't grow too large. 6017abe1fd01SDon Brady * 6018abe1fd01SDon Brady * In the case of one pool being built on another pool, we want 6019abe1fd01SDon Brady * to make sure we don't end up throttling the lower (backing) 6020abe1fd01SDon Brady * pool when the upper pool is the majority contributor to dirty 6021abe1fd01SDon Brady * data. To insure we make forward progress during throttling, we 6022abe1fd01SDon Brady * also check the current pool's net dirty data and only throttle 6023abe1fd01SDon Brady * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty 6024abe1fd01SDon Brady * data in the cache. 6025abe1fd01SDon Brady * 6026112fe045Smaybee * Note: if two requests come in concurrently, we might let them 6027112fe045Smaybee * both succeed, when one of them should fail. Not a huge deal. 6028fa9e4066Sahrens */ 6029abe1fd01SDon Brady uint64_t total_dirty = reserve + arc_tempreserve + anon_size; 6030abe1fd01SDon Brady uint64_t spa_dirty_anon = spa_dirty_data(spa); 60312fdbea25SAleksandr Guzovskiy 6032abe1fd01SDon Brady if (total_dirty > arc_c * zfs_arc_dirty_limit_percent / 100 && 6033abe1fd01SDon Brady anon_size > arc_c * zfs_arc_anon_limit_percent / 100 && 6034abe1fd01SDon Brady spa_dirty_anon > anon_size * zfs_arc_pool_dirty_percent / 100) { 6035dcbf3bd6SGeorge Wilson uint64_t meta_esize = 6036e914ace2STim Schumacher zfs_refcount_count( 6037e914ace2STim Schumacher &arc_anon->arcs_esize[ARC_BUFC_METADATA]); 6038dcbf3bd6SGeorge Wilson uint64_t data_esize = 6039e914ace2STim Schumacher zfs_refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 60400e8c6158Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 60410e8c6158Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 6042dcbf3bd6SGeorge Wilson arc_tempreserve >> 10, meta_esize >> 10, 6043dcbf3bd6SGeorge Wilson data_esize >> 10, reserve >> 10, arc_c >> 10); 6044be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART)); 6045fa9e4066Sahrens } 60461ab7f2deSmaybee atomic_add_64(&arc_tempreserve, reserve); 6047fa9e4066Sahrens return (0); 6048fa9e4066Sahrens } 6049fa9e4066Sahrens 60504076b1bfSPrakash Surya static void 60514076b1bfSPrakash Surya arc_kstat_update_state(arc_state_t *state, kstat_named_t *size, 60524076b1bfSPrakash Surya kstat_named_t *evict_data, kstat_named_t *evict_metadata) 60534076b1bfSPrakash Surya { 6054e914ace2STim Schumacher size->value.ui64 = zfs_refcount_count(&state->arcs_size); 6055dcbf3bd6SGeorge Wilson evict_data->value.ui64 = 6056e914ace2STim Schumacher zfs_refcount_count(&state->arcs_esize[ARC_BUFC_DATA]); 6057dcbf3bd6SGeorge Wilson evict_metadata->value.ui64 = 6058e914ace2STim Schumacher zfs_refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]); 60594076b1bfSPrakash Surya } 60604076b1bfSPrakash Surya 60614076b1bfSPrakash Surya static int 60624076b1bfSPrakash Surya arc_kstat_update(kstat_t *ksp, int rw) 60634076b1bfSPrakash Surya { 60644076b1bfSPrakash Surya arc_stats_t *as = ksp->ks_data; 60654076b1bfSPrakash Surya 60664076b1bfSPrakash Surya if (rw == KSTAT_WRITE) { 60674076b1bfSPrakash Surya return (EACCES); 60684076b1bfSPrakash Surya } else { 60694076b1bfSPrakash Surya arc_kstat_update_state(arc_anon, 60704076b1bfSPrakash Surya &as->arcstat_anon_size, 60714076b1bfSPrakash Surya &as->arcstat_anon_evictable_data, 60724076b1bfSPrakash Surya &as->arcstat_anon_evictable_metadata); 60734076b1bfSPrakash Surya arc_kstat_update_state(arc_mru, 60744076b1bfSPrakash Surya &as->arcstat_mru_size, 60754076b1bfSPrakash Surya &as->arcstat_mru_evictable_data, 60764076b1bfSPrakash Surya &as->arcstat_mru_evictable_metadata); 60774076b1bfSPrakash Surya arc_kstat_update_state(arc_mru_ghost, 60784076b1bfSPrakash Surya &as->arcstat_mru_ghost_size, 60794076b1bfSPrakash Surya &as->arcstat_mru_ghost_evictable_data, 60804076b1bfSPrakash Surya &as->arcstat_mru_ghost_evictable_metadata); 60814076b1bfSPrakash Surya arc_kstat_update_state(arc_mfu, 60824076b1bfSPrakash Surya &as->arcstat_mfu_size, 60834076b1bfSPrakash Surya &as->arcstat_mfu_evictable_data, 60844076b1bfSPrakash Surya &as->arcstat_mfu_evictable_metadata); 60854076b1bfSPrakash Surya arc_kstat_update_state(arc_mfu_ghost, 60864076b1bfSPrakash Surya &as->arcstat_mfu_ghost_size, 60874076b1bfSPrakash Surya &as->arcstat_mfu_ghost_evictable_data, 60884076b1bfSPrakash Surya &as->arcstat_mfu_ghost_evictable_metadata); 60893a2d8a1bSPaul Dagnelie 60903a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_size) = aggsum_value(&arc_size); 60913a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_meta_used) = aggsum_value(&arc_meta_used); 60923a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_data_size) = aggsum_value(&astat_data_size); 60933a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_metadata_size) = 60943a2d8a1bSPaul Dagnelie aggsum_value(&astat_metadata_size); 60953a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_hdr_size) = aggsum_value(&astat_hdr_size); 60963a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_other_size) = aggsum_value(&astat_other_size); 60973a2d8a1bSPaul Dagnelie ARCSTAT(arcstat_l2_hdr_size) = aggsum_value(&astat_l2_hdr_size); 60984076b1bfSPrakash Surya } 60994076b1bfSPrakash Surya 61004076b1bfSPrakash Surya return (0); 61014076b1bfSPrakash Surya } 61024076b1bfSPrakash Surya 6103dcbf3bd6SGeorge Wilson /* 6104dcbf3bd6SGeorge Wilson * This function *must* return indices evenly distributed between all 6105dcbf3bd6SGeorge Wilson * sublists of the multilist. This is needed due to how the ARC eviction 6106dcbf3bd6SGeorge Wilson * code is laid out; arc_evict_state() assumes ARC buffers are evenly 6107dcbf3bd6SGeorge Wilson * distributed between all sublists and uses this assumption when 6108dcbf3bd6SGeorge Wilson * deciding which sublist to evict from and how much to evict from it. 6109dcbf3bd6SGeorge Wilson */ 6110dcbf3bd6SGeorge Wilson unsigned int 6111dcbf3bd6SGeorge Wilson arc_state_multilist_index_func(multilist_t *ml, void *obj) 6112dcbf3bd6SGeorge Wilson { 6113dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = obj; 6114dcbf3bd6SGeorge Wilson 6115dcbf3bd6SGeorge Wilson /* 6116dcbf3bd6SGeorge Wilson * We rely on b_dva to generate evenly distributed index 6117dcbf3bd6SGeorge Wilson * numbers using buf_hash below. So, as an added precaution, 6118dcbf3bd6SGeorge Wilson * let's make sure we never add empty buffers to the arc lists. 6119dcbf3bd6SGeorge Wilson */ 6120dcbf3bd6SGeorge Wilson ASSERT(!HDR_EMPTY(hdr)); 6121dcbf3bd6SGeorge Wilson 6122dcbf3bd6SGeorge Wilson /* 6123dcbf3bd6SGeorge Wilson * The assumption here, is the hash value for a given 6124dcbf3bd6SGeorge Wilson * arc_buf_hdr_t will remain constant throughout it's lifetime 6125dcbf3bd6SGeorge Wilson * (i.e. it's b_spa, b_dva, and b_birth fields don't change). 6126dcbf3bd6SGeorge Wilson * Thus, we don't need to store the header's sublist index 6127dcbf3bd6SGeorge Wilson * on insertion, as this index can be recalculated on removal. 6128dcbf3bd6SGeorge Wilson * 6129dcbf3bd6SGeorge Wilson * Also, the low order bits of the hash value are thought to be 6130dcbf3bd6SGeorge Wilson * distributed evenly. Otherwise, in the case that the multilist 6131dcbf3bd6SGeorge Wilson * has a power of two number of sublists, each sublists' usage 6132dcbf3bd6SGeorge Wilson * would not be evenly distributed. 6133dcbf3bd6SGeorge Wilson */ 6134dcbf3bd6SGeorge Wilson return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % 6135dcbf3bd6SGeorge Wilson multilist_get_num_sublists(ml)); 6136dcbf3bd6SGeorge Wilson } 6137dcbf3bd6SGeorge Wilson 6138dcbf3bd6SGeorge Wilson static void 6139dcbf3bd6SGeorge Wilson arc_state_init(void) 6140dcbf3bd6SGeorge Wilson { 6141dcbf3bd6SGeorge Wilson arc_anon = &ARC_anon; 6142dcbf3bd6SGeorge Wilson arc_mru = &ARC_mru; 6143dcbf3bd6SGeorge Wilson arc_mru_ghost = &ARC_mru_ghost; 6144dcbf3bd6SGeorge Wilson arc_mfu = &ARC_mfu; 6145dcbf3bd6SGeorge Wilson arc_mfu_ghost = &ARC_mfu_ghost; 6146dcbf3bd6SGeorge Wilson arc_l2c_only = &ARC_l2c_only; 6147dcbf3bd6SGeorge Wilson 614894c2d0ebSMatthew Ahrens arc_mru->arcs_list[ARC_BUFC_METADATA] = 614994c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6150dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 615110fbdecbSMatthew Ahrens arc_state_multilist_index_func); 615294c2d0ebSMatthew Ahrens arc_mru->arcs_list[ARC_BUFC_DATA] = 615394c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6154dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 615510fbdecbSMatthew Ahrens arc_state_multilist_index_func); 615694c2d0ebSMatthew Ahrens arc_mru_ghost->arcs_list[ARC_BUFC_METADATA] = 615794c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6158dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 615910fbdecbSMatthew Ahrens arc_state_multilist_index_func); 616094c2d0ebSMatthew Ahrens arc_mru_ghost->arcs_list[ARC_BUFC_DATA] = 616194c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6162dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 616310fbdecbSMatthew Ahrens arc_state_multilist_index_func); 616494c2d0ebSMatthew Ahrens arc_mfu->arcs_list[ARC_BUFC_METADATA] = 616594c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6166dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 616710fbdecbSMatthew Ahrens arc_state_multilist_index_func); 616894c2d0ebSMatthew Ahrens arc_mfu->arcs_list[ARC_BUFC_DATA] = 616994c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6170dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 617110fbdecbSMatthew Ahrens arc_state_multilist_index_func); 617294c2d0ebSMatthew Ahrens arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA] = 617394c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6174dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 617510fbdecbSMatthew Ahrens arc_state_multilist_index_func); 617694c2d0ebSMatthew Ahrens arc_mfu_ghost->arcs_list[ARC_BUFC_DATA] = 617794c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6178dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 617910fbdecbSMatthew Ahrens arc_state_multilist_index_func); 618094c2d0ebSMatthew Ahrens arc_l2c_only->arcs_list[ARC_BUFC_METADATA] = 618194c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6182dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 618310fbdecbSMatthew Ahrens arc_state_multilist_index_func); 618494c2d0ebSMatthew Ahrens arc_l2c_only->arcs_list[ARC_BUFC_DATA] = 618594c2d0ebSMatthew Ahrens multilist_create(sizeof (arc_buf_hdr_t), 6186dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 618710fbdecbSMatthew Ahrens arc_state_multilist_index_func); 6188dcbf3bd6SGeorge Wilson 6189e914ace2STim Schumacher zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 6190e914ace2STim Schumacher zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 6191e914ace2STim Schumacher zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); 6192e914ace2STim Schumacher zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]); 6193e914ace2STim Schumacher zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); 6194e914ace2STim Schumacher zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); 6195e914ace2STim Schumacher zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); 6196e914ace2STim Schumacher zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); 6197e914ace2STim Schumacher zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); 6198e914ace2STim Schumacher zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); 6199e914ace2STim Schumacher zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); 6200e914ace2STim Schumacher zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); 6201e914ace2STim Schumacher 6202e914ace2STim Schumacher zfs_refcount_create(&arc_anon->arcs_size); 6203e914ace2STim Schumacher zfs_refcount_create(&arc_mru->arcs_size); 6204e914ace2STim Schumacher zfs_refcount_create(&arc_mru_ghost->arcs_size); 6205e914ace2STim Schumacher zfs_refcount_create(&arc_mfu->arcs_size); 6206e914ace2STim Schumacher zfs_refcount_create(&arc_mfu_ghost->arcs_size); 6207e914ace2STim Schumacher zfs_refcount_create(&arc_l2c_only->arcs_size); 62083a2d8a1bSPaul Dagnelie 62093a2d8a1bSPaul Dagnelie aggsum_init(&arc_meta_used, 0); 62103a2d8a1bSPaul Dagnelie aggsum_init(&arc_size, 0); 62113a2d8a1bSPaul Dagnelie aggsum_init(&astat_data_size, 0); 62123a2d8a1bSPaul Dagnelie aggsum_init(&astat_metadata_size, 0); 62133a2d8a1bSPaul Dagnelie aggsum_init(&astat_hdr_size, 0); 62143a2d8a1bSPaul Dagnelie aggsum_init(&astat_other_size, 0); 62153a2d8a1bSPaul Dagnelie aggsum_init(&astat_l2_hdr_size, 0); 6216dcbf3bd6SGeorge Wilson } 6217dcbf3bd6SGeorge Wilson 6218dcbf3bd6SGeorge Wilson static void 6219dcbf3bd6SGeorge Wilson arc_state_fini(void) 6220dcbf3bd6SGeorge Wilson { 6221e914ace2STim Schumacher zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 6222e914ace2STim Schumacher zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 6223e914ace2STim Schumacher zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); 6224e914ace2STim Schumacher zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]); 6225e914ace2STim Schumacher zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); 6226e914ace2STim Schumacher zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); 6227e914ace2STim Schumacher zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); 6228e914ace2STim Schumacher zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); 6229e914ace2STim Schumacher zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); 6230e914ace2STim Schumacher zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); 6231e914ace2STim Schumacher zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); 6232e914ace2STim Schumacher zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); 6233e914ace2STim Schumacher 6234e914ace2STim Schumacher zfs_refcount_destroy(&arc_anon->arcs_size); 6235e914ace2STim Schumacher zfs_refcount_destroy(&arc_mru->arcs_size); 6236e914ace2STim Schumacher zfs_refcount_destroy(&arc_mru_ghost->arcs_size); 6237e914ace2STim Schumacher zfs_refcount_destroy(&arc_mfu->arcs_size); 6238e914ace2STim Schumacher zfs_refcount_destroy(&arc_mfu_ghost->arcs_size); 6239e914ace2STim Schumacher zfs_refcount_destroy(&arc_l2c_only->arcs_size); 6240dcbf3bd6SGeorge Wilson 624194c2d0ebSMatthew Ahrens multilist_destroy(arc_mru->arcs_list[ARC_BUFC_METADATA]); 624294c2d0ebSMatthew Ahrens multilist_destroy(arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 624394c2d0ebSMatthew Ahrens multilist_destroy(arc_mfu->arcs_list[ARC_BUFC_METADATA]); 624494c2d0ebSMatthew Ahrens multilist_destroy(arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 624594c2d0ebSMatthew Ahrens multilist_destroy(arc_mru->arcs_list[ARC_BUFC_DATA]); 624694c2d0ebSMatthew Ahrens multilist_destroy(arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 624794c2d0ebSMatthew Ahrens multilist_destroy(arc_mfu->arcs_list[ARC_BUFC_DATA]); 624894c2d0ebSMatthew Ahrens multilist_destroy(arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 624929bf2d68SPaul Dagnelie 625029bf2d68SPaul Dagnelie aggsum_fini(&arc_meta_used); 625129bf2d68SPaul Dagnelie aggsum_fini(&arc_size); 625229bf2d68SPaul Dagnelie aggsum_fini(&astat_data_size); 625329bf2d68SPaul Dagnelie aggsum_fini(&astat_metadata_size); 625429bf2d68SPaul Dagnelie aggsum_fini(&astat_hdr_size); 625529bf2d68SPaul Dagnelie aggsum_fini(&astat_other_size); 625629bf2d68SPaul Dagnelie aggsum_fini(&astat_l2_hdr_size); 6257dcbf3bd6SGeorge Wilson } 6258dcbf3bd6SGeorge Wilson 6259dcbf3bd6SGeorge Wilson uint64_t 6260dcbf3bd6SGeorge Wilson arc_max_bytes(void) 6261244781f1SPrakash Surya { 6262dcbf3bd6SGeorge Wilson return (arc_c_max); 6263244781f1SPrakash Surya } 6264244781f1SPrakash Surya 6265fa9e4066Sahrens void 6266fa9e4066Sahrens arc_init(void) 6267fa9e4066Sahrens { 62682ec99e3eSMatthew Ahrens /* 62692ec99e3eSMatthew Ahrens * allmem is "all memory that we could possibly use". 62702ec99e3eSMatthew Ahrens */ 62712ec99e3eSMatthew Ahrens #ifdef _KERNEL 62722ec99e3eSMatthew Ahrens uint64_t allmem = ptob(physmem - swapfs_minfree); 62732ec99e3eSMatthew Ahrens #else 62742ec99e3eSMatthew Ahrens uint64_t allmem = (physmem * PAGESIZE) / 2; 62752ec99e3eSMatthew Ahrens #endif 6276de753e34SBrad Lewis mutex_init(&arc_adjust_lock, NULL, MUTEX_DEFAULT, NULL); 6277de753e34SBrad Lewis cv_init(&arc_adjust_waiters_cv, NULL, CV_DEFAULT, NULL); 6278244781f1SPrakash Surya 6279112fe045Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 62802ec99e3eSMatthew Ahrens arc_c_min = MAX(allmem / 32, 64 << 20); 6281112fe045Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 62822ec99e3eSMatthew Ahrens if (allmem >= 1 << 30) 62832ec99e3eSMatthew Ahrens arc_c_max = allmem - (1 << 30); 6284fa9e4066Sahrens else 628544cb6abcSbmc arc_c_max = arc_c_min; 62862ec99e3eSMatthew Ahrens arc_c_max = MAX(allmem * 3 / 4, arc_c_max); 6287a2eea2e1Sahrens 62888fe00bfbSMatthew Ahrens /* 62898fe00bfbSMatthew Ahrens * In userland, there's only the memory pressure that we artificially 62908fe00bfbSMatthew Ahrens * create (see arc_available_memory()). Don't let arc_c get too 62918fe00bfbSMatthew Ahrens * small, because it can cause transactions to be larger than 62928fe00bfbSMatthew Ahrens * arc_c, causing arc_tempreserve_space() to fail. 62938fe00bfbSMatthew Ahrens */ 62948fe00bfbSMatthew Ahrens #ifndef _KERNEL 62958fe00bfbSMatthew Ahrens arc_c_min = arc_c_max / 2; 62968fe00bfbSMatthew Ahrens #endif 62978fe00bfbSMatthew Ahrens 6298a2eea2e1Sahrens /* 6299a2eea2e1Sahrens * Allow the tunables to override our calculations if they are 6300a2eea2e1Sahrens * reasonable (ie. over 64MB) 6301a2eea2e1Sahrens */ 6302e5961710SMatthew Ahrens if (zfs_arc_max > 64 << 20 && zfs_arc_max < allmem) { 630344cb6abcSbmc arc_c_max = zfs_arc_max; 6304e5961710SMatthew Ahrens arc_c_min = MIN(arc_c_min, arc_c_max); 6305e5961710SMatthew Ahrens } 63062ec99e3eSMatthew Ahrens if (zfs_arc_min > 64 << 20 && zfs_arc_min <= arc_c_max) 630744cb6abcSbmc arc_c_min = zfs_arc_min; 6308a2eea2e1Sahrens 630944cb6abcSbmc arc_c = arc_c_max; 631044cb6abcSbmc arc_p = (arc_c >> 1); 6311fa9e4066Sahrens 63120e8c6158Smaybee /* limit meta-data to 1/4 of the arc capacity */ 63130e8c6158Smaybee arc_meta_limit = arc_c_max / 4; 63141116048bSek 6315af868f46SMatthew Ahrens #ifdef _KERNEL 6316af868f46SMatthew Ahrens /* 6317af868f46SMatthew Ahrens * Metadata is stored in the kernel's heap. Don't let us 6318af868f46SMatthew Ahrens * use more than half the heap for the ARC. 6319af868f46SMatthew Ahrens */ 6320af868f46SMatthew Ahrens arc_meta_limit = MIN(arc_meta_limit, 6321af868f46SMatthew Ahrens vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 2); 6322af868f46SMatthew Ahrens #endif 6323af868f46SMatthew Ahrens 63241116048bSek /* Allow the tunable to override if it is reasonable */ 63251116048bSek if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 63261116048bSek arc_meta_limit = zfs_arc_meta_limit; 63271116048bSek 63280e8c6158Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 63290e8c6158Smaybee arc_c_min = arc_meta_limit / 2; 63300e8c6158Smaybee 63313a5286a1SMatthew Ahrens if (zfs_arc_meta_min > 0) { 63323a5286a1SMatthew Ahrens arc_meta_min = zfs_arc_meta_min; 63333a5286a1SMatthew Ahrens } else { 63343a5286a1SMatthew Ahrens arc_meta_min = arc_c_min / 2; 63353a5286a1SMatthew Ahrens } 63363a5286a1SMatthew Ahrens 63375a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_grow_retry > 0) 63385a98e54bSBrendan Gregg - Sun Microsystems arc_grow_retry = zfs_arc_grow_retry; 63395a98e54bSBrendan Gregg - Sun Microsystems 63405a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_shrink_shift > 0) 63415a98e54bSBrendan Gregg - Sun Microsystems arc_shrink_shift = zfs_arc_shrink_shift; 63425a98e54bSBrendan Gregg - Sun Microsystems 63432ec99e3eSMatthew Ahrens /* 63442ec99e3eSMatthew Ahrens * Ensure that arc_no_grow_shift is less than arc_shrink_shift. 63452ec99e3eSMatthew Ahrens */ 63462ec99e3eSMatthew Ahrens if (arc_no_grow_shift >= arc_shrink_shift) 63472ec99e3eSMatthew Ahrens arc_no_grow_shift = arc_shrink_shift - 1; 63482ec99e3eSMatthew Ahrens 63495a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_p_min_shift > 0) 63505a98e54bSBrendan Gregg - Sun Microsystems arc_p_min_shift = zfs_arc_p_min_shift; 63515a98e54bSBrendan Gregg - Sun Microsystems 6352fa9e4066Sahrens /* if kmem_flags are set, lets try to use less memory */ 6353fa9e4066Sahrens if (kmem_debugging()) 635444cb6abcSbmc arc_c = arc_c / 2; 635544cb6abcSbmc if (arc_c < arc_c_min) 635644cb6abcSbmc arc_c = arc_c_min; 635744cb6abcSbmc 6358dcbf3bd6SGeorge Wilson arc_state_init(); 6359fa9e4066Sahrens 6360de753e34SBrad Lewis /* 6361de753e34SBrad Lewis * The arc must be "uninitialized", so that hdr_recl() (which is 6362de753e34SBrad Lewis * registered by buf_init()) will not access arc_reap_zthr before 6363de753e34SBrad Lewis * it is created. 6364de753e34SBrad Lewis */ 6365de753e34SBrad Lewis ASSERT(!arc_initialized); 6366de753e34SBrad Lewis buf_init(); 6367fa9e4066Sahrens 636844cb6abcSbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 636944cb6abcSbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 637044cb6abcSbmc 637144cb6abcSbmc if (arc_ksp != NULL) { 637244cb6abcSbmc arc_ksp->ks_data = &arc_stats; 63734076b1bfSPrakash Surya arc_ksp->ks_update = arc_kstat_update; 637444cb6abcSbmc kstat_install(arc_ksp); 637544cb6abcSbmc } 637644cb6abcSbmc 6377de753e34SBrad Lewis arc_adjust_zthr = zthr_create(arc_adjust_cb_check, 6378de753e34SBrad Lewis arc_adjust_cb, NULL); 6379de753e34SBrad Lewis arc_reap_zthr = zthr_create_timer(arc_reap_cb_check, 6380de753e34SBrad Lewis arc_reap_cb, NULL, SEC2NSEC(1)); 638149e3519aSmaybee 6382de753e34SBrad Lewis arc_initialized = B_TRUE; 63833a737e0dSbrendan arc_warm = B_FALSE; 63841ab7f2deSmaybee 638569962b56SMatthew Ahrens /* 638669962b56SMatthew Ahrens * Calculate maximum amount of dirty data per pool. 638769962b56SMatthew Ahrens * 638869962b56SMatthew Ahrens * If it has been set by /etc/system, take that. 638969962b56SMatthew Ahrens * Otherwise, use a percentage of physical memory defined by 639069962b56SMatthew Ahrens * zfs_dirty_data_max_percent (default 10%) with a cap at 639169962b56SMatthew Ahrens * zfs_dirty_data_max_max (default 4GB). 639269962b56SMatthew Ahrens */ 639369962b56SMatthew Ahrens if (zfs_dirty_data_max == 0) { 639469962b56SMatthew Ahrens zfs_dirty_data_max = physmem * PAGESIZE * 639569962b56SMatthew Ahrens zfs_dirty_data_max_percent / 100; 639669962b56SMatthew Ahrens zfs_dirty_data_max = MIN(zfs_dirty_data_max, 639769962b56SMatthew Ahrens zfs_dirty_data_max_max); 639869962b56SMatthew Ahrens } 6399fa9e4066Sahrens } 6400fa9e4066Sahrens 6401fa9e4066Sahrens void 6402fa9e4066Sahrens arc_fini(void) 6403fa9e4066Sahrens { 6404dcbf3bd6SGeorge Wilson /* Use B_TRUE to ensure *all* buffers are evicted */ 6405dcbf3bd6SGeorge Wilson arc_flush(NULL, B_TRUE); 6406fa9e4066Sahrens 6407de753e34SBrad Lewis arc_initialized = B_FALSE; 6408fa9e4066Sahrens 640944cb6abcSbmc if (arc_ksp != NULL) { 641044cb6abcSbmc kstat_delete(arc_ksp); 641144cb6abcSbmc arc_ksp = NULL; 641244cb6abcSbmc } 641344cb6abcSbmc 6414de753e34SBrad Lewis (void) zthr_cancel(arc_adjust_zthr); 6415de753e34SBrad Lewis zthr_destroy(arc_adjust_zthr); 6416de753e34SBrad Lewis 6417de753e34SBrad Lewis (void) zthr_cancel(arc_reap_zthr); 6418de753e34SBrad Lewis zthr_destroy(arc_reap_zthr); 6419de753e34SBrad Lewis 6420de753e34SBrad Lewis mutex_destroy(&arc_adjust_lock); 6421de753e34SBrad Lewis cv_destroy(&arc_adjust_waiters_cv); 6422244781f1SPrakash Surya 642329bf2d68SPaul Dagnelie /* 642429bf2d68SPaul Dagnelie * buf_fini() must proceed arc_state_fini() because buf_fin() may 642529bf2d68SPaul Dagnelie * trigger the release of kmem magazines, which can callback to 642629bf2d68SPaul Dagnelie * arc_space_return() which accesses aggsums freed in act_state_fini(). 642729bf2d68SPaul Dagnelie */ 6428fa9e4066Sahrens buf_fini(); 642929bf2d68SPaul Dagnelie arc_state_fini(); 64302fdbea25SAleksandr Guzovskiy 643189c86e32SChris Williamson ASSERT0(arc_loaned_bytes); 6432fa9e4066Sahrens } 6433fa94a07fSbrendan 6434fa94a07fSbrendan /* 6435fa94a07fSbrendan * Level 2 ARC 6436fa94a07fSbrendan * 6437fa94a07fSbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 6438fa94a07fSbrendan * It uses dedicated storage devices to hold cached data, which are populated 6439fa94a07fSbrendan * using large infrequent writes. The main role of this cache is to boost 6440fa94a07fSbrendan * the performance of random read workloads. The intended L2ARC devices 6441fa94a07fSbrendan * include short-stroked disks, solid state disks, and other media with 6442fa94a07fSbrendan * substantially faster read latency than disk. 6443fa94a07fSbrendan * 6444fa94a07fSbrendan * +-----------------------+ 6445fa94a07fSbrendan * | ARC | 6446fa94a07fSbrendan * +-----------------------+ 6447fa94a07fSbrendan * | ^ ^ 6448fa94a07fSbrendan * | | | 6449fa94a07fSbrendan * l2arc_feed_thread() arc_read() 6450fa94a07fSbrendan * | | | 6451fa94a07fSbrendan * | l2arc read | 6452fa94a07fSbrendan * V | | 6453fa94a07fSbrendan * +---------------+ | 6454fa94a07fSbrendan * | L2ARC | | 6455fa94a07fSbrendan * +---------------+ | 6456fa94a07fSbrendan * | ^ | 6457fa94a07fSbrendan * l2arc_write() | | 6458fa94a07fSbrendan * | | | 6459fa94a07fSbrendan * V | | 6460fa94a07fSbrendan * +-------+ +-------+ 6461fa94a07fSbrendan * | vdev | | vdev | 6462fa94a07fSbrendan * | cache | | cache | 6463fa94a07fSbrendan * +-------+ +-------+ 6464fa94a07fSbrendan * +=========+ .-----. 6465fa94a07fSbrendan * : L2ARC : |-_____-| 6466fa94a07fSbrendan * : devices : | Disks | 6467fa94a07fSbrendan * +=========+ `-_____-' 6468fa94a07fSbrendan * 6469fa94a07fSbrendan * Read requests are satisfied from the following sources, in order: 6470fa94a07fSbrendan * 6471fa94a07fSbrendan * 1) ARC 6472fa94a07fSbrendan * 2) vdev cache of L2ARC devices 6473fa94a07fSbrendan * 3) L2ARC devices 6474fa94a07fSbrendan * 4) vdev cache of disks 6475fa94a07fSbrendan * 5) disks 6476fa94a07fSbrendan * 6477fa94a07fSbrendan * Some L2ARC device types exhibit extremely slow write performance. 6478fa94a07fSbrendan * To accommodate for this there are some significant differences between 6479fa94a07fSbrendan * the L2ARC and traditional cache design: 6480fa94a07fSbrendan * 6481fa94a07fSbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 6482fa94a07fSbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 6483fa94a07fSbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 6484fa94a07fSbrendan * this would add inflated write latencies for all ARC memory pressure. 6485fa94a07fSbrendan * 6486fa94a07fSbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 6487fa94a07fSbrendan * It does this by periodically scanning buffers from the eviction-end of 6488fa94a07fSbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 6489aad02571SSaso Kiselkov * not already there. It scans until a headroom of buffers is satisfied, 6490aad02571SSaso Kiselkov * which itself is a buffer for ARC eviction. If a compressible buffer is 6491aad02571SSaso Kiselkov * found during scanning and selected for writing to an L2ARC device, we 6492aad02571SSaso Kiselkov * temporarily boost scanning headroom during the next scan cycle to make 6493aad02571SSaso Kiselkov * sure we adapt to compression effects (which might significantly reduce 6494aad02571SSaso Kiselkov * the data volume we write to L2ARC). The thread that does this is 6495fa94a07fSbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 6496fa94a07fSbrendan * provide a better sense of ratio than this diagram: 6497fa94a07fSbrendan * 6498fa94a07fSbrendan * head --> tail 6499fa94a07fSbrendan * +---------------------+----------+ 6500fa94a07fSbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 6501fa94a07fSbrendan * +---------------------+----------+ | o L2ARC eligible 6502fa94a07fSbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 6503fa94a07fSbrendan * +---------------------+----------+ | 6504fa94a07fSbrendan * 15.9 Gbytes ^ 32 Mbytes | 6505fa94a07fSbrendan * headroom | 6506fa94a07fSbrendan * l2arc_feed_thread() 6507fa94a07fSbrendan * | 6508fa94a07fSbrendan * l2arc write hand <--[oooo]--' 6509fa94a07fSbrendan * | 8 Mbyte 6510fa94a07fSbrendan * | write max 6511fa94a07fSbrendan * V 6512fa94a07fSbrendan * +==============================+ 6513fa94a07fSbrendan * L2ARC dev |####|#|###|###| |####| ... | 6514fa94a07fSbrendan * +==============================+ 6515fa94a07fSbrendan * 32 Gbytes 6516fa94a07fSbrendan * 6517fa94a07fSbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 6518fa94a07fSbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 6519fa94a07fSbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 6520fa94a07fSbrendan * safe to say that this is an uncommon case, since buffers at the end of 6521fa94a07fSbrendan * the ARC lists have moved there due to inactivity. 6522fa94a07fSbrendan * 6523fa94a07fSbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 6524fa94a07fSbrendan * then the L2ARC simply misses copying some buffers. This serves as a 6525fa94a07fSbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 6526fa94a07fSbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 6527fa94a07fSbrendan * the potential for the L2ARC to churn if it attempts to cache content too 6528fa94a07fSbrendan * quickly, such as during backups of the entire pool. 6529fa94a07fSbrendan * 65303a737e0dSbrendan * 5. After system boot and before the ARC has filled main memory, there are 65313a737e0dSbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 65323a737e0dSbrendan * lists can remain mostly static. Instead of searching from tail of these 65333a737e0dSbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 65343a737e0dSbrendan * for eligible buffers, greatly increasing its chance of finding them. 65353a737e0dSbrendan * 65363a737e0dSbrendan * The L2ARC device write speed is also boosted during this time so that 65373a737e0dSbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 65383a737e0dSbrendan * there are no L2ARC reads, and no fear of degrading read performance 65393a737e0dSbrendan * through increased writes. 65403a737e0dSbrendan * 65413a737e0dSbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 6542fa94a07fSbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 6543fa94a07fSbrendan * device is written to in a rotor fashion, sweeping writes through 6544fa94a07fSbrendan * available space then repeating. 6545fa94a07fSbrendan * 65463a737e0dSbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 6547fa94a07fSbrendan * write buffers back to disk based storage. 6548fa94a07fSbrendan * 65493a737e0dSbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 6550fa94a07fSbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 6551fa94a07fSbrendan * 6552fa94a07fSbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 6553fa94a07fSbrendan * may be necessary for different workloads: 6554fa94a07fSbrendan * 6555fa94a07fSbrendan * l2arc_write_max max write bytes per interval 65563a737e0dSbrendan * l2arc_write_boost extra write bytes during device warmup 6557fa94a07fSbrendan * l2arc_noprefetch skip caching prefetched buffers 6558fa94a07fSbrendan * l2arc_headroom number of max device writes to precache 6559aad02571SSaso Kiselkov * l2arc_headroom_boost when we find compressed buffers during ARC 6560aad02571SSaso Kiselkov * scanning, we multiply headroom by this 6561aad02571SSaso Kiselkov * percentage factor for the next scan cycle, 6562aad02571SSaso Kiselkov * since more compressed buffers are likely to 6563aad02571SSaso Kiselkov * be present 6564fa94a07fSbrendan * l2arc_feed_secs seconds between L2ARC writing 6565fa94a07fSbrendan * 6566fa94a07fSbrendan * Tunables may be removed or added as future performance improvements are 6567fa94a07fSbrendan * integrated, and also may become zpool properties. 65685a98e54bSBrendan Gregg - Sun Microsystems * 65695a98e54bSBrendan Gregg - Sun Microsystems * There are three key functions that control how the L2ARC warms up: 65705a98e54bSBrendan Gregg - Sun Microsystems * 65715a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_eligible() check if a buffer is eligible to cache 65725a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_size() calculate how much to write 65735a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_interval() calculate sleep delay between writes 65745a98e54bSBrendan Gregg - Sun Microsystems * 65755a98e54bSBrendan Gregg - Sun Microsystems * These three functions determine what to write, how much, and how quickly 65765a98e54bSBrendan Gregg - Sun Microsystems * to send writes. 6577fa94a07fSbrendan */ 6578fa94a07fSbrendan 65795a98e54bSBrendan Gregg - Sun Microsystems static boolean_t 65807adb730bSGeorge Wilson l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) 65815a98e54bSBrendan Gregg - Sun Microsystems { 65825a98e54bSBrendan Gregg - Sun Microsystems /* 65835a98e54bSBrendan Gregg - Sun Microsystems * A buffer is *not* eligible for the L2ARC if it: 65845a98e54bSBrendan Gregg - Sun Microsystems * 1. belongs to a different spa. 65855ea40c06SBrendan Gregg - Sun Microsystems * 2. is already cached on the L2ARC. 65865ea40c06SBrendan Gregg - Sun Microsystems * 3. has an I/O in progress (it may be an incomplete read). 65875ea40c06SBrendan Gregg - Sun Microsystems * 4. is flagged not eligible (zfs property). 65885a98e54bSBrendan Gregg - Sun Microsystems */ 658989c86e32SChris Williamson if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) || 65907adb730bSGeorge Wilson HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr)) 65915a98e54bSBrendan Gregg - Sun Microsystems return (B_FALSE); 65925a98e54bSBrendan Gregg - Sun Microsystems 65935a98e54bSBrendan Gregg - Sun Microsystems return (B_TRUE); 65945a98e54bSBrendan Gregg - Sun Microsystems } 65955a98e54bSBrendan Gregg - Sun Microsystems 65965a98e54bSBrendan Gregg - Sun Microsystems static uint64_t 6597aad02571SSaso Kiselkov l2arc_write_size(void) 65985a98e54bSBrendan Gregg - Sun Microsystems { 65995a98e54bSBrendan Gregg - Sun Microsystems uint64_t size; 66005a98e54bSBrendan Gregg - Sun Microsystems 6601aad02571SSaso Kiselkov /* 6602aad02571SSaso Kiselkov * Make sure our globals have meaningful values in case the user 6603aad02571SSaso Kiselkov * altered them. 6604aad02571SSaso Kiselkov */ 6605aad02571SSaso Kiselkov size = l2arc_write_max; 6606aad02571SSaso Kiselkov if (size == 0) { 6607aad02571SSaso Kiselkov cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 6608aad02571SSaso Kiselkov "be greater than zero, resetting it to the default (%d)", 6609aad02571SSaso Kiselkov L2ARC_WRITE_SIZE); 6610aad02571SSaso Kiselkov size = l2arc_write_max = L2ARC_WRITE_SIZE; 6611aad02571SSaso Kiselkov } 66125a98e54bSBrendan Gregg - Sun Microsystems 66135a98e54bSBrendan Gregg - Sun Microsystems if (arc_warm == B_FALSE) 6614aad02571SSaso Kiselkov size += l2arc_write_boost; 66155a98e54bSBrendan Gregg - Sun Microsystems 66165a98e54bSBrendan Gregg - Sun Microsystems return (size); 66175a98e54bSBrendan Gregg - Sun Microsystems 66185a98e54bSBrendan Gregg - Sun Microsystems } 66195a98e54bSBrendan Gregg - Sun Microsystems 66205a98e54bSBrendan Gregg - Sun Microsystems static clock_t 66215a98e54bSBrendan Gregg - Sun Microsystems l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 66225a98e54bSBrendan Gregg - Sun Microsystems { 6623d3d50737SRafael Vanoni clock_t interval, next, now; 66245a98e54bSBrendan Gregg - Sun Microsystems 66255a98e54bSBrendan Gregg - Sun Microsystems /* 66265a98e54bSBrendan Gregg - Sun Microsystems * If the ARC lists are busy, increase our write rate; if the 66275a98e54bSBrendan Gregg - Sun Microsystems * lists are stale, idle back. This is achieved by checking 66285a98e54bSBrendan Gregg - Sun Microsystems * how much we previously wrote - if it was more than half of 66295a98e54bSBrendan Gregg - Sun Microsystems * what we wanted, schedule the next write much sooner. 66305a98e54bSBrendan Gregg - Sun Microsystems */ 66315a98e54bSBrendan Gregg - Sun Microsystems if (l2arc_feed_again && wrote > (wanted / 2)) 66325a98e54bSBrendan Gregg - Sun Microsystems interval = (hz * l2arc_feed_min_ms) / 1000; 66335a98e54bSBrendan Gregg - Sun Microsystems else 66345a98e54bSBrendan Gregg - Sun Microsystems interval = hz * l2arc_feed_secs; 66355a98e54bSBrendan Gregg - Sun Microsystems 6636d3d50737SRafael Vanoni now = ddi_get_lbolt(); 6637d3d50737SRafael Vanoni next = MAX(now, MIN(now + interval, began + interval)); 66385a98e54bSBrendan Gregg - Sun Microsystems 66395a98e54bSBrendan Gregg - Sun Microsystems return (next); 66405a98e54bSBrendan Gregg - Sun Microsystems } 66415a98e54bSBrendan Gregg - Sun Microsystems 6642fa94a07fSbrendan /* 6643fa94a07fSbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 66443a737e0dSbrendan * If a device is returned, this also returns holding the spa config lock. 6645fa94a07fSbrendan */ 6646fa94a07fSbrendan static l2arc_dev_t * 6647fa94a07fSbrendan l2arc_dev_get_next(void) 6648fa94a07fSbrendan { 66493a737e0dSbrendan l2arc_dev_t *first, *next = NULL; 66503a737e0dSbrendan 66513a737e0dSbrendan /* 66523a737e0dSbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 66533a737e0dSbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 66543a737e0dSbrendan * both locks will be dropped and a spa config lock held instead. 66553a737e0dSbrendan */ 66563a737e0dSbrendan mutex_enter(&spa_namespace_lock); 66573a737e0dSbrendan mutex_enter(&l2arc_dev_mtx); 6658fa94a07fSbrendan 6659c5904d13Seschrock /* if there are no vdevs, there is nothing to do */ 6660c5904d13Seschrock if (l2arc_ndev == 0) 66613a737e0dSbrendan goto out; 6662c5904d13Seschrock 6663c5904d13Seschrock first = NULL; 6664c5904d13Seschrock next = l2arc_dev_last; 6665c5904d13Seschrock do { 6666c5904d13Seschrock /* loop around the list looking for a non-faulted vdev */ 6667c5904d13Seschrock if (next == NULL) { 6668fa94a07fSbrendan next = list_head(l2arc_dev_list); 6669c5904d13Seschrock } else { 6670c5904d13Seschrock next = list_next(l2arc_dev_list, next); 6671c5904d13Seschrock if (next == NULL) 6672c5904d13Seschrock next = list_head(l2arc_dev_list); 6673c5904d13Seschrock } 6674c5904d13Seschrock 6675c5904d13Seschrock /* if we have come back to the start, bail out */ 6676c5904d13Seschrock if (first == NULL) 6677c5904d13Seschrock first = next; 6678c5904d13Seschrock else if (next == first) 6679c5904d13Seschrock break; 6680c5904d13Seschrock 6681c5904d13Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 6682c5904d13Seschrock 6683c5904d13Seschrock /* if we were unable to find any usable vdevs, return NULL */ 6684c5904d13Seschrock if (vdev_is_dead(next->l2ad_vdev)) 66853a737e0dSbrendan next = NULL; 6686fa94a07fSbrendan 6687fa94a07fSbrendan l2arc_dev_last = next; 6688fa94a07fSbrendan 66893a737e0dSbrendan out: 66903a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 66913a737e0dSbrendan 66923a737e0dSbrendan /* 66933a737e0dSbrendan * Grab the config lock to prevent the 'next' device from being 66943a737e0dSbrendan * removed while we are writing to it. 66953a737e0dSbrendan */ 66963a737e0dSbrendan if (next != NULL) 6697e14bb325SJeff Bonwick spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 66983a737e0dSbrendan mutex_exit(&spa_namespace_lock); 66993a737e0dSbrendan 6700fa94a07fSbrendan return (next); 6701fa94a07fSbrendan } 6702fa94a07fSbrendan 67033a737e0dSbrendan /* 67043a737e0dSbrendan * Free buffers that were tagged for destruction. 67053a737e0dSbrendan */ 67063a737e0dSbrendan static void 67073a737e0dSbrendan l2arc_do_free_on_write() 67083a737e0dSbrendan { 67093a737e0dSbrendan list_t *buflist; 67103a737e0dSbrendan l2arc_data_free_t *df, *df_prev; 67113a737e0dSbrendan 67123a737e0dSbrendan mutex_enter(&l2arc_free_on_write_mtx); 67133a737e0dSbrendan buflist = l2arc_free_on_write; 67143a737e0dSbrendan 67153a737e0dSbrendan for (df = list_tail(buflist); df; df = df_prev) { 67163a737e0dSbrendan df_prev = list_prev(buflist, df); 6717770499e1SDan Kimmel ASSERT3P(df->l2df_abd, !=, NULL); 6718770499e1SDan Kimmel abd_free(df->l2df_abd); 67193a737e0dSbrendan list_remove(buflist, df); 67203a737e0dSbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 67213a737e0dSbrendan } 67223a737e0dSbrendan 67233a737e0dSbrendan mutex_exit(&l2arc_free_on_write_mtx); 67243a737e0dSbrendan } 67253a737e0dSbrendan 6726fa94a07fSbrendan /* 6727fa94a07fSbrendan * A write to a cache device has completed. Update all headers to allow 6728fa94a07fSbrendan * reads from these buffers to begin. 6729fa94a07fSbrendan */ 6730fa94a07fSbrendan static void 6731fa94a07fSbrendan l2arc_write_done(zio_t *zio) 6732fa94a07fSbrendan { 6733fa94a07fSbrendan l2arc_write_callback_t *cb; 6734fa94a07fSbrendan l2arc_dev_t *dev; 6735fa94a07fSbrendan list_t *buflist; 67367adb730bSGeorge Wilson arc_buf_hdr_t *head, *hdr, *hdr_prev; 6737fa94a07fSbrendan kmutex_t *hash_lock; 67383038a2b4SSaso Kiselkov int64_t bytes_dropped = 0; 6739fa94a07fSbrendan 6740fa94a07fSbrendan cb = zio->io_private; 6741dcbf3bd6SGeorge Wilson ASSERT3P(cb, !=, NULL); 6742fa94a07fSbrendan dev = cb->l2wcb_dev; 6743dcbf3bd6SGeorge Wilson ASSERT3P(dev, !=, NULL); 6744fa94a07fSbrendan head = cb->l2wcb_head; 6745dcbf3bd6SGeorge Wilson ASSERT3P(head, !=, NULL); 674689c86e32SChris Williamson buflist = &dev->l2ad_buflist; 6747dcbf3bd6SGeorge Wilson ASSERT3P(buflist, !=, NULL); 6748fa94a07fSbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 6749fa94a07fSbrendan l2arc_write_callback_t *, cb); 6750fa94a07fSbrendan 6751fa94a07fSbrendan if (zio->io_error != 0) 6752fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 6753fa94a07fSbrendan 6754fa94a07fSbrendan /* 6755fa94a07fSbrendan * All writes completed, or an error was hit. 6756fa94a07fSbrendan */ 6757244781f1SPrakash Surya top: 6758244781f1SPrakash Surya mutex_enter(&dev->l2ad_mtx); 67597adb730bSGeorge Wilson for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { 67607adb730bSGeorge Wilson hdr_prev = list_prev(buflist, hdr); 6761fa94a07fSbrendan 67627adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 6763244781f1SPrakash Surya 6764244781f1SPrakash Surya /* 6765244781f1SPrakash Surya * We cannot use mutex_enter or else we can deadlock 6766244781f1SPrakash Surya * with l2arc_write_buffers (due to swapping the order 6767244781f1SPrakash Surya * the hash lock and l2ad_mtx are taken). 6768244781f1SPrakash Surya */ 6769fa94a07fSbrendan if (!mutex_tryenter(hash_lock)) { 6770fa94a07fSbrendan /* 6771244781f1SPrakash Surya * Missed the hash lock. We must retry so we 6772244781f1SPrakash Surya * don't leave the ARC_FLAG_L2_WRITING bit set. 6773fa94a07fSbrendan */ 6774244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); 6775244781f1SPrakash Surya 6776244781f1SPrakash Surya /* 6777244781f1SPrakash Surya * We don't want to rescan the headers we've 6778244781f1SPrakash Surya * already marked as having been written out, so 6779244781f1SPrakash Surya * we reinsert the head node so we can pick up 6780244781f1SPrakash Surya * where we left off. 6781244781f1SPrakash Surya */ 6782244781f1SPrakash Surya list_remove(buflist, head); 6783244781f1SPrakash Surya list_insert_after(buflist, hdr, head); 6784244781f1SPrakash Surya 6785244781f1SPrakash Surya mutex_exit(&dev->l2ad_mtx); 6786244781f1SPrakash Surya 6787244781f1SPrakash Surya /* 6788244781f1SPrakash Surya * We wait for the hash lock to become available 6789244781f1SPrakash Surya * to try and prevent busy waiting, and increase 6790244781f1SPrakash Surya * the chance we'll be able to acquire the lock 6791244781f1SPrakash Surya * the next time around. 6792244781f1SPrakash Surya */ 6793244781f1SPrakash Surya mutex_enter(hash_lock); 6794244781f1SPrakash Surya mutex_exit(hash_lock); 6795244781f1SPrakash Surya goto top; 6796fa94a07fSbrendan } 6797fa94a07fSbrendan 679889c86e32SChris Williamson /* 6799244781f1SPrakash Surya * We could not have been moved into the arc_l2c_only 6800244781f1SPrakash Surya * state while in-flight due to our ARC_FLAG_L2_WRITING 6801244781f1SPrakash Surya * bit being set. Let's just ensure that's being enforced. 6802244781f1SPrakash Surya */ 6803244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 6804244781f1SPrakash Surya 6805fa94a07fSbrendan if (zio->io_error != 0) { 6806fa94a07fSbrendan /* 68073a737e0dSbrendan * Error - drop L2ARC entry. 6808fa94a07fSbrendan */ 68097adb730bSGeorge Wilson list_remove(buflist, hdr); 6810dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); 681189c86e32SChris Williamson 681216a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_psize, -arc_hdr_size(hdr)); 681316a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_lsize, -HDR_GET_LSIZE(hdr)); 6814a52fc310SPrakash Surya 6815dcbf3bd6SGeorge Wilson bytes_dropped += arc_hdr_size(hdr); 6816e914ace2STim Schumacher (void) zfs_refcount_remove_many(&dev->l2ad_alloc, 6817dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 6818fa94a07fSbrendan } 6819fa94a07fSbrendan 6820fa94a07fSbrendan /* 6821244781f1SPrakash Surya * Allow ARC to begin reads and ghost list evictions to 6822244781f1SPrakash Surya * this L2ARC entry. 6823fa94a07fSbrendan */ 6824dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING); 6825fa94a07fSbrendan 6826fa94a07fSbrendan mutex_exit(hash_lock); 6827fa94a07fSbrendan } 6828fa94a07fSbrendan 6829fa94a07fSbrendan atomic_inc_64(&l2arc_writes_done); 6830fa94a07fSbrendan list_remove(buflist, head); 683189c86e32SChris Williamson ASSERT(!HDR_HAS_L1HDR(head)); 683289c86e32SChris Williamson kmem_cache_free(hdr_l2only_cache, head); 683389c86e32SChris Williamson mutex_exit(&dev->l2ad_mtx); 6834fa94a07fSbrendan 68353038a2b4SSaso Kiselkov vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 68363038a2b4SSaso Kiselkov 68373a737e0dSbrendan l2arc_do_free_on_write(); 6838fa94a07fSbrendan 6839fa94a07fSbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 6840fa94a07fSbrendan } 6841fa94a07fSbrendan 6842fa94a07fSbrendan /* 6843fa94a07fSbrendan * A read to a cache device completed. Validate buffer contents before 6844fa94a07fSbrendan * handing over to the regular ARC routines. 6845fa94a07fSbrendan */ 6846fa94a07fSbrendan static void 6847fa94a07fSbrendan l2arc_read_done(zio_t *zio) 6848fa94a07fSbrendan { 6849fa94a07fSbrendan l2arc_read_callback_t *cb; 6850fa94a07fSbrendan arc_buf_hdr_t *hdr; 6851fa94a07fSbrendan kmutex_t *hash_lock; 6852dcbf3bd6SGeorge Wilson boolean_t valid_cksum; 6853fa94a07fSbrendan 6854dcbf3bd6SGeorge Wilson ASSERT3P(zio->io_vd, !=, NULL); 6855e14bb325SJeff Bonwick ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 6856e14bb325SJeff Bonwick 6857e14bb325SJeff Bonwick spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 6858e14bb325SJeff Bonwick 6859fa94a07fSbrendan cb = zio->io_private; 6860dcbf3bd6SGeorge Wilson ASSERT3P(cb, !=, NULL); 6861dcbf3bd6SGeorge Wilson hdr = cb->l2rcb_hdr; 6862dcbf3bd6SGeorge Wilson ASSERT3P(hdr, !=, NULL); 6863fa94a07fSbrendan 6864dcbf3bd6SGeorge Wilson hash_lock = HDR_LOCK(hdr); 6865fa94a07fSbrendan mutex_enter(hash_lock); 68663f9d6ad7SLin Ling ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 6867fa94a07fSbrendan 6868403a8da7SAndriy Gapon /* 6869403a8da7SAndriy Gapon * If the data was read into a temporary buffer, 6870403a8da7SAndriy Gapon * move it and free the buffer. 6871403a8da7SAndriy Gapon */ 6872403a8da7SAndriy Gapon if (cb->l2rcb_abd != NULL) { 6873403a8da7SAndriy Gapon ASSERT3U(arc_hdr_size(hdr), <, zio->io_size); 6874403a8da7SAndriy Gapon if (zio->io_error == 0) { 6875403a8da7SAndriy Gapon abd_copy(hdr->b_l1hdr.b_pabd, cb->l2rcb_abd, 6876403a8da7SAndriy Gapon arc_hdr_size(hdr)); 6877403a8da7SAndriy Gapon } 6878403a8da7SAndriy Gapon 6879403a8da7SAndriy Gapon /* 6880403a8da7SAndriy Gapon * The following must be done regardless of whether 6881403a8da7SAndriy Gapon * there was an error: 6882403a8da7SAndriy Gapon * - free the temporary buffer 6883403a8da7SAndriy Gapon * - point zio to the real ARC buffer 6884403a8da7SAndriy Gapon * - set zio size accordingly 6885403a8da7SAndriy Gapon * These are required because zio is either re-used for 6886403a8da7SAndriy Gapon * an I/O of the block in the case of the error 6887403a8da7SAndriy Gapon * or the zio is passed to arc_read_done() and it 6888403a8da7SAndriy Gapon * needs real data. 6889403a8da7SAndriy Gapon */ 6890403a8da7SAndriy Gapon abd_free(cb->l2rcb_abd); 6891403a8da7SAndriy Gapon zio->io_size = zio->io_orig_size = arc_hdr_size(hdr); 6892403a8da7SAndriy Gapon zio->io_abd = zio->io_orig_abd = hdr->b_l1hdr.b_pabd; 6893403a8da7SAndriy Gapon } 6894403a8da7SAndriy Gapon 6895770499e1SDan Kimmel ASSERT3P(zio->io_abd, !=, NULL); 6896aad02571SSaso Kiselkov 6897fa94a07fSbrendan /* 6898fa94a07fSbrendan * Check this survived the L2ARC journey. 6899fa94a07fSbrendan */ 6900770499e1SDan Kimmel ASSERT3P(zio->io_abd, ==, hdr->b_l1hdr.b_pabd); 6901dcbf3bd6SGeorge Wilson zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 6902dcbf3bd6SGeorge Wilson zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 6903dcbf3bd6SGeorge Wilson 6904dcbf3bd6SGeorge Wilson valid_cksum = arc_cksum_is_equal(hdr, zio); 6905dcbf3bd6SGeorge Wilson if (valid_cksum && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 6906fa94a07fSbrendan mutex_exit(hash_lock); 6907dcbf3bd6SGeorge Wilson zio->io_private = hdr; 6908fa94a07fSbrendan arc_read_done(zio); 6909fa94a07fSbrendan } else { 6910fa94a07fSbrendan mutex_exit(hash_lock); 6911fa94a07fSbrendan /* 6912fa94a07fSbrendan * Buffer didn't survive caching. Increment stats and 6913fa94a07fSbrendan * reissue to the original storage device. 6914fa94a07fSbrendan */ 69153a737e0dSbrendan if (zio->io_error != 0) { 6916fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 69173a737e0dSbrendan } else { 6918be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(EIO); 69193a737e0dSbrendan } 6920dcbf3bd6SGeorge Wilson if (!valid_cksum) 6921fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 6922fa94a07fSbrendan 6923e14bb325SJeff Bonwick /* 6924e14bb325SJeff Bonwick * If there's no waiter, issue an async i/o to the primary 6925e14bb325SJeff Bonwick * storage now. If there *is* a waiter, the caller must 6926e14bb325SJeff Bonwick * issue the i/o in a context where it's OK to block. 6927e14bb325SJeff Bonwick */ 6928a3f829aeSBill Moore if (zio->io_waiter == NULL) { 6929a3f829aeSBill Moore zio_t *pio = zio_unique_parent(zio); 6930a3f829aeSBill Moore 6931a3f829aeSBill Moore ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 6932a3f829aeSBill Moore 6933dcbf3bd6SGeorge Wilson zio_nowait(zio_read(pio, zio->io_spa, zio->io_bp, 6934770499e1SDan Kimmel hdr->b_l1hdr.b_pabd, zio->io_size, arc_read_done, 6935dcbf3bd6SGeorge Wilson hdr, zio->io_priority, cb->l2rcb_flags, 6936dcbf3bd6SGeorge Wilson &cb->l2rcb_zb)); 6937a3f829aeSBill Moore } 6938fa94a07fSbrendan } 6939fa94a07fSbrendan 6940fa94a07fSbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 6941fa94a07fSbrendan } 6942fa94a07fSbrendan 6943fa94a07fSbrendan /* 6944fa94a07fSbrendan * This is the list priority from which the L2ARC will search for pages to 6945fa94a07fSbrendan * cache. This is used within loops (0..3) to cycle through lists in the 6946fa94a07fSbrendan * desired order. This order can have a significant effect on cache 6947fa94a07fSbrendan * performance. 6948fa94a07fSbrendan * 6949fa94a07fSbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 6950fa94a07fSbrendan * the data lists. This function returns a locked list, and also returns 6951fa94a07fSbrendan * the lock pointer. 6952fa94a07fSbrendan */ 6953244781f1SPrakash Surya static multilist_sublist_t * 6954244781f1SPrakash Surya l2arc_sublist_lock(int list_num) 6955fa94a07fSbrendan { 6956244781f1SPrakash Surya multilist_t *ml = NULL; 6957244781f1SPrakash Surya unsigned int idx; 6958fa94a07fSbrendan 6959fa94a07fSbrendan ASSERT(list_num >= 0 && list_num <= 3); 6960fa94a07fSbrendan 6961fa94a07fSbrendan switch (list_num) { 6962fa94a07fSbrendan case 0: 696394c2d0ebSMatthew Ahrens ml = arc_mfu->arcs_list[ARC_BUFC_METADATA]; 6964fa94a07fSbrendan break; 6965fa94a07fSbrendan case 1: 696694c2d0ebSMatthew Ahrens ml = arc_mru->arcs_list[ARC_BUFC_METADATA]; 6967fa94a07fSbrendan break; 6968fa94a07fSbrendan case 2: 696994c2d0ebSMatthew Ahrens ml = arc_mfu->arcs_list[ARC_BUFC_DATA]; 6970fa94a07fSbrendan break; 6971fa94a07fSbrendan case 3: 697294c2d0ebSMatthew Ahrens ml = arc_mru->arcs_list[ARC_BUFC_DATA]; 6973fa94a07fSbrendan break; 6974fa94a07fSbrendan } 6975fa94a07fSbrendan 6976244781f1SPrakash Surya /* 6977244781f1SPrakash Surya * Return a randomly-selected sublist. This is acceptable 6978244781f1SPrakash Surya * because the caller feeds only a little bit of data for each 6979244781f1SPrakash Surya * call (8MB). Subsequent calls will result in different 6980244781f1SPrakash Surya * sublists being selected. 6981244781f1SPrakash Surya */ 6982244781f1SPrakash Surya idx = multilist_get_random_index(ml); 6983244781f1SPrakash Surya return (multilist_sublist_lock(ml, idx)); 6984fa94a07fSbrendan } 6985fa94a07fSbrendan 6986fa94a07fSbrendan /* 6987fa94a07fSbrendan * Evict buffers from the device write hand to the distance specified in 6988fa94a07fSbrendan * bytes. This distance may span populated buffers, it may span nothing. 6989fa94a07fSbrendan * This is clearing a region on the L2ARC device ready for writing. 6990fa94a07fSbrendan * If the 'all' boolean is set, every buffer is evicted. 6991fa94a07fSbrendan */ 6992fa94a07fSbrendan static void 6993fa94a07fSbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 6994fa94a07fSbrendan { 6995fa94a07fSbrendan list_t *buflist; 69967adb730bSGeorge Wilson arc_buf_hdr_t *hdr, *hdr_prev; 6997fa94a07fSbrendan kmutex_t *hash_lock; 6998fa94a07fSbrendan uint64_t taddr; 6999fa94a07fSbrendan 700089c86e32SChris Williamson buflist = &dev->l2ad_buflist; 7001fa94a07fSbrendan 7002fa94a07fSbrendan if (!all && dev->l2ad_first) { 7003fa94a07fSbrendan /* 7004fa94a07fSbrendan * This is the first sweep through the device. There is 7005fa94a07fSbrendan * nothing to evict. 7006fa94a07fSbrendan */ 7007fa94a07fSbrendan return; 7008fa94a07fSbrendan } 7009fa94a07fSbrendan 70103a737e0dSbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 7011fa94a07fSbrendan /* 7012fa94a07fSbrendan * When nearing the end of the device, evict to the end 7013fa94a07fSbrendan * before the device write hand jumps to the start. 7014fa94a07fSbrendan */ 7015fa94a07fSbrendan taddr = dev->l2ad_end; 7016fa94a07fSbrendan } else { 7017fa94a07fSbrendan taddr = dev->l2ad_hand + distance; 7018fa94a07fSbrendan } 7019fa94a07fSbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 7020fa94a07fSbrendan uint64_t, taddr, boolean_t, all); 7021fa94a07fSbrendan 7022fa94a07fSbrendan top: 702389c86e32SChris Williamson mutex_enter(&dev->l2ad_mtx); 70247adb730bSGeorge Wilson for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { 70257adb730bSGeorge Wilson hdr_prev = list_prev(buflist, hdr); 7026fa94a07fSbrendan 70277adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 7028244781f1SPrakash Surya 7029244781f1SPrakash Surya /* 7030244781f1SPrakash Surya * We cannot use mutex_enter or else we can deadlock 7031244781f1SPrakash Surya * with l2arc_write_buffers (due to swapping the order 7032244781f1SPrakash Surya * the hash lock and l2ad_mtx are taken). 7033244781f1SPrakash Surya */ 7034fa94a07fSbrendan if (!mutex_tryenter(hash_lock)) { 7035fa94a07fSbrendan /* 7036fa94a07fSbrendan * Missed the hash lock. Retry. 7037fa94a07fSbrendan */ 7038fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 703989c86e32SChris Williamson mutex_exit(&dev->l2ad_mtx); 7040fa94a07fSbrendan mutex_enter(hash_lock); 7041fa94a07fSbrendan mutex_exit(hash_lock); 7042fa94a07fSbrendan goto top; 7043fa94a07fSbrendan } 7044fa94a07fSbrendan 7045267ae6c3SAndriy Gapon /* 7046267ae6c3SAndriy Gapon * A header can't be on this list if it doesn't have L2 header. 7047267ae6c3SAndriy Gapon */ 7048267ae6c3SAndriy Gapon ASSERT(HDR_HAS_L2HDR(hdr)); 7049fa94a07fSbrendan 7050267ae6c3SAndriy Gapon /* Ensure this header has finished being written. */ 7051267ae6c3SAndriy Gapon ASSERT(!HDR_L2_WRITING(hdr)); 7052267ae6c3SAndriy Gapon ASSERT(!HDR_L2_WRITE_HEAD(hdr)); 7053267ae6c3SAndriy Gapon 7054267ae6c3SAndriy Gapon if (!all && (hdr->b_l2hdr.b_daddr >= taddr || 705589c86e32SChris Williamson hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { 7056fa94a07fSbrendan /* 7057fa94a07fSbrendan * We've evicted to the target address, 7058fa94a07fSbrendan * or the end of the device. 7059fa94a07fSbrendan */ 7060fa94a07fSbrendan mutex_exit(hash_lock); 7061fa94a07fSbrendan break; 7062fa94a07fSbrendan } 7063fa94a07fSbrendan 706489c86e32SChris Williamson if (!HDR_HAS_L1HDR(hdr)) { 70657adb730bSGeorge Wilson ASSERT(!HDR_L2_READING(hdr)); 7066fa94a07fSbrendan /* 7067fa94a07fSbrendan * This doesn't exist in the ARC. Destroy. 7068fa94a07fSbrendan * arc_hdr_destroy() will call list_remove() 706916a7e5acSAndriy Gapon * and decrement arcstat_l2_lsize. 7070fa94a07fSbrendan */ 70717adb730bSGeorge Wilson arc_change_state(arc_anon, hdr, hash_lock); 70727adb730bSGeorge Wilson arc_hdr_destroy(hdr); 7073fa94a07fSbrendan } else { 707489c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); 707589c86e32SChris Williamson ARCSTAT_BUMP(arcstat_l2_evict_l1cached); 70763a737e0dSbrendan /* 70773a737e0dSbrendan * Invalidate issued or about to be issued 70783a737e0dSbrendan * reads, since we may be about to write 70793a737e0dSbrendan * over this location. 70803a737e0dSbrendan */ 70817adb730bSGeorge Wilson if (HDR_L2_READING(hdr)) { 70823a737e0dSbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 7083dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED); 70843a737e0dSbrendan } 70853a737e0dSbrendan 7086a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(hdr); 7087fa94a07fSbrendan } 7088fa94a07fSbrendan mutex_exit(hash_lock); 7089fa94a07fSbrendan } 709089c86e32SChris Williamson mutex_exit(&dev->l2ad_mtx); 7091fa94a07fSbrendan } 7092fa94a07fSbrendan 7093fa94a07fSbrendan /* 7094fa94a07fSbrendan * Find and write ARC buffers to the L2ARC device. 7095fa94a07fSbrendan * 70967adb730bSGeorge Wilson * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid 7097fa94a07fSbrendan * for reading until they have completed writing. 7098aad02571SSaso Kiselkov * The headroom_boost is an in-out parameter used to maintain headroom boost 7099aad02571SSaso Kiselkov * state between calls to this function. 7100aad02571SSaso Kiselkov * 7101aad02571SSaso Kiselkov * Returns the number of bytes actually written (which may be smaller than 7102aad02571SSaso Kiselkov * the delta by which the device hand has changed due to alignment). 7103fa94a07fSbrendan */ 71045a98e54bSBrendan Gregg - Sun Microsystems static uint64_t 7105dcbf3bd6SGeorge Wilson l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 7106fa94a07fSbrendan { 71077adb730bSGeorge Wilson arc_buf_hdr_t *hdr, *hdr_prev, *head; 710816a7e5acSAndriy Gapon uint64_t write_asize, write_psize, write_lsize, headroom; 7109aad02571SSaso Kiselkov boolean_t full; 7110fa94a07fSbrendan l2arc_write_callback_t *cb; 7111fa94a07fSbrendan zio_t *pio, *wzio; 7112e9103aaeSGarrett D'Amore uint64_t guid = spa_load_guid(spa); 7113fa94a07fSbrendan 7114dcbf3bd6SGeorge Wilson ASSERT3P(dev->l2ad_vdev, !=, NULL); 7115aad02571SSaso Kiselkov 7116fa94a07fSbrendan pio = NULL; 711716a7e5acSAndriy Gapon write_lsize = write_asize = write_psize = 0; 7118fa94a07fSbrendan full = B_FALSE; 711989c86e32SChris Williamson head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); 7120dcbf3bd6SGeorge Wilson arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR); 7121aad02571SSaso Kiselkov 7122fa94a07fSbrendan /* 7123fa94a07fSbrendan * Copy buffers for L2ARC writing. 7124fa94a07fSbrendan */ 7125fa94a07fSbrendan for (int try = 0; try <= 3; try++) { 7126244781f1SPrakash Surya multilist_sublist_t *mls = l2arc_sublist_lock(try); 7127aad02571SSaso Kiselkov uint64_t passed_sz = 0; 7128aad02571SSaso Kiselkov 71293a737e0dSbrendan /* 71303a737e0dSbrendan * L2ARC fast warmup. 71313a737e0dSbrendan * 71323a737e0dSbrendan * Until the ARC is warm and starts to evict, read from the 71333a737e0dSbrendan * head of the ARC lists rather than the tail. 71343a737e0dSbrendan */ 71353a737e0dSbrendan if (arc_warm == B_FALSE) 7136244781f1SPrakash Surya hdr = multilist_sublist_head(mls); 71373a737e0dSbrendan else 7138244781f1SPrakash Surya hdr = multilist_sublist_tail(mls); 71393a737e0dSbrendan 7140aad02571SSaso Kiselkov headroom = target_sz * l2arc_headroom; 7141dcbf3bd6SGeorge Wilson if (zfs_compressed_arc_enabled) 7142aad02571SSaso Kiselkov headroom = (headroom * l2arc_headroom_boost) / 100; 7143aad02571SSaso Kiselkov 71447adb730bSGeorge Wilson for (; hdr; hdr = hdr_prev) { 7145aad02571SSaso Kiselkov kmutex_t *hash_lock; 7146aad02571SSaso Kiselkov 71473a737e0dSbrendan if (arc_warm == B_FALSE) 7148244781f1SPrakash Surya hdr_prev = multilist_sublist_next(mls, hdr); 71493a737e0dSbrendan else 7150244781f1SPrakash Surya hdr_prev = multilist_sublist_prev(mls, hdr); 7151fa94a07fSbrendan 71527adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 7153aad02571SSaso Kiselkov if (!mutex_tryenter(hash_lock)) { 7154fa94a07fSbrendan /* 7155fa94a07fSbrendan * Skip this buffer rather than waiting. 7156fa94a07fSbrendan */ 7157fa94a07fSbrendan continue; 7158fa94a07fSbrendan } 7159fa94a07fSbrendan 7160dcbf3bd6SGeorge Wilson passed_sz += HDR_GET_LSIZE(hdr); 7161fa94a07fSbrendan if (passed_sz > headroom) { 7162fa94a07fSbrendan /* 7163fa94a07fSbrendan * Searched too far. 7164fa94a07fSbrendan */ 7165fa94a07fSbrendan mutex_exit(hash_lock); 7166fa94a07fSbrendan break; 7167fa94a07fSbrendan } 7168fa94a07fSbrendan 71697adb730bSGeorge Wilson if (!l2arc_write_eligible(guid, hdr)) { 7170fa94a07fSbrendan mutex_exit(hash_lock); 7171fa94a07fSbrendan continue; 7172fa94a07fSbrendan } 7173fa94a07fSbrendan 717416a7e5acSAndriy Gapon /* 717516a7e5acSAndriy Gapon * We rely on the L1 portion of the header below, so 717616a7e5acSAndriy Gapon * it's invalid for this header to have been evicted out 717716a7e5acSAndriy Gapon * of the ghost cache, prior to being written out. The 717816a7e5acSAndriy Gapon * ARC_FLAG_L2_WRITING bit ensures this won't happen. 717916a7e5acSAndriy Gapon */ 718016a7e5acSAndriy Gapon ASSERT(HDR_HAS_L1HDR(hdr)); 718116a7e5acSAndriy Gapon 718216a7e5acSAndriy Gapon ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); 718316a7e5acSAndriy Gapon ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 718416a7e5acSAndriy Gapon ASSERT3U(arc_hdr_size(hdr), >, 0); 718516a7e5acSAndriy Gapon uint64_t psize = arc_hdr_size(hdr); 718616a7e5acSAndriy Gapon uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, 718716a7e5acSAndriy Gapon psize); 718816a7e5acSAndriy Gapon 718916a7e5acSAndriy Gapon if ((write_asize + asize) > target_sz) { 7190fa94a07fSbrendan full = B_TRUE; 7191fa94a07fSbrendan mutex_exit(hash_lock); 7192fa94a07fSbrendan break; 7193fa94a07fSbrendan } 7194fa94a07fSbrendan 7195fa94a07fSbrendan if (pio == NULL) { 7196fa94a07fSbrendan /* 7197fa94a07fSbrendan * Insert a dummy header on the buflist so 7198fa94a07fSbrendan * l2arc_write_done() can find where the 7199fa94a07fSbrendan * write buffers begin without searching. 7200fa94a07fSbrendan */ 7201244781f1SPrakash Surya mutex_enter(&dev->l2ad_mtx); 720289c86e32SChris Williamson list_insert_head(&dev->l2ad_buflist, head); 7203244781f1SPrakash Surya mutex_exit(&dev->l2ad_mtx); 7204fa94a07fSbrendan 7205fa94a07fSbrendan cb = kmem_alloc( 7206fa94a07fSbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 7207fa94a07fSbrendan cb->l2wcb_dev = dev; 7208fa94a07fSbrendan cb->l2wcb_head = head; 7209fa94a07fSbrendan pio = zio_root(spa, l2arc_write_done, cb, 7210fa94a07fSbrendan ZIO_FLAG_CANFAIL); 7211fa94a07fSbrendan } 7212fa94a07fSbrendan 721389c86e32SChris Williamson hdr->b_l2hdr.b_dev = dev; 7214dcbf3bd6SGeorge Wilson hdr->b_l2hdr.b_daddr = dev->l2ad_hand; 7215dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, 7216dcbf3bd6SGeorge Wilson ARC_FLAG_L2_WRITING | ARC_FLAG_HAS_L2HDR); 7217dcbf3bd6SGeorge Wilson 7218dcbf3bd6SGeorge Wilson mutex_enter(&dev->l2ad_mtx); 7219dcbf3bd6SGeorge Wilson list_insert_head(&dev->l2ad_buflist, hdr); 7220dcbf3bd6SGeorge Wilson mutex_exit(&dev->l2ad_mtx); 7221dcbf3bd6SGeorge Wilson 7222e914ace2STim Schumacher (void) zfs_refcount_add_many(&dev->l2ad_alloc, psize, 7223e914ace2STim Schumacher hdr); 7224aad02571SSaso Kiselkov 7225a52fc310SPrakash Surya /* 7226dcbf3bd6SGeorge Wilson * Normally the L2ARC can use the hdr's data, but if 7227dcbf3bd6SGeorge Wilson * we're sharing data between the hdr and one of its 7228dcbf3bd6SGeorge Wilson * bufs, L2ARC needs its own copy of the data so that 7229403a8da7SAndriy Gapon * the ZIO below can't race with the buf consumer. 7230403a8da7SAndriy Gapon * Another case where we need to create a copy of the 7231403a8da7SAndriy Gapon * data is when the buffer size is not device-aligned 7232403a8da7SAndriy Gapon * and we need to pad the block to make it such. 7233403a8da7SAndriy Gapon * That also keeps the clock hand suitably aligned. 7234403a8da7SAndriy Gapon * 7235403a8da7SAndriy Gapon * To ensure that the copy will be available for the 7236dcbf3bd6SGeorge Wilson * lifetime of the ZIO and be cleaned up afterwards, we 7237dcbf3bd6SGeorge Wilson * add it to the l2arc_free_on_write queue. 7238a52fc310SPrakash Surya */ 7239770499e1SDan Kimmel abd_t *to_write; 724016a7e5acSAndriy Gapon if (!HDR_SHARED_DATA(hdr) && psize == asize) { 7241770499e1SDan Kimmel to_write = hdr->b_l1hdr.b_pabd; 7242dcbf3bd6SGeorge Wilson } else { 7243403a8da7SAndriy Gapon to_write = abd_alloc_for_io(asize, 7244770499e1SDan Kimmel HDR_ISTYPE_METADATA(hdr)); 724516a7e5acSAndriy Gapon abd_copy(to_write, hdr->b_l1hdr.b_pabd, psize); 724616a7e5acSAndriy Gapon if (asize != psize) { 724716a7e5acSAndriy Gapon abd_zero_off(to_write, psize, 724816a7e5acSAndriy Gapon asize - psize); 7249403a8da7SAndriy Gapon } 725016a7e5acSAndriy Gapon l2arc_free_abd_on_write(to_write, asize, 7251770499e1SDan Kimmel arc_buf_type(hdr)); 7252dcbf3bd6SGeorge Wilson } 7253dcbf3bd6SGeorge Wilson wzio = zio_write_phys(pio, dev->l2ad_vdev, 7254403a8da7SAndriy Gapon hdr->b_l2hdr.b_daddr, asize, to_write, 7255dcbf3bd6SGeorge Wilson ZIO_CHECKSUM_OFF, NULL, hdr, 7256dcbf3bd6SGeorge Wilson ZIO_PRIORITY_ASYNC_WRITE, 7257dcbf3bd6SGeorge Wilson ZIO_FLAG_CANFAIL, B_FALSE); 7258aad02571SSaso Kiselkov 725916a7e5acSAndriy Gapon write_lsize += HDR_GET_LSIZE(hdr); 7260dcbf3bd6SGeorge Wilson DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 7261dcbf3bd6SGeorge Wilson zio_t *, wzio); 7262fa94a07fSbrendan 726316a7e5acSAndriy Gapon write_psize += psize; 726416a7e5acSAndriy Gapon write_asize += asize; 7265dcbf3bd6SGeorge Wilson dev->l2ad_hand += asize; 7266fa94a07fSbrendan 7267fa94a07fSbrendan mutex_exit(hash_lock); 7268fa94a07fSbrendan 7269dcbf3bd6SGeorge Wilson (void) zio_nowait(wzio); 7270aad02571SSaso Kiselkov } 7271aad02571SSaso Kiselkov 7272244781f1SPrakash Surya multilist_sublist_unlock(mls); 7273aad02571SSaso Kiselkov 7274aad02571SSaso Kiselkov if (full == B_TRUE) 7275aad02571SSaso Kiselkov break; 7276aad02571SSaso Kiselkov } 7277aad02571SSaso Kiselkov 7278aad02571SSaso Kiselkov /* No buffers selected for writing? */ 7279aad02571SSaso Kiselkov if (pio == NULL) { 728016a7e5acSAndriy Gapon ASSERT0(write_lsize); 728189c86e32SChris Williamson ASSERT(!HDR_HAS_L1HDR(head)); 728289c86e32SChris Williamson kmem_cache_free(hdr_l2only_cache, head); 7283aad02571SSaso Kiselkov return (0); 7284aad02571SSaso Kiselkov } 7285aad02571SSaso Kiselkov 7286aad02571SSaso Kiselkov ASSERT3U(write_asize, <=, target_sz); 7287fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 728816a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_write_bytes, write_psize); 728916a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_lsize, write_lsize); 729016a7e5acSAndriy Gapon ARCSTAT_INCR(arcstat_l2_psize, write_psize); 729116a7e5acSAndriy Gapon vdev_space_update(dev->l2ad_vdev, write_psize, 0, 0); 7292fa94a07fSbrendan 7293fa94a07fSbrendan /* 7294fa94a07fSbrendan * Bump device hand to the device start if it is approaching the end. 7295fa94a07fSbrendan * l2arc_evict() will already have evicted ahead for this case. 7296fa94a07fSbrendan */ 72973a737e0dSbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 7298fa94a07fSbrendan dev->l2ad_hand = dev->l2ad_start; 7299fa94a07fSbrendan dev->l2ad_first = B_FALSE; 7300fa94a07fSbrendan } 7301fa94a07fSbrendan 73025a98e54bSBrendan Gregg - Sun Microsystems dev->l2ad_writing = B_TRUE; 7303fa94a07fSbrendan (void) zio_wait(pio); 73045a98e54bSBrendan Gregg - Sun Microsystems dev->l2ad_writing = B_FALSE; 73055a98e54bSBrendan Gregg - Sun Microsystems 7306aad02571SSaso Kiselkov return (write_asize); 7307aad02571SSaso Kiselkov } 7308aad02571SSaso Kiselkov 7309fa94a07fSbrendan /* 7310fa94a07fSbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 7311fa94a07fSbrendan * heart of the L2ARC. 7312fa94a07fSbrendan */ 73133f7978d0SAlan Somers /* ARGSUSED */ 7314fa94a07fSbrendan static void 73153f7978d0SAlan Somers l2arc_feed_thread(void *unused) 7316fa94a07fSbrendan { 7317fa94a07fSbrendan callb_cpr_t cpr; 7318fa94a07fSbrendan l2arc_dev_t *dev; 7319fa94a07fSbrendan spa_t *spa; 73205a98e54bSBrendan Gregg - Sun Microsystems uint64_t size, wrote; 7321d3d50737SRafael Vanoni clock_t begin, next = ddi_get_lbolt(); 7322fa94a07fSbrendan 7323fa94a07fSbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 7324fa94a07fSbrendan 7325fa94a07fSbrendan mutex_enter(&l2arc_feed_thr_lock); 7326fa94a07fSbrendan 7327fa94a07fSbrendan while (l2arc_thread_exit == 0) { 7328fa94a07fSbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 7329fa94a07fSbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 73305a98e54bSBrendan Gregg - Sun Microsystems next); 7331fa94a07fSbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 7332d3d50737SRafael Vanoni next = ddi_get_lbolt() + hz; 7333fa94a07fSbrendan 73343a737e0dSbrendan /* 73353a737e0dSbrendan * Quick check for L2ARC devices. 73363a737e0dSbrendan */ 7337c5904d13Seschrock mutex_enter(&l2arc_dev_mtx); 73383a737e0dSbrendan if (l2arc_ndev == 0) { 73393a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 73403a737e0dSbrendan continue; 73413a737e0dSbrendan } 73423a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 7343d3d50737SRafael Vanoni begin = ddi_get_lbolt(); 7344c5904d13Seschrock 7345fa94a07fSbrendan /* 7346c5904d13Seschrock * This selects the next l2arc device to write to, and in 7347c5904d13Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 73483a737e0dSbrendan * will return NULL if there are now no l2arc devices or if 73493a737e0dSbrendan * they are all faulted. 73503a737e0dSbrendan * 73513a737e0dSbrendan * If a device is returned, its spa's config lock is also 73523a737e0dSbrendan * held to prevent device removal. l2arc_dev_get_next() 73533a737e0dSbrendan * will grab and release l2arc_dev_mtx. 7354fa94a07fSbrendan */ 73553a737e0dSbrendan if ((dev = l2arc_dev_get_next()) == NULL) 7356fa94a07fSbrendan continue; 73573a737e0dSbrendan 73583a737e0dSbrendan spa = dev->l2ad_spa; 7359dcbf3bd6SGeorge Wilson ASSERT3P(spa, !=, NULL); 7360fa94a07fSbrendan 7361f9af39baSGeorge Wilson /* 7362f9af39baSGeorge Wilson * If the pool is read-only then force the feed thread to 7363f9af39baSGeorge Wilson * sleep a little longer. 7364f9af39baSGeorge Wilson */ 7365f9af39baSGeorge Wilson if (!spa_writeable(spa)) { 7366f9af39baSGeorge Wilson next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 7367f9af39baSGeorge Wilson spa_config_exit(spa, SCL_L2ARC, dev); 7368f9af39baSGeorge Wilson continue; 7369f9af39baSGeorge Wilson } 7370f9af39baSGeorge Wilson 7371fa94a07fSbrendan /* 7372fa94a07fSbrendan * Avoid contributing to memory pressure. 7373fa94a07fSbrendan */ 7374fa94a07fSbrendan if (arc_reclaim_needed()) { 7375fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 7376e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, dev); 7377fa94a07fSbrendan continue; 7378fa94a07fSbrendan } 7379fa94a07fSbrendan 7380fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 7381fa94a07fSbrendan 7382aad02571SSaso Kiselkov size = l2arc_write_size(); 73833a737e0dSbrendan 7384fa94a07fSbrendan /* 7385fa94a07fSbrendan * Evict L2ARC buffers that will be overwritten. 7386fa94a07fSbrendan */ 73873a737e0dSbrendan l2arc_evict(dev, size, B_FALSE); 7388fa94a07fSbrendan 7389fa94a07fSbrendan /* 7390fa94a07fSbrendan * Write ARC buffers. 7391fa94a07fSbrendan */ 7392dcbf3bd6SGeorge Wilson wrote = l2arc_write_buffers(spa, dev, size); 73935a98e54bSBrendan Gregg - Sun Microsystems 73945a98e54bSBrendan Gregg - Sun Microsystems /* 73955a98e54bSBrendan Gregg - Sun Microsystems * Calculate interval between writes. 73965a98e54bSBrendan Gregg - Sun Microsystems */ 73975a98e54bSBrendan Gregg - Sun Microsystems next = l2arc_write_interval(begin, size, wrote); 7398e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, dev); 7399fa94a07fSbrendan } 7400fa94a07fSbrendan 7401fa94a07fSbrendan l2arc_thread_exit = 0; 7402fa94a07fSbrendan cv_broadcast(&l2arc_feed_thr_cv); 7403fa94a07fSbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 7404fa94a07fSbrendan thread_exit(); 7405fa94a07fSbrendan } 7406fa94a07fSbrendan 7407c5904d13Seschrock boolean_t 7408c5904d13Seschrock l2arc_vdev_present(vdev_t *vd) 7409c5904d13Seschrock { 7410c5904d13Seschrock l2arc_dev_t *dev; 7411c5904d13Seschrock 7412c5904d13Seschrock mutex_enter(&l2arc_dev_mtx); 7413c5904d13Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 7414c5904d13Seschrock dev = list_next(l2arc_dev_list, dev)) { 7415c5904d13Seschrock if (dev->l2ad_vdev == vd) 7416c5904d13Seschrock break; 7417c5904d13Seschrock } 7418c5904d13Seschrock mutex_exit(&l2arc_dev_mtx); 7419c5904d13Seschrock 7420c5904d13Seschrock return (dev != NULL); 7421c5904d13Seschrock } 7422c5904d13Seschrock 7423fa94a07fSbrendan /* 7424fa94a07fSbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 7425fa94a07fSbrendan * validated the vdev and opened it. 7426fa94a07fSbrendan */ 7427fa94a07fSbrendan void 7428573ca77eSGeorge Wilson l2arc_add_vdev(spa_t *spa, vdev_t *vd) 7429fa94a07fSbrendan { 7430fa94a07fSbrendan l2arc_dev_t *adddev; 7431fa94a07fSbrendan 7432c5904d13Seschrock ASSERT(!l2arc_vdev_present(vd)); 7433c5904d13Seschrock 7434fa94a07fSbrendan /* 7435fa94a07fSbrendan * Create a new l2arc device entry. 7436fa94a07fSbrendan */ 7437fa94a07fSbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 7438fa94a07fSbrendan adddev->l2ad_spa = spa; 7439fa94a07fSbrendan adddev->l2ad_vdev = vd; 7440573ca77eSGeorge Wilson adddev->l2ad_start = VDEV_LABEL_START_SIZE; 7441573ca77eSGeorge Wilson adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 7442fa94a07fSbrendan adddev->l2ad_hand = adddev->l2ad_start; 7443fa94a07fSbrendan adddev->l2ad_first = B_TRUE; 74445a98e54bSBrendan Gregg - Sun Microsystems adddev->l2ad_writing = B_FALSE; 7445fa94a07fSbrendan 744689c86e32SChris Williamson mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); 7447fa94a07fSbrendan /* 7448fa94a07fSbrendan * This is a list of all ARC buffers that are still valid on the 7449fa94a07fSbrendan * device. 7450fa94a07fSbrendan */ 745189c86e32SChris Williamson list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 745289c86e32SChris Williamson offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); 7453fa94a07fSbrendan 7454b24ab676SJeff Bonwick vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 7455e914ace2STim Schumacher zfs_refcount_create(&adddev->l2ad_alloc); 7456fa94a07fSbrendan 7457fa94a07fSbrendan /* 7458fa94a07fSbrendan * Add device to global list 7459fa94a07fSbrendan */ 7460fa94a07fSbrendan mutex_enter(&l2arc_dev_mtx); 7461fa94a07fSbrendan list_insert_head(l2arc_dev_list, adddev); 7462fa94a07fSbrendan atomic_inc_64(&l2arc_ndev); 7463fa94a07fSbrendan mutex_exit(&l2arc_dev_mtx); 7464fa94a07fSbrendan } 7465fa94a07fSbrendan 7466fa94a07fSbrendan /* 7467fa94a07fSbrendan * Remove a vdev from the L2ARC. 7468fa94a07fSbrendan */ 7469fa94a07fSbrendan void 7470fa94a07fSbrendan l2arc_remove_vdev(vdev_t *vd) 7471fa94a07fSbrendan { 7472fa94a07fSbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 7473fa94a07fSbrendan 7474fa94a07fSbrendan /* 7475fa94a07fSbrendan * Find the device by vdev 7476fa94a07fSbrendan */ 7477fa94a07fSbrendan mutex_enter(&l2arc_dev_mtx); 7478fa94a07fSbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 7479fa94a07fSbrendan nextdev = list_next(l2arc_dev_list, dev); 7480fa94a07fSbrendan if (vd == dev->l2ad_vdev) { 7481fa94a07fSbrendan remdev = dev; 7482fa94a07fSbrendan break; 7483fa94a07fSbrendan } 7484fa94a07fSbrendan } 7485dcbf3bd6SGeorge Wilson ASSERT3P(remdev, !=, NULL); 7486fa94a07fSbrendan 7487fa94a07fSbrendan /* 7488fa94a07fSbrendan * Remove device from global list 7489fa94a07fSbrendan */ 7490fa94a07fSbrendan list_remove(l2arc_dev_list, remdev); 7491fa94a07fSbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 74923a737e0dSbrendan atomic_dec_64(&l2arc_ndev); 74933a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 7494fa94a07fSbrendan 7495fa94a07fSbrendan /* 7496fa94a07fSbrendan * Clear all buflists and ARC references. L2ARC device flush. 7497fa94a07fSbrendan */ 7498fa94a07fSbrendan l2arc_evict(remdev, 0, B_TRUE); 749989c86e32SChris Williamson list_destroy(&remdev->l2ad_buflist); 750089c86e32SChris Williamson mutex_destroy(&remdev->l2ad_mtx); 7501e914ace2STim Schumacher zfs_refcount_destroy(&remdev->l2ad_alloc); 7502fa94a07fSbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 7503fa94a07fSbrendan } 7504fa94a07fSbrendan 7505fa94a07fSbrendan void 7506e14bb325SJeff Bonwick l2arc_init(void) 7507fa94a07fSbrendan { 7508fa94a07fSbrendan l2arc_thread_exit = 0; 7509fa94a07fSbrendan l2arc_ndev = 0; 7510fa94a07fSbrendan l2arc_writes_sent = 0; 7511fa94a07fSbrendan l2arc_writes_done = 0; 7512fa94a07fSbrendan 7513fa94a07fSbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 7514fa94a07fSbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 7515fa94a07fSbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 7516fa94a07fSbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 7517fa94a07fSbrendan 7518fa94a07fSbrendan l2arc_dev_list = &L2ARC_dev_list; 7519fa94a07fSbrendan l2arc_free_on_write = &L2ARC_free_on_write; 7520fa94a07fSbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 7521fa94a07fSbrendan offsetof(l2arc_dev_t, l2ad_node)); 7522fa94a07fSbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 7523fa94a07fSbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 7524fa94a07fSbrendan } 7525fa94a07fSbrendan 7526fa94a07fSbrendan void 7527e14bb325SJeff Bonwick l2arc_fini(void) 7528fa94a07fSbrendan { 75293a737e0dSbrendan /* 75303a737e0dSbrendan * This is called from dmu_fini(), which is called from spa_fini(); 75313a737e0dSbrendan * Because of this, we can assume that all l2arc devices have 75323a737e0dSbrendan * already been removed when the pools themselves were removed. 75333a737e0dSbrendan */ 75343a737e0dSbrendan 75353a737e0dSbrendan l2arc_do_free_on_write(); 75363a737e0dSbrendan 7537fa94a07fSbrendan mutex_destroy(&l2arc_feed_thr_lock); 7538fa94a07fSbrendan cv_destroy(&l2arc_feed_thr_cv); 7539fa94a07fSbrendan mutex_destroy(&l2arc_dev_mtx); 7540fa94a07fSbrendan mutex_destroy(&l2arc_free_on_write_mtx); 7541fa94a07fSbrendan 7542fa94a07fSbrendan list_destroy(l2arc_dev_list); 7543fa94a07fSbrendan list_destroy(l2arc_free_on_write); 7544fa94a07fSbrendan } 7545e14bb325SJeff Bonwick 7546e14bb325SJeff Bonwick void 7547e14bb325SJeff Bonwick l2arc_start(void) 7548e14bb325SJeff Bonwick { 75498ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 7550e14bb325SJeff Bonwick return; 7551e14bb325SJeff Bonwick 7552e14bb325SJeff Bonwick (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 7553e14bb325SJeff Bonwick TS_RUN, minclsyspri); 7554e14bb325SJeff Bonwick } 7555e14bb325SJeff Bonwick 7556e14bb325SJeff Bonwick void 7557e14bb325SJeff Bonwick l2arc_stop(void) 7558e14bb325SJeff Bonwick { 75598ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 7560e14bb325SJeff Bonwick return; 7561e14bb325SJeff Bonwick 7562e14bb325SJeff Bonwick mutex_enter(&l2arc_feed_thr_lock); 7563e14bb325SJeff Bonwick cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 7564e14bb325SJeff Bonwick l2arc_thread_exit = 1; 7565e14bb325SJeff Bonwick while (l2arc_thread_exit != 0) 7566e14bb325SJeff Bonwick cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 7567e14bb325SJeff Bonwick mutex_exit(&l2arc_feed_thr_lock); 7568e14bb325SJeff Bonwick } 7569