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. 23cf746768SBryan Cantrill * Copyright (c) 2012, Joyent, Inc. All rights reserved. 248df0bcf0SPaul Dagnelie * Copyright (c) 2011, 2016 by Delphix. All rights reserved. 2571cb1b74SSaso Kiselkov * Copyright (c) 2014 by Saso Kiselkov. All rights reserved. 2631c46cf2SAlek Pinchuk * Copyright 2015 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 80fa9e4066Sahrens * 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 83fa9e4066Sahrens * 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 * 96fa9e4066Sahrens * Each arc state also has a mutex which is used to protect the 97fa9e4066Sahrens * buffer list associated with the state. When attempting to 98fa9e4066Sahrens * 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 * 102ea8dc4b6Seschrock * Arc buffers may have an associated eviction callback function. 103ea8dc4b6Seschrock * This function will be invoked prior to removing the buffer (e.g. 104ea8dc4b6Seschrock * in arc_do_user_evicts()). Note however that the data associated 105ea8dc4b6Seschrock * with the buffer may be evicted prior to the callback. The callback 106ea8dc4b6Seschrock * must be made with *no locks held* (to prevent deadlock). Additionally, 107ea8dc4b6Seschrock * the users of callbacks must ensure that their private data is 108bbfa8ea8SMatthew Ahrens * protected from simultaneous callbacks from arc_clear_callback() 109ea8dc4b6Seschrock * and arc_do_user_evicts(). 110ea8dc4b6Seschrock * 111fa9e4066Sahrens * Note that the majority of the performance stats are manipulated 112fa9e4066Sahrens * with atomic operations. 113fa94a07fSbrendan * 11489c86e32SChris Williamson * The L2ARC uses the l2ad_mtx on each vdev for the following: 115fa94a07fSbrendan * 116fa94a07fSbrendan * - L2ARC buflist creation 117fa94a07fSbrendan * - L2ARC buflist eviction 118fa94a07fSbrendan * - L2ARC write completion, which walks L2ARC buflists 119fa94a07fSbrendan * - ARC header destruction, as it removes from L2ARC buflists 120fa94a07fSbrendan * - ARC header release, as it removes from L2ARC buflists 121fa9e4066Sahrens */ 122fa9e4066Sahrens 123*dcbf3bd6SGeorge Wilson /* 124*dcbf3bd6SGeorge Wilson * ARC operation: 125*dcbf3bd6SGeorge Wilson * 126*dcbf3bd6SGeorge Wilson * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure. 127*dcbf3bd6SGeorge Wilson * This structure can point either to a block that is still in the cache or to 128*dcbf3bd6SGeorge Wilson * one that is only accessible in an L2 ARC device, or it can provide 129*dcbf3bd6SGeorge Wilson * information about a block that was recently evicted. If a block is 130*dcbf3bd6SGeorge Wilson * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough 131*dcbf3bd6SGeorge Wilson * information to retrieve it from the L2ARC device. This information is 132*dcbf3bd6SGeorge Wilson * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block 133*dcbf3bd6SGeorge Wilson * that is in this state cannot access the data directly. 134*dcbf3bd6SGeorge Wilson * 135*dcbf3bd6SGeorge Wilson * Blocks that are actively being referenced or have not been evicted 136*dcbf3bd6SGeorge Wilson * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within 137*dcbf3bd6SGeorge Wilson * the arc_buf_hdr_t that will point to the data block in memory. A block can 138*dcbf3bd6SGeorge Wilson * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC 139*dcbf3bd6SGeorge Wilson * caches data in two ways -- in a list of arc buffers (arc_buf_t) and 140*dcbf3bd6SGeorge Wilson * also in the arc_buf_hdr_t's private physical data block pointer (b_pdata). 141*dcbf3bd6SGeorge Wilson * Each arc buffer (arc_buf_t) is being actively accessed by a specific ARC 142*dcbf3bd6SGeorge Wilson * consumer, and always contains uncompressed data. The ARC will provide 143*dcbf3bd6SGeorge Wilson * references to this data and will keep it cached until it is no longer in 144*dcbf3bd6SGeorge Wilson * use. Typically, the arc will try to cache only the L1ARC's physical data 145*dcbf3bd6SGeorge Wilson * block and will aggressively evict any arc_buf_t that is no longer referenced. 146*dcbf3bd6SGeorge Wilson * The amount of memory consumed by the arc_buf_t's can be seen via the 147*dcbf3bd6SGeorge Wilson * "overhead_size" kstat. 148*dcbf3bd6SGeorge Wilson * 149*dcbf3bd6SGeorge Wilson * 150*dcbf3bd6SGeorge Wilson * arc_buf_hdr_t 151*dcbf3bd6SGeorge Wilson * +-----------+ 152*dcbf3bd6SGeorge Wilson * | | 153*dcbf3bd6SGeorge Wilson * | | 154*dcbf3bd6SGeorge Wilson * | | 155*dcbf3bd6SGeorge Wilson * +-----------+ 156*dcbf3bd6SGeorge Wilson * l2arc_buf_hdr_t| | 157*dcbf3bd6SGeorge Wilson * | | 158*dcbf3bd6SGeorge Wilson * +-----------+ 159*dcbf3bd6SGeorge Wilson * l1arc_buf_hdr_t| | 160*dcbf3bd6SGeorge Wilson * | | arc_buf_t 161*dcbf3bd6SGeorge Wilson * | b_buf +------------>+---------+ arc_buf_t 162*dcbf3bd6SGeorge Wilson * | | |b_next +---->+---------+ 163*dcbf3bd6SGeorge Wilson * | b_pdata +-+ |---------| |b_next +-->NULL 164*dcbf3bd6SGeorge Wilson * +-----------+ | | | +---------+ 165*dcbf3bd6SGeorge Wilson * | |b_data +-+ | | 166*dcbf3bd6SGeorge Wilson * | +---------+ | |b_data +-+ 167*dcbf3bd6SGeorge Wilson * +->+------+ | +---------+ | 168*dcbf3bd6SGeorge Wilson * (potentially) | | | | 169*dcbf3bd6SGeorge Wilson * compressed | | | | 170*dcbf3bd6SGeorge Wilson * data +------+ | v 171*dcbf3bd6SGeorge Wilson * +->+------+ +------+ 172*dcbf3bd6SGeorge Wilson * uncompressed | | | | 173*dcbf3bd6SGeorge Wilson * data | | | | 174*dcbf3bd6SGeorge Wilson * +------+ +------+ 175*dcbf3bd6SGeorge Wilson * 176*dcbf3bd6SGeorge Wilson * The L1ARC's data pointer, however, may or may not be uncompressed. The 177*dcbf3bd6SGeorge Wilson * ARC has the ability to store the physical data (b_pdata) associated with 178*dcbf3bd6SGeorge Wilson * the DVA of the arc_buf_hdr_t. Since the b_pdata is a copy of the on-disk 179*dcbf3bd6SGeorge Wilson * physical block, it will match its on-disk compression characteristics. 180*dcbf3bd6SGeorge Wilson * If the block on-disk is compressed, then the physical data block 181*dcbf3bd6SGeorge Wilson * in the cache will also be compressed and vice-versa. This behavior 182*dcbf3bd6SGeorge Wilson * can be disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the 183*dcbf3bd6SGeorge Wilson * compressed ARC functionality is disabled, the b_pdata will point to an 184*dcbf3bd6SGeorge Wilson * uncompressed version of the on-disk data. 185*dcbf3bd6SGeorge Wilson * 186*dcbf3bd6SGeorge Wilson * When a consumer reads a block, the ARC must first look to see if the 187*dcbf3bd6SGeorge Wilson * arc_buf_hdr_t is cached. If the hdr is cached and already has an arc_buf_t, 188*dcbf3bd6SGeorge Wilson * then an additional arc_buf_t is allocated and the uncompressed data is 189*dcbf3bd6SGeorge Wilson * bcopied from the existing arc_buf_t. If the hdr is cached but does not 190*dcbf3bd6SGeorge Wilson * have an arc_buf_t, then the ARC allocates a new arc_buf_t and decompresses 191*dcbf3bd6SGeorge Wilson * the b_pdata contents into the arc_buf_t's b_data. If the arc_buf_hdr_t's 192*dcbf3bd6SGeorge Wilson * b_pdata is not compressed, then the block is shared with the newly 193*dcbf3bd6SGeorge Wilson * allocated arc_buf_t. This block sharing only occurs with one arc_buf_t 194*dcbf3bd6SGeorge Wilson * in the arc buffer chain. Sharing the block reduces the memory overhead 195*dcbf3bd6SGeorge Wilson * required when the hdr is caching uncompressed blocks or the compressed 196*dcbf3bd6SGeorge Wilson * arc functionality has been disabled via 'zfs_compressed_arc_enabled'. 197*dcbf3bd6SGeorge Wilson * 198*dcbf3bd6SGeorge Wilson * The diagram below shows an example of an uncompressed ARC hdr that is 199*dcbf3bd6SGeorge Wilson * sharing its data with an arc_buf_t: 200*dcbf3bd6SGeorge Wilson * 201*dcbf3bd6SGeorge Wilson * arc_buf_hdr_t 202*dcbf3bd6SGeorge Wilson * +-----------+ 203*dcbf3bd6SGeorge Wilson * | | 204*dcbf3bd6SGeorge Wilson * | | 205*dcbf3bd6SGeorge Wilson * | | 206*dcbf3bd6SGeorge Wilson * +-----------+ 207*dcbf3bd6SGeorge Wilson * l2arc_buf_hdr_t| | 208*dcbf3bd6SGeorge Wilson * | | 209*dcbf3bd6SGeorge Wilson * +-----------+ 210*dcbf3bd6SGeorge Wilson * l1arc_buf_hdr_t| | 211*dcbf3bd6SGeorge Wilson * | | arc_buf_t (shared) 212*dcbf3bd6SGeorge Wilson * | b_buf +------------>+---------+ arc_buf_t 213*dcbf3bd6SGeorge Wilson * | | |b_next +---->+---------+ 214*dcbf3bd6SGeorge Wilson * | b_pdata +-+ |---------| |b_next +-->NULL 215*dcbf3bd6SGeorge Wilson * +-----------+ | | | +---------+ 216*dcbf3bd6SGeorge Wilson * | |b_data +-+ | | 217*dcbf3bd6SGeorge Wilson * | +---------+ | |b_data +-+ 218*dcbf3bd6SGeorge Wilson * +->+------+ | +---------+ | 219*dcbf3bd6SGeorge Wilson * | | | | 220*dcbf3bd6SGeorge Wilson * uncompressed | | | | 221*dcbf3bd6SGeorge Wilson * data +------+ | | 222*dcbf3bd6SGeorge Wilson * ^ +->+------+ | 223*dcbf3bd6SGeorge Wilson * | uncompressed | | | 224*dcbf3bd6SGeorge Wilson * | data | | | 225*dcbf3bd6SGeorge Wilson * | +------+ | 226*dcbf3bd6SGeorge Wilson * +---------------------------------+ 227*dcbf3bd6SGeorge Wilson * 228*dcbf3bd6SGeorge Wilson * Writing to the arc requires that the ARC first discard the b_pdata 229*dcbf3bd6SGeorge Wilson * since the physical block is about to be rewritten. The new data contents 230*dcbf3bd6SGeorge Wilson * will be contained in the arc_buf_t (uncompressed). As the I/O pipeline 231*dcbf3bd6SGeorge Wilson * performs the write, it may compress the data before writing it to disk. 232*dcbf3bd6SGeorge Wilson * The ARC will be called with the transformed data and will bcopy the 233*dcbf3bd6SGeorge Wilson * transformed on-disk block into a newly allocated b_pdata. 234*dcbf3bd6SGeorge Wilson * 235*dcbf3bd6SGeorge Wilson * When the L2ARC is in use, it will also take advantage of the b_pdata. The 236*dcbf3bd6SGeorge Wilson * L2ARC will always write the contents of b_pdata to the L2ARC. This means 237*dcbf3bd6SGeorge Wilson * that when compressed arc is enabled that the L2ARC blocks are identical 238*dcbf3bd6SGeorge Wilson * to the on-disk block in the main data pool. This provides a significant 239*dcbf3bd6SGeorge Wilson * advantage since the ARC can leverage the bp's checksum when reading from the 240*dcbf3bd6SGeorge Wilson * L2ARC to determine if the contents are valid. However, if the compressed 241*dcbf3bd6SGeorge Wilson * arc is disabled, then the L2ARC's block must be transformed to look 242*dcbf3bd6SGeorge Wilson * like the physical block in the main data pool before comparing the 243*dcbf3bd6SGeorge Wilson * checksum and determining its validity. 244*dcbf3bd6SGeorge Wilson */ 245*dcbf3bd6SGeorge Wilson 246fa9e4066Sahrens #include <sys/spa.h> 247fa9e4066Sahrens #include <sys/zio.h> 248*dcbf3bd6SGeorge Wilson #include <sys/spa_impl.h> 249aad02571SSaso Kiselkov #include <sys/zio_compress.h> 250*dcbf3bd6SGeorge Wilson #include <sys/zio_checksum.h> 251fa9e4066Sahrens #include <sys/zfs_context.h> 252fa9e4066Sahrens #include <sys/arc.h> 253fa9e4066Sahrens #include <sys/refcount.h> 254c5904d13Seschrock #include <sys/vdev.h> 255573ca77eSGeorge Wilson #include <sys/vdev_impl.h> 25669962b56SMatthew Ahrens #include <sys/dsl_pool.h> 257244781f1SPrakash Surya #include <sys/multilist.h> 258fa9e4066Sahrens #ifdef _KERNEL 259fa9e4066Sahrens #include <sys/vmsystm.h> 260fa9e4066Sahrens #include <vm/anon.h> 261fa9e4066Sahrens #include <sys/fs/swapnode.h> 262033f9833Sek #include <sys/dnlc.h> 263fa9e4066Sahrens #endif 264fa9e4066Sahrens #include <sys/callb.h> 26544cb6abcSbmc #include <sys/kstat.h> 266b24ab676SJeff Bonwick #include <zfs_fletcher.h> 267fa9e4066Sahrens 268cd1c8b85SMatthew Ahrens #ifndef _KERNEL 269cd1c8b85SMatthew Ahrens /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ 270cd1c8b85SMatthew Ahrens boolean_t arc_watch = B_FALSE; 271cd1c8b85SMatthew Ahrens int arc_procfd; 272cd1c8b85SMatthew Ahrens #endif 273cd1c8b85SMatthew Ahrens 274244781f1SPrakash Surya static kmutex_t arc_reclaim_lock; 275244781f1SPrakash Surya static kcondvar_t arc_reclaim_thread_cv; 276244781f1SPrakash Surya static boolean_t arc_reclaim_thread_exit; 277244781f1SPrakash Surya static kcondvar_t arc_reclaim_waiters_cv; 278244781f1SPrakash Surya 2792ec99e3eSMatthew Ahrens uint_t arc_reduce_dnlc_percent = 3; 280fa9e4066Sahrens 28169962b56SMatthew Ahrens /* 282244781f1SPrakash Surya * The number of headers to evict in arc_evict_state_impl() before 283244781f1SPrakash Surya * dropping the sublist lock and evicting from another sublist. A lower 284244781f1SPrakash Surya * value means we're more likely to evict the "correct" header (i.e. the 285244781f1SPrakash Surya * oldest header in the arc state), but comes with higher overhead 286244781f1SPrakash Surya * (i.e. more invocations of arc_evict_state_impl()). 287244781f1SPrakash Surya */ 288244781f1SPrakash Surya int zfs_arc_evict_batch_limit = 10; 289244781f1SPrakash Surya 290244781f1SPrakash Surya /* 291244781f1SPrakash Surya * The number of sublists used for each of the arc state lists. If this 292244781f1SPrakash Surya * is not set to a suitable value by the user, it will be configured to 293244781f1SPrakash Surya * the number of CPUs on the system in arc_init(). 29469962b56SMatthew Ahrens */ 295244781f1SPrakash Surya int zfs_arc_num_sublists_per_state = 0; 29669962b56SMatthew Ahrens 297fa9e4066Sahrens /* number of seconds before growing cache again */ 298fa9e4066Sahrens static int arc_grow_retry = 60; 299fa9e4066Sahrens 300244781f1SPrakash Surya /* shift of arc_c for calculating overflow limit in arc_get_data_buf */ 301244781f1SPrakash Surya int zfs_arc_overflow_shift = 8; 302244781f1SPrakash Surya 3035a98e54bSBrendan Gregg - Sun Microsystems /* shift of arc_c for calculating both min and max arc_p */ 3045a98e54bSBrendan Gregg - Sun Microsystems static int arc_p_min_shift = 4; 3055a98e54bSBrendan Gregg - Sun Microsystems 3065a98e54bSBrendan Gregg - Sun Microsystems /* log2(fraction of arc to reclaim) */ 3072ec99e3eSMatthew Ahrens static int arc_shrink_shift = 7; 3082ec99e3eSMatthew Ahrens 3092ec99e3eSMatthew Ahrens /* 3102ec99e3eSMatthew Ahrens * log2(fraction of ARC which must be free to allow growing). 3112ec99e3eSMatthew Ahrens * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, 3122ec99e3eSMatthew Ahrens * when reading a new block into the ARC, we will evict an equal-sized block 3132ec99e3eSMatthew Ahrens * from the ARC. 3142ec99e3eSMatthew Ahrens * 3152ec99e3eSMatthew Ahrens * This must be less than arc_shrink_shift, so that when we shrink the ARC, 3162ec99e3eSMatthew Ahrens * we will still not allow it to grow. 3172ec99e3eSMatthew Ahrens */ 3182ec99e3eSMatthew Ahrens int arc_no_grow_shift = 5; 3192ec99e3eSMatthew Ahrens 3205a98e54bSBrendan Gregg - Sun Microsystems 32113506d1eSmaybee /* 322b19a79ecSperrin * minimum lifespan of a prefetch block in clock ticks 323b19a79ecSperrin * (initialized in arc_init()) 32413506d1eSmaybee */ 325b19a79ecSperrin static int arc_min_prefetch_lifespan; 32613506d1eSmaybee 32769962b56SMatthew Ahrens /* 32869962b56SMatthew Ahrens * If this percent of memory is free, don't throttle. 32969962b56SMatthew Ahrens */ 33069962b56SMatthew Ahrens int arc_lotsfree_percent = 10; 33169962b56SMatthew Ahrens 332fa9e4066Sahrens static int arc_dead; 333fa9e4066Sahrens 3343a737e0dSbrendan /* 3353a737e0dSbrendan * The arc has filled available memory and has now warmed up. 3363a737e0dSbrendan */ 3373a737e0dSbrendan static boolean_t arc_warm; 3383a737e0dSbrendan 3390dd053d7SPrakash Surya /* 3400dd053d7SPrakash Surya * log2 fraction of the zio arena to keep free. 3410dd053d7SPrakash Surya */ 3420dd053d7SPrakash Surya int arc_zio_arena_free_shift = 2; 3430dd053d7SPrakash Surya 344a2eea2e1Sahrens /* 345a2eea2e1Sahrens * These tunables are for performance analysis. 346a2eea2e1Sahrens */ 347a2eea2e1Sahrens uint64_t zfs_arc_max; 348a2eea2e1Sahrens uint64_t zfs_arc_min; 3491116048bSek uint64_t zfs_arc_meta_limit = 0; 3503a5286a1SMatthew Ahrens uint64_t zfs_arc_meta_min = 0; 3515a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_grow_retry = 0; 3525a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_shrink_shift = 0; 3535a98e54bSBrendan Gregg - Sun Microsystems int zfs_arc_p_min_shift = 0; 35463e911b6SMatthew Ahrens int zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ 355a2eea2e1Sahrens 356*dcbf3bd6SGeorge Wilson boolean_t zfs_compressed_arc_enabled = B_TRUE; 357*dcbf3bd6SGeorge Wilson 358fa9e4066Sahrens /* 359fa94a07fSbrendan * Note that buffers can be in one of 6 states: 360fa9e4066Sahrens * ARC_anon - anonymous (discussed below) 361ea8dc4b6Seschrock * ARC_mru - recently used, currently cached 362ea8dc4b6Seschrock * ARC_mru_ghost - recentely used, no longer in cache 363ea8dc4b6Seschrock * ARC_mfu - frequently used, currently cached 364ea8dc4b6Seschrock * ARC_mfu_ghost - frequently used, no longer in cache 365fa94a07fSbrendan * ARC_l2c_only - exists in L2ARC but not other states 3660e8c6158Smaybee * When there are no active references to the buffer, they are 3670e8c6158Smaybee * are linked onto a list in one of these arc states. These are 3680e8c6158Smaybee * the only buffers that can be evicted or deleted. Within each 3690e8c6158Smaybee * state there are multiple lists, one for meta-data and one for 3700e8c6158Smaybee * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 3710e8c6158Smaybee * etc.) is tracked separately so that it can be managed more 372fa94a07fSbrendan * explicitly: favored over data, limited explicitly. 373fa9e4066Sahrens * 374fa9e4066Sahrens * Anonymous buffers are buffers that are not associated with 375fa9e4066Sahrens * a DVA. These are buffers that hold dirty block copies 376fa9e4066Sahrens * before they are written to stable storage. By definition, 377ea8dc4b6Seschrock * they are "ref'd" and are considered part of arc_mru 378fa9e4066Sahrens * that cannot be freed. Generally, they will aquire a DVA 379ea8dc4b6Seschrock * as they are written and migrate onto the arc_mru list. 380fa94a07fSbrendan * 381fa94a07fSbrendan * The ARC_l2c_only state is for buffers that are in the second 382fa94a07fSbrendan * level ARC but no longer in any of the ARC_m* lists. The second 383fa94a07fSbrendan * level ARC itself may also contain buffers that are in any of 384fa94a07fSbrendan * the ARC_m* states - meaning that a buffer can exist in two 385fa94a07fSbrendan * places. The reason for the ARC_l2c_only state is to keep the 386fa94a07fSbrendan * buffer header in the hash table, so that reads that hit the 387fa94a07fSbrendan * second level ARC benefit from these fast lookups. 388fa9e4066Sahrens */ 389fa9e4066Sahrens 390fa9e4066Sahrens typedef struct arc_state { 391244781f1SPrakash Surya /* 392244781f1SPrakash Surya * list of evictable buffers 393244781f1SPrakash Surya */ 394244781f1SPrakash Surya multilist_t arcs_list[ARC_BUFC_NUMTYPES]; 395244781f1SPrakash Surya /* 396244781f1SPrakash Surya * total amount of evictable data in this state 397244781f1SPrakash Surya */ 398*dcbf3bd6SGeorge Wilson refcount_t arcs_esize[ARC_BUFC_NUMTYPES]; 399244781f1SPrakash Surya /* 400244781f1SPrakash Surya * total amount of data in this state; this includes: evictable, 401244781f1SPrakash Surya * non-evictable, ARC_BUFC_DATA, and ARC_BUFC_METADATA. 402244781f1SPrakash Surya */ 4032fd872a7SPrakash Surya refcount_t arcs_size; 404fa9e4066Sahrens } arc_state_t; 405fa9e4066Sahrens 406fa94a07fSbrendan /* The 6 states: */ 407fa9e4066Sahrens static arc_state_t ARC_anon; 408ea8dc4b6Seschrock static arc_state_t ARC_mru; 409ea8dc4b6Seschrock static arc_state_t ARC_mru_ghost; 410ea8dc4b6Seschrock static arc_state_t ARC_mfu; 411ea8dc4b6Seschrock static arc_state_t ARC_mfu_ghost; 412fa94a07fSbrendan static arc_state_t ARC_l2c_only; 413fa9e4066Sahrens 41444cb6abcSbmc typedef struct arc_stats { 41544cb6abcSbmc kstat_named_t arcstat_hits; 41644cb6abcSbmc kstat_named_t arcstat_misses; 41744cb6abcSbmc kstat_named_t arcstat_demand_data_hits; 41844cb6abcSbmc kstat_named_t arcstat_demand_data_misses; 41944cb6abcSbmc kstat_named_t arcstat_demand_metadata_hits; 42044cb6abcSbmc kstat_named_t arcstat_demand_metadata_misses; 42144cb6abcSbmc kstat_named_t arcstat_prefetch_data_hits; 42244cb6abcSbmc kstat_named_t arcstat_prefetch_data_misses; 42344cb6abcSbmc kstat_named_t arcstat_prefetch_metadata_hits; 42444cb6abcSbmc kstat_named_t arcstat_prefetch_metadata_misses; 42544cb6abcSbmc kstat_named_t arcstat_mru_hits; 42644cb6abcSbmc kstat_named_t arcstat_mru_ghost_hits; 42744cb6abcSbmc kstat_named_t arcstat_mfu_hits; 42844cb6abcSbmc kstat_named_t arcstat_mfu_ghost_hits; 42944cb6abcSbmc kstat_named_t arcstat_deleted; 4303e30c24aSWill Andrews /* 4313e30c24aSWill Andrews * Number of buffers that could not be evicted because the hash lock 4323e30c24aSWill Andrews * was held by another thread. The lock may not necessarily be held 4333e30c24aSWill Andrews * by something using the same buffer, since hash locks are shared 4343e30c24aSWill Andrews * by multiple buffers. 4353e30c24aSWill Andrews */ 43644cb6abcSbmc kstat_named_t arcstat_mutex_miss; 4373e30c24aSWill Andrews /* 4383e30c24aSWill Andrews * Number of buffers skipped because they have I/O in progress, are 4393e30c24aSWill Andrews * indrect prefetch buffers that have not lived long enough, or are 4403e30c24aSWill Andrews * not from the spa we're trying to evict from. 4413e30c24aSWill Andrews */ 44244cb6abcSbmc kstat_named_t arcstat_evict_skip; 443244781f1SPrakash Surya /* 444244781f1SPrakash Surya * Number of times arc_evict_state() was unable to evict enough 445244781f1SPrakash Surya * buffers to reach it's target amount. 446244781f1SPrakash Surya */ 447244781f1SPrakash Surya kstat_named_t arcstat_evict_not_enough; 4485ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_cached; 4495ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_eligible; 4505ea40c06SBrendan Gregg - Sun Microsystems kstat_named_t arcstat_evict_l2_ineligible; 451244781f1SPrakash Surya kstat_named_t arcstat_evict_l2_skip; 45244cb6abcSbmc kstat_named_t arcstat_hash_elements; 45344cb6abcSbmc kstat_named_t arcstat_hash_elements_max; 45444cb6abcSbmc kstat_named_t arcstat_hash_collisions; 45544cb6abcSbmc kstat_named_t arcstat_hash_chains; 45644cb6abcSbmc kstat_named_t arcstat_hash_chain_max; 45744cb6abcSbmc kstat_named_t arcstat_p; 45844cb6abcSbmc kstat_named_t arcstat_c; 45944cb6abcSbmc kstat_named_t arcstat_c_min; 46044cb6abcSbmc kstat_named_t arcstat_c_max; 46144cb6abcSbmc kstat_named_t arcstat_size; 462*dcbf3bd6SGeorge Wilson /* 463*dcbf3bd6SGeorge Wilson * Number of compressed bytes stored in the arc_buf_hdr_t's b_pdata. 464*dcbf3bd6SGeorge Wilson * Note that the compressed bytes may match the uncompressed bytes 465*dcbf3bd6SGeorge Wilson * if the block is either not compressed or compressed arc is disabled. 466*dcbf3bd6SGeorge Wilson */ 467*dcbf3bd6SGeorge Wilson kstat_named_t arcstat_compressed_size; 468*dcbf3bd6SGeorge Wilson /* 469*dcbf3bd6SGeorge Wilson * Uncompressed size of the data stored in b_pdata. If compressed 470*dcbf3bd6SGeorge Wilson * arc is disabled then this value will be identical to the stat 471*dcbf3bd6SGeorge Wilson * above. 472*dcbf3bd6SGeorge Wilson */ 473*dcbf3bd6SGeorge Wilson kstat_named_t arcstat_uncompressed_size; 474*dcbf3bd6SGeorge Wilson /* 475*dcbf3bd6SGeorge Wilson * Number of bytes stored in all the arc_buf_t's. This is classified 476*dcbf3bd6SGeorge Wilson * as "overhead" since this data is typically short-lived and will 477*dcbf3bd6SGeorge Wilson * be evicted from the arc when it becomes unreferenced unless the 478*dcbf3bd6SGeorge Wilson * zfs_keep_uncompressed_metadata or zfs_keep_uncompressed_level 479*dcbf3bd6SGeorge Wilson * values have been set (see comment in dbuf.c for more information). 480*dcbf3bd6SGeorge Wilson */ 481*dcbf3bd6SGeorge Wilson kstat_named_t arcstat_overhead_size; 4824076b1bfSPrakash Surya /* 4834076b1bfSPrakash Surya * Number of bytes consumed by internal ARC structures necessary 4844076b1bfSPrakash Surya * for tracking purposes; these structures are not actually 4854076b1bfSPrakash Surya * backed by ARC buffers. This includes arc_buf_hdr_t structures 4864076b1bfSPrakash Surya * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only 4874076b1bfSPrakash Surya * caches), and arc_buf_t structures (allocated via arc_buf_t 4884076b1bfSPrakash Surya * cache). 4894076b1bfSPrakash Surya */ 490fa94a07fSbrendan kstat_named_t arcstat_hdr_size; 4914076b1bfSPrakash Surya /* 4924076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers of type equal to 4934076b1bfSPrakash Surya * ARC_BUFC_DATA. This is generally consumed by buffers backing 4944076b1bfSPrakash Surya * on disk user data (e.g. plain file contents). 4954076b1bfSPrakash Surya */ 4965a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_data_size; 4974076b1bfSPrakash Surya /* 4984076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers of type equal to 4994076b1bfSPrakash Surya * ARC_BUFC_METADATA. This is generally consumed by buffers 5004076b1bfSPrakash Surya * backing on disk data that is used for internal ZFS 5014076b1bfSPrakash Surya * structures (e.g. ZAP, dnode, indirect blocks, etc). 5024076b1bfSPrakash Surya */ 5034076b1bfSPrakash Surya kstat_named_t arcstat_metadata_size; 5044076b1bfSPrakash Surya /* 5054076b1bfSPrakash Surya * Number of bytes consumed by various buffers and structures 5064076b1bfSPrakash Surya * not actually backed with ARC buffers. This includes bonus 5074076b1bfSPrakash Surya * buffers (allocated directly via zio_buf_* functions), 5084076b1bfSPrakash Surya * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t 5094076b1bfSPrakash Surya * cache), and dnode_t structures (allocated via dnode_t cache). 5104076b1bfSPrakash Surya */ 5115a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_other_size; 5124076b1bfSPrakash Surya /* 5134076b1bfSPrakash Surya * Total number of bytes consumed by ARC buffers residing in the 5144076b1bfSPrakash Surya * arc_anon state. This includes *all* buffers in the arc_anon 5154076b1bfSPrakash Surya * state; e.g. data, metadata, evictable, and unevictable buffers 5164076b1bfSPrakash Surya * are all included in this value. 5174076b1bfSPrakash Surya */ 5184076b1bfSPrakash Surya kstat_named_t arcstat_anon_size; 5194076b1bfSPrakash Surya /* 5204076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5214076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_DATA, 5224076b1bfSPrakash Surya * residing in the arc_anon state, and are eligible for eviction 5234076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 5244076b1bfSPrakash Surya */ 5254076b1bfSPrakash Surya kstat_named_t arcstat_anon_evictable_data; 5264076b1bfSPrakash Surya /* 5274076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5284076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_METADATA, 5294076b1bfSPrakash Surya * residing in the arc_anon state, and are eligible for eviction 5304076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 5314076b1bfSPrakash Surya */ 5324076b1bfSPrakash Surya kstat_named_t arcstat_anon_evictable_metadata; 5334076b1bfSPrakash Surya /* 5344076b1bfSPrakash Surya * Total number of bytes consumed by ARC buffers residing in the 5354076b1bfSPrakash Surya * arc_mru state. This includes *all* buffers in the arc_mru 5364076b1bfSPrakash Surya * state; e.g. data, metadata, evictable, and unevictable buffers 5374076b1bfSPrakash Surya * are all included in this value. 5384076b1bfSPrakash Surya */ 5394076b1bfSPrakash Surya kstat_named_t arcstat_mru_size; 5404076b1bfSPrakash Surya /* 5414076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5424076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_DATA, 5434076b1bfSPrakash Surya * residing in the arc_mru state, and are eligible for eviction 5444076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 5454076b1bfSPrakash Surya */ 5464076b1bfSPrakash Surya kstat_named_t arcstat_mru_evictable_data; 5474076b1bfSPrakash Surya /* 5484076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that meet the 5494076b1bfSPrakash Surya * following criteria: backing buffers of type ARC_BUFC_METADATA, 5504076b1bfSPrakash Surya * residing in the arc_mru state, and are eligible for eviction 5514076b1bfSPrakash Surya * (e.g. have no outstanding holds on the buffer). 5524076b1bfSPrakash Surya */ 5534076b1bfSPrakash Surya kstat_named_t arcstat_mru_evictable_metadata; 5544076b1bfSPrakash Surya /* 5554076b1bfSPrakash Surya * Total number of bytes that *would have been* consumed by ARC 5564076b1bfSPrakash Surya * buffers in the arc_mru_ghost state. The key thing to note 5574076b1bfSPrakash Surya * here, is the fact that this size doesn't actually indicate 5584076b1bfSPrakash Surya * RAM consumption. The ghost lists only consist of headers and 5594076b1bfSPrakash Surya * don't actually have ARC buffers linked off of these headers. 5604076b1bfSPrakash Surya * Thus, *if* the headers had associated ARC buffers, these 5614076b1bfSPrakash Surya * buffers *would have* consumed this number of bytes. 5624076b1bfSPrakash Surya */ 5634076b1bfSPrakash Surya kstat_named_t arcstat_mru_ghost_size; 5644076b1bfSPrakash Surya /* 5654076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 5664076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 5674076b1bfSPrakash Surya * ARC_BUFC_DATA, and linked off the arc_mru_ghost state. 5684076b1bfSPrakash Surya */ 5694076b1bfSPrakash Surya kstat_named_t arcstat_mru_ghost_evictable_data; 5704076b1bfSPrakash Surya /* 5714076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 5724076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 5734076b1bfSPrakash Surya * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 5744076b1bfSPrakash Surya */ 5754076b1bfSPrakash Surya kstat_named_t arcstat_mru_ghost_evictable_metadata; 5764076b1bfSPrakash Surya /* 5774076b1bfSPrakash Surya * Total number of bytes consumed by ARC buffers residing in the 5784076b1bfSPrakash Surya * arc_mfu state. This includes *all* buffers in the arc_mfu 5794076b1bfSPrakash Surya * state; e.g. data, metadata, evictable, and unevictable buffers 5804076b1bfSPrakash Surya * are all included in this value. 5814076b1bfSPrakash Surya */ 5824076b1bfSPrakash Surya kstat_named_t arcstat_mfu_size; 5834076b1bfSPrakash Surya /* 5844076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that are eligible for 5854076b1bfSPrakash Surya * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu 5864076b1bfSPrakash Surya * state. 5874076b1bfSPrakash Surya */ 5884076b1bfSPrakash Surya kstat_named_t arcstat_mfu_evictable_data; 5894076b1bfSPrakash Surya /* 5904076b1bfSPrakash Surya * Number of bytes consumed by ARC buffers that are eligible for 5914076b1bfSPrakash Surya * eviction, of type ARC_BUFC_METADATA, and reside in the 5924076b1bfSPrakash Surya * arc_mfu state. 5934076b1bfSPrakash Surya */ 5944076b1bfSPrakash Surya kstat_named_t arcstat_mfu_evictable_metadata; 5954076b1bfSPrakash Surya /* 5964076b1bfSPrakash Surya * Total number of bytes that *would have been* consumed by ARC 5974076b1bfSPrakash Surya * buffers in the arc_mfu_ghost state. See the comment above 5984076b1bfSPrakash Surya * arcstat_mru_ghost_size for more details. 5994076b1bfSPrakash Surya */ 6004076b1bfSPrakash Surya kstat_named_t arcstat_mfu_ghost_size; 6014076b1bfSPrakash Surya /* 6024076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 6034076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 6044076b1bfSPrakash Surya * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state. 6054076b1bfSPrakash Surya */ 6064076b1bfSPrakash Surya kstat_named_t arcstat_mfu_ghost_evictable_data; 6074076b1bfSPrakash Surya /* 6084076b1bfSPrakash Surya * Number of bytes that *would have been* consumed by ARC 6094076b1bfSPrakash Surya * buffers that are eligible for eviction, of type 6104076b1bfSPrakash Surya * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 6114076b1bfSPrakash Surya */ 6124076b1bfSPrakash Surya kstat_named_t arcstat_mfu_ghost_evictable_metadata; 613fa94a07fSbrendan kstat_named_t arcstat_l2_hits; 614fa94a07fSbrendan kstat_named_t arcstat_l2_misses; 615fa94a07fSbrendan kstat_named_t arcstat_l2_feeds; 616fa94a07fSbrendan kstat_named_t arcstat_l2_rw_clash; 6175a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_l2_read_bytes; 6185a98e54bSBrendan Gregg - Sun Microsystems kstat_named_t arcstat_l2_write_bytes; 619fa94a07fSbrendan kstat_named_t arcstat_l2_writes_sent; 620fa94a07fSbrendan kstat_named_t arcstat_l2_writes_done; 621fa94a07fSbrendan kstat_named_t arcstat_l2_writes_error; 622244781f1SPrakash Surya kstat_named_t arcstat_l2_writes_lock_retry; 623fa94a07fSbrendan kstat_named_t arcstat_l2_evict_lock_retry; 624fa94a07fSbrendan kstat_named_t arcstat_l2_evict_reading; 62589c86e32SChris Williamson kstat_named_t arcstat_l2_evict_l1cached; 626fa94a07fSbrendan kstat_named_t arcstat_l2_free_on_write; 627fa94a07fSbrendan kstat_named_t arcstat_l2_abort_lowmem; 628fa94a07fSbrendan kstat_named_t arcstat_l2_cksum_bad; 629fa94a07fSbrendan kstat_named_t arcstat_l2_io_error; 630fa94a07fSbrendan kstat_named_t arcstat_l2_size; 631aad02571SSaso Kiselkov kstat_named_t arcstat_l2_asize; 632fa94a07fSbrendan kstat_named_t arcstat_l2_hdr_size; 6331ab7f2deSmaybee kstat_named_t arcstat_memory_throttle_count; 63420128a08SGeorge Wilson kstat_named_t arcstat_meta_used; 63520128a08SGeorge Wilson kstat_named_t arcstat_meta_limit; 63620128a08SGeorge Wilson kstat_named_t arcstat_meta_max; 6373a5286a1SMatthew Ahrens kstat_named_t arcstat_meta_min; 638cf6106c8SMatthew Ahrens kstat_named_t arcstat_sync_wait_for_async; 639cf6106c8SMatthew Ahrens kstat_named_t arcstat_demand_hit_predictive_prefetch; 64044cb6abcSbmc } arc_stats_t; 64144cb6abcSbmc 64244cb6abcSbmc static arc_stats_t arc_stats = { 64344cb6abcSbmc { "hits", KSTAT_DATA_UINT64 }, 64444cb6abcSbmc { "misses", KSTAT_DATA_UINT64 }, 64544cb6abcSbmc { "demand_data_hits", KSTAT_DATA_UINT64 }, 64644cb6abcSbmc { "demand_data_misses", KSTAT_DATA_UINT64 }, 64744cb6abcSbmc { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 64844cb6abcSbmc { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 64944cb6abcSbmc { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 65044cb6abcSbmc { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 65144cb6abcSbmc { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 65244cb6abcSbmc { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 65344cb6abcSbmc { "mru_hits", KSTAT_DATA_UINT64 }, 65444cb6abcSbmc { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 65544cb6abcSbmc { "mfu_hits", KSTAT_DATA_UINT64 }, 65644cb6abcSbmc { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 65744cb6abcSbmc { "deleted", KSTAT_DATA_UINT64 }, 65844cb6abcSbmc { "mutex_miss", KSTAT_DATA_UINT64 }, 65944cb6abcSbmc { "evict_skip", KSTAT_DATA_UINT64 }, 660244781f1SPrakash Surya { "evict_not_enough", KSTAT_DATA_UINT64 }, 6615ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_cached", KSTAT_DATA_UINT64 }, 6625ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 6635ea40c06SBrendan Gregg - Sun Microsystems { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 664244781f1SPrakash Surya { "evict_l2_skip", KSTAT_DATA_UINT64 }, 66544cb6abcSbmc { "hash_elements", KSTAT_DATA_UINT64 }, 66644cb6abcSbmc { "hash_elements_max", KSTAT_DATA_UINT64 }, 66744cb6abcSbmc { "hash_collisions", KSTAT_DATA_UINT64 }, 66844cb6abcSbmc { "hash_chains", KSTAT_DATA_UINT64 }, 66944cb6abcSbmc { "hash_chain_max", KSTAT_DATA_UINT64 }, 67044cb6abcSbmc { "p", KSTAT_DATA_UINT64 }, 67144cb6abcSbmc { "c", KSTAT_DATA_UINT64 }, 67244cb6abcSbmc { "c_min", KSTAT_DATA_UINT64 }, 67344cb6abcSbmc { "c_max", KSTAT_DATA_UINT64 }, 674fa94a07fSbrendan { "size", KSTAT_DATA_UINT64 }, 675*dcbf3bd6SGeorge Wilson { "compressed_size", KSTAT_DATA_UINT64 }, 676*dcbf3bd6SGeorge Wilson { "uncompressed_size", KSTAT_DATA_UINT64 }, 677*dcbf3bd6SGeorge Wilson { "overhead_size", KSTAT_DATA_UINT64 }, 678fa94a07fSbrendan { "hdr_size", KSTAT_DATA_UINT64 }, 6795a98e54bSBrendan Gregg - Sun Microsystems { "data_size", KSTAT_DATA_UINT64 }, 6804076b1bfSPrakash Surya { "metadata_size", KSTAT_DATA_UINT64 }, 6815a98e54bSBrendan Gregg - Sun Microsystems { "other_size", KSTAT_DATA_UINT64 }, 6824076b1bfSPrakash Surya { "anon_size", KSTAT_DATA_UINT64 }, 6834076b1bfSPrakash Surya { "anon_evictable_data", KSTAT_DATA_UINT64 }, 6844076b1bfSPrakash Surya { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, 6854076b1bfSPrakash Surya { "mru_size", KSTAT_DATA_UINT64 }, 6864076b1bfSPrakash Surya { "mru_evictable_data", KSTAT_DATA_UINT64 }, 6874076b1bfSPrakash Surya { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, 6884076b1bfSPrakash Surya { "mru_ghost_size", KSTAT_DATA_UINT64 }, 6894076b1bfSPrakash Surya { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, 6904076b1bfSPrakash Surya { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 6914076b1bfSPrakash Surya { "mfu_size", KSTAT_DATA_UINT64 }, 6924076b1bfSPrakash Surya { "mfu_evictable_data", KSTAT_DATA_UINT64 }, 6934076b1bfSPrakash Surya { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, 6944076b1bfSPrakash Surya { "mfu_ghost_size", KSTAT_DATA_UINT64 }, 6954076b1bfSPrakash Surya { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, 6964076b1bfSPrakash Surya { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 697fa94a07fSbrendan { "l2_hits", KSTAT_DATA_UINT64 }, 698fa94a07fSbrendan { "l2_misses", KSTAT_DATA_UINT64 }, 699fa94a07fSbrendan { "l2_feeds", KSTAT_DATA_UINT64 }, 700fa94a07fSbrendan { "l2_rw_clash", KSTAT_DATA_UINT64 }, 7015a98e54bSBrendan Gregg - Sun Microsystems { "l2_read_bytes", KSTAT_DATA_UINT64 }, 7025a98e54bSBrendan Gregg - Sun Microsystems { "l2_write_bytes", KSTAT_DATA_UINT64 }, 703fa94a07fSbrendan { "l2_writes_sent", KSTAT_DATA_UINT64 }, 704fa94a07fSbrendan { "l2_writes_done", KSTAT_DATA_UINT64 }, 705fa94a07fSbrendan { "l2_writes_error", KSTAT_DATA_UINT64 }, 706244781f1SPrakash Surya { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, 707fa94a07fSbrendan { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 708fa94a07fSbrendan { "l2_evict_reading", KSTAT_DATA_UINT64 }, 70989c86e32SChris Williamson { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, 710fa94a07fSbrendan { "l2_free_on_write", KSTAT_DATA_UINT64 }, 711fa94a07fSbrendan { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 712fa94a07fSbrendan { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 713fa94a07fSbrendan { "l2_io_error", KSTAT_DATA_UINT64 }, 714fa94a07fSbrendan { "l2_size", KSTAT_DATA_UINT64 }, 715aad02571SSaso Kiselkov { "l2_asize", KSTAT_DATA_UINT64 }, 7161ab7f2deSmaybee { "l2_hdr_size", KSTAT_DATA_UINT64 }, 7179253d63dSGeorge Wilson { "memory_throttle_count", KSTAT_DATA_UINT64 }, 71820128a08SGeorge Wilson { "arc_meta_used", KSTAT_DATA_UINT64 }, 71920128a08SGeorge Wilson { "arc_meta_limit", KSTAT_DATA_UINT64 }, 7203a5286a1SMatthew Ahrens { "arc_meta_max", KSTAT_DATA_UINT64 }, 721cf6106c8SMatthew Ahrens { "arc_meta_min", KSTAT_DATA_UINT64 }, 722cf6106c8SMatthew Ahrens { "sync_wait_for_async", KSTAT_DATA_UINT64 }, 723cf6106c8SMatthew Ahrens { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 }, 72444cb6abcSbmc }; 72544cb6abcSbmc 72644cb6abcSbmc #define ARCSTAT(stat) (arc_stats.stat.value.ui64) 72744cb6abcSbmc 72844cb6abcSbmc #define ARCSTAT_INCR(stat, val) \ 729f7170741SWill Andrews atomic_add_64(&arc_stats.stat.value.ui64, (val)) 73044cb6abcSbmc 731b24ab676SJeff Bonwick #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 73244cb6abcSbmc #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 73344cb6abcSbmc 73444cb6abcSbmc #define ARCSTAT_MAX(stat, val) { \ 73544cb6abcSbmc uint64_t m; \ 73644cb6abcSbmc while ((val) > (m = arc_stats.stat.value.ui64) && \ 73744cb6abcSbmc (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 73844cb6abcSbmc continue; \ 73944cb6abcSbmc } 74044cb6abcSbmc 74144cb6abcSbmc #define ARCSTAT_MAXSTAT(stat) \ 74244cb6abcSbmc ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 74344cb6abcSbmc 74444cb6abcSbmc /* 74544cb6abcSbmc * We define a macro to allow ARC hits/misses to be easily broken down by 74644cb6abcSbmc * two separate conditions, giving a total of four different subtypes for 74744cb6abcSbmc * each of hits and misses (so eight statistics total). 74844cb6abcSbmc */ 74944cb6abcSbmc #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 75044cb6abcSbmc if (cond1) { \ 75144cb6abcSbmc if (cond2) { \ 75244cb6abcSbmc ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 75344cb6abcSbmc } else { \ 75444cb6abcSbmc ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 75544cb6abcSbmc } \ 75644cb6abcSbmc } else { \ 75744cb6abcSbmc if (cond2) { \ 75844cb6abcSbmc ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 75944cb6abcSbmc } else { \ 76044cb6abcSbmc ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 76144cb6abcSbmc } \ 76244cb6abcSbmc } 76344cb6abcSbmc 76444cb6abcSbmc kstat_t *arc_ksp; 765b24ab676SJeff Bonwick static arc_state_t *arc_anon; 76644cb6abcSbmc static arc_state_t *arc_mru; 76744cb6abcSbmc static arc_state_t *arc_mru_ghost; 76844cb6abcSbmc static arc_state_t *arc_mfu; 76944cb6abcSbmc static arc_state_t *arc_mfu_ghost; 770fa94a07fSbrendan static arc_state_t *arc_l2c_only; 77144cb6abcSbmc 77244cb6abcSbmc /* 77344cb6abcSbmc * There are several ARC variables that are critical to export as kstats -- 77444cb6abcSbmc * but we don't want to have to grovel around in the kstat whenever we wish to 77544cb6abcSbmc * manipulate them. For these variables, we therefore define them to be in 77644cb6abcSbmc * terms of the statistic variable. This assures that we are not introducing 77744cb6abcSbmc * the possibility of inconsistency by having shadow copies of the variables, 77844cb6abcSbmc * while still allowing the code to be readable. 77944cb6abcSbmc */ 78044cb6abcSbmc #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 78144cb6abcSbmc #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 78244cb6abcSbmc #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 78344cb6abcSbmc #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 78444cb6abcSbmc #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 78520128a08SGeorge Wilson #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ 7863a5286a1SMatthew Ahrens #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ 78720128a08SGeorge Wilson #define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */ 78820128a08SGeorge Wilson #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ 78944cb6abcSbmc 790*dcbf3bd6SGeorge Wilson /* compressed size of entire arc */ 791*dcbf3bd6SGeorge Wilson #define arc_compressed_size ARCSTAT(arcstat_compressed_size) 792*dcbf3bd6SGeorge Wilson /* uncompressed size of entire arc */ 793*dcbf3bd6SGeorge Wilson #define arc_uncompressed_size ARCSTAT(arcstat_uncompressed_size) 794*dcbf3bd6SGeorge Wilson /* number of bytes in the arc from arc_buf_t's */ 795*dcbf3bd6SGeorge Wilson #define arc_overhead_size ARCSTAT(arcstat_overhead_size) 796aad02571SSaso Kiselkov 79744cb6abcSbmc static int arc_no_grow; /* Don't try to grow cache size */ 79844cb6abcSbmc static uint64_t arc_tempreserve; 7992fdbea25SAleksandr Guzovskiy static uint64_t arc_loaned_bytes; 800fa9e4066Sahrens 801fa9e4066Sahrens typedef struct arc_callback arc_callback_t; 802fa9e4066Sahrens 803fa9e4066Sahrens struct arc_callback { 804fa9e4066Sahrens void *acb_private; 805c717a561Smaybee arc_done_func_t *acb_done; 806fa9e4066Sahrens arc_buf_t *acb_buf; 807fa9e4066Sahrens zio_t *acb_zio_dummy; 808fa9e4066Sahrens arc_callback_t *acb_next; 809fa9e4066Sahrens }; 810fa9e4066Sahrens 811c717a561Smaybee typedef struct arc_write_callback arc_write_callback_t; 812c717a561Smaybee 813c717a561Smaybee struct arc_write_callback { 814c717a561Smaybee void *awcb_private; 815c717a561Smaybee arc_done_func_t *awcb_ready; 8168df0bcf0SPaul Dagnelie arc_done_func_t *awcb_children_ready; 81769962b56SMatthew Ahrens arc_done_func_t *awcb_physdone; 818c717a561Smaybee arc_done_func_t *awcb_done; 819c717a561Smaybee arc_buf_t *awcb_buf; 820c717a561Smaybee }; 821c717a561Smaybee 82289c86e32SChris Williamson /* 82389c86e32SChris Williamson * ARC buffers are separated into multiple structs as a memory saving measure: 82489c86e32SChris Williamson * - Common fields struct, always defined, and embedded within it: 82589c86e32SChris Williamson * - L2-only fields, always allocated but undefined when not in L2ARC 82689c86e32SChris Williamson * - L1-only fields, only allocated when in L1ARC 82789c86e32SChris Williamson * 82889c86e32SChris Williamson * Buffer in L1 Buffer only in L2 82989c86e32SChris Williamson * +------------------------+ +------------------------+ 83089c86e32SChris Williamson * | arc_buf_hdr_t | | arc_buf_hdr_t | 83189c86e32SChris Williamson * | | | | 83289c86e32SChris Williamson * | | | | 83389c86e32SChris Williamson * | | | | 83489c86e32SChris Williamson * +------------------------+ +------------------------+ 83589c86e32SChris Williamson * | l2arc_buf_hdr_t | | l2arc_buf_hdr_t | 83689c86e32SChris Williamson * | (undefined if L1-only) | | | 83789c86e32SChris Williamson * +------------------------+ +------------------------+ 83889c86e32SChris Williamson * | l1arc_buf_hdr_t | 83989c86e32SChris Williamson * | | 84089c86e32SChris Williamson * | | 84189c86e32SChris Williamson * | | 84289c86e32SChris Williamson * | | 84389c86e32SChris Williamson * +------------------------+ 84489c86e32SChris Williamson * 84589c86e32SChris Williamson * Because it's possible for the L2ARC to become extremely large, we can wind 84689c86e32SChris Williamson * up eating a lot of memory in L2ARC buffer headers, so the size of a header 84789c86e32SChris Williamson * is minimized by only allocating the fields necessary for an L1-cached buffer 84889c86e32SChris Williamson * when a header is actually in the L1 cache. The sub-headers (l1arc_buf_hdr and 84989c86e32SChris Williamson * l2arc_buf_hdr) are embedded rather than allocated separately to save a couple 85089c86e32SChris Williamson * words in pointers. arc_hdr_realloc() is used to switch a header between 85189c86e32SChris Williamson * these two allocation states. 85289c86e32SChris Williamson */ 85389c86e32SChris Williamson typedef struct l1arc_buf_hdr { 8546b4acc8bSahrens kmutex_t b_freeze_lock; 855*dcbf3bd6SGeorge Wilson zio_cksum_t *b_freeze_cksum; 85689c86e32SChris Williamson #ifdef ZFS_DEBUG 85789c86e32SChris Williamson /* 85889c86e32SChris Williamson * used for debugging wtih kmem_flags - by allocating and freeing 85989c86e32SChris Williamson * b_thawed when the buffer is thawed, we get a record of the stack 86089c86e32SChris Williamson * trace that thawed it. 86189c86e32SChris Williamson */ 8623f9d6ad7SLin Ling void *b_thawed; 86389c86e32SChris Williamson #endif 8646b4acc8bSahrens 865fa9e4066Sahrens arc_buf_t *b_buf; 866*dcbf3bd6SGeorge Wilson uint32_t b_bufcnt; 86789c86e32SChris Williamson /* for waiting on writes to complete */ 868ad23a2dbSjohansen kcondvar_t b_cv; 869*dcbf3bd6SGeorge Wilson uint8_t b_byteswap; 870ad23a2dbSjohansen 871fa9e4066Sahrens /* protected by arc state mutex */ 872fa9e4066Sahrens arc_state_t *b_state; 873244781f1SPrakash Surya multilist_node_t b_arc_node; 874fa9e4066Sahrens 875fa9e4066Sahrens /* updated atomically */ 876fa9e4066Sahrens clock_t b_arc_access; 877fa9e4066Sahrens 878fa9e4066Sahrens /* self protecting */ 879fa9e4066Sahrens refcount_t b_refcnt; 880fa94a07fSbrendan 88189c86e32SChris Williamson arc_callback_t *b_acb; 882*dcbf3bd6SGeorge Wilson void *b_pdata; 88389c86e32SChris Williamson } l1arc_buf_hdr_t; 88489c86e32SChris Williamson 88589c86e32SChris Williamson typedef struct l2arc_dev l2arc_dev_t; 88689c86e32SChris Williamson 88789c86e32SChris Williamson typedef struct l2arc_buf_hdr { 88889c86e32SChris Williamson /* protected by arc_buf_hdr mutex */ 88989c86e32SChris Williamson l2arc_dev_t *b_dev; /* L2ARC device */ 89089c86e32SChris Williamson uint64_t b_daddr; /* disk address, offset byte */ 89189c86e32SChris Williamson 892fa94a07fSbrendan list_node_t b_l2node; 89389c86e32SChris Williamson } l2arc_buf_hdr_t; 89489c86e32SChris Williamson 89589c86e32SChris Williamson struct arc_buf_hdr { 89689c86e32SChris Williamson /* protected by hash lock */ 89789c86e32SChris Williamson dva_t b_dva; 89889c86e32SChris Williamson uint64_t b_birth; 89989c86e32SChris Williamson 900*dcbf3bd6SGeorge Wilson arc_buf_contents_t b_type; 90189c86e32SChris Williamson arc_buf_hdr_t *b_hash_next; 90289c86e32SChris Williamson arc_flags_t b_flags; 90389c86e32SChris Williamson 904*dcbf3bd6SGeorge Wilson /* 905*dcbf3bd6SGeorge Wilson * This field stores the size of the data buffer after 906*dcbf3bd6SGeorge Wilson * compression, and is set in the arc's zio completion handlers. 907*dcbf3bd6SGeorge Wilson * It is in units of SPA_MINBLOCKSIZE (e.g. 1 == 512 bytes). 908*dcbf3bd6SGeorge Wilson * 909*dcbf3bd6SGeorge Wilson * While the block pointers can store up to 32MB in their psize 910*dcbf3bd6SGeorge Wilson * field, we can only store up to 32MB minus 512B. This is due 911*dcbf3bd6SGeorge Wilson * to the bp using a bias of 1, whereas we use a bias of 0 (i.e. 912*dcbf3bd6SGeorge Wilson * a field of zeros represents 512B in the bp). We can't use a 913*dcbf3bd6SGeorge Wilson * bias of 1 since we need to reserve a psize of zero, here, to 914*dcbf3bd6SGeorge Wilson * represent holes and embedded blocks. 915*dcbf3bd6SGeorge Wilson * 916*dcbf3bd6SGeorge Wilson * This isn't a problem in practice, since the maximum size of a 917*dcbf3bd6SGeorge Wilson * buffer is limited to 16MB, so we never need to store 32MB in 918*dcbf3bd6SGeorge Wilson * this field. Even in the upstream illumos code base, the 919*dcbf3bd6SGeorge Wilson * maximum size of a buffer is limited to 16MB. 920*dcbf3bd6SGeorge Wilson */ 921*dcbf3bd6SGeorge Wilson uint16_t b_psize; 922*dcbf3bd6SGeorge Wilson 923*dcbf3bd6SGeorge Wilson /* 924*dcbf3bd6SGeorge Wilson * This field stores the size of the data buffer before 925*dcbf3bd6SGeorge Wilson * compression, and cannot change once set. It is in units 926*dcbf3bd6SGeorge Wilson * of SPA_MINBLOCKSIZE (e.g. 2 == 1024 bytes) 927*dcbf3bd6SGeorge Wilson */ 928*dcbf3bd6SGeorge Wilson uint16_t b_lsize; /* immutable */ 929*dcbf3bd6SGeorge Wilson uint64_t b_spa; /* immutable */ 93089c86e32SChris Williamson 93189c86e32SChris Williamson /* L2ARC fields. Undefined when not in L2ARC. */ 93289c86e32SChris Williamson l2arc_buf_hdr_t b_l2hdr; 93389c86e32SChris Williamson /* L1ARC fields. Undefined when in l2arc_only state */ 93489c86e32SChris Williamson l1arc_buf_hdr_t b_l1hdr; 935fa9e4066Sahrens }; 936fa9e4066Sahrens 937ea8dc4b6Seschrock #define GHOST_STATE(state) \ 938fa94a07fSbrendan ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 939fa94a07fSbrendan (state) == arc_l2c_only) 940ea8dc4b6Seschrock 9417adb730bSGeorge Wilson #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) 9427adb730bSGeorge Wilson #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) 9437adb730bSGeorge Wilson #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) 9447adb730bSGeorge Wilson #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) 945*dcbf3bd6SGeorge Wilson #define HDR_COMPRESSION_ENABLED(hdr) \ 946*dcbf3bd6SGeorge Wilson ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC) 94789c86e32SChris Williamson 9487adb730bSGeorge Wilson #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) 9497adb730bSGeorge Wilson #define HDR_L2_READING(hdr) \ 950*dcbf3bd6SGeorge Wilson (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ 951*dcbf3bd6SGeorge Wilson ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) 9527adb730bSGeorge Wilson #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) 9537adb730bSGeorge Wilson #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) 9547adb730bSGeorge Wilson #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) 955*dcbf3bd6SGeorge Wilson #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA) 956fa9e4066Sahrens 95789c86e32SChris Williamson #define HDR_ISTYPE_METADATA(hdr) \ 958*dcbf3bd6SGeorge Wilson ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) 95989c86e32SChris Williamson #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) 96089c86e32SChris Williamson 96189c86e32SChris Williamson #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) 96289c86e32SChris Williamson #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) 96389c86e32SChris Williamson 964*dcbf3bd6SGeorge Wilson /* For storing compression mode in b_flags */ 965*dcbf3bd6SGeorge Wilson #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1) 966*dcbf3bd6SGeorge Wilson 967*dcbf3bd6SGeorge Wilson #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \ 968*dcbf3bd6SGeorge Wilson HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS)) 969*dcbf3bd6SGeorge Wilson #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \ 970*dcbf3bd6SGeorge Wilson HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp)); 971*dcbf3bd6SGeorge Wilson 972*dcbf3bd6SGeorge Wilson #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL) 973*dcbf3bd6SGeorge Wilson 974e6c728e1Sbrendan /* 975e6c728e1Sbrendan * Other sizes 976e6c728e1Sbrendan */ 977e6c728e1Sbrendan 97889c86e32SChris Williamson #define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 97989c86e32SChris Williamson #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) 980e6c728e1Sbrendan 981fa9e4066Sahrens /* 982fa9e4066Sahrens * Hash table routines 983fa9e4066Sahrens */ 984fa9e4066Sahrens 985fa9e4066Sahrens #define HT_LOCK_PAD 64 986fa9e4066Sahrens 987fa9e4066Sahrens struct ht_lock { 988fa9e4066Sahrens kmutex_t ht_lock; 989fa9e4066Sahrens #ifdef _KERNEL 990fa9e4066Sahrens unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 991fa9e4066Sahrens #endif 992fa9e4066Sahrens }; 993fa9e4066Sahrens 994fa9e4066Sahrens #define BUF_LOCKS 256 995fa9e4066Sahrens typedef struct buf_hash_table { 996fa9e4066Sahrens uint64_t ht_mask; 997fa9e4066Sahrens arc_buf_hdr_t **ht_table; 998fa9e4066Sahrens struct ht_lock ht_locks[BUF_LOCKS]; 999fa9e4066Sahrens } buf_hash_table_t; 1000fa9e4066Sahrens 1001fa9e4066Sahrens static buf_hash_table_t buf_hash_table; 1002fa9e4066Sahrens 1003fa9e4066Sahrens #define BUF_HASH_INDEX(spa, dva, birth) \ 1004fa9e4066Sahrens (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 1005fa9e4066Sahrens #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 1006fa9e4066Sahrens #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 10073f9d6ad7SLin Ling #define HDR_LOCK(hdr) \ 10083f9d6ad7SLin Ling (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 1009fa9e4066Sahrens 1010fa9e4066Sahrens uint64_t zfs_crc64_table[256]; 1011fa9e4066Sahrens 1012fa94a07fSbrendan /* 1013fa94a07fSbrendan * Level 2 ARC 1014fa94a07fSbrendan */ 1015fa94a07fSbrendan 1016fa94a07fSbrendan #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 1017aad02571SSaso Kiselkov #define L2ARC_HEADROOM 2 /* num of writes */ 1018aad02571SSaso Kiselkov /* 1019aad02571SSaso Kiselkov * If we discover during ARC scan any buffers to be compressed, we boost 1020aad02571SSaso Kiselkov * our headroom for the next scanning cycle by this percentage multiple. 1021aad02571SSaso Kiselkov */ 1022aad02571SSaso Kiselkov #define L2ARC_HEADROOM_BOOST 200 10235a98e54bSBrendan Gregg - Sun Microsystems #define L2ARC_FEED_SECS 1 /* caching interval secs */ 10245a98e54bSBrendan Gregg - Sun Microsystems #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 1025fa94a07fSbrendan 1026fa94a07fSbrendan #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 1027fa94a07fSbrendan #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 1028fa94a07fSbrendan 1029f7170741SWill Andrews /* L2ARC Performance Tunables */ 1030fa94a07fSbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 10313a737e0dSbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 1032fa94a07fSbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 1033aad02571SSaso Kiselkov uint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 1034fa94a07fSbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 10355a98e54bSBrendan Gregg - Sun Microsystems uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 1036fa94a07fSbrendan boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 10375a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 10385a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 1039fa94a07fSbrendan 1040fa94a07fSbrendan /* 1041fa94a07fSbrendan * L2ARC Internals 1042fa94a07fSbrendan */ 104389c86e32SChris Williamson struct l2arc_dev { 1044fa94a07fSbrendan vdev_t *l2ad_vdev; /* vdev */ 1045fa94a07fSbrendan spa_t *l2ad_spa; /* spa */ 1046fa94a07fSbrendan uint64_t l2ad_hand; /* next write location */ 1047fa94a07fSbrendan uint64_t l2ad_start; /* first addr on device */ 1048fa94a07fSbrendan uint64_t l2ad_end; /* last addr on device */ 1049fa94a07fSbrendan boolean_t l2ad_first; /* first sweep through */ 10505a98e54bSBrendan Gregg - Sun Microsystems boolean_t l2ad_writing; /* currently writing */ 105189c86e32SChris Williamson kmutex_t l2ad_mtx; /* lock for buffer list */ 105289c86e32SChris Williamson list_t l2ad_buflist; /* buffer list */ 1053fa94a07fSbrendan list_node_t l2ad_node; /* device list node */ 1054a52fc310SPrakash Surya refcount_t l2ad_alloc; /* allocated bytes */ 105589c86e32SChris Williamson }; 1056fa94a07fSbrendan 1057fa94a07fSbrendan static list_t L2ARC_dev_list; /* device list */ 1058fa94a07fSbrendan static list_t *l2arc_dev_list; /* device list pointer */ 1059fa94a07fSbrendan static kmutex_t l2arc_dev_mtx; /* device list mutex */ 1060fa94a07fSbrendan static l2arc_dev_t *l2arc_dev_last; /* last device used */ 1061fa94a07fSbrendan static list_t L2ARC_free_on_write; /* free after write buf list */ 1062fa94a07fSbrendan static list_t *l2arc_free_on_write; /* free after write list ptr */ 1063fa94a07fSbrendan static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 1064fa94a07fSbrendan static uint64_t l2arc_ndev; /* number of devices */ 1065fa94a07fSbrendan 1066fa94a07fSbrendan typedef struct l2arc_read_callback { 1067*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *l2rcb_hdr; /* read buffer */ 1068aad02571SSaso Kiselkov blkptr_t l2rcb_bp; /* original blkptr */ 10697802d7bfSMatthew Ahrens zbookmark_phys_t l2rcb_zb; /* original bookmark */ 1070aad02571SSaso Kiselkov int l2rcb_flags; /* original flags */ 1071fa94a07fSbrendan } l2arc_read_callback_t; 1072fa94a07fSbrendan 1073fa94a07fSbrendan typedef struct l2arc_write_callback { 1074fa94a07fSbrendan l2arc_dev_t *l2wcb_dev; /* device info */ 1075fa94a07fSbrendan arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 1076fa94a07fSbrendan } l2arc_write_callback_t; 1077fa94a07fSbrendan 1078fa94a07fSbrendan typedef struct l2arc_data_free { 1079fa94a07fSbrendan /* protected by l2arc_free_on_write_mtx */ 1080fa94a07fSbrendan void *l2df_data; 1081fa94a07fSbrendan size_t l2df_size; 1082*dcbf3bd6SGeorge Wilson arc_buf_contents_t l2df_type; 1083fa94a07fSbrendan list_node_t l2df_list_node; 1084fa94a07fSbrendan } l2arc_data_free_t; 1085fa94a07fSbrendan 1086fa94a07fSbrendan static kmutex_t l2arc_feed_thr_lock; 1087fa94a07fSbrendan static kcondvar_t l2arc_feed_thr_cv; 1088fa94a07fSbrendan static uint8_t l2arc_thread_exit; 1089fa94a07fSbrendan 1090*dcbf3bd6SGeorge Wilson static void *arc_get_data_buf(arc_buf_hdr_t *, uint64_t, void *); 1091*dcbf3bd6SGeorge Wilson static void arc_free_data_buf(arc_buf_hdr_t *, void *, uint64_t, void *); 1092*dcbf3bd6SGeorge Wilson static void arc_hdr_free_pdata(arc_buf_hdr_t *hdr); 1093*dcbf3bd6SGeorge Wilson static void arc_hdr_alloc_pdata(arc_buf_hdr_t *); 10947adb730bSGeorge Wilson static void arc_access(arc_buf_hdr_t *, kmutex_t *); 1095244781f1SPrakash Surya static boolean_t arc_is_overflowing(); 10967adb730bSGeorge Wilson static void arc_buf_watch(arc_buf_t *); 10977adb730bSGeorge Wilson 109889c86e32SChris Williamson static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); 109989c86e32SChris Williamson static uint32_t arc_bufc_to_flags(arc_buf_contents_t); 1100*dcbf3bd6SGeorge Wilson static inline void arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); 1101*dcbf3bd6SGeorge Wilson static inline void arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); 110289c86e32SChris Williamson 11037adb730bSGeorge Wilson static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); 11047adb730bSGeorge Wilson static void l2arc_read_done(zio_t *); 1105fa94a07fSbrendan 1106fa9e4066Sahrens static uint64_t 1107ac05c741SMark Maybee buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 1108fa9e4066Sahrens { 1109fa9e4066Sahrens uint8_t *vdva = (uint8_t *)dva; 1110fa9e4066Sahrens uint64_t crc = -1ULL; 1111fa9e4066Sahrens int i; 1112fa9e4066Sahrens 1113fa9e4066Sahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 1114fa9e4066Sahrens 1115fa9e4066Sahrens for (i = 0; i < sizeof (dva_t); i++) 1116fa9e4066Sahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 1117fa9e4066Sahrens 1118ac05c741SMark Maybee crc ^= (spa>>8) ^ birth; 1119fa9e4066Sahrens 1120fa9e4066Sahrens return (crc); 1121fa9e4066Sahrens } 1122fa9e4066Sahrens 1123*dcbf3bd6SGeorge Wilson #define HDR_EMPTY(hdr) \ 1124*dcbf3bd6SGeorge Wilson ((hdr)->b_dva.dva_word[0] == 0 && \ 1125*dcbf3bd6SGeorge Wilson (hdr)->b_dva.dva_word[1] == 0) 1126fa9e4066Sahrens 1127*dcbf3bd6SGeorge Wilson #define HDR_EQUAL(spa, dva, birth, hdr) \ 1128*dcbf3bd6SGeorge Wilson ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 1129*dcbf3bd6SGeorge Wilson ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 1130*dcbf3bd6SGeorge Wilson ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa) 1131fa9e4066Sahrens 11323f9d6ad7SLin Ling static void 11333f9d6ad7SLin Ling buf_discard_identity(arc_buf_hdr_t *hdr) 11343f9d6ad7SLin Ling { 11353f9d6ad7SLin Ling hdr->b_dva.dva_word[0] = 0; 11363f9d6ad7SLin Ling hdr->b_dva.dva_word[1] = 0; 11373f9d6ad7SLin Ling hdr->b_birth = 0; 11383f9d6ad7SLin Ling } 11393f9d6ad7SLin Ling 1140fa9e4066Sahrens static arc_buf_hdr_t * 11415d7b4d43SMatthew Ahrens buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 1142fa9e4066Sahrens { 11435d7b4d43SMatthew Ahrens const dva_t *dva = BP_IDENTITY(bp); 11445d7b4d43SMatthew Ahrens uint64_t birth = BP_PHYSICAL_BIRTH(bp); 1145fa9e4066Sahrens uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 1146fa9e4066Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 11477adb730bSGeorge Wilson arc_buf_hdr_t *hdr; 1148fa9e4066Sahrens 1149fa9e4066Sahrens mutex_enter(hash_lock); 11507adb730bSGeorge Wilson for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; 11517adb730bSGeorge Wilson hdr = hdr->b_hash_next) { 1152*dcbf3bd6SGeorge Wilson if (HDR_EQUAL(spa, dva, birth, hdr)) { 1153fa9e4066Sahrens *lockp = hash_lock; 11547adb730bSGeorge Wilson return (hdr); 1155fa9e4066Sahrens } 1156fa9e4066Sahrens } 1157fa9e4066Sahrens mutex_exit(hash_lock); 1158fa9e4066Sahrens *lockp = NULL; 1159fa9e4066Sahrens return (NULL); 1160fa9e4066Sahrens } 1161fa9e4066Sahrens 1162fa9e4066Sahrens /* 1163fa9e4066Sahrens * Insert an entry into the hash table. If there is already an element 1164fa9e4066Sahrens * equal to elem in the hash table, then the already existing element 1165fa9e4066Sahrens * will be returned and the new element will not be inserted. 1166fa9e4066Sahrens * Otherwise returns NULL. 116789c86e32SChris Williamson * If lockp == NULL, the caller is assumed to already hold the hash lock. 1168fa9e4066Sahrens */ 1169fa9e4066Sahrens static arc_buf_hdr_t * 11707adb730bSGeorge Wilson buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) 1171fa9e4066Sahrens { 11727adb730bSGeorge Wilson uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1173fa9e4066Sahrens kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 11747adb730bSGeorge Wilson arc_buf_hdr_t *fhdr; 117544cb6abcSbmc uint32_t i; 1176fa9e4066Sahrens 11777adb730bSGeorge Wilson ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); 11787adb730bSGeorge Wilson ASSERT(hdr->b_birth != 0); 11797adb730bSGeorge Wilson ASSERT(!HDR_IN_HASH_TABLE(hdr)); 118089c86e32SChris Williamson 118189c86e32SChris Williamson if (lockp != NULL) { 118289c86e32SChris Williamson *lockp = hash_lock; 118389c86e32SChris Williamson mutex_enter(hash_lock); 118489c86e32SChris Williamson } else { 118589c86e32SChris Williamson ASSERT(MUTEX_HELD(hash_lock)); 118689c86e32SChris Williamson } 118789c86e32SChris Williamson 11887adb730bSGeorge Wilson for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; 11897adb730bSGeorge Wilson fhdr = fhdr->b_hash_next, i++) { 1190*dcbf3bd6SGeorge Wilson if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) 11917adb730bSGeorge Wilson return (fhdr); 1192fa9e4066Sahrens } 1193fa9e4066Sahrens 11947adb730bSGeorge Wilson hdr->b_hash_next = buf_hash_table.ht_table[idx]; 11957adb730bSGeorge Wilson buf_hash_table.ht_table[idx] = hdr; 1196*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 1197fa9e4066Sahrens 1198fa9e4066Sahrens /* collect some hash table performance data */ 1199fa9e4066Sahrens if (i > 0) { 120044cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_collisions); 1201fa9e4066Sahrens if (i == 1) 120244cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_chains); 120344cb6abcSbmc 120444cb6abcSbmc ARCSTAT_MAX(arcstat_hash_chain_max, i); 1205fa9e4066Sahrens } 120644cb6abcSbmc 120744cb6abcSbmc ARCSTAT_BUMP(arcstat_hash_elements); 120844cb6abcSbmc ARCSTAT_MAXSTAT(arcstat_hash_elements); 1209fa9e4066Sahrens 1210fa9e4066Sahrens return (NULL); 1211fa9e4066Sahrens } 1212fa9e4066Sahrens 1213fa9e4066Sahrens static void 12147adb730bSGeorge Wilson buf_hash_remove(arc_buf_hdr_t *hdr) 1215fa9e4066Sahrens { 12167adb730bSGeorge Wilson arc_buf_hdr_t *fhdr, **hdrp; 12177adb730bSGeorge Wilson uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1218fa9e4066Sahrens 1219fa9e4066Sahrens ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 12207adb730bSGeorge Wilson ASSERT(HDR_IN_HASH_TABLE(hdr)); 1221fa9e4066Sahrens 12227adb730bSGeorge Wilson hdrp = &buf_hash_table.ht_table[idx]; 12237adb730bSGeorge Wilson while ((fhdr = *hdrp) != hdr) { 1224*dcbf3bd6SGeorge Wilson ASSERT3P(fhdr, !=, NULL); 12257adb730bSGeorge Wilson hdrp = &fhdr->b_hash_next; 1226fa9e4066Sahrens } 12277adb730bSGeorge Wilson *hdrp = hdr->b_hash_next; 12287adb730bSGeorge Wilson hdr->b_hash_next = NULL; 1229*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 1230fa9e4066Sahrens 1231fa9e4066Sahrens /* collect some hash table performance data */ 123244cb6abcSbmc ARCSTAT_BUMPDOWN(arcstat_hash_elements); 123344cb6abcSbmc 1234fa9e4066Sahrens if (buf_hash_table.ht_table[idx] && 1235fa9e4066Sahrens buf_hash_table.ht_table[idx]->b_hash_next == NULL) 123644cb6abcSbmc ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1237fa9e4066Sahrens } 1238fa9e4066Sahrens 1239fa9e4066Sahrens /* 1240fa9e4066Sahrens * Global data structures and functions for the buf kmem cache. 1241fa9e4066Sahrens */ 124289c86e32SChris Williamson static kmem_cache_t *hdr_full_cache; 124389c86e32SChris Williamson static kmem_cache_t *hdr_l2only_cache; 1244fa9e4066Sahrens static kmem_cache_t *buf_cache; 1245fa9e4066Sahrens 1246fa9e4066Sahrens static void 1247fa9e4066Sahrens buf_fini(void) 1248fa9e4066Sahrens { 1249fa9e4066Sahrens int i; 1250fa9e4066Sahrens 1251fa9e4066Sahrens kmem_free(buf_hash_table.ht_table, 1252fa9e4066Sahrens (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1253fa9e4066Sahrens for (i = 0; i < BUF_LOCKS; i++) 1254fa9e4066Sahrens mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 125589c86e32SChris Williamson kmem_cache_destroy(hdr_full_cache); 125689c86e32SChris Williamson kmem_cache_destroy(hdr_l2only_cache); 1257fa9e4066Sahrens kmem_cache_destroy(buf_cache); 1258fa9e4066Sahrens } 1259fa9e4066Sahrens 1260fa9e4066Sahrens /* 1261fa9e4066Sahrens * Constructor callback - called when the cache is empty 1262fa9e4066Sahrens * and a new buf is requested. 1263fa9e4066Sahrens */ 1264fa9e4066Sahrens /* ARGSUSED */ 1265fa9e4066Sahrens static int 126689c86e32SChris Williamson hdr_full_cons(void *vbuf, void *unused, int kmflag) 1267fa9e4066Sahrens { 12687adb730bSGeorge Wilson arc_buf_hdr_t *hdr = vbuf; 1269fa9e4066Sahrens 127089c86e32SChris Williamson bzero(hdr, HDR_FULL_SIZE); 127189c86e32SChris Williamson cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); 127289c86e32SChris Williamson refcount_create(&hdr->b_l1hdr.b_refcnt); 127389c86e32SChris Williamson mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1274244781f1SPrakash Surya multilist_link_init(&hdr->b_l1hdr.b_arc_node); 127589c86e32SChris Williamson arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); 127689c86e32SChris Williamson 127789c86e32SChris Williamson return (0); 127889c86e32SChris Williamson } 127989c86e32SChris Williamson 128089c86e32SChris Williamson /* ARGSUSED */ 128189c86e32SChris Williamson static int 128289c86e32SChris Williamson hdr_l2only_cons(void *vbuf, void *unused, int kmflag) 128389c86e32SChris Williamson { 128489c86e32SChris Williamson arc_buf_hdr_t *hdr = vbuf; 128589c86e32SChris Williamson 128689c86e32SChris Williamson bzero(hdr, HDR_L2ONLY_SIZE); 128789c86e32SChris Williamson arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1288fa94a07fSbrendan 1289fa9e4066Sahrens return (0); 1290fa9e4066Sahrens } 1291fa9e4066Sahrens 12926f83844dSMark Maybee /* ARGSUSED */ 12936f83844dSMark Maybee static int 12946f83844dSMark Maybee buf_cons(void *vbuf, void *unused, int kmflag) 12956f83844dSMark Maybee { 12966f83844dSMark Maybee arc_buf_t *buf = vbuf; 12976f83844dSMark Maybee 12986f83844dSMark Maybee bzero(buf, sizeof (arc_buf_t)); 12993f9d6ad7SLin Ling mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 13005a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 13015a98e54bSBrendan Gregg - Sun Microsystems 13026f83844dSMark Maybee return (0); 13036f83844dSMark Maybee } 13046f83844dSMark Maybee 1305fa9e4066Sahrens /* 1306fa9e4066Sahrens * Destructor callback - called when a cached buf is 1307fa9e4066Sahrens * no longer required. 1308fa9e4066Sahrens */ 1309fa9e4066Sahrens /* ARGSUSED */ 1310fa9e4066Sahrens static void 131189c86e32SChris Williamson hdr_full_dest(void *vbuf, void *unused) 131289c86e32SChris Williamson { 131389c86e32SChris Williamson arc_buf_hdr_t *hdr = vbuf; 131489c86e32SChris Williamson 1315*dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 131689c86e32SChris Williamson cv_destroy(&hdr->b_l1hdr.b_cv); 131789c86e32SChris Williamson refcount_destroy(&hdr->b_l1hdr.b_refcnt); 131889c86e32SChris Williamson mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); 1319244781f1SPrakash Surya ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 132089c86e32SChris Williamson arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); 132189c86e32SChris Williamson } 132289c86e32SChris Williamson 132389c86e32SChris Williamson /* ARGSUSED */ 132489c86e32SChris Williamson static void 132589c86e32SChris Williamson hdr_l2only_dest(void *vbuf, void *unused) 1326fa9e4066Sahrens { 13277adb730bSGeorge Wilson arc_buf_hdr_t *hdr = vbuf; 1328fa9e4066Sahrens 1329*dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 133089c86e32SChris Williamson arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1331fa9e4066Sahrens } 1332fa9e4066Sahrens 13336f83844dSMark Maybee /* ARGSUSED */ 13346f83844dSMark Maybee static void 13356f83844dSMark Maybee buf_dest(void *vbuf, void *unused) 13366f83844dSMark Maybee { 13376f83844dSMark Maybee arc_buf_t *buf = vbuf; 13386f83844dSMark Maybee 13393f9d6ad7SLin Ling mutex_destroy(&buf->b_evict_lock); 13405a98e54bSBrendan Gregg - Sun Microsystems arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 13416f83844dSMark Maybee } 13426f83844dSMark Maybee 1343fa9e4066Sahrens /* 1344fa9e4066Sahrens * Reclaim callback -- invoked when memory is low. 1345fa9e4066Sahrens */ 1346fa9e4066Sahrens /* ARGSUSED */ 1347fa9e4066Sahrens static void 1348fa9e4066Sahrens hdr_recl(void *unused) 1349fa9e4066Sahrens { 1350fa9e4066Sahrens dprintf("hdr_recl called\n"); 135149e3519aSmaybee /* 135249e3519aSmaybee * umem calls the reclaim func when we destroy the buf cache, 135349e3519aSmaybee * which is after we do arc_fini(). 135449e3519aSmaybee */ 135549e3519aSmaybee if (!arc_dead) 1356244781f1SPrakash Surya cv_signal(&arc_reclaim_thread_cv); 1357fa9e4066Sahrens } 1358fa9e4066Sahrens 1359fa9e4066Sahrens static void 1360fa9e4066Sahrens buf_init(void) 1361fa9e4066Sahrens { 1362fa9e4066Sahrens uint64_t *ct; 1363ea8dc4b6Seschrock uint64_t hsize = 1ULL << 12; 1364fa9e4066Sahrens int i, j; 1365fa9e4066Sahrens 1366fa9e4066Sahrens /* 1367fa9e4066Sahrens * The hash table is big enough to fill all of physical memory 136863e911b6SMatthew Ahrens * with an average block size of zfs_arc_average_blocksize (default 8K). 136963e911b6SMatthew Ahrens * By default, the table will take up 137063e911b6SMatthew Ahrens * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1371fa9e4066Sahrens */ 137263e911b6SMatthew Ahrens while (hsize * zfs_arc_average_blocksize < physmem * PAGESIZE) 1373fa9e4066Sahrens hsize <<= 1; 1374ea8dc4b6Seschrock retry: 1375fa9e4066Sahrens buf_hash_table.ht_mask = hsize - 1; 1376ea8dc4b6Seschrock buf_hash_table.ht_table = 1377ea8dc4b6Seschrock kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1378ea8dc4b6Seschrock if (buf_hash_table.ht_table == NULL) { 1379ea8dc4b6Seschrock ASSERT(hsize > (1ULL << 8)); 1380ea8dc4b6Seschrock hsize >>= 1; 1381ea8dc4b6Seschrock goto retry; 1382ea8dc4b6Seschrock } 1383fa9e4066Sahrens 138489c86e32SChris Williamson hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, 138589c86e32SChris Williamson 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0); 138689c86e32SChris Williamson hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", 138789c86e32SChris Williamson HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl, 138889c86e32SChris Williamson NULL, NULL, 0); 1389fa9e4066Sahrens buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 13906f83844dSMark Maybee 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1391fa9e4066Sahrens 1392fa9e4066Sahrens for (i = 0; i < 256; i++) 1393fa9e4066Sahrens for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1394fa9e4066Sahrens *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1395fa9e4066Sahrens 1396fa9e4066Sahrens for (i = 0; i < BUF_LOCKS; i++) { 1397fa9e4066Sahrens mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1398fa9e4066Sahrens NULL, MUTEX_DEFAULT, NULL); 1399fa9e4066Sahrens } 1400fa9e4066Sahrens } 1401fa9e4066Sahrens 1402*dcbf3bd6SGeorge Wilson #define ARC_MINTIME (hz>>4) /* 62 ms */ 1403244781f1SPrakash Surya 1404*dcbf3bd6SGeorge Wilson static inline boolean_t 1405*dcbf3bd6SGeorge Wilson arc_buf_is_shared(arc_buf_t *buf) 1406*dcbf3bd6SGeorge Wilson { 1407*dcbf3bd6SGeorge Wilson boolean_t shared = (buf->b_data != NULL && 1408*dcbf3bd6SGeorge Wilson buf->b_data == buf->b_hdr->b_l1hdr.b_pdata); 1409*dcbf3bd6SGeorge Wilson IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr)); 1410*dcbf3bd6SGeorge Wilson return (shared); 1411*dcbf3bd6SGeorge Wilson } 1412c546f36aSArne Jansen 1413*dcbf3bd6SGeorge Wilson static inline void 1414*dcbf3bd6SGeorge Wilson arc_cksum_free(arc_buf_hdr_t *hdr) 1415*dcbf3bd6SGeorge Wilson { 1416*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1417*dcbf3bd6SGeorge Wilson mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1418*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_freeze_cksum != NULL) { 1419*dcbf3bd6SGeorge Wilson kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t)); 1420*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_freeze_cksum = NULL; 142189c86e32SChris Williamson } 1422*dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 142389c86e32SChris Williamson } 142489c86e32SChris Williamson 14256b4acc8bSahrens static void 14266b4acc8bSahrens arc_cksum_verify(arc_buf_t *buf) 14276b4acc8bSahrens { 1428*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 14296b4acc8bSahrens zio_cksum_t zc; 14306b4acc8bSahrens 1431cc60fd72Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 14326b4acc8bSahrens return; 14336b4acc8bSahrens 1434*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1435*dcbf3bd6SGeorge Wilson 1436*dcbf3bd6SGeorge Wilson mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1437*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) { 1438*dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 14396b4acc8bSahrens return; 14406b4acc8bSahrens } 1441*dcbf3bd6SGeorge Wilson fletcher_2_native(buf->b_data, HDR_GET_LSIZE(hdr), NULL, &zc); 1442*dcbf3bd6SGeorge Wilson if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc)) 14436b4acc8bSahrens panic("buffer modified while frozen!"); 1444*dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 14456b4acc8bSahrens } 14466b4acc8bSahrens 1447*dcbf3bd6SGeorge Wilson static boolean_t 1448*dcbf3bd6SGeorge Wilson arc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio) 1449fa94a07fSbrendan { 1450*dcbf3bd6SGeorge Wilson enum zio_compress compress = BP_GET_COMPRESS(zio->io_bp); 1451*dcbf3bd6SGeorge Wilson boolean_t valid_cksum; 1452fa94a07fSbrendan 1453*dcbf3bd6SGeorge Wilson ASSERT(!BP_IS_EMBEDDED(zio->io_bp)); 1454*dcbf3bd6SGeorge Wilson VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr)); 1455*dcbf3bd6SGeorge Wilson 1456*dcbf3bd6SGeorge Wilson /* 1457*dcbf3bd6SGeorge Wilson * We rely on the blkptr's checksum to determine if the block 1458*dcbf3bd6SGeorge Wilson * is valid or not. When compressed arc is enabled, the l2arc 1459*dcbf3bd6SGeorge Wilson * writes the block to the l2arc just as it appears in the pool. 1460*dcbf3bd6SGeorge Wilson * This allows us to use the blkptr's checksum to validate the 1461*dcbf3bd6SGeorge Wilson * data that we just read off of the l2arc without having to store 1462*dcbf3bd6SGeorge Wilson * a separate checksum in the arc_buf_hdr_t. However, if compressed 1463*dcbf3bd6SGeorge Wilson * arc is disabled, then the data written to the l2arc is always 1464*dcbf3bd6SGeorge Wilson * uncompressed and won't match the block as it exists in the main 1465*dcbf3bd6SGeorge Wilson * pool. When this is the case, we must first compress it if it is 1466*dcbf3bd6SGeorge Wilson * compressed on the main pool before we can validate the checksum. 1467*dcbf3bd6SGeorge Wilson */ 1468*dcbf3bd6SGeorge Wilson if (!HDR_COMPRESSION_ENABLED(hdr) && compress != ZIO_COMPRESS_OFF) { 1469*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 1470*dcbf3bd6SGeorge Wilson uint64_t lsize = HDR_GET_LSIZE(hdr); 1471*dcbf3bd6SGeorge Wilson uint64_t csize; 1472*dcbf3bd6SGeorge Wilson 1473*dcbf3bd6SGeorge Wilson void *cbuf = zio_buf_alloc(HDR_GET_PSIZE(hdr)); 1474*dcbf3bd6SGeorge Wilson csize = zio_compress_data(compress, zio->io_data, cbuf, lsize); 1475*dcbf3bd6SGeorge Wilson ASSERT3U(csize, <=, HDR_GET_PSIZE(hdr)); 1476*dcbf3bd6SGeorge Wilson if (csize < HDR_GET_PSIZE(hdr)) { 1477*dcbf3bd6SGeorge Wilson /* 1478*dcbf3bd6SGeorge Wilson * Compressed blocks are always a multiple of the 1479*dcbf3bd6SGeorge Wilson * smallest ashift in the pool. Ideally, we would 1480*dcbf3bd6SGeorge Wilson * like to round up the csize to the next 1481*dcbf3bd6SGeorge Wilson * spa_min_ashift but that value may have changed 1482*dcbf3bd6SGeorge Wilson * since the block was last written. Instead, 1483*dcbf3bd6SGeorge Wilson * we rely on the fact that the hdr's psize 1484*dcbf3bd6SGeorge Wilson * was set to the psize of the block when it was 1485*dcbf3bd6SGeorge Wilson * last written. We set the csize to that value 1486*dcbf3bd6SGeorge Wilson * and zero out any part that should not contain 1487*dcbf3bd6SGeorge Wilson * data. 1488*dcbf3bd6SGeorge Wilson */ 1489*dcbf3bd6SGeorge Wilson bzero((char *)cbuf + csize, HDR_GET_PSIZE(hdr) - csize); 1490*dcbf3bd6SGeorge Wilson csize = HDR_GET_PSIZE(hdr); 1491*dcbf3bd6SGeorge Wilson } 1492*dcbf3bd6SGeorge Wilson zio_push_transform(zio, cbuf, csize, HDR_GET_PSIZE(hdr), NULL); 1493*dcbf3bd6SGeorge Wilson } 1494fa94a07fSbrendan 1495*dcbf3bd6SGeorge Wilson /* 1496*dcbf3bd6SGeorge Wilson * Block pointers always store the checksum for the logical data. 1497*dcbf3bd6SGeorge Wilson * If the block pointer has the gang bit set, then the checksum 1498*dcbf3bd6SGeorge Wilson * it represents is for the reconstituted data and not for an 1499*dcbf3bd6SGeorge Wilson * individual gang member. The zio pipeline, however, must be able to 1500*dcbf3bd6SGeorge Wilson * determine the checksum of each of the gang constituents so it 1501*dcbf3bd6SGeorge Wilson * treats the checksum comparison differently than what we need 1502*dcbf3bd6SGeorge Wilson * for l2arc blocks. This prevents us from using the 1503*dcbf3bd6SGeorge Wilson * zio_checksum_error() interface directly. Instead we must call the 1504*dcbf3bd6SGeorge Wilson * zio_checksum_error_impl() so that we can ensure the checksum is 1505*dcbf3bd6SGeorge Wilson * generated using the correct checksum algorithm and accounts for the 1506*dcbf3bd6SGeorge Wilson * logical I/O size and not just a gang fragment. 1507*dcbf3bd6SGeorge Wilson */ 1508*dcbf3bd6SGeorge Wilson valid_cksum = (zio_checksum_error_impl(zio->io_spa, zio->io_bp, 1509*dcbf3bd6SGeorge Wilson BP_GET_CHECKSUM(zio->io_bp), zio->io_data, zio->io_size, 1510*dcbf3bd6SGeorge Wilson zio->io_offset, NULL) == 0); 1511*dcbf3bd6SGeorge Wilson zio_pop_transforms(zio); 1512*dcbf3bd6SGeorge Wilson return (valid_cksum); 1513fa94a07fSbrendan } 1514fa94a07fSbrendan 15156b4acc8bSahrens static void 1516*dcbf3bd6SGeorge Wilson arc_cksum_compute(arc_buf_t *buf) 15176b4acc8bSahrens { 1518*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 1519*dcbf3bd6SGeorge Wilson 1520*dcbf3bd6SGeorge Wilson if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 15216b4acc8bSahrens return; 15226b4acc8bSahrens 1523*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 152489c86e32SChris Williamson mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1525*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_freeze_cksum != NULL) { 1526*dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 15276b4acc8bSahrens return; 15286b4acc8bSahrens } 1529*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), 1530*dcbf3bd6SGeorge Wilson KM_SLEEP); 1531*dcbf3bd6SGeorge Wilson fletcher_2_native(buf->b_data, HDR_GET_LSIZE(hdr), NULL, 1532*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_freeze_cksum); 1533*dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1534cd1c8b85SMatthew Ahrens arc_buf_watch(buf); 1535cd1c8b85SMatthew Ahrens } 1536cd1c8b85SMatthew Ahrens 1537cd1c8b85SMatthew Ahrens #ifndef _KERNEL 1538cd1c8b85SMatthew Ahrens typedef struct procctl { 1539cd1c8b85SMatthew Ahrens long cmd; 1540cd1c8b85SMatthew Ahrens prwatch_t prwatch; 1541cd1c8b85SMatthew Ahrens } procctl_t; 1542cd1c8b85SMatthew Ahrens #endif 1543cd1c8b85SMatthew Ahrens 1544cd1c8b85SMatthew Ahrens /* ARGSUSED */ 1545cd1c8b85SMatthew Ahrens static void 1546cd1c8b85SMatthew Ahrens arc_buf_unwatch(arc_buf_t *buf) 1547cd1c8b85SMatthew Ahrens { 1548cd1c8b85SMatthew Ahrens #ifndef _KERNEL 1549cd1c8b85SMatthew Ahrens if (arc_watch) { 1550cd1c8b85SMatthew Ahrens int result; 1551cd1c8b85SMatthew Ahrens procctl_t ctl; 1552cd1c8b85SMatthew Ahrens ctl.cmd = PCWATCH; 1553cd1c8b85SMatthew Ahrens ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1554cd1c8b85SMatthew Ahrens ctl.prwatch.pr_size = 0; 1555cd1c8b85SMatthew Ahrens ctl.prwatch.pr_wflags = 0; 1556cd1c8b85SMatthew Ahrens result = write(arc_procfd, &ctl, sizeof (ctl)); 1557cd1c8b85SMatthew Ahrens ASSERT3U(result, ==, sizeof (ctl)); 1558cd1c8b85SMatthew Ahrens } 1559cd1c8b85SMatthew Ahrens #endif 1560cd1c8b85SMatthew Ahrens } 1561cd1c8b85SMatthew Ahrens 1562cd1c8b85SMatthew Ahrens /* ARGSUSED */ 1563cd1c8b85SMatthew Ahrens static void 1564cd1c8b85SMatthew Ahrens arc_buf_watch(arc_buf_t *buf) 1565cd1c8b85SMatthew Ahrens { 1566cd1c8b85SMatthew Ahrens #ifndef _KERNEL 1567cd1c8b85SMatthew Ahrens if (arc_watch) { 1568cd1c8b85SMatthew Ahrens int result; 1569cd1c8b85SMatthew Ahrens procctl_t ctl; 1570cd1c8b85SMatthew Ahrens ctl.cmd = PCWATCH; 1571cd1c8b85SMatthew Ahrens ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1572*dcbf3bd6SGeorge Wilson ctl.prwatch.pr_size = HDR_GET_LSIZE(buf->b_hdr); 1573cd1c8b85SMatthew Ahrens ctl.prwatch.pr_wflags = WA_WRITE; 1574cd1c8b85SMatthew Ahrens result = write(arc_procfd, &ctl, sizeof (ctl)); 1575cd1c8b85SMatthew Ahrens ASSERT3U(result, ==, sizeof (ctl)); 1576cd1c8b85SMatthew Ahrens } 1577cd1c8b85SMatthew Ahrens #endif 15786b4acc8bSahrens } 15796b4acc8bSahrens 158089c86e32SChris Williamson static arc_buf_contents_t 158189c86e32SChris Williamson arc_buf_type(arc_buf_hdr_t *hdr) 158289c86e32SChris Williamson { 1583*dcbf3bd6SGeorge Wilson arc_buf_contents_t type; 158489c86e32SChris Williamson if (HDR_ISTYPE_METADATA(hdr)) { 1585*dcbf3bd6SGeorge Wilson type = ARC_BUFC_METADATA; 158689c86e32SChris Williamson } else { 1587*dcbf3bd6SGeorge Wilson type = ARC_BUFC_DATA; 158889c86e32SChris Williamson } 1589*dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 1590*dcbf3bd6SGeorge Wilson return (type); 159189c86e32SChris Williamson } 159289c86e32SChris Williamson 159389c86e32SChris Williamson static uint32_t 159489c86e32SChris Williamson arc_bufc_to_flags(arc_buf_contents_t type) 159589c86e32SChris Williamson { 159689c86e32SChris Williamson switch (type) { 159789c86e32SChris Williamson case ARC_BUFC_DATA: 159889c86e32SChris Williamson /* metadata field is 0 if buffer contains normal data */ 159989c86e32SChris Williamson return (0); 160089c86e32SChris Williamson case ARC_BUFC_METADATA: 160189c86e32SChris Williamson return (ARC_FLAG_BUFC_METADATA); 160289c86e32SChris Williamson default: 160389c86e32SChris Williamson break; 160489c86e32SChris Williamson } 160589c86e32SChris Williamson panic("undefined ARC buffer type!"); 160689c86e32SChris Williamson return ((uint32_t)-1); 160789c86e32SChris Williamson } 160889c86e32SChris Williamson 16096b4acc8bSahrens void 16106b4acc8bSahrens arc_buf_thaw(arc_buf_t *buf) 16116b4acc8bSahrens { 1612*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 1613*dcbf3bd6SGeorge Wilson 1614fa94a07fSbrendan if (zfs_flags & ZFS_DEBUG_MODIFY) { 1615*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_state != arc_anon) 1616fa94a07fSbrendan panic("modifying non-anon buffer!"); 1617*dcbf3bd6SGeorge Wilson if (HDR_IO_IN_PROGRESS(hdr)) 1618fa94a07fSbrendan panic("modifying buffer while i/o in progress!"); 1619fa94a07fSbrendan arc_cksum_verify(buf); 1620fa94a07fSbrendan } 16216b4acc8bSahrens 1622*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1623*dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 16243f9d6ad7SLin Ling 1625*dcbf3bd6SGeorge Wilson mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 162689c86e32SChris Williamson #ifdef ZFS_DEBUG 16273f9d6ad7SLin Ling if (zfs_flags & ZFS_DEBUG_MODIFY) { 1628*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_thawed != NULL) 1629*dcbf3bd6SGeorge Wilson kmem_free(hdr->b_l1hdr.b_thawed, 1); 1630*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_thawed = kmem_alloc(1, KM_SLEEP); 16313f9d6ad7SLin Ling } 163289c86e32SChris Williamson #endif 16333f9d6ad7SLin Ling 1634*dcbf3bd6SGeorge Wilson mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1635cd1c8b85SMatthew Ahrens 1636cd1c8b85SMatthew Ahrens arc_buf_unwatch(buf); 16376b4acc8bSahrens } 16386b4acc8bSahrens 16396b4acc8bSahrens void 16406b4acc8bSahrens arc_buf_freeze(arc_buf_t *buf) 16416b4acc8bSahrens { 1642*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 16433f9d6ad7SLin Ling kmutex_t *hash_lock; 16443f9d6ad7SLin Ling 1645cc60fd72Sahrens if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1646cc60fd72Sahrens return; 1647cc60fd72Sahrens 1648*dcbf3bd6SGeorge Wilson hash_lock = HDR_LOCK(hdr); 16493f9d6ad7SLin Ling mutex_enter(hash_lock); 16503f9d6ad7SLin Ling 1651*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1652*dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_freeze_cksum != NULL || 1653*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_state == arc_anon); 1654*dcbf3bd6SGeorge Wilson arc_cksum_compute(buf); 16553f9d6ad7SLin Ling mutex_exit(hash_lock); 1656cd1c8b85SMatthew Ahrens 16576b4acc8bSahrens } 16586b4acc8bSahrens 1659*dcbf3bd6SGeorge Wilson /* 1660*dcbf3bd6SGeorge Wilson * The arc_buf_hdr_t's b_flags should never be modified directly. Instead, 1661*dcbf3bd6SGeorge Wilson * the following functions should be used to ensure that the flags are 1662*dcbf3bd6SGeorge Wilson * updated in a thread-safe way. When manipulating the flags either 1663*dcbf3bd6SGeorge Wilson * the hash_lock must be held or the hdr must be undiscoverable. This 1664*dcbf3bd6SGeorge Wilson * ensures that we're not racing with any other threads when updating 1665*dcbf3bd6SGeorge Wilson * the flags. 1666*dcbf3bd6SGeorge Wilson */ 1667*dcbf3bd6SGeorge Wilson static inline void 1668*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) 1669*dcbf3bd6SGeorge Wilson { 1670*dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 1671*dcbf3bd6SGeorge Wilson hdr->b_flags |= flags; 1672*dcbf3bd6SGeorge Wilson } 1673*dcbf3bd6SGeorge Wilson 1674*dcbf3bd6SGeorge Wilson static inline void 1675*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) 1676*dcbf3bd6SGeorge Wilson { 1677*dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 1678*dcbf3bd6SGeorge Wilson hdr->b_flags &= ~flags; 1679*dcbf3bd6SGeorge Wilson } 1680*dcbf3bd6SGeorge Wilson 1681*dcbf3bd6SGeorge Wilson /* 1682*dcbf3bd6SGeorge Wilson * Setting the compression bits in the arc_buf_hdr_t's b_flags is 1683*dcbf3bd6SGeorge Wilson * done in a special way since we have to clear and set bits 1684*dcbf3bd6SGeorge Wilson * at the same time. Consumers that wish to set the compression bits 1685*dcbf3bd6SGeorge Wilson * must use this function to ensure that the flags are updated in 1686*dcbf3bd6SGeorge Wilson * thread-safe manner. 1687*dcbf3bd6SGeorge Wilson */ 1688fa9e4066Sahrens static void 1689*dcbf3bd6SGeorge Wilson arc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp) 1690fa9e4066Sahrens { 1691*dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 1692fa9e4066Sahrens 1693*dcbf3bd6SGeorge Wilson /* 1694*dcbf3bd6SGeorge Wilson * Holes and embedded blocks will always have a psize = 0 so 1695*dcbf3bd6SGeorge Wilson * we ignore the compression of the blkptr and set the 1696*dcbf3bd6SGeorge Wilson * arc_buf_hdr_t's compression to ZIO_COMPRESS_OFF. 1697*dcbf3bd6SGeorge Wilson * Holes and embedded blocks remain anonymous so we don't 1698*dcbf3bd6SGeorge Wilson * want to uncompress them. Mark them as uncompressed. 1699*dcbf3bd6SGeorge Wilson */ 1700*dcbf3bd6SGeorge Wilson if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) { 1701*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC); 1702*dcbf3bd6SGeorge Wilson HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); 1703*dcbf3bd6SGeorge Wilson ASSERT(!HDR_COMPRESSION_ENABLED(hdr)); 1704*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 1705*dcbf3bd6SGeorge Wilson } else { 1706*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC); 1707*dcbf3bd6SGeorge Wilson HDR_SET_COMPRESS(hdr, cmp); 1708*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp); 1709*dcbf3bd6SGeorge Wilson ASSERT(HDR_COMPRESSION_ENABLED(hdr)); 1710*dcbf3bd6SGeorge Wilson } 1711*dcbf3bd6SGeorge Wilson } 1712244781f1SPrakash Surya 1713*dcbf3bd6SGeorge Wilson static int 1714*dcbf3bd6SGeorge Wilson arc_decompress(arc_buf_t *buf) 1715*dcbf3bd6SGeorge Wilson { 1716*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 1717*dcbf3bd6SGeorge Wilson dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap; 1718*dcbf3bd6SGeorge Wilson int error; 171989c86e32SChris Williamson 1720*dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 1721*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 1722*dcbf3bd6SGeorge Wilson } else if (HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) { 1723*dcbf3bd6SGeorge Wilson /* 1724*dcbf3bd6SGeorge Wilson * The arc_buf_hdr_t is either not compressed or is 1725*dcbf3bd6SGeorge Wilson * associated with an embedded block or a hole in which 1726*dcbf3bd6SGeorge Wilson * case they remain anonymous. 1727*dcbf3bd6SGeorge Wilson */ 1728*dcbf3bd6SGeorge Wilson IMPLY(HDR_COMPRESSION_ENABLED(hdr), HDR_GET_PSIZE(hdr) == 0 || 1729*dcbf3bd6SGeorge Wilson HDR_GET_PSIZE(hdr) == HDR_GET_LSIZE(hdr)); 1730*dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 1731*dcbf3bd6SGeorge Wilson bcopy(hdr->b_l1hdr.b_pdata, buf->b_data, HDR_GET_LSIZE(hdr)); 1732*dcbf3bd6SGeorge Wilson } else { 1733*dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 1734*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), !=, HDR_GET_PSIZE(hdr)); 1735*dcbf3bd6SGeorge Wilson error = zio_decompress_data(HDR_GET_COMPRESS(hdr), 1736*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_pdata, buf->b_data, HDR_GET_PSIZE(hdr), 1737*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr)); 1738*dcbf3bd6SGeorge Wilson if (error != 0) { 1739*dcbf3bd6SGeorge Wilson zfs_dbgmsg("hdr %p, compress %d, psize %d, lsize %d", 1740*dcbf3bd6SGeorge Wilson hdr, HDR_GET_COMPRESS(hdr), HDR_GET_PSIZE(hdr), 1741*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr)); 1742*dcbf3bd6SGeorge Wilson return (SET_ERROR(EIO)); 1743ea8dc4b6Seschrock } 1744fa9e4066Sahrens } 1745*dcbf3bd6SGeorge Wilson if (bswap != DMU_BSWAP_NUMFUNCS) { 1746*dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 1747*dcbf3bd6SGeorge Wilson ASSERT3U(bswap, <, DMU_BSWAP_NUMFUNCS); 1748*dcbf3bd6SGeorge Wilson dmu_ot_byteswap[bswap].ob_func(buf->b_data, HDR_GET_LSIZE(hdr)); 1749*dcbf3bd6SGeorge Wilson } 1750*dcbf3bd6SGeorge Wilson arc_cksum_compute(buf); 1751*dcbf3bd6SGeorge Wilson return (0); 1752fa9e4066Sahrens } 1753fa9e4066Sahrens 1754*dcbf3bd6SGeorge Wilson /* 1755*dcbf3bd6SGeorge Wilson * Return the size of the block, b_pdata, that is stored in the arc_buf_hdr_t. 1756*dcbf3bd6SGeorge Wilson */ 1757*dcbf3bd6SGeorge Wilson static uint64_t 1758*dcbf3bd6SGeorge Wilson arc_hdr_size(arc_buf_hdr_t *hdr) 1759fa9e4066Sahrens { 1760*dcbf3bd6SGeorge Wilson uint64_t size; 1761fa9e4066Sahrens 1762*dcbf3bd6SGeorge Wilson if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && 1763*dcbf3bd6SGeorge Wilson HDR_GET_PSIZE(hdr) > 0) { 1764*dcbf3bd6SGeorge Wilson size = HDR_GET_PSIZE(hdr); 1765*dcbf3bd6SGeorge Wilson } else { 1766*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0); 1767*dcbf3bd6SGeorge Wilson size = HDR_GET_LSIZE(hdr); 1768*dcbf3bd6SGeorge Wilson } 1769*dcbf3bd6SGeorge Wilson return (size); 1770*dcbf3bd6SGeorge Wilson } 1771fa9e4066Sahrens 1772*dcbf3bd6SGeorge Wilson /* 1773*dcbf3bd6SGeorge Wilson * Increment the amount of evictable space in the arc_state_t's refcount. 1774*dcbf3bd6SGeorge Wilson * We account for the space used by the hdr and the arc buf individually 1775*dcbf3bd6SGeorge Wilson * so that we can add and remove them from the refcount individually. 1776*dcbf3bd6SGeorge Wilson */ 1777*dcbf3bd6SGeorge Wilson static void 1778*dcbf3bd6SGeorge Wilson arc_evictable_space_increment(arc_buf_hdr_t *hdr, arc_state_t *state) 1779*dcbf3bd6SGeorge Wilson { 1780*dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 1781*dcbf3bd6SGeorge Wilson uint64_t lsize = HDR_GET_LSIZE(hdr); 1782244781f1SPrakash Surya 1783*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 17840e8c6158Smaybee 1785*dcbf3bd6SGeorge Wilson if (GHOST_STATE(state)) { 1786*dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 1787*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 1788*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 1789*dcbf3bd6SGeorge Wilson (void) refcount_add_many(&state->arcs_esize[type], lsize, hdr); 1790*dcbf3bd6SGeorge Wilson return; 1791*dcbf3bd6SGeorge Wilson } 1792*dcbf3bd6SGeorge Wilson 1793*dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(state)); 1794*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_pdata != NULL) { 1795*dcbf3bd6SGeorge Wilson (void) refcount_add_many(&state->arcs_esize[type], 1796*dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 1797*dcbf3bd6SGeorge Wilson } 1798*dcbf3bd6SGeorge Wilson for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 1799*dcbf3bd6SGeorge Wilson buf = buf->b_next) { 1800*dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 1801*dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(buf)); 1802*dcbf3bd6SGeorge Wilson continue; 1803*dcbf3bd6SGeorge Wilson } 1804*dcbf3bd6SGeorge Wilson (void) refcount_add_many(&state->arcs_esize[type], lsize, buf); 1805fa9e4066Sahrens } 1806fa9e4066Sahrens } 1807fa9e4066Sahrens 1808fa9e4066Sahrens /* 1809*dcbf3bd6SGeorge Wilson * Decrement the amount of evictable space in the arc_state_t's refcount. 1810*dcbf3bd6SGeorge Wilson * We account for the space used by the hdr and the arc buf individually 1811*dcbf3bd6SGeorge Wilson * so that we can add and remove them from the refcount individually. 1812fa9e4066Sahrens */ 1813fa9e4066Sahrens static void 1814*dcbf3bd6SGeorge Wilson arc_evitable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state) 1815fa9e4066Sahrens { 1816*dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 1817*dcbf3bd6SGeorge Wilson uint64_t lsize = HDR_GET_LSIZE(hdr); 1818*dcbf3bd6SGeorge Wilson 1819*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1820*dcbf3bd6SGeorge Wilson 1821*dcbf3bd6SGeorge Wilson if (GHOST_STATE(state)) { 1822*dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 1823*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 1824*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 1825*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 1826*dcbf3bd6SGeorge Wilson lsize, hdr); 1827*dcbf3bd6SGeorge Wilson return; 1828*dcbf3bd6SGeorge Wilson } 1829*dcbf3bd6SGeorge Wilson 1830*dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(state)); 1831*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_pdata != NULL) { 1832*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 1833*dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 1834*dcbf3bd6SGeorge Wilson } 1835*dcbf3bd6SGeorge Wilson for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 1836*dcbf3bd6SGeorge Wilson buf = buf->b_next) { 1837*dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 1838*dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(buf)); 1839*dcbf3bd6SGeorge Wilson continue; 1840*dcbf3bd6SGeorge Wilson } 1841*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 1842*dcbf3bd6SGeorge Wilson lsize, buf); 1843*dcbf3bd6SGeorge Wilson } 1844*dcbf3bd6SGeorge Wilson } 1845*dcbf3bd6SGeorge Wilson 1846*dcbf3bd6SGeorge Wilson /* 1847*dcbf3bd6SGeorge Wilson * Add a reference to this hdr indicating that someone is actively 1848*dcbf3bd6SGeorge Wilson * referencing that memory. When the refcount transitions from 0 to 1, 1849*dcbf3bd6SGeorge Wilson * we remove it from the respective arc_state_t list to indicate that 1850*dcbf3bd6SGeorge Wilson * it is not evictable. 1851*dcbf3bd6SGeorge Wilson */ 1852*dcbf3bd6SGeorge Wilson static void 1853*dcbf3bd6SGeorge Wilson add_reference(arc_buf_hdr_t *hdr, void *tag) 1854*dcbf3bd6SGeorge Wilson { 1855*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1856*dcbf3bd6SGeorge Wilson if (!MUTEX_HELD(HDR_LOCK(hdr))) { 1857*dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_state == arc_anon); 1858*dcbf3bd6SGeorge Wilson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 1859*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 1860*dcbf3bd6SGeorge Wilson } 1861*dcbf3bd6SGeorge Wilson 1862*dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 1863*dcbf3bd6SGeorge Wilson 1864*dcbf3bd6SGeorge Wilson if ((refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && 1865*dcbf3bd6SGeorge Wilson (state != arc_anon)) { 1866*dcbf3bd6SGeorge Wilson /* We don't use the L2-only state list. */ 1867*dcbf3bd6SGeorge Wilson if (state != arc_l2c_only) { 1868*dcbf3bd6SGeorge Wilson multilist_remove(&state->arcs_list[arc_buf_type(hdr)], 1869*dcbf3bd6SGeorge Wilson hdr); 1870*dcbf3bd6SGeorge Wilson arc_evitable_space_decrement(hdr, state); 1871*dcbf3bd6SGeorge Wilson } 1872*dcbf3bd6SGeorge Wilson /* remove the prefetch flag if we get a reference */ 1873*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); 1874*dcbf3bd6SGeorge Wilson } 1875*dcbf3bd6SGeorge Wilson } 1876*dcbf3bd6SGeorge Wilson 1877*dcbf3bd6SGeorge Wilson /* 1878*dcbf3bd6SGeorge Wilson * Remove a reference from this hdr. When the reference transitions from 1879*dcbf3bd6SGeorge Wilson * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's 1880*dcbf3bd6SGeorge Wilson * list making it eligible for eviction. 1881*dcbf3bd6SGeorge Wilson */ 1882*dcbf3bd6SGeorge Wilson static int 1883*dcbf3bd6SGeorge Wilson remove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1884*dcbf3bd6SGeorge Wilson { 1885*dcbf3bd6SGeorge Wilson int cnt; 1886*dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 1887*dcbf3bd6SGeorge Wilson 1888*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 1889*dcbf3bd6SGeorge Wilson ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1890*dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(state)); 1891*dcbf3bd6SGeorge Wilson 1892*dcbf3bd6SGeorge Wilson /* 1893*dcbf3bd6SGeorge Wilson * arc_l2c_only counts as a ghost state so we don't need to explicitly 1894*dcbf3bd6SGeorge Wilson * check to prevent usage of the arc_l2c_only list. 1895*dcbf3bd6SGeorge Wilson */ 1896*dcbf3bd6SGeorge Wilson if (((cnt = refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && 1897*dcbf3bd6SGeorge Wilson (state != arc_anon)) { 1898*dcbf3bd6SGeorge Wilson multilist_insert(&state->arcs_list[arc_buf_type(hdr)], hdr); 1899*dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); 1900*dcbf3bd6SGeorge Wilson arc_evictable_space_increment(hdr, state); 1901*dcbf3bd6SGeorge Wilson } 1902*dcbf3bd6SGeorge Wilson return (cnt); 1903*dcbf3bd6SGeorge Wilson } 1904*dcbf3bd6SGeorge Wilson 1905*dcbf3bd6SGeorge Wilson /* 1906*dcbf3bd6SGeorge Wilson * Move the supplied buffer to the indicated state. The hash lock 1907*dcbf3bd6SGeorge Wilson * for the buffer must be held by the caller. 1908*dcbf3bd6SGeorge Wilson */ 1909*dcbf3bd6SGeorge Wilson static void 1910*dcbf3bd6SGeorge Wilson arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, 1911*dcbf3bd6SGeorge Wilson kmutex_t *hash_lock) 1912*dcbf3bd6SGeorge Wilson { 1913*dcbf3bd6SGeorge Wilson arc_state_t *old_state; 1914*dcbf3bd6SGeorge Wilson int64_t refcnt; 1915*dcbf3bd6SGeorge Wilson uint32_t bufcnt; 1916*dcbf3bd6SGeorge Wilson boolean_t update_old, update_new; 1917*dcbf3bd6SGeorge Wilson arc_buf_contents_t buftype = arc_buf_type(hdr); 191889c86e32SChris Williamson 191989c86e32SChris Williamson /* 192089c86e32SChris Williamson * We almost always have an L1 hdr here, since we call arc_hdr_realloc() 192189c86e32SChris Williamson * in arc_read() when bringing a buffer out of the L2ARC. However, the 192289c86e32SChris Williamson * L1 hdr doesn't always exist when we change state to arc_anon before 192389c86e32SChris Williamson * destroying a header, in which case reallocating to add the L1 hdr is 192489c86e32SChris Williamson * pointless. 192589c86e32SChris Williamson */ 192689c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) { 192789c86e32SChris Williamson old_state = hdr->b_l1hdr.b_state; 192889c86e32SChris Williamson refcnt = refcount_count(&hdr->b_l1hdr.b_refcnt); 1929*dcbf3bd6SGeorge Wilson bufcnt = hdr->b_l1hdr.b_bufcnt; 1930*dcbf3bd6SGeorge Wilson update_old = (bufcnt > 0 || hdr->b_l1hdr.b_pdata != NULL); 193189c86e32SChris Williamson } else { 193289c86e32SChris Williamson old_state = arc_l2c_only; 193389c86e32SChris Williamson refcnt = 0; 1934*dcbf3bd6SGeorge Wilson bufcnt = 0; 1935*dcbf3bd6SGeorge Wilson update_old = B_FALSE; 193689c86e32SChris Williamson } 1937*dcbf3bd6SGeorge Wilson update_new = update_old; 1938fa9e4066Sahrens 1939fa9e4066Sahrens ASSERT(MUTEX_HELD(hash_lock)); 194069962b56SMatthew Ahrens ASSERT3P(new_state, !=, old_state); 1941*dcbf3bd6SGeorge Wilson ASSERT(!GHOST_STATE(new_state) || bufcnt == 0); 1942*dcbf3bd6SGeorge Wilson ASSERT(old_state != arc_anon || bufcnt <= 1); 1943fa9e4066Sahrens 1944fa9e4066Sahrens /* 1945fa9e4066Sahrens * If this buffer is evictable, transfer it from the 1946fa9e4066Sahrens * old state list to the new state list. 1947fa9e4066Sahrens */ 1948ea8dc4b6Seschrock if (refcnt == 0) { 194989c86e32SChris Williamson if (old_state != arc_anon && old_state != arc_l2c_only) { 195089c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 1951244781f1SPrakash Surya multilist_remove(&old_state->arcs_list[buftype], hdr); 1952ea8dc4b6Seschrock 1953*dcbf3bd6SGeorge Wilson if (GHOST_STATE(old_state)) { 1954*dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 1955*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 1956*dcbf3bd6SGeorge Wilson update_old = B_TRUE; 1957ea8dc4b6Seschrock } 1958*dcbf3bd6SGeorge Wilson arc_evitable_space_decrement(hdr, old_state); 1959fa9e4066Sahrens } 196089c86e32SChris Williamson if (new_state != arc_anon && new_state != arc_l2c_only) { 1961fa9e4066Sahrens 196289c86e32SChris Williamson /* 196389c86e32SChris Williamson * An L1 header always exists here, since if we're 196489c86e32SChris Williamson * moving to some L1-cached state (i.e. not l2c_only or 196589c86e32SChris Williamson * anonymous), we realloc the header to add an L1hdr 196689c86e32SChris Williamson * beforehand. 196789c86e32SChris Williamson */ 196889c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 1969244781f1SPrakash Surya multilist_insert(&new_state->arcs_list[buftype], hdr); 1970ea8dc4b6Seschrock 1971ea8dc4b6Seschrock if (GHOST_STATE(new_state)) { 1972*dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 1973*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 1974*dcbf3bd6SGeorge Wilson update_new = B_TRUE; 1975ea8dc4b6Seschrock } 1976*dcbf3bd6SGeorge Wilson arc_evictable_space_increment(hdr, new_state); 1977fa9e4066Sahrens } 1978fa9e4066Sahrens } 1979fa9e4066Sahrens 1980*dcbf3bd6SGeorge Wilson ASSERT(!HDR_EMPTY(hdr)); 19817adb730bSGeorge Wilson if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) 19827adb730bSGeorge Wilson buf_hash_remove(hdr); 1983fa9e4066Sahrens 198489c86e32SChris Williamson /* adjust state sizes (ignore arc_l2c_only) */ 19852fd872a7SPrakash Surya 1986*dcbf3bd6SGeorge Wilson if (update_new && new_state != arc_l2c_only) { 19872fd872a7SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 19882fd872a7SPrakash Surya if (GHOST_STATE(new_state)) { 1989*dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 19902fd872a7SPrakash Surya 19912fd872a7SPrakash Surya /* 1992*dcbf3bd6SGeorge Wilson * When moving a header to a ghost state, we first 19932fd872a7SPrakash Surya * remove all arc buffers. Thus, we'll have a 1994*dcbf3bd6SGeorge Wilson * bufcnt of zero, and no arc buffer to use for 19952fd872a7SPrakash Surya * the reference. As a result, we use the arc 19962fd872a7SPrakash Surya * header pointer for the reference. 19972fd872a7SPrakash Surya */ 19982fd872a7SPrakash Surya (void) refcount_add_many(&new_state->arcs_size, 1999*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr), hdr); 2000*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 20012fd872a7SPrakash Surya } else { 2002*dcbf3bd6SGeorge Wilson uint32_t buffers = 0; 20032fd872a7SPrakash Surya 20042fd872a7SPrakash Surya /* 20052fd872a7SPrakash Surya * Each individual buffer holds a unique reference, 20062fd872a7SPrakash Surya * thus we must remove each of these references one 20072fd872a7SPrakash Surya * at a time. 20082fd872a7SPrakash Surya */ 20092fd872a7SPrakash Surya for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 20102fd872a7SPrakash Surya buf = buf->b_next) { 2011*dcbf3bd6SGeorge Wilson ASSERT3U(bufcnt, !=, 0); 2012*dcbf3bd6SGeorge Wilson buffers++; 2013*dcbf3bd6SGeorge Wilson 2014*dcbf3bd6SGeorge Wilson /* 2015*dcbf3bd6SGeorge Wilson * When the arc_buf_t is sharing the data 2016*dcbf3bd6SGeorge Wilson * block with the hdr, the owner of the 2017*dcbf3bd6SGeorge Wilson * reference belongs to the hdr. Only 2018*dcbf3bd6SGeorge Wilson * add to the refcount if the arc_buf_t is 2019*dcbf3bd6SGeorge Wilson * not shared. 2020*dcbf3bd6SGeorge Wilson */ 2021*dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 2022*dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(buf)); 2023*dcbf3bd6SGeorge Wilson continue; 2024*dcbf3bd6SGeorge Wilson } 2025*dcbf3bd6SGeorge Wilson 20262fd872a7SPrakash Surya (void) refcount_add_many(&new_state->arcs_size, 2027*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr), buf); 2028*dcbf3bd6SGeorge Wilson } 2029*dcbf3bd6SGeorge Wilson ASSERT3U(bufcnt, ==, buffers); 2030*dcbf3bd6SGeorge Wilson 2031*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_pdata != NULL) { 2032*dcbf3bd6SGeorge Wilson (void) refcount_add_many(&new_state->arcs_size, 2033*dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 2034*dcbf3bd6SGeorge Wilson } else { 2035*dcbf3bd6SGeorge Wilson ASSERT(GHOST_STATE(old_state)); 20362fd872a7SPrakash Surya } 20372fd872a7SPrakash Surya } 20382fd872a7SPrakash Surya } 20392fd872a7SPrakash Surya 2040*dcbf3bd6SGeorge Wilson if (update_old && old_state != arc_l2c_only) { 20412fd872a7SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 20422fd872a7SPrakash Surya if (GHOST_STATE(old_state)) { 2043*dcbf3bd6SGeorge Wilson ASSERT0(bufcnt); 2044*dcbf3bd6SGeorge Wilson 20452fd872a7SPrakash Surya /* 20462fd872a7SPrakash Surya * When moving a header off of a ghost state, 2047*dcbf3bd6SGeorge Wilson * the header will not contain any arc buffers. 2048*dcbf3bd6SGeorge Wilson * We use the arc header pointer for the reference 2049*dcbf3bd6SGeorge Wilson * which is exactly what we did when we put the 2050*dcbf3bd6SGeorge Wilson * header on the ghost state. 20512fd872a7SPrakash Surya */ 20522fd872a7SPrakash Surya 20532fd872a7SPrakash Surya (void) refcount_remove_many(&old_state->arcs_size, 2054*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr), hdr); 2055*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 20562fd872a7SPrakash Surya } else { 2057*dcbf3bd6SGeorge Wilson uint32_t buffers = 0; 20582fd872a7SPrakash Surya 20592fd872a7SPrakash Surya /* 20602fd872a7SPrakash Surya * Each individual buffer holds a unique reference, 20612fd872a7SPrakash Surya * thus we must remove each of these references one 20622fd872a7SPrakash Surya * at a time. 20632fd872a7SPrakash Surya */ 20642fd872a7SPrakash Surya for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 20652fd872a7SPrakash Surya buf = buf->b_next) { 2066*dcbf3bd6SGeorge Wilson ASSERT3P(bufcnt, !=, 0); 2067*dcbf3bd6SGeorge Wilson buffers++; 2068*dcbf3bd6SGeorge Wilson 2069*dcbf3bd6SGeorge Wilson /* 2070*dcbf3bd6SGeorge Wilson * When the arc_buf_t is sharing the data 2071*dcbf3bd6SGeorge Wilson * block with the hdr, the owner of the 2072*dcbf3bd6SGeorge Wilson * reference belongs to the hdr. Only 2073*dcbf3bd6SGeorge Wilson * add to the refcount if the arc_buf_t is 2074*dcbf3bd6SGeorge Wilson * not shared. 2075*dcbf3bd6SGeorge Wilson */ 2076*dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 2077*dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(buf)); 2078*dcbf3bd6SGeorge Wilson continue; 2079*dcbf3bd6SGeorge Wilson } 2080*dcbf3bd6SGeorge Wilson 20812fd872a7SPrakash Surya (void) refcount_remove_many( 2082*dcbf3bd6SGeorge Wilson &old_state->arcs_size, HDR_GET_LSIZE(hdr), 2083*dcbf3bd6SGeorge Wilson buf); 20842fd872a7SPrakash Surya } 2085*dcbf3bd6SGeorge Wilson ASSERT3U(bufcnt, ==, buffers); 2086*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, !=, NULL); 2087*dcbf3bd6SGeorge Wilson (void) refcount_remove_many( 2088*dcbf3bd6SGeorge Wilson &old_state->arcs_size, arc_hdr_size(hdr), hdr); 20892fd872a7SPrakash Surya } 2090fa9e4066Sahrens } 20912fd872a7SPrakash Surya 209289c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) 209389c86e32SChris Williamson hdr->b_l1hdr.b_state = new_state; 2094fa94a07fSbrendan 209589c86e32SChris Williamson /* 209689c86e32SChris Williamson * L2 headers should never be on the L2 state list since they don't 209789c86e32SChris Williamson * have L1 headers allocated. 209889c86e32SChris Williamson */ 2099244781f1SPrakash Surya ASSERT(multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && 2100244781f1SPrakash Surya multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); 2101fa9e4066Sahrens } 2102fa9e4066Sahrens 21030e8c6158Smaybee void 21045a98e54bSBrendan Gregg - Sun Microsystems arc_space_consume(uint64_t space, arc_space_type_t type) 21050e8c6158Smaybee { 21065a98e54bSBrendan Gregg - Sun Microsystems ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 21075a98e54bSBrendan Gregg - Sun Microsystems 21085a98e54bSBrendan Gregg - Sun Microsystems switch (type) { 21095a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_DATA: 21105a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_data_size, space); 21115a98e54bSBrendan Gregg - Sun Microsystems break; 21124076b1bfSPrakash Surya case ARC_SPACE_META: 21134076b1bfSPrakash Surya ARCSTAT_INCR(arcstat_metadata_size, space); 21144076b1bfSPrakash Surya break; 21155a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_OTHER: 21165a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_other_size, space); 21175a98e54bSBrendan Gregg - Sun Microsystems break; 21185a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_HDRS: 21195a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_hdr_size, space); 21205a98e54bSBrendan Gregg - Sun Microsystems break; 21215a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_L2HDRS: 21225a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_l2_hdr_size, space); 21235a98e54bSBrendan Gregg - Sun Microsystems break; 21245a98e54bSBrendan Gregg - Sun Microsystems } 21255a98e54bSBrendan Gregg - Sun Microsystems 21264076b1bfSPrakash Surya if (type != ARC_SPACE_DATA) 21274076b1bfSPrakash Surya ARCSTAT_INCR(arcstat_meta_used, space); 21284076b1bfSPrakash Surya 21290e8c6158Smaybee atomic_add_64(&arc_size, space); 21300e8c6158Smaybee } 21310e8c6158Smaybee 21320e8c6158Smaybee void 21335a98e54bSBrendan Gregg - Sun Microsystems arc_space_return(uint64_t space, arc_space_type_t type) 21340e8c6158Smaybee { 21355a98e54bSBrendan Gregg - Sun Microsystems ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 21365a98e54bSBrendan Gregg - Sun Microsystems 21375a98e54bSBrendan Gregg - Sun Microsystems switch (type) { 21385a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_DATA: 21395a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_data_size, -space); 21405a98e54bSBrendan Gregg - Sun Microsystems break; 21414076b1bfSPrakash Surya case ARC_SPACE_META: 21424076b1bfSPrakash Surya ARCSTAT_INCR(arcstat_metadata_size, -space); 21434076b1bfSPrakash Surya break; 21445a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_OTHER: 21455a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_other_size, -space); 21465a98e54bSBrendan Gregg - Sun Microsystems break; 21475a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_HDRS: 21485a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_hdr_size, -space); 21495a98e54bSBrendan Gregg - Sun Microsystems break; 21505a98e54bSBrendan Gregg - Sun Microsystems case ARC_SPACE_L2HDRS: 21515a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 21525a98e54bSBrendan Gregg - Sun Microsystems break; 21535a98e54bSBrendan Gregg - Sun Microsystems } 21545a98e54bSBrendan Gregg - Sun Microsystems 21554076b1bfSPrakash Surya if (type != ARC_SPACE_DATA) { 21564076b1bfSPrakash Surya ASSERT(arc_meta_used >= space); 21574076b1bfSPrakash Surya if (arc_meta_max < arc_meta_used) 21584076b1bfSPrakash Surya arc_meta_max = arc_meta_used; 21594076b1bfSPrakash Surya ARCSTAT_INCR(arcstat_meta_used, -space); 21604076b1bfSPrakash Surya } 21614076b1bfSPrakash Surya 21620e8c6158Smaybee ASSERT(arc_size >= space); 21630e8c6158Smaybee atomic_add_64(&arc_size, -space); 21640e8c6158Smaybee } 21650e8c6158Smaybee 2166*dcbf3bd6SGeorge Wilson /* 2167*dcbf3bd6SGeorge Wilson * Allocate an initial buffer for this hdr, subsequent buffers will 2168*dcbf3bd6SGeorge Wilson * use arc_buf_clone(). 2169*dcbf3bd6SGeorge Wilson */ 2170*dcbf3bd6SGeorge Wilson static arc_buf_t * 2171*dcbf3bd6SGeorge Wilson arc_buf_alloc_impl(arc_buf_hdr_t *hdr, void *tag) 2172fa9e4066Sahrens { 2173fa9e4066Sahrens arc_buf_t *buf; 2174fa9e4066Sahrens 2175*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2176*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); 2177*dcbf3bd6SGeorge Wilson VERIFY(hdr->b_type == ARC_BUFC_DATA || 2178*dcbf3bd6SGeorge Wilson hdr->b_type == ARC_BUFC_METADATA); 2179*dcbf3bd6SGeorge Wilson 2180*dcbf3bd6SGeorge Wilson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2181*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2182*dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 218389c86e32SChris Williamson 21841ab7f2deSmaybee buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2185fa9e4066Sahrens buf->b_hdr = hdr; 218644eda4d7Smaybee buf->b_data = NULL; 2187fa9e4066Sahrens buf->b_next = NULL; 218889c86e32SChris Williamson 2189*dcbf3bd6SGeorge Wilson add_reference(hdr, tag); 2190*dcbf3bd6SGeorge Wilson 2191*dcbf3bd6SGeorge Wilson /* 2192*dcbf3bd6SGeorge Wilson * We're about to change the hdr's b_flags. We must either 2193*dcbf3bd6SGeorge Wilson * hold the hash_lock or be undiscoverable. 2194*dcbf3bd6SGeorge Wilson */ 2195*dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2196*dcbf3bd6SGeorge Wilson 2197*dcbf3bd6SGeorge Wilson /* 2198*dcbf3bd6SGeorge Wilson * If the hdr's data can be shared (no byteswapping, hdr is 2199*dcbf3bd6SGeorge Wilson * uncompressed, hdr's data is not currently being written to the 2200*dcbf3bd6SGeorge Wilson * L2ARC write) then we share the data buffer and set the appropriate 2201*dcbf3bd6SGeorge Wilson * bit in the hdr's b_flags to indicate the hdr is sharing it's 2202*dcbf3bd6SGeorge Wilson * b_pdata with the arc_buf_t. Otherwise, we allocate a new buffer to 2203*dcbf3bd6SGeorge Wilson * store the buf's data. 2204*dcbf3bd6SGeorge Wilson */ 2205*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS && 2206*dcbf3bd6SGeorge Wilson HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF && !HDR_L2_WRITING(hdr)) { 2207*dcbf3bd6SGeorge Wilson buf->b_data = hdr->b_l1hdr.b_pdata; 2208*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); 2209*dcbf3bd6SGeorge Wilson } else { 2210*dcbf3bd6SGeorge Wilson buf->b_data = arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); 2211*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); 2212*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 2213*dcbf3bd6SGeorge Wilson } 2214*dcbf3bd6SGeorge Wilson VERIFY3P(buf->b_data, !=, NULL); 221589c86e32SChris Williamson 221689c86e32SChris Williamson hdr->b_l1hdr.b_buf = buf; 2217*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt += 1; 221889c86e32SChris Williamson 2219*dcbf3bd6SGeorge Wilson return (buf); 2220*dcbf3bd6SGeorge Wilson } 2221*dcbf3bd6SGeorge Wilson 2222*dcbf3bd6SGeorge Wilson /* 2223*dcbf3bd6SGeorge Wilson * Used when allocating additional buffers. 2224*dcbf3bd6SGeorge Wilson */ 2225*dcbf3bd6SGeorge Wilson static arc_buf_t * 2226*dcbf3bd6SGeorge Wilson arc_buf_clone(arc_buf_t *from) 2227*dcbf3bd6SGeorge Wilson { 2228*dcbf3bd6SGeorge Wilson arc_buf_t *buf; 2229*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = from->b_hdr; 2230*dcbf3bd6SGeorge Wilson uint64_t size = HDR_GET_LSIZE(hdr); 2231*dcbf3bd6SGeorge Wilson 2232*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2233*dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_state != arc_anon); 2234fa9e4066Sahrens 2235*dcbf3bd6SGeorge Wilson buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2236*dcbf3bd6SGeorge Wilson buf->b_hdr = hdr; 2237*dcbf3bd6SGeorge Wilson buf->b_data = NULL; 2238*dcbf3bd6SGeorge Wilson buf->b_next = hdr->b_l1hdr.b_buf; 2239*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_buf = buf; 2240*dcbf3bd6SGeorge Wilson buf->b_data = arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); 2241*dcbf3bd6SGeorge Wilson bcopy(from->b_data, buf->b_data, size); 2242*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt += 1; 2243*dcbf3bd6SGeorge Wilson 2244*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); 2245fa9e4066Sahrens return (buf); 2246fa9e4066Sahrens } 2247fa9e4066Sahrens 22482fdbea25SAleksandr Guzovskiy static char *arc_onloan_tag = "onloan"; 22492fdbea25SAleksandr Guzovskiy 22502fdbea25SAleksandr Guzovskiy /* 22512fdbea25SAleksandr Guzovskiy * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 22522fdbea25SAleksandr Guzovskiy * flight data by arc_tempreserve_space() until they are "returned". Loaned 22532fdbea25SAleksandr Guzovskiy * buffers must be returned to the arc before they can be used by the DMU or 22542fdbea25SAleksandr Guzovskiy * freed. 22552fdbea25SAleksandr Guzovskiy */ 22562fdbea25SAleksandr Guzovskiy arc_buf_t * 22572fdbea25SAleksandr Guzovskiy arc_loan_buf(spa_t *spa, int size) 22582fdbea25SAleksandr Guzovskiy { 22592fdbea25SAleksandr Guzovskiy arc_buf_t *buf; 22602fdbea25SAleksandr Guzovskiy 2261*dcbf3bd6SGeorge Wilson buf = arc_alloc_buf(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 22622fdbea25SAleksandr Guzovskiy 22632fdbea25SAleksandr Guzovskiy atomic_add_64(&arc_loaned_bytes, size); 22642fdbea25SAleksandr Guzovskiy return (buf); 22652fdbea25SAleksandr Guzovskiy } 22662fdbea25SAleksandr Guzovskiy 22672fdbea25SAleksandr Guzovskiy /* 22682fdbea25SAleksandr Guzovskiy * Return a loaned arc buffer to the arc. 22692fdbea25SAleksandr Guzovskiy */ 22702fdbea25SAleksandr Guzovskiy void 22712fdbea25SAleksandr Guzovskiy arc_return_buf(arc_buf_t *buf, void *tag) 22722fdbea25SAleksandr Guzovskiy { 22732fdbea25SAleksandr Guzovskiy arc_buf_hdr_t *hdr = buf->b_hdr; 22742fdbea25SAleksandr Guzovskiy 2275*dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, !=, NULL); 227689c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 227789c86e32SChris Williamson (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 227889c86e32SChris Williamson (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 22792fdbea25SAleksandr Guzovskiy 2280*dcbf3bd6SGeorge Wilson atomic_add_64(&arc_loaned_bytes, -HDR_GET_LSIZE(hdr)); 22812fdbea25SAleksandr Guzovskiy } 22822fdbea25SAleksandr Guzovskiy 2283c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /* Detach an arc_buf from a dbuf (tag) */ 2284c242f9a0Schunli zhang - Sun Microsystems - Irvine United States void 2285c242f9a0Schunli zhang - Sun Microsystems - Irvine United States arc_loan_inuse_buf(arc_buf_t *buf, void *tag) 2286c242f9a0Schunli zhang - Sun Microsystems - Irvine United States { 228789c86e32SChris Williamson arc_buf_hdr_t *hdr = buf->b_hdr; 2288c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 2289*dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, !=, NULL); 229089c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 229189c86e32SChris Williamson (void) refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 229289c86e32SChris Williamson (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); 2293c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 2294*dcbf3bd6SGeorge Wilson atomic_add_64(&arc_loaned_bytes, HDR_GET_LSIZE(hdr)); 2295c242f9a0Schunli zhang - Sun Microsystems - Irvine United States } 2296c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 2297*dcbf3bd6SGeorge Wilson static void 2298*dcbf3bd6SGeorge Wilson l2arc_free_data_on_write(void *data, size_t size, arc_buf_contents_t type) 2299ea8dc4b6Seschrock { 2300*dcbf3bd6SGeorge Wilson l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP); 2301ea8dc4b6Seschrock 2302*dcbf3bd6SGeorge Wilson df->l2df_data = data; 2303*dcbf3bd6SGeorge Wilson df->l2df_size = size; 2304*dcbf3bd6SGeorge Wilson df->l2df_type = type; 2305*dcbf3bd6SGeorge Wilson mutex_enter(&l2arc_free_on_write_mtx); 2306*dcbf3bd6SGeorge Wilson list_insert_head(l2arc_free_on_write, df); 2307*dcbf3bd6SGeorge Wilson mutex_exit(&l2arc_free_on_write_mtx); 2308*dcbf3bd6SGeorge Wilson } 2309b24ab676SJeff Bonwick 2310*dcbf3bd6SGeorge Wilson static void 2311*dcbf3bd6SGeorge Wilson arc_hdr_free_on_write(arc_buf_hdr_t *hdr) 2312*dcbf3bd6SGeorge Wilson { 2313*dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2314*dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 2315*dcbf3bd6SGeorge Wilson uint64_t size = arc_hdr_size(hdr); 2316*dcbf3bd6SGeorge Wilson 2317*dcbf3bd6SGeorge Wilson /* protected by hash lock, if in the hash table */ 2318*dcbf3bd6SGeorge Wilson if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 2319*dcbf3bd6SGeorge Wilson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2320*dcbf3bd6SGeorge Wilson ASSERT(state != arc_anon && state != arc_l2c_only); 2321*dcbf3bd6SGeorge Wilson 2322*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 2323*dcbf3bd6SGeorge Wilson size, hdr); 2324*dcbf3bd6SGeorge Wilson } 2325*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_size, size, hdr); 2326*dcbf3bd6SGeorge Wilson 2327*dcbf3bd6SGeorge Wilson l2arc_free_data_on_write(hdr->b_l1hdr.b_pdata, size, type); 2328*dcbf3bd6SGeorge Wilson } 2329*dcbf3bd6SGeorge Wilson 2330*dcbf3bd6SGeorge Wilson /* 2331*dcbf3bd6SGeorge Wilson * Share the arc_buf_t's data with the hdr. Whenever we are sharing the 2332*dcbf3bd6SGeorge Wilson * data buffer, we transfer the refcount ownership to the hdr and update 2333*dcbf3bd6SGeorge Wilson * the appropriate kstats. 2334*dcbf3bd6SGeorge Wilson */ 2335*dcbf3bd6SGeorge Wilson static void 2336*dcbf3bd6SGeorge Wilson arc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2337*dcbf3bd6SGeorge Wilson { 2338*dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2339*dcbf3bd6SGeorge Wilson 2340*dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 2341*dcbf3bd6SGeorge Wilson ASSERT(!arc_buf_is_shared(buf)); 2342*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 2343*dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 23449253d63dSGeorge Wilson 23459253d63dSGeorge Wilson /* 2346*dcbf3bd6SGeorge Wilson * Start sharing the data buffer. We transfer the 2347*dcbf3bd6SGeorge Wilson * refcount ownership to the hdr since it always owns 2348*dcbf3bd6SGeorge Wilson * the refcount whenever an arc_buf_t is shared. 23499253d63dSGeorge Wilson */ 2350*dcbf3bd6SGeorge Wilson refcount_transfer_ownership(&state->arcs_size, buf, hdr); 2351*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_pdata = buf->b_data; 2352*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); 2353*dcbf3bd6SGeorge Wilson 2354*dcbf3bd6SGeorge Wilson /* 2355*dcbf3bd6SGeorge Wilson * Since we've transferred ownership to the hdr we need 2356*dcbf3bd6SGeorge Wilson * to increment its compressed and uncompressed kstats and 2357*dcbf3bd6SGeorge Wilson * decrement the overhead size. 2358*dcbf3bd6SGeorge Wilson */ 2359*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); 2360*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); 2361*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_overhead_size, -HDR_GET_LSIZE(hdr)); 2362ea8dc4b6Seschrock } 2363ea8dc4b6Seschrock 2364*dcbf3bd6SGeorge Wilson static void 2365*dcbf3bd6SGeorge Wilson arc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2366ea8dc4b6Seschrock { 2367*dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 2368*dcbf3bd6SGeorge Wilson 2369*dcbf3bd6SGeorge Wilson ASSERT(HDR_SHARED_DATA(hdr)); 2370*dcbf3bd6SGeorge Wilson ASSERT(arc_buf_is_shared(buf)); 2371*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, !=, NULL); 2372*dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2373ea8dc4b6Seschrock 23749b23f181Smaybee /* 2375*dcbf3bd6SGeorge Wilson * We are no longer sharing this buffer so we need 2376*dcbf3bd6SGeorge Wilson * to transfer its ownership to the rightful owner. 23779b23f181Smaybee */ 2378*dcbf3bd6SGeorge Wilson refcount_transfer_ownership(&state->arcs_size, hdr, buf); 2379*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 2380*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_pdata = NULL; 2381*dcbf3bd6SGeorge Wilson 2382*dcbf3bd6SGeorge Wilson /* 2383*dcbf3bd6SGeorge Wilson * Since the buffer is no longer shared between 2384*dcbf3bd6SGeorge Wilson * the arc buf and the hdr, count it as overhead. 2385*dcbf3bd6SGeorge Wilson */ 2386*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); 2387*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); 2388*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); 2389*dcbf3bd6SGeorge Wilson } 2390*dcbf3bd6SGeorge Wilson 2391*dcbf3bd6SGeorge Wilson /* 2392*dcbf3bd6SGeorge Wilson * Free up buf->b_data and if 'remove' is set, then pull the 2393*dcbf3bd6SGeorge Wilson * arc_buf_t off of the the arc_buf_hdr_t's list and free it. 2394*dcbf3bd6SGeorge Wilson */ 2395*dcbf3bd6SGeorge Wilson static void 2396*dcbf3bd6SGeorge Wilson arc_buf_destroy_impl(arc_buf_t *buf, boolean_t remove) 2397*dcbf3bd6SGeorge Wilson { 2398*dcbf3bd6SGeorge Wilson arc_buf_t **bufp; 2399*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = buf->b_hdr; 2400*dcbf3bd6SGeorge Wilson uint64_t size = HDR_GET_LSIZE(hdr); 2401*dcbf3bd6SGeorge Wilson boolean_t destroyed_buf_is_shared = arc_buf_is_shared(buf); 2402*dcbf3bd6SGeorge Wilson 2403*dcbf3bd6SGeorge Wilson /* 2404*dcbf3bd6SGeorge Wilson * Free up the data associated with the buf but only 2405*dcbf3bd6SGeorge Wilson * if we're not sharing this with the hdr. If we are sharing 2406*dcbf3bd6SGeorge Wilson * it with the hdr, then hdr will have performed the allocation 2407*dcbf3bd6SGeorge Wilson * so allow it to do the free. 2408*dcbf3bd6SGeorge Wilson */ 2409*dcbf3bd6SGeorge Wilson if (buf->b_data != NULL) { 2410*dcbf3bd6SGeorge Wilson /* 2411*dcbf3bd6SGeorge Wilson * We're about to change the hdr's b_flags. We must either 2412*dcbf3bd6SGeorge Wilson * hold the hash_lock or be undiscoverable. 2413*dcbf3bd6SGeorge Wilson */ 2414*dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2415*dcbf3bd6SGeorge Wilson 2416*dcbf3bd6SGeorge Wilson arc_cksum_verify(buf); 2417*dcbf3bd6SGeorge Wilson arc_buf_unwatch(buf); 2418*dcbf3bd6SGeorge Wilson 2419*dcbf3bd6SGeorge Wilson if (destroyed_buf_is_shared) { 2420*dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(buf)); 2421*dcbf3bd6SGeorge Wilson ASSERT(HDR_SHARED_DATA(hdr)); 2422*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 2423*dcbf3bd6SGeorge Wilson } else { 2424*dcbf3bd6SGeorge Wilson arc_free_data_buf(hdr, buf->b_data, size, buf); 2425*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_overhead_size, -size); 2426*dcbf3bd6SGeorge Wilson } 2427*dcbf3bd6SGeorge Wilson buf->b_data = NULL; 2428*dcbf3bd6SGeorge Wilson 2429*dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 2430*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt -= 1; 2431*dcbf3bd6SGeorge Wilson } 2432*dcbf3bd6SGeorge Wilson 2433*dcbf3bd6SGeorge Wilson /* only remove the buf if requested */ 2434*dcbf3bd6SGeorge Wilson if (!remove) 24359b23f181Smaybee return; 2436*dcbf3bd6SGeorge Wilson 2437*dcbf3bd6SGeorge Wilson /* remove the buf from the hdr list */ 2438*dcbf3bd6SGeorge Wilson arc_buf_t *lastbuf = NULL; 2439*dcbf3bd6SGeorge Wilson bufp = &hdr->b_l1hdr.b_buf; 2440*dcbf3bd6SGeorge Wilson while (*bufp != NULL) { 2441*dcbf3bd6SGeorge Wilson if (*bufp == buf) 2442*dcbf3bd6SGeorge Wilson *bufp = buf->b_next; 2443*dcbf3bd6SGeorge Wilson 2444*dcbf3bd6SGeorge Wilson /* 2445*dcbf3bd6SGeorge Wilson * If we've removed a buffer in the middle of 2446*dcbf3bd6SGeorge Wilson * the list then update the lastbuf and update 2447*dcbf3bd6SGeorge Wilson * bufp. 2448*dcbf3bd6SGeorge Wilson */ 2449*dcbf3bd6SGeorge Wilson if (*bufp != NULL) { 2450*dcbf3bd6SGeorge Wilson lastbuf = *bufp; 2451*dcbf3bd6SGeorge Wilson bufp = &(*bufp)->b_next; 2452*dcbf3bd6SGeorge Wilson } 245340d7d650Smaybee } 2454*dcbf3bd6SGeorge Wilson buf->b_next = NULL; 2455*dcbf3bd6SGeorge Wilson ASSERT3P(lastbuf, !=, buf); 2456*dcbf3bd6SGeorge Wilson 2457*dcbf3bd6SGeorge Wilson /* 2458*dcbf3bd6SGeorge Wilson * If the current arc_buf_t is sharing its data 2459*dcbf3bd6SGeorge Wilson * buffer with the hdr, then reassign the hdr's 2460*dcbf3bd6SGeorge Wilson * b_pdata to share it with the new buffer at the end 2461*dcbf3bd6SGeorge Wilson * of the list. The shared buffer is always the last one 2462*dcbf3bd6SGeorge Wilson * on the hdr's buffer list. 2463*dcbf3bd6SGeorge Wilson */ 2464*dcbf3bd6SGeorge Wilson if (destroyed_buf_is_shared && lastbuf != NULL) { 2465*dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(buf)); 2466*dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(lastbuf)); 2467*dcbf3bd6SGeorge Wilson VERIFY(!arc_buf_is_shared(lastbuf)); 2468*dcbf3bd6SGeorge Wilson 2469*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, !=, NULL); 2470*dcbf3bd6SGeorge Wilson arc_hdr_free_pdata(hdr); 2471*dcbf3bd6SGeorge Wilson 2472*dcbf3bd6SGeorge Wilson /* 2473*dcbf3bd6SGeorge Wilson * We must setup a new shared block between the 2474*dcbf3bd6SGeorge Wilson * last buffer and the hdr. The data would have 2475*dcbf3bd6SGeorge Wilson * been allocated by the arc buf so we need to transfer 2476*dcbf3bd6SGeorge Wilson * ownership to the hdr since it's now being shared. 2477*dcbf3bd6SGeorge Wilson */ 2478*dcbf3bd6SGeorge Wilson arc_share_buf(hdr, lastbuf); 2479*dcbf3bd6SGeorge Wilson } else if (HDR_SHARED_DATA(hdr)) { 2480*dcbf3bd6SGeorge Wilson ASSERT(arc_buf_is_shared(lastbuf)); 2481*dcbf3bd6SGeorge Wilson } 2482*dcbf3bd6SGeorge Wilson 2483*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_bufcnt == 0) 2484*dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 2485*dcbf3bd6SGeorge Wilson 2486*dcbf3bd6SGeorge Wilson /* clean up the buf */ 2487*dcbf3bd6SGeorge Wilson buf->b_hdr = NULL; 2488*dcbf3bd6SGeorge Wilson kmem_cache_free(buf_cache, buf); 2489*dcbf3bd6SGeorge Wilson } 2490*dcbf3bd6SGeorge Wilson 2491*dcbf3bd6SGeorge Wilson static void 2492*dcbf3bd6SGeorge Wilson arc_hdr_alloc_pdata(arc_buf_hdr_t *hdr) 2493*dcbf3bd6SGeorge Wilson { 2494*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); 249589c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 2496*dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 2497ea8dc4b6Seschrock 2498*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 2499*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_pdata = arc_get_data_buf(hdr, arc_hdr_size(hdr), hdr); 2500*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 2501*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, !=, NULL); 250289c86e32SChris Williamson 2503*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); 2504*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); 2505ea8dc4b6Seschrock } 2506ea8dc4b6Seschrock 2507244781f1SPrakash Surya static void 2508*dcbf3bd6SGeorge Wilson arc_hdr_free_pdata(arc_buf_hdr_t *hdr) 2509244781f1SPrakash Surya { 2510*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 2511*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, !=, NULL); 2512244781f1SPrakash Surya 2513*dcbf3bd6SGeorge Wilson /* 2514*dcbf3bd6SGeorge Wilson * If the hdr is currently being written to the l2arc then 2515*dcbf3bd6SGeorge Wilson * we defer freeing the data by adding it to the l2arc_free_on_write 2516*dcbf3bd6SGeorge Wilson * list. The l2arc will free the data once it's finished 2517*dcbf3bd6SGeorge Wilson * writing it to the l2arc device. 2518*dcbf3bd6SGeorge Wilson */ 2519*dcbf3bd6SGeorge Wilson if (HDR_L2_WRITING(hdr)) { 2520*dcbf3bd6SGeorge Wilson arc_hdr_free_on_write(hdr); 2521*dcbf3bd6SGeorge Wilson ARCSTAT_BUMP(arcstat_l2_free_on_write); 2522*dcbf3bd6SGeorge Wilson } else { 2523*dcbf3bd6SGeorge Wilson arc_free_data_buf(hdr, hdr->b_l1hdr.b_pdata, 2524*dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 2525*dcbf3bd6SGeorge Wilson } 2526*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_pdata = NULL; 2527*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 2528*dcbf3bd6SGeorge Wilson 2529*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); 2530*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); 2531*dcbf3bd6SGeorge Wilson } 2532*dcbf3bd6SGeorge Wilson 2533*dcbf3bd6SGeorge Wilson static arc_buf_hdr_t * 2534*dcbf3bd6SGeorge Wilson arc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize, 2535*dcbf3bd6SGeorge Wilson enum zio_compress compress, arc_buf_contents_t type) 2536*dcbf3bd6SGeorge Wilson { 2537*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr; 2538*dcbf3bd6SGeorge Wilson 2539*dcbf3bd6SGeorge Wilson ASSERT3U(lsize, >, 0); 2540*dcbf3bd6SGeorge Wilson VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA); 2541*dcbf3bd6SGeorge Wilson 2542*dcbf3bd6SGeorge Wilson hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 2543*dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 2544*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 2545*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_thawed, ==, NULL); 2546*dcbf3bd6SGeorge Wilson HDR_SET_PSIZE(hdr, psize); 2547*dcbf3bd6SGeorge Wilson HDR_SET_LSIZE(hdr, lsize); 2548*dcbf3bd6SGeorge Wilson hdr->b_spa = spa; 2549*dcbf3bd6SGeorge Wilson hdr->b_type = type; 2550*dcbf3bd6SGeorge Wilson hdr->b_flags = 0; 2551*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR); 2552*dcbf3bd6SGeorge Wilson arc_hdr_set_compress(hdr, compress); 2553*dcbf3bd6SGeorge Wilson 2554*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_state = arc_anon; 2555*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_arc_access = 0; 2556*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt = 0; 2557*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_buf = NULL; 2558*dcbf3bd6SGeorge Wilson 2559*dcbf3bd6SGeorge Wilson /* 2560*dcbf3bd6SGeorge Wilson * Allocate the hdr's buffer. This will contain either 2561*dcbf3bd6SGeorge Wilson * the compressed or uncompressed data depending on the block 2562*dcbf3bd6SGeorge Wilson * it references and compressed arc enablement. 2563*dcbf3bd6SGeorge Wilson */ 2564*dcbf3bd6SGeorge Wilson arc_hdr_alloc_pdata(hdr); 2565*dcbf3bd6SGeorge Wilson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2566*dcbf3bd6SGeorge Wilson 2567*dcbf3bd6SGeorge Wilson return (hdr); 2568244781f1SPrakash Surya } 2569244781f1SPrakash Surya 2570fa94a07fSbrendan /* 2571*dcbf3bd6SGeorge Wilson * Transition between the two allocation states for the arc_buf_hdr struct. 2572*dcbf3bd6SGeorge Wilson * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without 2573*dcbf3bd6SGeorge Wilson * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller 2574*dcbf3bd6SGeorge Wilson * version is used when a cache buffer is only in the L2ARC in order to reduce 2575*dcbf3bd6SGeorge Wilson * memory usage. 2576fa94a07fSbrendan */ 2577*dcbf3bd6SGeorge Wilson static arc_buf_hdr_t * 2578*dcbf3bd6SGeorge Wilson arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) 2579fa94a07fSbrendan { 2580*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L2HDR(hdr)); 2581*dcbf3bd6SGeorge Wilson 2582*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *nhdr; 2583*dcbf3bd6SGeorge Wilson l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 2584*dcbf3bd6SGeorge Wilson 2585*dcbf3bd6SGeorge Wilson ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || 2586*dcbf3bd6SGeorge Wilson (old == hdr_l2only_cache && new == hdr_full_cache)); 2587*dcbf3bd6SGeorge Wilson 2588*dcbf3bd6SGeorge Wilson nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); 2589*dcbf3bd6SGeorge Wilson 2590*dcbf3bd6SGeorge Wilson ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 2591*dcbf3bd6SGeorge Wilson buf_hash_remove(hdr); 2592*dcbf3bd6SGeorge Wilson 2593*dcbf3bd6SGeorge Wilson bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); 2594*dcbf3bd6SGeorge Wilson 2595*dcbf3bd6SGeorge Wilson if (new == hdr_full_cache) { 2596*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR); 2597*dcbf3bd6SGeorge Wilson /* 2598*dcbf3bd6SGeorge Wilson * arc_access and arc_change_state need to be aware that a 2599*dcbf3bd6SGeorge Wilson * header has just come out of L2ARC, so we set its state to 2600*dcbf3bd6SGeorge Wilson * l2c_only even though it's about to change. 2601*dcbf3bd6SGeorge Wilson */ 2602*dcbf3bd6SGeorge Wilson nhdr->b_l1hdr.b_state = arc_l2c_only; 2603*dcbf3bd6SGeorge Wilson 2604*dcbf3bd6SGeorge Wilson /* Verify previous threads set to NULL before freeing */ 2605*dcbf3bd6SGeorge Wilson ASSERT3P(nhdr->b_l1hdr.b_pdata, ==, NULL); 2606*dcbf3bd6SGeorge Wilson } else { 2607*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2608*dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 2609*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 2610*dcbf3bd6SGeorge Wilson 2611*dcbf3bd6SGeorge Wilson /* 2612*dcbf3bd6SGeorge Wilson * If we've reached here, We must have been called from 2613*dcbf3bd6SGeorge Wilson * arc_evict_hdr(), as such we should have already been 2614*dcbf3bd6SGeorge Wilson * removed from any ghost list we were previously on 2615*dcbf3bd6SGeorge Wilson * (which protects us from racing with arc_evict_state), 2616*dcbf3bd6SGeorge Wilson * thus no locking is needed during this check. 2617*dcbf3bd6SGeorge Wilson */ 2618*dcbf3bd6SGeorge Wilson ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 2619cd1c8b85SMatthew Ahrens 2620*dcbf3bd6SGeorge Wilson /* 2621*dcbf3bd6SGeorge Wilson * A buffer must not be moved into the arc_l2c_only 2622*dcbf3bd6SGeorge Wilson * state if it's not finished being written out to the 2623*dcbf3bd6SGeorge Wilson * l2arc device. Otherwise, the b_l1hdr.b_pdata field 2624*dcbf3bd6SGeorge Wilson * might try to be accessed, even though it was removed. 2625*dcbf3bd6SGeorge Wilson */ 2626*dcbf3bd6SGeorge Wilson VERIFY(!HDR_L2_WRITING(hdr)); 2627*dcbf3bd6SGeorge Wilson VERIFY3P(hdr->b_l1hdr.b_pdata, ==, NULL); 2628fa94a07fSbrendan 2629*dcbf3bd6SGeorge Wilson #ifdef ZFS_DEBUG 2630*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_thawed != NULL) { 2631*dcbf3bd6SGeorge Wilson kmem_free(hdr->b_l1hdr.b_thawed, 1); 2632*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_thawed = NULL; 2633*dcbf3bd6SGeorge Wilson } 2634*dcbf3bd6SGeorge Wilson #endif 2635244781f1SPrakash Surya 2636*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR); 2637*dcbf3bd6SGeorge Wilson } 2638244781f1SPrakash Surya /* 2639*dcbf3bd6SGeorge Wilson * The header has been reallocated so we need to re-insert it into any 2640*dcbf3bd6SGeorge Wilson * lists it was on. 2641244781f1SPrakash Surya */ 2642*dcbf3bd6SGeorge Wilson (void) buf_hash_insert(nhdr, NULL); 2643244781f1SPrakash Surya 2644*dcbf3bd6SGeorge Wilson ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); 2645*dcbf3bd6SGeorge Wilson 2646*dcbf3bd6SGeorge Wilson mutex_enter(&dev->l2ad_mtx); 2647244781f1SPrakash Surya 2648244781f1SPrakash Surya /* 2649*dcbf3bd6SGeorge Wilson * We must place the realloc'ed header back into the list at 2650*dcbf3bd6SGeorge Wilson * the same spot. Otherwise, if it's placed earlier in the list, 2651*dcbf3bd6SGeorge Wilson * l2arc_write_buffers() could find it during the function's 2652*dcbf3bd6SGeorge Wilson * write phase, and try to write it out to the l2arc. 2653244781f1SPrakash Surya */ 2654*dcbf3bd6SGeorge Wilson list_insert_after(&dev->l2ad_buflist, hdr, nhdr); 2655*dcbf3bd6SGeorge Wilson list_remove(&dev->l2ad_buflist, hdr); 2656*dcbf3bd6SGeorge Wilson 2657*dcbf3bd6SGeorge Wilson mutex_exit(&dev->l2ad_mtx); 2658244781f1SPrakash Surya 2659244781f1SPrakash Surya /* 2660*dcbf3bd6SGeorge Wilson * Since we're using the pointer address as the tag when 2661*dcbf3bd6SGeorge Wilson * incrementing and decrementing the l2ad_alloc refcount, we 2662*dcbf3bd6SGeorge Wilson * must remove the old pointer (that we're about to destroy) and 2663*dcbf3bd6SGeorge Wilson * add the new pointer to the refcount. Otherwise we'd remove 2664*dcbf3bd6SGeorge Wilson * the wrong pointer address when calling arc_hdr_destroy() later. 2665244781f1SPrakash Surya */ 2666244781f1SPrakash Surya 2667*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr), hdr); 2668*dcbf3bd6SGeorge Wilson (void) refcount_add_many(&dev->l2ad_alloc, arc_hdr_size(nhdr), nhdr); 2669244781f1SPrakash Surya 2670*dcbf3bd6SGeorge Wilson buf_discard_identity(hdr); 2671*dcbf3bd6SGeorge Wilson kmem_cache_free(old, hdr); 2672244781f1SPrakash Surya 2673*dcbf3bd6SGeorge Wilson return (nhdr); 2674244781f1SPrakash Surya } 2675244781f1SPrakash Surya 2676bbfa8ea8SMatthew Ahrens /* 2677*dcbf3bd6SGeorge Wilson * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller. 2678*dcbf3bd6SGeorge Wilson * The buf is returned thawed since we expect the consumer to modify it. 2679bbfa8ea8SMatthew Ahrens */ 2680*dcbf3bd6SGeorge Wilson arc_buf_t * 2681*dcbf3bd6SGeorge Wilson arc_alloc_buf(spa_t *spa, int32_t size, void *tag, arc_buf_contents_t type) 2682ea8dc4b6Seschrock { 2683*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size, 2684*dcbf3bd6SGeorge Wilson ZIO_COMPRESS_OFF, type); 2685*dcbf3bd6SGeorge Wilson ASSERT(!MUTEX_HELD(HDR_LOCK(hdr))); 2686*dcbf3bd6SGeorge Wilson arc_buf_t *buf = arc_buf_alloc_impl(hdr, tag); 2687*dcbf3bd6SGeorge Wilson arc_buf_thaw(buf); 2688*dcbf3bd6SGeorge Wilson return (buf); 2689ea8dc4b6Seschrock } 2690ea8dc4b6Seschrock 2691a52fc310SPrakash Surya static void 2692a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) 2693a52fc310SPrakash Surya { 2694a52fc310SPrakash Surya l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 2695a52fc310SPrakash Surya l2arc_dev_t *dev = l2hdr->b_dev; 2696*dcbf3bd6SGeorge Wilson uint64_t asize = arc_hdr_size(hdr); 2697a52fc310SPrakash Surya 2698a52fc310SPrakash Surya ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); 2699a52fc310SPrakash Surya ASSERT(HDR_HAS_L2HDR(hdr)); 2700a52fc310SPrakash Surya 2701a52fc310SPrakash Surya list_remove(&dev->l2ad_buflist, hdr); 2702a52fc310SPrakash Surya 2703*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_l2_asize, -asize); 2704*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_l2_size, -HDR_GET_LSIZE(hdr)); 2705a52fc310SPrakash Surya 2706*dcbf3bd6SGeorge Wilson vdev_space_update(dev->l2ad_vdev, -asize, 0, 0); 2707a52fc310SPrakash Surya 2708*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&dev->l2ad_alloc, asize, hdr); 2709*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); 2710a52fc310SPrakash Surya } 2711a52fc310SPrakash Surya 2712fa9e4066Sahrens static void 2713ea8dc4b6Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr) 2714fa9e4066Sahrens { 271589c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) { 271689c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_buf == NULL || 2717*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt > 0); 271889c86e32SChris Williamson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 271989c86e32SChris Williamson ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 272089c86e32SChris Williamson } 2721ea8dc4b6Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 272289c86e32SChris Williamson ASSERT(!HDR_IN_HASH_TABLE(hdr)); 272389c86e32SChris Williamson 2724*dcbf3bd6SGeorge Wilson if (!HDR_EMPTY(hdr)) 2725*dcbf3bd6SGeorge Wilson buf_discard_identity(hdr); 2726*dcbf3bd6SGeorge Wilson 272789c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr)) { 2728a52fc310SPrakash Surya l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 2729a52fc310SPrakash Surya boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); 2730b24ab676SJeff Bonwick 2731a52fc310SPrakash Surya if (!buflist_held) 2732a52fc310SPrakash Surya mutex_enter(&dev->l2ad_mtx); 273389c86e32SChris Williamson 2734244781f1SPrakash Surya /* 2735a52fc310SPrakash Surya * Even though we checked this conditional above, we 2736a52fc310SPrakash Surya * need to check this again now that we have the 2737a52fc310SPrakash Surya * l2ad_mtx. This is because we could be racing with 2738a52fc310SPrakash Surya * another thread calling l2arc_evict() which might have 2739a52fc310SPrakash Surya * destroyed this header's L2 portion as we were waiting 2740a52fc310SPrakash Surya * to acquire the l2ad_mtx. If that happens, we don't 2741a52fc310SPrakash Surya * want to re-destroy the header's L2 portion. 2742244781f1SPrakash Surya */ 2743a52fc310SPrakash Surya if (HDR_HAS_L2HDR(hdr)) 2744a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(hdr); 2745b24ab676SJeff Bonwick 2746b24ab676SJeff Bonwick if (!buflist_held) 2747a52fc310SPrakash Surya mutex_exit(&dev->l2ad_mtx); 2748fa94a07fSbrendan } 2749fa94a07fSbrendan 2750*dcbf3bd6SGeorge Wilson if (HDR_HAS_L1HDR(hdr)) { 2751*dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 275289c86e32SChris Williamson 2753*dcbf3bd6SGeorge Wilson while (hdr->b_l1hdr.b_buf != NULL) 2754*dcbf3bd6SGeorge Wilson arc_buf_destroy_impl(hdr->b_l1hdr.b_buf, B_TRUE); 275589c86e32SChris Williamson 275689c86e32SChris Williamson #ifdef ZFS_DEBUG 275789c86e32SChris Williamson if (hdr->b_l1hdr.b_thawed != NULL) { 275889c86e32SChris Williamson kmem_free(hdr->b_l1hdr.b_thawed, 1); 275989c86e32SChris Williamson hdr->b_l1hdr.b_thawed = NULL; 276089c86e32SChris Williamson } 276189c86e32SChris Williamson #endif 2762*dcbf3bd6SGeorge Wilson 2763*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_pdata != NULL) { 2764*dcbf3bd6SGeorge Wilson arc_hdr_free_pdata(hdr); 2765*dcbf3bd6SGeorge Wilson } 27663f9d6ad7SLin Ling } 2767ea8dc4b6Seschrock 2768fa9e4066Sahrens ASSERT3P(hdr->b_hash_next, ==, NULL); 276989c86e32SChris Williamson if (HDR_HAS_L1HDR(hdr)) { 2770244781f1SPrakash Surya ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 277189c86e32SChris Williamson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 277289c86e32SChris Williamson kmem_cache_free(hdr_full_cache, hdr); 277389c86e32SChris Williamson } else { 277489c86e32SChris Williamson kmem_cache_free(hdr_l2only_cache, hdr); 277589c86e32SChris Williamson } 2776fa9e4066Sahrens } 2777fa9e4066Sahrens 2778fa9e4066Sahrens void 2779*dcbf3bd6SGeorge Wilson arc_buf_destroy(arc_buf_t *buf, void* tag) 2780ea8dc4b6Seschrock { 2781ea8dc4b6Seschrock arc_buf_hdr_t *hdr = buf->b_hdr; 2782ea8dc4b6Seschrock kmutex_t *hash_lock = HDR_LOCK(hdr); 2783fa9e4066Sahrens 278489c86e32SChris Williamson if (hdr->b_l1hdr.b_state == arc_anon) { 2785*dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 2786*dcbf3bd6SGeorge Wilson ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2787*dcbf3bd6SGeorge Wilson VERIFY0(remove_reference(hdr, NULL, tag)); 2788*dcbf3bd6SGeorge Wilson arc_hdr_destroy(hdr); 2789*dcbf3bd6SGeorge Wilson return; 2790ea8dc4b6Seschrock } 2791ea8dc4b6Seschrock 2792ea8dc4b6Seschrock mutex_enter(hash_lock); 2793*dcbf3bd6SGeorge Wilson ASSERT3P(hdr, ==, buf->b_hdr); 2794*dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 27953f9d6ad7SLin Ling ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2796*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon); 2797*dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, !=, NULL); 2798ea8dc4b6Seschrock 2799ea8dc4b6Seschrock (void) remove_reference(hdr, hash_lock, tag); 2800*dcbf3bd6SGeorge Wilson arc_buf_destroy_impl(buf, B_TRUE); 2801ea8dc4b6Seschrock mutex_exit(hash_lock); 2802fa9e4066Sahrens } 2803fa9e4066Sahrens 280489c86e32SChris Williamson int32_t 2805fa9e4066Sahrens arc_buf_size(arc_buf_t *buf) 2806fa9e4066Sahrens { 2807*dcbf3bd6SGeorge Wilson return (HDR_GET_LSIZE(buf->b_hdr)); 28089253d63dSGeorge Wilson } 28099253d63dSGeorge Wilson 2810fa9e4066Sahrens /* 2811244781f1SPrakash Surya * Evict the arc_buf_hdr that is provided as a parameter. The resultant 2812244781f1SPrakash Surya * state of the header is dependent on it's state prior to entering this 2813244781f1SPrakash Surya * function. The following transitions are possible: 2814874395d5Smaybee * 2815244781f1SPrakash Surya * - arc_mru -> arc_mru_ghost 2816244781f1SPrakash Surya * - arc_mfu -> arc_mfu_ghost 2817244781f1SPrakash Surya * - arc_mru_ghost -> arc_l2c_only 2818244781f1SPrakash Surya * - arc_mru_ghost -> deleted 2819244781f1SPrakash Surya * - arc_mfu_ghost -> arc_l2c_only 2820244781f1SPrakash Surya * - arc_mfu_ghost -> deleted 2821fa9e4066Sahrens */ 2822244781f1SPrakash Surya static int64_t 2823244781f1SPrakash Surya arc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 2824fa9e4066Sahrens { 2825244781f1SPrakash Surya arc_state_t *evicted_state, *state; 2826244781f1SPrakash Surya int64_t bytes_evicted = 0; 2827fa9e4066Sahrens 2828244781f1SPrakash Surya ASSERT(MUTEX_HELD(hash_lock)); 2829244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 2830fa9e4066Sahrens 2831244781f1SPrakash Surya state = hdr->b_l1hdr.b_state; 2832244781f1SPrakash Surya if (GHOST_STATE(state)) { 2833244781f1SPrakash Surya ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2834*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2835fa9e4066Sahrens 2836244781f1SPrakash Surya /* 2837244781f1SPrakash Surya * l2arc_write_buffers() relies on a header's L1 portion 2838*dcbf3bd6SGeorge Wilson * (i.e. its b_pdata field) during its write phase. 2839244781f1SPrakash Surya * Thus, we cannot push a header onto the arc_l2c_only 2840244781f1SPrakash Surya * state (removing it's L1 piece) until the header is 2841244781f1SPrakash Surya * done being written to the l2arc. 2842244781f1SPrakash Surya */ 2843244781f1SPrakash Surya if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { 2844244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_l2_skip); 2845244781f1SPrakash Surya return (bytes_evicted); 28463a5286a1SMatthew Ahrens } 2847244781f1SPrakash Surya 2848244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_deleted); 2849*dcbf3bd6SGeorge Wilson bytes_evicted += HDR_GET_LSIZE(hdr); 2850244781f1SPrakash Surya 2851244781f1SPrakash Surya DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); 2852244781f1SPrakash Surya 2853*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 2854244781f1SPrakash Surya if (HDR_HAS_L2HDR(hdr)) { 2855*dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_pdata == NULL); 2856244781f1SPrakash Surya /* 2857244781f1SPrakash Surya * This buffer is cached on the 2nd Level ARC; 2858244781f1SPrakash Surya * don't destroy the header. 2859244781f1SPrakash Surya */ 2860244781f1SPrakash Surya arc_change_state(arc_l2c_only, hdr, hash_lock); 28613a5286a1SMatthew Ahrens /* 2862244781f1SPrakash Surya * dropping from L1+L2 cached to L2-only, 2863244781f1SPrakash Surya * realloc to remove the L1 header. 28643a5286a1SMatthew Ahrens */ 2865244781f1SPrakash Surya hdr = arc_hdr_realloc(hdr, hdr_full_cache, 2866244781f1SPrakash Surya hdr_l2only_cache); 2867244781f1SPrakash Surya } else { 2868*dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_pdata == NULL); 2869244781f1SPrakash Surya arc_change_state(arc_anon, hdr, hash_lock); 2870244781f1SPrakash Surya arc_hdr_destroy(hdr); 28713a5286a1SMatthew Ahrens } 2872244781f1SPrakash Surya return (bytes_evicted); 28733a5286a1SMatthew Ahrens } 28743a5286a1SMatthew Ahrens 2875244781f1SPrakash Surya ASSERT(state == arc_mru || state == arc_mfu); 2876244781f1SPrakash Surya evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 2877244781f1SPrakash Surya 2878244781f1SPrakash Surya /* prefetch buffers have a minimum lifespan */ 2879244781f1SPrakash Surya if (HDR_IO_IN_PROGRESS(hdr) || 2880244781f1SPrakash Surya ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && 2881244781f1SPrakash Surya ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < 2882244781f1SPrakash Surya arc_min_prefetch_lifespan)) { 2883244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_skip); 2884244781f1SPrakash Surya return (bytes_evicted); 2885244781f1SPrakash Surya } 2886244781f1SPrakash Surya 2887244781f1SPrakash Surya ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 2888244781f1SPrakash Surya while (hdr->b_l1hdr.b_buf) { 2889244781f1SPrakash Surya arc_buf_t *buf = hdr->b_l1hdr.b_buf; 2890244781f1SPrakash Surya if (!mutex_tryenter(&buf->b_evict_lock)) { 2891244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_mutex_miss); 2892244781f1SPrakash Surya break; 289313506d1eSmaybee } 2894244781f1SPrakash Surya if (buf->b_data != NULL) 2895*dcbf3bd6SGeorge Wilson bytes_evicted += HDR_GET_LSIZE(hdr); 2896*dcbf3bd6SGeorge Wilson mutex_exit(&buf->b_evict_lock); 2897*dcbf3bd6SGeorge Wilson arc_buf_destroy_impl(buf, B_TRUE); 2898244781f1SPrakash Surya } 289969962b56SMatthew Ahrens 2900244781f1SPrakash Surya if (HDR_HAS_L2HDR(hdr)) { 2901*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr)); 2902244781f1SPrakash Surya } else { 2903*dcbf3bd6SGeorge Wilson if (l2arc_write_eligible(hdr->b_spa, hdr)) { 2904*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_evict_l2_eligible, 2905*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr)); 2906*dcbf3bd6SGeorge Wilson } else { 2907*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_evict_l2_ineligible, 2908*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr)); 2909*dcbf3bd6SGeorge Wilson } 2910244781f1SPrakash Surya } 2911244781f1SPrakash Surya 2912*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_bufcnt == 0) { 2913*dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 2914*dcbf3bd6SGeorge Wilson 2915*dcbf3bd6SGeorge Wilson bytes_evicted += arc_hdr_size(hdr); 2916*dcbf3bd6SGeorge Wilson 2917*dcbf3bd6SGeorge Wilson /* 2918*dcbf3bd6SGeorge Wilson * If this hdr is being evicted and has a compressed 2919*dcbf3bd6SGeorge Wilson * buffer then we discard it here before we change states. 2920*dcbf3bd6SGeorge Wilson * This ensures that the accounting is updated correctly 2921*dcbf3bd6SGeorge Wilson * in arc_free_data_buf(). 2922*dcbf3bd6SGeorge Wilson */ 2923*dcbf3bd6SGeorge Wilson arc_hdr_free_pdata(hdr); 2924*dcbf3bd6SGeorge Wilson 2925244781f1SPrakash Surya arc_change_state(evicted_state, hdr, hash_lock); 2926244781f1SPrakash Surya ASSERT(HDR_IN_HASH_TABLE(hdr)); 2927*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 2928244781f1SPrakash Surya DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); 2929244781f1SPrakash Surya } 2930244781f1SPrakash Surya 2931244781f1SPrakash Surya return (bytes_evicted); 2932244781f1SPrakash Surya } 2933244781f1SPrakash Surya 2934244781f1SPrakash Surya static uint64_t 2935244781f1SPrakash Surya arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, 2936244781f1SPrakash Surya uint64_t spa, int64_t bytes) 2937244781f1SPrakash Surya { 2938244781f1SPrakash Surya multilist_sublist_t *mls; 2939244781f1SPrakash Surya uint64_t bytes_evicted = 0; 2940244781f1SPrakash Surya arc_buf_hdr_t *hdr; 2941244781f1SPrakash Surya kmutex_t *hash_lock; 2942244781f1SPrakash Surya int evict_count = 0; 2943244781f1SPrakash Surya 2944244781f1SPrakash Surya ASSERT3P(marker, !=, NULL); 2945244781f1SPrakash Surya IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 2946244781f1SPrakash Surya 2947244781f1SPrakash Surya mls = multilist_sublist_lock(ml, idx); 2948244781f1SPrakash Surya 2949244781f1SPrakash Surya for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL; 2950244781f1SPrakash Surya hdr = multilist_sublist_prev(mls, marker)) { 2951244781f1SPrakash Surya if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) || 2952244781f1SPrakash Surya (evict_count >= zfs_arc_evict_batch_limit)) 2953244781f1SPrakash Surya break; 295469962b56SMatthew Ahrens 295569962b56SMatthew Ahrens /* 2956244781f1SPrakash Surya * To keep our iteration location, move the marker 2957244781f1SPrakash Surya * forward. Since we're not holding hdr's hash lock, we 2958244781f1SPrakash Surya * must be very careful and not remove 'hdr' from the 2959244781f1SPrakash Surya * sublist. Otherwise, other consumers might mistake the 2960244781f1SPrakash Surya * 'hdr' as not being on a sublist when they call the 2961244781f1SPrakash Surya * multilist_link_active() function (they all rely on 2962244781f1SPrakash Surya * the hash lock protecting concurrent insertions and 2963244781f1SPrakash Surya * removals). multilist_sublist_move_forward() was 2964244781f1SPrakash Surya * specifically implemented to ensure this is the case 2965244781f1SPrakash Surya * (only 'marker' will be removed and re-inserted). 2966244781f1SPrakash Surya */ 2967244781f1SPrakash Surya multilist_sublist_move_forward(mls, marker); 2968244781f1SPrakash Surya 2969244781f1SPrakash Surya /* 2970244781f1SPrakash Surya * The only case where the b_spa field should ever be 2971244781f1SPrakash Surya * zero, is the marker headers inserted by 2972244781f1SPrakash Surya * arc_evict_state(). It's possible for multiple threads 2973244781f1SPrakash Surya * to be calling arc_evict_state() concurrently (e.g. 2974244781f1SPrakash Surya * dsl_pool_close() and zio_inject_fault()), so we must 2975244781f1SPrakash Surya * skip any markers we see from these other threads. 297669962b56SMatthew Ahrens */ 2977244781f1SPrakash Surya if (hdr->b_spa == 0) 2978244781f1SPrakash Surya continue; 2979244781f1SPrakash Surya 2980244781f1SPrakash Surya /* we're only interested in evicting buffers of a certain spa */ 2981244781f1SPrakash Surya if (spa != 0 && hdr->b_spa != spa) { 2982244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_skip); 298369962b56SMatthew Ahrens continue; 298469962b56SMatthew Ahrens } 298569962b56SMatthew Ahrens 29867adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 29875ea40c06SBrendan Gregg - Sun Microsystems 2988244781f1SPrakash Surya /* 2989244781f1SPrakash Surya * We aren't calling this function from any code path 2990244781f1SPrakash Surya * that would already be holding a hash lock, so we're 2991244781f1SPrakash Surya * asserting on this assumption to be defensive in case 2992244781f1SPrakash Surya * this ever changes. Without this check, it would be 2993244781f1SPrakash Surya * possible to incorrectly increment arcstat_mutex_miss 2994244781f1SPrakash Surya * below (e.g. if the code changed such that we called 2995244781f1SPrakash Surya * this function with a hash lock held). 2996244781f1SPrakash Surya */ 2997244781f1SPrakash Surya ASSERT(!MUTEX_HELD(hash_lock)); 299844cb6abcSbmc 2999244781f1SPrakash Surya if (mutex_tryenter(hash_lock)) { 3000244781f1SPrakash Surya uint64_t evicted = arc_evict_hdr(hdr, hash_lock); 3001244781f1SPrakash Surya mutex_exit(hash_lock); 3002fa9e4066Sahrens 3003244781f1SPrakash Surya bytes_evicted += evicted; 3004fa9e4066Sahrens 3005244781f1SPrakash Surya /* 3006244781f1SPrakash Surya * If evicted is zero, arc_evict_hdr() must have 3007244781f1SPrakash Surya * decided to skip this header, don't increment 3008244781f1SPrakash Surya * evict_count in this case. 3009244781f1SPrakash Surya */ 3010244781f1SPrakash Surya if (evicted != 0) 3011244781f1SPrakash Surya evict_count++; 301244cb6abcSbmc 3013244781f1SPrakash Surya /* 3014244781f1SPrakash Surya * If arc_size isn't overflowing, signal any 3015244781f1SPrakash Surya * threads that might happen to be waiting. 3016244781f1SPrakash Surya * 3017244781f1SPrakash Surya * For each header evicted, we wake up a single 3018244781f1SPrakash Surya * thread. If we used cv_broadcast, we could 3019244781f1SPrakash Surya * wake up "too many" threads causing arc_size 3020244781f1SPrakash Surya * to significantly overflow arc_c; since 3021244781f1SPrakash Surya * arc_get_data_buf() doesn't check for overflow 3022244781f1SPrakash Surya * when it's woken up (it doesn't because it's 3023244781f1SPrakash Surya * possible for the ARC to be overflowing while 3024244781f1SPrakash Surya * full of un-evictable buffers, and the 3025244781f1SPrakash Surya * function should proceed in this case). 3026244781f1SPrakash Surya * 3027244781f1SPrakash Surya * If threads are left sleeping, due to not 3028244781f1SPrakash Surya * using cv_broadcast, they will be woken up 3029244781f1SPrakash Surya * just before arc_reclaim_thread() sleeps. 3030244781f1SPrakash Surya */ 3031244781f1SPrakash Surya mutex_enter(&arc_reclaim_lock); 3032244781f1SPrakash Surya if (!arc_is_overflowing()) 3033244781f1SPrakash Surya cv_signal(&arc_reclaim_waiters_cv); 3034244781f1SPrakash Surya mutex_exit(&arc_reclaim_lock); 3035244781f1SPrakash Surya } else { 3036244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_mutex_miss); 3037244781f1SPrakash Surya } 3038244781f1SPrakash Surya } 3039f4d2e9e6Smaybee 3040244781f1SPrakash Surya multilist_sublist_unlock(mls); 304144cb6abcSbmc 3042244781f1SPrakash Surya return (bytes_evicted); 3043fa9e4066Sahrens } 3044fa9e4066Sahrens 3045fa9e4066Sahrens /* 3046244781f1SPrakash Surya * Evict buffers from the given arc state, until we've removed the 3047244781f1SPrakash Surya * specified number of bytes. Move the removed buffers to the 3048244781f1SPrakash Surya * appropriate evict state. 3049244781f1SPrakash Surya * 3050244781f1SPrakash Surya * This function makes a "best effort". It skips over any buffers 3051244781f1SPrakash Surya * it can't get a hash_lock on, and so, may not catch all candidates. 3052244781f1SPrakash Surya * It may also return without evicting as much space as requested. 3053244781f1SPrakash Surya * 3054244781f1SPrakash Surya * If bytes is specified using the special value ARC_EVICT_ALL, this 3055244781f1SPrakash Surya * will evict all available (i.e. unlocked and evictable) buffers from 3056244781f1SPrakash Surya * the given arc state; which is used by arc_flush(). 3057fa9e4066Sahrens */ 3058244781f1SPrakash Surya static uint64_t 3059244781f1SPrakash Surya arc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes, 3060244781f1SPrakash Surya arc_buf_contents_t type) 3061fa9e4066Sahrens { 3062244781f1SPrakash Surya uint64_t total_evicted = 0; 3063244781f1SPrakash Surya multilist_t *ml = &state->arcs_list[type]; 3064244781f1SPrakash Surya int num_sublists; 3065244781f1SPrakash Surya arc_buf_hdr_t **markers; 3066fa9e4066Sahrens 3067244781f1SPrakash Surya IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 3068b802aa8cSSanjeev Bagewadi 3069244781f1SPrakash Surya num_sublists = multilist_get_num_sublists(ml); 3070b802aa8cSSanjeev Bagewadi 3071244781f1SPrakash Surya /* 3072244781f1SPrakash Surya * If we've tried to evict from each sublist, made some 3073244781f1SPrakash Surya * progress, but still have not hit the target number of bytes 3074244781f1SPrakash Surya * to evict, we want to keep trying. The markers allow us to 3075244781f1SPrakash Surya * pick up where we left off for each individual sublist, rather 3076244781f1SPrakash Surya * than starting from the tail each time. 3077244781f1SPrakash Surya */ 3078244781f1SPrakash Surya markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); 3079244781f1SPrakash Surya for (int i = 0; i < num_sublists; i++) { 3080244781f1SPrakash Surya markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); 308169962b56SMatthew Ahrens 308269962b56SMatthew Ahrens /* 3083244781f1SPrakash Surya * A b_spa of 0 is used to indicate that this header is 3084244781f1SPrakash Surya * a marker. This fact is used in arc_adjust_type() and 3085244781f1SPrakash Surya * arc_evict_state_impl(). 308669962b56SMatthew Ahrens */ 3087244781f1SPrakash Surya markers[i]->b_spa = 0; 3088fa94a07fSbrendan 3089244781f1SPrakash Surya multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 3090244781f1SPrakash Surya multilist_sublist_insert_tail(mls, markers[i]); 3091244781f1SPrakash Surya multilist_sublist_unlock(mls); 3092244781f1SPrakash Surya } 3093fa94a07fSbrendan 3094244781f1SPrakash Surya /* 3095244781f1SPrakash Surya * While we haven't hit our target number of bytes to evict, or 3096244781f1SPrakash Surya * we're evicting all available buffers. 3097244781f1SPrakash Surya */ 3098244781f1SPrakash Surya while (total_evicted < bytes || bytes == ARC_EVICT_ALL) { 3099244781f1SPrakash Surya /* 3100244781f1SPrakash Surya * Start eviction using a randomly selected sublist, 3101244781f1SPrakash Surya * this is to try and evenly balance eviction across all 3102244781f1SPrakash Surya * sublists. Always starting at the same sublist 3103244781f1SPrakash Surya * (e.g. index 0) would cause evictions to favor certain 3104244781f1SPrakash Surya * sublists over others. 3105244781f1SPrakash Surya */ 3106244781f1SPrakash Surya int sublist_idx = multilist_get_random_index(ml); 3107244781f1SPrakash Surya uint64_t scan_evicted = 0; 3108244781f1SPrakash Surya 3109244781f1SPrakash Surya for (int i = 0; i < num_sublists; i++) { 3110244781f1SPrakash Surya uint64_t bytes_remaining; 3111244781f1SPrakash Surya uint64_t bytes_evicted; 3112244781f1SPrakash Surya 3113244781f1SPrakash Surya if (bytes == ARC_EVICT_ALL) 3114244781f1SPrakash Surya bytes_remaining = ARC_EVICT_ALL; 3115244781f1SPrakash Surya else if (total_evicted < bytes) 3116244781f1SPrakash Surya bytes_remaining = bytes - total_evicted; 3117244781f1SPrakash Surya else 3118fa9e4066Sahrens break; 3119244781f1SPrakash Surya 3120244781f1SPrakash Surya bytes_evicted = arc_evict_state_impl(ml, sublist_idx, 3121244781f1SPrakash Surya markers[sublist_idx], spa, bytes_remaining); 3122244781f1SPrakash Surya 3123244781f1SPrakash Surya scan_evicted += bytes_evicted; 3124244781f1SPrakash Surya total_evicted += bytes_evicted; 3125244781f1SPrakash Surya 3126244781f1SPrakash Surya /* we've reached the end, wrap to the beginning */ 3127244781f1SPrakash Surya if (++sublist_idx >= num_sublists) 3128244781f1SPrakash Surya sublist_idx = 0; 3129244781f1SPrakash Surya } 3130244781f1SPrakash Surya 3131244781f1SPrakash Surya /* 3132244781f1SPrakash Surya * If we didn't evict anything during this scan, we have 3133244781f1SPrakash Surya * no reason to believe we'll evict more during another 3134244781f1SPrakash Surya * scan, so break the loop. 3135244781f1SPrakash Surya */ 3136244781f1SPrakash Surya if (scan_evicted == 0) { 3137244781f1SPrakash Surya /* This isn't possible, let's make that obvious */ 3138244781f1SPrakash Surya ASSERT3S(bytes, !=, 0); 3139244781f1SPrakash Surya 3140b802aa8cSSanjeev Bagewadi /* 3141244781f1SPrakash Surya * When bytes is ARC_EVICT_ALL, the only way to 3142244781f1SPrakash Surya * break the loop is when scan_evicted is zero. 3143244781f1SPrakash Surya * In that case, we actually have evicted enough, 3144244781f1SPrakash Surya * so we don't want to increment the kstat. 3145b802aa8cSSanjeev Bagewadi */ 3146244781f1SPrakash Surya if (bytes != ARC_EVICT_ALL) { 3147244781f1SPrakash Surya ASSERT3S(total_evicted, <, bytes); 3148244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_evict_not_enough); 3149244781f1SPrakash Surya } 3150244781f1SPrakash Surya 3151244781f1SPrakash Surya break; 315269962b56SMatthew Ahrens } 3153244781f1SPrakash Surya } 3154244781f1SPrakash Surya 3155244781f1SPrakash Surya for (int i = 0; i < num_sublists; i++) { 3156244781f1SPrakash Surya multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 3157244781f1SPrakash Surya multilist_sublist_remove(mls, markers[i]); 3158244781f1SPrakash Surya multilist_sublist_unlock(mls); 315969962b56SMatthew Ahrens 3160244781f1SPrakash Surya kmem_cache_free(hdr_full_cache, markers[i]); 3161fa9e4066Sahrens } 3162244781f1SPrakash Surya kmem_free(markers, sizeof (*markers) * num_sublists); 3163fa9e4066Sahrens 3164244781f1SPrakash Surya return (total_evicted); 3165244781f1SPrakash Surya } 3166244781f1SPrakash Surya 3167244781f1SPrakash Surya /* 3168244781f1SPrakash Surya * Flush all "evictable" data of the given type from the arc state 3169244781f1SPrakash Surya * specified. This will not evict any "active" buffers (i.e. referenced). 3170244781f1SPrakash Surya * 3171*dcbf3bd6SGeorge Wilson * When 'retry' is set to B_FALSE, the function will make a single pass 3172244781f1SPrakash Surya * over the state and evict any buffers that it can. Since it doesn't 3173244781f1SPrakash Surya * continually retry the eviction, it might end up leaving some buffers 3174244781f1SPrakash Surya * in the ARC due to lock misses. 3175244781f1SPrakash Surya * 3176*dcbf3bd6SGeorge Wilson * When 'retry' is set to B_TRUE, the function will continually retry the 3177244781f1SPrakash Surya * eviction until *all* evictable buffers have been removed from the 3178244781f1SPrakash Surya * state. As a result, if concurrent insertions into the state are 3179244781f1SPrakash Surya * allowed (e.g. if the ARC isn't shutting down), this function might 3180244781f1SPrakash Surya * wind up in an infinite loop, continually trying to evict buffers. 3181244781f1SPrakash Surya */ 3182244781f1SPrakash Surya static uint64_t 3183244781f1SPrakash Surya arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, 3184244781f1SPrakash Surya boolean_t retry) 3185244781f1SPrakash Surya { 3186244781f1SPrakash Surya uint64_t evicted = 0; 3187244781f1SPrakash Surya 3188*dcbf3bd6SGeorge Wilson while (refcount_count(&state->arcs_esize[type]) != 0) { 3189244781f1SPrakash Surya evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); 3190244781f1SPrakash Surya 3191244781f1SPrakash Surya if (!retry) 3192244781f1SPrakash Surya break; 31930e8c6158Smaybee } 31940e8c6158Smaybee 3195244781f1SPrakash Surya return (evicted); 3196244781f1SPrakash Surya } 3197244781f1SPrakash Surya 3198244781f1SPrakash Surya /* 3199244781f1SPrakash Surya * Evict the specified number of bytes from the state specified, 3200244781f1SPrakash Surya * restricting eviction to the spa and type given. This function 3201244781f1SPrakash Surya * prevents us from trying to evict more from a state's list than 3202244781f1SPrakash Surya * is "evictable", and to skip evicting altogether when passed a 3203244781f1SPrakash Surya * negative value for "bytes". In contrast, arc_evict_state() will 3204244781f1SPrakash Surya * evict everything it can, when passed a negative value for "bytes". 3205244781f1SPrakash Surya */ 3206244781f1SPrakash Surya static uint64_t 3207244781f1SPrakash Surya arc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes, 3208244781f1SPrakash Surya arc_buf_contents_t type) 3209244781f1SPrakash Surya { 3210244781f1SPrakash Surya int64_t delta; 3211244781f1SPrakash Surya 3212*dcbf3bd6SGeorge Wilson if (bytes > 0 && refcount_count(&state->arcs_esize[type]) > 0) { 3213*dcbf3bd6SGeorge Wilson delta = MIN(refcount_count(&state->arcs_esize[type]), bytes); 3214244781f1SPrakash Surya return (arc_evict_state(state, spa, delta, type)); 3215fa9e4066Sahrens } 3216fa9e4066Sahrens 3217244781f1SPrakash Surya return (0); 3218fa9e4066Sahrens } 3219fa9e4066Sahrens 3220244781f1SPrakash Surya /* 3221244781f1SPrakash Surya * Evict metadata buffers from the cache, such that arc_meta_used is 3222244781f1SPrakash Surya * capped by the arc_meta_limit tunable. 3223244781f1SPrakash Surya */ 3224244781f1SPrakash Surya static uint64_t 3225244781f1SPrakash Surya arc_adjust_meta(void) 3226244781f1SPrakash Surya { 3227244781f1SPrakash Surya uint64_t total_evicted = 0; 3228244781f1SPrakash Surya int64_t target; 3229244781f1SPrakash Surya 3230244781f1SPrakash Surya /* 3231244781f1SPrakash Surya * If we're over the meta limit, we want to evict enough 3232244781f1SPrakash Surya * metadata to get back under the meta limit. We don't want to 3233244781f1SPrakash Surya * evict so much that we drop the MRU below arc_p, though. If 3234244781f1SPrakash Surya * we're over the meta limit more than we're over arc_p, we 3235244781f1SPrakash Surya * evict some from the MRU here, and some from the MFU below. 3236244781f1SPrakash Surya */ 3237244781f1SPrakash Surya target = MIN((int64_t)(arc_meta_used - arc_meta_limit), 32382fd872a7SPrakash Surya (int64_t)(refcount_count(&arc_anon->arcs_size) + 32392fd872a7SPrakash Surya refcount_count(&arc_mru->arcs_size) - arc_p)); 3240244781f1SPrakash Surya 3241244781f1SPrakash Surya total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3242244781f1SPrakash Surya 3243244781f1SPrakash Surya /* 3244244781f1SPrakash Surya * Similar to the above, we want to evict enough bytes to get us 3245244781f1SPrakash Surya * below the meta limit, but not so much as to drop us below the 3246244781f1SPrakash Surya * space alloted to the MFU (which is defined as arc_c - arc_p). 3247244781f1SPrakash Surya */ 3248244781f1SPrakash Surya target = MIN((int64_t)(arc_meta_used - arc_meta_limit), 32492fd872a7SPrakash Surya (int64_t)(refcount_count(&arc_mfu->arcs_size) - (arc_c - arc_p))); 3250244781f1SPrakash Surya 3251244781f1SPrakash Surya total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3252244781f1SPrakash Surya 3253244781f1SPrakash Surya return (total_evicted); 3254244781f1SPrakash Surya } 3255244781f1SPrakash Surya 3256244781f1SPrakash Surya /* 3257244781f1SPrakash Surya * Return the type of the oldest buffer in the given arc state 3258244781f1SPrakash Surya * 3259244781f1SPrakash Surya * This function will select a random sublist of type ARC_BUFC_DATA and 3260244781f1SPrakash Surya * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist 3261244781f1SPrakash Surya * is compared, and the type which contains the "older" buffer will be 3262244781f1SPrakash Surya * returned. 3263244781f1SPrakash Surya */ 3264244781f1SPrakash Surya static arc_buf_contents_t 3265244781f1SPrakash Surya arc_adjust_type(arc_state_t *state) 3266244781f1SPrakash Surya { 3267244781f1SPrakash Surya multilist_t *data_ml = &state->arcs_list[ARC_BUFC_DATA]; 3268244781f1SPrakash Surya multilist_t *meta_ml = &state->arcs_list[ARC_BUFC_METADATA]; 3269244781f1SPrakash Surya int data_idx = multilist_get_random_index(data_ml); 3270244781f1SPrakash Surya int meta_idx = multilist_get_random_index(meta_ml); 3271244781f1SPrakash Surya multilist_sublist_t *data_mls; 3272244781f1SPrakash Surya multilist_sublist_t *meta_mls; 3273244781f1SPrakash Surya arc_buf_contents_t type; 3274244781f1SPrakash Surya arc_buf_hdr_t *data_hdr; 3275244781f1SPrakash Surya arc_buf_hdr_t *meta_hdr; 3276244781f1SPrakash Surya 3277244781f1SPrakash Surya /* 3278244781f1SPrakash Surya * We keep the sublist lock until we're finished, to prevent 3279244781f1SPrakash Surya * the headers from being destroyed via arc_evict_state(). 3280244781f1SPrakash Surya */ 3281244781f1SPrakash Surya data_mls = multilist_sublist_lock(data_ml, data_idx); 3282244781f1SPrakash Surya meta_mls = multilist_sublist_lock(meta_ml, meta_idx); 3283244781f1SPrakash Surya 3284244781f1SPrakash Surya /* 3285244781f1SPrakash Surya * These two loops are to ensure we skip any markers that 3286244781f1SPrakash Surya * might be at the tail of the lists due to arc_evict_state(). 3287244781f1SPrakash Surya */ 3288244781f1SPrakash Surya 3289244781f1SPrakash Surya for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; 3290244781f1SPrakash Surya data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { 3291244781f1SPrakash Surya if (data_hdr->b_spa != 0) 3292244781f1SPrakash Surya break; 3293244781f1SPrakash Surya } 3294244781f1SPrakash Surya 3295244781f1SPrakash Surya for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; 3296244781f1SPrakash Surya meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { 3297244781f1SPrakash Surya if (meta_hdr->b_spa != 0) 3298244781f1SPrakash Surya break; 3299244781f1SPrakash Surya } 3300244781f1SPrakash Surya 3301244781f1SPrakash Surya if (data_hdr == NULL && meta_hdr == NULL) { 3302244781f1SPrakash Surya type = ARC_BUFC_DATA; 3303244781f1SPrakash Surya } else if (data_hdr == NULL) { 3304244781f1SPrakash Surya ASSERT3P(meta_hdr, !=, NULL); 3305244781f1SPrakash Surya type = ARC_BUFC_METADATA; 3306244781f1SPrakash Surya } else if (meta_hdr == NULL) { 3307244781f1SPrakash Surya ASSERT3P(data_hdr, !=, NULL); 3308244781f1SPrakash Surya type = ARC_BUFC_DATA; 3309244781f1SPrakash Surya } else { 3310244781f1SPrakash Surya ASSERT3P(data_hdr, !=, NULL); 3311244781f1SPrakash Surya ASSERT3P(meta_hdr, !=, NULL); 3312244781f1SPrakash Surya 3313244781f1SPrakash Surya /* The headers can't be on the sublist without an L1 header */ 3314244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(data_hdr)); 3315244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(meta_hdr)); 3316244781f1SPrakash Surya 3317244781f1SPrakash Surya if (data_hdr->b_l1hdr.b_arc_access < 3318244781f1SPrakash Surya meta_hdr->b_l1hdr.b_arc_access) { 3319244781f1SPrakash Surya type = ARC_BUFC_DATA; 3320244781f1SPrakash Surya } else { 3321244781f1SPrakash Surya type = ARC_BUFC_METADATA; 3322244781f1SPrakash Surya } 3323244781f1SPrakash Surya } 3324244781f1SPrakash Surya 3325244781f1SPrakash Surya multilist_sublist_unlock(meta_mls); 3326244781f1SPrakash Surya multilist_sublist_unlock(data_mls); 3327244781f1SPrakash Surya 3328244781f1SPrakash Surya return (type); 3329244781f1SPrakash Surya } 3330244781f1SPrakash Surya 3331244781f1SPrakash Surya /* 3332244781f1SPrakash Surya * Evict buffers from the cache, such that arc_size is capped by arc_c. 3333244781f1SPrakash Surya */ 3334244781f1SPrakash Surya static uint64_t 3335fa9e4066Sahrens arc_adjust(void) 3336fa9e4066Sahrens { 3337244781f1SPrakash Surya uint64_t total_evicted = 0; 3338244781f1SPrakash Surya uint64_t bytes; 3339244781f1SPrakash Surya int64_t target; 3340fa9e4066Sahrens 33415a98e54bSBrendan Gregg - Sun Microsystems /* 3342244781f1SPrakash Surya * If we're over arc_meta_limit, we want to correct that before 3343244781f1SPrakash Surya * potentially evicting data buffers below. 33445a98e54bSBrendan Gregg - Sun Microsystems */ 3345244781f1SPrakash Surya total_evicted += arc_adjust_meta(); 33465a98e54bSBrendan Gregg - Sun Microsystems 3347244781f1SPrakash Surya /* 3348244781f1SPrakash Surya * Adjust MRU size 3349244781f1SPrakash Surya * 3350244781f1SPrakash Surya * If we're over the target cache size, we want to evict enough 3351244781f1SPrakash Surya * from the list to get back to our target size. We don't want 3352244781f1SPrakash Surya * to evict too much from the MRU, such that it drops below 3353244781f1SPrakash Surya * arc_p. So, if we're over our target cache size more than 3354244781f1SPrakash Surya * the MRU is over arc_p, we'll evict enough to get back to 3355244781f1SPrakash Surya * arc_p here, and then evict more from the MFU below. 3356244781f1SPrakash Surya */ 3357244781f1SPrakash Surya target = MIN((int64_t)(arc_size - arc_c), 33582fd872a7SPrakash Surya (int64_t)(refcount_count(&arc_anon->arcs_size) + 33592fd872a7SPrakash Surya refcount_count(&arc_mru->arcs_size) + arc_meta_used - arc_p)); 3360fa9e4066Sahrens 3361244781f1SPrakash Surya /* 3362244781f1SPrakash Surya * If we're below arc_meta_min, always prefer to evict data. 3363244781f1SPrakash Surya * Otherwise, try to satisfy the requested number of bytes to 3364244781f1SPrakash Surya * evict from the type which contains older buffers; in an 3365244781f1SPrakash Surya * effort to keep newer buffers in the cache regardless of their 3366244781f1SPrakash Surya * type. If we cannot satisfy the number of bytes from this 3367244781f1SPrakash Surya * type, spill over into the next type. 3368244781f1SPrakash Surya */ 3369244781f1SPrakash Surya if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA && 3370244781f1SPrakash Surya arc_meta_used > arc_meta_min) { 3371244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3372244781f1SPrakash Surya total_evicted += bytes; 33730e8c6158Smaybee 3374244781f1SPrakash Surya /* 3375244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3376244781f1SPrakash Surya * metadata, we try to get the rest from data. 3377244781f1SPrakash Surya */ 3378244781f1SPrakash Surya target -= bytes; 3379244781f1SPrakash Surya 3380244781f1SPrakash Surya total_evicted += 3381244781f1SPrakash Surya arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3382244781f1SPrakash Surya } else { 3383244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3384244781f1SPrakash Surya total_evicted += bytes; 3385244781f1SPrakash Surya 3386244781f1SPrakash Surya /* 3387244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3388244781f1SPrakash Surya * data, we try to get the rest from metadata. 3389244781f1SPrakash Surya */ 3390244781f1SPrakash Surya target -= bytes; 3391244781f1SPrakash Surya 3392244781f1SPrakash Surya total_evicted += 3393244781f1SPrakash Surya arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3394fa9e4066Sahrens } 3395fa9e4066Sahrens 33965a98e54bSBrendan Gregg - Sun Microsystems /* 33975a98e54bSBrendan Gregg - Sun Microsystems * Adjust MFU size 3398244781f1SPrakash Surya * 3399244781f1SPrakash Surya * Now that we've tried to evict enough from the MRU to get its 3400244781f1SPrakash Surya * size back to arc_p, if we're still above the target cache 3401244781f1SPrakash Surya * size, we evict the rest from the MFU. 34025a98e54bSBrendan Gregg - Sun Microsystems */ 3403244781f1SPrakash Surya target = arc_size - arc_c; 3404fa9e4066Sahrens 340531c46cf2SAlek Pinchuk if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA && 3406244781f1SPrakash Surya arc_meta_used > arc_meta_min) { 3407244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3408244781f1SPrakash Surya total_evicted += bytes; 34095a98e54bSBrendan Gregg - Sun Microsystems 3410244781f1SPrakash Surya /* 3411244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3412244781f1SPrakash Surya * metadata, we try to get the rest from data. 3413244781f1SPrakash Surya */ 3414244781f1SPrakash Surya target -= bytes; 3415244781f1SPrakash Surya 3416244781f1SPrakash Surya total_evicted += 3417244781f1SPrakash Surya arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3418244781f1SPrakash Surya } else { 3419244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3420244781f1SPrakash Surya total_evicted += bytes; 3421244781f1SPrakash Surya 3422244781f1SPrakash Surya /* 3423244781f1SPrakash Surya * If we couldn't evict our target number of bytes from 3424244781f1SPrakash Surya * data, we try to get the rest from data. 3425244781f1SPrakash Surya */ 3426244781f1SPrakash Surya target -= bytes; 3427fa9e4066Sahrens 3428244781f1SPrakash Surya total_evicted += 3429244781f1SPrakash Surya arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 34305a98e54bSBrendan Gregg - Sun Microsystems } 3431fa9e4066Sahrens 34325a98e54bSBrendan Gregg - Sun Microsystems /* 34335a98e54bSBrendan Gregg - Sun Microsystems * Adjust ghost lists 3434244781f1SPrakash Surya * 3435244781f1SPrakash Surya * In addition to the above, the ARC also defines target values 3436244781f1SPrakash Surya * for the ghost lists. The sum of the mru list and mru ghost 3437244781f1SPrakash Surya * list should never exceed the target size of the cache, and 3438244781f1SPrakash Surya * the sum of the mru list, mfu list, mru ghost list, and mfu 3439244781f1SPrakash Surya * ghost list should never exceed twice the target size of the 3440244781f1SPrakash Surya * cache. The following logic enforces these limits on the ghost 3441244781f1SPrakash Surya * caches, and evicts from them as needed. 34425a98e54bSBrendan Gregg - Sun Microsystems */ 34432fd872a7SPrakash Surya target = refcount_count(&arc_mru->arcs_size) + 34442fd872a7SPrakash Surya refcount_count(&arc_mru_ghost->arcs_size) - arc_c; 3445fa9e4066Sahrens 3446244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); 3447244781f1SPrakash Surya total_evicted += bytes; 3448fa9e4066Sahrens 3449244781f1SPrakash Surya target -= bytes; 34500e8c6158Smaybee 3451244781f1SPrakash Surya total_evicted += 3452244781f1SPrakash Surya arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); 34535a98e54bSBrendan Gregg - Sun Microsystems 3454244781f1SPrakash Surya /* 3455244781f1SPrakash Surya * We assume the sum of the mru list and mfu list is less than 3456244781f1SPrakash Surya * or equal to arc_c (we enforced this above), which means we 3457244781f1SPrakash Surya * can use the simpler of the two equations below: 3458244781f1SPrakash Surya * 3459244781f1SPrakash Surya * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c 3460244781f1SPrakash Surya * mru ghost + mfu ghost <= arc_c 3461244781f1SPrakash Surya */ 34622fd872a7SPrakash Surya target = refcount_count(&arc_mru_ghost->arcs_size) + 34632fd872a7SPrakash Surya refcount_count(&arc_mfu_ghost->arcs_size) - arc_c; 3464244781f1SPrakash Surya 3465244781f1SPrakash Surya bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); 3466244781f1SPrakash Surya total_evicted += bytes; 3467244781f1SPrakash Surya 3468244781f1SPrakash Surya target -= bytes; 3469244781f1SPrakash Surya 3470244781f1SPrakash Surya total_evicted += 3471244781f1SPrakash Surya arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); 3472244781f1SPrakash Surya 3473244781f1SPrakash Surya return (total_evicted); 3474fa9e4066Sahrens } 3475fa9e4066Sahrens 3476fa9e4066Sahrens void 3477244781f1SPrakash Surya arc_flush(spa_t *spa, boolean_t retry) 3478fa9e4066Sahrens { 3479ac05c741SMark Maybee uint64_t guid = 0; 3480ac05c741SMark Maybee 3481244781f1SPrakash Surya /* 3482*dcbf3bd6SGeorge Wilson * If retry is B_TRUE, a spa must not be specified since we have 3483244781f1SPrakash Surya * no good way to determine if all of a spa's buffers have been 3484244781f1SPrakash Surya * evicted from an arc state. 3485244781f1SPrakash Surya */ 3486244781f1SPrakash Surya ASSERT(!retry || spa == 0); 3487244781f1SPrakash Surya 348889c86e32SChris Williamson if (spa != NULL) 3489e9103aaeSGarrett D'Amore guid = spa_load_guid(spa); 3490ac05c741SMark Maybee 3491244781f1SPrakash Surya (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); 3492244781f1SPrakash Surya (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); 3493244781f1SPrakash Surya 3494244781f1SPrakash Surya (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); 3495244781f1SPrakash Surya (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); 3496874395d5Smaybee 3497244781f1SPrakash Surya (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); 3498244781f1SPrakash Surya (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); 3499244781f1SPrakash Surya 3500244781f1SPrakash Surya (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); 3501244781f1SPrakash Surya (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); 3502fa9e4066Sahrens } 3503fa9e4066Sahrens 3504fa9e4066Sahrens void 35052ec99e3eSMatthew Ahrens arc_shrink(int64_t to_free) 3506fa9e4066Sahrens { 350744cb6abcSbmc if (arc_c > arc_c_min) { 3508fa9e4066Sahrens 350944cb6abcSbmc if (arc_c > arc_c_min + to_free) 351044cb6abcSbmc atomic_add_64(&arc_c, -to_free); 351149e3519aSmaybee else 351244cb6abcSbmc arc_c = arc_c_min; 351344cb6abcSbmc 351444cb6abcSbmc atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 351544cb6abcSbmc if (arc_c > arc_size) 351644cb6abcSbmc arc_c = MAX(arc_size, arc_c_min); 351744cb6abcSbmc if (arc_p > arc_c) 351844cb6abcSbmc arc_p = (arc_c >> 1); 351944cb6abcSbmc ASSERT(arc_c >= arc_c_min); 352044cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 352149e3519aSmaybee } 3522fa9e4066Sahrens 352344cb6abcSbmc if (arc_size > arc_c) 3524244781f1SPrakash Surya (void) arc_adjust(); 3525fa9e4066Sahrens } 3526fa9e4066Sahrens 35272ec99e3eSMatthew Ahrens typedef enum free_memory_reason_t { 35282ec99e3eSMatthew Ahrens FMR_UNKNOWN, 35292ec99e3eSMatthew Ahrens FMR_NEEDFREE, 35302ec99e3eSMatthew Ahrens FMR_LOTSFREE, 35312ec99e3eSMatthew Ahrens FMR_SWAPFS_MINFREE, 35322ec99e3eSMatthew Ahrens FMR_PAGES_PP_MAXIMUM, 35332ec99e3eSMatthew Ahrens FMR_HEAP_ARENA, 35342ec99e3eSMatthew Ahrens FMR_ZIO_ARENA, 35352ec99e3eSMatthew Ahrens } free_memory_reason_t; 35362ec99e3eSMatthew Ahrens 35372ec99e3eSMatthew Ahrens int64_t last_free_memory; 35382ec99e3eSMatthew Ahrens free_memory_reason_t last_free_reason; 35392ec99e3eSMatthew Ahrens 354094dd93aeSGeorge Wilson /* 35412ec99e3eSMatthew Ahrens * Additional reserve of pages for pp_reserve. 354294dd93aeSGeorge Wilson */ 35432ec99e3eSMatthew Ahrens int64_t arc_pages_pp_reserve = 64; 3544fa9e4066Sahrens 35452ec99e3eSMatthew Ahrens /* 35462ec99e3eSMatthew Ahrens * Additional reserve of pages for swapfs. 35472ec99e3eSMatthew Ahrens */ 35482ec99e3eSMatthew Ahrens int64_t arc_swapfs_reserve = 64; 35493cff2f43Sstans 35502ec99e3eSMatthew Ahrens /* 35512ec99e3eSMatthew Ahrens * Return the amount of memory that can be consumed before reclaim will be 35522ec99e3eSMatthew Ahrens * needed. Positive if there is sufficient free memory, negative indicates 35532ec99e3eSMatthew Ahrens * the amount of memory that needs to be freed up. 35542ec99e3eSMatthew Ahrens */ 35552ec99e3eSMatthew Ahrens static int64_t 35562ec99e3eSMatthew Ahrens arc_available_memory(void) 35572ec99e3eSMatthew Ahrens { 35582ec99e3eSMatthew Ahrens int64_t lowest = INT64_MAX; 35592ec99e3eSMatthew Ahrens int64_t n; 35602ec99e3eSMatthew Ahrens free_memory_reason_t r = FMR_UNKNOWN; 35613cff2f43Sstans 35622ec99e3eSMatthew Ahrens #ifdef _KERNEL 35632ec99e3eSMatthew Ahrens if (needfree > 0) { 35642ec99e3eSMatthew Ahrens n = PAGESIZE * (-needfree); 35652ec99e3eSMatthew Ahrens if (n < lowest) { 35662ec99e3eSMatthew Ahrens lowest = n; 35672ec99e3eSMatthew Ahrens r = FMR_NEEDFREE; 35682ec99e3eSMatthew Ahrens } 35692ec99e3eSMatthew Ahrens } 3570fa9e4066Sahrens 3571fa9e4066Sahrens /* 3572fa9e4066Sahrens * check that we're out of range of the pageout scanner. It starts to 3573fa9e4066Sahrens * schedule paging if freemem is less than lotsfree and needfree. 3574fa9e4066Sahrens * lotsfree is the high-water mark for pageout, and needfree is the 3575fa9e4066Sahrens * number of needed free pages. We add extra pages here to make sure 3576fa9e4066Sahrens * the scanner doesn't start up while we're freeing memory. 3577fa9e4066Sahrens */ 35782ec99e3eSMatthew Ahrens n = PAGESIZE * (freemem - lotsfree - needfree - desfree); 35792ec99e3eSMatthew Ahrens if (n < lowest) { 35802ec99e3eSMatthew Ahrens lowest = n; 35812ec99e3eSMatthew Ahrens r = FMR_LOTSFREE; 35822ec99e3eSMatthew Ahrens } 3583fa9e4066Sahrens 3584fa9e4066Sahrens /* 3585fa9e4066Sahrens * check to make sure that swapfs has enough space so that anon 3586fa94a07fSbrendan * reservations can still succeed. anon_resvmem() checks that the 3587fa9e4066Sahrens * availrmem is greater than swapfs_minfree, and the number of reserved 3588fa9e4066Sahrens * swap pages. We also add a bit of extra here just to prevent 3589fa9e4066Sahrens * circumstances from getting really dire. 3590fa9e4066Sahrens */ 35912ec99e3eSMatthew Ahrens n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve - 35922ec99e3eSMatthew Ahrens desfree - arc_swapfs_reserve); 35932ec99e3eSMatthew Ahrens if (n < lowest) { 35942ec99e3eSMatthew Ahrens lowest = n; 35952ec99e3eSMatthew Ahrens r = FMR_SWAPFS_MINFREE; 35962ec99e3eSMatthew Ahrens } 35972ec99e3eSMatthew Ahrens 3598fa9e4066Sahrens 3599cf746768SBryan Cantrill /* 3600cf746768SBryan Cantrill * Check that we have enough availrmem that memory locking (e.g., via 3601cf746768SBryan Cantrill * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 3602cf746768SBryan Cantrill * stores the number of pages that cannot be locked; when availrmem 3603cf746768SBryan Cantrill * drops below pages_pp_maximum, page locking mechanisms such as 3604cf746768SBryan Cantrill * page_pp_lock() will fail.) 3605cf746768SBryan Cantrill */ 36062ec99e3eSMatthew Ahrens n = PAGESIZE * (availrmem - pages_pp_maximum - 36072ec99e3eSMatthew Ahrens arc_pages_pp_reserve); 36082ec99e3eSMatthew Ahrens if (n < lowest) { 36092ec99e3eSMatthew Ahrens lowest = n; 36102ec99e3eSMatthew Ahrens r = FMR_PAGES_PP_MAXIMUM; 36112ec99e3eSMatthew Ahrens } 3612cf746768SBryan Cantrill 36135dc8af33Smaybee #if defined(__i386) 3614fa9e4066Sahrens /* 3615fa9e4066Sahrens * If we're on an i386 platform, it's possible that we'll exhaust the 3616fa9e4066Sahrens * kernel heap space before we ever run out of available physical 3617fa9e4066Sahrens * memory. Most checks of the size of the heap_area compare against 3618fa9e4066Sahrens * tune.t_minarmem, which is the minimum available real memory that we 3619fa9e4066Sahrens * can have in the system. However, this is generally fixed at 25 pages 3620fa9e4066Sahrens * which is so low that it's useless. In this comparison, we seek to 3621fa9e4066Sahrens * calculate the total heap-size, and reclaim if more than 3/4ths of the 3622fa94a07fSbrendan * heap is allocated. (Or, in the calculation, if less than 1/4th is 3623fa9e4066Sahrens * free) 3624fa9e4066Sahrens */ 36250dd053d7SPrakash Surya n = (int64_t)vmem_size(heap_arena, VMEM_FREE) - 36262ec99e3eSMatthew Ahrens (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2); 36272ec99e3eSMatthew Ahrens if (n < lowest) { 36282ec99e3eSMatthew Ahrens lowest = n; 36292ec99e3eSMatthew Ahrens r = FMR_HEAP_ARENA; 36302ec99e3eSMatthew Ahrens } 3631fa9e4066Sahrens #endif 3632fa9e4066Sahrens 363394dd93aeSGeorge Wilson /* 363494dd93aeSGeorge Wilson * If zio data pages are being allocated out of a separate heap segment, 363594dd93aeSGeorge Wilson * then enforce that the size of available vmem for this arena remains 36360dd053d7SPrakash Surya * above about 1/4th (1/(2^arc_zio_arena_free_shift)) free. 363794dd93aeSGeorge Wilson * 36380dd053d7SPrakash Surya * Note that reducing the arc_zio_arena_free_shift keeps more virtual 36390dd053d7SPrakash Surya * memory (in the zio_arena) free, which can avoid memory 36400dd053d7SPrakash Surya * fragmentation issues. 364194dd93aeSGeorge Wilson */ 36422ec99e3eSMatthew Ahrens if (zio_arena != NULL) { 36430dd053d7SPrakash Surya n = (int64_t)vmem_size(zio_arena, VMEM_FREE) - 36440dd053d7SPrakash Surya (vmem_size(zio_arena, VMEM_ALLOC) >> 36450dd053d7SPrakash Surya arc_zio_arena_free_shift); 36462ec99e3eSMatthew Ahrens if (n < lowest) { 36472ec99e3eSMatthew Ahrens lowest = n; 36482ec99e3eSMatthew Ahrens r = FMR_ZIO_ARENA; 36492ec99e3eSMatthew Ahrens } 36502ec99e3eSMatthew Ahrens } 3651fa9e4066Sahrens #else 36522ec99e3eSMatthew Ahrens /* Every 100 calls, free a small amount */ 3653fa9e4066Sahrens if (spa_get_random(100) == 0) 36542ec99e3eSMatthew Ahrens lowest = -1024; 3655fa9e4066Sahrens #endif 36562ec99e3eSMatthew Ahrens 36572ec99e3eSMatthew Ahrens last_free_memory = lowest; 36582ec99e3eSMatthew Ahrens last_free_reason = r; 36592ec99e3eSMatthew Ahrens 36602ec99e3eSMatthew Ahrens return (lowest); 36612ec99e3eSMatthew Ahrens } 36622ec99e3eSMatthew Ahrens 36632ec99e3eSMatthew Ahrens 36642ec99e3eSMatthew Ahrens /* 36652ec99e3eSMatthew Ahrens * Determine if the system is under memory pressure and is asking 3666*dcbf3bd6SGeorge Wilson * to reclaim memory. A return value of B_TRUE indicates that the system 36672ec99e3eSMatthew Ahrens * is under memory pressure and that the arc should adjust accordingly. 36682ec99e3eSMatthew Ahrens */ 36692ec99e3eSMatthew Ahrens static boolean_t 36702ec99e3eSMatthew Ahrens arc_reclaim_needed(void) 36712ec99e3eSMatthew Ahrens { 36722ec99e3eSMatthew Ahrens return (arc_available_memory() < 0); 3673fa9e4066Sahrens } 3674fa9e4066Sahrens 3675fa9e4066Sahrens static void 36762ec99e3eSMatthew Ahrens arc_kmem_reap_now(void) 3677fa9e4066Sahrens { 3678fa9e4066Sahrens size_t i; 3679fa9e4066Sahrens kmem_cache_t *prev_cache = NULL; 3680ad23a2dbSjohansen kmem_cache_t *prev_data_cache = NULL; 3681fa9e4066Sahrens extern kmem_cache_t *zio_buf_cache[]; 3682ad23a2dbSjohansen extern kmem_cache_t *zio_data_buf_cache[]; 368383803b51SGeorge Wilson extern kmem_cache_t *range_seg_cache; 3684fa9e4066Sahrens 3685033f9833Sek #ifdef _KERNEL 36860e8c6158Smaybee if (arc_meta_used >= arc_meta_limit) { 36870e8c6158Smaybee /* 36880e8c6158Smaybee * We are exceeding our meta-data cache limit. 36890e8c6158Smaybee * Purge some DNLC entries to release holds on meta-data. 36900e8c6158Smaybee */ 36910e8c6158Smaybee dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 36920e8c6158Smaybee } 36935dc8af33Smaybee #if defined(__i386) 36945dc8af33Smaybee /* 36955dc8af33Smaybee * Reclaim unused memory from all kmem caches. 36965dc8af33Smaybee */ 36975dc8af33Smaybee kmem_reap(); 36985dc8af33Smaybee #endif 3699033f9833Sek #endif 3700033f9833Sek 3701fa9e4066Sahrens for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 3702fa9e4066Sahrens if (zio_buf_cache[i] != prev_cache) { 3703fa9e4066Sahrens prev_cache = zio_buf_cache[i]; 3704fa9e4066Sahrens kmem_cache_reap_now(zio_buf_cache[i]); 3705fa9e4066Sahrens } 3706ad23a2dbSjohansen if (zio_data_buf_cache[i] != prev_data_cache) { 3707ad23a2dbSjohansen prev_data_cache = zio_data_buf_cache[i]; 3708ad23a2dbSjohansen kmem_cache_reap_now(zio_data_buf_cache[i]); 3709ad23a2dbSjohansen } 3710fa9e4066Sahrens } 3711ea8dc4b6Seschrock kmem_cache_reap_now(buf_cache); 371289c86e32SChris Williamson kmem_cache_reap_now(hdr_full_cache); 371389c86e32SChris Williamson kmem_cache_reap_now(hdr_l2only_cache); 371483803b51SGeorge Wilson kmem_cache_reap_now(range_seg_cache); 371594dd93aeSGeorge Wilson 37162ec99e3eSMatthew Ahrens if (zio_arena != NULL) { 37172ec99e3eSMatthew Ahrens /* 37182ec99e3eSMatthew Ahrens * Ask the vmem arena to reclaim unused memory from its 37192ec99e3eSMatthew Ahrens * quantum caches. 37202ec99e3eSMatthew Ahrens */ 372194dd93aeSGeorge Wilson vmem_qcache_reap(zio_arena); 37222ec99e3eSMatthew Ahrens } 3723fa9e4066Sahrens } 3724fa9e4066Sahrens 3725244781f1SPrakash Surya /* 3726244781f1SPrakash Surya * Threads can block in arc_get_data_buf() waiting for this thread to evict 3727244781f1SPrakash Surya * enough data and signal them to proceed. When this happens, the threads in 3728244781f1SPrakash Surya * arc_get_data_buf() are sleeping while holding the hash lock for their 3729244781f1SPrakash Surya * particular arc header. Thus, we must be careful to never sleep on a 3730244781f1SPrakash Surya * hash lock in this thread. This is to prevent the following deadlock: 3731244781f1SPrakash Surya * 3732244781f1SPrakash Surya * - Thread A sleeps on CV in arc_get_data_buf() holding hash lock "L", 3733244781f1SPrakash Surya * waiting for the reclaim thread to signal it. 3734244781f1SPrakash Surya * 3735244781f1SPrakash Surya * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter, 3736244781f1SPrakash Surya * fails, and goes to sleep forever. 3737244781f1SPrakash Surya * 3738244781f1SPrakash Surya * This possible deadlock is avoided by always acquiring a hash lock 3739244781f1SPrakash Surya * using mutex_tryenter() from arc_reclaim_thread(). 3740244781f1SPrakash Surya */ 3741fa9e4066Sahrens static void 3742fa9e4066Sahrens arc_reclaim_thread(void) 3743fa9e4066Sahrens { 3744a8f6344fSEli Rosenthal hrtime_t growtime = 0; 3745fa9e4066Sahrens callb_cpr_t cpr; 3746fa9e4066Sahrens 3747244781f1SPrakash Surya CALLB_CPR_INIT(&cpr, &arc_reclaim_lock, callb_generic_cpr, FTAG); 3748fa9e4066Sahrens 3749244781f1SPrakash Surya mutex_enter(&arc_reclaim_lock); 3750244781f1SPrakash Surya while (!arc_reclaim_thread_exit) { 37512ec99e3eSMatthew Ahrens int64_t free_memory = arc_available_memory(); 3752244781f1SPrakash Surya uint64_t evicted = 0; 3753244781f1SPrakash Surya 3754*dcbf3bd6SGeorge Wilson /* 3755*dcbf3bd6SGeorge Wilson * This is necessary in order for the mdb ::arc dcmd to 3756*dcbf3bd6SGeorge Wilson * show up to date information. Since the ::arc command 3757*dcbf3bd6SGeorge Wilson * does not call the kstat's update function, without 3758*dcbf3bd6SGeorge Wilson * this call, the command may show stale stats for the 3759*dcbf3bd6SGeorge Wilson * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even 3760*dcbf3bd6SGeorge Wilson * with this change, the data might be up to 1 second 3761*dcbf3bd6SGeorge Wilson * out of date; but that should suffice. The arc_state_t 3762*dcbf3bd6SGeorge Wilson * structures can be queried directly if more accurate 3763*dcbf3bd6SGeorge Wilson * information is needed. 3764*dcbf3bd6SGeorge Wilson */ 3765*dcbf3bd6SGeorge Wilson if (arc_ksp != NULL) 3766*dcbf3bd6SGeorge Wilson arc_ksp->ks_update(arc_ksp, KSTAT_READ); 3767*dcbf3bd6SGeorge Wilson 3768244781f1SPrakash Surya mutex_exit(&arc_reclaim_lock); 3769244781f1SPrakash Surya 37702ec99e3eSMatthew Ahrens if (free_memory < 0) { 3771fa9e4066Sahrens 37722ec99e3eSMatthew Ahrens arc_no_grow = B_TRUE; 37732ec99e3eSMatthew Ahrens arc_warm = B_TRUE; 3774fa9e4066Sahrens 37752ec99e3eSMatthew Ahrens /* 37762ec99e3eSMatthew Ahrens * Wait at least zfs_grow_retry (default 60) seconds 37772ec99e3eSMatthew Ahrens * before considering growing. 37782ec99e3eSMatthew Ahrens */ 3779a8f6344fSEli Rosenthal growtime = gethrtime() + SEC2NSEC(arc_grow_retry); 3780fa9e4066Sahrens 37812ec99e3eSMatthew Ahrens arc_kmem_reap_now(); 3782fa9e4066Sahrens 37832ec99e3eSMatthew Ahrens /* 37842ec99e3eSMatthew Ahrens * If we are still low on memory, shrink the ARC 37852ec99e3eSMatthew Ahrens * so that we have arc_shrink_min free space. 37862ec99e3eSMatthew Ahrens */ 37872ec99e3eSMatthew Ahrens free_memory = arc_available_memory(); 37882ec99e3eSMatthew Ahrens 37892ec99e3eSMatthew Ahrens int64_t to_free = 37902ec99e3eSMatthew Ahrens (arc_c >> arc_shrink_shift) - free_memory; 37912ec99e3eSMatthew Ahrens if (to_free > 0) { 37922ec99e3eSMatthew Ahrens #ifdef _KERNEL 37932ec99e3eSMatthew Ahrens to_free = MAX(to_free, ptob(needfree)); 37942ec99e3eSMatthew Ahrens #endif 37952ec99e3eSMatthew Ahrens arc_shrink(to_free); 37962ec99e3eSMatthew Ahrens } 37972ec99e3eSMatthew Ahrens } else if (free_memory < arc_c >> arc_no_grow_shift) { 37982ec99e3eSMatthew Ahrens arc_no_grow = B_TRUE; 3799a8f6344fSEli Rosenthal } else if (gethrtime() >= growtime) { 38002ec99e3eSMatthew Ahrens arc_no_grow = B_FALSE; 3801fa9e4066Sahrens } 3802fa9e4066Sahrens 3803244781f1SPrakash Surya evicted = arc_adjust(); 3804244781f1SPrakash Surya 3805244781f1SPrakash Surya mutex_enter(&arc_reclaim_lock); 3806244781f1SPrakash Surya 3807244781f1SPrakash Surya /* 3808244781f1SPrakash Surya * If evicted is zero, we couldn't evict anything via 3809244781f1SPrakash Surya * arc_adjust(). This could be due to hash lock 3810244781f1SPrakash Surya * collisions, but more likely due to the majority of 3811244781f1SPrakash Surya * arc buffers being unevictable. Therefore, even if 3812244781f1SPrakash Surya * arc_size is above arc_c, another pass is unlikely to 3813244781f1SPrakash Surya * be helpful and could potentially cause us to enter an 3814244781f1SPrakash Surya * infinite loop. 3815244781f1SPrakash Surya */ 3816244781f1SPrakash Surya if (arc_size <= arc_c || evicted == 0) { 3817244781f1SPrakash Surya /* 3818244781f1SPrakash Surya * We're either no longer overflowing, or we 3819244781f1SPrakash Surya * can't evict anything more, so we should wake 3820244781f1SPrakash Surya * up any threads before we go to sleep. 3821244781f1SPrakash Surya */ 3822244781f1SPrakash Surya cv_broadcast(&arc_reclaim_waiters_cv); 3823244781f1SPrakash Surya 3824244781f1SPrakash Surya /* 3825244781f1SPrakash Surya * Block until signaled, or after one second (we 3826244781f1SPrakash Surya * might need to perform arc_kmem_reap_now() 3827244781f1SPrakash Surya * even if we aren't being signalled) 3828244781f1SPrakash Surya */ 3829244781f1SPrakash Surya CALLB_CPR_SAFE_BEGIN(&cpr); 3830a8f6344fSEli Rosenthal (void) cv_timedwait_hires(&arc_reclaim_thread_cv, 3831a8f6344fSEli Rosenthal &arc_reclaim_lock, SEC2NSEC(1), MSEC2NSEC(1), 0); 3832244781f1SPrakash Surya CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_lock); 3833244781f1SPrakash Surya } 3834244781f1SPrakash Surya } 3835244781f1SPrakash Surya 3836*dcbf3bd6SGeorge Wilson arc_reclaim_thread_exit = B_FALSE; 3837244781f1SPrakash Surya cv_broadcast(&arc_reclaim_thread_cv); 3838244781f1SPrakash Surya CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_lock */ 3839244781f1SPrakash Surya thread_exit(); 3840244781f1SPrakash Surya } 3841244781f1SPrakash Surya 3842ea8dc4b6Seschrock /* 3843ea8dc4b6Seschrock * Adapt arc info given the number of bytes we are trying to add and 3844ea8dc4b6Seschrock * the state that we are comming from. This function is only called 3845ea8dc4b6Seschrock * when we are adding new content to the cache. 3846ea8dc4b6Seschrock */ 3847fa9e4066Sahrens static void 3848ea8dc4b6Seschrock arc_adapt(int bytes, arc_state_t *state) 3849fa9e4066Sahrens { 3850ea8dc4b6Seschrock int mult; 38515a98e54bSBrendan Gregg - Sun Microsystems uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 38522fd872a7SPrakash Surya int64_t mrug_size = refcount_count(&arc_mru_ghost->arcs_size); 38532fd872a7SPrakash Surya int64_t mfug_size = refcount_count(&arc_mfu_ghost->arcs_size); 3854ea8dc4b6Seschrock 3855fa94a07fSbrendan if (state == arc_l2c_only) 3856fa94a07fSbrendan return; 3857fa94a07fSbrendan 3858ea8dc4b6Seschrock ASSERT(bytes > 0); 3859fa9e4066Sahrens /* 3860ea8dc4b6Seschrock * Adapt the target size of the MRU list: 3861ea8dc4b6Seschrock * - if we just hit in the MRU ghost list, then increase 3862ea8dc4b6Seschrock * the target size of the MRU list. 3863ea8dc4b6Seschrock * - if we just hit in the MFU ghost list, then increase 3864ea8dc4b6Seschrock * the target size of the MFU list by decreasing the 3865ea8dc4b6Seschrock * target size of the MRU list. 3866fa9e4066Sahrens */ 386744cb6abcSbmc if (state == arc_mru_ghost) { 38682fd872a7SPrakash Surya mult = (mrug_size >= mfug_size) ? 1 : (mfug_size / mrug_size); 38693e4e8481STom Erickson mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 3870ea8dc4b6Seschrock 38715a98e54bSBrendan Gregg - Sun Microsystems arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 387244cb6abcSbmc } else if (state == arc_mfu_ghost) { 38735a98e54bSBrendan Gregg - Sun Microsystems uint64_t delta; 38745a98e54bSBrendan Gregg - Sun Microsystems 38752fd872a7SPrakash Surya mult = (mfug_size >= mrug_size) ? 1 : (mrug_size / mfug_size); 38763e4e8481STom Erickson mult = MIN(mult, 10); 3877ea8dc4b6Seschrock 38785a98e54bSBrendan Gregg - Sun Microsystems delta = MIN(bytes * mult, arc_p); 38795a98e54bSBrendan Gregg - Sun Microsystems arc_p = MAX(arc_p_min, arc_p - delta); 3880ea8dc4b6Seschrock } 388144cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 3882fa9e4066Sahrens 3883fa9e4066Sahrens if (arc_reclaim_needed()) { 3884244781f1SPrakash Surya cv_signal(&arc_reclaim_thread_cv); 3885fa9e4066Sahrens return; 3886fa9e4066Sahrens } 3887fa9e4066Sahrens 388844cb6abcSbmc if (arc_no_grow) 3889fa9e4066Sahrens return; 3890fa9e4066Sahrens 389144cb6abcSbmc if (arc_c >= arc_c_max) 3892ea8dc4b6Seschrock return; 3893ea8dc4b6Seschrock 3894fa9e4066Sahrens /* 3895ea8dc4b6Seschrock * If we're within (2 * maxblocksize) bytes of the target 3896ea8dc4b6Seschrock * cache size, increment the target cache size 3897fa9e4066Sahrens */ 389844cb6abcSbmc if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 389944cb6abcSbmc atomic_add_64(&arc_c, (int64_t)bytes); 390044cb6abcSbmc if (arc_c > arc_c_max) 390144cb6abcSbmc arc_c = arc_c_max; 390244cb6abcSbmc else if (state == arc_anon) 390344cb6abcSbmc atomic_add_64(&arc_p, (int64_t)bytes); 390444cb6abcSbmc if (arc_p > arc_c) 390544cb6abcSbmc arc_p = arc_c; 3906fa9e4066Sahrens } 390744cb6abcSbmc ASSERT((int64_t)arc_p >= 0); 3908fa9e4066Sahrens } 3909fa9e4066Sahrens 3910fa9e4066Sahrens /* 3911244781f1SPrakash Surya * Check if arc_size has grown past our upper threshold, determined by 3912244781f1SPrakash Surya * zfs_arc_overflow_shift. 3913fa9e4066Sahrens */ 3914244781f1SPrakash Surya static boolean_t 3915244781f1SPrakash Surya arc_is_overflowing(void) 3916fa9e4066Sahrens { 3917244781f1SPrakash Surya /* Always allow at least one block of overflow */ 3918244781f1SPrakash Surya uint64_t overflow = MAX(SPA_MAXBLOCKSIZE, 3919244781f1SPrakash Surya arc_c >> zfs_arc_overflow_shift); 3920fa9e4066Sahrens 3921244781f1SPrakash Surya return (arc_size >= arc_c + overflow); 3922fa9e4066Sahrens } 3923fa9e4066Sahrens 3924fa9e4066Sahrens /* 3925*dcbf3bd6SGeorge Wilson * Allocate a block and return it to the caller. If we are hitting the 3926*dcbf3bd6SGeorge Wilson * hard limit for the cache size, we must sleep, waiting for the eviction 3927*dcbf3bd6SGeorge Wilson * thread to catch up. If we're past the target size but below the hard 3928*dcbf3bd6SGeorge Wilson * limit, we'll only signal the reclaim thread and continue on. 3929fa9e4066Sahrens */ 3930*dcbf3bd6SGeorge Wilson static void * 3931*dcbf3bd6SGeorge Wilson arc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 3932fa9e4066Sahrens { 3933*dcbf3bd6SGeorge Wilson void *datap = NULL; 3934*dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 3935*dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 3936fa9e4066Sahrens 393744eda4d7Smaybee arc_adapt(size, state); 3938fa9e4066Sahrens 393944eda4d7Smaybee /* 3940244781f1SPrakash Surya * If arc_size is currently overflowing, and has grown past our 3941244781f1SPrakash Surya * upper limit, we must be adding data faster than the evict 3942244781f1SPrakash Surya * thread can evict. Thus, to ensure we don't compound the 3943244781f1SPrakash Surya * problem by adding more data and forcing arc_size to grow even 3944244781f1SPrakash Surya * further past it's target size, we halt and wait for the 3945244781f1SPrakash Surya * eviction thread to catch up. 3946244781f1SPrakash Surya * 3947244781f1SPrakash Surya * It's also possible that the reclaim thread is unable to evict 3948244781f1SPrakash Surya * enough buffers to get arc_size below the overflow limit (e.g. 3949244781f1SPrakash Surya * due to buffers being un-evictable, or hash lock collisions). 3950244781f1SPrakash Surya * In this case, we want to proceed regardless if we're 3951244781f1SPrakash Surya * overflowing; thus we don't use a while loop here. 395244eda4d7Smaybee */ 3953244781f1SPrakash Surya if (arc_is_overflowing()) { 3954244781f1SPrakash Surya mutex_enter(&arc_reclaim_lock); 3955244781f1SPrakash Surya 3956244781f1SPrakash Surya /* 3957244781f1SPrakash Surya * Now that we've acquired the lock, we may no longer be 3958244781f1SPrakash Surya * over the overflow limit, lets check. 3959244781f1SPrakash Surya * 3960244781f1SPrakash Surya * We're ignoring the case of spurious wake ups. If that 3961244781f1SPrakash Surya * were to happen, it'd let this thread consume an ARC 3962244781f1SPrakash Surya * buffer before it should have (i.e. before we're under 3963244781f1SPrakash Surya * the overflow limit and were signalled by the reclaim 3964244781f1SPrakash Surya * thread). As long as that is a rare occurrence, it 3965244781f1SPrakash Surya * shouldn't cause any harm. 3966244781f1SPrakash Surya */ 3967244781f1SPrakash Surya if (arc_is_overflowing()) { 3968244781f1SPrakash Surya cv_signal(&arc_reclaim_thread_cv); 3969244781f1SPrakash Surya cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); 3970ad23a2dbSjohansen } 3971244781f1SPrakash Surya 3972244781f1SPrakash Surya mutex_exit(&arc_reclaim_lock); 397344eda4d7Smaybee } 397444eda4d7Smaybee 3975*dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 3976244781f1SPrakash Surya if (type == ARC_BUFC_METADATA) { 3977*dcbf3bd6SGeorge Wilson datap = zio_buf_alloc(size); 3978244781f1SPrakash Surya arc_space_consume(size, ARC_SPACE_META); 3979fa9e4066Sahrens } else { 3980244781f1SPrakash Surya ASSERT(type == ARC_BUFC_DATA); 3981*dcbf3bd6SGeorge Wilson datap = zio_data_buf_alloc(size); 3982244781f1SPrakash Surya arc_space_consume(size, ARC_SPACE_DATA); 398344eda4d7Smaybee } 3984244781f1SPrakash Surya 398544eda4d7Smaybee /* 398644eda4d7Smaybee * Update the state size. Note that ghost states have a 398744eda4d7Smaybee * "ghost size" and so don't need to be updated. 398844eda4d7Smaybee */ 3989*dcbf3bd6SGeorge Wilson if (!GHOST_STATE(state)) { 399044eda4d7Smaybee 3991*dcbf3bd6SGeorge Wilson (void) refcount_add_many(&state->arcs_size, size, tag); 3992244781f1SPrakash Surya 3993244781f1SPrakash Surya /* 3994244781f1SPrakash Surya * If this is reached via arc_read, the link is 3995244781f1SPrakash Surya * protected by the hash lock. If reached via 3996244781f1SPrakash Surya * arc_buf_alloc, the header should not be accessed by 3997244781f1SPrakash Surya * any other thread. And, if reached via arc_read_done, 3998244781f1SPrakash Surya * the hash lock will protect it if it's found in the 3999244781f1SPrakash Surya * hash table; otherwise no other thread should be 4000244781f1SPrakash Surya * trying to [add|remove]_reference it. 4001244781f1SPrakash Surya */ 4002244781f1SPrakash Surya if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 400389c86e32SChris Williamson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4004*dcbf3bd6SGeorge Wilson (void) refcount_add_many(&state->arcs_esize[type], 4005*dcbf3bd6SGeorge Wilson size, tag); 4006fa9e4066Sahrens } 4007*dcbf3bd6SGeorge Wilson 4008641fbdaeSmaybee /* 4009641fbdaeSmaybee * If we are growing the cache, and we are adding anonymous 401044cb6abcSbmc * data, and we have outgrown arc_p, update arc_p 4011641fbdaeSmaybee */ 401289c86e32SChris Williamson if (arc_size < arc_c && hdr->b_l1hdr.b_state == arc_anon && 40132fd872a7SPrakash Surya (refcount_count(&arc_anon->arcs_size) + 40142fd872a7SPrakash Surya refcount_count(&arc_mru->arcs_size) > arc_p)) 401544cb6abcSbmc arc_p = MIN(arc_c, arc_p + size); 4016fa9e4066Sahrens } 4017*dcbf3bd6SGeorge Wilson return (datap); 4018*dcbf3bd6SGeorge Wilson } 4019*dcbf3bd6SGeorge Wilson 4020*dcbf3bd6SGeorge Wilson /* 4021*dcbf3bd6SGeorge Wilson * Free the arc data buffer. 4022*dcbf3bd6SGeorge Wilson */ 4023*dcbf3bd6SGeorge Wilson static void 4024*dcbf3bd6SGeorge Wilson arc_free_data_buf(arc_buf_hdr_t *hdr, void *data, uint64_t size, void *tag) 4025*dcbf3bd6SGeorge Wilson { 4026*dcbf3bd6SGeorge Wilson arc_state_t *state = hdr->b_l1hdr.b_state; 4027*dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 4028*dcbf3bd6SGeorge Wilson 4029*dcbf3bd6SGeorge Wilson /* protected by hash lock, if in the hash table */ 4030*dcbf3bd6SGeorge Wilson if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 4031*dcbf3bd6SGeorge Wilson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4032*dcbf3bd6SGeorge Wilson ASSERT(state != arc_anon && state != arc_l2c_only); 4033*dcbf3bd6SGeorge Wilson 4034*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 4035*dcbf3bd6SGeorge Wilson size, tag); 4036*dcbf3bd6SGeorge Wilson } 4037*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_size, size, tag); 4038*dcbf3bd6SGeorge Wilson 4039*dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 4040*dcbf3bd6SGeorge Wilson if (type == ARC_BUFC_METADATA) { 4041*dcbf3bd6SGeorge Wilson zio_buf_free(data, size); 4042*dcbf3bd6SGeorge Wilson arc_space_return(size, ARC_SPACE_META); 4043*dcbf3bd6SGeorge Wilson } else { 4044*dcbf3bd6SGeorge Wilson ASSERT(type == ARC_BUFC_DATA); 4045*dcbf3bd6SGeorge Wilson zio_data_buf_free(data, size); 4046*dcbf3bd6SGeorge Wilson arc_space_return(size, ARC_SPACE_DATA); 4047*dcbf3bd6SGeorge Wilson } 4048fa9e4066Sahrens } 4049fa9e4066Sahrens 4050fa9e4066Sahrens /* 4051fa9e4066Sahrens * This routine is called whenever a buffer is accessed. 4052ea8dc4b6Seschrock * NOTE: the hash lock is dropped in this function. 4053fa9e4066Sahrens */ 4054fa9e4066Sahrens static void 40557adb730bSGeorge Wilson arc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 4056fa9e4066Sahrens { 4057d3d50737SRafael Vanoni clock_t now; 4058d3d50737SRafael Vanoni 4059fa9e4066Sahrens ASSERT(MUTEX_HELD(hash_lock)); 406089c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 4061fa9e4066Sahrens 406289c86e32SChris Williamson if (hdr->b_l1hdr.b_state == arc_anon) { 4063fa9e4066Sahrens /* 4064fa9e4066Sahrens * This buffer is not in the cache, and does not 4065fa9e4066Sahrens * appear in our "ghost" list. Add the new buffer 4066fa9e4066Sahrens * to the MRU state. 4067fa9e4066Sahrens */ 4068fa9e4066Sahrens 406989c86e32SChris Williamson ASSERT0(hdr->b_l1hdr.b_arc_access); 407089c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 40717adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 40727adb730bSGeorge Wilson arc_change_state(arc_mru, hdr, hash_lock); 4073fa9e4066Sahrens 407489c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mru) { 4075d3d50737SRafael Vanoni now = ddi_get_lbolt(); 4076d3d50737SRafael Vanoni 4077fa9e4066Sahrens /* 407813506d1eSmaybee * If this buffer is here because of a prefetch, then either: 407913506d1eSmaybee * - clear the flag if this is a "referencing" read 408013506d1eSmaybee * (any subsequent access will bump this into the MFU state). 408113506d1eSmaybee * or 408213506d1eSmaybee * - move the buffer to the head of the list if this is 408313506d1eSmaybee * another prefetch (to make it less likely to be evicted). 4084fa9e4066Sahrens */ 408589c86e32SChris Williamson if (HDR_PREFETCH(hdr)) { 408689c86e32SChris Williamson if (refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 4087244781f1SPrakash Surya /* link protected by hash lock */ 4088244781f1SPrakash Surya ASSERT(multilist_link_active( 408989c86e32SChris Williamson &hdr->b_l1hdr.b_arc_node)); 409013506d1eSmaybee } else { 4091*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); 409244cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_hits); 409313506d1eSmaybee } 409489c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = now; 4095fa9e4066Sahrens return; 4096fa9e4066Sahrens } 4097fa9e4066Sahrens 4098fa9e4066Sahrens /* 4099fa9e4066Sahrens * This buffer has been "accessed" only once so far, 4100fa9e4066Sahrens * but it is still in the cache. Move it to the MFU 4101fa9e4066Sahrens * state. 4102fa9e4066Sahrens */ 410389c86e32SChris Williamson if (now > hdr->b_l1hdr.b_arc_access + ARC_MINTIME) { 4104fa9e4066Sahrens /* 4105fa9e4066Sahrens * More than 125ms have passed since we 4106fa9e4066Sahrens * instantiated this buffer. Move it to the 4107fa9e4066Sahrens * most frequently used state. 4108fa9e4066Sahrens */ 410989c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = now; 41107adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 41117adb730bSGeorge Wilson arc_change_state(arc_mfu, hdr, hash_lock); 4112fa9e4066Sahrens } 411344cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_hits); 411489c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { 4115fa9e4066Sahrens arc_state_t *new_state; 4116fa9e4066Sahrens /* 4117fa9e4066Sahrens * This buffer has been "accessed" recently, but 4118fa9e4066Sahrens * was evicted from the cache. Move it to the 4119fa9e4066Sahrens * MFU state. 4120fa9e4066Sahrens */ 4121fa9e4066Sahrens 412289c86e32SChris Williamson if (HDR_PREFETCH(hdr)) { 412344cb6abcSbmc new_state = arc_mru; 412489c86e32SChris Williamson if (refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) 4125*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); 41267adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 4127fa9e4066Sahrens } else { 412844cb6abcSbmc new_state = arc_mfu; 41297adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4130fa9e4066Sahrens } 4131fa9e4066Sahrens 413289c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 41337adb730bSGeorge Wilson arc_change_state(new_state, hdr, hash_lock); 4134fa9e4066Sahrens 413544cb6abcSbmc ARCSTAT_BUMP(arcstat_mru_ghost_hits); 413689c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mfu) { 4137fa9e4066Sahrens /* 4138fa9e4066Sahrens * This buffer has been accessed more than once and is 4139fa9e4066Sahrens * still in the cache. Keep it in the MFU state. 4140fa9e4066Sahrens * 414113506d1eSmaybee * NOTE: an add_reference() that occurred when we did 414213506d1eSmaybee * the arc_read() will have kicked this off the list. 414313506d1eSmaybee * If it was a prefetch, we will explicitly move it to 414413506d1eSmaybee * the head of the list now. 4145fa9e4066Sahrens */ 414689c86e32SChris Williamson if ((HDR_PREFETCH(hdr)) != 0) { 414789c86e32SChris Williamson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4148244781f1SPrakash Surya /* link protected by hash_lock */ 4149244781f1SPrakash Surya ASSERT(multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 415013506d1eSmaybee } 415144cb6abcSbmc ARCSTAT_BUMP(arcstat_mfu_hits); 415289c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 415389c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { 415444cb6abcSbmc arc_state_t *new_state = arc_mfu; 4155fa9e4066Sahrens /* 4156fa9e4066Sahrens * This buffer has been accessed more than once but has 4157fa9e4066Sahrens * been evicted from the cache. Move it back to the 4158fa9e4066Sahrens * MFU state. 4159fa9e4066Sahrens */ 4160fa9e4066Sahrens 416189c86e32SChris Williamson if (HDR_PREFETCH(hdr)) { 416213506d1eSmaybee /* 416313506d1eSmaybee * This is a prefetch access... 416413506d1eSmaybee * move this block back to the MRU state. 416513506d1eSmaybee */ 416689c86e32SChris Williamson ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 416744cb6abcSbmc new_state = arc_mru; 416813506d1eSmaybee } 416913506d1eSmaybee 417089c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 41717adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 41727adb730bSGeorge Wilson arc_change_state(new_state, hdr, hash_lock); 4173fa9e4066Sahrens 417444cb6abcSbmc ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 417589c86e32SChris Williamson } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { 4176fa94a07fSbrendan /* 4177fa94a07fSbrendan * This buffer is on the 2nd Level ARC. 4178fa94a07fSbrendan */ 4179fa94a07fSbrendan 418089c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 41817adb730bSGeorge Wilson DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 41827adb730bSGeorge Wilson arc_change_state(arc_mfu, hdr, hash_lock); 4183fa9e4066Sahrens } else { 4184fa9e4066Sahrens ASSERT(!"invalid arc state"); 4185fa9e4066Sahrens } 4186fa9e4066Sahrens } 4187fa9e4066Sahrens 4188fa9e4066Sahrens /* a generic arc_done_func_t which you can use */ 4189fa9e4066Sahrens /* ARGSUSED */ 4190fa9e4066Sahrens void 4191fa9e4066Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 4192fa9e4066Sahrens { 41933f9d6ad7SLin Ling if (zio == NULL || zio->io_error == 0) 4194*dcbf3bd6SGeorge Wilson bcopy(buf->b_data, arg, HDR_GET_LSIZE(buf->b_hdr)); 4195*dcbf3bd6SGeorge Wilson arc_buf_destroy(buf, arg); 4196fa9e4066Sahrens } 4197fa9e4066Sahrens 41980e8c6158Smaybee /* a generic arc_done_func_t */ 4199fa9e4066Sahrens void 4200fa9e4066Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 4201fa9e4066Sahrens { 4202fa9e4066Sahrens arc_buf_t **bufp = arg; 4203fa9e4066Sahrens if (zio && zio->io_error) { 4204*dcbf3bd6SGeorge Wilson arc_buf_destroy(buf, arg); 4205fa9e4066Sahrens *bufp = NULL; 4206fa9e4066Sahrens } else { 4207fa9e4066Sahrens *bufp = buf; 42083f9d6ad7SLin Ling ASSERT(buf->b_data); 4209fa9e4066Sahrens } 4210fa9e4066Sahrens } 4211fa9e4066Sahrens 4212*dcbf3bd6SGeorge Wilson static void 4213*dcbf3bd6SGeorge Wilson arc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp) 4214*dcbf3bd6SGeorge Wilson { 4215*dcbf3bd6SGeorge Wilson if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { 4216*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0); 4217*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 4218*dcbf3bd6SGeorge Wilson } else { 4219*dcbf3bd6SGeorge Wilson if (HDR_COMPRESSION_ENABLED(hdr)) { 4220*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), ==, 4221*dcbf3bd6SGeorge Wilson BP_GET_COMPRESS(bp)); 4222*dcbf3bd6SGeorge Wilson } 4223*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); 4224*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp)); 4225*dcbf3bd6SGeorge Wilson } 4226*dcbf3bd6SGeorge Wilson } 4227*dcbf3bd6SGeorge Wilson 4228fa9e4066Sahrens static void 4229fa9e4066Sahrens arc_read_done(zio_t *zio) 4230fa9e4066Sahrens { 4231*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = zio->io_private; 4232*dcbf3bd6SGeorge Wilson arc_buf_t *abuf = NULL; /* buffer we're assigning to callback */ 42335d7b4d43SMatthew Ahrens kmutex_t *hash_lock = NULL; 4234fa9e4066Sahrens arc_callback_t *callback_list, *acb; 4235*dcbf3bd6SGeorge Wilson int freeable = B_FALSE; 4236fa9e4066Sahrens 4237bbf4a8dfSmaybee /* 4238bbf4a8dfSmaybee * The hdr was inserted into hash-table and removed from lists 4239bbf4a8dfSmaybee * prior to starting I/O. We should find this header, since 4240bbf4a8dfSmaybee * it's in the hash table, and it should be legit since it's 4241bbf4a8dfSmaybee * not possible to evict it during the I/O. The only possible 4242bbf4a8dfSmaybee * reason for it not to be found is if we were freed during the 4243bbf4a8dfSmaybee * read. 4244bbf4a8dfSmaybee */ 42455d7b4d43SMatthew Ahrens if (HDR_IN_HASH_TABLE(hdr)) { 42465d7b4d43SMatthew Ahrens ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 42475d7b4d43SMatthew Ahrens ASSERT3U(hdr->b_dva.dva_word[0], ==, 42485d7b4d43SMatthew Ahrens BP_IDENTITY(zio->io_bp)->dva_word[0]); 42495d7b4d43SMatthew Ahrens ASSERT3U(hdr->b_dva.dva_word[1], ==, 42505d7b4d43SMatthew Ahrens BP_IDENTITY(zio->io_bp)->dva_word[1]); 42515d7b4d43SMatthew Ahrens 42525d7b4d43SMatthew Ahrens arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 42535d7b4d43SMatthew Ahrens &hash_lock); 42545d7b4d43SMatthew Ahrens 4255*dcbf3bd6SGeorge Wilson ASSERT((found == hdr && 42565d7b4d43SMatthew Ahrens DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 42575d7b4d43SMatthew Ahrens (found == hdr && HDR_L2_READING(hdr))); 4258*dcbf3bd6SGeorge Wilson ASSERT3P(hash_lock, !=, NULL); 4259*dcbf3bd6SGeorge Wilson } 4260*dcbf3bd6SGeorge Wilson 4261*dcbf3bd6SGeorge Wilson if (zio->io_error == 0) { 4262*dcbf3bd6SGeorge Wilson /* byteswap if necessary */ 4263*dcbf3bd6SGeorge Wilson if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 4264*dcbf3bd6SGeorge Wilson if (BP_GET_LEVEL(zio->io_bp) > 0) { 4265*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; 4266*dcbf3bd6SGeorge Wilson } else { 4267*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = 4268*dcbf3bd6SGeorge Wilson DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 4269*dcbf3bd6SGeorge Wilson } 4270*dcbf3bd6SGeorge Wilson } else { 4271*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 4272*dcbf3bd6SGeorge Wilson } 42735d7b4d43SMatthew Ahrens } 4274fa94a07fSbrendan 4275*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED); 427689c86e32SChris Williamson if (l2arc_noprefetch && HDR_PREFETCH(hdr)) 4277*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE); 4278fa9e4066Sahrens 427989c86e32SChris Williamson callback_list = hdr->b_l1hdr.b_acb; 4280*dcbf3bd6SGeorge Wilson ASSERT3P(callback_list, !=, NULL); 42816b4acc8bSahrens 428289c86e32SChris Williamson if (hash_lock && zio->io_error == 0 && 428389c86e32SChris Williamson hdr->b_l1hdr.b_state == arc_anon) { 4284b24ab676SJeff Bonwick /* 4285b24ab676SJeff Bonwick * Only call arc_access on anonymous buffers. This is because 4286b24ab676SJeff Bonwick * if we've issued an I/O for an evicted buffer, we've already 4287b24ab676SJeff Bonwick * called arc_access (to prevent any simultaneous readers from 4288b24ab676SJeff Bonwick * getting confused). 4289b24ab676SJeff Bonwick */ 4290b24ab676SJeff Bonwick arc_access(hdr, hash_lock); 4291b24ab676SJeff Bonwick } 4292b24ab676SJeff Bonwick 4293fa9e4066Sahrens /* create copies of the data buffer for the callers */ 4294fa9e4066Sahrens for (acb = callback_list; acb; acb = acb->acb_next) { 4295*dcbf3bd6SGeorge Wilson if (acb->acb_done != NULL) { 4296*dcbf3bd6SGeorge Wilson /* 4297*dcbf3bd6SGeorge Wilson * If we're here, then this must be a demand read 4298*dcbf3bd6SGeorge Wilson * since prefetch requests don't have callbacks. 4299*dcbf3bd6SGeorge Wilson * If a read request has a callback (i.e. acb_done is 4300*dcbf3bd6SGeorge Wilson * not NULL), then we decompress the data for the 4301*dcbf3bd6SGeorge Wilson * first request and clone the rest. This avoids 4302*dcbf3bd6SGeorge Wilson * having to waste cpu resources decompressing data 4303*dcbf3bd6SGeorge Wilson * that nobody is explicitly waiting to read. 4304*dcbf3bd6SGeorge Wilson */ 43059253d63dSGeorge Wilson if (abuf == NULL) { 4306*dcbf3bd6SGeorge Wilson acb->acb_buf = arc_buf_alloc_impl(hdr, 4307*dcbf3bd6SGeorge Wilson acb->acb_private); 4308*dcbf3bd6SGeorge Wilson if (zio->io_error == 0) { 4309*dcbf3bd6SGeorge Wilson zio->io_error = 4310*dcbf3bd6SGeorge Wilson arc_decompress(acb->acb_buf); 4311*dcbf3bd6SGeorge Wilson } 4312*dcbf3bd6SGeorge Wilson abuf = acb->acb_buf; 4313*dcbf3bd6SGeorge Wilson } else { 4314*dcbf3bd6SGeorge Wilson add_reference(hdr, acb->acb_private); 4315*dcbf3bd6SGeorge Wilson acb->acb_buf = arc_buf_clone(abuf); 43169253d63dSGeorge Wilson } 4317fa9e4066Sahrens } 4318fa9e4066Sahrens } 431989c86e32SChris Williamson hdr->b_l1hdr.b_acb = NULL; 4320*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 4321*dcbf3bd6SGeorge Wilson if (abuf == NULL) { 4322*dcbf3bd6SGeorge Wilson /* 4323*dcbf3bd6SGeorge Wilson * This buffer didn't have a callback so it must 4324*dcbf3bd6SGeorge Wilson * be a prefetch. 4325*dcbf3bd6SGeorge Wilson */ 4326*dcbf3bd6SGeorge Wilson ASSERT(HDR_PREFETCH(hdr)); 4327*dcbf3bd6SGeorge Wilson ASSERT0(hdr->b_l1hdr.b_bufcnt); 4328*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, !=, NULL); 4329b24ab676SJeff Bonwick } 4330fa9e4066Sahrens 433189c86e32SChris Williamson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || 433289c86e32SChris Williamson callback_list != NULL); 4333fa9e4066Sahrens 4334*dcbf3bd6SGeorge Wilson if (zio->io_error == 0) { 4335*dcbf3bd6SGeorge Wilson arc_hdr_verify(hdr, zio->io_bp); 4336*dcbf3bd6SGeorge Wilson } else { 4337*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); 433889c86e32SChris Williamson if (hdr->b_l1hdr.b_state != arc_anon) 433944cb6abcSbmc arc_change_state(arc_anon, hdr, hash_lock); 4340ea8dc4b6Seschrock if (HDR_IN_HASH_TABLE(hdr)) 4341ea8dc4b6Seschrock buf_hash_remove(hdr); 434289c86e32SChris Williamson freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4343fa9e4066Sahrens } 4344fa9e4066Sahrens 4345ea8dc4b6Seschrock /* 434613506d1eSmaybee * Broadcast before we drop the hash_lock to avoid the possibility 434713506d1eSmaybee * that the hdr (and hence the cv) might be freed before we get to 434813506d1eSmaybee * the cv_broadcast(). 4349ea8dc4b6Seschrock */ 435089c86e32SChris Williamson cv_broadcast(&hdr->b_l1hdr.b_cv); 4351ea8dc4b6Seschrock 435289c86e32SChris Williamson if (hash_lock != NULL) { 435344eda4d7Smaybee mutex_exit(hash_lock); 4354fa9e4066Sahrens } else { 4355fa9e4066Sahrens /* 4356fa9e4066Sahrens * This block was freed while we waited for the read to 4357fa9e4066Sahrens * complete. It has been removed from the hash table and 4358fa9e4066Sahrens * moved to the anonymous state (so that it won't show up 4359fa9e4066Sahrens * in the cache). 4360fa9e4066Sahrens */ 436189c86e32SChris Williamson ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 436289c86e32SChris Williamson freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4363fa9e4066Sahrens } 4364fa9e4066Sahrens 4365fa9e4066Sahrens /* execute each callback and free its structure */ 4366fa9e4066Sahrens while ((acb = callback_list) != NULL) { 4367fa9e4066Sahrens if (acb->acb_done) 4368fa9e4066Sahrens acb->acb_done(zio, acb->acb_buf, acb->acb_private); 4369fa9e4066Sahrens 4370fa9e4066Sahrens if (acb->acb_zio_dummy != NULL) { 4371fa9e4066Sahrens acb->acb_zio_dummy->io_error = zio->io_error; 4372fa9e4066Sahrens zio_nowait(acb->acb_zio_dummy); 4373fa9e4066Sahrens } 4374fa9e4066Sahrens 4375fa9e4066Sahrens callback_list = acb->acb_next; 4376fa9e4066Sahrens kmem_free(acb, sizeof (arc_callback_t)); 4377fa9e4066Sahrens } 4378fa9e4066Sahrens 4379fa9e4066Sahrens if (freeable) 4380ea8dc4b6Seschrock arc_hdr_destroy(hdr); 4381fa9e4066Sahrens } 4382fa9e4066Sahrens 4383fa9e4066Sahrens /* 4384fc98fea5SBart Coddens * "Read" the block at the specified DVA (in bp) via the 4385fa9e4066Sahrens * cache. If the block is found in the cache, invoke the provided 4386fa9e4066Sahrens * callback immediately and return. Note that the `zio' parameter 4387fa9e4066Sahrens * in the callback will be NULL in this case, since no IO was 4388fa9e4066Sahrens * required. If the block is not in the cache pass the read request 4389fa9e4066Sahrens * on to the spa with a substitute callback function, so that the 4390fa9e4066Sahrens * requested block will be added to the cache. 4391fa9e4066Sahrens * 4392fa9e4066Sahrens * If a read request arrives for a block that has a read in-progress, 4393fa9e4066Sahrens * either wait for the in-progress read to complete (and return the 4394fa9e4066Sahrens * results); or, if this is a read with a "done" func, add a record 4395fa9e4066Sahrens * to the read to invoke the "done" func when the read completes, 4396fa9e4066Sahrens * and return; or just return. 4397fa9e4066Sahrens * 4398fa9e4066Sahrens * arc_read_done() will invoke all the requested "done" functions 4399fa9e4066Sahrens * for readers of this block. 4400fa9e4066Sahrens */ 4401fa9e4066Sahrens int 44021b912ec7SGeorge Wilson arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 44037adb730bSGeorge Wilson void *private, zio_priority_t priority, int zio_flags, 44047adb730bSGeorge Wilson arc_flags_t *arc_flags, const zbookmark_phys_t *zb) 4405fa9e4066Sahrens { 44065d7b4d43SMatthew Ahrens arc_buf_hdr_t *hdr = NULL; 44075d7b4d43SMatthew Ahrens kmutex_t *hash_lock = NULL; 4408fa94a07fSbrendan zio_t *rzio; 4409e9103aaeSGarrett D'Amore uint64_t guid = spa_load_guid(spa); 4410fa9e4066Sahrens 44115d7b4d43SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp) || 44125d7b4d43SMatthew Ahrens BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 44135d7b4d43SMatthew Ahrens 4414fa9e4066Sahrens top: 44155d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) { 44165d7b4d43SMatthew Ahrens /* 44175d7b4d43SMatthew Ahrens * Embedded BP's have no DVA and require no I/O to "read". 44185d7b4d43SMatthew Ahrens * Create an anonymous arc buf to back it. 44195d7b4d43SMatthew Ahrens */ 44205d7b4d43SMatthew Ahrens hdr = buf_hash_find(guid, bp, &hash_lock); 44215d7b4d43SMatthew Ahrens } 44225d7b4d43SMatthew Ahrens 4423*dcbf3bd6SGeorge Wilson if (hdr != NULL && HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_pdata != NULL) { 4424*dcbf3bd6SGeorge Wilson arc_buf_t *buf = NULL; 44257adb730bSGeorge Wilson *arc_flags |= ARC_FLAG_CACHED; 442613506d1eSmaybee 4427fa9e4066Sahrens if (HDR_IO_IN_PROGRESS(hdr)) { 442813506d1eSmaybee 4429cf6106c8SMatthew Ahrens if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) && 4430cf6106c8SMatthew Ahrens priority == ZIO_PRIORITY_SYNC_READ) { 4431cf6106c8SMatthew Ahrens /* 4432cf6106c8SMatthew Ahrens * This sync read must wait for an 4433cf6106c8SMatthew Ahrens * in-progress async read (e.g. a predictive 4434cf6106c8SMatthew Ahrens * prefetch). Async reads are queued 4435cf6106c8SMatthew Ahrens * separately at the vdev_queue layer, so 4436cf6106c8SMatthew Ahrens * this is a form of priority inversion. 4437cf6106c8SMatthew Ahrens * Ideally, we would "inherit" the demand 4438cf6106c8SMatthew Ahrens * i/o's priority by moving the i/o from 4439cf6106c8SMatthew Ahrens * the async queue to the synchronous queue, 4440cf6106c8SMatthew Ahrens * but there is currently no mechanism to do 4441cf6106c8SMatthew Ahrens * so. Track this so that we can evaluate 4442cf6106c8SMatthew Ahrens * the magnitude of this potential performance 4443cf6106c8SMatthew Ahrens * problem. 4444cf6106c8SMatthew Ahrens * 4445cf6106c8SMatthew Ahrens * Note that if the prefetch i/o is already 4446cf6106c8SMatthew Ahrens * active (has been issued to the device), 4447cf6106c8SMatthew Ahrens * the prefetch improved performance, because 4448cf6106c8SMatthew Ahrens * we issued it sooner than we would have 4449cf6106c8SMatthew Ahrens * without the prefetch. 4450cf6106c8SMatthew Ahrens */ 4451cf6106c8SMatthew Ahrens DTRACE_PROBE1(arc__sync__wait__for__async, 4452cf6106c8SMatthew Ahrens arc_buf_hdr_t *, hdr); 4453cf6106c8SMatthew Ahrens ARCSTAT_BUMP(arcstat_sync_wait_for_async); 4454cf6106c8SMatthew Ahrens } 4455cf6106c8SMatthew Ahrens if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { 4456*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, 4457*dcbf3bd6SGeorge Wilson ARC_FLAG_PREDICTIVE_PREFETCH); 4458cf6106c8SMatthew Ahrens } 4459cf6106c8SMatthew Ahrens 44607adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_WAIT) { 446189c86e32SChris Williamson cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); 446213506d1eSmaybee mutex_exit(hash_lock); 446313506d1eSmaybee goto top; 446413506d1eSmaybee } 44657adb730bSGeorge Wilson ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 446613506d1eSmaybee 446713506d1eSmaybee if (done) { 4468cf6106c8SMatthew Ahrens arc_callback_t *acb = NULL; 4469fa9e4066Sahrens 4470fa9e4066Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), 4471fa9e4066Sahrens KM_SLEEP); 4472fa9e4066Sahrens acb->acb_done = done; 4473fa9e4066Sahrens acb->acb_private = private; 4474fa9e4066Sahrens if (pio != NULL) 4475fa9e4066Sahrens acb->acb_zio_dummy = zio_null(pio, 4476a3f829aeSBill Moore spa, NULL, NULL, NULL, zio_flags); 4477fa9e4066Sahrens 4478*dcbf3bd6SGeorge Wilson ASSERT3P(acb->acb_done, !=, NULL); 447989c86e32SChris Williamson acb->acb_next = hdr->b_l1hdr.b_acb; 448089c86e32SChris Williamson hdr->b_l1hdr.b_acb = acb; 4481fa9e4066Sahrens mutex_exit(hash_lock); 4482fa9e4066Sahrens return (0); 4483fa9e4066Sahrens } 4484fa9e4066Sahrens mutex_exit(hash_lock); 4485fa9e4066Sahrens return (0); 4486fa9e4066Sahrens } 4487fa9e4066Sahrens 448889c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_state == arc_mru || 448989c86e32SChris Williamson hdr->b_l1hdr.b_state == arc_mfu); 4490fa9e4066Sahrens 4491ea8dc4b6Seschrock if (done) { 4492cf6106c8SMatthew Ahrens if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { 4493cf6106c8SMatthew Ahrens /* 4494cf6106c8SMatthew Ahrens * This is a demand read which does not have to 4495cf6106c8SMatthew Ahrens * wait for i/o because we did a predictive 4496cf6106c8SMatthew Ahrens * prefetch i/o for it, which has completed. 4497cf6106c8SMatthew Ahrens */ 4498cf6106c8SMatthew Ahrens DTRACE_PROBE1( 4499cf6106c8SMatthew Ahrens arc__demand__hit__predictive__prefetch, 4500cf6106c8SMatthew Ahrens arc_buf_hdr_t *, hdr); 4501cf6106c8SMatthew Ahrens ARCSTAT_BUMP( 4502cf6106c8SMatthew Ahrens arcstat_demand_hit_predictive_prefetch); 4503*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, 4504*dcbf3bd6SGeorge Wilson ARC_FLAG_PREDICTIVE_PREFETCH); 4505cf6106c8SMatthew Ahrens } 4506*dcbf3bd6SGeorge Wilson ASSERT(!BP_IS_EMBEDDED(bp) || !BP_IS_HOLE(bp)); 4507*dcbf3bd6SGeorge Wilson 4508ea8dc4b6Seschrock /* 4509ea8dc4b6Seschrock * If this block is already in use, create a new 4510ea8dc4b6Seschrock * copy of the data so that we will be guaranteed 4511ea8dc4b6Seschrock * that arc_release() will always succeed. 4512ea8dc4b6Seschrock */ 451389c86e32SChris Williamson buf = hdr->b_l1hdr.b_buf; 4514*dcbf3bd6SGeorge Wilson if (buf == NULL) { 4515*dcbf3bd6SGeorge Wilson ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 4516*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 4517*dcbf3bd6SGeorge Wilson buf = arc_buf_alloc_impl(hdr, private); 4518*dcbf3bd6SGeorge Wilson VERIFY0(arc_decompress(buf)); 451944eda4d7Smaybee } else { 4520*dcbf3bd6SGeorge Wilson add_reference(hdr, private); 452144eda4d7Smaybee buf = arc_buf_clone(buf); 4522ea8dc4b6Seschrock } 4523*dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, !=, NULL); 4524b24ab676SJeff Bonwick 45257adb730bSGeorge Wilson } else if (*arc_flags & ARC_FLAG_PREFETCH && 452689c86e32SChris Williamson refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 4527*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); 4528fa9e4066Sahrens } 4529fa9e4066Sahrens DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 453044eda4d7Smaybee arc_access(hdr, hash_lock); 45317adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_L2CACHE) 4532*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 453344eda4d7Smaybee mutex_exit(hash_lock); 453444cb6abcSbmc ARCSTAT_BUMP(arcstat_hits); 453589c86e32SChris Williamson ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 453689c86e32SChris Williamson demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 453744cb6abcSbmc data, metadata, hits); 453844cb6abcSbmc 4539fa9e4066Sahrens if (done) 4540fa9e4066Sahrens done(NULL, buf, private); 4541fa9e4066Sahrens } else { 4542*dcbf3bd6SGeorge Wilson uint64_t lsize = BP_GET_LSIZE(bp); 4543*dcbf3bd6SGeorge Wilson uint64_t psize = BP_GET_PSIZE(bp); 45445d7b4d43SMatthew Ahrens arc_callback_t *acb; 45453a737e0dSbrendan vdev_t *vd = NULL; 4546d5285caeSGeorge Wilson uint64_t addr = 0; 45475a98e54bSBrendan Gregg - Sun Microsystems boolean_t devw = B_FALSE; 4548*dcbf3bd6SGeorge Wilson uint64_t size; 4549fa9e4066Sahrens 4550fa9e4066Sahrens if (hdr == NULL) { 4551fa9e4066Sahrens /* this block is not in the cache */ 45525d7b4d43SMatthew Ahrens arc_buf_hdr_t *exists = NULL; 4553ad23a2dbSjohansen arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 4554*dcbf3bd6SGeorge Wilson hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, 4555*dcbf3bd6SGeorge Wilson BP_GET_COMPRESS(bp), type); 4556*dcbf3bd6SGeorge Wilson 45575d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) { 45585d7b4d43SMatthew Ahrens hdr->b_dva = *BP_IDENTITY(bp); 45595d7b4d43SMatthew Ahrens hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 45605d7b4d43SMatthew Ahrens exists = buf_hash_insert(hdr, &hash_lock); 45615d7b4d43SMatthew Ahrens } 45625d7b4d43SMatthew Ahrens if (exists != NULL) { 4563fa9e4066Sahrens /* somebody beat us to the hash insert */ 4564fa9e4066Sahrens mutex_exit(hash_lock); 45653f9d6ad7SLin Ling buf_discard_identity(hdr); 4566*dcbf3bd6SGeorge Wilson arc_hdr_destroy(hdr); 4567fa9e4066Sahrens goto top; /* restart the IO request */ 4568fa9e4066Sahrens } 4569fa9e4066Sahrens } else { 457089c86e32SChris Williamson /* 457189c86e32SChris Williamson * This block is in the ghost cache. If it was L2-only 457289c86e32SChris Williamson * (and thus didn't have an L1 hdr), we realloc the 457389c86e32SChris Williamson * header to add an L1 hdr. 457489c86e32SChris Williamson */ 457589c86e32SChris Williamson if (!HDR_HAS_L1HDR(hdr)) { 457689c86e32SChris Williamson hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, 457789c86e32SChris Williamson hdr_full_cache); 457889c86e32SChris Williamson } 4579*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 458089c86e32SChris Williamson ASSERT(GHOST_STATE(hdr->b_l1hdr.b_state)); 4581ea8dc4b6Seschrock ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 458289c86e32SChris Williamson ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4583244781f1SPrakash Surya ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 458413506d1eSmaybee 4585cf6106c8SMatthew Ahrens /* 4586*dcbf3bd6SGeorge Wilson * This is a delicate dance that we play here. 4587*dcbf3bd6SGeorge Wilson * This hdr is in the ghost list so we access it 4588*dcbf3bd6SGeorge Wilson * to move it out of the ghost list before we 4589*dcbf3bd6SGeorge Wilson * initiate the read. If it's a prefetch then 4590*dcbf3bd6SGeorge Wilson * it won't have a callback so we'll remove the 4591*dcbf3bd6SGeorge Wilson * reference that arc_buf_alloc_impl() created. We 4592*dcbf3bd6SGeorge Wilson * do this after we've called arc_access() to 4593*dcbf3bd6SGeorge Wilson * avoid hitting an assert in remove_reference(). 4594cf6106c8SMatthew Ahrens */ 45957e453561SWilliam Gorrell arc_access(hdr, hash_lock); 4596*dcbf3bd6SGeorge Wilson arc_hdr_alloc_pdata(hdr); 4597*dcbf3bd6SGeorge Wilson } 4598*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, !=, NULL); 4599*dcbf3bd6SGeorge Wilson size = arc_hdr_size(hdr); 4600*dcbf3bd6SGeorge Wilson 4601*dcbf3bd6SGeorge Wilson /* 4602*dcbf3bd6SGeorge Wilson * If compression is enabled on the hdr, then will do 4603*dcbf3bd6SGeorge Wilson * RAW I/O and will store the compressed data in the hdr's 4604*dcbf3bd6SGeorge Wilson * data block. Otherwise, the hdr's data block will contain 4605*dcbf3bd6SGeorge Wilson * the uncompressed data. 4606*dcbf3bd6SGeorge Wilson */ 4607*dcbf3bd6SGeorge Wilson if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) { 4608*dcbf3bd6SGeorge Wilson zio_flags |= ZIO_FLAG_RAW; 4609fa9e4066Sahrens } 4610fa9e4066Sahrens 4611*dcbf3bd6SGeorge Wilson if (*arc_flags & ARC_FLAG_PREFETCH) 4612*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); 4613*dcbf3bd6SGeorge Wilson if (*arc_flags & ARC_FLAG_L2CACHE) 4614*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 4615*dcbf3bd6SGeorge Wilson if (BP_GET_LEVEL(bp) > 0) 4616*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT); 4617cf6106c8SMatthew Ahrens if (*arc_flags & ARC_FLAG_PREDICTIVE_PREFETCH) 4618*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PREDICTIVE_PREFETCH); 461989c86e32SChris Williamson ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); 46205614b00aSWilliam Gorrell 4621fa9e4066Sahrens acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 4622fa9e4066Sahrens acb->acb_done = done; 4623fa9e4066Sahrens acb->acb_private = private; 4624fa9e4066Sahrens 4625*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 462689c86e32SChris Williamson hdr->b_l1hdr.b_acb = acb; 4627*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 4628fa9e4066Sahrens 462989c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr) && 463089c86e32SChris Williamson (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { 463189c86e32SChris Williamson devw = hdr->b_l2hdr.b_dev->l2ad_writing; 463289c86e32SChris Williamson addr = hdr->b_l2hdr.b_daddr; 4633e14bb325SJeff Bonwick /* 4634e14bb325SJeff Bonwick * Lock out device removal. 4635e14bb325SJeff Bonwick */ 4636e14bb325SJeff Bonwick if (vdev_is_dead(vd) || 4637e14bb325SJeff Bonwick !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 4638e14bb325SJeff Bonwick vd = NULL; 46393a737e0dSbrendan } 46403a737e0dSbrendan 4641*dcbf3bd6SGeorge Wilson if (priority == ZIO_PRIORITY_ASYNC_READ) 4642*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); 4643*dcbf3bd6SGeorge Wilson else 4644*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); 4645*dcbf3bd6SGeorge Wilson 46465d7b4d43SMatthew Ahrens if (hash_lock != NULL) 46475d7b4d43SMatthew Ahrens mutex_exit(hash_lock); 46483a737e0dSbrendan 46493e30c24aSWill Andrews /* 46503e30c24aSWill Andrews * At this point, we have a level 1 cache miss. Try again in 46513e30c24aSWill Andrews * L2ARC if possible. 46523e30c24aSWill Andrews */ 4653*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize); 4654*dcbf3bd6SGeorge Wilson 46555c28183bSBrendan Gregg - Sun Microsystems DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 4656*dcbf3bd6SGeorge Wilson uint64_t, lsize, zbookmark_phys_t *, zb); 465744cb6abcSbmc ARCSTAT_BUMP(arcstat_misses); 465889c86e32SChris Williamson ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 465989c86e32SChris Williamson demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 466044cb6abcSbmc data, metadata, misses); 4661ea8dc4b6Seschrock 46625a98e54bSBrendan Gregg - Sun Microsystems if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 4663fa94a07fSbrendan /* 4664fa94a07fSbrendan * Read from the L2ARC if the following are true: 46653a737e0dSbrendan * 1. The L2ARC vdev was previously cached. 46663a737e0dSbrendan * 2. This buffer still has L2ARC metadata. 46673a737e0dSbrendan * 3. This buffer isn't currently writing to the L2ARC. 46683a737e0dSbrendan * 4. The L2ARC entry wasn't evicted, which may 46693a737e0dSbrendan * also have invalidated the vdev. 46705a98e54bSBrendan Gregg - Sun Microsystems * 5. This isn't prefetch and l2arc_noprefetch is set. 4671fa94a07fSbrendan */ 467289c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr) && 46735a98e54bSBrendan Gregg - Sun Microsystems !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 46745a98e54bSBrendan Gregg - Sun Microsystems !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 4675fa94a07fSbrendan l2arc_read_callback_t *cb; 4676fa94a07fSbrendan 4677c5904d13Seschrock DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 4678c5904d13Seschrock ARCSTAT_BUMP(arcstat_l2_hits); 4679c5904d13Seschrock 4680fa94a07fSbrendan cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 4681fa94a07fSbrendan KM_SLEEP); 4682*dcbf3bd6SGeorge Wilson cb->l2rcb_hdr = hdr; 4683fa94a07fSbrendan cb->l2rcb_bp = *bp; 4684fa94a07fSbrendan cb->l2rcb_zb = *zb; 46853baa08fcSek cb->l2rcb_flags = zio_flags; 4686fa94a07fSbrendan 4687d5285caeSGeorge Wilson ASSERT(addr >= VDEV_LABEL_START_SIZE && 4688*dcbf3bd6SGeorge Wilson addr + lsize < vd->vdev_psize - 4689d5285caeSGeorge Wilson VDEV_LABEL_END_SIZE); 4690d5285caeSGeorge Wilson 4691fa94a07fSbrendan /* 4692e14bb325SJeff Bonwick * l2arc read. The SCL_L2ARC lock will be 4693e14bb325SJeff Bonwick * released by l2arc_read_done(). 4694aad02571SSaso Kiselkov * Issue a null zio if the underlying buffer 4695aad02571SSaso Kiselkov * was squashed to zero size by compression. 4696fa94a07fSbrendan */ 4697*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_COMPRESS(hdr), !=, 4698*dcbf3bd6SGeorge Wilson ZIO_COMPRESS_EMPTY); 4699*dcbf3bd6SGeorge Wilson rzio = zio_read_phys(pio, vd, addr, 4700*dcbf3bd6SGeorge Wilson size, hdr->b_l1hdr.b_pdata, 4701*dcbf3bd6SGeorge Wilson ZIO_CHECKSUM_OFF, 4702*dcbf3bd6SGeorge Wilson l2arc_read_done, cb, priority, 4703*dcbf3bd6SGeorge Wilson zio_flags | ZIO_FLAG_DONT_CACHE | 4704*dcbf3bd6SGeorge Wilson ZIO_FLAG_CANFAIL | 4705*dcbf3bd6SGeorge Wilson ZIO_FLAG_DONT_PROPAGATE | 4706*dcbf3bd6SGeorge Wilson ZIO_FLAG_DONT_RETRY, B_FALSE); 4707fa94a07fSbrendan DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 4708fa94a07fSbrendan zio_t *, rzio); 4709*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_l2_read_bytes, size); 4710fa94a07fSbrendan 47117adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_NOWAIT) { 47123a737e0dSbrendan zio_nowait(rzio); 47133a737e0dSbrendan return (0); 47143a737e0dSbrendan } 4715fa94a07fSbrendan 47167adb730bSGeorge Wilson ASSERT(*arc_flags & ARC_FLAG_WAIT); 47173a737e0dSbrendan if (zio_wait(rzio) == 0) 47183a737e0dSbrendan return (0); 47193a737e0dSbrendan 47203a737e0dSbrendan /* l2arc read error; goto zio_read() */ 4721fa94a07fSbrendan } else { 4722fa94a07fSbrendan DTRACE_PROBE1(l2arc__miss, 4723fa94a07fSbrendan arc_buf_hdr_t *, hdr); 4724fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_misses); 4725fa94a07fSbrendan if (HDR_L2_WRITING(hdr)) 4726fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_rw_clash); 4727e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, vd); 4728fa94a07fSbrendan } 47295a98e54bSBrendan Gregg - Sun Microsystems } else { 473076a25fafSBill Moore if (vd != NULL) 473176a25fafSBill Moore spa_config_exit(spa, SCL_L2ARC, vd); 47325a98e54bSBrendan Gregg - Sun Microsystems if (l2arc_ndev != 0) { 47335a98e54bSBrendan Gregg - Sun Microsystems DTRACE_PROBE1(l2arc__miss, 47345a98e54bSBrendan Gregg - Sun Microsystems arc_buf_hdr_t *, hdr); 47355a98e54bSBrendan Gregg - Sun Microsystems ARCSTAT_BUMP(arcstat_l2_misses); 47365a98e54bSBrendan Gregg - Sun Microsystems } 4737fa94a07fSbrendan } 4738c5904d13Seschrock 4739*dcbf3bd6SGeorge Wilson rzio = zio_read(pio, spa, bp, hdr->b_l1hdr.b_pdata, size, 4740*dcbf3bd6SGeorge Wilson arc_read_done, hdr, priority, zio_flags, zb); 4741fa9e4066Sahrens 47427adb730bSGeorge Wilson if (*arc_flags & ARC_FLAG_WAIT) 4743fa9e4066Sahrens return (zio_wait(rzio)); 4744fa9e4066Sahrens 47457adb730bSGeorge Wilson ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 4746fa9e4066Sahrens zio_nowait(rzio); 4747fa9e4066Sahrens } 4748fa9e4066Sahrens return (0); 4749fa9e4066Sahrens } 4750fa9e4066Sahrens 47516e6d5868SMatthew Ahrens /* 47526e6d5868SMatthew Ahrens * Notify the arc that a block was freed, and thus will never be used again. 47536e6d5868SMatthew Ahrens */ 47546e6d5868SMatthew Ahrens void 47556e6d5868SMatthew Ahrens arc_freed(spa_t *spa, const blkptr_t *bp) 47566e6d5868SMatthew Ahrens { 47576e6d5868SMatthew Ahrens arc_buf_hdr_t *hdr; 47586e6d5868SMatthew Ahrens kmutex_t *hash_lock; 47596e6d5868SMatthew Ahrens uint64_t guid = spa_load_guid(spa); 47606e6d5868SMatthew Ahrens 47615d7b4d43SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp)); 47625d7b4d43SMatthew Ahrens 47635d7b4d43SMatthew Ahrens hdr = buf_hash_find(guid, bp, &hash_lock); 47646e6d5868SMatthew Ahrens if (hdr == NULL) 47656e6d5868SMatthew Ahrens return; 47666e6d5868SMatthew Ahrens 4767*dcbf3bd6SGeorge Wilson /* 4768*dcbf3bd6SGeorge Wilson * We might be trying to free a block that is still doing I/O 4769*dcbf3bd6SGeorge Wilson * (i.e. prefetch) or has a reference (i.e. a dedup-ed, 4770*dcbf3bd6SGeorge Wilson * dmu_sync-ed block). If this block is being prefetched, then it 4771*dcbf3bd6SGeorge Wilson * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr 4772*dcbf3bd6SGeorge Wilson * until the I/O completes. A block may also have a reference if it is 4773*dcbf3bd6SGeorge Wilson * part of a dedup-ed, dmu_synced write. The dmu_sync() function would 4774*dcbf3bd6SGeorge Wilson * have written the new block to its final resting place on disk but 4775*dcbf3bd6SGeorge Wilson * without the dedup flag set. This would have left the hdr in the MRU 4776*dcbf3bd6SGeorge Wilson * state and discoverable. When the txg finally syncs it detects that 4777*dcbf3bd6SGeorge Wilson * the block was overridden in open context and issues an override I/O. 4778*dcbf3bd6SGeorge Wilson * Since this is a dedup block, the override I/O will determine if the 4779*dcbf3bd6SGeorge Wilson * block is already in the DDT. If so, then it will replace the io_bp 4780*dcbf3bd6SGeorge Wilson * with the bp from the DDT and allow the I/O to finish. When the I/O 4781*dcbf3bd6SGeorge Wilson * reaches the done callback, dbuf_write_override_done, it will 4782*dcbf3bd6SGeorge Wilson * check to see if the io_bp and io_bp_override are identical. 4783*dcbf3bd6SGeorge Wilson * If they are not, then it indicates that the bp was replaced with 4784*dcbf3bd6SGeorge Wilson * the bp in the DDT and the override bp is freed. This allows 4785*dcbf3bd6SGeorge Wilson * us to arrive here with a reference on a block that is being 4786*dcbf3bd6SGeorge Wilson * freed. So if we have an I/O in progress, or a reference to 4787*dcbf3bd6SGeorge Wilson * this hdr, then we don't destroy the hdr. 4788*dcbf3bd6SGeorge Wilson */ 4789*dcbf3bd6SGeorge Wilson if (!HDR_HAS_L1HDR(hdr) || (!HDR_IO_IN_PROGRESS(hdr) && 4790*dcbf3bd6SGeorge Wilson refcount_is_zero(&hdr->b_l1hdr.b_refcnt))) { 4791*dcbf3bd6SGeorge Wilson arc_change_state(arc_anon, hdr, hash_lock); 4792*dcbf3bd6SGeorge Wilson arc_hdr_destroy(hdr); 47936e6d5868SMatthew Ahrens mutex_exit(hash_lock); 4794bbfa8ea8SMatthew Ahrens } else { 4795*dcbf3bd6SGeorge Wilson mutex_exit(hash_lock); 4796ea8dc4b6Seschrock } 4797dd6ef538Smaybee 4798ea8dc4b6Seschrock } 4799ea8dc4b6Seschrock 4800fa9e4066Sahrens /* 48013e30c24aSWill Andrews * Release this buffer from the cache, making it an anonymous buffer. This 48023e30c24aSWill Andrews * must be done after a read and prior to modifying the buffer contents. 4803fa9e4066Sahrens * If the buffer has more than one reference, we must make 4804088f3894Sahrens * a new hdr for the buffer. 4805fa9e4066Sahrens */ 4806fa9e4066Sahrens void 4807fa9e4066Sahrens arc_release(arc_buf_t *buf, void *tag) 4808fa9e4066Sahrens { 480989c86e32SChris Williamson arc_buf_hdr_t *hdr = buf->b_hdr; 4810fa9e4066Sahrens 48113f9d6ad7SLin Ling /* 48123f9d6ad7SLin Ling * It would be nice to assert that if it's DMU metadata (level > 48133f9d6ad7SLin Ling * 0 || it's the dnode file), then it must be syncing context. 48143f9d6ad7SLin Ling * But we don't know that information at this level. 48153f9d6ad7SLin Ling */ 48163f9d6ad7SLin Ling 48173f9d6ad7SLin Ling mutex_enter(&buf->b_evict_lock); 48186f83844dSMark Maybee 4819244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 4820244781f1SPrakash Surya 482189c86e32SChris Williamson /* 482289c86e32SChris Williamson * We don't grab the hash lock prior to this check, because if 482389c86e32SChris Williamson * the buffer's header is in the arc_anon state, it won't be 482489c86e32SChris Williamson * linked into the hash table. 482589c86e32SChris Williamson */ 482689c86e32SChris Williamson if (hdr->b_l1hdr.b_state == arc_anon) { 482789c86e32SChris Williamson mutex_exit(&buf->b_evict_lock); 482889c86e32SChris Williamson ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 482989c86e32SChris Williamson ASSERT(!HDR_IN_HASH_TABLE(hdr)); 483089c86e32SChris Williamson ASSERT(!HDR_HAS_L2HDR(hdr)); 4831*dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 4832fa9e4066Sahrens 4833*dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 483489c86e32SChris Williamson ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); 483589c86e32SChris Williamson ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 483689c86e32SChris Williamson 483789c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = 0; 4838*dcbf3bd6SGeorge Wilson 4839*dcbf3bd6SGeorge Wilson /* 4840*dcbf3bd6SGeorge Wilson * If the buf is being overridden then it may already 4841*dcbf3bd6SGeorge Wilson * have a hdr that is not empty. 4842*dcbf3bd6SGeorge Wilson */ 4843*dcbf3bd6SGeorge Wilson buf_discard_identity(hdr); 484489c86e32SChris Williamson arc_buf_thaw(buf); 484589c86e32SChris Williamson 484689c86e32SChris Williamson return; 4847fa9e4066Sahrens } 4848fa9e4066Sahrens 484989c86e32SChris Williamson kmutex_t *hash_lock = HDR_LOCK(hdr); 485089c86e32SChris Williamson mutex_enter(hash_lock); 485189c86e32SChris Williamson 485289c86e32SChris Williamson /* 485389c86e32SChris Williamson * This assignment is only valid as long as the hash_lock is 485489c86e32SChris Williamson * held, we must be careful not to reference state or the 485589c86e32SChris Williamson * b_state field after dropping the lock. 485689c86e32SChris Williamson */ 485789c86e32SChris Williamson arc_state_t *state = hdr->b_l1hdr.b_state; 485889c86e32SChris Williamson ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 485989c86e32SChris Williamson ASSERT3P(state, !=, arc_anon); 486089c86e32SChris Williamson 486189c86e32SChris Williamson /* this buffer is not on any list */ 486289c86e32SChris Williamson ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) > 0); 486389c86e32SChris Williamson 486489c86e32SChris Williamson if (HDR_HAS_L2HDR(hdr)) { 486589c86e32SChris Williamson mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); 4866244781f1SPrakash Surya 4867244781f1SPrakash Surya /* 4868a52fc310SPrakash Surya * We have to recheck this conditional again now that 4869a52fc310SPrakash Surya * we're holding the l2ad_mtx to prevent a race with 4870a52fc310SPrakash Surya * another thread which might be concurrently calling 4871a52fc310SPrakash Surya * l2arc_evict(). In that case, l2arc_evict() might have 4872a52fc310SPrakash Surya * destroyed the header's L2 portion as we were waiting 4873a52fc310SPrakash Surya * to acquire the l2ad_mtx. 4874244781f1SPrakash Surya */ 4875a52fc310SPrakash Surya if (HDR_HAS_L2HDR(hdr)) 4876a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(hdr); 4877244781f1SPrakash Surya 487889c86e32SChris Williamson mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); 48796f83844dSMark Maybee } 48806f83844dSMark Maybee 4881ea8dc4b6Seschrock /* 4882ea8dc4b6Seschrock * Do we have more than one buf? 4883ea8dc4b6Seschrock */ 4884*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_bufcnt > 1) { 4885fa9e4066Sahrens arc_buf_hdr_t *nhdr; 4886fa9e4066Sahrens arc_buf_t **bufp; 4887ac05c741SMark Maybee uint64_t spa = hdr->b_spa; 4888*dcbf3bd6SGeorge Wilson uint64_t psize = HDR_GET_PSIZE(hdr); 4889*dcbf3bd6SGeorge Wilson uint64_t lsize = HDR_GET_LSIZE(hdr); 4890*dcbf3bd6SGeorge Wilson enum zio_compress compress = HDR_GET_COMPRESS(hdr); 489189c86e32SChris Williamson arc_buf_contents_t type = arc_buf_type(hdr); 4892*dcbf3bd6SGeorge Wilson VERIFY3U(hdr->b_type, ==, type); 4893fa9e4066Sahrens 489489c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); 4895*dcbf3bd6SGeorge Wilson (void) remove_reference(hdr, hash_lock, tag); 4896*dcbf3bd6SGeorge Wilson 4897*dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 4898*dcbf3bd6SGeorge Wilson ASSERT(HDR_SHARED_DATA(hdr)); 4899*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); 4900*dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(buf)); 4901*dcbf3bd6SGeorge Wilson } 4902*dcbf3bd6SGeorge Wilson 4903fa9e4066Sahrens /* 49043f9d6ad7SLin Ling * Pull the data off of this hdr and attach it to 4905*dcbf3bd6SGeorge Wilson * a new anonymous hdr. Also find the last buffer 4906*dcbf3bd6SGeorge Wilson * in the hdr's buffer list. 4907fa9e4066Sahrens */ 4908*dcbf3bd6SGeorge Wilson arc_buf_t *lastbuf = NULL; 490989c86e32SChris Williamson bufp = &hdr->b_l1hdr.b_buf; 4910*dcbf3bd6SGeorge Wilson while (*bufp != NULL) { 4911*dcbf3bd6SGeorge Wilson if (*bufp == buf) { 4912*dcbf3bd6SGeorge Wilson *bufp = buf->b_next; 4913*dcbf3bd6SGeorge Wilson } 4914*dcbf3bd6SGeorge Wilson 4915*dcbf3bd6SGeorge Wilson /* 4916*dcbf3bd6SGeorge Wilson * If we've removed a buffer in the middle of 4917*dcbf3bd6SGeorge Wilson * the list then update the lastbuf and update 4918*dcbf3bd6SGeorge Wilson * bufp. 4919*dcbf3bd6SGeorge Wilson */ 4920*dcbf3bd6SGeorge Wilson if (*bufp != NULL) { 4921*dcbf3bd6SGeorge Wilson lastbuf = *bufp; 4922*dcbf3bd6SGeorge Wilson bufp = &(*bufp)->b_next; 4923*dcbf3bd6SGeorge Wilson } 4924*dcbf3bd6SGeorge Wilson } 4925af2c4821Smaybee buf->b_next = NULL; 4926*dcbf3bd6SGeorge Wilson ASSERT3P(lastbuf, !=, buf); 4927*dcbf3bd6SGeorge Wilson ASSERT3P(lastbuf, !=, NULL); 4928*dcbf3bd6SGeorge Wilson 4929*dcbf3bd6SGeorge Wilson /* 4930*dcbf3bd6SGeorge Wilson * If the current arc_buf_t and the hdr are sharing their data 4931*dcbf3bd6SGeorge Wilson * buffer, then we must stop sharing that block, transfer 4932*dcbf3bd6SGeorge Wilson * ownership and setup sharing with a new arc_buf_t at the end 4933*dcbf3bd6SGeorge Wilson * of the hdr's b_buf list. 4934*dcbf3bd6SGeorge Wilson */ 4935*dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 4936*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); 4937*dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(lastbuf)); 4938*dcbf3bd6SGeorge Wilson VERIFY(!arc_buf_is_shared(lastbuf)); 4939ea8dc4b6Seschrock 4940*dcbf3bd6SGeorge Wilson /* 4941*dcbf3bd6SGeorge Wilson * First, sever the block sharing relationship between 4942*dcbf3bd6SGeorge Wilson * buf and the arc_buf_hdr_t. Then, setup a new 4943*dcbf3bd6SGeorge Wilson * block sharing relationship with the last buffer 4944*dcbf3bd6SGeorge Wilson * on the arc_buf_t list. 4945*dcbf3bd6SGeorge Wilson */ 4946*dcbf3bd6SGeorge Wilson arc_unshare_buf(hdr, buf); 4947*dcbf3bd6SGeorge Wilson arc_share_buf(hdr, lastbuf); 4948*dcbf3bd6SGeorge Wilson VERIFY3P(lastbuf->b_data, !=, NULL); 4949*dcbf3bd6SGeorge Wilson } else if (HDR_SHARED_DATA(hdr)) { 4950*dcbf3bd6SGeorge Wilson ASSERT(arc_buf_is_shared(lastbuf)); 4951*dcbf3bd6SGeorge Wilson } 4952*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, !=, NULL); 495389c86e32SChris Williamson ASSERT3P(state, !=, arc_l2c_only); 49542fd872a7SPrakash Surya 4955*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_size, 4956*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr), buf); 49572fd872a7SPrakash Surya 495889c86e32SChris Williamson if (refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { 495989c86e32SChris Williamson ASSERT3P(state, !=, arc_l2c_only); 4960*dcbf3bd6SGeorge Wilson (void) refcount_remove_many(&state->arcs_esize[type], 4961*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr), buf); 4962ea8dc4b6Seschrock } 49639253d63dSGeorge Wilson 4964*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_bufcnt -= 1; 4965c717a561Smaybee arc_cksum_verify(buf); 4966cd1c8b85SMatthew Ahrens arc_buf_unwatch(buf); 4967ea8dc4b6Seschrock 4968fa9e4066Sahrens mutex_exit(hash_lock); 4969fa9e4066Sahrens 4970*dcbf3bd6SGeorge Wilson /* 4971*dcbf3bd6SGeorge Wilson * Allocate a new hdr. The new hdr will contain a b_pdata 4972*dcbf3bd6SGeorge Wilson * buffer which will be freed in arc_write(). 4973*dcbf3bd6SGeorge Wilson */ 4974*dcbf3bd6SGeorge Wilson nhdr = arc_hdr_alloc(spa, psize, lsize, compress, type); 4975*dcbf3bd6SGeorge Wilson ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL); 4976*dcbf3bd6SGeorge Wilson ASSERT0(nhdr->b_l1hdr.b_bufcnt); 4977*dcbf3bd6SGeorge Wilson ASSERT0(refcount_count(&nhdr->b_l1hdr.b_refcnt)); 4978*dcbf3bd6SGeorge Wilson VERIFY3U(nhdr->b_type, ==, type); 4979*dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(nhdr)); 498089c86e32SChris Williamson 498189c86e32SChris Williamson nhdr->b_l1hdr.b_buf = buf; 4982*dcbf3bd6SGeorge Wilson nhdr->b_l1hdr.b_bufcnt = 1; 498389c86e32SChris Williamson (void) refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); 4984af2c4821Smaybee buf->b_hdr = nhdr; 4985*dcbf3bd6SGeorge Wilson 49863f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 4987*dcbf3bd6SGeorge Wilson (void) refcount_add_many(&arc_anon->arcs_size, 4988*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(nhdr), buf); 4989fa9e4066Sahrens } else { 49903f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 499189c86e32SChris Williamson ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); 4992244781f1SPrakash Surya /* protected by hash lock, or hdr is on arc_anon */ 4993244781f1SPrakash Surya ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 4994fa9e4066Sahrens ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 499589c86e32SChris Williamson arc_change_state(arc_anon, hdr, hash_lock); 499689c86e32SChris Williamson hdr->b_l1hdr.b_arc_access = 0; 499789c86e32SChris Williamson mutex_exit(hash_lock); 4998fa94a07fSbrendan 49993f9d6ad7SLin Ling buf_discard_identity(hdr); 5000c717a561Smaybee arc_buf_thaw(buf); 5001fa9e4066Sahrens } 5002fa9e4066Sahrens } 5003fa9e4066Sahrens 5004fa9e4066Sahrens int 5005fa9e4066Sahrens arc_released(arc_buf_t *buf) 5006fa9e4066Sahrens { 50076f83844dSMark Maybee int released; 50086f83844dSMark Maybee 50093f9d6ad7SLin Ling mutex_enter(&buf->b_evict_lock); 501089c86e32SChris Williamson released = (buf->b_data != NULL && 501189c86e32SChris Williamson buf->b_hdr->b_l1hdr.b_state == arc_anon); 50123f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 50136f83844dSMark Maybee return (released); 5014ea8dc4b6Seschrock } 5015ea8dc4b6Seschrock 5016ea8dc4b6Seschrock #ifdef ZFS_DEBUG 5017ea8dc4b6Seschrock int 5018ea8dc4b6Seschrock arc_referenced(arc_buf_t *buf) 5019ea8dc4b6Seschrock { 50206f83844dSMark Maybee int referenced; 50216f83844dSMark Maybee 50223f9d6ad7SLin Ling mutex_enter(&buf->b_evict_lock); 502389c86e32SChris Williamson referenced = (refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); 50243f9d6ad7SLin Ling mutex_exit(&buf->b_evict_lock); 50256f83844dSMark Maybee return (referenced); 5026ea8dc4b6Seschrock } 5027ea8dc4b6Seschrock #endif 5028ea8dc4b6Seschrock 5029c717a561Smaybee static void 5030c717a561Smaybee arc_write_ready(zio_t *zio) 5031c717a561Smaybee { 5032c717a561Smaybee arc_write_callback_t *callback = zio->io_private; 5033c717a561Smaybee arc_buf_t *buf = callback->awcb_buf; 50340a4e9518Sgw arc_buf_hdr_t *hdr = buf->b_hdr; 5035*dcbf3bd6SGeorge Wilson uint64_t psize = BP_IS_HOLE(zio->io_bp) ? 0 : BP_GET_PSIZE(zio->io_bp); 5036c717a561Smaybee 503789c86e32SChris Williamson ASSERT(HDR_HAS_L1HDR(hdr)); 503889c86e32SChris Williamson ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); 5039*dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 5040e14bb325SJeff Bonwick 50410a4e9518Sgw /* 5042*dcbf3bd6SGeorge Wilson * If we're reexecuting this zio because the pool suspended, then 5043*dcbf3bd6SGeorge Wilson * cleanup any state that was previously set the first time the 5044*dcbf3bd6SGeorge Wilson * callback as invoked. 50450a4e9518Sgw */ 5046*dcbf3bd6SGeorge Wilson if (zio->io_flags & ZIO_FLAG_REEXECUTED) { 5047*dcbf3bd6SGeorge Wilson arc_cksum_free(hdr); 5048*dcbf3bd6SGeorge Wilson arc_buf_unwatch(buf); 5049*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_pdata != NULL) { 5050*dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 5051*dcbf3bd6SGeorge Wilson ASSERT(HDR_SHARED_DATA(hdr)); 5052*dcbf3bd6SGeorge Wilson 5053*dcbf3bd6SGeorge Wilson arc_unshare_buf(hdr, buf); 5054*dcbf3bd6SGeorge Wilson } else { 5055*dcbf3bd6SGeorge Wilson arc_hdr_free_pdata(hdr); 5056*dcbf3bd6SGeorge Wilson } 50570a4e9518Sgw } 50580a4e9518Sgw } 5059*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 5060*dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 5061*dcbf3bd6SGeorge Wilson ASSERT(!arc_buf_is_shared(buf)); 5062*dcbf3bd6SGeorge Wilson 5063*dcbf3bd6SGeorge Wilson callback->awcb_ready(zio, buf, callback->awcb_private); 5064*dcbf3bd6SGeorge Wilson 5065*dcbf3bd6SGeorge Wilson if (HDR_IO_IN_PROGRESS(hdr)) 5066*dcbf3bd6SGeorge Wilson ASSERT(zio->io_flags & ZIO_FLAG_REEXECUTED); 5067*dcbf3bd6SGeorge Wilson 5068*dcbf3bd6SGeorge Wilson arc_cksum_compute(buf); 5069*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5070*dcbf3bd6SGeorge Wilson 5071*dcbf3bd6SGeorge Wilson enum zio_compress compress; 5072*dcbf3bd6SGeorge Wilson if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 5073*dcbf3bd6SGeorge Wilson compress = ZIO_COMPRESS_OFF; 5074*dcbf3bd6SGeorge Wilson } else { 5075*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(zio->io_bp)); 5076*dcbf3bd6SGeorge Wilson compress = BP_GET_COMPRESS(zio->io_bp); 5077*dcbf3bd6SGeorge Wilson } 5078*dcbf3bd6SGeorge Wilson HDR_SET_PSIZE(hdr, psize); 5079*dcbf3bd6SGeorge Wilson arc_hdr_set_compress(hdr, compress); 5080*dcbf3bd6SGeorge Wilson 5081*dcbf3bd6SGeorge Wilson /* 5082*dcbf3bd6SGeorge Wilson * If the hdr is compressed, then copy the compressed 5083*dcbf3bd6SGeorge Wilson * zio contents into arc_buf_hdr_t. Otherwise, copy the original 5084*dcbf3bd6SGeorge Wilson * data buf into the hdr. Ideally, we would like to always copy the 5085*dcbf3bd6SGeorge Wilson * io_data into b_pdata but the user may have disabled compressed 5086*dcbf3bd6SGeorge Wilson * arc thus the on-disk block may or may not match what we maintain 5087*dcbf3bd6SGeorge Wilson * in the hdr's b_pdata field. 5088*dcbf3bd6SGeorge Wilson */ 5089*dcbf3bd6SGeorge Wilson if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) { 5090*dcbf3bd6SGeorge Wilson ASSERT(BP_GET_COMPRESS(zio->io_bp) != ZIO_COMPRESS_OFF); 5091*dcbf3bd6SGeorge Wilson ASSERT3U(psize, >, 0); 5092*dcbf3bd6SGeorge Wilson arc_hdr_alloc_pdata(hdr); 5093*dcbf3bd6SGeorge Wilson bcopy(zio->io_data, hdr->b_l1hdr.b_pdata, psize); 5094*dcbf3bd6SGeorge Wilson } else { 5095*dcbf3bd6SGeorge Wilson ASSERT3P(buf->b_data, ==, zio->io_orig_data); 5096*dcbf3bd6SGeorge Wilson ASSERT3U(zio->io_orig_size, ==, HDR_GET_LSIZE(hdr)); 5097*dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_byteswap, ==, DMU_BSWAP_NUMFUNCS); 5098*dcbf3bd6SGeorge Wilson ASSERT(!HDR_SHARED_DATA(hdr)); 5099*dcbf3bd6SGeorge Wilson ASSERT(!arc_buf_is_shared(buf)); 5100*dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 5101*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 5102*dcbf3bd6SGeorge Wilson 5103*dcbf3bd6SGeorge Wilson /* 5104*dcbf3bd6SGeorge Wilson * This hdr is not compressed so we're able to share 5105*dcbf3bd6SGeorge Wilson * the arc_buf_t data buffer with the hdr. 5106*dcbf3bd6SGeorge Wilson */ 5107*dcbf3bd6SGeorge Wilson arc_share_buf(hdr, buf); 5108*dcbf3bd6SGeorge Wilson VERIFY0(bcmp(zio->io_orig_data, hdr->b_l1hdr.b_pdata, 5109*dcbf3bd6SGeorge Wilson HDR_GET_LSIZE(hdr))); 5110*dcbf3bd6SGeorge Wilson } 5111*dcbf3bd6SGeorge Wilson arc_hdr_verify(hdr, zio->io_bp); 5112c717a561Smaybee } 5113c717a561Smaybee 51148df0bcf0SPaul Dagnelie static void 51158df0bcf0SPaul Dagnelie arc_write_children_ready(zio_t *zio) 51168df0bcf0SPaul Dagnelie { 51178df0bcf0SPaul Dagnelie arc_write_callback_t *callback = zio->io_private; 51188df0bcf0SPaul Dagnelie arc_buf_t *buf = callback->awcb_buf; 51198df0bcf0SPaul Dagnelie 51208df0bcf0SPaul Dagnelie callback->awcb_children_ready(zio, buf, callback->awcb_private); 51218df0bcf0SPaul Dagnelie } 51228df0bcf0SPaul Dagnelie 512369962b56SMatthew Ahrens /* 512469962b56SMatthew Ahrens * The SPA calls this callback for each physical write that happens on behalf 512569962b56SMatthew Ahrens * of a logical write. See the comment in dbuf_write_physdone() for details. 512669962b56SMatthew Ahrens */ 512769962b56SMatthew Ahrens static void 512869962b56SMatthew Ahrens arc_write_physdone(zio_t *zio) 512969962b56SMatthew Ahrens { 513069962b56SMatthew Ahrens arc_write_callback_t *cb = zio->io_private; 513169962b56SMatthew Ahrens if (cb->awcb_physdone != NULL) 513269962b56SMatthew Ahrens cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 513369962b56SMatthew Ahrens } 513469962b56SMatthew Ahrens 5135fa9e4066Sahrens static void 5136fa9e4066Sahrens arc_write_done(zio_t *zio) 5137fa9e4066Sahrens { 5138c717a561Smaybee arc_write_callback_t *callback = zio->io_private; 5139c717a561Smaybee arc_buf_t *buf = callback->awcb_buf; 5140c717a561Smaybee arc_buf_hdr_t *hdr = buf->b_hdr; 5141fa9e4066Sahrens 5142*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 5143b24ab676SJeff Bonwick 5144b24ab676SJeff Bonwick if (zio->io_error == 0) { 5145*dcbf3bd6SGeorge Wilson arc_hdr_verify(hdr, zio->io_bp); 5146*dcbf3bd6SGeorge Wilson 51475d7b4d43SMatthew Ahrens if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 514843466aaeSMax Grossman buf_discard_identity(hdr); 514943466aaeSMax Grossman } else { 515043466aaeSMax Grossman hdr->b_dva = *BP_IDENTITY(zio->io_bp); 515143466aaeSMax Grossman hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 515243466aaeSMax Grossman } 5153b24ab676SJeff Bonwick } else { 5154*dcbf3bd6SGeorge Wilson ASSERT(HDR_EMPTY(hdr)); 5155b24ab676SJeff Bonwick } 5156fa9e4066Sahrens 5157ea8dc4b6Seschrock /* 51585d7b4d43SMatthew Ahrens * If the block to be written was all-zero or compressed enough to be 51595d7b4d43SMatthew Ahrens * embedded in the BP, no write was performed so there will be no 51605d7b4d43SMatthew Ahrens * dva/birth/checksum. The buffer must therefore remain anonymous 51615d7b4d43SMatthew Ahrens * (and uncached). 5162ea8dc4b6Seschrock */ 5163*dcbf3bd6SGeorge Wilson if (!HDR_EMPTY(hdr)) { 5164fa9e4066Sahrens arc_buf_hdr_t *exists; 5165fa9e4066Sahrens kmutex_t *hash_lock; 5166fa9e4066Sahrens 5167b24ab676SJeff Bonwick ASSERT(zio->io_error == 0); 5168b24ab676SJeff Bonwick 51696b4acc8bSahrens arc_cksum_verify(buf); 51706b4acc8bSahrens 5171fa9e4066Sahrens exists = buf_hash_insert(hdr, &hash_lock); 517289c86e32SChris Williamson if (exists != NULL) { 5173fa9e4066Sahrens /* 5174fa9e4066Sahrens * This can only happen if we overwrite for 5175fa9e4066Sahrens * sync-to-convergence, because we remove 5176fa9e4066Sahrens * buffers from the hash table when we arc_free(). 5177fa9e4066Sahrens */ 5178b24ab676SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 5179b24ab676SJeff Bonwick if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 5180b24ab676SJeff Bonwick panic("bad overwrite, hdr=%p exists=%p", 5181b24ab676SJeff Bonwick (void *)hdr, (void *)exists); 518289c86e32SChris Williamson ASSERT(refcount_is_zero( 518389c86e32SChris Williamson &exists->b_l1hdr.b_refcnt)); 5184b24ab676SJeff Bonwick arc_change_state(arc_anon, exists, hash_lock); 5185b24ab676SJeff Bonwick mutex_exit(hash_lock); 5186b24ab676SJeff Bonwick arc_hdr_destroy(exists); 5187b24ab676SJeff Bonwick exists = buf_hash_insert(hdr, &hash_lock); 5188b24ab676SJeff Bonwick ASSERT3P(exists, ==, NULL); 518980901aeaSGeorge Wilson } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 519080901aeaSGeorge Wilson /* nopwrite */ 519180901aeaSGeorge Wilson ASSERT(zio->io_prop.zp_nopwrite); 519280901aeaSGeorge Wilson if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 519380901aeaSGeorge Wilson panic("bad nopwrite, hdr=%p exists=%p", 519480901aeaSGeorge Wilson (void *)hdr, (void *)exists); 5195b24ab676SJeff Bonwick } else { 5196b24ab676SJeff Bonwick /* Dedup */ 5197*dcbf3bd6SGeorge Wilson ASSERT(hdr->b_l1hdr.b_bufcnt == 1); 519889c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_state == arc_anon); 5199b24ab676SJeff Bonwick ASSERT(BP_GET_DEDUP(zio->io_bp)); 5200b24ab676SJeff Bonwick ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 5201ae46e4c7SMatthew Ahrens } 5202fa9e4066Sahrens } 5203*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5204088f3894Sahrens /* if it's not anon, we are doing a scrub */ 520589c86e32SChris Williamson if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) 5206088f3894Sahrens arc_access(hdr, hash_lock); 520744eda4d7Smaybee mutex_exit(hash_lock); 5208ea8dc4b6Seschrock } else { 5209*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5210fa9e4066Sahrens } 5211ea8dc4b6Seschrock 521289c86e32SChris Williamson ASSERT(!refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 5213b24ab676SJeff Bonwick callback->awcb_done(zio, buf, callback->awcb_private); 5214fa9e4066Sahrens 5215c717a561Smaybee kmem_free(callback, sizeof (arc_write_callback_t)); 5216fa9e4066Sahrens } 5217fa9e4066Sahrens 5218c717a561Smaybee zio_t * 5219*dcbf3bd6SGeorge Wilson arc_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 5220*dcbf3bd6SGeorge Wilson boolean_t l2arc, const zio_prop_t *zp, arc_done_func_t *ready, 52218df0bcf0SPaul Dagnelie arc_done_func_t *children_ready, arc_done_func_t *physdone, 522269962b56SMatthew Ahrens arc_done_func_t *done, void *private, zio_priority_t priority, 52237802d7bfSMatthew Ahrens int zio_flags, const zbookmark_phys_t *zb) 5224fa9e4066Sahrens { 5225fa9e4066Sahrens arc_buf_hdr_t *hdr = buf->b_hdr; 5226c717a561Smaybee arc_write_callback_t *callback; 5227e14bb325SJeff Bonwick zio_t *zio; 5228fa9e4066Sahrens 5229*dcbf3bd6SGeorge Wilson ASSERT3P(ready, !=, NULL); 5230*dcbf3bd6SGeorge Wilson ASSERT3P(done, !=, NULL); 5231fa9e4066Sahrens ASSERT(!HDR_IO_ERROR(hdr)); 523289c86e32SChris Williamson ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 5233*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 5234*dcbf3bd6SGeorge Wilson ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); 52353baa08fcSek if (l2arc) 5236*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 5237c717a561Smaybee callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 5238c717a561Smaybee callback->awcb_ready = ready; 52398df0bcf0SPaul Dagnelie callback->awcb_children_ready = children_ready; 524069962b56SMatthew Ahrens callback->awcb_physdone = physdone; 5241c717a561Smaybee callback->awcb_done = done; 5242c717a561Smaybee callback->awcb_private = private; 5243c717a561Smaybee callback->awcb_buf = buf; 5244088f3894Sahrens 5245*dcbf3bd6SGeorge Wilson /* 5246*dcbf3bd6SGeorge Wilson * The hdr's b_pdata is now stale, free it now. A new data block 5247*dcbf3bd6SGeorge Wilson * will be allocated when the zio pipeline calls arc_write_ready(). 5248*dcbf3bd6SGeorge Wilson */ 5249*dcbf3bd6SGeorge Wilson if (hdr->b_l1hdr.b_pdata != NULL) { 5250*dcbf3bd6SGeorge Wilson /* 5251*dcbf3bd6SGeorge Wilson * If the buf is currently sharing the data block with 5252*dcbf3bd6SGeorge Wilson * the hdr then we need to break that relationship here. 5253*dcbf3bd6SGeorge Wilson * The hdr will remain with a NULL data pointer and the 5254*dcbf3bd6SGeorge Wilson * buf will take sole ownership of the block. 5255*dcbf3bd6SGeorge Wilson */ 5256*dcbf3bd6SGeorge Wilson if (arc_buf_is_shared(buf)) { 5257*dcbf3bd6SGeorge Wilson ASSERT(ARC_BUF_LAST(buf)); 5258*dcbf3bd6SGeorge Wilson arc_unshare_buf(hdr, buf); 5259*dcbf3bd6SGeorge Wilson } else { 5260*dcbf3bd6SGeorge Wilson arc_hdr_free_pdata(hdr); 5261*dcbf3bd6SGeorge Wilson } 5262*dcbf3bd6SGeorge Wilson VERIFY3P(buf->b_data, !=, NULL); 5263*dcbf3bd6SGeorge Wilson arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF); 5264*dcbf3bd6SGeorge Wilson } 5265*dcbf3bd6SGeorge Wilson ASSERT(!arc_buf_is_shared(buf)); 5266*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, ==, NULL); 5267*dcbf3bd6SGeorge Wilson 5268*dcbf3bd6SGeorge Wilson zio = zio_write(pio, spa, txg, bp, buf->b_data, HDR_GET_LSIZE(hdr), zp, 52698df0bcf0SPaul Dagnelie arc_write_ready, 52708df0bcf0SPaul Dagnelie (children_ready != NULL) ? arc_write_children_ready : NULL, 52718df0bcf0SPaul Dagnelie arc_write_physdone, arc_write_done, callback, 527269962b56SMatthew Ahrens priority, zio_flags, zb); 5273fa9e4066Sahrens 5274c717a561Smaybee return (zio); 5275fa9e4066Sahrens } 5276fa9e4066Sahrens 52771ab7f2deSmaybee static int 527869962b56SMatthew Ahrens arc_memory_throttle(uint64_t reserve, uint64_t txg) 52791ab7f2deSmaybee { 52801ab7f2deSmaybee #ifdef _KERNEL 52811ab7f2deSmaybee uint64_t available_memory = ptob(freemem); 52821ab7f2deSmaybee static uint64_t page_load = 0; 52831ab7f2deSmaybee static uint64_t last_txg = 0; 52841ab7f2deSmaybee 52851ab7f2deSmaybee #if defined(__i386) 52861ab7f2deSmaybee available_memory = 52871ab7f2deSmaybee MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 52881ab7f2deSmaybee #endif 528969962b56SMatthew Ahrens 529069962b56SMatthew Ahrens if (freemem > physmem * arc_lotsfree_percent / 100) 52911ab7f2deSmaybee return (0); 52921ab7f2deSmaybee 52931ab7f2deSmaybee if (txg > last_txg) { 52941ab7f2deSmaybee last_txg = txg; 52951ab7f2deSmaybee page_load = 0; 52961ab7f2deSmaybee } 52971ab7f2deSmaybee /* 52981ab7f2deSmaybee * If we are in pageout, we know that memory is already tight, 52991ab7f2deSmaybee * the arc is already going to be evicting, so we just want to 53001ab7f2deSmaybee * continue to let page writes occur as quickly as possible. 53011ab7f2deSmaybee */ 53021ab7f2deSmaybee if (curproc == proc_pageout) { 53031ab7f2deSmaybee if (page_load > MAX(ptob(minfree), available_memory) / 4) 5304be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART)); 53051ab7f2deSmaybee /* Note: reserve is inflated, so we deflate */ 53061ab7f2deSmaybee page_load += reserve / 8; 53071ab7f2deSmaybee return (0); 53081ab7f2deSmaybee } else if (page_load > 0 && arc_reclaim_needed()) { 53091ab7f2deSmaybee /* memory is low, delay before restarting */ 53101ab7f2deSmaybee ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 5311be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 53121ab7f2deSmaybee } 53131ab7f2deSmaybee page_load = 0; 53141ab7f2deSmaybee #endif 53151ab7f2deSmaybee return (0); 53161ab7f2deSmaybee } 53171ab7f2deSmaybee 5318fa9e4066Sahrens void 53191ab7f2deSmaybee arc_tempreserve_clear(uint64_t reserve) 5320fa9e4066Sahrens { 53211ab7f2deSmaybee atomic_add_64(&arc_tempreserve, -reserve); 5322fa9e4066Sahrens ASSERT((int64_t)arc_tempreserve >= 0); 5323fa9e4066Sahrens } 5324fa9e4066Sahrens 5325fa9e4066Sahrens int 53261ab7f2deSmaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg) 5327fa9e4066Sahrens { 53281ab7f2deSmaybee int error; 53292fdbea25SAleksandr Guzovskiy uint64_t anon_size; 53301ab7f2deSmaybee 53311ab7f2deSmaybee if (reserve > arc_c/4 && !arc_no_grow) 53321ab7f2deSmaybee arc_c = MIN(arc_c_max, reserve * 4); 53331ab7f2deSmaybee if (reserve > arc_c) 5334be6fd75aSMatthew Ahrens return (SET_ERROR(ENOMEM)); 5335112fe045Smaybee 53362fdbea25SAleksandr Guzovskiy /* 53372fdbea25SAleksandr Guzovskiy * Don't count loaned bufs as in flight dirty data to prevent long 53382fdbea25SAleksandr Guzovskiy * network delays from blocking transactions that are ready to be 53392fdbea25SAleksandr Guzovskiy * assigned to a txg. 53402fdbea25SAleksandr Guzovskiy */ 53412fd872a7SPrakash Surya anon_size = MAX((int64_t)(refcount_count(&arc_anon->arcs_size) - 53422fd872a7SPrakash Surya arc_loaned_bytes), 0); 53432fdbea25SAleksandr Guzovskiy 53441ab7f2deSmaybee /* 53451ab7f2deSmaybee * Writes will, almost always, require additional memory allocations 5346f7170741SWill Andrews * in order to compress/encrypt/etc the data. We therefore need to 53471ab7f2deSmaybee * make sure that there is sufficient available memory for this. 53481ab7f2deSmaybee */ 534969962b56SMatthew Ahrens error = arc_memory_throttle(reserve, txg); 535069962b56SMatthew Ahrens if (error != 0) 53511ab7f2deSmaybee return (error); 53521ab7f2deSmaybee 5353fa9e4066Sahrens /* 5354112fe045Smaybee * Throttle writes when the amount of dirty data in the cache 5355112fe045Smaybee * gets too large. We try to keep the cache less than half full 5356112fe045Smaybee * of dirty blocks so that our sync times don't grow too large. 5357112fe045Smaybee * Note: if two requests come in concurrently, we might let them 5358112fe045Smaybee * both succeed, when one of them should fail. Not a huge deal. 5359fa9e4066Sahrens */ 53602fdbea25SAleksandr Guzovskiy 53612fdbea25SAleksandr Guzovskiy if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 53622fdbea25SAleksandr Guzovskiy anon_size > arc_c / 4) { 5363*dcbf3bd6SGeorge Wilson uint64_t meta_esize = 5364*dcbf3bd6SGeorge Wilson refcount_count(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 5365*dcbf3bd6SGeorge Wilson uint64_t data_esize = 5366*dcbf3bd6SGeorge Wilson refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 53670e8c6158Smaybee dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 53680e8c6158Smaybee "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 5369*dcbf3bd6SGeorge Wilson arc_tempreserve >> 10, meta_esize >> 10, 5370*dcbf3bd6SGeorge Wilson data_esize >> 10, reserve >> 10, arc_c >> 10); 5371be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART)); 5372fa9e4066Sahrens } 53731ab7f2deSmaybee atomic_add_64(&arc_tempreserve, reserve); 5374fa9e4066Sahrens return (0); 5375fa9e4066Sahrens } 5376fa9e4066Sahrens 53774076b1bfSPrakash Surya static void 53784076b1bfSPrakash Surya arc_kstat_update_state(arc_state_t *state, kstat_named_t *size, 53794076b1bfSPrakash Surya kstat_named_t *evict_data, kstat_named_t *evict_metadata) 53804076b1bfSPrakash Surya { 53812fd872a7SPrakash Surya size->value.ui64 = refcount_count(&state->arcs_size); 5382*dcbf3bd6SGeorge Wilson evict_data->value.ui64 = 5383*dcbf3bd6SGeorge Wilson refcount_count(&state->arcs_esize[ARC_BUFC_DATA]); 5384*dcbf3bd6SGeorge Wilson evict_metadata->value.ui64 = 5385*dcbf3bd6SGeorge Wilson refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]); 53864076b1bfSPrakash Surya } 53874076b1bfSPrakash Surya 53884076b1bfSPrakash Surya static int 53894076b1bfSPrakash Surya arc_kstat_update(kstat_t *ksp, int rw) 53904076b1bfSPrakash Surya { 53914076b1bfSPrakash Surya arc_stats_t *as = ksp->ks_data; 53924076b1bfSPrakash Surya 53934076b1bfSPrakash Surya if (rw == KSTAT_WRITE) { 53944076b1bfSPrakash Surya return (EACCES); 53954076b1bfSPrakash Surya } else { 53964076b1bfSPrakash Surya arc_kstat_update_state(arc_anon, 53974076b1bfSPrakash Surya &as->arcstat_anon_size, 53984076b1bfSPrakash Surya &as->arcstat_anon_evictable_data, 53994076b1bfSPrakash Surya &as->arcstat_anon_evictable_metadata); 54004076b1bfSPrakash Surya arc_kstat_update_state(arc_mru, 54014076b1bfSPrakash Surya &as->arcstat_mru_size, 54024076b1bfSPrakash Surya &as->arcstat_mru_evictable_data, 54034076b1bfSPrakash Surya &as->arcstat_mru_evictable_metadata); 54044076b1bfSPrakash Surya arc_kstat_update_state(arc_mru_ghost, 54054076b1bfSPrakash Surya &as->arcstat_mru_ghost_size, 54064076b1bfSPrakash Surya &as->arcstat_mru_ghost_evictable_data, 54074076b1bfSPrakash Surya &as->arcstat_mru_ghost_evictable_metadata); 54084076b1bfSPrakash Surya arc_kstat_update_state(arc_mfu, 54094076b1bfSPrakash Surya &as->arcstat_mfu_size, 54104076b1bfSPrakash Surya &as->arcstat_mfu_evictable_data, 54114076b1bfSPrakash Surya &as->arcstat_mfu_evictable_metadata); 54124076b1bfSPrakash Surya arc_kstat_update_state(arc_mfu_ghost, 54134076b1bfSPrakash Surya &as->arcstat_mfu_ghost_size, 54144076b1bfSPrakash Surya &as->arcstat_mfu_ghost_evictable_data, 54154076b1bfSPrakash Surya &as->arcstat_mfu_ghost_evictable_metadata); 54164076b1bfSPrakash Surya } 54174076b1bfSPrakash Surya 54184076b1bfSPrakash Surya return (0); 54194076b1bfSPrakash Surya } 54204076b1bfSPrakash Surya 5421*dcbf3bd6SGeorge Wilson /* 5422*dcbf3bd6SGeorge Wilson * This function *must* return indices evenly distributed between all 5423*dcbf3bd6SGeorge Wilson * sublists of the multilist. This is needed due to how the ARC eviction 5424*dcbf3bd6SGeorge Wilson * code is laid out; arc_evict_state() assumes ARC buffers are evenly 5425*dcbf3bd6SGeorge Wilson * distributed between all sublists and uses this assumption when 5426*dcbf3bd6SGeorge Wilson * deciding which sublist to evict from and how much to evict from it. 5427*dcbf3bd6SGeorge Wilson */ 5428*dcbf3bd6SGeorge Wilson unsigned int 5429*dcbf3bd6SGeorge Wilson arc_state_multilist_index_func(multilist_t *ml, void *obj) 5430*dcbf3bd6SGeorge Wilson { 5431*dcbf3bd6SGeorge Wilson arc_buf_hdr_t *hdr = obj; 5432*dcbf3bd6SGeorge Wilson 5433*dcbf3bd6SGeorge Wilson /* 5434*dcbf3bd6SGeorge Wilson * We rely on b_dva to generate evenly distributed index 5435*dcbf3bd6SGeorge Wilson * numbers using buf_hash below. So, as an added precaution, 5436*dcbf3bd6SGeorge Wilson * let's make sure we never add empty buffers to the arc lists. 5437*dcbf3bd6SGeorge Wilson */ 5438*dcbf3bd6SGeorge Wilson ASSERT(!HDR_EMPTY(hdr)); 5439*dcbf3bd6SGeorge Wilson 5440*dcbf3bd6SGeorge Wilson /* 5441*dcbf3bd6SGeorge Wilson * The assumption here, is the hash value for a given 5442*dcbf3bd6SGeorge Wilson * arc_buf_hdr_t will remain constant throughout it's lifetime 5443*dcbf3bd6SGeorge Wilson * (i.e. it's b_spa, b_dva, and b_birth fields don't change). 5444*dcbf3bd6SGeorge Wilson * Thus, we don't need to store the header's sublist index 5445*dcbf3bd6SGeorge Wilson * on insertion, as this index can be recalculated on removal. 5446*dcbf3bd6SGeorge Wilson * 5447*dcbf3bd6SGeorge Wilson * Also, the low order bits of the hash value are thought to be 5448*dcbf3bd6SGeorge Wilson * distributed evenly. Otherwise, in the case that the multilist 5449*dcbf3bd6SGeorge Wilson * has a power of two number of sublists, each sublists' usage 5450*dcbf3bd6SGeorge Wilson * would not be evenly distributed. 5451*dcbf3bd6SGeorge Wilson */ 5452*dcbf3bd6SGeorge Wilson return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % 5453*dcbf3bd6SGeorge Wilson multilist_get_num_sublists(ml)); 5454*dcbf3bd6SGeorge Wilson } 5455*dcbf3bd6SGeorge Wilson 5456*dcbf3bd6SGeorge Wilson static void 5457*dcbf3bd6SGeorge Wilson arc_state_init(void) 5458*dcbf3bd6SGeorge Wilson { 5459*dcbf3bd6SGeorge Wilson arc_anon = &ARC_anon; 5460*dcbf3bd6SGeorge Wilson arc_mru = &ARC_mru; 5461*dcbf3bd6SGeorge Wilson arc_mru_ghost = &ARC_mru_ghost; 5462*dcbf3bd6SGeorge Wilson arc_mfu = &ARC_mfu; 5463*dcbf3bd6SGeorge Wilson arc_mfu_ghost = &ARC_mfu_ghost; 5464*dcbf3bd6SGeorge Wilson arc_l2c_only = &ARC_l2c_only; 5465*dcbf3bd6SGeorge Wilson 5466*dcbf3bd6SGeorge Wilson multilist_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 5467*dcbf3bd6SGeorge Wilson sizeof (arc_buf_hdr_t), 5468*dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5469*dcbf3bd6SGeorge Wilson zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5470*dcbf3bd6SGeorge Wilson multilist_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 5471*dcbf3bd6SGeorge Wilson sizeof (arc_buf_hdr_t), 5472*dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5473*dcbf3bd6SGeorge Wilson zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5474*dcbf3bd6SGeorge Wilson multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 5475*dcbf3bd6SGeorge Wilson sizeof (arc_buf_hdr_t), 5476*dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5477*dcbf3bd6SGeorge Wilson zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5478*dcbf3bd6SGeorge Wilson multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 5479*dcbf3bd6SGeorge Wilson sizeof (arc_buf_hdr_t), 5480*dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5481*dcbf3bd6SGeorge Wilson zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5482*dcbf3bd6SGeorge Wilson multilist_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 5483*dcbf3bd6SGeorge Wilson sizeof (arc_buf_hdr_t), 5484*dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5485*dcbf3bd6SGeorge Wilson zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5486*dcbf3bd6SGeorge Wilson multilist_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 5487*dcbf3bd6SGeorge Wilson sizeof (arc_buf_hdr_t), 5488*dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5489*dcbf3bd6SGeorge Wilson zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5490*dcbf3bd6SGeorge Wilson multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 5491*dcbf3bd6SGeorge Wilson sizeof (arc_buf_hdr_t), 5492*dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5493*dcbf3bd6SGeorge Wilson zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5494*dcbf3bd6SGeorge Wilson multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 5495*dcbf3bd6SGeorge Wilson sizeof (arc_buf_hdr_t), 5496*dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5497*dcbf3bd6SGeorge Wilson zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5498*dcbf3bd6SGeorge Wilson multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 5499*dcbf3bd6SGeorge Wilson sizeof (arc_buf_hdr_t), 5500*dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5501*dcbf3bd6SGeorge Wilson zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5502*dcbf3bd6SGeorge Wilson multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 5503*dcbf3bd6SGeorge Wilson sizeof (arc_buf_hdr_t), 5504*dcbf3bd6SGeorge Wilson offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5505*dcbf3bd6SGeorge Wilson zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5506*dcbf3bd6SGeorge Wilson 5507*dcbf3bd6SGeorge Wilson refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 5508*dcbf3bd6SGeorge Wilson refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 5509*dcbf3bd6SGeorge Wilson refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); 5510*dcbf3bd6SGeorge Wilson refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]); 5511*dcbf3bd6SGeorge Wilson refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); 5512*dcbf3bd6SGeorge Wilson refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); 5513*dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); 5514*dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); 5515*dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); 5516*dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); 5517*dcbf3bd6SGeorge Wilson refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); 5518*dcbf3bd6SGeorge Wilson refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); 5519*dcbf3bd6SGeorge Wilson 5520*dcbf3bd6SGeorge Wilson refcount_create(&arc_anon->arcs_size); 5521*dcbf3bd6SGeorge Wilson refcount_create(&arc_mru->arcs_size); 5522*dcbf3bd6SGeorge Wilson refcount_create(&arc_mru_ghost->arcs_size); 5523*dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu->arcs_size); 5524*dcbf3bd6SGeorge Wilson refcount_create(&arc_mfu_ghost->arcs_size); 5525*dcbf3bd6SGeorge Wilson refcount_create(&arc_l2c_only->arcs_size); 5526*dcbf3bd6SGeorge Wilson } 5527*dcbf3bd6SGeorge Wilson 5528*dcbf3bd6SGeorge Wilson static void 5529*dcbf3bd6SGeorge Wilson arc_state_fini(void) 5530*dcbf3bd6SGeorge Wilson { 5531*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 5532*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 5533*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); 5534*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]); 5535*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); 5536*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); 5537*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); 5538*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); 5539*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); 5540*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); 5541*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); 5542*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); 5543*dcbf3bd6SGeorge Wilson 5544*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_anon->arcs_size); 5545*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru->arcs_size); 5546*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mru_ghost->arcs_size); 5547*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu->arcs_size); 5548*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_mfu_ghost->arcs_size); 5549*dcbf3bd6SGeorge Wilson refcount_destroy(&arc_l2c_only->arcs_size); 5550*dcbf3bd6SGeorge Wilson 5551*dcbf3bd6SGeorge Wilson multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 5552*dcbf3bd6SGeorge Wilson multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 5553*dcbf3bd6SGeorge Wilson multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 5554*dcbf3bd6SGeorge Wilson multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 5555*dcbf3bd6SGeorge Wilson multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 5556*dcbf3bd6SGeorge Wilson multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 5557*dcbf3bd6SGeorge Wilson multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 5558*dcbf3bd6SGeorge Wilson multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 5559*dcbf3bd6SGeorge Wilson } 5560*dcbf3bd6SGeorge Wilson 5561*dcbf3bd6SGeorge Wilson uint64_t 5562*dcbf3bd6SGeorge Wilson arc_max_bytes(void) 5563244781f1SPrakash Surya { 5564*dcbf3bd6SGeorge Wilson return (arc_c_max); 5565244781f1SPrakash Surya } 5566244781f1SPrakash Surya 5567fa9e4066Sahrens void 5568fa9e4066Sahrens arc_init(void) 5569fa9e4066Sahrens { 55702ec99e3eSMatthew Ahrens /* 55712ec99e3eSMatthew Ahrens * allmem is "all memory that we could possibly use". 55722ec99e3eSMatthew Ahrens */ 55732ec99e3eSMatthew Ahrens #ifdef _KERNEL 55742ec99e3eSMatthew Ahrens uint64_t allmem = ptob(physmem - swapfs_minfree); 55752ec99e3eSMatthew Ahrens #else 55762ec99e3eSMatthew Ahrens uint64_t allmem = (physmem * PAGESIZE) / 2; 55772ec99e3eSMatthew Ahrens #endif 55782ec99e3eSMatthew Ahrens 5579244781f1SPrakash Surya mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL); 5580244781f1SPrakash Surya cv_init(&arc_reclaim_thread_cv, NULL, CV_DEFAULT, NULL); 5581244781f1SPrakash Surya cv_init(&arc_reclaim_waiters_cv, NULL, CV_DEFAULT, NULL); 5582244781f1SPrakash Surya 558313506d1eSmaybee /* Convert seconds to clock ticks */ 5584b19a79ecSperrin arc_min_prefetch_lifespan = 1 * hz; 558513506d1eSmaybee 5586112fe045Smaybee /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 55872ec99e3eSMatthew Ahrens arc_c_min = MAX(allmem / 32, 64 << 20); 5588112fe045Smaybee /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 55892ec99e3eSMatthew Ahrens if (allmem >= 1 << 30) 55902ec99e3eSMatthew Ahrens arc_c_max = allmem - (1 << 30); 5591fa9e4066Sahrens else 559244cb6abcSbmc arc_c_max = arc_c_min; 55932ec99e3eSMatthew Ahrens arc_c_max = MAX(allmem * 3 / 4, arc_c_max); 5594a2eea2e1Sahrens 55958fe00bfbSMatthew Ahrens /* 55968fe00bfbSMatthew Ahrens * In userland, there's only the memory pressure that we artificially 55978fe00bfbSMatthew Ahrens * create (see arc_available_memory()). Don't let arc_c get too 55988fe00bfbSMatthew Ahrens * small, because it can cause transactions to be larger than 55998fe00bfbSMatthew Ahrens * arc_c, causing arc_tempreserve_space() to fail. 56008fe00bfbSMatthew Ahrens */ 56018fe00bfbSMatthew Ahrens #ifndef _KERNEL 56028fe00bfbSMatthew Ahrens arc_c_min = arc_c_max / 2; 56038fe00bfbSMatthew Ahrens #endif 56048fe00bfbSMatthew Ahrens 5605a2eea2e1Sahrens /* 5606a2eea2e1Sahrens * Allow the tunables to override our calculations if they are 5607a2eea2e1Sahrens * reasonable (ie. over 64MB) 5608a2eea2e1Sahrens */ 56092ec99e3eSMatthew Ahrens if (zfs_arc_max > 64 << 20 && zfs_arc_max < allmem) 561044cb6abcSbmc arc_c_max = zfs_arc_max; 56112ec99e3eSMatthew Ahrens if (zfs_arc_min > 64 << 20 && zfs_arc_min <= arc_c_max) 561244cb6abcSbmc arc_c_min = zfs_arc_min; 5613a2eea2e1Sahrens 561444cb6abcSbmc arc_c = arc_c_max; 561544cb6abcSbmc arc_p = (arc_c >> 1); 5616*dcbf3bd6SGeorge Wilson arc_size = 0; 5617fa9e4066Sahrens 56180e8c6158Smaybee /* limit meta-data to 1/4 of the arc capacity */ 56190e8c6158Smaybee arc_meta_limit = arc_c_max / 4; 56201116048bSek 5621af868f46SMatthew Ahrens #ifdef _KERNEL 5622af868f46SMatthew Ahrens /* 5623af868f46SMatthew Ahrens * Metadata is stored in the kernel's heap. Don't let us 5624af868f46SMatthew Ahrens * use more than half the heap for the ARC. 5625af868f46SMatthew Ahrens */ 5626af868f46SMatthew Ahrens arc_meta_limit = MIN(arc_meta_limit, 5627af868f46SMatthew Ahrens vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 2); 5628af868f46SMatthew Ahrens #endif 5629af868f46SMatthew Ahrens 56301116048bSek /* Allow the tunable to override if it is reasonable */ 56311116048bSek if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 56321116048bSek arc_meta_limit = zfs_arc_meta_limit; 56331116048bSek 56340e8c6158Smaybee if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 56350e8c6158Smaybee arc_c_min = arc_meta_limit / 2; 56360e8c6158Smaybee 56373a5286a1SMatthew Ahrens if (zfs_arc_meta_min > 0) { 56383a5286a1SMatthew Ahrens arc_meta_min = zfs_arc_meta_min; 56393a5286a1SMatthew Ahrens } else { 56403a5286a1SMatthew Ahrens arc_meta_min = arc_c_min / 2; 56413a5286a1SMatthew Ahrens } 56423a5286a1SMatthew Ahrens 56435a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_grow_retry > 0) 56445a98e54bSBrendan Gregg - Sun Microsystems arc_grow_retry = zfs_arc_grow_retry; 56455a98e54bSBrendan Gregg - Sun Microsystems 56465a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_shrink_shift > 0) 56475a98e54bSBrendan Gregg - Sun Microsystems arc_shrink_shift = zfs_arc_shrink_shift; 56485a98e54bSBrendan Gregg - Sun Microsystems 56492ec99e3eSMatthew Ahrens /* 56502ec99e3eSMatthew Ahrens * Ensure that arc_no_grow_shift is less than arc_shrink_shift. 56512ec99e3eSMatthew Ahrens */ 56522ec99e3eSMatthew Ahrens if (arc_no_grow_shift >= arc_shrink_shift) 56532ec99e3eSMatthew Ahrens arc_no_grow_shift = arc_shrink_shift - 1; 56542ec99e3eSMatthew Ahrens 56555a98e54bSBrendan Gregg - Sun Microsystems if (zfs_arc_p_min_shift > 0) 56565a98e54bSBrendan Gregg - Sun Microsystems arc_p_min_shift = zfs_arc_p_min_shift; 56575a98e54bSBrendan Gregg - Sun Microsystems 5658244781f1SPrakash Surya if (zfs_arc_num_sublists_per_state < 1) 5659244781f1SPrakash Surya zfs_arc_num_sublists_per_state = MAX(boot_ncpus, 1); 5660244781f1SPrakash Surya 5661fa9e4066Sahrens /* if kmem_flags are set, lets try to use less memory */ 5662fa9e4066Sahrens if (kmem_debugging()) 566344cb6abcSbmc arc_c = arc_c / 2; 566444cb6abcSbmc if (arc_c < arc_c_min) 566544cb6abcSbmc arc_c = arc_c_min; 566644cb6abcSbmc 5667*dcbf3bd6SGeorge Wilson arc_state_init(); 5668fa9e4066Sahrens buf_init(); 5669fa9e4066Sahrens 5670*dcbf3bd6SGeorge Wilson arc_reclaim_thread_exit = B_FALSE; 5671fa9e4066Sahrens 567244cb6abcSbmc arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 567344cb6abcSbmc sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 567444cb6abcSbmc 567544cb6abcSbmc if (arc_ksp != NULL) { 567644cb6abcSbmc arc_ksp->ks_data = &arc_stats; 56774076b1bfSPrakash Surya arc_ksp->ks_update = arc_kstat_update; 567844cb6abcSbmc kstat_install(arc_ksp); 567944cb6abcSbmc } 568044cb6abcSbmc 5681fa9e4066Sahrens (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 5682fa9e4066Sahrens TS_RUN, minclsyspri); 568349e3519aSmaybee 5684*dcbf3bd6SGeorge Wilson arc_dead = B_FALSE; 56853a737e0dSbrendan arc_warm = B_FALSE; 56861ab7f2deSmaybee 568769962b56SMatthew Ahrens /* 568869962b56SMatthew Ahrens * Calculate maximum amount of dirty data per pool. 568969962b56SMatthew Ahrens * 569069962b56SMatthew Ahrens * If it has been set by /etc/system, take that. 569169962b56SMatthew Ahrens * Otherwise, use a percentage of physical memory defined by 569269962b56SMatthew Ahrens * zfs_dirty_data_max_percent (default 10%) with a cap at 569369962b56SMatthew Ahrens * zfs_dirty_data_max_max (default 4GB). 569469962b56SMatthew Ahrens */ 569569962b56SMatthew Ahrens if (zfs_dirty_data_max == 0) { 569669962b56SMatthew Ahrens zfs_dirty_data_max = physmem * PAGESIZE * 569769962b56SMatthew Ahrens zfs_dirty_data_max_percent / 100; 569869962b56SMatthew Ahrens zfs_dirty_data_max = MIN(zfs_dirty_data_max, 569969962b56SMatthew Ahrens zfs_dirty_data_max_max); 570069962b56SMatthew Ahrens } 5701fa9e4066Sahrens } 5702fa9e4066Sahrens 5703fa9e4066Sahrens void 5704fa9e4066Sahrens arc_fini(void) 5705fa9e4066Sahrens { 5706244781f1SPrakash Surya mutex_enter(&arc_reclaim_lock); 5707*dcbf3bd6SGeorge Wilson arc_reclaim_thread_exit = B_TRUE; 5708244781f1SPrakash Surya /* 5709244781f1SPrakash Surya * The reclaim thread will set arc_reclaim_thread_exit back to 5710*dcbf3bd6SGeorge Wilson * B_FALSE when it is finished exiting; we're waiting for that. 5711244781f1SPrakash Surya */ 5712244781f1SPrakash Surya while (arc_reclaim_thread_exit) { 5713244781f1SPrakash Surya cv_signal(&arc_reclaim_thread_cv); 5714244781f1SPrakash Surya cv_wait(&arc_reclaim_thread_cv, &arc_reclaim_lock); 5715244781f1SPrakash Surya } 5716244781f1SPrakash Surya mutex_exit(&arc_reclaim_lock); 5717fa9e4066Sahrens 5718*dcbf3bd6SGeorge Wilson /* Use B_TRUE to ensure *all* buffers are evicted */ 5719*dcbf3bd6SGeorge Wilson arc_flush(NULL, B_TRUE); 5720fa9e4066Sahrens 5721*dcbf3bd6SGeorge Wilson arc_dead = B_TRUE; 5722fa9e4066Sahrens 572344cb6abcSbmc if (arc_ksp != NULL) { 572444cb6abcSbmc kstat_delete(arc_ksp); 572544cb6abcSbmc arc_ksp = NULL; 572644cb6abcSbmc } 572744cb6abcSbmc 5728244781f1SPrakash Surya mutex_destroy(&arc_reclaim_lock); 5729244781f1SPrakash Surya cv_destroy(&arc_reclaim_thread_cv); 5730244781f1SPrakash Surya cv_destroy(&arc_reclaim_waiters_cv); 5731244781f1SPrakash Surya 5732*dcbf3bd6SGeorge Wilson arc_state_fini(); 5733fa9e4066Sahrens buf_fini(); 57342fdbea25SAleksandr Guzovskiy 573589c86e32SChris Williamson ASSERT0(arc_loaned_bytes); 5736fa9e4066Sahrens } 5737fa94a07fSbrendan 5738fa94a07fSbrendan /* 5739fa94a07fSbrendan * Level 2 ARC 5740fa94a07fSbrendan * 5741fa94a07fSbrendan * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 5742fa94a07fSbrendan * It uses dedicated storage devices to hold cached data, which are populated 5743fa94a07fSbrendan * using large infrequent writes. The main role of this cache is to boost 5744fa94a07fSbrendan * the performance of random read workloads. The intended L2ARC devices 5745fa94a07fSbrendan * include short-stroked disks, solid state disks, and other media with 5746fa94a07fSbrendan * substantially faster read latency than disk. 5747fa94a07fSbrendan * 5748fa94a07fSbrendan * +-----------------------+ 5749fa94a07fSbrendan * | ARC | 5750fa94a07fSbrendan * +-----------------------+ 5751fa94a07fSbrendan * | ^ ^ 5752fa94a07fSbrendan * | | | 5753fa94a07fSbrendan * l2arc_feed_thread() arc_read() 5754fa94a07fSbrendan * | | | 5755fa94a07fSbrendan * | l2arc read | 5756fa94a07fSbrendan * V | | 5757fa94a07fSbrendan * +---------------+ | 5758fa94a07fSbrendan * | L2ARC | | 5759fa94a07fSbrendan * +---------------+ | 5760fa94a07fSbrendan * | ^ | 5761fa94a07fSbrendan * l2arc_write() | | 5762fa94a07fSbrendan * | | | 5763fa94a07fSbrendan * V | | 5764fa94a07fSbrendan * +-------+ +-------+ 5765fa94a07fSbrendan * | vdev | | vdev | 5766fa94a07fSbrendan * | cache | | cache | 5767fa94a07fSbrendan * +-------+ +-------+ 5768fa94a07fSbrendan * +=========+ .-----. 5769fa94a07fSbrendan * : L2ARC : |-_____-| 5770fa94a07fSbrendan * : devices : | Disks | 5771fa94a07fSbrendan * +=========+ `-_____-' 5772fa94a07fSbrendan * 5773fa94a07fSbrendan * Read requests are satisfied from the following sources, in order: 5774fa94a07fSbrendan * 5775fa94a07fSbrendan * 1) ARC 5776fa94a07fSbrendan * 2) vdev cache of L2ARC devices 5777fa94a07fSbrendan * 3) L2ARC devices 5778fa94a07fSbrendan * 4) vdev cache of disks 5779fa94a07fSbrendan * 5) disks 5780fa94a07fSbrendan * 5781fa94a07fSbrendan * Some L2ARC device types exhibit extremely slow write performance. 5782fa94a07fSbrendan * To accommodate for this there are some significant differences between 5783fa94a07fSbrendan * the L2ARC and traditional cache design: 5784fa94a07fSbrendan * 5785fa94a07fSbrendan * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 5786fa94a07fSbrendan * the ARC behave as usual, freeing buffers and placing headers on ghost 5787fa94a07fSbrendan * lists. The ARC does not send buffers to the L2ARC during eviction as 5788fa94a07fSbrendan * this would add inflated write latencies for all ARC memory pressure. 5789fa94a07fSbrendan * 5790fa94a07fSbrendan * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 5791fa94a07fSbrendan * It does this by periodically scanning buffers from the eviction-end of 5792fa94a07fSbrendan * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 5793aad02571SSaso Kiselkov * not already there. It scans until a headroom of buffers is satisfied, 5794aad02571SSaso Kiselkov * which itself is a buffer for ARC eviction. If a compressible buffer is 5795aad02571SSaso Kiselkov * found during scanning and selected for writing to an L2ARC device, we 5796aad02571SSaso Kiselkov * temporarily boost scanning headroom during the next scan cycle to make 5797aad02571SSaso Kiselkov * sure we adapt to compression effects (which might significantly reduce 5798aad02571SSaso Kiselkov * the data volume we write to L2ARC). The thread that does this is 5799fa94a07fSbrendan * l2arc_feed_thread(), illustrated below; example sizes are included to 5800fa94a07fSbrendan * provide a better sense of ratio than this diagram: 5801fa94a07fSbrendan * 5802fa94a07fSbrendan * head --> tail 5803fa94a07fSbrendan * +---------------------+----------+ 5804fa94a07fSbrendan * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 5805fa94a07fSbrendan * +---------------------+----------+ | o L2ARC eligible 5806fa94a07fSbrendan * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 5807fa94a07fSbrendan * +---------------------+----------+ | 5808fa94a07fSbrendan * 15.9 Gbytes ^ 32 Mbytes | 5809fa94a07fSbrendan * headroom | 5810fa94a07fSbrendan * l2arc_feed_thread() 5811fa94a07fSbrendan * | 5812fa94a07fSbrendan * l2arc write hand <--[oooo]--' 5813fa94a07fSbrendan * | 8 Mbyte 5814fa94a07fSbrendan * | write max 5815fa94a07fSbrendan * V 5816fa94a07fSbrendan * +==============================+ 5817fa94a07fSbrendan * L2ARC dev |####|#|###|###| |####| ... | 5818fa94a07fSbrendan * +==============================+ 5819fa94a07fSbrendan * 32 Gbytes 5820fa94a07fSbrendan * 5821fa94a07fSbrendan * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 5822fa94a07fSbrendan * evicted, then the L2ARC has cached a buffer much sooner than it probably 5823fa94a07fSbrendan * needed to, potentially wasting L2ARC device bandwidth and storage. It is 5824fa94a07fSbrendan * safe to say that this is an uncommon case, since buffers at the end of 5825fa94a07fSbrendan * the ARC lists have moved there due to inactivity. 5826fa94a07fSbrendan * 5827fa94a07fSbrendan * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 5828fa94a07fSbrendan * then the L2ARC simply misses copying some buffers. This serves as a 5829fa94a07fSbrendan * pressure valve to prevent heavy read workloads from both stalling the ARC 5830fa94a07fSbrendan * with waits and clogging the L2ARC with writes. This also helps prevent 5831fa94a07fSbrendan * the potential for the L2ARC to churn if it attempts to cache content too 5832fa94a07fSbrendan * quickly, such as during backups of the entire pool. 5833fa94a07fSbrendan * 58343a737e0dSbrendan * 5. After system boot and before the ARC has filled main memory, there are 58353a737e0dSbrendan * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 58363a737e0dSbrendan * lists can remain mostly static. Instead of searching from tail of these 58373a737e0dSbrendan * lists as pictured, the l2arc_feed_thread() will search from the list heads 58383a737e0dSbrendan * for eligible buffers, greatly increasing its chance of finding them. 58393a737e0dSbrendan * 58403a737e0dSbrendan * The L2ARC device write speed is also boosted during this time so that 58413a737e0dSbrendan * the L2ARC warms up faster. Since there have been no ARC evictions yet, 58423a737e0dSbrendan * there are no L2ARC reads, and no fear of degrading read performance 58433a737e0dSbrendan * through increased writes. 58443a737e0dSbrendan * 58453a737e0dSbrendan * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 5846fa94a07fSbrendan * the vdev queue can aggregate them into larger and fewer writes. Each 5847fa94a07fSbrendan * device is written to in a rotor fashion, sweeping writes through 5848fa94a07fSbrendan * available space then repeating. 5849fa94a07fSbrendan * 58503a737e0dSbrendan * 7. The L2ARC does not store dirty content. It never needs to flush 5851fa94a07fSbrendan * write buffers back to disk based storage. 5852fa94a07fSbrendan * 58533a737e0dSbrendan * 8. If an ARC buffer is written (and dirtied) which also exists in the 5854fa94a07fSbrendan * L2ARC, the now stale L2ARC buffer is immediately dropped. 5855fa94a07fSbrendan * 5856fa94a07fSbrendan * The performance of the L2ARC can be tweaked by a number of tunables, which 5857fa94a07fSbrendan * may be necessary for different workloads: 5858fa94a07fSbrendan * 5859fa94a07fSbrendan * l2arc_write_max max write bytes per interval 58603a737e0dSbrendan * l2arc_write_boost extra write bytes during device warmup 5861fa94a07fSbrendan * l2arc_noprefetch skip caching prefetched buffers 5862fa94a07fSbrendan * l2arc_headroom number of max device writes to precache 5863aad02571SSaso Kiselkov * l2arc_headroom_boost when we find compressed buffers during ARC 5864aad02571SSaso Kiselkov * scanning, we multiply headroom by this 5865aad02571SSaso Kiselkov * percentage factor for the next scan cycle, 5866aad02571SSaso Kiselkov * since more compressed buffers are likely to 5867aad02571SSaso Kiselkov * be present 5868fa94a07fSbrendan * l2arc_feed_secs seconds between L2ARC writing 5869fa94a07fSbrendan * 5870fa94a07fSbrendan * Tunables may be removed or added as future performance improvements are 5871fa94a07fSbrendan * integrated, and also may become zpool properties. 58725a98e54bSBrendan Gregg - Sun Microsystems * 58735a98e54bSBrendan Gregg - Sun Microsystems * There are three key functions that control how the L2ARC warms up: 58745a98e54bSBrendan Gregg - Sun Microsystems * 58755a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_eligible() check if a buffer is eligible to cache 58765a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_size() calculate how much to write 58775a98e54bSBrendan Gregg - Sun Microsystems * l2arc_write_interval() calculate sleep delay between writes 58785a98e54bSBrendan Gregg - Sun Microsystems * 58795a98e54bSBrendan Gregg - Sun Microsystems * These three functions determine what to write, how much, and how quickly 58805a98e54bSBrendan Gregg - Sun Microsystems * to send writes. 5881fa94a07fSbrendan */ 5882fa94a07fSbrendan 58835a98e54bSBrendan Gregg - Sun Microsystems static boolean_t 58847adb730bSGeorge Wilson l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) 58855a98e54bSBrendan Gregg - Sun Microsystems { 58865a98e54bSBrendan Gregg - Sun Microsystems /* 58875a98e54bSBrendan Gregg - Sun Microsystems * A buffer is *not* eligible for the L2ARC if it: 58885a98e54bSBrendan Gregg - Sun Microsystems * 1. belongs to a different spa. 58895ea40c06SBrendan Gregg - Sun Microsystems * 2. is already cached on the L2ARC. 58905ea40c06SBrendan Gregg - Sun Microsystems * 3. has an I/O in progress (it may be an incomplete read). 58915ea40c06SBrendan Gregg - Sun Microsystems * 4. is flagged not eligible (zfs property). 58925a98e54bSBrendan Gregg - Sun Microsystems */ 589389c86e32SChris Williamson if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) || 58947adb730bSGeorge Wilson HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr)) 58955a98e54bSBrendan Gregg - Sun Microsystems return (B_FALSE); 58965a98e54bSBrendan Gregg - Sun Microsystems 58975a98e54bSBrendan Gregg - Sun Microsystems return (B_TRUE); 58985a98e54bSBrendan Gregg - Sun Microsystems } 58995a98e54bSBrendan Gregg - Sun Microsystems 59005a98e54bSBrendan Gregg - Sun Microsystems static uint64_t 5901aad02571SSaso Kiselkov l2arc_write_size(void) 59025a98e54bSBrendan Gregg - Sun Microsystems { 59035a98e54bSBrendan Gregg - Sun Microsystems uint64_t size; 59045a98e54bSBrendan Gregg - Sun Microsystems 5905aad02571SSaso Kiselkov /* 5906aad02571SSaso Kiselkov * Make sure our globals have meaningful values in case the user 5907aad02571SSaso Kiselkov * altered them. 5908aad02571SSaso Kiselkov */ 5909aad02571SSaso Kiselkov size = l2arc_write_max; 5910aad02571SSaso Kiselkov if (size == 0) { 5911aad02571SSaso Kiselkov cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 5912aad02571SSaso Kiselkov "be greater than zero, resetting it to the default (%d)", 5913aad02571SSaso Kiselkov L2ARC_WRITE_SIZE); 5914aad02571SSaso Kiselkov size = l2arc_write_max = L2ARC_WRITE_SIZE; 5915aad02571SSaso Kiselkov } 59165a98e54bSBrendan Gregg - Sun Microsystems 59175a98e54bSBrendan Gregg - Sun Microsystems if (arc_warm == B_FALSE) 5918aad02571SSaso Kiselkov size += l2arc_write_boost; 59195a98e54bSBrendan Gregg - Sun Microsystems 59205a98e54bSBrendan Gregg - Sun Microsystems return (size); 59215a98e54bSBrendan Gregg - Sun Microsystems 59225a98e54bSBrendan Gregg - Sun Microsystems } 59235a98e54bSBrendan Gregg - Sun Microsystems 59245a98e54bSBrendan Gregg - Sun Microsystems static clock_t 59255a98e54bSBrendan Gregg - Sun Microsystems l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 59265a98e54bSBrendan Gregg - Sun Microsystems { 5927d3d50737SRafael Vanoni clock_t interval, next, now; 59285a98e54bSBrendan Gregg - Sun Microsystems 59295a98e54bSBrendan Gregg - Sun Microsystems /* 59305a98e54bSBrendan Gregg - Sun Microsystems * If the ARC lists are busy, increase our write rate; if the 59315a98e54bSBrendan Gregg - Sun Microsystems * lists are stale, idle back. This is achieved by checking 59325a98e54bSBrendan Gregg - Sun Microsystems * how much we previously wrote - if it was more than half of 59335a98e54bSBrendan Gregg - Sun Microsystems * what we wanted, schedule the next write much sooner. 59345a98e54bSBrendan Gregg - Sun Microsystems */ 59355a98e54bSBrendan Gregg - Sun Microsystems if (l2arc_feed_again && wrote > (wanted / 2)) 59365a98e54bSBrendan Gregg - Sun Microsystems interval = (hz * l2arc_feed_min_ms) / 1000; 59375a98e54bSBrendan Gregg - Sun Microsystems else 59385a98e54bSBrendan Gregg - Sun Microsystems interval = hz * l2arc_feed_secs; 59395a98e54bSBrendan Gregg - Sun Microsystems 5940d3d50737SRafael Vanoni now = ddi_get_lbolt(); 5941d3d50737SRafael Vanoni next = MAX(now, MIN(now + interval, began + interval)); 59425a98e54bSBrendan Gregg - Sun Microsystems 59435a98e54bSBrendan Gregg - Sun Microsystems return (next); 59445a98e54bSBrendan Gregg - Sun Microsystems } 59455a98e54bSBrendan Gregg - Sun Microsystems 5946fa94a07fSbrendan /* 5947fa94a07fSbrendan * Cycle through L2ARC devices. This is how L2ARC load balances. 59483a737e0dSbrendan * If a device is returned, this also returns holding the spa config lock. 5949fa94a07fSbrendan */ 5950fa94a07fSbrendan static l2arc_dev_t * 5951fa94a07fSbrendan l2arc_dev_get_next(void) 5952fa94a07fSbrendan { 59533a737e0dSbrendan l2arc_dev_t *first, *next = NULL; 59543a737e0dSbrendan 59553a737e0dSbrendan /* 59563a737e0dSbrendan * Lock out the removal of spas (spa_namespace_lock), then removal 59573a737e0dSbrendan * of cache devices (l2arc_dev_mtx). Once a device has been selected, 59583a737e0dSbrendan * both locks will be dropped and a spa config lock held instead. 59593a737e0dSbrendan */ 59603a737e0dSbrendan mutex_enter(&spa_namespace_lock); 59613a737e0dSbrendan mutex_enter(&l2arc_dev_mtx); 5962fa94a07fSbrendan 5963c5904d13Seschrock /* if there are no vdevs, there is nothing to do */ 5964c5904d13Seschrock if (l2arc_ndev == 0) 59653a737e0dSbrendan goto out; 5966c5904d13Seschrock 5967c5904d13Seschrock first = NULL; 5968c5904d13Seschrock next = l2arc_dev_last; 5969c5904d13Seschrock do { 5970c5904d13Seschrock /* loop around the list looking for a non-faulted vdev */ 5971c5904d13Seschrock if (next == NULL) { 5972fa94a07fSbrendan next = list_head(l2arc_dev_list); 5973c5904d13Seschrock } else { 5974c5904d13Seschrock next = list_next(l2arc_dev_list, next); 5975c5904d13Seschrock if (next == NULL) 5976c5904d13Seschrock next = list_head(l2arc_dev_list); 5977c5904d13Seschrock } 5978c5904d13Seschrock 5979c5904d13Seschrock /* if we have come back to the start, bail out */ 5980c5904d13Seschrock if (first == NULL) 5981c5904d13Seschrock first = next; 5982c5904d13Seschrock else if (next == first) 5983c5904d13Seschrock break; 5984c5904d13Seschrock 5985c5904d13Seschrock } while (vdev_is_dead(next->l2ad_vdev)); 5986c5904d13Seschrock 5987c5904d13Seschrock /* if we were unable to find any usable vdevs, return NULL */ 5988c5904d13Seschrock if (vdev_is_dead(next->l2ad_vdev)) 59893a737e0dSbrendan next = NULL; 5990fa94a07fSbrendan 5991fa94a07fSbrendan l2arc_dev_last = next; 5992fa94a07fSbrendan 59933a737e0dSbrendan out: 59943a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 59953a737e0dSbrendan 59963a737e0dSbrendan /* 59973a737e0dSbrendan * Grab the config lock to prevent the 'next' device from being 59983a737e0dSbrendan * removed while we are writing to it. 59993a737e0dSbrendan */ 60003a737e0dSbrendan if (next != NULL) 6001e14bb325SJeff Bonwick spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 60023a737e0dSbrendan mutex_exit(&spa_namespace_lock); 60033a737e0dSbrendan 6004fa94a07fSbrendan return (next); 6005fa94a07fSbrendan } 6006fa94a07fSbrendan 60073a737e0dSbrendan /* 60083a737e0dSbrendan * Free buffers that were tagged for destruction. 60093a737e0dSbrendan */ 60103a737e0dSbrendan static void 60113a737e0dSbrendan l2arc_do_free_on_write() 60123a737e0dSbrendan { 60133a737e0dSbrendan list_t *buflist; 60143a737e0dSbrendan l2arc_data_free_t *df, *df_prev; 60153a737e0dSbrendan 60163a737e0dSbrendan mutex_enter(&l2arc_free_on_write_mtx); 60173a737e0dSbrendan buflist = l2arc_free_on_write; 60183a737e0dSbrendan 60193a737e0dSbrendan for (df = list_tail(buflist); df; df = df_prev) { 60203a737e0dSbrendan df_prev = list_prev(buflist, df); 6021*dcbf3bd6SGeorge Wilson ASSERT3P(df->l2df_data, !=, NULL); 6022*dcbf3bd6SGeorge Wilson if (df->l2df_type == ARC_BUFC_METADATA) { 6023*dcbf3bd6SGeorge Wilson zio_buf_free(df->l2df_data, df->l2df_size); 6024*dcbf3bd6SGeorge Wilson } else { 6025*dcbf3bd6SGeorge Wilson ASSERT(df->l2df_type == ARC_BUFC_DATA); 6026*dcbf3bd6SGeorge Wilson zio_data_buf_free(df->l2df_data, df->l2df_size); 6027*dcbf3bd6SGeorge Wilson } 60283a737e0dSbrendan list_remove(buflist, df); 60293a737e0dSbrendan kmem_free(df, sizeof (l2arc_data_free_t)); 60303a737e0dSbrendan } 60313a737e0dSbrendan 60323a737e0dSbrendan mutex_exit(&l2arc_free_on_write_mtx); 60333a737e0dSbrendan } 60343a737e0dSbrendan 6035fa94a07fSbrendan /* 6036fa94a07fSbrendan * A write to a cache device has completed. Update all headers to allow 6037fa94a07fSbrendan * reads from these buffers to begin. 6038fa94a07fSbrendan */ 6039fa94a07fSbrendan static void 6040fa94a07fSbrendan l2arc_write_done(zio_t *zio) 6041fa94a07fSbrendan { 6042fa94a07fSbrendan l2arc_write_callback_t *cb; 6043fa94a07fSbrendan l2arc_dev_t *dev; 6044fa94a07fSbrendan list_t *buflist; 60457adb730bSGeorge Wilson arc_buf_hdr_t *head, *hdr, *hdr_prev; 6046fa94a07fSbrendan kmutex_t *hash_lock; 60473038a2b4SSaso Kiselkov int64_t bytes_dropped = 0; 6048fa94a07fSbrendan 6049fa94a07fSbrendan cb = zio->io_private; 6050*dcbf3bd6SGeorge Wilson ASSERT3P(cb, !=, NULL); 6051fa94a07fSbrendan dev = cb->l2wcb_dev; 6052*dcbf3bd6SGeorge Wilson ASSERT3P(dev, !=, NULL); 6053fa94a07fSbrendan head = cb->l2wcb_head; 6054*dcbf3bd6SGeorge Wilson ASSERT3P(head, !=, NULL); 605589c86e32SChris Williamson buflist = &dev->l2ad_buflist; 6056*dcbf3bd6SGeorge Wilson ASSERT3P(buflist, !=, NULL); 6057fa94a07fSbrendan DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 6058fa94a07fSbrendan l2arc_write_callback_t *, cb); 6059fa94a07fSbrendan 6060fa94a07fSbrendan if (zio->io_error != 0) 6061fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_writes_error); 6062fa94a07fSbrendan 6063fa94a07fSbrendan /* 6064fa94a07fSbrendan * All writes completed, or an error was hit. 6065fa94a07fSbrendan */ 6066244781f1SPrakash Surya top: 6067244781f1SPrakash Surya mutex_enter(&dev->l2ad_mtx); 60687adb730bSGeorge Wilson for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { 60697adb730bSGeorge Wilson hdr_prev = list_prev(buflist, hdr); 6070fa94a07fSbrendan 60717adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 6072244781f1SPrakash Surya 6073244781f1SPrakash Surya /* 6074244781f1SPrakash Surya * We cannot use mutex_enter or else we can deadlock 6075244781f1SPrakash Surya * with l2arc_write_buffers (due to swapping the order 6076244781f1SPrakash Surya * the hash lock and l2ad_mtx are taken). 6077244781f1SPrakash Surya */ 6078fa94a07fSbrendan if (!mutex_tryenter(hash_lock)) { 6079fa94a07fSbrendan /* 6080244781f1SPrakash Surya * Missed the hash lock. We must retry so we 6081244781f1SPrakash Surya * don't leave the ARC_FLAG_L2_WRITING bit set. 6082fa94a07fSbrendan */ 6083244781f1SPrakash Surya ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); 6084244781f1SPrakash Surya 6085244781f1SPrakash Surya /* 6086244781f1SPrakash Surya * We don't want to rescan the headers we've 6087244781f1SPrakash Surya * already marked as having been written out, so 6088244781f1SPrakash Surya * we reinsert the head node so we can pick up 6089244781f1SPrakash Surya * where we left off. 6090244781f1SPrakash Surya */ 6091244781f1SPrakash Surya list_remove(buflist, head); 6092244781f1SPrakash Surya list_insert_after(buflist, hdr, head); 6093244781f1SPrakash Surya 6094244781f1SPrakash Surya mutex_exit(&dev->l2ad_mtx); 6095244781f1SPrakash Surya 6096244781f1SPrakash Surya /* 6097244781f1SPrakash Surya * We wait for the hash lock to become available 6098244781f1SPrakash Surya * to try and prevent busy waiting, and increase 6099244781f1SPrakash Surya * the chance we'll be able to acquire the lock 6100244781f1SPrakash Surya * the next time around. 6101244781f1SPrakash Surya */ 6102244781f1SPrakash Surya mutex_enter(hash_lock); 6103244781f1SPrakash Surya mutex_exit(hash_lock); 6104244781f1SPrakash Surya goto top; 6105fa94a07fSbrendan } 6106fa94a07fSbrendan 610789c86e32SChris Williamson /* 6108244781f1SPrakash Surya * We could not have been moved into the arc_l2c_only 6109244781f1SPrakash Surya * state while in-flight due to our ARC_FLAG_L2_WRITING 6110244781f1SPrakash Surya * bit being set. Let's just ensure that's being enforced. 6111244781f1SPrakash Surya */ 6112244781f1SPrakash Surya ASSERT(HDR_HAS_L1HDR(hdr)); 6113244781f1SPrakash Surya 6114fa94a07fSbrendan if (zio->io_error != 0) { 6115fa94a07fSbrendan /* 61163a737e0dSbrendan * Error - drop L2ARC entry. 6117fa94a07fSbrendan */ 61187adb730bSGeorge Wilson list_remove(buflist, hdr); 6119*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); 612089c86e32SChris Williamson 6121*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_l2_asize, -arc_hdr_size(hdr)); 6122*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_l2_size, -HDR_GET_LSIZE(hdr)); 6123a52fc310SPrakash Surya 6124*dcbf3bd6SGeorge Wilson bytes_dropped += arc_hdr_size(hdr); 6125a52fc310SPrakash Surya (void) refcount_remove_many(&dev->l2ad_alloc, 6126*dcbf3bd6SGeorge Wilson arc_hdr_size(hdr), hdr); 6127fa94a07fSbrendan } 6128fa94a07fSbrendan 6129fa94a07fSbrendan /* 6130244781f1SPrakash Surya * Allow ARC to begin reads and ghost list evictions to 6131244781f1SPrakash Surya * this L2ARC entry. 6132fa94a07fSbrendan */ 6133*dcbf3bd6SGeorge Wilson arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING); 6134fa94a07fSbrendan 6135fa94a07fSbrendan mutex_exit(hash_lock); 6136fa94a07fSbrendan } 6137fa94a07fSbrendan 6138fa94a07fSbrendan atomic_inc_64(&l2arc_writes_done); 6139fa94a07fSbrendan list_remove(buflist, head); 614089c86e32SChris Williamson ASSERT(!HDR_HAS_L1HDR(head)); 614189c86e32SChris Williamson kmem_cache_free(hdr_l2only_cache, head); 614289c86e32SChris Williamson mutex_exit(&dev->l2ad_mtx); 6143fa94a07fSbrendan 61443038a2b4SSaso Kiselkov vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 61453038a2b4SSaso Kiselkov 61463a737e0dSbrendan l2arc_do_free_on_write(); 6147fa94a07fSbrendan 6148fa94a07fSbrendan kmem_free(cb, sizeof (l2arc_write_callback_t)); 6149fa94a07fSbrendan } 6150fa94a07fSbrendan 6151fa94a07fSbrendan /* 6152fa94a07fSbrendan * A read to a cache device completed. Validate buffer contents before 6153fa94a07fSbrendan * handing over to the regular ARC routines. 6154fa94a07fSbrendan */ 6155fa94a07fSbrendan static void 6156fa94a07fSbrendan l2arc_read_done(zio_t *zio) 6157fa94a07fSbrendan { 6158fa94a07fSbrendan l2arc_read_callback_t *cb; 6159fa94a07fSbrendan arc_buf_hdr_t *hdr; 6160fa94a07fSbrendan kmutex_t *hash_lock; 6161*dcbf3bd6SGeorge Wilson boolean_t valid_cksum; 6162fa94a07fSbrendan 6163*dcbf3bd6SGeorge Wilson ASSERT3P(zio->io_vd, !=, NULL); 6164e14bb325SJeff Bonwick ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 6165e14bb325SJeff Bonwick 6166e14bb325SJeff Bonwick spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 6167e14bb325SJeff Bonwick 6168fa94a07fSbrendan cb = zio->io_private; 6169*dcbf3bd6SGeorge Wilson ASSERT3P(cb, !=, NULL); 6170*dcbf3bd6SGeorge Wilson hdr = cb->l2rcb_hdr; 6171*dcbf3bd6SGeorge Wilson ASSERT3P(hdr, !=, NULL); 6172fa94a07fSbrendan 6173*dcbf3bd6SGeorge Wilson hash_lock = HDR_LOCK(hdr); 6174fa94a07fSbrendan mutex_enter(hash_lock); 61753f9d6ad7SLin Ling ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 6176fa94a07fSbrendan 6177*dcbf3bd6SGeorge Wilson ASSERT3P(zio->io_data, !=, NULL); 6178aad02571SSaso Kiselkov 6179fa94a07fSbrendan /* 6180fa94a07fSbrendan * Check this survived the L2ARC journey. 6181fa94a07fSbrendan */ 6182*dcbf3bd6SGeorge Wilson ASSERT3P(zio->io_data, ==, hdr->b_l1hdr.b_pdata); 6183*dcbf3bd6SGeorge Wilson zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 6184*dcbf3bd6SGeorge Wilson zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 6185*dcbf3bd6SGeorge Wilson 6186*dcbf3bd6SGeorge Wilson valid_cksum = arc_cksum_is_equal(hdr, zio); 6187*dcbf3bd6SGeorge Wilson if (valid_cksum && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 6188fa94a07fSbrendan mutex_exit(hash_lock); 6189*dcbf3bd6SGeorge Wilson zio->io_private = hdr; 6190fa94a07fSbrendan arc_read_done(zio); 6191fa94a07fSbrendan } else { 6192fa94a07fSbrendan mutex_exit(hash_lock); 6193fa94a07fSbrendan /* 6194fa94a07fSbrendan * Buffer didn't survive caching. Increment stats and 6195fa94a07fSbrendan * reissue to the original storage device. 6196fa94a07fSbrendan */ 61973a737e0dSbrendan if (zio->io_error != 0) { 6198fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_io_error); 61993a737e0dSbrendan } else { 6200be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(EIO); 62013a737e0dSbrendan } 6202*dcbf3bd6SGeorge Wilson if (!valid_cksum) 6203fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_cksum_bad); 6204fa94a07fSbrendan 6205e14bb325SJeff Bonwick /* 6206e14bb325SJeff Bonwick * If there's no waiter, issue an async i/o to the primary 6207e14bb325SJeff Bonwick * storage now. If there *is* a waiter, the caller must 6208e14bb325SJeff Bonwick * issue the i/o in a context where it's OK to block. 6209e14bb325SJeff Bonwick */ 6210a3f829aeSBill Moore if (zio->io_waiter == NULL) { 6211a3f829aeSBill Moore zio_t *pio = zio_unique_parent(zio); 6212a3f829aeSBill Moore 6213a3f829aeSBill Moore ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 6214a3f829aeSBill Moore 6215*dcbf3bd6SGeorge Wilson zio_nowait(zio_read(pio, zio->io_spa, zio->io_bp, 6216*dcbf3bd6SGeorge Wilson hdr->b_l1hdr.b_pdata, zio->io_size, arc_read_done, 6217*dcbf3bd6SGeorge Wilson hdr, zio->io_priority, cb->l2rcb_flags, 6218*dcbf3bd6SGeorge Wilson &cb->l2rcb_zb)); 6219a3f829aeSBill Moore } 6220fa94a07fSbrendan } 6221fa94a07fSbrendan 6222fa94a07fSbrendan kmem_free(cb, sizeof (l2arc_read_callback_t)); 6223fa94a07fSbrendan } 6224fa94a07fSbrendan 6225fa94a07fSbrendan /* 6226fa94a07fSbrendan * This is the list priority from which the L2ARC will search for pages to 6227fa94a07fSbrendan * cache. This is used within loops (0..3) to cycle through lists in the 6228fa94a07fSbrendan * desired order. This order can have a significant effect on cache 6229fa94a07fSbrendan * performance. 6230fa94a07fSbrendan * 6231fa94a07fSbrendan * Currently the metadata lists are hit first, MFU then MRU, followed by 6232fa94a07fSbrendan * the data lists. This function returns a locked list, and also returns 6233fa94a07fSbrendan * the lock pointer. 6234fa94a07fSbrendan */ 6235244781f1SPrakash Surya static multilist_sublist_t * 6236244781f1SPrakash Surya l2arc_sublist_lock(int list_num) 6237fa94a07fSbrendan { 6238244781f1SPrakash Surya multilist_t *ml = NULL; 6239244781f1SPrakash Surya unsigned int idx; 6240fa94a07fSbrendan 6241fa94a07fSbrendan ASSERT(list_num >= 0 && list_num <= 3); 6242fa94a07fSbrendan 6243fa94a07fSbrendan switch (list_num) { 6244fa94a07fSbrendan case 0: 6245244781f1SPrakash Surya ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 6246fa94a07fSbrendan break; 6247fa94a07fSbrendan case 1: 6248244781f1SPrakash Surya ml = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 6249fa94a07fSbrendan break; 6250fa94a07fSbrendan case 2: 6251244781f1SPrakash Surya ml = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 6252fa94a07fSbrendan break; 6253fa94a07fSbrendan case 3: 6254244781f1SPrakash Surya ml = &arc_mru->arcs_list[ARC_BUFC_DATA]; 6255fa94a07fSbrendan break; 6256fa94a07fSbrendan } 6257fa94a07fSbrendan 6258244781f1SPrakash Surya /* 6259244781f1SPrakash Surya * Return a randomly-selected sublist. This is acceptable 6260244781f1SPrakash Surya * because the caller feeds only a little bit of data for each 6261244781f1SPrakash Surya * call (8MB). Subsequent calls will result in different 6262244781f1SPrakash Surya * sublists being selected. 6263244781f1SPrakash Surya */ 6264244781f1SPrakash Surya idx = multilist_get_random_index(ml); 6265244781f1SPrakash Surya return (multilist_sublist_lock(ml, idx)); 6266fa94a07fSbrendan } 6267fa94a07fSbrendan 6268fa94a07fSbrendan /* 6269fa94a07fSbrendan * Evict buffers from the device write hand to the distance specified in 6270fa94a07fSbrendan * bytes. This distance may span populated buffers, it may span nothing. 6271fa94a07fSbrendan * This is clearing a region on the L2ARC device ready for writing. 6272fa94a07fSbrendan * If the 'all' boolean is set, every buffer is evicted. 6273fa94a07fSbrendan */ 6274fa94a07fSbrendan static void 6275fa94a07fSbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 6276fa94a07fSbrendan { 6277fa94a07fSbrendan list_t *buflist; 62787adb730bSGeorge Wilson arc_buf_hdr_t *hdr, *hdr_prev; 6279fa94a07fSbrendan kmutex_t *hash_lock; 6280fa94a07fSbrendan uint64_t taddr; 6281fa94a07fSbrendan 628289c86e32SChris Williamson buflist = &dev->l2ad_buflist; 6283fa94a07fSbrendan 6284fa94a07fSbrendan if (!all && dev->l2ad_first) { 6285fa94a07fSbrendan /* 6286fa94a07fSbrendan * This is the first sweep through the device. There is 6287fa94a07fSbrendan * nothing to evict. 6288fa94a07fSbrendan */ 6289fa94a07fSbrendan return; 6290fa94a07fSbrendan } 6291fa94a07fSbrendan 62923a737e0dSbrendan if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 6293fa94a07fSbrendan /* 6294fa94a07fSbrendan * When nearing the end of the device, evict to the end 6295fa94a07fSbrendan * before the device write hand jumps to the start. 6296fa94a07fSbrendan */ 6297fa94a07fSbrendan taddr = dev->l2ad_end; 6298fa94a07fSbrendan } else { 6299fa94a07fSbrendan taddr = dev->l2ad_hand + distance; 6300fa94a07fSbrendan } 6301fa94a07fSbrendan DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 6302fa94a07fSbrendan uint64_t, taddr, boolean_t, all); 6303fa94a07fSbrendan 6304fa94a07fSbrendan top: 630589c86e32SChris Williamson mutex_enter(&dev->l2ad_mtx); 63067adb730bSGeorge Wilson for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { 63077adb730bSGeorge Wilson hdr_prev = list_prev(buflist, hdr); 6308fa94a07fSbrendan 63097adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 6310244781f1SPrakash Surya 6311244781f1SPrakash Surya /* 6312244781f1SPrakash Surya * We cannot use mutex_enter or else we can deadlock 6313244781f1SPrakash Surya * with l2arc_write_buffers (due to swapping the order 6314244781f1SPrakash Surya * the hash lock and l2ad_mtx are taken). 6315244781f1SPrakash Surya */ 6316fa94a07fSbrendan if (!mutex_tryenter(hash_lock)) { 6317fa94a07fSbrendan /* 6318fa94a07fSbrendan * Missed the hash lock. Retry. 6319fa94a07fSbrendan */ 6320fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 632189c86e32SChris Williamson mutex_exit(&dev->l2ad_mtx); 6322fa94a07fSbrendan mutex_enter(hash_lock); 6323fa94a07fSbrendan mutex_exit(hash_lock); 6324fa94a07fSbrendan goto top; 6325fa94a07fSbrendan } 6326fa94a07fSbrendan 63277adb730bSGeorge Wilson if (HDR_L2_WRITE_HEAD(hdr)) { 6328fa94a07fSbrendan /* 6329fa94a07fSbrendan * We hit a write head node. Leave it for 6330fa94a07fSbrendan * l2arc_write_done(). 6331fa94a07fSbrendan */ 63327adb730bSGeorge Wilson list_remove(buflist, hdr); 6333fa94a07fSbrendan mutex_exit(hash_lock); 6334fa94a07fSbrendan continue; 6335fa94a07fSbrendan } 6336fa94a07fSbrendan 633789c86e32SChris Williamson if (!all && HDR_HAS_L2HDR(hdr) && 633889c86e32SChris Williamson (hdr->b_l2hdr.b_daddr > taddr || 633989c86e32SChris Williamson hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { 6340fa94a07fSbrendan /* 6341fa94a07fSbrendan * We've evicted to the target address, 6342fa94a07fSbrendan * or the end of the device. 6343fa94a07fSbrendan */ 6344fa94a07fSbrendan mutex_exit(hash_lock); 6345fa94a07fSbrendan break; 6346fa94a07fSbrendan } 6347fa94a07fSbrendan 634889c86e32SChris Williamson ASSERT(HDR_HAS_L2HDR(hdr)); 634989c86e32SChris Williamson if (!HDR_HAS_L1HDR(hdr)) { 63507adb730bSGeorge Wilson ASSERT(!HDR_L2_READING(hdr)); 6351fa94a07fSbrendan /* 6352fa94a07fSbrendan * This doesn't exist in the ARC. Destroy. 6353fa94a07fSbrendan * arc_hdr_destroy() will call list_remove() 6354fa94a07fSbrendan * and decrement arcstat_l2_size. 6355fa94a07fSbrendan */ 63567adb730bSGeorge Wilson arc_change_state(arc_anon, hdr, hash_lock); 63577adb730bSGeorge Wilson arc_hdr_destroy(hdr); 6358fa94a07fSbrendan } else { 635989c86e32SChris Williamson ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); 636089c86e32SChris Williamson ARCSTAT_BUMP(arcstat_l2_evict_l1cached); 63613a737e0dSbrendan /* 63623a737e0dSbrendan * Invalidate issued or about to be issued 63633a737e0dSbrendan * reads, since we may be about to write 63643a737e0dSbrendan * over this location. 63653a737e0dSbrendan */ 63667adb730bSGeorge Wilson if (HDR_L2_READING(hdr)) { 63673a737e0dSbrendan ARCSTAT_BUMP(arcstat_l2_evict_reading); 6368*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED); 63693a737e0dSbrendan } 63703a737e0dSbrendan 6371244781f1SPrakash Surya /* Ensure this header has finished being written */ 6372244781f1SPrakash Surya ASSERT(!HDR_L2_WRITING(hdr)); 6373a52fc310SPrakash Surya 6374a52fc310SPrakash Surya arc_hdr_l2hdr_destroy(hdr); 6375fa94a07fSbrendan } 6376fa94a07fSbrendan mutex_exit(hash_lock); 6377fa94a07fSbrendan } 637889c86e32SChris Williamson mutex_exit(&dev->l2ad_mtx); 6379fa94a07fSbrendan } 6380fa94a07fSbrendan 6381fa94a07fSbrendan /* 6382fa94a07fSbrendan * Find and write ARC buffers to the L2ARC device. 6383fa94a07fSbrendan * 63847adb730bSGeorge Wilson * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid 6385fa94a07fSbrendan * for reading until they have completed writing. 6386aad02571SSaso Kiselkov * The headroom_boost is an in-out parameter used to maintain headroom boost 6387aad02571SSaso Kiselkov * state between calls to this function. 6388aad02571SSaso Kiselkov * 6389aad02571SSaso Kiselkov * Returns the number of bytes actually written (which may be smaller than 6390aad02571SSaso Kiselkov * the delta by which the device hand has changed due to alignment). 6391fa94a07fSbrendan */ 63925a98e54bSBrendan Gregg - Sun Microsystems static uint64_t 6393*dcbf3bd6SGeorge Wilson l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 6394fa94a07fSbrendan { 63957adb730bSGeorge Wilson arc_buf_hdr_t *hdr, *hdr_prev, *head; 6396*dcbf3bd6SGeorge Wilson uint64_t write_asize, write_psize, write_sz, headroom; 6397aad02571SSaso Kiselkov boolean_t full; 6398fa94a07fSbrendan l2arc_write_callback_t *cb; 6399fa94a07fSbrendan zio_t *pio, *wzio; 6400e9103aaeSGarrett D'Amore uint64_t guid = spa_load_guid(spa); 6401fa94a07fSbrendan 6402*dcbf3bd6SGeorge Wilson ASSERT3P(dev->l2ad_vdev, !=, NULL); 6403aad02571SSaso Kiselkov 6404fa94a07fSbrendan pio = NULL; 6405*dcbf3bd6SGeorge Wilson write_sz = write_asize = write_psize = 0; 6406fa94a07fSbrendan full = B_FALSE; 640789c86e32SChris Williamson head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); 6408*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR); 6409aad02571SSaso Kiselkov 6410fa94a07fSbrendan /* 6411fa94a07fSbrendan * Copy buffers for L2ARC writing. 6412fa94a07fSbrendan */ 6413fa94a07fSbrendan for (int try = 0; try <= 3; try++) { 6414244781f1SPrakash Surya multilist_sublist_t *mls = l2arc_sublist_lock(try); 6415aad02571SSaso Kiselkov uint64_t passed_sz = 0; 6416aad02571SSaso Kiselkov 64173a737e0dSbrendan /* 64183a737e0dSbrendan * L2ARC fast warmup. 64193a737e0dSbrendan * 64203a737e0dSbrendan * Until the ARC is warm and starts to evict, read from the 64213a737e0dSbrendan * head of the ARC lists rather than the tail. 64223a737e0dSbrendan */ 64233a737e0dSbrendan if (arc_warm == B_FALSE) 6424244781f1SPrakash Surya hdr = multilist_sublist_head(mls); 64253a737e0dSbrendan else 6426244781f1SPrakash Surya hdr = multilist_sublist_tail(mls); 64273a737e0dSbrendan 6428aad02571SSaso Kiselkov headroom = target_sz * l2arc_headroom; 6429*dcbf3bd6SGeorge Wilson if (zfs_compressed_arc_enabled) 6430aad02571SSaso Kiselkov headroom = (headroom * l2arc_headroom_boost) / 100; 6431aad02571SSaso Kiselkov 64327adb730bSGeorge Wilson for (; hdr; hdr = hdr_prev) { 6433aad02571SSaso Kiselkov kmutex_t *hash_lock; 6434aad02571SSaso Kiselkov 64353a737e0dSbrendan if (arc_warm == B_FALSE) 6436244781f1SPrakash Surya hdr_prev = multilist_sublist_next(mls, hdr); 64373a737e0dSbrendan else 6438244781f1SPrakash Surya hdr_prev = multilist_sublist_prev(mls, hdr); 6439fa94a07fSbrendan 64407adb730bSGeorge Wilson hash_lock = HDR_LOCK(hdr); 6441aad02571SSaso Kiselkov if (!mutex_tryenter(hash_lock)) { 6442fa94a07fSbrendan /* 6443fa94a07fSbrendan * Skip this buffer rather than waiting. 6444fa94a07fSbrendan */ 6445fa94a07fSbrendan continue; 6446fa94a07fSbrendan } 6447fa94a07fSbrendan 6448*dcbf3bd6SGeorge Wilson passed_sz += HDR_GET_LSIZE(hdr); 6449fa94a07fSbrendan if (passed_sz > headroom) { 6450fa94a07fSbrendan /* 6451fa94a07fSbrendan * Searched too far. 6452fa94a07fSbrendan */ 6453fa94a07fSbrendan mutex_exit(hash_lock); 6454fa94a07fSbrendan break; 6455fa94a07fSbrendan } 6456fa94a07fSbrendan 64577adb730bSGeorge Wilson if (!l2arc_write_eligible(guid, hdr)) { 6458fa94a07fSbrendan mutex_exit(hash_lock); 6459fa94a07fSbrendan continue; 6460fa94a07fSbrendan } 6461fa94a07fSbrendan 6462*dcbf3bd6SGeorge Wilson if ((write_asize + HDR_GET_LSIZE(hdr)) > target_sz) { 6463fa94a07fSbrendan full = B_TRUE; 6464fa94a07fSbrendan mutex_exit(hash_lock); 6465fa94a07fSbrendan break; 6466fa94a07fSbrendan } 6467fa94a07fSbrendan 6468fa94a07fSbrendan if (pio == NULL) { 6469fa94a07fSbrendan /* 6470fa94a07fSbrendan * Insert a dummy header on the buflist so 6471fa94a07fSbrendan * l2arc_write_done() can find where the 6472fa94a07fSbrendan * write buffers begin without searching. 6473fa94a07fSbrendan */ 6474244781f1SPrakash Surya mutex_enter(&dev->l2ad_mtx); 647589c86e32SChris Williamson list_insert_head(&dev->l2ad_buflist, head); 6476244781f1SPrakash Surya mutex_exit(&dev->l2ad_mtx); 6477fa94a07fSbrendan 6478fa94a07fSbrendan cb = kmem_alloc( 6479fa94a07fSbrendan sizeof (l2arc_write_callback_t), KM_SLEEP); 6480fa94a07fSbrendan cb->l2wcb_dev = dev; 6481fa94a07fSbrendan cb->l2wcb_head = head; 6482fa94a07fSbrendan pio = zio_root(spa, l2arc_write_done, cb, 6483fa94a07fSbrendan ZIO_FLAG_CANFAIL); 6484fa94a07fSbrendan } 6485fa94a07fSbrendan 648689c86e32SChris Williamson hdr->b_l2hdr.b_dev = dev; 6487*dcbf3bd6SGeorge Wilson hdr->b_l2hdr.b_daddr = dev->l2ad_hand; 6488*dcbf3bd6SGeorge Wilson arc_hdr_set_flags(hdr, 6489*dcbf3bd6SGeorge Wilson ARC_FLAG_L2_WRITING | ARC_FLAG_HAS_L2HDR); 6490*dcbf3bd6SGeorge Wilson 6491*dcbf3bd6SGeorge Wilson mutex_enter(&dev->l2ad_mtx); 6492*dcbf3bd6SGeorge Wilson list_insert_head(&dev->l2ad_buflist, hdr); 6493*dcbf3bd6SGeorge Wilson mutex_exit(&dev->l2ad_mtx); 6494*dcbf3bd6SGeorge Wilson 6495aad02571SSaso Kiselkov /* 6496*dcbf3bd6SGeorge Wilson * We rely on the L1 portion of the header below, so 6497*dcbf3bd6SGeorge Wilson * it's invalid for this header to have been evicted out 6498*dcbf3bd6SGeorge Wilson * of the ghost cache, prior to being written out. The 6499*dcbf3bd6SGeorge Wilson * ARC_FLAG_L2_WRITING bit ensures this won't happen. 6500aad02571SSaso Kiselkov */ 6501*dcbf3bd6SGeorge Wilson ASSERT(HDR_HAS_L1HDR(hdr)); 6502*dcbf3bd6SGeorge Wilson 6503*dcbf3bd6SGeorge Wilson ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); 6504*dcbf3bd6SGeorge Wilson ASSERT3P(hdr->b_l1hdr.b_pdata, !=, NULL); 6505*dcbf3bd6SGeorge Wilson ASSERT3U(arc_hdr_size(hdr), >, 0); 6506*dcbf3bd6SGeorge Wilson uint64_t size = arc_hdr_size(hdr); 6507*dcbf3bd6SGeorge Wilson 6508*dcbf3bd6SGeorge Wilson (void) refcount_add_many(&dev->l2ad_alloc, size, hdr); 6509aad02571SSaso Kiselkov 6510a52fc310SPrakash Surya /* 6511*dcbf3bd6SGeorge Wilson * Normally the L2ARC can use the hdr's data, but if 6512*dcbf3bd6SGeorge Wilson * we're sharing data between the hdr and one of its 6513*dcbf3bd6SGeorge Wilson * bufs, L2ARC needs its own copy of the data so that 6514*dcbf3bd6SGeorge Wilson * the ZIO below can't race with the buf consumer. To 6515*dcbf3bd6SGeorge Wilson * ensure that this copy will be available for the 6516*dcbf3bd6SGeorge Wilson * lifetime of the ZIO and be cleaned up afterwards, we 6517*dcbf3bd6SGeorge Wilson * add it to the l2arc_free_on_write queue. 6518a52fc310SPrakash Surya */ 6519*dcbf3bd6SGeorge Wilson void *to_write; 6520*dcbf3bd6SGeorge Wilson if (!HDR_SHARED_DATA(hdr)) { 6521*dcbf3bd6SGeorge Wilson to_write = hdr->b_l1hdr.b_pdata; 6522*dcbf3bd6SGeorge Wilson } else { 6523*dcbf3bd6SGeorge Wilson arc_buf_contents_t type = arc_buf_type(hdr); 6524*dcbf3bd6SGeorge Wilson if (type == ARC_BUFC_METADATA) { 6525*dcbf3bd6SGeorge Wilson to_write = zio_buf_alloc(size); 6526*dcbf3bd6SGeorge Wilson } else { 6527*dcbf3bd6SGeorge Wilson ASSERT3U(type, ==, ARC_BUFC_DATA); 6528*dcbf3bd6SGeorge Wilson to_write = zio_data_buf_alloc(size); 6529*dcbf3bd6SGeorge Wilson } 6530a52fc310SPrakash Surya 6531*dcbf3bd6SGeorge Wilson bcopy(hdr->b_l1hdr.b_pdata, to_write, size); 6532*dcbf3bd6SGeorge Wilson l2arc_free_data_on_write(to_write, size, type); 6533*dcbf3bd6SGeorge Wilson } 6534*dcbf3bd6SGeorge Wilson wzio = zio_write_phys(pio, dev->l2ad_vdev, 6535*dcbf3bd6SGeorge Wilson hdr->b_l2hdr.b_daddr, size, to_write, 6536*dcbf3bd6SGeorge Wilson ZIO_CHECKSUM_OFF, NULL, hdr, 6537*dcbf3bd6SGeorge Wilson ZIO_PRIORITY_ASYNC_WRITE, 6538*dcbf3bd6SGeorge Wilson ZIO_FLAG_CANFAIL, B_FALSE); 6539aad02571SSaso Kiselkov 6540*dcbf3bd6SGeorge Wilson write_sz += HDR_GET_LSIZE(hdr); 6541*dcbf3bd6SGeorge Wilson DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 6542*dcbf3bd6SGeorge Wilson zio_t *, wzio); 6543fa94a07fSbrendan 6544*dcbf3bd6SGeorge Wilson write_asize += size; 6545fa94a07fSbrendan /* 6546*dcbf3bd6SGeorge Wilson * Keep the clock hand suitably device-aligned. 6547fa94a07fSbrendan */ 6548*dcbf3bd6SGeorge Wilson uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, 6549*dcbf3bd6SGeorge Wilson size); 6550*dcbf3bd6SGeorge Wilson write_psize += asize; 6551*dcbf3bd6SGeorge Wilson dev->l2ad_hand += asize; 6552fa94a07fSbrendan 6553fa94a07fSbrendan mutex_exit(hash_lock); 6554fa94a07fSbrendan 6555*dcbf3bd6SGeorge Wilson (void) zio_nowait(wzio); 6556aad02571SSaso Kiselkov } 6557aad02571SSaso Kiselkov 6558244781f1SPrakash Surya multilist_sublist_unlock(mls); 6559aad02571SSaso Kiselkov 6560aad02571SSaso Kiselkov if (full == B_TRUE) 6561aad02571SSaso Kiselkov break; 6562aad02571SSaso Kiselkov } 6563aad02571SSaso Kiselkov 6564aad02571SSaso Kiselkov /* No buffers selected for writing? */ 6565aad02571SSaso Kiselkov if (pio == NULL) { 6566aad02571SSaso Kiselkov ASSERT0(write_sz); 656789c86e32SChris Williamson ASSERT(!HDR_HAS_L1HDR(head)); 656889c86e32SChris Williamson kmem_cache_free(hdr_l2only_cache, head); 6569aad02571SSaso Kiselkov return (0); 6570aad02571SSaso Kiselkov } 6571aad02571SSaso Kiselkov 6572aad02571SSaso Kiselkov ASSERT3U(write_asize, <=, target_sz); 6573fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_writes_sent); 6574aad02571SSaso Kiselkov ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize); 6575fa94a07fSbrendan ARCSTAT_INCR(arcstat_l2_size, write_sz); 6576*dcbf3bd6SGeorge Wilson ARCSTAT_INCR(arcstat_l2_asize, write_asize); 6577*dcbf3bd6SGeorge Wilson vdev_space_update(dev->l2ad_vdev, write_asize, 0, 0); 6578fa94a07fSbrendan 6579fa94a07fSbrendan /* 6580fa94a07fSbrendan * Bump device hand to the device start if it is approaching the end. 6581fa94a07fSbrendan * l2arc_evict() will already have evicted ahead for this case. 6582fa94a07fSbrendan */ 65833a737e0dSbrendan if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 6584fa94a07fSbrendan dev->l2ad_hand = dev->l2ad_start; 6585fa94a07fSbrendan dev->l2ad_first = B_FALSE; 6586fa94a07fSbrendan } 6587fa94a07fSbrendan 65885a98e54bSBrendan Gregg - Sun Microsystems dev->l2ad_writing = B_TRUE; 6589fa94a07fSbrendan (void) zio_wait(pio); 65905a98e54bSBrendan Gregg - Sun Microsystems dev->l2ad_writing = B_FALSE; 65915a98e54bSBrendan Gregg - Sun Microsystems 6592aad02571SSaso Kiselkov return (write_asize); 6593aad02571SSaso Kiselkov } 6594aad02571SSaso Kiselkov 6595fa94a07fSbrendan /* 6596fa94a07fSbrendan * This thread feeds the L2ARC at regular intervals. This is the beating 6597fa94a07fSbrendan * heart of the L2ARC. 6598fa94a07fSbrendan */ 6599fa94a07fSbrendan static void 6600fa94a07fSbrendan l2arc_feed_thread(void) 6601fa94a07fSbrendan { 6602fa94a07fSbrendan callb_cpr_t cpr; 6603fa94a07fSbrendan l2arc_dev_t *dev; 6604fa94a07fSbrendan spa_t *spa; 66055a98e54bSBrendan Gregg - Sun Microsystems uint64_t size, wrote; 6606d3d50737SRafael Vanoni clock_t begin, next = ddi_get_lbolt(); 6607fa94a07fSbrendan 6608fa94a07fSbrendan CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 6609fa94a07fSbrendan 6610fa94a07fSbrendan mutex_enter(&l2arc_feed_thr_lock); 6611fa94a07fSbrendan 6612fa94a07fSbrendan while (l2arc_thread_exit == 0) { 6613fa94a07fSbrendan CALLB_CPR_SAFE_BEGIN(&cpr); 6614fa94a07fSbrendan (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 66155a98e54bSBrendan Gregg - Sun Microsystems next); 6616fa94a07fSbrendan CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 6617d3d50737SRafael Vanoni next = ddi_get_lbolt() + hz; 6618fa94a07fSbrendan 66193a737e0dSbrendan /* 66203a737e0dSbrendan * Quick check for L2ARC devices. 66213a737e0dSbrendan */ 6622c5904d13Seschrock mutex_enter(&l2arc_dev_mtx); 66233a737e0dSbrendan if (l2arc_ndev == 0) { 66243a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 66253a737e0dSbrendan continue; 66263a737e0dSbrendan } 66273a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 6628d3d50737SRafael Vanoni begin = ddi_get_lbolt(); 6629c5904d13Seschrock 6630fa94a07fSbrendan /* 6631c5904d13Seschrock * This selects the next l2arc device to write to, and in 6632c5904d13Seschrock * doing so the next spa to feed from: dev->l2ad_spa. This 66333a737e0dSbrendan * will return NULL if there are now no l2arc devices or if 66343a737e0dSbrendan * they are all faulted. 66353a737e0dSbrendan * 66363a737e0dSbrendan * If a device is returned, its spa's config lock is also 66373a737e0dSbrendan * held to prevent device removal. l2arc_dev_get_next() 66383a737e0dSbrendan * will grab and release l2arc_dev_mtx. 6639fa94a07fSbrendan */ 66403a737e0dSbrendan if ((dev = l2arc_dev_get_next()) == NULL) 6641fa94a07fSbrendan continue; 66423a737e0dSbrendan 66433a737e0dSbrendan spa = dev->l2ad_spa; 6644*dcbf3bd6SGeorge Wilson ASSERT3P(spa, !=, NULL); 6645fa94a07fSbrendan 6646f9af39baSGeorge Wilson /* 6647f9af39baSGeorge Wilson * If the pool is read-only then force the feed thread to 6648f9af39baSGeorge Wilson * sleep a little longer. 6649f9af39baSGeorge Wilson */ 6650f9af39baSGeorge Wilson if (!spa_writeable(spa)) { 6651f9af39baSGeorge Wilson next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 6652f9af39baSGeorge Wilson spa_config_exit(spa, SCL_L2ARC, dev); 6653f9af39baSGeorge Wilson continue; 6654f9af39baSGeorge Wilson } 6655f9af39baSGeorge Wilson 6656fa94a07fSbrendan /* 6657fa94a07fSbrendan * Avoid contributing to memory pressure. 6658fa94a07fSbrendan */ 6659fa94a07fSbrendan if (arc_reclaim_needed()) { 6660fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 6661e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, dev); 6662fa94a07fSbrendan continue; 6663fa94a07fSbrendan } 6664fa94a07fSbrendan 6665fa94a07fSbrendan ARCSTAT_BUMP(arcstat_l2_feeds); 6666fa94a07fSbrendan 6667aad02571SSaso Kiselkov size = l2arc_write_size(); 66683a737e0dSbrendan 6669fa94a07fSbrendan /* 6670fa94a07fSbrendan * Evict L2ARC buffers that will be overwritten. 6671fa94a07fSbrendan */ 66723a737e0dSbrendan l2arc_evict(dev, size, B_FALSE); 6673fa94a07fSbrendan 6674fa94a07fSbrendan /* 6675fa94a07fSbrendan * Write ARC buffers. 6676fa94a07fSbrendan */ 6677*dcbf3bd6SGeorge Wilson wrote = l2arc_write_buffers(spa, dev, size); 66785a98e54bSBrendan Gregg - Sun Microsystems 66795a98e54bSBrendan Gregg - Sun Microsystems /* 66805a98e54bSBrendan Gregg - Sun Microsystems * Calculate interval between writes. 66815a98e54bSBrendan Gregg - Sun Microsystems */ 66825a98e54bSBrendan Gregg - Sun Microsystems next = l2arc_write_interval(begin, size, wrote); 6683e14bb325SJeff Bonwick spa_config_exit(spa, SCL_L2ARC, dev); 6684fa94a07fSbrendan } 6685fa94a07fSbrendan 6686fa94a07fSbrendan l2arc_thread_exit = 0; 6687fa94a07fSbrendan cv_broadcast(&l2arc_feed_thr_cv); 6688fa94a07fSbrendan CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 6689fa94a07fSbrendan thread_exit(); 6690fa94a07fSbrendan } 6691fa94a07fSbrendan 6692c5904d13Seschrock boolean_t 6693c5904d13Seschrock l2arc_vdev_present(vdev_t *vd) 6694c5904d13Seschrock { 6695c5904d13Seschrock l2arc_dev_t *dev; 6696c5904d13Seschrock 6697c5904d13Seschrock mutex_enter(&l2arc_dev_mtx); 6698c5904d13Seschrock for (dev = list_head(l2arc_dev_list); dev != NULL; 6699c5904d13Seschrock dev = list_next(l2arc_dev_list, dev)) { 6700c5904d13Seschrock if (dev->l2ad_vdev == vd) 6701c5904d13Seschrock break; 6702c5904d13Seschrock } 6703c5904d13Seschrock mutex_exit(&l2arc_dev_mtx); 6704c5904d13Seschrock 6705c5904d13Seschrock return (dev != NULL); 6706c5904d13Seschrock } 6707c5904d13Seschrock 6708fa94a07fSbrendan /* 6709fa94a07fSbrendan * Add a vdev for use by the L2ARC. By this point the spa has already 6710fa94a07fSbrendan * validated the vdev and opened it. 6711fa94a07fSbrendan */ 6712fa94a07fSbrendan void 6713573ca77eSGeorge Wilson l2arc_add_vdev(spa_t *spa, vdev_t *vd) 6714fa94a07fSbrendan { 6715fa94a07fSbrendan l2arc_dev_t *adddev; 6716fa94a07fSbrendan 6717c5904d13Seschrock ASSERT(!l2arc_vdev_present(vd)); 6718c5904d13Seschrock 6719fa94a07fSbrendan /* 6720fa94a07fSbrendan * Create a new l2arc device entry. 6721fa94a07fSbrendan */ 6722fa94a07fSbrendan adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 6723fa94a07fSbrendan adddev->l2ad_spa = spa; 6724fa94a07fSbrendan adddev->l2ad_vdev = vd; 6725573ca77eSGeorge Wilson adddev->l2ad_start = VDEV_LABEL_START_SIZE; 6726573ca77eSGeorge Wilson adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 6727fa94a07fSbrendan adddev->l2ad_hand = adddev->l2ad_start; 6728fa94a07fSbrendan adddev->l2ad_first = B_TRUE; 67295a98e54bSBrendan Gregg - Sun Microsystems adddev->l2ad_writing = B_FALSE; 6730fa94a07fSbrendan 673189c86e32SChris Williamson mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); 6732fa94a07fSbrendan /* 6733fa94a07fSbrendan * This is a list of all ARC buffers that are still valid on the 6734fa94a07fSbrendan * device. 6735fa94a07fSbrendan */ 673689c86e32SChris Williamson list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 673789c86e32SChris Williamson offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); 6738fa94a07fSbrendan 6739b24ab676SJeff Bonwick vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 6740a52fc310SPrakash Surya refcount_create(&adddev->l2ad_alloc); 6741fa94a07fSbrendan 6742fa94a07fSbrendan /* 6743fa94a07fSbrendan * Add device to global list 6744fa94a07fSbrendan */ 6745fa94a07fSbrendan mutex_enter(&l2arc_dev_mtx); 6746fa94a07fSbrendan list_insert_head(l2arc_dev_list, adddev); 6747fa94a07fSbrendan atomic_inc_64(&l2arc_ndev); 6748fa94a07fSbrendan mutex_exit(&l2arc_dev_mtx); 6749fa94a07fSbrendan } 6750fa94a07fSbrendan 6751fa94a07fSbrendan /* 6752fa94a07fSbrendan * Remove a vdev from the L2ARC. 6753fa94a07fSbrendan */ 6754fa94a07fSbrendan void 6755fa94a07fSbrendan l2arc_remove_vdev(vdev_t *vd) 6756fa94a07fSbrendan { 6757fa94a07fSbrendan l2arc_dev_t *dev, *nextdev, *remdev = NULL; 6758fa94a07fSbrendan 6759fa94a07fSbrendan /* 6760fa94a07fSbrendan * Find the device by vdev 6761fa94a07fSbrendan */ 6762fa94a07fSbrendan mutex_enter(&l2arc_dev_mtx); 6763fa94a07fSbrendan for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 6764fa94a07fSbrendan nextdev = list_next(l2arc_dev_list, dev); 6765fa94a07fSbrendan if (vd == dev->l2ad_vdev) { 6766fa94a07fSbrendan remdev = dev; 6767fa94a07fSbrendan break; 6768fa94a07fSbrendan } 6769fa94a07fSbrendan } 6770*dcbf3bd6SGeorge Wilson ASSERT3P(remdev, !=, NULL); 6771fa94a07fSbrendan 6772fa94a07fSbrendan /* 6773fa94a07fSbrendan * Remove device from global list 6774fa94a07fSbrendan */ 6775fa94a07fSbrendan list_remove(l2arc_dev_list, remdev); 6776fa94a07fSbrendan l2arc_dev_last = NULL; /* may have been invalidated */ 67773a737e0dSbrendan atomic_dec_64(&l2arc_ndev); 67783a737e0dSbrendan mutex_exit(&l2arc_dev_mtx); 6779fa94a07fSbrendan 6780fa94a07fSbrendan /* 6781fa94a07fSbrendan * Clear all buflists and ARC references. L2ARC device flush. 6782fa94a07fSbrendan */ 6783fa94a07fSbrendan l2arc_evict(remdev, 0, B_TRUE); 678489c86e32SChris Williamson list_destroy(&remdev->l2ad_buflist); 678589c86e32SChris Williamson mutex_destroy(&remdev->l2ad_mtx); 6786a52fc310SPrakash Surya refcount_destroy(&remdev->l2ad_alloc); 6787fa94a07fSbrendan kmem_free(remdev, sizeof (l2arc_dev_t)); 6788fa94a07fSbrendan } 6789fa94a07fSbrendan 6790fa94a07fSbrendan void 6791e14bb325SJeff Bonwick l2arc_init(void) 6792fa94a07fSbrendan { 6793fa94a07fSbrendan l2arc_thread_exit = 0; 6794fa94a07fSbrendan l2arc_ndev = 0; 6795fa94a07fSbrendan l2arc_writes_sent = 0; 6796fa94a07fSbrendan l2arc_writes_done = 0; 6797fa94a07fSbrendan 6798fa94a07fSbrendan mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 6799fa94a07fSbrendan cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 6800fa94a07fSbrendan mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 6801fa94a07fSbrendan mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 6802fa94a07fSbrendan 6803fa94a07fSbrendan l2arc_dev_list = &L2ARC_dev_list; 6804fa94a07fSbrendan l2arc_free_on_write = &L2ARC_free_on_write; 6805fa94a07fSbrendan list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 6806fa94a07fSbrendan offsetof(l2arc_dev_t, l2ad_node)); 6807fa94a07fSbrendan list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 6808fa94a07fSbrendan offsetof(l2arc_data_free_t, l2df_list_node)); 6809fa94a07fSbrendan } 6810fa94a07fSbrendan 6811fa94a07fSbrendan void 6812e14bb325SJeff Bonwick l2arc_fini(void) 6813fa94a07fSbrendan { 68143a737e0dSbrendan /* 68153a737e0dSbrendan * This is called from dmu_fini(), which is called from spa_fini(); 68163a737e0dSbrendan * Because of this, we can assume that all l2arc devices have 68173a737e0dSbrendan * already been removed when the pools themselves were removed. 68183a737e0dSbrendan */ 68193a737e0dSbrendan 68203a737e0dSbrendan l2arc_do_free_on_write(); 68213a737e0dSbrendan 6822fa94a07fSbrendan mutex_destroy(&l2arc_feed_thr_lock); 6823fa94a07fSbrendan cv_destroy(&l2arc_feed_thr_cv); 6824fa94a07fSbrendan mutex_destroy(&l2arc_dev_mtx); 6825fa94a07fSbrendan mutex_destroy(&l2arc_free_on_write_mtx); 6826fa94a07fSbrendan 6827fa94a07fSbrendan list_destroy(l2arc_dev_list); 6828fa94a07fSbrendan list_destroy(l2arc_free_on_write); 6829fa94a07fSbrendan } 6830e14bb325SJeff Bonwick 6831e14bb325SJeff Bonwick void 6832e14bb325SJeff Bonwick l2arc_start(void) 6833e14bb325SJeff Bonwick { 68348ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 6835e14bb325SJeff Bonwick return; 6836e14bb325SJeff Bonwick 6837e14bb325SJeff Bonwick (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 6838e14bb325SJeff Bonwick TS_RUN, minclsyspri); 6839e14bb325SJeff Bonwick } 6840e14bb325SJeff Bonwick 6841e14bb325SJeff Bonwick void 6842e14bb325SJeff Bonwick l2arc_stop(void) 6843e14bb325SJeff Bonwick { 68448ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 6845e14bb325SJeff Bonwick return; 6846e14bb325SJeff Bonwick 6847e14bb325SJeff Bonwick mutex_enter(&l2arc_feed_thr_lock); 6848e14bb325SJeff Bonwick cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 6849e14bb325SJeff Bonwick l2arc_thread_exit = 1; 6850e14bb325SJeff Bonwick while (l2arc_thread_exit != 0) 6851e14bb325SJeff Bonwick cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 6852e14bb325SJeff Bonwick mutex_exit(&l2arc_feed_thr_lock); 6853e14bb325SJeff Bonwick } 6854