arc.c revision 274172
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. 23268123Sdelphij * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 24260835Sdelphij * Copyright (c) 2014 by Saso Kiselkov. All rights reserved. 25268085Sdelphij * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 26168404Spjd */ 27168404Spjd 28168404Spjd/* 29168404Spjd * DVA-based Adjustable Replacement Cache 30168404Spjd * 31168404Spjd * While much of the theory of operation used here is 32168404Spjd * based on the self-tuning, low overhead replacement cache 33168404Spjd * presented by Megiddo and Modha at FAST 2003, there are some 34168404Spjd * significant differences: 35168404Spjd * 36168404Spjd * 1. The Megiddo and Modha model assumes any page is evictable. 37168404Spjd * Pages in its cache cannot be "locked" into memory. This makes 38168404Spjd * the eviction algorithm simple: evict the last page in the list. 39168404Spjd * This also make the performance characteristics easy to reason 40168404Spjd * about. Our cache is not so simple. At any given moment, some 41168404Spjd * subset of the blocks in the cache are un-evictable because we 42168404Spjd * have handed out a reference to them. Blocks are only evictable 43168404Spjd * when there are no external references active. This makes 44168404Spjd * eviction far more problematic: we choose to evict the evictable 45168404Spjd * blocks that are the "lowest" in the list. 46168404Spjd * 47168404Spjd * There are times when it is not possible to evict the requested 48168404Spjd * space. In these circumstances we are unable to adjust the cache 49168404Spjd * size. To prevent the cache growing unbounded at these times we 50185029Spjd * implement a "cache throttle" that slows the flow of new data 51185029Spjd * into the cache until we can make space available. 52168404Spjd * 53168404Spjd * 2. The Megiddo and Modha model assumes a fixed cache size. 54168404Spjd * Pages are evicted when the cache is full and there is a cache 55168404Spjd * miss. Our model has a variable sized cache. It grows with 56185029Spjd * high use, but also tries to react to memory pressure from the 57168404Spjd * operating system: decreasing its size when system memory is 58168404Spjd * tight. 59168404Spjd * 60168404Spjd * 3. The Megiddo and Modha model assumes a fixed page size. All 61251631Sdelphij * elements of the cache are therefore exactly the same size. So 62168404Spjd * when adjusting the cache size following a cache miss, its simply 63168404Spjd * a matter of choosing a single page to evict. In our model, we 64168404Spjd * have variable sized cache blocks (rangeing from 512 bytes to 65251631Sdelphij * 128K bytes). We therefore choose a set of blocks to evict to make 66168404Spjd * space for a cache miss that approximates as closely as possible 67168404Spjd * the space used by the new block. 68168404Spjd * 69168404Spjd * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 70168404Spjd * by N. Megiddo & D. Modha, FAST 2003 71168404Spjd */ 72168404Spjd 73168404Spjd/* 74168404Spjd * The locking model: 75168404Spjd * 76168404Spjd * A new reference to a cache buffer can be obtained in two 77168404Spjd * ways: 1) via a hash table lookup using the DVA as a key, 78185029Spjd * or 2) via one of the ARC lists. The arc_read() interface 79168404Spjd * uses method 1, while the internal arc algorithms for 80251631Sdelphij * adjusting the cache use method 2. We therefore provide two 81168404Spjd * types of locks: 1) the hash table lock array, and 2) the 82168404Spjd * arc list locks. 83168404Spjd * 84168404Spjd * Buffers do not have their own mutexs, rather they rely on the 85168404Spjd * hash table mutexs for the bulk of their protection (i.e. most 86168404Spjd * fields in the arc_buf_hdr_t are protected by these mutexs). 87168404Spjd * 88168404Spjd * buf_hash_find() returns the appropriate mutex (held) when it 89168404Spjd * locates the requested buffer in the hash table. It returns 90168404Spjd * NULL for the mutex if the buffer was not in the table. 91168404Spjd * 92168404Spjd * buf_hash_remove() expects the appropriate hash mutex to be 93168404Spjd * already held before it is invoked. 94168404Spjd * 95168404Spjd * Each arc state also has a mutex which is used to protect the 96168404Spjd * buffer list associated with the state. When attempting to 97168404Spjd * obtain a hash table lock while holding an arc list lock you 98168404Spjd * must use: mutex_tryenter() to avoid deadlock. Also note that 99168404Spjd * the active state mutex must be held before the ghost state mutex. 100168404Spjd * 101168404Spjd * Arc buffers may have an associated eviction callback function. 102168404Spjd * This function will be invoked prior to removing the buffer (e.g. 103168404Spjd * in arc_do_user_evicts()). Note however that the data associated 104168404Spjd * with the buffer may be evicted prior to the callback. The callback 105168404Spjd * must be made with *no locks held* (to prevent deadlock). Additionally, 106168404Spjd * the users of callbacks must ensure that their private data is 107268858Sdelphij * protected from simultaneous callbacks from arc_clear_callback() 108168404Spjd * and arc_do_user_evicts(). 109168404Spjd * 110168404Spjd * Note that the majority of the performance stats are manipulated 111168404Spjd * with atomic operations. 112185029Spjd * 113185029Spjd * The L2ARC uses the l2arc_buflist_mtx global mutex for the following: 114185029Spjd * 115185029Spjd * - L2ARC buflist creation 116185029Spjd * - L2ARC buflist eviction 117185029Spjd * - L2ARC write completion, which walks L2ARC buflists 118185029Spjd * - ARC header destruction, as it removes from L2ARC buflists 119185029Spjd * - ARC header release, as it removes from L2ARC buflists 120168404Spjd */ 121168404Spjd 122168404Spjd#include <sys/spa.h> 123168404Spjd#include <sys/zio.h> 124251478Sdelphij#include <sys/zio_compress.h> 125168404Spjd#include <sys/zfs_context.h> 126168404Spjd#include <sys/arc.h> 127168404Spjd#include <sys/refcount.h> 128185029Spjd#include <sys/vdev.h> 129219089Spjd#include <sys/vdev_impl.h> 130258632Savg#include <sys/dsl_pool.h> 131168404Spjd#ifdef _KERNEL 132168404Spjd#include <sys/dnlc.h> 133168404Spjd#endif 134168404Spjd#include <sys/callb.h> 135168404Spjd#include <sys/kstat.h> 136248572Ssmh#include <sys/trim_map.h> 137219089Spjd#include <zfs_fletcher.h> 138168404Spjd#include <sys/sdt.h> 139168404Spjd 140191902Skmacy#include <vm/vm_pageout.h> 141272483Ssmh#include <machine/vmparam.h> 142191902Skmacy 143240133Smm#ifdef illumos 144240133Smm#ifndef _KERNEL 145240133Smm/* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ 146240133Smmboolean_t arc_watch = B_FALSE; 147240133Smmint arc_procfd; 148240133Smm#endif 149240133Smm#endif /* illumos */ 150240133Smm 151168404Spjdstatic kmutex_t arc_reclaim_thr_lock; 152168404Spjdstatic kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 153168404Spjdstatic uint8_t arc_thread_exit; 154168404Spjd 155168404Spjd#define ARC_REDUCE_DNLC_PERCENT 3 156168404Spjduint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 157168404Spjd 158168404Spjdtypedef enum arc_reclaim_strategy { 159168404Spjd ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 160168404Spjd ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 161168404Spjd} arc_reclaim_strategy_t; 162168404Spjd 163258632Savg/* 164258632Savg * The number of iterations through arc_evict_*() before we 165258632Savg * drop & reacquire the lock. 166258632Savg */ 167258632Savgint arc_evict_iterations = 100; 168258632Savg 169168404Spjd/* number of seconds before growing cache again */ 170168404Spjdstatic int arc_grow_retry = 60; 171168404Spjd 172208373Smm/* shift of arc_c for calculating both min and max arc_p */ 173208373Smmstatic int arc_p_min_shift = 4; 174208373Smm 175208373Smm/* log2(fraction of arc to reclaim) */ 176208373Smmstatic int arc_shrink_shift = 5; 177208373Smm 178168404Spjd/* 179168404Spjd * minimum lifespan of a prefetch block in clock ticks 180168404Spjd * (initialized in arc_init()) 181168404Spjd */ 182168404Spjdstatic int arc_min_prefetch_lifespan; 183168404Spjd 184258632Savg/* 185258632Savg * If this percent of memory is free, don't throttle. 186258632Savg */ 187258632Savgint arc_lotsfree_percent = 10; 188258632Savg 189208373Smmstatic int arc_dead; 190194043Skmacyextern int zfs_prefetch_disable; 191168404Spjd 192168404Spjd/* 193185029Spjd * The arc has filled available memory and has now warmed up. 194185029Spjd */ 195185029Spjdstatic boolean_t arc_warm; 196185029Spjd 197185029Spjduint64_t zfs_arc_max; 198185029Spjduint64_t zfs_arc_min; 199185029Spjduint64_t zfs_arc_meta_limit = 0; 200208373Smmint zfs_arc_grow_retry = 0; 201208373Smmint zfs_arc_shrink_shift = 0; 202208373Smmint zfs_arc_p_min_shift = 0; 203242845Sdelphijint zfs_disable_dup_eviction = 0; 204269230Sdelphijuint64_t zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ 205272483Ssmhu_int zfs_arc_free_target = 0; 206185029Spjd 207270759Ssmhstatic int sysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS); 208270759Ssmh 209270759Ssmh#ifdef _KERNEL 210270759Ssmhstatic void 211270759Ssmharc_free_target_init(void *unused __unused) 212270759Ssmh{ 213270759Ssmh 214272483Ssmh zfs_arc_free_target = vm_pageout_wakeup_thresh; 215270759Ssmh} 216270759SsmhSYSINIT(arc_free_target_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_ANY, 217270759Ssmh arc_free_target_init, NULL); 218270759Ssmh 219185029SpjdTUNABLE_QUAD("vfs.zfs.arc_meta_limit", &zfs_arc_meta_limit); 220273026SdelphijTUNABLE_INT("vfs.zfs.arc_shrink_shift", &zfs_arc_shrink_shift); 221168473SpjdSYSCTL_DECL(_vfs_zfs); 222217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_max, CTLFLAG_RDTUN, &zfs_arc_max, 0, 223168473Spjd "Maximum ARC size"); 224217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_min, CTLFLAG_RDTUN, &zfs_arc_min, 0, 225168473Spjd "Minimum ARC size"); 226269230SdelphijSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_average_blocksize, CTLFLAG_RDTUN, 227269230Sdelphij &zfs_arc_average_blocksize, 0, 228269230Sdelphij "ARC average blocksize"); 229273026SdelphijSYSCTL_INT(_vfs_zfs, OID_AUTO, arc_shrink_shift, CTLFLAG_RW, 230273026Sdelphij &arc_shrink_shift, 0, 231273026Sdelphij "log2(fraction of arc to reclaim)"); 232273026Sdelphij 233270759Ssmh/* 234270759Ssmh * We don't have a tunable for arc_free_target due to the dependency on 235270759Ssmh * pagedaemon initialisation. 236270759Ssmh */ 237270759SsmhSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_free_target, 238270759Ssmh CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(u_int), 239270759Ssmh sysctl_vfs_zfs_arc_free_target, "IU", 240270759Ssmh "Desired number of free pages below which ARC triggers reclaim"); 241168404Spjd 242270759Ssmhstatic int 243270759Ssmhsysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS) 244270759Ssmh{ 245270759Ssmh u_int val; 246270759Ssmh int err; 247270759Ssmh 248270759Ssmh val = zfs_arc_free_target; 249270759Ssmh err = sysctl_handle_int(oidp, &val, 0, req); 250270759Ssmh if (err != 0 || req->newptr == NULL) 251270759Ssmh return (err); 252270759Ssmh 253272483Ssmh if (val < minfree) 254270759Ssmh return (EINVAL); 255272483Ssmh if (val > vm_cnt.v_page_count) 256270759Ssmh return (EINVAL); 257270759Ssmh 258270759Ssmh zfs_arc_free_target = val; 259270759Ssmh 260270759Ssmh return (0); 261270759Ssmh} 262272483Ssmh#endif 263270759Ssmh 264168404Spjd/* 265185029Spjd * Note that buffers can be in one of 6 states: 266168404Spjd * ARC_anon - anonymous (discussed below) 267168404Spjd * ARC_mru - recently used, currently cached 268168404Spjd * ARC_mru_ghost - recentely used, no longer in cache 269168404Spjd * ARC_mfu - frequently used, currently cached 270168404Spjd * ARC_mfu_ghost - frequently used, no longer in cache 271185029Spjd * ARC_l2c_only - exists in L2ARC but not other states 272185029Spjd * When there are no active references to the buffer, they are 273185029Spjd * are linked onto a list in one of these arc states. These are 274185029Spjd * the only buffers that can be evicted or deleted. Within each 275185029Spjd * state there are multiple lists, one for meta-data and one for 276185029Spjd * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 277185029Spjd * etc.) is tracked separately so that it can be managed more 278185029Spjd * explicitly: favored over data, limited explicitly. 279168404Spjd * 280168404Spjd * Anonymous buffers are buffers that are not associated with 281168404Spjd * a DVA. These are buffers that hold dirty block copies 282168404Spjd * before they are written to stable storage. By definition, 283168404Spjd * they are "ref'd" and are considered part of arc_mru 284168404Spjd * that cannot be freed. Generally, they will aquire a DVA 285168404Spjd * as they are written and migrate onto the arc_mru list. 286185029Spjd * 287185029Spjd * The ARC_l2c_only state is for buffers that are in the second 288185029Spjd * level ARC but no longer in any of the ARC_m* lists. The second 289185029Spjd * level ARC itself may also contain buffers that are in any of 290185029Spjd * the ARC_m* states - meaning that a buffer can exist in two 291185029Spjd * places. The reason for the ARC_l2c_only state is to keep the 292185029Spjd * buffer header in the hash table, so that reads that hit the 293185029Spjd * second level ARC benefit from these fast lookups. 294168404Spjd */ 295168404Spjd 296205264Skmacy#define ARCS_LOCK_PAD CACHE_LINE_SIZE 297205231Skmacystruct arcs_lock { 298205231Skmacy kmutex_t arcs_lock; 299205231Skmacy#ifdef _KERNEL 300205231Skmacy unsigned char pad[(ARCS_LOCK_PAD - sizeof (kmutex_t))]; 301205231Skmacy#endif 302205231Skmacy}; 303205231Skmacy 304205231Skmacy/* 305205231Skmacy * must be power of two for mask use to work 306205231Skmacy * 307205231Skmacy */ 308205231Skmacy#define ARC_BUFC_NUMDATALISTS 16 309205231Skmacy#define ARC_BUFC_NUMMETADATALISTS 16 310206796Spjd#define ARC_BUFC_NUMLISTS (ARC_BUFC_NUMMETADATALISTS + ARC_BUFC_NUMDATALISTS) 311205231Skmacy 312168404Spjdtypedef struct arc_state { 313185029Spjd uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 314185029Spjd uint64_t arcs_size; /* total amount of data in this state */ 315205231Skmacy list_t arcs_lists[ARC_BUFC_NUMLISTS]; /* list of evictable buffers */ 316205264Skmacy struct arcs_lock arcs_locks[ARC_BUFC_NUMLISTS] __aligned(CACHE_LINE_SIZE); 317168404Spjd} arc_state_t; 318168404Spjd 319206796Spjd#define ARCS_LOCK(s, i) (&((s)->arcs_locks[(i)].arcs_lock)) 320205231Skmacy 321185029Spjd/* The 6 states: */ 322168404Spjdstatic arc_state_t ARC_anon; 323168404Spjdstatic arc_state_t ARC_mru; 324168404Spjdstatic arc_state_t ARC_mru_ghost; 325168404Spjdstatic arc_state_t ARC_mfu; 326168404Spjdstatic arc_state_t ARC_mfu_ghost; 327185029Spjdstatic arc_state_t ARC_l2c_only; 328168404Spjd 329168404Spjdtypedef struct arc_stats { 330168404Spjd kstat_named_t arcstat_hits; 331168404Spjd kstat_named_t arcstat_misses; 332168404Spjd kstat_named_t arcstat_demand_data_hits; 333168404Spjd kstat_named_t arcstat_demand_data_misses; 334168404Spjd kstat_named_t arcstat_demand_metadata_hits; 335168404Spjd kstat_named_t arcstat_demand_metadata_misses; 336168404Spjd kstat_named_t arcstat_prefetch_data_hits; 337168404Spjd kstat_named_t arcstat_prefetch_data_misses; 338168404Spjd kstat_named_t arcstat_prefetch_metadata_hits; 339168404Spjd kstat_named_t arcstat_prefetch_metadata_misses; 340168404Spjd kstat_named_t arcstat_mru_hits; 341168404Spjd kstat_named_t arcstat_mru_ghost_hits; 342168404Spjd kstat_named_t arcstat_mfu_hits; 343168404Spjd kstat_named_t arcstat_mfu_ghost_hits; 344205231Skmacy kstat_named_t arcstat_allocated; 345168404Spjd kstat_named_t arcstat_deleted; 346205231Skmacy kstat_named_t arcstat_stolen; 347168404Spjd kstat_named_t arcstat_recycle_miss; 348251629Sdelphij /* 349251629Sdelphij * Number of buffers that could not be evicted because the hash lock 350251629Sdelphij * was held by another thread. The lock may not necessarily be held 351251629Sdelphij * by something using the same buffer, since hash locks are shared 352251629Sdelphij * by multiple buffers. 353251629Sdelphij */ 354168404Spjd kstat_named_t arcstat_mutex_miss; 355251629Sdelphij /* 356251629Sdelphij * Number of buffers skipped because they have I/O in progress, are 357251629Sdelphij * indrect prefetch buffers that have not lived long enough, or are 358251629Sdelphij * not from the spa we're trying to evict from. 359251629Sdelphij */ 360168404Spjd kstat_named_t arcstat_evict_skip; 361208373Smm kstat_named_t arcstat_evict_l2_cached; 362208373Smm kstat_named_t arcstat_evict_l2_eligible; 363208373Smm kstat_named_t arcstat_evict_l2_ineligible; 364168404Spjd kstat_named_t arcstat_hash_elements; 365168404Spjd kstat_named_t arcstat_hash_elements_max; 366168404Spjd kstat_named_t arcstat_hash_collisions; 367168404Spjd kstat_named_t arcstat_hash_chains; 368168404Spjd kstat_named_t arcstat_hash_chain_max; 369168404Spjd kstat_named_t arcstat_p; 370168404Spjd kstat_named_t arcstat_c; 371168404Spjd kstat_named_t arcstat_c_min; 372168404Spjd kstat_named_t arcstat_c_max; 373168404Spjd kstat_named_t arcstat_size; 374185029Spjd kstat_named_t arcstat_hdr_size; 375208373Smm kstat_named_t arcstat_data_size; 376208373Smm kstat_named_t arcstat_other_size; 377185029Spjd kstat_named_t arcstat_l2_hits; 378185029Spjd kstat_named_t arcstat_l2_misses; 379185029Spjd kstat_named_t arcstat_l2_feeds; 380185029Spjd kstat_named_t arcstat_l2_rw_clash; 381208373Smm kstat_named_t arcstat_l2_read_bytes; 382208373Smm kstat_named_t arcstat_l2_write_bytes; 383185029Spjd kstat_named_t arcstat_l2_writes_sent; 384185029Spjd kstat_named_t arcstat_l2_writes_done; 385185029Spjd kstat_named_t arcstat_l2_writes_error; 386185029Spjd kstat_named_t arcstat_l2_writes_hdr_miss; 387185029Spjd kstat_named_t arcstat_l2_evict_lock_retry; 388185029Spjd kstat_named_t arcstat_l2_evict_reading; 389185029Spjd kstat_named_t arcstat_l2_free_on_write; 390274172Savg kstat_named_t arcstat_l2_cdata_free_on_write; 391185029Spjd kstat_named_t arcstat_l2_abort_lowmem; 392185029Spjd kstat_named_t arcstat_l2_cksum_bad; 393185029Spjd kstat_named_t arcstat_l2_io_error; 394185029Spjd kstat_named_t arcstat_l2_size; 395251478Sdelphij kstat_named_t arcstat_l2_asize; 396185029Spjd kstat_named_t arcstat_l2_hdr_size; 397251478Sdelphij kstat_named_t arcstat_l2_compress_successes; 398251478Sdelphij kstat_named_t arcstat_l2_compress_zeros; 399251478Sdelphij kstat_named_t arcstat_l2_compress_failures; 400205231Skmacy kstat_named_t arcstat_l2_write_trylock_fail; 401205231Skmacy kstat_named_t arcstat_l2_write_passed_headroom; 402205231Skmacy kstat_named_t arcstat_l2_write_spa_mismatch; 403206796Spjd kstat_named_t arcstat_l2_write_in_l2; 404205231Skmacy kstat_named_t arcstat_l2_write_hdr_io_in_progress; 405205231Skmacy kstat_named_t arcstat_l2_write_not_cacheable; 406205231Skmacy kstat_named_t arcstat_l2_write_full; 407205231Skmacy kstat_named_t arcstat_l2_write_buffer_iter; 408205231Skmacy kstat_named_t arcstat_l2_write_pios; 409205231Skmacy kstat_named_t arcstat_l2_write_buffer_bytes_scanned; 410205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_iter; 411205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_null_iter; 412242845Sdelphij kstat_named_t arcstat_memory_throttle_count; 413242845Sdelphij kstat_named_t arcstat_duplicate_buffers; 414242845Sdelphij kstat_named_t arcstat_duplicate_buffers_size; 415242845Sdelphij kstat_named_t arcstat_duplicate_reads; 416168404Spjd} arc_stats_t; 417168404Spjd 418168404Spjdstatic arc_stats_t arc_stats = { 419168404Spjd { "hits", KSTAT_DATA_UINT64 }, 420168404Spjd { "misses", KSTAT_DATA_UINT64 }, 421168404Spjd { "demand_data_hits", KSTAT_DATA_UINT64 }, 422168404Spjd { "demand_data_misses", KSTAT_DATA_UINT64 }, 423168404Spjd { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 424168404Spjd { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 425168404Spjd { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 426168404Spjd { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 427168404Spjd { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 428168404Spjd { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 429168404Spjd { "mru_hits", KSTAT_DATA_UINT64 }, 430168404Spjd { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 431168404Spjd { "mfu_hits", KSTAT_DATA_UINT64 }, 432168404Spjd { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 433205231Skmacy { "allocated", KSTAT_DATA_UINT64 }, 434168404Spjd { "deleted", KSTAT_DATA_UINT64 }, 435205231Skmacy { "stolen", KSTAT_DATA_UINT64 }, 436168404Spjd { "recycle_miss", KSTAT_DATA_UINT64 }, 437168404Spjd { "mutex_miss", KSTAT_DATA_UINT64 }, 438168404Spjd { "evict_skip", KSTAT_DATA_UINT64 }, 439208373Smm { "evict_l2_cached", KSTAT_DATA_UINT64 }, 440208373Smm { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 441208373Smm { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 442168404Spjd { "hash_elements", KSTAT_DATA_UINT64 }, 443168404Spjd { "hash_elements_max", KSTAT_DATA_UINT64 }, 444168404Spjd { "hash_collisions", KSTAT_DATA_UINT64 }, 445168404Spjd { "hash_chains", KSTAT_DATA_UINT64 }, 446168404Spjd { "hash_chain_max", KSTAT_DATA_UINT64 }, 447168404Spjd { "p", KSTAT_DATA_UINT64 }, 448168404Spjd { "c", KSTAT_DATA_UINT64 }, 449168404Spjd { "c_min", KSTAT_DATA_UINT64 }, 450168404Spjd { "c_max", KSTAT_DATA_UINT64 }, 451185029Spjd { "size", KSTAT_DATA_UINT64 }, 452185029Spjd { "hdr_size", KSTAT_DATA_UINT64 }, 453208373Smm { "data_size", KSTAT_DATA_UINT64 }, 454208373Smm { "other_size", KSTAT_DATA_UINT64 }, 455185029Spjd { "l2_hits", KSTAT_DATA_UINT64 }, 456185029Spjd { "l2_misses", KSTAT_DATA_UINT64 }, 457185029Spjd { "l2_feeds", KSTAT_DATA_UINT64 }, 458185029Spjd { "l2_rw_clash", KSTAT_DATA_UINT64 }, 459208373Smm { "l2_read_bytes", KSTAT_DATA_UINT64 }, 460208373Smm { "l2_write_bytes", KSTAT_DATA_UINT64 }, 461185029Spjd { "l2_writes_sent", KSTAT_DATA_UINT64 }, 462185029Spjd { "l2_writes_done", KSTAT_DATA_UINT64 }, 463185029Spjd { "l2_writes_error", KSTAT_DATA_UINT64 }, 464185029Spjd { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 465185029Spjd { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 466185029Spjd { "l2_evict_reading", KSTAT_DATA_UINT64 }, 467185029Spjd { "l2_free_on_write", KSTAT_DATA_UINT64 }, 468274172Savg { "l2_cdata_free_on_write", KSTAT_DATA_UINT64 }, 469185029Spjd { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 470185029Spjd { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 471185029Spjd { "l2_io_error", KSTAT_DATA_UINT64 }, 472185029Spjd { "l2_size", KSTAT_DATA_UINT64 }, 473251478Sdelphij { "l2_asize", KSTAT_DATA_UINT64 }, 474185029Spjd { "l2_hdr_size", KSTAT_DATA_UINT64 }, 475251478Sdelphij { "l2_compress_successes", KSTAT_DATA_UINT64 }, 476251478Sdelphij { "l2_compress_zeros", KSTAT_DATA_UINT64 }, 477251478Sdelphij { "l2_compress_failures", KSTAT_DATA_UINT64 }, 478206796Spjd { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 479206796Spjd { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 480206796Spjd { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 481206796Spjd { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 482206796Spjd { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 483206796Spjd { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 484206796Spjd { "l2_write_full", KSTAT_DATA_UINT64 }, 485206796Spjd { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 486206796Spjd { "l2_write_pios", KSTAT_DATA_UINT64 }, 487206796Spjd { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 488206796Spjd { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 489242845Sdelphij { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 }, 490242845Sdelphij { "memory_throttle_count", KSTAT_DATA_UINT64 }, 491242845Sdelphij { "duplicate_buffers", KSTAT_DATA_UINT64 }, 492242845Sdelphij { "duplicate_buffers_size", KSTAT_DATA_UINT64 }, 493242845Sdelphij { "duplicate_reads", KSTAT_DATA_UINT64 } 494168404Spjd}; 495168404Spjd 496168404Spjd#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 497168404Spjd 498168404Spjd#define ARCSTAT_INCR(stat, val) \ 499251631Sdelphij atomic_add_64(&arc_stats.stat.value.ui64, (val)) 500168404Spjd 501206796Spjd#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 502168404Spjd#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 503168404Spjd 504168404Spjd#define ARCSTAT_MAX(stat, val) { \ 505168404Spjd uint64_t m; \ 506168404Spjd while ((val) > (m = arc_stats.stat.value.ui64) && \ 507168404Spjd (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 508168404Spjd continue; \ 509168404Spjd} 510168404Spjd 511168404Spjd#define ARCSTAT_MAXSTAT(stat) \ 512168404Spjd ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 513168404Spjd 514168404Spjd/* 515168404Spjd * We define a macro to allow ARC hits/misses to be easily broken down by 516168404Spjd * two separate conditions, giving a total of four different subtypes for 517168404Spjd * each of hits and misses (so eight statistics total). 518168404Spjd */ 519168404Spjd#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 520168404Spjd if (cond1) { \ 521168404Spjd if (cond2) { \ 522168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 523168404Spjd } else { \ 524168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 525168404Spjd } \ 526168404Spjd } else { \ 527168404Spjd if (cond2) { \ 528168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 529168404Spjd } else { \ 530168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 531168404Spjd } \ 532168404Spjd } 533168404Spjd 534168404Spjdkstat_t *arc_ksp; 535206796Spjdstatic arc_state_t *arc_anon; 536168404Spjdstatic arc_state_t *arc_mru; 537168404Spjdstatic arc_state_t *arc_mru_ghost; 538168404Spjdstatic arc_state_t *arc_mfu; 539168404Spjdstatic arc_state_t *arc_mfu_ghost; 540185029Spjdstatic arc_state_t *arc_l2c_only; 541168404Spjd 542168404Spjd/* 543168404Spjd * There are several ARC variables that are critical to export as kstats -- 544168404Spjd * but we don't want to have to grovel around in the kstat whenever we wish to 545168404Spjd * manipulate them. For these variables, we therefore define them to be in 546168404Spjd * terms of the statistic variable. This assures that we are not introducing 547168404Spjd * the possibility of inconsistency by having shadow copies of the variables, 548168404Spjd * while still allowing the code to be readable. 549168404Spjd */ 550168404Spjd#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 551168404Spjd#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 552168404Spjd#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 553168404Spjd#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 554168404Spjd#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 555168404Spjd 556251478Sdelphij#define L2ARC_IS_VALID_COMPRESS(_c_) \ 557251478Sdelphij ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY) 558251478Sdelphij 559168404Spjdstatic int arc_no_grow; /* Don't try to grow cache size */ 560168404Spjdstatic uint64_t arc_tempreserve; 561209962Smmstatic uint64_t arc_loaned_bytes; 562185029Spjdstatic uint64_t arc_meta_used; 563185029Spjdstatic uint64_t arc_meta_limit; 564185029Spjdstatic uint64_t arc_meta_max = 0; 565229663SpjdSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_used, CTLFLAG_RD, &arc_meta_used, 0, 566229663Spjd "ARC metadata used"); 567229663SpjdSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_limit, CTLFLAG_RW, &arc_meta_limit, 0, 568229663Spjd "ARC metadata limit"); 569168404Spjd 570185029Spjdtypedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 571185029Spjd 572168404Spjdtypedef struct arc_callback arc_callback_t; 573168404Spjd 574168404Spjdstruct arc_callback { 575168404Spjd void *acb_private; 576168404Spjd arc_done_func_t *acb_done; 577168404Spjd arc_buf_t *acb_buf; 578168404Spjd zio_t *acb_zio_dummy; 579168404Spjd arc_callback_t *acb_next; 580168404Spjd}; 581168404Spjd 582168404Spjdtypedef struct arc_write_callback arc_write_callback_t; 583168404Spjd 584168404Spjdstruct arc_write_callback { 585168404Spjd void *awcb_private; 586168404Spjd arc_done_func_t *awcb_ready; 587258632Savg arc_done_func_t *awcb_physdone; 588168404Spjd arc_done_func_t *awcb_done; 589168404Spjd arc_buf_t *awcb_buf; 590168404Spjd}; 591168404Spjd 592168404Spjdstruct arc_buf_hdr { 593168404Spjd /* protected by hash lock */ 594168404Spjd dva_t b_dva; 595168404Spjd uint64_t b_birth; 596168404Spjd uint64_t b_cksum0; 597168404Spjd 598168404Spjd kmutex_t b_freeze_lock; 599168404Spjd zio_cksum_t *b_freeze_cksum; 600219089Spjd void *b_thawed; 601168404Spjd 602168404Spjd arc_buf_hdr_t *b_hash_next; 603168404Spjd arc_buf_t *b_buf; 604168404Spjd uint32_t b_flags; 605168404Spjd uint32_t b_datacnt; 606168404Spjd 607168404Spjd arc_callback_t *b_acb; 608168404Spjd kcondvar_t b_cv; 609168404Spjd 610168404Spjd /* immutable */ 611168404Spjd arc_buf_contents_t b_type; 612168404Spjd uint64_t b_size; 613209962Smm uint64_t b_spa; 614168404Spjd 615168404Spjd /* protected by arc state mutex */ 616168404Spjd arc_state_t *b_state; 617168404Spjd list_node_t b_arc_node; 618168404Spjd 619168404Spjd /* updated atomically */ 620168404Spjd clock_t b_arc_access; 621168404Spjd 622168404Spjd /* self protecting */ 623168404Spjd refcount_t b_refcnt; 624185029Spjd 625185029Spjd l2arc_buf_hdr_t *b_l2hdr; 626185029Spjd list_node_t b_l2node; 627168404Spjd}; 628168404Spjd 629168404Spjdstatic arc_buf_t *arc_eviction_list; 630168404Spjdstatic kmutex_t arc_eviction_mtx; 631168404Spjdstatic arc_buf_hdr_t arc_eviction_hdr; 632168404Spjdstatic void arc_get_data_buf(arc_buf_t *buf); 633168404Spjdstatic void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 634185029Spjdstatic int arc_evict_needed(arc_buf_contents_t type); 635209962Smmstatic void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes); 636240133Smm#ifdef illumos 637240133Smmstatic void arc_buf_watch(arc_buf_t *buf); 638240133Smm#endif /* illumos */ 639168404Spjd 640209962Smmstatic boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab); 641208373Smm 642168404Spjd#define GHOST_STATE(state) \ 643185029Spjd ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 644185029Spjd (state) == arc_l2c_only) 645168404Spjd 646168404Spjd/* 647168404Spjd * Private ARC flags. These flags are private ARC only flags that will show up 648168404Spjd * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 649168404Spjd * be passed in as arc_flags in things like arc_read. However, these flags 650168404Spjd * should never be passed and should only be set by ARC code. When adding new 651168404Spjd * public flags, make sure not to smash the private ones. 652168404Spjd */ 653168404Spjd 654168404Spjd#define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 655168404Spjd#define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 656168404Spjd#define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 657168404Spjd#define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 658168404Spjd#define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 659168404Spjd#define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 660185029Spjd#define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 661185029Spjd#define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 662185029Spjd#define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 663185029Spjd#define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 664168404Spjd 665168404Spjd#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 666168404Spjd#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 667168404Spjd#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 668208373Smm#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH) 669168404Spjd#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 670168404Spjd#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 671185029Spjd#define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 672185029Spjd#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 673185029Spjd#define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 674185029Spjd (hdr)->b_l2hdr != NULL) 675185029Spjd#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 676185029Spjd#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 677185029Spjd#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 678168404Spjd 679168404Spjd/* 680185029Spjd * Other sizes 681185029Spjd */ 682185029Spjd 683185029Spjd#define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 684185029Spjd#define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 685185029Spjd 686185029Spjd/* 687168404Spjd * Hash table routines 688168404Spjd */ 689168404Spjd 690205253Skmacy#define HT_LOCK_PAD CACHE_LINE_SIZE 691168404Spjd 692168404Spjdstruct ht_lock { 693168404Spjd kmutex_t ht_lock; 694168404Spjd#ifdef _KERNEL 695168404Spjd unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 696168404Spjd#endif 697168404Spjd}; 698168404Spjd 699168404Spjd#define BUF_LOCKS 256 700168404Spjdtypedef struct buf_hash_table { 701168404Spjd uint64_t ht_mask; 702168404Spjd arc_buf_hdr_t **ht_table; 703205264Skmacy struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 704168404Spjd} buf_hash_table_t; 705168404Spjd 706168404Spjdstatic buf_hash_table_t buf_hash_table; 707168404Spjd 708168404Spjd#define BUF_HASH_INDEX(spa, dva, birth) \ 709168404Spjd (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 710168404Spjd#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 711168404Spjd#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 712219089Spjd#define HDR_LOCK(hdr) \ 713219089Spjd (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 714168404Spjd 715168404Spjduint64_t zfs_crc64_table[256]; 716168404Spjd 717185029Spjd/* 718185029Spjd * Level 2 ARC 719185029Spjd */ 720185029Spjd 721272707Savg#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 722251478Sdelphij#define L2ARC_HEADROOM 2 /* num of writes */ 723251478Sdelphij/* 724251478Sdelphij * If we discover during ARC scan any buffers to be compressed, we boost 725251478Sdelphij * our headroom for the next scanning cycle by this percentage multiple. 726251478Sdelphij */ 727251478Sdelphij#define L2ARC_HEADROOM_BOOST 200 728208373Smm#define L2ARC_FEED_SECS 1 /* caching interval secs */ 729208373Smm#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 730185029Spjd 731185029Spjd#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 732185029Spjd#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 733185029Spjd 734251631Sdelphij/* L2ARC Performance Tunables */ 735185029Spjduint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 736185029Spjduint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 737185029Spjduint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 738251478Sdelphijuint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 739185029Spjduint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 740208373Smmuint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 741219089Spjdboolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 742208373Smmboolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 743208373Smmboolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 744185029Spjd 745217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 746205231Skmacy &l2arc_write_max, 0, "max write size"); 747217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 748205231Skmacy &l2arc_write_boost, 0, "extra write during warmup"); 749217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 750205231Skmacy &l2arc_headroom, 0, "number of dev writes"); 751217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 752205231Skmacy &l2arc_feed_secs, 0, "interval seconds"); 753217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 754208373Smm &l2arc_feed_min_ms, 0, "min interval milliseconds"); 755205231Skmacy 756205231SkmacySYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 757205231Skmacy &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 758208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 759208373Smm &l2arc_feed_again, 0, "turbo warmup"); 760208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 761208373Smm &l2arc_norw, 0, "no reads during writes"); 762205231Skmacy 763217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 764205231Skmacy &ARC_anon.arcs_size, 0, "size of anonymous state"); 765217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD, 766205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state"); 767217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD, 768205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state"); 769205231Skmacy 770217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 771205231Skmacy &ARC_mru.arcs_size, 0, "size of mru state"); 772217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD, 773205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state"); 774217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD, 775205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state"); 776205231Skmacy 777217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 778205231Skmacy &ARC_mru_ghost.arcs_size, 0, "size of mru ghost state"); 779217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD, 780205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 781205231Skmacy "size of metadata in mru ghost state"); 782217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD, 783205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 784205231Skmacy "size of data in mru ghost state"); 785205231Skmacy 786217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 787205231Skmacy &ARC_mfu.arcs_size, 0, "size of mfu state"); 788217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD, 789205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state"); 790217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD, 791205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state"); 792205231Skmacy 793217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 794205231Skmacy &ARC_mfu_ghost.arcs_size, 0, "size of mfu ghost state"); 795217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD, 796205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 797205231Skmacy "size of metadata in mfu ghost state"); 798217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD, 799205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 800205231Skmacy "size of data in mfu ghost state"); 801205231Skmacy 802217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 803205231Skmacy &ARC_l2c_only.arcs_size, 0, "size of mru state"); 804205231Skmacy 805185029Spjd/* 806185029Spjd * L2ARC Internals 807185029Spjd */ 808185029Spjdtypedef struct l2arc_dev { 809185029Spjd vdev_t *l2ad_vdev; /* vdev */ 810185029Spjd spa_t *l2ad_spa; /* spa */ 811185029Spjd uint64_t l2ad_hand; /* next write location */ 812185029Spjd uint64_t l2ad_start; /* first addr on device */ 813185029Spjd uint64_t l2ad_end; /* last addr on device */ 814185029Spjd uint64_t l2ad_evict; /* last addr eviction reached */ 815185029Spjd boolean_t l2ad_first; /* first sweep through */ 816208373Smm boolean_t l2ad_writing; /* currently writing */ 817185029Spjd list_t *l2ad_buflist; /* buffer list */ 818185029Spjd list_node_t l2ad_node; /* device list node */ 819185029Spjd} l2arc_dev_t; 820185029Spjd 821185029Spjdstatic list_t L2ARC_dev_list; /* device list */ 822185029Spjdstatic list_t *l2arc_dev_list; /* device list pointer */ 823185029Spjdstatic kmutex_t l2arc_dev_mtx; /* device list mutex */ 824185029Spjdstatic l2arc_dev_t *l2arc_dev_last; /* last device used */ 825185029Spjdstatic kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 826185029Spjdstatic list_t L2ARC_free_on_write; /* free after write buf list */ 827185029Spjdstatic list_t *l2arc_free_on_write; /* free after write list ptr */ 828185029Spjdstatic kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 829185029Spjdstatic uint64_t l2arc_ndev; /* number of devices */ 830185029Spjd 831185029Spjdtypedef struct l2arc_read_callback { 832251478Sdelphij arc_buf_t *l2rcb_buf; /* read buffer */ 833251478Sdelphij spa_t *l2rcb_spa; /* spa */ 834251478Sdelphij blkptr_t l2rcb_bp; /* original blkptr */ 835268123Sdelphij zbookmark_phys_t l2rcb_zb; /* original bookmark */ 836251478Sdelphij int l2rcb_flags; /* original flags */ 837251478Sdelphij enum zio_compress l2rcb_compress; /* applied compress */ 838185029Spjd} l2arc_read_callback_t; 839185029Spjd 840185029Spjdtypedef struct l2arc_write_callback { 841185029Spjd l2arc_dev_t *l2wcb_dev; /* device info */ 842185029Spjd arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 843185029Spjd} l2arc_write_callback_t; 844185029Spjd 845185029Spjdstruct l2arc_buf_hdr { 846185029Spjd /* protected by arc_buf_hdr mutex */ 847251478Sdelphij l2arc_dev_t *b_dev; /* L2ARC device */ 848251478Sdelphij uint64_t b_daddr; /* disk address, offset byte */ 849251478Sdelphij /* compression applied to buffer data */ 850251478Sdelphij enum zio_compress b_compress; 851251478Sdelphij /* real alloc'd buffer size depending on b_compress applied */ 852251478Sdelphij int b_asize; 853251478Sdelphij /* temporary buffer holder for in-flight compressed data */ 854251478Sdelphij void *b_tmp_cdata; 855185029Spjd}; 856185029Spjd 857185029Spjdtypedef struct l2arc_data_free { 858185029Spjd /* protected by l2arc_free_on_write_mtx */ 859185029Spjd void *l2df_data; 860185029Spjd size_t l2df_size; 861185029Spjd void (*l2df_func)(void *, size_t); 862185029Spjd list_node_t l2df_list_node; 863185029Spjd} l2arc_data_free_t; 864185029Spjd 865185029Spjdstatic kmutex_t l2arc_feed_thr_lock; 866185029Spjdstatic kcondvar_t l2arc_feed_thr_cv; 867185029Spjdstatic uint8_t l2arc_thread_exit; 868185029Spjd 869185029Spjdstatic void l2arc_read_done(zio_t *zio); 870185029Spjdstatic void l2arc_hdr_stat_add(void); 871185029Spjdstatic void l2arc_hdr_stat_remove(void); 872185029Spjd 873251478Sdelphijstatic boolean_t l2arc_compress_buf(l2arc_buf_hdr_t *l2hdr); 874251478Sdelphijstatic void l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, 875251478Sdelphij enum zio_compress c); 876251478Sdelphijstatic void l2arc_release_cdata_buf(arc_buf_hdr_t *ab); 877251478Sdelphij 878168404Spjdstatic uint64_t 879209962Smmbuf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 880168404Spjd{ 881168404Spjd uint8_t *vdva = (uint8_t *)dva; 882168404Spjd uint64_t crc = -1ULL; 883168404Spjd int i; 884168404Spjd 885168404Spjd ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 886168404Spjd 887168404Spjd for (i = 0; i < sizeof (dva_t); i++) 888168404Spjd crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 889168404Spjd 890209962Smm crc ^= (spa>>8) ^ birth; 891168404Spjd 892168404Spjd return (crc); 893168404Spjd} 894168404Spjd 895168404Spjd#define BUF_EMPTY(buf) \ 896168404Spjd ((buf)->b_dva.dva_word[0] == 0 && \ 897168404Spjd (buf)->b_dva.dva_word[1] == 0 && \ 898260150Sdelphij (buf)->b_cksum0 == 0) 899168404Spjd 900168404Spjd#define BUF_EQUAL(spa, dva, birth, buf) \ 901168404Spjd ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 902168404Spjd ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 903168404Spjd ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 904168404Spjd 905219089Spjdstatic void 906219089Spjdbuf_discard_identity(arc_buf_hdr_t *hdr) 907219089Spjd{ 908219089Spjd hdr->b_dva.dva_word[0] = 0; 909219089Spjd hdr->b_dva.dva_word[1] = 0; 910219089Spjd hdr->b_birth = 0; 911219089Spjd hdr->b_cksum0 = 0; 912219089Spjd} 913219089Spjd 914168404Spjdstatic arc_buf_hdr_t * 915268075Sdelphijbuf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 916168404Spjd{ 917268075Sdelphij const dva_t *dva = BP_IDENTITY(bp); 918268075Sdelphij uint64_t birth = BP_PHYSICAL_BIRTH(bp); 919168404Spjd uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 920168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 921168404Spjd arc_buf_hdr_t *buf; 922168404Spjd 923168404Spjd mutex_enter(hash_lock); 924168404Spjd for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 925168404Spjd buf = buf->b_hash_next) { 926168404Spjd if (BUF_EQUAL(spa, dva, birth, buf)) { 927168404Spjd *lockp = hash_lock; 928168404Spjd return (buf); 929168404Spjd } 930168404Spjd } 931168404Spjd mutex_exit(hash_lock); 932168404Spjd *lockp = NULL; 933168404Spjd return (NULL); 934168404Spjd} 935168404Spjd 936168404Spjd/* 937168404Spjd * Insert an entry into the hash table. If there is already an element 938168404Spjd * equal to elem in the hash table, then the already existing element 939168404Spjd * will be returned and the new element will not be inserted. 940168404Spjd * Otherwise returns NULL. 941168404Spjd */ 942168404Spjdstatic arc_buf_hdr_t * 943168404Spjdbuf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 944168404Spjd{ 945168404Spjd uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 946168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 947168404Spjd arc_buf_hdr_t *fbuf; 948168404Spjd uint32_t i; 949168404Spjd 950268075Sdelphij ASSERT(!DVA_IS_EMPTY(&buf->b_dva)); 951268075Sdelphij ASSERT(buf->b_birth != 0); 952168404Spjd ASSERT(!HDR_IN_HASH_TABLE(buf)); 953168404Spjd *lockp = hash_lock; 954168404Spjd mutex_enter(hash_lock); 955168404Spjd for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 956168404Spjd fbuf = fbuf->b_hash_next, i++) { 957168404Spjd if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 958168404Spjd return (fbuf); 959168404Spjd } 960168404Spjd 961168404Spjd buf->b_hash_next = buf_hash_table.ht_table[idx]; 962168404Spjd buf_hash_table.ht_table[idx] = buf; 963168404Spjd buf->b_flags |= ARC_IN_HASH_TABLE; 964168404Spjd 965168404Spjd /* collect some hash table performance data */ 966168404Spjd if (i > 0) { 967168404Spjd ARCSTAT_BUMP(arcstat_hash_collisions); 968168404Spjd if (i == 1) 969168404Spjd ARCSTAT_BUMP(arcstat_hash_chains); 970168404Spjd 971168404Spjd ARCSTAT_MAX(arcstat_hash_chain_max, i); 972168404Spjd } 973168404Spjd 974168404Spjd ARCSTAT_BUMP(arcstat_hash_elements); 975168404Spjd ARCSTAT_MAXSTAT(arcstat_hash_elements); 976168404Spjd 977168404Spjd return (NULL); 978168404Spjd} 979168404Spjd 980168404Spjdstatic void 981168404Spjdbuf_hash_remove(arc_buf_hdr_t *buf) 982168404Spjd{ 983168404Spjd arc_buf_hdr_t *fbuf, **bufp; 984168404Spjd uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 985168404Spjd 986168404Spjd ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 987168404Spjd ASSERT(HDR_IN_HASH_TABLE(buf)); 988168404Spjd 989168404Spjd bufp = &buf_hash_table.ht_table[idx]; 990168404Spjd while ((fbuf = *bufp) != buf) { 991168404Spjd ASSERT(fbuf != NULL); 992168404Spjd bufp = &fbuf->b_hash_next; 993168404Spjd } 994168404Spjd *bufp = buf->b_hash_next; 995168404Spjd buf->b_hash_next = NULL; 996168404Spjd buf->b_flags &= ~ARC_IN_HASH_TABLE; 997168404Spjd 998168404Spjd /* collect some hash table performance data */ 999168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_elements); 1000168404Spjd 1001168404Spjd if (buf_hash_table.ht_table[idx] && 1002168404Spjd buf_hash_table.ht_table[idx]->b_hash_next == NULL) 1003168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1004168404Spjd} 1005168404Spjd 1006168404Spjd/* 1007168404Spjd * Global data structures and functions for the buf kmem cache. 1008168404Spjd */ 1009168404Spjdstatic kmem_cache_t *hdr_cache; 1010168404Spjdstatic kmem_cache_t *buf_cache; 1011168404Spjd 1012168404Spjdstatic void 1013168404Spjdbuf_fini(void) 1014168404Spjd{ 1015168404Spjd int i; 1016168404Spjd 1017168404Spjd kmem_free(buf_hash_table.ht_table, 1018168404Spjd (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1019168404Spjd for (i = 0; i < BUF_LOCKS; i++) 1020168404Spjd mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 1021168404Spjd kmem_cache_destroy(hdr_cache); 1022168404Spjd kmem_cache_destroy(buf_cache); 1023168404Spjd} 1024168404Spjd 1025168404Spjd/* 1026168404Spjd * Constructor callback - called when the cache is empty 1027168404Spjd * and a new buf is requested. 1028168404Spjd */ 1029168404Spjd/* ARGSUSED */ 1030168404Spjdstatic int 1031168404Spjdhdr_cons(void *vbuf, void *unused, int kmflag) 1032168404Spjd{ 1033168404Spjd arc_buf_hdr_t *buf = vbuf; 1034168404Spjd 1035168404Spjd bzero(buf, sizeof (arc_buf_hdr_t)); 1036168404Spjd refcount_create(&buf->b_refcnt); 1037168404Spjd cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 1038185029Spjd mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1039208373Smm arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 1040185029Spjd 1041168404Spjd return (0); 1042168404Spjd} 1043168404Spjd 1044185029Spjd/* ARGSUSED */ 1045185029Spjdstatic int 1046185029Spjdbuf_cons(void *vbuf, void *unused, int kmflag) 1047185029Spjd{ 1048185029Spjd arc_buf_t *buf = vbuf; 1049185029Spjd 1050185029Spjd bzero(buf, sizeof (arc_buf_t)); 1051219089Spjd mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 1052208373Smm arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1053208373Smm 1054185029Spjd return (0); 1055185029Spjd} 1056185029Spjd 1057168404Spjd/* 1058168404Spjd * Destructor callback - called when a cached buf is 1059168404Spjd * no longer required. 1060168404Spjd */ 1061168404Spjd/* ARGSUSED */ 1062168404Spjdstatic void 1063168404Spjdhdr_dest(void *vbuf, void *unused) 1064168404Spjd{ 1065168404Spjd arc_buf_hdr_t *buf = vbuf; 1066168404Spjd 1067219089Spjd ASSERT(BUF_EMPTY(buf)); 1068168404Spjd refcount_destroy(&buf->b_refcnt); 1069168404Spjd cv_destroy(&buf->b_cv); 1070185029Spjd mutex_destroy(&buf->b_freeze_lock); 1071208373Smm arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 1072168404Spjd} 1073168404Spjd 1074185029Spjd/* ARGSUSED */ 1075185029Spjdstatic void 1076185029Spjdbuf_dest(void *vbuf, void *unused) 1077185029Spjd{ 1078185029Spjd arc_buf_t *buf = vbuf; 1079185029Spjd 1080219089Spjd mutex_destroy(&buf->b_evict_lock); 1081208373Smm arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1082185029Spjd} 1083185029Spjd 1084168404Spjd/* 1085168404Spjd * Reclaim callback -- invoked when memory is low. 1086168404Spjd */ 1087168404Spjd/* ARGSUSED */ 1088168404Spjdstatic void 1089168404Spjdhdr_recl(void *unused) 1090168404Spjd{ 1091168404Spjd dprintf("hdr_recl called\n"); 1092168404Spjd /* 1093168404Spjd * umem calls the reclaim func when we destroy the buf cache, 1094168404Spjd * which is after we do arc_fini(). 1095168404Spjd */ 1096168404Spjd if (!arc_dead) 1097168404Spjd cv_signal(&arc_reclaim_thr_cv); 1098168404Spjd} 1099168404Spjd 1100168404Spjdstatic void 1101168404Spjdbuf_init(void) 1102168404Spjd{ 1103168404Spjd uint64_t *ct; 1104168404Spjd uint64_t hsize = 1ULL << 12; 1105168404Spjd int i, j; 1106168404Spjd 1107168404Spjd /* 1108168404Spjd * The hash table is big enough to fill all of physical memory 1109269230Sdelphij * with an average block size of zfs_arc_average_blocksize (default 8K). 1110269230Sdelphij * By default, the table will take up 1111269230Sdelphij * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1112168404Spjd */ 1113269230Sdelphij while (hsize * zfs_arc_average_blocksize < (uint64_t)physmem * PAGESIZE) 1114168404Spjd hsize <<= 1; 1115168404Spjdretry: 1116168404Spjd buf_hash_table.ht_mask = hsize - 1; 1117168404Spjd buf_hash_table.ht_table = 1118168404Spjd kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1119168404Spjd if (buf_hash_table.ht_table == NULL) { 1120168404Spjd ASSERT(hsize > (1ULL << 8)); 1121168404Spjd hsize >>= 1; 1122168404Spjd goto retry; 1123168404Spjd } 1124168404Spjd 1125168404Spjd hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 1126168404Spjd 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 1127168404Spjd buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1128185029Spjd 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1129168404Spjd 1130168404Spjd for (i = 0; i < 256; i++) 1131168404Spjd for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1132168404Spjd *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1133168404Spjd 1134168404Spjd for (i = 0; i < BUF_LOCKS; i++) { 1135168404Spjd mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1136168404Spjd NULL, MUTEX_DEFAULT, NULL); 1137168404Spjd } 1138168404Spjd} 1139168404Spjd 1140168404Spjd#define ARC_MINTIME (hz>>4) /* 62 ms */ 1141168404Spjd 1142168404Spjdstatic void 1143168404Spjdarc_cksum_verify(arc_buf_t *buf) 1144168404Spjd{ 1145168404Spjd zio_cksum_t zc; 1146168404Spjd 1147168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1148168404Spjd return; 1149168404Spjd 1150168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1151168404Spjd if (buf->b_hdr->b_freeze_cksum == NULL || 1152168404Spjd (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 1153168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1154168404Spjd return; 1155168404Spjd } 1156168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1157168404Spjd if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 1158168404Spjd panic("buffer modified while frozen!"); 1159168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1160168404Spjd} 1161168404Spjd 1162185029Spjdstatic int 1163185029Spjdarc_cksum_equal(arc_buf_t *buf) 1164185029Spjd{ 1165185029Spjd zio_cksum_t zc; 1166185029Spjd int equal; 1167185029Spjd 1168185029Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1169185029Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1170185029Spjd equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 1171185029Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1172185029Spjd 1173185029Spjd return (equal); 1174185029Spjd} 1175185029Spjd 1176168404Spjdstatic void 1177185029Spjdarc_cksum_compute(arc_buf_t *buf, boolean_t force) 1178168404Spjd{ 1179185029Spjd if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 1180168404Spjd return; 1181168404Spjd 1182168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1183168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1184168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1185168404Spjd return; 1186168404Spjd } 1187168404Spjd buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 1188168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 1189168404Spjd buf->b_hdr->b_freeze_cksum); 1190168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1191240133Smm#ifdef illumos 1192240133Smm arc_buf_watch(buf); 1193240133Smm#endif /* illumos */ 1194168404Spjd} 1195168404Spjd 1196240133Smm#ifdef illumos 1197240133Smm#ifndef _KERNEL 1198240133Smmtypedef struct procctl { 1199240133Smm long cmd; 1200240133Smm prwatch_t prwatch; 1201240133Smm} procctl_t; 1202240133Smm#endif 1203240133Smm 1204240133Smm/* ARGSUSED */ 1205240133Smmstatic void 1206240133Smmarc_buf_unwatch(arc_buf_t *buf) 1207240133Smm{ 1208240133Smm#ifndef _KERNEL 1209240133Smm if (arc_watch) { 1210240133Smm int result; 1211240133Smm procctl_t ctl; 1212240133Smm ctl.cmd = PCWATCH; 1213240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1214240133Smm ctl.prwatch.pr_size = 0; 1215240133Smm ctl.prwatch.pr_wflags = 0; 1216240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1217240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1218240133Smm } 1219240133Smm#endif 1220240133Smm} 1221240133Smm 1222240133Smm/* ARGSUSED */ 1223240133Smmstatic void 1224240133Smmarc_buf_watch(arc_buf_t *buf) 1225240133Smm{ 1226240133Smm#ifndef _KERNEL 1227240133Smm if (arc_watch) { 1228240133Smm int result; 1229240133Smm procctl_t ctl; 1230240133Smm ctl.cmd = PCWATCH; 1231240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1232240133Smm ctl.prwatch.pr_size = buf->b_hdr->b_size; 1233240133Smm ctl.prwatch.pr_wflags = WA_WRITE; 1234240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1235240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1236240133Smm } 1237240133Smm#endif 1238240133Smm} 1239240133Smm#endif /* illumos */ 1240240133Smm 1241168404Spjdvoid 1242168404Spjdarc_buf_thaw(arc_buf_t *buf) 1243168404Spjd{ 1244185029Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1245185029Spjd if (buf->b_hdr->b_state != arc_anon) 1246185029Spjd panic("modifying non-anon buffer!"); 1247185029Spjd if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 1248185029Spjd panic("modifying buffer while i/o in progress!"); 1249185029Spjd arc_cksum_verify(buf); 1250185029Spjd } 1251168404Spjd 1252168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1253168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1254168404Spjd kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1255168404Spjd buf->b_hdr->b_freeze_cksum = NULL; 1256168404Spjd } 1257219089Spjd 1258219089Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1259219089Spjd if (buf->b_hdr->b_thawed) 1260219089Spjd kmem_free(buf->b_hdr->b_thawed, 1); 1261219089Spjd buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP); 1262219089Spjd } 1263219089Spjd 1264168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1265240133Smm 1266240133Smm#ifdef illumos 1267240133Smm arc_buf_unwatch(buf); 1268240133Smm#endif /* illumos */ 1269168404Spjd} 1270168404Spjd 1271168404Spjdvoid 1272168404Spjdarc_buf_freeze(arc_buf_t *buf) 1273168404Spjd{ 1274219089Spjd kmutex_t *hash_lock; 1275219089Spjd 1276168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1277168404Spjd return; 1278168404Spjd 1279219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1280219089Spjd mutex_enter(hash_lock); 1281219089Spjd 1282168404Spjd ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 1283168404Spjd buf->b_hdr->b_state == arc_anon); 1284185029Spjd arc_cksum_compute(buf, B_FALSE); 1285219089Spjd mutex_exit(hash_lock); 1286240133Smm 1287168404Spjd} 1288168404Spjd 1289168404Spjdstatic void 1290205231Skmacyget_buf_info(arc_buf_hdr_t *ab, arc_state_t *state, list_t **list, kmutex_t **lock) 1291205231Skmacy{ 1292205231Skmacy uint64_t buf_hashid = buf_hash(ab->b_spa, &ab->b_dva, ab->b_birth); 1293205231Skmacy 1294206796Spjd if (ab->b_type == ARC_BUFC_METADATA) 1295206796Spjd buf_hashid &= (ARC_BUFC_NUMMETADATALISTS - 1); 1296205231Skmacy else { 1297206796Spjd buf_hashid &= (ARC_BUFC_NUMDATALISTS - 1); 1298205231Skmacy buf_hashid += ARC_BUFC_NUMMETADATALISTS; 1299205231Skmacy } 1300205231Skmacy 1301205231Skmacy *list = &state->arcs_lists[buf_hashid]; 1302205231Skmacy *lock = ARCS_LOCK(state, buf_hashid); 1303205231Skmacy} 1304205231Skmacy 1305205231Skmacy 1306205231Skmacystatic void 1307168404Spjdadd_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1308168404Spjd{ 1309168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1310168404Spjd 1311168404Spjd if ((refcount_add(&ab->b_refcnt, tag) == 1) && 1312168404Spjd (ab->b_state != arc_anon)) { 1313206796Spjd uint64_t delta = ab->b_size * ab->b_datacnt; 1314206796Spjd uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 1315205231Skmacy list_t *list; 1316205231Skmacy kmutex_t *lock; 1317168404Spjd 1318205231Skmacy get_buf_info(ab, ab->b_state, &list, &lock); 1319205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1320205231Skmacy mutex_enter(lock); 1321168404Spjd ASSERT(list_link_active(&ab->b_arc_node)); 1322185029Spjd list_remove(list, ab); 1323168404Spjd if (GHOST_STATE(ab->b_state)) { 1324240415Smm ASSERT0(ab->b_datacnt); 1325168404Spjd ASSERT3P(ab->b_buf, ==, NULL); 1326168404Spjd delta = ab->b_size; 1327168404Spjd } 1328168404Spjd ASSERT(delta > 0); 1329185029Spjd ASSERT3U(*size, >=, delta); 1330185029Spjd atomic_add_64(size, -delta); 1331206794Spjd mutex_exit(lock); 1332185029Spjd /* remove the prefetch flag if we get a reference */ 1333168404Spjd if (ab->b_flags & ARC_PREFETCH) 1334168404Spjd ab->b_flags &= ~ARC_PREFETCH; 1335168404Spjd } 1336168404Spjd} 1337168404Spjd 1338168404Spjdstatic int 1339168404Spjdremove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1340168404Spjd{ 1341168404Spjd int cnt; 1342168404Spjd arc_state_t *state = ab->b_state; 1343168404Spjd 1344168404Spjd ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1345168404Spjd ASSERT(!GHOST_STATE(state)); 1346168404Spjd 1347168404Spjd if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 1348168404Spjd (state != arc_anon)) { 1349185029Spjd uint64_t *size = &state->arcs_lsize[ab->b_type]; 1350205231Skmacy list_t *list; 1351205231Skmacy kmutex_t *lock; 1352185029Spjd 1353205231Skmacy get_buf_info(ab, state, &list, &lock); 1354205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1355205231Skmacy mutex_enter(lock); 1356168404Spjd ASSERT(!list_link_active(&ab->b_arc_node)); 1357205231Skmacy list_insert_head(list, ab); 1358168404Spjd ASSERT(ab->b_datacnt > 0); 1359185029Spjd atomic_add_64(size, ab->b_size * ab->b_datacnt); 1360206794Spjd mutex_exit(lock); 1361168404Spjd } 1362168404Spjd return (cnt); 1363168404Spjd} 1364168404Spjd 1365168404Spjd/* 1366168404Spjd * Move the supplied buffer to the indicated state. The mutex 1367168404Spjd * for the buffer must be held by the caller. 1368168404Spjd */ 1369168404Spjdstatic void 1370168404Spjdarc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1371168404Spjd{ 1372168404Spjd arc_state_t *old_state = ab->b_state; 1373168404Spjd int64_t refcnt = refcount_count(&ab->b_refcnt); 1374168404Spjd uint64_t from_delta, to_delta; 1375205231Skmacy list_t *list; 1376205231Skmacy kmutex_t *lock; 1377168404Spjd 1378168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1379258632Savg ASSERT3P(new_state, !=, old_state); 1380168404Spjd ASSERT(refcnt == 0 || ab->b_datacnt > 0); 1381168404Spjd ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 1382219089Spjd ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon); 1383168404Spjd 1384168404Spjd from_delta = to_delta = ab->b_datacnt * ab->b_size; 1385168404Spjd 1386168404Spjd /* 1387168404Spjd * If this buffer is evictable, transfer it from the 1388168404Spjd * old state list to the new state list. 1389168404Spjd */ 1390168404Spjd if (refcnt == 0) { 1391168404Spjd if (old_state != arc_anon) { 1392205231Skmacy int use_mutex; 1393185029Spjd uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 1394168404Spjd 1395205231Skmacy get_buf_info(ab, old_state, &list, &lock); 1396205231Skmacy use_mutex = !MUTEX_HELD(lock); 1397168404Spjd if (use_mutex) 1398205231Skmacy mutex_enter(lock); 1399168404Spjd 1400168404Spjd ASSERT(list_link_active(&ab->b_arc_node)); 1401205231Skmacy list_remove(list, ab); 1402168404Spjd 1403168404Spjd /* 1404168404Spjd * If prefetching out of the ghost cache, 1405219089Spjd * we will have a non-zero datacnt. 1406168404Spjd */ 1407168404Spjd if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 1408168404Spjd /* ghost elements have a ghost size */ 1409168404Spjd ASSERT(ab->b_buf == NULL); 1410168404Spjd from_delta = ab->b_size; 1411168404Spjd } 1412185029Spjd ASSERT3U(*size, >=, from_delta); 1413185029Spjd atomic_add_64(size, -from_delta); 1414168404Spjd 1415168404Spjd if (use_mutex) 1416205231Skmacy mutex_exit(lock); 1417168404Spjd } 1418168404Spjd if (new_state != arc_anon) { 1419206796Spjd int use_mutex; 1420185029Spjd uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1421168404Spjd 1422205231Skmacy get_buf_info(ab, new_state, &list, &lock); 1423205231Skmacy use_mutex = !MUTEX_HELD(lock); 1424168404Spjd if (use_mutex) 1425205231Skmacy mutex_enter(lock); 1426168404Spjd 1427205231Skmacy list_insert_head(list, ab); 1428168404Spjd 1429168404Spjd /* ghost elements have a ghost size */ 1430168404Spjd if (GHOST_STATE(new_state)) { 1431168404Spjd ASSERT(ab->b_datacnt == 0); 1432168404Spjd ASSERT(ab->b_buf == NULL); 1433168404Spjd to_delta = ab->b_size; 1434168404Spjd } 1435185029Spjd atomic_add_64(size, to_delta); 1436168404Spjd 1437168404Spjd if (use_mutex) 1438205231Skmacy mutex_exit(lock); 1439168404Spjd } 1440168404Spjd } 1441168404Spjd 1442168404Spjd ASSERT(!BUF_EMPTY(ab)); 1443219089Spjd if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab)) 1444168404Spjd buf_hash_remove(ab); 1445168404Spjd 1446168404Spjd /* adjust state sizes */ 1447168404Spjd if (to_delta) 1448168404Spjd atomic_add_64(&new_state->arcs_size, to_delta); 1449168404Spjd if (from_delta) { 1450168404Spjd ASSERT3U(old_state->arcs_size, >=, from_delta); 1451168404Spjd atomic_add_64(&old_state->arcs_size, -from_delta); 1452168404Spjd } 1453168404Spjd ab->b_state = new_state; 1454185029Spjd 1455185029Spjd /* adjust l2arc hdr stats */ 1456185029Spjd if (new_state == arc_l2c_only) 1457185029Spjd l2arc_hdr_stat_add(); 1458185029Spjd else if (old_state == arc_l2c_only) 1459185029Spjd l2arc_hdr_stat_remove(); 1460168404Spjd} 1461168404Spjd 1462185029Spjdvoid 1463208373Smmarc_space_consume(uint64_t space, arc_space_type_t type) 1464185029Spjd{ 1465208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1466208373Smm 1467208373Smm switch (type) { 1468208373Smm case ARC_SPACE_DATA: 1469208373Smm ARCSTAT_INCR(arcstat_data_size, space); 1470208373Smm break; 1471208373Smm case ARC_SPACE_OTHER: 1472208373Smm ARCSTAT_INCR(arcstat_other_size, space); 1473208373Smm break; 1474208373Smm case ARC_SPACE_HDRS: 1475208373Smm ARCSTAT_INCR(arcstat_hdr_size, space); 1476208373Smm break; 1477208373Smm case ARC_SPACE_L2HDRS: 1478208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, space); 1479208373Smm break; 1480208373Smm } 1481208373Smm 1482185029Spjd atomic_add_64(&arc_meta_used, space); 1483185029Spjd atomic_add_64(&arc_size, space); 1484185029Spjd} 1485185029Spjd 1486185029Spjdvoid 1487208373Smmarc_space_return(uint64_t space, arc_space_type_t type) 1488185029Spjd{ 1489208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1490208373Smm 1491208373Smm switch (type) { 1492208373Smm case ARC_SPACE_DATA: 1493208373Smm ARCSTAT_INCR(arcstat_data_size, -space); 1494208373Smm break; 1495208373Smm case ARC_SPACE_OTHER: 1496208373Smm ARCSTAT_INCR(arcstat_other_size, -space); 1497208373Smm break; 1498208373Smm case ARC_SPACE_HDRS: 1499208373Smm ARCSTAT_INCR(arcstat_hdr_size, -space); 1500208373Smm break; 1501208373Smm case ARC_SPACE_L2HDRS: 1502208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 1503208373Smm break; 1504208373Smm } 1505208373Smm 1506185029Spjd ASSERT(arc_meta_used >= space); 1507185029Spjd if (arc_meta_max < arc_meta_used) 1508185029Spjd arc_meta_max = arc_meta_used; 1509185029Spjd atomic_add_64(&arc_meta_used, -space); 1510185029Spjd ASSERT(arc_size >= space); 1511185029Spjd atomic_add_64(&arc_size, -space); 1512185029Spjd} 1513185029Spjd 1514168404Spjdarc_buf_t * 1515168404Spjdarc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1516168404Spjd{ 1517168404Spjd arc_buf_hdr_t *hdr; 1518168404Spjd arc_buf_t *buf; 1519168404Spjd 1520168404Spjd ASSERT3U(size, >, 0); 1521185029Spjd hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1522168404Spjd ASSERT(BUF_EMPTY(hdr)); 1523168404Spjd hdr->b_size = size; 1524168404Spjd hdr->b_type = type; 1525228103Smm hdr->b_spa = spa_load_guid(spa); 1526168404Spjd hdr->b_state = arc_anon; 1527168404Spjd hdr->b_arc_access = 0; 1528185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1529168404Spjd buf->b_hdr = hdr; 1530168404Spjd buf->b_data = NULL; 1531168404Spjd buf->b_efunc = NULL; 1532168404Spjd buf->b_private = NULL; 1533168404Spjd buf->b_next = NULL; 1534168404Spjd hdr->b_buf = buf; 1535168404Spjd arc_get_data_buf(buf); 1536168404Spjd hdr->b_datacnt = 1; 1537168404Spjd hdr->b_flags = 0; 1538168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1539168404Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1540168404Spjd 1541168404Spjd return (buf); 1542168404Spjd} 1543168404Spjd 1544209962Smmstatic char *arc_onloan_tag = "onloan"; 1545209962Smm 1546209962Smm/* 1547209962Smm * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 1548209962Smm * flight data by arc_tempreserve_space() until they are "returned". Loaned 1549209962Smm * buffers must be returned to the arc before they can be used by the DMU or 1550209962Smm * freed. 1551209962Smm */ 1552209962Smmarc_buf_t * 1553209962Smmarc_loan_buf(spa_t *spa, int size) 1554209962Smm{ 1555209962Smm arc_buf_t *buf; 1556209962Smm 1557209962Smm buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 1558209962Smm 1559209962Smm atomic_add_64(&arc_loaned_bytes, size); 1560209962Smm return (buf); 1561209962Smm} 1562209962Smm 1563209962Smm/* 1564209962Smm * Return a loaned arc buffer to the arc. 1565209962Smm */ 1566209962Smmvoid 1567209962Smmarc_return_buf(arc_buf_t *buf, void *tag) 1568209962Smm{ 1569209962Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1570209962Smm 1571209962Smm ASSERT(buf->b_data != NULL); 1572219089Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1573219089Spjd (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag); 1574209962Smm 1575209962Smm atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 1576209962Smm} 1577209962Smm 1578219089Spjd/* Detach an arc_buf from a dbuf (tag) */ 1579219089Spjdvoid 1580219089Spjdarc_loan_inuse_buf(arc_buf_t *buf, void *tag) 1581219089Spjd{ 1582219089Spjd arc_buf_hdr_t *hdr; 1583219089Spjd 1584219089Spjd ASSERT(buf->b_data != NULL); 1585219089Spjd hdr = buf->b_hdr; 1586219089Spjd (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag); 1587219089Spjd (void) refcount_remove(&hdr->b_refcnt, tag); 1588219089Spjd buf->b_efunc = NULL; 1589219089Spjd buf->b_private = NULL; 1590219089Spjd 1591219089Spjd atomic_add_64(&arc_loaned_bytes, hdr->b_size); 1592219089Spjd} 1593219089Spjd 1594168404Spjdstatic arc_buf_t * 1595168404Spjdarc_buf_clone(arc_buf_t *from) 1596168404Spjd{ 1597168404Spjd arc_buf_t *buf; 1598168404Spjd arc_buf_hdr_t *hdr = from->b_hdr; 1599168404Spjd uint64_t size = hdr->b_size; 1600168404Spjd 1601219089Spjd ASSERT(hdr->b_state != arc_anon); 1602219089Spjd 1603185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1604168404Spjd buf->b_hdr = hdr; 1605168404Spjd buf->b_data = NULL; 1606168404Spjd buf->b_efunc = NULL; 1607168404Spjd buf->b_private = NULL; 1608168404Spjd buf->b_next = hdr->b_buf; 1609168404Spjd hdr->b_buf = buf; 1610168404Spjd arc_get_data_buf(buf); 1611168404Spjd bcopy(from->b_data, buf->b_data, size); 1612242845Sdelphij 1613242845Sdelphij /* 1614242845Sdelphij * This buffer already exists in the arc so create a duplicate 1615242845Sdelphij * copy for the caller. If the buffer is associated with user data 1616242845Sdelphij * then track the size and number of duplicates. These stats will be 1617242845Sdelphij * updated as duplicate buffers are created and destroyed. 1618242845Sdelphij */ 1619242845Sdelphij if (hdr->b_type == ARC_BUFC_DATA) { 1620242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_buffers); 1621242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, size); 1622242845Sdelphij } 1623168404Spjd hdr->b_datacnt += 1; 1624168404Spjd return (buf); 1625168404Spjd} 1626168404Spjd 1627168404Spjdvoid 1628168404Spjdarc_buf_add_ref(arc_buf_t *buf, void* tag) 1629168404Spjd{ 1630168404Spjd arc_buf_hdr_t *hdr; 1631168404Spjd kmutex_t *hash_lock; 1632168404Spjd 1633168404Spjd /* 1634185029Spjd * Check to see if this buffer is evicted. Callers 1635185029Spjd * must verify b_data != NULL to know if the add_ref 1636185029Spjd * was successful. 1637168404Spjd */ 1638219089Spjd mutex_enter(&buf->b_evict_lock); 1639185029Spjd if (buf->b_data == NULL) { 1640219089Spjd mutex_exit(&buf->b_evict_lock); 1641168404Spjd return; 1642168404Spjd } 1643219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1644219089Spjd mutex_enter(hash_lock); 1645185029Spjd hdr = buf->b_hdr; 1646219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1647219089Spjd mutex_exit(&buf->b_evict_lock); 1648168404Spjd 1649168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 1650168404Spjd add_reference(hdr, hash_lock, tag); 1651208373Smm DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 1652168404Spjd arc_access(hdr, hash_lock); 1653168404Spjd mutex_exit(hash_lock); 1654168404Spjd ARCSTAT_BUMP(arcstat_hits); 1655168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 1656168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 1657168404Spjd data, metadata, hits); 1658168404Spjd} 1659168404Spjd 1660274172Savgstatic void 1661274172Savgarc_buf_free_on_write(void *data, size_t size, 1662274172Savg void (*free_func)(void *, size_t)) 1663274172Savg{ 1664274172Savg l2arc_data_free_t *df; 1665274172Savg 1666274172Savg df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 1667274172Savg df->l2df_data = data; 1668274172Savg df->l2df_size = size; 1669274172Savg df->l2df_func = free_func; 1670274172Savg mutex_enter(&l2arc_free_on_write_mtx); 1671274172Savg list_insert_head(l2arc_free_on_write, df); 1672274172Savg mutex_exit(&l2arc_free_on_write_mtx); 1673274172Savg} 1674274172Savg 1675185029Spjd/* 1676185029Spjd * Free the arc data buffer. If it is an l2arc write in progress, 1677185029Spjd * the buffer is placed on l2arc_free_on_write to be freed later. 1678185029Spjd */ 1679168404Spjdstatic void 1680240133Smmarc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t)) 1681185029Spjd{ 1682240133Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1683240133Smm 1684185029Spjd if (HDR_L2_WRITING(hdr)) { 1685274172Savg arc_buf_free_on_write(buf->b_data, hdr->b_size, free_func); 1686185029Spjd ARCSTAT_BUMP(arcstat_l2_free_on_write); 1687185029Spjd } else { 1688240133Smm free_func(buf->b_data, hdr->b_size); 1689185029Spjd } 1690185029Spjd} 1691185029Spjd 1692268858Sdelphij/* 1693268858Sdelphij * Free up buf->b_data and if 'remove' is set, then pull the 1694268858Sdelphij * arc_buf_t off of the the arc_buf_hdr_t's list and free it. 1695268858Sdelphij */ 1696185029Spjdstatic void 1697274172Savgarc_buf_l2_cdata_free(arc_buf_hdr_t *hdr) 1698274172Savg{ 1699274172Savg l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 1700274172Savg 1701274172Savg ASSERT(MUTEX_HELD(&l2arc_buflist_mtx)); 1702274172Savg 1703274172Savg if (l2hdr->b_tmp_cdata == NULL) 1704274172Savg return; 1705274172Savg 1706274172Savg ASSERT(HDR_L2_WRITING(hdr)); 1707274172Savg arc_buf_free_on_write(l2hdr->b_tmp_cdata, hdr->b_size, 1708274172Savg zio_data_buf_free); 1709274172Savg ARCSTAT_BUMP(arcstat_l2_cdata_free_on_write); 1710274172Savg l2hdr->b_tmp_cdata = NULL; 1711274172Savg} 1712274172Savg 1713274172Savgstatic void 1714268858Sdelphijarc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t remove) 1715168404Spjd{ 1716168404Spjd arc_buf_t **bufp; 1717168404Spjd 1718168404Spjd /* free up data associated with the buf */ 1719168404Spjd if (buf->b_data) { 1720168404Spjd arc_state_t *state = buf->b_hdr->b_state; 1721168404Spjd uint64_t size = buf->b_hdr->b_size; 1722168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 1723168404Spjd 1724168404Spjd arc_cksum_verify(buf); 1725240133Smm#ifdef illumos 1726240133Smm arc_buf_unwatch(buf); 1727240133Smm#endif /* illumos */ 1728219089Spjd 1729168404Spjd if (!recycle) { 1730168404Spjd if (type == ARC_BUFC_METADATA) { 1731240133Smm arc_buf_data_free(buf, zio_buf_free); 1732208373Smm arc_space_return(size, ARC_SPACE_DATA); 1733168404Spjd } else { 1734168404Spjd ASSERT(type == ARC_BUFC_DATA); 1735240133Smm arc_buf_data_free(buf, zio_data_buf_free); 1736208373Smm ARCSTAT_INCR(arcstat_data_size, -size); 1737185029Spjd atomic_add_64(&arc_size, -size); 1738168404Spjd } 1739168404Spjd } 1740168404Spjd if (list_link_active(&buf->b_hdr->b_arc_node)) { 1741185029Spjd uint64_t *cnt = &state->arcs_lsize[type]; 1742185029Spjd 1743168404Spjd ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 1744168404Spjd ASSERT(state != arc_anon); 1745185029Spjd 1746185029Spjd ASSERT3U(*cnt, >=, size); 1747185029Spjd atomic_add_64(cnt, -size); 1748168404Spjd } 1749168404Spjd ASSERT3U(state->arcs_size, >=, size); 1750168404Spjd atomic_add_64(&state->arcs_size, -size); 1751168404Spjd buf->b_data = NULL; 1752242845Sdelphij 1753242845Sdelphij /* 1754242845Sdelphij * If we're destroying a duplicate buffer make sure 1755242845Sdelphij * that the appropriate statistics are updated. 1756242845Sdelphij */ 1757242845Sdelphij if (buf->b_hdr->b_datacnt > 1 && 1758242845Sdelphij buf->b_hdr->b_type == ARC_BUFC_DATA) { 1759242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 1760242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size); 1761242845Sdelphij } 1762168404Spjd ASSERT(buf->b_hdr->b_datacnt > 0); 1763168404Spjd buf->b_hdr->b_datacnt -= 1; 1764168404Spjd } 1765168404Spjd 1766168404Spjd /* only remove the buf if requested */ 1767268858Sdelphij if (!remove) 1768168404Spjd return; 1769168404Spjd 1770168404Spjd /* remove the buf from the hdr list */ 1771168404Spjd for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 1772168404Spjd continue; 1773168404Spjd *bufp = buf->b_next; 1774219089Spjd buf->b_next = NULL; 1775168404Spjd 1776168404Spjd ASSERT(buf->b_efunc == NULL); 1777168404Spjd 1778168404Spjd /* clean up the buf */ 1779168404Spjd buf->b_hdr = NULL; 1780168404Spjd kmem_cache_free(buf_cache, buf); 1781168404Spjd} 1782168404Spjd 1783168404Spjdstatic void 1784168404Spjdarc_hdr_destroy(arc_buf_hdr_t *hdr) 1785168404Spjd{ 1786168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1787168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 1788168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1789219089Spjd l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 1790168404Spjd 1791219089Spjd if (l2hdr != NULL) { 1792219089Spjd boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx); 1793219089Spjd /* 1794219089Spjd * To prevent arc_free() and l2arc_evict() from 1795219089Spjd * attempting to free the same buffer at the same time, 1796219089Spjd * a FREE_IN_PROGRESS flag is given to arc_free() to 1797219089Spjd * give it priority. l2arc_evict() can't destroy this 1798219089Spjd * header while we are waiting on l2arc_buflist_mtx. 1799219089Spjd * 1800219089Spjd * The hdr may be removed from l2ad_buflist before we 1801219089Spjd * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 1802219089Spjd */ 1803219089Spjd if (!buflist_held) { 1804185029Spjd mutex_enter(&l2arc_buflist_mtx); 1805219089Spjd l2hdr = hdr->b_l2hdr; 1806219089Spjd } 1807219089Spjd 1808219089Spjd if (l2hdr != NULL) { 1809248572Ssmh trim_map_free(l2hdr->b_dev->l2ad_vdev, l2hdr->b_daddr, 1810248574Ssmh hdr->b_size, 0); 1811219089Spjd list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 1812274172Savg arc_buf_l2_cdata_free(hdr); 1813219089Spjd ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 1814251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); 1815268085Sdelphij vdev_space_update(l2hdr->b_dev->l2ad_vdev, 1816268085Sdelphij -l2hdr->b_asize, 0, 0); 1817219089Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 1818219089Spjd if (hdr->b_state == arc_l2c_only) 1819219089Spjd l2arc_hdr_stat_remove(); 1820219089Spjd hdr->b_l2hdr = NULL; 1821219089Spjd } 1822219089Spjd 1823219089Spjd if (!buflist_held) 1824185029Spjd mutex_exit(&l2arc_buflist_mtx); 1825185029Spjd } 1826185029Spjd 1827168404Spjd if (!BUF_EMPTY(hdr)) { 1828168404Spjd ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1829219089Spjd buf_discard_identity(hdr); 1830168404Spjd } 1831168404Spjd while (hdr->b_buf) { 1832168404Spjd arc_buf_t *buf = hdr->b_buf; 1833168404Spjd 1834168404Spjd if (buf->b_efunc) { 1835168404Spjd mutex_enter(&arc_eviction_mtx); 1836219089Spjd mutex_enter(&buf->b_evict_lock); 1837168404Spjd ASSERT(buf->b_hdr != NULL); 1838168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 1839168404Spjd hdr->b_buf = buf->b_next; 1840168404Spjd buf->b_hdr = &arc_eviction_hdr; 1841168404Spjd buf->b_next = arc_eviction_list; 1842168404Spjd arc_eviction_list = buf; 1843219089Spjd mutex_exit(&buf->b_evict_lock); 1844168404Spjd mutex_exit(&arc_eviction_mtx); 1845168404Spjd } else { 1846168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 1847168404Spjd } 1848168404Spjd } 1849168404Spjd if (hdr->b_freeze_cksum != NULL) { 1850168404Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1851168404Spjd hdr->b_freeze_cksum = NULL; 1852168404Spjd } 1853219089Spjd if (hdr->b_thawed) { 1854219089Spjd kmem_free(hdr->b_thawed, 1); 1855219089Spjd hdr->b_thawed = NULL; 1856219089Spjd } 1857168404Spjd 1858168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 1859168404Spjd ASSERT3P(hdr->b_hash_next, ==, NULL); 1860168404Spjd ASSERT3P(hdr->b_acb, ==, NULL); 1861168404Spjd kmem_cache_free(hdr_cache, hdr); 1862168404Spjd} 1863168404Spjd 1864168404Spjdvoid 1865168404Spjdarc_buf_free(arc_buf_t *buf, void *tag) 1866168404Spjd{ 1867168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1868168404Spjd int hashed = hdr->b_state != arc_anon; 1869168404Spjd 1870168404Spjd ASSERT(buf->b_efunc == NULL); 1871168404Spjd ASSERT(buf->b_data != NULL); 1872168404Spjd 1873168404Spjd if (hashed) { 1874168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1875168404Spjd 1876168404Spjd mutex_enter(hash_lock); 1877219089Spjd hdr = buf->b_hdr; 1878219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1879219089Spjd 1880168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1881219089Spjd if (hdr->b_datacnt > 1) { 1882168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1883219089Spjd } else { 1884219089Spjd ASSERT(buf == hdr->b_buf); 1885219089Spjd ASSERT(buf->b_efunc == NULL); 1886168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 1887219089Spjd } 1888168404Spjd mutex_exit(hash_lock); 1889168404Spjd } else if (HDR_IO_IN_PROGRESS(hdr)) { 1890168404Spjd int destroy_hdr; 1891168404Spjd /* 1892168404Spjd * We are in the middle of an async write. Don't destroy 1893168404Spjd * this buffer unless the write completes before we finish 1894168404Spjd * decrementing the reference count. 1895168404Spjd */ 1896168404Spjd mutex_enter(&arc_eviction_mtx); 1897168404Spjd (void) remove_reference(hdr, NULL, tag); 1898168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1899168404Spjd destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 1900168404Spjd mutex_exit(&arc_eviction_mtx); 1901168404Spjd if (destroy_hdr) 1902168404Spjd arc_hdr_destroy(hdr); 1903168404Spjd } else { 1904219089Spjd if (remove_reference(hdr, NULL, tag) > 0) 1905168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1906219089Spjd else 1907168404Spjd arc_hdr_destroy(hdr); 1908168404Spjd } 1909168404Spjd} 1910168404Spjd 1911248571Smmboolean_t 1912168404Spjdarc_buf_remove_ref(arc_buf_t *buf, void* tag) 1913168404Spjd{ 1914168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1915168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1916248571Smm boolean_t no_callback = (buf->b_efunc == NULL); 1917168404Spjd 1918168404Spjd if (hdr->b_state == arc_anon) { 1919219089Spjd ASSERT(hdr->b_datacnt == 1); 1920168404Spjd arc_buf_free(buf, tag); 1921168404Spjd return (no_callback); 1922168404Spjd } 1923168404Spjd 1924168404Spjd mutex_enter(hash_lock); 1925219089Spjd hdr = buf->b_hdr; 1926219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1927168404Spjd ASSERT(hdr->b_state != arc_anon); 1928168404Spjd ASSERT(buf->b_data != NULL); 1929168404Spjd 1930168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1931168404Spjd if (hdr->b_datacnt > 1) { 1932168404Spjd if (no_callback) 1933168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1934168404Spjd } else if (no_callback) { 1935168404Spjd ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 1936219089Spjd ASSERT(buf->b_efunc == NULL); 1937168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 1938168404Spjd } 1939168404Spjd ASSERT(no_callback || hdr->b_datacnt > 1 || 1940168404Spjd refcount_is_zero(&hdr->b_refcnt)); 1941168404Spjd mutex_exit(hash_lock); 1942168404Spjd return (no_callback); 1943168404Spjd} 1944168404Spjd 1945168404Spjdint 1946168404Spjdarc_buf_size(arc_buf_t *buf) 1947168404Spjd{ 1948168404Spjd return (buf->b_hdr->b_size); 1949168404Spjd} 1950168404Spjd 1951168404Spjd/* 1952242845Sdelphij * Called from the DMU to determine if the current buffer should be 1953242845Sdelphij * evicted. In order to ensure proper locking, the eviction must be initiated 1954242845Sdelphij * from the DMU. Return true if the buffer is associated with user data and 1955242845Sdelphij * duplicate buffers still exist. 1956242845Sdelphij */ 1957242845Sdelphijboolean_t 1958242845Sdelphijarc_buf_eviction_needed(arc_buf_t *buf) 1959242845Sdelphij{ 1960242845Sdelphij arc_buf_hdr_t *hdr; 1961242845Sdelphij boolean_t evict_needed = B_FALSE; 1962242845Sdelphij 1963242845Sdelphij if (zfs_disable_dup_eviction) 1964242845Sdelphij return (B_FALSE); 1965242845Sdelphij 1966242845Sdelphij mutex_enter(&buf->b_evict_lock); 1967242845Sdelphij hdr = buf->b_hdr; 1968242845Sdelphij if (hdr == NULL) { 1969242845Sdelphij /* 1970242845Sdelphij * We are in arc_do_user_evicts(); let that function 1971242845Sdelphij * perform the eviction. 1972242845Sdelphij */ 1973242845Sdelphij ASSERT(buf->b_data == NULL); 1974242845Sdelphij mutex_exit(&buf->b_evict_lock); 1975242845Sdelphij return (B_FALSE); 1976242845Sdelphij } else if (buf->b_data == NULL) { 1977242845Sdelphij /* 1978242845Sdelphij * We have already been added to the arc eviction list; 1979242845Sdelphij * recommend eviction. 1980242845Sdelphij */ 1981242845Sdelphij ASSERT3P(hdr, ==, &arc_eviction_hdr); 1982242845Sdelphij mutex_exit(&buf->b_evict_lock); 1983242845Sdelphij return (B_TRUE); 1984242845Sdelphij } 1985242845Sdelphij 1986242845Sdelphij if (hdr->b_datacnt > 1 && hdr->b_type == ARC_BUFC_DATA) 1987242845Sdelphij evict_needed = B_TRUE; 1988242845Sdelphij 1989242845Sdelphij mutex_exit(&buf->b_evict_lock); 1990242845Sdelphij return (evict_needed); 1991242845Sdelphij} 1992242845Sdelphij 1993242845Sdelphij/* 1994168404Spjd * Evict buffers from list until we've removed the specified number of 1995168404Spjd * bytes. Move the removed buffers to the appropriate evict state. 1996168404Spjd * If the recycle flag is set, then attempt to "recycle" a buffer: 1997168404Spjd * - look for a buffer to evict that is `bytes' long. 1998168404Spjd * - return the data block from this buffer rather than freeing it. 1999168404Spjd * This flag is used by callers that are trying to make space for a 2000168404Spjd * new buffer in a full arc cache. 2001185029Spjd * 2002185029Spjd * This function makes a "best effort". It skips over any buffers 2003185029Spjd * it can't get a hash_lock on, and so may not catch all candidates. 2004185029Spjd * It may also return without evicting as much space as requested. 2005168404Spjd */ 2006168404Spjdstatic void * 2007209962Smmarc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 2008168404Spjd arc_buf_contents_t type) 2009168404Spjd{ 2010168404Spjd arc_state_t *evicted_state; 2011168404Spjd uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 2012205231Skmacy int64_t bytes_remaining; 2013168404Spjd arc_buf_hdr_t *ab, *ab_prev = NULL; 2014205231Skmacy list_t *evicted_list, *list, *evicted_list_start, *list_start; 2015205231Skmacy kmutex_t *lock, *evicted_lock; 2016168404Spjd kmutex_t *hash_lock; 2017168404Spjd boolean_t have_lock; 2018168404Spjd void *stolen = NULL; 2019258632Savg arc_buf_hdr_t marker = { 0 }; 2020258632Savg int count = 0; 2021205231Skmacy static int evict_metadata_offset, evict_data_offset; 2022258632Savg int i, idx, offset, list_count, lists; 2023168404Spjd 2024168404Spjd ASSERT(state == arc_mru || state == arc_mfu); 2025168404Spjd 2026168404Spjd evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 2027206796Spjd 2028205231Skmacy if (type == ARC_BUFC_METADATA) { 2029205231Skmacy offset = 0; 2030205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 2031205231Skmacy list_start = &state->arcs_lists[0]; 2032205231Skmacy evicted_list_start = &evicted_state->arcs_lists[0]; 2033205231Skmacy idx = evict_metadata_offset; 2034205231Skmacy } else { 2035205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 2036205231Skmacy list_start = &state->arcs_lists[offset]; 2037205231Skmacy evicted_list_start = &evicted_state->arcs_lists[offset]; 2038205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 2039205231Skmacy idx = evict_data_offset; 2040205231Skmacy } 2041205231Skmacy bytes_remaining = evicted_state->arcs_lsize[type]; 2042258632Savg lists = 0; 2043206796Spjd 2044205231Skmacyevict_start: 2045205231Skmacy list = &list_start[idx]; 2046205231Skmacy evicted_list = &evicted_list_start[idx]; 2047205231Skmacy lock = ARCS_LOCK(state, (offset + idx)); 2048206796Spjd evicted_lock = ARCS_LOCK(evicted_state, (offset + idx)); 2049168404Spjd 2050205231Skmacy mutex_enter(lock); 2051205231Skmacy mutex_enter(evicted_lock); 2052205231Skmacy 2053185029Spjd for (ab = list_tail(list); ab; ab = ab_prev) { 2054185029Spjd ab_prev = list_prev(list, ab); 2055205231Skmacy bytes_remaining -= (ab->b_size * ab->b_datacnt); 2056168404Spjd /* prefetch buffers have a minimum lifespan */ 2057168404Spjd if (HDR_IO_IN_PROGRESS(ab) || 2058185029Spjd (spa && ab->b_spa != spa) || 2059168404Spjd (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 2060219089Spjd ddi_get_lbolt() - ab->b_arc_access < 2061219089Spjd arc_min_prefetch_lifespan)) { 2062168404Spjd skipped++; 2063168404Spjd continue; 2064168404Spjd } 2065168404Spjd /* "lookahead" for better eviction candidate */ 2066168404Spjd if (recycle && ab->b_size != bytes && 2067168404Spjd ab_prev && ab_prev->b_size == bytes) 2068168404Spjd continue; 2069258632Savg 2070258632Savg /* ignore markers */ 2071258632Savg if (ab->b_spa == 0) 2072258632Savg continue; 2073258632Savg 2074258632Savg /* 2075258632Savg * It may take a long time to evict all the bufs requested. 2076258632Savg * To avoid blocking all arc activity, periodically drop 2077258632Savg * the arcs_mtx and give other threads a chance to run 2078258632Savg * before reacquiring the lock. 2079258632Savg * 2080258632Savg * If we are looking for a buffer to recycle, we are in 2081258632Savg * the hot code path, so don't sleep. 2082258632Savg */ 2083258632Savg if (!recycle && count++ > arc_evict_iterations) { 2084258632Savg list_insert_after(list, ab, &marker); 2085258632Savg mutex_exit(evicted_lock); 2086258632Savg mutex_exit(lock); 2087258632Savg kpreempt(KPREEMPT_SYNC); 2088258632Savg mutex_enter(lock); 2089258632Savg mutex_enter(evicted_lock); 2090258632Savg ab_prev = list_prev(list, &marker); 2091258632Savg list_remove(list, &marker); 2092258632Savg count = 0; 2093258632Savg continue; 2094258632Savg } 2095258632Savg 2096168404Spjd hash_lock = HDR_LOCK(ab); 2097168404Spjd have_lock = MUTEX_HELD(hash_lock); 2098168404Spjd if (have_lock || mutex_tryenter(hash_lock)) { 2099240415Smm ASSERT0(refcount_count(&ab->b_refcnt)); 2100168404Spjd ASSERT(ab->b_datacnt > 0); 2101168404Spjd while (ab->b_buf) { 2102168404Spjd arc_buf_t *buf = ab->b_buf; 2103219089Spjd if (!mutex_tryenter(&buf->b_evict_lock)) { 2104185029Spjd missed += 1; 2105185029Spjd break; 2106185029Spjd } 2107168404Spjd if (buf->b_data) { 2108168404Spjd bytes_evicted += ab->b_size; 2109168404Spjd if (recycle && ab->b_type == type && 2110185029Spjd ab->b_size == bytes && 2111185029Spjd !HDR_L2_WRITING(ab)) { 2112168404Spjd stolen = buf->b_data; 2113168404Spjd recycle = FALSE; 2114168404Spjd } 2115168404Spjd } 2116168404Spjd if (buf->b_efunc) { 2117168404Spjd mutex_enter(&arc_eviction_mtx); 2118168404Spjd arc_buf_destroy(buf, 2119168404Spjd buf->b_data == stolen, FALSE); 2120168404Spjd ab->b_buf = buf->b_next; 2121168404Spjd buf->b_hdr = &arc_eviction_hdr; 2122168404Spjd buf->b_next = arc_eviction_list; 2123168404Spjd arc_eviction_list = buf; 2124168404Spjd mutex_exit(&arc_eviction_mtx); 2125219089Spjd mutex_exit(&buf->b_evict_lock); 2126168404Spjd } else { 2127219089Spjd mutex_exit(&buf->b_evict_lock); 2128168404Spjd arc_buf_destroy(buf, 2129168404Spjd buf->b_data == stolen, TRUE); 2130168404Spjd } 2131168404Spjd } 2132208373Smm 2133208373Smm if (ab->b_l2hdr) { 2134208373Smm ARCSTAT_INCR(arcstat_evict_l2_cached, 2135208373Smm ab->b_size); 2136208373Smm } else { 2137208373Smm if (l2arc_write_eligible(ab->b_spa, ab)) { 2138208373Smm ARCSTAT_INCR(arcstat_evict_l2_eligible, 2139208373Smm ab->b_size); 2140208373Smm } else { 2141208373Smm ARCSTAT_INCR( 2142208373Smm arcstat_evict_l2_ineligible, 2143208373Smm ab->b_size); 2144208373Smm } 2145208373Smm } 2146208373Smm 2147185029Spjd if (ab->b_datacnt == 0) { 2148185029Spjd arc_change_state(evicted_state, ab, hash_lock); 2149185029Spjd ASSERT(HDR_IN_HASH_TABLE(ab)); 2150185029Spjd ab->b_flags |= ARC_IN_HASH_TABLE; 2151185029Spjd ab->b_flags &= ~ARC_BUF_AVAILABLE; 2152185029Spjd DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 2153185029Spjd } 2154168404Spjd if (!have_lock) 2155168404Spjd mutex_exit(hash_lock); 2156168404Spjd if (bytes >= 0 && bytes_evicted >= bytes) 2157168404Spjd break; 2158205231Skmacy if (bytes_remaining > 0) { 2159205231Skmacy mutex_exit(evicted_lock); 2160205231Skmacy mutex_exit(lock); 2161206796Spjd idx = ((idx + 1) & (list_count - 1)); 2162258632Savg lists++; 2163205231Skmacy goto evict_start; 2164205231Skmacy } 2165168404Spjd } else { 2166168404Spjd missed += 1; 2167168404Spjd } 2168168404Spjd } 2169168404Spjd 2170205231Skmacy mutex_exit(evicted_lock); 2171205231Skmacy mutex_exit(lock); 2172206796Spjd 2173206796Spjd idx = ((idx + 1) & (list_count - 1)); 2174258632Savg lists++; 2175168404Spjd 2176205231Skmacy if (bytes_evicted < bytes) { 2177258632Savg if (lists < list_count) 2178205231Skmacy goto evict_start; 2179205231Skmacy else 2180205231Skmacy dprintf("only evicted %lld bytes from %x", 2181205231Skmacy (longlong_t)bytes_evicted, state); 2182205231Skmacy } 2183206796Spjd if (type == ARC_BUFC_METADATA) 2184205231Skmacy evict_metadata_offset = idx; 2185205231Skmacy else 2186205231Skmacy evict_data_offset = idx; 2187206796Spjd 2188168404Spjd if (skipped) 2189168404Spjd ARCSTAT_INCR(arcstat_evict_skip, skipped); 2190168404Spjd 2191168404Spjd if (missed) 2192168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, missed); 2193168404Spjd 2194185029Spjd /* 2195258632Savg * Note: we have just evicted some data into the ghost state, 2196258632Savg * potentially putting the ghost size over the desired size. Rather 2197258632Savg * that evicting from the ghost list in this hot code path, leave 2198258632Savg * this chore to the arc_reclaim_thread(). 2199185029Spjd */ 2200185029Spjd 2201205231Skmacy if (stolen) 2202205231Skmacy ARCSTAT_BUMP(arcstat_stolen); 2203168404Spjd return (stolen); 2204168404Spjd} 2205168404Spjd 2206168404Spjd/* 2207168404Spjd * Remove buffers from list until we've removed the specified number of 2208168404Spjd * bytes. Destroy the buffers that are removed. 2209168404Spjd */ 2210168404Spjdstatic void 2211209962Smmarc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 2212168404Spjd{ 2213168404Spjd arc_buf_hdr_t *ab, *ab_prev; 2214219089Spjd arc_buf_hdr_t marker = { 0 }; 2215205231Skmacy list_t *list, *list_start; 2216205231Skmacy kmutex_t *hash_lock, *lock; 2217168404Spjd uint64_t bytes_deleted = 0; 2218168404Spjd uint64_t bufs_skipped = 0; 2219258632Savg int count = 0; 2220205231Skmacy static int evict_offset; 2221205231Skmacy int list_count, idx = evict_offset; 2222258632Savg int offset, lists = 0; 2223168404Spjd 2224168404Spjd ASSERT(GHOST_STATE(state)); 2225205231Skmacy 2226205231Skmacy /* 2227205231Skmacy * data lists come after metadata lists 2228205231Skmacy */ 2229205231Skmacy list_start = &state->arcs_lists[ARC_BUFC_NUMMETADATALISTS]; 2230205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 2231205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 2232206796Spjd 2233205231Skmacyevict_start: 2234205231Skmacy list = &list_start[idx]; 2235205231Skmacy lock = ARCS_LOCK(state, idx + offset); 2236205231Skmacy 2237205231Skmacy mutex_enter(lock); 2238185029Spjd for (ab = list_tail(list); ab; ab = ab_prev) { 2239185029Spjd ab_prev = list_prev(list, ab); 2240258632Savg if (ab->b_type > ARC_BUFC_NUMTYPES) 2241258632Savg panic("invalid ab=%p", (void *)ab); 2242185029Spjd if (spa && ab->b_spa != spa) 2243185029Spjd continue; 2244219089Spjd 2245219089Spjd /* ignore markers */ 2246219089Spjd if (ab->b_spa == 0) 2247219089Spjd continue; 2248219089Spjd 2249168404Spjd hash_lock = HDR_LOCK(ab); 2250219089Spjd /* caller may be trying to modify this buffer, skip it */ 2251219089Spjd if (MUTEX_HELD(hash_lock)) 2252219089Spjd continue; 2253258632Savg 2254258632Savg /* 2255258632Savg * It may take a long time to evict all the bufs requested. 2256258632Savg * To avoid blocking all arc activity, periodically drop 2257258632Savg * the arcs_mtx and give other threads a chance to run 2258258632Savg * before reacquiring the lock. 2259258632Savg */ 2260258632Savg if (count++ > arc_evict_iterations) { 2261258632Savg list_insert_after(list, ab, &marker); 2262258632Savg mutex_exit(lock); 2263258632Savg kpreempt(KPREEMPT_SYNC); 2264258632Savg mutex_enter(lock); 2265258632Savg ab_prev = list_prev(list, &marker); 2266258632Savg list_remove(list, &marker); 2267258632Savg count = 0; 2268258632Savg continue; 2269258632Savg } 2270168404Spjd if (mutex_tryenter(hash_lock)) { 2271168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(ab)); 2272168404Spjd ASSERT(ab->b_buf == NULL); 2273168404Spjd ARCSTAT_BUMP(arcstat_deleted); 2274168404Spjd bytes_deleted += ab->b_size; 2275185029Spjd 2276185029Spjd if (ab->b_l2hdr != NULL) { 2277185029Spjd /* 2278185029Spjd * This buffer is cached on the 2nd Level ARC; 2279185029Spjd * don't destroy the header. 2280185029Spjd */ 2281185029Spjd arc_change_state(arc_l2c_only, ab, hash_lock); 2282185029Spjd mutex_exit(hash_lock); 2283185029Spjd } else { 2284185029Spjd arc_change_state(arc_anon, ab, hash_lock); 2285185029Spjd mutex_exit(hash_lock); 2286185029Spjd arc_hdr_destroy(ab); 2287185029Spjd } 2288185029Spjd 2289168404Spjd DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 2290168404Spjd if (bytes >= 0 && bytes_deleted >= bytes) 2291168404Spjd break; 2292219089Spjd } else if (bytes < 0) { 2293219089Spjd /* 2294219089Spjd * Insert a list marker and then wait for the 2295219089Spjd * hash lock to become available. Once its 2296219089Spjd * available, restart from where we left off. 2297219089Spjd */ 2298219089Spjd list_insert_after(list, ab, &marker); 2299219089Spjd mutex_exit(lock); 2300219089Spjd mutex_enter(hash_lock); 2301219089Spjd mutex_exit(hash_lock); 2302219089Spjd mutex_enter(lock); 2303219089Spjd ab_prev = list_prev(list, &marker); 2304219089Spjd list_remove(list, &marker); 2305258632Savg } else { 2306168404Spjd bufs_skipped += 1; 2307258632Savg } 2308258632Savg 2309168404Spjd } 2310205231Skmacy mutex_exit(lock); 2311206796Spjd idx = ((idx + 1) & (ARC_BUFC_NUMDATALISTS - 1)); 2312258632Savg lists++; 2313206796Spjd 2314258632Savg if (lists < list_count) 2315205231Skmacy goto evict_start; 2316206796Spjd 2317205231Skmacy evict_offset = idx; 2318205231Skmacy if ((uintptr_t)list > (uintptr_t)&state->arcs_lists[ARC_BUFC_NUMMETADATALISTS] && 2319185029Spjd (bytes < 0 || bytes_deleted < bytes)) { 2320205231Skmacy list_start = &state->arcs_lists[0]; 2321205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 2322258632Savg offset = lists = 0; 2323205231Skmacy goto evict_start; 2324185029Spjd } 2325185029Spjd 2326168404Spjd if (bufs_skipped) { 2327168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 2328168404Spjd ASSERT(bytes >= 0); 2329168404Spjd } 2330168404Spjd 2331168404Spjd if (bytes_deleted < bytes) 2332168404Spjd dprintf("only deleted %lld bytes from %p", 2333168404Spjd (longlong_t)bytes_deleted, state); 2334168404Spjd} 2335168404Spjd 2336168404Spjdstatic void 2337168404Spjdarc_adjust(void) 2338168404Spjd{ 2339208373Smm int64_t adjustment, delta; 2340168404Spjd 2341208373Smm /* 2342208373Smm * Adjust MRU size 2343208373Smm */ 2344168404Spjd 2345209275Smm adjustment = MIN((int64_t)(arc_size - arc_c), 2346209275Smm (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - 2347209275Smm arc_p)); 2348208373Smm 2349208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 2350208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 2351209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA); 2352208373Smm adjustment -= delta; 2353168404Spjd } 2354168404Spjd 2355208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2356208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 2357209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, 2358185029Spjd ARC_BUFC_METADATA); 2359185029Spjd } 2360185029Spjd 2361208373Smm /* 2362208373Smm * Adjust MFU size 2363208373Smm */ 2364168404Spjd 2365208373Smm adjustment = arc_size - arc_c; 2366208373Smm 2367208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 2368208373Smm delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 2369209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA); 2370208373Smm adjustment -= delta; 2371168404Spjd } 2372168404Spjd 2373208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2374208373Smm int64_t delta = MIN(adjustment, 2375208373Smm arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 2376209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, 2377208373Smm ARC_BUFC_METADATA); 2378208373Smm } 2379168404Spjd 2380208373Smm /* 2381208373Smm * Adjust ghost lists 2382208373Smm */ 2383168404Spjd 2384208373Smm adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 2385168404Spjd 2386208373Smm if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 2387208373Smm delta = MIN(arc_mru_ghost->arcs_size, adjustment); 2388209962Smm arc_evict_ghost(arc_mru_ghost, 0, delta); 2389208373Smm } 2390185029Spjd 2391208373Smm adjustment = 2392208373Smm arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 2393208373Smm 2394208373Smm if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 2395208373Smm delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 2396209962Smm arc_evict_ghost(arc_mfu_ghost, 0, delta); 2397168404Spjd } 2398168404Spjd} 2399168404Spjd 2400168404Spjdstatic void 2401168404Spjdarc_do_user_evicts(void) 2402168404Spjd{ 2403191903Skmacy static arc_buf_t *tmp_arc_eviction_list; 2404191903Skmacy 2405191903Skmacy /* 2406191903Skmacy * Move list over to avoid LOR 2407191903Skmacy */ 2408206796Spjdrestart: 2409168404Spjd mutex_enter(&arc_eviction_mtx); 2410191903Skmacy tmp_arc_eviction_list = arc_eviction_list; 2411191903Skmacy arc_eviction_list = NULL; 2412191903Skmacy mutex_exit(&arc_eviction_mtx); 2413191903Skmacy 2414191903Skmacy while (tmp_arc_eviction_list != NULL) { 2415191903Skmacy arc_buf_t *buf = tmp_arc_eviction_list; 2416191903Skmacy tmp_arc_eviction_list = buf->b_next; 2417219089Spjd mutex_enter(&buf->b_evict_lock); 2418168404Spjd buf->b_hdr = NULL; 2419219089Spjd mutex_exit(&buf->b_evict_lock); 2420168404Spjd 2421168404Spjd if (buf->b_efunc != NULL) 2422268858Sdelphij VERIFY0(buf->b_efunc(buf->b_private)); 2423168404Spjd 2424168404Spjd buf->b_efunc = NULL; 2425168404Spjd buf->b_private = NULL; 2426168404Spjd kmem_cache_free(buf_cache, buf); 2427168404Spjd } 2428191903Skmacy 2429191903Skmacy if (arc_eviction_list != NULL) 2430191903Skmacy goto restart; 2431168404Spjd} 2432168404Spjd 2433168404Spjd/* 2434185029Spjd * Flush all *evictable* data from the cache for the given spa. 2435168404Spjd * NOTE: this will not touch "active" (i.e. referenced) data. 2436168404Spjd */ 2437168404Spjdvoid 2438185029Spjdarc_flush(spa_t *spa) 2439168404Spjd{ 2440209962Smm uint64_t guid = 0; 2441209962Smm 2442209962Smm if (spa) 2443228103Smm guid = spa_load_guid(spa); 2444209962Smm 2445205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_DATA]) { 2446209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 2447185029Spjd if (spa) 2448185029Spjd break; 2449185029Spjd } 2450205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_METADATA]) { 2451209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 2452185029Spjd if (spa) 2453185029Spjd break; 2454185029Spjd } 2455205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_DATA]) { 2456209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 2457185029Spjd if (spa) 2458185029Spjd break; 2459185029Spjd } 2460205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_METADATA]) { 2461209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 2462185029Spjd if (spa) 2463185029Spjd break; 2464185029Spjd } 2465168404Spjd 2466209962Smm arc_evict_ghost(arc_mru_ghost, guid, -1); 2467209962Smm arc_evict_ghost(arc_mfu_ghost, guid, -1); 2468168404Spjd 2469168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2470168404Spjd arc_do_user_evicts(); 2471168404Spjd mutex_exit(&arc_reclaim_thr_lock); 2472185029Spjd ASSERT(spa || arc_eviction_list == NULL); 2473168404Spjd} 2474168404Spjd 2475168404Spjdvoid 2476168404Spjdarc_shrink(void) 2477168404Spjd{ 2478270759Ssmh 2479168404Spjd if (arc_c > arc_c_min) { 2480168404Spjd uint64_t to_free; 2481168404Spjd 2482272483Ssmh DTRACE_PROBE4(arc__shrink, uint64_t, arc_c, uint64_t, 2483272483Ssmh arc_c_min, uint64_t, arc_p, uint64_t, to_free); 2484168404Spjd#ifdef _KERNEL 2485168404Spjd to_free = arc_c >> arc_shrink_shift; 2486168404Spjd#else 2487168404Spjd to_free = arc_c >> arc_shrink_shift; 2488168404Spjd#endif 2489168404Spjd if (arc_c > arc_c_min + to_free) 2490168404Spjd atomic_add_64(&arc_c, -to_free); 2491168404Spjd else 2492168404Spjd arc_c = arc_c_min; 2493168404Spjd 2494168404Spjd atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 2495168404Spjd if (arc_c > arc_size) 2496168404Spjd arc_c = MAX(arc_size, arc_c_min); 2497168404Spjd if (arc_p > arc_c) 2498168404Spjd arc_p = (arc_c >> 1); 2499272483Ssmh 2500272483Ssmh DTRACE_PROBE2(arc__shrunk, uint64_t, arc_c, uint64_t, 2501272483Ssmh arc_p); 2502272483Ssmh 2503168404Spjd ASSERT(arc_c >= arc_c_min); 2504168404Spjd ASSERT((int64_t)arc_p >= 0); 2505168404Spjd } 2506168404Spjd 2507270759Ssmh if (arc_size > arc_c) { 2508270759Ssmh DTRACE_PROBE2(arc__shrink_adjust, uint64_t, arc_size, 2509270759Ssmh uint64_t, arc_c); 2510168404Spjd arc_adjust(); 2511270759Ssmh } 2512168404Spjd} 2513168404Spjd 2514185029Spjdstatic int needfree = 0; 2515168404Spjd 2516168404Spjdstatic int 2517168404Spjdarc_reclaim_needed(void) 2518168404Spjd{ 2519168404Spjd 2520168404Spjd#ifdef _KERNEL 2521219089Spjd 2522270759Ssmh if (needfree) { 2523270759Ssmh DTRACE_PROBE(arc__reclaim_needfree); 2524197816Skmacy return (1); 2525270759Ssmh } 2526168404Spjd 2527191902Skmacy /* 2528212780Savg * Cooperate with pagedaemon when it's time for it to scan 2529212780Savg * and reclaim some pages. 2530191902Skmacy */ 2531272483Ssmh if (freemem < zfs_arc_free_target) { 2532272483Ssmh DTRACE_PROBE2(arc__reclaim_freemem, uint64_t, 2533272483Ssmh freemem, uint64_t, zfs_arc_free_target); 2534191902Skmacy return (1); 2535270759Ssmh } 2536191902Skmacy 2537219089Spjd#ifdef sun 2538168404Spjd /* 2539185029Spjd * take 'desfree' extra pages, so we reclaim sooner, rather than later 2540185029Spjd */ 2541185029Spjd extra = desfree; 2542185029Spjd 2543185029Spjd /* 2544185029Spjd * check that we're out of range of the pageout scanner. It starts to 2545185029Spjd * schedule paging if freemem is less than lotsfree and needfree. 2546185029Spjd * lotsfree is the high-water mark for pageout, and needfree is the 2547185029Spjd * number of needed free pages. We add extra pages here to make sure 2548185029Spjd * the scanner doesn't start up while we're freeing memory. 2549185029Spjd */ 2550185029Spjd if (freemem < lotsfree + needfree + extra) 2551185029Spjd return (1); 2552185029Spjd 2553185029Spjd /* 2554168404Spjd * check to make sure that swapfs has enough space so that anon 2555185029Spjd * reservations can still succeed. anon_resvmem() checks that the 2556168404Spjd * availrmem is greater than swapfs_minfree, and the number of reserved 2557168404Spjd * swap pages. We also add a bit of extra here just to prevent 2558168404Spjd * circumstances from getting really dire. 2559168404Spjd */ 2560168404Spjd if (availrmem < swapfs_minfree + swapfs_reserve + extra) 2561168404Spjd return (1); 2562168404Spjd 2563168404Spjd /* 2564272483Ssmh * Check that we have enough availrmem that memory locking (e.g., via 2565272483Ssmh * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 2566272483Ssmh * stores the number of pages that cannot be locked; when availrmem 2567272483Ssmh * drops below pages_pp_maximum, page locking mechanisms such as 2568272483Ssmh * page_pp_lock() will fail.) 2569272483Ssmh */ 2570272483Ssmh if (availrmem <= pages_pp_maximum) 2571272483Ssmh return (1); 2572272483Ssmh 2573272483Ssmh#endif /* sun */ 2574272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 2575272483Ssmh /* 2576168404Spjd * If we're on an i386 platform, it's possible that we'll exhaust the 2577168404Spjd * kernel heap space before we ever run out of available physical 2578168404Spjd * memory. Most checks of the size of the heap_area compare against 2579168404Spjd * tune.t_minarmem, which is the minimum available real memory that we 2580168404Spjd * can have in the system. However, this is generally fixed at 25 pages 2581168404Spjd * which is so low that it's useless. In this comparison, we seek to 2582168404Spjd * calculate the total heap-size, and reclaim if more than 3/4ths of the 2583185029Spjd * heap is allocated. (Or, in the calculation, if less than 1/4th is 2584168404Spjd * free) 2585168404Spjd */ 2586272483Ssmh if (vmem_size(heap_arena, VMEM_FREE) < 2587272483Ssmh (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2)) { 2588270861Ssmh DTRACE_PROBE2(arc__reclaim_used, uint64_t, 2589272483Ssmh vmem_size(heap_arena, VMEM_FREE), uint64_t, 2590272483Ssmh (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2); 2591270861Ssmh return (1); 2592270861Ssmh } 2593270861Ssmh#endif 2594272483Ssmh#ifdef sun 2595272483Ssmh /* 2596272483Ssmh * If zio data pages are being allocated out of a separate heap segment, 2597272483Ssmh * then enforce that the size of available vmem for this arena remains 2598272483Ssmh * above about 1/16th free. 2599272483Ssmh * 2600272483Ssmh * Note: The 1/16th arena free requirement was put in place 2601272483Ssmh * to aggressively evict memory from the arc in order to avoid 2602272483Ssmh * memory fragmentation issues. 2603272483Ssmh */ 2604272483Ssmh if (zio_arena != NULL && 2605272483Ssmh vmem_size(zio_arena, VMEM_FREE) < 2606272483Ssmh (vmem_size(zio_arena, VMEM_ALLOC) >> 4)) 2607272483Ssmh return (1); 2608270871Speter#endif /* sun */ 2609272483Ssmh#else /* _KERNEL */ 2610168404Spjd if (spa_get_random(100) == 0) 2611168404Spjd return (1); 2612272483Ssmh#endif /* _KERNEL */ 2613270759Ssmh DTRACE_PROBE(arc__reclaim_no); 2614270759Ssmh 2615168404Spjd return (0); 2616168404Spjd} 2617168404Spjd 2618208454Spjdextern kmem_cache_t *zio_buf_cache[]; 2619208454Spjdextern kmem_cache_t *zio_data_buf_cache[]; 2620272527Sdelphijextern kmem_cache_t *range_seg_cache; 2621208454Spjd 2622272483Ssmhstatic void __noinline 2623168404Spjdarc_kmem_reap_now(arc_reclaim_strategy_t strat) 2624168404Spjd{ 2625168404Spjd size_t i; 2626168404Spjd kmem_cache_t *prev_cache = NULL; 2627168404Spjd kmem_cache_t *prev_data_cache = NULL; 2628168404Spjd 2629272483Ssmh DTRACE_PROBE(arc__kmem_reap_start); 2630168404Spjd#ifdef _KERNEL 2631185029Spjd if (arc_meta_used >= arc_meta_limit) { 2632185029Spjd /* 2633185029Spjd * We are exceeding our meta-data cache limit. 2634185029Spjd * Purge some DNLC entries to release holds on meta-data. 2635185029Spjd */ 2636185029Spjd dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 2637185029Spjd } 2638168404Spjd#if defined(__i386) 2639168404Spjd /* 2640168404Spjd * Reclaim unused memory from all kmem caches. 2641168404Spjd */ 2642168404Spjd kmem_reap(); 2643168404Spjd#endif 2644168404Spjd#endif 2645168404Spjd 2646168404Spjd /* 2647185029Spjd * An aggressive reclamation will shrink the cache size as well as 2648168404Spjd * reap free buffers from the arc kmem caches. 2649168404Spjd */ 2650168404Spjd if (strat == ARC_RECLAIM_AGGR) 2651168404Spjd arc_shrink(); 2652168404Spjd 2653168404Spjd for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2654168404Spjd if (zio_buf_cache[i] != prev_cache) { 2655168404Spjd prev_cache = zio_buf_cache[i]; 2656168404Spjd kmem_cache_reap_now(zio_buf_cache[i]); 2657168404Spjd } 2658168404Spjd if (zio_data_buf_cache[i] != prev_data_cache) { 2659168404Spjd prev_data_cache = zio_data_buf_cache[i]; 2660168404Spjd kmem_cache_reap_now(zio_data_buf_cache[i]); 2661168404Spjd } 2662168404Spjd } 2663168404Spjd kmem_cache_reap_now(buf_cache); 2664168404Spjd kmem_cache_reap_now(hdr_cache); 2665272506Sdelphij kmem_cache_reap_now(range_seg_cache); 2666272483Ssmh 2667272483Ssmh#ifdef sun 2668272483Ssmh /* 2669272483Ssmh * Ask the vmem arena to reclaim unused memory from its 2670272483Ssmh * quantum caches. 2671272483Ssmh */ 2672272483Ssmh if (zio_arena != NULL && strat == ARC_RECLAIM_AGGR) 2673272483Ssmh vmem_qcache_reap(zio_arena); 2674272483Ssmh#endif 2675272483Ssmh DTRACE_PROBE(arc__kmem_reap_end); 2676168404Spjd} 2677168404Spjd 2678168404Spjdstatic void 2679168404Spjdarc_reclaim_thread(void *dummy __unused) 2680168404Spjd{ 2681168404Spjd clock_t growtime = 0; 2682168404Spjd arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2683168404Spjd callb_cpr_t cpr; 2684168404Spjd 2685168404Spjd CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2686168404Spjd 2687168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2688168404Spjd while (arc_thread_exit == 0) { 2689168404Spjd if (arc_reclaim_needed()) { 2690168404Spjd 2691168404Spjd if (arc_no_grow) { 2692168404Spjd if (last_reclaim == ARC_RECLAIM_CONS) { 2693272483Ssmh DTRACE_PROBE(arc__reclaim_aggr_no_grow); 2694168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2695168404Spjd } else { 2696168404Spjd last_reclaim = ARC_RECLAIM_CONS; 2697168404Spjd } 2698168404Spjd } else { 2699168404Spjd arc_no_grow = TRUE; 2700168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2701272483Ssmh DTRACE_PROBE(arc__reclaim_aggr); 2702168404Spjd membar_producer(); 2703168404Spjd } 2704168404Spjd 2705168404Spjd /* reset the growth delay for every reclaim */ 2706219089Spjd growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 2707168404Spjd 2708185029Spjd if (needfree && last_reclaim == ARC_RECLAIM_CONS) { 2709168404Spjd /* 2710185029Spjd * If needfree is TRUE our vm_lowmem hook 2711168404Spjd * was called and in that case we must free some 2712168404Spjd * memory, so switch to aggressive mode. 2713168404Spjd */ 2714168404Spjd arc_no_grow = TRUE; 2715168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2716168404Spjd } 2717168404Spjd arc_kmem_reap_now(last_reclaim); 2718185029Spjd arc_warm = B_TRUE; 2719185029Spjd 2720219089Spjd } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 2721168404Spjd arc_no_grow = FALSE; 2722168404Spjd } 2723168404Spjd 2724209275Smm arc_adjust(); 2725168404Spjd 2726168404Spjd if (arc_eviction_list != NULL) 2727168404Spjd arc_do_user_evicts(); 2728168404Spjd 2729211762Savg#ifdef _KERNEL 2730211762Savg if (needfree) { 2731185029Spjd needfree = 0; 2732185029Spjd wakeup(&needfree); 2733211762Savg } 2734168404Spjd#endif 2735168404Spjd 2736168404Spjd /* block until needed, or one second, whichever is shorter */ 2737168404Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 2738168404Spjd (void) cv_timedwait(&arc_reclaim_thr_cv, 2739168404Spjd &arc_reclaim_thr_lock, hz); 2740168404Spjd CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2741168404Spjd } 2742168404Spjd 2743168404Spjd arc_thread_exit = 0; 2744168404Spjd cv_broadcast(&arc_reclaim_thr_cv); 2745168404Spjd CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2746168404Spjd thread_exit(); 2747168404Spjd} 2748168404Spjd 2749168404Spjd/* 2750168404Spjd * Adapt arc info given the number of bytes we are trying to add and 2751168404Spjd * the state that we are comming from. This function is only called 2752168404Spjd * when we are adding new content to the cache. 2753168404Spjd */ 2754168404Spjdstatic void 2755168404Spjdarc_adapt(int bytes, arc_state_t *state) 2756168404Spjd{ 2757168404Spjd int mult; 2758208373Smm uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 2759168404Spjd 2760185029Spjd if (state == arc_l2c_only) 2761185029Spjd return; 2762185029Spjd 2763168404Spjd ASSERT(bytes > 0); 2764168404Spjd /* 2765168404Spjd * Adapt the target size of the MRU list: 2766168404Spjd * - if we just hit in the MRU ghost list, then increase 2767168404Spjd * the target size of the MRU list. 2768168404Spjd * - if we just hit in the MFU ghost list, then increase 2769168404Spjd * the target size of the MFU list by decreasing the 2770168404Spjd * target size of the MRU list. 2771168404Spjd */ 2772168404Spjd if (state == arc_mru_ghost) { 2773168404Spjd mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 2774168404Spjd 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 2775209275Smm mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 2776168404Spjd 2777208373Smm arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 2778168404Spjd } else if (state == arc_mfu_ghost) { 2779208373Smm uint64_t delta; 2780208373Smm 2781168404Spjd mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 2782168404Spjd 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 2783209275Smm mult = MIN(mult, 10); 2784168404Spjd 2785208373Smm delta = MIN(bytes * mult, arc_p); 2786208373Smm arc_p = MAX(arc_p_min, arc_p - delta); 2787168404Spjd } 2788168404Spjd ASSERT((int64_t)arc_p >= 0); 2789168404Spjd 2790168404Spjd if (arc_reclaim_needed()) { 2791168404Spjd cv_signal(&arc_reclaim_thr_cv); 2792168404Spjd return; 2793168404Spjd } 2794168404Spjd 2795168404Spjd if (arc_no_grow) 2796168404Spjd return; 2797168404Spjd 2798168404Spjd if (arc_c >= arc_c_max) 2799168404Spjd return; 2800168404Spjd 2801168404Spjd /* 2802168404Spjd * If we're within (2 * maxblocksize) bytes of the target 2803168404Spjd * cache size, increment the target cache size 2804168404Spjd */ 2805168404Spjd if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 2806272483Ssmh DTRACE_PROBE1(arc__inc_adapt, int, bytes); 2807168404Spjd atomic_add_64(&arc_c, (int64_t)bytes); 2808168404Spjd if (arc_c > arc_c_max) 2809168404Spjd arc_c = arc_c_max; 2810168404Spjd else if (state == arc_anon) 2811168404Spjd atomic_add_64(&arc_p, (int64_t)bytes); 2812168404Spjd if (arc_p > arc_c) 2813168404Spjd arc_p = arc_c; 2814168404Spjd } 2815168404Spjd ASSERT((int64_t)arc_p >= 0); 2816168404Spjd} 2817168404Spjd 2818168404Spjd/* 2819168404Spjd * Check if the cache has reached its limits and eviction is required 2820168404Spjd * prior to insert. 2821168404Spjd */ 2822168404Spjdstatic int 2823185029Spjdarc_evict_needed(arc_buf_contents_t type) 2824168404Spjd{ 2825185029Spjd if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 2826185029Spjd return (1); 2827185029Spjd 2828168404Spjd if (arc_reclaim_needed()) 2829168404Spjd return (1); 2830168404Spjd 2831168404Spjd return (arc_size > arc_c); 2832168404Spjd} 2833168404Spjd 2834168404Spjd/* 2835168404Spjd * The buffer, supplied as the first argument, needs a data block. 2836168404Spjd * So, if we are at cache max, determine which cache should be victimized. 2837168404Spjd * We have the following cases: 2838168404Spjd * 2839168404Spjd * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2840168404Spjd * In this situation if we're out of space, but the resident size of the MFU is 2841168404Spjd * under the limit, victimize the MFU cache to satisfy this insertion request. 2842168404Spjd * 2843168404Spjd * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2844168404Spjd * Here, we've used up all of the available space for the MRU, so we need to 2845168404Spjd * evict from our own cache instead. Evict from the set of resident MRU 2846168404Spjd * entries. 2847168404Spjd * 2848168404Spjd * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2849168404Spjd * c minus p represents the MFU space in the cache, since p is the size of the 2850168404Spjd * cache that is dedicated to the MRU. In this situation there's still space on 2851168404Spjd * the MFU side, so the MRU side needs to be victimized. 2852168404Spjd * 2853168404Spjd * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2854168404Spjd * MFU's resident set is consuming more space than it has been allotted. In 2855168404Spjd * this situation, we must victimize our own cache, the MFU, for this insertion. 2856168404Spjd */ 2857168404Spjdstatic void 2858168404Spjdarc_get_data_buf(arc_buf_t *buf) 2859168404Spjd{ 2860168404Spjd arc_state_t *state = buf->b_hdr->b_state; 2861168404Spjd uint64_t size = buf->b_hdr->b_size; 2862168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 2863168404Spjd 2864168404Spjd arc_adapt(size, state); 2865168404Spjd 2866168404Spjd /* 2867168404Spjd * We have not yet reached cache maximum size, 2868168404Spjd * just allocate a new buffer. 2869168404Spjd */ 2870185029Spjd if (!arc_evict_needed(type)) { 2871168404Spjd if (type == ARC_BUFC_METADATA) { 2872168404Spjd buf->b_data = zio_buf_alloc(size); 2873208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2874168404Spjd } else { 2875168404Spjd ASSERT(type == ARC_BUFC_DATA); 2876168404Spjd buf->b_data = zio_data_buf_alloc(size); 2877208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2878185029Spjd atomic_add_64(&arc_size, size); 2879168404Spjd } 2880168404Spjd goto out; 2881168404Spjd } 2882168404Spjd 2883168404Spjd /* 2884168404Spjd * If we are prefetching from the mfu ghost list, this buffer 2885168404Spjd * will end up on the mru list; so steal space from there. 2886168404Spjd */ 2887168404Spjd if (state == arc_mfu_ghost) 2888168404Spjd state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 2889168404Spjd else if (state == arc_mru_ghost) 2890168404Spjd state = arc_mru; 2891168404Spjd 2892168404Spjd if (state == arc_mru || state == arc_anon) { 2893168404Spjd uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 2894208373Smm state = (arc_mfu->arcs_lsize[type] >= size && 2895185029Spjd arc_p > mru_used) ? arc_mfu : arc_mru; 2896168404Spjd } else { 2897168404Spjd /* MFU cases */ 2898168404Spjd uint64_t mfu_space = arc_c - arc_p; 2899208373Smm state = (arc_mru->arcs_lsize[type] >= size && 2900185029Spjd mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 2901168404Spjd } 2902209962Smm if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) { 2903168404Spjd if (type == ARC_BUFC_METADATA) { 2904168404Spjd buf->b_data = zio_buf_alloc(size); 2905208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2906168404Spjd } else { 2907168404Spjd ASSERT(type == ARC_BUFC_DATA); 2908168404Spjd buf->b_data = zio_data_buf_alloc(size); 2909208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2910185029Spjd atomic_add_64(&arc_size, size); 2911168404Spjd } 2912168404Spjd ARCSTAT_BUMP(arcstat_recycle_miss); 2913168404Spjd } 2914168404Spjd ASSERT(buf->b_data != NULL); 2915168404Spjdout: 2916168404Spjd /* 2917168404Spjd * Update the state size. Note that ghost states have a 2918168404Spjd * "ghost size" and so don't need to be updated. 2919168404Spjd */ 2920168404Spjd if (!GHOST_STATE(buf->b_hdr->b_state)) { 2921168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2922168404Spjd 2923168404Spjd atomic_add_64(&hdr->b_state->arcs_size, size); 2924168404Spjd if (list_link_active(&hdr->b_arc_node)) { 2925168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 2926185029Spjd atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2927168404Spjd } 2928168404Spjd /* 2929168404Spjd * If we are growing the cache, and we are adding anonymous 2930168404Spjd * data, and we have outgrown arc_p, update arc_p 2931168404Spjd */ 2932168404Spjd if (arc_size < arc_c && hdr->b_state == arc_anon && 2933168404Spjd arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 2934168404Spjd arc_p = MIN(arc_c, arc_p + size); 2935168404Spjd } 2936205231Skmacy ARCSTAT_BUMP(arcstat_allocated); 2937168404Spjd} 2938168404Spjd 2939168404Spjd/* 2940168404Spjd * This routine is called whenever a buffer is accessed. 2941168404Spjd * NOTE: the hash lock is dropped in this function. 2942168404Spjd */ 2943168404Spjdstatic void 2944168404Spjdarc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2945168404Spjd{ 2946219089Spjd clock_t now; 2947219089Spjd 2948168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 2949168404Spjd 2950168404Spjd if (buf->b_state == arc_anon) { 2951168404Spjd /* 2952168404Spjd * This buffer is not in the cache, and does not 2953168404Spjd * appear in our "ghost" list. Add the new buffer 2954168404Spjd * to the MRU state. 2955168404Spjd */ 2956168404Spjd 2957168404Spjd ASSERT(buf->b_arc_access == 0); 2958219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2959168404Spjd DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2960168404Spjd arc_change_state(arc_mru, buf, hash_lock); 2961168404Spjd 2962168404Spjd } else if (buf->b_state == arc_mru) { 2963219089Spjd now = ddi_get_lbolt(); 2964219089Spjd 2965168404Spjd /* 2966168404Spjd * If this buffer is here because of a prefetch, then either: 2967168404Spjd * - clear the flag if this is a "referencing" read 2968168404Spjd * (any subsequent access will bump this into the MFU state). 2969168404Spjd * or 2970168404Spjd * - move the buffer to the head of the list if this is 2971168404Spjd * another prefetch (to make it less likely to be evicted). 2972168404Spjd */ 2973168404Spjd if ((buf->b_flags & ARC_PREFETCH) != 0) { 2974168404Spjd if (refcount_count(&buf->b_refcnt) == 0) { 2975168404Spjd ASSERT(list_link_active(&buf->b_arc_node)); 2976168404Spjd } else { 2977168404Spjd buf->b_flags &= ~ARC_PREFETCH; 2978168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 2979168404Spjd } 2980219089Spjd buf->b_arc_access = now; 2981168404Spjd return; 2982168404Spjd } 2983168404Spjd 2984168404Spjd /* 2985168404Spjd * This buffer has been "accessed" only once so far, 2986168404Spjd * but it is still in the cache. Move it to the MFU 2987168404Spjd * state. 2988168404Spjd */ 2989219089Spjd if (now > buf->b_arc_access + ARC_MINTIME) { 2990168404Spjd /* 2991168404Spjd * More than 125ms have passed since we 2992168404Spjd * instantiated this buffer. Move it to the 2993168404Spjd * most frequently used state. 2994168404Spjd */ 2995219089Spjd buf->b_arc_access = now; 2996168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2997168404Spjd arc_change_state(arc_mfu, buf, hash_lock); 2998168404Spjd } 2999168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 3000168404Spjd } else if (buf->b_state == arc_mru_ghost) { 3001168404Spjd arc_state_t *new_state; 3002168404Spjd /* 3003168404Spjd * This buffer has been "accessed" recently, but 3004168404Spjd * was evicted from the cache. Move it to the 3005168404Spjd * MFU state. 3006168404Spjd */ 3007168404Spjd 3008168404Spjd if (buf->b_flags & ARC_PREFETCH) { 3009168404Spjd new_state = arc_mru; 3010168404Spjd if (refcount_count(&buf->b_refcnt) > 0) 3011168404Spjd buf->b_flags &= ~ARC_PREFETCH; 3012168404Spjd DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 3013168404Spjd } else { 3014168404Spjd new_state = arc_mfu; 3015168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 3016168404Spjd } 3017168404Spjd 3018219089Spjd buf->b_arc_access = ddi_get_lbolt(); 3019168404Spjd arc_change_state(new_state, buf, hash_lock); 3020168404Spjd 3021168404Spjd ARCSTAT_BUMP(arcstat_mru_ghost_hits); 3022168404Spjd } else if (buf->b_state == arc_mfu) { 3023168404Spjd /* 3024168404Spjd * This buffer has been accessed more than once and is 3025168404Spjd * still in the cache. Keep it in the MFU state. 3026168404Spjd * 3027168404Spjd * NOTE: an add_reference() that occurred when we did 3028168404Spjd * the arc_read() will have kicked this off the list. 3029168404Spjd * If it was a prefetch, we will explicitly move it to 3030168404Spjd * the head of the list now. 3031168404Spjd */ 3032168404Spjd if ((buf->b_flags & ARC_PREFETCH) != 0) { 3033168404Spjd ASSERT(refcount_count(&buf->b_refcnt) == 0); 3034168404Spjd ASSERT(list_link_active(&buf->b_arc_node)); 3035168404Spjd } 3036168404Spjd ARCSTAT_BUMP(arcstat_mfu_hits); 3037219089Spjd buf->b_arc_access = ddi_get_lbolt(); 3038168404Spjd } else if (buf->b_state == arc_mfu_ghost) { 3039168404Spjd arc_state_t *new_state = arc_mfu; 3040168404Spjd /* 3041168404Spjd * This buffer has been accessed more than once but has 3042168404Spjd * been evicted from the cache. Move it back to the 3043168404Spjd * MFU state. 3044168404Spjd */ 3045168404Spjd 3046168404Spjd if (buf->b_flags & ARC_PREFETCH) { 3047168404Spjd /* 3048168404Spjd * This is a prefetch access... 3049168404Spjd * move this block back to the MRU state. 3050168404Spjd */ 3051240415Smm ASSERT0(refcount_count(&buf->b_refcnt)); 3052168404Spjd new_state = arc_mru; 3053168404Spjd } 3054168404Spjd 3055219089Spjd buf->b_arc_access = ddi_get_lbolt(); 3056168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 3057168404Spjd arc_change_state(new_state, buf, hash_lock); 3058168404Spjd 3059168404Spjd ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 3060185029Spjd } else if (buf->b_state == arc_l2c_only) { 3061185029Spjd /* 3062185029Spjd * This buffer is on the 2nd Level ARC. 3063185029Spjd */ 3064185029Spjd 3065219089Spjd buf->b_arc_access = ddi_get_lbolt(); 3066185029Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 3067185029Spjd arc_change_state(arc_mfu, buf, hash_lock); 3068168404Spjd } else { 3069168404Spjd ASSERT(!"invalid arc state"); 3070168404Spjd } 3071168404Spjd} 3072168404Spjd 3073168404Spjd/* a generic arc_done_func_t which you can use */ 3074168404Spjd/* ARGSUSED */ 3075168404Spjdvoid 3076168404Spjdarc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 3077168404Spjd{ 3078219089Spjd if (zio == NULL || zio->io_error == 0) 3079219089Spjd bcopy(buf->b_data, arg, buf->b_hdr->b_size); 3080248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 3081168404Spjd} 3082168404Spjd 3083185029Spjd/* a generic arc_done_func_t */ 3084168404Spjdvoid 3085168404Spjdarc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 3086168404Spjd{ 3087168404Spjd arc_buf_t **bufp = arg; 3088168404Spjd if (zio && zio->io_error) { 3089248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 3090168404Spjd *bufp = NULL; 3091168404Spjd } else { 3092168404Spjd *bufp = buf; 3093219089Spjd ASSERT(buf->b_data); 3094168404Spjd } 3095168404Spjd} 3096168404Spjd 3097168404Spjdstatic void 3098168404Spjdarc_read_done(zio_t *zio) 3099168404Spjd{ 3100268075Sdelphij arc_buf_hdr_t *hdr; 3101168404Spjd arc_buf_t *buf; 3102168404Spjd arc_buf_t *abuf; /* buffer we're assigning to callback */ 3103268075Sdelphij kmutex_t *hash_lock = NULL; 3104168404Spjd arc_callback_t *callback_list, *acb; 3105168404Spjd int freeable = FALSE; 3106168404Spjd 3107168404Spjd buf = zio->io_private; 3108168404Spjd hdr = buf->b_hdr; 3109168404Spjd 3110168404Spjd /* 3111168404Spjd * The hdr was inserted into hash-table and removed from lists 3112168404Spjd * prior to starting I/O. We should find this header, since 3113168404Spjd * it's in the hash table, and it should be legit since it's 3114168404Spjd * not possible to evict it during the I/O. The only possible 3115168404Spjd * reason for it not to be found is if we were freed during the 3116168404Spjd * read. 3117168404Spjd */ 3118268075Sdelphij if (HDR_IN_HASH_TABLE(hdr)) { 3119268075Sdelphij ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 3120268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[0], ==, 3121268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[0]); 3122268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[1], ==, 3123268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[1]); 3124168404Spjd 3125268075Sdelphij arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 3126268075Sdelphij &hash_lock); 3127168404Spjd 3128268075Sdelphij ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && 3129268075Sdelphij hash_lock == NULL) || 3130268075Sdelphij (found == hdr && 3131268075Sdelphij DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 3132268075Sdelphij (found == hdr && HDR_L2_READING(hdr))); 3133268075Sdelphij } 3134268075Sdelphij 3135185029Spjd hdr->b_flags &= ~ARC_L2_EVICTED; 3136185029Spjd if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 3137185029Spjd hdr->b_flags &= ~ARC_L2CACHE; 3138206796Spjd 3139168404Spjd /* byteswap if necessary */ 3140168404Spjd callback_list = hdr->b_acb; 3141168404Spjd ASSERT(callback_list != NULL); 3142209101Smm if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 3143236884Smm dmu_object_byteswap_t bswap = 3144236884Smm DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 3145185029Spjd arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 3146185029Spjd byteswap_uint64_array : 3147236884Smm dmu_ot_byteswap[bswap].ob_func; 3148185029Spjd func(buf->b_data, hdr->b_size); 3149185029Spjd } 3150168404Spjd 3151185029Spjd arc_cksum_compute(buf, B_FALSE); 3152240133Smm#ifdef illumos 3153240133Smm arc_buf_watch(buf); 3154240133Smm#endif /* illumos */ 3155168404Spjd 3156219089Spjd if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 3157219089Spjd /* 3158219089Spjd * Only call arc_access on anonymous buffers. This is because 3159219089Spjd * if we've issued an I/O for an evicted buffer, we've already 3160219089Spjd * called arc_access (to prevent any simultaneous readers from 3161219089Spjd * getting confused). 3162219089Spjd */ 3163219089Spjd arc_access(hdr, hash_lock); 3164219089Spjd } 3165219089Spjd 3166168404Spjd /* create copies of the data buffer for the callers */ 3167168404Spjd abuf = buf; 3168168404Spjd for (acb = callback_list; acb; acb = acb->acb_next) { 3169168404Spjd if (acb->acb_done) { 3170242845Sdelphij if (abuf == NULL) { 3171242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_reads); 3172168404Spjd abuf = arc_buf_clone(buf); 3173242845Sdelphij } 3174168404Spjd acb->acb_buf = abuf; 3175168404Spjd abuf = NULL; 3176168404Spjd } 3177168404Spjd } 3178168404Spjd hdr->b_acb = NULL; 3179168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3180168404Spjd ASSERT(!HDR_BUF_AVAILABLE(hdr)); 3181219089Spjd if (abuf == buf) { 3182219089Spjd ASSERT(buf->b_efunc == NULL); 3183219089Spjd ASSERT(hdr->b_datacnt == 1); 3184168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 3185219089Spjd } 3186168404Spjd 3187168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 3188168404Spjd 3189168404Spjd if (zio->io_error != 0) { 3190168404Spjd hdr->b_flags |= ARC_IO_ERROR; 3191168404Spjd if (hdr->b_state != arc_anon) 3192168404Spjd arc_change_state(arc_anon, hdr, hash_lock); 3193168404Spjd if (HDR_IN_HASH_TABLE(hdr)) 3194168404Spjd buf_hash_remove(hdr); 3195168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 3196168404Spjd } 3197168404Spjd 3198168404Spjd /* 3199168404Spjd * Broadcast before we drop the hash_lock to avoid the possibility 3200168404Spjd * that the hdr (and hence the cv) might be freed before we get to 3201168404Spjd * the cv_broadcast(). 3202168404Spjd */ 3203168404Spjd cv_broadcast(&hdr->b_cv); 3204168404Spjd 3205168404Spjd if (hash_lock) { 3206168404Spjd mutex_exit(hash_lock); 3207168404Spjd } else { 3208168404Spjd /* 3209168404Spjd * This block was freed while we waited for the read to 3210168404Spjd * complete. It has been removed from the hash table and 3211168404Spjd * moved to the anonymous state (so that it won't show up 3212168404Spjd * in the cache). 3213168404Spjd */ 3214168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 3215168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 3216168404Spjd } 3217168404Spjd 3218168404Spjd /* execute each callback and free its structure */ 3219168404Spjd while ((acb = callback_list) != NULL) { 3220168404Spjd if (acb->acb_done) 3221168404Spjd acb->acb_done(zio, acb->acb_buf, acb->acb_private); 3222168404Spjd 3223168404Spjd if (acb->acb_zio_dummy != NULL) { 3224168404Spjd acb->acb_zio_dummy->io_error = zio->io_error; 3225168404Spjd zio_nowait(acb->acb_zio_dummy); 3226168404Spjd } 3227168404Spjd 3228168404Spjd callback_list = acb->acb_next; 3229168404Spjd kmem_free(acb, sizeof (arc_callback_t)); 3230168404Spjd } 3231168404Spjd 3232168404Spjd if (freeable) 3233168404Spjd arc_hdr_destroy(hdr); 3234168404Spjd} 3235168404Spjd 3236168404Spjd/* 3237168404Spjd * "Read" the block block at the specified DVA (in bp) via the 3238168404Spjd * cache. If the block is found in the cache, invoke the provided 3239168404Spjd * callback immediately and return. Note that the `zio' parameter 3240168404Spjd * in the callback will be NULL in this case, since no IO was 3241168404Spjd * required. If the block is not in the cache pass the read request 3242168404Spjd * on to the spa with a substitute callback function, so that the 3243168404Spjd * requested block will be added to the cache. 3244168404Spjd * 3245168404Spjd * If a read request arrives for a block that has a read in-progress, 3246168404Spjd * either wait for the in-progress read to complete (and return the 3247168404Spjd * results); or, if this is a read with a "done" func, add a record 3248168404Spjd * to the read to invoke the "done" func when the read completes, 3249168404Spjd * and return; or just return. 3250168404Spjd * 3251168404Spjd * arc_read_done() will invoke all the requested "done" functions 3252168404Spjd * for readers of this block. 3253168404Spjd */ 3254168404Spjdint 3255246666Smmarc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 3256258632Savg void *private, zio_priority_t priority, int zio_flags, uint32_t *arc_flags, 3257268123Sdelphij const zbookmark_phys_t *zb) 3258168404Spjd{ 3259268075Sdelphij arc_buf_hdr_t *hdr = NULL; 3260247187Smm arc_buf_t *buf = NULL; 3261268075Sdelphij kmutex_t *hash_lock = NULL; 3262185029Spjd zio_t *rzio; 3263228103Smm uint64_t guid = spa_load_guid(spa); 3264168404Spjd 3265268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp) || 3266268075Sdelphij BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 3267268075Sdelphij 3268168404Spjdtop: 3269268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 3270268075Sdelphij /* 3271268075Sdelphij * Embedded BP's have no DVA and require no I/O to "read". 3272268075Sdelphij * Create an anonymous arc buf to back it. 3273268075Sdelphij */ 3274268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 3275268075Sdelphij } 3276168404Spjd 3277268075Sdelphij if (hdr != NULL && hdr->b_datacnt > 0) { 3278268075Sdelphij 3279168404Spjd *arc_flags |= ARC_CACHED; 3280168404Spjd 3281168404Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3282168404Spjd 3283168404Spjd if (*arc_flags & ARC_WAIT) { 3284168404Spjd cv_wait(&hdr->b_cv, hash_lock); 3285168404Spjd mutex_exit(hash_lock); 3286168404Spjd goto top; 3287168404Spjd } 3288168404Spjd ASSERT(*arc_flags & ARC_NOWAIT); 3289168404Spjd 3290168404Spjd if (done) { 3291168404Spjd arc_callback_t *acb = NULL; 3292168404Spjd 3293168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), 3294168404Spjd KM_SLEEP); 3295168404Spjd acb->acb_done = done; 3296168404Spjd acb->acb_private = private; 3297168404Spjd if (pio != NULL) 3298168404Spjd acb->acb_zio_dummy = zio_null(pio, 3299209962Smm spa, NULL, NULL, NULL, zio_flags); 3300168404Spjd 3301168404Spjd ASSERT(acb->acb_done != NULL); 3302168404Spjd acb->acb_next = hdr->b_acb; 3303168404Spjd hdr->b_acb = acb; 3304168404Spjd add_reference(hdr, hash_lock, private); 3305168404Spjd mutex_exit(hash_lock); 3306168404Spjd return (0); 3307168404Spjd } 3308168404Spjd mutex_exit(hash_lock); 3309168404Spjd return (0); 3310168404Spjd } 3311168404Spjd 3312168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3313168404Spjd 3314168404Spjd if (done) { 3315168404Spjd add_reference(hdr, hash_lock, private); 3316168404Spjd /* 3317168404Spjd * If this block is already in use, create a new 3318168404Spjd * copy of the data so that we will be guaranteed 3319168404Spjd * that arc_release() will always succeed. 3320168404Spjd */ 3321168404Spjd buf = hdr->b_buf; 3322168404Spjd ASSERT(buf); 3323168404Spjd ASSERT(buf->b_data); 3324168404Spjd if (HDR_BUF_AVAILABLE(hdr)) { 3325168404Spjd ASSERT(buf->b_efunc == NULL); 3326168404Spjd hdr->b_flags &= ~ARC_BUF_AVAILABLE; 3327168404Spjd } else { 3328168404Spjd buf = arc_buf_clone(buf); 3329168404Spjd } 3330219089Spjd 3331168404Spjd } else if (*arc_flags & ARC_PREFETCH && 3332168404Spjd refcount_count(&hdr->b_refcnt) == 0) { 3333168404Spjd hdr->b_flags |= ARC_PREFETCH; 3334168404Spjd } 3335168404Spjd DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 3336168404Spjd arc_access(hdr, hash_lock); 3337185029Spjd if (*arc_flags & ARC_L2CACHE) 3338185029Spjd hdr->b_flags |= ARC_L2CACHE; 3339251478Sdelphij if (*arc_flags & ARC_L2COMPRESS) 3340251478Sdelphij hdr->b_flags |= ARC_L2COMPRESS; 3341168404Spjd mutex_exit(hash_lock); 3342168404Spjd ARCSTAT_BUMP(arcstat_hits); 3343168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 3344168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3345168404Spjd data, metadata, hits); 3346168404Spjd 3347168404Spjd if (done) 3348168404Spjd done(NULL, buf, private); 3349168404Spjd } else { 3350168404Spjd uint64_t size = BP_GET_LSIZE(bp); 3351268075Sdelphij arc_callback_t *acb; 3352185029Spjd vdev_t *vd = NULL; 3353247187Smm uint64_t addr = 0; 3354208373Smm boolean_t devw = B_FALSE; 3355258389Savg enum zio_compress b_compress = ZIO_COMPRESS_OFF; 3356258389Savg uint64_t b_asize = 0; 3357168404Spjd 3358168404Spjd if (hdr == NULL) { 3359168404Spjd /* this block is not in the cache */ 3360268075Sdelphij arc_buf_hdr_t *exists = NULL; 3361168404Spjd arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 3362168404Spjd buf = arc_buf_alloc(spa, size, private, type); 3363168404Spjd hdr = buf->b_hdr; 3364268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 3365268075Sdelphij hdr->b_dva = *BP_IDENTITY(bp); 3366268075Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 3367268075Sdelphij hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 3368268075Sdelphij exists = buf_hash_insert(hdr, &hash_lock); 3369268075Sdelphij } 3370268075Sdelphij if (exists != NULL) { 3371168404Spjd /* somebody beat us to the hash insert */ 3372168404Spjd mutex_exit(hash_lock); 3373219089Spjd buf_discard_identity(hdr); 3374168404Spjd (void) arc_buf_remove_ref(buf, private); 3375168404Spjd goto top; /* restart the IO request */ 3376168404Spjd } 3377168404Spjd /* if this is a prefetch, we don't have a reference */ 3378168404Spjd if (*arc_flags & ARC_PREFETCH) { 3379168404Spjd (void) remove_reference(hdr, hash_lock, 3380168404Spjd private); 3381168404Spjd hdr->b_flags |= ARC_PREFETCH; 3382168404Spjd } 3383185029Spjd if (*arc_flags & ARC_L2CACHE) 3384185029Spjd hdr->b_flags |= ARC_L2CACHE; 3385251478Sdelphij if (*arc_flags & ARC_L2COMPRESS) 3386251478Sdelphij hdr->b_flags |= ARC_L2COMPRESS; 3387168404Spjd if (BP_GET_LEVEL(bp) > 0) 3388168404Spjd hdr->b_flags |= ARC_INDIRECT; 3389168404Spjd } else { 3390168404Spjd /* this block is in the ghost cache */ 3391168404Spjd ASSERT(GHOST_STATE(hdr->b_state)); 3392168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3393240415Smm ASSERT0(refcount_count(&hdr->b_refcnt)); 3394168404Spjd ASSERT(hdr->b_buf == NULL); 3395168404Spjd 3396168404Spjd /* if this is a prefetch, we don't have a reference */ 3397168404Spjd if (*arc_flags & ARC_PREFETCH) 3398168404Spjd hdr->b_flags |= ARC_PREFETCH; 3399168404Spjd else 3400168404Spjd add_reference(hdr, hash_lock, private); 3401185029Spjd if (*arc_flags & ARC_L2CACHE) 3402185029Spjd hdr->b_flags |= ARC_L2CACHE; 3403251478Sdelphij if (*arc_flags & ARC_L2COMPRESS) 3404251478Sdelphij hdr->b_flags |= ARC_L2COMPRESS; 3405185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 3406168404Spjd buf->b_hdr = hdr; 3407168404Spjd buf->b_data = NULL; 3408168404Spjd buf->b_efunc = NULL; 3409168404Spjd buf->b_private = NULL; 3410168404Spjd buf->b_next = NULL; 3411168404Spjd hdr->b_buf = buf; 3412168404Spjd ASSERT(hdr->b_datacnt == 0); 3413168404Spjd hdr->b_datacnt = 1; 3414219089Spjd arc_get_data_buf(buf); 3415219089Spjd arc_access(hdr, hash_lock); 3416168404Spjd } 3417168404Spjd 3418219089Spjd ASSERT(!GHOST_STATE(hdr->b_state)); 3419219089Spjd 3420168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 3421168404Spjd acb->acb_done = done; 3422168404Spjd acb->acb_private = private; 3423168404Spjd 3424168404Spjd ASSERT(hdr->b_acb == NULL); 3425168404Spjd hdr->b_acb = acb; 3426168404Spjd hdr->b_flags |= ARC_IO_IN_PROGRESS; 3427168404Spjd 3428258389Savg if (hdr->b_l2hdr != NULL && 3429185029Spjd (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 3430208373Smm devw = hdr->b_l2hdr->b_dev->l2ad_writing; 3431185029Spjd addr = hdr->b_l2hdr->b_daddr; 3432258389Savg b_compress = hdr->b_l2hdr->b_compress; 3433258389Savg b_asize = hdr->b_l2hdr->b_asize; 3434185029Spjd /* 3435185029Spjd * Lock out device removal. 3436185029Spjd */ 3437185029Spjd if (vdev_is_dead(vd) || 3438185029Spjd !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 3439185029Spjd vd = NULL; 3440185029Spjd } 3441185029Spjd 3442268075Sdelphij if (hash_lock != NULL) 3443268075Sdelphij mutex_exit(hash_lock); 3444168404Spjd 3445251629Sdelphij /* 3446251629Sdelphij * At this point, we have a level 1 cache miss. Try again in 3447251629Sdelphij * L2ARC if possible. 3448251629Sdelphij */ 3449168404Spjd ASSERT3U(hdr->b_size, ==, size); 3450219089Spjd DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 3451268123Sdelphij uint64_t, size, zbookmark_phys_t *, zb); 3452168404Spjd ARCSTAT_BUMP(arcstat_misses); 3453168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 3454168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3455168404Spjd data, metadata, misses); 3456228392Spjd#ifdef _KERNEL 3457228392Spjd curthread->td_ru.ru_inblock++; 3458228392Spjd#endif 3459168404Spjd 3460208373Smm if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 3461185029Spjd /* 3462185029Spjd * Read from the L2ARC if the following are true: 3463185029Spjd * 1. The L2ARC vdev was previously cached. 3464185029Spjd * 2. This buffer still has L2ARC metadata. 3465185029Spjd * 3. This buffer isn't currently writing to the L2ARC. 3466185029Spjd * 4. The L2ARC entry wasn't evicted, which may 3467185029Spjd * also have invalidated the vdev. 3468208373Smm * 5. This isn't prefetch and l2arc_noprefetch is set. 3469185029Spjd */ 3470185029Spjd if (hdr->b_l2hdr != NULL && 3471208373Smm !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 3472208373Smm !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 3473185029Spjd l2arc_read_callback_t *cb; 3474185029Spjd 3475185029Spjd DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 3476185029Spjd ARCSTAT_BUMP(arcstat_l2_hits); 3477185029Spjd 3478185029Spjd cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 3479185029Spjd KM_SLEEP); 3480185029Spjd cb->l2rcb_buf = buf; 3481185029Spjd cb->l2rcb_spa = spa; 3482185029Spjd cb->l2rcb_bp = *bp; 3483185029Spjd cb->l2rcb_zb = *zb; 3484185029Spjd cb->l2rcb_flags = zio_flags; 3485258389Savg cb->l2rcb_compress = b_compress; 3486185029Spjd 3487247187Smm ASSERT(addr >= VDEV_LABEL_START_SIZE && 3488247187Smm addr + size < vd->vdev_psize - 3489247187Smm VDEV_LABEL_END_SIZE); 3490247187Smm 3491185029Spjd /* 3492185029Spjd * l2arc read. The SCL_L2ARC lock will be 3493185029Spjd * released by l2arc_read_done(). 3494251478Sdelphij * Issue a null zio if the underlying buffer 3495251478Sdelphij * was squashed to zero size by compression. 3496185029Spjd */ 3497258389Savg if (b_compress == ZIO_COMPRESS_EMPTY) { 3498251478Sdelphij rzio = zio_null(pio, spa, vd, 3499251478Sdelphij l2arc_read_done, cb, 3500251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 3501251478Sdelphij ZIO_FLAG_CANFAIL | 3502251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 3503251478Sdelphij ZIO_FLAG_DONT_RETRY); 3504251478Sdelphij } else { 3505251478Sdelphij rzio = zio_read_phys(pio, vd, addr, 3506258389Savg b_asize, buf->b_data, 3507258389Savg ZIO_CHECKSUM_OFF, 3508251478Sdelphij l2arc_read_done, cb, priority, 3509251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 3510251478Sdelphij ZIO_FLAG_CANFAIL | 3511251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 3512251478Sdelphij ZIO_FLAG_DONT_RETRY, B_FALSE); 3513251478Sdelphij } 3514185029Spjd DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 3515185029Spjd zio_t *, rzio); 3516258389Savg ARCSTAT_INCR(arcstat_l2_read_bytes, b_asize); 3517185029Spjd 3518185029Spjd if (*arc_flags & ARC_NOWAIT) { 3519185029Spjd zio_nowait(rzio); 3520185029Spjd return (0); 3521185029Spjd } 3522185029Spjd 3523185029Spjd ASSERT(*arc_flags & ARC_WAIT); 3524185029Spjd if (zio_wait(rzio) == 0) 3525185029Spjd return (0); 3526185029Spjd 3527185029Spjd /* l2arc read error; goto zio_read() */ 3528185029Spjd } else { 3529185029Spjd DTRACE_PROBE1(l2arc__miss, 3530185029Spjd arc_buf_hdr_t *, hdr); 3531185029Spjd ARCSTAT_BUMP(arcstat_l2_misses); 3532185029Spjd if (HDR_L2_WRITING(hdr)) 3533185029Spjd ARCSTAT_BUMP(arcstat_l2_rw_clash); 3534185029Spjd spa_config_exit(spa, SCL_L2ARC, vd); 3535185029Spjd } 3536208373Smm } else { 3537208373Smm if (vd != NULL) 3538208373Smm spa_config_exit(spa, SCL_L2ARC, vd); 3539208373Smm if (l2arc_ndev != 0) { 3540208373Smm DTRACE_PROBE1(l2arc__miss, 3541208373Smm arc_buf_hdr_t *, hdr); 3542208373Smm ARCSTAT_BUMP(arcstat_l2_misses); 3543208373Smm } 3544185029Spjd } 3545185029Spjd 3546168404Spjd rzio = zio_read(pio, spa, bp, buf->b_data, size, 3547185029Spjd arc_read_done, buf, priority, zio_flags, zb); 3548168404Spjd 3549168404Spjd if (*arc_flags & ARC_WAIT) 3550168404Spjd return (zio_wait(rzio)); 3551168404Spjd 3552168404Spjd ASSERT(*arc_flags & ARC_NOWAIT); 3553168404Spjd zio_nowait(rzio); 3554168404Spjd } 3555168404Spjd return (0); 3556168404Spjd} 3557168404Spjd 3558168404Spjdvoid 3559168404Spjdarc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 3560168404Spjd{ 3561168404Spjd ASSERT(buf->b_hdr != NULL); 3562168404Spjd ASSERT(buf->b_hdr->b_state != arc_anon); 3563168404Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 3564219089Spjd ASSERT(buf->b_efunc == NULL); 3565219089Spjd ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 3566219089Spjd 3567168404Spjd buf->b_efunc = func; 3568168404Spjd buf->b_private = private; 3569168404Spjd} 3570168404Spjd 3571168404Spjd/* 3572251520Sdelphij * Notify the arc that a block was freed, and thus will never be used again. 3573251520Sdelphij */ 3574251520Sdelphijvoid 3575251520Sdelphijarc_freed(spa_t *spa, const blkptr_t *bp) 3576251520Sdelphij{ 3577251520Sdelphij arc_buf_hdr_t *hdr; 3578251520Sdelphij kmutex_t *hash_lock; 3579251520Sdelphij uint64_t guid = spa_load_guid(spa); 3580251520Sdelphij 3581268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp)); 3582268075Sdelphij 3583268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 3584251520Sdelphij if (hdr == NULL) 3585251520Sdelphij return; 3586251520Sdelphij if (HDR_BUF_AVAILABLE(hdr)) { 3587251520Sdelphij arc_buf_t *buf = hdr->b_buf; 3588251520Sdelphij add_reference(hdr, hash_lock, FTAG); 3589251520Sdelphij hdr->b_flags &= ~ARC_BUF_AVAILABLE; 3590251520Sdelphij mutex_exit(hash_lock); 3591251520Sdelphij 3592251520Sdelphij arc_release(buf, FTAG); 3593251520Sdelphij (void) arc_buf_remove_ref(buf, FTAG); 3594251520Sdelphij } else { 3595251520Sdelphij mutex_exit(hash_lock); 3596251520Sdelphij } 3597251520Sdelphij 3598251520Sdelphij} 3599251520Sdelphij 3600251520Sdelphij/* 3601268858Sdelphij * Clear the user eviction callback set by arc_set_callback(), first calling 3602268858Sdelphij * it if it exists. Because the presence of a callback keeps an arc_buf cached 3603268858Sdelphij * clearing the callback may result in the arc_buf being destroyed. However, 3604268858Sdelphij * it will not result in the *last* arc_buf being destroyed, hence the data 3605268858Sdelphij * will remain cached in the ARC. We make a copy of the arc buffer here so 3606268858Sdelphij * that we can process the callback without holding any locks. 3607268858Sdelphij * 3608268858Sdelphij * It's possible that the callback is already in the process of being cleared 3609268858Sdelphij * by another thread. In this case we can not clear the callback. 3610268858Sdelphij * 3611268858Sdelphij * Returns B_TRUE if the callback was successfully called and cleared. 3612168404Spjd */ 3613268858Sdelphijboolean_t 3614268858Sdelphijarc_clear_callback(arc_buf_t *buf) 3615168404Spjd{ 3616168404Spjd arc_buf_hdr_t *hdr; 3617168404Spjd kmutex_t *hash_lock; 3618268858Sdelphij arc_evict_func_t *efunc = buf->b_efunc; 3619268858Sdelphij void *private = buf->b_private; 3620205231Skmacy list_t *list, *evicted_list; 3621205231Skmacy kmutex_t *lock, *evicted_lock; 3622206796Spjd 3623219089Spjd mutex_enter(&buf->b_evict_lock); 3624168404Spjd hdr = buf->b_hdr; 3625168404Spjd if (hdr == NULL) { 3626168404Spjd /* 3627168404Spjd * We are in arc_do_user_evicts(). 3628168404Spjd */ 3629168404Spjd ASSERT(buf->b_data == NULL); 3630219089Spjd mutex_exit(&buf->b_evict_lock); 3631268858Sdelphij return (B_FALSE); 3632185029Spjd } else if (buf->b_data == NULL) { 3633185029Spjd /* 3634185029Spjd * We are on the eviction list; process this buffer now 3635185029Spjd * but let arc_do_user_evicts() do the reaping. 3636185029Spjd */ 3637185029Spjd buf->b_efunc = NULL; 3638219089Spjd mutex_exit(&buf->b_evict_lock); 3639268858Sdelphij VERIFY0(efunc(private)); 3640268858Sdelphij return (B_TRUE); 3641168404Spjd } 3642168404Spjd hash_lock = HDR_LOCK(hdr); 3643168404Spjd mutex_enter(hash_lock); 3644219089Spjd hdr = buf->b_hdr; 3645219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3646168404Spjd 3647168404Spjd ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 3648168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3649168404Spjd 3650268858Sdelphij buf->b_efunc = NULL; 3651268858Sdelphij buf->b_private = NULL; 3652168404Spjd 3653268858Sdelphij if (hdr->b_datacnt > 1) { 3654268858Sdelphij mutex_exit(&buf->b_evict_lock); 3655268858Sdelphij arc_buf_destroy(buf, FALSE, TRUE); 3656268858Sdelphij } else { 3657268858Sdelphij ASSERT(buf == hdr->b_buf); 3658268858Sdelphij hdr->b_flags |= ARC_BUF_AVAILABLE; 3659268858Sdelphij mutex_exit(&buf->b_evict_lock); 3660268858Sdelphij } 3661168404Spjd 3662168404Spjd mutex_exit(hash_lock); 3663268858Sdelphij VERIFY0(efunc(private)); 3664268858Sdelphij return (B_TRUE); 3665168404Spjd} 3666168404Spjd 3667168404Spjd/* 3668251629Sdelphij * Release this buffer from the cache, making it an anonymous buffer. This 3669251629Sdelphij * must be done after a read and prior to modifying the buffer contents. 3670168404Spjd * If the buffer has more than one reference, we must make 3671185029Spjd * a new hdr for the buffer. 3672168404Spjd */ 3673168404Spjdvoid 3674168404Spjdarc_release(arc_buf_t *buf, void *tag) 3675168404Spjd{ 3676185029Spjd arc_buf_hdr_t *hdr; 3677219089Spjd kmutex_t *hash_lock = NULL; 3678185029Spjd l2arc_buf_hdr_t *l2hdr; 3679185029Spjd uint64_t buf_size; 3680168404Spjd 3681219089Spjd /* 3682219089Spjd * It would be nice to assert that if it's DMU metadata (level > 3683219089Spjd * 0 || it's the dnode file), then it must be syncing context. 3684219089Spjd * But we don't know that information at this level. 3685219089Spjd */ 3686219089Spjd 3687219089Spjd mutex_enter(&buf->b_evict_lock); 3688185029Spjd hdr = buf->b_hdr; 3689185029Spjd 3690168404Spjd /* this buffer is not on any list */ 3691168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) > 0); 3692168404Spjd 3693168404Spjd if (hdr->b_state == arc_anon) { 3694168404Spjd /* this buffer is already released */ 3695168404Spjd ASSERT(buf->b_efunc == NULL); 3696208373Smm } else { 3697208373Smm hash_lock = HDR_LOCK(hdr); 3698208373Smm mutex_enter(hash_lock); 3699219089Spjd hdr = buf->b_hdr; 3700219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3701168404Spjd } 3702168404Spjd 3703185029Spjd l2hdr = hdr->b_l2hdr; 3704185029Spjd if (l2hdr) { 3705185029Spjd mutex_enter(&l2arc_buflist_mtx); 3706274172Savg arc_buf_l2_cdata_free(hdr); 3707185029Spjd hdr->b_l2hdr = NULL; 3708258388Savg list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 3709185029Spjd } 3710247187Smm buf_size = hdr->b_size; 3711185029Spjd 3712168404Spjd /* 3713168404Spjd * Do we have more than one buf? 3714168404Spjd */ 3715185029Spjd if (hdr->b_datacnt > 1) { 3716168404Spjd arc_buf_hdr_t *nhdr; 3717168404Spjd arc_buf_t **bufp; 3718168404Spjd uint64_t blksz = hdr->b_size; 3719209962Smm uint64_t spa = hdr->b_spa; 3720168404Spjd arc_buf_contents_t type = hdr->b_type; 3721185029Spjd uint32_t flags = hdr->b_flags; 3722168404Spjd 3723185029Spjd ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3724168404Spjd /* 3725219089Spjd * Pull the data off of this hdr and attach it to 3726219089Spjd * a new anonymous hdr. 3727168404Spjd */ 3728168404Spjd (void) remove_reference(hdr, hash_lock, tag); 3729168404Spjd bufp = &hdr->b_buf; 3730168404Spjd while (*bufp != buf) 3731168404Spjd bufp = &(*bufp)->b_next; 3732219089Spjd *bufp = buf->b_next; 3733168404Spjd buf->b_next = NULL; 3734168404Spjd 3735168404Spjd ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 3736168404Spjd atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 3737168404Spjd if (refcount_is_zero(&hdr->b_refcnt)) { 3738185029Spjd uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 3739185029Spjd ASSERT3U(*size, >=, hdr->b_size); 3740185029Spjd atomic_add_64(size, -hdr->b_size); 3741168404Spjd } 3742242845Sdelphij 3743242845Sdelphij /* 3744242845Sdelphij * We're releasing a duplicate user data buffer, update 3745242845Sdelphij * our statistics accordingly. 3746242845Sdelphij */ 3747242845Sdelphij if (hdr->b_type == ARC_BUFC_DATA) { 3748242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 3749242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, 3750242845Sdelphij -hdr->b_size); 3751242845Sdelphij } 3752168404Spjd hdr->b_datacnt -= 1; 3753168404Spjd arc_cksum_verify(buf); 3754240133Smm#ifdef illumos 3755240133Smm arc_buf_unwatch(buf); 3756240133Smm#endif /* illumos */ 3757168404Spjd 3758168404Spjd mutex_exit(hash_lock); 3759168404Spjd 3760185029Spjd nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3761168404Spjd nhdr->b_size = blksz; 3762168404Spjd nhdr->b_spa = spa; 3763168404Spjd nhdr->b_type = type; 3764168404Spjd nhdr->b_buf = buf; 3765168404Spjd nhdr->b_state = arc_anon; 3766168404Spjd nhdr->b_arc_access = 0; 3767185029Spjd nhdr->b_flags = flags & ARC_L2_WRITING; 3768185029Spjd nhdr->b_l2hdr = NULL; 3769168404Spjd nhdr->b_datacnt = 1; 3770168404Spjd nhdr->b_freeze_cksum = NULL; 3771168404Spjd (void) refcount_add(&nhdr->b_refcnt, tag); 3772168404Spjd buf->b_hdr = nhdr; 3773219089Spjd mutex_exit(&buf->b_evict_lock); 3774168404Spjd atomic_add_64(&arc_anon->arcs_size, blksz); 3775168404Spjd } else { 3776219089Spjd mutex_exit(&buf->b_evict_lock); 3777168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3778168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 3779168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3780219089Spjd if (hdr->b_state != arc_anon) 3781219089Spjd arc_change_state(arc_anon, hdr, hash_lock); 3782168404Spjd hdr->b_arc_access = 0; 3783219089Spjd if (hash_lock) 3784219089Spjd mutex_exit(hash_lock); 3785185029Spjd 3786219089Spjd buf_discard_identity(hdr); 3787168404Spjd arc_buf_thaw(buf); 3788168404Spjd } 3789168404Spjd buf->b_efunc = NULL; 3790168404Spjd buf->b_private = NULL; 3791185029Spjd 3792185029Spjd if (l2hdr) { 3793251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); 3794268085Sdelphij vdev_space_update(l2hdr->b_dev->l2ad_vdev, 3795268085Sdelphij -l2hdr->b_asize, 0, 0); 3796248572Ssmh trim_map_free(l2hdr->b_dev->l2ad_vdev, l2hdr->b_daddr, 3797248574Ssmh hdr->b_size, 0); 3798185029Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 3799185029Spjd ARCSTAT_INCR(arcstat_l2_size, -buf_size); 3800185029Spjd mutex_exit(&l2arc_buflist_mtx); 3801185029Spjd } 3802168404Spjd} 3803168404Spjd 3804168404Spjdint 3805168404Spjdarc_released(arc_buf_t *buf) 3806168404Spjd{ 3807185029Spjd int released; 3808185029Spjd 3809219089Spjd mutex_enter(&buf->b_evict_lock); 3810185029Spjd released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 3811219089Spjd mutex_exit(&buf->b_evict_lock); 3812185029Spjd return (released); 3813168404Spjd} 3814168404Spjd 3815168404Spjd#ifdef ZFS_DEBUG 3816168404Spjdint 3817168404Spjdarc_referenced(arc_buf_t *buf) 3818168404Spjd{ 3819185029Spjd int referenced; 3820185029Spjd 3821219089Spjd mutex_enter(&buf->b_evict_lock); 3822185029Spjd referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 3823219089Spjd mutex_exit(&buf->b_evict_lock); 3824185029Spjd return (referenced); 3825168404Spjd} 3826168404Spjd#endif 3827168404Spjd 3828168404Spjdstatic void 3829168404Spjdarc_write_ready(zio_t *zio) 3830168404Spjd{ 3831168404Spjd arc_write_callback_t *callback = zio->io_private; 3832168404Spjd arc_buf_t *buf = callback->awcb_buf; 3833185029Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3834168404Spjd 3835185029Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 3836185029Spjd callback->awcb_ready(zio, buf, callback->awcb_private); 3837185029Spjd 3838185029Spjd /* 3839185029Spjd * If the IO is already in progress, then this is a re-write 3840185029Spjd * attempt, so we need to thaw and re-compute the cksum. 3841185029Spjd * It is the responsibility of the callback to handle the 3842185029Spjd * accounting for any re-write attempt. 3843185029Spjd */ 3844185029Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3845185029Spjd mutex_enter(&hdr->b_freeze_lock); 3846185029Spjd if (hdr->b_freeze_cksum != NULL) { 3847185029Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 3848185029Spjd hdr->b_freeze_cksum = NULL; 3849185029Spjd } 3850185029Spjd mutex_exit(&hdr->b_freeze_lock); 3851168404Spjd } 3852185029Spjd arc_cksum_compute(buf, B_FALSE); 3853185029Spjd hdr->b_flags |= ARC_IO_IN_PROGRESS; 3854168404Spjd} 3855168404Spjd 3856258632Savg/* 3857258632Savg * The SPA calls this callback for each physical write that happens on behalf 3858258632Savg * of a logical write. See the comment in dbuf_write_physdone() for details. 3859258632Savg */ 3860168404Spjdstatic void 3861258632Savgarc_write_physdone(zio_t *zio) 3862258632Savg{ 3863258632Savg arc_write_callback_t *cb = zio->io_private; 3864258632Savg if (cb->awcb_physdone != NULL) 3865258632Savg cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 3866258632Savg} 3867258632Savg 3868258632Savgstatic void 3869168404Spjdarc_write_done(zio_t *zio) 3870168404Spjd{ 3871168404Spjd arc_write_callback_t *callback = zio->io_private; 3872168404Spjd arc_buf_t *buf = callback->awcb_buf; 3873168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3874168404Spjd 3875219089Spjd ASSERT(hdr->b_acb == NULL); 3876168404Spjd 3877219089Spjd if (zio->io_error == 0) { 3878268075Sdelphij if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 3879260150Sdelphij buf_discard_identity(hdr); 3880260150Sdelphij } else { 3881260150Sdelphij hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3882260150Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 3883260150Sdelphij hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 3884260150Sdelphij } 3885219089Spjd } else { 3886219089Spjd ASSERT(BUF_EMPTY(hdr)); 3887219089Spjd } 3888219089Spjd 3889168404Spjd /* 3890268075Sdelphij * If the block to be written was all-zero or compressed enough to be 3891268075Sdelphij * embedded in the BP, no write was performed so there will be no 3892268075Sdelphij * dva/birth/checksum. The buffer must therefore remain anonymous 3893268075Sdelphij * (and uncached). 3894168404Spjd */ 3895168404Spjd if (!BUF_EMPTY(hdr)) { 3896168404Spjd arc_buf_hdr_t *exists; 3897168404Spjd kmutex_t *hash_lock; 3898168404Spjd 3899219089Spjd ASSERT(zio->io_error == 0); 3900219089Spjd 3901168404Spjd arc_cksum_verify(buf); 3902168404Spjd 3903168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 3904168404Spjd if (exists) { 3905168404Spjd /* 3906168404Spjd * This can only happen if we overwrite for 3907168404Spjd * sync-to-convergence, because we remove 3908168404Spjd * buffers from the hash table when we arc_free(). 3909168404Spjd */ 3910219089Spjd if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 3911219089Spjd if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3912219089Spjd panic("bad overwrite, hdr=%p exists=%p", 3913219089Spjd (void *)hdr, (void *)exists); 3914219089Spjd ASSERT(refcount_is_zero(&exists->b_refcnt)); 3915219089Spjd arc_change_state(arc_anon, exists, hash_lock); 3916219089Spjd mutex_exit(hash_lock); 3917219089Spjd arc_hdr_destroy(exists); 3918219089Spjd exists = buf_hash_insert(hdr, &hash_lock); 3919219089Spjd ASSERT3P(exists, ==, NULL); 3920243524Smm } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 3921243524Smm /* nopwrite */ 3922243524Smm ASSERT(zio->io_prop.zp_nopwrite); 3923243524Smm if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3924243524Smm panic("bad nopwrite, hdr=%p exists=%p", 3925243524Smm (void *)hdr, (void *)exists); 3926219089Spjd } else { 3927219089Spjd /* Dedup */ 3928219089Spjd ASSERT(hdr->b_datacnt == 1); 3929219089Spjd ASSERT(hdr->b_state == arc_anon); 3930219089Spjd ASSERT(BP_GET_DEDUP(zio->io_bp)); 3931219089Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 3932219089Spjd } 3933168404Spjd } 3934168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3935185029Spjd /* if it's not anon, we are doing a scrub */ 3936219089Spjd if (!exists && hdr->b_state == arc_anon) 3937185029Spjd arc_access(hdr, hash_lock); 3938168404Spjd mutex_exit(hash_lock); 3939168404Spjd } else { 3940168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3941168404Spjd } 3942168404Spjd 3943219089Spjd ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 3944219089Spjd callback->awcb_done(zio, buf, callback->awcb_private); 3945168404Spjd 3946168404Spjd kmem_free(callback, sizeof (arc_write_callback_t)); 3947168404Spjd} 3948168404Spjd 3949168404Spjdzio_t * 3950219089Spjdarc_write(zio_t *pio, spa_t *spa, uint64_t txg, 3951251478Sdelphij blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress, 3952258632Savg const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone, 3953258632Savg arc_done_func_t *done, void *private, zio_priority_t priority, 3954268123Sdelphij int zio_flags, const zbookmark_phys_t *zb) 3955168404Spjd{ 3956168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3957168404Spjd arc_write_callback_t *callback; 3958185029Spjd zio_t *zio; 3959168404Spjd 3960185029Spjd ASSERT(ready != NULL); 3961219089Spjd ASSERT(done != NULL); 3962168404Spjd ASSERT(!HDR_IO_ERROR(hdr)); 3963168404Spjd ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 3964219089Spjd ASSERT(hdr->b_acb == NULL); 3965185029Spjd if (l2arc) 3966185029Spjd hdr->b_flags |= ARC_L2CACHE; 3967251478Sdelphij if (l2arc_compress) 3968251478Sdelphij hdr->b_flags |= ARC_L2COMPRESS; 3969168404Spjd callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 3970168404Spjd callback->awcb_ready = ready; 3971258632Savg callback->awcb_physdone = physdone; 3972168404Spjd callback->awcb_done = done; 3973168404Spjd callback->awcb_private = private; 3974168404Spjd callback->awcb_buf = buf; 3975168404Spjd 3976219089Spjd zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 3977258632Savg arc_write_ready, arc_write_physdone, arc_write_done, callback, 3978258632Savg priority, zio_flags, zb); 3979185029Spjd 3980168404Spjd return (zio); 3981168404Spjd} 3982168404Spjd 3983185029Spjdstatic int 3984258632Savgarc_memory_throttle(uint64_t reserve, uint64_t txg) 3985185029Spjd{ 3986185029Spjd#ifdef _KERNEL 3987272483Ssmh uint64_t available_memory = ptob(freemem); 3988185029Spjd static uint64_t page_load = 0; 3989185029Spjd static uint64_t last_txg = 0; 3990185029Spjd 3991272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 3992185029Spjd available_memory = 3993272483Ssmh MIN(available_memory, ptob(vmem_size(heap_arena, VMEM_FREE))); 3994185029Spjd#endif 3995258632Savg 3996272483Ssmh if (freemem > (uint64_t)physmem * arc_lotsfree_percent / 100) 3997185029Spjd return (0); 3998185029Spjd 3999185029Spjd if (txg > last_txg) { 4000185029Spjd last_txg = txg; 4001185029Spjd page_load = 0; 4002185029Spjd } 4003185029Spjd /* 4004185029Spjd * If we are in pageout, we know that memory is already tight, 4005185029Spjd * the arc is already going to be evicting, so we just want to 4006185029Spjd * continue to let page writes occur as quickly as possible. 4007185029Spjd */ 4008185029Spjd if (curproc == pageproc) { 4009272483Ssmh if (page_load > MAX(ptob(minfree), available_memory) / 4) 4010249195Smm return (SET_ERROR(ERESTART)); 4011185029Spjd /* Note: reserve is inflated, so we deflate */ 4012185029Spjd page_load += reserve / 8; 4013185029Spjd return (0); 4014185029Spjd } else if (page_load > 0 && arc_reclaim_needed()) { 4015185029Spjd /* memory is low, delay before restarting */ 4016185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 4017249195Smm return (SET_ERROR(EAGAIN)); 4018185029Spjd } 4019185029Spjd page_load = 0; 4020185029Spjd#endif 4021185029Spjd return (0); 4022185029Spjd} 4023185029Spjd 4024168404Spjdvoid 4025185029Spjdarc_tempreserve_clear(uint64_t reserve) 4026168404Spjd{ 4027185029Spjd atomic_add_64(&arc_tempreserve, -reserve); 4028168404Spjd ASSERT((int64_t)arc_tempreserve >= 0); 4029168404Spjd} 4030168404Spjd 4031168404Spjdint 4032185029Spjdarc_tempreserve_space(uint64_t reserve, uint64_t txg) 4033168404Spjd{ 4034185029Spjd int error; 4035209962Smm uint64_t anon_size; 4036185029Spjd 4037272483Ssmh if (reserve > arc_c/4 && !arc_no_grow) { 4038185029Spjd arc_c = MIN(arc_c_max, reserve * 4); 4039272483Ssmh DTRACE_PROBE1(arc__set_reserve, uint64_t, arc_c); 4040272483Ssmh } 4041185029Spjd if (reserve > arc_c) 4042249195Smm return (SET_ERROR(ENOMEM)); 4043168404Spjd 4044168404Spjd /* 4045209962Smm * Don't count loaned bufs as in flight dirty data to prevent long 4046209962Smm * network delays from blocking transactions that are ready to be 4047209962Smm * assigned to a txg. 4048209962Smm */ 4049209962Smm anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 4050209962Smm 4051209962Smm /* 4052185029Spjd * Writes will, almost always, require additional memory allocations 4053251631Sdelphij * in order to compress/encrypt/etc the data. We therefore need to 4054185029Spjd * make sure that there is sufficient available memory for this. 4055185029Spjd */ 4056258632Savg error = arc_memory_throttle(reserve, txg); 4057258632Savg if (error != 0) 4058185029Spjd return (error); 4059185029Spjd 4060185029Spjd /* 4061168404Spjd * Throttle writes when the amount of dirty data in the cache 4062168404Spjd * gets too large. We try to keep the cache less than half full 4063168404Spjd * of dirty blocks so that our sync times don't grow too large. 4064168404Spjd * Note: if two requests come in concurrently, we might let them 4065168404Spjd * both succeed, when one of them should fail. Not a huge deal. 4066168404Spjd */ 4067209962Smm 4068209962Smm if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 4069209962Smm anon_size > arc_c / 4) { 4070185029Spjd dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 4071185029Spjd "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 4072185029Spjd arc_tempreserve>>10, 4073185029Spjd arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 4074185029Spjd arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 4075185029Spjd reserve>>10, arc_c>>10); 4076249195Smm return (SET_ERROR(ERESTART)); 4077168404Spjd } 4078185029Spjd atomic_add_64(&arc_tempreserve, reserve); 4079168404Spjd return (0); 4080168404Spjd} 4081168404Spjd 4082168582Spjdstatic kmutex_t arc_lowmem_lock; 4083168404Spjd#ifdef _KERNEL 4084168566Spjdstatic eventhandler_tag arc_event_lowmem = NULL; 4085168404Spjd 4086168404Spjdstatic void 4087168566Spjdarc_lowmem(void *arg __unused, int howto __unused) 4088168404Spjd{ 4089168404Spjd 4090168566Spjd /* Serialize access via arc_lowmem_lock. */ 4091168566Spjd mutex_enter(&arc_lowmem_lock); 4092219089Spjd mutex_enter(&arc_reclaim_thr_lock); 4093185029Spjd needfree = 1; 4094272483Ssmh DTRACE_PROBE(arc__needfree); 4095168404Spjd cv_signal(&arc_reclaim_thr_cv); 4096241773Savg 4097241773Savg /* 4098241773Savg * It is unsafe to block here in arbitrary threads, because we can come 4099241773Savg * here from ARC itself and may hold ARC locks and thus risk a deadlock 4100241773Savg * with ARC reclaim thread. 4101241773Savg */ 4102241773Savg if (curproc == pageproc) { 4103241773Savg while (needfree) 4104241773Savg msleep(&needfree, &arc_reclaim_thr_lock, 0, "zfs:lowmem", 0); 4105241773Savg } 4106219089Spjd mutex_exit(&arc_reclaim_thr_lock); 4107168566Spjd mutex_exit(&arc_lowmem_lock); 4108168404Spjd} 4109168404Spjd#endif 4110168404Spjd 4111168404Spjdvoid 4112168404Spjdarc_init(void) 4113168404Spjd{ 4114219089Spjd int i, prefetch_tunable_set = 0; 4115205231Skmacy 4116168404Spjd mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 4117168404Spjd cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 4118168566Spjd mutex_init(&arc_lowmem_lock, NULL, MUTEX_DEFAULT, NULL); 4119168404Spjd 4120168404Spjd /* Convert seconds to clock ticks */ 4121168404Spjd arc_min_prefetch_lifespan = 1 * hz; 4122168404Spjd 4123168404Spjd /* Start out with 1/8 of all memory */ 4124168566Spjd arc_c = kmem_size() / 8; 4125219089Spjd 4126219089Spjd#ifdef sun 4127192360Skmacy#ifdef _KERNEL 4128192360Skmacy /* 4129192360Skmacy * On architectures where the physical memory can be larger 4130192360Skmacy * than the addressable space (intel in 32-bit mode), we may 4131192360Skmacy * need to limit the cache to 1/8 of VM size. 4132192360Skmacy */ 4133192360Skmacy arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 4134192360Skmacy#endif 4135219089Spjd#endif /* sun */ 4136168566Spjd /* set min cache to 1/32 of all memory, or 16MB, whichever is more */ 4137168566Spjd arc_c_min = MAX(arc_c / 4, 64<<18); 4138168566Spjd /* set max to 1/2 of all memory, or all but 1GB, whichever is more */ 4139168404Spjd if (arc_c * 8 >= 1<<30) 4140168404Spjd arc_c_max = (arc_c * 8) - (1<<30); 4141168404Spjd else 4142168404Spjd arc_c_max = arc_c_min; 4143175633Spjd arc_c_max = MAX(arc_c * 5, arc_c_max); 4144219089Spjd 4145168481Spjd#ifdef _KERNEL 4146168404Spjd /* 4147168404Spjd * Allow the tunables to override our calculations if they are 4148168566Spjd * reasonable (ie. over 16MB) 4149168404Spjd */ 4150219089Spjd if (zfs_arc_max > 64<<18 && zfs_arc_max < kmem_size()) 4151168404Spjd arc_c_max = zfs_arc_max; 4152219089Spjd if (zfs_arc_min > 64<<18 && zfs_arc_min <= arc_c_max) 4153168404Spjd arc_c_min = zfs_arc_min; 4154168481Spjd#endif 4155219089Spjd 4156168404Spjd arc_c = arc_c_max; 4157168404Spjd arc_p = (arc_c >> 1); 4158168404Spjd 4159185029Spjd /* limit meta-data to 1/4 of the arc capacity */ 4160185029Spjd arc_meta_limit = arc_c_max / 4; 4161185029Spjd 4162185029Spjd /* Allow the tunable to override if it is reasonable */ 4163185029Spjd if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 4164185029Spjd arc_meta_limit = zfs_arc_meta_limit; 4165185029Spjd 4166185029Spjd if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 4167185029Spjd arc_c_min = arc_meta_limit / 2; 4168185029Spjd 4169208373Smm if (zfs_arc_grow_retry > 0) 4170208373Smm arc_grow_retry = zfs_arc_grow_retry; 4171208373Smm 4172208373Smm if (zfs_arc_shrink_shift > 0) 4173208373Smm arc_shrink_shift = zfs_arc_shrink_shift; 4174208373Smm 4175208373Smm if (zfs_arc_p_min_shift > 0) 4176208373Smm arc_p_min_shift = zfs_arc_p_min_shift; 4177208373Smm 4178168404Spjd /* if kmem_flags are set, lets try to use less memory */ 4179168404Spjd if (kmem_debugging()) 4180168404Spjd arc_c = arc_c / 2; 4181168404Spjd if (arc_c < arc_c_min) 4182168404Spjd arc_c = arc_c_min; 4183168404Spjd 4184168473Spjd zfs_arc_min = arc_c_min; 4185168473Spjd zfs_arc_max = arc_c_max; 4186168473Spjd 4187168404Spjd arc_anon = &ARC_anon; 4188168404Spjd arc_mru = &ARC_mru; 4189168404Spjd arc_mru_ghost = &ARC_mru_ghost; 4190168404Spjd arc_mfu = &ARC_mfu; 4191168404Spjd arc_mfu_ghost = &ARC_mfu_ghost; 4192185029Spjd arc_l2c_only = &ARC_l2c_only; 4193168404Spjd arc_size = 0; 4194168404Spjd 4195205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 4196205231Skmacy mutex_init(&arc_anon->arcs_locks[i].arcs_lock, 4197205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4198205231Skmacy mutex_init(&arc_mru->arcs_locks[i].arcs_lock, 4199205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4200205231Skmacy mutex_init(&arc_mru_ghost->arcs_locks[i].arcs_lock, 4201205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4202205231Skmacy mutex_init(&arc_mfu->arcs_locks[i].arcs_lock, 4203205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4204205231Skmacy mutex_init(&arc_mfu_ghost->arcs_locks[i].arcs_lock, 4205205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4206205231Skmacy mutex_init(&arc_l2c_only->arcs_locks[i].arcs_lock, 4207205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4208206796Spjd 4209205231Skmacy list_create(&arc_mru->arcs_lists[i], 4210205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4211205231Skmacy list_create(&arc_mru_ghost->arcs_lists[i], 4212205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4213205231Skmacy list_create(&arc_mfu->arcs_lists[i], 4214205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4215205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 4216205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4217205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 4218205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4219205231Skmacy list_create(&arc_l2c_only->arcs_lists[i], 4220205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4221205231Skmacy } 4222168404Spjd 4223168404Spjd buf_init(); 4224168404Spjd 4225168404Spjd arc_thread_exit = 0; 4226168404Spjd arc_eviction_list = NULL; 4227168404Spjd mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 4228168404Spjd bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 4229168404Spjd 4230168404Spjd arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 4231168404Spjd sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 4232168404Spjd 4233168404Spjd if (arc_ksp != NULL) { 4234168404Spjd arc_ksp->ks_data = &arc_stats; 4235168404Spjd kstat_install(arc_ksp); 4236168404Spjd } 4237168404Spjd 4238168404Spjd (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 4239168404Spjd TS_RUN, minclsyspri); 4240168404Spjd 4241168404Spjd#ifdef _KERNEL 4242168566Spjd arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 4243168404Spjd EVENTHANDLER_PRI_FIRST); 4244168404Spjd#endif 4245168404Spjd 4246168404Spjd arc_dead = FALSE; 4247185029Spjd arc_warm = B_FALSE; 4248168566Spjd 4249258632Savg /* 4250258632Savg * Calculate maximum amount of dirty data per pool. 4251258632Savg * 4252258632Savg * If it has been set by /etc/system, take that. 4253258632Savg * Otherwise, use a percentage of physical memory defined by 4254258632Savg * zfs_dirty_data_max_percent (default 10%) with a cap at 4255258632Savg * zfs_dirty_data_max_max (default 4GB). 4256258632Savg */ 4257258632Savg if (zfs_dirty_data_max == 0) { 4258258632Savg zfs_dirty_data_max = ptob(physmem) * 4259258632Savg zfs_dirty_data_max_percent / 100; 4260258632Savg zfs_dirty_data_max = MIN(zfs_dirty_data_max, 4261258632Savg zfs_dirty_data_max_max); 4262258632Savg } 4263185029Spjd 4264168566Spjd#ifdef _KERNEL 4265194043Skmacy if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 4266193953Skmacy prefetch_tunable_set = 1; 4267206796Spjd 4268193878Skmacy#ifdef __i386__ 4269193953Skmacy if (prefetch_tunable_set == 0) { 4270196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 4271196863Strasz "-- to enable,\n"); 4272196863Strasz printf(" add \"vfs.zfs.prefetch_disable=0\" " 4273196863Strasz "to /boot/loader.conf.\n"); 4274219089Spjd zfs_prefetch_disable = 1; 4275193878Skmacy } 4276206796Spjd#else 4277193878Skmacy if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 4278193953Skmacy prefetch_tunable_set == 0) { 4279196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default if less " 4280196941Strasz "than 4GB of RAM is present;\n" 4281196863Strasz " to enable, add \"vfs.zfs.prefetch_disable=0\" " 4282196863Strasz "to /boot/loader.conf.\n"); 4283219089Spjd zfs_prefetch_disable = 1; 4284193878Skmacy } 4285206796Spjd#endif 4286175633Spjd /* Warn about ZFS memory and address space requirements. */ 4287168696Spjd if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 4288168987Sbmah printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 4289168987Sbmah "expect unstable behavior.\n"); 4290175633Spjd } 4291175633Spjd if (kmem_size() < 512 * (1 << 20)) { 4292173419Spjd printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 4293168987Sbmah "expect unstable behavior.\n"); 4294185029Spjd printf(" Consider tuning vm.kmem_size and " 4295173419Spjd "vm.kmem_size_max\n"); 4296185029Spjd printf(" in /boot/loader.conf.\n"); 4297168566Spjd } 4298168566Spjd#endif 4299168404Spjd} 4300168404Spjd 4301168404Spjdvoid 4302168404Spjdarc_fini(void) 4303168404Spjd{ 4304205231Skmacy int i; 4305206796Spjd 4306168404Spjd mutex_enter(&arc_reclaim_thr_lock); 4307168404Spjd arc_thread_exit = 1; 4308168404Spjd cv_signal(&arc_reclaim_thr_cv); 4309168404Spjd while (arc_thread_exit != 0) 4310168404Spjd cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 4311168404Spjd mutex_exit(&arc_reclaim_thr_lock); 4312168404Spjd 4313185029Spjd arc_flush(NULL); 4314168404Spjd 4315168404Spjd arc_dead = TRUE; 4316168404Spjd 4317168404Spjd if (arc_ksp != NULL) { 4318168404Spjd kstat_delete(arc_ksp); 4319168404Spjd arc_ksp = NULL; 4320168404Spjd } 4321168404Spjd 4322168404Spjd mutex_destroy(&arc_eviction_mtx); 4323168404Spjd mutex_destroy(&arc_reclaim_thr_lock); 4324168404Spjd cv_destroy(&arc_reclaim_thr_cv); 4325168404Spjd 4326205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 4327205231Skmacy list_destroy(&arc_mru->arcs_lists[i]); 4328205231Skmacy list_destroy(&arc_mru_ghost->arcs_lists[i]); 4329205231Skmacy list_destroy(&arc_mfu->arcs_lists[i]); 4330205231Skmacy list_destroy(&arc_mfu_ghost->arcs_lists[i]); 4331206795Spjd list_destroy(&arc_l2c_only->arcs_lists[i]); 4332168404Spjd 4333205231Skmacy mutex_destroy(&arc_anon->arcs_locks[i].arcs_lock); 4334205231Skmacy mutex_destroy(&arc_mru->arcs_locks[i].arcs_lock); 4335205231Skmacy mutex_destroy(&arc_mru_ghost->arcs_locks[i].arcs_lock); 4336205231Skmacy mutex_destroy(&arc_mfu->arcs_locks[i].arcs_lock); 4337205231Skmacy mutex_destroy(&arc_mfu_ghost->arcs_locks[i].arcs_lock); 4338206795Spjd mutex_destroy(&arc_l2c_only->arcs_locks[i].arcs_lock); 4339205231Skmacy } 4340206796Spjd 4341168404Spjd buf_fini(); 4342168404Spjd 4343209962Smm ASSERT(arc_loaned_bytes == 0); 4344209962Smm 4345168582Spjd mutex_destroy(&arc_lowmem_lock); 4346168404Spjd#ifdef _KERNEL 4347168566Spjd if (arc_event_lowmem != NULL) 4348168566Spjd EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 4349168404Spjd#endif 4350168404Spjd} 4351185029Spjd 4352185029Spjd/* 4353185029Spjd * Level 2 ARC 4354185029Spjd * 4355185029Spjd * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 4356185029Spjd * It uses dedicated storage devices to hold cached data, which are populated 4357185029Spjd * using large infrequent writes. The main role of this cache is to boost 4358185029Spjd * the performance of random read workloads. The intended L2ARC devices 4359185029Spjd * include short-stroked disks, solid state disks, and other media with 4360185029Spjd * substantially faster read latency than disk. 4361185029Spjd * 4362185029Spjd * +-----------------------+ 4363185029Spjd * | ARC | 4364185029Spjd * +-----------------------+ 4365185029Spjd * | ^ ^ 4366185029Spjd * | | | 4367185029Spjd * l2arc_feed_thread() arc_read() 4368185029Spjd * | | | 4369185029Spjd * | l2arc read | 4370185029Spjd * V | | 4371185029Spjd * +---------------+ | 4372185029Spjd * | L2ARC | | 4373185029Spjd * +---------------+ | 4374185029Spjd * | ^ | 4375185029Spjd * l2arc_write() | | 4376185029Spjd * | | | 4377185029Spjd * V | | 4378185029Spjd * +-------+ +-------+ 4379185029Spjd * | vdev | | vdev | 4380185029Spjd * | cache | | cache | 4381185029Spjd * +-------+ +-------+ 4382185029Spjd * +=========+ .-----. 4383185029Spjd * : L2ARC : |-_____-| 4384185029Spjd * : devices : | Disks | 4385185029Spjd * +=========+ `-_____-' 4386185029Spjd * 4387185029Spjd * Read requests are satisfied from the following sources, in order: 4388185029Spjd * 4389185029Spjd * 1) ARC 4390185029Spjd * 2) vdev cache of L2ARC devices 4391185029Spjd * 3) L2ARC devices 4392185029Spjd * 4) vdev cache of disks 4393185029Spjd * 5) disks 4394185029Spjd * 4395185029Spjd * Some L2ARC device types exhibit extremely slow write performance. 4396185029Spjd * To accommodate for this there are some significant differences between 4397185029Spjd * the L2ARC and traditional cache design: 4398185029Spjd * 4399185029Spjd * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 4400185029Spjd * the ARC behave as usual, freeing buffers and placing headers on ghost 4401185029Spjd * lists. The ARC does not send buffers to the L2ARC during eviction as 4402185029Spjd * this would add inflated write latencies for all ARC memory pressure. 4403185029Spjd * 4404185029Spjd * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 4405185029Spjd * It does this by periodically scanning buffers from the eviction-end of 4406185029Spjd * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 4407251478Sdelphij * not already there. It scans until a headroom of buffers is satisfied, 4408251478Sdelphij * which itself is a buffer for ARC eviction. If a compressible buffer is 4409251478Sdelphij * found during scanning and selected for writing to an L2ARC device, we 4410251478Sdelphij * temporarily boost scanning headroom during the next scan cycle to make 4411251478Sdelphij * sure we adapt to compression effects (which might significantly reduce 4412251478Sdelphij * the data volume we write to L2ARC). The thread that does this is 4413185029Spjd * l2arc_feed_thread(), illustrated below; example sizes are included to 4414185029Spjd * provide a better sense of ratio than this diagram: 4415185029Spjd * 4416185029Spjd * head --> tail 4417185029Spjd * +---------------------+----------+ 4418185029Spjd * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 4419185029Spjd * +---------------------+----------+ | o L2ARC eligible 4420185029Spjd * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 4421185029Spjd * +---------------------+----------+ | 4422185029Spjd * 15.9 Gbytes ^ 32 Mbytes | 4423185029Spjd * headroom | 4424185029Spjd * l2arc_feed_thread() 4425185029Spjd * | 4426185029Spjd * l2arc write hand <--[oooo]--' 4427185029Spjd * | 8 Mbyte 4428185029Spjd * | write max 4429185029Spjd * V 4430185029Spjd * +==============================+ 4431185029Spjd * L2ARC dev |####|#|###|###| |####| ... | 4432185029Spjd * +==============================+ 4433185029Spjd * 32 Gbytes 4434185029Spjd * 4435185029Spjd * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 4436185029Spjd * evicted, then the L2ARC has cached a buffer much sooner than it probably 4437185029Spjd * needed to, potentially wasting L2ARC device bandwidth and storage. It is 4438185029Spjd * safe to say that this is an uncommon case, since buffers at the end of 4439185029Spjd * the ARC lists have moved there due to inactivity. 4440185029Spjd * 4441185029Spjd * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 4442185029Spjd * then the L2ARC simply misses copying some buffers. This serves as a 4443185029Spjd * pressure valve to prevent heavy read workloads from both stalling the ARC 4444185029Spjd * with waits and clogging the L2ARC with writes. This also helps prevent 4445185029Spjd * the potential for the L2ARC to churn if it attempts to cache content too 4446185029Spjd * quickly, such as during backups of the entire pool. 4447185029Spjd * 4448185029Spjd * 5. After system boot and before the ARC has filled main memory, there are 4449185029Spjd * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 4450185029Spjd * lists can remain mostly static. Instead of searching from tail of these 4451185029Spjd * lists as pictured, the l2arc_feed_thread() will search from the list heads 4452185029Spjd * for eligible buffers, greatly increasing its chance of finding them. 4453185029Spjd * 4454185029Spjd * The L2ARC device write speed is also boosted during this time so that 4455185029Spjd * the L2ARC warms up faster. Since there have been no ARC evictions yet, 4456185029Spjd * there are no L2ARC reads, and no fear of degrading read performance 4457185029Spjd * through increased writes. 4458185029Spjd * 4459185029Spjd * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 4460185029Spjd * the vdev queue can aggregate them into larger and fewer writes. Each 4461185029Spjd * device is written to in a rotor fashion, sweeping writes through 4462185029Spjd * available space then repeating. 4463185029Spjd * 4464185029Spjd * 7. The L2ARC does not store dirty content. It never needs to flush 4465185029Spjd * write buffers back to disk based storage. 4466185029Spjd * 4467185029Spjd * 8. If an ARC buffer is written (and dirtied) which also exists in the 4468185029Spjd * L2ARC, the now stale L2ARC buffer is immediately dropped. 4469185029Spjd * 4470185029Spjd * The performance of the L2ARC can be tweaked by a number of tunables, which 4471185029Spjd * may be necessary for different workloads: 4472185029Spjd * 4473185029Spjd * l2arc_write_max max write bytes per interval 4474185029Spjd * l2arc_write_boost extra write bytes during device warmup 4475185029Spjd * l2arc_noprefetch skip caching prefetched buffers 4476185029Spjd * l2arc_headroom number of max device writes to precache 4477251478Sdelphij * l2arc_headroom_boost when we find compressed buffers during ARC 4478251478Sdelphij * scanning, we multiply headroom by this 4479251478Sdelphij * percentage factor for the next scan cycle, 4480251478Sdelphij * since more compressed buffers are likely to 4481251478Sdelphij * be present 4482185029Spjd * l2arc_feed_secs seconds between L2ARC writing 4483185029Spjd * 4484185029Spjd * Tunables may be removed or added as future performance improvements are 4485185029Spjd * integrated, and also may become zpool properties. 4486208373Smm * 4487208373Smm * There are three key functions that control how the L2ARC warms up: 4488208373Smm * 4489208373Smm * l2arc_write_eligible() check if a buffer is eligible to cache 4490208373Smm * l2arc_write_size() calculate how much to write 4491208373Smm * l2arc_write_interval() calculate sleep delay between writes 4492208373Smm * 4493208373Smm * These three functions determine what to write, how much, and how quickly 4494208373Smm * to send writes. 4495185029Spjd */ 4496185029Spjd 4497208373Smmstatic boolean_t 4498209962Smml2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 4499208373Smm{ 4500208373Smm /* 4501208373Smm * A buffer is *not* eligible for the L2ARC if it: 4502208373Smm * 1. belongs to a different spa. 4503208373Smm * 2. is already cached on the L2ARC. 4504208373Smm * 3. has an I/O in progress (it may be an incomplete read). 4505208373Smm * 4. is flagged not eligible (zfs property). 4506208373Smm */ 4507209962Smm if (ab->b_spa != spa_guid) { 4508208373Smm ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 4509208373Smm return (B_FALSE); 4510208373Smm } 4511208373Smm if (ab->b_l2hdr != NULL) { 4512208373Smm ARCSTAT_BUMP(arcstat_l2_write_in_l2); 4513208373Smm return (B_FALSE); 4514208373Smm } 4515208373Smm if (HDR_IO_IN_PROGRESS(ab)) { 4516208373Smm ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 4517208373Smm return (B_FALSE); 4518208373Smm } 4519208373Smm if (!HDR_L2CACHE(ab)) { 4520208373Smm ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 4521208373Smm return (B_FALSE); 4522208373Smm } 4523208373Smm 4524208373Smm return (B_TRUE); 4525208373Smm} 4526208373Smm 4527208373Smmstatic uint64_t 4528251478Sdelphijl2arc_write_size(void) 4529208373Smm{ 4530208373Smm uint64_t size; 4531208373Smm 4532251478Sdelphij /* 4533251478Sdelphij * Make sure our globals have meaningful values in case the user 4534251478Sdelphij * altered them. 4535251478Sdelphij */ 4536251478Sdelphij size = l2arc_write_max; 4537251478Sdelphij if (size == 0) { 4538251478Sdelphij cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 4539251478Sdelphij "be greater than zero, resetting it to the default (%d)", 4540251478Sdelphij L2ARC_WRITE_SIZE); 4541251478Sdelphij size = l2arc_write_max = L2ARC_WRITE_SIZE; 4542251478Sdelphij } 4543208373Smm 4544208373Smm if (arc_warm == B_FALSE) 4545251478Sdelphij size += l2arc_write_boost; 4546208373Smm 4547208373Smm return (size); 4548208373Smm 4549208373Smm} 4550208373Smm 4551208373Smmstatic clock_t 4552208373Smml2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 4553208373Smm{ 4554219089Spjd clock_t interval, next, now; 4555208373Smm 4556208373Smm /* 4557208373Smm * If the ARC lists are busy, increase our write rate; if the 4558208373Smm * lists are stale, idle back. This is achieved by checking 4559208373Smm * how much we previously wrote - if it was more than half of 4560208373Smm * what we wanted, schedule the next write much sooner. 4561208373Smm */ 4562208373Smm if (l2arc_feed_again && wrote > (wanted / 2)) 4563208373Smm interval = (hz * l2arc_feed_min_ms) / 1000; 4564208373Smm else 4565208373Smm interval = hz * l2arc_feed_secs; 4566208373Smm 4567219089Spjd now = ddi_get_lbolt(); 4568219089Spjd next = MAX(now, MIN(now + interval, began + interval)); 4569208373Smm 4570208373Smm return (next); 4571208373Smm} 4572208373Smm 4573185029Spjdstatic void 4574185029Spjdl2arc_hdr_stat_add(void) 4575185029Spjd{ 4576185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 4577185029Spjd ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 4578185029Spjd} 4579185029Spjd 4580185029Spjdstatic void 4581185029Spjdl2arc_hdr_stat_remove(void) 4582185029Spjd{ 4583185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 4584185029Spjd ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 4585185029Spjd} 4586185029Spjd 4587185029Spjd/* 4588185029Spjd * Cycle through L2ARC devices. This is how L2ARC load balances. 4589185029Spjd * If a device is returned, this also returns holding the spa config lock. 4590185029Spjd */ 4591185029Spjdstatic l2arc_dev_t * 4592185029Spjdl2arc_dev_get_next(void) 4593185029Spjd{ 4594185029Spjd l2arc_dev_t *first, *next = NULL; 4595185029Spjd 4596185029Spjd /* 4597185029Spjd * Lock out the removal of spas (spa_namespace_lock), then removal 4598185029Spjd * of cache devices (l2arc_dev_mtx). Once a device has been selected, 4599185029Spjd * both locks will be dropped and a spa config lock held instead. 4600185029Spjd */ 4601185029Spjd mutex_enter(&spa_namespace_lock); 4602185029Spjd mutex_enter(&l2arc_dev_mtx); 4603185029Spjd 4604185029Spjd /* if there are no vdevs, there is nothing to do */ 4605185029Spjd if (l2arc_ndev == 0) 4606185029Spjd goto out; 4607185029Spjd 4608185029Spjd first = NULL; 4609185029Spjd next = l2arc_dev_last; 4610185029Spjd do { 4611185029Spjd /* loop around the list looking for a non-faulted vdev */ 4612185029Spjd if (next == NULL) { 4613185029Spjd next = list_head(l2arc_dev_list); 4614185029Spjd } else { 4615185029Spjd next = list_next(l2arc_dev_list, next); 4616185029Spjd if (next == NULL) 4617185029Spjd next = list_head(l2arc_dev_list); 4618185029Spjd } 4619185029Spjd 4620185029Spjd /* if we have come back to the start, bail out */ 4621185029Spjd if (first == NULL) 4622185029Spjd first = next; 4623185029Spjd else if (next == first) 4624185029Spjd break; 4625185029Spjd 4626185029Spjd } while (vdev_is_dead(next->l2ad_vdev)); 4627185029Spjd 4628185029Spjd /* if we were unable to find any usable vdevs, return NULL */ 4629185029Spjd if (vdev_is_dead(next->l2ad_vdev)) 4630185029Spjd next = NULL; 4631185029Spjd 4632185029Spjd l2arc_dev_last = next; 4633185029Spjd 4634185029Spjdout: 4635185029Spjd mutex_exit(&l2arc_dev_mtx); 4636185029Spjd 4637185029Spjd /* 4638185029Spjd * Grab the config lock to prevent the 'next' device from being 4639185029Spjd * removed while we are writing to it. 4640185029Spjd */ 4641185029Spjd if (next != NULL) 4642185029Spjd spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 4643185029Spjd mutex_exit(&spa_namespace_lock); 4644185029Spjd 4645185029Spjd return (next); 4646185029Spjd} 4647185029Spjd 4648185029Spjd/* 4649185029Spjd * Free buffers that were tagged for destruction. 4650185029Spjd */ 4651185029Spjdstatic void 4652185029Spjdl2arc_do_free_on_write() 4653185029Spjd{ 4654185029Spjd list_t *buflist; 4655185029Spjd l2arc_data_free_t *df, *df_prev; 4656185029Spjd 4657185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 4658185029Spjd buflist = l2arc_free_on_write; 4659185029Spjd 4660185029Spjd for (df = list_tail(buflist); df; df = df_prev) { 4661185029Spjd df_prev = list_prev(buflist, df); 4662185029Spjd ASSERT(df->l2df_data != NULL); 4663185029Spjd ASSERT(df->l2df_func != NULL); 4664185029Spjd df->l2df_func(df->l2df_data, df->l2df_size); 4665185029Spjd list_remove(buflist, df); 4666185029Spjd kmem_free(df, sizeof (l2arc_data_free_t)); 4667185029Spjd } 4668185029Spjd 4669185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 4670185029Spjd} 4671185029Spjd 4672185029Spjd/* 4673185029Spjd * A write to a cache device has completed. Update all headers to allow 4674185029Spjd * reads from these buffers to begin. 4675185029Spjd */ 4676185029Spjdstatic void 4677185029Spjdl2arc_write_done(zio_t *zio) 4678185029Spjd{ 4679185029Spjd l2arc_write_callback_t *cb; 4680185029Spjd l2arc_dev_t *dev; 4681185029Spjd list_t *buflist; 4682185029Spjd arc_buf_hdr_t *head, *ab, *ab_prev; 4683185029Spjd l2arc_buf_hdr_t *abl2; 4684185029Spjd kmutex_t *hash_lock; 4685268085Sdelphij int64_t bytes_dropped = 0; 4686185029Spjd 4687185029Spjd cb = zio->io_private; 4688185029Spjd ASSERT(cb != NULL); 4689185029Spjd dev = cb->l2wcb_dev; 4690185029Spjd ASSERT(dev != NULL); 4691185029Spjd head = cb->l2wcb_head; 4692185029Spjd ASSERT(head != NULL); 4693185029Spjd buflist = dev->l2ad_buflist; 4694185029Spjd ASSERT(buflist != NULL); 4695185029Spjd DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 4696185029Spjd l2arc_write_callback_t *, cb); 4697185029Spjd 4698185029Spjd if (zio->io_error != 0) 4699185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_error); 4700185029Spjd 4701185029Spjd mutex_enter(&l2arc_buflist_mtx); 4702185029Spjd 4703185029Spjd /* 4704185029Spjd * All writes completed, or an error was hit. 4705185029Spjd */ 4706185029Spjd for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 4707185029Spjd ab_prev = list_prev(buflist, ab); 4708260835Sdelphij abl2 = ab->b_l2hdr; 4709185029Spjd 4710260835Sdelphij /* 4711260835Sdelphij * Release the temporary compressed buffer as soon as possible. 4712260835Sdelphij */ 4713260835Sdelphij if (abl2->b_compress != ZIO_COMPRESS_OFF) 4714260835Sdelphij l2arc_release_cdata_buf(ab); 4715260835Sdelphij 4716185029Spjd hash_lock = HDR_LOCK(ab); 4717185029Spjd if (!mutex_tryenter(hash_lock)) { 4718185029Spjd /* 4719185029Spjd * This buffer misses out. It may be in a stage 4720185029Spjd * of eviction. Its ARC_L2_WRITING flag will be 4721185029Spjd * left set, denying reads to this buffer. 4722185029Spjd */ 4723185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 4724185029Spjd continue; 4725185029Spjd } 4726185029Spjd 4727185029Spjd if (zio->io_error != 0) { 4728185029Spjd /* 4729185029Spjd * Error - drop L2ARC entry. 4730185029Spjd */ 4731185029Spjd list_remove(buflist, ab); 4732251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize); 4733268085Sdelphij bytes_dropped += abl2->b_asize; 4734185029Spjd ab->b_l2hdr = NULL; 4735248572Ssmh trim_map_free(abl2->b_dev->l2ad_vdev, abl2->b_daddr, 4736248574Ssmh ab->b_size, 0); 4737185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4738185029Spjd ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4739185029Spjd } 4740185029Spjd 4741185029Spjd /* 4742185029Spjd * Allow ARC to begin reads to this L2ARC entry. 4743185029Spjd */ 4744185029Spjd ab->b_flags &= ~ARC_L2_WRITING; 4745185029Spjd 4746185029Spjd mutex_exit(hash_lock); 4747185029Spjd } 4748185029Spjd 4749185029Spjd atomic_inc_64(&l2arc_writes_done); 4750185029Spjd list_remove(buflist, head); 4751185029Spjd kmem_cache_free(hdr_cache, head); 4752185029Spjd mutex_exit(&l2arc_buflist_mtx); 4753185029Spjd 4754268085Sdelphij vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 4755268085Sdelphij 4756185029Spjd l2arc_do_free_on_write(); 4757185029Spjd 4758185029Spjd kmem_free(cb, sizeof (l2arc_write_callback_t)); 4759185029Spjd} 4760185029Spjd 4761185029Spjd/* 4762185029Spjd * A read to a cache device completed. Validate buffer contents before 4763185029Spjd * handing over to the regular ARC routines. 4764185029Spjd */ 4765185029Spjdstatic void 4766185029Spjdl2arc_read_done(zio_t *zio) 4767185029Spjd{ 4768185029Spjd l2arc_read_callback_t *cb; 4769185029Spjd arc_buf_hdr_t *hdr; 4770185029Spjd arc_buf_t *buf; 4771185029Spjd kmutex_t *hash_lock; 4772185029Spjd int equal; 4773185029Spjd 4774185029Spjd ASSERT(zio->io_vd != NULL); 4775185029Spjd ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 4776185029Spjd 4777185029Spjd spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 4778185029Spjd 4779185029Spjd cb = zio->io_private; 4780185029Spjd ASSERT(cb != NULL); 4781185029Spjd buf = cb->l2rcb_buf; 4782185029Spjd ASSERT(buf != NULL); 4783185029Spjd 4784219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 4785185029Spjd mutex_enter(hash_lock); 4786219089Spjd hdr = buf->b_hdr; 4787219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4788185029Spjd 4789185029Spjd /* 4790251478Sdelphij * If the buffer was compressed, decompress it first. 4791251478Sdelphij */ 4792251478Sdelphij if (cb->l2rcb_compress != ZIO_COMPRESS_OFF) 4793251478Sdelphij l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress); 4794251478Sdelphij ASSERT(zio->io_data != NULL); 4795251478Sdelphij 4796251478Sdelphij /* 4797185029Spjd * Check this survived the L2ARC journey. 4798185029Spjd */ 4799185029Spjd equal = arc_cksum_equal(buf); 4800185029Spjd if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 4801185029Spjd mutex_exit(hash_lock); 4802185029Spjd zio->io_private = buf; 4803185029Spjd zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 4804185029Spjd zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 4805185029Spjd arc_read_done(zio); 4806185029Spjd } else { 4807185029Spjd mutex_exit(hash_lock); 4808185029Spjd /* 4809185029Spjd * Buffer didn't survive caching. Increment stats and 4810185029Spjd * reissue to the original storage device. 4811185029Spjd */ 4812185029Spjd if (zio->io_error != 0) { 4813185029Spjd ARCSTAT_BUMP(arcstat_l2_io_error); 4814185029Spjd } else { 4815249195Smm zio->io_error = SET_ERROR(EIO); 4816185029Spjd } 4817185029Spjd if (!equal) 4818185029Spjd ARCSTAT_BUMP(arcstat_l2_cksum_bad); 4819185029Spjd 4820185029Spjd /* 4821185029Spjd * If there's no waiter, issue an async i/o to the primary 4822185029Spjd * storage now. If there *is* a waiter, the caller must 4823185029Spjd * issue the i/o in a context where it's OK to block. 4824185029Spjd */ 4825209962Smm if (zio->io_waiter == NULL) { 4826209962Smm zio_t *pio = zio_unique_parent(zio); 4827209962Smm 4828209962Smm ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 4829209962Smm 4830209962Smm zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 4831185029Spjd buf->b_data, zio->io_size, arc_read_done, buf, 4832185029Spjd zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 4833209962Smm } 4834185029Spjd } 4835185029Spjd 4836185029Spjd kmem_free(cb, sizeof (l2arc_read_callback_t)); 4837185029Spjd} 4838185029Spjd 4839185029Spjd/* 4840185029Spjd * This is the list priority from which the L2ARC will search for pages to 4841185029Spjd * cache. This is used within loops (0..3) to cycle through lists in the 4842185029Spjd * desired order. This order can have a significant effect on cache 4843185029Spjd * performance. 4844185029Spjd * 4845185029Spjd * Currently the metadata lists are hit first, MFU then MRU, followed by 4846185029Spjd * the data lists. This function returns a locked list, and also returns 4847185029Spjd * the lock pointer. 4848185029Spjd */ 4849185029Spjdstatic list_t * 4850185029Spjdl2arc_list_locked(int list_num, kmutex_t **lock) 4851185029Spjd{ 4852247187Smm list_t *list = NULL; 4853205231Skmacy int idx; 4854185029Spjd 4855206796Spjd ASSERT(list_num >= 0 && list_num < 2 * ARC_BUFC_NUMLISTS); 4856206796Spjd 4857205231Skmacy if (list_num < ARC_BUFC_NUMMETADATALISTS) { 4858205231Skmacy idx = list_num; 4859205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4860205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4861206796Spjd } else if (list_num < ARC_BUFC_NUMMETADATALISTS * 2) { 4862205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4863205231Skmacy list = &arc_mru->arcs_lists[idx]; 4864205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4865206796Spjd } else if (list_num < (ARC_BUFC_NUMMETADATALISTS * 2 + 4866205231Skmacy ARC_BUFC_NUMDATALISTS)) { 4867205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4868205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4869205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4870205231Skmacy } else { 4871205231Skmacy idx = list_num - ARC_BUFC_NUMLISTS; 4872205231Skmacy list = &arc_mru->arcs_lists[idx]; 4873205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4874185029Spjd } 4875185029Spjd 4876185029Spjd ASSERT(!(MUTEX_HELD(*lock))); 4877185029Spjd mutex_enter(*lock); 4878185029Spjd return (list); 4879185029Spjd} 4880185029Spjd 4881185029Spjd/* 4882185029Spjd * Evict buffers from the device write hand to the distance specified in 4883185029Spjd * bytes. This distance may span populated buffers, it may span nothing. 4884185029Spjd * This is clearing a region on the L2ARC device ready for writing. 4885185029Spjd * If the 'all' boolean is set, every buffer is evicted. 4886185029Spjd */ 4887185029Spjdstatic void 4888185029Spjdl2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 4889185029Spjd{ 4890185029Spjd list_t *buflist; 4891185029Spjd l2arc_buf_hdr_t *abl2; 4892185029Spjd arc_buf_hdr_t *ab, *ab_prev; 4893185029Spjd kmutex_t *hash_lock; 4894185029Spjd uint64_t taddr; 4895268085Sdelphij int64_t bytes_evicted = 0; 4896185029Spjd 4897185029Spjd buflist = dev->l2ad_buflist; 4898185029Spjd 4899185029Spjd if (buflist == NULL) 4900185029Spjd return; 4901185029Spjd 4902185029Spjd if (!all && dev->l2ad_first) { 4903185029Spjd /* 4904185029Spjd * This is the first sweep through the device. There is 4905185029Spjd * nothing to evict. 4906185029Spjd */ 4907185029Spjd return; 4908185029Spjd } 4909185029Spjd 4910185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 4911185029Spjd /* 4912185029Spjd * When nearing the end of the device, evict to the end 4913185029Spjd * before the device write hand jumps to the start. 4914185029Spjd */ 4915185029Spjd taddr = dev->l2ad_end; 4916185029Spjd } else { 4917185029Spjd taddr = dev->l2ad_hand + distance; 4918185029Spjd } 4919185029Spjd DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 4920185029Spjd uint64_t, taddr, boolean_t, all); 4921185029Spjd 4922185029Spjdtop: 4923185029Spjd mutex_enter(&l2arc_buflist_mtx); 4924185029Spjd for (ab = list_tail(buflist); ab; ab = ab_prev) { 4925185029Spjd ab_prev = list_prev(buflist, ab); 4926185029Spjd 4927185029Spjd hash_lock = HDR_LOCK(ab); 4928185029Spjd if (!mutex_tryenter(hash_lock)) { 4929185029Spjd /* 4930185029Spjd * Missed the hash lock. Retry. 4931185029Spjd */ 4932185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 4933185029Spjd mutex_exit(&l2arc_buflist_mtx); 4934185029Spjd mutex_enter(hash_lock); 4935185029Spjd mutex_exit(hash_lock); 4936185029Spjd goto top; 4937185029Spjd } 4938185029Spjd 4939185029Spjd if (HDR_L2_WRITE_HEAD(ab)) { 4940185029Spjd /* 4941185029Spjd * We hit a write head node. Leave it for 4942185029Spjd * l2arc_write_done(). 4943185029Spjd */ 4944185029Spjd list_remove(buflist, ab); 4945185029Spjd mutex_exit(hash_lock); 4946185029Spjd continue; 4947185029Spjd } 4948185029Spjd 4949185029Spjd if (!all && ab->b_l2hdr != NULL && 4950185029Spjd (ab->b_l2hdr->b_daddr > taddr || 4951185029Spjd ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 4952185029Spjd /* 4953185029Spjd * We've evicted to the target address, 4954185029Spjd * or the end of the device. 4955185029Spjd */ 4956185029Spjd mutex_exit(hash_lock); 4957185029Spjd break; 4958185029Spjd } 4959185029Spjd 4960185029Spjd if (HDR_FREE_IN_PROGRESS(ab)) { 4961185029Spjd /* 4962185029Spjd * Already on the path to destruction. 4963185029Spjd */ 4964185029Spjd mutex_exit(hash_lock); 4965185029Spjd continue; 4966185029Spjd } 4967185029Spjd 4968185029Spjd if (ab->b_state == arc_l2c_only) { 4969185029Spjd ASSERT(!HDR_L2_READING(ab)); 4970185029Spjd /* 4971185029Spjd * This doesn't exist in the ARC. Destroy. 4972185029Spjd * arc_hdr_destroy() will call list_remove() 4973185029Spjd * and decrement arcstat_l2_size. 4974185029Spjd */ 4975185029Spjd arc_change_state(arc_anon, ab, hash_lock); 4976185029Spjd arc_hdr_destroy(ab); 4977185029Spjd } else { 4978185029Spjd /* 4979185029Spjd * Invalidate issued or about to be issued 4980185029Spjd * reads, since we may be about to write 4981185029Spjd * over this location. 4982185029Spjd */ 4983185029Spjd if (HDR_L2_READING(ab)) { 4984185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_reading); 4985185029Spjd ab->b_flags |= ARC_L2_EVICTED; 4986185029Spjd } 4987185029Spjd 4988185029Spjd /* 4989185029Spjd * Tell ARC this no longer exists in L2ARC. 4990185029Spjd */ 4991185029Spjd if (ab->b_l2hdr != NULL) { 4992185029Spjd abl2 = ab->b_l2hdr; 4993251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize); 4994268085Sdelphij bytes_evicted += abl2->b_asize; 4995185029Spjd ab->b_l2hdr = NULL; 4996274172Savg /* 4997274172Savg * We are destroying l2hdr, so ensure that 4998274172Savg * its compressed buffer, if any, is not leaked. 4999274172Savg */ 5000274172Savg ASSERT(abl2->b_tmp_cdata == NULL); 5001185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 5002185029Spjd ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 5003185029Spjd } 5004185029Spjd list_remove(buflist, ab); 5005185029Spjd 5006185029Spjd /* 5007185029Spjd * This may have been leftover after a 5008185029Spjd * failed write. 5009185029Spjd */ 5010185029Spjd ab->b_flags &= ~ARC_L2_WRITING; 5011185029Spjd } 5012185029Spjd mutex_exit(hash_lock); 5013185029Spjd } 5014185029Spjd mutex_exit(&l2arc_buflist_mtx); 5015185029Spjd 5016268085Sdelphij vdev_space_update(dev->l2ad_vdev, -bytes_evicted, 0, 0); 5017185029Spjd dev->l2ad_evict = taddr; 5018185029Spjd} 5019185029Spjd 5020185029Spjd/* 5021185029Spjd * Find and write ARC buffers to the L2ARC device. 5022185029Spjd * 5023185029Spjd * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 5024185029Spjd * for reading until they have completed writing. 5025251478Sdelphij * The headroom_boost is an in-out parameter used to maintain headroom boost 5026251478Sdelphij * state between calls to this function. 5027251478Sdelphij * 5028251478Sdelphij * Returns the number of bytes actually written (which may be smaller than 5029251478Sdelphij * the delta by which the device hand has changed due to alignment). 5030185029Spjd */ 5031208373Smmstatic uint64_t 5032251478Sdelphijl2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz, 5033251478Sdelphij boolean_t *headroom_boost) 5034185029Spjd{ 5035185029Spjd arc_buf_hdr_t *ab, *ab_prev, *head; 5036185029Spjd list_t *list; 5037251478Sdelphij uint64_t write_asize, write_psize, write_sz, headroom, 5038251478Sdelphij buf_compress_minsz; 5039185029Spjd void *buf_data; 5040251478Sdelphij kmutex_t *list_lock; 5041251478Sdelphij boolean_t full; 5042185029Spjd l2arc_write_callback_t *cb; 5043185029Spjd zio_t *pio, *wzio; 5044228103Smm uint64_t guid = spa_load_guid(spa); 5045251478Sdelphij const boolean_t do_headroom_boost = *headroom_boost; 5046185029Spjd int try; 5047185029Spjd 5048185029Spjd ASSERT(dev->l2ad_vdev != NULL); 5049185029Spjd 5050251478Sdelphij /* Lower the flag now, we might want to raise it again later. */ 5051251478Sdelphij *headroom_boost = B_FALSE; 5052251478Sdelphij 5053185029Spjd pio = NULL; 5054251478Sdelphij write_sz = write_asize = write_psize = 0; 5055185029Spjd full = B_FALSE; 5056185029Spjd head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 5057185029Spjd head->b_flags |= ARC_L2_WRITE_HEAD; 5058185029Spjd 5059205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 5060185029Spjd /* 5061251478Sdelphij * We will want to try to compress buffers that are at least 2x the 5062251478Sdelphij * device sector size. 5063251478Sdelphij */ 5064251478Sdelphij buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift; 5065251478Sdelphij 5066251478Sdelphij /* 5067185029Spjd * Copy buffers for L2ARC writing. 5068185029Spjd */ 5069185029Spjd mutex_enter(&l2arc_buflist_mtx); 5070206796Spjd for (try = 0; try < 2 * ARC_BUFC_NUMLISTS; try++) { 5071251478Sdelphij uint64_t passed_sz = 0; 5072251478Sdelphij 5073185029Spjd list = l2arc_list_locked(try, &list_lock); 5074205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 5075185029Spjd 5076185029Spjd /* 5077185029Spjd * L2ARC fast warmup. 5078185029Spjd * 5079185029Spjd * Until the ARC is warm and starts to evict, read from the 5080185029Spjd * head of the ARC lists rather than the tail. 5081185029Spjd */ 5082185029Spjd if (arc_warm == B_FALSE) 5083185029Spjd ab = list_head(list); 5084185029Spjd else 5085185029Spjd ab = list_tail(list); 5086206796Spjd if (ab == NULL) 5087205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 5088185029Spjd 5089272708Savg headroom = target_sz * l2arc_headroom * 2 / ARC_BUFC_NUMLISTS; 5090251478Sdelphij if (do_headroom_boost) 5091251478Sdelphij headroom = (headroom * l2arc_headroom_boost) / 100; 5092251478Sdelphij 5093185029Spjd for (; ab; ab = ab_prev) { 5094251478Sdelphij l2arc_buf_hdr_t *l2hdr; 5095251478Sdelphij kmutex_t *hash_lock; 5096251478Sdelphij uint64_t buf_sz; 5097251478Sdelphij 5098185029Spjd if (arc_warm == B_FALSE) 5099185029Spjd ab_prev = list_next(list, ab); 5100185029Spjd else 5101185029Spjd ab_prev = list_prev(list, ab); 5102205231Skmacy ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, ab->b_size); 5103206796Spjd 5104185029Spjd hash_lock = HDR_LOCK(ab); 5105251478Sdelphij if (!mutex_tryenter(hash_lock)) { 5106205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 5107185029Spjd /* 5108185029Spjd * Skip this buffer rather than waiting. 5109185029Spjd */ 5110185029Spjd continue; 5111185029Spjd } 5112185029Spjd 5113185029Spjd passed_sz += ab->b_size; 5114185029Spjd if (passed_sz > headroom) { 5115185029Spjd /* 5116185029Spjd * Searched too far. 5117185029Spjd */ 5118185029Spjd mutex_exit(hash_lock); 5119205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 5120185029Spjd break; 5121185029Spjd } 5122185029Spjd 5123209962Smm if (!l2arc_write_eligible(guid, ab)) { 5124185029Spjd mutex_exit(hash_lock); 5125185029Spjd continue; 5126185029Spjd } 5127185029Spjd 5128185029Spjd if ((write_sz + ab->b_size) > target_sz) { 5129185029Spjd full = B_TRUE; 5130185029Spjd mutex_exit(hash_lock); 5131205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_full); 5132185029Spjd break; 5133185029Spjd } 5134185029Spjd 5135185029Spjd if (pio == NULL) { 5136185029Spjd /* 5137185029Spjd * Insert a dummy header on the buflist so 5138185029Spjd * l2arc_write_done() can find where the 5139185029Spjd * write buffers begin without searching. 5140185029Spjd */ 5141185029Spjd list_insert_head(dev->l2ad_buflist, head); 5142185029Spjd 5143185029Spjd cb = kmem_alloc( 5144185029Spjd sizeof (l2arc_write_callback_t), KM_SLEEP); 5145185029Spjd cb->l2wcb_dev = dev; 5146185029Spjd cb->l2wcb_head = head; 5147185029Spjd pio = zio_root(spa, l2arc_write_done, cb, 5148185029Spjd ZIO_FLAG_CANFAIL); 5149205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_pios); 5150185029Spjd } 5151185029Spjd 5152185029Spjd /* 5153185029Spjd * Create and add a new L2ARC header. 5154185029Spjd */ 5155251478Sdelphij l2hdr = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 5156251478Sdelphij l2hdr->b_dev = dev; 5157251478Sdelphij ab->b_flags |= ARC_L2_WRITING; 5158185029Spjd 5159251478Sdelphij /* 5160251478Sdelphij * Temporarily stash the data buffer in b_tmp_cdata. 5161251478Sdelphij * The subsequent write step will pick it up from 5162251478Sdelphij * there. This is because can't access ab->b_buf 5163251478Sdelphij * without holding the hash_lock, which we in turn 5164251478Sdelphij * can't access without holding the ARC list locks 5165251478Sdelphij * (which we want to avoid during compression/writing). 5166251478Sdelphij */ 5167251478Sdelphij l2hdr->b_compress = ZIO_COMPRESS_OFF; 5168251478Sdelphij l2hdr->b_asize = ab->b_size; 5169251478Sdelphij l2hdr->b_tmp_cdata = ab->b_buf->b_data; 5170251478Sdelphij 5171185029Spjd buf_sz = ab->b_size; 5172251478Sdelphij ab->b_l2hdr = l2hdr; 5173185029Spjd 5174251478Sdelphij list_insert_head(dev->l2ad_buflist, ab); 5175251478Sdelphij 5176185029Spjd /* 5177185029Spjd * Compute and store the buffer cksum before 5178185029Spjd * writing. On debug the cksum is verified first. 5179185029Spjd */ 5180185029Spjd arc_cksum_verify(ab->b_buf); 5181185029Spjd arc_cksum_compute(ab->b_buf, B_TRUE); 5182185029Spjd 5183185029Spjd mutex_exit(hash_lock); 5184185029Spjd 5185251478Sdelphij write_sz += buf_sz; 5186251478Sdelphij } 5187251478Sdelphij 5188251478Sdelphij mutex_exit(list_lock); 5189251478Sdelphij 5190251478Sdelphij if (full == B_TRUE) 5191251478Sdelphij break; 5192251478Sdelphij } 5193251478Sdelphij 5194251478Sdelphij /* No buffers selected for writing? */ 5195251478Sdelphij if (pio == NULL) { 5196251478Sdelphij ASSERT0(write_sz); 5197251478Sdelphij mutex_exit(&l2arc_buflist_mtx); 5198251478Sdelphij kmem_cache_free(hdr_cache, head); 5199251478Sdelphij return (0); 5200251478Sdelphij } 5201251478Sdelphij 5202251478Sdelphij /* 5203251478Sdelphij * Now start writing the buffers. We're starting at the write head 5204251478Sdelphij * and work backwards, retracing the course of the buffer selector 5205251478Sdelphij * loop above. 5206251478Sdelphij */ 5207251478Sdelphij for (ab = list_prev(dev->l2ad_buflist, head); ab; 5208251478Sdelphij ab = list_prev(dev->l2ad_buflist, ab)) { 5209251478Sdelphij l2arc_buf_hdr_t *l2hdr; 5210251478Sdelphij uint64_t buf_sz; 5211251478Sdelphij 5212251478Sdelphij /* 5213251478Sdelphij * We shouldn't need to lock the buffer here, since we flagged 5214251478Sdelphij * it as ARC_L2_WRITING in the previous step, but we must take 5215251478Sdelphij * care to only access its L2 cache parameters. In particular, 5216251478Sdelphij * ab->b_buf may be invalid by now due to ARC eviction. 5217251478Sdelphij */ 5218251478Sdelphij l2hdr = ab->b_l2hdr; 5219251478Sdelphij l2hdr->b_daddr = dev->l2ad_hand; 5220251478Sdelphij 5221251478Sdelphij if ((ab->b_flags & ARC_L2COMPRESS) && 5222251478Sdelphij l2hdr->b_asize >= buf_compress_minsz) { 5223251478Sdelphij if (l2arc_compress_buf(l2hdr)) { 5224251478Sdelphij /* 5225251478Sdelphij * If compression succeeded, enable headroom 5226251478Sdelphij * boost on the next scan cycle. 5227251478Sdelphij */ 5228251478Sdelphij *headroom_boost = B_TRUE; 5229251478Sdelphij } 5230251478Sdelphij } 5231251478Sdelphij 5232251478Sdelphij /* 5233251478Sdelphij * Pick up the buffer data we had previously stashed away 5234251478Sdelphij * (and now potentially also compressed). 5235251478Sdelphij */ 5236251478Sdelphij buf_data = l2hdr->b_tmp_cdata; 5237251478Sdelphij buf_sz = l2hdr->b_asize; 5238251478Sdelphij 5239274172Savg /* 5240274172Savg * If the data has not been compressed, then clear b_tmp_cdata 5241274172Savg * to make sure that it points only to a temporary compression 5242274172Savg * buffer. 5243274172Savg */ 5244274172Savg if (!L2ARC_IS_VALID_COMPRESS(l2hdr->b_compress)) 5245274172Savg l2hdr->b_tmp_cdata = NULL; 5246274172Savg 5247251478Sdelphij /* Compression may have squashed the buffer to zero length. */ 5248251478Sdelphij if (buf_sz != 0) { 5249251478Sdelphij uint64_t buf_p_sz; 5250251478Sdelphij 5251185029Spjd wzio = zio_write_phys(pio, dev->l2ad_vdev, 5252185029Spjd dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 5253185029Spjd NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 5254185029Spjd ZIO_FLAG_CANFAIL, B_FALSE); 5255185029Spjd 5256185029Spjd DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 5257185029Spjd zio_t *, wzio); 5258185029Spjd (void) zio_nowait(wzio); 5259185029Spjd 5260251478Sdelphij write_asize += buf_sz; 5261185029Spjd /* 5262185029Spjd * Keep the clock hand suitably device-aligned. 5263185029Spjd */ 5264251478Sdelphij buf_p_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 5265251478Sdelphij write_psize += buf_p_sz; 5266251478Sdelphij dev->l2ad_hand += buf_p_sz; 5267185029Spjd } 5268251478Sdelphij } 5269185029Spjd 5270185029Spjd mutex_exit(&l2arc_buflist_mtx); 5271185029Spjd 5272251478Sdelphij ASSERT3U(write_asize, <=, target_sz); 5273185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_sent); 5274251478Sdelphij ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize); 5275185029Spjd ARCSTAT_INCR(arcstat_l2_size, write_sz); 5276251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, write_asize); 5277273060Sdelphij vdev_space_update(dev->l2ad_vdev, write_psize, 0, 0); 5278185029Spjd 5279185029Spjd /* 5280185029Spjd * Bump device hand to the device start if it is approaching the end. 5281185029Spjd * l2arc_evict() will already have evicted ahead for this case. 5282185029Spjd */ 5283185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 5284185029Spjd dev->l2ad_hand = dev->l2ad_start; 5285185029Spjd dev->l2ad_evict = dev->l2ad_start; 5286185029Spjd dev->l2ad_first = B_FALSE; 5287185029Spjd } 5288185029Spjd 5289208373Smm dev->l2ad_writing = B_TRUE; 5290185029Spjd (void) zio_wait(pio); 5291208373Smm dev->l2ad_writing = B_FALSE; 5292208373Smm 5293251478Sdelphij return (write_asize); 5294185029Spjd} 5295185029Spjd 5296185029Spjd/* 5297251478Sdelphij * Compresses an L2ARC buffer. 5298251478Sdelphij * The data to be compressed must be prefilled in l2hdr->b_tmp_cdata and its 5299251478Sdelphij * size in l2hdr->b_asize. This routine tries to compress the data and 5300251478Sdelphij * depending on the compression result there are three possible outcomes: 5301251478Sdelphij * *) The buffer was incompressible. The original l2hdr contents were left 5302251478Sdelphij * untouched and are ready for writing to an L2 device. 5303251478Sdelphij * *) The buffer was all-zeros, so there is no need to write it to an L2 5304251478Sdelphij * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is 5305251478Sdelphij * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY. 5306251478Sdelphij * *) Compression succeeded and b_tmp_cdata was replaced with a temporary 5307251478Sdelphij * data buffer which holds the compressed data to be written, and b_asize 5308251478Sdelphij * tells us how much data there is. b_compress is set to the appropriate 5309251478Sdelphij * compression algorithm. Once writing is done, invoke 5310251478Sdelphij * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer. 5311251478Sdelphij * 5312251478Sdelphij * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the 5313251478Sdelphij * buffer was incompressible). 5314251478Sdelphij */ 5315251478Sdelphijstatic boolean_t 5316251478Sdelphijl2arc_compress_buf(l2arc_buf_hdr_t *l2hdr) 5317251478Sdelphij{ 5318251478Sdelphij void *cdata; 5319268075Sdelphij size_t csize, len, rounded; 5320251478Sdelphij 5321251478Sdelphij ASSERT(l2hdr->b_compress == ZIO_COMPRESS_OFF); 5322251478Sdelphij ASSERT(l2hdr->b_tmp_cdata != NULL); 5323251478Sdelphij 5324251478Sdelphij len = l2hdr->b_asize; 5325251478Sdelphij cdata = zio_data_buf_alloc(len); 5326251478Sdelphij csize = zio_compress_data(ZIO_COMPRESS_LZ4, l2hdr->b_tmp_cdata, 5327269086Sdelphij cdata, l2hdr->b_asize); 5328251478Sdelphij 5329268075Sdelphij rounded = P2ROUNDUP(csize, (size_t)SPA_MINBLOCKSIZE); 5330268075Sdelphij if (rounded > csize) { 5331268075Sdelphij bzero((char *)cdata + csize, rounded - csize); 5332268075Sdelphij csize = rounded; 5333268075Sdelphij } 5334268075Sdelphij 5335251478Sdelphij if (csize == 0) { 5336251478Sdelphij /* zero block, indicate that there's nothing to write */ 5337251478Sdelphij zio_data_buf_free(cdata, len); 5338251478Sdelphij l2hdr->b_compress = ZIO_COMPRESS_EMPTY; 5339251478Sdelphij l2hdr->b_asize = 0; 5340251478Sdelphij l2hdr->b_tmp_cdata = NULL; 5341251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_zeros); 5342251478Sdelphij return (B_TRUE); 5343251478Sdelphij } else if (csize > 0 && csize < len) { 5344251478Sdelphij /* 5345251478Sdelphij * Compression succeeded, we'll keep the cdata around for 5346251478Sdelphij * writing and release it afterwards. 5347251478Sdelphij */ 5348251478Sdelphij l2hdr->b_compress = ZIO_COMPRESS_LZ4; 5349251478Sdelphij l2hdr->b_asize = csize; 5350251478Sdelphij l2hdr->b_tmp_cdata = cdata; 5351251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_successes); 5352251478Sdelphij return (B_TRUE); 5353251478Sdelphij } else { 5354251478Sdelphij /* 5355251478Sdelphij * Compression failed, release the compressed buffer. 5356251478Sdelphij * l2hdr will be left unmodified. 5357251478Sdelphij */ 5358251478Sdelphij zio_data_buf_free(cdata, len); 5359251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_failures); 5360251478Sdelphij return (B_FALSE); 5361251478Sdelphij } 5362251478Sdelphij} 5363251478Sdelphij 5364251478Sdelphij/* 5365251478Sdelphij * Decompresses a zio read back from an l2arc device. On success, the 5366251478Sdelphij * underlying zio's io_data buffer is overwritten by the uncompressed 5367251478Sdelphij * version. On decompression error (corrupt compressed stream), the 5368251478Sdelphij * zio->io_error value is set to signal an I/O error. 5369251478Sdelphij * 5370251478Sdelphij * Please note that the compressed data stream is not checksummed, so 5371251478Sdelphij * if the underlying device is experiencing data corruption, we may feed 5372251478Sdelphij * corrupt data to the decompressor, so the decompressor needs to be 5373251478Sdelphij * able to handle this situation (LZ4 does). 5374251478Sdelphij */ 5375251478Sdelphijstatic void 5376251478Sdelphijl2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c) 5377251478Sdelphij{ 5378251478Sdelphij ASSERT(L2ARC_IS_VALID_COMPRESS(c)); 5379251478Sdelphij 5380251478Sdelphij if (zio->io_error != 0) { 5381251478Sdelphij /* 5382251478Sdelphij * An io error has occured, just restore the original io 5383251478Sdelphij * size in preparation for a main pool read. 5384251478Sdelphij */ 5385251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 5386251478Sdelphij return; 5387251478Sdelphij } 5388251478Sdelphij 5389251478Sdelphij if (c == ZIO_COMPRESS_EMPTY) { 5390251478Sdelphij /* 5391251478Sdelphij * An empty buffer results in a null zio, which means we 5392251478Sdelphij * need to fill its io_data after we're done restoring the 5393251478Sdelphij * buffer's contents. 5394251478Sdelphij */ 5395251478Sdelphij ASSERT(hdr->b_buf != NULL); 5396251478Sdelphij bzero(hdr->b_buf->b_data, hdr->b_size); 5397251478Sdelphij zio->io_data = zio->io_orig_data = hdr->b_buf->b_data; 5398251478Sdelphij } else { 5399251478Sdelphij ASSERT(zio->io_data != NULL); 5400251478Sdelphij /* 5401251478Sdelphij * We copy the compressed data from the start of the arc buffer 5402251478Sdelphij * (the zio_read will have pulled in only what we need, the 5403251478Sdelphij * rest is garbage which we will overwrite at decompression) 5404251478Sdelphij * and then decompress back to the ARC data buffer. This way we 5405251478Sdelphij * can minimize copying by simply decompressing back over the 5406251478Sdelphij * original compressed data (rather than decompressing to an 5407251478Sdelphij * aux buffer and then copying back the uncompressed buffer, 5408251478Sdelphij * which is likely to be much larger). 5409251478Sdelphij */ 5410251478Sdelphij uint64_t csize; 5411251478Sdelphij void *cdata; 5412251478Sdelphij 5413251478Sdelphij csize = zio->io_size; 5414251478Sdelphij cdata = zio_data_buf_alloc(csize); 5415251478Sdelphij bcopy(zio->io_data, cdata, csize); 5416251478Sdelphij if (zio_decompress_data(c, cdata, zio->io_data, csize, 5417251478Sdelphij hdr->b_size) != 0) 5418251478Sdelphij zio->io_error = EIO; 5419251478Sdelphij zio_data_buf_free(cdata, csize); 5420251478Sdelphij } 5421251478Sdelphij 5422251478Sdelphij /* Restore the expected uncompressed IO size. */ 5423251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 5424251478Sdelphij} 5425251478Sdelphij 5426251478Sdelphij/* 5427251478Sdelphij * Releases the temporary b_tmp_cdata buffer in an l2arc header structure. 5428251478Sdelphij * This buffer serves as a temporary holder of compressed data while 5429251478Sdelphij * the buffer entry is being written to an l2arc device. Once that is 5430251478Sdelphij * done, we can dispose of it. 5431251478Sdelphij */ 5432251478Sdelphijstatic void 5433251478Sdelphijl2arc_release_cdata_buf(arc_buf_hdr_t *ab) 5434251478Sdelphij{ 5435251478Sdelphij l2arc_buf_hdr_t *l2hdr = ab->b_l2hdr; 5436251478Sdelphij 5437274172Savg ASSERT(L2ARC_IS_VALID_COMPRESS(l2hdr->b_compress)); 5438274172Savg if (l2hdr->b_compress != ZIO_COMPRESS_EMPTY) { 5439251478Sdelphij /* 5440251478Sdelphij * If the data was compressed, then we've allocated a 5441251478Sdelphij * temporary buffer for it, so now we need to release it. 5442251478Sdelphij */ 5443251478Sdelphij ASSERT(l2hdr->b_tmp_cdata != NULL); 5444251478Sdelphij zio_data_buf_free(l2hdr->b_tmp_cdata, ab->b_size); 5445274172Savg l2hdr->b_tmp_cdata = NULL; 5446274172Savg } else { 5447274172Savg ASSERT(l2hdr->b_tmp_cdata == NULL); 5448251478Sdelphij } 5449251478Sdelphij} 5450251478Sdelphij 5451251478Sdelphij/* 5452185029Spjd * This thread feeds the L2ARC at regular intervals. This is the beating 5453185029Spjd * heart of the L2ARC. 5454185029Spjd */ 5455185029Spjdstatic void 5456185029Spjdl2arc_feed_thread(void *dummy __unused) 5457185029Spjd{ 5458185029Spjd callb_cpr_t cpr; 5459185029Spjd l2arc_dev_t *dev; 5460185029Spjd spa_t *spa; 5461208373Smm uint64_t size, wrote; 5462219089Spjd clock_t begin, next = ddi_get_lbolt(); 5463251478Sdelphij boolean_t headroom_boost = B_FALSE; 5464185029Spjd 5465185029Spjd CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 5466185029Spjd 5467185029Spjd mutex_enter(&l2arc_feed_thr_lock); 5468185029Spjd 5469185029Spjd while (l2arc_thread_exit == 0) { 5470185029Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 5471185029Spjd (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 5472219089Spjd next - ddi_get_lbolt()); 5473185029Spjd CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 5474219089Spjd next = ddi_get_lbolt() + hz; 5475185029Spjd 5476185029Spjd /* 5477185029Spjd * Quick check for L2ARC devices. 5478185029Spjd */ 5479185029Spjd mutex_enter(&l2arc_dev_mtx); 5480185029Spjd if (l2arc_ndev == 0) { 5481185029Spjd mutex_exit(&l2arc_dev_mtx); 5482185029Spjd continue; 5483185029Spjd } 5484185029Spjd mutex_exit(&l2arc_dev_mtx); 5485219089Spjd begin = ddi_get_lbolt(); 5486185029Spjd 5487185029Spjd /* 5488185029Spjd * This selects the next l2arc device to write to, and in 5489185029Spjd * doing so the next spa to feed from: dev->l2ad_spa. This 5490185029Spjd * will return NULL if there are now no l2arc devices or if 5491185029Spjd * they are all faulted. 5492185029Spjd * 5493185029Spjd * If a device is returned, its spa's config lock is also 5494185029Spjd * held to prevent device removal. l2arc_dev_get_next() 5495185029Spjd * will grab and release l2arc_dev_mtx. 5496185029Spjd */ 5497185029Spjd if ((dev = l2arc_dev_get_next()) == NULL) 5498185029Spjd continue; 5499185029Spjd 5500185029Spjd spa = dev->l2ad_spa; 5501185029Spjd ASSERT(spa != NULL); 5502185029Spjd 5503185029Spjd /* 5504219089Spjd * If the pool is read-only then force the feed thread to 5505219089Spjd * sleep a little longer. 5506219089Spjd */ 5507219089Spjd if (!spa_writeable(spa)) { 5508219089Spjd next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 5509219089Spjd spa_config_exit(spa, SCL_L2ARC, dev); 5510219089Spjd continue; 5511219089Spjd } 5512219089Spjd 5513219089Spjd /* 5514185029Spjd * Avoid contributing to memory pressure. 5515185029Spjd */ 5516185029Spjd if (arc_reclaim_needed()) { 5517185029Spjd ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 5518185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 5519185029Spjd continue; 5520185029Spjd } 5521185029Spjd 5522185029Spjd ARCSTAT_BUMP(arcstat_l2_feeds); 5523185029Spjd 5524251478Sdelphij size = l2arc_write_size(); 5525185029Spjd 5526185029Spjd /* 5527185029Spjd * Evict L2ARC buffers that will be overwritten. 5528185029Spjd */ 5529185029Spjd l2arc_evict(dev, size, B_FALSE); 5530185029Spjd 5531185029Spjd /* 5532185029Spjd * Write ARC buffers. 5533185029Spjd */ 5534251478Sdelphij wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost); 5535208373Smm 5536208373Smm /* 5537208373Smm * Calculate interval between writes. 5538208373Smm */ 5539208373Smm next = l2arc_write_interval(begin, size, wrote); 5540185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 5541185029Spjd } 5542185029Spjd 5543185029Spjd l2arc_thread_exit = 0; 5544185029Spjd cv_broadcast(&l2arc_feed_thr_cv); 5545185029Spjd CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 5546185029Spjd thread_exit(); 5547185029Spjd} 5548185029Spjd 5549185029Spjdboolean_t 5550185029Spjdl2arc_vdev_present(vdev_t *vd) 5551185029Spjd{ 5552185029Spjd l2arc_dev_t *dev; 5553185029Spjd 5554185029Spjd mutex_enter(&l2arc_dev_mtx); 5555185029Spjd for (dev = list_head(l2arc_dev_list); dev != NULL; 5556185029Spjd dev = list_next(l2arc_dev_list, dev)) { 5557185029Spjd if (dev->l2ad_vdev == vd) 5558185029Spjd break; 5559185029Spjd } 5560185029Spjd mutex_exit(&l2arc_dev_mtx); 5561185029Spjd 5562185029Spjd return (dev != NULL); 5563185029Spjd} 5564185029Spjd 5565185029Spjd/* 5566185029Spjd * Add a vdev for use by the L2ARC. By this point the spa has already 5567185029Spjd * validated the vdev and opened it. 5568185029Spjd */ 5569185029Spjdvoid 5570219089Spjdl2arc_add_vdev(spa_t *spa, vdev_t *vd) 5571185029Spjd{ 5572185029Spjd l2arc_dev_t *adddev; 5573185029Spjd 5574185029Spjd ASSERT(!l2arc_vdev_present(vd)); 5575185029Spjd 5576255753Sgibbs vdev_ashift_optimize(vd); 5577255753Sgibbs 5578185029Spjd /* 5579185029Spjd * Create a new l2arc device entry. 5580185029Spjd */ 5581185029Spjd adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 5582185029Spjd adddev->l2ad_spa = spa; 5583185029Spjd adddev->l2ad_vdev = vd; 5584219089Spjd adddev->l2ad_start = VDEV_LABEL_START_SIZE; 5585219089Spjd adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 5586185029Spjd adddev->l2ad_hand = adddev->l2ad_start; 5587185029Spjd adddev->l2ad_evict = adddev->l2ad_start; 5588185029Spjd adddev->l2ad_first = B_TRUE; 5589208373Smm adddev->l2ad_writing = B_FALSE; 5590185029Spjd 5591185029Spjd /* 5592185029Spjd * This is a list of all ARC buffers that are still valid on the 5593185029Spjd * device. 5594185029Spjd */ 5595185029Spjd adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 5596185029Spjd list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 5597185029Spjd offsetof(arc_buf_hdr_t, b_l2node)); 5598185029Spjd 5599219089Spjd vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 5600185029Spjd 5601185029Spjd /* 5602185029Spjd * Add device to global list 5603185029Spjd */ 5604185029Spjd mutex_enter(&l2arc_dev_mtx); 5605185029Spjd list_insert_head(l2arc_dev_list, adddev); 5606185029Spjd atomic_inc_64(&l2arc_ndev); 5607185029Spjd mutex_exit(&l2arc_dev_mtx); 5608185029Spjd} 5609185029Spjd 5610185029Spjd/* 5611185029Spjd * Remove a vdev from the L2ARC. 5612185029Spjd */ 5613185029Spjdvoid 5614185029Spjdl2arc_remove_vdev(vdev_t *vd) 5615185029Spjd{ 5616185029Spjd l2arc_dev_t *dev, *nextdev, *remdev = NULL; 5617185029Spjd 5618185029Spjd /* 5619185029Spjd * Find the device by vdev 5620185029Spjd */ 5621185029Spjd mutex_enter(&l2arc_dev_mtx); 5622185029Spjd for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 5623185029Spjd nextdev = list_next(l2arc_dev_list, dev); 5624185029Spjd if (vd == dev->l2ad_vdev) { 5625185029Spjd remdev = dev; 5626185029Spjd break; 5627185029Spjd } 5628185029Spjd } 5629185029Spjd ASSERT(remdev != NULL); 5630185029Spjd 5631185029Spjd /* 5632185029Spjd * Remove device from global list 5633185029Spjd */ 5634185029Spjd list_remove(l2arc_dev_list, remdev); 5635185029Spjd l2arc_dev_last = NULL; /* may have been invalidated */ 5636185029Spjd atomic_dec_64(&l2arc_ndev); 5637185029Spjd mutex_exit(&l2arc_dev_mtx); 5638185029Spjd 5639185029Spjd /* 5640185029Spjd * Clear all buflists and ARC references. L2ARC device flush. 5641185029Spjd */ 5642185029Spjd l2arc_evict(remdev, 0, B_TRUE); 5643185029Spjd list_destroy(remdev->l2ad_buflist); 5644185029Spjd kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 5645185029Spjd kmem_free(remdev, sizeof (l2arc_dev_t)); 5646185029Spjd} 5647185029Spjd 5648185029Spjdvoid 5649185029Spjdl2arc_init(void) 5650185029Spjd{ 5651185029Spjd l2arc_thread_exit = 0; 5652185029Spjd l2arc_ndev = 0; 5653185029Spjd l2arc_writes_sent = 0; 5654185029Spjd l2arc_writes_done = 0; 5655185029Spjd 5656185029Spjd mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 5657185029Spjd cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 5658185029Spjd mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 5659185029Spjd mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 5660185029Spjd mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 5661185029Spjd 5662185029Spjd l2arc_dev_list = &L2ARC_dev_list; 5663185029Spjd l2arc_free_on_write = &L2ARC_free_on_write; 5664185029Spjd list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 5665185029Spjd offsetof(l2arc_dev_t, l2ad_node)); 5666185029Spjd list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 5667185029Spjd offsetof(l2arc_data_free_t, l2df_list_node)); 5668185029Spjd} 5669185029Spjd 5670185029Spjdvoid 5671185029Spjdl2arc_fini(void) 5672185029Spjd{ 5673185029Spjd /* 5674185029Spjd * This is called from dmu_fini(), which is called from spa_fini(); 5675185029Spjd * Because of this, we can assume that all l2arc devices have 5676185029Spjd * already been removed when the pools themselves were removed. 5677185029Spjd */ 5678185029Spjd 5679185029Spjd l2arc_do_free_on_write(); 5680185029Spjd 5681185029Spjd mutex_destroy(&l2arc_feed_thr_lock); 5682185029Spjd cv_destroy(&l2arc_feed_thr_cv); 5683185029Spjd mutex_destroy(&l2arc_dev_mtx); 5684185029Spjd mutex_destroy(&l2arc_buflist_mtx); 5685185029Spjd mutex_destroy(&l2arc_free_on_write_mtx); 5686185029Spjd 5687185029Spjd list_destroy(l2arc_dev_list); 5688185029Spjd list_destroy(l2arc_free_on_write); 5689185029Spjd} 5690185029Spjd 5691185029Spjdvoid 5692185029Spjdl2arc_start(void) 5693185029Spjd{ 5694209962Smm if (!(spa_mode_global & FWRITE)) 5695185029Spjd return; 5696185029Spjd 5697185029Spjd (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 5698185029Spjd TS_RUN, minclsyspri); 5699185029Spjd} 5700185029Spjd 5701185029Spjdvoid 5702185029Spjdl2arc_stop(void) 5703185029Spjd{ 5704209962Smm if (!(spa_mode_global & FWRITE)) 5705185029Spjd return; 5706185029Spjd 5707185029Spjd mutex_enter(&l2arc_feed_thr_lock); 5708185029Spjd cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 5709185029Spjd l2arc_thread_exit = 1; 5710185029Spjd while (l2arc_thread_exit != 0) 5711185029Spjd cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 5712185029Spjd mutex_exit(&l2arc_feed_thr_lock); 5713185029Spjd} 5714