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. 23268657Sdelphij * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 24262115Savg * Copyright (c) 2014 by Saso Kiselkov. All rights reserved. 25268654Sdelphij * 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 107269417Sdelphij * 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> 130260763Savg#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> 141272875Ssmh#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 163260763Savg/* 164260763Savg * The number of iterations through arc_evict_*() before we 165260763Savg * drop & reacquire the lock. 166260763Savg */ 167260763Savgint arc_evict_iterations = 100; 168260763Savg 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 184260763Savg/* 185260763Savg * If this percent of memory is free, don't throttle. 186260763Savg */ 187260763Savgint arc_lotsfree_percent = 10; 188260763Savg 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; 204269846Sdelphijuint64_t zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ 205272875Ssmhu_int zfs_arc_free_target = 0; 206185029Spjd 207272875Ssmhstatic int sysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS); 208277583Sdelphijstatic int sysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS); 209272875Ssmh 210272875Ssmh#ifdef _KERNEL 211272875Ssmhstatic void 212272875Ssmharc_free_target_init(void *unused __unused) 213272875Ssmh{ 214272875Ssmh 215272875Ssmh zfs_arc_free_target = vm_pageout_wakeup_thresh; 216272875Ssmh} 217272875SsmhSYSINIT(arc_free_target_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_ANY, 218272875Ssmh arc_free_target_init, NULL); 219272875Ssmh 220185029SpjdTUNABLE_QUAD("vfs.zfs.arc_max", &zfs_arc_max); 221185029SpjdTUNABLE_QUAD("vfs.zfs.arc_min", &zfs_arc_min); 222185029SpjdTUNABLE_QUAD("vfs.zfs.arc_meta_limit", &zfs_arc_meta_limit); 223269846SdelphijTUNABLE_QUAD("vfs.zfs.arc_average_blocksize", &zfs_arc_average_blocksize); 224273984SdelphijTUNABLE_INT("vfs.zfs.arc_shrink_shift", &zfs_arc_shrink_shift); 225168473SpjdSYSCTL_DECL(_vfs_zfs); 226217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_max, CTLFLAG_RDTUN, &zfs_arc_max, 0, 227168473Spjd "Maximum ARC size"); 228217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_min, CTLFLAG_RDTUN, &zfs_arc_min, 0, 229168473Spjd "Minimum ARC size"); 230269846SdelphijSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_average_blocksize, CTLFLAG_RDTUN, 231269846Sdelphij &zfs_arc_average_blocksize, 0, 232269846Sdelphij "ARC average blocksize"); 233273984SdelphijSYSCTL_INT(_vfs_zfs, OID_AUTO, arc_shrink_shift, CTLFLAG_RW, 234273984Sdelphij &arc_shrink_shift, 0, 235273984Sdelphij "log2(fraction of arc to reclaim)"); 236273984Sdelphij 237272875Ssmh/* 238272875Ssmh * We don't have a tunable for arc_free_target due to the dependency on 239272875Ssmh * pagedaemon initialisation. 240272875Ssmh */ 241272875SsmhSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_free_target, 242272875Ssmh CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(u_int), 243272875Ssmh sysctl_vfs_zfs_arc_free_target, "IU", 244272875Ssmh "Desired number of free pages below which ARC triggers reclaim"); 245168404Spjd 246272875Ssmhstatic int 247272875Ssmhsysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS) 248272875Ssmh{ 249272875Ssmh u_int val; 250272875Ssmh int err; 251272875Ssmh 252272875Ssmh val = zfs_arc_free_target; 253272875Ssmh err = sysctl_handle_int(oidp, &val, 0, req); 254272875Ssmh if (err != 0 || req->newptr == NULL) 255272875Ssmh return (err); 256272875Ssmh 257272875Ssmh if (val < minfree) 258272875Ssmh return (EINVAL); 259272875Ssmh if (val > cnt.v_page_count) 260272875Ssmh return (EINVAL); 261272875Ssmh 262272875Ssmh zfs_arc_free_target = val; 263272875Ssmh 264272875Ssmh return (0); 265272875Ssmh} 266277583Sdelphij 267277583Sdelphij/* 268277583Sdelphij * Must be declared here, before the definition of corresponding kstat 269277583Sdelphij * macro which uses the same names will confuse the compiler. 270277583Sdelphij */ 271277583SdelphijSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_meta_limit, 272277583Sdelphij CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t), 273277583Sdelphij sysctl_vfs_zfs_arc_meta_limit, "QU", 274277583Sdelphij "ARC metadata limit"); 275272875Ssmh#endif 276272875Ssmh 277168404Spjd/* 278185029Spjd * Note that buffers can be in one of 6 states: 279168404Spjd * ARC_anon - anonymous (discussed below) 280168404Spjd * ARC_mru - recently used, currently cached 281168404Spjd * ARC_mru_ghost - recentely used, no longer in cache 282168404Spjd * ARC_mfu - frequently used, currently cached 283168404Spjd * ARC_mfu_ghost - frequently used, no longer in cache 284185029Spjd * ARC_l2c_only - exists in L2ARC but not other states 285185029Spjd * When there are no active references to the buffer, they are 286185029Spjd * are linked onto a list in one of these arc states. These are 287185029Spjd * the only buffers that can be evicted or deleted. Within each 288185029Spjd * state there are multiple lists, one for meta-data and one for 289185029Spjd * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 290185029Spjd * etc.) is tracked separately so that it can be managed more 291185029Spjd * explicitly: favored over data, limited explicitly. 292168404Spjd * 293168404Spjd * Anonymous buffers are buffers that are not associated with 294168404Spjd * a DVA. These are buffers that hold dirty block copies 295168404Spjd * before they are written to stable storage. By definition, 296168404Spjd * they are "ref'd" and are considered part of arc_mru 297168404Spjd * that cannot be freed. Generally, they will aquire a DVA 298168404Spjd * as they are written and migrate onto the arc_mru list. 299185029Spjd * 300185029Spjd * The ARC_l2c_only state is for buffers that are in the second 301185029Spjd * level ARC but no longer in any of the ARC_m* lists. The second 302185029Spjd * level ARC itself may also contain buffers that are in any of 303185029Spjd * the ARC_m* states - meaning that a buffer can exist in two 304185029Spjd * places. The reason for the ARC_l2c_only state is to keep the 305185029Spjd * buffer header in the hash table, so that reads that hit the 306185029Spjd * second level ARC benefit from these fast lookups. 307168404Spjd */ 308168404Spjd 309205264Skmacy#define ARCS_LOCK_PAD CACHE_LINE_SIZE 310205231Skmacystruct arcs_lock { 311205231Skmacy kmutex_t arcs_lock; 312205231Skmacy#ifdef _KERNEL 313205231Skmacy unsigned char pad[(ARCS_LOCK_PAD - sizeof (kmutex_t))]; 314205231Skmacy#endif 315205231Skmacy}; 316205231Skmacy 317205231Skmacy/* 318205231Skmacy * must be power of two for mask use to work 319205231Skmacy * 320205231Skmacy */ 321205231Skmacy#define ARC_BUFC_NUMDATALISTS 16 322205231Skmacy#define ARC_BUFC_NUMMETADATALISTS 16 323206796Spjd#define ARC_BUFC_NUMLISTS (ARC_BUFC_NUMMETADATALISTS + ARC_BUFC_NUMDATALISTS) 324205231Skmacy 325168404Spjdtypedef struct arc_state { 326185029Spjd uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 327185029Spjd uint64_t arcs_size; /* total amount of data in this state */ 328205231Skmacy list_t arcs_lists[ARC_BUFC_NUMLISTS]; /* list of evictable buffers */ 329205264Skmacy struct arcs_lock arcs_locks[ARC_BUFC_NUMLISTS] __aligned(CACHE_LINE_SIZE); 330168404Spjd} arc_state_t; 331168404Spjd 332206796Spjd#define ARCS_LOCK(s, i) (&((s)->arcs_locks[(i)].arcs_lock)) 333205231Skmacy 334185029Spjd/* The 6 states: */ 335168404Spjdstatic arc_state_t ARC_anon; 336168404Spjdstatic arc_state_t ARC_mru; 337168404Spjdstatic arc_state_t ARC_mru_ghost; 338168404Spjdstatic arc_state_t ARC_mfu; 339168404Spjdstatic arc_state_t ARC_mfu_ghost; 340185029Spjdstatic arc_state_t ARC_l2c_only; 341168404Spjd 342168404Spjdtypedef struct arc_stats { 343168404Spjd kstat_named_t arcstat_hits; 344168404Spjd kstat_named_t arcstat_misses; 345168404Spjd kstat_named_t arcstat_demand_data_hits; 346168404Spjd kstat_named_t arcstat_demand_data_misses; 347168404Spjd kstat_named_t arcstat_demand_metadata_hits; 348168404Spjd kstat_named_t arcstat_demand_metadata_misses; 349168404Spjd kstat_named_t arcstat_prefetch_data_hits; 350168404Spjd kstat_named_t arcstat_prefetch_data_misses; 351168404Spjd kstat_named_t arcstat_prefetch_metadata_hits; 352168404Spjd kstat_named_t arcstat_prefetch_metadata_misses; 353168404Spjd kstat_named_t arcstat_mru_hits; 354168404Spjd kstat_named_t arcstat_mru_ghost_hits; 355168404Spjd kstat_named_t arcstat_mfu_hits; 356168404Spjd kstat_named_t arcstat_mfu_ghost_hits; 357205231Skmacy kstat_named_t arcstat_allocated; 358168404Spjd kstat_named_t arcstat_deleted; 359205231Skmacy kstat_named_t arcstat_stolen; 360168404Spjd kstat_named_t arcstat_recycle_miss; 361251629Sdelphij /* 362251629Sdelphij * Number of buffers that could not be evicted because the hash lock 363251629Sdelphij * was held by another thread. The lock may not necessarily be held 364251629Sdelphij * by something using the same buffer, since hash locks are shared 365251629Sdelphij * by multiple buffers. 366251629Sdelphij */ 367168404Spjd kstat_named_t arcstat_mutex_miss; 368251629Sdelphij /* 369251629Sdelphij * Number of buffers skipped because they have I/O in progress, are 370251629Sdelphij * indrect prefetch buffers that have not lived long enough, or are 371251629Sdelphij * not from the spa we're trying to evict from. 372251629Sdelphij */ 373168404Spjd kstat_named_t arcstat_evict_skip; 374208373Smm kstat_named_t arcstat_evict_l2_cached; 375208373Smm kstat_named_t arcstat_evict_l2_eligible; 376208373Smm kstat_named_t arcstat_evict_l2_ineligible; 377168404Spjd kstat_named_t arcstat_hash_elements; 378168404Spjd kstat_named_t arcstat_hash_elements_max; 379168404Spjd kstat_named_t arcstat_hash_collisions; 380168404Spjd kstat_named_t arcstat_hash_chains; 381168404Spjd kstat_named_t arcstat_hash_chain_max; 382168404Spjd kstat_named_t arcstat_p; 383168404Spjd kstat_named_t arcstat_c; 384168404Spjd kstat_named_t arcstat_c_min; 385168404Spjd kstat_named_t arcstat_c_max; 386168404Spjd kstat_named_t arcstat_size; 387185029Spjd kstat_named_t arcstat_hdr_size; 388208373Smm kstat_named_t arcstat_data_size; 389208373Smm kstat_named_t arcstat_other_size; 390185029Spjd kstat_named_t arcstat_l2_hits; 391185029Spjd kstat_named_t arcstat_l2_misses; 392185029Spjd kstat_named_t arcstat_l2_feeds; 393185029Spjd kstat_named_t arcstat_l2_rw_clash; 394208373Smm kstat_named_t arcstat_l2_read_bytes; 395208373Smm kstat_named_t arcstat_l2_write_bytes; 396185029Spjd kstat_named_t arcstat_l2_writes_sent; 397185029Spjd kstat_named_t arcstat_l2_writes_done; 398185029Spjd kstat_named_t arcstat_l2_writes_error; 399185029Spjd kstat_named_t arcstat_l2_writes_hdr_miss; 400185029Spjd kstat_named_t arcstat_l2_evict_lock_retry; 401185029Spjd kstat_named_t arcstat_l2_evict_reading; 402185029Spjd kstat_named_t arcstat_l2_free_on_write; 403275492Sdelphij kstat_named_t arcstat_l2_cdata_free_on_write; 404185029Spjd kstat_named_t arcstat_l2_abort_lowmem; 405185029Spjd kstat_named_t arcstat_l2_cksum_bad; 406185029Spjd kstat_named_t arcstat_l2_io_error; 407185029Spjd kstat_named_t arcstat_l2_size; 408251478Sdelphij kstat_named_t arcstat_l2_asize; 409185029Spjd kstat_named_t arcstat_l2_hdr_size; 410251478Sdelphij kstat_named_t arcstat_l2_compress_successes; 411251478Sdelphij kstat_named_t arcstat_l2_compress_zeros; 412251478Sdelphij kstat_named_t arcstat_l2_compress_failures; 413205231Skmacy kstat_named_t arcstat_l2_write_trylock_fail; 414205231Skmacy kstat_named_t arcstat_l2_write_passed_headroom; 415205231Skmacy kstat_named_t arcstat_l2_write_spa_mismatch; 416206796Spjd kstat_named_t arcstat_l2_write_in_l2; 417205231Skmacy kstat_named_t arcstat_l2_write_hdr_io_in_progress; 418205231Skmacy kstat_named_t arcstat_l2_write_not_cacheable; 419205231Skmacy kstat_named_t arcstat_l2_write_full; 420205231Skmacy kstat_named_t arcstat_l2_write_buffer_iter; 421205231Skmacy kstat_named_t arcstat_l2_write_pios; 422205231Skmacy kstat_named_t arcstat_l2_write_buffer_bytes_scanned; 423205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_iter; 424205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_null_iter; 425242845Sdelphij kstat_named_t arcstat_memory_throttle_count; 426242845Sdelphij kstat_named_t arcstat_duplicate_buffers; 427242845Sdelphij kstat_named_t arcstat_duplicate_buffers_size; 428242845Sdelphij kstat_named_t arcstat_duplicate_reads; 429277583Sdelphij kstat_named_t arcstat_meta_used; 430277583Sdelphij kstat_named_t arcstat_meta_limit; 431277583Sdelphij kstat_named_t arcstat_meta_max; 432168404Spjd} arc_stats_t; 433168404Spjd 434168404Spjdstatic arc_stats_t arc_stats = { 435168404Spjd { "hits", KSTAT_DATA_UINT64 }, 436168404Spjd { "misses", KSTAT_DATA_UINT64 }, 437168404Spjd { "demand_data_hits", KSTAT_DATA_UINT64 }, 438168404Spjd { "demand_data_misses", KSTAT_DATA_UINT64 }, 439168404Spjd { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 440168404Spjd { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 441168404Spjd { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 442168404Spjd { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 443168404Spjd { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 444168404Spjd { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 445168404Spjd { "mru_hits", KSTAT_DATA_UINT64 }, 446168404Spjd { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 447168404Spjd { "mfu_hits", KSTAT_DATA_UINT64 }, 448168404Spjd { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 449205231Skmacy { "allocated", KSTAT_DATA_UINT64 }, 450168404Spjd { "deleted", KSTAT_DATA_UINT64 }, 451205231Skmacy { "stolen", KSTAT_DATA_UINT64 }, 452168404Spjd { "recycle_miss", KSTAT_DATA_UINT64 }, 453168404Spjd { "mutex_miss", KSTAT_DATA_UINT64 }, 454168404Spjd { "evict_skip", KSTAT_DATA_UINT64 }, 455208373Smm { "evict_l2_cached", KSTAT_DATA_UINT64 }, 456208373Smm { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 457208373Smm { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 458168404Spjd { "hash_elements", KSTAT_DATA_UINT64 }, 459168404Spjd { "hash_elements_max", KSTAT_DATA_UINT64 }, 460168404Spjd { "hash_collisions", KSTAT_DATA_UINT64 }, 461168404Spjd { "hash_chains", KSTAT_DATA_UINT64 }, 462168404Spjd { "hash_chain_max", KSTAT_DATA_UINT64 }, 463168404Spjd { "p", KSTAT_DATA_UINT64 }, 464168404Spjd { "c", KSTAT_DATA_UINT64 }, 465168404Spjd { "c_min", KSTAT_DATA_UINT64 }, 466168404Spjd { "c_max", KSTAT_DATA_UINT64 }, 467185029Spjd { "size", KSTAT_DATA_UINT64 }, 468185029Spjd { "hdr_size", KSTAT_DATA_UINT64 }, 469208373Smm { "data_size", KSTAT_DATA_UINT64 }, 470208373Smm { "other_size", KSTAT_DATA_UINT64 }, 471185029Spjd { "l2_hits", KSTAT_DATA_UINT64 }, 472185029Spjd { "l2_misses", KSTAT_DATA_UINT64 }, 473185029Spjd { "l2_feeds", KSTAT_DATA_UINT64 }, 474185029Spjd { "l2_rw_clash", KSTAT_DATA_UINT64 }, 475208373Smm { "l2_read_bytes", KSTAT_DATA_UINT64 }, 476208373Smm { "l2_write_bytes", KSTAT_DATA_UINT64 }, 477185029Spjd { "l2_writes_sent", KSTAT_DATA_UINT64 }, 478185029Spjd { "l2_writes_done", KSTAT_DATA_UINT64 }, 479185029Spjd { "l2_writes_error", KSTAT_DATA_UINT64 }, 480185029Spjd { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 481185029Spjd { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 482185029Spjd { "l2_evict_reading", KSTAT_DATA_UINT64 }, 483185029Spjd { "l2_free_on_write", KSTAT_DATA_UINT64 }, 484275492Sdelphij { "l2_cdata_free_on_write", KSTAT_DATA_UINT64 }, 485185029Spjd { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 486185029Spjd { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 487185029Spjd { "l2_io_error", KSTAT_DATA_UINT64 }, 488185029Spjd { "l2_size", KSTAT_DATA_UINT64 }, 489251478Sdelphij { "l2_asize", KSTAT_DATA_UINT64 }, 490185029Spjd { "l2_hdr_size", KSTAT_DATA_UINT64 }, 491251478Sdelphij { "l2_compress_successes", KSTAT_DATA_UINT64 }, 492251478Sdelphij { "l2_compress_zeros", KSTAT_DATA_UINT64 }, 493251478Sdelphij { "l2_compress_failures", KSTAT_DATA_UINT64 }, 494206796Spjd { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 495206796Spjd { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 496206796Spjd { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 497206796Spjd { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 498206796Spjd { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 499206796Spjd { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 500206796Spjd { "l2_write_full", KSTAT_DATA_UINT64 }, 501206796Spjd { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 502206796Spjd { "l2_write_pios", KSTAT_DATA_UINT64 }, 503206796Spjd { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 504206796Spjd { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 505242845Sdelphij { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 }, 506242845Sdelphij { "memory_throttle_count", KSTAT_DATA_UINT64 }, 507242845Sdelphij { "duplicate_buffers", KSTAT_DATA_UINT64 }, 508242845Sdelphij { "duplicate_buffers_size", KSTAT_DATA_UINT64 }, 509277583Sdelphij { "duplicate_reads", KSTAT_DATA_UINT64 }, 510277583Sdelphij { "arc_meta_used", KSTAT_DATA_UINT64 }, 511277583Sdelphij { "arc_meta_limit", KSTAT_DATA_UINT64 }, 512277583Sdelphij { "arc_meta_max", KSTAT_DATA_UINT64 } 513168404Spjd}; 514168404Spjd 515168404Spjd#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 516168404Spjd 517168404Spjd#define ARCSTAT_INCR(stat, val) \ 518251631Sdelphij atomic_add_64(&arc_stats.stat.value.ui64, (val)) 519168404Spjd 520206796Spjd#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 521168404Spjd#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 522168404Spjd 523168404Spjd#define ARCSTAT_MAX(stat, val) { \ 524168404Spjd uint64_t m; \ 525168404Spjd while ((val) > (m = arc_stats.stat.value.ui64) && \ 526168404Spjd (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 527168404Spjd continue; \ 528168404Spjd} 529168404Spjd 530168404Spjd#define ARCSTAT_MAXSTAT(stat) \ 531168404Spjd ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 532168404Spjd 533168404Spjd/* 534168404Spjd * We define a macro to allow ARC hits/misses to be easily broken down by 535168404Spjd * two separate conditions, giving a total of four different subtypes for 536168404Spjd * each of hits and misses (so eight statistics total). 537168404Spjd */ 538168404Spjd#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 539168404Spjd if (cond1) { \ 540168404Spjd if (cond2) { \ 541168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 542168404Spjd } else { \ 543168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 544168404Spjd } \ 545168404Spjd } else { \ 546168404Spjd if (cond2) { \ 547168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 548168404Spjd } else { \ 549168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 550168404Spjd } \ 551168404Spjd } 552168404Spjd 553168404Spjdkstat_t *arc_ksp; 554206796Spjdstatic arc_state_t *arc_anon; 555168404Spjdstatic arc_state_t *arc_mru; 556168404Spjdstatic arc_state_t *arc_mru_ghost; 557168404Spjdstatic arc_state_t *arc_mfu; 558168404Spjdstatic arc_state_t *arc_mfu_ghost; 559185029Spjdstatic arc_state_t *arc_l2c_only; 560168404Spjd 561168404Spjd/* 562168404Spjd * There are several ARC variables that are critical to export as kstats -- 563168404Spjd * but we don't want to have to grovel around in the kstat whenever we wish to 564168404Spjd * manipulate them. For these variables, we therefore define them to be in 565168404Spjd * terms of the statistic variable. This assures that we are not introducing 566168404Spjd * the possibility of inconsistency by having shadow copies of the variables, 567168404Spjd * while still allowing the code to be readable. 568168404Spjd */ 569168404Spjd#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 570168404Spjd#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 571168404Spjd#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 572168404Spjd#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 573168404Spjd#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 574277583Sdelphij#define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ 575277583Sdelphij#define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */ 576277583Sdelphij#define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ 577168404Spjd 578251478Sdelphij#define L2ARC_IS_VALID_COMPRESS(_c_) \ 579251478Sdelphij ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY) 580251478Sdelphij 581168404Spjdstatic int arc_no_grow; /* Don't try to grow cache size */ 582168404Spjdstatic uint64_t arc_tempreserve; 583209962Smmstatic uint64_t arc_loaned_bytes; 584168404Spjd 585185029Spjdtypedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 586185029Spjd 587168404Spjdtypedef struct arc_callback arc_callback_t; 588168404Spjd 589168404Spjdstruct arc_callback { 590168404Spjd void *acb_private; 591168404Spjd arc_done_func_t *acb_done; 592168404Spjd arc_buf_t *acb_buf; 593168404Spjd zio_t *acb_zio_dummy; 594168404Spjd arc_callback_t *acb_next; 595168404Spjd}; 596168404Spjd 597168404Spjdtypedef struct arc_write_callback arc_write_callback_t; 598168404Spjd 599168404Spjdstruct arc_write_callback { 600168404Spjd void *awcb_private; 601168404Spjd arc_done_func_t *awcb_ready; 602260763Savg arc_done_func_t *awcb_physdone; 603168404Spjd arc_done_func_t *awcb_done; 604168404Spjd arc_buf_t *awcb_buf; 605168404Spjd}; 606168404Spjd 607168404Spjdstruct arc_buf_hdr { 608168404Spjd /* protected by hash lock */ 609168404Spjd dva_t b_dva; 610168404Spjd uint64_t b_birth; 611168404Spjd uint64_t b_cksum0; 612168404Spjd 613168404Spjd kmutex_t b_freeze_lock; 614168404Spjd zio_cksum_t *b_freeze_cksum; 615219089Spjd void *b_thawed; 616168404Spjd 617168404Spjd arc_buf_hdr_t *b_hash_next; 618168404Spjd arc_buf_t *b_buf; 619277586Sdelphij arc_flags_t b_flags; 620168404Spjd uint32_t b_datacnt; 621168404Spjd 622168404Spjd arc_callback_t *b_acb; 623168404Spjd kcondvar_t b_cv; 624168404Spjd 625168404Spjd /* immutable */ 626168404Spjd arc_buf_contents_t b_type; 627168404Spjd uint64_t b_size; 628209962Smm uint64_t b_spa; 629168404Spjd 630168404Spjd /* protected by arc state mutex */ 631168404Spjd arc_state_t *b_state; 632168404Spjd list_node_t b_arc_node; 633168404Spjd 634168404Spjd /* updated atomically */ 635168404Spjd clock_t b_arc_access; 636168404Spjd 637168404Spjd /* self protecting */ 638168404Spjd refcount_t b_refcnt; 639185029Spjd 640185029Spjd l2arc_buf_hdr_t *b_l2hdr; 641185029Spjd list_node_t b_l2node; 642168404Spjd}; 643168404Spjd 644277583Sdelphij#ifdef _KERNEL 645277583Sdelphijstatic int 646277583Sdelphijsysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS) 647277583Sdelphij{ 648277583Sdelphij uint64_t val; 649277583Sdelphij int err; 650277583Sdelphij 651277583Sdelphij val = arc_meta_limit; 652277583Sdelphij err = sysctl_handle_64(oidp, &val, 0, req); 653277583Sdelphij if (err != 0 || req->newptr == NULL) 654277583Sdelphij return (err); 655277583Sdelphij 656277583Sdelphij if (val <= 0 || val > arc_c_max) 657277583Sdelphij return (EINVAL); 658277583Sdelphij 659277583Sdelphij arc_meta_limit = val; 660277583Sdelphij return (0); 661277583Sdelphij} 662277583Sdelphij#endif 663277583Sdelphij 664168404Spjdstatic arc_buf_t *arc_eviction_list; 665168404Spjdstatic kmutex_t arc_eviction_mtx; 666168404Spjdstatic arc_buf_hdr_t arc_eviction_hdr; 667168404Spjd 668168404Spjd#define GHOST_STATE(state) \ 669185029Spjd ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 670185029Spjd (state) == arc_l2c_only) 671168404Spjd 672277586Sdelphij#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) 673277586Sdelphij#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) 674277586Sdelphij#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) 675277586Sdelphij#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) 676277586Sdelphij#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FLAG_FREED_IN_READ) 677277586Sdelphij#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_FLAG_BUF_AVAILABLE) 678277586Sdelphij#define HDR_FREE_IN_PROGRESS(hdr) \ 679277586Sdelphij ((hdr)->b_flags & ARC_FLAG_FREE_IN_PROGRESS) 680277586Sdelphij#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) 681277586Sdelphij#define HDR_L2_READING(hdr) \ 682277586Sdelphij ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS && \ 683277586Sdelphij (hdr)->b_l2hdr != NULL) 684277586Sdelphij#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) 685277586Sdelphij#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) 686277586Sdelphij#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) 687168404Spjd 688168404Spjd/* 689185029Spjd * Other sizes 690185029Spjd */ 691185029Spjd 692185029Spjd#define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 693185029Spjd#define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 694185029Spjd 695185029Spjd/* 696168404Spjd * Hash table routines 697168404Spjd */ 698168404Spjd 699205253Skmacy#define HT_LOCK_PAD CACHE_LINE_SIZE 700168404Spjd 701168404Spjdstruct ht_lock { 702168404Spjd kmutex_t ht_lock; 703168404Spjd#ifdef _KERNEL 704168404Spjd unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 705168404Spjd#endif 706168404Spjd}; 707168404Spjd 708168404Spjd#define BUF_LOCKS 256 709168404Spjdtypedef struct buf_hash_table { 710168404Spjd uint64_t ht_mask; 711168404Spjd arc_buf_hdr_t **ht_table; 712205264Skmacy struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 713168404Spjd} buf_hash_table_t; 714168404Spjd 715168404Spjdstatic buf_hash_table_t buf_hash_table; 716168404Spjd 717168404Spjd#define BUF_HASH_INDEX(spa, dva, birth) \ 718168404Spjd (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 719168404Spjd#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 720168404Spjd#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 721219089Spjd#define HDR_LOCK(hdr) \ 722219089Spjd (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 723168404Spjd 724168404Spjduint64_t zfs_crc64_table[256]; 725168404Spjd 726185029Spjd/* 727185029Spjd * Level 2 ARC 728185029Spjd */ 729185029Spjd 730208373Smm#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 731251478Sdelphij#define L2ARC_HEADROOM 2 /* num of writes */ 732251478Sdelphij/* 733251478Sdelphij * If we discover during ARC scan any buffers to be compressed, we boost 734251478Sdelphij * our headroom for the next scanning cycle by this percentage multiple. 735251478Sdelphij */ 736251478Sdelphij#define L2ARC_HEADROOM_BOOST 200 737208373Smm#define L2ARC_FEED_SECS 1 /* caching interval secs */ 738208373Smm#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 739185029Spjd 740185029Spjd#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 741185029Spjd#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 742185029Spjd 743251631Sdelphij/* L2ARC Performance Tunables */ 744185029Spjduint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 745185029Spjduint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 746185029Spjduint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 747251478Sdelphijuint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 748185029Spjduint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 749208373Smmuint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 750219089Spjdboolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 751208373Smmboolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 752208373Smmboolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 753185029Spjd 754217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 755205231Skmacy &l2arc_write_max, 0, "max write size"); 756217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 757205231Skmacy &l2arc_write_boost, 0, "extra write during warmup"); 758217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 759205231Skmacy &l2arc_headroom, 0, "number of dev writes"); 760217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 761205231Skmacy &l2arc_feed_secs, 0, "interval seconds"); 762217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 763208373Smm &l2arc_feed_min_ms, 0, "min interval milliseconds"); 764205231Skmacy 765205231SkmacySYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 766205231Skmacy &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 767208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 768208373Smm &l2arc_feed_again, 0, "turbo warmup"); 769208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 770208373Smm &l2arc_norw, 0, "no reads during writes"); 771205231Skmacy 772217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 773205231Skmacy &ARC_anon.arcs_size, 0, "size of anonymous state"); 774217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD, 775205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state"); 776217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD, 777205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state"); 778205231Skmacy 779217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 780205231Skmacy &ARC_mru.arcs_size, 0, "size of mru state"); 781217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD, 782205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state"); 783217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD, 784205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state"); 785205231Skmacy 786217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 787205231Skmacy &ARC_mru_ghost.arcs_size, 0, "size of mru ghost state"); 788217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD, 789205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 790205231Skmacy "size of metadata in mru ghost state"); 791217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD, 792205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 793205231Skmacy "size of data in mru ghost state"); 794205231Skmacy 795217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 796205231Skmacy &ARC_mfu.arcs_size, 0, "size of mfu state"); 797217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD, 798205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state"); 799217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD, 800205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state"); 801205231Skmacy 802217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 803205231Skmacy &ARC_mfu_ghost.arcs_size, 0, "size of mfu ghost state"); 804217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD, 805205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 806205231Skmacy "size of metadata in mfu ghost state"); 807217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD, 808205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 809205231Skmacy "size of data in mfu ghost state"); 810205231Skmacy 811217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 812205231Skmacy &ARC_l2c_only.arcs_size, 0, "size of mru state"); 813205231Skmacy 814185029Spjd/* 815185029Spjd * L2ARC Internals 816185029Spjd */ 817185029Spjdtypedef struct l2arc_dev { 818185029Spjd vdev_t *l2ad_vdev; /* vdev */ 819185029Spjd spa_t *l2ad_spa; /* spa */ 820185029Spjd uint64_t l2ad_hand; /* next write location */ 821185029Spjd uint64_t l2ad_start; /* first addr on device */ 822185029Spjd uint64_t l2ad_end; /* last addr on device */ 823185029Spjd uint64_t l2ad_evict; /* last addr eviction reached */ 824185029Spjd boolean_t l2ad_first; /* first sweep through */ 825208373Smm boolean_t l2ad_writing; /* currently writing */ 826185029Spjd list_t *l2ad_buflist; /* buffer list */ 827185029Spjd list_node_t l2ad_node; /* device list node */ 828185029Spjd} l2arc_dev_t; 829185029Spjd 830185029Spjdstatic list_t L2ARC_dev_list; /* device list */ 831185029Spjdstatic list_t *l2arc_dev_list; /* device list pointer */ 832185029Spjdstatic kmutex_t l2arc_dev_mtx; /* device list mutex */ 833185029Spjdstatic l2arc_dev_t *l2arc_dev_last; /* last device used */ 834185029Spjdstatic kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 835185029Spjdstatic list_t L2ARC_free_on_write; /* free after write buf list */ 836185029Spjdstatic list_t *l2arc_free_on_write; /* free after write list ptr */ 837185029Spjdstatic kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 838185029Spjdstatic uint64_t l2arc_ndev; /* number of devices */ 839185029Spjd 840185029Spjdtypedef struct l2arc_read_callback { 841251478Sdelphij arc_buf_t *l2rcb_buf; /* read buffer */ 842251478Sdelphij spa_t *l2rcb_spa; /* spa */ 843251478Sdelphij blkptr_t l2rcb_bp; /* original blkptr */ 844268657Sdelphij zbookmark_phys_t l2rcb_zb; /* original bookmark */ 845251478Sdelphij int l2rcb_flags; /* original flags */ 846251478Sdelphij enum zio_compress l2rcb_compress; /* applied compress */ 847185029Spjd} l2arc_read_callback_t; 848185029Spjd 849185029Spjdtypedef struct l2arc_write_callback { 850185029Spjd l2arc_dev_t *l2wcb_dev; /* device info */ 851185029Spjd arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 852185029Spjd} l2arc_write_callback_t; 853185029Spjd 854185029Spjdstruct l2arc_buf_hdr { 855185029Spjd /* protected by arc_buf_hdr mutex */ 856251478Sdelphij l2arc_dev_t *b_dev; /* L2ARC device */ 857251478Sdelphij uint64_t b_daddr; /* disk address, offset byte */ 858251478Sdelphij /* compression applied to buffer data */ 859251478Sdelphij enum zio_compress b_compress; 860251478Sdelphij /* real alloc'd buffer size depending on b_compress applied */ 861251478Sdelphij int b_asize; 862251478Sdelphij /* temporary buffer holder for in-flight compressed data */ 863251478Sdelphij void *b_tmp_cdata; 864185029Spjd}; 865185029Spjd 866185029Spjdtypedef struct l2arc_data_free { 867185029Spjd /* protected by l2arc_free_on_write_mtx */ 868185029Spjd void *l2df_data; 869185029Spjd size_t l2df_size; 870185029Spjd void (*l2df_func)(void *, size_t); 871185029Spjd list_node_t l2df_list_node; 872185029Spjd} l2arc_data_free_t; 873185029Spjd 874185029Spjdstatic kmutex_t l2arc_feed_thr_lock; 875185029Spjdstatic kcondvar_t l2arc_feed_thr_cv; 876185029Spjdstatic uint8_t l2arc_thread_exit; 877185029Spjd 878277586Sdelphijstatic void arc_get_data_buf(arc_buf_t *); 879277586Sdelphijstatic void arc_access(arc_buf_hdr_t *, kmutex_t *); 880277586Sdelphijstatic int arc_evict_needed(arc_buf_contents_t); 881277586Sdelphijstatic void arc_evict_ghost(arc_state_t *, uint64_t, int64_t); 882277586Sdelphijstatic void arc_buf_watch(arc_buf_t *); 883277586Sdelphij 884277586Sdelphijstatic boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); 885277586Sdelphijstatic void l2arc_read_done(zio_t *); 886185029Spjdstatic void l2arc_hdr_stat_add(void); 887185029Spjdstatic void l2arc_hdr_stat_remove(void); 888185029Spjd 889277586Sdelphijstatic boolean_t l2arc_compress_buf(l2arc_buf_hdr_t *); 890277586Sdelphijstatic void l2arc_decompress_zio(zio_t *, arc_buf_hdr_t *, enum zio_compress); 891277586Sdelphijstatic void l2arc_release_cdata_buf(arc_buf_hdr_t *); 892251478Sdelphij 893168404Spjdstatic uint64_t 894209962Smmbuf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 895168404Spjd{ 896168404Spjd uint8_t *vdva = (uint8_t *)dva; 897168404Spjd uint64_t crc = -1ULL; 898168404Spjd int i; 899168404Spjd 900168404Spjd ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 901168404Spjd 902168404Spjd for (i = 0; i < sizeof (dva_t); i++) 903168404Spjd crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 904168404Spjd 905209962Smm crc ^= (spa>>8) ^ birth; 906168404Spjd 907168404Spjd return (crc); 908168404Spjd} 909168404Spjd 910168404Spjd#define BUF_EMPTY(buf) \ 911168404Spjd ((buf)->b_dva.dva_word[0] == 0 && \ 912168404Spjd (buf)->b_dva.dva_word[1] == 0 && \ 913263397Sdelphij (buf)->b_cksum0 == 0) 914168404Spjd 915168404Spjd#define BUF_EQUAL(spa, dva, birth, buf) \ 916168404Spjd ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 917168404Spjd ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 918168404Spjd ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 919168404Spjd 920219089Spjdstatic void 921219089Spjdbuf_discard_identity(arc_buf_hdr_t *hdr) 922219089Spjd{ 923219089Spjd hdr->b_dva.dva_word[0] = 0; 924219089Spjd hdr->b_dva.dva_word[1] = 0; 925219089Spjd hdr->b_birth = 0; 926219089Spjd hdr->b_cksum0 = 0; 927219089Spjd} 928219089Spjd 929168404Spjdstatic arc_buf_hdr_t * 930268649Sdelphijbuf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 931168404Spjd{ 932268649Sdelphij const dva_t *dva = BP_IDENTITY(bp); 933268649Sdelphij uint64_t birth = BP_PHYSICAL_BIRTH(bp); 934168404Spjd uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 935168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 936277586Sdelphij arc_buf_hdr_t *hdr; 937168404Spjd 938168404Spjd mutex_enter(hash_lock); 939277586Sdelphij for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; 940277586Sdelphij hdr = hdr->b_hash_next) { 941277586Sdelphij if (BUF_EQUAL(spa, dva, birth, hdr)) { 942168404Spjd *lockp = hash_lock; 943277586Sdelphij return (hdr); 944168404Spjd } 945168404Spjd } 946168404Spjd mutex_exit(hash_lock); 947168404Spjd *lockp = NULL; 948168404Spjd return (NULL); 949168404Spjd} 950168404Spjd 951168404Spjd/* 952168404Spjd * Insert an entry into the hash table. If there is already an element 953168404Spjd * equal to elem in the hash table, then the already existing element 954168404Spjd * will be returned and the new element will not be inserted. 955168404Spjd * Otherwise returns NULL. 956168404Spjd */ 957168404Spjdstatic arc_buf_hdr_t * 958277586Sdelphijbuf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) 959168404Spjd{ 960277586Sdelphij uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 961168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 962277586Sdelphij arc_buf_hdr_t *fhdr; 963168404Spjd uint32_t i; 964168404Spjd 965277586Sdelphij ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); 966277586Sdelphij ASSERT(hdr->b_birth != 0); 967277586Sdelphij ASSERT(!HDR_IN_HASH_TABLE(hdr)); 968168404Spjd *lockp = hash_lock; 969168404Spjd mutex_enter(hash_lock); 970277586Sdelphij for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; 971277586Sdelphij fhdr = fhdr->b_hash_next, i++) { 972277586Sdelphij if (BUF_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) 973277586Sdelphij return (fhdr); 974168404Spjd } 975168404Spjd 976277586Sdelphij hdr->b_hash_next = buf_hash_table.ht_table[idx]; 977277586Sdelphij buf_hash_table.ht_table[idx] = hdr; 978277586Sdelphij hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; 979168404Spjd 980168404Spjd /* collect some hash table performance data */ 981168404Spjd if (i > 0) { 982168404Spjd ARCSTAT_BUMP(arcstat_hash_collisions); 983168404Spjd if (i == 1) 984168404Spjd ARCSTAT_BUMP(arcstat_hash_chains); 985168404Spjd 986168404Spjd ARCSTAT_MAX(arcstat_hash_chain_max, i); 987168404Spjd } 988168404Spjd 989168404Spjd ARCSTAT_BUMP(arcstat_hash_elements); 990168404Spjd ARCSTAT_MAXSTAT(arcstat_hash_elements); 991168404Spjd 992168404Spjd return (NULL); 993168404Spjd} 994168404Spjd 995168404Spjdstatic void 996277586Sdelphijbuf_hash_remove(arc_buf_hdr_t *hdr) 997168404Spjd{ 998277586Sdelphij arc_buf_hdr_t *fhdr, **hdrp; 999277586Sdelphij uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1000168404Spjd 1001168404Spjd ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 1002277586Sdelphij ASSERT(HDR_IN_HASH_TABLE(hdr)); 1003168404Spjd 1004277586Sdelphij hdrp = &buf_hash_table.ht_table[idx]; 1005277586Sdelphij while ((fhdr = *hdrp) != hdr) { 1006277586Sdelphij ASSERT(fhdr != NULL); 1007277586Sdelphij hdrp = &fhdr->b_hash_next; 1008168404Spjd } 1009277586Sdelphij *hdrp = hdr->b_hash_next; 1010277586Sdelphij hdr->b_hash_next = NULL; 1011277586Sdelphij hdr->b_flags &= ~ARC_FLAG_IN_HASH_TABLE; 1012168404Spjd 1013168404Spjd /* collect some hash table performance data */ 1014168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_elements); 1015168404Spjd 1016168404Spjd if (buf_hash_table.ht_table[idx] && 1017168404Spjd buf_hash_table.ht_table[idx]->b_hash_next == NULL) 1018168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1019168404Spjd} 1020168404Spjd 1021168404Spjd/* 1022168404Spjd * Global data structures and functions for the buf kmem cache. 1023168404Spjd */ 1024168404Spjdstatic kmem_cache_t *hdr_cache; 1025168404Spjdstatic kmem_cache_t *buf_cache; 1026168404Spjd 1027168404Spjdstatic void 1028168404Spjdbuf_fini(void) 1029168404Spjd{ 1030168404Spjd int i; 1031168404Spjd 1032168404Spjd kmem_free(buf_hash_table.ht_table, 1033168404Spjd (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1034168404Spjd for (i = 0; i < BUF_LOCKS; i++) 1035168404Spjd mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 1036168404Spjd kmem_cache_destroy(hdr_cache); 1037168404Spjd kmem_cache_destroy(buf_cache); 1038168404Spjd} 1039168404Spjd 1040168404Spjd/* 1041168404Spjd * Constructor callback - called when the cache is empty 1042168404Spjd * and a new buf is requested. 1043168404Spjd */ 1044168404Spjd/* ARGSUSED */ 1045168404Spjdstatic int 1046168404Spjdhdr_cons(void *vbuf, void *unused, int kmflag) 1047168404Spjd{ 1048277586Sdelphij arc_buf_hdr_t *hdr = vbuf; 1049168404Spjd 1050277586Sdelphij bzero(hdr, sizeof (arc_buf_hdr_t)); 1051277586Sdelphij refcount_create(&hdr->b_refcnt); 1052277586Sdelphij cv_init(&hdr->b_cv, NULL, CV_DEFAULT, NULL); 1053277586Sdelphij mutex_init(&hdr->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1054208373Smm arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 1055185029Spjd 1056168404Spjd return (0); 1057168404Spjd} 1058168404Spjd 1059185029Spjd/* ARGSUSED */ 1060185029Spjdstatic int 1061185029Spjdbuf_cons(void *vbuf, void *unused, int kmflag) 1062185029Spjd{ 1063185029Spjd arc_buf_t *buf = vbuf; 1064185029Spjd 1065185029Spjd bzero(buf, sizeof (arc_buf_t)); 1066219089Spjd mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 1067208373Smm arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1068208373Smm 1069185029Spjd return (0); 1070185029Spjd} 1071185029Spjd 1072168404Spjd/* 1073168404Spjd * Destructor callback - called when a cached buf is 1074168404Spjd * no longer required. 1075168404Spjd */ 1076168404Spjd/* ARGSUSED */ 1077168404Spjdstatic void 1078168404Spjdhdr_dest(void *vbuf, void *unused) 1079168404Spjd{ 1080277586Sdelphij arc_buf_hdr_t *hdr = vbuf; 1081168404Spjd 1082277586Sdelphij ASSERT(BUF_EMPTY(hdr)); 1083277586Sdelphij refcount_destroy(&hdr->b_refcnt); 1084277586Sdelphij cv_destroy(&hdr->b_cv); 1085277586Sdelphij mutex_destroy(&hdr->b_freeze_lock); 1086208373Smm arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 1087168404Spjd} 1088168404Spjd 1089185029Spjd/* ARGSUSED */ 1090185029Spjdstatic void 1091185029Spjdbuf_dest(void *vbuf, void *unused) 1092185029Spjd{ 1093185029Spjd arc_buf_t *buf = vbuf; 1094185029Spjd 1095219089Spjd mutex_destroy(&buf->b_evict_lock); 1096208373Smm arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1097185029Spjd} 1098185029Spjd 1099168404Spjd/* 1100168404Spjd * Reclaim callback -- invoked when memory is low. 1101168404Spjd */ 1102168404Spjd/* ARGSUSED */ 1103168404Spjdstatic void 1104168404Spjdhdr_recl(void *unused) 1105168404Spjd{ 1106168404Spjd dprintf("hdr_recl called\n"); 1107168404Spjd /* 1108168404Spjd * umem calls the reclaim func when we destroy the buf cache, 1109168404Spjd * which is after we do arc_fini(). 1110168404Spjd */ 1111168404Spjd if (!arc_dead) 1112168404Spjd cv_signal(&arc_reclaim_thr_cv); 1113168404Spjd} 1114168404Spjd 1115168404Spjdstatic void 1116168404Spjdbuf_init(void) 1117168404Spjd{ 1118168404Spjd uint64_t *ct; 1119168404Spjd uint64_t hsize = 1ULL << 12; 1120168404Spjd int i, j; 1121168404Spjd 1122168404Spjd /* 1123168404Spjd * The hash table is big enough to fill all of physical memory 1124269846Sdelphij * with an average block size of zfs_arc_average_blocksize (default 8K). 1125269846Sdelphij * By default, the table will take up 1126269846Sdelphij * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1127168404Spjd */ 1128269846Sdelphij while (hsize * zfs_arc_average_blocksize < (uint64_t)physmem * PAGESIZE) 1129168404Spjd hsize <<= 1; 1130168404Spjdretry: 1131168404Spjd buf_hash_table.ht_mask = hsize - 1; 1132168404Spjd buf_hash_table.ht_table = 1133168404Spjd kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1134168404Spjd if (buf_hash_table.ht_table == NULL) { 1135168404Spjd ASSERT(hsize > (1ULL << 8)); 1136168404Spjd hsize >>= 1; 1137168404Spjd goto retry; 1138168404Spjd } 1139168404Spjd 1140168404Spjd hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 1141168404Spjd 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 1142168404Spjd buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1143185029Spjd 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1144168404Spjd 1145168404Spjd for (i = 0; i < 256; i++) 1146168404Spjd for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1147168404Spjd *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1148168404Spjd 1149168404Spjd for (i = 0; i < BUF_LOCKS; i++) { 1150168404Spjd mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1151168404Spjd NULL, MUTEX_DEFAULT, NULL); 1152168404Spjd } 1153168404Spjd} 1154168404Spjd 1155168404Spjd#define ARC_MINTIME (hz>>4) /* 62 ms */ 1156168404Spjd 1157168404Spjdstatic void 1158168404Spjdarc_cksum_verify(arc_buf_t *buf) 1159168404Spjd{ 1160168404Spjd zio_cksum_t zc; 1161168404Spjd 1162168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1163168404Spjd return; 1164168404Spjd 1165168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1166168404Spjd if (buf->b_hdr->b_freeze_cksum == NULL || 1167277586Sdelphij (buf->b_hdr->b_flags & ARC_FLAG_IO_ERROR)) { 1168168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1169168404Spjd return; 1170168404Spjd } 1171168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1172168404Spjd if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 1173168404Spjd panic("buffer modified while frozen!"); 1174168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1175168404Spjd} 1176168404Spjd 1177185029Spjdstatic int 1178185029Spjdarc_cksum_equal(arc_buf_t *buf) 1179185029Spjd{ 1180185029Spjd zio_cksum_t zc; 1181185029Spjd int equal; 1182185029Spjd 1183185029Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1184185029Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1185185029Spjd equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 1186185029Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1187185029Spjd 1188185029Spjd return (equal); 1189185029Spjd} 1190185029Spjd 1191168404Spjdstatic void 1192185029Spjdarc_cksum_compute(arc_buf_t *buf, boolean_t force) 1193168404Spjd{ 1194185029Spjd if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 1195168404Spjd return; 1196168404Spjd 1197168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1198168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1199168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1200168404Spjd return; 1201168404Spjd } 1202168404Spjd buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 1203168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 1204168404Spjd buf->b_hdr->b_freeze_cksum); 1205168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1206240133Smm#ifdef illumos 1207240133Smm arc_buf_watch(buf); 1208240133Smm#endif /* illumos */ 1209168404Spjd} 1210168404Spjd 1211240133Smm#ifdef illumos 1212240133Smm#ifndef _KERNEL 1213240133Smmtypedef struct procctl { 1214240133Smm long cmd; 1215240133Smm prwatch_t prwatch; 1216240133Smm} procctl_t; 1217240133Smm#endif 1218240133Smm 1219240133Smm/* ARGSUSED */ 1220240133Smmstatic void 1221240133Smmarc_buf_unwatch(arc_buf_t *buf) 1222240133Smm{ 1223240133Smm#ifndef _KERNEL 1224240133Smm if (arc_watch) { 1225240133Smm int result; 1226240133Smm procctl_t ctl; 1227240133Smm ctl.cmd = PCWATCH; 1228240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1229240133Smm ctl.prwatch.pr_size = 0; 1230240133Smm ctl.prwatch.pr_wflags = 0; 1231240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1232240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1233240133Smm } 1234240133Smm#endif 1235240133Smm} 1236240133Smm 1237240133Smm/* ARGSUSED */ 1238240133Smmstatic void 1239240133Smmarc_buf_watch(arc_buf_t *buf) 1240240133Smm{ 1241240133Smm#ifndef _KERNEL 1242240133Smm if (arc_watch) { 1243240133Smm int result; 1244240133Smm procctl_t ctl; 1245240133Smm ctl.cmd = PCWATCH; 1246240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1247240133Smm ctl.prwatch.pr_size = buf->b_hdr->b_size; 1248240133Smm ctl.prwatch.pr_wflags = WA_WRITE; 1249240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1250240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1251240133Smm } 1252240133Smm#endif 1253240133Smm} 1254240133Smm#endif /* illumos */ 1255240133Smm 1256168404Spjdvoid 1257168404Spjdarc_buf_thaw(arc_buf_t *buf) 1258168404Spjd{ 1259185029Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1260185029Spjd if (buf->b_hdr->b_state != arc_anon) 1261185029Spjd panic("modifying non-anon buffer!"); 1262277586Sdelphij if (buf->b_hdr->b_flags & ARC_FLAG_IO_IN_PROGRESS) 1263185029Spjd panic("modifying buffer while i/o in progress!"); 1264185029Spjd arc_cksum_verify(buf); 1265185029Spjd } 1266168404Spjd 1267168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1268168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1269168404Spjd kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1270168404Spjd buf->b_hdr->b_freeze_cksum = NULL; 1271168404Spjd } 1272219089Spjd 1273219089Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1274219089Spjd if (buf->b_hdr->b_thawed) 1275219089Spjd kmem_free(buf->b_hdr->b_thawed, 1); 1276219089Spjd buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP); 1277219089Spjd } 1278219089Spjd 1279168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1280240133Smm 1281240133Smm#ifdef illumos 1282240133Smm arc_buf_unwatch(buf); 1283240133Smm#endif /* illumos */ 1284168404Spjd} 1285168404Spjd 1286168404Spjdvoid 1287168404Spjdarc_buf_freeze(arc_buf_t *buf) 1288168404Spjd{ 1289219089Spjd kmutex_t *hash_lock; 1290219089Spjd 1291168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1292168404Spjd return; 1293168404Spjd 1294219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1295219089Spjd mutex_enter(hash_lock); 1296219089Spjd 1297168404Spjd ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 1298168404Spjd buf->b_hdr->b_state == arc_anon); 1299185029Spjd arc_cksum_compute(buf, B_FALSE); 1300219089Spjd mutex_exit(hash_lock); 1301240133Smm 1302168404Spjd} 1303168404Spjd 1304168404Spjdstatic void 1305277586Sdelphijget_buf_info(arc_buf_hdr_t *hdr, arc_state_t *state, list_t **list, kmutex_t **lock) 1306205231Skmacy{ 1307277586Sdelphij uint64_t buf_hashid = buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1308205231Skmacy 1309277586Sdelphij if (hdr->b_type == ARC_BUFC_METADATA) 1310206796Spjd buf_hashid &= (ARC_BUFC_NUMMETADATALISTS - 1); 1311205231Skmacy else { 1312206796Spjd buf_hashid &= (ARC_BUFC_NUMDATALISTS - 1); 1313205231Skmacy buf_hashid += ARC_BUFC_NUMMETADATALISTS; 1314205231Skmacy } 1315205231Skmacy 1316205231Skmacy *list = &state->arcs_lists[buf_hashid]; 1317205231Skmacy *lock = ARCS_LOCK(state, buf_hashid); 1318205231Skmacy} 1319205231Skmacy 1320205231Skmacy 1321205231Skmacystatic void 1322277586Sdelphijadd_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1323168404Spjd{ 1324168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1325168404Spjd 1326277586Sdelphij if ((refcount_add(&hdr->b_refcnt, tag) == 1) && 1327277586Sdelphij (hdr->b_state != arc_anon)) { 1328277586Sdelphij uint64_t delta = hdr->b_size * hdr->b_datacnt; 1329277586Sdelphij uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 1330205231Skmacy list_t *list; 1331205231Skmacy kmutex_t *lock; 1332168404Spjd 1333277586Sdelphij get_buf_info(hdr, hdr->b_state, &list, &lock); 1334205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1335205231Skmacy mutex_enter(lock); 1336277586Sdelphij ASSERT(list_link_active(&hdr->b_arc_node)); 1337277586Sdelphij list_remove(list, hdr); 1338277586Sdelphij if (GHOST_STATE(hdr->b_state)) { 1339277586Sdelphij ASSERT0(hdr->b_datacnt); 1340277586Sdelphij ASSERT3P(hdr->b_buf, ==, NULL); 1341277586Sdelphij delta = hdr->b_size; 1342168404Spjd } 1343168404Spjd ASSERT(delta > 0); 1344185029Spjd ASSERT3U(*size, >=, delta); 1345185029Spjd atomic_add_64(size, -delta); 1346206794Spjd mutex_exit(lock); 1347185029Spjd /* remove the prefetch flag if we get a reference */ 1348277586Sdelphij if (hdr->b_flags & ARC_FLAG_PREFETCH) 1349277586Sdelphij hdr->b_flags &= ~ARC_FLAG_PREFETCH; 1350168404Spjd } 1351168404Spjd} 1352168404Spjd 1353168404Spjdstatic int 1354277586Sdelphijremove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1355168404Spjd{ 1356168404Spjd int cnt; 1357277586Sdelphij arc_state_t *state = hdr->b_state; 1358168404Spjd 1359168404Spjd ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1360168404Spjd ASSERT(!GHOST_STATE(state)); 1361168404Spjd 1362277586Sdelphij if (((cnt = refcount_remove(&hdr->b_refcnt, tag)) == 0) && 1363168404Spjd (state != arc_anon)) { 1364277586Sdelphij uint64_t *size = &state->arcs_lsize[hdr->b_type]; 1365205231Skmacy list_t *list; 1366205231Skmacy kmutex_t *lock; 1367185029Spjd 1368277586Sdelphij get_buf_info(hdr, state, &list, &lock); 1369205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1370205231Skmacy mutex_enter(lock); 1371277586Sdelphij ASSERT(!list_link_active(&hdr->b_arc_node)); 1372277586Sdelphij list_insert_head(list, hdr); 1373277586Sdelphij ASSERT(hdr->b_datacnt > 0); 1374277586Sdelphij atomic_add_64(size, hdr->b_size * hdr->b_datacnt); 1375206794Spjd mutex_exit(lock); 1376168404Spjd } 1377168404Spjd return (cnt); 1378168404Spjd} 1379168404Spjd 1380168404Spjd/* 1381168404Spjd * Move the supplied buffer to the indicated state. The mutex 1382168404Spjd * for the buffer must be held by the caller. 1383168404Spjd */ 1384168404Spjdstatic void 1385277586Sdelphijarc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, 1386277586Sdelphij kmutex_t *hash_lock) 1387168404Spjd{ 1388277586Sdelphij arc_state_t *old_state = hdr->b_state; 1389277586Sdelphij int64_t refcnt = refcount_count(&hdr->b_refcnt); 1390168404Spjd uint64_t from_delta, to_delta; 1391205231Skmacy list_t *list; 1392205231Skmacy kmutex_t *lock; 1393168404Spjd 1394168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1395260763Savg ASSERT3P(new_state, !=, old_state); 1396277586Sdelphij ASSERT(refcnt == 0 || hdr->b_datacnt > 0); 1397277586Sdelphij ASSERT(hdr->b_datacnt == 0 || !GHOST_STATE(new_state)); 1398277586Sdelphij ASSERT(hdr->b_datacnt <= 1 || old_state != arc_anon); 1399168404Spjd 1400277586Sdelphij from_delta = to_delta = hdr->b_datacnt * hdr->b_size; 1401168404Spjd 1402168404Spjd /* 1403168404Spjd * If this buffer is evictable, transfer it from the 1404168404Spjd * old state list to the new state list. 1405168404Spjd */ 1406168404Spjd if (refcnt == 0) { 1407168404Spjd if (old_state != arc_anon) { 1408205231Skmacy int use_mutex; 1409277586Sdelphij uint64_t *size = &old_state->arcs_lsize[hdr->b_type]; 1410168404Spjd 1411277586Sdelphij get_buf_info(hdr, old_state, &list, &lock); 1412205231Skmacy use_mutex = !MUTEX_HELD(lock); 1413168404Spjd if (use_mutex) 1414205231Skmacy mutex_enter(lock); 1415168404Spjd 1416277586Sdelphij ASSERT(list_link_active(&hdr->b_arc_node)); 1417277586Sdelphij list_remove(list, hdr); 1418168404Spjd 1419168404Spjd /* 1420168404Spjd * If prefetching out of the ghost cache, 1421219089Spjd * we will have a non-zero datacnt. 1422168404Spjd */ 1423277586Sdelphij if (GHOST_STATE(old_state) && hdr->b_datacnt == 0) { 1424168404Spjd /* ghost elements have a ghost size */ 1425277586Sdelphij ASSERT(hdr->b_buf == NULL); 1426277586Sdelphij from_delta = hdr->b_size; 1427168404Spjd } 1428185029Spjd ASSERT3U(*size, >=, from_delta); 1429185029Spjd atomic_add_64(size, -from_delta); 1430168404Spjd 1431168404Spjd if (use_mutex) 1432205231Skmacy mutex_exit(lock); 1433168404Spjd } 1434168404Spjd if (new_state != arc_anon) { 1435206796Spjd int use_mutex; 1436277586Sdelphij uint64_t *size = &new_state->arcs_lsize[hdr->b_type]; 1437168404Spjd 1438277586Sdelphij get_buf_info(hdr, new_state, &list, &lock); 1439205231Skmacy use_mutex = !MUTEX_HELD(lock); 1440168404Spjd if (use_mutex) 1441205231Skmacy mutex_enter(lock); 1442168404Spjd 1443277586Sdelphij list_insert_head(list, hdr); 1444168404Spjd 1445168404Spjd /* ghost elements have a ghost size */ 1446168404Spjd if (GHOST_STATE(new_state)) { 1447277586Sdelphij ASSERT(hdr->b_datacnt == 0); 1448277586Sdelphij ASSERT(hdr->b_buf == NULL); 1449277586Sdelphij to_delta = hdr->b_size; 1450168404Spjd } 1451185029Spjd atomic_add_64(size, to_delta); 1452168404Spjd 1453168404Spjd if (use_mutex) 1454205231Skmacy mutex_exit(lock); 1455168404Spjd } 1456168404Spjd } 1457168404Spjd 1458277586Sdelphij ASSERT(!BUF_EMPTY(hdr)); 1459277586Sdelphij if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) 1460277586Sdelphij buf_hash_remove(hdr); 1461168404Spjd 1462168404Spjd /* adjust state sizes */ 1463168404Spjd if (to_delta) 1464168404Spjd atomic_add_64(&new_state->arcs_size, to_delta); 1465168404Spjd if (from_delta) { 1466168404Spjd ASSERT3U(old_state->arcs_size, >=, from_delta); 1467168404Spjd atomic_add_64(&old_state->arcs_size, -from_delta); 1468168404Spjd } 1469277586Sdelphij hdr->b_state = new_state; 1470185029Spjd 1471185029Spjd /* adjust l2arc hdr stats */ 1472185029Spjd if (new_state == arc_l2c_only) 1473185029Spjd l2arc_hdr_stat_add(); 1474185029Spjd else if (old_state == arc_l2c_only) 1475185029Spjd l2arc_hdr_stat_remove(); 1476168404Spjd} 1477168404Spjd 1478185029Spjdvoid 1479208373Smmarc_space_consume(uint64_t space, arc_space_type_t type) 1480185029Spjd{ 1481208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1482208373Smm 1483208373Smm switch (type) { 1484208373Smm case ARC_SPACE_DATA: 1485208373Smm ARCSTAT_INCR(arcstat_data_size, space); 1486208373Smm break; 1487208373Smm case ARC_SPACE_OTHER: 1488208373Smm ARCSTAT_INCR(arcstat_other_size, space); 1489208373Smm break; 1490208373Smm case ARC_SPACE_HDRS: 1491208373Smm ARCSTAT_INCR(arcstat_hdr_size, space); 1492208373Smm break; 1493208373Smm case ARC_SPACE_L2HDRS: 1494208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, space); 1495208373Smm break; 1496208373Smm } 1497208373Smm 1498277583Sdelphij ARCSTAT_INCR(arcstat_meta_used, space); 1499185029Spjd atomic_add_64(&arc_size, space); 1500185029Spjd} 1501185029Spjd 1502185029Spjdvoid 1503208373Smmarc_space_return(uint64_t space, arc_space_type_t type) 1504185029Spjd{ 1505208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1506208373Smm 1507208373Smm switch (type) { 1508208373Smm case ARC_SPACE_DATA: 1509208373Smm ARCSTAT_INCR(arcstat_data_size, -space); 1510208373Smm break; 1511208373Smm case ARC_SPACE_OTHER: 1512208373Smm ARCSTAT_INCR(arcstat_other_size, -space); 1513208373Smm break; 1514208373Smm case ARC_SPACE_HDRS: 1515208373Smm ARCSTAT_INCR(arcstat_hdr_size, -space); 1516208373Smm break; 1517208373Smm case ARC_SPACE_L2HDRS: 1518208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 1519208373Smm break; 1520208373Smm } 1521208373Smm 1522185029Spjd ASSERT(arc_meta_used >= space); 1523185029Spjd if (arc_meta_max < arc_meta_used) 1524185029Spjd arc_meta_max = arc_meta_used; 1525277583Sdelphij ARCSTAT_INCR(arcstat_meta_used, -space); 1526185029Spjd ASSERT(arc_size >= space); 1527185029Spjd atomic_add_64(&arc_size, -space); 1528185029Spjd} 1529185029Spjd 1530168404Spjdarc_buf_t * 1531168404Spjdarc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1532168404Spjd{ 1533168404Spjd arc_buf_hdr_t *hdr; 1534168404Spjd arc_buf_t *buf; 1535168404Spjd 1536168404Spjd ASSERT3U(size, >, 0); 1537185029Spjd hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1538168404Spjd ASSERT(BUF_EMPTY(hdr)); 1539168404Spjd hdr->b_size = size; 1540168404Spjd hdr->b_type = type; 1541228103Smm hdr->b_spa = spa_load_guid(spa); 1542168404Spjd hdr->b_state = arc_anon; 1543168404Spjd hdr->b_arc_access = 0; 1544185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1545168404Spjd buf->b_hdr = hdr; 1546168404Spjd buf->b_data = NULL; 1547168404Spjd buf->b_efunc = NULL; 1548168404Spjd buf->b_private = NULL; 1549168404Spjd buf->b_next = NULL; 1550168404Spjd hdr->b_buf = buf; 1551168404Spjd arc_get_data_buf(buf); 1552168404Spjd hdr->b_datacnt = 1; 1553168404Spjd hdr->b_flags = 0; 1554168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1555168404Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1556168404Spjd 1557168404Spjd return (buf); 1558168404Spjd} 1559168404Spjd 1560209962Smmstatic char *arc_onloan_tag = "onloan"; 1561209962Smm 1562209962Smm/* 1563209962Smm * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 1564209962Smm * flight data by arc_tempreserve_space() until they are "returned". Loaned 1565209962Smm * buffers must be returned to the arc before they can be used by the DMU or 1566209962Smm * freed. 1567209962Smm */ 1568209962Smmarc_buf_t * 1569209962Smmarc_loan_buf(spa_t *spa, int size) 1570209962Smm{ 1571209962Smm arc_buf_t *buf; 1572209962Smm 1573209962Smm buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 1574209962Smm 1575209962Smm atomic_add_64(&arc_loaned_bytes, size); 1576209962Smm return (buf); 1577209962Smm} 1578209962Smm 1579209962Smm/* 1580209962Smm * Return a loaned arc buffer to the arc. 1581209962Smm */ 1582209962Smmvoid 1583209962Smmarc_return_buf(arc_buf_t *buf, void *tag) 1584209962Smm{ 1585209962Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1586209962Smm 1587209962Smm ASSERT(buf->b_data != NULL); 1588219089Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1589219089Spjd (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag); 1590209962Smm 1591209962Smm atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 1592209962Smm} 1593209962Smm 1594219089Spjd/* Detach an arc_buf from a dbuf (tag) */ 1595219089Spjdvoid 1596219089Spjdarc_loan_inuse_buf(arc_buf_t *buf, void *tag) 1597219089Spjd{ 1598219089Spjd arc_buf_hdr_t *hdr; 1599219089Spjd 1600219089Spjd ASSERT(buf->b_data != NULL); 1601219089Spjd hdr = buf->b_hdr; 1602219089Spjd (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag); 1603219089Spjd (void) refcount_remove(&hdr->b_refcnt, tag); 1604219089Spjd buf->b_efunc = NULL; 1605219089Spjd buf->b_private = NULL; 1606219089Spjd 1607219089Spjd atomic_add_64(&arc_loaned_bytes, hdr->b_size); 1608219089Spjd} 1609219089Spjd 1610168404Spjdstatic arc_buf_t * 1611168404Spjdarc_buf_clone(arc_buf_t *from) 1612168404Spjd{ 1613168404Spjd arc_buf_t *buf; 1614168404Spjd arc_buf_hdr_t *hdr = from->b_hdr; 1615168404Spjd uint64_t size = hdr->b_size; 1616168404Spjd 1617219089Spjd ASSERT(hdr->b_state != arc_anon); 1618219089Spjd 1619185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1620168404Spjd buf->b_hdr = hdr; 1621168404Spjd buf->b_data = NULL; 1622168404Spjd buf->b_efunc = NULL; 1623168404Spjd buf->b_private = NULL; 1624168404Spjd buf->b_next = hdr->b_buf; 1625168404Spjd hdr->b_buf = buf; 1626168404Spjd arc_get_data_buf(buf); 1627168404Spjd bcopy(from->b_data, buf->b_data, size); 1628242845Sdelphij 1629242845Sdelphij /* 1630242845Sdelphij * This buffer already exists in the arc so create a duplicate 1631242845Sdelphij * copy for the caller. If the buffer is associated with user data 1632242845Sdelphij * then track the size and number of duplicates. These stats will be 1633242845Sdelphij * updated as duplicate buffers are created and destroyed. 1634242845Sdelphij */ 1635242845Sdelphij if (hdr->b_type == ARC_BUFC_DATA) { 1636242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_buffers); 1637242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, size); 1638242845Sdelphij } 1639168404Spjd hdr->b_datacnt += 1; 1640168404Spjd return (buf); 1641168404Spjd} 1642168404Spjd 1643168404Spjdvoid 1644168404Spjdarc_buf_add_ref(arc_buf_t *buf, void* tag) 1645168404Spjd{ 1646168404Spjd arc_buf_hdr_t *hdr; 1647168404Spjd kmutex_t *hash_lock; 1648168404Spjd 1649168404Spjd /* 1650185029Spjd * Check to see if this buffer is evicted. Callers 1651185029Spjd * must verify b_data != NULL to know if the add_ref 1652185029Spjd * was successful. 1653168404Spjd */ 1654219089Spjd mutex_enter(&buf->b_evict_lock); 1655185029Spjd if (buf->b_data == NULL) { 1656219089Spjd mutex_exit(&buf->b_evict_lock); 1657168404Spjd return; 1658168404Spjd } 1659219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1660219089Spjd mutex_enter(hash_lock); 1661185029Spjd hdr = buf->b_hdr; 1662219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1663219089Spjd mutex_exit(&buf->b_evict_lock); 1664168404Spjd 1665168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 1666168404Spjd add_reference(hdr, hash_lock, tag); 1667208373Smm DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 1668168404Spjd arc_access(hdr, hash_lock); 1669168404Spjd mutex_exit(hash_lock); 1670168404Spjd ARCSTAT_BUMP(arcstat_hits); 1671277586Sdelphij ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_FLAG_PREFETCH), 1672168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 1673168404Spjd data, metadata, hits); 1674168404Spjd} 1675168404Spjd 1676275492Sdelphijstatic void 1677275492Sdelphijarc_buf_free_on_write(void *data, size_t size, 1678275492Sdelphij void (*free_func)(void *, size_t)) 1679275492Sdelphij{ 1680275492Sdelphij l2arc_data_free_t *df; 1681275492Sdelphij 1682275492Sdelphij df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 1683275492Sdelphij df->l2df_data = data; 1684275492Sdelphij df->l2df_size = size; 1685275492Sdelphij df->l2df_func = free_func; 1686275492Sdelphij mutex_enter(&l2arc_free_on_write_mtx); 1687275492Sdelphij list_insert_head(l2arc_free_on_write, df); 1688275492Sdelphij mutex_exit(&l2arc_free_on_write_mtx); 1689275492Sdelphij} 1690275492Sdelphij 1691185029Spjd/* 1692185029Spjd * Free the arc data buffer. If it is an l2arc write in progress, 1693185029Spjd * the buffer is placed on l2arc_free_on_write to be freed later. 1694185029Spjd */ 1695168404Spjdstatic void 1696240133Smmarc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t)) 1697185029Spjd{ 1698240133Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1699240133Smm 1700185029Spjd if (HDR_L2_WRITING(hdr)) { 1701275492Sdelphij arc_buf_free_on_write(buf->b_data, hdr->b_size, free_func); 1702185029Spjd ARCSTAT_BUMP(arcstat_l2_free_on_write); 1703185029Spjd } else { 1704240133Smm free_func(buf->b_data, hdr->b_size); 1705185029Spjd } 1706185029Spjd} 1707185029Spjd 1708269417Sdelphij/* 1709269417Sdelphij * Free up buf->b_data and if 'remove' is set, then pull the 1710269417Sdelphij * arc_buf_t off of the the arc_buf_hdr_t's list and free it. 1711269417Sdelphij */ 1712185029Spjdstatic void 1713275492Sdelphijarc_buf_l2_cdata_free(arc_buf_hdr_t *hdr) 1714275492Sdelphij{ 1715275492Sdelphij l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 1716275492Sdelphij 1717275492Sdelphij ASSERT(MUTEX_HELD(&l2arc_buflist_mtx)); 1718275492Sdelphij 1719275492Sdelphij if (l2hdr->b_tmp_cdata == NULL) 1720275492Sdelphij return; 1721275492Sdelphij 1722275492Sdelphij ASSERT(HDR_L2_WRITING(hdr)); 1723275492Sdelphij arc_buf_free_on_write(l2hdr->b_tmp_cdata, hdr->b_size, 1724275492Sdelphij zio_data_buf_free); 1725275492Sdelphij ARCSTAT_BUMP(arcstat_l2_cdata_free_on_write); 1726275492Sdelphij l2hdr->b_tmp_cdata = NULL; 1727275492Sdelphij} 1728275492Sdelphij 1729275492Sdelphijstatic void 1730269417Sdelphijarc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t remove) 1731168404Spjd{ 1732168404Spjd arc_buf_t **bufp; 1733168404Spjd 1734168404Spjd /* free up data associated with the buf */ 1735168404Spjd if (buf->b_data) { 1736168404Spjd arc_state_t *state = buf->b_hdr->b_state; 1737168404Spjd uint64_t size = buf->b_hdr->b_size; 1738168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 1739168404Spjd 1740168404Spjd arc_cksum_verify(buf); 1741240133Smm#ifdef illumos 1742240133Smm arc_buf_unwatch(buf); 1743240133Smm#endif /* illumos */ 1744219089Spjd 1745168404Spjd if (!recycle) { 1746168404Spjd if (type == ARC_BUFC_METADATA) { 1747240133Smm arc_buf_data_free(buf, zio_buf_free); 1748208373Smm arc_space_return(size, ARC_SPACE_DATA); 1749168404Spjd } else { 1750168404Spjd ASSERT(type == ARC_BUFC_DATA); 1751240133Smm arc_buf_data_free(buf, zio_data_buf_free); 1752208373Smm ARCSTAT_INCR(arcstat_data_size, -size); 1753185029Spjd atomic_add_64(&arc_size, -size); 1754168404Spjd } 1755168404Spjd } 1756168404Spjd if (list_link_active(&buf->b_hdr->b_arc_node)) { 1757185029Spjd uint64_t *cnt = &state->arcs_lsize[type]; 1758185029Spjd 1759168404Spjd ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 1760168404Spjd ASSERT(state != arc_anon); 1761185029Spjd 1762185029Spjd ASSERT3U(*cnt, >=, size); 1763185029Spjd atomic_add_64(cnt, -size); 1764168404Spjd } 1765168404Spjd ASSERT3U(state->arcs_size, >=, size); 1766168404Spjd atomic_add_64(&state->arcs_size, -size); 1767168404Spjd buf->b_data = NULL; 1768242845Sdelphij 1769242845Sdelphij /* 1770242845Sdelphij * If we're destroying a duplicate buffer make sure 1771242845Sdelphij * that the appropriate statistics are updated. 1772242845Sdelphij */ 1773242845Sdelphij if (buf->b_hdr->b_datacnt > 1 && 1774242845Sdelphij buf->b_hdr->b_type == ARC_BUFC_DATA) { 1775242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 1776242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size); 1777242845Sdelphij } 1778168404Spjd ASSERT(buf->b_hdr->b_datacnt > 0); 1779168404Spjd buf->b_hdr->b_datacnt -= 1; 1780168404Spjd } 1781168404Spjd 1782168404Spjd /* only remove the buf if requested */ 1783269417Sdelphij if (!remove) 1784168404Spjd return; 1785168404Spjd 1786168404Spjd /* remove the buf from the hdr list */ 1787168404Spjd for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 1788168404Spjd continue; 1789168404Spjd *bufp = buf->b_next; 1790219089Spjd buf->b_next = NULL; 1791168404Spjd 1792168404Spjd ASSERT(buf->b_efunc == NULL); 1793168404Spjd 1794168404Spjd /* clean up the buf */ 1795168404Spjd buf->b_hdr = NULL; 1796168404Spjd kmem_cache_free(buf_cache, buf); 1797168404Spjd} 1798168404Spjd 1799168404Spjdstatic void 1800168404Spjdarc_hdr_destroy(arc_buf_hdr_t *hdr) 1801168404Spjd{ 1802168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1803168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 1804168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1805219089Spjd l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 1806168404Spjd 1807219089Spjd if (l2hdr != NULL) { 1808219089Spjd boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx); 1809219089Spjd /* 1810219089Spjd * To prevent arc_free() and l2arc_evict() from 1811219089Spjd * attempting to free the same buffer at the same time, 1812219089Spjd * a FREE_IN_PROGRESS flag is given to arc_free() to 1813219089Spjd * give it priority. l2arc_evict() can't destroy this 1814219089Spjd * header while we are waiting on l2arc_buflist_mtx. 1815219089Spjd * 1816219089Spjd * The hdr may be removed from l2ad_buflist before we 1817219089Spjd * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 1818219089Spjd */ 1819219089Spjd if (!buflist_held) { 1820185029Spjd mutex_enter(&l2arc_buflist_mtx); 1821219089Spjd l2hdr = hdr->b_l2hdr; 1822219089Spjd } 1823219089Spjd 1824219089Spjd if (l2hdr != NULL) { 1825248572Ssmh trim_map_free(l2hdr->b_dev->l2ad_vdev, l2hdr->b_daddr, 1826248574Ssmh hdr->b_size, 0); 1827219089Spjd list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 1828275492Sdelphij arc_buf_l2_cdata_free(hdr); 1829219089Spjd ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 1830251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); 1831268654Sdelphij vdev_space_update(l2hdr->b_dev->l2ad_vdev, 1832268654Sdelphij -l2hdr->b_asize, 0, 0); 1833219089Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 1834219089Spjd if (hdr->b_state == arc_l2c_only) 1835219089Spjd l2arc_hdr_stat_remove(); 1836219089Spjd hdr->b_l2hdr = NULL; 1837219089Spjd } 1838219089Spjd 1839219089Spjd if (!buflist_held) 1840185029Spjd mutex_exit(&l2arc_buflist_mtx); 1841185029Spjd } 1842185029Spjd 1843168404Spjd if (!BUF_EMPTY(hdr)) { 1844168404Spjd ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1845219089Spjd buf_discard_identity(hdr); 1846168404Spjd } 1847168404Spjd while (hdr->b_buf) { 1848168404Spjd arc_buf_t *buf = hdr->b_buf; 1849168404Spjd 1850168404Spjd if (buf->b_efunc) { 1851168404Spjd mutex_enter(&arc_eviction_mtx); 1852219089Spjd mutex_enter(&buf->b_evict_lock); 1853168404Spjd ASSERT(buf->b_hdr != NULL); 1854168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 1855168404Spjd hdr->b_buf = buf->b_next; 1856168404Spjd buf->b_hdr = &arc_eviction_hdr; 1857168404Spjd buf->b_next = arc_eviction_list; 1858168404Spjd arc_eviction_list = buf; 1859219089Spjd mutex_exit(&buf->b_evict_lock); 1860168404Spjd mutex_exit(&arc_eviction_mtx); 1861168404Spjd } else { 1862168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 1863168404Spjd } 1864168404Spjd } 1865168404Spjd if (hdr->b_freeze_cksum != NULL) { 1866168404Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1867168404Spjd hdr->b_freeze_cksum = NULL; 1868168404Spjd } 1869219089Spjd if (hdr->b_thawed) { 1870219089Spjd kmem_free(hdr->b_thawed, 1); 1871219089Spjd hdr->b_thawed = NULL; 1872219089Spjd } 1873168404Spjd 1874168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 1875168404Spjd ASSERT3P(hdr->b_hash_next, ==, NULL); 1876168404Spjd ASSERT3P(hdr->b_acb, ==, NULL); 1877168404Spjd kmem_cache_free(hdr_cache, hdr); 1878168404Spjd} 1879168404Spjd 1880168404Spjdvoid 1881168404Spjdarc_buf_free(arc_buf_t *buf, void *tag) 1882168404Spjd{ 1883168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1884168404Spjd int hashed = hdr->b_state != arc_anon; 1885168404Spjd 1886168404Spjd ASSERT(buf->b_efunc == NULL); 1887168404Spjd ASSERT(buf->b_data != NULL); 1888168404Spjd 1889168404Spjd if (hashed) { 1890168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1891168404Spjd 1892168404Spjd mutex_enter(hash_lock); 1893219089Spjd hdr = buf->b_hdr; 1894219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1895219089Spjd 1896168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1897219089Spjd if (hdr->b_datacnt > 1) { 1898168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1899219089Spjd } else { 1900219089Spjd ASSERT(buf == hdr->b_buf); 1901219089Spjd ASSERT(buf->b_efunc == NULL); 1902277586Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 1903219089Spjd } 1904168404Spjd mutex_exit(hash_lock); 1905168404Spjd } else if (HDR_IO_IN_PROGRESS(hdr)) { 1906168404Spjd int destroy_hdr; 1907168404Spjd /* 1908168404Spjd * We are in the middle of an async write. Don't destroy 1909168404Spjd * this buffer unless the write completes before we finish 1910168404Spjd * decrementing the reference count. 1911168404Spjd */ 1912168404Spjd mutex_enter(&arc_eviction_mtx); 1913168404Spjd (void) remove_reference(hdr, NULL, tag); 1914168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1915168404Spjd destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 1916168404Spjd mutex_exit(&arc_eviction_mtx); 1917168404Spjd if (destroy_hdr) 1918168404Spjd arc_hdr_destroy(hdr); 1919168404Spjd } else { 1920219089Spjd if (remove_reference(hdr, NULL, tag) > 0) 1921168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1922219089Spjd else 1923168404Spjd arc_hdr_destroy(hdr); 1924168404Spjd } 1925168404Spjd} 1926168404Spjd 1927248571Smmboolean_t 1928168404Spjdarc_buf_remove_ref(arc_buf_t *buf, void* tag) 1929168404Spjd{ 1930168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1931168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1932248571Smm boolean_t no_callback = (buf->b_efunc == NULL); 1933168404Spjd 1934168404Spjd if (hdr->b_state == arc_anon) { 1935219089Spjd ASSERT(hdr->b_datacnt == 1); 1936168404Spjd arc_buf_free(buf, tag); 1937168404Spjd return (no_callback); 1938168404Spjd } 1939168404Spjd 1940168404Spjd mutex_enter(hash_lock); 1941219089Spjd hdr = buf->b_hdr; 1942219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1943168404Spjd ASSERT(hdr->b_state != arc_anon); 1944168404Spjd ASSERT(buf->b_data != NULL); 1945168404Spjd 1946168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1947168404Spjd if (hdr->b_datacnt > 1) { 1948168404Spjd if (no_callback) 1949168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1950168404Spjd } else if (no_callback) { 1951168404Spjd ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 1952219089Spjd ASSERT(buf->b_efunc == NULL); 1953277586Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 1954168404Spjd } 1955168404Spjd ASSERT(no_callback || hdr->b_datacnt > 1 || 1956168404Spjd refcount_is_zero(&hdr->b_refcnt)); 1957168404Spjd mutex_exit(hash_lock); 1958168404Spjd return (no_callback); 1959168404Spjd} 1960168404Spjd 1961168404Spjdint 1962168404Spjdarc_buf_size(arc_buf_t *buf) 1963168404Spjd{ 1964168404Spjd return (buf->b_hdr->b_size); 1965168404Spjd} 1966168404Spjd 1967168404Spjd/* 1968242845Sdelphij * Called from the DMU to determine if the current buffer should be 1969242845Sdelphij * evicted. In order to ensure proper locking, the eviction must be initiated 1970242845Sdelphij * from the DMU. Return true if the buffer is associated with user data and 1971242845Sdelphij * duplicate buffers still exist. 1972242845Sdelphij */ 1973242845Sdelphijboolean_t 1974242845Sdelphijarc_buf_eviction_needed(arc_buf_t *buf) 1975242845Sdelphij{ 1976242845Sdelphij arc_buf_hdr_t *hdr; 1977242845Sdelphij boolean_t evict_needed = B_FALSE; 1978242845Sdelphij 1979242845Sdelphij if (zfs_disable_dup_eviction) 1980242845Sdelphij return (B_FALSE); 1981242845Sdelphij 1982242845Sdelphij mutex_enter(&buf->b_evict_lock); 1983242845Sdelphij hdr = buf->b_hdr; 1984242845Sdelphij if (hdr == NULL) { 1985242845Sdelphij /* 1986242845Sdelphij * We are in arc_do_user_evicts(); let that function 1987242845Sdelphij * perform the eviction. 1988242845Sdelphij */ 1989242845Sdelphij ASSERT(buf->b_data == NULL); 1990242845Sdelphij mutex_exit(&buf->b_evict_lock); 1991242845Sdelphij return (B_FALSE); 1992242845Sdelphij } else if (buf->b_data == NULL) { 1993242845Sdelphij /* 1994242845Sdelphij * We have already been added to the arc eviction list; 1995242845Sdelphij * recommend eviction. 1996242845Sdelphij */ 1997242845Sdelphij ASSERT3P(hdr, ==, &arc_eviction_hdr); 1998242845Sdelphij mutex_exit(&buf->b_evict_lock); 1999242845Sdelphij return (B_TRUE); 2000242845Sdelphij } 2001242845Sdelphij 2002242845Sdelphij if (hdr->b_datacnt > 1 && hdr->b_type == ARC_BUFC_DATA) 2003242845Sdelphij evict_needed = B_TRUE; 2004242845Sdelphij 2005242845Sdelphij mutex_exit(&buf->b_evict_lock); 2006242845Sdelphij return (evict_needed); 2007242845Sdelphij} 2008242845Sdelphij 2009242845Sdelphij/* 2010168404Spjd * Evict buffers from list until we've removed the specified number of 2011168404Spjd * bytes. Move the removed buffers to the appropriate evict state. 2012168404Spjd * If the recycle flag is set, then attempt to "recycle" a buffer: 2013168404Spjd * - look for a buffer to evict that is `bytes' long. 2014168404Spjd * - return the data block from this buffer rather than freeing it. 2015168404Spjd * This flag is used by callers that are trying to make space for a 2016168404Spjd * new buffer in a full arc cache. 2017185029Spjd * 2018185029Spjd * This function makes a "best effort". It skips over any buffers 2019185029Spjd * it can't get a hash_lock on, and so may not catch all candidates. 2020185029Spjd * It may also return without evicting as much space as requested. 2021168404Spjd */ 2022168404Spjdstatic void * 2023209962Smmarc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 2024168404Spjd arc_buf_contents_t type) 2025168404Spjd{ 2026168404Spjd arc_state_t *evicted_state; 2027168404Spjd uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 2028205231Skmacy int64_t bytes_remaining; 2029277586Sdelphij arc_buf_hdr_t *hdr, *hdr_prev = NULL; 2030205231Skmacy list_t *evicted_list, *list, *evicted_list_start, *list_start; 2031205231Skmacy kmutex_t *lock, *evicted_lock; 2032168404Spjd kmutex_t *hash_lock; 2033168404Spjd boolean_t have_lock; 2034168404Spjd void *stolen = NULL; 2035260763Savg arc_buf_hdr_t marker = { 0 }; 2036260763Savg int count = 0; 2037205231Skmacy static int evict_metadata_offset, evict_data_offset; 2038260763Savg int i, idx, offset, list_count, lists; 2039168404Spjd 2040168404Spjd ASSERT(state == arc_mru || state == arc_mfu); 2041168404Spjd 2042168404Spjd evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 2043206796Spjd 2044205231Skmacy if (type == ARC_BUFC_METADATA) { 2045205231Skmacy offset = 0; 2046205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 2047205231Skmacy list_start = &state->arcs_lists[0]; 2048205231Skmacy evicted_list_start = &evicted_state->arcs_lists[0]; 2049205231Skmacy idx = evict_metadata_offset; 2050205231Skmacy } else { 2051205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 2052205231Skmacy list_start = &state->arcs_lists[offset]; 2053205231Skmacy evicted_list_start = &evicted_state->arcs_lists[offset]; 2054205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 2055205231Skmacy idx = evict_data_offset; 2056205231Skmacy } 2057205231Skmacy bytes_remaining = evicted_state->arcs_lsize[type]; 2058260763Savg lists = 0; 2059206796Spjd 2060205231Skmacyevict_start: 2061205231Skmacy list = &list_start[idx]; 2062205231Skmacy evicted_list = &evicted_list_start[idx]; 2063205231Skmacy lock = ARCS_LOCK(state, (offset + idx)); 2064206796Spjd evicted_lock = ARCS_LOCK(evicted_state, (offset + idx)); 2065168404Spjd 2066205231Skmacy mutex_enter(lock); 2067205231Skmacy mutex_enter(evicted_lock); 2068205231Skmacy 2069277586Sdelphij for (hdr = list_tail(list); hdr; hdr = hdr_prev) { 2070277586Sdelphij hdr_prev = list_prev(list, hdr); 2071277586Sdelphij bytes_remaining -= (hdr->b_size * hdr->b_datacnt); 2072168404Spjd /* prefetch buffers have a minimum lifespan */ 2073277586Sdelphij if (HDR_IO_IN_PROGRESS(hdr) || 2074277586Sdelphij (spa && hdr->b_spa != spa) || 2075277586Sdelphij (hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT) && 2076277586Sdelphij ddi_get_lbolt() - hdr->b_arc_access < 2077219089Spjd arc_min_prefetch_lifespan)) { 2078168404Spjd skipped++; 2079168404Spjd continue; 2080168404Spjd } 2081168404Spjd /* "lookahead" for better eviction candidate */ 2082277586Sdelphij if (recycle && hdr->b_size != bytes && 2083277586Sdelphij hdr_prev && hdr_prev->b_size == bytes) 2084168404Spjd continue; 2085260763Savg 2086260763Savg /* ignore markers */ 2087277586Sdelphij if (hdr->b_spa == 0) 2088260763Savg continue; 2089260763Savg 2090260763Savg /* 2091260763Savg * It may take a long time to evict all the bufs requested. 2092260763Savg * To avoid blocking all arc activity, periodically drop 2093260763Savg * the arcs_mtx and give other threads a chance to run 2094260763Savg * before reacquiring the lock. 2095260763Savg * 2096260763Savg * If we are looking for a buffer to recycle, we are in 2097260763Savg * the hot code path, so don't sleep. 2098260763Savg */ 2099260763Savg if (!recycle && count++ > arc_evict_iterations) { 2100277586Sdelphij list_insert_after(list, hdr, &marker); 2101260763Savg mutex_exit(evicted_lock); 2102260763Savg mutex_exit(lock); 2103260763Savg kpreempt(KPREEMPT_SYNC); 2104260763Savg mutex_enter(lock); 2105260763Savg mutex_enter(evicted_lock); 2106277586Sdelphij hdr_prev = list_prev(list, &marker); 2107260763Savg list_remove(list, &marker); 2108260763Savg count = 0; 2109260763Savg continue; 2110260763Savg } 2111260763Savg 2112277586Sdelphij hash_lock = HDR_LOCK(hdr); 2113168404Spjd have_lock = MUTEX_HELD(hash_lock); 2114168404Spjd if (have_lock || mutex_tryenter(hash_lock)) { 2115277586Sdelphij ASSERT0(refcount_count(&hdr->b_refcnt)); 2116277586Sdelphij ASSERT(hdr->b_datacnt > 0); 2117277586Sdelphij while (hdr->b_buf) { 2118277586Sdelphij arc_buf_t *buf = hdr->b_buf; 2119219089Spjd if (!mutex_tryenter(&buf->b_evict_lock)) { 2120185029Spjd missed += 1; 2121185029Spjd break; 2122185029Spjd } 2123168404Spjd if (buf->b_data) { 2124277586Sdelphij bytes_evicted += hdr->b_size; 2125277586Sdelphij if (recycle && hdr->b_type == type && 2126277586Sdelphij hdr->b_size == bytes && 2127277586Sdelphij !HDR_L2_WRITING(hdr)) { 2128168404Spjd stolen = buf->b_data; 2129168404Spjd recycle = FALSE; 2130168404Spjd } 2131168404Spjd } 2132168404Spjd if (buf->b_efunc) { 2133168404Spjd mutex_enter(&arc_eviction_mtx); 2134168404Spjd arc_buf_destroy(buf, 2135168404Spjd buf->b_data == stolen, FALSE); 2136277586Sdelphij hdr->b_buf = buf->b_next; 2137168404Spjd buf->b_hdr = &arc_eviction_hdr; 2138168404Spjd buf->b_next = arc_eviction_list; 2139168404Spjd arc_eviction_list = buf; 2140168404Spjd mutex_exit(&arc_eviction_mtx); 2141219089Spjd mutex_exit(&buf->b_evict_lock); 2142168404Spjd } else { 2143219089Spjd mutex_exit(&buf->b_evict_lock); 2144168404Spjd arc_buf_destroy(buf, 2145168404Spjd buf->b_data == stolen, TRUE); 2146168404Spjd } 2147168404Spjd } 2148208373Smm 2149277586Sdelphij if (hdr->b_l2hdr) { 2150208373Smm ARCSTAT_INCR(arcstat_evict_l2_cached, 2151277586Sdelphij hdr->b_size); 2152208373Smm } else { 2153277586Sdelphij if (l2arc_write_eligible(hdr->b_spa, hdr)) { 2154208373Smm ARCSTAT_INCR(arcstat_evict_l2_eligible, 2155277586Sdelphij hdr->b_size); 2156208373Smm } else { 2157208373Smm ARCSTAT_INCR( 2158208373Smm arcstat_evict_l2_ineligible, 2159277586Sdelphij hdr->b_size); 2160208373Smm } 2161208373Smm } 2162208373Smm 2163277586Sdelphij if (hdr->b_datacnt == 0) { 2164277586Sdelphij arc_change_state(evicted_state, hdr, hash_lock); 2165277586Sdelphij ASSERT(HDR_IN_HASH_TABLE(hdr)); 2166277586Sdelphij hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; 2167277586Sdelphij hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 2168277586Sdelphij DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); 2169185029Spjd } 2170168404Spjd if (!have_lock) 2171168404Spjd mutex_exit(hash_lock); 2172168404Spjd if (bytes >= 0 && bytes_evicted >= bytes) 2173168404Spjd break; 2174205231Skmacy if (bytes_remaining > 0) { 2175205231Skmacy mutex_exit(evicted_lock); 2176205231Skmacy mutex_exit(lock); 2177206796Spjd idx = ((idx + 1) & (list_count - 1)); 2178260763Savg lists++; 2179205231Skmacy goto evict_start; 2180205231Skmacy } 2181168404Spjd } else { 2182168404Spjd missed += 1; 2183168404Spjd } 2184168404Spjd } 2185168404Spjd 2186205231Skmacy mutex_exit(evicted_lock); 2187205231Skmacy mutex_exit(lock); 2188206796Spjd 2189206796Spjd idx = ((idx + 1) & (list_count - 1)); 2190260763Savg lists++; 2191168404Spjd 2192205231Skmacy if (bytes_evicted < bytes) { 2193260763Savg if (lists < list_count) 2194205231Skmacy goto evict_start; 2195205231Skmacy else 2196205231Skmacy dprintf("only evicted %lld bytes from %x", 2197205231Skmacy (longlong_t)bytes_evicted, state); 2198205231Skmacy } 2199206796Spjd if (type == ARC_BUFC_METADATA) 2200205231Skmacy evict_metadata_offset = idx; 2201205231Skmacy else 2202205231Skmacy evict_data_offset = idx; 2203206796Spjd 2204168404Spjd if (skipped) 2205168404Spjd ARCSTAT_INCR(arcstat_evict_skip, skipped); 2206168404Spjd 2207168404Spjd if (missed) 2208168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, missed); 2209168404Spjd 2210185029Spjd /* 2211260763Savg * Note: we have just evicted some data into the ghost state, 2212260763Savg * potentially putting the ghost size over the desired size. Rather 2213260763Savg * that evicting from the ghost list in this hot code path, leave 2214260763Savg * this chore to the arc_reclaim_thread(). 2215185029Spjd */ 2216185029Spjd 2217205231Skmacy if (stolen) 2218205231Skmacy ARCSTAT_BUMP(arcstat_stolen); 2219168404Spjd return (stolen); 2220168404Spjd} 2221168404Spjd 2222168404Spjd/* 2223168404Spjd * Remove buffers from list until we've removed the specified number of 2224168404Spjd * bytes. Destroy the buffers that are removed. 2225168404Spjd */ 2226168404Spjdstatic void 2227209962Smmarc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 2228168404Spjd{ 2229277586Sdelphij arc_buf_hdr_t *hdr, *hdr_prev; 2230219089Spjd arc_buf_hdr_t marker = { 0 }; 2231205231Skmacy list_t *list, *list_start; 2232205231Skmacy kmutex_t *hash_lock, *lock; 2233168404Spjd uint64_t bytes_deleted = 0; 2234168404Spjd uint64_t bufs_skipped = 0; 2235260763Savg int count = 0; 2236205231Skmacy static int evict_offset; 2237205231Skmacy int list_count, idx = evict_offset; 2238260763Savg int offset, lists = 0; 2239168404Spjd 2240168404Spjd ASSERT(GHOST_STATE(state)); 2241205231Skmacy 2242205231Skmacy /* 2243205231Skmacy * data lists come after metadata lists 2244205231Skmacy */ 2245205231Skmacy list_start = &state->arcs_lists[ARC_BUFC_NUMMETADATALISTS]; 2246205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 2247205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 2248206796Spjd 2249205231Skmacyevict_start: 2250205231Skmacy list = &list_start[idx]; 2251205231Skmacy lock = ARCS_LOCK(state, idx + offset); 2252205231Skmacy 2253205231Skmacy mutex_enter(lock); 2254277586Sdelphij for (hdr = list_tail(list); hdr; hdr = hdr_prev) { 2255277586Sdelphij hdr_prev = list_prev(list, hdr); 2256277586Sdelphij if (hdr->b_type > ARC_BUFC_NUMTYPES) 2257277586Sdelphij panic("invalid hdr=%p", (void *)hdr); 2258277586Sdelphij if (spa && hdr->b_spa != spa) 2259185029Spjd continue; 2260219089Spjd 2261219089Spjd /* ignore markers */ 2262277586Sdelphij if (hdr->b_spa == 0) 2263219089Spjd continue; 2264219089Spjd 2265277586Sdelphij hash_lock = HDR_LOCK(hdr); 2266219089Spjd /* caller may be trying to modify this buffer, skip it */ 2267219089Spjd if (MUTEX_HELD(hash_lock)) 2268219089Spjd continue; 2269260763Savg 2270260763Savg /* 2271260763Savg * It may take a long time to evict all the bufs requested. 2272260763Savg * To avoid blocking all arc activity, periodically drop 2273260763Savg * the arcs_mtx and give other threads a chance to run 2274260763Savg * before reacquiring the lock. 2275260763Savg */ 2276260763Savg if (count++ > arc_evict_iterations) { 2277277586Sdelphij list_insert_after(list, hdr, &marker); 2278260763Savg mutex_exit(lock); 2279260763Savg kpreempt(KPREEMPT_SYNC); 2280260763Savg mutex_enter(lock); 2281277586Sdelphij hdr_prev = list_prev(list, &marker); 2282260763Savg list_remove(list, &marker); 2283260763Savg count = 0; 2284260763Savg continue; 2285260763Savg } 2286168404Spjd if (mutex_tryenter(hash_lock)) { 2287277586Sdelphij ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2288277586Sdelphij ASSERT(hdr->b_buf == NULL); 2289168404Spjd ARCSTAT_BUMP(arcstat_deleted); 2290277586Sdelphij bytes_deleted += hdr->b_size; 2291185029Spjd 2292277586Sdelphij if (hdr->b_l2hdr != NULL) { 2293185029Spjd /* 2294185029Spjd * This buffer is cached on the 2nd Level ARC; 2295185029Spjd * don't destroy the header. 2296185029Spjd */ 2297277586Sdelphij arc_change_state(arc_l2c_only, hdr, hash_lock); 2298185029Spjd mutex_exit(hash_lock); 2299185029Spjd } else { 2300277586Sdelphij arc_change_state(arc_anon, hdr, hash_lock); 2301185029Spjd mutex_exit(hash_lock); 2302277586Sdelphij arc_hdr_destroy(hdr); 2303185029Spjd } 2304185029Spjd 2305277586Sdelphij DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); 2306168404Spjd if (bytes >= 0 && bytes_deleted >= bytes) 2307168404Spjd break; 2308219089Spjd } else if (bytes < 0) { 2309219089Spjd /* 2310219089Spjd * Insert a list marker and then wait for the 2311219089Spjd * hash lock to become available. Once its 2312219089Spjd * available, restart from where we left off. 2313219089Spjd */ 2314277586Sdelphij list_insert_after(list, hdr, &marker); 2315219089Spjd mutex_exit(lock); 2316219089Spjd mutex_enter(hash_lock); 2317219089Spjd mutex_exit(hash_lock); 2318219089Spjd mutex_enter(lock); 2319277586Sdelphij hdr_prev = list_prev(list, &marker); 2320219089Spjd list_remove(list, &marker); 2321260763Savg } else { 2322168404Spjd bufs_skipped += 1; 2323260763Savg } 2324260763Savg 2325168404Spjd } 2326205231Skmacy mutex_exit(lock); 2327206796Spjd idx = ((idx + 1) & (ARC_BUFC_NUMDATALISTS - 1)); 2328260763Savg lists++; 2329206796Spjd 2330260763Savg if (lists < list_count) 2331205231Skmacy goto evict_start; 2332206796Spjd 2333205231Skmacy evict_offset = idx; 2334205231Skmacy if ((uintptr_t)list > (uintptr_t)&state->arcs_lists[ARC_BUFC_NUMMETADATALISTS] && 2335185029Spjd (bytes < 0 || bytes_deleted < bytes)) { 2336205231Skmacy list_start = &state->arcs_lists[0]; 2337205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 2338260763Savg offset = lists = 0; 2339205231Skmacy goto evict_start; 2340185029Spjd } 2341185029Spjd 2342168404Spjd if (bufs_skipped) { 2343168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 2344168404Spjd ASSERT(bytes >= 0); 2345168404Spjd } 2346168404Spjd 2347168404Spjd if (bytes_deleted < bytes) 2348168404Spjd dprintf("only deleted %lld bytes from %p", 2349168404Spjd (longlong_t)bytes_deleted, state); 2350168404Spjd} 2351168404Spjd 2352168404Spjdstatic void 2353168404Spjdarc_adjust(void) 2354168404Spjd{ 2355208373Smm int64_t adjustment, delta; 2356168404Spjd 2357208373Smm /* 2358208373Smm * Adjust MRU size 2359208373Smm */ 2360168404Spjd 2361209275Smm adjustment = MIN((int64_t)(arc_size - arc_c), 2362209275Smm (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - 2363209275Smm arc_p)); 2364208373Smm 2365208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 2366208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 2367209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA); 2368208373Smm adjustment -= delta; 2369168404Spjd } 2370168404Spjd 2371208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2372208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 2373209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, 2374185029Spjd ARC_BUFC_METADATA); 2375185029Spjd } 2376185029Spjd 2377208373Smm /* 2378208373Smm * Adjust MFU size 2379208373Smm */ 2380168404Spjd 2381208373Smm adjustment = arc_size - arc_c; 2382208373Smm 2383208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 2384208373Smm delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 2385209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA); 2386208373Smm adjustment -= delta; 2387168404Spjd } 2388168404Spjd 2389208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2390208373Smm int64_t delta = MIN(adjustment, 2391208373Smm arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 2392209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, 2393208373Smm ARC_BUFC_METADATA); 2394208373Smm } 2395168404Spjd 2396208373Smm /* 2397208373Smm * Adjust ghost lists 2398208373Smm */ 2399168404Spjd 2400208373Smm adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 2401168404Spjd 2402208373Smm if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 2403208373Smm delta = MIN(arc_mru_ghost->arcs_size, adjustment); 2404209962Smm arc_evict_ghost(arc_mru_ghost, 0, delta); 2405208373Smm } 2406185029Spjd 2407208373Smm adjustment = 2408208373Smm arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 2409208373Smm 2410208373Smm if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 2411208373Smm delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 2412209962Smm arc_evict_ghost(arc_mfu_ghost, 0, delta); 2413168404Spjd } 2414168404Spjd} 2415168404Spjd 2416168404Spjdstatic void 2417168404Spjdarc_do_user_evicts(void) 2418168404Spjd{ 2419191903Skmacy static arc_buf_t *tmp_arc_eviction_list; 2420191903Skmacy 2421191903Skmacy /* 2422191903Skmacy * Move list over to avoid LOR 2423191903Skmacy */ 2424206796Spjdrestart: 2425168404Spjd mutex_enter(&arc_eviction_mtx); 2426191903Skmacy tmp_arc_eviction_list = arc_eviction_list; 2427191903Skmacy arc_eviction_list = NULL; 2428191903Skmacy mutex_exit(&arc_eviction_mtx); 2429191903Skmacy 2430191903Skmacy while (tmp_arc_eviction_list != NULL) { 2431191903Skmacy arc_buf_t *buf = tmp_arc_eviction_list; 2432191903Skmacy tmp_arc_eviction_list = buf->b_next; 2433219089Spjd mutex_enter(&buf->b_evict_lock); 2434168404Spjd buf->b_hdr = NULL; 2435219089Spjd mutex_exit(&buf->b_evict_lock); 2436168404Spjd 2437168404Spjd if (buf->b_efunc != NULL) 2438269417Sdelphij VERIFY0(buf->b_efunc(buf->b_private)); 2439168404Spjd 2440168404Spjd buf->b_efunc = NULL; 2441168404Spjd buf->b_private = NULL; 2442168404Spjd kmem_cache_free(buf_cache, buf); 2443168404Spjd } 2444191903Skmacy 2445191903Skmacy if (arc_eviction_list != NULL) 2446191903Skmacy goto restart; 2447168404Spjd} 2448168404Spjd 2449168404Spjd/* 2450185029Spjd * Flush all *evictable* data from the cache for the given spa. 2451168404Spjd * NOTE: this will not touch "active" (i.e. referenced) data. 2452168404Spjd */ 2453168404Spjdvoid 2454185029Spjdarc_flush(spa_t *spa) 2455168404Spjd{ 2456209962Smm uint64_t guid = 0; 2457209962Smm 2458209962Smm if (spa) 2459228103Smm guid = spa_load_guid(spa); 2460209962Smm 2461205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_DATA]) { 2462209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 2463185029Spjd if (spa) 2464185029Spjd break; 2465185029Spjd } 2466205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_METADATA]) { 2467209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 2468185029Spjd if (spa) 2469185029Spjd break; 2470185029Spjd } 2471205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_DATA]) { 2472209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 2473185029Spjd if (spa) 2474185029Spjd break; 2475185029Spjd } 2476205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_METADATA]) { 2477209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 2478185029Spjd if (spa) 2479185029Spjd break; 2480185029Spjd } 2481168404Spjd 2482209962Smm arc_evict_ghost(arc_mru_ghost, guid, -1); 2483209962Smm arc_evict_ghost(arc_mfu_ghost, guid, -1); 2484168404Spjd 2485168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2486168404Spjd arc_do_user_evicts(); 2487168404Spjd mutex_exit(&arc_reclaim_thr_lock); 2488185029Spjd ASSERT(spa || arc_eviction_list == NULL); 2489168404Spjd} 2490168404Spjd 2491168404Spjdvoid 2492168404Spjdarc_shrink(void) 2493168404Spjd{ 2494272875Ssmh 2495168404Spjd if (arc_c > arc_c_min) { 2496168404Spjd uint64_t to_free; 2497168404Spjd 2498272875Ssmh DTRACE_PROBE4(arc__shrink, uint64_t, arc_c, uint64_t, 2499272875Ssmh arc_c_min, uint64_t, arc_p, uint64_t, to_free); 2500168404Spjd#ifdef _KERNEL 2501168404Spjd to_free = arc_c >> arc_shrink_shift; 2502168404Spjd#else 2503168404Spjd to_free = arc_c >> arc_shrink_shift; 2504168404Spjd#endif 2505168404Spjd if (arc_c > arc_c_min + to_free) 2506168404Spjd atomic_add_64(&arc_c, -to_free); 2507168404Spjd else 2508168404Spjd arc_c = arc_c_min; 2509168404Spjd 2510168404Spjd atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 2511168404Spjd if (arc_c > arc_size) 2512168404Spjd arc_c = MAX(arc_size, arc_c_min); 2513168404Spjd if (arc_p > arc_c) 2514168404Spjd arc_p = (arc_c >> 1); 2515272875Ssmh 2516272875Ssmh DTRACE_PROBE2(arc__shrunk, uint64_t, arc_c, uint64_t, 2517272875Ssmh arc_p); 2518272875Ssmh 2519168404Spjd ASSERT(arc_c >= arc_c_min); 2520168404Spjd ASSERT((int64_t)arc_p >= 0); 2521168404Spjd } 2522168404Spjd 2523272875Ssmh if (arc_size > arc_c) { 2524272875Ssmh DTRACE_PROBE2(arc__shrink_adjust, uint64_t, arc_size, 2525272875Ssmh uint64_t, arc_c); 2526168404Spjd arc_adjust(); 2527272875Ssmh } 2528168404Spjd} 2529168404Spjd 2530185029Spjdstatic int needfree = 0; 2531168404Spjd 2532168404Spjdstatic int 2533168404Spjdarc_reclaim_needed(void) 2534168404Spjd{ 2535168404Spjd 2536168404Spjd#ifdef _KERNEL 2537219089Spjd 2538272875Ssmh if (needfree) { 2539272875Ssmh DTRACE_PROBE(arc__reclaim_needfree); 2540197816Skmacy return (1); 2541272875Ssmh } 2542168404Spjd 2543191902Skmacy /* 2544212780Savg * Cooperate with pagedaemon when it's time for it to scan 2545212780Savg * and reclaim some pages. 2546191902Skmacy */ 2547272875Ssmh if (freemem < zfs_arc_free_target) { 2548272875Ssmh DTRACE_PROBE2(arc__reclaim_freemem, uint64_t, 2549272875Ssmh freemem, uint64_t, zfs_arc_free_target); 2550191902Skmacy return (1); 2551272875Ssmh } 2552191902Skmacy 2553219089Spjd#ifdef sun 2554168404Spjd /* 2555185029Spjd * take 'desfree' extra pages, so we reclaim sooner, rather than later 2556185029Spjd */ 2557185029Spjd extra = desfree; 2558185029Spjd 2559185029Spjd /* 2560185029Spjd * check that we're out of range of the pageout scanner. It starts to 2561185029Spjd * schedule paging if freemem is less than lotsfree and needfree. 2562185029Spjd * lotsfree is the high-water mark for pageout, and needfree is the 2563185029Spjd * number of needed free pages. We add extra pages here to make sure 2564185029Spjd * the scanner doesn't start up while we're freeing memory. 2565185029Spjd */ 2566185029Spjd if (freemem < lotsfree + needfree + extra) 2567185029Spjd return (1); 2568185029Spjd 2569185029Spjd /* 2570168404Spjd * check to make sure that swapfs has enough space so that anon 2571185029Spjd * reservations can still succeed. anon_resvmem() checks that the 2572168404Spjd * availrmem is greater than swapfs_minfree, and the number of reserved 2573168404Spjd * swap pages. We also add a bit of extra here just to prevent 2574168404Spjd * circumstances from getting really dire. 2575168404Spjd */ 2576168404Spjd if (availrmem < swapfs_minfree + swapfs_reserve + extra) 2577168404Spjd return (1); 2578168404Spjd 2579168404Spjd /* 2580272875Ssmh * Check that we have enough availrmem that memory locking (e.g., via 2581272875Ssmh * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 2582272875Ssmh * stores the number of pages that cannot be locked; when availrmem 2583272875Ssmh * drops below pages_pp_maximum, page locking mechanisms such as 2584272875Ssmh * page_pp_lock() will fail.) 2585272875Ssmh */ 2586272875Ssmh if (availrmem <= pages_pp_maximum) 2587272875Ssmh return (1); 2588272875Ssmh 2589272875Ssmh#endif /* sun */ 2590272875Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 2591272875Ssmh /* 2592168404Spjd * If we're on an i386 platform, it's possible that we'll exhaust the 2593168404Spjd * kernel heap space before we ever run out of available physical 2594168404Spjd * memory. Most checks of the size of the heap_area compare against 2595168404Spjd * tune.t_minarmem, which is the minimum available real memory that we 2596168404Spjd * can have in the system. However, this is generally fixed at 25 pages 2597168404Spjd * which is so low that it's useless. In this comparison, we seek to 2598168404Spjd * calculate the total heap-size, and reclaim if more than 3/4ths of the 2599185029Spjd * heap is allocated. (Or, in the calculation, if less than 1/4th is 2600168404Spjd * free) 2601168404Spjd */ 2602272875Ssmh if (vmem_size(heap_arena, VMEM_FREE) < 2603272875Ssmh (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2)) { 2604272875Ssmh DTRACE_PROBE2(arc__reclaim_used, uint64_t, 2605272875Ssmh vmem_size(heap_arena, VMEM_FREE), uint64_t, 2606272875Ssmh (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2); 2607168404Spjd return (1); 2608272875Ssmh } 2609282361Smav#define zio_arena NULL 2610282361Smav#else 2611282361Smav#define zio_arena heap_arena 2612168404Spjd#endif 2613282361Smav 2614272875Ssmh /* 2615272875Ssmh * If zio data pages are being allocated out of a separate heap segment, 2616272875Ssmh * then enforce that the size of available vmem for this arena remains 2617272875Ssmh * above about 1/16th free. 2618272875Ssmh * 2619272875Ssmh * Note: The 1/16th arena free requirement was put in place 2620272875Ssmh * to aggressively evict memory from the arc in order to avoid 2621272875Ssmh * memory fragmentation issues. 2622272875Ssmh */ 2623272875Ssmh if (zio_arena != NULL && 2624272875Ssmh vmem_size(zio_arena, VMEM_FREE) < 2625272875Ssmh (vmem_size(zio_arena, VMEM_ALLOC) >> 4)) 2626168404Spjd return (1); 2627282361Smav 2628282361Smav /* 2629282361Smav * Above limits know nothing about real level of KVA fragmentation. 2630282361Smav * Start aggressive reclamation if too little sequential KVA left. 2631282361Smav */ 2632282361Smav if (vmem_size(heap_arena, VMEM_MAXFREE) < zfs_max_recordsize) 2633282361Smav return (1); 2634282361Smav 2635272875Ssmh#else /* _KERNEL */ 2636168404Spjd if (spa_get_random(100) == 0) 2637168404Spjd return (1); 2638272875Ssmh#endif /* _KERNEL */ 2639272875Ssmh DTRACE_PROBE(arc__reclaim_no); 2640272875Ssmh 2641168404Spjd return (0); 2642168404Spjd} 2643168404Spjd 2644208454Spjdextern kmem_cache_t *zio_buf_cache[]; 2645208454Spjdextern kmem_cache_t *zio_data_buf_cache[]; 2646273194Sdelphijextern kmem_cache_t *range_seg_cache; 2647208454Spjd 2648285717Sjpaetzelstatic __noinline void 2649168404Spjdarc_kmem_reap_now(arc_reclaim_strategy_t strat) 2650168404Spjd{ 2651168404Spjd size_t i; 2652168404Spjd kmem_cache_t *prev_cache = NULL; 2653168404Spjd kmem_cache_t *prev_data_cache = NULL; 2654168404Spjd 2655272875Ssmh DTRACE_PROBE(arc__kmem_reap_start); 2656168404Spjd#ifdef _KERNEL 2657185029Spjd if (arc_meta_used >= arc_meta_limit) { 2658185029Spjd /* 2659185029Spjd * We are exceeding our meta-data cache limit. 2660185029Spjd * Purge some DNLC entries to release holds on meta-data. 2661185029Spjd */ 2662185029Spjd dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 2663185029Spjd } 2664168404Spjd#if defined(__i386) 2665168404Spjd /* 2666168404Spjd * Reclaim unused memory from all kmem caches. 2667168404Spjd */ 2668168404Spjd kmem_reap(); 2669168404Spjd#endif 2670168404Spjd#endif 2671168404Spjd 2672168404Spjd /* 2673185029Spjd * An aggressive reclamation will shrink the cache size as well as 2674168404Spjd * reap free buffers from the arc kmem caches. 2675168404Spjd */ 2676168404Spjd if (strat == ARC_RECLAIM_AGGR) 2677168404Spjd arc_shrink(); 2678168404Spjd 2679168404Spjd for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2680168404Spjd if (zio_buf_cache[i] != prev_cache) { 2681168404Spjd prev_cache = zio_buf_cache[i]; 2682168404Spjd kmem_cache_reap_now(zio_buf_cache[i]); 2683168404Spjd } 2684168404Spjd if (zio_data_buf_cache[i] != prev_data_cache) { 2685168404Spjd prev_data_cache = zio_data_buf_cache[i]; 2686168404Spjd kmem_cache_reap_now(zio_data_buf_cache[i]); 2687168404Spjd } 2688168404Spjd } 2689168404Spjd kmem_cache_reap_now(buf_cache); 2690168404Spjd kmem_cache_reap_now(hdr_cache); 2691273193Sdelphij kmem_cache_reap_now(range_seg_cache); 2692272875Ssmh 2693272875Ssmh#ifdef sun 2694272875Ssmh /* 2695272875Ssmh * Ask the vmem arena to reclaim unused memory from its 2696272875Ssmh * quantum caches. 2697272875Ssmh */ 2698272875Ssmh if (zio_arena != NULL && strat == ARC_RECLAIM_AGGR) 2699272875Ssmh vmem_qcache_reap(zio_arena); 2700272875Ssmh#endif 2701272875Ssmh DTRACE_PROBE(arc__kmem_reap_end); 2702168404Spjd} 2703168404Spjd 2704168404Spjdstatic void 2705168404Spjdarc_reclaim_thread(void *dummy __unused) 2706168404Spjd{ 2707168404Spjd clock_t growtime = 0; 2708168404Spjd arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2709168404Spjd callb_cpr_t cpr; 2710168404Spjd 2711168404Spjd CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2712168404Spjd 2713168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2714168404Spjd while (arc_thread_exit == 0) { 2715168404Spjd if (arc_reclaim_needed()) { 2716168404Spjd 2717168404Spjd if (arc_no_grow) { 2718168404Spjd if (last_reclaim == ARC_RECLAIM_CONS) { 2719272875Ssmh DTRACE_PROBE(arc__reclaim_aggr_no_grow); 2720168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2721168404Spjd } else { 2722168404Spjd last_reclaim = ARC_RECLAIM_CONS; 2723168404Spjd } 2724168404Spjd } else { 2725168404Spjd arc_no_grow = TRUE; 2726168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2727272875Ssmh DTRACE_PROBE(arc__reclaim_aggr); 2728168404Spjd membar_producer(); 2729168404Spjd } 2730168404Spjd 2731168404Spjd /* reset the growth delay for every reclaim */ 2732219089Spjd growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 2733168404Spjd 2734185029Spjd if (needfree && last_reclaim == ARC_RECLAIM_CONS) { 2735168404Spjd /* 2736185029Spjd * If needfree is TRUE our vm_lowmem hook 2737168404Spjd * was called and in that case we must free some 2738168404Spjd * memory, so switch to aggressive mode. 2739168404Spjd */ 2740168404Spjd arc_no_grow = TRUE; 2741168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2742168404Spjd } 2743168404Spjd arc_kmem_reap_now(last_reclaim); 2744185029Spjd arc_warm = B_TRUE; 2745185029Spjd 2746219089Spjd } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 2747168404Spjd arc_no_grow = FALSE; 2748168404Spjd } 2749168404Spjd 2750209275Smm arc_adjust(); 2751168404Spjd 2752168404Spjd if (arc_eviction_list != NULL) 2753168404Spjd arc_do_user_evicts(); 2754168404Spjd 2755211762Savg#ifdef _KERNEL 2756211762Savg if (needfree) { 2757185029Spjd needfree = 0; 2758185029Spjd wakeup(&needfree); 2759211762Savg } 2760168404Spjd#endif 2761168404Spjd 2762168404Spjd /* block until needed, or one second, whichever is shorter */ 2763168404Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 2764168404Spjd (void) cv_timedwait(&arc_reclaim_thr_cv, 2765168404Spjd &arc_reclaim_thr_lock, hz); 2766168404Spjd CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2767168404Spjd } 2768168404Spjd 2769168404Spjd arc_thread_exit = 0; 2770168404Spjd cv_broadcast(&arc_reclaim_thr_cv); 2771168404Spjd CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2772168404Spjd thread_exit(); 2773168404Spjd} 2774168404Spjd 2775168404Spjd/* 2776168404Spjd * Adapt arc info given the number of bytes we are trying to add and 2777168404Spjd * the state that we are comming from. This function is only called 2778168404Spjd * when we are adding new content to the cache. 2779168404Spjd */ 2780168404Spjdstatic void 2781168404Spjdarc_adapt(int bytes, arc_state_t *state) 2782168404Spjd{ 2783168404Spjd int mult; 2784208373Smm uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 2785168404Spjd 2786185029Spjd if (state == arc_l2c_only) 2787185029Spjd return; 2788185029Spjd 2789168404Spjd ASSERT(bytes > 0); 2790168404Spjd /* 2791168404Spjd * Adapt the target size of the MRU list: 2792168404Spjd * - if we just hit in the MRU ghost list, then increase 2793168404Spjd * the target size of the MRU list. 2794168404Spjd * - if we just hit in the MFU ghost list, then increase 2795168404Spjd * the target size of the MFU list by decreasing the 2796168404Spjd * target size of the MRU list. 2797168404Spjd */ 2798168404Spjd if (state == arc_mru_ghost) { 2799168404Spjd mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 2800168404Spjd 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 2801209275Smm mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 2802168404Spjd 2803208373Smm arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 2804168404Spjd } else if (state == arc_mfu_ghost) { 2805208373Smm uint64_t delta; 2806208373Smm 2807168404Spjd mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 2808168404Spjd 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 2809209275Smm mult = MIN(mult, 10); 2810168404Spjd 2811208373Smm delta = MIN(bytes * mult, arc_p); 2812208373Smm arc_p = MAX(arc_p_min, arc_p - delta); 2813168404Spjd } 2814168404Spjd ASSERT((int64_t)arc_p >= 0); 2815168404Spjd 2816168404Spjd if (arc_reclaim_needed()) { 2817168404Spjd cv_signal(&arc_reclaim_thr_cv); 2818168404Spjd return; 2819168404Spjd } 2820168404Spjd 2821168404Spjd if (arc_no_grow) 2822168404Spjd return; 2823168404Spjd 2824168404Spjd if (arc_c >= arc_c_max) 2825168404Spjd return; 2826168404Spjd 2827168404Spjd /* 2828168404Spjd * If we're within (2 * maxblocksize) bytes of the target 2829168404Spjd * cache size, increment the target cache size 2830168404Spjd */ 2831168404Spjd if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 2832272875Ssmh DTRACE_PROBE1(arc__inc_adapt, int, bytes); 2833168404Spjd atomic_add_64(&arc_c, (int64_t)bytes); 2834168404Spjd if (arc_c > arc_c_max) 2835168404Spjd arc_c = arc_c_max; 2836168404Spjd else if (state == arc_anon) 2837168404Spjd atomic_add_64(&arc_p, (int64_t)bytes); 2838168404Spjd if (arc_p > arc_c) 2839168404Spjd arc_p = arc_c; 2840168404Spjd } 2841168404Spjd ASSERT((int64_t)arc_p >= 0); 2842168404Spjd} 2843168404Spjd 2844168404Spjd/* 2845168404Spjd * Check if the cache has reached its limits and eviction is required 2846168404Spjd * prior to insert. 2847168404Spjd */ 2848168404Spjdstatic int 2849185029Spjdarc_evict_needed(arc_buf_contents_t type) 2850168404Spjd{ 2851185029Spjd if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 2852185029Spjd return (1); 2853185029Spjd 2854168404Spjd if (arc_reclaim_needed()) 2855168404Spjd return (1); 2856168404Spjd 2857168404Spjd return (arc_size > arc_c); 2858168404Spjd} 2859168404Spjd 2860168404Spjd/* 2861168404Spjd * The buffer, supplied as the first argument, needs a data block. 2862168404Spjd * So, if we are at cache max, determine which cache should be victimized. 2863168404Spjd * We have the following cases: 2864168404Spjd * 2865168404Spjd * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2866168404Spjd * In this situation if we're out of space, but the resident size of the MFU is 2867168404Spjd * under the limit, victimize the MFU cache to satisfy this insertion request. 2868168404Spjd * 2869168404Spjd * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2870168404Spjd * Here, we've used up all of the available space for the MRU, so we need to 2871168404Spjd * evict from our own cache instead. Evict from the set of resident MRU 2872168404Spjd * entries. 2873168404Spjd * 2874168404Spjd * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2875168404Spjd * c minus p represents the MFU space in the cache, since p is the size of the 2876168404Spjd * cache that is dedicated to the MRU. In this situation there's still space on 2877168404Spjd * the MFU side, so the MRU side needs to be victimized. 2878168404Spjd * 2879168404Spjd * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2880168404Spjd * MFU's resident set is consuming more space than it has been allotted. In 2881168404Spjd * this situation, we must victimize our own cache, the MFU, for this insertion. 2882168404Spjd */ 2883168404Spjdstatic void 2884168404Spjdarc_get_data_buf(arc_buf_t *buf) 2885168404Spjd{ 2886168404Spjd arc_state_t *state = buf->b_hdr->b_state; 2887168404Spjd uint64_t size = buf->b_hdr->b_size; 2888168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 2889168404Spjd 2890168404Spjd arc_adapt(size, state); 2891168404Spjd 2892168404Spjd /* 2893168404Spjd * We have not yet reached cache maximum size, 2894168404Spjd * just allocate a new buffer. 2895168404Spjd */ 2896185029Spjd if (!arc_evict_needed(type)) { 2897168404Spjd if (type == ARC_BUFC_METADATA) { 2898168404Spjd buf->b_data = zio_buf_alloc(size); 2899208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2900168404Spjd } else { 2901168404Spjd ASSERT(type == ARC_BUFC_DATA); 2902168404Spjd buf->b_data = zio_data_buf_alloc(size); 2903208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2904185029Spjd atomic_add_64(&arc_size, size); 2905168404Spjd } 2906168404Spjd goto out; 2907168404Spjd } 2908168404Spjd 2909168404Spjd /* 2910168404Spjd * If we are prefetching from the mfu ghost list, this buffer 2911168404Spjd * will end up on the mru list; so steal space from there. 2912168404Spjd */ 2913168404Spjd if (state == arc_mfu_ghost) 2914277586Sdelphij state = buf->b_hdr->b_flags & ARC_FLAG_PREFETCH ? 2915277586Sdelphij arc_mru : arc_mfu; 2916168404Spjd else if (state == arc_mru_ghost) 2917168404Spjd state = arc_mru; 2918168404Spjd 2919168404Spjd if (state == arc_mru || state == arc_anon) { 2920168404Spjd uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 2921208373Smm state = (arc_mfu->arcs_lsize[type] >= size && 2922185029Spjd arc_p > mru_used) ? arc_mfu : arc_mru; 2923168404Spjd } else { 2924168404Spjd /* MFU cases */ 2925168404Spjd uint64_t mfu_space = arc_c - arc_p; 2926208373Smm state = (arc_mru->arcs_lsize[type] >= size && 2927185029Spjd mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 2928168404Spjd } 2929209962Smm if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) { 2930168404Spjd if (type == ARC_BUFC_METADATA) { 2931168404Spjd buf->b_data = zio_buf_alloc(size); 2932208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2933168404Spjd } else { 2934168404Spjd ASSERT(type == ARC_BUFC_DATA); 2935168404Spjd buf->b_data = zio_data_buf_alloc(size); 2936208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2937185029Spjd atomic_add_64(&arc_size, size); 2938168404Spjd } 2939168404Spjd ARCSTAT_BUMP(arcstat_recycle_miss); 2940168404Spjd } 2941168404Spjd ASSERT(buf->b_data != NULL); 2942168404Spjdout: 2943168404Spjd /* 2944168404Spjd * Update the state size. Note that ghost states have a 2945168404Spjd * "ghost size" and so don't need to be updated. 2946168404Spjd */ 2947168404Spjd if (!GHOST_STATE(buf->b_hdr->b_state)) { 2948168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2949168404Spjd 2950168404Spjd atomic_add_64(&hdr->b_state->arcs_size, size); 2951168404Spjd if (list_link_active(&hdr->b_arc_node)) { 2952168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 2953185029Spjd atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2954168404Spjd } 2955168404Spjd /* 2956168404Spjd * If we are growing the cache, and we are adding anonymous 2957168404Spjd * data, and we have outgrown arc_p, update arc_p 2958168404Spjd */ 2959168404Spjd if (arc_size < arc_c && hdr->b_state == arc_anon && 2960168404Spjd arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 2961168404Spjd arc_p = MIN(arc_c, arc_p + size); 2962168404Spjd } 2963205231Skmacy ARCSTAT_BUMP(arcstat_allocated); 2964168404Spjd} 2965168404Spjd 2966168404Spjd/* 2967168404Spjd * This routine is called whenever a buffer is accessed. 2968168404Spjd * NOTE: the hash lock is dropped in this function. 2969168404Spjd */ 2970168404Spjdstatic void 2971277586Sdelphijarc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 2972168404Spjd{ 2973219089Spjd clock_t now; 2974219089Spjd 2975168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 2976168404Spjd 2977277586Sdelphij if (hdr->b_state == arc_anon) { 2978168404Spjd /* 2979168404Spjd * This buffer is not in the cache, and does not 2980168404Spjd * appear in our "ghost" list. Add the new buffer 2981168404Spjd * to the MRU state. 2982168404Spjd */ 2983168404Spjd 2984277586Sdelphij ASSERT(hdr->b_arc_access == 0); 2985277586Sdelphij hdr->b_arc_access = ddi_get_lbolt(); 2986277586Sdelphij DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 2987277586Sdelphij arc_change_state(arc_mru, hdr, hash_lock); 2988168404Spjd 2989277586Sdelphij } else if (hdr->b_state == arc_mru) { 2990219089Spjd now = ddi_get_lbolt(); 2991219089Spjd 2992168404Spjd /* 2993168404Spjd * If this buffer is here because of a prefetch, then either: 2994168404Spjd * - clear the flag if this is a "referencing" read 2995168404Spjd * (any subsequent access will bump this into the MFU state). 2996168404Spjd * or 2997168404Spjd * - move the buffer to the head of the list if this is 2998168404Spjd * another prefetch (to make it less likely to be evicted). 2999168404Spjd */ 3000277586Sdelphij if ((hdr->b_flags & ARC_FLAG_PREFETCH) != 0) { 3001277586Sdelphij if (refcount_count(&hdr->b_refcnt) == 0) { 3002277586Sdelphij ASSERT(list_link_active(&hdr->b_arc_node)); 3003168404Spjd } else { 3004277586Sdelphij hdr->b_flags &= ~ARC_FLAG_PREFETCH; 3005168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 3006168404Spjd } 3007277586Sdelphij hdr->b_arc_access = now; 3008168404Spjd return; 3009168404Spjd } 3010168404Spjd 3011168404Spjd /* 3012168404Spjd * This buffer has been "accessed" only once so far, 3013168404Spjd * but it is still in the cache. Move it to the MFU 3014168404Spjd * state. 3015168404Spjd */ 3016277586Sdelphij if (now > hdr->b_arc_access + ARC_MINTIME) { 3017168404Spjd /* 3018168404Spjd * More than 125ms have passed since we 3019168404Spjd * instantiated this buffer. Move it to the 3020168404Spjd * most frequently used state. 3021168404Spjd */ 3022277586Sdelphij hdr->b_arc_access = now; 3023277586Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3024277586Sdelphij arc_change_state(arc_mfu, hdr, hash_lock); 3025168404Spjd } 3026168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 3027277586Sdelphij } else if (hdr->b_state == arc_mru_ghost) { 3028168404Spjd arc_state_t *new_state; 3029168404Spjd /* 3030168404Spjd * This buffer has been "accessed" recently, but 3031168404Spjd * was evicted from the cache. Move it to the 3032168404Spjd * MFU state. 3033168404Spjd */ 3034168404Spjd 3035277586Sdelphij if (hdr->b_flags & ARC_FLAG_PREFETCH) { 3036168404Spjd new_state = arc_mru; 3037277586Sdelphij if (refcount_count(&hdr->b_refcnt) > 0) 3038277586Sdelphij hdr->b_flags &= ~ARC_FLAG_PREFETCH; 3039277586Sdelphij DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 3040168404Spjd } else { 3041168404Spjd new_state = arc_mfu; 3042277586Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3043168404Spjd } 3044168404Spjd 3045277586Sdelphij hdr->b_arc_access = ddi_get_lbolt(); 3046277586Sdelphij arc_change_state(new_state, hdr, hash_lock); 3047168404Spjd 3048168404Spjd ARCSTAT_BUMP(arcstat_mru_ghost_hits); 3049277586Sdelphij } else if (hdr->b_state == arc_mfu) { 3050168404Spjd /* 3051168404Spjd * This buffer has been accessed more than once and is 3052168404Spjd * still in the cache. Keep it in the MFU state. 3053168404Spjd * 3054168404Spjd * NOTE: an add_reference() that occurred when we did 3055168404Spjd * the arc_read() will have kicked this off the list. 3056168404Spjd * If it was a prefetch, we will explicitly move it to 3057168404Spjd * the head of the list now. 3058168404Spjd */ 3059277586Sdelphij if ((hdr->b_flags & ARC_FLAG_PREFETCH) != 0) { 3060277586Sdelphij ASSERT(refcount_count(&hdr->b_refcnt) == 0); 3061277586Sdelphij ASSERT(list_link_active(&hdr->b_arc_node)); 3062168404Spjd } 3063168404Spjd ARCSTAT_BUMP(arcstat_mfu_hits); 3064277586Sdelphij hdr->b_arc_access = ddi_get_lbolt(); 3065277586Sdelphij } else if (hdr->b_state == arc_mfu_ghost) { 3066168404Spjd arc_state_t *new_state = arc_mfu; 3067168404Spjd /* 3068168404Spjd * This buffer has been accessed more than once but has 3069168404Spjd * been evicted from the cache. Move it back to the 3070168404Spjd * MFU state. 3071168404Spjd */ 3072168404Spjd 3073277586Sdelphij if (hdr->b_flags & ARC_FLAG_PREFETCH) { 3074168404Spjd /* 3075168404Spjd * This is a prefetch access... 3076168404Spjd * move this block back to the MRU state. 3077168404Spjd */ 3078277586Sdelphij ASSERT0(refcount_count(&hdr->b_refcnt)); 3079168404Spjd new_state = arc_mru; 3080168404Spjd } 3081168404Spjd 3082277586Sdelphij hdr->b_arc_access = ddi_get_lbolt(); 3083277586Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3084277586Sdelphij arc_change_state(new_state, hdr, hash_lock); 3085168404Spjd 3086168404Spjd ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 3087277586Sdelphij } else if (hdr->b_state == arc_l2c_only) { 3088185029Spjd /* 3089185029Spjd * This buffer is on the 2nd Level ARC. 3090185029Spjd */ 3091185029Spjd 3092277586Sdelphij hdr->b_arc_access = ddi_get_lbolt(); 3093277586Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3094277586Sdelphij arc_change_state(arc_mfu, hdr, hash_lock); 3095168404Spjd } else { 3096168404Spjd ASSERT(!"invalid arc state"); 3097168404Spjd } 3098168404Spjd} 3099168404Spjd 3100168404Spjd/* a generic arc_done_func_t which you can use */ 3101168404Spjd/* ARGSUSED */ 3102168404Spjdvoid 3103168404Spjdarc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 3104168404Spjd{ 3105219089Spjd if (zio == NULL || zio->io_error == 0) 3106219089Spjd bcopy(buf->b_data, arg, buf->b_hdr->b_size); 3107248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 3108168404Spjd} 3109168404Spjd 3110185029Spjd/* a generic arc_done_func_t */ 3111168404Spjdvoid 3112168404Spjdarc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 3113168404Spjd{ 3114168404Spjd arc_buf_t **bufp = arg; 3115168404Spjd if (zio && zio->io_error) { 3116248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 3117168404Spjd *bufp = NULL; 3118168404Spjd } else { 3119168404Spjd *bufp = buf; 3120219089Spjd ASSERT(buf->b_data); 3121168404Spjd } 3122168404Spjd} 3123168404Spjd 3124168404Spjdstatic void 3125168404Spjdarc_read_done(zio_t *zio) 3126168404Spjd{ 3127268649Sdelphij arc_buf_hdr_t *hdr; 3128168404Spjd arc_buf_t *buf; 3129168404Spjd arc_buf_t *abuf; /* buffer we're assigning to callback */ 3130268649Sdelphij kmutex_t *hash_lock = NULL; 3131168404Spjd arc_callback_t *callback_list, *acb; 3132168404Spjd int freeable = FALSE; 3133168404Spjd 3134168404Spjd buf = zio->io_private; 3135168404Spjd hdr = buf->b_hdr; 3136168404Spjd 3137168404Spjd /* 3138168404Spjd * The hdr was inserted into hash-table and removed from lists 3139168404Spjd * prior to starting I/O. We should find this header, since 3140168404Spjd * it's in the hash table, and it should be legit since it's 3141168404Spjd * not possible to evict it during the I/O. The only possible 3142168404Spjd * reason for it not to be found is if we were freed during the 3143168404Spjd * read. 3144168404Spjd */ 3145268649Sdelphij if (HDR_IN_HASH_TABLE(hdr)) { 3146268649Sdelphij ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 3147268649Sdelphij ASSERT3U(hdr->b_dva.dva_word[0], ==, 3148268649Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[0]); 3149268649Sdelphij ASSERT3U(hdr->b_dva.dva_word[1], ==, 3150268649Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[1]); 3151168404Spjd 3152268649Sdelphij arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 3153268649Sdelphij &hash_lock); 3154168404Spjd 3155268649Sdelphij ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && 3156268649Sdelphij hash_lock == NULL) || 3157268649Sdelphij (found == hdr && 3158268649Sdelphij DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 3159268649Sdelphij (found == hdr && HDR_L2_READING(hdr))); 3160268649Sdelphij } 3161268649Sdelphij 3162277586Sdelphij hdr->b_flags &= ~ARC_FLAG_L2_EVICTED; 3163277586Sdelphij if (l2arc_noprefetch && (hdr->b_flags & ARC_FLAG_PREFETCH)) 3164277586Sdelphij hdr->b_flags &= ~ARC_FLAG_L2CACHE; 3165206796Spjd 3166168404Spjd /* byteswap if necessary */ 3167168404Spjd callback_list = hdr->b_acb; 3168168404Spjd ASSERT(callback_list != NULL); 3169209101Smm if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 3170236884Smm dmu_object_byteswap_t bswap = 3171236884Smm DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 3172185029Spjd arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 3173185029Spjd byteswap_uint64_array : 3174236884Smm dmu_ot_byteswap[bswap].ob_func; 3175185029Spjd func(buf->b_data, hdr->b_size); 3176185029Spjd } 3177168404Spjd 3178185029Spjd arc_cksum_compute(buf, B_FALSE); 3179240133Smm#ifdef illumos 3180240133Smm arc_buf_watch(buf); 3181240133Smm#endif /* illumos */ 3182168404Spjd 3183219089Spjd if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 3184219089Spjd /* 3185219089Spjd * Only call arc_access on anonymous buffers. This is because 3186219089Spjd * if we've issued an I/O for an evicted buffer, we've already 3187219089Spjd * called arc_access (to prevent any simultaneous readers from 3188219089Spjd * getting confused). 3189219089Spjd */ 3190219089Spjd arc_access(hdr, hash_lock); 3191219089Spjd } 3192219089Spjd 3193168404Spjd /* create copies of the data buffer for the callers */ 3194168404Spjd abuf = buf; 3195168404Spjd for (acb = callback_list; acb; acb = acb->acb_next) { 3196168404Spjd if (acb->acb_done) { 3197242845Sdelphij if (abuf == NULL) { 3198242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_reads); 3199168404Spjd abuf = arc_buf_clone(buf); 3200242845Sdelphij } 3201168404Spjd acb->acb_buf = abuf; 3202168404Spjd abuf = NULL; 3203168404Spjd } 3204168404Spjd } 3205168404Spjd hdr->b_acb = NULL; 3206277586Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 3207168404Spjd ASSERT(!HDR_BUF_AVAILABLE(hdr)); 3208219089Spjd if (abuf == buf) { 3209219089Spjd ASSERT(buf->b_efunc == NULL); 3210219089Spjd ASSERT(hdr->b_datacnt == 1); 3211277586Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 3212219089Spjd } 3213168404Spjd 3214168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 3215168404Spjd 3216168404Spjd if (zio->io_error != 0) { 3217277586Sdelphij hdr->b_flags |= ARC_FLAG_IO_ERROR; 3218168404Spjd if (hdr->b_state != arc_anon) 3219168404Spjd arc_change_state(arc_anon, hdr, hash_lock); 3220168404Spjd if (HDR_IN_HASH_TABLE(hdr)) 3221168404Spjd buf_hash_remove(hdr); 3222168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 3223168404Spjd } 3224168404Spjd 3225168404Spjd /* 3226168404Spjd * Broadcast before we drop the hash_lock to avoid the possibility 3227168404Spjd * that the hdr (and hence the cv) might be freed before we get to 3228168404Spjd * the cv_broadcast(). 3229168404Spjd */ 3230168404Spjd cv_broadcast(&hdr->b_cv); 3231168404Spjd 3232168404Spjd if (hash_lock) { 3233168404Spjd mutex_exit(hash_lock); 3234168404Spjd } else { 3235168404Spjd /* 3236168404Spjd * This block was freed while we waited for the read to 3237168404Spjd * complete. It has been removed from the hash table and 3238168404Spjd * moved to the anonymous state (so that it won't show up 3239168404Spjd * in the cache). 3240168404Spjd */ 3241168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 3242168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 3243168404Spjd } 3244168404Spjd 3245168404Spjd /* execute each callback and free its structure */ 3246168404Spjd while ((acb = callback_list) != NULL) { 3247168404Spjd if (acb->acb_done) 3248168404Spjd acb->acb_done(zio, acb->acb_buf, acb->acb_private); 3249168404Spjd 3250168404Spjd if (acb->acb_zio_dummy != NULL) { 3251168404Spjd acb->acb_zio_dummy->io_error = zio->io_error; 3252168404Spjd zio_nowait(acb->acb_zio_dummy); 3253168404Spjd } 3254168404Spjd 3255168404Spjd callback_list = acb->acb_next; 3256168404Spjd kmem_free(acb, sizeof (arc_callback_t)); 3257168404Spjd } 3258168404Spjd 3259168404Spjd if (freeable) 3260168404Spjd arc_hdr_destroy(hdr); 3261168404Spjd} 3262168404Spjd 3263168404Spjd/* 3264168404Spjd * "Read" the block block at the specified DVA (in bp) via the 3265168404Spjd * cache. If the block is found in the cache, invoke the provided 3266168404Spjd * callback immediately and return. Note that the `zio' parameter 3267168404Spjd * in the callback will be NULL in this case, since no IO was 3268168404Spjd * required. If the block is not in the cache pass the read request 3269168404Spjd * on to the spa with a substitute callback function, so that the 3270168404Spjd * requested block will be added to the cache. 3271168404Spjd * 3272168404Spjd * If a read request arrives for a block that has a read in-progress, 3273168404Spjd * either wait for the in-progress read to complete (and return the 3274168404Spjd * results); or, if this is a read with a "done" func, add a record 3275168404Spjd * to the read to invoke the "done" func when the read completes, 3276168404Spjd * and return; or just return. 3277168404Spjd * 3278168404Spjd * arc_read_done() will invoke all the requested "done" functions 3279168404Spjd * for readers of this block. 3280168404Spjd */ 3281168404Spjdint 3282246666Smmarc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 3283277586Sdelphij void *private, zio_priority_t priority, int zio_flags, 3284277586Sdelphij arc_flags_t *arc_flags, const zbookmark_phys_t *zb) 3285168404Spjd{ 3286268649Sdelphij arc_buf_hdr_t *hdr = NULL; 3287247187Smm arc_buf_t *buf = NULL; 3288268649Sdelphij kmutex_t *hash_lock = NULL; 3289185029Spjd zio_t *rzio; 3290228103Smm uint64_t guid = spa_load_guid(spa); 3291168404Spjd 3292268649Sdelphij ASSERT(!BP_IS_EMBEDDED(bp) || 3293268649Sdelphij BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 3294268649Sdelphij 3295168404Spjdtop: 3296268649Sdelphij if (!BP_IS_EMBEDDED(bp)) { 3297268649Sdelphij /* 3298268649Sdelphij * Embedded BP's have no DVA and require no I/O to "read". 3299268649Sdelphij * Create an anonymous arc buf to back it. 3300268649Sdelphij */ 3301268649Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 3302268649Sdelphij } 3303168404Spjd 3304268649Sdelphij if (hdr != NULL && hdr->b_datacnt > 0) { 3305268649Sdelphij 3306277586Sdelphij *arc_flags |= ARC_FLAG_CACHED; 3307168404Spjd 3308168404Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3309168404Spjd 3310277586Sdelphij if (*arc_flags & ARC_FLAG_WAIT) { 3311168404Spjd cv_wait(&hdr->b_cv, hash_lock); 3312168404Spjd mutex_exit(hash_lock); 3313168404Spjd goto top; 3314168404Spjd } 3315277586Sdelphij ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 3316168404Spjd 3317168404Spjd if (done) { 3318168404Spjd arc_callback_t *acb = NULL; 3319168404Spjd 3320168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), 3321168404Spjd KM_SLEEP); 3322168404Spjd acb->acb_done = done; 3323168404Spjd acb->acb_private = private; 3324168404Spjd if (pio != NULL) 3325168404Spjd acb->acb_zio_dummy = zio_null(pio, 3326209962Smm spa, NULL, NULL, NULL, zio_flags); 3327168404Spjd 3328168404Spjd ASSERT(acb->acb_done != NULL); 3329168404Spjd acb->acb_next = hdr->b_acb; 3330168404Spjd hdr->b_acb = acb; 3331168404Spjd add_reference(hdr, hash_lock, private); 3332168404Spjd mutex_exit(hash_lock); 3333168404Spjd return (0); 3334168404Spjd } 3335168404Spjd mutex_exit(hash_lock); 3336168404Spjd return (0); 3337168404Spjd } 3338168404Spjd 3339168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3340168404Spjd 3341168404Spjd if (done) { 3342168404Spjd add_reference(hdr, hash_lock, private); 3343168404Spjd /* 3344168404Spjd * If this block is already in use, create a new 3345168404Spjd * copy of the data so that we will be guaranteed 3346168404Spjd * that arc_release() will always succeed. 3347168404Spjd */ 3348168404Spjd buf = hdr->b_buf; 3349168404Spjd ASSERT(buf); 3350168404Spjd ASSERT(buf->b_data); 3351168404Spjd if (HDR_BUF_AVAILABLE(hdr)) { 3352168404Spjd ASSERT(buf->b_efunc == NULL); 3353277586Sdelphij hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 3354168404Spjd } else { 3355168404Spjd buf = arc_buf_clone(buf); 3356168404Spjd } 3357219089Spjd 3358277586Sdelphij } else if (*arc_flags & ARC_FLAG_PREFETCH && 3359168404Spjd refcount_count(&hdr->b_refcnt) == 0) { 3360277586Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 3361168404Spjd } 3362168404Spjd DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 3363168404Spjd arc_access(hdr, hash_lock); 3364277586Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 3365277586Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 3366277586Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 3367277586Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 3368168404Spjd mutex_exit(hash_lock); 3369168404Spjd ARCSTAT_BUMP(arcstat_hits); 3370277586Sdelphij ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_FLAG_PREFETCH), 3371168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3372168404Spjd data, metadata, hits); 3373168404Spjd 3374168404Spjd if (done) 3375168404Spjd done(NULL, buf, private); 3376168404Spjd } else { 3377168404Spjd uint64_t size = BP_GET_LSIZE(bp); 3378268649Sdelphij arc_callback_t *acb; 3379185029Spjd vdev_t *vd = NULL; 3380247187Smm uint64_t addr = 0; 3381208373Smm boolean_t devw = B_FALSE; 3382258566Savg enum zio_compress b_compress = ZIO_COMPRESS_OFF; 3383258566Savg uint64_t b_asize = 0; 3384168404Spjd 3385168404Spjd if (hdr == NULL) { 3386168404Spjd /* this block is not in the cache */ 3387268649Sdelphij arc_buf_hdr_t *exists = NULL; 3388168404Spjd arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 3389168404Spjd buf = arc_buf_alloc(spa, size, private, type); 3390168404Spjd hdr = buf->b_hdr; 3391268649Sdelphij if (!BP_IS_EMBEDDED(bp)) { 3392268649Sdelphij hdr->b_dva = *BP_IDENTITY(bp); 3393268649Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 3394268649Sdelphij hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 3395268649Sdelphij exists = buf_hash_insert(hdr, &hash_lock); 3396268649Sdelphij } 3397268649Sdelphij if (exists != NULL) { 3398168404Spjd /* somebody beat us to the hash insert */ 3399168404Spjd mutex_exit(hash_lock); 3400219089Spjd buf_discard_identity(hdr); 3401168404Spjd (void) arc_buf_remove_ref(buf, private); 3402168404Spjd goto top; /* restart the IO request */ 3403168404Spjd } 3404277586Sdelphij 3405168404Spjd /* if this is a prefetch, we don't have a reference */ 3406277586Sdelphij if (*arc_flags & ARC_FLAG_PREFETCH) { 3407168404Spjd (void) remove_reference(hdr, hash_lock, 3408168404Spjd private); 3409277586Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 3410168404Spjd } 3411277586Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 3412277586Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 3413277586Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 3414277586Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 3415168404Spjd if (BP_GET_LEVEL(bp) > 0) 3416277586Sdelphij hdr->b_flags |= ARC_FLAG_INDIRECT; 3417168404Spjd } else { 3418168404Spjd /* this block is in the ghost cache */ 3419168404Spjd ASSERT(GHOST_STATE(hdr->b_state)); 3420168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3421240415Smm ASSERT0(refcount_count(&hdr->b_refcnt)); 3422168404Spjd ASSERT(hdr->b_buf == NULL); 3423168404Spjd 3424168404Spjd /* if this is a prefetch, we don't have a reference */ 3425277586Sdelphij if (*arc_flags & ARC_FLAG_PREFETCH) 3426277586Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 3427168404Spjd else 3428168404Spjd add_reference(hdr, hash_lock, private); 3429277586Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 3430277586Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 3431277586Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 3432277586Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 3433185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 3434168404Spjd buf->b_hdr = hdr; 3435168404Spjd buf->b_data = NULL; 3436168404Spjd buf->b_efunc = NULL; 3437168404Spjd buf->b_private = NULL; 3438168404Spjd buf->b_next = NULL; 3439168404Spjd hdr->b_buf = buf; 3440168404Spjd ASSERT(hdr->b_datacnt == 0); 3441168404Spjd hdr->b_datacnt = 1; 3442219089Spjd arc_get_data_buf(buf); 3443219089Spjd arc_access(hdr, hash_lock); 3444168404Spjd } 3445168404Spjd 3446219089Spjd ASSERT(!GHOST_STATE(hdr->b_state)); 3447219089Spjd 3448168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 3449168404Spjd acb->acb_done = done; 3450168404Spjd acb->acb_private = private; 3451168404Spjd 3452168404Spjd ASSERT(hdr->b_acb == NULL); 3453168404Spjd hdr->b_acb = acb; 3454277586Sdelphij hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; 3455168404Spjd 3456258566Savg if (hdr->b_l2hdr != NULL && 3457185029Spjd (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 3458208373Smm devw = hdr->b_l2hdr->b_dev->l2ad_writing; 3459185029Spjd addr = hdr->b_l2hdr->b_daddr; 3460258566Savg b_compress = hdr->b_l2hdr->b_compress; 3461258566Savg b_asize = hdr->b_l2hdr->b_asize; 3462185029Spjd /* 3463185029Spjd * Lock out device removal. 3464185029Spjd */ 3465185029Spjd if (vdev_is_dead(vd) || 3466185029Spjd !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 3467185029Spjd vd = NULL; 3468185029Spjd } 3469185029Spjd 3470268649Sdelphij if (hash_lock != NULL) 3471268649Sdelphij mutex_exit(hash_lock); 3472168404Spjd 3473251629Sdelphij /* 3474251629Sdelphij * At this point, we have a level 1 cache miss. Try again in 3475251629Sdelphij * L2ARC if possible. 3476251629Sdelphij */ 3477168404Spjd ASSERT3U(hdr->b_size, ==, size); 3478219089Spjd DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 3479268657Sdelphij uint64_t, size, zbookmark_phys_t *, zb); 3480168404Spjd ARCSTAT_BUMP(arcstat_misses); 3481277586Sdelphij ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_FLAG_PREFETCH), 3482168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3483168404Spjd data, metadata, misses); 3484228392Spjd#ifdef _KERNEL 3485228392Spjd curthread->td_ru.ru_inblock++; 3486228392Spjd#endif 3487168404Spjd 3488208373Smm if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 3489185029Spjd /* 3490185029Spjd * Read from the L2ARC if the following are true: 3491185029Spjd * 1. The L2ARC vdev was previously cached. 3492185029Spjd * 2. This buffer still has L2ARC metadata. 3493185029Spjd * 3. This buffer isn't currently writing to the L2ARC. 3494185029Spjd * 4. The L2ARC entry wasn't evicted, which may 3495185029Spjd * also have invalidated the vdev. 3496208373Smm * 5. This isn't prefetch and l2arc_noprefetch is set. 3497185029Spjd */ 3498185029Spjd if (hdr->b_l2hdr != NULL && 3499208373Smm !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 3500208373Smm !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 3501185029Spjd l2arc_read_callback_t *cb; 3502185029Spjd 3503185029Spjd DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 3504185029Spjd ARCSTAT_BUMP(arcstat_l2_hits); 3505185029Spjd 3506185029Spjd cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 3507185029Spjd KM_SLEEP); 3508185029Spjd cb->l2rcb_buf = buf; 3509185029Spjd cb->l2rcb_spa = spa; 3510185029Spjd cb->l2rcb_bp = *bp; 3511185029Spjd cb->l2rcb_zb = *zb; 3512185029Spjd cb->l2rcb_flags = zio_flags; 3513258566Savg cb->l2rcb_compress = b_compress; 3514185029Spjd 3515247187Smm ASSERT(addr >= VDEV_LABEL_START_SIZE && 3516247187Smm addr + size < vd->vdev_psize - 3517247187Smm VDEV_LABEL_END_SIZE); 3518247187Smm 3519185029Spjd /* 3520185029Spjd * l2arc read. The SCL_L2ARC lock will be 3521185029Spjd * released by l2arc_read_done(). 3522251478Sdelphij * Issue a null zio if the underlying buffer 3523251478Sdelphij * was squashed to zero size by compression. 3524185029Spjd */ 3525258566Savg if (b_compress == ZIO_COMPRESS_EMPTY) { 3526251478Sdelphij rzio = zio_null(pio, spa, vd, 3527251478Sdelphij l2arc_read_done, cb, 3528251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 3529251478Sdelphij ZIO_FLAG_CANFAIL | 3530251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 3531251478Sdelphij ZIO_FLAG_DONT_RETRY); 3532251478Sdelphij } else { 3533251478Sdelphij rzio = zio_read_phys(pio, vd, addr, 3534258566Savg b_asize, buf->b_data, 3535258566Savg ZIO_CHECKSUM_OFF, 3536251478Sdelphij l2arc_read_done, cb, priority, 3537251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 3538251478Sdelphij ZIO_FLAG_CANFAIL | 3539251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 3540251478Sdelphij ZIO_FLAG_DONT_RETRY, B_FALSE); 3541251478Sdelphij } 3542185029Spjd DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 3543185029Spjd zio_t *, rzio); 3544258566Savg ARCSTAT_INCR(arcstat_l2_read_bytes, b_asize); 3545185029Spjd 3546277586Sdelphij if (*arc_flags & ARC_FLAG_NOWAIT) { 3547185029Spjd zio_nowait(rzio); 3548185029Spjd return (0); 3549185029Spjd } 3550185029Spjd 3551277586Sdelphij ASSERT(*arc_flags & ARC_FLAG_WAIT); 3552185029Spjd if (zio_wait(rzio) == 0) 3553185029Spjd return (0); 3554185029Spjd 3555185029Spjd /* l2arc read error; goto zio_read() */ 3556185029Spjd } else { 3557185029Spjd DTRACE_PROBE1(l2arc__miss, 3558185029Spjd arc_buf_hdr_t *, hdr); 3559185029Spjd ARCSTAT_BUMP(arcstat_l2_misses); 3560185029Spjd if (HDR_L2_WRITING(hdr)) 3561185029Spjd ARCSTAT_BUMP(arcstat_l2_rw_clash); 3562185029Spjd spa_config_exit(spa, SCL_L2ARC, vd); 3563185029Spjd } 3564208373Smm } else { 3565208373Smm if (vd != NULL) 3566208373Smm spa_config_exit(spa, SCL_L2ARC, vd); 3567208373Smm if (l2arc_ndev != 0) { 3568208373Smm DTRACE_PROBE1(l2arc__miss, 3569208373Smm arc_buf_hdr_t *, hdr); 3570208373Smm ARCSTAT_BUMP(arcstat_l2_misses); 3571208373Smm } 3572185029Spjd } 3573185029Spjd 3574168404Spjd rzio = zio_read(pio, spa, bp, buf->b_data, size, 3575185029Spjd arc_read_done, buf, priority, zio_flags, zb); 3576168404Spjd 3577277586Sdelphij if (*arc_flags & ARC_FLAG_WAIT) 3578168404Spjd return (zio_wait(rzio)); 3579168404Spjd 3580277586Sdelphij ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 3581168404Spjd zio_nowait(rzio); 3582168404Spjd } 3583168404Spjd return (0); 3584168404Spjd} 3585168404Spjd 3586168404Spjdvoid 3587168404Spjdarc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 3588168404Spjd{ 3589168404Spjd ASSERT(buf->b_hdr != NULL); 3590168404Spjd ASSERT(buf->b_hdr->b_state != arc_anon); 3591168404Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 3592219089Spjd ASSERT(buf->b_efunc == NULL); 3593219089Spjd ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 3594219089Spjd 3595168404Spjd buf->b_efunc = func; 3596168404Spjd buf->b_private = private; 3597168404Spjd} 3598168404Spjd 3599168404Spjd/* 3600251520Sdelphij * Notify the arc that a block was freed, and thus will never be used again. 3601251520Sdelphij */ 3602251520Sdelphijvoid 3603251520Sdelphijarc_freed(spa_t *spa, const blkptr_t *bp) 3604251520Sdelphij{ 3605251520Sdelphij arc_buf_hdr_t *hdr; 3606251520Sdelphij kmutex_t *hash_lock; 3607251520Sdelphij uint64_t guid = spa_load_guid(spa); 3608251520Sdelphij 3609268649Sdelphij ASSERT(!BP_IS_EMBEDDED(bp)); 3610268649Sdelphij 3611268649Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 3612251520Sdelphij if (hdr == NULL) 3613251520Sdelphij return; 3614251520Sdelphij if (HDR_BUF_AVAILABLE(hdr)) { 3615251520Sdelphij arc_buf_t *buf = hdr->b_buf; 3616251520Sdelphij add_reference(hdr, hash_lock, FTAG); 3617277586Sdelphij hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 3618251520Sdelphij mutex_exit(hash_lock); 3619251520Sdelphij 3620251520Sdelphij arc_release(buf, FTAG); 3621251520Sdelphij (void) arc_buf_remove_ref(buf, FTAG); 3622251520Sdelphij } else { 3623251520Sdelphij mutex_exit(hash_lock); 3624251520Sdelphij } 3625251520Sdelphij 3626251520Sdelphij} 3627251520Sdelphij 3628251520Sdelphij/* 3629269417Sdelphij * Clear the user eviction callback set by arc_set_callback(), first calling 3630269417Sdelphij * it if it exists. Because the presence of a callback keeps an arc_buf cached 3631269417Sdelphij * clearing the callback may result in the arc_buf being destroyed. However, 3632269417Sdelphij * it will not result in the *last* arc_buf being destroyed, hence the data 3633269417Sdelphij * will remain cached in the ARC. We make a copy of the arc buffer here so 3634269417Sdelphij * that we can process the callback without holding any locks. 3635269417Sdelphij * 3636269417Sdelphij * It's possible that the callback is already in the process of being cleared 3637269417Sdelphij * by another thread. In this case we can not clear the callback. 3638269417Sdelphij * 3639269417Sdelphij * Returns B_TRUE if the callback was successfully called and cleared. 3640168404Spjd */ 3641269417Sdelphijboolean_t 3642269417Sdelphijarc_clear_callback(arc_buf_t *buf) 3643168404Spjd{ 3644168404Spjd arc_buf_hdr_t *hdr; 3645168404Spjd kmutex_t *hash_lock; 3646269417Sdelphij arc_evict_func_t *efunc = buf->b_efunc; 3647269417Sdelphij void *private = buf->b_private; 3648205231Skmacy list_t *list, *evicted_list; 3649205231Skmacy kmutex_t *lock, *evicted_lock; 3650206796Spjd 3651219089Spjd mutex_enter(&buf->b_evict_lock); 3652168404Spjd hdr = buf->b_hdr; 3653168404Spjd if (hdr == NULL) { 3654168404Spjd /* 3655168404Spjd * We are in arc_do_user_evicts(). 3656168404Spjd */ 3657168404Spjd ASSERT(buf->b_data == NULL); 3658219089Spjd mutex_exit(&buf->b_evict_lock); 3659269417Sdelphij return (B_FALSE); 3660185029Spjd } else if (buf->b_data == NULL) { 3661185029Spjd /* 3662185029Spjd * We are on the eviction list; process this buffer now 3663185029Spjd * but let arc_do_user_evicts() do the reaping. 3664185029Spjd */ 3665185029Spjd buf->b_efunc = NULL; 3666219089Spjd mutex_exit(&buf->b_evict_lock); 3667269417Sdelphij VERIFY0(efunc(private)); 3668269417Sdelphij return (B_TRUE); 3669168404Spjd } 3670168404Spjd hash_lock = HDR_LOCK(hdr); 3671168404Spjd mutex_enter(hash_lock); 3672219089Spjd hdr = buf->b_hdr; 3673219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3674168404Spjd 3675168404Spjd ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 3676168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3677168404Spjd 3678269417Sdelphij buf->b_efunc = NULL; 3679269417Sdelphij buf->b_private = NULL; 3680168404Spjd 3681269417Sdelphij if (hdr->b_datacnt > 1) { 3682269417Sdelphij mutex_exit(&buf->b_evict_lock); 3683269417Sdelphij arc_buf_destroy(buf, FALSE, TRUE); 3684269417Sdelphij } else { 3685269417Sdelphij ASSERT(buf == hdr->b_buf); 3686277586Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 3687269417Sdelphij mutex_exit(&buf->b_evict_lock); 3688269417Sdelphij } 3689168404Spjd 3690168404Spjd mutex_exit(hash_lock); 3691269417Sdelphij VERIFY0(efunc(private)); 3692269417Sdelphij return (B_TRUE); 3693168404Spjd} 3694168404Spjd 3695168404Spjd/* 3696251629Sdelphij * Release this buffer from the cache, making it an anonymous buffer. This 3697251629Sdelphij * must be done after a read and prior to modifying the buffer contents. 3698168404Spjd * If the buffer has more than one reference, we must make 3699185029Spjd * a new hdr for the buffer. 3700168404Spjd */ 3701168404Spjdvoid 3702168404Spjdarc_release(arc_buf_t *buf, void *tag) 3703168404Spjd{ 3704185029Spjd arc_buf_hdr_t *hdr; 3705219089Spjd kmutex_t *hash_lock = NULL; 3706185029Spjd l2arc_buf_hdr_t *l2hdr; 3707185029Spjd uint64_t buf_size; 3708168404Spjd 3709219089Spjd /* 3710219089Spjd * It would be nice to assert that if it's DMU metadata (level > 3711219089Spjd * 0 || it's the dnode file), then it must be syncing context. 3712219089Spjd * But we don't know that information at this level. 3713219089Spjd */ 3714219089Spjd 3715219089Spjd mutex_enter(&buf->b_evict_lock); 3716185029Spjd hdr = buf->b_hdr; 3717185029Spjd 3718168404Spjd /* this buffer is not on any list */ 3719168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) > 0); 3720168404Spjd 3721168404Spjd if (hdr->b_state == arc_anon) { 3722168404Spjd /* this buffer is already released */ 3723168404Spjd ASSERT(buf->b_efunc == NULL); 3724208373Smm } else { 3725208373Smm hash_lock = HDR_LOCK(hdr); 3726208373Smm mutex_enter(hash_lock); 3727219089Spjd hdr = buf->b_hdr; 3728219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3729168404Spjd } 3730168404Spjd 3731185029Spjd l2hdr = hdr->b_l2hdr; 3732185029Spjd if (l2hdr) { 3733185029Spjd mutex_enter(&l2arc_buflist_mtx); 3734275492Sdelphij arc_buf_l2_cdata_free(hdr); 3735185029Spjd hdr->b_l2hdr = NULL; 3736258565Savg list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 3737185029Spjd } 3738247187Smm buf_size = hdr->b_size; 3739185029Spjd 3740168404Spjd /* 3741168404Spjd * Do we have more than one buf? 3742168404Spjd */ 3743185029Spjd if (hdr->b_datacnt > 1) { 3744168404Spjd arc_buf_hdr_t *nhdr; 3745168404Spjd arc_buf_t **bufp; 3746168404Spjd uint64_t blksz = hdr->b_size; 3747209962Smm uint64_t spa = hdr->b_spa; 3748168404Spjd arc_buf_contents_t type = hdr->b_type; 3749185029Spjd uint32_t flags = hdr->b_flags; 3750168404Spjd 3751185029Spjd ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3752168404Spjd /* 3753219089Spjd * Pull the data off of this hdr and attach it to 3754219089Spjd * a new anonymous hdr. 3755168404Spjd */ 3756168404Spjd (void) remove_reference(hdr, hash_lock, tag); 3757168404Spjd bufp = &hdr->b_buf; 3758168404Spjd while (*bufp != buf) 3759168404Spjd bufp = &(*bufp)->b_next; 3760219089Spjd *bufp = buf->b_next; 3761168404Spjd buf->b_next = NULL; 3762168404Spjd 3763168404Spjd ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 3764168404Spjd atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 3765168404Spjd if (refcount_is_zero(&hdr->b_refcnt)) { 3766185029Spjd uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 3767185029Spjd ASSERT3U(*size, >=, hdr->b_size); 3768185029Spjd atomic_add_64(size, -hdr->b_size); 3769168404Spjd } 3770242845Sdelphij 3771242845Sdelphij /* 3772242845Sdelphij * We're releasing a duplicate user data buffer, update 3773242845Sdelphij * our statistics accordingly. 3774242845Sdelphij */ 3775242845Sdelphij if (hdr->b_type == ARC_BUFC_DATA) { 3776242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 3777242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, 3778242845Sdelphij -hdr->b_size); 3779242845Sdelphij } 3780168404Spjd hdr->b_datacnt -= 1; 3781168404Spjd arc_cksum_verify(buf); 3782240133Smm#ifdef illumos 3783240133Smm arc_buf_unwatch(buf); 3784240133Smm#endif /* illumos */ 3785168404Spjd 3786168404Spjd mutex_exit(hash_lock); 3787168404Spjd 3788185029Spjd nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3789168404Spjd nhdr->b_size = blksz; 3790168404Spjd nhdr->b_spa = spa; 3791168404Spjd nhdr->b_type = type; 3792168404Spjd nhdr->b_buf = buf; 3793168404Spjd nhdr->b_state = arc_anon; 3794168404Spjd nhdr->b_arc_access = 0; 3795277586Sdelphij nhdr->b_flags = flags & ARC_FLAG_L2_WRITING; 3796185029Spjd nhdr->b_l2hdr = NULL; 3797168404Spjd nhdr->b_datacnt = 1; 3798168404Spjd nhdr->b_freeze_cksum = NULL; 3799168404Spjd (void) refcount_add(&nhdr->b_refcnt, tag); 3800168404Spjd buf->b_hdr = nhdr; 3801219089Spjd mutex_exit(&buf->b_evict_lock); 3802168404Spjd atomic_add_64(&arc_anon->arcs_size, blksz); 3803168404Spjd } else { 3804219089Spjd mutex_exit(&buf->b_evict_lock); 3805168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3806168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 3807168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3808219089Spjd if (hdr->b_state != arc_anon) 3809219089Spjd arc_change_state(arc_anon, hdr, hash_lock); 3810168404Spjd hdr->b_arc_access = 0; 3811219089Spjd if (hash_lock) 3812219089Spjd mutex_exit(hash_lock); 3813185029Spjd 3814219089Spjd buf_discard_identity(hdr); 3815168404Spjd arc_buf_thaw(buf); 3816168404Spjd } 3817168404Spjd buf->b_efunc = NULL; 3818168404Spjd buf->b_private = NULL; 3819185029Spjd 3820185029Spjd if (l2hdr) { 3821251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); 3822268654Sdelphij vdev_space_update(l2hdr->b_dev->l2ad_vdev, 3823268654Sdelphij -l2hdr->b_asize, 0, 0); 3824248572Ssmh trim_map_free(l2hdr->b_dev->l2ad_vdev, l2hdr->b_daddr, 3825248574Ssmh hdr->b_size, 0); 3826185029Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 3827185029Spjd ARCSTAT_INCR(arcstat_l2_size, -buf_size); 3828185029Spjd mutex_exit(&l2arc_buflist_mtx); 3829185029Spjd } 3830168404Spjd} 3831168404Spjd 3832168404Spjdint 3833168404Spjdarc_released(arc_buf_t *buf) 3834168404Spjd{ 3835185029Spjd int released; 3836185029Spjd 3837219089Spjd mutex_enter(&buf->b_evict_lock); 3838185029Spjd released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 3839219089Spjd mutex_exit(&buf->b_evict_lock); 3840185029Spjd return (released); 3841168404Spjd} 3842168404Spjd 3843168404Spjd#ifdef ZFS_DEBUG 3844168404Spjdint 3845168404Spjdarc_referenced(arc_buf_t *buf) 3846168404Spjd{ 3847185029Spjd int referenced; 3848185029Spjd 3849219089Spjd mutex_enter(&buf->b_evict_lock); 3850185029Spjd referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 3851219089Spjd mutex_exit(&buf->b_evict_lock); 3852185029Spjd return (referenced); 3853168404Spjd} 3854168404Spjd#endif 3855168404Spjd 3856168404Spjdstatic void 3857168404Spjdarc_write_ready(zio_t *zio) 3858168404Spjd{ 3859168404Spjd arc_write_callback_t *callback = zio->io_private; 3860168404Spjd arc_buf_t *buf = callback->awcb_buf; 3861185029Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3862168404Spjd 3863185029Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 3864185029Spjd callback->awcb_ready(zio, buf, callback->awcb_private); 3865185029Spjd 3866185029Spjd /* 3867185029Spjd * If the IO is already in progress, then this is a re-write 3868185029Spjd * attempt, so we need to thaw and re-compute the cksum. 3869185029Spjd * It is the responsibility of the callback to handle the 3870185029Spjd * accounting for any re-write attempt. 3871185029Spjd */ 3872185029Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3873185029Spjd mutex_enter(&hdr->b_freeze_lock); 3874185029Spjd if (hdr->b_freeze_cksum != NULL) { 3875185029Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 3876185029Spjd hdr->b_freeze_cksum = NULL; 3877185029Spjd } 3878185029Spjd mutex_exit(&hdr->b_freeze_lock); 3879168404Spjd } 3880185029Spjd arc_cksum_compute(buf, B_FALSE); 3881277586Sdelphij hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; 3882168404Spjd} 3883168404Spjd 3884260763Savg/* 3885260763Savg * The SPA calls this callback for each physical write that happens on behalf 3886260763Savg * of a logical write. See the comment in dbuf_write_physdone() for details. 3887260763Savg */ 3888168404Spjdstatic void 3889260763Savgarc_write_physdone(zio_t *zio) 3890260763Savg{ 3891260763Savg arc_write_callback_t *cb = zio->io_private; 3892260763Savg if (cb->awcb_physdone != NULL) 3893260763Savg cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 3894260763Savg} 3895260763Savg 3896260763Savgstatic void 3897168404Spjdarc_write_done(zio_t *zio) 3898168404Spjd{ 3899168404Spjd arc_write_callback_t *callback = zio->io_private; 3900168404Spjd arc_buf_t *buf = callback->awcb_buf; 3901168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3902168404Spjd 3903219089Spjd ASSERT(hdr->b_acb == NULL); 3904168404Spjd 3905219089Spjd if (zio->io_error == 0) { 3906268649Sdelphij if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 3907263397Sdelphij buf_discard_identity(hdr); 3908263397Sdelphij } else { 3909263397Sdelphij hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3910263397Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 3911263397Sdelphij hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 3912263397Sdelphij } 3913219089Spjd } else { 3914219089Spjd ASSERT(BUF_EMPTY(hdr)); 3915219089Spjd } 3916219089Spjd 3917168404Spjd /* 3918268649Sdelphij * If the block to be written was all-zero or compressed enough to be 3919268649Sdelphij * embedded in the BP, no write was performed so there will be no 3920268649Sdelphij * dva/birth/checksum. The buffer must therefore remain anonymous 3921268649Sdelphij * (and uncached). 3922168404Spjd */ 3923168404Spjd if (!BUF_EMPTY(hdr)) { 3924168404Spjd arc_buf_hdr_t *exists; 3925168404Spjd kmutex_t *hash_lock; 3926168404Spjd 3927219089Spjd ASSERT(zio->io_error == 0); 3928219089Spjd 3929168404Spjd arc_cksum_verify(buf); 3930168404Spjd 3931168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 3932168404Spjd if (exists) { 3933168404Spjd /* 3934168404Spjd * This can only happen if we overwrite for 3935168404Spjd * sync-to-convergence, because we remove 3936168404Spjd * buffers from the hash table when we arc_free(). 3937168404Spjd */ 3938219089Spjd if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 3939219089Spjd if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3940219089Spjd panic("bad overwrite, hdr=%p exists=%p", 3941219089Spjd (void *)hdr, (void *)exists); 3942219089Spjd ASSERT(refcount_is_zero(&exists->b_refcnt)); 3943219089Spjd arc_change_state(arc_anon, exists, hash_lock); 3944219089Spjd mutex_exit(hash_lock); 3945219089Spjd arc_hdr_destroy(exists); 3946219089Spjd exists = buf_hash_insert(hdr, &hash_lock); 3947219089Spjd ASSERT3P(exists, ==, NULL); 3948243524Smm } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 3949243524Smm /* nopwrite */ 3950243524Smm ASSERT(zio->io_prop.zp_nopwrite); 3951243524Smm if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3952243524Smm panic("bad nopwrite, hdr=%p exists=%p", 3953243524Smm (void *)hdr, (void *)exists); 3954219089Spjd } else { 3955219089Spjd /* Dedup */ 3956219089Spjd ASSERT(hdr->b_datacnt == 1); 3957219089Spjd ASSERT(hdr->b_state == arc_anon); 3958219089Spjd ASSERT(BP_GET_DEDUP(zio->io_bp)); 3959219089Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 3960219089Spjd } 3961168404Spjd } 3962277586Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 3963185029Spjd /* if it's not anon, we are doing a scrub */ 3964219089Spjd if (!exists && hdr->b_state == arc_anon) 3965185029Spjd arc_access(hdr, hash_lock); 3966168404Spjd mutex_exit(hash_lock); 3967168404Spjd } else { 3968277586Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 3969168404Spjd } 3970168404Spjd 3971219089Spjd ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 3972219089Spjd callback->awcb_done(zio, buf, callback->awcb_private); 3973168404Spjd 3974168404Spjd kmem_free(callback, sizeof (arc_write_callback_t)); 3975168404Spjd} 3976168404Spjd 3977168404Spjdzio_t * 3978219089Spjdarc_write(zio_t *pio, spa_t *spa, uint64_t txg, 3979251478Sdelphij blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress, 3980260763Savg const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone, 3981260763Savg arc_done_func_t *done, void *private, zio_priority_t priority, 3982268657Sdelphij int zio_flags, const zbookmark_phys_t *zb) 3983168404Spjd{ 3984168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3985168404Spjd arc_write_callback_t *callback; 3986185029Spjd zio_t *zio; 3987168404Spjd 3988185029Spjd ASSERT(ready != NULL); 3989219089Spjd ASSERT(done != NULL); 3990168404Spjd ASSERT(!HDR_IO_ERROR(hdr)); 3991277586Sdelphij ASSERT((hdr->b_flags & ARC_FLAG_IO_IN_PROGRESS) == 0); 3992219089Spjd ASSERT(hdr->b_acb == NULL); 3993185029Spjd if (l2arc) 3994277586Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 3995251478Sdelphij if (l2arc_compress) 3996277586Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 3997168404Spjd callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 3998168404Spjd callback->awcb_ready = ready; 3999260763Savg callback->awcb_physdone = physdone; 4000168404Spjd callback->awcb_done = done; 4001168404Spjd callback->awcb_private = private; 4002168404Spjd callback->awcb_buf = buf; 4003168404Spjd 4004219089Spjd zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 4005260763Savg arc_write_ready, arc_write_physdone, arc_write_done, callback, 4006260763Savg priority, zio_flags, zb); 4007185029Spjd 4008168404Spjd return (zio); 4009168404Spjd} 4010168404Spjd 4011185029Spjdstatic int 4012260763Savgarc_memory_throttle(uint64_t reserve, uint64_t txg) 4013185029Spjd{ 4014185029Spjd#ifdef _KERNEL 4015272875Ssmh uint64_t available_memory = ptob(freemem); 4016185029Spjd static uint64_t page_load = 0; 4017185029Spjd static uint64_t last_txg = 0; 4018185029Spjd 4019272875Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 4020185029Spjd available_memory = 4021272875Ssmh MIN(available_memory, ptob(vmem_size(heap_arena, VMEM_FREE))); 4022185029Spjd#endif 4023260763Savg 4024272875Ssmh if (freemem > (uint64_t)physmem * arc_lotsfree_percent / 100) 4025185029Spjd return (0); 4026185029Spjd 4027185029Spjd if (txg > last_txg) { 4028185029Spjd last_txg = txg; 4029185029Spjd page_load = 0; 4030185029Spjd } 4031185029Spjd /* 4032185029Spjd * If we are in pageout, we know that memory is already tight, 4033185029Spjd * the arc is already going to be evicting, so we just want to 4034185029Spjd * continue to let page writes occur as quickly as possible. 4035185029Spjd */ 4036185029Spjd if (curproc == pageproc) { 4037272875Ssmh if (page_load > MAX(ptob(minfree), available_memory) / 4) 4038249195Smm return (SET_ERROR(ERESTART)); 4039185029Spjd /* Note: reserve is inflated, so we deflate */ 4040185029Spjd page_load += reserve / 8; 4041185029Spjd return (0); 4042185029Spjd } else if (page_load > 0 && arc_reclaim_needed()) { 4043185029Spjd /* memory is low, delay before restarting */ 4044185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 4045249195Smm return (SET_ERROR(EAGAIN)); 4046185029Spjd } 4047185029Spjd page_load = 0; 4048185029Spjd#endif 4049185029Spjd return (0); 4050185029Spjd} 4051185029Spjd 4052168404Spjdvoid 4053185029Spjdarc_tempreserve_clear(uint64_t reserve) 4054168404Spjd{ 4055185029Spjd atomic_add_64(&arc_tempreserve, -reserve); 4056168404Spjd ASSERT((int64_t)arc_tempreserve >= 0); 4057168404Spjd} 4058168404Spjd 4059168404Spjdint 4060185029Spjdarc_tempreserve_space(uint64_t reserve, uint64_t txg) 4061168404Spjd{ 4062185029Spjd int error; 4063209962Smm uint64_t anon_size; 4064185029Spjd 4065272875Ssmh if (reserve > arc_c/4 && !arc_no_grow) { 4066185029Spjd arc_c = MIN(arc_c_max, reserve * 4); 4067272875Ssmh DTRACE_PROBE1(arc__set_reserve, uint64_t, arc_c); 4068272875Ssmh } 4069185029Spjd if (reserve > arc_c) 4070249195Smm return (SET_ERROR(ENOMEM)); 4071168404Spjd 4072168404Spjd /* 4073209962Smm * Don't count loaned bufs as in flight dirty data to prevent long 4074209962Smm * network delays from blocking transactions that are ready to be 4075209962Smm * assigned to a txg. 4076209962Smm */ 4077209962Smm anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 4078209962Smm 4079209962Smm /* 4080185029Spjd * Writes will, almost always, require additional memory allocations 4081251631Sdelphij * in order to compress/encrypt/etc the data. We therefore need to 4082185029Spjd * make sure that there is sufficient available memory for this. 4083185029Spjd */ 4084260763Savg error = arc_memory_throttle(reserve, txg); 4085260763Savg if (error != 0) 4086185029Spjd return (error); 4087185029Spjd 4088185029Spjd /* 4089168404Spjd * Throttle writes when the amount of dirty data in the cache 4090168404Spjd * gets too large. We try to keep the cache less than half full 4091168404Spjd * of dirty blocks so that our sync times don't grow too large. 4092168404Spjd * Note: if two requests come in concurrently, we might let them 4093168404Spjd * both succeed, when one of them should fail. Not a huge deal. 4094168404Spjd */ 4095209962Smm 4096209962Smm if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 4097209962Smm anon_size > arc_c / 4) { 4098185029Spjd dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 4099185029Spjd "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 4100185029Spjd arc_tempreserve>>10, 4101185029Spjd arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 4102185029Spjd arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 4103185029Spjd reserve>>10, arc_c>>10); 4104249195Smm return (SET_ERROR(ERESTART)); 4105168404Spjd } 4106185029Spjd atomic_add_64(&arc_tempreserve, reserve); 4107168404Spjd return (0); 4108168404Spjd} 4109168404Spjd 4110168582Spjdstatic kmutex_t arc_lowmem_lock; 4111168404Spjd#ifdef _KERNEL 4112168566Spjdstatic eventhandler_tag arc_event_lowmem = NULL; 4113168404Spjd 4114168404Spjdstatic void 4115168566Spjdarc_lowmem(void *arg __unused, int howto __unused) 4116168404Spjd{ 4117168404Spjd 4118168566Spjd /* Serialize access via arc_lowmem_lock. */ 4119168566Spjd mutex_enter(&arc_lowmem_lock); 4120219089Spjd mutex_enter(&arc_reclaim_thr_lock); 4121185029Spjd needfree = 1; 4122272875Ssmh DTRACE_PROBE(arc__needfree); 4123168404Spjd cv_signal(&arc_reclaim_thr_cv); 4124241773Savg 4125241773Savg /* 4126241773Savg * It is unsafe to block here in arbitrary threads, because we can come 4127241773Savg * here from ARC itself and may hold ARC locks and thus risk a deadlock 4128241773Savg * with ARC reclaim thread. 4129241773Savg */ 4130241773Savg if (curproc == pageproc) { 4131241773Savg while (needfree) 4132241773Savg msleep(&needfree, &arc_reclaim_thr_lock, 0, "zfs:lowmem", 0); 4133241773Savg } 4134219089Spjd mutex_exit(&arc_reclaim_thr_lock); 4135168566Spjd mutex_exit(&arc_lowmem_lock); 4136168404Spjd} 4137168404Spjd#endif 4138168404Spjd 4139168404Spjdvoid 4140168404Spjdarc_init(void) 4141168404Spjd{ 4142219089Spjd int i, prefetch_tunable_set = 0; 4143205231Skmacy 4144168404Spjd mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 4145168404Spjd cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 4146168566Spjd mutex_init(&arc_lowmem_lock, NULL, MUTEX_DEFAULT, NULL); 4147168404Spjd 4148168404Spjd /* Convert seconds to clock ticks */ 4149168404Spjd arc_min_prefetch_lifespan = 1 * hz; 4150168404Spjd 4151168404Spjd /* Start out with 1/8 of all memory */ 4152168566Spjd arc_c = kmem_size() / 8; 4153219089Spjd 4154219089Spjd#ifdef sun 4155192360Skmacy#ifdef _KERNEL 4156192360Skmacy /* 4157192360Skmacy * On architectures where the physical memory can be larger 4158192360Skmacy * than the addressable space (intel in 32-bit mode), we may 4159192360Skmacy * need to limit the cache to 1/8 of VM size. 4160192360Skmacy */ 4161192360Skmacy arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 4162192360Skmacy#endif 4163219089Spjd#endif /* sun */ 4164168566Spjd /* set min cache to 1/32 of all memory, or 16MB, whichever is more */ 4165281104Smav arc_c_min = MAX(arc_c / 4, 16 << 20); 4166168566Spjd /* set max to 1/2 of all memory, or all but 1GB, whichever is more */ 4167281104Smav if (arc_c * 8 >= 1 << 30) 4168281104Smav arc_c_max = (arc_c * 8) - (1 << 30); 4169168404Spjd else 4170168404Spjd arc_c_max = arc_c_min; 4171175633Spjd arc_c_max = MAX(arc_c * 5, arc_c_max); 4172219089Spjd 4173168481Spjd#ifdef _KERNEL 4174168404Spjd /* 4175168404Spjd * Allow the tunables to override our calculations if they are 4176168566Spjd * reasonable (ie. over 16MB) 4177168404Spjd */ 4178281104Smav if (zfs_arc_max > 16 << 20 && zfs_arc_max < kmem_size()) 4179168404Spjd arc_c_max = zfs_arc_max; 4180281104Smav if (zfs_arc_min > 16 << 20 && zfs_arc_min <= arc_c_max) 4181168404Spjd arc_c_min = zfs_arc_min; 4182168481Spjd#endif 4183219089Spjd 4184168404Spjd arc_c = arc_c_max; 4185168404Spjd arc_p = (arc_c >> 1); 4186168404Spjd 4187185029Spjd /* limit meta-data to 1/4 of the arc capacity */ 4188185029Spjd arc_meta_limit = arc_c_max / 4; 4189185029Spjd 4190185029Spjd /* Allow the tunable to override if it is reasonable */ 4191185029Spjd if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 4192185029Spjd arc_meta_limit = zfs_arc_meta_limit; 4193185029Spjd 4194185029Spjd if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 4195185029Spjd arc_c_min = arc_meta_limit / 2; 4196185029Spjd 4197208373Smm if (zfs_arc_grow_retry > 0) 4198208373Smm arc_grow_retry = zfs_arc_grow_retry; 4199208373Smm 4200208373Smm if (zfs_arc_shrink_shift > 0) 4201208373Smm arc_shrink_shift = zfs_arc_shrink_shift; 4202208373Smm 4203208373Smm if (zfs_arc_p_min_shift > 0) 4204208373Smm arc_p_min_shift = zfs_arc_p_min_shift; 4205208373Smm 4206168404Spjd /* if kmem_flags are set, lets try to use less memory */ 4207168404Spjd if (kmem_debugging()) 4208168404Spjd arc_c = arc_c / 2; 4209168404Spjd if (arc_c < arc_c_min) 4210168404Spjd arc_c = arc_c_min; 4211168404Spjd 4212168473Spjd zfs_arc_min = arc_c_min; 4213168473Spjd zfs_arc_max = arc_c_max; 4214168473Spjd 4215168404Spjd arc_anon = &ARC_anon; 4216168404Spjd arc_mru = &ARC_mru; 4217168404Spjd arc_mru_ghost = &ARC_mru_ghost; 4218168404Spjd arc_mfu = &ARC_mfu; 4219168404Spjd arc_mfu_ghost = &ARC_mfu_ghost; 4220185029Spjd arc_l2c_only = &ARC_l2c_only; 4221168404Spjd arc_size = 0; 4222168404Spjd 4223205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 4224205231Skmacy mutex_init(&arc_anon->arcs_locks[i].arcs_lock, 4225205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4226205231Skmacy mutex_init(&arc_mru->arcs_locks[i].arcs_lock, 4227205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4228205231Skmacy mutex_init(&arc_mru_ghost->arcs_locks[i].arcs_lock, 4229205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4230205231Skmacy mutex_init(&arc_mfu->arcs_locks[i].arcs_lock, 4231205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4232205231Skmacy mutex_init(&arc_mfu_ghost->arcs_locks[i].arcs_lock, 4233205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4234205231Skmacy mutex_init(&arc_l2c_only->arcs_locks[i].arcs_lock, 4235205231Skmacy NULL, MUTEX_DEFAULT, NULL); 4236206796Spjd 4237205231Skmacy list_create(&arc_mru->arcs_lists[i], 4238205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4239205231Skmacy list_create(&arc_mru_ghost->arcs_lists[i], 4240205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4241205231Skmacy list_create(&arc_mfu->arcs_lists[i], 4242205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4243205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 4244205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4245205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 4246205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4247205231Skmacy list_create(&arc_l2c_only->arcs_lists[i], 4248205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 4249205231Skmacy } 4250168404Spjd 4251168404Spjd buf_init(); 4252168404Spjd 4253168404Spjd arc_thread_exit = 0; 4254168404Spjd arc_eviction_list = NULL; 4255168404Spjd mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 4256168404Spjd bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 4257168404Spjd 4258168404Spjd arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 4259168404Spjd sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 4260168404Spjd 4261168404Spjd if (arc_ksp != NULL) { 4262168404Spjd arc_ksp->ks_data = &arc_stats; 4263168404Spjd kstat_install(arc_ksp); 4264168404Spjd } 4265168404Spjd 4266168404Spjd (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 4267168404Spjd TS_RUN, minclsyspri); 4268168404Spjd 4269168404Spjd#ifdef _KERNEL 4270168566Spjd arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 4271168404Spjd EVENTHANDLER_PRI_FIRST); 4272168404Spjd#endif 4273168404Spjd 4274168404Spjd arc_dead = FALSE; 4275185029Spjd arc_warm = B_FALSE; 4276168566Spjd 4277260763Savg /* 4278260763Savg * Calculate maximum amount of dirty data per pool. 4279260763Savg * 4280260763Savg * If it has been set by /etc/system, take that. 4281260763Savg * Otherwise, use a percentage of physical memory defined by 4282260763Savg * zfs_dirty_data_max_percent (default 10%) with a cap at 4283260763Savg * zfs_dirty_data_max_max (default 4GB). 4284260763Savg */ 4285260763Savg if (zfs_dirty_data_max == 0) { 4286260763Savg zfs_dirty_data_max = ptob(physmem) * 4287260763Savg zfs_dirty_data_max_percent / 100; 4288260763Savg zfs_dirty_data_max = MIN(zfs_dirty_data_max, 4289260763Savg zfs_dirty_data_max_max); 4290260763Savg } 4291185029Spjd 4292168566Spjd#ifdef _KERNEL 4293194043Skmacy if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 4294193953Skmacy prefetch_tunable_set = 1; 4295206796Spjd 4296193878Skmacy#ifdef __i386__ 4297193953Skmacy if (prefetch_tunable_set == 0) { 4298196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 4299196863Strasz "-- to enable,\n"); 4300196863Strasz printf(" add \"vfs.zfs.prefetch_disable=0\" " 4301196863Strasz "to /boot/loader.conf.\n"); 4302219089Spjd zfs_prefetch_disable = 1; 4303193878Skmacy } 4304206796Spjd#else 4305193878Skmacy if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 4306193953Skmacy prefetch_tunable_set == 0) { 4307196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default if less " 4308196941Strasz "than 4GB of RAM is present;\n" 4309196863Strasz " to enable, add \"vfs.zfs.prefetch_disable=0\" " 4310196863Strasz "to /boot/loader.conf.\n"); 4311219089Spjd zfs_prefetch_disable = 1; 4312193878Skmacy } 4313206796Spjd#endif 4314175633Spjd /* Warn about ZFS memory and address space requirements. */ 4315168696Spjd if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 4316168987Sbmah printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 4317168987Sbmah "expect unstable behavior.\n"); 4318175633Spjd } 4319175633Spjd if (kmem_size() < 512 * (1 << 20)) { 4320173419Spjd printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 4321168987Sbmah "expect unstable behavior.\n"); 4322185029Spjd printf(" Consider tuning vm.kmem_size and " 4323173419Spjd "vm.kmem_size_max\n"); 4324185029Spjd printf(" in /boot/loader.conf.\n"); 4325168566Spjd } 4326168566Spjd#endif 4327168404Spjd} 4328168404Spjd 4329168404Spjdvoid 4330168404Spjdarc_fini(void) 4331168404Spjd{ 4332205231Skmacy int i; 4333206796Spjd 4334168404Spjd mutex_enter(&arc_reclaim_thr_lock); 4335168404Spjd arc_thread_exit = 1; 4336168404Spjd cv_signal(&arc_reclaim_thr_cv); 4337168404Spjd while (arc_thread_exit != 0) 4338168404Spjd cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 4339168404Spjd mutex_exit(&arc_reclaim_thr_lock); 4340168404Spjd 4341185029Spjd arc_flush(NULL); 4342168404Spjd 4343168404Spjd arc_dead = TRUE; 4344168404Spjd 4345168404Spjd if (arc_ksp != NULL) { 4346168404Spjd kstat_delete(arc_ksp); 4347168404Spjd arc_ksp = NULL; 4348168404Spjd } 4349168404Spjd 4350168404Spjd mutex_destroy(&arc_eviction_mtx); 4351168404Spjd mutex_destroy(&arc_reclaim_thr_lock); 4352168404Spjd cv_destroy(&arc_reclaim_thr_cv); 4353168404Spjd 4354205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 4355205231Skmacy list_destroy(&arc_mru->arcs_lists[i]); 4356205231Skmacy list_destroy(&arc_mru_ghost->arcs_lists[i]); 4357205231Skmacy list_destroy(&arc_mfu->arcs_lists[i]); 4358205231Skmacy list_destroy(&arc_mfu_ghost->arcs_lists[i]); 4359206795Spjd list_destroy(&arc_l2c_only->arcs_lists[i]); 4360168404Spjd 4361205231Skmacy mutex_destroy(&arc_anon->arcs_locks[i].arcs_lock); 4362205231Skmacy mutex_destroy(&arc_mru->arcs_locks[i].arcs_lock); 4363205231Skmacy mutex_destroy(&arc_mru_ghost->arcs_locks[i].arcs_lock); 4364205231Skmacy mutex_destroy(&arc_mfu->arcs_locks[i].arcs_lock); 4365205231Skmacy mutex_destroy(&arc_mfu_ghost->arcs_locks[i].arcs_lock); 4366206795Spjd mutex_destroy(&arc_l2c_only->arcs_locks[i].arcs_lock); 4367205231Skmacy } 4368206796Spjd 4369168404Spjd buf_fini(); 4370168404Spjd 4371209962Smm ASSERT(arc_loaned_bytes == 0); 4372209962Smm 4373168582Spjd mutex_destroy(&arc_lowmem_lock); 4374168404Spjd#ifdef _KERNEL 4375168566Spjd if (arc_event_lowmem != NULL) 4376168566Spjd EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 4377168404Spjd#endif 4378168404Spjd} 4379185029Spjd 4380185029Spjd/* 4381185029Spjd * Level 2 ARC 4382185029Spjd * 4383185029Spjd * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 4384185029Spjd * It uses dedicated storage devices to hold cached data, which are populated 4385185029Spjd * using large infrequent writes. The main role of this cache is to boost 4386185029Spjd * the performance of random read workloads. The intended L2ARC devices 4387185029Spjd * include short-stroked disks, solid state disks, and other media with 4388185029Spjd * substantially faster read latency than disk. 4389185029Spjd * 4390185029Spjd * +-----------------------+ 4391185029Spjd * | ARC | 4392185029Spjd * +-----------------------+ 4393185029Spjd * | ^ ^ 4394185029Spjd * | | | 4395185029Spjd * l2arc_feed_thread() arc_read() 4396185029Spjd * | | | 4397185029Spjd * | l2arc read | 4398185029Spjd * V | | 4399185029Spjd * +---------------+ | 4400185029Spjd * | L2ARC | | 4401185029Spjd * +---------------+ | 4402185029Spjd * | ^ | 4403185029Spjd * l2arc_write() | | 4404185029Spjd * | | | 4405185029Spjd * V | | 4406185029Spjd * +-------+ +-------+ 4407185029Spjd * | vdev | | vdev | 4408185029Spjd * | cache | | cache | 4409185029Spjd * +-------+ +-------+ 4410185029Spjd * +=========+ .-----. 4411185029Spjd * : L2ARC : |-_____-| 4412185029Spjd * : devices : | Disks | 4413185029Spjd * +=========+ `-_____-' 4414185029Spjd * 4415185029Spjd * Read requests are satisfied from the following sources, in order: 4416185029Spjd * 4417185029Spjd * 1) ARC 4418185029Spjd * 2) vdev cache of L2ARC devices 4419185029Spjd * 3) L2ARC devices 4420185029Spjd * 4) vdev cache of disks 4421185029Spjd * 5) disks 4422185029Spjd * 4423185029Spjd * Some L2ARC device types exhibit extremely slow write performance. 4424185029Spjd * To accommodate for this there are some significant differences between 4425185029Spjd * the L2ARC and traditional cache design: 4426185029Spjd * 4427185029Spjd * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 4428185029Spjd * the ARC behave as usual, freeing buffers and placing headers on ghost 4429185029Spjd * lists. The ARC does not send buffers to the L2ARC during eviction as 4430185029Spjd * this would add inflated write latencies for all ARC memory pressure. 4431185029Spjd * 4432185029Spjd * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 4433185029Spjd * It does this by periodically scanning buffers from the eviction-end of 4434185029Spjd * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 4435251478Sdelphij * not already there. It scans until a headroom of buffers is satisfied, 4436251478Sdelphij * which itself is a buffer for ARC eviction. If a compressible buffer is 4437251478Sdelphij * found during scanning and selected for writing to an L2ARC device, we 4438251478Sdelphij * temporarily boost scanning headroom during the next scan cycle to make 4439251478Sdelphij * sure we adapt to compression effects (which might significantly reduce 4440251478Sdelphij * the data volume we write to L2ARC). The thread that does this is 4441185029Spjd * l2arc_feed_thread(), illustrated below; example sizes are included to 4442185029Spjd * provide a better sense of ratio than this diagram: 4443185029Spjd * 4444185029Spjd * head --> tail 4445185029Spjd * +---------------------+----------+ 4446185029Spjd * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 4447185029Spjd * +---------------------+----------+ | o L2ARC eligible 4448185029Spjd * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 4449185029Spjd * +---------------------+----------+ | 4450185029Spjd * 15.9 Gbytes ^ 32 Mbytes | 4451185029Spjd * headroom | 4452185029Spjd * l2arc_feed_thread() 4453185029Spjd * | 4454185029Spjd * l2arc write hand <--[oooo]--' 4455185029Spjd * | 8 Mbyte 4456185029Spjd * | write max 4457185029Spjd * V 4458185029Spjd * +==============================+ 4459185029Spjd * L2ARC dev |####|#|###|###| |####| ... | 4460185029Spjd * +==============================+ 4461185029Spjd * 32 Gbytes 4462185029Spjd * 4463185029Spjd * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 4464185029Spjd * evicted, then the L2ARC has cached a buffer much sooner than it probably 4465185029Spjd * needed to, potentially wasting L2ARC device bandwidth and storage. It is 4466185029Spjd * safe to say that this is an uncommon case, since buffers at the end of 4467185029Spjd * the ARC lists have moved there due to inactivity. 4468185029Spjd * 4469185029Spjd * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 4470185029Spjd * then the L2ARC simply misses copying some buffers. This serves as a 4471185029Spjd * pressure valve to prevent heavy read workloads from both stalling the ARC 4472185029Spjd * with waits and clogging the L2ARC with writes. This also helps prevent 4473185029Spjd * the potential for the L2ARC to churn if it attempts to cache content too 4474185029Spjd * quickly, such as during backups of the entire pool. 4475185029Spjd * 4476185029Spjd * 5. After system boot and before the ARC has filled main memory, there are 4477185029Spjd * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 4478185029Spjd * lists can remain mostly static. Instead of searching from tail of these 4479185029Spjd * lists as pictured, the l2arc_feed_thread() will search from the list heads 4480185029Spjd * for eligible buffers, greatly increasing its chance of finding them. 4481185029Spjd * 4482185029Spjd * The L2ARC device write speed is also boosted during this time so that 4483185029Spjd * the L2ARC warms up faster. Since there have been no ARC evictions yet, 4484185029Spjd * there are no L2ARC reads, and no fear of degrading read performance 4485185029Spjd * through increased writes. 4486185029Spjd * 4487185029Spjd * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 4488185029Spjd * the vdev queue can aggregate them into larger and fewer writes. Each 4489185029Spjd * device is written to in a rotor fashion, sweeping writes through 4490185029Spjd * available space then repeating. 4491185029Spjd * 4492185029Spjd * 7. The L2ARC does not store dirty content. It never needs to flush 4493185029Spjd * write buffers back to disk based storage. 4494185029Spjd * 4495185029Spjd * 8. If an ARC buffer is written (and dirtied) which also exists in the 4496185029Spjd * L2ARC, the now stale L2ARC buffer is immediately dropped. 4497185029Spjd * 4498185029Spjd * The performance of the L2ARC can be tweaked by a number of tunables, which 4499185029Spjd * may be necessary for different workloads: 4500185029Spjd * 4501185029Spjd * l2arc_write_max max write bytes per interval 4502185029Spjd * l2arc_write_boost extra write bytes during device warmup 4503185029Spjd * l2arc_noprefetch skip caching prefetched buffers 4504185029Spjd * l2arc_headroom number of max device writes to precache 4505251478Sdelphij * l2arc_headroom_boost when we find compressed buffers during ARC 4506251478Sdelphij * scanning, we multiply headroom by this 4507251478Sdelphij * percentage factor for the next scan cycle, 4508251478Sdelphij * since more compressed buffers are likely to 4509251478Sdelphij * be present 4510185029Spjd * l2arc_feed_secs seconds between L2ARC writing 4511185029Spjd * 4512185029Spjd * Tunables may be removed or added as future performance improvements are 4513185029Spjd * integrated, and also may become zpool properties. 4514208373Smm * 4515208373Smm * There are three key functions that control how the L2ARC warms up: 4516208373Smm * 4517208373Smm * l2arc_write_eligible() check if a buffer is eligible to cache 4518208373Smm * l2arc_write_size() calculate how much to write 4519208373Smm * l2arc_write_interval() calculate sleep delay between writes 4520208373Smm * 4521208373Smm * These three functions determine what to write, how much, and how quickly 4522208373Smm * to send writes. 4523185029Spjd */ 4524185029Spjd 4525208373Smmstatic boolean_t 4526277586Sdelphijl2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) 4527208373Smm{ 4528208373Smm /* 4529208373Smm * A buffer is *not* eligible for the L2ARC if it: 4530208373Smm * 1. belongs to a different spa. 4531208373Smm * 2. is already cached on the L2ARC. 4532208373Smm * 3. has an I/O in progress (it may be an incomplete read). 4533208373Smm * 4. is flagged not eligible (zfs property). 4534208373Smm */ 4535277586Sdelphij if (hdr->b_spa != spa_guid) { 4536208373Smm ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 4537208373Smm return (B_FALSE); 4538208373Smm } 4539277586Sdelphij if (hdr->b_l2hdr != NULL) { 4540208373Smm ARCSTAT_BUMP(arcstat_l2_write_in_l2); 4541208373Smm return (B_FALSE); 4542208373Smm } 4543277586Sdelphij if (HDR_IO_IN_PROGRESS(hdr)) { 4544208373Smm ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 4545208373Smm return (B_FALSE); 4546208373Smm } 4547277586Sdelphij if (!HDR_L2CACHE(hdr)) { 4548208373Smm ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 4549208373Smm return (B_FALSE); 4550208373Smm } 4551208373Smm 4552208373Smm return (B_TRUE); 4553208373Smm} 4554208373Smm 4555208373Smmstatic uint64_t 4556251478Sdelphijl2arc_write_size(void) 4557208373Smm{ 4558208373Smm uint64_t size; 4559208373Smm 4560251478Sdelphij /* 4561251478Sdelphij * Make sure our globals have meaningful values in case the user 4562251478Sdelphij * altered them. 4563251478Sdelphij */ 4564251478Sdelphij size = l2arc_write_max; 4565251478Sdelphij if (size == 0) { 4566251478Sdelphij cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 4567251478Sdelphij "be greater than zero, resetting it to the default (%d)", 4568251478Sdelphij L2ARC_WRITE_SIZE); 4569251478Sdelphij size = l2arc_write_max = L2ARC_WRITE_SIZE; 4570251478Sdelphij } 4571208373Smm 4572208373Smm if (arc_warm == B_FALSE) 4573251478Sdelphij size += l2arc_write_boost; 4574208373Smm 4575208373Smm return (size); 4576208373Smm 4577208373Smm} 4578208373Smm 4579208373Smmstatic clock_t 4580208373Smml2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 4581208373Smm{ 4582219089Spjd clock_t interval, next, now; 4583208373Smm 4584208373Smm /* 4585208373Smm * If the ARC lists are busy, increase our write rate; if the 4586208373Smm * lists are stale, idle back. This is achieved by checking 4587208373Smm * how much we previously wrote - if it was more than half of 4588208373Smm * what we wanted, schedule the next write much sooner. 4589208373Smm */ 4590208373Smm if (l2arc_feed_again && wrote > (wanted / 2)) 4591208373Smm interval = (hz * l2arc_feed_min_ms) / 1000; 4592208373Smm else 4593208373Smm interval = hz * l2arc_feed_secs; 4594208373Smm 4595219089Spjd now = ddi_get_lbolt(); 4596219089Spjd next = MAX(now, MIN(now + interval, began + interval)); 4597208373Smm 4598208373Smm return (next); 4599208373Smm} 4600208373Smm 4601185029Spjdstatic void 4602185029Spjdl2arc_hdr_stat_add(void) 4603185029Spjd{ 4604185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 4605185029Spjd ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 4606185029Spjd} 4607185029Spjd 4608185029Spjdstatic void 4609185029Spjdl2arc_hdr_stat_remove(void) 4610185029Spjd{ 4611185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 4612185029Spjd ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 4613185029Spjd} 4614185029Spjd 4615185029Spjd/* 4616185029Spjd * Cycle through L2ARC devices. This is how L2ARC load balances. 4617185029Spjd * If a device is returned, this also returns holding the spa config lock. 4618185029Spjd */ 4619185029Spjdstatic l2arc_dev_t * 4620185029Spjdl2arc_dev_get_next(void) 4621185029Spjd{ 4622185029Spjd l2arc_dev_t *first, *next = NULL; 4623185029Spjd 4624185029Spjd /* 4625185029Spjd * Lock out the removal of spas (spa_namespace_lock), then removal 4626185029Spjd * of cache devices (l2arc_dev_mtx). Once a device has been selected, 4627185029Spjd * both locks will be dropped and a spa config lock held instead. 4628185029Spjd */ 4629185029Spjd mutex_enter(&spa_namespace_lock); 4630185029Spjd mutex_enter(&l2arc_dev_mtx); 4631185029Spjd 4632185029Spjd /* if there are no vdevs, there is nothing to do */ 4633185029Spjd if (l2arc_ndev == 0) 4634185029Spjd goto out; 4635185029Spjd 4636185029Spjd first = NULL; 4637185029Spjd next = l2arc_dev_last; 4638185029Spjd do { 4639185029Spjd /* loop around the list looking for a non-faulted vdev */ 4640185029Spjd if (next == NULL) { 4641185029Spjd next = list_head(l2arc_dev_list); 4642185029Spjd } else { 4643185029Spjd next = list_next(l2arc_dev_list, next); 4644185029Spjd if (next == NULL) 4645185029Spjd next = list_head(l2arc_dev_list); 4646185029Spjd } 4647185029Spjd 4648185029Spjd /* if we have come back to the start, bail out */ 4649185029Spjd if (first == NULL) 4650185029Spjd first = next; 4651185029Spjd else if (next == first) 4652185029Spjd break; 4653185029Spjd 4654185029Spjd } while (vdev_is_dead(next->l2ad_vdev)); 4655185029Spjd 4656185029Spjd /* if we were unable to find any usable vdevs, return NULL */ 4657185029Spjd if (vdev_is_dead(next->l2ad_vdev)) 4658185029Spjd next = NULL; 4659185029Spjd 4660185029Spjd l2arc_dev_last = next; 4661185029Spjd 4662185029Spjdout: 4663185029Spjd mutex_exit(&l2arc_dev_mtx); 4664185029Spjd 4665185029Spjd /* 4666185029Spjd * Grab the config lock to prevent the 'next' device from being 4667185029Spjd * removed while we are writing to it. 4668185029Spjd */ 4669185029Spjd if (next != NULL) 4670185029Spjd spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 4671185029Spjd mutex_exit(&spa_namespace_lock); 4672185029Spjd 4673185029Spjd return (next); 4674185029Spjd} 4675185029Spjd 4676185029Spjd/* 4677185029Spjd * Free buffers that were tagged for destruction. 4678185029Spjd */ 4679185029Spjdstatic void 4680185029Spjdl2arc_do_free_on_write() 4681185029Spjd{ 4682185029Spjd list_t *buflist; 4683185029Spjd l2arc_data_free_t *df, *df_prev; 4684185029Spjd 4685185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 4686185029Spjd buflist = l2arc_free_on_write; 4687185029Spjd 4688185029Spjd for (df = list_tail(buflist); df; df = df_prev) { 4689185029Spjd df_prev = list_prev(buflist, df); 4690185029Spjd ASSERT(df->l2df_data != NULL); 4691185029Spjd ASSERT(df->l2df_func != NULL); 4692185029Spjd df->l2df_func(df->l2df_data, df->l2df_size); 4693185029Spjd list_remove(buflist, df); 4694185029Spjd kmem_free(df, sizeof (l2arc_data_free_t)); 4695185029Spjd } 4696185029Spjd 4697185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 4698185029Spjd} 4699185029Spjd 4700185029Spjd/* 4701185029Spjd * A write to a cache device has completed. Update all headers to allow 4702185029Spjd * reads from these buffers to begin. 4703185029Spjd */ 4704185029Spjdstatic void 4705185029Spjdl2arc_write_done(zio_t *zio) 4706185029Spjd{ 4707185029Spjd l2arc_write_callback_t *cb; 4708185029Spjd l2arc_dev_t *dev; 4709185029Spjd list_t *buflist; 4710277586Sdelphij arc_buf_hdr_t *head, *hdr, *hdr_prev; 4711185029Spjd l2arc_buf_hdr_t *abl2; 4712185029Spjd kmutex_t *hash_lock; 4713268654Sdelphij int64_t bytes_dropped = 0; 4714185029Spjd 4715185029Spjd cb = zio->io_private; 4716185029Spjd ASSERT(cb != NULL); 4717185029Spjd dev = cb->l2wcb_dev; 4718185029Spjd ASSERT(dev != NULL); 4719185029Spjd head = cb->l2wcb_head; 4720185029Spjd ASSERT(head != NULL); 4721185029Spjd buflist = dev->l2ad_buflist; 4722185029Spjd ASSERT(buflist != NULL); 4723185029Spjd DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 4724185029Spjd l2arc_write_callback_t *, cb); 4725185029Spjd 4726185029Spjd if (zio->io_error != 0) 4727185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_error); 4728185029Spjd 4729185029Spjd mutex_enter(&l2arc_buflist_mtx); 4730185029Spjd 4731185029Spjd /* 4732185029Spjd * All writes completed, or an error was hit. 4733185029Spjd */ 4734277586Sdelphij for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { 4735277586Sdelphij hdr_prev = list_prev(buflist, hdr); 4736277586Sdelphij abl2 = hdr->b_l2hdr; 4737185029Spjd 4738262115Savg /* 4739262115Savg * Release the temporary compressed buffer as soon as possible. 4740262115Savg */ 4741262115Savg if (abl2->b_compress != ZIO_COMPRESS_OFF) 4742277586Sdelphij l2arc_release_cdata_buf(hdr); 4743262115Savg 4744277586Sdelphij hash_lock = HDR_LOCK(hdr); 4745185029Spjd if (!mutex_tryenter(hash_lock)) { 4746185029Spjd /* 4747185029Spjd * This buffer misses out. It may be in a stage 4748185029Spjd * of eviction. Its ARC_L2_WRITING flag will be 4749185029Spjd * left set, denying reads to this buffer. 4750185029Spjd */ 4751185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 4752185029Spjd continue; 4753185029Spjd } 4754185029Spjd 4755185029Spjd if (zio->io_error != 0) { 4756185029Spjd /* 4757185029Spjd * Error - drop L2ARC entry. 4758185029Spjd */ 4759277586Sdelphij list_remove(buflist, hdr); 4760251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize); 4761268654Sdelphij bytes_dropped += abl2->b_asize; 4762277586Sdelphij hdr->b_l2hdr = NULL; 4763248572Ssmh trim_map_free(abl2->b_dev->l2ad_vdev, abl2->b_daddr, 4764277586Sdelphij hdr->b_size, 0); 4765185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4766277586Sdelphij ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 4767185029Spjd } 4768185029Spjd 4769185029Spjd /* 4770185029Spjd * Allow ARC to begin reads to this L2ARC entry. 4771185029Spjd */ 4772277586Sdelphij hdr->b_flags &= ~ARC_FLAG_L2_WRITING; 4773185029Spjd 4774185029Spjd mutex_exit(hash_lock); 4775185029Spjd } 4776185029Spjd 4777185029Spjd atomic_inc_64(&l2arc_writes_done); 4778185029Spjd list_remove(buflist, head); 4779185029Spjd kmem_cache_free(hdr_cache, head); 4780185029Spjd mutex_exit(&l2arc_buflist_mtx); 4781185029Spjd 4782268654Sdelphij vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 4783268654Sdelphij 4784185029Spjd l2arc_do_free_on_write(); 4785185029Spjd 4786185029Spjd kmem_free(cb, sizeof (l2arc_write_callback_t)); 4787185029Spjd} 4788185029Spjd 4789185029Spjd/* 4790185029Spjd * A read to a cache device completed. Validate buffer contents before 4791185029Spjd * handing over to the regular ARC routines. 4792185029Spjd */ 4793185029Spjdstatic void 4794185029Spjdl2arc_read_done(zio_t *zio) 4795185029Spjd{ 4796185029Spjd l2arc_read_callback_t *cb; 4797185029Spjd arc_buf_hdr_t *hdr; 4798185029Spjd arc_buf_t *buf; 4799185029Spjd kmutex_t *hash_lock; 4800185029Spjd int equal; 4801185029Spjd 4802185029Spjd ASSERT(zio->io_vd != NULL); 4803185029Spjd ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 4804185029Spjd 4805185029Spjd spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 4806185029Spjd 4807185029Spjd cb = zio->io_private; 4808185029Spjd ASSERT(cb != NULL); 4809185029Spjd buf = cb->l2rcb_buf; 4810185029Spjd ASSERT(buf != NULL); 4811185029Spjd 4812219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 4813185029Spjd mutex_enter(hash_lock); 4814219089Spjd hdr = buf->b_hdr; 4815219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4816185029Spjd 4817185029Spjd /* 4818251478Sdelphij * If the buffer was compressed, decompress it first. 4819251478Sdelphij */ 4820251478Sdelphij if (cb->l2rcb_compress != ZIO_COMPRESS_OFF) 4821251478Sdelphij l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress); 4822251478Sdelphij ASSERT(zio->io_data != NULL); 4823251478Sdelphij 4824251478Sdelphij /* 4825185029Spjd * Check this survived the L2ARC journey. 4826185029Spjd */ 4827185029Spjd equal = arc_cksum_equal(buf); 4828185029Spjd if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 4829185029Spjd mutex_exit(hash_lock); 4830185029Spjd zio->io_private = buf; 4831185029Spjd zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 4832185029Spjd zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 4833185029Spjd arc_read_done(zio); 4834185029Spjd } else { 4835185029Spjd mutex_exit(hash_lock); 4836185029Spjd /* 4837185029Spjd * Buffer didn't survive caching. Increment stats and 4838185029Spjd * reissue to the original storage device. 4839185029Spjd */ 4840185029Spjd if (zio->io_error != 0) { 4841185029Spjd ARCSTAT_BUMP(arcstat_l2_io_error); 4842185029Spjd } else { 4843249195Smm zio->io_error = SET_ERROR(EIO); 4844185029Spjd } 4845185029Spjd if (!equal) 4846185029Spjd ARCSTAT_BUMP(arcstat_l2_cksum_bad); 4847185029Spjd 4848185029Spjd /* 4849185029Spjd * If there's no waiter, issue an async i/o to the primary 4850185029Spjd * storage now. If there *is* a waiter, the caller must 4851185029Spjd * issue the i/o in a context where it's OK to block. 4852185029Spjd */ 4853209962Smm if (zio->io_waiter == NULL) { 4854209962Smm zio_t *pio = zio_unique_parent(zio); 4855209962Smm 4856209962Smm ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 4857209962Smm 4858209962Smm zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 4859185029Spjd buf->b_data, zio->io_size, arc_read_done, buf, 4860185029Spjd zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 4861209962Smm } 4862185029Spjd } 4863185029Spjd 4864185029Spjd kmem_free(cb, sizeof (l2arc_read_callback_t)); 4865185029Spjd} 4866185029Spjd 4867185029Spjd/* 4868185029Spjd * This is the list priority from which the L2ARC will search for pages to 4869185029Spjd * cache. This is used within loops (0..3) to cycle through lists in the 4870185029Spjd * desired order. This order can have a significant effect on cache 4871185029Spjd * performance. 4872185029Spjd * 4873185029Spjd * Currently the metadata lists are hit first, MFU then MRU, followed by 4874185029Spjd * the data lists. This function returns a locked list, and also returns 4875185029Spjd * the lock pointer. 4876185029Spjd */ 4877185029Spjdstatic list_t * 4878185029Spjdl2arc_list_locked(int list_num, kmutex_t **lock) 4879185029Spjd{ 4880247187Smm list_t *list = NULL; 4881205231Skmacy int idx; 4882185029Spjd 4883206796Spjd ASSERT(list_num >= 0 && list_num < 2 * ARC_BUFC_NUMLISTS); 4884206796Spjd 4885205231Skmacy if (list_num < ARC_BUFC_NUMMETADATALISTS) { 4886205231Skmacy idx = list_num; 4887205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4888205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4889206796Spjd } else if (list_num < ARC_BUFC_NUMMETADATALISTS * 2) { 4890205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4891205231Skmacy list = &arc_mru->arcs_lists[idx]; 4892205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4893206796Spjd } else if (list_num < (ARC_BUFC_NUMMETADATALISTS * 2 + 4894205231Skmacy ARC_BUFC_NUMDATALISTS)) { 4895205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4896205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4897205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4898205231Skmacy } else { 4899205231Skmacy idx = list_num - ARC_BUFC_NUMLISTS; 4900205231Skmacy list = &arc_mru->arcs_lists[idx]; 4901205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4902185029Spjd } 4903185029Spjd 4904185029Spjd ASSERT(!(MUTEX_HELD(*lock))); 4905185029Spjd mutex_enter(*lock); 4906185029Spjd return (list); 4907185029Spjd} 4908185029Spjd 4909185029Spjd/* 4910185029Spjd * Evict buffers from the device write hand to the distance specified in 4911185029Spjd * bytes. This distance may span populated buffers, it may span nothing. 4912185029Spjd * This is clearing a region on the L2ARC device ready for writing. 4913185029Spjd * If the 'all' boolean is set, every buffer is evicted. 4914185029Spjd */ 4915185029Spjdstatic void 4916185029Spjdl2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 4917185029Spjd{ 4918185029Spjd list_t *buflist; 4919185029Spjd l2arc_buf_hdr_t *abl2; 4920277586Sdelphij arc_buf_hdr_t *hdr, *hdr_prev; 4921185029Spjd kmutex_t *hash_lock; 4922185029Spjd uint64_t taddr; 4923268654Sdelphij int64_t bytes_evicted = 0; 4924185029Spjd 4925185029Spjd buflist = dev->l2ad_buflist; 4926185029Spjd 4927185029Spjd if (buflist == NULL) 4928185029Spjd return; 4929185029Spjd 4930185029Spjd if (!all && dev->l2ad_first) { 4931185029Spjd /* 4932185029Spjd * This is the first sweep through the device. There is 4933185029Spjd * nothing to evict. 4934185029Spjd */ 4935185029Spjd return; 4936185029Spjd } 4937185029Spjd 4938185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 4939185029Spjd /* 4940185029Spjd * When nearing the end of the device, evict to the end 4941185029Spjd * before the device write hand jumps to the start. 4942185029Spjd */ 4943185029Spjd taddr = dev->l2ad_end; 4944185029Spjd } else { 4945185029Spjd taddr = dev->l2ad_hand + distance; 4946185029Spjd } 4947185029Spjd DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 4948185029Spjd uint64_t, taddr, boolean_t, all); 4949185029Spjd 4950185029Spjdtop: 4951185029Spjd mutex_enter(&l2arc_buflist_mtx); 4952277586Sdelphij for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { 4953277586Sdelphij hdr_prev = list_prev(buflist, hdr); 4954185029Spjd 4955277586Sdelphij hash_lock = HDR_LOCK(hdr); 4956185029Spjd if (!mutex_tryenter(hash_lock)) { 4957185029Spjd /* 4958185029Spjd * Missed the hash lock. Retry. 4959185029Spjd */ 4960185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 4961185029Spjd mutex_exit(&l2arc_buflist_mtx); 4962185029Spjd mutex_enter(hash_lock); 4963185029Spjd mutex_exit(hash_lock); 4964185029Spjd goto top; 4965185029Spjd } 4966185029Spjd 4967277586Sdelphij if (HDR_L2_WRITE_HEAD(hdr)) { 4968185029Spjd /* 4969185029Spjd * We hit a write head node. Leave it for 4970185029Spjd * l2arc_write_done(). 4971185029Spjd */ 4972277586Sdelphij list_remove(buflist, hdr); 4973185029Spjd mutex_exit(hash_lock); 4974185029Spjd continue; 4975185029Spjd } 4976185029Spjd 4977277586Sdelphij if (!all && hdr->b_l2hdr != NULL && 4978277586Sdelphij (hdr->b_l2hdr->b_daddr > taddr || 4979277586Sdelphij hdr->b_l2hdr->b_daddr < dev->l2ad_hand)) { 4980185029Spjd /* 4981185029Spjd * We've evicted to the target address, 4982185029Spjd * or the end of the device. 4983185029Spjd */ 4984185029Spjd mutex_exit(hash_lock); 4985185029Spjd break; 4986185029Spjd } 4987185029Spjd 4988277586Sdelphij if (HDR_FREE_IN_PROGRESS(hdr)) { 4989185029Spjd /* 4990185029Spjd * Already on the path to destruction. 4991185029Spjd */ 4992185029Spjd mutex_exit(hash_lock); 4993185029Spjd continue; 4994185029Spjd } 4995185029Spjd 4996277586Sdelphij if (hdr->b_state == arc_l2c_only) { 4997277586Sdelphij ASSERT(!HDR_L2_READING(hdr)); 4998185029Spjd /* 4999185029Spjd * This doesn't exist in the ARC. Destroy. 5000185029Spjd * arc_hdr_destroy() will call list_remove() 5001185029Spjd * and decrement arcstat_l2_size. 5002185029Spjd */ 5003277586Sdelphij arc_change_state(arc_anon, hdr, hash_lock); 5004277586Sdelphij arc_hdr_destroy(hdr); 5005185029Spjd } else { 5006185029Spjd /* 5007185029Spjd * Invalidate issued or about to be issued 5008185029Spjd * reads, since we may be about to write 5009185029Spjd * over this location. 5010185029Spjd */ 5011277586Sdelphij if (HDR_L2_READING(hdr)) { 5012185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_reading); 5013277586Sdelphij hdr->b_flags |= ARC_FLAG_L2_EVICTED; 5014185029Spjd } 5015185029Spjd 5016185029Spjd /* 5017185029Spjd * Tell ARC this no longer exists in L2ARC. 5018185029Spjd */ 5019277586Sdelphij if (hdr->b_l2hdr != NULL) { 5020277586Sdelphij abl2 = hdr->b_l2hdr; 5021251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize); 5022268654Sdelphij bytes_evicted += abl2->b_asize; 5023277586Sdelphij hdr->b_l2hdr = NULL; 5024275492Sdelphij /* 5025275492Sdelphij * We are destroying l2hdr, so ensure that 5026275492Sdelphij * its compressed buffer, if any, is not leaked. 5027275492Sdelphij */ 5028275492Sdelphij ASSERT(abl2->b_tmp_cdata == NULL); 5029185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 5030277586Sdelphij ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 5031185029Spjd } 5032277586Sdelphij list_remove(buflist, hdr); 5033185029Spjd 5034185029Spjd /* 5035185029Spjd * This may have been leftover after a 5036185029Spjd * failed write. 5037185029Spjd */ 5038277586Sdelphij hdr->b_flags &= ~ARC_FLAG_L2_WRITING; 5039185029Spjd } 5040185029Spjd mutex_exit(hash_lock); 5041185029Spjd } 5042185029Spjd mutex_exit(&l2arc_buflist_mtx); 5043185029Spjd 5044268654Sdelphij vdev_space_update(dev->l2ad_vdev, -bytes_evicted, 0, 0); 5045185029Spjd dev->l2ad_evict = taddr; 5046185029Spjd} 5047185029Spjd 5048185029Spjd/* 5049185029Spjd * Find and write ARC buffers to the L2ARC device. 5050185029Spjd * 5051277586Sdelphij * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid 5052185029Spjd * for reading until they have completed writing. 5053251478Sdelphij * The headroom_boost is an in-out parameter used to maintain headroom boost 5054251478Sdelphij * state between calls to this function. 5055251478Sdelphij * 5056251478Sdelphij * Returns the number of bytes actually written (which may be smaller than 5057251478Sdelphij * the delta by which the device hand has changed due to alignment). 5058185029Spjd */ 5059208373Smmstatic uint64_t 5060251478Sdelphijl2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz, 5061251478Sdelphij boolean_t *headroom_boost) 5062185029Spjd{ 5063277586Sdelphij arc_buf_hdr_t *hdr, *hdr_prev, *head; 5064185029Spjd list_t *list; 5065251478Sdelphij uint64_t write_asize, write_psize, write_sz, headroom, 5066251478Sdelphij buf_compress_minsz; 5067185029Spjd void *buf_data; 5068251478Sdelphij kmutex_t *list_lock; 5069251478Sdelphij boolean_t full; 5070185029Spjd l2arc_write_callback_t *cb; 5071185029Spjd zio_t *pio, *wzio; 5072228103Smm uint64_t guid = spa_load_guid(spa); 5073251478Sdelphij const boolean_t do_headroom_boost = *headroom_boost; 5074185029Spjd int try; 5075185029Spjd 5076185029Spjd ASSERT(dev->l2ad_vdev != NULL); 5077185029Spjd 5078251478Sdelphij /* Lower the flag now, we might want to raise it again later. */ 5079251478Sdelphij *headroom_boost = B_FALSE; 5080251478Sdelphij 5081185029Spjd pio = NULL; 5082251478Sdelphij write_sz = write_asize = write_psize = 0; 5083185029Spjd full = B_FALSE; 5084185029Spjd head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 5085277586Sdelphij head->b_flags |= ARC_FLAG_L2_WRITE_HEAD; 5086185029Spjd 5087205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 5088185029Spjd /* 5089251478Sdelphij * We will want to try to compress buffers that are at least 2x the 5090251478Sdelphij * device sector size. 5091251478Sdelphij */ 5092251478Sdelphij buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift; 5093251478Sdelphij 5094251478Sdelphij /* 5095185029Spjd * Copy buffers for L2ARC writing. 5096185029Spjd */ 5097185029Spjd mutex_enter(&l2arc_buflist_mtx); 5098206796Spjd for (try = 0; try < 2 * ARC_BUFC_NUMLISTS; try++) { 5099251478Sdelphij uint64_t passed_sz = 0; 5100251478Sdelphij 5101185029Spjd list = l2arc_list_locked(try, &list_lock); 5102205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 5103185029Spjd 5104185029Spjd /* 5105185029Spjd * L2ARC fast warmup. 5106185029Spjd * 5107185029Spjd * Until the ARC is warm and starts to evict, read from the 5108185029Spjd * head of the ARC lists rather than the tail. 5109185029Spjd */ 5110185029Spjd if (arc_warm == B_FALSE) 5111277586Sdelphij hdr = list_head(list); 5112185029Spjd else 5113277586Sdelphij hdr = list_tail(list); 5114277586Sdelphij if (hdr == NULL) 5115205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 5116185029Spjd 5117274625Savg headroom = target_sz * l2arc_headroom * 2 / ARC_BUFC_NUMLISTS; 5118251478Sdelphij if (do_headroom_boost) 5119251478Sdelphij headroom = (headroom * l2arc_headroom_boost) / 100; 5120251478Sdelphij 5121277586Sdelphij for (; hdr; hdr = hdr_prev) { 5122251478Sdelphij l2arc_buf_hdr_t *l2hdr; 5123251478Sdelphij kmutex_t *hash_lock; 5124251478Sdelphij uint64_t buf_sz; 5125251478Sdelphij 5126185029Spjd if (arc_warm == B_FALSE) 5127277586Sdelphij hdr_prev = list_next(list, hdr); 5128185029Spjd else 5129277586Sdelphij hdr_prev = list_prev(list, hdr); 5130277586Sdelphij ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, hdr->b_size); 5131206796Spjd 5132277586Sdelphij hash_lock = HDR_LOCK(hdr); 5133251478Sdelphij if (!mutex_tryenter(hash_lock)) { 5134205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 5135185029Spjd /* 5136185029Spjd * Skip this buffer rather than waiting. 5137185029Spjd */ 5138185029Spjd continue; 5139185029Spjd } 5140185029Spjd 5141277586Sdelphij passed_sz += hdr->b_size; 5142185029Spjd if (passed_sz > headroom) { 5143185029Spjd /* 5144185029Spjd * Searched too far. 5145185029Spjd */ 5146185029Spjd mutex_exit(hash_lock); 5147205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 5148185029Spjd break; 5149185029Spjd } 5150185029Spjd 5151277586Sdelphij if (!l2arc_write_eligible(guid, hdr)) { 5152185029Spjd mutex_exit(hash_lock); 5153185029Spjd continue; 5154185029Spjd } 5155185029Spjd 5156277586Sdelphij if ((write_sz + hdr->b_size) > target_sz) { 5157185029Spjd full = B_TRUE; 5158185029Spjd mutex_exit(hash_lock); 5159205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_full); 5160185029Spjd break; 5161185029Spjd } 5162185029Spjd 5163185029Spjd if (pio == NULL) { 5164185029Spjd /* 5165185029Spjd * Insert a dummy header on the buflist so 5166185029Spjd * l2arc_write_done() can find where the 5167185029Spjd * write buffers begin without searching. 5168185029Spjd */ 5169185029Spjd list_insert_head(dev->l2ad_buflist, head); 5170185029Spjd 5171185029Spjd cb = kmem_alloc( 5172185029Spjd sizeof (l2arc_write_callback_t), KM_SLEEP); 5173185029Spjd cb->l2wcb_dev = dev; 5174185029Spjd cb->l2wcb_head = head; 5175185029Spjd pio = zio_root(spa, l2arc_write_done, cb, 5176185029Spjd ZIO_FLAG_CANFAIL); 5177205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_pios); 5178185029Spjd } 5179185029Spjd 5180185029Spjd /* 5181185029Spjd * Create and add a new L2ARC header. 5182185029Spjd */ 5183251478Sdelphij l2hdr = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 5184251478Sdelphij l2hdr->b_dev = dev; 5185277586Sdelphij hdr->b_flags |= ARC_FLAG_L2_WRITING; 5186185029Spjd 5187251478Sdelphij /* 5188251478Sdelphij * Temporarily stash the data buffer in b_tmp_cdata. 5189251478Sdelphij * The subsequent write step will pick it up from 5190277586Sdelphij * there. This is because can't access hdr->b_buf 5191251478Sdelphij * without holding the hash_lock, which we in turn 5192251478Sdelphij * can't access without holding the ARC list locks 5193251478Sdelphij * (which we want to avoid during compression/writing). 5194251478Sdelphij */ 5195251478Sdelphij l2hdr->b_compress = ZIO_COMPRESS_OFF; 5196277586Sdelphij l2hdr->b_asize = hdr->b_size; 5197277586Sdelphij l2hdr->b_tmp_cdata = hdr->b_buf->b_data; 5198251478Sdelphij 5199277586Sdelphij buf_sz = hdr->b_size; 5200277586Sdelphij hdr->b_l2hdr = l2hdr; 5201185029Spjd 5202277586Sdelphij list_insert_head(dev->l2ad_buflist, hdr); 5203251478Sdelphij 5204185029Spjd /* 5205185029Spjd * Compute and store the buffer cksum before 5206185029Spjd * writing. On debug the cksum is verified first. 5207185029Spjd */ 5208277586Sdelphij arc_cksum_verify(hdr->b_buf); 5209277586Sdelphij arc_cksum_compute(hdr->b_buf, B_TRUE); 5210185029Spjd 5211185029Spjd mutex_exit(hash_lock); 5212185029Spjd 5213251478Sdelphij write_sz += buf_sz; 5214251478Sdelphij } 5215251478Sdelphij 5216251478Sdelphij mutex_exit(list_lock); 5217251478Sdelphij 5218251478Sdelphij if (full == B_TRUE) 5219251478Sdelphij break; 5220251478Sdelphij } 5221251478Sdelphij 5222251478Sdelphij /* No buffers selected for writing? */ 5223251478Sdelphij if (pio == NULL) { 5224251478Sdelphij ASSERT0(write_sz); 5225251478Sdelphij mutex_exit(&l2arc_buflist_mtx); 5226251478Sdelphij kmem_cache_free(hdr_cache, head); 5227251478Sdelphij return (0); 5228251478Sdelphij } 5229251478Sdelphij 5230251478Sdelphij /* 5231251478Sdelphij * Now start writing the buffers. We're starting at the write head 5232251478Sdelphij * and work backwards, retracing the course of the buffer selector 5233251478Sdelphij * loop above. 5234251478Sdelphij */ 5235277586Sdelphij for (hdr = list_prev(dev->l2ad_buflist, head); hdr; 5236277586Sdelphij hdr = list_prev(dev->l2ad_buflist, hdr)) { 5237251478Sdelphij l2arc_buf_hdr_t *l2hdr; 5238251478Sdelphij uint64_t buf_sz; 5239251478Sdelphij 5240251478Sdelphij /* 5241251478Sdelphij * We shouldn't need to lock the buffer here, since we flagged 5242277586Sdelphij * it as ARC_FLAG_L2_WRITING in the previous step, but we must 5243277586Sdelphij * take care to only access its L2 cache parameters. In 5244277586Sdelphij * particular, hdr->b_buf may be invalid by now due to 5245277586Sdelphij * ARC eviction. 5246251478Sdelphij */ 5247277586Sdelphij l2hdr = hdr->b_l2hdr; 5248251478Sdelphij l2hdr->b_daddr = dev->l2ad_hand; 5249251478Sdelphij 5250277586Sdelphij if ((hdr->b_flags & ARC_FLAG_L2COMPRESS) && 5251251478Sdelphij l2hdr->b_asize >= buf_compress_minsz) { 5252251478Sdelphij if (l2arc_compress_buf(l2hdr)) { 5253251478Sdelphij /* 5254251478Sdelphij * If compression succeeded, enable headroom 5255251478Sdelphij * boost on the next scan cycle. 5256251478Sdelphij */ 5257251478Sdelphij *headroom_boost = B_TRUE; 5258251478Sdelphij } 5259251478Sdelphij } 5260251478Sdelphij 5261251478Sdelphij /* 5262251478Sdelphij * Pick up the buffer data we had previously stashed away 5263251478Sdelphij * (and now potentially also compressed). 5264251478Sdelphij */ 5265251478Sdelphij buf_data = l2hdr->b_tmp_cdata; 5266251478Sdelphij buf_sz = l2hdr->b_asize; 5267251478Sdelphij 5268275492Sdelphij /* 5269275492Sdelphij * If the data has not been compressed, then clear b_tmp_cdata 5270275492Sdelphij * to make sure that it points only to a temporary compression 5271275492Sdelphij * buffer. 5272275492Sdelphij */ 5273275492Sdelphij if (!L2ARC_IS_VALID_COMPRESS(l2hdr->b_compress)) 5274275492Sdelphij l2hdr->b_tmp_cdata = NULL; 5275275492Sdelphij 5276251478Sdelphij /* Compression may have squashed the buffer to zero length. */ 5277251478Sdelphij if (buf_sz != 0) { 5278251478Sdelphij uint64_t buf_p_sz; 5279251478Sdelphij 5280185029Spjd wzio = zio_write_phys(pio, dev->l2ad_vdev, 5281185029Spjd dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 5282185029Spjd NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 5283185029Spjd ZIO_FLAG_CANFAIL, B_FALSE); 5284185029Spjd 5285185029Spjd DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 5286185029Spjd zio_t *, wzio); 5287185029Spjd (void) zio_nowait(wzio); 5288185029Spjd 5289251478Sdelphij write_asize += buf_sz; 5290185029Spjd /* 5291185029Spjd * Keep the clock hand suitably device-aligned. 5292185029Spjd */ 5293251478Sdelphij buf_p_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 5294251478Sdelphij write_psize += buf_p_sz; 5295251478Sdelphij dev->l2ad_hand += buf_p_sz; 5296185029Spjd } 5297251478Sdelphij } 5298185029Spjd 5299185029Spjd mutex_exit(&l2arc_buflist_mtx); 5300185029Spjd 5301251478Sdelphij ASSERT3U(write_asize, <=, target_sz); 5302185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_sent); 5303251478Sdelphij ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize); 5304185029Spjd ARCSTAT_INCR(arcstat_l2_size, write_sz); 5305251478Sdelphij ARCSTAT_INCR(arcstat_l2_asize, write_asize); 5306273191Sdelphij vdev_space_update(dev->l2ad_vdev, write_psize, 0, 0); 5307185029Spjd 5308185029Spjd /* 5309185029Spjd * Bump device hand to the device start if it is approaching the end. 5310185029Spjd * l2arc_evict() will already have evicted ahead for this case. 5311185029Spjd */ 5312185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 5313185029Spjd dev->l2ad_hand = dev->l2ad_start; 5314185029Spjd dev->l2ad_evict = dev->l2ad_start; 5315185029Spjd dev->l2ad_first = B_FALSE; 5316185029Spjd } 5317185029Spjd 5318208373Smm dev->l2ad_writing = B_TRUE; 5319185029Spjd (void) zio_wait(pio); 5320208373Smm dev->l2ad_writing = B_FALSE; 5321208373Smm 5322251478Sdelphij return (write_asize); 5323185029Spjd} 5324185029Spjd 5325185029Spjd/* 5326251478Sdelphij * Compresses an L2ARC buffer. 5327251478Sdelphij * The data to be compressed must be prefilled in l2hdr->b_tmp_cdata and its 5328251478Sdelphij * size in l2hdr->b_asize. This routine tries to compress the data and 5329251478Sdelphij * depending on the compression result there are three possible outcomes: 5330251478Sdelphij * *) The buffer was incompressible. The original l2hdr contents were left 5331251478Sdelphij * untouched and are ready for writing to an L2 device. 5332251478Sdelphij * *) The buffer was all-zeros, so there is no need to write it to an L2 5333251478Sdelphij * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is 5334251478Sdelphij * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY. 5335251478Sdelphij * *) Compression succeeded and b_tmp_cdata was replaced with a temporary 5336251478Sdelphij * data buffer which holds the compressed data to be written, and b_asize 5337251478Sdelphij * tells us how much data there is. b_compress is set to the appropriate 5338251478Sdelphij * compression algorithm. Once writing is done, invoke 5339251478Sdelphij * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer. 5340251478Sdelphij * 5341251478Sdelphij * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the 5342251478Sdelphij * buffer was incompressible). 5343251478Sdelphij */ 5344251478Sdelphijstatic boolean_t 5345251478Sdelphijl2arc_compress_buf(l2arc_buf_hdr_t *l2hdr) 5346251478Sdelphij{ 5347251478Sdelphij void *cdata; 5348268649Sdelphij size_t csize, len, rounded; 5349251478Sdelphij 5350251478Sdelphij ASSERT(l2hdr->b_compress == ZIO_COMPRESS_OFF); 5351251478Sdelphij ASSERT(l2hdr->b_tmp_cdata != NULL); 5352251478Sdelphij 5353251478Sdelphij len = l2hdr->b_asize; 5354251478Sdelphij cdata = zio_data_buf_alloc(len); 5355251478Sdelphij csize = zio_compress_data(ZIO_COMPRESS_LZ4, l2hdr->b_tmp_cdata, 5356269732Sdelphij cdata, l2hdr->b_asize); 5357251478Sdelphij 5358251478Sdelphij if (csize == 0) { 5359251478Sdelphij /* zero block, indicate that there's nothing to write */ 5360251478Sdelphij zio_data_buf_free(cdata, len); 5361251478Sdelphij l2hdr->b_compress = ZIO_COMPRESS_EMPTY; 5362251478Sdelphij l2hdr->b_asize = 0; 5363251478Sdelphij l2hdr->b_tmp_cdata = NULL; 5364251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_zeros); 5365251478Sdelphij return (B_TRUE); 5366275609Savg } 5367275609Savg 5368275609Savg rounded = P2ROUNDUP(csize, 5369275609Savg (size_t)1 << l2hdr->b_dev->l2ad_vdev->vdev_ashift); 5370275609Savg if (rounded < len) { 5371251478Sdelphij /* 5372251478Sdelphij * Compression succeeded, we'll keep the cdata around for 5373251478Sdelphij * writing and release it afterwards. 5374251478Sdelphij */ 5375275609Savg if (rounded > csize) { 5376275609Savg bzero((char *)cdata + csize, rounded - csize); 5377275609Savg csize = rounded; 5378275609Savg } 5379251478Sdelphij l2hdr->b_compress = ZIO_COMPRESS_LZ4; 5380251478Sdelphij l2hdr->b_asize = csize; 5381251478Sdelphij l2hdr->b_tmp_cdata = cdata; 5382251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_successes); 5383251478Sdelphij return (B_TRUE); 5384251478Sdelphij } else { 5385251478Sdelphij /* 5386251478Sdelphij * Compression failed, release the compressed buffer. 5387251478Sdelphij * l2hdr will be left unmodified. 5388251478Sdelphij */ 5389251478Sdelphij zio_data_buf_free(cdata, len); 5390251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_failures); 5391251478Sdelphij return (B_FALSE); 5392251478Sdelphij } 5393251478Sdelphij} 5394251478Sdelphij 5395251478Sdelphij/* 5396251478Sdelphij * Decompresses a zio read back from an l2arc device. On success, the 5397251478Sdelphij * underlying zio's io_data buffer is overwritten by the uncompressed 5398251478Sdelphij * version. On decompression error (corrupt compressed stream), the 5399251478Sdelphij * zio->io_error value is set to signal an I/O error. 5400251478Sdelphij * 5401251478Sdelphij * Please note that the compressed data stream is not checksummed, so 5402251478Sdelphij * if the underlying device is experiencing data corruption, we may feed 5403251478Sdelphij * corrupt data to the decompressor, so the decompressor needs to be 5404251478Sdelphij * able to handle this situation (LZ4 does). 5405251478Sdelphij */ 5406251478Sdelphijstatic void 5407251478Sdelphijl2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c) 5408251478Sdelphij{ 5409251478Sdelphij ASSERT(L2ARC_IS_VALID_COMPRESS(c)); 5410251478Sdelphij 5411251478Sdelphij if (zio->io_error != 0) { 5412251478Sdelphij /* 5413251478Sdelphij * An io error has occured, just restore the original io 5414251478Sdelphij * size in preparation for a main pool read. 5415251478Sdelphij */ 5416251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 5417251478Sdelphij return; 5418251478Sdelphij } 5419251478Sdelphij 5420251478Sdelphij if (c == ZIO_COMPRESS_EMPTY) { 5421251478Sdelphij /* 5422251478Sdelphij * An empty buffer results in a null zio, which means we 5423251478Sdelphij * need to fill its io_data after we're done restoring the 5424251478Sdelphij * buffer's contents. 5425251478Sdelphij */ 5426251478Sdelphij ASSERT(hdr->b_buf != NULL); 5427251478Sdelphij bzero(hdr->b_buf->b_data, hdr->b_size); 5428251478Sdelphij zio->io_data = zio->io_orig_data = hdr->b_buf->b_data; 5429251478Sdelphij } else { 5430251478Sdelphij ASSERT(zio->io_data != NULL); 5431251478Sdelphij /* 5432251478Sdelphij * We copy the compressed data from the start of the arc buffer 5433251478Sdelphij * (the zio_read will have pulled in only what we need, the 5434251478Sdelphij * rest is garbage which we will overwrite at decompression) 5435251478Sdelphij * and then decompress back to the ARC data buffer. This way we 5436251478Sdelphij * can minimize copying by simply decompressing back over the 5437251478Sdelphij * original compressed data (rather than decompressing to an 5438251478Sdelphij * aux buffer and then copying back the uncompressed buffer, 5439251478Sdelphij * which is likely to be much larger). 5440251478Sdelphij */ 5441251478Sdelphij uint64_t csize; 5442251478Sdelphij void *cdata; 5443251478Sdelphij 5444251478Sdelphij csize = zio->io_size; 5445251478Sdelphij cdata = zio_data_buf_alloc(csize); 5446251478Sdelphij bcopy(zio->io_data, cdata, csize); 5447251478Sdelphij if (zio_decompress_data(c, cdata, zio->io_data, csize, 5448251478Sdelphij hdr->b_size) != 0) 5449251478Sdelphij zio->io_error = EIO; 5450251478Sdelphij zio_data_buf_free(cdata, csize); 5451251478Sdelphij } 5452251478Sdelphij 5453251478Sdelphij /* Restore the expected uncompressed IO size. */ 5454251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 5455251478Sdelphij} 5456251478Sdelphij 5457251478Sdelphij/* 5458251478Sdelphij * Releases the temporary b_tmp_cdata buffer in an l2arc header structure. 5459251478Sdelphij * This buffer serves as a temporary holder of compressed data while 5460251478Sdelphij * the buffer entry is being written to an l2arc device. Once that is 5461251478Sdelphij * done, we can dispose of it. 5462251478Sdelphij */ 5463251478Sdelphijstatic void 5464277586Sdelphijl2arc_release_cdata_buf(arc_buf_hdr_t *hdr) 5465251478Sdelphij{ 5466277586Sdelphij l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 5467251478Sdelphij 5468275492Sdelphij ASSERT(L2ARC_IS_VALID_COMPRESS(l2hdr->b_compress)); 5469275492Sdelphij if (l2hdr->b_compress != ZIO_COMPRESS_EMPTY) { 5470251478Sdelphij /* 5471251478Sdelphij * If the data was compressed, then we've allocated a 5472251478Sdelphij * temporary buffer for it, so now we need to release it. 5473251478Sdelphij */ 5474251478Sdelphij ASSERT(l2hdr->b_tmp_cdata != NULL); 5475277586Sdelphij zio_data_buf_free(l2hdr->b_tmp_cdata, hdr->b_size); 5476275492Sdelphij l2hdr->b_tmp_cdata = NULL; 5477275492Sdelphij } else { 5478275492Sdelphij ASSERT(l2hdr->b_tmp_cdata == NULL); 5479251478Sdelphij } 5480251478Sdelphij} 5481251478Sdelphij 5482251478Sdelphij/* 5483185029Spjd * This thread feeds the L2ARC at regular intervals. This is the beating 5484185029Spjd * heart of the L2ARC. 5485185029Spjd */ 5486185029Spjdstatic void 5487185029Spjdl2arc_feed_thread(void *dummy __unused) 5488185029Spjd{ 5489185029Spjd callb_cpr_t cpr; 5490185029Spjd l2arc_dev_t *dev; 5491185029Spjd spa_t *spa; 5492208373Smm uint64_t size, wrote; 5493219089Spjd clock_t begin, next = ddi_get_lbolt(); 5494251478Sdelphij boolean_t headroom_boost = B_FALSE; 5495185029Spjd 5496185029Spjd CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 5497185029Spjd 5498185029Spjd mutex_enter(&l2arc_feed_thr_lock); 5499185029Spjd 5500185029Spjd while (l2arc_thread_exit == 0) { 5501185029Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 5502185029Spjd (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 5503219089Spjd next - ddi_get_lbolt()); 5504185029Spjd CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 5505219089Spjd next = ddi_get_lbolt() + hz; 5506185029Spjd 5507185029Spjd /* 5508185029Spjd * Quick check for L2ARC devices. 5509185029Spjd */ 5510185029Spjd mutex_enter(&l2arc_dev_mtx); 5511185029Spjd if (l2arc_ndev == 0) { 5512185029Spjd mutex_exit(&l2arc_dev_mtx); 5513185029Spjd continue; 5514185029Spjd } 5515185029Spjd mutex_exit(&l2arc_dev_mtx); 5516219089Spjd begin = ddi_get_lbolt(); 5517185029Spjd 5518185029Spjd /* 5519185029Spjd * This selects the next l2arc device to write to, and in 5520185029Spjd * doing so the next spa to feed from: dev->l2ad_spa. This 5521185029Spjd * will return NULL if there are now no l2arc devices or if 5522185029Spjd * they are all faulted. 5523185029Spjd * 5524185029Spjd * If a device is returned, its spa's config lock is also 5525185029Spjd * held to prevent device removal. l2arc_dev_get_next() 5526185029Spjd * will grab and release l2arc_dev_mtx. 5527185029Spjd */ 5528185029Spjd if ((dev = l2arc_dev_get_next()) == NULL) 5529185029Spjd continue; 5530185029Spjd 5531185029Spjd spa = dev->l2ad_spa; 5532185029Spjd ASSERT(spa != NULL); 5533185029Spjd 5534185029Spjd /* 5535219089Spjd * If the pool is read-only then force the feed thread to 5536219089Spjd * sleep a little longer. 5537219089Spjd */ 5538219089Spjd if (!spa_writeable(spa)) { 5539219089Spjd next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 5540219089Spjd spa_config_exit(spa, SCL_L2ARC, dev); 5541219089Spjd continue; 5542219089Spjd } 5543219089Spjd 5544219089Spjd /* 5545185029Spjd * Avoid contributing to memory pressure. 5546185029Spjd */ 5547185029Spjd if (arc_reclaim_needed()) { 5548185029Spjd ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 5549185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 5550185029Spjd continue; 5551185029Spjd } 5552185029Spjd 5553185029Spjd ARCSTAT_BUMP(arcstat_l2_feeds); 5554185029Spjd 5555251478Sdelphij size = l2arc_write_size(); 5556185029Spjd 5557185029Spjd /* 5558185029Spjd * Evict L2ARC buffers that will be overwritten. 5559185029Spjd */ 5560185029Spjd l2arc_evict(dev, size, B_FALSE); 5561185029Spjd 5562185029Spjd /* 5563185029Spjd * Write ARC buffers. 5564185029Spjd */ 5565251478Sdelphij wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost); 5566208373Smm 5567208373Smm /* 5568208373Smm * Calculate interval between writes. 5569208373Smm */ 5570208373Smm next = l2arc_write_interval(begin, size, wrote); 5571185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 5572185029Spjd } 5573185029Spjd 5574185029Spjd l2arc_thread_exit = 0; 5575185029Spjd cv_broadcast(&l2arc_feed_thr_cv); 5576185029Spjd CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 5577185029Spjd thread_exit(); 5578185029Spjd} 5579185029Spjd 5580185029Spjdboolean_t 5581185029Spjdl2arc_vdev_present(vdev_t *vd) 5582185029Spjd{ 5583185029Spjd l2arc_dev_t *dev; 5584185029Spjd 5585185029Spjd mutex_enter(&l2arc_dev_mtx); 5586185029Spjd for (dev = list_head(l2arc_dev_list); dev != NULL; 5587185029Spjd dev = list_next(l2arc_dev_list, dev)) { 5588185029Spjd if (dev->l2ad_vdev == vd) 5589185029Spjd break; 5590185029Spjd } 5591185029Spjd mutex_exit(&l2arc_dev_mtx); 5592185029Spjd 5593185029Spjd return (dev != NULL); 5594185029Spjd} 5595185029Spjd 5596185029Spjd/* 5597185029Spjd * Add a vdev for use by the L2ARC. By this point the spa has already 5598185029Spjd * validated the vdev and opened it. 5599185029Spjd */ 5600185029Spjdvoid 5601219089Spjdl2arc_add_vdev(spa_t *spa, vdev_t *vd) 5602185029Spjd{ 5603185029Spjd l2arc_dev_t *adddev; 5604185029Spjd 5605185029Spjd ASSERT(!l2arc_vdev_present(vd)); 5606185029Spjd 5607255753Sgibbs vdev_ashift_optimize(vd); 5608255753Sgibbs 5609185029Spjd /* 5610185029Spjd * Create a new l2arc device entry. 5611185029Spjd */ 5612185029Spjd adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 5613185029Spjd adddev->l2ad_spa = spa; 5614185029Spjd adddev->l2ad_vdev = vd; 5615219089Spjd adddev->l2ad_start = VDEV_LABEL_START_SIZE; 5616219089Spjd adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 5617185029Spjd adddev->l2ad_hand = adddev->l2ad_start; 5618185029Spjd adddev->l2ad_evict = adddev->l2ad_start; 5619185029Spjd adddev->l2ad_first = B_TRUE; 5620208373Smm adddev->l2ad_writing = B_FALSE; 5621185029Spjd 5622185029Spjd /* 5623185029Spjd * This is a list of all ARC buffers that are still valid on the 5624185029Spjd * device. 5625185029Spjd */ 5626185029Spjd adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 5627185029Spjd list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 5628185029Spjd offsetof(arc_buf_hdr_t, b_l2node)); 5629185029Spjd 5630219089Spjd vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 5631185029Spjd 5632185029Spjd /* 5633185029Spjd * Add device to global list 5634185029Spjd */ 5635185029Spjd mutex_enter(&l2arc_dev_mtx); 5636185029Spjd list_insert_head(l2arc_dev_list, adddev); 5637185029Spjd atomic_inc_64(&l2arc_ndev); 5638185029Spjd mutex_exit(&l2arc_dev_mtx); 5639185029Spjd} 5640185029Spjd 5641185029Spjd/* 5642185029Spjd * Remove a vdev from the L2ARC. 5643185029Spjd */ 5644185029Spjdvoid 5645185029Spjdl2arc_remove_vdev(vdev_t *vd) 5646185029Spjd{ 5647185029Spjd l2arc_dev_t *dev, *nextdev, *remdev = NULL; 5648185029Spjd 5649185029Spjd /* 5650185029Spjd * Find the device by vdev 5651185029Spjd */ 5652185029Spjd mutex_enter(&l2arc_dev_mtx); 5653185029Spjd for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 5654185029Spjd nextdev = list_next(l2arc_dev_list, dev); 5655185029Spjd if (vd == dev->l2ad_vdev) { 5656185029Spjd remdev = dev; 5657185029Spjd break; 5658185029Spjd } 5659185029Spjd } 5660185029Spjd ASSERT(remdev != NULL); 5661185029Spjd 5662185029Spjd /* 5663185029Spjd * Remove device from global list 5664185029Spjd */ 5665185029Spjd list_remove(l2arc_dev_list, remdev); 5666185029Spjd l2arc_dev_last = NULL; /* may have been invalidated */ 5667185029Spjd atomic_dec_64(&l2arc_ndev); 5668185029Spjd mutex_exit(&l2arc_dev_mtx); 5669185029Spjd 5670185029Spjd /* 5671185029Spjd * Clear all buflists and ARC references. L2ARC device flush. 5672185029Spjd */ 5673185029Spjd l2arc_evict(remdev, 0, B_TRUE); 5674185029Spjd list_destroy(remdev->l2ad_buflist); 5675185029Spjd kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 5676185029Spjd kmem_free(remdev, sizeof (l2arc_dev_t)); 5677185029Spjd} 5678185029Spjd 5679185029Spjdvoid 5680185029Spjdl2arc_init(void) 5681185029Spjd{ 5682185029Spjd l2arc_thread_exit = 0; 5683185029Spjd l2arc_ndev = 0; 5684185029Spjd l2arc_writes_sent = 0; 5685185029Spjd l2arc_writes_done = 0; 5686185029Spjd 5687185029Spjd mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 5688185029Spjd cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 5689185029Spjd mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 5690185029Spjd mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 5691185029Spjd mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 5692185029Spjd 5693185029Spjd l2arc_dev_list = &L2ARC_dev_list; 5694185029Spjd l2arc_free_on_write = &L2ARC_free_on_write; 5695185029Spjd list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 5696185029Spjd offsetof(l2arc_dev_t, l2ad_node)); 5697185029Spjd list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 5698185029Spjd offsetof(l2arc_data_free_t, l2df_list_node)); 5699185029Spjd} 5700185029Spjd 5701185029Spjdvoid 5702185029Spjdl2arc_fini(void) 5703185029Spjd{ 5704185029Spjd /* 5705185029Spjd * This is called from dmu_fini(), which is called from spa_fini(); 5706185029Spjd * Because of this, we can assume that all l2arc devices have 5707185029Spjd * already been removed when the pools themselves were removed. 5708185029Spjd */ 5709185029Spjd 5710185029Spjd l2arc_do_free_on_write(); 5711185029Spjd 5712185029Spjd mutex_destroy(&l2arc_feed_thr_lock); 5713185029Spjd cv_destroy(&l2arc_feed_thr_cv); 5714185029Spjd mutex_destroy(&l2arc_dev_mtx); 5715185029Spjd mutex_destroy(&l2arc_buflist_mtx); 5716185029Spjd mutex_destroy(&l2arc_free_on_write_mtx); 5717185029Spjd 5718185029Spjd list_destroy(l2arc_dev_list); 5719185029Spjd list_destroy(l2arc_free_on_write); 5720185029Spjd} 5721185029Spjd 5722185029Spjdvoid 5723185029Spjdl2arc_start(void) 5724185029Spjd{ 5725209962Smm if (!(spa_mode_global & FWRITE)) 5726185029Spjd return; 5727185029Spjd 5728185029Spjd (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 5729185029Spjd TS_RUN, minclsyspri); 5730185029Spjd} 5731185029Spjd 5732185029Spjdvoid 5733185029Spjdl2arc_stop(void) 5734185029Spjd{ 5735209962Smm if (!(spa_mode_global & FWRITE)) 5736185029Spjd return; 5737185029Spjd 5738185029Spjd mutex_enter(&l2arc_feed_thr_lock); 5739185029Spjd cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 5740185029Spjd l2arc_thread_exit = 1; 5741185029Spjd while (l2arc_thread_exit != 0) 5742185029Spjd cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 5743185029Spjd mutex_exit(&l2arc_feed_thr_lock); 5744185029Spjd} 5745