arc.c revision 240133
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; 194185029Spjd 195185029SpjdTUNABLE_QUAD("vfs.zfs.arc_max", &zfs_arc_max); 196185029SpjdTUNABLE_QUAD("vfs.zfs.arc_min", &zfs_arc_min); 197185029SpjdTUNABLE_QUAD("vfs.zfs.arc_meta_limit", &zfs_arc_meta_limit); 198168473SpjdSYSCTL_DECL(_vfs_zfs); 199217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_max, CTLFLAG_RDTUN, &zfs_arc_max, 0, 200168473Spjd "Maximum ARC size"); 201217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_min, CTLFLAG_RDTUN, &zfs_arc_min, 0, 202168473Spjd "Minimum ARC size"); 203168404Spjd 204168404Spjd/* 205185029Spjd * Note that buffers can be in one of 6 states: 206168404Spjd * ARC_anon - anonymous (discussed below) 207168404Spjd * ARC_mru - recently used, currently cached 208168404Spjd * ARC_mru_ghost - recentely used, no longer in cache 209168404Spjd * ARC_mfu - frequently used, currently cached 210168404Spjd * ARC_mfu_ghost - frequently used, no longer in cache 211185029Spjd * ARC_l2c_only - exists in L2ARC but not other states 212185029Spjd * When there are no active references to the buffer, they are 213185029Spjd * are linked onto a list in one of these arc states. These are 214185029Spjd * the only buffers that can be evicted or deleted. Within each 215185029Spjd * state there are multiple lists, one for meta-data and one for 216185029Spjd * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 217185029Spjd * etc.) is tracked separately so that it can be managed more 218185029Spjd * explicitly: favored over data, limited explicitly. 219168404Spjd * 220168404Spjd * Anonymous buffers are buffers that are not associated with 221168404Spjd * a DVA. These are buffers that hold dirty block copies 222168404Spjd * before they are written to stable storage. By definition, 223168404Spjd * they are "ref'd" and are considered part of arc_mru 224168404Spjd * that cannot be freed. Generally, they will aquire a DVA 225168404Spjd * as they are written and migrate onto the arc_mru list. 226185029Spjd * 227185029Spjd * The ARC_l2c_only state is for buffers that are in the second 228185029Spjd * level ARC but no longer in any of the ARC_m* lists. The second 229185029Spjd * level ARC itself may also contain buffers that are in any of 230185029Spjd * the ARC_m* states - meaning that a buffer can exist in two 231185029Spjd * places. The reason for the ARC_l2c_only state is to keep the 232185029Spjd * buffer header in the hash table, so that reads that hit the 233185029Spjd * second level ARC benefit from these fast lookups. 234168404Spjd */ 235168404Spjd 236205264Skmacy#define ARCS_LOCK_PAD CACHE_LINE_SIZE 237205231Skmacystruct arcs_lock { 238205231Skmacy kmutex_t arcs_lock; 239205231Skmacy#ifdef _KERNEL 240205231Skmacy unsigned char pad[(ARCS_LOCK_PAD - sizeof (kmutex_t))]; 241205231Skmacy#endif 242205231Skmacy}; 243205231Skmacy 244205231Skmacy/* 245205231Skmacy * must be power of two for mask use to work 246205231Skmacy * 247205231Skmacy */ 248205231Skmacy#define ARC_BUFC_NUMDATALISTS 16 249205231Skmacy#define ARC_BUFC_NUMMETADATALISTS 16 250206796Spjd#define ARC_BUFC_NUMLISTS (ARC_BUFC_NUMMETADATALISTS + ARC_BUFC_NUMDATALISTS) 251205231Skmacy 252168404Spjdtypedef struct arc_state { 253185029Spjd uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */ 254185029Spjd uint64_t arcs_size; /* total amount of data in this state */ 255205231Skmacy list_t arcs_lists[ARC_BUFC_NUMLISTS]; /* list of evictable buffers */ 256205264Skmacy struct arcs_lock arcs_locks[ARC_BUFC_NUMLISTS] __aligned(CACHE_LINE_SIZE); 257168404Spjd} arc_state_t; 258168404Spjd 259206796Spjd#define ARCS_LOCK(s, i) (&((s)->arcs_locks[(i)].arcs_lock)) 260205231Skmacy 261185029Spjd/* The 6 states: */ 262168404Spjdstatic arc_state_t ARC_anon; 263168404Spjdstatic arc_state_t ARC_mru; 264168404Spjdstatic arc_state_t ARC_mru_ghost; 265168404Spjdstatic arc_state_t ARC_mfu; 266168404Spjdstatic arc_state_t ARC_mfu_ghost; 267185029Spjdstatic arc_state_t ARC_l2c_only; 268168404Spjd 269168404Spjdtypedef struct arc_stats { 270168404Spjd kstat_named_t arcstat_hits; 271168404Spjd kstat_named_t arcstat_misses; 272168404Spjd kstat_named_t arcstat_demand_data_hits; 273168404Spjd kstat_named_t arcstat_demand_data_misses; 274168404Spjd kstat_named_t arcstat_demand_metadata_hits; 275168404Spjd kstat_named_t arcstat_demand_metadata_misses; 276168404Spjd kstat_named_t arcstat_prefetch_data_hits; 277168404Spjd kstat_named_t arcstat_prefetch_data_misses; 278168404Spjd kstat_named_t arcstat_prefetch_metadata_hits; 279168404Spjd kstat_named_t arcstat_prefetch_metadata_misses; 280168404Spjd kstat_named_t arcstat_mru_hits; 281168404Spjd kstat_named_t arcstat_mru_ghost_hits; 282168404Spjd kstat_named_t arcstat_mfu_hits; 283168404Spjd kstat_named_t arcstat_mfu_ghost_hits; 284205231Skmacy kstat_named_t arcstat_allocated; 285168404Spjd kstat_named_t arcstat_deleted; 286205231Skmacy kstat_named_t arcstat_stolen; 287168404Spjd kstat_named_t arcstat_recycle_miss; 288168404Spjd kstat_named_t arcstat_mutex_miss; 289168404Spjd kstat_named_t arcstat_evict_skip; 290208373Smm kstat_named_t arcstat_evict_l2_cached; 291208373Smm kstat_named_t arcstat_evict_l2_eligible; 292208373Smm kstat_named_t arcstat_evict_l2_ineligible; 293168404Spjd kstat_named_t arcstat_hash_elements; 294168404Spjd kstat_named_t arcstat_hash_elements_max; 295168404Spjd kstat_named_t arcstat_hash_collisions; 296168404Spjd kstat_named_t arcstat_hash_chains; 297168404Spjd kstat_named_t arcstat_hash_chain_max; 298168404Spjd kstat_named_t arcstat_p; 299168404Spjd kstat_named_t arcstat_c; 300168404Spjd kstat_named_t arcstat_c_min; 301168404Spjd kstat_named_t arcstat_c_max; 302168404Spjd kstat_named_t arcstat_size; 303185029Spjd kstat_named_t arcstat_hdr_size; 304208373Smm kstat_named_t arcstat_data_size; 305208373Smm kstat_named_t arcstat_other_size; 306185029Spjd kstat_named_t arcstat_l2_hits; 307185029Spjd kstat_named_t arcstat_l2_misses; 308185029Spjd kstat_named_t arcstat_l2_feeds; 309185029Spjd kstat_named_t arcstat_l2_rw_clash; 310208373Smm kstat_named_t arcstat_l2_read_bytes; 311208373Smm kstat_named_t arcstat_l2_write_bytes; 312185029Spjd kstat_named_t arcstat_l2_writes_sent; 313185029Spjd kstat_named_t arcstat_l2_writes_done; 314185029Spjd kstat_named_t arcstat_l2_writes_error; 315185029Spjd kstat_named_t arcstat_l2_writes_hdr_miss; 316185029Spjd kstat_named_t arcstat_l2_evict_lock_retry; 317185029Spjd kstat_named_t arcstat_l2_evict_reading; 318185029Spjd kstat_named_t arcstat_l2_free_on_write; 319185029Spjd kstat_named_t arcstat_l2_abort_lowmem; 320185029Spjd kstat_named_t arcstat_l2_cksum_bad; 321185029Spjd kstat_named_t arcstat_l2_io_error; 322185029Spjd kstat_named_t arcstat_l2_size; 323185029Spjd kstat_named_t arcstat_l2_hdr_size; 324185029Spjd kstat_named_t arcstat_memory_throttle_count; 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; 337168404Spjd} arc_stats_t; 338168404Spjd 339168404Spjdstatic arc_stats_t arc_stats = { 340168404Spjd { "hits", KSTAT_DATA_UINT64 }, 341168404Spjd { "misses", KSTAT_DATA_UINT64 }, 342168404Spjd { "demand_data_hits", KSTAT_DATA_UINT64 }, 343168404Spjd { "demand_data_misses", KSTAT_DATA_UINT64 }, 344168404Spjd { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 345168404Spjd { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 346168404Spjd { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 347168404Spjd { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 348168404Spjd { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 349168404Spjd { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 350168404Spjd { "mru_hits", KSTAT_DATA_UINT64 }, 351168404Spjd { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 352168404Spjd { "mfu_hits", KSTAT_DATA_UINT64 }, 353168404Spjd { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 354205231Skmacy { "allocated", KSTAT_DATA_UINT64 }, 355168404Spjd { "deleted", KSTAT_DATA_UINT64 }, 356205231Skmacy { "stolen", KSTAT_DATA_UINT64 }, 357168404Spjd { "recycle_miss", KSTAT_DATA_UINT64 }, 358168404Spjd { "mutex_miss", KSTAT_DATA_UINT64 }, 359168404Spjd { "evict_skip", KSTAT_DATA_UINT64 }, 360208373Smm { "evict_l2_cached", KSTAT_DATA_UINT64 }, 361208373Smm { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 362208373Smm { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 363168404Spjd { "hash_elements", KSTAT_DATA_UINT64 }, 364168404Spjd { "hash_elements_max", KSTAT_DATA_UINT64 }, 365168404Spjd { "hash_collisions", KSTAT_DATA_UINT64 }, 366168404Spjd { "hash_chains", KSTAT_DATA_UINT64 }, 367168404Spjd { "hash_chain_max", KSTAT_DATA_UINT64 }, 368168404Spjd { "p", KSTAT_DATA_UINT64 }, 369168404Spjd { "c", KSTAT_DATA_UINT64 }, 370168404Spjd { "c_min", KSTAT_DATA_UINT64 }, 371168404Spjd { "c_max", KSTAT_DATA_UINT64 }, 372185029Spjd { "size", KSTAT_DATA_UINT64 }, 373185029Spjd { "hdr_size", KSTAT_DATA_UINT64 }, 374208373Smm { "data_size", KSTAT_DATA_UINT64 }, 375208373Smm { "other_size", KSTAT_DATA_UINT64 }, 376185029Spjd { "l2_hits", KSTAT_DATA_UINT64 }, 377185029Spjd { "l2_misses", KSTAT_DATA_UINT64 }, 378185029Spjd { "l2_feeds", KSTAT_DATA_UINT64 }, 379185029Spjd { "l2_rw_clash", KSTAT_DATA_UINT64 }, 380208373Smm { "l2_read_bytes", KSTAT_DATA_UINT64 }, 381208373Smm { "l2_write_bytes", KSTAT_DATA_UINT64 }, 382185029Spjd { "l2_writes_sent", KSTAT_DATA_UINT64 }, 383185029Spjd { "l2_writes_done", KSTAT_DATA_UINT64 }, 384185029Spjd { "l2_writes_error", KSTAT_DATA_UINT64 }, 385185029Spjd { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 }, 386185029Spjd { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 387185029Spjd { "l2_evict_reading", KSTAT_DATA_UINT64 }, 388185029Spjd { "l2_free_on_write", KSTAT_DATA_UINT64 }, 389185029Spjd { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 390185029Spjd { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 391185029Spjd { "l2_io_error", KSTAT_DATA_UINT64 }, 392185029Spjd { "l2_size", KSTAT_DATA_UINT64 }, 393185029Spjd { "l2_hdr_size", KSTAT_DATA_UINT64 }, 394205231Skmacy { "memory_throttle_count", KSTAT_DATA_UINT64 }, 395206796Spjd { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 396206796Spjd { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 397206796Spjd { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 398206796Spjd { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 399206796Spjd { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 400206796Spjd { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 401206796Spjd { "l2_write_full", KSTAT_DATA_UINT64 }, 402206796Spjd { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 403206796Spjd { "l2_write_pios", KSTAT_DATA_UINT64 }, 404206796Spjd { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 405206796Spjd { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 406206796Spjd { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 } 407168404Spjd}; 408168404Spjd 409168404Spjd#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 410168404Spjd 411168404Spjd#define ARCSTAT_INCR(stat, val) \ 412168404Spjd atomic_add_64(&arc_stats.stat.value.ui64, (val)); 413168404Spjd 414206796Spjd#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 415168404Spjd#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 416168404Spjd 417168404Spjd#define ARCSTAT_MAX(stat, val) { \ 418168404Spjd uint64_t m; \ 419168404Spjd while ((val) > (m = arc_stats.stat.value.ui64) && \ 420168404Spjd (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 421168404Spjd continue; \ 422168404Spjd} 423168404Spjd 424168404Spjd#define ARCSTAT_MAXSTAT(stat) \ 425168404Spjd ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 426168404Spjd 427168404Spjd/* 428168404Spjd * We define a macro to allow ARC hits/misses to be easily broken down by 429168404Spjd * two separate conditions, giving a total of four different subtypes for 430168404Spjd * each of hits and misses (so eight statistics total). 431168404Spjd */ 432168404Spjd#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 433168404Spjd if (cond1) { \ 434168404Spjd if (cond2) { \ 435168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 436168404Spjd } else { \ 437168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 438168404Spjd } \ 439168404Spjd } else { \ 440168404Spjd if (cond2) { \ 441168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 442168404Spjd } else { \ 443168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 444168404Spjd } \ 445168404Spjd } 446168404Spjd 447168404Spjdkstat_t *arc_ksp; 448206796Spjdstatic arc_state_t *arc_anon; 449168404Spjdstatic arc_state_t *arc_mru; 450168404Spjdstatic arc_state_t *arc_mru_ghost; 451168404Spjdstatic arc_state_t *arc_mfu; 452168404Spjdstatic arc_state_t *arc_mfu_ghost; 453185029Spjdstatic arc_state_t *arc_l2c_only; 454168404Spjd 455168404Spjd/* 456168404Spjd * There are several ARC variables that are critical to export as kstats -- 457168404Spjd * but we don't want to have to grovel around in the kstat whenever we wish to 458168404Spjd * manipulate them. For these variables, we therefore define them to be in 459168404Spjd * terms of the statistic variable. This assures that we are not introducing 460168404Spjd * the possibility of inconsistency by having shadow copies of the variables, 461168404Spjd * while still allowing the code to be readable. 462168404Spjd */ 463168404Spjd#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 464168404Spjd#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 465168404Spjd#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 466168404Spjd#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 467168404Spjd#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 468168404Spjd 469168404Spjdstatic int arc_no_grow; /* Don't try to grow cache size */ 470168404Spjdstatic uint64_t arc_tempreserve; 471209962Smmstatic uint64_t arc_loaned_bytes; 472185029Spjdstatic uint64_t arc_meta_used; 473185029Spjdstatic uint64_t arc_meta_limit; 474185029Spjdstatic uint64_t arc_meta_max = 0; 475229663SpjdSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_used, CTLFLAG_RD, &arc_meta_used, 0, 476229663Spjd "ARC metadata used"); 477229663SpjdSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_limit, CTLFLAG_RW, &arc_meta_limit, 0, 478229663Spjd "ARC metadata limit"); 479168404Spjd 480185029Spjdtypedef struct l2arc_buf_hdr l2arc_buf_hdr_t; 481185029Spjd 482168404Spjdtypedef struct arc_callback arc_callback_t; 483168404Spjd 484168404Spjdstruct arc_callback { 485168404Spjd void *acb_private; 486168404Spjd arc_done_func_t *acb_done; 487168404Spjd arc_buf_t *acb_buf; 488168404Spjd zio_t *acb_zio_dummy; 489168404Spjd arc_callback_t *acb_next; 490168404Spjd}; 491168404Spjd 492168404Spjdtypedef struct arc_write_callback arc_write_callback_t; 493168404Spjd 494168404Spjdstruct arc_write_callback { 495168404Spjd void *awcb_private; 496168404Spjd arc_done_func_t *awcb_ready; 497168404Spjd arc_done_func_t *awcb_done; 498168404Spjd arc_buf_t *awcb_buf; 499168404Spjd}; 500168404Spjd 501168404Spjdstruct arc_buf_hdr { 502168404Spjd /* protected by hash lock */ 503168404Spjd dva_t b_dva; 504168404Spjd uint64_t b_birth; 505168404Spjd uint64_t b_cksum0; 506168404Spjd 507168404Spjd kmutex_t b_freeze_lock; 508168404Spjd zio_cksum_t *b_freeze_cksum; 509219089Spjd void *b_thawed; 510168404Spjd 511168404Spjd arc_buf_hdr_t *b_hash_next; 512168404Spjd arc_buf_t *b_buf; 513168404Spjd uint32_t b_flags; 514168404Spjd uint32_t b_datacnt; 515168404Spjd 516168404Spjd arc_callback_t *b_acb; 517168404Spjd kcondvar_t b_cv; 518168404Spjd 519168404Spjd /* immutable */ 520168404Spjd arc_buf_contents_t b_type; 521168404Spjd uint64_t b_size; 522209962Smm uint64_t b_spa; 523168404Spjd 524168404Spjd /* protected by arc state mutex */ 525168404Spjd arc_state_t *b_state; 526168404Spjd list_node_t b_arc_node; 527168404Spjd 528168404Spjd /* updated atomically */ 529168404Spjd clock_t b_arc_access; 530168404Spjd 531168404Spjd /* self protecting */ 532168404Spjd refcount_t b_refcnt; 533185029Spjd 534185029Spjd l2arc_buf_hdr_t *b_l2hdr; 535185029Spjd list_node_t b_l2node; 536168404Spjd}; 537168404Spjd 538168404Spjdstatic arc_buf_t *arc_eviction_list; 539168404Spjdstatic kmutex_t arc_eviction_mtx; 540168404Spjdstatic arc_buf_hdr_t arc_eviction_hdr; 541168404Spjdstatic void arc_get_data_buf(arc_buf_t *buf); 542168404Spjdstatic void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 543185029Spjdstatic int arc_evict_needed(arc_buf_contents_t type); 544209962Smmstatic void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes); 545240133Smm#ifdef illumos 546240133Smmstatic void arc_buf_watch(arc_buf_t *buf); 547240133Smm#endif /* illumos */ 548168404Spjd 549209962Smmstatic boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab); 550208373Smm 551168404Spjd#define GHOST_STATE(state) \ 552185029Spjd ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 553185029Spjd (state) == arc_l2c_only) 554168404Spjd 555168404Spjd/* 556168404Spjd * Private ARC flags. These flags are private ARC only flags that will show up 557168404Spjd * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 558168404Spjd * be passed in as arc_flags in things like arc_read. However, these flags 559168404Spjd * should never be passed and should only be set by ARC code. When adding new 560168404Spjd * public flags, make sure not to smash the private ones. 561168404Spjd */ 562168404Spjd 563168404Spjd#define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 564168404Spjd#define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 565168404Spjd#define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 566168404Spjd#define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 567168404Spjd#define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 568168404Spjd#define ARC_INDIRECT (1 << 14) /* this is an indirect block */ 569185029Spjd#define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */ 570185029Spjd#define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */ 571185029Spjd#define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */ 572185029Spjd#define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */ 573168404Spjd 574168404Spjd#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 575168404Spjd#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 576168404Spjd#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 577208373Smm#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH) 578168404Spjd#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 579168404Spjd#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 580185029Spjd#define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS) 581185029Spjd#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE) 582185029Spjd#define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \ 583185029Spjd (hdr)->b_l2hdr != NULL) 584185029Spjd#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING) 585185029Spjd#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED) 586185029Spjd#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD) 587168404Spjd 588168404Spjd/* 589185029Spjd * Other sizes 590185029Spjd */ 591185029Spjd 592185029Spjd#define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 593185029Spjd#define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t)) 594185029Spjd 595185029Spjd/* 596168404Spjd * Hash table routines 597168404Spjd */ 598168404Spjd 599205253Skmacy#define HT_LOCK_PAD CACHE_LINE_SIZE 600168404Spjd 601168404Spjdstruct ht_lock { 602168404Spjd kmutex_t ht_lock; 603168404Spjd#ifdef _KERNEL 604168404Spjd unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 605168404Spjd#endif 606168404Spjd}; 607168404Spjd 608168404Spjd#define BUF_LOCKS 256 609168404Spjdtypedef struct buf_hash_table { 610168404Spjd uint64_t ht_mask; 611168404Spjd arc_buf_hdr_t **ht_table; 612205264Skmacy struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 613168404Spjd} buf_hash_table_t; 614168404Spjd 615168404Spjdstatic buf_hash_table_t buf_hash_table; 616168404Spjd 617168404Spjd#define BUF_HASH_INDEX(spa, dva, birth) \ 618168404Spjd (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 619168404Spjd#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 620168404Spjd#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 621219089Spjd#define HDR_LOCK(hdr) \ 622219089Spjd (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 623168404Spjd 624168404Spjduint64_t zfs_crc64_table[256]; 625168404Spjd 626185029Spjd/* 627185029Spjd * Level 2 ARC 628185029Spjd */ 629185029Spjd 630208373Smm#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 631208373Smm#define L2ARC_HEADROOM 2 /* num of writes */ 632208373Smm#define L2ARC_FEED_SECS 1 /* caching interval secs */ 633208373Smm#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 634185029Spjd 635185029Spjd#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 636185029Spjd#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 637185029Spjd 638185029Spjd/* 639185029Spjd * L2ARC Performance Tunables 640185029Spjd */ 641185029Spjduint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 642185029Spjduint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 643185029Spjduint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 644185029Spjduint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 645208373Smmuint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 646219089Spjdboolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 647208373Smmboolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 648208373Smmboolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 649185029Spjd 650217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 651205231Skmacy &l2arc_write_max, 0, "max write size"); 652217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 653205231Skmacy &l2arc_write_boost, 0, "extra write during warmup"); 654217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 655205231Skmacy &l2arc_headroom, 0, "number of dev writes"); 656217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 657205231Skmacy &l2arc_feed_secs, 0, "interval seconds"); 658217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 659208373Smm &l2arc_feed_min_ms, 0, "min interval milliseconds"); 660205231Skmacy 661205231SkmacySYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 662205231Skmacy &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 663208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 664208373Smm &l2arc_feed_again, 0, "turbo warmup"); 665208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 666208373Smm &l2arc_norw, 0, "no reads during writes"); 667205231Skmacy 668217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 669205231Skmacy &ARC_anon.arcs_size, 0, "size of anonymous state"); 670217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD, 671205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state"); 672217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD, 673205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state"); 674205231Skmacy 675217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 676205231Skmacy &ARC_mru.arcs_size, 0, "size of mru state"); 677217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD, 678205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state"); 679217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD, 680205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state"); 681205231Skmacy 682217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 683205231Skmacy &ARC_mru_ghost.arcs_size, 0, "size of mru ghost state"); 684217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD, 685205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 686205231Skmacy "size of metadata in mru ghost state"); 687217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD, 688205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 689205231Skmacy "size of data in mru ghost state"); 690205231Skmacy 691217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 692205231Skmacy &ARC_mfu.arcs_size, 0, "size of mfu state"); 693217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD, 694205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state"); 695217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD, 696205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state"); 697205231Skmacy 698217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 699205231Skmacy &ARC_mfu_ghost.arcs_size, 0, "size of mfu ghost state"); 700217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD, 701205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 702205231Skmacy "size of metadata in mfu ghost state"); 703217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD, 704205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 705205231Skmacy "size of data in mfu ghost state"); 706205231Skmacy 707217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 708205231Skmacy &ARC_l2c_only.arcs_size, 0, "size of mru state"); 709205231Skmacy 710185029Spjd/* 711185029Spjd * L2ARC Internals 712185029Spjd */ 713185029Spjdtypedef struct l2arc_dev { 714185029Spjd vdev_t *l2ad_vdev; /* vdev */ 715185029Spjd spa_t *l2ad_spa; /* spa */ 716185029Spjd uint64_t l2ad_hand; /* next write location */ 717185029Spjd uint64_t l2ad_write; /* desired write size, bytes */ 718185029Spjd uint64_t l2ad_boost; /* warmup write boost, bytes */ 719185029Spjd uint64_t l2ad_start; /* first addr on device */ 720185029Spjd uint64_t l2ad_end; /* last addr on device */ 721185029Spjd uint64_t l2ad_evict; /* last addr eviction reached */ 722185029Spjd boolean_t l2ad_first; /* first sweep through */ 723208373Smm boolean_t l2ad_writing; /* currently writing */ 724185029Spjd list_t *l2ad_buflist; /* buffer list */ 725185029Spjd list_node_t l2ad_node; /* device list node */ 726185029Spjd} l2arc_dev_t; 727185029Spjd 728185029Spjdstatic list_t L2ARC_dev_list; /* device list */ 729185029Spjdstatic list_t *l2arc_dev_list; /* device list pointer */ 730185029Spjdstatic kmutex_t l2arc_dev_mtx; /* device list mutex */ 731185029Spjdstatic l2arc_dev_t *l2arc_dev_last; /* last device used */ 732185029Spjdstatic kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */ 733185029Spjdstatic list_t L2ARC_free_on_write; /* free after write buf list */ 734185029Spjdstatic list_t *l2arc_free_on_write; /* free after write list ptr */ 735185029Spjdstatic kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 736185029Spjdstatic uint64_t l2arc_ndev; /* number of devices */ 737185029Spjd 738185029Spjdtypedef struct l2arc_read_callback { 739185029Spjd arc_buf_t *l2rcb_buf; /* read buffer */ 740185029Spjd spa_t *l2rcb_spa; /* spa */ 741185029Spjd blkptr_t l2rcb_bp; /* original blkptr */ 742185029Spjd zbookmark_t l2rcb_zb; /* original bookmark */ 743185029Spjd int l2rcb_flags; /* original flags */ 744185029Spjd} l2arc_read_callback_t; 745185029Spjd 746185029Spjdtypedef struct l2arc_write_callback { 747185029Spjd l2arc_dev_t *l2wcb_dev; /* device info */ 748185029Spjd arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 749185029Spjd} l2arc_write_callback_t; 750185029Spjd 751185029Spjdstruct l2arc_buf_hdr { 752185029Spjd /* protected by arc_buf_hdr mutex */ 753185029Spjd l2arc_dev_t *b_dev; /* L2ARC device */ 754208373Smm uint64_t b_daddr; /* disk address, offset byte */ 755185029Spjd}; 756185029Spjd 757185029Spjdtypedef struct l2arc_data_free { 758185029Spjd /* protected by l2arc_free_on_write_mtx */ 759185029Spjd void *l2df_data; 760185029Spjd size_t l2df_size; 761185029Spjd void (*l2df_func)(void *, size_t); 762185029Spjd list_node_t l2df_list_node; 763185029Spjd} l2arc_data_free_t; 764185029Spjd 765185029Spjdstatic kmutex_t l2arc_feed_thr_lock; 766185029Spjdstatic kcondvar_t l2arc_feed_thr_cv; 767185029Spjdstatic uint8_t l2arc_thread_exit; 768185029Spjd 769185029Spjdstatic void l2arc_read_done(zio_t *zio); 770185029Spjdstatic void l2arc_hdr_stat_add(void); 771185029Spjdstatic void l2arc_hdr_stat_remove(void); 772185029Spjd 773168404Spjdstatic uint64_t 774209962Smmbuf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 775168404Spjd{ 776168404Spjd uint8_t *vdva = (uint8_t *)dva; 777168404Spjd uint64_t crc = -1ULL; 778168404Spjd int i; 779168404Spjd 780168404Spjd ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 781168404Spjd 782168404Spjd for (i = 0; i < sizeof (dva_t); i++) 783168404Spjd crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 784168404Spjd 785209962Smm crc ^= (spa>>8) ^ birth; 786168404Spjd 787168404Spjd return (crc); 788168404Spjd} 789168404Spjd 790168404Spjd#define BUF_EMPTY(buf) \ 791168404Spjd ((buf)->b_dva.dva_word[0] == 0 && \ 792168404Spjd (buf)->b_dva.dva_word[1] == 0 && \ 793168404Spjd (buf)->b_birth == 0) 794168404Spjd 795168404Spjd#define BUF_EQUAL(spa, dva, birth, buf) \ 796168404Spjd ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 797168404Spjd ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 798168404Spjd ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 799168404Spjd 800219089Spjdstatic void 801219089Spjdbuf_discard_identity(arc_buf_hdr_t *hdr) 802219089Spjd{ 803219089Spjd hdr->b_dva.dva_word[0] = 0; 804219089Spjd hdr->b_dva.dva_word[1] = 0; 805219089Spjd hdr->b_birth = 0; 806219089Spjd hdr->b_cksum0 = 0; 807219089Spjd} 808219089Spjd 809168404Spjdstatic arc_buf_hdr_t * 810209962Smmbuf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp) 811168404Spjd{ 812168404Spjd uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 813168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 814168404Spjd arc_buf_hdr_t *buf; 815168404Spjd 816168404Spjd mutex_enter(hash_lock); 817168404Spjd for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 818168404Spjd buf = buf->b_hash_next) { 819168404Spjd if (BUF_EQUAL(spa, dva, birth, buf)) { 820168404Spjd *lockp = hash_lock; 821168404Spjd return (buf); 822168404Spjd } 823168404Spjd } 824168404Spjd mutex_exit(hash_lock); 825168404Spjd *lockp = NULL; 826168404Spjd return (NULL); 827168404Spjd} 828168404Spjd 829168404Spjd/* 830168404Spjd * Insert an entry into the hash table. If there is already an element 831168404Spjd * equal to elem in the hash table, then the already existing element 832168404Spjd * will be returned and the new element will not be inserted. 833168404Spjd * Otherwise returns NULL. 834168404Spjd */ 835168404Spjdstatic arc_buf_hdr_t * 836168404Spjdbuf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 837168404Spjd{ 838168404Spjd uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 839168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 840168404Spjd arc_buf_hdr_t *fbuf; 841168404Spjd uint32_t i; 842168404Spjd 843168404Spjd ASSERT(!HDR_IN_HASH_TABLE(buf)); 844168404Spjd *lockp = hash_lock; 845168404Spjd mutex_enter(hash_lock); 846168404Spjd for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 847168404Spjd fbuf = fbuf->b_hash_next, i++) { 848168404Spjd if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 849168404Spjd return (fbuf); 850168404Spjd } 851168404Spjd 852168404Spjd buf->b_hash_next = buf_hash_table.ht_table[idx]; 853168404Spjd buf_hash_table.ht_table[idx] = buf; 854168404Spjd buf->b_flags |= ARC_IN_HASH_TABLE; 855168404Spjd 856168404Spjd /* collect some hash table performance data */ 857168404Spjd if (i > 0) { 858168404Spjd ARCSTAT_BUMP(arcstat_hash_collisions); 859168404Spjd if (i == 1) 860168404Spjd ARCSTAT_BUMP(arcstat_hash_chains); 861168404Spjd 862168404Spjd ARCSTAT_MAX(arcstat_hash_chain_max, i); 863168404Spjd } 864168404Spjd 865168404Spjd ARCSTAT_BUMP(arcstat_hash_elements); 866168404Spjd ARCSTAT_MAXSTAT(arcstat_hash_elements); 867168404Spjd 868168404Spjd return (NULL); 869168404Spjd} 870168404Spjd 871168404Spjdstatic void 872168404Spjdbuf_hash_remove(arc_buf_hdr_t *buf) 873168404Spjd{ 874168404Spjd arc_buf_hdr_t *fbuf, **bufp; 875168404Spjd uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 876168404Spjd 877168404Spjd ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 878168404Spjd ASSERT(HDR_IN_HASH_TABLE(buf)); 879168404Spjd 880168404Spjd bufp = &buf_hash_table.ht_table[idx]; 881168404Spjd while ((fbuf = *bufp) != buf) { 882168404Spjd ASSERT(fbuf != NULL); 883168404Spjd bufp = &fbuf->b_hash_next; 884168404Spjd } 885168404Spjd *bufp = buf->b_hash_next; 886168404Spjd buf->b_hash_next = NULL; 887168404Spjd buf->b_flags &= ~ARC_IN_HASH_TABLE; 888168404Spjd 889168404Spjd /* collect some hash table performance data */ 890168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_elements); 891168404Spjd 892168404Spjd if (buf_hash_table.ht_table[idx] && 893168404Spjd buf_hash_table.ht_table[idx]->b_hash_next == NULL) 894168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_chains); 895168404Spjd} 896168404Spjd 897168404Spjd/* 898168404Spjd * Global data structures and functions for the buf kmem cache. 899168404Spjd */ 900168404Spjdstatic kmem_cache_t *hdr_cache; 901168404Spjdstatic kmem_cache_t *buf_cache; 902168404Spjd 903168404Spjdstatic void 904168404Spjdbuf_fini(void) 905168404Spjd{ 906168404Spjd int i; 907168404Spjd 908168404Spjd kmem_free(buf_hash_table.ht_table, 909168404Spjd (buf_hash_table.ht_mask + 1) * sizeof (void *)); 910168404Spjd for (i = 0; i < BUF_LOCKS; i++) 911168404Spjd mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 912168404Spjd kmem_cache_destroy(hdr_cache); 913168404Spjd kmem_cache_destroy(buf_cache); 914168404Spjd} 915168404Spjd 916168404Spjd/* 917168404Spjd * Constructor callback - called when the cache is empty 918168404Spjd * and a new buf is requested. 919168404Spjd */ 920168404Spjd/* ARGSUSED */ 921168404Spjdstatic int 922168404Spjdhdr_cons(void *vbuf, void *unused, int kmflag) 923168404Spjd{ 924168404Spjd arc_buf_hdr_t *buf = vbuf; 925168404Spjd 926168404Spjd bzero(buf, sizeof (arc_buf_hdr_t)); 927168404Spjd refcount_create(&buf->b_refcnt); 928168404Spjd cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 929185029Spjd mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 930208373Smm arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 931185029Spjd 932168404Spjd return (0); 933168404Spjd} 934168404Spjd 935185029Spjd/* ARGSUSED */ 936185029Spjdstatic int 937185029Spjdbuf_cons(void *vbuf, void *unused, int kmflag) 938185029Spjd{ 939185029Spjd arc_buf_t *buf = vbuf; 940185029Spjd 941185029Spjd bzero(buf, sizeof (arc_buf_t)); 942219089Spjd mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 943219089Spjd rw_init(&buf->b_data_lock, NULL, RW_DEFAULT, NULL); 944208373Smm arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 945208373Smm 946185029Spjd return (0); 947185029Spjd} 948185029Spjd 949168404Spjd/* 950168404Spjd * Destructor callback - called when a cached buf is 951168404Spjd * no longer required. 952168404Spjd */ 953168404Spjd/* ARGSUSED */ 954168404Spjdstatic void 955168404Spjdhdr_dest(void *vbuf, void *unused) 956168404Spjd{ 957168404Spjd arc_buf_hdr_t *buf = vbuf; 958168404Spjd 959219089Spjd ASSERT(BUF_EMPTY(buf)); 960168404Spjd refcount_destroy(&buf->b_refcnt); 961168404Spjd cv_destroy(&buf->b_cv); 962185029Spjd mutex_destroy(&buf->b_freeze_lock); 963208373Smm arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS); 964168404Spjd} 965168404Spjd 966185029Spjd/* ARGSUSED */ 967185029Spjdstatic void 968185029Spjdbuf_dest(void *vbuf, void *unused) 969185029Spjd{ 970185029Spjd arc_buf_t *buf = vbuf; 971185029Spjd 972219089Spjd mutex_destroy(&buf->b_evict_lock); 973219089Spjd rw_destroy(&buf->b_data_lock); 974208373Smm arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 975185029Spjd} 976185029Spjd 977168404Spjd/* 978168404Spjd * Reclaim callback -- invoked when memory is low. 979168404Spjd */ 980168404Spjd/* ARGSUSED */ 981168404Spjdstatic void 982168404Spjdhdr_recl(void *unused) 983168404Spjd{ 984168404Spjd dprintf("hdr_recl called\n"); 985168404Spjd /* 986168404Spjd * umem calls the reclaim func when we destroy the buf cache, 987168404Spjd * which is after we do arc_fini(). 988168404Spjd */ 989168404Spjd if (!arc_dead) 990168404Spjd cv_signal(&arc_reclaim_thr_cv); 991168404Spjd} 992168404Spjd 993168404Spjdstatic void 994168404Spjdbuf_init(void) 995168404Spjd{ 996168404Spjd uint64_t *ct; 997168404Spjd uint64_t hsize = 1ULL << 12; 998168404Spjd int i, j; 999168404Spjd 1000168404Spjd /* 1001168404Spjd * The hash table is big enough to fill all of physical memory 1002168404Spjd * with an average 64K block size. The table will take up 1003168404Spjd * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 1004168404Spjd */ 1005168696Spjd while (hsize * 65536 < (uint64_t)physmem * PAGESIZE) 1006168404Spjd hsize <<= 1; 1007168404Spjdretry: 1008168404Spjd buf_hash_table.ht_mask = hsize - 1; 1009168404Spjd buf_hash_table.ht_table = 1010168404Spjd kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1011168404Spjd if (buf_hash_table.ht_table == NULL) { 1012168404Spjd ASSERT(hsize > (1ULL << 8)); 1013168404Spjd hsize >>= 1; 1014168404Spjd goto retry; 1015168404Spjd } 1016168404Spjd 1017168404Spjd hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 1018168404Spjd 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 1019168404Spjd buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1020185029Spjd 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1021168404Spjd 1022168404Spjd for (i = 0; i < 256; i++) 1023168404Spjd for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1024168404Spjd *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1025168404Spjd 1026168404Spjd for (i = 0; i < BUF_LOCKS; i++) { 1027168404Spjd mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1028168404Spjd NULL, MUTEX_DEFAULT, NULL); 1029168404Spjd } 1030168404Spjd} 1031168404Spjd 1032168404Spjd#define ARC_MINTIME (hz>>4) /* 62 ms */ 1033168404Spjd 1034168404Spjdstatic void 1035168404Spjdarc_cksum_verify(arc_buf_t *buf) 1036168404Spjd{ 1037168404Spjd zio_cksum_t zc; 1038168404Spjd 1039168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1040168404Spjd return; 1041168404Spjd 1042168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1043168404Spjd if (buf->b_hdr->b_freeze_cksum == NULL || 1044168404Spjd (buf->b_hdr->b_flags & ARC_IO_ERROR)) { 1045168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1046168404Spjd return; 1047168404Spjd } 1048168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1049168404Spjd if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 1050168404Spjd panic("buffer modified while frozen!"); 1051168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1052168404Spjd} 1053168404Spjd 1054185029Spjdstatic int 1055185029Spjdarc_cksum_equal(arc_buf_t *buf) 1056185029Spjd{ 1057185029Spjd zio_cksum_t zc; 1058185029Spjd int equal; 1059185029Spjd 1060185029Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1061185029Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1062185029Spjd equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 1063185029Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1064185029Spjd 1065185029Spjd return (equal); 1066185029Spjd} 1067185029Spjd 1068168404Spjdstatic void 1069185029Spjdarc_cksum_compute(arc_buf_t *buf, boolean_t force) 1070168404Spjd{ 1071185029Spjd if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 1072168404Spjd return; 1073168404Spjd 1074168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1075168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1076168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1077168404Spjd return; 1078168404Spjd } 1079168404Spjd buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 1080168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 1081168404Spjd buf->b_hdr->b_freeze_cksum); 1082168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1083240133Smm#ifdef illumos 1084240133Smm arc_buf_watch(buf); 1085240133Smm#endif /* illumos */ 1086168404Spjd} 1087168404Spjd 1088240133Smm#ifdef illumos 1089240133Smm#ifndef _KERNEL 1090240133Smmtypedef struct procctl { 1091240133Smm long cmd; 1092240133Smm prwatch_t prwatch; 1093240133Smm} procctl_t; 1094240133Smm#endif 1095240133Smm 1096240133Smm/* ARGSUSED */ 1097240133Smmstatic void 1098240133Smmarc_buf_unwatch(arc_buf_t *buf) 1099240133Smm{ 1100240133Smm#ifndef _KERNEL 1101240133Smm if (arc_watch) { 1102240133Smm int result; 1103240133Smm procctl_t ctl; 1104240133Smm ctl.cmd = PCWATCH; 1105240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1106240133Smm ctl.prwatch.pr_size = 0; 1107240133Smm ctl.prwatch.pr_wflags = 0; 1108240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1109240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1110240133Smm } 1111240133Smm#endif 1112240133Smm} 1113240133Smm 1114240133Smm/* ARGSUSED */ 1115240133Smmstatic void 1116240133Smmarc_buf_watch(arc_buf_t *buf) 1117240133Smm{ 1118240133Smm#ifndef _KERNEL 1119240133Smm if (arc_watch) { 1120240133Smm int result; 1121240133Smm procctl_t ctl; 1122240133Smm ctl.cmd = PCWATCH; 1123240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1124240133Smm ctl.prwatch.pr_size = buf->b_hdr->b_size; 1125240133Smm ctl.prwatch.pr_wflags = WA_WRITE; 1126240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1127240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1128240133Smm } 1129240133Smm#endif 1130240133Smm} 1131240133Smm#endif /* illumos */ 1132240133Smm 1133168404Spjdvoid 1134168404Spjdarc_buf_thaw(arc_buf_t *buf) 1135168404Spjd{ 1136185029Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1137185029Spjd if (buf->b_hdr->b_state != arc_anon) 1138185029Spjd panic("modifying non-anon buffer!"); 1139185029Spjd if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS) 1140185029Spjd panic("modifying buffer while i/o in progress!"); 1141185029Spjd arc_cksum_verify(buf); 1142185029Spjd } 1143168404Spjd 1144168404Spjd mutex_enter(&buf->b_hdr->b_freeze_lock); 1145168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1146168404Spjd kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1147168404Spjd buf->b_hdr->b_freeze_cksum = NULL; 1148168404Spjd } 1149219089Spjd 1150219089Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1151219089Spjd if (buf->b_hdr->b_thawed) 1152219089Spjd kmem_free(buf->b_hdr->b_thawed, 1); 1153219089Spjd buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP); 1154219089Spjd } 1155219089Spjd 1156168404Spjd mutex_exit(&buf->b_hdr->b_freeze_lock); 1157240133Smm 1158240133Smm#ifdef illumos 1159240133Smm arc_buf_unwatch(buf); 1160240133Smm#endif /* illumos */ 1161168404Spjd} 1162168404Spjd 1163168404Spjdvoid 1164168404Spjdarc_buf_freeze(arc_buf_t *buf) 1165168404Spjd{ 1166219089Spjd kmutex_t *hash_lock; 1167219089Spjd 1168168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1169168404Spjd return; 1170168404Spjd 1171219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1172219089Spjd mutex_enter(hash_lock); 1173219089Spjd 1174168404Spjd ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 1175168404Spjd buf->b_hdr->b_state == arc_anon); 1176185029Spjd arc_cksum_compute(buf, B_FALSE); 1177219089Spjd mutex_exit(hash_lock); 1178240133Smm 1179168404Spjd} 1180168404Spjd 1181168404Spjdstatic void 1182205231Skmacyget_buf_info(arc_buf_hdr_t *ab, arc_state_t *state, list_t **list, kmutex_t **lock) 1183205231Skmacy{ 1184205231Skmacy uint64_t buf_hashid = buf_hash(ab->b_spa, &ab->b_dva, ab->b_birth); 1185205231Skmacy 1186206796Spjd if (ab->b_type == ARC_BUFC_METADATA) 1187206796Spjd buf_hashid &= (ARC_BUFC_NUMMETADATALISTS - 1); 1188205231Skmacy else { 1189206796Spjd buf_hashid &= (ARC_BUFC_NUMDATALISTS - 1); 1190205231Skmacy buf_hashid += ARC_BUFC_NUMMETADATALISTS; 1191205231Skmacy } 1192205231Skmacy 1193205231Skmacy *list = &state->arcs_lists[buf_hashid]; 1194205231Skmacy *lock = ARCS_LOCK(state, buf_hashid); 1195205231Skmacy} 1196205231Skmacy 1197205231Skmacy 1198205231Skmacystatic void 1199168404Spjdadd_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1200168404Spjd{ 1201168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1202168404Spjd 1203168404Spjd if ((refcount_add(&ab->b_refcnt, tag) == 1) && 1204168404Spjd (ab->b_state != arc_anon)) { 1205206796Spjd uint64_t delta = ab->b_size * ab->b_datacnt; 1206206796Spjd uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type]; 1207205231Skmacy list_t *list; 1208205231Skmacy kmutex_t *lock; 1209168404Spjd 1210205231Skmacy get_buf_info(ab, ab->b_state, &list, &lock); 1211205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1212205231Skmacy mutex_enter(lock); 1213168404Spjd ASSERT(list_link_active(&ab->b_arc_node)); 1214185029Spjd list_remove(list, ab); 1215168404Spjd if (GHOST_STATE(ab->b_state)) { 1216168404Spjd ASSERT3U(ab->b_datacnt, ==, 0); 1217168404Spjd ASSERT3P(ab->b_buf, ==, NULL); 1218168404Spjd delta = ab->b_size; 1219168404Spjd } 1220168404Spjd ASSERT(delta > 0); 1221185029Spjd ASSERT3U(*size, >=, delta); 1222185029Spjd atomic_add_64(size, -delta); 1223206794Spjd mutex_exit(lock); 1224185029Spjd /* remove the prefetch flag if we get a reference */ 1225168404Spjd if (ab->b_flags & ARC_PREFETCH) 1226168404Spjd ab->b_flags &= ~ARC_PREFETCH; 1227168404Spjd } 1228168404Spjd} 1229168404Spjd 1230168404Spjdstatic int 1231168404Spjdremove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 1232168404Spjd{ 1233168404Spjd int cnt; 1234168404Spjd arc_state_t *state = ab->b_state; 1235168404Spjd 1236168404Spjd ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1237168404Spjd ASSERT(!GHOST_STATE(state)); 1238168404Spjd 1239168404Spjd if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 1240168404Spjd (state != arc_anon)) { 1241185029Spjd uint64_t *size = &state->arcs_lsize[ab->b_type]; 1242205231Skmacy list_t *list; 1243205231Skmacy kmutex_t *lock; 1244185029Spjd 1245205231Skmacy get_buf_info(ab, state, &list, &lock); 1246205231Skmacy ASSERT(!MUTEX_HELD(lock)); 1247205231Skmacy mutex_enter(lock); 1248168404Spjd ASSERT(!list_link_active(&ab->b_arc_node)); 1249205231Skmacy list_insert_head(list, ab); 1250168404Spjd ASSERT(ab->b_datacnt > 0); 1251185029Spjd atomic_add_64(size, ab->b_size * ab->b_datacnt); 1252206794Spjd mutex_exit(lock); 1253168404Spjd } 1254168404Spjd return (cnt); 1255168404Spjd} 1256168404Spjd 1257168404Spjd/* 1258168404Spjd * Move the supplied buffer to the indicated state. The mutex 1259168404Spjd * for the buffer must be held by the caller. 1260168404Spjd */ 1261168404Spjdstatic void 1262168404Spjdarc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 1263168404Spjd{ 1264168404Spjd arc_state_t *old_state = ab->b_state; 1265168404Spjd int64_t refcnt = refcount_count(&ab->b_refcnt); 1266168404Spjd uint64_t from_delta, to_delta; 1267205231Skmacy list_t *list; 1268205231Skmacy kmutex_t *lock; 1269168404Spjd 1270168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1271168404Spjd ASSERT(new_state != old_state); 1272168404Spjd ASSERT(refcnt == 0 || ab->b_datacnt > 0); 1273168404Spjd ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 1274219089Spjd ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon); 1275168404Spjd 1276168404Spjd from_delta = to_delta = ab->b_datacnt * ab->b_size; 1277168404Spjd 1278168404Spjd /* 1279168404Spjd * If this buffer is evictable, transfer it from the 1280168404Spjd * old state list to the new state list. 1281168404Spjd */ 1282168404Spjd if (refcnt == 0) { 1283168404Spjd if (old_state != arc_anon) { 1284205231Skmacy int use_mutex; 1285185029Spjd uint64_t *size = &old_state->arcs_lsize[ab->b_type]; 1286168404Spjd 1287205231Skmacy get_buf_info(ab, old_state, &list, &lock); 1288205231Skmacy use_mutex = !MUTEX_HELD(lock); 1289168404Spjd if (use_mutex) 1290205231Skmacy mutex_enter(lock); 1291168404Spjd 1292168404Spjd ASSERT(list_link_active(&ab->b_arc_node)); 1293205231Skmacy list_remove(list, ab); 1294168404Spjd 1295168404Spjd /* 1296168404Spjd * If prefetching out of the ghost cache, 1297219089Spjd * we will have a non-zero datacnt. 1298168404Spjd */ 1299168404Spjd if (GHOST_STATE(old_state) && ab->b_datacnt == 0) { 1300168404Spjd /* ghost elements have a ghost size */ 1301168404Spjd ASSERT(ab->b_buf == NULL); 1302168404Spjd from_delta = ab->b_size; 1303168404Spjd } 1304185029Spjd ASSERT3U(*size, >=, from_delta); 1305185029Spjd atomic_add_64(size, -from_delta); 1306168404Spjd 1307168404Spjd if (use_mutex) 1308205231Skmacy mutex_exit(lock); 1309168404Spjd } 1310168404Spjd if (new_state != arc_anon) { 1311206796Spjd int use_mutex; 1312185029Spjd uint64_t *size = &new_state->arcs_lsize[ab->b_type]; 1313168404Spjd 1314205231Skmacy get_buf_info(ab, new_state, &list, &lock); 1315205231Skmacy use_mutex = !MUTEX_HELD(lock); 1316168404Spjd if (use_mutex) 1317205231Skmacy mutex_enter(lock); 1318168404Spjd 1319205231Skmacy list_insert_head(list, ab); 1320168404Spjd 1321168404Spjd /* ghost elements have a ghost size */ 1322168404Spjd if (GHOST_STATE(new_state)) { 1323168404Spjd ASSERT(ab->b_datacnt == 0); 1324168404Spjd ASSERT(ab->b_buf == NULL); 1325168404Spjd to_delta = ab->b_size; 1326168404Spjd } 1327185029Spjd atomic_add_64(size, to_delta); 1328168404Spjd 1329168404Spjd if (use_mutex) 1330205231Skmacy mutex_exit(lock); 1331168404Spjd } 1332168404Spjd } 1333168404Spjd 1334168404Spjd ASSERT(!BUF_EMPTY(ab)); 1335219089Spjd if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab)) 1336168404Spjd buf_hash_remove(ab); 1337168404Spjd 1338168404Spjd /* adjust state sizes */ 1339168404Spjd if (to_delta) 1340168404Spjd atomic_add_64(&new_state->arcs_size, to_delta); 1341168404Spjd if (from_delta) { 1342168404Spjd ASSERT3U(old_state->arcs_size, >=, from_delta); 1343168404Spjd atomic_add_64(&old_state->arcs_size, -from_delta); 1344168404Spjd } 1345168404Spjd ab->b_state = new_state; 1346185029Spjd 1347185029Spjd /* adjust l2arc hdr stats */ 1348185029Spjd if (new_state == arc_l2c_only) 1349185029Spjd l2arc_hdr_stat_add(); 1350185029Spjd else if (old_state == arc_l2c_only) 1351185029Spjd l2arc_hdr_stat_remove(); 1352168404Spjd} 1353168404Spjd 1354185029Spjdvoid 1355208373Smmarc_space_consume(uint64_t space, arc_space_type_t type) 1356185029Spjd{ 1357208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1358208373Smm 1359208373Smm switch (type) { 1360208373Smm case ARC_SPACE_DATA: 1361208373Smm ARCSTAT_INCR(arcstat_data_size, space); 1362208373Smm break; 1363208373Smm case ARC_SPACE_OTHER: 1364208373Smm ARCSTAT_INCR(arcstat_other_size, space); 1365208373Smm break; 1366208373Smm case ARC_SPACE_HDRS: 1367208373Smm ARCSTAT_INCR(arcstat_hdr_size, space); 1368208373Smm break; 1369208373Smm case ARC_SPACE_L2HDRS: 1370208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, space); 1371208373Smm break; 1372208373Smm } 1373208373Smm 1374185029Spjd atomic_add_64(&arc_meta_used, space); 1375185029Spjd atomic_add_64(&arc_size, space); 1376185029Spjd} 1377185029Spjd 1378185029Spjdvoid 1379208373Smmarc_space_return(uint64_t space, arc_space_type_t type) 1380185029Spjd{ 1381208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1382208373Smm 1383208373Smm switch (type) { 1384208373Smm case ARC_SPACE_DATA: 1385208373Smm ARCSTAT_INCR(arcstat_data_size, -space); 1386208373Smm break; 1387208373Smm case ARC_SPACE_OTHER: 1388208373Smm ARCSTAT_INCR(arcstat_other_size, -space); 1389208373Smm break; 1390208373Smm case ARC_SPACE_HDRS: 1391208373Smm ARCSTAT_INCR(arcstat_hdr_size, -space); 1392208373Smm break; 1393208373Smm case ARC_SPACE_L2HDRS: 1394208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 1395208373Smm break; 1396208373Smm } 1397208373Smm 1398185029Spjd ASSERT(arc_meta_used >= space); 1399185029Spjd if (arc_meta_max < arc_meta_used) 1400185029Spjd arc_meta_max = arc_meta_used; 1401185029Spjd atomic_add_64(&arc_meta_used, -space); 1402185029Spjd ASSERT(arc_size >= space); 1403185029Spjd atomic_add_64(&arc_size, -space); 1404185029Spjd} 1405185029Spjd 1406185029Spjdvoid * 1407185029Spjdarc_data_buf_alloc(uint64_t size) 1408185029Spjd{ 1409185029Spjd if (arc_evict_needed(ARC_BUFC_DATA)) 1410185029Spjd cv_signal(&arc_reclaim_thr_cv); 1411185029Spjd atomic_add_64(&arc_size, size); 1412185029Spjd return (zio_data_buf_alloc(size)); 1413185029Spjd} 1414185029Spjd 1415185029Spjdvoid 1416185029Spjdarc_data_buf_free(void *buf, uint64_t size) 1417185029Spjd{ 1418185029Spjd zio_data_buf_free(buf, size); 1419185029Spjd ASSERT(arc_size >= size); 1420185029Spjd atomic_add_64(&arc_size, -size); 1421185029Spjd} 1422185029Spjd 1423168404Spjdarc_buf_t * 1424168404Spjdarc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type) 1425168404Spjd{ 1426168404Spjd arc_buf_hdr_t *hdr; 1427168404Spjd arc_buf_t *buf; 1428168404Spjd 1429168404Spjd ASSERT3U(size, >, 0); 1430185029Spjd hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 1431168404Spjd ASSERT(BUF_EMPTY(hdr)); 1432168404Spjd hdr->b_size = size; 1433168404Spjd hdr->b_type = type; 1434228103Smm hdr->b_spa = spa_load_guid(spa); 1435168404Spjd hdr->b_state = arc_anon; 1436168404Spjd hdr->b_arc_access = 0; 1437185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1438168404Spjd buf->b_hdr = hdr; 1439168404Spjd buf->b_data = NULL; 1440168404Spjd buf->b_efunc = NULL; 1441168404Spjd buf->b_private = NULL; 1442168404Spjd buf->b_next = NULL; 1443168404Spjd hdr->b_buf = buf; 1444168404Spjd arc_get_data_buf(buf); 1445168404Spjd hdr->b_datacnt = 1; 1446168404Spjd hdr->b_flags = 0; 1447168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1448168404Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1449168404Spjd 1450168404Spjd return (buf); 1451168404Spjd} 1452168404Spjd 1453209962Smmstatic char *arc_onloan_tag = "onloan"; 1454209962Smm 1455209962Smm/* 1456209962Smm * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 1457209962Smm * flight data by arc_tempreserve_space() until they are "returned". Loaned 1458209962Smm * buffers must be returned to the arc before they can be used by the DMU or 1459209962Smm * freed. 1460209962Smm */ 1461209962Smmarc_buf_t * 1462209962Smmarc_loan_buf(spa_t *spa, int size) 1463209962Smm{ 1464209962Smm arc_buf_t *buf; 1465209962Smm 1466209962Smm buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 1467209962Smm 1468209962Smm atomic_add_64(&arc_loaned_bytes, size); 1469209962Smm return (buf); 1470209962Smm} 1471209962Smm 1472209962Smm/* 1473209962Smm * Return a loaned arc buffer to the arc. 1474209962Smm */ 1475209962Smmvoid 1476209962Smmarc_return_buf(arc_buf_t *buf, void *tag) 1477209962Smm{ 1478209962Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1479209962Smm 1480209962Smm ASSERT(buf->b_data != NULL); 1481219089Spjd (void) refcount_add(&hdr->b_refcnt, tag); 1482219089Spjd (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag); 1483209962Smm 1484209962Smm atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 1485209962Smm} 1486209962Smm 1487219089Spjd/* Detach an arc_buf from a dbuf (tag) */ 1488219089Spjdvoid 1489219089Spjdarc_loan_inuse_buf(arc_buf_t *buf, void *tag) 1490219089Spjd{ 1491219089Spjd arc_buf_hdr_t *hdr; 1492219089Spjd 1493219089Spjd ASSERT(buf->b_data != NULL); 1494219089Spjd hdr = buf->b_hdr; 1495219089Spjd (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag); 1496219089Spjd (void) refcount_remove(&hdr->b_refcnt, tag); 1497219089Spjd buf->b_efunc = NULL; 1498219089Spjd buf->b_private = NULL; 1499219089Spjd 1500219089Spjd atomic_add_64(&arc_loaned_bytes, hdr->b_size); 1501219089Spjd} 1502219089Spjd 1503168404Spjdstatic arc_buf_t * 1504168404Spjdarc_buf_clone(arc_buf_t *from) 1505168404Spjd{ 1506168404Spjd arc_buf_t *buf; 1507168404Spjd arc_buf_hdr_t *hdr = from->b_hdr; 1508168404Spjd uint64_t size = hdr->b_size; 1509168404Spjd 1510219089Spjd ASSERT(hdr->b_state != arc_anon); 1511219089Spjd 1512185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 1513168404Spjd buf->b_hdr = hdr; 1514168404Spjd buf->b_data = NULL; 1515168404Spjd buf->b_efunc = NULL; 1516168404Spjd buf->b_private = NULL; 1517168404Spjd buf->b_next = hdr->b_buf; 1518168404Spjd hdr->b_buf = buf; 1519168404Spjd arc_get_data_buf(buf); 1520168404Spjd bcopy(from->b_data, buf->b_data, size); 1521168404Spjd hdr->b_datacnt += 1; 1522168404Spjd return (buf); 1523168404Spjd} 1524168404Spjd 1525168404Spjdvoid 1526168404Spjdarc_buf_add_ref(arc_buf_t *buf, void* tag) 1527168404Spjd{ 1528168404Spjd arc_buf_hdr_t *hdr; 1529168404Spjd kmutex_t *hash_lock; 1530168404Spjd 1531168404Spjd /* 1532185029Spjd * Check to see if this buffer is evicted. Callers 1533185029Spjd * must verify b_data != NULL to know if the add_ref 1534185029Spjd * was successful. 1535168404Spjd */ 1536219089Spjd mutex_enter(&buf->b_evict_lock); 1537185029Spjd if (buf->b_data == NULL) { 1538219089Spjd mutex_exit(&buf->b_evict_lock); 1539168404Spjd return; 1540168404Spjd } 1541219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1542219089Spjd mutex_enter(hash_lock); 1543185029Spjd hdr = buf->b_hdr; 1544219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1545219089Spjd mutex_exit(&buf->b_evict_lock); 1546168404Spjd 1547168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 1548168404Spjd add_reference(hdr, hash_lock, tag); 1549208373Smm DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 1550168404Spjd arc_access(hdr, hash_lock); 1551168404Spjd mutex_exit(hash_lock); 1552168404Spjd ARCSTAT_BUMP(arcstat_hits); 1553168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 1554168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 1555168404Spjd data, metadata, hits); 1556168404Spjd} 1557168404Spjd 1558185029Spjd/* 1559185029Spjd * Free the arc data buffer. If it is an l2arc write in progress, 1560185029Spjd * the buffer is placed on l2arc_free_on_write to be freed later. 1561185029Spjd */ 1562168404Spjdstatic void 1563240133Smmarc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t)) 1564185029Spjd{ 1565240133Smm arc_buf_hdr_t *hdr = buf->b_hdr; 1566240133Smm 1567185029Spjd if (HDR_L2_WRITING(hdr)) { 1568185029Spjd l2arc_data_free_t *df; 1569185029Spjd df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP); 1570240133Smm df->l2df_data = buf->b_data; 1571240133Smm df->l2df_size = hdr->b_size; 1572185029Spjd df->l2df_func = free_func; 1573185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 1574185029Spjd list_insert_head(l2arc_free_on_write, df); 1575185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 1576185029Spjd ARCSTAT_BUMP(arcstat_l2_free_on_write); 1577185029Spjd } else { 1578240133Smm free_func(buf->b_data, hdr->b_size); 1579185029Spjd } 1580185029Spjd} 1581185029Spjd 1582185029Spjdstatic void 1583168404Spjdarc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all) 1584168404Spjd{ 1585168404Spjd arc_buf_t **bufp; 1586168404Spjd 1587168404Spjd /* free up data associated with the buf */ 1588168404Spjd if (buf->b_data) { 1589168404Spjd arc_state_t *state = buf->b_hdr->b_state; 1590168404Spjd uint64_t size = buf->b_hdr->b_size; 1591168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 1592168404Spjd 1593168404Spjd arc_cksum_verify(buf); 1594240133Smm#ifdef illumos 1595240133Smm arc_buf_unwatch(buf); 1596240133Smm#endif /* illumos */ 1597219089Spjd 1598168404Spjd if (!recycle) { 1599168404Spjd if (type == ARC_BUFC_METADATA) { 1600240133Smm arc_buf_data_free(buf, zio_buf_free); 1601208373Smm arc_space_return(size, ARC_SPACE_DATA); 1602168404Spjd } else { 1603168404Spjd ASSERT(type == ARC_BUFC_DATA); 1604240133Smm arc_buf_data_free(buf, zio_data_buf_free); 1605208373Smm ARCSTAT_INCR(arcstat_data_size, -size); 1606185029Spjd atomic_add_64(&arc_size, -size); 1607168404Spjd } 1608168404Spjd } 1609168404Spjd if (list_link_active(&buf->b_hdr->b_arc_node)) { 1610185029Spjd uint64_t *cnt = &state->arcs_lsize[type]; 1611185029Spjd 1612168404Spjd ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 1613168404Spjd ASSERT(state != arc_anon); 1614185029Spjd 1615185029Spjd ASSERT3U(*cnt, >=, size); 1616185029Spjd atomic_add_64(cnt, -size); 1617168404Spjd } 1618168404Spjd ASSERT3U(state->arcs_size, >=, size); 1619168404Spjd atomic_add_64(&state->arcs_size, -size); 1620168404Spjd buf->b_data = NULL; 1621168404Spjd ASSERT(buf->b_hdr->b_datacnt > 0); 1622168404Spjd buf->b_hdr->b_datacnt -= 1; 1623168404Spjd } 1624168404Spjd 1625168404Spjd /* only remove the buf if requested */ 1626168404Spjd if (!all) 1627168404Spjd return; 1628168404Spjd 1629168404Spjd /* remove the buf from the hdr list */ 1630168404Spjd for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 1631168404Spjd continue; 1632168404Spjd *bufp = buf->b_next; 1633219089Spjd buf->b_next = NULL; 1634168404Spjd 1635168404Spjd ASSERT(buf->b_efunc == NULL); 1636168404Spjd 1637168404Spjd /* clean up the buf */ 1638168404Spjd buf->b_hdr = NULL; 1639168404Spjd kmem_cache_free(buf_cache, buf); 1640168404Spjd} 1641168404Spjd 1642168404Spjdstatic void 1643168404Spjdarc_hdr_destroy(arc_buf_hdr_t *hdr) 1644168404Spjd{ 1645168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1646168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 1647168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1648219089Spjd l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr; 1649168404Spjd 1650219089Spjd if (l2hdr != NULL) { 1651219089Spjd boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx); 1652219089Spjd /* 1653219089Spjd * To prevent arc_free() and l2arc_evict() from 1654219089Spjd * attempting to free the same buffer at the same time, 1655219089Spjd * a FREE_IN_PROGRESS flag is given to arc_free() to 1656219089Spjd * give it priority. l2arc_evict() can't destroy this 1657219089Spjd * header while we are waiting on l2arc_buflist_mtx. 1658219089Spjd * 1659219089Spjd * The hdr may be removed from l2ad_buflist before we 1660219089Spjd * grab l2arc_buflist_mtx, so b_l2hdr is rechecked. 1661219089Spjd */ 1662219089Spjd if (!buflist_held) { 1663185029Spjd mutex_enter(&l2arc_buflist_mtx); 1664219089Spjd l2hdr = hdr->b_l2hdr; 1665219089Spjd } 1666219089Spjd 1667219089Spjd if (l2hdr != NULL) { 1668219089Spjd list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 1669219089Spjd ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 1670219089Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 1671219089Spjd if (hdr->b_state == arc_l2c_only) 1672219089Spjd l2arc_hdr_stat_remove(); 1673219089Spjd hdr->b_l2hdr = NULL; 1674219089Spjd } 1675219089Spjd 1676219089Spjd if (!buflist_held) 1677185029Spjd mutex_exit(&l2arc_buflist_mtx); 1678185029Spjd } 1679185029Spjd 1680168404Spjd if (!BUF_EMPTY(hdr)) { 1681168404Spjd ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1682219089Spjd buf_discard_identity(hdr); 1683168404Spjd } 1684168404Spjd while (hdr->b_buf) { 1685168404Spjd arc_buf_t *buf = hdr->b_buf; 1686168404Spjd 1687168404Spjd if (buf->b_efunc) { 1688168404Spjd mutex_enter(&arc_eviction_mtx); 1689219089Spjd mutex_enter(&buf->b_evict_lock); 1690168404Spjd ASSERT(buf->b_hdr != NULL); 1691168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, FALSE); 1692168404Spjd hdr->b_buf = buf->b_next; 1693168404Spjd buf->b_hdr = &arc_eviction_hdr; 1694168404Spjd buf->b_next = arc_eviction_list; 1695168404Spjd arc_eviction_list = buf; 1696219089Spjd mutex_exit(&buf->b_evict_lock); 1697168404Spjd mutex_exit(&arc_eviction_mtx); 1698168404Spjd } else { 1699168404Spjd arc_buf_destroy(hdr->b_buf, FALSE, TRUE); 1700168404Spjd } 1701168404Spjd } 1702168404Spjd if (hdr->b_freeze_cksum != NULL) { 1703168404Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1704168404Spjd hdr->b_freeze_cksum = NULL; 1705168404Spjd } 1706219089Spjd if (hdr->b_thawed) { 1707219089Spjd kmem_free(hdr->b_thawed, 1); 1708219089Spjd hdr->b_thawed = NULL; 1709219089Spjd } 1710168404Spjd 1711168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 1712168404Spjd ASSERT3P(hdr->b_hash_next, ==, NULL); 1713168404Spjd ASSERT3P(hdr->b_acb, ==, NULL); 1714168404Spjd kmem_cache_free(hdr_cache, hdr); 1715168404Spjd} 1716168404Spjd 1717168404Spjdvoid 1718168404Spjdarc_buf_free(arc_buf_t *buf, void *tag) 1719168404Spjd{ 1720168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1721168404Spjd int hashed = hdr->b_state != arc_anon; 1722168404Spjd 1723168404Spjd ASSERT(buf->b_efunc == NULL); 1724168404Spjd ASSERT(buf->b_data != NULL); 1725168404Spjd 1726168404Spjd if (hashed) { 1727168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1728168404Spjd 1729168404Spjd mutex_enter(hash_lock); 1730219089Spjd hdr = buf->b_hdr; 1731219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1732219089Spjd 1733168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1734219089Spjd if (hdr->b_datacnt > 1) { 1735168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1736219089Spjd } else { 1737219089Spjd ASSERT(buf == hdr->b_buf); 1738219089Spjd ASSERT(buf->b_efunc == NULL); 1739168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 1740219089Spjd } 1741168404Spjd mutex_exit(hash_lock); 1742168404Spjd } else if (HDR_IO_IN_PROGRESS(hdr)) { 1743168404Spjd int destroy_hdr; 1744168404Spjd /* 1745168404Spjd * We are in the middle of an async write. Don't destroy 1746168404Spjd * this buffer unless the write completes before we finish 1747168404Spjd * decrementing the reference count. 1748168404Spjd */ 1749168404Spjd mutex_enter(&arc_eviction_mtx); 1750168404Spjd (void) remove_reference(hdr, NULL, tag); 1751168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1752168404Spjd destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 1753168404Spjd mutex_exit(&arc_eviction_mtx); 1754168404Spjd if (destroy_hdr) 1755168404Spjd arc_hdr_destroy(hdr); 1756168404Spjd } else { 1757219089Spjd if (remove_reference(hdr, NULL, tag) > 0) 1758168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1759219089Spjd else 1760168404Spjd arc_hdr_destroy(hdr); 1761168404Spjd } 1762168404Spjd} 1763168404Spjd 1764168404Spjdint 1765168404Spjdarc_buf_remove_ref(arc_buf_t *buf, void* tag) 1766168404Spjd{ 1767168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 1768168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 1769168404Spjd int no_callback = (buf->b_efunc == NULL); 1770168404Spjd 1771168404Spjd if (hdr->b_state == arc_anon) { 1772219089Spjd ASSERT(hdr->b_datacnt == 1); 1773168404Spjd arc_buf_free(buf, tag); 1774168404Spjd return (no_callback); 1775168404Spjd } 1776168404Spjd 1777168404Spjd mutex_enter(hash_lock); 1778219089Spjd hdr = buf->b_hdr; 1779219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 1780168404Spjd ASSERT(hdr->b_state != arc_anon); 1781168404Spjd ASSERT(buf->b_data != NULL); 1782168404Spjd 1783168404Spjd (void) remove_reference(hdr, hash_lock, tag); 1784168404Spjd if (hdr->b_datacnt > 1) { 1785168404Spjd if (no_callback) 1786168404Spjd arc_buf_destroy(buf, FALSE, TRUE); 1787168404Spjd } else if (no_callback) { 1788168404Spjd ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 1789219089Spjd ASSERT(buf->b_efunc == NULL); 1790168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 1791168404Spjd } 1792168404Spjd ASSERT(no_callback || hdr->b_datacnt > 1 || 1793168404Spjd refcount_is_zero(&hdr->b_refcnt)); 1794168404Spjd mutex_exit(hash_lock); 1795168404Spjd return (no_callback); 1796168404Spjd} 1797168404Spjd 1798168404Spjdint 1799168404Spjdarc_buf_size(arc_buf_t *buf) 1800168404Spjd{ 1801168404Spjd return (buf->b_hdr->b_size); 1802168404Spjd} 1803168404Spjd 1804168404Spjd/* 1805168404Spjd * Evict buffers from list until we've removed the specified number of 1806168404Spjd * bytes. Move the removed buffers to the appropriate evict state. 1807168404Spjd * If the recycle flag is set, then attempt to "recycle" a buffer: 1808168404Spjd * - look for a buffer to evict that is `bytes' long. 1809168404Spjd * - return the data block from this buffer rather than freeing it. 1810168404Spjd * This flag is used by callers that are trying to make space for a 1811168404Spjd * new buffer in a full arc cache. 1812185029Spjd * 1813185029Spjd * This function makes a "best effort". It skips over any buffers 1814185029Spjd * it can't get a hash_lock on, and so may not catch all candidates. 1815185029Spjd * It may also return without evicting as much space as requested. 1816168404Spjd */ 1817168404Spjdstatic void * 1818209962Smmarc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle, 1819168404Spjd arc_buf_contents_t type) 1820168404Spjd{ 1821168404Spjd arc_state_t *evicted_state; 1822168404Spjd uint64_t bytes_evicted = 0, skipped = 0, missed = 0; 1823205231Skmacy int64_t bytes_remaining; 1824168404Spjd arc_buf_hdr_t *ab, *ab_prev = NULL; 1825205231Skmacy list_t *evicted_list, *list, *evicted_list_start, *list_start; 1826205231Skmacy kmutex_t *lock, *evicted_lock; 1827168404Spjd kmutex_t *hash_lock; 1828168404Spjd boolean_t have_lock; 1829168404Spjd void *stolen = NULL; 1830205231Skmacy static int evict_metadata_offset, evict_data_offset; 1831205231Skmacy int i, idx, offset, list_count, count; 1832168404Spjd 1833168404Spjd ASSERT(state == arc_mru || state == arc_mfu); 1834168404Spjd 1835168404Spjd evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 1836206796Spjd 1837205231Skmacy if (type == ARC_BUFC_METADATA) { 1838205231Skmacy offset = 0; 1839205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 1840205231Skmacy list_start = &state->arcs_lists[0]; 1841205231Skmacy evicted_list_start = &evicted_state->arcs_lists[0]; 1842205231Skmacy idx = evict_metadata_offset; 1843205231Skmacy } else { 1844205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 1845205231Skmacy list_start = &state->arcs_lists[offset]; 1846205231Skmacy evicted_list_start = &evicted_state->arcs_lists[offset]; 1847205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 1848205231Skmacy idx = evict_data_offset; 1849205231Skmacy } 1850205231Skmacy bytes_remaining = evicted_state->arcs_lsize[type]; 1851205231Skmacy count = 0; 1852206796Spjd 1853205231Skmacyevict_start: 1854205231Skmacy list = &list_start[idx]; 1855205231Skmacy evicted_list = &evicted_list_start[idx]; 1856205231Skmacy lock = ARCS_LOCK(state, (offset + idx)); 1857206796Spjd evicted_lock = ARCS_LOCK(evicted_state, (offset + idx)); 1858168404Spjd 1859205231Skmacy mutex_enter(lock); 1860205231Skmacy mutex_enter(evicted_lock); 1861205231Skmacy 1862185029Spjd for (ab = list_tail(list); ab; ab = ab_prev) { 1863185029Spjd ab_prev = list_prev(list, ab); 1864205231Skmacy bytes_remaining -= (ab->b_size * ab->b_datacnt); 1865168404Spjd /* prefetch buffers have a minimum lifespan */ 1866168404Spjd if (HDR_IO_IN_PROGRESS(ab) || 1867185029Spjd (spa && ab->b_spa != spa) || 1868168404Spjd (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) && 1869219089Spjd ddi_get_lbolt() - ab->b_arc_access < 1870219089Spjd arc_min_prefetch_lifespan)) { 1871168404Spjd skipped++; 1872168404Spjd continue; 1873168404Spjd } 1874168404Spjd /* "lookahead" for better eviction candidate */ 1875168404Spjd if (recycle && ab->b_size != bytes && 1876168404Spjd ab_prev && ab_prev->b_size == bytes) 1877168404Spjd continue; 1878168404Spjd hash_lock = HDR_LOCK(ab); 1879168404Spjd have_lock = MUTEX_HELD(hash_lock); 1880168404Spjd if (have_lock || mutex_tryenter(hash_lock)) { 1881168404Spjd ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 1882168404Spjd ASSERT(ab->b_datacnt > 0); 1883168404Spjd while (ab->b_buf) { 1884168404Spjd arc_buf_t *buf = ab->b_buf; 1885219089Spjd if (!mutex_tryenter(&buf->b_evict_lock)) { 1886185029Spjd missed += 1; 1887185029Spjd break; 1888185029Spjd } 1889168404Spjd if (buf->b_data) { 1890168404Spjd bytes_evicted += ab->b_size; 1891168404Spjd if (recycle && ab->b_type == type && 1892185029Spjd ab->b_size == bytes && 1893185029Spjd !HDR_L2_WRITING(ab)) { 1894168404Spjd stolen = buf->b_data; 1895168404Spjd recycle = FALSE; 1896168404Spjd } 1897168404Spjd } 1898168404Spjd if (buf->b_efunc) { 1899168404Spjd mutex_enter(&arc_eviction_mtx); 1900168404Spjd arc_buf_destroy(buf, 1901168404Spjd buf->b_data == stolen, FALSE); 1902168404Spjd ab->b_buf = buf->b_next; 1903168404Spjd buf->b_hdr = &arc_eviction_hdr; 1904168404Spjd buf->b_next = arc_eviction_list; 1905168404Spjd arc_eviction_list = buf; 1906168404Spjd mutex_exit(&arc_eviction_mtx); 1907219089Spjd mutex_exit(&buf->b_evict_lock); 1908168404Spjd } else { 1909219089Spjd mutex_exit(&buf->b_evict_lock); 1910168404Spjd arc_buf_destroy(buf, 1911168404Spjd buf->b_data == stolen, TRUE); 1912168404Spjd } 1913168404Spjd } 1914208373Smm 1915208373Smm if (ab->b_l2hdr) { 1916208373Smm ARCSTAT_INCR(arcstat_evict_l2_cached, 1917208373Smm ab->b_size); 1918208373Smm } else { 1919208373Smm if (l2arc_write_eligible(ab->b_spa, ab)) { 1920208373Smm ARCSTAT_INCR(arcstat_evict_l2_eligible, 1921208373Smm ab->b_size); 1922208373Smm } else { 1923208373Smm ARCSTAT_INCR( 1924208373Smm arcstat_evict_l2_ineligible, 1925208373Smm ab->b_size); 1926208373Smm } 1927208373Smm } 1928208373Smm 1929185029Spjd if (ab->b_datacnt == 0) { 1930185029Spjd arc_change_state(evicted_state, ab, hash_lock); 1931185029Spjd ASSERT(HDR_IN_HASH_TABLE(ab)); 1932185029Spjd ab->b_flags |= ARC_IN_HASH_TABLE; 1933185029Spjd ab->b_flags &= ~ARC_BUF_AVAILABLE; 1934185029Spjd DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 1935185029Spjd } 1936168404Spjd if (!have_lock) 1937168404Spjd mutex_exit(hash_lock); 1938168404Spjd if (bytes >= 0 && bytes_evicted >= bytes) 1939168404Spjd break; 1940205231Skmacy if (bytes_remaining > 0) { 1941205231Skmacy mutex_exit(evicted_lock); 1942205231Skmacy mutex_exit(lock); 1943206796Spjd idx = ((idx + 1) & (list_count - 1)); 1944205231Skmacy count++; 1945205231Skmacy goto evict_start; 1946205231Skmacy } 1947168404Spjd } else { 1948168404Spjd missed += 1; 1949168404Spjd } 1950168404Spjd } 1951168404Spjd 1952205231Skmacy mutex_exit(evicted_lock); 1953205231Skmacy mutex_exit(lock); 1954206796Spjd 1955206796Spjd idx = ((idx + 1) & (list_count - 1)); 1956205231Skmacy count++; 1957168404Spjd 1958205231Skmacy if (bytes_evicted < bytes) { 1959205231Skmacy if (count < list_count) 1960205231Skmacy goto evict_start; 1961205231Skmacy else 1962205231Skmacy dprintf("only evicted %lld bytes from %x", 1963205231Skmacy (longlong_t)bytes_evicted, state); 1964205231Skmacy } 1965206796Spjd if (type == ARC_BUFC_METADATA) 1966205231Skmacy evict_metadata_offset = idx; 1967205231Skmacy else 1968205231Skmacy evict_data_offset = idx; 1969206796Spjd 1970168404Spjd if (skipped) 1971168404Spjd ARCSTAT_INCR(arcstat_evict_skip, skipped); 1972168404Spjd 1973168404Spjd if (missed) 1974168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, missed); 1975168404Spjd 1976185029Spjd /* 1977185029Spjd * We have just evicted some date into the ghost state, make 1978185029Spjd * sure we also adjust the ghost state size if necessary. 1979185029Spjd */ 1980185029Spjd if (arc_no_grow && 1981185029Spjd arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) { 1982185029Spjd int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size + 1983185029Spjd arc_mru_ghost->arcs_size - arc_c; 1984185029Spjd 1985185029Spjd if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { 1986185029Spjd int64_t todelete = 1987185029Spjd MIN(arc_mru_ghost->arcs_lsize[type], mru_over); 1988209962Smm arc_evict_ghost(arc_mru_ghost, 0, todelete); 1989185029Spjd } else if (arc_mfu_ghost->arcs_lsize[type] > 0) { 1990185029Spjd int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type], 1991185029Spjd arc_mru_ghost->arcs_size + 1992185029Spjd arc_mfu_ghost->arcs_size - arc_c); 1993209962Smm arc_evict_ghost(arc_mfu_ghost, 0, todelete); 1994185029Spjd } 1995185029Spjd } 1996205231Skmacy if (stolen) 1997205231Skmacy ARCSTAT_BUMP(arcstat_stolen); 1998185029Spjd 1999168404Spjd return (stolen); 2000168404Spjd} 2001168404Spjd 2002168404Spjd/* 2003168404Spjd * Remove buffers from list until we've removed the specified number of 2004168404Spjd * bytes. Destroy the buffers that are removed. 2005168404Spjd */ 2006168404Spjdstatic void 2007209962Smmarc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes) 2008168404Spjd{ 2009168404Spjd arc_buf_hdr_t *ab, *ab_prev; 2010219089Spjd arc_buf_hdr_t marker = { 0 }; 2011205231Skmacy list_t *list, *list_start; 2012205231Skmacy kmutex_t *hash_lock, *lock; 2013168404Spjd uint64_t bytes_deleted = 0; 2014168404Spjd uint64_t bufs_skipped = 0; 2015205231Skmacy static int evict_offset; 2016205231Skmacy int list_count, idx = evict_offset; 2017205231Skmacy int offset, count = 0; 2018168404Spjd 2019168404Spjd ASSERT(GHOST_STATE(state)); 2020205231Skmacy 2021205231Skmacy /* 2022205231Skmacy * data lists come after metadata lists 2023205231Skmacy */ 2024205231Skmacy list_start = &state->arcs_lists[ARC_BUFC_NUMMETADATALISTS]; 2025205231Skmacy list_count = ARC_BUFC_NUMDATALISTS; 2026205231Skmacy offset = ARC_BUFC_NUMMETADATALISTS; 2027206796Spjd 2028205231Skmacyevict_start: 2029205231Skmacy list = &list_start[idx]; 2030205231Skmacy lock = ARCS_LOCK(state, idx + offset); 2031205231Skmacy 2032205231Skmacy mutex_enter(lock); 2033185029Spjd for (ab = list_tail(list); ab; ab = ab_prev) { 2034185029Spjd ab_prev = list_prev(list, ab); 2035185029Spjd if (spa && ab->b_spa != spa) 2036185029Spjd continue; 2037219089Spjd 2038219089Spjd /* ignore markers */ 2039219089Spjd if (ab->b_spa == 0) 2040219089Spjd continue; 2041219089Spjd 2042168404Spjd hash_lock = HDR_LOCK(ab); 2043219089Spjd /* caller may be trying to modify this buffer, skip it */ 2044219089Spjd if (MUTEX_HELD(hash_lock)) 2045219089Spjd continue; 2046168404Spjd if (mutex_tryenter(hash_lock)) { 2047168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(ab)); 2048168404Spjd ASSERT(ab->b_buf == NULL); 2049168404Spjd ARCSTAT_BUMP(arcstat_deleted); 2050168404Spjd bytes_deleted += ab->b_size; 2051185029Spjd 2052185029Spjd if (ab->b_l2hdr != NULL) { 2053185029Spjd /* 2054185029Spjd * This buffer is cached on the 2nd Level ARC; 2055185029Spjd * don't destroy the header. 2056185029Spjd */ 2057185029Spjd arc_change_state(arc_l2c_only, ab, hash_lock); 2058185029Spjd mutex_exit(hash_lock); 2059185029Spjd } else { 2060185029Spjd arc_change_state(arc_anon, ab, hash_lock); 2061185029Spjd mutex_exit(hash_lock); 2062185029Spjd arc_hdr_destroy(ab); 2063185029Spjd } 2064185029Spjd 2065168404Spjd DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 2066168404Spjd if (bytes >= 0 && bytes_deleted >= bytes) 2067168404Spjd break; 2068219089Spjd } else if (bytes < 0) { 2069219089Spjd /* 2070219089Spjd * Insert a list marker and then wait for the 2071219089Spjd * hash lock to become available. Once its 2072219089Spjd * available, restart from where we left off. 2073219089Spjd */ 2074219089Spjd list_insert_after(list, ab, &marker); 2075219089Spjd mutex_exit(lock); 2076219089Spjd mutex_enter(hash_lock); 2077219089Spjd mutex_exit(hash_lock); 2078219089Spjd mutex_enter(lock); 2079219089Spjd ab_prev = list_prev(list, &marker); 2080219089Spjd list_remove(list, &marker); 2081219089Spjd } else 2082168404Spjd bufs_skipped += 1; 2083168404Spjd } 2084205231Skmacy mutex_exit(lock); 2085206796Spjd idx = ((idx + 1) & (ARC_BUFC_NUMDATALISTS - 1)); 2086205231Skmacy count++; 2087206796Spjd 2088205231Skmacy if (count < list_count) 2089205231Skmacy goto evict_start; 2090206796Spjd 2091205231Skmacy evict_offset = idx; 2092205231Skmacy if ((uintptr_t)list > (uintptr_t)&state->arcs_lists[ARC_BUFC_NUMMETADATALISTS] && 2093185029Spjd (bytes < 0 || bytes_deleted < bytes)) { 2094205231Skmacy list_start = &state->arcs_lists[0]; 2095205231Skmacy list_count = ARC_BUFC_NUMMETADATALISTS; 2096205231Skmacy offset = count = 0; 2097205231Skmacy goto evict_start; 2098185029Spjd } 2099185029Spjd 2100168404Spjd if (bufs_skipped) { 2101168404Spjd ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped); 2102168404Spjd ASSERT(bytes >= 0); 2103168404Spjd } 2104168404Spjd 2105168404Spjd if (bytes_deleted < bytes) 2106168404Spjd dprintf("only deleted %lld bytes from %p", 2107168404Spjd (longlong_t)bytes_deleted, state); 2108168404Spjd} 2109168404Spjd 2110168404Spjdstatic void 2111168404Spjdarc_adjust(void) 2112168404Spjd{ 2113208373Smm int64_t adjustment, delta; 2114168404Spjd 2115208373Smm /* 2116208373Smm * Adjust MRU size 2117208373Smm */ 2118168404Spjd 2119209275Smm adjustment = MIN((int64_t)(arc_size - arc_c), 2120209275Smm (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - 2121209275Smm arc_p)); 2122208373Smm 2123208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) { 2124208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment); 2125209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA); 2126208373Smm adjustment -= delta; 2127168404Spjd } 2128168404Spjd 2129208373Smm if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2130208373Smm delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment); 2131209962Smm (void) arc_evict(arc_mru, 0, delta, FALSE, 2132185029Spjd ARC_BUFC_METADATA); 2133185029Spjd } 2134185029Spjd 2135208373Smm /* 2136208373Smm * Adjust MFU size 2137208373Smm */ 2138168404Spjd 2139208373Smm adjustment = arc_size - arc_c; 2140208373Smm 2141208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) { 2142208373Smm delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]); 2143209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA); 2144208373Smm adjustment -= delta; 2145168404Spjd } 2146168404Spjd 2147208373Smm if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) { 2148208373Smm int64_t delta = MIN(adjustment, 2149208373Smm arc_mfu->arcs_lsize[ARC_BUFC_METADATA]); 2150209962Smm (void) arc_evict(arc_mfu, 0, delta, FALSE, 2151208373Smm ARC_BUFC_METADATA); 2152208373Smm } 2153168404Spjd 2154208373Smm /* 2155208373Smm * Adjust ghost lists 2156208373Smm */ 2157168404Spjd 2158208373Smm adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; 2159168404Spjd 2160208373Smm if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) { 2161208373Smm delta = MIN(arc_mru_ghost->arcs_size, adjustment); 2162209962Smm arc_evict_ghost(arc_mru_ghost, 0, delta); 2163208373Smm } 2164185029Spjd 2165208373Smm adjustment = 2166208373Smm arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; 2167208373Smm 2168208373Smm if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) { 2169208373Smm delta = MIN(arc_mfu_ghost->arcs_size, adjustment); 2170209962Smm arc_evict_ghost(arc_mfu_ghost, 0, delta); 2171168404Spjd } 2172168404Spjd} 2173168404Spjd 2174168404Spjdstatic void 2175168404Spjdarc_do_user_evicts(void) 2176168404Spjd{ 2177191903Skmacy static arc_buf_t *tmp_arc_eviction_list; 2178191903Skmacy 2179191903Skmacy /* 2180191903Skmacy * Move list over to avoid LOR 2181191903Skmacy */ 2182206796Spjdrestart: 2183168404Spjd mutex_enter(&arc_eviction_mtx); 2184191903Skmacy tmp_arc_eviction_list = arc_eviction_list; 2185191903Skmacy arc_eviction_list = NULL; 2186191903Skmacy mutex_exit(&arc_eviction_mtx); 2187191903Skmacy 2188191903Skmacy while (tmp_arc_eviction_list != NULL) { 2189191903Skmacy arc_buf_t *buf = tmp_arc_eviction_list; 2190191903Skmacy tmp_arc_eviction_list = buf->b_next; 2191219089Spjd mutex_enter(&buf->b_evict_lock); 2192168404Spjd buf->b_hdr = NULL; 2193219089Spjd mutex_exit(&buf->b_evict_lock); 2194168404Spjd 2195168404Spjd if (buf->b_efunc != NULL) 2196168404Spjd VERIFY(buf->b_efunc(buf) == 0); 2197168404Spjd 2198168404Spjd buf->b_efunc = NULL; 2199168404Spjd buf->b_private = NULL; 2200168404Spjd kmem_cache_free(buf_cache, buf); 2201168404Spjd } 2202191903Skmacy 2203191903Skmacy if (arc_eviction_list != NULL) 2204191903Skmacy goto restart; 2205168404Spjd} 2206168404Spjd 2207168404Spjd/* 2208185029Spjd * Flush all *evictable* data from the cache for the given spa. 2209168404Spjd * NOTE: this will not touch "active" (i.e. referenced) data. 2210168404Spjd */ 2211168404Spjdvoid 2212185029Spjdarc_flush(spa_t *spa) 2213168404Spjd{ 2214209962Smm uint64_t guid = 0; 2215209962Smm 2216209962Smm if (spa) 2217228103Smm guid = spa_load_guid(spa); 2218209962Smm 2219205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_DATA]) { 2220209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA); 2221185029Spjd if (spa) 2222185029Spjd break; 2223185029Spjd } 2224205231Skmacy while (arc_mru->arcs_lsize[ARC_BUFC_METADATA]) { 2225209962Smm (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA); 2226185029Spjd if (spa) 2227185029Spjd break; 2228185029Spjd } 2229205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_DATA]) { 2230209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA); 2231185029Spjd if (spa) 2232185029Spjd break; 2233185029Spjd } 2234205231Skmacy while (arc_mfu->arcs_lsize[ARC_BUFC_METADATA]) { 2235209962Smm (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA); 2236185029Spjd if (spa) 2237185029Spjd break; 2238185029Spjd } 2239168404Spjd 2240209962Smm arc_evict_ghost(arc_mru_ghost, guid, -1); 2241209962Smm arc_evict_ghost(arc_mfu_ghost, guid, -1); 2242168404Spjd 2243168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2244168404Spjd arc_do_user_evicts(); 2245168404Spjd mutex_exit(&arc_reclaim_thr_lock); 2246185029Spjd ASSERT(spa || arc_eviction_list == NULL); 2247168404Spjd} 2248168404Spjd 2249168404Spjdvoid 2250168404Spjdarc_shrink(void) 2251168404Spjd{ 2252168404Spjd if (arc_c > arc_c_min) { 2253168404Spjd uint64_t to_free; 2254168404Spjd 2255168404Spjd#ifdef _KERNEL 2256168404Spjd to_free = arc_c >> arc_shrink_shift; 2257168404Spjd#else 2258168404Spjd to_free = arc_c >> arc_shrink_shift; 2259168404Spjd#endif 2260168404Spjd if (arc_c > arc_c_min + to_free) 2261168404Spjd atomic_add_64(&arc_c, -to_free); 2262168404Spjd else 2263168404Spjd arc_c = arc_c_min; 2264168404Spjd 2265168404Spjd atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 2266168404Spjd if (arc_c > arc_size) 2267168404Spjd arc_c = MAX(arc_size, arc_c_min); 2268168404Spjd if (arc_p > arc_c) 2269168404Spjd arc_p = (arc_c >> 1); 2270168404Spjd ASSERT(arc_c >= arc_c_min); 2271168404Spjd ASSERT((int64_t)arc_p >= 0); 2272168404Spjd } 2273168404Spjd 2274168404Spjd if (arc_size > arc_c) 2275168404Spjd arc_adjust(); 2276168404Spjd} 2277168404Spjd 2278185029Spjdstatic int needfree = 0; 2279168404Spjd 2280168404Spjdstatic int 2281168404Spjdarc_reclaim_needed(void) 2282168404Spjd{ 2283168404Spjd 2284168404Spjd#ifdef _KERNEL 2285219089Spjd 2286197816Skmacy if (needfree) 2287197816Skmacy return (1); 2288168404Spjd 2289191902Skmacy /* 2290212780Savg * Cooperate with pagedaemon when it's time for it to scan 2291212780Savg * and reclaim some pages. 2292191902Skmacy */ 2293212783Savg if (vm_paging_needed()) 2294191902Skmacy return (1); 2295191902Skmacy 2296219089Spjd#ifdef sun 2297168404Spjd /* 2298185029Spjd * take 'desfree' extra pages, so we reclaim sooner, rather than later 2299185029Spjd */ 2300185029Spjd extra = desfree; 2301185029Spjd 2302185029Spjd /* 2303185029Spjd * check that we're out of range of the pageout scanner. It starts to 2304185029Spjd * schedule paging if freemem is less than lotsfree and needfree. 2305185029Spjd * lotsfree is the high-water mark for pageout, and needfree is the 2306185029Spjd * number of needed free pages. We add extra pages here to make sure 2307185029Spjd * the scanner doesn't start up while we're freeing memory. 2308185029Spjd */ 2309185029Spjd if (freemem < lotsfree + needfree + extra) 2310185029Spjd return (1); 2311185029Spjd 2312185029Spjd /* 2313168404Spjd * check to make sure that swapfs has enough space so that anon 2314185029Spjd * reservations can still succeed. anon_resvmem() checks that the 2315168404Spjd * availrmem is greater than swapfs_minfree, and the number of reserved 2316168404Spjd * swap pages. We also add a bit of extra here just to prevent 2317168404Spjd * circumstances from getting really dire. 2318168404Spjd */ 2319168404Spjd if (availrmem < swapfs_minfree + swapfs_reserve + extra) 2320168404Spjd return (1); 2321168404Spjd 2322168404Spjd#if defined(__i386) 2323168404Spjd /* 2324168404Spjd * If we're on an i386 platform, it's possible that we'll exhaust the 2325168404Spjd * kernel heap space before we ever run out of available physical 2326168404Spjd * memory. Most checks of the size of the heap_area compare against 2327168404Spjd * tune.t_minarmem, which is the minimum available real memory that we 2328168404Spjd * can have in the system. However, this is generally fixed at 25 pages 2329168404Spjd * which is so low that it's useless. In this comparison, we seek to 2330168404Spjd * calculate the total heap-size, and reclaim if more than 3/4ths of the 2331185029Spjd * heap is allocated. (Or, in the calculation, if less than 1/4th is 2332168404Spjd * free) 2333168404Spjd */ 2334168404Spjd if (btop(vmem_size(heap_arena, VMEM_FREE)) < 2335168404Spjd (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 2336168404Spjd return (1); 2337168404Spjd#endif 2338219089Spjd#else /* !sun */ 2339175633Spjd if (kmem_used() > (kmem_size() * 3) / 4) 2340168404Spjd return (1); 2341219089Spjd#endif /* sun */ 2342168404Spjd 2343168404Spjd#else 2344168404Spjd if (spa_get_random(100) == 0) 2345168404Spjd return (1); 2346168404Spjd#endif 2347168404Spjd return (0); 2348168404Spjd} 2349168404Spjd 2350208454Spjdextern kmem_cache_t *zio_buf_cache[]; 2351208454Spjdextern kmem_cache_t *zio_data_buf_cache[]; 2352208454Spjd 2353168404Spjdstatic void 2354168404Spjdarc_kmem_reap_now(arc_reclaim_strategy_t strat) 2355168404Spjd{ 2356168404Spjd size_t i; 2357168404Spjd kmem_cache_t *prev_cache = NULL; 2358168404Spjd kmem_cache_t *prev_data_cache = NULL; 2359168404Spjd 2360168404Spjd#ifdef _KERNEL 2361185029Spjd if (arc_meta_used >= arc_meta_limit) { 2362185029Spjd /* 2363185029Spjd * We are exceeding our meta-data cache limit. 2364185029Spjd * Purge some DNLC entries to release holds on meta-data. 2365185029Spjd */ 2366185029Spjd dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 2367185029Spjd } 2368168404Spjd#if defined(__i386) 2369168404Spjd /* 2370168404Spjd * Reclaim unused memory from all kmem caches. 2371168404Spjd */ 2372168404Spjd kmem_reap(); 2373168404Spjd#endif 2374168404Spjd#endif 2375168404Spjd 2376168404Spjd /* 2377185029Spjd * An aggressive reclamation will shrink the cache size as well as 2378168404Spjd * reap free buffers from the arc kmem caches. 2379168404Spjd */ 2380168404Spjd if (strat == ARC_RECLAIM_AGGR) 2381168404Spjd arc_shrink(); 2382168404Spjd 2383168404Spjd for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 2384168404Spjd if (zio_buf_cache[i] != prev_cache) { 2385168404Spjd prev_cache = zio_buf_cache[i]; 2386168404Spjd kmem_cache_reap_now(zio_buf_cache[i]); 2387168404Spjd } 2388168404Spjd if (zio_data_buf_cache[i] != prev_data_cache) { 2389168404Spjd prev_data_cache = zio_data_buf_cache[i]; 2390168404Spjd kmem_cache_reap_now(zio_data_buf_cache[i]); 2391168404Spjd } 2392168404Spjd } 2393168404Spjd kmem_cache_reap_now(buf_cache); 2394168404Spjd kmem_cache_reap_now(hdr_cache); 2395168404Spjd} 2396168404Spjd 2397168404Spjdstatic void 2398168404Spjdarc_reclaim_thread(void *dummy __unused) 2399168404Spjd{ 2400168404Spjd clock_t growtime = 0; 2401168404Spjd arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 2402168404Spjd callb_cpr_t cpr; 2403168404Spjd 2404168404Spjd CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 2405168404Spjd 2406168404Spjd mutex_enter(&arc_reclaim_thr_lock); 2407168404Spjd while (arc_thread_exit == 0) { 2408168404Spjd if (arc_reclaim_needed()) { 2409168404Spjd 2410168404Spjd if (arc_no_grow) { 2411168404Spjd if (last_reclaim == ARC_RECLAIM_CONS) { 2412168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2413168404Spjd } else { 2414168404Spjd last_reclaim = ARC_RECLAIM_CONS; 2415168404Spjd } 2416168404Spjd } else { 2417168404Spjd arc_no_grow = TRUE; 2418168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2419168404Spjd membar_producer(); 2420168404Spjd } 2421168404Spjd 2422168404Spjd /* reset the growth delay for every reclaim */ 2423219089Spjd growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 2424168404Spjd 2425185029Spjd if (needfree && last_reclaim == ARC_RECLAIM_CONS) { 2426168404Spjd /* 2427185029Spjd * If needfree is TRUE our vm_lowmem hook 2428168404Spjd * was called and in that case we must free some 2429168404Spjd * memory, so switch to aggressive mode. 2430168404Spjd */ 2431168404Spjd arc_no_grow = TRUE; 2432168404Spjd last_reclaim = ARC_RECLAIM_AGGR; 2433168404Spjd } 2434168404Spjd arc_kmem_reap_now(last_reclaim); 2435185029Spjd arc_warm = B_TRUE; 2436185029Spjd 2437219089Spjd } else if (arc_no_grow && ddi_get_lbolt() >= growtime) { 2438168404Spjd arc_no_grow = FALSE; 2439168404Spjd } 2440168404Spjd 2441209275Smm arc_adjust(); 2442168404Spjd 2443168404Spjd if (arc_eviction_list != NULL) 2444168404Spjd arc_do_user_evicts(); 2445168404Spjd 2446211762Savg#ifdef _KERNEL 2447211762Savg if (needfree) { 2448185029Spjd needfree = 0; 2449185029Spjd wakeup(&needfree); 2450211762Savg } 2451168404Spjd#endif 2452168404Spjd 2453168404Spjd /* block until needed, or one second, whichever is shorter */ 2454168404Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 2455168404Spjd (void) cv_timedwait(&arc_reclaim_thr_cv, 2456168404Spjd &arc_reclaim_thr_lock, hz); 2457168404Spjd CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 2458168404Spjd } 2459168404Spjd 2460168404Spjd arc_thread_exit = 0; 2461168404Spjd cv_broadcast(&arc_reclaim_thr_cv); 2462168404Spjd CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 2463168404Spjd thread_exit(); 2464168404Spjd} 2465168404Spjd 2466168404Spjd/* 2467168404Spjd * Adapt arc info given the number of bytes we are trying to add and 2468168404Spjd * the state that we are comming from. This function is only called 2469168404Spjd * when we are adding new content to the cache. 2470168404Spjd */ 2471168404Spjdstatic void 2472168404Spjdarc_adapt(int bytes, arc_state_t *state) 2473168404Spjd{ 2474168404Spjd int mult; 2475208373Smm uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 2476168404Spjd 2477185029Spjd if (state == arc_l2c_only) 2478185029Spjd return; 2479185029Spjd 2480168404Spjd ASSERT(bytes > 0); 2481168404Spjd /* 2482168404Spjd * Adapt the target size of the MRU list: 2483168404Spjd * - if we just hit in the MRU ghost list, then increase 2484168404Spjd * the target size of the MRU list. 2485168404Spjd * - if we just hit in the MFU ghost list, then increase 2486168404Spjd * the target size of the MFU list by decreasing the 2487168404Spjd * target size of the MRU list. 2488168404Spjd */ 2489168404Spjd if (state == arc_mru_ghost) { 2490168404Spjd mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? 2491168404Spjd 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); 2492209275Smm mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 2493168404Spjd 2494208373Smm arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 2495168404Spjd } else if (state == arc_mfu_ghost) { 2496208373Smm uint64_t delta; 2497208373Smm 2498168404Spjd mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? 2499168404Spjd 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); 2500209275Smm mult = MIN(mult, 10); 2501168404Spjd 2502208373Smm delta = MIN(bytes * mult, arc_p); 2503208373Smm arc_p = MAX(arc_p_min, arc_p - delta); 2504168404Spjd } 2505168404Spjd ASSERT((int64_t)arc_p >= 0); 2506168404Spjd 2507168404Spjd if (arc_reclaim_needed()) { 2508168404Spjd cv_signal(&arc_reclaim_thr_cv); 2509168404Spjd return; 2510168404Spjd } 2511168404Spjd 2512168404Spjd if (arc_no_grow) 2513168404Spjd return; 2514168404Spjd 2515168404Spjd if (arc_c >= arc_c_max) 2516168404Spjd return; 2517168404Spjd 2518168404Spjd /* 2519168404Spjd * If we're within (2 * maxblocksize) bytes of the target 2520168404Spjd * cache size, increment the target cache size 2521168404Spjd */ 2522168404Spjd if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 2523168404Spjd atomic_add_64(&arc_c, (int64_t)bytes); 2524168404Spjd if (arc_c > arc_c_max) 2525168404Spjd arc_c = arc_c_max; 2526168404Spjd else if (state == arc_anon) 2527168404Spjd atomic_add_64(&arc_p, (int64_t)bytes); 2528168404Spjd if (arc_p > arc_c) 2529168404Spjd arc_p = arc_c; 2530168404Spjd } 2531168404Spjd ASSERT((int64_t)arc_p >= 0); 2532168404Spjd} 2533168404Spjd 2534168404Spjd/* 2535168404Spjd * Check if the cache has reached its limits and eviction is required 2536168404Spjd * prior to insert. 2537168404Spjd */ 2538168404Spjdstatic int 2539185029Spjdarc_evict_needed(arc_buf_contents_t type) 2540168404Spjd{ 2541185029Spjd if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit) 2542185029Spjd return (1); 2543185029Spjd 2544219089Spjd#ifdef sun 2545185029Spjd#ifdef _KERNEL 2546185029Spjd /* 2547185029Spjd * If zio data pages are being allocated out of a separate heap segment, 2548185029Spjd * then enforce that the size of available vmem for this area remains 2549185029Spjd * above about 1/32nd free. 2550185029Spjd */ 2551185029Spjd if (type == ARC_BUFC_DATA && zio_arena != NULL && 2552185029Spjd vmem_size(zio_arena, VMEM_FREE) < 2553185029Spjd (vmem_size(zio_arena, VMEM_ALLOC) >> 5)) 2554185029Spjd return (1); 2555185029Spjd#endif 2556219089Spjd#endif /* sun */ 2557185029Spjd 2558168404Spjd if (arc_reclaim_needed()) 2559168404Spjd return (1); 2560168404Spjd 2561168404Spjd return (arc_size > arc_c); 2562168404Spjd} 2563168404Spjd 2564168404Spjd/* 2565168404Spjd * The buffer, supplied as the first argument, needs a data block. 2566168404Spjd * So, if we are at cache max, determine which cache should be victimized. 2567168404Spjd * We have the following cases: 2568168404Spjd * 2569168404Spjd * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) -> 2570168404Spjd * In this situation if we're out of space, but the resident size of the MFU is 2571168404Spjd * under the limit, victimize the MFU cache to satisfy this insertion request. 2572168404Spjd * 2573168404Spjd * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) -> 2574168404Spjd * Here, we've used up all of the available space for the MRU, so we need to 2575168404Spjd * evict from our own cache instead. Evict from the set of resident MRU 2576168404Spjd * entries. 2577168404Spjd * 2578168404Spjd * 3. Insert for MFU (c - p) > sizeof(arc_mfu) -> 2579168404Spjd * c minus p represents the MFU space in the cache, since p is the size of the 2580168404Spjd * cache that is dedicated to the MRU. In this situation there's still space on 2581168404Spjd * the MFU side, so the MRU side needs to be victimized. 2582168404Spjd * 2583168404Spjd * 4. Insert for MFU (c - p) < sizeof(arc_mfu) -> 2584168404Spjd * MFU's resident set is consuming more space than it has been allotted. In 2585168404Spjd * this situation, we must victimize our own cache, the MFU, for this insertion. 2586168404Spjd */ 2587168404Spjdstatic void 2588168404Spjdarc_get_data_buf(arc_buf_t *buf) 2589168404Spjd{ 2590168404Spjd arc_state_t *state = buf->b_hdr->b_state; 2591168404Spjd uint64_t size = buf->b_hdr->b_size; 2592168404Spjd arc_buf_contents_t type = buf->b_hdr->b_type; 2593168404Spjd 2594168404Spjd arc_adapt(size, state); 2595168404Spjd 2596168404Spjd /* 2597168404Spjd * We have not yet reached cache maximum size, 2598168404Spjd * just allocate a new buffer. 2599168404Spjd */ 2600185029Spjd if (!arc_evict_needed(type)) { 2601168404Spjd if (type == ARC_BUFC_METADATA) { 2602168404Spjd buf->b_data = zio_buf_alloc(size); 2603208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2604168404Spjd } else { 2605168404Spjd ASSERT(type == ARC_BUFC_DATA); 2606168404Spjd buf->b_data = zio_data_buf_alloc(size); 2607208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2608185029Spjd atomic_add_64(&arc_size, size); 2609168404Spjd } 2610168404Spjd goto out; 2611168404Spjd } 2612168404Spjd 2613168404Spjd /* 2614168404Spjd * If we are prefetching from the mfu ghost list, this buffer 2615168404Spjd * will end up on the mru list; so steal space from there. 2616168404Spjd */ 2617168404Spjd if (state == arc_mfu_ghost) 2618168404Spjd state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu; 2619168404Spjd else if (state == arc_mru_ghost) 2620168404Spjd state = arc_mru; 2621168404Spjd 2622168404Spjd if (state == arc_mru || state == arc_anon) { 2623168404Spjd uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size; 2624208373Smm state = (arc_mfu->arcs_lsize[type] >= size && 2625185029Spjd arc_p > mru_used) ? arc_mfu : arc_mru; 2626168404Spjd } else { 2627168404Spjd /* MFU cases */ 2628168404Spjd uint64_t mfu_space = arc_c - arc_p; 2629208373Smm state = (arc_mru->arcs_lsize[type] >= size && 2630185029Spjd mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu; 2631168404Spjd } 2632209962Smm if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) { 2633168404Spjd if (type == ARC_BUFC_METADATA) { 2634168404Spjd buf->b_data = zio_buf_alloc(size); 2635208373Smm arc_space_consume(size, ARC_SPACE_DATA); 2636168404Spjd } else { 2637168404Spjd ASSERT(type == ARC_BUFC_DATA); 2638168404Spjd buf->b_data = zio_data_buf_alloc(size); 2639208373Smm ARCSTAT_INCR(arcstat_data_size, size); 2640185029Spjd atomic_add_64(&arc_size, size); 2641168404Spjd } 2642168404Spjd ARCSTAT_BUMP(arcstat_recycle_miss); 2643168404Spjd } 2644168404Spjd ASSERT(buf->b_data != NULL); 2645168404Spjdout: 2646168404Spjd /* 2647168404Spjd * Update the state size. Note that ghost states have a 2648168404Spjd * "ghost size" and so don't need to be updated. 2649168404Spjd */ 2650168404Spjd if (!GHOST_STATE(buf->b_hdr->b_state)) { 2651168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2652168404Spjd 2653168404Spjd atomic_add_64(&hdr->b_state->arcs_size, size); 2654168404Spjd if (list_link_active(&hdr->b_arc_node)) { 2655168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 2656185029Spjd atomic_add_64(&hdr->b_state->arcs_lsize[type], size); 2657168404Spjd } 2658168404Spjd /* 2659168404Spjd * If we are growing the cache, and we are adding anonymous 2660168404Spjd * data, and we have outgrown arc_p, update arc_p 2661168404Spjd */ 2662168404Spjd if (arc_size < arc_c && hdr->b_state == arc_anon && 2663168404Spjd arc_anon->arcs_size + arc_mru->arcs_size > arc_p) 2664168404Spjd arc_p = MIN(arc_c, arc_p + size); 2665168404Spjd } 2666205231Skmacy ARCSTAT_BUMP(arcstat_allocated); 2667168404Spjd} 2668168404Spjd 2669168404Spjd/* 2670168404Spjd * This routine is called whenever a buffer is accessed. 2671168404Spjd * NOTE: the hash lock is dropped in this function. 2672168404Spjd */ 2673168404Spjdstatic void 2674168404Spjdarc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 2675168404Spjd{ 2676219089Spjd clock_t now; 2677219089Spjd 2678168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 2679168404Spjd 2680168404Spjd if (buf->b_state == arc_anon) { 2681168404Spjd /* 2682168404Spjd * This buffer is not in the cache, and does not 2683168404Spjd * appear in our "ghost" list. Add the new buffer 2684168404Spjd * to the MRU state. 2685168404Spjd */ 2686168404Spjd 2687168404Spjd ASSERT(buf->b_arc_access == 0); 2688219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2689168404Spjd DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2690168404Spjd arc_change_state(arc_mru, buf, hash_lock); 2691168404Spjd 2692168404Spjd } else if (buf->b_state == arc_mru) { 2693219089Spjd now = ddi_get_lbolt(); 2694219089Spjd 2695168404Spjd /* 2696168404Spjd * If this buffer is here because of a prefetch, then either: 2697168404Spjd * - clear the flag if this is a "referencing" read 2698168404Spjd * (any subsequent access will bump this into the MFU state). 2699168404Spjd * or 2700168404Spjd * - move the buffer to the head of the list if this is 2701168404Spjd * another prefetch (to make it less likely to be evicted). 2702168404Spjd */ 2703168404Spjd if ((buf->b_flags & ARC_PREFETCH) != 0) { 2704168404Spjd if (refcount_count(&buf->b_refcnt) == 0) { 2705168404Spjd ASSERT(list_link_active(&buf->b_arc_node)); 2706168404Spjd } else { 2707168404Spjd buf->b_flags &= ~ARC_PREFETCH; 2708168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 2709168404Spjd } 2710219089Spjd buf->b_arc_access = now; 2711168404Spjd return; 2712168404Spjd } 2713168404Spjd 2714168404Spjd /* 2715168404Spjd * This buffer has been "accessed" only once so far, 2716168404Spjd * but it is still in the cache. Move it to the MFU 2717168404Spjd * state. 2718168404Spjd */ 2719219089Spjd if (now > buf->b_arc_access + ARC_MINTIME) { 2720168404Spjd /* 2721168404Spjd * More than 125ms have passed since we 2722168404Spjd * instantiated this buffer. Move it to the 2723168404Spjd * most frequently used state. 2724168404Spjd */ 2725219089Spjd buf->b_arc_access = now; 2726168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2727168404Spjd arc_change_state(arc_mfu, buf, hash_lock); 2728168404Spjd } 2729168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 2730168404Spjd } else if (buf->b_state == arc_mru_ghost) { 2731168404Spjd arc_state_t *new_state; 2732168404Spjd /* 2733168404Spjd * This buffer has been "accessed" recently, but 2734168404Spjd * was evicted from the cache. Move it to the 2735168404Spjd * MFU state. 2736168404Spjd */ 2737168404Spjd 2738168404Spjd if (buf->b_flags & ARC_PREFETCH) { 2739168404Spjd new_state = arc_mru; 2740168404Spjd if (refcount_count(&buf->b_refcnt) > 0) 2741168404Spjd buf->b_flags &= ~ARC_PREFETCH; 2742168404Spjd DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 2743168404Spjd } else { 2744168404Spjd new_state = arc_mfu; 2745168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2746168404Spjd } 2747168404Spjd 2748219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2749168404Spjd arc_change_state(new_state, buf, hash_lock); 2750168404Spjd 2751168404Spjd ARCSTAT_BUMP(arcstat_mru_ghost_hits); 2752168404Spjd } else if (buf->b_state == arc_mfu) { 2753168404Spjd /* 2754168404Spjd * This buffer has been accessed more than once and is 2755168404Spjd * still in the cache. Keep it in the MFU state. 2756168404Spjd * 2757168404Spjd * NOTE: an add_reference() that occurred when we did 2758168404Spjd * the arc_read() will have kicked this off the list. 2759168404Spjd * If it was a prefetch, we will explicitly move it to 2760168404Spjd * the head of the list now. 2761168404Spjd */ 2762168404Spjd if ((buf->b_flags & ARC_PREFETCH) != 0) { 2763168404Spjd ASSERT(refcount_count(&buf->b_refcnt) == 0); 2764168404Spjd ASSERT(list_link_active(&buf->b_arc_node)); 2765168404Spjd } 2766168404Spjd ARCSTAT_BUMP(arcstat_mfu_hits); 2767219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2768168404Spjd } else if (buf->b_state == arc_mfu_ghost) { 2769168404Spjd arc_state_t *new_state = arc_mfu; 2770168404Spjd /* 2771168404Spjd * This buffer has been accessed more than once but has 2772168404Spjd * been evicted from the cache. Move it back to the 2773168404Spjd * MFU state. 2774168404Spjd */ 2775168404Spjd 2776168404Spjd if (buf->b_flags & ARC_PREFETCH) { 2777168404Spjd /* 2778168404Spjd * This is a prefetch access... 2779168404Spjd * move this block back to the MRU state. 2780168404Spjd */ 2781168404Spjd ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0); 2782168404Spjd new_state = arc_mru; 2783168404Spjd } 2784168404Spjd 2785219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2786168404Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2787168404Spjd arc_change_state(new_state, buf, hash_lock); 2788168404Spjd 2789168404Spjd ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 2790185029Spjd } else if (buf->b_state == arc_l2c_only) { 2791185029Spjd /* 2792185029Spjd * This buffer is on the 2nd Level ARC. 2793185029Spjd */ 2794185029Spjd 2795219089Spjd buf->b_arc_access = ddi_get_lbolt(); 2796185029Spjd DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 2797185029Spjd arc_change_state(arc_mfu, buf, hash_lock); 2798168404Spjd } else { 2799168404Spjd ASSERT(!"invalid arc state"); 2800168404Spjd } 2801168404Spjd} 2802168404Spjd 2803168404Spjd/* a generic arc_done_func_t which you can use */ 2804168404Spjd/* ARGSUSED */ 2805168404Spjdvoid 2806168404Spjdarc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 2807168404Spjd{ 2808219089Spjd if (zio == NULL || zio->io_error == 0) 2809219089Spjd bcopy(buf->b_data, arg, buf->b_hdr->b_size); 2810168404Spjd VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2811168404Spjd} 2812168404Spjd 2813185029Spjd/* a generic arc_done_func_t */ 2814168404Spjdvoid 2815168404Spjdarc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 2816168404Spjd{ 2817168404Spjd arc_buf_t **bufp = arg; 2818168404Spjd if (zio && zio->io_error) { 2819168404Spjd VERIFY(arc_buf_remove_ref(buf, arg) == 1); 2820168404Spjd *bufp = NULL; 2821168404Spjd } else { 2822168404Spjd *bufp = buf; 2823219089Spjd ASSERT(buf->b_data); 2824168404Spjd } 2825168404Spjd} 2826168404Spjd 2827168404Spjdstatic void 2828168404Spjdarc_read_done(zio_t *zio) 2829168404Spjd{ 2830168404Spjd arc_buf_hdr_t *hdr, *found; 2831168404Spjd arc_buf_t *buf; 2832168404Spjd arc_buf_t *abuf; /* buffer we're assigning to callback */ 2833168404Spjd kmutex_t *hash_lock; 2834168404Spjd arc_callback_t *callback_list, *acb; 2835168404Spjd int freeable = FALSE; 2836168404Spjd 2837168404Spjd buf = zio->io_private; 2838168404Spjd hdr = buf->b_hdr; 2839168404Spjd 2840168404Spjd /* 2841168404Spjd * The hdr was inserted into hash-table and removed from lists 2842168404Spjd * prior to starting I/O. We should find this header, since 2843168404Spjd * it's in the hash table, and it should be legit since it's 2844168404Spjd * not possible to evict it during the I/O. The only possible 2845168404Spjd * reason for it not to be found is if we were freed during the 2846168404Spjd * read. 2847168404Spjd */ 2848209962Smm found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth, 2849168404Spjd &hash_lock); 2850168404Spjd 2851168404Spjd ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) || 2852185029Spjd (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 2853185029Spjd (found == hdr && HDR_L2_READING(hdr))); 2854168404Spjd 2855185029Spjd hdr->b_flags &= ~ARC_L2_EVICTED; 2856185029Spjd if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH)) 2857185029Spjd hdr->b_flags &= ~ARC_L2CACHE; 2858206796Spjd 2859168404Spjd /* byteswap if necessary */ 2860168404Spjd callback_list = hdr->b_acb; 2861168404Spjd ASSERT(callback_list != NULL); 2862209101Smm if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 2863236884Smm dmu_object_byteswap_t bswap = 2864236884Smm DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 2865185029Spjd arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 2866185029Spjd byteswap_uint64_array : 2867236884Smm dmu_ot_byteswap[bswap].ob_func; 2868185029Spjd func(buf->b_data, hdr->b_size); 2869185029Spjd } 2870168404Spjd 2871185029Spjd arc_cksum_compute(buf, B_FALSE); 2872240133Smm#ifdef illumos 2873240133Smm arc_buf_watch(buf); 2874240133Smm#endif /* illumos */ 2875168404Spjd 2876219089Spjd if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) { 2877219089Spjd /* 2878219089Spjd * Only call arc_access on anonymous buffers. This is because 2879219089Spjd * if we've issued an I/O for an evicted buffer, we've already 2880219089Spjd * called arc_access (to prevent any simultaneous readers from 2881219089Spjd * getting confused). 2882219089Spjd */ 2883219089Spjd arc_access(hdr, hash_lock); 2884219089Spjd } 2885219089Spjd 2886168404Spjd /* create copies of the data buffer for the callers */ 2887168404Spjd abuf = buf; 2888168404Spjd for (acb = callback_list; acb; acb = acb->acb_next) { 2889168404Spjd if (acb->acb_done) { 2890168404Spjd if (abuf == NULL) 2891168404Spjd abuf = arc_buf_clone(buf); 2892168404Spjd acb->acb_buf = abuf; 2893168404Spjd abuf = NULL; 2894168404Spjd } 2895168404Spjd } 2896168404Spjd hdr->b_acb = NULL; 2897168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 2898168404Spjd ASSERT(!HDR_BUF_AVAILABLE(hdr)); 2899219089Spjd if (abuf == buf) { 2900219089Spjd ASSERT(buf->b_efunc == NULL); 2901219089Spjd ASSERT(hdr->b_datacnt == 1); 2902168404Spjd hdr->b_flags |= ARC_BUF_AVAILABLE; 2903219089Spjd } 2904168404Spjd 2905168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 2906168404Spjd 2907168404Spjd if (zio->io_error != 0) { 2908168404Spjd hdr->b_flags |= ARC_IO_ERROR; 2909168404Spjd if (hdr->b_state != arc_anon) 2910168404Spjd arc_change_state(arc_anon, hdr, hash_lock); 2911168404Spjd if (HDR_IN_HASH_TABLE(hdr)) 2912168404Spjd buf_hash_remove(hdr); 2913168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 2914168404Spjd } 2915168404Spjd 2916168404Spjd /* 2917168404Spjd * Broadcast before we drop the hash_lock to avoid the possibility 2918168404Spjd * that the hdr (and hence the cv) might be freed before we get to 2919168404Spjd * the cv_broadcast(). 2920168404Spjd */ 2921168404Spjd cv_broadcast(&hdr->b_cv); 2922168404Spjd 2923168404Spjd if (hash_lock) { 2924168404Spjd mutex_exit(hash_lock); 2925168404Spjd } else { 2926168404Spjd /* 2927168404Spjd * This block was freed while we waited for the read to 2928168404Spjd * complete. It has been removed from the hash table and 2929168404Spjd * moved to the anonymous state (so that it won't show up 2930168404Spjd * in the cache). 2931168404Spjd */ 2932168404Spjd ASSERT3P(hdr->b_state, ==, arc_anon); 2933168404Spjd freeable = refcount_is_zero(&hdr->b_refcnt); 2934168404Spjd } 2935168404Spjd 2936168404Spjd /* execute each callback and free its structure */ 2937168404Spjd while ((acb = callback_list) != NULL) { 2938168404Spjd if (acb->acb_done) 2939168404Spjd acb->acb_done(zio, acb->acb_buf, acb->acb_private); 2940168404Spjd 2941168404Spjd if (acb->acb_zio_dummy != NULL) { 2942168404Spjd acb->acb_zio_dummy->io_error = zio->io_error; 2943168404Spjd zio_nowait(acb->acb_zio_dummy); 2944168404Spjd } 2945168404Spjd 2946168404Spjd callback_list = acb->acb_next; 2947168404Spjd kmem_free(acb, sizeof (arc_callback_t)); 2948168404Spjd } 2949168404Spjd 2950168404Spjd if (freeable) 2951168404Spjd arc_hdr_destroy(hdr); 2952168404Spjd} 2953168404Spjd 2954168404Spjd/* 2955168404Spjd * "Read" the block block at the specified DVA (in bp) via the 2956168404Spjd * cache. If the block is found in the cache, invoke the provided 2957168404Spjd * callback immediately and return. Note that the `zio' parameter 2958168404Spjd * in the callback will be NULL in this case, since no IO was 2959168404Spjd * required. If the block is not in the cache pass the read request 2960168404Spjd * on to the spa with a substitute callback function, so that the 2961168404Spjd * requested block will be added to the cache. 2962168404Spjd * 2963168404Spjd * If a read request arrives for a block that has a read in-progress, 2964168404Spjd * either wait for the in-progress read to complete (and return the 2965168404Spjd * results); or, if this is a read with a "done" func, add a record 2966168404Spjd * to the read to invoke the "done" func when the read completes, 2967168404Spjd * and return; or just return. 2968168404Spjd * 2969168404Spjd * arc_read_done() will invoke all the requested "done" functions 2970168404Spjd * for readers of this block. 2971185029Spjd * 2972185029Spjd * Normal callers should use arc_read and pass the arc buffer and offset 2973185029Spjd * for the bp. But if you know you don't need locking, you can use 2974219089Spjd * arc_read_nolock. 2975168404Spjd */ 2976168404Spjdint 2977219089Spjdarc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf, 2978185029Spjd arc_done_func_t *done, void *private, int priority, int zio_flags, 2979185029Spjd uint32_t *arc_flags, const zbookmark_t *zb) 2980168404Spjd{ 2981185029Spjd int err; 2982185029Spjd 2983219089Spjd if (pbuf == NULL) { 2984219089Spjd /* 2985219089Spjd * XXX This happens from traverse callback funcs, for 2986219089Spjd * the objset_phys_t block. 2987219089Spjd */ 2988219089Spjd return (arc_read_nolock(pio, spa, bp, done, private, priority, 2989219089Spjd zio_flags, arc_flags, zb)); 2990219089Spjd } 2991219089Spjd 2992185029Spjd ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt)); 2993185029Spjd ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size); 2994219089Spjd rw_enter(&pbuf->b_data_lock, RW_READER); 2995185029Spjd 2996185029Spjd err = arc_read_nolock(pio, spa, bp, done, private, priority, 2997185029Spjd zio_flags, arc_flags, zb); 2998219089Spjd rw_exit(&pbuf->b_data_lock); 2999219089Spjd 3000185029Spjd return (err); 3001185029Spjd} 3002185029Spjd 3003185029Spjdint 3004219089Spjdarc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp, 3005185029Spjd arc_done_func_t *done, void *private, int priority, int zio_flags, 3006185029Spjd uint32_t *arc_flags, const zbookmark_t *zb) 3007185029Spjd{ 3008168404Spjd arc_buf_hdr_t *hdr; 3009168404Spjd arc_buf_t *buf; 3010168404Spjd kmutex_t *hash_lock; 3011185029Spjd zio_t *rzio; 3012228103Smm uint64_t guid = spa_load_guid(spa); 3013168404Spjd 3014168404Spjdtop: 3015219089Spjd hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp), 3016219089Spjd &hash_lock); 3017168404Spjd if (hdr && hdr->b_datacnt > 0) { 3018168404Spjd 3019168404Spjd *arc_flags |= ARC_CACHED; 3020168404Spjd 3021168404Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3022168404Spjd 3023168404Spjd if (*arc_flags & ARC_WAIT) { 3024168404Spjd cv_wait(&hdr->b_cv, hash_lock); 3025168404Spjd mutex_exit(hash_lock); 3026168404Spjd goto top; 3027168404Spjd } 3028168404Spjd ASSERT(*arc_flags & ARC_NOWAIT); 3029168404Spjd 3030168404Spjd if (done) { 3031168404Spjd arc_callback_t *acb = NULL; 3032168404Spjd 3033168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), 3034168404Spjd KM_SLEEP); 3035168404Spjd acb->acb_done = done; 3036168404Spjd acb->acb_private = private; 3037168404Spjd if (pio != NULL) 3038168404Spjd acb->acb_zio_dummy = zio_null(pio, 3039209962Smm spa, NULL, NULL, NULL, zio_flags); 3040168404Spjd 3041168404Spjd ASSERT(acb->acb_done != NULL); 3042168404Spjd acb->acb_next = hdr->b_acb; 3043168404Spjd hdr->b_acb = acb; 3044168404Spjd add_reference(hdr, hash_lock, private); 3045168404Spjd mutex_exit(hash_lock); 3046168404Spjd return (0); 3047168404Spjd } 3048168404Spjd mutex_exit(hash_lock); 3049168404Spjd return (0); 3050168404Spjd } 3051168404Spjd 3052168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3053168404Spjd 3054168404Spjd if (done) { 3055168404Spjd add_reference(hdr, hash_lock, private); 3056168404Spjd /* 3057168404Spjd * If this block is already in use, create a new 3058168404Spjd * copy of the data so that we will be guaranteed 3059168404Spjd * that arc_release() will always succeed. 3060168404Spjd */ 3061168404Spjd buf = hdr->b_buf; 3062168404Spjd ASSERT(buf); 3063168404Spjd ASSERT(buf->b_data); 3064168404Spjd if (HDR_BUF_AVAILABLE(hdr)) { 3065168404Spjd ASSERT(buf->b_efunc == NULL); 3066168404Spjd hdr->b_flags &= ~ARC_BUF_AVAILABLE; 3067168404Spjd } else { 3068168404Spjd buf = arc_buf_clone(buf); 3069168404Spjd } 3070219089Spjd 3071168404Spjd } else if (*arc_flags & ARC_PREFETCH && 3072168404Spjd refcount_count(&hdr->b_refcnt) == 0) { 3073168404Spjd hdr->b_flags |= ARC_PREFETCH; 3074168404Spjd } 3075168404Spjd DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 3076168404Spjd arc_access(hdr, hash_lock); 3077185029Spjd if (*arc_flags & ARC_L2CACHE) 3078185029Spjd hdr->b_flags |= ARC_L2CACHE; 3079168404Spjd mutex_exit(hash_lock); 3080168404Spjd ARCSTAT_BUMP(arcstat_hits); 3081168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 3082168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3083168404Spjd data, metadata, hits); 3084168404Spjd 3085168404Spjd if (done) 3086168404Spjd done(NULL, buf, private); 3087168404Spjd } else { 3088168404Spjd uint64_t size = BP_GET_LSIZE(bp); 3089168404Spjd arc_callback_t *acb; 3090185029Spjd vdev_t *vd = NULL; 3091208373Smm uint64_t addr; 3092208373Smm boolean_t devw = B_FALSE; 3093168404Spjd 3094168404Spjd if (hdr == NULL) { 3095168404Spjd /* this block is not in the cache */ 3096168404Spjd arc_buf_hdr_t *exists; 3097168404Spjd arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 3098168404Spjd buf = arc_buf_alloc(spa, size, private, type); 3099168404Spjd hdr = buf->b_hdr; 3100168404Spjd hdr->b_dva = *BP_IDENTITY(bp); 3101219089Spjd hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 3102168404Spjd hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 3103168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 3104168404Spjd if (exists) { 3105168404Spjd /* somebody beat us to the hash insert */ 3106168404Spjd mutex_exit(hash_lock); 3107219089Spjd buf_discard_identity(hdr); 3108168404Spjd (void) arc_buf_remove_ref(buf, private); 3109168404Spjd goto top; /* restart the IO request */ 3110168404Spjd } 3111168404Spjd /* if this is a prefetch, we don't have a reference */ 3112168404Spjd if (*arc_flags & ARC_PREFETCH) { 3113168404Spjd (void) remove_reference(hdr, hash_lock, 3114168404Spjd private); 3115168404Spjd hdr->b_flags |= ARC_PREFETCH; 3116168404Spjd } 3117185029Spjd if (*arc_flags & ARC_L2CACHE) 3118185029Spjd hdr->b_flags |= ARC_L2CACHE; 3119168404Spjd if (BP_GET_LEVEL(bp) > 0) 3120168404Spjd hdr->b_flags |= ARC_INDIRECT; 3121168404Spjd } else { 3122168404Spjd /* this block is in the ghost cache */ 3123168404Spjd ASSERT(GHOST_STATE(hdr->b_state)); 3124168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3125168404Spjd ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0); 3126168404Spjd ASSERT(hdr->b_buf == NULL); 3127168404Spjd 3128168404Spjd /* if this is a prefetch, we don't have a reference */ 3129168404Spjd if (*arc_flags & ARC_PREFETCH) 3130168404Spjd hdr->b_flags |= ARC_PREFETCH; 3131168404Spjd else 3132168404Spjd add_reference(hdr, hash_lock, private); 3133185029Spjd if (*arc_flags & ARC_L2CACHE) 3134185029Spjd hdr->b_flags |= ARC_L2CACHE; 3135185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 3136168404Spjd buf->b_hdr = hdr; 3137168404Spjd buf->b_data = NULL; 3138168404Spjd buf->b_efunc = NULL; 3139168404Spjd buf->b_private = NULL; 3140168404Spjd buf->b_next = NULL; 3141168404Spjd hdr->b_buf = buf; 3142168404Spjd ASSERT(hdr->b_datacnt == 0); 3143168404Spjd hdr->b_datacnt = 1; 3144219089Spjd arc_get_data_buf(buf); 3145219089Spjd arc_access(hdr, hash_lock); 3146168404Spjd } 3147168404Spjd 3148219089Spjd ASSERT(!GHOST_STATE(hdr->b_state)); 3149219089Spjd 3150168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 3151168404Spjd acb->acb_done = done; 3152168404Spjd acb->acb_private = private; 3153168404Spjd 3154168404Spjd ASSERT(hdr->b_acb == NULL); 3155168404Spjd hdr->b_acb = acb; 3156168404Spjd hdr->b_flags |= ARC_IO_IN_PROGRESS; 3157168404Spjd 3158185029Spjd if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL && 3159185029Spjd (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) { 3160208373Smm devw = hdr->b_l2hdr->b_dev->l2ad_writing; 3161185029Spjd addr = hdr->b_l2hdr->b_daddr; 3162185029Spjd /* 3163185029Spjd * Lock out device removal. 3164185029Spjd */ 3165185029Spjd if (vdev_is_dead(vd) || 3166185029Spjd !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 3167185029Spjd vd = NULL; 3168185029Spjd } 3169185029Spjd 3170168404Spjd mutex_exit(hash_lock); 3171168404Spjd 3172168404Spjd ASSERT3U(hdr->b_size, ==, size); 3173219089Spjd DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 3174219089Spjd uint64_t, size, zbookmark_t *, zb); 3175168404Spjd ARCSTAT_BUMP(arcstat_misses); 3176168404Spjd ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH), 3177168404Spjd demand, prefetch, hdr->b_type != ARC_BUFC_METADATA, 3178168404Spjd data, metadata, misses); 3179228392Spjd#ifdef _KERNEL 3180228392Spjd curthread->td_ru.ru_inblock++; 3181228392Spjd#endif 3182168404Spjd 3183208373Smm if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 3184185029Spjd /* 3185185029Spjd * Read from the L2ARC if the following are true: 3186185029Spjd * 1. The L2ARC vdev was previously cached. 3187185029Spjd * 2. This buffer still has L2ARC metadata. 3188185029Spjd * 3. This buffer isn't currently writing to the L2ARC. 3189185029Spjd * 4. The L2ARC entry wasn't evicted, which may 3190185029Spjd * also have invalidated the vdev. 3191208373Smm * 5. This isn't prefetch and l2arc_noprefetch is set. 3192185029Spjd */ 3193185029Spjd if (hdr->b_l2hdr != NULL && 3194208373Smm !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 3195208373Smm !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 3196185029Spjd l2arc_read_callback_t *cb; 3197185029Spjd 3198185029Spjd DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 3199185029Spjd ARCSTAT_BUMP(arcstat_l2_hits); 3200185029Spjd 3201185029Spjd cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 3202185029Spjd KM_SLEEP); 3203185029Spjd cb->l2rcb_buf = buf; 3204185029Spjd cb->l2rcb_spa = spa; 3205185029Spjd cb->l2rcb_bp = *bp; 3206185029Spjd cb->l2rcb_zb = *zb; 3207185029Spjd cb->l2rcb_flags = zio_flags; 3208185029Spjd 3209185029Spjd /* 3210185029Spjd * l2arc read. The SCL_L2ARC lock will be 3211185029Spjd * released by l2arc_read_done(). 3212185029Spjd */ 3213185029Spjd rzio = zio_read_phys(pio, vd, addr, size, 3214206796Spjd buf->b_data, ZIO_CHECKSUM_OFF, 3215185029Spjd l2arc_read_done, cb, priority, zio_flags | 3216185029Spjd ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | 3217185029Spjd ZIO_FLAG_DONT_PROPAGATE | 3218185029Spjd ZIO_FLAG_DONT_RETRY, B_FALSE); 3219185029Spjd DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 3220185029Spjd zio_t *, rzio); 3221208373Smm ARCSTAT_INCR(arcstat_l2_read_bytes, size); 3222185029Spjd 3223185029Spjd if (*arc_flags & ARC_NOWAIT) { 3224185029Spjd zio_nowait(rzio); 3225185029Spjd return (0); 3226185029Spjd } 3227185029Spjd 3228185029Spjd ASSERT(*arc_flags & ARC_WAIT); 3229185029Spjd if (zio_wait(rzio) == 0) 3230185029Spjd return (0); 3231185029Spjd 3232185029Spjd /* l2arc read error; goto zio_read() */ 3233185029Spjd } else { 3234185029Spjd DTRACE_PROBE1(l2arc__miss, 3235185029Spjd arc_buf_hdr_t *, hdr); 3236185029Spjd ARCSTAT_BUMP(arcstat_l2_misses); 3237185029Spjd if (HDR_L2_WRITING(hdr)) 3238185029Spjd ARCSTAT_BUMP(arcstat_l2_rw_clash); 3239185029Spjd spa_config_exit(spa, SCL_L2ARC, vd); 3240185029Spjd } 3241208373Smm } else { 3242208373Smm if (vd != NULL) 3243208373Smm spa_config_exit(spa, SCL_L2ARC, vd); 3244208373Smm if (l2arc_ndev != 0) { 3245208373Smm DTRACE_PROBE1(l2arc__miss, 3246208373Smm arc_buf_hdr_t *, hdr); 3247208373Smm ARCSTAT_BUMP(arcstat_l2_misses); 3248208373Smm } 3249185029Spjd } 3250185029Spjd 3251168404Spjd rzio = zio_read(pio, spa, bp, buf->b_data, size, 3252185029Spjd arc_read_done, buf, priority, zio_flags, zb); 3253168404Spjd 3254168404Spjd if (*arc_flags & ARC_WAIT) 3255168404Spjd return (zio_wait(rzio)); 3256168404Spjd 3257168404Spjd ASSERT(*arc_flags & ARC_NOWAIT); 3258168404Spjd zio_nowait(rzio); 3259168404Spjd } 3260168404Spjd return (0); 3261168404Spjd} 3262168404Spjd 3263168404Spjdvoid 3264168404Spjdarc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 3265168404Spjd{ 3266168404Spjd ASSERT(buf->b_hdr != NULL); 3267168404Spjd ASSERT(buf->b_hdr->b_state != arc_anon); 3268168404Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 3269219089Spjd ASSERT(buf->b_efunc == NULL); 3270219089Spjd ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 3271219089Spjd 3272168404Spjd buf->b_efunc = func; 3273168404Spjd buf->b_private = private; 3274168404Spjd} 3275168404Spjd 3276168404Spjd/* 3277168404Spjd * This is used by the DMU to let the ARC know that a buffer is 3278168404Spjd * being evicted, so the ARC should clean up. If this arc buf 3279168404Spjd * is not yet in the evicted state, it will be put there. 3280168404Spjd */ 3281168404Spjdint 3282168404Spjdarc_buf_evict(arc_buf_t *buf) 3283168404Spjd{ 3284168404Spjd arc_buf_hdr_t *hdr; 3285168404Spjd kmutex_t *hash_lock; 3286168404Spjd arc_buf_t **bufp; 3287205231Skmacy list_t *list, *evicted_list; 3288205231Skmacy kmutex_t *lock, *evicted_lock; 3289206796Spjd 3290219089Spjd mutex_enter(&buf->b_evict_lock); 3291168404Spjd hdr = buf->b_hdr; 3292168404Spjd if (hdr == NULL) { 3293168404Spjd /* 3294168404Spjd * We are in arc_do_user_evicts(). 3295168404Spjd */ 3296168404Spjd ASSERT(buf->b_data == NULL); 3297219089Spjd mutex_exit(&buf->b_evict_lock); 3298168404Spjd return (0); 3299185029Spjd } else if (buf->b_data == NULL) { 3300185029Spjd arc_buf_t copy = *buf; /* structure assignment */ 3301185029Spjd /* 3302185029Spjd * We are on the eviction list; process this buffer now 3303185029Spjd * but let arc_do_user_evicts() do the reaping. 3304185029Spjd */ 3305185029Spjd buf->b_efunc = NULL; 3306219089Spjd mutex_exit(&buf->b_evict_lock); 3307185029Spjd VERIFY(copy.b_efunc(©) == 0); 3308185029Spjd return (1); 3309168404Spjd } 3310168404Spjd hash_lock = HDR_LOCK(hdr); 3311168404Spjd mutex_enter(hash_lock); 3312219089Spjd hdr = buf->b_hdr; 3313219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3314168404Spjd 3315168404Spjd ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 3316168404Spjd ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu); 3317168404Spjd 3318168404Spjd /* 3319168404Spjd * Pull this buffer off of the hdr 3320168404Spjd */ 3321168404Spjd bufp = &hdr->b_buf; 3322168404Spjd while (*bufp != buf) 3323168404Spjd bufp = &(*bufp)->b_next; 3324168404Spjd *bufp = buf->b_next; 3325168404Spjd 3326168404Spjd ASSERT(buf->b_data != NULL); 3327168404Spjd arc_buf_destroy(buf, FALSE, FALSE); 3328168404Spjd 3329168404Spjd if (hdr->b_datacnt == 0) { 3330168404Spjd arc_state_t *old_state = hdr->b_state; 3331168404Spjd arc_state_t *evicted_state; 3332168404Spjd 3333219089Spjd ASSERT(hdr->b_buf == NULL); 3334168404Spjd ASSERT(refcount_is_zero(&hdr->b_refcnt)); 3335168404Spjd 3336168404Spjd evicted_state = 3337168404Spjd (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 3338168404Spjd 3339205231Skmacy get_buf_info(hdr, old_state, &list, &lock); 3340205231Skmacy get_buf_info(hdr, evicted_state, &evicted_list, &evicted_lock); 3341205231Skmacy mutex_enter(lock); 3342205231Skmacy mutex_enter(evicted_lock); 3343168404Spjd 3344168404Spjd arc_change_state(evicted_state, hdr, hash_lock); 3345168404Spjd ASSERT(HDR_IN_HASH_TABLE(hdr)); 3346185029Spjd hdr->b_flags |= ARC_IN_HASH_TABLE; 3347185029Spjd hdr->b_flags &= ~ARC_BUF_AVAILABLE; 3348168404Spjd 3349205231Skmacy mutex_exit(evicted_lock); 3350205231Skmacy mutex_exit(lock); 3351168404Spjd } 3352168404Spjd mutex_exit(hash_lock); 3353219089Spjd mutex_exit(&buf->b_evict_lock); 3354168404Spjd 3355168404Spjd VERIFY(buf->b_efunc(buf) == 0); 3356168404Spjd buf->b_efunc = NULL; 3357168404Spjd buf->b_private = NULL; 3358168404Spjd buf->b_hdr = NULL; 3359219089Spjd buf->b_next = NULL; 3360168404Spjd kmem_cache_free(buf_cache, buf); 3361168404Spjd return (1); 3362168404Spjd} 3363168404Spjd 3364168404Spjd/* 3365168404Spjd * Release this buffer from the cache. This must be done 3366168404Spjd * after a read and prior to modifying the buffer contents. 3367168404Spjd * If the buffer has more than one reference, we must make 3368185029Spjd * a new hdr for the buffer. 3369168404Spjd */ 3370168404Spjdvoid 3371168404Spjdarc_release(arc_buf_t *buf, void *tag) 3372168404Spjd{ 3373185029Spjd arc_buf_hdr_t *hdr; 3374219089Spjd kmutex_t *hash_lock = NULL; 3375185029Spjd l2arc_buf_hdr_t *l2hdr; 3376185029Spjd uint64_t buf_size; 3377168404Spjd 3378219089Spjd /* 3379219089Spjd * It would be nice to assert that if it's DMU metadata (level > 3380219089Spjd * 0 || it's the dnode file), then it must be syncing context. 3381219089Spjd * But we don't know that information at this level. 3382219089Spjd */ 3383219089Spjd 3384219089Spjd mutex_enter(&buf->b_evict_lock); 3385185029Spjd hdr = buf->b_hdr; 3386185029Spjd 3387168404Spjd /* this buffer is not on any list */ 3388168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) > 0); 3389168404Spjd 3390168404Spjd if (hdr->b_state == arc_anon) { 3391168404Spjd /* this buffer is already released */ 3392168404Spjd ASSERT(buf->b_efunc == NULL); 3393208373Smm } else { 3394208373Smm hash_lock = HDR_LOCK(hdr); 3395208373Smm mutex_enter(hash_lock); 3396219089Spjd hdr = buf->b_hdr; 3397219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3398168404Spjd } 3399168404Spjd 3400185029Spjd l2hdr = hdr->b_l2hdr; 3401185029Spjd if (l2hdr) { 3402185029Spjd mutex_enter(&l2arc_buflist_mtx); 3403185029Spjd hdr->b_l2hdr = NULL; 3404185029Spjd buf_size = hdr->b_size; 3405185029Spjd } 3406185029Spjd 3407168404Spjd /* 3408168404Spjd * Do we have more than one buf? 3409168404Spjd */ 3410185029Spjd if (hdr->b_datacnt > 1) { 3411168404Spjd arc_buf_hdr_t *nhdr; 3412168404Spjd arc_buf_t **bufp; 3413168404Spjd uint64_t blksz = hdr->b_size; 3414209962Smm uint64_t spa = hdr->b_spa; 3415168404Spjd arc_buf_contents_t type = hdr->b_type; 3416185029Spjd uint32_t flags = hdr->b_flags; 3417168404Spjd 3418185029Spjd ASSERT(hdr->b_buf != buf || buf->b_next != NULL); 3419168404Spjd /* 3420219089Spjd * Pull the data off of this hdr and attach it to 3421219089Spjd * a new anonymous hdr. 3422168404Spjd */ 3423168404Spjd (void) remove_reference(hdr, hash_lock, tag); 3424168404Spjd bufp = &hdr->b_buf; 3425168404Spjd while (*bufp != buf) 3426168404Spjd bufp = &(*bufp)->b_next; 3427219089Spjd *bufp = buf->b_next; 3428168404Spjd buf->b_next = NULL; 3429168404Spjd 3430168404Spjd ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size); 3431168404Spjd atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size); 3432168404Spjd if (refcount_is_zero(&hdr->b_refcnt)) { 3433185029Spjd uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type]; 3434185029Spjd ASSERT3U(*size, >=, hdr->b_size); 3435185029Spjd atomic_add_64(size, -hdr->b_size); 3436168404Spjd } 3437168404Spjd hdr->b_datacnt -= 1; 3438168404Spjd arc_cksum_verify(buf); 3439240133Smm#ifdef illumos 3440240133Smm arc_buf_unwatch(buf); 3441240133Smm#endif /* illumos */ 3442168404Spjd 3443168404Spjd mutex_exit(hash_lock); 3444168404Spjd 3445185029Spjd nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 3446168404Spjd nhdr->b_size = blksz; 3447168404Spjd nhdr->b_spa = spa; 3448168404Spjd nhdr->b_type = type; 3449168404Spjd nhdr->b_buf = buf; 3450168404Spjd nhdr->b_state = arc_anon; 3451168404Spjd nhdr->b_arc_access = 0; 3452185029Spjd nhdr->b_flags = flags & ARC_L2_WRITING; 3453185029Spjd nhdr->b_l2hdr = NULL; 3454168404Spjd nhdr->b_datacnt = 1; 3455168404Spjd nhdr->b_freeze_cksum = NULL; 3456168404Spjd (void) refcount_add(&nhdr->b_refcnt, tag); 3457168404Spjd buf->b_hdr = nhdr; 3458219089Spjd mutex_exit(&buf->b_evict_lock); 3459168404Spjd atomic_add_64(&arc_anon->arcs_size, blksz); 3460168404Spjd } else { 3461219089Spjd mutex_exit(&buf->b_evict_lock); 3462168404Spjd ASSERT(refcount_count(&hdr->b_refcnt) == 1); 3463168404Spjd ASSERT(!list_link_active(&hdr->b_arc_node)); 3464168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3465219089Spjd if (hdr->b_state != arc_anon) 3466219089Spjd arc_change_state(arc_anon, hdr, hash_lock); 3467168404Spjd hdr->b_arc_access = 0; 3468219089Spjd if (hash_lock) 3469219089Spjd mutex_exit(hash_lock); 3470185029Spjd 3471219089Spjd buf_discard_identity(hdr); 3472168404Spjd arc_buf_thaw(buf); 3473168404Spjd } 3474168404Spjd buf->b_efunc = NULL; 3475168404Spjd buf->b_private = NULL; 3476185029Spjd 3477185029Spjd if (l2hdr) { 3478185029Spjd list_remove(l2hdr->b_dev->l2ad_buflist, hdr); 3479185029Spjd kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t)); 3480185029Spjd ARCSTAT_INCR(arcstat_l2_size, -buf_size); 3481185029Spjd mutex_exit(&l2arc_buflist_mtx); 3482185029Spjd } 3483168404Spjd} 3484168404Spjd 3485219089Spjd/* 3486219089Spjd * Release this buffer. If it does not match the provided BP, fill it 3487219089Spjd * with that block's contents. 3488219089Spjd */ 3489219089Spjd/* ARGSUSED */ 3490168404Spjdint 3491219089Spjdarc_release_bp(arc_buf_t *buf, void *tag, blkptr_t *bp, spa_t *spa, 3492219089Spjd zbookmark_t *zb) 3493219089Spjd{ 3494219089Spjd arc_release(buf, tag); 3495219089Spjd return (0); 3496219089Spjd} 3497219089Spjd 3498219089Spjdint 3499168404Spjdarc_released(arc_buf_t *buf) 3500168404Spjd{ 3501185029Spjd int released; 3502185029Spjd 3503219089Spjd mutex_enter(&buf->b_evict_lock); 3504185029Spjd released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon); 3505219089Spjd mutex_exit(&buf->b_evict_lock); 3506185029Spjd return (released); 3507168404Spjd} 3508168404Spjd 3509168404Spjdint 3510168404Spjdarc_has_callback(arc_buf_t *buf) 3511168404Spjd{ 3512185029Spjd int callback; 3513185029Spjd 3514219089Spjd mutex_enter(&buf->b_evict_lock); 3515185029Spjd callback = (buf->b_efunc != NULL); 3516219089Spjd mutex_exit(&buf->b_evict_lock); 3517185029Spjd return (callback); 3518168404Spjd} 3519168404Spjd 3520168404Spjd#ifdef ZFS_DEBUG 3521168404Spjdint 3522168404Spjdarc_referenced(arc_buf_t *buf) 3523168404Spjd{ 3524185029Spjd int referenced; 3525185029Spjd 3526219089Spjd mutex_enter(&buf->b_evict_lock); 3527185029Spjd referenced = (refcount_count(&buf->b_hdr->b_refcnt)); 3528219089Spjd mutex_exit(&buf->b_evict_lock); 3529185029Spjd return (referenced); 3530168404Spjd} 3531168404Spjd#endif 3532168404Spjd 3533168404Spjdstatic void 3534168404Spjdarc_write_ready(zio_t *zio) 3535168404Spjd{ 3536168404Spjd arc_write_callback_t *callback = zio->io_private; 3537168404Spjd arc_buf_t *buf = callback->awcb_buf; 3538185029Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3539168404Spjd 3540185029Spjd ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt)); 3541185029Spjd callback->awcb_ready(zio, buf, callback->awcb_private); 3542185029Spjd 3543185029Spjd /* 3544185029Spjd * If the IO is already in progress, then this is a re-write 3545185029Spjd * attempt, so we need to thaw and re-compute the cksum. 3546185029Spjd * It is the responsibility of the callback to handle the 3547185029Spjd * accounting for any re-write attempt. 3548185029Spjd */ 3549185029Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 3550185029Spjd mutex_enter(&hdr->b_freeze_lock); 3551185029Spjd if (hdr->b_freeze_cksum != NULL) { 3552185029Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 3553185029Spjd hdr->b_freeze_cksum = NULL; 3554185029Spjd } 3555185029Spjd mutex_exit(&hdr->b_freeze_lock); 3556168404Spjd } 3557185029Spjd arc_cksum_compute(buf, B_FALSE); 3558185029Spjd hdr->b_flags |= ARC_IO_IN_PROGRESS; 3559168404Spjd} 3560168404Spjd 3561168404Spjdstatic void 3562168404Spjdarc_write_done(zio_t *zio) 3563168404Spjd{ 3564168404Spjd arc_write_callback_t *callback = zio->io_private; 3565168404Spjd arc_buf_t *buf = callback->awcb_buf; 3566168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3567168404Spjd 3568219089Spjd ASSERT(hdr->b_acb == NULL); 3569168404Spjd 3570219089Spjd if (zio->io_error == 0) { 3571219089Spjd hdr->b_dva = *BP_IDENTITY(zio->io_bp); 3572219089Spjd hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 3573219089Spjd hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 3574219089Spjd } else { 3575219089Spjd ASSERT(BUF_EMPTY(hdr)); 3576219089Spjd } 3577219089Spjd 3578168404Spjd /* 3579168404Spjd * If the block to be written was all-zero, we may have 3580168404Spjd * compressed it away. In this case no write was performed 3581219089Spjd * so there will be no dva/birth/checksum. The buffer must 3582219089Spjd * therefore remain anonymous (and uncached). 3583168404Spjd */ 3584168404Spjd if (!BUF_EMPTY(hdr)) { 3585168404Spjd arc_buf_hdr_t *exists; 3586168404Spjd kmutex_t *hash_lock; 3587168404Spjd 3588219089Spjd ASSERT(zio->io_error == 0); 3589219089Spjd 3590168404Spjd arc_cksum_verify(buf); 3591168404Spjd 3592168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 3593168404Spjd if (exists) { 3594168404Spjd /* 3595168404Spjd * This can only happen if we overwrite for 3596168404Spjd * sync-to-convergence, because we remove 3597168404Spjd * buffers from the hash table when we arc_free(). 3598168404Spjd */ 3599219089Spjd if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 3600219089Spjd if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 3601219089Spjd panic("bad overwrite, hdr=%p exists=%p", 3602219089Spjd (void *)hdr, (void *)exists); 3603219089Spjd ASSERT(refcount_is_zero(&exists->b_refcnt)); 3604219089Spjd arc_change_state(arc_anon, exists, hash_lock); 3605219089Spjd mutex_exit(hash_lock); 3606219089Spjd arc_hdr_destroy(exists); 3607219089Spjd exists = buf_hash_insert(hdr, &hash_lock); 3608219089Spjd ASSERT3P(exists, ==, NULL); 3609219089Spjd } else { 3610219089Spjd /* Dedup */ 3611219089Spjd ASSERT(hdr->b_datacnt == 1); 3612219089Spjd ASSERT(hdr->b_state == arc_anon); 3613219089Spjd ASSERT(BP_GET_DEDUP(zio->io_bp)); 3614219089Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 3615219089Spjd } 3616168404Spjd } 3617168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3618185029Spjd /* if it's not anon, we are doing a scrub */ 3619219089Spjd if (!exists && hdr->b_state == arc_anon) 3620185029Spjd arc_access(hdr, hash_lock); 3621168404Spjd mutex_exit(hash_lock); 3622168404Spjd } else { 3623168404Spjd hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 3624168404Spjd } 3625168404Spjd 3626219089Spjd ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 3627219089Spjd callback->awcb_done(zio, buf, callback->awcb_private); 3628168404Spjd 3629168404Spjd kmem_free(callback, sizeof (arc_write_callback_t)); 3630168404Spjd} 3631168404Spjd 3632168404Spjdzio_t * 3633219089Spjdarc_write(zio_t *pio, spa_t *spa, uint64_t txg, 3634219089Spjd blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp, 3635219089Spjd arc_done_func_t *ready, arc_done_func_t *done, void *private, 3636219089Spjd int priority, int zio_flags, const zbookmark_t *zb) 3637168404Spjd{ 3638168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3639168404Spjd arc_write_callback_t *callback; 3640185029Spjd zio_t *zio; 3641168404Spjd 3642185029Spjd ASSERT(ready != NULL); 3643219089Spjd ASSERT(done != NULL); 3644168404Spjd ASSERT(!HDR_IO_ERROR(hdr)); 3645168404Spjd ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0); 3646219089Spjd ASSERT(hdr->b_acb == NULL); 3647185029Spjd if (l2arc) 3648185029Spjd hdr->b_flags |= ARC_L2CACHE; 3649168404Spjd callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 3650168404Spjd callback->awcb_ready = ready; 3651168404Spjd callback->awcb_done = done; 3652168404Spjd callback->awcb_private = private; 3653168404Spjd callback->awcb_buf = buf; 3654168404Spjd 3655219089Spjd zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 3656185029Spjd arc_write_ready, arc_write_done, callback, priority, zio_flags, zb); 3657185029Spjd 3658168404Spjd return (zio); 3659168404Spjd} 3660168404Spjd 3661185029Spjdstatic int 3662209962Smmarc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg) 3663185029Spjd{ 3664185029Spjd#ifdef _KERNEL 3665219089Spjd uint64_t available_memory = 3666219089Spjd ptoa((uintmax_t)cnt.v_free_count + cnt.v_cache_count); 3667185029Spjd static uint64_t page_load = 0; 3668185029Spjd static uint64_t last_txg = 0; 3669185029Spjd 3670219089Spjd#ifdef sun 3671185029Spjd#if defined(__i386) 3672185029Spjd available_memory = 3673185029Spjd MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); 3674185029Spjd#endif 3675219089Spjd#endif /* sun */ 3676185029Spjd if (available_memory >= zfs_write_limit_max) 3677185029Spjd return (0); 3678185029Spjd 3679185029Spjd if (txg > last_txg) { 3680185029Spjd last_txg = txg; 3681185029Spjd page_load = 0; 3682185029Spjd } 3683185029Spjd /* 3684185029Spjd * If we are in pageout, we know that memory is already tight, 3685185029Spjd * the arc is already going to be evicting, so we just want to 3686185029Spjd * continue to let page writes occur as quickly as possible. 3687185029Spjd */ 3688185029Spjd if (curproc == pageproc) { 3689185029Spjd if (page_load > available_memory / 4) 3690185029Spjd return (ERESTART); 3691185029Spjd /* Note: reserve is inflated, so we deflate */ 3692185029Spjd page_load += reserve / 8; 3693185029Spjd return (0); 3694185029Spjd } else if (page_load > 0 && arc_reclaim_needed()) { 3695185029Spjd /* memory is low, delay before restarting */ 3696185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 3697185029Spjd return (EAGAIN); 3698185029Spjd } 3699185029Spjd page_load = 0; 3700185029Spjd 3701185029Spjd if (arc_size > arc_c_min) { 3702185029Spjd uint64_t evictable_memory = 3703185029Spjd arc_mru->arcs_lsize[ARC_BUFC_DATA] + 3704185029Spjd arc_mru->arcs_lsize[ARC_BUFC_METADATA] + 3705185029Spjd arc_mfu->arcs_lsize[ARC_BUFC_DATA] + 3706185029Spjd arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; 3707185029Spjd available_memory += MIN(evictable_memory, arc_size - arc_c_min); 3708185029Spjd } 3709185029Spjd 3710185029Spjd if (inflight_data > available_memory / 4) { 3711185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 3712185029Spjd return (ERESTART); 3713185029Spjd } 3714185029Spjd#endif 3715185029Spjd return (0); 3716185029Spjd} 3717185029Spjd 3718168404Spjdvoid 3719185029Spjdarc_tempreserve_clear(uint64_t reserve) 3720168404Spjd{ 3721185029Spjd atomic_add_64(&arc_tempreserve, -reserve); 3722168404Spjd ASSERT((int64_t)arc_tempreserve >= 0); 3723168404Spjd} 3724168404Spjd 3725168404Spjdint 3726185029Spjdarc_tempreserve_space(uint64_t reserve, uint64_t txg) 3727168404Spjd{ 3728185029Spjd int error; 3729209962Smm uint64_t anon_size; 3730185029Spjd 3731168404Spjd#ifdef ZFS_DEBUG 3732168404Spjd /* 3733168404Spjd * Once in a while, fail for no reason. Everything should cope. 3734168404Spjd */ 3735168404Spjd if (spa_get_random(10000) == 0) { 3736168404Spjd dprintf("forcing random failure\n"); 3737168404Spjd return (ERESTART); 3738168404Spjd } 3739168404Spjd#endif 3740185029Spjd if (reserve > arc_c/4 && !arc_no_grow) 3741185029Spjd arc_c = MIN(arc_c_max, reserve * 4); 3742185029Spjd if (reserve > arc_c) 3743168404Spjd return (ENOMEM); 3744168404Spjd 3745168404Spjd /* 3746209962Smm * Don't count loaned bufs as in flight dirty data to prevent long 3747209962Smm * network delays from blocking transactions that are ready to be 3748209962Smm * assigned to a txg. 3749209962Smm */ 3750209962Smm anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); 3751209962Smm 3752209962Smm /* 3753185029Spjd * Writes will, almost always, require additional memory allocations 3754185029Spjd * in order to compress/encrypt/etc the data. We therefor need to 3755185029Spjd * make sure that there is sufficient available memory for this. 3756185029Spjd */ 3757209962Smm if (error = arc_memory_throttle(reserve, anon_size, txg)) 3758185029Spjd return (error); 3759185029Spjd 3760185029Spjd /* 3761168404Spjd * Throttle writes when the amount of dirty data in the cache 3762168404Spjd * gets too large. We try to keep the cache less than half full 3763168404Spjd * of dirty blocks so that our sync times don't grow too large. 3764168404Spjd * Note: if two requests come in concurrently, we might let them 3765168404Spjd * both succeed, when one of them should fail. Not a huge deal. 3766168404Spjd */ 3767209962Smm 3768209962Smm if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 3769209962Smm anon_size > arc_c / 4) { 3770185029Spjd dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 3771185029Spjd "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 3772185029Spjd arc_tempreserve>>10, 3773185029Spjd arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 3774185029Spjd arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 3775185029Spjd reserve>>10, arc_c>>10); 3776168404Spjd return (ERESTART); 3777168404Spjd } 3778185029Spjd atomic_add_64(&arc_tempreserve, reserve); 3779168404Spjd return (0); 3780168404Spjd} 3781168404Spjd 3782168582Spjdstatic kmutex_t arc_lowmem_lock; 3783168404Spjd#ifdef _KERNEL 3784168566Spjdstatic eventhandler_tag arc_event_lowmem = NULL; 3785168404Spjd 3786168404Spjdstatic void 3787168566Spjdarc_lowmem(void *arg __unused, int howto __unused) 3788168404Spjd{ 3789168404Spjd 3790168566Spjd /* Serialize access via arc_lowmem_lock. */ 3791168566Spjd mutex_enter(&arc_lowmem_lock); 3792219089Spjd mutex_enter(&arc_reclaim_thr_lock); 3793185029Spjd needfree = 1; 3794168404Spjd cv_signal(&arc_reclaim_thr_cv); 3795185029Spjd while (needfree) 3796219089Spjd msleep(&needfree, &arc_reclaim_thr_lock, 0, "zfs:lowmem", 0); 3797219089Spjd mutex_exit(&arc_reclaim_thr_lock); 3798168566Spjd mutex_exit(&arc_lowmem_lock); 3799168404Spjd} 3800168404Spjd#endif 3801168404Spjd 3802168404Spjdvoid 3803168404Spjdarc_init(void) 3804168404Spjd{ 3805219089Spjd int i, prefetch_tunable_set = 0; 3806205231Skmacy 3807168404Spjd mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 3808168404Spjd cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 3809168566Spjd mutex_init(&arc_lowmem_lock, NULL, MUTEX_DEFAULT, NULL); 3810168404Spjd 3811168404Spjd /* Convert seconds to clock ticks */ 3812168404Spjd arc_min_prefetch_lifespan = 1 * hz; 3813168404Spjd 3814168404Spjd /* Start out with 1/8 of all memory */ 3815168566Spjd arc_c = kmem_size() / 8; 3816219089Spjd 3817219089Spjd#ifdef sun 3818192360Skmacy#ifdef _KERNEL 3819192360Skmacy /* 3820192360Skmacy * On architectures where the physical memory can be larger 3821192360Skmacy * than the addressable space (intel in 32-bit mode), we may 3822192360Skmacy * need to limit the cache to 1/8 of VM size. 3823192360Skmacy */ 3824192360Skmacy arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 3825192360Skmacy#endif 3826219089Spjd#endif /* sun */ 3827168566Spjd /* set min cache to 1/32 of all memory, or 16MB, whichever is more */ 3828168566Spjd arc_c_min = MAX(arc_c / 4, 64<<18); 3829168566Spjd /* set max to 1/2 of all memory, or all but 1GB, whichever is more */ 3830168404Spjd if (arc_c * 8 >= 1<<30) 3831168404Spjd arc_c_max = (arc_c * 8) - (1<<30); 3832168404Spjd else 3833168404Spjd arc_c_max = arc_c_min; 3834175633Spjd arc_c_max = MAX(arc_c * 5, arc_c_max); 3835219089Spjd 3836168481Spjd#ifdef _KERNEL 3837168404Spjd /* 3838168404Spjd * Allow the tunables to override our calculations if they are 3839168566Spjd * reasonable (ie. over 16MB) 3840168404Spjd */ 3841219089Spjd if (zfs_arc_max > 64<<18 && zfs_arc_max < kmem_size()) 3842168404Spjd arc_c_max = zfs_arc_max; 3843219089Spjd if (zfs_arc_min > 64<<18 && zfs_arc_min <= arc_c_max) 3844168404Spjd arc_c_min = zfs_arc_min; 3845168481Spjd#endif 3846219089Spjd 3847168404Spjd arc_c = arc_c_max; 3848168404Spjd arc_p = (arc_c >> 1); 3849168404Spjd 3850185029Spjd /* limit meta-data to 1/4 of the arc capacity */ 3851185029Spjd arc_meta_limit = arc_c_max / 4; 3852185029Spjd 3853185029Spjd /* Allow the tunable to override if it is reasonable */ 3854185029Spjd if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 3855185029Spjd arc_meta_limit = zfs_arc_meta_limit; 3856185029Spjd 3857185029Spjd if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 3858185029Spjd arc_c_min = arc_meta_limit / 2; 3859185029Spjd 3860208373Smm if (zfs_arc_grow_retry > 0) 3861208373Smm arc_grow_retry = zfs_arc_grow_retry; 3862208373Smm 3863208373Smm if (zfs_arc_shrink_shift > 0) 3864208373Smm arc_shrink_shift = zfs_arc_shrink_shift; 3865208373Smm 3866208373Smm if (zfs_arc_p_min_shift > 0) 3867208373Smm arc_p_min_shift = zfs_arc_p_min_shift; 3868208373Smm 3869168404Spjd /* if kmem_flags are set, lets try to use less memory */ 3870168404Spjd if (kmem_debugging()) 3871168404Spjd arc_c = arc_c / 2; 3872168404Spjd if (arc_c < arc_c_min) 3873168404Spjd arc_c = arc_c_min; 3874168404Spjd 3875168473Spjd zfs_arc_min = arc_c_min; 3876168473Spjd zfs_arc_max = arc_c_max; 3877168473Spjd 3878168404Spjd arc_anon = &ARC_anon; 3879168404Spjd arc_mru = &ARC_mru; 3880168404Spjd arc_mru_ghost = &ARC_mru_ghost; 3881168404Spjd arc_mfu = &ARC_mfu; 3882168404Spjd arc_mfu_ghost = &ARC_mfu_ghost; 3883185029Spjd arc_l2c_only = &ARC_l2c_only; 3884168404Spjd arc_size = 0; 3885168404Spjd 3886205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 3887205231Skmacy mutex_init(&arc_anon->arcs_locks[i].arcs_lock, 3888205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3889205231Skmacy mutex_init(&arc_mru->arcs_locks[i].arcs_lock, 3890205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3891205231Skmacy mutex_init(&arc_mru_ghost->arcs_locks[i].arcs_lock, 3892205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3893205231Skmacy mutex_init(&arc_mfu->arcs_locks[i].arcs_lock, 3894205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3895205231Skmacy mutex_init(&arc_mfu_ghost->arcs_locks[i].arcs_lock, 3896205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3897205231Skmacy mutex_init(&arc_l2c_only->arcs_locks[i].arcs_lock, 3898205231Skmacy NULL, MUTEX_DEFAULT, NULL); 3899206796Spjd 3900205231Skmacy list_create(&arc_mru->arcs_lists[i], 3901205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3902205231Skmacy list_create(&arc_mru_ghost->arcs_lists[i], 3903205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3904205231Skmacy list_create(&arc_mfu->arcs_lists[i], 3905205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3906205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 3907205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3908205231Skmacy list_create(&arc_mfu_ghost->arcs_lists[i], 3909205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3910205231Skmacy list_create(&arc_l2c_only->arcs_lists[i], 3911205231Skmacy sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node)); 3912205231Skmacy } 3913168404Spjd 3914168404Spjd buf_init(); 3915168404Spjd 3916168404Spjd arc_thread_exit = 0; 3917168404Spjd arc_eviction_list = NULL; 3918168404Spjd mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 3919168404Spjd bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 3920168404Spjd 3921168404Spjd arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 3922168404Spjd sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 3923168404Spjd 3924168404Spjd if (arc_ksp != NULL) { 3925168404Spjd arc_ksp->ks_data = &arc_stats; 3926168404Spjd kstat_install(arc_ksp); 3927168404Spjd } 3928168404Spjd 3929168404Spjd (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 3930168404Spjd TS_RUN, minclsyspri); 3931168404Spjd 3932168404Spjd#ifdef _KERNEL 3933168566Spjd arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 3934168404Spjd EVENTHANDLER_PRI_FIRST); 3935168404Spjd#endif 3936168404Spjd 3937168404Spjd arc_dead = FALSE; 3938185029Spjd arc_warm = B_FALSE; 3939168566Spjd 3940185029Spjd if (zfs_write_limit_max == 0) 3941185029Spjd zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 3942185029Spjd else 3943185029Spjd zfs_write_limit_shift = 0; 3944185029Spjd mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL); 3945185029Spjd 3946168566Spjd#ifdef _KERNEL 3947194043Skmacy if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 3948193953Skmacy prefetch_tunable_set = 1; 3949206796Spjd 3950193878Skmacy#ifdef __i386__ 3951193953Skmacy if (prefetch_tunable_set == 0) { 3952196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 3953196863Strasz "-- to enable,\n"); 3954196863Strasz printf(" add \"vfs.zfs.prefetch_disable=0\" " 3955196863Strasz "to /boot/loader.conf.\n"); 3956219089Spjd zfs_prefetch_disable = 1; 3957193878Skmacy } 3958206796Spjd#else 3959193878Skmacy if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 3960193953Skmacy prefetch_tunable_set == 0) { 3961196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default if less " 3962196941Strasz "than 4GB of RAM is present;\n" 3963196863Strasz " to enable, add \"vfs.zfs.prefetch_disable=0\" " 3964196863Strasz "to /boot/loader.conf.\n"); 3965219089Spjd zfs_prefetch_disable = 1; 3966193878Skmacy } 3967206796Spjd#endif 3968175633Spjd /* Warn about ZFS memory and address space requirements. */ 3969168696Spjd if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 3970168987Sbmah printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 3971168987Sbmah "expect unstable behavior.\n"); 3972175633Spjd } 3973175633Spjd if (kmem_size() < 512 * (1 << 20)) { 3974173419Spjd printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 3975168987Sbmah "expect unstable behavior.\n"); 3976185029Spjd printf(" Consider tuning vm.kmem_size and " 3977173419Spjd "vm.kmem_size_max\n"); 3978185029Spjd printf(" in /boot/loader.conf.\n"); 3979168566Spjd } 3980168566Spjd#endif 3981168404Spjd} 3982168404Spjd 3983168404Spjdvoid 3984168404Spjdarc_fini(void) 3985168404Spjd{ 3986205231Skmacy int i; 3987206796Spjd 3988168404Spjd mutex_enter(&arc_reclaim_thr_lock); 3989168404Spjd arc_thread_exit = 1; 3990168404Spjd cv_signal(&arc_reclaim_thr_cv); 3991168404Spjd while (arc_thread_exit != 0) 3992168404Spjd cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 3993168404Spjd mutex_exit(&arc_reclaim_thr_lock); 3994168404Spjd 3995185029Spjd arc_flush(NULL); 3996168404Spjd 3997168404Spjd arc_dead = TRUE; 3998168404Spjd 3999168404Spjd if (arc_ksp != NULL) { 4000168404Spjd kstat_delete(arc_ksp); 4001168404Spjd arc_ksp = NULL; 4002168404Spjd } 4003168404Spjd 4004168404Spjd mutex_destroy(&arc_eviction_mtx); 4005168404Spjd mutex_destroy(&arc_reclaim_thr_lock); 4006168404Spjd cv_destroy(&arc_reclaim_thr_cv); 4007168404Spjd 4008205231Skmacy for (i = 0; i < ARC_BUFC_NUMLISTS; i++) { 4009205231Skmacy list_destroy(&arc_mru->arcs_lists[i]); 4010205231Skmacy list_destroy(&arc_mru_ghost->arcs_lists[i]); 4011205231Skmacy list_destroy(&arc_mfu->arcs_lists[i]); 4012205231Skmacy list_destroy(&arc_mfu_ghost->arcs_lists[i]); 4013206795Spjd list_destroy(&arc_l2c_only->arcs_lists[i]); 4014168404Spjd 4015205231Skmacy mutex_destroy(&arc_anon->arcs_locks[i].arcs_lock); 4016205231Skmacy mutex_destroy(&arc_mru->arcs_locks[i].arcs_lock); 4017205231Skmacy mutex_destroy(&arc_mru_ghost->arcs_locks[i].arcs_lock); 4018205231Skmacy mutex_destroy(&arc_mfu->arcs_locks[i].arcs_lock); 4019205231Skmacy mutex_destroy(&arc_mfu_ghost->arcs_locks[i].arcs_lock); 4020206795Spjd mutex_destroy(&arc_l2c_only->arcs_locks[i].arcs_lock); 4021205231Skmacy } 4022206796Spjd 4023185029Spjd mutex_destroy(&zfs_write_limit_lock); 4024185029Spjd 4025168404Spjd buf_fini(); 4026168404Spjd 4027209962Smm ASSERT(arc_loaned_bytes == 0); 4028209962Smm 4029168582Spjd mutex_destroy(&arc_lowmem_lock); 4030168404Spjd#ifdef _KERNEL 4031168566Spjd if (arc_event_lowmem != NULL) 4032168566Spjd EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 4033168404Spjd#endif 4034168404Spjd} 4035185029Spjd 4036185029Spjd/* 4037185029Spjd * Level 2 ARC 4038185029Spjd * 4039185029Spjd * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 4040185029Spjd * It uses dedicated storage devices to hold cached data, which are populated 4041185029Spjd * using large infrequent writes. The main role of this cache is to boost 4042185029Spjd * the performance of random read workloads. The intended L2ARC devices 4043185029Spjd * include short-stroked disks, solid state disks, and other media with 4044185029Spjd * substantially faster read latency than disk. 4045185029Spjd * 4046185029Spjd * +-----------------------+ 4047185029Spjd * | ARC | 4048185029Spjd * +-----------------------+ 4049185029Spjd * | ^ ^ 4050185029Spjd * | | | 4051185029Spjd * l2arc_feed_thread() arc_read() 4052185029Spjd * | | | 4053185029Spjd * | l2arc read | 4054185029Spjd * V | | 4055185029Spjd * +---------------+ | 4056185029Spjd * | L2ARC | | 4057185029Spjd * +---------------+ | 4058185029Spjd * | ^ | 4059185029Spjd * l2arc_write() | | 4060185029Spjd * | | | 4061185029Spjd * V | | 4062185029Spjd * +-------+ +-------+ 4063185029Spjd * | vdev | | vdev | 4064185029Spjd * | cache | | cache | 4065185029Spjd * +-------+ +-------+ 4066185029Spjd * +=========+ .-----. 4067185029Spjd * : L2ARC : |-_____-| 4068185029Spjd * : devices : | Disks | 4069185029Spjd * +=========+ `-_____-' 4070185029Spjd * 4071185029Spjd * Read requests are satisfied from the following sources, in order: 4072185029Spjd * 4073185029Spjd * 1) ARC 4074185029Spjd * 2) vdev cache of L2ARC devices 4075185029Spjd * 3) L2ARC devices 4076185029Spjd * 4) vdev cache of disks 4077185029Spjd * 5) disks 4078185029Spjd * 4079185029Spjd * Some L2ARC device types exhibit extremely slow write performance. 4080185029Spjd * To accommodate for this there are some significant differences between 4081185029Spjd * the L2ARC and traditional cache design: 4082185029Spjd * 4083185029Spjd * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 4084185029Spjd * the ARC behave as usual, freeing buffers and placing headers on ghost 4085185029Spjd * lists. The ARC does not send buffers to the L2ARC during eviction as 4086185029Spjd * this would add inflated write latencies for all ARC memory pressure. 4087185029Spjd * 4088185029Spjd * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 4089185029Spjd * It does this by periodically scanning buffers from the eviction-end of 4090185029Spjd * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 4091185029Spjd * not already there. It scans until a headroom of buffers is satisfied, 4092185029Spjd * which itself is a buffer for ARC eviction. The thread that does this is 4093185029Spjd * l2arc_feed_thread(), illustrated below; example sizes are included to 4094185029Spjd * provide a better sense of ratio than this diagram: 4095185029Spjd * 4096185029Spjd * head --> tail 4097185029Spjd * +---------------------+----------+ 4098185029Spjd * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 4099185029Spjd * +---------------------+----------+ | o L2ARC eligible 4100185029Spjd * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 4101185029Spjd * +---------------------+----------+ | 4102185029Spjd * 15.9 Gbytes ^ 32 Mbytes | 4103185029Spjd * headroom | 4104185029Spjd * l2arc_feed_thread() 4105185029Spjd * | 4106185029Spjd * l2arc write hand <--[oooo]--' 4107185029Spjd * | 8 Mbyte 4108185029Spjd * | write max 4109185029Spjd * V 4110185029Spjd * +==============================+ 4111185029Spjd * L2ARC dev |####|#|###|###| |####| ... | 4112185029Spjd * +==============================+ 4113185029Spjd * 32 Gbytes 4114185029Spjd * 4115185029Spjd * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 4116185029Spjd * evicted, then the L2ARC has cached a buffer much sooner than it probably 4117185029Spjd * needed to, potentially wasting L2ARC device bandwidth and storage. It is 4118185029Spjd * safe to say that this is an uncommon case, since buffers at the end of 4119185029Spjd * the ARC lists have moved there due to inactivity. 4120185029Spjd * 4121185029Spjd * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 4122185029Spjd * then the L2ARC simply misses copying some buffers. This serves as a 4123185029Spjd * pressure valve to prevent heavy read workloads from both stalling the ARC 4124185029Spjd * with waits and clogging the L2ARC with writes. This also helps prevent 4125185029Spjd * the potential for the L2ARC to churn if it attempts to cache content too 4126185029Spjd * quickly, such as during backups of the entire pool. 4127185029Spjd * 4128185029Spjd * 5. After system boot and before the ARC has filled main memory, there are 4129185029Spjd * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 4130185029Spjd * lists can remain mostly static. Instead of searching from tail of these 4131185029Spjd * lists as pictured, the l2arc_feed_thread() will search from the list heads 4132185029Spjd * for eligible buffers, greatly increasing its chance of finding them. 4133185029Spjd * 4134185029Spjd * The L2ARC device write speed is also boosted during this time so that 4135185029Spjd * the L2ARC warms up faster. Since there have been no ARC evictions yet, 4136185029Spjd * there are no L2ARC reads, and no fear of degrading read performance 4137185029Spjd * through increased writes. 4138185029Spjd * 4139185029Spjd * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 4140185029Spjd * the vdev queue can aggregate them into larger and fewer writes. Each 4141185029Spjd * device is written to in a rotor fashion, sweeping writes through 4142185029Spjd * available space then repeating. 4143185029Spjd * 4144185029Spjd * 7. The L2ARC does not store dirty content. It never needs to flush 4145185029Spjd * write buffers back to disk based storage. 4146185029Spjd * 4147185029Spjd * 8. If an ARC buffer is written (and dirtied) which also exists in the 4148185029Spjd * L2ARC, the now stale L2ARC buffer is immediately dropped. 4149185029Spjd * 4150185029Spjd * The performance of the L2ARC can be tweaked by a number of tunables, which 4151185029Spjd * may be necessary for different workloads: 4152185029Spjd * 4153185029Spjd * l2arc_write_max max write bytes per interval 4154185029Spjd * l2arc_write_boost extra write bytes during device warmup 4155185029Spjd * l2arc_noprefetch skip caching prefetched buffers 4156185029Spjd * l2arc_headroom number of max device writes to precache 4157185029Spjd * l2arc_feed_secs seconds between L2ARC writing 4158185029Spjd * 4159185029Spjd * Tunables may be removed or added as future performance improvements are 4160185029Spjd * integrated, and also may become zpool properties. 4161208373Smm * 4162208373Smm * There are three key functions that control how the L2ARC warms up: 4163208373Smm * 4164208373Smm * l2arc_write_eligible() check if a buffer is eligible to cache 4165208373Smm * l2arc_write_size() calculate how much to write 4166208373Smm * l2arc_write_interval() calculate sleep delay between writes 4167208373Smm * 4168208373Smm * These three functions determine what to write, how much, and how quickly 4169208373Smm * to send writes. 4170185029Spjd */ 4171185029Spjd 4172208373Smmstatic boolean_t 4173209962Smml2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab) 4174208373Smm{ 4175208373Smm /* 4176208373Smm * A buffer is *not* eligible for the L2ARC if it: 4177208373Smm * 1. belongs to a different spa. 4178208373Smm * 2. is already cached on the L2ARC. 4179208373Smm * 3. has an I/O in progress (it may be an incomplete read). 4180208373Smm * 4. is flagged not eligible (zfs property). 4181208373Smm */ 4182209962Smm if (ab->b_spa != spa_guid) { 4183208373Smm ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 4184208373Smm return (B_FALSE); 4185208373Smm } 4186208373Smm if (ab->b_l2hdr != NULL) { 4187208373Smm ARCSTAT_BUMP(arcstat_l2_write_in_l2); 4188208373Smm return (B_FALSE); 4189208373Smm } 4190208373Smm if (HDR_IO_IN_PROGRESS(ab)) { 4191208373Smm ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 4192208373Smm return (B_FALSE); 4193208373Smm } 4194208373Smm if (!HDR_L2CACHE(ab)) { 4195208373Smm ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 4196208373Smm return (B_FALSE); 4197208373Smm } 4198208373Smm 4199208373Smm return (B_TRUE); 4200208373Smm} 4201208373Smm 4202208373Smmstatic uint64_t 4203208373Smml2arc_write_size(l2arc_dev_t *dev) 4204208373Smm{ 4205208373Smm uint64_t size; 4206208373Smm 4207208373Smm size = dev->l2ad_write; 4208208373Smm 4209208373Smm if (arc_warm == B_FALSE) 4210208373Smm size += dev->l2ad_boost; 4211208373Smm 4212208373Smm return (size); 4213208373Smm 4214208373Smm} 4215208373Smm 4216208373Smmstatic clock_t 4217208373Smml2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 4218208373Smm{ 4219219089Spjd clock_t interval, next, now; 4220208373Smm 4221208373Smm /* 4222208373Smm * If the ARC lists are busy, increase our write rate; if the 4223208373Smm * lists are stale, idle back. This is achieved by checking 4224208373Smm * how much we previously wrote - if it was more than half of 4225208373Smm * what we wanted, schedule the next write much sooner. 4226208373Smm */ 4227208373Smm if (l2arc_feed_again && wrote > (wanted / 2)) 4228208373Smm interval = (hz * l2arc_feed_min_ms) / 1000; 4229208373Smm else 4230208373Smm interval = hz * l2arc_feed_secs; 4231208373Smm 4232219089Spjd now = ddi_get_lbolt(); 4233219089Spjd next = MAX(now, MIN(now + interval, began + interval)); 4234208373Smm 4235208373Smm return (next); 4236208373Smm} 4237208373Smm 4238185029Spjdstatic void 4239185029Spjdl2arc_hdr_stat_add(void) 4240185029Spjd{ 4241185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE); 4242185029Spjd ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE); 4243185029Spjd} 4244185029Spjd 4245185029Spjdstatic void 4246185029Spjdl2arc_hdr_stat_remove(void) 4247185029Spjd{ 4248185029Spjd ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE)); 4249185029Spjd ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE); 4250185029Spjd} 4251185029Spjd 4252185029Spjd/* 4253185029Spjd * Cycle through L2ARC devices. This is how L2ARC load balances. 4254185029Spjd * If a device is returned, this also returns holding the spa config lock. 4255185029Spjd */ 4256185029Spjdstatic l2arc_dev_t * 4257185029Spjdl2arc_dev_get_next(void) 4258185029Spjd{ 4259185029Spjd l2arc_dev_t *first, *next = NULL; 4260185029Spjd 4261185029Spjd /* 4262185029Spjd * Lock out the removal of spas (spa_namespace_lock), then removal 4263185029Spjd * of cache devices (l2arc_dev_mtx). Once a device has been selected, 4264185029Spjd * both locks will be dropped and a spa config lock held instead. 4265185029Spjd */ 4266185029Spjd mutex_enter(&spa_namespace_lock); 4267185029Spjd mutex_enter(&l2arc_dev_mtx); 4268185029Spjd 4269185029Spjd /* if there are no vdevs, there is nothing to do */ 4270185029Spjd if (l2arc_ndev == 0) 4271185029Spjd goto out; 4272185029Spjd 4273185029Spjd first = NULL; 4274185029Spjd next = l2arc_dev_last; 4275185029Spjd do { 4276185029Spjd /* loop around the list looking for a non-faulted vdev */ 4277185029Spjd if (next == NULL) { 4278185029Spjd next = list_head(l2arc_dev_list); 4279185029Spjd } else { 4280185029Spjd next = list_next(l2arc_dev_list, next); 4281185029Spjd if (next == NULL) 4282185029Spjd next = list_head(l2arc_dev_list); 4283185029Spjd } 4284185029Spjd 4285185029Spjd /* if we have come back to the start, bail out */ 4286185029Spjd if (first == NULL) 4287185029Spjd first = next; 4288185029Spjd else if (next == first) 4289185029Spjd break; 4290185029Spjd 4291185029Spjd } while (vdev_is_dead(next->l2ad_vdev)); 4292185029Spjd 4293185029Spjd /* if we were unable to find any usable vdevs, return NULL */ 4294185029Spjd if (vdev_is_dead(next->l2ad_vdev)) 4295185029Spjd next = NULL; 4296185029Spjd 4297185029Spjd l2arc_dev_last = next; 4298185029Spjd 4299185029Spjdout: 4300185029Spjd mutex_exit(&l2arc_dev_mtx); 4301185029Spjd 4302185029Spjd /* 4303185029Spjd * Grab the config lock to prevent the 'next' device from being 4304185029Spjd * removed while we are writing to it. 4305185029Spjd */ 4306185029Spjd if (next != NULL) 4307185029Spjd spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 4308185029Spjd mutex_exit(&spa_namespace_lock); 4309185029Spjd 4310185029Spjd return (next); 4311185029Spjd} 4312185029Spjd 4313185029Spjd/* 4314185029Spjd * Free buffers that were tagged for destruction. 4315185029Spjd */ 4316185029Spjdstatic void 4317185029Spjdl2arc_do_free_on_write() 4318185029Spjd{ 4319185029Spjd list_t *buflist; 4320185029Spjd l2arc_data_free_t *df, *df_prev; 4321185029Spjd 4322185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 4323185029Spjd buflist = l2arc_free_on_write; 4324185029Spjd 4325185029Spjd for (df = list_tail(buflist); df; df = df_prev) { 4326185029Spjd df_prev = list_prev(buflist, df); 4327185029Spjd ASSERT(df->l2df_data != NULL); 4328185029Spjd ASSERT(df->l2df_func != NULL); 4329185029Spjd df->l2df_func(df->l2df_data, df->l2df_size); 4330185029Spjd list_remove(buflist, df); 4331185029Spjd kmem_free(df, sizeof (l2arc_data_free_t)); 4332185029Spjd } 4333185029Spjd 4334185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 4335185029Spjd} 4336185029Spjd 4337185029Spjd/* 4338185029Spjd * A write to a cache device has completed. Update all headers to allow 4339185029Spjd * reads from these buffers to begin. 4340185029Spjd */ 4341185029Spjdstatic void 4342185029Spjdl2arc_write_done(zio_t *zio) 4343185029Spjd{ 4344185029Spjd l2arc_write_callback_t *cb; 4345185029Spjd l2arc_dev_t *dev; 4346185029Spjd list_t *buflist; 4347185029Spjd arc_buf_hdr_t *head, *ab, *ab_prev; 4348185029Spjd l2arc_buf_hdr_t *abl2; 4349185029Spjd kmutex_t *hash_lock; 4350185029Spjd 4351185029Spjd cb = zio->io_private; 4352185029Spjd ASSERT(cb != NULL); 4353185029Spjd dev = cb->l2wcb_dev; 4354185029Spjd ASSERT(dev != NULL); 4355185029Spjd head = cb->l2wcb_head; 4356185029Spjd ASSERT(head != NULL); 4357185029Spjd buflist = dev->l2ad_buflist; 4358185029Spjd ASSERT(buflist != NULL); 4359185029Spjd DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 4360185029Spjd l2arc_write_callback_t *, cb); 4361185029Spjd 4362185029Spjd if (zio->io_error != 0) 4363185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_error); 4364185029Spjd 4365185029Spjd mutex_enter(&l2arc_buflist_mtx); 4366185029Spjd 4367185029Spjd /* 4368185029Spjd * All writes completed, or an error was hit. 4369185029Spjd */ 4370185029Spjd for (ab = list_prev(buflist, head); ab; ab = ab_prev) { 4371185029Spjd ab_prev = list_prev(buflist, ab); 4372185029Spjd 4373185029Spjd hash_lock = HDR_LOCK(ab); 4374185029Spjd if (!mutex_tryenter(hash_lock)) { 4375185029Spjd /* 4376185029Spjd * This buffer misses out. It may be in a stage 4377185029Spjd * of eviction. Its ARC_L2_WRITING flag will be 4378185029Spjd * left set, denying reads to this buffer. 4379185029Spjd */ 4380185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss); 4381185029Spjd continue; 4382185029Spjd } 4383185029Spjd 4384185029Spjd if (zio->io_error != 0) { 4385185029Spjd /* 4386185029Spjd * Error - drop L2ARC entry. 4387185029Spjd */ 4388185029Spjd list_remove(buflist, ab); 4389185029Spjd abl2 = ab->b_l2hdr; 4390185029Spjd ab->b_l2hdr = NULL; 4391185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4392185029Spjd ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4393185029Spjd } 4394185029Spjd 4395185029Spjd /* 4396185029Spjd * Allow ARC to begin reads to this L2ARC entry. 4397185029Spjd */ 4398185029Spjd ab->b_flags &= ~ARC_L2_WRITING; 4399185029Spjd 4400185029Spjd mutex_exit(hash_lock); 4401185029Spjd } 4402185029Spjd 4403185029Spjd atomic_inc_64(&l2arc_writes_done); 4404185029Spjd list_remove(buflist, head); 4405185029Spjd kmem_cache_free(hdr_cache, head); 4406185029Spjd mutex_exit(&l2arc_buflist_mtx); 4407185029Spjd 4408185029Spjd l2arc_do_free_on_write(); 4409185029Spjd 4410185029Spjd kmem_free(cb, sizeof (l2arc_write_callback_t)); 4411185029Spjd} 4412185029Spjd 4413185029Spjd/* 4414185029Spjd * A read to a cache device completed. Validate buffer contents before 4415185029Spjd * handing over to the regular ARC routines. 4416185029Spjd */ 4417185029Spjdstatic void 4418185029Spjdl2arc_read_done(zio_t *zio) 4419185029Spjd{ 4420185029Spjd l2arc_read_callback_t *cb; 4421185029Spjd arc_buf_hdr_t *hdr; 4422185029Spjd arc_buf_t *buf; 4423185029Spjd kmutex_t *hash_lock; 4424185029Spjd int equal; 4425185029Spjd 4426185029Spjd ASSERT(zio->io_vd != NULL); 4427185029Spjd ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 4428185029Spjd 4429185029Spjd spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 4430185029Spjd 4431185029Spjd cb = zio->io_private; 4432185029Spjd ASSERT(cb != NULL); 4433185029Spjd buf = cb->l2rcb_buf; 4434185029Spjd ASSERT(buf != NULL); 4435185029Spjd 4436219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 4437185029Spjd mutex_enter(hash_lock); 4438219089Spjd hdr = buf->b_hdr; 4439219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4440185029Spjd 4441185029Spjd /* 4442185029Spjd * Check this survived the L2ARC journey. 4443185029Spjd */ 4444185029Spjd equal = arc_cksum_equal(buf); 4445185029Spjd if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 4446185029Spjd mutex_exit(hash_lock); 4447185029Spjd zio->io_private = buf; 4448185029Spjd zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 4449185029Spjd zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 4450185029Spjd arc_read_done(zio); 4451185029Spjd } else { 4452185029Spjd mutex_exit(hash_lock); 4453185029Spjd /* 4454185029Spjd * Buffer didn't survive caching. Increment stats and 4455185029Spjd * reissue to the original storage device. 4456185029Spjd */ 4457185029Spjd if (zio->io_error != 0) { 4458185029Spjd ARCSTAT_BUMP(arcstat_l2_io_error); 4459185029Spjd } else { 4460185029Spjd zio->io_error = EIO; 4461185029Spjd } 4462185029Spjd if (!equal) 4463185029Spjd ARCSTAT_BUMP(arcstat_l2_cksum_bad); 4464185029Spjd 4465185029Spjd /* 4466185029Spjd * If there's no waiter, issue an async i/o to the primary 4467185029Spjd * storage now. If there *is* a waiter, the caller must 4468185029Spjd * issue the i/o in a context where it's OK to block. 4469185029Spjd */ 4470209962Smm if (zio->io_waiter == NULL) { 4471209962Smm zio_t *pio = zio_unique_parent(zio); 4472209962Smm 4473209962Smm ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 4474209962Smm 4475209962Smm zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 4476185029Spjd buf->b_data, zio->io_size, arc_read_done, buf, 4477185029Spjd zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 4478209962Smm } 4479185029Spjd } 4480185029Spjd 4481185029Spjd kmem_free(cb, sizeof (l2arc_read_callback_t)); 4482185029Spjd} 4483185029Spjd 4484185029Spjd/* 4485185029Spjd * This is the list priority from which the L2ARC will search for pages to 4486185029Spjd * cache. This is used within loops (0..3) to cycle through lists in the 4487185029Spjd * desired order. This order can have a significant effect on cache 4488185029Spjd * performance. 4489185029Spjd * 4490185029Spjd * Currently the metadata lists are hit first, MFU then MRU, followed by 4491185029Spjd * the data lists. This function returns a locked list, and also returns 4492185029Spjd * the lock pointer. 4493185029Spjd */ 4494185029Spjdstatic list_t * 4495185029Spjdl2arc_list_locked(int list_num, kmutex_t **lock) 4496185029Spjd{ 4497185029Spjd list_t *list; 4498205231Skmacy int idx; 4499185029Spjd 4500206796Spjd ASSERT(list_num >= 0 && list_num < 2 * ARC_BUFC_NUMLISTS); 4501206796Spjd 4502205231Skmacy if (list_num < ARC_BUFC_NUMMETADATALISTS) { 4503205231Skmacy idx = list_num; 4504205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4505205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4506206796Spjd } else if (list_num < ARC_BUFC_NUMMETADATALISTS * 2) { 4507205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4508205231Skmacy list = &arc_mru->arcs_lists[idx]; 4509205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4510206796Spjd } else if (list_num < (ARC_BUFC_NUMMETADATALISTS * 2 + 4511205231Skmacy ARC_BUFC_NUMDATALISTS)) { 4512205231Skmacy idx = list_num - ARC_BUFC_NUMMETADATALISTS; 4513205231Skmacy list = &arc_mfu->arcs_lists[idx]; 4514205231Skmacy *lock = ARCS_LOCK(arc_mfu, idx); 4515205231Skmacy } else { 4516205231Skmacy idx = list_num - ARC_BUFC_NUMLISTS; 4517205231Skmacy list = &arc_mru->arcs_lists[idx]; 4518205231Skmacy *lock = ARCS_LOCK(arc_mru, idx); 4519185029Spjd } 4520185029Spjd 4521185029Spjd ASSERT(!(MUTEX_HELD(*lock))); 4522185029Spjd mutex_enter(*lock); 4523185029Spjd return (list); 4524185029Spjd} 4525185029Spjd 4526185029Spjd/* 4527185029Spjd * Evict buffers from the device write hand to the distance specified in 4528185029Spjd * bytes. This distance may span populated buffers, it may span nothing. 4529185029Spjd * This is clearing a region on the L2ARC device ready for writing. 4530185029Spjd * If the 'all' boolean is set, every buffer is evicted. 4531185029Spjd */ 4532185029Spjdstatic void 4533185029Spjdl2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 4534185029Spjd{ 4535185029Spjd list_t *buflist; 4536185029Spjd l2arc_buf_hdr_t *abl2; 4537185029Spjd arc_buf_hdr_t *ab, *ab_prev; 4538185029Spjd kmutex_t *hash_lock; 4539185029Spjd uint64_t taddr; 4540185029Spjd 4541185029Spjd buflist = dev->l2ad_buflist; 4542185029Spjd 4543185029Spjd if (buflist == NULL) 4544185029Spjd return; 4545185029Spjd 4546185029Spjd if (!all && dev->l2ad_first) { 4547185029Spjd /* 4548185029Spjd * This is the first sweep through the device. There is 4549185029Spjd * nothing to evict. 4550185029Spjd */ 4551185029Spjd return; 4552185029Spjd } 4553185029Spjd 4554185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 4555185029Spjd /* 4556185029Spjd * When nearing the end of the device, evict to the end 4557185029Spjd * before the device write hand jumps to the start. 4558185029Spjd */ 4559185029Spjd taddr = dev->l2ad_end; 4560185029Spjd } else { 4561185029Spjd taddr = dev->l2ad_hand + distance; 4562185029Spjd } 4563185029Spjd DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 4564185029Spjd uint64_t, taddr, boolean_t, all); 4565185029Spjd 4566185029Spjdtop: 4567185029Spjd mutex_enter(&l2arc_buflist_mtx); 4568185029Spjd for (ab = list_tail(buflist); ab; ab = ab_prev) { 4569185029Spjd ab_prev = list_prev(buflist, ab); 4570185029Spjd 4571185029Spjd hash_lock = HDR_LOCK(ab); 4572185029Spjd if (!mutex_tryenter(hash_lock)) { 4573185029Spjd /* 4574185029Spjd * Missed the hash lock. Retry. 4575185029Spjd */ 4576185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 4577185029Spjd mutex_exit(&l2arc_buflist_mtx); 4578185029Spjd mutex_enter(hash_lock); 4579185029Spjd mutex_exit(hash_lock); 4580185029Spjd goto top; 4581185029Spjd } 4582185029Spjd 4583185029Spjd if (HDR_L2_WRITE_HEAD(ab)) { 4584185029Spjd /* 4585185029Spjd * We hit a write head node. Leave it for 4586185029Spjd * l2arc_write_done(). 4587185029Spjd */ 4588185029Spjd list_remove(buflist, ab); 4589185029Spjd mutex_exit(hash_lock); 4590185029Spjd continue; 4591185029Spjd } 4592185029Spjd 4593185029Spjd if (!all && ab->b_l2hdr != NULL && 4594185029Spjd (ab->b_l2hdr->b_daddr > taddr || 4595185029Spjd ab->b_l2hdr->b_daddr < dev->l2ad_hand)) { 4596185029Spjd /* 4597185029Spjd * We've evicted to the target address, 4598185029Spjd * or the end of the device. 4599185029Spjd */ 4600185029Spjd mutex_exit(hash_lock); 4601185029Spjd break; 4602185029Spjd } 4603185029Spjd 4604185029Spjd if (HDR_FREE_IN_PROGRESS(ab)) { 4605185029Spjd /* 4606185029Spjd * Already on the path to destruction. 4607185029Spjd */ 4608185029Spjd mutex_exit(hash_lock); 4609185029Spjd continue; 4610185029Spjd } 4611185029Spjd 4612185029Spjd if (ab->b_state == arc_l2c_only) { 4613185029Spjd ASSERT(!HDR_L2_READING(ab)); 4614185029Spjd /* 4615185029Spjd * This doesn't exist in the ARC. Destroy. 4616185029Spjd * arc_hdr_destroy() will call list_remove() 4617185029Spjd * and decrement arcstat_l2_size. 4618185029Spjd */ 4619185029Spjd arc_change_state(arc_anon, ab, hash_lock); 4620185029Spjd arc_hdr_destroy(ab); 4621185029Spjd } else { 4622185029Spjd /* 4623185029Spjd * Invalidate issued or about to be issued 4624185029Spjd * reads, since we may be about to write 4625185029Spjd * over this location. 4626185029Spjd */ 4627185029Spjd if (HDR_L2_READING(ab)) { 4628185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_reading); 4629185029Spjd ab->b_flags |= ARC_L2_EVICTED; 4630185029Spjd } 4631185029Spjd 4632185029Spjd /* 4633185029Spjd * Tell ARC this no longer exists in L2ARC. 4634185029Spjd */ 4635185029Spjd if (ab->b_l2hdr != NULL) { 4636185029Spjd abl2 = ab->b_l2hdr; 4637185029Spjd ab->b_l2hdr = NULL; 4638185029Spjd kmem_free(abl2, sizeof (l2arc_buf_hdr_t)); 4639185029Spjd ARCSTAT_INCR(arcstat_l2_size, -ab->b_size); 4640185029Spjd } 4641185029Spjd list_remove(buflist, ab); 4642185029Spjd 4643185029Spjd /* 4644185029Spjd * This may have been leftover after a 4645185029Spjd * failed write. 4646185029Spjd */ 4647185029Spjd ab->b_flags &= ~ARC_L2_WRITING; 4648185029Spjd } 4649185029Spjd mutex_exit(hash_lock); 4650185029Spjd } 4651185029Spjd mutex_exit(&l2arc_buflist_mtx); 4652185029Spjd 4653219089Spjd vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0); 4654185029Spjd dev->l2ad_evict = taddr; 4655185029Spjd} 4656185029Spjd 4657185029Spjd/* 4658185029Spjd * Find and write ARC buffers to the L2ARC device. 4659185029Spjd * 4660185029Spjd * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid 4661185029Spjd * for reading until they have completed writing. 4662185029Spjd */ 4663208373Smmstatic uint64_t 4664185029Spjdl2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 4665185029Spjd{ 4666185029Spjd arc_buf_hdr_t *ab, *ab_prev, *head; 4667185029Spjd l2arc_buf_hdr_t *hdrl2; 4668185029Spjd list_t *list; 4669185029Spjd uint64_t passed_sz, write_sz, buf_sz, headroom; 4670185029Spjd void *buf_data; 4671185029Spjd kmutex_t *hash_lock, *list_lock; 4672185029Spjd boolean_t have_lock, full; 4673185029Spjd l2arc_write_callback_t *cb; 4674185029Spjd zio_t *pio, *wzio; 4675228103Smm uint64_t guid = spa_load_guid(spa); 4676185029Spjd int try; 4677185029Spjd 4678185029Spjd ASSERT(dev->l2ad_vdev != NULL); 4679185029Spjd 4680185029Spjd pio = NULL; 4681185029Spjd write_sz = 0; 4682185029Spjd full = B_FALSE; 4683185029Spjd head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE); 4684185029Spjd head->b_flags |= ARC_L2_WRITE_HEAD; 4685185029Spjd 4686205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 4687185029Spjd /* 4688185029Spjd * Copy buffers for L2ARC writing. 4689185029Spjd */ 4690185029Spjd mutex_enter(&l2arc_buflist_mtx); 4691206796Spjd for (try = 0; try < 2 * ARC_BUFC_NUMLISTS; try++) { 4692185029Spjd list = l2arc_list_locked(try, &list_lock); 4693185029Spjd passed_sz = 0; 4694205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 4695185029Spjd 4696185029Spjd /* 4697185029Spjd * L2ARC fast warmup. 4698185029Spjd * 4699185029Spjd * Until the ARC is warm and starts to evict, read from the 4700185029Spjd * head of the ARC lists rather than the tail. 4701185029Spjd */ 4702185029Spjd headroom = target_sz * l2arc_headroom; 4703185029Spjd if (arc_warm == B_FALSE) 4704185029Spjd ab = list_head(list); 4705185029Spjd else 4706185029Spjd ab = list_tail(list); 4707206796Spjd if (ab == NULL) 4708205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 4709185029Spjd 4710185029Spjd for (; ab; ab = ab_prev) { 4711185029Spjd if (arc_warm == B_FALSE) 4712185029Spjd ab_prev = list_next(list, ab); 4713185029Spjd else 4714185029Spjd ab_prev = list_prev(list, ab); 4715205231Skmacy ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, ab->b_size); 4716206796Spjd 4717185029Spjd hash_lock = HDR_LOCK(ab); 4718185029Spjd have_lock = MUTEX_HELD(hash_lock); 4719185029Spjd if (!have_lock && !mutex_tryenter(hash_lock)) { 4720205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 4721185029Spjd /* 4722185029Spjd * Skip this buffer rather than waiting. 4723185029Spjd */ 4724185029Spjd continue; 4725185029Spjd } 4726185029Spjd 4727185029Spjd passed_sz += ab->b_size; 4728185029Spjd if (passed_sz > headroom) { 4729185029Spjd /* 4730185029Spjd * Searched too far. 4731185029Spjd */ 4732185029Spjd mutex_exit(hash_lock); 4733205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 4734185029Spjd break; 4735185029Spjd } 4736185029Spjd 4737209962Smm if (!l2arc_write_eligible(guid, ab)) { 4738185029Spjd mutex_exit(hash_lock); 4739185029Spjd continue; 4740185029Spjd } 4741185029Spjd 4742185029Spjd if ((write_sz + ab->b_size) > target_sz) { 4743185029Spjd full = B_TRUE; 4744185029Spjd mutex_exit(hash_lock); 4745205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_full); 4746185029Spjd break; 4747185029Spjd } 4748185029Spjd 4749185029Spjd if (pio == NULL) { 4750185029Spjd /* 4751185029Spjd * Insert a dummy header on the buflist so 4752185029Spjd * l2arc_write_done() can find where the 4753185029Spjd * write buffers begin without searching. 4754185029Spjd */ 4755185029Spjd list_insert_head(dev->l2ad_buflist, head); 4756185029Spjd 4757185029Spjd cb = kmem_alloc( 4758185029Spjd sizeof (l2arc_write_callback_t), KM_SLEEP); 4759185029Spjd cb->l2wcb_dev = dev; 4760185029Spjd cb->l2wcb_head = head; 4761185029Spjd pio = zio_root(spa, l2arc_write_done, cb, 4762185029Spjd ZIO_FLAG_CANFAIL); 4763205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_pios); 4764185029Spjd } 4765185029Spjd 4766185029Spjd /* 4767185029Spjd * Create and add a new L2ARC header. 4768185029Spjd */ 4769185029Spjd hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP); 4770185029Spjd hdrl2->b_dev = dev; 4771185029Spjd hdrl2->b_daddr = dev->l2ad_hand; 4772185029Spjd 4773206792Spjd ab->b_flags |= ARC_L2_WRITING; 4774185029Spjd ab->b_l2hdr = hdrl2; 4775185029Spjd list_insert_head(dev->l2ad_buflist, ab); 4776185029Spjd buf_data = ab->b_buf->b_data; 4777185029Spjd buf_sz = ab->b_size; 4778185029Spjd 4779185029Spjd /* 4780185029Spjd * Compute and store the buffer cksum before 4781185029Spjd * writing. On debug the cksum is verified first. 4782185029Spjd */ 4783185029Spjd arc_cksum_verify(ab->b_buf); 4784185029Spjd arc_cksum_compute(ab->b_buf, B_TRUE); 4785185029Spjd 4786185029Spjd mutex_exit(hash_lock); 4787185029Spjd 4788185029Spjd wzio = zio_write_phys(pio, dev->l2ad_vdev, 4789185029Spjd dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 4790185029Spjd NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 4791185029Spjd ZIO_FLAG_CANFAIL, B_FALSE); 4792185029Spjd 4793185029Spjd DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 4794185029Spjd zio_t *, wzio); 4795185029Spjd (void) zio_nowait(wzio); 4796185029Spjd 4797185029Spjd /* 4798185029Spjd * Keep the clock hand suitably device-aligned. 4799185029Spjd */ 4800185029Spjd buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 4801185029Spjd 4802185029Spjd write_sz += buf_sz; 4803185029Spjd dev->l2ad_hand += buf_sz; 4804185029Spjd } 4805185029Spjd 4806185029Spjd mutex_exit(list_lock); 4807185029Spjd 4808185029Spjd if (full == B_TRUE) 4809185029Spjd break; 4810185029Spjd } 4811185029Spjd mutex_exit(&l2arc_buflist_mtx); 4812185029Spjd 4813185029Spjd if (pio == NULL) { 4814185029Spjd ASSERT3U(write_sz, ==, 0); 4815185029Spjd kmem_cache_free(hdr_cache, head); 4816208373Smm return (0); 4817185029Spjd } 4818185029Spjd 4819185029Spjd ASSERT3U(write_sz, <=, target_sz); 4820185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_sent); 4821208373Smm ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz); 4822185029Spjd ARCSTAT_INCR(arcstat_l2_size, write_sz); 4823219089Spjd vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0); 4824185029Spjd 4825185029Spjd /* 4826185029Spjd * Bump device hand to the device start if it is approaching the end. 4827185029Spjd * l2arc_evict() will already have evicted ahead for this case. 4828185029Spjd */ 4829185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 4830219089Spjd vdev_space_update(dev->l2ad_vdev, 4831219089Spjd dev->l2ad_end - dev->l2ad_hand, 0, 0); 4832185029Spjd dev->l2ad_hand = dev->l2ad_start; 4833185029Spjd dev->l2ad_evict = dev->l2ad_start; 4834185029Spjd dev->l2ad_first = B_FALSE; 4835185029Spjd } 4836185029Spjd 4837208373Smm dev->l2ad_writing = B_TRUE; 4838185029Spjd (void) zio_wait(pio); 4839208373Smm dev->l2ad_writing = B_FALSE; 4840208373Smm 4841208373Smm return (write_sz); 4842185029Spjd} 4843185029Spjd 4844185029Spjd/* 4845185029Spjd * This thread feeds the L2ARC at regular intervals. This is the beating 4846185029Spjd * heart of the L2ARC. 4847185029Spjd */ 4848185029Spjdstatic void 4849185029Spjdl2arc_feed_thread(void *dummy __unused) 4850185029Spjd{ 4851185029Spjd callb_cpr_t cpr; 4852185029Spjd l2arc_dev_t *dev; 4853185029Spjd spa_t *spa; 4854208373Smm uint64_t size, wrote; 4855219089Spjd clock_t begin, next = ddi_get_lbolt(); 4856185029Spjd 4857185029Spjd CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 4858185029Spjd 4859185029Spjd mutex_enter(&l2arc_feed_thr_lock); 4860185029Spjd 4861185029Spjd while (l2arc_thread_exit == 0) { 4862185029Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 4863185029Spjd (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 4864219089Spjd next - ddi_get_lbolt()); 4865185029Spjd CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 4866219089Spjd next = ddi_get_lbolt() + hz; 4867185029Spjd 4868185029Spjd /* 4869185029Spjd * Quick check for L2ARC devices. 4870185029Spjd */ 4871185029Spjd mutex_enter(&l2arc_dev_mtx); 4872185029Spjd if (l2arc_ndev == 0) { 4873185029Spjd mutex_exit(&l2arc_dev_mtx); 4874185029Spjd continue; 4875185029Spjd } 4876185029Spjd mutex_exit(&l2arc_dev_mtx); 4877219089Spjd begin = ddi_get_lbolt(); 4878185029Spjd 4879185029Spjd /* 4880185029Spjd * This selects the next l2arc device to write to, and in 4881185029Spjd * doing so the next spa to feed from: dev->l2ad_spa. This 4882185029Spjd * will return NULL if there are now no l2arc devices or if 4883185029Spjd * they are all faulted. 4884185029Spjd * 4885185029Spjd * If a device is returned, its spa's config lock is also 4886185029Spjd * held to prevent device removal. l2arc_dev_get_next() 4887185029Spjd * will grab and release l2arc_dev_mtx. 4888185029Spjd */ 4889185029Spjd if ((dev = l2arc_dev_get_next()) == NULL) 4890185029Spjd continue; 4891185029Spjd 4892185029Spjd spa = dev->l2ad_spa; 4893185029Spjd ASSERT(spa != NULL); 4894185029Spjd 4895185029Spjd /* 4896219089Spjd * If the pool is read-only then force the feed thread to 4897219089Spjd * sleep a little longer. 4898219089Spjd */ 4899219089Spjd if (!spa_writeable(spa)) { 4900219089Spjd next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 4901219089Spjd spa_config_exit(spa, SCL_L2ARC, dev); 4902219089Spjd continue; 4903219089Spjd } 4904219089Spjd 4905219089Spjd /* 4906185029Spjd * Avoid contributing to memory pressure. 4907185029Spjd */ 4908185029Spjd if (arc_reclaim_needed()) { 4909185029Spjd ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 4910185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 4911185029Spjd continue; 4912185029Spjd } 4913185029Spjd 4914185029Spjd ARCSTAT_BUMP(arcstat_l2_feeds); 4915185029Spjd 4916208373Smm size = l2arc_write_size(dev); 4917185029Spjd 4918185029Spjd /* 4919185029Spjd * Evict L2ARC buffers that will be overwritten. 4920185029Spjd */ 4921185029Spjd l2arc_evict(dev, size, B_FALSE); 4922185029Spjd 4923185029Spjd /* 4924185029Spjd * Write ARC buffers. 4925185029Spjd */ 4926208373Smm wrote = l2arc_write_buffers(spa, dev, size); 4927208373Smm 4928208373Smm /* 4929208373Smm * Calculate interval between writes. 4930208373Smm */ 4931208373Smm next = l2arc_write_interval(begin, size, wrote); 4932185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 4933185029Spjd } 4934185029Spjd 4935185029Spjd l2arc_thread_exit = 0; 4936185029Spjd cv_broadcast(&l2arc_feed_thr_cv); 4937185029Spjd CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 4938185029Spjd thread_exit(); 4939185029Spjd} 4940185029Spjd 4941185029Spjdboolean_t 4942185029Spjdl2arc_vdev_present(vdev_t *vd) 4943185029Spjd{ 4944185029Spjd l2arc_dev_t *dev; 4945185029Spjd 4946185029Spjd mutex_enter(&l2arc_dev_mtx); 4947185029Spjd for (dev = list_head(l2arc_dev_list); dev != NULL; 4948185029Spjd dev = list_next(l2arc_dev_list, dev)) { 4949185029Spjd if (dev->l2ad_vdev == vd) 4950185029Spjd break; 4951185029Spjd } 4952185029Spjd mutex_exit(&l2arc_dev_mtx); 4953185029Spjd 4954185029Spjd return (dev != NULL); 4955185029Spjd} 4956185029Spjd 4957185029Spjd/* 4958185029Spjd * Add a vdev for use by the L2ARC. By this point the spa has already 4959185029Spjd * validated the vdev and opened it. 4960185029Spjd */ 4961185029Spjdvoid 4962219089Spjdl2arc_add_vdev(spa_t *spa, vdev_t *vd) 4963185029Spjd{ 4964185029Spjd l2arc_dev_t *adddev; 4965185029Spjd 4966185029Spjd ASSERT(!l2arc_vdev_present(vd)); 4967185029Spjd 4968185029Spjd /* 4969185029Spjd * Create a new l2arc device entry. 4970185029Spjd */ 4971185029Spjd adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 4972185029Spjd adddev->l2ad_spa = spa; 4973185029Spjd adddev->l2ad_vdev = vd; 4974185029Spjd adddev->l2ad_write = l2arc_write_max; 4975185029Spjd adddev->l2ad_boost = l2arc_write_boost; 4976219089Spjd adddev->l2ad_start = VDEV_LABEL_START_SIZE; 4977219089Spjd adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 4978185029Spjd adddev->l2ad_hand = adddev->l2ad_start; 4979185029Spjd adddev->l2ad_evict = adddev->l2ad_start; 4980185029Spjd adddev->l2ad_first = B_TRUE; 4981208373Smm adddev->l2ad_writing = B_FALSE; 4982185029Spjd ASSERT3U(adddev->l2ad_write, >, 0); 4983185029Spjd 4984185029Spjd /* 4985185029Spjd * This is a list of all ARC buffers that are still valid on the 4986185029Spjd * device. 4987185029Spjd */ 4988185029Spjd adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP); 4989185029Spjd list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 4990185029Spjd offsetof(arc_buf_hdr_t, b_l2node)); 4991185029Spjd 4992219089Spjd vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 4993185029Spjd 4994185029Spjd /* 4995185029Spjd * Add device to global list 4996185029Spjd */ 4997185029Spjd mutex_enter(&l2arc_dev_mtx); 4998185029Spjd list_insert_head(l2arc_dev_list, adddev); 4999185029Spjd atomic_inc_64(&l2arc_ndev); 5000185029Spjd mutex_exit(&l2arc_dev_mtx); 5001185029Spjd} 5002185029Spjd 5003185029Spjd/* 5004185029Spjd * Remove a vdev from the L2ARC. 5005185029Spjd */ 5006185029Spjdvoid 5007185029Spjdl2arc_remove_vdev(vdev_t *vd) 5008185029Spjd{ 5009185029Spjd l2arc_dev_t *dev, *nextdev, *remdev = NULL; 5010185029Spjd 5011185029Spjd /* 5012185029Spjd * Find the device by vdev 5013185029Spjd */ 5014185029Spjd mutex_enter(&l2arc_dev_mtx); 5015185029Spjd for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 5016185029Spjd nextdev = list_next(l2arc_dev_list, dev); 5017185029Spjd if (vd == dev->l2ad_vdev) { 5018185029Spjd remdev = dev; 5019185029Spjd break; 5020185029Spjd } 5021185029Spjd } 5022185029Spjd ASSERT(remdev != NULL); 5023185029Spjd 5024185029Spjd /* 5025185029Spjd * Remove device from global list 5026185029Spjd */ 5027185029Spjd list_remove(l2arc_dev_list, remdev); 5028185029Spjd l2arc_dev_last = NULL; /* may have been invalidated */ 5029185029Spjd atomic_dec_64(&l2arc_ndev); 5030185029Spjd mutex_exit(&l2arc_dev_mtx); 5031185029Spjd 5032185029Spjd /* 5033185029Spjd * Clear all buflists and ARC references. L2ARC device flush. 5034185029Spjd */ 5035185029Spjd l2arc_evict(remdev, 0, B_TRUE); 5036185029Spjd list_destroy(remdev->l2ad_buflist); 5037185029Spjd kmem_free(remdev->l2ad_buflist, sizeof (list_t)); 5038185029Spjd kmem_free(remdev, sizeof (l2arc_dev_t)); 5039185029Spjd} 5040185029Spjd 5041185029Spjdvoid 5042185029Spjdl2arc_init(void) 5043185029Spjd{ 5044185029Spjd l2arc_thread_exit = 0; 5045185029Spjd l2arc_ndev = 0; 5046185029Spjd l2arc_writes_sent = 0; 5047185029Spjd l2arc_writes_done = 0; 5048185029Spjd 5049185029Spjd mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 5050185029Spjd cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 5051185029Spjd mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 5052185029Spjd mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL); 5053185029Spjd mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 5054185029Spjd 5055185029Spjd l2arc_dev_list = &L2ARC_dev_list; 5056185029Spjd l2arc_free_on_write = &L2ARC_free_on_write; 5057185029Spjd list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 5058185029Spjd offsetof(l2arc_dev_t, l2ad_node)); 5059185029Spjd list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 5060185029Spjd offsetof(l2arc_data_free_t, l2df_list_node)); 5061185029Spjd} 5062185029Spjd 5063185029Spjdvoid 5064185029Spjdl2arc_fini(void) 5065185029Spjd{ 5066185029Spjd /* 5067185029Spjd * This is called from dmu_fini(), which is called from spa_fini(); 5068185029Spjd * Because of this, we can assume that all l2arc devices have 5069185029Spjd * already been removed when the pools themselves were removed. 5070185029Spjd */ 5071185029Spjd 5072185029Spjd l2arc_do_free_on_write(); 5073185029Spjd 5074185029Spjd mutex_destroy(&l2arc_feed_thr_lock); 5075185029Spjd cv_destroy(&l2arc_feed_thr_cv); 5076185029Spjd mutex_destroy(&l2arc_dev_mtx); 5077185029Spjd mutex_destroy(&l2arc_buflist_mtx); 5078185029Spjd mutex_destroy(&l2arc_free_on_write_mtx); 5079185029Spjd 5080185029Spjd list_destroy(l2arc_dev_list); 5081185029Spjd list_destroy(l2arc_free_on_write); 5082185029Spjd} 5083185029Spjd 5084185029Spjdvoid 5085185029Spjdl2arc_start(void) 5086185029Spjd{ 5087209962Smm if (!(spa_mode_global & FWRITE)) 5088185029Spjd return; 5089185029Spjd 5090185029Spjd (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 5091185029Spjd TS_RUN, minclsyspri); 5092185029Spjd} 5093185029Spjd 5094185029Spjdvoid 5095185029Spjdl2arc_stop(void) 5096185029Spjd{ 5097209962Smm if (!(spa_mode_global & FWRITE)) 5098185029Spjd return; 5099185029Spjd 5100185029Spjd mutex_enter(&l2arc_feed_thr_lock); 5101185029Spjd cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 5102185029Spjd l2arc_thread_exit = 1; 5103185029Spjd while (l2arc_thread_exit != 0) 5104185029Spjd cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 5105185029Spjd mutex_exit(&l2arc_feed_thr_lock); 5106185029Spjd} 5107