arc.c revision 330061
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21168404Spjd/* 22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23277826Sdelphij * Copyright (c) 2012, Joyent, Inc. All rights reserved. 24321552Smav * Copyright (c) 2011, 2017 by Delphix. All rights reserved. 25260835Sdelphij * Copyright (c) 2014 by Saso Kiselkov. All rights reserved. 26329490Smav * Copyright 2017 Nexenta Systems, Inc. All rights reserved. 27168404Spjd */ 28168404Spjd 29168404Spjd/* 30168404Spjd * DVA-based Adjustable Replacement Cache 31168404Spjd * 32168404Spjd * While much of the theory of operation used here is 33168404Spjd * based on the self-tuning, low overhead replacement cache 34168404Spjd * presented by Megiddo and Modha at FAST 2003, there are some 35168404Spjd * significant differences: 36168404Spjd * 37168404Spjd * 1. The Megiddo and Modha model assumes any page is evictable. 38168404Spjd * Pages in its cache cannot be "locked" into memory. This makes 39168404Spjd * the eviction algorithm simple: evict the last page in the list. 40168404Spjd * This also make the performance characteristics easy to reason 41168404Spjd * about. Our cache is not so simple. At any given moment, some 42168404Spjd * subset of the blocks in the cache are un-evictable because we 43168404Spjd * have handed out a reference to them. Blocks are only evictable 44168404Spjd * when there are no external references active. This makes 45168404Spjd * eviction far more problematic: we choose to evict the evictable 46168404Spjd * blocks that are the "lowest" in the list. 47168404Spjd * 48168404Spjd * There are times when it is not possible to evict the requested 49168404Spjd * space. In these circumstances we are unable to adjust the cache 50168404Spjd * size. To prevent the cache growing unbounded at these times we 51185029Spjd * implement a "cache throttle" that slows the flow of new data 52185029Spjd * into the cache until we can make space available. 53168404Spjd * 54168404Spjd * 2. The Megiddo and Modha model assumes a fixed cache size. 55168404Spjd * Pages are evicted when the cache is full and there is a cache 56168404Spjd * miss. Our model has a variable sized cache. It grows with 57185029Spjd * high use, but also tries to react to memory pressure from the 58168404Spjd * operating system: decreasing its size when system memory is 59168404Spjd * tight. 60168404Spjd * 61168404Spjd * 3. The Megiddo and Modha model assumes a fixed page size. All 62251631Sdelphij * elements of the cache are therefore exactly the same size. So 63168404Spjd * when adjusting the cache size following a cache miss, its simply 64168404Spjd * a matter of choosing a single page to evict. In our model, we 65168404Spjd * have variable sized cache blocks (rangeing from 512 bytes to 66251631Sdelphij * 128K bytes). We therefore choose a set of blocks to evict to make 67168404Spjd * space for a cache miss that approximates as closely as possible 68168404Spjd * the space used by the new block. 69168404Spjd * 70168404Spjd * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 71168404Spjd * by N. Megiddo & D. Modha, FAST 2003 72168404Spjd */ 73168404Spjd 74168404Spjd/* 75168404Spjd * The locking model: 76168404Spjd * 77168404Spjd * A new reference to a cache buffer can be obtained in two 78168404Spjd * ways: 1) via a hash table lookup using the DVA as a key, 79185029Spjd * or 2) via one of the ARC lists. The arc_read() interface 80321535Smav * uses method 1, while the internal ARC algorithms for 81251631Sdelphij * adjusting the cache use method 2. We therefore provide two 82168404Spjd * types of locks: 1) the hash table lock array, and 2) the 83321535Smav * ARC list locks. 84168404Spjd * 85286774Smav * Buffers do not have their own mutexes, rather they rely on the 86286774Smav * hash table mutexes for the bulk of their protection (i.e. most 87286774Smav * fields in the arc_buf_hdr_t are protected by these mutexes). 88168404Spjd * 89168404Spjd * buf_hash_find() returns the appropriate mutex (held) when it 90168404Spjd * locates the requested buffer in the hash table. It returns 91168404Spjd * NULL for the mutex if the buffer was not in the table. 92168404Spjd * 93168404Spjd * buf_hash_remove() expects the appropriate hash mutex to be 94168404Spjd * already held before it is invoked. 95168404Spjd * 96321535Smav * Each ARC state also has a mutex which is used to protect the 97168404Spjd * buffer list associated with the state. When attempting to 98321535Smav * obtain a hash table lock while holding an ARC list lock you 99168404Spjd * must use: mutex_tryenter() to avoid deadlock. Also note that 100168404Spjd * the active state mutex must be held before the ghost state mutex. 101168404Spjd * 102168404Spjd * Note that the majority of the performance stats are manipulated 103168404Spjd * with atomic operations. 104185029Spjd * 105286570Smav * The L2ARC uses the l2ad_mtx on each vdev for the following: 106185029Spjd * 107185029Spjd * - L2ARC buflist creation 108185029Spjd * - L2ARC buflist eviction 109185029Spjd * - L2ARC write completion, which walks L2ARC buflists 110185029Spjd * - ARC header destruction, as it removes from L2ARC buflists 111185029Spjd * - ARC header release, as it removes from L2ARC buflists 112168404Spjd */ 113168404Spjd 114307265Smav/* 115307265Smav * ARC operation: 116307265Smav * 117307265Smav * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure. 118307265Smav * This structure can point either to a block that is still in the cache or to 119307265Smav * one that is only accessible in an L2 ARC device, or it can provide 120307265Smav * information about a block that was recently evicted. If a block is 121307265Smav * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough 122307265Smav * information to retrieve it from the L2ARC device. This information is 123307265Smav * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block 124307265Smav * that is in this state cannot access the data directly. 125307265Smav * 126307265Smav * Blocks that are actively being referenced or have not been evicted 127307265Smav * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within 128307265Smav * the arc_buf_hdr_t that will point to the data block in memory. A block can 129307265Smav * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC 130321535Smav * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and 131321610Smav * also in the arc_buf_hdr_t's private physical data block pointer (b_pabd). 132321535Smav * 133321535Smav * The L1ARC's data pointer may or may not be uncompressed. The ARC has the 134321610Smav * ability to store the physical data (b_pabd) associated with the DVA of the 135321610Smav * arc_buf_hdr_t. Since the b_pabd is a copy of the on-disk physical block, 136321535Smav * it will match its on-disk compression characteristics. This behavior can be 137321535Smav * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the 138321610Smav * compressed ARC functionality is disabled, the b_pabd will point to an 139321535Smav * uncompressed version of the on-disk data. 140321535Smav * 141321535Smav * Data in the L1ARC is not accessed by consumers of the ARC directly. Each 142321535Smav * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it. 143321535Smav * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC 144321535Smav * consumer. The ARC will provide references to this data and will keep it 145321535Smav * cached until it is no longer in use. The ARC caches only the L1ARC's physical 146321535Smav * data block and will evict any arc_buf_t that is no longer referenced. The 147321535Smav * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the 148307265Smav * "overhead_size" kstat. 149307265Smav * 150321535Smav * Depending on the consumer, an arc_buf_t can be requested in uncompressed or 151321535Smav * compressed form. The typical case is that consumers will want uncompressed 152321535Smav * data, and when that happens a new data buffer is allocated where the data is 153321535Smav * decompressed for them to use. Currently the only consumer who wants 154321535Smav * compressed arc_buf_t's is "zfs send", when it streams data exactly as it 155321535Smav * exists on disk. When this happens, the arc_buf_t's data buffer is shared 156321535Smav * with the arc_buf_hdr_t. 157307265Smav * 158321535Smav * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The 159321535Smav * first one is owned by a compressed send consumer (and therefore references 160321535Smav * the same compressed data buffer as the arc_buf_hdr_t) and the second could be 161321535Smav * used by any other consumer (and has its own uncompressed copy of the data 162321535Smav * buffer). 163307265Smav * 164321535Smav * arc_buf_hdr_t 165321535Smav * +-----------+ 166321535Smav * | fields | 167321535Smav * | common to | 168321535Smav * | L1- and | 169321535Smav * | L2ARC | 170321535Smav * +-----------+ 171321535Smav * | l2arc_buf_hdr_t 172321535Smav * | | 173321535Smav * +-----------+ 174321535Smav * | l1arc_buf_hdr_t 175321535Smav * | | arc_buf_t 176321535Smav * | b_buf +------------>+-----------+ arc_buf_t 177321610Smav * | b_pabd +-+ |b_next +---->+-----------+ 178321535Smav * +-----------+ | |-----------| |b_next +-->NULL 179321535Smav * | |b_comp = T | +-----------+ 180321535Smav * | |b_data +-+ |b_comp = F | 181321535Smav * | +-----------+ | |b_data +-+ 182321535Smav * +->+------+ | +-----------+ | 183321535Smav * compressed | | | | 184321535Smav * data | |<--------------+ | uncompressed 185321535Smav * +------+ compressed, | data 186321535Smav * shared +-->+------+ 187321535Smav * data | | 188321535Smav * | | 189321535Smav * +------+ 190307265Smav * 191307265Smav * When a consumer reads a block, the ARC must first look to see if the 192321535Smav * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new 193321535Smav * arc_buf_t and either copies uncompressed data into a new data buffer from an 194321610Smav * existing uncompressed arc_buf_t, decompresses the hdr's b_pabd buffer into a 195321610Smav * new data buffer, or shares the hdr's b_pabd buffer, depending on whether the 196321535Smav * hdr is compressed and the desired compression characteristics of the 197321535Smav * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the 198321535Smav * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be 199321535Smav * the last buffer in the hdr's b_buf list, however a shared compressed buf can 200321535Smav * be anywhere in the hdr's list. 201307265Smav * 202307265Smav * The diagram below shows an example of an uncompressed ARC hdr that is 203321535Smav * sharing its data with an arc_buf_t (note that the shared uncompressed buf is 204321535Smav * the last element in the buf list): 205307265Smav * 206307265Smav * arc_buf_hdr_t 207307265Smav * +-----------+ 208307265Smav * | | 209307265Smav * | | 210307265Smav * | | 211307265Smav * +-----------+ 212307265Smav * l2arc_buf_hdr_t| | 213307265Smav * | | 214307265Smav * +-----------+ 215307265Smav * l1arc_buf_hdr_t| | 216307265Smav * | | arc_buf_t (shared) 217307265Smav * | b_buf +------------>+---------+ arc_buf_t 218307265Smav * | | |b_next +---->+---------+ 219321610Smav * | b_pabd +-+ |---------| |b_next +-->NULL 220307265Smav * +-----------+ | | | +---------+ 221307265Smav * | |b_data +-+ | | 222307265Smav * | +---------+ | |b_data +-+ 223307265Smav * +->+------+ | +---------+ | 224307265Smav * | | | | 225307265Smav * uncompressed | | | | 226307265Smav * data +------+ | | 227307265Smav * ^ +->+------+ | 228307265Smav * | uncompressed | | | 229307265Smav * | data | | | 230307265Smav * | +------+ | 231307265Smav * +---------------------------------+ 232307265Smav * 233321610Smav * Writing to the ARC requires that the ARC first discard the hdr's b_pabd 234307265Smav * since the physical block is about to be rewritten. The new data contents 235321535Smav * will be contained in the arc_buf_t. As the I/O pipeline performs the write, 236321535Smav * it may compress the data before writing it to disk. The ARC will be called 237321535Smav * with the transformed data and will bcopy the transformed on-disk block into 238321610Smav * a newly allocated b_pabd. Writes are always done into buffers which have 239321535Smav * either been loaned (and hence are new and don't have other readers) or 240321535Smav * buffers which have been released (and hence have their own hdr, if there 241321535Smav * were originally other readers of the buf's original hdr). This ensures that 242321535Smav * the ARC only needs to update a single buf and its hdr after a write occurs. 243307265Smav * 244321610Smav * When the L2ARC is in use, it will also take advantage of the b_pabd. The 245321610Smav * L2ARC will always write the contents of b_pabd to the L2ARC. This means 246321535Smav * that when compressed ARC is enabled that the L2ARC blocks are identical 247307265Smav * to the on-disk block in the main data pool. This provides a significant 248307265Smav * advantage since the ARC can leverage the bp's checksum when reading from the 249307265Smav * L2ARC to determine if the contents are valid. However, if the compressed 250321535Smav * ARC is disabled, then the L2ARC's block must be transformed to look 251307265Smav * like the physical block in the main data pool before comparing the 252307265Smav * checksum and determining its validity. 253307265Smav */ 254307265Smav 255168404Spjd#include <sys/spa.h> 256168404Spjd#include <sys/zio.h> 257307265Smav#include <sys/spa_impl.h> 258251478Sdelphij#include <sys/zio_compress.h> 259307265Smav#include <sys/zio_checksum.h> 260168404Spjd#include <sys/zfs_context.h> 261168404Spjd#include <sys/arc.h> 262168404Spjd#include <sys/refcount.h> 263185029Spjd#include <sys/vdev.h> 264219089Spjd#include <sys/vdev_impl.h> 265258632Savg#include <sys/dsl_pool.h> 266321610Smav#include <sys/zio_checksum.h> 267286763Smav#include <sys/multilist.h> 268321610Smav#include <sys/abd.h> 269168404Spjd#ifdef _KERNEL 270168404Spjd#include <sys/dnlc.h> 271297633Strasz#include <sys/racct.h> 272168404Spjd#endif 273168404Spjd#include <sys/callb.h> 274168404Spjd#include <sys/kstat.h> 275248572Ssmh#include <sys/trim_map.h> 276219089Spjd#include <zfs_fletcher.h> 277168404Spjd#include <sys/sdt.h> 278168404Spjd 279272483Ssmh#include <machine/vmparam.h> 280191902Skmacy 281240133Smm#ifdef illumos 282240133Smm#ifndef _KERNEL 283240133Smm/* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ 284240133Smmboolean_t arc_watch = B_FALSE; 285240133Smmint arc_procfd; 286240133Smm#endif 287240133Smm#endif /* illumos */ 288240133Smm 289286763Smavstatic kmutex_t arc_reclaim_lock; 290286763Smavstatic kcondvar_t arc_reclaim_thread_cv; 291286763Smavstatic boolean_t arc_reclaim_thread_exit; 292286763Smavstatic kcondvar_t arc_reclaim_waiters_cv; 293168404Spjd 294301997Skibstatic kmutex_t arc_dnlc_evicts_lock; 295301997Skibstatic kcondvar_t arc_dnlc_evicts_cv; 296301997Skibstatic boolean_t arc_dnlc_evicts_thread_exit; 297301997Skib 298286625Smavuint_t arc_reduce_dnlc_percent = 3; 299168404Spjd 300258632Savg/* 301286763Smav * The number of headers to evict in arc_evict_state_impl() before 302286763Smav * dropping the sublist lock and evicting from another sublist. A lower 303286763Smav * value means we're more likely to evict the "correct" header (i.e. the 304286763Smav * oldest header in the arc state), but comes with higher overhead 305286763Smav * (i.e. more invocations of arc_evict_state_impl()). 306258632Savg */ 307286763Smavint zfs_arc_evict_batch_limit = 10; 308258632Savg 309168404Spjd/* number of seconds before growing cache again */ 310168404Spjdstatic int arc_grow_retry = 60; 311168404Spjd 312321610Smav/* shift of arc_c for calculating overflow limit in arc_get_data_impl */ 313286763Smavint zfs_arc_overflow_shift = 8; 314286763Smav 315208373Smm/* shift of arc_c for calculating both min and max arc_p */ 316208373Smmstatic int arc_p_min_shift = 4; 317208373Smm 318208373Smm/* log2(fraction of arc to reclaim) */ 319286625Smavstatic int arc_shrink_shift = 7; 320208373Smm 321168404Spjd/* 322286625Smav * log2(fraction of ARC which must be free to allow growing). 323286625Smav * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, 324286625Smav * when reading a new block into the ARC, we will evict an equal-sized block 325286625Smav * from the ARC. 326286625Smav * 327286625Smav * This must be less than arc_shrink_shift, so that when we shrink the ARC, 328286625Smav * we will still not allow it to grow. 329286625Smav */ 330286625Smavint arc_no_grow_shift = 5; 331286625Smav 332286625Smav 333286625Smav/* 334168404Spjd * minimum lifespan of a prefetch block in clock ticks 335168404Spjd * (initialized in arc_init()) 336168404Spjd */ 337168404Spjdstatic int arc_min_prefetch_lifespan; 338168404Spjd 339258632Savg/* 340258632Savg * If this percent of memory is free, don't throttle. 341258632Savg */ 342258632Savgint arc_lotsfree_percent = 10; 343258632Savg 344208373Smmstatic int arc_dead; 345287702Sdelphijextern boolean_t zfs_prefetch_disable; 346168404Spjd 347168404Spjd/* 348185029Spjd * The arc has filled available memory and has now warmed up. 349185029Spjd */ 350185029Spjdstatic boolean_t arc_warm; 351185029Spjd 352286762Smav/* 353286762Smav * These tunables are for performance analysis. 354286762Smav */ 355185029Spjduint64_t zfs_arc_max; 356185029Spjduint64_t zfs_arc_min; 357185029Spjduint64_t zfs_arc_meta_limit = 0; 358275780Sdelphijuint64_t zfs_arc_meta_min = 0; 359208373Smmint zfs_arc_grow_retry = 0; 360208373Smmint zfs_arc_shrink_shift = 0; 361323667Sbaptint zfs_arc_no_grow_shift = 0; 362208373Smmint zfs_arc_p_min_shift = 0; 363269230Sdelphijuint64_t zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ 364272483Ssmhu_int zfs_arc_free_target = 0; 365185029Spjd 366302265Ssmh/* Absolute min for arc min / max is 16MB. */ 367302265Ssmhstatic uint64_t arc_abs_min = 16 << 20; 368302265Ssmh 369307265Smavboolean_t zfs_compressed_arc_enabled = B_TRUE; 370307265Smav 371270759Ssmhstatic int sysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS); 372275748Sdelphijstatic int sysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS); 373302265Ssmhstatic int sysctl_vfs_zfs_arc_max(SYSCTL_HANDLER_ARGS); 374302265Ssmhstatic int sysctl_vfs_zfs_arc_min(SYSCTL_HANDLER_ARGS); 375323667Sbaptstatic int sysctl_vfs_zfs_arc_no_grow_shift(SYSCTL_HANDLER_ARGS); 376270759Ssmh 377302265Ssmh#if defined(__FreeBSD__) && defined(_KERNEL) 378270759Ssmhstatic void 379270759Ssmharc_free_target_init(void *unused __unused) 380270759Ssmh{ 381270759Ssmh 382272483Ssmh zfs_arc_free_target = vm_pageout_wakeup_thresh; 383270759Ssmh} 384270759SsmhSYSINIT(arc_free_target_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_ANY, 385270759Ssmh arc_free_target_init, NULL); 386270759Ssmh 387185029SpjdTUNABLE_QUAD("vfs.zfs.arc_meta_limit", &zfs_arc_meta_limit); 388275780SdelphijTUNABLE_QUAD("vfs.zfs.arc_meta_min", &zfs_arc_meta_min); 389273026SdelphijTUNABLE_INT("vfs.zfs.arc_shrink_shift", &zfs_arc_shrink_shift); 390323667SbaptTUNABLE_INT("vfs.zfs.arc_grow_retry", &zfs_arc_grow_retry); 391323667SbaptTUNABLE_INT("vfs.zfs.arc_no_grow_shift", &zfs_arc_no_grow_shift); 392168473SpjdSYSCTL_DECL(_vfs_zfs); 393302265SsmhSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_max, CTLTYPE_U64 | CTLFLAG_RWTUN, 394302265Ssmh 0, sizeof(uint64_t), sysctl_vfs_zfs_arc_max, "QU", "Maximum ARC size"); 395302265SsmhSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_min, CTLTYPE_U64 | CTLFLAG_RWTUN, 396302265Ssmh 0, sizeof(uint64_t), sysctl_vfs_zfs_arc_min, "QU", "Minimum ARC size"); 397323667SbaptSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_no_grow_shift, CTLTYPE_U32 | CTLFLAG_RWTUN, 398323667Sbapt 0, sizeof(uint32_t), sysctl_vfs_zfs_arc_no_grow_shift, "U", 399323667Sbapt "log2(fraction of ARC which must be free to allow growing)"); 400269230SdelphijSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_average_blocksize, CTLFLAG_RDTUN, 401269230Sdelphij &zfs_arc_average_blocksize, 0, 402269230Sdelphij "ARC average blocksize"); 403273026SdelphijSYSCTL_INT(_vfs_zfs, OID_AUTO, arc_shrink_shift, CTLFLAG_RW, 404273026Sdelphij &arc_shrink_shift, 0, 405273026Sdelphij "log2(fraction of arc to reclaim)"); 406323667SbaptSYSCTL_INT(_vfs_zfs, OID_AUTO, arc_grow_retry, CTLFLAG_RW, 407323667Sbapt &arc_grow_retry, 0, 408323667Sbapt "Wait in seconds before considering growing ARC"); 409307265SmavSYSCTL_INT(_vfs_zfs, OID_AUTO, compressed_arc_enabled, CTLFLAG_RDTUN, 410307265Smav &zfs_compressed_arc_enabled, 0, "Enable compressed ARC"); 411273026Sdelphij 412270759Ssmh/* 413270759Ssmh * We don't have a tunable for arc_free_target due to the dependency on 414270759Ssmh * pagedaemon initialisation. 415270759Ssmh */ 416270759SsmhSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_free_target, 417270759Ssmh CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(u_int), 418270759Ssmh sysctl_vfs_zfs_arc_free_target, "IU", 419270759Ssmh "Desired number of free pages below which ARC triggers reclaim"); 420168404Spjd 421270759Ssmhstatic int 422270759Ssmhsysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS) 423270759Ssmh{ 424270759Ssmh u_int val; 425270759Ssmh int err; 426270759Ssmh 427270759Ssmh val = zfs_arc_free_target; 428270759Ssmh err = sysctl_handle_int(oidp, &val, 0, req); 429270759Ssmh if (err != 0 || req->newptr == NULL) 430270759Ssmh return (err); 431270759Ssmh 432272483Ssmh if (val < minfree) 433270759Ssmh return (EINVAL); 434272483Ssmh if (val > vm_cnt.v_page_count) 435270759Ssmh return (EINVAL); 436270759Ssmh 437270759Ssmh zfs_arc_free_target = val; 438270759Ssmh 439270759Ssmh return (0); 440270759Ssmh} 441275748Sdelphij 442275748Sdelphij/* 443275748Sdelphij * Must be declared here, before the definition of corresponding kstat 444275748Sdelphij * macro which uses the same names will confuse the compiler. 445275748Sdelphij */ 446275748SdelphijSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_meta_limit, 447275748Sdelphij CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t), 448275748Sdelphij sysctl_vfs_zfs_arc_meta_limit, "QU", 449275748Sdelphij "ARC metadata limit"); 450272483Ssmh#endif 451270759Ssmh 452168404Spjd/* 453185029Spjd * Note that buffers can be in one of 6 states: 454168404Spjd * ARC_anon - anonymous (discussed below) 455168404Spjd * ARC_mru - recently used, currently cached 456168404Spjd * ARC_mru_ghost - recentely used, no longer in cache 457168404Spjd * ARC_mfu - frequently used, currently cached 458168404Spjd * ARC_mfu_ghost - frequently used, no longer in cache 459185029Spjd * ARC_l2c_only - exists in L2ARC but not other states 460185029Spjd * When there are no active references to the buffer, they are 461185029Spjd * are linked onto a list in one of these arc states. These are 462185029Spjd * the only buffers that can be evicted or deleted. Within each 463185029Spjd * state there are multiple lists, one for meta-data and one for 464185029Spjd * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 465185029Spjd * etc.) is tracked separately so that it can be managed more 466185029Spjd * explicitly: favored over data, limited explicitly. 467168404Spjd * 468168404Spjd * Anonymous buffers are buffers that are not associated with 469168404Spjd * a DVA. These are buffers that hold dirty block copies 470168404Spjd * before they are written to stable storage. By definition, 471168404Spjd * they are "ref'd" and are considered part of arc_mru 472168404Spjd * that cannot be freed. Generally, they will aquire a DVA 473168404Spjd * as they are written and migrate onto the arc_mru list. 474185029Spjd * 475185029Spjd * The ARC_l2c_only state is for buffers that are in the second 476185029Spjd * level ARC but no longer in any of the ARC_m* lists. The second 477185029Spjd * level ARC itself may also contain buffers that are in any of 478185029Spjd * the ARC_m* states - meaning that a buffer can exist in two 479185029Spjd * places. The reason for the ARC_l2c_only state is to keep the 480185029Spjd * buffer header in the hash table, so that reads that hit the 481185029Spjd * second level ARC benefit from these fast lookups. 482168404Spjd */ 483168404Spjd 484168404Spjdtypedef struct arc_state { 485286763Smav /* 486286763Smav * list of evictable buffers 487286763Smav */ 488321553Smav multilist_t *arcs_list[ARC_BUFC_NUMTYPES]; 489286763Smav /* 490286763Smav * total amount of evictable data in this state 491286763Smav */ 492307265Smav refcount_t arcs_esize[ARC_BUFC_NUMTYPES]; 493286763Smav /* 494286763Smav * total amount of data in this state; this includes: evictable, 495286763Smav * non-evictable, ARC_BUFC_DATA, and ARC_BUFC_METADATA. 496286763Smav */ 497286766Smav refcount_t arcs_size; 498168404Spjd} arc_state_t; 499168404Spjd 500185029Spjd/* The 6 states: */ 501168404Spjdstatic arc_state_t ARC_anon; 502168404Spjdstatic arc_state_t ARC_mru; 503168404Spjdstatic arc_state_t ARC_mru_ghost; 504168404Spjdstatic arc_state_t ARC_mfu; 505168404Spjdstatic arc_state_t ARC_mfu_ghost; 506185029Spjdstatic arc_state_t ARC_l2c_only; 507168404Spjd 508168404Spjdtypedef struct arc_stats { 509168404Spjd kstat_named_t arcstat_hits; 510168404Spjd kstat_named_t arcstat_misses; 511168404Spjd kstat_named_t arcstat_demand_data_hits; 512168404Spjd kstat_named_t arcstat_demand_data_misses; 513168404Spjd kstat_named_t arcstat_demand_metadata_hits; 514168404Spjd kstat_named_t arcstat_demand_metadata_misses; 515168404Spjd kstat_named_t arcstat_prefetch_data_hits; 516168404Spjd kstat_named_t arcstat_prefetch_data_misses; 517168404Spjd kstat_named_t arcstat_prefetch_metadata_hits; 518168404Spjd kstat_named_t arcstat_prefetch_metadata_misses; 519168404Spjd kstat_named_t arcstat_mru_hits; 520168404Spjd kstat_named_t arcstat_mru_ghost_hits; 521168404Spjd kstat_named_t arcstat_mfu_hits; 522168404Spjd kstat_named_t arcstat_mfu_ghost_hits; 523205231Skmacy kstat_named_t arcstat_allocated; 524168404Spjd kstat_named_t arcstat_deleted; 525251629Sdelphij /* 526251629Sdelphij * Number of buffers that could not be evicted because the hash lock 527251629Sdelphij * was held by another thread. The lock may not necessarily be held 528251629Sdelphij * by something using the same buffer, since hash locks are shared 529251629Sdelphij * by multiple buffers. 530251629Sdelphij */ 531168404Spjd kstat_named_t arcstat_mutex_miss; 532251629Sdelphij /* 533251629Sdelphij * Number of buffers skipped because they have I/O in progress, are 534251629Sdelphij * indrect prefetch buffers that have not lived long enough, or are 535251629Sdelphij * not from the spa we're trying to evict from. 536251629Sdelphij */ 537168404Spjd kstat_named_t arcstat_evict_skip; 538286763Smav /* 539286763Smav * Number of times arc_evict_state() was unable to evict enough 540286763Smav * buffers to reach it's target amount. 541286763Smav */ 542286763Smav kstat_named_t arcstat_evict_not_enough; 543208373Smm kstat_named_t arcstat_evict_l2_cached; 544208373Smm kstat_named_t arcstat_evict_l2_eligible; 545208373Smm kstat_named_t arcstat_evict_l2_ineligible; 546286763Smav kstat_named_t arcstat_evict_l2_skip; 547168404Spjd kstat_named_t arcstat_hash_elements; 548168404Spjd kstat_named_t arcstat_hash_elements_max; 549168404Spjd kstat_named_t arcstat_hash_collisions; 550168404Spjd kstat_named_t arcstat_hash_chains; 551168404Spjd kstat_named_t arcstat_hash_chain_max; 552168404Spjd kstat_named_t arcstat_p; 553168404Spjd kstat_named_t arcstat_c; 554168404Spjd kstat_named_t arcstat_c_min; 555168404Spjd kstat_named_t arcstat_c_max; 556168404Spjd kstat_named_t arcstat_size; 557286574Smav /* 558321610Smav * Number of compressed bytes stored in the arc_buf_hdr_t's b_pabd. 559307265Smav * Note that the compressed bytes may match the uncompressed bytes 560307265Smav * if the block is either not compressed or compressed arc is disabled. 561307265Smav */ 562307265Smav kstat_named_t arcstat_compressed_size; 563307265Smav /* 564321610Smav * Uncompressed size of the data stored in b_pabd. If compressed 565307265Smav * arc is disabled then this value will be identical to the stat 566307265Smav * above. 567307265Smav */ 568307265Smav kstat_named_t arcstat_uncompressed_size; 569307265Smav /* 570307265Smav * Number of bytes stored in all the arc_buf_t's. This is classified 571307265Smav * as "overhead" since this data is typically short-lived and will 572307265Smav * be evicted from the arc when it becomes unreferenced unless the 573307265Smav * zfs_keep_uncompressed_metadata or zfs_keep_uncompressed_level 574307265Smav * values have been set (see comment in dbuf.c for more information). 575307265Smav */ 576307265Smav kstat_named_t arcstat_overhead_size; 577307265Smav /* 578286574Smav * Number of bytes consumed by internal ARC structures necessary 579286574Smav * for tracking purposes; these structures are not actually 580286574Smav * backed by ARC buffers. This includes arc_buf_hdr_t structures 581286574Smav * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only 582286574Smav * caches), and arc_buf_t structures (allocated via arc_buf_t 583286574Smav * cache). 584286574Smav */ 585185029Spjd kstat_named_t arcstat_hdr_size; 586286574Smav /* 587286574Smav * Number of bytes consumed by ARC buffers of type equal to 588286574Smav * ARC_BUFC_DATA. This is generally consumed by buffers backing 589286574Smav * on disk user data (e.g. plain file contents). 590286574Smav */ 591208373Smm kstat_named_t arcstat_data_size; 592286574Smav /* 593286574Smav * Number of bytes consumed by ARC buffers of type equal to 594286574Smav * ARC_BUFC_METADATA. This is generally consumed by buffers 595286574Smav * backing on disk data that is used for internal ZFS 596286574Smav * structures (e.g. ZAP, dnode, indirect blocks, etc). 597286574Smav */ 598286574Smav kstat_named_t arcstat_metadata_size; 599286574Smav /* 600286574Smav * Number of bytes consumed by various buffers and structures 601286574Smav * not actually backed with ARC buffers. This includes bonus 602286574Smav * buffers (allocated directly via zio_buf_* functions), 603286574Smav * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t 604286574Smav * cache), and dnode_t structures (allocated via dnode_t cache). 605286574Smav */ 606208373Smm kstat_named_t arcstat_other_size; 607286574Smav /* 608286574Smav * Total number of bytes consumed by ARC buffers residing in the 609286574Smav * arc_anon state. This includes *all* buffers in the arc_anon 610286574Smav * state; e.g. data, metadata, evictable, and unevictable buffers 611286574Smav * are all included in this value. 612286574Smav */ 613286574Smav kstat_named_t arcstat_anon_size; 614286574Smav /* 615286574Smav * Number of bytes consumed by ARC buffers that meet the 616286574Smav * following criteria: backing buffers of type ARC_BUFC_DATA, 617286574Smav * residing in the arc_anon state, and are eligible for eviction 618286574Smav * (e.g. have no outstanding holds on the buffer). 619286574Smav */ 620286574Smav kstat_named_t arcstat_anon_evictable_data; 621286574Smav /* 622286574Smav * Number of bytes consumed by ARC buffers that meet the 623286574Smav * following criteria: backing buffers of type ARC_BUFC_METADATA, 624286574Smav * residing in the arc_anon state, and are eligible for eviction 625286574Smav * (e.g. have no outstanding holds on the buffer). 626286574Smav */ 627286574Smav kstat_named_t arcstat_anon_evictable_metadata; 628286574Smav /* 629286574Smav * Total number of bytes consumed by ARC buffers residing in the 630286574Smav * arc_mru state. This includes *all* buffers in the arc_mru 631286574Smav * state; e.g. data, metadata, evictable, and unevictable buffers 632286574Smav * are all included in this value. 633286574Smav */ 634286574Smav kstat_named_t arcstat_mru_size; 635286574Smav /* 636286574Smav * Number of bytes consumed by ARC buffers that meet the 637286574Smav * following criteria: backing buffers of type ARC_BUFC_DATA, 638286574Smav * residing in the arc_mru state, and are eligible for eviction 639286574Smav * (e.g. have no outstanding holds on the buffer). 640286574Smav */ 641286574Smav kstat_named_t arcstat_mru_evictable_data; 642286574Smav /* 643286574Smav * Number of bytes consumed by ARC buffers that meet the 644286574Smav * following criteria: backing buffers of type ARC_BUFC_METADATA, 645286574Smav * residing in the arc_mru state, and are eligible for eviction 646286574Smav * (e.g. have no outstanding holds on the buffer). 647286574Smav */ 648286574Smav kstat_named_t arcstat_mru_evictable_metadata; 649286574Smav /* 650286574Smav * Total number of bytes that *would have been* consumed by ARC 651286574Smav * buffers in the arc_mru_ghost state. The key thing to note 652286574Smav * here, is the fact that this size doesn't actually indicate 653286574Smav * RAM consumption. The ghost lists only consist of headers and 654286574Smav * don't actually have ARC buffers linked off of these headers. 655286574Smav * Thus, *if* the headers had associated ARC buffers, these 656286574Smav * buffers *would have* consumed this number of bytes. 657286574Smav */ 658286574Smav kstat_named_t arcstat_mru_ghost_size; 659286574Smav /* 660286574Smav * Number of bytes that *would have been* consumed by ARC 661286574Smav * buffers that are eligible for eviction, of type 662286574Smav * ARC_BUFC_DATA, and linked off the arc_mru_ghost state. 663286574Smav */ 664286574Smav kstat_named_t arcstat_mru_ghost_evictable_data; 665286574Smav /* 666286574Smav * Number of bytes that *would have been* consumed by ARC 667286574Smav * buffers that are eligible for eviction, of type 668286574Smav * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 669286574Smav */ 670286574Smav kstat_named_t arcstat_mru_ghost_evictable_metadata; 671286574Smav /* 672286574Smav * Total number of bytes consumed by ARC buffers residing in the 673286574Smav * arc_mfu state. This includes *all* buffers in the arc_mfu 674286574Smav * state; e.g. data, metadata, evictable, and unevictable buffers 675286574Smav * are all included in this value. 676286574Smav */ 677286574Smav kstat_named_t arcstat_mfu_size; 678286574Smav /* 679286574Smav * Number of bytes consumed by ARC buffers that are eligible for 680286574Smav * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu 681286574Smav * state. 682286574Smav */ 683286574Smav kstat_named_t arcstat_mfu_evictable_data; 684286574Smav /* 685286574Smav * Number of bytes consumed by ARC buffers that are eligible for 686286574Smav * eviction, of type ARC_BUFC_METADATA, and reside in the 687286574Smav * arc_mfu state. 688286574Smav */ 689286574Smav kstat_named_t arcstat_mfu_evictable_metadata; 690286574Smav /* 691286574Smav * Total number of bytes that *would have been* consumed by ARC 692286574Smav * buffers in the arc_mfu_ghost state. See the comment above 693286574Smav * arcstat_mru_ghost_size for more details. 694286574Smav */ 695286574Smav kstat_named_t arcstat_mfu_ghost_size; 696286574Smav /* 697286574Smav * Number of bytes that *would have been* consumed by ARC 698286574Smav * buffers that are eligible for eviction, of type 699286574Smav * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state. 700286574Smav */ 701286574Smav kstat_named_t arcstat_mfu_ghost_evictable_data; 702286574Smav /* 703286574Smav * Number of bytes that *would have been* consumed by ARC 704286574Smav * buffers that are eligible for eviction, of type 705286574Smav * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 706286574Smav */ 707286574Smav kstat_named_t arcstat_mfu_ghost_evictable_metadata; 708185029Spjd kstat_named_t arcstat_l2_hits; 709185029Spjd kstat_named_t arcstat_l2_misses; 710185029Spjd kstat_named_t arcstat_l2_feeds; 711185029Spjd kstat_named_t arcstat_l2_rw_clash; 712208373Smm kstat_named_t arcstat_l2_read_bytes; 713208373Smm kstat_named_t arcstat_l2_write_bytes; 714185029Spjd kstat_named_t arcstat_l2_writes_sent; 715185029Spjd kstat_named_t arcstat_l2_writes_done; 716185029Spjd kstat_named_t arcstat_l2_writes_error; 717286763Smav kstat_named_t arcstat_l2_writes_lock_retry; 718185029Spjd kstat_named_t arcstat_l2_evict_lock_retry; 719185029Spjd kstat_named_t arcstat_l2_evict_reading; 720286570Smav kstat_named_t arcstat_l2_evict_l1cached; 721185029Spjd kstat_named_t arcstat_l2_free_on_write; 722185029Spjd kstat_named_t arcstat_l2_abort_lowmem; 723185029Spjd kstat_named_t arcstat_l2_cksum_bad; 724185029Spjd kstat_named_t arcstat_l2_io_error; 725323754Savg kstat_named_t arcstat_l2_lsize; 726323754Savg kstat_named_t arcstat_l2_psize; 727185029Spjd kstat_named_t arcstat_l2_hdr_size; 728205231Skmacy kstat_named_t arcstat_l2_write_trylock_fail; 729205231Skmacy kstat_named_t arcstat_l2_write_passed_headroom; 730205231Skmacy kstat_named_t arcstat_l2_write_spa_mismatch; 731206796Spjd kstat_named_t arcstat_l2_write_in_l2; 732205231Skmacy kstat_named_t arcstat_l2_write_hdr_io_in_progress; 733205231Skmacy kstat_named_t arcstat_l2_write_not_cacheable; 734205231Skmacy kstat_named_t arcstat_l2_write_full; 735205231Skmacy kstat_named_t arcstat_l2_write_buffer_iter; 736205231Skmacy kstat_named_t arcstat_l2_write_pios; 737205231Skmacy kstat_named_t arcstat_l2_write_buffer_bytes_scanned; 738205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_iter; 739205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_null_iter; 740242845Sdelphij kstat_named_t arcstat_memory_throttle_count; 741275748Sdelphij kstat_named_t arcstat_meta_used; 742275748Sdelphij kstat_named_t arcstat_meta_limit; 743275748Sdelphij kstat_named_t arcstat_meta_max; 744275780Sdelphij kstat_named_t arcstat_meta_min; 745287702Sdelphij kstat_named_t arcstat_sync_wait_for_async; 746287702Sdelphij kstat_named_t arcstat_demand_hit_predictive_prefetch; 747168404Spjd} arc_stats_t; 748168404Spjd 749168404Spjdstatic arc_stats_t arc_stats = { 750168404Spjd { "hits", KSTAT_DATA_UINT64 }, 751168404Spjd { "misses", KSTAT_DATA_UINT64 }, 752168404Spjd { "demand_data_hits", KSTAT_DATA_UINT64 }, 753168404Spjd { "demand_data_misses", KSTAT_DATA_UINT64 }, 754168404Spjd { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 755168404Spjd { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 756168404Spjd { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 757168404Spjd { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 758168404Spjd { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 759168404Spjd { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 760168404Spjd { "mru_hits", KSTAT_DATA_UINT64 }, 761168404Spjd { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 762168404Spjd { "mfu_hits", KSTAT_DATA_UINT64 }, 763168404Spjd { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 764205231Skmacy { "allocated", KSTAT_DATA_UINT64 }, 765168404Spjd { "deleted", KSTAT_DATA_UINT64 }, 766168404Spjd { "mutex_miss", KSTAT_DATA_UINT64 }, 767168404Spjd { "evict_skip", KSTAT_DATA_UINT64 }, 768286763Smav { "evict_not_enough", KSTAT_DATA_UINT64 }, 769208373Smm { "evict_l2_cached", KSTAT_DATA_UINT64 }, 770208373Smm { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 771208373Smm { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 772286763Smav { "evict_l2_skip", KSTAT_DATA_UINT64 }, 773168404Spjd { "hash_elements", KSTAT_DATA_UINT64 }, 774168404Spjd { "hash_elements_max", KSTAT_DATA_UINT64 }, 775168404Spjd { "hash_collisions", KSTAT_DATA_UINT64 }, 776168404Spjd { "hash_chains", KSTAT_DATA_UINT64 }, 777168404Spjd { "hash_chain_max", KSTAT_DATA_UINT64 }, 778168404Spjd { "p", KSTAT_DATA_UINT64 }, 779168404Spjd { "c", KSTAT_DATA_UINT64 }, 780168404Spjd { "c_min", KSTAT_DATA_UINT64 }, 781168404Spjd { "c_max", KSTAT_DATA_UINT64 }, 782185029Spjd { "size", KSTAT_DATA_UINT64 }, 783307265Smav { "compressed_size", KSTAT_DATA_UINT64 }, 784307265Smav { "uncompressed_size", KSTAT_DATA_UINT64 }, 785307265Smav { "overhead_size", KSTAT_DATA_UINT64 }, 786185029Spjd { "hdr_size", KSTAT_DATA_UINT64 }, 787208373Smm { "data_size", KSTAT_DATA_UINT64 }, 788286574Smav { "metadata_size", KSTAT_DATA_UINT64 }, 789208373Smm { "other_size", KSTAT_DATA_UINT64 }, 790286574Smav { "anon_size", KSTAT_DATA_UINT64 }, 791286574Smav { "anon_evictable_data", KSTAT_DATA_UINT64 }, 792286574Smav { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, 793286574Smav { "mru_size", KSTAT_DATA_UINT64 }, 794286574Smav { "mru_evictable_data", KSTAT_DATA_UINT64 }, 795286574Smav { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, 796286574Smav { "mru_ghost_size", KSTAT_DATA_UINT64 }, 797286574Smav { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, 798286574Smav { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 799286574Smav { "mfu_size", KSTAT_DATA_UINT64 }, 800286574Smav { "mfu_evictable_data", KSTAT_DATA_UINT64 }, 801286574Smav { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, 802286574Smav { "mfu_ghost_size", KSTAT_DATA_UINT64 }, 803286574Smav { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, 804286574Smav { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 805185029Spjd { "l2_hits", KSTAT_DATA_UINT64 }, 806185029Spjd { "l2_misses", KSTAT_DATA_UINT64 }, 807185029Spjd { "l2_feeds", KSTAT_DATA_UINT64 }, 808185029Spjd { "l2_rw_clash", KSTAT_DATA_UINT64 }, 809208373Smm { "l2_read_bytes", KSTAT_DATA_UINT64 }, 810208373Smm { "l2_write_bytes", KSTAT_DATA_UINT64 }, 811185029Spjd { "l2_writes_sent", KSTAT_DATA_UINT64 }, 812185029Spjd { "l2_writes_done", KSTAT_DATA_UINT64 }, 813185029Spjd { "l2_writes_error", KSTAT_DATA_UINT64 }, 814286763Smav { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, 815185029Spjd { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 816185029Spjd { "l2_evict_reading", KSTAT_DATA_UINT64 }, 817286570Smav { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, 818185029Spjd { "l2_free_on_write", KSTAT_DATA_UINT64 }, 819185029Spjd { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 820185029Spjd { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 821185029Spjd { "l2_io_error", KSTAT_DATA_UINT64 }, 822185029Spjd { "l2_size", KSTAT_DATA_UINT64 }, 823251478Sdelphij { "l2_asize", KSTAT_DATA_UINT64 }, 824185029Spjd { "l2_hdr_size", KSTAT_DATA_UINT64 }, 825206796Spjd { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 826206796Spjd { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 827206796Spjd { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 828206796Spjd { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 829206796Spjd { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 830206796Spjd { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 831206796Spjd { "l2_write_full", KSTAT_DATA_UINT64 }, 832206796Spjd { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 833206796Spjd { "l2_write_pios", KSTAT_DATA_UINT64 }, 834206796Spjd { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 835206796Spjd { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 836242845Sdelphij { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 }, 837242845Sdelphij { "memory_throttle_count", KSTAT_DATA_UINT64 }, 838275748Sdelphij { "arc_meta_used", KSTAT_DATA_UINT64 }, 839275748Sdelphij { "arc_meta_limit", KSTAT_DATA_UINT64 }, 840275780Sdelphij { "arc_meta_max", KSTAT_DATA_UINT64 }, 841287702Sdelphij { "arc_meta_min", KSTAT_DATA_UINT64 }, 842287702Sdelphij { "sync_wait_for_async", KSTAT_DATA_UINT64 }, 843287702Sdelphij { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 }, 844168404Spjd}; 845168404Spjd 846168404Spjd#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 847168404Spjd 848168404Spjd#define ARCSTAT_INCR(stat, val) \ 849251631Sdelphij atomic_add_64(&arc_stats.stat.value.ui64, (val)) 850168404Spjd 851206796Spjd#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 852168404Spjd#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 853168404Spjd 854168404Spjd#define ARCSTAT_MAX(stat, val) { \ 855168404Spjd uint64_t m; \ 856168404Spjd while ((val) > (m = arc_stats.stat.value.ui64) && \ 857168404Spjd (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 858168404Spjd continue; \ 859168404Spjd} 860168404Spjd 861168404Spjd#define ARCSTAT_MAXSTAT(stat) \ 862168404Spjd ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 863168404Spjd 864168404Spjd/* 865168404Spjd * We define a macro to allow ARC hits/misses to be easily broken down by 866168404Spjd * two separate conditions, giving a total of four different subtypes for 867168404Spjd * each of hits and misses (so eight statistics total). 868168404Spjd */ 869168404Spjd#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 870168404Spjd if (cond1) { \ 871168404Spjd if (cond2) { \ 872168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 873168404Spjd } else { \ 874168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 875168404Spjd } \ 876168404Spjd } else { \ 877168404Spjd if (cond2) { \ 878168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 879168404Spjd } else { \ 880168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 881168404Spjd } \ 882168404Spjd } 883168404Spjd 884168404Spjdkstat_t *arc_ksp; 885206796Spjdstatic arc_state_t *arc_anon; 886168404Spjdstatic arc_state_t *arc_mru; 887168404Spjdstatic arc_state_t *arc_mru_ghost; 888168404Spjdstatic arc_state_t *arc_mfu; 889168404Spjdstatic arc_state_t *arc_mfu_ghost; 890185029Spjdstatic arc_state_t *arc_l2c_only; 891168404Spjd 892168404Spjd/* 893168404Spjd * There are several ARC variables that are critical to export as kstats -- 894168404Spjd * but we don't want to have to grovel around in the kstat whenever we wish to 895168404Spjd * manipulate them. For these variables, we therefore define them to be in 896168404Spjd * terms of the statistic variable. This assures that we are not introducing 897168404Spjd * the possibility of inconsistency by having shadow copies of the variables, 898168404Spjd * while still allowing the code to be readable. 899168404Spjd */ 900168404Spjd#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 901168404Spjd#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 902168404Spjd#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 903168404Spjd#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 904168404Spjd#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 905275748Sdelphij#define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ 906275780Sdelphij#define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ 907275748Sdelphij#define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */ 908275748Sdelphij#define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ 909168404Spjd 910307265Smav/* compressed size of entire arc */ 911307265Smav#define arc_compressed_size ARCSTAT(arcstat_compressed_size) 912307265Smav/* uncompressed size of entire arc */ 913307265Smav#define arc_uncompressed_size ARCSTAT(arcstat_uncompressed_size) 914307265Smav/* number of bytes in the arc from arc_buf_t's */ 915307265Smav#define arc_overhead_size ARCSTAT(arcstat_overhead_size) 916251478Sdelphij 917168404Spjdstatic int arc_no_grow; /* Don't try to grow cache size */ 918168404Spjdstatic uint64_t arc_tempreserve; 919209962Smmstatic uint64_t arc_loaned_bytes; 920168404Spjd 921168404Spjdtypedef struct arc_callback arc_callback_t; 922168404Spjd 923168404Spjdstruct arc_callback { 924168404Spjd void *acb_private; 925168404Spjd arc_done_func_t *acb_done; 926168404Spjd arc_buf_t *acb_buf; 927321535Smav boolean_t acb_compressed; 928168404Spjd zio_t *acb_zio_dummy; 929168404Spjd arc_callback_t *acb_next; 930168404Spjd}; 931168404Spjd 932168404Spjdtypedef struct arc_write_callback arc_write_callback_t; 933168404Spjd 934168404Spjdstruct arc_write_callback { 935168404Spjd void *awcb_private; 936168404Spjd arc_done_func_t *awcb_ready; 937304138Savg arc_done_func_t *awcb_children_ready; 938258632Savg arc_done_func_t *awcb_physdone; 939168404Spjd arc_done_func_t *awcb_done; 940168404Spjd arc_buf_t *awcb_buf; 941168404Spjd}; 942168404Spjd 943286570Smav/* 944286570Smav * ARC buffers are separated into multiple structs as a memory saving measure: 945286570Smav * - Common fields struct, always defined, and embedded within it: 946286570Smav * - L2-only fields, always allocated but undefined when not in L2ARC 947286570Smav * - L1-only fields, only allocated when in L1ARC 948286570Smav * 949286570Smav * Buffer in L1 Buffer only in L2 950286570Smav * +------------------------+ +------------------------+ 951286570Smav * | arc_buf_hdr_t | | arc_buf_hdr_t | 952286570Smav * | | | | 953286570Smav * | | | | 954286570Smav * | | | | 955286570Smav * +------------------------+ +------------------------+ 956286570Smav * | l2arc_buf_hdr_t | | l2arc_buf_hdr_t | 957286570Smav * | (undefined if L1-only) | | | 958286570Smav * +------------------------+ +------------------------+ 959286570Smav * | l1arc_buf_hdr_t | 960286570Smav * | | 961286570Smav * | | 962286570Smav * | | 963286570Smav * | | 964286570Smav * +------------------------+ 965286570Smav * 966286570Smav * Because it's possible for the L2ARC to become extremely large, we can wind 967286570Smav * up eating a lot of memory in L2ARC buffer headers, so the size of a header 968286570Smav * is minimized by only allocating the fields necessary for an L1-cached buffer 969286570Smav * when a header is actually in the L1 cache. The sub-headers (l1arc_buf_hdr and 970286570Smav * l2arc_buf_hdr) are embedded rather than allocated separately to save a couple 971286570Smav * words in pointers. arc_hdr_realloc() is used to switch a header between 972286570Smav * these two allocation states. 973286570Smav */ 974286570Smavtypedef struct l1arc_buf_hdr { 975168404Spjd kmutex_t b_freeze_lock; 976307265Smav zio_cksum_t *b_freeze_cksum; 977286570Smav#ifdef ZFS_DEBUG 978286570Smav /* 979321535Smav * Used for debugging with kmem_flags - by allocating and freeing 980286570Smav * b_thawed when the buffer is thawed, we get a record of the stack 981286570Smav * trace that thawed it. 982286570Smav */ 983219089Spjd void *b_thawed; 984286570Smav#endif 985168404Spjd 986168404Spjd arc_buf_t *b_buf; 987307265Smav uint32_t b_bufcnt; 988286570Smav /* for waiting on writes to complete */ 989168404Spjd kcondvar_t b_cv; 990307265Smav uint8_t b_byteswap; 991168404Spjd 992168404Spjd /* protected by arc state mutex */ 993168404Spjd arc_state_t *b_state; 994286763Smav multilist_node_t b_arc_node; 995168404Spjd 996168404Spjd /* updated atomically */ 997168404Spjd clock_t b_arc_access; 998168404Spjd 999168404Spjd /* self protecting */ 1000168404Spjd refcount_t b_refcnt; 1001185029Spjd 1002286570Smav arc_callback_t *b_acb; 1003321610Smav abd_t *b_pabd; 1004286570Smav} l1arc_buf_hdr_t; 1005286570Smav 1006286570Smavtypedef struct l2arc_dev l2arc_dev_t; 1007286570Smav 1008286570Smavtypedef struct l2arc_buf_hdr { 1009286570Smav /* protected by arc_buf_hdr mutex */ 1010286570Smav l2arc_dev_t *b_dev; /* L2ARC device */ 1011286570Smav uint64_t b_daddr; /* disk address, offset byte */ 1012286570Smav 1013185029Spjd list_node_t b_l2node; 1014286570Smav} l2arc_buf_hdr_t; 1015286570Smav 1016286570Smavstruct arc_buf_hdr { 1017286570Smav /* protected by hash lock */ 1018286570Smav dva_t b_dva; 1019286570Smav uint64_t b_birth; 1020286570Smav 1021307265Smav arc_buf_contents_t b_type; 1022286570Smav arc_buf_hdr_t *b_hash_next; 1023286570Smav arc_flags_t b_flags; 1024286570Smav 1025307265Smav /* 1026307265Smav * This field stores the size of the data buffer after 1027307265Smav * compression, and is set in the arc's zio completion handlers. 1028307265Smav * It is in units of SPA_MINBLOCKSIZE (e.g. 1 == 512 bytes). 1029307265Smav * 1030307265Smav * While the block pointers can store up to 32MB in their psize 1031307265Smav * field, we can only store up to 32MB minus 512B. This is due 1032307265Smav * to the bp using a bias of 1, whereas we use a bias of 0 (i.e. 1033307265Smav * a field of zeros represents 512B in the bp). We can't use a 1034307265Smav * bias of 1 since we need to reserve a psize of zero, here, to 1035307265Smav * represent holes and embedded blocks. 1036307265Smav * 1037307265Smav * This isn't a problem in practice, since the maximum size of a 1038307265Smav * buffer is limited to 16MB, so we never need to store 32MB in 1039307265Smav * this field. Even in the upstream illumos code base, the 1040307265Smav * maximum size of a buffer is limited to 16MB. 1041307265Smav */ 1042307265Smav uint16_t b_psize; 1043286570Smav 1044307265Smav /* 1045307265Smav * This field stores the size of the data buffer before 1046307265Smav * compression, and cannot change once set. It is in units 1047307265Smav * of SPA_MINBLOCKSIZE (e.g. 2 == 1024 bytes) 1048307265Smav */ 1049307265Smav uint16_t b_lsize; /* immutable */ 1050307265Smav uint64_t b_spa; /* immutable */ 1051307265Smav 1052286570Smav /* L2ARC fields. Undefined when not in L2ARC. */ 1053286570Smav l2arc_buf_hdr_t b_l2hdr; 1054286570Smav /* L1ARC fields. Undefined when in l2arc_only state */ 1055286570Smav l1arc_buf_hdr_t b_l1hdr; 1056168404Spjd}; 1057168404Spjd 1058302265Ssmh#if defined(__FreeBSD__) && defined(_KERNEL) 1059275748Sdelphijstatic int 1060275748Sdelphijsysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS) 1061275748Sdelphij{ 1062275748Sdelphij uint64_t val; 1063275748Sdelphij int err; 1064275748Sdelphij 1065275748Sdelphij val = arc_meta_limit; 1066275748Sdelphij err = sysctl_handle_64(oidp, &val, 0, req); 1067275748Sdelphij if (err != 0 || req->newptr == NULL) 1068275748Sdelphij return (err); 1069275748Sdelphij 1070275748Sdelphij if (val <= 0 || val > arc_c_max) 1071275748Sdelphij return (EINVAL); 1072275748Sdelphij 1073275748Sdelphij arc_meta_limit = val; 1074275748Sdelphij return (0); 1075275748Sdelphij} 1076302265Ssmh 1077302265Ssmhstatic int 1078323667Sbaptsysctl_vfs_zfs_arc_no_grow_shift(SYSCTL_HANDLER_ARGS) 1079323667Sbapt{ 1080323667Sbapt uint32_t val; 1081323667Sbapt int err; 1082323667Sbapt 1083323667Sbapt val = arc_no_grow_shift; 1084323667Sbapt err = sysctl_handle_32(oidp, &val, 0, req); 1085323667Sbapt if (err != 0 || req->newptr == NULL) 1086323667Sbapt return (err); 1087323667Sbapt 1088323667Sbapt if (val >= arc_shrink_shift) 1089323667Sbapt return (EINVAL); 1090323667Sbapt 1091323667Sbapt arc_no_grow_shift = val; 1092323667Sbapt return (0); 1093323667Sbapt} 1094323667Sbapt 1095323667Sbaptstatic int 1096302265Ssmhsysctl_vfs_zfs_arc_max(SYSCTL_HANDLER_ARGS) 1097302265Ssmh{ 1098302265Ssmh uint64_t val; 1099302265Ssmh int err; 1100302265Ssmh 1101302265Ssmh val = zfs_arc_max; 1102302265Ssmh err = sysctl_handle_64(oidp, &val, 0, req); 1103302265Ssmh if (err != 0 || req->newptr == NULL) 1104302265Ssmh return (err); 1105302265Ssmh 1106302382Ssmh if (zfs_arc_max == 0) { 1107302382Ssmh /* Loader tunable so blindly set */ 1108302382Ssmh zfs_arc_max = val; 1109302382Ssmh return (0); 1110302382Ssmh } 1111302382Ssmh 1112302265Ssmh if (val < arc_abs_min || val > kmem_size()) 1113302265Ssmh return (EINVAL); 1114302265Ssmh if (val < arc_c_min) 1115302265Ssmh return (EINVAL); 1116302265Ssmh if (zfs_arc_meta_limit > 0 && val < zfs_arc_meta_limit) 1117302265Ssmh return (EINVAL); 1118302265Ssmh 1119302265Ssmh arc_c_max = val; 1120302265Ssmh 1121302265Ssmh arc_c = arc_c_max; 1122302265Ssmh arc_p = (arc_c >> 1); 1123302265Ssmh 1124302265Ssmh if (zfs_arc_meta_limit == 0) { 1125302265Ssmh /* limit meta-data to 1/4 of the arc capacity */ 1126302265Ssmh arc_meta_limit = arc_c_max / 4; 1127302265Ssmh } 1128302265Ssmh 1129302265Ssmh /* if kmem_flags are set, lets try to use less memory */ 1130302265Ssmh if (kmem_debugging()) 1131302265Ssmh arc_c = arc_c / 2; 1132302265Ssmh 1133302265Ssmh zfs_arc_max = arc_c; 1134302265Ssmh 1135302265Ssmh return (0); 1136302265Ssmh} 1137302265Ssmh 1138302265Ssmhstatic int 1139302265Ssmhsysctl_vfs_zfs_arc_min(SYSCTL_HANDLER_ARGS) 1140302265Ssmh{ 1141302265Ssmh uint64_t val; 1142302265Ssmh int err; 1143302265Ssmh 1144302265Ssmh val = zfs_arc_min; 1145302265Ssmh err = sysctl_handle_64(oidp, &val, 0, req); 1146302265Ssmh if (err != 0 || req->newptr == NULL) 1147302265Ssmh return (err); 1148302265Ssmh 1149302382Ssmh if (zfs_arc_min == 0) { 1150302382Ssmh /* Loader tunable so blindly set */ 1151302382Ssmh zfs_arc_min = val; 1152302382Ssmh return (0); 1153302382Ssmh } 1154302382Ssmh 1155302265Ssmh if (val < arc_abs_min || val > arc_c_max) 1156302265Ssmh return (EINVAL); 1157302265Ssmh 1158302265Ssmh arc_c_min = val; 1159302265Ssmh 1160302265Ssmh if (zfs_arc_meta_min == 0) 1161302265Ssmh arc_meta_min = arc_c_min / 2; 1162302265Ssmh 1163302265Ssmh if (arc_c < arc_c_min) 1164302265Ssmh arc_c = arc_c_min; 1165302265Ssmh 1166302265Ssmh zfs_arc_min = arc_c_min; 1167302265Ssmh 1168302265Ssmh return (0); 1169302265Ssmh} 1170275748Sdelphij#endif 1171275748Sdelphij 1172168404Spjd#define GHOST_STATE(state) \ 1173185029Spjd ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 1174185029Spjd (state) == arc_l2c_only) 1175168404Spjd 1176275811Sdelphij#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) 1177275811Sdelphij#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) 1178275811Sdelphij#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) 1179275811Sdelphij#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) 1180307265Smav#define HDR_COMPRESSION_ENABLED(hdr) \ 1181307265Smav ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC) 1182286570Smav 1183275811Sdelphij#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) 1184275811Sdelphij#define HDR_L2_READING(hdr) \ 1185307265Smav (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ 1186307265Smav ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) 1187275811Sdelphij#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) 1188275811Sdelphij#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) 1189275811Sdelphij#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) 1190307265Smav#define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA) 1191168404Spjd 1192286570Smav#define HDR_ISTYPE_METADATA(hdr) \ 1193307265Smav ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) 1194286570Smav#define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) 1195286570Smav 1196286570Smav#define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) 1197286570Smav#define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) 1198286570Smav 1199307265Smav/* For storing compression mode in b_flags */ 1200307265Smav#define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1) 1201307265Smav 1202307265Smav#define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \ 1203307265Smav HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS)) 1204307265Smav#define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \ 1205307265Smav HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp)); 1206307265Smav 1207307265Smav#define ARC_BUF_LAST(buf) ((buf)->b_next == NULL) 1208321535Smav#define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED) 1209321535Smav#define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED) 1210307265Smav 1211168404Spjd/* 1212185029Spjd * Other sizes 1213185029Spjd */ 1214185029Spjd 1215286570Smav#define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 1216286570Smav#define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) 1217185029Spjd 1218185029Spjd/* 1219168404Spjd * Hash table routines 1220168404Spjd */ 1221168404Spjd 1222205253Skmacy#define HT_LOCK_PAD CACHE_LINE_SIZE 1223168404Spjd 1224168404Spjdstruct ht_lock { 1225168404Spjd kmutex_t ht_lock; 1226168404Spjd#ifdef _KERNEL 1227168404Spjd unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 1228168404Spjd#endif 1229168404Spjd}; 1230168404Spjd 1231168404Spjd#define BUF_LOCKS 256 1232168404Spjdtypedef struct buf_hash_table { 1233168404Spjd uint64_t ht_mask; 1234168404Spjd arc_buf_hdr_t **ht_table; 1235205264Skmacy struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 1236168404Spjd} buf_hash_table_t; 1237168404Spjd 1238168404Spjdstatic buf_hash_table_t buf_hash_table; 1239168404Spjd 1240168404Spjd#define BUF_HASH_INDEX(spa, dva, birth) \ 1241168404Spjd (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 1242168404Spjd#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 1243168404Spjd#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 1244219089Spjd#define HDR_LOCK(hdr) \ 1245219089Spjd (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 1246168404Spjd 1247168404Spjduint64_t zfs_crc64_table[256]; 1248168404Spjd 1249185029Spjd/* 1250185029Spjd * Level 2 ARC 1251185029Spjd */ 1252185029Spjd 1253272707Savg#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 1254251478Sdelphij#define L2ARC_HEADROOM 2 /* num of writes */ 1255251478Sdelphij/* 1256251478Sdelphij * If we discover during ARC scan any buffers to be compressed, we boost 1257251478Sdelphij * our headroom for the next scanning cycle by this percentage multiple. 1258251478Sdelphij */ 1259251478Sdelphij#define L2ARC_HEADROOM_BOOST 200 1260208373Smm#define L2ARC_FEED_SECS 1 /* caching interval secs */ 1261208373Smm#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 1262185029Spjd 1263185029Spjd#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 1264185029Spjd#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 1265185029Spjd 1266251631Sdelphij/* L2ARC Performance Tunables */ 1267185029Spjduint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 1268185029Spjduint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 1269185029Spjduint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 1270251478Sdelphijuint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 1271185029Spjduint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 1272208373Smmuint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 1273219089Spjdboolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 1274208373Smmboolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 1275208373Smmboolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 1276185029Spjd 1277217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 1278205231Skmacy &l2arc_write_max, 0, "max write size"); 1279217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 1280205231Skmacy &l2arc_write_boost, 0, "extra write during warmup"); 1281217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 1282205231Skmacy &l2arc_headroom, 0, "number of dev writes"); 1283217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 1284205231Skmacy &l2arc_feed_secs, 0, "interval seconds"); 1285217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 1286208373Smm &l2arc_feed_min_ms, 0, "min interval milliseconds"); 1287205231Skmacy 1288205231SkmacySYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 1289205231Skmacy &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 1290208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 1291208373Smm &l2arc_feed_again, 0, "turbo warmup"); 1292208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 1293208373Smm &l2arc_norw, 0, "no reads during writes"); 1294205231Skmacy 1295217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 1296286770Smav &ARC_anon.arcs_size.rc_count, 0, "size of anonymous state"); 1297307265SmavSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_esize, CTLFLAG_RD, 1298307265Smav &ARC_anon.arcs_esize[ARC_BUFC_METADATA].rc_count, 0, 1299307265Smav "size of anonymous state"); 1300307265SmavSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_esize, CTLFLAG_RD, 1301307265Smav &ARC_anon.arcs_esize[ARC_BUFC_DATA].rc_count, 0, 1302307265Smav "size of anonymous state"); 1303205231Skmacy 1304217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 1305286770Smav &ARC_mru.arcs_size.rc_count, 0, "size of mru state"); 1306307265SmavSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_esize, CTLFLAG_RD, 1307307265Smav &ARC_mru.arcs_esize[ARC_BUFC_METADATA].rc_count, 0, 1308307265Smav "size of metadata in mru state"); 1309307265SmavSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_esize, CTLFLAG_RD, 1310307265Smav &ARC_mru.arcs_esize[ARC_BUFC_DATA].rc_count, 0, 1311307265Smav "size of data in mru state"); 1312205231Skmacy 1313217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 1314286770Smav &ARC_mru_ghost.arcs_size.rc_count, 0, "size of mru ghost state"); 1315307265SmavSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_esize, CTLFLAG_RD, 1316307265Smav &ARC_mru_ghost.arcs_esize[ARC_BUFC_METADATA].rc_count, 0, 1317205231Skmacy "size of metadata in mru ghost state"); 1318307265SmavSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_esize, CTLFLAG_RD, 1319307265Smav &ARC_mru_ghost.arcs_esize[ARC_BUFC_DATA].rc_count, 0, 1320205231Skmacy "size of data in mru ghost state"); 1321205231Skmacy 1322217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 1323286770Smav &ARC_mfu.arcs_size.rc_count, 0, "size of mfu state"); 1324307265SmavSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_esize, CTLFLAG_RD, 1325307265Smav &ARC_mfu.arcs_esize[ARC_BUFC_METADATA].rc_count, 0, 1326307265Smav "size of metadata in mfu state"); 1327307265SmavSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_esize, CTLFLAG_RD, 1328307265Smav &ARC_mfu.arcs_esize[ARC_BUFC_DATA].rc_count, 0, 1329307265Smav "size of data in mfu state"); 1330205231Skmacy 1331217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 1332286770Smav &ARC_mfu_ghost.arcs_size.rc_count, 0, "size of mfu ghost state"); 1333307265SmavSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_esize, CTLFLAG_RD, 1334307265Smav &ARC_mfu_ghost.arcs_esize[ARC_BUFC_METADATA].rc_count, 0, 1335205231Skmacy "size of metadata in mfu ghost state"); 1336307265SmavSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_esize, CTLFLAG_RD, 1337307265Smav &ARC_mfu_ghost.arcs_esize[ARC_BUFC_DATA].rc_count, 0, 1338205231Skmacy "size of data in mfu ghost state"); 1339205231Skmacy 1340217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 1341286770Smav &ARC_l2c_only.arcs_size.rc_count, 0, "size of mru state"); 1342205231Skmacy 1343185029Spjd/* 1344185029Spjd * L2ARC Internals 1345185029Spjd */ 1346286570Smavstruct l2arc_dev { 1347185029Spjd vdev_t *l2ad_vdev; /* vdev */ 1348185029Spjd spa_t *l2ad_spa; /* spa */ 1349185029Spjd uint64_t l2ad_hand; /* next write location */ 1350185029Spjd uint64_t l2ad_start; /* first addr on device */ 1351185029Spjd uint64_t l2ad_end; /* last addr on device */ 1352185029Spjd boolean_t l2ad_first; /* first sweep through */ 1353208373Smm boolean_t l2ad_writing; /* currently writing */ 1354286570Smav kmutex_t l2ad_mtx; /* lock for buffer list */ 1355286570Smav list_t l2ad_buflist; /* buffer list */ 1356185029Spjd list_node_t l2ad_node; /* device list node */ 1357286598Smav refcount_t l2ad_alloc; /* allocated bytes */ 1358286570Smav}; 1359185029Spjd 1360185029Spjdstatic list_t L2ARC_dev_list; /* device list */ 1361185029Spjdstatic list_t *l2arc_dev_list; /* device list pointer */ 1362185029Spjdstatic kmutex_t l2arc_dev_mtx; /* device list mutex */ 1363185029Spjdstatic l2arc_dev_t *l2arc_dev_last; /* last device used */ 1364185029Spjdstatic list_t L2ARC_free_on_write; /* free after write buf list */ 1365185029Spjdstatic list_t *l2arc_free_on_write; /* free after write list ptr */ 1366185029Spjdstatic kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 1367185029Spjdstatic uint64_t l2arc_ndev; /* number of devices */ 1368185029Spjd 1369185029Spjdtypedef struct l2arc_read_callback { 1370321535Smav arc_buf_hdr_t *l2rcb_hdr; /* read header */ 1371251478Sdelphij blkptr_t l2rcb_bp; /* original blkptr */ 1372268123Sdelphij zbookmark_phys_t l2rcb_zb; /* original bookmark */ 1373251478Sdelphij int l2rcb_flags; /* original flags */ 1374321613Smav abd_t *l2rcb_abd; /* temporary buffer */ 1375185029Spjd} l2arc_read_callback_t; 1376185029Spjd 1377185029Spjdtypedef struct l2arc_write_callback { 1378185029Spjd l2arc_dev_t *l2wcb_dev; /* device info */ 1379185029Spjd arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 1380185029Spjd} l2arc_write_callback_t; 1381185029Spjd 1382185029Spjdtypedef struct l2arc_data_free { 1383185029Spjd /* protected by l2arc_free_on_write_mtx */ 1384321610Smav abd_t *l2df_abd; 1385185029Spjd size_t l2df_size; 1386307265Smav arc_buf_contents_t l2df_type; 1387185029Spjd list_node_t l2df_list_node; 1388185029Spjd} l2arc_data_free_t; 1389185029Spjd 1390185029Spjdstatic kmutex_t l2arc_feed_thr_lock; 1391185029Spjdstatic kcondvar_t l2arc_feed_thr_cv; 1392185029Spjdstatic uint8_t l2arc_thread_exit; 1393185029Spjd 1394321610Smavstatic abd_t *arc_get_data_abd(arc_buf_hdr_t *, uint64_t, void *); 1395307265Smavstatic void *arc_get_data_buf(arc_buf_hdr_t *, uint64_t, void *); 1396321610Smavstatic void arc_get_data_impl(arc_buf_hdr_t *, uint64_t, void *); 1397321610Smavstatic void arc_free_data_abd(arc_buf_hdr_t *, abd_t *, uint64_t, void *); 1398307265Smavstatic void arc_free_data_buf(arc_buf_hdr_t *, void *, uint64_t, void *); 1399321610Smavstatic void arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag); 1400321610Smavstatic void arc_hdr_free_pabd(arc_buf_hdr_t *); 1401321610Smavstatic void arc_hdr_alloc_pabd(arc_buf_hdr_t *); 1402275811Sdelphijstatic void arc_access(arc_buf_hdr_t *, kmutex_t *); 1403286763Smavstatic boolean_t arc_is_overflowing(); 1404275811Sdelphijstatic void arc_buf_watch(arc_buf_t *); 1405275811Sdelphij 1406286570Smavstatic arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); 1407286570Smavstatic uint32_t arc_bufc_to_flags(arc_buf_contents_t); 1408307265Smavstatic inline void arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); 1409307265Smavstatic inline void arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); 1410286570Smav 1411275811Sdelphijstatic boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); 1412275811Sdelphijstatic void l2arc_read_done(zio_t *); 1413185029Spjd 1414290191Savgstatic void 1415290191Savgl2arc_trim(const arc_buf_hdr_t *hdr) 1416290191Savg{ 1417290191Savg l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 1418290191Savg 1419290191Savg ASSERT(HDR_HAS_L2HDR(hdr)); 1420290191Savg ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); 1421290191Savg 1422307265Smav if (HDR_GET_PSIZE(hdr) != 0) { 1423290191Savg trim_map_free(dev->l2ad_vdev, hdr->b_l2hdr.b_daddr, 1424307265Smav HDR_GET_PSIZE(hdr), 0); 1425290191Savg } 1426290191Savg} 1427290191Savg 1428168404Spjdstatic uint64_t 1429209962Smmbuf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 1430168404Spjd{ 1431168404Spjd uint8_t *vdva = (uint8_t *)dva; 1432168404Spjd uint64_t crc = -1ULL; 1433168404Spjd int i; 1434168404Spjd 1435168404Spjd ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 1436168404Spjd 1437168404Spjd for (i = 0; i < sizeof (dva_t); i++) 1438168404Spjd crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 1439168404Spjd 1440209962Smm crc ^= (spa>>8) ^ birth; 1441168404Spjd 1442168404Spjd return (crc); 1443168404Spjd} 1444168404Spjd 1445307265Smav#define HDR_EMPTY(hdr) \ 1446307265Smav ((hdr)->b_dva.dva_word[0] == 0 && \ 1447307265Smav (hdr)->b_dva.dva_word[1] == 0) 1448168404Spjd 1449307265Smav#define HDR_EQUAL(spa, dva, birth, hdr) \ 1450307265Smav ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 1451307265Smav ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 1452307265Smav ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa) 1453168404Spjd 1454219089Spjdstatic void 1455219089Spjdbuf_discard_identity(arc_buf_hdr_t *hdr) 1456219089Spjd{ 1457219089Spjd hdr->b_dva.dva_word[0] = 0; 1458219089Spjd hdr->b_dva.dva_word[1] = 0; 1459219089Spjd hdr->b_birth = 0; 1460219089Spjd} 1461219089Spjd 1462168404Spjdstatic arc_buf_hdr_t * 1463268075Sdelphijbuf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 1464168404Spjd{ 1465268075Sdelphij const dva_t *dva = BP_IDENTITY(bp); 1466268075Sdelphij uint64_t birth = BP_PHYSICAL_BIRTH(bp); 1467168404Spjd uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 1468168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1469275811Sdelphij arc_buf_hdr_t *hdr; 1470168404Spjd 1471168404Spjd mutex_enter(hash_lock); 1472275811Sdelphij for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; 1473275811Sdelphij hdr = hdr->b_hash_next) { 1474307265Smav if (HDR_EQUAL(spa, dva, birth, hdr)) { 1475168404Spjd *lockp = hash_lock; 1476275811Sdelphij return (hdr); 1477168404Spjd } 1478168404Spjd } 1479168404Spjd mutex_exit(hash_lock); 1480168404Spjd *lockp = NULL; 1481168404Spjd return (NULL); 1482168404Spjd} 1483168404Spjd 1484168404Spjd/* 1485168404Spjd * Insert an entry into the hash table. If there is already an element 1486168404Spjd * equal to elem in the hash table, then the already existing element 1487168404Spjd * will be returned and the new element will not be inserted. 1488168404Spjd * Otherwise returns NULL. 1489286570Smav * If lockp == NULL, the caller is assumed to already hold the hash lock. 1490168404Spjd */ 1491168404Spjdstatic arc_buf_hdr_t * 1492275811Sdelphijbuf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) 1493168404Spjd{ 1494275811Sdelphij uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1495168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1496275811Sdelphij arc_buf_hdr_t *fhdr; 1497168404Spjd uint32_t i; 1498168404Spjd 1499275811Sdelphij ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); 1500275811Sdelphij ASSERT(hdr->b_birth != 0); 1501275811Sdelphij ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1502286570Smav 1503286570Smav if (lockp != NULL) { 1504286570Smav *lockp = hash_lock; 1505286570Smav mutex_enter(hash_lock); 1506286570Smav } else { 1507286570Smav ASSERT(MUTEX_HELD(hash_lock)); 1508286570Smav } 1509286570Smav 1510275811Sdelphij for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; 1511275811Sdelphij fhdr = fhdr->b_hash_next, i++) { 1512307265Smav if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) 1513275811Sdelphij return (fhdr); 1514168404Spjd } 1515168404Spjd 1516275811Sdelphij hdr->b_hash_next = buf_hash_table.ht_table[idx]; 1517275811Sdelphij buf_hash_table.ht_table[idx] = hdr; 1518307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 1519168404Spjd 1520168404Spjd /* collect some hash table performance data */ 1521168404Spjd if (i > 0) { 1522168404Spjd ARCSTAT_BUMP(arcstat_hash_collisions); 1523168404Spjd if (i == 1) 1524168404Spjd ARCSTAT_BUMP(arcstat_hash_chains); 1525168404Spjd 1526168404Spjd ARCSTAT_MAX(arcstat_hash_chain_max, i); 1527168404Spjd } 1528168404Spjd 1529168404Spjd ARCSTAT_BUMP(arcstat_hash_elements); 1530168404Spjd ARCSTAT_MAXSTAT(arcstat_hash_elements); 1531168404Spjd 1532168404Spjd return (NULL); 1533168404Spjd} 1534168404Spjd 1535168404Spjdstatic void 1536275811Sdelphijbuf_hash_remove(arc_buf_hdr_t *hdr) 1537168404Spjd{ 1538275811Sdelphij arc_buf_hdr_t *fhdr, **hdrp; 1539275811Sdelphij uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1540168404Spjd 1541168404Spjd ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 1542275811Sdelphij ASSERT(HDR_IN_HASH_TABLE(hdr)); 1543168404Spjd 1544275811Sdelphij hdrp = &buf_hash_table.ht_table[idx]; 1545275811Sdelphij while ((fhdr = *hdrp) != hdr) { 1546307265Smav ASSERT3P(fhdr, !=, NULL); 1547275811Sdelphij hdrp = &fhdr->b_hash_next; 1548168404Spjd } 1549275811Sdelphij *hdrp = hdr->b_hash_next; 1550275811Sdelphij hdr->b_hash_next = NULL; 1551307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 1552168404Spjd 1553168404Spjd /* collect some hash table performance data */ 1554168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_elements); 1555168404Spjd 1556168404Spjd if (buf_hash_table.ht_table[idx] && 1557168404Spjd buf_hash_table.ht_table[idx]->b_hash_next == NULL) 1558168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1559168404Spjd} 1560168404Spjd 1561168404Spjd/* 1562168404Spjd * Global data structures and functions for the buf kmem cache. 1563168404Spjd */ 1564286570Smavstatic kmem_cache_t *hdr_full_cache; 1565286570Smavstatic kmem_cache_t *hdr_l2only_cache; 1566168404Spjdstatic kmem_cache_t *buf_cache; 1567168404Spjd 1568168404Spjdstatic void 1569168404Spjdbuf_fini(void) 1570168404Spjd{ 1571168404Spjd int i; 1572168404Spjd 1573168404Spjd kmem_free(buf_hash_table.ht_table, 1574168404Spjd (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1575168404Spjd for (i = 0; i < BUF_LOCKS; i++) 1576168404Spjd mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 1577286570Smav kmem_cache_destroy(hdr_full_cache); 1578286570Smav kmem_cache_destroy(hdr_l2only_cache); 1579168404Spjd kmem_cache_destroy(buf_cache); 1580168404Spjd} 1581168404Spjd 1582168404Spjd/* 1583168404Spjd * Constructor callback - called when the cache is empty 1584168404Spjd * and a new buf is requested. 1585168404Spjd */ 1586168404Spjd/* ARGSUSED */ 1587168404Spjdstatic int 1588286570Smavhdr_full_cons(void *vbuf, void *unused, int kmflag) 1589168404Spjd{ 1590275811Sdelphij arc_buf_hdr_t *hdr = vbuf; 1591168404Spjd 1592286570Smav bzero(hdr, HDR_FULL_SIZE); 1593286570Smav cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); 1594286570Smav refcount_create(&hdr->b_l1hdr.b_refcnt); 1595286570Smav mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1596286763Smav multilist_link_init(&hdr->b_l1hdr.b_arc_node); 1597286570Smav arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1598185029Spjd 1599168404Spjd return (0); 1600168404Spjd} 1601168404Spjd 1602185029Spjd/* ARGSUSED */ 1603185029Spjdstatic int 1604286570Smavhdr_l2only_cons(void *vbuf, void *unused, int kmflag) 1605286570Smav{ 1606286570Smav arc_buf_hdr_t *hdr = vbuf; 1607286570Smav 1608286570Smav bzero(hdr, HDR_L2ONLY_SIZE); 1609286570Smav arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1610286570Smav 1611286570Smav return (0); 1612286570Smav} 1613286570Smav 1614286570Smav/* ARGSUSED */ 1615286570Smavstatic int 1616185029Spjdbuf_cons(void *vbuf, void *unused, int kmflag) 1617185029Spjd{ 1618185029Spjd arc_buf_t *buf = vbuf; 1619185029Spjd 1620185029Spjd bzero(buf, sizeof (arc_buf_t)); 1621219089Spjd mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 1622208373Smm arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1623208373Smm 1624185029Spjd return (0); 1625185029Spjd} 1626185029Spjd 1627168404Spjd/* 1628168404Spjd * Destructor callback - called when a cached buf is 1629168404Spjd * no longer required. 1630168404Spjd */ 1631168404Spjd/* ARGSUSED */ 1632168404Spjdstatic void 1633286570Smavhdr_full_dest(void *vbuf, void *unused) 1634168404Spjd{ 1635275811Sdelphij arc_buf_hdr_t *hdr = vbuf; 1636168404Spjd 1637307265Smav ASSERT(HDR_EMPTY(hdr)); 1638286570Smav cv_destroy(&hdr->b_l1hdr.b_cv); 1639286570Smav refcount_destroy(&hdr->b_l1hdr.b_refcnt); 1640286570Smav mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); 1641286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 1642286570Smav arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1643168404Spjd} 1644168404Spjd 1645185029Spjd/* ARGSUSED */ 1646185029Spjdstatic void 1647286570Smavhdr_l2only_dest(void *vbuf, void *unused) 1648286570Smav{ 1649286570Smav arc_buf_hdr_t *hdr = vbuf; 1650286570Smav 1651307265Smav ASSERT(HDR_EMPTY(hdr)); 1652286570Smav arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1653286570Smav} 1654286570Smav 1655286570Smav/* ARGSUSED */ 1656286570Smavstatic void 1657185029Spjdbuf_dest(void *vbuf, void *unused) 1658185029Spjd{ 1659185029Spjd arc_buf_t *buf = vbuf; 1660185029Spjd 1661219089Spjd mutex_destroy(&buf->b_evict_lock); 1662208373Smm arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1663185029Spjd} 1664185029Spjd 1665168404Spjd/* 1666168404Spjd * Reclaim callback -- invoked when memory is low. 1667168404Spjd */ 1668168404Spjd/* ARGSUSED */ 1669168404Spjdstatic void 1670168404Spjdhdr_recl(void *unused) 1671168404Spjd{ 1672168404Spjd dprintf("hdr_recl called\n"); 1673168404Spjd /* 1674168404Spjd * umem calls the reclaim func when we destroy the buf cache, 1675168404Spjd * which is after we do arc_fini(). 1676168404Spjd */ 1677168404Spjd if (!arc_dead) 1678286763Smav cv_signal(&arc_reclaim_thread_cv); 1679168404Spjd} 1680168404Spjd 1681168404Spjdstatic void 1682168404Spjdbuf_init(void) 1683168404Spjd{ 1684168404Spjd uint64_t *ct; 1685168404Spjd uint64_t hsize = 1ULL << 12; 1686168404Spjd int i, j; 1687168404Spjd 1688168404Spjd /* 1689168404Spjd * The hash table is big enough to fill all of physical memory 1690269230Sdelphij * with an average block size of zfs_arc_average_blocksize (default 8K). 1691269230Sdelphij * By default, the table will take up 1692269230Sdelphij * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1693168404Spjd */ 1694269230Sdelphij while (hsize * zfs_arc_average_blocksize < (uint64_t)physmem * PAGESIZE) 1695168404Spjd hsize <<= 1; 1696168404Spjdretry: 1697168404Spjd buf_hash_table.ht_mask = hsize - 1; 1698168404Spjd buf_hash_table.ht_table = 1699168404Spjd kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1700168404Spjd if (buf_hash_table.ht_table == NULL) { 1701168404Spjd ASSERT(hsize > (1ULL << 8)); 1702168404Spjd hsize >>= 1; 1703168404Spjd goto retry; 1704168404Spjd } 1705168404Spjd 1706286570Smav hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, 1707286570Smav 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0); 1708286570Smav hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", 1709286570Smav HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl, 1710286570Smav NULL, NULL, 0); 1711168404Spjd buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1712185029Spjd 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1713168404Spjd 1714168404Spjd for (i = 0; i < 256; i++) 1715168404Spjd for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1716168404Spjd *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1717168404Spjd 1718168404Spjd for (i = 0; i < BUF_LOCKS; i++) { 1719168404Spjd mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1720168404Spjd NULL, MUTEX_DEFAULT, NULL); 1721168404Spjd } 1722168404Spjd} 1723168404Spjd 1724321535Smav/* 1725321535Smav * This is the size that the buf occupies in memory. If the buf is compressed, 1726321535Smav * it will correspond to the compressed size. You should use this method of 1727321535Smav * getting the buf size unless you explicitly need the logical size. 1728321535Smav */ 1729321535Smavint32_t 1730321535Smavarc_buf_size(arc_buf_t *buf) 1731321535Smav{ 1732321535Smav return (ARC_BUF_COMPRESSED(buf) ? 1733321535Smav HDR_GET_PSIZE(buf->b_hdr) : HDR_GET_LSIZE(buf->b_hdr)); 1734321535Smav} 1735321535Smav 1736321535Smavint32_t 1737321535Smavarc_buf_lsize(arc_buf_t *buf) 1738321535Smav{ 1739321535Smav return (HDR_GET_LSIZE(buf->b_hdr)); 1740321535Smav} 1741321535Smav 1742321535Smavenum zio_compress 1743321535Smavarc_get_compression(arc_buf_t *buf) 1744321535Smav{ 1745321535Smav return (ARC_BUF_COMPRESSED(buf) ? 1746321535Smav HDR_GET_COMPRESS(buf->b_hdr) : ZIO_COMPRESS_OFF); 1747321535Smav} 1748321535Smav 1749307265Smav#define ARC_MINTIME (hz>>4) /* 62 ms */ 1750307265Smav 1751307265Smavstatic inline boolean_t 1752307265Smavarc_buf_is_shared(arc_buf_t *buf) 1753286570Smav{ 1754307265Smav boolean_t shared = (buf->b_data != NULL && 1755321610Smav buf->b_hdr->b_l1hdr.b_pabd != NULL && 1756321610Smav abd_is_linear(buf->b_hdr->b_l1hdr.b_pabd) && 1757321610Smav buf->b_data == abd_to_buf(buf->b_hdr->b_l1hdr.b_pabd)); 1758307265Smav IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr)); 1759321535Smav IMPLY(shared, ARC_BUF_SHARED(buf)); 1760321535Smav IMPLY(shared, ARC_BUF_COMPRESSED(buf) || ARC_BUF_LAST(buf)); 1761321535Smav 1762321535Smav /* 1763321535Smav * It would be nice to assert arc_can_share() too, but the "hdr isn't 1764321535Smav * already being shared" requirement prevents us from doing that. 1765321535Smav */ 1766321535Smav 1767307265Smav return (shared); 1768307265Smav} 1769286570Smav 1770321535Smav/* 1771321535Smav * Free the checksum associated with this header. If there is no checksum, this 1772321535Smav * is a no-op. 1773321535Smav */ 1774307265Smavstatic inline void 1775307265Smavarc_cksum_free(arc_buf_hdr_t *hdr) 1776307265Smav{ 1777307265Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1778307265Smav mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1779307265Smav if (hdr->b_l1hdr.b_freeze_cksum != NULL) { 1780307265Smav kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t)); 1781307265Smav hdr->b_l1hdr.b_freeze_cksum = NULL; 1782286570Smav } 1783307265Smav mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1784286570Smav} 1785286570Smav 1786321535Smav/* 1787321535Smav * Return true iff at least one of the bufs on hdr is not compressed. 1788321535Smav */ 1789321535Smavstatic boolean_t 1790321535Smavarc_hdr_has_uncompressed_buf(arc_buf_hdr_t *hdr) 1791321535Smav{ 1792321535Smav for (arc_buf_t *b = hdr->b_l1hdr.b_buf; b != NULL; b = b->b_next) { 1793321535Smav if (!ARC_BUF_COMPRESSED(b)) { 1794321535Smav return (B_TRUE); 1795321535Smav } 1796321535Smav } 1797321535Smav return (B_FALSE); 1798321535Smav} 1799321535Smav 1800321535Smav/* 1801321535Smav * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data 1802321535Smav * matches the checksum that is stored in the hdr. If there is no checksum, 1803321535Smav * or if the buf is compressed, this is a no-op. 1804321535Smav */ 1805168404Spjdstatic void 1806168404Spjdarc_cksum_verify(arc_buf_t *buf) 1807168404Spjd{ 1808307265Smav arc_buf_hdr_t *hdr = buf->b_hdr; 1809168404Spjd zio_cksum_t zc; 1810168404Spjd 1811168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1812168404Spjd return; 1813168404Spjd 1814321535Smav if (ARC_BUF_COMPRESSED(buf)) { 1815321535Smav ASSERT(hdr->b_l1hdr.b_freeze_cksum == NULL || 1816321535Smav arc_hdr_has_uncompressed_buf(hdr)); 1817321535Smav return; 1818321535Smav } 1819321535Smav 1820307265Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1821307265Smav 1822307265Smav mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1823307265Smav if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) { 1824307265Smav mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1825168404Spjd return; 1826168404Spjd } 1827321535Smav 1828321535Smav fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, &zc); 1829307265Smav if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc)) 1830168404Spjd panic("buffer modified while frozen!"); 1831307265Smav mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1832168404Spjd} 1833168404Spjd 1834307265Smavstatic boolean_t 1835307265Smavarc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio) 1836185029Spjd{ 1837307265Smav enum zio_compress compress = BP_GET_COMPRESS(zio->io_bp); 1838307265Smav boolean_t valid_cksum; 1839185029Spjd 1840307265Smav ASSERT(!BP_IS_EMBEDDED(zio->io_bp)); 1841307265Smav VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr)); 1842185029Spjd 1843307265Smav /* 1844307265Smav * We rely on the blkptr's checksum to determine if the block 1845307265Smav * is valid or not. When compressed arc is enabled, the l2arc 1846307265Smav * writes the block to the l2arc just as it appears in the pool. 1847307265Smav * This allows us to use the blkptr's checksum to validate the 1848307265Smav * data that we just read off of the l2arc without having to store 1849307265Smav * a separate checksum in the arc_buf_hdr_t. However, if compressed 1850307265Smav * arc is disabled, then the data written to the l2arc is always 1851307265Smav * uncompressed and won't match the block as it exists in the main 1852307265Smav * pool. When this is the case, we must first compress it if it is 1853307265Smav * compressed on the main pool before we can validate the checksum. 1854307265Smav */ 1855307265Smav if (!HDR_COMPRESSION_ENABLED(hdr) && compress != ZIO_COMPRESS_OFF) { 1856307265Smav ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 1857307265Smav uint64_t lsize = HDR_GET_LSIZE(hdr); 1858307265Smav uint64_t csize; 1859307265Smav 1860329490Smav abd_t *cdata = abd_alloc_linear(HDR_GET_PSIZE(hdr), B_TRUE); 1861329490Smav csize = zio_compress_data(compress, zio->io_abd, 1862329490Smav abd_to_buf(cdata), lsize); 1863321610Smav 1864307265Smav ASSERT3U(csize, <=, HDR_GET_PSIZE(hdr)); 1865307265Smav if (csize < HDR_GET_PSIZE(hdr)) { 1866307265Smav /* 1867307265Smav * Compressed blocks are always a multiple of the 1868307265Smav * smallest ashift in the pool. Ideally, we would 1869307265Smav * like to round up the csize to the next 1870307265Smav * spa_min_ashift but that value may have changed 1871307265Smav * since the block was last written. Instead, 1872307265Smav * we rely on the fact that the hdr's psize 1873307265Smav * was set to the psize of the block when it was 1874307265Smav * last written. We set the csize to that value 1875307265Smav * and zero out any part that should not contain 1876307265Smav * data. 1877307265Smav */ 1878329490Smav abd_zero_off(cdata, csize, HDR_GET_PSIZE(hdr) - csize); 1879307265Smav csize = HDR_GET_PSIZE(hdr); 1880307265Smav } 1881329490Smav zio_push_transform(zio, cdata, csize, HDR_GET_PSIZE(hdr), NULL); 1882307265Smav } 1883307265Smav 1884307265Smav /* 1885307265Smav * Block pointers always store the checksum for the logical data. 1886307265Smav * If the block pointer has the gang bit set, then the checksum 1887307265Smav * it represents is for the reconstituted data and not for an 1888307265Smav * individual gang member. The zio pipeline, however, must be able to 1889307265Smav * determine the checksum of each of the gang constituents so it 1890307265Smav * treats the checksum comparison differently than what we need 1891307265Smav * for l2arc blocks. This prevents us from using the 1892307265Smav * zio_checksum_error() interface directly. Instead we must call the 1893307265Smav * zio_checksum_error_impl() so that we can ensure the checksum is 1894307265Smav * generated using the correct checksum algorithm and accounts for the 1895307265Smav * logical I/O size and not just a gang fragment. 1896307265Smav */ 1897307265Smav valid_cksum = (zio_checksum_error_impl(zio->io_spa, zio->io_bp, 1898321610Smav BP_GET_CHECKSUM(zio->io_bp), zio->io_abd, zio->io_size, 1899307265Smav zio->io_offset, NULL) == 0); 1900307265Smav zio_pop_transforms(zio); 1901307265Smav return (valid_cksum); 1902185029Spjd} 1903185029Spjd 1904321535Smav/* 1905321535Smav * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a 1906321535Smav * checksum and attaches it to the buf's hdr so that we can ensure that the buf 1907321535Smav * isn't modified later on. If buf is compressed or there is already a checksum 1908321535Smav * on the hdr, this is a no-op (we only checksum uncompressed bufs). 1909321535Smav */ 1910168404Spjdstatic void 1911307265Smavarc_cksum_compute(arc_buf_t *buf) 1912168404Spjd{ 1913307265Smav arc_buf_hdr_t *hdr = buf->b_hdr; 1914307265Smav 1915307265Smav if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1916168404Spjd return; 1917168404Spjd 1918307265Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1919321535Smav 1920286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1921307265Smav if (hdr->b_l1hdr.b_freeze_cksum != NULL) { 1922321535Smav ASSERT(arc_hdr_has_uncompressed_buf(hdr)); 1923307265Smav mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1924168404Spjd return; 1925321535Smav } else if (ARC_BUF_COMPRESSED(buf)) { 1926321535Smav mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1927321535Smav return; 1928168404Spjd } 1929321535Smav 1930321535Smav ASSERT(!ARC_BUF_COMPRESSED(buf)); 1931307265Smav hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), 1932307265Smav KM_SLEEP); 1933321535Smav fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, 1934307265Smav hdr->b_l1hdr.b_freeze_cksum); 1935307265Smav mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1936240133Smm#ifdef illumos 1937240133Smm arc_buf_watch(buf); 1938277300Ssmh#endif 1939168404Spjd} 1940168404Spjd 1941240133Smm#ifdef illumos 1942240133Smm#ifndef _KERNEL 1943240133Smmtypedef struct procctl { 1944240133Smm long cmd; 1945240133Smm prwatch_t prwatch; 1946240133Smm} procctl_t; 1947240133Smm#endif 1948240133Smm 1949240133Smm/* ARGSUSED */ 1950240133Smmstatic void 1951240133Smmarc_buf_unwatch(arc_buf_t *buf) 1952240133Smm{ 1953240133Smm#ifndef _KERNEL 1954240133Smm if (arc_watch) { 1955240133Smm int result; 1956240133Smm procctl_t ctl; 1957240133Smm ctl.cmd = PCWATCH; 1958240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1959240133Smm ctl.prwatch.pr_size = 0; 1960240133Smm ctl.prwatch.pr_wflags = 0; 1961240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1962240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1963240133Smm } 1964240133Smm#endif 1965240133Smm} 1966240133Smm 1967240133Smm/* ARGSUSED */ 1968240133Smmstatic void 1969240133Smmarc_buf_watch(arc_buf_t *buf) 1970240133Smm{ 1971240133Smm#ifndef _KERNEL 1972240133Smm if (arc_watch) { 1973240133Smm int result; 1974240133Smm procctl_t ctl; 1975240133Smm ctl.cmd = PCWATCH; 1976240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1977321535Smav ctl.prwatch.pr_size = arc_buf_size(buf); 1978240133Smm ctl.prwatch.pr_wflags = WA_WRITE; 1979240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1980240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1981240133Smm } 1982240133Smm#endif 1983240133Smm} 1984240133Smm#endif /* illumos */ 1985240133Smm 1986286570Smavstatic arc_buf_contents_t 1987286570Smavarc_buf_type(arc_buf_hdr_t *hdr) 1988286570Smav{ 1989307265Smav arc_buf_contents_t type; 1990286570Smav if (HDR_ISTYPE_METADATA(hdr)) { 1991307265Smav type = ARC_BUFC_METADATA; 1992286570Smav } else { 1993307265Smav type = ARC_BUFC_DATA; 1994286570Smav } 1995307265Smav VERIFY3U(hdr->b_type, ==, type); 1996307265Smav return (type); 1997286570Smav} 1998286570Smav 1999321535Smavboolean_t 2000321535Smavarc_is_metadata(arc_buf_t *buf) 2001321535Smav{ 2002321535Smav return (HDR_ISTYPE_METADATA(buf->b_hdr) != 0); 2003321535Smav} 2004321535Smav 2005286570Smavstatic uint32_t 2006286570Smavarc_bufc_to_flags(arc_buf_contents_t type) 2007286570Smav{ 2008286570Smav switch (type) { 2009286570Smav case ARC_BUFC_DATA: 2010286570Smav /* metadata field is 0 if buffer contains normal data */ 2011286570Smav return (0); 2012286570Smav case ARC_BUFC_METADATA: 2013286570Smav return (ARC_FLAG_BUFC_METADATA); 2014286570Smav default: 2015286570Smav break; 2016286570Smav } 2017286570Smav panic("undefined ARC buffer type!"); 2018286570Smav return ((uint32_t)-1); 2019286570Smav} 2020286570Smav 2021168404Spjdvoid 2022168404Spjdarc_buf_thaw(arc_buf_t *buf) 2023168404Spjd{ 2024307265Smav arc_buf_hdr_t *hdr = buf->b_hdr; 2025307265Smav 2026321535Smav ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 2027321535Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2028321535Smav 2029321535Smav arc_cksum_verify(buf); 2030321535Smav 2031321535Smav /* 2032321535Smav * Compressed buffers do not manipulate the b_freeze_cksum or 2033321535Smav * allocate b_thawed. 2034321535Smav */ 2035321535Smav if (ARC_BUF_COMPRESSED(buf)) { 2036321535Smav ASSERT(hdr->b_l1hdr.b_freeze_cksum == NULL || 2037321535Smav arc_hdr_has_uncompressed_buf(hdr)); 2038321535Smav return; 2039185029Spjd } 2040168404Spjd 2041307265Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2042307265Smav arc_cksum_free(hdr); 2043219089Spjd 2044307265Smav mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 2045286570Smav#ifdef ZFS_DEBUG 2046219089Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 2047307265Smav if (hdr->b_l1hdr.b_thawed != NULL) 2048307265Smav kmem_free(hdr->b_l1hdr.b_thawed, 1); 2049307265Smav hdr->b_l1hdr.b_thawed = kmem_alloc(1, KM_SLEEP); 2050219089Spjd } 2051286570Smav#endif 2052219089Spjd 2053307265Smav mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 2054240133Smm 2055240133Smm#ifdef illumos 2056240133Smm arc_buf_unwatch(buf); 2057277300Ssmh#endif 2058168404Spjd} 2059168404Spjd 2060168404Spjdvoid 2061168404Spjdarc_buf_freeze(arc_buf_t *buf) 2062168404Spjd{ 2063307265Smav arc_buf_hdr_t *hdr = buf->b_hdr; 2064219089Spjd kmutex_t *hash_lock; 2065219089Spjd 2066168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 2067168404Spjd return; 2068168404Spjd 2069321535Smav if (ARC_BUF_COMPRESSED(buf)) { 2070321535Smav ASSERT(hdr->b_l1hdr.b_freeze_cksum == NULL || 2071321535Smav arc_hdr_has_uncompressed_buf(hdr)); 2072321535Smav return; 2073321535Smav } 2074321535Smav 2075307265Smav hash_lock = HDR_LOCK(hdr); 2076219089Spjd mutex_enter(hash_lock); 2077219089Spjd 2078307265Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2079307265Smav ASSERT(hdr->b_l1hdr.b_freeze_cksum != NULL || 2080307265Smav hdr->b_l1hdr.b_state == arc_anon); 2081307265Smav arc_cksum_compute(buf); 2082219089Spjd mutex_exit(hash_lock); 2083168404Spjd} 2084168404Spjd 2085307265Smav/* 2086307265Smav * The arc_buf_hdr_t's b_flags should never be modified directly. Instead, 2087307265Smav * the following functions should be used to ensure that the flags are 2088307265Smav * updated in a thread-safe way. When manipulating the flags either 2089307265Smav * the hash_lock must be held or the hdr must be undiscoverable. This 2090307265Smav * ensures that we're not racing with any other threads when updating 2091307265Smav * the flags. 2092307265Smav */ 2093307265Smavstatic inline void 2094307265Smavarc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) 2095307265Smav{ 2096307265Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2097307265Smav hdr->b_flags |= flags; 2098307265Smav} 2099307265Smav 2100307265Smavstatic inline void 2101307265Smavarc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) 2102307265Smav{ 2103307265Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2104307265Smav hdr->b_flags &= ~flags; 2105307265Smav} 2106307265Smav 2107307265Smav/* 2108307265Smav * Setting the compression bits in the arc_buf_hdr_t's b_flags is 2109307265Smav * done in a special way since we have to clear and set bits 2110307265Smav * at the same time. Consumers that wish to set the compression bits 2111307265Smav * must use this function to ensure that the flags are updated in 2112307265Smav * thread-safe manner. 2113307265Smav */ 2114168404Spjdstatic void 2115307265Smavarc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp) 2116168404Spjd{ 2117307265Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2118307265Smav 2119307265Smav /* 2120307265Smav * Holes and embedded blocks will always have a psize = 0 so 2121307265Smav * we ignore the compression of the blkptr and set the 2122307265Smav * arc_buf_hdr_t's compression to ZIO_COMPRESS_OFF. 2123307265Smav * Holes and embedded blocks remain anonymous so we don't 2124307265Smav * want to uncompress them. Mark them as uncompressed. 2125307265Smav */ 2126307265Smav if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) { 2127307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC); 2128307265Smav HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); 2129307265Smav ASSERT(!HDR_COMPRESSION_ENABLED(hdr)); 2130307265Smav ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 2131307265Smav } else { 2132307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC); 2133307265Smav HDR_SET_COMPRESS(hdr, cmp); 2134307265Smav ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp); 2135307265Smav ASSERT(HDR_COMPRESSION_ENABLED(hdr)); 2136307265Smav } 2137307265Smav} 2138307265Smav 2139321535Smav/* 2140321535Smav * Looks for another buf on the same hdr which has the data decompressed, copies 2141321535Smav * from it, and returns true. If no such buf exists, returns false. 2142321535Smav */ 2143321535Smavstatic boolean_t 2144321535Smavarc_buf_try_copy_decompressed_data(arc_buf_t *buf) 2145321535Smav{ 2146321535Smav arc_buf_hdr_t *hdr = buf->b_hdr; 2147321535Smav boolean_t copied = B_FALSE; 2148321535Smav 2149321535Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2150321535Smav ASSERT3P(buf->b_data, !=, NULL); 2151321535Smav ASSERT(!ARC_BUF_COMPRESSED(buf)); 2152321535Smav 2153321535Smav for (arc_buf_t *from = hdr->b_l1hdr.b_buf; from != NULL; 2154321535Smav from = from->b_next) { 2155321535Smav /* can't use our own data buffer */ 2156321535Smav if (from == buf) { 2157321535Smav continue; 2158321535Smav } 2159321535Smav 2160321535Smav if (!ARC_BUF_COMPRESSED(from)) { 2161321535Smav bcopy(from->b_data, buf->b_data, arc_buf_size(buf)); 2162321535Smav copied = B_TRUE; 2163321535Smav break; 2164321535Smav } 2165321535Smav } 2166321535Smav 2167321535Smav /* 2168321535Smav * There were no decompressed bufs, so there should not be a 2169321535Smav * checksum on the hdr either. 2170321535Smav */ 2171321535Smav EQUIV(!copied, hdr->b_l1hdr.b_freeze_cksum == NULL); 2172321535Smav 2173321535Smav return (copied); 2174321535Smav} 2175321535Smav 2176321535Smav/* 2177321535Smav * Given a buf that has a data buffer attached to it, this function will 2178321535Smav * efficiently fill the buf with data of the specified compression setting from 2179321535Smav * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr 2180321535Smav * are already sharing a data buf, no copy is performed. 2181321535Smav * 2182321535Smav * If the buf is marked as compressed but uncompressed data was requested, this 2183321535Smav * will allocate a new data buffer for the buf, remove that flag, and fill the 2184321535Smav * buf with uncompressed data. You can't request a compressed buf on a hdr with 2185321535Smav * uncompressed data, and (since we haven't added support for it yet) if you 2186321535Smav * want compressed data your buf must already be marked as compressed and have 2187321535Smav * the correct-sized data buffer. 2188321535Smav */ 2189307265Smavstatic int 2190321535Smavarc_buf_fill(arc_buf_t *buf, boolean_t compressed) 2191307265Smav{ 2192307265Smav arc_buf_hdr_t *hdr = buf->b_hdr; 2193321535Smav boolean_t hdr_compressed = (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF); 2194307265Smav dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap; 2195307265Smav 2196321535Smav ASSERT3P(buf->b_data, !=, NULL); 2197321535Smav IMPLY(compressed, hdr_compressed); 2198321535Smav IMPLY(compressed, ARC_BUF_COMPRESSED(buf)); 2199321535Smav 2200321535Smav if (hdr_compressed == compressed) { 2201321535Smav if (!arc_buf_is_shared(buf)) { 2202321610Smav abd_copy_to_buf(buf->b_data, hdr->b_l1hdr.b_pabd, 2203321535Smav arc_buf_size(buf)); 2204321535Smav } 2205321535Smav } else { 2206321535Smav ASSERT(hdr_compressed); 2207321535Smav ASSERT(!compressed); 2208321535Smav ASSERT3U(HDR_GET_LSIZE(hdr), !=, HDR_GET_PSIZE(hdr)); 2209321535Smav 2210307265Smav /* 2211321535Smav * If the buf is sharing its data with the hdr, unlink it and 2212321535Smav * allocate a new data buffer for the buf. 2213307265Smav */ 2214321535Smav if (arc_buf_is_shared(buf)) { 2215321535Smav ASSERT(ARC_BUF_COMPRESSED(buf)); 2216321535Smav 2217321535Smav /* We need to give the buf it's own b_data */ 2218321535Smav buf->b_flags &= ~ARC_BUF_FLAG_SHARED; 2219321535Smav buf->b_data = 2220321535Smav arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); 2221321535Smav arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 2222321535Smav 2223321535Smav /* Previously overhead was 0; just add new overhead */ 2224321535Smav ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); 2225321535Smav } else if (ARC_BUF_COMPRESSED(buf)) { 2226321535Smav /* We need to reallocate the buf's b_data */ 2227321535Smav arc_free_data_buf(hdr, buf->b_data, HDR_GET_PSIZE(hdr), 2228321535Smav buf); 2229321535Smav buf->b_data = 2230321535Smav arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); 2231321535Smav 2232321535Smav /* We increased the size of b_data; update overhead */ 2233321535Smav ARCSTAT_INCR(arcstat_overhead_size, 2234321535Smav HDR_GET_LSIZE(hdr) - HDR_GET_PSIZE(hdr)); 2235307265Smav } 2236321535Smav 2237321535Smav /* 2238321535Smav * Regardless of the buf's previous compression settings, it 2239321535Smav * should not be compressed at the end of this function. 2240321535Smav */ 2241321535Smav buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; 2242321535Smav 2243321535Smav /* 2244321535Smav * Try copying the data from another buf which already has a 2245321535Smav * decompressed version. If that's not possible, it's time to 2246321535Smav * bite the bullet and decompress the data from the hdr. 2247321535Smav */ 2248321535Smav if (arc_buf_try_copy_decompressed_data(buf)) { 2249321535Smav /* Skip byteswapping and checksumming (already done) */ 2250321535Smav ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, !=, NULL); 2251321535Smav return (0); 2252321535Smav } else { 2253321535Smav int error = zio_decompress_data(HDR_GET_COMPRESS(hdr), 2254321610Smav hdr->b_l1hdr.b_pabd, buf->b_data, 2255321535Smav HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); 2256321535Smav 2257321535Smav /* 2258321535Smav * Absent hardware errors or software bugs, this should 2259321535Smav * be impossible, but log it anyway so we can debug it. 2260321535Smav */ 2261321535Smav if (error != 0) { 2262321535Smav zfs_dbgmsg( 2263321535Smav "hdr %p, compress %d, psize %d, lsize %d", 2264321535Smav hdr, HDR_GET_COMPRESS(hdr), 2265321535Smav HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); 2266321535Smav return (SET_ERROR(EIO)); 2267321535Smav } 2268321535Smav } 2269307265Smav } 2270321535Smav 2271321535Smav /* Byteswap the buf's data if necessary */ 2272307265Smav if (bswap != DMU_BSWAP_NUMFUNCS) { 2273307265Smav ASSERT(!HDR_SHARED_DATA(hdr)); 2274307265Smav ASSERT3U(bswap, <, DMU_BSWAP_NUMFUNCS); 2275307265Smav dmu_ot_byteswap[bswap].ob_func(buf->b_data, HDR_GET_LSIZE(hdr)); 2276307265Smav } 2277321535Smav 2278321535Smav /* Compute the hdr's checksum if necessary */ 2279307265Smav arc_cksum_compute(buf); 2280321535Smav 2281307265Smav return (0); 2282307265Smav} 2283307265Smav 2284321535Smavint 2285321535Smavarc_decompress(arc_buf_t *buf) 2286321535Smav{ 2287321535Smav return (arc_buf_fill(buf, B_FALSE)); 2288321535Smav} 2289321535Smav 2290307265Smav/* 2291321610Smav * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t. 2292307265Smav */ 2293307265Smavstatic uint64_t 2294307265Smavarc_hdr_size(arc_buf_hdr_t *hdr) 2295307265Smav{ 2296307265Smav uint64_t size; 2297307265Smav 2298307265Smav if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && 2299307265Smav HDR_GET_PSIZE(hdr) > 0) { 2300307265Smav size = HDR_GET_PSIZE(hdr); 2301307265Smav } else { 2302307265Smav ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0); 2303307265Smav size = HDR_GET_LSIZE(hdr); 2304307265Smav } 2305307265Smav return (size); 2306307265Smav} 2307307265Smav 2308307265Smav/* 2309307265Smav * Increment the amount of evictable space in the arc_state_t's refcount. 2310307265Smav * We account for the space used by the hdr and the arc buf individually 2311307265Smav * so that we can add and remove them from the refcount individually. 2312307265Smav */ 2313307265Smavstatic void 2314307265Smavarc_evictable_space_increment(arc_buf_hdr_t *hdr, arc_state_t *state) 2315307265Smav{ 2316307265Smav arc_buf_contents_t type = arc_buf_type(hdr); 2317307265Smav 2318286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2319307265Smav 2320307265Smav if (GHOST_STATE(state)) { 2321307265Smav ASSERT0(hdr->b_l1hdr.b_bufcnt); 2322307265Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2323321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2324321535Smav (void) refcount_add_many(&state->arcs_esize[type], 2325321535Smav HDR_GET_LSIZE(hdr), hdr); 2326307265Smav return; 2327307265Smav } 2328307265Smav 2329307265Smav ASSERT(!GHOST_STATE(state)); 2330321610Smav if (hdr->b_l1hdr.b_pabd != NULL) { 2331307265Smav (void) refcount_add_many(&state->arcs_esize[type], 2332307265Smav arc_hdr_size(hdr), hdr); 2333307265Smav } 2334307265Smav for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2335307265Smav buf = buf->b_next) { 2336321535Smav if (arc_buf_is_shared(buf)) 2337307265Smav continue; 2338321535Smav (void) refcount_add_many(&state->arcs_esize[type], 2339321535Smav arc_buf_size(buf), buf); 2340307265Smav } 2341307265Smav} 2342307265Smav 2343307265Smav/* 2344307265Smav * Decrement the amount of evictable space in the arc_state_t's refcount. 2345307265Smav * We account for the space used by the hdr and the arc buf individually 2346307265Smav * so that we can add and remove them from the refcount individually. 2347307265Smav */ 2348307265Smavstatic void 2349321535Smavarc_evictable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state) 2350307265Smav{ 2351307265Smav arc_buf_contents_t type = arc_buf_type(hdr); 2352307265Smav 2353307265Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2354307265Smav 2355307265Smav if (GHOST_STATE(state)) { 2356307265Smav ASSERT0(hdr->b_l1hdr.b_bufcnt); 2357307265Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2358321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2359307265Smav (void) refcount_remove_many(&state->arcs_esize[type], 2360321535Smav HDR_GET_LSIZE(hdr), hdr); 2361307265Smav return; 2362307265Smav } 2363307265Smav 2364307265Smav ASSERT(!GHOST_STATE(state)); 2365321610Smav if (hdr->b_l1hdr.b_pabd != NULL) { 2366307265Smav (void) refcount_remove_many(&state->arcs_esize[type], 2367307265Smav arc_hdr_size(hdr), hdr); 2368307265Smav } 2369307265Smav for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2370307265Smav buf = buf->b_next) { 2371321535Smav if (arc_buf_is_shared(buf)) 2372307265Smav continue; 2373307265Smav (void) refcount_remove_many(&state->arcs_esize[type], 2374321535Smav arc_buf_size(buf), buf); 2375307265Smav } 2376307265Smav} 2377307265Smav 2378307265Smav/* 2379307265Smav * Add a reference to this hdr indicating that someone is actively 2380307265Smav * referencing that memory. When the refcount transitions from 0 to 1, 2381307265Smav * we remove it from the respective arc_state_t list to indicate that 2382307265Smav * it is not evictable. 2383307265Smav */ 2384307265Smavstatic void 2385307265Smavadd_reference(arc_buf_hdr_t *hdr, void *tag) 2386307265Smav{ 2387307265Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2388307265Smav if (!MUTEX_HELD(HDR_LOCK(hdr))) { 2389307265Smav ASSERT(hdr->b_l1hdr.b_state == arc_anon); 2390307265Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2391307265Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2392307265Smav } 2393307265Smav 2394286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 2395168404Spjd 2396286570Smav if ((refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && 2397286570Smav (state != arc_anon)) { 2398286570Smav /* We don't use the L2-only state list. */ 2399286570Smav if (state != arc_l2c_only) { 2400321553Smav multilist_remove(state->arcs_list[arc_buf_type(hdr)], 2401307265Smav hdr); 2402321535Smav arc_evictable_space_decrement(hdr, state); 2403168404Spjd } 2404185029Spjd /* remove the prefetch flag if we get a reference */ 2405307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); 2406168404Spjd } 2407168404Spjd} 2408168404Spjd 2409307265Smav/* 2410307265Smav * Remove a reference from this hdr. When the reference transitions from 2411307265Smav * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's 2412307265Smav * list making it eligible for eviction. 2413307265Smav */ 2414168404Spjdstatic int 2415275811Sdelphijremove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 2416168404Spjd{ 2417168404Spjd int cnt; 2418286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 2419168404Spjd 2420286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2421168404Spjd ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 2422168404Spjd ASSERT(!GHOST_STATE(state)); 2423168404Spjd 2424286570Smav /* 2425286570Smav * arc_l2c_only counts as a ghost state so we don't need to explicitly 2426286570Smav * check to prevent usage of the arc_l2c_only list. 2427286570Smav */ 2428286570Smav if (((cnt = refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && 2429168404Spjd (state != arc_anon)) { 2430321553Smav multilist_insert(state->arcs_list[arc_buf_type(hdr)], hdr); 2431307265Smav ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); 2432307265Smav arc_evictable_space_increment(hdr, state); 2433168404Spjd } 2434168404Spjd return (cnt); 2435168404Spjd} 2436168404Spjd 2437168404Spjd/* 2438286763Smav * Move the supplied buffer to the indicated state. The hash lock 2439168404Spjd * for the buffer must be held by the caller. 2440168404Spjd */ 2441168404Spjdstatic void 2442275811Sdelphijarc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, 2443275811Sdelphij kmutex_t *hash_lock) 2444168404Spjd{ 2445286570Smav arc_state_t *old_state; 2446286570Smav int64_t refcnt; 2447307265Smav uint32_t bufcnt; 2448307265Smav boolean_t update_old, update_new; 2449286570Smav arc_buf_contents_t buftype = arc_buf_type(hdr); 2450168404Spjd 2451286570Smav /* 2452286570Smav * We almost always have an L1 hdr here, since we call arc_hdr_realloc() 2453286570Smav * in arc_read() when bringing a buffer out of the L2ARC. However, the 2454286570Smav * L1 hdr doesn't always exist when we change state to arc_anon before 2455286570Smav * destroying a header, in which case reallocating to add the L1 hdr is 2456286570Smav * pointless. 2457286570Smav */ 2458286570Smav if (HDR_HAS_L1HDR(hdr)) { 2459286570Smav old_state = hdr->b_l1hdr.b_state; 2460286570Smav refcnt = refcount_count(&hdr->b_l1hdr.b_refcnt); 2461307265Smav bufcnt = hdr->b_l1hdr.b_bufcnt; 2462321610Smav update_old = (bufcnt > 0 || hdr->b_l1hdr.b_pabd != NULL); 2463286570Smav } else { 2464286570Smav old_state = arc_l2c_only; 2465286570Smav refcnt = 0; 2466307265Smav bufcnt = 0; 2467307265Smav update_old = B_FALSE; 2468286570Smav } 2469307265Smav update_new = update_old; 2470286570Smav 2471168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 2472258632Savg ASSERT3P(new_state, !=, old_state); 2473307265Smav ASSERT(!GHOST_STATE(new_state) || bufcnt == 0); 2474307265Smav ASSERT(old_state != arc_anon || bufcnt <= 1); 2475168404Spjd 2476168404Spjd /* 2477168404Spjd * If this buffer is evictable, transfer it from the 2478168404Spjd * old state list to the new state list. 2479168404Spjd */ 2480168404Spjd if (refcnt == 0) { 2481286570Smav if (old_state != arc_anon && old_state != arc_l2c_only) { 2482286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2483321553Smav multilist_remove(old_state->arcs_list[buftype], hdr); 2484168404Spjd 2485307265Smav if (GHOST_STATE(old_state)) { 2486307265Smav ASSERT0(bufcnt); 2487307265Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2488307265Smav update_old = B_TRUE; 2489168404Spjd } 2490321535Smav arc_evictable_space_decrement(hdr, old_state); 2491168404Spjd } 2492286570Smav if (new_state != arc_anon && new_state != arc_l2c_only) { 2493168404Spjd 2494286570Smav /* 2495286570Smav * An L1 header always exists here, since if we're 2496286570Smav * moving to some L1-cached state (i.e. not l2c_only or 2497286570Smav * anonymous), we realloc the header to add an L1hdr 2498286570Smav * beforehand. 2499286570Smav */ 2500286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2501321553Smav multilist_insert(new_state->arcs_list[buftype], hdr); 2502168404Spjd 2503168404Spjd if (GHOST_STATE(new_state)) { 2504307265Smav ASSERT0(bufcnt); 2505307265Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2506307265Smav update_new = B_TRUE; 2507168404Spjd } 2508307265Smav arc_evictable_space_increment(hdr, new_state); 2509168404Spjd } 2510168404Spjd } 2511168404Spjd 2512307265Smav ASSERT(!HDR_EMPTY(hdr)); 2513275811Sdelphij if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) 2514275811Sdelphij buf_hash_remove(hdr); 2515168404Spjd 2516286570Smav /* adjust state sizes (ignore arc_l2c_only) */ 2517286766Smav 2518307265Smav if (update_new && new_state != arc_l2c_only) { 2519286766Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2520286766Smav if (GHOST_STATE(new_state)) { 2521307265Smav ASSERT0(bufcnt); 2522286766Smav 2523286766Smav /* 2524307265Smav * When moving a header to a ghost state, we first 2525286766Smav * remove all arc buffers. Thus, we'll have a 2526307265Smav * bufcnt of zero, and no arc buffer to use for 2527286766Smav * the reference. As a result, we use the arc 2528286766Smav * header pointer for the reference. 2529286766Smav */ 2530286766Smav (void) refcount_add_many(&new_state->arcs_size, 2531307265Smav HDR_GET_LSIZE(hdr), hdr); 2532321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2533286766Smav } else { 2534307265Smav uint32_t buffers = 0; 2535286766Smav 2536286766Smav /* 2537286766Smav * Each individual buffer holds a unique reference, 2538286766Smav * thus we must remove each of these references one 2539286766Smav * at a time. 2540286766Smav */ 2541286766Smav for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2542286766Smav buf = buf->b_next) { 2543307265Smav ASSERT3U(bufcnt, !=, 0); 2544307265Smav buffers++; 2545307265Smav 2546307265Smav /* 2547307265Smav * When the arc_buf_t is sharing the data 2548307265Smav * block with the hdr, the owner of the 2549307265Smav * reference belongs to the hdr. Only 2550307265Smav * add to the refcount if the arc_buf_t is 2551307265Smav * not shared. 2552307265Smav */ 2553321535Smav if (arc_buf_is_shared(buf)) 2554307265Smav continue; 2555307265Smav 2556286766Smav (void) refcount_add_many(&new_state->arcs_size, 2557321535Smav arc_buf_size(buf), buf); 2558286766Smav } 2559307265Smav ASSERT3U(bufcnt, ==, buffers); 2560307265Smav 2561321610Smav if (hdr->b_l1hdr.b_pabd != NULL) { 2562307265Smav (void) refcount_add_many(&new_state->arcs_size, 2563307265Smav arc_hdr_size(hdr), hdr); 2564307265Smav } else { 2565307265Smav ASSERT(GHOST_STATE(old_state)); 2566307265Smav } 2567286766Smav } 2568286766Smav } 2569286766Smav 2570307265Smav if (update_old && old_state != arc_l2c_only) { 2571286766Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2572286766Smav if (GHOST_STATE(old_state)) { 2573307265Smav ASSERT0(bufcnt); 2574321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2575307265Smav 2576286766Smav /* 2577286766Smav * When moving a header off of a ghost state, 2578307265Smav * the header will not contain any arc buffers. 2579307265Smav * We use the arc header pointer for the reference 2580307265Smav * which is exactly what we did when we put the 2581307265Smav * header on the ghost state. 2582286766Smav */ 2583286766Smav 2584286766Smav (void) refcount_remove_many(&old_state->arcs_size, 2585307265Smav HDR_GET_LSIZE(hdr), hdr); 2586286766Smav } else { 2587307265Smav uint32_t buffers = 0; 2588286766Smav 2589286766Smav /* 2590286766Smav * Each individual buffer holds a unique reference, 2591286766Smav * thus we must remove each of these references one 2592286766Smav * at a time. 2593286766Smav */ 2594286766Smav for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2595286766Smav buf = buf->b_next) { 2596321535Smav ASSERT3U(bufcnt, !=, 0); 2597307265Smav buffers++; 2598307265Smav 2599307265Smav /* 2600307265Smav * When the arc_buf_t is sharing the data 2601307265Smav * block with the hdr, the owner of the 2602307265Smav * reference belongs to the hdr. Only 2603307265Smav * add to the refcount if the arc_buf_t is 2604307265Smav * not shared. 2605307265Smav */ 2606321535Smav if (arc_buf_is_shared(buf)) 2607307265Smav continue; 2608307265Smav 2609286766Smav (void) refcount_remove_many( 2610321535Smav &old_state->arcs_size, arc_buf_size(buf), 2611307265Smav buf); 2612286766Smav } 2613307265Smav ASSERT3U(bufcnt, ==, buffers); 2614321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 2615307265Smav (void) refcount_remove_many( 2616307265Smav &old_state->arcs_size, arc_hdr_size(hdr), hdr); 2617286766Smav } 2618168404Spjd } 2619286766Smav 2620286570Smav if (HDR_HAS_L1HDR(hdr)) 2621286570Smav hdr->b_l1hdr.b_state = new_state; 2622185029Spjd 2623286570Smav /* 2624286570Smav * L2 headers should never be on the L2 state list since they don't 2625286570Smav * have L1 headers allocated. 2626286570Smav */ 2627321553Smav ASSERT(multilist_is_empty(arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && 2628321553Smav multilist_is_empty(arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); 2629168404Spjd} 2630168404Spjd 2631185029Spjdvoid 2632208373Smmarc_space_consume(uint64_t space, arc_space_type_t type) 2633185029Spjd{ 2634208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 2635208373Smm 2636208373Smm switch (type) { 2637208373Smm case ARC_SPACE_DATA: 2638208373Smm ARCSTAT_INCR(arcstat_data_size, space); 2639208373Smm break; 2640286574Smav case ARC_SPACE_META: 2641286574Smav ARCSTAT_INCR(arcstat_metadata_size, space); 2642286574Smav break; 2643208373Smm case ARC_SPACE_OTHER: 2644208373Smm ARCSTAT_INCR(arcstat_other_size, space); 2645208373Smm break; 2646208373Smm case ARC_SPACE_HDRS: 2647208373Smm ARCSTAT_INCR(arcstat_hdr_size, space); 2648208373Smm break; 2649208373Smm case ARC_SPACE_L2HDRS: 2650208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, space); 2651208373Smm break; 2652208373Smm } 2653208373Smm 2654286574Smav if (type != ARC_SPACE_DATA) 2655286574Smav ARCSTAT_INCR(arcstat_meta_used, space); 2656286574Smav 2657185029Spjd atomic_add_64(&arc_size, space); 2658185029Spjd} 2659185029Spjd 2660185029Spjdvoid 2661208373Smmarc_space_return(uint64_t space, arc_space_type_t type) 2662185029Spjd{ 2663208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 2664208373Smm 2665208373Smm switch (type) { 2666208373Smm case ARC_SPACE_DATA: 2667208373Smm ARCSTAT_INCR(arcstat_data_size, -space); 2668208373Smm break; 2669286574Smav case ARC_SPACE_META: 2670286574Smav ARCSTAT_INCR(arcstat_metadata_size, -space); 2671286574Smav break; 2672208373Smm case ARC_SPACE_OTHER: 2673208373Smm ARCSTAT_INCR(arcstat_other_size, -space); 2674208373Smm break; 2675208373Smm case ARC_SPACE_HDRS: 2676208373Smm ARCSTAT_INCR(arcstat_hdr_size, -space); 2677208373Smm break; 2678208373Smm case ARC_SPACE_L2HDRS: 2679208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 2680208373Smm break; 2681208373Smm } 2682208373Smm 2683286574Smav if (type != ARC_SPACE_DATA) { 2684286574Smav ASSERT(arc_meta_used >= space); 2685286574Smav if (arc_meta_max < arc_meta_used) 2686286574Smav arc_meta_max = arc_meta_used; 2687286574Smav ARCSTAT_INCR(arcstat_meta_used, -space); 2688286574Smav } 2689286574Smav 2690185029Spjd ASSERT(arc_size >= space); 2691185029Spjd atomic_add_64(&arc_size, -space); 2692185029Spjd} 2693185029Spjd 2694307265Smav/* 2695321535Smav * Given a hdr and a buf, returns whether that buf can share its b_data buffer 2696321610Smav * with the hdr's b_pabd. 2697307265Smav */ 2698321535Smavstatic boolean_t 2699321535Smavarc_can_share(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2700168404Spjd{ 2701321535Smav /* 2702321535Smav * The criteria for sharing a hdr's data are: 2703321535Smav * 1. the hdr's compression matches the buf's compression 2704321535Smav * 2. the hdr doesn't need to be byteswapped 2705321535Smav * 3. the hdr isn't already being shared 2706321535Smav * 4. the buf is either compressed or it is the last buf in the hdr list 2707321535Smav * 2708321535Smav * Criterion #4 maintains the invariant that shared uncompressed 2709321535Smav * bufs must be the final buf in the hdr's b_buf list. Reading this, you 2710321535Smav * might ask, "if a compressed buf is allocated first, won't that be the 2711321535Smav * last thing in the list?", but in that case it's impossible to create 2712321535Smav * a shared uncompressed buf anyway (because the hdr must be compressed 2713321535Smav * to have the compressed buf). You might also think that #3 is 2714321535Smav * sufficient to make this guarantee, however it's possible 2715321535Smav * (specifically in the rare L2ARC write race mentioned in 2716321535Smav * arc_buf_alloc_impl()) there will be an existing uncompressed buf that 2717321535Smav * is sharable, but wasn't at the time of its allocation. Rather than 2718321535Smav * allow a new shared uncompressed buf to be created and then shuffle 2719321535Smav * the list around to make it the last element, this simply disallows 2720321535Smav * sharing if the new buf isn't the first to be added. 2721321535Smav */ 2722321535Smav ASSERT3P(buf->b_hdr, ==, hdr); 2723321535Smav boolean_t hdr_compressed = HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF; 2724321535Smav boolean_t buf_compressed = ARC_BUF_COMPRESSED(buf) != 0; 2725321535Smav return (buf_compressed == hdr_compressed && 2726321535Smav hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS && 2727321535Smav !HDR_SHARED_DATA(hdr) && 2728321535Smav (ARC_BUF_LAST(buf) || ARC_BUF_COMPRESSED(buf))); 2729321535Smav} 2730321535Smav 2731321535Smav/* 2732321535Smav * Allocate a buf for this hdr. If you care about the data that's in the hdr, 2733321535Smav * or if you want a compressed buffer, pass those flags in. Returns 0 if the 2734321535Smav * copy was made successfully, or an error code otherwise. 2735321535Smav */ 2736321535Smavstatic int 2737321535Smavarc_buf_alloc_impl(arc_buf_hdr_t *hdr, void *tag, boolean_t compressed, 2738321535Smav boolean_t fill, arc_buf_t **ret) 2739321535Smav{ 2740168404Spjd arc_buf_t *buf; 2741168404Spjd 2742307265Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2743307265Smav ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); 2744307265Smav VERIFY(hdr->b_type == ARC_BUFC_DATA || 2745307265Smav hdr->b_type == ARC_BUFC_METADATA); 2746321535Smav ASSERT3P(ret, !=, NULL); 2747321535Smav ASSERT3P(*ret, ==, NULL); 2748286570Smav 2749321535Smav buf = *ret = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2750168404Spjd buf->b_hdr = hdr; 2751168404Spjd buf->b_data = NULL; 2752321535Smav buf->b_next = hdr->b_l1hdr.b_buf; 2753321535Smav buf->b_flags = 0; 2754286570Smav 2755307265Smav add_reference(hdr, tag); 2756286570Smav 2757307265Smav /* 2758307265Smav * We're about to change the hdr's b_flags. We must either 2759307265Smav * hold the hash_lock or be undiscoverable. 2760307265Smav */ 2761307265Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2762307265Smav 2763307265Smav /* 2764321535Smav * Only honor requests for compressed bufs if the hdr is actually 2765321535Smav * compressed. 2766307265Smav */ 2767321535Smav if (compressed && HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) 2768321535Smav buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; 2769321535Smav 2770321535Smav /* 2771321535Smav * If the hdr's data can be shared then we share the data buffer and 2772321535Smav * set the appropriate bit in the hdr's b_flags to indicate the hdr is 2773321610Smav * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new 2774321535Smav * buffer to store the buf's data. 2775321535Smav * 2776321610Smav * There are two additional restrictions here because we're sharing 2777321610Smav * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be 2778321610Smav * actively involved in an L2ARC write, because if this buf is used by 2779321610Smav * an arc_write() then the hdr's data buffer will be released when the 2780321535Smav * write completes, even though the L2ARC write might still be using it. 2781321610Smav * Second, the hdr's ABD must be linear so that the buf's user doesn't 2782321610Smav * need to be ABD-aware. 2783321535Smav */ 2784321610Smav boolean_t can_share = arc_can_share(hdr, buf) && !HDR_L2_WRITING(hdr) && 2785321610Smav abd_is_linear(hdr->b_l1hdr.b_pabd); 2786321535Smav 2787321535Smav /* Set up b_data and sharing */ 2788321535Smav if (can_share) { 2789321610Smav buf->b_data = abd_to_buf(hdr->b_l1hdr.b_pabd); 2790321535Smav buf->b_flags |= ARC_BUF_FLAG_SHARED; 2791307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); 2792307265Smav } else { 2793321535Smav buf->b_data = 2794321535Smav arc_get_data_buf(hdr, arc_buf_size(buf), buf); 2795321535Smav ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); 2796307265Smav } 2797307265Smav VERIFY3P(buf->b_data, !=, NULL); 2798307265Smav 2799286570Smav hdr->b_l1hdr.b_buf = buf; 2800307265Smav hdr->b_l1hdr.b_bufcnt += 1; 2801286570Smav 2802321535Smav /* 2803321535Smav * If the user wants the data from the hdr, we need to either copy or 2804321535Smav * decompress the data. 2805321535Smav */ 2806321535Smav if (fill) { 2807321535Smav return (arc_buf_fill(buf, ARC_BUF_COMPRESSED(buf) != 0)); 2808321535Smav } 2809321535Smav 2810321535Smav return (0); 2811307265Smav} 2812168404Spjd 2813321535Smavstatic char *arc_onloan_tag = "onloan"; 2814321535Smav 2815321535Smavstatic inline void 2816321535Smavarc_loaned_bytes_update(int64_t delta) 2817307265Smav{ 2818321535Smav atomic_add_64(&arc_loaned_bytes, delta); 2819307265Smav 2820321535Smav /* assert that it did not wrap around */ 2821321535Smav ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); 2822168404Spjd} 2823168404Spjd 2824209962Smm/* 2825209962Smm * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 2826209962Smm * flight data by arc_tempreserve_space() until they are "returned". Loaned 2827209962Smm * buffers must be returned to the arc before they can be used by the DMU or 2828209962Smm * freed. 2829209962Smm */ 2830209962Smmarc_buf_t * 2831321535Smavarc_loan_buf(spa_t *spa, boolean_t is_metadata, int size) 2832209962Smm{ 2833321535Smav arc_buf_t *buf = arc_alloc_buf(spa, arc_onloan_tag, 2834321535Smav is_metadata ? ARC_BUFC_METADATA : ARC_BUFC_DATA, size); 2835209962Smm 2836321535Smav arc_loaned_bytes_update(size); 2837209962Smm 2838209962Smm return (buf); 2839209962Smm} 2840209962Smm 2841321535Smavarc_buf_t * 2842321535Smavarc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize, 2843321535Smav enum zio_compress compression_type) 2844321535Smav{ 2845321535Smav arc_buf_t *buf = arc_alloc_compressed_buf(spa, arc_onloan_tag, 2846321535Smav psize, lsize, compression_type); 2847321535Smav 2848321535Smav arc_loaned_bytes_update(psize); 2849321535Smav 2850321535Smav return (buf); 2851321535Smav} 2852321535Smav 2853321535Smav 2854209962Smm/* 2855209962Smm * Return a loaned arc buffer to the arc. 2856209962Smm */ 2857209962Smmvoid 2858209962Smmarc_return_buf(arc_buf_t *buf, void *tag) 2859209962Smm{ 2860209962Smm arc_buf_hdr_t *hdr = buf->b_hdr; 2861209962Smm 2862307265Smav ASSERT3P(buf->b_data, !=, NULL); 2863286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2864286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 2865286570Smav (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2866209962Smm 2867321535Smav arc_loaned_bytes_update(-arc_buf_size(buf)); 2868209962Smm} 2869209962Smm 2870219089Spjd/* Detach an arc_buf from a dbuf (tag) */ 2871219089Spjdvoid 2872219089Spjdarc_loan_inuse_buf(arc_buf_t *buf, void *tag) 2873219089Spjd{ 2874286570Smav arc_buf_hdr_t *hdr = buf->b_hdr; 2875219089Spjd 2876307265Smav ASSERT3P(buf->b_data, !=, NULL); 2877286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2878286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2879286570Smav (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); 2880219089Spjd 2881321535Smav arc_loaned_bytes_update(arc_buf_size(buf)); 2882219089Spjd} 2883219089Spjd 2884274172Savgstatic void 2885321610Smavl2arc_free_abd_on_write(abd_t *abd, size_t size, arc_buf_contents_t type) 2886274172Savg{ 2887307265Smav l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP); 2888274172Savg 2889321610Smav df->l2df_abd = abd; 2890274172Savg df->l2df_size = size; 2891307265Smav df->l2df_type = type; 2892274172Savg mutex_enter(&l2arc_free_on_write_mtx); 2893274172Savg list_insert_head(l2arc_free_on_write, df); 2894274172Savg mutex_exit(&l2arc_free_on_write_mtx); 2895274172Savg} 2896274172Savg 2897168404Spjdstatic void 2898307265Smavarc_hdr_free_on_write(arc_buf_hdr_t *hdr) 2899185029Spjd{ 2900307265Smav arc_state_t *state = hdr->b_l1hdr.b_state; 2901307265Smav arc_buf_contents_t type = arc_buf_type(hdr); 2902307265Smav uint64_t size = arc_hdr_size(hdr); 2903240133Smm 2904307265Smav /* protected by hash lock, if in the hash table */ 2905307265Smav if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 2906307265Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2907307265Smav ASSERT(state != arc_anon && state != arc_l2c_only); 2908307265Smav 2909307265Smav (void) refcount_remove_many(&state->arcs_esize[type], 2910307265Smav size, hdr); 2911185029Spjd } 2912307265Smav (void) refcount_remove_many(&state->arcs_size, size, hdr); 2913315834Savg if (type == ARC_BUFC_METADATA) { 2914315834Savg arc_space_return(size, ARC_SPACE_META); 2915315834Savg } else { 2916315834Savg ASSERT(type == ARC_BUFC_DATA); 2917315834Savg arc_space_return(size, ARC_SPACE_DATA); 2918315834Savg } 2919307265Smav 2920321610Smav l2arc_free_abd_on_write(hdr->b_l1hdr.b_pabd, size, type); 2921185029Spjd} 2922185029Spjd 2923307265Smav/* 2924307265Smav * Share the arc_buf_t's data with the hdr. Whenever we are sharing the 2925307265Smav * data buffer, we transfer the refcount ownership to the hdr and update 2926307265Smav * the appropriate kstats. 2927307265Smav */ 2928185029Spjdstatic void 2929307265Smavarc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2930274172Savg{ 2931307265Smav arc_state_t *state = hdr->b_l1hdr.b_state; 2932297848Savg 2933321535Smav ASSERT(arc_can_share(hdr, buf)); 2934321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2935307265Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2936274172Savg 2937286570Smav /* 2938307265Smav * Start sharing the data buffer. We transfer the 2939307265Smav * refcount ownership to the hdr since it always owns 2940307265Smav * the refcount whenever an arc_buf_t is shared. 2941286570Smav */ 2942307265Smav refcount_transfer_ownership(&state->arcs_size, buf, hdr); 2943321610Smav hdr->b_l1hdr.b_pabd = abd_get_from_buf(buf->b_data, arc_buf_size(buf)); 2944321610Smav abd_take_ownership_of_buf(hdr->b_l1hdr.b_pabd, 2945321610Smav HDR_ISTYPE_METADATA(hdr)); 2946307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); 2947321535Smav buf->b_flags |= ARC_BUF_FLAG_SHARED; 2948274172Savg 2949286763Smav /* 2950307265Smav * Since we've transferred ownership to the hdr we need 2951307265Smav * to increment its compressed and uncompressed kstats and 2952307265Smav * decrement the overhead size. 2953286763Smav */ 2954307265Smav ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); 2955307265Smav ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); 2956321535Smav ARCSTAT_INCR(arcstat_overhead_size, -arc_buf_size(buf)); 2957307265Smav} 2958274172Savg 2959307265Smavstatic void 2960307265Smavarc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2961307265Smav{ 2962307265Smav arc_state_t *state = hdr->b_l1hdr.b_state; 2963286570Smav 2964307265Smav ASSERT(arc_buf_is_shared(buf)); 2965321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 2966307265Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2967307265Smav 2968286763Smav /* 2969307265Smav * We are no longer sharing this buffer so we need 2970307265Smav * to transfer its ownership to the rightful owner. 2971286763Smav */ 2972307265Smav refcount_transfer_ownership(&state->arcs_size, hdr, buf); 2973307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 2974321610Smav abd_release_ownership_of_buf(hdr->b_l1hdr.b_pabd); 2975321610Smav abd_put(hdr->b_l1hdr.b_pabd); 2976321610Smav hdr->b_l1hdr.b_pabd = NULL; 2977321535Smav buf->b_flags &= ~ARC_BUF_FLAG_SHARED; 2978286763Smav 2979297848Savg /* 2980307265Smav * Since the buffer is no longer shared between 2981307265Smav * the arc buf and the hdr, count it as overhead. 2982297848Savg */ 2983307265Smav ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); 2984307265Smav ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); 2985321535Smav ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); 2986274172Savg} 2987274172Savg 2988286767Smav/* 2989321535Smav * Remove an arc_buf_t from the hdr's buf list and return the last 2990321535Smav * arc_buf_t on the list. If no buffers remain on the list then return 2991321535Smav * NULL. 2992286767Smav */ 2993321535Smavstatic arc_buf_t * 2994321535Smavarc_buf_remove(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2995321535Smav{ 2996321535Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2997321535Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 2998321535Smav 2999321535Smav arc_buf_t **bufp = &hdr->b_l1hdr.b_buf; 3000321535Smav arc_buf_t *lastbuf = NULL; 3001321535Smav 3002321535Smav /* 3003321535Smav * Remove the buf from the hdr list and locate the last 3004321535Smav * remaining buffer on the list. 3005321535Smav */ 3006321535Smav while (*bufp != NULL) { 3007321535Smav if (*bufp == buf) 3008321535Smav *bufp = buf->b_next; 3009321535Smav 3010321535Smav /* 3011321535Smav * If we've removed a buffer in the middle of 3012321535Smav * the list then update the lastbuf and update 3013321535Smav * bufp. 3014321535Smav */ 3015321535Smav if (*bufp != NULL) { 3016321535Smav lastbuf = *bufp; 3017321535Smav bufp = &(*bufp)->b_next; 3018321535Smav } 3019321535Smav } 3020321535Smav buf->b_next = NULL; 3021321535Smav ASSERT3P(lastbuf, !=, buf); 3022321535Smav IMPLY(hdr->b_l1hdr.b_bufcnt > 0, lastbuf != NULL); 3023321535Smav IMPLY(hdr->b_l1hdr.b_bufcnt > 0, hdr->b_l1hdr.b_buf != NULL); 3024321535Smav IMPLY(lastbuf != NULL, ARC_BUF_LAST(lastbuf)); 3025321535Smav 3026321535Smav return (lastbuf); 3027321535Smav} 3028321535Smav 3029321535Smav/* 3030321535Smav * Free up buf->b_data and pull the arc_buf_t off of the the arc_buf_hdr_t's 3031321535Smav * list and free it. 3032321535Smav */ 3033274172Savgstatic void 3034321535Smavarc_buf_destroy_impl(arc_buf_t *buf) 3035168404Spjd{ 3036307265Smav arc_buf_hdr_t *hdr = buf->b_hdr; 3037168404Spjd 3038307265Smav /* 3039321535Smav * Free up the data associated with the buf but only if we're not 3040321535Smav * sharing this with the hdr. If we are sharing it with the hdr, the 3041321535Smav * hdr is responsible for doing the free. 3042307265Smav */ 3043286570Smav if (buf->b_data != NULL) { 3044307265Smav /* 3045307265Smav * We're about to change the hdr's b_flags. We must either 3046307265Smav * hold the hash_lock or be undiscoverable. 3047307265Smav */ 3048307265Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); 3049168404Spjd 3050168404Spjd arc_cksum_verify(buf); 3051240133Smm#ifdef illumos 3052240133Smm arc_buf_unwatch(buf); 3053277300Ssmh#endif 3054219089Spjd 3055321535Smav if (arc_buf_is_shared(buf)) { 3056307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 3057286763Smav } else { 3058321535Smav uint64_t size = arc_buf_size(buf); 3059307265Smav arc_free_data_buf(hdr, buf->b_data, size, buf); 3060307265Smav ARCSTAT_INCR(arcstat_overhead_size, -size); 3061168404Spjd } 3062168404Spjd buf->b_data = NULL; 3063242845Sdelphij 3064307265Smav ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 3065307265Smav hdr->b_l1hdr.b_bufcnt -= 1; 3066168404Spjd } 3067168404Spjd 3068321535Smav arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); 3069168404Spjd 3070321535Smav if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) { 3071307265Smav /* 3072321535Smav * If the current arc_buf_t is sharing its data buffer with the 3073321610Smav * hdr, then reassign the hdr's b_pabd to share it with the new 3074321535Smav * buffer at the end of the list. The shared buffer is always 3075321535Smav * the last one on the hdr's buffer list. 3076321535Smav * 3077321535Smav * There is an equivalent case for compressed bufs, but since 3078321535Smav * they aren't guaranteed to be the last buf in the list and 3079321535Smav * that is an exceedingly rare case, we just allow that space be 3080321535Smav * wasted temporarily. 3081307265Smav */ 3082321535Smav if (lastbuf != NULL) { 3083321535Smav /* Only one buf can be shared at once */ 3084321535Smav VERIFY(!arc_buf_is_shared(lastbuf)); 3085321535Smav /* hdr is uncompressed so can't have compressed buf */ 3086321535Smav VERIFY(!ARC_BUF_COMPRESSED(lastbuf)); 3087168404Spjd 3088321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 3089321610Smav arc_hdr_free_pabd(hdr); 3090168404Spjd 3091321535Smav /* 3092321535Smav * We must setup a new shared block between the 3093321535Smav * last buffer and the hdr. The data would have 3094321535Smav * been allocated by the arc buf so we need to transfer 3095321535Smav * ownership to the hdr since it's now being shared. 3096321535Smav */ 3097321535Smav arc_share_buf(hdr, lastbuf); 3098321535Smav } 3099321535Smav } else if (HDR_SHARED_DATA(hdr)) { 3100307265Smav /* 3101321535Smav * Uncompressed shared buffers are always at the end 3102321535Smav * of the list. Compressed buffers don't have the 3103321535Smav * same requirements. This makes it hard to 3104321535Smav * simply assert that the lastbuf is shared so 3105321535Smav * we rely on the hdr's compression flags to determine 3106321535Smav * if we have a compressed, shared buffer. 3107307265Smav */ 3108321535Smav ASSERT3P(lastbuf, !=, NULL); 3109321535Smav ASSERT(arc_buf_is_shared(lastbuf) || 3110321535Smav HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF); 3111307265Smav } 3112307265Smav 3113321535Smav /* 3114321535Smav * Free the checksum if we're removing the last uncompressed buf from 3115321535Smav * this hdr. 3116321535Smav */ 3117321535Smav if (!arc_hdr_has_uncompressed_buf(hdr)) { 3118307265Smav arc_cksum_free(hdr); 3119321535Smav } 3120307265Smav 3121168404Spjd /* clean up the buf */ 3122168404Spjd buf->b_hdr = NULL; 3123168404Spjd kmem_cache_free(buf_cache, buf); 3124168404Spjd} 3125168404Spjd 3126168404Spjdstatic void 3127321610Smavarc_hdr_alloc_pabd(arc_buf_hdr_t *hdr) 3128286598Smav{ 3129307265Smav ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); 3130307265Smav ASSERT(HDR_HAS_L1HDR(hdr)); 3131307265Smav ASSERT(!HDR_SHARED_DATA(hdr)); 3132286598Smav 3133321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 3134321610Smav hdr->b_l1hdr.b_pabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr); 3135307265Smav hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 3136321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 3137307265Smav 3138307265Smav ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); 3139307265Smav ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); 3140307265Smav} 3141307265Smav 3142307265Smavstatic void 3143321610Smavarc_hdr_free_pabd(arc_buf_hdr_t *hdr) 3144307265Smav{ 3145307265Smav ASSERT(HDR_HAS_L1HDR(hdr)); 3146321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 3147307265Smav 3148307265Smav /* 3149307265Smav * If the hdr is currently being written to the l2arc then 3150307265Smav * we defer freeing the data by adding it to the l2arc_free_on_write 3151307265Smav * list. The l2arc will free the data once it's finished 3152307265Smav * writing it to the l2arc device. 3153307265Smav */ 3154307265Smav if (HDR_L2_WRITING(hdr)) { 3155307265Smav arc_hdr_free_on_write(hdr); 3156307265Smav ARCSTAT_BUMP(arcstat_l2_free_on_write); 3157307265Smav } else { 3158321610Smav arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, 3159307265Smav arc_hdr_size(hdr), hdr); 3160307265Smav } 3161321610Smav hdr->b_l1hdr.b_pabd = NULL; 3162307265Smav hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 3163307265Smav 3164307265Smav ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); 3165307265Smav ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); 3166307265Smav} 3167307265Smav 3168307265Smavstatic arc_buf_hdr_t * 3169307265Smavarc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize, 3170321535Smav enum zio_compress compression_type, arc_buf_contents_t type) 3171307265Smav{ 3172307265Smav arc_buf_hdr_t *hdr; 3173307265Smav 3174307265Smav VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA); 3175307265Smav 3176307265Smav hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 3177307265Smav ASSERT(HDR_EMPTY(hdr)); 3178307265Smav ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 3179307265Smav ASSERT3P(hdr->b_l1hdr.b_thawed, ==, NULL); 3180307265Smav HDR_SET_PSIZE(hdr, psize); 3181307265Smav HDR_SET_LSIZE(hdr, lsize); 3182307265Smav hdr->b_spa = spa; 3183307265Smav hdr->b_type = type; 3184307265Smav hdr->b_flags = 0; 3185307265Smav arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR); 3186321535Smav arc_hdr_set_compress(hdr, compression_type); 3187307265Smav 3188307265Smav hdr->b_l1hdr.b_state = arc_anon; 3189307265Smav hdr->b_l1hdr.b_arc_access = 0; 3190307265Smav hdr->b_l1hdr.b_bufcnt = 0; 3191307265Smav hdr->b_l1hdr.b_buf = NULL; 3192307265Smav 3193307265Smav /* 3194307265Smav * Allocate the hdr's buffer. This will contain either 3195307265Smav * the compressed or uncompressed data depending on the block 3196307265Smav * it references and compressed arc enablement. 3197307265Smav */ 3198321610Smav arc_hdr_alloc_pabd(hdr); 3199307265Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 3200307265Smav 3201307265Smav return (hdr); 3202307265Smav} 3203307265Smav 3204307265Smav/* 3205307265Smav * Transition between the two allocation states for the arc_buf_hdr struct. 3206307265Smav * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without 3207307265Smav * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller 3208307265Smav * version is used when a cache buffer is only in the L2ARC in order to reduce 3209307265Smav * memory usage. 3210307265Smav */ 3211307265Smavstatic arc_buf_hdr_t * 3212307265Smavarc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) 3213307265Smav{ 3214286598Smav ASSERT(HDR_HAS_L2HDR(hdr)); 3215286598Smav 3216307265Smav arc_buf_hdr_t *nhdr; 3217307265Smav l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 3218286598Smav 3219307265Smav ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || 3220307265Smav (old == hdr_l2only_cache && new == hdr_full_cache)); 3221307265Smav 3222307265Smav nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); 3223307265Smav 3224307265Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 3225307265Smav buf_hash_remove(hdr); 3226307265Smav 3227307265Smav bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); 3228307265Smav 3229307265Smav if (new == hdr_full_cache) { 3230307265Smav arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR); 3231307265Smav /* 3232307265Smav * arc_access and arc_change_state need to be aware that a 3233307265Smav * header has just come out of L2ARC, so we set its state to 3234307265Smav * l2c_only even though it's about to change. 3235307265Smav */ 3236307265Smav nhdr->b_l1hdr.b_state = arc_l2c_only; 3237307265Smav 3238307265Smav /* Verify previous threads set to NULL before freeing */ 3239321610Smav ASSERT3P(nhdr->b_l1hdr.b_pabd, ==, NULL); 3240307265Smav } else { 3241307265Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 3242307265Smav ASSERT0(hdr->b_l1hdr.b_bufcnt); 3243307265Smav ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 3244307265Smav 3245307265Smav /* 3246307265Smav * If we've reached here, We must have been called from 3247307265Smav * arc_evict_hdr(), as such we should have already been 3248307265Smav * removed from any ghost list we were previously on 3249307265Smav * (which protects us from racing with arc_evict_state), 3250307265Smav * thus no locking is needed during this check. 3251307265Smav */ 3252307265Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 3253307265Smav 3254307265Smav /* 3255307265Smav * A buffer must not be moved into the arc_l2c_only 3256307265Smav * state if it's not finished being written out to the 3257321610Smav * l2arc device. Otherwise, the b_l1hdr.b_pabd field 3258307265Smav * might try to be accessed, even though it was removed. 3259307265Smav */ 3260307265Smav VERIFY(!HDR_L2_WRITING(hdr)); 3261321610Smav VERIFY3P(hdr->b_l1hdr.b_pabd, ==, NULL); 3262307265Smav 3263307265Smav#ifdef ZFS_DEBUG 3264307265Smav if (hdr->b_l1hdr.b_thawed != NULL) { 3265307265Smav kmem_free(hdr->b_l1hdr.b_thawed, 1); 3266307265Smav hdr->b_l1hdr.b_thawed = NULL; 3267307265Smav } 3268307265Smav#endif 3269307265Smav 3270307265Smav arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR); 3271307265Smav } 3272286598Smav /* 3273307265Smav * The header has been reallocated so we need to re-insert it into any 3274307265Smav * lists it was on. 3275286598Smav */ 3276307265Smav (void) buf_hash_insert(nhdr, NULL); 3277286598Smav 3278307265Smav ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); 3279307265Smav 3280307265Smav mutex_enter(&dev->l2ad_mtx); 3281307265Smav 3282286598Smav /* 3283307265Smav * We must place the realloc'ed header back into the list at 3284307265Smav * the same spot. Otherwise, if it's placed earlier in the list, 3285307265Smav * l2arc_write_buffers() could find it during the function's 3286307265Smav * write phase, and try to write it out to the l2arc. 3287286598Smav */ 3288307265Smav list_insert_after(&dev->l2ad_buflist, hdr, nhdr); 3289307265Smav list_remove(&dev->l2ad_buflist, hdr); 3290286598Smav 3291307265Smav mutex_exit(&dev->l2ad_mtx); 3292307265Smav 3293286598Smav /* 3294307265Smav * Since we're using the pointer address as the tag when 3295307265Smav * incrementing and decrementing the l2ad_alloc refcount, we 3296307265Smav * must remove the old pointer (that we're about to destroy) and 3297307265Smav * add the new pointer to the refcount. Otherwise we'd remove 3298307265Smav * the wrong pointer address when calling arc_hdr_destroy() later. 3299286598Smav */ 3300286598Smav 3301307265Smav (void) refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr), hdr); 3302307265Smav (void) refcount_add_many(&dev->l2ad_alloc, arc_hdr_size(nhdr), nhdr); 3303286598Smav 3304307265Smav buf_discard_identity(hdr); 3305307265Smav kmem_cache_free(old, hdr); 3306286598Smav 3307307265Smav return (nhdr); 3308286598Smav} 3309286598Smav 3310307265Smav/* 3311307265Smav * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller. 3312307265Smav * The buf is returned thawed since we expect the consumer to modify it. 3313307265Smav */ 3314307265Smavarc_buf_t * 3315321535Smavarc_alloc_buf(spa_t *spa, void *tag, arc_buf_contents_t type, int32_t size) 3316307265Smav{ 3317307265Smav arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size, 3318307265Smav ZIO_COMPRESS_OFF, type); 3319307265Smav ASSERT(!MUTEX_HELD(HDR_LOCK(hdr))); 3320321535Smav 3321321535Smav arc_buf_t *buf = NULL; 3322321535Smav VERIFY0(arc_buf_alloc_impl(hdr, tag, B_FALSE, B_FALSE, &buf)); 3323307265Smav arc_buf_thaw(buf); 3324321535Smav 3325307265Smav return (buf); 3326307265Smav} 3327307265Smav 3328321535Smav/* 3329321535Smav * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this 3330321535Smav * for bufs containing metadata. 3331321535Smav */ 3332321535Smavarc_buf_t * 3333321535Smavarc_alloc_compressed_buf(spa_t *spa, void *tag, uint64_t psize, uint64_t lsize, 3334321535Smav enum zio_compress compression_type) 3335321535Smav{ 3336321535Smav ASSERT3U(lsize, >, 0); 3337321535Smav ASSERT3U(lsize, >=, psize); 3338321535Smav ASSERT(compression_type > ZIO_COMPRESS_OFF); 3339321535Smav ASSERT(compression_type < ZIO_COMPRESS_FUNCTIONS); 3340321535Smav 3341321535Smav arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, 3342321535Smav compression_type, ARC_BUFC_DATA); 3343321535Smav ASSERT(!MUTEX_HELD(HDR_LOCK(hdr))); 3344321535Smav 3345321535Smav arc_buf_t *buf = NULL; 3346321535Smav VERIFY0(arc_buf_alloc_impl(hdr, tag, B_TRUE, B_FALSE, &buf)); 3347321535Smav arc_buf_thaw(buf); 3348321535Smav ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 3349321535Smav 3350321610Smav if (!arc_buf_is_shared(buf)) { 3351321610Smav /* 3352321610Smav * To ensure that the hdr has the correct data in it if we call 3353321610Smav * arc_decompress() on this buf before it's been written to 3354321610Smav * disk, it's easiest if we just set up sharing between the 3355321610Smav * buf and the hdr. 3356321610Smav */ 3357321610Smav ASSERT(!abd_is_linear(hdr->b_l1hdr.b_pabd)); 3358321610Smav arc_hdr_free_pabd(hdr); 3359321610Smav arc_share_buf(hdr, buf); 3360321610Smav } 3361321610Smav 3362321535Smav return (buf); 3363321535Smav} 3364321535Smav 3365286598Smavstatic void 3366307265Smavarc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) 3367307265Smav{ 3368307265Smav l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 3369307265Smav l2arc_dev_t *dev = l2hdr->b_dev; 3370323754Savg uint64_t psize = arc_hdr_size(hdr); 3371307265Smav 3372307265Smav ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); 3373307265Smav ASSERT(HDR_HAS_L2HDR(hdr)); 3374307265Smav 3375307265Smav list_remove(&dev->l2ad_buflist, hdr); 3376307265Smav 3377323754Savg ARCSTAT_INCR(arcstat_l2_psize, -psize); 3378323754Savg ARCSTAT_INCR(arcstat_l2_lsize, -HDR_GET_LSIZE(hdr)); 3379307265Smav 3380323754Savg vdev_space_update(dev->l2ad_vdev, -psize, 0, 0); 3381307265Smav 3382323754Savg (void) refcount_remove_many(&dev->l2ad_alloc, psize, hdr); 3383307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); 3384307265Smav} 3385307265Smav 3386307265Smavstatic void 3387168404Spjdarc_hdr_destroy(arc_buf_hdr_t *hdr) 3388168404Spjd{ 3389286570Smav if (HDR_HAS_L1HDR(hdr)) { 3390286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL || 3391307265Smav hdr->b_l1hdr.b_bufcnt > 0); 3392286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 3393286570Smav ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 3394286570Smav } 3395168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3396286570Smav ASSERT(!HDR_IN_HASH_TABLE(hdr)); 3397168404Spjd 3398307265Smav if (!HDR_EMPTY(hdr)) 3399307265Smav buf_discard_identity(hdr); 3400307265Smav 3401286570Smav if (HDR_HAS_L2HDR(hdr)) { 3402286598Smav l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 3403286598Smav boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); 3404286570Smav 3405286598Smav if (!buflist_held) 3406286598Smav mutex_enter(&dev->l2ad_mtx); 3407219089Spjd 3408286570Smav /* 3409286598Smav * Even though we checked this conditional above, we 3410286598Smav * need to check this again now that we have the 3411286598Smav * l2ad_mtx. This is because we could be racing with 3412286598Smav * another thread calling l2arc_evict() which might have 3413286598Smav * destroyed this header's L2 portion as we were waiting 3414286598Smav * to acquire the l2ad_mtx. If that happens, we don't 3415286598Smav * want to re-destroy the header's L2 portion. 3416286570Smav */ 3417286598Smav if (HDR_HAS_L2HDR(hdr)) { 3418290191Savg l2arc_trim(hdr); 3419286598Smav arc_hdr_l2hdr_destroy(hdr); 3420286598Smav } 3421286570Smav 3422219089Spjd if (!buflist_held) 3423286598Smav mutex_exit(&dev->l2ad_mtx); 3424185029Spjd } 3425185029Spjd 3426307265Smav if (HDR_HAS_L1HDR(hdr)) { 3427307265Smav arc_cksum_free(hdr); 3428286776Smav 3429307265Smav while (hdr->b_l1hdr.b_buf != NULL) 3430321535Smav arc_buf_destroy_impl(hdr->b_l1hdr.b_buf); 3431286570Smav 3432286570Smav#ifdef ZFS_DEBUG 3433286570Smav if (hdr->b_l1hdr.b_thawed != NULL) { 3434286570Smav kmem_free(hdr->b_l1hdr.b_thawed, 1); 3435286570Smav hdr->b_l1hdr.b_thawed = NULL; 3436286570Smav } 3437286570Smav#endif 3438307265Smav 3439321610Smav if (hdr->b_l1hdr.b_pabd != NULL) { 3440321610Smav arc_hdr_free_pabd(hdr); 3441307265Smav } 3442219089Spjd } 3443168404Spjd 3444168404Spjd ASSERT3P(hdr->b_hash_next, ==, NULL); 3445286570Smav if (HDR_HAS_L1HDR(hdr)) { 3446286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 3447286570Smav ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 3448286570Smav kmem_cache_free(hdr_full_cache, hdr); 3449286570Smav } else { 3450286570Smav kmem_cache_free(hdr_l2only_cache, hdr); 3451286570Smav } 3452168404Spjd} 3453168404Spjd 3454168404Spjdvoid 3455307265Smavarc_buf_destroy(arc_buf_t *buf, void* tag) 3456168404Spjd{ 3457168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3458168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 3459168404Spjd 3460286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 3461307265Smav ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 3462307265Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3463307265Smav VERIFY0(remove_reference(hdr, NULL, tag)); 3464307265Smav arc_hdr_destroy(hdr); 3465307265Smav return; 3466168404Spjd } 3467168404Spjd 3468168404Spjd mutex_enter(hash_lock); 3469307265Smav ASSERT3P(hdr, ==, buf->b_hdr); 3470307265Smav ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 3471219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3472307265Smav ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon); 3473307265Smav ASSERT3P(buf->b_data, !=, NULL); 3474168404Spjd 3475168404Spjd (void) remove_reference(hdr, hash_lock, tag); 3476321535Smav arc_buf_destroy_impl(buf); 3477168404Spjd mutex_exit(hash_lock); 3478168404Spjd} 3479168404Spjd 3480168404Spjd/* 3481286763Smav * Evict the arc_buf_hdr that is provided as a parameter. The resultant 3482286763Smav * state of the header is dependent on it's state prior to entering this 3483286763Smav * function. The following transitions are possible: 3484185029Spjd * 3485286763Smav * - arc_mru -> arc_mru_ghost 3486286763Smav * - arc_mfu -> arc_mfu_ghost 3487286763Smav * - arc_mru_ghost -> arc_l2c_only 3488286763Smav * - arc_mru_ghost -> deleted 3489286763Smav * - arc_mfu_ghost -> arc_l2c_only 3490286763Smav * - arc_mfu_ghost -> deleted 3491168404Spjd */ 3492286763Smavstatic int64_t 3493286763Smavarc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 3494168404Spjd{ 3495286763Smav arc_state_t *evicted_state, *state; 3496286763Smav int64_t bytes_evicted = 0; 3497168404Spjd 3498286763Smav ASSERT(MUTEX_HELD(hash_lock)); 3499286763Smav ASSERT(HDR_HAS_L1HDR(hdr)); 3500168404Spjd 3501286763Smav state = hdr->b_l1hdr.b_state; 3502286763Smav if (GHOST_STATE(state)) { 3503286763Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3504307265Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 3505206796Spjd 3506286763Smav /* 3507286763Smav * l2arc_write_buffers() relies on a header's L1 portion 3508321610Smav * (i.e. its b_pabd field) during it's write phase. 3509286763Smav * Thus, we cannot push a header onto the arc_l2c_only 3510286763Smav * state (removing it's L1 piece) until the header is 3511286763Smav * done being written to the l2arc. 3512286763Smav */ 3513286763Smav if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { 3514286763Smav ARCSTAT_BUMP(arcstat_evict_l2_skip); 3515286763Smav return (bytes_evicted); 3516286763Smav } 3517286762Smav 3518286763Smav ARCSTAT_BUMP(arcstat_deleted); 3519307265Smav bytes_evicted += HDR_GET_LSIZE(hdr); 3520286762Smav 3521286763Smav DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); 3522286763Smav 3523321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 3524286763Smav if (HDR_HAS_L2HDR(hdr)) { 3525275780Sdelphij /* 3526286763Smav * This buffer is cached on the 2nd Level ARC; 3527286763Smav * don't destroy the header. 3528275780Sdelphij */ 3529286763Smav arc_change_state(arc_l2c_only, hdr, hash_lock); 3530286763Smav /* 3531286763Smav * dropping from L1+L2 cached to L2-only, 3532286763Smav * realloc to remove the L1 header. 3533286763Smav */ 3534286763Smav hdr = arc_hdr_realloc(hdr, hdr_full_cache, 3535286763Smav hdr_l2only_cache); 3536286763Smav } else { 3537286763Smav arc_change_state(arc_anon, hdr, hash_lock); 3538286763Smav arc_hdr_destroy(hdr); 3539275780Sdelphij } 3540286763Smav return (bytes_evicted); 3541275780Sdelphij } 3542275780Sdelphij 3543286763Smav ASSERT(state == arc_mru || state == arc_mfu); 3544286763Smav evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 3545206796Spjd 3546286763Smav /* prefetch buffers have a minimum lifespan */ 3547286763Smav if (HDR_IO_IN_PROGRESS(hdr) || 3548286763Smav ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && 3549286763Smav ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < 3550286763Smav arc_min_prefetch_lifespan)) { 3551286763Smav ARCSTAT_BUMP(arcstat_evict_skip); 3552286763Smav return (bytes_evicted); 3553286763Smav } 3554286763Smav 3555286763Smav ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 3556286763Smav while (hdr->b_l1hdr.b_buf) { 3557286763Smav arc_buf_t *buf = hdr->b_l1hdr.b_buf; 3558286763Smav if (!mutex_tryenter(&buf->b_evict_lock)) { 3559286763Smav ARCSTAT_BUMP(arcstat_mutex_miss); 3560286763Smav break; 3561168404Spjd } 3562286763Smav if (buf->b_data != NULL) 3563307265Smav bytes_evicted += HDR_GET_LSIZE(hdr); 3564307265Smav mutex_exit(&buf->b_evict_lock); 3565321535Smav arc_buf_destroy_impl(buf); 3566286763Smav } 3567258632Savg 3568286763Smav if (HDR_HAS_L2HDR(hdr)) { 3569307265Smav ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr)); 3570286763Smav } else { 3571307265Smav if (l2arc_write_eligible(hdr->b_spa, hdr)) { 3572307265Smav ARCSTAT_INCR(arcstat_evict_l2_eligible, 3573307265Smav HDR_GET_LSIZE(hdr)); 3574307265Smav } else { 3575307265Smav ARCSTAT_INCR(arcstat_evict_l2_ineligible, 3576307265Smav HDR_GET_LSIZE(hdr)); 3577307265Smav } 3578286763Smav } 3579258632Savg 3580307265Smav if (hdr->b_l1hdr.b_bufcnt == 0) { 3581307265Smav arc_cksum_free(hdr); 3582307265Smav 3583307265Smav bytes_evicted += arc_hdr_size(hdr); 3584307265Smav 3585307265Smav /* 3586307265Smav * If this hdr is being evicted and has a compressed 3587307265Smav * buffer then we discard it here before we change states. 3588307265Smav * This ensures that the accounting is updated correctly 3589321610Smav * in arc_free_data_impl(). 3590307265Smav */ 3591321610Smav arc_hdr_free_pabd(hdr); 3592307265Smav 3593286763Smav arc_change_state(evicted_state, hdr, hash_lock); 3594286763Smav ASSERT(HDR_IN_HASH_TABLE(hdr)); 3595307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 3596286763Smav DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); 3597286763Smav } 3598286763Smav 3599286763Smav return (bytes_evicted); 3600286763Smav} 3601286763Smav 3602286763Smavstatic uint64_t 3603286763Smavarc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, 3604286763Smav uint64_t spa, int64_t bytes) 3605286763Smav{ 3606286763Smav multilist_sublist_t *mls; 3607286763Smav uint64_t bytes_evicted = 0; 3608286763Smav arc_buf_hdr_t *hdr; 3609286763Smav kmutex_t *hash_lock; 3610286763Smav int evict_count = 0; 3611286763Smav 3612286763Smav ASSERT3P(marker, !=, NULL); 3613286763Smav IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 3614286763Smav 3615286763Smav mls = multilist_sublist_lock(ml, idx); 3616286763Smav 3617286763Smav for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL; 3618286763Smav hdr = multilist_sublist_prev(mls, marker)) { 3619286763Smav if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) || 3620286763Smav (evict_count >= zfs_arc_evict_batch_limit)) 3621286763Smav break; 3622286763Smav 3623258632Savg /* 3624286763Smav * To keep our iteration location, move the marker 3625286763Smav * forward. Since we're not holding hdr's hash lock, we 3626286763Smav * must be very careful and not remove 'hdr' from the 3627286763Smav * sublist. Otherwise, other consumers might mistake the 3628286763Smav * 'hdr' as not being on a sublist when they call the 3629286763Smav * multilist_link_active() function (they all rely on 3630286763Smav * the hash lock protecting concurrent insertions and 3631286763Smav * removals). multilist_sublist_move_forward() was 3632286763Smav * specifically implemented to ensure this is the case 3633286763Smav * (only 'marker' will be removed and re-inserted). 3634258632Savg */ 3635286763Smav multilist_sublist_move_forward(mls, marker); 3636286763Smav 3637286763Smav /* 3638286763Smav * The only case where the b_spa field should ever be 3639286763Smav * zero, is the marker headers inserted by 3640286763Smav * arc_evict_state(). It's possible for multiple threads 3641286763Smav * to be calling arc_evict_state() concurrently (e.g. 3642286763Smav * dsl_pool_close() and zio_inject_fault()), so we must 3643286763Smav * skip any markers we see from these other threads. 3644286763Smav */ 3645286763Smav if (hdr->b_spa == 0) 3646258632Savg continue; 3647286763Smav 3648286763Smav /* we're only interested in evicting buffers of a certain spa */ 3649286763Smav if (spa != 0 && hdr->b_spa != spa) { 3650286763Smav ARCSTAT_BUMP(arcstat_evict_skip); 3651286763Smav continue; 3652258632Savg } 3653258632Savg 3654275811Sdelphij hash_lock = HDR_LOCK(hdr); 3655208373Smm 3656286763Smav /* 3657286763Smav * We aren't calling this function from any code path 3658286763Smav * that would already be holding a hash lock, so we're 3659286763Smav * asserting on this assumption to be defensive in case 3660286763Smav * this ever changes. Without this check, it would be 3661286763Smav * possible to incorrectly increment arcstat_mutex_miss 3662286763Smav * below (e.g. if the code changed such that we called 3663286763Smav * this function with a hash lock held). 3664286763Smav */ 3665286763Smav ASSERT(!MUTEX_HELD(hash_lock)); 3666208373Smm 3667286763Smav if (mutex_tryenter(hash_lock)) { 3668286763Smav uint64_t evicted = arc_evict_hdr(hdr, hash_lock); 3669286763Smav mutex_exit(hash_lock); 3670286763Smav 3671286763Smav bytes_evicted += evicted; 3672286763Smav 3673286763Smav /* 3674286763Smav * If evicted is zero, arc_evict_hdr() must have 3675286763Smav * decided to skip this header, don't increment 3676286763Smav * evict_count in this case. 3677286763Smav */ 3678286763Smav if (evicted != 0) 3679286763Smav evict_count++; 3680286763Smav 3681286763Smav /* 3682286763Smav * If arc_size isn't overflowing, signal any 3683286763Smav * threads that might happen to be waiting. 3684286763Smav * 3685286763Smav * For each header evicted, we wake up a single 3686286763Smav * thread. If we used cv_broadcast, we could 3687286763Smav * wake up "too many" threads causing arc_size 3688286763Smav * to significantly overflow arc_c; since 3689321610Smav * arc_get_data_impl() doesn't check for overflow 3690286763Smav * when it's woken up (it doesn't because it's 3691286763Smav * possible for the ARC to be overflowing while 3692286763Smav * full of un-evictable buffers, and the 3693286763Smav * function should proceed in this case). 3694286763Smav * 3695286763Smav * If threads are left sleeping, due to not 3696286763Smav * using cv_broadcast, they will be woken up 3697286763Smav * just before arc_reclaim_thread() sleeps. 3698286763Smav */ 3699286763Smav mutex_enter(&arc_reclaim_lock); 3700286763Smav if (!arc_is_overflowing()) 3701286763Smav cv_signal(&arc_reclaim_waiters_cv); 3702286763Smav mutex_exit(&arc_reclaim_lock); 3703168404Spjd } else { 3704286763Smav ARCSTAT_BUMP(arcstat_mutex_miss); 3705168404Spjd } 3706168404Spjd } 3707168404Spjd 3708286763Smav multilist_sublist_unlock(mls); 3709206796Spjd 3710286763Smav return (bytes_evicted); 3711286763Smav} 3712168404Spjd 3713286763Smav/* 3714286763Smav * Evict buffers from the given arc state, until we've removed the 3715286763Smav * specified number of bytes. Move the removed buffers to the 3716286763Smav * appropriate evict state. 3717286763Smav * 3718286763Smav * This function makes a "best effort". It skips over any buffers 3719286763Smav * it can't get a hash_lock on, and so, may not catch all candidates. 3720286763Smav * It may also return without evicting as much space as requested. 3721286763Smav * 3722286763Smav * If bytes is specified using the special value ARC_EVICT_ALL, this 3723286763Smav * will evict all available (i.e. unlocked and evictable) buffers from 3724286763Smav * the given arc state; which is used by arc_flush(). 3725286763Smav */ 3726286763Smavstatic uint64_t 3727286763Smavarc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes, 3728286763Smav arc_buf_contents_t type) 3729286763Smav{ 3730286763Smav uint64_t total_evicted = 0; 3731321553Smav multilist_t *ml = state->arcs_list[type]; 3732286763Smav int num_sublists; 3733286763Smav arc_buf_hdr_t **markers; 3734168404Spjd 3735286763Smav IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 3736168404Spjd 3737286763Smav num_sublists = multilist_get_num_sublists(ml); 3738286763Smav 3739185029Spjd /* 3740286763Smav * If we've tried to evict from each sublist, made some 3741286763Smav * progress, but still have not hit the target number of bytes 3742286763Smav * to evict, we want to keep trying. The markers allow us to 3743286763Smav * pick up where we left off for each individual sublist, rather 3744286763Smav * than starting from the tail each time. 3745185029Spjd */ 3746286763Smav markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); 3747286763Smav for (int i = 0; i < num_sublists; i++) { 3748286763Smav markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); 3749185029Spjd 3750286763Smav /* 3751286763Smav * A b_spa of 0 is used to indicate that this header is 3752286763Smav * a marker. This fact is used in arc_adjust_type() and 3753286763Smav * arc_evict_state_impl(). 3754286763Smav */ 3755286763Smav markers[i]->b_spa = 0; 3756168404Spjd 3757286763Smav multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 3758286763Smav multilist_sublist_insert_tail(mls, markers[i]); 3759286763Smav multilist_sublist_unlock(mls); 3760286763Smav } 3761168404Spjd 3762286763Smav /* 3763286763Smav * While we haven't hit our target number of bytes to evict, or 3764286763Smav * we're evicting all available buffers. 3765286763Smav */ 3766286763Smav while (total_evicted < bytes || bytes == ARC_EVICT_ALL) { 3767286763Smav /* 3768286763Smav * Start eviction using a randomly selected sublist, 3769286763Smav * this is to try and evenly balance eviction across all 3770286763Smav * sublists. Always starting at the same sublist 3771286763Smav * (e.g. index 0) would cause evictions to favor certain 3772286763Smav * sublists over others. 3773286763Smav */ 3774286763Smav int sublist_idx = multilist_get_random_index(ml); 3775286763Smav uint64_t scan_evicted = 0; 3776219089Spjd 3777286763Smav for (int i = 0; i < num_sublists; i++) { 3778286763Smav uint64_t bytes_remaining; 3779286763Smav uint64_t bytes_evicted; 3780219089Spjd 3781286763Smav if (bytes == ARC_EVICT_ALL) 3782286763Smav bytes_remaining = ARC_EVICT_ALL; 3783286763Smav else if (total_evicted < bytes) 3784286763Smav bytes_remaining = bytes - total_evicted; 3785286763Smav else 3786286763Smav break; 3787258632Savg 3788286763Smav bytes_evicted = arc_evict_state_impl(ml, sublist_idx, 3789286763Smav markers[sublist_idx], spa, bytes_remaining); 3790286763Smav 3791286763Smav scan_evicted += bytes_evicted; 3792286763Smav total_evicted += bytes_evicted; 3793286763Smav 3794286763Smav /* we've reached the end, wrap to the beginning */ 3795286763Smav if (++sublist_idx >= num_sublists) 3796286763Smav sublist_idx = 0; 3797286763Smav } 3798286763Smav 3799258632Savg /* 3800286763Smav * If we didn't evict anything during this scan, we have 3801286763Smav * no reason to believe we'll evict more during another 3802286763Smav * scan, so break the loop. 3803258632Savg */ 3804286763Smav if (scan_evicted == 0) { 3805286763Smav /* This isn't possible, let's make that obvious */ 3806286763Smav ASSERT3S(bytes, !=, 0); 3807185029Spjd 3808286763Smav /* 3809286763Smav * When bytes is ARC_EVICT_ALL, the only way to 3810286763Smav * break the loop is when scan_evicted is zero. 3811286763Smav * In that case, we actually have evicted enough, 3812286763Smav * so we don't want to increment the kstat. 3813286763Smav */ 3814286763Smav if (bytes != ARC_EVICT_ALL) { 3815286763Smav ASSERT3S(total_evicted, <, bytes); 3816286763Smav ARCSTAT_BUMP(arcstat_evict_not_enough); 3817185029Spjd } 3818185029Spjd 3819286763Smav break; 3820258632Savg } 3821286763Smav } 3822258632Savg 3823286763Smav for (int i = 0; i < num_sublists; i++) { 3824286763Smav multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 3825286763Smav multilist_sublist_remove(mls, markers[i]); 3826286763Smav multilist_sublist_unlock(mls); 3827286763Smav 3828286763Smav kmem_cache_free(hdr_full_cache, markers[i]); 3829168404Spjd } 3830286763Smav kmem_free(markers, sizeof (*markers) * num_sublists); 3831206796Spjd 3832286763Smav return (total_evicted); 3833286763Smav} 3834286763Smav 3835286763Smav/* 3836286763Smav * Flush all "evictable" data of the given type from the arc state 3837286763Smav * specified. This will not evict any "active" buffers (i.e. referenced). 3838286763Smav * 3839307265Smav * When 'retry' is set to B_FALSE, the function will make a single pass 3840286763Smav * over the state and evict any buffers that it can. Since it doesn't 3841286763Smav * continually retry the eviction, it might end up leaving some buffers 3842286763Smav * in the ARC due to lock misses. 3843286763Smav * 3844307265Smav * When 'retry' is set to B_TRUE, the function will continually retry the 3845286763Smav * eviction until *all* evictable buffers have been removed from the 3846286763Smav * state. As a result, if concurrent insertions into the state are 3847286763Smav * allowed (e.g. if the ARC isn't shutting down), this function might 3848286763Smav * wind up in an infinite loop, continually trying to evict buffers. 3849286763Smav */ 3850286763Smavstatic uint64_t 3851286763Smavarc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, 3852286763Smav boolean_t retry) 3853286763Smav{ 3854286763Smav uint64_t evicted = 0; 3855286763Smav 3856307265Smav while (refcount_count(&state->arcs_esize[type]) != 0) { 3857286763Smav evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); 3858286763Smav 3859286763Smav if (!retry) 3860286763Smav break; 3861185029Spjd } 3862185029Spjd 3863286763Smav return (evicted); 3864286763Smav} 3865286763Smav 3866286763Smav/* 3867286763Smav * Evict the specified number of bytes from the state specified, 3868286763Smav * restricting eviction to the spa and type given. This function 3869286763Smav * prevents us from trying to evict more from a state's list than 3870286763Smav * is "evictable", and to skip evicting altogether when passed a 3871286763Smav * negative value for "bytes". In contrast, arc_evict_state() will 3872286763Smav * evict everything it can, when passed a negative value for "bytes". 3873286763Smav */ 3874286763Smavstatic uint64_t 3875286763Smavarc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes, 3876286763Smav arc_buf_contents_t type) 3877286763Smav{ 3878286763Smav int64_t delta; 3879286763Smav 3880307265Smav if (bytes > 0 && refcount_count(&state->arcs_esize[type]) > 0) { 3881307265Smav delta = MIN(refcount_count(&state->arcs_esize[type]), bytes); 3882286763Smav return (arc_evict_state(state, spa, delta, type)); 3883168404Spjd } 3884168404Spjd 3885286763Smav return (0); 3886168404Spjd} 3887168404Spjd 3888286763Smav/* 3889286763Smav * Evict metadata buffers from the cache, such that arc_meta_used is 3890286763Smav * capped by the arc_meta_limit tunable. 3891286763Smav */ 3892286763Smavstatic uint64_t 3893286763Smavarc_adjust_meta(void) 3894286763Smav{ 3895286763Smav uint64_t total_evicted = 0; 3896286763Smav int64_t target; 3897286763Smav 3898286763Smav /* 3899286763Smav * If we're over the meta limit, we want to evict enough 3900286763Smav * metadata to get back under the meta limit. We don't want to 3901286763Smav * evict so much that we drop the MRU below arc_p, though. If 3902286763Smav * we're over the meta limit more than we're over arc_p, we 3903286763Smav * evict some from the MRU here, and some from the MFU below. 3904286763Smav */ 3905286763Smav target = MIN((int64_t)(arc_meta_used - arc_meta_limit), 3906286766Smav (int64_t)(refcount_count(&arc_anon->arcs_size) + 3907286766Smav refcount_count(&arc_mru->arcs_size) - arc_p)); 3908286763Smav 3909286763Smav total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3910286763Smav 3911286763Smav /* 3912286763Smav * Similar to the above, we want to evict enough bytes to get us 3913286763Smav * below the meta limit, but not so much as to drop us below the 3914321535Smav * space allotted to the MFU (which is defined as arc_c - arc_p). 3915286763Smav */ 3916286763Smav target = MIN((int64_t)(arc_meta_used - arc_meta_limit), 3917286766Smav (int64_t)(refcount_count(&arc_mfu->arcs_size) - (arc_c - arc_p))); 3918286763Smav 3919286763Smav total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3920286763Smav 3921286763Smav return (total_evicted); 3922286763Smav} 3923286763Smav 3924286763Smav/* 3925286763Smav * Return the type of the oldest buffer in the given arc state 3926286763Smav * 3927286763Smav * This function will select a random sublist of type ARC_BUFC_DATA and 3928286763Smav * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist 3929286763Smav * is compared, and the type which contains the "older" buffer will be 3930286763Smav * returned. 3931286763Smav */ 3932286763Smavstatic arc_buf_contents_t 3933286763Smavarc_adjust_type(arc_state_t *state) 3934286763Smav{ 3935321553Smav multilist_t *data_ml = state->arcs_list[ARC_BUFC_DATA]; 3936321553Smav multilist_t *meta_ml = state->arcs_list[ARC_BUFC_METADATA]; 3937286763Smav int data_idx = multilist_get_random_index(data_ml); 3938286763Smav int meta_idx = multilist_get_random_index(meta_ml); 3939286763Smav multilist_sublist_t *data_mls; 3940286763Smav multilist_sublist_t *meta_mls; 3941286763Smav arc_buf_contents_t type; 3942286763Smav arc_buf_hdr_t *data_hdr; 3943286763Smav arc_buf_hdr_t *meta_hdr; 3944286763Smav 3945286763Smav /* 3946286763Smav * We keep the sublist lock until we're finished, to prevent 3947286763Smav * the headers from being destroyed via arc_evict_state(). 3948286763Smav */ 3949286763Smav data_mls = multilist_sublist_lock(data_ml, data_idx); 3950286763Smav meta_mls = multilist_sublist_lock(meta_ml, meta_idx); 3951286763Smav 3952286763Smav /* 3953286763Smav * These two loops are to ensure we skip any markers that 3954286763Smav * might be at the tail of the lists due to arc_evict_state(). 3955286763Smav */ 3956286763Smav 3957286763Smav for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; 3958286763Smav data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { 3959286763Smav if (data_hdr->b_spa != 0) 3960286763Smav break; 3961286763Smav } 3962286763Smav 3963286763Smav for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; 3964286763Smav meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { 3965286763Smav if (meta_hdr->b_spa != 0) 3966286763Smav break; 3967286763Smav } 3968286763Smav 3969286763Smav if (data_hdr == NULL && meta_hdr == NULL) { 3970286763Smav type = ARC_BUFC_DATA; 3971286763Smav } else if (data_hdr == NULL) { 3972286763Smav ASSERT3P(meta_hdr, !=, NULL); 3973286763Smav type = ARC_BUFC_METADATA; 3974286763Smav } else if (meta_hdr == NULL) { 3975286763Smav ASSERT3P(data_hdr, !=, NULL); 3976286763Smav type = ARC_BUFC_DATA; 3977286763Smav } else { 3978286763Smav ASSERT3P(data_hdr, !=, NULL); 3979286763Smav ASSERT3P(meta_hdr, !=, NULL); 3980286763Smav 3981286763Smav /* The headers can't be on the sublist without an L1 header */ 3982286763Smav ASSERT(HDR_HAS_L1HDR(data_hdr)); 3983286763Smav ASSERT(HDR_HAS_L1HDR(meta_hdr)); 3984286763Smav 3985286763Smav if (data_hdr->b_l1hdr.b_arc_access < 3986286763Smav meta_hdr->b_l1hdr.b_arc_access) { 3987286763Smav type = ARC_BUFC_DATA; 3988286763Smav } else { 3989286763Smav type = ARC_BUFC_METADATA; 3990286763Smav } 3991286763Smav } 3992286763Smav 3993286763Smav multilist_sublist_unlock(meta_mls); 3994286763Smav multilist_sublist_unlock(data_mls); 3995286763Smav 3996286763Smav return (type); 3997286763Smav} 3998286763Smav 3999286763Smav/* 4000286763Smav * Evict buffers from the cache, such that arc_size is capped by arc_c. 4001286763Smav */ 4002286763Smavstatic uint64_t 4003168404Spjdarc_adjust(void) 4004168404Spjd{ 4005286763Smav uint64_t total_evicted = 0; 4006286763Smav uint64_t bytes; 4007286763Smav int64_t target; 4008168404Spjd 4009208373Smm /* 4010286763Smav * If we're over arc_meta_limit, we want to correct that before 4011286763Smav * potentially evicting data buffers below. 4012286763Smav */ 4013286763Smav total_evicted += arc_adjust_meta(); 4014286763Smav 4015286763Smav /* 4016208373Smm * Adjust MRU size 4017286763Smav * 4018286763Smav * If we're over the target cache size, we want to evict enough 4019286763Smav * from the list to get back to our target size. We don't want 4020286763Smav * to evict too much from the MRU, such that it drops below 4021286763Smav * arc_p. So, if we're over our target cache size more than 4022286763Smav * the MRU is over arc_p, we'll evict enough to get back to 4023286763Smav * arc_p here, and then evict more from the MFU below. 4024208373Smm */ 4025286763Smav target = MIN((int64_t)(arc_size - arc_c), 4026286766Smav (int64_t)(refcount_count(&arc_anon->arcs_size) + 4027286766Smav refcount_count(&arc_mru->arcs_size) + arc_meta_used - arc_p)); 4028208373Smm 4029286763Smav /* 4030286763Smav * If we're below arc_meta_min, always prefer to evict data. 4031286763Smav * Otherwise, try to satisfy the requested number of bytes to 4032286763Smav * evict from the type which contains older buffers; in an 4033286763Smav * effort to keep newer buffers in the cache regardless of their 4034286763Smav * type. If we cannot satisfy the number of bytes from this 4035286763Smav * type, spill over into the next type. 4036286763Smav */ 4037286763Smav if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA && 4038286763Smav arc_meta_used > arc_meta_min) { 4039286763Smav bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 4040286763Smav total_evicted += bytes; 4041168404Spjd 4042286763Smav /* 4043286763Smav * If we couldn't evict our target number of bytes from 4044286763Smav * metadata, we try to get the rest from data. 4045286763Smav */ 4046286763Smav target -= bytes; 4047286763Smav 4048286763Smav total_evicted += 4049286763Smav arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 4050286763Smav } else { 4051286763Smav bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 4052286763Smav total_evicted += bytes; 4053286763Smav 4054286763Smav /* 4055286763Smav * If we couldn't evict our target number of bytes from 4056286763Smav * data, we try to get the rest from metadata. 4057286763Smav */ 4058286763Smav target -= bytes; 4059286763Smav 4060286763Smav total_evicted += 4061286763Smav arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 4062185029Spjd } 4063185029Spjd 4064208373Smm /* 4065208373Smm * Adjust MFU size 4066286763Smav * 4067286763Smav * Now that we've tried to evict enough from the MRU to get its 4068286763Smav * size back to arc_p, if we're still above the target cache 4069286763Smav * size, we evict the rest from the MFU. 4070208373Smm */ 4071286763Smav target = arc_size - arc_c; 4072168404Spjd 4073286764Smav if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA && 4074286763Smav arc_meta_used > arc_meta_min) { 4075286763Smav bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 4076286763Smav total_evicted += bytes; 4077208373Smm 4078286763Smav /* 4079286763Smav * If we couldn't evict our target number of bytes from 4080286763Smav * metadata, we try to get the rest from data. 4081286763Smav */ 4082286763Smav target -= bytes; 4083168404Spjd 4084286763Smav total_evicted += 4085286763Smav arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 4086286763Smav } else { 4087286763Smav bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 4088286763Smav total_evicted += bytes; 4089286763Smav 4090286763Smav /* 4091286763Smav * If we couldn't evict our target number of bytes from 4092286763Smav * data, we try to get the rest from data. 4093286763Smav */ 4094286763Smav target -= bytes; 4095286763Smav 4096286763Smav total_evicted += 4097286763Smav arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 4098208373Smm } 4099168404Spjd 4100208373Smm /* 4101208373Smm * Adjust ghost lists 4102286763Smav * 4103286763Smav * In addition to the above, the ARC also defines target values 4104286763Smav * for the ghost lists. The sum of the mru list and mru ghost 4105286763Smav * list should never exceed the target size of the cache, and 4106286763Smav * the sum of the mru list, mfu list, mru ghost list, and mfu 4107286763Smav * ghost list should never exceed twice the target size of the 4108286763Smav * cache. The following logic enforces these limits on the ghost 4109286763Smav * caches, and evicts from them as needed. 4110208373Smm */ 4111286766Smav target = refcount_count(&arc_mru->arcs_size) + 4112286766Smav refcount_count(&arc_mru_ghost->arcs_size) - arc_c; 4113168404Spjd 4114286763Smav bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); 4115286763Smav total_evicted += bytes; 4116168404Spjd 4117286763Smav target -= bytes; 4118185029Spjd 4119286763Smav total_evicted += 4120286763Smav arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); 4121208373Smm 4122286763Smav /* 4123286763Smav * We assume the sum of the mru list and mfu list is less than 4124286763Smav * or equal to arc_c (we enforced this above), which means we 4125286763Smav * can use the simpler of the two equations below: 4126286763Smav * 4127286763Smav * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c 4128286763Smav * mru ghost + mfu ghost <= arc_c 4129286763Smav */ 4130286766Smav target = refcount_count(&arc_mru_ghost->arcs_size) + 4131286766Smav refcount_count(&arc_mfu_ghost->arcs_size) - arc_c; 4132286763Smav 4133286763Smav bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); 4134286763Smav total_evicted += bytes; 4135286763Smav 4136286763Smav target -= bytes; 4137286763Smav 4138286763Smav total_evicted += 4139286763Smav arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); 4140286763Smav 4141286763Smav return (total_evicted); 4142168404Spjd} 4143168404Spjd 4144168404Spjdvoid 4145286763Smavarc_flush(spa_t *spa, boolean_t retry) 4146168404Spjd{ 4147209962Smm uint64_t guid = 0; 4148209962Smm 4149286763Smav /* 4150307265Smav * If retry is B_TRUE, a spa must not be specified since we have 4151286763Smav * no good way to determine if all of a spa's buffers have been 4152286763Smav * evicted from an arc state. 4153286763Smav */ 4154286763Smav ASSERT(!retry || spa == 0); 4155286763Smav 4156286570Smav if (spa != NULL) 4157228103Smm guid = spa_load_guid(spa); 4158209962Smm 4159286763Smav (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); 4160286763Smav (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); 4161168404Spjd 4162286763Smav (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); 4163286763Smav (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); 4164168404Spjd 4165286763Smav (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); 4166286763Smav (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); 4167286763Smav 4168286763Smav (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); 4169286763Smav (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); 4170168404Spjd} 4171168404Spjd 4172168404Spjdvoid 4173286625Smavarc_shrink(int64_t to_free) 4174168404Spjd{ 4175168404Spjd if (arc_c > arc_c_min) { 4176272483Ssmh DTRACE_PROBE4(arc__shrink, uint64_t, arc_c, uint64_t, 4177272483Ssmh arc_c_min, uint64_t, arc_p, uint64_t, to_free); 4178168404Spjd if (arc_c > arc_c_min + to_free) 4179168404Spjd atomic_add_64(&arc_c, -to_free); 4180168404Spjd else 4181168404Spjd arc_c = arc_c_min; 4182168404Spjd 4183168404Spjd atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 4184168404Spjd if (arc_c > arc_size) 4185168404Spjd arc_c = MAX(arc_size, arc_c_min); 4186168404Spjd if (arc_p > arc_c) 4187168404Spjd arc_p = (arc_c >> 1); 4188272483Ssmh 4189272483Ssmh DTRACE_PROBE2(arc__shrunk, uint64_t, arc_c, uint64_t, 4190272483Ssmh arc_p); 4191272483Ssmh 4192168404Spjd ASSERT(arc_c >= arc_c_min); 4193168404Spjd ASSERT((int64_t)arc_p >= 0); 4194168404Spjd } 4195168404Spjd 4196270759Ssmh if (arc_size > arc_c) { 4197270759Ssmh DTRACE_PROBE2(arc__shrink_adjust, uint64_t, arc_size, 4198270759Ssmh uint64_t, arc_c); 4199286763Smav (void) arc_adjust(); 4200270759Ssmh } 4201168404Spjd} 4202168404Spjd 4203286625Smavtypedef enum free_memory_reason_t { 4204286625Smav FMR_UNKNOWN, 4205286625Smav FMR_NEEDFREE, 4206286625Smav FMR_LOTSFREE, 4207286625Smav FMR_SWAPFS_MINFREE, 4208286625Smav FMR_PAGES_PP_MAXIMUM, 4209286625Smav FMR_HEAP_ARENA, 4210286625Smav FMR_ZIO_ARENA, 4211286625Smav FMR_ZIO_FRAG, 4212286625Smav} free_memory_reason_t; 4213286625Smav 4214286625Smavint64_t last_free_memory; 4215286625Smavfree_memory_reason_t last_free_reason; 4216286625Smav 4217286625Smav/* 4218286625Smav * Additional reserve of pages for pp_reserve. 4219286625Smav */ 4220286625Smavint64_t arc_pages_pp_reserve = 64; 4221286625Smav 4222286625Smav/* 4223286625Smav * Additional reserve of pages for swapfs. 4224286625Smav */ 4225286625Smavint64_t arc_swapfs_reserve = 64; 4226286625Smav 4227286625Smav/* 4228286625Smav * Return the amount of memory that can be consumed before reclaim will be 4229286625Smav * needed. Positive if there is sufficient free memory, negative indicates 4230286625Smav * the amount of memory that needs to be freed up. 4231286625Smav */ 4232286625Smavstatic int64_t 4233286625Smavarc_available_memory(void) 4234168404Spjd{ 4235286625Smav int64_t lowest = INT64_MAX; 4236286625Smav int64_t n; 4237286625Smav free_memory_reason_t r = FMR_UNKNOWN; 4238168404Spjd 4239168404Spjd#ifdef _KERNEL 4240330061Savg#ifdef __FreeBSD__ 4241191902Skmacy /* 4242212780Savg * Cooperate with pagedaemon when it's time for it to scan 4243212780Savg * and reclaim some pages. 4244191902Skmacy */ 4245286655Smav n = PAGESIZE * ((int64_t)freemem - zfs_arc_free_target); 4246286625Smav if (n < lowest) { 4247286625Smav lowest = n; 4248286625Smav r = FMR_LOTSFREE; 4249270759Ssmh } 4250191902Skmacy 4251330061Savg#else 4252330061Savg if (needfree > 0) { 4253330061Savg n = PAGESIZE * (-needfree); 4254330061Savg if (n < lowest) { 4255330061Savg lowest = n; 4256330061Savg r = FMR_NEEDFREE; 4257330061Savg } 4258330061Savg } 4259330061Savg 4260168404Spjd /* 4261185029Spjd * check that we're out of range of the pageout scanner. It starts to 4262185029Spjd * schedule paging if freemem is less than lotsfree and needfree. 4263185029Spjd * lotsfree is the high-water mark for pageout, and needfree is the 4264185029Spjd * number of needed free pages. We add extra pages here to make sure 4265185029Spjd * the scanner doesn't start up while we're freeing memory. 4266185029Spjd */ 4267286625Smav n = PAGESIZE * (freemem - lotsfree - needfree - desfree); 4268286625Smav if (n < lowest) { 4269286625Smav lowest = n; 4270286625Smav r = FMR_LOTSFREE; 4271286625Smav } 4272185029Spjd 4273185029Spjd /* 4274168404Spjd * check to make sure that swapfs has enough space so that anon 4275185029Spjd * reservations can still succeed. anon_resvmem() checks that the 4276168404Spjd * availrmem is greater than swapfs_minfree, and the number of reserved 4277168404Spjd * swap pages. We also add a bit of extra here just to prevent 4278168404Spjd * circumstances from getting really dire. 4279168404Spjd */ 4280286625Smav n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve - 4281286625Smav desfree - arc_swapfs_reserve); 4282286625Smav if (n < lowest) { 4283286625Smav lowest = n; 4284286625Smav r = FMR_SWAPFS_MINFREE; 4285286625Smav } 4286168404Spjd 4287286625Smav 4288168404Spjd /* 4289272483Ssmh * Check that we have enough availrmem that memory locking (e.g., via 4290272483Ssmh * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 4291272483Ssmh * stores the number of pages that cannot be locked; when availrmem 4292272483Ssmh * drops below pages_pp_maximum, page locking mechanisms such as 4293272483Ssmh * page_pp_lock() will fail.) 4294272483Ssmh */ 4295286625Smav n = PAGESIZE * (availrmem - pages_pp_maximum - 4296286625Smav arc_pages_pp_reserve); 4297286625Smav if (n < lowest) { 4298286625Smav lowest = n; 4299286625Smav r = FMR_PAGES_PP_MAXIMUM; 4300286625Smav } 4301272483Ssmh 4302330061Savg#endif /* __FreeBSD__ */ 4303272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 4304272483Ssmh /* 4305168404Spjd * If we're on an i386 platform, it's possible that we'll exhaust the 4306168404Spjd * kernel heap space before we ever run out of available physical 4307168404Spjd * memory. Most checks of the size of the heap_area compare against 4308168404Spjd * tune.t_minarmem, which is the minimum available real memory that we 4309168404Spjd * can have in the system. However, this is generally fixed at 25 pages 4310168404Spjd * which is so low that it's useless. In this comparison, we seek to 4311168404Spjd * calculate the total heap-size, and reclaim if more than 3/4ths of the 4312185029Spjd * heap is allocated. (Or, in the calculation, if less than 1/4th is 4313168404Spjd * free) 4314168404Spjd */ 4315286655Smav n = (int64_t)vmem_size(heap_arena, VMEM_FREE) - 4316286628Smav (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2); 4317286625Smav if (n < lowest) { 4318286625Smav lowest = n; 4319286625Smav r = FMR_HEAP_ARENA; 4320270861Ssmh } 4321281026Smav#define zio_arena NULL 4322281026Smav#else 4323281026Smav#define zio_arena heap_arena 4324270861Ssmh#endif 4325281026Smav 4326272483Ssmh /* 4327272483Ssmh * If zio data pages are being allocated out of a separate heap segment, 4328272483Ssmh * then enforce that the size of available vmem for this arena remains 4329272483Ssmh * above about 1/16th free. 4330272483Ssmh * 4331272483Ssmh * Note: The 1/16th arena free requirement was put in place 4332272483Ssmh * to aggressively evict memory from the arc in order to avoid 4333272483Ssmh * memory fragmentation issues. 4334272483Ssmh */ 4335286625Smav if (zio_arena != NULL) { 4336286655Smav n = (int64_t)vmem_size(zio_arena, VMEM_FREE) - 4337286625Smav (vmem_size(zio_arena, VMEM_ALLOC) >> 4); 4338286625Smav if (n < lowest) { 4339286625Smav lowest = n; 4340286625Smav r = FMR_ZIO_ARENA; 4341286625Smav } 4342286625Smav } 4343281026Smav 4344281026Smav /* 4345281026Smav * Above limits know nothing about real level of KVA fragmentation. 4346281026Smav * Start aggressive reclamation if too little sequential KVA left. 4347281026Smav */ 4348286625Smav if (lowest > 0) { 4349317470Ssmh n = (vmem_size(heap_arena, VMEM_MAXFREE) < SPA_MAXBLOCKSIZE) ? 4350286655Smav -((int64_t)vmem_size(heap_arena, VMEM_ALLOC) >> 4) : 4351286655Smav INT64_MAX; 4352286625Smav if (n < lowest) { 4353286625Smav lowest = n; 4354286625Smav r = FMR_ZIO_FRAG; 4355286625Smav } 4356281109Smav } 4357281026Smav 4358272483Ssmh#else /* _KERNEL */ 4359286625Smav /* Every 100 calls, free a small amount */ 4360168404Spjd if (spa_get_random(100) == 0) 4361286625Smav lowest = -1024; 4362272483Ssmh#endif /* _KERNEL */ 4363270759Ssmh 4364286625Smav last_free_memory = lowest; 4365286625Smav last_free_reason = r; 4366286625Smav DTRACE_PROBE2(arc__available_memory, int64_t, lowest, int, r); 4367286625Smav return (lowest); 4368168404Spjd} 4369168404Spjd 4370286625Smav 4371286625Smav/* 4372286625Smav * Determine if the system is under memory pressure and is asking 4373307265Smav * to reclaim memory. A return value of B_TRUE indicates that the system 4374286625Smav * is under memory pressure and that the arc should adjust accordingly. 4375286625Smav */ 4376286625Smavstatic boolean_t 4377286625Smavarc_reclaim_needed(void) 4378286625Smav{ 4379286625Smav return (arc_available_memory() < 0); 4380286625Smav} 4381286625Smav 4382208454Spjdextern kmem_cache_t *zio_buf_cache[]; 4383208454Spjdextern kmem_cache_t *zio_data_buf_cache[]; 4384272527Sdelphijextern kmem_cache_t *range_seg_cache; 4385321610Smavextern kmem_cache_t *abd_chunk_cache; 4386208454Spjd 4387278040Ssmhstatic __noinline void 4388286625Smavarc_kmem_reap_now(void) 4389168404Spjd{ 4390168404Spjd size_t i; 4391168404Spjd kmem_cache_t *prev_cache = NULL; 4392168404Spjd kmem_cache_t *prev_data_cache = NULL; 4393168404Spjd 4394272483Ssmh DTRACE_PROBE(arc__kmem_reap_start); 4395168404Spjd#ifdef _KERNEL 4396185029Spjd if (arc_meta_used >= arc_meta_limit) { 4397185029Spjd /* 4398185029Spjd * We are exceeding our meta-data cache limit. 4399185029Spjd * Purge some DNLC entries to release holds on meta-data. 4400185029Spjd */ 4401185029Spjd dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 4402185029Spjd } 4403168404Spjd#if defined(__i386) 4404168404Spjd /* 4405168404Spjd * Reclaim unused memory from all kmem caches. 4406168404Spjd */ 4407168404Spjd kmem_reap(); 4408168404Spjd#endif 4409168404Spjd#endif 4410168404Spjd 4411168404Spjd for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 4412168404Spjd if (zio_buf_cache[i] != prev_cache) { 4413168404Spjd prev_cache = zio_buf_cache[i]; 4414168404Spjd kmem_cache_reap_now(zio_buf_cache[i]); 4415168404Spjd } 4416168404Spjd if (zio_data_buf_cache[i] != prev_data_cache) { 4417168404Spjd prev_data_cache = zio_data_buf_cache[i]; 4418168404Spjd kmem_cache_reap_now(zio_data_buf_cache[i]); 4419168404Spjd } 4420168404Spjd } 4421321610Smav kmem_cache_reap_now(abd_chunk_cache); 4422168404Spjd kmem_cache_reap_now(buf_cache); 4423286570Smav kmem_cache_reap_now(hdr_full_cache); 4424286570Smav kmem_cache_reap_now(hdr_l2only_cache); 4425272506Sdelphij kmem_cache_reap_now(range_seg_cache); 4426272483Ssmh 4427277300Ssmh#ifdef illumos 4428286625Smav if (zio_arena != NULL) { 4429286625Smav /* 4430286625Smav * Ask the vmem arena to reclaim unused memory from its 4431286625Smav * quantum caches. 4432286625Smav */ 4433272483Ssmh vmem_qcache_reap(zio_arena); 4434286625Smav } 4435272483Ssmh#endif 4436272483Ssmh DTRACE_PROBE(arc__kmem_reap_end); 4437168404Spjd} 4438168404Spjd 4439286763Smav/* 4440321610Smav * Threads can block in arc_get_data_impl() waiting for this thread to evict 4441286763Smav * enough data and signal them to proceed. When this happens, the threads in 4442321610Smav * arc_get_data_impl() are sleeping while holding the hash lock for their 4443286763Smav * particular arc header. Thus, we must be careful to never sleep on a 4444286763Smav * hash lock in this thread. This is to prevent the following deadlock: 4445286763Smav * 4446321610Smav * - Thread A sleeps on CV in arc_get_data_impl() holding hash lock "L", 4447286763Smav * waiting for the reclaim thread to signal it. 4448286763Smav * 4449286763Smav * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter, 4450286763Smav * fails, and goes to sleep forever. 4451286763Smav * 4452286763Smav * This possible deadlock is avoided by always acquiring a hash lock 4453286763Smav * using mutex_tryenter() from arc_reclaim_thread(). 4454286763Smav */ 4455168404Spjdstatic void 4456168404Spjdarc_reclaim_thread(void *dummy __unused) 4457168404Spjd{ 4458296530Smav hrtime_t growtime = 0; 4459168404Spjd callb_cpr_t cpr; 4460168404Spjd 4461286763Smav CALLB_CPR_INIT(&cpr, &arc_reclaim_lock, callb_generic_cpr, FTAG); 4462168404Spjd 4463286763Smav mutex_enter(&arc_reclaim_lock); 4464286763Smav while (!arc_reclaim_thread_exit) { 4465286763Smav uint64_t evicted = 0; 4466286763Smav 4467307265Smav /* 4468307265Smav * This is necessary in order for the mdb ::arc dcmd to 4469307265Smav * show up to date information. Since the ::arc command 4470307265Smav * does not call the kstat's update function, without 4471307265Smav * this call, the command may show stale stats for the 4472307265Smav * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even 4473307265Smav * with this change, the data might be up to 1 second 4474307265Smav * out of date; but that should suffice. The arc_state_t 4475307265Smav * structures can be queried directly if more accurate 4476307265Smav * information is needed. 4477307265Smav */ 4478307265Smav if (arc_ksp != NULL) 4479307265Smav arc_ksp->ks_update(arc_ksp, KSTAT_READ); 4480307265Smav 4481286763Smav mutex_exit(&arc_reclaim_lock); 4482286763Smav 4483314873Sjpaetzel /* 4484314873Sjpaetzel * We call arc_adjust() before (possibly) calling 4485314873Sjpaetzel * arc_kmem_reap_now(), so that we can wake up 4486321610Smav * arc_get_data_impl() sooner. 4487314873Sjpaetzel */ 4488314873Sjpaetzel evicted = arc_adjust(); 4489314873Sjpaetzel 4490314873Sjpaetzel int64_t free_memory = arc_available_memory(); 4491286625Smav if (free_memory < 0) { 4492168404Spjd 4493286625Smav arc_no_grow = B_TRUE; 4494286625Smav arc_warm = B_TRUE; 4495168404Spjd 4496286625Smav /* 4497286625Smav * Wait at least zfs_grow_retry (default 60) seconds 4498286625Smav * before considering growing. 4499286625Smav */ 4500296530Smav growtime = gethrtime() + SEC2NSEC(arc_grow_retry); 4501168404Spjd 4502286625Smav arc_kmem_reap_now(); 4503286625Smav 4504286625Smav /* 4505286625Smav * If we are still low on memory, shrink the ARC 4506286625Smav * so that we have arc_shrink_min free space. 4507286625Smav */ 4508286625Smav free_memory = arc_available_memory(); 4509286625Smav 4510286625Smav int64_t to_free = 4511286625Smav (arc_c >> arc_shrink_shift) - free_memory; 4512286625Smav if (to_free > 0) { 4513330061Savg#ifdef _KERNEL 4514330061Savg#ifdef illumos 4515330061Savg to_free = MAX(to_free, ptob(needfree)); 4516330061Savg#endif 4517330061Savg#endif 4518286625Smav arc_shrink(to_free); 4519168404Spjd } 4520286625Smav } else if (free_memory < arc_c >> arc_no_grow_shift) { 4521286625Smav arc_no_grow = B_TRUE; 4522296530Smav } else if (gethrtime() >= growtime) { 4523286625Smav arc_no_grow = B_FALSE; 4524168404Spjd } 4525168404Spjd 4526286763Smav mutex_enter(&arc_reclaim_lock); 4527168404Spjd 4528286763Smav /* 4529286763Smav * If evicted is zero, we couldn't evict anything via 4530286763Smav * arc_adjust(). This could be due to hash lock 4531286763Smav * collisions, but more likely due to the majority of 4532286763Smav * arc buffers being unevictable. Therefore, even if 4533286763Smav * arc_size is above arc_c, another pass is unlikely to 4534286763Smav * be helpful and could potentially cause us to enter an 4535286763Smav * infinite loop. 4536286763Smav */ 4537286763Smav if (arc_size <= arc_c || evicted == 0) { 4538286763Smav /* 4539286763Smav * We're either no longer overflowing, or we 4540286763Smav * can't evict anything more, so we should wake 4541286763Smav * up any threads before we go to sleep. 4542286763Smav */ 4543286763Smav cv_broadcast(&arc_reclaim_waiters_cv); 4544168404Spjd 4545286763Smav /* 4546286763Smav * Block until signaled, or after one second (we 4547286763Smav * might need to perform arc_kmem_reap_now() 4548286763Smav * even if we aren't being signalled) 4549286763Smav */ 4550286763Smav CALLB_CPR_SAFE_BEGIN(&cpr); 4551296530Smav (void) cv_timedwait_hires(&arc_reclaim_thread_cv, 4552296530Smav &arc_reclaim_lock, SEC2NSEC(1), MSEC2NSEC(1), 0); 4553286763Smav CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_lock); 4554286763Smav } 4555286763Smav } 4556286763Smav 4557307265Smav arc_reclaim_thread_exit = B_FALSE; 4558286763Smav cv_broadcast(&arc_reclaim_thread_cv); 4559286763Smav CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_lock */ 4560286763Smav thread_exit(); 4561286763Smav} 4562286763Smav 4563301997Skibstatic u_int arc_dnlc_evicts_arg; 4564301997Skibextern struct vfsops zfs_vfsops; 4565301997Skib 4566301997Skibstatic void 4567301997Skibarc_dnlc_evicts_thread(void *dummy __unused) 4568301997Skib{ 4569301997Skib callb_cpr_t cpr; 4570301997Skib u_int percent; 4571301997Skib 4572301997Skib CALLB_CPR_INIT(&cpr, &arc_dnlc_evicts_lock, callb_generic_cpr, FTAG); 4573301997Skib 4574301997Skib mutex_enter(&arc_dnlc_evicts_lock); 4575301997Skib while (!arc_dnlc_evicts_thread_exit) { 4576301997Skib CALLB_CPR_SAFE_BEGIN(&cpr); 4577301997Skib (void) cv_wait(&arc_dnlc_evicts_cv, &arc_dnlc_evicts_lock); 4578301997Skib CALLB_CPR_SAFE_END(&cpr, &arc_dnlc_evicts_lock); 4579301997Skib if (arc_dnlc_evicts_arg != 0) { 4580301997Skib percent = arc_dnlc_evicts_arg; 4581301997Skib mutex_exit(&arc_dnlc_evicts_lock); 4582301997Skib#ifdef _KERNEL 4583301997Skib vnlru_free(desiredvnodes * percent / 100, &zfs_vfsops); 4584301997Skib#endif 4585301997Skib mutex_enter(&arc_dnlc_evicts_lock); 4586301997Skib /* 4587301997Skib * Clear our token only after vnlru_free() 4588301997Skib * pass is done, to avoid false queueing of 4589301997Skib * the requests. 4590301997Skib */ 4591301997Skib arc_dnlc_evicts_arg = 0; 4592301997Skib } 4593301997Skib } 4594301997Skib arc_dnlc_evicts_thread_exit = FALSE; 4595301997Skib cv_broadcast(&arc_dnlc_evicts_cv); 4596301997Skib CALLB_CPR_EXIT(&cpr); 4597301997Skib thread_exit(); 4598301997Skib} 4599301997Skib 4600301997Skibvoid 4601301997Skibdnlc_reduce_cache(void *arg) 4602301997Skib{ 4603301997Skib u_int percent; 4604301997Skib 4605302012Skib percent = (u_int)(uintptr_t)arg; 4606301997Skib mutex_enter(&arc_dnlc_evicts_lock); 4607301997Skib if (arc_dnlc_evicts_arg == 0) { 4608301997Skib arc_dnlc_evicts_arg = percent; 4609301997Skib cv_broadcast(&arc_dnlc_evicts_cv); 4610301997Skib } 4611301997Skib mutex_exit(&arc_dnlc_evicts_lock); 4612301997Skib} 4613301997Skib 4614168404Spjd/* 4615168404Spjd * Adapt arc info given the number of bytes we are trying to add and 4616168404Spjd * the state that we are comming from. This function is only called 4617168404Spjd * when we are adding new content to the cache. 4618168404Spjd */ 4619168404Spjdstatic void 4620168404Spjdarc_adapt(int bytes, arc_state_t *state) 4621168404Spjd{ 4622168404Spjd int mult; 4623208373Smm uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 4624286766Smav int64_t mrug_size = refcount_count(&arc_mru_ghost->arcs_size); 4625286766Smav int64_t mfug_size = refcount_count(&arc_mfu_ghost->arcs_size); 4626168404Spjd 4627185029Spjd if (state == arc_l2c_only) 4628185029Spjd return; 4629185029Spjd 4630168404Spjd ASSERT(bytes > 0); 4631168404Spjd /* 4632168404Spjd * Adapt the target size of the MRU list: 4633168404Spjd * - if we just hit in the MRU ghost list, then increase 4634168404Spjd * the target size of the MRU list. 4635168404Spjd * - if we just hit in the MFU ghost list, then increase 4636168404Spjd * the target size of the MFU list by decreasing the 4637168404Spjd * target size of the MRU list. 4638168404Spjd */ 4639168404Spjd if (state == arc_mru_ghost) { 4640286766Smav mult = (mrug_size >= mfug_size) ? 1 : (mfug_size / mrug_size); 4641209275Smm mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 4642168404Spjd 4643208373Smm arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 4644168404Spjd } else if (state == arc_mfu_ghost) { 4645208373Smm uint64_t delta; 4646208373Smm 4647286766Smav mult = (mfug_size >= mrug_size) ? 1 : (mrug_size / mfug_size); 4648209275Smm mult = MIN(mult, 10); 4649168404Spjd 4650208373Smm delta = MIN(bytes * mult, arc_p); 4651208373Smm arc_p = MAX(arc_p_min, arc_p - delta); 4652168404Spjd } 4653168404Spjd ASSERT((int64_t)arc_p >= 0); 4654168404Spjd 4655168404Spjd if (arc_reclaim_needed()) { 4656286763Smav cv_signal(&arc_reclaim_thread_cv); 4657168404Spjd return; 4658168404Spjd } 4659168404Spjd 4660168404Spjd if (arc_no_grow) 4661168404Spjd return; 4662168404Spjd 4663168404Spjd if (arc_c >= arc_c_max) 4664168404Spjd return; 4665168404Spjd 4666168404Spjd /* 4667168404Spjd * If we're within (2 * maxblocksize) bytes of the target 4668168404Spjd * cache size, increment the target cache size 4669168404Spjd */ 4670168404Spjd if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 4671272483Ssmh DTRACE_PROBE1(arc__inc_adapt, int, bytes); 4672168404Spjd atomic_add_64(&arc_c, (int64_t)bytes); 4673168404Spjd if (arc_c > arc_c_max) 4674168404Spjd arc_c = arc_c_max; 4675168404Spjd else if (state == arc_anon) 4676168404Spjd atomic_add_64(&arc_p, (int64_t)bytes); 4677168404Spjd if (arc_p > arc_c) 4678168404Spjd arc_p = arc_c; 4679168404Spjd } 4680168404Spjd ASSERT((int64_t)arc_p >= 0); 4681168404Spjd} 4682168404Spjd 4683168404Spjd/* 4684286763Smav * Check if arc_size has grown past our upper threshold, determined by 4685286763Smav * zfs_arc_overflow_shift. 4686168404Spjd */ 4687286763Smavstatic boolean_t 4688286763Smavarc_is_overflowing(void) 4689168404Spjd{ 4690286763Smav /* Always allow at least one block of overflow */ 4691286763Smav uint64_t overflow = MAX(SPA_MAXBLOCKSIZE, 4692286763Smav arc_c >> zfs_arc_overflow_shift); 4693185029Spjd 4694286763Smav return (arc_size >= arc_c + overflow); 4695168404Spjd} 4696168404Spjd 4697321610Smavstatic abd_t * 4698321610Smavarc_get_data_abd(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4699321610Smav{ 4700321610Smav arc_buf_contents_t type = arc_buf_type(hdr); 4701321610Smav 4702321610Smav arc_get_data_impl(hdr, size, tag); 4703321610Smav if (type == ARC_BUFC_METADATA) { 4704321610Smav return (abd_alloc(size, B_TRUE)); 4705321610Smav } else { 4706321610Smav ASSERT(type == ARC_BUFC_DATA); 4707321610Smav return (abd_alloc(size, B_FALSE)); 4708321610Smav } 4709321610Smav} 4710321610Smav 4711321610Smavstatic void * 4712321610Smavarc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4713321610Smav{ 4714321610Smav arc_buf_contents_t type = arc_buf_type(hdr); 4715321610Smav 4716321610Smav arc_get_data_impl(hdr, size, tag); 4717321610Smav if (type == ARC_BUFC_METADATA) { 4718321610Smav return (zio_buf_alloc(size)); 4719321610Smav } else { 4720321610Smav ASSERT(type == ARC_BUFC_DATA); 4721321610Smav return (zio_data_buf_alloc(size)); 4722321610Smav } 4723321610Smav} 4724321610Smav 4725168404Spjd/* 4726307265Smav * Allocate a block and return it to the caller. If we are hitting the 4727307265Smav * hard limit for the cache size, we must sleep, waiting for the eviction 4728307265Smav * thread to catch up. If we're past the target size but below the hard 4729307265Smav * limit, we'll only signal the reclaim thread and continue on. 4730168404Spjd */ 4731321610Smavstatic void 4732321610Smavarc_get_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4733168404Spjd{ 4734321610Smav arc_state_t *state = hdr->b_l1hdr.b_state; 4735321610Smav arc_buf_contents_t type = arc_buf_type(hdr); 4736168404Spjd 4737168404Spjd arc_adapt(size, state); 4738168404Spjd 4739168404Spjd /* 4740286763Smav * If arc_size is currently overflowing, and has grown past our 4741286763Smav * upper limit, we must be adding data faster than the evict 4742286763Smav * thread can evict. Thus, to ensure we don't compound the 4743286763Smav * problem by adding more data and forcing arc_size to grow even 4744286763Smav * further past it's target size, we halt and wait for the 4745286763Smav * eviction thread to catch up. 4746286763Smav * 4747286763Smav * It's also possible that the reclaim thread is unable to evict 4748286763Smav * enough buffers to get arc_size below the overflow limit (e.g. 4749286763Smav * due to buffers being un-evictable, or hash lock collisions). 4750286763Smav * In this case, we want to proceed regardless if we're 4751286763Smav * overflowing; thus we don't use a while loop here. 4752168404Spjd */ 4753286763Smav if (arc_is_overflowing()) { 4754286763Smav mutex_enter(&arc_reclaim_lock); 4755286763Smav 4756286763Smav /* 4757286763Smav * Now that we've acquired the lock, we may no longer be 4758286763Smav * over the overflow limit, lets check. 4759286763Smav * 4760286763Smav * We're ignoring the case of spurious wake ups. If that 4761286763Smav * were to happen, it'd let this thread consume an ARC 4762286763Smav * buffer before it should have (i.e. before we're under 4763286763Smav * the overflow limit and were signalled by the reclaim 4764286763Smav * thread). As long as that is a rare occurrence, it 4765286763Smav * shouldn't cause any harm. 4766286763Smav */ 4767286763Smav if (arc_is_overflowing()) { 4768286763Smav cv_signal(&arc_reclaim_thread_cv); 4769286763Smav cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); 4770168404Spjd } 4771286763Smav 4772286763Smav mutex_exit(&arc_reclaim_lock); 4773168404Spjd } 4774168404Spjd 4775307265Smav VERIFY3U(hdr->b_type, ==, type); 4776286763Smav if (type == ARC_BUFC_METADATA) { 4777286763Smav arc_space_consume(size, ARC_SPACE_META); 4778168404Spjd } else { 4779286763Smav arc_space_consume(size, ARC_SPACE_DATA); 4780168404Spjd } 4781286763Smav 4782168404Spjd /* 4783168404Spjd * Update the state size. Note that ghost states have a 4784168404Spjd * "ghost size" and so don't need to be updated. 4785168404Spjd */ 4786307265Smav if (!GHOST_STATE(state)) { 4787168404Spjd 4788307265Smav (void) refcount_add_many(&state->arcs_size, size, tag); 4789286763Smav 4790286763Smav /* 4791286763Smav * If this is reached via arc_read, the link is 4792286763Smav * protected by the hash lock. If reached via 4793286763Smav * arc_buf_alloc, the header should not be accessed by 4794286763Smav * any other thread. And, if reached via arc_read_done, 4795286763Smav * the hash lock will protect it if it's found in the 4796286763Smav * hash table; otherwise no other thread should be 4797286763Smav * trying to [add|remove]_reference it. 4798286763Smav */ 4799286763Smav if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 4800286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4801307265Smav (void) refcount_add_many(&state->arcs_esize[type], 4802307265Smav size, tag); 4803168404Spjd } 4804307265Smav 4805168404Spjd /* 4806168404Spjd * If we are growing the cache, and we are adding anonymous 4807168404Spjd * data, and we have outgrown arc_p, update arc_p 4808168404Spjd */ 4809286570Smav if (arc_size < arc_c && hdr->b_l1hdr.b_state == arc_anon && 4810286766Smav (refcount_count(&arc_anon->arcs_size) + 4811286766Smav refcount_count(&arc_mru->arcs_size) > arc_p)) 4812168404Spjd arc_p = MIN(arc_c, arc_p + size); 4813168404Spjd } 4814205231Skmacy ARCSTAT_BUMP(arcstat_allocated); 4815168404Spjd} 4816168404Spjd 4817321610Smavstatic void 4818321610Smavarc_free_data_abd(arc_buf_hdr_t *hdr, abd_t *abd, uint64_t size, void *tag) 4819321610Smav{ 4820321610Smav arc_free_data_impl(hdr, size, tag); 4821321610Smav abd_free(abd); 4822321610Smav} 4823321610Smav 4824321610Smavstatic void 4825321610Smavarc_free_data_buf(arc_buf_hdr_t *hdr, void *buf, uint64_t size, void *tag) 4826321610Smav{ 4827321610Smav arc_buf_contents_t type = arc_buf_type(hdr); 4828321610Smav 4829321610Smav arc_free_data_impl(hdr, size, tag); 4830321610Smav if (type == ARC_BUFC_METADATA) { 4831321610Smav zio_buf_free(buf, size); 4832321610Smav } else { 4833321610Smav ASSERT(type == ARC_BUFC_DATA); 4834321610Smav zio_data_buf_free(buf, size); 4835321610Smav } 4836321610Smav} 4837321610Smav 4838168404Spjd/* 4839307265Smav * Free the arc data buffer. 4840307265Smav */ 4841307265Smavstatic void 4842321610Smavarc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) 4843307265Smav{ 4844307265Smav arc_state_t *state = hdr->b_l1hdr.b_state; 4845307265Smav arc_buf_contents_t type = arc_buf_type(hdr); 4846307265Smav 4847307265Smav /* protected by hash lock, if in the hash table */ 4848307265Smav if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 4849307265Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4850307265Smav ASSERT(state != arc_anon && state != arc_l2c_only); 4851307265Smav 4852307265Smav (void) refcount_remove_many(&state->arcs_esize[type], 4853307265Smav size, tag); 4854307265Smav } 4855307265Smav (void) refcount_remove_many(&state->arcs_size, size, tag); 4856307265Smav 4857307265Smav VERIFY3U(hdr->b_type, ==, type); 4858307265Smav if (type == ARC_BUFC_METADATA) { 4859307265Smav arc_space_return(size, ARC_SPACE_META); 4860307265Smav } else { 4861307265Smav ASSERT(type == ARC_BUFC_DATA); 4862307265Smav arc_space_return(size, ARC_SPACE_DATA); 4863307265Smav } 4864307265Smav} 4865307265Smav 4866307265Smav/* 4867168404Spjd * This routine is called whenever a buffer is accessed. 4868168404Spjd * NOTE: the hash lock is dropped in this function. 4869168404Spjd */ 4870168404Spjdstatic void 4871275811Sdelphijarc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 4872168404Spjd{ 4873219089Spjd clock_t now; 4874219089Spjd 4875168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 4876286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 4877168404Spjd 4878286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 4879168404Spjd /* 4880168404Spjd * This buffer is not in the cache, and does not 4881168404Spjd * appear in our "ghost" list. Add the new buffer 4882168404Spjd * to the MRU state. 4883168404Spjd */ 4884168404Spjd 4885286570Smav ASSERT0(hdr->b_l1hdr.b_arc_access); 4886286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4887275811Sdelphij DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 4888275811Sdelphij arc_change_state(arc_mru, hdr, hash_lock); 4889168404Spjd 4890286570Smav } else if (hdr->b_l1hdr.b_state == arc_mru) { 4891219089Spjd now = ddi_get_lbolt(); 4892219089Spjd 4893168404Spjd /* 4894168404Spjd * If this buffer is here because of a prefetch, then either: 4895168404Spjd * - clear the flag if this is a "referencing" read 4896168404Spjd * (any subsequent access will bump this into the MFU state). 4897168404Spjd * or 4898168404Spjd * - move the buffer to the head of the list if this is 4899168404Spjd * another prefetch (to make it less likely to be evicted). 4900168404Spjd */ 4901286570Smav if (HDR_PREFETCH(hdr)) { 4902286570Smav if (refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 4903286763Smav /* link protected by hash lock */ 4904286763Smav ASSERT(multilist_link_active( 4905286570Smav &hdr->b_l1hdr.b_arc_node)); 4906168404Spjd } else { 4907307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); 4908168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 4909168404Spjd } 4910286570Smav hdr->b_l1hdr.b_arc_access = now; 4911168404Spjd return; 4912168404Spjd } 4913168404Spjd 4914168404Spjd /* 4915168404Spjd * This buffer has been "accessed" only once so far, 4916168404Spjd * but it is still in the cache. Move it to the MFU 4917168404Spjd * state. 4918168404Spjd */ 4919286570Smav if (now > hdr->b_l1hdr.b_arc_access + ARC_MINTIME) { 4920168404Spjd /* 4921168404Spjd * More than 125ms have passed since we 4922168404Spjd * instantiated this buffer. Move it to the 4923168404Spjd * most frequently used state. 4924168404Spjd */ 4925286570Smav hdr->b_l1hdr.b_arc_access = now; 4926275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4927275811Sdelphij arc_change_state(arc_mfu, hdr, hash_lock); 4928168404Spjd } 4929168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 4930286570Smav } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { 4931168404Spjd arc_state_t *new_state; 4932168404Spjd /* 4933168404Spjd * This buffer has been "accessed" recently, but 4934168404Spjd * was evicted from the cache. Move it to the 4935168404Spjd * MFU state. 4936168404Spjd */ 4937168404Spjd 4938286570Smav if (HDR_PREFETCH(hdr)) { 4939168404Spjd new_state = arc_mru; 4940286570Smav if (refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) 4941307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); 4942275811Sdelphij DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 4943168404Spjd } else { 4944168404Spjd new_state = arc_mfu; 4945275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4946168404Spjd } 4947168404Spjd 4948286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4949275811Sdelphij arc_change_state(new_state, hdr, hash_lock); 4950168404Spjd 4951168404Spjd ARCSTAT_BUMP(arcstat_mru_ghost_hits); 4952286570Smav } else if (hdr->b_l1hdr.b_state == arc_mfu) { 4953168404Spjd /* 4954168404Spjd * This buffer has been accessed more than once and is 4955168404Spjd * still in the cache. Keep it in the MFU state. 4956168404Spjd * 4957168404Spjd * NOTE: an add_reference() that occurred when we did 4958168404Spjd * the arc_read() will have kicked this off the list. 4959168404Spjd * If it was a prefetch, we will explicitly move it to 4960168404Spjd * the head of the list now. 4961168404Spjd */ 4962286570Smav if ((HDR_PREFETCH(hdr)) != 0) { 4963286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4964286763Smav /* link protected by hash_lock */ 4965286763Smav ASSERT(multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 4966168404Spjd } 4967168404Spjd ARCSTAT_BUMP(arcstat_mfu_hits); 4968286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4969286570Smav } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { 4970168404Spjd arc_state_t *new_state = arc_mfu; 4971168404Spjd /* 4972168404Spjd * This buffer has been accessed more than once but has 4973168404Spjd * been evicted from the cache. Move it back to the 4974168404Spjd * MFU state. 4975168404Spjd */ 4976168404Spjd 4977286570Smav if (HDR_PREFETCH(hdr)) { 4978168404Spjd /* 4979168404Spjd * This is a prefetch access... 4980168404Spjd * move this block back to the MRU state. 4981168404Spjd */ 4982286570Smav ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 4983168404Spjd new_state = arc_mru; 4984168404Spjd } 4985168404Spjd 4986286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4987275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4988275811Sdelphij arc_change_state(new_state, hdr, hash_lock); 4989168404Spjd 4990168404Spjd ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 4991286570Smav } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { 4992185029Spjd /* 4993185029Spjd * This buffer is on the 2nd Level ARC. 4994185029Spjd */ 4995185029Spjd 4996286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4997275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4998275811Sdelphij arc_change_state(arc_mfu, hdr, hash_lock); 4999168404Spjd } else { 5000168404Spjd ASSERT(!"invalid arc state"); 5001168404Spjd } 5002168404Spjd} 5003168404Spjd 5004168404Spjd/* a generic arc_done_func_t which you can use */ 5005168404Spjd/* ARGSUSED */ 5006168404Spjdvoid 5007168404Spjdarc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 5008168404Spjd{ 5009219089Spjd if (zio == NULL || zio->io_error == 0) 5010321535Smav bcopy(buf->b_data, arg, arc_buf_size(buf)); 5011307265Smav arc_buf_destroy(buf, arg); 5012168404Spjd} 5013168404Spjd 5014185029Spjd/* a generic arc_done_func_t */ 5015168404Spjdvoid 5016168404Spjdarc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 5017168404Spjd{ 5018168404Spjd arc_buf_t **bufp = arg; 5019168404Spjd if (zio && zio->io_error) { 5020307265Smav arc_buf_destroy(buf, arg); 5021168404Spjd *bufp = NULL; 5022168404Spjd } else { 5023168404Spjd *bufp = buf; 5024219089Spjd ASSERT(buf->b_data); 5025168404Spjd } 5026168404Spjd} 5027168404Spjd 5028168404Spjdstatic void 5029307265Smavarc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp) 5030307265Smav{ 5031307265Smav if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { 5032307265Smav ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0); 5033307265Smav ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 5034307265Smav } else { 5035307265Smav if (HDR_COMPRESSION_ENABLED(hdr)) { 5036307265Smav ASSERT3U(HDR_GET_COMPRESS(hdr), ==, 5037307265Smav BP_GET_COMPRESS(bp)); 5038307265Smav } 5039307265Smav ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); 5040307265Smav ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp)); 5041307265Smav } 5042307265Smav} 5043307265Smav 5044307265Smavstatic void 5045168404Spjdarc_read_done(zio_t *zio) 5046168404Spjd{ 5047307265Smav arc_buf_hdr_t *hdr = zio->io_private; 5048268075Sdelphij kmutex_t *hash_lock = NULL; 5049321535Smav arc_callback_t *callback_list; 5050321535Smav arc_callback_t *acb; 5051321535Smav boolean_t freeable = B_FALSE; 5052321535Smav boolean_t no_zio_error = (zio->io_error == 0); 5053168404Spjd 5054168404Spjd /* 5055168404Spjd * The hdr was inserted into hash-table and removed from lists 5056168404Spjd * prior to starting I/O. We should find this header, since 5057168404Spjd * it's in the hash table, and it should be legit since it's 5058168404Spjd * not possible to evict it during the I/O. The only possible 5059168404Spjd * reason for it not to be found is if we were freed during the 5060168404Spjd * read. 5061168404Spjd */ 5062268075Sdelphij if (HDR_IN_HASH_TABLE(hdr)) { 5063268075Sdelphij ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 5064268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[0], ==, 5065268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[0]); 5066268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[1], ==, 5067268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[1]); 5068168404Spjd 5069268075Sdelphij arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 5070268075Sdelphij &hash_lock); 5071168404Spjd 5072307265Smav ASSERT((found == hdr && 5073268075Sdelphij DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 5074268075Sdelphij (found == hdr && HDR_L2_READING(hdr))); 5075307265Smav ASSERT3P(hash_lock, !=, NULL); 5076268075Sdelphij } 5077268075Sdelphij 5078321535Smav if (no_zio_error) { 5079307265Smav /* byteswap if necessary */ 5080307265Smav if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 5081307265Smav if (BP_GET_LEVEL(zio->io_bp) > 0) { 5082307265Smav hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; 5083307265Smav } else { 5084307265Smav hdr->b_l1hdr.b_byteswap = 5085307265Smav DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 5086307265Smav } 5087307265Smav } else { 5088307265Smav hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 5089307265Smav } 5090307265Smav } 5091307265Smav 5092307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED); 5093286570Smav if (l2arc_noprefetch && HDR_PREFETCH(hdr)) 5094307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE); 5095206796Spjd 5096286570Smav callback_list = hdr->b_l1hdr.b_acb; 5097307265Smav ASSERT3P(callback_list, !=, NULL); 5098168404Spjd 5099321535Smav if (hash_lock && no_zio_error && hdr->b_l1hdr.b_state == arc_anon) { 5100219089Spjd /* 5101219089Spjd * Only call arc_access on anonymous buffers. This is because 5102219089Spjd * if we've issued an I/O for an evicted buffer, we've already 5103219089Spjd * called arc_access (to prevent any simultaneous readers from 5104219089Spjd * getting confused). 5105219089Spjd */ 5106219089Spjd arc_access(hdr, hash_lock); 5107219089Spjd } 5108219089Spjd 5109321535Smav /* 5110321535Smav * If a read request has a callback (i.e. acb_done is not NULL), then we 5111321535Smav * make a buf containing the data according to the parameters which were 5112321535Smav * passed in. The implementation of arc_buf_alloc_impl() ensures that we 5113321535Smav * aren't needlessly decompressing the data multiple times. 5114321535Smav */ 5115321535Smav int callback_cnt = 0; 5116321535Smav for (acb = callback_list; acb != NULL; acb = acb->acb_next) { 5117321535Smav if (!acb->acb_done) 5118321535Smav continue; 5119321535Smav 5120321535Smav /* This is a demand read since prefetches don't use callbacks */ 5121321535Smav callback_cnt++; 5122321535Smav 5123321535Smav int error = arc_buf_alloc_impl(hdr, acb->acb_private, 5124321535Smav acb->acb_compressed, no_zio_error, &acb->acb_buf); 5125321535Smav if (no_zio_error) { 5126321535Smav zio->io_error = error; 5127168404Spjd } 5128168404Spjd } 5129286570Smav hdr->b_l1hdr.b_acb = NULL; 5130307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5131321535Smav if (callback_cnt == 0) { 5132307265Smav ASSERT(HDR_PREFETCH(hdr)); 5133307265Smav ASSERT0(hdr->b_l1hdr.b_bufcnt); 5134321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 5135219089Spjd } 5136168404Spjd 5137286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || 5138286570Smav callback_list != NULL); 5139168404Spjd 5140321535Smav if (no_zio_error) { 5141307265Smav arc_hdr_verify(hdr, zio->io_bp); 5142307265Smav } else { 5143307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); 5144286570Smav if (hdr->b_l1hdr.b_state != arc_anon) 5145168404Spjd arc_change_state(arc_anon, hdr, hash_lock); 5146168404Spjd if (HDR_IN_HASH_TABLE(hdr)) 5147168404Spjd buf_hash_remove(hdr); 5148286570Smav freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 5149168404Spjd } 5150168404Spjd 5151168404Spjd /* 5152168404Spjd * Broadcast before we drop the hash_lock to avoid the possibility 5153168404Spjd * that the hdr (and hence the cv) might be freed before we get to 5154168404Spjd * the cv_broadcast(). 5155168404Spjd */ 5156286570Smav cv_broadcast(&hdr->b_l1hdr.b_cv); 5157168404Spjd 5158286570Smav if (hash_lock != NULL) { 5159168404Spjd mutex_exit(hash_lock); 5160168404Spjd } else { 5161168404Spjd /* 5162168404Spjd * This block was freed while we waited for the read to 5163168404Spjd * complete. It has been removed from the hash table and 5164168404Spjd * moved to the anonymous state (so that it won't show up 5165168404Spjd * in the cache). 5166168404Spjd */ 5167286570Smav ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 5168286570Smav freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 5169168404Spjd } 5170168404Spjd 5171168404Spjd /* execute each callback and free its structure */ 5172168404Spjd while ((acb = callback_list) != NULL) { 5173168404Spjd if (acb->acb_done) 5174168404Spjd acb->acb_done(zio, acb->acb_buf, acb->acb_private); 5175168404Spjd 5176168404Spjd if (acb->acb_zio_dummy != NULL) { 5177168404Spjd acb->acb_zio_dummy->io_error = zio->io_error; 5178168404Spjd zio_nowait(acb->acb_zio_dummy); 5179168404Spjd } 5180168404Spjd 5181168404Spjd callback_list = acb->acb_next; 5182168404Spjd kmem_free(acb, sizeof (arc_callback_t)); 5183168404Spjd } 5184168404Spjd 5185168404Spjd if (freeable) 5186168404Spjd arc_hdr_destroy(hdr); 5187168404Spjd} 5188168404Spjd 5189168404Spjd/* 5190286762Smav * "Read" the block at the specified DVA (in bp) via the 5191168404Spjd * cache. If the block is found in the cache, invoke the provided 5192168404Spjd * callback immediately and return. Note that the `zio' parameter 5193168404Spjd * in the callback will be NULL in this case, since no IO was 5194168404Spjd * required. If the block is not in the cache pass the read request 5195168404Spjd * on to the spa with a substitute callback function, so that the 5196168404Spjd * requested block will be added to the cache. 5197168404Spjd * 5198168404Spjd * If a read request arrives for a block that has a read in-progress, 5199168404Spjd * either wait for the in-progress read to complete (and return the 5200168404Spjd * results); or, if this is a read with a "done" func, add a record 5201168404Spjd * to the read to invoke the "done" func when the read completes, 5202168404Spjd * and return; or just return. 5203168404Spjd * 5204168404Spjd * arc_read_done() will invoke all the requested "done" functions 5205168404Spjd * for readers of this block. 5206168404Spjd */ 5207168404Spjdint 5208246666Smmarc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 5209275811Sdelphij void *private, zio_priority_t priority, int zio_flags, 5210275811Sdelphij arc_flags_t *arc_flags, const zbookmark_phys_t *zb) 5211168404Spjd{ 5212268075Sdelphij arc_buf_hdr_t *hdr = NULL; 5213268075Sdelphij kmutex_t *hash_lock = NULL; 5214185029Spjd zio_t *rzio; 5215228103Smm uint64_t guid = spa_load_guid(spa); 5216321535Smav boolean_t compressed_read = (zio_flags & ZIO_FLAG_RAW) != 0; 5217168404Spjd 5218268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp) || 5219268075Sdelphij BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 5220268075Sdelphij 5221168404Spjdtop: 5222268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 5223268075Sdelphij /* 5224268075Sdelphij * Embedded BP's have no DVA and require no I/O to "read". 5225268075Sdelphij * Create an anonymous arc buf to back it. 5226268075Sdelphij */ 5227268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 5228268075Sdelphij } 5229168404Spjd 5230321610Smav if (hdr != NULL && HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_pabd != NULL) { 5231307265Smav arc_buf_t *buf = NULL; 5232275811Sdelphij *arc_flags |= ARC_FLAG_CACHED; 5233168404Spjd 5234168404Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 5235168404Spjd 5236287702Sdelphij if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) && 5237287702Sdelphij priority == ZIO_PRIORITY_SYNC_READ) { 5238287702Sdelphij /* 5239287702Sdelphij * This sync read must wait for an 5240287702Sdelphij * in-progress async read (e.g. a predictive 5241287702Sdelphij * prefetch). Async reads are queued 5242287702Sdelphij * separately at the vdev_queue layer, so 5243287702Sdelphij * this is a form of priority inversion. 5244287702Sdelphij * Ideally, we would "inherit" the demand 5245287702Sdelphij * i/o's priority by moving the i/o from 5246287702Sdelphij * the async queue to the synchronous queue, 5247287702Sdelphij * but there is currently no mechanism to do 5248287702Sdelphij * so. Track this so that we can evaluate 5249287702Sdelphij * the magnitude of this potential performance 5250287702Sdelphij * problem. 5251287702Sdelphij * 5252287702Sdelphij * Note that if the prefetch i/o is already 5253287702Sdelphij * active (has been issued to the device), 5254287702Sdelphij * the prefetch improved performance, because 5255287702Sdelphij * we issued it sooner than we would have 5256287702Sdelphij * without the prefetch. 5257287702Sdelphij */ 5258287702Sdelphij DTRACE_PROBE1(arc__sync__wait__for__async, 5259287702Sdelphij arc_buf_hdr_t *, hdr); 5260287702Sdelphij ARCSTAT_BUMP(arcstat_sync_wait_for_async); 5261287702Sdelphij } 5262287702Sdelphij if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { 5263307265Smav arc_hdr_clear_flags(hdr, 5264307265Smav ARC_FLAG_PREDICTIVE_PREFETCH); 5265287702Sdelphij } 5266287702Sdelphij 5267275811Sdelphij if (*arc_flags & ARC_FLAG_WAIT) { 5268286570Smav cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); 5269168404Spjd mutex_exit(hash_lock); 5270168404Spjd goto top; 5271168404Spjd } 5272275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 5273168404Spjd 5274168404Spjd if (done) { 5275287702Sdelphij arc_callback_t *acb = NULL; 5276168404Spjd 5277168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), 5278168404Spjd KM_SLEEP); 5279168404Spjd acb->acb_done = done; 5280168404Spjd acb->acb_private = private; 5281321535Smav acb->acb_compressed = compressed_read; 5282168404Spjd if (pio != NULL) 5283168404Spjd acb->acb_zio_dummy = zio_null(pio, 5284209962Smm spa, NULL, NULL, NULL, zio_flags); 5285168404Spjd 5286307265Smav ASSERT3P(acb->acb_done, !=, NULL); 5287286570Smav acb->acb_next = hdr->b_l1hdr.b_acb; 5288286570Smav hdr->b_l1hdr.b_acb = acb; 5289168404Spjd mutex_exit(hash_lock); 5290168404Spjd return (0); 5291168404Spjd } 5292168404Spjd mutex_exit(hash_lock); 5293168404Spjd return (0); 5294168404Spjd } 5295168404Spjd 5296286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_mru || 5297286570Smav hdr->b_l1hdr.b_state == arc_mfu); 5298168404Spjd 5299168404Spjd if (done) { 5300287702Sdelphij if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { 5301287702Sdelphij /* 5302287702Sdelphij * This is a demand read which does not have to 5303287702Sdelphij * wait for i/o because we did a predictive 5304287702Sdelphij * prefetch i/o for it, which has completed. 5305287702Sdelphij */ 5306287702Sdelphij DTRACE_PROBE1( 5307287702Sdelphij arc__demand__hit__predictive__prefetch, 5308287702Sdelphij arc_buf_hdr_t *, hdr); 5309287702Sdelphij ARCSTAT_BUMP( 5310287702Sdelphij arcstat_demand_hit_predictive_prefetch); 5311307265Smav arc_hdr_clear_flags(hdr, 5312307265Smav ARC_FLAG_PREDICTIVE_PREFETCH); 5313287702Sdelphij } 5314307265Smav ASSERT(!BP_IS_EMBEDDED(bp) || !BP_IS_HOLE(bp)); 5315307265Smav 5316321535Smav /* Get a buf with the desired data in it. */ 5317321535Smav VERIFY0(arc_buf_alloc_impl(hdr, private, 5318321535Smav compressed_read, B_TRUE, &buf)); 5319275811Sdelphij } else if (*arc_flags & ARC_FLAG_PREFETCH && 5320286570Smav refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 5321307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); 5322168404Spjd } 5323168404Spjd DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 5324168404Spjd arc_access(hdr, hash_lock); 5325275811Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 5326307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 5327168404Spjd mutex_exit(hash_lock); 5328168404Spjd ARCSTAT_BUMP(arcstat_hits); 5329286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 5330286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 5331168404Spjd data, metadata, hits); 5332168404Spjd 5333168404Spjd if (done) 5334168404Spjd done(NULL, buf, private); 5335168404Spjd } else { 5336307265Smav uint64_t lsize = BP_GET_LSIZE(bp); 5337307265Smav uint64_t psize = BP_GET_PSIZE(bp); 5338268075Sdelphij arc_callback_t *acb; 5339185029Spjd vdev_t *vd = NULL; 5340247187Smm uint64_t addr = 0; 5341208373Smm boolean_t devw = B_FALSE; 5342307265Smav uint64_t size; 5343168404Spjd 5344168404Spjd if (hdr == NULL) { 5345168404Spjd /* this block is not in the cache */ 5346268075Sdelphij arc_buf_hdr_t *exists = NULL; 5347168404Spjd arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 5348307265Smav hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, 5349307265Smav BP_GET_COMPRESS(bp), type); 5350307265Smav 5351268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 5352268075Sdelphij hdr->b_dva = *BP_IDENTITY(bp); 5353268075Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 5354268075Sdelphij exists = buf_hash_insert(hdr, &hash_lock); 5355268075Sdelphij } 5356268075Sdelphij if (exists != NULL) { 5357168404Spjd /* somebody beat us to the hash insert */ 5358168404Spjd mutex_exit(hash_lock); 5359219089Spjd buf_discard_identity(hdr); 5360307265Smav arc_hdr_destroy(hdr); 5361168404Spjd goto top; /* restart the IO request */ 5362168404Spjd } 5363168404Spjd } else { 5364286570Smav /* 5365286570Smav * This block is in the ghost cache. If it was L2-only 5366286570Smav * (and thus didn't have an L1 hdr), we realloc the 5367286570Smav * header to add an L1 hdr. 5368286570Smav */ 5369286570Smav if (!HDR_HAS_L1HDR(hdr)) { 5370286570Smav hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, 5371286570Smav hdr_full_cache); 5372286570Smav } 5373321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 5374286570Smav ASSERT(GHOST_STATE(hdr->b_l1hdr.b_state)); 5375168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 5376286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 5377286763Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 5378321535Smav ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 5379168404Spjd 5380287702Sdelphij /* 5381307265Smav * This is a delicate dance that we play here. 5382307265Smav * This hdr is in the ghost list so we access it 5383307265Smav * to move it out of the ghost list before we 5384307265Smav * initiate the read. If it's a prefetch then 5385307265Smav * it won't have a callback so we'll remove the 5386307265Smav * reference that arc_buf_alloc_impl() created. We 5387307265Smav * do this after we've called arc_access() to 5388307265Smav * avoid hitting an assert in remove_reference(). 5389287702Sdelphij */ 5390219089Spjd arc_access(hdr, hash_lock); 5391321610Smav arc_hdr_alloc_pabd(hdr); 5392168404Spjd } 5393321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 5394307265Smav size = arc_hdr_size(hdr); 5395168404Spjd 5396307265Smav /* 5397307265Smav * If compression is enabled on the hdr, then will do 5398307265Smav * RAW I/O and will store the compressed data in the hdr's 5399307265Smav * data block. Otherwise, the hdr's data block will contain 5400307265Smav * the uncompressed data. 5401307265Smav */ 5402307265Smav if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) { 5403307265Smav zio_flags |= ZIO_FLAG_RAW; 5404307265Smav } 5405307265Smav 5406307265Smav if (*arc_flags & ARC_FLAG_PREFETCH) 5407307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); 5408307265Smav if (*arc_flags & ARC_FLAG_L2CACHE) 5409307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 5410307265Smav if (BP_GET_LEVEL(bp) > 0) 5411307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT); 5412287702Sdelphij if (*arc_flags & ARC_FLAG_PREDICTIVE_PREFETCH) 5413307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_PREDICTIVE_PREFETCH); 5414286570Smav ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); 5415219089Spjd 5416168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 5417168404Spjd acb->acb_done = done; 5418168404Spjd acb->acb_private = private; 5419321535Smav acb->acb_compressed = compressed_read; 5420168404Spjd 5421307265Smav ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 5422286570Smav hdr->b_l1hdr.b_acb = acb; 5423307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5424168404Spjd 5425286570Smav if (HDR_HAS_L2HDR(hdr) && 5426286570Smav (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { 5427286570Smav devw = hdr->b_l2hdr.b_dev->l2ad_writing; 5428286570Smav addr = hdr->b_l2hdr.b_daddr; 5429185029Spjd /* 5430185029Spjd * Lock out device removal. 5431185029Spjd */ 5432185029Spjd if (vdev_is_dead(vd) || 5433185029Spjd !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 5434185029Spjd vd = NULL; 5435185029Spjd } 5436185029Spjd 5437307265Smav if (priority == ZIO_PRIORITY_ASYNC_READ) 5438307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); 5439307265Smav else 5440307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); 5441307265Smav 5442268075Sdelphij if (hash_lock != NULL) 5443268075Sdelphij mutex_exit(hash_lock); 5444168404Spjd 5445251629Sdelphij /* 5446251629Sdelphij * At this point, we have a level 1 cache miss. Try again in 5447251629Sdelphij * L2ARC if possible. 5448251629Sdelphij */ 5449307265Smav ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize); 5450307265Smav 5451219089Spjd DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 5452307265Smav uint64_t, lsize, zbookmark_phys_t *, zb); 5453168404Spjd ARCSTAT_BUMP(arcstat_misses); 5454286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 5455286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 5456168404Spjd data, metadata, misses); 5457228392Spjd#ifdef _KERNEL 5458297633Strasz#ifdef RACCT 5459297633Strasz if (racct_enable) { 5460297633Strasz PROC_LOCK(curproc); 5461297633Strasz racct_add_force(curproc, RACCT_READBPS, size); 5462297633Strasz racct_add_force(curproc, RACCT_READIOPS, 1); 5463297633Strasz PROC_UNLOCK(curproc); 5464297633Strasz } 5465297633Strasz#endif /* RACCT */ 5466228392Spjd curthread->td_ru.ru_inblock++; 5467228392Spjd#endif 5468168404Spjd 5469208373Smm if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 5470185029Spjd /* 5471185029Spjd * Read from the L2ARC if the following are true: 5472185029Spjd * 1. The L2ARC vdev was previously cached. 5473185029Spjd * 2. This buffer still has L2ARC metadata. 5474185029Spjd * 3. This buffer isn't currently writing to the L2ARC. 5475185029Spjd * 4. The L2ARC entry wasn't evicted, which may 5476185029Spjd * also have invalidated the vdev. 5477208373Smm * 5. This isn't prefetch and l2arc_noprefetch is set. 5478185029Spjd */ 5479286570Smav if (HDR_HAS_L2HDR(hdr) && 5480208373Smm !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 5481208373Smm !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 5482185029Spjd l2arc_read_callback_t *cb; 5483321610Smav abd_t *abd; 5484321610Smav uint64_t asize; 5485185029Spjd 5486185029Spjd DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 5487185029Spjd ARCSTAT_BUMP(arcstat_l2_hits); 5488185029Spjd 5489185029Spjd cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 5490185029Spjd KM_SLEEP); 5491307265Smav cb->l2rcb_hdr = hdr; 5492185029Spjd cb->l2rcb_bp = *bp; 5493185029Spjd cb->l2rcb_zb = *zb; 5494185029Spjd cb->l2rcb_flags = zio_flags; 5495321610Smav 5496321610Smav asize = vdev_psize_to_asize(vd, size); 5497307265Smav if (asize != size) { 5498321610Smav abd = abd_alloc_for_io(asize, 5499321610Smav HDR_ISTYPE_METADATA(hdr)); 5500321610Smav cb->l2rcb_abd = abd; 5501297848Savg } else { 5502321610Smav abd = hdr->b_l1hdr.b_pabd; 5503297848Savg } 5504185029Spjd 5505247187Smm ASSERT(addr >= VDEV_LABEL_START_SIZE && 5506321610Smav addr + asize <= vd->vdev_psize - 5507247187Smm VDEV_LABEL_END_SIZE); 5508247187Smm 5509185029Spjd /* 5510185029Spjd * l2arc read. The SCL_L2ARC lock will be 5511185029Spjd * released by l2arc_read_done(). 5512251478Sdelphij * Issue a null zio if the underlying buffer 5513251478Sdelphij * was squashed to zero size by compression. 5514185029Spjd */ 5515307265Smav ASSERT3U(HDR_GET_COMPRESS(hdr), !=, 5516307265Smav ZIO_COMPRESS_EMPTY); 5517307265Smav rzio = zio_read_phys(pio, vd, addr, 5518321610Smav asize, abd, 5519307265Smav ZIO_CHECKSUM_OFF, 5520307265Smav l2arc_read_done, cb, priority, 5521307265Smav zio_flags | ZIO_FLAG_DONT_CACHE | 5522307265Smav ZIO_FLAG_CANFAIL | 5523307265Smav ZIO_FLAG_DONT_PROPAGATE | 5524307265Smav ZIO_FLAG_DONT_RETRY, B_FALSE); 5525185029Spjd DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 5526185029Spjd zio_t *, rzio); 5527307265Smav ARCSTAT_INCR(arcstat_l2_read_bytes, size); 5528185029Spjd 5529275811Sdelphij if (*arc_flags & ARC_FLAG_NOWAIT) { 5530185029Spjd zio_nowait(rzio); 5531185029Spjd return (0); 5532185029Spjd } 5533185029Spjd 5534275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_WAIT); 5535185029Spjd if (zio_wait(rzio) == 0) 5536185029Spjd return (0); 5537185029Spjd 5538185029Spjd /* l2arc read error; goto zio_read() */ 5539185029Spjd } else { 5540185029Spjd DTRACE_PROBE1(l2arc__miss, 5541185029Spjd arc_buf_hdr_t *, hdr); 5542185029Spjd ARCSTAT_BUMP(arcstat_l2_misses); 5543185029Spjd if (HDR_L2_WRITING(hdr)) 5544185029Spjd ARCSTAT_BUMP(arcstat_l2_rw_clash); 5545185029Spjd spa_config_exit(spa, SCL_L2ARC, vd); 5546185029Spjd } 5547208373Smm } else { 5548208373Smm if (vd != NULL) 5549208373Smm spa_config_exit(spa, SCL_L2ARC, vd); 5550208373Smm if (l2arc_ndev != 0) { 5551208373Smm DTRACE_PROBE1(l2arc__miss, 5552208373Smm arc_buf_hdr_t *, hdr); 5553208373Smm ARCSTAT_BUMP(arcstat_l2_misses); 5554208373Smm } 5555185029Spjd } 5556185029Spjd 5557321610Smav rzio = zio_read(pio, spa, bp, hdr->b_l1hdr.b_pabd, size, 5558307265Smav arc_read_done, hdr, priority, zio_flags, zb); 5559168404Spjd 5560275811Sdelphij if (*arc_flags & ARC_FLAG_WAIT) 5561168404Spjd return (zio_wait(rzio)); 5562168404Spjd 5563275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 5564168404Spjd zio_nowait(rzio); 5565168404Spjd } 5566168404Spjd return (0); 5567168404Spjd} 5568168404Spjd 5569168404Spjd/* 5570251520Sdelphij * Notify the arc that a block was freed, and thus will never be used again. 5571251520Sdelphij */ 5572251520Sdelphijvoid 5573251520Sdelphijarc_freed(spa_t *spa, const blkptr_t *bp) 5574251520Sdelphij{ 5575251520Sdelphij arc_buf_hdr_t *hdr; 5576251520Sdelphij kmutex_t *hash_lock; 5577251520Sdelphij uint64_t guid = spa_load_guid(spa); 5578251520Sdelphij 5579268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp)); 5580268075Sdelphij 5581268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 5582251520Sdelphij if (hdr == NULL) 5583251520Sdelphij return; 5584307265Smav 5585307265Smav /* 5586307265Smav * We might be trying to free a block that is still doing I/O 5587307265Smav * (i.e. prefetch) or has a reference (i.e. a dedup-ed, 5588307265Smav * dmu_sync-ed block). If this block is being prefetched, then it 5589307265Smav * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr 5590307265Smav * until the I/O completes. A block may also have a reference if it is 5591307265Smav * part of a dedup-ed, dmu_synced write. The dmu_sync() function would 5592307265Smav * have written the new block to its final resting place on disk but 5593307265Smav * without the dedup flag set. This would have left the hdr in the MRU 5594307265Smav * state and discoverable. When the txg finally syncs it detects that 5595307265Smav * the block was overridden in open context and issues an override I/O. 5596307265Smav * Since this is a dedup block, the override I/O will determine if the 5597307265Smav * block is already in the DDT. If so, then it will replace the io_bp 5598307265Smav * with the bp from the DDT and allow the I/O to finish. When the I/O 5599307265Smav * reaches the done callback, dbuf_write_override_done, it will 5600307265Smav * check to see if the io_bp and io_bp_override are identical. 5601307265Smav * If they are not, then it indicates that the bp was replaced with 5602307265Smav * the bp in the DDT and the override bp is freed. This allows 5603307265Smav * us to arrive here with a reference on a block that is being 5604307265Smav * freed. So if we have an I/O in progress, or a reference to 5605307265Smav * this hdr, then we don't destroy the hdr. 5606307265Smav */ 5607307265Smav if (!HDR_HAS_L1HDR(hdr) || (!HDR_IO_IN_PROGRESS(hdr) && 5608307265Smav refcount_is_zero(&hdr->b_l1hdr.b_refcnt))) { 5609307265Smav arc_change_state(arc_anon, hdr, hash_lock); 5610307265Smav arc_hdr_destroy(hdr); 5611251520Sdelphij mutex_exit(hash_lock); 5612251520Sdelphij } else { 5613251520Sdelphij mutex_exit(hash_lock); 5614251520Sdelphij } 5615251520Sdelphij 5616251520Sdelphij} 5617251520Sdelphij 5618251520Sdelphij/* 5619251629Sdelphij * Release this buffer from the cache, making it an anonymous buffer. This 5620251629Sdelphij * must be done after a read and prior to modifying the buffer contents. 5621168404Spjd * If the buffer has more than one reference, we must make 5622185029Spjd * a new hdr for the buffer. 5623168404Spjd */ 5624168404Spjdvoid 5625168404Spjdarc_release(arc_buf_t *buf, void *tag) 5626168404Spjd{ 5627286570Smav arc_buf_hdr_t *hdr = buf->b_hdr; 5628168404Spjd 5629219089Spjd /* 5630219089Spjd * It would be nice to assert that if it's DMU metadata (level > 5631219089Spjd * 0 || it's the dnode file), then it must be syncing context. 5632219089Spjd * But we don't know that information at this level. 5633219089Spjd */ 5634219089Spjd 5635219089Spjd mutex_enter(&buf->b_evict_lock); 5636286776Smav 5637286776Smav ASSERT(HDR_HAS_L1HDR(hdr)); 5638286776Smav 5639286570Smav /* 5640286570Smav * We don't grab the hash lock prior to this check, because if 5641286570Smav * the buffer's header is in the arc_anon state, it won't be 5642286570Smav * linked into the hash table. 5643286570Smav */ 5644286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 5645286570Smav mutex_exit(&buf->b_evict_lock); 5646286570Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 5647286570Smav ASSERT(!HDR_IN_HASH_TABLE(hdr)); 5648286570Smav ASSERT(!HDR_HAS_L2HDR(hdr)); 5649307265Smav ASSERT(HDR_EMPTY(hdr)); 5650307265Smav ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 5651286570Smav ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); 5652286570Smav ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 5653185029Spjd 5654307265Smav hdr->b_l1hdr.b_arc_access = 0; 5655168404Spjd 5656307265Smav /* 5657307265Smav * If the buf is being overridden then it may already 5658307265Smav * have a hdr that is not empty. 5659307265Smav */ 5660307265Smav buf_discard_identity(hdr); 5661286570Smav arc_buf_thaw(buf); 5662286570Smav 5663286570Smav return; 5664168404Spjd } 5665168404Spjd 5666286570Smav kmutex_t *hash_lock = HDR_LOCK(hdr); 5667286570Smav mutex_enter(hash_lock); 5668286570Smav 5669286570Smav /* 5670286570Smav * This assignment is only valid as long as the hash_lock is 5671286570Smav * held, we must be careful not to reference state or the 5672286570Smav * b_state field after dropping the lock. 5673286570Smav */ 5674286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 5675286570Smav ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 5676286570Smav ASSERT3P(state, !=, arc_anon); 5677286570Smav 5678286570Smav /* this buffer is not on any list */ 5679321535Smav ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), >, 0); 5680286570Smav 5681286570Smav if (HDR_HAS_L2HDR(hdr)) { 5682286570Smav mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); 5683286570Smav 5684286570Smav /* 5685286598Smav * We have to recheck this conditional again now that 5686286598Smav * we're holding the l2ad_mtx to prevent a race with 5687286598Smav * another thread which might be concurrently calling 5688286598Smav * l2arc_evict(). In that case, l2arc_evict() might have 5689286598Smav * destroyed the header's L2 portion as we were waiting 5690286598Smav * to acquire the l2ad_mtx. 5691286570Smav */ 5692286598Smav if (HDR_HAS_L2HDR(hdr)) { 5693290191Savg l2arc_trim(hdr); 5694286598Smav arc_hdr_l2hdr_destroy(hdr); 5695286598Smav } 5696286570Smav 5697286570Smav mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); 5698185029Spjd } 5699185029Spjd 5700168404Spjd /* 5701168404Spjd * Do we have more than one buf? 5702168404Spjd */ 5703307265Smav if (hdr->b_l1hdr.b_bufcnt > 1) { 5704168404Spjd arc_buf_hdr_t *nhdr; 5705209962Smm uint64_t spa = hdr->b_spa; 5706307265Smav uint64_t psize = HDR_GET_PSIZE(hdr); 5707307265Smav uint64_t lsize = HDR_GET_LSIZE(hdr); 5708307265Smav enum zio_compress compress = HDR_GET_COMPRESS(hdr); 5709286570Smav arc_buf_contents_t type = arc_buf_type(hdr); 5710307265Smav VERIFY3U(hdr->b_type, ==, type); 5711168404Spjd 5712286570Smav ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); 5713307265Smav (void) remove_reference(hdr, hash_lock, tag); 5714307265Smav 5715321535Smav if (arc_buf_is_shared(buf) && !ARC_BUF_COMPRESSED(buf)) { 5716307265Smav ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); 5717307265Smav ASSERT(ARC_BUF_LAST(buf)); 5718307265Smav } 5719307265Smav 5720168404Spjd /* 5721219089Spjd * Pull the data off of this hdr and attach it to 5722307265Smav * a new anonymous hdr. Also find the last buffer 5723307265Smav * in the hdr's buffer list. 5724168404Spjd */ 5725321535Smav arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); 5726307265Smav ASSERT3P(lastbuf, !=, NULL); 5727168404Spjd 5728307265Smav /* 5729307265Smav * If the current arc_buf_t and the hdr are sharing their data 5730321535Smav * buffer, then we must stop sharing that block. 5731307265Smav */ 5732307265Smav if (arc_buf_is_shared(buf)) { 5733307265Smav VERIFY(!arc_buf_is_shared(lastbuf)); 5734307265Smav 5735307265Smav /* 5736307265Smav * First, sever the block sharing relationship between 5737321535Smav * buf and the arc_buf_hdr_t. 5738307265Smav */ 5739307265Smav arc_unshare_buf(hdr, buf); 5740321535Smav 5741321535Smav /* 5742321610Smav * Now we need to recreate the hdr's b_pabd. Since we 5743321535Smav * have lastbuf handy, we try to share with it, but if 5744321610Smav * we can't then we allocate a new b_pabd and copy the 5745321535Smav * data from buf into it. 5746321535Smav */ 5747321535Smav if (arc_can_share(hdr, lastbuf)) { 5748321535Smav arc_share_buf(hdr, lastbuf); 5749321535Smav } else { 5750321610Smav arc_hdr_alloc_pabd(hdr); 5751321610Smav abd_copy_from_buf(hdr->b_l1hdr.b_pabd, 5752321610Smav buf->b_data, psize); 5753321535Smav } 5754307265Smav VERIFY3P(lastbuf->b_data, !=, NULL); 5755307265Smav } else if (HDR_SHARED_DATA(hdr)) { 5756321535Smav /* 5757321535Smav * Uncompressed shared buffers are always at the end 5758321535Smav * of the list. Compressed buffers don't have the 5759321535Smav * same requirements. This makes it hard to 5760321535Smav * simply assert that the lastbuf is shared so 5761321535Smav * we rely on the hdr's compression flags to determine 5762321535Smav * if we have a compressed, shared buffer. 5763321535Smav */ 5764321535Smav ASSERT(arc_buf_is_shared(lastbuf) || 5765321535Smav HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF); 5766321535Smav ASSERT(!ARC_BUF_SHARED(buf)); 5767307265Smav } 5768321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 5769286570Smav ASSERT3P(state, !=, arc_l2c_only); 5770286766Smav 5771307265Smav (void) refcount_remove_many(&state->arcs_size, 5772321535Smav arc_buf_size(buf), buf); 5773286766Smav 5774286570Smav if (refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { 5775286570Smav ASSERT3P(state, !=, arc_l2c_only); 5776307265Smav (void) refcount_remove_many(&state->arcs_esize[type], 5777321535Smav arc_buf_size(buf), buf); 5778168404Spjd } 5779242845Sdelphij 5780307265Smav hdr->b_l1hdr.b_bufcnt -= 1; 5781168404Spjd arc_cksum_verify(buf); 5782240133Smm#ifdef illumos 5783240133Smm arc_buf_unwatch(buf); 5784277300Ssmh#endif 5785168404Spjd 5786168404Spjd mutex_exit(hash_lock); 5787168404Spjd 5788307265Smav /* 5789321610Smav * Allocate a new hdr. The new hdr will contain a b_pabd 5790307265Smav * buffer which will be freed in arc_write(). 5791307265Smav */ 5792307265Smav nhdr = arc_hdr_alloc(spa, psize, lsize, compress, type); 5793307265Smav ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL); 5794307265Smav ASSERT0(nhdr->b_l1hdr.b_bufcnt); 5795307265Smav ASSERT0(refcount_count(&nhdr->b_l1hdr.b_refcnt)); 5796307265Smav VERIFY3U(nhdr->b_type, ==, type); 5797307265Smav ASSERT(!HDR_SHARED_DATA(nhdr)); 5798286570Smav 5799286570Smav nhdr->b_l1hdr.b_buf = buf; 5800307265Smav nhdr->b_l1hdr.b_bufcnt = 1; 5801286570Smav (void) refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); 5802168404Spjd buf->b_hdr = nhdr; 5803307265Smav 5804219089Spjd mutex_exit(&buf->b_evict_lock); 5805307265Smav (void) refcount_add_many(&arc_anon->arcs_size, 5806321535Smav arc_buf_size(buf), buf); 5807168404Spjd } else { 5808219089Spjd mutex_exit(&buf->b_evict_lock); 5809286570Smav ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); 5810286763Smav /* protected by hash lock, or hdr is on arc_anon */ 5811286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 5812168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 5813286570Smav arc_change_state(arc_anon, hdr, hash_lock); 5814286570Smav hdr->b_l1hdr.b_arc_access = 0; 5815286570Smav mutex_exit(hash_lock); 5816185029Spjd 5817219089Spjd buf_discard_identity(hdr); 5818168404Spjd arc_buf_thaw(buf); 5819168404Spjd } 5820168404Spjd} 5821168404Spjd 5822168404Spjdint 5823168404Spjdarc_released(arc_buf_t *buf) 5824168404Spjd{ 5825185029Spjd int released; 5826185029Spjd 5827219089Spjd mutex_enter(&buf->b_evict_lock); 5828286570Smav released = (buf->b_data != NULL && 5829286570Smav buf->b_hdr->b_l1hdr.b_state == arc_anon); 5830219089Spjd mutex_exit(&buf->b_evict_lock); 5831185029Spjd return (released); 5832168404Spjd} 5833168404Spjd 5834168404Spjd#ifdef ZFS_DEBUG 5835168404Spjdint 5836168404Spjdarc_referenced(arc_buf_t *buf) 5837168404Spjd{ 5838185029Spjd int referenced; 5839185029Spjd 5840219089Spjd mutex_enter(&buf->b_evict_lock); 5841286570Smav referenced = (refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); 5842219089Spjd mutex_exit(&buf->b_evict_lock); 5843185029Spjd return (referenced); 5844168404Spjd} 5845168404Spjd#endif 5846168404Spjd 5847168404Spjdstatic void 5848168404Spjdarc_write_ready(zio_t *zio) 5849168404Spjd{ 5850168404Spjd arc_write_callback_t *callback = zio->io_private; 5851168404Spjd arc_buf_t *buf = callback->awcb_buf; 5852185029Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 5853307265Smav uint64_t psize = BP_IS_HOLE(zio->io_bp) ? 0 : BP_GET_PSIZE(zio->io_bp); 5854168404Spjd 5855286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 5856286570Smav ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); 5857307265Smav ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 5858185029Spjd 5859185029Spjd /* 5860307265Smav * If we're reexecuting this zio because the pool suspended, then 5861307265Smav * cleanup any state that was previously set the first time the 5862321535Smav * callback was invoked. 5863185029Spjd */ 5864307265Smav if (zio->io_flags & ZIO_FLAG_REEXECUTED) { 5865307265Smav arc_cksum_free(hdr); 5866307265Smav#ifdef illumos 5867307265Smav arc_buf_unwatch(buf); 5868307265Smav#endif 5869321610Smav if (hdr->b_l1hdr.b_pabd != NULL) { 5870307265Smav if (arc_buf_is_shared(buf)) { 5871307265Smav arc_unshare_buf(hdr, buf); 5872307265Smav } else { 5873321610Smav arc_hdr_free_pabd(hdr); 5874307265Smav } 5875185029Spjd } 5876168404Spjd } 5877321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 5878307265Smav ASSERT(!HDR_SHARED_DATA(hdr)); 5879307265Smav ASSERT(!arc_buf_is_shared(buf)); 5880307265Smav 5881307265Smav callback->awcb_ready(zio, buf, callback->awcb_private); 5882307265Smav 5883307265Smav if (HDR_IO_IN_PROGRESS(hdr)) 5884307265Smav ASSERT(zio->io_flags & ZIO_FLAG_REEXECUTED); 5885307265Smav 5886307265Smav arc_cksum_compute(buf); 5887307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5888307265Smav 5889307265Smav enum zio_compress compress; 5890307265Smav if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 5891307265Smav compress = ZIO_COMPRESS_OFF; 5892307265Smav } else { 5893307265Smav ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(zio->io_bp)); 5894307265Smav compress = BP_GET_COMPRESS(zio->io_bp); 5895307265Smav } 5896307265Smav HDR_SET_PSIZE(hdr, psize); 5897307265Smav arc_hdr_set_compress(hdr, compress); 5898307265Smav 5899321610Smav 5900307265Smav /* 5901321610Smav * Fill the hdr with data. If the hdr is compressed, the data we want 5902321610Smav * is available from the zio, otherwise we can take it from the buf. 5903321610Smav * 5904321610Smav * We might be able to share the buf's data with the hdr here. However, 5905321610Smav * doing so would cause the ARC to be full of linear ABDs if we write a 5906321610Smav * lot of shareable data. As a compromise, we check whether scattered 5907321610Smav * ABDs are allowed, and assume that if they are then the user wants 5908321610Smav * the ARC to be primarily filled with them regardless of the data being 5909321610Smav * written. Therefore, if they're allowed then we allocate one and copy 5910321610Smav * the data into it; otherwise, we share the data directly if we can. 5911307265Smav */ 5912321610Smav if (zfs_abd_scatter_enabled || !arc_can_share(hdr, buf)) { 5913321610Smav arc_hdr_alloc_pabd(hdr); 5914321610Smav 5915321610Smav /* 5916321610Smav * Ideally, we would always copy the io_abd into b_pabd, but the 5917321610Smav * user may have disabled compressed ARC, thus we must check the 5918321610Smav * hdr's compression setting rather than the io_bp's. 5919321610Smav */ 5920321610Smav if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) { 5921321610Smav ASSERT3U(BP_GET_COMPRESS(zio->io_bp), !=, 5922321610Smav ZIO_COMPRESS_OFF); 5923321610Smav ASSERT3U(psize, >, 0); 5924321610Smav 5925321610Smav abd_copy(hdr->b_l1hdr.b_pabd, zio->io_abd, psize); 5926321610Smav } else { 5927321610Smav ASSERT3U(zio->io_orig_size, ==, arc_hdr_size(hdr)); 5928321610Smav 5929321610Smav abd_copy_from_buf(hdr->b_l1hdr.b_pabd, buf->b_data, 5930321610Smav arc_buf_size(buf)); 5931321610Smav } 5932307265Smav } else { 5933321610Smav ASSERT3P(buf->b_data, ==, abd_to_buf(zio->io_orig_abd)); 5934321535Smav ASSERT3U(zio->io_orig_size, ==, arc_buf_size(buf)); 5935307265Smav ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 5936307265Smav 5937307265Smav arc_share_buf(hdr, buf); 5938307265Smav } 5939321610Smav 5940307265Smav arc_hdr_verify(hdr, zio->io_bp); 5941168404Spjd} 5942168404Spjd 5943304138Savgstatic void 5944304138Savgarc_write_children_ready(zio_t *zio) 5945304138Savg{ 5946304138Savg arc_write_callback_t *callback = zio->io_private; 5947304138Savg arc_buf_t *buf = callback->awcb_buf; 5948304138Savg 5949304138Savg callback->awcb_children_ready(zio, buf, callback->awcb_private); 5950304138Savg} 5951304138Savg 5952258632Savg/* 5953258632Savg * The SPA calls this callback for each physical write that happens on behalf 5954258632Savg * of a logical write. See the comment in dbuf_write_physdone() for details. 5955258632Savg */ 5956168404Spjdstatic void 5957258632Savgarc_write_physdone(zio_t *zio) 5958258632Savg{ 5959258632Savg arc_write_callback_t *cb = zio->io_private; 5960258632Savg if (cb->awcb_physdone != NULL) 5961258632Savg cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 5962258632Savg} 5963258632Savg 5964258632Savgstatic void 5965168404Spjdarc_write_done(zio_t *zio) 5966168404Spjd{ 5967168404Spjd arc_write_callback_t *callback = zio->io_private; 5968168404Spjd arc_buf_t *buf = callback->awcb_buf; 5969168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 5970168404Spjd 5971307265Smav ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 5972168404Spjd 5973219089Spjd if (zio->io_error == 0) { 5974307265Smav arc_hdr_verify(hdr, zio->io_bp); 5975307265Smav 5976268075Sdelphij if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 5977260150Sdelphij buf_discard_identity(hdr); 5978260150Sdelphij } else { 5979260150Sdelphij hdr->b_dva = *BP_IDENTITY(zio->io_bp); 5980260150Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 5981260150Sdelphij } 5982219089Spjd } else { 5983307265Smav ASSERT(HDR_EMPTY(hdr)); 5984219089Spjd } 5985219089Spjd 5986168404Spjd /* 5987268075Sdelphij * If the block to be written was all-zero or compressed enough to be 5988268075Sdelphij * embedded in the BP, no write was performed so there will be no 5989268075Sdelphij * dva/birth/checksum. The buffer must therefore remain anonymous 5990268075Sdelphij * (and uncached). 5991168404Spjd */ 5992307265Smav if (!HDR_EMPTY(hdr)) { 5993168404Spjd arc_buf_hdr_t *exists; 5994168404Spjd kmutex_t *hash_lock; 5995168404Spjd 5996321535Smav ASSERT3U(zio->io_error, ==, 0); 5997219089Spjd 5998168404Spjd arc_cksum_verify(buf); 5999168404Spjd 6000168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 6001286570Smav if (exists != NULL) { 6002168404Spjd /* 6003168404Spjd * This can only happen if we overwrite for 6004168404Spjd * sync-to-convergence, because we remove 6005168404Spjd * buffers from the hash table when we arc_free(). 6006168404Spjd */ 6007219089Spjd if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 6008219089Spjd if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 6009219089Spjd panic("bad overwrite, hdr=%p exists=%p", 6010219089Spjd (void *)hdr, (void *)exists); 6011286570Smav ASSERT(refcount_is_zero( 6012286570Smav &exists->b_l1hdr.b_refcnt)); 6013219089Spjd arc_change_state(arc_anon, exists, hash_lock); 6014219089Spjd mutex_exit(hash_lock); 6015219089Spjd arc_hdr_destroy(exists); 6016219089Spjd exists = buf_hash_insert(hdr, &hash_lock); 6017219089Spjd ASSERT3P(exists, ==, NULL); 6018243524Smm } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 6019243524Smm /* nopwrite */ 6020243524Smm ASSERT(zio->io_prop.zp_nopwrite); 6021243524Smm if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 6022243524Smm panic("bad nopwrite, hdr=%p exists=%p", 6023243524Smm (void *)hdr, (void *)exists); 6024219089Spjd } else { 6025219089Spjd /* Dedup */ 6026307265Smav ASSERT(hdr->b_l1hdr.b_bufcnt == 1); 6027286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_anon); 6028219089Spjd ASSERT(BP_GET_DEDUP(zio->io_bp)); 6029219089Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 6030219089Spjd } 6031168404Spjd } 6032307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 6033185029Spjd /* if it's not anon, we are doing a scrub */ 6034286570Smav if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) 6035185029Spjd arc_access(hdr, hash_lock); 6036168404Spjd mutex_exit(hash_lock); 6037168404Spjd } else { 6038307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 6039168404Spjd } 6040168404Spjd 6041286570Smav ASSERT(!refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 6042219089Spjd callback->awcb_done(zio, buf, callback->awcb_private); 6043168404Spjd 6044321610Smav abd_put(zio->io_abd); 6045168404Spjd kmem_free(callback, sizeof (arc_write_callback_t)); 6046168404Spjd} 6047168404Spjd 6048168404Spjdzio_t * 6049307265Smavarc_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 6050307265Smav boolean_t l2arc, const zio_prop_t *zp, arc_done_func_t *ready, 6051304138Savg arc_done_func_t *children_ready, arc_done_func_t *physdone, 6052258632Savg arc_done_func_t *done, void *private, zio_priority_t priority, 6053268123Sdelphij int zio_flags, const zbookmark_phys_t *zb) 6054168404Spjd{ 6055168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 6056168404Spjd arc_write_callback_t *callback; 6057185029Spjd zio_t *zio; 6058321573Smav zio_prop_t localprop = *zp; 6059168404Spjd 6060307265Smav ASSERT3P(ready, !=, NULL); 6061307265Smav ASSERT3P(done, !=, NULL); 6062168404Spjd ASSERT(!HDR_IO_ERROR(hdr)); 6063286570Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 6064307265Smav ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 6065307265Smav ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); 6066185029Spjd if (l2arc) 6067307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 6068321535Smav if (ARC_BUF_COMPRESSED(buf)) { 6069321573Smav /* 6070321573Smav * We're writing a pre-compressed buffer. Make the 6071321573Smav * compression algorithm requested by the zio_prop_t match 6072321573Smav * the pre-compressed buffer's compression algorithm. 6073321573Smav */ 6074321573Smav localprop.zp_compress = HDR_GET_COMPRESS(hdr); 6075321573Smav 6076321535Smav ASSERT3U(HDR_GET_LSIZE(hdr), !=, arc_buf_size(buf)); 6077321535Smav zio_flags |= ZIO_FLAG_RAW; 6078321535Smav } 6079168404Spjd callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 6080168404Spjd callback->awcb_ready = ready; 6081304138Savg callback->awcb_children_ready = children_ready; 6082258632Savg callback->awcb_physdone = physdone; 6083168404Spjd callback->awcb_done = done; 6084168404Spjd callback->awcb_private = private; 6085168404Spjd callback->awcb_buf = buf; 6086168404Spjd 6087307265Smav /* 6088321610Smav * The hdr's b_pabd is now stale, free it now. A new data block 6089307265Smav * will be allocated when the zio pipeline calls arc_write_ready(). 6090307265Smav */ 6091321610Smav if (hdr->b_l1hdr.b_pabd != NULL) { 6092307265Smav /* 6093307265Smav * If the buf is currently sharing the data block with 6094307265Smav * the hdr then we need to break that relationship here. 6095307265Smav * The hdr will remain with a NULL data pointer and the 6096307265Smav * buf will take sole ownership of the block. 6097307265Smav */ 6098307265Smav if (arc_buf_is_shared(buf)) { 6099307265Smav arc_unshare_buf(hdr, buf); 6100307265Smav } else { 6101321610Smav arc_hdr_free_pabd(hdr); 6102307265Smav } 6103307265Smav VERIFY3P(buf->b_data, !=, NULL); 6104307265Smav arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF); 6105307265Smav } 6106307265Smav ASSERT(!arc_buf_is_shared(buf)); 6107321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 6108307265Smav 6109321610Smav zio = zio_write(pio, spa, txg, bp, 6110321610Smav abd_get_from_buf(buf->b_data, HDR_GET_LSIZE(hdr)), 6111321573Smav HDR_GET_LSIZE(hdr), arc_buf_size(buf), &localprop, arc_write_ready, 6112304138Savg (children_ready != NULL) ? arc_write_children_ready : NULL, 6113304138Savg arc_write_physdone, arc_write_done, callback, 6114258632Savg priority, zio_flags, zb); 6115185029Spjd 6116168404Spjd return (zio); 6117168404Spjd} 6118168404Spjd 6119185029Spjdstatic int 6120258632Savgarc_memory_throttle(uint64_t reserve, uint64_t txg) 6121185029Spjd{ 6122185029Spjd#ifdef _KERNEL 6123272483Ssmh uint64_t available_memory = ptob(freemem); 6124185029Spjd static uint64_t page_load = 0; 6125185029Spjd static uint64_t last_txg = 0; 6126185029Spjd 6127272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 6128185029Spjd available_memory = 6129272483Ssmh MIN(available_memory, ptob(vmem_size(heap_arena, VMEM_FREE))); 6130185029Spjd#endif 6131258632Savg 6132272483Ssmh if (freemem > (uint64_t)physmem * arc_lotsfree_percent / 100) 6133185029Spjd return (0); 6134185029Spjd 6135185029Spjd if (txg > last_txg) { 6136185029Spjd last_txg = txg; 6137185029Spjd page_load = 0; 6138185029Spjd } 6139185029Spjd /* 6140185029Spjd * If we are in pageout, we know that memory is already tight, 6141185029Spjd * the arc is already going to be evicting, so we just want to 6142185029Spjd * continue to let page writes occur as quickly as possible. 6143185029Spjd */ 6144185029Spjd if (curproc == pageproc) { 6145272483Ssmh if (page_load > MAX(ptob(minfree), available_memory) / 4) 6146249195Smm return (SET_ERROR(ERESTART)); 6147185029Spjd /* Note: reserve is inflated, so we deflate */ 6148185029Spjd page_load += reserve / 8; 6149185029Spjd return (0); 6150185029Spjd } else if (page_load > 0 && arc_reclaim_needed()) { 6151185029Spjd /* memory is low, delay before restarting */ 6152185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 6153249195Smm return (SET_ERROR(EAGAIN)); 6154185029Spjd } 6155185029Spjd page_load = 0; 6156185029Spjd#endif 6157185029Spjd return (0); 6158185029Spjd} 6159185029Spjd 6160168404Spjdvoid 6161185029Spjdarc_tempreserve_clear(uint64_t reserve) 6162168404Spjd{ 6163185029Spjd atomic_add_64(&arc_tempreserve, -reserve); 6164168404Spjd ASSERT((int64_t)arc_tempreserve >= 0); 6165168404Spjd} 6166168404Spjd 6167168404Spjdint 6168185029Spjdarc_tempreserve_space(uint64_t reserve, uint64_t txg) 6169168404Spjd{ 6170185029Spjd int error; 6171209962Smm uint64_t anon_size; 6172185029Spjd 6173272483Ssmh if (reserve > arc_c/4 && !arc_no_grow) { 6174185029Spjd arc_c = MIN(arc_c_max, reserve * 4); 6175272483Ssmh DTRACE_PROBE1(arc__set_reserve, uint64_t, arc_c); 6176272483Ssmh } 6177185029Spjd if (reserve > arc_c) 6178249195Smm return (SET_ERROR(ENOMEM)); 6179168404Spjd 6180168404Spjd /* 6181209962Smm * Don't count loaned bufs as in flight dirty data to prevent long 6182209962Smm * network delays from blocking transactions that are ready to be 6183209962Smm * assigned to a txg. 6184209962Smm */ 6185321535Smav 6186321535Smav /* assert that it has not wrapped around */ 6187321535Smav ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); 6188321535Smav 6189286766Smav anon_size = MAX((int64_t)(refcount_count(&arc_anon->arcs_size) - 6190286766Smav arc_loaned_bytes), 0); 6191209962Smm 6192209962Smm /* 6193185029Spjd * Writes will, almost always, require additional memory allocations 6194251631Sdelphij * in order to compress/encrypt/etc the data. We therefore need to 6195185029Spjd * make sure that there is sufficient available memory for this. 6196185029Spjd */ 6197258632Savg error = arc_memory_throttle(reserve, txg); 6198258632Savg if (error != 0) 6199185029Spjd return (error); 6200185029Spjd 6201185029Spjd /* 6202168404Spjd * Throttle writes when the amount of dirty data in the cache 6203168404Spjd * gets too large. We try to keep the cache less than half full 6204168404Spjd * of dirty blocks so that our sync times don't grow too large. 6205168404Spjd * Note: if two requests come in concurrently, we might let them 6206168404Spjd * both succeed, when one of them should fail. Not a huge deal. 6207168404Spjd */ 6208209962Smm 6209209962Smm if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 6210209962Smm anon_size > arc_c / 4) { 6211307265Smav uint64_t meta_esize = 6212307265Smav refcount_count(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 6213307265Smav uint64_t data_esize = 6214307265Smav refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 6215185029Spjd dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 6216185029Spjd "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 6217307265Smav arc_tempreserve >> 10, meta_esize >> 10, 6218307265Smav data_esize >> 10, reserve >> 10, arc_c >> 10); 6219249195Smm return (SET_ERROR(ERESTART)); 6220168404Spjd } 6221185029Spjd atomic_add_64(&arc_tempreserve, reserve); 6222168404Spjd return (0); 6223168404Spjd} 6224168404Spjd 6225286626Smavstatic void 6226286626Smavarc_kstat_update_state(arc_state_t *state, kstat_named_t *size, 6227286626Smav kstat_named_t *evict_data, kstat_named_t *evict_metadata) 6228286626Smav{ 6229286766Smav size->value.ui64 = refcount_count(&state->arcs_size); 6230307265Smav evict_data->value.ui64 = 6231307265Smav refcount_count(&state->arcs_esize[ARC_BUFC_DATA]); 6232307265Smav evict_metadata->value.ui64 = 6233307265Smav refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]); 6234286626Smav} 6235286626Smav 6236286626Smavstatic int 6237286626Smavarc_kstat_update(kstat_t *ksp, int rw) 6238286626Smav{ 6239286626Smav arc_stats_t *as = ksp->ks_data; 6240286626Smav 6241286626Smav if (rw == KSTAT_WRITE) { 6242286626Smav return (EACCES); 6243286626Smav } else { 6244286626Smav arc_kstat_update_state(arc_anon, 6245286626Smav &as->arcstat_anon_size, 6246286626Smav &as->arcstat_anon_evictable_data, 6247286626Smav &as->arcstat_anon_evictable_metadata); 6248286626Smav arc_kstat_update_state(arc_mru, 6249286626Smav &as->arcstat_mru_size, 6250286626Smav &as->arcstat_mru_evictable_data, 6251286626Smav &as->arcstat_mru_evictable_metadata); 6252286626Smav arc_kstat_update_state(arc_mru_ghost, 6253286626Smav &as->arcstat_mru_ghost_size, 6254286626Smav &as->arcstat_mru_ghost_evictable_data, 6255286626Smav &as->arcstat_mru_ghost_evictable_metadata); 6256286626Smav arc_kstat_update_state(arc_mfu, 6257286626Smav &as->arcstat_mfu_size, 6258286626Smav &as->arcstat_mfu_evictable_data, 6259286626Smav &as->arcstat_mfu_evictable_metadata); 6260286626Smav arc_kstat_update_state(arc_mfu_ghost, 6261286626Smav &as->arcstat_mfu_ghost_size, 6262286626Smav &as->arcstat_mfu_ghost_evictable_data, 6263286626Smav &as->arcstat_mfu_ghost_evictable_metadata); 6264286626Smav } 6265286626Smav 6266286626Smav return (0); 6267286626Smav} 6268286626Smav 6269286763Smav/* 6270286763Smav * This function *must* return indices evenly distributed between all 6271286763Smav * sublists of the multilist. This is needed due to how the ARC eviction 6272286763Smav * code is laid out; arc_evict_state() assumes ARC buffers are evenly 6273286763Smav * distributed between all sublists and uses this assumption when 6274286763Smav * deciding which sublist to evict from and how much to evict from it. 6275286763Smav */ 6276286763Smavunsigned int 6277286763Smavarc_state_multilist_index_func(multilist_t *ml, void *obj) 6278286763Smav{ 6279286763Smav arc_buf_hdr_t *hdr = obj; 6280286763Smav 6281286763Smav /* 6282286763Smav * We rely on b_dva to generate evenly distributed index 6283286763Smav * numbers using buf_hash below. So, as an added precaution, 6284286763Smav * let's make sure we never add empty buffers to the arc lists. 6285286763Smav */ 6286307265Smav ASSERT(!HDR_EMPTY(hdr)); 6287286763Smav 6288286763Smav /* 6289286763Smav * The assumption here, is the hash value for a given 6290286763Smav * arc_buf_hdr_t will remain constant throughout it's lifetime 6291286763Smav * (i.e. it's b_spa, b_dva, and b_birth fields don't change). 6292286763Smav * Thus, we don't need to store the header's sublist index 6293286763Smav * on insertion, as this index can be recalculated on removal. 6294286763Smav * 6295286763Smav * Also, the low order bits of the hash value are thought to be 6296286763Smav * distributed evenly. Otherwise, in the case that the multilist 6297286763Smav * has a power of two number of sublists, each sublists' usage 6298286763Smav * would not be evenly distributed. 6299286763Smav */ 6300286763Smav return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % 6301286763Smav multilist_get_num_sublists(ml)); 6302286763Smav} 6303286763Smav 6304168404Spjd#ifdef _KERNEL 6305168566Spjdstatic eventhandler_tag arc_event_lowmem = NULL; 6306168404Spjd 6307168404Spjdstatic void 6308168566Spjdarc_lowmem(void *arg __unused, int howto __unused) 6309168404Spjd{ 6310168404Spjd 6311286763Smav mutex_enter(&arc_reclaim_lock); 6312326619Sbapt DTRACE_PROBE1(arc__needfree, int64_t, ((int64_t)freemem - zfs_arc_free_target) * PAGESIZE); 6313286763Smav cv_signal(&arc_reclaim_thread_cv); 6314241773Savg 6315241773Savg /* 6316241773Savg * It is unsafe to block here in arbitrary threads, because we can come 6317241773Savg * here from ARC itself and may hold ARC locks and thus risk a deadlock 6318241773Savg * with ARC reclaim thread. 6319241773Savg */ 6320286623Smav if (curproc == pageproc) 6321286763Smav (void) cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); 6322286763Smav mutex_exit(&arc_reclaim_lock); 6323168404Spjd} 6324168404Spjd#endif 6325168404Spjd 6326307265Smavstatic void 6327307265Smavarc_state_init(void) 6328307265Smav{ 6329307265Smav arc_anon = &ARC_anon; 6330307265Smav arc_mru = &ARC_mru; 6331307265Smav arc_mru_ghost = &ARC_mru_ghost; 6332307265Smav arc_mfu = &ARC_mfu; 6333307265Smav arc_mfu_ghost = &ARC_mfu_ghost; 6334307265Smav arc_l2c_only = &ARC_l2c_only; 6335307265Smav 6336321553Smav arc_mru->arcs_list[ARC_BUFC_METADATA] = 6337321553Smav multilist_create(sizeof (arc_buf_hdr_t), 6338307265Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 6339321552Smav arc_state_multilist_index_func); 6340321553Smav arc_mru->arcs_list[ARC_BUFC_DATA] = 6341321553Smav multilist_create(sizeof (arc_buf_hdr_t), 6342307265Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 6343321552Smav arc_state_multilist_index_func); 6344321553Smav arc_mru_ghost->arcs_list[ARC_BUFC_METADATA] = 6345321553Smav multilist_create(sizeof (arc_buf_hdr_t), 6346307265Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 6347321552Smav arc_state_multilist_index_func); 6348321553Smav arc_mru_ghost->arcs_list[ARC_BUFC_DATA] = 6349321553Smav multilist_create(sizeof (arc_buf_hdr_t), 6350307265Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 6351321552Smav arc_state_multilist_index_func); 6352321553Smav arc_mfu->arcs_list[ARC_BUFC_METADATA] = 6353321553Smav multilist_create(sizeof (arc_buf_hdr_t), 6354307265Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 6355321552Smav arc_state_multilist_index_func); 6356321553Smav arc_mfu->arcs_list[ARC_BUFC_DATA] = 6357321553Smav multilist_create(sizeof (arc_buf_hdr_t), 6358307265Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 6359321552Smav arc_state_multilist_index_func); 6360321553Smav arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA] = 6361321553Smav multilist_create(sizeof (arc_buf_hdr_t), 6362307265Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 6363321552Smav arc_state_multilist_index_func); 6364321553Smav arc_mfu_ghost->arcs_list[ARC_BUFC_DATA] = 6365321553Smav multilist_create(sizeof (arc_buf_hdr_t), 6366307265Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 6367321552Smav arc_state_multilist_index_func); 6368321553Smav arc_l2c_only->arcs_list[ARC_BUFC_METADATA] = 6369321553Smav multilist_create(sizeof (arc_buf_hdr_t), 6370307265Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 6371321552Smav arc_state_multilist_index_func); 6372321553Smav arc_l2c_only->arcs_list[ARC_BUFC_DATA] = 6373321553Smav multilist_create(sizeof (arc_buf_hdr_t), 6374307265Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 6375321552Smav arc_state_multilist_index_func); 6376307265Smav 6377307265Smav refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 6378307265Smav refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 6379307265Smav refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); 6380307265Smav refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]); 6381307265Smav refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); 6382307265Smav refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); 6383307265Smav refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); 6384307265Smav refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); 6385307265Smav refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); 6386307265Smav refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); 6387307265Smav refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); 6388307265Smav refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); 6389307265Smav 6390307265Smav refcount_create(&arc_anon->arcs_size); 6391307265Smav refcount_create(&arc_mru->arcs_size); 6392307265Smav refcount_create(&arc_mru_ghost->arcs_size); 6393307265Smav refcount_create(&arc_mfu->arcs_size); 6394307265Smav refcount_create(&arc_mfu_ghost->arcs_size); 6395307265Smav refcount_create(&arc_l2c_only->arcs_size); 6396307265Smav} 6397307265Smav 6398307265Smavstatic void 6399307265Smavarc_state_fini(void) 6400307265Smav{ 6401307265Smav refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 6402307265Smav refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 6403307265Smav refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); 6404307265Smav refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]); 6405307265Smav refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); 6406307265Smav refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); 6407307265Smav refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); 6408307265Smav refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); 6409307265Smav refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); 6410307265Smav refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); 6411307265Smav refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); 6412307265Smav refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); 6413307265Smav 6414307265Smav refcount_destroy(&arc_anon->arcs_size); 6415307265Smav refcount_destroy(&arc_mru->arcs_size); 6416307265Smav refcount_destroy(&arc_mru_ghost->arcs_size); 6417307265Smav refcount_destroy(&arc_mfu->arcs_size); 6418307265Smav refcount_destroy(&arc_mfu_ghost->arcs_size); 6419307265Smav refcount_destroy(&arc_l2c_only->arcs_size); 6420307265Smav 6421321553Smav multilist_destroy(arc_mru->arcs_list[ARC_BUFC_METADATA]); 6422321553Smav multilist_destroy(arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 6423321553Smav multilist_destroy(arc_mfu->arcs_list[ARC_BUFC_METADATA]); 6424321553Smav multilist_destroy(arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 6425321553Smav multilist_destroy(arc_mru->arcs_list[ARC_BUFC_DATA]); 6426321553Smav multilist_destroy(arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 6427321553Smav multilist_destroy(arc_mfu->arcs_list[ARC_BUFC_DATA]); 6428321553Smav multilist_destroy(arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 6429307265Smav} 6430307265Smav 6431307265Smavuint64_t 6432307265Smavarc_max_bytes(void) 6433307265Smav{ 6434307265Smav return (arc_c_max); 6435307265Smav} 6436307265Smav 6437168404Spjdvoid 6438168404Spjdarc_init(void) 6439168404Spjd{ 6440219089Spjd int i, prefetch_tunable_set = 0; 6441205231Skmacy 6442321562Smav /* 6443321562Smav * allmem is "all memory that we could possibly use". 6444321562Smav */ 6445321562Smav#ifdef illumos 6446321562Smav#ifdef _KERNEL 6447321562Smav uint64_t allmem = ptob(physmem - swapfs_minfree); 6448321562Smav#else 6449321562Smav uint64_t allmem = (physmem * PAGESIZE) / 2; 6450321562Smav#endif 6451321562Smav#else 6452321562Smav uint64_t allmem = kmem_size(); 6453321562Smav#endif 6454321562Smav 6455321562Smav 6456286763Smav mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL); 6457286763Smav cv_init(&arc_reclaim_thread_cv, NULL, CV_DEFAULT, NULL); 6458286763Smav cv_init(&arc_reclaim_waiters_cv, NULL, CV_DEFAULT, NULL); 6459168404Spjd 6460301997Skib mutex_init(&arc_dnlc_evicts_lock, NULL, MUTEX_DEFAULT, NULL); 6461301997Skib cv_init(&arc_dnlc_evicts_cv, NULL, CV_DEFAULT, NULL); 6462301997Skib 6463168404Spjd /* Convert seconds to clock ticks */ 6464168404Spjd arc_min_prefetch_lifespan = 1 * hz; 6465168404Spjd 6466302265Ssmh /* set min cache to 1/32 of all memory, or arc_abs_min, whichever is more */ 6467321562Smav arc_c_min = MAX(allmem / 32, arc_abs_min); 6468321562Smav /* set max to 5/8 of all memory, or all but 1GB, whichever is more */ 6469321562Smav if (allmem >= 1 << 30) 6470321562Smav arc_c_max = allmem - (1 << 30); 6471168404Spjd else 6472168404Spjd arc_c_max = arc_c_min; 6473321562Smav arc_c_max = MAX(allmem * 5 / 8, arc_c_max); 6474219089Spjd 6475289305Smav /* 6476289305Smav * In userland, there's only the memory pressure that we artificially 6477289305Smav * create (see arc_available_memory()). Don't let arc_c get too 6478289305Smav * small, because it can cause transactions to be larger than 6479289305Smav * arc_c, causing arc_tempreserve_space() to fail. 6480289305Smav */ 6481289305Smav#ifndef _KERNEL 6482289305Smav arc_c_min = arc_c_max / 2; 6483289305Smav#endif 6484289305Smav 6485168481Spjd#ifdef _KERNEL 6486168404Spjd /* 6487168404Spjd * Allow the tunables to override our calculations if they are 6488302265Ssmh * reasonable. 6489168404Spjd */ 6490321562Smav if (zfs_arc_max > arc_abs_min && zfs_arc_max < allmem) { 6491168404Spjd arc_c_max = zfs_arc_max; 6492307297Smav arc_c_min = MIN(arc_c_min, arc_c_max); 6493307297Smav } 6494302265Ssmh if (zfs_arc_min > arc_abs_min && zfs_arc_min <= arc_c_max) 6495168404Spjd arc_c_min = zfs_arc_min; 6496168481Spjd#endif 6497219089Spjd 6498168404Spjd arc_c = arc_c_max; 6499168404Spjd arc_p = (arc_c >> 1); 6500307265Smav arc_size = 0; 6501168404Spjd 6502185029Spjd /* limit meta-data to 1/4 of the arc capacity */ 6503185029Spjd arc_meta_limit = arc_c_max / 4; 6504185029Spjd 6505321563Smav#ifdef _KERNEL 6506321563Smav /* 6507321563Smav * Metadata is stored in the kernel's heap. Don't let us 6508321563Smav * use more than half the heap for the ARC. 6509321563Smav */ 6510321563Smav arc_meta_limit = MIN(arc_meta_limit, 6511321563Smav vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 2); 6512321563Smav#endif 6513321563Smav 6514185029Spjd /* Allow the tunable to override if it is reasonable */ 6515185029Spjd if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 6516185029Spjd arc_meta_limit = zfs_arc_meta_limit; 6517185029Spjd 6518185029Spjd if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 6519185029Spjd arc_c_min = arc_meta_limit / 2; 6520185029Spjd 6521275780Sdelphij if (zfs_arc_meta_min > 0) { 6522275780Sdelphij arc_meta_min = zfs_arc_meta_min; 6523275780Sdelphij } else { 6524275780Sdelphij arc_meta_min = arc_c_min / 2; 6525275780Sdelphij } 6526275780Sdelphij 6527208373Smm if (zfs_arc_grow_retry > 0) 6528208373Smm arc_grow_retry = zfs_arc_grow_retry; 6529208373Smm 6530208373Smm if (zfs_arc_shrink_shift > 0) 6531208373Smm arc_shrink_shift = zfs_arc_shrink_shift; 6532208373Smm 6533323667Sbapt if (zfs_arc_no_grow_shift > 0) 6534323667Sbapt arc_no_grow_shift = zfs_arc_no_grow_shift; 6535286625Smav /* 6536286625Smav * Ensure that arc_no_grow_shift is less than arc_shrink_shift. 6537286625Smav */ 6538286625Smav if (arc_no_grow_shift >= arc_shrink_shift) 6539286625Smav arc_no_grow_shift = arc_shrink_shift - 1; 6540286625Smav 6541208373Smm if (zfs_arc_p_min_shift > 0) 6542208373Smm arc_p_min_shift = zfs_arc_p_min_shift; 6543208373Smm 6544168404Spjd /* if kmem_flags are set, lets try to use less memory */ 6545168404Spjd if (kmem_debugging()) 6546168404Spjd arc_c = arc_c / 2; 6547168404Spjd if (arc_c < arc_c_min) 6548168404Spjd arc_c = arc_c_min; 6549168404Spjd 6550168473Spjd zfs_arc_min = arc_c_min; 6551168473Spjd zfs_arc_max = arc_c_max; 6552168473Spjd 6553307265Smav arc_state_init(); 6554168404Spjd buf_init(); 6555168404Spjd 6556307265Smav arc_reclaim_thread_exit = B_FALSE; 6557301997Skib arc_dnlc_evicts_thread_exit = FALSE; 6558168404Spjd 6559168404Spjd arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 6560168404Spjd sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 6561168404Spjd 6562168404Spjd if (arc_ksp != NULL) { 6563168404Spjd arc_ksp->ks_data = &arc_stats; 6564286574Smav arc_ksp->ks_update = arc_kstat_update; 6565168404Spjd kstat_install(arc_ksp); 6566168404Spjd } 6567168404Spjd 6568168404Spjd (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 6569168404Spjd TS_RUN, minclsyspri); 6570168404Spjd 6571168404Spjd#ifdef _KERNEL 6572168566Spjd arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 6573168404Spjd EVENTHANDLER_PRI_FIRST); 6574168404Spjd#endif 6575168404Spjd 6576301997Skib (void) thread_create(NULL, 0, arc_dnlc_evicts_thread, NULL, 0, &p0, 6577301997Skib TS_RUN, minclsyspri); 6578301997Skib 6579307265Smav arc_dead = B_FALSE; 6580185029Spjd arc_warm = B_FALSE; 6581168566Spjd 6582258632Savg /* 6583258632Savg * Calculate maximum amount of dirty data per pool. 6584258632Savg * 6585258632Savg * If it has been set by /etc/system, take that. 6586258632Savg * Otherwise, use a percentage of physical memory defined by 6587258632Savg * zfs_dirty_data_max_percent (default 10%) with a cap at 6588258632Savg * zfs_dirty_data_max_max (default 4GB). 6589258632Savg */ 6590258632Savg if (zfs_dirty_data_max == 0) { 6591258632Savg zfs_dirty_data_max = ptob(physmem) * 6592258632Savg zfs_dirty_data_max_percent / 100; 6593258632Savg zfs_dirty_data_max = MIN(zfs_dirty_data_max, 6594258632Savg zfs_dirty_data_max_max); 6595258632Savg } 6596185029Spjd 6597168566Spjd#ifdef _KERNEL 6598194043Skmacy if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 6599193953Skmacy prefetch_tunable_set = 1; 6600206796Spjd 6601193878Skmacy#ifdef __i386__ 6602193953Skmacy if (prefetch_tunable_set == 0) { 6603196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 6604196863Strasz "-- to enable,\n"); 6605196863Strasz printf(" add \"vfs.zfs.prefetch_disable=0\" " 6606196863Strasz "to /boot/loader.conf.\n"); 6607219089Spjd zfs_prefetch_disable = 1; 6608193878Skmacy } 6609206796Spjd#else 6610193878Skmacy if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 6611193953Skmacy prefetch_tunable_set == 0) { 6612196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default if less " 6613196941Strasz "than 4GB of RAM is present;\n" 6614196863Strasz " to enable, add \"vfs.zfs.prefetch_disable=0\" " 6615196863Strasz "to /boot/loader.conf.\n"); 6616219089Spjd zfs_prefetch_disable = 1; 6617193878Skmacy } 6618206796Spjd#endif 6619175633Spjd /* Warn about ZFS memory and address space requirements. */ 6620168696Spjd if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 6621168987Sbmah printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 6622168987Sbmah "expect unstable behavior.\n"); 6623175633Spjd } 6624321562Smav if (allmem < 512 * (1 << 20)) { 6625173419Spjd printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 6626168987Sbmah "expect unstable behavior.\n"); 6627185029Spjd printf(" Consider tuning vm.kmem_size and " 6628173419Spjd "vm.kmem_size_max\n"); 6629185029Spjd printf(" in /boot/loader.conf.\n"); 6630168566Spjd } 6631168566Spjd#endif 6632168404Spjd} 6633168404Spjd 6634168404Spjdvoid 6635168404Spjdarc_fini(void) 6636168404Spjd{ 6637327491Smarkj#ifdef _KERNEL 6638327491Smarkj if (arc_event_lowmem != NULL) 6639327491Smarkj EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 6640327491Smarkj#endif 6641327491Smarkj 6642286763Smav mutex_enter(&arc_reclaim_lock); 6643307265Smav arc_reclaim_thread_exit = B_TRUE; 6644286763Smav /* 6645286763Smav * The reclaim thread will set arc_reclaim_thread_exit back to 6646307265Smav * B_FALSE when it is finished exiting; we're waiting for that. 6647286763Smav */ 6648286763Smav while (arc_reclaim_thread_exit) { 6649286763Smav cv_signal(&arc_reclaim_thread_cv); 6650286763Smav cv_wait(&arc_reclaim_thread_cv, &arc_reclaim_lock); 6651286763Smav } 6652286763Smav mutex_exit(&arc_reclaim_lock); 6653168404Spjd 6654307265Smav /* Use B_TRUE to ensure *all* buffers are evicted */ 6655307265Smav arc_flush(NULL, B_TRUE); 6656168404Spjd 6657301997Skib mutex_enter(&arc_dnlc_evicts_lock); 6658301997Skib arc_dnlc_evicts_thread_exit = TRUE; 6659301997Skib /* 6660301997Skib * The user evicts thread will set arc_user_evicts_thread_exit 6661301997Skib * to FALSE when it is finished exiting; we're waiting for that. 6662301997Skib */ 6663301997Skib while (arc_dnlc_evicts_thread_exit) { 6664301997Skib cv_signal(&arc_dnlc_evicts_cv); 6665301997Skib cv_wait(&arc_dnlc_evicts_cv, &arc_dnlc_evicts_lock); 6666301997Skib } 6667301997Skib mutex_exit(&arc_dnlc_evicts_lock); 6668301997Skib 6669307265Smav arc_dead = B_TRUE; 6670286763Smav 6671168404Spjd if (arc_ksp != NULL) { 6672168404Spjd kstat_delete(arc_ksp); 6673168404Spjd arc_ksp = NULL; 6674168404Spjd } 6675168404Spjd 6676286763Smav mutex_destroy(&arc_reclaim_lock); 6677286763Smav cv_destroy(&arc_reclaim_thread_cv); 6678286763Smav cv_destroy(&arc_reclaim_waiters_cv); 6679168404Spjd 6680301997Skib mutex_destroy(&arc_dnlc_evicts_lock); 6681301997Skib cv_destroy(&arc_dnlc_evicts_cv); 6682301997Skib 6683307265Smav arc_state_fini(); 6684168404Spjd buf_fini(); 6685168404Spjd 6686286570Smav ASSERT0(arc_loaned_bytes); 6687168404Spjd} 6688185029Spjd 6689185029Spjd/* 6690185029Spjd * Level 2 ARC 6691185029Spjd * 6692185029Spjd * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 6693185029Spjd * It uses dedicated storage devices to hold cached data, which are populated 6694185029Spjd * using large infrequent writes. The main role of this cache is to boost 6695185029Spjd * the performance of random read workloads. The intended L2ARC devices 6696185029Spjd * include short-stroked disks, solid state disks, and other media with 6697185029Spjd * substantially faster read latency than disk. 6698185029Spjd * 6699185029Spjd * +-----------------------+ 6700185029Spjd * | ARC | 6701185029Spjd * +-----------------------+ 6702185029Spjd * | ^ ^ 6703185029Spjd * | | | 6704185029Spjd * l2arc_feed_thread() arc_read() 6705185029Spjd * | | | 6706185029Spjd * | l2arc read | 6707185029Spjd * V | | 6708185029Spjd * +---------------+ | 6709185029Spjd * | L2ARC | | 6710185029Spjd * +---------------+ | 6711185029Spjd * | ^ | 6712185029Spjd * l2arc_write() | | 6713185029Spjd * | | | 6714185029Spjd * V | | 6715185029Spjd * +-------+ +-------+ 6716185029Spjd * | vdev | | vdev | 6717185029Spjd * | cache | | cache | 6718185029Spjd * +-------+ +-------+ 6719185029Spjd * +=========+ .-----. 6720185029Spjd * : L2ARC : |-_____-| 6721185029Spjd * : devices : | Disks | 6722185029Spjd * +=========+ `-_____-' 6723185029Spjd * 6724185029Spjd * Read requests are satisfied from the following sources, in order: 6725185029Spjd * 6726185029Spjd * 1) ARC 6727185029Spjd * 2) vdev cache of L2ARC devices 6728185029Spjd * 3) L2ARC devices 6729185029Spjd * 4) vdev cache of disks 6730185029Spjd * 5) disks 6731185029Spjd * 6732185029Spjd * Some L2ARC device types exhibit extremely slow write performance. 6733185029Spjd * To accommodate for this there are some significant differences between 6734185029Spjd * the L2ARC and traditional cache design: 6735185029Spjd * 6736185029Spjd * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 6737185029Spjd * the ARC behave as usual, freeing buffers and placing headers on ghost 6738185029Spjd * lists. The ARC does not send buffers to the L2ARC during eviction as 6739185029Spjd * this would add inflated write latencies for all ARC memory pressure. 6740185029Spjd * 6741185029Spjd * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 6742185029Spjd * It does this by periodically scanning buffers from the eviction-end of 6743185029Spjd * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 6744251478Sdelphij * not already there. It scans until a headroom of buffers is satisfied, 6745251478Sdelphij * which itself is a buffer for ARC eviction. If a compressible buffer is 6746251478Sdelphij * found during scanning and selected for writing to an L2ARC device, we 6747251478Sdelphij * temporarily boost scanning headroom during the next scan cycle to make 6748251478Sdelphij * sure we adapt to compression effects (which might significantly reduce 6749251478Sdelphij * the data volume we write to L2ARC). The thread that does this is 6750185029Spjd * l2arc_feed_thread(), illustrated below; example sizes are included to 6751185029Spjd * provide a better sense of ratio than this diagram: 6752185029Spjd * 6753185029Spjd * head --> tail 6754185029Spjd * +---------------------+----------+ 6755185029Spjd * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 6756185029Spjd * +---------------------+----------+ | o L2ARC eligible 6757185029Spjd * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 6758185029Spjd * +---------------------+----------+ | 6759185029Spjd * 15.9 Gbytes ^ 32 Mbytes | 6760185029Spjd * headroom | 6761185029Spjd * l2arc_feed_thread() 6762185029Spjd * | 6763185029Spjd * l2arc write hand <--[oooo]--' 6764185029Spjd * | 8 Mbyte 6765185029Spjd * | write max 6766185029Spjd * V 6767185029Spjd * +==============================+ 6768185029Spjd * L2ARC dev |####|#|###|###| |####| ... | 6769185029Spjd * +==============================+ 6770185029Spjd * 32 Gbytes 6771185029Spjd * 6772185029Spjd * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 6773185029Spjd * evicted, then the L2ARC has cached a buffer much sooner than it probably 6774185029Spjd * needed to, potentially wasting L2ARC device bandwidth and storage. It is 6775185029Spjd * safe to say that this is an uncommon case, since buffers at the end of 6776185029Spjd * the ARC lists have moved there due to inactivity. 6777185029Spjd * 6778185029Spjd * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 6779185029Spjd * then the L2ARC simply misses copying some buffers. This serves as a 6780185029Spjd * pressure valve to prevent heavy read workloads from both stalling the ARC 6781185029Spjd * with waits and clogging the L2ARC with writes. This also helps prevent 6782185029Spjd * the potential for the L2ARC to churn if it attempts to cache content too 6783185029Spjd * quickly, such as during backups of the entire pool. 6784185029Spjd * 6785185029Spjd * 5. After system boot and before the ARC has filled main memory, there are 6786185029Spjd * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 6787185029Spjd * lists can remain mostly static. Instead of searching from tail of these 6788185029Spjd * lists as pictured, the l2arc_feed_thread() will search from the list heads 6789185029Spjd * for eligible buffers, greatly increasing its chance of finding them. 6790185029Spjd * 6791185029Spjd * The L2ARC device write speed is also boosted during this time so that 6792185029Spjd * the L2ARC warms up faster. Since there have been no ARC evictions yet, 6793185029Spjd * there are no L2ARC reads, and no fear of degrading read performance 6794185029Spjd * through increased writes. 6795185029Spjd * 6796185029Spjd * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 6797185029Spjd * the vdev queue can aggregate them into larger and fewer writes. Each 6798185029Spjd * device is written to in a rotor fashion, sweeping writes through 6799185029Spjd * available space then repeating. 6800185029Spjd * 6801185029Spjd * 7. The L2ARC does not store dirty content. It never needs to flush 6802185029Spjd * write buffers back to disk based storage. 6803185029Spjd * 6804185029Spjd * 8. If an ARC buffer is written (and dirtied) which also exists in the 6805185029Spjd * L2ARC, the now stale L2ARC buffer is immediately dropped. 6806185029Spjd * 6807185029Spjd * The performance of the L2ARC can be tweaked by a number of tunables, which 6808185029Spjd * may be necessary for different workloads: 6809185029Spjd * 6810185029Spjd * l2arc_write_max max write bytes per interval 6811185029Spjd * l2arc_write_boost extra write bytes during device warmup 6812185029Spjd * l2arc_noprefetch skip caching prefetched buffers 6813185029Spjd * l2arc_headroom number of max device writes to precache 6814251478Sdelphij * l2arc_headroom_boost when we find compressed buffers during ARC 6815251478Sdelphij * scanning, we multiply headroom by this 6816251478Sdelphij * percentage factor for the next scan cycle, 6817251478Sdelphij * since more compressed buffers are likely to 6818251478Sdelphij * be present 6819185029Spjd * l2arc_feed_secs seconds between L2ARC writing 6820185029Spjd * 6821185029Spjd * Tunables may be removed or added as future performance improvements are 6822185029Spjd * integrated, and also may become zpool properties. 6823208373Smm * 6824208373Smm * There are three key functions that control how the L2ARC warms up: 6825208373Smm * 6826208373Smm * l2arc_write_eligible() check if a buffer is eligible to cache 6827208373Smm * l2arc_write_size() calculate how much to write 6828208373Smm * l2arc_write_interval() calculate sleep delay between writes 6829208373Smm * 6830208373Smm * These three functions determine what to write, how much, and how quickly 6831208373Smm * to send writes. 6832185029Spjd */ 6833185029Spjd 6834208373Smmstatic boolean_t 6835275811Sdelphijl2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) 6836208373Smm{ 6837208373Smm /* 6838208373Smm * A buffer is *not* eligible for the L2ARC if it: 6839208373Smm * 1. belongs to a different spa. 6840208373Smm * 2. is already cached on the L2ARC. 6841208373Smm * 3. has an I/O in progress (it may be an incomplete read). 6842208373Smm * 4. is flagged not eligible (zfs property). 6843208373Smm */ 6844275811Sdelphij if (hdr->b_spa != spa_guid) { 6845208373Smm ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 6846208373Smm return (B_FALSE); 6847208373Smm } 6848286570Smav if (HDR_HAS_L2HDR(hdr)) { 6849208373Smm ARCSTAT_BUMP(arcstat_l2_write_in_l2); 6850208373Smm return (B_FALSE); 6851208373Smm } 6852275811Sdelphij if (HDR_IO_IN_PROGRESS(hdr)) { 6853208373Smm ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 6854208373Smm return (B_FALSE); 6855208373Smm } 6856275811Sdelphij if (!HDR_L2CACHE(hdr)) { 6857208373Smm ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 6858208373Smm return (B_FALSE); 6859208373Smm } 6860208373Smm 6861208373Smm return (B_TRUE); 6862208373Smm} 6863208373Smm 6864208373Smmstatic uint64_t 6865251478Sdelphijl2arc_write_size(void) 6866208373Smm{ 6867208373Smm uint64_t size; 6868208373Smm 6869251478Sdelphij /* 6870251478Sdelphij * Make sure our globals have meaningful values in case the user 6871251478Sdelphij * altered them. 6872251478Sdelphij */ 6873251478Sdelphij size = l2arc_write_max; 6874251478Sdelphij if (size == 0) { 6875251478Sdelphij cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 6876251478Sdelphij "be greater than zero, resetting it to the default (%d)", 6877251478Sdelphij L2ARC_WRITE_SIZE); 6878251478Sdelphij size = l2arc_write_max = L2ARC_WRITE_SIZE; 6879251478Sdelphij } 6880208373Smm 6881208373Smm if (arc_warm == B_FALSE) 6882251478Sdelphij size += l2arc_write_boost; 6883208373Smm 6884208373Smm return (size); 6885208373Smm 6886208373Smm} 6887208373Smm 6888208373Smmstatic clock_t 6889208373Smml2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 6890208373Smm{ 6891219089Spjd clock_t interval, next, now; 6892208373Smm 6893208373Smm /* 6894208373Smm * If the ARC lists are busy, increase our write rate; if the 6895208373Smm * lists are stale, idle back. This is achieved by checking 6896208373Smm * how much we previously wrote - if it was more than half of 6897208373Smm * what we wanted, schedule the next write much sooner. 6898208373Smm */ 6899208373Smm if (l2arc_feed_again && wrote > (wanted / 2)) 6900208373Smm interval = (hz * l2arc_feed_min_ms) / 1000; 6901208373Smm else 6902208373Smm interval = hz * l2arc_feed_secs; 6903208373Smm 6904219089Spjd now = ddi_get_lbolt(); 6905219089Spjd next = MAX(now, MIN(now + interval, began + interval)); 6906208373Smm 6907208373Smm return (next); 6908208373Smm} 6909208373Smm 6910185029Spjd/* 6911185029Spjd * Cycle through L2ARC devices. This is how L2ARC load balances. 6912185029Spjd * If a device is returned, this also returns holding the spa config lock. 6913185029Spjd */ 6914185029Spjdstatic l2arc_dev_t * 6915185029Spjdl2arc_dev_get_next(void) 6916185029Spjd{ 6917185029Spjd l2arc_dev_t *first, *next = NULL; 6918185029Spjd 6919185029Spjd /* 6920185029Spjd * Lock out the removal of spas (spa_namespace_lock), then removal 6921185029Spjd * of cache devices (l2arc_dev_mtx). Once a device has been selected, 6922185029Spjd * both locks will be dropped and a spa config lock held instead. 6923185029Spjd */ 6924185029Spjd mutex_enter(&spa_namespace_lock); 6925185029Spjd mutex_enter(&l2arc_dev_mtx); 6926185029Spjd 6927185029Spjd /* if there are no vdevs, there is nothing to do */ 6928185029Spjd if (l2arc_ndev == 0) 6929185029Spjd goto out; 6930185029Spjd 6931185029Spjd first = NULL; 6932185029Spjd next = l2arc_dev_last; 6933185029Spjd do { 6934185029Spjd /* loop around the list looking for a non-faulted vdev */ 6935185029Spjd if (next == NULL) { 6936185029Spjd next = list_head(l2arc_dev_list); 6937185029Spjd } else { 6938185029Spjd next = list_next(l2arc_dev_list, next); 6939185029Spjd if (next == NULL) 6940185029Spjd next = list_head(l2arc_dev_list); 6941185029Spjd } 6942185029Spjd 6943185029Spjd /* if we have come back to the start, bail out */ 6944185029Spjd if (first == NULL) 6945185029Spjd first = next; 6946185029Spjd else if (next == first) 6947185029Spjd break; 6948185029Spjd 6949185029Spjd } while (vdev_is_dead(next->l2ad_vdev)); 6950185029Spjd 6951185029Spjd /* if we were unable to find any usable vdevs, return NULL */ 6952185029Spjd if (vdev_is_dead(next->l2ad_vdev)) 6953185029Spjd next = NULL; 6954185029Spjd 6955185029Spjd l2arc_dev_last = next; 6956185029Spjd 6957185029Spjdout: 6958185029Spjd mutex_exit(&l2arc_dev_mtx); 6959185029Spjd 6960185029Spjd /* 6961185029Spjd * Grab the config lock to prevent the 'next' device from being 6962185029Spjd * removed while we are writing to it. 6963185029Spjd */ 6964185029Spjd if (next != NULL) 6965185029Spjd spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 6966185029Spjd mutex_exit(&spa_namespace_lock); 6967185029Spjd 6968185029Spjd return (next); 6969185029Spjd} 6970185029Spjd 6971185029Spjd/* 6972185029Spjd * Free buffers that were tagged for destruction. 6973185029Spjd */ 6974185029Spjdstatic void 6975185029Spjdl2arc_do_free_on_write() 6976185029Spjd{ 6977185029Spjd list_t *buflist; 6978185029Spjd l2arc_data_free_t *df, *df_prev; 6979185029Spjd 6980185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 6981185029Spjd buflist = l2arc_free_on_write; 6982185029Spjd 6983185029Spjd for (df = list_tail(buflist); df; df = df_prev) { 6984185029Spjd df_prev = list_prev(buflist, df); 6985321610Smav ASSERT3P(df->l2df_abd, !=, NULL); 6986321610Smav abd_free(df->l2df_abd); 6987185029Spjd list_remove(buflist, df); 6988185029Spjd kmem_free(df, sizeof (l2arc_data_free_t)); 6989185029Spjd } 6990185029Spjd 6991185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 6992185029Spjd} 6993185029Spjd 6994185029Spjd/* 6995185029Spjd * A write to a cache device has completed. Update all headers to allow 6996185029Spjd * reads from these buffers to begin. 6997185029Spjd */ 6998185029Spjdstatic void 6999185029Spjdl2arc_write_done(zio_t *zio) 7000185029Spjd{ 7001185029Spjd l2arc_write_callback_t *cb; 7002185029Spjd l2arc_dev_t *dev; 7003185029Spjd list_t *buflist; 7004275811Sdelphij arc_buf_hdr_t *head, *hdr, *hdr_prev; 7005185029Spjd kmutex_t *hash_lock; 7006268085Sdelphij int64_t bytes_dropped = 0; 7007185029Spjd 7008185029Spjd cb = zio->io_private; 7009307265Smav ASSERT3P(cb, !=, NULL); 7010185029Spjd dev = cb->l2wcb_dev; 7011307265Smav ASSERT3P(dev, !=, NULL); 7012185029Spjd head = cb->l2wcb_head; 7013307265Smav ASSERT3P(head, !=, NULL); 7014286570Smav buflist = &dev->l2ad_buflist; 7015307265Smav ASSERT3P(buflist, !=, NULL); 7016185029Spjd DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 7017185029Spjd l2arc_write_callback_t *, cb); 7018185029Spjd 7019185029Spjd if (zio->io_error != 0) 7020185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_error); 7021185029Spjd 7022185029Spjd /* 7023185029Spjd * All writes completed, or an error was hit. 7024185029Spjd */ 7025286763Smavtop: 7026286763Smav mutex_enter(&dev->l2ad_mtx); 7027275811Sdelphij for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { 7028275811Sdelphij hdr_prev = list_prev(buflist, hdr); 7029185029Spjd 7030275811Sdelphij hash_lock = HDR_LOCK(hdr); 7031286763Smav 7032286763Smav /* 7033286763Smav * We cannot use mutex_enter or else we can deadlock 7034286763Smav * with l2arc_write_buffers (due to swapping the order 7035286763Smav * the hash lock and l2ad_mtx are taken). 7036286763Smav */ 7037185029Spjd if (!mutex_tryenter(hash_lock)) { 7038185029Spjd /* 7039286763Smav * Missed the hash lock. We must retry so we 7040286763Smav * don't leave the ARC_FLAG_L2_WRITING bit set. 7041185029Spjd */ 7042286763Smav ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); 7043286763Smav 7044286763Smav /* 7045286763Smav * We don't want to rescan the headers we've 7046286763Smav * already marked as having been written out, so 7047286763Smav * we reinsert the head node so we can pick up 7048286763Smav * where we left off. 7049286763Smav */ 7050286763Smav list_remove(buflist, head); 7051286763Smav list_insert_after(buflist, hdr, head); 7052286763Smav 7053286763Smav mutex_exit(&dev->l2ad_mtx); 7054286763Smav 7055286763Smav /* 7056286763Smav * We wait for the hash lock to become available 7057286763Smav * to try and prevent busy waiting, and increase 7058286763Smav * the chance we'll be able to acquire the lock 7059286763Smav * the next time around. 7060286763Smav */ 7061286763Smav mutex_enter(hash_lock); 7062286763Smav mutex_exit(hash_lock); 7063286763Smav goto top; 7064185029Spjd } 7065185029Spjd 7066286570Smav /* 7067286763Smav * We could not have been moved into the arc_l2c_only 7068286763Smav * state while in-flight due to our ARC_FLAG_L2_WRITING 7069286763Smav * bit being set. Let's just ensure that's being enforced. 7070286570Smav */ 7071286763Smav ASSERT(HDR_HAS_L1HDR(hdr)); 7072286570Smav 7073185029Spjd if (zio->io_error != 0) { 7074185029Spjd /* 7075185029Spjd * Error - drop L2ARC entry. 7076185029Spjd */ 7077286776Smav list_remove(buflist, hdr); 7078290191Savg l2arc_trim(hdr); 7079307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); 7080286570Smav 7081323754Savg ARCSTAT_INCR(arcstat_l2_psize, -arc_hdr_size(hdr)); 7082323754Savg ARCSTAT_INCR(arcstat_l2_lsize, -HDR_GET_LSIZE(hdr)); 7083286598Smav 7084307265Smav bytes_dropped += arc_hdr_size(hdr); 7085286598Smav (void) refcount_remove_many(&dev->l2ad_alloc, 7086307265Smav arc_hdr_size(hdr), hdr); 7087185029Spjd } 7088185029Spjd 7089185029Spjd /* 7090286763Smav * Allow ARC to begin reads and ghost list evictions to 7091286763Smav * this L2ARC entry. 7092185029Spjd */ 7093307265Smav arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING); 7094185029Spjd 7095185029Spjd mutex_exit(hash_lock); 7096185029Spjd } 7097185029Spjd 7098185029Spjd atomic_inc_64(&l2arc_writes_done); 7099185029Spjd list_remove(buflist, head); 7100286570Smav ASSERT(!HDR_HAS_L1HDR(head)); 7101286570Smav kmem_cache_free(hdr_l2only_cache, head); 7102286570Smav mutex_exit(&dev->l2ad_mtx); 7103185029Spjd 7104268085Sdelphij vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 7105268085Sdelphij 7106185029Spjd l2arc_do_free_on_write(); 7107185029Spjd 7108185029Spjd kmem_free(cb, sizeof (l2arc_write_callback_t)); 7109185029Spjd} 7110185029Spjd 7111185029Spjd/* 7112185029Spjd * A read to a cache device completed. Validate buffer contents before 7113185029Spjd * handing over to the regular ARC routines. 7114185029Spjd */ 7115185029Spjdstatic void 7116185029Spjdl2arc_read_done(zio_t *zio) 7117185029Spjd{ 7118185029Spjd l2arc_read_callback_t *cb; 7119185029Spjd arc_buf_hdr_t *hdr; 7120185029Spjd kmutex_t *hash_lock; 7121307265Smav boolean_t valid_cksum; 7122185029Spjd 7123307265Smav ASSERT3P(zio->io_vd, !=, NULL); 7124185029Spjd ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 7125185029Spjd 7126185029Spjd spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 7127185029Spjd 7128185029Spjd cb = zio->io_private; 7129307265Smav ASSERT3P(cb, !=, NULL); 7130307265Smav hdr = cb->l2rcb_hdr; 7131307265Smav ASSERT3P(hdr, !=, NULL); 7132185029Spjd 7133307265Smav hash_lock = HDR_LOCK(hdr); 7134185029Spjd mutex_enter(hash_lock); 7135219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 7136185029Spjd 7137185029Spjd /* 7138297848Savg * If the data was read into a temporary buffer, 7139297848Savg * move it and free the buffer. 7140297848Savg */ 7141321610Smav if (cb->l2rcb_abd != NULL) { 7142307265Smav ASSERT3U(arc_hdr_size(hdr), <, zio->io_size); 7143307265Smav if (zio->io_error == 0) { 7144321610Smav abd_copy(hdr->b_l1hdr.b_pabd, cb->l2rcb_abd, 7145307265Smav arc_hdr_size(hdr)); 7146307265Smav } 7147297848Savg 7148297848Savg /* 7149297848Savg * The following must be done regardless of whether 7150297848Savg * there was an error: 7151297848Savg * - free the temporary buffer 7152297848Savg * - point zio to the real ARC buffer 7153297848Savg * - set zio size accordingly 7154297848Savg * These are required because zio is either re-used for 7155297848Savg * an I/O of the block in the case of the error 7156297848Savg * or the zio is passed to arc_read_done() and it 7157297848Savg * needs real data. 7158297848Savg */ 7159321610Smav abd_free(cb->l2rcb_abd); 7160307265Smav zio->io_size = zio->io_orig_size = arc_hdr_size(hdr); 7161321610Smav zio->io_abd = zio->io_orig_abd = hdr->b_l1hdr.b_pabd; 7162297848Savg } 7163297848Savg 7164321610Smav ASSERT3P(zio->io_abd, !=, NULL); 7165251478Sdelphij 7166251478Sdelphij /* 7167185029Spjd * Check this survived the L2ARC journey. 7168185029Spjd */ 7169321610Smav ASSERT3P(zio->io_abd, ==, hdr->b_l1hdr.b_pabd); 7170307265Smav zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 7171307265Smav zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 7172307265Smav 7173307265Smav valid_cksum = arc_cksum_is_equal(hdr, zio); 7174307265Smav if (valid_cksum && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 7175185029Spjd mutex_exit(hash_lock); 7176307265Smav zio->io_private = hdr; 7177185029Spjd arc_read_done(zio); 7178185029Spjd } else { 7179185029Spjd mutex_exit(hash_lock); 7180185029Spjd /* 7181185029Spjd * Buffer didn't survive caching. Increment stats and 7182185029Spjd * reissue to the original storage device. 7183185029Spjd */ 7184185029Spjd if (zio->io_error != 0) { 7185185029Spjd ARCSTAT_BUMP(arcstat_l2_io_error); 7186185029Spjd } else { 7187249195Smm zio->io_error = SET_ERROR(EIO); 7188185029Spjd } 7189307265Smav if (!valid_cksum) 7190185029Spjd ARCSTAT_BUMP(arcstat_l2_cksum_bad); 7191185029Spjd 7192185029Spjd /* 7193185029Spjd * If there's no waiter, issue an async i/o to the primary 7194185029Spjd * storage now. If there *is* a waiter, the caller must 7195185029Spjd * issue the i/o in a context where it's OK to block. 7196185029Spjd */ 7197209962Smm if (zio->io_waiter == NULL) { 7198209962Smm zio_t *pio = zio_unique_parent(zio); 7199209962Smm 7200209962Smm ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 7201209962Smm 7202307265Smav zio_nowait(zio_read(pio, zio->io_spa, zio->io_bp, 7203321610Smav hdr->b_l1hdr.b_pabd, zio->io_size, arc_read_done, 7204307265Smav hdr, zio->io_priority, cb->l2rcb_flags, 7205307265Smav &cb->l2rcb_zb)); 7206209962Smm } 7207185029Spjd } 7208185029Spjd 7209185029Spjd kmem_free(cb, sizeof (l2arc_read_callback_t)); 7210185029Spjd} 7211185029Spjd 7212185029Spjd/* 7213185029Spjd * This is the list priority from which the L2ARC will search for pages to 7214185029Spjd * cache. This is used within loops (0..3) to cycle through lists in the 7215185029Spjd * desired order. This order can have a significant effect on cache 7216185029Spjd * performance. 7217185029Spjd * 7218185029Spjd * Currently the metadata lists are hit first, MFU then MRU, followed by 7219185029Spjd * the data lists. This function returns a locked list, and also returns 7220185029Spjd * the lock pointer. 7221185029Spjd */ 7222286763Smavstatic multilist_sublist_t * 7223286763Smavl2arc_sublist_lock(int list_num) 7224185029Spjd{ 7225286763Smav multilist_t *ml = NULL; 7226286763Smav unsigned int idx; 7227185029Spjd 7228286762Smav ASSERT(list_num >= 0 && list_num <= 3); 7229206796Spjd 7230286762Smav switch (list_num) { 7231286762Smav case 0: 7232321553Smav ml = arc_mfu->arcs_list[ARC_BUFC_METADATA]; 7233286762Smav break; 7234286762Smav case 1: 7235321553Smav ml = arc_mru->arcs_list[ARC_BUFC_METADATA]; 7236286762Smav break; 7237286762Smav case 2: 7238321553Smav ml = arc_mfu->arcs_list[ARC_BUFC_DATA]; 7239286762Smav break; 7240286762Smav case 3: 7241321553Smav ml = arc_mru->arcs_list[ARC_BUFC_DATA]; 7242286762Smav break; 7243185029Spjd } 7244185029Spjd 7245286763Smav /* 7246286763Smav * Return a randomly-selected sublist. This is acceptable 7247286763Smav * because the caller feeds only a little bit of data for each 7248286763Smav * call (8MB). Subsequent calls will result in different 7249286763Smav * sublists being selected. 7250286763Smav */ 7251286763Smav idx = multilist_get_random_index(ml); 7252286763Smav return (multilist_sublist_lock(ml, idx)); 7253185029Spjd} 7254185029Spjd 7255185029Spjd/* 7256185029Spjd * Evict buffers from the device write hand to the distance specified in 7257185029Spjd * bytes. This distance may span populated buffers, it may span nothing. 7258185029Spjd * This is clearing a region on the L2ARC device ready for writing. 7259185029Spjd * If the 'all' boolean is set, every buffer is evicted. 7260185029Spjd */ 7261185029Spjdstatic void 7262185029Spjdl2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 7263185029Spjd{ 7264185029Spjd list_t *buflist; 7265275811Sdelphij arc_buf_hdr_t *hdr, *hdr_prev; 7266185029Spjd kmutex_t *hash_lock; 7267185029Spjd uint64_t taddr; 7268185029Spjd 7269286570Smav buflist = &dev->l2ad_buflist; 7270185029Spjd 7271185029Spjd if (!all && dev->l2ad_first) { 7272185029Spjd /* 7273185029Spjd * This is the first sweep through the device. There is 7274185029Spjd * nothing to evict. 7275185029Spjd */ 7276185029Spjd return; 7277185029Spjd } 7278185029Spjd 7279185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 7280185029Spjd /* 7281185029Spjd * When nearing the end of the device, evict to the end 7282185029Spjd * before the device write hand jumps to the start. 7283185029Spjd */ 7284185029Spjd taddr = dev->l2ad_end; 7285185029Spjd } else { 7286185029Spjd taddr = dev->l2ad_hand + distance; 7287185029Spjd } 7288185029Spjd DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 7289185029Spjd uint64_t, taddr, boolean_t, all); 7290185029Spjd 7291185029Spjdtop: 7292286570Smav mutex_enter(&dev->l2ad_mtx); 7293275811Sdelphij for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { 7294275811Sdelphij hdr_prev = list_prev(buflist, hdr); 7295185029Spjd 7296275811Sdelphij hash_lock = HDR_LOCK(hdr); 7297286763Smav 7298286763Smav /* 7299286763Smav * We cannot use mutex_enter or else we can deadlock 7300286763Smav * with l2arc_write_buffers (due to swapping the order 7301286763Smav * the hash lock and l2ad_mtx are taken). 7302286763Smav */ 7303185029Spjd if (!mutex_tryenter(hash_lock)) { 7304185029Spjd /* 7305185029Spjd * Missed the hash lock. Retry. 7306185029Spjd */ 7307185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 7308286570Smav mutex_exit(&dev->l2ad_mtx); 7309185029Spjd mutex_enter(hash_lock); 7310185029Spjd mutex_exit(hash_lock); 7311185029Spjd goto top; 7312185029Spjd } 7313185029Spjd 7314323752Savg /* 7315323752Savg * A header can't be on this list if it doesn't have L2 header. 7316323752Savg */ 7317323752Savg ASSERT(HDR_HAS_L2HDR(hdr)); 7318185029Spjd 7319323752Savg /* Ensure this header has finished being written. */ 7320323752Savg ASSERT(!HDR_L2_WRITING(hdr)); 7321323752Savg ASSERT(!HDR_L2_WRITE_HEAD(hdr)); 7322323752Savg 7323323752Savg if (!all && (hdr->b_l2hdr.b_daddr >= taddr || 7324286570Smav hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { 7325185029Spjd /* 7326185029Spjd * We've evicted to the target address, 7327185029Spjd * or the end of the device. 7328185029Spjd */ 7329185029Spjd mutex_exit(hash_lock); 7330185029Spjd break; 7331185029Spjd } 7332185029Spjd 7333286570Smav if (!HDR_HAS_L1HDR(hdr)) { 7334275811Sdelphij ASSERT(!HDR_L2_READING(hdr)); 7335185029Spjd /* 7336185029Spjd * This doesn't exist in the ARC. Destroy. 7337185029Spjd * arc_hdr_destroy() will call list_remove() 7338323754Savg * and decrement arcstat_l2_lsize. 7339185029Spjd */ 7340275811Sdelphij arc_change_state(arc_anon, hdr, hash_lock); 7341275811Sdelphij arc_hdr_destroy(hdr); 7342185029Spjd } else { 7343286570Smav ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); 7344286570Smav ARCSTAT_BUMP(arcstat_l2_evict_l1cached); 7345185029Spjd /* 7346185029Spjd * Invalidate issued or about to be issued 7347185029Spjd * reads, since we may be about to write 7348185029Spjd * over this location. 7349185029Spjd */ 7350275811Sdelphij if (HDR_L2_READING(hdr)) { 7351185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_reading); 7352307265Smav arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED); 7353185029Spjd } 7354185029Spjd 7355286598Smav arc_hdr_l2hdr_destroy(hdr); 7356185029Spjd } 7357185029Spjd mutex_exit(hash_lock); 7358185029Spjd } 7359286570Smav mutex_exit(&dev->l2ad_mtx); 7360185029Spjd} 7361185029Spjd 7362185029Spjd/* 7363185029Spjd * Find and write ARC buffers to the L2ARC device. 7364185029Spjd * 7365275811Sdelphij * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid 7366185029Spjd * for reading until they have completed writing. 7367251478Sdelphij * The headroom_boost is an in-out parameter used to maintain headroom boost 7368251478Sdelphij * state between calls to this function. 7369251478Sdelphij * 7370251478Sdelphij * Returns the number of bytes actually written (which may be smaller than 7371251478Sdelphij * the delta by which the device hand has changed due to alignment). 7372185029Spjd */ 7373208373Smmstatic uint64_t 7374307265Smavl2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 7375185029Spjd{ 7376275811Sdelphij arc_buf_hdr_t *hdr, *hdr_prev, *head; 7377323754Savg uint64_t write_asize, write_psize, write_lsize, headroom; 7378251478Sdelphij boolean_t full; 7379185029Spjd l2arc_write_callback_t *cb; 7380185029Spjd zio_t *pio, *wzio; 7381228103Smm uint64_t guid = spa_load_guid(spa); 7382185029Spjd int try; 7383185029Spjd 7384307265Smav ASSERT3P(dev->l2ad_vdev, !=, NULL); 7385185029Spjd 7386185029Spjd pio = NULL; 7387323754Savg write_lsize = write_asize = write_psize = 0; 7388185029Spjd full = B_FALSE; 7389286570Smav head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); 7390307265Smav arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR); 7391185029Spjd 7392205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 7393185029Spjd /* 7394185029Spjd * Copy buffers for L2ARC writing. 7395185029Spjd */ 7396286762Smav for (try = 0; try <= 3; try++) { 7397286763Smav multilist_sublist_t *mls = l2arc_sublist_lock(try); 7398251478Sdelphij uint64_t passed_sz = 0; 7399251478Sdelphij 7400205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 7401185029Spjd 7402185029Spjd /* 7403185029Spjd * L2ARC fast warmup. 7404185029Spjd * 7405185029Spjd * Until the ARC is warm and starts to evict, read from the 7406185029Spjd * head of the ARC lists rather than the tail. 7407185029Spjd */ 7408185029Spjd if (arc_warm == B_FALSE) 7409286763Smav hdr = multilist_sublist_head(mls); 7410185029Spjd else 7411286763Smav hdr = multilist_sublist_tail(mls); 7412275811Sdelphij if (hdr == NULL) 7413205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 7414185029Spjd 7415286762Smav headroom = target_sz * l2arc_headroom; 7416307265Smav if (zfs_compressed_arc_enabled) 7417251478Sdelphij headroom = (headroom * l2arc_headroom_boost) / 100; 7418251478Sdelphij 7419275811Sdelphij for (; hdr; hdr = hdr_prev) { 7420251478Sdelphij kmutex_t *hash_lock; 7421251478Sdelphij 7422185029Spjd if (arc_warm == B_FALSE) 7423286763Smav hdr_prev = multilist_sublist_next(mls, hdr); 7424185029Spjd else 7425286763Smav hdr_prev = multilist_sublist_prev(mls, hdr); 7426307265Smav ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, 7427307265Smav HDR_GET_LSIZE(hdr)); 7428206796Spjd 7429275811Sdelphij hash_lock = HDR_LOCK(hdr); 7430251478Sdelphij if (!mutex_tryenter(hash_lock)) { 7431205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 7432185029Spjd /* 7433185029Spjd * Skip this buffer rather than waiting. 7434185029Spjd */ 7435185029Spjd continue; 7436185029Spjd } 7437185029Spjd 7438307265Smav passed_sz += HDR_GET_LSIZE(hdr); 7439185029Spjd if (passed_sz > headroom) { 7440185029Spjd /* 7441185029Spjd * Searched too far. 7442185029Spjd */ 7443185029Spjd mutex_exit(hash_lock); 7444205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 7445185029Spjd break; 7446185029Spjd } 7447185029Spjd 7448275811Sdelphij if (!l2arc_write_eligible(guid, hdr)) { 7449185029Spjd mutex_exit(hash_lock); 7450185029Spjd continue; 7451185029Spjd } 7452185029Spjd 7453315072Savg /* 7454315072Savg * We rely on the L1 portion of the header below, so 7455315072Savg * it's invalid for this header to have been evicted out 7456315072Savg * of the ghost cache, prior to being written out. The 7457315072Savg * ARC_FLAG_L2_WRITING bit ensures this won't happen. 7458315072Savg */ 7459315072Savg ASSERT(HDR_HAS_L1HDR(hdr)); 7460315072Savg 7461315072Savg ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); 7462321610Smav ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 7463315072Savg ASSERT3U(arc_hdr_size(hdr), >, 0); 7464323754Savg uint64_t psize = arc_hdr_size(hdr); 7465315072Savg uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, 7466323754Savg psize); 7467315072Savg 7468323754Savg if ((write_asize + asize) > target_sz) { 7469185029Spjd full = B_TRUE; 7470185029Spjd mutex_exit(hash_lock); 7471205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_full); 7472185029Spjd break; 7473185029Spjd } 7474185029Spjd 7475185029Spjd if (pio == NULL) { 7476185029Spjd /* 7477185029Spjd * Insert a dummy header on the buflist so 7478185029Spjd * l2arc_write_done() can find where the 7479185029Spjd * write buffers begin without searching. 7480185029Spjd */ 7481286763Smav mutex_enter(&dev->l2ad_mtx); 7482286570Smav list_insert_head(&dev->l2ad_buflist, head); 7483286763Smav mutex_exit(&dev->l2ad_mtx); 7484185029Spjd 7485185029Spjd cb = kmem_alloc( 7486185029Spjd sizeof (l2arc_write_callback_t), KM_SLEEP); 7487185029Spjd cb->l2wcb_dev = dev; 7488185029Spjd cb->l2wcb_head = head; 7489185029Spjd pio = zio_root(spa, l2arc_write_done, cb, 7490185029Spjd ZIO_FLAG_CANFAIL); 7491205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_pios); 7492185029Spjd } 7493185029Spjd 7494286570Smav hdr->b_l2hdr.b_dev = dev; 7495307265Smav hdr->b_l2hdr.b_daddr = dev->l2ad_hand; 7496307265Smav arc_hdr_set_flags(hdr, 7497307265Smav ARC_FLAG_L2_WRITING | ARC_FLAG_HAS_L2HDR); 7498251478Sdelphij 7499307265Smav mutex_enter(&dev->l2ad_mtx); 7500307265Smav list_insert_head(&dev->l2ad_buflist, hdr); 7501307265Smav mutex_exit(&dev->l2ad_mtx); 7502307265Smav 7503323754Savg (void) refcount_add_many(&dev->l2ad_alloc, psize, hdr); 7504251478Sdelphij 7505185029Spjd /* 7506307265Smav * Normally the L2ARC can use the hdr's data, but if 7507307265Smav * we're sharing data between the hdr and one of its 7508307265Smav * bufs, L2ARC needs its own copy of the data so that 7509321613Smav * the ZIO below can't race with the buf consumer. 7510321613Smav * Another case where we need to create a copy of the 7511321613Smav * data is when the buffer size is not device-aligned 7512321613Smav * and we need to pad the block to make it such. 7513321613Smav * That also keeps the clock hand suitably aligned. 7514321613Smav * 7515321613Smav * To ensure that the copy will be available for the 7516307265Smav * lifetime of the ZIO and be cleaned up afterwards, we 7517307265Smav * add it to the l2arc_free_on_write queue. 7518185029Spjd */ 7519321610Smav abd_t *to_write; 7520323754Savg if (!HDR_SHARED_DATA(hdr) && psize == asize) { 7521321610Smav to_write = hdr->b_l1hdr.b_pabd; 7522307265Smav } else { 7523321610Smav to_write = abd_alloc_for_io(asize, 7524321610Smav HDR_ISTYPE_METADATA(hdr)); 7525323754Savg abd_copy(to_write, hdr->b_l1hdr.b_pabd, psize); 7526323754Savg if (asize != psize) { 7527323754Savg abd_zero_off(to_write, psize, 7528323754Savg asize - psize); 7529307265Smav } 7530321610Smav l2arc_free_abd_on_write(to_write, asize, 7531321610Smav arc_buf_type(hdr)); 7532307265Smav } 7533307265Smav wzio = zio_write_phys(pio, dev->l2ad_vdev, 7534307265Smav hdr->b_l2hdr.b_daddr, asize, to_write, 7535307265Smav ZIO_CHECKSUM_OFF, NULL, hdr, 7536307265Smav ZIO_PRIORITY_ASYNC_WRITE, 7537307265Smav ZIO_FLAG_CANFAIL, B_FALSE); 7538307265Smav 7539323754Savg write_lsize += HDR_GET_LSIZE(hdr); 7540307265Smav DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 7541307265Smav zio_t *, wzio); 7542307265Smav 7543323754Savg write_psize += psize; 7544323754Savg write_asize += asize; 7545307265Smav dev->l2ad_hand += asize; 7546307265Smav 7547185029Spjd mutex_exit(hash_lock); 7548185029Spjd 7549307265Smav (void) zio_nowait(wzio); 7550251478Sdelphij } 7551251478Sdelphij 7552286763Smav multilist_sublist_unlock(mls); 7553251478Sdelphij 7554251478Sdelphij if (full == B_TRUE) 7555251478Sdelphij break; 7556251478Sdelphij } 7557251478Sdelphij 7558251478Sdelphij /* No buffers selected for writing? */ 7559251478Sdelphij if (pio == NULL) { 7560323754Savg ASSERT0(write_lsize); 7561286570Smav ASSERT(!HDR_HAS_L1HDR(head)); 7562286570Smav kmem_cache_free(hdr_l2only_cache, head); 7563251478Sdelphij return (0); 7564251478Sdelphij } 7565251478Sdelphij 7566315072Savg ASSERT3U(write_psize, <=, target_sz); 7567185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_sent); 7568323754Savg ARCSTAT_INCR(arcstat_l2_write_bytes, write_psize); 7569323754Savg ARCSTAT_INCR(arcstat_l2_lsize, write_lsize); 7570323754Savg ARCSTAT_INCR(arcstat_l2_psize, write_psize); 7571323754Savg vdev_space_update(dev->l2ad_vdev, write_psize, 0, 0); 7572185029Spjd 7573185029Spjd /* 7574185029Spjd * Bump device hand to the device start if it is approaching the end. 7575185029Spjd * l2arc_evict() will already have evicted ahead for this case. 7576185029Spjd */ 7577185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 7578185029Spjd dev->l2ad_hand = dev->l2ad_start; 7579185029Spjd dev->l2ad_first = B_FALSE; 7580185029Spjd } 7581185029Spjd 7582208373Smm dev->l2ad_writing = B_TRUE; 7583185029Spjd (void) zio_wait(pio); 7584208373Smm dev->l2ad_writing = B_FALSE; 7585208373Smm 7586251478Sdelphij return (write_asize); 7587185029Spjd} 7588185029Spjd 7589185029Spjd/* 7590185029Spjd * This thread feeds the L2ARC at regular intervals. This is the beating 7591185029Spjd * heart of the L2ARC. 7592185029Spjd */ 7593185029Spjdstatic void 7594185029Spjdl2arc_feed_thread(void *dummy __unused) 7595185029Spjd{ 7596185029Spjd callb_cpr_t cpr; 7597185029Spjd l2arc_dev_t *dev; 7598185029Spjd spa_t *spa; 7599208373Smm uint64_t size, wrote; 7600219089Spjd clock_t begin, next = ddi_get_lbolt(); 7601185029Spjd 7602185029Spjd CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 7603185029Spjd 7604185029Spjd mutex_enter(&l2arc_feed_thr_lock); 7605185029Spjd 7606185029Spjd while (l2arc_thread_exit == 0) { 7607185029Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 7608185029Spjd (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 7609219089Spjd next - ddi_get_lbolt()); 7610185029Spjd CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 7611219089Spjd next = ddi_get_lbolt() + hz; 7612185029Spjd 7613185029Spjd /* 7614185029Spjd * Quick check for L2ARC devices. 7615185029Spjd */ 7616185029Spjd mutex_enter(&l2arc_dev_mtx); 7617185029Spjd if (l2arc_ndev == 0) { 7618185029Spjd mutex_exit(&l2arc_dev_mtx); 7619185029Spjd continue; 7620185029Spjd } 7621185029Spjd mutex_exit(&l2arc_dev_mtx); 7622219089Spjd begin = ddi_get_lbolt(); 7623185029Spjd 7624185029Spjd /* 7625185029Spjd * This selects the next l2arc device to write to, and in 7626185029Spjd * doing so the next spa to feed from: dev->l2ad_spa. This 7627185029Spjd * will return NULL if there are now no l2arc devices or if 7628185029Spjd * they are all faulted. 7629185029Spjd * 7630185029Spjd * If a device is returned, its spa's config lock is also 7631185029Spjd * held to prevent device removal. l2arc_dev_get_next() 7632185029Spjd * will grab and release l2arc_dev_mtx. 7633185029Spjd */ 7634185029Spjd if ((dev = l2arc_dev_get_next()) == NULL) 7635185029Spjd continue; 7636185029Spjd 7637185029Spjd spa = dev->l2ad_spa; 7638307265Smav ASSERT3P(spa, !=, NULL); 7639185029Spjd 7640185029Spjd /* 7641219089Spjd * If the pool is read-only then force the feed thread to 7642219089Spjd * sleep a little longer. 7643219089Spjd */ 7644219089Spjd if (!spa_writeable(spa)) { 7645219089Spjd next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 7646219089Spjd spa_config_exit(spa, SCL_L2ARC, dev); 7647219089Spjd continue; 7648219089Spjd } 7649219089Spjd 7650219089Spjd /* 7651185029Spjd * Avoid contributing to memory pressure. 7652185029Spjd */ 7653185029Spjd if (arc_reclaim_needed()) { 7654185029Spjd ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 7655185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 7656185029Spjd continue; 7657185029Spjd } 7658185029Spjd 7659185029Spjd ARCSTAT_BUMP(arcstat_l2_feeds); 7660185029Spjd 7661251478Sdelphij size = l2arc_write_size(); 7662185029Spjd 7663185029Spjd /* 7664185029Spjd * Evict L2ARC buffers that will be overwritten. 7665185029Spjd */ 7666185029Spjd l2arc_evict(dev, size, B_FALSE); 7667185029Spjd 7668185029Spjd /* 7669185029Spjd * Write ARC buffers. 7670185029Spjd */ 7671307265Smav wrote = l2arc_write_buffers(spa, dev, size); 7672208373Smm 7673208373Smm /* 7674208373Smm * Calculate interval between writes. 7675208373Smm */ 7676208373Smm next = l2arc_write_interval(begin, size, wrote); 7677185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 7678185029Spjd } 7679185029Spjd 7680185029Spjd l2arc_thread_exit = 0; 7681185029Spjd cv_broadcast(&l2arc_feed_thr_cv); 7682185029Spjd CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 7683185029Spjd thread_exit(); 7684185029Spjd} 7685185029Spjd 7686185029Spjdboolean_t 7687185029Spjdl2arc_vdev_present(vdev_t *vd) 7688185029Spjd{ 7689185029Spjd l2arc_dev_t *dev; 7690185029Spjd 7691185029Spjd mutex_enter(&l2arc_dev_mtx); 7692185029Spjd for (dev = list_head(l2arc_dev_list); dev != NULL; 7693185029Spjd dev = list_next(l2arc_dev_list, dev)) { 7694185029Spjd if (dev->l2ad_vdev == vd) 7695185029Spjd break; 7696185029Spjd } 7697185029Spjd mutex_exit(&l2arc_dev_mtx); 7698185029Spjd 7699185029Spjd return (dev != NULL); 7700185029Spjd} 7701185029Spjd 7702185029Spjd/* 7703185029Spjd * Add a vdev for use by the L2ARC. By this point the spa has already 7704185029Spjd * validated the vdev and opened it. 7705185029Spjd */ 7706185029Spjdvoid 7707219089Spjdl2arc_add_vdev(spa_t *spa, vdev_t *vd) 7708185029Spjd{ 7709185029Spjd l2arc_dev_t *adddev; 7710185029Spjd 7711185029Spjd ASSERT(!l2arc_vdev_present(vd)); 7712185029Spjd 7713255753Sgibbs vdev_ashift_optimize(vd); 7714255753Sgibbs 7715185029Spjd /* 7716185029Spjd * Create a new l2arc device entry. 7717185029Spjd */ 7718185029Spjd adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 7719185029Spjd adddev->l2ad_spa = spa; 7720185029Spjd adddev->l2ad_vdev = vd; 7721219089Spjd adddev->l2ad_start = VDEV_LABEL_START_SIZE; 7722219089Spjd adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 7723185029Spjd adddev->l2ad_hand = adddev->l2ad_start; 7724185029Spjd adddev->l2ad_first = B_TRUE; 7725208373Smm adddev->l2ad_writing = B_FALSE; 7726185029Spjd 7727286570Smav mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); 7728185029Spjd /* 7729185029Spjd * This is a list of all ARC buffers that are still valid on the 7730185029Spjd * device. 7731185029Spjd */ 7732286570Smav list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 7733286570Smav offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); 7734185029Spjd 7735219089Spjd vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 7736286598Smav refcount_create(&adddev->l2ad_alloc); 7737185029Spjd 7738185029Spjd /* 7739185029Spjd * Add device to global list 7740185029Spjd */ 7741185029Spjd mutex_enter(&l2arc_dev_mtx); 7742185029Spjd list_insert_head(l2arc_dev_list, adddev); 7743185029Spjd atomic_inc_64(&l2arc_ndev); 7744185029Spjd mutex_exit(&l2arc_dev_mtx); 7745185029Spjd} 7746185029Spjd 7747185029Spjd/* 7748185029Spjd * Remove a vdev from the L2ARC. 7749185029Spjd */ 7750185029Spjdvoid 7751185029Spjdl2arc_remove_vdev(vdev_t *vd) 7752185029Spjd{ 7753185029Spjd l2arc_dev_t *dev, *nextdev, *remdev = NULL; 7754185029Spjd 7755185029Spjd /* 7756185029Spjd * Find the device by vdev 7757185029Spjd */ 7758185029Spjd mutex_enter(&l2arc_dev_mtx); 7759185029Spjd for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 7760185029Spjd nextdev = list_next(l2arc_dev_list, dev); 7761185029Spjd if (vd == dev->l2ad_vdev) { 7762185029Spjd remdev = dev; 7763185029Spjd break; 7764185029Spjd } 7765185029Spjd } 7766307265Smav ASSERT3P(remdev, !=, NULL); 7767185029Spjd 7768185029Spjd /* 7769185029Spjd * Remove device from global list 7770185029Spjd */ 7771185029Spjd list_remove(l2arc_dev_list, remdev); 7772185029Spjd l2arc_dev_last = NULL; /* may have been invalidated */ 7773185029Spjd atomic_dec_64(&l2arc_ndev); 7774185029Spjd mutex_exit(&l2arc_dev_mtx); 7775185029Spjd 7776185029Spjd /* 7777185029Spjd * Clear all buflists and ARC references. L2ARC device flush. 7778185029Spjd */ 7779185029Spjd l2arc_evict(remdev, 0, B_TRUE); 7780286570Smav list_destroy(&remdev->l2ad_buflist); 7781286570Smav mutex_destroy(&remdev->l2ad_mtx); 7782286598Smav refcount_destroy(&remdev->l2ad_alloc); 7783185029Spjd kmem_free(remdev, sizeof (l2arc_dev_t)); 7784185029Spjd} 7785185029Spjd 7786185029Spjdvoid 7787185029Spjdl2arc_init(void) 7788185029Spjd{ 7789185029Spjd l2arc_thread_exit = 0; 7790185029Spjd l2arc_ndev = 0; 7791185029Spjd l2arc_writes_sent = 0; 7792185029Spjd l2arc_writes_done = 0; 7793185029Spjd 7794185029Spjd mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 7795185029Spjd cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 7796185029Spjd mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 7797185029Spjd mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 7798185029Spjd 7799185029Spjd l2arc_dev_list = &L2ARC_dev_list; 7800185029Spjd l2arc_free_on_write = &L2ARC_free_on_write; 7801185029Spjd list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 7802185029Spjd offsetof(l2arc_dev_t, l2ad_node)); 7803185029Spjd list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 7804185029Spjd offsetof(l2arc_data_free_t, l2df_list_node)); 7805185029Spjd} 7806185029Spjd 7807185029Spjdvoid 7808185029Spjdl2arc_fini(void) 7809185029Spjd{ 7810185029Spjd /* 7811185029Spjd * This is called from dmu_fini(), which is called from spa_fini(); 7812185029Spjd * Because of this, we can assume that all l2arc devices have 7813185029Spjd * already been removed when the pools themselves were removed. 7814185029Spjd */ 7815185029Spjd 7816185029Spjd l2arc_do_free_on_write(); 7817185029Spjd 7818185029Spjd mutex_destroy(&l2arc_feed_thr_lock); 7819185029Spjd cv_destroy(&l2arc_feed_thr_cv); 7820185029Spjd mutex_destroy(&l2arc_dev_mtx); 7821185029Spjd mutex_destroy(&l2arc_free_on_write_mtx); 7822185029Spjd 7823185029Spjd list_destroy(l2arc_dev_list); 7824185029Spjd list_destroy(l2arc_free_on_write); 7825185029Spjd} 7826185029Spjd 7827185029Spjdvoid 7828185029Spjdl2arc_start(void) 7829185029Spjd{ 7830209962Smm if (!(spa_mode_global & FWRITE)) 7831185029Spjd return; 7832185029Spjd 7833185029Spjd (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 7834185029Spjd TS_RUN, minclsyspri); 7835185029Spjd} 7836185029Spjd 7837185029Spjdvoid 7838185029Spjdl2arc_stop(void) 7839185029Spjd{ 7840209962Smm if (!(spa_mode_global & FWRITE)) 7841185029Spjd return; 7842185029Spjd 7843185029Spjd mutex_enter(&l2arc_feed_thr_lock); 7844185029Spjd cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 7845185029Spjd l2arc_thread_exit = 1; 7846185029Spjd while (l2arc_thread_exit != 0) 7847185029Spjd cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 7848185029Spjd mutex_exit(&l2arc_feed_thr_lock); 7849185029Spjd} 7850