arc.c revision 286598
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. 24268123Sdelphij * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 25260835Sdelphij * Copyright (c) 2014 by Saso Kiselkov. All rights reserved. 26268085Sdelphij * Copyright 2014 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 80168404Spjd * 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 83168404Spjd * arc list locks. 84168404Spjd * 85168404Spjd * Buffers do not have their own mutexs, rather they rely on the 86168404Spjd * hash table mutexs for the bulk of their protection (i.e. most 87168404Spjd * fields in the arc_buf_hdr_t are protected by these mutexs). 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 * 96168404Spjd * Each arc state also has a mutex which is used to protect the 97168404Spjd * buffer list associated with the state. When attempting to 98168404Spjd * 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 * Arc buffers may have an associated eviction callback function. 103168404Spjd * This function will be invoked prior to removing the buffer (e.g. 104168404Spjd * in arc_do_user_evicts()). Note however that the data associated 105168404Spjd * with the buffer may be evicted prior to the callback. The callback 106168404Spjd * must be made with *no locks held* (to prevent deadlock). Additionally, 107168404Spjd * the users of callbacks must ensure that their private data is 108268858Sdelphij * protected from simultaneous callbacks from arc_clear_callback() 109168404Spjd * and arc_do_user_evicts(). 110168404Spjd * 111168404Spjd * Note that the majority of the performance stats are manipulated 112168404Spjd * with atomic operations. 113185029Spjd * 114286570Smav * The L2ARC uses the l2ad_mtx on each vdev for the following: 115185029Spjd * 116185029Spjd * - L2ARC buflist creation 117185029Spjd * - L2ARC buflist eviction 118185029Spjd * - L2ARC write completion, which walks L2ARC buflists 119185029Spjd * - ARC header destruction, as it removes from L2ARC buflists 120185029Spjd * - ARC header release, as it removes from L2ARC buflists 121168404Spjd */ 122168404Spjd 123168404Spjd#include <sys/spa.h> 124168404Spjd#include <sys/zio.h> 125251478Sdelphij#include <sys/zio_compress.h> 126168404Spjd#include <sys/zfs_context.h> 127168404Spjd#include <sys/arc.h> 128168404Spjd#include <sys/refcount.h> 129185029Spjd#include <sys/vdev.h> 130219089Spjd#include <sys/vdev_impl.h> 131258632Savg#include <sys/dsl_pool.h> 132168404Spjd#ifdef _KERNEL 133168404Spjd#include <sys/dnlc.h> 134168404Spjd#endif 135168404Spjd#include <sys/callb.h> 136168404Spjd#include <sys/kstat.h> 137248572Ssmh#include <sys/trim_map.h> 138219089Spjd#include <zfs_fletcher.h> 139168404Spjd#include <sys/sdt.h> 140168404Spjd 141191902Skmacy#include <vm/vm_pageout.h> 142272483Ssmh#include <machine/vmparam.h> 143191902Skmacy 144240133Smm#ifdef illumos 145240133Smm#ifndef _KERNEL 146240133Smm/* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ 147240133Smmboolean_t arc_watch = B_FALSE; 148240133Smmint arc_procfd; 149240133Smm#endif 150240133Smm#endif /* illumos */ 151240133Smm 152168404Spjdstatic kmutex_t arc_reclaim_thr_lock; 153168404Spjdstatic kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 154168404Spjdstatic uint8_t arc_thread_exit; 155168404Spjd 156168404Spjd#define ARC_REDUCE_DNLC_PERCENT 3 157168404Spjduint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 158168404Spjd 159168404Spjdtypedef enum arc_reclaim_strategy { 160168404Spjd ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 161168404Spjd ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 162168404Spjd} arc_reclaim_strategy_t; 163168404Spjd 164258632Savg/* 165258632Savg * The number of iterations through arc_evict_*() before we 166258632Savg * drop & reacquire the lock. 167258632Savg */ 168258632Savgint arc_evict_iterations = 100; 169258632Savg 170168404Spjd/* number of seconds before growing cache again */ 171168404Spjdstatic int arc_grow_retry = 60; 172168404Spjd 173208373Smm/* shift of arc_c for calculating both min and max arc_p */ 174208373Smmstatic int arc_p_min_shift = 4; 175208373Smm 176208373Smm/* log2(fraction of arc to reclaim) */ 177208373Smmstatic int arc_shrink_shift = 5; 178208373Smm 179168404Spjd/* 180168404Spjd * minimum lifespan of a prefetch block in clock ticks 181168404Spjd * (initialized in arc_init()) 182168404Spjd */ 183168404Spjdstatic int arc_min_prefetch_lifespan; 184168404Spjd 185258632Savg/* 186258632Savg * If this percent of memory is free, don't throttle. 187258632Savg */ 188258632Savgint arc_lotsfree_percent = 10; 189258632Savg 190208373Smmstatic int arc_dead; 191194043Skmacyextern int zfs_prefetch_disable; 192168404Spjd 193168404Spjd/* 194185029Spjd * The arc has filled available memory and has now warmed up. 195185029Spjd */ 196185029Spjdstatic boolean_t arc_warm; 197185029Spjd 198185029Spjduint64_t zfs_arc_max; 199185029Spjduint64_t zfs_arc_min; 200185029Spjduint64_t zfs_arc_meta_limit = 0; 201275780Sdelphijuint64_t zfs_arc_meta_min = 0; 202208373Smmint zfs_arc_grow_retry = 0; 203208373Smmint zfs_arc_shrink_shift = 0; 204208373Smmint zfs_arc_p_min_shift = 0; 205242845Sdelphijint zfs_disable_dup_eviction = 0; 206269230Sdelphijuint64_t zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ 207272483Ssmhu_int zfs_arc_free_target = 0; 208185029Spjd 209270759Ssmhstatic int sysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS); 210275748Sdelphijstatic int sysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS); 211270759Ssmh 212270759Ssmh#ifdef _KERNEL 213270759Ssmhstatic void 214270759Ssmharc_free_target_init(void *unused __unused) 215270759Ssmh{ 216270759Ssmh 217272483Ssmh zfs_arc_free_target = vm_pageout_wakeup_thresh; 218270759Ssmh} 219270759SsmhSYSINIT(arc_free_target_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_ANY, 220270759Ssmh arc_free_target_init, NULL); 221270759Ssmh 222185029SpjdTUNABLE_QUAD("vfs.zfs.arc_meta_limit", &zfs_arc_meta_limit); 223275780SdelphijTUNABLE_QUAD("vfs.zfs.arc_meta_min", &zfs_arc_meta_min); 224273026SdelphijTUNABLE_INT("vfs.zfs.arc_shrink_shift", &zfs_arc_shrink_shift); 225168473SpjdSYSCTL_DECL(_vfs_zfs); 226217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_max, CTLFLAG_RDTUN, &zfs_arc_max, 0, 227168473Spjd "Maximum ARC size"); 228217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_min, CTLFLAG_RDTUN, &zfs_arc_min, 0, 229168473Spjd "Minimum ARC size"); 230269230SdelphijSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_average_blocksize, CTLFLAG_RDTUN, 231269230Sdelphij &zfs_arc_average_blocksize, 0, 232269230Sdelphij "ARC average blocksize"); 233273026SdelphijSYSCTL_INT(_vfs_zfs, OID_AUTO, arc_shrink_shift, CTLFLAG_RW, 234273026Sdelphij &arc_shrink_shift, 0, 235273026Sdelphij "log2(fraction of arc to reclaim)"); 236273026Sdelphij 237270759Ssmh/* 238270759Ssmh * We don't have a tunable for arc_free_target due to the dependency on 239270759Ssmh * pagedaemon initialisation. 240270759Ssmh */ 241270759SsmhSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_free_target, 242270759Ssmh CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(u_int), 243270759Ssmh sysctl_vfs_zfs_arc_free_target, "IU", 244270759Ssmh "Desired number of free pages below which ARC triggers reclaim"); 245168404Spjd 246270759Ssmhstatic int 247270759Ssmhsysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS) 248270759Ssmh{ 249270759Ssmh u_int val; 250270759Ssmh int err; 251270759Ssmh 252270759Ssmh val = zfs_arc_free_target; 253270759Ssmh err = sysctl_handle_int(oidp, &val, 0, req); 254270759Ssmh if (err != 0 || req->newptr == NULL) 255270759Ssmh return (err); 256270759Ssmh 257272483Ssmh if (val < minfree) 258270759Ssmh return (EINVAL); 259272483Ssmh if (val > vm_cnt.v_page_count) 260270759Ssmh return (EINVAL); 261270759Ssmh 262270759Ssmh zfs_arc_free_target = val; 263270759Ssmh 264270759Ssmh return (0); 265270759Ssmh} 266275748Sdelphij 267275748Sdelphij/* 268275748Sdelphij * Must be declared here, before the definition of corresponding kstat 269275748Sdelphij * macro which uses the same names will confuse the compiler. 270275748Sdelphij */ 271275748SdelphijSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_meta_limit, 272275748Sdelphij CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t), 273275748Sdelphij sysctl_vfs_zfs_arc_meta_limit, "QU", 274275748Sdelphij "ARC metadata limit"); 275272483Ssmh#endif 276270759Ssmh 277168404Spjd/* 278185029Spjd * Note that buffers can be in one of 6 states: 279168404Spjd * ARC_anon - anonymous (discussed below) 280168404Spjd * ARC_mru - recently used, currently cached 281168404Spjd * ARC_mru_ghost - recentely used, no longer in cache 282168404Spjd * ARC_mfu - frequently used, currently cached 283168404Spjd * ARC_mfu_ghost - frequently used, no longer in cache 284185029Spjd * ARC_l2c_only - exists in L2ARC but not other states 285185029Spjd * When there are no active references to the buffer, they are 286185029Spjd * are linked onto a list in one of these arc states. These are 287185029Spjd * the only buffers that can be evicted or deleted. Within each 288185029Spjd * state there are multiple lists, one for meta-data and one for 289185029Spjd * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 290185029Spjd * etc.) is tracked separately so that it can be managed more 291185029Spjd * explicitly: favored over data, limited explicitly. 292168404Spjd * 293168404Spjd * Anonymous buffers are buffers that are not associated with 294168404Spjd * a DVA. These are buffers that hold dirty block copies 295168404Spjd * before they are written to stable storage. By definition, 296168404Spjd * they are "ref'd" and are considered part of arc_mru 297168404Spjd * that cannot be freed. Generally, they will aquire a DVA 298168404Spjd * as they are written and migrate onto the arc_mru list. 299185029Spjd * 300185029Spjd * The ARC_l2c_only state is for buffers that are in the second 301185029Spjd * level ARC but no longer in any of the ARC_m* lists. The second 302185029Spjd * level ARC itself may also contain buffers that are in any of 303185029Spjd * the ARC_m* states - meaning that a buffer can exist in two 304185029Spjd * places. The reason for the ARC_l2c_only state is to keep the 305185029Spjd * buffer header in the hash table, so that reads that hit the 306185029Spjd * second level ARC benefit from these fast lookups. 307168404Spjd */ 308168404Spjd 309205264Skmacy#define ARCS_LOCK_PAD CACHE_LINE_SIZE 310205231Skmacystruct arcs_lock { 311205231Skmacy kmutex_t arcs_lock; 312205231Skmacy#ifdef _KERNEL 313205231Skmacy unsigned char pad[(ARCS_LOCK_PAD - sizeof (kmutex_t))]; 314205231Skmacy#endif 315205231Skmacy}; 316205231Skmacy 317205231Skmacy/* 318205231Skmacy * must be power of two for mask use to work 319205231Skmacy * 320205231Skmacy */ 321205231Skmacy#define ARC_BUFC_NUMDATALISTS 16 322205231Skmacy#define ARC_BUFC_NUMMETADATALISTS 16 323206796Spjd#define ARC_BUFC_NUMLISTS (ARC_BUFC_NUMMETADATALISTS + ARC_BUFC_NUMDATALISTS) 324205231Skmacy 325168404Spjdtypedef struct arc_state { 326185029Spjd uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 327185029Spjd uint64_t arcs_size; /* total amount of data in this state */ 328205231Skmacy list_t arcs_lists[ARC_BUFC_NUMLISTS]; /* list of evictable buffers */ 329205264Skmacy struct arcs_lock arcs_locks[ARC_BUFC_NUMLISTS] __aligned(CACHE_LINE_SIZE); 330168404Spjd} arc_state_t; 331168404Spjd 332206796Spjd#define ARCS_LOCK(s, i) (&((s)->arcs_locks[(i)].arcs_lock)) 333205231Skmacy 334185029Spjd/* The 6 states: */ 335168404Spjdstatic arc_state_t ARC_anon; 336168404Spjdstatic arc_state_t ARC_mru; 337168404Spjdstatic arc_state_t ARC_mru_ghost; 338168404Spjdstatic arc_state_t ARC_mfu; 339168404Spjdstatic arc_state_t ARC_mfu_ghost; 340185029Spjdstatic arc_state_t ARC_l2c_only; 341168404Spjd 342168404Spjdtypedef struct arc_stats { 343168404Spjd kstat_named_t arcstat_hits; 344168404Spjd kstat_named_t arcstat_misses; 345168404Spjd kstat_named_t arcstat_demand_data_hits; 346168404Spjd kstat_named_t arcstat_demand_data_misses; 347168404Spjd kstat_named_t arcstat_demand_metadata_hits; 348168404Spjd kstat_named_t arcstat_demand_metadata_misses; 349168404Spjd kstat_named_t arcstat_prefetch_data_hits; 350168404Spjd kstat_named_t arcstat_prefetch_data_misses; 351168404Spjd kstat_named_t arcstat_prefetch_metadata_hits; 352168404Spjd kstat_named_t arcstat_prefetch_metadata_misses; 353168404Spjd kstat_named_t arcstat_mru_hits; 354168404Spjd kstat_named_t arcstat_mru_ghost_hits; 355168404Spjd kstat_named_t arcstat_mfu_hits; 356168404Spjd kstat_named_t arcstat_mfu_ghost_hits; 357205231Skmacy kstat_named_t arcstat_allocated; 358168404Spjd kstat_named_t arcstat_deleted; 359205231Skmacy kstat_named_t arcstat_stolen; 360168404Spjd kstat_named_t arcstat_recycle_miss; 361251629Sdelphij /* 362251629Sdelphij * Number of buffers that could not be evicted because the hash lock 363251629Sdelphij * was held by another thread. The lock may not necessarily be held 364251629Sdelphij * by something using the same buffer, since hash locks are shared 365251629Sdelphij * by multiple buffers. 366251629Sdelphij */ 367168404Spjd kstat_named_t arcstat_mutex_miss; 368251629Sdelphij /* 369251629Sdelphij * Number of buffers skipped because they have I/O in progress, are 370251629Sdelphij * indrect prefetch buffers that have not lived long enough, or are 371251629Sdelphij * not from the spa we're trying to evict from. 372251629Sdelphij */ 373168404Spjd kstat_named_t arcstat_evict_skip; 374208373Smm kstat_named_t arcstat_evict_l2_cached; 375208373Smm kstat_named_t arcstat_evict_l2_eligible; 376208373Smm kstat_named_t arcstat_evict_l2_ineligible; 377168404Spjd kstat_named_t arcstat_hash_elements; 378168404Spjd kstat_named_t arcstat_hash_elements_max; 379168404Spjd kstat_named_t arcstat_hash_collisions; 380168404Spjd kstat_named_t arcstat_hash_chains; 381168404Spjd kstat_named_t arcstat_hash_chain_max; 382168404Spjd kstat_named_t arcstat_p; 383168404Spjd kstat_named_t arcstat_c; 384168404Spjd kstat_named_t arcstat_c_min; 385168404Spjd kstat_named_t arcstat_c_max; 386168404Spjd kstat_named_t arcstat_size; 387286574Smav /* 388286574Smav * Number of bytes consumed by internal ARC structures necessary 389286574Smav * for tracking purposes; these structures are not actually 390286574Smav * backed by ARC buffers. This includes arc_buf_hdr_t structures 391286574Smav * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only 392286574Smav * caches), and arc_buf_t structures (allocated via arc_buf_t 393286574Smav * cache). 394286574Smav */ 395185029Spjd kstat_named_t arcstat_hdr_size; 396286574Smav /* 397286574Smav * Number of bytes consumed by ARC buffers of type equal to 398286574Smav * ARC_BUFC_DATA. This is generally consumed by buffers backing 399286574Smav * on disk user data (e.g. plain file contents). 400286574Smav */ 401208373Smm kstat_named_t arcstat_data_size; 402286574Smav /* 403286574Smav * Number of bytes consumed by ARC buffers of type equal to 404286574Smav * ARC_BUFC_METADATA. This is generally consumed by buffers 405286574Smav * backing on disk data that is used for internal ZFS 406286574Smav * structures (e.g. ZAP, dnode, indirect blocks, etc). 407286574Smav */ 408286574Smav kstat_named_t arcstat_metadata_size; 409286574Smav /* 410286574Smav * Number of bytes consumed by various buffers and structures 411286574Smav * not actually backed with ARC buffers. This includes bonus 412286574Smav * buffers (allocated directly via zio_buf_* functions), 413286574Smav * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t 414286574Smav * cache), and dnode_t structures (allocated via dnode_t cache). 415286574Smav */ 416208373Smm kstat_named_t arcstat_other_size; 417286574Smav /* 418286574Smav * Total number of bytes consumed by ARC buffers residing in the 419286574Smav * arc_anon state. This includes *all* buffers in the arc_anon 420286574Smav * state; e.g. data, metadata, evictable, and unevictable buffers 421286574Smav * are all included in this value. 422286574Smav */ 423286574Smav kstat_named_t arcstat_anon_size; 424286574Smav /* 425286574Smav * Number of bytes consumed by ARC buffers that meet the 426286574Smav * following criteria: backing buffers of type ARC_BUFC_DATA, 427286574Smav * residing in the arc_anon state, and are eligible for eviction 428286574Smav * (e.g. have no outstanding holds on the buffer). 429286574Smav */ 430286574Smav kstat_named_t arcstat_anon_evictable_data; 431286574Smav /* 432286574Smav * Number of bytes consumed by ARC buffers that meet the 433286574Smav * following criteria: backing buffers of type ARC_BUFC_METADATA, 434286574Smav * residing in the arc_anon state, and are eligible for eviction 435286574Smav * (e.g. have no outstanding holds on the buffer). 436286574Smav */ 437286574Smav kstat_named_t arcstat_anon_evictable_metadata; 438286574Smav /* 439286574Smav * Total number of bytes consumed by ARC buffers residing in the 440286574Smav * arc_mru state. This includes *all* buffers in the arc_mru 441286574Smav * state; e.g. data, metadata, evictable, and unevictable buffers 442286574Smav * are all included in this value. 443286574Smav */ 444286574Smav kstat_named_t arcstat_mru_size; 445286574Smav /* 446286574Smav * Number of bytes consumed by ARC buffers that meet the 447286574Smav * following criteria: backing buffers of type ARC_BUFC_DATA, 448286574Smav * residing in the arc_mru state, and are eligible for eviction 449286574Smav * (e.g. have no outstanding holds on the buffer). 450286574Smav */ 451286574Smav kstat_named_t arcstat_mru_evictable_data; 452286574Smav /* 453286574Smav * Number of bytes consumed by ARC buffers that meet the 454286574Smav * following criteria: backing buffers of type ARC_BUFC_METADATA, 455286574Smav * residing in the arc_mru state, and are eligible for eviction 456286574Smav * (e.g. have no outstanding holds on the buffer). 457286574Smav */ 458286574Smav kstat_named_t arcstat_mru_evictable_metadata; 459286574Smav /* 460286574Smav * Total number of bytes that *would have been* consumed by ARC 461286574Smav * buffers in the arc_mru_ghost state. The key thing to note 462286574Smav * here, is the fact that this size doesn't actually indicate 463286574Smav * RAM consumption. The ghost lists only consist of headers and 464286574Smav * don't actually have ARC buffers linked off of these headers. 465286574Smav * Thus, *if* the headers had associated ARC buffers, these 466286574Smav * buffers *would have* consumed this number of bytes. 467286574Smav */ 468286574Smav kstat_named_t arcstat_mru_ghost_size; 469286574Smav /* 470286574Smav * Number of bytes that *would have been* consumed by ARC 471286574Smav * buffers that are eligible for eviction, of type 472286574Smav * ARC_BUFC_DATA, and linked off the arc_mru_ghost state. 473286574Smav */ 474286574Smav kstat_named_t arcstat_mru_ghost_evictable_data; 475286574Smav /* 476286574Smav * Number of bytes that *would have been* consumed by ARC 477286574Smav * buffers that are eligible for eviction, of type 478286574Smav * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 479286574Smav */ 480286574Smav kstat_named_t arcstat_mru_ghost_evictable_metadata; 481286574Smav /* 482286574Smav * Total number of bytes consumed by ARC buffers residing in the 483286574Smav * arc_mfu state. This includes *all* buffers in the arc_mfu 484286574Smav * state; e.g. data, metadata, evictable, and unevictable buffers 485286574Smav * are all included in this value. 486286574Smav */ 487286574Smav kstat_named_t arcstat_mfu_size; 488286574Smav /* 489286574Smav * Number of bytes consumed by ARC buffers that are eligible for 490286574Smav * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu 491286574Smav * state. 492286574Smav */ 493286574Smav kstat_named_t arcstat_mfu_evictable_data; 494286574Smav /* 495286574Smav * Number of bytes consumed by ARC buffers that are eligible for 496286574Smav * eviction, of type ARC_BUFC_METADATA, and reside in the 497286574Smav * arc_mfu state. 498286574Smav */ 499286574Smav kstat_named_t arcstat_mfu_evictable_metadata; 500286574Smav /* 501286574Smav * Total number of bytes that *would have been* consumed by ARC 502286574Smav * buffers in the arc_mfu_ghost state. See the comment above 503286574Smav * arcstat_mru_ghost_size for more details. 504286574Smav */ 505286574Smav kstat_named_t arcstat_mfu_ghost_size; 506286574Smav /* 507286574Smav * Number of bytes that *would have been* consumed by ARC 508286574Smav * buffers that are eligible for eviction, of type 509286574Smav * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state. 510286574Smav */ 511286574Smav kstat_named_t arcstat_mfu_ghost_evictable_data; 512286574Smav /* 513286574Smav * Number of bytes that *would have been* consumed by ARC 514286574Smav * buffers that are eligible for eviction, of type 515286574Smav * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 516286574Smav */ 517286574Smav kstat_named_t arcstat_mfu_ghost_evictable_metadata; 518185029Spjd kstat_named_t arcstat_l2_hits; 519185029Spjd kstat_named_t arcstat_l2_misses; 520185029Spjd kstat_named_t arcstat_l2_feeds; 521185029Spjd kstat_named_t arcstat_l2_rw_clash; 522208373Smm kstat_named_t arcstat_l2_read_bytes; 523208373Smm kstat_named_t arcstat_l2_write_bytes; 524185029Spjd kstat_named_t arcstat_l2_writes_sent; 525185029Spjd kstat_named_t arcstat_l2_writes_done; 526185029Spjd kstat_named_t arcstat_l2_writes_error; 527185029Spjd kstat_named_t arcstat_l2_writes_hdr_miss; 528185029Spjd kstat_named_t arcstat_l2_evict_lock_retry; 529185029Spjd kstat_named_t arcstat_l2_evict_reading; 530286570Smav kstat_named_t arcstat_l2_evict_l1cached; 531185029Spjd kstat_named_t arcstat_l2_free_on_write; 532274172Savg kstat_named_t arcstat_l2_cdata_free_on_write; 533185029Spjd kstat_named_t arcstat_l2_abort_lowmem; 534185029Spjd kstat_named_t arcstat_l2_cksum_bad; 535185029Spjd kstat_named_t arcstat_l2_io_error; 536185029Spjd kstat_named_t arcstat_l2_size; 537251478Sdelphij kstat_named_t arcstat_l2_asize; 538185029Spjd kstat_named_t arcstat_l2_hdr_size; 539251478Sdelphij kstat_named_t arcstat_l2_compress_successes; 540251478Sdelphij kstat_named_t arcstat_l2_compress_zeros; 541251478Sdelphij kstat_named_t arcstat_l2_compress_failures; 542205231Skmacy kstat_named_t arcstat_l2_write_trylock_fail; 543205231Skmacy kstat_named_t arcstat_l2_write_passed_headroom; 544205231Skmacy kstat_named_t arcstat_l2_write_spa_mismatch; 545206796Spjd kstat_named_t arcstat_l2_write_in_l2; 546205231Skmacy kstat_named_t arcstat_l2_write_hdr_io_in_progress; 547205231Skmacy kstat_named_t arcstat_l2_write_not_cacheable; 548205231Skmacy kstat_named_t arcstat_l2_write_full; 549205231Skmacy kstat_named_t arcstat_l2_write_buffer_iter; 550205231Skmacy kstat_named_t arcstat_l2_write_pios; 551205231Skmacy kstat_named_t arcstat_l2_write_buffer_bytes_scanned; 552205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_iter; 553205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_null_iter; 554242845Sdelphij kstat_named_t arcstat_memory_throttle_count; 555242845Sdelphij kstat_named_t arcstat_duplicate_buffers; 556242845Sdelphij kstat_named_t arcstat_duplicate_buffers_size; 557242845Sdelphij kstat_named_t arcstat_duplicate_reads; 558275748Sdelphij kstat_named_t arcstat_meta_used; 559275748Sdelphij kstat_named_t arcstat_meta_limit; 560275748Sdelphij kstat_named_t arcstat_meta_max; 561275780Sdelphij kstat_named_t arcstat_meta_min; 562168404Spjd} arc_stats_t; 563168404Spjd 564168404Spjdstatic arc_stats_t arc_stats = { 565168404Spjd { "hits", KSTAT_DATA_UINT64 }, 566168404Spjd { "misses", KSTAT_DATA_UINT64 }, 567168404Spjd { "demand_data_hits", KSTAT_DATA_UINT64 }, 568168404Spjd { "demand_data_misses", KSTAT_DATA_UINT64 }, 569168404Spjd { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 570168404Spjd { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 571168404Spjd { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 572168404Spjd { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 573168404Spjd { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 574168404Spjd { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 575168404Spjd { "mru_hits", KSTAT_DATA_UINT64 }, 576168404Spjd { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 577168404Spjd { "mfu_hits", KSTAT_DATA_UINT64 }, 578168404Spjd { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 579205231Skmacy { "allocated", KSTAT_DATA_UINT64 }, 580168404Spjd { "deleted", KSTAT_DATA_UINT64 }, 581205231Skmacy { "stolen", KSTAT_DATA_UINT64 }, 582168404Spjd { "recycle_miss", KSTAT_DATA_UINT64 }, 583168404Spjd { "mutex_miss", KSTAT_DATA_UINT64 }, 584168404Spjd { "evict_skip", KSTAT_DATA_UINT64 }, 585208373Smm { "evict_l2_cached", KSTAT_DATA_UINT64 }, 586208373Smm { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 587208373Smm { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 588168404Spjd { "hash_elements", KSTAT_DATA_UINT64 }, 589168404Spjd { "hash_elements_max", KSTAT_DATA_UINT64 }, 590168404Spjd { "hash_collisions", KSTAT_DATA_UINT64 }, 591168404Spjd { "hash_chains", KSTAT_DATA_UINT64 }, 592168404Spjd { "hash_chain_max", KSTAT_DATA_UINT64 }, 593168404Spjd { "p", KSTAT_DATA_UINT64 }, 594168404Spjd { "c", KSTAT_DATA_UINT64 }, 595168404Spjd { "c_min", KSTAT_DATA_UINT64 }, 596168404Spjd { "c_max", KSTAT_DATA_UINT64 }, 597185029Spjd { "size", KSTAT_DATA_UINT64 }, 598185029Spjd { "hdr_size", KSTAT_DATA_UINT64 }, 599208373Smm { "data_size", KSTAT_DATA_UINT64 }, 600286574Smav { "metadata_size", KSTAT_DATA_UINT64 }, 601208373Smm { "other_size", KSTAT_DATA_UINT64 }, 602286574Smav { "anon_size", KSTAT_DATA_UINT64 }, 603286574Smav { "anon_evictable_data", KSTAT_DATA_UINT64 }, 604286574Smav { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, 605286574Smav { "mru_size", KSTAT_DATA_UINT64 }, 606286574Smav { "mru_evictable_data", KSTAT_DATA_UINT64 }, 607286574Smav { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, 608286574Smav { "mru_ghost_size", KSTAT_DATA_UINT64 }, 609286574Smav { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, 610286574Smav { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 611286574Smav { "mfu_size", KSTAT_DATA_UINT64 }, 612286574Smav { "mfu_evictable_data", KSTAT_DATA_UINT64 }, 613286574Smav { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, 614286574Smav { "mfu_ghost_size", KSTAT_DATA_UINT64 }, 615286574Smav { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, 616286574Smav { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 617185029Spjd { "l2_hits", KSTAT_DATA_UINT64 }, 618185029Spjd { "l2_misses", KSTAT_DATA_UINT64 }, 619185029Spjd { "l2_feeds", KSTAT_DATA_UINT64 }, 620185029Spjd { "l2_rw_clash", KSTAT_DATA_UINT64 }, 621208373Smm { "l2_read_bytes", KSTAT_DATA_UINT64 }, 622208373Smm { "l2_write_bytes", KSTAT_DATA_UINT64 }, 623185029Spjd { "l2_writes_sent", KSTAT_DATA_UINT64 }, 624185029Spjd { "l2_writes_done", KSTAT_DATA_UINT64 }, 625185029Spjd { "l2_writes_error", KSTAT_DATA_UINT64 }, 626185029Spjd { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 627185029Spjd { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 628185029Spjd { "l2_evict_reading", KSTAT_DATA_UINT64 }, 629286570Smav { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, 630185029Spjd { "l2_free_on_write", KSTAT_DATA_UINT64 }, 631274172Savg { "l2_cdata_free_on_write", KSTAT_DATA_UINT64 }, 632185029Spjd { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 633185029Spjd { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 634185029Spjd { "l2_io_error", KSTAT_DATA_UINT64 }, 635185029Spjd { "l2_size", KSTAT_DATA_UINT64 }, 636251478Sdelphij { "l2_asize", KSTAT_DATA_UINT64 }, 637185029Spjd { "l2_hdr_size", KSTAT_DATA_UINT64 }, 638251478Sdelphij { "l2_compress_successes", KSTAT_DATA_UINT64 }, 639251478Sdelphij { "l2_compress_zeros", KSTAT_DATA_UINT64 }, 640251478Sdelphij { "l2_compress_failures", KSTAT_DATA_UINT64 }, 641206796Spjd { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 642206796Spjd { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 643206796Spjd { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 644206796Spjd { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 645206796Spjd { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 646206796Spjd { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 647206796Spjd { "l2_write_full", KSTAT_DATA_UINT64 }, 648206796Spjd { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 649206796Spjd { "l2_write_pios", KSTAT_DATA_UINT64 }, 650206796Spjd { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 651206796Spjd { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 652242845Sdelphij { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 }, 653242845Sdelphij { "memory_throttle_count", KSTAT_DATA_UINT64 }, 654242845Sdelphij { "duplicate_buffers", KSTAT_DATA_UINT64 }, 655242845Sdelphij { "duplicate_buffers_size", KSTAT_DATA_UINT64 }, 656275748Sdelphij { "duplicate_reads", KSTAT_DATA_UINT64 }, 657275748Sdelphij { "arc_meta_used", KSTAT_DATA_UINT64 }, 658275748Sdelphij { "arc_meta_limit", KSTAT_DATA_UINT64 }, 659275780Sdelphij { "arc_meta_max", KSTAT_DATA_UINT64 }, 660275780Sdelphij { "arc_meta_min", KSTAT_DATA_UINT64 } 661168404Spjd}; 662168404Spjd 663168404Spjd#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 664168404Spjd 665168404Spjd#define ARCSTAT_INCR(stat, val) \ 666251631Sdelphij atomic_add_64(&arc_stats.stat.value.ui64, (val)) 667168404Spjd 668206796Spjd#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 669168404Spjd#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 670168404Spjd 671168404Spjd#define ARCSTAT_MAX(stat, val) { \ 672168404Spjd uint64_t m; \ 673168404Spjd while ((val) > (m = arc_stats.stat.value.ui64) && \ 674168404Spjd (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 675168404Spjd continue; \ 676168404Spjd} 677168404Spjd 678168404Spjd#define ARCSTAT_MAXSTAT(stat) \ 679168404Spjd ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 680168404Spjd 681168404Spjd/* 682168404Spjd * We define a macro to allow ARC hits/misses to be easily broken down by 683168404Spjd * two separate conditions, giving a total of four different subtypes for 684168404Spjd * each of hits and misses (so eight statistics total). 685168404Spjd */ 686168404Spjd#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 687168404Spjd if (cond1) { \ 688168404Spjd if (cond2) { \ 689168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 690168404Spjd } else { \ 691168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 692168404Spjd } \ 693168404Spjd } else { \ 694168404Spjd if (cond2) { \ 695168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 696168404Spjd } else { \ 697168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 698168404Spjd } \ 699168404Spjd } 700168404Spjd 701168404Spjdkstat_t *arc_ksp; 702206796Spjdstatic arc_state_t *arc_anon; 703168404Spjdstatic arc_state_t *arc_mru; 704168404Spjdstatic arc_state_t *arc_mru_ghost; 705168404Spjdstatic arc_state_t *arc_mfu; 706168404Spjdstatic arc_state_t *arc_mfu_ghost; 707185029Spjdstatic arc_state_t *arc_l2c_only; 708168404Spjd 709168404Spjd/* 710168404Spjd * There are several ARC variables that are critical to export as kstats -- 711168404Spjd * but we don't want to have to grovel around in the kstat whenever we wish to 712168404Spjd * manipulate them. For these variables, we therefore define them to be in 713168404Spjd * terms of the statistic variable. This assures that we are not introducing 714168404Spjd * the possibility of inconsistency by having shadow copies of the variables, 715168404Spjd * while still allowing the code to be readable. 716168404Spjd */ 717168404Spjd#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 718168404Spjd#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 719168404Spjd#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 720168404Spjd#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 721168404Spjd#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 722275748Sdelphij#define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ 723275780Sdelphij#define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ 724275748Sdelphij#define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */ 725275748Sdelphij#define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ 726168404Spjd 727251478Sdelphij#define L2ARC_IS_VALID_COMPRESS(_c_) \ 728251478Sdelphij ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY) 729251478Sdelphij 730168404Spjdstatic int arc_no_grow; /* Don't try to grow cache size */ 731168404Spjdstatic uint64_t arc_tempreserve; 732209962Smmstatic uint64_t arc_loaned_bytes; 733168404Spjd 734168404Spjdtypedef struct arc_callback arc_callback_t; 735168404Spjd 736168404Spjdstruct arc_callback { 737168404Spjd void *acb_private; 738168404Spjd arc_done_func_t *acb_done; 739168404Spjd arc_buf_t *acb_buf; 740168404Spjd zio_t *acb_zio_dummy; 741168404Spjd arc_callback_t *acb_next; 742168404Spjd}; 743168404Spjd 744168404Spjdtypedef struct arc_write_callback arc_write_callback_t; 745168404Spjd 746168404Spjdstruct arc_write_callback { 747168404Spjd void *awcb_private; 748168404Spjd arc_done_func_t *awcb_ready; 749258632Savg arc_done_func_t *awcb_physdone; 750168404Spjd arc_done_func_t *awcb_done; 751168404Spjd arc_buf_t *awcb_buf; 752168404Spjd}; 753168404Spjd 754286570Smav/* 755286570Smav * ARC buffers are separated into multiple structs as a memory saving measure: 756286570Smav * - Common fields struct, always defined, and embedded within it: 757286570Smav * - L2-only fields, always allocated but undefined when not in L2ARC 758286570Smav * - L1-only fields, only allocated when in L1ARC 759286570Smav * 760286570Smav * Buffer in L1 Buffer only in L2 761286570Smav * +------------------------+ +------------------------+ 762286570Smav * | arc_buf_hdr_t | | arc_buf_hdr_t | 763286570Smav * | | | | 764286570Smav * | | | | 765286570Smav * | | | | 766286570Smav * +------------------------+ +------------------------+ 767286570Smav * | l2arc_buf_hdr_t | | l2arc_buf_hdr_t | 768286570Smav * | (undefined if L1-only) | | | 769286570Smav * +------------------------+ +------------------------+ 770286570Smav * | l1arc_buf_hdr_t | 771286570Smav * | | 772286570Smav * | | 773286570Smav * | | 774286570Smav * | | 775286570Smav * +------------------------+ 776286570Smav * 777286570Smav * Because it's possible for the L2ARC to become extremely large, we can wind 778286570Smav * up eating a lot of memory in L2ARC buffer headers, so the size of a header 779286570Smav * is minimized by only allocating the fields necessary for an L1-cached buffer 780286570Smav * when a header is actually in the L1 cache. The sub-headers (l1arc_buf_hdr and 781286570Smav * l2arc_buf_hdr) are embedded rather than allocated separately to save a couple 782286570Smav * words in pointers. arc_hdr_realloc() is used to switch a header between 783286570Smav * these two allocation states. 784286570Smav */ 785286570Smavtypedef struct l1arc_buf_hdr { 786168404Spjd kmutex_t b_freeze_lock; 787286570Smav#ifdef ZFS_DEBUG 788286570Smav /* 789286570Smav * used for debugging wtih kmem_flags - by allocating and freeing 790286570Smav * b_thawed when the buffer is thawed, we get a record of the stack 791286570Smav * trace that thawed it. 792286570Smav */ 793219089Spjd void *b_thawed; 794286570Smav#endif 795168404Spjd 796168404Spjd arc_buf_t *b_buf; 797168404Spjd uint32_t b_datacnt; 798286570Smav /* for waiting on writes to complete */ 799168404Spjd kcondvar_t b_cv; 800168404Spjd 801168404Spjd /* protected by arc state mutex */ 802168404Spjd arc_state_t *b_state; 803168404Spjd list_node_t b_arc_node; 804168404Spjd 805168404Spjd /* updated atomically */ 806168404Spjd clock_t b_arc_access; 807168404Spjd 808168404Spjd /* self protecting */ 809168404Spjd refcount_t b_refcnt; 810185029Spjd 811286570Smav arc_callback_t *b_acb; 812286570Smav /* temporary buffer holder for in-flight compressed data */ 813286570Smav void *b_tmp_cdata; 814286570Smav} l1arc_buf_hdr_t; 815286570Smav 816286570Smavtypedef struct l2arc_dev l2arc_dev_t; 817286570Smav 818286570Smavtypedef struct l2arc_buf_hdr { 819286570Smav /* protected by arc_buf_hdr mutex */ 820286570Smav l2arc_dev_t *b_dev; /* L2ARC device */ 821286570Smav uint64_t b_daddr; /* disk address, offset byte */ 822286570Smav /* real alloc'd buffer size depending on b_compress applied */ 823286570Smav int32_t b_asize; 824286570Smav 825185029Spjd list_node_t b_l2node; 826286570Smav} l2arc_buf_hdr_t; 827286570Smav 828286570Smavstruct arc_buf_hdr { 829286570Smav /* protected by hash lock */ 830286570Smav dva_t b_dva; 831286570Smav uint64_t b_birth; 832286570Smav /* 833286570Smav * Even though this checksum is only set/verified when a buffer is in 834286570Smav * the L1 cache, it needs to be in the set of common fields because it 835286570Smav * must be preserved from the time before a buffer is written out to 836286570Smav * L2ARC until after it is read back in. 837286570Smav */ 838286570Smav zio_cksum_t *b_freeze_cksum; 839286570Smav 840286570Smav arc_buf_hdr_t *b_hash_next; 841286570Smav arc_flags_t b_flags; 842286570Smav 843286570Smav /* immutable */ 844286570Smav int32_t b_size; 845286570Smav uint64_t b_spa; 846286570Smav 847286570Smav /* L2ARC fields. Undefined when not in L2ARC. */ 848286570Smav l2arc_buf_hdr_t b_l2hdr; 849286570Smav /* L1ARC fields. Undefined when in l2arc_only state */ 850286570Smav l1arc_buf_hdr_t b_l1hdr; 851168404Spjd}; 852168404Spjd 853275748Sdelphij#ifdef _KERNEL 854275748Sdelphijstatic int 855275748Sdelphijsysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS) 856275748Sdelphij{ 857275748Sdelphij uint64_t val; 858275748Sdelphij int err; 859275748Sdelphij 860275748Sdelphij val = arc_meta_limit; 861275748Sdelphij err = sysctl_handle_64(oidp, &val, 0, req); 862275748Sdelphij if (err != 0 || req->newptr == NULL) 863275748Sdelphij return (err); 864275748Sdelphij 865275748Sdelphij if (val <= 0 || val > arc_c_max) 866275748Sdelphij return (EINVAL); 867275748Sdelphij 868275748Sdelphij arc_meta_limit = val; 869275748Sdelphij return (0); 870275748Sdelphij} 871275748Sdelphij#endif 872275748Sdelphij 873168404Spjdstatic arc_buf_t *arc_eviction_list; 874168404Spjdstatic kmutex_t arc_eviction_mtx; 875168404Spjdstatic arc_buf_hdr_t arc_eviction_hdr; 876168404Spjd 877168404Spjd#define GHOST_STATE(state) \ 878185029Spjd ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 879185029Spjd (state) == arc_l2c_only) 880168404Spjd 881275811Sdelphij#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) 882275811Sdelphij#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) 883275811Sdelphij#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) 884275811Sdelphij#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) 885275811Sdelphij#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FLAG_FREED_IN_READ) 886275811Sdelphij#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_FLAG_BUF_AVAILABLE) 887286570Smav 888275811Sdelphij#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) 889286570Smav#define HDR_L2COMPRESS(hdr) ((hdr)->b_flags & ARC_FLAG_L2COMPRESS) 890275811Sdelphij#define HDR_L2_READING(hdr) \ 891286570Smav (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ 892286570Smav ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) 893275811Sdelphij#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) 894275811Sdelphij#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) 895275811Sdelphij#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) 896168404Spjd 897286570Smav#define HDR_ISTYPE_METADATA(hdr) \ 898286570Smav ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) 899286570Smav#define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) 900286570Smav 901286570Smav#define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) 902286570Smav#define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) 903286570Smav 904286570Smav/* For storing compression mode in b_flags */ 905286570Smav#define HDR_COMPRESS_OFFSET 24 906286570Smav#define HDR_COMPRESS_NBITS 7 907286570Smav 908286570Smav#define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET(hdr->b_flags, \ 909286570Smav HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS)) 910286570Smav#define HDR_SET_COMPRESS(hdr, cmp) BF32_SET(hdr->b_flags, \ 911286570Smav HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS, (cmp)) 912286570Smav 913168404Spjd/* 914185029Spjd * Other sizes 915185029Spjd */ 916185029Spjd 917286570Smav#define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 918286570Smav#define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) 919185029Spjd 920185029Spjd/* 921168404Spjd * Hash table routines 922168404Spjd */ 923168404Spjd 924205253Skmacy#define HT_LOCK_PAD CACHE_LINE_SIZE 925168404Spjd 926168404Spjdstruct ht_lock { 927168404Spjd kmutex_t ht_lock; 928168404Spjd#ifdef _KERNEL 929168404Spjd unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 930168404Spjd#endif 931168404Spjd}; 932168404Spjd 933168404Spjd#define BUF_LOCKS 256 934168404Spjdtypedef struct buf_hash_table { 935168404Spjd uint64_t ht_mask; 936168404Spjd arc_buf_hdr_t **ht_table; 937205264Skmacy struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 938168404Spjd} buf_hash_table_t; 939168404Spjd 940168404Spjdstatic buf_hash_table_t buf_hash_table; 941168404Spjd 942168404Spjd#define BUF_HASH_INDEX(spa, dva, birth) \ 943168404Spjd (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 944168404Spjd#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 945168404Spjd#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 946219089Spjd#define HDR_LOCK(hdr) \ 947219089Spjd (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 948168404Spjd 949168404Spjduint64_t zfs_crc64_table[256]; 950168404Spjd 951185029Spjd/* 952185029Spjd * Level 2 ARC 953185029Spjd */ 954185029Spjd 955272707Savg#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 956251478Sdelphij#define L2ARC_HEADROOM 2 /* num of writes */ 957251478Sdelphij/* 958251478Sdelphij * If we discover during ARC scan any buffers to be compressed, we boost 959251478Sdelphij * our headroom for the next scanning cycle by this percentage multiple. 960251478Sdelphij */ 961251478Sdelphij#define L2ARC_HEADROOM_BOOST 200 962208373Smm#define L2ARC_FEED_SECS 1 /* caching interval secs */ 963208373Smm#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 964185029Spjd 965286598Smav/* 966286598Smav * Used to distinguish headers that are being process by 967286598Smav * l2arc_write_buffers(), but have yet to be assigned to a l2arc disk 968286598Smav * address. This can happen when the header is added to the l2arc's list 969286598Smav * of buffers to write in the first stage of l2arc_write_buffers(), but 970286598Smav * has not yet been written out which happens in the second stage of 971286598Smav * l2arc_write_buffers(). 972286598Smav */ 973286598Smav#define L2ARC_ADDR_UNSET ((uint64_t)(-1)) 974286598Smav 975185029Spjd#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 976185029Spjd#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 977185029Spjd 978251631Sdelphij/* L2ARC Performance Tunables */ 979185029Spjduint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 980185029Spjduint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 981185029Spjduint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 982251478Sdelphijuint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 983185029Spjduint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 984208373Smmuint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 985219089Spjdboolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 986208373Smmboolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 987208373Smmboolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 988185029Spjd 989217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 990205231Skmacy &l2arc_write_max, 0, "max write size"); 991217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 992205231Skmacy &l2arc_write_boost, 0, "extra write during warmup"); 993217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 994205231Skmacy &l2arc_headroom, 0, "number of dev writes"); 995217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 996205231Skmacy &l2arc_feed_secs, 0, "interval seconds"); 997217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 998208373Smm &l2arc_feed_min_ms, 0, "min interval milliseconds"); 999205231Skmacy 1000205231SkmacySYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 1001205231Skmacy &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 1002208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 1003208373Smm &l2arc_feed_again, 0, "turbo warmup"); 1004208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 1005208373Smm &l2arc_norw, 0, "no reads during writes"); 1006205231Skmacy 1007217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 1008205231Skmacy &ARC_anon.arcs_size, 0, "size of anonymous state"); 1009217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD, 1010205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state"); 1011217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD, 1012205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state"); 1013205231Skmacy 1014217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 1015205231Skmacy &ARC_mru.arcs_size, 0, "size of mru state"); 1016217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD, 1017205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state"); 1018217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD, 1019205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state"); 1020205231Skmacy 1021217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 1022205231Skmacy &ARC_mru_ghost.arcs_size, 0, "size of mru ghost state"); 1023217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD, 1024205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 1025205231Skmacy "size of metadata in mru ghost state"); 1026217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD, 1027205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 1028205231Skmacy "size of data in mru ghost state"); 1029205231Skmacy 1030217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 1031205231Skmacy &ARC_mfu.arcs_size, 0, "size of mfu state"); 1032217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD, 1033205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state"); 1034217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD, 1035205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state"); 1036205231Skmacy 1037217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 1038205231Skmacy &ARC_mfu_ghost.arcs_size, 0, "size of mfu ghost state"); 1039217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD, 1040205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 1041205231Skmacy "size of metadata in mfu ghost state"); 1042217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD, 1043205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 1044205231Skmacy "size of data in mfu ghost state"); 1045205231Skmacy 1046217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 1047205231Skmacy &ARC_l2c_only.arcs_size, 0, "size of mru state"); 1048205231Skmacy 1049185029Spjd/* 1050185029Spjd * L2ARC Internals 1051185029Spjd */ 1052286570Smavstruct l2arc_dev { 1053185029Spjd vdev_t *l2ad_vdev; /* vdev */ 1054185029Spjd spa_t *l2ad_spa; /* spa */ 1055185029Spjd uint64_t l2ad_hand; /* next write location */ 1056185029Spjd uint64_t l2ad_start; /* first addr on device */ 1057185029Spjd uint64_t l2ad_end; /* last addr on device */ 1058185029Spjd boolean_t l2ad_first; /* first sweep through */ 1059208373Smm boolean_t l2ad_writing; /* currently writing */ 1060286570Smav kmutex_t l2ad_mtx; /* lock for buffer list */ 1061286570Smav list_t l2ad_buflist; /* buffer list */ 1062185029Spjd list_node_t l2ad_node; /* device list node */ 1063286598Smav refcount_t l2ad_alloc; /* allocated bytes */ 1064286570Smav}; 1065185029Spjd 1066185029Spjdstatic list_t L2ARC_dev_list; /* device list */ 1067185029Spjdstatic list_t *l2arc_dev_list; /* device list pointer */ 1068185029Spjdstatic kmutex_t l2arc_dev_mtx; /* device list mutex */ 1069185029Spjdstatic l2arc_dev_t *l2arc_dev_last; /* last device used */ 1070185029Spjdstatic list_t L2ARC_free_on_write; /* free after write buf list */ 1071185029Spjdstatic list_t *l2arc_free_on_write; /* free after write list ptr */ 1072185029Spjdstatic kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 1073185029Spjdstatic uint64_t l2arc_ndev; /* number of devices */ 1074185029Spjd 1075185029Spjdtypedef struct l2arc_read_callback { 1076251478Sdelphij arc_buf_t *l2rcb_buf; /* read buffer */ 1077251478Sdelphij spa_t *l2rcb_spa; /* spa */ 1078251478Sdelphij blkptr_t l2rcb_bp; /* original blkptr */ 1079268123Sdelphij zbookmark_phys_t l2rcb_zb; /* original bookmark */ 1080251478Sdelphij int l2rcb_flags; /* original flags */ 1081251478Sdelphij enum zio_compress l2rcb_compress; /* applied compress */ 1082185029Spjd} l2arc_read_callback_t; 1083185029Spjd 1084185029Spjdtypedef struct l2arc_write_callback { 1085185029Spjd l2arc_dev_t *l2wcb_dev; /* device info */ 1086185029Spjd arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 1087185029Spjd} l2arc_write_callback_t; 1088185029Spjd 1089185029Spjdtypedef struct l2arc_data_free { 1090185029Spjd /* protected by l2arc_free_on_write_mtx */ 1091185029Spjd void *l2df_data; 1092185029Spjd size_t l2df_size; 1093185029Spjd void (*l2df_func)(void *, size_t); 1094185029Spjd list_node_t l2df_list_node; 1095185029Spjd} l2arc_data_free_t; 1096185029Spjd 1097185029Spjdstatic kmutex_t l2arc_feed_thr_lock; 1098185029Spjdstatic kcondvar_t l2arc_feed_thr_cv; 1099185029Spjdstatic uint8_t l2arc_thread_exit; 1100185029Spjd 1101275811Sdelphijstatic void arc_get_data_buf(arc_buf_t *); 1102275811Sdelphijstatic void arc_access(arc_buf_hdr_t *, kmutex_t *); 1103275811Sdelphijstatic int arc_evict_needed(arc_buf_contents_t); 1104275811Sdelphijstatic void arc_evict_ghost(arc_state_t *, uint64_t, int64_t); 1105275811Sdelphijstatic void arc_buf_watch(arc_buf_t *); 1106275811Sdelphij 1107286570Smavstatic arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); 1108286570Smavstatic uint32_t arc_bufc_to_flags(arc_buf_contents_t); 1109286570Smav 1110275811Sdelphijstatic boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); 1111275811Sdelphijstatic void l2arc_read_done(zio_t *); 1112185029Spjd 1113286570Smavstatic boolean_t l2arc_compress_buf(arc_buf_hdr_t *); 1114275811Sdelphijstatic void l2arc_decompress_zio(zio_t *, arc_buf_hdr_t *, enum zio_compress); 1115275811Sdelphijstatic void l2arc_release_cdata_buf(arc_buf_hdr_t *); 1116251478Sdelphij 1117168404Spjdstatic uint64_t 1118209962Smmbuf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 1119168404Spjd{ 1120168404Spjd uint8_t *vdva = (uint8_t *)dva; 1121168404Spjd uint64_t crc = -1ULL; 1122168404Spjd int i; 1123168404Spjd 1124168404Spjd ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 1125168404Spjd 1126168404Spjd for (i = 0; i < sizeof (dva_t); i++) 1127168404Spjd crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 1128168404Spjd 1129209962Smm crc ^= (spa>>8) ^ birth; 1130168404Spjd 1131168404Spjd return (crc); 1132168404Spjd} 1133168404Spjd 1134168404Spjd#define BUF_EMPTY(buf) \ 1135168404Spjd ((buf)->b_dva.dva_word[0] == 0 && \ 1136286570Smav (buf)->b_dva.dva_word[1] == 0) 1137168404Spjd 1138168404Spjd#define BUF_EQUAL(spa, dva, birth, buf) \ 1139168404Spjd ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 1140168404Spjd ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 1141168404Spjd ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 1142168404Spjd 1143219089Spjdstatic void 1144219089Spjdbuf_discard_identity(arc_buf_hdr_t *hdr) 1145219089Spjd{ 1146219089Spjd hdr->b_dva.dva_word[0] = 0; 1147219089Spjd hdr->b_dva.dva_word[1] = 0; 1148219089Spjd hdr->b_birth = 0; 1149219089Spjd} 1150219089Spjd 1151168404Spjdstatic arc_buf_hdr_t * 1152268075Sdelphijbuf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 1153168404Spjd{ 1154268075Sdelphij const dva_t *dva = BP_IDENTITY(bp); 1155268075Sdelphij uint64_t birth = BP_PHYSICAL_BIRTH(bp); 1156168404Spjd uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 1157168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1158275811Sdelphij arc_buf_hdr_t *hdr; 1159168404Spjd 1160168404Spjd mutex_enter(hash_lock); 1161275811Sdelphij for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; 1162275811Sdelphij hdr = hdr->b_hash_next) { 1163275811Sdelphij if (BUF_EQUAL(spa, dva, birth, hdr)) { 1164168404Spjd *lockp = hash_lock; 1165275811Sdelphij return (hdr); 1166168404Spjd } 1167168404Spjd } 1168168404Spjd mutex_exit(hash_lock); 1169168404Spjd *lockp = NULL; 1170168404Spjd return (NULL); 1171168404Spjd} 1172168404Spjd 1173168404Spjd/* 1174168404Spjd * Insert an entry into the hash table. If there is already an element 1175168404Spjd * equal to elem in the hash table, then the already existing element 1176168404Spjd * will be returned and the new element will not be inserted. 1177168404Spjd * Otherwise returns NULL. 1178286570Smav * If lockp == NULL, the caller is assumed to already hold the hash lock. 1179168404Spjd */ 1180168404Spjdstatic arc_buf_hdr_t * 1181275811Sdelphijbuf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) 1182168404Spjd{ 1183275811Sdelphij uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1184168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1185275811Sdelphij arc_buf_hdr_t *fhdr; 1186168404Spjd uint32_t i; 1187168404Spjd 1188275811Sdelphij ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); 1189275811Sdelphij ASSERT(hdr->b_birth != 0); 1190275811Sdelphij ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1191286570Smav 1192286570Smav if (lockp != NULL) { 1193286570Smav *lockp = hash_lock; 1194286570Smav mutex_enter(hash_lock); 1195286570Smav } else { 1196286570Smav ASSERT(MUTEX_HELD(hash_lock)); 1197286570Smav } 1198286570Smav 1199275811Sdelphij for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; 1200275811Sdelphij fhdr = fhdr->b_hash_next, i++) { 1201275811Sdelphij if (BUF_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) 1202275811Sdelphij return (fhdr); 1203168404Spjd } 1204168404Spjd 1205275811Sdelphij hdr->b_hash_next = buf_hash_table.ht_table[idx]; 1206275811Sdelphij buf_hash_table.ht_table[idx] = hdr; 1207275811Sdelphij hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; 1208168404Spjd 1209168404Spjd /* collect some hash table performance data */ 1210168404Spjd if (i > 0) { 1211168404Spjd ARCSTAT_BUMP(arcstat_hash_collisions); 1212168404Spjd if (i == 1) 1213168404Spjd ARCSTAT_BUMP(arcstat_hash_chains); 1214168404Spjd 1215168404Spjd ARCSTAT_MAX(arcstat_hash_chain_max, i); 1216168404Spjd } 1217168404Spjd 1218168404Spjd ARCSTAT_BUMP(arcstat_hash_elements); 1219168404Spjd ARCSTAT_MAXSTAT(arcstat_hash_elements); 1220168404Spjd 1221168404Spjd return (NULL); 1222168404Spjd} 1223168404Spjd 1224168404Spjdstatic void 1225275811Sdelphijbuf_hash_remove(arc_buf_hdr_t *hdr) 1226168404Spjd{ 1227275811Sdelphij arc_buf_hdr_t *fhdr, **hdrp; 1228275811Sdelphij uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1229168404Spjd 1230168404Spjd ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 1231275811Sdelphij ASSERT(HDR_IN_HASH_TABLE(hdr)); 1232168404Spjd 1233275811Sdelphij hdrp = &buf_hash_table.ht_table[idx]; 1234275811Sdelphij while ((fhdr = *hdrp) != hdr) { 1235275811Sdelphij ASSERT(fhdr != NULL); 1236275811Sdelphij hdrp = &fhdr->b_hash_next; 1237168404Spjd } 1238275811Sdelphij *hdrp = hdr->b_hash_next; 1239275811Sdelphij hdr->b_hash_next = NULL; 1240275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IN_HASH_TABLE; 1241168404Spjd 1242168404Spjd /* collect some hash table performance data */ 1243168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_elements); 1244168404Spjd 1245168404Spjd if (buf_hash_table.ht_table[idx] && 1246168404Spjd buf_hash_table.ht_table[idx]->b_hash_next == NULL) 1247168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1248168404Spjd} 1249168404Spjd 1250168404Spjd/* 1251168404Spjd * Global data structures and functions for the buf kmem cache. 1252168404Spjd */ 1253286570Smavstatic kmem_cache_t *hdr_full_cache; 1254286570Smavstatic kmem_cache_t *hdr_l2only_cache; 1255168404Spjdstatic kmem_cache_t *buf_cache; 1256168404Spjd 1257168404Spjdstatic void 1258168404Spjdbuf_fini(void) 1259168404Spjd{ 1260168404Spjd int i; 1261168404Spjd 1262168404Spjd kmem_free(buf_hash_table.ht_table, 1263168404Spjd (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1264168404Spjd for (i = 0; i < BUF_LOCKS; i++) 1265168404Spjd mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 1266286570Smav kmem_cache_destroy(hdr_full_cache); 1267286570Smav kmem_cache_destroy(hdr_l2only_cache); 1268168404Spjd kmem_cache_destroy(buf_cache); 1269168404Spjd} 1270168404Spjd 1271168404Spjd/* 1272168404Spjd * Constructor callback - called when the cache is empty 1273168404Spjd * and a new buf is requested. 1274168404Spjd */ 1275168404Spjd/* ARGSUSED */ 1276168404Spjdstatic int 1277286570Smavhdr_full_cons(void *vbuf, void *unused, int kmflag) 1278168404Spjd{ 1279275811Sdelphij arc_buf_hdr_t *hdr = vbuf; 1280168404Spjd 1281286570Smav bzero(hdr, HDR_FULL_SIZE); 1282286570Smav cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); 1283286570Smav refcount_create(&hdr->b_l1hdr.b_refcnt); 1284286570Smav mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1285286570Smav arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1286185029Spjd 1287168404Spjd return (0); 1288168404Spjd} 1289168404Spjd 1290185029Spjd/* ARGSUSED */ 1291185029Spjdstatic int 1292286570Smavhdr_l2only_cons(void *vbuf, void *unused, int kmflag) 1293286570Smav{ 1294286570Smav arc_buf_hdr_t *hdr = vbuf; 1295286570Smav 1296286570Smav bzero(hdr, HDR_L2ONLY_SIZE); 1297286570Smav arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1298286570Smav 1299286570Smav return (0); 1300286570Smav} 1301286570Smav 1302286570Smav/* ARGSUSED */ 1303286570Smavstatic int 1304185029Spjdbuf_cons(void *vbuf, void *unused, int kmflag) 1305185029Spjd{ 1306185029Spjd arc_buf_t *buf = vbuf; 1307185029Spjd 1308185029Spjd bzero(buf, sizeof (arc_buf_t)); 1309219089Spjd mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 1310208373Smm arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1311208373Smm 1312185029Spjd return (0); 1313185029Spjd} 1314185029Spjd 1315168404Spjd/* 1316168404Spjd * Destructor callback - called when a cached buf is 1317168404Spjd * no longer required. 1318168404Spjd */ 1319168404Spjd/* ARGSUSED */ 1320168404Spjdstatic void 1321286570Smavhdr_full_dest(void *vbuf, void *unused) 1322168404Spjd{ 1323275811Sdelphij arc_buf_hdr_t *hdr = vbuf; 1324168404Spjd 1325275811Sdelphij ASSERT(BUF_EMPTY(hdr)); 1326286570Smav cv_destroy(&hdr->b_l1hdr.b_cv); 1327286570Smav refcount_destroy(&hdr->b_l1hdr.b_refcnt); 1328286570Smav mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); 1329286570Smav arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1330168404Spjd} 1331168404Spjd 1332185029Spjd/* ARGSUSED */ 1333185029Spjdstatic void 1334286570Smavhdr_l2only_dest(void *vbuf, void *unused) 1335286570Smav{ 1336286570Smav arc_buf_hdr_t *hdr = vbuf; 1337286570Smav 1338286570Smav ASSERT(BUF_EMPTY(hdr)); 1339286570Smav arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1340286570Smav} 1341286570Smav 1342286570Smav/* ARGSUSED */ 1343286570Smavstatic void 1344185029Spjdbuf_dest(void *vbuf, void *unused) 1345185029Spjd{ 1346185029Spjd arc_buf_t *buf = vbuf; 1347185029Spjd 1348219089Spjd mutex_destroy(&buf->b_evict_lock); 1349208373Smm arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1350185029Spjd} 1351185029Spjd 1352168404Spjd/* 1353168404Spjd * Reclaim callback -- invoked when memory is low. 1354168404Spjd */ 1355168404Spjd/* ARGSUSED */ 1356168404Spjdstatic void 1357168404Spjdhdr_recl(void *unused) 1358168404Spjd{ 1359168404Spjd dprintf("hdr_recl called\n"); 1360168404Spjd /* 1361168404Spjd * umem calls the reclaim func when we destroy the buf cache, 1362168404Spjd * which is after we do arc_fini(). 1363168404Spjd */ 1364168404Spjd if (!arc_dead) 1365168404Spjd cv_signal(&arc_reclaim_thr_cv); 1366168404Spjd} 1367168404Spjd 1368168404Spjdstatic void 1369168404Spjdbuf_init(void) 1370168404Spjd{ 1371168404Spjd uint64_t *ct; 1372168404Spjd uint64_t hsize = 1ULL << 12; 1373168404Spjd int i, j; 1374168404Spjd 1375168404Spjd /* 1376168404Spjd * The hash table is big enough to fill all of physical memory 1377269230Sdelphij * with an average block size of zfs_arc_average_blocksize (default 8K). 1378269230Sdelphij * By default, the table will take up 1379269230Sdelphij * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1380168404Spjd */ 1381269230Sdelphij while (hsize * zfs_arc_average_blocksize < (uint64_t)physmem * PAGESIZE) 1382168404Spjd hsize <<= 1; 1383168404Spjdretry: 1384168404Spjd buf_hash_table.ht_mask = hsize - 1; 1385168404Spjd buf_hash_table.ht_table = 1386168404Spjd kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1387168404Spjd if (buf_hash_table.ht_table == NULL) { 1388168404Spjd ASSERT(hsize > (1ULL << 8)); 1389168404Spjd hsize >>= 1; 1390168404Spjd goto retry; 1391168404Spjd } 1392168404Spjd 1393286570Smav hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, 1394286570Smav 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0); 1395286570Smav hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", 1396286570Smav HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl, 1397286570Smav NULL, NULL, 0); 1398168404Spjd buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1399185029Spjd 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1400168404Spjd 1401168404Spjd for (i = 0; i < 256; i++) 1402168404Spjd for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1403168404Spjd *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1404168404Spjd 1405168404Spjd for (i = 0; i < BUF_LOCKS; i++) { 1406168404Spjd mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1407168404Spjd NULL, MUTEX_DEFAULT, NULL); 1408168404Spjd } 1409168404Spjd} 1410168404Spjd 1411286570Smav/* 1412286570Smav * Transition between the two allocation states for the arc_buf_hdr struct. 1413286570Smav * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without 1414286570Smav * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller 1415286570Smav * version is used when a cache buffer is only in the L2ARC in order to reduce 1416286570Smav * memory usage. 1417286570Smav */ 1418286570Smavstatic arc_buf_hdr_t * 1419286570Smavarc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) 1420286570Smav{ 1421286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 1422286570Smav 1423286570Smav arc_buf_hdr_t *nhdr; 1424286570Smav l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 1425286570Smav 1426286570Smav ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || 1427286570Smav (old == hdr_l2only_cache && new == hdr_full_cache)); 1428286570Smav 1429286570Smav nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); 1430286570Smav 1431286570Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 1432286570Smav buf_hash_remove(hdr); 1433286570Smav 1434286570Smav bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); 1435286598Smav 1436286570Smav if (new == hdr_full_cache) { 1437286570Smav nhdr->b_flags |= ARC_FLAG_HAS_L1HDR; 1438286570Smav /* 1439286570Smav * arc_access and arc_change_state need to be aware that a 1440286570Smav * header has just come out of L2ARC, so we set its state to 1441286570Smav * l2c_only even though it's about to change. 1442286570Smav */ 1443286570Smav nhdr->b_l1hdr.b_state = arc_l2c_only; 1444286570Smav } else { 1445286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 1446286570Smav ASSERT0(hdr->b_l1hdr.b_datacnt); 1447286570Smav ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 1448286570Smav /* 1449286570Smav * We might be removing the L1hdr of a buffer which was just 1450286570Smav * written out to L2ARC. If such a buffer is compressed then we 1451286570Smav * need to free its b_tmp_cdata before destroying the header. 1452286570Smav */ 1453286570Smav if (hdr->b_l1hdr.b_tmp_cdata != NULL && 1454286570Smav HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) 1455286570Smav l2arc_release_cdata_buf(hdr); 1456286570Smav nhdr->b_flags &= ~ARC_FLAG_HAS_L1HDR; 1457286570Smav } 1458286570Smav /* 1459286570Smav * The header has been reallocated so we need to re-insert it into any 1460286570Smav * lists it was on. 1461286570Smav */ 1462286570Smav (void) buf_hash_insert(nhdr, NULL); 1463286570Smav 1464286570Smav ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); 1465286570Smav 1466286570Smav mutex_enter(&dev->l2ad_mtx); 1467286570Smav 1468286570Smav /* 1469286570Smav * We must place the realloc'ed header back into the list at 1470286570Smav * the same spot. Otherwise, if it's placed earlier in the list, 1471286570Smav * l2arc_write_buffers() could find it during the function's 1472286570Smav * write phase, and try to write it out to the l2arc. 1473286570Smav */ 1474286570Smav list_insert_after(&dev->l2ad_buflist, hdr, nhdr); 1475286570Smav list_remove(&dev->l2ad_buflist, hdr); 1476286570Smav 1477286570Smav mutex_exit(&dev->l2ad_mtx); 1478286570Smav 1479286598Smav /* 1480286598Smav * Since we're using the pointer address as the tag when 1481286598Smav * incrementing and decrementing the l2ad_alloc refcount, we 1482286598Smav * must remove the old pointer (that we're about to destroy) and 1483286598Smav * add the new pointer to the refcount. Otherwise we'd remove 1484286598Smav * the wrong pointer address when calling arc_hdr_destroy() later. 1485286598Smav */ 1486286598Smav 1487286598Smav (void) refcount_remove_many(&dev->l2ad_alloc, 1488286598Smav hdr->b_l2hdr.b_asize, hdr); 1489286598Smav 1490286598Smav (void) refcount_add_many(&dev->l2ad_alloc, 1491286598Smav nhdr->b_l2hdr.b_asize, nhdr); 1492286598Smav 1493286570Smav buf_discard_identity(hdr); 1494286570Smav hdr->b_freeze_cksum = NULL; 1495286570Smav kmem_cache_free(old, hdr); 1496286570Smav 1497286570Smav return (nhdr); 1498286570Smav} 1499286570Smav 1500286570Smav 1501168404Spjd#define ARC_MINTIME (hz>>4) /* 62 ms */ 1502168404Spjd 1503168404Spjdstatic void 1504168404Spjdarc_cksum_verify(arc_buf_t *buf) 1505168404Spjd{ 1506168404Spjd zio_cksum_t zc; 1507168404Spjd 1508168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1509168404Spjd return; 1510168404Spjd 1511286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1512286570Smav if (buf->b_hdr->b_freeze_cksum == NULL || HDR_IO_ERROR(buf->b_hdr)) { 1513286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1514168404Spjd return; 1515168404Spjd } 1516168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1517168404Spjd if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 1518168404Spjd panic("buffer modified while frozen!"); 1519286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1520168404Spjd} 1521168404Spjd 1522185029Spjdstatic int 1523185029Spjdarc_cksum_equal(arc_buf_t *buf) 1524185029Spjd{ 1525185029Spjd zio_cksum_t zc; 1526185029Spjd int equal; 1527185029Spjd 1528286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1529185029Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1530185029Spjd equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 1531286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1532185029Spjd 1533185029Spjd return (equal); 1534185029Spjd} 1535185029Spjd 1536168404Spjdstatic void 1537185029Spjdarc_cksum_compute(arc_buf_t *buf, boolean_t force) 1538168404Spjd{ 1539185029Spjd if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 1540168404Spjd return; 1541168404Spjd 1542286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1543168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1544286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1545168404Spjd return; 1546168404Spjd } 1547168404Spjd buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 1548168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 1549168404Spjd buf->b_hdr->b_freeze_cksum); 1550286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1551240133Smm#ifdef illumos 1552240133Smm arc_buf_watch(buf); 1553277300Ssmh#endif 1554168404Spjd} 1555168404Spjd 1556240133Smm#ifdef illumos 1557240133Smm#ifndef _KERNEL 1558240133Smmtypedef struct procctl { 1559240133Smm long cmd; 1560240133Smm prwatch_t prwatch; 1561240133Smm} procctl_t; 1562240133Smm#endif 1563240133Smm 1564240133Smm/* ARGSUSED */ 1565240133Smmstatic void 1566240133Smmarc_buf_unwatch(arc_buf_t *buf) 1567240133Smm{ 1568240133Smm#ifndef _KERNEL 1569240133Smm if (arc_watch) { 1570240133Smm int result; 1571240133Smm procctl_t ctl; 1572240133Smm ctl.cmd = PCWATCH; 1573240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1574240133Smm ctl.prwatch.pr_size = 0; 1575240133Smm ctl.prwatch.pr_wflags = 0; 1576240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1577240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1578240133Smm } 1579240133Smm#endif 1580240133Smm} 1581240133Smm 1582240133Smm/* ARGSUSED */ 1583240133Smmstatic void 1584240133Smmarc_buf_watch(arc_buf_t *buf) 1585240133Smm{ 1586240133Smm#ifndef _KERNEL 1587240133Smm if (arc_watch) { 1588240133Smm int result; 1589240133Smm procctl_t ctl; 1590240133Smm ctl.cmd = PCWATCH; 1591240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1592240133Smm ctl.prwatch.pr_size = buf->b_hdr->b_size; 1593240133Smm ctl.prwatch.pr_wflags = WA_WRITE; 1594240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1595240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1596240133Smm } 1597240133Smm#endif 1598240133Smm} 1599240133Smm#endif /* illumos */ 1600240133Smm 1601286570Smavstatic arc_buf_contents_t 1602286570Smavarc_buf_type(arc_buf_hdr_t *hdr) 1603286570Smav{ 1604286570Smav if (HDR_ISTYPE_METADATA(hdr)) { 1605286570Smav return (ARC_BUFC_METADATA); 1606286570Smav } else { 1607286570Smav return (ARC_BUFC_DATA); 1608286570Smav } 1609286570Smav} 1610286570Smav 1611286570Smavstatic uint32_t 1612286570Smavarc_bufc_to_flags(arc_buf_contents_t type) 1613286570Smav{ 1614286570Smav switch (type) { 1615286570Smav case ARC_BUFC_DATA: 1616286570Smav /* metadata field is 0 if buffer contains normal data */ 1617286570Smav return (0); 1618286570Smav case ARC_BUFC_METADATA: 1619286570Smav return (ARC_FLAG_BUFC_METADATA); 1620286570Smav default: 1621286570Smav break; 1622286570Smav } 1623286570Smav panic("undefined ARC buffer type!"); 1624286570Smav return ((uint32_t)-1); 1625286570Smav} 1626286570Smav 1627168404Spjdvoid 1628168404Spjdarc_buf_thaw(arc_buf_t *buf) 1629168404Spjd{ 1630185029Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1631286570Smav if (buf->b_hdr->b_l1hdr.b_state != arc_anon) 1632185029Spjd panic("modifying non-anon buffer!"); 1633286570Smav if (HDR_IO_IN_PROGRESS(buf->b_hdr)) 1634185029Spjd panic("modifying buffer while i/o in progress!"); 1635185029Spjd arc_cksum_verify(buf); 1636185029Spjd } 1637168404Spjd 1638286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1639168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1640168404Spjd kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1641168404Spjd buf->b_hdr->b_freeze_cksum = NULL; 1642168404Spjd } 1643219089Spjd 1644286570Smav#ifdef ZFS_DEBUG 1645219089Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1646286570Smav if (buf->b_hdr->b_l1hdr.b_thawed != NULL) 1647286570Smav kmem_free(buf->b_hdr->b_l1hdr.b_thawed, 1); 1648286570Smav buf->b_hdr->b_l1hdr.b_thawed = kmem_alloc(1, KM_SLEEP); 1649219089Spjd } 1650286570Smav#endif 1651219089Spjd 1652286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1653240133Smm 1654240133Smm#ifdef illumos 1655240133Smm arc_buf_unwatch(buf); 1656277300Ssmh#endif 1657168404Spjd} 1658168404Spjd 1659168404Spjdvoid 1660168404Spjdarc_buf_freeze(arc_buf_t *buf) 1661168404Spjd{ 1662219089Spjd kmutex_t *hash_lock; 1663219089Spjd 1664168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1665168404Spjd return; 1666168404Spjd 1667219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1668219089Spjd mutex_enter(hash_lock); 1669219089Spjd 1670168404Spjd ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 1671286570Smav buf->b_hdr->b_l1hdr.b_state == arc_anon); 1672185029Spjd arc_cksum_compute(buf, B_FALSE); 1673219089Spjd mutex_exit(hash_lock); 1674240133Smm 1675168404Spjd} 1676168404Spjd 1677168404Spjdstatic void 1678275811Sdelphijget_buf_info(arc_buf_hdr_t *hdr, arc_state_t *state, list_t **list, kmutex_t **lock) 1679205231Skmacy{ 1680275811Sdelphij uint64_t buf_hashid = buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1681205231Skmacy 1682286570Smav if (arc_buf_type(hdr) == ARC_BUFC_METADATA) 1683206796Spjd buf_hashid &= (ARC_BUFC_NUMMETADATALISTS - 1); 1684205231Skmacy else { 1685206796Spjd buf_hashid &= (ARC_BUFC_NUMDATALISTS - 1); 1686205231Skmacy buf_hashid += ARC_BUFC_NUMMETADATALISTS; 1687205231Skmacy } 1688205231Skmacy 1689205231Skmacy *list = &state->arcs_lists[buf_hashid]; 1690205231Skmacy *lock = ARCS_LOCK(state, buf_hashid); 1691205231Skmacy} 1692205231Skmacy 1693205231Skmacy 1694205231Skmacystatic void 1695275811Sdelphijadd_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1696168404Spjd{ 1697286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1698168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1699286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 1700168404Spjd 1701286570Smav if ((refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && 1702286570Smav (state != arc_anon)) { 1703286570Smav /* We don't use the L2-only state list. */ 1704286570Smav if (state != arc_l2c_only) { 1705286570Smav uint64_t delta = hdr->b_size * hdr->b_l1hdr.b_datacnt; 1706286570Smav uint64_t *size = &state->arcs_lsize[arc_buf_type(hdr)]; 1707286570Smav list_t *list; 1708286570Smav kmutex_t *lock; 1709168404Spjd 1710286570Smav get_buf_info(hdr, state, &list, &lock); 1711286570Smav ASSERT(!MUTEX_HELD(lock)); 1712286570Smav mutex_enter(lock); 1713286570Smav ASSERT(list_link_active(&hdr->b_l1hdr.b_arc_node)); 1714286570Smav list_remove(list, hdr); 1715286570Smav if (GHOST_STATE(state)) { 1716286570Smav ASSERT0(hdr->b_l1hdr.b_datacnt); 1717286570Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 1718286570Smav delta = hdr->b_size; 1719286570Smav } 1720286570Smav ASSERT(delta > 0); 1721286570Smav ASSERT3U(*size, >=, delta); 1722286570Smav atomic_add_64(size, -delta); 1723286570Smav mutex_exit(lock); 1724168404Spjd } 1725185029Spjd /* remove the prefetch flag if we get a reference */ 1726286570Smav hdr->b_flags &= ~ARC_FLAG_PREFETCH; 1727168404Spjd } 1728168404Spjd} 1729168404Spjd 1730168404Spjdstatic int 1731275811Sdelphijremove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1732168404Spjd{ 1733168404Spjd int cnt; 1734286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 1735168404Spjd 1736286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1737168404Spjd ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1738168404Spjd ASSERT(!GHOST_STATE(state)); 1739168404Spjd 1740286570Smav /* 1741286570Smav * arc_l2c_only counts as a ghost state so we don't need to explicitly 1742286570Smav * check to prevent usage of the arc_l2c_only list. 1743286570Smav */ 1744286570Smav if (((cnt = refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && 1745168404Spjd (state != arc_anon)) { 1746286570Smav uint64_t *size = &state->arcs_lsize[arc_buf_type(hdr)]; 1747205231Skmacy list_t *list; 1748205231Skmacy kmutex_t *lock; 1749185029Spjd 1750275811Sdelphij get_buf_info(hdr, state, &list, &lock); 1751205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1752205231Skmacy mutex_enter(lock); 1753286570Smav ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 1754275811Sdelphij list_insert_head(list, hdr); 1755286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 1756286570Smav atomic_add_64(size, hdr->b_size * 1757286570Smav hdr->b_l1hdr.b_datacnt); 1758206794Spjd mutex_exit(lock); 1759168404Spjd } 1760168404Spjd return (cnt); 1761168404Spjd} 1762168404Spjd 1763168404Spjd/* 1764168404Spjd * Move the supplied buffer to the indicated state. The mutex 1765168404Spjd * for the buffer must be held by the caller. 1766168404Spjd */ 1767168404Spjdstatic void 1768275811Sdelphijarc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, 1769275811Sdelphij kmutex_t *hash_lock) 1770168404Spjd{ 1771286570Smav arc_state_t *old_state; 1772286570Smav int64_t refcnt; 1773286570Smav uint32_t datacnt; 1774168404Spjd uint64_t from_delta, to_delta; 1775286570Smav arc_buf_contents_t buftype = arc_buf_type(hdr); 1776205231Skmacy list_t *list; 1777205231Skmacy kmutex_t *lock; 1778168404Spjd 1779286570Smav /* 1780286570Smav * We almost always have an L1 hdr here, since we call arc_hdr_realloc() 1781286570Smav * in arc_read() when bringing a buffer out of the L2ARC. However, the 1782286570Smav * L1 hdr doesn't always exist when we change state to arc_anon before 1783286570Smav * destroying a header, in which case reallocating to add the L1 hdr is 1784286570Smav * pointless. 1785286570Smav */ 1786286570Smav if (HDR_HAS_L1HDR(hdr)) { 1787286570Smav old_state = hdr->b_l1hdr.b_state; 1788286570Smav refcnt = refcount_count(&hdr->b_l1hdr.b_refcnt); 1789286570Smav datacnt = hdr->b_l1hdr.b_datacnt; 1790286570Smav } else { 1791286570Smav old_state = arc_l2c_only; 1792286570Smav refcnt = 0; 1793286570Smav datacnt = 0; 1794286570Smav } 1795286570Smav 1796168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1797258632Savg ASSERT3P(new_state, !=, old_state); 1798286570Smav ASSERT(refcnt == 0 || datacnt > 0); 1799286570Smav ASSERT(!GHOST_STATE(new_state) || datacnt == 0); 1800286570Smav ASSERT(old_state != arc_anon || datacnt <= 1); 1801168404Spjd 1802286570Smav from_delta = to_delta = datacnt * hdr->b_size; 1803168404Spjd 1804168404Spjd /* 1805168404Spjd * If this buffer is evictable, transfer it from the 1806168404Spjd * old state list to the new state list. 1807168404Spjd */ 1808168404Spjd if (refcnt == 0) { 1809286570Smav if (old_state != arc_anon && old_state != arc_l2c_only) { 1810205231Skmacy int use_mutex; 1811286570Smav uint64_t *size = &old_state->arcs_lsize[buftype]; 1812168404Spjd 1813275811Sdelphij get_buf_info(hdr, old_state, &list, &lock); 1814205231Skmacy use_mutex = !MUTEX_HELD(lock); 1815168404Spjd if (use_mutex) 1816205231Skmacy mutex_enter(lock); 1817168404Spjd 1818286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1819286570Smav ASSERT(list_link_active(&hdr->b_l1hdr.b_arc_node)); 1820275811Sdelphij list_remove(list, hdr); 1821168404Spjd 1822168404Spjd /* 1823168404Spjd * If prefetching out of the ghost cache, 1824219089Spjd * we will have a non-zero datacnt. 1825168404Spjd */ 1826286570Smav if (GHOST_STATE(old_state) && datacnt == 0) { 1827168404Spjd /* ghost elements have a ghost size */ 1828286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 1829275811Sdelphij from_delta = hdr->b_size; 1830168404Spjd } 1831185029Spjd ASSERT3U(*size, >=, from_delta); 1832185029Spjd atomic_add_64(size, -from_delta); 1833168404Spjd 1834168404Spjd if (use_mutex) 1835205231Skmacy mutex_exit(lock); 1836168404Spjd } 1837286570Smav if (new_state != arc_anon && new_state != arc_l2c_only) { 1838206796Spjd int use_mutex; 1839286570Smav uint64_t *size = &new_state->arcs_lsize[buftype]; 1840168404Spjd 1841286570Smav /* 1842286570Smav * An L1 header always exists here, since if we're 1843286570Smav * moving to some L1-cached state (i.e. not l2c_only or 1844286570Smav * anonymous), we realloc the header to add an L1hdr 1845286570Smav * beforehand. 1846286570Smav */ 1847286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1848275811Sdelphij get_buf_info(hdr, new_state, &list, &lock); 1849205231Skmacy use_mutex = !MUTEX_HELD(lock); 1850168404Spjd if (use_mutex) 1851205231Skmacy mutex_enter(lock); 1852168404Spjd 1853275811Sdelphij list_insert_head(list, hdr); 1854168404Spjd 1855168404Spjd /* ghost elements have a ghost size */ 1856168404Spjd if (GHOST_STATE(new_state)) { 1857286570Smav ASSERT(datacnt == 0); 1858286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 1859275811Sdelphij to_delta = hdr->b_size; 1860168404Spjd } 1861185029Spjd atomic_add_64(size, to_delta); 1862168404Spjd 1863168404Spjd if (use_mutex) 1864205231Skmacy mutex_exit(lock); 1865168404Spjd } 1866168404Spjd } 1867168404Spjd 1868275811Sdelphij ASSERT(!BUF_EMPTY(hdr)); 1869275811Sdelphij if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) 1870275811Sdelphij buf_hash_remove(hdr); 1871168404Spjd 1872286570Smav /* adjust state sizes (ignore arc_l2c_only) */ 1873286570Smav if (to_delta && new_state != arc_l2c_only) 1874168404Spjd atomic_add_64(&new_state->arcs_size, to_delta); 1875286570Smav if (from_delta && old_state != arc_l2c_only) { 1876168404Spjd ASSERT3U(old_state->arcs_size, >=, from_delta); 1877168404Spjd atomic_add_64(&old_state->arcs_size, -from_delta); 1878168404Spjd } 1879286570Smav if (HDR_HAS_L1HDR(hdr)) 1880286570Smav hdr->b_l1hdr.b_state = new_state; 1881185029Spjd 1882286570Smav /* 1883286570Smav * L2 headers should never be on the L2 state list since they don't 1884286570Smav * have L1 headers allocated. 1885286570Smav */ 1886286576Smav#ifdef illumos 1887286570Smav ASSERT(list_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && 1888286570Smav list_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); 1889286576Smav#endif 1890168404Spjd} 1891168404Spjd 1892185029Spjdvoid 1893208373Smmarc_space_consume(uint64_t space, arc_space_type_t type) 1894185029Spjd{ 1895208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1896208373Smm 1897208373Smm switch (type) { 1898208373Smm case ARC_SPACE_DATA: 1899208373Smm ARCSTAT_INCR(arcstat_data_size, space); 1900208373Smm break; 1901286574Smav case ARC_SPACE_META: 1902286574Smav ARCSTAT_INCR(arcstat_metadata_size, space); 1903286574Smav break; 1904208373Smm case ARC_SPACE_OTHER: 1905208373Smm ARCSTAT_INCR(arcstat_other_size, space); 1906208373Smm break; 1907208373Smm case ARC_SPACE_HDRS: 1908208373Smm ARCSTAT_INCR(arcstat_hdr_size, space); 1909208373Smm break; 1910208373Smm case ARC_SPACE_L2HDRS: 1911208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, space); 1912208373Smm break; 1913208373Smm } 1914208373Smm 1915286574Smav if (type != ARC_SPACE_DATA) 1916286574Smav ARCSTAT_INCR(arcstat_meta_used, space); 1917286574Smav 1918185029Spjd atomic_add_64(&arc_size, space); 1919185029Spjd} 1920185029Spjd 1921185029Spjdvoid 1922208373Smmarc_space_return(uint64_t space, arc_space_type_t type) 1923185029Spjd{ 1924208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1925208373Smm 1926208373Smm switch (type) { 1927208373Smm case ARC_SPACE_DATA: 1928208373Smm ARCSTAT_INCR(arcstat_data_size, -space); 1929208373Smm break; 1930286574Smav case ARC_SPACE_META: 1931286574Smav ARCSTAT_INCR(arcstat_metadata_size, -space); 1932286574Smav break; 1933208373Smm case ARC_SPACE_OTHER: 1934208373Smm ARCSTAT_INCR(arcstat_other_size, -space); 1935208373Smm break; 1936208373Smm case ARC_SPACE_HDRS: 1937208373Smm ARCSTAT_INCR(arcstat_hdr_size, -space); 1938208373Smm break; 1939208373Smm case ARC_SPACE_L2HDRS: 1940208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 1941208373Smm break; 1942208373Smm } 1943208373Smm 1944286574Smav if (type != ARC_SPACE_DATA) { 1945286574Smav ASSERT(arc_meta_used >= space); 1946286574Smav if (arc_meta_max < arc_meta_used) 1947286574Smav arc_meta_max = arc_meta_used; 1948286574Smav ARCSTAT_INCR(arcstat_meta_used, -space); 1949286574Smav } 1950286574Smav 1951185029Spjd ASSERT(arc_size >= space); 1952185029Spjd atomic_add_64(&arc_size, -space); 1953185029Spjd} 1954185029Spjd 1955168404Spjdarc_buf_t * 1956286570Smavarc_buf_alloc(spa_t *spa, int32_t size, void *tag, arc_buf_contents_t type) 1957168404Spjd{ 1958168404Spjd arc_buf_hdr_t *hdr; 1959168404Spjd arc_buf_t *buf; 1960168404Spjd 1961168404Spjd ASSERT3U(size, >, 0); 1962286570Smav hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 1963168404Spjd ASSERT(BUF_EMPTY(hdr)); 1964286570Smav ASSERT3P(hdr->b_freeze_cksum, ==, NULL); 1965168404Spjd hdr->b_size = size; 1966228103Smm hdr->b_spa = spa_load_guid(spa); 1967286570Smav 1968185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1969168404Spjd buf->b_hdr = hdr; 1970168404Spjd buf->b_data = NULL; 1971168404Spjd buf->b_efunc = NULL; 1972168404Spjd buf->b_private = NULL; 1973168404Spjd buf->b_next = NULL; 1974286570Smav 1975286570Smav hdr->b_flags = arc_bufc_to_flags(type); 1976286570Smav hdr->b_flags |= ARC_FLAG_HAS_L1HDR; 1977286570Smav 1978286570Smav hdr->b_l1hdr.b_buf = buf; 1979286570Smav hdr->b_l1hdr.b_state = arc_anon; 1980286570Smav hdr->b_l1hdr.b_arc_access = 0; 1981286570Smav hdr->b_l1hdr.b_datacnt = 1; 1982286570Smav 1983168404Spjd arc_get_data_buf(buf); 1984286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 1985286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 1986168404Spjd 1987168404Spjd return (buf); 1988168404Spjd} 1989168404Spjd 1990209962Smmstatic char *arc_onloan_tag = "onloan"; 1991209962Smm 1992209962Smm/* 1993209962Smm * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 1994209962Smm * flight data by arc_tempreserve_space() until they are "returned". Loaned 1995209962Smm * buffers must be returned to the arc before they can be used by the DMU or 1996209962Smm * freed. 1997209962Smm */ 1998209962Smmarc_buf_t * 1999209962Smmarc_loan_buf(spa_t *spa, int size) 2000209962Smm{ 2001209962Smm arc_buf_t *buf; 2002209962Smm 2003209962Smm buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 2004209962Smm 2005209962Smm atomic_add_64(&arc_loaned_bytes, size); 2006209962Smm return (buf); 2007209962Smm} 2008209962Smm 2009209962Smm/* 2010209962Smm * Return a loaned arc buffer to the arc. 2011209962Smm */ 2012209962Smmvoid 2013209962Smmarc_return_buf(arc_buf_t *buf, void *tag) 2014209962Smm{ 2015209962Smm arc_buf_hdr_t *hdr = buf->b_hdr; 2016209962Smm 2017209962Smm ASSERT(buf->b_data != NULL); 2018286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2019286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 2020286570Smav (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2021209962Smm 2022209962Smm atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 2023209962Smm} 2024209962Smm 2025219089Spjd/* Detach an arc_buf from a dbuf (tag) */ 2026219089Spjdvoid 2027219089Spjdarc_loan_inuse_buf(arc_buf_t *buf, void *tag) 2028219089Spjd{ 2029286570Smav arc_buf_hdr_t *hdr = buf->b_hdr; 2030219089Spjd 2031219089Spjd ASSERT(buf->b_data != NULL); 2032286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2033286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2034286570Smav (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); 2035219089Spjd buf->b_efunc = NULL; 2036219089Spjd buf->b_private = NULL; 2037219089Spjd 2038219089Spjd atomic_add_64(&arc_loaned_bytes, hdr->b_size); 2039219089Spjd} 2040219089Spjd 2041168404Spjdstatic arc_buf_t * 2042168404Spjdarc_buf_clone(arc_buf_t *from) 2043168404Spjd{ 2044168404Spjd arc_buf_t *buf; 2045168404Spjd arc_buf_hdr_t *hdr = from->b_hdr; 2046168404Spjd uint64_t size = hdr->b_size; 2047168404Spjd 2048286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2049286570Smav ASSERT(hdr->b_l1hdr.b_state != arc_anon); 2050219089Spjd 2051185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2052168404Spjd buf->b_hdr = hdr; 2053168404Spjd buf->b_data = NULL; 2054168404Spjd buf->b_efunc = NULL; 2055168404Spjd buf->b_private = NULL; 2056286570Smav buf->b_next = hdr->b_l1hdr.b_buf; 2057286570Smav hdr->b_l1hdr.b_buf = buf; 2058168404Spjd arc_get_data_buf(buf); 2059168404Spjd bcopy(from->b_data, buf->b_data, size); 2060242845Sdelphij 2061242845Sdelphij /* 2062242845Sdelphij * This buffer already exists in the arc so create a duplicate 2063242845Sdelphij * copy for the caller. If the buffer is associated with user data 2064242845Sdelphij * then track the size and number of duplicates. These stats will be 2065242845Sdelphij * updated as duplicate buffers are created and destroyed. 2066242845Sdelphij */ 2067286570Smav if (HDR_ISTYPE_DATA(hdr)) { 2068242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_buffers); 2069242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, size); 2070242845Sdelphij } 2071286570Smav hdr->b_l1hdr.b_datacnt += 1; 2072168404Spjd return (buf); 2073168404Spjd} 2074168404Spjd 2075168404Spjdvoid 2076168404Spjdarc_buf_add_ref(arc_buf_t *buf, void* tag) 2077168404Spjd{ 2078168404Spjd arc_buf_hdr_t *hdr; 2079168404Spjd kmutex_t *hash_lock; 2080168404Spjd 2081168404Spjd /* 2082185029Spjd * Check to see if this buffer is evicted. Callers 2083185029Spjd * must verify b_data != NULL to know if the add_ref 2084185029Spjd * was successful. 2085168404Spjd */ 2086219089Spjd mutex_enter(&buf->b_evict_lock); 2087185029Spjd if (buf->b_data == NULL) { 2088219089Spjd mutex_exit(&buf->b_evict_lock); 2089168404Spjd return; 2090168404Spjd } 2091219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 2092219089Spjd mutex_enter(hash_lock); 2093185029Spjd hdr = buf->b_hdr; 2094286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2095219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2096219089Spjd mutex_exit(&buf->b_evict_lock); 2097168404Spjd 2098286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_mru || 2099286570Smav hdr->b_l1hdr.b_state == arc_mfu); 2100286570Smav 2101168404Spjd add_reference(hdr, hash_lock, tag); 2102208373Smm DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 2103168404Spjd arc_access(hdr, hash_lock); 2104168404Spjd mutex_exit(hash_lock); 2105168404Spjd ARCSTAT_BUMP(arcstat_hits); 2106286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 2107286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 2108168404Spjd data, metadata, hits); 2109168404Spjd} 2110168404Spjd 2111274172Savgstatic void 2112274172Savgarc_buf_free_on_write(void *data, size_t size, 2113274172Savg void (*free_func)(void *, size_t)) 2114274172Savg{ 2115274172Savg l2arc_data_free_t *df; 2116274172Savg 2117274172Savg df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 2118274172Savg df->l2df_data = data; 2119274172Savg df->l2df_size = size; 2120274172Savg df->l2df_func = free_func; 2121274172Savg mutex_enter(&l2arc_free_on_write_mtx); 2122274172Savg list_insert_head(l2arc_free_on_write, df); 2123274172Savg mutex_exit(&l2arc_free_on_write_mtx); 2124274172Savg} 2125274172Savg 2126185029Spjd/* 2127185029Spjd * Free the arc data buffer. If it is an l2arc write in progress, 2128185029Spjd * the buffer is placed on l2arc_free_on_write to be freed later. 2129185029Spjd */ 2130168404Spjdstatic void 2131240133Smmarc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t)) 2132185029Spjd{ 2133240133Smm arc_buf_hdr_t *hdr = buf->b_hdr; 2134240133Smm 2135185029Spjd if (HDR_L2_WRITING(hdr)) { 2136274172Savg arc_buf_free_on_write(buf->b_data, hdr->b_size, free_func); 2137185029Spjd ARCSTAT_BUMP(arcstat_l2_free_on_write); 2138185029Spjd } else { 2139240133Smm free_func(buf->b_data, hdr->b_size); 2140185029Spjd } 2141185029Spjd} 2142185029Spjd 2143268858Sdelphij/* 2144268858Sdelphij * Free up buf->b_data and if 'remove' is set, then pull the 2145268858Sdelphij * arc_buf_t off of the the arc_buf_hdr_t's list and free it. 2146268858Sdelphij */ 2147185029Spjdstatic void 2148274172Savgarc_buf_l2_cdata_free(arc_buf_hdr_t *hdr) 2149274172Savg{ 2150286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 2151286570Smav ASSERT(MUTEX_HELD(&hdr->b_l2hdr.b_dev->l2ad_mtx)); 2152274172Savg 2153286570Smav /* 2154286570Smav * The b_tmp_cdata field is linked off of the b_l1hdr, so if 2155286570Smav * that doesn't exist, the header is in the arc_l2c_only state, 2156286570Smav * and there isn't anything to free (it's already been freed). 2157286570Smav */ 2158286570Smav if (!HDR_HAS_L1HDR(hdr)) 2159286570Smav return; 2160274172Savg 2161286570Smav if (hdr->b_l1hdr.b_tmp_cdata == NULL) 2162274172Savg return; 2163274172Savg 2164274172Savg ASSERT(HDR_L2_WRITING(hdr)); 2165286570Smav arc_buf_free_on_write(hdr->b_l1hdr.b_tmp_cdata, hdr->b_size, 2166274172Savg zio_data_buf_free); 2167286570Smav 2168274172Savg ARCSTAT_BUMP(arcstat_l2_cdata_free_on_write); 2169286570Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 2170274172Savg} 2171274172Savg 2172274172Savgstatic void 2173268858Sdelphijarc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t remove) 2174168404Spjd{ 2175168404Spjd arc_buf_t **bufp; 2176168404Spjd 2177168404Spjd /* free up data associated with the buf */ 2178286570Smav if (buf->b_data != NULL) { 2179286570Smav arc_state_t *state = buf->b_hdr->b_l1hdr.b_state; 2180168404Spjd uint64_t size = buf->b_hdr->b_size; 2181286570Smav arc_buf_contents_t type = arc_buf_type(buf->b_hdr); 2182168404Spjd 2183168404Spjd arc_cksum_verify(buf); 2184240133Smm#ifdef illumos 2185240133Smm arc_buf_unwatch(buf); 2186277300Ssmh#endif 2187219089Spjd 2188168404Spjd if (!recycle) { 2189168404Spjd if (type == ARC_BUFC_METADATA) { 2190240133Smm arc_buf_data_free(buf, zio_buf_free); 2191286574Smav arc_space_return(size, ARC_SPACE_META); 2192168404Spjd } else { 2193168404Spjd ASSERT(type == ARC_BUFC_DATA); 2194240133Smm arc_buf_data_free(buf, zio_data_buf_free); 2195286574Smav arc_space_return(size, ARC_SPACE_DATA); 2196168404Spjd } 2197168404Spjd } 2198286570Smav if (list_link_active(&buf->b_hdr->b_l1hdr.b_arc_node)) { 2199185029Spjd uint64_t *cnt = &state->arcs_lsize[type]; 2200185029Spjd 2201286570Smav ASSERT(refcount_is_zero( 2202286570Smav &buf->b_hdr->b_l1hdr.b_refcnt)); 2203286570Smav ASSERT(state != arc_anon && state != arc_l2c_only); 2204185029Spjd 2205185029Spjd ASSERT3U(*cnt, >=, size); 2206185029Spjd atomic_add_64(cnt, -size); 2207168404Spjd } 2208168404Spjd ASSERT3U(state->arcs_size, >=, size); 2209168404Spjd atomic_add_64(&state->arcs_size, -size); 2210168404Spjd buf->b_data = NULL; 2211242845Sdelphij 2212242845Sdelphij /* 2213242845Sdelphij * If we're destroying a duplicate buffer make sure 2214242845Sdelphij * that the appropriate statistics are updated. 2215242845Sdelphij */ 2216286570Smav if (buf->b_hdr->b_l1hdr.b_datacnt > 1 && 2217286570Smav HDR_ISTYPE_DATA(buf->b_hdr)) { 2218242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 2219242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size); 2220242845Sdelphij } 2221286570Smav ASSERT(buf->b_hdr->b_l1hdr.b_datacnt > 0); 2222286570Smav buf->b_hdr->b_l1hdr.b_datacnt -= 1; 2223168404Spjd } 2224168404Spjd 2225168404Spjd /* only remove the buf if requested */ 2226268858Sdelphij if (!remove) 2227168404Spjd return; 2228168404Spjd 2229168404Spjd /* remove the buf from the hdr list */ 2230286570Smav for (bufp = &buf->b_hdr->b_l1hdr.b_buf; *bufp != buf; 2231286570Smav bufp = &(*bufp)->b_next) 2232168404Spjd continue; 2233168404Spjd *bufp = buf->b_next; 2234219089Spjd buf->b_next = NULL; 2235168404Spjd 2236168404Spjd ASSERT(buf->b_efunc == NULL); 2237168404Spjd 2238168404Spjd /* clean up the buf */ 2239168404Spjd buf->b_hdr = NULL; 2240168404Spjd kmem_cache_free(buf_cache, buf); 2241168404Spjd} 2242168404Spjd 2243168404Spjdstatic void 2244286598Smavarc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) 2245286598Smav{ 2246286598Smav l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 2247286598Smav l2arc_dev_t *dev = l2hdr->b_dev; 2248286598Smav 2249286598Smav ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); 2250286598Smav ASSERT(HDR_HAS_L2HDR(hdr)); 2251286598Smav 2252286598Smav list_remove(&dev->l2ad_buflist, hdr); 2253286598Smav 2254286598Smav /* 2255286598Smav * We don't want to leak the b_tmp_cdata buffer that was 2256286598Smav * allocated in l2arc_write_buffers() 2257286598Smav */ 2258286598Smav arc_buf_l2_cdata_free(hdr); 2259286598Smav 2260286598Smav /* 2261286598Smav * If the l2hdr's b_daddr is equal to L2ARC_ADDR_UNSET, then 2262286598Smav * this header is being processed by l2arc_write_buffers() (i.e. 2263286598Smav * it's in the first stage of l2arc_write_buffers()). 2264286598Smav * Re-affirming that truth here, just to serve as a reminder. If 2265286598Smav * b_daddr does not equal L2ARC_ADDR_UNSET, then the header may or 2266286598Smav * may not have its HDR_L2_WRITING flag set. (the write may have 2267286598Smav * completed, in which case HDR_L2_WRITING will be false and the 2268286598Smav * b_daddr field will point to the address of the buffer on disk). 2269286598Smav */ 2270286598Smav IMPLY(l2hdr->b_daddr == L2ARC_ADDR_UNSET, HDR_L2_WRITING(hdr)); 2271286598Smav 2272286598Smav /* 2273286598Smav * If b_daddr is equal to L2ARC_ADDR_UNSET, we're racing with 2274286598Smav * l2arc_write_buffers(). Since we've just removed this header 2275286598Smav * from the l2arc buffer list, this header will never reach the 2276286598Smav * second stage of l2arc_write_buffers(), which increments the 2277286598Smav * accounting stats for this header. Thus, we must be careful 2278286598Smav * not to decrement them for this header either. 2279286598Smav */ 2280286598Smav if (l2hdr->b_daddr != L2ARC_ADDR_UNSET) { 2281286598Smav ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); 2282286598Smav ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 2283286598Smav 2284286598Smav vdev_space_update(dev->l2ad_vdev, 2285286598Smav -l2hdr->b_asize, 0, 0); 2286286598Smav 2287286598Smav (void) refcount_remove_many(&dev->l2ad_alloc, 2288286598Smav l2hdr->b_asize, hdr); 2289286598Smav } 2290286598Smav 2291286598Smav hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR; 2292286598Smav} 2293286598Smav 2294286598Smavstatic void 2295168404Spjdarc_hdr_destroy(arc_buf_hdr_t *hdr) 2296168404Spjd{ 2297286570Smav if (HDR_HAS_L1HDR(hdr)) { 2298286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL || 2299286570Smav hdr->b_l1hdr.b_datacnt > 0); 2300286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2301286570Smav ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 2302286570Smav } 2303168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2304286570Smav ASSERT(!HDR_IN_HASH_TABLE(hdr)); 2305168404Spjd 2306286570Smav if (HDR_HAS_L2HDR(hdr)) { 2307286598Smav l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 2308286598Smav boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); 2309286570Smav 2310286598Smav if (!buflist_held) 2311286598Smav mutex_enter(&dev->l2ad_mtx); 2312219089Spjd 2313286570Smav /* 2314286598Smav * Even though we checked this conditional above, we 2315286598Smav * need to check this again now that we have the 2316286598Smav * l2ad_mtx. This is because we could be racing with 2317286598Smav * another thread calling l2arc_evict() which might have 2318286598Smav * destroyed this header's L2 portion as we were waiting 2319286598Smav * to acquire the l2ad_mtx. If that happens, we don't 2320286598Smav * want to re-destroy the header's L2 portion. 2321286570Smav */ 2322286598Smav if (HDR_HAS_L2HDR(hdr)) { 2323286598Smav trim_map_free(dev->l2ad_vdev, hdr->b_l2hdr.b_daddr, 2324286598Smav hdr->b_l2hdr.b_asize, 0); 2325286598Smav arc_hdr_l2hdr_destroy(hdr); 2326286598Smav } 2327286570Smav 2328219089Spjd if (!buflist_held) 2329286598Smav mutex_exit(&dev->l2ad_mtx); 2330185029Spjd } 2331185029Spjd 2332286570Smav if (!BUF_EMPTY(hdr)) 2333219089Spjd buf_discard_identity(hdr); 2334168404Spjd if (hdr->b_freeze_cksum != NULL) { 2335168404Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 2336168404Spjd hdr->b_freeze_cksum = NULL; 2337168404Spjd } 2338286570Smav 2339286570Smav if (HDR_HAS_L1HDR(hdr)) { 2340286570Smav while (hdr->b_l1hdr.b_buf) { 2341286570Smav arc_buf_t *buf = hdr->b_l1hdr.b_buf; 2342286570Smav 2343286570Smav if (buf->b_efunc != NULL) { 2344286570Smav mutex_enter(&arc_eviction_mtx); 2345286570Smav mutex_enter(&buf->b_evict_lock); 2346286570Smav ASSERT(buf->b_hdr != NULL); 2347286570Smav arc_buf_destroy(hdr->b_l1hdr.b_buf, FALSE, 2348286570Smav FALSE); 2349286570Smav hdr->b_l1hdr.b_buf = buf->b_next; 2350286570Smav buf->b_hdr = &arc_eviction_hdr; 2351286570Smav buf->b_next = arc_eviction_list; 2352286570Smav arc_eviction_list = buf; 2353286570Smav mutex_exit(&buf->b_evict_lock); 2354286570Smav mutex_exit(&arc_eviction_mtx); 2355286570Smav } else { 2356286570Smav arc_buf_destroy(hdr->b_l1hdr.b_buf, FALSE, 2357286570Smav TRUE); 2358286570Smav } 2359286570Smav } 2360286570Smav#ifdef ZFS_DEBUG 2361286570Smav if (hdr->b_l1hdr.b_thawed != NULL) { 2362286570Smav kmem_free(hdr->b_l1hdr.b_thawed, 1); 2363286570Smav hdr->b_l1hdr.b_thawed = NULL; 2364286570Smav } 2365286570Smav#endif 2366219089Spjd } 2367168404Spjd 2368168404Spjd ASSERT3P(hdr->b_hash_next, ==, NULL); 2369286570Smav if (HDR_HAS_L1HDR(hdr)) { 2370286570Smav ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 2371286570Smav ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 2372286570Smav kmem_cache_free(hdr_full_cache, hdr); 2373286570Smav } else { 2374286570Smav kmem_cache_free(hdr_l2only_cache, hdr); 2375286570Smav } 2376168404Spjd} 2377168404Spjd 2378168404Spjdvoid 2379168404Spjdarc_buf_free(arc_buf_t *buf, void *tag) 2380168404Spjd{ 2381168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2382286570Smav int hashed = hdr->b_l1hdr.b_state != arc_anon; 2383168404Spjd 2384168404Spjd ASSERT(buf->b_efunc == NULL); 2385168404Spjd ASSERT(buf->b_data != NULL); 2386168404Spjd 2387168404Spjd if (hashed) { 2388168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 2389168404Spjd 2390168404Spjd mutex_enter(hash_lock); 2391219089Spjd hdr = buf->b_hdr; 2392219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2393219089Spjd 2394168404Spjd (void) remove_reference(hdr, hash_lock, tag); 2395286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 2396168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 2397219089Spjd } else { 2398286570Smav ASSERT(buf == hdr->b_l1hdr.b_buf); 2399219089Spjd ASSERT(buf->b_efunc == NULL); 2400275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 2401219089Spjd } 2402168404Spjd mutex_exit(hash_lock); 2403168404Spjd } else if (HDR_IO_IN_PROGRESS(hdr)) { 2404168404Spjd int destroy_hdr; 2405168404Spjd /* 2406168404Spjd * We are in the middle of an async write. Don't destroy 2407168404Spjd * this buffer unless the write completes before we finish 2408168404Spjd * decrementing the reference count. 2409168404Spjd */ 2410168404Spjd mutex_enter(&arc_eviction_mtx); 2411168404Spjd (void) remove_reference(hdr, NULL, tag); 2412286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2413168404Spjd destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 2414168404Spjd mutex_exit(&arc_eviction_mtx); 2415168404Spjd if (destroy_hdr) 2416168404Spjd arc_hdr_destroy(hdr); 2417168404Spjd } else { 2418219089Spjd if (remove_reference(hdr, NULL, tag) > 0) 2419168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 2420219089Spjd else 2421168404Spjd arc_hdr_destroy(hdr); 2422168404Spjd } 2423168404Spjd} 2424168404Spjd 2425248571Smmboolean_t 2426168404Spjdarc_buf_remove_ref(arc_buf_t *buf, void* tag) 2427168404Spjd{ 2428168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2429168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 2430248571Smm boolean_t no_callback = (buf->b_efunc == NULL); 2431168404Spjd 2432286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 2433286570Smav ASSERT(hdr->b_l1hdr.b_datacnt == 1); 2434168404Spjd arc_buf_free(buf, tag); 2435168404Spjd return (no_callback); 2436168404Spjd } 2437168404Spjd 2438168404Spjd mutex_enter(hash_lock); 2439219089Spjd hdr = buf->b_hdr; 2440286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 2441219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2442286570Smav ASSERT(hdr->b_l1hdr.b_state != arc_anon); 2443168404Spjd ASSERT(buf->b_data != NULL); 2444168404Spjd 2445168404Spjd (void) remove_reference(hdr, hash_lock, tag); 2446286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 2447168404Spjd if (no_callback) 2448168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 2449168404Spjd } else if (no_callback) { 2450286570Smav ASSERT(hdr->b_l1hdr.b_buf == buf && buf->b_next == NULL); 2451219089Spjd ASSERT(buf->b_efunc == NULL); 2452275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 2453168404Spjd } 2454286570Smav ASSERT(no_callback || hdr->b_l1hdr.b_datacnt > 1 || 2455286570Smav refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2456168404Spjd mutex_exit(hash_lock); 2457168404Spjd return (no_callback); 2458168404Spjd} 2459168404Spjd 2460286570Smavint32_t 2461168404Spjdarc_buf_size(arc_buf_t *buf) 2462168404Spjd{ 2463168404Spjd return (buf->b_hdr->b_size); 2464168404Spjd} 2465168404Spjd 2466168404Spjd/* 2467242845Sdelphij * Called from the DMU to determine if the current buffer should be 2468242845Sdelphij * evicted. In order to ensure proper locking, the eviction must be initiated 2469242845Sdelphij * from the DMU. Return true if the buffer is associated with user data and 2470242845Sdelphij * duplicate buffers still exist. 2471242845Sdelphij */ 2472242845Sdelphijboolean_t 2473242845Sdelphijarc_buf_eviction_needed(arc_buf_t *buf) 2474242845Sdelphij{ 2475242845Sdelphij arc_buf_hdr_t *hdr; 2476242845Sdelphij boolean_t evict_needed = B_FALSE; 2477242845Sdelphij 2478242845Sdelphij if (zfs_disable_dup_eviction) 2479242845Sdelphij return (B_FALSE); 2480242845Sdelphij 2481242845Sdelphij mutex_enter(&buf->b_evict_lock); 2482242845Sdelphij hdr = buf->b_hdr; 2483242845Sdelphij if (hdr == NULL) { 2484242845Sdelphij /* 2485242845Sdelphij * We are in arc_do_user_evicts(); let that function 2486242845Sdelphij * perform the eviction. 2487242845Sdelphij */ 2488242845Sdelphij ASSERT(buf->b_data == NULL); 2489242845Sdelphij mutex_exit(&buf->b_evict_lock); 2490242845Sdelphij return (B_FALSE); 2491242845Sdelphij } else if (buf->b_data == NULL) { 2492242845Sdelphij /* 2493242845Sdelphij * We have already been added to the arc eviction list; 2494242845Sdelphij * recommend eviction. 2495242845Sdelphij */ 2496242845Sdelphij ASSERT3P(hdr, ==, &arc_eviction_hdr); 2497242845Sdelphij mutex_exit(&buf->b_evict_lock); 2498242845Sdelphij return (B_TRUE); 2499242845Sdelphij } 2500242845Sdelphij 2501286570Smav if (hdr->b_l1hdr.b_datacnt > 1 && HDR_ISTYPE_DATA(hdr)) 2502242845Sdelphij evict_needed = B_TRUE; 2503242845Sdelphij 2504242845Sdelphij mutex_exit(&buf->b_evict_lock); 2505242845Sdelphij return (evict_needed); 2506242845Sdelphij} 2507242845Sdelphij 2508242845Sdelphij/* 2509168404Spjd * Evict buffers from list until we've removed the specified number of 2510168404Spjd * bytes. Move the removed buffers to the appropriate evict state. 2511168404Spjd * If the recycle flag is set, then attempt to "recycle" a buffer: 2512168404Spjd * - look for a buffer to evict that is `bytes' long. 2513168404Spjd * - return the data block from this buffer rather than freeing it. 2514168404Spjd * This flag is used by callers that are trying to make space for a 2515168404Spjd * new buffer in a full arc cache. 2516185029Spjd * 2517185029Spjd * This function makes a "best effort". It skips over any buffers 2518185029Spjd * it can't get a hash_lock on, and so may not catch all candidates. 2519185029Spjd * It may also return without evicting as much space as requested. 2520168404Spjd */ 2521168404Spjdstatic void * 2522209962Smmarc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 2523168404Spjd arc_buf_contents_t type) 2524168404Spjd{ 2525168404Spjd arc_state_t *evicted_state; 2526168404Spjd uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 2527205231Skmacy int64_t bytes_remaining; 2528275811Sdelphij arc_buf_hdr_t *hdr, *hdr_prev = NULL; 2529205231Skmacy list_t *evicted_list, *list, *evicted_list_start, *list_start; 2530205231Skmacy kmutex_t *lock, *evicted_lock; 2531168404Spjd kmutex_t *hash_lock; 2532168404Spjd boolean_t have_lock; 2533168404Spjd void *stolen = NULL; 2534258632Savg arc_buf_hdr_t marker = { 0 }; 2535258632Savg int count = 0; 2536205231Skmacy static int evict_metadata_offset, evict_data_offset; 2537258632Savg int i, idx, offset, list_count, lists; 2538168404Spjd 2539168404Spjd ASSERT(state == arc_mru || state == arc_mfu); 2540168404Spjd 2541168404Spjd evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 2542206796Spjd 2543275780Sdelphij /* 2544275780Sdelphij * Decide which "type" (data vs metadata) to recycle from. 2545275780Sdelphij * 2546275780Sdelphij * If we are over the metadata limit, recycle from metadata. 2547275780Sdelphij * If we are under the metadata minimum, recycle from data. 2548275780Sdelphij * Otherwise, recycle from whichever type has the oldest (least 2549275780Sdelphij * recently accessed) header. This is not yet implemented. 2550275780Sdelphij */ 2551275780Sdelphij if (recycle) { 2552275780Sdelphij arc_buf_contents_t realtype; 2553275780Sdelphij if (state->arcs_lsize[ARC_BUFC_DATA] == 0) { 2554275780Sdelphij realtype = ARC_BUFC_METADATA; 2555275780Sdelphij } else if (state->arcs_lsize[ARC_BUFC_METADATA] == 0) { 2556275780Sdelphij realtype = ARC_BUFC_DATA; 2557275780Sdelphij } else if (arc_meta_used >= arc_meta_limit) { 2558275780Sdelphij realtype = ARC_BUFC_METADATA; 2559275780Sdelphij } else if (arc_meta_used <= arc_meta_min) { 2560275780Sdelphij realtype = ARC_BUFC_DATA; 2561286570Smav#ifdef illumos 2562286570Smav } else if (HDR_HAS_L1HDR(data_hdr) && 2563286570Smav HDR_HAS_L1HDR(metadata_hdr) && 2564286570Smav data_hdr->b_l1hdr.b_arc_access < 2565286570Smav metadata_hdr->b_l1hdr.b_arc_access) { 2566286570Smav realtype = ARC_BUFC_DATA; 2567275780Sdelphij } else { 2568286570Smav realtype = ARC_BUFC_METADATA; 2569275780Sdelphij#else 2570286570Smav } else { 2571275780Sdelphij /* TODO */ 2572275780Sdelphij realtype = type; 2573275780Sdelphij#endif 2574275780Sdelphij } 2575275780Sdelphij if (realtype != type) { 2576275780Sdelphij /* 2577275780Sdelphij * If we want to evict from a different list, 2578275780Sdelphij * we can not recycle, because DATA vs METADATA 2579275780Sdelphij * buffers are segregated into different kmem 2580275780Sdelphij * caches (and vmem arenas). 2581275780Sdelphij */ 2582275780Sdelphij type = realtype; 2583275780Sdelphij recycle = B_FALSE; 2584275780Sdelphij } 2585275780Sdelphij } 2586275780Sdelphij 2587205231Skmacy if (type == ARC_BUFC_METADATA) { 2588205231Skmacy offset = 0; 2589205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 2590205231Skmacy list_start = &state->arcs_lists[0]; 2591205231Skmacy evicted_list_start = &evicted_state->arcs_lists[0]; 2592205231Skmacy idx = evict_metadata_offset; 2593205231Skmacy } else { 2594205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 2595205231Skmacy list_start = &state->arcs_lists[offset]; 2596205231Skmacy evicted_list_start = &evicted_state->arcs_lists[offset]; 2597205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 2598205231Skmacy idx = evict_data_offset; 2599205231Skmacy } 2600205231Skmacy bytes_remaining = evicted_state->arcs_lsize[type]; 2601258632Savg lists = 0; 2602206796Spjd 2603205231Skmacyevict_start: 2604205231Skmacy list = &list_start[idx]; 2605205231Skmacy evicted_list = &evicted_list_start[idx]; 2606205231Skmacy lock = ARCS_LOCK(state, (offset + idx)); 2607206796Spjd evicted_lock = ARCS_LOCK(evicted_state, (offset + idx)); 2608168404Spjd 2609286570Smav /* 2610286570Smav * The ghost list lock must be acquired first in order to prevent 2611286570Smav * a 3 party deadlock: 2612286570Smav * 2613286570Smav * - arc_evict_ghost acquires arc_*_ghost->arcs_mtx, followed by 2614286570Smav * l2ad_mtx in arc_hdr_realloc 2615286570Smav * - l2arc_write_buffers acquires l2ad_mtx, followed by arc_*->arcs_mtx 2616286570Smav * - arc_evict acquires arc_*_ghost->arcs_mtx, followed by 2617286570Smav * arc_*_ghost->arcs_mtx and forms a deadlock cycle. 2618286570Smav * 2619286570Smav * This situation is avoided by acquiring the ghost list lock first. 2620286570Smav */ 2621286570Smav mutex_enter(evicted_lock); 2622205231Skmacy mutex_enter(lock); 2623205231Skmacy 2624275811Sdelphij for (hdr = list_tail(list); hdr; hdr = hdr_prev) { 2625275811Sdelphij hdr_prev = list_prev(list, hdr); 2626286570Smav if (HDR_HAS_L1HDR(hdr)) { 2627286570Smav bytes_remaining -= 2628286570Smav (hdr->b_size * hdr->b_l1hdr.b_datacnt); 2629286570Smav } 2630168404Spjd /* prefetch buffers have a minimum lifespan */ 2631275811Sdelphij if (HDR_IO_IN_PROGRESS(hdr) || 2632275811Sdelphij (spa && hdr->b_spa != spa) || 2633286570Smav ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && 2634286570Smav ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < 2635219089Spjd arc_min_prefetch_lifespan)) { 2636168404Spjd skipped++; 2637168404Spjd continue; 2638168404Spjd } 2639168404Spjd /* "lookahead" for better eviction candidate */ 2640275811Sdelphij if (recycle && hdr->b_size != bytes && 2641275811Sdelphij hdr_prev && hdr_prev->b_size == bytes) 2642168404Spjd continue; 2643258632Savg 2644258632Savg /* ignore markers */ 2645275811Sdelphij if (hdr->b_spa == 0) 2646258632Savg continue; 2647258632Savg 2648258632Savg /* 2649258632Savg * It may take a long time to evict all the bufs requested. 2650258632Savg * To avoid blocking all arc activity, periodically drop 2651258632Savg * the arcs_mtx and give other threads a chance to run 2652258632Savg * before reacquiring the lock. 2653258632Savg * 2654258632Savg * If we are looking for a buffer to recycle, we are in 2655258632Savg * the hot code path, so don't sleep. 2656258632Savg */ 2657258632Savg if (!recycle && count++ > arc_evict_iterations) { 2658275811Sdelphij list_insert_after(list, hdr, &marker); 2659286570Smav mutex_exit(lock); 2660258632Savg mutex_exit(evicted_lock); 2661258632Savg kpreempt(KPREEMPT_SYNC); 2662286570Smav mutex_enter(evicted_lock); 2663258632Savg mutex_enter(lock); 2664275811Sdelphij hdr_prev = list_prev(list, &marker); 2665258632Savg list_remove(list, &marker); 2666258632Savg count = 0; 2667258632Savg continue; 2668258632Savg } 2669258632Savg 2670275811Sdelphij hash_lock = HDR_LOCK(hdr); 2671168404Spjd have_lock = MUTEX_HELD(hash_lock); 2672168404Spjd if (have_lock || mutex_tryenter(hash_lock)) { 2673286570Smav ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 2674286570Smav ASSERT3U(hdr->b_l1hdr.b_datacnt, >, 0); 2675286570Smav while (hdr->b_l1hdr.b_buf) { 2676286570Smav arc_buf_t *buf = hdr->b_l1hdr.b_buf; 2677219089Spjd if (!mutex_tryenter(&buf->b_evict_lock)) { 2678185029Spjd missed += 1; 2679185029Spjd break; 2680185029Spjd } 2681286570Smav if (buf->b_data != NULL) { 2682275811Sdelphij bytes_evicted += hdr->b_size; 2683286570Smav if (recycle && 2684286570Smav arc_buf_type(hdr) == type && 2685275811Sdelphij hdr->b_size == bytes && 2686275811Sdelphij !HDR_L2_WRITING(hdr)) { 2687168404Spjd stolen = buf->b_data; 2688168404Spjd recycle = FALSE; 2689168404Spjd } 2690168404Spjd } 2691286570Smav if (buf->b_efunc != NULL) { 2692168404Spjd mutex_enter(&arc_eviction_mtx); 2693168404Spjd arc_buf_destroy(buf, 2694168404Spjd buf->b_data == stolen, FALSE); 2695286570Smav hdr->b_l1hdr.b_buf = buf->b_next; 2696168404Spjd buf->b_hdr = &arc_eviction_hdr; 2697168404Spjd buf->b_next = arc_eviction_list; 2698168404Spjd arc_eviction_list = buf; 2699168404Spjd mutex_exit(&arc_eviction_mtx); 2700219089Spjd mutex_exit(&buf->b_evict_lock); 2701168404Spjd } else { 2702219089Spjd mutex_exit(&buf->b_evict_lock); 2703168404Spjd arc_buf_destroy(buf, 2704168404Spjd buf->b_data == stolen, TRUE); 2705168404Spjd } 2706168404Spjd } 2707208373Smm 2708286570Smav if (HDR_HAS_L2HDR(hdr)) { 2709208373Smm ARCSTAT_INCR(arcstat_evict_l2_cached, 2710275811Sdelphij hdr->b_size); 2711208373Smm } else { 2712275811Sdelphij if (l2arc_write_eligible(hdr->b_spa, hdr)) { 2713208373Smm ARCSTAT_INCR(arcstat_evict_l2_eligible, 2714275811Sdelphij hdr->b_size); 2715208373Smm } else { 2716208373Smm ARCSTAT_INCR( 2717208373Smm arcstat_evict_l2_ineligible, 2718275811Sdelphij hdr->b_size); 2719208373Smm } 2720208373Smm } 2721208373Smm 2722286570Smav if (hdr->b_l1hdr.b_datacnt == 0) { 2723275811Sdelphij arc_change_state(evicted_state, hdr, hash_lock); 2724275811Sdelphij ASSERT(HDR_IN_HASH_TABLE(hdr)); 2725275811Sdelphij hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; 2726275811Sdelphij hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 2727275811Sdelphij DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); 2728185029Spjd } 2729168404Spjd if (!have_lock) 2730168404Spjd mutex_exit(hash_lock); 2731168404Spjd if (bytes >= 0 && bytes_evicted >= bytes) 2732168404Spjd break; 2733205231Skmacy if (bytes_remaining > 0) { 2734205231Skmacy mutex_exit(evicted_lock); 2735205231Skmacy mutex_exit(lock); 2736206796Spjd idx = ((idx + 1) & (list_count - 1)); 2737258632Savg lists++; 2738205231Skmacy goto evict_start; 2739205231Skmacy } 2740168404Spjd } else { 2741168404Spjd missed += 1; 2742168404Spjd } 2743168404Spjd } 2744168404Spjd 2745286570Smav mutex_exit(lock); 2746205231Skmacy mutex_exit(evicted_lock); 2747206796Spjd 2748206796Spjd idx = ((idx + 1) & (list_count - 1)); 2749258632Savg lists++; 2750168404Spjd 2751205231Skmacy if (bytes_evicted < bytes) { 2752258632Savg if (lists < list_count) 2753205231Skmacy goto evict_start; 2754205231Skmacy else 2755205231Skmacy dprintf("only evicted %lld bytes from %x", 2756205231Skmacy (longlong_t)bytes_evicted, state); 2757205231Skmacy } 2758206796Spjd if (type == ARC_BUFC_METADATA) 2759205231Skmacy evict_metadata_offset = idx; 2760205231Skmacy else 2761205231Skmacy evict_data_offset = idx; 2762206796Spjd 2763168404Spjd if (skipped) 2764168404Spjd ARCSTAT_INCR(arcstat_evict_skip, skipped); 2765168404Spjd 2766168404Spjd if (missed) 2767168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, missed); 2768168404Spjd 2769185029Spjd /* 2770258632Savg * Note: we have just evicted some data into the ghost state, 2771258632Savg * potentially putting the ghost size over the desired size. Rather 2772258632Savg * that evicting from the ghost list in this hot code path, leave 2773258632Savg * this chore to the arc_reclaim_thread(). 2774185029Spjd */ 2775185029Spjd 2776205231Skmacy if (stolen) 2777205231Skmacy ARCSTAT_BUMP(arcstat_stolen); 2778168404Spjd return (stolen); 2779168404Spjd} 2780168404Spjd 2781168404Spjd/* 2782168404Spjd * Remove buffers from list until we've removed the specified number of 2783168404Spjd * bytes. Destroy the buffers that are removed. 2784168404Spjd */ 2785168404Spjdstatic void 2786209962Smmarc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 2787168404Spjd{ 2788275811Sdelphij arc_buf_hdr_t *hdr, *hdr_prev; 2789219089Spjd arc_buf_hdr_t marker = { 0 }; 2790205231Skmacy list_t *list, *list_start; 2791205231Skmacy kmutex_t *hash_lock, *lock; 2792168404Spjd uint64_t bytes_deleted = 0; 2793168404Spjd uint64_t bufs_skipped = 0; 2794258632Savg int count = 0; 2795205231Skmacy static int evict_offset; 2796205231Skmacy int list_count, idx = evict_offset; 2797258632Savg int offset, lists = 0; 2798168404Spjd 2799168404Spjd ASSERT(GHOST_STATE(state)); 2800205231Skmacy 2801205231Skmacy /* 2802205231Skmacy * data lists come after metadata lists 2803205231Skmacy */ 2804205231Skmacy list_start = &state->arcs_lists[ARC_BUFC_NUMMETADATALISTS]; 2805205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 2806205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 2807206796Spjd 2808205231Skmacyevict_start: 2809205231Skmacy list = &list_start[idx]; 2810205231Skmacy lock = ARCS_LOCK(state, idx + offset); 2811205231Skmacy 2812205231Skmacy mutex_enter(lock); 2813275811Sdelphij for (hdr = list_tail(list); hdr; hdr = hdr_prev) { 2814275811Sdelphij hdr_prev = list_prev(list, hdr); 2815286570Smav if (arc_buf_type(hdr) >= ARC_BUFC_NUMTYPES) 2816275811Sdelphij panic("invalid hdr=%p", (void *)hdr); 2817275811Sdelphij if (spa && hdr->b_spa != spa) 2818185029Spjd continue; 2819219089Spjd 2820219089Spjd /* ignore markers */ 2821275811Sdelphij if (hdr->b_spa == 0) 2822219089Spjd continue; 2823219089Spjd 2824275811Sdelphij hash_lock = HDR_LOCK(hdr); 2825219089Spjd /* caller may be trying to modify this buffer, skip it */ 2826219089Spjd if (MUTEX_HELD(hash_lock)) 2827219089Spjd continue; 2828258632Savg 2829258632Savg /* 2830258632Savg * It may take a long time to evict all the bufs requested. 2831258632Savg * To avoid blocking all arc activity, periodically drop 2832258632Savg * the arcs_mtx and give other threads a chance to run 2833258632Savg * before reacquiring the lock. 2834258632Savg */ 2835258632Savg if (count++ > arc_evict_iterations) { 2836275811Sdelphij list_insert_after(list, hdr, &marker); 2837258632Savg mutex_exit(lock); 2838258632Savg kpreempt(KPREEMPT_SYNC); 2839258632Savg mutex_enter(lock); 2840275811Sdelphij hdr_prev = list_prev(list, &marker); 2841258632Savg list_remove(list, &marker); 2842258632Savg count = 0; 2843258632Savg continue; 2844258632Savg } 2845168404Spjd if (mutex_tryenter(hash_lock)) { 2846275811Sdelphij ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2847286570Smav ASSERT(!HDR_HAS_L1HDR(hdr) || 2848286570Smav hdr->b_l1hdr.b_buf == NULL); 2849168404Spjd ARCSTAT_BUMP(arcstat_deleted); 2850275811Sdelphij bytes_deleted += hdr->b_size; 2851185029Spjd 2852286570Smav if (HDR_HAS_L2HDR(hdr)) { 2853185029Spjd /* 2854185029Spjd * This buffer is cached on the 2nd Level ARC; 2855185029Spjd * don't destroy the header. 2856185029Spjd */ 2857275811Sdelphij arc_change_state(arc_l2c_only, hdr, hash_lock); 2858286570Smav /* 2859286570Smav * dropping from L1+L2 cached to L2-only, 2860286570Smav * realloc to remove the L1 header. 2861286570Smav */ 2862286570Smav hdr = arc_hdr_realloc(hdr, hdr_full_cache, 2863286570Smav hdr_l2only_cache); 2864185029Spjd mutex_exit(hash_lock); 2865185029Spjd } else { 2866275811Sdelphij arc_change_state(arc_anon, hdr, hash_lock); 2867185029Spjd mutex_exit(hash_lock); 2868275811Sdelphij arc_hdr_destroy(hdr); 2869185029Spjd } 2870185029Spjd 2871275811Sdelphij DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); 2872168404Spjd if (bytes >= 0 && bytes_deleted >= bytes) 2873168404Spjd break; 2874219089Spjd } else if (bytes < 0) { 2875219089Spjd /* 2876219089Spjd * Insert a list marker and then wait for the 2877219089Spjd * hash lock to become available. Once its 2878219089Spjd * available, restart from where we left off. 2879219089Spjd */ 2880275811Sdelphij list_insert_after(list, hdr, &marker); 2881219089Spjd mutex_exit(lock); 2882219089Spjd mutex_enter(hash_lock); 2883219089Spjd mutex_exit(hash_lock); 2884219089Spjd mutex_enter(lock); 2885275811Sdelphij hdr_prev = list_prev(list, &marker); 2886219089Spjd list_remove(list, &marker); 2887258632Savg } else { 2888168404Spjd bufs_skipped += 1; 2889258632Savg } 2890258632Savg 2891168404Spjd } 2892205231Skmacy mutex_exit(lock); 2893206796Spjd idx = ((idx + 1) & (ARC_BUFC_NUMDATALISTS - 1)); 2894258632Savg lists++; 2895206796Spjd 2896258632Savg if (lists < list_count) 2897205231Skmacy goto evict_start; 2898206796Spjd 2899205231Skmacy evict_offset = idx; 2900205231Skmacy if ((uintptr_t)list > (uintptr_t)&state->arcs_lists[ARC_BUFC_NUMMETADATALISTS] && 2901185029Spjd (bytes < 0 || bytes_deleted < bytes)) { 2902205231Skmacy list_start = &state->arcs_lists[0]; 2903205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 2904258632Savg offset = lists = 0; 2905205231Skmacy goto evict_start; 2906185029Spjd } 2907185029Spjd 2908168404Spjd if (bufs_skipped) { 2909168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 2910168404Spjd ASSERT(bytes >= 0); 2911168404Spjd } 2912168404Spjd 2913168404Spjd if (bytes_deleted < bytes) 2914168404Spjd dprintf("only deleted %lld bytes from %p", 2915168404Spjd (longlong_t)bytes_deleted, state); 2916168404Spjd} 2917168404Spjd 2918168404Spjdstatic void 2919168404Spjdarc_adjust(void) 2920168404Spjd{ 2921208373Smm int64_t adjustment, delta; 2922168404Spjd 2923208373Smm /* 2924208373Smm * Adjust MRU size 2925208373Smm */ 2926168404Spjd 2927209275Smm adjustment = MIN((int64_t)(arc_size - arc_c), 2928209275Smm (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - 2929209275Smm arc_p)); 2930208373Smm 2931208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 2932208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 2933209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA); 2934208373Smm adjustment -= delta; 2935168404Spjd } 2936168404Spjd 2937208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2938208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 2939209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, 2940185029Spjd ARC_BUFC_METADATA); 2941185029Spjd } 2942185029Spjd 2943208373Smm /* 2944208373Smm * Adjust MFU size 2945208373Smm */ 2946168404Spjd 2947208373Smm adjustment = arc_size - arc_c; 2948208373Smm 2949208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 2950208373Smm delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 2951209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA); 2952208373Smm adjustment -= delta; 2953168404Spjd } 2954168404Spjd 2955208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2956208373Smm int64_t delta = MIN(adjustment, 2957208373Smm arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 2958209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, 2959208373Smm ARC_BUFC_METADATA); 2960208373Smm } 2961168404Spjd 2962208373Smm /* 2963208373Smm * Adjust ghost lists 2964208373Smm */ 2965168404Spjd 2966208373Smm adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 2967168404Spjd 2968208373Smm if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 2969208373Smm delta = MIN(arc_mru_ghost->arcs_size, adjustment); 2970209962Smm arc_evict_ghost(arc_mru_ghost, 0, delta); 2971208373Smm } 2972185029Spjd 2973208373Smm adjustment = 2974208373Smm arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 2975208373Smm 2976208373Smm if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 2977208373Smm delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 2978209962Smm arc_evict_ghost(arc_mfu_ghost, 0, delta); 2979168404Spjd } 2980168404Spjd} 2981168404Spjd 2982168404Spjdstatic void 2983168404Spjdarc_do_user_evicts(void) 2984168404Spjd{ 2985191903Skmacy static arc_buf_t *tmp_arc_eviction_list; 2986191903Skmacy 2987191903Skmacy /* 2988191903Skmacy * Move list over to avoid LOR 2989191903Skmacy */ 2990206796Spjdrestart: 2991168404Spjd mutex_enter(&arc_eviction_mtx); 2992191903Skmacy tmp_arc_eviction_list = arc_eviction_list; 2993191903Skmacy arc_eviction_list = NULL; 2994191903Skmacy mutex_exit(&arc_eviction_mtx); 2995191903Skmacy 2996191903Skmacy while (tmp_arc_eviction_list != NULL) { 2997191903Skmacy arc_buf_t *buf = tmp_arc_eviction_list; 2998191903Skmacy tmp_arc_eviction_list = buf->b_next; 2999219089Spjd mutex_enter(&buf->b_evict_lock); 3000168404Spjd buf->b_hdr = NULL; 3001219089Spjd mutex_exit(&buf->b_evict_lock); 3002168404Spjd 3003168404Spjd if (buf->b_efunc != NULL) 3004268858Sdelphij VERIFY0(buf->b_efunc(buf->b_private)); 3005168404Spjd 3006168404Spjd buf->b_efunc = NULL; 3007168404Spjd buf->b_private = NULL; 3008168404Spjd kmem_cache_free(buf_cache, buf); 3009168404Spjd } 3010191903Skmacy 3011191903Skmacy if (arc_eviction_list != NULL) 3012191903Skmacy goto restart; 3013168404Spjd} 3014168404Spjd 3015168404Spjd/* 3016185029Spjd * Flush all *evictable* data from the cache for the given spa. 3017168404Spjd * NOTE: this will not touch "active" (i.e. referenced) data. 3018168404Spjd */ 3019168404Spjdvoid 3020185029Spjdarc_flush(spa_t *spa) 3021168404Spjd{ 3022209962Smm uint64_t guid = 0; 3023209962Smm 3024286570Smav if (spa != NULL) 3025228103Smm guid = spa_load_guid(spa); 3026209962Smm 3027205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_DATA]) { 3028209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 3029286570Smav if (spa != NULL) 3030185029Spjd break; 3031185029Spjd } 3032205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_METADATA]) { 3033209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 3034286570Smav if (spa != NULL) 3035185029Spjd break; 3036185029Spjd } 3037205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_DATA]) { 3038209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 3039286570Smav if (spa != NULL) 3040185029Spjd break; 3041185029Spjd } 3042205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_METADATA]) { 3043209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 3044286570Smav if (spa != NULL) 3045185029Spjd break; 3046185029Spjd } 3047168404Spjd 3048209962Smm arc_evict_ghost(arc_mru_ghost, guid, -1); 3049209962Smm arc_evict_ghost(arc_mfu_ghost, guid, -1); 3050168404Spjd 3051168404Spjd mutex_enter(&arc_reclaim_thr_lock); 3052168404Spjd arc_do_user_evicts(); 3053168404Spjd mutex_exit(&arc_reclaim_thr_lock); 3054185029Spjd ASSERT(spa || arc_eviction_list == NULL); 3055168404Spjd} 3056168404Spjd 3057168404Spjdvoid 3058168404Spjdarc_shrink(void) 3059168404Spjd{ 3060270759Ssmh 3061168404Spjd if (arc_c > arc_c_min) { 3062168404Spjd uint64_t to_free; 3063168404Spjd 3064277452Swill to_free = arc_c >> arc_shrink_shift; 3065272483Ssmh DTRACE_PROBE4(arc__shrink, uint64_t, arc_c, uint64_t, 3066272483Ssmh arc_c_min, uint64_t, arc_p, uint64_t, to_free); 3067168404Spjd if (arc_c > arc_c_min + to_free) 3068168404Spjd atomic_add_64(&arc_c, -to_free); 3069168404Spjd else 3070168404Spjd arc_c = arc_c_min; 3071168404Spjd 3072168404Spjd atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 3073168404Spjd if (arc_c > arc_size) 3074168404Spjd arc_c = MAX(arc_size, arc_c_min); 3075168404Spjd if (arc_p > arc_c) 3076168404Spjd arc_p = (arc_c >> 1); 3077272483Ssmh 3078272483Ssmh DTRACE_PROBE2(arc__shrunk, uint64_t, arc_c, uint64_t, 3079272483Ssmh arc_p); 3080272483Ssmh 3081168404Spjd ASSERT(arc_c >= arc_c_min); 3082168404Spjd ASSERT((int64_t)arc_p >= 0); 3083168404Spjd } 3084168404Spjd 3085270759Ssmh if (arc_size > arc_c) { 3086270759Ssmh DTRACE_PROBE2(arc__shrink_adjust, uint64_t, arc_size, 3087270759Ssmh uint64_t, arc_c); 3088168404Spjd arc_adjust(); 3089270759Ssmh } 3090168404Spjd} 3091168404Spjd 3092185029Spjdstatic int needfree = 0; 3093168404Spjd 3094168404Spjdstatic int 3095168404Spjdarc_reclaim_needed(void) 3096168404Spjd{ 3097168404Spjd 3098168404Spjd#ifdef _KERNEL 3099219089Spjd 3100270759Ssmh if (needfree) { 3101270759Ssmh DTRACE_PROBE(arc__reclaim_needfree); 3102197816Skmacy return (1); 3103270759Ssmh } 3104168404Spjd 3105191902Skmacy /* 3106212780Savg * Cooperate with pagedaemon when it's time for it to scan 3107212780Savg * and reclaim some pages. 3108191902Skmacy */ 3109272483Ssmh if (freemem < zfs_arc_free_target) { 3110272483Ssmh DTRACE_PROBE2(arc__reclaim_freemem, uint64_t, 3111272483Ssmh freemem, uint64_t, zfs_arc_free_target); 3112191902Skmacy return (1); 3113270759Ssmh } 3114191902Skmacy 3115277300Ssmh#ifdef illumos 3116168404Spjd /* 3117185029Spjd * take 'desfree' extra pages, so we reclaim sooner, rather than later 3118185029Spjd */ 3119185029Spjd extra = desfree; 3120185029Spjd 3121185029Spjd /* 3122185029Spjd * check that we're out of range of the pageout scanner. It starts to 3123185029Spjd * schedule paging if freemem is less than lotsfree and needfree. 3124185029Spjd * lotsfree is the high-water mark for pageout, and needfree is the 3125185029Spjd * number of needed free pages. We add extra pages here to make sure 3126185029Spjd * the scanner doesn't start up while we're freeing memory. 3127185029Spjd */ 3128185029Spjd if (freemem < lotsfree + needfree + extra) 3129185029Spjd return (1); 3130185029Spjd 3131185029Spjd /* 3132168404Spjd * check to make sure that swapfs has enough space so that anon 3133185029Spjd * reservations can still succeed. anon_resvmem() checks that the 3134168404Spjd * availrmem is greater than swapfs_minfree, and the number of reserved 3135168404Spjd * swap pages. We also add a bit of extra here just to prevent 3136168404Spjd * circumstances from getting really dire. 3137168404Spjd */ 3138168404Spjd if (availrmem < swapfs_minfree + swapfs_reserve + extra) 3139168404Spjd return (1); 3140168404Spjd 3141168404Spjd /* 3142272483Ssmh * Check that we have enough availrmem that memory locking (e.g., via 3143272483Ssmh * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 3144272483Ssmh * stores the number of pages that cannot be locked; when availrmem 3145272483Ssmh * drops below pages_pp_maximum, page locking mechanisms such as 3146272483Ssmh * page_pp_lock() will fail.) 3147272483Ssmh */ 3148272483Ssmh if (availrmem <= pages_pp_maximum) 3149272483Ssmh return (1); 3150272483Ssmh 3151277300Ssmh#endif /* illumos */ 3152272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 3153272483Ssmh /* 3154168404Spjd * If we're on an i386 platform, it's possible that we'll exhaust the 3155168404Spjd * kernel heap space before we ever run out of available physical 3156168404Spjd * memory. Most checks of the size of the heap_area compare against 3157168404Spjd * tune.t_minarmem, which is the minimum available real memory that we 3158168404Spjd * can have in the system. However, this is generally fixed at 25 pages 3159168404Spjd * which is so low that it's useless. In this comparison, we seek to 3160168404Spjd * calculate the total heap-size, and reclaim if more than 3/4ths of the 3161185029Spjd * heap is allocated. (Or, in the calculation, if less than 1/4th is 3162168404Spjd * free) 3163168404Spjd */ 3164272483Ssmh if (vmem_size(heap_arena, VMEM_FREE) < 3165272483Ssmh (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2)) { 3166270861Ssmh DTRACE_PROBE2(arc__reclaim_used, uint64_t, 3167272483Ssmh vmem_size(heap_arena, VMEM_FREE), uint64_t, 3168272483Ssmh (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2); 3169270861Ssmh return (1); 3170270861Ssmh } 3171281026Smav#define zio_arena NULL 3172281026Smav#else 3173281026Smav#define zio_arena heap_arena 3174270861Ssmh#endif 3175281026Smav 3176272483Ssmh /* 3177272483Ssmh * If zio data pages are being allocated out of a separate heap segment, 3178272483Ssmh * then enforce that the size of available vmem for this arena remains 3179272483Ssmh * above about 1/16th free. 3180272483Ssmh * 3181272483Ssmh * Note: The 1/16th arena free requirement was put in place 3182272483Ssmh * to aggressively evict memory from the arc in order to avoid 3183272483Ssmh * memory fragmentation issues. 3184272483Ssmh */ 3185272483Ssmh if (zio_arena != NULL && 3186272483Ssmh vmem_size(zio_arena, VMEM_FREE) < 3187272483Ssmh (vmem_size(zio_arena, VMEM_ALLOC) >> 4)) 3188272483Ssmh return (1); 3189281026Smav 3190281026Smav /* 3191281026Smav * Above limits know nothing about real level of KVA fragmentation. 3192281026Smav * Start aggressive reclamation if too little sequential KVA left. 3193281026Smav */ 3194281109Smav if (vmem_size(heap_arena, VMEM_MAXFREE) < zfs_max_recordsize) { 3195281109Smav DTRACE_PROBE2(arc__reclaim_maxfree, uint64_t, 3196281109Smav vmem_size(heap_arena, VMEM_MAXFREE), 3197281109Smav uint64_t, zfs_max_recordsize); 3198281026Smav return (1); 3199281109Smav } 3200281026Smav 3201272483Ssmh#else /* _KERNEL */ 3202168404Spjd if (spa_get_random(100) == 0) 3203168404Spjd return (1); 3204272483Ssmh#endif /* _KERNEL */ 3205270759Ssmh DTRACE_PROBE(arc__reclaim_no); 3206270759Ssmh 3207168404Spjd return (0); 3208168404Spjd} 3209168404Spjd 3210208454Spjdextern kmem_cache_t *zio_buf_cache[]; 3211208454Spjdextern kmem_cache_t *zio_data_buf_cache[]; 3212272527Sdelphijextern kmem_cache_t *range_seg_cache; 3213208454Spjd 3214278040Ssmhstatic __noinline void 3215168404Spjdarc_kmem_reap_now(arc_reclaim_strategy_t strat) 3216168404Spjd{ 3217168404Spjd size_t i; 3218168404Spjd kmem_cache_t *prev_cache = NULL; 3219168404Spjd kmem_cache_t *prev_data_cache = NULL; 3220168404Spjd 3221272483Ssmh DTRACE_PROBE(arc__kmem_reap_start); 3222168404Spjd#ifdef _KERNEL 3223185029Spjd if (arc_meta_used >= arc_meta_limit) { 3224185029Spjd /* 3225185029Spjd * We are exceeding our meta-data cache limit. 3226185029Spjd * Purge some DNLC entries to release holds on meta-data. 3227185029Spjd */ 3228185029Spjd dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 3229185029Spjd } 3230168404Spjd#if defined(__i386) 3231168404Spjd /* 3232168404Spjd * Reclaim unused memory from all kmem caches. 3233168404Spjd */ 3234168404Spjd kmem_reap(); 3235168404Spjd#endif 3236168404Spjd#endif 3237168404Spjd 3238168404Spjd /* 3239185029Spjd * An aggressive reclamation will shrink the cache size as well as 3240168404Spjd * reap free buffers from the arc kmem caches. 3241168404Spjd */ 3242168404Spjd if (strat == ARC_RECLAIM_AGGR) 3243168404Spjd arc_shrink(); 3244168404Spjd 3245168404Spjd for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 3246168404Spjd if (zio_buf_cache[i] != prev_cache) { 3247168404Spjd prev_cache = zio_buf_cache[i]; 3248168404Spjd kmem_cache_reap_now(zio_buf_cache[i]); 3249168404Spjd } 3250168404Spjd if (zio_data_buf_cache[i] != prev_data_cache) { 3251168404Spjd prev_data_cache = zio_data_buf_cache[i]; 3252168404Spjd kmem_cache_reap_now(zio_data_buf_cache[i]); 3253168404Spjd } 3254168404Spjd } 3255168404Spjd kmem_cache_reap_now(buf_cache); 3256286570Smav kmem_cache_reap_now(hdr_full_cache); 3257286570Smav kmem_cache_reap_now(hdr_l2only_cache); 3258272506Sdelphij kmem_cache_reap_now(range_seg_cache); 3259272483Ssmh 3260277300Ssmh#ifdef illumos 3261272483Ssmh /* 3262272483Ssmh * Ask the vmem arena to reclaim unused memory from its 3263272483Ssmh * quantum caches. 3264272483Ssmh */ 3265272483Ssmh if (zio_arena != NULL && strat == ARC_RECLAIM_AGGR) 3266272483Ssmh vmem_qcache_reap(zio_arena); 3267272483Ssmh#endif 3268272483Ssmh DTRACE_PROBE(arc__kmem_reap_end); 3269168404Spjd} 3270168404Spjd 3271168404Spjdstatic void 3272168404Spjdarc_reclaim_thread(void *dummy __unused) 3273168404Spjd{ 3274168404Spjd clock_t growtime = 0; 3275168404Spjd arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 3276168404Spjd callb_cpr_t cpr; 3277168404Spjd 3278168404Spjd CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 3279168404Spjd 3280168404Spjd mutex_enter(&arc_reclaim_thr_lock); 3281168404Spjd while (arc_thread_exit == 0) { 3282168404Spjd if (arc_reclaim_needed()) { 3283168404Spjd 3284168404Spjd if (arc_no_grow) { 3285168404Spjd if (last_reclaim == ARC_RECLAIM_CONS) { 3286272483Ssmh DTRACE_PROBE(arc__reclaim_aggr_no_grow); 3287168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 3288168404Spjd } else { 3289168404Spjd last_reclaim = ARC_RECLAIM_CONS; 3290168404Spjd } 3291168404Spjd } else { 3292168404Spjd arc_no_grow = TRUE; 3293168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 3294272483Ssmh DTRACE_PROBE(arc__reclaim_aggr); 3295168404Spjd membar_producer(); 3296168404Spjd } 3297168404Spjd 3298168404Spjd /* reset the growth delay for every reclaim */ 3299219089Spjd growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 3300168404Spjd 3301185029Spjd if (needfree && last_reclaim == ARC_RECLAIM_CONS) { 3302168404Spjd /* 3303185029Spjd * If needfree is TRUE our vm_lowmem hook 3304168404Spjd * was called and in that case we must free some 3305168404Spjd * memory, so switch to aggressive mode. 3306168404Spjd */ 3307168404Spjd arc_no_grow = TRUE; 3308168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 3309168404Spjd } 3310168404Spjd arc_kmem_reap_now(last_reclaim); 3311185029Spjd arc_warm = B_TRUE; 3312185029Spjd 3313219089Spjd } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 3314168404Spjd arc_no_grow = FALSE; 3315168404Spjd } 3316168404Spjd 3317209275Smm arc_adjust(); 3318168404Spjd 3319168404Spjd if (arc_eviction_list != NULL) 3320168404Spjd arc_do_user_evicts(); 3321168404Spjd 3322211762Savg#ifdef _KERNEL 3323211762Savg if (needfree) { 3324185029Spjd needfree = 0; 3325185029Spjd wakeup(&needfree); 3326211762Savg } 3327168404Spjd#endif 3328168404Spjd 3329286574Smav /* 3330286574Smav * This is necessary in order for the mdb ::arc dcmd to 3331286574Smav * show up to date information. Since the ::arc command 3332286574Smav * does not call the kstat's update function, without 3333286574Smav * this call, the command may show stale stats for the 3334286574Smav * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even 3335286574Smav * with this change, the data might be up to 1 second 3336286574Smav * out of date; but that should suffice. The arc_state_t 3337286574Smav * structures can be queried directly if more accurate 3338286574Smav * information is needed. 3339286574Smav */ 3340286574Smav if (arc_ksp != NULL) 3341286574Smav arc_ksp->ks_update(arc_ksp, KSTAT_READ); 3342286574Smav 3343168404Spjd /* block until needed, or one second, whichever is shorter */ 3344168404Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 3345168404Spjd (void) cv_timedwait(&arc_reclaim_thr_cv, 3346168404Spjd &arc_reclaim_thr_lock, hz); 3347168404Spjd CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 3348168404Spjd } 3349168404Spjd 3350168404Spjd arc_thread_exit = 0; 3351168404Spjd cv_broadcast(&arc_reclaim_thr_cv); 3352168404Spjd CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 3353168404Spjd thread_exit(); 3354168404Spjd} 3355168404Spjd 3356168404Spjd/* 3357168404Spjd * Adapt arc info given the number of bytes we are trying to add and 3358168404Spjd * the state that we are comming from. This function is only called 3359168404Spjd * when we are adding new content to the cache. 3360168404Spjd */ 3361168404Spjdstatic void 3362168404Spjdarc_adapt(int bytes, arc_state_t *state) 3363168404Spjd{ 3364168404Spjd int mult; 3365208373Smm uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 3366168404Spjd 3367185029Spjd if (state == arc_l2c_only) 3368185029Spjd return; 3369185029Spjd 3370168404Spjd ASSERT(bytes > 0); 3371168404Spjd /* 3372168404Spjd * Adapt the target size of the MRU list: 3373168404Spjd * - if we just hit in the MRU ghost list, then increase 3374168404Spjd * the target size of the MRU list. 3375168404Spjd * - if we just hit in the MFU ghost list, then increase 3376168404Spjd * the target size of the MFU list by decreasing the 3377168404Spjd * target size of the MRU list. 3378168404Spjd */ 3379168404Spjd if (state == arc_mru_ghost) { 3380168404Spjd mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 3381168404Spjd 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 3382209275Smm mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 3383168404Spjd 3384208373Smm arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 3385168404Spjd } else if (state == arc_mfu_ghost) { 3386208373Smm uint64_t delta; 3387208373Smm 3388168404Spjd mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 3389168404Spjd 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 3390209275Smm mult = MIN(mult, 10); 3391168404Spjd 3392208373Smm delta = MIN(bytes * mult, arc_p); 3393208373Smm arc_p = MAX(arc_p_min, arc_p - delta); 3394168404Spjd } 3395168404Spjd ASSERT((int64_t)arc_p >= 0); 3396168404Spjd 3397168404Spjd if (arc_reclaim_needed()) { 3398168404Spjd cv_signal(&arc_reclaim_thr_cv); 3399168404Spjd return; 3400168404Spjd } 3401168404Spjd 3402168404Spjd if (arc_no_grow) 3403168404Spjd return; 3404168404Spjd 3405168404Spjd if (arc_c >= arc_c_max) 3406168404Spjd return; 3407168404Spjd 3408168404Spjd /* 3409168404Spjd * If we're within (2 * maxblocksize) bytes of the target 3410168404Spjd * cache size, increment the target cache size 3411168404Spjd */ 3412168404Spjd if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 3413272483Ssmh DTRACE_PROBE1(arc__inc_adapt, int, bytes); 3414168404Spjd atomic_add_64(&arc_c, (int64_t)bytes); 3415168404Spjd if (arc_c > arc_c_max) 3416168404Spjd arc_c = arc_c_max; 3417168404Spjd else if (state == arc_anon) 3418168404Spjd atomic_add_64(&arc_p, (int64_t)bytes); 3419168404Spjd if (arc_p > arc_c) 3420168404Spjd arc_p = arc_c; 3421168404Spjd } 3422168404Spjd ASSERT((int64_t)arc_p >= 0); 3423168404Spjd} 3424168404Spjd 3425168404Spjd/* 3426168404Spjd * Check if the cache has reached its limits and eviction is required 3427168404Spjd * prior to insert. 3428168404Spjd */ 3429168404Spjdstatic int 3430185029Spjdarc_evict_needed(arc_buf_contents_t type) 3431168404Spjd{ 3432185029Spjd if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 3433185029Spjd return (1); 3434185029Spjd 3435168404Spjd if (arc_reclaim_needed()) 3436168404Spjd return (1); 3437168404Spjd 3438168404Spjd return (arc_size > arc_c); 3439168404Spjd} 3440168404Spjd 3441168404Spjd/* 3442168404Spjd * The buffer, supplied as the first argument, needs a data block. 3443168404Spjd * So, if we are at cache max, determine which cache should be victimized. 3444168404Spjd * We have the following cases: 3445168404Spjd * 3446168404Spjd * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 3447168404Spjd * In this situation if we're out of space, but the resident size of the MFU is 3448168404Spjd * under the limit, victimize the MFU cache to satisfy this insertion request. 3449168404Spjd * 3450168404Spjd * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 3451168404Spjd * Here, we've used up all of the available space for the MRU, so we need to 3452168404Spjd * evict from our own cache instead. Evict from the set of resident MRU 3453168404Spjd * entries. 3454168404Spjd * 3455168404Spjd * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 3456168404Spjd * c minus p represents the MFU space in the cache, since p is the size of the 3457168404Spjd * cache that is dedicated to the MRU. In this situation there's still space on 3458168404Spjd * the MFU side, so the MRU side needs to be victimized. 3459168404Spjd * 3460168404Spjd * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 3461168404Spjd * MFU's resident set is consuming more space than it has been allotted. In 3462168404Spjd * this situation, we must victimize our own cache, the MFU, for this insertion. 3463168404Spjd */ 3464168404Spjdstatic void 3465168404Spjdarc_get_data_buf(arc_buf_t *buf) 3466168404Spjd{ 3467286570Smav arc_state_t *state = buf->b_hdr->b_l1hdr.b_state; 3468168404Spjd uint64_t size = buf->b_hdr->b_size; 3469286570Smav arc_buf_contents_t type = arc_buf_type(buf->b_hdr); 3470168404Spjd 3471168404Spjd arc_adapt(size, state); 3472168404Spjd 3473168404Spjd /* 3474168404Spjd * We have not yet reached cache maximum size, 3475168404Spjd * just allocate a new buffer. 3476168404Spjd */ 3477185029Spjd if (!arc_evict_needed(type)) { 3478168404Spjd if (type == ARC_BUFC_METADATA) { 3479168404Spjd buf->b_data = zio_buf_alloc(size); 3480286574Smav arc_space_consume(size, ARC_SPACE_META); 3481168404Spjd } else { 3482168404Spjd ASSERT(type == ARC_BUFC_DATA); 3483168404Spjd buf->b_data = zio_data_buf_alloc(size); 3484286574Smav arc_space_consume(size, ARC_SPACE_DATA); 3485168404Spjd } 3486168404Spjd goto out; 3487168404Spjd } 3488168404Spjd 3489168404Spjd /* 3490168404Spjd * If we are prefetching from the mfu ghost list, this buffer 3491168404Spjd * will end up on the mru list; so steal space from there. 3492168404Spjd */ 3493168404Spjd if (state == arc_mfu_ghost) 3494286570Smav state = HDR_PREFETCH(buf->b_hdr) ? arc_mru : arc_mfu; 3495168404Spjd else if (state == arc_mru_ghost) 3496168404Spjd state = arc_mru; 3497168404Spjd 3498168404Spjd if (state == arc_mru || state == arc_anon) { 3499168404Spjd uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 3500208373Smm state = (arc_mfu->arcs_lsize[type] >= size && 3501185029Spjd arc_p > mru_used) ? arc_mfu : arc_mru; 3502168404Spjd } else { 3503168404Spjd /* MFU cases */ 3504168404Spjd uint64_t mfu_space = arc_c - arc_p; 3505208373Smm state = (arc_mru->arcs_lsize[type] >= size && 3506185029Spjd mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 3507168404Spjd } 3508209962Smm if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) { 3509168404Spjd if (type == ARC_BUFC_METADATA) { 3510168404Spjd buf->b_data = zio_buf_alloc(size); 3511286574Smav arc_space_consume(size, ARC_SPACE_META); 3512168404Spjd } else { 3513168404Spjd ASSERT(type == ARC_BUFC_DATA); 3514168404Spjd buf->b_data = zio_data_buf_alloc(size); 3515286574Smav arc_space_consume(size, ARC_SPACE_DATA); 3516168404Spjd } 3517168404Spjd ARCSTAT_BUMP(arcstat_recycle_miss); 3518168404Spjd } 3519168404Spjd ASSERT(buf->b_data != NULL); 3520168404Spjdout: 3521168404Spjd /* 3522168404Spjd * Update the state size. Note that ghost states have a 3523168404Spjd * "ghost size" and so don't need to be updated. 3524168404Spjd */ 3525286570Smav if (!GHOST_STATE(buf->b_hdr->b_l1hdr.b_state)) { 3526168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3527168404Spjd 3528286570Smav atomic_add_64(&hdr->b_l1hdr.b_state->arcs_size, size); 3529286570Smav if (list_link_active(&hdr->b_l1hdr.b_arc_node)) { 3530286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 3531286570Smav atomic_add_64(&hdr->b_l1hdr.b_state->arcs_lsize[type], 3532286570Smav size); 3533168404Spjd } 3534168404Spjd /* 3535168404Spjd * If we are growing the cache, and we are adding anonymous 3536168404Spjd * data, and we have outgrown arc_p, update arc_p 3537168404Spjd */ 3538286570Smav if (arc_size < arc_c && hdr->b_l1hdr.b_state == arc_anon && 3539168404Spjd arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 3540168404Spjd arc_p = MIN(arc_c, arc_p + size); 3541168404Spjd } 3542205231Skmacy ARCSTAT_BUMP(arcstat_allocated); 3543168404Spjd} 3544168404Spjd 3545168404Spjd/* 3546168404Spjd * This routine is called whenever a buffer is accessed. 3547168404Spjd * NOTE: the hash lock is dropped in this function. 3548168404Spjd */ 3549168404Spjdstatic void 3550275811Sdelphijarc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 3551168404Spjd{ 3552219089Spjd clock_t now; 3553219089Spjd 3554168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 3555286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 3556168404Spjd 3557286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 3558168404Spjd /* 3559168404Spjd * This buffer is not in the cache, and does not 3560168404Spjd * appear in our "ghost" list. Add the new buffer 3561168404Spjd * to the MRU state. 3562168404Spjd */ 3563168404Spjd 3564286570Smav ASSERT0(hdr->b_l1hdr.b_arc_access); 3565286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3566275811Sdelphij DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 3567275811Sdelphij arc_change_state(arc_mru, hdr, hash_lock); 3568168404Spjd 3569286570Smav } else if (hdr->b_l1hdr.b_state == arc_mru) { 3570219089Spjd now = ddi_get_lbolt(); 3571219089Spjd 3572168404Spjd /* 3573168404Spjd * If this buffer is here because of a prefetch, then either: 3574168404Spjd * - clear the flag if this is a "referencing" read 3575168404Spjd * (any subsequent access will bump this into the MFU state). 3576168404Spjd * or 3577168404Spjd * - move the buffer to the head of the list if this is 3578168404Spjd * another prefetch (to make it less likely to be evicted). 3579168404Spjd */ 3580286570Smav if (HDR_PREFETCH(hdr)) { 3581286570Smav if (refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 3582286570Smav ASSERT(list_link_active( 3583286570Smav &hdr->b_l1hdr.b_arc_node)); 3584168404Spjd } else { 3585275811Sdelphij hdr->b_flags &= ~ARC_FLAG_PREFETCH; 3586168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 3587168404Spjd } 3588286570Smav hdr->b_l1hdr.b_arc_access = now; 3589168404Spjd return; 3590168404Spjd } 3591168404Spjd 3592168404Spjd /* 3593168404Spjd * This buffer has been "accessed" only once so far, 3594168404Spjd * but it is still in the cache. Move it to the MFU 3595168404Spjd * state. 3596168404Spjd */ 3597286570Smav if (now > hdr->b_l1hdr.b_arc_access + ARC_MINTIME) { 3598168404Spjd /* 3599168404Spjd * More than 125ms have passed since we 3600168404Spjd * instantiated this buffer. Move it to the 3601168404Spjd * most frequently used state. 3602168404Spjd */ 3603286570Smav hdr->b_l1hdr.b_arc_access = now; 3604275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3605275811Sdelphij arc_change_state(arc_mfu, hdr, hash_lock); 3606168404Spjd } 3607168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 3608286570Smav } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { 3609168404Spjd arc_state_t *new_state; 3610168404Spjd /* 3611168404Spjd * This buffer has been "accessed" recently, but 3612168404Spjd * was evicted from the cache. Move it to the 3613168404Spjd * MFU state. 3614168404Spjd */ 3615168404Spjd 3616286570Smav if (HDR_PREFETCH(hdr)) { 3617168404Spjd new_state = arc_mru; 3618286570Smav if (refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) 3619275811Sdelphij hdr->b_flags &= ~ARC_FLAG_PREFETCH; 3620275811Sdelphij DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 3621168404Spjd } else { 3622168404Spjd new_state = arc_mfu; 3623275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3624168404Spjd } 3625168404Spjd 3626286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3627275811Sdelphij arc_change_state(new_state, hdr, hash_lock); 3628168404Spjd 3629168404Spjd ARCSTAT_BUMP(arcstat_mru_ghost_hits); 3630286570Smav } else if (hdr->b_l1hdr.b_state == arc_mfu) { 3631168404Spjd /* 3632168404Spjd * This buffer has been accessed more than once and is 3633168404Spjd * still in the cache. Keep it in the MFU state. 3634168404Spjd * 3635168404Spjd * NOTE: an add_reference() that occurred when we did 3636168404Spjd * the arc_read() will have kicked this off the list. 3637168404Spjd * If it was a prefetch, we will explicitly move it to 3638168404Spjd * the head of the list now. 3639168404Spjd */ 3640286570Smav if ((HDR_PREFETCH(hdr)) != 0) { 3641286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 3642286570Smav ASSERT(list_link_active(&hdr->b_l1hdr.b_arc_node)); 3643168404Spjd } 3644168404Spjd ARCSTAT_BUMP(arcstat_mfu_hits); 3645286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3646286570Smav } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { 3647168404Spjd arc_state_t *new_state = arc_mfu; 3648168404Spjd /* 3649168404Spjd * This buffer has been accessed more than once but has 3650168404Spjd * been evicted from the cache. Move it back to the 3651168404Spjd * MFU state. 3652168404Spjd */ 3653168404Spjd 3654286570Smav if (HDR_PREFETCH(hdr)) { 3655168404Spjd /* 3656168404Spjd * This is a prefetch access... 3657168404Spjd * move this block back to the MRU state. 3658168404Spjd */ 3659286570Smav ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 3660168404Spjd new_state = arc_mru; 3661168404Spjd } 3662168404Spjd 3663286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3664275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3665275811Sdelphij arc_change_state(new_state, hdr, hash_lock); 3666168404Spjd 3667168404Spjd ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 3668286570Smav } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { 3669185029Spjd /* 3670185029Spjd * This buffer is on the 2nd Level ARC. 3671185029Spjd */ 3672185029Spjd 3673286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3674275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3675275811Sdelphij arc_change_state(arc_mfu, hdr, hash_lock); 3676168404Spjd } else { 3677168404Spjd ASSERT(!"invalid arc state"); 3678168404Spjd } 3679168404Spjd} 3680168404Spjd 3681168404Spjd/* a generic arc_done_func_t which you can use */ 3682168404Spjd/* ARGSUSED */ 3683168404Spjdvoid 3684168404Spjdarc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 3685168404Spjd{ 3686219089Spjd if (zio == NULL || zio->io_error == 0) 3687219089Spjd bcopy(buf->b_data, arg, buf->b_hdr->b_size); 3688248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 3689168404Spjd} 3690168404Spjd 3691185029Spjd/* a generic arc_done_func_t */ 3692168404Spjdvoid 3693168404Spjdarc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 3694168404Spjd{ 3695168404Spjd arc_buf_t **bufp = arg; 3696168404Spjd if (zio && zio->io_error) { 3697248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 3698168404Spjd *bufp = NULL; 3699168404Spjd } else { 3700168404Spjd *bufp = buf; 3701219089Spjd ASSERT(buf->b_data); 3702168404Spjd } 3703168404Spjd} 3704168404Spjd 3705168404Spjdstatic void 3706168404Spjdarc_read_done(zio_t *zio) 3707168404Spjd{ 3708268075Sdelphij arc_buf_hdr_t *hdr; 3709168404Spjd arc_buf_t *buf; 3710168404Spjd arc_buf_t *abuf; /* buffer we're assigning to callback */ 3711268075Sdelphij kmutex_t *hash_lock = NULL; 3712168404Spjd arc_callback_t *callback_list, *acb; 3713168404Spjd int freeable = FALSE; 3714168404Spjd 3715168404Spjd buf = zio->io_private; 3716168404Spjd hdr = buf->b_hdr; 3717168404Spjd 3718168404Spjd /* 3719168404Spjd * The hdr was inserted into hash-table and removed from lists 3720168404Spjd * prior to starting I/O. We should find this header, since 3721168404Spjd * it's in the hash table, and it should be legit since it's 3722168404Spjd * not possible to evict it during the I/O. The only possible 3723168404Spjd * reason for it not to be found is if we were freed during the 3724168404Spjd * read. 3725168404Spjd */ 3726268075Sdelphij if (HDR_IN_HASH_TABLE(hdr)) { 3727268075Sdelphij ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 3728268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[0], ==, 3729268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[0]); 3730268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[1], ==, 3731268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[1]); 3732168404Spjd 3733268075Sdelphij arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 3734268075Sdelphij &hash_lock); 3735168404Spjd 3736268075Sdelphij ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && 3737268075Sdelphij hash_lock == NULL) || 3738268075Sdelphij (found == hdr && 3739268075Sdelphij DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 3740268075Sdelphij (found == hdr && HDR_L2_READING(hdr))); 3741268075Sdelphij } 3742268075Sdelphij 3743275811Sdelphij hdr->b_flags &= ~ARC_FLAG_L2_EVICTED; 3744286570Smav if (l2arc_noprefetch && HDR_PREFETCH(hdr)) 3745275811Sdelphij hdr->b_flags &= ~ARC_FLAG_L2CACHE; 3746206796Spjd 3747168404Spjd /* byteswap if necessary */ 3748286570Smav callback_list = hdr->b_l1hdr.b_acb; 3749168404Spjd ASSERT(callback_list != NULL); 3750209101Smm if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 3751236884Smm dmu_object_byteswap_t bswap = 3752236884Smm DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 3753185029Spjd arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 3754185029Spjd byteswap_uint64_array : 3755236884Smm dmu_ot_byteswap[bswap].ob_func; 3756185029Spjd func(buf->b_data, hdr->b_size); 3757185029Spjd } 3758168404Spjd 3759185029Spjd arc_cksum_compute(buf, B_FALSE); 3760240133Smm#ifdef illumos 3761240133Smm arc_buf_watch(buf); 3762277300Ssmh#endif 3763168404Spjd 3764286570Smav if (hash_lock && zio->io_error == 0 && 3765286570Smav hdr->b_l1hdr.b_state == arc_anon) { 3766219089Spjd /* 3767219089Spjd * Only call arc_access on anonymous buffers. This is because 3768219089Spjd * if we've issued an I/O for an evicted buffer, we've already 3769219089Spjd * called arc_access (to prevent any simultaneous readers from 3770219089Spjd * getting confused). 3771219089Spjd */ 3772219089Spjd arc_access(hdr, hash_lock); 3773219089Spjd } 3774219089Spjd 3775168404Spjd /* create copies of the data buffer for the callers */ 3776168404Spjd abuf = buf; 3777168404Spjd for (acb = callback_list; acb; acb = acb->acb_next) { 3778168404Spjd if (acb->acb_done) { 3779242845Sdelphij if (abuf == NULL) { 3780242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_reads); 3781168404Spjd abuf = arc_buf_clone(buf); 3782242845Sdelphij } 3783168404Spjd acb->acb_buf = abuf; 3784168404Spjd abuf = NULL; 3785168404Spjd } 3786168404Spjd } 3787286570Smav hdr->b_l1hdr.b_acb = NULL; 3788275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 3789168404Spjd ASSERT(!HDR_BUF_AVAILABLE(hdr)); 3790219089Spjd if (abuf == buf) { 3791219089Spjd ASSERT(buf->b_efunc == NULL); 3792286570Smav ASSERT(hdr->b_l1hdr.b_datacnt == 1); 3793275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 3794219089Spjd } 3795168404Spjd 3796286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || 3797286570Smav callback_list != NULL); 3798168404Spjd 3799168404Spjd if (zio->io_error != 0) { 3800275811Sdelphij hdr->b_flags |= ARC_FLAG_IO_ERROR; 3801286570Smav if (hdr->b_l1hdr.b_state != arc_anon) 3802168404Spjd arc_change_state(arc_anon, hdr, hash_lock); 3803168404Spjd if (HDR_IN_HASH_TABLE(hdr)) 3804168404Spjd buf_hash_remove(hdr); 3805286570Smav freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 3806168404Spjd } 3807168404Spjd 3808168404Spjd /* 3809168404Spjd * Broadcast before we drop the hash_lock to avoid the possibility 3810168404Spjd * that the hdr (and hence the cv) might be freed before we get to 3811168404Spjd * the cv_broadcast(). 3812168404Spjd */ 3813286570Smav cv_broadcast(&hdr->b_l1hdr.b_cv); 3814168404Spjd 3815286570Smav if (hash_lock != NULL) { 3816168404Spjd mutex_exit(hash_lock); 3817168404Spjd } else { 3818168404Spjd /* 3819168404Spjd * This block was freed while we waited for the read to 3820168404Spjd * complete. It has been removed from the hash table and 3821168404Spjd * moved to the anonymous state (so that it won't show up 3822168404Spjd * in the cache). 3823168404Spjd */ 3824286570Smav ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 3825286570Smav freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 3826168404Spjd } 3827168404Spjd 3828168404Spjd /* execute each callback and free its structure */ 3829168404Spjd while ((acb = callback_list) != NULL) { 3830168404Spjd if (acb->acb_done) 3831168404Spjd acb->acb_done(zio, acb->acb_buf, acb->acb_private); 3832168404Spjd 3833168404Spjd if (acb->acb_zio_dummy != NULL) { 3834168404Spjd acb->acb_zio_dummy->io_error = zio->io_error; 3835168404Spjd zio_nowait(acb->acb_zio_dummy); 3836168404Spjd } 3837168404Spjd 3838168404Spjd callback_list = acb->acb_next; 3839168404Spjd kmem_free(acb, sizeof (arc_callback_t)); 3840168404Spjd } 3841168404Spjd 3842168404Spjd if (freeable) 3843168404Spjd arc_hdr_destroy(hdr); 3844168404Spjd} 3845168404Spjd 3846168404Spjd/* 3847168404Spjd * "Read" the block block at the specified DVA (in bp) via the 3848168404Spjd * cache. If the block is found in the cache, invoke the provided 3849168404Spjd * callback immediately and return. Note that the `zio' parameter 3850168404Spjd * in the callback will be NULL in this case, since no IO was 3851168404Spjd * required. If the block is not in the cache pass the read request 3852168404Spjd * on to the spa with a substitute callback function, so that the 3853168404Spjd * requested block will be added to the cache. 3854168404Spjd * 3855168404Spjd * If a read request arrives for a block that has a read in-progress, 3856168404Spjd * either wait for the in-progress read to complete (and return the 3857168404Spjd * results); or, if this is a read with a "done" func, add a record 3858168404Spjd * to the read to invoke the "done" func when the read completes, 3859168404Spjd * and return; or just return. 3860168404Spjd * 3861168404Spjd * arc_read_done() will invoke all the requested "done" functions 3862168404Spjd * for readers of this block. 3863168404Spjd */ 3864168404Spjdint 3865246666Smmarc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 3866275811Sdelphij void *private, zio_priority_t priority, int zio_flags, 3867275811Sdelphij arc_flags_t *arc_flags, const zbookmark_phys_t *zb) 3868168404Spjd{ 3869268075Sdelphij arc_buf_hdr_t *hdr = NULL; 3870247187Smm arc_buf_t *buf = NULL; 3871268075Sdelphij kmutex_t *hash_lock = NULL; 3872185029Spjd zio_t *rzio; 3873228103Smm uint64_t guid = spa_load_guid(spa); 3874168404Spjd 3875268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp) || 3876268075Sdelphij BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 3877268075Sdelphij 3878168404Spjdtop: 3879268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 3880268075Sdelphij /* 3881268075Sdelphij * Embedded BP's have no DVA and require no I/O to "read". 3882268075Sdelphij * Create an anonymous arc buf to back it. 3883268075Sdelphij */ 3884268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 3885268075Sdelphij } 3886168404Spjd 3887286570Smav if (hdr != NULL && HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_datacnt > 0) { 3888268075Sdelphij 3889275811Sdelphij *arc_flags |= ARC_FLAG_CACHED; 3890168404Spjd 3891168404Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3892168404Spjd 3893275811Sdelphij if (*arc_flags & ARC_FLAG_WAIT) { 3894286570Smav cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); 3895168404Spjd mutex_exit(hash_lock); 3896168404Spjd goto top; 3897168404Spjd } 3898275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 3899168404Spjd 3900168404Spjd if (done) { 3901168404Spjd arc_callback_t *acb = NULL; 3902168404Spjd 3903168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), 3904168404Spjd KM_SLEEP); 3905168404Spjd acb->acb_done = done; 3906168404Spjd acb->acb_private = private; 3907168404Spjd if (pio != NULL) 3908168404Spjd acb->acb_zio_dummy = zio_null(pio, 3909209962Smm spa, NULL, NULL, NULL, zio_flags); 3910168404Spjd 3911168404Spjd ASSERT(acb->acb_done != NULL); 3912286570Smav acb->acb_next = hdr->b_l1hdr.b_acb; 3913286570Smav hdr->b_l1hdr.b_acb = acb; 3914168404Spjd add_reference(hdr, hash_lock, private); 3915168404Spjd mutex_exit(hash_lock); 3916168404Spjd return (0); 3917168404Spjd } 3918168404Spjd mutex_exit(hash_lock); 3919168404Spjd return (0); 3920168404Spjd } 3921168404Spjd 3922286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_mru || 3923286570Smav hdr->b_l1hdr.b_state == arc_mfu); 3924168404Spjd 3925168404Spjd if (done) { 3926168404Spjd add_reference(hdr, hash_lock, private); 3927168404Spjd /* 3928168404Spjd * If this block is already in use, create a new 3929168404Spjd * copy of the data so that we will be guaranteed 3930168404Spjd * that arc_release() will always succeed. 3931168404Spjd */ 3932286570Smav buf = hdr->b_l1hdr.b_buf; 3933168404Spjd ASSERT(buf); 3934168404Spjd ASSERT(buf->b_data); 3935168404Spjd if (HDR_BUF_AVAILABLE(hdr)) { 3936168404Spjd ASSERT(buf->b_efunc == NULL); 3937275811Sdelphij hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 3938168404Spjd } else { 3939168404Spjd buf = arc_buf_clone(buf); 3940168404Spjd } 3941219089Spjd 3942275811Sdelphij } else if (*arc_flags & ARC_FLAG_PREFETCH && 3943286570Smav refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 3944275811Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 3945168404Spjd } 3946168404Spjd DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 3947168404Spjd arc_access(hdr, hash_lock); 3948275811Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 3949275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 3950275811Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 3951275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 3952168404Spjd mutex_exit(hash_lock); 3953168404Spjd ARCSTAT_BUMP(arcstat_hits); 3954286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 3955286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 3956168404Spjd data, metadata, hits); 3957168404Spjd 3958168404Spjd if (done) 3959168404Spjd done(NULL, buf, private); 3960168404Spjd } else { 3961168404Spjd uint64_t size = BP_GET_LSIZE(bp); 3962268075Sdelphij arc_callback_t *acb; 3963185029Spjd vdev_t *vd = NULL; 3964247187Smm uint64_t addr = 0; 3965208373Smm boolean_t devw = B_FALSE; 3966258389Savg enum zio_compress b_compress = ZIO_COMPRESS_OFF; 3967286570Smav int32_t b_asize = 0; 3968168404Spjd 3969168404Spjd if (hdr == NULL) { 3970168404Spjd /* this block is not in the cache */ 3971268075Sdelphij arc_buf_hdr_t *exists = NULL; 3972168404Spjd arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 3973168404Spjd buf = arc_buf_alloc(spa, size, private, type); 3974168404Spjd hdr = buf->b_hdr; 3975268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 3976268075Sdelphij hdr->b_dva = *BP_IDENTITY(bp); 3977268075Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 3978268075Sdelphij exists = buf_hash_insert(hdr, &hash_lock); 3979268075Sdelphij } 3980268075Sdelphij if (exists != NULL) { 3981168404Spjd /* somebody beat us to the hash insert */ 3982168404Spjd mutex_exit(hash_lock); 3983219089Spjd buf_discard_identity(hdr); 3984168404Spjd (void) arc_buf_remove_ref(buf, private); 3985168404Spjd goto top; /* restart the IO request */ 3986168404Spjd } 3987275811Sdelphij 3988168404Spjd /* if this is a prefetch, we don't have a reference */ 3989275811Sdelphij if (*arc_flags & ARC_FLAG_PREFETCH) { 3990168404Spjd (void) remove_reference(hdr, hash_lock, 3991168404Spjd private); 3992275811Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 3993168404Spjd } 3994275811Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 3995275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 3996275811Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 3997275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 3998168404Spjd if (BP_GET_LEVEL(bp) > 0) 3999275811Sdelphij hdr->b_flags |= ARC_FLAG_INDIRECT; 4000168404Spjd } else { 4001286570Smav /* 4002286570Smav * This block is in the ghost cache. If it was L2-only 4003286570Smav * (and thus didn't have an L1 hdr), we realloc the 4004286570Smav * header to add an L1 hdr. 4005286570Smav */ 4006286570Smav if (!HDR_HAS_L1HDR(hdr)) { 4007286570Smav hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, 4008286570Smav hdr_full_cache); 4009286570Smav } 4010286570Smav 4011286570Smav ASSERT(GHOST_STATE(hdr->b_l1hdr.b_state)); 4012168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4013286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4014286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 4015168404Spjd 4016168404Spjd /* if this is a prefetch, we don't have a reference */ 4017275811Sdelphij if (*arc_flags & ARC_FLAG_PREFETCH) 4018275811Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 4019168404Spjd else 4020168404Spjd add_reference(hdr, hash_lock, private); 4021275811Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 4022275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 4023275811Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 4024275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4025185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 4026168404Spjd buf->b_hdr = hdr; 4027168404Spjd buf->b_data = NULL; 4028168404Spjd buf->b_efunc = NULL; 4029168404Spjd buf->b_private = NULL; 4030168404Spjd buf->b_next = NULL; 4031286570Smav hdr->b_l1hdr.b_buf = buf; 4032286570Smav ASSERT0(hdr->b_l1hdr.b_datacnt); 4033286570Smav hdr->b_l1hdr.b_datacnt = 1; 4034219089Spjd arc_get_data_buf(buf); 4035219089Spjd arc_access(hdr, hash_lock); 4036168404Spjd } 4037168404Spjd 4038286570Smav ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); 4039219089Spjd 4040168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 4041168404Spjd acb->acb_done = done; 4042168404Spjd acb->acb_private = private; 4043168404Spjd 4044286570Smav ASSERT(hdr->b_l1hdr.b_acb == NULL); 4045286570Smav hdr->b_l1hdr.b_acb = acb; 4046275811Sdelphij hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; 4047168404Spjd 4048286570Smav if (HDR_HAS_L2HDR(hdr) && 4049286570Smav (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { 4050286570Smav devw = hdr->b_l2hdr.b_dev->l2ad_writing; 4051286570Smav addr = hdr->b_l2hdr.b_daddr; 4052286570Smav b_compress = HDR_GET_COMPRESS(hdr); 4053286570Smav b_asize = hdr->b_l2hdr.b_asize; 4054185029Spjd /* 4055185029Spjd * Lock out device removal. 4056185029Spjd */ 4057185029Spjd if (vdev_is_dead(vd) || 4058185029Spjd !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 4059185029Spjd vd = NULL; 4060185029Spjd } 4061185029Spjd 4062268075Sdelphij if (hash_lock != NULL) 4063268075Sdelphij mutex_exit(hash_lock); 4064168404Spjd 4065251629Sdelphij /* 4066251629Sdelphij * At this point, we have a level 1 cache miss. Try again in 4067251629Sdelphij * L2ARC if possible. 4068251629Sdelphij */ 4069168404Spjd ASSERT3U(hdr->b_size, ==, size); 4070219089Spjd DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 4071268123Sdelphij uint64_t, size, zbookmark_phys_t *, zb); 4072168404Spjd ARCSTAT_BUMP(arcstat_misses); 4073286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 4074286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 4075168404Spjd data, metadata, misses); 4076228392Spjd#ifdef _KERNEL 4077228392Spjd curthread->td_ru.ru_inblock++; 4078228392Spjd#endif 4079168404Spjd 4080208373Smm if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 4081185029Spjd /* 4082185029Spjd * Read from the L2ARC if the following are true: 4083185029Spjd * 1. The L2ARC vdev was previously cached. 4084185029Spjd * 2. This buffer still has L2ARC metadata. 4085185029Spjd * 3. This buffer isn't currently writing to the L2ARC. 4086185029Spjd * 4. The L2ARC entry wasn't evicted, which may 4087185029Spjd * also have invalidated the vdev. 4088208373Smm * 5. This isn't prefetch and l2arc_noprefetch is set. 4089185029Spjd */ 4090286570Smav if (HDR_HAS_L2HDR(hdr) && 4091208373Smm !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 4092208373Smm !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 4093185029Spjd l2arc_read_callback_t *cb; 4094185029Spjd 4095185029Spjd DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 4096185029Spjd ARCSTAT_BUMP(arcstat_l2_hits); 4097185029Spjd 4098185029Spjd cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 4099185029Spjd KM_SLEEP); 4100185029Spjd cb->l2rcb_buf = buf; 4101185029Spjd cb->l2rcb_spa = spa; 4102185029Spjd cb->l2rcb_bp = *bp; 4103185029Spjd cb->l2rcb_zb = *zb; 4104185029Spjd cb->l2rcb_flags = zio_flags; 4105258389Savg cb->l2rcb_compress = b_compress; 4106185029Spjd 4107247187Smm ASSERT(addr >= VDEV_LABEL_START_SIZE && 4108247187Smm addr + size < vd->vdev_psize - 4109247187Smm VDEV_LABEL_END_SIZE); 4110247187Smm 4111185029Spjd /* 4112185029Spjd * l2arc read. The SCL_L2ARC lock will be 4113185029Spjd * released by l2arc_read_done(). 4114251478Sdelphij * Issue a null zio if the underlying buffer 4115251478Sdelphij * was squashed to zero size by compression. 4116185029Spjd */ 4117258389Savg if (b_compress == ZIO_COMPRESS_EMPTY) { 4118251478Sdelphij rzio = zio_null(pio, spa, vd, 4119251478Sdelphij l2arc_read_done, cb, 4120251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 4121251478Sdelphij ZIO_FLAG_CANFAIL | 4122251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 4123251478Sdelphij ZIO_FLAG_DONT_RETRY); 4124251478Sdelphij } else { 4125251478Sdelphij rzio = zio_read_phys(pio, vd, addr, 4126258389Savg b_asize, buf->b_data, 4127258389Savg ZIO_CHECKSUM_OFF, 4128251478Sdelphij l2arc_read_done, cb, priority, 4129251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 4130251478Sdelphij ZIO_FLAG_CANFAIL | 4131251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 4132251478Sdelphij ZIO_FLAG_DONT_RETRY, B_FALSE); 4133251478Sdelphij } 4134185029Spjd DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 4135185029Spjd zio_t *, rzio); 4136258389Savg ARCSTAT_INCR(arcstat_l2_read_bytes, b_asize); 4137185029Spjd 4138275811Sdelphij if (*arc_flags & ARC_FLAG_NOWAIT) { 4139185029Spjd zio_nowait(rzio); 4140185029Spjd return (0); 4141185029Spjd } 4142185029Spjd 4143275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_WAIT); 4144185029Spjd if (zio_wait(rzio) == 0) 4145185029Spjd return (0); 4146185029Spjd 4147185029Spjd /* l2arc read error; goto zio_read() */ 4148185029Spjd } else { 4149185029Spjd DTRACE_PROBE1(l2arc__miss, 4150185029Spjd arc_buf_hdr_t *, hdr); 4151185029Spjd ARCSTAT_BUMP(arcstat_l2_misses); 4152185029Spjd if (HDR_L2_WRITING(hdr)) 4153185029Spjd ARCSTAT_BUMP(arcstat_l2_rw_clash); 4154185029Spjd spa_config_exit(spa, SCL_L2ARC, vd); 4155185029Spjd } 4156208373Smm } else { 4157208373Smm if (vd != NULL) 4158208373Smm spa_config_exit(spa, SCL_L2ARC, vd); 4159208373Smm if (l2arc_ndev != 0) { 4160208373Smm DTRACE_PROBE1(l2arc__miss, 4161208373Smm arc_buf_hdr_t *, hdr); 4162208373Smm ARCSTAT_BUMP(arcstat_l2_misses); 4163208373Smm } 4164185029Spjd } 4165185029Spjd 4166168404Spjd rzio = zio_read(pio, spa, bp, buf->b_data, size, 4167185029Spjd arc_read_done, buf, priority, zio_flags, zb); 4168168404Spjd 4169275811Sdelphij if (*arc_flags & ARC_FLAG_WAIT) 4170168404Spjd return (zio_wait(rzio)); 4171168404Spjd 4172275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 4173168404Spjd zio_nowait(rzio); 4174168404Spjd } 4175168404Spjd return (0); 4176168404Spjd} 4177168404Spjd 4178168404Spjdvoid 4179168404Spjdarc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 4180168404Spjd{ 4181168404Spjd ASSERT(buf->b_hdr != NULL); 4182286570Smav ASSERT(buf->b_hdr->b_l1hdr.b_state != arc_anon); 4183286570Smav ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt) || 4184286570Smav func == NULL); 4185219089Spjd ASSERT(buf->b_efunc == NULL); 4186219089Spjd ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 4187219089Spjd 4188168404Spjd buf->b_efunc = func; 4189168404Spjd buf->b_private = private; 4190168404Spjd} 4191168404Spjd 4192168404Spjd/* 4193251520Sdelphij * Notify the arc that a block was freed, and thus will never be used again. 4194251520Sdelphij */ 4195251520Sdelphijvoid 4196251520Sdelphijarc_freed(spa_t *spa, const blkptr_t *bp) 4197251520Sdelphij{ 4198251520Sdelphij arc_buf_hdr_t *hdr; 4199251520Sdelphij kmutex_t *hash_lock; 4200251520Sdelphij uint64_t guid = spa_load_guid(spa); 4201251520Sdelphij 4202268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp)); 4203268075Sdelphij 4204268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 4205251520Sdelphij if (hdr == NULL) 4206251520Sdelphij return; 4207251520Sdelphij if (HDR_BUF_AVAILABLE(hdr)) { 4208286570Smav arc_buf_t *buf = hdr->b_l1hdr.b_buf; 4209251520Sdelphij add_reference(hdr, hash_lock, FTAG); 4210275811Sdelphij hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 4211251520Sdelphij mutex_exit(hash_lock); 4212251520Sdelphij 4213251520Sdelphij arc_release(buf, FTAG); 4214251520Sdelphij (void) arc_buf_remove_ref(buf, FTAG); 4215251520Sdelphij } else { 4216251520Sdelphij mutex_exit(hash_lock); 4217251520Sdelphij } 4218251520Sdelphij 4219251520Sdelphij} 4220251520Sdelphij 4221251520Sdelphij/* 4222268858Sdelphij * Clear the user eviction callback set by arc_set_callback(), first calling 4223268858Sdelphij * it if it exists. Because the presence of a callback keeps an arc_buf cached 4224268858Sdelphij * clearing the callback may result in the arc_buf being destroyed. However, 4225268858Sdelphij * it will not result in the *last* arc_buf being destroyed, hence the data 4226268858Sdelphij * will remain cached in the ARC. We make a copy of the arc buffer here so 4227268858Sdelphij * that we can process the callback without holding any locks. 4228268858Sdelphij * 4229268858Sdelphij * It's possible that the callback is already in the process of being cleared 4230268858Sdelphij * by another thread. In this case we can not clear the callback. 4231268858Sdelphij * 4232268858Sdelphij * Returns B_TRUE if the callback was successfully called and cleared. 4233168404Spjd */ 4234268858Sdelphijboolean_t 4235268858Sdelphijarc_clear_callback(arc_buf_t *buf) 4236168404Spjd{ 4237168404Spjd arc_buf_hdr_t *hdr; 4238168404Spjd kmutex_t *hash_lock; 4239268858Sdelphij arc_evict_func_t *efunc = buf->b_efunc; 4240268858Sdelphij void *private = buf->b_private; 4241205231Skmacy list_t *list, *evicted_list; 4242205231Skmacy kmutex_t *lock, *evicted_lock; 4243206796Spjd 4244219089Spjd mutex_enter(&buf->b_evict_lock); 4245168404Spjd hdr = buf->b_hdr; 4246168404Spjd if (hdr == NULL) { 4247168404Spjd /* 4248168404Spjd * We are in arc_do_user_evicts(). 4249168404Spjd */ 4250168404Spjd ASSERT(buf->b_data == NULL); 4251219089Spjd mutex_exit(&buf->b_evict_lock); 4252268858Sdelphij return (B_FALSE); 4253185029Spjd } else if (buf->b_data == NULL) { 4254185029Spjd /* 4255185029Spjd * We are on the eviction list; process this buffer now 4256185029Spjd * but let arc_do_user_evicts() do the reaping. 4257185029Spjd */ 4258185029Spjd buf->b_efunc = NULL; 4259219089Spjd mutex_exit(&buf->b_evict_lock); 4260268858Sdelphij VERIFY0(efunc(private)); 4261268858Sdelphij return (B_TRUE); 4262168404Spjd } 4263168404Spjd hash_lock = HDR_LOCK(hdr); 4264168404Spjd mutex_enter(hash_lock); 4265219089Spjd hdr = buf->b_hdr; 4266219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4267168404Spjd 4268286570Smav ASSERT3U(refcount_count(&hdr->b_l1hdr.b_refcnt), <, 4269286570Smav hdr->b_l1hdr.b_datacnt); 4270286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_mru || 4271286570Smav hdr->b_l1hdr.b_state == arc_mfu); 4272168404Spjd 4273268858Sdelphij buf->b_efunc = NULL; 4274268858Sdelphij buf->b_private = NULL; 4275168404Spjd 4276286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 4277268858Sdelphij mutex_exit(&buf->b_evict_lock); 4278268858Sdelphij arc_buf_destroy(buf, FALSE, TRUE); 4279268858Sdelphij } else { 4280286570Smav ASSERT(buf == hdr->b_l1hdr.b_buf); 4281275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 4282268858Sdelphij mutex_exit(&buf->b_evict_lock); 4283268858Sdelphij } 4284168404Spjd 4285168404Spjd mutex_exit(hash_lock); 4286268858Sdelphij VERIFY0(efunc(private)); 4287268858Sdelphij return (B_TRUE); 4288168404Spjd} 4289168404Spjd 4290168404Spjd/* 4291251629Sdelphij * Release this buffer from the cache, making it an anonymous buffer. This 4292251629Sdelphij * must be done after a read and prior to modifying the buffer contents. 4293168404Spjd * If the buffer has more than one reference, we must make 4294185029Spjd * a new hdr for the buffer. 4295168404Spjd */ 4296168404Spjdvoid 4297168404Spjdarc_release(arc_buf_t *buf, void *tag) 4298168404Spjd{ 4299286570Smav arc_buf_hdr_t *hdr = buf->b_hdr; 4300168404Spjd 4301219089Spjd /* 4302219089Spjd * It would be nice to assert that if it's DMU metadata (level > 4303219089Spjd * 0 || it's the dnode file), then it must be syncing context. 4304219089Spjd * But we don't know that information at this level. 4305219089Spjd */ 4306219089Spjd 4307219089Spjd mutex_enter(&buf->b_evict_lock); 4308286570Smav /* 4309286570Smav * We don't grab the hash lock prior to this check, because if 4310286570Smav * the buffer's header is in the arc_anon state, it won't be 4311286570Smav * linked into the hash table. 4312286570Smav */ 4313286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 4314286570Smav mutex_exit(&buf->b_evict_lock); 4315286570Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4316286570Smav ASSERT(!HDR_IN_HASH_TABLE(hdr)); 4317286570Smav ASSERT(!HDR_HAS_L2HDR(hdr)); 4318286570Smav ASSERT(BUF_EMPTY(hdr)); 4319286570Smav ASSERT3U(hdr->b_l1hdr.b_datacnt, ==, 1); 4320286570Smav ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); 4321286570Smav ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 4322185029Spjd 4323286570Smav ASSERT3P(buf->b_efunc, ==, NULL); 4324286570Smav ASSERT3P(buf->b_private, ==, NULL); 4325168404Spjd 4326286570Smav hdr->b_l1hdr.b_arc_access = 0; 4327286570Smav arc_buf_thaw(buf); 4328286570Smav 4329286570Smav return; 4330168404Spjd } 4331168404Spjd 4332286570Smav kmutex_t *hash_lock = HDR_LOCK(hdr); 4333286570Smav mutex_enter(hash_lock); 4334286570Smav 4335286570Smav /* 4336286570Smav * This assignment is only valid as long as the hash_lock is 4337286570Smav * held, we must be careful not to reference state or the 4338286570Smav * b_state field after dropping the lock. 4339286570Smav */ 4340286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 4341286570Smav ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4342286570Smav ASSERT3P(state, !=, arc_anon); 4343286570Smav 4344286570Smav /* this buffer is not on any list */ 4345286570Smav ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) > 0); 4346286570Smav 4347286570Smav if (HDR_HAS_L2HDR(hdr)) { 4348286570Smav mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); 4349286570Smav 4350286570Smav /* 4351286598Smav * We have to recheck this conditional again now that 4352286598Smav * we're holding the l2ad_mtx to prevent a race with 4353286598Smav * another thread which might be concurrently calling 4354286598Smav * l2arc_evict(). In that case, l2arc_evict() might have 4355286598Smav * destroyed the header's L2 portion as we were waiting 4356286598Smav * to acquire the l2ad_mtx. 4357286570Smav */ 4358286598Smav if (HDR_HAS_L2HDR(hdr)) { 4359286598Smav trim_map_free(hdr->b_l2hdr.b_dev->l2ad_vdev, 4360286598Smav hdr->b_l2hdr.b_daddr, hdr->b_l2hdr.b_asize, 0); 4361286598Smav arc_hdr_l2hdr_destroy(hdr); 4362286598Smav } 4363286570Smav 4364286570Smav mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); 4365185029Spjd } 4366185029Spjd 4367168404Spjd /* 4368168404Spjd * Do we have more than one buf? 4369168404Spjd */ 4370286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 4371168404Spjd arc_buf_hdr_t *nhdr; 4372168404Spjd arc_buf_t **bufp; 4373168404Spjd uint64_t blksz = hdr->b_size; 4374209962Smm uint64_t spa = hdr->b_spa; 4375286570Smav arc_buf_contents_t type = arc_buf_type(hdr); 4376185029Spjd uint32_t flags = hdr->b_flags; 4377168404Spjd 4378286570Smav ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); 4379168404Spjd /* 4380219089Spjd * Pull the data off of this hdr and attach it to 4381219089Spjd * a new anonymous hdr. 4382168404Spjd */ 4383168404Spjd (void) remove_reference(hdr, hash_lock, tag); 4384286570Smav bufp = &hdr->b_l1hdr.b_buf; 4385168404Spjd while (*bufp != buf) 4386168404Spjd bufp = &(*bufp)->b_next; 4387219089Spjd *bufp = buf->b_next; 4388168404Spjd buf->b_next = NULL; 4389168404Spjd 4390286570Smav ASSERT3P(state, !=, arc_l2c_only); 4391286570Smav ASSERT3U(state->arcs_size, >=, hdr->b_size); 4392286570Smav atomic_add_64(&state->arcs_size, -hdr->b_size); 4393286570Smav if (refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { 4394286570Smav ASSERT3P(state, !=, arc_l2c_only); 4395286570Smav uint64_t *size = &state->arcs_lsize[type]; 4396185029Spjd ASSERT3U(*size, >=, hdr->b_size); 4397185029Spjd atomic_add_64(size, -hdr->b_size); 4398168404Spjd } 4399242845Sdelphij 4400242845Sdelphij /* 4401242845Sdelphij * We're releasing a duplicate user data buffer, update 4402242845Sdelphij * our statistics accordingly. 4403242845Sdelphij */ 4404286570Smav if (HDR_ISTYPE_DATA(hdr)) { 4405242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 4406242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, 4407242845Sdelphij -hdr->b_size); 4408242845Sdelphij } 4409286570Smav hdr->b_l1hdr.b_datacnt -= 1; 4410168404Spjd arc_cksum_verify(buf); 4411240133Smm#ifdef illumos 4412240133Smm arc_buf_unwatch(buf); 4413277300Ssmh#endif 4414168404Spjd 4415168404Spjd mutex_exit(hash_lock); 4416168404Spjd 4417286570Smav nhdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 4418168404Spjd nhdr->b_size = blksz; 4419168404Spjd nhdr->b_spa = spa; 4420286570Smav 4421275811Sdelphij nhdr->b_flags = flags & ARC_FLAG_L2_WRITING; 4422286570Smav nhdr->b_flags |= arc_bufc_to_flags(type); 4423286570Smav nhdr->b_flags |= ARC_FLAG_HAS_L1HDR; 4424286570Smav 4425286570Smav nhdr->b_l1hdr.b_buf = buf; 4426286570Smav nhdr->b_l1hdr.b_datacnt = 1; 4427286570Smav nhdr->b_l1hdr.b_state = arc_anon; 4428286570Smav nhdr->b_l1hdr.b_arc_access = 0; 4429168404Spjd nhdr->b_freeze_cksum = NULL; 4430286570Smav 4431286570Smav (void) refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); 4432168404Spjd buf->b_hdr = nhdr; 4433219089Spjd mutex_exit(&buf->b_evict_lock); 4434168404Spjd atomic_add_64(&arc_anon->arcs_size, blksz); 4435168404Spjd } else { 4436219089Spjd mutex_exit(&buf->b_evict_lock); 4437286570Smav ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); 4438286570Smav /* protected by hash lock */ 4439286570Smav ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 4440168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4441286570Smav arc_change_state(arc_anon, hdr, hash_lock); 4442286570Smav hdr->b_l1hdr.b_arc_access = 0; 4443286570Smav mutex_exit(hash_lock); 4444185029Spjd 4445219089Spjd buf_discard_identity(hdr); 4446168404Spjd arc_buf_thaw(buf); 4447168404Spjd } 4448168404Spjd buf->b_efunc = NULL; 4449168404Spjd buf->b_private = NULL; 4450168404Spjd} 4451168404Spjd 4452168404Spjdint 4453168404Spjdarc_released(arc_buf_t *buf) 4454168404Spjd{ 4455185029Spjd int released; 4456185029Spjd 4457219089Spjd mutex_enter(&buf->b_evict_lock); 4458286570Smav released = (buf->b_data != NULL && 4459286570Smav buf->b_hdr->b_l1hdr.b_state == arc_anon); 4460219089Spjd mutex_exit(&buf->b_evict_lock); 4461185029Spjd return (released); 4462168404Spjd} 4463168404Spjd 4464168404Spjd#ifdef ZFS_DEBUG 4465168404Spjdint 4466168404Spjdarc_referenced(arc_buf_t *buf) 4467168404Spjd{ 4468185029Spjd int referenced; 4469185029Spjd 4470219089Spjd mutex_enter(&buf->b_evict_lock); 4471286570Smav referenced = (refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); 4472219089Spjd mutex_exit(&buf->b_evict_lock); 4473185029Spjd return (referenced); 4474168404Spjd} 4475168404Spjd#endif 4476168404Spjd 4477168404Spjdstatic void 4478168404Spjdarc_write_ready(zio_t *zio) 4479168404Spjd{ 4480168404Spjd arc_write_callback_t *callback = zio->io_private; 4481168404Spjd arc_buf_t *buf = callback->awcb_buf; 4482185029Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 4483168404Spjd 4484286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 4485286570Smav ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); 4486286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 4487185029Spjd callback->awcb_ready(zio, buf, callback->awcb_private); 4488185029Spjd 4489185029Spjd /* 4490185029Spjd * If the IO is already in progress, then this is a re-write 4491185029Spjd * attempt, so we need to thaw and re-compute the cksum. 4492185029Spjd * It is the responsibility of the callback to handle the 4493185029Spjd * accounting for any re-write attempt. 4494185029Spjd */ 4495185029Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 4496286570Smav mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 4497185029Spjd if (hdr->b_freeze_cksum != NULL) { 4498185029Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 4499185029Spjd hdr->b_freeze_cksum = NULL; 4500185029Spjd } 4501286570Smav mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 4502168404Spjd } 4503185029Spjd arc_cksum_compute(buf, B_FALSE); 4504275811Sdelphij hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; 4505168404Spjd} 4506168404Spjd 4507258632Savg/* 4508258632Savg * The SPA calls this callback for each physical write that happens on behalf 4509258632Savg * of a logical write. See the comment in dbuf_write_physdone() for details. 4510258632Savg */ 4511168404Spjdstatic void 4512258632Savgarc_write_physdone(zio_t *zio) 4513258632Savg{ 4514258632Savg arc_write_callback_t *cb = zio->io_private; 4515258632Savg if (cb->awcb_physdone != NULL) 4516258632Savg cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 4517258632Savg} 4518258632Savg 4519258632Savgstatic void 4520168404Spjdarc_write_done(zio_t *zio) 4521168404Spjd{ 4522168404Spjd arc_write_callback_t *callback = zio->io_private; 4523168404Spjd arc_buf_t *buf = callback->awcb_buf; 4524168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 4525168404Spjd 4526286570Smav ASSERT(hdr->b_l1hdr.b_acb == NULL); 4527168404Spjd 4528219089Spjd if (zio->io_error == 0) { 4529268075Sdelphij if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 4530260150Sdelphij buf_discard_identity(hdr); 4531260150Sdelphij } else { 4532260150Sdelphij hdr->b_dva = *BP_IDENTITY(zio->io_bp); 4533260150Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 4534260150Sdelphij } 4535219089Spjd } else { 4536219089Spjd ASSERT(BUF_EMPTY(hdr)); 4537219089Spjd } 4538219089Spjd 4539168404Spjd /* 4540268075Sdelphij * If the block to be written was all-zero or compressed enough to be 4541268075Sdelphij * embedded in the BP, no write was performed so there will be no 4542268075Sdelphij * dva/birth/checksum. The buffer must therefore remain anonymous 4543268075Sdelphij * (and uncached). 4544168404Spjd */ 4545168404Spjd if (!BUF_EMPTY(hdr)) { 4546168404Spjd arc_buf_hdr_t *exists; 4547168404Spjd kmutex_t *hash_lock; 4548168404Spjd 4549219089Spjd ASSERT(zio->io_error == 0); 4550219089Spjd 4551168404Spjd arc_cksum_verify(buf); 4552168404Spjd 4553168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 4554286570Smav if (exists != NULL) { 4555168404Spjd /* 4556168404Spjd * This can only happen if we overwrite for 4557168404Spjd * sync-to-convergence, because we remove 4558168404Spjd * buffers from the hash table when we arc_free(). 4559168404Spjd */ 4560219089Spjd if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 4561219089Spjd if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 4562219089Spjd panic("bad overwrite, hdr=%p exists=%p", 4563219089Spjd (void *)hdr, (void *)exists); 4564286570Smav ASSERT(refcount_is_zero( 4565286570Smav &exists->b_l1hdr.b_refcnt)); 4566219089Spjd arc_change_state(arc_anon, exists, hash_lock); 4567219089Spjd mutex_exit(hash_lock); 4568219089Spjd arc_hdr_destroy(exists); 4569219089Spjd exists = buf_hash_insert(hdr, &hash_lock); 4570219089Spjd ASSERT3P(exists, ==, NULL); 4571243524Smm } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 4572243524Smm /* nopwrite */ 4573243524Smm ASSERT(zio->io_prop.zp_nopwrite); 4574243524Smm if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 4575243524Smm panic("bad nopwrite, hdr=%p exists=%p", 4576243524Smm (void *)hdr, (void *)exists); 4577219089Spjd } else { 4578219089Spjd /* Dedup */ 4579286570Smav ASSERT(hdr->b_l1hdr.b_datacnt == 1); 4580286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_anon); 4581219089Spjd ASSERT(BP_GET_DEDUP(zio->io_bp)); 4582219089Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 4583219089Spjd } 4584168404Spjd } 4585275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 4586185029Spjd /* if it's not anon, we are doing a scrub */ 4587286570Smav if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) 4588185029Spjd arc_access(hdr, hash_lock); 4589168404Spjd mutex_exit(hash_lock); 4590168404Spjd } else { 4591275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 4592168404Spjd } 4593168404Spjd 4594286570Smav ASSERT(!refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4595219089Spjd callback->awcb_done(zio, buf, callback->awcb_private); 4596168404Spjd 4597168404Spjd kmem_free(callback, sizeof (arc_write_callback_t)); 4598168404Spjd} 4599168404Spjd 4600168404Spjdzio_t * 4601219089Spjdarc_write(zio_t *pio, spa_t *spa, uint64_t txg, 4602251478Sdelphij blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress, 4603258632Savg const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone, 4604258632Savg arc_done_func_t *done, void *private, zio_priority_t priority, 4605268123Sdelphij int zio_flags, const zbookmark_phys_t *zb) 4606168404Spjd{ 4607168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 4608168404Spjd arc_write_callback_t *callback; 4609185029Spjd zio_t *zio; 4610168404Spjd 4611185029Spjd ASSERT(ready != NULL); 4612219089Spjd ASSERT(done != NULL); 4613168404Spjd ASSERT(!HDR_IO_ERROR(hdr)); 4614286570Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4615286570Smav ASSERT(hdr->b_l1hdr.b_acb == NULL); 4616286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 4617185029Spjd if (l2arc) 4618275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 4619251478Sdelphij if (l2arc_compress) 4620275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4621168404Spjd callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 4622168404Spjd callback->awcb_ready = ready; 4623258632Savg callback->awcb_physdone = physdone; 4624168404Spjd callback->awcb_done = done; 4625168404Spjd callback->awcb_private = private; 4626168404Spjd callback->awcb_buf = buf; 4627168404Spjd 4628219089Spjd zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 4629258632Savg arc_write_ready, arc_write_physdone, arc_write_done, callback, 4630258632Savg priority, zio_flags, zb); 4631185029Spjd 4632168404Spjd return (zio); 4633168404Spjd} 4634168404Spjd 4635185029Spjdstatic int 4636258632Savgarc_memory_throttle(uint64_t reserve, uint64_t txg) 4637185029Spjd{ 4638185029Spjd#ifdef _KERNEL 4639272483Ssmh uint64_t available_memory = ptob(freemem); 4640185029Spjd static uint64_t page_load = 0; 4641185029Spjd static uint64_t last_txg = 0; 4642185029Spjd 4643272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 4644185029Spjd available_memory = 4645272483Ssmh MIN(available_memory, ptob(vmem_size(heap_arena, VMEM_FREE))); 4646185029Spjd#endif 4647258632Savg 4648272483Ssmh if (freemem > (uint64_t)physmem * arc_lotsfree_percent / 100) 4649185029Spjd return (0); 4650185029Spjd 4651185029Spjd if (txg > last_txg) { 4652185029Spjd last_txg = txg; 4653185029Spjd page_load = 0; 4654185029Spjd } 4655185029Spjd /* 4656185029Spjd * If we are in pageout, we know that memory is already tight, 4657185029Spjd * the arc is already going to be evicting, so we just want to 4658185029Spjd * continue to let page writes occur as quickly as possible. 4659185029Spjd */ 4660185029Spjd if (curproc == pageproc) { 4661272483Ssmh if (page_load > MAX(ptob(minfree), available_memory) / 4) 4662249195Smm return (SET_ERROR(ERESTART)); 4663185029Spjd /* Note: reserve is inflated, so we deflate */ 4664185029Spjd page_load += reserve / 8; 4665185029Spjd return (0); 4666185029Spjd } else if (page_load > 0 && arc_reclaim_needed()) { 4667185029Spjd /* memory is low, delay before restarting */ 4668185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 4669249195Smm return (SET_ERROR(EAGAIN)); 4670185029Spjd } 4671185029Spjd page_load = 0; 4672185029Spjd#endif 4673185029Spjd return (0); 4674185029Spjd} 4675185029Spjd 4676286574Smavstatic void 4677286574Smavarc_kstat_update_state(arc_state_t *state, kstat_named_t *size, 4678286574Smav kstat_named_t *evict_data, kstat_named_t *evict_metadata) 4679286574Smav{ 4680286574Smav size->value.ui64 = state->arcs_size; 4681286574Smav evict_data->value.ui64 = state->arcs_lsize[ARC_BUFC_DATA]; 4682286574Smav evict_metadata->value.ui64 = state->arcs_lsize[ARC_BUFC_METADATA]; 4683286574Smav} 4684286574Smav 4685286574Smavstatic int 4686286574Smavarc_kstat_update(kstat_t *ksp, int rw) 4687286574Smav{ 4688286574Smav arc_stats_t *as = ksp->ks_data; 4689286574Smav 4690286574Smav if (rw == KSTAT_WRITE) { 4691286574Smav return (EACCES); 4692286574Smav } else { 4693286574Smav arc_kstat_update_state(arc_anon, 4694286574Smav &as->arcstat_anon_size, 4695286574Smav &as->arcstat_anon_evictable_data, 4696286574Smav &as->arcstat_anon_evictable_metadata); 4697286574Smav arc_kstat_update_state(arc_mru, 4698286574Smav &as->arcstat_mru_size, 4699286574Smav &as->arcstat_mru_evictable_data, 4700286574Smav &as->arcstat_mru_evictable_metadata); 4701286574Smav arc_kstat_update_state(arc_mru_ghost, 4702286574Smav &as->arcstat_mru_ghost_size, 4703286574Smav &as->arcstat_mru_ghost_evictable_data, 4704286574Smav &as->arcstat_mru_ghost_evictable_metadata); 4705286574Smav arc_kstat_update_state(arc_mfu, 4706286574Smav &as->arcstat_mfu_size, 4707286574Smav &as->arcstat_mfu_evictable_data, 4708286574Smav &as->arcstat_mfu_evictable_metadata); 4709286574Smav arc_kstat_update_state(arc_mfu_ghost, 4710286574Smav &as->arcstat_mfu_ghost_size, 4711286574Smav &as->arcstat_mfu_ghost_evictable_data, 4712286574Smav &as->arcstat_mfu_ghost_evictable_metadata); 4713286574Smav } 4714286574Smav 4715286574Smav return (0); 4716286574Smav} 4717286574Smav 4718168404Spjdvoid 4719185029Spjdarc_tempreserve_clear(uint64_t reserve) 4720168404Spjd{ 4721185029Spjd atomic_add_64(&arc_tempreserve, -reserve); 4722168404Spjd ASSERT((int64_t)arc_tempreserve >= 0); 4723168404Spjd} 4724168404Spjd 4725168404Spjdint 4726185029Spjdarc_tempreserve_space(uint64_t reserve, uint64_t txg) 4727168404Spjd{ 4728185029Spjd int error; 4729209962Smm uint64_t anon_size; 4730185029Spjd 4731272483Ssmh if (reserve > arc_c/4 && !arc_no_grow) { 4732185029Spjd arc_c = MIN(arc_c_max, reserve * 4); 4733272483Ssmh DTRACE_PROBE1(arc__set_reserve, uint64_t, arc_c); 4734272483Ssmh } 4735185029Spjd if (reserve > arc_c) 4736249195Smm return (SET_ERROR(ENOMEM)); 4737168404Spjd 4738168404Spjd /* 4739209962Smm * Don't count loaned bufs as in flight dirty data to prevent long 4740209962Smm * network delays from blocking transactions that are ready to be 4741209962Smm * assigned to a txg. 4742209962Smm */ 4743209962Smm anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 4744209962Smm 4745209962Smm /* 4746185029Spjd * Writes will, almost always, require additional memory allocations 4747251631Sdelphij * in order to compress/encrypt/etc the data. We therefore need to 4748185029Spjd * make sure that there is sufficient available memory for this. 4749185029Spjd */ 4750258632Savg error = arc_memory_throttle(reserve, txg); 4751258632Savg if (error != 0) 4752185029Spjd return (error); 4753185029Spjd 4754185029Spjd /* 4755168404Spjd * Throttle writes when the amount of dirty data in the cache 4756168404Spjd * gets too large. We try to keep the cache less than half full 4757168404Spjd * of dirty blocks so that our sync times don't grow too large. 4758168404Spjd * Note: if two requests come in concurrently, we might let them 4759168404Spjd * both succeed, when one of them should fail. Not a huge deal. 4760168404Spjd */ 4761209962Smm 4762209962Smm if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 4763209962Smm anon_size > arc_c / 4) { 4764185029Spjd dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 4765185029Spjd "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 4766185029Spjd arc_tempreserve>>10, 4767185029Spjd arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 4768185029Spjd arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 4769185029Spjd reserve>>10, arc_c>>10); 4770249195Smm return (SET_ERROR(ERESTART)); 4771168404Spjd } 4772185029Spjd atomic_add_64(&arc_tempreserve, reserve); 4773168404Spjd return (0); 4774168404Spjd} 4775168404Spjd 4776168582Spjdstatic kmutex_t arc_lowmem_lock; 4777168404Spjd#ifdef _KERNEL 4778168566Spjdstatic eventhandler_tag arc_event_lowmem = NULL; 4779168404Spjd 4780168404Spjdstatic void 4781168566Spjdarc_lowmem(void *arg __unused, int howto __unused) 4782168404Spjd{ 4783168404Spjd 4784168566Spjd /* Serialize access via arc_lowmem_lock. */ 4785168566Spjd mutex_enter(&arc_lowmem_lock); 4786219089Spjd mutex_enter(&arc_reclaim_thr_lock); 4787185029Spjd needfree = 1; 4788272483Ssmh DTRACE_PROBE(arc__needfree); 4789168404Spjd cv_signal(&arc_reclaim_thr_cv); 4790241773Savg 4791241773Savg /* 4792241773Savg * It is unsafe to block here in arbitrary threads, because we can come 4793241773Savg * here from ARC itself and may hold ARC locks and thus risk a deadlock 4794241773Savg * with ARC reclaim thread. 4795241773Savg */ 4796241773Savg if (curproc == pageproc) { 4797241773Savg while (needfree) 4798241773Savg msleep(&needfree, &arc_reclaim_thr_lock, 0, "zfs:lowmem", 0); 4799241773Savg } 4800219089Spjd mutex_exit(&arc_reclaim_thr_lock); 4801168566Spjd mutex_exit(&arc_lowmem_lock); 4802168404Spjd} 4803168404Spjd#endif 4804168404Spjd 4805168404Spjdvoid 4806168404Spjdarc_init(void) 4807168404Spjd{ 4808219089Spjd int i, prefetch_tunable_set = 0; 4809205231Skmacy 4810168404Spjd mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 4811168404Spjd cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 4812168566Spjd mutex_init(&arc_lowmem_lock, NULL, MUTEX_DEFAULT, NULL); 4813168404Spjd 4814168404Spjd /* Convert seconds to clock ticks */ 4815168404Spjd arc_min_prefetch_lifespan = 1 * hz; 4816168404Spjd 4817168404Spjd /* Start out with 1/8 of all memory */ 4818168566Spjd arc_c = kmem_size() / 8; 4819219089Spjd 4820277300Ssmh#ifdef illumos 4821192360Skmacy#ifdef _KERNEL 4822192360Skmacy /* 4823192360Skmacy * On architectures where the physical memory can be larger 4824192360Skmacy * than the addressable space (intel in 32-bit mode), we may 4825192360Skmacy * need to limit the cache to 1/8 of VM size. 4826192360Skmacy */ 4827192360Skmacy arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 4828192360Skmacy#endif 4829277300Ssmh#endif /* illumos */ 4830168566Spjd /* set min cache to 1/32 of all memory, or 16MB, whichever is more */ 4831280822Smav arc_c_min = MAX(arc_c / 4, 16 << 20); 4832168566Spjd /* set max to 1/2 of all memory, or all but 1GB, whichever is more */ 4833280822Smav if (arc_c * 8 >= 1 << 30) 4834280822Smav arc_c_max = (arc_c * 8) - (1 << 30); 4835168404Spjd else 4836168404Spjd arc_c_max = arc_c_min; 4837175633Spjd arc_c_max = MAX(arc_c * 5, arc_c_max); 4838219089Spjd 4839168481Spjd#ifdef _KERNEL 4840168404Spjd /* 4841168404Spjd * Allow the tunables to override our calculations if they are 4842168566Spjd * reasonable (ie. over 16MB) 4843168404Spjd */ 4844280822Smav if (zfs_arc_max > 16 << 20 && zfs_arc_max < kmem_size()) 4845168404Spjd arc_c_max = zfs_arc_max; 4846280822Smav if (zfs_arc_min > 16 << 20 && zfs_arc_min <= arc_c_max) 4847168404Spjd arc_c_min = zfs_arc_min; 4848168481Spjd#endif 4849219089Spjd 4850168404Spjd arc_c = arc_c_max; 4851168404Spjd arc_p = (arc_c >> 1); 4852168404Spjd 4853185029Spjd /* limit meta-data to 1/4 of the arc capacity */ 4854185029Spjd arc_meta_limit = arc_c_max / 4; 4855185029Spjd 4856185029Spjd /* Allow the tunable to override if it is reasonable */ 4857185029Spjd if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 4858185029Spjd arc_meta_limit = zfs_arc_meta_limit; 4859185029Spjd 4860185029Spjd if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 4861185029Spjd arc_c_min = arc_meta_limit / 2; 4862185029Spjd 4863275780Sdelphij if (zfs_arc_meta_min > 0) { 4864275780Sdelphij arc_meta_min = zfs_arc_meta_min; 4865275780Sdelphij } else { 4866275780Sdelphij arc_meta_min = arc_c_min / 2; 4867275780Sdelphij } 4868275780Sdelphij 4869208373Smm if (zfs_arc_grow_retry > 0) 4870208373Smm arc_grow_retry = zfs_arc_grow_retry; 4871208373Smm 4872208373Smm if (zfs_arc_shrink_shift > 0) 4873208373Smm arc_shrink_shift = zfs_arc_shrink_shift; 4874208373Smm 4875208373Smm if (zfs_arc_p_min_shift > 0) 4876208373Smm arc_p_min_shift = zfs_arc_p_min_shift; 4877208373Smm 4878168404Spjd /* if kmem_flags are set, lets try to use less memory */ 4879168404Spjd if (kmem_debugging()) 4880168404Spjd arc_c = arc_c / 2; 4881168404Spjd if (arc_c < arc_c_min) 4882168404Spjd arc_c = arc_c_min; 4883168404Spjd 4884168473Spjd zfs_arc_min = arc_c_min; 4885168473Spjd zfs_arc_max = arc_c_max; 4886168473Spjd 4887168404Spjd arc_anon = &ARC_anon; 4888168404Spjd arc_mru = &ARC_mru; 4889168404Spjd arc_mru_ghost = &ARC_mru_ghost; 4890168404Spjd arc_mfu = &ARC_mfu; 4891168404Spjd arc_mfu_ghost = &ARC_mfu_ghost; 4892185029Spjd arc_l2c_only = &ARC_l2c_only; 4893168404Spjd arc_size = 0; 4894168404Spjd 4895205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 4896205231Skmacy mutex_init(&arc_anon->arcs_locks[i].arcs_lock, 4897205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4898205231Skmacy mutex_init(&arc_mru->arcs_locks[i].arcs_lock, 4899205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4900205231Skmacy mutex_init(&arc_mru_ghost->arcs_locks[i].arcs_lock, 4901205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4902205231Skmacy mutex_init(&arc_mfu->arcs_locks[i].arcs_lock, 4903205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4904205231Skmacy mutex_init(&arc_mfu_ghost->arcs_locks[i].arcs_lock, 4905205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4906205231Skmacy mutex_init(&arc_l2c_only->arcs_locks[i].arcs_lock, 4907205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4908206796Spjd 4909205231Skmacy list_create(&arc_mru->arcs_lists[i], 4910286570Smav sizeof (arc_buf_hdr_t), 4911286570Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node)); 4912205231Skmacy list_create(&arc_mru_ghost->arcs_lists[i], 4913286570Smav sizeof (arc_buf_hdr_t), 4914286570Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node)); 4915205231Skmacy list_create(&arc_mfu->arcs_lists[i], 4916286570Smav sizeof (arc_buf_hdr_t), 4917286570Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node)); 4918205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 4919286570Smav sizeof (arc_buf_hdr_t), 4920286570Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node)); 4921205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 4922286570Smav sizeof (arc_buf_hdr_t), 4923286570Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node)); 4924205231Skmacy list_create(&arc_l2c_only->arcs_lists[i], 4925286570Smav sizeof (arc_buf_hdr_t), 4926286570Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node)); 4927205231Skmacy } 4928168404Spjd 4929168404Spjd buf_init(); 4930168404Spjd 4931168404Spjd arc_thread_exit = 0; 4932168404Spjd arc_eviction_list = NULL; 4933168404Spjd mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 4934168404Spjd bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 4935168404Spjd 4936168404Spjd arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 4937168404Spjd sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 4938168404Spjd 4939168404Spjd if (arc_ksp != NULL) { 4940168404Spjd arc_ksp->ks_data = &arc_stats; 4941286574Smav arc_ksp->ks_update = arc_kstat_update; 4942168404Spjd kstat_install(arc_ksp); 4943168404Spjd } 4944168404Spjd 4945168404Spjd (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 4946168404Spjd TS_RUN, minclsyspri); 4947168404Spjd 4948168404Spjd#ifdef _KERNEL 4949168566Spjd arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 4950168404Spjd EVENTHANDLER_PRI_FIRST); 4951168404Spjd#endif 4952168404Spjd 4953168404Spjd arc_dead = FALSE; 4954185029Spjd arc_warm = B_FALSE; 4955168566Spjd 4956258632Savg /* 4957258632Savg * Calculate maximum amount of dirty data per pool. 4958258632Savg * 4959258632Savg * If it has been set by /etc/system, take that. 4960258632Savg * Otherwise, use a percentage of physical memory defined by 4961258632Savg * zfs_dirty_data_max_percent (default 10%) with a cap at 4962258632Savg * zfs_dirty_data_max_max (default 4GB). 4963258632Savg */ 4964258632Savg if (zfs_dirty_data_max == 0) { 4965258632Savg zfs_dirty_data_max = ptob(physmem) * 4966258632Savg zfs_dirty_data_max_percent / 100; 4967258632Savg zfs_dirty_data_max = MIN(zfs_dirty_data_max, 4968258632Savg zfs_dirty_data_max_max); 4969258632Savg } 4970185029Spjd 4971168566Spjd#ifdef _KERNEL 4972194043Skmacy if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 4973193953Skmacy prefetch_tunable_set = 1; 4974206796Spjd 4975193878Skmacy#ifdef __i386__ 4976193953Skmacy if (prefetch_tunable_set == 0) { 4977196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 4978196863Strasz "-- to enable,\n"); 4979196863Strasz printf(" add \"vfs.zfs.prefetch_disable=0\" " 4980196863Strasz "to /boot/loader.conf.\n"); 4981219089Spjd zfs_prefetch_disable = 1; 4982193878Skmacy } 4983206796Spjd#else 4984193878Skmacy if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 4985193953Skmacy prefetch_tunable_set == 0) { 4986196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default if less " 4987196941Strasz "than 4GB of RAM is present;\n" 4988196863Strasz " to enable, add \"vfs.zfs.prefetch_disable=0\" " 4989196863Strasz "to /boot/loader.conf.\n"); 4990219089Spjd zfs_prefetch_disable = 1; 4991193878Skmacy } 4992206796Spjd#endif 4993175633Spjd /* Warn about ZFS memory and address space requirements. */ 4994168696Spjd if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 4995168987Sbmah printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 4996168987Sbmah "expect unstable behavior.\n"); 4997175633Spjd } 4998175633Spjd if (kmem_size() < 512 * (1 << 20)) { 4999173419Spjd printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 5000168987Sbmah "expect unstable behavior.\n"); 5001185029Spjd printf(" Consider tuning vm.kmem_size and " 5002173419Spjd "vm.kmem_size_max\n"); 5003185029Spjd printf(" in /boot/loader.conf.\n"); 5004168566Spjd } 5005168566Spjd#endif 5006168404Spjd} 5007168404Spjd 5008168404Spjdvoid 5009168404Spjdarc_fini(void) 5010168404Spjd{ 5011205231Skmacy int i; 5012206796Spjd 5013168404Spjd mutex_enter(&arc_reclaim_thr_lock); 5014168404Spjd arc_thread_exit = 1; 5015168404Spjd cv_signal(&arc_reclaim_thr_cv); 5016168404Spjd while (arc_thread_exit != 0) 5017168404Spjd cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 5018168404Spjd mutex_exit(&arc_reclaim_thr_lock); 5019168404Spjd 5020185029Spjd arc_flush(NULL); 5021168404Spjd 5022168404Spjd arc_dead = TRUE; 5023168404Spjd 5024168404Spjd if (arc_ksp != NULL) { 5025168404Spjd kstat_delete(arc_ksp); 5026168404Spjd arc_ksp = NULL; 5027168404Spjd } 5028168404Spjd 5029168404Spjd mutex_destroy(&arc_eviction_mtx); 5030168404Spjd mutex_destroy(&arc_reclaim_thr_lock); 5031168404Spjd cv_destroy(&arc_reclaim_thr_cv); 5032168404Spjd 5033205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 5034205231Skmacy list_destroy(&arc_mru->arcs_lists[i]); 5035205231Skmacy list_destroy(&arc_mru_ghost->arcs_lists[i]); 5036205231Skmacy list_destroy(&arc_mfu->arcs_lists[i]); 5037205231Skmacy list_destroy(&arc_mfu_ghost->arcs_lists[i]); 5038206795Spjd list_destroy(&arc_l2c_only->arcs_lists[i]); 5039168404Spjd 5040205231Skmacy mutex_destroy(&arc_anon->arcs_locks[i].arcs_lock); 5041205231Skmacy mutex_destroy(&arc_mru->arcs_locks[i].arcs_lock); 5042205231Skmacy mutex_destroy(&arc_mru_ghost->arcs_locks[i].arcs_lock); 5043205231Skmacy mutex_destroy(&arc_mfu->arcs_locks[i].arcs_lock); 5044205231Skmacy mutex_destroy(&arc_mfu_ghost->arcs_locks[i].arcs_lock); 5045206795Spjd mutex_destroy(&arc_l2c_only->arcs_locks[i].arcs_lock); 5046205231Skmacy } 5047206796Spjd 5048168404Spjd buf_fini(); 5049168404Spjd 5050286570Smav ASSERT0(arc_loaned_bytes); 5051209962Smm 5052168582Spjd mutex_destroy(&arc_lowmem_lock); 5053168404Spjd#ifdef _KERNEL 5054168566Spjd if (arc_event_lowmem != NULL) 5055168566Spjd EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 5056168404Spjd#endif 5057168404Spjd} 5058185029Spjd 5059185029Spjd/* 5060185029Spjd * Level 2 ARC 5061185029Spjd * 5062185029Spjd * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 5063185029Spjd * It uses dedicated storage devices to hold cached data, which are populated 5064185029Spjd * using large infrequent writes. The main role of this cache is to boost 5065185029Spjd * the performance of random read workloads. The intended L2ARC devices 5066185029Spjd * include short-stroked disks, solid state disks, and other media with 5067185029Spjd * substantially faster read latency than disk. 5068185029Spjd * 5069185029Spjd * +-----------------------+ 5070185029Spjd * | ARC | 5071185029Spjd * +-----------------------+ 5072185029Spjd * | ^ ^ 5073185029Spjd * | | | 5074185029Spjd * l2arc_feed_thread() arc_read() 5075185029Spjd * | | | 5076185029Spjd * | l2arc read | 5077185029Spjd * V | | 5078185029Spjd * +---------------+ | 5079185029Spjd * | L2ARC | | 5080185029Spjd * +---------------+ | 5081185029Spjd * | ^ | 5082185029Spjd * l2arc_write() | | 5083185029Spjd * | | | 5084185029Spjd * V | | 5085185029Spjd * +-------+ +-------+ 5086185029Spjd * | vdev | | vdev | 5087185029Spjd * | cache | | cache | 5088185029Spjd * +-------+ +-------+ 5089185029Spjd * +=========+ .-----. 5090185029Spjd * : L2ARC : |-_____-| 5091185029Spjd * : devices : | Disks | 5092185029Spjd * +=========+ `-_____-' 5093185029Spjd * 5094185029Spjd * Read requests are satisfied from the following sources, in order: 5095185029Spjd * 5096185029Spjd * 1) ARC 5097185029Spjd * 2) vdev cache of L2ARC devices 5098185029Spjd * 3) L2ARC devices 5099185029Spjd * 4) vdev cache of disks 5100185029Spjd * 5) disks 5101185029Spjd * 5102185029Spjd * Some L2ARC device types exhibit extremely slow write performance. 5103185029Spjd * To accommodate for this there are some significant differences between 5104185029Spjd * the L2ARC and traditional cache design: 5105185029Spjd * 5106185029Spjd * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 5107185029Spjd * the ARC behave as usual, freeing buffers and placing headers on ghost 5108185029Spjd * lists. The ARC does not send buffers to the L2ARC during eviction as 5109185029Spjd * this would add inflated write latencies for all ARC memory pressure. 5110185029Spjd * 5111185029Spjd * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 5112185029Spjd * It does this by periodically scanning buffers from the eviction-end of 5113185029Spjd * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 5114251478Sdelphij * not already there. It scans until a headroom of buffers is satisfied, 5115251478Sdelphij * which itself is a buffer for ARC eviction. If a compressible buffer is 5116251478Sdelphij * found during scanning and selected for writing to an L2ARC device, we 5117251478Sdelphij * temporarily boost scanning headroom during the next scan cycle to make 5118251478Sdelphij * sure we adapt to compression effects (which might significantly reduce 5119251478Sdelphij * the data volume we write to L2ARC). The thread that does this is 5120185029Spjd * l2arc_feed_thread(), illustrated below; example sizes are included to 5121185029Spjd * provide a better sense of ratio than this diagram: 5122185029Spjd * 5123185029Spjd * head --> tail 5124185029Spjd * +---------------------+----------+ 5125185029Spjd * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 5126185029Spjd * +---------------------+----------+ | o L2ARC eligible 5127185029Spjd * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 5128185029Spjd * +---------------------+----------+ | 5129185029Spjd * 15.9 Gbytes ^ 32 Mbytes | 5130185029Spjd * headroom | 5131185029Spjd * l2arc_feed_thread() 5132185029Spjd * | 5133185029Spjd * l2arc write hand <--[oooo]--' 5134185029Spjd * | 8 Mbyte 5135185029Spjd * | write max 5136185029Spjd * V 5137185029Spjd * +==============================+ 5138185029Spjd * L2ARC dev |####|#|###|###| |####| ... | 5139185029Spjd * +==============================+ 5140185029Spjd * 32 Gbytes 5141185029Spjd * 5142185029Spjd * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 5143185029Spjd * evicted, then the L2ARC has cached a buffer much sooner than it probably 5144185029Spjd * needed to, potentially wasting L2ARC device bandwidth and storage. It is 5145185029Spjd * safe to say that this is an uncommon case, since buffers at the end of 5146185029Spjd * the ARC lists have moved there due to inactivity. 5147185029Spjd * 5148185029Spjd * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 5149185029Spjd * then the L2ARC simply misses copying some buffers. This serves as a 5150185029Spjd * pressure valve to prevent heavy read workloads from both stalling the ARC 5151185029Spjd * with waits and clogging the L2ARC with writes. This also helps prevent 5152185029Spjd * the potential for the L2ARC to churn if it attempts to cache content too 5153185029Spjd * quickly, such as during backups of the entire pool. 5154185029Spjd * 5155185029Spjd * 5. After system boot and before the ARC has filled main memory, there are 5156185029Spjd * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 5157185029Spjd * lists can remain mostly static. Instead of searching from tail of these 5158185029Spjd * lists as pictured, the l2arc_feed_thread() will search from the list heads 5159185029Spjd * for eligible buffers, greatly increasing its chance of finding them. 5160185029Spjd * 5161185029Spjd * The L2ARC device write speed is also boosted during this time so that 5162185029Spjd * the L2ARC warms up faster. Since there have been no ARC evictions yet, 5163185029Spjd * there are no L2ARC reads, and no fear of degrading read performance 5164185029Spjd * through increased writes. 5165185029Spjd * 5166185029Spjd * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 5167185029Spjd * the vdev queue can aggregate them into larger and fewer writes. Each 5168185029Spjd * device is written to in a rotor fashion, sweeping writes through 5169185029Spjd * available space then repeating. 5170185029Spjd * 5171185029Spjd * 7. The L2ARC does not store dirty content. It never needs to flush 5172185029Spjd * write buffers back to disk based storage. 5173185029Spjd * 5174185029Spjd * 8. If an ARC buffer is written (and dirtied) which also exists in the 5175185029Spjd * L2ARC, the now stale L2ARC buffer is immediately dropped. 5176185029Spjd * 5177185029Spjd * The performance of the L2ARC can be tweaked by a number of tunables, which 5178185029Spjd * may be necessary for different workloads: 5179185029Spjd * 5180185029Spjd * l2arc_write_max max write bytes per interval 5181185029Spjd * l2arc_write_boost extra write bytes during device warmup 5182185029Spjd * l2arc_noprefetch skip caching prefetched buffers 5183185029Spjd * l2arc_headroom number of max device writes to precache 5184251478Sdelphij * l2arc_headroom_boost when we find compressed buffers during ARC 5185251478Sdelphij * scanning, we multiply headroom by this 5186251478Sdelphij * percentage factor for the next scan cycle, 5187251478Sdelphij * since more compressed buffers are likely to 5188251478Sdelphij * be present 5189185029Spjd * l2arc_feed_secs seconds between L2ARC writing 5190185029Spjd * 5191185029Spjd * Tunables may be removed or added as future performance improvements are 5192185029Spjd * integrated, and also may become zpool properties. 5193208373Smm * 5194208373Smm * There are three key functions that control how the L2ARC warms up: 5195208373Smm * 5196208373Smm * l2arc_write_eligible() check if a buffer is eligible to cache 5197208373Smm * l2arc_write_size() calculate how much to write 5198208373Smm * l2arc_write_interval() calculate sleep delay between writes 5199208373Smm * 5200208373Smm * These three functions determine what to write, how much, and how quickly 5201208373Smm * to send writes. 5202185029Spjd */ 5203185029Spjd 5204208373Smmstatic boolean_t 5205275811Sdelphijl2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) 5206208373Smm{ 5207208373Smm /* 5208208373Smm * A buffer is *not* eligible for the L2ARC if it: 5209208373Smm * 1. belongs to a different spa. 5210208373Smm * 2. is already cached on the L2ARC. 5211208373Smm * 3. has an I/O in progress (it may be an incomplete read). 5212208373Smm * 4. is flagged not eligible (zfs property). 5213208373Smm */ 5214275811Sdelphij if (hdr->b_spa != spa_guid) { 5215208373Smm ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 5216208373Smm return (B_FALSE); 5217208373Smm } 5218286570Smav if (HDR_HAS_L2HDR(hdr)) { 5219208373Smm ARCSTAT_BUMP(arcstat_l2_write_in_l2); 5220208373Smm return (B_FALSE); 5221208373Smm } 5222275811Sdelphij if (HDR_IO_IN_PROGRESS(hdr)) { 5223208373Smm ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 5224208373Smm return (B_FALSE); 5225208373Smm } 5226275811Sdelphij if (!HDR_L2CACHE(hdr)) { 5227208373Smm ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 5228208373Smm return (B_FALSE); 5229208373Smm } 5230208373Smm 5231208373Smm return (B_TRUE); 5232208373Smm} 5233208373Smm 5234208373Smmstatic uint64_t 5235251478Sdelphijl2arc_write_size(void) 5236208373Smm{ 5237208373Smm uint64_t size; 5238208373Smm 5239251478Sdelphij /* 5240251478Sdelphij * Make sure our globals have meaningful values in case the user 5241251478Sdelphij * altered them. 5242251478Sdelphij */ 5243251478Sdelphij size = l2arc_write_max; 5244251478Sdelphij if (size == 0) { 5245251478Sdelphij cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 5246251478Sdelphij "be greater than zero, resetting it to the default (%d)", 5247251478Sdelphij L2ARC_WRITE_SIZE); 5248251478Sdelphij size = l2arc_write_max = L2ARC_WRITE_SIZE; 5249251478Sdelphij } 5250208373Smm 5251208373Smm if (arc_warm == B_FALSE) 5252251478Sdelphij size += l2arc_write_boost; 5253208373Smm 5254208373Smm return (size); 5255208373Smm 5256208373Smm} 5257208373Smm 5258208373Smmstatic clock_t 5259208373Smml2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 5260208373Smm{ 5261219089Spjd clock_t interval, next, now; 5262208373Smm 5263208373Smm /* 5264208373Smm * If the ARC lists are busy, increase our write rate; if the 5265208373Smm * lists are stale, idle back. This is achieved by checking 5266208373Smm * how much we previously wrote - if it was more than half of 5267208373Smm * what we wanted, schedule the next write much sooner. 5268208373Smm */ 5269208373Smm if (l2arc_feed_again && wrote > (wanted / 2)) 5270208373Smm interval = (hz * l2arc_feed_min_ms) / 1000; 5271208373Smm else 5272208373Smm interval = hz * l2arc_feed_secs; 5273208373Smm 5274219089Spjd now = ddi_get_lbolt(); 5275219089Spjd next = MAX(now, MIN(now + interval, began + interval)); 5276208373Smm 5277208373Smm return (next); 5278208373Smm} 5279208373Smm 5280185029Spjd/* 5281185029Spjd * Cycle through L2ARC devices. This is how L2ARC load balances. 5282185029Spjd * If a device is returned, this also returns holding the spa config lock. 5283185029Spjd */ 5284185029Spjdstatic l2arc_dev_t * 5285185029Spjdl2arc_dev_get_next(void) 5286185029Spjd{ 5287185029Spjd l2arc_dev_t *first, *next = NULL; 5288185029Spjd 5289185029Spjd /* 5290185029Spjd * Lock out the removal of spas (spa_namespace_lock), then removal 5291185029Spjd * of cache devices (l2arc_dev_mtx). Once a device has been selected, 5292185029Spjd * both locks will be dropped and a spa config lock held instead. 5293185029Spjd */ 5294185029Spjd mutex_enter(&spa_namespace_lock); 5295185029Spjd mutex_enter(&l2arc_dev_mtx); 5296185029Spjd 5297185029Spjd /* if there are no vdevs, there is nothing to do */ 5298185029Spjd if (l2arc_ndev == 0) 5299185029Spjd goto out; 5300185029Spjd 5301185029Spjd first = NULL; 5302185029Spjd next = l2arc_dev_last; 5303185029Spjd do { 5304185029Spjd /* loop around the list looking for a non-faulted vdev */ 5305185029Spjd if (next == NULL) { 5306185029Spjd next = list_head(l2arc_dev_list); 5307185029Spjd } else { 5308185029Spjd next = list_next(l2arc_dev_list, next); 5309185029Spjd if (next == NULL) 5310185029Spjd next = list_head(l2arc_dev_list); 5311185029Spjd } 5312185029Spjd 5313185029Spjd /* if we have come back to the start, bail out */ 5314185029Spjd if (first == NULL) 5315185029Spjd first = next; 5316185029Spjd else if (next == first) 5317185029Spjd break; 5318185029Spjd 5319185029Spjd } while (vdev_is_dead(next->l2ad_vdev)); 5320185029Spjd 5321185029Spjd /* if we were unable to find any usable vdevs, return NULL */ 5322185029Spjd if (vdev_is_dead(next->l2ad_vdev)) 5323185029Spjd next = NULL; 5324185029Spjd 5325185029Spjd l2arc_dev_last = next; 5326185029Spjd 5327185029Spjdout: 5328185029Spjd mutex_exit(&l2arc_dev_mtx); 5329185029Spjd 5330185029Spjd /* 5331185029Spjd * Grab the config lock to prevent the 'next' device from being 5332185029Spjd * removed while we are writing to it. 5333185029Spjd */ 5334185029Spjd if (next != NULL) 5335185029Spjd spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 5336185029Spjd mutex_exit(&spa_namespace_lock); 5337185029Spjd 5338185029Spjd return (next); 5339185029Spjd} 5340185029Spjd 5341185029Spjd/* 5342185029Spjd * Free buffers that were tagged for destruction. 5343185029Spjd */ 5344185029Spjdstatic void 5345185029Spjdl2arc_do_free_on_write() 5346185029Spjd{ 5347185029Spjd list_t *buflist; 5348185029Spjd l2arc_data_free_t *df, *df_prev; 5349185029Spjd 5350185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 5351185029Spjd buflist = l2arc_free_on_write; 5352185029Spjd 5353185029Spjd for (df = list_tail(buflist); df; df = df_prev) { 5354185029Spjd df_prev = list_prev(buflist, df); 5355185029Spjd ASSERT(df->l2df_data != NULL); 5356185029Spjd ASSERT(df->l2df_func != NULL); 5357185029Spjd df->l2df_func(df->l2df_data, df->l2df_size); 5358185029Spjd list_remove(buflist, df); 5359185029Spjd kmem_free(df, sizeof (l2arc_data_free_t)); 5360185029Spjd } 5361185029Spjd 5362185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 5363185029Spjd} 5364185029Spjd 5365185029Spjd/* 5366185029Spjd * A write to a cache device has completed. Update all headers to allow 5367185029Spjd * reads from these buffers to begin. 5368185029Spjd */ 5369185029Spjdstatic void 5370185029Spjdl2arc_write_done(zio_t *zio) 5371185029Spjd{ 5372185029Spjd l2arc_write_callback_t *cb; 5373185029Spjd l2arc_dev_t *dev; 5374185029Spjd list_t *buflist; 5375275811Sdelphij arc_buf_hdr_t *head, *hdr, *hdr_prev; 5376185029Spjd kmutex_t *hash_lock; 5377268085Sdelphij int64_t bytes_dropped = 0; 5378185029Spjd 5379185029Spjd cb = zio->io_private; 5380185029Spjd ASSERT(cb != NULL); 5381185029Spjd dev = cb->l2wcb_dev; 5382185029Spjd ASSERT(dev != NULL); 5383185029Spjd head = cb->l2wcb_head; 5384185029Spjd ASSERT(head != NULL); 5385286570Smav buflist = &dev->l2ad_buflist; 5386185029Spjd ASSERT(buflist != NULL); 5387185029Spjd DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 5388185029Spjd l2arc_write_callback_t *, cb); 5389185029Spjd 5390185029Spjd if (zio->io_error != 0) 5391185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_error); 5392185029Spjd 5393286570Smav mutex_enter(&dev->l2ad_mtx); 5394185029Spjd 5395185029Spjd /* 5396185029Spjd * All writes completed, or an error was hit. 5397185029Spjd */ 5398275811Sdelphij for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { 5399275811Sdelphij hdr_prev = list_prev(buflist, hdr); 5400185029Spjd 5401275811Sdelphij hash_lock = HDR_LOCK(hdr); 5402185029Spjd if (!mutex_tryenter(hash_lock)) { 5403185029Spjd /* 5404185029Spjd * This buffer misses out. It may be in a stage 5405286570Smav * of eviction. Its ARC_FLAG_L2_WRITING flag will be 5406185029Spjd * left set, denying reads to this buffer. 5407185029Spjd */ 5408185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 5409185029Spjd continue; 5410185029Spjd } 5411185029Spjd 5412286570Smav /* 5413286570Smav * It's possible that this buffer got evicted from the L1 cache 5414286570Smav * before we grabbed the vdev + hash locks, in which case 5415286570Smav * arc_hdr_realloc freed b_tmp_cdata for us if it was allocated. 5416286570Smav * Only free the buffer if we still have an L1 hdr. 5417286570Smav */ 5418286570Smav if (HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_tmp_cdata != NULL && 5419286570Smav HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF) 5420286570Smav l2arc_release_cdata_buf(hdr); 5421286570Smav 5422185029Spjd if (zio->io_error != 0) { 5423185029Spjd /* 5424185029Spjd * Error - drop L2ARC entry. 5425185029Spjd */ 5426286570Smav trim_map_free(hdr->b_l2hdr.b_dev->l2ad_vdev, 5427286570Smav hdr->b_l2hdr.b_daddr, hdr->b_l2hdr.b_asize, 0); 5428286570Smav hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR; 5429286570Smav 5430286570Smav ARCSTAT_INCR(arcstat_l2_asize, -hdr->b_l2hdr.b_asize); 5431275811Sdelphij ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 5432286598Smav 5433286598Smav bytes_dropped += hdr->b_l2hdr.b_asize; 5434286598Smav (void) refcount_remove_many(&dev->l2ad_alloc, 5435286598Smav hdr->b_l2hdr.b_asize, hdr); 5436185029Spjd } 5437185029Spjd 5438185029Spjd /* 5439185029Spjd * Allow ARC to begin reads to this L2ARC entry. 5440185029Spjd */ 5441275811Sdelphij hdr->b_flags &= ~ARC_FLAG_L2_WRITING; 5442185029Spjd 5443185029Spjd mutex_exit(hash_lock); 5444185029Spjd } 5445185029Spjd 5446185029Spjd atomic_inc_64(&l2arc_writes_done); 5447185029Spjd list_remove(buflist, head); 5448286570Smav ASSERT(!HDR_HAS_L1HDR(head)); 5449286570Smav kmem_cache_free(hdr_l2only_cache, head); 5450286570Smav mutex_exit(&dev->l2ad_mtx); 5451185029Spjd 5452268085Sdelphij vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 5453268085Sdelphij 5454185029Spjd l2arc_do_free_on_write(); 5455185029Spjd 5456185029Spjd kmem_free(cb, sizeof (l2arc_write_callback_t)); 5457185029Spjd} 5458185029Spjd 5459185029Spjd/* 5460185029Spjd * A read to a cache device completed. Validate buffer contents before 5461185029Spjd * handing over to the regular ARC routines. 5462185029Spjd */ 5463185029Spjdstatic void 5464185029Spjdl2arc_read_done(zio_t *zio) 5465185029Spjd{ 5466185029Spjd l2arc_read_callback_t *cb; 5467185029Spjd arc_buf_hdr_t *hdr; 5468185029Spjd arc_buf_t *buf; 5469185029Spjd kmutex_t *hash_lock; 5470185029Spjd int equal; 5471185029Spjd 5472185029Spjd ASSERT(zio->io_vd != NULL); 5473185029Spjd ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 5474185029Spjd 5475185029Spjd spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 5476185029Spjd 5477185029Spjd cb = zio->io_private; 5478185029Spjd ASSERT(cb != NULL); 5479185029Spjd buf = cb->l2rcb_buf; 5480185029Spjd ASSERT(buf != NULL); 5481185029Spjd 5482219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 5483185029Spjd mutex_enter(hash_lock); 5484219089Spjd hdr = buf->b_hdr; 5485219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 5486185029Spjd 5487185029Spjd /* 5488251478Sdelphij * If the buffer was compressed, decompress it first. 5489251478Sdelphij */ 5490251478Sdelphij if (cb->l2rcb_compress != ZIO_COMPRESS_OFF) 5491251478Sdelphij l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress); 5492251478Sdelphij ASSERT(zio->io_data != NULL); 5493251478Sdelphij 5494251478Sdelphij /* 5495185029Spjd * Check this survived the L2ARC journey. 5496185029Spjd */ 5497185029Spjd equal = arc_cksum_equal(buf); 5498185029Spjd if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 5499185029Spjd mutex_exit(hash_lock); 5500185029Spjd zio->io_private = buf; 5501185029Spjd zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 5502185029Spjd zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 5503185029Spjd arc_read_done(zio); 5504185029Spjd } else { 5505185029Spjd mutex_exit(hash_lock); 5506185029Spjd /* 5507185029Spjd * Buffer didn't survive caching. Increment stats and 5508185029Spjd * reissue to the original storage device. 5509185029Spjd */ 5510185029Spjd if (zio->io_error != 0) { 5511185029Spjd ARCSTAT_BUMP(arcstat_l2_io_error); 5512185029Spjd } else { 5513249195Smm zio->io_error = SET_ERROR(EIO); 5514185029Spjd } 5515185029Spjd if (!equal) 5516185029Spjd ARCSTAT_BUMP(arcstat_l2_cksum_bad); 5517185029Spjd 5518185029Spjd /* 5519185029Spjd * If there's no waiter, issue an async i/o to the primary 5520185029Spjd * storage now. If there *is* a waiter, the caller must 5521185029Spjd * issue the i/o in a context where it's OK to block. 5522185029Spjd */ 5523209962Smm if (zio->io_waiter == NULL) { 5524209962Smm zio_t *pio = zio_unique_parent(zio); 5525209962Smm 5526209962Smm ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 5527209962Smm 5528209962Smm zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 5529185029Spjd buf->b_data, zio->io_size, arc_read_done, buf, 5530185029Spjd zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 5531209962Smm } 5532185029Spjd } 5533185029Spjd 5534185029Spjd kmem_free(cb, sizeof (l2arc_read_callback_t)); 5535185029Spjd} 5536185029Spjd 5537185029Spjd/* 5538185029Spjd * This is the list priority from which the L2ARC will search for pages to 5539185029Spjd * cache. This is used within loops (0..3) to cycle through lists in the 5540185029Spjd * desired order. This order can have a significant effect on cache 5541185029Spjd * performance. 5542185029Spjd * 5543185029Spjd * Currently the metadata lists are hit first, MFU then MRU, followed by 5544185029Spjd * the data lists. This function returns a locked list, and also returns 5545185029Spjd * the lock pointer. 5546185029Spjd */ 5547185029Spjdstatic list_t * 5548185029Spjdl2arc_list_locked(int list_num, kmutex_t **lock) 5549185029Spjd{ 5550247187Smm list_t *list = NULL; 5551205231Skmacy int idx; 5552185029Spjd 5553206796Spjd ASSERT(list_num >= 0 && list_num < 2 * ARC_BUFC_NUMLISTS); 5554206796Spjd 5555205231Skmacy if (list_num < ARC_BUFC_NUMMETADATALISTS) { 5556205231Skmacy idx = list_num; 5557205231Skmacy list = &arc_mfu->arcs_lists[idx]; 5558205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 5559206796Spjd } else if (list_num < ARC_BUFC_NUMMETADATALISTS * 2) { 5560205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 5561205231Skmacy list = &arc_mru->arcs_lists[idx]; 5562205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 5563206796Spjd } else if (list_num < (ARC_BUFC_NUMMETADATALISTS * 2 + 5564205231Skmacy ARC_BUFC_NUMDATALISTS)) { 5565205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 5566205231Skmacy list = &arc_mfu->arcs_lists[idx]; 5567205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 5568205231Skmacy } else { 5569205231Skmacy idx = list_num - ARC_BUFC_NUMLISTS; 5570205231Skmacy list = &arc_mru->arcs_lists[idx]; 5571205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 5572185029Spjd } 5573185029Spjd 5574185029Spjd ASSERT(!(MUTEX_HELD(*lock))); 5575185029Spjd mutex_enter(*lock); 5576185029Spjd return (list); 5577185029Spjd} 5578185029Spjd 5579185029Spjd/* 5580185029Spjd * Evict buffers from the device write hand to the distance specified in 5581185029Spjd * bytes. This distance may span populated buffers, it may span nothing. 5582185029Spjd * This is clearing a region on the L2ARC device ready for writing. 5583185029Spjd * If the 'all' boolean is set, every buffer is evicted. 5584185029Spjd */ 5585185029Spjdstatic void 5586185029Spjdl2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 5587185029Spjd{ 5588185029Spjd list_t *buflist; 5589275811Sdelphij arc_buf_hdr_t *hdr, *hdr_prev; 5590185029Spjd kmutex_t *hash_lock; 5591185029Spjd uint64_t taddr; 5592185029Spjd 5593286570Smav buflist = &dev->l2ad_buflist; 5594185029Spjd 5595185029Spjd if (!all && dev->l2ad_first) { 5596185029Spjd /* 5597185029Spjd * This is the first sweep through the device. There is 5598185029Spjd * nothing to evict. 5599185029Spjd */ 5600185029Spjd return; 5601185029Spjd } 5602185029Spjd 5603185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 5604185029Spjd /* 5605185029Spjd * When nearing the end of the device, evict to the end 5606185029Spjd * before the device write hand jumps to the start. 5607185029Spjd */ 5608185029Spjd taddr = dev->l2ad_end; 5609185029Spjd } else { 5610185029Spjd taddr = dev->l2ad_hand + distance; 5611185029Spjd } 5612185029Spjd DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 5613185029Spjd uint64_t, taddr, boolean_t, all); 5614185029Spjd 5615185029Spjdtop: 5616286570Smav mutex_enter(&dev->l2ad_mtx); 5617275811Sdelphij for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { 5618275811Sdelphij hdr_prev = list_prev(buflist, hdr); 5619185029Spjd 5620275811Sdelphij hash_lock = HDR_LOCK(hdr); 5621185029Spjd if (!mutex_tryenter(hash_lock)) { 5622185029Spjd /* 5623185029Spjd * Missed the hash lock. Retry. 5624185029Spjd */ 5625185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 5626286570Smav mutex_exit(&dev->l2ad_mtx); 5627185029Spjd mutex_enter(hash_lock); 5628185029Spjd mutex_exit(hash_lock); 5629185029Spjd goto top; 5630185029Spjd } 5631185029Spjd 5632275811Sdelphij if (HDR_L2_WRITE_HEAD(hdr)) { 5633185029Spjd /* 5634185029Spjd * We hit a write head node. Leave it for 5635185029Spjd * l2arc_write_done(). 5636185029Spjd */ 5637275811Sdelphij list_remove(buflist, hdr); 5638185029Spjd mutex_exit(hash_lock); 5639185029Spjd continue; 5640185029Spjd } 5641185029Spjd 5642286570Smav if (!all && HDR_HAS_L2HDR(hdr) && 5643286570Smav (hdr->b_l2hdr.b_daddr > taddr || 5644286570Smav hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { 5645185029Spjd /* 5646185029Spjd * We've evicted to the target address, 5647185029Spjd * or the end of the device. 5648185029Spjd */ 5649185029Spjd mutex_exit(hash_lock); 5650185029Spjd break; 5651185029Spjd } 5652185029Spjd 5653286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 5654286570Smav if (!HDR_HAS_L1HDR(hdr)) { 5655275811Sdelphij ASSERT(!HDR_L2_READING(hdr)); 5656185029Spjd /* 5657185029Spjd * This doesn't exist in the ARC. Destroy. 5658185029Spjd * arc_hdr_destroy() will call list_remove() 5659185029Spjd * and decrement arcstat_l2_size. 5660185029Spjd */ 5661275811Sdelphij arc_change_state(arc_anon, hdr, hash_lock); 5662275811Sdelphij arc_hdr_destroy(hdr); 5663185029Spjd } else { 5664286570Smav ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); 5665286570Smav ARCSTAT_BUMP(arcstat_l2_evict_l1cached); 5666185029Spjd /* 5667185029Spjd * Invalidate issued or about to be issued 5668185029Spjd * reads, since we may be about to write 5669185029Spjd * over this location. 5670185029Spjd */ 5671275811Sdelphij if (HDR_L2_READING(hdr)) { 5672185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_reading); 5673275811Sdelphij hdr->b_flags |= ARC_FLAG_L2_EVICTED; 5674185029Spjd } 5675185029Spjd 5676286598Smav arc_hdr_l2hdr_destroy(hdr); 5677185029Spjd } 5678185029Spjd mutex_exit(hash_lock); 5679185029Spjd } 5680286570Smav mutex_exit(&dev->l2ad_mtx); 5681185029Spjd} 5682185029Spjd 5683185029Spjd/* 5684185029Spjd * Find and write ARC buffers to the L2ARC device. 5685185029Spjd * 5686275811Sdelphij * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid 5687185029Spjd * for reading until they have completed writing. 5688251478Sdelphij * The headroom_boost is an in-out parameter used to maintain headroom boost 5689251478Sdelphij * state between calls to this function. 5690251478Sdelphij * 5691251478Sdelphij * Returns the number of bytes actually written (which may be smaller than 5692251478Sdelphij * the delta by which the device hand has changed due to alignment). 5693185029Spjd */ 5694208373Smmstatic uint64_t 5695251478Sdelphijl2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz, 5696251478Sdelphij boolean_t *headroom_boost) 5697185029Spjd{ 5698275811Sdelphij arc_buf_hdr_t *hdr, *hdr_prev, *head; 5699185029Spjd list_t *list; 5700251478Sdelphij uint64_t write_asize, write_psize, write_sz, headroom, 5701251478Sdelphij buf_compress_minsz; 5702185029Spjd void *buf_data; 5703251478Sdelphij kmutex_t *list_lock; 5704251478Sdelphij boolean_t full; 5705185029Spjd l2arc_write_callback_t *cb; 5706185029Spjd zio_t *pio, *wzio; 5707228103Smm uint64_t guid = spa_load_guid(spa); 5708251478Sdelphij const boolean_t do_headroom_boost = *headroom_boost; 5709185029Spjd int try; 5710185029Spjd 5711185029Spjd ASSERT(dev->l2ad_vdev != NULL); 5712185029Spjd 5713251478Sdelphij /* Lower the flag now, we might want to raise it again later. */ 5714251478Sdelphij *headroom_boost = B_FALSE; 5715251478Sdelphij 5716185029Spjd pio = NULL; 5717251478Sdelphij write_sz = write_asize = write_psize = 0; 5718185029Spjd full = B_FALSE; 5719286570Smav head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); 5720275811Sdelphij head->b_flags |= ARC_FLAG_L2_WRITE_HEAD; 5721286570Smav head->b_flags |= ARC_FLAG_HAS_L2HDR; 5722185029Spjd 5723205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 5724185029Spjd /* 5725251478Sdelphij * We will want to try to compress buffers that are at least 2x the 5726251478Sdelphij * device sector size. 5727251478Sdelphij */ 5728251478Sdelphij buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift; 5729251478Sdelphij 5730251478Sdelphij /* 5731185029Spjd * Copy buffers for L2ARC writing. 5732185029Spjd */ 5733286570Smav mutex_enter(&dev->l2ad_mtx); 5734206796Spjd for (try = 0; try < 2 * ARC_BUFC_NUMLISTS; try++) { 5735251478Sdelphij uint64_t passed_sz = 0; 5736251478Sdelphij 5737185029Spjd list = l2arc_list_locked(try, &list_lock); 5738205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 5739185029Spjd 5740185029Spjd /* 5741185029Spjd * L2ARC fast warmup. 5742185029Spjd * 5743185029Spjd * Until the ARC is warm and starts to evict, read from the 5744185029Spjd * head of the ARC lists rather than the tail. 5745185029Spjd */ 5746185029Spjd if (arc_warm == B_FALSE) 5747275811Sdelphij hdr = list_head(list); 5748185029Spjd else 5749275811Sdelphij hdr = list_tail(list); 5750275811Sdelphij if (hdr == NULL) 5751205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 5752185029Spjd 5753272708Savg headroom = target_sz * l2arc_headroom * 2 / ARC_BUFC_NUMLISTS; 5754251478Sdelphij if (do_headroom_boost) 5755251478Sdelphij headroom = (headroom * l2arc_headroom_boost) / 100; 5756251478Sdelphij 5757275811Sdelphij for (; hdr; hdr = hdr_prev) { 5758251478Sdelphij kmutex_t *hash_lock; 5759251478Sdelphij uint64_t buf_sz; 5760251478Sdelphij 5761185029Spjd if (arc_warm == B_FALSE) 5762275811Sdelphij hdr_prev = list_next(list, hdr); 5763185029Spjd else 5764275811Sdelphij hdr_prev = list_prev(list, hdr); 5765275811Sdelphij ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, hdr->b_size); 5766206796Spjd 5767275811Sdelphij hash_lock = HDR_LOCK(hdr); 5768251478Sdelphij if (!mutex_tryenter(hash_lock)) { 5769205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 5770185029Spjd /* 5771185029Spjd * Skip this buffer rather than waiting. 5772185029Spjd */ 5773185029Spjd continue; 5774185029Spjd } 5775185029Spjd 5776275811Sdelphij passed_sz += hdr->b_size; 5777185029Spjd if (passed_sz > headroom) { 5778185029Spjd /* 5779185029Spjd * Searched too far. 5780185029Spjd */ 5781185029Spjd mutex_exit(hash_lock); 5782205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 5783185029Spjd break; 5784185029Spjd } 5785185029Spjd 5786275811Sdelphij if (!l2arc_write_eligible(guid, hdr)) { 5787185029Spjd mutex_exit(hash_lock); 5788185029Spjd continue; 5789185029Spjd } 5790185029Spjd 5791275811Sdelphij if ((write_sz + hdr->b_size) > target_sz) { 5792185029Spjd full = B_TRUE; 5793185029Spjd mutex_exit(hash_lock); 5794205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_full); 5795185029Spjd break; 5796185029Spjd } 5797185029Spjd 5798185029Spjd if (pio == NULL) { 5799185029Spjd /* 5800185029Spjd * Insert a dummy header on the buflist so 5801185029Spjd * l2arc_write_done() can find where the 5802185029Spjd * write buffers begin without searching. 5803185029Spjd */ 5804286570Smav list_insert_head(&dev->l2ad_buflist, head); 5805185029Spjd 5806185029Spjd cb = kmem_alloc( 5807185029Spjd sizeof (l2arc_write_callback_t), KM_SLEEP); 5808185029Spjd cb->l2wcb_dev = dev; 5809185029Spjd cb->l2wcb_head = head; 5810185029Spjd pio = zio_root(spa, l2arc_write_done, cb, 5811185029Spjd ZIO_FLAG_CANFAIL); 5812205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_pios); 5813185029Spjd } 5814185029Spjd 5815185029Spjd /* 5816185029Spjd * Create and add a new L2ARC header. 5817185029Spjd */ 5818286570Smav hdr->b_l2hdr.b_dev = dev; 5819275811Sdelphij hdr->b_flags |= ARC_FLAG_L2_WRITING; 5820251478Sdelphij /* 5821251478Sdelphij * Temporarily stash the data buffer in b_tmp_cdata. 5822251478Sdelphij * The subsequent write step will pick it up from 5823286570Smav * there. This is because can't access b_l1hdr.b_buf 5824251478Sdelphij * without holding the hash_lock, which we in turn 5825251478Sdelphij * can't access without holding the ARC list locks 5826251478Sdelphij * (which we want to avoid during compression/writing). 5827251478Sdelphij */ 5828286570Smav HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); 5829286570Smav hdr->b_l2hdr.b_asize = hdr->b_size; 5830286570Smav hdr->b_l1hdr.b_tmp_cdata = hdr->b_l1hdr.b_buf->b_data; 5831251478Sdelphij 5832286598Smav /* 5833286598Smav * Explicitly set the b_daddr field to a known 5834286598Smav * value which means "invalid address". This 5835286598Smav * enables us to differentiate which stage of 5836286598Smav * l2arc_write_buffers() the particular header 5837286598Smav * is in (e.g. this loop, or the one below). 5838286598Smav * ARC_FLAG_L2_WRITING is not enough to make 5839286598Smav * this distinction, and we need to know in 5840286598Smav * order to do proper l2arc vdev accounting in 5841286598Smav * arc_release() and arc_hdr_destroy(). 5842286598Smav * 5843286598Smav * Note, we can't use a new flag to distinguish 5844286598Smav * the two stages because we don't hold the 5845286598Smav * header's hash_lock below, in the second stage 5846286598Smav * of this function. Thus, we can't simply 5847286598Smav * change the b_flags field to denote that the 5848286598Smav * IO has been sent. We can change the b_daddr 5849286598Smav * field of the L2 portion, though, since we'll 5850286598Smav * be holding the l2ad_mtx; which is why we're 5851286598Smav * using it to denote the header's state change. 5852286598Smav */ 5853286598Smav hdr->b_l2hdr.b_daddr = L2ARC_ADDR_UNSET; 5854286598Smav 5855275811Sdelphij buf_sz = hdr->b_size; 5856286570Smav hdr->b_flags |= ARC_FLAG_HAS_L2HDR; 5857185029Spjd 5858286570Smav list_insert_head(&dev->l2ad_buflist, hdr); 5859251478Sdelphij 5860185029Spjd /* 5861185029Spjd * Compute and store the buffer cksum before 5862185029Spjd * writing. On debug the cksum is verified first. 5863185029Spjd */ 5864286570Smav arc_cksum_verify(hdr->b_l1hdr.b_buf); 5865286570Smav arc_cksum_compute(hdr->b_l1hdr.b_buf, B_TRUE); 5866185029Spjd 5867185029Spjd mutex_exit(hash_lock); 5868185029Spjd 5869251478Sdelphij write_sz += buf_sz; 5870251478Sdelphij } 5871251478Sdelphij 5872251478Sdelphij mutex_exit(list_lock); 5873251478Sdelphij 5874251478Sdelphij if (full == B_TRUE) 5875251478Sdelphij break; 5876251478Sdelphij } 5877251478Sdelphij 5878251478Sdelphij /* No buffers selected for writing? */ 5879251478Sdelphij if (pio == NULL) { 5880251478Sdelphij ASSERT0(write_sz); 5881286570Smav mutex_exit(&dev->l2ad_mtx); 5882286570Smav ASSERT(!HDR_HAS_L1HDR(head)); 5883286570Smav kmem_cache_free(hdr_l2only_cache, head); 5884251478Sdelphij return (0); 5885251478Sdelphij } 5886251478Sdelphij 5887251478Sdelphij /* 5888251478Sdelphij * Now start writing the buffers. We're starting at the write head 5889251478Sdelphij * and work backwards, retracing the course of the buffer selector 5890251478Sdelphij * loop above. 5891251478Sdelphij */ 5892286570Smav for (hdr = list_prev(&dev->l2ad_buflist, head); hdr; 5893286570Smav hdr = list_prev(&dev->l2ad_buflist, hdr)) { 5894251478Sdelphij uint64_t buf_sz; 5895251478Sdelphij 5896251478Sdelphij /* 5897251478Sdelphij * We shouldn't need to lock the buffer here, since we flagged 5898275811Sdelphij * it as ARC_FLAG_L2_WRITING in the previous step, but we must 5899275811Sdelphij * take care to only access its L2 cache parameters. In 5900286570Smav * particular, hdr->l1hdr.b_buf may be invalid by now due to 5901275811Sdelphij * ARC eviction. 5902251478Sdelphij */ 5903286570Smav hdr->b_l2hdr.b_daddr = dev->l2ad_hand; 5904251478Sdelphij 5905286570Smav if ((HDR_L2COMPRESS(hdr)) && 5906286570Smav hdr->b_l2hdr.b_asize >= buf_compress_minsz) { 5907286570Smav if (l2arc_compress_buf(hdr)) { 5908251478Sdelphij /* 5909251478Sdelphij * If compression succeeded, enable headroom 5910251478Sdelphij * boost on the next scan cycle. 5911251478Sdelphij */ 5912251478Sdelphij *headroom_boost = B_TRUE; 5913251478Sdelphij } 5914251478Sdelphij } 5915251478Sdelphij 5916251478Sdelphij /* 5917251478Sdelphij * Pick up the buffer data we had previously stashed away 5918251478Sdelphij * (and now potentially also compressed). 5919251478Sdelphij */ 5920286570Smav buf_data = hdr->b_l1hdr.b_tmp_cdata; 5921286570Smav buf_sz = hdr->b_l2hdr.b_asize; 5922251478Sdelphij 5923274172Savg /* 5924274172Savg * If the data has not been compressed, then clear b_tmp_cdata 5925274172Savg * to make sure that it points only to a temporary compression 5926274172Savg * buffer. 5927274172Savg */ 5928286570Smav if (!L2ARC_IS_VALID_COMPRESS(HDR_GET_COMPRESS(hdr))) 5929286570Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 5930274172Savg 5931286598Smav /* 5932286598Smav * We need to do this regardless if buf_sz is zero or 5933286598Smav * not, otherwise, when this l2hdr is evicted we'll 5934286598Smav * remove a reference that was never added. 5935286598Smav */ 5936286598Smav (void) refcount_add_many(&dev->l2ad_alloc, buf_sz, hdr); 5937286598Smav 5938251478Sdelphij /* Compression may have squashed the buffer to zero length. */ 5939251478Sdelphij if (buf_sz != 0) { 5940251478Sdelphij uint64_t buf_p_sz; 5941251478Sdelphij 5942185029Spjd wzio = zio_write_phys(pio, dev->l2ad_vdev, 5943185029Spjd dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 5944185029Spjd NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 5945185029Spjd ZIO_FLAG_CANFAIL, B_FALSE); 5946185029Spjd 5947185029Spjd DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 5948185029Spjd zio_t *, wzio); 5949185029Spjd (void) zio_nowait(wzio); 5950185029Spjd 5951251478Sdelphij write_asize += buf_sz; 5952286598Smav 5953185029Spjd /* 5954185029Spjd * Keep the clock hand suitably device-aligned. 5955185029Spjd */ 5956251478Sdelphij buf_p_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 5957251478Sdelphij write_psize += buf_p_sz; 5958251478Sdelphij dev->l2ad_hand += buf_p_sz; 5959185029Spjd } 5960251478Sdelphij } 5961185029Spjd 5962286570Smav mutex_exit(&dev->l2ad_mtx); 5963185029Spjd 5964251478Sdelphij ASSERT3U(write_asize, <=, target_sz); 5965185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_sent); 5966251478Sdelphij ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize); 5967185029Spjd ARCSTAT_INCR(arcstat_l2_size, write_sz); 5968251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, write_asize); 5969275096Sdelphij vdev_space_update(dev->l2ad_vdev, write_asize, 0, 0); 5970185029Spjd 5971185029Spjd /* 5972185029Spjd * Bump device hand to the device start if it is approaching the end. 5973185029Spjd * l2arc_evict() will already have evicted ahead for this case. 5974185029Spjd */ 5975185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 5976185029Spjd dev->l2ad_hand = dev->l2ad_start; 5977185029Spjd dev->l2ad_first = B_FALSE; 5978185029Spjd } 5979185029Spjd 5980208373Smm dev->l2ad_writing = B_TRUE; 5981185029Spjd (void) zio_wait(pio); 5982208373Smm dev->l2ad_writing = B_FALSE; 5983208373Smm 5984251478Sdelphij return (write_asize); 5985185029Spjd} 5986185029Spjd 5987185029Spjd/* 5988251478Sdelphij * Compresses an L2ARC buffer. 5989286570Smav * The data to be compressed must be prefilled in l1hdr.b_tmp_cdata and its 5990251478Sdelphij * size in l2hdr->b_asize. This routine tries to compress the data and 5991251478Sdelphij * depending on the compression result there are three possible outcomes: 5992251478Sdelphij * *) The buffer was incompressible. The original l2hdr contents were left 5993251478Sdelphij * untouched and are ready for writing to an L2 device. 5994251478Sdelphij * *) The buffer was all-zeros, so there is no need to write it to an L2 5995251478Sdelphij * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is 5996251478Sdelphij * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY. 5997251478Sdelphij * *) Compression succeeded and b_tmp_cdata was replaced with a temporary 5998251478Sdelphij * data buffer which holds the compressed data to be written, and b_asize 5999251478Sdelphij * tells us how much data there is. b_compress is set to the appropriate 6000251478Sdelphij * compression algorithm. Once writing is done, invoke 6001251478Sdelphij * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer. 6002251478Sdelphij * 6003251478Sdelphij * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the 6004251478Sdelphij * buffer was incompressible). 6005251478Sdelphij */ 6006251478Sdelphijstatic boolean_t 6007286570Smavl2arc_compress_buf(arc_buf_hdr_t *hdr) 6008251478Sdelphij{ 6009251478Sdelphij void *cdata; 6010268075Sdelphij size_t csize, len, rounded; 6011286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 6012286570Smav l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 6013251478Sdelphij 6014286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 6015286570Smav ASSERT(HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF); 6016286570Smav ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL); 6017251478Sdelphij 6018251478Sdelphij len = l2hdr->b_asize; 6019251478Sdelphij cdata = zio_data_buf_alloc(len); 6020286570Smav ASSERT3P(cdata, !=, NULL); 6021286570Smav csize = zio_compress_data(ZIO_COMPRESS_LZ4, hdr->b_l1hdr.b_tmp_cdata, 6022269086Sdelphij cdata, l2hdr->b_asize); 6023251478Sdelphij 6024251478Sdelphij if (csize == 0) { 6025251478Sdelphij /* zero block, indicate that there's nothing to write */ 6026251478Sdelphij zio_data_buf_free(cdata, len); 6027286570Smav HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_EMPTY); 6028251478Sdelphij l2hdr->b_asize = 0; 6029286570Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 6030251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_zeros); 6031251478Sdelphij return (B_TRUE); 6032274628Savg } 6033274628Savg 6034274628Savg rounded = P2ROUNDUP(csize, 6035274628Savg (size_t)1 << l2hdr->b_dev->l2ad_vdev->vdev_ashift); 6036274628Savg if (rounded < len) { 6037251478Sdelphij /* 6038251478Sdelphij * Compression succeeded, we'll keep the cdata around for 6039251478Sdelphij * writing and release it afterwards. 6040251478Sdelphij */ 6041274628Savg if (rounded > csize) { 6042274628Savg bzero((char *)cdata + csize, rounded - csize); 6043274628Savg csize = rounded; 6044274628Savg } 6045286570Smav HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_LZ4); 6046251478Sdelphij l2hdr->b_asize = csize; 6047286570Smav hdr->b_l1hdr.b_tmp_cdata = cdata; 6048251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_successes); 6049251478Sdelphij return (B_TRUE); 6050251478Sdelphij } else { 6051251478Sdelphij /* 6052251478Sdelphij * Compression failed, release the compressed buffer. 6053251478Sdelphij * l2hdr will be left unmodified. 6054251478Sdelphij */ 6055251478Sdelphij zio_data_buf_free(cdata, len); 6056251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_failures); 6057251478Sdelphij return (B_FALSE); 6058251478Sdelphij } 6059251478Sdelphij} 6060251478Sdelphij 6061251478Sdelphij/* 6062251478Sdelphij * Decompresses a zio read back from an l2arc device. On success, the 6063251478Sdelphij * underlying zio's io_data buffer is overwritten by the uncompressed 6064251478Sdelphij * version. On decompression error (corrupt compressed stream), the 6065251478Sdelphij * zio->io_error value is set to signal an I/O error. 6066251478Sdelphij * 6067251478Sdelphij * Please note that the compressed data stream is not checksummed, so 6068251478Sdelphij * if the underlying device is experiencing data corruption, we may feed 6069251478Sdelphij * corrupt data to the decompressor, so the decompressor needs to be 6070251478Sdelphij * able to handle this situation (LZ4 does). 6071251478Sdelphij */ 6072251478Sdelphijstatic void 6073251478Sdelphijl2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c) 6074251478Sdelphij{ 6075251478Sdelphij ASSERT(L2ARC_IS_VALID_COMPRESS(c)); 6076251478Sdelphij 6077251478Sdelphij if (zio->io_error != 0) { 6078251478Sdelphij /* 6079251478Sdelphij * An io error has occured, just restore the original io 6080251478Sdelphij * size in preparation for a main pool read. 6081251478Sdelphij */ 6082251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 6083251478Sdelphij return; 6084251478Sdelphij } 6085251478Sdelphij 6086251478Sdelphij if (c == ZIO_COMPRESS_EMPTY) { 6087251478Sdelphij /* 6088251478Sdelphij * An empty buffer results in a null zio, which means we 6089251478Sdelphij * need to fill its io_data after we're done restoring the 6090251478Sdelphij * buffer's contents. 6091251478Sdelphij */ 6092286570Smav ASSERT(hdr->b_l1hdr.b_buf != NULL); 6093286570Smav bzero(hdr->b_l1hdr.b_buf->b_data, hdr->b_size); 6094286570Smav zio->io_data = zio->io_orig_data = hdr->b_l1hdr.b_buf->b_data; 6095251478Sdelphij } else { 6096251478Sdelphij ASSERT(zio->io_data != NULL); 6097251478Sdelphij /* 6098251478Sdelphij * We copy the compressed data from the start of the arc buffer 6099251478Sdelphij * (the zio_read will have pulled in only what we need, the 6100251478Sdelphij * rest is garbage which we will overwrite at decompression) 6101251478Sdelphij * and then decompress back to the ARC data buffer. This way we 6102251478Sdelphij * can minimize copying by simply decompressing back over the 6103251478Sdelphij * original compressed data (rather than decompressing to an 6104251478Sdelphij * aux buffer and then copying back the uncompressed buffer, 6105251478Sdelphij * which is likely to be much larger). 6106251478Sdelphij */ 6107251478Sdelphij uint64_t csize; 6108251478Sdelphij void *cdata; 6109251478Sdelphij 6110251478Sdelphij csize = zio->io_size; 6111251478Sdelphij cdata = zio_data_buf_alloc(csize); 6112251478Sdelphij bcopy(zio->io_data, cdata, csize); 6113251478Sdelphij if (zio_decompress_data(c, cdata, zio->io_data, csize, 6114251478Sdelphij hdr->b_size) != 0) 6115251478Sdelphij zio->io_error = EIO; 6116251478Sdelphij zio_data_buf_free(cdata, csize); 6117251478Sdelphij } 6118251478Sdelphij 6119251478Sdelphij /* Restore the expected uncompressed IO size. */ 6120251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 6121251478Sdelphij} 6122251478Sdelphij 6123251478Sdelphij/* 6124251478Sdelphij * Releases the temporary b_tmp_cdata buffer in an l2arc header structure. 6125251478Sdelphij * This buffer serves as a temporary holder of compressed data while 6126251478Sdelphij * the buffer entry is being written to an l2arc device. Once that is 6127251478Sdelphij * done, we can dispose of it. 6128251478Sdelphij */ 6129251478Sdelphijstatic void 6130275811Sdelphijl2arc_release_cdata_buf(arc_buf_hdr_t *hdr) 6131251478Sdelphij{ 6132286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 6133286570Smav if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_EMPTY) { 6134251478Sdelphij /* 6135251478Sdelphij * If the data was compressed, then we've allocated a 6136251478Sdelphij * temporary buffer for it, so now we need to release it. 6137251478Sdelphij */ 6138286570Smav ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL); 6139286570Smav zio_data_buf_free(hdr->b_l1hdr.b_tmp_cdata, 6140286570Smav hdr->b_size); 6141286570Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 6142274172Savg } else { 6143286570Smav ASSERT(hdr->b_l1hdr.b_tmp_cdata == NULL); 6144251478Sdelphij } 6145251478Sdelphij} 6146251478Sdelphij 6147251478Sdelphij/* 6148185029Spjd * This thread feeds the L2ARC at regular intervals. This is the beating 6149185029Spjd * heart of the L2ARC. 6150185029Spjd */ 6151185029Spjdstatic void 6152185029Spjdl2arc_feed_thread(void *dummy __unused) 6153185029Spjd{ 6154185029Spjd callb_cpr_t cpr; 6155185029Spjd l2arc_dev_t *dev; 6156185029Spjd spa_t *spa; 6157208373Smm uint64_t size, wrote; 6158219089Spjd clock_t begin, next = ddi_get_lbolt(); 6159251478Sdelphij boolean_t headroom_boost = B_FALSE; 6160185029Spjd 6161185029Spjd CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 6162185029Spjd 6163185029Spjd mutex_enter(&l2arc_feed_thr_lock); 6164185029Spjd 6165185029Spjd while (l2arc_thread_exit == 0) { 6166185029Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 6167185029Spjd (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 6168219089Spjd next - ddi_get_lbolt()); 6169185029Spjd CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 6170219089Spjd next = ddi_get_lbolt() + hz; 6171185029Spjd 6172185029Spjd /* 6173185029Spjd * Quick check for L2ARC devices. 6174185029Spjd */ 6175185029Spjd mutex_enter(&l2arc_dev_mtx); 6176185029Spjd if (l2arc_ndev == 0) { 6177185029Spjd mutex_exit(&l2arc_dev_mtx); 6178185029Spjd continue; 6179185029Spjd } 6180185029Spjd mutex_exit(&l2arc_dev_mtx); 6181219089Spjd begin = ddi_get_lbolt(); 6182185029Spjd 6183185029Spjd /* 6184185029Spjd * This selects the next l2arc device to write to, and in 6185185029Spjd * doing so the next spa to feed from: dev->l2ad_spa. This 6186185029Spjd * will return NULL if there are now no l2arc devices or if 6187185029Spjd * they are all faulted. 6188185029Spjd * 6189185029Spjd * If a device is returned, its spa's config lock is also 6190185029Spjd * held to prevent device removal. l2arc_dev_get_next() 6191185029Spjd * will grab and release l2arc_dev_mtx. 6192185029Spjd */ 6193185029Spjd if ((dev = l2arc_dev_get_next()) == NULL) 6194185029Spjd continue; 6195185029Spjd 6196185029Spjd spa = dev->l2ad_spa; 6197185029Spjd ASSERT(spa != NULL); 6198185029Spjd 6199185029Spjd /* 6200219089Spjd * If the pool is read-only then force the feed thread to 6201219089Spjd * sleep a little longer. 6202219089Spjd */ 6203219089Spjd if (!spa_writeable(spa)) { 6204219089Spjd next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 6205219089Spjd spa_config_exit(spa, SCL_L2ARC, dev); 6206219089Spjd continue; 6207219089Spjd } 6208219089Spjd 6209219089Spjd /* 6210185029Spjd * Avoid contributing to memory pressure. 6211185029Spjd */ 6212185029Spjd if (arc_reclaim_needed()) { 6213185029Spjd ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 6214185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 6215185029Spjd continue; 6216185029Spjd } 6217185029Spjd 6218185029Spjd ARCSTAT_BUMP(arcstat_l2_feeds); 6219185029Spjd 6220251478Sdelphij size = l2arc_write_size(); 6221185029Spjd 6222185029Spjd /* 6223185029Spjd * Evict L2ARC buffers that will be overwritten. 6224185029Spjd */ 6225185029Spjd l2arc_evict(dev, size, B_FALSE); 6226185029Spjd 6227185029Spjd /* 6228185029Spjd * Write ARC buffers. 6229185029Spjd */ 6230251478Sdelphij wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost); 6231208373Smm 6232208373Smm /* 6233208373Smm * Calculate interval between writes. 6234208373Smm */ 6235208373Smm next = l2arc_write_interval(begin, size, wrote); 6236185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 6237185029Spjd } 6238185029Spjd 6239185029Spjd l2arc_thread_exit = 0; 6240185029Spjd cv_broadcast(&l2arc_feed_thr_cv); 6241185029Spjd CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 6242185029Spjd thread_exit(); 6243185029Spjd} 6244185029Spjd 6245185029Spjdboolean_t 6246185029Spjdl2arc_vdev_present(vdev_t *vd) 6247185029Spjd{ 6248185029Spjd l2arc_dev_t *dev; 6249185029Spjd 6250185029Spjd mutex_enter(&l2arc_dev_mtx); 6251185029Spjd for (dev = list_head(l2arc_dev_list); dev != NULL; 6252185029Spjd dev = list_next(l2arc_dev_list, dev)) { 6253185029Spjd if (dev->l2ad_vdev == vd) 6254185029Spjd break; 6255185029Spjd } 6256185029Spjd mutex_exit(&l2arc_dev_mtx); 6257185029Spjd 6258185029Spjd return (dev != NULL); 6259185029Spjd} 6260185029Spjd 6261185029Spjd/* 6262185029Spjd * Add a vdev for use by the L2ARC. By this point the spa has already 6263185029Spjd * validated the vdev and opened it. 6264185029Spjd */ 6265185029Spjdvoid 6266219089Spjdl2arc_add_vdev(spa_t *spa, vdev_t *vd) 6267185029Spjd{ 6268185029Spjd l2arc_dev_t *adddev; 6269185029Spjd 6270185029Spjd ASSERT(!l2arc_vdev_present(vd)); 6271185029Spjd 6272255753Sgibbs vdev_ashift_optimize(vd); 6273255753Sgibbs 6274185029Spjd /* 6275185029Spjd * Create a new l2arc device entry. 6276185029Spjd */ 6277185029Spjd adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 6278185029Spjd adddev->l2ad_spa = spa; 6279185029Spjd adddev->l2ad_vdev = vd; 6280219089Spjd adddev->l2ad_start = VDEV_LABEL_START_SIZE; 6281219089Spjd adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 6282185029Spjd adddev->l2ad_hand = adddev->l2ad_start; 6283185029Spjd adddev->l2ad_first = B_TRUE; 6284208373Smm adddev->l2ad_writing = B_FALSE; 6285185029Spjd 6286286570Smav mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); 6287185029Spjd /* 6288185029Spjd * This is a list of all ARC buffers that are still valid on the 6289185029Spjd * device. 6290185029Spjd */ 6291286570Smav list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 6292286570Smav offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); 6293185029Spjd 6294219089Spjd vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 6295286598Smav refcount_create(&adddev->l2ad_alloc); 6296185029Spjd 6297185029Spjd /* 6298185029Spjd * Add device to global list 6299185029Spjd */ 6300185029Spjd mutex_enter(&l2arc_dev_mtx); 6301185029Spjd list_insert_head(l2arc_dev_list, adddev); 6302185029Spjd atomic_inc_64(&l2arc_ndev); 6303185029Spjd mutex_exit(&l2arc_dev_mtx); 6304185029Spjd} 6305185029Spjd 6306185029Spjd/* 6307185029Spjd * Remove a vdev from the L2ARC. 6308185029Spjd */ 6309185029Spjdvoid 6310185029Spjdl2arc_remove_vdev(vdev_t *vd) 6311185029Spjd{ 6312185029Spjd l2arc_dev_t *dev, *nextdev, *remdev = NULL; 6313185029Spjd 6314185029Spjd /* 6315185029Spjd * Find the device by vdev 6316185029Spjd */ 6317185029Spjd mutex_enter(&l2arc_dev_mtx); 6318185029Spjd for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 6319185029Spjd nextdev = list_next(l2arc_dev_list, dev); 6320185029Spjd if (vd == dev->l2ad_vdev) { 6321185029Spjd remdev = dev; 6322185029Spjd break; 6323185029Spjd } 6324185029Spjd } 6325185029Spjd ASSERT(remdev != NULL); 6326185029Spjd 6327185029Spjd /* 6328185029Spjd * Remove device from global list 6329185029Spjd */ 6330185029Spjd list_remove(l2arc_dev_list, remdev); 6331185029Spjd l2arc_dev_last = NULL; /* may have been invalidated */ 6332185029Spjd atomic_dec_64(&l2arc_ndev); 6333185029Spjd mutex_exit(&l2arc_dev_mtx); 6334185029Spjd 6335185029Spjd /* 6336185029Spjd * Clear all buflists and ARC references. L2ARC device flush. 6337185029Spjd */ 6338185029Spjd l2arc_evict(remdev, 0, B_TRUE); 6339286570Smav list_destroy(&remdev->l2ad_buflist); 6340286570Smav mutex_destroy(&remdev->l2ad_mtx); 6341286598Smav refcount_destroy(&remdev->l2ad_alloc); 6342185029Spjd kmem_free(remdev, sizeof (l2arc_dev_t)); 6343185029Spjd} 6344185029Spjd 6345185029Spjdvoid 6346185029Spjdl2arc_init(void) 6347185029Spjd{ 6348185029Spjd l2arc_thread_exit = 0; 6349185029Spjd l2arc_ndev = 0; 6350185029Spjd l2arc_writes_sent = 0; 6351185029Spjd l2arc_writes_done = 0; 6352185029Spjd 6353185029Spjd mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 6354185029Spjd cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 6355185029Spjd mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 6356185029Spjd mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 6357185029Spjd 6358185029Spjd l2arc_dev_list = &L2ARC_dev_list; 6359185029Spjd l2arc_free_on_write = &L2ARC_free_on_write; 6360185029Spjd list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 6361185029Spjd offsetof(l2arc_dev_t, l2ad_node)); 6362185029Spjd list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 6363185029Spjd offsetof(l2arc_data_free_t, l2df_list_node)); 6364185029Spjd} 6365185029Spjd 6366185029Spjdvoid 6367185029Spjdl2arc_fini(void) 6368185029Spjd{ 6369185029Spjd /* 6370185029Spjd * This is called from dmu_fini(), which is called from spa_fini(); 6371185029Spjd * Because of this, we can assume that all l2arc devices have 6372185029Spjd * already been removed when the pools themselves were removed. 6373185029Spjd */ 6374185029Spjd 6375185029Spjd l2arc_do_free_on_write(); 6376185029Spjd 6377185029Spjd mutex_destroy(&l2arc_feed_thr_lock); 6378185029Spjd cv_destroy(&l2arc_feed_thr_cv); 6379185029Spjd mutex_destroy(&l2arc_dev_mtx); 6380185029Spjd mutex_destroy(&l2arc_free_on_write_mtx); 6381185029Spjd 6382185029Spjd list_destroy(l2arc_dev_list); 6383185029Spjd list_destroy(l2arc_free_on_write); 6384185029Spjd} 6385185029Spjd 6386185029Spjdvoid 6387185029Spjdl2arc_start(void) 6388185029Spjd{ 6389209962Smm if (!(spa_mode_global & FWRITE)) 6390185029Spjd return; 6391185029Spjd 6392185029Spjd (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 6393185029Spjd TS_RUN, minclsyspri); 6394185029Spjd} 6395185029Spjd 6396185029Spjdvoid 6397185029Spjdl2arc_stop(void) 6398185029Spjd{ 6399209962Smm if (!(spa_mode_global & FWRITE)) 6400185029Spjd return; 6401185029Spjd 6402185029Spjd mutex_enter(&l2arc_feed_thr_lock); 6403185029Spjd cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 6404185029Spjd l2arc_thread_exit = 1; 6405185029Spjd while (l2arc_thread_exit != 0) 6406185029Spjd cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 6407185029Spjd mutex_exit(&l2arc_feed_thr_lock); 6408185029Spjd} 6409