arc.c revision 272527
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); 220168473SpjdSYSCTL_DECL(_vfs_zfs); 221217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_max, CTLFLAG_RDTUN, &zfs_arc_max, 0, 222168473Spjd "Maximum ARC size"); 223217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_min, CTLFLAG_RDTUN, &zfs_arc_min, 0, 224168473Spjd "Minimum ARC size"); 225269230SdelphijSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_average_blocksize, CTLFLAG_RDTUN, 226269230Sdelphij &zfs_arc_average_blocksize, 0, 227269230Sdelphij "ARC average blocksize"); 228270759Ssmh/* 229270759Ssmh * We don't have a tunable for arc_free_target due to the dependency on 230270759Ssmh * pagedaemon initialisation. 231270759Ssmh */ 232270759SsmhSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_free_target, 233270759Ssmh CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(u_int), 234270759Ssmh sysctl_vfs_zfs_arc_free_target, "IU", 235270759Ssmh "Desired number of free pages below which ARC triggers reclaim"); 236168404Spjd 237270759Ssmhstatic int 238270759Ssmhsysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS) 239270759Ssmh{ 240270759Ssmh u_int val; 241270759Ssmh int err; 242270759Ssmh 243270759Ssmh val = zfs_arc_free_target; 244270759Ssmh err = sysctl_handle_int(oidp, &val, 0, req); 245270759Ssmh if (err != 0 || req->newptr == NULL) 246270759Ssmh return (err); 247270759Ssmh 248272483Ssmh if (val < minfree) 249270759Ssmh return (EINVAL); 250272483Ssmh if (val > vm_cnt.v_page_count) 251270759Ssmh return (EINVAL); 252270759Ssmh 253270759Ssmh zfs_arc_free_target = val; 254270759Ssmh 255270759Ssmh return (0); 256270759Ssmh} 257272483Ssmh#endif 258270759Ssmh 259168404Spjd/* 260185029Spjd * Note that buffers can be in one of 6 states: 261168404Spjd * ARC_anon - anonymous (discussed below) 262168404Spjd * ARC_mru - recently used, currently cached 263168404Spjd * ARC_mru_ghost - recentely used, no longer in cache 264168404Spjd * ARC_mfu - frequently used, currently cached 265168404Spjd * ARC_mfu_ghost - frequently used, no longer in cache 266185029Spjd * ARC_l2c_only - exists in L2ARC but not other states 267185029Spjd * When there are no active references to the buffer, they are 268185029Spjd * are linked onto a list in one of these arc states. These are 269185029Spjd * the only buffers that can be evicted or deleted. Within each 270185029Spjd * state there are multiple lists, one for meta-data and one for 271185029Spjd * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 272185029Spjd * etc.) is tracked separately so that it can be managed more 273185029Spjd * explicitly: favored over data, limited explicitly. 274168404Spjd * 275168404Spjd * Anonymous buffers are buffers that are not associated with 276168404Spjd * a DVA. These are buffers that hold dirty block copies 277168404Spjd * before they are written to stable storage. By definition, 278168404Spjd * they are "ref'd" and are considered part of arc_mru 279168404Spjd * that cannot be freed. Generally, they will aquire a DVA 280168404Spjd * as they are written and migrate onto the arc_mru list. 281185029Spjd * 282185029Spjd * The ARC_l2c_only state is for buffers that are in the second 283185029Spjd * level ARC but no longer in any of the ARC_m* lists. The second 284185029Spjd * level ARC itself may also contain buffers that are in any of 285185029Spjd * the ARC_m* states - meaning that a buffer can exist in two 286185029Spjd * places. The reason for the ARC_l2c_only state is to keep the 287185029Spjd * buffer header in the hash table, so that reads that hit the 288185029Spjd * second level ARC benefit from these fast lookups. 289168404Spjd */ 290168404Spjd 291205264Skmacy#define ARCS_LOCK_PAD CACHE_LINE_SIZE 292205231Skmacystruct arcs_lock { 293205231Skmacy kmutex_t arcs_lock; 294205231Skmacy#ifdef _KERNEL 295205231Skmacy unsigned char pad[(ARCS_LOCK_PAD - sizeof (kmutex_t))]; 296205231Skmacy#endif 297205231Skmacy}; 298205231Skmacy 299205231Skmacy/* 300205231Skmacy * must be power of two for mask use to work 301205231Skmacy * 302205231Skmacy */ 303205231Skmacy#define ARC_BUFC_NUMDATALISTS 16 304205231Skmacy#define ARC_BUFC_NUMMETADATALISTS 16 305206796Spjd#define ARC_BUFC_NUMLISTS (ARC_BUFC_NUMMETADATALISTS + ARC_BUFC_NUMDATALISTS) 306205231Skmacy 307168404Spjdtypedef struct arc_state { 308185029Spjd uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 309185029Spjd uint64_t arcs_size; /* total amount of data in this state */ 310205231Skmacy list_t arcs_lists[ARC_BUFC_NUMLISTS]; /* list of evictable buffers */ 311205264Skmacy struct arcs_lock arcs_locks[ARC_BUFC_NUMLISTS] __aligned(CACHE_LINE_SIZE); 312168404Spjd} arc_state_t; 313168404Spjd 314206796Spjd#define ARCS_LOCK(s, i) (&((s)->arcs_locks[(i)].arcs_lock)) 315205231Skmacy 316185029Spjd/* The 6 states: */ 317168404Spjdstatic arc_state_t ARC_anon; 318168404Spjdstatic arc_state_t ARC_mru; 319168404Spjdstatic arc_state_t ARC_mru_ghost; 320168404Spjdstatic arc_state_t ARC_mfu; 321168404Spjdstatic arc_state_t ARC_mfu_ghost; 322185029Spjdstatic arc_state_t ARC_l2c_only; 323168404Spjd 324168404Spjdtypedef struct arc_stats { 325168404Spjd kstat_named_t arcstat_hits; 326168404Spjd kstat_named_t arcstat_misses; 327168404Spjd kstat_named_t arcstat_demand_data_hits; 328168404Spjd kstat_named_t arcstat_demand_data_misses; 329168404Spjd kstat_named_t arcstat_demand_metadata_hits; 330168404Spjd kstat_named_t arcstat_demand_metadata_misses; 331168404Spjd kstat_named_t arcstat_prefetch_data_hits; 332168404Spjd kstat_named_t arcstat_prefetch_data_misses; 333168404Spjd kstat_named_t arcstat_prefetch_metadata_hits; 334168404Spjd kstat_named_t arcstat_prefetch_metadata_misses; 335168404Spjd kstat_named_t arcstat_mru_hits; 336168404Spjd kstat_named_t arcstat_mru_ghost_hits; 337168404Spjd kstat_named_t arcstat_mfu_hits; 338168404Spjd kstat_named_t arcstat_mfu_ghost_hits; 339205231Skmacy kstat_named_t arcstat_allocated; 340168404Spjd kstat_named_t arcstat_deleted; 341205231Skmacy kstat_named_t arcstat_stolen; 342168404Spjd kstat_named_t arcstat_recycle_miss; 343251629Sdelphij /* 344251629Sdelphij * Number of buffers that could not be evicted because the hash lock 345251629Sdelphij * was held by another thread. The lock may not necessarily be held 346251629Sdelphij * by something using the same buffer, since hash locks are shared 347251629Sdelphij * by multiple buffers. 348251629Sdelphij */ 349168404Spjd kstat_named_t arcstat_mutex_miss; 350251629Sdelphij /* 351251629Sdelphij * Number of buffers skipped because they have I/O in progress, are 352251629Sdelphij * indrect prefetch buffers that have not lived long enough, or are 353251629Sdelphij * not from the spa we're trying to evict from. 354251629Sdelphij */ 355168404Spjd kstat_named_t arcstat_evict_skip; 356208373Smm kstat_named_t arcstat_evict_l2_cached; 357208373Smm kstat_named_t arcstat_evict_l2_eligible; 358208373Smm kstat_named_t arcstat_evict_l2_ineligible; 359168404Spjd kstat_named_t arcstat_hash_elements; 360168404Spjd kstat_named_t arcstat_hash_elements_max; 361168404Spjd kstat_named_t arcstat_hash_collisions; 362168404Spjd kstat_named_t arcstat_hash_chains; 363168404Spjd kstat_named_t arcstat_hash_chain_max; 364168404Spjd kstat_named_t arcstat_p; 365168404Spjd kstat_named_t arcstat_c; 366168404Spjd kstat_named_t arcstat_c_min; 367168404Spjd kstat_named_t arcstat_c_max; 368168404Spjd kstat_named_t arcstat_size; 369185029Spjd kstat_named_t arcstat_hdr_size; 370208373Smm kstat_named_t arcstat_data_size; 371208373Smm kstat_named_t arcstat_other_size; 372185029Spjd kstat_named_t arcstat_l2_hits; 373185029Spjd kstat_named_t arcstat_l2_misses; 374185029Spjd kstat_named_t arcstat_l2_feeds; 375185029Spjd kstat_named_t arcstat_l2_rw_clash; 376208373Smm kstat_named_t arcstat_l2_read_bytes; 377208373Smm kstat_named_t arcstat_l2_write_bytes; 378185029Spjd kstat_named_t arcstat_l2_writes_sent; 379185029Spjd kstat_named_t arcstat_l2_writes_done; 380185029Spjd kstat_named_t arcstat_l2_writes_error; 381185029Spjd kstat_named_t arcstat_l2_writes_hdr_miss; 382185029Spjd kstat_named_t arcstat_l2_evict_lock_retry; 383185029Spjd kstat_named_t arcstat_l2_evict_reading; 384185029Spjd kstat_named_t arcstat_l2_free_on_write; 385185029Spjd kstat_named_t arcstat_l2_abort_lowmem; 386185029Spjd kstat_named_t arcstat_l2_cksum_bad; 387185029Spjd kstat_named_t arcstat_l2_io_error; 388185029Spjd kstat_named_t arcstat_l2_size; 389251478Sdelphij kstat_named_t arcstat_l2_asize; 390185029Spjd kstat_named_t arcstat_l2_hdr_size; 391251478Sdelphij kstat_named_t arcstat_l2_compress_successes; 392251478Sdelphij kstat_named_t arcstat_l2_compress_zeros; 393251478Sdelphij kstat_named_t arcstat_l2_compress_failures; 394205231Skmacy kstat_named_t arcstat_l2_write_trylock_fail; 395205231Skmacy kstat_named_t arcstat_l2_write_passed_headroom; 396205231Skmacy kstat_named_t arcstat_l2_write_spa_mismatch; 397206796Spjd kstat_named_t arcstat_l2_write_in_l2; 398205231Skmacy kstat_named_t arcstat_l2_write_hdr_io_in_progress; 399205231Skmacy kstat_named_t arcstat_l2_write_not_cacheable; 400205231Skmacy kstat_named_t arcstat_l2_write_full; 401205231Skmacy kstat_named_t arcstat_l2_write_buffer_iter; 402205231Skmacy kstat_named_t arcstat_l2_write_pios; 403205231Skmacy kstat_named_t arcstat_l2_write_buffer_bytes_scanned; 404205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_iter; 405205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_null_iter; 406242845Sdelphij kstat_named_t arcstat_memory_throttle_count; 407242845Sdelphij kstat_named_t arcstat_duplicate_buffers; 408242845Sdelphij kstat_named_t arcstat_duplicate_buffers_size; 409242845Sdelphij kstat_named_t arcstat_duplicate_reads; 410168404Spjd} arc_stats_t; 411168404Spjd 412168404Spjdstatic arc_stats_t arc_stats = { 413168404Spjd { "hits", KSTAT_DATA_UINT64 }, 414168404Spjd { "misses", KSTAT_DATA_UINT64 }, 415168404Spjd { "demand_data_hits", KSTAT_DATA_UINT64 }, 416168404Spjd { "demand_data_misses", KSTAT_DATA_UINT64 }, 417168404Spjd { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 418168404Spjd { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 419168404Spjd { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 420168404Spjd { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 421168404Spjd { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 422168404Spjd { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 423168404Spjd { "mru_hits", KSTAT_DATA_UINT64 }, 424168404Spjd { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 425168404Spjd { "mfu_hits", KSTAT_DATA_UINT64 }, 426168404Spjd { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 427205231Skmacy { "allocated", KSTAT_DATA_UINT64 }, 428168404Spjd { "deleted", KSTAT_DATA_UINT64 }, 429205231Skmacy { "stolen", KSTAT_DATA_UINT64 }, 430168404Spjd { "recycle_miss", KSTAT_DATA_UINT64 }, 431168404Spjd { "mutex_miss", KSTAT_DATA_UINT64 }, 432168404Spjd { "evict_skip", KSTAT_DATA_UINT64 }, 433208373Smm { "evict_l2_cached", KSTAT_DATA_UINT64 }, 434208373Smm { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 435208373Smm { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 436168404Spjd { "hash_elements", KSTAT_DATA_UINT64 }, 437168404Spjd { "hash_elements_max", KSTAT_DATA_UINT64 }, 438168404Spjd { "hash_collisions", KSTAT_DATA_UINT64 }, 439168404Spjd { "hash_chains", KSTAT_DATA_UINT64 }, 440168404Spjd { "hash_chain_max", KSTAT_DATA_UINT64 }, 441168404Spjd { "p", KSTAT_DATA_UINT64 }, 442168404Spjd { "c", KSTAT_DATA_UINT64 }, 443168404Spjd { "c_min", KSTAT_DATA_UINT64 }, 444168404Spjd { "c_max", KSTAT_DATA_UINT64 }, 445185029Spjd { "size", KSTAT_DATA_UINT64 }, 446185029Spjd { "hdr_size", KSTAT_DATA_UINT64 }, 447208373Smm { "data_size", KSTAT_DATA_UINT64 }, 448208373Smm { "other_size", KSTAT_DATA_UINT64 }, 449185029Spjd { "l2_hits", KSTAT_DATA_UINT64 }, 450185029Spjd { "l2_misses", KSTAT_DATA_UINT64 }, 451185029Spjd { "l2_feeds", KSTAT_DATA_UINT64 }, 452185029Spjd { "l2_rw_clash", KSTAT_DATA_UINT64 }, 453208373Smm { "l2_read_bytes", KSTAT_DATA_UINT64 }, 454208373Smm { "l2_write_bytes", KSTAT_DATA_UINT64 }, 455185029Spjd { "l2_writes_sent", KSTAT_DATA_UINT64 }, 456185029Spjd { "l2_writes_done", KSTAT_DATA_UINT64 }, 457185029Spjd { "l2_writes_error", KSTAT_DATA_UINT64 }, 458185029Spjd { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 459185029Spjd { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 460185029Spjd { "l2_evict_reading", KSTAT_DATA_UINT64 }, 461185029Spjd { "l2_free_on_write", KSTAT_DATA_UINT64 }, 462185029Spjd { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 463185029Spjd { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 464185029Spjd { "l2_io_error", KSTAT_DATA_UINT64 }, 465185029Spjd { "l2_size", KSTAT_DATA_UINT64 }, 466251478Sdelphij { "l2_asize", KSTAT_DATA_UINT64 }, 467185029Spjd { "l2_hdr_size", KSTAT_DATA_UINT64 }, 468251478Sdelphij { "l2_compress_successes", KSTAT_DATA_UINT64 }, 469251478Sdelphij { "l2_compress_zeros", KSTAT_DATA_UINT64 }, 470251478Sdelphij { "l2_compress_failures", KSTAT_DATA_UINT64 }, 471206796Spjd { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 472206796Spjd { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 473206796Spjd { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 474206796Spjd { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 475206796Spjd { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 476206796Spjd { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 477206796Spjd { "l2_write_full", KSTAT_DATA_UINT64 }, 478206796Spjd { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 479206796Spjd { "l2_write_pios", KSTAT_DATA_UINT64 }, 480206796Spjd { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 481206796Spjd { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 482242845Sdelphij { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 }, 483242845Sdelphij { "memory_throttle_count", KSTAT_DATA_UINT64 }, 484242845Sdelphij { "duplicate_buffers", KSTAT_DATA_UINT64 }, 485242845Sdelphij { "duplicate_buffers_size", KSTAT_DATA_UINT64 }, 486242845Sdelphij { "duplicate_reads", KSTAT_DATA_UINT64 } 487168404Spjd}; 488168404Spjd 489168404Spjd#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 490168404Spjd 491168404Spjd#define ARCSTAT_INCR(stat, val) \ 492251631Sdelphij atomic_add_64(&arc_stats.stat.value.ui64, (val)) 493168404Spjd 494206796Spjd#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 495168404Spjd#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 496168404Spjd 497168404Spjd#define ARCSTAT_MAX(stat, val) { \ 498168404Spjd uint64_t m; \ 499168404Spjd while ((val) > (m = arc_stats.stat.value.ui64) && \ 500168404Spjd (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 501168404Spjd continue; \ 502168404Spjd} 503168404Spjd 504168404Spjd#define ARCSTAT_MAXSTAT(stat) \ 505168404Spjd ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 506168404Spjd 507168404Spjd/* 508168404Spjd * We define a macro to allow ARC hits/misses to be easily broken down by 509168404Spjd * two separate conditions, giving a total of four different subtypes for 510168404Spjd * each of hits and misses (so eight statistics total). 511168404Spjd */ 512168404Spjd#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 513168404Spjd if (cond1) { \ 514168404Spjd if (cond2) { \ 515168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 516168404Spjd } else { \ 517168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 518168404Spjd } \ 519168404Spjd } else { \ 520168404Spjd if (cond2) { \ 521168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 522168404Spjd } else { \ 523168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 524168404Spjd } \ 525168404Spjd } 526168404Spjd 527168404Spjdkstat_t *arc_ksp; 528206796Spjdstatic arc_state_t *arc_anon; 529168404Spjdstatic arc_state_t *arc_mru; 530168404Spjdstatic arc_state_t *arc_mru_ghost; 531168404Spjdstatic arc_state_t *arc_mfu; 532168404Spjdstatic arc_state_t *arc_mfu_ghost; 533185029Spjdstatic arc_state_t *arc_l2c_only; 534168404Spjd 535168404Spjd/* 536168404Spjd * There are several ARC variables that are critical to export as kstats -- 537168404Spjd * but we don't want to have to grovel around in the kstat whenever we wish to 538168404Spjd * manipulate them. For these variables, we therefore define them to be in 539168404Spjd * terms of the statistic variable. This assures that we are not introducing 540168404Spjd * the possibility of inconsistency by having shadow copies of the variables, 541168404Spjd * while still allowing the code to be readable. 542168404Spjd */ 543168404Spjd#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 544168404Spjd#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 545168404Spjd#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 546168404Spjd#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 547168404Spjd#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 548168404Spjd 549251478Sdelphij#define L2ARC_IS_VALID_COMPRESS(_c_) \ 550251478Sdelphij ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY) 551251478Sdelphij 552168404Spjdstatic int arc_no_grow; /* Don't try to grow cache size */ 553168404Spjdstatic uint64_t arc_tempreserve; 554209962Smmstatic uint64_t arc_loaned_bytes; 555185029Spjdstatic uint64_t arc_meta_used; 556185029Spjdstatic uint64_t arc_meta_limit; 557185029Spjdstatic uint64_t arc_meta_max = 0; 558229663SpjdSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_used, CTLFLAG_RD, &arc_meta_used, 0, 559229663Spjd "ARC metadata used"); 560229663SpjdSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_limit, CTLFLAG_RW, &arc_meta_limit, 0, 561229663Spjd "ARC metadata limit"); 562168404Spjd 563185029Spjdtypedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 564185029Spjd 565168404Spjdtypedef struct arc_callback arc_callback_t; 566168404Spjd 567168404Spjdstruct arc_callback { 568168404Spjd void *acb_private; 569168404Spjd arc_done_func_t *acb_done; 570168404Spjd arc_buf_t *acb_buf; 571168404Spjd zio_t *acb_zio_dummy; 572168404Spjd arc_callback_t *acb_next; 573168404Spjd}; 574168404Spjd 575168404Spjdtypedef struct arc_write_callback arc_write_callback_t; 576168404Spjd 577168404Spjdstruct arc_write_callback { 578168404Spjd void *awcb_private; 579168404Spjd arc_done_func_t *awcb_ready; 580258632Savg arc_done_func_t *awcb_physdone; 581168404Spjd arc_done_func_t *awcb_done; 582168404Spjd arc_buf_t *awcb_buf; 583168404Spjd}; 584168404Spjd 585168404Spjdstruct arc_buf_hdr { 586168404Spjd /* protected by hash lock */ 587168404Spjd dva_t b_dva; 588168404Spjd uint64_t b_birth; 589168404Spjd uint64_t b_cksum0; 590168404Spjd 591168404Spjd kmutex_t b_freeze_lock; 592168404Spjd zio_cksum_t *b_freeze_cksum; 593219089Spjd void *b_thawed; 594168404Spjd 595168404Spjd arc_buf_hdr_t *b_hash_next; 596168404Spjd arc_buf_t *b_buf; 597168404Spjd uint32_t b_flags; 598168404Spjd uint32_t b_datacnt; 599168404Spjd 600168404Spjd arc_callback_t *b_acb; 601168404Spjd kcondvar_t b_cv; 602168404Spjd 603168404Spjd /* immutable */ 604168404Spjd arc_buf_contents_t b_type; 605168404Spjd uint64_t b_size; 606209962Smm uint64_t b_spa; 607168404Spjd 608168404Spjd /* protected by arc state mutex */ 609168404Spjd arc_state_t *b_state; 610168404Spjd list_node_t b_arc_node; 611168404Spjd 612168404Spjd /* updated atomically */ 613168404Spjd clock_t b_arc_access; 614168404Spjd 615168404Spjd /* self protecting */ 616168404Spjd refcount_t b_refcnt; 617185029Spjd 618185029Spjd l2arc_buf_hdr_t *b_l2hdr; 619185029Spjd list_node_t b_l2node; 620168404Spjd}; 621168404Spjd 622168404Spjdstatic arc_buf_t *arc_eviction_list; 623168404Spjdstatic kmutex_t arc_eviction_mtx; 624168404Spjdstatic arc_buf_hdr_t arc_eviction_hdr; 625168404Spjdstatic void arc_get_data_buf(arc_buf_t *buf); 626168404Spjdstatic void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 627185029Spjdstatic int arc_evict_needed(arc_buf_contents_t type); 628209962Smmstatic void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes); 629240133Smm#ifdef illumos 630240133Smmstatic void arc_buf_watch(arc_buf_t *buf); 631240133Smm#endif /* illumos */ 632168404Spjd 633209962Smmstatic boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab); 634208373Smm 635168404Spjd#define GHOST_STATE(state) \ 636185029Spjd ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 637185029Spjd (state) == arc_l2c_only) 638168404Spjd 639168404Spjd/* 640168404Spjd * Private ARC flags. These flags are private ARC only flags that will show up 641168404Spjd * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 642168404Spjd * be passed in as arc_flags in things like arc_read. However, these flags 643168404Spjd * should never be passed and should only be set by ARC code. When adding new 644168404Spjd * public flags, make sure not to smash the private ones. 645168404Spjd */ 646168404Spjd 647168404Spjd#define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 648168404Spjd#define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 649168404Spjd#define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 650168404Spjd#define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 651168404Spjd#define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 652168404Spjd#define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 653185029Spjd#define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 654185029Spjd#define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 655185029Spjd#define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 656185029Spjd#define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 657168404Spjd 658168404Spjd#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 659168404Spjd#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 660168404Spjd#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 661208373Smm#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH) 662168404Spjd#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 663168404Spjd#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 664185029Spjd#define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 665185029Spjd#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 666185029Spjd#define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 667185029Spjd (hdr)->b_l2hdr != NULL) 668185029Spjd#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 669185029Spjd#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 670185029Spjd#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 671168404Spjd 672168404Spjd/* 673185029Spjd * Other sizes 674185029Spjd */ 675185029Spjd 676185029Spjd#define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 677185029Spjd#define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 678185029Spjd 679185029Spjd/* 680168404Spjd * Hash table routines 681168404Spjd */ 682168404Spjd 683205253Skmacy#define HT_LOCK_PAD CACHE_LINE_SIZE 684168404Spjd 685168404Spjdstruct ht_lock { 686168404Spjd kmutex_t ht_lock; 687168404Spjd#ifdef _KERNEL 688168404Spjd unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 689168404Spjd#endif 690168404Spjd}; 691168404Spjd 692168404Spjd#define BUF_LOCKS 256 693168404Spjdtypedef struct buf_hash_table { 694168404Spjd uint64_t ht_mask; 695168404Spjd arc_buf_hdr_t **ht_table; 696205264Skmacy struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 697168404Spjd} buf_hash_table_t; 698168404Spjd 699168404Spjdstatic buf_hash_table_t buf_hash_table; 700168404Spjd 701168404Spjd#define BUF_HASH_INDEX(spa, dva, birth) \ 702168404Spjd (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 703168404Spjd#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 704168404Spjd#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 705219089Spjd#define HDR_LOCK(hdr) \ 706219089Spjd (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 707168404Spjd 708168404Spjduint64_t zfs_crc64_table[256]; 709168404Spjd 710185029Spjd/* 711185029Spjd * Level 2 ARC 712185029Spjd */ 713185029Spjd 714208373Smm#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 715251478Sdelphij#define L2ARC_HEADROOM 2 /* num of writes */ 716251478Sdelphij/* 717251478Sdelphij * If we discover during ARC scan any buffers to be compressed, we boost 718251478Sdelphij * our headroom for the next scanning cycle by this percentage multiple. 719251478Sdelphij */ 720251478Sdelphij#define L2ARC_HEADROOM_BOOST 200 721208373Smm#define L2ARC_FEED_SECS 1 /* caching interval secs */ 722208373Smm#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 723185029Spjd 724185029Spjd#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 725185029Spjd#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 726185029Spjd 727251631Sdelphij/* L2ARC Performance Tunables */ 728185029Spjduint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 729185029Spjduint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 730185029Spjduint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 731251478Sdelphijuint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 732185029Spjduint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 733208373Smmuint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 734219089Spjdboolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 735208373Smmboolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 736208373Smmboolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 737185029Spjd 738217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 739205231Skmacy &l2arc_write_max, 0, "max write size"); 740217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 741205231Skmacy &l2arc_write_boost, 0, "extra write during warmup"); 742217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 743205231Skmacy &l2arc_headroom, 0, "number of dev writes"); 744217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 745205231Skmacy &l2arc_feed_secs, 0, "interval seconds"); 746217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 747208373Smm &l2arc_feed_min_ms, 0, "min interval milliseconds"); 748205231Skmacy 749205231SkmacySYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 750205231Skmacy &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 751208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 752208373Smm &l2arc_feed_again, 0, "turbo warmup"); 753208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 754208373Smm &l2arc_norw, 0, "no reads during writes"); 755205231Skmacy 756217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 757205231Skmacy &ARC_anon.arcs_size, 0, "size of anonymous state"); 758217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD, 759205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state"); 760217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD, 761205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state"); 762205231Skmacy 763217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 764205231Skmacy &ARC_mru.arcs_size, 0, "size of mru state"); 765217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD, 766205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state"); 767217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD, 768205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state"); 769205231Skmacy 770217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 771205231Skmacy &ARC_mru_ghost.arcs_size, 0, "size of mru ghost state"); 772217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD, 773205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 774205231Skmacy "size of metadata in mru ghost state"); 775217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD, 776205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 777205231Skmacy "size of data in mru ghost state"); 778205231Skmacy 779217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 780205231Skmacy &ARC_mfu.arcs_size, 0, "size of mfu state"); 781217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD, 782205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state"); 783217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD, 784205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state"); 785205231Skmacy 786217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 787205231Skmacy &ARC_mfu_ghost.arcs_size, 0, "size of mfu ghost state"); 788217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD, 789205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 790205231Skmacy "size of metadata in mfu ghost state"); 791217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD, 792205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 793205231Skmacy "size of data in mfu ghost state"); 794205231Skmacy 795217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 796205231Skmacy &ARC_l2c_only.arcs_size, 0, "size of mru state"); 797205231Skmacy 798185029Spjd/* 799185029Spjd * L2ARC Internals 800185029Spjd */ 801185029Spjdtypedef struct l2arc_dev { 802185029Spjd vdev_t *l2ad_vdev; /* vdev */ 803185029Spjd spa_t *l2ad_spa; /* spa */ 804185029Spjd uint64_t l2ad_hand; /* next write location */ 805185029Spjd uint64_t l2ad_start; /* first addr on device */ 806185029Spjd uint64_t l2ad_end; /* last addr on device */ 807185029Spjd uint64_t l2ad_evict; /* last addr eviction reached */ 808185029Spjd boolean_t l2ad_first; /* first sweep through */ 809208373Smm boolean_t l2ad_writing; /* currently writing */ 810185029Spjd list_t *l2ad_buflist; /* buffer list */ 811185029Spjd list_node_t l2ad_node; /* device list node */ 812185029Spjd} l2arc_dev_t; 813185029Spjd 814185029Spjdstatic list_t L2ARC_dev_list; /* device list */ 815185029Spjdstatic list_t *l2arc_dev_list; /* device list pointer */ 816185029Spjdstatic kmutex_t l2arc_dev_mtx; /* device list mutex */ 817185029Spjdstatic l2arc_dev_t *l2arc_dev_last; /* last device used */ 818185029Spjdstatic kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 819185029Spjdstatic list_t L2ARC_free_on_write; /* free after write buf list */ 820185029Spjdstatic list_t *l2arc_free_on_write; /* free after write list ptr */ 821185029Spjdstatic kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 822185029Spjdstatic uint64_t l2arc_ndev; /* number of devices */ 823185029Spjd 824185029Spjdtypedef struct l2arc_read_callback { 825251478Sdelphij arc_buf_t *l2rcb_buf; /* read buffer */ 826251478Sdelphij spa_t *l2rcb_spa; /* spa */ 827251478Sdelphij blkptr_t l2rcb_bp; /* original blkptr */ 828268123Sdelphij zbookmark_phys_t l2rcb_zb; /* original bookmark */ 829251478Sdelphij int l2rcb_flags; /* original flags */ 830251478Sdelphij enum zio_compress l2rcb_compress; /* applied compress */ 831185029Spjd} l2arc_read_callback_t; 832185029Spjd 833185029Spjdtypedef struct l2arc_write_callback { 834185029Spjd l2arc_dev_t *l2wcb_dev; /* device info */ 835185029Spjd arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 836185029Spjd} l2arc_write_callback_t; 837185029Spjd 838185029Spjdstruct l2arc_buf_hdr { 839185029Spjd /* protected by arc_buf_hdr mutex */ 840251478Sdelphij l2arc_dev_t *b_dev; /* L2ARC device */ 841251478Sdelphij uint64_t b_daddr; /* disk address, offset byte */ 842251478Sdelphij /* compression applied to buffer data */ 843251478Sdelphij enum zio_compress b_compress; 844251478Sdelphij /* real alloc'd buffer size depending on b_compress applied */ 845251478Sdelphij int b_asize; 846251478Sdelphij /* temporary buffer holder for in-flight compressed data */ 847251478Sdelphij void *b_tmp_cdata; 848185029Spjd}; 849185029Spjd 850185029Spjdtypedef struct l2arc_data_free { 851185029Spjd /* protected by l2arc_free_on_write_mtx */ 852185029Spjd void *l2df_data; 853185029Spjd size_t l2df_size; 854185029Spjd void (*l2df_func)(void *, size_t); 855185029Spjd list_node_t l2df_list_node; 856185029Spjd} l2arc_data_free_t; 857185029Spjd 858185029Spjdstatic kmutex_t l2arc_feed_thr_lock; 859185029Spjdstatic kcondvar_t l2arc_feed_thr_cv; 860185029Spjdstatic uint8_t l2arc_thread_exit; 861185029Spjd 862185029Spjdstatic void l2arc_read_done(zio_t *zio); 863185029Spjdstatic void l2arc_hdr_stat_add(void); 864185029Spjdstatic void l2arc_hdr_stat_remove(void); 865185029Spjd 866251478Sdelphijstatic boolean_t l2arc_compress_buf(l2arc_buf_hdr_t *l2hdr); 867251478Sdelphijstatic void l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, 868251478Sdelphij enum zio_compress c); 869251478Sdelphijstatic void l2arc_release_cdata_buf(arc_buf_hdr_t *ab); 870251478Sdelphij 871168404Spjdstatic uint64_t 872209962Smmbuf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 873168404Spjd{ 874168404Spjd uint8_t *vdva = (uint8_t *)dva; 875168404Spjd uint64_t crc = -1ULL; 876168404Spjd int i; 877168404Spjd 878168404Spjd ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 879168404Spjd 880168404Spjd for (i = 0; i < sizeof (dva_t); i++) 881168404Spjd crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 882168404Spjd 883209962Smm crc ^= (spa>>8) ^ birth; 884168404Spjd 885168404Spjd return (crc); 886168404Spjd} 887168404Spjd 888168404Spjd#define BUF_EMPTY(buf) \ 889168404Spjd ((buf)->b_dva.dva_word[0] == 0 && \ 890168404Spjd (buf)->b_dva.dva_word[1] == 0 && \ 891260150Sdelphij (buf)->b_cksum0 == 0) 892168404Spjd 893168404Spjd#define BUF_EQUAL(spa, dva, birth, buf) \ 894168404Spjd ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 895168404Spjd ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 896168404Spjd ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 897168404Spjd 898219089Spjdstatic void 899219089Spjdbuf_discard_identity(arc_buf_hdr_t *hdr) 900219089Spjd{ 901219089Spjd hdr->b_dva.dva_word[0] = 0; 902219089Spjd hdr->b_dva.dva_word[1] = 0; 903219089Spjd hdr->b_birth = 0; 904219089Spjd hdr->b_cksum0 = 0; 905219089Spjd} 906219089Spjd 907168404Spjdstatic arc_buf_hdr_t * 908268075Sdelphijbuf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 909168404Spjd{ 910268075Sdelphij const dva_t *dva = BP_IDENTITY(bp); 911268075Sdelphij uint64_t birth = BP_PHYSICAL_BIRTH(bp); 912168404Spjd uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 913168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 914168404Spjd arc_buf_hdr_t *buf; 915168404Spjd 916168404Spjd mutex_enter(hash_lock); 917168404Spjd for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 918168404Spjd buf = buf->b_hash_next) { 919168404Spjd if (BUF_EQUAL(spa, dva, birth, buf)) { 920168404Spjd *lockp = hash_lock; 921168404Spjd return (buf); 922168404Spjd } 923168404Spjd } 924168404Spjd mutex_exit(hash_lock); 925168404Spjd *lockp = NULL; 926168404Spjd return (NULL); 927168404Spjd} 928168404Spjd 929168404Spjd/* 930168404Spjd * Insert an entry into the hash table. If there is already an element 931168404Spjd * equal to elem in the hash table, then the already existing element 932168404Spjd * will be returned and the new element will not be inserted. 933168404Spjd * Otherwise returns NULL. 934168404Spjd */ 935168404Spjdstatic arc_buf_hdr_t * 936168404Spjdbuf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 937168404Spjd{ 938168404Spjd uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 939168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 940168404Spjd arc_buf_hdr_t *fbuf; 941168404Spjd uint32_t i; 942168404Spjd 943268075Sdelphij ASSERT(!DVA_IS_EMPTY(&buf->b_dva)); 944268075Sdelphij ASSERT(buf->b_birth != 0); 945168404Spjd ASSERT(!HDR_IN_HASH_TABLE(buf)); 946168404Spjd *lockp = hash_lock; 947168404Spjd mutex_enter(hash_lock); 948168404Spjd for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 949168404Spjd fbuf = fbuf->b_hash_next, i++) { 950168404Spjd if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 951168404Spjd return (fbuf); 952168404Spjd } 953168404Spjd 954168404Spjd buf->b_hash_next = buf_hash_table.ht_table[idx]; 955168404Spjd buf_hash_table.ht_table[idx] = buf; 956168404Spjd buf->b_flags |= ARC_IN_HASH_TABLE; 957168404Spjd 958168404Spjd /* collect some hash table performance data */ 959168404Spjd if (i > 0) { 960168404Spjd ARCSTAT_BUMP(arcstat_hash_collisions); 961168404Spjd if (i == 1) 962168404Spjd ARCSTAT_BUMP(arcstat_hash_chains); 963168404Spjd 964168404Spjd ARCSTAT_MAX(arcstat_hash_chain_max, i); 965168404Spjd } 966168404Spjd 967168404Spjd ARCSTAT_BUMP(arcstat_hash_elements); 968168404Spjd ARCSTAT_MAXSTAT(arcstat_hash_elements); 969168404Spjd 970168404Spjd return (NULL); 971168404Spjd} 972168404Spjd 973168404Spjdstatic void 974168404Spjdbuf_hash_remove(arc_buf_hdr_t *buf) 975168404Spjd{ 976168404Spjd arc_buf_hdr_t *fbuf, **bufp; 977168404Spjd uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 978168404Spjd 979168404Spjd ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 980168404Spjd ASSERT(HDR_IN_HASH_TABLE(buf)); 981168404Spjd 982168404Spjd bufp = &buf_hash_table.ht_table[idx]; 983168404Spjd while ((fbuf = *bufp) != buf) { 984168404Spjd ASSERT(fbuf != NULL); 985168404Spjd bufp = &fbuf->b_hash_next; 986168404Spjd } 987168404Spjd *bufp = buf->b_hash_next; 988168404Spjd buf->b_hash_next = NULL; 989168404Spjd buf->b_flags &= ~ARC_IN_HASH_TABLE; 990168404Spjd 991168404Spjd /* collect some hash table performance data */ 992168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_elements); 993168404Spjd 994168404Spjd if (buf_hash_table.ht_table[idx] && 995168404Spjd buf_hash_table.ht_table[idx]->b_hash_next == NULL) 996168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_chains); 997168404Spjd} 998168404Spjd 999168404Spjd/* 1000168404Spjd * Global data structures and functions for the buf kmem cache. 1001168404Spjd */ 1002168404Spjdstatic kmem_cache_t *hdr_cache; 1003168404Spjdstatic kmem_cache_t *buf_cache; 1004168404Spjd 1005168404Spjdstatic void 1006168404Spjdbuf_fini(void) 1007168404Spjd{ 1008168404Spjd int i; 1009168404Spjd 1010168404Spjd kmem_free(buf_hash_table.ht_table, 1011168404Spjd (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1012168404Spjd for (i = 0; i < BUF_LOCKS; i++) 1013168404Spjd mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 1014168404Spjd kmem_cache_destroy(hdr_cache); 1015168404Spjd kmem_cache_destroy(buf_cache); 1016168404Spjd} 1017168404Spjd 1018168404Spjd/* 1019168404Spjd * Constructor callback - called when the cache is empty 1020168404Spjd * and a new buf is requested. 1021168404Spjd */ 1022168404Spjd/* ARGSUSED */ 1023168404Spjdstatic int 1024168404Spjdhdr_cons(void *vbuf, void *unused, int kmflag) 1025168404Spjd{ 1026168404Spjd arc_buf_hdr_t *buf = vbuf; 1027168404Spjd 1028168404Spjd bzero(buf, sizeof (arc_buf_hdr_t)); 1029168404Spjd refcount_create(&buf->b_refcnt); 1030168404Spjd cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 1031185029Spjd mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1032208373Smm arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 1033185029Spjd 1034168404Spjd return (0); 1035168404Spjd} 1036168404Spjd 1037185029Spjd/* ARGSUSED */ 1038185029Spjdstatic int 1039185029Spjdbuf_cons(void *vbuf, void *unused, int kmflag) 1040185029Spjd{ 1041185029Spjd arc_buf_t *buf = vbuf; 1042185029Spjd 1043185029Spjd bzero(buf, sizeof (arc_buf_t)); 1044219089Spjd mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 1045208373Smm arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1046208373Smm 1047185029Spjd return (0); 1048185029Spjd} 1049185029Spjd 1050168404Spjd/* 1051168404Spjd * Destructor callback - called when a cached buf is 1052168404Spjd * no longer required. 1053168404Spjd */ 1054168404Spjd/* ARGSUSED */ 1055168404Spjdstatic void 1056168404Spjdhdr_dest(void *vbuf, void *unused) 1057168404Spjd{ 1058168404Spjd arc_buf_hdr_t *buf = vbuf; 1059168404Spjd 1060219089Spjd ASSERT(BUF_EMPTY(buf)); 1061168404Spjd refcount_destroy(&buf->b_refcnt); 1062168404Spjd cv_destroy(&buf->b_cv); 1063185029Spjd mutex_destroy(&buf->b_freeze_lock); 1064208373Smm arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 1065168404Spjd} 1066168404Spjd 1067185029Spjd/* ARGSUSED */ 1068185029Spjdstatic void 1069185029Spjdbuf_dest(void *vbuf, void *unused) 1070185029Spjd{ 1071185029Spjd arc_buf_t *buf = vbuf; 1072185029Spjd 1073219089Spjd mutex_destroy(&buf->b_evict_lock); 1074208373Smm arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1075185029Spjd} 1076185029Spjd 1077168404Spjd/* 1078168404Spjd * Reclaim callback -- invoked when memory is low. 1079168404Spjd */ 1080168404Spjd/* ARGSUSED */ 1081168404Spjdstatic void 1082168404Spjdhdr_recl(void *unused) 1083168404Spjd{ 1084168404Spjd dprintf("hdr_recl called\n"); 1085168404Spjd /* 1086168404Spjd * umem calls the reclaim func when we destroy the buf cache, 1087168404Spjd * which is after we do arc_fini(). 1088168404Spjd */ 1089168404Spjd if (!arc_dead) 1090168404Spjd cv_signal(&arc_reclaim_thr_cv); 1091168404Spjd} 1092168404Spjd 1093168404Spjdstatic void 1094168404Spjdbuf_init(void) 1095168404Spjd{ 1096168404Spjd uint64_t *ct; 1097168404Spjd uint64_t hsize = 1ULL << 12; 1098168404Spjd int i, j; 1099168404Spjd 1100168404Spjd /* 1101168404Spjd * The hash table is big enough to fill all of physical memory 1102269230Sdelphij * with an average block size of zfs_arc_average_blocksize (default 8K). 1103269230Sdelphij * By default, the table will take up 1104269230Sdelphij * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1105168404Spjd */ 1106269230Sdelphij while (hsize * zfs_arc_average_blocksize < (uint64_t)physmem * PAGESIZE) 1107168404Spjd hsize <<= 1; 1108168404Spjdretry: 1109168404Spjd buf_hash_table.ht_mask = hsize - 1; 1110168404Spjd buf_hash_table.ht_table = 1111168404Spjd kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1112168404Spjd if (buf_hash_table.ht_table == NULL) { 1113168404Spjd ASSERT(hsize > (1ULL << 8)); 1114168404Spjd hsize >>= 1; 1115168404Spjd goto retry; 1116168404Spjd } 1117168404Spjd 1118168404Spjd hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 1119168404Spjd 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 1120168404Spjd buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1121185029Spjd 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1122168404Spjd 1123168404Spjd for (i = 0; i < 256; i++) 1124168404Spjd for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1125168404Spjd *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1126168404Spjd 1127168404Spjd for (i = 0; i < BUF_LOCKS; i++) { 1128168404Spjd mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1129168404Spjd NULL, MUTEX_DEFAULT, NULL); 1130168404Spjd } 1131168404Spjd} 1132168404Spjd 1133168404Spjd#define ARC_MINTIME (hz>>4) /* 62 ms */ 1134168404Spjd 1135168404Spjdstatic void 1136168404Spjdarc_cksum_verify(arc_buf_t *buf) 1137168404Spjd{ 1138168404Spjd zio_cksum_t zc; 1139168404Spjd 1140168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1141168404Spjd return; 1142168404Spjd 1143168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1144168404Spjd if (buf->b_hdr->b_freeze_cksum == NULL || 1145168404Spjd (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 1146168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1147168404Spjd return; 1148168404Spjd } 1149168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1150168404Spjd if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 1151168404Spjd panic("buffer modified while frozen!"); 1152168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1153168404Spjd} 1154168404Spjd 1155185029Spjdstatic int 1156185029Spjdarc_cksum_equal(arc_buf_t *buf) 1157185029Spjd{ 1158185029Spjd zio_cksum_t zc; 1159185029Spjd int equal; 1160185029Spjd 1161185029Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1162185029Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1163185029Spjd equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 1164185029Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1165185029Spjd 1166185029Spjd return (equal); 1167185029Spjd} 1168185029Spjd 1169168404Spjdstatic void 1170185029Spjdarc_cksum_compute(arc_buf_t *buf, boolean_t force) 1171168404Spjd{ 1172185029Spjd if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 1173168404Spjd return; 1174168404Spjd 1175168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1176168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1177168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1178168404Spjd return; 1179168404Spjd } 1180168404Spjd buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 1181168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 1182168404Spjd buf->b_hdr->b_freeze_cksum); 1183168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1184240133Smm#ifdef illumos 1185240133Smm arc_buf_watch(buf); 1186240133Smm#endif /* illumos */ 1187168404Spjd} 1188168404Spjd 1189240133Smm#ifdef illumos 1190240133Smm#ifndef _KERNEL 1191240133Smmtypedef struct procctl { 1192240133Smm long cmd; 1193240133Smm prwatch_t prwatch; 1194240133Smm} procctl_t; 1195240133Smm#endif 1196240133Smm 1197240133Smm/* ARGSUSED */ 1198240133Smmstatic void 1199240133Smmarc_buf_unwatch(arc_buf_t *buf) 1200240133Smm{ 1201240133Smm#ifndef _KERNEL 1202240133Smm if (arc_watch) { 1203240133Smm int result; 1204240133Smm procctl_t ctl; 1205240133Smm ctl.cmd = PCWATCH; 1206240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1207240133Smm ctl.prwatch.pr_size = 0; 1208240133Smm ctl.prwatch.pr_wflags = 0; 1209240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1210240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1211240133Smm } 1212240133Smm#endif 1213240133Smm} 1214240133Smm 1215240133Smm/* ARGSUSED */ 1216240133Smmstatic void 1217240133Smmarc_buf_watch(arc_buf_t *buf) 1218240133Smm{ 1219240133Smm#ifndef _KERNEL 1220240133Smm if (arc_watch) { 1221240133Smm int result; 1222240133Smm procctl_t ctl; 1223240133Smm ctl.cmd = PCWATCH; 1224240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1225240133Smm ctl.prwatch.pr_size = buf->b_hdr->b_size; 1226240133Smm ctl.prwatch.pr_wflags = WA_WRITE; 1227240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1228240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1229240133Smm } 1230240133Smm#endif 1231240133Smm} 1232240133Smm#endif /* illumos */ 1233240133Smm 1234168404Spjdvoid 1235168404Spjdarc_buf_thaw(arc_buf_t *buf) 1236168404Spjd{ 1237185029Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1238185029Spjd if (buf->b_hdr->b_state != arc_anon) 1239185029Spjd panic("modifying non-anon buffer!"); 1240185029Spjd if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 1241185029Spjd panic("modifying buffer while i/o in progress!"); 1242185029Spjd arc_cksum_verify(buf); 1243185029Spjd } 1244168404Spjd 1245168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1246168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1247168404Spjd kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1248168404Spjd buf->b_hdr->b_freeze_cksum = NULL; 1249168404Spjd } 1250219089Spjd 1251219089Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1252219089Spjd if (buf->b_hdr->b_thawed) 1253219089Spjd kmem_free(buf->b_hdr->b_thawed, 1); 1254219089Spjd buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP); 1255219089Spjd } 1256219089Spjd 1257168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1258240133Smm 1259240133Smm#ifdef illumos 1260240133Smm arc_buf_unwatch(buf); 1261240133Smm#endif /* illumos */ 1262168404Spjd} 1263168404Spjd 1264168404Spjdvoid 1265168404Spjdarc_buf_freeze(arc_buf_t *buf) 1266168404Spjd{ 1267219089Spjd kmutex_t *hash_lock; 1268219089Spjd 1269168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1270168404Spjd return; 1271168404Spjd 1272219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1273219089Spjd mutex_enter(hash_lock); 1274219089Spjd 1275168404Spjd ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 1276168404Spjd buf->b_hdr->b_state == arc_anon); 1277185029Spjd arc_cksum_compute(buf, B_FALSE); 1278219089Spjd mutex_exit(hash_lock); 1279240133Smm 1280168404Spjd} 1281168404Spjd 1282168404Spjdstatic void 1283205231Skmacyget_buf_info(arc_buf_hdr_t *ab, arc_state_t *state, list_t **list, kmutex_t **lock) 1284205231Skmacy{ 1285205231Skmacy uint64_t buf_hashid = buf_hash(ab->b_spa, &ab->b_dva, ab->b_birth); 1286205231Skmacy 1287206796Spjd if (ab->b_type == ARC_BUFC_METADATA) 1288206796Spjd buf_hashid &= (ARC_BUFC_NUMMETADATALISTS - 1); 1289205231Skmacy else { 1290206796Spjd buf_hashid &= (ARC_BUFC_NUMDATALISTS - 1); 1291205231Skmacy buf_hashid += ARC_BUFC_NUMMETADATALISTS; 1292205231Skmacy } 1293205231Skmacy 1294205231Skmacy *list = &state->arcs_lists[buf_hashid]; 1295205231Skmacy *lock = ARCS_LOCK(state, buf_hashid); 1296205231Skmacy} 1297205231Skmacy 1298205231Skmacy 1299205231Skmacystatic void 1300168404Spjdadd_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1301168404Spjd{ 1302168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1303168404Spjd 1304168404Spjd if ((refcount_add(&ab->b_refcnt, tag) == 1) && 1305168404Spjd (ab->b_state != arc_anon)) { 1306206796Spjd uint64_t delta = ab->b_size * ab->b_datacnt; 1307206796Spjd uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 1308205231Skmacy list_t *list; 1309205231Skmacy kmutex_t *lock; 1310168404Spjd 1311205231Skmacy get_buf_info(ab, ab->b_state, &list, &lock); 1312205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1313205231Skmacy mutex_enter(lock); 1314168404Spjd ASSERT(list_link_active(&ab->b_arc_node)); 1315185029Spjd list_remove(list, ab); 1316168404Spjd if (GHOST_STATE(ab->b_state)) { 1317240415Smm ASSERT0(ab->b_datacnt); 1318168404Spjd ASSERT3P(ab->b_buf, ==, NULL); 1319168404Spjd delta = ab->b_size; 1320168404Spjd } 1321168404Spjd ASSERT(delta > 0); 1322185029Spjd ASSERT3U(*size, >=, delta); 1323185029Spjd atomic_add_64(size, -delta); 1324206794Spjd mutex_exit(lock); 1325185029Spjd /* remove the prefetch flag if we get a reference */ 1326168404Spjd if (ab->b_flags & ARC_PREFETCH) 1327168404Spjd ab->b_flags &= ~ARC_PREFETCH; 1328168404Spjd } 1329168404Spjd} 1330168404Spjd 1331168404Spjdstatic int 1332168404Spjdremove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1333168404Spjd{ 1334168404Spjd int cnt; 1335168404Spjd arc_state_t *state = ab->b_state; 1336168404Spjd 1337168404Spjd ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1338168404Spjd ASSERT(!GHOST_STATE(state)); 1339168404Spjd 1340168404Spjd if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 1341168404Spjd (state != arc_anon)) { 1342185029Spjd uint64_t *size = &state->arcs_lsize[ab->b_type]; 1343205231Skmacy list_t *list; 1344205231Skmacy kmutex_t *lock; 1345185029Spjd 1346205231Skmacy get_buf_info(ab, state, &list, &lock); 1347205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1348205231Skmacy mutex_enter(lock); 1349168404Spjd ASSERT(!list_link_active(&ab->b_arc_node)); 1350205231Skmacy list_insert_head(list, ab); 1351168404Spjd ASSERT(ab->b_datacnt > 0); 1352185029Spjd atomic_add_64(size, ab->b_size * ab->b_datacnt); 1353206794Spjd mutex_exit(lock); 1354168404Spjd } 1355168404Spjd return (cnt); 1356168404Spjd} 1357168404Spjd 1358168404Spjd/* 1359168404Spjd * Move the supplied buffer to the indicated state. The mutex 1360168404Spjd * for the buffer must be held by the caller. 1361168404Spjd */ 1362168404Spjdstatic void 1363168404Spjdarc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1364168404Spjd{ 1365168404Spjd arc_state_t *old_state = ab->b_state; 1366168404Spjd int64_t refcnt = refcount_count(&ab->b_refcnt); 1367168404Spjd uint64_t from_delta, to_delta; 1368205231Skmacy list_t *list; 1369205231Skmacy kmutex_t *lock; 1370168404Spjd 1371168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1372258632Savg ASSERT3P(new_state, !=, old_state); 1373168404Spjd ASSERT(refcnt == 0 || ab->b_datacnt > 0); 1374168404Spjd ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 1375219089Spjd ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon); 1376168404Spjd 1377168404Spjd from_delta = to_delta = ab->b_datacnt * ab->b_size; 1378168404Spjd 1379168404Spjd /* 1380168404Spjd * If this buffer is evictable, transfer it from the 1381168404Spjd * old state list to the new state list. 1382168404Spjd */ 1383168404Spjd if (refcnt == 0) { 1384168404Spjd if (old_state != arc_anon) { 1385205231Skmacy int use_mutex; 1386185029Spjd uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 1387168404Spjd 1388205231Skmacy get_buf_info(ab, old_state, &list, &lock); 1389205231Skmacy use_mutex = !MUTEX_HELD(lock); 1390168404Spjd if (use_mutex) 1391205231Skmacy mutex_enter(lock); 1392168404Spjd 1393168404Spjd ASSERT(list_link_active(&ab->b_arc_node)); 1394205231Skmacy list_remove(list, ab); 1395168404Spjd 1396168404Spjd /* 1397168404Spjd * If prefetching out of the ghost cache, 1398219089Spjd * we will have a non-zero datacnt. 1399168404Spjd */ 1400168404Spjd if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 1401168404Spjd /* ghost elements have a ghost size */ 1402168404Spjd ASSERT(ab->b_buf == NULL); 1403168404Spjd from_delta = ab->b_size; 1404168404Spjd } 1405185029Spjd ASSERT3U(*size, >=, from_delta); 1406185029Spjd atomic_add_64(size, -from_delta); 1407168404Spjd 1408168404Spjd if (use_mutex) 1409205231Skmacy mutex_exit(lock); 1410168404Spjd } 1411168404Spjd if (new_state != arc_anon) { 1412206796Spjd int use_mutex; 1413185029Spjd uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1414168404Spjd 1415205231Skmacy get_buf_info(ab, new_state, &list, &lock); 1416205231Skmacy use_mutex = !MUTEX_HELD(lock); 1417168404Spjd if (use_mutex) 1418205231Skmacy mutex_enter(lock); 1419168404Spjd 1420205231Skmacy list_insert_head(list, ab); 1421168404Spjd 1422168404Spjd /* ghost elements have a ghost size */ 1423168404Spjd if (GHOST_STATE(new_state)) { 1424168404Spjd ASSERT(ab->b_datacnt == 0); 1425168404Spjd ASSERT(ab->b_buf == NULL); 1426168404Spjd to_delta = ab->b_size; 1427168404Spjd } 1428185029Spjd atomic_add_64(size, to_delta); 1429168404Spjd 1430168404Spjd if (use_mutex) 1431205231Skmacy mutex_exit(lock); 1432168404Spjd } 1433168404Spjd } 1434168404Spjd 1435168404Spjd ASSERT(!BUF_EMPTY(ab)); 1436219089Spjd if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab)) 1437168404Spjd buf_hash_remove(ab); 1438168404Spjd 1439168404Spjd /* adjust state sizes */ 1440168404Spjd if (to_delta) 1441168404Spjd atomic_add_64(&new_state->arcs_size, to_delta); 1442168404Spjd if (from_delta) { 1443168404Spjd ASSERT3U(old_state->arcs_size, >=, from_delta); 1444168404Spjd atomic_add_64(&old_state->arcs_size, -from_delta); 1445168404Spjd } 1446168404Spjd ab->b_state = new_state; 1447185029Spjd 1448185029Spjd /* adjust l2arc hdr stats */ 1449185029Spjd if (new_state == arc_l2c_only) 1450185029Spjd l2arc_hdr_stat_add(); 1451185029Spjd else if (old_state == arc_l2c_only) 1452185029Spjd l2arc_hdr_stat_remove(); 1453168404Spjd} 1454168404Spjd 1455185029Spjdvoid 1456208373Smmarc_space_consume(uint64_t space, arc_space_type_t type) 1457185029Spjd{ 1458208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1459208373Smm 1460208373Smm switch (type) { 1461208373Smm case ARC_SPACE_DATA: 1462208373Smm ARCSTAT_INCR(arcstat_data_size, space); 1463208373Smm break; 1464208373Smm case ARC_SPACE_OTHER: 1465208373Smm ARCSTAT_INCR(arcstat_other_size, space); 1466208373Smm break; 1467208373Smm case ARC_SPACE_HDRS: 1468208373Smm ARCSTAT_INCR(arcstat_hdr_size, space); 1469208373Smm break; 1470208373Smm case ARC_SPACE_L2HDRS: 1471208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, space); 1472208373Smm break; 1473208373Smm } 1474208373Smm 1475185029Spjd atomic_add_64(&arc_meta_used, space); 1476185029Spjd atomic_add_64(&arc_size, space); 1477185029Spjd} 1478185029Spjd 1479185029Spjdvoid 1480208373Smmarc_space_return(uint64_t space, arc_space_type_t type) 1481185029Spjd{ 1482208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1483208373Smm 1484208373Smm switch (type) { 1485208373Smm case ARC_SPACE_DATA: 1486208373Smm ARCSTAT_INCR(arcstat_data_size, -space); 1487208373Smm break; 1488208373Smm case ARC_SPACE_OTHER: 1489208373Smm ARCSTAT_INCR(arcstat_other_size, -space); 1490208373Smm break; 1491208373Smm case ARC_SPACE_HDRS: 1492208373Smm ARCSTAT_INCR(arcstat_hdr_size, -space); 1493208373Smm break; 1494208373Smm case ARC_SPACE_L2HDRS: 1495208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 1496208373Smm break; 1497208373Smm } 1498208373Smm 1499185029Spjd ASSERT(arc_meta_used >= space); 1500185029Spjd if (arc_meta_max < arc_meta_used) 1501185029Spjd arc_meta_max = arc_meta_used; 1502185029Spjd atomic_add_64(&arc_meta_used, -space); 1503185029Spjd ASSERT(arc_size >= space); 1504185029Spjd atomic_add_64(&arc_size, -space); 1505185029Spjd} 1506185029Spjd 1507168404Spjdarc_buf_t * 1508168404Spjdarc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1509168404Spjd{ 1510168404Spjd arc_buf_hdr_t *hdr; 1511168404Spjd arc_buf_t *buf; 1512168404Spjd 1513168404Spjd ASSERT3U(size, >, 0); 1514185029Spjd hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1515168404Spjd ASSERT(BUF_EMPTY(hdr)); 1516168404Spjd hdr->b_size = size; 1517168404Spjd hdr->b_type = type; 1518228103Smm hdr->b_spa = spa_load_guid(spa); 1519168404Spjd hdr->b_state = arc_anon; 1520168404Spjd hdr->b_arc_access = 0; 1521185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1522168404Spjd buf->b_hdr = hdr; 1523168404Spjd buf->b_data = NULL; 1524168404Spjd buf->b_efunc = NULL; 1525168404Spjd buf->b_private = NULL; 1526168404Spjd buf->b_next = NULL; 1527168404Spjd hdr->b_buf = buf; 1528168404Spjd arc_get_data_buf(buf); 1529168404Spjd hdr->b_datacnt = 1; 1530168404Spjd hdr->b_flags = 0; 1531168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1532168404Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1533168404Spjd 1534168404Spjd return (buf); 1535168404Spjd} 1536168404Spjd 1537209962Smmstatic char *arc_onloan_tag = "onloan"; 1538209962Smm 1539209962Smm/* 1540209962Smm * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 1541209962Smm * flight data by arc_tempreserve_space() until they are "returned". Loaned 1542209962Smm * buffers must be returned to the arc before they can be used by the DMU or 1543209962Smm * freed. 1544209962Smm */ 1545209962Smmarc_buf_t * 1546209962Smmarc_loan_buf(spa_t *spa, int size) 1547209962Smm{ 1548209962Smm arc_buf_t *buf; 1549209962Smm 1550209962Smm buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 1551209962Smm 1552209962Smm atomic_add_64(&arc_loaned_bytes, size); 1553209962Smm return (buf); 1554209962Smm} 1555209962Smm 1556209962Smm/* 1557209962Smm * Return a loaned arc buffer to the arc. 1558209962Smm */ 1559209962Smmvoid 1560209962Smmarc_return_buf(arc_buf_t *buf, void *tag) 1561209962Smm{ 1562209962Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1563209962Smm 1564209962Smm ASSERT(buf->b_data != NULL); 1565219089Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1566219089Spjd (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag); 1567209962Smm 1568209962Smm atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 1569209962Smm} 1570209962Smm 1571219089Spjd/* Detach an arc_buf from a dbuf (tag) */ 1572219089Spjdvoid 1573219089Spjdarc_loan_inuse_buf(arc_buf_t *buf, void *tag) 1574219089Spjd{ 1575219089Spjd arc_buf_hdr_t *hdr; 1576219089Spjd 1577219089Spjd ASSERT(buf->b_data != NULL); 1578219089Spjd hdr = buf->b_hdr; 1579219089Spjd (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag); 1580219089Spjd (void) refcount_remove(&hdr->b_refcnt, tag); 1581219089Spjd buf->b_efunc = NULL; 1582219089Spjd buf->b_private = NULL; 1583219089Spjd 1584219089Spjd atomic_add_64(&arc_loaned_bytes, hdr->b_size); 1585219089Spjd} 1586219089Spjd 1587168404Spjdstatic arc_buf_t * 1588168404Spjdarc_buf_clone(arc_buf_t *from) 1589168404Spjd{ 1590168404Spjd arc_buf_t *buf; 1591168404Spjd arc_buf_hdr_t *hdr = from->b_hdr; 1592168404Spjd uint64_t size = hdr->b_size; 1593168404Spjd 1594219089Spjd ASSERT(hdr->b_state != arc_anon); 1595219089Spjd 1596185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1597168404Spjd buf->b_hdr = hdr; 1598168404Spjd buf->b_data = NULL; 1599168404Spjd buf->b_efunc = NULL; 1600168404Spjd buf->b_private = NULL; 1601168404Spjd buf->b_next = hdr->b_buf; 1602168404Spjd hdr->b_buf = buf; 1603168404Spjd arc_get_data_buf(buf); 1604168404Spjd bcopy(from->b_data, buf->b_data, size); 1605242845Sdelphij 1606242845Sdelphij /* 1607242845Sdelphij * This buffer already exists in the arc so create a duplicate 1608242845Sdelphij * copy for the caller. If the buffer is associated with user data 1609242845Sdelphij * then track the size and number of duplicates. These stats will be 1610242845Sdelphij * updated as duplicate buffers are created and destroyed. 1611242845Sdelphij */ 1612242845Sdelphij if (hdr->b_type == ARC_BUFC_DATA) { 1613242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_buffers); 1614242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, size); 1615242845Sdelphij } 1616168404Spjd hdr->b_datacnt += 1; 1617168404Spjd return (buf); 1618168404Spjd} 1619168404Spjd 1620168404Spjdvoid 1621168404Spjdarc_buf_add_ref(arc_buf_t *buf, void* tag) 1622168404Spjd{ 1623168404Spjd arc_buf_hdr_t *hdr; 1624168404Spjd kmutex_t *hash_lock; 1625168404Spjd 1626168404Spjd /* 1627185029Spjd * Check to see if this buffer is evicted. Callers 1628185029Spjd * must verify b_data != NULL to know if the add_ref 1629185029Spjd * was successful. 1630168404Spjd */ 1631219089Spjd mutex_enter(&buf->b_evict_lock); 1632185029Spjd if (buf->b_data == NULL) { 1633219089Spjd mutex_exit(&buf->b_evict_lock); 1634168404Spjd return; 1635168404Spjd } 1636219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1637219089Spjd mutex_enter(hash_lock); 1638185029Spjd hdr = buf->b_hdr; 1639219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1640219089Spjd mutex_exit(&buf->b_evict_lock); 1641168404Spjd 1642168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 1643168404Spjd add_reference(hdr, hash_lock, tag); 1644208373Smm DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 1645168404Spjd arc_access(hdr, hash_lock); 1646168404Spjd mutex_exit(hash_lock); 1647168404Spjd ARCSTAT_BUMP(arcstat_hits); 1648168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 1649168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 1650168404Spjd data, metadata, hits); 1651168404Spjd} 1652168404Spjd 1653185029Spjd/* 1654185029Spjd * Free the arc data buffer. If it is an l2arc write in progress, 1655185029Spjd * the buffer is placed on l2arc_free_on_write to be freed later. 1656185029Spjd */ 1657168404Spjdstatic void 1658240133Smmarc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t)) 1659185029Spjd{ 1660240133Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1661240133Smm 1662185029Spjd if (HDR_L2_WRITING(hdr)) { 1663185029Spjd l2arc_data_free_t *df; 1664185029Spjd df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 1665240133Smm df->l2df_data = buf->b_data; 1666240133Smm df->l2df_size = hdr->b_size; 1667185029Spjd df->l2df_func = free_func; 1668185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 1669185029Spjd list_insert_head(l2arc_free_on_write, df); 1670185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 1671185029Spjd ARCSTAT_BUMP(arcstat_l2_free_on_write); 1672185029Spjd } else { 1673240133Smm free_func(buf->b_data, hdr->b_size); 1674185029Spjd } 1675185029Spjd} 1676185029Spjd 1677268858Sdelphij/* 1678268858Sdelphij * Free up buf->b_data and if 'remove' is set, then pull the 1679268858Sdelphij * arc_buf_t off of the the arc_buf_hdr_t's list and free it. 1680268858Sdelphij */ 1681185029Spjdstatic void 1682268858Sdelphijarc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t remove) 1683168404Spjd{ 1684168404Spjd arc_buf_t **bufp; 1685168404Spjd 1686168404Spjd /* free up data associated with the buf */ 1687168404Spjd if (buf->b_data) { 1688168404Spjd arc_state_t *state = buf->b_hdr->b_state; 1689168404Spjd uint64_t size = buf->b_hdr->b_size; 1690168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 1691168404Spjd 1692168404Spjd arc_cksum_verify(buf); 1693240133Smm#ifdef illumos 1694240133Smm arc_buf_unwatch(buf); 1695240133Smm#endif /* illumos */ 1696219089Spjd 1697168404Spjd if (!recycle) { 1698168404Spjd if (type == ARC_BUFC_METADATA) { 1699240133Smm arc_buf_data_free(buf, zio_buf_free); 1700208373Smm arc_space_return(size, ARC_SPACE_DATA); 1701168404Spjd } else { 1702168404Spjd ASSERT(type == ARC_BUFC_DATA); 1703240133Smm arc_buf_data_free(buf, zio_data_buf_free); 1704208373Smm ARCSTAT_INCR(arcstat_data_size, -size); 1705185029Spjd atomic_add_64(&arc_size, -size); 1706168404Spjd } 1707168404Spjd } 1708168404Spjd if (list_link_active(&buf->b_hdr->b_arc_node)) { 1709185029Spjd uint64_t *cnt = &state->arcs_lsize[type]; 1710185029Spjd 1711168404Spjd ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 1712168404Spjd ASSERT(state != arc_anon); 1713185029Spjd 1714185029Spjd ASSERT3U(*cnt, >=, size); 1715185029Spjd atomic_add_64(cnt, -size); 1716168404Spjd } 1717168404Spjd ASSERT3U(state->arcs_size, >=, size); 1718168404Spjd atomic_add_64(&state->arcs_size, -size); 1719168404Spjd buf->b_data = NULL; 1720242845Sdelphij 1721242845Sdelphij /* 1722242845Sdelphij * If we're destroying a duplicate buffer make sure 1723242845Sdelphij * that the appropriate statistics are updated. 1724242845Sdelphij */ 1725242845Sdelphij if (buf->b_hdr->b_datacnt > 1 && 1726242845Sdelphij buf->b_hdr->b_type == ARC_BUFC_DATA) { 1727242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 1728242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size); 1729242845Sdelphij } 1730168404Spjd ASSERT(buf->b_hdr->b_datacnt > 0); 1731168404Spjd buf->b_hdr->b_datacnt -= 1; 1732168404Spjd } 1733168404Spjd 1734168404Spjd /* only remove the buf if requested */ 1735268858Sdelphij if (!remove) 1736168404Spjd return; 1737168404Spjd 1738168404Spjd /* remove the buf from the hdr list */ 1739168404Spjd for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 1740168404Spjd continue; 1741168404Spjd *bufp = buf->b_next; 1742219089Spjd buf->b_next = NULL; 1743168404Spjd 1744168404Spjd ASSERT(buf->b_efunc == NULL); 1745168404Spjd 1746168404Spjd /* clean up the buf */ 1747168404Spjd buf->b_hdr = NULL; 1748168404Spjd kmem_cache_free(buf_cache, buf); 1749168404Spjd} 1750168404Spjd 1751168404Spjdstatic void 1752168404Spjdarc_hdr_destroy(arc_buf_hdr_t *hdr) 1753168404Spjd{ 1754168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1755168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 1756168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1757219089Spjd l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 1758168404Spjd 1759219089Spjd if (l2hdr != NULL) { 1760219089Spjd boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx); 1761219089Spjd /* 1762219089Spjd * To prevent arc_free() and l2arc_evict() from 1763219089Spjd * attempting to free the same buffer at the same time, 1764219089Spjd * a FREE_IN_PROGRESS flag is given to arc_free() to 1765219089Spjd * give it priority. l2arc_evict() can't destroy this 1766219089Spjd * header while we are waiting on l2arc_buflist_mtx. 1767219089Spjd * 1768219089Spjd * The hdr may be removed from l2ad_buflist before we 1769219089Spjd * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 1770219089Spjd */ 1771219089Spjd if (!buflist_held) { 1772185029Spjd mutex_enter(&l2arc_buflist_mtx); 1773219089Spjd l2hdr = hdr->b_l2hdr; 1774219089Spjd } 1775219089Spjd 1776219089Spjd if (l2hdr != NULL) { 1777248572Ssmh trim_map_free(l2hdr->b_dev->l2ad_vdev, l2hdr->b_daddr, 1778248574Ssmh hdr->b_size, 0); 1779219089Spjd list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 1780219089Spjd ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 1781251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); 1782268085Sdelphij vdev_space_update(l2hdr->b_dev->l2ad_vdev, 1783268085Sdelphij -l2hdr->b_asize, 0, 0); 1784219089Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 1785219089Spjd if (hdr->b_state == arc_l2c_only) 1786219089Spjd l2arc_hdr_stat_remove(); 1787219089Spjd hdr->b_l2hdr = NULL; 1788219089Spjd } 1789219089Spjd 1790219089Spjd if (!buflist_held) 1791185029Spjd mutex_exit(&l2arc_buflist_mtx); 1792185029Spjd } 1793185029Spjd 1794168404Spjd if (!BUF_EMPTY(hdr)) { 1795168404Spjd ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1796219089Spjd buf_discard_identity(hdr); 1797168404Spjd } 1798168404Spjd while (hdr->b_buf) { 1799168404Spjd arc_buf_t *buf = hdr->b_buf; 1800168404Spjd 1801168404Spjd if (buf->b_efunc) { 1802168404Spjd mutex_enter(&arc_eviction_mtx); 1803219089Spjd mutex_enter(&buf->b_evict_lock); 1804168404Spjd ASSERT(buf->b_hdr != NULL); 1805168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 1806168404Spjd hdr->b_buf = buf->b_next; 1807168404Spjd buf->b_hdr = &arc_eviction_hdr; 1808168404Spjd buf->b_next = arc_eviction_list; 1809168404Spjd arc_eviction_list = buf; 1810219089Spjd mutex_exit(&buf->b_evict_lock); 1811168404Spjd mutex_exit(&arc_eviction_mtx); 1812168404Spjd } else { 1813168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 1814168404Spjd } 1815168404Spjd } 1816168404Spjd if (hdr->b_freeze_cksum != NULL) { 1817168404Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1818168404Spjd hdr->b_freeze_cksum = NULL; 1819168404Spjd } 1820219089Spjd if (hdr->b_thawed) { 1821219089Spjd kmem_free(hdr->b_thawed, 1); 1822219089Spjd hdr->b_thawed = NULL; 1823219089Spjd } 1824168404Spjd 1825168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 1826168404Spjd ASSERT3P(hdr->b_hash_next, ==, NULL); 1827168404Spjd ASSERT3P(hdr->b_acb, ==, NULL); 1828168404Spjd kmem_cache_free(hdr_cache, hdr); 1829168404Spjd} 1830168404Spjd 1831168404Spjdvoid 1832168404Spjdarc_buf_free(arc_buf_t *buf, void *tag) 1833168404Spjd{ 1834168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1835168404Spjd int hashed = hdr->b_state != arc_anon; 1836168404Spjd 1837168404Spjd ASSERT(buf->b_efunc == NULL); 1838168404Spjd ASSERT(buf->b_data != NULL); 1839168404Spjd 1840168404Spjd if (hashed) { 1841168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1842168404Spjd 1843168404Spjd mutex_enter(hash_lock); 1844219089Spjd hdr = buf->b_hdr; 1845219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1846219089Spjd 1847168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1848219089Spjd if (hdr->b_datacnt > 1) { 1849168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1850219089Spjd } else { 1851219089Spjd ASSERT(buf == hdr->b_buf); 1852219089Spjd ASSERT(buf->b_efunc == NULL); 1853168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 1854219089Spjd } 1855168404Spjd mutex_exit(hash_lock); 1856168404Spjd } else if (HDR_IO_IN_PROGRESS(hdr)) { 1857168404Spjd int destroy_hdr; 1858168404Spjd /* 1859168404Spjd * We are in the middle of an async write. Don't destroy 1860168404Spjd * this buffer unless the write completes before we finish 1861168404Spjd * decrementing the reference count. 1862168404Spjd */ 1863168404Spjd mutex_enter(&arc_eviction_mtx); 1864168404Spjd (void) remove_reference(hdr, NULL, tag); 1865168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1866168404Spjd destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 1867168404Spjd mutex_exit(&arc_eviction_mtx); 1868168404Spjd if (destroy_hdr) 1869168404Spjd arc_hdr_destroy(hdr); 1870168404Spjd } else { 1871219089Spjd if (remove_reference(hdr, NULL, tag) > 0) 1872168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1873219089Spjd else 1874168404Spjd arc_hdr_destroy(hdr); 1875168404Spjd } 1876168404Spjd} 1877168404Spjd 1878248571Smmboolean_t 1879168404Spjdarc_buf_remove_ref(arc_buf_t *buf, void* tag) 1880168404Spjd{ 1881168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1882168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1883248571Smm boolean_t no_callback = (buf->b_efunc == NULL); 1884168404Spjd 1885168404Spjd if (hdr->b_state == arc_anon) { 1886219089Spjd ASSERT(hdr->b_datacnt == 1); 1887168404Spjd arc_buf_free(buf, tag); 1888168404Spjd return (no_callback); 1889168404Spjd } 1890168404Spjd 1891168404Spjd mutex_enter(hash_lock); 1892219089Spjd hdr = buf->b_hdr; 1893219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1894168404Spjd ASSERT(hdr->b_state != arc_anon); 1895168404Spjd ASSERT(buf->b_data != NULL); 1896168404Spjd 1897168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1898168404Spjd if (hdr->b_datacnt > 1) { 1899168404Spjd if (no_callback) 1900168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1901168404Spjd } else if (no_callback) { 1902168404Spjd ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 1903219089Spjd ASSERT(buf->b_efunc == NULL); 1904168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 1905168404Spjd } 1906168404Spjd ASSERT(no_callback || hdr->b_datacnt > 1 || 1907168404Spjd refcount_is_zero(&hdr->b_refcnt)); 1908168404Spjd mutex_exit(hash_lock); 1909168404Spjd return (no_callback); 1910168404Spjd} 1911168404Spjd 1912168404Spjdint 1913168404Spjdarc_buf_size(arc_buf_t *buf) 1914168404Spjd{ 1915168404Spjd return (buf->b_hdr->b_size); 1916168404Spjd} 1917168404Spjd 1918168404Spjd/* 1919242845Sdelphij * Called from the DMU to determine if the current buffer should be 1920242845Sdelphij * evicted. In order to ensure proper locking, the eviction must be initiated 1921242845Sdelphij * from the DMU. Return true if the buffer is associated with user data and 1922242845Sdelphij * duplicate buffers still exist. 1923242845Sdelphij */ 1924242845Sdelphijboolean_t 1925242845Sdelphijarc_buf_eviction_needed(arc_buf_t *buf) 1926242845Sdelphij{ 1927242845Sdelphij arc_buf_hdr_t *hdr; 1928242845Sdelphij boolean_t evict_needed = B_FALSE; 1929242845Sdelphij 1930242845Sdelphij if (zfs_disable_dup_eviction) 1931242845Sdelphij return (B_FALSE); 1932242845Sdelphij 1933242845Sdelphij mutex_enter(&buf->b_evict_lock); 1934242845Sdelphij hdr = buf->b_hdr; 1935242845Sdelphij if (hdr == NULL) { 1936242845Sdelphij /* 1937242845Sdelphij * We are in arc_do_user_evicts(); let that function 1938242845Sdelphij * perform the eviction. 1939242845Sdelphij */ 1940242845Sdelphij ASSERT(buf->b_data == NULL); 1941242845Sdelphij mutex_exit(&buf->b_evict_lock); 1942242845Sdelphij return (B_FALSE); 1943242845Sdelphij } else if (buf->b_data == NULL) { 1944242845Sdelphij /* 1945242845Sdelphij * We have already been added to the arc eviction list; 1946242845Sdelphij * recommend eviction. 1947242845Sdelphij */ 1948242845Sdelphij ASSERT3P(hdr, ==, &arc_eviction_hdr); 1949242845Sdelphij mutex_exit(&buf->b_evict_lock); 1950242845Sdelphij return (B_TRUE); 1951242845Sdelphij } 1952242845Sdelphij 1953242845Sdelphij if (hdr->b_datacnt > 1 && hdr->b_type == ARC_BUFC_DATA) 1954242845Sdelphij evict_needed = B_TRUE; 1955242845Sdelphij 1956242845Sdelphij mutex_exit(&buf->b_evict_lock); 1957242845Sdelphij return (evict_needed); 1958242845Sdelphij} 1959242845Sdelphij 1960242845Sdelphij/* 1961168404Spjd * Evict buffers from list until we've removed the specified number of 1962168404Spjd * bytes. Move the removed buffers to the appropriate evict state. 1963168404Spjd * If the recycle flag is set, then attempt to "recycle" a buffer: 1964168404Spjd * - look for a buffer to evict that is `bytes' long. 1965168404Spjd * - return the data block from this buffer rather than freeing it. 1966168404Spjd * This flag is used by callers that are trying to make space for a 1967168404Spjd * new buffer in a full arc cache. 1968185029Spjd * 1969185029Spjd * This function makes a "best effort". It skips over any buffers 1970185029Spjd * it can't get a hash_lock on, and so may not catch all candidates. 1971185029Spjd * It may also return without evicting as much space as requested. 1972168404Spjd */ 1973168404Spjdstatic void * 1974209962Smmarc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 1975168404Spjd arc_buf_contents_t type) 1976168404Spjd{ 1977168404Spjd arc_state_t *evicted_state; 1978168404Spjd uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 1979205231Skmacy int64_t bytes_remaining; 1980168404Spjd arc_buf_hdr_t *ab, *ab_prev = NULL; 1981205231Skmacy list_t *evicted_list, *list, *evicted_list_start, *list_start; 1982205231Skmacy kmutex_t *lock, *evicted_lock; 1983168404Spjd kmutex_t *hash_lock; 1984168404Spjd boolean_t have_lock; 1985168404Spjd void *stolen = NULL; 1986258632Savg arc_buf_hdr_t marker = { 0 }; 1987258632Savg int count = 0; 1988205231Skmacy static int evict_metadata_offset, evict_data_offset; 1989258632Savg int i, idx, offset, list_count, lists; 1990168404Spjd 1991168404Spjd ASSERT(state == arc_mru || state == arc_mfu); 1992168404Spjd 1993168404Spjd evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1994206796Spjd 1995205231Skmacy if (type == ARC_BUFC_METADATA) { 1996205231Skmacy offset = 0; 1997205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 1998205231Skmacy list_start = &state->arcs_lists[0]; 1999205231Skmacy evicted_list_start = &evicted_state->arcs_lists[0]; 2000205231Skmacy idx = evict_metadata_offset; 2001205231Skmacy } else { 2002205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 2003205231Skmacy list_start = &state->arcs_lists[offset]; 2004205231Skmacy evicted_list_start = &evicted_state->arcs_lists[offset]; 2005205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 2006205231Skmacy idx = evict_data_offset; 2007205231Skmacy } 2008205231Skmacy bytes_remaining = evicted_state->arcs_lsize[type]; 2009258632Savg lists = 0; 2010206796Spjd 2011205231Skmacyevict_start: 2012205231Skmacy list = &list_start[idx]; 2013205231Skmacy evicted_list = &evicted_list_start[idx]; 2014205231Skmacy lock = ARCS_LOCK(state, (offset + idx)); 2015206796Spjd evicted_lock = ARCS_LOCK(evicted_state, (offset + idx)); 2016168404Spjd 2017205231Skmacy mutex_enter(lock); 2018205231Skmacy mutex_enter(evicted_lock); 2019205231Skmacy 2020185029Spjd for (ab = list_tail(list); ab; ab = ab_prev) { 2021185029Spjd ab_prev = list_prev(list, ab); 2022205231Skmacy bytes_remaining -= (ab->b_size * ab->b_datacnt); 2023168404Spjd /* prefetch buffers have a minimum lifespan */ 2024168404Spjd if (HDR_IO_IN_PROGRESS(ab) || 2025185029Spjd (spa && ab->b_spa != spa) || 2026168404Spjd (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 2027219089Spjd ddi_get_lbolt() - ab->b_arc_access < 2028219089Spjd arc_min_prefetch_lifespan)) { 2029168404Spjd skipped++; 2030168404Spjd continue; 2031168404Spjd } 2032168404Spjd /* "lookahead" for better eviction candidate */ 2033168404Spjd if (recycle && ab->b_size != bytes && 2034168404Spjd ab_prev && ab_prev->b_size == bytes) 2035168404Spjd continue; 2036258632Savg 2037258632Savg /* ignore markers */ 2038258632Savg if (ab->b_spa == 0) 2039258632Savg continue; 2040258632Savg 2041258632Savg /* 2042258632Savg * It may take a long time to evict all the bufs requested. 2043258632Savg * To avoid blocking all arc activity, periodically drop 2044258632Savg * the arcs_mtx and give other threads a chance to run 2045258632Savg * before reacquiring the lock. 2046258632Savg * 2047258632Savg * If we are looking for a buffer to recycle, we are in 2048258632Savg * the hot code path, so don't sleep. 2049258632Savg */ 2050258632Savg if (!recycle && count++ > arc_evict_iterations) { 2051258632Savg list_insert_after(list, ab, &marker); 2052258632Savg mutex_exit(evicted_lock); 2053258632Savg mutex_exit(lock); 2054258632Savg kpreempt(KPREEMPT_SYNC); 2055258632Savg mutex_enter(lock); 2056258632Savg mutex_enter(evicted_lock); 2057258632Savg ab_prev = list_prev(list, &marker); 2058258632Savg list_remove(list, &marker); 2059258632Savg count = 0; 2060258632Savg continue; 2061258632Savg } 2062258632Savg 2063168404Spjd hash_lock = HDR_LOCK(ab); 2064168404Spjd have_lock = MUTEX_HELD(hash_lock); 2065168404Spjd if (have_lock || mutex_tryenter(hash_lock)) { 2066240415Smm ASSERT0(refcount_count(&ab->b_refcnt)); 2067168404Spjd ASSERT(ab->b_datacnt > 0); 2068168404Spjd while (ab->b_buf) { 2069168404Spjd arc_buf_t *buf = ab->b_buf; 2070219089Spjd if (!mutex_tryenter(&buf->b_evict_lock)) { 2071185029Spjd missed += 1; 2072185029Spjd break; 2073185029Spjd } 2074168404Spjd if (buf->b_data) { 2075168404Spjd bytes_evicted += ab->b_size; 2076168404Spjd if (recycle && ab->b_type == type && 2077185029Spjd ab->b_size == bytes && 2078185029Spjd !HDR_L2_WRITING(ab)) { 2079168404Spjd stolen = buf->b_data; 2080168404Spjd recycle = FALSE; 2081168404Spjd } 2082168404Spjd } 2083168404Spjd if (buf->b_efunc) { 2084168404Spjd mutex_enter(&arc_eviction_mtx); 2085168404Spjd arc_buf_destroy(buf, 2086168404Spjd buf->b_data == stolen, FALSE); 2087168404Spjd ab->b_buf = buf->b_next; 2088168404Spjd buf->b_hdr = &arc_eviction_hdr; 2089168404Spjd buf->b_next = arc_eviction_list; 2090168404Spjd arc_eviction_list = buf; 2091168404Spjd mutex_exit(&arc_eviction_mtx); 2092219089Spjd mutex_exit(&buf->b_evict_lock); 2093168404Spjd } else { 2094219089Spjd mutex_exit(&buf->b_evict_lock); 2095168404Spjd arc_buf_destroy(buf, 2096168404Spjd buf->b_data == stolen, TRUE); 2097168404Spjd } 2098168404Spjd } 2099208373Smm 2100208373Smm if (ab->b_l2hdr) { 2101208373Smm ARCSTAT_INCR(arcstat_evict_l2_cached, 2102208373Smm ab->b_size); 2103208373Smm } else { 2104208373Smm if (l2arc_write_eligible(ab->b_spa, ab)) { 2105208373Smm ARCSTAT_INCR(arcstat_evict_l2_eligible, 2106208373Smm ab->b_size); 2107208373Smm } else { 2108208373Smm ARCSTAT_INCR( 2109208373Smm arcstat_evict_l2_ineligible, 2110208373Smm ab->b_size); 2111208373Smm } 2112208373Smm } 2113208373Smm 2114185029Spjd if (ab->b_datacnt == 0) { 2115185029Spjd arc_change_state(evicted_state, ab, hash_lock); 2116185029Spjd ASSERT(HDR_IN_HASH_TABLE(ab)); 2117185029Spjd ab->b_flags |= ARC_IN_HASH_TABLE; 2118185029Spjd ab->b_flags &= ~ARC_BUF_AVAILABLE; 2119185029Spjd DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 2120185029Spjd } 2121168404Spjd if (!have_lock) 2122168404Spjd mutex_exit(hash_lock); 2123168404Spjd if (bytes >= 0 && bytes_evicted >= bytes) 2124168404Spjd break; 2125205231Skmacy if (bytes_remaining > 0) { 2126205231Skmacy mutex_exit(evicted_lock); 2127205231Skmacy mutex_exit(lock); 2128206796Spjd idx = ((idx + 1) & (list_count - 1)); 2129258632Savg lists++; 2130205231Skmacy goto evict_start; 2131205231Skmacy } 2132168404Spjd } else { 2133168404Spjd missed += 1; 2134168404Spjd } 2135168404Spjd } 2136168404Spjd 2137205231Skmacy mutex_exit(evicted_lock); 2138205231Skmacy mutex_exit(lock); 2139206796Spjd 2140206796Spjd idx = ((idx + 1) & (list_count - 1)); 2141258632Savg lists++; 2142168404Spjd 2143205231Skmacy if (bytes_evicted < bytes) { 2144258632Savg if (lists < list_count) 2145205231Skmacy goto evict_start; 2146205231Skmacy else 2147205231Skmacy dprintf("only evicted %lld bytes from %x", 2148205231Skmacy (longlong_t)bytes_evicted, state); 2149205231Skmacy } 2150206796Spjd if (type == ARC_BUFC_METADATA) 2151205231Skmacy evict_metadata_offset = idx; 2152205231Skmacy else 2153205231Skmacy evict_data_offset = idx; 2154206796Spjd 2155168404Spjd if (skipped) 2156168404Spjd ARCSTAT_INCR(arcstat_evict_skip, skipped); 2157168404Spjd 2158168404Spjd if (missed) 2159168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, missed); 2160168404Spjd 2161185029Spjd /* 2162258632Savg * Note: we have just evicted some data into the ghost state, 2163258632Savg * potentially putting the ghost size over the desired size. Rather 2164258632Savg * that evicting from the ghost list in this hot code path, leave 2165258632Savg * this chore to the arc_reclaim_thread(). 2166185029Spjd */ 2167185029Spjd 2168205231Skmacy if (stolen) 2169205231Skmacy ARCSTAT_BUMP(arcstat_stolen); 2170168404Spjd return (stolen); 2171168404Spjd} 2172168404Spjd 2173168404Spjd/* 2174168404Spjd * Remove buffers from list until we've removed the specified number of 2175168404Spjd * bytes. Destroy the buffers that are removed. 2176168404Spjd */ 2177168404Spjdstatic void 2178209962Smmarc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 2179168404Spjd{ 2180168404Spjd arc_buf_hdr_t *ab, *ab_prev; 2181219089Spjd arc_buf_hdr_t marker = { 0 }; 2182205231Skmacy list_t *list, *list_start; 2183205231Skmacy kmutex_t *hash_lock, *lock; 2184168404Spjd uint64_t bytes_deleted = 0; 2185168404Spjd uint64_t bufs_skipped = 0; 2186258632Savg int count = 0; 2187205231Skmacy static int evict_offset; 2188205231Skmacy int list_count, idx = evict_offset; 2189258632Savg int offset, lists = 0; 2190168404Spjd 2191168404Spjd ASSERT(GHOST_STATE(state)); 2192205231Skmacy 2193205231Skmacy /* 2194205231Skmacy * data lists come after metadata lists 2195205231Skmacy */ 2196205231Skmacy list_start = &state->arcs_lists[ARC_BUFC_NUMMETADATALISTS]; 2197205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 2198205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 2199206796Spjd 2200205231Skmacyevict_start: 2201205231Skmacy list = &list_start[idx]; 2202205231Skmacy lock = ARCS_LOCK(state, idx + offset); 2203205231Skmacy 2204205231Skmacy mutex_enter(lock); 2205185029Spjd for (ab = list_tail(list); ab; ab = ab_prev) { 2206185029Spjd ab_prev = list_prev(list, ab); 2207258632Savg if (ab->b_type > ARC_BUFC_NUMTYPES) 2208258632Savg panic("invalid ab=%p", (void *)ab); 2209185029Spjd if (spa && ab->b_spa != spa) 2210185029Spjd continue; 2211219089Spjd 2212219089Spjd /* ignore markers */ 2213219089Spjd if (ab->b_spa == 0) 2214219089Spjd continue; 2215219089Spjd 2216168404Spjd hash_lock = HDR_LOCK(ab); 2217219089Spjd /* caller may be trying to modify this buffer, skip it */ 2218219089Spjd if (MUTEX_HELD(hash_lock)) 2219219089Spjd continue; 2220258632Savg 2221258632Savg /* 2222258632Savg * It may take a long time to evict all the bufs requested. 2223258632Savg * To avoid blocking all arc activity, periodically drop 2224258632Savg * the arcs_mtx and give other threads a chance to run 2225258632Savg * before reacquiring the lock. 2226258632Savg */ 2227258632Savg if (count++ > arc_evict_iterations) { 2228258632Savg list_insert_after(list, ab, &marker); 2229258632Savg mutex_exit(lock); 2230258632Savg kpreempt(KPREEMPT_SYNC); 2231258632Savg mutex_enter(lock); 2232258632Savg ab_prev = list_prev(list, &marker); 2233258632Savg list_remove(list, &marker); 2234258632Savg count = 0; 2235258632Savg continue; 2236258632Savg } 2237168404Spjd if (mutex_tryenter(hash_lock)) { 2238168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(ab)); 2239168404Spjd ASSERT(ab->b_buf == NULL); 2240168404Spjd ARCSTAT_BUMP(arcstat_deleted); 2241168404Spjd bytes_deleted += ab->b_size; 2242185029Spjd 2243185029Spjd if (ab->b_l2hdr != NULL) { 2244185029Spjd /* 2245185029Spjd * This buffer is cached on the 2nd Level ARC; 2246185029Spjd * don't destroy the header. 2247185029Spjd */ 2248185029Spjd arc_change_state(arc_l2c_only, ab, hash_lock); 2249185029Spjd mutex_exit(hash_lock); 2250185029Spjd } else { 2251185029Spjd arc_change_state(arc_anon, ab, hash_lock); 2252185029Spjd mutex_exit(hash_lock); 2253185029Spjd arc_hdr_destroy(ab); 2254185029Spjd } 2255185029Spjd 2256168404Spjd DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 2257168404Spjd if (bytes >= 0 && bytes_deleted >= bytes) 2258168404Spjd break; 2259219089Spjd } else if (bytes < 0) { 2260219089Spjd /* 2261219089Spjd * Insert a list marker and then wait for the 2262219089Spjd * hash lock to become available. Once its 2263219089Spjd * available, restart from where we left off. 2264219089Spjd */ 2265219089Spjd list_insert_after(list, ab, &marker); 2266219089Spjd mutex_exit(lock); 2267219089Spjd mutex_enter(hash_lock); 2268219089Spjd mutex_exit(hash_lock); 2269219089Spjd mutex_enter(lock); 2270219089Spjd ab_prev = list_prev(list, &marker); 2271219089Spjd list_remove(list, &marker); 2272258632Savg } else { 2273168404Spjd bufs_skipped += 1; 2274258632Savg } 2275258632Savg 2276168404Spjd } 2277205231Skmacy mutex_exit(lock); 2278206796Spjd idx = ((idx + 1) & (ARC_BUFC_NUMDATALISTS - 1)); 2279258632Savg lists++; 2280206796Spjd 2281258632Savg if (lists < list_count) 2282205231Skmacy goto evict_start; 2283206796Spjd 2284205231Skmacy evict_offset = idx; 2285205231Skmacy if ((uintptr_t)list > (uintptr_t)&state->arcs_lists[ARC_BUFC_NUMMETADATALISTS] && 2286185029Spjd (bytes < 0 || bytes_deleted < bytes)) { 2287205231Skmacy list_start = &state->arcs_lists[0]; 2288205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 2289258632Savg offset = lists = 0; 2290205231Skmacy goto evict_start; 2291185029Spjd } 2292185029Spjd 2293168404Spjd if (bufs_skipped) { 2294168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 2295168404Spjd ASSERT(bytes >= 0); 2296168404Spjd } 2297168404Spjd 2298168404Spjd if (bytes_deleted < bytes) 2299168404Spjd dprintf("only deleted %lld bytes from %p", 2300168404Spjd (longlong_t)bytes_deleted, state); 2301168404Spjd} 2302168404Spjd 2303168404Spjdstatic void 2304168404Spjdarc_adjust(void) 2305168404Spjd{ 2306208373Smm int64_t adjustment, delta; 2307168404Spjd 2308208373Smm /* 2309208373Smm * Adjust MRU size 2310208373Smm */ 2311168404Spjd 2312209275Smm adjustment = MIN((int64_t)(arc_size - arc_c), 2313209275Smm (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - 2314209275Smm arc_p)); 2315208373Smm 2316208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 2317208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 2318209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA); 2319208373Smm adjustment -= delta; 2320168404Spjd } 2321168404Spjd 2322208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2323208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 2324209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, 2325185029Spjd ARC_BUFC_METADATA); 2326185029Spjd } 2327185029Spjd 2328208373Smm /* 2329208373Smm * Adjust MFU size 2330208373Smm */ 2331168404Spjd 2332208373Smm adjustment = arc_size - arc_c; 2333208373Smm 2334208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 2335208373Smm delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 2336209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA); 2337208373Smm adjustment -= delta; 2338168404Spjd } 2339168404Spjd 2340208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2341208373Smm int64_t delta = MIN(adjustment, 2342208373Smm arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 2343209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, 2344208373Smm ARC_BUFC_METADATA); 2345208373Smm } 2346168404Spjd 2347208373Smm /* 2348208373Smm * Adjust ghost lists 2349208373Smm */ 2350168404Spjd 2351208373Smm adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 2352168404Spjd 2353208373Smm if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 2354208373Smm delta = MIN(arc_mru_ghost->arcs_size, adjustment); 2355209962Smm arc_evict_ghost(arc_mru_ghost, 0, delta); 2356208373Smm } 2357185029Spjd 2358208373Smm adjustment = 2359208373Smm arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 2360208373Smm 2361208373Smm if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 2362208373Smm delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 2363209962Smm arc_evict_ghost(arc_mfu_ghost, 0, delta); 2364168404Spjd } 2365168404Spjd} 2366168404Spjd 2367168404Spjdstatic void 2368168404Spjdarc_do_user_evicts(void) 2369168404Spjd{ 2370191903Skmacy static arc_buf_t *tmp_arc_eviction_list; 2371191903Skmacy 2372191903Skmacy /* 2373191903Skmacy * Move list over to avoid LOR 2374191903Skmacy */ 2375206796Spjdrestart: 2376168404Spjd mutex_enter(&arc_eviction_mtx); 2377191903Skmacy tmp_arc_eviction_list = arc_eviction_list; 2378191903Skmacy arc_eviction_list = NULL; 2379191903Skmacy mutex_exit(&arc_eviction_mtx); 2380191903Skmacy 2381191903Skmacy while (tmp_arc_eviction_list != NULL) { 2382191903Skmacy arc_buf_t *buf = tmp_arc_eviction_list; 2383191903Skmacy tmp_arc_eviction_list = buf->b_next; 2384219089Spjd mutex_enter(&buf->b_evict_lock); 2385168404Spjd buf->b_hdr = NULL; 2386219089Spjd mutex_exit(&buf->b_evict_lock); 2387168404Spjd 2388168404Spjd if (buf->b_efunc != NULL) 2389268858Sdelphij VERIFY0(buf->b_efunc(buf->b_private)); 2390168404Spjd 2391168404Spjd buf->b_efunc = NULL; 2392168404Spjd buf->b_private = NULL; 2393168404Spjd kmem_cache_free(buf_cache, buf); 2394168404Spjd } 2395191903Skmacy 2396191903Skmacy if (arc_eviction_list != NULL) 2397191903Skmacy goto restart; 2398168404Spjd} 2399168404Spjd 2400168404Spjd/* 2401185029Spjd * Flush all *evictable* data from the cache for the given spa. 2402168404Spjd * NOTE: this will not touch "active" (i.e. referenced) data. 2403168404Spjd */ 2404168404Spjdvoid 2405185029Spjdarc_flush(spa_t *spa) 2406168404Spjd{ 2407209962Smm uint64_t guid = 0; 2408209962Smm 2409209962Smm if (spa) 2410228103Smm guid = spa_load_guid(spa); 2411209962Smm 2412205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_DATA]) { 2413209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 2414185029Spjd if (spa) 2415185029Spjd break; 2416185029Spjd } 2417205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_METADATA]) { 2418209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 2419185029Spjd if (spa) 2420185029Spjd break; 2421185029Spjd } 2422205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_DATA]) { 2423209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 2424185029Spjd if (spa) 2425185029Spjd break; 2426185029Spjd } 2427205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_METADATA]) { 2428209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 2429185029Spjd if (spa) 2430185029Spjd break; 2431185029Spjd } 2432168404Spjd 2433209962Smm arc_evict_ghost(arc_mru_ghost, guid, -1); 2434209962Smm arc_evict_ghost(arc_mfu_ghost, guid, -1); 2435168404Spjd 2436168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2437168404Spjd arc_do_user_evicts(); 2438168404Spjd mutex_exit(&arc_reclaim_thr_lock); 2439185029Spjd ASSERT(spa || arc_eviction_list == NULL); 2440168404Spjd} 2441168404Spjd 2442168404Spjdvoid 2443168404Spjdarc_shrink(void) 2444168404Spjd{ 2445270759Ssmh 2446168404Spjd if (arc_c > arc_c_min) { 2447168404Spjd uint64_t to_free; 2448168404Spjd 2449272483Ssmh DTRACE_PROBE4(arc__shrink, uint64_t, arc_c, uint64_t, 2450272483Ssmh arc_c_min, uint64_t, arc_p, uint64_t, to_free); 2451168404Spjd#ifdef _KERNEL 2452168404Spjd to_free = arc_c >> arc_shrink_shift; 2453168404Spjd#else 2454168404Spjd to_free = arc_c >> arc_shrink_shift; 2455168404Spjd#endif 2456168404Spjd if (arc_c > arc_c_min + to_free) 2457168404Spjd atomic_add_64(&arc_c, -to_free); 2458168404Spjd else 2459168404Spjd arc_c = arc_c_min; 2460168404Spjd 2461168404Spjd atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 2462168404Spjd if (arc_c > arc_size) 2463168404Spjd arc_c = MAX(arc_size, arc_c_min); 2464168404Spjd if (arc_p > arc_c) 2465168404Spjd arc_p = (arc_c >> 1); 2466272483Ssmh 2467272483Ssmh DTRACE_PROBE2(arc__shrunk, uint64_t, arc_c, uint64_t, 2468272483Ssmh arc_p); 2469272483Ssmh 2470168404Spjd ASSERT(arc_c >= arc_c_min); 2471168404Spjd ASSERT((int64_t)arc_p >= 0); 2472168404Spjd } 2473168404Spjd 2474270759Ssmh if (arc_size > arc_c) { 2475270759Ssmh DTRACE_PROBE2(arc__shrink_adjust, uint64_t, arc_size, 2476270759Ssmh uint64_t, arc_c); 2477168404Spjd arc_adjust(); 2478270759Ssmh } 2479168404Spjd} 2480168404Spjd 2481185029Spjdstatic int needfree = 0; 2482168404Spjd 2483168404Spjdstatic int 2484168404Spjdarc_reclaim_needed(void) 2485168404Spjd{ 2486168404Spjd 2487168404Spjd#ifdef _KERNEL 2488219089Spjd 2489270759Ssmh if (needfree) { 2490270759Ssmh DTRACE_PROBE(arc__reclaim_needfree); 2491197816Skmacy return (1); 2492270759Ssmh } 2493168404Spjd 2494191902Skmacy /* 2495212780Savg * Cooperate with pagedaemon when it's time for it to scan 2496212780Savg * and reclaim some pages. 2497191902Skmacy */ 2498272483Ssmh if (freemem < zfs_arc_free_target) { 2499272483Ssmh DTRACE_PROBE2(arc__reclaim_freemem, uint64_t, 2500272483Ssmh freemem, uint64_t, zfs_arc_free_target); 2501191902Skmacy return (1); 2502270759Ssmh } 2503191902Skmacy 2504219089Spjd#ifdef sun 2505168404Spjd /* 2506185029Spjd * take 'desfree' extra pages, so we reclaim sooner, rather than later 2507185029Spjd */ 2508185029Spjd extra = desfree; 2509185029Spjd 2510185029Spjd /* 2511185029Spjd * check that we're out of range of the pageout scanner. It starts to 2512185029Spjd * schedule paging if freemem is less than lotsfree and needfree. 2513185029Spjd * lotsfree is the high-water mark for pageout, and needfree is the 2514185029Spjd * number of needed free pages. We add extra pages here to make sure 2515185029Spjd * the scanner doesn't start up while we're freeing memory. 2516185029Spjd */ 2517185029Spjd if (freemem < lotsfree + needfree + extra) 2518185029Spjd return (1); 2519185029Spjd 2520185029Spjd /* 2521168404Spjd * check to make sure that swapfs has enough space so that anon 2522185029Spjd * reservations can still succeed. anon_resvmem() checks that the 2523168404Spjd * availrmem is greater than swapfs_minfree, and the number of reserved 2524168404Spjd * swap pages. We also add a bit of extra here just to prevent 2525168404Spjd * circumstances from getting really dire. 2526168404Spjd */ 2527168404Spjd if (availrmem < swapfs_minfree + swapfs_reserve + extra) 2528168404Spjd return (1); 2529168404Spjd 2530168404Spjd /* 2531272483Ssmh * Check that we have enough availrmem that memory locking (e.g., via 2532272483Ssmh * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 2533272483Ssmh * stores the number of pages that cannot be locked; when availrmem 2534272483Ssmh * drops below pages_pp_maximum, page locking mechanisms such as 2535272483Ssmh * page_pp_lock() will fail.) 2536272483Ssmh */ 2537272483Ssmh if (availrmem <= pages_pp_maximum) 2538272483Ssmh return (1); 2539272483Ssmh 2540272483Ssmh#endif /* sun */ 2541272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 2542272483Ssmh /* 2543168404Spjd * If we're on an i386 platform, it's possible that we'll exhaust the 2544168404Spjd * kernel heap space before we ever run out of available physical 2545168404Spjd * memory. Most checks of the size of the heap_area compare against 2546168404Spjd * tune.t_minarmem, which is the minimum available real memory that we 2547168404Spjd * can have in the system. However, this is generally fixed at 25 pages 2548168404Spjd * which is so low that it's useless. In this comparison, we seek to 2549168404Spjd * calculate the total heap-size, and reclaim if more than 3/4ths of the 2550185029Spjd * heap is allocated. (Or, in the calculation, if less than 1/4th is 2551168404Spjd * free) 2552168404Spjd */ 2553272483Ssmh if (vmem_size(heap_arena, VMEM_FREE) < 2554272483Ssmh (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2)) { 2555270861Ssmh DTRACE_PROBE2(arc__reclaim_used, uint64_t, 2556272483Ssmh vmem_size(heap_arena, VMEM_FREE), uint64_t, 2557272483Ssmh (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2); 2558270861Ssmh return (1); 2559270861Ssmh } 2560270861Ssmh#endif 2561272483Ssmh#ifdef sun 2562272483Ssmh /* 2563272483Ssmh * If zio data pages are being allocated out of a separate heap segment, 2564272483Ssmh * then enforce that the size of available vmem for this arena remains 2565272483Ssmh * above about 1/16th free. 2566272483Ssmh * 2567272483Ssmh * Note: The 1/16th arena free requirement was put in place 2568272483Ssmh * to aggressively evict memory from the arc in order to avoid 2569272483Ssmh * memory fragmentation issues. 2570272483Ssmh */ 2571272483Ssmh if (zio_arena != NULL && 2572272483Ssmh vmem_size(zio_arena, VMEM_FREE) < 2573272483Ssmh (vmem_size(zio_arena, VMEM_ALLOC) >> 4)) 2574272483Ssmh return (1); 2575270871Speter#endif /* sun */ 2576272483Ssmh#else /* _KERNEL */ 2577168404Spjd if (spa_get_random(100) == 0) 2578168404Spjd return (1); 2579272483Ssmh#endif /* _KERNEL */ 2580270759Ssmh DTRACE_PROBE(arc__reclaim_no); 2581270759Ssmh 2582168404Spjd return (0); 2583168404Spjd} 2584168404Spjd 2585208454Spjdextern kmem_cache_t *zio_buf_cache[]; 2586208454Spjdextern kmem_cache_t *zio_data_buf_cache[]; 2587272527Sdelphijextern kmem_cache_t *range_seg_cache; 2588208454Spjd 2589272483Ssmhstatic void __noinline 2590168404Spjdarc_kmem_reap_now(arc_reclaim_strategy_t strat) 2591168404Spjd{ 2592168404Spjd size_t i; 2593168404Spjd kmem_cache_t *prev_cache = NULL; 2594168404Spjd kmem_cache_t *prev_data_cache = NULL; 2595168404Spjd 2596272483Ssmh DTRACE_PROBE(arc__kmem_reap_start); 2597168404Spjd#ifdef _KERNEL 2598185029Spjd if (arc_meta_used >= arc_meta_limit) { 2599185029Spjd /* 2600185029Spjd * We are exceeding our meta-data cache limit. 2601185029Spjd * Purge some DNLC entries to release holds on meta-data. 2602185029Spjd */ 2603185029Spjd dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 2604185029Spjd } 2605168404Spjd#if defined(__i386) 2606168404Spjd /* 2607168404Spjd * Reclaim unused memory from all kmem caches. 2608168404Spjd */ 2609168404Spjd kmem_reap(); 2610168404Spjd#endif 2611168404Spjd#endif 2612168404Spjd 2613168404Spjd /* 2614185029Spjd * An aggressive reclamation will shrink the cache size as well as 2615168404Spjd * reap free buffers from the arc kmem caches. 2616168404Spjd */ 2617168404Spjd if (strat == ARC_RECLAIM_AGGR) 2618168404Spjd arc_shrink(); 2619168404Spjd 2620168404Spjd for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2621168404Spjd if (zio_buf_cache[i] != prev_cache) { 2622168404Spjd prev_cache = zio_buf_cache[i]; 2623168404Spjd kmem_cache_reap_now(zio_buf_cache[i]); 2624168404Spjd } 2625168404Spjd if (zio_data_buf_cache[i] != prev_data_cache) { 2626168404Spjd prev_data_cache = zio_data_buf_cache[i]; 2627168404Spjd kmem_cache_reap_now(zio_data_buf_cache[i]); 2628168404Spjd } 2629168404Spjd } 2630168404Spjd kmem_cache_reap_now(buf_cache); 2631168404Spjd kmem_cache_reap_now(hdr_cache); 2632272506Sdelphij kmem_cache_reap_now(range_seg_cache); 2633272483Ssmh 2634272483Ssmh#ifdef sun 2635272483Ssmh /* 2636272483Ssmh * Ask the vmem arena to reclaim unused memory from its 2637272483Ssmh * quantum caches. 2638272483Ssmh */ 2639272483Ssmh if (zio_arena != NULL && strat == ARC_RECLAIM_AGGR) 2640272483Ssmh vmem_qcache_reap(zio_arena); 2641272483Ssmh#endif 2642272483Ssmh DTRACE_PROBE(arc__kmem_reap_end); 2643168404Spjd} 2644168404Spjd 2645168404Spjdstatic void 2646168404Spjdarc_reclaim_thread(void *dummy __unused) 2647168404Spjd{ 2648168404Spjd clock_t growtime = 0; 2649168404Spjd arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2650168404Spjd callb_cpr_t cpr; 2651168404Spjd 2652168404Spjd CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2653168404Spjd 2654168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2655168404Spjd while (arc_thread_exit == 0) { 2656168404Spjd if (arc_reclaim_needed()) { 2657168404Spjd 2658168404Spjd if (arc_no_grow) { 2659168404Spjd if (last_reclaim == ARC_RECLAIM_CONS) { 2660272483Ssmh DTRACE_PROBE(arc__reclaim_aggr_no_grow); 2661168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2662168404Spjd } else { 2663168404Spjd last_reclaim = ARC_RECLAIM_CONS; 2664168404Spjd } 2665168404Spjd } else { 2666168404Spjd arc_no_grow = TRUE; 2667168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2668272483Ssmh DTRACE_PROBE(arc__reclaim_aggr); 2669168404Spjd membar_producer(); 2670168404Spjd } 2671168404Spjd 2672168404Spjd /* reset the growth delay for every reclaim */ 2673219089Spjd growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 2674168404Spjd 2675185029Spjd if (needfree && last_reclaim == ARC_RECLAIM_CONS) { 2676168404Spjd /* 2677185029Spjd * If needfree is TRUE our vm_lowmem hook 2678168404Spjd * was called and in that case we must free some 2679168404Spjd * memory, so switch to aggressive mode. 2680168404Spjd */ 2681168404Spjd arc_no_grow = TRUE; 2682168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2683168404Spjd } 2684168404Spjd arc_kmem_reap_now(last_reclaim); 2685185029Spjd arc_warm = B_TRUE; 2686185029Spjd 2687219089Spjd } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 2688168404Spjd arc_no_grow = FALSE; 2689168404Spjd } 2690168404Spjd 2691209275Smm arc_adjust(); 2692168404Spjd 2693168404Spjd if (arc_eviction_list != NULL) 2694168404Spjd arc_do_user_evicts(); 2695168404Spjd 2696211762Savg#ifdef _KERNEL 2697211762Savg if (needfree) { 2698185029Spjd needfree = 0; 2699185029Spjd wakeup(&needfree); 2700211762Savg } 2701168404Spjd#endif 2702168404Spjd 2703168404Spjd /* block until needed, or one second, whichever is shorter */ 2704168404Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 2705168404Spjd (void) cv_timedwait(&arc_reclaim_thr_cv, 2706168404Spjd &arc_reclaim_thr_lock, hz); 2707168404Spjd CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2708168404Spjd } 2709168404Spjd 2710168404Spjd arc_thread_exit = 0; 2711168404Spjd cv_broadcast(&arc_reclaim_thr_cv); 2712168404Spjd CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2713168404Spjd thread_exit(); 2714168404Spjd} 2715168404Spjd 2716168404Spjd/* 2717168404Spjd * Adapt arc info given the number of bytes we are trying to add and 2718168404Spjd * the state that we are comming from. This function is only called 2719168404Spjd * when we are adding new content to the cache. 2720168404Spjd */ 2721168404Spjdstatic void 2722168404Spjdarc_adapt(int bytes, arc_state_t *state) 2723168404Spjd{ 2724168404Spjd int mult; 2725208373Smm uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 2726168404Spjd 2727185029Spjd if (state == arc_l2c_only) 2728185029Spjd return; 2729185029Spjd 2730168404Spjd ASSERT(bytes > 0); 2731168404Spjd /* 2732168404Spjd * Adapt the target size of the MRU list: 2733168404Spjd * - if we just hit in the MRU ghost list, then increase 2734168404Spjd * the target size of the MRU list. 2735168404Spjd * - if we just hit in the MFU ghost list, then increase 2736168404Spjd * the target size of the MFU list by decreasing the 2737168404Spjd * target size of the MRU list. 2738168404Spjd */ 2739168404Spjd if (state == arc_mru_ghost) { 2740168404Spjd mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 2741168404Spjd 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 2742209275Smm mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 2743168404Spjd 2744208373Smm arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 2745168404Spjd } else if (state == arc_mfu_ghost) { 2746208373Smm uint64_t delta; 2747208373Smm 2748168404Spjd mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 2749168404Spjd 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 2750209275Smm mult = MIN(mult, 10); 2751168404Spjd 2752208373Smm delta = MIN(bytes * mult, arc_p); 2753208373Smm arc_p = MAX(arc_p_min, arc_p - delta); 2754168404Spjd } 2755168404Spjd ASSERT((int64_t)arc_p >= 0); 2756168404Spjd 2757168404Spjd if (arc_reclaim_needed()) { 2758168404Spjd cv_signal(&arc_reclaim_thr_cv); 2759168404Spjd return; 2760168404Spjd } 2761168404Spjd 2762168404Spjd if (arc_no_grow) 2763168404Spjd return; 2764168404Spjd 2765168404Spjd if (arc_c >= arc_c_max) 2766168404Spjd return; 2767168404Spjd 2768168404Spjd /* 2769168404Spjd * If we're within (2 * maxblocksize) bytes of the target 2770168404Spjd * cache size, increment the target cache size 2771168404Spjd */ 2772168404Spjd if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 2773272483Ssmh DTRACE_PROBE1(arc__inc_adapt, int, bytes); 2774168404Spjd atomic_add_64(&arc_c, (int64_t)bytes); 2775168404Spjd if (arc_c > arc_c_max) 2776168404Spjd arc_c = arc_c_max; 2777168404Spjd else if (state == arc_anon) 2778168404Spjd atomic_add_64(&arc_p, (int64_t)bytes); 2779168404Spjd if (arc_p > arc_c) 2780168404Spjd arc_p = arc_c; 2781168404Spjd } 2782168404Spjd ASSERT((int64_t)arc_p >= 0); 2783168404Spjd} 2784168404Spjd 2785168404Spjd/* 2786168404Spjd * Check if the cache has reached its limits and eviction is required 2787168404Spjd * prior to insert. 2788168404Spjd */ 2789168404Spjdstatic int 2790185029Spjdarc_evict_needed(arc_buf_contents_t type) 2791168404Spjd{ 2792185029Spjd if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 2793185029Spjd return (1); 2794185029Spjd 2795168404Spjd if (arc_reclaim_needed()) 2796168404Spjd return (1); 2797168404Spjd 2798168404Spjd return (arc_size > arc_c); 2799168404Spjd} 2800168404Spjd 2801168404Spjd/* 2802168404Spjd * The buffer, supplied as the first argument, needs a data block. 2803168404Spjd * So, if we are at cache max, determine which cache should be victimized. 2804168404Spjd * We have the following cases: 2805168404Spjd * 2806168404Spjd * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2807168404Spjd * In this situation if we're out of space, but the resident size of the MFU is 2808168404Spjd * under the limit, victimize the MFU cache to satisfy this insertion request. 2809168404Spjd * 2810168404Spjd * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2811168404Spjd * Here, we've used up all of the available space for the MRU, so we need to 2812168404Spjd * evict from our own cache instead. Evict from the set of resident MRU 2813168404Spjd * entries. 2814168404Spjd * 2815168404Spjd * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2816168404Spjd * c minus p represents the MFU space in the cache, since p is the size of the 2817168404Spjd * cache that is dedicated to the MRU. In this situation there's still space on 2818168404Spjd * the MFU side, so the MRU side needs to be victimized. 2819168404Spjd * 2820168404Spjd * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2821168404Spjd * MFU's resident set is consuming more space than it has been allotted. In 2822168404Spjd * this situation, we must victimize our own cache, the MFU, for this insertion. 2823168404Spjd */ 2824168404Spjdstatic void 2825168404Spjdarc_get_data_buf(arc_buf_t *buf) 2826168404Spjd{ 2827168404Spjd arc_state_t *state = buf->b_hdr->b_state; 2828168404Spjd uint64_t size = buf->b_hdr->b_size; 2829168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 2830168404Spjd 2831168404Spjd arc_adapt(size, state); 2832168404Spjd 2833168404Spjd /* 2834168404Spjd * We have not yet reached cache maximum size, 2835168404Spjd * just allocate a new buffer. 2836168404Spjd */ 2837185029Spjd if (!arc_evict_needed(type)) { 2838168404Spjd if (type == ARC_BUFC_METADATA) { 2839168404Spjd buf->b_data = zio_buf_alloc(size); 2840208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2841168404Spjd } else { 2842168404Spjd ASSERT(type == ARC_BUFC_DATA); 2843168404Spjd buf->b_data = zio_data_buf_alloc(size); 2844208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2845185029Spjd atomic_add_64(&arc_size, size); 2846168404Spjd } 2847168404Spjd goto out; 2848168404Spjd } 2849168404Spjd 2850168404Spjd /* 2851168404Spjd * If we are prefetching from the mfu ghost list, this buffer 2852168404Spjd * will end up on the mru list; so steal space from there. 2853168404Spjd */ 2854168404Spjd if (state == arc_mfu_ghost) 2855168404Spjd state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 2856168404Spjd else if (state == arc_mru_ghost) 2857168404Spjd state = arc_mru; 2858168404Spjd 2859168404Spjd if (state == arc_mru || state == arc_anon) { 2860168404Spjd uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 2861208373Smm state = (arc_mfu->arcs_lsize[type] >= size && 2862185029Spjd arc_p > mru_used) ? arc_mfu : arc_mru; 2863168404Spjd } else { 2864168404Spjd /* MFU cases */ 2865168404Spjd uint64_t mfu_space = arc_c - arc_p; 2866208373Smm state = (arc_mru->arcs_lsize[type] >= size && 2867185029Spjd mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 2868168404Spjd } 2869209962Smm if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) { 2870168404Spjd if (type == ARC_BUFC_METADATA) { 2871168404Spjd buf->b_data = zio_buf_alloc(size); 2872208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2873168404Spjd } else { 2874168404Spjd ASSERT(type == ARC_BUFC_DATA); 2875168404Spjd buf->b_data = zio_data_buf_alloc(size); 2876208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2877185029Spjd atomic_add_64(&arc_size, size); 2878168404Spjd } 2879168404Spjd ARCSTAT_BUMP(arcstat_recycle_miss); 2880168404Spjd } 2881168404Spjd ASSERT(buf->b_data != NULL); 2882168404Spjdout: 2883168404Spjd /* 2884168404Spjd * Update the state size. Note that ghost states have a 2885168404Spjd * "ghost size" and so don't need to be updated. 2886168404Spjd */ 2887168404Spjd if (!GHOST_STATE(buf->b_hdr->b_state)) { 2888168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2889168404Spjd 2890168404Spjd atomic_add_64(&hdr->b_state->arcs_size, size); 2891168404Spjd if (list_link_active(&hdr->b_arc_node)) { 2892168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 2893185029Spjd atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2894168404Spjd } 2895168404Spjd /* 2896168404Spjd * If we are growing the cache, and we are adding anonymous 2897168404Spjd * data, and we have outgrown arc_p, update arc_p 2898168404Spjd */ 2899168404Spjd if (arc_size < arc_c && hdr->b_state == arc_anon && 2900168404Spjd arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 2901168404Spjd arc_p = MIN(arc_c, arc_p + size); 2902168404Spjd } 2903205231Skmacy ARCSTAT_BUMP(arcstat_allocated); 2904168404Spjd} 2905168404Spjd 2906168404Spjd/* 2907168404Spjd * This routine is called whenever a buffer is accessed. 2908168404Spjd * NOTE: the hash lock is dropped in this function. 2909168404Spjd */ 2910168404Spjdstatic void 2911168404Spjdarc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2912168404Spjd{ 2913219089Spjd clock_t now; 2914219089Spjd 2915168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 2916168404Spjd 2917168404Spjd if (buf->b_state == arc_anon) { 2918168404Spjd /* 2919168404Spjd * This buffer is not in the cache, and does not 2920168404Spjd * appear in our "ghost" list. Add the new buffer 2921168404Spjd * to the MRU state. 2922168404Spjd */ 2923168404Spjd 2924168404Spjd ASSERT(buf->b_arc_access == 0); 2925219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2926168404Spjd DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2927168404Spjd arc_change_state(arc_mru, buf, hash_lock); 2928168404Spjd 2929168404Spjd } else if (buf->b_state == arc_mru) { 2930219089Spjd now = ddi_get_lbolt(); 2931219089Spjd 2932168404Spjd /* 2933168404Spjd * If this buffer is here because of a prefetch, then either: 2934168404Spjd * - clear the flag if this is a "referencing" read 2935168404Spjd * (any subsequent access will bump this into the MFU state). 2936168404Spjd * or 2937168404Spjd * - move the buffer to the head of the list if this is 2938168404Spjd * another prefetch (to make it less likely to be evicted). 2939168404Spjd */ 2940168404Spjd if ((buf->b_flags & ARC_PREFETCH) != 0) { 2941168404Spjd if (refcount_count(&buf->b_refcnt) == 0) { 2942168404Spjd ASSERT(list_link_active(&buf->b_arc_node)); 2943168404Spjd } else { 2944168404Spjd buf->b_flags &= ~ARC_PREFETCH; 2945168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 2946168404Spjd } 2947219089Spjd buf->b_arc_access = now; 2948168404Spjd return; 2949168404Spjd } 2950168404Spjd 2951168404Spjd /* 2952168404Spjd * This buffer has been "accessed" only once so far, 2953168404Spjd * but it is still in the cache. Move it to the MFU 2954168404Spjd * state. 2955168404Spjd */ 2956219089Spjd if (now > buf->b_arc_access + ARC_MINTIME) { 2957168404Spjd /* 2958168404Spjd * More than 125ms have passed since we 2959168404Spjd * instantiated this buffer. Move it to the 2960168404Spjd * most frequently used state. 2961168404Spjd */ 2962219089Spjd buf->b_arc_access = now; 2963168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2964168404Spjd arc_change_state(arc_mfu, buf, hash_lock); 2965168404Spjd } 2966168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 2967168404Spjd } else if (buf->b_state == arc_mru_ghost) { 2968168404Spjd arc_state_t *new_state; 2969168404Spjd /* 2970168404Spjd * This buffer has been "accessed" recently, but 2971168404Spjd * was evicted from the cache. Move it to the 2972168404Spjd * MFU state. 2973168404Spjd */ 2974168404Spjd 2975168404Spjd if (buf->b_flags & ARC_PREFETCH) { 2976168404Spjd new_state = arc_mru; 2977168404Spjd if (refcount_count(&buf->b_refcnt) > 0) 2978168404Spjd buf->b_flags &= ~ARC_PREFETCH; 2979168404Spjd DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2980168404Spjd } else { 2981168404Spjd new_state = arc_mfu; 2982168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2983168404Spjd } 2984168404Spjd 2985219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2986168404Spjd arc_change_state(new_state, buf, hash_lock); 2987168404Spjd 2988168404Spjd ARCSTAT_BUMP(arcstat_mru_ghost_hits); 2989168404Spjd } else if (buf->b_state == arc_mfu) { 2990168404Spjd /* 2991168404Spjd * This buffer has been accessed more than once and is 2992168404Spjd * still in the cache. Keep it in the MFU state. 2993168404Spjd * 2994168404Spjd * NOTE: an add_reference() that occurred when we did 2995168404Spjd * the arc_read() will have kicked this off the list. 2996168404Spjd * If it was a prefetch, we will explicitly move it to 2997168404Spjd * the head of the list now. 2998168404Spjd */ 2999168404Spjd if ((buf->b_flags & ARC_PREFETCH) != 0) { 3000168404Spjd ASSERT(refcount_count(&buf->b_refcnt) == 0); 3001168404Spjd ASSERT(list_link_active(&buf->b_arc_node)); 3002168404Spjd } 3003168404Spjd ARCSTAT_BUMP(arcstat_mfu_hits); 3004219089Spjd buf->b_arc_access = ddi_get_lbolt(); 3005168404Spjd } else if (buf->b_state == arc_mfu_ghost) { 3006168404Spjd arc_state_t *new_state = arc_mfu; 3007168404Spjd /* 3008168404Spjd * This buffer has been accessed more than once but has 3009168404Spjd * been evicted from the cache. Move it back to the 3010168404Spjd * MFU state. 3011168404Spjd */ 3012168404Spjd 3013168404Spjd if (buf->b_flags & ARC_PREFETCH) { 3014168404Spjd /* 3015168404Spjd * This is a prefetch access... 3016168404Spjd * move this block back to the MRU state. 3017168404Spjd */ 3018240415Smm ASSERT0(refcount_count(&buf->b_refcnt)); 3019168404Spjd new_state = arc_mru; 3020168404Spjd } 3021168404Spjd 3022219089Spjd buf->b_arc_access = ddi_get_lbolt(); 3023168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 3024168404Spjd arc_change_state(new_state, buf, hash_lock); 3025168404Spjd 3026168404Spjd ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 3027185029Spjd } else if (buf->b_state == arc_l2c_only) { 3028185029Spjd /* 3029185029Spjd * This buffer is on the 2nd Level ARC. 3030185029Spjd */ 3031185029Spjd 3032219089Spjd buf->b_arc_access = ddi_get_lbolt(); 3033185029Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 3034185029Spjd arc_change_state(arc_mfu, buf, hash_lock); 3035168404Spjd } else { 3036168404Spjd ASSERT(!"invalid arc state"); 3037168404Spjd } 3038168404Spjd} 3039168404Spjd 3040168404Spjd/* a generic arc_done_func_t which you can use */ 3041168404Spjd/* ARGSUSED */ 3042168404Spjdvoid 3043168404Spjdarc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 3044168404Spjd{ 3045219089Spjd if (zio == NULL || zio->io_error == 0) 3046219089Spjd bcopy(buf->b_data, arg, buf->b_hdr->b_size); 3047248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 3048168404Spjd} 3049168404Spjd 3050185029Spjd/* a generic arc_done_func_t */ 3051168404Spjdvoid 3052168404Spjdarc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 3053168404Spjd{ 3054168404Spjd arc_buf_t **bufp = arg; 3055168404Spjd if (zio && zio->io_error) { 3056248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 3057168404Spjd *bufp = NULL; 3058168404Spjd } else { 3059168404Spjd *bufp = buf; 3060219089Spjd ASSERT(buf->b_data); 3061168404Spjd } 3062168404Spjd} 3063168404Spjd 3064168404Spjdstatic void 3065168404Spjdarc_read_done(zio_t *zio) 3066168404Spjd{ 3067268075Sdelphij arc_buf_hdr_t *hdr; 3068168404Spjd arc_buf_t *buf; 3069168404Spjd arc_buf_t *abuf; /* buffer we're assigning to callback */ 3070268075Sdelphij kmutex_t *hash_lock = NULL; 3071168404Spjd arc_callback_t *callback_list, *acb; 3072168404Spjd int freeable = FALSE; 3073168404Spjd 3074168404Spjd buf = zio->io_private; 3075168404Spjd hdr = buf->b_hdr; 3076168404Spjd 3077168404Spjd /* 3078168404Spjd * The hdr was inserted into hash-table and removed from lists 3079168404Spjd * prior to starting I/O. We should find this header, since 3080168404Spjd * it's in the hash table, and it should be legit since it's 3081168404Spjd * not possible to evict it during the I/O. The only possible 3082168404Spjd * reason for it not to be found is if we were freed during the 3083168404Spjd * read. 3084168404Spjd */ 3085268075Sdelphij if (HDR_IN_HASH_TABLE(hdr)) { 3086268075Sdelphij ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 3087268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[0], ==, 3088268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[0]); 3089268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[1], ==, 3090268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[1]); 3091168404Spjd 3092268075Sdelphij arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 3093268075Sdelphij &hash_lock); 3094168404Spjd 3095268075Sdelphij ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && 3096268075Sdelphij hash_lock == NULL) || 3097268075Sdelphij (found == hdr && 3098268075Sdelphij DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 3099268075Sdelphij (found == hdr && HDR_L2_READING(hdr))); 3100268075Sdelphij } 3101268075Sdelphij 3102185029Spjd hdr->b_flags &= ~ARC_L2_EVICTED; 3103185029Spjd if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 3104185029Spjd hdr->b_flags &= ~ARC_L2CACHE; 3105206796Spjd 3106168404Spjd /* byteswap if necessary */ 3107168404Spjd callback_list = hdr->b_acb; 3108168404Spjd ASSERT(callback_list != NULL); 3109209101Smm if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 3110236884Smm dmu_object_byteswap_t bswap = 3111236884Smm DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 3112185029Spjd arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 3113185029Spjd byteswap_uint64_array : 3114236884Smm dmu_ot_byteswap[bswap].ob_func; 3115185029Spjd func(buf->b_data, hdr->b_size); 3116185029Spjd } 3117168404Spjd 3118185029Spjd arc_cksum_compute(buf, B_FALSE); 3119240133Smm#ifdef illumos 3120240133Smm arc_buf_watch(buf); 3121240133Smm#endif /* illumos */ 3122168404Spjd 3123219089Spjd if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 3124219089Spjd /* 3125219089Spjd * Only call arc_access on anonymous buffers. This is because 3126219089Spjd * if we've issued an I/O for an evicted buffer, we've already 3127219089Spjd * called arc_access (to prevent any simultaneous readers from 3128219089Spjd * getting confused). 3129219089Spjd */ 3130219089Spjd arc_access(hdr, hash_lock); 3131219089Spjd } 3132219089Spjd 3133168404Spjd /* create copies of the data buffer for the callers */ 3134168404Spjd abuf = buf; 3135168404Spjd for (acb = callback_list; acb; acb = acb->acb_next) { 3136168404Spjd if (acb->acb_done) { 3137242845Sdelphij if (abuf == NULL) { 3138242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_reads); 3139168404Spjd abuf = arc_buf_clone(buf); 3140242845Sdelphij } 3141168404Spjd acb->acb_buf = abuf; 3142168404Spjd abuf = NULL; 3143168404Spjd } 3144168404Spjd } 3145168404Spjd hdr->b_acb = NULL; 3146168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3147168404Spjd ASSERT(!HDR_BUF_AVAILABLE(hdr)); 3148219089Spjd if (abuf == buf) { 3149219089Spjd ASSERT(buf->b_efunc == NULL); 3150219089Spjd ASSERT(hdr->b_datacnt == 1); 3151168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 3152219089Spjd } 3153168404Spjd 3154168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 3155168404Spjd 3156168404Spjd if (zio->io_error != 0) { 3157168404Spjd hdr->b_flags |= ARC_IO_ERROR; 3158168404Spjd if (hdr->b_state != arc_anon) 3159168404Spjd arc_change_state(arc_anon, hdr, hash_lock); 3160168404Spjd if (HDR_IN_HASH_TABLE(hdr)) 3161168404Spjd buf_hash_remove(hdr); 3162168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 3163168404Spjd } 3164168404Spjd 3165168404Spjd /* 3166168404Spjd * Broadcast before we drop the hash_lock to avoid the possibility 3167168404Spjd * that the hdr (and hence the cv) might be freed before we get to 3168168404Spjd * the cv_broadcast(). 3169168404Spjd */ 3170168404Spjd cv_broadcast(&hdr->b_cv); 3171168404Spjd 3172168404Spjd if (hash_lock) { 3173168404Spjd mutex_exit(hash_lock); 3174168404Spjd } else { 3175168404Spjd /* 3176168404Spjd * This block was freed while we waited for the read to 3177168404Spjd * complete. It has been removed from the hash table and 3178168404Spjd * moved to the anonymous state (so that it won't show up 3179168404Spjd * in the cache). 3180168404Spjd */ 3181168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 3182168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 3183168404Spjd } 3184168404Spjd 3185168404Spjd /* execute each callback and free its structure */ 3186168404Spjd while ((acb = callback_list) != NULL) { 3187168404Spjd if (acb->acb_done) 3188168404Spjd acb->acb_done(zio, acb->acb_buf, acb->acb_private); 3189168404Spjd 3190168404Spjd if (acb->acb_zio_dummy != NULL) { 3191168404Spjd acb->acb_zio_dummy->io_error = zio->io_error; 3192168404Spjd zio_nowait(acb->acb_zio_dummy); 3193168404Spjd } 3194168404Spjd 3195168404Spjd callback_list = acb->acb_next; 3196168404Spjd kmem_free(acb, sizeof (arc_callback_t)); 3197168404Spjd } 3198168404Spjd 3199168404Spjd if (freeable) 3200168404Spjd arc_hdr_destroy(hdr); 3201168404Spjd} 3202168404Spjd 3203168404Spjd/* 3204168404Spjd * "Read" the block block at the specified DVA (in bp) via the 3205168404Spjd * cache. If the block is found in the cache, invoke the provided 3206168404Spjd * callback immediately and return. Note that the `zio' parameter 3207168404Spjd * in the callback will be NULL in this case, since no IO was 3208168404Spjd * required. If the block is not in the cache pass the read request 3209168404Spjd * on to the spa with a substitute callback function, so that the 3210168404Spjd * requested block will be added to the cache. 3211168404Spjd * 3212168404Spjd * If a read request arrives for a block that has a read in-progress, 3213168404Spjd * either wait for the in-progress read to complete (and return the 3214168404Spjd * results); or, if this is a read with a "done" func, add a record 3215168404Spjd * to the read to invoke the "done" func when the read completes, 3216168404Spjd * and return; or just return. 3217168404Spjd * 3218168404Spjd * arc_read_done() will invoke all the requested "done" functions 3219168404Spjd * for readers of this block. 3220168404Spjd */ 3221168404Spjdint 3222246666Smmarc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 3223258632Savg void *private, zio_priority_t priority, int zio_flags, uint32_t *arc_flags, 3224268123Sdelphij const zbookmark_phys_t *zb) 3225168404Spjd{ 3226268075Sdelphij arc_buf_hdr_t *hdr = NULL; 3227247187Smm arc_buf_t *buf = NULL; 3228268075Sdelphij kmutex_t *hash_lock = NULL; 3229185029Spjd zio_t *rzio; 3230228103Smm uint64_t guid = spa_load_guid(spa); 3231168404Spjd 3232268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp) || 3233268075Sdelphij BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 3234268075Sdelphij 3235168404Spjdtop: 3236268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 3237268075Sdelphij /* 3238268075Sdelphij * Embedded BP's have no DVA and require no I/O to "read". 3239268075Sdelphij * Create an anonymous arc buf to back it. 3240268075Sdelphij */ 3241268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 3242268075Sdelphij } 3243168404Spjd 3244268075Sdelphij if (hdr != NULL && hdr->b_datacnt > 0) { 3245268075Sdelphij 3246168404Spjd *arc_flags |= ARC_CACHED; 3247168404Spjd 3248168404Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3249168404Spjd 3250168404Spjd if (*arc_flags & ARC_WAIT) { 3251168404Spjd cv_wait(&hdr->b_cv, hash_lock); 3252168404Spjd mutex_exit(hash_lock); 3253168404Spjd goto top; 3254168404Spjd } 3255168404Spjd ASSERT(*arc_flags & ARC_NOWAIT); 3256168404Spjd 3257168404Spjd if (done) { 3258168404Spjd arc_callback_t *acb = NULL; 3259168404Spjd 3260168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), 3261168404Spjd KM_SLEEP); 3262168404Spjd acb->acb_done = done; 3263168404Spjd acb->acb_private = private; 3264168404Spjd if (pio != NULL) 3265168404Spjd acb->acb_zio_dummy = zio_null(pio, 3266209962Smm spa, NULL, NULL, NULL, zio_flags); 3267168404Spjd 3268168404Spjd ASSERT(acb->acb_done != NULL); 3269168404Spjd acb->acb_next = hdr->b_acb; 3270168404Spjd hdr->b_acb = acb; 3271168404Spjd add_reference(hdr, hash_lock, private); 3272168404Spjd mutex_exit(hash_lock); 3273168404Spjd return (0); 3274168404Spjd } 3275168404Spjd mutex_exit(hash_lock); 3276168404Spjd return (0); 3277168404Spjd } 3278168404Spjd 3279168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3280168404Spjd 3281168404Spjd if (done) { 3282168404Spjd add_reference(hdr, hash_lock, private); 3283168404Spjd /* 3284168404Spjd * If this block is already in use, create a new 3285168404Spjd * copy of the data so that we will be guaranteed 3286168404Spjd * that arc_release() will always succeed. 3287168404Spjd */ 3288168404Spjd buf = hdr->b_buf; 3289168404Spjd ASSERT(buf); 3290168404Spjd ASSERT(buf->b_data); 3291168404Spjd if (HDR_BUF_AVAILABLE(hdr)) { 3292168404Spjd ASSERT(buf->b_efunc == NULL); 3293168404Spjd hdr->b_flags &= ~ARC_BUF_AVAILABLE; 3294168404Spjd } else { 3295168404Spjd buf = arc_buf_clone(buf); 3296168404Spjd } 3297219089Spjd 3298168404Spjd } else if (*arc_flags & ARC_PREFETCH && 3299168404Spjd refcount_count(&hdr->b_refcnt) == 0) { 3300168404Spjd hdr->b_flags |= ARC_PREFETCH; 3301168404Spjd } 3302168404Spjd DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 3303168404Spjd arc_access(hdr, hash_lock); 3304185029Spjd if (*arc_flags & ARC_L2CACHE) 3305185029Spjd hdr->b_flags |= ARC_L2CACHE; 3306251478Sdelphij if (*arc_flags & ARC_L2COMPRESS) 3307251478Sdelphij hdr->b_flags |= ARC_L2COMPRESS; 3308168404Spjd mutex_exit(hash_lock); 3309168404Spjd ARCSTAT_BUMP(arcstat_hits); 3310168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 3311168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3312168404Spjd data, metadata, hits); 3313168404Spjd 3314168404Spjd if (done) 3315168404Spjd done(NULL, buf, private); 3316168404Spjd } else { 3317168404Spjd uint64_t size = BP_GET_LSIZE(bp); 3318268075Sdelphij arc_callback_t *acb; 3319185029Spjd vdev_t *vd = NULL; 3320247187Smm uint64_t addr = 0; 3321208373Smm boolean_t devw = B_FALSE; 3322258389Savg enum zio_compress b_compress = ZIO_COMPRESS_OFF; 3323258389Savg uint64_t b_asize = 0; 3324168404Spjd 3325168404Spjd if (hdr == NULL) { 3326168404Spjd /* this block is not in the cache */ 3327268075Sdelphij arc_buf_hdr_t *exists = NULL; 3328168404Spjd arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 3329168404Spjd buf = arc_buf_alloc(spa, size, private, type); 3330168404Spjd hdr = buf->b_hdr; 3331268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 3332268075Sdelphij hdr->b_dva = *BP_IDENTITY(bp); 3333268075Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 3334268075Sdelphij hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 3335268075Sdelphij exists = buf_hash_insert(hdr, &hash_lock); 3336268075Sdelphij } 3337268075Sdelphij if (exists != NULL) { 3338168404Spjd /* somebody beat us to the hash insert */ 3339168404Spjd mutex_exit(hash_lock); 3340219089Spjd buf_discard_identity(hdr); 3341168404Spjd (void) arc_buf_remove_ref(buf, private); 3342168404Spjd goto top; /* restart the IO request */ 3343168404Spjd } 3344168404Spjd /* if this is a prefetch, we don't have a reference */ 3345168404Spjd if (*arc_flags & ARC_PREFETCH) { 3346168404Spjd (void) remove_reference(hdr, hash_lock, 3347168404Spjd private); 3348168404Spjd hdr->b_flags |= ARC_PREFETCH; 3349168404Spjd } 3350185029Spjd if (*arc_flags & ARC_L2CACHE) 3351185029Spjd hdr->b_flags |= ARC_L2CACHE; 3352251478Sdelphij if (*arc_flags & ARC_L2COMPRESS) 3353251478Sdelphij hdr->b_flags |= ARC_L2COMPRESS; 3354168404Spjd if (BP_GET_LEVEL(bp) > 0) 3355168404Spjd hdr->b_flags |= ARC_INDIRECT; 3356168404Spjd } else { 3357168404Spjd /* this block is in the ghost cache */ 3358168404Spjd ASSERT(GHOST_STATE(hdr->b_state)); 3359168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3360240415Smm ASSERT0(refcount_count(&hdr->b_refcnt)); 3361168404Spjd ASSERT(hdr->b_buf == NULL); 3362168404Spjd 3363168404Spjd /* if this is a prefetch, we don't have a reference */ 3364168404Spjd if (*arc_flags & ARC_PREFETCH) 3365168404Spjd hdr->b_flags |= ARC_PREFETCH; 3366168404Spjd else 3367168404Spjd add_reference(hdr, hash_lock, private); 3368185029Spjd if (*arc_flags & ARC_L2CACHE) 3369185029Spjd hdr->b_flags |= ARC_L2CACHE; 3370251478Sdelphij if (*arc_flags & ARC_L2COMPRESS) 3371251478Sdelphij hdr->b_flags |= ARC_L2COMPRESS; 3372185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 3373168404Spjd buf->b_hdr = hdr; 3374168404Spjd buf->b_data = NULL; 3375168404Spjd buf->b_efunc = NULL; 3376168404Spjd buf->b_private = NULL; 3377168404Spjd buf->b_next = NULL; 3378168404Spjd hdr->b_buf = buf; 3379168404Spjd ASSERT(hdr->b_datacnt == 0); 3380168404Spjd hdr->b_datacnt = 1; 3381219089Spjd arc_get_data_buf(buf); 3382219089Spjd arc_access(hdr, hash_lock); 3383168404Spjd } 3384168404Spjd 3385219089Spjd ASSERT(!GHOST_STATE(hdr->b_state)); 3386219089Spjd 3387168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 3388168404Spjd acb->acb_done = done; 3389168404Spjd acb->acb_private = private; 3390168404Spjd 3391168404Spjd ASSERT(hdr->b_acb == NULL); 3392168404Spjd hdr->b_acb = acb; 3393168404Spjd hdr->b_flags |= ARC_IO_IN_PROGRESS; 3394168404Spjd 3395258389Savg if (hdr->b_l2hdr != NULL && 3396185029Spjd (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 3397208373Smm devw = hdr->b_l2hdr->b_dev->l2ad_writing; 3398185029Spjd addr = hdr->b_l2hdr->b_daddr; 3399258389Savg b_compress = hdr->b_l2hdr->b_compress; 3400258389Savg b_asize = hdr->b_l2hdr->b_asize; 3401185029Spjd /* 3402185029Spjd * Lock out device removal. 3403185029Spjd */ 3404185029Spjd if (vdev_is_dead(vd) || 3405185029Spjd !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 3406185029Spjd vd = NULL; 3407185029Spjd } 3408185029Spjd 3409268075Sdelphij if (hash_lock != NULL) 3410268075Sdelphij mutex_exit(hash_lock); 3411168404Spjd 3412251629Sdelphij /* 3413251629Sdelphij * At this point, we have a level 1 cache miss. Try again in 3414251629Sdelphij * L2ARC if possible. 3415251629Sdelphij */ 3416168404Spjd ASSERT3U(hdr->b_size, ==, size); 3417219089Spjd DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 3418268123Sdelphij uint64_t, size, zbookmark_phys_t *, zb); 3419168404Spjd ARCSTAT_BUMP(arcstat_misses); 3420168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 3421168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3422168404Spjd data, metadata, misses); 3423228392Spjd#ifdef _KERNEL 3424228392Spjd curthread->td_ru.ru_inblock++; 3425228392Spjd#endif 3426168404Spjd 3427208373Smm if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 3428185029Spjd /* 3429185029Spjd * Read from the L2ARC if the following are true: 3430185029Spjd * 1. The L2ARC vdev was previously cached. 3431185029Spjd * 2. This buffer still has L2ARC metadata. 3432185029Spjd * 3. This buffer isn't currently writing to the L2ARC. 3433185029Spjd * 4. The L2ARC entry wasn't evicted, which may 3434185029Spjd * also have invalidated the vdev. 3435208373Smm * 5. This isn't prefetch and l2arc_noprefetch is set. 3436185029Spjd */ 3437185029Spjd if (hdr->b_l2hdr != NULL && 3438208373Smm !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 3439208373Smm !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 3440185029Spjd l2arc_read_callback_t *cb; 3441185029Spjd 3442185029Spjd DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 3443185029Spjd ARCSTAT_BUMP(arcstat_l2_hits); 3444185029Spjd 3445185029Spjd cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 3446185029Spjd KM_SLEEP); 3447185029Spjd cb->l2rcb_buf = buf; 3448185029Spjd cb->l2rcb_spa = spa; 3449185029Spjd cb->l2rcb_bp = *bp; 3450185029Spjd cb->l2rcb_zb = *zb; 3451185029Spjd cb->l2rcb_flags = zio_flags; 3452258389Savg cb->l2rcb_compress = b_compress; 3453185029Spjd 3454247187Smm ASSERT(addr >= VDEV_LABEL_START_SIZE && 3455247187Smm addr + size < vd->vdev_psize - 3456247187Smm VDEV_LABEL_END_SIZE); 3457247187Smm 3458185029Spjd /* 3459185029Spjd * l2arc read. The SCL_L2ARC lock will be 3460185029Spjd * released by l2arc_read_done(). 3461251478Sdelphij * Issue a null zio if the underlying buffer 3462251478Sdelphij * was squashed to zero size by compression. 3463185029Spjd */ 3464258389Savg if (b_compress == ZIO_COMPRESS_EMPTY) { 3465251478Sdelphij rzio = zio_null(pio, spa, vd, 3466251478Sdelphij l2arc_read_done, cb, 3467251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 3468251478Sdelphij ZIO_FLAG_CANFAIL | 3469251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 3470251478Sdelphij ZIO_FLAG_DONT_RETRY); 3471251478Sdelphij } else { 3472251478Sdelphij rzio = zio_read_phys(pio, vd, addr, 3473258389Savg b_asize, buf->b_data, 3474258389Savg ZIO_CHECKSUM_OFF, 3475251478Sdelphij l2arc_read_done, cb, priority, 3476251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 3477251478Sdelphij ZIO_FLAG_CANFAIL | 3478251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 3479251478Sdelphij ZIO_FLAG_DONT_RETRY, B_FALSE); 3480251478Sdelphij } 3481185029Spjd DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 3482185029Spjd zio_t *, rzio); 3483258389Savg ARCSTAT_INCR(arcstat_l2_read_bytes, b_asize); 3484185029Spjd 3485185029Spjd if (*arc_flags & ARC_NOWAIT) { 3486185029Spjd zio_nowait(rzio); 3487185029Spjd return (0); 3488185029Spjd } 3489185029Spjd 3490185029Spjd ASSERT(*arc_flags & ARC_WAIT); 3491185029Spjd if (zio_wait(rzio) == 0) 3492185029Spjd return (0); 3493185029Spjd 3494185029Spjd /* l2arc read error; goto zio_read() */ 3495185029Spjd } else { 3496185029Spjd DTRACE_PROBE1(l2arc__miss, 3497185029Spjd arc_buf_hdr_t *, hdr); 3498185029Spjd ARCSTAT_BUMP(arcstat_l2_misses); 3499185029Spjd if (HDR_L2_WRITING(hdr)) 3500185029Spjd ARCSTAT_BUMP(arcstat_l2_rw_clash); 3501185029Spjd spa_config_exit(spa, SCL_L2ARC, vd); 3502185029Spjd } 3503208373Smm } else { 3504208373Smm if (vd != NULL) 3505208373Smm spa_config_exit(spa, SCL_L2ARC, vd); 3506208373Smm if (l2arc_ndev != 0) { 3507208373Smm DTRACE_PROBE1(l2arc__miss, 3508208373Smm arc_buf_hdr_t *, hdr); 3509208373Smm ARCSTAT_BUMP(arcstat_l2_misses); 3510208373Smm } 3511185029Spjd } 3512185029Spjd 3513168404Spjd rzio = zio_read(pio, spa, bp, buf->b_data, size, 3514185029Spjd arc_read_done, buf, priority, zio_flags, zb); 3515168404Spjd 3516168404Spjd if (*arc_flags & ARC_WAIT) 3517168404Spjd return (zio_wait(rzio)); 3518168404Spjd 3519168404Spjd ASSERT(*arc_flags & ARC_NOWAIT); 3520168404Spjd zio_nowait(rzio); 3521168404Spjd } 3522168404Spjd return (0); 3523168404Spjd} 3524168404Spjd 3525168404Spjdvoid 3526168404Spjdarc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 3527168404Spjd{ 3528168404Spjd ASSERT(buf->b_hdr != NULL); 3529168404Spjd ASSERT(buf->b_hdr->b_state != arc_anon); 3530168404Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 3531219089Spjd ASSERT(buf->b_efunc == NULL); 3532219089Spjd ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 3533219089Spjd 3534168404Spjd buf->b_efunc = func; 3535168404Spjd buf->b_private = private; 3536168404Spjd} 3537168404Spjd 3538168404Spjd/* 3539251520Sdelphij * Notify the arc that a block was freed, and thus will never be used again. 3540251520Sdelphij */ 3541251520Sdelphijvoid 3542251520Sdelphijarc_freed(spa_t *spa, const blkptr_t *bp) 3543251520Sdelphij{ 3544251520Sdelphij arc_buf_hdr_t *hdr; 3545251520Sdelphij kmutex_t *hash_lock; 3546251520Sdelphij uint64_t guid = spa_load_guid(spa); 3547251520Sdelphij 3548268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp)); 3549268075Sdelphij 3550268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 3551251520Sdelphij if (hdr == NULL) 3552251520Sdelphij return; 3553251520Sdelphij if (HDR_BUF_AVAILABLE(hdr)) { 3554251520Sdelphij arc_buf_t *buf = hdr->b_buf; 3555251520Sdelphij add_reference(hdr, hash_lock, FTAG); 3556251520Sdelphij hdr->b_flags &= ~ARC_BUF_AVAILABLE; 3557251520Sdelphij mutex_exit(hash_lock); 3558251520Sdelphij 3559251520Sdelphij arc_release(buf, FTAG); 3560251520Sdelphij (void) arc_buf_remove_ref(buf, FTAG); 3561251520Sdelphij } else { 3562251520Sdelphij mutex_exit(hash_lock); 3563251520Sdelphij } 3564251520Sdelphij 3565251520Sdelphij} 3566251520Sdelphij 3567251520Sdelphij/* 3568268858Sdelphij * Clear the user eviction callback set by arc_set_callback(), first calling 3569268858Sdelphij * it if it exists. Because the presence of a callback keeps an arc_buf cached 3570268858Sdelphij * clearing the callback may result in the arc_buf being destroyed. However, 3571268858Sdelphij * it will not result in the *last* arc_buf being destroyed, hence the data 3572268858Sdelphij * will remain cached in the ARC. We make a copy of the arc buffer here so 3573268858Sdelphij * that we can process the callback without holding any locks. 3574268858Sdelphij * 3575268858Sdelphij * It's possible that the callback is already in the process of being cleared 3576268858Sdelphij * by another thread. In this case we can not clear the callback. 3577268858Sdelphij * 3578268858Sdelphij * Returns B_TRUE if the callback was successfully called and cleared. 3579168404Spjd */ 3580268858Sdelphijboolean_t 3581268858Sdelphijarc_clear_callback(arc_buf_t *buf) 3582168404Spjd{ 3583168404Spjd arc_buf_hdr_t *hdr; 3584168404Spjd kmutex_t *hash_lock; 3585268858Sdelphij arc_evict_func_t *efunc = buf->b_efunc; 3586268858Sdelphij void *private = buf->b_private; 3587205231Skmacy list_t *list, *evicted_list; 3588205231Skmacy kmutex_t *lock, *evicted_lock; 3589206796Spjd 3590219089Spjd mutex_enter(&buf->b_evict_lock); 3591168404Spjd hdr = buf->b_hdr; 3592168404Spjd if (hdr == NULL) { 3593168404Spjd /* 3594168404Spjd * We are in arc_do_user_evicts(). 3595168404Spjd */ 3596168404Spjd ASSERT(buf->b_data == NULL); 3597219089Spjd mutex_exit(&buf->b_evict_lock); 3598268858Sdelphij return (B_FALSE); 3599185029Spjd } else if (buf->b_data == NULL) { 3600185029Spjd /* 3601185029Spjd * We are on the eviction list; process this buffer now 3602185029Spjd * but let arc_do_user_evicts() do the reaping. 3603185029Spjd */ 3604185029Spjd buf->b_efunc = NULL; 3605219089Spjd mutex_exit(&buf->b_evict_lock); 3606268858Sdelphij VERIFY0(efunc(private)); 3607268858Sdelphij return (B_TRUE); 3608168404Spjd } 3609168404Spjd hash_lock = HDR_LOCK(hdr); 3610168404Spjd mutex_enter(hash_lock); 3611219089Spjd hdr = buf->b_hdr; 3612219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3613168404Spjd 3614168404Spjd ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 3615168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3616168404Spjd 3617268858Sdelphij buf->b_efunc = NULL; 3618268858Sdelphij buf->b_private = NULL; 3619168404Spjd 3620268858Sdelphij if (hdr->b_datacnt > 1) { 3621268858Sdelphij mutex_exit(&buf->b_evict_lock); 3622268858Sdelphij arc_buf_destroy(buf, FALSE, TRUE); 3623268858Sdelphij } else { 3624268858Sdelphij ASSERT(buf == hdr->b_buf); 3625268858Sdelphij hdr->b_flags |= ARC_BUF_AVAILABLE; 3626268858Sdelphij mutex_exit(&buf->b_evict_lock); 3627268858Sdelphij } 3628168404Spjd 3629168404Spjd mutex_exit(hash_lock); 3630268858Sdelphij VERIFY0(efunc(private)); 3631268858Sdelphij return (B_TRUE); 3632168404Spjd} 3633168404Spjd 3634168404Spjd/* 3635251629Sdelphij * Release this buffer from the cache, making it an anonymous buffer. This 3636251629Sdelphij * must be done after a read and prior to modifying the buffer contents. 3637168404Spjd * If the buffer has more than one reference, we must make 3638185029Spjd * a new hdr for the buffer. 3639168404Spjd */ 3640168404Spjdvoid 3641168404Spjdarc_release(arc_buf_t *buf, void *tag) 3642168404Spjd{ 3643185029Spjd arc_buf_hdr_t *hdr; 3644219089Spjd kmutex_t *hash_lock = NULL; 3645185029Spjd l2arc_buf_hdr_t *l2hdr; 3646185029Spjd uint64_t buf_size; 3647168404Spjd 3648219089Spjd /* 3649219089Spjd * It would be nice to assert that if it's DMU metadata (level > 3650219089Spjd * 0 || it's the dnode file), then it must be syncing context. 3651219089Spjd * But we don't know that information at this level. 3652219089Spjd */ 3653219089Spjd 3654219089Spjd mutex_enter(&buf->b_evict_lock); 3655185029Spjd hdr = buf->b_hdr; 3656185029Spjd 3657168404Spjd /* this buffer is not on any list */ 3658168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) > 0); 3659168404Spjd 3660168404Spjd if (hdr->b_state == arc_anon) { 3661168404Spjd /* this buffer is already released */ 3662168404Spjd ASSERT(buf->b_efunc == NULL); 3663208373Smm } else { 3664208373Smm hash_lock = HDR_LOCK(hdr); 3665208373Smm mutex_enter(hash_lock); 3666219089Spjd hdr = buf->b_hdr; 3667219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3668168404Spjd } 3669168404Spjd 3670185029Spjd l2hdr = hdr->b_l2hdr; 3671185029Spjd if (l2hdr) { 3672185029Spjd mutex_enter(&l2arc_buflist_mtx); 3673185029Spjd hdr->b_l2hdr = NULL; 3674258388Savg list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 3675185029Spjd } 3676247187Smm buf_size = hdr->b_size; 3677185029Spjd 3678168404Spjd /* 3679168404Spjd * Do we have more than one buf? 3680168404Spjd */ 3681185029Spjd if (hdr->b_datacnt > 1) { 3682168404Spjd arc_buf_hdr_t *nhdr; 3683168404Spjd arc_buf_t **bufp; 3684168404Spjd uint64_t blksz = hdr->b_size; 3685209962Smm uint64_t spa = hdr->b_spa; 3686168404Spjd arc_buf_contents_t type = hdr->b_type; 3687185029Spjd uint32_t flags = hdr->b_flags; 3688168404Spjd 3689185029Spjd ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3690168404Spjd /* 3691219089Spjd * Pull the data off of this hdr and attach it to 3692219089Spjd * a new anonymous hdr. 3693168404Spjd */ 3694168404Spjd (void) remove_reference(hdr, hash_lock, tag); 3695168404Spjd bufp = &hdr->b_buf; 3696168404Spjd while (*bufp != buf) 3697168404Spjd bufp = &(*bufp)->b_next; 3698219089Spjd *bufp = buf->b_next; 3699168404Spjd buf->b_next = NULL; 3700168404Spjd 3701168404Spjd ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 3702168404Spjd atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 3703168404Spjd if (refcount_is_zero(&hdr->b_refcnt)) { 3704185029Spjd uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 3705185029Spjd ASSERT3U(*size, >=, hdr->b_size); 3706185029Spjd atomic_add_64(size, -hdr->b_size); 3707168404Spjd } 3708242845Sdelphij 3709242845Sdelphij /* 3710242845Sdelphij * We're releasing a duplicate user data buffer, update 3711242845Sdelphij * our statistics accordingly. 3712242845Sdelphij */ 3713242845Sdelphij if (hdr->b_type == ARC_BUFC_DATA) { 3714242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 3715242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, 3716242845Sdelphij -hdr->b_size); 3717242845Sdelphij } 3718168404Spjd hdr->b_datacnt -= 1; 3719168404Spjd arc_cksum_verify(buf); 3720240133Smm#ifdef illumos 3721240133Smm arc_buf_unwatch(buf); 3722240133Smm#endif /* illumos */ 3723168404Spjd 3724168404Spjd mutex_exit(hash_lock); 3725168404Spjd 3726185029Spjd nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3727168404Spjd nhdr->b_size = blksz; 3728168404Spjd nhdr->b_spa = spa; 3729168404Spjd nhdr->b_type = type; 3730168404Spjd nhdr->b_buf = buf; 3731168404Spjd nhdr->b_state = arc_anon; 3732168404Spjd nhdr->b_arc_access = 0; 3733185029Spjd nhdr->b_flags = flags & ARC_L2_WRITING; 3734185029Spjd nhdr->b_l2hdr = NULL; 3735168404Spjd nhdr->b_datacnt = 1; 3736168404Spjd nhdr->b_freeze_cksum = NULL; 3737168404Spjd (void) refcount_add(&nhdr->b_refcnt, tag); 3738168404Spjd buf->b_hdr = nhdr; 3739219089Spjd mutex_exit(&buf->b_evict_lock); 3740168404Spjd atomic_add_64(&arc_anon->arcs_size, blksz); 3741168404Spjd } else { 3742219089Spjd mutex_exit(&buf->b_evict_lock); 3743168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3744168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 3745168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3746219089Spjd if (hdr->b_state != arc_anon) 3747219089Spjd arc_change_state(arc_anon, hdr, hash_lock); 3748168404Spjd hdr->b_arc_access = 0; 3749219089Spjd if (hash_lock) 3750219089Spjd mutex_exit(hash_lock); 3751185029Spjd 3752219089Spjd buf_discard_identity(hdr); 3753168404Spjd arc_buf_thaw(buf); 3754168404Spjd } 3755168404Spjd buf->b_efunc = NULL; 3756168404Spjd buf->b_private = NULL; 3757185029Spjd 3758185029Spjd if (l2hdr) { 3759251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); 3760268085Sdelphij vdev_space_update(l2hdr->b_dev->l2ad_vdev, 3761268085Sdelphij -l2hdr->b_asize, 0, 0); 3762248572Ssmh trim_map_free(l2hdr->b_dev->l2ad_vdev, l2hdr->b_daddr, 3763248574Ssmh hdr->b_size, 0); 3764185029Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 3765185029Spjd ARCSTAT_INCR(arcstat_l2_size, -buf_size); 3766185029Spjd mutex_exit(&l2arc_buflist_mtx); 3767185029Spjd } 3768168404Spjd} 3769168404Spjd 3770168404Spjdint 3771168404Spjdarc_released(arc_buf_t *buf) 3772168404Spjd{ 3773185029Spjd int released; 3774185029Spjd 3775219089Spjd mutex_enter(&buf->b_evict_lock); 3776185029Spjd released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 3777219089Spjd mutex_exit(&buf->b_evict_lock); 3778185029Spjd return (released); 3779168404Spjd} 3780168404Spjd 3781168404Spjd#ifdef ZFS_DEBUG 3782168404Spjdint 3783168404Spjdarc_referenced(arc_buf_t *buf) 3784168404Spjd{ 3785185029Spjd int referenced; 3786185029Spjd 3787219089Spjd mutex_enter(&buf->b_evict_lock); 3788185029Spjd referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 3789219089Spjd mutex_exit(&buf->b_evict_lock); 3790185029Spjd return (referenced); 3791168404Spjd} 3792168404Spjd#endif 3793168404Spjd 3794168404Spjdstatic void 3795168404Spjdarc_write_ready(zio_t *zio) 3796168404Spjd{ 3797168404Spjd arc_write_callback_t *callback = zio->io_private; 3798168404Spjd arc_buf_t *buf = callback->awcb_buf; 3799185029Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3800168404Spjd 3801185029Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 3802185029Spjd callback->awcb_ready(zio, buf, callback->awcb_private); 3803185029Spjd 3804185029Spjd /* 3805185029Spjd * If the IO is already in progress, then this is a re-write 3806185029Spjd * attempt, so we need to thaw and re-compute the cksum. 3807185029Spjd * It is the responsibility of the callback to handle the 3808185029Spjd * accounting for any re-write attempt. 3809185029Spjd */ 3810185029Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3811185029Spjd mutex_enter(&hdr->b_freeze_lock); 3812185029Spjd if (hdr->b_freeze_cksum != NULL) { 3813185029Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 3814185029Spjd hdr->b_freeze_cksum = NULL; 3815185029Spjd } 3816185029Spjd mutex_exit(&hdr->b_freeze_lock); 3817168404Spjd } 3818185029Spjd arc_cksum_compute(buf, B_FALSE); 3819185029Spjd hdr->b_flags |= ARC_IO_IN_PROGRESS; 3820168404Spjd} 3821168404Spjd 3822258632Savg/* 3823258632Savg * The SPA calls this callback for each physical write that happens on behalf 3824258632Savg * of a logical write. See the comment in dbuf_write_physdone() for details. 3825258632Savg */ 3826168404Spjdstatic void 3827258632Savgarc_write_physdone(zio_t *zio) 3828258632Savg{ 3829258632Savg arc_write_callback_t *cb = zio->io_private; 3830258632Savg if (cb->awcb_physdone != NULL) 3831258632Savg cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 3832258632Savg} 3833258632Savg 3834258632Savgstatic void 3835168404Spjdarc_write_done(zio_t *zio) 3836168404Spjd{ 3837168404Spjd arc_write_callback_t *callback = zio->io_private; 3838168404Spjd arc_buf_t *buf = callback->awcb_buf; 3839168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3840168404Spjd 3841219089Spjd ASSERT(hdr->b_acb == NULL); 3842168404Spjd 3843219089Spjd if (zio->io_error == 0) { 3844268075Sdelphij if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 3845260150Sdelphij buf_discard_identity(hdr); 3846260150Sdelphij } else { 3847260150Sdelphij hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3848260150Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 3849260150Sdelphij hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 3850260150Sdelphij } 3851219089Spjd } else { 3852219089Spjd ASSERT(BUF_EMPTY(hdr)); 3853219089Spjd } 3854219089Spjd 3855168404Spjd /* 3856268075Sdelphij * If the block to be written was all-zero or compressed enough to be 3857268075Sdelphij * embedded in the BP, no write was performed so there will be no 3858268075Sdelphij * dva/birth/checksum. The buffer must therefore remain anonymous 3859268075Sdelphij * (and uncached). 3860168404Spjd */ 3861168404Spjd if (!BUF_EMPTY(hdr)) { 3862168404Spjd arc_buf_hdr_t *exists; 3863168404Spjd kmutex_t *hash_lock; 3864168404Spjd 3865219089Spjd ASSERT(zio->io_error == 0); 3866219089Spjd 3867168404Spjd arc_cksum_verify(buf); 3868168404Spjd 3869168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 3870168404Spjd if (exists) { 3871168404Spjd /* 3872168404Spjd * This can only happen if we overwrite for 3873168404Spjd * sync-to-convergence, because we remove 3874168404Spjd * buffers from the hash table when we arc_free(). 3875168404Spjd */ 3876219089Spjd if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 3877219089Spjd if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3878219089Spjd panic("bad overwrite, hdr=%p exists=%p", 3879219089Spjd (void *)hdr, (void *)exists); 3880219089Spjd ASSERT(refcount_is_zero(&exists->b_refcnt)); 3881219089Spjd arc_change_state(arc_anon, exists, hash_lock); 3882219089Spjd mutex_exit(hash_lock); 3883219089Spjd arc_hdr_destroy(exists); 3884219089Spjd exists = buf_hash_insert(hdr, &hash_lock); 3885219089Spjd ASSERT3P(exists, ==, NULL); 3886243524Smm } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 3887243524Smm /* nopwrite */ 3888243524Smm ASSERT(zio->io_prop.zp_nopwrite); 3889243524Smm if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3890243524Smm panic("bad nopwrite, hdr=%p exists=%p", 3891243524Smm (void *)hdr, (void *)exists); 3892219089Spjd } else { 3893219089Spjd /* Dedup */ 3894219089Spjd ASSERT(hdr->b_datacnt == 1); 3895219089Spjd ASSERT(hdr->b_state == arc_anon); 3896219089Spjd ASSERT(BP_GET_DEDUP(zio->io_bp)); 3897219089Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 3898219089Spjd } 3899168404Spjd } 3900168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3901185029Spjd /* if it's not anon, we are doing a scrub */ 3902219089Spjd if (!exists && hdr->b_state == arc_anon) 3903185029Spjd arc_access(hdr, hash_lock); 3904168404Spjd mutex_exit(hash_lock); 3905168404Spjd } else { 3906168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3907168404Spjd } 3908168404Spjd 3909219089Spjd ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 3910219089Spjd callback->awcb_done(zio, buf, callback->awcb_private); 3911168404Spjd 3912168404Spjd kmem_free(callback, sizeof (arc_write_callback_t)); 3913168404Spjd} 3914168404Spjd 3915168404Spjdzio_t * 3916219089Spjdarc_write(zio_t *pio, spa_t *spa, uint64_t txg, 3917251478Sdelphij blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress, 3918258632Savg const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone, 3919258632Savg arc_done_func_t *done, void *private, zio_priority_t priority, 3920268123Sdelphij int zio_flags, const zbookmark_phys_t *zb) 3921168404Spjd{ 3922168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3923168404Spjd arc_write_callback_t *callback; 3924185029Spjd zio_t *zio; 3925168404Spjd 3926185029Spjd ASSERT(ready != NULL); 3927219089Spjd ASSERT(done != NULL); 3928168404Spjd ASSERT(!HDR_IO_ERROR(hdr)); 3929168404Spjd ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 3930219089Spjd ASSERT(hdr->b_acb == NULL); 3931185029Spjd if (l2arc) 3932185029Spjd hdr->b_flags |= ARC_L2CACHE; 3933251478Sdelphij if (l2arc_compress) 3934251478Sdelphij hdr->b_flags |= ARC_L2COMPRESS; 3935168404Spjd callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 3936168404Spjd callback->awcb_ready = ready; 3937258632Savg callback->awcb_physdone = physdone; 3938168404Spjd callback->awcb_done = done; 3939168404Spjd callback->awcb_private = private; 3940168404Spjd callback->awcb_buf = buf; 3941168404Spjd 3942219089Spjd zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 3943258632Savg arc_write_ready, arc_write_physdone, arc_write_done, callback, 3944258632Savg priority, zio_flags, zb); 3945185029Spjd 3946168404Spjd return (zio); 3947168404Spjd} 3948168404Spjd 3949185029Spjdstatic int 3950258632Savgarc_memory_throttle(uint64_t reserve, uint64_t txg) 3951185029Spjd{ 3952185029Spjd#ifdef _KERNEL 3953272483Ssmh uint64_t available_memory = ptob(freemem); 3954185029Spjd static uint64_t page_load = 0; 3955185029Spjd static uint64_t last_txg = 0; 3956185029Spjd 3957272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 3958185029Spjd available_memory = 3959272483Ssmh MIN(available_memory, ptob(vmem_size(heap_arena, VMEM_FREE))); 3960185029Spjd#endif 3961258632Savg 3962272483Ssmh if (freemem > (uint64_t)physmem * arc_lotsfree_percent / 100) 3963185029Spjd return (0); 3964185029Spjd 3965185029Spjd if (txg > last_txg) { 3966185029Spjd last_txg = txg; 3967185029Spjd page_load = 0; 3968185029Spjd } 3969185029Spjd /* 3970185029Spjd * If we are in pageout, we know that memory is already tight, 3971185029Spjd * the arc is already going to be evicting, so we just want to 3972185029Spjd * continue to let page writes occur as quickly as possible. 3973185029Spjd */ 3974185029Spjd if (curproc == pageproc) { 3975272483Ssmh if (page_load > MAX(ptob(minfree), available_memory) / 4) 3976249195Smm return (SET_ERROR(ERESTART)); 3977185029Spjd /* Note: reserve is inflated, so we deflate */ 3978185029Spjd page_load += reserve / 8; 3979185029Spjd return (0); 3980185029Spjd } else if (page_load > 0 && arc_reclaim_needed()) { 3981185029Spjd /* memory is low, delay before restarting */ 3982185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 3983249195Smm return (SET_ERROR(EAGAIN)); 3984185029Spjd } 3985185029Spjd page_load = 0; 3986185029Spjd#endif 3987185029Spjd return (0); 3988185029Spjd} 3989185029Spjd 3990168404Spjdvoid 3991185029Spjdarc_tempreserve_clear(uint64_t reserve) 3992168404Spjd{ 3993185029Spjd atomic_add_64(&arc_tempreserve, -reserve); 3994168404Spjd ASSERT((int64_t)arc_tempreserve >= 0); 3995168404Spjd} 3996168404Spjd 3997168404Spjdint 3998185029Spjdarc_tempreserve_space(uint64_t reserve, uint64_t txg) 3999168404Spjd{ 4000185029Spjd int error; 4001209962Smm uint64_t anon_size; 4002185029Spjd 4003272483Ssmh if (reserve > arc_c/4 && !arc_no_grow) { 4004185029Spjd arc_c = MIN(arc_c_max, reserve * 4); 4005272483Ssmh DTRACE_PROBE1(arc__set_reserve, uint64_t, arc_c); 4006272483Ssmh } 4007185029Spjd if (reserve > arc_c) 4008249195Smm return (SET_ERROR(ENOMEM)); 4009168404Spjd 4010168404Spjd /* 4011209962Smm * Don't count loaned bufs as in flight dirty data to prevent long 4012209962Smm * network delays from blocking transactions that are ready to be 4013209962Smm * assigned to a txg. 4014209962Smm */ 4015209962Smm anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 4016209962Smm 4017209962Smm /* 4018185029Spjd * Writes will, almost always, require additional memory allocations 4019251631Sdelphij * in order to compress/encrypt/etc the data. We therefore need to 4020185029Spjd * make sure that there is sufficient available memory for this. 4021185029Spjd */ 4022258632Savg error = arc_memory_throttle(reserve, txg); 4023258632Savg if (error != 0) 4024185029Spjd return (error); 4025185029Spjd 4026185029Spjd /* 4027168404Spjd * Throttle writes when the amount of dirty data in the cache 4028168404Spjd * gets too large. We try to keep the cache less than half full 4029168404Spjd * of dirty blocks so that our sync times don't grow too large. 4030168404Spjd * Note: if two requests come in concurrently, we might let them 4031168404Spjd * both succeed, when one of them should fail. Not a huge deal. 4032168404Spjd */ 4033209962Smm 4034209962Smm if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 4035209962Smm anon_size > arc_c / 4) { 4036185029Spjd dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 4037185029Spjd "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 4038185029Spjd arc_tempreserve>>10, 4039185029Spjd arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 4040185029Spjd arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 4041185029Spjd reserve>>10, arc_c>>10); 4042249195Smm return (SET_ERROR(ERESTART)); 4043168404Spjd } 4044185029Spjd atomic_add_64(&arc_tempreserve, reserve); 4045168404Spjd return (0); 4046168404Spjd} 4047168404Spjd 4048168582Spjdstatic kmutex_t arc_lowmem_lock; 4049168404Spjd#ifdef _KERNEL 4050168566Spjdstatic eventhandler_tag arc_event_lowmem = NULL; 4051168404Spjd 4052168404Spjdstatic void 4053168566Spjdarc_lowmem(void *arg __unused, int howto __unused) 4054168404Spjd{ 4055168404Spjd 4056168566Spjd /* Serialize access via arc_lowmem_lock. */ 4057168566Spjd mutex_enter(&arc_lowmem_lock); 4058219089Spjd mutex_enter(&arc_reclaim_thr_lock); 4059185029Spjd needfree = 1; 4060272483Ssmh DTRACE_PROBE(arc__needfree); 4061168404Spjd cv_signal(&arc_reclaim_thr_cv); 4062241773Savg 4063241773Savg /* 4064241773Savg * It is unsafe to block here in arbitrary threads, because we can come 4065241773Savg * here from ARC itself and may hold ARC locks and thus risk a deadlock 4066241773Savg * with ARC reclaim thread. 4067241773Savg */ 4068241773Savg if (curproc == pageproc) { 4069241773Savg while (needfree) 4070241773Savg msleep(&needfree, &arc_reclaim_thr_lock, 0, "zfs:lowmem", 0); 4071241773Savg } 4072219089Spjd mutex_exit(&arc_reclaim_thr_lock); 4073168566Spjd mutex_exit(&arc_lowmem_lock); 4074168404Spjd} 4075168404Spjd#endif 4076168404Spjd 4077168404Spjdvoid 4078168404Spjdarc_init(void) 4079168404Spjd{ 4080219089Spjd int i, prefetch_tunable_set = 0; 4081205231Skmacy 4082168404Spjd mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 4083168404Spjd cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 4084168566Spjd mutex_init(&arc_lowmem_lock, NULL, MUTEX_DEFAULT, NULL); 4085168404Spjd 4086168404Spjd /* Convert seconds to clock ticks */ 4087168404Spjd arc_min_prefetch_lifespan = 1 * hz; 4088168404Spjd 4089168404Spjd /* Start out with 1/8 of all memory */ 4090168566Spjd arc_c = kmem_size() / 8; 4091219089Spjd 4092219089Spjd#ifdef sun 4093192360Skmacy#ifdef _KERNEL 4094192360Skmacy /* 4095192360Skmacy * On architectures where the physical memory can be larger 4096192360Skmacy * than the addressable space (intel in 32-bit mode), we may 4097192360Skmacy * need to limit the cache to 1/8 of VM size. 4098192360Skmacy */ 4099192360Skmacy arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 4100192360Skmacy#endif 4101219089Spjd#endif /* sun */ 4102168566Spjd /* set min cache to 1/32 of all memory, or 16MB, whichever is more */ 4103168566Spjd arc_c_min = MAX(arc_c / 4, 64<<18); 4104168566Spjd /* set max to 1/2 of all memory, or all but 1GB, whichever is more */ 4105168404Spjd if (arc_c * 8 >= 1<<30) 4106168404Spjd arc_c_max = (arc_c * 8) - (1<<30); 4107168404Spjd else 4108168404Spjd arc_c_max = arc_c_min; 4109175633Spjd arc_c_max = MAX(arc_c * 5, arc_c_max); 4110219089Spjd 4111168481Spjd#ifdef _KERNEL 4112168404Spjd /* 4113168404Spjd * Allow the tunables to override our calculations if they are 4114168566Spjd * reasonable (ie. over 16MB) 4115168404Spjd */ 4116219089Spjd if (zfs_arc_max > 64<<18 && zfs_arc_max < kmem_size()) 4117168404Spjd arc_c_max = zfs_arc_max; 4118219089Spjd if (zfs_arc_min > 64<<18 && zfs_arc_min <= arc_c_max) 4119168404Spjd arc_c_min = zfs_arc_min; 4120168481Spjd#endif 4121219089Spjd 4122168404Spjd arc_c = arc_c_max; 4123168404Spjd arc_p = (arc_c >> 1); 4124168404Spjd 4125185029Spjd /* limit meta-data to 1/4 of the arc capacity */ 4126185029Spjd arc_meta_limit = arc_c_max / 4; 4127185029Spjd 4128185029Spjd /* Allow the tunable to override if it is reasonable */ 4129185029Spjd if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 4130185029Spjd arc_meta_limit = zfs_arc_meta_limit; 4131185029Spjd 4132185029Spjd if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 4133185029Spjd arc_c_min = arc_meta_limit / 2; 4134185029Spjd 4135208373Smm if (zfs_arc_grow_retry > 0) 4136208373Smm arc_grow_retry = zfs_arc_grow_retry; 4137208373Smm 4138208373Smm if (zfs_arc_shrink_shift > 0) 4139208373Smm arc_shrink_shift = zfs_arc_shrink_shift; 4140208373Smm 4141208373Smm if (zfs_arc_p_min_shift > 0) 4142208373Smm arc_p_min_shift = zfs_arc_p_min_shift; 4143208373Smm 4144168404Spjd /* if kmem_flags are set, lets try to use less memory */ 4145168404Spjd if (kmem_debugging()) 4146168404Spjd arc_c = arc_c / 2; 4147168404Spjd if (arc_c < arc_c_min) 4148168404Spjd arc_c = arc_c_min; 4149168404Spjd 4150168473Spjd zfs_arc_min = arc_c_min; 4151168473Spjd zfs_arc_max = arc_c_max; 4152168473Spjd 4153168404Spjd arc_anon = &ARC_anon; 4154168404Spjd arc_mru = &ARC_mru; 4155168404Spjd arc_mru_ghost = &ARC_mru_ghost; 4156168404Spjd arc_mfu = &ARC_mfu; 4157168404Spjd arc_mfu_ghost = &ARC_mfu_ghost; 4158185029Spjd arc_l2c_only = &ARC_l2c_only; 4159168404Spjd arc_size = 0; 4160168404Spjd 4161205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 4162205231Skmacy mutex_init(&arc_anon->arcs_locks[i].arcs_lock, 4163205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4164205231Skmacy mutex_init(&arc_mru->arcs_locks[i].arcs_lock, 4165205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4166205231Skmacy mutex_init(&arc_mru_ghost->arcs_locks[i].arcs_lock, 4167205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4168205231Skmacy mutex_init(&arc_mfu->arcs_locks[i].arcs_lock, 4169205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4170205231Skmacy mutex_init(&arc_mfu_ghost->arcs_locks[i].arcs_lock, 4171205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4172205231Skmacy mutex_init(&arc_l2c_only->arcs_locks[i].arcs_lock, 4173205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4174206796Spjd 4175205231Skmacy list_create(&arc_mru->arcs_lists[i], 4176205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4177205231Skmacy list_create(&arc_mru_ghost->arcs_lists[i], 4178205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4179205231Skmacy list_create(&arc_mfu->arcs_lists[i], 4180205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4181205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 4182205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4183205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 4184205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4185205231Skmacy list_create(&arc_l2c_only->arcs_lists[i], 4186205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4187205231Skmacy } 4188168404Spjd 4189168404Spjd buf_init(); 4190168404Spjd 4191168404Spjd arc_thread_exit = 0; 4192168404Spjd arc_eviction_list = NULL; 4193168404Spjd mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 4194168404Spjd bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 4195168404Spjd 4196168404Spjd arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 4197168404Spjd sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 4198168404Spjd 4199168404Spjd if (arc_ksp != NULL) { 4200168404Spjd arc_ksp->ks_data = &arc_stats; 4201168404Spjd kstat_install(arc_ksp); 4202168404Spjd } 4203168404Spjd 4204168404Spjd (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 4205168404Spjd TS_RUN, minclsyspri); 4206168404Spjd 4207168404Spjd#ifdef _KERNEL 4208168566Spjd arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 4209168404Spjd EVENTHANDLER_PRI_FIRST); 4210168404Spjd#endif 4211168404Spjd 4212168404Spjd arc_dead = FALSE; 4213185029Spjd arc_warm = B_FALSE; 4214168566Spjd 4215258632Savg /* 4216258632Savg * Calculate maximum amount of dirty data per pool. 4217258632Savg * 4218258632Savg * If it has been set by /etc/system, take that. 4219258632Savg * Otherwise, use a percentage of physical memory defined by 4220258632Savg * zfs_dirty_data_max_percent (default 10%) with a cap at 4221258632Savg * zfs_dirty_data_max_max (default 4GB). 4222258632Savg */ 4223258632Savg if (zfs_dirty_data_max == 0) { 4224258632Savg zfs_dirty_data_max = ptob(physmem) * 4225258632Savg zfs_dirty_data_max_percent / 100; 4226258632Savg zfs_dirty_data_max = MIN(zfs_dirty_data_max, 4227258632Savg zfs_dirty_data_max_max); 4228258632Savg } 4229185029Spjd 4230168566Spjd#ifdef _KERNEL 4231194043Skmacy if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 4232193953Skmacy prefetch_tunable_set = 1; 4233206796Spjd 4234193878Skmacy#ifdef __i386__ 4235193953Skmacy if (prefetch_tunable_set == 0) { 4236196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 4237196863Strasz "-- to enable,\n"); 4238196863Strasz printf(" add \"vfs.zfs.prefetch_disable=0\" " 4239196863Strasz "to /boot/loader.conf.\n"); 4240219089Spjd zfs_prefetch_disable = 1; 4241193878Skmacy } 4242206796Spjd#else 4243193878Skmacy if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 4244193953Skmacy prefetch_tunable_set == 0) { 4245196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default if less " 4246196941Strasz "than 4GB of RAM is present;\n" 4247196863Strasz " to enable, add \"vfs.zfs.prefetch_disable=0\" " 4248196863Strasz "to /boot/loader.conf.\n"); 4249219089Spjd zfs_prefetch_disable = 1; 4250193878Skmacy } 4251206796Spjd#endif 4252175633Spjd /* Warn about ZFS memory and address space requirements. */ 4253168696Spjd if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 4254168987Sbmah printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 4255168987Sbmah "expect unstable behavior.\n"); 4256175633Spjd } 4257175633Spjd if (kmem_size() < 512 * (1 << 20)) { 4258173419Spjd printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 4259168987Sbmah "expect unstable behavior.\n"); 4260185029Spjd printf(" Consider tuning vm.kmem_size and " 4261173419Spjd "vm.kmem_size_max\n"); 4262185029Spjd printf(" in /boot/loader.conf.\n"); 4263168566Spjd } 4264168566Spjd#endif 4265168404Spjd} 4266168404Spjd 4267168404Spjdvoid 4268168404Spjdarc_fini(void) 4269168404Spjd{ 4270205231Skmacy int i; 4271206796Spjd 4272168404Spjd mutex_enter(&arc_reclaim_thr_lock); 4273168404Spjd arc_thread_exit = 1; 4274168404Spjd cv_signal(&arc_reclaim_thr_cv); 4275168404Spjd while (arc_thread_exit != 0) 4276168404Spjd cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 4277168404Spjd mutex_exit(&arc_reclaim_thr_lock); 4278168404Spjd 4279185029Spjd arc_flush(NULL); 4280168404Spjd 4281168404Spjd arc_dead = TRUE; 4282168404Spjd 4283168404Spjd if (arc_ksp != NULL) { 4284168404Spjd kstat_delete(arc_ksp); 4285168404Spjd arc_ksp = NULL; 4286168404Spjd } 4287168404Spjd 4288168404Spjd mutex_destroy(&arc_eviction_mtx); 4289168404Spjd mutex_destroy(&arc_reclaim_thr_lock); 4290168404Spjd cv_destroy(&arc_reclaim_thr_cv); 4291168404Spjd 4292205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 4293205231Skmacy list_destroy(&arc_mru->arcs_lists[i]); 4294205231Skmacy list_destroy(&arc_mru_ghost->arcs_lists[i]); 4295205231Skmacy list_destroy(&arc_mfu->arcs_lists[i]); 4296205231Skmacy list_destroy(&arc_mfu_ghost->arcs_lists[i]); 4297206795Spjd list_destroy(&arc_l2c_only->arcs_lists[i]); 4298168404Spjd 4299205231Skmacy mutex_destroy(&arc_anon->arcs_locks[i].arcs_lock); 4300205231Skmacy mutex_destroy(&arc_mru->arcs_locks[i].arcs_lock); 4301205231Skmacy mutex_destroy(&arc_mru_ghost->arcs_locks[i].arcs_lock); 4302205231Skmacy mutex_destroy(&arc_mfu->arcs_locks[i].arcs_lock); 4303205231Skmacy mutex_destroy(&arc_mfu_ghost->arcs_locks[i].arcs_lock); 4304206795Spjd mutex_destroy(&arc_l2c_only->arcs_locks[i].arcs_lock); 4305205231Skmacy } 4306206796Spjd 4307168404Spjd buf_fini(); 4308168404Spjd 4309209962Smm ASSERT(arc_loaned_bytes == 0); 4310209962Smm 4311168582Spjd mutex_destroy(&arc_lowmem_lock); 4312168404Spjd#ifdef _KERNEL 4313168566Spjd if (arc_event_lowmem != NULL) 4314168566Spjd EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 4315168404Spjd#endif 4316168404Spjd} 4317185029Spjd 4318185029Spjd/* 4319185029Spjd * Level 2 ARC 4320185029Spjd * 4321185029Spjd * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 4322185029Spjd * It uses dedicated storage devices to hold cached data, which are populated 4323185029Spjd * using large infrequent writes. The main role of this cache is to boost 4324185029Spjd * the performance of random read workloads. The intended L2ARC devices 4325185029Spjd * include short-stroked disks, solid state disks, and other media with 4326185029Spjd * substantially faster read latency than disk. 4327185029Spjd * 4328185029Spjd * +-----------------------+ 4329185029Spjd * | ARC | 4330185029Spjd * +-----------------------+ 4331185029Spjd * | ^ ^ 4332185029Spjd * | | | 4333185029Spjd * l2arc_feed_thread() arc_read() 4334185029Spjd * | | | 4335185029Spjd * | l2arc read | 4336185029Spjd * V | | 4337185029Spjd * +---------------+ | 4338185029Spjd * | L2ARC | | 4339185029Spjd * +---------------+ | 4340185029Spjd * | ^ | 4341185029Spjd * l2arc_write() | | 4342185029Spjd * | | | 4343185029Spjd * V | | 4344185029Spjd * +-------+ +-------+ 4345185029Spjd * | vdev | | vdev | 4346185029Spjd * | cache | | cache | 4347185029Spjd * +-------+ +-------+ 4348185029Spjd * +=========+ .-----. 4349185029Spjd * : L2ARC : |-_____-| 4350185029Spjd * : devices : | Disks | 4351185029Spjd * +=========+ `-_____-' 4352185029Spjd * 4353185029Spjd * Read requests are satisfied from the following sources, in order: 4354185029Spjd * 4355185029Spjd * 1) ARC 4356185029Spjd * 2) vdev cache of L2ARC devices 4357185029Spjd * 3) L2ARC devices 4358185029Spjd * 4) vdev cache of disks 4359185029Spjd * 5) disks 4360185029Spjd * 4361185029Spjd * Some L2ARC device types exhibit extremely slow write performance. 4362185029Spjd * To accommodate for this there are some significant differences between 4363185029Spjd * the L2ARC and traditional cache design: 4364185029Spjd * 4365185029Spjd * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 4366185029Spjd * the ARC behave as usual, freeing buffers and placing headers on ghost 4367185029Spjd * lists. The ARC does not send buffers to the L2ARC during eviction as 4368185029Spjd * this would add inflated write latencies for all ARC memory pressure. 4369185029Spjd * 4370185029Spjd * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 4371185029Spjd * It does this by periodically scanning buffers from the eviction-end of 4372185029Spjd * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 4373251478Sdelphij * not already there. It scans until a headroom of buffers is satisfied, 4374251478Sdelphij * which itself is a buffer for ARC eviction. If a compressible buffer is 4375251478Sdelphij * found during scanning and selected for writing to an L2ARC device, we 4376251478Sdelphij * temporarily boost scanning headroom during the next scan cycle to make 4377251478Sdelphij * sure we adapt to compression effects (which might significantly reduce 4378251478Sdelphij * the data volume we write to L2ARC). The thread that does this is 4379185029Spjd * l2arc_feed_thread(), illustrated below; example sizes are included to 4380185029Spjd * provide a better sense of ratio than this diagram: 4381185029Spjd * 4382185029Spjd * head --> tail 4383185029Spjd * +---------------------+----------+ 4384185029Spjd * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 4385185029Spjd * +---------------------+----------+ | o L2ARC eligible 4386185029Spjd * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 4387185029Spjd * +---------------------+----------+ | 4388185029Spjd * 15.9 Gbytes ^ 32 Mbytes | 4389185029Spjd * headroom | 4390185029Spjd * l2arc_feed_thread() 4391185029Spjd * | 4392185029Spjd * l2arc write hand <--[oooo]--' 4393185029Spjd * | 8 Mbyte 4394185029Spjd * | write max 4395185029Spjd * V 4396185029Spjd * +==============================+ 4397185029Spjd * L2ARC dev |####|#|###|###| |####| ... | 4398185029Spjd * +==============================+ 4399185029Spjd * 32 Gbytes 4400185029Spjd * 4401185029Spjd * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 4402185029Spjd * evicted, then the L2ARC has cached a buffer much sooner than it probably 4403185029Spjd * needed to, potentially wasting L2ARC device bandwidth and storage. It is 4404185029Spjd * safe to say that this is an uncommon case, since buffers at the end of 4405185029Spjd * the ARC lists have moved there due to inactivity. 4406185029Spjd * 4407185029Spjd * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 4408185029Spjd * then the L2ARC simply misses copying some buffers. This serves as a 4409185029Spjd * pressure valve to prevent heavy read workloads from both stalling the ARC 4410185029Spjd * with waits and clogging the L2ARC with writes. This also helps prevent 4411185029Spjd * the potential for the L2ARC to churn if it attempts to cache content too 4412185029Spjd * quickly, such as during backups of the entire pool. 4413185029Spjd * 4414185029Spjd * 5. After system boot and before the ARC has filled main memory, there are 4415185029Spjd * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 4416185029Spjd * lists can remain mostly static. Instead of searching from tail of these 4417185029Spjd * lists as pictured, the l2arc_feed_thread() will search from the list heads 4418185029Spjd * for eligible buffers, greatly increasing its chance of finding them. 4419185029Spjd * 4420185029Spjd * The L2ARC device write speed is also boosted during this time so that 4421185029Spjd * the L2ARC warms up faster. Since there have been no ARC evictions yet, 4422185029Spjd * there are no L2ARC reads, and no fear of degrading read performance 4423185029Spjd * through increased writes. 4424185029Spjd * 4425185029Spjd * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 4426185029Spjd * the vdev queue can aggregate them into larger and fewer writes. Each 4427185029Spjd * device is written to in a rotor fashion, sweeping writes through 4428185029Spjd * available space then repeating. 4429185029Spjd * 4430185029Spjd * 7. The L2ARC does not store dirty content. It never needs to flush 4431185029Spjd * write buffers back to disk based storage. 4432185029Spjd * 4433185029Spjd * 8. If an ARC buffer is written (and dirtied) which also exists in the 4434185029Spjd * L2ARC, the now stale L2ARC buffer is immediately dropped. 4435185029Spjd * 4436185029Spjd * The performance of the L2ARC can be tweaked by a number of tunables, which 4437185029Spjd * may be necessary for different workloads: 4438185029Spjd * 4439185029Spjd * l2arc_write_max max write bytes per interval 4440185029Spjd * l2arc_write_boost extra write bytes during device warmup 4441185029Spjd * l2arc_noprefetch skip caching prefetched buffers 4442185029Spjd * l2arc_headroom number of max device writes to precache 4443251478Sdelphij * l2arc_headroom_boost when we find compressed buffers during ARC 4444251478Sdelphij * scanning, we multiply headroom by this 4445251478Sdelphij * percentage factor for the next scan cycle, 4446251478Sdelphij * since more compressed buffers are likely to 4447251478Sdelphij * be present 4448185029Spjd * l2arc_feed_secs seconds between L2ARC writing 4449185029Spjd * 4450185029Spjd * Tunables may be removed or added as future performance improvements are 4451185029Spjd * integrated, and also may become zpool properties. 4452208373Smm * 4453208373Smm * There are three key functions that control how the L2ARC warms up: 4454208373Smm * 4455208373Smm * l2arc_write_eligible() check if a buffer is eligible to cache 4456208373Smm * l2arc_write_size() calculate how much to write 4457208373Smm * l2arc_write_interval() calculate sleep delay between writes 4458208373Smm * 4459208373Smm * These three functions determine what to write, how much, and how quickly 4460208373Smm * to send writes. 4461185029Spjd */ 4462185029Spjd 4463208373Smmstatic boolean_t 4464209962Smml2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 4465208373Smm{ 4466208373Smm /* 4467208373Smm * A buffer is *not* eligible for the L2ARC if it: 4468208373Smm * 1. belongs to a different spa. 4469208373Smm * 2. is already cached on the L2ARC. 4470208373Smm * 3. has an I/O in progress (it may be an incomplete read). 4471208373Smm * 4. is flagged not eligible (zfs property). 4472208373Smm */ 4473209962Smm if (ab->b_spa != spa_guid) { 4474208373Smm ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 4475208373Smm return (B_FALSE); 4476208373Smm } 4477208373Smm if (ab->b_l2hdr != NULL) { 4478208373Smm ARCSTAT_BUMP(arcstat_l2_write_in_l2); 4479208373Smm return (B_FALSE); 4480208373Smm } 4481208373Smm if (HDR_IO_IN_PROGRESS(ab)) { 4482208373Smm ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 4483208373Smm return (B_FALSE); 4484208373Smm } 4485208373Smm if (!HDR_L2CACHE(ab)) { 4486208373Smm ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 4487208373Smm return (B_FALSE); 4488208373Smm } 4489208373Smm 4490208373Smm return (B_TRUE); 4491208373Smm} 4492208373Smm 4493208373Smmstatic uint64_t 4494251478Sdelphijl2arc_write_size(void) 4495208373Smm{ 4496208373Smm uint64_t size; 4497208373Smm 4498251478Sdelphij /* 4499251478Sdelphij * Make sure our globals have meaningful values in case the user 4500251478Sdelphij * altered them. 4501251478Sdelphij */ 4502251478Sdelphij size = l2arc_write_max; 4503251478Sdelphij if (size == 0) { 4504251478Sdelphij cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 4505251478Sdelphij "be greater than zero, resetting it to the default (%d)", 4506251478Sdelphij L2ARC_WRITE_SIZE); 4507251478Sdelphij size = l2arc_write_max = L2ARC_WRITE_SIZE; 4508251478Sdelphij } 4509208373Smm 4510208373Smm if (arc_warm == B_FALSE) 4511251478Sdelphij size += l2arc_write_boost; 4512208373Smm 4513208373Smm return (size); 4514208373Smm 4515208373Smm} 4516208373Smm 4517208373Smmstatic clock_t 4518208373Smml2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 4519208373Smm{ 4520219089Spjd clock_t interval, next, now; 4521208373Smm 4522208373Smm /* 4523208373Smm * If the ARC lists are busy, increase our write rate; if the 4524208373Smm * lists are stale, idle back. This is achieved by checking 4525208373Smm * how much we previously wrote - if it was more than half of 4526208373Smm * what we wanted, schedule the next write much sooner. 4527208373Smm */ 4528208373Smm if (l2arc_feed_again && wrote > (wanted / 2)) 4529208373Smm interval = (hz * l2arc_feed_min_ms) / 1000; 4530208373Smm else 4531208373Smm interval = hz * l2arc_feed_secs; 4532208373Smm 4533219089Spjd now = ddi_get_lbolt(); 4534219089Spjd next = MAX(now, MIN(now + interval, began + interval)); 4535208373Smm 4536208373Smm return (next); 4537208373Smm} 4538208373Smm 4539185029Spjdstatic void 4540185029Spjdl2arc_hdr_stat_add(void) 4541185029Spjd{ 4542185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 4543185029Spjd ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 4544185029Spjd} 4545185029Spjd 4546185029Spjdstatic void 4547185029Spjdl2arc_hdr_stat_remove(void) 4548185029Spjd{ 4549185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 4550185029Spjd ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 4551185029Spjd} 4552185029Spjd 4553185029Spjd/* 4554185029Spjd * Cycle through L2ARC devices. This is how L2ARC load balances. 4555185029Spjd * If a device is returned, this also returns holding the spa config lock. 4556185029Spjd */ 4557185029Spjdstatic l2arc_dev_t * 4558185029Spjdl2arc_dev_get_next(void) 4559185029Spjd{ 4560185029Spjd l2arc_dev_t *first, *next = NULL; 4561185029Spjd 4562185029Spjd /* 4563185029Spjd * Lock out the removal of spas (spa_namespace_lock), then removal 4564185029Spjd * of cache devices (l2arc_dev_mtx). Once a device has been selected, 4565185029Spjd * both locks will be dropped and a spa config lock held instead. 4566185029Spjd */ 4567185029Spjd mutex_enter(&spa_namespace_lock); 4568185029Spjd mutex_enter(&l2arc_dev_mtx); 4569185029Spjd 4570185029Spjd /* if there are no vdevs, there is nothing to do */ 4571185029Spjd if (l2arc_ndev == 0) 4572185029Spjd goto out; 4573185029Spjd 4574185029Spjd first = NULL; 4575185029Spjd next = l2arc_dev_last; 4576185029Spjd do { 4577185029Spjd /* loop around the list looking for a non-faulted vdev */ 4578185029Spjd if (next == NULL) { 4579185029Spjd next = list_head(l2arc_dev_list); 4580185029Spjd } else { 4581185029Spjd next = list_next(l2arc_dev_list, next); 4582185029Spjd if (next == NULL) 4583185029Spjd next = list_head(l2arc_dev_list); 4584185029Spjd } 4585185029Spjd 4586185029Spjd /* if we have come back to the start, bail out */ 4587185029Spjd if (first == NULL) 4588185029Spjd first = next; 4589185029Spjd else if (next == first) 4590185029Spjd break; 4591185029Spjd 4592185029Spjd } while (vdev_is_dead(next->l2ad_vdev)); 4593185029Spjd 4594185029Spjd /* if we were unable to find any usable vdevs, return NULL */ 4595185029Spjd if (vdev_is_dead(next->l2ad_vdev)) 4596185029Spjd next = NULL; 4597185029Spjd 4598185029Spjd l2arc_dev_last = next; 4599185029Spjd 4600185029Spjdout: 4601185029Spjd mutex_exit(&l2arc_dev_mtx); 4602185029Spjd 4603185029Spjd /* 4604185029Spjd * Grab the config lock to prevent the 'next' device from being 4605185029Spjd * removed while we are writing to it. 4606185029Spjd */ 4607185029Spjd if (next != NULL) 4608185029Spjd spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 4609185029Spjd mutex_exit(&spa_namespace_lock); 4610185029Spjd 4611185029Spjd return (next); 4612185029Spjd} 4613185029Spjd 4614185029Spjd/* 4615185029Spjd * Free buffers that were tagged for destruction. 4616185029Spjd */ 4617185029Spjdstatic void 4618185029Spjdl2arc_do_free_on_write() 4619185029Spjd{ 4620185029Spjd list_t *buflist; 4621185029Spjd l2arc_data_free_t *df, *df_prev; 4622185029Spjd 4623185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 4624185029Spjd buflist = l2arc_free_on_write; 4625185029Spjd 4626185029Spjd for (df = list_tail(buflist); df; df = df_prev) { 4627185029Spjd df_prev = list_prev(buflist, df); 4628185029Spjd ASSERT(df->l2df_data != NULL); 4629185029Spjd ASSERT(df->l2df_func != NULL); 4630185029Spjd df->l2df_func(df->l2df_data, df->l2df_size); 4631185029Spjd list_remove(buflist, df); 4632185029Spjd kmem_free(df, sizeof (l2arc_data_free_t)); 4633185029Spjd } 4634185029Spjd 4635185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 4636185029Spjd} 4637185029Spjd 4638185029Spjd/* 4639185029Spjd * A write to a cache device has completed. Update all headers to allow 4640185029Spjd * reads from these buffers to begin. 4641185029Spjd */ 4642185029Spjdstatic void 4643185029Spjdl2arc_write_done(zio_t *zio) 4644185029Spjd{ 4645185029Spjd l2arc_write_callback_t *cb; 4646185029Spjd l2arc_dev_t *dev; 4647185029Spjd list_t *buflist; 4648185029Spjd arc_buf_hdr_t *head, *ab, *ab_prev; 4649185029Spjd l2arc_buf_hdr_t *abl2; 4650185029Spjd kmutex_t *hash_lock; 4651268085Sdelphij int64_t bytes_dropped = 0; 4652185029Spjd 4653185029Spjd cb = zio->io_private; 4654185029Spjd ASSERT(cb != NULL); 4655185029Spjd dev = cb->l2wcb_dev; 4656185029Spjd ASSERT(dev != NULL); 4657185029Spjd head = cb->l2wcb_head; 4658185029Spjd ASSERT(head != NULL); 4659185029Spjd buflist = dev->l2ad_buflist; 4660185029Spjd ASSERT(buflist != NULL); 4661185029Spjd DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 4662185029Spjd l2arc_write_callback_t *, cb); 4663185029Spjd 4664185029Spjd if (zio->io_error != 0) 4665185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_error); 4666185029Spjd 4667185029Spjd mutex_enter(&l2arc_buflist_mtx); 4668185029Spjd 4669185029Spjd /* 4670185029Spjd * All writes completed, or an error was hit. 4671185029Spjd */ 4672185029Spjd for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 4673185029Spjd ab_prev = list_prev(buflist, ab); 4674260835Sdelphij abl2 = ab->b_l2hdr; 4675185029Spjd 4676260835Sdelphij /* 4677260835Sdelphij * Release the temporary compressed buffer as soon as possible. 4678260835Sdelphij */ 4679260835Sdelphij if (abl2->b_compress != ZIO_COMPRESS_OFF) 4680260835Sdelphij l2arc_release_cdata_buf(ab); 4681260835Sdelphij 4682185029Spjd hash_lock = HDR_LOCK(ab); 4683185029Spjd if (!mutex_tryenter(hash_lock)) { 4684185029Spjd /* 4685185029Spjd * This buffer misses out. It may be in a stage 4686185029Spjd * of eviction. Its ARC_L2_WRITING flag will be 4687185029Spjd * left set, denying reads to this buffer. 4688185029Spjd */ 4689185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 4690185029Spjd continue; 4691185029Spjd } 4692185029Spjd 4693185029Spjd if (zio->io_error != 0) { 4694185029Spjd /* 4695185029Spjd * Error - drop L2ARC entry. 4696185029Spjd */ 4697185029Spjd list_remove(buflist, ab); 4698251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize); 4699268085Sdelphij bytes_dropped += abl2->b_asize; 4700185029Spjd ab->b_l2hdr = NULL; 4701248572Ssmh trim_map_free(abl2->b_dev->l2ad_vdev, abl2->b_daddr, 4702248574Ssmh ab->b_size, 0); 4703185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4704185029Spjd ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4705185029Spjd } 4706185029Spjd 4707185029Spjd /* 4708185029Spjd * Allow ARC to begin reads to this L2ARC entry. 4709185029Spjd */ 4710185029Spjd ab->b_flags &= ~ARC_L2_WRITING; 4711185029Spjd 4712185029Spjd mutex_exit(hash_lock); 4713185029Spjd } 4714185029Spjd 4715185029Spjd atomic_inc_64(&l2arc_writes_done); 4716185029Spjd list_remove(buflist, head); 4717185029Spjd kmem_cache_free(hdr_cache, head); 4718185029Spjd mutex_exit(&l2arc_buflist_mtx); 4719185029Spjd 4720268085Sdelphij vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 4721268085Sdelphij 4722185029Spjd l2arc_do_free_on_write(); 4723185029Spjd 4724185029Spjd kmem_free(cb, sizeof (l2arc_write_callback_t)); 4725185029Spjd} 4726185029Spjd 4727185029Spjd/* 4728185029Spjd * A read to a cache device completed. Validate buffer contents before 4729185029Spjd * handing over to the regular ARC routines. 4730185029Spjd */ 4731185029Spjdstatic void 4732185029Spjdl2arc_read_done(zio_t *zio) 4733185029Spjd{ 4734185029Spjd l2arc_read_callback_t *cb; 4735185029Spjd arc_buf_hdr_t *hdr; 4736185029Spjd arc_buf_t *buf; 4737185029Spjd kmutex_t *hash_lock; 4738185029Spjd int equal; 4739185029Spjd 4740185029Spjd ASSERT(zio->io_vd != NULL); 4741185029Spjd ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 4742185029Spjd 4743185029Spjd spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 4744185029Spjd 4745185029Spjd cb = zio->io_private; 4746185029Spjd ASSERT(cb != NULL); 4747185029Spjd buf = cb->l2rcb_buf; 4748185029Spjd ASSERT(buf != NULL); 4749185029Spjd 4750219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 4751185029Spjd mutex_enter(hash_lock); 4752219089Spjd hdr = buf->b_hdr; 4753219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4754185029Spjd 4755185029Spjd /* 4756251478Sdelphij * If the buffer was compressed, decompress it first. 4757251478Sdelphij */ 4758251478Sdelphij if (cb->l2rcb_compress != ZIO_COMPRESS_OFF) 4759251478Sdelphij l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress); 4760251478Sdelphij ASSERT(zio->io_data != NULL); 4761251478Sdelphij 4762251478Sdelphij /* 4763185029Spjd * Check this survived the L2ARC journey. 4764185029Spjd */ 4765185029Spjd equal = arc_cksum_equal(buf); 4766185029Spjd if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 4767185029Spjd mutex_exit(hash_lock); 4768185029Spjd zio->io_private = buf; 4769185029Spjd zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 4770185029Spjd zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 4771185029Spjd arc_read_done(zio); 4772185029Spjd } else { 4773185029Spjd mutex_exit(hash_lock); 4774185029Spjd /* 4775185029Spjd * Buffer didn't survive caching. Increment stats and 4776185029Spjd * reissue to the original storage device. 4777185029Spjd */ 4778185029Spjd if (zio->io_error != 0) { 4779185029Spjd ARCSTAT_BUMP(arcstat_l2_io_error); 4780185029Spjd } else { 4781249195Smm zio->io_error = SET_ERROR(EIO); 4782185029Spjd } 4783185029Spjd if (!equal) 4784185029Spjd ARCSTAT_BUMP(arcstat_l2_cksum_bad); 4785185029Spjd 4786185029Spjd /* 4787185029Spjd * If there's no waiter, issue an async i/o to the primary 4788185029Spjd * storage now. If there *is* a waiter, the caller must 4789185029Spjd * issue the i/o in a context where it's OK to block. 4790185029Spjd */ 4791209962Smm if (zio->io_waiter == NULL) { 4792209962Smm zio_t *pio = zio_unique_parent(zio); 4793209962Smm 4794209962Smm ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 4795209962Smm 4796209962Smm zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 4797185029Spjd buf->b_data, zio->io_size, arc_read_done, buf, 4798185029Spjd zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 4799209962Smm } 4800185029Spjd } 4801185029Spjd 4802185029Spjd kmem_free(cb, sizeof (l2arc_read_callback_t)); 4803185029Spjd} 4804185029Spjd 4805185029Spjd/* 4806185029Spjd * This is the list priority from which the L2ARC will search for pages to 4807185029Spjd * cache. This is used within loops (0..3) to cycle through lists in the 4808185029Spjd * desired order. This order can have a significant effect on cache 4809185029Spjd * performance. 4810185029Spjd * 4811185029Spjd * Currently the metadata lists are hit first, MFU then MRU, followed by 4812185029Spjd * the data lists. This function returns a locked list, and also returns 4813185029Spjd * the lock pointer. 4814185029Spjd */ 4815185029Spjdstatic list_t * 4816185029Spjdl2arc_list_locked(int list_num, kmutex_t **lock) 4817185029Spjd{ 4818247187Smm list_t *list = NULL; 4819205231Skmacy int idx; 4820185029Spjd 4821206796Spjd ASSERT(list_num >= 0 && list_num < 2 * ARC_BUFC_NUMLISTS); 4822206796Spjd 4823205231Skmacy if (list_num < ARC_BUFC_NUMMETADATALISTS) { 4824205231Skmacy idx = list_num; 4825205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4826205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4827206796Spjd } else if (list_num < ARC_BUFC_NUMMETADATALISTS * 2) { 4828205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4829205231Skmacy list = &arc_mru->arcs_lists[idx]; 4830205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4831206796Spjd } else if (list_num < (ARC_BUFC_NUMMETADATALISTS * 2 + 4832205231Skmacy ARC_BUFC_NUMDATALISTS)) { 4833205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4834205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4835205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4836205231Skmacy } else { 4837205231Skmacy idx = list_num - ARC_BUFC_NUMLISTS; 4838205231Skmacy list = &arc_mru->arcs_lists[idx]; 4839205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4840185029Spjd } 4841185029Spjd 4842185029Spjd ASSERT(!(MUTEX_HELD(*lock))); 4843185029Spjd mutex_enter(*lock); 4844185029Spjd return (list); 4845185029Spjd} 4846185029Spjd 4847185029Spjd/* 4848185029Spjd * Evict buffers from the device write hand to the distance specified in 4849185029Spjd * bytes. This distance may span populated buffers, it may span nothing. 4850185029Spjd * This is clearing a region on the L2ARC device ready for writing. 4851185029Spjd * If the 'all' boolean is set, every buffer is evicted. 4852185029Spjd */ 4853185029Spjdstatic void 4854185029Spjdl2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 4855185029Spjd{ 4856185029Spjd list_t *buflist; 4857185029Spjd l2arc_buf_hdr_t *abl2; 4858185029Spjd arc_buf_hdr_t *ab, *ab_prev; 4859185029Spjd kmutex_t *hash_lock; 4860185029Spjd uint64_t taddr; 4861268085Sdelphij int64_t bytes_evicted = 0; 4862185029Spjd 4863185029Spjd buflist = dev->l2ad_buflist; 4864185029Spjd 4865185029Spjd if (buflist == NULL) 4866185029Spjd return; 4867185029Spjd 4868185029Spjd if (!all && dev->l2ad_first) { 4869185029Spjd /* 4870185029Spjd * This is the first sweep through the device. There is 4871185029Spjd * nothing to evict. 4872185029Spjd */ 4873185029Spjd return; 4874185029Spjd } 4875185029Spjd 4876185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 4877185029Spjd /* 4878185029Spjd * When nearing the end of the device, evict to the end 4879185029Spjd * before the device write hand jumps to the start. 4880185029Spjd */ 4881185029Spjd taddr = dev->l2ad_end; 4882185029Spjd } else { 4883185029Spjd taddr = dev->l2ad_hand + distance; 4884185029Spjd } 4885185029Spjd DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 4886185029Spjd uint64_t, taddr, boolean_t, all); 4887185029Spjd 4888185029Spjdtop: 4889185029Spjd mutex_enter(&l2arc_buflist_mtx); 4890185029Spjd for (ab = list_tail(buflist); ab; ab = ab_prev) { 4891185029Spjd ab_prev = list_prev(buflist, ab); 4892185029Spjd 4893185029Spjd hash_lock = HDR_LOCK(ab); 4894185029Spjd if (!mutex_tryenter(hash_lock)) { 4895185029Spjd /* 4896185029Spjd * Missed the hash lock. Retry. 4897185029Spjd */ 4898185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 4899185029Spjd mutex_exit(&l2arc_buflist_mtx); 4900185029Spjd mutex_enter(hash_lock); 4901185029Spjd mutex_exit(hash_lock); 4902185029Spjd goto top; 4903185029Spjd } 4904185029Spjd 4905185029Spjd if (HDR_L2_WRITE_HEAD(ab)) { 4906185029Spjd /* 4907185029Spjd * We hit a write head node. Leave it for 4908185029Spjd * l2arc_write_done(). 4909185029Spjd */ 4910185029Spjd list_remove(buflist, ab); 4911185029Spjd mutex_exit(hash_lock); 4912185029Spjd continue; 4913185029Spjd } 4914185029Spjd 4915185029Spjd if (!all && ab->b_l2hdr != NULL && 4916185029Spjd (ab->b_l2hdr->b_daddr > taddr || 4917185029Spjd ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 4918185029Spjd /* 4919185029Spjd * We've evicted to the target address, 4920185029Spjd * or the end of the device. 4921185029Spjd */ 4922185029Spjd mutex_exit(hash_lock); 4923185029Spjd break; 4924185029Spjd } 4925185029Spjd 4926185029Spjd if (HDR_FREE_IN_PROGRESS(ab)) { 4927185029Spjd /* 4928185029Spjd * Already on the path to destruction. 4929185029Spjd */ 4930185029Spjd mutex_exit(hash_lock); 4931185029Spjd continue; 4932185029Spjd } 4933185029Spjd 4934185029Spjd if (ab->b_state == arc_l2c_only) { 4935185029Spjd ASSERT(!HDR_L2_READING(ab)); 4936185029Spjd /* 4937185029Spjd * This doesn't exist in the ARC. Destroy. 4938185029Spjd * arc_hdr_destroy() will call list_remove() 4939185029Spjd * and decrement arcstat_l2_size. 4940185029Spjd */ 4941185029Spjd arc_change_state(arc_anon, ab, hash_lock); 4942185029Spjd arc_hdr_destroy(ab); 4943185029Spjd } else { 4944185029Spjd /* 4945185029Spjd * Invalidate issued or about to be issued 4946185029Spjd * reads, since we may be about to write 4947185029Spjd * over this location. 4948185029Spjd */ 4949185029Spjd if (HDR_L2_READING(ab)) { 4950185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_reading); 4951185029Spjd ab->b_flags |= ARC_L2_EVICTED; 4952185029Spjd } 4953185029Spjd 4954185029Spjd /* 4955185029Spjd * Tell ARC this no longer exists in L2ARC. 4956185029Spjd */ 4957185029Spjd if (ab->b_l2hdr != NULL) { 4958185029Spjd abl2 = ab->b_l2hdr; 4959251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize); 4960268085Sdelphij bytes_evicted += abl2->b_asize; 4961185029Spjd ab->b_l2hdr = NULL; 4962185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4963185029Spjd ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4964185029Spjd } 4965185029Spjd list_remove(buflist, ab); 4966185029Spjd 4967185029Spjd /* 4968185029Spjd * This may have been leftover after a 4969185029Spjd * failed write. 4970185029Spjd */ 4971185029Spjd ab->b_flags &= ~ARC_L2_WRITING; 4972185029Spjd } 4973185029Spjd mutex_exit(hash_lock); 4974185029Spjd } 4975185029Spjd mutex_exit(&l2arc_buflist_mtx); 4976185029Spjd 4977268085Sdelphij vdev_space_update(dev->l2ad_vdev, -bytes_evicted, 0, 0); 4978185029Spjd dev->l2ad_evict = taddr; 4979185029Spjd} 4980185029Spjd 4981185029Spjd/* 4982185029Spjd * Find and write ARC buffers to the L2ARC device. 4983185029Spjd * 4984185029Spjd * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 4985185029Spjd * for reading until they have completed writing. 4986251478Sdelphij * The headroom_boost is an in-out parameter used to maintain headroom boost 4987251478Sdelphij * state between calls to this function. 4988251478Sdelphij * 4989251478Sdelphij * Returns the number of bytes actually written (which may be smaller than 4990251478Sdelphij * the delta by which the device hand has changed due to alignment). 4991185029Spjd */ 4992208373Smmstatic uint64_t 4993251478Sdelphijl2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz, 4994251478Sdelphij boolean_t *headroom_boost) 4995185029Spjd{ 4996185029Spjd arc_buf_hdr_t *ab, *ab_prev, *head; 4997185029Spjd list_t *list; 4998251478Sdelphij uint64_t write_asize, write_psize, write_sz, headroom, 4999251478Sdelphij buf_compress_minsz; 5000185029Spjd void *buf_data; 5001251478Sdelphij kmutex_t *list_lock; 5002251478Sdelphij boolean_t full; 5003185029Spjd l2arc_write_callback_t *cb; 5004185029Spjd zio_t *pio, *wzio; 5005228103Smm uint64_t guid = spa_load_guid(spa); 5006251478Sdelphij const boolean_t do_headroom_boost = *headroom_boost; 5007185029Spjd int try; 5008185029Spjd 5009185029Spjd ASSERT(dev->l2ad_vdev != NULL); 5010185029Spjd 5011251478Sdelphij /* Lower the flag now, we might want to raise it again later. */ 5012251478Sdelphij *headroom_boost = B_FALSE; 5013251478Sdelphij 5014185029Spjd pio = NULL; 5015251478Sdelphij write_sz = write_asize = write_psize = 0; 5016185029Spjd full = B_FALSE; 5017185029Spjd head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 5018185029Spjd head->b_flags |= ARC_L2_WRITE_HEAD; 5019185029Spjd 5020205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 5021185029Spjd /* 5022251478Sdelphij * We will want to try to compress buffers that are at least 2x the 5023251478Sdelphij * device sector size. 5024251478Sdelphij */ 5025251478Sdelphij buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift; 5026251478Sdelphij 5027251478Sdelphij /* 5028185029Spjd * Copy buffers for L2ARC writing. 5029185029Spjd */ 5030185029Spjd mutex_enter(&l2arc_buflist_mtx); 5031206796Spjd for (try = 0; try < 2 * ARC_BUFC_NUMLISTS; try++) { 5032251478Sdelphij uint64_t passed_sz = 0; 5033251478Sdelphij 5034185029Spjd list = l2arc_list_locked(try, &list_lock); 5035205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 5036185029Spjd 5037185029Spjd /* 5038185029Spjd * L2ARC fast warmup. 5039185029Spjd * 5040185029Spjd * Until the ARC is warm and starts to evict, read from the 5041185029Spjd * head of the ARC lists rather than the tail. 5042185029Spjd */ 5043185029Spjd if (arc_warm == B_FALSE) 5044185029Spjd ab = list_head(list); 5045185029Spjd else 5046185029Spjd ab = list_tail(list); 5047206796Spjd if (ab == NULL) 5048205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 5049185029Spjd 5050251478Sdelphij headroom = target_sz * l2arc_headroom; 5051251478Sdelphij if (do_headroom_boost) 5052251478Sdelphij headroom = (headroom * l2arc_headroom_boost) / 100; 5053251478Sdelphij 5054185029Spjd for (; ab; ab = ab_prev) { 5055251478Sdelphij l2arc_buf_hdr_t *l2hdr; 5056251478Sdelphij kmutex_t *hash_lock; 5057251478Sdelphij uint64_t buf_sz; 5058251478Sdelphij 5059185029Spjd if (arc_warm == B_FALSE) 5060185029Spjd ab_prev = list_next(list, ab); 5061185029Spjd else 5062185029Spjd ab_prev = list_prev(list, ab); 5063205231Skmacy ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, ab->b_size); 5064206796Spjd 5065185029Spjd hash_lock = HDR_LOCK(ab); 5066251478Sdelphij if (!mutex_tryenter(hash_lock)) { 5067205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 5068185029Spjd /* 5069185029Spjd * Skip this buffer rather than waiting. 5070185029Spjd */ 5071185029Spjd continue; 5072185029Spjd } 5073185029Spjd 5074185029Spjd passed_sz += ab->b_size; 5075185029Spjd if (passed_sz > headroom) { 5076185029Spjd /* 5077185029Spjd * Searched too far. 5078185029Spjd */ 5079185029Spjd mutex_exit(hash_lock); 5080205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 5081185029Spjd break; 5082185029Spjd } 5083185029Spjd 5084209962Smm if (!l2arc_write_eligible(guid, ab)) { 5085185029Spjd mutex_exit(hash_lock); 5086185029Spjd continue; 5087185029Spjd } 5088185029Spjd 5089185029Spjd if ((write_sz + ab->b_size) > target_sz) { 5090185029Spjd full = B_TRUE; 5091185029Spjd mutex_exit(hash_lock); 5092205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_full); 5093185029Spjd break; 5094185029Spjd } 5095185029Spjd 5096185029Spjd if (pio == NULL) { 5097185029Spjd /* 5098185029Spjd * Insert a dummy header on the buflist so 5099185029Spjd * l2arc_write_done() can find where the 5100185029Spjd * write buffers begin without searching. 5101185029Spjd */ 5102185029Spjd list_insert_head(dev->l2ad_buflist, head); 5103185029Spjd 5104185029Spjd cb = kmem_alloc( 5105185029Spjd sizeof (l2arc_write_callback_t), KM_SLEEP); 5106185029Spjd cb->l2wcb_dev = dev; 5107185029Spjd cb->l2wcb_head = head; 5108185029Spjd pio = zio_root(spa, l2arc_write_done, cb, 5109185029Spjd ZIO_FLAG_CANFAIL); 5110205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_pios); 5111185029Spjd } 5112185029Spjd 5113185029Spjd /* 5114185029Spjd * Create and add a new L2ARC header. 5115185029Spjd */ 5116251478Sdelphij l2hdr = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 5117251478Sdelphij l2hdr->b_dev = dev; 5118251478Sdelphij ab->b_flags |= ARC_L2_WRITING; 5119185029Spjd 5120251478Sdelphij /* 5121251478Sdelphij * Temporarily stash the data buffer in b_tmp_cdata. 5122251478Sdelphij * The subsequent write step will pick it up from 5123251478Sdelphij * there. This is because can't access ab->b_buf 5124251478Sdelphij * without holding the hash_lock, which we in turn 5125251478Sdelphij * can't access without holding the ARC list locks 5126251478Sdelphij * (which we want to avoid during compression/writing). 5127251478Sdelphij */ 5128251478Sdelphij l2hdr->b_compress = ZIO_COMPRESS_OFF; 5129251478Sdelphij l2hdr->b_asize = ab->b_size; 5130251478Sdelphij l2hdr->b_tmp_cdata = ab->b_buf->b_data; 5131251478Sdelphij 5132185029Spjd buf_sz = ab->b_size; 5133251478Sdelphij ab->b_l2hdr = l2hdr; 5134185029Spjd 5135251478Sdelphij list_insert_head(dev->l2ad_buflist, ab); 5136251478Sdelphij 5137185029Spjd /* 5138185029Spjd * Compute and store the buffer cksum before 5139185029Spjd * writing. On debug the cksum is verified first. 5140185029Spjd */ 5141185029Spjd arc_cksum_verify(ab->b_buf); 5142185029Spjd arc_cksum_compute(ab->b_buf, B_TRUE); 5143185029Spjd 5144185029Spjd mutex_exit(hash_lock); 5145185029Spjd 5146251478Sdelphij write_sz += buf_sz; 5147251478Sdelphij } 5148251478Sdelphij 5149251478Sdelphij mutex_exit(list_lock); 5150251478Sdelphij 5151251478Sdelphij if (full == B_TRUE) 5152251478Sdelphij break; 5153251478Sdelphij } 5154251478Sdelphij 5155251478Sdelphij /* No buffers selected for writing? */ 5156251478Sdelphij if (pio == NULL) { 5157251478Sdelphij ASSERT0(write_sz); 5158251478Sdelphij mutex_exit(&l2arc_buflist_mtx); 5159251478Sdelphij kmem_cache_free(hdr_cache, head); 5160251478Sdelphij return (0); 5161251478Sdelphij } 5162251478Sdelphij 5163251478Sdelphij /* 5164251478Sdelphij * Now start writing the buffers. We're starting at the write head 5165251478Sdelphij * and work backwards, retracing the course of the buffer selector 5166251478Sdelphij * loop above. 5167251478Sdelphij */ 5168251478Sdelphij for (ab = list_prev(dev->l2ad_buflist, head); ab; 5169251478Sdelphij ab = list_prev(dev->l2ad_buflist, ab)) { 5170251478Sdelphij l2arc_buf_hdr_t *l2hdr; 5171251478Sdelphij uint64_t buf_sz; 5172251478Sdelphij 5173251478Sdelphij /* 5174251478Sdelphij * We shouldn't need to lock the buffer here, since we flagged 5175251478Sdelphij * it as ARC_L2_WRITING in the previous step, but we must take 5176251478Sdelphij * care to only access its L2 cache parameters. In particular, 5177251478Sdelphij * ab->b_buf may be invalid by now due to ARC eviction. 5178251478Sdelphij */ 5179251478Sdelphij l2hdr = ab->b_l2hdr; 5180251478Sdelphij l2hdr->b_daddr = dev->l2ad_hand; 5181251478Sdelphij 5182251478Sdelphij if ((ab->b_flags & ARC_L2COMPRESS) && 5183251478Sdelphij l2hdr->b_asize >= buf_compress_minsz) { 5184251478Sdelphij if (l2arc_compress_buf(l2hdr)) { 5185251478Sdelphij /* 5186251478Sdelphij * If compression succeeded, enable headroom 5187251478Sdelphij * boost on the next scan cycle. 5188251478Sdelphij */ 5189251478Sdelphij *headroom_boost = B_TRUE; 5190251478Sdelphij } 5191251478Sdelphij } 5192251478Sdelphij 5193251478Sdelphij /* 5194251478Sdelphij * Pick up the buffer data we had previously stashed away 5195251478Sdelphij * (and now potentially also compressed). 5196251478Sdelphij */ 5197251478Sdelphij buf_data = l2hdr->b_tmp_cdata; 5198251478Sdelphij buf_sz = l2hdr->b_asize; 5199251478Sdelphij 5200251478Sdelphij /* Compression may have squashed the buffer to zero length. */ 5201251478Sdelphij if (buf_sz != 0) { 5202251478Sdelphij uint64_t buf_p_sz; 5203251478Sdelphij 5204185029Spjd wzio = zio_write_phys(pio, dev->l2ad_vdev, 5205185029Spjd dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 5206185029Spjd NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 5207185029Spjd ZIO_FLAG_CANFAIL, B_FALSE); 5208185029Spjd 5209185029Spjd DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 5210185029Spjd zio_t *, wzio); 5211185029Spjd (void) zio_nowait(wzio); 5212185029Spjd 5213251478Sdelphij write_asize += buf_sz; 5214185029Spjd /* 5215185029Spjd * Keep the clock hand suitably device-aligned. 5216185029Spjd */ 5217251478Sdelphij buf_p_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 5218251478Sdelphij write_psize += buf_p_sz; 5219251478Sdelphij dev->l2ad_hand += buf_p_sz; 5220185029Spjd } 5221251478Sdelphij } 5222185029Spjd 5223185029Spjd mutex_exit(&l2arc_buflist_mtx); 5224185029Spjd 5225251478Sdelphij ASSERT3U(write_asize, <=, target_sz); 5226185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_sent); 5227251478Sdelphij ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize); 5228185029Spjd ARCSTAT_INCR(arcstat_l2_size, write_sz); 5229251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, write_asize); 5230268085Sdelphij vdev_space_update(dev->l2ad_vdev, write_asize, 0, 0); 5231185029Spjd 5232185029Spjd /* 5233185029Spjd * Bump device hand to the device start if it is approaching the end. 5234185029Spjd * l2arc_evict() will already have evicted ahead for this case. 5235185029Spjd */ 5236185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 5237185029Spjd dev->l2ad_hand = dev->l2ad_start; 5238185029Spjd dev->l2ad_evict = dev->l2ad_start; 5239185029Spjd dev->l2ad_first = B_FALSE; 5240185029Spjd } 5241185029Spjd 5242208373Smm dev->l2ad_writing = B_TRUE; 5243185029Spjd (void) zio_wait(pio); 5244208373Smm dev->l2ad_writing = B_FALSE; 5245208373Smm 5246251478Sdelphij return (write_asize); 5247185029Spjd} 5248185029Spjd 5249185029Spjd/* 5250251478Sdelphij * Compresses an L2ARC buffer. 5251251478Sdelphij * The data to be compressed must be prefilled in l2hdr->b_tmp_cdata and its 5252251478Sdelphij * size in l2hdr->b_asize. This routine tries to compress the data and 5253251478Sdelphij * depending on the compression result there are three possible outcomes: 5254251478Sdelphij * *) The buffer was incompressible. The original l2hdr contents were left 5255251478Sdelphij * untouched and are ready for writing to an L2 device. 5256251478Sdelphij * *) The buffer was all-zeros, so there is no need to write it to an L2 5257251478Sdelphij * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is 5258251478Sdelphij * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY. 5259251478Sdelphij * *) Compression succeeded and b_tmp_cdata was replaced with a temporary 5260251478Sdelphij * data buffer which holds the compressed data to be written, and b_asize 5261251478Sdelphij * tells us how much data there is. b_compress is set to the appropriate 5262251478Sdelphij * compression algorithm. Once writing is done, invoke 5263251478Sdelphij * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer. 5264251478Sdelphij * 5265251478Sdelphij * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the 5266251478Sdelphij * buffer was incompressible). 5267251478Sdelphij */ 5268251478Sdelphijstatic boolean_t 5269251478Sdelphijl2arc_compress_buf(l2arc_buf_hdr_t *l2hdr) 5270251478Sdelphij{ 5271251478Sdelphij void *cdata; 5272268075Sdelphij size_t csize, len, rounded; 5273251478Sdelphij 5274251478Sdelphij ASSERT(l2hdr->b_compress == ZIO_COMPRESS_OFF); 5275251478Sdelphij ASSERT(l2hdr->b_tmp_cdata != NULL); 5276251478Sdelphij 5277251478Sdelphij len = l2hdr->b_asize; 5278251478Sdelphij cdata = zio_data_buf_alloc(len); 5279251478Sdelphij csize = zio_compress_data(ZIO_COMPRESS_LZ4, l2hdr->b_tmp_cdata, 5280269086Sdelphij cdata, l2hdr->b_asize); 5281251478Sdelphij 5282268075Sdelphij rounded = P2ROUNDUP(csize, (size_t)SPA_MINBLOCKSIZE); 5283268075Sdelphij if (rounded > csize) { 5284268075Sdelphij bzero((char *)cdata + csize, rounded - csize); 5285268075Sdelphij csize = rounded; 5286268075Sdelphij } 5287268075Sdelphij 5288251478Sdelphij if (csize == 0) { 5289251478Sdelphij /* zero block, indicate that there's nothing to write */ 5290251478Sdelphij zio_data_buf_free(cdata, len); 5291251478Sdelphij l2hdr->b_compress = ZIO_COMPRESS_EMPTY; 5292251478Sdelphij l2hdr->b_asize = 0; 5293251478Sdelphij l2hdr->b_tmp_cdata = NULL; 5294251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_zeros); 5295251478Sdelphij return (B_TRUE); 5296251478Sdelphij } else if (csize > 0 && csize < len) { 5297251478Sdelphij /* 5298251478Sdelphij * Compression succeeded, we'll keep the cdata around for 5299251478Sdelphij * writing and release it afterwards. 5300251478Sdelphij */ 5301251478Sdelphij l2hdr->b_compress = ZIO_COMPRESS_LZ4; 5302251478Sdelphij l2hdr->b_asize = csize; 5303251478Sdelphij l2hdr->b_tmp_cdata = cdata; 5304251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_successes); 5305251478Sdelphij return (B_TRUE); 5306251478Sdelphij } else { 5307251478Sdelphij /* 5308251478Sdelphij * Compression failed, release the compressed buffer. 5309251478Sdelphij * l2hdr will be left unmodified. 5310251478Sdelphij */ 5311251478Sdelphij zio_data_buf_free(cdata, len); 5312251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_failures); 5313251478Sdelphij return (B_FALSE); 5314251478Sdelphij } 5315251478Sdelphij} 5316251478Sdelphij 5317251478Sdelphij/* 5318251478Sdelphij * Decompresses a zio read back from an l2arc device. On success, the 5319251478Sdelphij * underlying zio's io_data buffer is overwritten by the uncompressed 5320251478Sdelphij * version. On decompression error (corrupt compressed stream), the 5321251478Sdelphij * zio->io_error value is set to signal an I/O error. 5322251478Sdelphij * 5323251478Sdelphij * Please note that the compressed data stream is not checksummed, so 5324251478Sdelphij * if the underlying device is experiencing data corruption, we may feed 5325251478Sdelphij * corrupt data to the decompressor, so the decompressor needs to be 5326251478Sdelphij * able to handle this situation (LZ4 does). 5327251478Sdelphij */ 5328251478Sdelphijstatic void 5329251478Sdelphijl2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c) 5330251478Sdelphij{ 5331251478Sdelphij ASSERT(L2ARC_IS_VALID_COMPRESS(c)); 5332251478Sdelphij 5333251478Sdelphij if (zio->io_error != 0) { 5334251478Sdelphij /* 5335251478Sdelphij * An io error has occured, just restore the original io 5336251478Sdelphij * size in preparation for a main pool read. 5337251478Sdelphij */ 5338251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 5339251478Sdelphij return; 5340251478Sdelphij } 5341251478Sdelphij 5342251478Sdelphij if (c == ZIO_COMPRESS_EMPTY) { 5343251478Sdelphij /* 5344251478Sdelphij * An empty buffer results in a null zio, which means we 5345251478Sdelphij * need to fill its io_data after we're done restoring the 5346251478Sdelphij * buffer's contents. 5347251478Sdelphij */ 5348251478Sdelphij ASSERT(hdr->b_buf != NULL); 5349251478Sdelphij bzero(hdr->b_buf->b_data, hdr->b_size); 5350251478Sdelphij zio->io_data = zio->io_orig_data = hdr->b_buf->b_data; 5351251478Sdelphij } else { 5352251478Sdelphij ASSERT(zio->io_data != NULL); 5353251478Sdelphij /* 5354251478Sdelphij * We copy the compressed data from the start of the arc buffer 5355251478Sdelphij * (the zio_read will have pulled in only what we need, the 5356251478Sdelphij * rest is garbage which we will overwrite at decompression) 5357251478Sdelphij * and then decompress back to the ARC data buffer. This way we 5358251478Sdelphij * can minimize copying by simply decompressing back over the 5359251478Sdelphij * original compressed data (rather than decompressing to an 5360251478Sdelphij * aux buffer and then copying back the uncompressed buffer, 5361251478Sdelphij * which is likely to be much larger). 5362251478Sdelphij */ 5363251478Sdelphij uint64_t csize; 5364251478Sdelphij void *cdata; 5365251478Sdelphij 5366251478Sdelphij csize = zio->io_size; 5367251478Sdelphij cdata = zio_data_buf_alloc(csize); 5368251478Sdelphij bcopy(zio->io_data, cdata, csize); 5369251478Sdelphij if (zio_decompress_data(c, cdata, zio->io_data, csize, 5370251478Sdelphij hdr->b_size) != 0) 5371251478Sdelphij zio->io_error = EIO; 5372251478Sdelphij zio_data_buf_free(cdata, csize); 5373251478Sdelphij } 5374251478Sdelphij 5375251478Sdelphij /* Restore the expected uncompressed IO size. */ 5376251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 5377251478Sdelphij} 5378251478Sdelphij 5379251478Sdelphij/* 5380251478Sdelphij * Releases the temporary b_tmp_cdata buffer in an l2arc header structure. 5381251478Sdelphij * This buffer serves as a temporary holder of compressed data while 5382251478Sdelphij * the buffer entry is being written to an l2arc device. Once that is 5383251478Sdelphij * done, we can dispose of it. 5384251478Sdelphij */ 5385251478Sdelphijstatic void 5386251478Sdelphijl2arc_release_cdata_buf(arc_buf_hdr_t *ab) 5387251478Sdelphij{ 5388251478Sdelphij l2arc_buf_hdr_t *l2hdr = ab->b_l2hdr; 5389251478Sdelphij 5390251478Sdelphij if (l2hdr->b_compress == ZIO_COMPRESS_LZ4) { 5391251478Sdelphij /* 5392251478Sdelphij * If the data was compressed, then we've allocated a 5393251478Sdelphij * temporary buffer for it, so now we need to release it. 5394251478Sdelphij */ 5395251478Sdelphij ASSERT(l2hdr->b_tmp_cdata != NULL); 5396251478Sdelphij zio_data_buf_free(l2hdr->b_tmp_cdata, ab->b_size); 5397251478Sdelphij } 5398251478Sdelphij l2hdr->b_tmp_cdata = NULL; 5399251478Sdelphij} 5400251478Sdelphij 5401251478Sdelphij/* 5402185029Spjd * This thread feeds the L2ARC at regular intervals. This is the beating 5403185029Spjd * heart of the L2ARC. 5404185029Spjd */ 5405185029Spjdstatic void 5406185029Spjdl2arc_feed_thread(void *dummy __unused) 5407185029Spjd{ 5408185029Spjd callb_cpr_t cpr; 5409185029Spjd l2arc_dev_t *dev; 5410185029Spjd spa_t *spa; 5411208373Smm uint64_t size, wrote; 5412219089Spjd clock_t begin, next = ddi_get_lbolt(); 5413251478Sdelphij boolean_t headroom_boost = B_FALSE; 5414185029Spjd 5415185029Spjd CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 5416185029Spjd 5417185029Spjd mutex_enter(&l2arc_feed_thr_lock); 5418185029Spjd 5419185029Spjd while (l2arc_thread_exit == 0) { 5420185029Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 5421185029Spjd (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 5422219089Spjd next - ddi_get_lbolt()); 5423185029Spjd CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 5424219089Spjd next = ddi_get_lbolt() + hz; 5425185029Spjd 5426185029Spjd /* 5427185029Spjd * Quick check for L2ARC devices. 5428185029Spjd */ 5429185029Spjd mutex_enter(&l2arc_dev_mtx); 5430185029Spjd if (l2arc_ndev == 0) { 5431185029Spjd mutex_exit(&l2arc_dev_mtx); 5432185029Spjd continue; 5433185029Spjd } 5434185029Spjd mutex_exit(&l2arc_dev_mtx); 5435219089Spjd begin = ddi_get_lbolt(); 5436185029Spjd 5437185029Spjd /* 5438185029Spjd * This selects the next l2arc device to write to, and in 5439185029Spjd * doing so the next spa to feed from: dev->l2ad_spa. This 5440185029Spjd * will return NULL if there are now no l2arc devices or if 5441185029Spjd * they are all faulted. 5442185029Spjd * 5443185029Spjd * If a device is returned, its spa's config lock is also 5444185029Spjd * held to prevent device removal. l2arc_dev_get_next() 5445185029Spjd * will grab and release l2arc_dev_mtx. 5446185029Spjd */ 5447185029Spjd if ((dev = l2arc_dev_get_next()) == NULL) 5448185029Spjd continue; 5449185029Spjd 5450185029Spjd spa = dev->l2ad_spa; 5451185029Spjd ASSERT(spa != NULL); 5452185029Spjd 5453185029Spjd /* 5454219089Spjd * If the pool is read-only then force the feed thread to 5455219089Spjd * sleep a little longer. 5456219089Spjd */ 5457219089Spjd if (!spa_writeable(spa)) { 5458219089Spjd next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 5459219089Spjd spa_config_exit(spa, SCL_L2ARC, dev); 5460219089Spjd continue; 5461219089Spjd } 5462219089Spjd 5463219089Spjd /* 5464185029Spjd * Avoid contributing to memory pressure. 5465185029Spjd */ 5466185029Spjd if (arc_reclaim_needed()) { 5467185029Spjd ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 5468185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 5469185029Spjd continue; 5470185029Spjd } 5471185029Spjd 5472185029Spjd ARCSTAT_BUMP(arcstat_l2_feeds); 5473185029Spjd 5474251478Sdelphij size = l2arc_write_size(); 5475185029Spjd 5476185029Spjd /* 5477185029Spjd * Evict L2ARC buffers that will be overwritten. 5478185029Spjd */ 5479185029Spjd l2arc_evict(dev, size, B_FALSE); 5480185029Spjd 5481185029Spjd /* 5482185029Spjd * Write ARC buffers. 5483185029Spjd */ 5484251478Sdelphij wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost); 5485208373Smm 5486208373Smm /* 5487208373Smm * Calculate interval between writes. 5488208373Smm */ 5489208373Smm next = l2arc_write_interval(begin, size, wrote); 5490185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 5491185029Spjd } 5492185029Spjd 5493185029Spjd l2arc_thread_exit = 0; 5494185029Spjd cv_broadcast(&l2arc_feed_thr_cv); 5495185029Spjd CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 5496185029Spjd thread_exit(); 5497185029Spjd} 5498185029Spjd 5499185029Spjdboolean_t 5500185029Spjdl2arc_vdev_present(vdev_t *vd) 5501185029Spjd{ 5502185029Spjd l2arc_dev_t *dev; 5503185029Spjd 5504185029Spjd mutex_enter(&l2arc_dev_mtx); 5505185029Spjd for (dev = list_head(l2arc_dev_list); dev != NULL; 5506185029Spjd dev = list_next(l2arc_dev_list, dev)) { 5507185029Spjd if (dev->l2ad_vdev == vd) 5508185029Spjd break; 5509185029Spjd } 5510185029Spjd mutex_exit(&l2arc_dev_mtx); 5511185029Spjd 5512185029Spjd return (dev != NULL); 5513185029Spjd} 5514185029Spjd 5515185029Spjd/* 5516185029Spjd * Add a vdev for use by the L2ARC. By this point the spa has already 5517185029Spjd * validated the vdev and opened it. 5518185029Spjd */ 5519185029Spjdvoid 5520219089Spjdl2arc_add_vdev(spa_t *spa, vdev_t *vd) 5521185029Spjd{ 5522185029Spjd l2arc_dev_t *adddev; 5523185029Spjd 5524185029Spjd ASSERT(!l2arc_vdev_present(vd)); 5525185029Spjd 5526255753Sgibbs vdev_ashift_optimize(vd); 5527255753Sgibbs 5528185029Spjd /* 5529185029Spjd * Create a new l2arc device entry. 5530185029Spjd */ 5531185029Spjd adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 5532185029Spjd adddev->l2ad_spa = spa; 5533185029Spjd adddev->l2ad_vdev = vd; 5534219089Spjd adddev->l2ad_start = VDEV_LABEL_START_SIZE; 5535219089Spjd adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 5536185029Spjd adddev->l2ad_hand = adddev->l2ad_start; 5537185029Spjd adddev->l2ad_evict = adddev->l2ad_start; 5538185029Spjd adddev->l2ad_first = B_TRUE; 5539208373Smm adddev->l2ad_writing = B_FALSE; 5540185029Spjd 5541185029Spjd /* 5542185029Spjd * This is a list of all ARC buffers that are still valid on the 5543185029Spjd * device. 5544185029Spjd */ 5545185029Spjd adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 5546185029Spjd list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 5547185029Spjd offsetof(arc_buf_hdr_t, b_l2node)); 5548185029Spjd 5549219089Spjd vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 5550185029Spjd 5551185029Spjd /* 5552185029Spjd * Add device to global list 5553185029Spjd */ 5554185029Spjd mutex_enter(&l2arc_dev_mtx); 5555185029Spjd list_insert_head(l2arc_dev_list, adddev); 5556185029Spjd atomic_inc_64(&l2arc_ndev); 5557185029Spjd mutex_exit(&l2arc_dev_mtx); 5558185029Spjd} 5559185029Spjd 5560185029Spjd/* 5561185029Spjd * Remove a vdev from the L2ARC. 5562185029Spjd */ 5563185029Spjdvoid 5564185029Spjdl2arc_remove_vdev(vdev_t *vd) 5565185029Spjd{ 5566185029Spjd l2arc_dev_t *dev, *nextdev, *remdev = NULL; 5567185029Spjd 5568185029Spjd /* 5569185029Spjd * Find the device by vdev 5570185029Spjd */ 5571185029Spjd mutex_enter(&l2arc_dev_mtx); 5572185029Spjd for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 5573185029Spjd nextdev = list_next(l2arc_dev_list, dev); 5574185029Spjd if (vd == dev->l2ad_vdev) { 5575185029Spjd remdev = dev; 5576185029Spjd break; 5577185029Spjd } 5578185029Spjd } 5579185029Spjd ASSERT(remdev != NULL); 5580185029Spjd 5581185029Spjd /* 5582185029Spjd * Remove device from global list 5583185029Spjd */ 5584185029Spjd list_remove(l2arc_dev_list, remdev); 5585185029Spjd l2arc_dev_last = NULL; /* may have been invalidated */ 5586185029Spjd atomic_dec_64(&l2arc_ndev); 5587185029Spjd mutex_exit(&l2arc_dev_mtx); 5588185029Spjd 5589185029Spjd /* 5590185029Spjd * Clear all buflists and ARC references. L2ARC device flush. 5591185029Spjd */ 5592185029Spjd l2arc_evict(remdev, 0, B_TRUE); 5593185029Spjd list_destroy(remdev->l2ad_buflist); 5594185029Spjd kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 5595185029Spjd kmem_free(remdev, sizeof (l2arc_dev_t)); 5596185029Spjd} 5597185029Spjd 5598185029Spjdvoid 5599185029Spjdl2arc_init(void) 5600185029Spjd{ 5601185029Spjd l2arc_thread_exit = 0; 5602185029Spjd l2arc_ndev = 0; 5603185029Spjd l2arc_writes_sent = 0; 5604185029Spjd l2arc_writes_done = 0; 5605185029Spjd 5606185029Spjd mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 5607185029Spjd cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 5608185029Spjd mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 5609185029Spjd mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 5610185029Spjd mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 5611185029Spjd 5612185029Spjd l2arc_dev_list = &L2ARC_dev_list; 5613185029Spjd l2arc_free_on_write = &L2ARC_free_on_write; 5614185029Spjd list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 5615185029Spjd offsetof(l2arc_dev_t, l2ad_node)); 5616185029Spjd list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 5617185029Spjd offsetof(l2arc_data_free_t, l2df_list_node)); 5618185029Spjd} 5619185029Spjd 5620185029Spjdvoid 5621185029Spjdl2arc_fini(void) 5622185029Spjd{ 5623185029Spjd /* 5624185029Spjd * This is called from dmu_fini(), which is called from spa_fini(); 5625185029Spjd * Because of this, we can assume that all l2arc devices have 5626185029Spjd * already been removed when the pools themselves were removed. 5627185029Spjd */ 5628185029Spjd 5629185029Spjd l2arc_do_free_on_write(); 5630185029Spjd 5631185029Spjd mutex_destroy(&l2arc_feed_thr_lock); 5632185029Spjd cv_destroy(&l2arc_feed_thr_cv); 5633185029Spjd mutex_destroy(&l2arc_dev_mtx); 5634185029Spjd mutex_destroy(&l2arc_buflist_mtx); 5635185029Spjd mutex_destroy(&l2arc_free_on_write_mtx); 5636185029Spjd 5637185029Spjd list_destroy(l2arc_dev_list); 5638185029Spjd list_destroy(l2arc_free_on_write); 5639185029Spjd} 5640185029Spjd 5641185029Spjdvoid 5642185029Spjdl2arc_start(void) 5643185029Spjd{ 5644209962Smm if (!(spa_mode_global & FWRITE)) 5645185029Spjd return; 5646185029Spjd 5647185029Spjd (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 5648185029Spjd TS_RUN, minclsyspri); 5649185029Spjd} 5650185029Spjd 5651185029Spjdvoid 5652185029Spjdl2arc_stop(void) 5653185029Spjd{ 5654209962Smm if (!(spa_mode_global & FWRITE)) 5655185029Spjd return; 5656185029Spjd 5657185029Spjd mutex_enter(&l2arc_feed_thr_lock); 5658185029Spjd cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 5659185029Spjd l2arc_thread_exit = 1; 5660185029Spjd while (l2arc_thread_exit != 0) 5661185029Spjd cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 5662185029Spjd mutex_exit(&l2arc_feed_thr_lock); 5663185029Spjd} 5664