arc.c revision 288064
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. 23277826Sdelphij * Copyright (c) 2012, Joyent, Inc. All rights reserved. 24286766Smav * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 25260835Sdelphij * Copyright (c) 2014 by Saso Kiselkov. All rights reserved. 26286764Smav * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 27168404Spjd */ 28168404Spjd 29168404Spjd/* 30168404Spjd * DVA-based Adjustable Replacement Cache 31168404Spjd * 32168404Spjd * While much of the theory of operation used here is 33168404Spjd * based on the self-tuning, low overhead replacement cache 34168404Spjd * presented by Megiddo and Modha at FAST 2003, there are some 35168404Spjd * significant differences: 36168404Spjd * 37168404Spjd * 1. The Megiddo and Modha model assumes any page is evictable. 38168404Spjd * Pages in its cache cannot be "locked" into memory. This makes 39168404Spjd * the eviction algorithm simple: evict the last page in the list. 40168404Spjd * This also make the performance characteristics easy to reason 41168404Spjd * about. Our cache is not so simple. At any given moment, some 42168404Spjd * subset of the blocks in the cache are un-evictable because we 43168404Spjd * have handed out a reference to them. Blocks are only evictable 44168404Spjd * when there are no external references active. This makes 45168404Spjd * eviction far more problematic: we choose to evict the evictable 46168404Spjd * blocks that are the "lowest" in the list. 47168404Spjd * 48168404Spjd * There are times when it is not possible to evict the requested 49168404Spjd * space. In these circumstances we are unable to adjust the cache 50168404Spjd * size. To prevent the cache growing unbounded at these times we 51185029Spjd * implement a "cache throttle" that slows the flow of new data 52185029Spjd * into the cache until we can make space available. 53168404Spjd * 54168404Spjd * 2. The Megiddo and Modha model assumes a fixed cache size. 55168404Spjd * Pages are evicted when the cache is full and there is a cache 56168404Spjd * miss. Our model has a variable sized cache. It grows with 57185029Spjd * high use, but also tries to react to memory pressure from the 58168404Spjd * operating system: decreasing its size when system memory is 59168404Spjd * tight. 60168404Spjd * 61168404Spjd * 3. The Megiddo and Modha model assumes a fixed page size. All 62251631Sdelphij * elements of the cache are therefore exactly the same size. So 63168404Spjd * when adjusting the cache size following a cache miss, its simply 64168404Spjd * a matter of choosing a single page to evict. In our model, we 65168404Spjd * have variable sized cache blocks (rangeing from 512 bytes to 66251631Sdelphij * 128K bytes). We therefore choose a set of blocks to evict to make 67168404Spjd * space for a cache miss that approximates as closely as possible 68168404Spjd * the space used by the new block. 69168404Spjd * 70168404Spjd * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 71168404Spjd * by N. Megiddo & D. Modha, FAST 2003 72168404Spjd */ 73168404Spjd 74168404Spjd/* 75168404Spjd * The locking model: 76168404Spjd * 77168404Spjd * A new reference to a cache buffer can be obtained in two 78168404Spjd * ways: 1) via a hash table lookup using the DVA as a key, 79185029Spjd * or 2) via one of the ARC lists. The arc_read() interface 80168404Spjd * uses method 1, while the internal arc algorithms for 81251631Sdelphij * adjusting the cache use method 2. We therefore provide two 82168404Spjd * types of locks: 1) the hash table lock array, and 2) the 83168404Spjd * arc list locks. 84168404Spjd * 85286774Smav * Buffers do not have their own mutexes, rather they rely on the 86286774Smav * hash table mutexes for the bulk of their protection (i.e. most 87286774Smav * fields in the arc_buf_hdr_t are protected by these mutexes). 88168404Spjd * 89168404Spjd * buf_hash_find() returns the appropriate mutex (held) when it 90168404Spjd * locates the requested buffer in the hash table. It returns 91168404Spjd * NULL for the mutex if the buffer was not in the table. 92168404Spjd * 93168404Spjd * buf_hash_remove() expects the appropriate hash mutex to be 94168404Spjd * already held before it is invoked. 95168404Spjd * 96168404Spjd * Each arc state also has a mutex which is used to protect the 97168404Spjd * buffer list associated with the state. When attempting to 98168404Spjd * obtain a hash table lock while holding an arc list lock you 99168404Spjd * must use: mutex_tryenter() to avoid deadlock. Also note that 100168404Spjd * the active state mutex must be held before the ghost state mutex. 101168404Spjd * 102168404Spjd * Arc buffers may have an associated eviction callback function. 103168404Spjd * This function will be invoked prior to removing the buffer (e.g. 104168404Spjd * in arc_do_user_evicts()). Note however that the data associated 105168404Spjd * with the buffer may be evicted prior to the callback. The callback 106168404Spjd * must be made with *no locks held* (to prevent deadlock). Additionally, 107168404Spjd * the users of callbacks must ensure that their private data is 108268858Sdelphij * protected from simultaneous callbacks from arc_clear_callback() 109168404Spjd * and arc_do_user_evicts(). 110168404Spjd * 111168404Spjd * Note that the majority of the performance stats are manipulated 112168404Spjd * with atomic operations. 113185029Spjd * 114286570Smav * The L2ARC uses the l2ad_mtx on each vdev for the following: 115185029Spjd * 116185029Spjd * - L2ARC buflist creation 117185029Spjd * - L2ARC buflist eviction 118185029Spjd * - L2ARC write completion, which walks L2ARC buflists 119185029Spjd * - ARC header destruction, as it removes from L2ARC buflists 120185029Spjd * - ARC header release, as it removes from L2ARC buflists 121168404Spjd */ 122168404Spjd 123168404Spjd#include <sys/spa.h> 124168404Spjd#include <sys/zio.h> 125251478Sdelphij#include <sys/zio_compress.h> 126168404Spjd#include <sys/zfs_context.h> 127168404Spjd#include <sys/arc.h> 128168404Spjd#include <sys/refcount.h> 129185029Spjd#include <sys/vdev.h> 130219089Spjd#include <sys/vdev_impl.h> 131258632Savg#include <sys/dsl_pool.h> 132286763Smav#include <sys/multilist.h> 133168404Spjd#ifdef _KERNEL 134168404Spjd#include <sys/dnlc.h> 135168404Spjd#endif 136168404Spjd#include <sys/callb.h> 137168404Spjd#include <sys/kstat.h> 138248572Ssmh#include <sys/trim_map.h> 139219089Spjd#include <zfs_fletcher.h> 140168404Spjd#include <sys/sdt.h> 141168404Spjd 142191902Skmacy#include <vm/vm_pageout.h> 143272483Ssmh#include <machine/vmparam.h> 144191902Skmacy 145240133Smm#ifdef illumos 146240133Smm#ifndef _KERNEL 147240133Smm/* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ 148240133Smmboolean_t arc_watch = B_FALSE; 149240133Smmint arc_procfd; 150240133Smm#endif 151240133Smm#endif /* illumos */ 152240133Smm 153286763Smavstatic kmutex_t arc_reclaim_lock; 154286763Smavstatic kcondvar_t arc_reclaim_thread_cv; 155286763Smavstatic boolean_t arc_reclaim_thread_exit; 156286763Smavstatic kcondvar_t arc_reclaim_waiters_cv; 157168404Spjd 158286763Smavstatic kmutex_t arc_user_evicts_lock; 159286763Smavstatic kcondvar_t arc_user_evicts_cv; 160286763Smavstatic boolean_t arc_user_evicts_thread_exit; 161286763Smav 162286625Smavuint_t arc_reduce_dnlc_percent = 3; 163168404Spjd 164258632Savg/* 165286763Smav * The number of headers to evict in arc_evict_state_impl() before 166286763Smav * dropping the sublist lock and evicting from another sublist. A lower 167286763Smav * value means we're more likely to evict the "correct" header (i.e. the 168286763Smav * oldest header in the arc state), but comes with higher overhead 169286763Smav * (i.e. more invocations of arc_evict_state_impl()). 170258632Savg */ 171286763Smavint zfs_arc_evict_batch_limit = 10; 172258632Savg 173286763Smav/* 174286763Smav * The number of sublists used for each of the arc state lists. If this 175286763Smav * is not set to a suitable value by the user, it will be configured to 176286763Smav * the number of CPUs on the system in arc_init(). 177286763Smav */ 178286763Smavint zfs_arc_num_sublists_per_state = 0; 179286763Smav 180168404Spjd/* number of seconds before growing cache again */ 181168404Spjdstatic int arc_grow_retry = 60; 182168404Spjd 183286763Smav/* shift of arc_c for calculating overflow limit in arc_get_data_buf */ 184286763Smavint zfs_arc_overflow_shift = 8; 185286763Smav 186208373Smm/* shift of arc_c for calculating both min and max arc_p */ 187208373Smmstatic int arc_p_min_shift = 4; 188208373Smm 189208373Smm/* log2(fraction of arc to reclaim) */ 190286625Smavstatic int arc_shrink_shift = 7; 191208373Smm 192168404Spjd/* 193286625Smav * log2(fraction of ARC which must be free to allow growing). 194286625Smav * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, 195286625Smav * when reading a new block into the ARC, we will evict an equal-sized block 196286625Smav * from the ARC. 197286625Smav * 198286625Smav * This must be less than arc_shrink_shift, so that when we shrink the ARC, 199286625Smav * we will still not allow it to grow. 200286625Smav */ 201286625Smavint arc_no_grow_shift = 5; 202286625Smav 203286625Smav 204286625Smav/* 205168404Spjd * minimum lifespan of a prefetch block in clock ticks 206168404Spjd * (initialized in arc_init()) 207168404Spjd */ 208168404Spjdstatic int arc_min_prefetch_lifespan; 209168404Spjd 210258632Savg/* 211258632Savg * If this percent of memory is free, don't throttle. 212258632Savg */ 213258632Savgint arc_lotsfree_percent = 10; 214258632Savg 215208373Smmstatic int arc_dead; 216287702Sdelphijextern boolean_t zfs_prefetch_disable; 217168404Spjd 218168404Spjd/* 219185029Spjd * The arc has filled available memory and has now warmed up. 220185029Spjd */ 221185029Spjdstatic boolean_t arc_warm; 222185029Spjd 223286762Smav/* 224286762Smav * These tunables are for performance analysis. 225286762Smav */ 226185029Spjduint64_t zfs_arc_max; 227185029Spjduint64_t zfs_arc_min; 228185029Spjduint64_t zfs_arc_meta_limit = 0; 229275780Sdelphijuint64_t zfs_arc_meta_min = 0; 230208373Smmint zfs_arc_grow_retry = 0; 231208373Smmint zfs_arc_shrink_shift = 0; 232208373Smmint zfs_arc_p_min_shift = 0; 233242845Sdelphijint zfs_disable_dup_eviction = 0; 234269230Sdelphijuint64_t zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ 235272483Ssmhu_int zfs_arc_free_target = 0; 236185029Spjd 237270759Ssmhstatic int sysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS); 238275748Sdelphijstatic int sysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS); 239270759Ssmh 240270759Ssmh#ifdef _KERNEL 241270759Ssmhstatic void 242270759Ssmharc_free_target_init(void *unused __unused) 243270759Ssmh{ 244270759Ssmh 245272483Ssmh zfs_arc_free_target = vm_pageout_wakeup_thresh; 246270759Ssmh} 247270759SsmhSYSINIT(arc_free_target_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_ANY, 248270759Ssmh arc_free_target_init, NULL); 249270759Ssmh 250185029SpjdTUNABLE_QUAD("vfs.zfs.arc_meta_limit", &zfs_arc_meta_limit); 251275780SdelphijTUNABLE_QUAD("vfs.zfs.arc_meta_min", &zfs_arc_meta_min); 252273026SdelphijTUNABLE_INT("vfs.zfs.arc_shrink_shift", &zfs_arc_shrink_shift); 253168473SpjdSYSCTL_DECL(_vfs_zfs); 254217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_max, CTLFLAG_RDTUN, &zfs_arc_max, 0, 255168473Spjd "Maximum ARC size"); 256217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_min, CTLFLAG_RDTUN, &zfs_arc_min, 0, 257168473Spjd "Minimum ARC size"); 258269230SdelphijSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_average_blocksize, CTLFLAG_RDTUN, 259269230Sdelphij &zfs_arc_average_blocksize, 0, 260269230Sdelphij "ARC average blocksize"); 261273026SdelphijSYSCTL_INT(_vfs_zfs, OID_AUTO, arc_shrink_shift, CTLFLAG_RW, 262273026Sdelphij &arc_shrink_shift, 0, 263273026Sdelphij "log2(fraction of arc to reclaim)"); 264273026Sdelphij 265270759Ssmh/* 266270759Ssmh * We don't have a tunable for arc_free_target due to the dependency on 267270759Ssmh * pagedaemon initialisation. 268270759Ssmh */ 269270759SsmhSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_free_target, 270270759Ssmh CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(u_int), 271270759Ssmh sysctl_vfs_zfs_arc_free_target, "IU", 272270759Ssmh "Desired number of free pages below which ARC triggers reclaim"); 273168404Spjd 274270759Ssmhstatic int 275270759Ssmhsysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS) 276270759Ssmh{ 277270759Ssmh u_int val; 278270759Ssmh int err; 279270759Ssmh 280270759Ssmh val = zfs_arc_free_target; 281270759Ssmh err = sysctl_handle_int(oidp, &val, 0, req); 282270759Ssmh if (err != 0 || req->newptr == NULL) 283270759Ssmh return (err); 284270759Ssmh 285272483Ssmh if (val < minfree) 286270759Ssmh return (EINVAL); 287272483Ssmh if (val > vm_cnt.v_page_count) 288270759Ssmh return (EINVAL); 289270759Ssmh 290270759Ssmh zfs_arc_free_target = val; 291270759Ssmh 292270759Ssmh return (0); 293270759Ssmh} 294275748Sdelphij 295275748Sdelphij/* 296275748Sdelphij * Must be declared here, before the definition of corresponding kstat 297275748Sdelphij * macro which uses the same names will confuse the compiler. 298275748Sdelphij */ 299275748SdelphijSYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_meta_limit, 300275748Sdelphij CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t), 301275748Sdelphij sysctl_vfs_zfs_arc_meta_limit, "QU", 302275748Sdelphij "ARC metadata limit"); 303272483Ssmh#endif 304270759Ssmh 305168404Spjd/* 306185029Spjd * Note that buffers can be in one of 6 states: 307168404Spjd * ARC_anon - anonymous (discussed below) 308168404Spjd * ARC_mru - recently used, currently cached 309168404Spjd * ARC_mru_ghost - recentely used, no longer in cache 310168404Spjd * ARC_mfu - frequently used, currently cached 311168404Spjd * ARC_mfu_ghost - frequently used, no longer in cache 312185029Spjd * ARC_l2c_only - exists in L2ARC but not other states 313185029Spjd * When there are no active references to the buffer, they are 314185029Spjd * are linked onto a list in one of these arc states. These are 315185029Spjd * the only buffers that can be evicted or deleted. Within each 316185029Spjd * state there are multiple lists, one for meta-data and one for 317185029Spjd * non-meta-data. Meta-data (indirect blocks, blocks of dnodes, 318185029Spjd * etc.) is tracked separately so that it can be managed more 319185029Spjd * explicitly: favored over data, limited explicitly. 320168404Spjd * 321168404Spjd * Anonymous buffers are buffers that are not associated with 322168404Spjd * a DVA. These are buffers that hold dirty block copies 323168404Spjd * before they are written to stable storage. By definition, 324168404Spjd * they are "ref'd" and are considered part of arc_mru 325168404Spjd * that cannot be freed. Generally, they will aquire a DVA 326168404Spjd * as they are written and migrate onto the arc_mru list. 327185029Spjd * 328185029Spjd * The ARC_l2c_only state is for buffers that are in the second 329185029Spjd * level ARC but no longer in any of the ARC_m* lists. The second 330185029Spjd * level ARC itself may also contain buffers that are in any of 331185029Spjd * the ARC_m* states - meaning that a buffer can exist in two 332185029Spjd * places. The reason for the ARC_l2c_only state is to keep the 333185029Spjd * buffer header in the hash table, so that reads that hit the 334185029Spjd * second level ARC benefit from these fast lookups. 335168404Spjd */ 336168404Spjd 337168404Spjdtypedef struct arc_state { 338286763Smav /* 339286763Smav * list of evictable buffers 340286763Smav */ 341286763Smav multilist_t arcs_list[ARC_BUFC_NUMTYPES]; 342286763Smav /* 343286763Smav * total amount of evictable data in this state 344286763Smav */ 345286763Smav uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; 346286763Smav /* 347286763Smav * total amount of data in this state; this includes: evictable, 348286763Smav * non-evictable, ARC_BUFC_DATA, and ARC_BUFC_METADATA. 349286763Smav */ 350286766Smav refcount_t arcs_size; 351168404Spjd} arc_state_t; 352168404Spjd 353185029Spjd/* The 6 states: */ 354168404Spjdstatic arc_state_t ARC_anon; 355168404Spjdstatic arc_state_t ARC_mru; 356168404Spjdstatic arc_state_t ARC_mru_ghost; 357168404Spjdstatic arc_state_t ARC_mfu; 358168404Spjdstatic arc_state_t ARC_mfu_ghost; 359185029Spjdstatic arc_state_t ARC_l2c_only; 360168404Spjd 361168404Spjdtypedef struct arc_stats { 362168404Spjd kstat_named_t arcstat_hits; 363168404Spjd kstat_named_t arcstat_misses; 364168404Spjd kstat_named_t arcstat_demand_data_hits; 365168404Spjd kstat_named_t arcstat_demand_data_misses; 366168404Spjd kstat_named_t arcstat_demand_metadata_hits; 367168404Spjd kstat_named_t arcstat_demand_metadata_misses; 368168404Spjd kstat_named_t arcstat_prefetch_data_hits; 369168404Spjd kstat_named_t arcstat_prefetch_data_misses; 370168404Spjd kstat_named_t arcstat_prefetch_metadata_hits; 371168404Spjd kstat_named_t arcstat_prefetch_metadata_misses; 372168404Spjd kstat_named_t arcstat_mru_hits; 373168404Spjd kstat_named_t arcstat_mru_ghost_hits; 374168404Spjd kstat_named_t arcstat_mfu_hits; 375168404Spjd kstat_named_t arcstat_mfu_ghost_hits; 376205231Skmacy kstat_named_t arcstat_allocated; 377168404Spjd kstat_named_t arcstat_deleted; 378251629Sdelphij /* 379251629Sdelphij * Number of buffers that could not be evicted because the hash lock 380251629Sdelphij * was held by another thread. The lock may not necessarily be held 381251629Sdelphij * by something using the same buffer, since hash locks are shared 382251629Sdelphij * by multiple buffers. 383251629Sdelphij */ 384168404Spjd kstat_named_t arcstat_mutex_miss; 385251629Sdelphij /* 386251629Sdelphij * Number of buffers skipped because they have I/O in progress, are 387251629Sdelphij * indrect prefetch buffers that have not lived long enough, or are 388251629Sdelphij * not from the spa we're trying to evict from. 389251629Sdelphij */ 390168404Spjd kstat_named_t arcstat_evict_skip; 391286763Smav /* 392286763Smav * Number of times arc_evict_state() was unable to evict enough 393286763Smav * buffers to reach it's target amount. 394286763Smav */ 395286763Smav kstat_named_t arcstat_evict_not_enough; 396208373Smm kstat_named_t arcstat_evict_l2_cached; 397208373Smm kstat_named_t arcstat_evict_l2_eligible; 398208373Smm kstat_named_t arcstat_evict_l2_ineligible; 399286763Smav kstat_named_t arcstat_evict_l2_skip; 400168404Spjd kstat_named_t arcstat_hash_elements; 401168404Spjd kstat_named_t arcstat_hash_elements_max; 402168404Spjd kstat_named_t arcstat_hash_collisions; 403168404Spjd kstat_named_t arcstat_hash_chains; 404168404Spjd kstat_named_t arcstat_hash_chain_max; 405168404Spjd kstat_named_t arcstat_p; 406168404Spjd kstat_named_t arcstat_c; 407168404Spjd kstat_named_t arcstat_c_min; 408168404Spjd kstat_named_t arcstat_c_max; 409168404Spjd kstat_named_t arcstat_size; 410286574Smav /* 411286574Smav * Number of bytes consumed by internal ARC structures necessary 412286574Smav * for tracking purposes; these structures are not actually 413286574Smav * backed by ARC buffers. This includes arc_buf_hdr_t structures 414286574Smav * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only 415286574Smav * caches), and arc_buf_t structures (allocated via arc_buf_t 416286574Smav * cache). 417286574Smav */ 418185029Spjd kstat_named_t arcstat_hdr_size; 419286574Smav /* 420286574Smav * Number of bytes consumed by ARC buffers of type equal to 421286574Smav * ARC_BUFC_DATA. This is generally consumed by buffers backing 422286574Smav * on disk user data (e.g. plain file contents). 423286574Smav */ 424208373Smm kstat_named_t arcstat_data_size; 425286574Smav /* 426286574Smav * Number of bytes consumed by ARC buffers of type equal to 427286574Smav * ARC_BUFC_METADATA. This is generally consumed by buffers 428286574Smav * backing on disk data that is used for internal ZFS 429286574Smav * structures (e.g. ZAP, dnode, indirect blocks, etc). 430286574Smav */ 431286574Smav kstat_named_t arcstat_metadata_size; 432286574Smav /* 433286574Smav * Number of bytes consumed by various buffers and structures 434286574Smav * not actually backed with ARC buffers. This includes bonus 435286574Smav * buffers (allocated directly via zio_buf_* functions), 436286574Smav * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t 437286574Smav * cache), and dnode_t structures (allocated via dnode_t cache). 438286574Smav */ 439208373Smm kstat_named_t arcstat_other_size; 440286574Smav /* 441286574Smav * Total number of bytes consumed by ARC buffers residing in the 442286574Smav * arc_anon state. This includes *all* buffers in the arc_anon 443286574Smav * state; e.g. data, metadata, evictable, and unevictable buffers 444286574Smav * are all included in this value. 445286574Smav */ 446286574Smav kstat_named_t arcstat_anon_size; 447286574Smav /* 448286574Smav * Number of bytes consumed by ARC buffers that meet the 449286574Smav * following criteria: backing buffers of type ARC_BUFC_DATA, 450286574Smav * residing in the arc_anon state, and are eligible for eviction 451286574Smav * (e.g. have no outstanding holds on the buffer). 452286574Smav */ 453286574Smav kstat_named_t arcstat_anon_evictable_data; 454286574Smav /* 455286574Smav * Number of bytes consumed by ARC buffers that meet the 456286574Smav * following criteria: backing buffers of type ARC_BUFC_METADATA, 457286574Smav * residing in the arc_anon state, and are eligible for eviction 458286574Smav * (e.g. have no outstanding holds on the buffer). 459286574Smav */ 460286574Smav kstat_named_t arcstat_anon_evictable_metadata; 461286574Smav /* 462286574Smav * Total number of bytes consumed by ARC buffers residing in the 463286574Smav * arc_mru state. This includes *all* buffers in the arc_mru 464286574Smav * state; e.g. data, metadata, evictable, and unevictable buffers 465286574Smav * are all included in this value. 466286574Smav */ 467286574Smav kstat_named_t arcstat_mru_size; 468286574Smav /* 469286574Smav * Number of bytes consumed by ARC buffers that meet the 470286574Smav * following criteria: backing buffers of type ARC_BUFC_DATA, 471286574Smav * residing in the arc_mru state, and are eligible for eviction 472286574Smav * (e.g. have no outstanding holds on the buffer). 473286574Smav */ 474286574Smav kstat_named_t arcstat_mru_evictable_data; 475286574Smav /* 476286574Smav * Number of bytes consumed by ARC buffers that meet the 477286574Smav * following criteria: backing buffers of type ARC_BUFC_METADATA, 478286574Smav * residing in the arc_mru state, and are eligible for eviction 479286574Smav * (e.g. have no outstanding holds on the buffer). 480286574Smav */ 481286574Smav kstat_named_t arcstat_mru_evictable_metadata; 482286574Smav /* 483286574Smav * Total number of bytes that *would have been* consumed by ARC 484286574Smav * buffers in the arc_mru_ghost state. The key thing to note 485286574Smav * here, is the fact that this size doesn't actually indicate 486286574Smav * RAM consumption. The ghost lists only consist of headers and 487286574Smav * don't actually have ARC buffers linked off of these headers. 488286574Smav * Thus, *if* the headers had associated ARC buffers, these 489286574Smav * buffers *would have* consumed this number of bytes. 490286574Smav */ 491286574Smav kstat_named_t arcstat_mru_ghost_size; 492286574Smav /* 493286574Smav * Number of bytes that *would have been* consumed by ARC 494286574Smav * buffers that are eligible for eviction, of type 495286574Smav * ARC_BUFC_DATA, and linked off the arc_mru_ghost state. 496286574Smav */ 497286574Smav kstat_named_t arcstat_mru_ghost_evictable_data; 498286574Smav /* 499286574Smav * Number of bytes that *would have been* consumed by ARC 500286574Smav * buffers that are eligible for eviction, of type 501286574Smav * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 502286574Smav */ 503286574Smav kstat_named_t arcstat_mru_ghost_evictable_metadata; 504286574Smav /* 505286574Smav * Total number of bytes consumed by ARC buffers residing in the 506286574Smav * arc_mfu state. This includes *all* buffers in the arc_mfu 507286574Smav * state; e.g. data, metadata, evictable, and unevictable buffers 508286574Smav * are all included in this value. 509286574Smav */ 510286574Smav kstat_named_t arcstat_mfu_size; 511286574Smav /* 512286574Smav * Number of bytes consumed by ARC buffers that are eligible for 513286574Smav * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu 514286574Smav * state. 515286574Smav */ 516286574Smav kstat_named_t arcstat_mfu_evictable_data; 517286574Smav /* 518286574Smav * Number of bytes consumed by ARC buffers that are eligible for 519286574Smav * eviction, of type ARC_BUFC_METADATA, and reside in the 520286574Smav * arc_mfu state. 521286574Smav */ 522286574Smav kstat_named_t arcstat_mfu_evictable_metadata; 523286574Smav /* 524286574Smav * Total number of bytes that *would have been* consumed by ARC 525286574Smav * buffers in the arc_mfu_ghost state. See the comment above 526286574Smav * arcstat_mru_ghost_size for more details. 527286574Smav */ 528286574Smav kstat_named_t arcstat_mfu_ghost_size; 529286574Smav /* 530286574Smav * Number of bytes that *would have been* consumed by ARC 531286574Smav * buffers that are eligible for eviction, of type 532286574Smav * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state. 533286574Smav */ 534286574Smav kstat_named_t arcstat_mfu_ghost_evictable_data; 535286574Smav /* 536286574Smav * Number of bytes that *would have been* consumed by ARC 537286574Smav * buffers that are eligible for eviction, of type 538286574Smav * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. 539286574Smav */ 540286574Smav kstat_named_t arcstat_mfu_ghost_evictable_metadata; 541185029Spjd kstat_named_t arcstat_l2_hits; 542185029Spjd kstat_named_t arcstat_l2_misses; 543185029Spjd kstat_named_t arcstat_l2_feeds; 544185029Spjd kstat_named_t arcstat_l2_rw_clash; 545208373Smm kstat_named_t arcstat_l2_read_bytes; 546208373Smm kstat_named_t arcstat_l2_write_bytes; 547185029Spjd kstat_named_t arcstat_l2_writes_sent; 548185029Spjd kstat_named_t arcstat_l2_writes_done; 549185029Spjd kstat_named_t arcstat_l2_writes_error; 550286763Smav kstat_named_t arcstat_l2_writes_lock_retry; 551185029Spjd kstat_named_t arcstat_l2_evict_lock_retry; 552185029Spjd kstat_named_t arcstat_l2_evict_reading; 553286570Smav kstat_named_t arcstat_l2_evict_l1cached; 554185029Spjd kstat_named_t arcstat_l2_free_on_write; 555274172Savg kstat_named_t arcstat_l2_cdata_free_on_write; 556185029Spjd kstat_named_t arcstat_l2_abort_lowmem; 557185029Spjd kstat_named_t arcstat_l2_cksum_bad; 558185029Spjd kstat_named_t arcstat_l2_io_error; 559185029Spjd kstat_named_t arcstat_l2_size; 560251478Sdelphij kstat_named_t arcstat_l2_asize; 561185029Spjd kstat_named_t arcstat_l2_hdr_size; 562251478Sdelphij kstat_named_t arcstat_l2_compress_successes; 563251478Sdelphij kstat_named_t arcstat_l2_compress_zeros; 564251478Sdelphij kstat_named_t arcstat_l2_compress_failures; 565205231Skmacy kstat_named_t arcstat_l2_write_trylock_fail; 566205231Skmacy kstat_named_t arcstat_l2_write_passed_headroom; 567205231Skmacy kstat_named_t arcstat_l2_write_spa_mismatch; 568206796Spjd kstat_named_t arcstat_l2_write_in_l2; 569205231Skmacy kstat_named_t arcstat_l2_write_hdr_io_in_progress; 570205231Skmacy kstat_named_t arcstat_l2_write_not_cacheable; 571205231Skmacy kstat_named_t arcstat_l2_write_full; 572205231Skmacy kstat_named_t arcstat_l2_write_buffer_iter; 573205231Skmacy kstat_named_t arcstat_l2_write_pios; 574205231Skmacy kstat_named_t arcstat_l2_write_buffer_bytes_scanned; 575205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_iter; 576205231Skmacy kstat_named_t arcstat_l2_write_buffer_list_null_iter; 577242845Sdelphij kstat_named_t arcstat_memory_throttle_count; 578242845Sdelphij kstat_named_t arcstat_duplicate_buffers; 579242845Sdelphij kstat_named_t arcstat_duplicate_buffers_size; 580242845Sdelphij kstat_named_t arcstat_duplicate_reads; 581275748Sdelphij kstat_named_t arcstat_meta_used; 582275748Sdelphij kstat_named_t arcstat_meta_limit; 583275748Sdelphij kstat_named_t arcstat_meta_max; 584275780Sdelphij kstat_named_t arcstat_meta_min; 585287702Sdelphij kstat_named_t arcstat_sync_wait_for_async; 586287702Sdelphij kstat_named_t arcstat_demand_hit_predictive_prefetch; 587168404Spjd} arc_stats_t; 588168404Spjd 589168404Spjdstatic arc_stats_t arc_stats = { 590168404Spjd { "hits", KSTAT_DATA_UINT64 }, 591168404Spjd { "misses", KSTAT_DATA_UINT64 }, 592168404Spjd { "demand_data_hits", KSTAT_DATA_UINT64 }, 593168404Spjd { "demand_data_misses", KSTAT_DATA_UINT64 }, 594168404Spjd { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 595168404Spjd { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 596168404Spjd { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 597168404Spjd { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 598168404Spjd { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 599168404Spjd { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 600168404Spjd { "mru_hits", KSTAT_DATA_UINT64 }, 601168404Spjd { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 602168404Spjd { "mfu_hits", KSTAT_DATA_UINT64 }, 603168404Spjd { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 604205231Skmacy { "allocated", KSTAT_DATA_UINT64 }, 605168404Spjd { "deleted", KSTAT_DATA_UINT64 }, 606168404Spjd { "mutex_miss", KSTAT_DATA_UINT64 }, 607168404Spjd { "evict_skip", KSTAT_DATA_UINT64 }, 608286763Smav { "evict_not_enough", KSTAT_DATA_UINT64 }, 609208373Smm { "evict_l2_cached", KSTAT_DATA_UINT64 }, 610208373Smm { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 611208373Smm { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 612286763Smav { "evict_l2_skip", KSTAT_DATA_UINT64 }, 613168404Spjd { "hash_elements", KSTAT_DATA_UINT64 }, 614168404Spjd { "hash_elements_max", KSTAT_DATA_UINT64 }, 615168404Spjd { "hash_collisions", KSTAT_DATA_UINT64 }, 616168404Spjd { "hash_chains", KSTAT_DATA_UINT64 }, 617168404Spjd { "hash_chain_max", KSTAT_DATA_UINT64 }, 618168404Spjd { "p", KSTAT_DATA_UINT64 }, 619168404Spjd { "c", KSTAT_DATA_UINT64 }, 620168404Spjd { "c_min", KSTAT_DATA_UINT64 }, 621168404Spjd { "c_max", KSTAT_DATA_UINT64 }, 622185029Spjd { "size", KSTAT_DATA_UINT64 }, 623185029Spjd { "hdr_size", KSTAT_DATA_UINT64 }, 624208373Smm { "data_size", KSTAT_DATA_UINT64 }, 625286574Smav { "metadata_size", KSTAT_DATA_UINT64 }, 626208373Smm { "other_size", KSTAT_DATA_UINT64 }, 627286574Smav { "anon_size", KSTAT_DATA_UINT64 }, 628286574Smav { "anon_evictable_data", KSTAT_DATA_UINT64 }, 629286574Smav { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, 630286574Smav { "mru_size", KSTAT_DATA_UINT64 }, 631286574Smav { "mru_evictable_data", KSTAT_DATA_UINT64 }, 632286574Smav { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, 633286574Smav { "mru_ghost_size", KSTAT_DATA_UINT64 }, 634286574Smav { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, 635286574Smav { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 636286574Smav { "mfu_size", KSTAT_DATA_UINT64 }, 637286574Smav { "mfu_evictable_data", KSTAT_DATA_UINT64 }, 638286574Smav { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, 639286574Smav { "mfu_ghost_size", KSTAT_DATA_UINT64 }, 640286574Smav { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, 641286574Smav { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 642185029Spjd { "l2_hits", KSTAT_DATA_UINT64 }, 643185029Spjd { "l2_misses", KSTAT_DATA_UINT64 }, 644185029Spjd { "l2_feeds", KSTAT_DATA_UINT64 }, 645185029Spjd { "l2_rw_clash", KSTAT_DATA_UINT64 }, 646208373Smm { "l2_read_bytes", KSTAT_DATA_UINT64 }, 647208373Smm { "l2_write_bytes", KSTAT_DATA_UINT64 }, 648185029Spjd { "l2_writes_sent", KSTAT_DATA_UINT64 }, 649185029Spjd { "l2_writes_done", KSTAT_DATA_UINT64 }, 650185029Spjd { "l2_writes_error", KSTAT_DATA_UINT64 }, 651286763Smav { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, 652185029Spjd { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 653185029Spjd { "l2_evict_reading", KSTAT_DATA_UINT64 }, 654286570Smav { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, 655185029Spjd { "l2_free_on_write", KSTAT_DATA_UINT64 }, 656274172Savg { "l2_cdata_free_on_write", KSTAT_DATA_UINT64 }, 657185029Spjd { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 658185029Spjd { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 659185029Spjd { "l2_io_error", KSTAT_DATA_UINT64 }, 660185029Spjd { "l2_size", KSTAT_DATA_UINT64 }, 661251478Sdelphij { "l2_asize", KSTAT_DATA_UINT64 }, 662185029Spjd { "l2_hdr_size", KSTAT_DATA_UINT64 }, 663251478Sdelphij { "l2_compress_successes", KSTAT_DATA_UINT64 }, 664251478Sdelphij { "l2_compress_zeros", KSTAT_DATA_UINT64 }, 665251478Sdelphij { "l2_compress_failures", KSTAT_DATA_UINT64 }, 666206796Spjd { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 667206796Spjd { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 668206796Spjd { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 669206796Spjd { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 670206796Spjd { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 671206796Spjd { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 672206796Spjd { "l2_write_full", KSTAT_DATA_UINT64 }, 673206796Spjd { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 674206796Spjd { "l2_write_pios", KSTAT_DATA_UINT64 }, 675206796Spjd { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 676206796Spjd { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 677242845Sdelphij { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 }, 678242845Sdelphij { "memory_throttle_count", KSTAT_DATA_UINT64 }, 679242845Sdelphij { "duplicate_buffers", KSTAT_DATA_UINT64 }, 680242845Sdelphij { "duplicate_buffers_size", KSTAT_DATA_UINT64 }, 681275748Sdelphij { "duplicate_reads", KSTAT_DATA_UINT64 }, 682275748Sdelphij { "arc_meta_used", KSTAT_DATA_UINT64 }, 683275748Sdelphij { "arc_meta_limit", KSTAT_DATA_UINT64 }, 684275780Sdelphij { "arc_meta_max", KSTAT_DATA_UINT64 }, 685287702Sdelphij { "arc_meta_min", KSTAT_DATA_UINT64 }, 686287702Sdelphij { "sync_wait_for_async", KSTAT_DATA_UINT64 }, 687287702Sdelphij { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 }, 688168404Spjd}; 689168404Spjd 690168404Spjd#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 691168404Spjd 692168404Spjd#define ARCSTAT_INCR(stat, val) \ 693251631Sdelphij atomic_add_64(&arc_stats.stat.value.ui64, (val)) 694168404Spjd 695206796Spjd#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 696168404Spjd#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 697168404Spjd 698168404Spjd#define ARCSTAT_MAX(stat, val) { \ 699168404Spjd uint64_t m; \ 700168404Spjd while ((val) > (m = arc_stats.stat.value.ui64) && \ 701168404Spjd (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 702168404Spjd continue; \ 703168404Spjd} 704168404Spjd 705168404Spjd#define ARCSTAT_MAXSTAT(stat) \ 706168404Spjd ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 707168404Spjd 708168404Spjd/* 709168404Spjd * We define a macro to allow ARC hits/misses to be easily broken down by 710168404Spjd * two separate conditions, giving a total of four different subtypes for 711168404Spjd * each of hits and misses (so eight statistics total). 712168404Spjd */ 713168404Spjd#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 714168404Spjd if (cond1) { \ 715168404Spjd if (cond2) { \ 716168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 717168404Spjd } else { \ 718168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 719168404Spjd } \ 720168404Spjd } else { \ 721168404Spjd if (cond2) { \ 722168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 723168404Spjd } else { \ 724168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 725168404Spjd } \ 726168404Spjd } 727168404Spjd 728168404Spjdkstat_t *arc_ksp; 729206796Spjdstatic arc_state_t *arc_anon; 730168404Spjdstatic arc_state_t *arc_mru; 731168404Spjdstatic arc_state_t *arc_mru_ghost; 732168404Spjdstatic arc_state_t *arc_mfu; 733168404Spjdstatic arc_state_t *arc_mfu_ghost; 734185029Spjdstatic arc_state_t *arc_l2c_only; 735168404Spjd 736168404Spjd/* 737168404Spjd * There are several ARC variables that are critical to export as kstats -- 738168404Spjd * but we don't want to have to grovel around in the kstat whenever we wish to 739168404Spjd * manipulate them. For these variables, we therefore define them to be in 740168404Spjd * terms of the statistic variable. This assures that we are not introducing 741168404Spjd * the possibility of inconsistency by having shadow copies of the variables, 742168404Spjd * while still allowing the code to be readable. 743168404Spjd */ 744168404Spjd#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 745168404Spjd#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 746168404Spjd#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 747168404Spjd#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 748168404Spjd#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 749275748Sdelphij#define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ 750275780Sdelphij#define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ 751275748Sdelphij#define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */ 752275748Sdelphij#define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ 753168404Spjd 754251478Sdelphij#define L2ARC_IS_VALID_COMPRESS(_c_) \ 755251478Sdelphij ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY) 756251478Sdelphij 757168404Spjdstatic int arc_no_grow; /* Don't try to grow cache size */ 758168404Spjdstatic uint64_t arc_tempreserve; 759209962Smmstatic uint64_t arc_loaned_bytes; 760168404Spjd 761168404Spjdtypedef struct arc_callback arc_callback_t; 762168404Spjd 763168404Spjdstruct arc_callback { 764168404Spjd void *acb_private; 765168404Spjd arc_done_func_t *acb_done; 766168404Spjd arc_buf_t *acb_buf; 767168404Spjd zio_t *acb_zio_dummy; 768168404Spjd arc_callback_t *acb_next; 769168404Spjd}; 770168404Spjd 771168404Spjdtypedef struct arc_write_callback arc_write_callback_t; 772168404Spjd 773168404Spjdstruct arc_write_callback { 774168404Spjd void *awcb_private; 775168404Spjd arc_done_func_t *awcb_ready; 776258632Savg arc_done_func_t *awcb_physdone; 777168404Spjd arc_done_func_t *awcb_done; 778168404Spjd arc_buf_t *awcb_buf; 779168404Spjd}; 780168404Spjd 781286570Smav/* 782286570Smav * ARC buffers are separated into multiple structs as a memory saving measure: 783286570Smav * - Common fields struct, always defined, and embedded within it: 784286570Smav * - L2-only fields, always allocated but undefined when not in L2ARC 785286570Smav * - L1-only fields, only allocated when in L1ARC 786286570Smav * 787286570Smav * Buffer in L1 Buffer only in L2 788286570Smav * +------------------------+ +------------------------+ 789286570Smav * | arc_buf_hdr_t | | arc_buf_hdr_t | 790286570Smav * | | | | 791286570Smav * | | | | 792286570Smav * | | | | 793286570Smav * +------------------------+ +------------------------+ 794286570Smav * | l2arc_buf_hdr_t | | l2arc_buf_hdr_t | 795286570Smav * | (undefined if L1-only) | | | 796286570Smav * +------------------------+ +------------------------+ 797286570Smav * | l1arc_buf_hdr_t | 798286570Smav * | | 799286570Smav * | | 800286570Smav * | | 801286570Smav * | | 802286570Smav * +------------------------+ 803286570Smav * 804286570Smav * Because it's possible for the L2ARC to become extremely large, we can wind 805286570Smav * up eating a lot of memory in L2ARC buffer headers, so the size of a header 806286570Smav * is minimized by only allocating the fields necessary for an L1-cached buffer 807286570Smav * when a header is actually in the L1 cache. The sub-headers (l1arc_buf_hdr and 808286570Smav * l2arc_buf_hdr) are embedded rather than allocated separately to save a couple 809286570Smav * words in pointers. arc_hdr_realloc() is used to switch a header between 810286570Smav * these two allocation states. 811286570Smav */ 812286570Smavtypedef struct l1arc_buf_hdr { 813168404Spjd kmutex_t b_freeze_lock; 814286570Smav#ifdef ZFS_DEBUG 815286570Smav /* 816286570Smav * used for debugging wtih kmem_flags - by allocating and freeing 817286570Smav * b_thawed when the buffer is thawed, we get a record of the stack 818286570Smav * trace that thawed it. 819286570Smav */ 820219089Spjd void *b_thawed; 821286570Smav#endif 822168404Spjd 823168404Spjd arc_buf_t *b_buf; 824168404Spjd uint32_t b_datacnt; 825286570Smav /* for waiting on writes to complete */ 826168404Spjd kcondvar_t b_cv; 827168404Spjd 828168404Spjd /* protected by arc state mutex */ 829168404Spjd arc_state_t *b_state; 830286763Smav multilist_node_t b_arc_node; 831168404Spjd 832168404Spjd /* updated atomically */ 833168404Spjd clock_t b_arc_access; 834168404Spjd 835168404Spjd /* self protecting */ 836168404Spjd refcount_t b_refcnt; 837185029Spjd 838286570Smav arc_callback_t *b_acb; 839286570Smav /* temporary buffer holder for in-flight compressed data */ 840286570Smav void *b_tmp_cdata; 841286570Smav} l1arc_buf_hdr_t; 842286570Smav 843286570Smavtypedef struct l2arc_dev l2arc_dev_t; 844286570Smav 845286570Smavtypedef struct l2arc_buf_hdr { 846286570Smav /* protected by arc_buf_hdr mutex */ 847286570Smav l2arc_dev_t *b_dev; /* L2ARC device */ 848286570Smav uint64_t b_daddr; /* disk address, offset byte */ 849286570Smav /* real alloc'd buffer size depending on b_compress applied */ 850286570Smav int32_t b_asize; 851287706Sdelphij uint8_t b_compress; 852286570Smav 853185029Spjd list_node_t b_l2node; 854286570Smav} l2arc_buf_hdr_t; 855286570Smav 856286570Smavstruct arc_buf_hdr { 857286570Smav /* protected by hash lock */ 858286570Smav dva_t b_dva; 859286570Smav uint64_t b_birth; 860286570Smav /* 861286570Smav * Even though this checksum is only set/verified when a buffer is in 862286570Smav * the L1 cache, it needs to be in the set of common fields because it 863286570Smav * must be preserved from the time before a buffer is written out to 864286570Smav * L2ARC until after it is read back in. 865286570Smav */ 866286570Smav zio_cksum_t *b_freeze_cksum; 867286570Smav 868286570Smav arc_buf_hdr_t *b_hash_next; 869286570Smav arc_flags_t b_flags; 870286570Smav 871286570Smav /* immutable */ 872286570Smav int32_t b_size; 873286570Smav uint64_t b_spa; 874286570Smav 875286570Smav /* L2ARC fields. Undefined when not in L2ARC. */ 876286570Smav l2arc_buf_hdr_t b_l2hdr; 877286570Smav /* L1ARC fields. Undefined when in l2arc_only state */ 878286570Smav l1arc_buf_hdr_t b_l1hdr; 879168404Spjd}; 880168404Spjd 881275748Sdelphij#ifdef _KERNEL 882275748Sdelphijstatic int 883275748Sdelphijsysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS) 884275748Sdelphij{ 885275748Sdelphij uint64_t val; 886275748Sdelphij int err; 887275748Sdelphij 888275748Sdelphij val = arc_meta_limit; 889275748Sdelphij err = sysctl_handle_64(oidp, &val, 0, req); 890275748Sdelphij if (err != 0 || req->newptr == NULL) 891275748Sdelphij return (err); 892275748Sdelphij 893275748Sdelphij if (val <= 0 || val > arc_c_max) 894275748Sdelphij return (EINVAL); 895275748Sdelphij 896275748Sdelphij arc_meta_limit = val; 897275748Sdelphij return (0); 898275748Sdelphij} 899275748Sdelphij#endif 900275748Sdelphij 901168404Spjdstatic arc_buf_t *arc_eviction_list; 902168404Spjdstatic arc_buf_hdr_t arc_eviction_hdr; 903168404Spjd 904168404Spjd#define GHOST_STATE(state) \ 905185029Spjd ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 906185029Spjd (state) == arc_l2c_only) 907168404Spjd 908275811Sdelphij#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) 909275811Sdelphij#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) 910275811Sdelphij#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) 911275811Sdelphij#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) 912275811Sdelphij#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FLAG_FREED_IN_READ) 913275811Sdelphij#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_FLAG_BUF_AVAILABLE) 914286570Smav 915275811Sdelphij#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) 916286570Smav#define HDR_L2COMPRESS(hdr) ((hdr)->b_flags & ARC_FLAG_L2COMPRESS) 917275811Sdelphij#define HDR_L2_READING(hdr) \ 918286570Smav (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ 919286570Smav ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) 920275811Sdelphij#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) 921275811Sdelphij#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) 922275811Sdelphij#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) 923168404Spjd 924286570Smav#define HDR_ISTYPE_METADATA(hdr) \ 925286570Smav ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) 926286570Smav#define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) 927286570Smav 928286570Smav#define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) 929286570Smav#define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) 930286570Smav 931168404Spjd/* 932185029Spjd * Other sizes 933185029Spjd */ 934185029Spjd 935286570Smav#define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 936286570Smav#define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) 937185029Spjd 938185029Spjd/* 939168404Spjd * Hash table routines 940168404Spjd */ 941168404Spjd 942205253Skmacy#define HT_LOCK_PAD CACHE_LINE_SIZE 943168404Spjd 944168404Spjdstruct ht_lock { 945168404Spjd kmutex_t ht_lock; 946168404Spjd#ifdef _KERNEL 947168404Spjd unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 948168404Spjd#endif 949168404Spjd}; 950168404Spjd 951168404Spjd#define BUF_LOCKS 256 952168404Spjdtypedef struct buf_hash_table { 953168404Spjd uint64_t ht_mask; 954168404Spjd arc_buf_hdr_t **ht_table; 955205264Skmacy struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 956168404Spjd} buf_hash_table_t; 957168404Spjd 958168404Spjdstatic buf_hash_table_t buf_hash_table; 959168404Spjd 960168404Spjd#define BUF_HASH_INDEX(spa, dva, birth) \ 961168404Spjd (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 962168404Spjd#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 963168404Spjd#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 964219089Spjd#define HDR_LOCK(hdr) \ 965219089Spjd (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 966168404Spjd 967168404Spjduint64_t zfs_crc64_table[256]; 968168404Spjd 969185029Spjd/* 970185029Spjd * Level 2 ARC 971185029Spjd */ 972185029Spjd 973272707Savg#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 974251478Sdelphij#define L2ARC_HEADROOM 2 /* num of writes */ 975251478Sdelphij/* 976251478Sdelphij * If we discover during ARC scan any buffers to be compressed, we boost 977251478Sdelphij * our headroom for the next scanning cycle by this percentage multiple. 978251478Sdelphij */ 979251478Sdelphij#define L2ARC_HEADROOM_BOOST 200 980208373Smm#define L2ARC_FEED_SECS 1 /* caching interval secs */ 981208373Smm#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 982185029Spjd 983286598Smav/* 984286598Smav * Used to distinguish headers that are being process by 985286598Smav * l2arc_write_buffers(), but have yet to be assigned to a l2arc disk 986286598Smav * address. This can happen when the header is added to the l2arc's list 987286598Smav * of buffers to write in the first stage of l2arc_write_buffers(), but 988286598Smav * has not yet been written out which happens in the second stage of 989286598Smav * l2arc_write_buffers(). 990286598Smav */ 991286598Smav#define L2ARC_ADDR_UNSET ((uint64_t)(-1)) 992286598Smav 993185029Spjd#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 994185029Spjd#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 995185029Spjd 996251631Sdelphij/* L2ARC Performance Tunables */ 997185029Spjduint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 998185029Spjduint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 999185029Spjduint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 1000251478Sdelphijuint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 1001185029Spjduint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 1002208373Smmuint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 1003219089Spjdboolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 1004208373Smmboolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 1005208373Smmboolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 1006185029Spjd 1007217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 1008205231Skmacy &l2arc_write_max, 0, "max write size"); 1009217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 1010205231Skmacy &l2arc_write_boost, 0, "extra write during warmup"); 1011217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 1012205231Skmacy &l2arc_headroom, 0, "number of dev writes"); 1013217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 1014205231Skmacy &l2arc_feed_secs, 0, "interval seconds"); 1015217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 1016208373Smm &l2arc_feed_min_ms, 0, "min interval milliseconds"); 1017205231Skmacy 1018205231SkmacySYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 1019205231Skmacy &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 1020208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 1021208373Smm &l2arc_feed_again, 0, "turbo warmup"); 1022208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 1023208373Smm &l2arc_norw, 0, "no reads during writes"); 1024205231Skmacy 1025217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 1026286770Smav &ARC_anon.arcs_size.rc_count, 0, "size of anonymous state"); 1027217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD, 1028205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state"); 1029217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD, 1030205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state"); 1031205231Skmacy 1032217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 1033286770Smav &ARC_mru.arcs_size.rc_count, 0, "size of mru state"); 1034217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD, 1035205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state"); 1036217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD, 1037205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state"); 1038205231Skmacy 1039217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 1040286770Smav &ARC_mru_ghost.arcs_size.rc_count, 0, "size of mru ghost state"); 1041217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD, 1042205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 1043205231Skmacy "size of metadata in mru ghost state"); 1044217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD, 1045205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 1046205231Skmacy "size of data in mru ghost state"); 1047205231Skmacy 1048217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 1049286770Smav &ARC_mfu.arcs_size.rc_count, 0, "size of mfu state"); 1050217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD, 1051205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state"); 1052217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD, 1053205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state"); 1054205231Skmacy 1055217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 1056286770Smav &ARC_mfu_ghost.arcs_size.rc_count, 0, "size of mfu ghost state"); 1057217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD, 1058205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 1059205231Skmacy "size of metadata in mfu ghost state"); 1060217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD, 1061205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 1062205231Skmacy "size of data in mfu ghost state"); 1063205231Skmacy 1064217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 1065286770Smav &ARC_l2c_only.arcs_size.rc_count, 0, "size of mru state"); 1066205231Skmacy 1067185029Spjd/* 1068185029Spjd * L2ARC Internals 1069185029Spjd */ 1070286570Smavstruct l2arc_dev { 1071185029Spjd vdev_t *l2ad_vdev; /* vdev */ 1072185029Spjd spa_t *l2ad_spa; /* spa */ 1073185029Spjd uint64_t l2ad_hand; /* next write location */ 1074185029Spjd uint64_t l2ad_start; /* first addr on device */ 1075185029Spjd uint64_t l2ad_end; /* last addr on device */ 1076185029Spjd boolean_t l2ad_first; /* first sweep through */ 1077208373Smm boolean_t l2ad_writing; /* currently writing */ 1078286570Smav kmutex_t l2ad_mtx; /* lock for buffer list */ 1079286570Smav list_t l2ad_buflist; /* buffer list */ 1080185029Spjd list_node_t l2ad_node; /* device list node */ 1081286598Smav refcount_t l2ad_alloc; /* allocated bytes */ 1082286570Smav}; 1083185029Spjd 1084185029Spjdstatic list_t L2ARC_dev_list; /* device list */ 1085185029Spjdstatic list_t *l2arc_dev_list; /* device list pointer */ 1086185029Spjdstatic kmutex_t l2arc_dev_mtx; /* device list mutex */ 1087185029Spjdstatic l2arc_dev_t *l2arc_dev_last; /* last device used */ 1088185029Spjdstatic list_t L2ARC_free_on_write; /* free after write buf list */ 1089185029Spjdstatic list_t *l2arc_free_on_write; /* free after write list ptr */ 1090185029Spjdstatic kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 1091185029Spjdstatic uint64_t l2arc_ndev; /* number of devices */ 1092185029Spjd 1093185029Spjdtypedef struct l2arc_read_callback { 1094251478Sdelphij arc_buf_t *l2rcb_buf; /* read buffer */ 1095251478Sdelphij spa_t *l2rcb_spa; /* spa */ 1096251478Sdelphij blkptr_t l2rcb_bp; /* original blkptr */ 1097268123Sdelphij zbookmark_phys_t l2rcb_zb; /* original bookmark */ 1098251478Sdelphij int l2rcb_flags; /* original flags */ 1099251478Sdelphij enum zio_compress l2rcb_compress; /* applied compress */ 1100185029Spjd} l2arc_read_callback_t; 1101185029Spjd 1102185029Spjdtypedef struct l2arc_write_callback { 1103185029Spjd l2arc_dev_t *l2wcb_dev; /* device info */ 1104185029Spjd arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 1105185029Spjd} l2arc_write_callback_t; 1106185029Spjd 1107185029Spjdtypedef struct l2arc_data_free { 1108185029Spjd /* protected by l2arc_free_on_write_mtx */ 1109185029Spjd void *l2df_data; 1110185029Spjd size_t l2df_size; 1111185029Spjd void (*l2df_func)(void *, size_t); 1112185029Spjd list_node_t l2df_list_node; 1113185029Spjd} l2arc_data_free_t; 1114185029Spjd 1115185029Spjdstatic kmutex_t l2arc_feed_thr_lock; 1116185029Spjdstatic kcondvar_t l2arc_feed_thr_cv; 1117185029Spjdstatic uint8_t l2arc_thread_exit; 1118185029Spjd 1119275811Sdelphijstatic void arc_get_data_buf(arc_buf_t *); 1120275811Sdelphijstatic void arc_access(arc_buf_hdr_t *, kmutex_t *); 1121286763Smavstatic boolean_t arc_is_overflowing(); 1122275811Sdelphijstatic void arc_buf_watch(arc_buf_t *); 1123275811Sdelphij 1124286570Smavstatic arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); 1125286570Smavstatic uint32_t arc_bufc_to_flags(arc_buf_contents_t); 1126286570Smav 1127275811Sdelphijstatic boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); 1128275811Sdelphijstatic void l2arc_read_done(zio_t *); 1129185029Spjd 1130286570Smavstatic boolean_t l2arc_compress_buf(arc_buf_hdr_t *); 1131275811Sdelphijstatic void l2arc_decompress_zio(zio_t *, arc_buf_hdr_t *, enum zio_compress); 1132275811Sdelphijstatic void l2arc_release_cdata_buf(arc_buf_hdr_t *); 1133251478Sdelphij 1134168404Spjdstatic uint64_t 1135209962Smmbuf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 1136168404Spjd{ 1137168404Spjd uint8_t *vdva = (uint8_t *)dva; 1138168404Spjd uint64_t crc = -1ULL; 1139168404Spjd int i; 1140168404Spjd 1141168404Spjd ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 1142168404Spjd 1143168404Spjd for (i = 0; i < sizeof (dva_t); i++) 1144168404Spjd crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 1145168404Spjd 1146209962Smm crc ^= (spa>>8) ^ birth; 1147168404Spjd 1148168404Spjd return (crc); 1149168404Spjd} 1150168404Spjd 1151168404Spjd#define BUF_EMPTY(buf) \ 1152168404Spjd ((buf)->b_dva.dva_word[0] == 0 && \ 1153286570Smav (buf)->b_dva.dva_word[1] == 0) 1154168404Spjd 1155168404Spjd#define BUF_EQUAL(spa, dva, birth, buf) \ 1156168404Spjd ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 1157168404Spjd ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 1158168404Spjd ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 1159168404Spjd 1160219089Spjdstatic void 1161219089Spjdbuf_discard_identity(arc_buf_hdr_t *hdr) 1162219089Spjd{ 1163219089Spjd hdr->b_dva.dva_word[0] = 0; 1164219089Spjd hdr->b_dva.dva_word[1] = 0; 1165219089Spjd hdr->b_birth = 0; 1166219089Spjd} 1167219089Spjd 1168168404Spjdstatic arc_buf_hdr_t * 1169268075Sdelphijbuf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 1170168404Spjd{ 1171268075Sdelphij const dva_t *dva = BP_IDENTITY(bp); 1172268075Sdelphij uint64_t birth = BP_PHYSICAL_BIRTH(bp); 1173168404Spjd uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 1174168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1175275811Sdelphij arc_buf_hdr_t *hdr; 1176168404Spjd 1177168404Spjd mutex_enter(hash_lock); 1178275811Sdelphij for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; 1179275811Sdelphij hdr = hdr->b_hash_next) { 1180275811Sdelphij if (BUF_EQUAL(spa, dva, birth, hdr)) { 1181168404Spjd *lockp = hash_lock; 1182275811Sdelphij return (hdr); 1183168404Spjd } 1184168404Spjd } 1185168404Spjd mutex_exit(hash_lock); 1186168404Spjd *lockp = NULL; 1187168404Spjd return (NULL); 1188168404Spjd} 1189168404Spjd 1190168404Spjd/* 1191168404Spjd * Insert an entry into the hash table. If there is already an element 1192168404Spjd * equal to elem in the hash table, then the already existing element 1193168404Spjd * will be returned and the new element will not be inserted. 1194168404Spjd * Otherwise returns NULL. 1195286570Smav * If lockp == NULL, the caller is assumed to already hold the hash lock. 1196168404Spjd */ 1197168404Spjdstatic arc_buf_hdr_t * 1198275811Sdelphijbuf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) 1199168404Spjd{ 1200275811Sdelphij uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1201168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1202275811Sdelphij arc_buf_hdr_t *fhdr; 1203168404Spjd uint32_t i; 1204168404Spjd 1205275811Sdelphij ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); 1206275811Sdelphij ASSERT(hdr->b_birth != 0); 1207275811Sdelphij ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1208286570Smav 1209286570Smav if (lockp != NULL) { 1210286570Smav *lockp = hash_lock; 1211286570Smav mutex_enter(hash_lock); 1212286570Smav } else { 1213286570Smav ASSERT(MUTEX_HELD(hash_lock)); 1214286570Smav } 1215286570Smav 1216275811Sdelphij for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; 1217275811Sdelphij fhdr = fhdr->b_hash_next, i++) { 1218275811Sdelphij if (BUF_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) 1219275811Sdelphij return (fhdr); 1220168404Spjd } 1221168404Spjd 1222275811Sdelphij hdr->b_hash_next = buf_hash_table.ht_table[idx]; 1223275811Sdelphij buf_hash_table.ht_table[idx] = hdr; 1224275811Sdelphij hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; 1225168404Spjd 1226168404Spjd /* collect some hash table performance data */ 1227168404Spjd if (i > 0) { 1228168404Spjd ARCSTAT_BUMP(arcstat_hash_collisions); 1229168404Spjd if (i == 1) 1230168404Spjd ARCSTAT_BUMP(arcstat_hash_chains); 1231168404Spjd 1232168404Spjd ARCSTAT_MAX(arcstat_hash_chain_max, i); 1233168404Spjd } 1234168404Spjd 1235168404Spjd ARCSTAT_BUMP(arcstat_hash_elements); 1236168404Spjd ARCSTAT_MAXSTAT(arcstat_hash_elements); 1237168404Spjd 1238168404Spjd return (NULL); 1239168404Spjd} 1240168404Spjd 1241168404Spjdstatic void 1242275811Sdelphijbuf_hash_remove(arc_buf_hdr_t *hdr) 1243168404Spjd{ 1244275811Sdelphij arc_buf_hdr_t *fhdr, **hdrp; 1245275811Sdelphij uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1246168404Spjd 1247168404Spjd ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 1248275811Sdelphij ASSERT(HDR_IN_HASH_TABLE(hdr)); 1249168404Spjd 1250275811Sdelphij hdrp = &buf_hash_table.ht_table[idx]; 1251275811Sdelphij while ((fhdr = *hdrp) != hdr) { 1252275811Sdelphij ASSERT(fhdr != NULL); 1253275811Sdelphij hdrp = &fhdr->b_hash_next; 1254168404Spjd } 1255275811Sdelphij *hdrp = hdr->b_hash_next; 1256275811Sdelphij hdr->b_hash_next = NULL; 1257275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IN_HASH_TABLE; 1258168404Spjd 1259168404Spjd /* collect some hash table performance data */ 1260168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_elements); 1261168404Spjd 1262168404Spjd if (buf_hash_table.ht_table[idx] && 1263168404Spjd buf_hash_table.ht_table[idx]->b_hash_next == NULL) 1264168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1265168404Spjd} 1266168404Spjd 1267168404Spjd/* 1268168404Spjd * Global data structures and functions for the buf kmem cache. 1269168404Spjd */ 1270286570Smavstatic kmem_cache_t *hdr_full_cache; 1271286570Smavstatic kmem_cache_t *hdr_l2only_cache; 1272168404Spjdstatic kmem_cache_t *buf_cache; 1273168404Spjd 1274168404Spjdstatic void 1275168404Spjdbuf_fini(void) 1276168404Spjd{ 1277168404Spjd int i; 1278168404Spjd 1279168404Spjd kmem_free(buf_hash_table.ht_table, 1280168404Spjd (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1281168404Spjd for (i = 0; i < BUF_LOCKS; i++) 1282168404Spjd mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 1283286570Smav kmem_cache_destroy(hdr_full_cache); 1284286570Smav kmem_cache_destroy(hdr_l2only_cache); 1285168404Spjd kmem_cache_destroy(buf_cache); 1286168404Spjd} 1287168404Spjd 1288168404Spjd/* 1289168404Spjd * Constructor callback - called when the cache is empty 1290168404Spjd * and a new buf is requested. 1291168404Spjd */ 1292168404Spjd/* ARGSUSED */ 1293168404Spjdstatic int 1294286570Smavhdr_full_cons(void *vbuf, void *unused, int kmflag) 1295168404Spjd{ 1296275811Sdelphij arc_buf_hdr_t *hdr = vbuf; 1297168404Spjd 1298286570Smav bzero(hdr, HDR_FULL_SIZE); 1299286570Smav cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); 1300286570Smav refcount_create(&hdr->b_l1hdr.b_refcnt); 1301286570Smav mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1302286763Smav multilist_link_init(&hdr->b_l1hdr.b_arc_node); 1303286570Smav arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1304185029Spjd 1305168404Spjd return (0); 1306168404Spjd} 1307168404Spjd 1308185029Spjd/* ARGSUSED */ 1309185029Spjdstatic int 1310286570Smavhdr_l2only_cons(void *vbuf, void *unused, int kmflag) 1311286570Smav{ 1312286570Smav arc_buf_hdr_t *hdr = vbuf; 1313286570Smav 1314286570Smav bzero(hdr, HDR_L2ONLY_SIZE); 1315286570Smav arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1316286570Smav 1317286570Smav return (0); 1318286570Smav} 1319286570Smav 1320286570Smav/* ARGSUSED */ 1321286570Smavstatic int 1322185029Spjdbuf_cons(void *vbuf, void *unused, int kmflag) 1323185029Spjd{ 1324185029Spjd arc_buf_t *buf = vbuf; 1325185029Spjd 1326185029Spjd bzero(buf, sizeof (arc_buf_t)); 1327219089Spjd mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 1328208373Smm arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1329208373Smm 1330185029Spjd return (0); 1331185029Spjd} 1332185029Spjd 1333168404Spjd/* 1334168404Spjd * Destructor callback - called when a cached buf is 1335168404Spjd * no longer required. 1336168404Spjd */ 1337168404Spjd/* ARGSUSED */ 1338168404Spjdstatic void 1339286570Smavhdr_full_dest(void *vbuf, void *unused) 1340168404Spjd{ 1341275811Sdelphij arc_buf_hdr_t *hdr = vbuf; 1342168404Spjd 1343275811Sdelphij ASSERT(BUF_EMPTY(hdr)); 1344286570Smav cv_destroy(&hdr->b_l1hdr.b_cv); 1345286570Smav refcount_destroy(&hdr->b_l1hdr.b_refcnt); 1346286570Smav mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); 1347286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 1348286570Smav arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1349168404Spjd} 1350168404Spjd 1351185029Spjd/* ARGSUSED */ 1352185029Spjdstatic void 1353286570Smavhdr_l2only_dest(void *vbuf, void *unused) 1354286570Smav{ 1355286570Smav arc_buf_hdr_t *hdr = vbuf; 1356286570Smav 1357286570Smav ASSERT(BUF_EMPTY(hdr)); 1358286570Smav arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1359286570Smav} 1360286570Smav 1361286570Smav/* ARGSUSED */ 1362286570Smavstatic void 1363185029Spjdbuf_dest(void *vbuf, void *unused) 1364185029Spjd{ 1365185029Spjd arc_buf_t *buf = vbuf; 1366185029Spjd 1367219089Spjd mutex_destroy(&buf->b_evict_lock); 1368208373Smm arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1369185029Spjd} 1370185029Spjd 1371168404Spjd/* 1372168404Spjd * Reclaim callback -- invoked when memory is low. 1373168404Spjd */ 1374168404Spjd/* ARGSUSED */ 1375168404Spjdstatic void 1376168404Spjdhdr_recl(void *unused) 1377168404Spjd{ 1378168404Spjd dprintf("hdr_recl called\n"); 1379168404Spjd /* 1380168404Spjd * umem calls the reclaim func when we destroy the buf cache, 1381168404Spjd * which is after we do arc_fini(). 1382168404Spjd */ 1383168404Spjd if (!arc_dead) 1384286763Smav cv_signal(&arc_reclaim_thread_cv); 1385168404Spjd} 1386168404Spjd 1387168404Spjdstatic void 1388168404Spjdbuf_init(void) 1389168404Spjd{ 1390168404Spjd uint64_t *ct; 1391168404Spjd uint64_t hsize = 1ULL << 12; 1392168404Spjd int i, j; 1393168404Spjd 1394168404Spjd /* 1395168404Spjd * The hash table is big enough to fill all of physical memory 1396269230Sdelphij * with an average block size of zfs_arc_average_blocksize (default 8K). 1397269230Sdelphij * By default, the table will take up 1398269230Sdelphij * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1399168404Spjd */ 1400269230Sdelphij while (hsize * zfs_arc_average_blocksize < (uint64_t)physmem * PAGESIZE) 1401168404Spjd hsize <<= 1; 1402168404Spjdretry: 1403168404Spjd buf_hash_table.ht_mask = hsize - 1; 1404168404Spjd buf_hash_table.ht_table = 1405168404Spjd kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1406168404Spjd if (buf_hash_table.ht_table == NULL) { 1407168404Spjd ASSERT(hsize > (1ULL << 8)); 1408168404Spjd hsize >>= 1; 1409168404Spjd goto retry; 1410168404Spjd } 1411168404Spjd 1412286570Smav hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, 1413286570Smav 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0); 1414286570Smav hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", 1415286570Smav HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl, 1416286570Smav NULL, NULL, 0); 1417168404Spjd buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1418185029Spjd 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1419168404Spjd 1420168404Spjd for (i = 0; i < 256; i++) 1421168404Spjd for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1422168404Spjd *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1423168404Spjd 1424168404Spjd for (i = 0; i < BUF_LOCKS; i++) { 1425168404Spjd mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1426168404Spjd NULL, MUTEX_DEFAULT, NULL); 1427168404Spjd } 1428168404Spjd} 1429168404Spjd 1430286570Smav/* 1431286570Smav * Transition between the two allocation states for the arc_buf_hdr struct. 1432286570Smav * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without 1433286570Smav * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller 1434286570Smav * version is used when a cache buffer is only in the L2ARC in order to reduce 1435286570Smav * memory usage. 1436286570Smav */ 1437286570Smavstatic arc_buf_hdr_t * 1438286570Smavarc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) 1439286570Smav{ 1440286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 1441286570Smav 1442286570Smav arc_buf_hdr_t *nhdr; 1443286570Smav l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 1444286570Smav 1445286570Smav ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || 1446286570Smav (old == hdr_l2only_cache && new == hdr_full_cache)); 1447286570Smav 1448286570Smav nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); 1449286570Smav 1450286570Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 1451286570Smav buf_hash_remove(hdr); 1452286570Smav 1453286570Smav bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); 1454286598Smav 1455286570Smav if (new == hdr_full_cache) { 1456286570Smav nhdr->b_flags |= ARC_FLAG_HAS_L1HDR; 1457286570Smav /* 1458286570Smav * arc_access and arc_change_state need to be aware that a 1459286570Smav * header has just come out of L2ARC, so we set its state to 1460286570Smav * l2c_only even though it's about to change. 1461286570Smav */ 1462286570Smav nhdr->b_l1hdr.b_state = arc_l2c_only; 1463286763Smav 1464286763Smav /* Verify previous threads set to NULL before freeing */ 1465286763Smav ASSERT3P(nhdr->b_l1hdr.b_tmp_cdata, ==, NULL); 1466286570Smav } else { 1467286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 1468286570Smav ASSERT0(hdr->b_l1hdr.b_datacnt); 1469286763Smav 1470286570Smav /* 1471286763Smav * If we've reached here, We must have been called from 1472286763Smav * arc_evict_hdr(), as such we should have already been 1473286763Smav * removed from any ghost list we were previously on 1474286763Smav * (which protects us from racing with arc_evict_state), 1475286763Smav * thus no locking is needed during this check. 1476286570Smav */ 1477286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 1478286763Smav 1479286763Smav /* 1480286763Smav * A buffer must not be moved into the arc_l2c_only 1481286763Smav * state if it's not finished being written out to the 1482286763Smav * l2arc device. Otherwise, the b_l1hdr.b_tmp_cdata field 1483286763Smav * might try to be accessed, even though it was removed. 1484286763Smav */ 1485286763Smav VERIFY(!HDR_L2_WRITING(hdr)); 1486286763Smav VERIFY3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 1487286763Smav 1488288064Savg#ifdef ZFS_DEBUG 1489288064Savg if (hdr->b_l1hdr.b_thawed != NULL) { 1490288064Savg kmem_free(hdr->b_l1hdr.b_thawed, 1); 1491288064Savg hdr->b_l1hdr.b_thawed = NULL; 1492288064Savg } 1493288064Savg#endif 1494288064Savg 1495286570Smav nhdr->b_flags &= ~ARC_FLAG_HAS_L1HDR; 1496286570Smav } 1497286570Smav /* 1498286570Smav * The header has been reallocated so we need to re-insert it into any 1499286570Smav * lists it was on. 1500286570Smav */ 1501286570Smav (void) buf_hash_insert(nhdr, NULL); 1502286570Smav 1503286570Smav ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); 1504286570Smav 1505286570Smav mutex_enter(&dev->l2ad_mtx); 1506286570Smav 1507286570Smav /* 1508286570Smav * We must place the realloc'ed header back into the list at 1509286570Smav * the same spot. Otherwise, if it's placed earlier in the list, 1510286570Smav * l2arc_write_buffers() could find it during the function's 1511286570Smav * write phase, and try to write it out to the l2arc. 1512286570Smav */ 1513286570Smav list_insert_after(&dev->l2ad_buflist, hdr, nhdr); 1514286570Smav list_remove(&dev->l2ad_buflist, hdr); 1515286570Smav 1516286570Smav mutex_exit(&dev->l2ad_mtx); 1517286570Smav 1518286598Smav /* 1519286598Smav * Since we're using the pointer address as the tag when 1520286598Smav * incrementing and decrementing the l2ad_alloc refcount, we 1521286598Smav * must remove the old pointer (that we're about to destroy) and 1522286598Smav * add the new pointer to the refcount. Otherwise we'd remove 1523286598Smav * the wrong pointer address when calling arc_hdr_destroy() later. 1524286598Smav */ 1525286598Smav 1526286598Smav (void) refcount_remove_many(&dev->l2ad_alloc, 1527286598Smav hdr->b_l2hdr.b_asize, hdr); 1528286598Smav 1529286598Smav (void) refcount_add_many(&dev->l2ad_alloc, 1530286598Smav nhdr->b_l2hdr.b_asize, nhdr); 1531286598Smav 1532286570Smav buf_discard_identity(hdr); 1533286570Smav hdr->b_freeze_cksum = NULL; 1534286570Smav kmem_cache_free(old, hdr); 1535286570Smav 1536286570Smav return (nhdr); 1537286570Smav} 1538286570Smav 1539286570Smav 1540168404Spjd#define ARC_MINTIME (hz>>4) /* 62 ms */ 1541168404Spjd 1542168404Spjdstatic void 1543168404Spjdarc_cksum_verify(arc_buf_t *buf) 1544168404Spjd{ 1545168404Spjd zio_cksum_t zc; 1546168404Spjd 1547168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1548168404Spjd return; 1549168404Spjd 1550286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1551286570Smav if (buf->b_hdr->b_freeze_cksum == NULL || HDR_IO_ERROR(buf->b_hdr)) { 1552286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1553168404Spjd return; 1554168404Spjd } 1555168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1556168404Spjd if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 1557168404Spjd panic("buffer modified while frozen!"); 1558286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1559168404Spjd} 1560168404Spjd 1561185029Spjdstatic int 1562185029Spjdarc_cksum_equal(arc_buf_t *buf) 1563185029Spjd{ 1564185029Spjd zio_cksum_t zc; 1565185029Spjd int equal; 1566185029Spjd 1567286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1568185029Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1569185029Spjd equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 1570286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1571185029Spjd 1572185029Spjd return (equal); 1573185029Spjd} 1574185029Spjd 1575168404Spjdstatic void 1576185029Spjdarc_cksum_compute(arc_buf_t *buf, boolean_t force) 1577168404Spjd{ 1578185029Spjd if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 1579168404Spjd return; 1580168404Spjd 1581286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1582168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1583286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1584168404Spjd return; 1585168404Spjd } 1586168404Spjd buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 1587168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 1588168404Spjd buf->b_hdr->b_freeze_cksum); 1589286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1590240133Smm#ifdef illumos 1591240133Smm arc_buf_watch(buf); 1592277300Ssmh#endif 1593168404Spjd} 1594168404Spjd 1595240133Smm#ifdef illumos 1596240133Smm#ifndef _KERNEL 1597240133Smmtypedef struct procctl { 1598240133Smm long cmd; 1599240133Smm prwatch_t prwatch; 1600240133Smm} procctl_t; 1601240133Smm#endif 1602240133Smm 1603240133Smm/* ARGSUSED */ 1604240133Smmstatic void 1605240133Smmarc_buf_unwatch(arc_buf_t *buf) 1606240133Smm{ 1607240133Smm#ifndef _KERNEL 1608240133Smm if (arc_watch) { 1609240133Smm int result; 1610240133Smm procctl_t ctl; 1611240133Smm ctl.cmd = PCWATCH; 1612240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1613240133Smm ctl.prwatch.pr_size = 0; 1614240133Smm ctl.prwatch.pr_wflags = 0; 1615240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1616240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1617240133Smm } 1618240133Smm#endif 1619240133Smm} 1620240133Smm 1621240133Smm/* ARGSUSED */ 1622240133Smmstatic void 1623240133Smmarc_buf_watch(arc_buf_t *buf) 1624240133Smm{ 1625240133Smm#ifndef _KERNEL 1626240133Smm if (arc_watch) { 1627240133Smm int result; 1628240133Smm procctl_t ctl; 1629240133Smm ctl.cmd = PCWATCH; 1630240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1631240133Smm ctl.prwatch.pr_size = buf->b_hdr->b_size; 1632240133Smm ctl.prwatch.pr_wflags = WA_WRITE; 1633240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1634240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1635240133Smm } 1636240133Smm#endif 1637240133Smm} 1638240133Smm#endif /* illumos */ 1639240133Smm 1640286570Smavstatic arc_buf_contents_t 1641286570Smavarc_buf_type(arc_buf_hdr_t *hdr) 1642286570Smav{ 1643286570Smav if (HDR_ISTYPE_METADATA(hdr)) { 1644286570Smav return (ARC_BUFC_METADATA); 1645286570Smav } else { 1646286570Smav return (ARC_BUFC_DATA); 1647286570Smav } 1648286570Smav} 1649286570Smav 1650286570Smavstatic uint32_t 1651286570Smavarc_bufc_to_flags(arc_buf_contents_t type) 1652286570Smav{ 1653286570Smav switch (type) { 1654286570Smav case ARC_BUFC_DATA: 1655286570Smav /* metadata field is 0 if buffer contains normal data */ 1656286570Smav return (0); 1657286570Smav case ARC_BUFC_METADATA: 1658286570Smav return (ARC_FLAG_BUFC_METADATA); 1659286570Smav default: 1660286570Smav break; 1661286570Smav } 1662286570Smav panic("undefined ARC buffer type!"); 1663286570Smav return ((uint32_t)-1); 1664286570Smav} 1665286570Smav 1666168404Spjdvoid 1667168404Spjdarc_buf_thaw(arc_buf_t *buf) 1668168404Spjd{ 1669185029Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1670286570Smav if (buf->b_hdr->b_l1hdr.b_state != arc_anon) 1671185029Spjd panic("modifying non-anon buffer!"); 1672286570Smav if (HDR_IO_IN_PROGRESS(buf->b_hdr)) 1673185029Spjd panic("modifying buffer while i/o in progress!"); 1674185029Spjd arc_cksum_verify(buf); 1675185029Spjd } 1676168404Spjd 1677286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1678168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1679168404Spjd kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1680168404Spjd buf->b_hdr->b_freeze_cksum = NULL; 1681168404Spjd } 1682219089Spjd 1683286570Smav#ifdef ZFS_DEBUG 1684219089Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1685286570Smav if (buf->b_hdr->b_l1hdr.b_thawed != NULL) 1686286570Smav kmem_free(buf->b_hdr->b_l1hdr.b_thawed, 1); 1687286570Smav buf->b_hdr->b_l1hdr.b_thawed = kmem_alloc(1, KM_SLEEP); 1688219089Spjd } 1689286570Smav#endif 1690219089Spjd 1691286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1692240133Smm 1693240133Smm#ifdef illumos 1694240133Smm arc_buf_unwatch(buf); 1695277300Ssmh#endif 1696168404Spjd} 1697168404Spjd 1698168404Spjdvoid 1699168404Spjdarc_buf_freeze(arc_buf_t *buf) 1700168404Spjd{ 1701219089Spjd kmutex_t *hash_lock; 1702219089Spjd 1703168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1704168404Spjd return; 1705168404Spjd 1706219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1707219089Spjd mutex_enter(hash_lock); 1708219089Spjd 1709168404Spjd ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 1710286570Smav buf->b_hdr->b_l1hdr.b_state == arc_anon); 1711185029Spjd arc_cksum_compute(buf, B_FALSE); 1712219089Spjd mutex_exit(hash_lock); 1713240133Smm 1714168404Spjd} 1715168404Spjd 1716168404Spjdstatic void 1717275811Sdelphijadd_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1718168404Spjd{ 1719286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1720168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1721286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 1722168404Spjd 1723286570Smav if ((refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && 1724286570Smav (state != arc_anon)) { 1725286570Smav /* We don't use the L2-only state list. */ 1726286570Smav if (state != arc_l2c_only) { 1727286763Smav arc_buf_contents_t type = arc_buf_type(hdr); 1728286570Smav uint64_t delta = hdr->b_size * hdr->b_l1hdr.b_datacnt; 1729286763Smav multilist_t *list = &state->arcs_list[type]; 1730286763Smav uint64_t *size = &state->arcs_lsize[type]; 1731168404Spjd 1732286763Smav multilist_remove(list, hdr); 1733286763Smav 1734286570Smav if (GHOST_STATE(state)) { 1735286570Smav ASSERT0(hdr->b_l1hdr.b_datacnt); 1736286570Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 1737286570Smav delta = hdr->b_size; 1738286570Smav } 1739286570Smav ASSERT(delta > 0); 1740286570Smav ASSERT3U(*size, >=, delta); 1741286570Smav atomic_add_64(size, -delta); 1742168404Spjd } 1743185029Spjd /* remove the prefetch flag if we get a reference */ 1744286570Smav hdr->b_flags &= ~ARC_FLAG_PREFETCH; 1745168404Spjd } 1746168404Spjd} 1747168404Spjd 1748168404Spjdstatic int 1749275811Sdelphijremove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1750168404Spjd{ 1751168404Spjd int cnt; 1752286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 1753168404Spjd 1754286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1755168404Spjd ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1756168404Spjd ASSERT(!GHOST_STATE(state)); 1757168404Spjd 1758286570Smav /* 1759286570Smav * arc_l2c_only counts as a ghost state so we don't need to explicitly 1760286570Smav * check to prevent usage of the arc_l2c_only list. 1761286570Smav */ 1762286570Smav if (((cnt = refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && 1763168404Spjd (state != arc_anon)) { 1764286763Smav arc_buf_contents_t type = arc_buf_type(hdr); 1765286763Smav multilist_t *list = &state->arcs_list[type]; 1766286763Smav uint64_t *size = &state->arcs_lsize[type]; 1767185029Spjd 1768286763Smav multilist_insert(list, hdr); 1769286763Smav 1770286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 1771286570Smav atomic_add_64(size, hdr->b_size * 1772286570Smav hdr->b_l1hdr.b_datacnt); 1773168404Spjd } 1774168404Spjd return (cnt); 1775168404Spjd} 1776168404Spjd 1777168404Spjd/* 1778286763Smav * Move the supplied buffer to the indicated state. The hash lock 1779168404Spjd * for the buffer must be held by the caller. 1780168404Spjd */ 1781168404Spjdstatic void 1782275811Sdelphijarc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, 1783275811Sdelphij kmutex_t *hash_lock) 1784168404Spjd{ 1785286570Smav arc_state_t *old_state; 1786286570Smav int64_t refcnt; 1787286570Smav uint32_t datacnt; 1788168404Spjd uint64_t from_delta, to_delta; 1789286570Smav arc_buf_contents_t buftype = arc_buf_type(hdr); 1790168404Spjd 1791286570Smav /* 1792286570Smav * We almost always have an L1 hdr here, since we call arc_hdr_realloc() 1793286570Smav * in arc_read() when bringing a buffer out of the L2ARC. However, the 1794286570Smav * L1 hdr doesn't always exist when we change state to arc_anon before 1795286570Smav * destroying a header, in which case reallocating to add the L1 hdr is 1796286570Smav * pointless. 1797286570Smav */ 1798286570Smav if (HDR_HAS_L1HDR(hdr)) { 1799286570Smav old_state = hdr->b_l1hdr.b_state; 1800286570Smav refcnt = refcount_count(&hdr->b_l1hdr.b_refcnt); 1801286570Smav datacnt = hdr->b_l1hdr.b_datacnt; 1802286570Smav } else { 1803286570Smav old_state = arc_l2c_only; 1804286570Smav refcnt = 0; 1805286570Smav datacnt = 0; 1806286570Smav } 1807286570Smav 1808168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1809258632Savg ASSERT3P(new_state, !=, old_state); 1810286570Smav ASSERT(refcnt == 0 || datacnt > 0); 1811286570Smav ASSERT(!GHOST_STATE(new_state) || datacnt == 0); 1812286570Smav ASSERT(old_state != arc_anon || datacnt <= 1); 1813168404Spjd 1814286570Smav from_delta = to_delta = datacnt * hdr->b_size; 1815168404Spjd 1816168404Spjd /* 1817168404Spjd * If this buffer is evictable, transfer it from the 1818168404Spjd * old state list to the new state list. 1819168404Spjd */ 1820168404Spjd if (refcnt == 0) { 1821286570Smav if (old_state != arc_anon && old_state != arc_l2c_only) { 1822286570Smav uint64_t *size = &old_state->arcs_lsize[buftype]; 1823168404Spjd 1824286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1825286763Smav multilist_remove(&old_state->arcs_list[buftype], hdr); 1826168404Spjd 1827168404Spjd /* 1828168404Spjd * If prefetching out of the ghost cache, 1829219089Spjd * we will have a non-zero datacnt. 1830168404Spjd */ 1831286570Smav if (GHOST_STATE(old_state) && datacnt == 0) { 1832168404Spjd /* ghost elements have a ghost size */ 1833286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 1834275811Sdelphij from_delta = hdr->b_size; 1835168404Spjd } 1836185029Spjd ASSERT3U(*size, >=, from_delta); 1837185029Spjd atomic_add_64(size, -from_delta); 1838168404Spjd } 1839286570Smav if (new_state != arc_anon && new_state != arc_l2c_only) { 1840286570Smav uint64_t *size = &new_state->arcs_lsize[buftype]; 1841168404Spjd 1842286570Smav /* 1843286570Smav * An L1 header always exists here, since if we're 1844286570Smav * moving to some L1-cached state (i.e. not l2c_only or 1845286570Smav * anonymous), we realloc the header to add an L1hdr 1846286570Smav * beforehand. 1847286570Smav */ 1848286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1849286763Smav multilist_insert(&new_state->arcs_list[buftype], hdr); 1850168404Spjd 1851168404Spjd /* ghost elements have a ghost size */ 1852168404Spjd if (GHOST_STATE(new_state)) { 1853286762Smav ASSERT0(datacnt); 1854286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 1855275811Sdelphij to_delta = hdr->b_size; 1856168404Spjd } 1857185029Spjd atomic_add_64(size, to_delta); 1858168404Spjd } 1859168404Spjd } 1860168404Spjd 1861275811Sdelphij ASSERT(!BUF_EMPTY(hdr)); 1862275811Sdelphij if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) 1863275811Sdelphij buf_hash_remove(hdr); 1864168404Spjd 1865286570Smav /* adjust state sizes (ignore arc_l2c_only) */ 1866286766Smav 1867286766Smav if (to_delta && new_state != arc_l2c_only) { 1868286766Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1869286766Smav if (GHOST_STATE(new_state)) { 1870286766Smav ASSERT0(datacnt); 1871286766Smav 1872286766Smav /* 1873286766Smav * We moving a header to a ghost state, we first 1874286766Smav * remove all arc buffers. Thus, we'll have a 1875286766Smav * datacnt of zero, and no arc buffer to use for 1876286766Smav * the reference. As a result, we use the arc 1877286766Smav * header pointer for the reference. 1878286766Smav */ 1879286766Smav (void) refcount_add_many(&new_state->arcs_size, 1880286766Smav hdr->b_size, hdr); 1881286766Smav } else { 1882286766Smav ASSERT3U(datacnt, !=, 0); 1883286766Smav 1884286766Smav /* 1885286766Smav * Each individual buffer holds a unique reference, 1886286766Smav * thus we must remove each of these references one 1887286766Smav * at a time. 1888286766Smav */ 1889286766Smav for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 1890286766Smav buf = buf->b_next) { 1891286766Smav (void) refcount_add_many(&new_state->arcs_size, 1892286766Smav hdr->b_size, buf); 1893286766Smav } 1894286766Smav } 1895286766Smav } 1896286766Smav 1897286570Smav if (from_delta && old_state != arc_l2c_only) { 1898286766Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1899286766Smav if (GHOST_STATE(old_state)) { 1900286766Smav /* 1901286766Smav * When moving a header off of a ghost state, 1902286766Smav * there's the possibility for datacnt to be 1903286766Smav * non-zero. This is because we first add the 1904286766Smav * arc buffer to the header prior to changing 1905286766Smav * the header's state. Since we used the header 1906286766Smav * for the reference when putting the header on 1907286766Smav * the ghost state, we must balance that and use 1908286766Smav * the header when removing off the ghost state 1909286766Smav * (even though datacnt is non zero). 1910286766Smav */ 1911286766Smav 1912286766Smav IMPLY(datacnt == 0, new_state == arc_anon || 1913286766Smav new_state == arc_l2c_only); 1914286766Smav 1915286766Smav (void) refcount_remove_many(&old_state->arcs_size, 1916286766Smav hdr->b_size, hdr); 1917286766Smav } else { 1918286766Smav ASSERT3P(datacnt, !=, 0); 1919286766Smav 1920286766Smav /* 1921286766Smav * Each individual buffer holds a unique reference, 1922286766Smav * thus we must remove each of these references one 1923286766Smav * at a time. 1924286766Smav */ 1925286766Smav for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 1926286766Smav buf = buf->b_next) { 1927286766Smav (void) refcount_remove_many( 1928286766Smav &old_state->arcs_size, hdr->b_size, buf); 1929286766Smav } 1930286766Smav } 1931168404Spjd } 1932286766Smav 1933286570Smav if (HDR_HAS_L1HDR(hdr)) 1934286570Smav hdr->b_l1hdr.b_state = new_state; 1935185029Spjd 1936286570Smav /* 1937286570Smav * L2 headers should never be on the L2 state list since they don't 1938286570Smav * have L1 headers allocated. 1939286570Smav */ 1940286763Smav ASSERT(multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && 1941286763Smav multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); 1942168404Spjd} 1943168404Spjd 1944185029Spjdvoid 1945208373Smmarc_space_consume(uint64_t space, arc_space_type_t type) 1946185029Spjd{ 1947208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1948208373Smm 1949208373Smm switch (type) { 1950208373Smm case ARC_SPACE_DATA: 1951208373Smm ARCSTAT_INCR(arcstat_data_size, space); 1952208373Smm break; 1953286574Smav case ARC_SPACE_META: 1954286574Smav ARCSTAT_INCR(arcstat_metadata_size, space); 1955286574Smav break; 1956208373Smm case ARC_SPACE_OTHER: 1957208373Smm ARCSTAT_INCR(arcstat_other_size, space); 1958208373Smm break; 1959208373Smm case ARC_SPACE_HDRS: 1960208373Smm ARCSTAT_INCR(arcstat_hdr_size, space); 1961208373Smm break; 1962208373Smm case ARC_SPACE_L2HDRS: 1963208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, space); 1964208373Smm break; 1965208373Smm } 1966208373Smm 1967286574Smav if (type != ARC_SPACE_DATA) 1968286574Smav ARCSTAT_INCR(arcstat_meta_used, space); 1969286574Smav 1970185029Spjd atomic_add_64(&arc_size, space); 1971185029Spjd} 1972185029Spjd 1973185029Spjdvoid 1974208373Smmarc_space_return(uint64_t space, arc_space_type_t type) 1975185029Spjd{ 1976208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1977208373Smm 1978208373Smm switch (type) { 1979208373Smm case ARC_SPACE_DATA: 1980208373Smm ARCSTAT_INCR(arcstat_data_size, -space); 1981208373Smm break; 1982286574Smav case ARC_SPACE_META: 1983286574Smav ARCSTAT_INCR(arcstat_metadata_size, -space); 1984286574Smav break; 1985208373Smm case ARC_SPACE_OTHER: 1986208373Smm ARCSTAT_INCR(arcstat_other_size, -space); 1987208373Smm break; 1988208373Smm case ARC_SPACE_HDRS: 1989208373Smm ARCSTAT_INCR(arcstat_hdr_size, -space); 1990208373Smm break; 1991208373Smm case ARC_SPACE_L2HDRS: 1992208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 1993208373Smm break; 1994208373Smm } 1995208373Smm 1996286574Smav if (type != ARC_SPACE_DATA) { 1997286574Smav ASSERT(arc_meta_used >= space); 1998286574Smav if (arc_meta_max < arc_meta_used) 1999286574Smav arc_meta_max = arc_meta_used; 2000286574Smav ARCSTAT_INCR(arcstat_meta_used, -space); 2001286574Smav } 2002286574Smav 2003185029Spjd ASSERT(arc_size >= space); 2004185029Spjd atomic_add_64(&arc_size, -space); 2005185029Spjd} 2006185029Spjd 2007168404Spjdarc_buf_t * 2008286570Smavarc_buf_alloc(spa_t *spa, int32_t size, void *tag, arc_buf_contents_t type) 2009168404Spjd{ 2010168404Spjd arc_buf_hdr_t *hdr; 2011168404Spjd arc_buf_t *buf; 2012168404Spjd 2013168404Spjd ASSERT3U(size, >, 0); 2014286570Smav hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 2015168404Spjd ASSERT(BUF_EMPTY(hdr)); 2016286570Smav ASSERT3P(hdr->b_freeze_cksum, ==, NULL); 2017168404Spjd hdr->b_size = size; 2018228103Smm hdr->b_spa = spa_load_guid(spa); 2019286570Smav 2020185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2021168404Spjd buf->b_hdr = hdr; 2022168404Spjd buf->b_data = NULL; 2023168404Spjd buf->b_efunc = NULL; 2024168404Spjd buf->b_private = NULL; 2025168404Spjd buf->b_next = NULL; 2026286570Smav 2027286570Smav hdr->b_flags = arc_bufc_to_flags(type); 2028286570Smav hdr->b_flags |= ARC_FLAG_HAS_L1HDR; 2029286570Smav 2030286570Smav hdr->b_l1hdr.b_buf = buf; 2031286570Smav hdr->b_l1hdr.b_state = arc_anon; 2032286570Smav hdr->b_l1hdr.b_arc_access = 0; 2033286570Smav hdr->b_l1hdr.b_datacnt = 1; 2034286763Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 2035286570Smav 2036168404Spjd arc_get_data_buf(buf); 2037286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2038286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 2039168404Spjd 2040168404Spjd return (buf); 2041168404Spjd} 2042168404Spjd 2043209962Smmstatic char *arc_onloan_tag = "onloan"; 2044209962Smm 2045209962Smm/* 2046209962Smm * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 2047209962Smm * flight data by arc_tempreserve_space() until they are "returned". Loaned 2048209962Smm * buffers must be returned to the arc before they can be used by the DMU or 2049209962Smm * freed. 2050209962Smm */ 2051209962Smmarc_buf_t * 2052209962Smmarc_loan_buf(spa_t *spa, int size) 2053209962Smm{ 2054209962Smm arc_buf_t *buf; 2055209962Smm 2056209962Smm buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 2057209962Smm 2058209962Smm atomic_add_64(&arc_loaned_bytes, size); 2059209962Smm return (buf); 2060209962Smm} 2061209962Smm 2062209962Smm/* 2063209962Smm * Return a loaned arc buffer to the arc. 2064209962Smm */ 2065209962Smmvoid 2066209962Smmarc_return_buf(arc_buf_t *buf, void *tag) 2067209962Smm{ 2068209962Smm arc_buf_hdr_t *hdr = buf->b_hdr; 2069209962Smm 2070209962Smm ASSERT(buf->b_data != NULL); 2071286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2072286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 2073286570Smav (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2074209962Smm 2075209962Smm atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 2076209962Smm} 2077209962Smm 2078219089Spjd/* Detach an arc_buf from a dbuf (tag) */ 2079219089Spjdvoid 2080219089Spjdarc_loan_inuse_buf(arc_buf_t *buf, void *tag) 2081219089Spjd{ 2082286570Smav arc_buf_hdr_t *hdr = buf->b_hdr; 2083219089Spjd 2084219089Spjd ASSERT(buf->b_data != NULL); 2085286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2086286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2087286570Smav (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); 2088219089Spjd buf->b_efunc = NULL; 2089219089Spjd buf->b_private = NULL; 2090219089Spjd 2091219089Spjd atomic_add_64(&arc_loaned_bytes, hdr->b_size); 2092219089Spjd} 2093219089Spjd 2094168404Spjdstatic arc_buf_t * 2095168404Spjdarc_buf_clone(arc_buf_t *from) 2096168404Spjd{ 2097168404Spjd arc_buf_t *buf; 2098168404Spjd arc_buf_hdr_t *hdr = from->b_hdr; 2099168404Spjd uint64_t size = hdr->b_size; 2100168404Spjd 2101286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2102286570Smav ASSERT(hdr->b_l1hdr.b_state != arc_anon); 2103219089Spjd 2104185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2105168404Spjd buf->b_hdr = hdr; 2106168404Spjd buf->b_data = NULL; 2107168404Spjd buf->b_efunc = NULL; 2108168404Spjd buf->b_private = NULL; 2109286570Smav buf->b_next = hdr->b_l1hdr.b_buf; 2110286570Smav hdr->b_l1hdr.b_buf = buf; 2111168404Spjd arc_get_data_buf(buf); 2112168404Spjd bcopy(from->b_data, buf->b_data, size); 2113242845Sdelphij 2114242845Sdelphij /* 2115242845Sdelphij * This buffer already exists in the arc so create a duplicate 2116242845Sdelphij * copy for the caller. If the buffer is associated with user data 2117242845Sdelphij * then track the size and number of duplicates. These stats will be 2118242845Sdelphij * updated as duplicate buffers are created and destroyed. 2119242845Sdelphij */ 2120286570Smav if (HDR_ISTYPE_DATA(hdr)) { 2121242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_buffers); 2122242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, size); 2123242845Sdelphij } 2124286570Smav hdr->b_l1hdr.b_datacnt += 1; 2125168404Spjd return (buf); 2126168404Spjd} 2127168404Spjd 2128168404Spjdvoid 2129168404Spjdarc_buf_add_ref(arc_buf_t *buf, void* tag) 2130168404Spjd{ 2131168404Spjd arc_buf_hdr_t *hdr; 2132168404Spjd kmutex_t *hash_lock; 2133168404Spjd 2134168404Spjd /* 2135185029Spjd * Check to see if this buffer is evicted. Callers 2136185029Spjd * must verify b_data != NULL to know if the add_ref 2137185029Spjd * was successful. 2138168404Spjd */ 2139219089Spjd mutex_enter(&buf->b_evict_lock); 2140185029Spjd if (buf->b_data == NULL) { 2141219089Spjd mutex_exit(&buf->b_evict_lock); 2142168404Spjd return; 2143168404Spjd } 2144219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 2145219089Spjd mutex_enter(hash_lock); 2146185029Spjd hdr = buf->b_hdr; 2147286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2148219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2149219089Spjd mutex_exit(&buf->b_evict_lock); 2150168404Spjd 2151286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_mru || 2152286570Smav hdr->b_l1hdr.b_state == arc_mfu); 2153286570Smav 2154168404Spjd add_reference(hdr, hash_lock, tag); 2155208373Smm DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 2156168404Spjd arc_access(hdr, hash_lock); 2157168404Spjd mutex_exit(hash_lock); 2158168404Spjd ARCSTAT_BUMP(arcstat_hits); 2159286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 2160286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 2161168404Spjd data, metadata, hits); 2162168404Spjd} 2163168404Spjd 2164274172Savgstatic void 2165274172Savgarc_buf_free_on_write(void *data, size_t size, 2166274172Savg void (*free_func)(void *, size_t)) 2167274172Savg{ 2168274172Savg l2arc_data_free_t *df; 2169274172Savg 2170286763Smav df = kmem_alloc(sizeof (*df), KM_SLEEP); 2171274172Savg df->l2df_data = data; 2172274172Savg df->l2df_size = size; 2173274172Savg df->l2df_func = free_func; 2174274172Savg mutex_enter(&l2arc_free_on_write_mtx); 2175274172Savg list_insert_head(l2arc_free_on_write, df); 2176274172Savg mutex_exit(&l2arc_free_on_write_mtx); 2177274172Savg} 2178274172Savg 2179185029Spjd/* 2180185029Spjd * Free the arc data buffer. If it is an l2arc write in progress, 2181185029Spjd * the buffer is placed on l2arc_free_on_write to be freed later. 2182185029Spjd */ 2183168404Spjdstatic void 2184240133Smmarc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t)) 2185185029Spjd{ 2186240133Smm arc_buf_hdr_t *hdr = buf->b_hdr; 2187240133Smm 2188185029Spjd if (HDR_L2_WRITING(hdr)) { 2189274172Savg arc_buf_free_on_write(buf->b_data, hdr->b_size, free_func); 2190185029Spjd ARCSTAT_BUMP(arcstat_l2_free_on_write); 2191185029Spjd } else { 2192240133Smm free_func(buf->b_data, hdr->b_size); 2193185029Spjd } 2194185029Spjd} 2195185029Spjd 2196185029Spjdstatic void 2197274172Savgarc_buf_l2_cdata_free(arc_buf_hdr_t *hdr) 2198274172Savg{ 2199286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 2200286570Smav ASSERT(MUTEX_HELD(&hdr->b_l2hdr.b_dev->l2ad_mtx)); 2201274172Savg 2202286570Smav /* 2203286570Smav * The b_tmp_cdata field is linked off of the b_l1hdr, so if 2204286570Smav * that doesn't exist, the header is in the arc_l2c_only state, 2205286570Smav * and there isn't anything to free (it's already been freed). 2206286570Smav */ 2207286570Smav if (!HDR_HAS_L1HDR(hdr)) 2208286570Smav return; 2209274172Savg 2210286763Smav /* 2211286763Smav * The header isn't being written to the l2arc device, thus it 2212286763Smav * shouldn't have a b_tmp_cdata to free. 2213286763Smav */ 2214286763Smav if (!HDR_L2_WRITING(hdr)) { 2215286763Smav ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 2216274172Savg return; 2217286763Smav } 2218274172Savg 2219286763Smav /* 2220286763Smav * The header does not have compression enabled. This can be due 2221286763Smav * to the buffer not being compressible, or because we're 2222286763Smav * freeing the buffer before the second phase of 2223286763Smav * l2arc_write_buffer() has started (which does the compression 2224286763Smav * step). In either case, b_tmp_cdata does not point to a 2225286763Smav * separately compressed buffer, so there's nothing to free (it 2226286763Smav * points to the same buffer as the arc_buf_t's b_data field). 2227286763Smav */ 2228287706Sdelphij if (hdr->b_l2hdr.b_compress == ZIO_COMPRESS_OFF) { 2229286763Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 2230286763Smav return; 2231286763Smav } 2232286570Smav 2233286763Smav /* 2234286763Smav * There's nothing to free since the buffer was all zero's and 2235286763Smav * compressed to a zero length buffer. 2236286763Smav */ 2237287706Sdelphij if (hdr->b_l2hdr.b_compress == ZIO_COMPRESS_EMPTY) { 2238286763Smav ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 2239286763Smav return; 2240286763Smav } 2241286763Smav 2242287706Sdelphij ASSERT(L2ARC_IS_VALID_COMPRESS(hdr->b_l2hdr.b_compress)); 2243286763Smav 2244286763Smav arc_buf_free_on_write(hdr->b_l1hdr.b_tmp_cdata, 2245286763Smav hdr->b_size, zio_data_buf_free); 2246286763Smav 2247274172Savg ARCSTAT_BUMP(arcstat_l2_cdata_free_on_write); 2248286570Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 2249274172Savg} 2250274172Savg 2251286767Smav/* 2252286767Smav * Free up buf->b_data and if 'remove' is set, then pull the 2253286767Smav * arc_buf_t off of the the arc_buf_hdr_t's list and free it. 2254286767Smav */ 2255274172Savgstatic void 2256286763Smavarc_buf_destroy(arc_buf_t *buf, boolean_t remove) 2257168404Spjd{ 2258168404Spjd arc_buf_t **bufp; 2259168404Spjd 2260168404Spjd /* free up data associated with the buf */ 2261286570Smav if (buf->b_data != NULL) { 2262286570Smav arc_state_t *state = buf->b_hdr->b_l1hdr.b_state; 2263168404Spjd uint64_t size = buf->b_hdr->b_size; 2264286570Smav arc_buf_contents_t type = arc_buf_type(buf->b_hdr); 2265168404Spjd 2266168404Spjd arc_cksum_verify(buf); 2267240133Smm#ifdef illumos 2268240133Smm arc_buf_unwatch(buf); 2269277300Ssmh#endif 2270219089Spjd 2271286763Smav if (type == ARC_BUFC_METADATA) { 2272286763Smav arc_buf_data_free(buf, zio_buf_free); 2273286763Smav arc_space_return(size, ARC_SPACE_META); 2274286763Smav } else { 2275286763Smav ASSERT(type == ARC_BUFC_DATA); 2276286763Smav arc_buf_data_free(buf, zio_data_buf_free); 2277286763Smav arc_space_return(size, ARC_SPACE_DATA); 2278168404Spjd } 2279286763Smav 2280286763Smav /* protected by hash lock, if in the hash table */ 2281286763Smav if (multilist_link_active(&buf->b_hdr->b_l1hdr.b_arc_node)) { 2282185029Spjd uint64_t *cnt = &state->arcs_lsize[type]; 2283185029Spjd 2284286570Smav ASSERT(refcount_is_zero( 2285286570Smav &buf->b_hdr->b_l1hdr.b_refcnt)); 2286286570Smav ASSERT(state != arc_anon && state != arc_l2c_only); 2287185029Spjd 2288185029Spjd ASSERT3U(*cnt, >=, size); 2289185029Spjd atomic_add_64(cnt, -size); 2290168404Spjd } 2291286766Smav 2292286766Smav (void) refcount_remove_many(&state->arcs_size, size, buf); 2293168404Spjd buf->b_data = NULL; 2294242845Sdelphij 2295242845Sdelphij /* 2296242845Sdelphij * If we're destroying a duplicate buffer make sure 2297242845Sdelphij * that the appropriate statistics are updated. 2298242845Sdelphij */ 2299286570Smav if (buf->b_hdr->b_l1hdr.b_datacnt > 1 && 2300286570Smav HDR_ISTYPE_DATA(buf->b_hdr)) { 2301242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 2302242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size); 2303242845Sdelphij } 2304286570Smav ASSERT(buf->b_hdr->b_l1hdr.b_datacnt > 0); 2305286570Smav buf->b_hdr->b_l1hdr.b_datacnt -= 1; 2306168404Spjd } 2307168404Spjd 2308168404Spjd /* only remove the buf if requested */ 2309268858Sdelphij if (!remove) 2310168404Spjd return; 2311168404Spjd 2312168404Spjd /* remove the buf from the hdr list */ 2313286570Smav for (bufp = &buf->b_hdr->b_l1hdr.b_buf; *bufp != buf; 2314286570Smav bufp = &(*bufp)->b_next) 2315168404Spjd continue; 2316168404Spjd *bufp = buf->b_next; 2317219089Spjd buf->b_next = NULL; 2318168404Spjd 2319168404Spjd ASSERT(buf->b_efunc == NULL); 2320168404Spjd 2321168404Spjd /* clean up the buf */ 2322168404Spjd buf->b_hdr = NULL; 2323168404Spjd kmem_cache_free(buf_cache, buf); 2324168404Spjd} 2325168404Spjd 2326168404Spjdstatic void 2327286598Smavarc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) 2328286598Smav{ 2329286598Smav l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 2330286598Smav l2arc_dev_t *dev = l2hdr->b_dev; 2331286598Smav 2332286598Smav ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); 2333286598Smav ASSERT(HDR_HAS_L2HDR(hdr)); 2334286598Smav 2335286598Smav list_remove(&dev->l2ad_buflist, hdr); 2336286598Smav 2337286598Smav /* 2338286598Smav * We don't want to leak the b_tmp_cdata buffer that was 2339286598Smav * allocated in l2arc_write_buffers() 2340286598Smav */ 2341286598Smav arc_buf_l2_cdata_free(hdr); 2342286598Smav 2343286598Smav /* 2344286598Smav * If the l2hdr's b_daddr is equal to L2ARC_ADDR_UNSET, then 2345286598Smav * this header is being processed by l2arc_write_buffers() (i.e. 2346286598Smav * it's in the first stage of l2arc_write_buffers()). 2347286598Smav * Re-affirming that truth here, just to serve as a reminder. If 2348286598Smav * b_daddr does not equal L2ARC_ADDR_UNSET, then the header may or 2349286598Smav * may not have its HDR_L2_WRITING flag set. (the write may have 2350286598Smav * completed, in which case HDR_L2_WRITING will be false and the 2351286598Smav * b_daddr field will point to the address of the buffer on disk). 2352286598Smav */ 2353286598Smav IMPLY(l2hdr->b_daddr == L2ARC_ADDR_UNSET, HDR_L2_WRITING(hdr)); 2354286598Smav 2355286598Smav /* 2356286598Smav * If b_daddr is equal to L2ARC_ADDR_UNSET, we're racing with 2357286598Smav * l2arc_write_buffers(). Since we've just removed this header 2358286598Smav * from the l2arc buffer list, this header will never reach the 2359286598Smav * second stage of l2arc_write_buffers(), which increments the 2360286598Smav * accounting stats for this header. Thus, we must be careful 2361286598Smav * not to decrement them for this header either. 2362286598Smav */ 2363286598Smav if (l2hdr->b_daddr != L2ARC_ADDR_UNSET) { 2364286598Smav ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); 2365286598Smav ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 2366286598Smav 2367286598Smav vdev_space_update(dev->l2ad_vdev, 2368286598Smav -l2hdr->b_asize, 0, 0); 2369286598Smav 2370286598Smav (void) refcount_remove_many(&dev->l2ad_alloc, 2371286598Smav l2hdr->b_asize, hdr); 2372286598Smav } 2373286598Smav 2374286598Smav hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR; 2375286598Smav} 2376286598Smav 2377286598Smavstatic void 2378168404Spjdarc_hdr_destroy(arc_buf_hdr_t *hdr) 2379168404Spjd{ 2380286570Smav if (HDR_HAS_L1HDR(hdr)) { 2381286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL || 2382286570Smav hdr->b_l1hdr.b_datacnt > 0); 2383286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2384286570Smav ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 2385286570Smav } 2386168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2387286570Smav ASSERT(!HDR_IN_HASH_TABLE(hdr)); 2388168404Spjd 2389286570Smav if (HDR_HAS_L2HDR(hdr)) { 2390286598Smav l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 2391286598Smav boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); 2392286570Smav 2393286598Smav if (!buflist_held) 2394286598Smav mutex_enter(&dev->l2ad_mtx); 2395219089Spjd 2396286570Smav /* 2397286598Smav * Even though we checked this conditional above, we 2398286598Smav * need to check this again now that we have the 2399286598Smav * l2ad_mtx. This is because we could be racing with 2400286598Smav * another thread calling l2arc_evict() which might have 2401286598Smav * destroyed this header's L2 portion as we were waiting 2402286598Smav * to acquire the l2ad_mtx. If that happens, we don't 2403286598Smav * want to re-destroy the header's L2 portion. 2404286570Smav */ 2405286598Smav if (HDR_HAS_L2HDR(hdr)) { 2406286647Smav if (hdr->b_l2hdr.b_daddr != L2ARC_ADDR_UNSET) 2407286647Smav trim_map_free(dev->l2ad_vdev, 2408286647Smav hdr->b_l2hdr.b_daddr, 2409286647Smav hdr->b_l2hdr.b_asize, 0); 2410286598Smav arc_hdr_l2hdr_destroy(hdr); 2411286598Smav } 2412286570Smav 2413219089Spjd if (!buflist_held) 2414286598Smav mutex_exit(&dev->l2ad_mtx); 2415185029Spjd } 2416185029Spjd 2417286570Smav if (!BUF_EMPTY(hdr)) 2418219089Spjd buf_discard_identity(hdr); 2419286776Smav 2420168404Spjd if (hdr->b_freeze_cksum != NULL) { 2421168404Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 2422168404Spjd hdr->b_freeze_cksum = NULL; 2423168404Spjd } 2424286570Smav 2425286570Smav if (HDR_HAS_L1HDR(hdr)) { 2426286570Smav while (hdr->b_l1hdr.b_buf) { 2427286570Smav arc_buf_t *buf = hdr->b_l1hdr.b_buf; 2428286570Smav 2429286570Smav if (buf->b_efunc != NULL) { 2430286763Smav mutex_enter(&arc_user_evicts_lock); 2431286570Smav mutex_enter(&buf->b_evict_lock); 2432286570Smav ASSERT(buf->b_hdr != NULL); 2433286763Smav arc_buf_destroy(hdr->b_l1hdr.b_buf, FALSE); 2434286570Smav hdr->b_l1hdr.b_buf = buf->b_next; 2435286570Smav buf->b_hdr = &arc_eviction_hdr; 2436286570Smav buf->b_next = arc_eviction_list; 2437286570Smav arc_eviction_list = buf; 2438286570Smav mutex_exit(&buf->b_evict_lock); 2439286763Smav cv_signal(&arc_user_evicts_cv); 2440286763Smav mutex_exit(&arc_user_evicts_lock); 2441286570Smav } else { 2442286763Smav arc_buf_destroy(hdr->b_l1hdr.b_buf, TRUE); 2443286570Smav } 2444286570Smav } 2445286570Smav#ifdef ZFS_DEBUG 2446286570Smav if (hdr->b_l1hdr.b_thawed != NULL) { 2447286570Smav kmem_free(hdr->b_l1hdr.b_thawed, 1); 2448286570Smav hdr->b_l1hdr.b_thawed = NULL; 2449286570Smav } 2450286570Smav#endif 2451219089Spjd } 2452168404Spjd 2453168404Spjd ASSERT3P(hdr->b_hash_next, ==, NULL); 2454286570Smav if (HDR_HAS_L1HDR(hdr)) { 2455286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 2456286570Smav ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 2457286570Smav kmem_cache_free(hdr_full_cache, hdr); 2458286570Smav } else { 2459286570Smav kmem_cache_free(hdr_l2only_cache, hdr); 2460286570Smav } 2461168404Spjd} 2462168404Spjd 2463168404Spjdvoid 2464168404Spjdarc_buf_free(arc_buf_t *buf, void *tag) 2465168404Spjd{ 2466168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2467286570Smav int hashed = hdr->b_l1hdr.b_state != arc_anon; 2468168404Spjd 2469168404Spjd ASSERT(buf->b_efunc == NULL); 2470168404Spjd ASSERT(buf->b_data != NULL); 2471168404Spjd 2472168404Spjd if (hashed) { 2473168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 2474168404Spjd 2475168404Spjd mutex_enter(hash_lock); 2476219089Spjd hdr = buf->b_hdr; 2477219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2478219089Spjd 2479168404Spjd (void) remove_reference(hdr, hash_lock, tag); 2480286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 2481286763Smav arc_buf_destroy(buf, TRUE); 2482219089Spjd } else { 2483286570Smav ASSERT(buf == hdr->b_l1hdr.b_buf); 2484219089Spjd ASSERT(buf->b_efunc == NULL); 2485275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 2486219089Spjd } 2487168404Spjd mutex_exit(hash_lock); 2488168404Spjd } else if (HDR_IO_IN_PROGRESS(hdr)) { 2489168404Spjd int destroy_hdr; 2490168404Spjd /* 2491168404Spjd * We are in the middle of an async write. Don't destroy 2492168404Spjd * this buffer unless the write completes before we finish 2493168404Spjd * decrementing the reference count. 2494168404Spjd */ 2495286763Smav mutex_enter(&arc_user_evicts_lock); 2496168404Spjd (void) remove_reference(hdr, NULL, tag); 2497286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2498168404Spjd destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 2499286763Smav mutex_exit(&arc_user_evicts_lock); 2500168404Spjd if (destroy_hdr) 2501168404Spjd arc_hdr_destroy(hdr); 2502168404Spjd } else { 2503219089Spjd if (remove_reference(hdr, NULL, tag) > 0) 2504286763Smav arc_buf_destroy(buf, TRUE); 2505219089Spjd else 2506168404Spjd arc_hdr_destroy(hdr); 2507168404Spjd } 2508168404Spjd} 2509168404Spjd 2510248571Smmboolean_t 2511168404Spjdarc_buf_remove_ref(arc_buf_t *buf, void* tag) 2512168404Spjd{ 2513168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2514168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 2515248571Smm boolean_t no_callback = (buf->b_efunc == NULL); 2516168404Spjd 2517286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 2518286570Smav ASSERT(hdr->b_l1hdr.b_datacnt == 1); 2519168404Spjd arc_buf_free(buf, tag); 2520168404Spjd return (no_callback); 2521168404Spjd } 2522168404Spjd 2523168404Spjd mutex_enter(hash_lock); 2524219089Spjd hdr = buf->b_hdr; 2525286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 2526219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2527286570Smav ASSERT(hdr->b_l1hdr.b_state != arc_anon); 2528168404Spjd ASSERT(buf->b_data != NULL); 2529168404Spjd 2530168404Spjd (void) remove_reference(hdr, hash_lock, tag); 2531286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 2532168404Spjd if (no_callback) 2533286763Smav arc_buf_destroy(buf, TRUE); 2534168404Spjd } else if (no_callback) { 2535286570Smav ASSERT(hdr->b_l1hdr.b_buf == buf && buf->b_next == NULL); 2536219089Spjd ASSERT(buf->b_efunc == NULL); 2537275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 2538168404Spjd } 2539286570Smav ASSERT(no_callback || hdr->b_l1hdr.b_datacnt > 1 || 2540286570Smav refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2541168404Spjd mutex_exit(hash_lock); 2542168404Spjd return (no_callback); 2543168404Spjd} 2544168404Spjd 2545286570Smavint32_t 2546168404Spjdarc_buf_size(arc_buf_t *buf) 2547168404Spjd{ 2548168404Spjd return (buf->b_hdr->b_size); 2549168404Spjd} 2550168404Spjd 2551168404Spjd/* 2552242845Sdelphij * Called from the DMU to determine if the current buffer should be 2553242845Sdelphij * evicted. In order to ensure proper locking, the eviction must be initiated 2554242845Sdelphij * from the DMU. Return true if the buffer is associated with user data and 2555242845Sdelphij * duplicate buffers still exist. 2556242845Sdelphij */ 2557242845Sdelphijboolean_t 2558242845Sdelphijarc_buf_eviction_needed(arc_buf_t *buf) 2559242845Sdelphij{ 2560242845Sdelphij arc_buf_hdr_t *hdr; 2561242845Sdelphij boolean_t evict_needed = B_FALSE; 2562242845Sdelphij 2563242845Sdelphij if (zfs_disable_dup_eviction) 2564242845Sdelphij return (B_FALSE); 2565242845Sdelphij 2566242845Sdelphij mutex_enter(&buf->b_evict_lock); 2567242845Sdelphij hdr = buf->b_hdr; 2568242845Sdelphij if (hdr == NULL) { 2569242845Sdelphij /* 2570242845Sdelphij * We are in arc_do_user_evicts(); let that function 2571242845Sdelphij * perform the eviction. 2572242845Sdelphij */ 2573242845Sdelphij ASSERT(buf->b_data == NULL); 2574242845Sdelphij mutex_exit(&buf->b_evict_lock); 2575242845Sdelphij return (B_FALSE); 2576242845Sdelphij } else if (buf->b_data == NULL) { 2577242845Sdelphij /* 2578242845Sdelphij * We have already been added to the arc eviction list; 2579242845Sdelphij * recommend eviction. 2580242845Sdelphij */ 2581242845Sdelphij ASSERT3P(hdr, ==, &arc_eviction_hdr); 2582242845Sdelphij mutex_exit(&buf->b_evict_lock); 2583242845Sdelphij return (B_TRUE); 2584242845Sdelphij } 2585242845Sdelphij 2586286570Smav if (hdr->b_l1hdr.b_datacnt > 1 && HDR_ISTYPE_DATA(hdr)) 2587242845Sdelphij evict_needed = B_TRUE; 2588242845Sdelphij 2589242845Sdelphij mutex_exit(&buf->b_evict_lock); 2590242845Sdelphij return (evict_needed); 2591242845Sdelphij} 2592242845Sdelphij 2593242845Sdelphij/* 2594286763Smav * Evict the arc_buf_hdr that is provided as a parameter. The resultant 2595286763Smav * state of the header is dependent on it's state prior to entering this 2596286763Smav * function. The following transitions are possible: 2597185029Spjd * 2598286763Smav * - arc_mru -> arc_mru_ghost 2599286763Smav * - arc_mfu -> arc_mfu_ghost 2600286763Smav * - arc_mru_ghost -> arc_l2c_only 2601286763Smav * - arc_mru_ghost -> deleted 2602286763Smav * - arc_mfu_ghost -> arc_l2c_only 2603286763Smav * - arc_mfu_ghost -> deleted 2604168404Spjd */ 2605286763Smavstatic int64_t 2606286763Smavarc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 2607168404Spjd{ 2608286763Smav arc_state_t *evicted_state, *state; 2609286763Smav int64_t bytes_evicted = 0; 2610168404Spjd 2611286763Smav ASSERT(MUTEX_HELD(hash_lock)); 2612286763Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2613168404Spjd 2614286763Smav state = hdr->b_l1hdr.b_state; 2615286763Smav if (GHOST_STATE(state)) { 2616286763Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2617286763Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 2618206796Spjd 2619286763Smav /* 2620286763Smav * l2arc_write_buffers() relies on a header's L1 portion 2621286763Smav * (i.e. it's b_tmp_cdata field) during it's write phase. 2622286763Smav * Thus, we cannot push a header onto the arc_l2c_only 2623286763Smav * state (removing it's L1 piece) until the header is 2624286763Smav * done being written to the l2arc. 2625286763Smav */ 2626286763Smav if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { 2627286763Smav ARCSTAT_BUMP(arcstat_evict_l2_skip); 2628286763Smav return (bytes_evicted); 2629286763Smav } 2630286762Smav 2631286763Smav ARCSTAT_BUMP(arcstat_deleted); 2632286763Smav bytes_evicted += hdr->b_size; 2633286762Smav 2634286763Smav DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); 2635286763Smav 2636286763Smav if (HDR_HAS_L2HDR(hdr)) { 2637275780Sdelphij /* 2638286763Smav * This buffer is cached on the 2nd Level ARC; 2639286763Smav * don't destroy the header. 2640275780Sdelphij */ 2641286763Smav arc_change_state(arc_l2c_only, hdr, hash_lock); 2642286763Smav /* 2643286763Smav * dropping from L1+L2 cached to L2-only, 2644286763Smav * realloc to remove the L1 header. 2645286763Smav */ 2646286763Smav hdr = arc_hdr_realloc(hdr, hdr_full_cache, 2647286763Smav hdr_l2only_cache); 2648286763Smav } else { 2649286763Smav arc_change_state(arc_anon, hdr, hash_lock); 2650286763Smav arc_hdr_destroy(hdr); 2651275780Sdelphij } 2652286763Smav return (bytes_evicted); 2653275780Sdelphij } 2654275780Sdelphij 2655286763Smav ASSERT(state == arc_mru || state == arc_mfu); 2656286763Smav evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 2657206796Spjd 2658286763Smav /* prefetch buffers have a minimum lifespan */ 2659286763Smav if (HDR_IO_IN_PROGRESS(hdr) || 2660286763Smav ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && 2661286763Smav ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < 2662286763Smav arc_min_prefetch_lifespan)) { 2663286763Smav ARCSTAT_BUMP(arcstat_evict_skip); 2664286763Smav return (bytes_evicted); 2665286763Smav } 2666286763Smav 2667286763Smav ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 2668286763Smav ASSERT3U(hdr->b_l1hdr.b_datacnt, >, 0); 2669286763Smav while (hdr->b_l1hdr.b_buf) { 2670286763Smav arc_buf_t *buf = hdr->b_l1hdr.b_buf; 2671286763Smav if (!mutex_tryenter(&buf->b_evict_lock)) { 2672286763Smav ARCSTAT_BUMP(arcstat_mutex_miss); 2673286763Smav break; 2674168404Spjd } 2675286763Smav if (buf->b_data != NULL) 2676286763Smav bytes_evicted += hdr->b_size; 2677286763Smav if (buf->b_efunc != NULL) { 2678286763Smav mutex_enter(&arc_user_evicts_lock); 2679286763Smav arc_buf_destroy(buf, FALSE); 2680286763Smav hdr->b_l1hdr.b_buf = buf->b_next; 2681286763Smav buf->b_hdr = &arc_eviction_hdr; 2682286763Smav buf->b_next = arc_eviction_list; 2683286763Smav arc_eviction_list = buf; 2684286763Smav cv_signal(&arc_user_evicts_cv); 2685286763Smav mutex_exit(&arc_user_evicts_lock); 2686286763Smav mutex_exit(&buf->b_evict_lock); 2687286763Smav } else { 2688286763Smav mutex_exit(&buf->b_evict_lock); 2689286763Smav arc_buf_destroy(buf, TRUE); 2690286763Smav } 2691286763Smav } 2692258632Savg 2693286763Smav if (HDR_HAS_L2HDR(hdr)) { 2694286763Smav ARCSTAT_INCR(arcstat_evict_l2_cached, hdr->b_size); 2695286763Smav } else { 2696286763Smav if (l2arc_write_eligible(hdr->b_spa, hdr)) 2697286763Smav ARCSTAT_INCR(arcstat_evict_l2_eligible, hdr->b_size); 2698286763Smav else 2699286763Smav ARCSTAT_INCR(arcstat_evict_l2_ineligible, hdr->b_size); 2700286763Smav } 2701258632Savg 2702286763Smav if (hdr->b_l1hdr.b_datacnt == 0) { 2703286763Smav arc_change_state(evicted_state, hdr, hash_lock); 2704286763Smav ASSERT(HDR_IN_HASH_TABLE(hdr)); 2705286763Smav hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; 2706286763Smav hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 2707286763Smav DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); 2708286763Smav } 2709286763Smav 2710286763Smav return (bytes_evicted); 2711286763Smav} 2712286763Smav 2713286763Smavstatic uint64_t 2714286763Smavarc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, 2715286763Smav uint64_t spa, int64_t bytes) 2716286763Smav{ 2717286763Smav multilist_sublist_t *mls; 2718286763Smav uint64_t bytes_evicted = 0; 2719286763Smav arc_buf_hdr_t *hdr; 2720286763Smav kmutex_t *hash_lock; 2721286763Smav int evict_count = 0; 2722286763Smav 2723286763Smav ASSERT3P(marker, !=, NULL); 2724286763Smav IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 2725286763Smav 2726286763Smav mls = multilist_sublist_lock(ml, idx); 2727286763Smav 2728286763Smav for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL; 2729286763Smav hdr = multilist_sublist_prev(mls, marker)) { 2730286763Smav if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) || 2731286763Smav (evict_count >= zfs_arc_evict_batch_limit)) 2732286763Smav break; 2733286763Smav 2734258632Savg /* 2735286763Smav * To keep our iteration location, move the marker 2736286763Smav * forward. Since we're not holding hdr's hash lock, we 2737286763Smav * must be very careful and not remove 'hdr' from the 2738286763Smav * sublist. Otherwise, other consumers might mistake the 2739286763Smav * 'hdr' as not being on a sublist when they call the 2740286763Smav * multilist_link_active() function (they all rely on 2741286763Smav * the hash lock protecting concurrent insertions and 2742286763Smav * removals). multilist_sublist_move_forward() was 2743286763Smav * specifically implemented to ensure this is the case 2744286763Smav * (only 'marker' will be removed and re-inserted). 2745258632Savg */ 2746286763Smav multilist_sublist_move_forward(mls, marker); 2747286763Smav 2748286763Smav /* 2749286763Smav * The only case where the b_spa field should ever be 2750286763Smav * zero, is the marker headers inserted by 2751286763Smav * arc_evict_state(). It's possible for multiple threads 2752286763Smav * to be calling arc_evict_state() concurrently (e.g. 2753286763Smav * dsl_pool_close() and zio_inject_fault()), so we must 2754286763Smav * skip any markers we see from these other threads. 2755286763Smav */ 2756286763Smav if (hdr->b_spa == 0) 2757258632Savg continue; 2758286763Smav 2759286763Smav /* we're only interested in evicting buffers of a certain spa */ 2760286763Smav if (spa != 0 && hdr->b_spa != spa) { 2761286763Smav ARCSTAT_BUMP(arcstat_evict_skip); 2762286763Smav continue; 2763258632Savg } 2764258632Savg 2765275811Sdelphij hash_lock = HDR_LOCK(hdr); 2766208373Smm 2767286763Smav /* 2768286763Smav * We aren't calling this function from any code path 2769286763Smav * that would already be holding a hash lock, so we're 2770286763Smav * asserting on this assumption to be defensive in case 2771286763Smav * this ever changes. Without this check, it would be 2772286763Smav * possible to incorrectly increment arcstat_mutex_miss 2773286763Smav * below (e.g. if the code changed such that we called 2774286763Smav * this function with a hash lock held). 2775286763Smav */ 2776286763Smav ASSERT(!MUTEX_HELD(hash_lock)); 2777208373Smm 2778286763Smav if (mutex_tryenter(hash_lock)) { 2779286763Smav uint64_t evicted = arc_evict_hdr(hdr, hash_lock); 2780286763Smav mutex_exit(hash_lock); 2781286763Smav 2782286763Smav bytes_evicted += evicted; 2783286763Smav 2784286763Smav /* 2785286763Smav * If evicted is zero, arc_evict_hdr() must have 2786286763Smav * decided to skip this header, don't increment 2787286763Smav * evict_count in this case. 2788286763Smav */ 2789286763Smav if (evicted != 0) 2790286763Smav evict_count++; 2791286763Smav 2792286763Smav /* 2793286763Smav * If arc_size isn't overflowing, signal any 2794286763Smav * threads that might happen to be waiting. 2795286763Smav * 2796286763Smav * For each header evicted, we wake up a single 2797286763Smav * thread. If we used cv_broadcast, we could 2798286763Smav * wake up "too many" threads causing arc_size 2799286763Smav * to significantly overflow arc_c; since 2800286763Smav * arc_get_data_buf() doesn't check for overflow 2801286763Smav * when it's woken up (it doesn't because it's 2802286763Smav * possible for the ARC to be overflowing while 2803286763Smav * full of un-evictable buffers, and the 2804286763Smav * function should proceed in this case). 2805286763Smav * 2806286763Smav * If threads are left sleeping, due to not 2807286763Smav * using cv_broadcast, they will be woken up 2808286763Smav * just before arc_reclaim_thread() sleeps. 2809286763Smav */ 2810286763Smav mutex_enter(&arc_reclaim_lock); 2811286763Smav if (!arc_is_overflowing()) 2812286763Smav cv_signal(&arc_reclaim_waiters_cv); 2813286763Smav mutex_exit(&arc_reclaim_lock); 2814168404Spjd } else { 2815286763Smav ARCSTAT_BUMP(arcstat_mutex_miss); 2816168404Spjd } 2817168404Spjd } 2818168404Spjd 2819286763Smav multilist_sublist_unlock(mls); 2820206796Spjd 2821286763Smav return (bytes_evicted); 2822286763Smav} 2823168404Spjd 2824286763Smav/* 2825286763Smav * Evict buffers from the given arc state, until we've removed the 2826286763Smav * specified number of bytes. Move the removed buffers to the 2827286763Smav * appropriate evict state. 2828286763Smav * 2829286763Smav * This function makes a "best effort". It skips over any buffers 2830286763Smav * it can't get a hash_lock on, and so, may not catch all candidates. 2831286763Smav * It may also return without evicting as much space as requested. 2832286763Smav * 2833286763Smav * If bytes is specified using the special value ARC_EVICT_ALL, this 2834286763Smav * will evict all available (i.e. unlocked and evictable) buffers from 2835286763Smav * the given arc state; which is used by arc_flush(). 2836286763Smav */ 2837286763Smavstatic uint64_t 2838286763Smavarc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes, 2839286763Smav arc_buf_contents_t type) 2840286763Smav{ 2841286763Smav uint64_t total_evicted = 0; 2842286763Smav multilist_t *ml = &state->arcs_list[type]; 2843286763Smav int num_sublists; 2844286763Smav arc_buf_hdr_t **markers; 2845168404Spjd 2846286763Smav IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 2847168404Spjd 2848286763Smav num_sublists = multilist_get_num_sublists(ml); 2849286763Smav 2850185029Spjd /* 2851286763Smav * If we've tried to evict from each sublist, made some 2852286763Smav * progress, but still have not hit the target number of bytes 2853286763Smav * to evict, we want to keep trying. The markers allow us to 2854286763Smav * pick up where we left off for each individual sublist, rather 2855286763Smav * than starting from the tail each time. 2856185029Spjd */ 2857286763Smav markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); 2858286763Smav for (int i = 0; i < num_sublists; i++) { 2859286763Smav markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); 2860185029Spjd 2861286763Smav /* 2862286763Smav * A b_spa of 0 is used to indicate that this header is 2863286763Smav * a marker. This fact is used in arc_adjust_type() and 2864286763Smav * arc_evict_state_impl(). 2865286763Smav */ 2866286763Smav markers[i]->b_spa = 0; 2867168404Spjd 2868286763Smav multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 2869286763Smav multilist_sublist_insert_tail(mls, markers[i]); 2870286763Smav multilist_sublist_unlock(mls); 2871286763Smav } 2872168404Spjd 2873286763Smav /* 2874286763Smav * While we haven't hit our target number of bytes to evict, or 2875286763Smav * we're evicting all available buffers. 2876286763Smav */ 2877286763Smav while (total_evicted < bytes || bytes == ARC_EVICT_ALL) { 2878286763Smav /* 2879286763Smav * Start eviction using a randomly selected sublist, 2880286763Smav * this is to try and evenly balance eviction across all 2881286763Smav * sublists. Always starting at the same sublist 2882286763Smav * (e.g. index 0) would cause evictions to favor certain 2883286763Smav * sublists over others. 2884286763Smav */ 2885286763Smav int sublist_idx = multilist_get_random_index(ml); 2886286763Smav uint64_t scan_evicted = 0; 2887219089Spjd 2888286763Smav for (int i = 0; i < num_sublists; i++) { 2889286763Smav uint64_t bytes_remaining; 2890286763Smav uint64_t bytes_evicted; 2891219089Spjd 2892286763Smav if (bytes == ARC_EVICT_ALL) 2893286763Smav bytes_remaining = ARC_EVICT_ALL; 2894286763Smav else if (total_evicted < bytes) 2895286763Smav bytes_remaining = bytes - total_evicted; 2896286763Smav else 2897286763Smav break; 2898258632Savg 2899286763Smav bytes_evicted = arc_evict_state_impl(ml, sublist_idx, 2900286763Smav markers[sublist_idx], spa, bytes_remaining); 2901286763Smav 2902286763Smav scan_evicted += bytes_evicted; 2903286763Smav total_evicted += bytes_evicted; 2904286763Smav 2905286763Smav /* we've reached the end, wrap to the beginning */ 2906286763Smav if (++sublist_idx >= num_sublists) 2907286763Smav sublist_idx = 0; 2908286763Smav } 2909286763Smav 2910258632Savg /* 2911286763Smav * If we didn't evict anything during this scan, we have 2912286763Smav * no reason to believe we'll evict more during another 2913286763Smav * scan, so break the loop. 2914258632Savg */ 2915286763Smav if (scan_evicted == 0) { 2916286763Smav /* This isn't possible, let's make that obvious */ 2917286763Smav ASSERT3S(bytes, !=, 0); 2918185029Spjd 2919286763Smav /* 2920286763Smav * When bytes is ARC_EVICT_ALL, the only way to 2921286763Smav * break the loop is when scan_evicted is zero. 2922286763Smav * In that case, we actually have evicted enough, 2923286763Smav * so we don't want to increment the kstat. 2924286763Smav */ 2925286763Smav if (bytes != ARC_EVICT_ALL) { 2926286763Smav ASSERT3S(total_evicted, <, bytes); 2927286763Smav ARCSTAT_BUMP(arcstat_evict_not_enough); 2928185029Spjd } 2929185029Spjd 2930286763Smav break; 2931258632Savg } 2932286763Smav } 2933258632Savg 2934286763Smav for (int i = 0; i < num_sublists; i++) { 2935286763Smav multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 2936286763Smav multilist_sublist_remove(mls, markers[i]); 2937286763Smav multilist_sublist_unlock(mls); 2938286763Smav 2939286763Smav kmem_cache_free(hdr_full_cache, markers[i]); 2940168404Spjd } 2941286763Smav kmem_free(markers, sizeof (*markers) * num_sublists); 2942206796Spjd 2943286763Smav return (total_evicted); 2944286763Smav} 2945286763Smav 2946286763Smav/* 2947286763Smav * Flush all "evictable" data of the given type from the arc state 2948286763Smav * specified. This will not evict any "active" buffers (i.e. referenced). 2949286763Smav * 2950286763Smav * When 'retry' is set to FALSE, the function will make a single pass 2951286763Smav * over the state and evict any buffers that it can. Since it doesn't 2952286763Smav * continually retry the eviction, it might end up leaving some buffers 2953286763Smav * in the ARC due to lock misses. 2954286763Smav * 2955286763Smav * When 'retry' is set to TRUE, the function will continually retry the 2956286763Smav * eviction until *all* evictable buffers have been removed from the 2957286763Smav * state. As a result, if concurrent insertions into the state are 2958286763Smav * allowed (e.g. if the ARC isn't shutting down), this function might 2959286763Smav * wind up in an infinite loop, continually trying to evict buffers. 2960286763Smav */ 2961286763Smavstatic uint64_t 2962286763Smavarc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, 2963286763Smav boolean_t retry) 2964286763Smav{ 2965286763Smav uint64_t evicted = 0; 2966286763Smav 2967286763Smav while (state->arcs_lsize[type] != 0) { 2968286763Smav evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); 2969286763Smav 2970286763Smav if (!retry) 2971286763Smav break; 2972185029Spjd } 2973185029Spjd 2974286763Smav return (evicted); 2975286763Smav} 2976286763Smav 2977286763Smav/* 2978286763Smav * Evict the specified number of bytes from the state specified, 2979286763Smav * restricting eviction to the spa and type given. This function 2980286763Smav * prevents us from trying to evict more from a state's list than 2981286763Smav * is "evictable", and to skip evicting altogether when passed a 2982286763Smav * negative value for "bytes". In contrast, arc_evict_state() will 2983286763Smav * evict everything it can, when passed a negative value for "bytes". 2984286763Smav */ 2985286763Smavstatic uint64_t 2986286763Smavarc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes, 2987286763Smav arc_buf_contents_t type) 2988286763Smav{ 2989286763Smav int64_t delta; 2990286763Smav 2991286763Smav if (bytes > 0 && state->arcs_lsize[type] > 0) { 2992286763Smav delta = MIN(state->arcs_lsize[type], bytes); 2993286763Smav return (arc_evict_state(state, spa, delta, type)); 2994168404Spjd } 2995168404Spjd 2996286763Smav return (0); 2997168404Spjd} 2998168404Spjd 2999286763Smav/* 3000286763Smav * Evict metadata buffers from the cache, such that arc_meta_used is 3001286763Smav * capped by the arc_meta_limit tunable. 3002286763Smav */ 3003286763Smavstatic uint64_t 3004286763Smavarc_adjust_meta(void) 3005286763Smav{ 3006286763Smav uint64_t total_evicted = 0; 3007286763Smav int64_t target; 3008286763Smav 3009286763Smav /* 3010286763Smav * If we're over the meta limit, we want to evict enough 3011286763Smav * metadata to get back under the meta limit. We don't want to 3012286763Smav * evict so much that we drop the MRU below arc_p, though. If 3013286763Smav * we're over the meta limit more than we're over arc_p, we 3014286763Smav * evict some from the MRU here, and some from the MFU below. 3015286763Smav */ 3016286763Smav target = MIN((int64_t)(arc_meta_used - arc_meta_limit), 3017286766Smav (int64_t)(refcount_count(&arc_anon->arcs_size) + 3018286766Smav refcount_count(&arc_mru->arcs_size) - arc_p)); 3019286763Smav 3020286763Smav total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3021286763Smav 3022286763Smav /* 3023286763Smav * Similar to the above, we want to evict enough bytes to get us 3024286763Smav * below the meta limit, but not so much as to drop us below the 3025286763Smav * space alloted to the MFU (which is defined as arc_c - arc_p). 3026286763Smav */ 3027286763Smav target = MIN((int64_t)(arc_meta_used - arc_meta_limit), 3028286766Smav (int64_t)(refcount_count(&arc_mfu->arcs_size) - (arc_c - arc_p))); 3029286763Smav 3030286763Smav total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3031286763Smav 3032286763Smav return (total_evicted); 3033286763Smav} 3034286763Smav 3035286763Smav/* 3036286763Smav * Return the type of the oldest buffer in the given arc state 3037286763Smav * 3038286763Smav * This function will select a random sublist of type ARC_BUFC_DATA and 3039286763Smav * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist 3040286763Smav * is compared, and the type which contains the "older" buffer will be 3041286763Smav * returned. 3042286763Smav */ 3043286763Smavstatic arc_buf_contents_t 3044286763Smavarc_adjust_type(arc_state_t *state) 3045286763Smav{ 3046286763Smav multilist_t *data_ml = &state->arcs_list[ARC_BUFC_DATA]; 3047286763Smav multilist_t *meta_ml = &state->arcs_list[ARC_BUFC_METADATA]; 3048286763Smav int data_idx = multilist_get_random_index(data_ml); 3049286763Smav int meta_idx = multilist_get_random_index(meta_ml); 3050286763Smav multilist_sublist_t *data_mls; 3051286763Smav multilist_sublist_t *meta_mls; 3052286763Smav arc_buf_contents_t type; 3053286763Smav arc_buf_hdr_t *data_hdr; 3054286763Smav arc_buf_hdr_t *meta_hdr; 3055286763Smav 3056286763Smav /* 3057286763Smav * We keep the sublist lock until we're finished, to prevent 3058286763Smav * the headers from being destroyed via arc_evict_state(). 3059286763Smav */ 3060286763Smav data_mls = multilist_sublist_lock(data_ml, data_idx); 3061286763Smav meta_mls = multilist_sublist_lock(meta_ml, meta_idx); 3062286763Smav 3063286763Smav /* 3064286763Smav * These two loops are to ensure we skip any markers that 3065286763Smav * might be at the tail of the lists due to arc_evict_state(). 3066286763Smav */ 3067286763Smav 3068286763Smav for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; 3069286763Smav data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { 3070286763Smav if (data_hdr->b_spa != 0) 3071286763Smav break; 3072286763Smav } 3073286763Smav 3074286763Smav for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; 3075286763Smav meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { 3076286763Smav if (meta_hdr->b_spa != 0) 3077286763Smav break; 3078286763Smav } 3079286763Smav 3080286763Smav if (data_hdr == NULL && meta_hdr == NULL) { 3081286763Smav type = ARC_BUFC_DATA; 3082286763Smav } else if (data_hdr == NULL) { 3083286763Smav ASSERT3P(meta_hdr, !=, NULL); 3084286763Smav type = ARC_BUFC_METADATA; 3085286763Smav } else if (meta_hdr == NULL) { 3086286763Smav ASSERT3P(data_hdr, !=, NULL); 3087286763Smav type = ARC_BUFC_DATA; 3088286763Smav } else { 3089286763Smav ASSERT3P(data_hdr, !=, NULL); 3090286763Smav ASSERT3P(meta_hdr, !=, NULL); 3091286763Smav 3092286763Smav /* The headers can't be on the sublist without an L1 header */ 3093286763Smav ASSERT(HDR_HAS_L1HDR(data_hdr)); 3094286763Smav ASSERT(HDR_HAS_L1HDR(meta_hdr)); 3095286763Smav 3096286763Smav if (data_hdr->b_l1hdr.b_arc_access < 3097286763Smav meta_hdr->b_l1hdr.b_arc_access) { 3098286763Smav type = ARC_BUFC_DATA; 3099286763Smav } else { 3100286763Smav type = ARC_BUFC_METADATA; 3101286763Smav } 3102286763Smav } 3103286763Smav 3104286763Smav multilist_sublist_unlock(meta_mls); 3105286763Smav multilist_sublist_unlock(data_mls); 3106286763Smav 3107286763Smav return (type); 3108286763Smav} 3109286763Smav 3110286763Smav/* 3111286763Smav * Evict buffers from the cache, such that arc_size is capped by arc_c. 3112286763Smav */ 3113286763Smavstatic uint64_t 3114168404Spjdarc_adjust(void) 3115168404Spjd{ 3116286763Smav uint64_t total_evicted = 0; 3117286763Smav uint64_t bytes; 3118286763Smav int64_t target; 3119168404Spjd 3120208373Smm /* 3121286763Smav * If we're over arc_meta_limit, we want to correct that before 3122286763Smav * potentially evicting data buffers below. 3123286763Smav */ 3124286763Smav total_evicted += arc_adjust_meta(); 3125286763Smav 3126286763Smav /* 3127208373Smm * Adjust MRU size 3128286763Smav * 3129286763Smav * If we're over the target cache size, we want to evict enough 3130286763Smav * from the list to get back to our target size. We don't want 3131286763Smav * to evict too much from the MRU, such that it drops below 3132286763Smav * arc_p. So, if we're over our target cache size more than 3133286763Smav * the MRU is over arc_p, we'll evict enough to get back to 3134286763Smav * arc_p here, and then evict more from the MFU below. 3135208373Smm */ 3136286763Smav target = MIN((int64_t)(arc_size - arc_c), 3137286766Smav (int64_t)(refcount_count(&arc_anon->arcs_size) + 3138286766Smav refcount_count(&arc_mru->arcs_size) + arc_meta_used - arc_p)); 3139208373Smm 3140286763Smav /* 3141286763Smav * If we're below arc_meta_min, always prefer to evict data. 3142286763Smav * Otherwise, try to satisfy the requested number of bytes to 3143286763Smav * evict from the type which contains older buffers; in an 3144286763Smav * effort to keep newer buffers in the cache regardless of their 3145286763Smav * type. If we cannot satisfy the number of bytes from this 3146286763Smav * type, spill over into the next type. 3147286763Smav */ 3148286763Smav if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA && 3149286763Smav arc_meta_used > arc_meta_min) { 3150286763Smav bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3151286763Smav total_evicted += bytes; 3152168404Spjd 3153286763Smav /* 3154286763Smav * If we couldn't evict our target number of bytes from 3155286763Smav * metadata, we try to get the rest from data. 3156286763Smav */ 3157286763Smav target -= bytes; 3158286763Smav 3159286763Smav total_evicted += 3160286763Smav arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3161286763Smav } else { 3162286763Smav bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3163286763Smav total_evicted += bytes; 3164286763Smav 3165286763Smav /* 3166286763Smav * If we couldn't evict our target number of bytes from 3167286763Smav * data, we try to get the rest from metadata. 3168286763Smav */ 3169286763Smav target -= bytes; 3170286763Smav 3171286763Smav total_evicted += 3172286763Smav arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3173185029Spjd } 3174185029Spjd 3175208373Smm /* 3176208373Smm * Adjust MFU size 3177286763Smav * 3178286763Smav * Now that we've tried to evict enough from the MRU to get its 3179286763Smav * size back to arc_p, if we're still above the target cache 3180286763Smav * size, we evict the rest from the MFU. 3181208373Smm */ 3182286763Smav target = arc_size - arc_c; 3183168404Spjd 3184286764Smav if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA && 3185286763Smav arc_meta_used > arc_meta_min) { 3186286763Smav bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3187286763Smav total_evicted += bytes; 3188208373Smm 3189286763Smav /* 3190286763Smav * If we couldn't evict our target number of bytes from 3191286763Smav * metadata, we try to get the rest from data. 3192286763Smav */ 3193286763Smav target -= bytes; 3194168404Spjd 3195286763Smav total_evicted += 3196286763Smav arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3197286763Smav } else { 3198286763Smav bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3199286763Smav total_evicted += bytes; 3200286763Smav 3201286763Smav /* 3202286763Smav * If we couldn't evict our target number of bytes from 3203286763Smav * data, we try to get the rest from data. 3204286763Smav */ 3205286763Smav target -= bytes; 3206286763Smav 3207286763Smav total_evicted += 3208286763Smav arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3209208373Smm } 3210168404Spjd 3211208373Smm /* 3212208373Smm * Adjust ghost lists 3213286763Smav * 3214286763Smav * In addition to the above, the ARC also defines target values 3215286763Smav * for the ghost lists. The sum of the mru list and mru ghost 3216286763Smav * list should never exceed the target size of the cache, and 3217286763Smav * the sum of the mru list, mfu list, mru ghost list, and mfu 3218286763Smav * ghost list should never exceed twice the target size of the 3219286763Smav * cache. The following logic enforces these limits on the ghost 3220286763Smav * caches, and evicts from them as needed. 3221208373Smm */ 3222286766Smav target = refcount_count(&arc_mru->arcs_size) + 3223286766Smav refcount_count(&arc_mru_ghost->arcs_size) - arc_c; 3224168404Spjd 3225286763Smav bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); 3226286763Smav total_evicted += bytes; 3227168404Spjd 3228286763Smav target -= bytes; 3229185029Spjd 3230286763Smav total_evicted += 3231286763Smav arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); 3232208373Smm 3233286763Smav /* 3234286763Smav * We assume the sum of the mru list and mfu list is less than 3235286763Smav * or equal to arc_c (we enforced this above), which means we 3236286763Smav * can use the simpler of the two equations below: 3237286763Smav * 3238286763Smav * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c 3239286763Smav * mru ghost + mfu ghost <= arc_c 3240286763Smav */ 3241286766Smav target = refcount_count(&arc_mru_ghost->arcs_size) + 3242286766Smav refcount_count(&arc_mfu_ghost->arcs_size) - arc_c; 3243286763Smav 3244286763Smav bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); 3245286763Smav total_evicted += bytes; 3246286763Smav 3247286763Smav target -= bytes; 3248286763Smav 3249286763Smav total_evicted += 3250286763Smav arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); 3251286763Smav 3252286763Smav return (total_evicted); 3253168404Spjd} 3254168404Spjd 3255168404Spjdstatic void 3256168404Spjdarc_do_user_evicts(void) 3257168404Spjd{ 3258286763Smav mutex_enter(&arc_user_evicts_lock); 3259286762Smav while (arc_eviction_list != NULL) { 3260286762Smav arc_buf_t *buf = arc_eviction_list; 3261286762Smav arc_eviction_list = buf->b_next; 3262219089Spjd mutex_enter(&buf->b_evict_lock); 3263168404Spjd buf->b_hdr = NULL; 3264219089Spjd mutex_exit(&buf->b_evict_lock); 3265286763Smav mutex_exit(&arc_user_evicts_lock); 3266168404Spjd 3267168404Spjd if (buf->b_efunc != NULL) 3268268858Sdelphij VERIFY0(buf->b_efunc(buf->b_private)); 3269168404Spjd 3270168404Spjd buf->b_efunc = NULL; 3271168404Spjd buf->b_private = NULL; 3272168404Spjd kmem_cache_free(buf_cache, buf); 3273286763Smav mutex_enter(&arc_user_evicts_lock); 3274168404Spjd } 3275286763Smav mutex_exit(&arc_user_evicts_lock); 3276168404Spjd} 3277168404Spjd 3278168404Spjdvoid 3279286763Smavarc_flush(spa_t *spa, boolean_t retry) 3280168404Spjd{ 3281209962Smm uint64_t guid = 0; 3282209962Smm 3283286763Smav /* 3284286763Smav * If retry is TRUE, a spa must not be specified since we have 3285286763Smav * no good way to determine if all of a spa's buffers have been 3286286763Smav * evicted from an arc state. 3287286763Smav */ 3288286763Smav ASSERT(!retry || spa == 0); 3289286763Smav 3290286570Smav if (spa != NULL) 3291228103Smm guid = spa_load_guid(spa); 3292209962Smm 3293286763Smav (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); 3294286763Smav (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); 3295168404Spjd 3296286763Smav (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); 3297286763Smav (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); 3298168404Spjd 3299286763Smav (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); 3300286763Smav (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); 3301286763Smav 3302286763Smav (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); 3303286763Smav (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); 3304286763Smav 3305168404Spjd arc_do_user_evicts(); 3306185029Spjd ASSERT(spa || arc_eviction_list == NULL); 3307168404Spjd} 3308168404Spjd 3309168404Spjdvoid 3310286625Smavarc_shrink(int64_t to_free) 3311168404Spjd{ 3312168404Spjd if (arc_c > arc_c_min) { 3313272483Ssmh DTRACE_PROBE4(arc__shrink, uint64_t, arc_c, uint64_t, 3314272483Ssmh arc_c_min, uint64_t, arc_p, uint64_t, to_free); 3315168404Spjd if (arc_c > arc_c_min + to_free) 3316168404Spjd atomic_add_64(&arc_c, -to_free); 3317168404Spjd else 3318168404Spjd arc_c = arc_c_min; 3319168404Spjd 3320168404Spjd atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 3321168404Spjd if (arc_c > arc_size) 3322168404Spjd arc_c = MAX(arc_size, arc_c_min); 3323168404Spjd if (arc_p > arc_c) 3324168404Spjd arc_p = (arc_c >> 1); 3325272483Ssmh 3326272483Ssmh DTRACE_PROBE2(arc__shrunk, uint64_t, arc_c, uint64_t, 3327272483Ssmh arc_p); 3328272483Ssmh 3329168404Spjd ASSERT(arc_c >= arc_c_min); 3330168404Spjd ASSERT((int64_t)arc_p >= 0); 3331168404Spjd } 3332168404Spjd 3333270759Ssmh if (arc_size > arc_c) { 3334270759Ssmh DTRACE_PROBE2(arc__shrink_adjust, uint64_t, arc_size, 3335270759Ssmh uint64_t, arc_c); 3336286763Smav (void) arc_adjust(); 3337270759Ssmh } 3338168404Spjd} 3339168404Spjd 3340286625Smavstatic long needfree = 0; 3341168404Spjd 3342286625Smavtypedef enum free_memory_reason_t { 3343286625Smav FMR_UNKNOWN, 3344286625Smav FMR_NEEDFREE, 3345286625Smav FMR_LOTSFREE, 3346286625Smav FMR_SWAPFS_MINFREE, 3347286625Smav FMR_PAGES_PP_MAXIMUM, 3348286625Smav FMR_HEAP_ARENA, 3349286625Smav FMR_ZIO_ARENA, 3350286625Smav FMR_ZIO_FRAG, 3351286625Smav} free_memory_reason_t; 3352286625Smav 3353286625Smavint64_t last_free_memory; 3354286625Smavfree_memory_reason_t last_free_reason; 3355286625Smav 3356286625Smav/* 3357286625Smav * Additional reserve of pages for pp_reserve. 3358286625Smav */ 3359286625Smavint64_t arc_pages_pp_reserve = 64; 3360286625Smav 3361286625Smav/* 3362286625Smav * Additional reserve of pages for swapfs. 3363286625Smav */ 3364286625Smavint64_t arc_swapfs_reserve = 64; 3365286625Smav 3366286625Smav/* 3367286625Smav * Return the amount of memory that can be consumed before reclaim will be 3368286625Smav * needed. Positive if there is sufficient free memory, negative indicates 3369286625Smav * the amount of memory that needs to be freed up. 3370286625Smav */ 3371286625Smavstatic int64_t 3372286625Smavarc_available_memory(void) 3373168404Spjd{ 3374286625Smav int64_t lowest = INT64_MAX; 3375286625Smav int64_t n; 3376286625Smav free_memory_reason_t r = FMR_UNKNOWN; 3377168404Spjd 3378168404Spjd#ifdef _KERNEL 3379286625Smav if (needfree > 0) { 3380286625Smav n = PAGESIZE * (-needfree); 3381286625Smav if (n < lowest) { 3382286625Smav lowest = n; 3383286625Smav r = FMR_NEEDFREE; 3384286625Smav } 3385270759Ssmh } 3386168404Spjd 3387191902Skmacy /* 3388212780Savg * Cooperate with pagedaemon when it's time for it to scan 3389212780Savg * and reclaim some pages. 3390191902Skmacy */ 3391286655Smav n = PAGESIZE * ((int64_t)freemem - zfs_arc_free_target); 3392286625Smav if (n < lowest) { 3393286625Smav lowest = n; 3394286625Smav r = FMR_LOTSFREE; 3395270759Ssmh } 3396191902Skmacy 3397277300Ssmh#ifdef illumos 3398168404Spjd /* 3399185029Spjd * check that we're out of range of the pageout scanner. It starts to 3400185029Spjd * schedule paging if freemem is less than lotsfree and needfree. 3401185029Spjd * lotsfree is the high-water mark for pageout, and needfree is the 3402185029Spjd * number of needed free pages. We add extra pages here to make sure 3403185029Spjd * the scanner doesn't start up while we're freeing memory. 3404185029Spjd */ 3405286625Smav n = PAGESIZE * (freemem - lotsfree - needfree - desfree); 3406286625Smav if (n < lowest) { 3407286625Smav lowest = n; 3408286625Smav r = FMR_LOTSFREE; 3409286625Smav } 3410185029Spjd 3411185029Spjd /* 3412168404Spjd * check to make sure that swapfs has enough space so that anon 3413185029Spjd * reservations can still succeed. anon_resvmem() checks that the 3414168404Spjd * availrmem is greater than swapfs_minfree, and the number of reserved 3415168404Spjd * swap pages. We also add a bit of extra here just to prevent 3416168404Spjd * circumstances from getting really dire. 3417168404Spjd */ 3418286625Smav n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve - 3419286625Smav desfree - arc_swapfs_reserve); 3420286625Smav if (n < lowest) { 3421286625Smav lowest = n; 3422286625Smav r = FMR_SWAPFS_MINFREE; 3423286625Smav } 3424168404Spjd 3425286625Smav 3426168404Spjd /* 3427272483Ssmh * Check that we have enough availrmem that memory locking (e.g., via 3428272483Ssmh * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 3429272483Ssmh * stores the number of pages that cannot be locked; when availrmem 3430272483Ssmh * drops below pages_pp_maximum, page locking mechanisms such as 3431272483Ssmh * page_pp_lock() will fail.) 3432272483Ssmh */ 3433286625Smav n = PAGESIZE * (availrmem - pages_pp_maximum - 3434286625Smav arc_pages_pp_reserve); 3435286625Smav if (n < lowest) { 3436286625Smav lowest = n; 3437286625Smav r = FMR_PAGES_PP_MAXIMUM; 3438286625Smav } 3439272483Ssmh 3440277300Ssmh#endif /* illumos */ 3441272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 3442272483Ssmh /* 3443168404Spjd * If we're on an i386 platform, it's possible that we'll exhaust the 3444168404Spjd * kernel heap space before we ever run out of available physical 3445168404Spjd * memory. Most checks of the size of the heap_area compare against 3446168404Spjd * tune.t_minarmem, which is the minimum available real memory that we 3447168404Spjd * can have in the system. However, this is generally fixed at 25 pages 3448168404Spjd * which is so low that it's useless. In this comparison, we seek to 3449168404Spjd * calculate the total heap-size, and reclaim if more than 3/4ths of the 3450185029Spjd * heap is allocated. (Or, in the calculation, if less than 1/4th is 3451168404Spjd * free) 3452168404Spjd */ 3453286655Smav n = (int64_t)vmem_size(heap_arena, VMEM_FREE) - 3454286628Smav (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2); 3455286625Smav if (n < lowest) { 3456286625Smav lowest = n; 3457286625Smav r = FMR_HEAP_ARENA; 3458270861Ssmh } 3459281026Smav#define zio_arena NULL 3460281026Smav#else 3461281026Smav#define zio_arena heap_arena 3462270861Ssmh#endif 3463281026Smav 3464272483Ssmh /* 3465272483Ssmh * If zio data pages are being allocated out of a separate heap segment, 3466272483Ssmh * then enforce that the size of available vmem for this arena remains 3467272483Ssmh * above about 1/16th free. 3468272483Ssmh * 3469272483Ssmh * Note: The 1/16th arena free requirement was put in place 3470272483Ssmh * to aggressively evict memory from the arc in order to avoid 3471272483Ssmh * memory fragmentation issues. 3472272483Ssmh */ 3473286625Smav if (zio_arena != NULL) { 3474286655Smav n = (int64_t)vmem_size(zio_arena, VMEM_FREE) - 3475286625Smav (vmem_size(zio_arena, VMEM_ALLOC) >> 4); 3476286625Smav if (n < lowest) { 3477286625Smav lowest = n; 3478286625Smav r = FMR_ZIO_ARENA; 3479286625Smav } 3480286625Smav } 3481281026Smav 3482281026Smav /* 3483281026Smav * Above limits know nothing about real level of KVA fragmentation. 3484281026Smav * Start aggressive reclamation if too little sequential KVA left. 3485281026Smav */ 3486286625Smav if (lowest > 0) { 3487286625Smav n = (vmem_size(heap_arena, VMEM_MAXFREE) < zfs_max_recordsize) ? 3488286655Smav -((int64_t)vmem_size(heap_arena, VMEM_ALLOC) >> 4) : 3489286655Smav INT64_MAX; 3490286625Smav if (n < lowest) { 3491286625Smav lowest = n; 3492286625Smav r = FMR_ZIO_FRAG; 3493286625Smav } 3494281109Smav } 3495281026Smav 3496272483Ssmh#else /* _KERNEL */ 3497286625Smav /* Every 100 calls, free a small amount */ 3498168404Spjd if (spa_get_random(100) == 0) 3499286625Smav lowest = -1024; 3500272483Ssmh#endif /* _KERNEL */ 3501270759Ssmh 3502286625Smav last_free_memory = lowest; 3503286625Smav last_free_reason = r; 3504286625Smav DTRACE_PROBE2(arc__available_memory, int64_t, lowest, int, r); 3505286625Smav return (lowest); 3506168404Spjd} 3507168404Spjd 3508286625Smav 3509286625Smav/* 3510286625Smav * Determine if the system is under memory pressure and is asking 3511286625Smav * to reclaim memory. A return value of TRUE indicates that the system 3512286625Smav * is under memory pressure and that the arc should adjust accordingly. 3513286625Smav */ 3514286625Smavstatic boolean_t 3515286625Smavarc_reclaim_needed(void) 3516286625Smav{ 3517286625Smav return (arc_available_memory() < 0); 3518286625Smav} 3519286625Smav 3520208454Spjdextern kmem_cache_t *zio_buf_cache[]; 3521208454Spjdextern kmem_cache_t *zio_data_buf_cache[]; 3522272527Sdelphijextern kmem_cache_t *range_seg_cache; 3523208454Spjd 3524278040Ssmhstatic __noinline void 3525286625Smavarc_kmem_reap_now(void) 3526168404Spjd{ 3527168404Spjd size_t i; 3528168404Spjd kmem_cache_t *prev_cache = NULL; 3529168404Spjd kmem_cache_t *prev_data_cache = NULL; 3530168404Spjd 3531272483Ssmh DTRACE_PROBE(arc__kmem_reap_start); 3532168404Spjd#ifdef _KERNEL 3533185029Spjd if (arc_meta_used >= arc_meta_limit) { 3534185029Spjd /* 3535185029Spjd * We are exceeding our meta-data cache limit. 3536185029Spjd * Purge some DNLC entries to release holds on meta-data. 3537185029Spjd */ 3538185029Spjd dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 3539185029Spjd } 3540168404Spjd#if defined(__i386) 3541168404Spjd /* 3542168404Spjd * Reclaim unused memory from all kmem caches. 3543168404Spjd */ 3544168404Spjd kmem_reap(); 3545168404Spjd#endif 3546168404Spjd#endif 3547168404Spjd 3548168404Spjd for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 3549168404Spjd if (zio_buf_cache[i] != prev_cache) { 3550168404Spjd prev_cache = zio_buf_cache[i]; 3551168404Spjd kmem_cache_reap_now(zio_buf_cache[i]); 3552168404Spjd } 3553168404Spjd if (zio_data_buf_cache[i] != prev_data_cache) { 3554168404Spjd prev_data_cache = zio_data_buf_cache[i]; 3555168404Spjd kmem_cache_reap_now(zio_data_buf_cache[i]); 3556168404Spjd } 3557168404Spjd } 3558168404Spjd kmem_cache_reap_now(buf_cache); 3559286570Smav kmem_cache_reap_now(hdr_full_cache); 3560286570Smav kmem_cache_reap_now(hdr_l2only_cache); 3561272506Sdelphij kmem_cache_reap_now(range_seg_cache); 3562272483Ssmh 3563277300Ssmh#ifdef illumos 3564286625Smav if (zio_arena != NULL) { 3565286625Smav /* 3566286625Smav * Ask the vmem arena to reclaim unused memory from its 3567286625Smav * quantum caches. 3568286625Smav */ 3569272483Ssmh vmem_qcache_reap(zio_arena); 3570286625Smav } 3571272483Ssmh#endif 3572272483Ssmh DTRACE_PROBE(arc__kmem_reap_end); 3573168404Spjd} 3574168404Spjd 3575286763Smav/* 3576286763Smav * Threads can block in arc_get_data_buf() waiting for this thread to evict 3577286763Smav * enough data and signal them to proceed. When this happens, the threads in 3578286763Smav * arc_get_data_buf() are sleeping while holding the hash lock for their 3579286763Smav * particular arc header. Thus, we must be careful to never sleep on a 3580286763Smav * hash lock in this thread. This is to prevent the following deadlock: 3581286763Smav * 3582286763Smav * - Thread A sleeps on CV in arc_get_data_buf() holding hash lock "L", 3583286763Smav * waiting for the reclaim thread to signal it. 3584286763Smav * 3585286763Smav * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter, 3586286763Smav * fails, and goes to sleep forever. 3587286763Smav * 3588286763Smav * This possible deadlock is avoided by always acquiring a hash lock 3589286763Smav * using mutex_tryenter() from arc_reclaim_thread(). 3590286763Smav */ 3591168404Spjdstatic void 3592168404Spjdarc_reclaim_thread(void *dummy __unused) 3593168404Spjd{ 3594168404Spjd clock_t growtime = 0; 3595168404Spjd callb_cpr_t cpr; 3596168404Spjd 3597286763Smav CALLB_CPR_INIT(&cpr, &arc_reclaim_lock, callb_generic_cpr, FTAG); 3598168404Spjd 3599286763Smav mutex_enter(&arc_reclaim_lock); 3600286763Smav while (!arc_reclaim_thread_exit) { 3601286625Smav int64_t free_memory = arc_available_memory(); 3602286763Smav uint64_t evicted = 0; 3603286763Smav 3604286763Smav mutex_exit(&arc_reclaim_lock); 3605286763Smav 3606286625Smav if (free_memory < 0) { 3607168404Spjd 3608286625Smav arc_no_grow = B_TRUE; 3609286625Smav arc_warm = B_TRUE; 3610168404Spjd 3611286625Smav /* 3612286625Smav * Wait at least zfs_grow_retry (default 60) seconds 3613286625Smav * before considering growing. 3614286625Smav */ 3615219089Spjd growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 3616168404Spjd 3617286625Smav arc_kmem_reap_now(); 3618286625Smav 3619286625Smav /* 3620286625Smav * If we are still low on memory, shrink the ARC 3621286625Smav * so that we have arc_shrink_min free space. 3622286625Smav */ 3623286625Smav free_memory = arc_available_memory(); 3624286625Smav 3625286625Smav int64_t to_free = 3626286625Smav (arc_c >> arc_shrink_shift) - free_memory; 3627286625Smav if (to_free > 0) { 3628286625Smav#ifdef _KERNEL 3629286625Smav to_free = MAX(to_free, ptob(needfree)); 3630286625Smav#endif 3631286625Smav arc_shrink(to_free); 3632168404Spjd } 3633286625Smav } else if (free_memory < arc_c >> arc_no_grow_shift) { 3634286625Smav arc_no_grow = B_TRUE; 3635286625Smav } else if (ddi_get_lbolt() >= growtime) { 3636286625Smav arc_no_grow = B_FALSE; 3637168404Spjd } 3638168404Spjd 3639286763Smav evicted = arc_adjust(); 3640168404Spjd 3641286763Smav mutex_enter(&arc_reclaim_lock); 3642168404Spjd 3643286763Smav /* 3644286763Smav * If evicted is zero, we couldn't evict anything via 3645286763Smav * arc_adjust(). This could be due to hash lock 3646286763Smav * collisions, but more likely due to the majority of 3647286763Smav * arc buffers being unevictable. Therefore, even if 3648286763Smav * arc_size is above arc_c, another pass is unlikely to 3649286763Smav * be helpful and could potentially cause us to enter an 3650286763Smav * infinite loop. 3651286763Smav */ 3652286763Smav if (arc_size <= arc_c || evicted == 0) { 3653211762Savg#ifdef _KERNEL 3654185029Spjd needfree = 0; 3655168404Spjd#endif 3656286763Smav /* 3657286763Smav * We're either no longer overflowing, or we 3658286763Smav * can't evict anything more, so we should wake 3659286763Smav * up any threads before we go to sleep. 3660286763Smav */ 3661286763Smav cv_broadcast(&arc_reclaim_waiters_cv); 3662168404Spjd 3663286763Smav /* 3664286763Smav * Block until signaled, or after one second (we 3665286763Smav * might need to perform arc_kmem_reap_now() 3666286763Smav * even if we aren't being signalled) 3667286763Smav */ 3668286763Smav CALLB_CPR_SAFE_BEGIN(&cpr); 3669286763Smav (void) cv_timedwait(&arc_reclaim_thread_cv, 3670286763Smav &arc_reclaim_lock, hz); 3671286763Smav CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_lock); 3672286763Smav } 3673286763Smav } 3674286763Smav 3675286763Smav arc_reclaim_thread_exit = FALSE; 3676286763Smav cv_broadcast(&arc_reclaim_thread_cv); 3677286763Smav CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_lock */ 3678286763Smav thread_exit(); 3679286763Smav} 3680286763Smav 3681286763Smavstatic void 3682286763Smavarc_user_evicts_thread(void *dummy __unused) 3683286763Smav{ 3684286763Smav callb_cpr_t cpr; 3685286763Smav 3686286763Smav CALLB_CPR_INIT(&cpr, &arc_user_evicts_lock, callb_generic_cpr, FTAG); 3687286763Smav 3688286763Smav mutex_enter(&arc_user_evicts_lock); 3689286763Smav while (!arc_user_evicts_thread_exit) { 3690286763Smav mutex_exit(&arc_user_evicts_lock); 3691286763Smav 3692286763Smav arc_do_user_evicts(); 3693286763Smav 3694286574Smav /* 3695286574Smav * This is necessary in order for the mdb ::arc dcmd to 3696286574Smav * show up to date information. Since the ::arc command 3697286574Smav * does not call the kstat's update function, without 3698286574Smav * this call, the command may show stale stats for the 3699286574Smav * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even 3700286574Smav * with this change, the data might be up to 1 second 3701286574Smav * out of date; but that should suffice. The arc_state_t 3702286574Smav * structures can be queried directly if more accurate 3703286574Smav * information is needed. 3704286574Smav */ 3705286574Smav if (arc_ksp != NULL) 3706286574Smav arc_ksp->ks_update(arc_ksp, KSTAT_READ); 3707286574Smav 3708286763Smav mutex_enter(&arc_user_evicts_lock); 3709286763Smav 3710286763Smav /* 3711286763Smav * Block until signaled, or after one second (we need to 3712286763Smav * call the arc's kstat update function regularly). 3713286763Smav */ 3714168404Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 3715286763Smav (void) cv_timedwait(&arc_user_evicts_cv, 3716286763Smav &arc_user_evicts_lock, hz); 3717286763Smav CALLB_CPR_SAFE_END(&cpr, &arc_user_evicts_lock); 3718168404Spjd } 3719168404Spjd 3720286763Smav arc_user_evicts_thread_exit = FALSE; 3721286763Smav cv_broadcast(&arc_user_evicts_cv); 3722286763Smav CALLB_CPR_EXIT(&cpr); /* drops arc_user_evicts_lock */ 3723168404Spjd thread_exit(); 3724168404Spjd} 3725168404Spjd 3726168404Spjd/* 3727168404Spjd * Adapt arc info given the number of bytes we are trying to add and 3728168404Spjd * the state that we are comming from. This function is only called 3729168404Spjd * when we are adding new content to the cache. 3730168404Spjd */ 3731168404Spjdstatic void 3732168404Spjdarc_adapt(int bytes, arc_state_t *state) 3733168404Spjd{ 3734168404Spjd int mult; 3735208373Smm uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 3736286766Smav int64_t mrug_size = refcount_count(&arc_mru_ghost->arcs_size); 3737286766Smav int64_t mfug_size = refcount_count(&arc_mfu_ghost->arcs_size); 3738168404Spjd 3739185029Spjd if (state == arc_l2c_only) 3740185029Spjd return; 3741185029Spjd 3742168404Spjd ASSERT(bytes > 0); 3743168404Spjd /* 3744168404Spjd * Adapt the target size of the MRU list: 3745168404Spjd * - if we just hit in the MRU ghost list, then increase 3746168404Spjd * the target size of the MRU list. 3747168404Spjd * - if we just hit in the MFU ghost list, then increase 3748168404Spjd * the target size of the MFU list by decreasing the 3749168404Spjd * target size of the MRU list. 3750168404Spjd */ 3751168404Spjd if (state == arc_mru_ghost) { 3752286766Smav mult = (mrug_size >= mfug_size) ? 1 : (mfug_size / mrug_size); 3753209275Smm mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 3754168404Spjd 3755208373Smm arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 3756168404Spjd } else if (state == arc_mfu_ghost) { 3757208373Smm uint64_t delta; 3758208373Smm 3759286766Smav mult = (mfug_size >= mrug_size) ? 1 : (mrug_size / mfug_size); 3760209275Smm mult = MIN(mult, 10); 3761168404Spjd 3762208373Smm delta = MIN(bytes * mult, arc_p); 3763208373Smm arc_p = MAX(arc_p_min, arc_p - delta); 3764168404Spjd } 3765168404Spjd ASSERT((int64_t)arc_p >= 0); 3766168404Spjd 3767168404Spjd if (arc_reclaim_needed()) { 3768286763Smav cv_signal(&arc_reclaim_thread_cv); 3769168404Spjd return; 3770168404Spjd } 3771168404Spjd 3772168404Spjd if (arc_no_grow) 3773168404Spjd return; 3774168404Spjd 3775168404Spjd if (arc_c >= arc_c_max) 3776168404Spjd return; 3777168404Spjd 3778168404Spjd /* 3779168404Spjd * If we're within (2 * maxblocksize) bytes of the target 3780168404Spjd * cache size, increment the target cache size 3781168404Spjd */ 3782168404Spjd if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 3783272483Ssmh DTRACE_PROBE1(arc__inc_adapt, int, bytes); 3784168404Spjd atomic_add_64(&arc_c, (int64_t)bytes); 3785168404Spjd if (arc_c > arc_c_max) 3786168404Spjd arc_c = arc_c_max; 3787168404Spjd else if (state == arc_anon) 3788168404Spjd atomic_add_64(&arc_p, (int64_t)bytes); 3789168404Spjd if (arc_p > arc_c) 3790168404Spjd arc_p = arc_c; 3791168404Spjd } 3792168404Spjd ASSERT((int64_t)arc_p >= 0); 3793168404Spjd} 3794168404Spjd 3795168404Spjd/* 3796286763Smav * Check if arc_size has grown past our upper threshold, determined by 3797286763Smav * zfs_arc_overflow_shift. 3798168404Spjd */ 3799286763Smavstatic boolean_t 3800286763Smavarc_is_overflowing(void) 3801168404Spjd{ 3802286763Smav /* Always allow at least one block of overflow */ 3803286763Smav uint64_t overflow = MAX(SPA_MAXBLOCKSIZE, 3804286763Smav arc_c >> zfs_arc_overflow_shift); 3805185029Spjd 3806286763Smav return (arc_size >= arc_c + overflow); 3807168404Spjd} 3808168404Spjd 3809168404Spjd/* 3810286763Smav * The buffer, supplied as the first argument, needs a data block. If we 3811286763Smav * are hitting the hard limit for the cache size, we must sleep, waiting 3812286763Smav * for the eviction thread to catch up. If we're past the target size 3813286763Smav * but below the hard limit, we'll only signal the reclaim thread and 3814286763Smav * continue on. 3815168404Spjd */ 3816168404Spjdstatic void 3817168404Spjdarc_get_data_buf(arc_buf_t *buf) 3818168404Spjd{ 3819286570Smav arc_state_t *state = buf->b_hdr->b_l1hdr.b_state; 3820168404Spjd uint64_t size = buf->b_hdr->b_size; 3821286570Smav arc_buf_contents_t type = arc_buf_type(buf->b_hdr); 3822168404Spjd 3823168404Spjd arc_adapt(size, state); 3824168404Spjd 3825168404Spjd /* 3826286763Smav * If arc_size is currently overflowing, and has grown past our 3827286763Smav * upper limit, we must be adding data faster than the evict 3828286763Smav * thread can evict. Thus, to ensure we don't compound the 3829286763Smav * problem by adding more data and forcing arc_size to grow even 3830286763Smav * further past it's target size, we halt and wait for the 3831286763Smav * eviction thread to catch up. 3832286763Smav * 3833286763Smav * It's also possible that the reclaim thread is unable to evict 3834286763Smav * enough buffers to get arc_size below the overflow limit (e.g. 3835286763Smav * due to buffers being un-evictable, or hash lock collisions). 3836286763Smav * In this case, we want to proceed regardless if we're 3837286763Smav * overflowing; thus we don't use a while loop here. 3838168404Spjd */ 3839286763Smav if (arc_is_overflowing()) { 3840286763Smav mutex_enter(&arc_reclaim_lock); 3841286763Smav 3842286763Smav /* 3843286763Smav * Now that we've acquired the lock, we may no longer be 3844286763Smav * over the overflow limit, lets check. 3845286763Smav * 3846286763Smav * We're ignoring the case of spurious wake ups. If that 3847286763Smav * were to happen, it'd let this thread consume an ARC 3848286763Smav * buffer before it should have (i.e. before we're under 3849286763Smav * the overflow limit and were signalled by the reclaim 3850286763Smav * thread). As long as that is a rare occurrence, it 3851286763Smav * shouldn't cause any harm. 3852286763Smav */ 3853286763Smav if (arc_is_overflowing()) { 3854286763Smav cv_signal(&arc_reclaim_thread_cv); 3855286763Smav cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); 3856168404Spjd } 3857286763Smav 3858286763Smav mutex_exit(&arc_reclaim_lock); 3859168404Spjd } 3860168404Spjd 3861286763Smav if (type == ARC_BUFC_METADATA) { 3862286763Smav buf->b_data = zio_buf_alloc(size); 3863286763Smav arc_space_consume(size, ARC_SPACE_META); 3864168404Spjd } else { 3865286763Smav ASSERT(type == ARC_BUFC_DATA); 3866286763Smav buf->b_data = zio_data_buf_alloc(size); 3867286763Smav arc_space_consume(size, ARC_SPACE_DATA); 3868168404Spjd } 3869286763Smav 3870168404Spjd /* 3871168404Spjd * Update the state size. Note that ghost states have a 3872168404Spjd * "ghost size" and so don't need to be updated. 3873168404Spjd */ 3874286570Smav if (!GHOST_STATE(buf->b_hdr->b_l1hdr.b_state)) { 3875168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3876286766Smav arc_state_t *state = hdr->b_l1hdr.b_state; 3877168404Spjd 3878286766Smav (void) refcount_add_many(&state->arcs_size, size, buf); 3879286763Smav 3880286763Smav /* 3881286763Smav * If this is reached via arc_read, the link is 3882286763Smav * protected by the hash lock. If reached via 3883286763Smav * arc_buf_alloc, the header should not be accessed by 3884286763Smav * any other thread. And, if reached via arc_read_done, 3885286763Smav * the hash lock will protect it if it's found in the 3886286763Smav * hash table; otherwise no other thread should be 3887286763Smav * trying to [add|remove]_reference it. 3888286763Smav */ 3889286763Smav if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 3890286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 3891286570Smav atomic_add_64(&hdr->b_l1hdr.b_state->arcs_lsize[type], 3892286570Smav size); 3893168404Spjd } 3894168404Spjd /* 3895168404Spjd * If we are growing the cache, and we are adding anonymous 3896168404Spjd * data, and we have outgrown arc_p, update arc_p 3897168404Spjd */ 3898286570Smav if (arc_size < arc_c && hdr->b_l1hdr.b_state == arc_anon && 3899286766Smav (refcount_count(&arc_anon->arcs_size) + 3900286766Smav refcount_count(&arc_mru->arcs_size) > arc_p)) 3901168404Spjd arc_p = MIN(arc_c, arc_p + size); 3902168404Spjd } 3903205231Skmacy ARCSTAT_BUMP(arcstat_allocated); 3904168404Spjd} 3905168404Spjd 3906168404Spjd/* 3907168404Spjd * This routine is called whenever a buffer is accessed. 3908168404Spjd * NOTE: the hash lock is dropped in this function. 3909168404Spjd */ 3910168404Spjdstatic void 3911275811Sdelphijarc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 3912168404Spjd{ 3913219089Spjd clock_t now; 3914219089Spjd 3915168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 3916286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 3917168404Spjd 3918286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 3919168404Spjd /* 3920168404Spjd * This buffer is not in the cache, and does not 3921168404Spjd * appear in our "ghost" list. Add the new buffer 3922168404Spjd * to the MRU state. 3923168404Spjd */ 3924168404Spjd 3925286570Smav ASSERT0(hdr->b_l1hdr.b_arc_access); 3926286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3927275811Sdelphij DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 3928275811Sdelphij arc_change_state(arc_mru, hdr, hash_lock); 3929168404Spjd 3930286570Smav } else if (hdr->b_l1hdr.b_state == arc_mru) { 3931219089Spjd now = ddi_get_lbolt(); 3932219089Spjd 3933168404Spjd /* 3934168404Spjd * If this buffer is here because of a prefetch, then either: 3935168404Spjd * - clear the flag if this is a "referencing" read 3936168404Spjd * (any subsequent access will bump this into the MFU state). 3937168404Spjd * or 3938168404Spjd * - move the buffer to the head of the list if this is 3939168404Spjd * another prefetch (to make it less likely to be evicted). 3940168404Spjd */ 3941286570Smav if (HDR_PREFETCH(hdr)) { 3942286570Smav if (refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 3943286763Smav /* link protected by hash lock */ 3944286763Smav ASSERT(multilist_link_active( 3945286570Smav &hdr->b_l1hdr.b_arc_node)); 3946168404Spjd } else { 3947275811Sdelphij hdr->b_flags &= ~ARC_FLAG_PREFETCH; 3948168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 3949168404Spjd } 3950286570Smav hdr->b_l1hdr.b_arc_access = now; 3951168404Spjd return; 3952168404Spjd } 3953168404Spjd 3954168404Spjd /* 3955168404Spjd * This buffer has been "accessed" only once so far, 3956168404Spjd * but it is still in the cache. Move it to the MFU 3957168404Spjd * state. 3958168404Spjd */ 3959286570Smav if (now > hdr->b_l1hdr.b_arc_access + ARC_MINTIME) { 3960168404Spjd /* 3961168404Spjd * More than 125ms have passed since we 3962168404Spjd * instantiated this buffer. Move it to the 3963168404Spjd * most frequently used state. 3964168404Spjd */ 3965286570Smav hdr->b_l1hdr.b_arc_access = now; 3966275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3967275811Sdelphij arc_change_state(arc_mfu, hdr, hash_lock); 3968168404Spjd } 3969168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 3970286570Smav } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { 3971168404Spjd arc_state_t *new_state; 3972168404Spjd /* 3973168404Spjd * This buffer has been "accessed" recently, but 3974168404Spjd * was evicted from the cache. Move it to the 3975168404Spjd * MFU state. 3976168404Spjd */ 3977168404Spjd 3978286570Smav if (HDR_PREFETCH(hdr)) { 3979168404Spjd new_state = arc_mru; 3980286570Smav if (refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) 3981275811Sdelphij hdr->b_flags &= ~ARC_FLAG_PREFETCH; 3982275811Sdelphij DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 3983168404Spjd } else { 3984168404Spjd new_state = arc_mfu; 3985275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3986168404Spjd } 3987168404Spjd 3988286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3989275811Sdelphij arc_change_state(new_state, hdr, hash_lock); 3990168404Spjd 3991168404Spjd ARCSTAT_BUMP(arcstat_mru_ghost_hits); 3992286570Smav } else if (hdr->b_l1hdr.b_state == arc_mfu) { 3993168404Spjd /* 3994168404Spjd * This buffer has been accessed more than once and is 3995168404Spjd * still in the cache. Keep it in the MFU state. 3996168404Spjd * 3997168404Spjd * NOTE: an add_reference() that occurred when we did 3998168404Spjd * the arc_read() will have kicked this off the list. 3999168404Spjd * If it was a prefetch, we will explicitly move it to 4000168404Spjd * the head of the list now. 4001168404Spjd */ 4002286570Smav if ((HDR_PREFETCH(hdr)) != 0) { 4003286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4004286763Smav /* link protected by hash_lock */ 4005286763Smav ASSERT(multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 4006168404Spjd } 4007168404Spjd ARCSTAT_BUMP(arcstat_mfu_hits); 4008286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4009286570Smav } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { 4010168404Spjd arc_state_t *new_state = arc_mfu; 4011168404Spjd /* 4012168404Spjd * This buffer has been accessed more than once but has 4013168404Spjd * been evicted from the cache. Move it back to the 4014168404Spjd * MFU state. 4015168404Spjd */ 4016168404Spjd 4017286570Smav if (HDR_PREFETCH(hdr)) { 4018168404Spjd /* 4019168404Spjd * This is a prefetch access... 4020168404Spjd * move this block back to the MRU state. 4021168404Spjd */ 4022286570Smav ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 4023168404Spjd new_state = arc_mru; 4024168404Spjd } 4025168404Spjd 4026286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4027275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4028275811Sdelphij arc_change_state(new_state, hdr, hash_lock); 4029168404Spjd 4030168404Spjd ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 4031286570Smav } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { 4032185029Spjd /* 4033185029Spjd * This buffer is on the 2nd Level ARC. 4034185029Spjd */ 4035185029Spjd 4036286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4037275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4038275811Sdelphij arc_change_state(arc_mfu, hdr, hash_lock); 4039168404Spjd } else { 4040168404Spjd ASSERT(!"invalid arc state"); 4041168404Spjd } 4042168404Spjd} 4043168404Spjd 4044168404Spjd/* a generic arc_done_func_t which you can use */ 4045168404Spjd/* ARGSUSED */ 4046168404Spjdvoid 4047168404Spjdarc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 4048168404Spjd{ 4049219089Spjd if (zio == NULL || zio->io_error == 0) 4050219089Spjd bcopy(buf->b_data, arg, buf->b_hdr->b_size); 4051248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 4052168404Spjd} 4053168404Spjd 4054185029Spjd/* a generic arc_done_func_t */ 4055168404Spjdvoid 4056168404Spjdarc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 4057168404Spjd{ 4058168404Spjd arc_buf_t **bufp = arg; 4059168404Spjd if (zio && zio->io_error) { 4060248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 4061168404Spjd *bufp = NULL; 4062168404Spjd } else { 4063168404Spjd *bufp = buf; 4064219089Spjd ASSERT(buf->b_data); 4065168404Spjd } 4066168404Spjd} 4067168404Spjd 4068168404Spjdstatic void 4069168404Spjdarc_read_done(zio_t *zio) 4070168404Spjd{ 4071268075Sdelphij arc_buf_hdr_t *hdr; 4072168404Spjd arc_buf_t *buf; 4073168404Spjd arc_buf_t *abuf; /* buffer we're assigning to callback */ 4074268075Sdelphij kmutex_t *hash_lock = NULL; 4075168404Spjd arc_callback_t *callback_list, *acb; 4076168404Spjd int freeable = FALSE; 4077168404Spjd 4078168404Spjd buf = zio->io_private; 4079168404Spjd hdr = buf->b_hdr; 4080168404Spjd 4081168404Spjd /* 4082168404Spjd * The hdr was inserted into hash-table and removed from lists 4083168404Spjd * prior to starting I/O. We should find this header, since 4084168404Spjd * it's in the hash table, and it should be legit since it's 4085168404Spjd * not possible to evict it during the I/O. The only possible 4086168404Spjd * reason for it not to be found is if we were freed during the 4087168404Spjd * read. 4088168404Spjd */ 4089268075Sdelphij if (HDR_IN_HASH_TABLE(hdr)) { 4090268075Sdelphij ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 4091268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[0], ==, 4092268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[0]); 4093268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[1], ==, 4094268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[1]); 4095168404Spjd 4096268075Sdelphij arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 4097268075Sdelphij &hash_lock); 4098168404Spjd 4099268075Sdelphij ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && 4100268075Sdelphij hash_lock == NULL) || 4101268075Sdelphij (found == hdr && 4102268075Sdelphij DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 4103268075Sdelphij (found == hdr && HDR_L2_READING(hdr))); 4104268075Sdelphij } 4105268075Sdelphij 4106275811Sdelphij hdr->b_flags &= ~ARC_FLAG_L2_EVICTED; 4107286570Smav if (l2arc_noprefetch && HDR_PREFETCH(hdr)) 4108275811Sdelphij hdr->b_flags &= ~ARC_FLAG_L2CACHE; 4109206796Spjd 4110168404Spjd /* byteswap if necessary */ 4111286570Smav callback_list = hdr->b_l1hdr.b_acb; 4112168404Spjd ASSERT(callback_list != NULL); 4113209101Smm if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 4114236884Smm dmu_object_byteswap_t bswap = 4115236884Smm DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 4116185029Spjd arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 4117185029Spjd byteswap_uint64_array : 4118236884Smm dmu_ot_byteswap[bswap].ob_func; 4119185029Spjd func(buf->b_data, hdr->b_size); 4120185029Spjd } 4121168404Spjd 4122185029Spjd arc_cksum_compute(buf, B_FALSE); 4123240133Smm#ifdef illumos 4124240133Smm arc_buf_watch(buf); 4125277300Ssmh#endif 4126168404Spjd 4127286570Smav if (hash_lock && zio->io_error == 0 && 4128286570Smav hdr->b_l1hdr.b_state == arc_anon) { 4129219089Spjd /* 4130219089Spjd * Only call arc_access on anonymous buffers. This is because 4131219089Spjd * if we've issued an I/O for an evicted buffer, we've already 4132219089Spjd * called arc_access (to prevent any simultaneous readers from 4133219089Spjd * getting confused). 4134219089Spjd */ 4135219089Spjd arc_access(hdr, hash_lock); 4136219089Spjd } 4137219089Spjd 4138168404Spjd /* create copies of the data buffer for the callers */ 4139168404Spjd abuf = buf; 4140168404Spjd for (acb = callback_list; acb; acb = acb->acb_next) { 4141168404Spjd if (acb->acb_done) { 4142242845Sdelphij if (abuf == NULL) { 4143242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_reads); 4144168404Spjd abuf = arc_buf_clone(buf); 4145242845Sdelphij } 4146168404Spjd acb->acb_buf = abuf; 4147168404Spjd abuf = NULL; 4148168404Spjd } 4149168404Spjd } 4150286570Smav hdr->b_l1hdr.b_acb = NULL; 4151275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 4152168404Spjd ASSERT(!HDR_BUF_AVAILABLE(hdr)); 4153219089Spjd if (abuf == buf) { 4154219089Spjd ASSERT(buf->b_efunc == NULL); 4155286570Smav ASSERT(hdr->b_l1hdr.b_datacnt == 1); 4156275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 4157219089Spjd } 4158168404Spjd 4159286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || 4160286570Smav callback_list != NULL); 4161168404Spjd 4162168404Spjd if (zio->io_error != 0) { 4163275811Sdelphij hdr->b_flags |= ARC_FLAG_IO_ERROR; 4164286570Smav if (hdr->b_l1hdr.b_state != arc_anon) 4165168404Spjd arc_change_state(arc_anon, hdr, hash_lock); 4166168404Spjd if (HDR_IN_HASH_TABLE(hdr)) 4167168404Spjd buf_hash_remove(hdr); 4168286570Smav freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4169168404Spjd } 4170168404Spjd 4171168404Spjd /* 4172168404Spjd * Broadcast before we drop the hash_lock to avoid the possibility 4173168404Spjd * that the hdr (and hence the cv) might be freed before we get to 4174168404Spjd * the cv_broadcast(). 4175168404Spjd */ 4176286570Smav cv_broadcast(&hdr->b_l1hdr.b_cv); 4177168404Spjd 4178286570Smav if (hash_lock != NULL) { 4179168404Spjd mutex_exit(hash_lock); 4180168404Spjd } else { 4181168404Spjd /* 4182168404Spjd * This block was freed while we waited for the read to 4183168404Spjd * complete. It has been removed from the hash table and 4184168404Spjd * moved to the anonymous state (so that it won't show up 4185168404Spjd * in the cache). 4186168404Spjd */ 4187286570Smav ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 4188286570Smav freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4189168404Spjd } 4190168404Spjd 4191168404Spjd /* execute each callback and free its structure */ 4192168404Spjd while ((acb = callback_list) != NULL) { 4193168404Spjd if (acb->acb_done) 4194168404Spjd acb->acb_done(zio, acb->acb_buf, acb->acb_private); 4195168404Spjd 4196168404Spjd if (acb->acb_zio_dummy != NULL) { 4197168404Spjd acb->acb_zio_dummy->io_error = zio->io_error; 4198168404Spjd zio_nowait(acb->acb_zio_dummy); 4199168404Spjd } 4200168404Spjd 4201168404Spjd callback_list = acb->acb_next; 4202168404Spjd kmem_free(acb, sizeof (arc_callback_t)); 4203168404Spjd } 4204168404Spjd 4205168404Spjd if (freeable) 4206168404Spjd arc_hdr_destroy(hdr); 4207168404Spjd} 4208168404Spjd 4209168404Spjd/* 4210286762Smav * "Read" the block at the specified DVA (in bp) via the 4211168404Spjd * cache. If the block is found in the cache, invoke the provided 4212168404Spjd * callback immediately and return. Note that the `zio' parameter 4213168404Spjd * in the callback will be NULL in this case, since no IO was 4214168404Spjd * required. If the block is not in the cache pass the read request 4215168404Spjd * on to the spa with a substitute callback function, so that the 4216168404Spjd * requested block will be added to the cache. 4217168404Spjd * 4218168404Spjd * If a read request arrives for a block that has a read in-progress, 4219168404Spjd * either wait for the in-progress read to complete (and return the 4220168404Spjd * results); or, if this is a read with a "done" func, add a record 4221168404Spjd * to the read to invoke the "done" func when the read completes, 4222168404Spjd * and return; or just return. 4223168404Spjd * 4224168404Spjd * arc_read_done() will invoke all the requested "done" functions 4225168404Spjd * for readers of this block. 4226168404Spjd */ 4227168404Spjdint 4228246666Smmarc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 4229275811Sdelphij void *private, zio_priority_t priority, int zio_flags, 4230275811Sdelphij arc_flags_t *arc_flags, const zbookmark_phys_t *zb) 4231168404Spjd{ 4232268075Sdelphij arc_buf_hdr_t *hdr = NULL; 4233247187Smm arc_buf_t *buf = NULL; 4234268075Sdelphij kmutex_t *hash_lock = NULL; 4235185029Spjd zio_t *rzio; 4236228103Smm uint64_t guid = spa_load_guid(spa); 4237168404Spjd 4238268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp) || 4239268075Sdelphij BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 4240268075Sdelphij 4241168404Spjdtop: 4242268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 4243268075Sdelphij /* 4244268075Sdelphij * Embedded BP's have no DVA and require no I/O to "read". 4245268075Sdelphij * Create an anonymous arc buf to back it. 4246268075Sdelphij */ 4247268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 4248268075Sdelphij } 4249168404Spjd 4250286570Smav if (hdr != NULL && HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_datacnt > 0) { 4251268075Sdelphij 4252275811Sdelphij *arc_flags |= ARC_FLAG_CACHED; 4253168404Spjd 4254168404Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 4255168404Spjd 4256287702Sdelphij if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) && 4257287702Sdelphij priority == ZIO_PRIORITY_SYNC_READ) { 4258287702Sdelphij /* 4259287702Sdelphij * This sync read must wait for an 4260287702Sdelphij * in-progress async read (e.g. a predictive 4261287702Sdelphij * prefetch). Async reads are queued 4262287702Sdelphij * separately at the vdev_queue layer, so 4263287702Sdelphij * this is a form of priority inversion. 4264287702Sdelphij * Ideally, we would "inherit" the demand 4265287702Sdelphij * i/o's priority by moving the i/o from 4266287702Sdelphij * the async queue to the synchronous queue, 4267287702Sdelphij * but there is currently no mechanism to do 4268287702Sdelphij * so. Track this so that we can evaluate 4269287702Sdelphij * the magnitude of this potential performance 4270287702Sdelphij * problem. 4271287702Sdelphij * 4272287702Sdelphij * Note that if the prefetch i/o is already 4273287702Sdelphij * active (has been issued to the device), 4274287702Sdelphij * the prefetch improved performance, because 4275287702Sdelphij * we issued it sooner than we would have 4276287702Sdelphij * without the prefetch. 4277287702Sdelphij */ 4278287702Sdelphij DTRACE_PROBE1(arc__sync__wait__for__async, 4279287702Sdelphij arc_buf_hdr_t *, hdr); 4280287702Sdelphij ARCSTAT_BUMP(arcstat_sync_wait_for_async); 4281287702Sdelphij } 4282287702Sdelphij if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { 4283287702Sdelphij hdr->b_flags &= ~ARC_FLAG_PREDICTIVE_PREFETCH; 4284287702Sdelphij } 4285287702Sdelphij 4286275811Sdelphij if (*arc_flags & ARC_FLAG_WAIT) { 4287286570Smav cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); 4288168404Spjd mutex_exit(hash_lock); 4289168404Spjd goto top; 4290168404Spjd } 4291275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 4292168404Spjd 4293168404Spjd if (done) { 4294287702Sdelphij arc_callback_t *acb = NULL; 4295168404Spjd 4296168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), 4297168404Spjd KM_SLEEP); 4298168404Spjd acb->acb_done = done; 4299168404Spjd acb->acb_private = private; 4300168404Spjd if (pio != NULL) 4301168404Spjd acb->acb_zio_dummy = zio_null(pio, 4302209962Smm spa, NULL, NULL, NULL, zio_flags); 4303168404Spjd 4304168404Spjd ASSERT(acb->acb_done != NULL); 4305286570Smav acb->acb_next = hdr->b_l1hdr.b_acb; 4306286570Smav hdr->b_l1hdr.b_acb = acb; 4307168404Spjd add_reference(hdr, hash_lock, private); 4308168404Spjd mutex_exit(hash_lock); 4309168404Spjd return (0); 4310168404Spjd } 4311168404Spjd mutex_exit(hash_lock); 4312168404Spjd return (0); 4313168404Spjd } 4314168404Spjd 4315286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_mru || 4316286570Smav hdr->b_l1hdr.b_state == arc_mfu); 4317168404Spjd 4318168404Spjd if (done) { 4319287702Sdelphij if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { 4320287702Sdelphij /* 4321287702Sdelphij * This is a demand read which does not have to 4322287702Sdelphij * wait for i/o because we did a predictive 4323287702Sdelphij * prefetch i/o for it, which has completed. 4324287702Sdelphij */ 4325287702Sdelphij DTRACE_PROBE1( 4326287702Sdelphij arc__demand__hit__predictive__prefetch, 4327287702Sdelphij arc_buf_hdr_t *, hdr); 4328287702Sdelphij ARCSTAT_BUMP( 4329287702Sdelphij arcstat_demand_hit_predictive_prefetch); 4330287702Sdelphij hdr->b_flags &= ~ARC_FLAG_PREDICTIVE_PREFETCH; 4331287702Sdelphij } 4332168404Spjd add_reference(hdr, hash_lock, private); 4333168404Spjd /* 4334168404Spjd * If this block is already in use, create a new 4335168404Spjd * copy of the data so that we will be guaranteed 4336168404Spjd * that arc_release() will always succeed. 4337168404Spjd */ 4338286570Smav buf = hdr->b_l1hdr.b_buf; 4339168404Spjd ASSERT(buf); 4340168404Spjd ASSERT(buf->b_data); 4341168404Spjd if (HDR_BUF_AVAILABLE(hdr)) { 4342168404Spjd ASSERT(buf->b_efunc == NULL); 4343275811Sdelphij hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 4344168404Spjd } else { 4345168404Spjd buf = arc_buf_clone(buf); 4346168404Spjd } 4347219089Spjd 4348275811Sdelphij } else if (*arc_flags & ARC_FLAG_PREFETCH && 4349286570Smav refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 4350275811Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 4351168404Spjd } 4352168404Spjd DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 4353168404Spjd arc_access(hdr, hash_lock); 4354275811Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 4355275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 4356275811Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 4357275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4358168404Spjd mutex_exit(hash_lock); 4359168404Spjd ARCSTAT_BUMP(arcstat_hits); 4360286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 4361286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 4362168404Spjd data, metadata, hits); 4363168404Spjd 4364168404Spjd if (done) 4365168404Spjd done(NULL, buf, private); 4366168404Spjd } else { 4367168404Spjd uint64_t size = BP_GET_LSIZE(bp); 4368268075Sdelphij arc_callback_t *acb; 4369185029Spjd vdev_t *vd = NULL; 4370247187Smm uint64_t addr = 0; 4371208373Smm boolean_t devw = B_FALSE; 4372258389Savg enum zio_compress b_compress = ZIO_COMPRESS_OFF; 4373286570Smav int32_t b_asize = 0; 4374168404Spjd 4375168404Spjd if (hdr == NULL) { 4376168404Spjd /* this block is not in the cache */ 4377268075Sdelphij arc_buf_hdr_t *exists = NULL; 4378168404Spjd arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 4379168404Spjd buf = arc_buf_alloc(spa, size, private, type); 4380168404Spjd hdr = buf->b_hdr; 4381268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 4382268075Sdelphij hdr->b_dva = *BP_IDENTITY(bp); 4383268075Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 4384268075Sdelphij exists = buf_hash_insert(hdr, &hash_lock); 4385268075Sdelphij } 4386268075Sdelphij if (exists != NULL) { 4387168404Spjd /* somebody beat us to the hash insert */ 4388168404Spjd mutex_exit(hash_lock); 4389219089Spjd buf_discard_identity(hdr); 4390168404Spjd (void) arc_buf_remove_ref(buf, private); 4391168404Spjd goto top; /* restart the IO request */ 4392168404Spjd } 4393275811Sdelphij 4394287702Sdelphij /* 4395287702Sdelphij * If there is a callback, we pass our reference to 4396287702Sdelphij * it; otherwise we remove our reference. 4397287702Sdelphij */ 4398287702Sdelphij if (done == NULL) { 4399168404Spjd (void) remove_reference(hdr, hash_lock, 4400168404Spjd private); 4401287702Sdelphij } 4402287702Sdelphij if (*arc_flags & ARC_FLAG_PREFETCH) 4403275811Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 4404275811Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 4405275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 4406275811Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 4407275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4408168404Spjd if (BP_GET_LEVEL(bp) > 0) 4409275811Sdelphij hdr->b_flags |= ARC_FLAG_INDIRECT; 4410168404Spjd } else { 4411286570Smav /* 4412286570Smav * This block is in the ghost cache. If it was L2-only 4413286570Smav * (and thus didn't have an L1 hdr), we realloc the 4414286570Smav * header to add an L1 hdr. 4415286570Smav */ 4416286570Smav if (!HDR_HAS_L1HDR(hdr)) { 4417286570Smav hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, 4418286570Smav hdr_full_cache); 4419286570Smav } 4420286570Smav 4421286570Smav ASSERT(GHOST_STATE(hdr->b_l1hdr.b_state)); 4422168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4423286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4424286763Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 4425168404Spjd 4426287702Sdelphij /* 4427287702Sdelphij * If there is a callback, we pass a reference to it. 4428287702Sdelphij */ 4429287702Sdelphij if (done != NULL) 4430287702Sdelphij add_reference(hdr, hash_lock, private); 4431275811Sdelphij if (*arc_flags & ARC_FLAG_PREFETCH) 4432275811Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 4433275811Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 4434275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 4435275811Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 4436275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4437185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 4438168404Spjd buf->b_hdr = hdr; 4439168404Spjd buf->b_data = NULL; 4440168404Spjd buf->b_efunc = NULL; 4441168404Spjd buf->b_private = NULL; 4442168404Spjd buf->b_next = NULL; 4443286570Smav hdr->b_l1hdr.b_buf = buf; 4444286570Smav ASSERT0(hdr->b_l1hdr.b_datacnt); 4445286570Smav hdr->b_l1hdr.b_datacnt = 1; 4446219089Spjd arc_get_data_buf(buf); 4447219089Spjd arc_access(hdr, hash_lock); 4448168404Spjd } 4449168404Spjd 4450287702Sdelphij if (*arc_flags & ARC_FLAG_PREDICTIVE_PREFETCH) 4451287702Sdelphij hdr->b_flags |= ARC_FLAG_PREDICTIVE_PREFETCH; 4452286570Smav ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); 4453219089Spjd 4454168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 4455168404Spjd acb->acb_done = done; 4456168404Spjd acb->acb_private = private; 4457168404Spjd 4458286570Smav ASSERT(hdr->b_l1hdr.b_acb == NULL); 4459286570Smav hdr->b_l1hdr.b_acb = acb; 4460275811Sdelphij hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; 4461168404Spjd 4462286570Smav if (HDR_HAS_L2HDR(hdr) && 4463286570Smav (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { 4464286570Smav devw = hdr->b_l2hdr.b_dev->l2ad_writing; 4465286570Smav addr = hdr->b_l2hdr.b_daddr; 4466287706Sdelphij b_compress = hdr->b_l2hdr.b_compress; 4467286570Smav b_asize = hdr->b_l2hdr.b_asize; 4468185029Spjd /* 4469185029Spjd * Lock out device removal. 4470185029Spjd */ 4471185029Spjd if (vdev_is_dead(vd) || 4472185029Spjd !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 4473185029Spjd vd = NULL; 4474185029Spjd } 4475185029Spjd 4476268075Sdelphij if (hash_lock != NULL) 4477268075Sdelphij mutex_exit(hash_lock); 4478168404Spjd 4479251629Sdelphij /* 4480251629Sdelphij * At this point, we have a level 1 cache miss. Try again in 4481251629Sdelphij * L2ARC if possible. 4482251629Sdelphij */ 4483168404Spjd ASSERT3U(hdr->b_size, ==, size); 4484219089Spjd DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 4485268123Sdelphij uint64_t, size, zbookmark_phys_t *, zb); 4486168404Spjd ARCSTAT_BUMP(arcstat_misses); 4487286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 4488286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 4489168404Spjd data, metadata, misses); 4490228392Spjd#ifdef _KERNEL 4491228392Spjd curthread->td_ru.ru_inblock++; 4492228392Spjd#endif 4493168404Spjd 4494287702Sdelphij if (priority == ZIO_PRIORITY_ASYNC_READ) 4495287702Sdelphij hdr->b_flags |= ARC_FLAG_PRIO_ASYNC_READ; 4496287702Sdelphij else 4497287702Sdelphij hdr->b_flags &= ~ARC_FLAG_PRIO_ASYNC_READ; 4498287702Sdelphij 4499208373Smm if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 4500185029Spjd /* 4501185029Spjd * Read from the L2ARC if the following are true: 4502185029Spjd * 1. The L2ARC vdev was previously cached. 4503185029Spjd * 2. This buffer still has L2ARC metadata. 4504185029Spjd * 3. This buffer isn't currently writing to the L2ARC. 4505185029Spjd * 4. The L2ARC entry wasn't evicted, which may 4506185029Spjd * also have invalidated the vdev. 4507208373Smm * 5. This isn't prefetch and l2arc_noprefetch is set. 4508185029Spjd */ 4509286570Smav if (HDR_HAS_L2HDR(hdr) && 4510208373Smm !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 4511208373Smm !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 4512185029Spjd l2arc_read_callback_t *cb; 4513185029Spjd 4514185029Spjd DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 4515185029Spjd ARCSTAT_BUMP(arcstat_l2_hits); 4516185029Spjd 4517185029Spjd cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 4518185029Spjd KM_SLEEP); 4519185029Spjd cb->l2rcb_buf = buf; 4520185029Spjd cb->l2rcb_spa = spa; 4521185029Spjd cb->l2rcb_bp = *bp; 4522185029Spjd cb->l2rcb_zb = *zb; 4523185029Spjd cb->l2rcb_flags = zio_flags; 4524258389Savg cb->l2rcb_compress = b_compress; 4525185029Spjd 4526247187Smm ASSERT(addr >= VDEV_LABEL_START_SIZE && 4527247187Smm addr + size < vd->vdev_psize - 4528247187Smm VDEV_LABEL_END_SIZE); 4529247187Smm 4530185029Spjd /* 4531185029Spjd * l2arc read. The SCL_L2ARC lock will be 4532185029Spjd * released by l2arc_read_done(). 4533251478Sdelphij * Issue a null zio if the underlying buffer 4534251478Sdelphij * was squashed to zero size by compression. 4535185029Spjd */ 4536258389Savg if (b_compress == ZIO_COMPRESS_EMPTY) { 4537251478Sdelphij rzio = zio_null(pio, spa, vd, 4538251478Sdelphij l2arc_read_done, cb, 4539251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 4540251478Sdelphij ZIO_FLAG_CANFAIL | 4541251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 4542251478Sdelphij ZIO_FLAG_DONT_RETRY); 4543251478Sdelphij } else { 4544251478Sdelphij rzio = zio_read_phys(pio, vd, addr, 4545258389Savg b_asize, buf->b_data, 4546258389Savg ZIO_CHECKSUM_OFF, 4547251478Sdelphij l2arc_read_done, cb, priority, 4548251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 4549251478Sdelphij ZIO_FLAG_CANFAIL | 4550251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 4551251478Sdelphij ZIO_FLAG_DONT_RETRY, B_FALSE); 4552251478Sdelphij } 4553185029Spjd DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 4554185029Spjd zio_t *, rzio); 4555258389Savg ARCSTAT_INCR(arcstat_l2_read_bytes, b_asize); 4556185029Spjd 4557275811Sdelphij if (*arc_flags & ARC_FLAG_NOWAIT) { 4558185029Spjd zio_nowait(rzio); 4559185029Spjd return (0); 4560185029Spjd } 4561185029Spjd 4562275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_WAIT); 4563185029Spjd if (zio_wait(rzio) == 0) 4564185029Spjd return (0); 4565185029Spjd 4566185029Spjd /* l2arc read error; goto zio_read() */ 4567185029Spjd } else { 4568185029Spjd DTRACE_PROBE1(l2arc__miss, 4569185029Spjd arc_buf_hdr_t *, hdr); 4570185029Spjd ARCSTAT_BUMP(arcstat_l2_misses); 4571185029Spjd if (HDR_L2_WRITING(hdr)) 4572185029Spjd ARCSTAT_BUMP(arcstat_l2_rw_clash); 4573185029Spjd spa_config_exit(spa, SCL_L2ARC, vd); 4574185029Spjd } 4575208373Smm } else { 4576208373Smm if (vd != NULL) 4577208373Smm spa_config_exit(spa, SCL_L2ARC, vd); 4578208373Smm if (l2arc_ndev != 0) { 4579208373Smm DTRACE_PROBE1(l2arc__miss, 4580208373Smm arc_buf_hdr_t *, hdr); 4581208373Smm ARCSTAT_BUMP(arcstat_l2_misses); 4582208373Smm } 4583185029Spjd } 4584185029Spjd 4585168404Spjd rzio = zio_read(pio, spa, bp, buf->b_data, size, 4586185029Spjd arc_read_done, buf, priority, zio_flags, zb); 4587168404Spjd 4588275811Sdelphij if (*arc_flags & ARC_FLAG_WAIT) 4589168404Spjd return (zio_wait(rzio)); 4590168404Spjd 4591275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 4592168404Spjd zio_nowait(rzio); 4593168404Spjd } 4594168404Spjd return (0); 4595168404Spjd} 4596168404Spjd 4597168404Spjdvoid 4598168404Spjdarc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 4599168404Spjd{ 4600168404Spjd ASSERT(buf->b_hdr != NULL); 4601286570Smav ASSERT(buf->b_hdr->b_l1hdr.b_state != arc_anon); 4602286570Smav ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt) || 4603286570Smav func == NULL); 4604219089Spjd ASSERT(buf->b_efunc == NULL); 4605219089Spjd ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 4606219089Spjd 4607168404Spjd buf->b_efunc = func; 4608168404Spjd buf->b_private = private; 4609168404Spjd} 4610168404Spjd 4611168404Spjd/* 4612251520Sdelphij * Notify the arc that a block was freed, and thus will never be used again. 4613251520Sdelphij */ 4614251520Sdelphijvoid 4615251520Sdelphijarc_freed(spa_t *spa, const blkptr_t *bp) 4616251520Sdelphij{ 4617251520Sdelphij arc_buf_hdr_t *hdr; 4618251520Sdelphij kmutex_t *hash_lock; 4619251520Sdelphij uint64_t guid = spa_load_guid(spa); 4620251520Sdelphij 4621268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp)); 4622268075Sdelphij 4623268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 4624251520Sdelphij if (hdr == NULL) 4625251520Sdelphij return; 4626251520Sdelphij if (HDR_BUF_AVAILABLE(hdr)) { 4627286570Smav arc_buf_t *buf = hdr->b_l1hdr.b_buf; 4628251520Sdelphij add_reference(hdr, hash_lock, FTAG); 4629275811Sdelphij hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 4630251520Sdelphij mutex_exit(hash_lock); 4631251520Sdelphij 4632251520Sdelphij arc_release(buf, FTAG); 4633251520Sdelphij (void) arc_buf_remove_ref(buf, FTAG); 4634251520Sdelphij } else { 4635251520Sdelphij mutex_exit(hash_lock); 4636251520Sdelphij } 4637251520Sdelphij 4638251520Sdelphij} 4639251520Sdelphij 4640251520Sdelphij/* 4641268858Sdelphij * Clear the user eviction callback set by arc_set_callback(), first calling 4642268858Sdelphij * it if it exists. Because the presence of a callback keeps an arc_buf cached 4643268858Sdelphij * clearing the callback may result in the arc_buf being destroyed. However, 4644268858Sdelphij * it will not result in the *last* arc_buf being destroyed, hence the data 4645268858Sdelphij * will remain cached in the ARC. We make a copy of the arc buffer here so 4646268858Sdelphij * that we can process the callback without holding any locks. 4647268858Sdelphij * 4648268858Sdelphij * It's possible that the callback is already in the process of being cleared 4649268858Sdelphij * by another thread. In this case we can not clear the callback. 4650268858Sdelphij * 4651268858Sdelphij * Returns B_TRUE if the callback was successfully called and cleared. 4652168404Spjd */ 4653268858Sdelphijboolean_t 4654268858Sdelphijarc_clear_callback(arc_buf_t *buf) 4655168404Spjd{ 4656168404Spjd arc_buf_hdr_t *hdr; 4657168404Spjd kmutex_t *hash_lock; 4658268858Sdelphij arc_evict_func_t *efunc = buf->b_efunc; 4659268858Sdelphij void *private = buf->b_private; 4660206796Spjd 4661219089Spjd mutex_enter(&buf->b_evict_lock); 4662168404Spjd hdr = buf->b_hdr; 4663168404Spjd if (hdr == NULL) { 4664168404Spjd /* 4665168404Spjd * We are in arc_do_user_evicts(). 4666168404Spjd */ 4667168404Spjd ASSERT(buf->b_data == NULL); 4668219089Spjd mutex_exit(&buf->b_evict_lock); 4669268858Sdelphij return (B_FALSE); 4670185029Spjd } else if (buf->b_data == NULL) { 4671185029Spjd /* 4672185029Spjd * We are on the eviction list; process this buffer now 4673185029Spjd * but let arc_do_user_evicts() do the reaping. 4674185029Spjd */ 4675185029Spjd buf->b_efunc = NULL; 4676219089Spjd mutex_exit(&buf->b_evict_lock); 4677268858Sdelphij VERIFY0(efunc(private)); 4678268858Sdelphij return (B_TRUE); 4679168404Spjd } 4680168404Spjd hash_lock = HDR_LOCK(hdr); 4681168404Spjd mutex_enter(hash_lock); 4682219089Spjd hdr = buf->b_hdr; 4683219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4684168404Spjd 4685286570Smav ASSERT3U(refcount_count(&hdr->b_l1hdr.b_refcnt), <, 4686286570Smav hdr->b_l1hdr.b_datacnt); 4687286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_mru || 4688286570Smav hdr->b_l1hdr.b_state == arc_mfu); 4689168404Spjd 4690268858Sdelphij buf->b_efunc = NULL; 4691268858Sdelphij buf->b_private = NULL; 4692168404Spjd 4693286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 4694268858Sdelphij mutex_exit(&buf->b_evict_lock); 4695286763Smav arc_buf_destroy(buf, TRUE); 4696268858Sdelphij } else { 4697286570Smav ASSERT(buf == hdr->b_l1hdr.b_buf); 4698275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 4699268858Sdelphij mutex_exit(&buf->b_evict_lock); 4700268858Sdelphij } 4701168404Spjd 4702168404Spjd mutex_exit(hash_lock); 4703268858Sdelphij VERIFY0(efunc(private)); 4704268858Sdelphij return (B_TRUE); 4705168404Spjd} 4706168404Spjd 4707168404Spjd/* 4708251629Sdelphij * Release this buffer from the cache, making it an anonymous buffer. This 4709251629Sdelphij * must be done after a read and prior to modifying the buffer contents. 4710168404Spjd * If the buffer has more than one reference, we must make 4711185029Spjd * a new hdr for the buffer. 4712168404Spjd */ 4713168404Spjdvoid 4714168404Spjdarc_release(arc_buf_t *buf, void *tag) 4715168404Spjd{ 4716286570Smav arc_buf_hdr_t *hdr = buf->b_hdr; 4717168404Spjd 4718219089Spjd /* 4719219089Spjd * It would be nice to assert that if it's DMU metadata (level > 4720219089Spjd * 0 || it's the dnode file), then it must be syncing context. 4721219089Spjd * But we don't know that information at this level. 4722219089Spjd */ 4723219089Spjd 4724219089Spjd mutex_enter(&buf->b_evict_lock); 4725286776Smav 4726286776Smav ASSERT(HDR_HAS_L1HDR(hdr)); 4727286776Smav 4728286570Smav /* 4729286570Smav * We don't grab the hash lock prior to this check, because if 4730286570Smav * the buffer's header is in the arc_anon state, it won't be 4731286570Smav * linked into the hash table. 4732286570Smav */ 4733286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 4734286570Smav mutex_exit(&buf->b_evict_lock); 4735286570Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4736286570Smav ASSERT(!HDR_IN_HASH_TABLE(hdr)); 4737286570Smav ASSERT(!HDR_HAS_L2HDR(hdr)); 4738286570Smav ASSERT(BUF_EMPTY(hdr)); 4739286570Smav ASSERT3U(hdr->b_l1hdr.b_datacnt, ==, 1); 4740286570Smav ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); 4741286570Smav ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 4742185029Spjd 4743286570Smav ASSERT3P(buf->b_efunc, ==, NULL); 4744286570Smav ASSERT3P(buf->b_private, ==, NULL); 4745168404Spjd 4746286570Smav hdr->b_l1hdr.b_arc_access = 0; 4747286570Smav arc_buf_thaw(buf); 4748286570Smav 4749286570Smav return; 4750168404Spjd } 4751168404Spjd 4752286570Smav kmutex_t *hash_lock = HDR_LOCK(hdr); 4753286570Smav mutex_enter(hash_lock); 4754286570Smav 4755286570Smav /* 4756286570Smav * This assignment is only valid as long as the hash_lock is 4757286570Smav * held, we must be careful not to reference state or the 4758286570Smav * b_state field after dropping the lock. 4759286570Smav */ 4760286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 4761286570Smav ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4762286570Smav ASSERT3P(state, !=, arc_anon); 4763286570Smav 4764286570Smav /* this buffer is not on any list */ 4765286570Smav ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) > 0); 4766286570Smav 4767286570Smav if (HDR_HAS_L2HDR(hdr)) { 4768286570Smav mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); 4769286570Smav 4770286570Smav /* 4771286598Smav * We have to recheck this conditional again now that 4772286598Smav * we're holding the l2ad_mtx to prevent a race with 4773286598Smav * another thread which might be concurrently calling 4774286598Smav * l2arc_evict(). In that case, l2arc_evict() might have 4775286598Smav * destroyed the header's L2 portion as we were waiting 4776286598Smav * to acquire the l2ad_mtx. 4777286570Smav */ 4778286598Smav if (HDR_HAS_L2HDR(hdr)) { 4779286647Smav if (hdr->b_l2hdr.b_daddr != L2ARC_ADDR_UNSET) 4780286647Smav trim_map_free(hdr->b_l2hdr.b_dev->l2ad_vdev, 4781286647Smav hdr->b_l2hdr.b_daddr, 4782286647Smav hdr->b_l2hdr.b_asize, 0); 4783286598Smav arc_hdr_l2hdr_destroy(hdr); 4784286598Smav } 4785286570Smav 4786286570Smav mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); 4787185029Spjd } 4788185029Spjd 4789168404Spjd /* 4790168404Spjd * Do we have more than one buf? 4791168404Spjd */ 4792286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 4793168404Spjd arc_buf_hdr_t *nhdr; 4794168404Spjd arc_buf_t **bufp; 4795168404Spjd uint64_t blksz = hdr->b_size; 4796209962Smm uint64_t spa = hdr->b_spa; 4797286570Smav arc_buf_contents_t type = arc_buf_type(hdr); 4798185029Spjd uint32_t flags = hdr->b_flags; 4799168404Spjd 4800286570Smav ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); 4801168404Spjd /* 4802219089Spjd * Pull the data off of this hdr and attach it to 4803219089Spjd * a new anonymous hdr. 4804168404Spjd */ 4805168404Spjd (void) remove_reference(hdr, hash_lock, tag); 4806286570Smav bufp = &hdr->b_l1hdr.b_buf; 4807168404Spjd while (*bufp != buf) 4808168404Spjd bufp = &(*bufp)->b_next; 4809219089Spjd *bufp = buf->b_next; 4810168404Spjd buf->b_next = NULL; 4811168404Spjd 4812286570Smav ASSERT3P(state, !=, arc_l2c_only); 4813286766Smav 4814286766Smav (void) refcount_remove_many( 4815286766Smav &state->arcs_size, hdr->b_size, buf); 4816286766Smav 4817286570Smav if (refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { 4818286570Smav ASSERT3P(state, !=, arc_l2c_only); 4819286570Smav uint64_t *size = &state->arcs_lsize[type]; 4820185029Spjd ASSERT3U(*size, >=, hdr->b_size); 4821185029Spjd atomic_add_64(size, -hdr->b_size); 4822168404Spjd } 4823242845Sdelphij 4824242845Sdelphij /* 4825242845Sdelphij * We're releasing a duplicate user data buffer, update 4826242845Sdelphij * our statistics accordingly. 4827242845Sdelphij */ 4828286570Smav if (HDR_ISTYPE_DATA(hdr)) { 4829242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 4830242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, 4831242845Sdelphij -hdr->b_size); 4832242845Sdelphij } 4833286570Smav hdr->b_l1hdr.b_datacnt -= 1; 4834168404Spjd arc_cksum_verify(buf); 4835240133Smm#ifdef illumos 4836240133Smm arc_buf_unwatch(buf); 4837277300Ssmh#endif 4838168404Spjd 4839168404Spjd mutex_exit(hash_lock); 4840168404Spjd 4841286570Smav nhdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 4842168404Spjd nhdr->b_size = blksz; 4843168404Spjd nhdr->b_spa = spa; 4844286570Smav 4845275811Sdelphij nhdr->b_flags = flags & ARC_FLAG_L2_WRITING; 4846286570Smav nhdr->b_flags |= arc_bufc_to_flags(type); 4847286570Smav nhdr->b_flags |= ARC_FLAG_HAS_L1HDR; 4848286570Smav 4849286570Smav nhdr->b_l1hdr.b_buf = buf; 4850286570Smav nhdr->b_l1hdr.b_datacnt = 1; 4851286570Smav nhdr->b_l1hdr.b_state = arc_anon; 4852286570Smav nhdr->b_l1hdr.b_arc_access = 0; 4853286763Smav nhdr->b_l1hdr.b_tmp_cdata = NULL; 4854168404Spjd nhdr->b_freeze_cksum = NULL; 4855286570Smav 4856286570Smav (void) refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); 4857168404Spjd buf->b_hdr = nhdr; 4858219089Spjd mutex_exit(&buf->b_evict_lock); 4859286766Smav (void) refcount_add_many(&arc_anon->arcs_size, blksz, buf); 4860168404Spjd } else { 4861219089Spjd mutex_exit(&buf->b_evict_lock); 4862286570Smav ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); 4863286763Smav /* protected by hash lock, or hdr is on arc_anon */ 4864286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 4865168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4866286570Smav arc_change_state(arc_anon, hdr, hash_lock); 4867286570Smav hdr->b_l1hdr.b_arc_access = 0; 4868286570Smav mutex_exit(hash_lock); 4869185029Spjd 4870219089Spjd buf_discard_identity(hdr); 4871168404Spjd arc_buf_thaw(buf); 4872168404Spjd } 4873168404Spjd buf->b_efunc = NULL; 4874168404Spjd buf->b_private = NULL; 4875168404Spjd} 4876168404Spjd 4877168404Spjdint 4878168404Spjdarc_released(arc_buf_t *buf) 4879168404Spjd{ 4880185029Spjd int released; 4881185029Spjd 4882219089Spjd mutex_enter(&buf->b_evict_lock); 4883286570Smav released = (buf->b_data != NULL && 4884286570Smav buf->b_hdr->b_l1hdr.b_state == arc_anon); 4885219089Spjd mutex_exit(&buf->b_evict_lock); 4886185029Spjd return (released); 4887168404Spjd} 4888168404Spjd 4889168404Spjd#ifdef ZFS_DEBUG 4890168404Spjdint 4891168404Spjdarc_referenced(arc_buf_t *buf) 4892168404Spjd{ 4893185029Spjd int referenced; 4894185029Spjd 4895219089Spjd mutex_enter(&buf->b_evict_lock); 4896286570Smav referenced = (refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); 4897219089Spjd mutex_exit(&buf->b_evict_lock); 4898185029Spjd return (referenced); 4899168404Spjd} 4900168404Spjd#endif 4901168404Spjd 4902168404Spjdstatic void 4903168404Spjdarc_write_ready(zio_t *zio) 4904168404Spjd{ 4905168404Spjd arc_write_callback_t *callback = zio->io_private; 4906168404Spjd arc_buf_t *buf = callback->awcb_buf; 4907185029Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 4908168404Spjd 4909286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 4910286570Smav ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); 4911286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 4912185029Spjd callback->awcb_ready(zio, buf, callback->awcb_private); 4913185029Spjd 4914185029Spjd /* 4915185029Spjd * If the IO is already in progress, then this is a re-write 4916185029Spjd * attempt, so we need to thaw and re-compute the cksum. 4917185029Spjd * It is the responsibility of the callback to handle the 4918185029Spjd * accounting for any re-write attempt. 4919185029Spjd */ 4920185029Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 4921286570Smav mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 4922185029Spjd if (hdr->b_freeze_cksum != NULL) { 4923185029Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 4924185029Spjd hdr->b_freeze_cksum = NULL; 4925185029Spjd } 4926286570Smav mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 4927168404Spjd } 4928185029Spjd arc_cksum_compute(buf, B_FALSE); 4929275811Sdelphij hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; 4930168404Spjd} 4931168404Spjd 4932258632Savg/* 4933258632Savg * The SPA calls this callback for each physical write that happens on behalf 4934258632Savg * of a logical write. See the comment in dbuf_write_physdone() for details. 4935258632Savg */ 4936168404Spjdstatic void 4937258632Savgarc_write_physdone(zio_t *zio) 4938258632Savg{ 4939258632Savg arc_write_callback_t *cb = zio->io_private; 4940258632Savg if (cb->awcb_physdone != NULL) 4941258632Savg cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 4942258632Savg} 4943258632Savg 4944258632Savgstatic void 4945168404Spjdarc_write_done(zio_t *zio) 4946168404Spjd{ 4947168404Spjd arc_write_callback_t *callback = zio->io_private; 4948168404Spjd arc_buf_t *buf = callback->awcb_buf; 4949168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 4950168404Spjd 4951286570Smav ASSERT(hdr->b_l1hdr.b_acb == NULL); 4952168404Spjd 4953219089Spjd if (zio->io_error == 0) { 4954268075Sdelphij if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 4955260150Sdelphij buf_discard_identity(hdr); 4956260150Sdelphij } else { 4957260150Sdelphij hdr->b_dva = *BP_IDENTITY(zio->io_bp); 4958260150Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 4959260150Sdelphij } 4960219089Spjd } else { 4961219089Spjd ASSERT(BUF_EMPTY(hdr)); 4962219089Spjd } 4963219089Spjd 4964168404Spjd /* 4965268075Sdelphij * If the block to be written was all-zero or compressed enough to be 4966268075Sdelphij * embedded in the BP, no write was performed so there will be no 4967268075Sdelphij * dva/birth/checksum. The buffer must therefore remain anonymous 4968268075Sdelphij * (and uncached). 4969168404Spjd */ 4970168404Spjd if (!BUF_EMPTY(hdr)) { 4971168404Spjd arc_buf_hdr_t *exists; 4972168404Spjd kmutex_t *hash_lock; 4973168404Spjd 4974219089Spjd ASSERT(zio->io_error == 0); 4975219089Spjd 4976168404Spjd arc_cksum_verify(buf); 4977168404Spjd 4978168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 4979286570Smav if (exists != NULL) { 4980168404Spjd /* 4981168404Spjd * This can only happen if we overwrite for 4982168404Spjd * sync-to-convergence, because we remove 4983168404Spjd * buffers from the hash table when we arc_free(). 4984168404Spjd */ 4985219089Spjd if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 4986219089Spjd if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 4987219089Spjd panic("bad overwrite, hdr=%p exists=%p", 4988219089Spjd (void *)hdr, (void *)exists); 4989286570Smav ASSERT(refcount_is_zero( 4990286570Smav &exists->b_l1hdr.b_refcnt)); 4991219089Spjd arc_change_state(arc_anon, exists, hash_lock); 4992219089Spjd mutex_exit(hash_lock); 4993219089Spjd arc_hdr_destroy(exists); 4994219089Spjd exists = buf_hash_insert(hdr, &hash_lock); 4995219089Spjd ASSERT3P(exists, ==, NULL); 4996243524Smm } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 4997243524Smm /* nopwrite */ 4998243524Smm ASSERT(zio->io_prop.zp_nopwrite); 4999243524Smm if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 5000243524Smm panic("bad nopwrite, hdr=%p exists=%p", 5001243524Smm (void *)hdr, (void *)exists); 5002219089Spjd } else { 5003219089Spjd /* Dedup */ 5004286570Smav ASSERT(hdr->b_l1hdr.b_datacnt == 1); 5005286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_anon); 5006219089Spjd ASSERT(BP_GET_DEDUP(zio->io_bp)); 5007219089Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 5008219089Spjd } 5009168404Spjd } 5010275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 5011185029Spjd /* if it's not anon, we are doing a scrub */ 5012286570Smav if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) 5013185029Spjd arc_access(hdr, hash_lock); 5014168404Spjd mutex_exit(hash_lock); 5015168404Spjd } else { 5016275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 5017168404Spjd } 5018168404Spjd 5019286570Smav ASSERT(!refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 5020219089Spjd callback->awcb_done(zio, buf, callback->awcb_private); 5021168404Spjd 5022168404Spjd kmem_free(callback, sizeof (arc_write_callback_t)); 5023168404Spjd} 5024168404Spjd 5025168404Spjdzio_t * 5026219089Spjdarc_write(zio_t *pio, spa_t *spa, uint64_t txg, 5027251478Sdelphij blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress, 5028258632Savg const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone, 5029258632Savg arc_done_func_t *done, void *private, zio_priority_t priority, 5030268123Sdelphij int zio_flags, const zbookmark_phys_t *zb) 5031168404Spjd{ 5032168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 5033168404Spjd arc_write_callback_t *callback; 5034185029Spjd zio_t *zio; 5035168404Spjd 5036185029Spjd ASSERT(ready != NULL); 5037219089Spjd ASSERT(done != NULL); 5038168404Spjd ASSERT(!HDR_IO_ERROR(hdr)); 5039286570Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 5040286570Smav ASSERT(hdr->b_l1hdr.b_acb == NULL); 5041286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 5042185029Spjd if (l2arc) 5043275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 5044251478Sdelphij if (l2arc_compress) 5045275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 5046168404Spjd callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 5047168404Spjd callback->awcb_ready = ready; 5048258632Savg callback->awcb_physdone = physdone; 5049168404Spjd callback->awcb_done = done; 5050168404Spjd callback->awcb_private = private; 5051168404Spjd callback->awcb_buf = buf; 5052168404Spjd 5053219089Spjd zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 5054258632Savg arc_write_ready, arc_write_physdone, arc_write_done, callback, 5055258632Savg priority, zio_flags, zb); 5056185029Spjd 5057168404Spjd return (zio); 5058168404Spjd} 5059168404Spjd 5060185029Spjdstatic int 5061258632Savgarc_memory_throttle(uint64_t reserve, uint64_t txg) 5062185029Spjd{ 5063185029Spjd#ifdef _KERNEL 5064272483Ssmh uint64_t available_memory = ptob(freemem); 5065185029Spjd static uint64_t page_load = 0; 5066185029Spjd static uint64_t last_txg = 0; 5067185029Spjd 5068272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 5069185029Spjd available_memory = 5070272483Ssmh MIN(available_memory, ptob(vmem_size(heap_arena, VMEM_FREE))); 5071185029Spjd#endif 5072258632Savg 5073272483Ssmh if (freemem > (uint64_t)physmem * arc_lotsfree_percent / 100) 5074185029Spjd return (0); 5075185029Spjd 5076185029Spjd if (txg > last_txg) { 5077185029Spjd last_txg = txg; 5078185029Spjd page_load = 0; 5079185029Spjd } 5080185029Spjd /* 5081185029Spjd * If we are in pageout, we know that memory is already tight, 5082185029Spjd * the arc is already going to be evicting, so we just want to 5083185029Spjd * continue to let page writes occur as quickly as possible. 5084185029Spjd */ 5085185029Spjd if (curproc == pageproc) { 5086272483Ssmh if (page_load > MAX(ptob(minfree), available_memory) / 4) 5087249195Smm return (SET_ERROR(ERESTART)); 5088185029Spjd /* Note: reserve is inflated, so we deflate */ 5089185029Spjd page_load += reserve / 8; 5090185029Spjd return (0); 5091185029Spjd } else if (page_load > 0 && arc_reclaim_needed()) { 5092185029Spjd /* memory is low, delay before restarting */ 5093185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 5094249195Smm return (SET_ERROR(EAGAIN)); 5095185029Spjd } 5096185029Spjd page_load = 0; 5097185029Spjd#endif 5098185029Spjd return (0); 5099185029Spjd} 5100185029Spjd 5101168404Spjdvoid 5102185029Spjdarc_tempreserve_clear(uint64_t reserve) 5103168404Spjd{ 5104185029Spjd atomic_add_64(&arc_tempreserve, -reserve); 5105168404Spjd ASSERT((int64_t)arc_tempreserve >= 0); 5106168404Spjd} 5107168404Spjd 5108168404Spjdint 5109185029Spjdarc_tempreserve_space(uint64_t reserve, uint64_t txg) 5110168404Spjd{ 5111185029Spjd int error; 5112209962Smm uint64_t anon_size; 5113185029Spjd 5114272483Ssmh if (reserve > arc_c/4 && !arc_no_grow) { 5115185029Spjd arc_c = MIN(arc_c_max, reserve * 4); 5116272483Ssmh DTRACE_PROBE1(arc__set_reserve, uint64_t, arc_c); 5117272483Ssmh } 5118185029Spjd if (reserve > arc_c) 5119249195Smm return (SET_ERROR(ENOMEM)); 5120168404Spjd 5121168404Spjd /* 5122209962Smm * Don't count loaned bufs as in flight dirty data to prevent long 5123209962Smm * network delays from blocking transactions that are ready to be 5124209962Smm * assigned to a txg. 5125209962Smm */ 5126286766Smav anon_size = MAX((int64_t)(refcount_count(&arc_anon->arcs_size) - 5127286766Smav arc_loaned_bytes), 0); 5128209962Smm 5129209962Smm /* 5130185029Spjd * Writes will, almost always, require additional memory allocations 5131251631Sdelphij * in order to compress/encrypt/etc the data. We therefore need to 5132185029Spjd * make sure that there is sufficient available memory for this. 5133185029Spjd */ 5134258632Savg error = arc_memory_throttle(reserve, txg); 5135258632Savg if (error != 0) 5136185029Spjd return (error); 5137185029Spjd 5138185029Spjd /* 5139168404Spjd * Throttle writes when the amount of dirty data in the cache 5140168404Spjd * gets too large. We try to keep the cache less than half full 5141168404Spjd * of dirty blocks so that our sync times don't grow too large. 5142168404Spjd * Note: if two requests come in concurrently, we might let them 5143168404Spjd * both succeed, when one of them should fail. Not a huge deal. 5144168404Spjd */ 5145209962Smm 5146209962Smm if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 5147209962Smm anon_size > arc_c / 4) { 5148185029Spjd dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 5149185029Spjd "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 5150185029Spjd arc_tempreserve>>10, 5151185029Spjd arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 5152185029Spjd arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 5153185029Spjd reserve>>10, arc_c>>10); 5154249195Smm return (SET_ERROR(ERESTART)); 5155168404Spjd } 5156185029Spjd atomic_add_64(&arc_tempreserve, reserve); 5157168404Spjd return (0); 5158168404Spjd} 5159168404Spjd 5160286626Smavstatic void 5161286626Smavarc_kstat_update_state(arc_state_t *state, kstat_named_t *size, 5162286626Smav kstat_named_t *evict_data, kstat_named_t *evict_metadata) 5163286626Smav{ 5164286766Smav size->value.ui64 = refcount_count(&state->arcs_size); 5165286626Smav evict_data->value.ui64 = state->arcs_lsize[ARC_BUFC_DATA]; 5166286626Smav evict_metadata->value.ui64 = state->arcs_lsize[ARC_BUFC_METADATA]; 5167286626Smav} 5168286626Smav 5169286626Smavstatic int 5170286626Smavarc_kstat_update(kstat_t *ksp, int rw) 5171286626Smav{ 5172286626Smav arc_stats_t *as = ksp->ks_data; 5173286626Smav 5174286626Smav if (rw == KSTAT_WRITE) { 5175286626Smav return (EACCES); 5176286626Smav } else { 5177286626Smav arc_kstat_update_state(arc_anon, 5178286626Smav &as->arcstat_anon_size, 5179286626Smav &as->arcstat_anon_evictable_data, 5180286626Smav &as->arcstat_anon_evictable_metadata); 5181286626Smav arc_kstat_update_state(arc_mru, 5182286626Smav &as->arcstat_mru_size, 5183286626Smav &as->arcstat_mru_evictable_data, 5184286626Smav &as->arcstat_mru_evictable_metadata); 5185286626Smav arc_kstat_update_state(arc_mru_ghost, 5186286626Smav &as->arcstat_mru_ghost_size, 5187286626Smav &as->arcstat_mru_ghost_evictable_data, 5188286626Smav &as->arcstat_mru_ghost_evictable_metadata); 5189286626Smav arc_kstat_update_state(arc_mfu, 5190286626Smav &as->arcstat_mfu_size, 5191286626Smav &as->arcstat_mfu_evictable_data, 5192286626Smav &as->arcstat_mfu_evictable_metadata); 5193286626Smav arc_kstat_update_state(arc_mfu_ghost, 5194286626Smav &as->arcstat_mfu_ghost_size, 5195286626Smav &as->arcstat_mfu_ghost_evictable_data, 5196286626Smav &as->arcstat_mfu_ghost_evictable_metadata); 5197286626Smav } 5198286626Smav 5199286626Smav return (0); 5200286626Smav} 5201286626Smav 5202286763Smav/* 5203286763Smav * This function *must* return indices evenly distributed between all 5204286763Smav * sublists of the multilist. This is needed due to how the ARC eviction 5205286763Smav * code is laid out; arc_evict_state() assumes ARC buffers are evenly 5206286763Smav * distributed between all sublists and uses this assumption when 5207286763Smav * deciding which sublist to evict from and how much to evict from it. 5208286763Smav */ 5209286763Smavunsigned int 5210286763Smavarc_state_multilist_index_func(multilist_t *ml, void *obj) 5211286763Smav{ 5212286763Smav arc_buf_hdr_t *hdr = obj; 5213286763Smav 5214286763Smav /* 5215286763Smav * We rely on b_dva to generate evenly distributed index 5216286763Smav * numbers using buf_hash below. So, as an added precaution, 5217286763Smav * let's make sure we never add empty buffers to the arc lists. 5218286763Smav */ 5219286763Smav ASSERT(!BUF_EMPTY(hdr)); 5220286763Smav 5221286763Smav /* 5222286763Smav * The assumption here, is the hash value for a given 5223286763Smav * arc_buf_hdr_t will remain constant throughout it's lifetime 5224286763Smav * (i.e. it's b_spa, b_dva, and b_birth fields don't change). 5225286763Smav * Thus, we don't need to store the header's sublist index 5226286763Smav * on insertion, as this index can be recalculated on removal. 5227286763Smav * 5228286763Smav * Also, the low order bits of the hash value are thought to be 5229286763Smav * distributed evenly. Otherwise, in the case that the multilist 5230286763Smav * has a power of two number of sublists, each sublists' usage 5231286763Smav * would not be evenly distributed. 5232286763Smav */ 5233286763Smav return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % 5234286763Smav multilist_get_num_sublists(ml)); 5235286763Smav} 5236286763Smav 5237168404Spjd#ifdef _KERNEL 5238168566Spjdstatic eventhandler_tag arc_event_lowmem = NULL; 5239168404Spjd 5240168404Spjdstatic void 5241168566Spjdarc_lowmem(void *arg __unused, int howto __unused) 5242168404Spjd{ 5243168404Spjd 5244286763Smav mutex_enter(&arc_reclaim_lock); 5245286625Smav /* XXX: Memory deficit should be passed as argument. */ 5246286625Smav needfree = btoc(arc_c >> arc_shrink_shift); 5247272483Ssmh DTRACE_PROBE(arc__needfree); 5248286763Smav cv_signal(&arc_reclaim_thread_cv); 5249241773Savg 5250241773Savg /* 5251241773Savg * It is unsafe to block here in arbitrary threads, because we can come 5252241773Savg * here from ARC itself and may hold ARC locks and thus risk a deadlock 5253241773Savg * with ARC reclaim thread. 5254241773Savg */ 5255286623Smav if (curproc == pageproc) 5256286763Smav (void) cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); 5257286763Smav mutex_exit(&arc_reclaim_lock); 5258168404Spjd} 5259168404Spjd#endif 5260168404Spjd 5261168404Spjdvoid 5262168404Spjdarc_init(void) 5263168404Spjd{ 5264219089Spjd int i, prefetch_tunable_set = 0; 5265205231Skmacy 5266286763Smav mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL); 5267286763Smav cv_init(&arc_reclaim_thread_cv, NULL, CV_DEFAULT, NULL); 5268286763Smav cv_init(&arc_reclaim_waiters_cv, NULL, CV_DEFAULT, NULL); 5269168404Spjd 5270286763Smav mutex_init(&arc_user_evicts_lock, NULL, MUTEX_DEFAULT, NULL); 5271286763Smav cv_init(&arc_user_evicts_cv, NULL, CV_DEFAULT, NULL); 5272286763Smav 5273168404Spjd /* Convert seconds to clock ticks */ 5274168404Spjd arc_min_prefetch_lifespan = 1 * hz; 5275168404Spjd 5276168404Spjd /* Start out with 1/8 of all memory */ 5277168566Spjd arc_c = kmem_size() / 8; 5278219089Spjd 5279277300Ssmh#ifdef illumos 5280192360Skmacy#ifdef _KERNEL 5281192360Skmacy /* 5282192360Skmacy * On architectures where the physical memory can be larger 5283192360Skmacy * than the addressable space (intel in 32-bit mode), we may 5284192360Skmacy * need to limit the cache to 1/8 of VM size. 5285192360Skmacy */ 5286192360Skmacy arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 5287192360Skmacy#endif 5288277300Ssmh#endif /* illumos */ 5289168566Spjd /* set min cache to 1/32 of all memory, or 16MB, whichever is more */ 5290280822Smav arc_c_min = MAX(arc_c / 4, 16 << 20); 5291168566Spjd /* set max to 1/2 of all memory, or all but 1GB, whichever is more */ 5292280822Smav if (arc_c * 8 >= 1 << 30) 5293280822Smav arc_c_max = (arc_c * 8) - (1 << 30); 5294168404Spjd else 5295168404Spjd arc_c_max = arc_c_min; 5296175633Spjd arc_c_max = MAX(arc_c * 5, arc_c_max); 5297219089Spjd 5298168481Spjd#ifdef _KERNEL 5299168404Spjd /* 5300168404Spjd * Allow the tunables to override our calculations if they are 5301168566Spjd * reasonable (ie. over 16MB) 5302168404Spjd */ 5303280822Smav if (zfs_arc_max > 16 << 20 && zfs_arc_max < kmem_size()) 5304168404Spjd arc_c_max = zfs_arc_max; 5305280822Smav if (zfs_arc_min > 16 << 20 && zfs_arc_min <= arc_c_max) 5306168404Spjd arc_c_min = zfs_arc_min; 5307168481Spjd#endif 5308219089Spjd 5309168404Spjd arc_c = arc_c_max; 5310168404Spjd arc_p = (arc_c >> 1); 5311168404Spjd 5312185029Spjd /* limit meta-data to 1/4 of the arc capacity */ 5313185029Spjd arc_meta_limit = arc_c_max / 4; 5314185029Spjd 5315185029Spjd /* Allow the tunable to override if it is reasonable */ 5316185029Spjd if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 5317185029Spjd arc_meta_limit = zfs_arc_meta_limit; 5318185029Spjd 5319185029Spjd if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 5320185029Spjd arc_c_min = arc_meta_limit / 2; 5321185029Spjd 5322275780Sdelphij if (zfs_arc_meta_min > 0) { 5323275780Sdelphij arc_meta_min = zfs_arc_meta_min; 5324275780Sdelphij } else { 5325275780Sdelphij arc_meta_min = arc_c_min / 2; 5326275780Sdelphij } 5327275780Sdelphij 5328208373Smm if (zfs_arc_grow_retry > 0) 5329208373Smm arc_grow_retry = zfs_arc_grow_retry; 5330208373Smm 5331208373Smm if (zfs_arc_shrink_shift > 0) 5332208373Smm arc_shrink_shift = zfs_arc_shrink_shift; 5333208373Smm 5334286625Smav /* 5335286625Smav * Ensure that arc_no_grow_shift is less than arc_shrink_shift. 5336286625Smav */ 5337286625Smav if (arc_no_grow_shift >= arc_shrink_shift) 5338286625Smav arc_no_grow_shift = arc_shrink_shift - 1; 5339286625Smav 5340208373Smm if (zfs_arc_p_min_shift > 0) 5341208373Smm arc_p_min_shift = zfs_arc_p_min_shift; 5342208373Smm 5343286763Smav if (zfs_arc_num_sublists_per_state < 1) 5344286763Smav zfs_arc_num_sublists_per_state = MAX(max_ncpus, 1); 5345286763Smav 5346168404Spjd /* if kmem_flags are set, lets try to use less memory */ 5347168404Spjd if (kmem_debugging()) 5348168404Spjd arc_c = arc_c / 2; 5349168404Spjd if (arc_c < arc_c_min) 5350168404Spjd arc_c = arc_c_min; 5351168404Spjd 5352168473Spjd zfs_arc_min = arc_c_min; 5353168473Spjd zfs_arc_max = arc_c_max; 5354168473Spjd 5355168404Spjd arc_anon = &ARC_anon; 5356168404Spjd arc_mru = &ARC_mru; 5357168404Spjd arc_mru_ghost = &ARC_mru_ghost; 5358168404Spjd arc_mfu = &ARC_mfu; 5359168404Spjd arc_mfu_ghost = &ARC_mfu_ghost; 5360185029Spjd arc_l2c_only = &ARC_l2c_only; 5361168404Spjd arc_size = 0; 5362168404Spjd 5363286763Smav multilist_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 5364286762Smav sizeof (arc_buf_hdr_t), 5365286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5366286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5367286763Smav multilist_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 5368286762Smav sizeof (arc_buf_hdr_t), 5369286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5370286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5371286763Smav multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 5372286762Smav sizeof (arc_buf_hdr_t), 5373286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5374286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5375286763Smav multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 5376286762Smav sizeof (arc_buf_hdr_t), 5377286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5378286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5379286763Smav multilist_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 5380286762Smav sizeof (arc_buf_hdr_t), 5381286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5382286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5383286763Smav multilist_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 5384286762Smav sizeof (arc_buf_hdr_t), 5385286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5386286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5387286763Smav multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 5388286762Smav sizeof (arc_buf_hdr_t), 5389286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5390286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5391286763Smav multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 5392286762Smav sizeof (arc_buf_hdr_t), 5393286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5394286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5395286763Smav multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 5396286762Smav sizeof (arc_buf_hdr_t), 5397286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5398286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5399286763Smav multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 5400286762Smav sizeof (arc_buf_hdr_t), 5401286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5402286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5403168404Spjd 5404286766Smav refcount_create(&arc_anon->arcs_size); 5405286766Smav refcount_create(&arc_mru->arcs_size); 5406286766Smav refcount_create(&arc_mru_ghost->arcs_size); 5407286766Smav refcount_create(&arc_mfu->arcs_size); 5408286766Smav refcount_create(&arc_mfu_ghost->arcs_size); 5409286766Smav refcount_create(&arc_l2c_only->arcs_size); 5410286766Smav 5411168404Spjd buf_init(); 5412168404Spjd 5413286763Smav arc_reclaim_thread_exit = FALSE; 5414286763Smav arc_user_evicts_thread_exit = FALSE; 5415168404Spjd arc_eviction_list = NULL; 5416168404Spjd bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 5417168404Spjd 5418168404Spjd arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 5419168404Spjd sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 5420168404Spjd 5421168404Spjd if (arc_ksp != NULL) { 5422168404Spjd arc_ksp->ks_data = &arc_stats; 5423286574Smav arc_ksp->ks_update = arc_kstat_update; 5424168404Spjd kstat_install(arc_ksp); 5425168404Spjd } 5426168404Spjd 5427168404Spjd (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 5428168404Spjd TS_RUN, minclsyspri); 5429168404Spjd 5430168404Spjd#ifdef _KERNEL 5431168566Spjd arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 5432168404Spjd EVENTHANDLER_PRI_FIRST); 5433168404Spjd#endif 5434168404Spjd 5435286763Smav (void) thread_create(NULL, 0, arc_user_evicts_thread, NULL, 0, &p0, 5436286763Smav TS_RUN, minclsyspri); 5437286763Smav 5438168404Spjd arc_dead = FALSE; 5439185029Spjd arc_warm = B_FALSE; 5440168566Spjd 5441258632Savg /* 5442258632Savg * Calculate maximum amount of dirty data per pool. 5443258632Savg * 5444258632Savg * If it has been set by /etc/system, take that. 5445258632Savg * Otherwise, use a percentage of physical memory defined by 5446258632Savg * zfs_dirty_data_max_percent (default 10%) with a cap at 5447258632Savg * zfs_dirty_data_max_max (default 4GB). 5448258632Savg */ 5449258632Savg if (zfs_dirty_data_max == 0) { 5450258632Savg zfs_dirty_data_max = ptob(physmem) * 5451258632Savg zfs_dirty_data_max_percent / 100; 5452258632Savg zfs_dirty_data_max = MIN(zfs_dirty_data_max, 5453258632Savg zfs_dirty_data_max_max); 5454258632Savg } 5455185029Spjd 5456168566Spjd#ifdef _KERNEL 5457194043Skmacy if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 5458193953Skmacy prefetch_tunable_set = 1; 5459206796Spjd 5460193878Skmacy#ifdef __i386__ 5461193953Skmacy if (prefetch_tunable_set == 0) { 5462196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 5463196863Strasz "-- to enable,\n"); 5464196863Strasz printf(" add \"vfs.zfs.prefetch_disable=0\" " 5465196863Strasz "to /boot/loader.conf.\n"); 5466219089Spjd zfs_prefetch_disable = 1; 5467193878Skmacy } 5468206796Spjd#else 5469193878Skmacy if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 5470193953Skmacy prefetch_tunable_set == 0) { 5471196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default if less " 5472196941Strasz "than 4GB of RAM is present;\n" 5473196863Strasz " to enable, add \"vfs.zfs.prefetch_disable=0\" " 5474196863Strasz "to /boot/loader.conf.\n"); 5475219089Spjd zfs_prefetch_disable = 1; 5476193878Skmacy } 5477206796Spjd#endif 5478175633Spjd /* Warn about ZFS memory and address space requirements. */ 5479168696Spjd if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 5480168987Sbmah printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 5481168987Sbmah "expect unstable behavior.\n"); 5482175633Spjd } 5483175633Spjd if (kmem_size() < 512 * (1 << 20)) { 5484173419Spjd printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 5485168987Sbmah "expect unstable behavior.\n"); 5486185029Spjd printf(" Consider tuning vm.kmem_size and " 5487173419Spjd "vm.kmem_size_max\n"); 5488185029Spjd printf(" in /boot/loader.conf.\n"); 5489168566Spjd } 5490168566Spjd#endif 5491168404Spjd} 5492168404Spjd 5493168404Spjdvoid 5494168404Spjdarc_fini(void) 5495168404Spjd{ 5496286763Smav mutex_enter(&arc_reclaim_lock); 5497286763Smav arc_reclaim_thread_exit = TRUE; 5498286763Smav /* 5499286763Smav * The reclaim thread will set arc_reclaim_thread_exit back to 5500286763Smav * FALSE when it is finished exiting; we're waiting for that. 5501286763Smav */ 5502286763Smav while (arc_reclaim_thread_exit) { 5503286763Smav cv_signal(&arc_reclaim_thread_cv); 5504286763Smav cv_wait(&arc_reclaim_thread_cv, &arc_reclaim_lock); 5505286763Smav } 5506286763Smav mutex_exit(&arc_reclaim_lock); 5507168404Spjd 5508286763Smav mutex_enter(&arc_user_evicts_lock); 5509286763Smav arc_user_evicts_thread_exit = TRUE; 5510286763Smav /* 5511286763Smav * The user evicts thread will set arc_user_evicts_thread_exit 5512286763Smav * to FALSE when it is finished exiting; we're waiting for that. 5513286763Smav */ 5514286763Smav while (arc_user_evicts_thread_exit) { 5515286763Smav cv_signal(&arc_user_evicts_cv); 5516286763Smav cv_wait(&arc_user_evicts_cv, &arc_user_evicts_lock); 5517286763Smav } 5518286763Smav mutex_exit(&arc_user_evicts_lock); 5519168404Spjd 5520286763Smav /* Use TRUE to ensure *all* buffers are evicted */ 5521286763Smav arc_flush(NULL, TRUE); 5522286763Smav 5523168404Spjd arc_dead = TRUE; 5524168404Spjd 5525168404Spjd if (arc_ksp != NULL) { 5526168404Spjd kstat_delete(arc_ksp); 5527168404Spjd arc_ksp = NULL; 5528168404Spjd } 5529168404Spjd 5530286763Smav mutex_destroy(&arc_reclaim_lock); 5531286763Smav cv_destroy(&arc_reclaim_thread_cv); 5532286763Smav cv_destroy(&arc_reclaim_waiters_cv); 5533168404Spjd 5534286763Smav mutex_destroy(&arc_user_evicts_lock); 5535286763Smav cv_destroy(&arc_user_evicts_cv); 5536168404Spjd 5537286766Smav refcount_destroy(&arc_anon->arcs_size); 5538286766Smav refcount_destroy(&arc_mru->arcs_size); 5539286766Smav refcount_destroy(&arc_mru_ghost->arcs_size); 5540286766Smav refcount_destroy(&arc_mfu->arcs_size); 5541286766Smav refcount_destroy(&arc_mfu_ghost->arcs_size); 5542286766Smav refcount_destroy(&arc_l2c_only->arcs_size); 5543286766Smav 5544286763Smav multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 5545286763Smav multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 5546286763Smav multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 5547286763Smav multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 5548286763Smav multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 5549286763Smav multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 5550286763Smav multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 5551286763Smav multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 5552206796Spjd 5553168404Spjd buf_fini(); 5554168404Spjd 5555286570Smav ASSERT0(arc_loaned_bytes); 5556209962Smm 5557168404Spjd#ifdef _KERNEL 5558168566Spjd if (arc_event_lowmem != NULL) 5559168566Spjd EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 5560168404Spjd#endif 5561168404Spjd} 5562185029Spjd 5563185029Spjd/* 5564185029Spjd * Level 2 ARC 5565185029Spjd * 5566185029Spjd * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 5567185029Spjd * It uses dedicated storage devices to hold cached data, which are populated 5568185029Spjd * using large infrequent writes. The main role of this cache is to boost 5569185029Spjd * the performance of random read workloads. The intended L2ARC devices 5570185029Spjd * include short-stroked disks, solid state disks, and other media with 5571185029Spjd * substantially faster read latency than disk. 5572185029Spjd * 5573185029Spjd * +-----------------------+ 5574185029Spjd * | ARC | 5575185029Spjd * +-----------------------+ 5576185029Spjd * | ^ ^ 5577185029Spjd * | | | 5578185029Spjd * l2arc_feed_thread() arc_read() 5579185029Spjd * | | | 5580185029Spjd * | l2arc read | 5581185029Spjd * V | | 5582185029Spjd * +---------------+ | 5583185029Spjd * | L2ARC | | 5584185029Spjd * +---------------+ | 5585185029Spjd * | ^ | 5586185029Spjd * l2arc_write() | | 5587185029Spjd * | | | 5588185029Spjd * V | | 5589185029Spjd * +-------+ +-------+ 5590185029Spjd * | vdev | | vdev | 5591185029Spjd * | cache | | cache | 5592185029Spjd * +-------+ +-------+ 5593185029Spjd * +=========+ .-----. 5594185029Spjd * : L2ARC : |-_____-| 5595185029Spjd * : devices : | Disks | 5596185029Spjd * +=========+ `-_____-' 5597185029Spjd * 5598185029Spjd * Read requests are satisfied from the following sources, in order: 5599185029Spjd * 5600185029Spjd * 1) ARC 5601185029Spjd * 2) vdev cache of L2ARC devices 5602185029Spjd * 3) L2ARC devices 5603185029Spjd * 4) vdev cache of disks 5604185029Spjd * 5) disks 5605185029Spjd * 5606185029Spjd * Some L2ARC device types exhibit extremely slow write performance. 5607185029Spjd * To accommodate for this there are some significant differences between 5608185029Spjd * the L2ARC and traditional cache design: 5609185029Spjd * 5610185029Spjd * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 5611185029Spjd * the ARC behave as usual, freeing buffers and placing headers on ghost 5612185029Spjd * lists. The ARC does not send buffers to the L2ARC during eviction as 5613185029Spjd * this would add inflated write latencies for all ARC memory pressure. 5614185029Spjd * 5615185029Spjd * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 5616185029Spjd * It does this by periodically scanning buffers from the eviction-end of 5617185029Spjd * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 5618251478Sdelphij * not already there. It scans until a headroom of buffers is satisfied, 5619251478Sdelphij * which itself is a buffer for ARC eviction. If a compressible buffer is 5620251478Sdelphij * found during scanning and selected for writing to an L2ARC device, we 5621251478Sdelphij * temporarily boost scanning headroom during the next scan cycle to make 5622251478Sdelphij * sure we adapt to compression effects (which might significantly reduce 5623251478Sdelphij * the data volume we write to L2ARC). The thread that does this is 5624185029Spjd * l2arc_feed_thread(), illustrated below; example sizes are included to 5625185029Spjd * provide a better sense of ratio than this diagram: 5626185029Spjd * 5627185029Spjd * head --> tail 5628185029Spjd * +---------------------+----------+ 5629185029Spjd * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 5630185029Spjd * +---------------------+----------+ | o L2ARC eligible 5631185029Spjd * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 5632185029Spjd * +---------------------+----------+ | 5633185029Spjd * 15.9 Gbytes ^ 32 Mbytes | 5634185029Spjd * headroom | 5635185029Spjd * l2arc_feed_thread() 5636185029Spjd * | 5637185029Spjd * l2arc write hand <--[oooo]--' 5638185029Spjd * | 8 Mbyte 5639185029Spjd * | write max 5640185029Spjd * V 5641185029Spjd * +==============================+ 5642185029Spjd * L2ARC dev |####|#|###|###| |####| ... | 5643185029Spjd * +==============================+ 5644185029Spjd * 32 Gbytes 5645185029Spjd * 5646185029Spjd * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 5647185029Spjd * evicted, then the L2ARC has cached a buffer much sooner than it probably 5648185029Spjd * needed to, potentially wasting L2ARC device bandwidth and storage. It is 5649185029Spjd * safe to say that this is an uncommon case, since buffers at the end of 5650185029Spjd * the ARC lists have moved there due to inactivity. 5651185029Spjd * 5652185029Spjd * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 5653185029Spjd * then the L2ARC simply misses copying some buffers. This serves as a 5654185029Spjd * pressure valve to prevent heavy read workloads from both stalling the ARC 5655185029Spjd * with waits and clogging the L2ARC with writes. This also helps prevent 5656185029Spjd * the potential for the L2ARC to churn if it attempts to cache content too 5657185029Spjd * quickly, such as during backups of the entire pool. 5658185029Spjd * 5659185029Spjd * 5. After system boot and before the ARC has filled main memory, there are 5660185029Spjd * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 5661185029Spjd * lists can remain mostly static. Instead of searching from tail of these 5662185029Spjd * lists as pictured, the l2arc_feed_thread() will search from the list heads 5663185029Spjd * for eligible buffers, greatly increasing its chance of finding them. 5664185029Spjd * 5665185029Spjd * The L2ARC device write speed is also boosted during this time so that 5666185029Spjd * the L2ARC warms up faster. Since there have been no ARC evictions yet, 5667185029Spjd * there are no L2ARC reads, and no fear of degrading read performance 5668185029Spjd * through increased writes. 5669185029Spjd * 5670185029Spjd * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 5671185029Spjd * the vdev queue can aggregate them into larger and fewer writes. Each 5672185029Spjd * device is written to in a rotor fashion, sweeping writes through 5673185029Spjd * available space then repeating. 5674185029Spjd * 5675185029Spjd * 7. The L2ARC does not store dirty content. It never needs to flush 5676185029Spjd * write buffers back to disk based storage. 5677185029Spjd * 5678185029Spjd * 8. If an ARC buffer is written (and dirtied) which also exists in the 5679185029Spjd * L2ARC, the now stale L2ARC buffer is immediately dropped. 5680185029Spjd * 5681185029Spjd * The performance of the L2ARC can be tweaked by a number of tunables, which 5682185029Spjd * may be necessary for different workloads: 5683185029Spjd * 5684185029Spjd * l2arc_write_max max write bytes per interval 5685185029Spjd * l2arc_write_boost extra write bytes during device warmup 5686185029Spjd * l2arc_noprefetch skip caching prefetched buffers 5687185029Spjd * l2arc_headroom number of max device writes to precache 5688251478Sdelphij * l2arc_headroom_boost when we find compressed buffers during ARC 5689251478Sdelphij * scanning, we multiply headroom by this 5690251478Sdelphij * percentage factor for the next scan cycle, 5691251478Sdelphij * since more compressed buffers are likely to 5692251478Sdelphij * be present 5693185029Spjd * l2arc_feed_secs seconds between L2ARC writing 5694185029Spjd * 5695185029Spjd * Tunables may be removed or added as future performance improvements are 5696185029Spjd * integrated, and also may become zpool properties. 5697208373Smm * 5698208373Smm * There are three key functions that control how the L2ARC warms up: 5699208373Smm * 5700208373Smm * l2arc_write_eligible() check if a buffer is eligible to cache 5701208373Smm * l2arc_write_size() calculate how much to write 5702208373Smm * l2arc_write_interval() calculate sleep delay between writes 5703208373Smm * 5704208373Smm * These three functions determine what to write, how much, and how quickly 5705208373Smm * to send writes. 5706185029Spjd */ 5707185029Spjd 5708208373Smmstatic boolean_t 5709275811Sdelphijl2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) 5710208373Smm{ 5711208373Smm /* 5712208373Smm * A buffer is *not* eligible for the L2ARC if it: 5713208373Smm * 1. belongs to a different spa. 5714208373Smm * 2. is already cached on the L2ARC. 5715208373Smm * 3. has an I/O in progress (it may be an incomplete read). 5716208373Smm * 4. is flagged not eligible (zfs property). 5717208373Smm */ 5718275811Sdelphij if (hdr->b_spa != spa_guid) { 5719208373Smm ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 5720208373Smm return (B_FALSE); 5721208373Smm } 5722286570Smav if (HDR_HAS_L2HDR(hdr)) { 5723208373Smm ARCSTAT_BUMP(arcstat_l2_write_in_l2); 5724208373Smm return (B_FALSE); 5725208373Smm } 5726275811Sdelphij if (HDR_IO_IN_PROGRESS(hdr)) { 5727208373Smm ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 5728208373Smm return (B_FALSE); 5729208373Smm } 5730275811Sdelphij if (!HDR_L2CACHE(hdr)) { 5731208373Smm ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 5732208373Smm return (B_FALSE); 5733208373Smm } 5734208373Smm 5735208373Smm return (B_TRUE); 5736208373Smm} 5737208373Smm 5738208373Smmstatic uint64_t 5739251478Sdelphijl2arc_write_size(void) 5740208373Smm{ 5741208373Smm uint64_t size; 5742208373Smm 5743251478Sdelphij /* 5744251478Sdelphij * Make sure our globals have meaningful values in case the user 5745251478Sdelphij * altered them. 5746251478Sdelphij */ 5747251478Sdelphij size = l2arc_write_max; 5748251478Sdelphij if (size == 0) { 5749251478Sdelphij cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 5750251478Sdelphij "be greater than zero, resetting it to the default (%d)", 5751251478Sdelphij L2ARC_WRITE_SIZE); 5752251478Sdelphij size = l2arc_write_max = L2ARC_WRITE_SIZE; 5753251478Sdelphij } 5754208373Smm 5755208373Smm if (arc_warm == B_FALSE) 5756251478Sdelphij size += l2arc_write_boost; 5757208373Smm 5758208373Smm return (size); 5759208373Smm 5760208373Smm} 5761208373Smm 5762208373Smmstatic clock_t 5763208373Smml2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 5764208373Smm{ 5765219089Spjd clock_t interval, next, now; 5766208373Smm 5767208373Smm /* 5768208373Smm * If the ARC lists are busy, increase our write rate; if the 5769208373Smm * lists are stale, idle back. This is achieved by checking 5770208373Smm * how much we previously wrote - if it was more than half of 5771208373Smm * what we wanted, schedule the next write much sooner. 5772208373Smm */ 5773208373Smm if (l2arc_feed_again && wrote > (wanted / 2)) 5774208373Smm interval = (hz * l2arc_feed_min_ms) / 1000; 5775208373Smm else 5776208373Smm interval = hz * l2arc_feed_secs; 5777208373Smm 5778219089Spjd now = ddi_get_lbolt(); 5779219089Spjd next = MAX(now, MIN(now + interval, began + interval)); 5780208373Smm 5781208373Smm return (next); 5782208373Smm} 5783208373Smm 5784185029Spjd/* 5785185029Spjd * Cycle through L2ARC devices. This is how L2ARC load balances. 5786185029Spjd * If a device is returned, this also returns holding the spa config lock. 5787185029Spjd */ 5788185029Spjdstatic l2arc_dev_t * 5789185029Spjdl2arc_dev_get_next(void) 5790185029Spjd{ 5791185029Spjd l2arc_dev_t *first, *next = NULL; 5792185029Spjd 5793185029Spjd /* 5794185029Spjd * Lock out the removal of spas (spa_namespace_lock), then removal 5795185029Spjd * of cache devices (l2arc_dev_mtx). Once a device has been selected, 5796185029Spjd * both locks will be dropped and a spa config lock held instead. 5797185029Spjd */ 5798185029Spjd mutex_enter(&spa_namespace_lock); 5799185029Spjd mutex_enter(&l2arc_dev_mtx); 5800185029Spjd 5801185029Spjd /* if there are no vdevs, there is nothing to do */ 5802185029Spjd if (l2arc_ndev == 0) 5803185029Spjd goto out; 5804185029Spjd 5805185029Spjd first = NULL; 5806185029Spjd next = l2arc_dev_last; 5807185029Spjd do { 5808185029Spjd /* loop around the list looking for a non-faulted vdev */ 5809185029Spjd if (next == NULL) { 5810185029Spjd next = list_head(l2arc_dev_list); 5811185029Spjd } else { 5812185029Spjd next = list_next(l2arc_dev_list, next); 5813185029Spjd if (next == NULL) 5814185029Spjd next = list_head(l2arc_dev_list); 5815185029Spjd } 5816185029Spjd 5817185029Spjd /* if we have come back to the start, bail out */ 5818185029Spjd if (first == NULL) 5819185029Spjd first = next; 5820185029Spjd else if (next == first) 5821185029Spjd break; 5822185029Spjd 5823185029Spjd } while (vdev_is_dead(next->l2ad_vdev)); 5824185029Spjd 5825185029Spjd /* if we were unable to find any usable vdevs, return NULL */ 5826185029Spjd if (vdev_is_dead(next->l2ad_vdev)) 5827185029Spjd next = NULL; 5828185029Spjd 5829185029Spjd l2arc_dev_last = next; 5830185029Spjd 5831185029Spjdout: 5832185029Spjd mutex_exit(&l2arc_dev_mtx); 5833185029Spjd 5834185029Spjd /* 5835185029Spjd * Grab the config lock to prevent the 'next' device from being 5836185029Spjd * removed while we are writing to it. 5837185029Spjd */ 5838185029Spjd if (next != NULL) 5839185029Spjd spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 5840185029Spjd mutex_exit(&spa_namespace_lock); 5841185029Spjd 5842185029Spjd return (next); 5843185029Spjd} 5844185029Spjd 5845185029Spjd/* 5846185029Spjd * Free buffers that were tagged for destruction. 5847185029Spjd */ 5848185029Spjdstatic void 5849185029Spjdl2arc_do_free_on_write() 5850185029Spjd{ 5851185029Spjd list_t *buflist; 5852185029Spjd l2arc_data_free_t *df, *df_prev; 5853185029Spjd 5854185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 5855185029Spjd buflist = l2arc_free_on_write; 5856185029Spjd 5857185029Spjd for (df = list_tail(buflist); df; df = df_prev) { 5858185029Spjd df_prev = list_prev(buflist, df); 5859185029Spjd ASSERT(df->l2df_data != NULL); 5860185029Spjd ASSERT(df->l2df_func != NULL); 5861185029Spjd df->l2df_func(df->l2df_data, df->l2df_size); 5862185029Spjd list_remove(buflist, df); 5863185029Spjd kmem_free(df, sizeof (l2arc_data_free_t)); 5864185029Spjd } 5865185029Spjd 5866185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 5867185029Spjd} 5868185029Spjd 5869185029Spjd/* 5870185029Spjd * A write to a cache device has completed. Update all headers to allow 5871185029Spjd * reads from these buffers to begin. 5872185029Spjd */ 5873185029Spjdstatic void 5874185029Spjdl2arc_write_done(zio_t *zio) 5875185029Spjd{ 5876185029Spjd l2arc_write_callback_t *cb; 5877185029Spjd l2arc_dev_t *dev; 5878185029Spjd list_t *buflist; 5879275811Sdelphij arc_buf_hdr_t *head, *hdr, *hdr_prev; 5880185029Spjd kmutex_t *hash_lock; 5881268085Sdelphij int64_t bytes_dropped = 0; 5882185029Spjd 5883185029Spjd cb = zio->io_private; 5884185029Spjd ASSERT(cb != NULL); 5885185029Spjd dev = cb->l2wcb_dev; 5886185029Spjd ASSERT(dev != NULL); 5887185029Spjd head = cb->l2wcb_head; 5888185029Spjd ASSERT(head != NULL); 5889286570Smav buflist = &dev->l2ad_buflist; 5890185029Spjd ASSERT(buflist != NULL); 5891185029Spjd DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 5892185029Spjd l2arc_write_callback_t *, cb); 5893185029Spjd 5894185029Spjd if (zio->io_error != 0) 5895185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_error); 5896185029Spjd 5897185029Spjd /* 5898185029Spjd * All writes completed, or an error was hit. 5899185029Spjd */ 5900286763Smavtop: 5901286763Smav mutex_enter(&dev->l2ad_mtx); 5902275811Sdelphij for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { 5903275811Sdelphij hdr_prev = list_prev(buflist, hdr); 5904185029Spjd 5905275811Sdelphij hash_lock = HDR_LOCK(hdr); 5906286763Smav 5907286763Smav /* 5908286763Smav * We cannot use mutex_enter or else we can deadlock 5909286763Smav * with l2arc_write_buffers (due to swapping the order 5910286763Smav * the hash lock and l2ad_mtx are taken). 5911286763Smav */ 5912185029Spjd if (!mutex_tryenter(hash_lock)) { 5913185029Spjd /* 5914286763Smav * Missed the hash lock. We must retry so we 5915286763Smav * don't leave the ARC_FLAG_L2_WRITING bit set. 5916185029Spjd */ 5917286763Smav ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); 5918286763Smav 5919286763Smav /* 5920286763Smav * We don't want to rescan the headers we've 5921286763Smav * already marked as having been written out, so 5922286763Smav * we reinsert the head node so we can pick up 5923286763Smav * where we left off. 5924286763Smav */ 5925286763Smav list_remove(buflist, head); 5926286763Smav list_insert_after(buflist, hdr, head); 5927286763Smav 5928286763Smav mutex_exit(&dev->l2ad_mtx); 5929286763Smav 5930286763Smav /* 5931286763Smav * We wait for the hash lock to become available 5932286763Smav * to try and prevent busy waiting, and increase 5933286763Smav * the chance we'll be able to acquire the lock 5934286763Smav * the next time around. 5935286763Smav */ 5936286763Smav mutex_enter(hash_lock); 5937286763Smav mutex_exit(hash_lock); 5938286763Smav goto top; 5939185029Spjd } 5940185029Spjd 5941286570Smav /* 5942286763Smav * We could not have been moved into the arc_l2c_only 5943286763Smav * state while in-flight due to our ARC_FLAG_L2_WRITING 5944286763Smav * bit being set. Let's just ensure that's being enforced. 5945286570Smav */ 5946286763Smav ASSERT(HDR_HAS_L1HDR(hdr)); 5947286570Smav 5948286763Smav /* 5949286763Smav * We may have allocated a buffer for L2ARC compression, 5950286763Smav * we must release it to avoid leaking this data. 5951286763Smav */ 5952286763Smav l2arc_release_cdata_buf(hdr); 5953286763Smav 5954185029Spjd if (zio->io_error != 0) { 5955185029Spjd /* 5956185029Spjd * Error - drop L2ARC entry. 5957185029Spjd */ 5958286776Smav list_remove(buflist, hdr); 5959286570Smav trim_map_free(hdr->b_l2hdr.b_dev->l2ad_vdev, 5960286570Smav hdr->b_l2hdr.b_daddr, hdr->b_l2hdr.b_asize, 0); 5961286570Smav hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR; 5962286570Smav 5963286570Smav ARCSTAT_INCR(arcstat_l2_asize, -hdr->b_l2hdr.b_asize); 5964275811Sdelphij ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 5965286598Smav 5966286598Smav bytes_dropped += hdr->b_l2hdr.b_asize; 5967286598Smav (void) refcount_remove_many(&dev->l2ad_alloc, 5968286598Smav hdr->b_l2hdr.b_asize, hdr); 5969185029Spjd } 5970185029Spjd 5971185029Spjd /* 5972286763Smav * Allow ARC to begin reads and ghost list evictions to 5973286763Smav * this L2ARC entry. 5974185029Spjd */ 5975275811Sdelphij hdr->b_flags &= ~ARC_FLAG_L2_WRITING; 5976185029Spjd 5977185029Spjd mutex_exit(hash_lock); 5978185029Spjd } 5979185029Spjd 5980185029Spjd atomic_inc_64(&l2arc_writes_done); 5981185029Spjd list_remove(buflist, head); 5982286570Smav ASSERT(!HDR_HAS_L1HDR(head)); 5983286570Smav kmem_cache_free(hdr_l2only_cache, head); 5984286570Smav mutex_exit(&dev->l2ad_mtx); 5985185029Spjd 5986268085Sdelphij vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 5987268085Sdelphij 5988185029Spjd l2arc_do_free_on_write(); 5989185029Spjd 5990185029Spjd kmem_free(cb, sizeof (l2arc_write_callback_t)); 5991185029Spjd} 5992185029Spjd 5993185029Spjd/* 5994185029Spjd * A read to a cache device completed. Validate buffer contents before 5995185029Spjd * handing over to the regular ARC routines. 5996185029Spjd */ 5997185029Spjdstatic void 5998185029Spjdl2arc_read_done(zio_t *zio) 5999185029Spjd{ 6000185029Spjd l2arc_read_callback_t *cb; 6001185029Spjd arc_buf_hdr_t *hdr; 6002185029Spjd arc_buf_t *buf; 6003185029Spjd kmutex_t *hash_lock; 6004185029Spjd int equal; 6005185029Spjd 6006185029Spjd ASSERT(zio->io_vd != NULL); 6007185029Spjd ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 6008185029Spjd 6009185029Spjd spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 6010185029Spjd 6011185029Spjd cb = zio->io_private; 6012185029Spjd ASSERT(cb != NULL); 6013185029Spjd buf = cb->l2rcb_buf; 6014185029Spjd ASSERT(buf != NULL); 6015185029Spjd 6016219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 6017185029Spjd mutex_enter(hash_lock); 6018219089Spjd hdr = buf->b_hdr; 6019219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 6020185029Spjd 6021185029Spjd /* 6022251478Sdelphij * If the buffer was compressed, decompress it first. 6023251478Sdelphij */ 6024251478Sdelphij if (cb->l2rcb_compress != ZIO_COMPRESS_OFF) 6025251478Sdelphij l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress); 6026251478Sdelphij ASSERT(zio->io_data != NULL); 6027287706Sdelphij ASSERT3U(zio->io_size, ==, hdr->b_size); 6028287706Sdelphij ASSERT3U(BP_GET_LSIZE(&cb->l2rcb_bp), ==, hdr->b_size); 6029251478Sdelphij 6030251478Sdelphij /* 6031185029Spjd * Check this survived the L2ARC journey. 6032185029Spjd */ 6033185029Spjd equal = arc_cksum_equal(buf); 6034185029Spjd if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 6035185029Spjd mutex_exit(hash_lock); 6036185029Spjd zio->io_private = buf; 6037185029Spjd zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 6038185029Spjd zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 6039185029Spjd arc_read_done(zio); 6040185029Spjd } else { 6041185029Spjd mutex_exit(hash_lock); 6042185029Spjd /* 6043185029Spjd * Buffer didn't survive caching. Increment stats and 6044185029Spjd * reissue to the original storage device. 6045185029Spjd */ 6046185029Spjd if (zio->io_error != 0) { 6047185029Spjd ARCSTAT_BUMP(arcstat_l2_io_error); 6048185029Spjd } else { 6049249195Smm zio->io_error = SET_ERROR(EIO); 6050185029Spjd } 6051185029Spjd if (!equal) 6052185029Spjd ARCSTAT_BUMP(arcstat_l2_cksum_bad); 6053185029Spjd 6054185029Spjd /* 6055185029Spjd * If there's no waiter, issue an async i/o to the primary 6056185029Spjd * storage now. If there *is* a waiter, the caller must 6057185029Spjd * issue the i/o in a context where it's OK to block. 6058185029Spjd */ 6059209962Smm if (zio->io_waiter == NULL) { 6060209962Smm zio_t *pio = zio_unique_parent(zio); 6061209962Smm 6062209962Smm ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 6063209962Smm 6064209962Smm zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 6065287706Sdelphij buf->b_data, hdr->b_size, arc_read_done, buf, 6066185029Spjd zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 6067209962Smm } 6068185029Spjd } 6069185029Spjd 6070185029Spjd kmem_free(cb, sizeof (l2arc_read_callback_t)); 6071185029Spjd} 6072185029Spjd 6073185029Spjd/* 6074185029Spjd * This is the list priority from which the L2ARC will search for pages to 6075185029Spjd * cache. This is used within loops (0..3) to cycle through lists in the 6076185029Spjd * desired order. This order can have a significant effect on cache 6077185029Spjd * performance. 6078185029Spjd * 6079185029Spjd * Currently the metadata lists are hit first, MFU then MRU, followed by 6080185029Spjd * the data lists. This function returns a locked list, and also returns 6081185029Spjd * the lock pointer. 6082185029Spjd */ 6083286763Smavstatic multilist_sublist_t * 6084286763Smavl2arc_sublist_lock(int list_num) 6085185029Spjd{ 6086286763Smav multilist_t *ml = NULL; 6087286763Smav unsigned int idx; 6088185029Spjd 6089286762Smav ASSERT(list_num >= 0 && list_num <= 3); 6090206796Spjd 6091286762Smav switch (list_num) { 6092286762Smav case 0: 6093286763Smav ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 6094286762Smav break; 6095286762Smav case 1: 6096286763Smav ml = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 6097286762Smav break; 6098286762Smav case 2: 6099286763Smav ml = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 6100286762Smav break; 6101286762Smav case 3: 6102286763Smav ml = &arc_mru->arcs_list[ARC_BUFC_DATA]; 6103286762Smav break; 6104185029Spjd } 6105185029Spjd 6106286763Smav /* 6107286763Smav * Return a randomly-selected sublist. This is acceptable 6108286763Smav * because the caller feeds only a little bit of data for each 6109286763Smav * call (8MB). Subsequent calls will result in different 6110286763Smav * sublists being selected. 6111286763Smav */ 6112286763Smav idx = multilist_get_random_index(ml); 6113286763Smav return (multilist_sublist_lock(ml, idx)); 6114185029Spjd} 6115185029Spjd 6116185029Spjd/* 6117185029Spjd * Evict buffers from the device write hand to the distance specified in 6118185029Spjd * bytes. This distance may span populated buffers, it may span nothing. 6119185029Spjd * This is clearing a region on the L2ARC device ready for writing. 6120185029Spjd * If the 'all' boolean is set, every buffer is evicted. 6121185029Spjd */ 6122185029Spjdstatic void 6123185029Spjdl2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 6124185029Spjd{ 6125185029Spjd list_t *buflist; 6126275811Sdelphij arc_buf_hdr_t *hdr, *hdr_prev; 6127185029Spjd kmutex_t *hash_lock; 6128185029Spjd uint64_t taddr; 6129185029Spjd 6130286570Smav buflist = &dev->l2ad_buflist; 6131185029Spjd 6132185029Spjd if (!all && dev->l2ad_first) { 6133185029Spjd /* 6134185029Spjd * This is the first sweep through the device. There is 6135185029Spjd * nothing to evict. 6136185029Spjd */ 6137185029Spjd return; 6138185029Spjd } 6139185029Spjd 6140185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 6141185029Spjd /* 6142185029Spjd * When nearing the end of the device, evict to the end 6143185029Spjd * before the device write hand jumps to the start. 6144185029Spjd */ 6145185029Spjd taddr = dev->l2ad_end; 6146185029Spjd } else { 6147185029Spjd taddr = dev->l2ad_hand + distance; 6148185029Spjd } 6149185029Spjd DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 6150185029Spjd uint64_t, taddr, boolean_t, all); 6151185029Spjd 6152185029Spjdtop: 6153286570Smav mutex_enter(&dev->l2ad_mtx); 6154275811Sdelphij for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { 6155275811Sdelphij hdr_prev = list_prev(buflist, hdr); 6156185029Spjd 6157275811Sdelphij hash_lock = HDR_LOCK(hdr); 6158286763Smav 6159286763Smav /* 6160286763Smav * We cannot use mutex_enter or else we can deadlock 6161286763Smav * with l2arc_write_buffers (due to swapping the order 6162286763Smav * the hash lock and l2ad_mtx are taken). 6163286763Smav */ 6164185029Spjd if (!mutex_tryenter(hash_lock)) { 6165185029Spjd /* 6166185029Spjd * Missed the hash lock. Retry. 6167185029Spjd */ 6168185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 6169286570Smav mutex_exit(&dev->l2ad_mtx); 6170185029Spjd mutex_enter(hash_lock); 6171185029Spjd mutex_exit(hash_lock); 6172185029Spjd goto top; 6173185029Spjd } 6174185029Spjd 6175275811Sdelphij if (HDR_L2_WRITE_HEAD(hdr)) { 6176185029Spjd /* 6177185029Spjd * We hit a write head node. Leave it for 6178185029Spjd * l2arc_write_done(). 6179185029Spjd */ 6180275811Sdelphij list_remove(buflist, hdr); 6181185029Spjd mutex_exit(hash_lock); 6182185029Spjd continue; 6183185029Spjd } 6184185029Spjd 6185286570Smav if (!all && HDR_HAS_L2HDR(hdr) && 6186286570Smav (hdr->b_l2hdr.b_daddr > taddr || 6187286570Smav hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { 6188185029Spjd /* 6189185029Spjd * We've evicted to the target address, 6190185029Spjd * or the end of the device. 6191185029Spjd */ 6192185029Spjd mutex_exit(hash_lock); 6193185029Spjd break; 6194185029Spjd } 6195185029Spjd 6196286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 6197286570Smav if (!HDR_HAS_L1HDR(hdr)) { 6198275811Sdelphij ASSERT(!HDR_L2_READING(hdr)); 6199185029Spjd /* 6200185029Spjd * This doesn't exist in the ARC. Destroy. 6201185029Spjd * arc_hdr_destroy() will call list_remove() 6202185029Spjd * and decrement arcstat_l2_size. 6203185029Spjd */ 6204275811Sdelphij arc_change_state(arc_anon, hdr, hash_lock); 6205275811Sdelphij arc_hdr_destroy(hdr); 6206185029Spjd } else { 6207286570Smav ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); 6208286570Smav ARCSTAT_BUMP(arcstat_l2_evict_l1cached); 6209185029Spjd /* 6210185029Spjd * Invalidate issued or about to be issued 6211185029Spjd * reads, since we may be about to write 6212185029Spjd * over this location. 6213185029Spjd */ 6214275811Sdelphij if (HDR_L2_READING(hdr)) { 6215185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_reading); 6216275811Sdelphij hdr->b_flags |= ARC_FLAG_L2_EVICTED; 6217185029Spjd } 6218185029Spjd 6219286763Smav /* Ensure this header has finished being written */ 6220286763Smav ASSERT(!HDR_L2_WRITING(hdr)); 6221286763Smav ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 6222286763Smav 6223286598Smav arc_hdr_l2hdr_destroy(hdr); 6224185029Spjd } 6225185029Spjd mutex_exit(hash_lock); 6226185029Spjd } 6227286570Smav mutex_exit(&dev->l2ad_mtx); 6228185029Spjd} 6229185029Spjd 6230185029Spjd/* 6231185029Spjd * Find and write ARC buffers to the L2ARC device. 6232185029Spjd * 6233275811Sdelphij * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid 6234185029Spjd * for reading until they have completed writing. 6235251478Sdelphij * The headroom_boost is an in-out parameter used to maintain headroom boost 6236251478Sdelphij * state between calls to this function. 6237251478Sdelphij * 6238251478Sdelphij * Returns the number of bytes actually written (which may be smaller than 6239251478Sdelphij * the delta by which the device hand has changed due to alignment). 6240185029Spjd */ 6241208373Smmstatic uint64_t 6242251478Sdelphijl2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz, 6243251478Sdelphij boolean_t *headroom_boost) 6244185029Spjd{ 6245275811Sdelphij arc_buf_hdr_t *hdr, *hdr_prev, *head; 6246287099Savg uint64_t write_asize, write_sz, headroom, buf_compress_minsz; 6247185029Spjd void *buf_data; 6248251478Sdelphij boolean_t full; 6249185029Spjd l2arc_write_callback_t *cb; 6250185029Spjd zio_t *pio, *wzio; 6251228103Smm uint64_t guid = spa_load_guid(spa); 6252251478Sdelphij const boolean_t do_headroom_boost = *headroom_boost; 6253185029Spjd int try; 6254185029Spjd 6255185029Spjd ASSERT(dev->l2ad_vdev != NULL); 6256185029Spjd 6257251478Sdelphij /* Lower the flag now, we might want to raise it again later. */ 6258251478Sdelphij *headroom_boost = B_FALSE; 6259251478Sdelphij 6260185029Spjd pio = NULL; 6261287099Savg write_sz = write_asize = 0; 6262185029Spjd full = B_FALSE; 6263286570Smav head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); 6264275811Sdelphij head->b_flags |= ARC_FLAG_L2_WRITE_HEAD; 6265286570Smav head->b_flags |= ARC_FLAG_HAS_L2HDR; 6266185029Spjd 6267205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 6268185029Spjd /* 6269251478Sdelphij * We will want to try to compress buffers that are at least 2x the 6270251478Sdelphij * device sector size. 6271251478Sdelphij */ 6272251478Sdelphij buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift; 6273251478Sdelphij 6274251478Sdelphij /* 6275185029Spjd * Copy buffers for L2ARC writing. 6276185029Spjd */ 6277286762Smav for (try = 0; try <= 3; try++) { 6278286763Smav multilist_sublist_t *mls = l2arc_sublist_lock(try); 6279251478Sdelphij uint64_t passed_sz = 0; 6280251478Sdelphij 6281205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 6282185029Spjd 6283185029Spjd /* 6284185029Spjd * L2ARC fast warmup. 6285185029Spjd * 6286185029Spjd * Until the ARC is warm and starts to evict, read from the 6287185029Spjd * head of the ARC lists rather than the tail. 6288185029Spjd */ 6289185029Spjd if (arc_warm == B_FALSE) 6290286763Smav hdr = multilist_sublist_head(mls); 6291185029Spjd else 6292286763Smav hdr = multilist_sublist_tail(mls); 6293275811Sdelphij if (hdr == NULL) 6294205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 6295185029Spjd 6296286762Smav headroom = target_sz * l2arc_headroom; 6297251478Sdelphij if (do_headroom_boost) 6298251478Sdelphij headroom = (headroom * l2arc_headroom_boost) / 100; 6299251478Sdelphij 6300275811Sdelphij for (; hdr; hdr = hdr_prev) { 6301251478Sdelphij kmutex_t *hash_lock; 6302251478Sdelphij uint64_t buf_sz; 6303287099Savg uint64_t buf_a_sz; 6304251478Sdelphij 6305185029Spjd if (arc_warm == B_FALSE) 6306286763Smav hdr_prev = multilist_sublist_next(mls, hdr); 6307185029Spjd else 6308286763Smav hdr_prev = multilist_sublist_prev(mls, hdr); 6309275811Sdelphij ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, hdr->b_size); 6310206796Spjd 6311275811Sdelphij hash_lock = HDR_LOCK(hdr); 6312251478Sdelphij if (!mutex_tryenter(hash_lock)) { 6313205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 6314185029Spjd /* 6315185029Spjd * Skip this buffer rather than waiting. 6316185029Spjd */ 6317185029Spjd continue; 6318185029Spjd } 6319185029Spjd 6320275811Sdelphij passed_sz += hdr->b_size; 6321185029Spjd if (passed_sz > headroom) { 6322185029Spjd /* 6323185029Spjd * Searched too far. 6324185029Spjd */ 6325185029Spjd mutex_exit(hash_lock); 6326205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 6327185029Spjd break; 6328185029Spjd } 6329185029Spjd 6330275811Sdelphij if (!l2arc_write_eligible(guid, hdr)) { 6331185029Spjd mutex_exit(hash_lock); 6332185029Spjd continue; 6333185029Spjd } 6334185029Spjd 6335287099Savg /* 6336287099Savg * Assume that the buffer is not going to be compressed 6337287099Savg * and could take more space on disk because of a larger 6338287099Savg * disk block size. 6339287099Savg */ 6340287099Savg buf_sz = hdr->b_size; 6341287099Savg buf_a_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 6342287099Savg 6343287099Savg if ((write_asize + buf_a_sz) > target_sz) { 6344185029Spjd full = B_TRUE; 6345185029Spjd mutex_exit(hash_lock); 6346205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_full); 6347185029Spjd break; 6348185029Spjd } 6349185029Spjd 6350185029Spjd if (pio == NULL) { 6351185029Spjd /* 6352185029Spjd * Insert a dummy header on the buflist so 6353185029Spjd * l2arc_write_done() can find where the 6354185029Spjd * write buffers begin without searching. 6355185029Spjd */ 6356286763Smav mutex_enter(&dev->l2ad_mtx); 6357286570Smav list_insert_head(&dev->l2ad_buflist, head); 6358286763Smav mutex_exit(&dev->l2ad_mtx); 6359185029Spjd 6360185029Spjd cb = kmem_alloc( 6361185029Spjd sizeof (l2arc_write_callback_t), KM_SLEEP); 6362185029Spjd cb->l2wcb_dev = dev; 6363185029Spjd cb->l2wcb_head = head; 6364185029Spjd pio = zio_root(spa, l2arc_write_done, cb, 6365185029Spjd ZIO_FLAG_CANFAIL); 6366205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_pios); 6367185029Spjd } 6368185029Spjd 6369185029Spjd /* 6370185029Spjd * Create and add a new L2ARC header. 6371185029Spjd */ 6372286570Smav hdr->b_l2hdr.b_dev = dev; 6373275811Sdelphij hdr->b_flags |= ARC_FLAG_L2_WRITING; 6374251478Sdelphij /* 6375251478Sdelphij * Temporarily stash the data buffer in b_tmp_cdata. 6376251478Sdelphij * The subsequent write step will pick it up from 6377286570Smav * there. This is because can't access b_l1hdr.b_buf 6378251478Sdelphij * without holding the hash_lock, which we in turn 6379251478Sdelphij * can't access without holding the ARC list locks 6380251478Sdelphij * (which we want to avoid during compression/writing). 6381251478Sdelphij */ 6382287706Sdelphij hdr->b_l2hdr.b_compress = ZIO_COMPRESS_OFF; 6383286570Smav hdr->b_l2hdr.b_asize = hdr->b_size; 6384286570Smav hdr->b_l1hdr.b_tmp_cdata = hdr->b_l1hdr.b_buf->b_data; 6385251478Sdelphij 6386286598Smav /* 6387286598Smav * Explicitly set the b_daddr field to a known 6388286598Smav * value which means "invalid address". This 6389286598Smav * enables us to differentiate which stage of 6390286598Smav * l2arc_write_buffers() the particular header 6391286598Smav * is in (e.g. this loop, or the one below). 6392286598Smav * ARC_FLAG_L2_WRITING is not enough to make 6393286598Smav * this distinction, and we need to know in 6394286598Smav * order to do proper l2arc vdev accounting in 6395286598Smav * arc_release() and arc_hdr_destroy(). 6396286598Smav * 6397286598Smav * Note, we can't use a new flag to distinguish 6398286598Smav * the two stages because we don't hold the 6399286598Smav * header's hash_lock below, in the second stage 6400286598Smav * of this function. Thus, we can't simply 6401286598Smav * change the b_flags field to denote that the 6402286598Smav * IO has been sent. We can change the b_daddr 6403286598Smav * field of the L2 portion, though, since we'll 6404286598Smav * be holding the l2ad_mtx; which is why we're 6405286598Smav * using it to denote the header's state change. 6406286598Smav */ 6407286598Smav hdr->b_l2hdr.b_daddr = L2ARC_ADDR_UNSET; 6408286570Smav hdr->b_flags |= ARC_FLAG_HAS_L2HDR; 6409185029Spjd 6410286763Smav mutex_enter(&dev->l2ad_mtx); 6411286570Smav list_insert_head(&dev->l2ad_buflist, hdr); 6412286763Smav mutex_exit(&dev->l2ad_mtx); 6413251478Sdelphij 6414185029Spjd /* 6415185029Spjd * Compute and store the buffer cksum before 6416185029Spjd * writing. On debug the cksum is verified first. 6417185029Spjd */ 6418286570Smav arc_cksum_verify(hdr->b_l1hdr.b_buf); 6419286570Smav arc_cksum_compute(hdr->b_l1hdr.b_buf, B_TRUE); 6420185029Spjd 6421185029Spjd mutex_exit(hash_lock); 6422185029Spjd 6423251478Sdelphij write_sz += buf_sz; 6424287099Savg write_asize += buf_a_sz; 6425251478Sdelphij } 6426251478Sdelphij 6427286763Smav multilist_sublist_unlock(mls); 6428251478Sdelphij 6429251478Sdelphij if (full == B_TRUE) 6430251478Sdelphij break; 6431251478Sdelphij } 6432251478Sdelphij 6433251478Sdelphij /* No buffers selected for writing? */ 6434251478Sdelphij if (pio == NULL) { 6435251478Sdelphij ASSERT0(write_sz); 6436286570Smav ASSERT(!HDR_HAS_L1HDR(head)); 6437286570Smav kmem_cache_free(hdr_l2only_cache, head); 6438251478Sdelphij return (0); 6439251478Sdelphij } 6440251478Sdelphij 6441286763Smav mutex_enter(&dev->l2ad_mtx); 6442286763Smav 6443251478Sdelphij /* 6444287099Savg * Note that elsewhere in this file arcstat_l2_asize 6445287099Savg * and the used space on l2ad_vdev are updated using b_asize, 6446287099Savg * which is not necessarily rounded up to the device block size. 6447287099Savg * Too keep accounting consistent we do the same here as well: 6448287099Savg * stats_size accumulates the sum of b_asize of the written buffers, 6449287099Savg * while write_asize accumulates the sum of b_asize rounded up 6450287099Savg * to the device block size. 6451287099Savg * The latter sum is used only to validate the corectness of the code. 6452287099Savg */ 6453287099Savg uint64_t stats_size = 0; 6454287099Savg write_asize = 0; 6455287099Savg 6456287099Savg /* 6457251478Sdelphij * Now start writing the buffers. We're starting at the write head 6458251478Sdelphij * and work backwards, retracing the course of the buffer selector 6459251478Sdelphij * loop above. 6460251478Sdelphij */ 6461286570Smav for (hdr = list_prev(&dev->l2ad_buflist, head); hdr; 6462286570Smav hdr = list_prev(&dev->l2ad_buflist, hdr)) { 6463251478Sdelphij uint64_t buf_sz; 6464251478Sdelphij 6465251478Sdelphij /* 6466286763Smav * We rely on the L1 portion of the header below, so 6467286763Smav * it's invalid for this header to have been evicted out 6468286763Smav * of the ghost cache, prior to being written out. The 6469286763Smav * ARC_FLAG_L2_WRITING bit ensures this won't happen. 6470286763Smav */ 6471286763Smav ASSERT(HDR_HAS_L1HDR(hdr)); 6472286763Smav 6473286763Smav /* 6474251478Sdelphij * We shouldn't need to lock the buffer here, since we flagged 6475275811Sdelphij * it as ARC_FLAG_L2_WRITING in the previous step, but we must 6476275811Sdelphij * take care to only access its L2 cache parameters. In 6477286570Smav * particular, hdr->l1hdr.b_buf may be invalid by now due to 6478275811Sdelphij * ARC eviction. 6479251478Sdelphij */ 6480286570Smav hdr->b_l2hdr.b_daddr = dev->l2ad_hand; 6481251478Sdelphij 6482286570Smav if ((HDR_L2COMPRESS(hdr)) && 6483286570Smav hdr->b_l2hdr.b_asize >= buf_compress_minsz) { 6484286570Smav if (l2arc_compress_buf(hdr)) { 6485251478Sdelphij /* 6486251478Sdelphij * If compression succeeded, enable headroom 6487251478Sdelphij * boost on the next scan cycle. 6488251478Sdelphij */ 6489251478Sdelphij *headroom_boost = B_TRUE; 6490251478Sdelphij } 6491251478Sdelphij } 6492251478Sdelphij 6493251478Sdelphij /* 6494251478Sdelphij * Pick up the buffer data we had previously stashed away 6495251478Sdelphij * (and now potentially also compressed). 6496251478Sdelphij */ 6497286570Smav buf_data = hdr->b_l1hdr.b_tmp_cdata; 6498286570Smav buf_sz = hdr->b_l2hdr.b_asize; 6499251478Sdelphij 6500274172Savg /* 6501286598Smav * We need to do this regardless if buf_sz is zero or 6502286598Smav * not, otherwise, when this l2hdr is evicted we'll 6503286598Smav * remove a reference that was never added. 6504286598Smav */ 6505286598Smav (void) refcount_add_many(&dev->l2ad_alloc, buf_sz, hdr); 6506286598Smav 6507251478Sdelphij /* Compression may have squashed the buffer to zero length. */ 6508251478Sdelphij if (buf_sz != 0) { 6509287099Savg uint64_t buf_a_sz; 6510251478Sdelphij 6511185029Spjd wzio = zio_write_phys(pio, dev->l2ad_vdev, 6512185029Spjd dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 6513185029Spjd NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 6514185029Spjd ZIO_FLAG_CANFAIL, B_FALSE); 6515185029Spjd 6516185029Spjd DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 6517185029Spjd zio_t *, wzio); 6518185029Spjd (void) zio_nowait(wzio); 6519185029Spjd 6520287099Savg stats_size += buf_sz; 6521286598Smav 6522185029Spjd /* 6523185029Spjd * Keep the clock hand suitably device-aligned. 6524185029Spjd */ 6525287099Savg buf_a_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 6526287099Savg write_asize += buf_a_sz; 6527287099Savg dev->l2ad_hand += buf_a_sz; 6528185029Spjd } 6529251478Sdelphij } 6530185029Spjd 6531286570Smav mutex_exit(&dev->l2ad_mtx); 6532185029Spjd 6533251478Sdelphij ASSERT3U(write_asize, <=, target_sz); 6534185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_sent); 6535251478Sdelphij ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize); 6536185029Spjd ARCSTAT_INCR(arcstat_l2_size, write_sz); 6537287099Savg ARCSTAT_INCR(arcstat_l2_asize, stats_size); 6538287099Savg vdev_space_update(dev->l2ad_vdev, stats_size, 0, 0); 6539185029Spjd 6540185029Spjd /* 6541185029Spjd * Bump device hand to the device start if it is approaching the end. 6542185029Spjd * l2arc_evict() will already have evicted ahead for this case. 6543185029Spjd */ 6544185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 6545185029Spjd dev->l2ad_hand = dev->l2ad_start; 6546185029Spjd dev->l2ad_first = B_FALSE; 6547185029Spjd } 6548185029Spjd 6549208373Smm dev->l2ad_writing = B_TRUE; 6550185029Spjd (void) zio_wait(pio); 6551208373Smm dev->l2ad_writing = B_FALSE; 6552208373Smm 6553251478Sdelphij return (write_asize); 6554185029Spjd} 6555185029Spjd 6556185029Spjd/* 6557251478Sdelphij * Compresses an L2ARC buffer. 6558286570Smav * The data to be compressed must be prefilled in l1hdr.b_tmp_cdata and its 6559251478Sdelphij * size in l2hdr->b_asize. This routine tries to compress the data and 6560251478Sdelphij * depending on the compression result there are three possible outcomes: 6561251478Sdelphij * *) The buffer was incompressible. The original l2hdr contents were left 6562251478Sdelphij * untouched and are ready for writing to an L2 device. 6563251478Sdelphij * *) The buffer was all-zeros, so there is no need to write it to an L2 6564251478Sdelphij * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is 6565251478Sdelphij * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY. 6566251478Sdelphij * *) Compression succeeded and b_tmp_cdata was replaced with a temporary 6567251478Sdelphij * data buffer which holds the compressed data to be written, and b_asize 6568251478Sdelphij * tells us how much data there is. b_compress is set to the appropriate 6569251478Sdelphij * compression algorithm. Once writing is done, invoke 6570251478Sdelphij * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer. 6571251478Sdelphij * 6572251478Sdelphij * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the 6573251478Sdelphij * buffer was incompressible). 6574251478Sdelphij */ 6575251478Sdelphijstatic boolean_t 6576286570Smavl2arc_compress_buf(arc_buf_hdr_t *hdr) 6577251478Sdelphij{ 6578251478Sdelphij void *cdata; 6579268075Sdelphij size_t csize, len, rounded; 6580286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 6581286570Smav l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 6582251478Sdelphij 6583286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 6584287706Sdelphij ASSERT3S(l2hdr->b_compress, ==, ZIO_COMPRESS_OFF); 6585286570Smav ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL); 6586251478Sdelphij 6587251478Sdelphij len = l2hdr->b_asize; 6588251478Sdelphij cdata = zio_data_buf_alloc(len); 6589286570Smav ASSERT3P(cdata, !=, NULL); 6590286570Smav csize = zio_compress_data(ZIO_COMPRESS_LZ4, hdr->b_l1hdr.b_tmp_cdata, 6591269086Sdelphij cdata, l2hdr->b_asize); 6592251478Sdelphij 6593251478Sdelphij if (csize == 0) { 6594251478Sdelphij /* zero block, indicate that there's nothing to write */ 6595251478Sdelphij zio_data_buf_free(cdata, len); 6596287706Sdelphij l2hdr->b_compress = ZIO_COMPRESS_EMPTY; 6597251478Sdelphij l2hdr->b_asize = 0; 6598286570Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 6599251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_zeros); 6600251478Sdelphij return (B_TRUE); 6601287283Sdelphij } 6602287283Sdelphij 6603287283Sdelphij rounded = P2ROUNDUP(csize, 6604287283Sdelphij (size_t)1 << l2hdr->b_dev->l2ad_vdev->vdev_ashift); 6605287283Sdelphij if (rounded < len) { 6606251478Sdelphij /* 6607251478Sdelphij * Compression succeeded, we'll keep the cdata around for 6608251478Sdelphij * writing and release it afterwards. 6609251478Sdelphij */ 6610287283Sdelphij if (rounded > csize) { 6611287283Sdelphij bzero((char *)cdata + csize, rounded - csize); 6612287283Sdelphij csize = rounded; 6613287283Sdelphij } 6614287706Sdelphij l2hdr->b_compress = ZIO_COMPRESS_LZ4; 6615251478Sdelphij l2hdr->b_asize = csize; 6616286570Smav hdr->b_l1hdr.b_tmp_cdata = cdata; 6617251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_successes); 6618251478Sdelphij return (B_TRUE); 6619251478Sdelphij } else { 6620251478Sdelphij /* 6621251478Sdelphij * Compression failed, release the compressed buffer. 6622251478Sdelphij * l2hdr will be left unmodified. 6623251478Sdelphij */ 6624251478Sdelphij zio_data_buf_free(cdata, len); 6625251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_failures); 6626251478Sdelphij return (B_FALSE); 6627251478Sdelphij } 6628251478Sdelphij} 6629251478Sdelphij 6630251478Sdelphij/* 6631251478Sdelphij * Decompresses a zio read back from an l2arc device. On success, the 6632251478Sdelphij * underlying zio's io_data buffer is overwritten by the uncompressed 6633251478Sdelphij * version. On decompression error (corrupt compressed stream), the 6634251478Sdelphij * zio->io_error value is set to signal an I/O error. 6635251478Sdelphij * 6636251478Sdelphij * Please note that the compressed data stream is not checksummed, so 6637251478Sdelphij * if the underlying device is experiencing data corruption, we may feed 6638251478Sdelphij * corrupt data to the decompressor, so the decompressor needs to be 6639251478Sdelphij * able to handle this situation (LZ4 does). 6640251478Sdelphij */ 6641251478Sdelphijstatic void 6642251478Sdelphijl2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c) 6643251478Sdelphij{ 6644251478Sdelphij ASSERT(L2ARC_IS_VALID_COMPRESS(c)); 6645251478Sdelphij 6646251478Sdelphij if (zio->io_error != 0) { 6647251478Sdelphij /* 6648251478Sdelphij * An io error has occured, just restore the original io 6649251478Sdelphij * size in preparation for a main pool read. 6650251478Sdelphij */ 6651251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 6652251478Sdelphij return; 6653251478Sdelphij } 6654251478Sdelphij 6655251478Sdelphij if (c == ZIO_COMPRESS_EMPTY) { 6656251478Sdelphij /* 6657251478Sdelphij * An empty buffer results in a null zio, which means we 6658251478Sdelphij * need to fill its io_data after we're done restoring the 6659251478Sdelphij * buffer's contents. 6660251478Sdelphij */ 6661286570Smav ASSERT(hdr->b_l1hdr.b_buf != NULL); 6662286570Smav bzero(hdr->b_l1hdr.b_buf->b_data, hdr->b_size); 6663286570Smav zio->io_data = zio->io_orig_data = hdr->b_l1hdr.b_buf->b_data; 6664251478Sdelphij } else { 6665251478Sdelphij ASSERT(zio->io_data != NULL); 6666251478Sdelphij /* 6667251478Sdelphij * We copy the compressed data from the start of the arc buffer 6668251478Sdelphij * (the zio_read will have pulled in only what we need, the 6669251478Sdelphij * rest is garbage which we will overwrite at decompression) 6670251478Sdelphij * and then decompress back to the ARC data buffer. This way we 6671251478Sdelphij * can minimize copying by simply decompressing back over the 6672251478Sdelphij * original compressed data (rather than decompressing to an 6673251478Sdelphij * aux buffer and then copying back the uncompressed buffer, 6674251478Sdelphij * which is likely to be much larger). 6675251478Sdelphij */ 6676251478Sdelphij uint64_t csize; 6677251478Sdelphij void *cdata; 6678251478Sdelphij 6679251478Sdelphij csize = zio->io_size; 6680251478Sdelphij cdata = zio_data_buf_alloc(csize); 6681251478Sdelphij bcopy(zio->io_data, cdata, csize); 6682251478Sdelphij if (zio_decompress_data(c, cdata, zio->io_data, csize, 6683251478Sdelphij hdr->b_size) != 0) 6684251478Sdelphij zio->io_error = EIO; 6685251478Sdelphij zio_data_buf_free(cdata, csize); 6686251478Sdelphij } 6687251478Sdelphij 6688251478Sdelphij /* Restore the expected uncompressed IO size. */ 6689251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 6690251478Sdelphij} 6691251478Sdelphij 6692251478Sdelphij/* 6693251478Sdelphij * Releases the temporary b_tmp_cdata buffer in an l2arc header structure. 6694251478Sdelphij * This buffer serves as a temporary holder of compressed data while 6695251478Sdelphij * the buffer entry is being written to an l2arc device. Once that is 6696251478Sdelphij * done, we can dispose of it. 6697251478Sdelphij */ 6698251478Sdelphijstatic void 6699275811Sdelphijl2arc_release_cdata_buf(arc_buf_hdr_t *hdr) 6700251478Sdelphij{ 6701287706Sdelphij ASSERT(HDR_HAS_L2HDR(hdr)); 6702287706Sdelphij enum zio_compress comp = hdr->b_l2hdr.b_compress; 6703286763Smav 6704286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 6705286763Smav ASSERT(comp == ZIO_COMPRESS_OFF || L2ARC_IS_VALID_COMPRESS(comp)); 6706286763Smav 6707286763Smav if (comp == ZIO_COMPRESS_OFF) { 6708251478Sdelphij /* 6709286763Smav * In this case, b_tmp_cdata points to the same buffer 6710286763Smav * as the arc_buf_t's b_data field. We don't want to 6711286763Smav * free it, since the arc_buf_t will handle that. 6712286763Smav */ 6713286763Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 6714286763Smav } else if (comp == ZIO_COMPRESS_EMPTY) { 6715286763Smav /* 6716286763Smav * In this case, b_tmp_cdata was compressed to an empty 6717286763Smav * buffer, thus there's nothing to free and b_tmp_cdata 6718286763Smav * should have been set to NULL in l2arc_write_buffers(). 6719286763Smav */ 6720286763Smav ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 6721286763Smav } else { 6722286763Smav /* 6723251478Sdelphij * If the data was compressed, then we've allocated a 6724251478Sdelphij * temporary buffer for it, so now we need to release it. 6725251478Sdelphij */ 6726286570Smav ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL); 6727286570Smav zio_data_buf_free(hdr->b_l1hdr.b_tmp_cdata, 6728286570Smav hdr->b_size); 6729286570Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 6730251478Sdelphij } 6731286776Smav 6732251478Sdelphij} 6733251478Sdelphij 6734251478Sdelphij/* 6735185029Spjd * This thread feeds the L2ARC at regular intervals. This is the beating 6736185029Spjd * heart of the L2ARC. 6737185029Spjd */ 6738185029Spjdstatic void 6739185029Spjdl2arc_feed_thread(void *dummy __unused) 6740185029Spjd{ 6741185029Spjd callb_cpr_t cpr; 6742185029Spjd l2arc_dev_t *dev; 6743185029Spjd spa_t *spa; 6744208373Smm uint64_t size, wrote; 6745219089Spjd clock_t begin, next = ddi_get_lbolt(); 6746251478Sdelphij boolean_t headroom_boost = B_FALSE; 6747185029Spjd 6748185029Spjd CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 6749185029Spjd 6750185029Spjd mutex_enter(&l2arc_feed_thr_lock); 6751185029Spjd 6752185029Spjd while (l2arc_thread_exit == 0) { 6753185029Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 6754185029Spjd (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 6755219089Spjd next - ddi_get_lbolt()); 6756185029Spjd CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 6757219089Spjd next = ddi_get_lbolt() + hz; 6758185029Spjd 6759185029Spjd /* 6760185029Spjd * Quick check for L2ARC devices. 6761185029Spjd */ 6762185029Spjd mutex_enter(&l2arc_dev_mtx); 6763185029Spjd if (l2arc_ndev == 0) { 6764185029Spjd mutex_exit(&l2arc_dev_mtx); 6765185029Spjd continue; 6766185029Spjd } 6767185029Spjd mutex_exit(&l2arc_dev_mtx); 6768219089Spjd begin = ddi_get_lbolt(); 6769185029Spjd 6770185029Spjd /* 6771185029Spjd * This selects the next l2arc device to write to, and in 6772185029Spjd * doing so the next spa to feed from: dev->l2ad_spa. This 6773185029Spjd * will return NULL if there are now no l2arc devices or if 6774185029Spjd * they are all faulted. 6775185029Spjd * 6776185029Spjd * If a device is returned, its spa's config lock is also 6777185029Spjd * held to prevent device removal. l2arc_dev_get_next() 6778185029Spjd * will grab and release l2arc_dev_mtx. 6779185029Spjd */ 6780185029Spjd if ((dev = l2arc_dev_get_next()) == NULL) 6781185029Spjd continue; 6782185029Spjd 6783185029Spjd spa = dev->l2ad_spa; 6784185029Spjd ASSERT(spa != NULL); 6785185029Spjd 6786185029Spjd /* 6787219089Spjd * If the pool is read-only then force the feed thread to 6788219089Spjd * sleep a little longer. 6789219089Spjd */ 6790219089Spjd if (!spa_writeable(spa)) { 6791219089Spjd next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 6792219089Spjd spa_config_exit(spa, SCL_L2ARC, dev); 6793219089Spjd continue; 6794219089Spjd } 6795219089Spjd 6796219089Spjd /* 6797185029Spjd * Avoid contributing to memory pressure. 6798185029Spjd */ 6799185029Spjd if (arc_reclaim_needed()) { 6800185029Spjd ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 6801185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 6802185029Spjd continue; 6803185029Spjd } 6804185029Spjd 6805185029Spjd ARCSTAT_BUMP(arcstat_l2_feeds); 6806185029Spjd 6807251478Sdelphij size = l2arc_write_size(); 6808185029Spjd 6809185029Spjd /* 6810185029Spjd * Evict L2ARC buffers that will be overwritten. 6811185029Spjd */ 6812185029Spjd l2arc_evict(dev, size, B_FALSE); 6813185029Spjd 6814185029Spjd /* 6815185029Spjd * Write ARC buffers. 6816185029Spjd */ 6817251478Sdelphij wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost); 6818208373Smm 6819208373Smm /* 6820208373Smm * Calculate interval between writes. 6821208373Smm */ 6822208373Smm next = l2arc_write_interval(begin, size, wrote); 6823185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 6824185029Spjd } 6825185029Spjd 6826185029Spjd l2arc_thread_exit = 0; 6827185029Spjd cv_broadcast(&l2arc_feed_thr_cv); 6828185029Spjd CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 6829185029Spjd thread_exit(); 6830185029Spjd} 6831185029Spjd 6832185029Spjdboolean_t 6833185029Spjdl2arc_vdev_present(vdev_t *vd) 6834185029Spjd{ 6835185029Spjd l2arc_dev_t *dev; 6836185029Spjd 6837185029Spjd mutex_enter(&l2arc_dev_mtx); 6838185029Spjd for (dev = list_head(l2arc_dev_list); dev != NULL; 6839185029Spjd dev = list_next(l2arc_dev_list, dev)) { 6840185029Spjd if (dev->l2ad_vdev == vd) 6841185029Spjd break; 6842185029Spjd } 6843185029Spjd mutex_exit(&l2arc_dev_mtx); 6844185029Spjd 6845185029Spjd return (dev != NULL); 6846185029Spjd} 6847185029Spjd 6848185029Spjd/* 6849185029Spjd * Add a vdev for use by the L2ARC. By this point the spa has already 6850185029Spjd * validated the vdev and opened it. 6851185029Spjd */ 6852185029Spjdvoid 6853219089Spjdl2arc_add_vdev(spa_t *spa, vdev_t *vd) 6854185029Spjd{ 6855185029Spjd l2arc_dev_t *adddev; 6856185029Spjd 6857185029Spjd ASSERT(!l2arc_vdev_present(vd)); 6858185029Spjd 6859255753Sgibbs vdev_ashift_optimize(vd); 6860255753Sgibbs 6861185029Spjd /* 6862185029Spjd * Create a new l2arc device entry. 6863185029Spjd */ 6864185029Spjd adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 6865185029Spjd adddev->l2ad_spa = spa; 6866185029Spjd adddev->l2ad_vdev = vd; 6867219089Spjd adddev->l2ad_start = VDEV_LABEL_START_SIZE; 6868219089Spjd adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 6869185029Spjd adddev->l2ad_hand = adddev->l2ad_start; 6870185029Spjd adddev->l2ad_first = B_TRUE; 6871208373Smm adddev->l2ad_writing = B_FALSE; 6872185029Spjd 6873286570Smav mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); 6874185029Spjd /* 6875185029Spjd * This is a list of all ARC buffers that are still valid on the 6876185029Spjd * device. 6877185029Spjd */ 6878286570Smav list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 6879286570Smav offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); 6880185029Spjd 6881219089Spjd vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 6882286598Smav refcount_create(&adddev->l2ad_alloc); 6883185029Spjd 6884185029Spjd /* 6885185029Spjd * Add device to global list 6886185029Spjd */ 6887185029Spjd mutex_enter(&l2arc_dev_mtx); 6888185029Spjd list_insert_head(l2arc_dev_list, adddev); 6889185029Spjd atomic_inc_64(&l2arc_ndev); 6890185029Spjd mutex_exit(&l2arc_dev_mtx); 6891185029Spjd} 6892185029Spjd 6893185029Spjd/* 6894185029Spjd * Remove a vdev from the L2ARC. 6895185029Spjd */ 6896185029Spjdvoid 6897185029Spjdl2arc_remove_vdev(vdev_t *vd) 6898185029Spjd{ 6899185029Spjd l2arc_dev_t *dev, *nextdev, *remdev = NULL; 6900185029Spjd 6901185029Spjd /* 6902185029Spjd * Find the device by vdev 6903185029Spjd */ 6904185029Spjd mutex_enter(&l2arc_dev_mtx); 6905185029Spjd for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 6906185029Spjd nextdev = list_next(l2arc_dev_list, dev); 6907185029Spjd if (vd == dev->l2ad_vdev) { 6908185029Spjd remdev = dev; 6909185029Spjd break; 6910185029Spjd } 6911185029Spjd } 6912185029Spjd ASSERT(remdev != NULL); 6913185029Spjd 6914185029Spjd /* 6915185029Spjd * Remove device from global list 6916185029Spjd */ 6917185029Spjd list_remove(l2arc_dev_list, remdev); 6918185029Spjd l2arc_dev_last = NULL; /* may have been invalidated */ 6919185029Spjd atomic_dec_64(&l2arc_ndev); 6920185029Spjd mutex_exit(&l2arc_dev_mtx); 6921185029Spjd 6922185029Spjd /* 6923185029Spjd * Clear all buflists and ARC references. L2ARC device flush. 6924185029Spjd */ 6925185029Spjd l2arc_evict(remdev, 0, B_TRUE); 6926286570Smav list_destroy(&remdev->l2ad_buflist); 6927286570Smav mutex_destroy(&remdev->l2ad_mtx); 6928286598Smav refcount_destroy(&remdev->l2ad_alloc); 6929185029Spjd kmem_free(remdev, sizeof (l2arc_dev_t)); 6930185029Spjd} 6931185029Spjd 6932185029Spjdvoid 6933185029Spjdl2arc_init(void) 6934185029Spjd{ 6935185029Spjd l2arc_thread_exit = 0; 6936185029Spjd l2arc_ndev = 0; 6937185029Spjd l2arc_writes_sent = 0; 6938185029Spjd l2arc_writes_done = 0; 6939185029Spjd 6940185029Spjd mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 6941185029Spjd cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 6942185029Spjd mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 6943185029Spjd mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 6944185029Spjd 6945185029Spjd l2arc_dev_list = &L2ARC_dev_list; 6946185029Spjd l2arc_free_on_write = &L2ARC_free_on_write; 6947185029Spjd list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 6948185029Spjd offsetof(l2arc_dev_t, l2ad_node)); 6949185029Spjd list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 6950185029Spjd offsetof(l2arc_data_free_t, l2df_list_node)); 6951185029Spjd} 6952185029Spjd 6953185029Spjdvoid 6954185029Spjdl2arc_fini(void) 6955185029Spjd{ 6956185029Spjd /* 6957185029Spjd * This is called from dmu_fini(), which is called from spa_fini(); 6958185029Spjd * Because of this, we can assume that all l2arc devices have 6959185029Spjd * already been removed when the pools themselves were removed. 6960185029Spjd */ 6961185029Spjd 6962185029Spjd l2arc_do_free_on_write(); 6963185029Spjd 6964185029Spjd mutex_destroy(&l2arc_feed_thr_lock); 6965185029Spjd cv_destroy(&l2arc_feed_thr_cv); 6966185029Spjd mutex_destroy(&l2arc_dev_mtx); 6967185029Spjd mutex_destroy(&l2arc_free_on_write_mtx); 6968185029Spjd 6969185029Spjd list_destroy(l2arc_dev_list); 6970185029Spjd list_destroy(l2arc_free_on_write); 6971185029Spjd} 6972185029Spjd 6973185029Spjdvoid 6974185029Spjdl2arc_start(void) 6975185029Spjd{ 6976209962Smm if (!(spa_mode_global & FWRITE)) 6977185029Spjd return; 6978185029Spjd 6979185029Spjd (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 6980185029Spjd TS_RUN, minclsyspri); 6981185029Spjd} 6982185029Spjd 6983185029Spjdvoid 6984185029Spjdl2arc_stop(void) 6985185029Spjd{ 6986209962Smm if (!(spa_mode_global & FWRITE)) 6987185029Spjd return; 6988185029Spjd 6989185029Spjd mutex_enter(&l2arc_feed_thr_lock); 6990185029Spjd cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 6991185029Spjd l2arc_thread_exit = 1; 6992185029Spjd while (l2arc_thread_exit != 0) 6993185029Spjd cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 6994185029Spjd mutex_exit(&l2arc_feed_thr_lock); 6995185029Spjd} 6996