arc.c revision 246666
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. 23228103Smm * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24228103Smm * Copyright (c) 2011 by Delphix. All rights reserved. 25168404Spjd */ 26168404Spjd 27168404Spjd/* 28168404Spjd * DVA-based Adjustable Replacement Cache 29168404Spjd * 30168404Spjd * While much of the theory of operation used here is 31168404Spjd * based on the self-tuning, low overhead replacement cache 32168404Spjd * presented by Megiddo and Modha at FAST 2003, there are some 33168404Spjd * significant differences: 34168404Spjd * 35168404Spjd * 1. The Megiddo and Modha model assumes any page is evictable. 36168404Spjd * Pages in its cache cannot be "locked" into memory. This makes 37168404Spjd * the eviction algorithm simple: evict the last page in the list. 38168404Spjd * This also make the performance characteristics easy to reason 39168404Spjd * about. Our cache is not so simple. At any given moment, some 40168404Spjd * subset of the blocks in the cache are un-evictable because we 41168404Spjd * have handed out a reference to them. Blocks are only evictable 42168404Spjd * when there are no external references active. This makes 43168404Spjd * eviction far more problematic: we choose to evict the evictable 44168404Spjd * blocks that are the "lowest" in the list. 45168404Spjd * 46168404Spjd * There are times when it is not possible to evict the requested 47168404Spjd * space. In these circumstances we are unable to adjust the cache 48168404Spjd * size. To prevent the cache growing unbounded at these times we 49185029Spjd * implement a "cache throttle" that slows the flow of new data 50185029Spjd * into the cache until we can make space available. 51168404Spjd * 52168404Spjd * 2. The Megiddo and Modha model assumes a fixed cache size. 53168404Spjd * Pages are evicted when the cache is full and there is a cache 54168404Spjd * miss. Our model has a variable sized cache. It grows with 55185029Spjd * high use, but also tries to react to memory pressure from the 56168404Spjd * operating system: decreasing its size when system memory is 57168404Spjd * tight. 58168404Spjd * 59168404Spjd * 3. The Megiddo and Modha model assumes a fixed page size. All 60168404Spjd * elements of the cache are therefor exactly the same size. So 61168404Spjd * when adjusting the cache size following a cache miss, its simply 62168404Spjd * a matter of choosing a single page to evict. In our model, we 63168404Spjd * have variable sized cache blocks (rangeing from 512 bytes to 64168404Spjd * 128K bytes). We therefor choose a set of blocks to evict to make 65168404Spjd * space for a cache miss that approximates as closely as possible 66168404Spjd * the space used by the new block. 67168404Spjd * 68168404Spjd * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 69168404Spjd * by N. Megiddo & D. Modha, FAST 2003 70168404Spjd */ 71168404Spjd 72168404Spjd/* 73168404Spjd * The locking model: 74168404Spjd * 75168404Spjd * A new reference to a cache buffer can be obtained in two 76168404Spjd * ways: 1) via a hash table lookup using the DVA as a key, 77185029Spjd * or 2) via one of the ARC lists. The arc_read() interface 78168404Spjd * uses method 1, while the internal arc algorithms for 79168404Spjd * adjusting the cache use method 2. We therefor provide two 80168404Spjd * types of locks: 1) the hash table lock array, and 2) the 81168404Spjd * arc list locks. 82168404Spjd * 83168404Spjd * Buffers do not have their own mutexs, rather they rely on the 84168404Spjd * hash table mutexs for the bulk of their protection (i.e. most 85168404Spjd * fields in the arc_buf_hdr_t are protected by these mutexs). 86168404Spjd * 87168404Spjd * buf_hash_find() returns the appropriate mutex (held) when it 88168404Spjd * locates the requested buffer in the hash table. It returns 89168404Spjd * NULL for the mutex if the buffer was not in the table. 90168404Spjd * 91168404Spjd * buf_hash_remove() expects the appropriate hash mutex to be 92168404Spjd * already held before it is invoked. 93168404Spjd * 94168404Spjd * Each arc state also has a mutex which is used to protect the 95168404Spjd * buffer list associated with the state. When attempting to 96168404Spjd * obtain a hash table lock while holding an arc list lock you 97168404Spjd * must use: mutex_tryenter() to avoid deadlock. Also note that 98168404Spjd * the active state mutex must be held before the ghost state mutex. 99168404Spjd * 100168404Spjd * Arc buffers may have an associated eviction callback function. 101168404Spjd * This function will be invoked prior to removing the buffer (e.g. 102168404Spjd * in arc_do_user_evicts()). Note however that the data associated 103168404Spjd * with the buffer may be evicted prior to the callback. The callback 104168404Spjd * must be made with *no locks held* (to prevent deadlock). Additionally, 105168404Spjd * the users of callbacks must ensure that their private data is 106168404Spjd * protected from simultaneous callbacks from arc_buf_evict() 107168404Spjd * and arc_do_user_evicts(). 108168404Spjd * 109168404Spjd * Note that the majority of the performance stats are manipulated 110168404Spjd * with atomic operations. 111185029Spjd * 112185029Spjd * The L2ARC uses the l2arc_buflist_mtx global mutex for the following: 113185029Spjd * 114185029Spjd * - L2ARC buflist creation 115185029Spjd * - L2ARC buflist eviction 116185029Spjd * - L2ARC write completion, which walks L2ARC buflists 117185029Spjd * - ARC header destruction, as it removes from L2ARC buflists 118185029Spjd * - ARC header release, as it removes from L2ARC buflists 119168404Spjd */ 120168404Spjd 121168404Spjd#include <sys/spa.h> 122168404Spjd#include <sys/zio.h> 123168404Spjd#include <sys/zfs_context.h> 124168404Spjd#include <sys/arc.h> 125168404Spjd#include <sys/refcount.h> 126185029Spjd#include <sys/vdev.h> 127219089Spjd#include <sys/vdev_impl.h> 128168404Spjd#ifdef _KERNEL 129168404Spjd#include <sys/dnlc.h> 130168404Spjd#endif 131168404Spjd#include <sys/callb.h> 132168404Spjd#include <sys/kstat.h> 133219089Spjd#include <zfs_fletcher.h> 134168404Spjd#include <sys/sdt.h> 135168404Spjd 136191902Skmacy#include <vm/vm_pageout.h> 137191902Skmacy 138240133Smm#ifdef illumos 139240133Smm#ifndef _KERNEL 140240133Smm/* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ 141240133Smmboolean_t arc_watch = B_FALSE; 142240133Smmint arc_procfd; 143240133Smm#endif 144240133Smm#endif /* illumos */ 145240133Smm 146168404Spjdstatic kmutex_t arc_reclaim_thr_lock; 147168404Spjdstatic kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 148168404Spjdstatic uint8_t arc_thread_exit; 149168404Spjd 150185029Spjdextern int zfs_write_limit_shift; 151185029Spjdextern uint64_t zfs_write_limit_max; 152185029Spjdextern kmutex_t zfs_write_limit_lock; 153185029Spjd 154168404Spjd#define ARC_REDUCE_DNLC_PERCENT 3 155168404Spjduint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 156168404Spjd 157168404Spjdtypedef enum arc_reclaim_strategy { 158168404Spjd ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 159168404Spjd ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 160168404Spjd} arc_reclaim_strategy_t; 161168404Spjd 162168404Spjd/* number of seconds before growing cache again */ 163168404Spjdstatic int arc_grow_retry = 60; 164168404Spjd 165208373Smm/* shift of arc_c for calculating both min and max arc_p */ 166208373Smmstatic int arc_p_min_shift = 4; 167208373Smm 168208373Smm/* log2(fraction of arc to reclaim) */ 169208373Smmstatic int arc_shrink_shift = 5; 170208373Smm 171168404Spjd/* 172168404Spjd * minimum lifespan of a prefetch block in clock ticks 173168404Spjd * (initialized in arc_init()) 174168404Spjd */ 175168404Spjdstatic int arc_min_prefetch_lifespan; 176168404Spjd 177208373Smmstatic int arc_dead; 178194043Skmacyextern int zfs_prefetch_disable; 179168404Spjd 180168404Spjd/* 181185029Spjd * The arc has filled available memory and has now warmed up. 182185029Spjd */ 183185029Spjdstatic boolean_t arc_warm; 184185029Spjd 185185029Spjd/* 186168404Spjd * These tunables are for performance analysis. 187168404Spjd */ 188185029Spjduint64_t zfs_arc_max; 189185029Spjduint64_t zfs_arc_min; 190185029Spjduint64_t zfs_arc_meta_limit = 0; 191208373Smmint zfs_arc_grow_retry = 0; 192208373Smmint zfs_arc_shrink_shift = 0; 193208373Smmint zfs_arc_p_min_shift = 0; 194242845Sdelphijint zfs_disable_dup_eviction = 0; 195185029Spjd 196185029SpjdTUNABLE_QUAD("vfs.zfs.arc_max", &zfs_arc_max); 197185029SpjdTUNABLE_QUAD("vfs.zfs.arc_min", &zfs_arc_min); 198185029SpjdTUNABLE_QUAD("vfs.zfs.arc_meta_limit", &zfs_arc_meta_limit); 199168473SpjdSYSCTL_DECL(_vfs_zfs); 200217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_max, CTLFLAG_RDTUN, &zfs_arc_max, 0, 201168473Spjd "Maximum ARC size"); 202217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_min, CTLFLAG_RDTUN, &zfs_arc_min, 0, 203168473Spjd "Minimum ARC size"); 204168404Spjd 205168404Spjd/* 206185029Spjd * Note that buffers can be in one of 6 states: 207168404Spjd * ARC_anon - anonymous (discussed below) 208168404Spjd * ARC_mru - recently used, currently cached 209168404Spjd * ARC_mru_ghost - recentely used, no longer in cache 210168404Spjd * ARC_mfu - frequently used, currently cached 211168404Spjd * ARC_mfu_ghost - frequently used, no longer in cache 212185029Spjd * ARC_l2c_only - exists in L2ARC but not other states 213185029Spjd * When there are no active references to the buffer, they are 214185029Spjd * are linked onto a list in one of these arc states. These are 215185029Spjd * the only buffers that can be evicted or deleted. Within each 216185029Spjd * state there are multiple lists, one for meta-data and one for 217185029Spjd * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 218185029Spjd * etc.) is tracked separately so that it can be managed more 219185029Spjd * explicitly: favored over data, limited explicitly. 220168404Spjd * 221168404Spjd * Anonymous buffers are buffers that are not associated with 222168404Spjd * a DVA. These are buffers that hold dirty block copies 223168404Spjd * before they are written to stable storage. By definition, 224168404Spjd * they are "ref'd" and are considered part of arc_mru 225168404Spjd * that cannot be freed. Generally, they will aquire a DVA 226168404Spjd * as they are written and migrate onto the arc_mru list. 227185029Spjd * 228185029Spjd * The ARC_l2c_only state is for buffers that are in the second 229185029Spjd * level ARC but no longer in any of the ARC_m* lists. The second 230185029Spjd * level ARC itself may also contain buffers that are in any of 231185029Spjd * the ARC_m* states - meaning that a buffer can exist in two 232185029Spjd * places. The reason for the ARC_l2c_only state is to keep the 233185029Spjd * buffer header in the hash table, so that reads that hit the 234185029Spjd * second level ARC benefit from these fast lookups. 235168404Spjd */ 236168404Spjd 237205264Skmacy#define ARCS_LOCK_PAD CACHE_LINE_SIZE 238205231Skmacystruct arcs_lock { 239205231Skmacy kmutex_t arcs_lock; 240205231Skmacy#ifdef _KERNEL 241205231Skmacy unsigned char pad[(ARCS_LOCK_PAD - sizeof (kmutex_t))]; 242205231Skmacy#endif 243205231Skmacy}; 244205231Skmacy 245205231Skmacy/* 246205231Skmacy * must be power of two for mask use to work 247205231Skmacy * 248205231Skmacy */ 249205231Skmacy#define ARC_BUFC_NUMDATALISTS 16 250205231Skmacy#define ARC_BUFC_NUMMETADATALISTS 16 251206796Spjd#define ARC_BUFC_NUMLISTS (ARC_BUFC_NUMMETADATALISTS + ARC_BUFC_NUMDATALISTS) 252205231Skmacy 253168404Spjdtypedef struct arc_state { 254185029Spjd uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 255185029Spjd uint64_t arcs_size; /* total amount of data in this state */ 256205231Skmacy list_t arcs_lists[ARC_BUFC_NUMLISTS]; /* list of evictable buffers */ 257205264Skmacy struct arcs_lock arcs_locks[ARC_BUFC_NUMLISTS] __aligned(CACHE_LINE_SIZE); 258168404Spjd} arc_state_t; 259168404Spjd 260206796Spjd#define ARCS_LOCK(s, i) (&((s)->arcs_locks[(i)].arcs_lock)) 261205231Skmacy 262185029Spjd/* The 6 states: */ 263168404Spjdstatic arc_state_t ARC_anon; 264168404Spjdstatic arc_state_t ARC_mru; 265168404Spjdstatic arc_state_t ARC_mru_ghost; 266168404Spjdstatic arc_state_t ARC_mfu; 267168404Spjdstatic arc_state_t ARC_mfu_ghost; 268185029Spjdstatic arc_state_t ARC_l2c_only; 269168404Spjd 270168404Spjdtypedef struct arc_stats { 271168404Spjd kstat_named_t arcstat_hits; 272168404Spjd kstat_named_t arcstat_misses; 273168404Spjd kstat_named_t arcstat_demand_data_hits; 274168404Spjd kstat_named_t arcstat_demand_data_misses; 275168404Spjd kstat_named_t arcstat_demand_metadata_hits; 276168404Spjd kstat_named_t arcstat_demand_metadata_misses; 277168404Spjd kstat_named_t arcstat_prefetch_data_hits; 278168404Spjd kstat_named_t arcstat_prefetch_data_misses; 279168404Spjd kstat_named_t arcstat_prefetch_metadata_hits; 280168404Spjd kstat_named_t arcstat_prefetch_metadata_misses; 281168404Spjd kstat_named_t arcstat_mru_hits; 282168404Spjd kstat_named_t arcstat_mru_ghost_hits; 283168404Spjd kstat_named_t arcstat_mfu_hits; 284168404Spjd kstat_named_t arcstat_mfu_ghost_hits; 285205231Skmacy kstat_named_t arcstat_allocated; 286168404Spjd kstat_named_t arcstat_deleted; 287205231Skmacy kstat_named_t arcstat_stolen; 288168404Spjd kstat_named_t arcstat_recycle_miss; 289168404Spjd kstat_named_t arcstat_mutex_miss; 290168404Spjd kstat_named_t arcstat_evict_skip; 291208373Smm kstat_named_t arcstat_evict_l2_cached; 292208373Smm kstat_named_t arcstat_evict_l2_eligible; 293208373Smm kstat_named_t arcstat_evict_l2_ineligible; 294168404Spjd kstat_named_t arcstat_hash_elements; 295168404Spjd kstat_named_t arcstat_hash_elements_max; 296168404Spjd kstat_named_t arcstat_hash_collisions; 297168404Spjd kstat_named_t arcstat_hash_chains; 298168404Spjd kstat_named_t arcstat_hash_chain_max; 299168404Spjd kstat_named_t arcstat_p; 300168404Spjd kstat_named_t arcstat_c; 301168404Spjd kstat_named_t arcstat_c_min; 302168404Spjd kstat_named_t arcstat_c_max; 303168404Spjd kstat_named_t arcstat_size; 304185029Spjd kstat_named_t arcstat_hdr_size; 305208373Smm kstat_named_t arcstat_data_size; 306208373Smm kstat_named_t arcstat_other_size; 307185029Spjd kstat_named_t arcstat_l2_hits; 308185029Spjd kstat_named_t arcstat_l2_misses; 309185029Spjd kstat_named_t arcstat_l2_feeds; 310185029Spjd kstat_named_t arcstat_l2_rw_clash; 311208373Smm kstat_named_t arcstat_l2_read_bytes; 312208373Smm kstat_named_t arcstat_l2_write_bytes; 313185029Spjd kstat_named_t arcstat_l2_writes_sent; 314185029Spjd kstat_named_t arcstat_l2_writes_done; 315185029Spjd kstat_named_t arcstat_l2_writes_error; 316185029Spjd kstat_named_t arcstat_l2_writes_hdr_miss; 317185029Spjd kstat_named_t arcstat_l2_evict_lock_retry; 318185029Spjd kstat_named_t arcstat_l2_evict_reading; 319185029Spjd kstat_named_t arcstat_l2_free_on_write; 320185029Spjd kstat_named_t arcstat_l2_abort_lowmem; 321185029Spjd kstat_named_t arcstat_l2_cksum_bad; 322185029Spjd kstat_named_t arcstat_l2_io_error; 323185029Spjd kstat_named_t arcstat_l2_size; 324185029Spjd kstat_named_t arcstat_l2_hdr_size; 325205231Skmacy kstat_named_t arcstat_l2_write_trylock_fail; 326205231Skmacy kstat_named_t arcstat_l2_write_passed_headroom; 327205231Skmacy kstat_named_t arcstat_l2_write_spa_mismatch; 328206796Spjd kstat_named_t arcstat_l2_write_in_l2; 329205231Skmacy kstat_named_t arcstat_l2_write_hdr_io_in_progress; 330205231Skmacy kstat_named_t arcstat_l2_write_not_cacheable; 331205231Skmacy kstat_named_t arcstat_l2_write_full; 332205231Skmacy kstat_named_t arcstat_l2_write_buffer_iter; 333205231Skmacy kstat_named_t arcstat_l2_write_pios; 334205231Skmacy kstat_named_t arcstat_l2_write_buffer_bytes_scanned; 335205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_iter; 336205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_null_iter; 337242845Sdelphij kstat_named_t arcstat_memory_throttle_count; 338242845Sdelphij kstat_named_t arcstat_duplicate_buffers; 339242845Sdelphij kstat_named_t arcstat_duplicate_buffers_size; 340242845Sdelphij kstat_named_t arcstat_duplicate_reads; 341168404Spjd} arc_stats_t; 342168404Spjd 343168404Spjdstatic arc_stats_t arc_stats = { 344168404Spjd { "hits", KSTAT_DATA_UINT64 }, 345168404Spjd { "misses", KSTAT_DATA_UINT64 }, 346168404Spjd { "demand_data_hits", KSTAT_DATA_UINT64 }, 347168404Spjd { "demand_data_misses", KSTAT_DATA_UINT64 }, 348168404Spjd { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 349168404Spjd { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 350168404Spjd { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 351168404Spjd { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 352168404Spjd { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 353168404Spjd { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 354168404Spjd { "mru_hits", KSTAT_DATA_UINT64 }, 355168404Spjd { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 356168404Spjd { "mfu_hits", KSTAT_DATA_UINT64 }, 357168404Spjd { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 358205231Skmacy { "allocated", KSTAT_DATA_UINT64 }, 359168404Spjd { "deleted", KSTAT_DATA_UINT64 }, 360205231Skmacy { "stolen", KSTAT_DATA_UINT64 }, 361168404Spjd { "recycle_miss", KSTAT_DATA_UINT64 }, 362168404Spjd { "mutex_miss", KSTAT_DATA_UINT64 }, 363168404Spjd { "evict_skip", KSTAT_DATA_UINT64 }, 364208373Smm { "evict_l2_cached", KSTAT_DATA_UINT64 }, 365208373Smm { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 366208373Smm { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 367168404Spjd { "hash_elements", KSTAT_DATA_UINT64 }, 368168404Spjd { "hash_elements_max", KSTAT_DATA_UINT64 }, 369168404Spjd { "hash_collisions", KSTAT_DATA_UINT64 }, 370168404Spjd { "hash_chains", KSTAT_DATA_UINT64 }, 371168404Spjd { "hash_chain_max", KSTAT_DATA_UINT64 }, 372168404Spjd { "p", KSTAT_DATA_UINT64 }, 373168404Spjd { "c", KSTAT_DATA_UINT64 }, 374168404Spjd { "c_min", KSTAT_DATA_UINT64 }, 375168404Spjd { "c_max", KSTAT_DATA_UINT64 }, 376185029Spjd { "size", KSTAT_DATA_UINT64 }, 377185029Spjd { "hdr_size", KSTAT_DATA_UINT64 }, 378208373Smm { "data_size", KSTAT_DATA_UINT64 }, 379208373Smm { "other_size", KSTAT_DATA_UINT64 }, 380185029Spjd { "l2_hits", KSTAT_DATA_UINT64 }, 381185029Spjd { "l2_misses", KSTAT_DATA_UINT64 }, 382185029Spjd { "l2_feeds", KSTAT_DATA_UINT64 }, 383185029Spjd { "l2_rw_clash", KSTAT_DATA_UINT64 }, 384208373Smm { "l2_read_bytes", KSTAT_DATA_UINT64 }, 385208373Smm { "l2_write_bytes", KSTAT_DATA_UINT64 }, 386185029Spjd { "l2_writes_sent", KSTAT_DATA_UINT64 }, 387185029Spjd { "l2_writes_done", KSTAT_DATA_UINT64 }, 388185029Spjd { "l2_writes_error", KSTAT_DATA_UINT64 }, 389185029Spjd { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 390185029Spjd { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 391185029Spjd { "l2_evict_reading", KSTAT_DATA_UINT64 }, 392185029Spjd { "l2_free_on_write", KSTAT_DATA_UINT64 }, 393185029Spjd { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 394185029Spjd { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 395185029Spjd { "l2_io_error", KSTAT_DATA_UINT64 }, 396185029Spjd { "l2_size", KSTAT_DATA_UINT64 }, 397185029Spjd { "l2_hdr_size", KSTAT_DATA_UINT64 }, 398206796Spjd { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 399206796Spjd { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 400206796Spjd { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 401206796Spjd { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 402206796Spjd { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 403206796Spjd { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 404206796Spjd { "l2_write_full", KSTAT_DATA_UINT64 }, 405206796Spjd { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 406206796Spjd { "l2_write_pios", KSTAT_DATA_UINT64 }, 407206796Spjd { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 408206796Spjd { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 409242845Sdelphij { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 }, 410242845Sdelphij { "memory_throttle_count", KSTAT_DATA_UINT64 }, 411242845Sdelphij { "duplicate_buffers", KSTAT_DATA_UINT64 }, 412242845Sdelphij { "duplicate_buffers_size", KSTAT_DATA_UINT64 }, 413242845Sdelphij { "duplicate_reads", KSTAT_DATA_UINT64 } 414168404Spjd}; 415168404Spjd 416168404Spjd#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 417168404Spjd 418168404Spjd#define ARCSTAT_INCR(stat, val) \ 419168404Spjd atomic_add_64(&arc_stats.stat.value.ui64, (val)); 420168404Spjd 421206796Spjd#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 422168404Spjd#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 423168404Spjd 424168404Spjd#define ARCSTAT_MAX(stat, val) { \ 425168404Spjd uint64_t m; \ 426168404Spjd while ((val) > (m = arc_stats.stat.value.ui64) && \ 427168404Spjd (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 428168404Spjd continue; \ 429168404Spjd} 430168404Spjd 431168404Spjd#define ARCSTAT_MAXSTAT(stat) \ 432168404Spjd ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 433168404Spjd 434168404Spjd/* 435168404Spjd * We define a macro to allow ARC hits/misses to be easily broken down by 436168404Spjd * two separate conditions, giving a total of four different subtypes for 437168404Spjd * each of hits and misses (so eight statistics total). 438168404Spjd */ 439168404Spjd#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 440168404Spjd if (cond1) { \ 441168404Spjd if (cond2) { \ 442168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 443168404Spjd } else { \ 444168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 445168404Spjd } \ 446168404Spjd } else { \ 447168404Spjd if (cond2) { \ 448168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 449168404Spjd } else { \ 450168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 451168404Spjd } \ 452168404Spjd } 453168404Spjd 454168404Spjdkstat_t *arc_ksp; 455206796Spjdstatic arc_state_t *arc_anon; 456168404Spjdstatic arc_state_t *arc_mru; 457168404Spjdstatic arc_state_t *arc_mru_ghost; 458168404Spjdstatic arc_state_t *arc_mfu; 459168404Spjdstatic arc_state_t *arc_mfu_ghost; 460185029Spjdstatic arc_state_t *arc_l2c_only; 461168404Spjd 462168404Spjd/* 463168404Spjd * There are several ARC variables that are critical to export as kstats -- 464168404Spjd * but we don't want to have to grovel around in the kstat whenever we wish to 465168404Spjd * manipulate them. For these variables, we therefore define them to be in 466168404Spjd * terms of the statistic variable. This assures that we are not introducing 467168404Spjd * the possibility of inconsistency by having shadow copies of the variables, 468168404Spjd * while still allowing the code to be readable. 469168404Spjd */ 470168404Spjd#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 471168404Spjd#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 472168404Spjd#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 473168404Spjd#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 474168404Spjd#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 475168404Spjd 476168404Spjdstatic int arc_no_grow; /* Don't try to grow cache size */ 477168404Spjdstatic uint64_t arc_tempreserve; 478209962Smmstatic uint64_t arc_loaned_bytes; 479185029Spjdstatic uint64_t arc_meta_used; 480185029Spjdstatic uint64_t arc_meta_limit; 481185029Spjdstatic uint64_t arc_meta_max = 0; 482229663SpjdSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_used, CTLFLAG_RD, &arc_meta_used, 0, 483229663Spjd "ARC metadata used"); 484229663SpjdSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_limit, CTLFLAG_RW, &arc_meta_limit, 0, 485229663Spjd "ARC metadata limit"); 486168404Spjd 487185029Spjdtypedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 488185029Spjd 489168404Spjdtypedef struct arc_callback arc_callback_t; 490168404Spjd 491168404Spjdstruct arc_callback { 492168404Spjd void *acb_private; 493168404Spjd arc_done_func_t *acb_done; 494168404Spjd arc_buf_t *acb_buf; 495168404Spjd zio_t *acb_zio_dummy; 496168404Spjd arc_callback_t *acb_next; 497168404Spjd}; 498168404Spjd 499168404Spjdtypedef struct arc_write_callback arc_write_callback_t; 500168404Spjd 501168404Spjdstruct arc_write_callback { 502168404Spjd void *awcb_private; 503168404Spjd arc_done_func_t *awcb_ready; 504168404Spjd arc_done_func_t *awcb_done; 505168404Spjd arc_buf_t *awcb_buf; 506168404Spjd}; 507168404Spjd 508168404Spjdstruct arc_buf_hdr { 509168404Spjd /* protected by hash lock */ 510168404Spjd dva_t b_dva; 511168404Spjd uint64_t b_birth; 512168404Spjd uint64_t b_cksum0; 513168404Spjd 514168404Spjd kmutex_t b_freeze_lock; 515168404Spjd zio_cksum_t *b_freeze_cksum; 516219089Spjd void *b_thawed; 517168404Spjd 518168404Spjd arc_buf_hdr_t *b_hash_next; 519168404Spjd arc_buf_t *b_buf; 520168404Spjd uint32_t b_flags; 521168404Spjd uint32_t b_datacnt; 522168404Spjd 523168404Spjd arc_callback_t *b_acb; 524168404Spjd kcondvar_t b_cv; 525168404Spjd 526168404Spjd /* immutable */ 527168404Spjd arc_buf_contents_t b_type; 528168404Spjd uint64_t b_size; 529209962Smm uint64_t b_spa; 530168404Spjd 531168404Spjd /* protected by arc state mutex */ 532168404Spjd arc_state_t *b_state; 533168404Spjd list_node_t b_arc_node; 534168404Spjd 535168404Spjd /* updated atomically */ 536168404Spjd clock_t b_arc_access; 537168404Spjd 538168404Spjd /* self protecting */ 539168404Spjd refcount_t b_refcnt; 540185029Spjd 541185029Spjd l2arc_buf_hdr_t *b_l2hdr; 542185029Spjd list_node_t b_l2node; 543168404Spjd}; 544168404Spjd 545168404Spjdstatic arc_buf_t *arc_eviction_list; 546168404Spjdstatic kmutex_t arc_eviction_mtx; 547168404Spjdstatic arc_buf_hdr_t arc_eviction_hdr; 548168404Spjdstatic void arc_get_data_buf(arc_buf_t *buf); 549168404Spjdstatic void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 550185029Spjdstatic int arc_evict_needed(arc_buf_contents_t type); 551209962Smmstatic void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes); 552240133Smm#ifdef illumos 553240133Smmstatic void arc_buf_watch(arc_buf_t *buf); 554240133Smm#endif /* illumos */ 555168404Spjd 556209962Smmstatic boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab); 557208373Smm 558168404Spjd#define GHOST_STATE(state) \ 559185029Spjd ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 560185029Spjd (state) == arc_l2c_only) 561168404Spjd 562168404Spjd/* 563168404Spjd * Private ARC flags. These flags are private ARC only flags that will show up 564168404Spjd * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 565168404Spjd * be passed in as arc_flags in things like arc_read. However, these flags 566168404Spjd * should never be passed and should only be set by ARC code. When adding new 567168404Spjd * public flags, make sure not to smash the private ones. 568168404Spjd */ 569168404Spjd 570168404Spjd#define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 571168404Spjd#define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 572168404Spjd#define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 573168404Spjd#define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 574168404Spjd#define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 575168404Spjd#define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 576185029Spjd#define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 577185029Spjd#define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 578185029Spjd#define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 579185029Spjd#define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 580168404Spjd 581168404Spjd#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 582168404Spjd#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 583168404Spjd#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 584208373Smm#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH) 585168404Spjd#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 586168404Spjd#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 587185029Spjd#define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 588185029Spjd#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 589185029Spjd#define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 590185029Spjd (hdr)->b_l2hdr != NULL) 591185029Spjd#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 592185029Spjd#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 593185029Spjd#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 594168404Spjd 595168404Spjd/* 596185029Spjd * Other sizes 597185029Spjd */ 598185029Spjd 599185029Spjd#define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 600185029Spjd#define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 601185029Spjd 602185029Spjd/* 603168404Spjd * Hash table routines 604168404Spjd */ 605168404Spjd 606205253Skmacy#define HT_LOCK_PAD CACHE_LINE_SIZE 607168404Spjd 608168404Spjdstruct ht_lock { 609168404Spjd kmutex_t ht_lock; 610168404Spjd#ifdef _KERNEL 611168404Spjd unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 612168404Spjd#endif 613168404Spjd}; 614168404Spjd 615168404Spjd#define BUF_LOCKS 256 616168404Spjdtypedef struct buf_hash_table { 617168404Spjd uint64_t ht_mask; 618168404Spjd arc_buf_hdr_t **ht_table; 619205264Skmacy struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 620168404Spjd} buf_hash_table_t; 621168404Spjd 622168404Spjdstatic buf_hash_table_t buf_hash_table; 623168404Spjd 624168404Spjd#define BUF_HASH_INDEX(spa, dva, birth) \ 625168404Spjd (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 626168404Spjd#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 627168404Spjd#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 628219089Spjd#define HDR_LOCK(hdr) \ 629219089Spjd (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 630168404Spjd 631168404Spjduint64_t zfs_crc64_table[256]; 632168404Spjd 633185029Spjd/* 634185029Spjd * Level 2 ARC 635185029Spjd */ 636185029Spjd 637208373Smm#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 638208373Smm#define L2ARC_HEADROOM 2 /* num of writes */ 639208373Smm#define L2ARC_FEED_SECS 1 /* caching interval secs */ 640208373Smm#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 641185029Spjd 642185029Spjd#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 643185029Spjd#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 644185029Spjd 645185029Spjd/* 646185029Spjd * L2ARC Performance Tunables 647185029Spjd */ 648185029Spjduint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 649185029Spjduint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 650185029Spjduint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 651185029Spjduint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 652208373Smmuint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 653219089Spjdboolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 654208373Smmboolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 655208373Smmboolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 656185029Spjd 657217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 658205231Skmacy &l2arc_write_max, 0, "max write size"); 659217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 660205231Skmacy &l2arc_write_boost, 0, "extra write during warmup"); 661217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 662205231Skmacy &l2arc_headroom, 0, "number of dev writes"); 663217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 664205231Skmacy &l2arc_feed_secs, 0, "interval seconds"); 665217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 666208373Smm &l2arc_feed_min_ms, 0, "min interval milliseconds"); 667205231Skmacy 668205231SkmacySYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 669205231Skmacy &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 670208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 671208373Smm &l2arc_feed_again, 0, "turbo warmup"); 672208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 673208373Smm &l2arc_norw, 0, "no reads during writes"); 674205231Skmacy 675217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 676205231Skmacy &ARC_anon.arcs_size, 0, "size of anonymous state"); 677217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD, 678205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state"); 679217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD, 680205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state"); 681205231Skmacy 682217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 683205231Skmacy &ARC_mru.arcs_size, 0, "size of mru state"); 684217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD, 685205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state"); 686217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD, 687205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state"); 688205231Skmacy 689217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 690205231Skmacy &ARC_mru_ghost.arcs_size, 0, "size of mru ghost state"); 691217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD, 692205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 693205231Skmacy "size of metadata in mru ghost state"); 694217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD, 695205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 696205231Skmacy "size of data in mru ghost state"); 697205231Skmacy 698217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 699205231Skmacy &ARC_mfu.arcs_size, 0, "size of mfu state"); 700217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD, 701205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state"); 702217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD, 703205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state"); 704205231Skmacy 705217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 706205231Skmacy &ARC_mfu_ghost.arcs_size, 0, "size of mfu ghost state"); 707217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD, 708205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 709205231Skmacy "size of metadata in mfu ghost state"); 710217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD, 711205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 712205231Skmacy "size of data in mfu ghost state"); 713205231Skmacy 714217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 715205231Skmacy &ARC_l2c_only.arcs_size, 0, "size of mru state"); 716205231Skmacy 717185029Spjd/* 718185029Spjd * L2ARC Internals 719185029Spjd */ 720185029Spjdtypedef struct l2arc_dev { 721185029Spjd vdev_t *l2ad_vdev; /* vdev */ 722185029Spjd spa_t *l2ad_spa; /* spa */ 723185029Spjd uint64_t l2ad_hand; /* next write location */ 724185029Spjd uint64_t l2ad_write; /* desired write size, bytes */ 725185029Spjd uint64_t l2ad_boost; /* warmup write boost, bytes */ 726185029Spjd uint64_t l2ad_start; /* first addr on device */ 727185029Spjd uint64_t l2ad_end; /* last addr on device */ 728185029Spjd uint64_t l2ad_evict; /* last addr eviction reached */ 729185029Spjd boolean_t l2ad_first; /* first sweep through */ 730208373Smm boolean_t l2ad_writing; /* currently writing */ 731185029Spjd list_t *l2ad_buflist; /* buffer list */ 732185029Spjd list_node_t l2ad_node; /* device list node */ 733185029Spjd} l2arc_dev_t; 734185029Spjd 735185029Spjdstatic list_t L2ARC_dev_list; /* device list */ 736185029Spjdstatic list_t *l2arc_dev_list; /* device list pointer */ 737185029Spjdstatic kmutex_t l2arc_dev_mtx; /* device list mutex */ 738185029Spjdstatic l2arc_dev_t *l2arc_dev_last; /* last device used */ 739185029Spjdstatic kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 740185029Spjdstatic list_t L2ARC_free_on_write; /* free after write buf list */ 741185029Spjdstatic list_t *l2arc_free_on_write; /* free after write list ptr */ 742185029Spjdstatic kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 743185029Spjdstatic uint64_t l2arc_ndev; /* number of devices */ 744185029Spjd 745185029Spjdtypedef struct l2arc_read_callback { 746185029Spjd arc_buf_t *l2rcb_buf; /* read buffer */ 747185029Spjd spa_t *l2rcb_spa; /* spa */ 748185029Spjd blkptr_t l2rcb_bp; /* original blkptr */ 749185029Spjd zbookmark_t l2rcb_zb; /* original bookmark */ 750185029Spjd int l2rcb_flags; /* original flags */ 751185029Spjd} l2arc_read_callback_t; 752185029Spjd 753185029Spjdtypedef struct l2arc_write_callback { 754185029Spjd l2arc_dev_t *l2wcb_dev; /* device info */ 755185029Spjd arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 756185029Spjd} l2arc_write_callback_t; 757185029Spjd 758185029Spjdstruct l2arc_buf_hdr { 759185029Spjd /* protected by arc_buf_hdr mutex */ 760185029Spjd l2arc_dev_t *b_dev; /* L2ARC device */ 761208373Smm uint64_t b_daddr; /* disk address, offset byte */ 762185029Spjd}; 763185029Spjd 764185029Spjdtypedef struct l2arc_data_free { 765185029Spjd /* protected by l2arc_free_on_write_mtx */ 766185029Spjd void *l2df_data; 767185029Spjd size_t l2df_size; 768185029Spjd void (*l2df_func)(void *, size_t); 769185029Spjd list_node_t l2df_list_node; 770185029Spjd} l2arc_data_free_t; 771185029Spjd 772185029Spjdstatic kmutex_t l2arc_feed_thr_lock; 773185029Spjdstatic kcondvar_t l2arc_feed_thr_cv; 774185029Spjdstatic uint8_t l2arc_thread_exit; 775185029Spjd 776185029Spjdstatic void l2arc_read_done(zio_t *zio); 777185029Spjdstatic void l2arc_hdr_stat_add(void); 778185029Spjdstatic void l2arc_hdr_stat_remove(void); 779185029Spjd 780168404Spjdstatic uint64_t 781209962Smmbuf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 782168404Spjd{ 783168404Spjd uint8_t *vdva = (uint8_t *)dva; 784168404Spjd uint64_t crc = -1ULL; 785168404Spjd int i; 786168404Spjd 787168404Spjd ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 788168404Spjd 789168404Spjd for (i = 0; i < sizeof (dva_t); i++) 790168404Spjd crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 791168404Spjd 792209962Smm crc ^= (spa>>8) ^ birth; 793168404Spjd 794168404Spjd return (crc); 795168404Spjd} 796168404Spjd 797168404Spjd#define BUF_EMPTY(buf) \ 798168404Spjd ((buf)->b_dva.dva_word[0] == 0 && \ 799168404Spjd (buf)->b_dva.dva_word[1] == 0 && \ 800168404Spjd (buf)->b_birth == 0) 801168404Spjd 802168404Spjd#define BUF_EQUAL(spa, dva, birth, buf) \ 803168404Spjd ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 804168404Spjd ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 805168404Spjd ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 806168404Spjd 807219089Spjdstatic void 808219089Spjdbuf_discard_identity(arc_buf_hdr_t *hdr) 809219089Spjd{ 810219089Spjd hdr->b_dva.dva_word[0] = 0; 811219089Spjd hdr->b_dva.dva_word[1] = 0; 812219089Spjd hdr->b_birth = 0; 813219089Spjd hdr->b_cksum0 = 0; 814219089Spjd} 815219089Spjd 816168404Spjdstatic arc_buf_hdr_t * 817209962Smmbuf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 818168404Spjd{ 819168404Spjd uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 820168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 821168404Spjd arc_buf_hdr_t *buf; 822168404Spjd 823168404Spjd mutex_enter(hash_lock); 824168404Spjd for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 825168404Spjd buf = buf->b_hash_next) { 826168404Spjd if (BUF_EQUAL(spa, dva, birth, buf)) { 827168404Spjd *lockp = hash_lock; 828168404Spjd return (buf); 829168404Spjd } 830168404Spjd } 831168404Spjd mutex_exit(hash_lock); 832168404Spjd *lockp = NULL; 833168404Spjd return (NULL); 834168404Spjd} 835168404Spjd 836168404Spjd/* 837168404Spjd * Insert an entry into the hash table. If there is already an element 838168404Spjd * equal to elem in the hash table, then the already existing element 839168404Spjd * will be returned and the new element will not be inserted. 840168404Spjd * Otherwise returns NULL. 841168404Spjd */ 842168404Spjdstatic arc_buf_hdr_t * 843168404Spjdbuf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 844168404Spjd{ 845168404Spjd uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 846168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 847168404Spjd arc_buf_hdr_t *fbuf; 848168404Spjd uint32_t i; 849168404Spjd 850168404Spjd ASSERT(!HDR_IN_HASH_TABLE(buf)); 851168404Spjd *lockp = hash_lock; 852168404Spjd mutex_enter(hash_lock); 853168404Spjd for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 854168404Spjd fbuf = fbuf->b_hash_next, i++) { 855168404Spjd if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 856168404Spjd return (fbuf); 857168404Spjd } 858168404Spjd 859168404Spjd buf->b_hash_next = buf_hash_table.ht_table[idx]; 860168404Spjd buf_hash_table.ht_table[idx] = buf; 861168404Spjd buf->b_flags |= ARC_IN_HASH_TABLE; 862168404Spjd 863168404Spjd /* collect some hash table performance data */ 864168404Spjd if (i > 0) { 865168404Spjd ARCSTAT_BUMP(arcstat_hash_collisions); 866168404Spjd if (i == 1) 867168404Spjd ARCSTAT_BUMP(arcstat_hash_chains); 868168404Spjd 869168404Spjd ARCSTAT_MAX(arcstat_hash_chain_max, i); 870168404Spjd } 871168404Spjd 872168404Spjd ARCSTAT_BUMP(arcstat_hash_elements); 873168404Spjd ARCSTAT_MAXSTAT(arcstat_hash_elements); 874168404Spjd 875168404Spjd return (NULL); 876168404Spjd} 877168404Spjd 878168404Spjdstatic void 879168404Spjdbuf_hash_remove(arc_buf_hdr_t *buf) 880168404Spjd{ 881168404Spjd arc_buf_hdr_t *fbuf, **bufp; 882168404Spjd uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 883168404Spjd 884168404Spjd ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 885168404Spjd ASSERT(HDR_IN_HASH_TABLE(buf)); 886168404Spjd 887168404Spjd bufp = &buf_hash_table.ht_table[idx]; 888168404Spjd while ((fbuf = *bufp) != buf) { 889168404Spjd ASSERT(fbuf != NULL); 890168404Spjd bufp = &fbuf->b_hash_next; 891168404Spjd } 892168404Spjd *bufp = buf->b_hash_next; 893168404Spjd buf->b_hash_next = NULL; 894168404Spjd buf->b_flags &= ~ARC_IN_HASH_TABLE; 895168404Spjd 896168404Spjd /* collect some hash table performance data */ 897168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_elements); 898168404Spjd 899168404Spjd if (buf_hash_table.ht_table[idx] && 900168404Spjd buf_hash_table.ht_table[idx]->b_hash_next == NULL) 901168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_chains); 902168404Spjd} 903168404Spjd 904168404Spjd/* 905168404Spjd * Global data structures and functions for the buf kmem cache. 906168404Spjd */ 907168404Spjdstatic kmem_cache_t *hdr_cache; 908168404Spjdstatic kmem_cache_t *buf_cache; 909168404Spjd 910168404Spjdstatic void 911168404Spjdbuf_fini(void) 912168404Spjd{ 913168404Spjd int i; 914168404Spjd 915168404Spjd kmem_free(buf_hash_table.ht_table, 916168404Spjd (buf_hash_table.ht_mask + 1) * sizeof (void *)); 917168404Spjd for (i = 0; i < BUF_LOCKS; i++) 918168404Spjd mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 919168404Spjd kmem_cache_destroy(hdr_cache); 920168404Spjd kmem_cache_destroy(buf_cache); 921168404Spjd} 922168404Spjd 923168404Spjd/* 924168404Spjd * Constructor callback - called when the cache is empty 925168404Spjd * and a new buf is requested. 926168404Spjd */ 927168404Spjd/* ARGSUSED */ 928168404Spjdstatic int 929168404Spjdhdr_cons(void *vbuf, void *unused, int kmflag) 930168404Spjd{ 931168404Spjd arc_buf_hdr_t *buf = vbuf; 932168404Spjd 933168404Spjd bzero(buf, sizeof (arc_buf_hdr_t)); 934168404Spjd refcount_create(&buf->b_refcnt); 935168404Spjd cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 936185029Spjd mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 937208373Smm arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 938185029Spjd 939168404Spjd return (0); 940168404Spjd} 941168404Spjd 942185029Spjd/* ARGSUSED */ 943185029Spjdstatic int 944185029Spjdbuf_cons(void *vbuf, void *unused, int kmflag) 945185029Spjd{ 946185029Spjd arc_buf_t *buf = vbuf; 947185029Spjd 948185029Spjd bzero(buf, sizeof (arc_buf_t)); 949219089Spjd mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 950208373Smm arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 951208373Smm 952185029Spjd return (0); 953185029Spjd} 954185029Spjd 955168404Spjd/* 956168404Spjd * Destructor callback - called when a cached buf is 957168404Spjd * no longer required. 958168404Spjd */ 959168404Spjd/* ARGSUSED */ 960168404Spjdstatic void 961168404Spjdhdr_dest(void *vbuf, void *unused) 962168404Spjd{ 963168404Spjd arc_buf_hdr_t *buf = vbuf; 964168404Spjd 965219089Spjd ASSERT(BUF_EMPTY(buf)); 966168404Spjd refcount_destroy(&buf->b_refcnt); 967168404Spjd cv_destroy(&buf->b_cv); 968185029Spjd mutex_destroy(&buf->b_freeze_lock); 969208373Smm arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 970168404Spjd} 971168404Spjd 972185029Spjd/* ARGSUSED */ 973185029Spjdstatic void 974185029Spjdbuf_dest(void *vbuf, void *unused) 975185029Spjd{ 976185029Spjd arc_buf_t *buf = vbuf; 977185029Spjd 978219089Spjd mutex_destroy(&buf->b_evict_lock); 979208373Smm arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 980185029Spjd} 981185029Spjd 982168404Spjd/* 983168404Spjd * Reclaim callback -- invoked when memory is low. 984168404Spjd */ 985168404Spjd/* ARGSUSED */ 986168404Spjdstatic void 987168404Spjdhdr_recl(void *unused) 988168404Spjd{ 989168404Spjd dprintf("hdr_recl called\n"); 990168404Spjd /* 991168404Spjd * umem calls the reclaim func when we destroy the buf cache, 992168404Spjd * which is after we do arc_fini(). 993168404Spjd */ 994168404Spjd if (!arc_dead) 995168404Spjd cv_signal(&arc_reclaim_thr_cv); 996168404Spjd} 997168404Spjd 998168404Spjdstatic void 999168404Spjdbuf_init(void) 1000168404Spjd{ 1001168404Spjd uint64_t *ct; 1002168404Spjd uint64_t hsize = 1ULL << 12; 1003168404Spjd int i, j; 1004168404Spjd 1005168404Spjd /* 1006168404Spjd * The hash table is big enough to fill all of physical memory 1007168404Spjd * with an average 64K block size. The table will take up 1008168404Spjd * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 1009168404Spjd */ 1010168696Spjd while (hsize * 65536 < (uint64_t)physmem * PAGESIZE) 1011168404Spjd hsize <<= 1; 1012168404Spjdretry: 1013168404Spjd buf_hash_table.ht_mask = hsize - 1; 1014168404Spjd buf_hash_table.ht_table = 1015168404Spjd kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1016168404Spjd if (buf_hash_table.ht_table == NULL) { 1017168404Spjd ASSERT(hsize > (1ULL << 8)); 1018168404Spjd hsize >>= 1; 1019168404Spjd goto retry; 1020168404Spjd } 1021168404Spjd 1022168404Spjd hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 1023168404Spjd 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 1024168404Spjd buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1025185029Spjd 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1026168404Spjd 1027168404Spjd for (i = 0; i < 256; i++) 1028168404Spjd for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1029168404Spjd *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1030168404Spjd 1031168404Spjd for (i = 0; i < BUF_LOCKS; i++) { 1032168404Spjd mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1033168404Spjd NULL, MUTEX_DEFAULT, NULL); 1034168404Spjd } 1035168404Spjd} 1036168404Spjd 1037168404Spjd#define ARC_MINTIME (hz>>4) /* 62 ms */ 1038168404Spjd 1039168404Spjdstatic void 1040168404Spjdarc_cksum_verify(arc_buf_t *buf) 1041168404Spjd{ 1042168404Spjd zio_cksum_t zc; 1043168404Spjd 1044168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1045168404Spjd return; 1046168404Spjd 1047168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1048168404Spjd if (buf->b_hdr->b_freeze_cksum == NULL || 1049168404Spjd (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 1050168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1051168404Spjd return; 1052168404Spjd } 1053168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1054168404Spjd if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 1055168404Spjd panic("buffer modified while frozen!"); 1056168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1057168404Spjd} 1058168404Spjd 1059185029Spjdstatic int 1060185029Spjdarc_cksum_equal(arc_buf_t *buf) 1061185029Spjd{ 1062185029Spjd zio_cksum_t zc; 1063185029Spjd int equal; 1064185029Spjd 1065185029Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1066185029Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1067185029Spjd equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 1068185029Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1069185029Spjd 1070185029Spjd return (equal); 1071185029Spjd} 1072185029Spjd 1073168404Spjdstatic void 1074185029Spjdarc_cksum_compute(arc_buf_t *buf, boolean_t force) 1075168404Spjd{ 1076185029Spjd if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 1077168404Spjd return; 1078168404Spjd 1079168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1080168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1081168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1082168404Spjd return; 1083168404Spjd } 1084168404Spjd buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 1085168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 1086168404Spjd buf->b_hdr->b_freeze_cksum); 1087168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1088240133Smm#ifdef illumos 1089240133Smm arc_buf_watch(buf); 1090240133Smm#endif /* illumos */ 1091168404Spjd} 1092168404Spjd 1093240133Smm#ifdef illumos 1094240133Smm#ifndef _KERNEL 1095240133Smmtypedef struct procctl { 1096240133Smm long cmd; 1097240133Smm prwatch_t prwatch; 1098240133Smm} procctl_t; 1099240133Smm#endif 1100240133Smm 1101240133Smm/* ARGSUSED */ 1102240133Smmstatic void 1103240133Smmarc_buf_unwatch(arc_buf_t *buf) 1104240133Smm{ 1105240133Smm#ifndef _KERNEL 1106240133Smm if (arc_watch) { 1107240133Smm int result; 1108240133Smm procctl_t ctl; 1109240133Smm ctl.cmd = PCWATCH; 1110240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1111240133Smm ctl.prwatch.pr_size = 0; 1112240133Smm ctl.prwatch.pr_wflags = 0; 1113240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1114240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1115240133Smm } 1116240133Smm#endif 1117240133Smm} 1118240133Smm 1119240133Smm/* ARGSUSED */ 1120240133Smmstatic void 1121240133Smmarc_buf_watch(arc_buf_t *buf) 1122240133Smm{ 1123240133Smm#ifndef _KERNEL 1124240133Smm if (arc_watch) { 1125240133Smm int result; 1126240133Smm procctl_t ctl; 1127240133Smm ctl.cmd = PCWATCH; 1128240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1129240133Smm ctl.prwatch.pr_size = buf->b_hdr->b_size; 1130240133Smm ctl.prwatch.pr_wflags = WA_WRITE; 1131240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1132240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1133240133Smm } 1134240133Smm#endif 1135240133Smm} 1136240133Smm#endif /* illumos */ 1137240133Smm 1138168404Spjdvoid 1139168404Spjdarc_buf_thaw(arc_buf_t *buf) 1140168404Spjd{ 1141185029Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1142185029Spjd if (buf->b_hdr->b_state != arc_anon) 1143185029Spjd panic("modifying non-anon buffer!"); 1144185029Spjd if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 1145185029Spjd panic("modifying buffer while i/o in progress!"); 1146185029Spjd arc_cksum_verify(buf); 1147185029Spjd } 1148168404Spjd 1149168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1150168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1151168404Spjd kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1152168404Spjd buf->b_hdr->b_freeze_cksum = NULL; 1153168404Spjd } 1154219089Spjd 1155219089Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1156219089Spjd if (buf->b_hdr->b_thawed) 1157219089Spjd kmem_free(buf->b_hdr->b_thawed, 1); 1158219089Spjd buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP); 1159219089Spjd } 1160219089Spjd 1161168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1162240133Smm 1163240133Smm#ifdef illumos 1164240133Smm arc_buf_unwatch(buf); 1165240133Smm#endif /* illumos */ 1166168404Spjd} 1167168404Spjd 1168168404Spjdvoid 1169168404Spjdarc_buf_freeze(arc_buf_t *buf) 1170168404Spjd{ 1171219089Spjd kmutex_t *hash_lock; 1172219089Spjd 1173168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1174168404Spjd return; 1175168404Spjd 1176219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1177219089Spjd mutex_enter(hash_lock); 1178219089Spjd 1179168404Spjd ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 1180168404Spjd buf->b_hdr->b_state == arc_anon); 1181185029Spjd arc_cksum_compute(buf, B_FALSE); 1182219089Spjd mutex_exit(hash_lock); 1183240133Smm 1184168404Spjd} 1185168404Spjd 1186168404Spjdstatic void 1187205231Skmacyget_buf_info(arc_buf_hdr_t *ab, arc_state_t *state, list_t **list, kmutex_t **lock) 1188205231Skmacy{ 1189205231Skmacy uint64_t buf_hashid = buf_hash(ab->b_spa, &ab->b_dva, ab->b_birth); 1190205231Skmacy 1191206796Spjd if (ab->b_type == ARC_BUFC_METADATA) 1192206796Spjd buf_hashid &= (ARC_BUFC_NUMMETADATALISTS - 1); 1193205231Skmacy else { 1194206796Spjd buf_hashid &= (ARC_BUFC_NUMDATALISTS - 1); 1195205231Skmacy buf_hashid += ARC_BUFC_NUMMETADATALISTS; 1196205231Skmacy } 1197205231Skmacy 1198205231Skmacy *list = &state->arcs_lists[buf_hashid]; 1199205231Skmacy *lock = ARCS_LOCK(state, buf_hashid); 1200205231Skmacy} 1201205231Skmacy 1202205231Skmacy 1203205231Skmacystatic void 1204168404Spjdadd_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1205168404Spjd{ 1206168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1207168404Spjd 1208168404Spjd if ((refcount_add(&ab->b_refcnt, tag) == 1) && 1209168404Spjd (ab->b_state != arc_anon)) { 1210206796Spjd uint64_t delta = ab->b_size * ab->b_datacnt; 1211206796Spjd uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 1212205231Skmacy list_t *list; 1213205231Skmacy kmutex_t *lock; 1214168404Spjd 1215205231Skmacy get_buf_info(ab, ab->b_state, &list, &lock); 1216205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1217205231Skmacy mutex_enter(lock); 1218168404Spjd ASSERT(list_link_active(&ab->b_arc_node)); 1219185029Spjd list_remove(list, ab); 1220168404Spjd if (GHOST_STATE(ab->b_state)) { 1221240415Smm ASSERT0(ab->b_datacnt); 1222168404Spjd ASSERT3P(ab->b_buf, ==, NULL); 1223168404Spjd delta = ab->b_size; 1224168404Spjd } 1225168404Spjd ASSERT(delta > 0); 1226185029Spjd ASSERT3U(*size, >=, delta); 1227185029Spjd atomic_add_64(size, -delta); 1228206794Spjd mutex_exit(lock); 1229185029Spjd /* remove the prefetch flag if we get a reference */ 1230168404Spjd if (ab->b_flags & ARC_PREFETCH) 1231168404Spjd ab->b_flags &= ~ARC_PREFETCH; 1232168404Spjd } 1233168404Spjd} 1234168404Spjd 1235168404Spjdstatic int 1236168404Spjdremove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1237168404Spjd{ 1238168404Spjd int cnt; 1239168404Spjd arc_state_t *state = ab->b_state; 1240168404Spjd 1241168404Spjd ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1242168404Spjd ASSERT(!GHOST_STATE(state)); 1243168404Spjd 1244168404Spjd if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 1245168404Spjd (state != arc_anon)) { 1246185029Spjd uint64_t *size = &state->arcs_lsize[ab->b_type]; 1247205231Skmacy list_t *list; 1248205231Skmacy kmutex_t *lock; 1249185029Spjd 1250205231Skmacy get_buf_info(ab, state, &list, &lock); 1251205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1252205231Skmacy mutex_enter(lock); 1253168404Spjd ASSERT(!list_link_active(&ab->b_arc_node)); 1254205231Skmacy list_insert_head(list, ab); 1255168404Spjd ASSERT(ab->b_datacnt > 0); 1256185029Spjd atomic_add_64(size, ab->b_size * ab->b_datacnt); 1257206794Spjd mutex_exit(lock); 1258168404Spjd } 1259168404Spjd return (cnt); 1260168404Spjd} 1261168404Spjd 1262168404Spjd/* 1263168404Spjd * Move the supplied buffer to the indicated state. The mutex 1264168404Spjd * for the buffer must be held by the caller. 1265168404Spjd */ 1266168404Spjdstatic void 1267168404Spjdarc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1268168404Spjd{ 1269168404Spjd arc_state_t *old_state = ab->b_state; 1270168404Spjd int64_t refcnt = refcount_count(&ab->b_refcnt); 1271168404Spjd uint64_t from_delta, to_delta; 1272205231Skmacy list_t *list; 1273205231Skmacy kmutex_t *lock; 1274168404Spjd 1275168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1276168404Spjd ASSERT(new_state != old_state); 1277168404Spjd ASSERT(refcnt == 0 || ab->b_datacnt > 0); 1278168404Spjd ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 1279219089Spjd ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon); 1280168404Spjd 1281168404Spjd from_delta = to_delta = ab->b_datacnt * ab->b_size; 1282168404Spjd 1283168404Spjd /* 1284168404Spjd * If this buffer is evictable, transfer it from the 1285168404Spjd * old state list to the new state list. 1286168404Spjd */ 1287168404Spjd if (refcnt == 0) { 1288168404Spjd if (old_state != arc_anon) { 1289205231Skmacy int use_mutex; 1290185029Spjd uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 1291168404Spjd 1292205231Skmacy get_buf_info(ab, old_state, &list, &lock); 1293205231Skmacy use_mutex = !MUTEX_HELD(lock); 1294168404Spjd if (use_mutex) 1295205231Skmacy mutex_enter(lock); 1296168404Spjd 1297168404Spjd ASSERT(list_link_active(&ab->b_arc_node)); 1298205231Skmacy list_remove(list, ab); 1299168404Spjd 1300168404Spjd /* 1301168404Spjd * If prefetching out of the ghost cache, 1302219089Spjd * we will have a non-zero datacnt. 1303168404Spjd */ 1304168404Spjd if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 1305168404Spjd /* ghost elements have a ghost size */ 1306168404Spjd ASSERT(ab->b_buf == NULL); 1307168404Spjd from_delta = ab->b_size; 1308168404Spjd } 1309185029Spjd ASSERT3U(*size, >=, from_delta); 1310185029Spjd atomic_add_64(size, -from_delta); 1311168404Spjd 1312168404Spjd if (use_mutex) 1313205231Skmacy mutex_exit(lock); 1314168404Spjd } 1315168404Spjd if (new_state != arc_anon) { 1316206796Spjd int use_mutex; 1317185029Spjd uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1318168404Spjd 1319205231Skmacy get_buf_info(ab, new_state, &list, &lock); 1320205231Skmacy use_mutex = !MUTEX_HELD(lock); 1321168404Spjd if (use_mutex) 1322205231Skmacy mutex_enter(lock); 1323168404Spjd 1324205231Skmacy list_insert_head(list, ab); 1325168404Spjd 1326168404Spjd /* ghost elements have a ghost size */ 1327168404Spjd if (GHOST_STATE(new_state)) { 1328168404Spjd ASSERT(ab->b_datacnt == 0); 1329168404Spjd ASSERT(ab->b_buf == NULL); 1330168404Spjd to_delta = ab->b_size; 1331168404Spjd } 1332185029Spjd atomic_add_64(size, to_delta); 1333168404Spjd 1334168404Spjd if (use_mutex) 1335205231Skmacy mutex_exit(lock); 1336168404Spjd } 1337168404Spjd } 1338168404Spjd 1339168404Spjd ASSERT(!BUF_EMPTY(ab)); 1340219089Spjd if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab)) 1341168404Spjd buf_hash_remove(ab); 1342168404Spjd 1343168404Spjd /* adjust state sizes */ 1344168404Spjd if (to_delta) 1345168404Spjd atomic_add_64(&new_state->arcs_size, to_delta); 1346168404Spjd if (from_delta) { 1347168404Spjd ASSERT3U(old_state->arcs_size, >=, from_delta); 1348168404Spjd atomic_add_64(&old_state->arcs_size, -from_delta); 1349168404Spjd } 1350168404Spjd ab->b_state = new_state; 1351185029Spjd 1352185029Spjd /* adjust l2arc hdr stats */ 1353185029Spjd if (new_state == arc_l2c_only) 1354185029Spjd l2arc_hdr_stat_add(); 1355185029Spjd else if (old_state == arc_l2c_only) 1356185029Spjd l2arc_hdr_stat_remove(); 1357168404Spjd} 1358168404Spjd 1359185029Spjdvoid 1360208373Smmarc_space_consume(uint64_t space, arc_space_type_t type) 1361185029Spjd{ 1362208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1363208373Smm 1364208373Smm switch (type) { 1365208373Smm case ARC_SPACE_DATA: 1366208373Smm ARCSTAT_INCR(arcstat_data_size, space); 1367208373Smm break; 1368208373Smm case ARC_SPACE_OTHER: 1369208373Smm ARCSTAT_INCR(arcstat_other_size, space); 1370208373Smm break; 1371208373Smm case ARC_SPACE_HDRS: 1372208373Smm ARCSTAT_INCR(arcstat_hdr_size, space); 1373208373Smm break; 1374208373Smm case ARC_SPACE_L2HDRS: 1375208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, space); 1376208373Smm break; 1377208373Smm } 1378208373Smm 1379185029Spjd atomic_add_64(&arc_meta_used, space); 1380185029Spjd atomic_add_64(&arc_size, space); 1381185029Spjd} 1382185029Spjd 1383185029Spjdvoid 1384208373Smmarc_space_return(uint64_t space, arc_space_type_t type) 1385185029Spjd{ 1386208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1387208373Smm 1388208373Smm switch (type) { 1389208373Smm case ARC_SPACE_DATA: 1390208373Smm ARCSTAT_INCR(arcstat_data_size, -space); 1391208373Smm break; 1392208373Smm case ARC_SPACE_OTHER: 1393208373Smm ARCSTAT_INCR(arcstat_other_size, -space); 1394208373Smm break; 1395208373Smm case ARC_SPACE_HDRS: 1396208373Smm ARCSTAT_INCR(arcstat_hdr_size, -space); 1397208373Smm break; 1398208373Smm case ARC_SPACE_L2HDRS: 1399208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 1400208373Smm break; 1401208373Smm } 1402208373Smm 1403185029Spjd ASSERT(arc_meta_used >= space); 1404185029Spjd if (arc_meta_max < arc_meta_used) 1405185029Spjd arc_meta_max = arc_meta_used; 1406185029Spjd atomic_add_64(&arc_meta_used, -space); 1407185029Spjd ASSERT(arc_size >= space); 1408185029Spjd atomic_add_64(&arc_size, -space); 1409185029Spjd} 1410185029Spjd 1411185029Spjdvoid * 1412185029Spjdarc_data_buf_alloc(uint64_t size) 1413185029Spjd{ 1414185029Spjd if (arc_evict_needed(ARC_BUFC_DATA)) 1415185029Spjd cv_signal(&arc_reclaim_thr_cv); 1416185029Spjd atomic_add_64(&arc_size, size); 1417185029Spjd return (zio_data_buf_alloc(size)); 1418185029Spjd} 1419185029Spjd 1420185029Spjdvoid 1421185029Spjdarc_data_buf_free(void *buf, uint64_t size) 1422185029Spjd{ 1423185029Spjd zio_data_buf_free(buf, size); 1424185029Spjd ASSERT(arc_size >= size); 1425185029Spjd atomic_add_64(&arc_size, -size); 1426185029Spjd} 1427185029Spjd 1428168404Spjdarc_buf_t * 1429168404Spjdarc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1430168404Spjd{ 1431168404Spjd arc_buf_hdr_t *hdr; 1432168404Spjd arc_buf_t *buf; 1433168404Spjd 1434168404Spjd ASSERT3U(size, >, 0); 1435185029Spjd hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1436168404Spjd ASSERT(BUF_EMPTY(hdr)); 1437168404Spjd hdr->b_size = size; 1438168404Spjd hdr->b_type = type; 1439228103Smm hdr->b_spa = spa_load_guid(spa); 1440168404Spjd hdr->b_state = arc_anon; 1441168404Spjd hdr->b_arc_access = 0; 1442185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1443168404Spjd buf->b_hdr = hdr; 1444168404Spjd buf->b_data = NULL; 1445168404Spjd buf->b_efunc = NULL; 1446168404Spjd buf->b_private = NULL; 1447168404Spjd buf->b_next = NULL; 1448168404Spjd hdr->b_buf = buf; 1449168404Spjd arc_get_data_buf(buf); 1450168404Spjd hdr->b_datacnt = 1; 1451168404Spjd hdr->b_flags = 0; 1452168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1453168404Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1454168404Spjd 1455168404Spjd return (buf); 1456168404Spjd} 1457168404Spjd 1458209962Smmstatic char *arc_onloan_tag = "onloan"; 1459209962Smm 1460209962Smm/* 1461209962Smm * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 1462209962Smm * flight data by arc_tempreserve_space() until they are "returned". Loaned 1463209962Smm * buffers must be returned to the arc before they can be used by the DMU or 1464209962Smm * freed. 1465209962Smm */ 1466209962Smmarc_buf_t * 1467209962Smmarc_loan_buf(spa_t *spa, int size) 1468209962Smm{ 1469209962Smm arc_buf_t *buf; 1470209962Smm 1471209962Smm buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 1472209962Smm 1473209962Smm atomic_add_64(&arc_loaned_bytes, size); 1474209962Smm return (buf); 1475209962Smm} 1476209962Smm 1477209962Smm/* 1478209962Smm * Return a loaned arc buffer to the arc. 1479209962Smm */ 1480209962Smmvoid 1481209962Smmarc_return_buf(arc_buf_t *buf, void *tag) 1482209962Smm{ 1483209962Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1484209962Smm 1485209962Smm ASSERT(buf->b_data != NULL); 1486219089Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1487219089Spjd (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag); 1488209962Smm 1489209962Smm atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 1490209962Smm} 1491209962Smm 1492219089Spjd/* Detach an arc_buf from a dbuf (tag) */ 1493219089Spjdvoid 1494219089Spjdarc_loan_inuse_buf(arc_buf_t *buf, void *tag) 1495219089Spjd{ 1496219089Spjd arc_buf_hdr_t *hdr; 1497219089Spjd 1498219089Spjd ASSERT(buf->b_data != NULL); 1499219089Spjd hdr = buf->b_hdr; 1500219089Spjd (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag); 1501219089Spjd (void) refcount_remove(&hdr->b_refcnt, tag); 1502219089Spjd buf->b_efunc = NULL; 1503219089Spjd buf->b_private = NULL; 1504219089Spjd 1505219089Spjd atomic_add_64(&arc_loaned_bytes, hdr->b_size); 1506219089Spjd} 1507219089Spjd 1508168404Spjdstatic arc_buf_t * 1509168404Spjdarc_buf_clone(arc_buf_t *from) 1510168404Spjd{ 1511168404Spjd arc_buf_t *buf; 1512168404Spjd arc_buf_hdr_t *hdr = from->b_hdr; 1513168404Spjd uint64_t size = hdr->b_size; 1514168404Spjd 1515219089Spjd ASSERT(hdr->b_state != arc_anon); 1516219089Spjd 1517185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1518168404Spjd buf->b_hdr = hdr; 1519168404Spjd buf->b_data = NULL; 1520168404Spjd buf->b_efunc = NULL; 1521168404Spjd buf->b_private = NULL; 1522168404Spjd buf->b_next = hdr->b_buf; 1523168404Spjd hdr->b_buf = buf; 1524168404Spjd arc_get_data_buf(buf); 1525168404Spjd bcopy(from->b_data, buf->b_data, size); 1526242845Sdelphij 1527242845Sdelphij /* 1528242845Sdelphij * This buffer already exists in the arc so create a duplicate 1529242845Sdelphij * copy for the caller. If the buffer is associated with user data 1530242845Sdelphij * then track the size and number of duplicates. These stats will be 1531242845Sdelphij * updated as duplicate buffers are created and destroyed. 1532242845Sdelphij */ 1533242845Sdelphij if (hdr->b_type == ARC_BUFC_DATA) { 1534242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_buffers); 1535242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, size); 1536242845Sdelphij } 1537168404Spjd hdr->b_datacnt += 1; 1538168404Spjd return (buf); 1539168404Spjd} 1540168404Spjd 1541168404Spjdvoid 1542168404Spjdarc_buf_add_ref(arc_buf_t *buf, void* tag) 1543168404Spjd{ 1544168404Spjd arc_buf_hdr_t *hdr; 1545168404Spjd kmutex_t *hash_lock; 1546168404Spjd 1547168404Spjd /* 1548185029Spjd * Check to see if this buffer is evicted. Callers 1549185029Spjd * must verify b_data != NULL to know if the add_ref 1550185029Spjd * was successful. 1551168404Spjd */ 1552219089Spjd mutex_enter(&buf->b_evict_lock); 1553185029Spjd if (buf->b_data == NULL) { 1554219089Spjd mutex_exit(&buf->b_evict_lock); 1555168404Spjd return; 1556168404Spjd } 1557219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1558219089Spjd mutex_enter(hash_lock); 1559185029Spjd hdr = buf->b_hdr; 1560219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1561219089Spjd mutex_exit(&buf->b_evict_lock); 1562168404Spjd 1563168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 1564168404Spjd add_reference(hdr, hash_lock, tag); 1565208373Smm DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 1566168404Spjd arc_access(hdr, hash_lock); 1567168404Spjd mutex_exit(hash_lock); 1568168404Spjd ARCSTAT_BUMP(arcstat_hits); 1569168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 1570168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 1571168404Spjd data, metadata, hits); 1572168404Spjd} 1573168404Spjd 1574185029Spjd/* 1575185029Spjd * Free the arc data buffer. If it is an l2arc write in progress, 1576185029Spjd * the buffer is placed on l2arc_free_on_write to be freed later. 1577185029Spjd */ 1578168404Spjdstatic void 1579240133Smmarc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t)) 1580185029Spjd{ 1581240133Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1582240133Smm 1583185029Spjd if (HDR_L2_WRITING(hdr)) { 1584185029Spjd l2arc_data_free_t *df; 1585185029Spjd df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 1586240133Smm df->l2df_data = buf->b_data; 1587240133Smm df->l2df_size = hdr->b_size; 1588185029Spjd df->l2df_func = free_func; 1589185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 1590185029Spjd list_insert_head(l2arc_free_on_write, df); 1591185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 1592185029Spjd ARCSTAT_BUMP(arcstat_l2_free_on_write); 1593185029Spjd } else { 1594240133Smm free_func(buf->b_data, hdr->b_size); 1595185029Spjd } 1596185029Spjd} 1597185029Spjd 1598185029Spjdstatic void 1599168404Spjdarc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 1600168404Spjd{ 1601168404Spjd arc_buf_t **bufp; 1602168404Spjd 1603168404Spjd /* free up data associated with the buf */ 1604168404Spjd if (buf->b_data) { 1605168404Spjd arc_state_t *state = buf->b_hdr->b_state; 1606168404Spjd uint64_t size = buf->b_hdr->b_size; 1607168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 1608168404Spjd 1609168404Spjd arc_cksum_verify(buf); 1610240133Smm#ifdef illumos 1611240133Smm arc_buf_unwatch(buf); 1612240133Smm#endif /* illumos */ 1613219089Spjd 1614168404Spjd if (!recycle) { 1615168404Spjd if (type == ARC_BUFC_METADATA) { 1616240133Smm arc_buf_data_free(buf, zio_buf_free); 1617208373Smm arc_space_return(size, ARC_SPACE_DATA); 1618168404Spjd } else { 1619168404Spjd ASSERT(type == ARC_BUFC_DATA); 1620240133Smm arc_buf_data_free(buf, zio_data_buf_free); 1621208373Smm ARCSTAT_INCR(arcstat_data_size, -size); 1622185029Spjd atomic_add_64(&arc_size, -size); 1623168404Spjd } 1624168404Spjd } 1625168404Spjd if (list_link_active(&buf->b_hdr->b_arc_node)) { 1626185029Spjd uint64_t *cnt = &state->arcs_lsize[type]; 1627185029Spjd 1628168404Spjd ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 1629168404Spjd ASSERT(state != arc_anon); 1630185029Spjd 1631185029Spjd ASSERT3U(*cnt, >=, size); 1632185029Spjd atomic_add_64(cnt, -size); 1633168404Spjd } 1634168404Spjd ASSERT3U(state->arcs_size, >=, size); 1635168404Spjd atomic_add_64(&state->arcs_size, -size); 1636168404Spjd buf->b_data = NULL; 1637242845Sdelphij 1638242845Sdelphij /* 1639242845Sdelphij * If we're destroying a duplicate buffer make sure 1640242845Sdelphij * that the appropriate statistics are updated. 1641242845Sdelphij */ 1642242845Sdelphij if (buf->b_hdr->b_datacnt > 1 && 1643242845Sdelphij buf->b_hdr->b_type == ARC_BUFC_DATA) { 1644242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 1645242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size); 1646242845Sdelphij } 1647168404Spjd ASSERT(buf->b_hdr->b_datacnt > 0); 1648168404Spjd buf->b_hdr->b_datacnt -= 1; 1649168404Spjd } 1650168404Spjd 1651168404Spjd /* only remove the buf if requested */ 1652168404Spjd if (!all) 1653168404Spjd return; 1654168404Spjd 1655168404Spjd /* remove the buf from the hdr list */ 1656168404Spjd for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 1657168404Spjd continue; 1658168404Spjd *bufp = buf->b_next; 1659219089Spjd buf->b_next = NULL; 1660168404Spjd 1661168404Spjd ASSERT(buf->b_efunc == NULL); 1662168404Spjd 1663168404Spjd /* clean up the buf */ 1664168404Spjd buf->b_hdr = NULL; 1665168404Spjd kmem_cache_free(buf_cache, buf); 1666168404Spjd} 1667168404Spjd 1668168404Spjdstatic void 1669168404Spjdarc_hdr_destroy(arc_buf_hdr_t *hdr) 1670168404Spjd{ 1671168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1672168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 1673168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1674219089Spjd l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 1675168404Spjd 1676219089Spjd if (l2hdr != NULL) { 1677219089Spjd boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx); 1678219089Spjd /* 1679219089Spjd * To prevent arc_free() and l2arc_evict() from 1680219089Spjd * attempting to free the same buffer at the same time, 1681219089Spjd * a FREE_IN_PROGRESS flag is given to arc_free() to 1682219089Spjd * give it priority. l2arc_evict() can't destroy this 1683219089Spjd * header while we are waiting on l2arc_buflist_mtx. 1684219089Spjd * 1685219089Spjd * The hdr may be removed from l2ad_buflist before we 1686219089Spjd * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 1687219089Spjd */ 1688219089Spjd if (!buflist_held) { 1689185029Spjd mutex_enter(&l2arc_buflist_mtx); 1690219089Spjd l2hdr = hdr->b_l2hdr; 1691219089Spjd } 1692219089Spjd 1693219089Spjd if (l2hdr != NULL) { 1694219089Spjd list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 1695219089Spjd ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 1696219089Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 1697219089Spjd if (hdr->b_state == arc_l2c_only) 1698219089Spjd l2arc_hdr_stat_remove(); 1699219089Spjd hdr->b_l2hdr = NULL; 1700219089Spjd } 1701219089Spjd 1702219089Spjd if (!buflist_held) 1703185029Spjd mutex_exit(&l2arc_buflist_mtx); 1704185029Spjd } 1705185029Spjd 1706168404Spjd if (!BUF_EMPTY(hdr)) { 1707168404Spjd ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1708219089Spjd buf_discard_identity(hdr); 1709168404Spjd } 1710168404Spjd while (hdr->b_buf) { 1711168404Spjd arc_buf_t *buf = hdr->b_buf; 1712168404Spjd 1713168404Spjd if (buf->b_efunc) { 1714168404Spjd mutex_enter(&arc_eviction_mtx); 1715219089Spjd mutex_enter(&buf->b_evict_lock); 1716168404Spjd ASSERT(buf->b_hdr != NULL); 1717168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 1718168404Spjd hdr->b_buf = buf->b_next; 1719168404Spjd buf->b_hdr = &arc_eviction_hdr; 1720168404Spjd buf->b_next = arc_eviction_list; 1721168404Spjd arc_eviction_list = buf; 1722219089Spjd mutex_exit(&buf->b_evict_lock); 1723168404Spjd mutex_exit(&arc_eviction_mtx); 1724168404Spjd } else { 1725168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 1726168404Spjd } 1727168404Spjd } 1728168404Spjd if (hdr->b_freeze_cksum != NULL) { 1729168404Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1730168404Spjd hdr->b_freeze_cksum = NULL; 1731168404Spjd } 1732219089Spjd if (hdr->b_thawed) { 1733219089Spjd kmem_free(hdr->b_thawed, 1); 1734219089Spjd hdr->b_thawed = NULL; 1735219089Spjd } 1736168404Spjd 1737168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 1738168404Spjd ASSERT3P(hdr->b_hash_next, ==, NULL); 1739168404Spjd ASSERT3P(hdr->b_acb, ==, NULL); 1740168404Spjd kmem_cache_free(hdr_cache, hdr); 1741168404Spjd} 1742168404Spjd 1743168404Spjdvoid 1744168404Spjdarc_buf_free(arc_buf_t *buf, void *tag) 1745168404Spjd{ 1746168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1747168404Spjd int hashed = hdr->b_state != arc_anon; 1748168404Spjd 1749168404Spjd ASSERT(buf->b_efunc == NULL); 1750168404Spjd ASSERT(buf->b_data != NULL); 1751168404Spjd 1752168404Spjd if (hashed) { 1753168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1754168404Spjd 1755168404Spjd mutex_enter(hash_lock); 1756219089Spjd hdr = buf->b_hdr; 1757219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1758219089Spjd 1759168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1760219089Spjd if (hdr->b_datacnt > 1) { 1761168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1762219089Spjd } else { 1763219089Spjd ASSERT(buf == hdr->b_buf); 1764219089Spjd ASSERT(buf->b_efunc == NULL); 1765168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 1766219089Spjd } 1767168404Spjd mutex_exit(hash_lock); 1768168404Spjd } else if (HDR_IO_IN_PROGRESS(hdr)) { 1769168404Spjd int destroy_hdr; 1770168404Spjd /* 1771168404Spjd * We are in the middle of an async write. Don't destroy 1772168404Spjd * this buffer unless the write completes before we finish 1773168404Spjd * decrementing the reference count. 1774168404Spjd */ 1775168404Spjd mutex_enter(&arc_eviction_mtx); 1776168404Spjd (void) remove_reference(hdr, NULL, tag); 1777168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1778168404Spjd destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 1779168404Spjd mutex_exit(&arc_eviction_mtx); 1780168404Spjd if (destroy_hdr) 1781168404Spjd arc_hdr_destroy(hdr); 1782168404Spjd } else { 1783219089Spjd if (remove_reference(hdr, NULL, tag) > 0) 1784168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1785219089Spjd else 1786168404Spjd arc_hdr_destroy(hdr); 1787168404Spjd } 1788168404Spjd} 1789168404Spjd 1790168404Spjdint 1791168404Spjdarc_buf_remove_ref(arc_buf_t *buf, void* tag) 1792168404Spjd{ 1793168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1794168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1795168404Spjd int no_callback = (buf->b_efunc == NULL); 1796168404Spjd 1797168404Spjd if (hdr->b_state == arc_anon) { 1798219089Spjd ASSERT(hdr->b_datacnt == 1); 1799168404Spjd arc_buf_free(buf, tag); 1800168404Spjd return (no_callback); 1801168404Spjd } 1802168404Spjd 1803168404Spjd mutex_enter(hash_lock); 1804219089Spjd hdr = buf->b_hdr; 1805219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1806168404Spjd ASSERT(hdr->b_state != arc_anon); 1807168404Spjd ASSERT(buf->b_data != NULL); 1808168404Spjd 1809168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1810168404Spjd if (hdr->b_datacnt > 1) { 1811168404Spjd if (no_callback) 1812168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1813168404Spjd } else if (no_callback) { 1814168404Spjd ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 1815219089Spjd ASSERT(buf->b_efunc == NULL); 1816168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 1817168404Spjd } 1818168404Spjd ASSERT(no_callback || hdr->b_datacnt > 1 || 1819168404Spjd refcount_is_zero(&hdr->b_refcnt)); 1820168404Spjd mutex_exit(hash_lock); 1821168404Spjd return (no_callback); 1822168404Spjd} 1823168404Spjd 1824168404Spjdint 1825168404Spjdarc_buf_size(arc_buf_t *buf) 1826168404Spjd{ 1827168404Spjd return (buf->b_hdr->b_size); 1828168404Spjd} 1829168404Spjd 1830168404Spjd/* 1831242845Sdelphij * Called from the DMU to determine if the current buffer should be 1832242845Sdelphij * evicted. In order to ensure proper locking, the eviction must be initiated 1833242845Sdelphij * from the DMU. Return true if the buffer is associated with user data and 1834242845Sdelphij * duplicate buffers still exist. 1835242845Sdelphij */ 1836242845Sdelphijboolean_t 1837242845Sdelphijarc_buf_eviction_needed(arc_buf_t *buf) 1838242845Sdelphij{ 1839242845Sdelphij arc_buf_hdr_t *hdr; 1840242845Sdelphij boolean_t evict_needed = B_FALSE; 1841242845Sdelphij 1842242845Sdelphij if (zfs_disable_dup_eviction) 1843242845Sdelphij return (B_FALSE); 1844242845Sdelphij 1845242845Sdelphij mutex_enter(&buf->b_evict_lock); 1846242845Sdelphij hdr = buf->b_hdr; 1847242845Sdelphij if (hdr == NULL) { 1848242845Sdelphij /* 1849242845Sdelphij * We are in arc_do_user_evicts(); let that function 1850242845Sdelphij * perform the eviction. 1851242845Sdelphij */ 1852242845Sdelphij ASSERT(buf->b_data == NULL); 1853242845Sdelphij mutex_exit(&buf->b_evict_lock); 1854242845Sdelphij return (B_FALSE); 1855242845Sdelphij } else if (buf->b_data == NULL) { 1856242845Sdelphij /* 1857242845Sdelphij * We have already been added to the arc eviction list; 1858242845Sdelphij * recommend eviction. 1859242845Sdelphij */ 1860242845Sdelphij ASSERT3P(hdr, ==, &arc_eviction_hdr); 1861242845Sdelphij mutex_exit(&buf->b_evict_lock); 1862242845Sdelphij return (B_TRUE); 1863242845Sdelphij } 1864242845Sdelphij 1865242845Sdelphij if (hdr->b_datacnt > 1 && hdr->b_type == ARC_BUFC_DATA) 1866242845Sdelphij evict_needed = B_TRUE; 1867242845Sdelphij 1868242845Sdelphij mutex_exit(&buf->b_evict_lock); 1869242845Sdelphij return (evict_needed); 1870242845Sdelphij} 1871242845Sdelphij 1872242845Sdelphij/* 1873168404Spjd * Evict buffers from list until we've removed the specified number of 1874168404Spjd * bytes. Move the removed buffers to the appropriate evict state. 1875168404Spjd * If the recycle flag is set, then attempt to "recycle" a buffer: 1876168404Spjd * - look for a buffer to evict that is `bytes' long. 1877168404Spjd * - return the data block from this buffer rather than freeing it. 1878168404Spjd * This flag is used by callers that are trying to make space for a 1879168404Spjd * new buffer in a full arc cache. 1880185029Spjd * 1881185029Spjd * This function makes a "best effort". It skips over any buffers 1882185029Spjd * it can't get a hash_lock on, and so may not catch all candidates. 1883185029Spjd * It may also return without evicting as much space as requested. 1884168404Spjd */ 1885168404Spjdstatic void * 1886209962Smmarc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 1887168404Spjd arc_buf_contents_t type) 1888168404Spjd{ 1889168404Spjd arc_state_t *evicted_state; 1890168404Spjd uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 1891205231Skmacy int64_t bytes_remaining; 1892168404Spjd arc_buf_hdr_t *ab, *ab_prev = NULL; 1893205231Skmacy list_t *evicted_list, *list, *evicted_list_start, *list_start; 1894205231Skmacy kmutex_t *lock, *evicted_lock; 1895168404Spjd kmutex_t *hash_lock; 1896168404Spjd boolean_t have_lock; 1897168404Spjd void *stolen = NULL; 1898205231Skmacy static int evict_metadata_offset, evict_data_offset; 1899205231Skmacy int i, idx, offset, list_count, count; 1900168404Spjd 1901168404Spjd ASSERT(state == arc_mru || state == arc_mfu); 1902168404Spjd 1903168404Spjd evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1904206796Spjd 1905205231Skmacy if (type == ARC_BUFC_METADATA) { 1906205231Skmacy offset = 0; 1907205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 1908205231Skmacy list_start = &state->arcs_lists[0]; 1909205231Skmacy evicted_list_start = &evicted_state->arcs_lists[0]; 1910205231Skmacy idx = evict_metadata_offset; 1911205231Skmacy } else { 1912205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 1913205231Skmacy list_start = &state->arcs_lists[offset]; 1914205231Skmacy evicted_list_start = &evicted_state->arcs_lists[offset]; 1915205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 1916205231Skmacy idx = evict_data_offset; 1917205231Skmacy } 1918205231Skmacy bytes_remaining = evicted_state->arcs_lsize[type]; 1919205231Skmacy count = 0; 1920206796Spjd 1921205231Skmacyevict_start: 1922205231Skmacy list = &list_start[idx]; 1923205231Skmacy evicted_list = &evicted_list_start[idx]; 1924205231Skmacy lock = ARCS_LOCK(state, (offset + idx)); 1925206796Spjd evicted_lock = ARCS_LOCK(evicted_state, (offset + idx)); 1926168404Spjd 1927205231Skmacy mutex_enter(lock); 1928205231Skmacy mutex_enter(evicted_lock); 1929205231Skmacy 1930185029Spjd for (ab = list_tail(list); ab; ab = ab_prev) { 1931185029Spjd ab_prev = list_prev(list, ab); 1932205231Skmacy bytes_remaining -= (ab->b_size * ab->b_datacnt); 1933168404Spjd /* prefetch buffers have a minimum lifespan */ 1934168404Spjd if (HDR_IO_IN_PROGRESS(ab) || 1935185029Spjd (spa && ab->b_spa != spa) || 1936168404Spjd (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 1937219089Spjd ddi_get_lbolt() - ab->b_arc_access < 1938219089Spjd arc_min_prefetch_lifespan)) { 1939168404Spjd skipped++; 1940168404Spjd continue; 1941168404Spjd } 1942168404Spjd /* "lookahead" for better eviction candidate */ 1943168404Spjd if (recycle && ab->b_size != bytes && 1944168404Spjd ab_prev && ab_prev->b_size == bytes) 1945168404Spjd continue; 1946168404Spjd hash_lock = HDR_LOCK(ab); 1947168404Spjd have_lock = MUTEX_HELD(hash_lock); 1948168404Spjd if (have_lock || mutex_tryenter(hash_lock)) { 1949240415Smm ASSERT0(refcount_count(&ab->b_refcnt)); 1950168404Spjd ASSERT(ab->b_datacnt > 0); 1951168404Spjd while (ab->b_buf) { 1952168404Spjd arc_buf_t *buf = ab->b_buf; 1953219089Spjd if (!mutex_tryenter(&buf->b_evict_lock)) { 1954185029Spjd missed += 1; 1955185029Spjd break; 1956185029Spjd } 1957168404Spjd if (buf->b_data) { 1958168404Spjd bytes_evicted += ab->b_size; 1959168404Spjd if (recycle && ab->b_type == type && 1960185029Spjd ab->b_size == bytes && 1961185029Spjd !HDR_L2_WRITING(ab)) { 1962168404Spjd stolen = buf->b_data; 1963168404Spjd recycle = FALSE; 1964168404Spjd } 1965168404Spjd } 1966168404Spjd if (buf->b_efunc) { 1967168404Spjd mutex_enter(&arc_eviction_mtx); 1968168404Spjd arc_buf_destroy(buf, 1969168404Spjd buf->b_data == stolen, FALSE); 1970168404Spjd ab->b_buf = buf->b_next; 1971168404Spjd buf->b_hdr = &arc_eviction_hdr; 1972168404Spjd buf->b_next = arc_eviction_list; 1973168404Spjd arc_eviction_list = buf; 1974168404Spjd mutex_exit(&arc_eviction_mtx); 1975219089Spjd mutex_exit(&buf->b_evict_lock); 1976168404Spjd } else { 1977219089Spjd mutex_exit(&buf->b_evict_lock); 1978168404Spjd arc_buf_destroy(buf, 1979168404Spjd buf->b_data == stolen, TRUE); 1980168404Spjd } 1981168404Spjd } 1982208373Smm 1983208373Smm if (ab->b_l2hdr) { 1984208373Smm ARCSTAT_INCR(arcstat_evict_l2_cached, 1985208373Smm ab->b_size); 1986208373Smm } else { 1987208373Smm if (l2arc_write_eligible(ab->b_spa, ab)) { 1988208373Smm ARCSTAT_INCR(arcstat_evict_l2_eligible, 1989208373Smm ab->b_size); 1990208373Smm } else { 1991208373Smm ARCSTAT_INCR( 1992208373Smm arcstat_evict_l2_ineligible, 1993208373Smm ab->b_size); 1994208373Smm } 1995208373Smm } 1996208373Smm 1997185029Spjd if (ab->b_datacnt == 0) { 1998185029Spjd arc_change_state(evicted_state, ab, hash_lock); 1999185029Spjd ASSERT(HDR_IN_HASH_TABLE(ab)); 2000185029Spjd ab->b_flags |= ARC_IN_HASH_TABLE; 2001185029Spjd ab->b_flags &= ~ARC_BUF_AVAILABLE; 2002185029Spjd DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 2003185029Spjd } 2004168404Spjd if (!have_lock) 2005168404Spjd mutex_exit(hash_lock); 2006168404Spjd if (bytes >= 0 && bytes_evicted >= bytes) 2007168404Spjd break; 2008205231Skmacy if (bytes_remaining > 0) { 2009205231Skmacy mutex_exit(evicted_lock); 2010205231Skmacy mutex_exit(lock); 2011206796Spjd idx = ((idx + 1) & (list_count - 1)); 2012205231Skmacy count++; 2013205231Skmacy goto evict_start; 2014205231Skmacy } 2015168404Spjd } else { 2016168404Spjd missed += 1; 2017168404Spjd } 2018168404Spjd } 2019168404Spjd 2020205231Skmacy mutex_exit(evicted_lock); 2021205231Skmacy mutex_exit(lock); 2022206796Spjd 2023206796Spjd idx = ((idx + 1) & (list_count - 1)); 2024205231Skmacy count++; 2025168404Spjd 2026205231Skmacy if (bytes_evicted < bytes) { 2027205231Skmacy if (count < list_count) 2028205231Skmacy goto evict_start; 2029205231Skmacy else 2030205231Skmacy dprintf("only evicted %lld bytes from %x", 2031205231Skmacy (longlong_t)bytes_evicted, state); 2032205231Skmacy } 2033206796Spjd if (type == ARC_BUFC_METADATA) 2034205231Skmacy evict_metadata_offset = idx; 2035205231Skmacy else 2036205231Skmacy evict_data_offset = idx; 2037206796Spjd 2038168404Spjd if (skipped) 2039168404Spjd ARCSTAT_INCR(arcstat_evict_skip, skipped); 2040168404Spjd 2041168404Spjd if (missed) 2042168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, missed); 2043168404Spjd 2044185029Spjd /* 2045185029Spjd * We have just evicted some date into the ghost state, make 2046185029Spjd * sure we also adjust the ghost state size if necessary. 2047185029Spjd */ 2048185029Spjd if (arc_no_grow && 2049185029Spjd arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 2050185029Spjd int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 2051185029Spjd arc_mru_ghost->arcs_size - arc_c; 2052185029Spjd 2053185029Spjd if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 2054185029Spjd int64_t todelete = 2055185029Spjd MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 2056209962Smm arc_evict_ghost(arc_mru_ghost, 0, todelete); 2057185029Spjd } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 2058185029Spjd int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 2059185029Spjd arc_mru_ghost->arcs_size + 2060185029Spjd arc_mfu_ghost->arcs_size - arc_c); 2061209962Smm arc_evict_ghost(arc_mfu_ghost, 0, todelete); 2062185029Spjd } 2063185029Spjd } 2064205231Skmacy if (stolen) 2065205231Skmacy ARCSTAT_BUMP(arcstat_stolen); 2066185029Spjd 2067168404Spjd return (stolen); 2068168404Spjd} 2069168404Spjd 2070168404Spjd/* 2071168404Spjd * Remove buffers from list until we've removed the specified number of 2072168404Spjd * bytes. Destroy the buffers that are removed. 2073168404Spjd */ 2074168404Spjdstatic void 2075209962Smmarc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 2076168404Spjd{ 2077168404Spjd arc_buf_hdr_t *ab, *ab_prev; 2078219089Spjd arc_buf_hdr_t marker = { 0 }; 2079205231Skmacy list_t *list, *list_start; 2080205231Skmacy kmutex_t *hash_lock, *lock; 2081168404Spjd uint64_t bytes_deleted = 0; 2082168404Spjd uint64_t bufs_skipped = 0; 2083205231Skmacy static int evict_offset; 2084205231Skmacy int list_count, idx = evict_offset; 2085205231Skmacy int offset, count = 0; 2086168404Spjd 2087168404Spjd ASSERT(GHOST_STATE(state)); 2088205231Skmacy 2089205231Skmacy /* 2090205231Skmacy * data lists come after metadata lists 2091205231Skmacy */ 2092205231Skmacy list_start = &state->arcs_lists[ARC_BUFC_NUMMETADATALISTS]; 2093205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 2094205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 2095206796Spjd 2096205231Skmacyevict_start: 2097205231Skmacy list = &list_start[idx]; 2098205231Skmacy lock = ARCS_LOCK(state, idx + offset); 2099205231Skmacy 2100205231Skmacy mutex_enter(lock); 2101185029Spjd for (ab = list_tail(list); ab; ab = ab_prev) { 2102185029Spjd ab_prev = list_prev(list, ab); 2103185029Spjd if (spa && ab->b_spa != spa) 2104185029Spjd continue; 2105219089Spjd 2106219089Spjd /* ignore markers */ 2107219089Spjd if (ab->b_spa == 0) 2108219089Spjd continue; 2109219089Spjd 2110168404Spjd hash_lock = HDR_LOCK(ab); 2111219089Spjd /* caller may be trying to modify this buffer, skip it */ 2112219089Spjd if (MUTEX_HELD(hash_lock)) 2113219089Spjd continue; 2114168404Spjd if (mutex_tryenter(hash_lock)) { 2115168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(ab)); 2116168404Spjd ASSERT(ab->b_buf == NULL); 2117168404Spjd ARCSTAT_BUMP(arcstat_deleted); 2118168404Spjd bytes_deleted += ab->b_size; 2119185029Spjd 2120185029Spjd if (ab->b_l2hdr != NULL) { 2121185029Spjd /* 2122185029Spjd * This buffer is cached on the 2nd Level ARC; 2123185029Spjd * don't destroy the header. 2124185029Spjd */ 2125185029Spjd arc_change_state(arc_l2c_only, ab, hash_lock); 2126185029Spjd mutex_exit(hash_lock); 2127185029Spjd } else { 2128185029Spjd arc_change_state(arc_anon, ab, hash_lock); 2129185029Spjd mutex_exit(hash_lock); 2130185029Spjd arc_hdr_destroy(ab); 2131185029Spjd } 2132185029Spjd 2133168404Spjd DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 2134168404Spjd if (bytes >= 0 && bytes_deleted >= bytes) 2135168404Spjd break; 2136219089Spjd } else if (bytes < 0) { 2137219089Spjd /* 2138219089Spjd * Insert a list marker and then wait for the 2139219089Spjd * hash lock to become available. Once its 2140219089Spjd * available, restart from where we left off. 2141219089Spjd */ 2142219089Spjd list_insert_after(list, ab, &marker); 2143219089Spjd mutex_exit(lock); 2144219089Spjd mutex_enter(hash_lock); 2145219089Spjd mutex_exit(hash_lock); 2146219089Spjd mutex_enter(lock); 2147219089Spjd ab_prev = list_prev(list, &marker); 2148219089Spjd list_remove(list, &marker); 2149219089Spjd } else 2150168404Spjd bufs_skipped += 1; 2151168404Spjd } 2152205231Skmacy mutex_exit(lock); 2153206796Spjd idx = ((idx + 1) & (ARC_BUFC_NUMDATALISTS - 1)); 2154205231Skmacy count++; 2155206796Spjd 2156205231Skmacy if (count < list_count) 2157205231Skmacy goto evict_start; 2158206796Spjd 2159205231Skmacy evict_offset = idx; 2160205231Skmacy if ((uintptr_t)list > (uintptr_t)&state->arcs_lists[ARC_BUFC_NUMMETADATALISTS] && 2161185029Spjd (bytes < 0 || bytes_deleted < bytes)) { 2162205231Skmacy list_start = &state->arcs_lists[0]; 2163205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 2164205231Skmacy offset = count = 0; 2165205231Skmacy goto evict_start; 2166185029Spjd } 2167185029Spjd 2168168404Spjd if (bufs_skipped) { 2169168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 2170168404Spjd ASSERT(bytes >= 0); 2171168404Spjd } 2172168404Spjd 2173168404Spjd if (bytes_deleted < bytes) 2174168404Spjd dprintf("only deleted %lld bytes from %p", 2175168404Spjd (longlong_t)bytes_deleted, state); 2176168404Spjd} 2177168404Spjd 2178168404Spjdstatic void 2179168404Spjdarc_adjust(void) 2180168404Spjd{ 2181208373Smm int64_t adjustment, delta; 2182168404Spjd 2183208373Smm /* 2184208373Smm * Adjust MRU size 2185208373Smm */ 2186168404Spjd 2187209275Smm adjustment = MIN((int64_t)(arc_size - arc_c), 2188209275Smm (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - 2189209275Smm arc_p)); 2190208373Smm 2191208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 2192208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 2193209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA); 2194208373Smm adjustment -= delta; 2195168404Spjd } 2196168404Spjd 2197208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2198208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 2199209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, 2200185029Spjd ARC_BUFC_METADATA); 2201185029Spjd } 2202185029Spjd 2203208373Smm /* 2204208373Smm * Adjust MFU size 2205208373Smm */ 2206168404Spjd 2207208373Smm adjustment = arc_size - arc_c; 2208208373Smm 2209208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 2210208373Smm delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 2211209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA); 2212208373Smm adjustment -= delta; 2213168404Spjd } 2214168404Spjd 2215208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2216208373Smm int64_t delta = MIN(adjustment, 2217208373Smm arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 2218209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, 2219208373Smm ARC_BUFC_METADATA); 2220208373Smm } 2221168404Spjd 2222208373Smm /* 2223208373Smm * Adjust ghost lists 2224208373Smm */ 2225168404Spjd 2226208373Smm adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 2227168404Spjd 2228208373Smm if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 2229208373Smm delta = MIN(arc_mru_ghost->arcs_size, adjustment); 2230209962Smm arc_evict_ghost(arc_mru_ghost, 0, delta); 2231208373Smm } 2232185029Spjd 2233208373Smm adjustment = 2234208373Smm arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 2235208373Smm 2236208373Smm if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 2237208373Smm delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 2238209962Smm arc_evict_ghost(arc_mfu_ghost, 0, delta); 2239168404Spjd } 2240168404Spjd} 2241168404Spjd 2242168404Spjdstatic void 2243168404Spjdarc_do_user_evicts(void) 2244168404Spjd{ 2245191903Skmacy static arc_buf_t *tmp_arc_eviction_list; 2246191903Skmacy 2247191903Skmacy /* 2248191903Skmacy * Move list over to avoid LOR 2249191903Skmacy */ 2250206796Spjdrestart: 2251168404Spjd mutex_enter(&arc_eviction_mtx); 2252191903Skmacy tmp_arc_eviction_list = arc_eviction_list; 2253191903Skmacy arc_eviction_list = NULL; 2254191903Skmacy mutex_exit(&arc_eviction_mtx); 2255191903Skmacy 2256191903Skmacy while (tmp_arc_eviction_list != NULL) { 2257191903Skmacy arc_buf_t *buf = tmp_arc_eviction_list; 2258191903Skmacy tmp_arc_eviction_list = buf->b_next; 2259219089Spjd mutex_enter(&buf->b_evict_lock); 2260168404Spjd buf->b_hdr = NULL; 2261219089Spjd mutex_exit(&buf->b_evict_lock); 2262168404Spjd 2263168404Spjd if (buf->b_efunc != NULL) 2264168404Spjd VERIFY(buf->b_efunc(buf) == 0); 2265168404Spjd 2266168404Spjd buf->b_efunc = NULL; 2267168404Spjd buf->b_private = NULL; 2268168404Spjd kmem_cache_free(buf_cache, buf); 2269168404Spjd } 2270191903Skmacy 2271191903Skmacy if (arc_eviction_list != NULL) 2272191903Skmacy goto restart; 2273168404Spjd} 2274168404Spjd 2275168404Spjd/* 2276185029Spjd * Flush all *evictable* data from the cache for the given spa. 2277168404Spjd * NOTE: this will not touch "active" (i.e. referenced) data. 2278168404Spjd */ 2279168404Spjdvoid 2280185029Spjdarc_flush(spa_t *spa) 2281168404Spjd{ 2282209962Smm uint64_t guid = 0; 2283209962Smm 2284209962Smm if (spa) 2285228103Smm guid = spa_load_guid(spa); 2286209962Smm 2287205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_DATA]) { 2288209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 2289185029Spjd if (spa) 2290185029Spjd break; 2291185029Spjd } 2292205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_METADATA]) { 2293209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 2294185029Spjd if (spa) 2295185029Spjd break; 2296185029Spjd } 2297205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_DATA]) { 2298209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 2299185029Spjd if (spa) 2300185029Spjd break; 2301185029Spjd } 2302205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_METADATA]) { 2303209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 2304185029Spjd if (spa) 2305185029Spjd break; 2306185029Spjd } 2307168404Spjd 2308209962Smm arc_evict_ghost(arc_mru_ghost, guid, -1); 2309209962Smm arc_evict_ghost(arc_mfu_ghost, guid, -1); 2310168404Spjd 2311168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2312168404Spjd arc_do_user_evicts(); 2313168404Spjd mutex_exit(&arc_reclaim_thr_lock); 2314185029Spjd ASSERT(spa || arc_eviction_list == NULL); 2315168404Spjd} 2316168404Spjd 2317168404Spjdvoid 2318168404Spjdarc_shrink(void) 2319168404Spjd{ 2320168404Spjd if (arc_c > arc_c_min) { 2321168404Spjd uint64_t to_free; 2322168404Spjd 2323168404Spjd#ifdef _KERNEL 2324168404Spjd to_free = arc_c >> arc_shrink_shift; 2325168404Spjd#else 2326168404Spjd to_free = arc_c >> arc_shrink_shift; 2327168404Spjd#endif 2328168404Spjd if (arc_c > arc_c_min + to_free) 2329168404Spjd atomic_add_64(&arc_c, -to_free); 2330168404Spjd else 2331168404Spjd arc_c = arc_c_min; 2332168404Spjd 2333168404Spjd atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 2334168404Spjd if (arc_c > arc_size) 2335168404Spjd arc_c = MAX(arc_size, arc_c_min); 2336168404Spjd if (arc_p > arc_c) 2337168404Spjd arc_p = (arc_c >> 1); 2338168404Spjd ASSERT(arc_c >= arc_c_min); 2339168404Spjd ASSERT((int64_t)arc_p >= 0); 2340168404Spjd } 2341168404Spjd 2342168404Spjd if (arc_size > arc_c) 2343168404Spjd arc_adjust(); 2344168404Spjd} 2345168404Spjd 2346185029Spjdstatic int needfree = 0; 2347168404Spjd 2348168404Spjdstatic int 2349168404Spjdarc_reclaim_needed(void) 2350168404Spjd{ 2351168404Spjd 2352168404Spjd#ifdef _KERNEL 2353219089Spjd 2354197816Skmacy if (needfree) 2355197816Skmacy return (1); 2356168404Spjd 2357191902Skmacy /* 2358212780Savg * Cooperate with pagedaemon when it's time for it to scan 2359212780Savg * and reclaim some pages. 2360191902Skmacy */ 2361212783Savg if (vm_paging_needed()) 2362191902Skmacy return (1); 2363191902Skmacy 2364219089Spjd#ifdef sun 2365168404Spjd /* 2366185029Spjd * take 'desfree' extra pages, so we reclaim sooner, rather than later 2367185029Spjd */ 2368185029Spjd extra = desfree; 2369185029Spjd 2370185029Spjd /* 2371185029Spjd * check that we're out of range of the pageout scanner. It starts to 2372185029Spjd * schedule paging if freemem is less than lotsfree and needfree. 2373185029Spjd * lotsfree is the high-water mark for pageout, and needfree is the 2374185029Spjd * number of needed free pages. We add extra pages here to make sure 2375185029Spjd * the scanner doesn't start up while we're freeing memory. 2376185029Spjd */ 2377185029Spjd if (freemem < lotsfree + needfree + extra) 2378185029Spjd return (1); 2379185029Spjd 2380185029Spjd /* 2381168404Spjd * check to make sure that swapfs has enough space so that anon 2382185029Spjd * reservations can still succeed. anon_resvmem() checks that the 2383168404Spjd * availrmem is greater than swapfs_minfree, and the number of reserved 2384168404Spjd * swap pages. We also add a bit of extra here just to prevent 2385168404Spjd * circumstances from getting really dire. 2386168404Spjd */ 2387168404Spjd if (availrmem < swapfs_minfree + swapfs_reserve + extra) 2388168404Spjd return (1); 2389168404Spjd 2390168404Spjd#if defined(__i386) 2391168404Spjd /* 2392168404Spjd * If we're on an i386 platform, it's possible that we'll exhaust the 2393168404Spjd * kernel heap space before we ever run out of available physical 2394168404Spjd * memory. Most checks of the size of the heap_area compare against 2395168404Spjd * tune.t_minarmem, which is the minimum available real memory that we 2396168404Spjd * can have in the system. However, this is generally fixed at 25 pages 2397168404Spjd * which is so low that it's useless. In this comparison, we seek to 2398168404Spjd * calculate the total heap-size, and reclaim if more than 3/4ths of the 2399185029Spjd * heap is allocated. (Or, in the calculation, if less than 1/4th is 2400168404Spjd * free) 2401168404Spjd */ 2402168404Spjd if (btop(vmem_size(heap_arena, VMEM_FREE)) < 2403168404Spjd (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 2404168404Spjd return (1); 2405168404Spjd#endif 2406219089Spjd#else /* !sun */ 2407175633Spjd if (kmem_used() > (kmem_size() * 3) / 4) 2408168404Spjd return (1); 2409219089Spjd#endif /* sun */ 2410168404Spjd 2411168404Spjd#else 2412168404Spjd if (spa_get_random(100) == 0) 2413168404Spjd return (1); 2414168404Spjd#endif 2415168404Spjd return (0); 2416168404Spjd} 2417168404Spjd 2418208454Spjdextern kmem_cache_t *zio_buf_cache[]; 2419208454Spjdextern kmem_cache_t *zio_data_buf_cache[]; 2420208454Spjd 2421168404Spjdstatic void 2422168404Spjdarc_kmem_reap_now(arc_reclaim_strategy_t strat) 2423168404Spjd{ 2424168404Spjd size_t i; 2425168404Spjd kmem_cache_t *prev_cache = NULL; 2426168404Spjd kmem_cache_t *prev_data_cache = NULL; 2427168404Spjd 2428168404Spjd#ifdef _KERNEL 2429185029Spjd if (arc_meta_used >= arc_meta_limit) { 2430185029Spjd /* 2431185029Spjd * We are exceeding our meta-data cache limit. 2432185029Spjd * Purge some DNLC entries to release holds on meta-data. 2433185029Spjd */ 2434185029Spjd dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 2435185029Spjd } 2436168404Spjd#if defined(__i386) 2437168404Spjd /* 2438168404Spjd * Reclaim unused memory from all kmem caches. 2439168404Spjd */ 2440168404Spjd kmem_reap(); 2441168404Spjd#endif 2442168404Spjd#endif 2443168404Spjd 2444168404Spjd /* 2445185029Spjd * An aggressive reclamation will shrink the cache size as well as 2446168404Spjd * reap free buffers from the arc kmem caches. 2447168404Spjd */ 2448168404Spjd if (strat == ARC_RECLAIM_AGGR) 2449168404Spjd arc_shrink(); 2450168404Spjd 2451168404Spjd for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2452168404Spjd if (zio_buf_cache[i] != prev_cache) { 2453168404Spjd prev_cache = zio_buf_cache[i]; 2454168404Spjd kmem_cache_reap_now(zio_buf_cache[i]); 2455168404Spjd } 2456168404Spjd if (zio_data_buf_cache[i] != prev_data_cache) { 2457168404Spjd prev_data_cache = zio_data_buf_cache[i]; 2458168404Spjd kmem_cache_reap_now(zio_data_buf_cache[i]); 2459168404Spjd } 2460168404Spjd } 2461168404Spjd kmem_cache_reap_now(buf_cache); 2462168404Spjd kmem_cache_reap_now(hdr_cache); 2463168404Spjd} 2464168404Spjd 2465168404Spjdstatic void 2466168404Spjdarc_reclaim_thread(void *dummy __unused) 2467168404Spjd{ 2468168404Spjd clock_t growtime = 0; 2469168404Spjd arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2470168404Spjd callb_cpr_t cpr; 2471168404Spjd 2472168404Spjd CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2473168404Spjd 2474168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2475168404Spjd while (arc_thread_exit == 0) { 2476168404Spjd if (arc_reclaim_needed()) { 2477168404Spjd 2478168404Spjd if (arc_no_grow) { 2479168404Spjd if (last_reclaim == ARC_RECLAIM_CONS) { 2480168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2481168404Spjd } else { 2482168404Spjd last_reclaim = ARC_RECLAIM_CONS; 2483168404Spjd } 2484168404Spjd } else { 2485168404Spjd arc_no_grow = TRUE; 2486168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2487168404Spjd membar_producer(); 2488168404Spjd } 2489168404Spjd 2490168404Spjd /* reset the growth delay for every reclaim */ 2491219089Spjd growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 2492168404Spjd 2493185029Spjd if (needfree && last_reclaim == ARC_RECLAIM_CONS) { 2494168404Spjd /* 2495185029Spjd * If needfree is TRUE our vm_lowmem hook 2496168404Spjd * was called and in that case we must free some 2497168404Spjd * memory, so switch to aggressive mode. 2498168404Spjd */ 2499168404Spjd arc_no_grow = TRUE; 2500168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2501168404Spjd } 2502168404Spjd arc_kmem_reap_now(last_reclaim); 2503185029Spjd arc_warm = B_TRUE; 2504185029Spjd 2505219089Spjd } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 2506168404Spjd arc_no_grow = FALSE; 2507168404Spjd } 2508168404Spjd 2509209275Smm arc_adjust(); 2510168404Spjd 2511168404Spjd if (arc_eviction_list != NULL) 2512168404Spjd arc_do_user_evicts(); 2513168404Spjd 2514211762Savg#ifdef _KERNEL 2515211762Savg if (needfree) { 2516185029Spjd needfree = 0; 2517185029Spjd wakeup(&needfree); 2518211762Savg } 2519168404Spjd#endif 2520168404Spjd 2521168404Spjd /* block until needed, or one second, whichever is shorter */ 2522168404Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 2523168404Spjd (void) cv_timedwait(&arc_reclaim_thr_cv, 2524168404Spjd &arc_reclaim_thr_lock, hz); 2525168404Spjd CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2526168404Spjd } 2527168404Spjd 2528168404Spjd arc_thread_exit = 0; 2529168404Spjd cv_broadcast(&arc_reclaim_thr_cv); 2530168404Spjd CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2531168404Spjd thread_exit(); 2532168404Spjd} 2533168404Spjd 2534168404Spjd/* 2535168404Spjd * Adapt arc info given the number of bytes we are trying to add and 2536168404Spjd * the state that we are comming from. This function is only called 2537168404Spjd * when we are adding new content to the cache. 2538168404Spjd */ 2539168404Spjdstatic void 2540168404Spjdarc_adapt(int bytes, arc_state_t *state) 2541168404Spjd{ 2542168404Spjd int mult; 2543208373Smm uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 2544168404Spjd 2545185029Spjd if (state == arc_l2c_only) 2546185029Spjd return; 2547185029Spjd 2548168404Spjd ASSERT(bytes > 0); 2549168404Spjd /* 2550168404Spjd * Adapt the target size of the MRU list: 2551168404Spjd * - if we just hit in the MRU ghost list, then increase 2552168404Spjd * the target size of the MRU list. 2553168404Spjd * - if we just hit in the MFU ghost list, then increase 2554168404Spjd * the target size of the MFU list by decreasing the 2555168404Spjd * target size of the MRU list. 2556168404Spjd */ 2557168404Spjd if (state == arc_mru_ghost) { 2558168404Spjd mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 2559168404Spjd 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 2560209275Smm mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 2561168404Spjd 2562208373Smm arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 2563168404Spjd } else if (state == arc_mfu_ghost) { 2564208373Smm uint64_t delta; 2565208373Smm 2566168404Spjd mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 2567168404Spjd 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 2568209275Smm mult = MIN(mult, 10); 2569168404Spjd 2570208373Smm delta = MIN(bytes * mult, arc_p); 2571208373Smm arc_p = MAX(arc_p_min, arc_p - delta); 2572168404Spjd } 2573168404Spjd ASSERT((int64_t)arc_p >= 0); 2574168404Spjd 2575168404Spjd if (arc_reclaim_needed()) { 2576168404Spjd cv_signal(&arc_reclaim_thr_cv); 2577168404Spjd return; 2578168404Spjd } 2579168404Spjd 2580168404Spjd if (arc_no_grow) 2581168404Spjd return; 2582168404Spjd 2583168404Spjd if (arc_c >= arc_c_max) 2584168404Spjd return; 2585168404Spjd 2586168404Spjd /* 2587168404Spjd * If we're within (2 * maxblocksize) bytes of the target 2588168404Spjd * cache size, increment the target cache size 2589168404Spjd */ 2590168404Spjd if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 2591168404Spjd atomic_add_64(&arc_c, (int64_t)bytes); 2592168404Spjd if (arc_c > arc_c_max) 2593168404Spjd arc_c = arc_c_max; 2594168404Spjd else if (state == arc_anon) 2595168404Spjd atomic_add_64(&arc_p, (int64_t)bytes); 2596168404Spjd if (arc_p > arc_c) 2597168404Spjd arc_p = arc_c; 2598168404Spjd } 2599168404Spjd ASSERT((int64_t)arc_p >= 0); 2600168404Spjd} 2601168404Spjd 2602168404Spjd/* 2603168404Spjd * Check if the cache has reached its limits and eviction is required 2604168404Spjd * prior to insert. 2605168404Spjd */ 2606168404Spjdstatic int 2607185029Spjdarc_evict_needed(arc_buf_contents_t type) 2608168404Spjd{ 2609185029Spjd if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 2610185029Spjd return (1); 2611185029Spjd 2612219089Spjd#ifdef sun 2613185029Spjd#ifdef _KERNEL 2614185029Spjd /* 2615185029Spjd * If zio data pages are being allocated out of a separate heap segment, 2616185029Spjd * then enforce that the size of available vmem for this area remains 2617185029Spjd * above about 1/32nd free. 2618185029Spjd */ 2619185029Spjd if (type == ARC_BUFC_DATA && zio_arena != NULL && 2620185029Spjd vmem_size(zio_arena, VMEM_FREE) < 2621185029Spjd (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 2622185029Spjd return (1); 2623185029Spjd#endif 2624219089Spjd#endif /* sun */ 2625185029Spjd 2626168404Spjd if (arc_reclaim_needed()) 2627168404Spjd return (1); 2628168404Spjd 2629168404Spjd return (arc_size > arc_c); 2630168404Spjd} 2631168404Spjd 2632168404Spjd/* 2633168404Spjd * The buffer, supplied as the first argument, needs a data block. 2634168404Spjd * So, if we are at cache max, determine which cache should be victimized. 2635168404Spjd * We have the following cases: 2636168404Spjd * 2637168404Spjd * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2638168404Spjd * In this situation if we're out of space, but the resident size of the MFU is 2639168404Spjd * under the limit, victimize the MFU cache to satisfy this insertion request. 2640168404Spjd * 2641168404Spjd * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2642168404Spjd * Here, we've used up all of the available space for the MRU, so we need to 2643168404Spjd * evict from our own cache instead. Evict from the set of resident MRU 2644168404Spjd * entries. 2645168404Spjd * 2646168404Spjd * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2647168404Spjd * c minus p represents the MFU space in the cache, since p is the size of the 2648168404Spjd * cache that is dedicated to the MRU. In this situation there's still space on 2649168404Spjd * the MFU side, so the MRU side needs to be victimized. 2650168404Spjd * 2651168404Spjd * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2652168404Spjd * MFU's resident set is consuming more space than it has been allotted. In 2653168404Spjd * this situation, we must victimize our own cache, the MFU, for this insertion. 2654168404Spjd */ 2655168404Spjdstatic void 2656168404Spjdarc_get_data_buf(arc_buf_t *buf) 2657168404Spjd{ 2658168404Spjd arc_state_t *state = buf->b_hdr->b_state; 2659168404Spjd uint64_t size = buf->b_hdr->b_size; 2660168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 2661168404Spjd 2662168404Spjd arc_adapt(size, state); 2663168404Spjd 2664168404Spjd /* 2665168404Spjd * We have not yet reached cache maximum size, 2666168404Spjd * just allocate a new buffer. 2667168404Spjd */ 2668185029Spjd if (!arc_evict_needed(type)) { 2669168404Spjd if (type == ARC_BUFC_METADATA) { 2670168404Spjd buf->b_data = zio_buf_alloc(size); 2671208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2672168404Spjd } else { 2673168404Spjd ASSERT(type == ARC_BUFC_DATA); 2674168404Spjd buf->b_data = zio_data_buf_alloc(size); 2675208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2676185029Spjd atomic_add_64(&arc_size, size); 2677168404Spjd } 2678168404Spjd goto out; 2679168404Spjd } 2680168404Spjd 2681168404Spjd /* 2682168404Spjd * If we are prefetching from the mfu ghost list, this buffer 2683168404Spjd * will end up on the mru list; so steal space from there. 2684168404Spjd */ 2685168404Spjd if (state == arc_mfu_ghost) 2686168404Spjd state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 2687168404Spjd else if (state == arc_mru_ghost) 2688168404Spjd state = arc_mru; 2689168404Spjd 2690168404Spjd if (state == arc_mru || state == arc_anon) { 2691168404Spjd uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 2692208373Smm state = (arc_mfu->arcs_lsize[type] >= size && 2693185029Spjd arc_p > mru_used) ? arc_mfu : arc_mru; 2694168404Spjd } else { 2695168404Spjd /* MFU cases */ 2696168404Spjd uint64_t mfu_space = arc_c - arc_p; 2697208373Smm state = (arc_mru->arcs_lsize[type] >= size && 2698185029Spjd mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 2699168404Spjd } 2700209962Smm if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) { 2701168404Spjd if (type == ARC_BUFC_METADATA) { 2702168404Spjd buf->b_data = zio_buf_alloc(size); 2703208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2704168404Spjd } else { 2705168404Spjd ASSERT(type == ARC_BUFC_DATA); 2706168404Spjd buf->b_data = zio_data_buf_alloc(size); 2707208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2708185029Spjd atomic_add_64(&arc_size, size); 2709168404Spjd } 2710168404Spjd ARCSTAT_BUMP(arcstat_recycle_miss); 2711168404Spjd } 2712168404Spjd ASSERT(buf->b_data != NULL); 2713168404Spjdout: 2714168404Spjd /* 2715168404Spjd * Update the state size. Note that ghost states have a 2716168404Spjd * "ghost size" and so don't need to be updated. 2717168404Spjd */ 2718168404Spjd if (!GHOST_STATE(buf->b_hdr->b_state)) { 2719168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2720168404Spjd 2721168404Spjd atomic_add_64(&hdr->b_state->arcs_size, size); 2722168404Spjd if (list_link_active(&hdr->b_arc_node)) { 2723168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 2724185029Spjd atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2725168404Spjd } 2726168404Spjd /* 2727168404Spjd * If we are growing the cache, and we are adding anonymous 2728168404Spjd * data, and we have outgrown arc_p, update arc_p 2729168404Spjd */ 2730168404Spjd if (arc_size < arc_c && hdr->b_state == arc_anon && 2731168404Spjd arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 2732168404Spjd arc_p = MIN(arc_c, arc_p + size); 2733168404Spjd } 2734205231Skmacy ARCSTAT_BUMP(arcstat_allocated); 2735168404Spjd} 2736168404Spjd 2737168404Spjd/* 2738168404Spjd * This routine is called whenever a buffer is accessed. 2739168404Spjd * NOTE: the hash lock is dropped in this function. 2740168404Spjd */ 2741168404Spjdstatic void 2742168404Spjdarc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2743168404Spjd{ 2744219089Spjd clock_t now; 2745219089Spjd 2746168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 2747168404Spjd 2748168404Spjd if (buf->b_state == arc_anon) { 2749168404Spjd /* 2750168404Spjd * This buffer is not in the cache, and does not 2751168404Spjd * appear in our "ghost" list. Add the new buffer 2752168404Spjd * to the MRU state. 2753168404Spjd */ 2754168404Spjd 2755168404Spjd ASSERT(buf->b_arc_access == 0); 2756219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2757168404Spjd DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2758168404Spjd arc_change_state(arc_mru, buf, hash_lock); 2759168404Spjd 2760168404Spjd } else if (buf->b_state == arc_mru) { 2761219089Spjd now = ddi_get_lbolt(); 2762219089Spjd 2763168404Spjd /* 2764168404Spjd * If this buffer is here because of a prefetch, then either: 2765168404Spjd * - clear the flag if this is a "referencing" read 2766168404Spjd * (any subsequent access will bump this into the MFU state). 2767168404Spjd * or 2768168404Spjd * - move the buffer to the head of the list if this is 2769168404Spjd * another prefetch (to make it less likely to be evicted). 2770168404Spjd */ 2771168404Spjd if ((buf->b_flags & ARC_PREFETCH) != 0) { 2772168404Spjd if (refcount_count(&buf->b_refcnt) == 0) { 2773168404Spjd ASSERT(list_link_active(&buf->b_arc_node)); 2774168404Spjd } else { 2775168404Spjd buf->b_flags &= ~ARC_PREFETCH; 2776168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 2777168404Spjd } 2778219089Spjd buf->b_arc_access = now; 2779168404Spjd return; 2780168404Spjd } 2781168404Spjd 2782168404Spjd /* 2783168404Spjd * This buffer has been "accessed" only once so far, 2784168404Spjd * but it is still in the cache. Move it to the MFU 2785168404Spjd * state. 2786168404Spjd */ 2787219089Spjd if (now > buf->b_arc_access + ARC_MINTIME) { 2788168404Spjd /* 2789168404Spjd * More than 125ms have passed since we 2790168404Spjd * instantiated this buffer. Move it to the 2791168404Spjd * most frequently used state. 2792168404Spjd */ 2793219089Spjd buf->b_arc_access = now; 2794168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2795168404Spjd arc_change_state(arc_mfu, buf, hash_lock); 2796168404Spjd } 2797168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 2798168404Spjd } else if (buf->b_state == arc_mru_ghost) { 2799168404Spjd arc_state_t *new_state; 2800168404Spjd /* 2801168404Spjd * This buffer has been "accessed" recently, but 2802168404Spjd * was evicted from the cache. Move it to the 2803168404Spjd * MFU state. 2804168404Spjd */ 2805168404Spjd 2806168404Spjd if (buf->b_flags & ARC_PREFETCH) { 2807168404Spjd new_state = arc_mru; 2808168404Spjd if (refcount_count(&buf->b_refcnt) > 0) 2809168404Spjd buf->b_flags &= ~ARC_PREFETCH; 2810168404Spjd DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2811168404Spjd } else { 2812168404Spjd new_state = arc_mfu; 2813168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2814168404Spjd } 2815168404Spjd 2816219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2817168404Spjd arc_change_state(new_state, buf, hash_lock); 2818168404Spjd 2819168404Spjd ARCSTAT_BUMP(arcstat_mru_ghost_hits); 2820168404Spjd } else if (buf->b_state == arc_mfu) { 2821168404Spjd /* 2822168404Spjd * This buffer has been accessed more than once and is 2823168404Spjd * still in the cache. Keep it in the MFU state. 2824168404Spjd * 2825168404Spjd * NOTE: an add_reference() that occurred when we did 2826168404Spjd * the arc_read() will have kicked this off the list. 2827168404Spjd * If it was a prefetch, we will explicitly move it to 2828168404Spjd * the head of the list now. 2829168404Spjd */ 2830168404Spjd if ((buf->b_flags & ARC_PREFETCH) != 0) { 2831168404Spjd ASSERT(refcount_count(&buf->b_refcnt) == 0); 2832168404Spjd ASSERT(list_link_active(&buf->b_arc_node)); 2833168404Spjd } 2834168404Spjd ARCSTAT_BUMP(arcstat_mfu_hits); 2835219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2836168404Spjd } else if (buf->b_state == arc_mfu_ghost) { 2837168404Spjd arc_state_t *new_state = arc_mfu; 2838168404Spjd /* 2839168404Spjd * This buffer has been accessed more than once but has 2840168404Spjd * been evicted from the cache. Move it back to the 2841168404Spjd * MFU state. 2842168404Spjd */ 2843168404Spjd 2844168404Spjd if (buf->b_flags & ARC_PREFETCH) { 2845168404Spjd /* 2846168404Spjd * This is a prefetch access... 2847168404Spjd * move this block back to the MRU state. 2848168404Spjd */ 2849240415Smm ASSERT0(refcount_count(&buf->b_refcnt)); 2850168404Spjd new_state = arc_mru; 2851168404Spjd } 2852168404Spjd 2853219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2854168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2855168404Spjd arc_change_state(new_state, buf, hash_lock); 2856168404Spjd 2857168404Spjd ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 2858185029Spjd } else if (buf->b_state == arc_l2c_only) { 2859185029Spjd /* 2860185029Spjd * This buffer is on the 2nd Level ARC. 2861185029Spjd */ 2862185029Spjd 2863219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2864185029Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2865185029Spjd arc_change_state(arc_mfu, buf, hash_lock); 2866168404Spjd } else { 2867168404Spjd ASSERT(!"invalid arc state"); 2868168404Spjd } 2869168404Spjd} 2870168404Spjd 2871168404Spjd/* a generic arc_done_func_t which you can use */ 2872168404Spjd/* ARGSUSED */ 2873168404Spjdvoid 2874168404Spjdarc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2875168404Spjd{ 2876219089Spjd if (zio == NULL || zio->io_error == 0) 2877219089Spjd bcopy(buf->b_data, arg, buf->b_hdr->b_size); 2878168404Spjd VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2879168404Spjd} 2880168404Spjd 2881185029Spjd/* a generic arc_done_func_t */ 2882168404Spjdvoid 2883168404Spjdarc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2884168404Spjd{ 2885168404Spjd arc_buf_t **bufp = arg; 2886168404Spjd if (zio && zio->io_error) { 2887168404Spjd VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2888168404Spjd *bufp = NULL; 2889168404Spjd } else { 2890168404Spjd *bufp = buf; 2891219089Spjd ASSERT(buf->b_data); 2892168404Spjd } 2893168404Spjd} 2894168404Spjd 2895168404Spjdstatic void 2896168404Spjdarc_read_done(zio_t *zio) 2897168404Spjd{ 2898168404Spjd arc_buf_hdr_t *hdr, *found; 2899168404Spjd arc_buf_t *buf; 2900168404Spjd arc_buf_t *abuf; /* buffer we're assigning to callback */ 2901168404Spjd kmutex_t *hash_lock; 2902168404Spjd arc_callback_t *callback_list, *acb; 2903168404Spjd int freeable = FALSE; 2904168404Spjd 2905168404Spjd buf = zio->io_private; 2906168404Spjd hdr = buf->b_hdr; 2907168404Spjd 2908168404Spjd /* 2909168404Spjd * The hdr was inserted into hash-table and removed from lists 2910168404Spjd * prior to starting I/O. We should find this header, since 2911168404Spjd * it's in the hash table, and it should be legit since it's 2912168404Spjd * not possible to evict it during the I/O. The only possible 2913168404Spjd * reason for it not to be found is if we were freed during the 2914168404Spjd * read. 2915168404Spjd */ 2916209962Smm found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 2917168404Spjd &hash_lock); 2918168404Spjd 2919168404Spjd ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 2920185029Spjd (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 2921185029Spjd (found == hdr && HDR_L2_READING(hdr))); 2922168404Spjd 2923185029Spjd hdr->b_flags &= ~ARC_L2_EVICTED; 2924185029Spjd if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 2925185029Spjd hdr->b_flags &= ~ARC_L2CACHE; 2926206796Spjd 2927168404Spjd /* byteswap if necessary */ 2928168404Spjd callback_list = hdr->b_acb; 2929168404Spjd ASSERT(callback_list != NULL); 2930209101Smm if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 2931236884Smm dmu_object_byteswap_t bswap = 2932236884Smm DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 2933185029Spjd arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 2934185029Spjd byteswap_uint64_array : 2935236884Smm dmu_ot_byteswap[bswap].ob_func; 2936185029Spjd func(buf->b_data, hdr->b_size); 2937185029Spjd } 2938168404Spjd 2939185029Spjd arc_cksum_compute(buf, B_FALSE); 2940240133Smm#ifdef illumos 2941240133Smm arc_buf_watch(buf); 2942240133Smm#endif /* illumos */ 2943168404Spjd 2944219089Spjd if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 2945219089Spjd /* 2946219089Spjd * Only call arc_access on anonymous buffers. This is because 2947219089Spjd * if we've issued an I/O for an evicted buffer, we've already 2948219089Spjd * called arc_access (to prevent any simultaneous readers from 2949219089Spjd * getting confused). 2950219089Spjd */ 2951219089Spjd arc_access(hdr, hash_lock); 2952219089Spjd } 2953219089Spjd 2954168404Spjd /* create copies of the data buffer for the callers */ 2955168404Spjd abuf = buf; 2956168404Spjd for (acb = callback_list; acb; acb = acb->acb_next) { 2957168404Spjd if (acb->acb_done) { 2958242845Sdelphij if (abuf == NULL) { 2959242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_reads); 2960168404Spjd abuf = arc_buf_clone(buf); 2961242845Sdelphij } 2962168404Spjd acb->acb_buf = abuf; 2963168404Spjd abuf = NULL; 2964168404Spjd } 2965168404Spjd } 2966168404Spjd hdr->b_acb = NULL; 2967168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 2968168404Spjd ASSERT(!HDR_BUF_AVAILABLE(hdr)); 2969219089Spjd if (abuf == buf) { 2970219089Spjd ASSERT(buf->b_efunc == NULL); 2971219089Spjd ASSERT(hdr->b_datacnt == 1); 2972168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 2973219089Spjd } 2974168404Spjd 2975168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2976168404Spjd 2977168404Spjd if (zio->io_error != 0) { 2978168404Spjd hdr->b_flags |= ARC_IO_ERROR; 2979168404Spjd if (hdr->b_state != arc_anon) 2980168404Spjd arc_change_state(arc_anon, hdr, hash_lock); 2981168404Spjd if (HDR_IN_HASH_TABLE(hdr)) 2982168404Spjd buf_hash_remove(hdr); 2983168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 2984168404Spjd } 2985168404Spjd 2986168404Spjd /* 2987168404Spjd * Broadcast before we drop the hash_lock to avoid the possibility 2988168404Spjd * that the hdr (and hence the cv) might be freed before we get to 2989168404Spjd * the cv_broadcast(). 2990168404Spjd */ 2991168404Spjd cv_broadcast(&hdr->b_cv); 2992168404Spjd 2993168404Spjd if (hash_lock) { 2994168404Spjd mutex_exit(hash_lock); 2995168404Spjd } else { 2996168404Spjd /* 2997168404Spjd * This block was freed while we waited for the read to 2998168404Spjd * complete. It has been removed from the hash table and 2999168404Spjd * moved to the anonymous state (so that it won't show up 3000168404Spjd * in the cache). 3001168404Spjd */ 3002168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 3003168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 3004168404Spjd } 3005168404Spjd 3006168404Spjd /* execute each callback and free its structure */ 3007168404Spjd while ((acb = callback_list) != NULL) { 3008168404Spjd if (acb->acb_done) 3009168404Spjd acb->acb_done(zio, acb->acb_buf, acb->acb_private); 3010168404Spjd 3011168404Spjd if (acb->acb_zio_dummy != NULL) { 3012168404Spjd acb->acb_zio_dummy->io_error = zio->io_error; 3013168404Spjd zio_nowait(acb->acb_zio_dummy); 3014168404Spjd } 3015168404Spjd 3016168404Spjd callback_list = acb->acb_next; 3017168404Spjd kmem_free(acb, sizeof (arc_callback_t)); 3018168404Spjd } 3019168404Spjd 3020168404Spjd if (freeable) 3021168404Spjd arc_hdr_destroy(hdr); 3022168404Spjd} 3023168404Spjd 3024168404Spjd/* 3025168404Spjd * "Read" the block block at the specified DVA (in bp) via the 3026168404Spjd * cache. If the block is found in the cache, invoke the provided 3027168404Spjd * callback immediately and return. Note that the `zio' parameter 3028168404Spjd * in the callback will be NULL in this case, since no IO was 3029168404Spjd * required. If the block is not in the cache pass the read request 3030168404Spjd * on to the spa with a substitute callback function, so that the 3031168404Spjd * requested block will be added to the cache. 3032168404Spjd * 3033168404Spjd * If a read request arrives for a block that has a read in-progress, 3034168404Spjd * either wait for the in-progress read to complete (and return the 3035168404Spjd * results); or, if this is a read with a "done" func, add a record 3036168404Spjd * to the read to invoke the "done" func when the read completes, 3037168404Spjd * and return; or just return. 3038168404Spjd * 3039168404Spjd * arc_read_done() will invoke all the requested "done" functions 3040168404Spjd * for readers of this block. 3041168404Spjd */ 3042168404Spjdint 3043246666Smmarc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 3044246666Smm void *private, int priority, int zio_flags, uint32_t *arc_flags, 3045246666Smm const zbookmark_t *zb) 3046168404Spjd{ 3047168404Spjd arc_buf_hdr_t *hdr; 3048168404Spjd arc_buf_t *buf; 3049168404Spjd kmutex_t *hash_lock; 3050185029Spjd zio_t *rzio; 3051228103Smm uint64_t guid = spa_load_guid(spa); 3052168404Spjd 3053168404Spjdtop: 3054219089Spjd hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 3055219089Spjd &hash_lock); 3056168404Spjd if (hdr && hdr->b_datacnt > 0) { 3057168404Spjd 3058168404Spjd *arc_flags |= ARC_CACHED; 3059168404Spjd 3060168404Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3061168404Spjd 3062168404Spjd if (*arc_flags & ARC_WAIT) { 3063168404Spjd cv_wait(&hdr->b_cv, hash_lock); 3064168404Spjd mutex_exit(hash_lock); 3065168404Spjd goto top; 3066168404Spjd } 3067168404Spjd ASSERT(*arc_flags & ARC_NOWAIT); 3068168404Spjd 3069168404Spjd if (done) { 3070168404Spjd arc_callback_t *acb = NULL; 3071168404Spjd 3072168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), 3073168404Spjd KM_SLEEP); 3074168404Spjd acb->acb_done = done; 3075168404Spjd acb->acb_private = private; 3076168404Spjd if (pio != NULL) 3077168404Spjd acb->acb_zio_dummy = zio_null(pio, 3078209962Smm spa, NULL, NULL, NULL, zio_flags); 3079168404Spjd 3080168404Spjd ASSERT(acb->acb_done != NULL); 3081168404Spjd acb->acb_next = hdr->b_acb; 3082168404Spjd hdr->b_acb = acb; 3083168404Spjd add_reference(hdr, hash_lock, private); 3084168404Spjd mutex_exit(hash_lock); 3085168404Spjd return (0); 3086168404Spjd } 3087168404Spjd mutex_exit(hash_lock); 3088168404Spjd return (0); 3089168404Spjd } 3090168404Spjd 3091168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3092168404Spjd 3093168404Spjd if (done) { 3094168404Spjd add_reference(hdr, hash_lock, private); 3095168404Spjd /* 3096168404Spjd * If this block is already in use, create a new 3097168404Spjd * copy of the data so that we will be guaranteed 3098168404Spjd * that arc_release() will always succeed. 3099168404Spjd */ 3100168404Spjd buf = hdr->b_buf; 3101168404Spjd ASSERT(buf); 3102168404Spjd ASSERT(buf->b_data); 3103168404Spjd if (HDR_BUF_AVAILABLE(hdr)) { 3104168404Spjd ASSERT(buf->b_efunc == NULL); 3105168404Spjd hdr->b_flags &= ~ARC_BUF_AVAILABLE; 3106168404Spjd } else { 3107168404Spjd buf = arc_buf_clone(buf); 3108168404Spjd } 3109219089Spjd 3110168404Spjd } else if (*arc_flags & ARC_PREFETCH && 3111168404Spjd refcount_count(&hdr->b_refcnt) == 0) { 3112168404Spjd hdr->b_flags |= ARC_PREFETCH; 3113168404Spjd } 3114168404Spjd DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 3115168404Spjd arc_access(hdr, hash_lock); 3116185029Spjd if (*arc_flags & ARC_L2CACHE) 3117185029Spjd hdr->b_flags |= ARC_L2CACHE; 3118168404Spjd mutex_exit(hash_lock); 3119168404Spjd ARCSTAT_BUMP(arcstat_hits); 3120168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 3121168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3122168404Spjd data, metadata, hits); 3123168404Spjd 3124168404Spjd if (done) 3125168404Spjd done(NULL, buf, private); 3126168404Spjd } else { 3127168404Spjd uint64_t size = BP_GET_LSIZE(bp); 3128168404Spjd arc_callback_t *acb; 3129185029Spjd vdev_t *vd = NULL; 3130208373Smm uint64_t addr; 3131208373Smm boolean_t devw = B_FALSE; 3132168404Spjd 3133168404Spjd if (hdr == NULL) { 3134168404Spjd /* this block is not in the cache */ 3135168404Spjd arc_buf_hdr_t *exists; 3136168404Spjd arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 3137168404Spjd buf = arc_buf_alloc(spa, size, private, type); 3138168404Spjd hdr = buf->b_hdr; 3139168404Spjd hdr->b_dva = *BP_IDENTITY(bp); 3140219089Spjd hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 3141168404Spjd hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 3142168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 3143168404Spjd if (exists) { 3144168404Spjd /* somebody beat us to the hash insert */ 3145168404Spjd mutex_exit(hash_lock); 3146219089Spjd buf_discard_identity(hdr); 3147168404Spjd (void) arc_buf_remove_ref(buf, private); 3148168404Spjd goto top; /* restart the IO request */ 3149168404Spjd } 3150168404Spjd /* if this is a prefetch, we don't have a reference */ 3151168404Spjd if (*arc_flags & ARC_PREFETCH) { 3152168404Spjd (void) remove_reference(hdr, hash_lock, 3153168404Spjd private); 3154168404Spjd hdr->b_flags |= ARC_PREFETCH; 3155168404Spjd } 3156185029Spjd if (*arc_flags & ARC_L2CACHE) 3157185029Spjd hdr->b_flags |= ARC_L2CACHE; 3158168404Spjd if (BP_GET_LEVEL(bp) > 0) 3159168404Spjd hdr->b_flags |= ARC_INDIRECT; 3160168404Spjd } else { 3161168404Spjd /* this block is in the ghost cache */ 3162168404Spjd ASSERT(GHOST_STATE(hdr->b_state)); 3163168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3164240415Smm ASSERT0(refcount_count(&hdr->b_refcnt)); 3165168404Spjd ASSERT(hdr->b_buf == NULL); 3166168404Spjd 3167168404Spjd /* if this is a prefetch, we don't have a reference */ 3168168404Spjd if (*arc_flags & ARC_PREFETCH) 3169168404Spjd hdr->b_flags |= ARC_PREFETCH; 3170168404Spjd else 3171168404Spjd add_reference(hdr, hash_lock, private); 3172185029Spjd if (*arc_flags & ARC_L2CACHE) 3173185029Spjd hdr->b_flags |= ARC_L2CACHE; 3174185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 3175168404Spjd buf->b_hdr = hdr; 3176168404Spjd buf->b_data = NULL; 3177168404Spjd buf->b_efunc = NULL; 3178168404Spjd buf->b_private = NULL; 3179168404Spjd buf->b_next = NULL; 3180168404Spjd hdr->b_buf = buf; 3181168404Spjd ASSERT(hdr->b_datacnt == 0); 3182168404Spjd hdr->b_datacnt = 1; 3183219089Spjd arc_get_data_buf(buf); 3184219089Spjd arc_access(hdr, hash_lock); 3185168404Spjd } 3186168404Spjd 3187219089Spjd ASSERT(!GHOST_STATE(hdr->b_state)); 3188219089Spjd 3189168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 3190168404Spjd acb->acb_done = done; 3191168404Spjd acb->acb_private = private; 3192168404Spjd 3193168404Spjd ASSERT(hdr->b_acb == NULL); 3194168404Spjd hdr->b_acb = acb; 3195168404Spjd hdr->b_flags |= ARC_IO_IN_PROGRESS; 3196168404Spjd 3197185029Spjd if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 3198185029Spjd (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 3199208373Smm devw = hdr->b_l2hdr->b_dev->l2ad_writing; 3200185029Spjd addr = hdr->b_l2hdr->b_daddr; 3201185029Spjd /* 3202185029Spjd * Lock out device removal. 3203185029Spjd */ 3204185029Spjd if (vdev_is_dead(vd) || 3205185029Spjd !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 3206185029Spjd vd = NULL; 3207185029Spjd } 3208185029Spjd 3209168404Spjd mutex_exit(hash_lock); 3210168404Spjd 3211168404Spjd ASSERT3U(hdr->b_size, ==, size); 3212219089Spjd DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 3213219089Spjd uint64_t, size, zbookmark_t *, zb); 3214168404Spjd ARCSTAT_BUMP(arcstat_misses); 3215168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 3216168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3217168404Spjd data, metadata, misses); 3218228392Spjd#ifdef _KERNEL 3219228392Spjd curthread->td_ru.ru_inblock++; 3220228392Spjd#endif 3221168404Spjd 3222208373Smm if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 3223185029Spjd /* 3224185029Spjd * Read from the L2ARC if the following are true: 3225185029Spjd * 1. The L2ARC vdev was previously cached. 3226185029Spjd * 2. This buffer still has L2ARC metadata. 3227185029Spjd * 3. This buffer isn't currently writing to the L2ARC. 3228185029Spjd * 4. The L2ARC entry wasn't evicted, which may 3229185029Spjd * also have invalidated the vdev. 3230208373Smm * 5. This isn't prefetch and l2arc_noprefetch is set. 3231185029Spjd */ 3232185029Spjd if (hdr->b_l2hdr != NULL && 3233208373Smm !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 3234208373Smm !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 3235185029Spjd l2arc_read_callback_t *cb; 3236185029Spjd 3237185029Spjd DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 3238185029Spjd ARCSTAT_BUMP(arcstat_l2_hits); 3239185029Spjd 3240185029Spjd cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 3241185029Spjd KM_SLEEP); 3242185029Spjd cb->l2rcb_buf = buf; 3243185029Spjd cb->l2rcb_spa = spa; 3244185029Spjd cb->l2rcb_bp = *bp; 3245185029Spjd cb->l2rcb_zb = *zb; 3246185029Spjd cb->l2rcb_flags = zio_flags; 3247185029Spjd 3248185029Spjd /* 3249185029Spjd * l2arc read. The SCL_L2ARC lock will be 3250185029Spjd * released by l2arc_read_done(). 3251185029Spjd */ 3252185029Spjd rzio = zio_read_phys(pio, vd, addr, size, 3253206796Spjd buf->b_data, ZIO_CHECKSUM_OFF, 3254185029Spjd l2arc_read_done, cb, priority, zio_flags | 3255185029Spjd ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 3256185029Spjd ZIO_FLAG_DONT_PROPAGATE | 3257185029Spjd ZIO_FLAG_DONT_RETRY, B_FALSE); 3258185029Spjd DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 3259185029Spjd zio_t *, rzio); 3260208373Smm ARCSTAT_INCR(arcstat_l2_read_bytes, size); 3261185029Spjd 3262185029Spjd if (*arc_flags & ARC_NOWAIT) { 3263185029Spjd zio_nowait(rzio); 3264185029Spjd return (0); 3265185029Spjd } 3266185029Spjd 3267185029Spjd ASSERT(*arc_flags & ARC_WAIT); 3268185029Spjd if (zio_wait(rzio) == 0) 3269185029Spjd return (0); 3270185029Spjd 3271185029Spjd /* l2arc read error; goto zio_read() */ 3272185029Spjd } else { 3273185029Spjd DTRACE_PROBE1(l2arc__miss, 3274185029Spjd arc_buf_hdr_t *, hdr); 3275185029Spjd ARCSTAT_BUMP(arcstat_l2_misses); 3276185029Spjd if (HDR_L2_WRITING(hdr)) 3277185029Spjd ARCSTAT_BUMP(arcstat_l2_rw_clash); 3278185029Spjd spa_config_exit(spa, SCL_L2ARC, vd); 3279185029Spjd } 3280208373Smm } else { 3281208373Smm if (vd != NULL) 3282208373Smm spa_config_exit(spa, SCL_L2ARC, vd); 3283208373Smm if (l2arc_ndev != 0) { 3284208373Smm DTRACE_PROBE1(l2arc__miss, 3285208373Smm arc_buf_hdr_t *, hdr); 3286208373Smm ARCSTAT_BUMP(arcstat_l2_misses); 3287208373Smm } 3288185029Spjd } 3289185029Spjd 3290168404Spjd rzio = zio_read(pio, spa, bp, buf->b_data, size, 3291185029Spjd arc_read_done, buf, priority, zio_flags, zb); 3292168404Spjd 3293168404Spjd if (*arc_flags & ARC_WAIT) 3294168404Spjd return (zio_wait(rzio)); 3295168404Spjd 3296168404Spjd ASSERT(*arc_flags & ARC_NOWAIT); 3297168404Spjd zio_nowait(rzio); 3298168404Spjd } 3299168404Spjd return (0); 3300168404Spjd} 3301168404Spjd 3302168404Spjdvoid 3303168404Spjdarc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 3304168404Spjd{ 3305168404Spjd ASSERT(buf->b_hdr != NULL); 3306168404Spjd ASSERT(buf->b_hdr->b_state != arc_anon); 3307168404Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 3308219089Spjd ASSERT(buf->b_efunc == NULL); 3309219089Spjd ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 3310219089Spjd 3311168404Spjd buf->b_efunc = func; 3312168404Spjd buf->b_private = private; 3313168404Spjd} 3314168404Spjd 3315168404Spjd/* 3316168404Spjd * This is used by the DMU to let the ARC know that a buffer is 3317168404Spjd * being evicted, so the ARC should clean up. If this arc buf 3318168404Spjd * is not yet in the evicted state, it will be put there. 3319168404Spjd */ 3320168404Spjdint 3321168404Spjdarc_buf_evict(arc_buf_t *buf) 3322168404Spjd{ 3323168404Spjd arc_buf_hdr_t *hdr; 3324168404Spjd kmutex_t *hash_lock; 3325168404Spjd arc_buf_t **bufp; 3326205231Skmacy list_t *list, *evicted_list; 3327205231Skmacy kmutex_t *lock, *evicted_lock; 3328206796Spjd 3329219089Spjd mutex_enter(&buf->b_evict_lock); 3330168404Spjd hdr = buf->b_hdr; 3331168404Spjd if (hdr == NULL) { 3332168404Spjd /* 3333168404Spjd * We are in arc_do_user_evicts(). 3334168404Spjd */ 3335168404Spjd ASSERT(buf->b_data == NULL); 3336219089Spjd mutex_exit(&buf->b_evict_lock); 3337168404Spjd return (0); 3338185029Spjd } else if (buf->b_data == NULL) { 3339185029Spjd arc_buf_t copy = *buf; /* structure assignment */ 3340185029Spjd /* 3341185029Spjd * We are on the eviction list; process this buffer now 3342185029Spjd * but let arc_do_user_evicts() do the reaping. 3343185029Spjd */ 3344185029Spjd buf->b_efunc = NULL; 3345219089Spjd mutex_exit(&buf->b_evict_lock); 3346185029Spjd VERIFY(copy.b_efunc(©) == 0); 3347185029Spjd return (1); 3348168404Spjd } 3349168404Spjd hash_lock = HDR_LOCK(hdr); 3350168404Spjd mutex_enter(hash_lock); 3351219089Spjd hdr = buf->b_hdr; 3352219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3353168404Spjd 3354168404Spjd ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 3355168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3356168404Spjd 3357168404Spjd /* 3358168404Spjd * Pull this buffer off of the hdr 3359168404Spjd */ 3360168404Spjd bufp = &hdr->b_buf; 3361168404Spjd while (*bufp != buf) 3362168404Spjd bufp = &(*bufp)->b_next; 3363168404Spjd *bufp = buf->b_next; 3364168404Spjd 3365168404Spjd ASSERT(buf->b_data != NULL); 3366168404Spjd arc_buf_destroy(buf, FALSE, FALSE); 3367168404Spjd 3368168404Spjd if (hdr->b_datacnt == 0) { 3369168404Spjd arc_state_t *old_state = hdr->b_state; 3370168404Spjd arc_state_t *evicted_state; 3371168404Spjd 3372219089Spjd ASSERT(hdr->b_buf == NULL); 3373168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 3374168404Spjd 3375168404Spjd evicted_state = 3376168404Spjd (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 3377168404Spjd 3378205231Skmacy get_buf_info(hdr, old_state, &list, &lock); 3379205231Skmacy get_buf_info(hdr, evicted_state, &evicted_list, &evicted_lock); 3380205231Skmacy mutex_enter(lock); 3381205231Skmacy mutex_enter(evicted_lock); 3382168404Spjd 3383168404Spjd arc_change_state(evicted_state, hdr, hash_lock); 3384168404Spjd ASSERT(HDR_IN_HASH_TABLE(hdr)); 3385185029Spjd hdr->b_flags |= ARC_IN_HASH_TABLE; 3386185029Spjd hdr->b_flags &= ~ARC_BUF_AVAILABLE; 3387168404Spjd 3388205231Skmacy mutex_exit(evicted_lock); 3389205231Skmacy mutex_exit(lock); 3390168404Spjd } 3391168404Spjd mutex_exit(hash_lock); 3392219089Spjd mutex_exit(&buf->b_evict_lock); 3393168404Spjd 3394168404Spjd VERIFY(buf->b_efunc(buf) == 0); 3395168404Spjd buf->b_efunc = NULL; 3396168404Spjd buf->b_private = NULL; 3397168404Spjd buf->b_hdr = NULL; 3398219089Spjd buf->b_next = NULL; 3399168404Spjd kmem_cache_free(buf_cache, buf); 3400168404Spjd return (1); 3401168404Spjd} 3402168404Spjd 3403168404Spjd/* 3404168404Spjd * Release this buffer from the cache. This must be done 3405168404Spjd * after a read and prior to modifying the buffer contents. 3406168404Spjd * If the buffer has more than one reference, we must make 3407185029Spjd * a new hdr for the buffer. 3408168404Spjd */ 3409168404Spjdvoid 3410168404Spjdarc_release(arc_buf_t *buf, void *tag) 3411168404Spjd{ 3412185029Spjd arc_buf_hdr_t *hdr; 3413219089Spjd kmutex_t *hash_lock = NULL; 3414185029Spjd l2arc_buf_hdr_t *l2hdr; 3415185029Spjd uint64_t buf_size; 3416168404Spjd 3417219089Spjd /* 3418219089Spjd * It would be nice to assert that if it's DMU metadata (level > 3419219089Spjd * 0 || it's the dnode file), then it must be syncing context. 3420219089Spjd * But we don't know that information at this level. 3421219089Spjd */ 3422219089Spjd 3423219089Spjd mutex_enter(&buf->b_evict_lock); 3424185029Spjd hdr = buf->b_hdr; 3425185029Spjd 3426168404Spjd /* this buffer is not on any list */ 3427168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) > 0); 3428168404Spjd 3429168404Spjd if (hdr->b_state == arc_anon) { 3430168404Spjd /* this buffer is already released */ 3431168404Spjd ASSERT(buf->b_efunc == NULL); 3432208373Smm } else { 3433208373Smm hash_lock = HDR_LOCK(hdr); 3434208373Smm mutex_enter(hash_lock); 3435219089Spjd hdr = buf->b_hdr; 3436219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3437168404Spjd } 3438168404Spjd 3439185029Spjd l2hdr = hdr->b_l2hdr; 3440185029Spjd if (l2hdr) { 3441185029Spjd mutex_enter(&l2arc_buflist_mtx); 3442185029Spjd hdr->b_l2hdr = NULL; 3443185029Spjd buf_size = hdr->b_size; 3444185029Spjd } 3445185029Spjd 3446168404Spjd /* 3447168404Spjd * Do we have more than one buf? 3448168404Spjd */ 3449185029Spjd if (hdr->b_datacnt > 1) { 3450168404Spjd arc_buf_hdr_t *nhdr; 3451168404Spjd arc_buf_t **bufp; 3452168404Spjd uint64_t blksz = hdr->b_size; 3453209962Smm uint64_t spa = hdr->b_spa; 3454168404Spjd arc_buf_contents_t type = hdr->b_type; 3455185029Spjd uint32_t flags = hdr->b_flags; 3456168404Spjd 3457185029Spjd ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3458168404Spjd /* 3459219089Spjd * Pull the data off of this hdr and attach it to 3460219089Spjd * a new anonymous hdr. 3461168404Spjd */ 3462168404Spjd (void) remove_reference(hdr, hash_lock, tag); 3463168404Spjd bufp = &hdr->b_buf; 3464168404Spjd while (*bufp != buf) 3465168404Spjd bufp = &(*bufp)->b_next; 3466219089Spjd *bufp = buf->b_next; 3467168404Spjd buf->b_next = NULL; 3468168404Spjd 3469168404Spjd ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 3470168404Spjd atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 3471168404Spjd if (refcount_is_zero(&hdr->b_refcnt)) { 3472185029Spjd uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 3473185029Spjd ASSERT3U(*size, >=, hdr->b_size); 3474185029Spjd atomic_add_64(size, -hdr->b_size); 3475168404Spjd } 3476242845Sdelphij 3477242845Sdelphij /* 3478242845Sdelphij * We're releasing a duplicate user data buffer, update 3479242845Sdelphij * our statistics accordingly. 3480242845Sdelphij */ 3481242845Sdelphij if (hdr->b_type == ARC_BUFC_DATA) { 3482242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 3483242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, 3484242845Sdelphij -hdr->b_size); 3485242845Sdelphij } 3486168404Spjd hdr->b_datacnt -= 1; 3487168404Spjd arc_cksum_verify(buf); 3488240133Smm#ifdef illumos 3489240133Smm arc_buf_unwatch(buf); 3490240133Smm#endif /* illumos */ 3491168404Spjd 3492168404Spjd mutex_exit(hash_lock); 3493168404Spjd 3494185029Spjd nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3495168404Spjd nhdr->b_size = blksz; 3496168404Spjd nhdr->b_spa = spa; 3497168404Spjd nhdr->b_type = type; 3498168404Spjd nhdr->b_buf = buf; 3499168404Spjd nhdr->b_state = arc_anon; 3500168404Spjd nhdr->b_arc_access = 0; 3501185029Spjd nhdr->b_flags = flags & ARC_L2_WRITING; 3502185029Spjd nhdr->b_l2hdr = NULL; 3503168404Spjd nhdr->b_datacnt = 1; 3504168404Spjd nhdr->b_freeze_cksum = NULL; 3505168404Spjd (void) refcount_add(&nhdr->b_refcnt, tag); 3506168404Spjd buf->b_hdr = nhdr; 3507219089Spjd mutex_exit(&buf->b_evict_lock); 3508168404Spjd atomic_add_64(&arc_anon->arcs_size, blksz); 3509168404Spjd } else { 3510219089Spjd mutex_exit(&buf->b_evict_lock); 3511168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3512168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 3513168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3514219089Spjd if (hdr->b_state != arc_anon) 3515219089Spjd arc_change_state(arc_anon, hdr, hash_lock); 3516168404Spjd hdr->b_arc_access = 0; 3517219089Spjd if (hash_lock) 3518219089Spjd mutex_exit(hash_lock); 3519185029Spjd 3520219089Spjd buf_discard_identity(hdr); 3521168404Spjd arc_buf_thaw(buf); 3522168404Spjd } 3523168404Spjd buf->b_efunc = NULL; 3524168404Spjd buf->b_private = NULL; 3525185029Spjd 3526185029Spjd if (l2hdr) { 3527185029Spjd list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 3528185029Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 3529185029Spjd ARCSTAT_INCR(arcstat_l2_size, -buf_size); 3530185029Spjd mutex_exit(&l2arc_buflist_mtx); 3531185029Spjd } 3532168404Spjd} 3533168404Spjd 3534168404Spjdint 3535168404Spjdarc_released(arc_buf_t *buf) 3536168404Spjd{ 3537185029Spjd int released; 3538185029Spjd 3539219089Spjd mutex_enter(&buf->b_evict_lock); 3540185029Spjd released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 3541219089Spjd mutex_exit(&buf->b_evict_lock); 3542185029Spjd return (released); 3543168404Spjd} 3544168404Spjd 3545168404Spjdint 3546168404Spjdarc_has_callback(arc_buf_t *buf) 3547168404Spjd{ 3548185029Spjd int callback; 3549185029Spjd 3550219089Spjd mutex_enter(&buf->b_evict_lock); 3551185029Spjd callback = (buf->b_efunc != NULL); 3552219089Spjd mutex_exit(&buf->b_evict_lock); 3553185029Spjd return (callback); 3554168404Spjd} 3555168404Spjd 3556168404Spjd#ifdef ZFS_DEBUG 3557168404Spjdint 3558168404Spjdarc_referenced(arc_buf_t *buf) 3559168404Spjd{ 3560185029Spjd int referenced; 3561185029Spjd 3562219089Spjd mutex_enter(&buf->b_evict_lock); 3563185029Spjd referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 3564219089Spjd mutex_exit(&buf->b_evict_lock); 3565185029Spjd return (referenced); 3566168404Spjd} 3567168404Spjd#endif 3568168404Spjd 3569168404Spjdstatic void 3570168404Spjdarc_write_ready(zio_t *zio) 3571168404Spjd{ 3572168404Spjd arc_write_callback_t *callback = zio->io_private; 3573168404Spjd arc_buf_t *buf = callback->awcb_buf; 3574185029Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3575168404Spjd 3576185029Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 3577185029Spjd callback->awcb_ready(zio, buf, callback->awcb_private); 3578185029Spjd 3579185029Spjd /* 3580185029Spjd * If the IO is already in progress, then this is a re-write 3581185029Spjd * attempt, so we need to thaw and re-compute the cksum. 3582185029Spjd * It is the responsibility of the callback to handle the 3583185029Spjd * accounting for any re-write attempt. 3584185029Spjd */ 3585185029Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3586185029Spjd mutex_enter(&hdr->b_freeze_lock); 3587185029Spjd if (hdr->b_freeze_cksum != NULL) { 3588185029Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 3589185029Spjd hdr->b_freeze_cksum = NULL; 3590185029Spjd } 3591185029Spjd mutex_exit(&hdr->b_freeze_lock); 3592168404Spjd } 3593185029Spjd arc_cksum_compute(buf, B_FALSE); 3594185029Spjd hdr->b_flags |= ARC_IO_IN_PROGRESS; 3595168404Spjd} 3596168404Spjd 3597168404Spjdstatic void 3598168404Spjdarc_write_done(zio_t *zio) 3599168404Spjd{ 3600168404Spjd arc_write_callback_t *callback = zio->io_private; 3601168404Spjd arc_buf_t *buf = callback->awcb_buf; 3602168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3603168404Spjd 3604219089Spjd ASSERT(hdr->b_acb == NULL); 3605168404Spjd 3606219089Spjd if (zio->io_error == 0) { 3607219089Spjd hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3608219089Spjd hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 3609219089Spjd hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 3610219089Spjd } else { 3611219089Spjd ASSERT(BUF_EMPTY(hdr)); 3612219089Spjd } 3613219089Spjd 3614168404Spjd /* 3615168404Spjd * If the block to be written was all-zero, we may have 3616168404Spjd * compressed it away. In this case no write was performed 3617219089Spjd * so there will be no dva/birth/checksum. The buffer must 3618219089Spjd * therefore remain anonymous (and uncached). 3619168404Spjd */ 3620168404Spjd if (!BUF_EMPTY(hdr)) { 3621168404Spjd arc_buf_hdr_t *exists; 3622168404Spjd kmutex_t *hash_lock; 3623168404Spjd 3624219089Spjd ASSERT(zio->io_error == 0); 3625219089Spjd 3626168404Spjd arc_cksum_verify(buf); 3627168404Spjd 3628168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 3629168404Spjd if (exists) { 3630168404Spjd /* 3631168404Spjd * This can only happen if we overwrite for 3632168404Spjd * sync-to-convergence, because we remove 3633168404Spjd * buffers from the hash table when we arc_free(). 3634168404Spjd */ 3635219089Spjd if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 3636219089Spjd if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3637219089Spjd panic("bad overwrite, hdr=%p exists=%p", 3638219089Spjd (void *)hdr, (void *)exists); 3639219089Spjd ASSERT(refcount_is_zero(&exists->b_refcnt)); 3640219089Spjd arc_change_state(arc_anon, exists, hash_lock); 3641219089Spjd mutex_exit(hash_lock); 3642219089Spjd arc_hdr_destroy(exists); 3643219089Spjd exists = buf_hash_insert(hdr, &hash_lock); 3644219089Spjd ASSERT3P(exists, ==, NULL); 3645243524Smm } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 3646243524Smm /* nopwrite */ 3647243524Smm ASSERT(zio->io_prop.zp_nopwrite); 3648243524Smm if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3649243524Smm panic("bad nopwrite, hdr=%p exists=%p", 3650243524Smm (void *)hdr, (void *)exists); 3651219089Spjd } else { 3652219089Spjd /* Dedup */ 3653219089Spjd ASSERT(hdr->b_datacnt == 1); 3654219089Spjd ASSERT(hdr->b_state == arc_anon); 3655219089Spjd ASSERT(BP_GET_DEDUP(zio->io_bp)); 3656219089Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 3657219089Spjd } 3658168404Spjd } 3659168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3660185029Spjd /* if it's not anon, we are doing a scrub */ 3661219089Spjd if (!exists && hdr->b_state == arc_anon) 3662185029Spjd arc_access(hdr, hash_lock); 3663168404Spjd mutex_exit(hash_lock); 3664168404Spjd } else { 3665168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3666168404Spjd } 3667168404Spjd 3668219089Spjd ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 3669219089Spjd callback->awcb_done(zio, buf, callback->awcb_private); 3670168404Spjd 3671168404Spjd kmem_free(callback, sizeof (arc_write_callback_t)); 3672168404Spjd} 3673168404Spjd 3674168404Spjdzio_t * 3675219089Spjdarc_write(zio_t *pio, spa_t *spa, uint64_t txg, 3676219089Spjd blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp, 3677219089Spjd arc_done_func_t *ready, arc_done_func_t *done, void *private, 3678219089Spjd int priority, int zio_flags, const zbookmark_t *zb) 3679168404Spjd{ 3680168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3681168404Spjd arc_write_callback_t *callback; 3682185029Spjd zio_t *zio; 3683168404Spjd 3684185029Spjd ASSERT(ready != NULL); 3685219089Spjd ASSERT(done != NULL); 3686168404Spjd ASSERT(!HDR_IO_ERROR(hdr)); 3687168404Spjd ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 3688219089Spjd ASSERT(hdr->b_acb == NULL); 3689185029Spjd if (l2arc) 3690185029Spjd hdr->b_flags |= ARC_L2CACHE; 3691168404Spjd callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 3692168404Spjd callback->awcb_ready = ready; 3693168404Spjd callback->awcb_done = done; 3694168404Spjd callback->awcb_private = private; 3695168404Spjd callback->awcb_buf = buf; 3696168404Spjd 3697219089Spjd zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 3698185029Spjd arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3699185029Spjd 3700168404Spjd return (zio); 3701168404Spjd} 3702168404Spjd 3703185029Spjdstatic int 3704209962Smmarc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg) 3705185029Spjd{ 3706185029Spjd#ifdef _KERNEL 3707219089Spjd uint64_t available_memory = 3708219089Spjd ptoa((uintmax_t)cnt.v_free_count + cnt.v_cache_count); 3709185029Spjd static uint64_t page_load = 0; 3710185029Spjd static uint64_t last_txg = 0; 3711185029Spjd 3712219089Spjd#ifdef sun 3713185029Spjd#if defined(__i386) 3714185029Spjd available_memory = 3715185029Spjd MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 3716185029Spjd#endif 3717219089Spjd#endif /* sun */ 3718185029Spjd if (available_memory >= zfs_write_limit_max) 3719185029Spjd return (0); 3720185029Spjd 3721185029Spjd if (txg > last_txg) { 3722185029Spjd last_txg = txg; 3723185029Spjd page_load = 0; 3724185029Spjd } 3725185029Spjd /* 3726185029Spjd * If we are in pageout, we know that memory is already tight, 3727185029Spjd * the arc is already going to be evicting, so we just want to 3728185029Spjd * continue to let page writes occur as quickly as possible. 3729185029Spjd */ 3730185029Spjd if (curproc == pageproc) { 3731185029Spjd if (page_load > available_memory / 4) 3732185029Spjd return (ERESTART); 3733185029Spjd /* Note: reserve is inflated, so we deflate */ 3734185029Spjd page_load += reserve / 8; 3735185029Spjd return (0); 3736185029Spjd } else if (page_load > 0 && arc_reclaim_needed()) { 3737185029Spjd /* memory is low, delay before restarting */ 3738185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 3739185029Spjd return (EAGAIN); 3740185029Spjd } 3741185029Spjd page_load = 0; 3742185029Spjd 3743185029Spjd if (arc_size > arc_c_min) { 3744185029Spjd uint64_t evictable_memory = 3745185029Spjd arc_mru->arcs_lsize[ARC_BUFC_DATA] + 3746185029Spjd arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 3747185029Spjd arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 3748185029Spjd arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 3749185029Spjd available_memory += MIN(evictable_memory, arc_size - arc_c_min); 3750185029Spjd } 3751185029Spjd 3752185029Spjd if (inflight_data > available_memory / 4) { 3753185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 3754185029Spjd return (ERESTART); 3755185029Spjd } 3756185029Spjd#endif 3757185029Spjd return (0); 3758185029Spjd} 3759185029Spjd 3760168404Spjdvoid 3761185029Spjdarc_tempreserve_clear(uint64_t reserve) 3762168404Spjd{ 3763185029Spjd atomic_add_64(&arc_tempreserve, -reserve); 3764168404Spjd ASSERT((int64_t)arc_tempreserve >= 0); 3765168404Spjd} 3766168404Spjd 3767168404Spjdint 3768185029Spjdarc_tempreserve_space(uint64_t reserve, uint64_t txg) 3769168404Spjd{ 3770185029Spjd int error; 3771209962Smm uint64_t anon_size; 3772185029Spjd 3773168404Spjd#ifdef ZFS_DEBUG 3774168404Spjd /* 3775168404Spjd * Once in a while, fail for no reason. Everything should cope. 3776168404Spjd */ 3777168404Spjd if (spa_get_random(10000) == 0) { 3778168404Spjd dprintf("forcing random failure\n"); 3779168404Spjd return (ERESTART); 3780168404Spjd } 3781168404Spjd#endif 3782185029Spjd if (reserve > arc_c/4 && !arc_no_grow) 3783185029Spjd arc_c = MIN(arc_c_max, reserve * 4); 3784185029Spjd if (reserve > arc_c) 3785168404Spjd return (ENOMEM); 3786168404Spjd 3787168404Spjd /* 3788209962Smm * Don't count loaned bufs as in flight dirty data to prevent long 3789209962Smm * network delays from blocking transactions that are ready to be 3790209962Smm * assigned to a txg. 3791209962Smm */ 3792209962Smm anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 3793209962Smm 3794209962Smm /* 3795185029Spjd * Writes will, almost always, require additional memory allocations 3796185029Spjd * in order to compress/encrypt/etc the data. We therefor need to 3797185029Spjd * make sure that there is sufficient available memory for this. 3798185029Spjd */ 3799209962Smm if (error = arc_memory_throttle(reserve, anon_size, txg)) 3800185029Spjd return (error); 3801185029Spjd 3802185029Spjd /* 3803168404Spjd * Throttle writes when the amount of dirty data in the cache 3804168404Spjd * gets too large. We try to keep the cache less than half full 3805168404Spjd * of dirty blocks so that our sync times don't grow too large. 3806168404Spjd * Note: if two requests come in concurrently, we might let them 3807168404Spjd * both succeed, when one of them should fail. Not a huge deal. 3808168404Spjd */ 3809209962Smm 3810209962Smm if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 3811209962Smm anon_size > arc_c / 4) { 3812185029Spjd dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 3813185029Spjd "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 3814185029Spjd arc_tempreserve>>10, 3815185029Spjd arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 3816185029Spjd arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 3817185029Spjd reserve>>10, arc_c>>10); 3818168404Spjd return (ERESTART); 3819168404Spjd } 3820185029Spjd atomic_add_64(&arc_tempreserve, reserve); 3821168404Spjd return (0); 3822168404Spjd} 3823168404Spjd 3824168582Spjdstatic kmutex_t arc_lowmem_lock; 3825168404Spjd#ifdef _KERNEL 3826168566Spjdstatic eventhandler_tag arc_event_lowmem = NULL; 3827168404Spjd 3828168404Spjdstatic void 3829168566Spjdarc_lowmem(void *arg __unused, int howto __unused) 3830168404Spjd{ 3831168404Spjd 3832168566Spjd /* Serialize access via arc_lowmem_lock. */ 3833168566Spjd mutex_enter(&arc_lowmem_lock); 3834219089Spjd mutex_enter(&arc_reclaim_thr_lock); 3835185029Spjd needfree = 1; 3836168404Spjd cv_signal(&arc_reclaim_thr_cv); 3837241773Savg 3838241773Savg /* 3839241773Savg * It is unsafe to block here in arbitrary threads, because we can come 3840241773Savg * here from ARC itself and may hold ARC locks and thus risk a deadlock 3841241773Savg * with ARC reclaim thread. 3842241773Savg */ 3843241773Savg if (curproc == pageproc) { 3844241773Savg while (needfree) 3845241773Savg msleep(&needfree, &arc_reclaim_thr_lock, 0, "zfs:lowmem", 0); 3846241773Savg } 3847219089Spjd mutex_exit(&arc_reclaim_thr_lock); 3848168566Spjd mutex_exit(&arc_lowmem_lock); 3849168404Spjd} 3850168404Spjd#endif 3851168404Spjd 3852168404Spjdvoid 3853168404Spjdarc_init(void) 3854168404Spjd{ 3855219089Spjd int i, prefetch_tunable_set = 0; 3856205231Skmacy 3857168404Spjd mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3858168404Spjd cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3859168566Spjd mutex_init(&arc_lowmem_lock, NULL, MUTEX_DEFAULT, NULL); 3860168404Spjd 3861168404Spjd /* Convert seconds to clock ticks */ 3862168404Spjd arc_min_prefetch_lifespan = 1 * hz; 3863168404Spjd 3864168404Spjd /* Start out with 1/8 of all memory */ 3865168566Spjd arc_c = kmem_size() / 8; 3866219089Spjd 3867219089Spjd#ifdef sun 3868192360Skmacy#ifdef _KERNEL 3869192360Skmacy /* 3870192360Skmacy * On architectures where the physical memory can be larger 3871192360Skmacy * than the addressable space (intel in 32-bit mode), we may 3872192360Skmacy * need to limit the cache to 1/8 of VM size. 3873192360Skmacy */ 3874192360Skmacy arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3875192360Skmacy#endif 3876219089Spjd#endif /* sun */ 3877168566Spjd /* set min cache to 1/32 of all memory, or 16MB, whichever is more */ 3878168566Spjd arc_c_min = MAX(arc_c / 4, 64<<18); 3879168566Spjd /* set max to 1/2 of all memory, or all but 1GB, whichever is more */ 3880168404Spjd if (arc_c * 8 >= 1<<30) 3881168404Spjd arc_c_max = (arc_c * 8) - (1<<30); 3882168404Spjd else 3883168404Spjd arc_c_max = arc_c_min; 3884175633Spjd arc_c_max = MAX(arc_c * 5, arc_c_max); 3885219089Spjd 3886168481Spjd#ifdef _KERNEL 3887168404Spjd /* 3888168404Spjd * Allow the tunables to override our calculations if they are 3889168566Spjd * reasonable (ie. over 16MB) 3890168404Spjd */ 3891219089Spjd if (zfs_arc_max > 64<<18 && zfs_arc_max < kmem_size()) 3892168404Spjd arc_c_max = zfs_arc_max; 3893219089Spjd if (zfs_arc_min > 64<<18 && zfs_arc_min <= arc_c_max) 3894168404Spjd arc_c_min = zfs_arc_min; 3895168481Spjd#endif 3896219089Spjd 3897168404Spjd arc_c = arc_c_max; 3898168404Spjd arc_p = (arc_c >> 1); 3899168404Spjd 3900185029Spjd /* limit meta-data to 1/4 of the arc capacity */ 3901185029Spjd arc_meta_limit = arc_c_max / 4; 3902185029Spjd 3903185029Spjd /* Allow the tunable to override if it is reasonable */ 3904185029Spjd if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 3905185029Spjd arc_meta_limit = zfs_arc_meta_limit; 3906185029Spjd 3907185029Spjd if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 3908185029Spjd arc_c_min = arc_meta_limit / 2; 3909185029Spjd 3910208373Smm if (zfs_arc_grow_retry > 0) 3911208373Smm arc_grow_retry = zfs_arc_grow_retry; 3912208373Smm 3913208373Smm if (zfs_arc_shrink_shift > 0) 3914208373Smm arc_shrink_shift = zfs_arc_shrink_shift; 3915208373Smm 3916208373Smm if (zfs_arc_p_min_shift > 0) 3917208373Smm arc_p_min_shift = zfs_arc_p_min_shift; 3918208373Smm 3919168404Spjd /* if kmem_flags are set, lets try to use less memory */ 3920168404Spjd if (kmem_debugging()) 3921168404Spjd arc_c = arc_c / 2; 3922168404Spjd if (arc_c < arc_c_min) 3923168404Spjd arc_c = arc_c_min; 3924168404Spjd 3925168473Spjd zfs_arc_min = arc_c_min; 3926168473Spjd zfs_arc_max = arc_c_max; 3927168473Spjd 3928168404Spjd arc_anon = &ARC_anon; 3929168404Spjd arc_mru = &ARC_mru; 3930168404Spjd arc_mru_ghost = &ARC_mru_ghost; 3931168404Spjd arc_mfu = &ARC_mfu; 3932168404Spjd arc_mfu_ghost = &ARC_mfu_ghost; 3933185029Spjd arc_l2c_only = &ARC_l2c_only; 3934168404Spjd arc_size = 0; 3935168404Spjd 3936205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 3937205231Skmacy mutex_init(&arc_anon->arcs_locks[i].arcs_lock, 3938205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3939205231Skmacy mutex_init(&arc_mru->arcs_locks[i].arcs_lock, 3940205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3941205231Skmacy mutex_init(&arc_mru_ghost->arcs_locks[i].arcs_lock, 3942205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3943205231Skmacy mutex_init(&arc_mfu->arcs_locks[i].arcs_lock, 3944205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3945205231Skmacy mutex_init(&arc_mfu_ghost->arcs_locks[i].arcs_lock, 3946205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3947205231Skmacy mutex_init(&arc_l2c_only->arcs_locks[i].arcs_lock, 3948205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3949206796Spjd 3950205231Skmacy list_create(&arc_mru->arcs_lists[i], 3951205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3952205231Skmacy list_create(&arc_mru_ghost->arcs_lists[i], 3953205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3954205231Skmacy list_create(&arc_mfu->arcs_lists[i], 3955205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3956205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 3957205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3958205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 3959205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3960205231Skmacy list_create(&arc_l2c_only->arcs_lists[i], 3961205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3962205231Skmacy } 3963168404Spjd 3964168404Spjd buf_init(); 3965168404Spjd 3966168404Spjd arc_thread_exit = 0; 3967168404Spjd arc_eviction_list = NULL; 3968168404Spjd mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 3969168404Spjd bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3970168404Spjd 3971168404Spjd arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 3972168404Spjd sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 3973168404Spjd 3974168404Spjd if (arc_ksp != NULL) { 3975168404Spjd arc_ksp->ks_data = &arc_stats; 3976168404Spjd kstat_install(arc_ksp); 3977168404Spjd } 3978168404Spjd 3979168404Spjd (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3980168404Spjd TS_RUN, minclsyspri); 3981168404Spjd 3982168404Spjd#ifdef _KERNEL 3983168566Spjd arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 3984168404Spjd EVENTHANDLER_PRI_FIRST); 3985168404Spjd#endif 3986168404Spjd 3987168404Spjd arc_dead = FALSE; 3988185029Spjd arc_warm = B_FALSE; 3989168566Spjd 3990185029Spjd if (zfs_write_limit_max == 0) 3991185029Spjd zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 3992185029Spjd else 3993185029Spjd zfs_write_limit_shift = 0; 3994185029Spjd mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3995185029Spjd 3996168566Spjd#ifdef _KERNEL 3997194043Skmacy if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 3998193953Skmacy prefetch_tunable_set = 1; 3999206796Spjd 4000193878Skmacy#ifdef __i386__ 4001193953Skmacy if (prefetch_tunable_set == 0) { 4002196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 4003196863Strasz "-- to enable,\n"); 4004196863Strasz printf(" add \"vfs.zfs.prefetch_disable=0\" " 4005196863Strasz "to /boot/loader.conf.\n"); 4006219089Spjd zfs_prefetch_disable = 1; 4007193878Skmacy } 4008206796Spjd#else 4009193878Skmacy if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 4010193953Skmacy prefetch_tunable_set == 0) { 4011196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default if less " 4012196941Strasz "than 4GB of RAM is present;\n" 4013196863Strasz " to enable, add \"vfs.zfs.prefetch_disable=0\" " 4014196863Strasz "to /boot/loader.conf.\n"); 4015219089Spjd zfs_prefetch_disable = 1; 4016193878Skmacy } 4017206796Spjd#endif 4018175633Spjd /* Warn about ZFS memory and address space requirements. */ 4019168696Spjd if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 4020168987Sbmah printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 4021168987Sbmah "expect unstable behavior.\n"); 4022175633Spjd } 4023175633Spjd if (kmem_size() < 512 * (1 << 20)) { 4024173419Spjd printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 4025168987Sbmah "expect unstable behavior.\n"); 4026185029Spjd printf(" Consider tuning vm.kmem_size and " 4027173419Spjd "vm.kmem_size_max\n"); 4028185029Spjd printf(" in /boot/loader.conf.\n"); 4029168566Spjd } 4030168566Spjd#endif 4031168404Spjd} 4032168404Spjd 4033168404Spjdvoid 4034168404Spjdarc_fini(void) 4035168404Spjd{ 4036205231Skmacy int i; 4037206796Spjd 4038168404Spjd mutex_enter(&arc_reclaim_thr_lock); 4039168404Spjd arc_thread_exit = 1; 4040168404Spjd cv_signal(&arc_reclaim_thr_cv); 4041168404Spjd while (arc_thread_exit != 0) 4042168404Spjd cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 4043168404Spjd mutex_exit(&arc_reclaim_thr_lock); 4044168404Spjd 4045185029Spjd arc_flush(NULL); 4046168404Spjd 4047168404Spjd arc_dead = TRUE; 4048168404Spjd 4049168404Spjd if (arc_ksp != NULL) { 4050168404Spjd kstat_delete(arc_ksp); 4051168404Spjd arc_ksp = NULL; 4052168404Spjd } 4053168404Spjd 4054168404Spjd mutex_destroy(&arc_eviction_mtx); 4055168404Spjd mutex_destroy(&arc_reclaim_thr_lock); 4056168404Spjd cv_destroy(&arc_reclaim_thr_cv); 4057168404Spjd 4058205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 4059205231Skmacy list_destroy(&arc_mru->arcs_lists[i]); 4060205231Skmacy list_destroy(&arc_mru_ghost->arcs_lists[i]); 4061205231Skmacy list_destroy(&arc_mfu->arcs_lists[i]); 4062205231Skmacy list_destroy(&arc_mfu_ghost->arcs_lists[i]); 4063206795Spjd list_destroy(&arc_l2c_only->arcs_lists[i]); 4064168404Spjd 4065205231Skmacy mutex_destroy(&arc_anon->arcs_locks[i].arcs_lock); 4066205231Skmacy mutex_destroy(&arc_mru->arcs_locks[i].arcs_lock); 4067205231Skmacy mutex_destroy(&arc_mru_ghost->arcs_locks[i].arcs_lock); 4068205231Skmacy mutex_destroy(&arc_mfu->arcs_locks[i].arcs_lock); 4069205231Skmacy mutex_destroy(&arc_mfu_ghost->arcs_locks[i].arcs_lock); 4070206795Spjd mutex_destroy(&arc_l2c_only->arcs_locks[i].arcs_lock); 4071205231Skmacy } 4072206796Spjd 4073185029Spjd mutex_destroy(&zfs_write_limit_lock); 4074185029Spjd 4075168404Spjd buf_fini(); 4076168404Spjd 4077209962Smm ASSERT(arc_loaned_bytes == 0); 4078209962Smm 4079168582Spjd mutex_destroy(&arc_lowmem_lock); 4080168404Spjd#ifdef _KERNEL 4081168566Spjd if (arc_event_lowmem != NULL) 4082168566Spjd EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 4083168404Spjd#endif 4084168404Spjd} 4085185029Spjd 4086185029Spjd/* 4087185029Spjd * Level 2 ARC 4088185029Spjd * 4089185029Spjd * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 4090185029Spjd * It uses dedicated storage devices to hold cached data, which are populated 4091185029Spjd * using large infrequent writes. The main role of this cache is to boost 4092185029Spjd * the performance of random read workloads. The intended L2ARC devices 4093185029Spjd * include short-stroked disks, solid state disks, and other media with 4094185029Spjd * substantially faster read latency than disk. 4095185029Spjd * 4096185029Spjd * +-----------------------+ 4097185029Spjd * | ARC | 4098185029Spjd * +-----------------------+ 4099185029Spjd * | ^ ^ 4100185029Spjd * | | | 4101185029Spjd * l2arc_feed_thread() arc_read() 4102185029Spjd * | | | 4103185029Spjd * | l2arc read | 4104185029Spjd * V | | 4105185029Spjd * +---------------+ | 4106185029Spjd * | L2ARC | | 4107185029Spjd * +---------------+ | 4108185029Spjd * | ^ | 4109185029Spjd * l2arc_write() | | 4110185029Spjd * | | | 4111185029Spjd * V | | 4112185029Spjd * +-------+ +-------+ 4113185029Spjd * | vdev | | vdev | 4114185029Spjd * | cache | | cache | 4115185029Spjd * +-------+ +-------+ 4116185029Spjd * +=========+ .-----. 4117185029Spjd * : L2ARC : |-_____-| 4118185029Spjd * : devices : | Disks | 4119185029Spjd * +=========+ `-_____-' 4120185029Spjd * 4121185029Spjd * Read requests are satisfied from the following sources, in order: 4122185029Spjd * 4123185029Spjd * 1) ARC 4124185029Spjd * 2) vdev cache of L2ARC devices 4125185029Spjd * 3) L2ARC devices 4126185029Spjd * 4) vdev cache of disks 4127185029Spjd * 5) disks 4128185029Spjd * 4129185029Spjd * Some L2ARC device types exhibit extremely slow write performance. 4130185029Spjd * To accommodate for this there are some significant differences between 4131185029Spjd * the L2ARC and traditional cache design: 4132185029Spjd * 4133185029Spjd * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 4134185029Spjd * the ARC behave as usual, freeing buffers and placing headers on ghost 4135185029Spjd * lists. The ARC does not send buffers to the L2ARC during eviction as 4136185029Spjd * this would add inflated write latencies for all ARC memory pressure. 4137185029Spjd * 4138185029Spjd * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 4139185029Spjd * It does this by periodically scanning buffers from the eviction-end of 4140185029Spjd * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 4141185029Spjd * not already there. It scans until a headroom of buffers is satisfied, 4142185029Spjd * which itself is a buffer for ARC eviction. The thread that does this is 4143185029Spjd * l2arc_feed_thread(), illustrated below; example sizes are included to 4144185029Spjd * provide a better sense of ratio than this diagram: 4145185029Spjd * 4146185029Spjd * head --> tail 4147185029Spjd * +---------------------+----------+ 4148185029Spjd * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 4149185029Spjd * +---------------------+----------+ | o L2ARC eligible 4150185029Spjd * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 4151185029Spjd * +---------------------+----------+ | 4152185029Spjd * 15.9 Gbytes ^ 32 Mbytes | 4153185029Spjd * headroom | 4154185029Spjd * l2arc_feed_thread() 4155185029Spjd * | 4156185029Spjd * l2arc write hand <--[oooo]--' 4157185029Spjd * | 8 Mbyte 4158185029Spjd * | write max 4159185029Spjd * V 4160185029Spjd * +==============================+ 4161185029Spjd * L2ARC dev |####|#|###|###| |####| ... | 4162185029Spjd * +==============================+ 4163185029Spjd * 32 Gbytes 4164185029Spjd * 4165185029Spjd * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 4166185029Spjd * evicted, then the L2ARC has cached a buffer much sooner than it probably 4167185029Spjd * needed to, potentially wasting L2ARC device bandwidth and storage. It is 4168185029Spjd * safe to say that this is an uncommon case, since buffers at the end of 4169185029Spjd * the ARC lists have moved there due to inactivity. 4170185029Spjd * 4171185029Spjd * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 4172185029Spjd * then the L2ARC simply misses copying some buffers. This serves as a 4173185029Spjd * pressure valve to prevent heavy read workloads from both stalling the ARC 4174185029Spjd * with waits and clogging the L2ARC with writes. This also helps prevent 4175185029Spjd * the potential for the L2ARC to churn if it attempts to cache content too 4176185029Spjd * quickly, such as during backups of the entire pool. 4177185029Spjd * 4178185029Spjd * 5. After system boot and before the ARC has filled main memory, there are 4179185029Spjd * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 4180185029Spjd * lists can remain mostly static. Instead of searching from tail of these 4181185029Spjd * lists as pictured, the l2arc_feed_thread() will search from the list heads 4182185029Spjd * for eligible buffers, greatly increasing its chance of finding them. 4183185029Spjd * 4184185029Spjd * The L2ARC device write speed is also boosted during this time so that 4185185029Spjd * the L2ARC warms up faster. Since there have been no ARC evictions yet, 4186185029Spjd * there are no L2ARC reads, and no fear of degrading read performance 4187185029Spjd * through increased writes. 4188185029Spjd * 4189185029Spjd * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 4190185029Spjd * the vdev queue can aggregate them into larger and fewer writes. Each 4191185029Spjd * device is written to in a rotor fashion, sweeping writes through 4192185029Spjd * available space then repeating. 4193185029Spjd * 4194185029Spjd * 7. The L2ARC does not store dirty content. It never needs to flush 4195185029Spjd * write buffers back to disk based storage. 4196185029Spjd * 4197185029Spjd * 8. If an ARC buffer is written (and dirtied) which also exists in the 4198185029Spjd * L2ARC, the now stale L2ARC buffer is immediately dropped. 4199185029Spjd * 4200185029Spjd * The performance of the L2ARC can be tweaked by a number of tunables, which 4201185029Spjd * may be necessary for different workloads: 4202185029Spjd * 4203185029Spjd * l2arc_write_max max write bytes per interval 4204185029Spjd * l2arc_write_boost extra write bytes during device warmup 4205185029Spjd * l2arc_noprefetch skip caching prefetched buffers 4206185029Spjd * l2arc_headroom number of max device writes to precache 4207185029Spjd * l2arc_feed_secs seconds between L2ARC writing 4208185029Spjd * 4209185029Spjd * Tunables may be removed or added as future performance improvements are 4210185029Spjd * integrated, and also may become zpool properties. 4211208373Smm * 4212208373Smm * There are three key functions that control how the L2ARC warms up: 4213208373Smm * 4214208373Smm * l2arc_write_eligible() check if a buffer is eligible to cache 4215208373Smm * l2arc_write_size() calculate how much to write 4216208373Smm * l2arc_write_interval() calculate sleep delay between writes 4217208373Smm * 4218208373Smm * These three functions determine what to write, how much, and how quickly 4219208373Smm * to send writes. 4220185029Spjd */ 4221185029Spjd 4222208373Smmstatic boolean_t 4223209962Smml2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 4224208373Smm{ 4225208373Smm /* 4226208373Smm * A buffer is *not* eligible for the L2ARC if it: 4227208373Smm * 1. belongs to a different spa. 4228208373Smm * 2. is already cached on the L2ARC. 4229208373Smm * 3. has an I/O in progress (it may be an incomplete read). 4230208373Smm * 4. is flagged not eligible (zfs property). 4231208373Smm */ 4232209962Smm if (ab->b_spa != spa_guid) { 4233208373Smm ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 4234208373Smm return (B_FALSE); 4235208373Smm } 4236208373Smm if (ab->b_l2hdr != NULL) { 4237208373Smm ARCSTAT_BUMP(arcstat_l2_write_in_l2); 4238208373Smm return (B_FALSE); 4239208373Smm } 4240208373Smm if (HDR_IO_IN_PROGRESS(ab)) { 4241208373Smm ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 4242208373Smm return (B_FALSE); 4243208373Smm } 4244208373Smm if (!HDR_L2CACHE(ab)) { 4245208373Smm ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 4246208373Smm return (B_FALSE); 4247208373Smm } 4248208373Smm 4249208373Smm return (B_TRUE); 4250208373Smm} 4251208373Smm 4252208373Smmstatic uint64_t 4253208373Smml2arc_write_size(l2arc_dev_t *dev) 4254208373Smm{ 4255208373Smm uint64_t size; 4256208373Smm 4257208373Smm size = dev->l2ad_write; 4258208373Smm 4259208373Smm if (arc_warm == B_FALSE) 4260208373Smm size += dev->l2ad_boost; 4261208373Smm 4262208373Smm return (size); 4263208373Smm 4264208373Smm} 4265208373Smm 4266208373Smmstatic clock_t 4267208373Smml2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 4268208373Smm{ 4269219089Spjd clock_t interval, next, now; 4270208373Smm 4271208373Smm /* 4272208373Smm * If the ARC lists are busy, increase our write rate; if the 4273208373Smm * lists are stale, idle back. This is achieved by checking 4274208373Smm * how much we previously wrote - if it was more than half of 4275208373Smm * what we wanted, schedule the next write much sooner. 4276208373Smm */ 4277208373Smm if (l2arc_feed_again && wrote > (wanted / 2)) 4278208373Smm interval = (hz * l2arc_feed_min_ms) / 1000; 4279208373Smm else 4280208373Smm interval = hz * l2arc_feed_secs; 4281208373Smm 4282219089Spjd now = ddi_get_lbolt(); 4283219089Spjd next = MAX(now, MIN(now + interval, began + interval)); 4284208373Smm 4285208373Smm return (next); 4286208373Smm} 4287208373Smm 4288185029Spjdstatic void 4289185029Spjdl2arc_hdr_stat_add(void) 4290185029Spjd{ 4291185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 4292185029Spjd ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 4293185029Spjd} 4294185029Spjd 4295185029Spjdstatic void 4296185029Spjdl2arc_hdr_stat_remove(void) 4297185029Spjd{ 4298185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 4299185029Spjd ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 4300185029Spjd} 4301185029Spjd 4302185029Spjd/* 4303185029Spjd * Cycle through L2ARC devices. This is how L2ARC load balances. 4304185029Spjd * If a device is returned, this also returns holding the spa config lock. 4305185029Spjd */ 4306185029Spjdstatic l2arc_dev_t * 4307185029Spjdl2arc_dev_get_next(void) 4308185029Spjd{ 4309185029Spjd l2arc_dev_t *first, *next = NULL; 4310185029Spjd 4311185029Spjd /* 4312185029Spjd * Lock out the removal of spas (spa_namespace_lock), then removal 4313185029Spjd * of cache devices (l2arc_dev_mtx). Once a device has been selected, 4314185029Spjd * both locks will be dropped and a spa config lock held instead. 4315185029Spjd */ 4316185029Spjd mutex_enter(&spa_namespace_lock); 4317185029Spjd mutex_enter(&l2arc_dev_mtx); 4318185029Spjd 4319185029Spjd /* if there are no vdevs, there is nothing to do */ 4320185029Spjd if (l2arc_ndev == 0) 4321185029Spjd goto out; 4322185029Spjd 4323185029Spjd first = NULL; 4324185029Spjd next = l2arc_dev_last; 4325185029Spjd do { 4326185029Spjd /* loop around the list looking for a non-faulted vdev */ 4327185029Spjd if (next == NULL) { 4328185029Spjd next = list_head(l2arc_dev_list); 4329185029Spjd } else { 4330185029Spjd next = list_next(l2arc_dev_list, next); 4331185029Spjd if (next == NULL) 4332185029Spjd next = list_head(l2arc_dev_list); 4333185029Spjd } 4334185029Spjd 4335185029Spjd /* if we have come back to the start, bail out */ 4336185029Spjd if (first == NULL) 4337185029Spjd first = next; 4338185029Spjd else if (next == first) 4339185029Spjd break; 4340185029Spjd 4341185029Spjd } while (vdev_is_dead(next->l2ad_vdev)); 4342185029Spjd 4343185029Spjd /* if we were unable to find any usable vdevs, return NULL */ 4344185029Spjd if (vdev_is_dead(next->l2ad_vdev)) 4345185029Spjd next = NULL; 4346185029Spjd 4347185029Spjd l2arc_dev_last = next; 4348185029Spjd 4349185029Spjdout: 4350185029Spjd mutex_exit(&l2arc_dev_mtx); 4351185029Spjd 4352185029Spjd /* 4353185029Spjd * Grab the config lock to prevent the 'next' device from being 4354185029Spjd * removed while we are writing to it. 4355185029Spjd */ 4356185029Spjd if (next != NULL) 4357185029Spjd spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 4358185029Spjd mutex_exit(&spa_namespace_lock); 4359185029Spjd 4360185029Spjd return (next); 4361185029Spjd} 4362185029Spjd 4363185029Spjd/* 4364185029Spjd * Free buffers that were tagged for destruction. 4365185029Spjd */ 4366185029Spjdstatic void 4367185029Spjdl2arc_do_free_on_write() 4368185029Spjd{ 4369185029Spjd list_t *buflist; 4370185029Spjd l2arc_data_free_t *df, *df_prev; 4371185029Spjd 4372185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 4373185029Spjd buflist = l2arc_free_on_write; 4374185029Spjd 4375185029Spjd for (df = list_tail(buflist); df; df = df_prev) { 4376185029Spjd df_prev = list_prev(buflist, df); 4377185029Spjd ASSERT(df->l2df_data != NULL); 4378185029Spjd ASSERT(df->l2df_func != NULL); 4379185029Spjd df->l2df_func(df->l2df_data, df->l2df_size); 4380185029Spjd list_remove(buflist, df); 4381185029Spjd kmem_free(df, sizeof (l2arc_data_free_t)); 4382185029Spjd } 4383185029Spjd 4384185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 4385185029Spjd} 4386185029Spjd 4387185029Spjd/* 4388185029Spjd * A write to a cache device has completed. Update all headers to allow 4389185029Spjd * reads from these buffers to begin. 4390185029Spjd */ 4391185029Spjdstatic void 4392185029Spjdl2arc_write_done(zio_t *zio) 4393185029Spjd{ 4394185029Spjd l2arc_write_callback_t *cb; 4395185029Spjd l2arc_dev_t *dev; 4396185029Spjd list_t *buflist; 4397185029Spjd arc_buf_hdr_t *head, *ab, *ab_prev; 4398185029Spjd l2arc_buf_hdr_t *abl2; 4399185029Spjd kmutex_t *hash_lock; 4400185029Spjd 4401185029Spjd cb = zio->io_private; 4402185029Spjd ASSERT(cb != NULL); 4403185029Spjd dev = cb->l2wcb_dev; 4404185029Spjd ASSERT(dev != NULL); 4405185029Spjd head = cb->l2wcb_head; 4406185029Spjd ASSERT(head != NULL); 4407185029Spjd buflist = dev->l2ad_buflist; 4408185029Spjd ASSERT(buflist != NULL); 4409185029Spjd DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 4410185029Spjd l2arc_write_callback_t *, cb); 4411185029Spjd 4412185029Spjd if (zio->io_error != 0) 4413185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_error); 4414185029Spjd 4415185029Spjd mutex_enter(&l2arc_buflist_mtx); 4416185029Spjd 4417185029Spjd /* 4418185029Spjd * All writes completed, or an error was hit. 4419185029Spjd */ 4420185029Spjd for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 4421185029Spjd ab_prev = list_prev(buflist, ab); 4422185029Spjd 4423185029Spjd hash_lock = HDR_LOCK(ab); 4424185029Spjd if (!mutex_tryenter(hash_lock)) { 4425185029Spjd /* 4426185029Spjd * This buffer misses out. It may be in a stage 4427185029Spjd * of eviction. Its ARC_L2_WRITING flag will be 4428185029Spjd * left set, denying reads to this buffer. 4429185029Spjd */ 4430185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 4431185029Spjd continue; 4432185029Spjd } 4433185029Spjd 4434185029Spjd if (zio->io_error != 0) { 4435185029Spjd /* 4436185029Spjd * Error - drop L2ARC entry. 4437185029Spjd */ 4438185029Spjd list_remove(buflist, ab); 4439185029Spjd abl2 = ab->b_l2hdr; 4440185029Spjd ab->b_l2hdr = NULL; 4441185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4442185029Spjd ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4443185029Spjd } 4444185029Spjd 4445185029Spjd /* 4446185029Spjd * Allow ARC to begin reads to this L2ARC entry. 4447185029Spjd */ 4448185029Spjd ab->b_flags &= ~ARC_L2_WRITING; 4449185029Spjd 4450185029Spjd mutex_exit(hash_lock); 4451185029Spjd } 4452185029Spjd 4453185029Spjd atomic_inc_64(&l2arc_writes_done); 4454185029Spjd list_remove(buflist, head); 4455185029Spjd kmem_cache_free(hdr_cache, head); 4456185029Spjd mutex_exit(&l2arc_buflist_mtx); 4457185029Spjd 4458185029Spjd l2arc_do_free_on_write(); 4459185029Spjd 4460185029Spjd kmem_free(cb, sizeof (l2arc_write_callback_t)); 4461185029Spjd} 4462185029Spjd 4463185029Spjd/* 4464185029Spjd * A read to a cache device completed. Validate buffer contents before 4465185029Spjd * handing over to the regular ARC routines. 4466185029Spjd */ 4467185029Spjdstatic void 4468185029Spjdl2arc_read_done(zio_t *zio) 4469185029Spjd{ 4470185029Spjd l2arc_read_callback_t *cb; 4471185029Spjd arc_buf_hdr_t *hdr; 4472185029Spjd arc_buf_t *buf; 4473185029Spjd kmutex_t *hash_lock; 4474185029Spjd int equal; 4475185029Spjd 4476185029Spjd ASSERT(zio->io_vd != NULL); 4477185029Spjd ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 4478185029Spjd 4479185029Spjd spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 4480185029Spjd 4481185029Spjd cb = zio->io_private; 4482185029Spjd ASSERT(cb != NULL); 4483185029Spjd buf = cb->l2rcb_buf; 4484185029Spjd ASSERT(buf != NULL); 4485185029Spjd 4486219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 4487185029Spjd mutex_enter(hash_lock); 4488219089Spjd hdr = buf->b_hdr; 4489219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4490185029Spjd 4491185029Spjd /* 4492185029Spjd * Check this survived the L2ARC journey. 4493185029Spjd */ 4494185029Spjd equal = arc_cksum_equal(buf); 4495185029Spjd if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 4496185029Spjd mutex_exit(hash_lock); 4497185029Spjd zio->io_private = buf; 4498185029Spjd zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 4499185029Spjd zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 4500185029Spjd arc_read_done(zio); 4501185029Spjd } else { 4502185029Spjd mutex_exit(hash_lock); 4503185029Spjd /* 4504185029Spjd * Buffer didn't survive caching. Increment stats and 4505185029Spjd * reissue to the original storage device. 4506185029Spjd */ 4507185029Spjd if (zio->io_error != 0) { 4508185029Spjd ARCSTAT_BUMP(arcstat_l2_io_error); 4509185029Spjd } else { 4510185029Spjd zio->io_error = EIO; 4511185029Spjd } 4512185029Spjd if (!equal) 4513185029Spjd ARCSTAT_BUMP(arcstat_l2_cksum_bad); 4514185029Spjd 4515185029Spjd /* 4516185029Spjd * If there's no waiter, issue an async i/o to the primary 4517185029Spjd * storage now. If there *is* a waiter, the caller must 4518185029Spjd * issue the i/o in a context where it's OK to block. 4519185029Spjd */ 4520209962Smm if (zio->io_waiter == NULL) { 4521209962Smm zio_t *pio = zio_unique_parent(zio); 4522209962Smm 4523209962Smm ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 4524209962Smm 4525209962Smm zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 4526185029Spjd buf->b_data, zio->io_size, arc_read_done, buf, 4527185029Spjd zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 4528209962Smm } 4529185029Spjd } 4530185029Spjd 4531185029Spjd kmem_free(cb, sizeof (l2arc_read_callback_t)); 4532185029Spjd} 4533185029Spjd 4534185029Spjd/* 4535185029Spjd * This is the list priority from which the L2ARC will search for pages to 4536185029Spjd * cache. This is used within loops (0..3) to cycle through lists in the 4537185029Spjd * desired order. This order can have a significant effect on cache 4538185029Spjd * performance. 4539185029Spjd * 4540185029Spjd * Currently the metadata lists are hit first, MFU then MRU, followed by 4541185029Spjd * the data lists. This function returns a locked list, and also returns 4542185029Spjd * the lock pointer. 4543185029Spjd */ 4544185029Spjdstatic list_t * 4545185029Spjdl2arc_list_locked(int list_num, kmutex_t **lock) 4546185029Spjd{ 4547185029Spjd list_t *list; 4548205231Skmacy int idx; 4549185029Spjd 4550206796Spjd ASSERT(list_num >= 0 && list_num < 2 * ARC_BUFC_NUMLISTS); 4551206796Spjd 4552205231Skmacy if (list_num < ARC_BUFC_NUMMETADATALISTS) { 4553205231Skmacy idx = list_num; 4554205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4555205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4556206796Spjd } else if (list_num < ARC_BUFC_NUMMETADATALISTS * 2) { 4557205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4558205231Skmacy list = &arc_mru->arcs_lists[idx]; 4559205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4560206796Spjd } else if (list_num < (ARC_BUFC_NUMMETADATALISTS * 2 + 4561205231Skmacy ARC_BUFC_NUMDATALISTS)) { 4562205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4563205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4564205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4565205231Skmacy } else { 4566205231Skmacy idx = list_num - ARC_BUFC_NUMLISTS; 4567205231Skmacy list = &arc_mru->arcs_lists[idx]; 4568205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4569185029Spjd } 4570185029Spjd 4571185029Spjd ASSERT(!(MUTEX_HELD(*lock))); 4572185029Spjd mutex_enter(*lock); 4573185029Spjd return (list); 4574185029Spjd} 4575185029Spjd 4576185029Spjd/* 4577185029Spjd * Evict buffers from the device write hand to the distance specified in 4578185029Spjd * bytes. This distance may span populated buffers, it may span nothing. 4579185029Spjd * This is clearing a region on the L2ARC device ready for writing. 4580185029Spjd * If the 'all' boolean is set, every buffer is evicted. 4581185029Spjd */ 4582185029Spjdstatic void 4583185029Spjdl2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 4584185029Spjd{ 4585185029Spjd list_t *buflist; 4586185029Spjd l2arc_buf_hdr_t *abl2; 4587185029Spjd arc_buf_hdr_t *ab, *ab_prev; 4588185029Spjd kmutex_t *hash_lock; 4589185029Spjd uint64_t taddr; 4590185029Spjd 4591185029Spjd buflist = dev->l2ad_buflist; 4592185029Spjd 4593185029Spjd if (buflist == NULL) 4594185029Spjd return; 4595185029Spjd 4596185029Spjd if (!all && dev->l2ad_first) { 4597185029Spjd /* 4598185029Spjd * This is the first sweep through the device. There is 4599185029Spjd * nothing to evict. 4600185029Spjd */ 4601185029Spjd return; 4602185029Spjd } 4603185029Spjd 4604185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 4605185029Spjd /* 4606185029Spjd * When nearing the end of the device, evict to the end 4607185029Spjd * before the device write hand jumps to the start. 4608185029Spjd */ 4609185029Spjd taddr = dev->l2ad_end; 4610185029Spjd } else { 4611185029Spjd taddr = dev->l2ad_hand + distance; 4612185029Spjd } 4613185029Spjd DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 4614185029Spjd uint64_t, taddr, boolean_t, all); 4615185029Spjd 4616185029Spjdtop: 4617185029Spjd mutex_enter(&l2arc_buflist_mtx); 4618185029Spjd for (ab = list_tail(buflist); ab; ab = ab_prev) { 4619185029Spjd ab_prev = list_prev(buflist, ab); 4620185029Spjd 4621185029Spjd hash_lock = HDR_LOCK(ab); 4622185029Spjd if (!mutex_tryenter(hash_lock)) { 4623185029Spjd /* 4624185029Spjd * Missed the hash lock. Retry. 4625185029Spjd */ 4626185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 4627185029Spjd mutex_exit(&l2arc_buflist_mtx); 4628185029Spjd mutex_enter(hash_lock); 4629185029Spjd mutex_exit(hash_lock); 4630185029Spjd goto top; 4631185029Spjd } 4632185029Spjd 4633185029Spjd if (HDR_L2_WRITE_HEAD(ab)) { 4634185029Spjd /* 4635185029Spjd * We hit a write head node. Leave it for 4636185029Spjd * l2arc_write_done(). 4637185029Spjd */ 4638185029Spjd list_remove(buflist, ab); 4639185029Spjd mutex_exit(hash_lock); 4640185029Spjd continue; 4641185029Spjd } 4642185029Spjd 4643185029Spjd if (!all && ab->b_l2hdr != NULL && 4644185029Spjd (ab->b_l2hdr->b_daddr > taddr || 4645185029Spjd ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 4646185029Spjd /* 4647185029Spjd * We've evicted to the target address, 4648185029Spjd * or the end of the device. 4649185029Spjd */ 4650185029Spjd mutex_exit(hash_lock); 4651185029Spjd break; 4652185029Spjd } 4653185029Spjd 4654185029Spjd if (HDR_FREE_IN_PROGRESS(ab)) { 4655185029Spjd /* 4656185029Spjd * Already on the path to destruction. 4657185029Spjd */ 4658185029Spjd mutex_exit(hash_lock); 4659185029Spjd continue; 4660185029Spjd } 4661185029Spjd 4662185029Spjd if (ab->b_state == arc_l2c_only) { 4663185029Spjd ASSERT(!HDR_L2_READING(ab)); 4664185029Spjd /* 4665185029Spjd * This doesn't exist in the ARC. Destroy. 4666185029Spjd * arc_hdr_destroy() will call list_remove() 4667185029Spjd * and decrement arcstat_l2_size. 4668185029Spjd */ 4669185029Spjd arc_change_state(arc_anon, ab, hash_lock); 4670185029Spjd arc_hdr_destroy(ab); 4671185029Spjd } else { 4672185029Spjd /* 4673185029Spjd * Invalidate issued or about to be issued 4674185029Spjd * reads, since we may be about to write 4675185029Spjd * over this location. 4676185029Spjd */ 4677185029Spjd if (HDR_L2_READING(ab)) { 4678185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_reading); 4679185029Spjd ab->b_flags |= ARC_L2_EVICTED; 4680185029Spjd } 4681185029Spjd 4682185029Spjd /* 4683185029Spjd * Tell ARC this no longer exists in L2ARC. 4684185029Spjd */ 4685185029Spjd if (ab->b_l2hdr != NULL) { 4686185029Spjd abl2 = ab->b_l2hdr; 4687185029Spjd ab->b_l2hdr = NULL; 4688185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4689185029Spjd ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4690185029Spjd } 4691185029Spjd list_remove(buflist, ab); 4692185029Spjd 4693185029Spjd /* 4694185029Spjd * This may have been leftover after a 4695185029Spjd * failed write. 4696185029Spjd */ 4697185029Spjd ab->b_flags &= ~ARC_L2_WRITING; 4698185029Spjd } 4699185029Spjd mutex_exit(hash_lock); 4700185029Spjd } 4701185029Spjd mutex_exit(&l2arc_buflist_mtx); 4702185029Spjd 4703219089Spjd vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0); 4704185029Spjd dev->l2ad_evict = taddr; 4705185029Spjd} 4706185029Spjd 4707185029Spjd/* 4708185029Spjd * Find and write ARC buffers to the L2ARC device. 4709185029Spjd * 4710185029Spjd * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 4711185029Spjd * for reading until they have completed writing. 4712185029Spjd */ 4713208373Smmstatic uint64_t 4714185029Spjdl2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 4715185029Spjd{ 4716185029Spjd arc_buf_hdr_t *ab, *ab_prev, *head; 4717185029Spjd l2arc_buf_hdr_t *hdrl2; 4718185029Spjd list_t *list; 4719185029Spjd uint64_t passed_sz, write_sz, buf_sz, headroom; 4720185029Spjd void *buf_data; 4721185029Spjd kmutex_t *hash_lock, *list_lock; 4722185029Spjd boolean_t have_lock, full; 4723185029Spjd l2arc_write_callback_t *cb; 4724185029Spjd zio_t *pio, *wzio; 4725228103Smm uint64_t guid = spa_load_guid(spa); 4726185029Spjd int try; 4727185029Spjd 4728185029Spjd ASSERT(dev->l2ad_vdev != NULL); 4729185029Spjd 4730185029Spjd pio = NULL; 4731185029Spjd write_sz = 0; 4732185029Spjd full = B_FALSE; 4733185029Spjd head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 4734185029Spjd head->b_flags |= ARC_L2_WRITE_HEAD; 4735185029Spjd 4736205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 4737185029Spjd /* 4738185029Spjd * Copy buffers for L2ARC writing. 4739185029Spjd */ 4740185029Spjd mutex_enter(&l2arc_buflist_mtx); 4741206796Spjd for (try = 0; try < 2 * ARC_BUFC_NUMLISTS; try++) { 4742185029Spjd list = l2arc_list_locked(try, &list_lock); 4743185029Spjd passed_sz = 0; 4744205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 4745185029Spjd 4746185029Spjd /* 4747185029Spjd * L2ARC fast warmup. 4748185029Spjd * 4749185029Spjd * Until the ARC is warm and starts to evict, read from the 4750185029Spjd * head of the ARC lists rather than the tail. 4751185029Spjd */ 4752185029Spjd headroom = target_sz * l2arc_headroom; 4753185029Spjd if (arc_warm == B_FALSE) 4754185029Spjd ab = list_head(list); 4755185029Spjd else 4756185029Spjd ab = list_tail(list); 4757206796Spjd if (ab == NULL) 4758205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 4759185029Spjd 4760185029Spjd for (; ab; ab = ab_prev) { 4761185029Spjd if (arc_warm == B_FALSE) 4762185029Spjd ab_prev = list_next(list, ab); 4763185029Spjd else 4764185029Spjd ab_prev = list_prev(list, ab); 4765205231Skmacy ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, ab->b_size); 4766206796Spjd 4767185029Spjd hash_lock = HDR_LOCK(ab); 4768185029Spjd have_lock = MUTEX_HELD(hash_lock); 4769185029Spjd if (!have_lock && !mutex_tryenter(hash_lock)) { 4770205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 4771185029Spjd /* 4772185029Spjd * Skip this buffer rather than waiting. 4773185029Spjd */ 4774185029Spjd continue; 4775185029Spjd } 4776185029Spjd 4777185029Spjd passed_sz += ab->b_size; 4778185029Spjd if (passed_sz > headroom) { 4779185029Spjd /* 4780185029Spjd * Searched too far. 4781185029Spjd */ 4782185029Spjd mutex_exit(hash_lock); 4783205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 4784185029Spjd break; 4785185029Spjd } 4786185029Spjd 4787209962Smm if (!l2arc_write_eligible(guid, ab)) { 4788185029Spjd mutex_exit(hash_lock); 4789185029Spjd continue; 4790185029Spjd } 4791185029Spjd 4792185029Spjd if ((write_sz + ab->b_size) > target_sz) { 4793185029Spjd full = B_TRUE; 4794185029Spjd mutex_exit(hash_lock); 4795205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_full); 4796185029Spjd break; 4797185029Spjd } 4798185029Spjd 4799185029Spjd if (pio == NULL) { 4800185029Spjd /* 4801185029Spjd * Insert a dummy header on the buflist so 4802185029Spjd * l2arc_write_done() can find where the 4803185029Spjd * write buffers begin without searching. 4804185029Spjd */ 4805185029Spjd list_insert_head(dev->l2ad_buflist, head); 4806185029Spjd 4807185029Spjd cb = kmem_alloc( 4808185029Spjd sizeof (l2arc_write_callback_t), KM_SLEEP); 4809185029Spjd cb->l2wcb_dev = dev; 4810185029Spjd cb->l2wcb_head = head; 4811185029Spjd pio = zio_root(spa, l2arc_write_done, cb, 4812185029Spjd ZIO_FLAG_CANFAIL); 4813205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_pios); 4814185029Spjd } 4815185029Spjd 4816185029Spjd /* 4817185029Spjd * Create and add a new L2ARC header. 4818185029Spjd */ 4819185029Spjd hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 4820185029Spjd hdrl2->b_dev = dev; 4821185029Spjd hdrl2->b_daddr = dev->l2ad_hand; 4822185029Spjd 4823206792Spjd ab->b_flags |= ARC_L2_WRITING; 4824185029Spjd ab->b_l2hdr = hdrl2; 4825185029Spjd list_insert_head(dev->l2ad_buflist, ab); 4826185029Spjd buf_data = ab->b_buf->b_data; 4827185029Spjd buf_sz = ab->b_size; 4828185029Spjd 4829185029Spjd /* 4830185029Spjd * Compute and store the buffer cksum before 4831185029Spjd * writing. On debug the cksum is verified first. 4832185029Spjd */ 4833185029Spjd arc_cksum_verify(ab->b_buf); 4834185029Spjd arc_cksum_compute(ab->b_buf, B_TRUE); 4835185029Spjd 4836185029Spjd mutex_exit(hash_lock); 4837185029Spjd 4838185029Spjd wzio = zio_write_phys(pio, dev->l2ad_vdev, 4839185029Spjd dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 4840185029Spjd NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 4841185029Spjd ZIO_FLAG_CANFAIL, B_FALSE); 4842185029Spjd 4843185029Spjd DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 4844185029Spjd zio_t *, wzio); 4845185029Spjd (void) zio_nowait(wzio); 4846185029Spjd 4847185029Spjd /* 4848185029Spjd * Keep the clock hand suitably device-aligned. 4849185029Spjd */ 4850185029Spjd buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 4851185029Spjd 4852185029Spjd write_sz += buf_sz; 4853185029Spjd dev->l2ad_hand += buf_sz; 4854185029Spjd } 4855185029Spjd 4856185029Spjd mutex_exit(list_lock); 4857185029Spjd 4858185029Spjd if (full == B_TRUE) 4859185029Spjd break; 4860185029Spjd } 4861185029Spjd mutex_exit(&l2arc_buflist_mtx); 4862185029Spjd 4863185029Spjd if (pio == NULL) { 4864240415Smm ASSERT0(write_sz); 4865185029Spjd kmem_cache_free(hdr_cache, head); 4866208373Smm return (0); 4867185029Spjd } 4868185029Spjd 4869185029Spjd ASSERT3U(write_sz, <=, target_sz); 4870185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_sent); 4871208373Smm ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 4872185029Spjd ARCSTAT_INCR(arcstat_l2_size, write_sz); 4873219089Spjd vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0); 4874185029Spjd 4875185029Spjd /* 4876185029Spjd * Bump device hand to the device start if it is approaching the end. 4877185029Spjd * l2arc_evict() will already have evicted ahead for this case. 4878185029Spjd */ 4879185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 4880219089Spjd vdev_space_update(dev->l2ad_vdev, 4881219089Spjd dev->l2ad_end - dev->l2ad_hand, 0, 0); 4882185029Spjd dev->l2ad_hand = dev->l2ad_start; 4883185029Spjd dev->l2ad_evict = dev->l2ad_start; 4884185029Spjd dev->l2ad_first = B_FALSE; 4885185029Spjd } 4886185029Spjd 4887208373Smm dev->l2ad_writing = B_TRUE; 4888185029Spjd (void) zio_wait(pio); 4889208373Smm dev->l2ad_writing = B_FALSE; 4890208373Smm 4891208373Smm return (write_sz); 4892185029Spjd} 4893185029Spjd 4894185029Spjd/* 4895185029Spjd * This thread feeds the L2ARC at regular intervals. This is the beating 4896185029Spjd * heart of the L2ARC. 4897185029Spjd */ 4898185029Spjdstatic void 4899185029Spjdl2arc_feed_thread(void *dummy __unused) 4900185029Spjd{ 4901185029Spjd callb_cpr_t cpr; 4902185029Spjd l2arc_dev_t *dev; 4903185029Spjd spa_t *spa; 4904208373Smm uint64_t size, wrote; 4905219089Spjd clock_t begin, next = ddi_get_lbolt(); 4906185029Spjd 4907185029Spjd CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 4908185029Spjd 4909185029Spjd mutex_enter(&l2arc_feed_thr_lock); 4910185029Spjd 4911185029Spjd while (l2arc_thread_exit == 0) { 4912185029Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 4913185029Spjd (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 4914219089Spjd next - ddi_get_lbolt()); 4915185029Spjd CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 4916219089Spjd next = ddi_get_lbolt() + hz; 4917185029Spjd 4918185029Spjd /* 4919185029Spjd * Quick check for L2ARC devices. 4920185029Spjd */ 4921185029Spjd mutex_enter(&l2arc_dev_mtx); 4922185029Spjd if (l2arc_ndev == 0) { 4923185029Spjd mutex_exit(&l2arc_dev_mtx); 4924185029Spjd continue; 4925185029Spjd } 4926185029Spjd mutex_exit(&l2arc_dev_mtx); 4927219089Spjd begin = ddi_get_lbolt(); 4928185029Spjd 4929185029Spjd /* 4930185029Spjd * This selects the next l2arc device to write to, and in 4931185029Spjd * doing so the next spa to feed from: dev->l2ad_spa. This 4932185029Spjd * will return NULL if there are now no l2arc devices or if 4933185029Spjd * they are all faulted. 4934185029Spjd * 4935185029Spjd * If a device is returned, its spa's config lock is also 4936185029Spjd * held to prevent device removal. l2arc_dev_get_next() 4937185029Spjd * will grab and release l2arc_dev_mtx. 4938185029Spjd */ 4939185029Spjd if ((dev = l2arc_dev_get_next()) == NULL) 4940185029Spjd continue; 4941185029Spjd 4942185029Spjd spa = dev->l2ad_spa; 4943185029Spjd ASSERT(spa != NULL); 4944185029Spjd 4945185029Spjd /* 4946219089Spjd * If the pool is read-only then force the feed thread to 4947219089Spjd * sleep a little longer. 4948219089Spjd */ 4949219089Spjd if (!spa_writeable(spa)) { 4950219089Spjd next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 4951219089Spjd spa_config_exit(spa, SCL_L2ARC, dev); 4952219089Spjd continue; 4953219089Spjd } 4954219089Spjd 4955219089Spjd /* 4956185029Spjd * Avoid contributing to memory pressure. 4957185029Spjd */ 4958185029Spjd if (arc_reclaim_needed()) { 4959185029Spjd ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 4960185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 4961185029Spjd continue; 4962185029Spjd } 4963185029Spjd 4964185029Spjd ARCSTAT_BUMP(arcstat_l2_feeds); 4965185029Spjd 4966208373Smm size = l2arc_write_size(dev); 4967185029Spjd 4968185029Spjd /* 4969185029Spjd * Evict L2ARC buffers that will be overwritten. 4970185029Spjd */ 4971185029Spjd l2arc_evict(dev, size, B_FALSE); 4972185029Spjd 4973185029Spjd /* 4974185029Spjd * Write ARC buffers. 4975185029Spjd */ 4976208373Smm wrote = l2arc_write_buffers(spa, dev, size); 4977208373Smm 4978208373Smm /* 4979208373Smm * Calculate interval between writes. 4980208373Smm */ 4981208373Smm next = l2arc_write_interval(begin, size, wrote); 4982185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 4983185029Spjd } 4984185029Spjd 4985185029Spjd l2arc_thread_exit = 0; 4986185029Spjd cv_broadcast(&l2arc_feed_thr_cv); 4987185029Spjd CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 4988185029Spjd thread_exit(); 4989185029Spjd} 4990185029Spjd 4991185029Spjdboolean_t 4992185029Spjdl2arc_vdev_present(vdev_t *vd) 4993185029Spjd{ 4994185029Spjd l2arc_dev_t *dev; 4995185029Spjd 4996185029Spjd mutex_enter(&l2arc_dev_mtx); 4997185029Spjd for (dev = list_head(l2arc_dev_list); dev != NULL; 4998185029Spjd dev = list_next(l2arc_dev_list, dev)) { 4999185029Spjd if (dev->l2ad_vdev == vd) 5000185029Spjd break; 5001185029Spjd } 5002185029Spjd mutex_exit(&l2arc_dev_mtx); 5003185029Spjd 5004185029Spjd return (dev != NULL); 5005185029Spjd} 5006185029Spjd 5007185029Spjd/* 5008185029Spjd * Add a vdev for use by the L2ARC. By this point the spa has already 5009185029Spjd * validated the vdev and opened it. 5010185029Spjd */ 5011185029Spjdvoid 5012219089Spjdl2arc_add_vdev(spa_t *spa, vdev_t *vd) 5013185029Spjd{ 5014185029Spjd l2arc_dev_t *adddev; 5015185029Spjd 5016185029Spjd ASSERT(!l2arc_vdev_present(vd)); 5017185029Spjd 5018185029Spjd /* 5019185029Spjd * Create a new l2arc device entry. 5020185029Spjd */ 5021185029Spjd adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 5022185029Spjd adddev->l2ad_spa = spa; 5023185029Spjd adddev->l2ad_vdev = vd; 5024185029Spjd adddev->l2ad_write = l2arc_write_max; 5025185029Spjd adddev->l2ad_boost = l2arc_write_boost; 5026219089Spjd adddev->l2ad_start = VDEV_LABEL_START_SIZE; 5027219089Spjd adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 5028185029Spjd adddev->l2ad_hand = adddev->l2ad_start; 5029185029Spjd adddev->l2ad_evict = adddev->l2ad_start; 5030185029Spjd adddev->l2ad_first = B_TRUE; 5031208373Smm adddev->l2ad_writing = B_FALSE; 5032185029Spjd ASSERT3U(adddev->l2ad_write, >, 0); 5033185029Spjd 5034185029Spjd /* 5035185029Spjd * This is a list of all ARC buffers that are still valid on the 5036185029Spjd * device. 5037185029Spjd */ 5038185029Spjd adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 5039185029Spjd list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 5040185029Spjd offsetof(arc_buf_hdr_t, b_l2node)); 5041185029Spjd 5042219089Spjd vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 5043185029Spjd 5044185029Spjd /* 5045185029Spjd * Add device to global list 5046185029Spjd */ 5047185029Spjd mutex_enter(&l2arc_dev_mtx); 5048185029Spjd list_insert_head(l2arc_dev_list, adddev); 5049185029Spjd atomic_inc_64(&l2arc_ndev); 5050185029Spjd mutex_exit(&l2arc_dev_mtx); 5051185029Spjd} 5052185029Spjd 5053185029Spjd/* 5054185029Spjd * Remove a vdev from the L2ARC. 5055185029Spjd */ 5056185029Spjdvoid 5057185029Spjdl2arc_remove_vdev(vdev_t *vd) 5058185029Spjd{ 5059185029Spjd l2arc_dev_t *dev, *nextdev, *remdev = NULL; 5060185029Spjd 5061185029Spjd /* 5062185029Spjd * Find the device by vdev 5063185029Spjd */ 5064185029Spjd mutex_enter(&l2arc_dev_mtx); 5065185029Spjd for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 5066185029Spjd nextdev = list_next(l2arc_dev_list, dev); 5067185029Spjd if (vd == dev->l2ad_vdev) { 5068185029Spjd remdev = dev; 5069185029Spjd break; 5070185029Spjd } 5071185029Spjd } 5072185029Spjd ASSERT(remdev != NULL); 5073185029Spjd 5074185029Spjd /* 5075185029Spjd * Remove device from global list 5076185029Spjd */ 5077185029Spjd list_remove(l2arc_dev_list, remdev); 5078185029Spjd l2arc_dev_last = NULL; /* may have been invalidated */ 5079185029Spjd atomic_dec_64(&l2arc_ndev); 5080185029Spjd mutex_exit(&l2arc_dev_mtx); 5081185029Spjd 5082185029Spjd /* 5083185029Spjd * Clear all buflists and ARC references. L2ARC device flush. 5084185029Spjd */ 5085185029Spjd l2arc_evict(remdev, 0, B_TRUE); 5086185029Spjd list_destroy(remdev->l2ad_buflist); 5087185029Spjd kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 5088185029Spjd kmem_free(remdev, sizeof (l2arc_dev_t)); 5089185029Spjd} 5090185029Spjd 5091185029Spjdvoid 5092185029Spjdl2arc_init(void) 5093185029Spjd{ 5094185029Spjd l2arc_thread_exit = 0; 5095185029Spjd l2arc_ndev = 0; 5096185029Spjd l2arc_writes_sent = 0; 5097185029Spjd l2arc_writes_done = 0; 5098185029Spjd 5099185029Spjd mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 5100185029Spjd cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 5101185029Spjd mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 5102185029Spjd mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 5103185029Spjd mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 5104185029Spjd 5105185029Spjd l2arc_dev_list = &L2ARC_dev_list; 5106185029Spjd l2arc_free_on_write = &L2ARC_free_on_write; 5107185029Spjd list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 5108185029Spjd offsetof(l2arc_dev_t, l2ad_node)); 5109185029Spjd list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 5110185029Spjd offsetof(l2arc_data_free_t, l2df_list_node)); 5111185029Spjd} 5112185029Spjd 5113185029Spjdvoid 5114185029Spjdl2arc_fini(void) 5115185029Spjd{ 5116185029Spjd /* 5117185029Spjd * This is called from dmu_fini(), which is called from spa_fini(); 5118185029Spjd * Because of this, we can assume that all l2arc devices have 5119185029Spjd * already been removed when the pools themselves were removed. 5120185029Spjd */ 5121185029Spjd 5122185029Spjd l2arc_do_free_on_write(); 5123185029Spjd 5124185029Spjd mutex_destroy(&l2arc_feed_thr_lock); 5125185029Spjd cv_destroy(&l2arc_feed_thr_cv); 5126185029Spjd mutex_destroy(&l2arc_dev_mtx); 5127185029Spjd mutex_destroy(&l2arc_buflist_mtx); 5128185029Spjd mutex_destroy(&l2arc_free_on_write_mtx); 5129185029Spjd 5130185029Spjd list_destroy(l2arc_dev_list); 5131185029Spjd list_destroy(l2arc_free_on_write); 5132185029Spjd} 5133185029Spjd 5134185029Spjdvoid 5135185029Spjdl2arc_start(void) 5136185029Spjd{ 5137209962Smm if (!(spa_mode_global & FWRITE)) 5138185029Spjd return; 5139185029Spjd 5140185029Spjd (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 5141185029Spjd TS_RUN, minclsyspri); 5142185029Spjd} 5143185029Spjd 5144185029Spjdvoid 5145185029Spjdl2arc_stop(void) 5146185029Spjd{ 5147209962Smm if (!(spa_mode_global & FWRITE)) 5148185029Spjd return; 5149185029Spjd 5150185029Spjd mutex_enter(&l2arc_feed_thr_lock); 5151185029Spjd cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 5152185029Spjd l2arc_thread_exit = 1; 5153185029Spjd while (l2arc_thread_exit != 0) 5154185029Spjd cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 5155185029Spjd mutex_exit(&l2arc_feed_thr_lock); 5156185029Spjd} 5157