arc.c revision 287099
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; 216194043Skmacyextern int 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; 585168404Spjd} arc_stats_t; 586168404Spjd 587168404Spjdstatic arc_stats_t arc_stats = { 588168404Spjd { "hits", KSTAT_DATA_UINT64 }, 589168404Spjd { "misses", KSTAT_DATA_UINT64 }, 590168404Spjd { "demand_data_hits", KSTAT_DATA_UINT64 }, 591168404Spjd { "demand_data_misses", KSTAT_DATA_UINT64 }, 592168404Spjd { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 593168404Spjd { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 594168404Spjd { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 595168404Spjd { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 596168404Spjd { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 597168404Spjd { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 598168404Spjd { "mru_hits", KSTAT_DATA_UINT64 }, 599168404Spjd { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 600168404Spjd { "mfu_hits", KSTAT_DATA_UINT64 }, 601168404Spjd { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 602205231Skmacy { "allocated", KSTAT_DATA_UINT64 }, 603168404Spjd { "deleted", KSTAT_DATA_UINT64 }, 604168404Spjd { "mutex_miss", KSTAT_DATA_UINT64 }, 605168404Spjd { "evict_skip", KSTAT_DATA_UINT64 }, 606286763Smav { "evict_not_enough", KSTAT_DATA_UINT64 }, 607208373Smm { "evict_l2_cached", KSTAT_DATA_UINT64 }, 608208373Smm { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 609208373Smm { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 610286763Smav { "evict_l2_skip", KSTAT_DATA_UINT64 }, 611168404Spjd { "hash_elements", KSTAT_DATA_UINT64 }, 612168404Spjd { "hash_elements_max", KSTAT_DATA_UINT64 }, 613168404Spjd { "hash_collisions", KSTAT_DATA_UINT64 }, 614168404Spjd { "hash_chains", KSTAT_DATA_UINT64 }, 615168404Spjd { "hash_chain_max", KSTAT_DATA_UINT64 }, 616168404Spjd { "p", KSTAT_DATA_UINT64 }, 617168404Spjd { "c", KSTAT_DATA_UINT64 }, 618168404Spjd { "c_min", KSTAT_DATA_UINT64 }, 619168404Spjd { "c_max", KSTAT_DATA_UINT64 }, 620185029Spjd { "size", KSTAT_DATA_UINT64 }, 621185029Spjd { "hdr_size", KSTAT_DATA_UINT64 }, 622208373Smm { "data_size", KSTAT_DATA_UINT64 }, 623286574Smav { "metadata_size", KSTAT_DATA_UINT64 }, 624208373Smm { "other_size", KSTAT_DATA_UINT64 }, 625286574Smav { "anon_size", KSTAT_DATA_UINT64 }, 626286574Smav { "anon_evictable_data", KSTAT_DATA_UINT64 }, 627286574Smav { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, 628286574Smav { "mru_size", KSTAT_DATA_UINT64 }, 629286574Smav { "mru_evictable_data", KSTAT_DATA_UINT64 }, 630286574Smav { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, 631286574Smav { "mru_ghost_size", KSTAT_DATA_UINT64 }, 632286574Smav { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, 633286574Smav { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 634286574Smav { "mfu_size", KSTAT_DATA_UINT64 }, 635286574Smav { "mfu_evictable_data", KSTAT_DATA_UINT64 }, 636286574Smav { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, 637286574Smav { "mfu_ghost_size", KSTAT_DATA_UINT64 }, 638286574Smav { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, 639286574Smav { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 640185029Spjd { "l2_hits", KSTAT_DATA_UINT64 }, 641185029Spjd { "l2_misses", KSTAT_DATA_UINT64 }, 642185029Spjd { "l2_feeds", KSTAT_DATA_UINT64 }, 643185029Spjd { "l2_rw_clash", KSTAT_DATA_UINT64 }, 644208373Smm { "l2_read_bytes", KSTAT_DATA_UINT64 }, 645208373Smm { "l2_write_bytes", KSTAT_DATA_UINT64 }, 646185029Spjd { "l2_writes_sent", KSTAT_DATA_UINT64 }, 647185029Spjd { "l2_writes_done", KSTAT_DATA_UINT64 }, 648185029Spjd { "l2_writes_error", KSTAT_DATA_UINT64 }, 649286763Smav { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, 650185029Spjd { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 651185029Spjd { "l2_evict_reading", KSTAT_DATA_UINT64 }, 652286570Smav { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, 653185029Spjd { "l2_free_on_write", KSTAT_DATA_UINT64 }, 654274172Savg { "l2_cdata_free_on_write", KSTAT_DATA_UINT64 }, 655185029Spjd { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 656185029Spjd { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 657185029Spjd { "l2_io_error", KSTAT_DATA_UINT64 }, 658185029Spjd { "l2_size", KSTAT_DATA_UINT64 }, 659251478Sdelphij { "l2_asize", KSTAT_DATA_UINT64 }, 660185029Spjd { "l2_hdr_size", KSTAT_DATA_UINT64 }, 661251478Sdelphij { "l2_compress_successes", KSTAT_DATA_UINT64 }, 662251478Sdelphij { "l2_compress_zeros", KSTAT_DATA_UINT64 }, 663251478Sdelphij { "l2_compress_failures", KSTAT_DATA_UINT64 }, 664206796Spjd { "l2_write_trylock_fail", KSTAT_DATA_UINT64 }, 665206796Spjd { "l2_write_passed_headroom", KSTAT_DATA_UINT64 }, 666206796Spjd { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 }, 667206796Spjd { "l2_write_in_l2", KSTAT_DATA_UINT64 }, 668206796Spjd { "l2_write_io_in_progress", KSTAT_DATA_UINT64 }, 669206796Spjd { "l2_write_not_cacheable", KSTAT_DATA_UINT64 }, 670206796Spjd { "l2_write_full", KSTAT_DATA_UINT64 }, 671206796Spjd { "l2_write_buffer_iter", KSTAT_DATA_UINT64 }, 672206796Spjd { "l2_write_pios", KSTAT_DATA_UINT64 }, 673206796Spjd { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 }, 674206796Spjd { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 }, 675242845Sdelphij { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 }, 676242845Sdelphij { "memory_throttle_count", KSTAT_DATA_UINT64 }, 677242845Sdelphij { "duplicate_buffers", KSTAT_DATA_UINT64 }, 678242845Sdelphij { "duplicate_buffers_size", KSTAT_DATA_UINT64 }, 679275748Sdelphij { "duplicate_reads", KSTAT_DATA_UINT64 }, 680275748Sdelphij { "arc_meta_used", KSTAT_DATA_UINT64 }, 681275748Sdelphij { "arc_meta_limit", KSTAT_DATA_UINT64 }, 682275780Sdelphij { "arc_meta_max", KSTAT_DATA_UINT64 }, 683275780Sdelphij { "arc_meta_min", KSTAT_DATA_UINT64 } 684168404Spjd}; 685168404Spjd 686168404Spjd#define ARCSTAT(stat) (arc_stats.stat.value.ui64) 687168404Spjd 688168404Spjd#define ARCSTAT_INCR(stat, val) \ 689251631Sdelphij atomic_add_64(&arc_stats.stat.value.ui64, (val)) 690168404Spjd 691206796Spjd#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) 692168404Spjd#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) 693168404Spjd 694168404Spjd#define ARCSTAT_MAX(stat, val) { \ 695168404Spjd uint64_t m; \ 696168404Spjd while ((val) > (m = arc_stats.stat.value.ui64) && \ 697168404Spjd (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 698168404Spjd continue; \ 699168404Spjd} 700168404Spjd 701168404Spjd#define ARCSTAT_MAXSTAT(stat) \ 702168404Spjd ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) 703168404Spjd 704168404Spjd/* 705168404Spjd * We define a macro to allow ARC hits/misses to be easily broken down by 706168404Spjd * two separate conditions, giving a total of four different subtypes for 707168404Spjd * each of hits and misses (so eight statistics total). 708168404Spjd */ 709168404Spjd#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 710168404Spjd if (cond1) { \ 711168404Spjd if (cond2) { \ 712168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 713168404Spjd } else { \ 714168404Spjd ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 715168404Spjd } \ 716168404Spjd } else { \ 717168404Spjd if (cond2) { \ 718168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 719168404Spjd } else { \ 720168404Spjd ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 721168404Spjd } \ 722168404Spjd } 723168404Spjd 724168404Spjdkstat_t *arc_ksp; 725206796Spjdstatic arc_state_t *arc_anon; 726168404Spjdstatic arc_state_t *arc_mru; 727168404Spjdstatic arc_state_t *arc_mru_ghost; 728168404Spjdstatic arc_state_t *arc_mfu; 729168404Spjdstatic arc_state_t *arc_mfu_ghost; 730185029Spjdstatic arc_state_t *arc_l2c_only; 731168404Spjd 732168404Spjd/* 733168404Spjd * There are several ARC variables that are critical to export as kstats -- 734168404Spjd * but we don't want to have to grovel around in the kstat whenever we wish to 735168404Spjd * manipulate them. For these variables, we therefore define them to be in 736168404Spjd * terms of the statistic variable. This assures that we are not introducing 737168404Spjd * the possibility of inconsistency by having shadow copies of the variables, 738168404Spjd * while still allowing the code to be readable. 739168404Spjd */ 740168404Spjd#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ 741168404Spjd#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ 742168404Spjd#define arc_c ARCSTAT(arcstat_c) /* target size of cache */ 743168404Spjd#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ 744168404Spjd#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ 745275748Sdelphij#define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ 746275780Sdelphij#define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ 747275748Sdelphij#define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */ 748275748Sdelphij#define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ 749168404Spjd 750251478Sdelphij#define L2ARC_IS_VALID_COMPRESS(_c_) \ 751251478Sdelphij ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY) 752251478Sdelphij 753168404Spjdstatic int arc_no_grow; /* Don't try to grow cache size */ 754168404Spjdstatic uint64_t arc_tempreserve; 755209962Smmstatic uint64_t arc_loaned_bytes; 756168404Spjd 757168404Spjdtypedef struct arc_callback arc_callback_t; 758168404Spjd 759168404Spjdstruct arc_callback { 760168404Spjd void *acb_private; 761168404Spjd arc_done_func_t *acb_done; 762168404Spjd arc_buf_t *acb_buf; 763168404Spjd zio_t *acb_zio_dummy; 764168404Spjd arc_callback_t *acb_next; 765168404Spjd}; 766168404Spjd 767168404Spjdtypedef struct arc_write_callback arc_write_callback_t; 768168404Spjd 769168404Spjdstruct arc_write_callback { 770168404Spjd void *awcb_private; 771168404Spjd arc_done_func_t *awcb_ready; 772258632Savg arc_done_func_t *awcb_physdone; 773168404Spjd arc_done_func_t *awcb_done; 774168404Spjd arc_buf_t *awcb_buf; 775168404Spjd}; 776168404Spjd 777286570Smav/* 778286570Smav * ARC buffers are separated into multiple structs as a memory saving measure: 779286570Smav * - Common fields struct, always defined, and embedded within it: 780286570Smav * - L2-only fields, always allocated but undefined when not in L2ARC 781286570Smav * - L1-only fields, only allocated when in L1ARC 782286570Smav * 783286570Smav * Buffer in L1 Buffer only in L2 784286570Smav * +------------------------+ +------------------------+ 785286570Smav * | arc_buf_hdr_t | | arc_buf_hdr_t | 786286570Smav * | | | | 787286570Smav * | | | | 788286570Smav * | | | | 789286570Smav * +------------------------+ +------------------------+ 790286570Smav * | l2arc_buf_hdr_t | | l2arc_buf_hdr_t | 791286570Smav * | (undefined if L1-only) | | | 792286570Smav * +------------------------+ +------------------------+ 793286570Smav * | l1arc_buf_hdr_t | 794286570Smav * | | 795286570Smav * | | 796286570Smav * | | 797286570Smav * | | 798286570Smav * +------------------------+ 799286570Smav * 800286570Smav * Because it's possible for the L2ARC to become extremely large, we can wind 801286570Smav * up eating a lot of memory in L2ARC buffer headers, so the size of a header 802286570Smav * is minimized by only allocating the fields necessary for an L1-cached buffer 803286570Smav * when a header is actually in the L1 cache. The sub-headers (l1arc_buf_hdr and 804286570Smav * l2arc_buf_hdr) are embedded rather than allocated separately to save a couple 805286570Smav * words in pointers. arc_hdr_realloc() is used to switch a header between 806286570Smav * these two allocation states. 807286570Smav */ 808286570Smavtypedef struct l1arc_buf_hdr { 809168404Spjd kmutex_t b_freeze_lock; 810286570Smav#ifdef ZFS_DEBUG 811286570Smav /* 812286570Smav * used for debugging wtih kmem_flags - by allocating and freeing 813286570Smav * b_thawed when the buffer is thawed, we get a record of the stack 814286570Smav * trace that thawed it. 815286570Smav */ 816219089Spjd void *b_thawed; 817286570Smav#endif 818168404Spjd 819168404Spjd arc_buf_t *b_buf; 820168404Spjd uint32_t b_datacnt; 821286570Smav /* for waiting on writes to complete */ 822168404Spjd kcondvar_t b_cv; 823168404Spjd 824168404Spjd /* protected by arc state mutex */ 825168404Spjd arc_state_t *b_state; 826286763Smav multilist_node_t b_arc_node; 827168404Spjd 828168404Spjd /* updated atomically */ 829168404Spjd clock_t b_arc_access; 830168404Spjd 831168404Spjd /* self protecting */ 832168404Spjd refcount_t b_refcnt; 833185029Spjd 834286570Smav arc_callback_t *b_acb; 835286570Smav /* temporary buffer holder for in-flight compressed data */ 836286570Smav void *b_tmp_cdata; 837286570Smav} l1arc_buf_hdr_t; 838286570Smav 839286570Smavtypedef struct l2arc_dev l2arc_dev_t; 840286570Smav 841286570Smavtypedef struct l2arc_buf_hdr { 842286570Smav /* protected by arc_buf_hdr mutex */ 843286570Smav l2arc_dev_t *b_dev; /* L2ARC device */ 844286570Smav uint64_t b_daddr; /* disk address, offset byte */ 845286570Smav /* real alloc'd buffer size depending on b_compress applied */ 846286570Smav int32_t b_asize; 847286570Smav 848185029Spjd list_node_t b_l2node; 849286570Smav} l2arc_buf_hdr_t; 850286570Smav 851286570Smavstruct arc_buf_hdr { 852286570Smav /* protected by hash lock */ 853286570Smav dva_t b_dva; 854286570Smav uint64_t b_birth; 855286570Smav /* 856286570Smav * Even though this checksum is only set/verified when a buffer is in 857286570Smav * the L1 cache, it needs to be in the set of common fields because it 858286570Smav * must be preserved from the time before a buffer is written out to 859286570Smav * L2ARC until after it is read back in. 860286570Smav */ 861286570Smav zio_cksum_t *b_freeze_cksum; 862286570Smav 863286570Smav arc_buf_hdr_t *b_hash_next; 864286570Smav arc_flags_t b_flags; 865286570Smav 866286570Smav /* immutable */ 867286570Smav int32_t b_size; 868286570Smav uint64_t b_spa; 869286570Smav 870286570Smav /* L2ARC fields. Undefined when not in L2ARC. */ 871286570Smav l2arc_buf_hdr_t b_l2hdr; 872286570Smav /* L1ARC fields. Undefined when in l2arc_only state */ 873286570Smav l1arc_buf_hdr_t b_l1hdr; 874168404Spjd}; 875168404Spjd 876275748Sdelphij#ifdef _KERNEL 877275748Sdelphijstatic int 878275748Sdelphijsysctl_vfs_zfs_arc_meta_limit(SYSCTL_HANDLER_ARGS) 879275748Sdelphij{ 880275748Sdelphij uint64_t val; 881275748Sdelphij int err; 882275748Sdelphij 883275748Sdelphij val = arc_meta_limit; 884275748Sdelphij err = sysctl_handle_64(oidp, &val, 0, req); 885275748Sdelphij if (err != 0 || req->newptr == NULL) 886275748Sdelphij return (err); 887275748Sdelphij 888275748Sdelphij if (val <= 0 || val > arc_c_max) 889275748Sdelphij return (EINVAL); 890275748Sdelphij 891275748Sdelphij arc_meta_limit = val; 892275748Sdelphij return (0); 893275748Sdelphij} 894275748Sdelphij#endif 895275748Sdelphij 896168404Spjdstatic arc_buf_t *arc_eviction_list; 897168404Spjdstatic arc_buf_hdr_t arc_eviction_hdr; 898168404Spjd 899168404Spjd#define GHOST_STATE(state) \ 900185029Spjd ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 901185029Spjd (state) == arc_l2c_only) 902168404Spjd 903275811Sdelphij#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) 904275811Sdelphij#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) 905275811Sdelphij#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) 906275811Sdelphij#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) 907275811Sdelphij#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FLAG_FREED_IN_READ) 908275811Sdelphij#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_FLAG_BUF_AVAILABLE) 909286570Smav 910275811Sdelphij#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) 911286570Smav#define HDR_L2COMPRESS(hdr) ((hdr)->b_flags & ARC_FLAG_L2COMPRESS) 912275811Sdelphij#define HDR_L2_READING(hdr) \ 913286570Smav (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ 914286570Smav ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) 915275811Sdelphij#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) 916275811Sdelphij#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) 917275811Sdelphij#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) 918168404Spjd 919286570Smav#define HDR_ISTYPE_METADATA(hdr) \ 920286570Smav ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) 921286570Smav#define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) 922286570Smav 923286570Smav#define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) 924286570Smav#define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) 925286570Smav 926286570Smav/* For storing compression mode in b_flags */ 927286570Smav#define HDR_COMPRESS_OFFSET 24 928286570Smav#define HDR_COMPRESS_NBITS 7 929286570Smav 930286570Smav#define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET(hdr->b_flags, \ 931286570Smav HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS)) 932286570Smav#define HDR_SET_COMPRESS(hdr, cmp) BF32_SET(hdr->b_flags, \ 933286570Smav HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS, (cmp)) 934286570Smav 935168404Spjd/* 936185029Spjd * Other sizes 937185029Spjd */ 938185029Spjd 939286570Smav#define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 940286570Smav#define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) 941185029Spjd 942185029Spjd/* 943168404Spjd * Hash table routines 944168404Spjd */ 945168404Spjd 946205253Skmacy#define HT_LOCK_PAD CACHE_LINE_SIZE 947168404Spjd 948168404Spjdstruct ht_lock { 949168404Spjd kmutex_t ht_lock; 950168404Spjd#ifdef _KERNEL 951168404Spjd unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 952168404Spjd#endif 953168404Spjd}; 954168404Spjd 955168404Spjd#define BUF_LOCKS 256 956168404Spjdtypedef struct buf_hash_table { 957168404Spjd uint64_t ht_mask; 958168404Spjd arc_buf_hdr_t **ht_table; 959205264Skmacy struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE); 960168404Spjd} buf_hash_table_t; 961168404Spjd 962168404Spjdstatic buf_hash_table_t buf_hash_table; 963168404Spjd 964168404Spjd#define BUF_HASH_INDEX(spa, dva, birth) \ 965168404Spjd (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 966168404Spjd#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 967168404Spjd#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 968219089Spjd#define HDR_LOCK(hdr) \ 969219089Spjd (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 970168404Spjd 971168404Spjduint64_t zfs_crc64_table[256]; 972168404Spjd 973185029Spjd/* 974185029Spjd * Level 2 ARC 975185029Spjd */ 976185029Spjd 977272707Savg#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 978251478Sdelphij#define L2ARC_HEADROOM 2 /* num of writes */ 979251478Sdelphij/* 980251478Sdelphij * If we discover during ARC scan any buffers to be compressed, we boost 981251478Sdelphij * our headroom for the next scanning cycle by this percentage multiple. 982251478Sdelphij */ 983251478Sdelphij#define L2ARC_HEADROOM_BOOST 200 984208373Smm#define L2ARC_FEED_SECS 1 /* caching interval secs */ 985208373Smm#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 986185029Spjd 987286598Smav/* 988286598Smav * Used to distinguish headers that are being process by 989286598Smav * l2arc_write_buffers(), but have yet to be assigned to a l2arc disk 990286598Smav * address. This can happen when the header is added to the l2arc's list 991286598Smav * of buffers to write in the first stage of l2arc_write_buffers(), but 992286598Smav * has not yet been written out which happens in the second stage of 993286598Smav * l2arc_write_buffers(). 994286598Smav */ 995286598Smav#define L2ARC_ADDR_UNSET ((uint64_t)(-1)) 996286598Smav 997185029Spjd#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) 998185029Spjd#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) 999185029Spjd 1000251631Sdelphij/* L2ARC Performance Tunables */ 1001185029Spjduint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */ 1002185029Spjduint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */ 1003185029Spjduint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */ 1004251478Sdelphijuint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 1005185029Spjduint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 1006208373Smmuint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */ 1007219089Spjdboolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 1008208373Smmboolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */ 1009208373Smmboolean_t l2arc_norw = B_TRUE; /* no reads during writes */ 1010185029Spjd 1011217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW, 1012205231Skmacy &l2arc_write_max, 0, "max write size"); 1013217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW, 1014205231Skmacy &l2arc_write_boost, 0, "extra write during warmup"); 1015217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW, 1016205231Skmacy &l2arc_headroom, 0, "number of dev writes"); 1017217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW, 1018205231Skmacy &l2arc_feed_secs, 0, "interval seconds"); 1019217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW, 1020208373Smm &l2arc_feed_min_ms, 0, "min interval milliseconds"); 1021205231Skmacy 1022205231SkmacySYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW, 1023205231Skmacy &l2arc_noprefetch, 0, "don't cache prefetch bufs"); 1024208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW, 1025208373Smm &l2arc_feed_again, 0, "turbo warmup"); 1026208373SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW, 1027208373Smm &l2arc_norw, 0, "no reads during writes"); 1028205231Skmacy 1029217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD, 1030286770Smav &ARC_anon.arcs_size.rc_count, 0, "size of anonymous state"); 1031217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD, 1032205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state"); 1033217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD, 1034205231Skmacy &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state"); 1035205231Skmacy 1036217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD, 1037286770Smav &ARC_mru.arcs_size.rc_count, 0, "size of mru state"); 1038217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD, 1039205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state"); 1040217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD, 1041205231Skmacy &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state"); 1042205231Skmacy 1043217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD, 1044286770Smav &ARC_mru_ghost.arcs_size.rc_count, 0, "size of mru ghost state"); 1045217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD, 1046205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 1047205231Skmacy "size of metadata in mru ghost state"); 1048217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD, 1049205231Skmacy &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 1050205231Skmacy "size of data in mru ghost state"); 1051205231Skmacy 1052217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD, 1053286770Smav &ARC_mfu.arcs_size.rc_count, 0, "size of mfu state"); 1054217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD, 1055205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state"); 1056217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD, 1057205231Skmacy &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state"); 1058205231Skmacy 1059217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD, 1060286770Smav &ARC_mfu_ghost.arcs_size.rc_count, 0, "size of mfu ghost state"); 1061217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD, 1062205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0, 1063205231Skmacy "size of metadata in mfu ghost state"); 1064217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD, 1065205231Skmacy &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0, 1066205231Skmacy "size of data in mfu ghost state"); 1067205231Skmacy 1068217367SmdfSYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD, 1069286770Smav &ARC_l2c_only.arcs_size.rc_count, 0, "size of mru state"); 1070205231Skmacy 1071185029Spjd/* 1072185029Spjd * L2ARC Internals 1073185029Spjd */ 1074286570Smavstruct l2arc_dev { 1075185029Spjd vdev_t *l2ad_vdev; /* vdev */ 1076185029Spjd spa_t *l2ad_spa; /* spa */ 1077185029Spjd uint64_t l2ad_hand; /* next write location */ 1078185029Spjd uint64_t l2ad_start; /* first addr on device */ 1079185029Spjd uint64_t l2ad_end; /* last addr on device */ 1080185029Spjd boolean_t l2ad_first; /* first sweep through */ 1081208373Smm boolean_t l2ad_writing; /* currently writing */ 1082286570Smav kmutex_t l2ad_mtx; /* lock for buffer list */ 1083286570Smav list_t l2ad_buflist; /* buffer list */ 1084185029Spjd list_node_t l2ad_node; /* device list node */ 1085286598Smav refcount_t l2ad_alloc; /* allocated bytes */ 1086286570Smav}; 1087185029Spjd 1088185029Spjdstatic list_t L2ARC_dev_list; /* device list */ 1089185029Spjdstatic list_t *l2arc_dev_list; /* device list pointer */ 1090185029Spjdstatic kmutex_t l2arc_dev_mtx; /* device list mutex */ 1091185029Spjdstatic l2arc_dev_t *l2arc_dev_last; /* last device used */ 1092185029Spjdstatic list_t L2ARC_free_on_write; /* free after write buf list */ 1093185029Spjdstatic list_t *l2arc_free_on_write; /* free after write list ptr */ 1094185029Spjdstatic kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 1095185029Spjdstatic uint64_t l2arc_ndev; /* number of devices */ 1096185029Spjd 1097185029Spjdtypedef struct l2arc_read_callback { 1098251478Sdelphij arc_buf_t *l2rcb_buf; /* read buffer */ 1099251478Sdelphij spa_t *l2rcb_spa; /* spa */ 1100251478Sdelphij blkptr_t l2rcb_bp; /* original blkptr */ 1101268123Sdelphij zbookmark_phys_t l2rcb_zb; /* original bookmark */ 1102251478Sdelphij int l2rcb_flags; /* original flags */ 1103251478Sdelphij enum zio_compress l2rcb_compress; /* applied compress */ 1104185029Spjd} l2arc_read_callback_t; 1105185029Spjd 1106185029Spjdtypedef struct l2arc_write_callback { 1107185029Spjd l2arc_dev_t *l2wcb_dev; /* device info */ 1108185029Spjd arc_buf_hdr_t *l2wcb_head; /* head of write buflist */ 1109185029Spjd} l2arc_write_callback_t; 1110185029Spjd 1111185029Spjdtypedef struct l2arc_data_free { 1112185029Spjd /* protected by l2arc_free_on_write_mtx */ 1113185029Spjd void *l2df_data; 1114185029Spjd size_t l2df_size; 1115185029Spjd void (*l2df_func)(void *, size_t); 1116185029Spjd list_node_t l2df_list_node; 1117185029Spjd} l2arc_data_free_t; 1118185029Spjd 1119185029Spjdstatic kmutex_t l2arc_feed_thr_lock; 1120185029Spjdstatic kcondvar_t l2arc_feed_thr_cv; 1121185029Spjdstatic uint8_t l2arc_thread_exit; 1122185029Spjd 1123275811Sdelphijstatic void arc_get_data_buf(arc_buf_t *); 1124275811Sdelphijstatic void arc_access(arc_buf_hdr_t *, kmutex_t *); 1125286763Smavstatic boolean_t arc_is_overflowing(); 1126275811Sdelphijstatic void arc_buf_watch(arc_buf_t *); 1127275811Sdelphij 1128286570Smavstatic arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); 1129286570Smavstatic uint32_t arc_bufc_to_flags(arc_buf_contents_t); 1130286570Smav 1131275811Sdelphijstatic boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); 1132275811Sdelphijstatic void l2arc_read_done(zio_t *); 1133185029Spjd 1134286570Smavstatic boolean_t l2arc_compress_buf(arc_buf_hdr_t *); 1135275811Sdelphijstatic void l2arc_decompress_zio(zio_t *, arc_buf_hdr_t *, enum zio_compress); 1136275811Sdelphijstatic void l2arc_release_cdata_buf(arc_buf_hdr_t *); 1137251478Sdelphij 1138168404Spjdstatic uint64_t 1139209962Smmbuf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 1140168404Spjd{ 1141168404Spjd uint8_t *vdva = (uint8_t *)dva; 1142168404Spjd uint64_t crc = -1ULL; 1143168404Spjd int i; 1144168404Spjd 1145168404Spjd ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 1146168404Spjd 1147168404Spjd for (i = 0; i < sizeof (dva_t); i++) 1148168404Spjd crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 1149168404Spjd 1150209962Smm crc ^= (spa>>8) ^ birth; 1151168404Spjd 1152168404Spjd return (crc); 1153168404Spjd} 1154168404Spjd 1155168404Spjd#define BUF_EMPTY(buf) \ 1156168404Spjd ((buf)->b_dva.dva_word[0] == 0 && \ 1157286570Smav (buf)->b_dva.dva_word[1] == 0) 1158168404Spjd 1159168404Spjd#define BUF_EQUAL(spa, dva, birth, buf) \ 1160168404Spjd ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 1161168404Spjd ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 1162168404Spjd ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 1163168404Spjd 1164219089Spjdstatic void 1165219089Spjdbuf_discard_identity(arc_buf_hdr_t *hdr) 1166219089Spjd{ 1167219089Spjd hdr->b_dva.dva_word[0] = 0; 1168219089Spjd hdr->b_dva.dva_word[1] = 0; 1169219089Spjd hdr->b_birth = 0; 1170219089Spjd} 1171219089Spjd 1172168404Spjdstatic arc_buf_hdr_t * 1173268075Sdelphijbuf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 1174168404Spjd{ 1175268075Sdelphij const dva_t *dva = BP_IDENTITY(bp); 1176268075Sdelphij uint64_t birth = BP_PHYSICAL_BIRTH(bp); 1177168404Spjd uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 1178168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1179275811Sdelphij arc_buf_hdr_t *hdr; 1180168404Spjd 1181168404Spjd mutex_enter(hash_lock); 1182275811Sdelphij for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; 1183275811Sdelphij hdr = hdr->b_hash_next) { 1184275811Sdelphij if (BUF_EQUAL(spa, dva, birth, hdr)) { 1185168404Spjd *lockp = hash_lock; 1186275811Sdelphij return (hdr); 1187168404Spjd } 1188168404Spjd } 1189168404Spjd mutex_exit(hash_lock); 1190168404Spjd *lockp = NULL; 1191168404Spjd return (NULL); 1192168404Spjd} 1193168404Spjd 1194168404Spjd/* 1195168404Spjd * Insert an entry into the hash table. If there is already an element 1196168404Spjd * equal to elem in the hash table, then the already existing element 1197168404Spjd * will be returned and the new element will not be inserted. 1198168404Spjd * Otherwise returns NULL. 1199286570Smav * If lockp == NULL, the caller is assumed to already hold the hash lock. 1200168404Spjd */ 1201168404Spjdstatic arc_buf_hdr_t * 1202275811Sdelphijbuf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) 1203168404Spjd{ 1204275811Sdelphij uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1205168404Spjd kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1206275811Sdelphij arc_buf_hdr_t *fhdr; 1207168404Spjd uint32_t i; 1208168404Spjd 1209275811Sdelphij ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); 1210275811Sdelphij ASSERT(hdr->b_birth != 0); 1211275811Sdelphij ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1212286570Smav 1213286570Smav if (lockp != NULL) { 1214286570Smav *lockp = hash_lock; 1215286570Smav mutex_enter(hash_lock); 1216286570Smav } else { 1217286570Smav ASSERT(MUTEX_HELD(hash_lock)); 1218286570Smav } 1219286570Smav 1220275811Sdelphij for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; 1221275811Sdelphij fhdr = fhdr->b_hash_next, i++) { 1222275811Sdelphij if (BUF_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) 1223275811Sdelphij return (fhdr); 1224168404Spjd } 1225168404Spjd 1226275811Sdelphij hdr->b_hash_next = buf_hash_table.ht_table[idx]; 1227275811Sdelphij buf_hash_table.ht_table[idx] = hdr; 1228275811Sdelphij hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; 1229168404Spjd 1230168404Spjd /* collect some hash table performance data */ 1231168404Spjd if (i > 0) { 1232168404Spjd ARCSTAT_BUMP(arcstat_hash_collisions); 1233168404Spjd if (i == 1) 1234168404Spjd ARCSTAT_BUMP(arcstat_hash_chains); 1235168404Spjd 1236168404Spjd ARCSTAT_MAX(arcstat_hash_chain_max, i); 1237168404Spjd } 1238168404Spjd 1239168404Spjd ARCSTAT_BUMP(arcstat_hash_elements); 1240168404Spjd ARCSTAT_MAXSTAT(arcstat_hash_elements); 1241168404Spjd 1242168404Spjd return (NULL); 1243168404Spjd} 1244168404Spjd 1245168404Spjdstatic void 1246275811Sdelphijbuf_hash_remove(arc_buf_hdr_t *hdr) 1247168404Spjd{ 1248275811Sdelphij arc_buf_hdr_t *fhdr, **hdrp; 1249275811Sdelphij uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1250168404Spjd 1251168404Spjd ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 1252275811Sdelphij ASSERT(HDR_IN_HASH_TABLE(hdr)); 1253168404Spjd 1254275811Sdelphij hdrp = &buf_hash_table.ht_table[idx]; 1255275811Sdelphij while ((fhdr = *hdrp) != hdr) { 1256275811Sdelphij ASSERT(fhdr != NULL); 1257275811Sdelphij hdrp = &fhdr->b_hash_next; 1258168404Spjd } 1259275811Sdelphij *hdrp = hdr->b_hash_next; 1260275811Sdelphij hdr->b_hash_next = NULL; 1261275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IN_HASH_TABLE; 1262168404Spjd 1263168404Spjd /* collect some hash table performance data */ 1264168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_elements); 1265168404Spjd 1266168404Spjd if (buf_hash_table.ht_table[idx] && 1267168404Spjd buf_hash_table.ht_table[idx]->b_hash_next == NULL) 1268168404Spjd ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1269168404Spjd} 1270168404Spjd 1271168404Spjd/* 1272168404Spjd * Global data structures and functions for the buf kmem cache. 1273168404Spjd */ 1274286570Smavstatic kmem_cache_t *hdr_full_cache; 1275286570Smavstatic kmem_cache_t *hdr_l2only_cache; 1276168404Spjdstatic kmem_cache_t *buf_cache; 1277168404Spjd 1278168404Spjdstatic void 1279168404Spjdbuf_fini(void) 1280168404Spjd{ 1281168404Spjd int i; 1282168404Spjd 1283168404Spjd kmem_free(buf_hash_table.ht_table, 1284168404Spjd (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1285168404Spjd for (i = 0; i < BUF_LOCKS; i++) 1286168404Spjd mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 1287286570Smav kmem_cache_destroy(hdr_full_cache); 1288286570Smav kmem_cache_destroy(hdr_l2only_cache); 1289168404Spjd kmem_cache_destroy(buf_cache); 1290168404Spjd} 1291168404Spjd 1292168404Spjd/* 1293168404Spjd * Constructor callback - called when the cache is empty 1294168404Spjd * and a new buf is requested. 1295168404Spjd */ 1296168404Spjd/* ARGSUSED */ 1297168404Spjdstatic int 1298286570Smavhdr_full_cons(void *vbuf, void *unused, int kmflag) 1299168404Spjd{ 1300275811Sdelphij arc_buf_hdr_t *hdr = vbuf; 1301168404Spjd 1302286570Smav bzero(hdr, HDR_FULL_SIZE); 1303286570Smav cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); 1304286570Smav refcount_create(&hdr->b_l1hdr.b_refcnt); 1305286570Smav mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1306286763Smav multilist_link_init(&hdr->b_l1hdr.b_arc_node); 1307286570Smav arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1308185029Spjd 1309168404Spjd return (0); 1310168404Spjd} 1311168404Spjd 1312185029Spjd/* ARGSUSED */ 1313185029Spjdstatic int 1314286570Smavhdr_l2only_cons(void *vbuf, void *unused, int kmflag) 1315286570Smav{ 1316286570Smav arc_buf_hdr_t *hdr = vbuf; 1317286570Smav 1318286570Smav bzero(hdr, HDR_L2ONLY_SIZE); 1319286570Smav arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1320286570Smav 1321286570Smav return (0); 1322286570Smav} 1323286570Smav 1324286570Smav/* ARGSUSED */ 1325286570Smavstatic int 1326185029Spjdbuf_cons(void *vbuf, void *unused, int kmflag) 1327185029Spjd{ 1328185029Spjd arc_buf_t *buf = vbuf; 1329185029Spjd 1330185029Spjd bzero(buf, sizeof (arc_buf_t)); 1331219089Spjd mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); 1332208373Smm arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1333208373Smm 1334185029Spjd return (0); 1335185029Spjd} 1336185029Spjd 1337168404Spjd/* 1338168404Spjd * Destructor callback - called when a cached buf is 1339168404Spjd * no longer required. 1340168404Spjd */ 1341168404Spjd/* ARGSUSED */ 1342168404Spjdstatic void 1343286570Smavhdr_full_dest(void *vbuf, void *unused) 1344168404Spjd{ 1345275811Sdelphij arc_buf_hdr_t *hdr = vbuf; 1346168404Spjd 1347275811Sdelphij ASSERT(BUF_EMPTY(hdr)); 1348286570Smav cv_destroy(&hdr->b_l1hdr.b_cv); 1349286570Smav refcount_destroy(&hdr->b_l1hdr.b_refcnt); 1350286570Smav mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); 1351286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 1352286570Smav arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1353168404Spjd} 1354168404Spjd 1355185029Spjd/* ARGSUSED */ 1356185029Spjdstatic void 1357286570Smavhdr_l2only_dest(void *vbuf, void *unused) 1358286570Smav{ 1359286570Smav arc_buf_hdr_t *hdr = vbuf; 1360286570Smav 1361286570Smav ASSERT(BUF_EMPTY(hdr)); 1362286570Smav arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1363286570Smav} 1364286570Smav 1365286570Smav/* ARGSUSED */ 1366286570Smavstatic void 1367185029Spjdbuf_dest(void *vbuf, void *unused) 1368185029Spjd{ 1369185029Spjd arc_buf_t *buf = vbuf; 1370185029Spjd 1371219089Spjd mutex_destroy(&buf->b_evict_lock); 1372208373Smm arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1373185029Spjd} 1374185029Spjd 1375168404Spjd/* 1376168404Spjd * Reclaim callback -- invoked when memory is low. 1377168404Spjd */ 1378168404Spjd/* ARGSUSED */ 1379168404Spjdstatic void 1380168404Spjdhdr_recl(void *unused) 1381168404Spjd{ 1382168404Spjd dprintf("hdr_recl called\n"); 1383168404Spjd /* 1384168404Spjd * umem calls the reclaim func when we destroy the buf cache, 1385168404Spjd * which is after we do arc_fini(). 1386168404Spjd */ 1387168404Spjd if (!arc_dead) 1388286763Smav cv_signal(&arc_reclaim_thread_cv); 1389168404Spjd} 1390168404Spjd 1391168404Spjdstatic void 1392168404Spjdbuf_init(void) 1393168404Spjd{ 1394168404Spjd uint64_t *ct; 1395168404Spjd uint64_t hsize = 1ULL << 12; 1396168404Spjd int i, j; 1397168404Spjd 1398168404Spjd /* 1399168404Spjd * The hash table is big enough to fill all of physical memory 1400269230Sdelphij * with an average block size of zfs_arc_average_blocksize (default 8K). 1401269230Sdelphij * By default, the table will take up 1402269230Sdelphij * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1403168404Spjd */ 1404269230Sdelphij while (hsize * zfs_arc_average_blocksize < (uint64_t)physmem * PAGESIZE) 1405168404Spjd hsize <<= 1; 1406168404Spjdretry: 1407168404Spjd buf_hash_table.ht_mask = hsize - 1; 1408168404Spjd buf_hash_table.ht_table = 1409168404Spjd kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1410168404Spjd if (buf_hash_table.ht_table == NULL) { 1411168404Spjd ASSERT(hsize > (1ULL << 8)); 1412168404Spjd hsize >>= 1; 1413168404Spjd goto retry; 1414168404Spjd } 1415168404Spjd 1416286570Smav hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, 1417286570Smav 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0); 1418286570Smav hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", 1419286570Smav HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl, 1420286570Smav NULL, NULL, 0); 1421168404Spjd buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1422185029Spjd 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1423168404Spjd 1424168404Spjd for (i = 0; i < 256; i++) 1425168404Spjd for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1426168404Spjd *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1427168404Spjd 1428168404Spjd for (i = 0; i < BUF_LOCKS; i++) { 1429168404Spjd mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 1430168404Spjd NULL, MUTEX_DEFAULT, NULL); 1431168404Spjd } 1432168404Spjd} 1433168404Spjd 1434286570Smav/* 1435286570Smav * Transition between the two allocation states for the arc_buf_hdr struct. 1436286570Smav * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without 1437286570Smav * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller 1438286570Smav * version is used when a cache buffer is only in the L2ARC in order to reduce 1439286570Smav * memory usage. 1440286570Smav */ 1441286570Smavstatic arc_buf_hdr_t * 1442286570Smavarc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) 1443286570Smav{ 1444286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 1445286570Smav 1446286570Smav arc_buf_hdr_t *nhdr; 1447286570Smav l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 1448286570Smav 1449286570Smav ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || 1450286570Smav (old == hdr_l2only_cache && new == hdr_full_cache)); 1451286570Smav 1452286570Smav nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); 1453286570Smav 1454286570Smav ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 1455286570Smav buf_hash_remove(hdr); 1456286570Smav 1457286570Smav bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); 1458286598Smav 1459286570Smav if (new == hdr_full_cache) { 1460286570Smav nhdr->b_flags |= ARC_FLAG_HAS_L1HDR; 1461286570Smav /* 1462286570Smav * arc_access and arc_change_state need to be aware that a 1463286570Smav * header has just come out of L2ARC, so we set its state to 1464286570Smav * l2c_only even though it's about to change. 1465286570Smav */ 1466286570Smav nhdr->b_l1hdr.b_state = arc_l2c_only; 1467286763Smav 1468286763Smav /* Verify previous threads set to NULL before freeing */ 1469286763Smav ASSERT3P(nhdr->b_l1hdr.b_tmp_cdata, ==, NULL); 1470286570Smav } else { 1471286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 1472286570Smav ASSERT0(hdr->b_l1hdr.b_datacnt); 1473286763Smav 1474286570Smav /* 1475286763Smav * If we've reached here, We must have been called from 1476286763Smav * arc_evict_hdr(), as such we should have already been 1477286763Smav * removed from any ghost list we were previously on 1478286763Smav * (which protects us from racing with arc_evict_state), 1479286763Smav * thus no locking is needed during this check. 1480286570Smav */ 1481286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 1482286763Smav 1483286763Smav /* 1484286763Smav * A buffer must not be moved into the arc_l2c_only 1485286763Smav * state if it's not finished being written out to the 1486286763Smav * l2arc device. Otherwise, the b_l1hdr.b_tmp_cdata field 1487286763Smav * might try to be accessed, even though it was removed. 1488286763Smav */ 1489286763Smav VERIFY(!HDR_L2_WRITING(hdr)); 1490286763Smav VERIFY3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 1491286763Smav 1492286570Smav nhdr->b_flags &= ~ARC_FLAG_HAS_L1HDR; 1493286570Smav } 1494286570Smav /* 1495286570Smav * The header has been reallocated so we need to re-insert it into any 1496286570Smav * lists it was on. 1497286570Smav */ 1498286570Smav (void) buf_hash_insert(nhdr, NULL); 1499286570Smav 1500286570Smav ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); 1501286570Smav 1502286570Smav mutex_enter(&dev->l2ad_mtx); 1503286570Smav 1504286570Smav /* 1505286570Smav * We must place the realloc'ed header back into the list at 1506286570Smav * the same spot. Otherwise, if it's placed earlier in the list, 1507286570Smav * l2arc_write_buffers() could find it during the function's 1508286570Smav * write phase, and try to write it out to the l2arc. 1509286570Smav */ 1510286570Smav list_insert_after(&dev->l2ad_buflist, hdr, nhdr); 1511286570Smav list_remove(&dev->l2ad_buflist, hdr); 1512286570Smav 1513286570Smav mutex_exit(&dev->l2ad_mtx); 1514286570Smav 1515286598Smav /* 1516286598Smav * Since we're using the pointer address as the tag when 1517286598Smav * incrementing and decrementing the l2ad_alloc refcount, we 1518286598Smav * must remove the old pointer (that we're about to destroy) and 1519286598Smav * add the new pointer to the refcount. Otherwise we'd remove 1520286598Smav * the wrong pointer address when calling arc_hdr_destroy() later. 1521286598Smav */ 1522286598Smav 1523286598Smav (void) refcount_remove_many(&dev->l2ad_alloc, 1524286598Smav hdr->b_l2hdr.b_asize, hdr); 1525286598Smav 1526286598Smav (void) refcount_add_many(&dev->l2ad_alloc, 1527286598Smav nhdr->b_l2hdr.b_asize, nhdr); 1528286598Smav 1529286570Smav buf_discard_identity(hdr); 1530286570Smav hdr->b_freeze_cksum = NULL; 1531286570Smav kmem_cache_free(old, hdr); 1532286570Smav 1533286570Smav return (nhdr); 1534286570Smav} 1535286570Smav 1536286570Smav 1537168404Spjd#define ARC_MINTIME (hz>>4) /* 62 ms */ 1538168404Spjd 1539168404Spjdstatic void 1540168404Spjdarc_cksum_verify(arc_buf_t *buf) 1541168404Spjd{ 1542168404Spjd zio_cksum_t zc; 1543168404Spjd 1544168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1545168404Spjd return; 1546168404Spjd 1547286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1548286570Smav if (buf->b_hdr->b_freeze_cksum == NULL || HDR_IO_ERROR(buf->b_hdr)) { 1549286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1550168404Spjd return; 1551168404Spjd } 1552168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1553168404Spjd if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) 1554168404Spjd panic("buffer modified while frozen!"); 1555286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1556168404Spjd} 1557168404Spjd 1558185029Spjdstatic int 1559185029Spjdarc_cksum_equal(arc_buf_t *buf) 1560185029Spjd{ 1561185029Spjd zio_cksum_t zc; 1562185029Spjd int equal; 1563185029Spjd 1564286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1565185029Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); 1566185029Spjd equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); 1567286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1568185029Spjd 1569185029Spjd return (equal); 1570185029Spjd} 1571185029Spjd 1572168404Spjdstatic void 1573185029Spjdarc_cksum_compute(arc_buf_t *buf, boolean_t force) 1574168404Spjd{ 1575185029Spjd if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) 1576168404Spjd return; 1577168404Spjd 1578286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1579168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1580286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1581168404Spjd return; 1582168404Spjd } 1583168404Spjd buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP); 1584168404Spjd fletcher_2_native(buf->b_data, buf->b_hdr->b_size, 1585168404Spjd buf->b_hdr->b_freeze_cksum); 1586286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1587240133Smm#ifdef illumos 1588240133Smm arc_buf_watch(buf); 1589277300Ssmh#endif 1590168404Spjd} 1591168404Spjd 1592240133Smm#ifdef illumos 1593240133Smm#ifndef _KERNEL 1594240133Smmtypedef struct procctl { 1595240133Smm long cmd; 1596240133Smm prwatch_t prwatch; 1597240133Smm} procctl_t; 1598240133Smm#endif 1599240133Smm 1600240133Smm/* ARGSUSED */ 1601240133Smmstatic void 1602240133Smmarc_buf_unwatch(arc_buf_t *buf) 1603240133Smm{ 1604240133Smm#ifndef _KERNEL 1605240133Smm if (arc_watch) { 1606240133Smm int result; 1607240133Smm procctl_t ctl; 1608240133Smm ctl.cmd = PCWATCH; 1609240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1610240133Smm ctl.prwatch.pr_size = 0; 1611240133Smm ctl.prwatch.pr_wflags = 0; 1612240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1613240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1614240133Smm } 1615240133Smm#endif 1616240133Smm} 1617240133Smm 1618240133Smm/* ARGSUSED */ 1619240133Smmstatic void 1620240133Smmarc_buf_watch(arc_buf_t *buf) 1621240133Smm{ 1622240133Smm#ifndef _KERNEL 1623240133Smm if (arc_watch) { 1624240133Smm int result; 1625240133Smm procctl_t ctl; 1626240133Smm ctl.cmd = PCWATCH; 1627240133Smm ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data; 1628240133Smm ctl.prwatch.pr_size = buf->b_hdr->b_size; 1629240133Smm ctl.prwatch.pr_wflags = WA_WRITE; 1630240133Smm result = write(arc_procfd, &ctl, sizeof (ctl)); 1631240133Smm ASSERT3U(result, ==, sizeof (ctl)); 1632240133Smm } 1633240133Smm#endif 1634240133Smm} 1635240133Smm#endif /* illumos */ 1636240133Smm 1637286570Smavstatic arc_buf_contents_t 1638286570Smavarc_buf_type(arc_buf_hdr_t *hdr) 1639286570Smav{ 1640286570Smav if (HDR_ISTYPE_METADATA(hdr)) { 1641286570Smav return (ARC_BUFC_METADATA); 1642286570Smav } else { 1643286570Smav return (ARC_BUFC_DATA); 1644286570Smav } 1645286570Smav} 1646286570Smav 1647286570Smavstatic uint32_t 1648286570Smavarc_bufc_to_flags(arc_buf_contents_t type) 1649286570Smav{ 1650286570Smav switch (type) { 1651286570Smav case ARC_BUFC_DATA: 1652286570Smav /* metadata field is 0 if buffer contains normal data */ 1653286570Smav return (0); 1654286570Smav case ARC_BUFC_METADATA: 1655286570Smav return (ARC_FLAG_BUFC_METADATA); 1656286570Smav default: 1657286570Smav break; 1658286570Smav } 1659286570Smav panic("undefined ARC buffer type!"); 1660286570Smav return ((uint32_t)-1); 1661286570Smav} 1662286570Smav 1663168404Spjdvoid 1664168404Spjdarc_buf_thaw(arc_buf_t *buf) 1665168404Spjd{ 1666185029Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1667286570Smav if (buf->b_hdr->b_l1hdr.b_state != arc_anon) 1668185029Spjd panic("modifying non-anon buffer!"); 1669286570Smav if (HDR_IO_IN_PROGRESS(buf->b_hdr)) 1670185029Spjd panic("modifying buffer while i/o in progress!"); 1671185029Spjd arc_cksum_verify(buf); 1672185029Spjd } 1673168404Spjd 1674286570Smav mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1675168404Spjd if (buf->b_hdr->b_freeze_cksum != NULL) { 1676168404Spjd kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 1677168404Spjd buf->b_hdr->b_freeze_cksum = NULL; 1678168404Spjd } 1679219089Spjd 1680286570Smav#ifdef ZFS_DEBUG 1681219089Spjd if (zfs_flags & ZFS_DEBUG_MODIFY) { 1682286570Smav if (buf->b_hdr->b_l1hdr.b_thawed != NULL) 1683286570Smav kmem_free(buf->b_hdr->b_l1hdr.b_thawed, 1); 1684286570Smav buf->b_hdr->b_l1hdr.b_thawed = kmem_alloc(1, KM_SLEEP); 1685219089Spjd } 1686286570Smav#endif 1687219089Spjd 1688286570Smav mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); 1689240133Smm 1690240133Smm#ifdef illumos 1691240133Smm arc_buf_unwatch(buf); 1692277300Ssmh#endif 1693168404Spjd} 1694168404Spjd 1695168404Spjdvoid 1696168404Spjdarc_buf_freeze(arc_buf_t *buf) 1697168404Spjd{ 1698219089Spjd kmutex_t *hash_lock; 1699219089Spjd 1700168404Spjd if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1701168404Spjd return; 1702168404Spjd 1703219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 1704219089Spjd mutex_enter(hash_lock); 1705219089Spjd 1706168404Spjd ASSERT(buf->b_hdr->b_freeze_cksum != NULL || 1707286570Smav buf->b_hdr->b_l1hdr.b_state == arc_anon); 1708185029Spjd arc_cksum_compute(buf, B_FALSE); 1709219089Spjd mutex_exit(hash_lock); 1710240133Smm 1711168404Spjd} 1712168404Spjd 1713168404Spjdstatic void 1714275811Sdelphijadd_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1715168404Spjd{ 1716286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1717168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1718286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 1719168404Spjd 1720286570Smav if ((refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && 1721286570Smav (state != arc_anon)) { 1722286570Smav /* We don't use the L2-only state list. */ 1723286570Smav if (state != arc_l2c_only) { 1724286763Smav arc_buf_contents_t type = arc_buf_type(hdr); 1725286570Smav uint64_t delta = hdr->b_size * hdr->b_l1hdr.b_datacnt; 1726286763Smav multilist_t *list = &state->arcs_list[type]; 1727286763Smav uint64_t *size = &state->arcs_lsize[type]; 1728168404Spjd 1729286763Smav multilist_remove(list, hdr); 1730286763Smav 1731286570Smav if (GHOST_STATE(state)) { 1732286570Smav ASSERT0(hdr->b_l1hdr.b_datacnt); 1733286570Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 1734286570Smav delta = hdr->b_size; 1735286570Smav } 1736286570Smav ASSERT(delta > 0); 1737286570Smav ASSERT3U(*size, >=, delta); 1738286570Smav atomic_add_64(size, -delta); 1739168404Spjd } 1740185029Spjd /* remove the prefetch flag if we get a reference */ 1741286570Smav hdr->b_flags &= ~ARC_FLAG_PREFETCH; 1742168404Spjd } 1743168404Spjd} 1744168404Spjd 1745168404Spjdstatic int 1746275811Sdelphijremove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) 1747168404Spjd{ 1748168404Spjd int cnt; 1749286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 1750168404Spjd 1751286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1752168404Spjd ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); 1753168404Spjd ASSERT(!GHOST_STATE(state)); 1754168404Spjd 1755286570Smav /* 1756286570Smav * arc_l2c_only counts as a ghost state so we don't need to explicitly 1757286570Smav * check to prevent usage of the arc_l2c_only list. 1758286570Smav */ 1759286570Smav if (((cnt = refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && 1760168404Spjd (state != arc_anon)) { 1761286763Smav arc_buf_contents_t type = arc_buf_type(hdr); 1762286763Smav multilist_t *list = &state->arcs_list[type]; 1763286763Smav uint64_t *size = &state->arcs_lsize[type]; 1764185029Spjd 1765286763Smav multilist_insert(list, hdr); 1766286763Smav 1767286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 1768286570Smav atomic_add_64(size, hdr->b_size * 1769286570Smav hdr->b_l1hdr.b_datacnt); 1770168404Spjd } 1771168404Spjd return (cnt); 1772168404Spjd} 1773168404Spjd 1774168404Spjd/* 1775286763Smav * Move the supplied buffer to the indicated state. The hash lock 1776168404Spjd * for the buffer must be held by the caller. 1777168404Spjd */ 1778168404Spjdstatic void 1779275811Sdelphijarc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, 1780275811Sdelphij kmutex_t *hash_lock) 1781168404Spjd{ 1782286570Smav arc_state_t *old_state; 1783286570Smav int64_t refcnt; 1784286570Smav uint32_t datacnt; 1785168404Spjd uint64_t from_delta, to_delta; 1786286570Smav arc_buf_contents_t buftype = arc_buf_type(hdr); 1787168404Spjd 1788286570Smav /* 1789286570Smav * We almost always have an L1 hdr here, since we call arc_hdr_realloc() 1790286570Smav * in arc_read() when bringing a buffer out of the L2ARC. However, the 1791286570Smav * L1 hdr doesn't always exist when we change state to arc_anon before 1792286570Smav * destroying a header, in which case reallocating to add the L1 hdr is 1793286570Smav * pointless. 1794286570Smav */ 1795286570Smav if (HDR_HAS_L1HDR(hdr)) { 1796286570Smav old_state = hdr->b_l1hdr.b_state; 1797286570Smav refcnt = refcount_count(&hdr->b_l1hdr.b_refcnt); 1798286570Smav datacnt = hdr->b_l1hdr.b_datacnt; 1799286570Smav } else { 1800286570Smav old_state = arc_l2c_only; 1801286570Smav refcnt = 0; 1802286570Smav datacnt = 0; 1803286570Smav } 1804286570Smav 1805168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 1806258632Savg ASSERT3P(new_state, !=, old_state); 1807286570Smav ASSERT(refcnt == 0 || datacnt > 0); 1808286570Smav ASSERT(!GHOST_STATE(new_state) || datacnt == 0); 1809286570Smav ASSERT(old_state != arc_anon || datacnt <= 1); 1810168404Spjd 1811286570Smav from_delta = to_delta = datacnt * hdr->b_size; 1812168404Spjd 1813168404Spjd /* 1814168404Spjd * If this buffer is evictable, transfer it from the 1815168404Spjd * old state list to the new state list. 1816168404Spjd */ 1817168404Spjd if (refcnt == 0) { 1818286570Smav if (old_state != arc_anon && old_state != arc_l2c_only) { 1819286570Smav uint64_t *size = &old_state->arcs_lsize[buftype]; 1820168404Spjd 1821286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1822286763Smav multilist_remove(&old_state->arcs_list[buftype], hdr); 1823168404Spjd 1824168404Spjd /* 1825168404Spjd * If prefetching out of the ghost cache, 1826219089Spjd * we will have a non-zero datacnt. 1827168404Spjd */ 1828286570Smav if (GHOST_STATE(old_state) && datacnt == 0) { 1829168404Spjd /* ghost elements have a ghost size */ 1830286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 1831275811Sdelphij from_delta = hdr->b_size; 1832168404Spjd } 1833185029Spjd ASSERT3U(*size, >=, from_delta); 1834185029Spjd atomic_add_64(size, -from_delta); 1835168404Spjd } 1836286570Smav if (new_state != arc_anon && new_state != arc_l2c_only) { 1837286570Smav uint64_t *size = &new_state->arcs_lsize[buftype]; 1838168404Spjd 1839286570Smav /* 1840286570Smav * An L1 header always exists here, since if we're 1841286570Smav * moving to some L1-cached state (i.e. not l2c_only or 1842286570Smav * anonymous), we realloc the header to add an L1hdr 1843286570Smav * beforehand. 1844286570Smav */ 1845286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1846286763Smav multilist_insert(&new_state->arcs_list[buftype], hdr); 1847168404Spjd 1848168404Spjd /* ghost elements have a ghost size */ 1849168404Spjd if (GHOST_STATE(new_state)) { 1850286762Smav ASSERT0(datacnt); 1851286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 1852275811Sdelphij to_delta = hdr->b_size; 1853168404Spjd } 1854185029Spjd atomic_add_64(size, to_delta); 1855168404Spjd } 1856168404Spjd } 1857168404Spjd 1858275811Sdelphij ASSERT(!BUF_EMPTY(hdr)); 1859275811Sdelphij if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) 1860275811Sdelphij buf_hash_remove(hdr); 1861168404Spjd 1862286570Smav /* adjust state sizes (ignore arc_l2c_only) */ 1863286766Smav 1864286766Smav if (to_delta && new_state != arc_l2c_only) { 1865286766Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1866286766Smav if (GHOST_STATE(new_state)) { 1867286766Smav ASSERT0(datacnt); 1868286766Smav 1869286766Smav /* 1870286766Smav * We moving a header to a ghost state, we first 1871286766Smav * remove all arc buffers. Thus, we'll have a 1872286766Smav * datacnt of zero, and no arc buffer to use for 1873286766Smav * the reference. As a result, we use the arc 1874286766Smav * header pointer for the reference. 1875286766Smav */ 1876286766Smav (void) refcount_add_many(&new_state->arcs_size, 1877286766Smav hdr->b_size, hdr); 1878286766Smav } else { 1879286766Smav ASSERT3U(datacnt, !=, 0); 1880286766Smav 1881286766Smav /* 1882286766Smav * Each individual buffer holds a unique reference, 1883286766Smav * thus we must remove each of these references one 1884286766Smav * at a time. 1885286766Smav */ 1886286766Smav for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 1887286766Smav buf = buf->b_next) { 1888286766Smav (void) refcount_add_many(&new_state->arcs_size, 1889286766Smav hdr->b_size, buf); 1890286766Smav } 1891286766Smav } 1892286766Smav } 1893286766Smav 1894286570Smav if (from_delta && old_state != arc_l2c_only) { 1895286766Smav ASSERT(HDR_HAS_L1HDR(hdr)); 1896286766Smav if (GHOST_STATE(old_state)) { 1897286766Smav /* 1898286766Smav * When moving a header off of a ghost state, 1899286766Smav * there's the possibility for datacnt to be 1900286766Smav * non-zero. This is because we first add the 1901286766Smav * arc buffer to the header prior to changing 1902286766Smav * the header's state. Since we used the header 1903286766Smav * for the reference when putting the header on 1904286766Smav * the ghost state, we must balance that and use 1905286766Smav * the header when removing off the ghost state 1906286766Smav * (even though datacnt is non zero). 1907286766Smav */ 1908286766Smav 1909286766Smav IMPLY(datacnt == 0, new_state == arc_anon || 1910286766Smav new_state == arc_l2c_only); 1911286766Smav 1912286766Smav (void) refcount_remove_many(&old_state->arcs_size, 1913286766Smav hdr->b_size, hdr); 1914286766Smav } else { 1915286766Smav ASSERT3P(datacnt, !=, 0); 1916286766Smav 1917286766Smav /* 1918286766Smav * Each individual buffer holds a unique reference, 1919286766Smav * thus we must remove each of these references one 1920286766Smav * at a time. 1921286766Smav */ 1922286766Smav for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 1923286766Smav buf = buf->b_next) { 1924286766Smav (void) refcount_remove_many( 1925286766Smav &old_state->arcs_size, hdr->b_size, buf); 1926286766Smav } 1927286766Smav } 1928168404Spjd } 1929286766Smav 1930286570Smav if (HDR_HAS_L1HDR(hdr)) 1931286570Smav hdr->b_l1hdr.b_state = new_state; 1932185029Spjd 1933286570Smav /* 1934286570Smav * L2 headers should never be on the L2 state list since they don't 1935286570Smav * have L1 headers allocated. 1936286570Smav */ 1937286763Smav ASSERT(multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && 1938286763Smav multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); 1939168404Spjd} 1940168404Spjd 1941185029Spjdvoid 1942208373Smmarc_space_consume(uint64_t space, arc_space_type_t type) 1943185029Spjd{ 1944208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1945208373Smm 1946208373Smm switch (type) { 1947208373Smm case ARC_SPACE_DATA: 1948208373Smm ARCSTAT_INCR(arcstat_data_size, space); 1949208373Smm break; 1950286574Smav case ARC_SPACE_META: 1951286574Smav ARCSTAT_INCR(arcstat_metadata_size, space); 1952286574Smav break; 1953208373Smm case ARC_SPACE_OTHER: 1954208373Smm ARCSTAT_INCR(arcstat_other_size, space); 1955208373Smm break; 1956208373Smm case ARC_SPACE_HDRS: 1957208373Smm ARCSTAT_INCR(arcstat_hdr_size, space); 1958208373Smm break; 1959208373Smm case ARC_SPACE_L2HDRS: 1960208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, space); 1961208373Smm break; 1962208373Smm } 1963208373Smm 1964286574Smav if (type != ARC_SPACE_DATA) 1965286574Smav ARCSTAT_INCR(arcstat_meta_used, space); 1966286574Smav 1967185029Spjd atomic_add_64(&arc_size, space); 1968185029Spjd} 1969185029Spjd 1970185029Spjdvoid 1971208373Smmarc_space_return(uint64_t space, arc_space_type_t type) 1972185029Spjd{ 1973208373Smm ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 1974208373Smm 1975208373Smm switch (type) { 1976208373Smm case ARC_SPACE_DATA: 1977208373Smm ARCSTAT_INCR(arcstat_data_size, -space); 1978208373Smm break; 1979286574Smav case ARC_SPACE_META: 1980286574Smav ARCSTAT_INCR(arcstat_metadata_size, -space); 1981286574Smav break; 1982208373Smm case ARC_SPACE_OTHER: 1983208373Smm ARCSTAT_INCR(arcstat_other_size, -space); 1984208373Smm break; 1985208373Smm case ARC_SPACE_HDRS: 1986208373Smm ARCSTAT_INCR(arcstat_hdr_size, -space); 1987208373Smm break; 1988208373Smm case ARC_SPACE_L2HDRS: 1989208373Smm ARCSTAT_INCR(arcstat_l2_hdr_size, -space); 1990208373Smm break; 1991208373Smm } 1992208373Smm 1993286574Smav if (type != ARC_SPACE_DATA) { 1994286574Smav ASSERT(arc_meta_used >= space); 1995286574Smav if (arc_meta_max < arc_meta_used) 1996286574Smav arc_meta_max = arc_meta_used; 1997286574Smav ARCSTAT_INCR(arcstat_meta_used, -space); 1998286574Smav } 1999286574Smav 2000185029Spjd ASSERT(arc_size >= space); 2001185029Spjd atomic_add_64(&arc_size, -space); 2002185029Spjd} 2003185029Spjd 2004168404Spjdarc_buf_t * 2005286570Smavarc_buf_alloc(spa_t *spa, int32_t size, void *tag, arc_buf_contents_t type) 2006168404Spjd{ 2007168404Spjd arc_buf_hdr_t *hdr; 2008168404Spjd arc_buf_t *buf; 2009168404Spjd 2010168404Spjd ASSERT3U(size, >, 0); 2011286570Smav hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 2012168404Spjd ASSERT(BUF_EMPTY(hdr)); 2013286570Smav ASSERT3P(hdr->b_freeze_cksum, ==, NULL); 2014168404Spjd hdr->b_size = size; 2015228103Smm hdr->b_spa = spa_load_guid(spa); 2016286570Smav 2017185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2018168404Spjd buf->b_hdr = hdr; 2019168404Spjd buf->b_data = NULL; 2020168404Spjd buf->b_efunc = NULL; 2021168404Spjd buf->b_private = NULL; 2022168404Spjd buf->b_next = NULL; 2023286570Smav 2024286570Smav hdr->b_flags = arc_bufc_to_flags(type); 2025286570Smav hdr->b_flags |= ARC_FLAG_HAS_L1HDR; 2026286570Smav 2027286570Smav hdr->b_l1hdr.b_buf = buf; 2028286570Smav hdr->b_l1hdr.b_state = arc_anon; 2029286570Smav hdr->b_l1hdr.b_arc_access = 0; 2030286570Smav hdr->b_l1hdr.b_datacnt = 1; 2031286763Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 2032286570Smav 2033168404Spjd arc_get_data_buf(buf); 2034286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2035286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 2036168404Spjd 2037168404Spjd return (buf); 2038168404Spjd} 2039168404Spjd 2040209962Smmstatic char *arc_onloan_tag = "onloan"; 2041209962Smm 2042209962Smm/* 2043209962Smm * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 2044209962Smm * flight data by arc_tempreserve_space() until they are "returned". Loaned 2045209962Smm * buffers must be returned to the arc before they can be used by the DMU or 2046209962Smm * freed. 2047209962Smm */ 2048209962Smmarc_buf_t * 2049209962Smmarc_loan_buf(spa_t *spa, int size) 2050209962Smm{ 2051209962Smm arc_buf_t *buf; 2052209962Smm 2053209962Smm buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); 2054209962Smm 2055209962Smm atomic_add_64(&arc_loaned_bytes, size); 2056209962Smm return (buf); 2057209962Smm} 2058209962Smm 2059209962Smm/* 2060209962Smm * Return a loaned arc buffer to the arc. 2061209962Smm */ 2062209962Smmvoid 2063209962Smmarc_return_buf(arc_buf_t *buf, void *tag) 2064209962Smm{ 2065209962Smm arc_buf_hdr_t *hdr = buf->b_hdr; 2066209962Smm 2067209962Smm ASSERT(buf->b_data != NULL); 2068286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2069286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 2070286570Smav (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2071209962Smm 2072209962Smm atomic_add_64(&arc_loaned_bytes, -hdr->b_size); 2073209962Smm} 2074209962Smm 2075219089Spjd/* Detach an arc_buf from a dbuf (tag) */ 2076219089Spjdvoid 2077219089Spjdarc_loan_inuse_buf(arc_buf_t *buf, void *tag) 2078219089Spjd{ 2079286570Smav arc_buf_hdr_t *hdr = buf->b_hdr; 2080219089Spjd 2081219089Spjd ASSERT(buf->b_data != NULL); 2082286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2083286570Smav (void) refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2084286570Smav (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); 2085219089Spjd buf->b_efunc = NULL; 2086219089Spjd buf->b_private = NULL; 2087219089Spjd 2088219089Spjd atomic_add_64(&arc_loaned_bytes, hdr->b_size); 2089219089Spjd} 2090219089Spjd 2091168404Spjdstatic arc_buf_t * 2092168404Spjdarc_buf_clone(arc_buf_t *from) 2093168404Spjd{ 2094168404Spjd arc_buf_t *buf; 2095168404Spjd arc_buf_hdr_t *hdr = from->b_hdr; 2096168404Spjd uint64_t size = hdr->b_size; 2097168404Spjd 2098286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2099286570Smav ASSERT(hdr->b_l1hdr.b_state != arc_anon); 2100219089Spjd 2101185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2102168404Spjd buf->b_hdr = hdr; 2103168404Spjd buf->b_data = NULL; 2104168404Spjd buf->b_efunc = NULL; 2105168404Spjd buf->b_private = NULL; 2106286570Smav buf->b_next = hdr->b_l1hdr.b_buf; 2107286570Smav hdr->b_l1hdr.b_buf = buf; 2108168404Spjd arc_get_data_buf(buf); 2109168404Spjd bcopy(from->b_data, buf->b_data, size); 2110242845Sdelphij 2111242845Sdelphij /* 2112242845Sdelphij * This buffer already exists in the arc so create a duplicate 2113242845Sdelphij * copy for the caller. If the buffer is associated with user data 2114242845Sdelphij * then track the size and number of duplicates. These stats will be 2115242845Sdelphij * updated as duplicate buffers are created and destroyed. 2116242845Sdelphij */ 2117286570Smav if (HDR_ISTYPE_DATA(hdr)) { 2118242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_buffers); 2119242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, size); 2120242845Sdelphij } 2121286570Smav hdr->b_l1hdr.b_datacnt += 1; 2122168404Spjd return (buf); 2123168404Spjd} 2124168404Spjd 2125168404Spjdvoid 2126168404Spjdarc_buf_add_ref(arc_buf_t *buf, void* tag) 2127168404Spjd{ 2128168404Spjd arc_buf_hdr_t *hdr; 2129168404Spjd kmutex_t *hash_lock; 2130168404Spjd 2131168404Spjd /* 2132185029Spjd * Check to see if this buffer is evicted. Callers 2133185029Spjd * must verify b_data != NULL to know if the add_ref 2134185029Spjd * was successful. 2135168404Spjd */ 2136219089Spjd mutex_enter(&buf->b_evict_lock); 2137185029Spjd if (buf->b_data == NULL) { 2138219089Spjd mutex_exit(&buf->b_evict_lock); 2139168404Spjd return; 2140168404Spjd } 2141219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 2142219089Spjd mutex_enter(hash_lock); 2143185029Spjd hdr = buf->b_hdr; 2144286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2145219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2146219089Spjd mutex_exit(&buf->b_evict_lock); 2147168404Spjd 2148286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_mru || 2149286570Smav hdr->b_l1hdr.b_state == arc_mfu); 2150286570Smav 2151168404Spjd add_reference(hdr, hash_lock, tag); 2152208373Smm DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 2153168404Spjd arc_access(hdr, hash_lock); 2154168404Spjd mutex_exit(hash_lock); 2155168404Spjd ARCSTAT_BUMP(arcstat_hits); 2156286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 2157286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 2158168404Spjd data, metadata, hits); 2159168404Spjd} 2160168404Spjd 2161274172Savgstatic void 2162274172Savgarc_buf_free_on_write(void *data, size_t size, 2163274172Savg void (*free_func)(void *, size_t)) 2164274172Savg{ 2165274172Savg l2arc_data_free_t *df; 2166274172Savg 2167286763Smav df = kmem_alloc(sizeof (*df), KM_SLEEP); 2168274172Savg df->l2df_data = data; 2169274172Savg df->l2df_size = size; 2170274172Savg df->l2df_func = free_func; 2171274172Savg mutex_enter(&l2arc_free_on_write_mtx); 2172274172Savg list_insert_head(l2arc_free_on_write, df); 2173274172Savg mutex_exit(&l2arc_free_on_write_mtx); 2174274172Savg} 2175274172Savg 2176185029Spjd/* 2177185029Spjd * Free the arc data buffer. If it is an l2arc write in progress, 2178185029Spjd * the buffer is placed on l2arc_free_on_write to be freed later. 2179185029Spjd */ 2180168404Spjdstatic void 2181240133Smmarc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t)) 2182185029Spjd{ 2183240133Smm arc_buf_hdr_t *hdr = buf->b_hdr; 2184240133Smm 2185185029Spjd if (HDR_L2_WRITING(hdr)) { 2186274172Savg arc_buf_free_on_write(buf->b_data, hdr->b_size, free_func); 2187185029Spjd ARCSTAT_BUMP(arcstat_l2_free_on_write); 2188185029Spjd } else { 2189240133Smm free_func(buf->b_data, hdr->b_size); 2190185029Spjd } 2191185029Spjd} 2192185029Spjd 2193185029Spjdstatic void 2194274172Savgarc_buf_l2_cdata_free(arc_buf_hdr_t *hdr) 2195274172Savg{ 2196286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 2197286570Smav ASSERT(MUTEX_HELD(&hdr->b_l2hdr.b_dev->l2ad_mtx)); 2198274172Savg 2199286570Smav /* 2200286570Smav * The b_tmp_cdata field is linked off of the b_l1hdr, so if 2201286570Smav * that doesn't exist, the header is in the arc_l2c_only state, 2202286570Smav * and there isn't anything to free (it's already been freed). 2203286570Smav */ 2204286570Smav if (!HDR_HAS_L1HDR(hdr)) 2205286570Smav return; 2206274172Savg 2207286763Smav /* 2208286763Smav * The header isn't being written to the l2arc device, thus it 2209286763Smav * shouldn't have a b_tmp_cdata to free. 2210286763Smav */ 2211286763Smav if (!HDR_L2_WRITING(hdr)) { 2212286763Smav ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 2213274172Savg return; 2214286763Smav } 2215274172Savg 2216286763Smav /* 2217286763Smav * The header does not have compression enabled. This can be due 2218286763Smav * to the buffer not being compressible, or because we're 2219286763Smav * freeing the buffer before the second phase of 2220286763Smav * l2arc_write_buffer() has started (which does the compression 2221286763Smav * step). In either case, b_tmp_cdata does not point to a 2222286763Smav * separately compressed buffer, so there's nothing to free (it 2223286763Smav * points to the same buffer as the arc_buf_t's b_data field). 2224286763Smav */ 2225286763Smav if (HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) { 2226286763Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 2227286763Smav return; 2228286763Smav } 2229286570Smav 2230286763Smav /* 2231286763Smav * There's nothing to free since the buffer was all zero's and 2232286763Smav * compressed to a zero length buffer. 2233286763Smav */ 2234286763Smav if (HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_EMPTY) { 2235286763Smav ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 2236286763Smav return; 2237286763Smav } 2238286763Smav 2239286763Smav ASSERT(L2ARC_IS_VALID_COMPRESS(HDR_GET_COMPRESS(hdr))); 2240286763Smav 2241286763Smav arc_buf_free_on_write(hdr->b_l1hdr.b_tmp_cdata, 2242286763Smav hdr->b_size, zio_data_buf_free); 2243286763Smav 2244274172Savg ARCSTAT_BUMP(arcstat_l2_cdata_free_on_write); 2245286570Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 2246274172Savg} 2247274172Savg 2248286767Smav/* 2249286767Smav * Free up buf->b_data and if 'remove' is set, then pull the 2250286767Smav * arc_buf_t off of the the arc_buf_hdr_t's list and free it. 2251286767Smav */ 2252274172Savgstatic void 2253286763Smavarc_buf_destroy(arc_buf_t *buf, boolean_t remove) 2254168404Spjd{ 2255168404Spjd arc_buf_t **bufp; 2256168404Spjd 2257168404Spjd /* free up data associated with the buf */ 2258286570Smav if (buf->b_data != NULL) { 2259286570Smav arc_state_t *state = buf->b_hdr->b_l1hdr.b_state; 2260168404Spjd uint64_t size = buf->b_hdr->b_size; 2261286570Smav arc_buf_contents_t type = arc_buf_type(buf->b_hdr); 2262168404Spjd 2263168404Spjd arc_cksum_verify(buf); 2264240133Smm#ifdef illumos 2265240133Smm arc_buf_unwatch(buf); 2266277300Ssmh#endif 2267219089Spjd 2268286763Smav if (type == ARC_BUFC_METADATA) { 2269286763Smav arc_buf_data_free(buf, zio_buf_free); 2270286763Smav arc_space_return(size, ARC_SPACE_META); 2271286763Smav } else { 2272286763Smav ASSERT(type == ARC_BUFC_DATA); 2273286763Smav arc_buf_data_free(buf, zio_data_buf_free); 2274286763Smav arc_space_return(size, ARC_SPACE_DATA); 2275168404Spjd } 2276286763Smav 2277286763Smav /* protected by hash lock, if in the hash table */ 2278286763Smav if (multilist_link_active(&buf->b_hdr->b_l1hdr.b_arc_node)) { 2279185029Spjd uint64_t *cnt = &state->arcs_lsize[type]; 2280185029Spjd 2281286570Smav ASSERT(refcount_is_zero( 2282286570Smav &buf->b_hdr->b_l1hdr.b_refcnt)); 2283286570Smav ASSERT(state != arc_anon && state != arc_l2c_only); 2284185029Spjd 2285185029Spjd ASSERT3U(*cnt, >=, size); 2286185029Spjd atomic_add_64(cnt, -size); 2287168404Spjd } 2288286766Smav 2289286766Smav (void) refcount_remove_many(&state->arcs_size, size, buf); 2290168404Spjd buf->b_data = NULL; 2291242845Sdelphij 2292242845Sdelphij /* 2293242845Sdelphij * If we're destroying a duplicate buffer make sure 2294242845Sdelphij * that the appropriate statistics are updated. 2295242845Sdelphij */ 2296286570Smav if (buf->b_hdr->b_l1hdr.b_datacnt > 1 && 2297286570Smav HDR_ISTYPE_DATA(buf->b_hdr)) { 2298242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 2299242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size); 2300242845Sdelphij } 2301286570Smav ASSERT(buf->b_hdr->b_l1hdr.b_datacnt > 0); 2302286570Smav buf->b_hdr->b_l1hdr.b_datacnt -= 1; 2303168404Spjd } 2304168404Spjd 2305168404Spjd /* only remove the buf if requested */ 2306268858Sdelphij if (!remove) 2307168404Spjd return; 2308168404Spjd 2309168404Spjd /* remove the buf from the hdr list */ 2310286570Smav for (bufp = &buf->b_hdr->b_l1hdr.b_buf; *bufp != buf; 2311286570Smav bufp = &(*bufp)->b_next) 2312168404Spjd continue; 2313168404Spjd *bufp = buf->b_next; 2314219089Spjd buf->b_next = NULL; 2315168404Spjd 2316168404Spjd ASSERT(buf->b_efunc == NULL); 2317168404Spjd 2318168404Spjd /* clean up the buf */ 2319168404Spjd buf->b_hdr = NULL; 2320168404Spjd kmem_cache_free(buf_cache, buf); 2321168404Spjd} 2322168404Spjd 2323168404Spjdstatic void 2324286598Smavarc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) 2325286598Smav{ 2326286598Smav l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 2327286598Smav l2arc_dev_t *dev = l2hdr->b_dev; 2328286598Smav 2329286598Smav ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); 2330286598Smav ASSERT(HDR_HAS_L2HDR(hdr)); 2331286598Smav 2332286598Smav list_remove(&dev->l2ad_buflist, hdr); 2333286598Smav 2334286598Smav /* 2335286598Smav * We don't want to leak the b_tmp_cdata buffer that was 2336286598Smav * allocated in l2arc_write_buffers() 2337286598Smav */ 2338286598Smav arc_buf_l2_cdata_free(hdr); 2339286598Smav 2340286598Smav /* 2341286598Smav * If the l2hdr's b_daddr is equal to L2ARC_ADDR_UNSET, then 2342286598Smav * this header is being processed by l2arc_write_buffers() (i.e. 2343286598Smav * it's in the first stage of l2arc_write_buffers()). 2344286598Smav * Re-affirming that truth here, just to serve as a reminder. If 2345286598Smav * b_daddr does not equal L2ARC_ADDR_UNSET, then the header may or 2346286598Smav * may not have its HDR_L2_WRITING flag set. (the write may have 2347286598Smav * completed, in which case HDR_L2_WRITING will be false and the 2348286598Smav * b_daddr field will point to the address of the buffer on disk). 2349286598Smav */ 2350286598Smav IMPLY(l2hdr->b_daddr == L2ARC_ADDR_UNSET, HDR_L2_WRITING(hdr)); 2351286598Smav 2352286598Smav /* 2353286598Smav * If b_daddr is equal to L2ARC_ADDR_UNSET, we're racing with 2354286598Smav * l2arc_write_buffers(). Since we've just removed this header 2355286598Smav * from the l2arc buffer list, this header will never reach the 2356286598Smav * second stage of l2arc_write_buffers(), which increments the 2357286598Smav * accounting stats for this header. Thus, we must be careful 2358286598Smav * not to decrement them for this header either. 2359286598Smav */ 2360286598Smav if (l2hdr->b_daddr != L2ARC_ADDR_UNSET) { 2361286598Smav ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); 2362286598Smav ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 2363286598Smav 2364286598Smav vdev_space_update(dev->l2ad_vdev, 2365286598Smav -l2hdr->b_asize, 0, 0); 2366286598Smav 2367286598Smav (void) refcount_remove_many(&dev->l2ad_alloc, 2368286598Smav l2hdr->b_asize, hdr); 2369286598Smav } 2370286598Smav 2371286598Smav hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR; 2372286598Smav} 2373286598Smav 2374286598Smavstatic void 2375168404Spjdarc_hdr_destroy(arc_buf_hdr_t *hdr) 2376168404Spjd{ 2377286570Smav if (HDR_HAS_L1HDR(hdr)) { 2378286570Smav ASSERT(hdr->b_l1hdr.b_buf == NULL || 2379286570Smav hdr->b_l1hdr.b_datacnt > 0); 2380286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2381286570Smav ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 2382286570Smav } 2383168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2384286570Smav ASSERT(!HDR_IN_HASH_TABLE(hdr)); 2385168404Spjd 2386286570Smav if (HDR_HAS_L2HDR(hdr)) { 2387286598Smav l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 2388286598Smav boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); 2389286570Smav 2390286598Smav if (!buflist_held) 2391286598Smav mutex_enter(&dev->l2ad_mtx); 2392219089Spjd 2393286570Smav /* 2394286598Smav * Even though we checked this conditional above, we 2395286598Smav * need to check this again now that we have the 2396286598Smav * l2ad_mtx. This is because we could be racing with 2397286598Smav * another thread calling l2arc_evict() which might have 2398286598Smav * destroyed this header's L2 portion as we were waiting 2399286598Smav * to acquire the l2ad_mtx. If that happens, we don't 2400286598Smav * want to re-destroy the header's L2 portion. 2401286570Smav */ 2402286598Smav if (HDR_HAS_L2HDR(hdr)) { 2403286647Smav if (hdr->b_l2hdr.b_daddr != L2ARC_ADDR_UNSET) 2404286647Smav trim_map_free(dev->l2ad_vdev, 2405286647Smav hdr->b_l2hdr.b_daddr, 2406286647Smav hdr->b_l2hdr.b_asize, 0); 2407286598Smav arc_hdr_l2hdr_destroy(hdr); 2408286598Smav } 2409286570Smav 2410219089Spjd if (!buflist_held) 2411286598Smav mutex_exit(&dev->l2ad_mtx); 2412185029Spjd } 2413185029Spjd 2414286570Smav if (!BUF_EMPTY(hdr)) 2415219089Spjd buf_discard_identity(hdr); 2416286776Smav 2417168404Spjd if (hdr->b_freeze_cksum != NULL) { 2418168404Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 2419168404Spjd hdr->b_freeze_cksum = NULL; 2420168404Spjd } 2421286570Smav 2422286570Smav if (HDR_HAS_L1HDR(hdr)) { 2423286570Smav while (hdr->b_l1hdr.b_buf) { 2424286570Smav arc_buf_t *buf = hdr->b_l1hdr.b_buf; 2425286570Smav 2426286570Smav if (buf->b_efunc != NULL) { 2427286763Smav mutex_enter(&arc_user_evicts_lock); 2428286570Smav mutex_enter(&buf->b_evict_lock); 2429286570Smav ASSERT(buf->b_hdr != NULL); 2430286763Smav arc_buf_destroy(hdr->b_l1hdr.b_buf, FALSE); 2431286570Smav hdr->b_l1hdr.b_buf = buf->b_next; 2432286570Smav buf->b_hdr = &arc_eviction_hdr; 2433286570Smav buf->b_next = arc_eviction_list; 2434286570Smav arc_eviction_list = buf; 2435286570Smav mutex_exit(&buf->b_evict_lock); 2436286763Smav cv_signal(&arc_user_evicts_cv); 2437286763Smav mutex_exit(&arc_user_evicts_lock); 2438286570Smav } else { 2439286763Smav arc_buf_destroy(hdr->b_l1hdr.b_buf, TRUE); 2440286570Smav } 2441286570Smav } 2442286570Smav#ifdef ZFS_DEBUG 2443286570Smav if (hdr->b_l1hdr.b_thawed != NULL) { 2444286570Smav kmem_free(hdr->b_l1hdr.b_thawed, 1); 2445286570Smav hdr->b_l1hdr.b_thawed = NULL; 2446286570Smav } 2447286570Smav#endif 2448219089Spjd } 2449168404Spjd 2450168404Spjd ASSERT3P(hdr->b_hash_next, ==, NULL); 2451286570Smav if (HDR_HAS_L1HDR(hdr)) { 2452286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 2453286570Smav ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 2454286570Smav kmem_cache_free(hdr_full_cache, hdr); 2455286570Smav } else { 2456286570Smav kmem_cache_free(hdr_l2only_cache, hdr); 2457286570Smav } 2458168404Spjd} 2459168404Spjd 2460168404Spjdvoid 2461168404Spjdarc_buf_free(arc_buf_t *buf, void *tag) 2462168404Spjd{ 2463168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2464286570Smav int hashed = hdr->b_l1hdr.b_state != arc_anon; 2465168404Spjd 2466168404Spjd ASSERT(buf->b_efunc == NULL); 2467168404Spjd ASSERT(buf->b_data != NULL); 2468168404Spjd 2469168404Spjd if (hashed) { 2470168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 2471168404Spjd 2472168404Spjd mutex_enter(hash_lock); 2473219089Spjd hdr = buf->b_hdr; 2474219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2475219089Spjd 2476168404Spjd (void) remove_reference(hdr, hash_lock, tag); 2477286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 2478286763Smav arc_buf_destroy(buf, TRUE); 2479219089Spjd } else { 2480286570Smav ASSERT(buf == hdr->b_l1hdr.b_buf); 2481219089Spjd ASSERT(buf->b_efunc == NULL); 2482275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 2483219089Spjd } 2484168404Spjd mutex_exit(hash_lock); 2485168404Spjd } else if (HDR_IO_IN_PROGRESS(hdr)) { 2486168404Spjd int destroy_hdr; 2487168404Spjd /* 2488168404Spjd * We are in the middle of an async write. Don't destroy 2489168404Spjd * this buffer unless the write completes before we finish 2490168404Spjd * decrementing the reference count. 2491168404Spjd */ 2492286763Smav mutex_enter(&arc_user_evicts_lock); 2493168404Spjd (void) remove_reference(hdr, NULL, tag); 2494286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2495168404Spjd destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 2496286763Smav mutex_exit(&arc_user_evicts_lock); 2497168404Spjd if (destroy_hdr) 2498168404Spjd arc_hdr_destroy(hdr); 2499168404Spjd } else { 2500219089Spjd if (remove_reference(hdr, NULL, tag) > 0) 2501286763Smav arc_buf_destroy(buf, TRUE); 2502219089Spjd else 2503168404Spjd arc_hdr_destroy(hdr); 2504168404Spjd } 2505168404Spjd} 2506168404Spjd 2507248571Smmboolean_t 2508168404Spjdarc_buf_remove_ref(arc_buf_t *buf, void* tag) 2509168404Spjd{ 2510168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 2511168404Spjd kmutex_t *hash_lock = HDR_LOCK(hdr); 2512248571Smm boolean_t no_callback = (buf->b_efunc == NULL); 2513168404Spjd 2514286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 2515286570Smav ASSERT(hdr->b_l1hdr.b_datacnt == 1); 2516168404Spjd arc_buf_free(buf, tag); 2517168404Spjd return (no_callback); 2518168404Spjd } 2519168404Spjd 2520168404Spjd mutex_enter(hash_lock); 2521219089Spjd hdr = buf->b_hdr; 2522286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 2523219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 2524286570Smav ASSERT(hdr->b_l1hdr.b_state != arc_anon); 2525168404Spjd ASSERT(buf->b_data != NULL); 2526168404Spjd 2527168404Spjd (void) remove_reference(hdr, hash_lock, tag); 2528286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 2529168404Spjd if (no_callback) 2530286763Smav arc_buf_destroy(buf, TRUE); 2531168404Spjd } else if (no_callback) { 2532286570Smav ASSERT(hdr->b_l1hdr.b_buf == buf && buf->b_next == NULL); 2533219089Spjd ASSERT(buf->b_efunc == NULL); 2534275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 2535168404Spjd } 2536286570Smav ASSERT(no_callback || hdr->b_l1hdr.b_datacnt > 1 || 2537286570Smav refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2538168404Spjd mutex_exit(hash_lock); 2539168404Spjd return (no_callback); 2540168404Spjd} 2541168404Spjd 2542286570Smavint32_t 2543168404Spjdarc_buf_size(arc_buf_t *buf) 2544168404Spjd{ 2545168404Spjd return (buf->b_hdr->b_size); 2546168404Spjd} 2547168404Spjd 2548168404Spjd/* 2549242845Sdelphij * Called from the DMU to determine if the current buffer should be 2550242845Sdelphij * evicted. In order to ensure proper locking, the eviction must be initiated 2551242845Sdelphij * from the DMU. Return true if the buffer is associated with user data and 2552242845Sdelphij * duplicate buffers still exist. 2553242845Sdelphij */ 2554242845Sdelphijboolean_t 2555242845Sdelphijarc_buf_eviction_needed(arc_buf_t *buf) 2556242845Sdelphij{ 2557242845Sdelphij arc_buf_hdr_t *hdr; 2558242845Sdelphij boolean_t evict_needed = B_FALSE; 2559242845Sdelphij 2560242845Sdelphij if (zfs_disable_dup_eviction) 2561242845Sdelphij return (B_FALSE); 2562242845Sdelphij 2563242845Sdelphij mutex_enter(&buf->b_evict_lock); 2564242845Sdelphij hdr = buf->b_hdr; 2565242845Sdelphij if (hdr == NULL) { 2566242845Sdelphij /* 2567242845Sdelphij * We are in arc_do_user_evicts(); let that function 2568242845Sdelphij * perform the eviction. 2569242845Sdelphij */ 2570242845Sdelphij ASSERT(buf->b_data == NULL); 2571242845Sdelphij mutex_exit(&buf->b_evict_lock); 2572242845Sdelphij return (B_FALSE); 2573242845Sdelphij } else if (buf->b_data == NULL) { 2574242845Sdelphij /* 2575242845Sdelphij * We have already been added to the arc eviction list; 2576242845Sdelphij * recommend eviction. 2577242845Sdelphij */ 2578242845Sdelphij ASSERT3P(hdr, ==, &arc_eviction_hdr); 2579242845Sdelphij mutex_exit(&buf->b_evict_lock); 2580242845Sdelphij return (B_TRUE); 2581242845Sdelphij } 2582242845Sdelphij 2583286570Smav if (hdr->b_l1hdr.b_datacnt > 1 && HDR_ISTYPE_DATA(hdr)) 2584242845Sdelphij evict_needed = B_TRUE; 2585242845Sdelphij 2586242845Sdelphij mutex_exit(&buf->b_evict_lock); 2587242845Sdelphij return (evict_needed); 2588242845Sdelphij} 2589242845Sdelphij 2590242845Sdelphij/* 2591286763Smav * Evict the arc_buf_hdr that is provided as a parameter. The resultant 2592286763Smav * state of the header is dependent on it's state prior to entering this 2593286763Smav * function. The following transitions are possible: 2594185029Spjd * 2595286763Smav * - arc_mru -> arc_mru_ghost 2596286763Smav * - arc_mfu -> arc_mfu_ghost 2597286763Smav * - arc_mru_ghost -> arc_l2c_only 2598286763Smav * - arc_mru_ghost -> deleted 2599286763Smav * - arc_mfu_ghost -> arc_l2c_only 2600286763Smav * - arc_mfu_ghost -> deleted 2601168404Spjd */ 2602286763Smavstatic int64_t 2603286763Smavarc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 2604168404Spjd{ 2605286763Smav arc_state_t *evicted_state, *state; 2606286763Smav int64_t bytes_evicted = 0; 2607168404Spjd 2608286763Smav ASSERT(MUTEX_HELD(hash_lock)); 2609286763Smav ASSERT(HDR_HAS_L1HDR(hdr)); 2610168404Spjd 2611286763Smav state = hdr->b_l1hdr.b_state; 2612286763Smav if (GHOST_STATE(state)) { 2613286763Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2614286763Smav ASSERT(hdr->b_l1hdr.b_buf == NULL); 2615206796Spjd 2616286763Smav /* 2617286763Smav * l2arc_write_buffers() relies on a header's L1 portion 2618286763Smav * (i.e. it's b_tmp_cdata field) during it's write phase. 2619286763Smav * Thus, we cannot push a header onto the arc_l2c_only 2620286763Smav * state (removing it's L1 piece) until the header is 2621286763Smav * done being written to the l2arc. 2622286763Smav */ 2623286763Smav if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { 2624286763Smav ARCSTAT_BUMP(arcstat_evict_l2_skip); 2625286763Smav return (bytes_evicted); 2626286763Smav } 2627286762Smav 2628286763Smav ARCSTAT_BUMP(arcstat_deleted); 2629286763Smav bytes_evicted += hdr->b_size; 2630286762Smav 2631286763Smav DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); 2632286763Smav 2633286763Smav if (HDR_HAS_L2HDR(hdr)) { 2634275780Sdelphij /* 2635286763Smav * This buffer is cached on the 2nd Level ARC; 2636286763Smav * don't destroy the header. 2637275780Sdelphij */ 2638286763Smav arc_change_state(arc_l2c_only, hdr, hash_lock); 2639286763Smav /* 2640286763Smav * dropping from L1+L2 cached to L2-only, 2641286763Smav * realloc to remove the L1 header. 2642286763Smav */ 2643286763Smav hdr = arc_hdr_realloc(hdr, hdr_full_cache, 2644286763Smav hdr_l2only_cache); 2645286763Smav } else { 2646286763Smav arc_change_state(arc_anon, hdr, hash_lock); 2647286763Smav arc_hdr_destroy(hdr); 2648275780Sdelphij } 2649286763Smav return (bytes_evicted); 2650275780Sdelphij } 2651275780Sdelphij 2652286763Smav ASSERT(state == arc_mru || state == arc_mfu); 2653286763Smav evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; 2654206796Spjd 2655286763Smav /* prefetch buffers have a minimum lifespan */ 2656286763Smav if (HDR_IO_IN_PROGRESS(hdr) || 2657286763Smav ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && 2658286763Smav ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < 2659286763Smav arc_min_prefetch_lifespan)) { 2660286763Smav ARCSTAT_BUMP(arcstat_evict_skip); 2661286763Smav return (bytes_evicted); 2662286763Smav } 2663286763Smav 2664286763Smav ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 2665286763Smav ASSERT3U(hdr->b_l1hdr.b_datacnt, >, 0); 2666286763Smav while (hdr->b_l1hdr.b_buf) { 2667286763Smav arc_buf_t *buf = hdr->b_l1hdr.b_buf; 2668286763Smav if (!mutex_tryenter(&buf->b_evict_lock)) { 2669286763Smav ARCSTAT_BUMP(arcstat_mutex_miss); 2670286763Smav break; 2671168404Spjd } 2672286763Smav if (buf->b_data != NULL) 2673286763Smav bytes_evicted += hdr->b_size; 2674286763Smav if (buf->b_efunc != NULL) { 2675286763Smav mutex_enter(&arc_user_evicts_lock); 2676286763Smav arc_buf_destroy(buf, FALSE); 2677286763Smav hdr->b_l1hdr.b_buf = buf->b_next; 2678286763Smav buf->b_hdr = &arc_eviction_hdr; 2679286763Smav buf->b_next = arc_eviction_list; 2680286763Smav arc_eviction_list = buf; 2681286763Smav cv_signal(&arc_user_evicts_cv); 2682286763Smav mutex_exit(&arc_user_evicts_lock); 2683286763Smav mutex_exit(&buf->b_evict_lock); 2684286763Smav } else { 2685286763Smav mutex_exit(&buf->b_evict_lock); 2686286763Smav arc_buf_destroy(buf, TRUE); 2687286763Smav } 2688286763Smav } 2689258632Savg 2690286763Smav if (HDR_HAS_L2HDR(hdr)) { 2691286763Smav ARCSTAT_INCR(arcstat_evict_l2_cached, hdr->b_size); 2692286763Smav } else { 2693286763Smav if (l2arc_write_eligible(hdr->b_spa, hdr)) 2694286763Smav ARCSTAT_INCR(arcstat_evict_l2_eligible, hdr->b_size); 2695286763Smav else 2696286763Smav ARCSTAT_INCR(arcstat_evict_l2_ineligible, hdr->b_size); 2697286763Smav } 2698258632Savg 2699286763Smav if (hdr->b_l1hdr.b_datacnt == 0) { 2700286763Smav arc_change_state(evicted_state, hdr, hash_lock); 2701286763Smav ASSERT(HDR_IN_HASH_TABLE(hdr)); 2702286763Smav hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; 2703286763Smav hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 2704286763Smav DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); 2705286763Smav } 2706286763Smav 2707286763Smav return (bytes_evicted); 2708286763Smav} 2709286763Smav 2710286763Smavstatic uint64_t 2711286763Smavarc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, 2712286763Smav uint64_t spa, int64_t bytes) 2713286763Smav{ 2714286763Smav multilist_sublist_t *mls; 2715286763Smav uint64_t bytes_evicted = 0; 2716286763Smav arc_buf_hdr_t *hdr; 2717286763Smav kmutex_t *hash_lock; 2718286763Smav int evict_count = 0; 2719286763Smav 2720286763Smav ASSERT3P(marker, !=, NULL); 2721286763Smav IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 2722286763Smav 2723286763Smav mls = multilist_sublist_lock(ml, idx); 2724286763Smav 2725286763Smav for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL; 2726286763Smav hdr = multilist_sublist_prev(mls, marker)) { 2727286763Smav if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) || 2728286763Smav (evict_count >= zfs_arc_evict_batch_limit)) 2729286763Smav break; 2730286763Smav 2731258632Savg /* 2732286763Smav * To keep our iteration location, move the marker 2733286763Smav * forward. Since we're not holding hdr's hash lock, we 2734286763Smav * must be very careful and not remove 'hdr' from the 2735286763Smav * sublist. Otherwise, other consumers might mistake the 2736286763Smav * 'hdr' as not being on a sublist when they call the 2737286763Smav * multilist_link_active() function (they all rely on 2738286763Smav * the hash lock protecting concurrent insertions and 2739286763Smav * removals). multilist_sublist_move_forward() was 2740286763Smav * specifically implemented to ensure this is the case 2741286763Smav * (only 'marker' will be removed and re-inserted). 2742258632Savg */ 2743286763Smav multilist_sublist_move_forward(mls, marker); 2744286763Smav 2745286763Smav /* 2746286763Smav * The only case where the b_spa field should ever be 2747286763Smav * zero, is the marker headers inserted by 2748286763Smav * arc_evict_state(). It's possible for multiple threads 2749286763Smav * to be calling arc_evict_state() concurrently (e.g. 2750286763Smav * dsl_pool_close() and zio_inject_fault()), so we must 2751286763Smav * skip any markers we see from these other threads. 2752286763Smav */ 2753286763Smav if (hdr->b_spa == 0) 2754258632Savg continue; 2755286763Smav 2756286763Smav /* we're only interested in evicting buffers of a certain spa */ 2757286763Smav if (spa != 0 && hdr->b_spa != spa) { 2758286763Smav ARCSTAT_BUMP(arcstat_evict_skip); 2759286763Smav continue; 2760258632Savg } 2761258632Savg 2762275811Sdelphij hash_lock = HDR_LOCK(hdr); 2763208373Smm 2764286763Smav /* 2765286763Smav * We aren't calling this function from any code path 2766286763Smav * that would already be holding a hash lock, so we're 2767286763Smav * asserting on this assumption to be defensive in case 2768286763Smav * this ever changes. Without this check, it would be 2769286763Smav * possible to incorrectly increment arcstat_mutex_miss 2770286763Smav * below (e.g. if the code changed such that we called 2771286763Smav * this function with a hash lock held). 2772286763Smav */ 2773286763Smav ASSERT(!MUTEX_HELD(hash_lock)); 2774208373Smm 2775286763Smav if (mutex_tryenter(hash_lock)) { 2776286763Smav uint64_t evicted = arc_evict_hdr(hdr, hash_lock); 2777286763Smav mutex_exit(hash_lock); 2778286763Smav 2779286763Smav bytes_evicted += evicted; 2780286763Smav 2781286763Smav /* 2782286763Smav * If evicted is zero, arc_evict_hdr() must have 2783286763Smav * decided to skip this header, don't increment 2784286763Smav * evict_count in this case. 2785286763Smav */ 2786286763Smav if (evicted != 0) 2787286763Smav evict_count++; 2788286763Smav 2789286763Smav /* 2790286763Smav * If arc_size isn't overflowing, signal any 2791286763Smav * threads that might happen to be waiting. 2792286763Smav * 2793286763Smav * For each header evicted, we wake up a single 2794286763Smav * thread. If we used cv_broadcast, we could 2795286763Smav * wake up "too many" threads causing arc_size 2796286763Smav * to significantly overflow arc_c; since 2797286763Smav * arc_get_data_buf() doesn't check for overflow 2798286763Smav * when it's woken up (it doesn't because it's 2799286763Smav * possible for the ARC to be overflowing while 2800286763Smav * full of un-evictable buffers, and the 2801286763Smav * function should proceed in this case). 2802286763Smav * 2803286763Smav * If threads are left sleeping, due to not 2804286763Smav * using cv_broadcast, they will be woken up 2805286763Smav * just before arc_reclaim_thread() sleeps. 2806286763Smav */ 2807286763Smav mutex_enter(&arc_reclaim_lock); 2808286763Smav if (!arc_is_overflowing()) 2809286763Smav cv_signal(&arc_reclaim_waiters_cv); 2810286763Smav mutex_exit(&arc_reclaim_lock); 2811168404Spjd } else { 2812286763Smav ARCSTAT_BUMP(arcstat_mutex_miss); 2813168404Spjd } 2814168404Spjd } 2815168404Spjd 2816286763Smav multilist_sublist_unlock(mls); 2817206796Spjd 2818286763Smav return (bytes_evicted); 2819286763Smav} 2820168404Spjd 2821286763Smav/* 2822286763Smav * Evict buffers from the given arc state, until we've removed the 2823286763Smav * specified number of bytes. Move the removed buffers to the 2824286763Smav * appropriate evict state. 2825286763Smav * 2826286763Smav * This function makes a "best effort". It skips over any buffers 2827286763Smav * it can't get a hash_lock on, and so, may not catch all candidates. 2828286763Smav * It may also return without evicting as much space as requested. 2829286763Smav * 2830286763Smav * If bytes is specified using the special value ARC_EVICT_ALL, this 2831286763Smav * will evict all available (i.e. unlocked and evictable) buffers from 2832286763Smav * the given arc state; which is used by arc_flush(). 2833286763Smav */ 2834286763Smavstatic uint64_t 2835286763Smavarc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes, 2836286763Smav arc_buf_contents_t type) 2837286763Smav{ 2838286763Smav uint64_t total_evicted = 0; 2839286763Smav multilist_t *ml = &state->arcs_list[type]; 2840286763Smav int num_sublists; 2841286763Smav arc_buf_hdr_t **markers; 2842168404Spjd 2843286763Smav IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); 2844168404Spjd 2845286763Smav num_sublists = multilist_get_num_sublists(ml); 2846286763Smav 2847185029Spjd /* 2848286763Smav * If we've tried to evict from each sublist, made some 2849286763Smav * progress, but still have not hit the target number of bytes 2850286763Smav * to evict, we want to keep trying. The markers allow us to 2851286763Smav * pick up where we left off for each individual sublist, rather 2852286763Smav * than starting from the tail each time. 2853185029Spjd */ 2854286763Smav markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); 2855286763Smav for (int i = 0; i < num_sublists; i++) { 2856286763Smav markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); 2857185029Spjd 2858286763Smav /* 2859286763Smav * A b_spa of 0 is used to indicate that this header is 2860286763Smav * a marker. This fact is used in arc_adjust_type() and 2861286763Smav * arc_evict_state_impl(). 2862286763Smav */ 2863286763Smav markers[i]->b_spa = 0; 2864168404Spjd 2865286763Smav multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 2866286763Smav multilist_sublist_insert_tail(mls, markers[i]); 2867286763Smav multilist_sublist_unlock(mls); 2868286763Smav } 2869168404Spjd 2870286763Smav /* 2871286763Smav * While we haven't hit our target number of bytes to evict, or 2872286763Smav * we're evicting all available buffers. 2873286763Smav */ 2874286763Smav while (total_evicted < bytes || bytes == ARC_EVICT_ALL) { 2875286763Smav /* 2876286763Smav * Start eviction using a randomly selected sublist, 2877286763Smav * this is to try and evenly balance eviction across all 2878286763Smav * sublists. Always starting at the same sublist 2879286763Smav * (e.g. index 0) would cause evictions to favor certain 2880286763Smav * sublists over others. 2881286763Smav */ 2882286763Smav int sublist_idx = multilist_get_random_index(ml); 2883286763Smav uint64_t scan_evicted = 0; 2884219089Spjd 2885286763Smav for (int i = 0; i < num_sublists; i++) { 2886286763Smav uint64_t bytes_remaining; 2887286763Smav uint64_t bytes_evicted; 2888219089Spjd 2889286763Smav if (bytes == ARC_EVICT_ALL) 2890286763Smav bytes_remaining = ARC_EVICT_ALL; 2891286763Smav else if (total_evicted < bytes) 2892286763Smav bytes_remaining = bytes - total_evicted; 2893286763Smav else 2894286763Smav break; 2895258632Savg 2896286763Smav bytes_evicted = arc_evict_state_impl(ml, sublist_idx, 2897286763Smav markers[sublist_idx], spa, bytes_remaining); 2898286763Smav 2899286763Smav scan_evicted += bytes_evicted; 2900286763Smav total_evicted += bytes_evicted; 2901286763Smav 2902286763Smav /* we've reached the end, wrap to the beginning */ 2903286763Smav if (++sublist_idx >= num_sublists) 2904286763Smav sublist_idx = 0; 2905286763Smav } 2906286763Smav 2907258632Savg /* 2908286763Smav * If we didn't evict anything during this scan, we have 2909286763Smav * no reason to believe we'll evict more during another 2910286763Smav * scan, so break the loop. 2911258632Savg */ 2912286763Smav if (scan_evicted == 0) { 2913286763Smav /* This isn't possible, let's make that obvious */ 2914286763Smav ASSERT3S(bytes, !=, 0); 2915185029Spjd 2916286763Smav /* 2917286763Smav * When bytes is ARC_EVICT_ALL, the only way to 2918286763Smav * break the loop is when scan_evicted is zero. 2919286763Smav * In that case, we actually have evicted enough, 2920286763Smav * so we don't want to increment the kstat. 2921286763Smav */ 2922286763Smav if (bytes != ARC_EVICT_ALL) { 2923286763Smav ASSERT3S(total_evicted, <, bytes); 2924286763Smav ARCSTAT_BUMP(arcstat_evict_not_enough); 2925185029Spjd } 2926185029Spjd 2927286763Smav break; 2928258632Savg } 2929286763Smav } 2930258632Savg 2931286763Smav for (int i = 0; i < num_sublists; i++) { 2932286763Smav multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 2933286763Smav multilist_sublist_remove(mls, markers[i]); 2934286763Smav multilist_sublist_unlock(mls); 2935286763Smav 2936286763Smav kmem_cache_free(hdr_full_cache, markers[i]); 2937168404Spjd } 2938286763Smav kmem_free(markers, sizeof (*markers) * num_sublists); 2939206796Spjd 2940286763Smav return (total_evicted); 2941286763Smav} 2942286763Smav 2943286763Smav/* 2944286763Smav * Flush all "evictable" data of the given type from the arc state 2945286763Smav * specified. This will not evict any "active" buffers (i.e. referenced). 2946286763Smav * 2947286763Smav * When 'retry' is set to FALSE, the function will make a single pass 2948286763Smav * over the state and evict any buffers that it can. Since it doesn't 2949286763Smav * continually retry the eviction, it might end up leaving some buffers 2950286763Smav * in the ARC due to lock misses. 2951286763Smav * 2952286763Smav * When 'retry' is set to TRUE, the function will continually retry the 2953286763Smav * eviction until *all* evictable buffers have been removed from the 2954286763Smav * state. As a result, if concurrent insertions into the state are 2955286763Smav * allowed (e.g. if the ARC isn't shutting down), this function might 2956286763Smav * wind up in an infinite loop, continually trying to evict buffers. 2957286763Smav */ 2958286763Smavstatic uint64_t 2959286763Smavarc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, 2960286763Smav boolean_t retry) 2961286763Smav{ 2962286763Smav uint64_t evicted = 0; 2963286763Smav 2964286763Smav while (state->arcs_lsize[type] != 0) { 2965286763Smav evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); 2966286763Smav 2967286763Smav if (!retry) 2968286763Smav break; 2969185029Spjd } 2970185029Spjd 2971286763Smav return (evicted); 2972286763Smav} 2973286763Smav 2974286763Smav/* 2975286763Smav * Evict the specified number of bytes from the state specified, 2976286763Smav * restricting eviction to the spa and type given. This function 2977286763Smav * prevents us from trying to evict more from a state's list than 2978286763Smav * is "evictable", and to skip evicting altogether when passed a 2979286763Smav * negative value for "bytes". In contrast, arc_evict_state() will 2980286763Smav * evict everything it can, when passed a negative value for "bytes". 2981286763Smav */ 2982286763Smavstatic uint64_t 2983286763Smavarc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes, 2984286763Smav arc_buf_contents_t type) 2985286763Smav{ 2986286763Smav int64_t delta; 2987286763Smav 2988286763Smav if (bytes > 0 && state->arcs_lsize[type] > 0) { 2989286763Smav delta = MIN(state->arcs_lsize[type], bytes); 2990286763Smav return (arc_evict_state(state, spa, delta, type)); 2991168404Spjd } 2992168404Spjd 2993286763Smav return (0); 2994168404Spjd} 2995168404Spjd 2996286763Smav/* 2997286763Smav * Evict metadata buffers from the cache, such that arc_meta_used is 2998286763Smav * capped by the arc_meta_limit tunable. 2999286763Smav */ 3000286763Smavstatic uint64_t 3001286763Smavarc_adjust_meta(void) 3002286763Smav{ 3003286763Smav uint64_t total_evicted = 0; 3004286763Smav int64_t target; 3005286763Smav 3006286763Smav /* 3007286763Smav * If we're over the meta limit, we want to evict enough 3008286763Smav * metadata to get back under the meta limit. We don't want to 3009286763Smav * evict so much that we drop the MRU below arc_p, though. If 3010286763Smav * we're over the meta limit more than we're over arc_p, we 3011286763Smav * evict some from the MRU here, and some from the MFU below. 3012286763Smav */ 3013286763Smav target = MIN((int64_t)(arc_meta_used - arc_meta_limit), 3014286766Smav (int64_t)(refcount_count(&arc_anon->arcs_size) + 3015286766Smav refcount_count(&arc_mru->arcs_size) - arc_p)); 3016286763Smav 3017286763Smav total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3018286763Smav 3019286763Smav /* 3020286763Smav * Similar to the above, we want to evict enough bytes to get us 3021286763Smav * below the meta limit, but not so much as to drop us below the 3022286763Smav * space alloted to the MFU (which is defined as arc_c - arc_p). 3023286763Smav */ 3024286763Smav target = MIN((int64_t)(arc_meta_used - arc_meta_limit), 3025286766Smav (int64_t)(refcount_count(&arc_mfu->arcs_size) - (arc_c - arc_p))); 3026286763Smav 3027286763Smav total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3028286763Smav 3029286763Smav return (total_evicted); 3030286763Smav} 3031286763Smav 3032286763Smav/* 3033286763Smav * Return the type of the oldest buffer in the given arc state 3034286763Smav * 3035286763Smav * This function will select a random sublist of type ARC_BUFC_DATA and 3036286763Smav * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist 3037286763Smav * is compared, and the type which contains the "older" buffer will be 3038286763Smav * returned. 3039286763Smav */ 3040286763Smavstatic arc_buf_contents_t 3041286763Smavarc_adjust_type(arc_state_t *state) 3042286763Smav{ 3043286763Smav multilist_t *data_ml = &state->arcs_list[ARC_BUFC_DATA]; 3044286763Smav multilist_t *meta_ml = &state->arcs_list[ARC_BUFC_METADATA]; 3045286763Smav int data_idx = multilist_get_random_index(data_ml); 3046286763Smav int meta_idx = multilist_get_random_index(meta_ml); 3047286763Smav multilist_sublist_t *data_mls; 3048286763Smav multilist_sublist_t *meta_mls; 3049286763Smav arc_buf_contents_t type; 3050286763Smav arc_buf_hdr_t *data_hdr; 3051286763Smav arc_buf_hdr_t *meta_hdr; 3052286763Smav 3053286763Smav /* 3054286763Smav * We keep the sublist lock until we're finished, to prevent 3055286763Smav * the headers from being destroyed via arc_evict_state(). 3056286763Smav */ 3057286763Smav data_mls = multilist_sublist_lock(data_ml, data_idx); 3058286763Smav meta_mls = multilist_sublist_lock(meta_ml, meta_idx); 3059286763Smav 3060286763Smav /* 3061286763Smav * These two loops are to ensure we skip any markers that 3062286763Smav * might be at the tail of the lists due to arc_evict_state(). 3063286763Smav */ 3064286763Smav 3065286763Smav for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; 3066286763Smav data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { 3067286763Smav if (data_hdr->b_spa != 0) 3068286763Smav break; 3069286763Smav } 3070286763Smav 3071286763Smav for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; 3072286763Smav meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { 3073286763Smav if (meta_hdr->b_spa != 0) 3074286763Smav break; 3075286763Smav } 3076286763Smav 3077286763Smav if (data_hdr == NULL && meta_hdr == NULL) { 3078286763Smav type = ARC_BUFC_DATA; 3079286763Smav } else if (data_hdr == NULL) { 3080286763Smav ASSERT3P(meta_hdr, !=, NULL); 3081286763Smav type = ARC_BUFC_METADATA; 3082286763Smav } else if (meta_hdr == NULL) { 3083286763Smav ASSERT3P(data_hdr, !=, NULL); 3084286763Smav type = ARC_BUFC_DATA; 3085286763Smav } else { 3086286763Smav ASSERT3P(data_hdr, !=, NULL); 3087286763Smav ASSERT3P(meta_hdr, !=, NULL); 3088286763Smav 3089286763Smav /* The headers can't be on the sublist without an L1 header */ 3090286763Smav ASSERT(HDR_HAS_L1HDR(data_hdr)); 3091286763Smav ASSERT(HDR_HAS_L1HDR(meta_hdr)); 3092286763Smav 3093286763Smav if (data_hdr->b_l1hdr.b_arc_access < 3094286763Smav meta_hdr->b_l1hdr.b_arc_access) { 3095286763Smav type = ARC_BUFC_DATA; 3096286763Smav } else { 3097286763Smav type = ARC_BUFC_METADATA; 3098286763Smav } 3099286763Smav } 3100286763Smav 3101286763Smav multilist_sublist_unlock(meta_mls); 3102286763Smav multilist_sublist_unlock(data_mls); 3103286763Smav 3104286763Smav return (type); 3105286763Smav} 3106286763Smav 3107286763Smav/* 3108286763Smav * Evict buffers from the cache, such that arc_size is capped by arc_c. 3109286763Smav */ 3110286763Smavstatic uint64_t 3111168404Spjdarc_adjust(void) 3112168404Spjd{ 3113286763Smav uint64_t total_evicted = 0; 3114286763Smav uint64_t bytes; 3115286763Smav int64_t target; 3116168404Spjd 3117208373Smm /* 3118286763Smav * If we're over arc_meta_limit, we want to correct that before 3119286763Smav * potentially evicting data buffers below. 3120286763Smav */ 3121286763Smav total_evicted += arc_adjust_meta(); 3122286763Smav 3123286763Smav /* 3124208373Smm * Adjust MRU size 3125286763Smav * 3126286763Smav * If we're over the target cache size, we want to evict enough 3127286763Smav * from the list to get back to our target size. We don't want 3128286763Smav * to evict too much from the MRU, such that it drops below 3129286763Smav * arc_p. So, if we're over our target cache size more than 3130286763Smav * the MRU is over arc_p, we'll evict enough to get back to 3131286763Smav * arc_p here, and then evict more from the MFU below. 3132208373Smm */ 3133286763Smav target = MIN((int64_t)(arc_size - arc_c), 3134286766Smav (int64_t)(refcount_count(&arc_anon->arcs_size) + 3135286766Smav refcount_count(&arc_mru->arcs_size) + arc_meta_used - arc_p)); 3136208373Smm 3137286763Smav /* 3138286763Smav * If we're below arc_meta_min, always prefer to evict data. 3139286763Smav * Otherwise, try to satisfy the requested number of bytes to 3140286763Smav * evict from the type which contains older buffers; in an 3141286763Smav * effort to keep newer buffers in the cache regardless of their 3142286763Smav * type. If we cannot satisfy the number of bytes from this 3143286763Smav * type, spill over into the next type. 3144286763Smav */ 3145286763Smav if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA && 3146286763Smav arc_meta_used > arc_meta_min) { 3147286763Smav bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3148286763Smav total_evicted += bytes; 3149168404Spjd 3150286763Smav /* 3151286763Smav * If we couldn't evict our target number of bytes from 3152286763Smav * metadata, we try to get the rest from data. 3153286763Smav */ 3154286763Smav target -= bytes; 3155286763Smav 3156286763Smav total_evicted += 3157286763Smav arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3158286763Smav } else { 3159286763Smav bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); 3160286763Smav total_evicted += bytes; 3161286763Smav 3162286763Smav /* 3163286763Smav * If we couldn't evict our target number of bytes from 3164286763Smav * data, we try to get the rest from metadata. 3165286763Smav */ 3166286763Smav target -= bytes; 3167286763Smav 3168286763Smav total_evicted += 3169286763Smav arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); 3170185029Spjd } 3171185029Spjd 3172208373Smm /* 3173208373Smm * Adjust MFU size 3174286763Smav * 3175286763Smav * Now that we've tried to evict enough from the MRU to get its 3176286763Smav * size back to arc_p, if we're still above the target cache 3177286763Smav * size, we evict the rest from the MFU. 3178208373Smm */ 3179286763Smav target = arc_size - arc_c; 3180168404Spjd 3181286764Smav if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA && 3182286763Smav arc_meta_used > arc_meta_min) { 3183286763Smav bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3184286763Smav total_evicted += bytes; 3185208373Smm 3186286763Smav /* 3187286763Smav * If we couldn't evict our target number of bytes from 3188286763Smav * metadata, we try to get the rest from data. 3189286763Smav */ 3190286763Smav target -= bytes; 3191168404Spjd 3192286763Smav total_evicted += 3193286763Smav arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3194286763Smav } else { 3195286763Smav bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); 3196286763Smav total_evicted += bytes; 3197286763Smav 3198286763Smav /* 3199286763Smav * If we couldn't evict our target number of bytes from 3200286763Smav * data, we try to get the rest from data. 3201286763Smav */ 3202286763Smav target -= bytes; 3203286763Smav 3204286763Smav total_evicted += 3205286763Smav arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); 3206208373Smm } 3207168404Spjd 3208208373Smm /* 3209208373Smm * Adjust ghost lists 3210286763Smav * 3211286763Smav * In addition to the above, the ARC also defines target values 3212286763Smav * for the ghost lists. The sum of the mru list and mru ghost 3213286763Smav * list should never exceed the target size of the cache, and 3214286763Smav * the sum of the mru list, mfu list, mru ghost list, and mfu 3215286763Smav * ghost list should never exceed twice the target size of the 3216286763Smav * cache. The following logic enforces these limits on the ghost 3217286763Smav * caches, and evicts from them as needed. 3218208373Smm */ 3219286766Smav target = refcount_count(&arc_mru->arcs_size) + 3220286766Smav refcount_count(&arc_mru_ghost->arcs_size) - arc_c; 3221168404Spjd 3222286763Smav bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); 3223286763Smav total_evicted += bytes; 3224168404Spjd 3225286763Smav target -= bytes; 3226185029Spjd 3227286763Smav total_evicted += 3228286763Smav arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); 3229208373Smm 3230286763Smav /* 3231286763Smav * We assume the sum of the mru list and mfu list is less than 3232286763Smav * or equal to arc_c (we enforced this above), which means we 3233286763Smav * can use the simpler of the two equations below: 3234286763Smav * 3235286763Smav * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c 3236286763Smav * mru ghost + mfu ghost <= arc_c 3237286763Smav */ 3238286766Smav target = refcount_count(&arc_mru_ghost->arcs_size) + 3239286766Smav refcount_count(&arc_mfu_ghost->arcs_size) - arc_c; 3240286763Smav 3241286763Smav bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); 3242286763Smav total_evicted += bytes; 3243286763Smav 3244286763Smav target -= bytes; 3245286763Smav 3246286763Smav total_evicted += 3247286763Smav arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); 3248286763Smav 3249286763Smav return (total_evicted); 3250168404Spjd} 3251168404Spjd 3252168404Spjdstatic void 3253168404Spjdarc_do_user_evicts(void) 3254168404Spjd{ 3255286763Smav mutex_enter(&arc_user_evicts_lock); 3256286762Smav while (arc_eviction_list != NULL) { 3257286762Smav arc_buf_t *buf = arc_eviction_list; 3258286762Smav arc_eviction_list = buf->b_next; 3259219089Spjd mutex_enter(&buf->b_evict_lock); 3260168404Spjd buf->b_hdr = NULL; 3261219089Spjd mutex_exit(&buf->b_evict_lock); 3262286763Smav mutex_exit(&arc_user_evicts_lock); 3263168404Spjd 3264168404Spjd if (buf->b_efunc != NULL) 3265268858Sdelphij VERIFY0(buf->b_efunc(buf->b_private)); 3266168404Spjd 3267168404Spjd buf->b_efunc = NULL; 3268168404Spjd buf->b_private = NULL; 3269168404Spjd kmem_cache_free(buf_cache, buf); 3270286763Smav mutex_enter(&arc_user_evicts_lock); 3271168404Spjd } 3272286763Smav mutex_exit(&arc_user_evicts_lock); 3273168404Spjd} 3274168404Spjd 3275168404Spjdvoid 3276286763Smavarc_flush(spa_t *spa, boolean_t retry) 3277168404Spjd{ 3278209962Smm uint64_t guid = 0; 3279209962Smm 3280286763Smav /* 3281286763Smav * If retry is TRUE, a spa must not be specified since we have 3282286763Smav * no good way to determine if all of a spa's buffers have been 3283286763Smav * evicted from an arc state. 3284286763Smav */ 3285286763Smav ASSERT(!retry || spa == 0); 3286286763Smav 3287286570Smav if (spa != NULL) 3288228103Smm guid = spa_load_guid(spa); 3289209962Smm 3290286763Smav (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); 3291286763Smav (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); 3292168404Spjd 3293286763Smav (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); 3294286763Smav (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); 3295168404Spjd 3296286763Smav (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); 3297286763Smav (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); 3298286763Smav 3299286763Smav (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); 3300286763Smav (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); 3301286763Smav 3302168404Spjd arc_do_user_evicts(); 3303185029Spjd ASSERT(spa || arc_eviction_list == NULL); 3304168404Spjd} 3305168404Spjd 3306168404Spjdvoid 3307286625Smavarc_shrink(int64_t to_free) 3308168404Spjd{ 3309168404Spjd if (arc_c > arc_c_min) { 3310272483Ssmh DTRACE_PROBE4(arc__shrink, uint64_t, arc_c, uint64_t, 3311272483Ssmh arc_c_min, uint64_t, arc_p, uint64_t, to_free); 3312168404Spjd if (arc_c > arc_c_min + to_free) 3313168404Spjd atomic_add_64(&arc_c, -to_free); 3314168404Spjd else 3315168404Spjd arc_c = arc_c_min; 3316168404Spjd 3317168404Spjd atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); 3318168404Spjd if (arc_c > arc_size) 3319168404Spjd arc_c = MAX(arc_size, arc_c_min); 3320168404Spjd if (arc_p > arc_c) 3321168404Spjd arc_p = (arc_c >> 1); 3322272483Ssmh 3323272483Ssmh DTRACE_PROBE2(arc__shrunk, uint64_t, arc_c, uint64_t, 3324272483Ssmh arc_p); 3325272483Ssmh 3326168404Spjd ASSERT(arc_c >= arc_c_min); 3327168404Spjd ASSERT((int64_t)arc_p >= 0); 3328168404Spjd } 3329168404Spjd 3330270759Ssmh if (arc_size > arc_c) { 3331270759Ssmh DTRACE_PROBE2(arc__shrink_adjust, uint64_t, arc_size, 3332270759Ssmh uint64_t, arc_c); 3333286763Smav (void) arc_adjust(); 3334270759Ssmh } 3335168404Spjd} 3336168404Spjd 3337286625Smavstatic long needfree = 0; 3338168404Spjd 3339286625Smavtypedef enum free_memory_reason_t { 3340286625Smav FMR_UNKNOWN, 3341286625Smav FMR_NEEDFREE, 3342286625Smav FMR_LOTSFREE, 3343286625Smav FMR_SWAPFS_MINFREE, 3344286625Smav FMR_PAGES_PP_MAXIMUM, 3345286625Smav FMR_HEAP_ARENA, 3346286625Smav FMR_ZIO_ARENA, 3347286625Smav FMR_ZIO_FRAG, 3348286625Smav} free_memory_reason_t; 3349286625Smav 3350286625Smavint64_t last_free_memory; 3351286625Smavfree_memory_reason_t last_free_reason; 3352286625Smav 3353286625Smav/* 3354286625Smav * Additional reserve of pages for pp_reserve. 3355286625Smav */ 3356286625Smavint64_t arc_pages_pp_reserve = 64; 3357286625Smav 3358286625Smav/* 3359286625Smav * Additional reserve of pages for swapfs. 3360286625Smav */ 3361286625Smavint64_t arc_swapfs_reserve = 64; 3362286625Smav 3363286625Smav/* 3364286625Smav * Return the amount of memory that can be consumed before reclaim will be 3365286625Smav * needed. Positive if there is sufficient free memory, negative indicates 3366286625Smav * the amount of memory that needs to be freed up. 3367286625Smav */ 3368286625Smavstatic int64_t 3369286625Smavarc_available_memory(void) 3370168404Spjd{ 3371286625Smav int64_t lowest = INT64_MAX; 3372286625Smav int64_t n; 3373286625Smav free_memory_reason_t r = FMR_UNKNOWN; 3374168404Spjd 3375168404Spjd#ifdef _KERNEL 3376286625Smav if (needfree > 0) { 3377286625Smav n = PAGESIZE * (-needfree); 3378286625Smav if (n < lowest) { 3379286625Smav lowest = n; 3380286625Smav r = FMR_NEEDFREE; 3381286625Smav } 3382270759Ssmh } 3383168404Spjd 3384191902Skmacy /* 3385212780Savg * Cooperate with pagedaemon when it's time for it to scan 3386212780Savg * and reclaim some pages. 3387191902Skmacy */ 3388286655Smav n = PAGESIZE * ((int64_t)freemem - zfs_arc_free_target); 3389286625Smav if (n < lowest) { 3390286625Smav lowest = n; 3391286625Smav r = FMR_LOTSFREE; 3392270759Ssmh } 3393191902Skmacy 3394277300Ssmh#ifdef illumos 3395168404Spjd /* 3396185029Spjd * check that we're out of range of the pageout scanner. It starts to 3397185029Spjd * schedule paging if freemem is less than lotsfree and needfree. 3398185029Spjd * lotsfree is the high-water mark for pageout, and needfree is the 3399185029Spjd * number of needed free pages. We add extra pages here to make sure 3400185029Spjd * the scanner doesn't start up while we're freeing memory. 3401185029Spjd */ 3402286625Smav n = PAGESIZE * (freemem - lotsfree - needfree - desfree); 3403286625Smav if (n < lowest) { 3404286625Smav lowest = n; 3405286625Smav r = FMR_LOTSFREE; 3406286625Smav } 3407185029Spjd 3408185029Spjd /* 3409168404Spjd * check to make sure that swapfs has enough space so that anon 3410185029Spjd * reservations can still succeed. anon_resvmem() checks that the 3411168404Spjd * availrmem is greater than swapfs_minfree, and the number of reserved 3412168404Spjd * swap pages. We also add a bit of extra here just to prevent 3413168404Spjd * circumstances from getting really dire. 3414168404Spjd */ 3415286625Smav n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve - 3416286625Smav desfree - arc_swapfs_reserve); 3417286625Smav if (n < lowest) { 3418286625Smav lowest = n; 3419286625Smav r = FMR_SWAPFS_MINFREE; 3420286625Smav } 3421168404Spjd 3422286625Smav 3423168404Spjd /* 3424272483Ssmh * Check that we have enough availrmem that memory locking (e.g., via 3425272483Ssmh * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum 3426272483Ssmh * stores the number of pages that cannot be locked; when availrmem 3427272483Ssmh * drops below pages_pp_maximum, page locking mechanisms such as 3428272483Ssmh * page_pp_lock() will fail.) 3429272483Ssmh */ 3430286625Smav n = PAGESIZE * (availrmem - pages_pp_maximum - 3431286625Smav arc_pages_pp_reserve); 3432286625Smav if (n < lowest) { 3433286625Smav lowest = n; 3434286625Smav r = FMR_PAGES_PP_MAXIMUM; 3435286625Smav } 3436272483Ssmh 3437277300Ssmh#endif /* illumos */ 3438272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 3439272483Ssmh /* 3440168404Spjd * If we're on an i386 platform, it's possible that we'll exhaust the 3441168404Spjd * kernel heap space before we ever run out of available physical 3442168404Spjd * memory. Most checks of the size of the heap_area compare against 3443168404Spjd * tune.t_minarmem, which is the minimum available real memory that we 3444168404Spjd * can have in the system. However, this is generally fixed at 25 pages 3445168404Spjd * which is so low that it's useless. In this comparison, we seek to 3446168404Spjd * calculate the total heap-size, and reclaim if more than 3/4ths of the 3447185029Spjd * heap is allocated. (Or, in the calculation, if less than 1/4th is 3448168404Spjd * free) 3449168404Spjd */ 3450286655Smav n = (int64_t)vmem_size(heap_arena, VMEM_FREE) - 3451286628Smav (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2); 3452286625Smav if (n < lowest) { 3453286625Smav lowest = n; 3454286625Smav r = FMR_HEAP_ARENA; 3455270861Ssmh } 3456281026Smav#define zio_arena NULL 3457281026Smav#else 3458281026Smav#define zio_arena heap_arena 3459270861Ssmh#endif 3460281026Smav 3461272483Ssmh /* 3462272483Ssmh * If zio data pages are being allocated out of a separate heap segment, 3463272483Ssmh * then enforce that the size of available vmem for this arena remains 3464272483Ssmh * above about 1/16th free. 3465272483Ssmh * 3466272483Ssmh * Note: The 1/16th arena free requirement was put in place 3467272483Ssmh * to aggressively evict memory from the arc in order to avoid 3468272483Ssmh * memory fragmentation issues. 3469272483Ssmh */ 3470286625Smav if (zio_arena != NULL) { 3471286655Smav n = (int64_t)vmem_size(zio_arena, VMEM_FREE) - 3472286625Smav (vmem_size(zio_arena, VMEM_ALLOC) >> 4); 3473286625Smav if (n < lowest) { 3474286625Smav lowest = n; 3475286625Smav r = FMR_ZIO_ARENA; 3476286625Smav } 3477286625Smav } 3478281026Smav 3479281026Smav /* 3480281026Smav * Above limits know nothing about real level of KVA fragmentation. 3481281026Smav * Start aggressive reclamation if too little sequential KVA left. 3482281026Smav */ 3483286625Smav if (lowest > 0) { 3484286625Smav n = (vmem_size(heap_arena, VMEM_MAXFREE) < zfs_max_recordsize) ? 3485286655Smav -((int64_t)vmem_size(heap_arena, VMEM_ALLOC) >> 4) : 3486286655Smav INT64_MAX; 3487286625Smav if (n < lowest) { 3488286625Smav lowest = n; 3489286625Smav r = FMR_ZIO_FRAG; 3490286625Smav } 3491281109Smav } 3492281026Smav 3493272483Ssmh#else /* _KERNEL */ 3494286625Smav /* Every 100 calls, free a small amount */ 3495168404Spjd if (spa_get_random(100) == 0) 3496286625Smav lowest = -1024; 3497272483Ssmh#endif /* _KERNEL */ 3498270759Ssmh 3499286625Smav last_free_memory = lowest; 3500286625Smav last_free_reason = r; 3501286625Smav DTRACE_PROBE2(arc__available_memory, int64_t, lowest, int, r); 3502286625Smav return (lowest); 3503168404Spjd} 3504168404Spjd 3505286625Smav 3506286625Smav/* 3507286625Smav * Determine if the system is under memory pressure and is asking 3508286625Smav * to reclaim memory. A return value of TRUE indicates that the system 3509286625Smav * is under memory pressure and that the arc should adjust accordingly. 3510286625Smav */ 3511286625Smavstatic boolean_t 3512286625Smavarc_reclaim_needed(void) 3513286625Smav{ 3514286625Smav return (arc_available_memory() < 0); 3515286625Smav} 3516286625Smav 3517208454Spjdextern kmem_cache_t *zio_buf_cache[]; 3518208454Spjdextern kmem_cache_t *zio_data_buf_cache[]; 3519272527Sdelphijextern kmem_cache_t *range_seg_cache; 3520208454Spjd 3521278040Ssmhstatic __noinline void 3522286625Smavarc_kmem_reap_now(void) 3523168404Spjd{ 3524168404Spjd size_t i; 3525168404Spjd kmem_cache_t *prev_cache = NULL; 3526168404Spjd kmem_cache_t *prev_data_cache = NULL; 3527168404Spjd 3528272483Ssmh DTRACE_PROBE(arc__kmem_reap_start); 3529168404Spjd#ifdef _KERNEL 3530185029Spjd if (arc_meta_used >= arc_meta_limit) { 3531185029Spjd /* 3532185029Spjd * We are exceeding our meta-data cache limit. 3533185029Spjd * Purge some DNLC entries to release holds on meta-data. 3534185029Spjd */ 3535185029Spjd dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 3536185029Spjd } 3537168404Spjd#if defined(__i386) 3538168404Spjd /* 3539168404Spjd * Reclaim unused memory from all kmem caches. 3540168404Spjd */ 3541168404Spjd kmem_reap(); 3542168404Spjd#endif 3543168404Spjd#endif 3544168404Spjd 3545168404Spjd for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 3546168404Spjd if (zio_buf_cache[i] != prev_cache) { 3547168404Spjd prev_cache = zio_buf_cache[i]; 3548168404Spjd kmem_cache_reap_now(zio_buf_cache[i]); 3549168404Spjd } 3550168404Spjd if (zio_data_buf_cache[i] != prev_data_cache) { 3551168404Spjd prev_data_cache = zio_data_buf_cache[i]; 3552168404Spjd kmem_cache_reap_now(zio_data_buf_cache[i]); 3553168404Spjd } 3554168404Spjd } 3555168404Spjd kmem_cache_reap_now(buf_cache); 3556286570Smav kmem_cache_reap_now(hdr_full_cache); 3557286570Smav kmem_cache_reap_now(hdr_l2only_cache); 3558272506Sdelphij kmem_cache_reap_now(range_seg_cache); 3559272483Ssmh 3560277300Ssmh#ifdef illumos 3561286625Smav if (zio_arena != NULL) { 3562286625Smav /* 3563286625Smav * Ask the vmem arena to reclaim unused memory from its 3564286625Smav * quantum caches. 3565286625Smav */ 3566272483Ssmh vmem_qcache_reap(zio_arena); 3567286625Smav } 3568272483Ssmh#endif 3569272483Ssmh DTRACE_PROBE(arc__kmem_reap_end); 3570168404Spjd} 3571168404Spjd 3572286763Smav/* 3573286763Smav * Threads can block in arc_get_data_buf() waiting for this thread to evict 3574286763Smav * enough data and signal them to proceed. When this happens, the threads in 3575286763Smav * arc_get_data_buf() are sleeping while holding the hash lock for their 3576286763Smav * particular arc header. Thus, we must be careful to never sleep on a 3577286763Smav * hash lock in this thread. This is to prevent the following deadlock: 3578286763Smav * 3579286763Smav * - Thread A sleeps on CV in arc_get_data_buf() holding hash lock "L", 3580286763Smav * waiting for the reclaim thread to signal it. 3581286763Smav * 3582286763Smav * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter, 3583286763Smav * fails, and goes to sleep forever. 3584286763Smav * 3585286763Smav * This possible deadlock is avoided by always acquiring a hash lock 3586286763Smav * using mutex_tryenter() from arc_reclaim_thread(). 3587286763Smav */ 3588168404Spjdstatic void 3589168404Spjdarc_reclaim_thread(void *dummy __unused) 3590168404Spjd{ 3591168404Spjd clock_t growtime = 0; 3592168404Spjd callb_cpr_t cpr; 3593168404Spjd 3594286763Smav CALLB_CPR_INIT(&cpr, &arc_reclaim_lock, callb_generic_cpr, FTAG); 3595168404Spjd 3596286763Smav mutex_enter(&arc_reclaim_lock); 3597286763Smav while (!arc_reclaim_thread_exit) { 3598286625Smav int64_t free_memory = arc_available_memory(); 3599286763Smav uint64_t evicted = 0; 3600286763Smav 3601286763Smav mutex_exit(&arc_reclaim_lock); 3602286763Smav 3603286625Smav if (free_memory < 0) { 3604168404Spjd 3605286625Smav arc_no_grow = B_TRUE; 3606286625Smav arc_warm = B_TRUE; 3607168404Spjd 3608286625Smav /* 3609286625Smav * Wait at least zfs_grow_retry (default 60) seconds 3610286625Smav * before considering growing. 3611286625Smav */ 3612219089Spjd growtime = ddi_get_lbolt() + (arc_grow_retry * hz); 3613168404Spjd 3614286625Smav arc_kmem_reap_now(); 3615286625Smav 3616286625Smav /* 3617286625Smav * If we are still low on memory, shrink the ARC 3618286625Smav * so that we have arc_shrink_min free space. 3619286625Smav */ 3620286625Smav free_memory = arc_available_memory(); 3621286625Smav 3622286625Smav int64_t to_free = 3623286625Smav (arc_c >> arc_shrink_shift) - free_memory; 3624286625Smav if (to_free > 0) { 3625286625Smav#ifdef _KERNEL 3626286625Smav to_free = MAX(to_free, ptob(needfree)); 3627286625Smav#endif 3628286625Smav arc_shrink(to_free); 3629168404Spjd } 3630286625Smav } else if (free_memory < arc_c >> arc_no_grow_shift) { 3631286625Smav arc_no_grow = B_TRUE; 3632286625Smav } else if (ddi_get_lbolt() >= growtime) { 3633286625Smav arc_no_grow = B_FALSE; 3634168404Spjd } 3635168404Spjd 3636286763Smav evicted = arc_adjust(); 3637168404Spjd 3638286763Smav mutex_enter(&arc_reclaim_lock); 3639168404Spjd 3640286763Smav /* 3641286763Smav * If evicted is zero, we couldn't evict anything via 3642286763Smav * arc_adjust(). This could be due to hash lock 3643286763Smav * collisions, but more likely due to the majority of 3644286763Smav * arc buffers being unevictable. Therefore, even if 3645286763Smav * arc_size is above arc_c, another pass is unlikely to 3646286763Smav * be helpful and could potentially cause us to enter an 3647286763Smav * infinite loop. 3648286763Smav */ 3649286763Smav if (arc_size <= arc_c || evicted == 0) { 3650211762Savg#ifdef _KERNEL 3651185029Spjd needfree = 0; 3652168404Spjd#endif 3653286763Smav /* 3654286763Smav * We're either no longer overflowing, or we 3655286763Smav * can't evict anything more, so we should wake 3656286763Smav * up any threads before we go to sleep. 3657286763Smav */ 3658286763Smav cv_broadcast(&arc_reclaim_waiters_cv); 3659168404Spjd 3660286763Smav /* 3661286763Smav * Block until signaled, or after one second (we 3662286763Smav * might need to perform arc_kmem_reap_now() 3663286763Smav * even if we aren't being signalled) 3664286763Smav */ 3665286763Smav CALLB_CPR_SAFE_BEGIN(&cpr); 3666286763Smav (void) cv_timedwait(&arc_reclaim_thread_cv, 3667286763Smav &arc_reclaim_lock, hz); 3668286763Smav CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_lock); 3669286763Smav } 3670286763Smav } 3671286763Smav 3672286763Smav arc_reclaim_thread_exit = FALSE; 3673286763Smav cv_broadcast(&arc_reclaim_thread_cv); 3674286763Smav CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_lock */ 3675286763Smav thread_exit(); 3676286763Smav} 3677286763Smav 3678286763Smavstatic void 3679286763Smavarc_user_evicts_thread(void *dummy __unused) 3680286763Smav{ 3681286763Smav callb_cpr_t cpr; 3682286763Smav 3683286763Smav CALLB_CPR_INIT(&cpr, &arc_user_evicts_lock, callb_generic_cpr, FTAG); 3684286763Smav 3685286763Smav mutex_enter(&arc_user_evicts_lock); 3686286763Smav while (!arc_user_evicts_thread_exit) { 3687286763Smav mutex_exit(&arc_user_evicts_lock); 3688286763Smav 3689286763Smav arc_do_user_evicts(); 3690286763Smav 3691286574Smav /* 3692286574Smav * This is necessary in order for the mdb ::arc dcmd to 3693286574Smav * show up to date information. Since the ::arc command 3694286574Smav * does not call the kstat's update function, without 3695286574Smav * this call, the command may show stale stats for the 3696286574Smav * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even 3697286574Smav * with this change, the data might be up to 1 second 3698286574Smav * out of date; but that should suffice. The arc_state_t 3699286574Smav * structures can be queried directly if more accurate 3700286574Smav * information is needed. 3701286574Smav */ 3702286574Smav if (arc_ksp != NULL) 3703286574Smav arc_ksp->ks_update(arc_ksp, KSTAT_READ); 3704286574Smav 3705286763Smav mutex_enter(&arc_user_evicts_lock); 3706286763Smav 3707286763Smav /* 3708286763Smav * Block until signaled, or after one second (we need to 3709286763Smav * call the arc's kstat update function regularly). 3710286763Smav */ 3711168404Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 3712286763Smav (void) cv_timedwait(&arc_user_evicts_cv, 3713286763Smav &arc_user_evicts_lock, hz); 3714286763Smav CALLB_CPR_SAFE_END(&cpr, &arc_user_evicts_lock); 3715168404Spjd } 3716168404Spjd 3717286763Smav arc_user_evicts_thread_exit = FALSE; 3718286763Smav cv_broadcast(&arc_user_evicts_cv); 3719286763Smav CALLB_CPR_EXIT(&cpr); /* drops arc_user_evicts_lock */ 3720168404Spjd thread_exit(); 3721168404Spjd} 3722168404Spjd 3723168404Spjd/* 3724168404Spjd * Adapt arc info given the number of bytes we are trying to add and 3725168404Spjd * the state that we are comming from. This function is only called 3726168404Spjd * when we are adding new content to the cache. 3727168404Spjd */ 3728168404Spjdstatic void 3729168404Spjdarc_adapt(int bytes, arc_state_t *state) 3730168404Spjd{ 3731168404Spjd int mult; 3732208373Smm uint64_t arc_p_min = (arc_c >> arc_p_min_shift); 3733286766Smav int64_t mrug_size = refcount_count(&arc_mru_ghost->arcs_size); 3734286766Smav int64_t mfug_size = refcount_count(&arc_mfu_ghost->arcs_size); 3735168404Spjd 3736185029Spjd if (state == arc_l2c_only) 3737185029Spjd return; 3738185029Spjd 3739168404Spjd ASSERT(bytes > 0); 3740168404Spjd /* 3741168404Spjd * Adapt the target size of the MRU list: 3742168404Spjd * - if we just hit in the MRU ghost list, then increase 3743168404Spjd * the target size of the MRU list. 3744168404Spjd * - if we just hit in the MFU ghost list, then increase 3745168404Spjd * the target size of the MFU list by decreasing the 3746168404Spjd * target size of the MRU list. 3747168404Spjd */ 3748168404Spjd if (state == arc_mru_ghost) { 3749286766Smav mult = (mrug_size >= mfug_size) ? 1 : (mfug_size / mrug_size); 3750209275Smm mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ 3751168404Spjd 3752208373Smm arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); 3753168404Spjd } else if (state == arc_mfu_ghost) { 3754208373Smm uint64_t delta; 3755208373Smm 3756286766Smav mult = (mfug_size >= mrug_size) ? 1 : (mrug_size / mfug_size); 3757209275Smm mult = MIN(mult, 10); 3758168404Spjd 3759208373Smm delta = MIN(bytes * mult, arc_p); 3760208373Smm arc_p = MAX(arc_p_min, arc_p - delta); 3761168404Spjd } 3762168404Spjd ASSERT((int64_t)arc_p >= 0); 3763168404Spjd 3764168404Spjd if (arc_reclaim_needed()) { 3765286763Smav cv_signal(&arc_reclaim_thread_cv); 3766168404Spjd return; 3767168404Spjd } 3768168404Spjd 3769168404Spjd if (arc_no_grow) 3770168404Spjd return; 3771168404Spjd 3772168404Spjd if (arc_c >= arc_c_max) 3773168404Spjd return; 3774168404Spjd 3775168404Spjd /* 3776168404Spjd * If we're within (2 * maxblocksize) bytes of the target 3777168404Spjd * cache size, increment the target cache size 3778168404Spjd */ 3779168404Spjd if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { 3780272483Ssmh DTRACE_PROBE1(arc__inc_adapt, int, bytes); 3781168404Spjd atomic_add_64(&arc_c, (int64_t)bytes); 3782168404Spjd if (arc_c > arc_c_max) 3783168404Spjd arc_c = arc_c_max; 3784168404Spjd else if (state == arc_anon) 3785168404Spjd atomic_add_64(&arc_p, (int64_t)bytes); 3786168404Spjd if (arc_p > arc_c) 3787168404Spjd arc_p = arc_c; 3788168404Spjd } 3789168404Spjd ASSERT((int64_t)arc_p >= 0); 3790168404Spjd} 3791168404Spjd 3792168404Spjd/* 3793286763Smav * Check if arc_size has grown past our upper threshold, determined by 3794286763Smav * zfs_arc_overflow_shift. 3795168404Spjd */ 3796286763Smavstatic boolean_t 3797286763Smavarc_is_overflowing(void) 3798168404Spjd{ 3799286763Smav /* Always allow at least one block of overflow */ 3800286763Smav uint64_t overflow = MAX(SPA_MAXBLOCKSIZE, 3801286763Smav arc_c >> zfs_arc_overflow_shift); 3802185029Spjd 3803286763Smav return (arc_size >= arc_c + overflow); 3804168404Spjd} 3805168404Spjd 3806168404Spjd/* 3807286763Smav * The buffer, supplied as the first argument, needs a data block. If we 3808286763Smav * are hitting the hard limit for the cache size, we must sleep, waiting 3809286763Smav * for the eviction thread to catch up. If we're past the target size 3810286763Smav * but below the hard limit, we'll only signal the reclaim thread and 3811286763Smav * continue on. 3812168404Spjd */ 3813168404Spjdstatic void 3814168404Spjdarc_get_data_buf(arc_buf_t *buf) 3815168404Spjd{ 3816286570Smav arc_state_t *state = buf->b_hdr->b_l1hdr.b_state; 3817168404Spjd uint64_t size = buf->b_hdr->b_size; 3818286570Smav arc_buf_contents_t type = arc_buf_type(buf->b_hdr); 3819168404Spjd 3820168404Spjd arc_adapt(size, state); 3821168404Spjd 3822168404Spjd /* 3823286763Smav * If arc_size is currently overflowing, and has grown past our 3824286763Smav * upper limit, we must be adding data faster than the evict 3825286763Smav * thread can evict. Thus, to ensure we don't compound the 3826286763Smav * problem by adding more data and forcing arc_size to grow even 3827286763Smav * further past it's target size, we halt and wait for the 3828286763Smav * eviction thread to catch up. 3829286763Smav * 3830286763Smav * It's also possible that the reclaim thread is unable to evict 3831286763Smav * enough buffers to get arc_size below the overflow limit (e.g. 3832286763Smav * due to buffers being un-evictable, or hash lock collisions). 3833286763Smav * In this case, we want to proceed regardless if we're 3834286763Smav * overflowing; thus we don't use a while loop here. 3835168404Spjd */ 3836286763Smav if (arc_is_overflowing()) { 3837286763Smav mutex_enter(&arc_reclaim_lock); 3838286763Smav 3839286763Smav /* 3840286763Smav * Now that we've acquired the lock, we may no longer be 3841286763Smav * over the overflow limit, lets check. 3842286763Smav * 3843286763Smav * We're ignoring the case of spurious wake ups. If that 3844286763Smav * were to happen, it'd let this thread consume an ARC 3845286763Smav * buffer before it should have (i.e. before we're under 3846286763Smav * the overflow limit and were signalled by the reclaim 3847286763Smav * thread). As long as that is a rare occurrence, it 3848286763Smav * shouldn't cause any harm. 3849286763Smav */ 3850286763Smav if (arc_is_overflowing()) { 3851286763Smav cv_signal(&arc_reclaim_thread_cv); 3852286763Smav cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); 3853168404Spjd } 3854286763Smav 3855286763Smav mutex_exit(&arc_reclaim_lock); 3856168404Spjd } 3857168404Spjd 3858286763Smav if (type == ARC_BUFC_METADATA) { 3859286763Smav buf->b_data = zio_buf_alloc(size); 3860286763Smav arc_space_consume(size, ARC_SPACE_META); 3861168404Spjd } else { 3862286763Smav ASSERT(type == ARC_BUFC_DATA); 3863286763Smav buf->b_data = zio_data_buf_alloc(size); 3864286763Smav arc_space_consume(size, ARC_SPACE_DATA); 3865168404Spjd } 3866286763Smav 3867168404Spjd /* 3868168404Spjd * Update the state size. Note that ghost states have a 3869168404Spjd * "ghost size" and so don't need to be updated. 3870168404Spjd */ 3871286570Smav if (!GHOST_STATE(buf->b_hdr->b_l1hdr.b_state)) { 3872168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 3873286766Smav arc_state_t *state = hdr->b_l1hdr.b_state; 3874168404Spjd 3875286766Smav (void) refcount_add_many(&state->arcs_size, size, buf); 3876286763Smav 3877286763Smav /* 3878286763Smav * If this is reached via arc_read, the link is 3879286763Smav * protected by the hash lock. If reached via 3880286763Smav * arc_buf_alloc, the header should not be accessed by 3881286763Smav * any other thread. And, if reached via arc_read_done, 3882286763Smav * the hash lock will protect it if it's found in the 3883286763Smav * hash table; otherwise no other thread should be 3884286763Smav * trying to [add|remove]_reference it. 3885286763Smav */ 3886286763Smav if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 3887286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 3888286570Smav atomic_add_64(&hdr->b_l1hdr.b_state->arcs_lsize[type], 3889286570Smav size); 3890168404Spjd } 3891168404Spjd /* 3892168404Spjd * If we are growing the cache, and we are adding anonymous 3893168404Spjd * data, and we have outgrown arc_p, update arc_p 3894168404Spjd */ 3895286570Smav if (arc_size < arc_c && hdr->b_l1hdr.b_state == arc_anon && 3896286766Smav (refcount_count(&arc_anon->arcs_size) + 3897286766Smav refcount_count(&arc_mru->arcs_size) > arc_p)) 3898168404Spjd arc_p = MIN(arc_c, arc_p + size); 3899168404Spjd } 3900205231Skmacy ARCSTAT_BUMP(arcstat_allocated); 3901168404Spjd} 3902168404Spjd 3903168404Spjd/* 3904168404Spjd * This routine is called whenever a buffer is accessed. 3905168404Spjd * NOTE: the hash lock is dropped in this function. 3906168404Spjd */ 3907168404Spjdstatic void 3908275811Sdelphijarc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) 3909168404Spjd{ 3910219089Spjd clock_t now; 3911219089Spjd 3912168404Spjd ASSERT(MUTEX_HELD(hash_lock)); 3913286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 3914168404Spjd 3915286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 3916168404Spjd /* 3917168404Spjd * This buffer is not in the cache, and does not 3918168404Spjd * appear in our "ghost" list. Add the new buffer 3919168404Spjd * to the MRU state. 3920168404Spjd */ 3921168404Spjd 3922286570Smav ASSERT0(hdr->b_l1hdr.b_arc_access); 3923286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3924275811Sdelphij DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 3925275811Sdelphij arc_change_state(arc_mru, hdr, hash_lock); 3926168404Spjd 3927286570Smav } else if (hdr->b_l1hdr.b_state == arc_mru) { 3928219089Spjd now = ddi_get_lbolt(); 3929219089Spjd 3930168404Spjd /* 3931168404Spjd * If this buffer is here because of a prefetch, then either: 3932168404Spjd * - clear the flag if this is a "referencing" read 3933168404Spjd * (any subsequent access will bump this into the MFU state). 3934168404Spjd * or 3935168404Spjd * - move the buffer to the head of the list if this is 3936168404Spjd * another prefetch (to make it less likely to be evicted). 3937168404Spjd */ 3938286570Smav if (HDR_PREFETCH(hdr)) { 3939286570Smav if (refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 3940286763Smav /* link protected by hash lock */ 3941286763Smav ASSERT(multilist_link_active( 3942286570Smav &hdr->b_l1hdr.b_arc_node)); 3943168404Spjd } else { 3944275811Sdelphij hdr->b_flags &= ~ARC_FLAG_PREFETCH; 3945168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 3946168404Spjd } 3947286570Smav hdr->b_l1hdr.b_arc_access = now; 3948168404Spjd return; 3949168404Spjd } 3950168404Spjd 3951168404Spjd /* 3952168404Spjd * This buffer has been "accessed" only once so far, 3953168404Spjd * but it is still in the cache. Move it to the MFU 3954168404Spjd * state. 3955168404Spjd */ 3956286570Smav if (now > hdr->b_l1hdr.b_arc_access + ARC_MINTIME) { 3957168404Spjd /* 3958168404Spjd * More than 125ms have passed since we 3959168404Spjd * instantiated this buffer. Move it to the 3960168404Spjd * most frequently used state. 3961168404Spjd */ 3962286570Smav hdr->b_l1hdr.b_arc_access = now; 3963275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3964275811Sdelphij arc_change_state(arc_mfu, hdr, hash_lock); 3965168404Spjd } 3966168404Spjd ARCSTAT_BUMP(arcstat_mru_hits); 3967286570Smav } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { 3968168404Spjd arc_state_t *new_state; 3969168404Spjd /* 3970168404Spjd * This buffer has been "accessed" recently, but 3971168404Spjd * was evicted from the cache. Move it to the 3972168404Spjd * MFU state. 3973168404Spjd */ 3974168404Spjd 3975286570Smav if (HDR_PREFETCH(hdr)) { 3976168404Spjd new_state = arc_mru; 3977286570Smav if (refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) 3978275811Sdelphij hdr->b_flags &= ~ARC_FLAG_PREFETCH; 3979275811Sdelphij DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 3980168404Spjd } else { 3981168404Spjd new_state = arc_mfu; 3982275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 3983168404Spjd } 3984168404Spjd 3985286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 3986275811Sdelphij arc_change_state(new_state, hdr, hash_lock); 3987168404Spjd 3988168404Spjd ARCSTAT_BUMP(arcstat_mru_ghost_hits); 3989286570Smav } else if (hdr->b_l1hdr.b_state == arc_mfu) { 3990168404Spjd /* 3991168404Spjd * This buffer has been accessed more than once and is 3992168404Spjd * still in the cache. Keep it in the MFU state. 3993168404Spjd * 3994168404Spjd * NOTE: an add_reference() that occurred when we did 3995168404Spjd * the arc_read() will have kicked this off the list. 3996168404Spjd * If it was a prefetch, we will explicitly move it to 3997168404Spjd * the head of the list now. 3998168404Spjd */ 3999286570Smav if ((HDR_PREFETCH(hdr)) != 0) { 4000286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4001286763Smav /* link protected by hash_lock */ 4002286763Smav ASSERT(multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 4003168404Spjd } 4004168404Spjd ARCSTAT_BUMP(arcstat_mfu_hits); 4005286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4006286570Smav } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { 4007168404Spjd arc_state_t *new_state = arc_mfu; 4008168404Spjd /* 4009168404Spjd * This buffer has been accessed more than once but has 4010168404Spjd * been evicted from the cache. Move it back to the 4011168404Spjd * MFU state. 4012168404Spjd */ 4013168404Spjd 4014286570Smav if (HDR_PREFETCH(hdr)) { 4015168404Spjd /* 4016168404Spjd * This is a prefetch access... 4017168404Spjd * move this block back to the MRU state. 4018168404Spjd */ 4019286570Smav ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); 4020168404Spjd new_state = arc_mru; 4021168404Spjd } 4022168404Spjd 4023286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4024275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4025275811Sdelphij arc_change_state(new_state, hdr, hash_lock); 4026168404Spjd 4027168404Spjd ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 4028286570Smav } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { 4029185029Spjd /* 4030185029Spjd * This buffer is on the 2nd Level ARC. 4031185029Spjd */ 4032185029Spjd 4033286570Smav hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); 4034275811Sdelphij DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 4035275811Sdelphij arc_change_state(arc_mfu, hdr, hash_lock); 4036168404Spjd } else { 4037168404Spjd ASSERT(!"invalid arc state"); 4038168404Spjd } 4039168404Spjd} 4040168404Spjd 4041168404Spjd/* a generic arc_done_func_t which you can use */ 4042168404Spjd/* ARGSUSED */ 4043168404Spjdvoid 4044168404Spjdarc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 4045168404Spjd{ 4046219089Spjd if (zio == NULL || zio->io_error == 0) 4047219089Spjd bcopy(buf->b_data, arg, buf->b_hdr->b_size); 4048248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 4049168404Spjd} 4050168404Spjd 4051185029Spjd/* a generic arc_done_func_t */ 4052168404Spjdvoid 4053168404Spjdarc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 4054168404Spjd{ 4055168404Spjd arc_buf_t **bufp = arg; 4056168404Spjd if (zio && zio->io_error) { 4057248571Smm VERIFY(arc_buf_remove_ref(buf, arg)); 4058168404Spjd *bufp = NULL; 4059168404Spjd } else { 4060168404Spjd *bufp = buf; 4061219089Spjd ASSERT(buf->b_data); 4062168404Spjd } 4063168404Spjd} 4064168404Spjd 4065168404Spjdstatic void 4066168404Spjdarc_read_done(zio_t *zio) 4067168404Spjd{ 4068268075Sdelphij arc_buf_hdr_t *hdr; 4069168404Spjd arc_buf_t *buf; 4070168404Spjd arc_buf_t *abuf; /* buffer we're assigning to callback */ 4071268075Sdelphij kmutex_t *hash_lock = NULL; 4072168404Spjd arc_callback_t *callback_list, *acb; 4073168404Spjd int freeable = FALSE; 4074168404Spjd 4075168404Spjd buf = zio->io_private; 4076168404Spjd hdr = buf->b_hdr; 4077168404Spjd 4078168404Spjd /* 4079168404Spjd * The hdr was inserted into hash-table and removed from lists 4080168404Spjd * prior to starting I/O. We should find this header, since 4081168404Spjd * it's in the hash table, and it should be legit since it's 4082168404Spjd * not possible to evict it during the I/O. The only possible 4083168404Spjd * reason for it not to be found is if we were freed during the 4084168404Spjd * read. 4085168404Spjd */ 4086268075Sdelphij if (HDR_IN_HASH_TABLE(hdr)) { 4087268075Sdelphij ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 4088268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[0], ==, 4089268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[0]); 4090268075Sdelphij ASSERT3U(hdr->b_dva.dva_word[1], ==, 4091268075Sdelphij BP_IDENTITY(zio->io_bp)->dva_word[1]); 4092168404Spjd 4093268075Sdelphij arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp, 4094268075Sdelphij &hash_lock); 4095168404Spjd 4096268075Sdelphij ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && 4097268075Sdelphij hash_lock == NULL) || 4098268075Sdelphij (found == hdr && 4099268075Sdelphij DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 4100268075Sdelphij (found == hdr && HDR_L2_READING(hdr))); 4101268075Sdelphij } 4102268075Sdelphij 4103275811Sdelphij hdr->b_flags &= ~ARC_FLAG_L2_EVICTED; 4104286570Smav if (l2arc_noprefetch && HDR_PREFETCH(hdr)) 4105275811Sdelphij hdr->b_flags &= ~ARC_FLAG_L2CACHE; 4106206796Spjd 4107168404Spjd /* byteswap if necessary */ 4108286570Smav callback_list = hdr->b_l1hdr.b_acb; 4109168404Spjd ASSERT(callback_list != NULL); 4110209101Smm if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { 4111236884Smm dmu_object_byteswap_t bswap = 4112236884Smm DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 4113185029Spjd arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ? 4114185029Spjd byteswap_uint64_array : 4115236884Smm dmu_ot_byteswap[bswap].ob_func; 4116185029Spjd func(buf->b_data, hdr->b_size); 4117185029Spjd } 4118168404Spjd 4119185029Spjd arc_cksum_compute(buf, B_FALSE); 4120240133Smm#ifdef illumos 4121240133Smm arc_buf_watch(buf); 4122277300Ssmh#endif 4123168404Spjd 4124286570Smav if (hash_lock && zio->io_error == 0 && 4125286570Smav hdr->b_l1hdr.b_state == arc_anon) { 4126219089Spjd /* 4127219089Spjd * Only call arc_access on anonymous buffers. This is because 4128219089Spjd * if we've issued an I/O for an evicted buffer, we've already 4129219089Spjd * called arc_access (to prevent any simultaneous readers from 4130219089Spjd * getting confused). 4131219089Spjd */ 4132219089Spjd arc_access(hdr, hash_lock); 4133219089Spjd } 4134219089Spjd 4135168404Spjd /* create copies of the data buffer for the callers */ 4136168404Spjd abuf = buf; 4137168404Spjd for (acb = callback_list; acb; acb = acb->acb_next) { 4138168404Spjd if (acb->acb_done) { 4139242845Sdelphij if (abuf == NULL) { 4140242845Sdelphij ARCSTAT_BUMP(arcstat_duplicate_reads); 4141168404Spjd abuf = arc_buf_clone(buf); 4142242845Sdelphij } 4143168404Spjd acb->acb_buf = abuf; 4144168404Spjd abuf = NULL; 4145168404Spjd } 4146168404Spjd } 4147286570Smav hdr->b_l1hdr.b_acb = NULL; 4148275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 4149168404Spjd ASSERT(!HDR_BUF_AVAILABLE(hdr)); 4150219089Spjd if (abuf == buf) { 4151219089Spjd ASSERT(buf->b_efunc == NULL); 4152286570Smav ASSERT(hdr->b_l1hdr.b_datacnt == 1); 4153275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 4154219089Spjd } 4155168404Spjd 4156286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || 4157286570Smav callback_list != NULL); 4158168404Spjd 4159168404Spjd if (zio->io_error != 0) { 4160275811Sdelphij hdr->b_flags |= ARC_FLAG_IO_ERROR; 4161286570Smav if (hdr->b_l1hdr.b_state != arc_anon) 4162168404Spjd arc_change_state(arc_anon, hdr, hash_lock); 4163168404Spjd if (HDR_IN_HASH_TABLE(hdr)) 4164168404Spjd buf_hash_remove(hdr); 4165286570Smav freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4166168404Spjd } 4167168404Spjd 4168168404Spjd /* 4169168404Spjd * Broadcast before we drop the hash_lock to avoid the possibility 4170168404Spjd * that the hdr (and hence the cv) might be freed before we get to 4171168404Spjd * the cv_broadcast(). 4172168404Spjd */ 4173286570Smav cv_broadcast(&hdr->b_l1hdr.b_cv); 4174168404Spjd 4175286570Smav if (hash_lock != NULL) { 4176168404Spjd mutex_exit(hash_lock); 4177168404Spjd } else { 4178168404Spjd /* 4179168404Spjd * This block was freed while we waited for the read to 4180168404Spjd * complete. It has been removed from the hash table and 4181168404Spjd * moved to the anonymous state (so that it won't show up 4182168404Spjd * in the cache). 4183168404Spjd */ 4184286570Smav ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 4185286570Smav freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); 4186168404Spjd } 4187168404Spjd 4188168404Spjd /* execute each callback and free its structure */ 4189168404Spjd while ((acb = callback_list) != NULL) { 4190168404Spjd if (acb->acb_done) 4191168404Spjd acb->acb_done(zio, acb->acb_buf, acb->acb_private); 4192168404Spjd 4193168404Spjd if (acb->acb_zio_dummy != NULL) { 4194168404Spjd acb->acb_zio_dummy->io_error = zio->io_error; 4195168404Spjd zio_nowait(acb->acb_zio_dummy); 4196168404Spjd } 4197168404Spjd 4198168404Spjd callback_list = acb->acb_next; 4199168404Spjd kmem_free(acb, sizeof (arc_callback_t)); 4200168404Spjd } 4201168404Spjd 4202168404Spjd if (freeable) 4203168404Spjd arc_hdr_destroy(hdr); 4204168404Spjd} 4205168404Spjd 4206168404Spjd/* 4207286762Smav * "Read" the block at the specified DVA (in bp) via the 4208168404Spjd * cache. If the block is found in the cache, invoke the provided 4209168404Spjd * callback immediately and return. Note that the `zio' parameter 4210168404Spjd * in the callback will be NULL in this case, since no IO was 4211168404Spjd * required. If the block is not in the cache pass the read request 4212168404Spjd * on to the spa with a substitute callback function, so that the 4213168404Spjd * requested block will be added to the cache. 4214168404Spjd * 4215168404Spjd * If a read request arrives for a block that has a read in-progress, 4216168404Spjd * either wait for the in-progress read to complete (and return the 4217168404Spjd * results); or, if this is a read with a "done" func, add a record 4218168404Spjd * to the read to invoke the "done" func when the read completes, 4219168404Spjd * and return; or just return. 4220168404Spjd * 4221168404Spjd * arc_read_done() will invoke all the requested "done" functions 4222168404Spjd * for readers of this block. 4223168404Spjd */ 4224168404Spjdint 4225246666Smmarc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, 4226275811Sdelphij void *private, zio_priority_t priority, int zio_flags, 4227275811Sdelphij arc_flags_t *arc_flags, const zbookmark_phys_t *zb) 4228168404Spjd{ 4229268075Sdelphij arc_buf_hdr_t *hdr = NULL; 4230247187Smm arc_buf_t *buf = NULL; 4231268075Sdelphij kmutex_t *hash_lock = NULL; 4232185029Spjd zio_t *rzio; 4233228103Smm uint64_t guid = spa_load_guid(spa); 4234168404Spjd 4235268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp) || 4236268075Sdelphij BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 4237268075Sdelphij 4238168404Spjdtop: 4239268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 4240268075Sdelphij /* 4241268075Sdelphij * Embedded BP's have no DVA and require no I/O to "read". 4242268075Sdelphij * Create an anonymous arc buf to back it. 4243268075Sdelphij */ 4244268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 4245268075Sdelphij } 4246168404Spjd 4247286570Smav if (hdr != NULL && HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_datacnt > 0) { 4248268075Sdelphij 4249275811Sdelphij *arc_flags |= ARC_FLAG_CACHED; 4250168404Spjd 4251168404Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 4252168404Spjd 4253275811Sdelphij if (*arc_flags & ARC_FLAG_WAIT) { 4254286570Smav cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); 4255168404Spjd mutex_exit(hash_lock); 4256168404Spjd goto top; 4257168404Spjd } 4258275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 4259168404Spjd 4260168404Spjd if (done) { 4261168404Spjd arc_callback_t *acb = NULL; 4262168404Spjd 4263168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), 4264168404Spjd KM_SLEEP); 4265168404Spjd acb->acb_done = done; 4266168404Spjd acb->acb_private = private; 4267168404Spjd if (pio != NULL) 4268168404Spjd acb->acb_zio_dummy = zio_null(pio, 4269209962Smm spa, NULL, NULL, NULL, zio_flags); 4270168404Spjd 4271168404Spjd ASSERT(acb->acb_done != NULL); 4272286570Smav acb->acb_next = hdr->b_l1hdr.b_acb; 4273286570Smav hdr->b_l1hdr.b_acb = acb; 4274168404Spjd add_reference(hdr, hash_lock, private); 4275168404Spjd mutex_exit(hash_lock); 4276168404Spjd return (0); 4277168404Spjd } 4278168404Spjd mutex_exit(hash_lock); 4279168404Spjd return (0); 4280168404Spjd } 4281168404Spjd 4282286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_mru || 4283286570Smav hdr->b_l1hdr.b_state == arc_mfu); 4284168404Spjd 4285168404Spjd if (done) { 4286168404Spjd add_reference(hdr, hash_lock, private); 4287168404Spjd /* 4288168404Spjd * If this block is already in use, create a new 4289168404Spjd * copy of the data so that we will be guaranteed 4290168404Spjd * that arc_release() will always succeed. 4291168404Spjd */ 4292286570Smav buf = hdr->b_l1hdr.b_buf; 4293168404Spjd ASSERT(buf); 4294168404Spjd ASSERT(buf->b_data); 4295168404Spjd if (HDR_BUF_AVAILABLE(hdr)) { 4296168404Spjd ASSERT(buf->b_efunc == NULL); 4297275811Sdelphij hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 4298168404Spjd } else { 4299168404Spjd buf = arc_buf_clone(buf); 4300168404Spjd } 4301219089Spjd 4302275811Sdelphij } else if (*arc_flags & ARC_FLAG_PREFETCH && 4303286570Smav refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { 4304275811Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 4305168404Spjd } 4306168404Spjd DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 4307168404Spjd arc_access(hdr, hash_lock); 4308275811Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 4309275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 4310275811Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 4311275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4312168404Spjd mutex_exit(hash_lock); 4313168404Spjd ARCSTAT_BUMP(arcstat_hits); 4314286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 4315286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 4316168404Spjd data, metadata, hits); 4317168404Spjd 4318168404Spjd if (done) 4319168404Spjd done(NULL, buf, private); 4320168404Spjd } else { 4321168404Spjd uint64_t size = BP_GET_LSIZE(bp); 4322268075Sdelphij arc_callback_t *acb; 4323185029Spjd vdev_t *vd = NULL; 4324247187Smm uint64_t addr = 0; 4325208373Smm boolean_t devw = B_FALSE; 4326258389Savg enum zio_compress b_compress = ZIO_COMPRESS_OFF; 4327286570Smav int32_t b_asize = 0; 4328168404Spjd 4329168404Spjd if (hdr == NULL) { 4330168404Spjd /* this block is not in the cache */ 4331268075Sdelphij arc_buf_hdr_t *exists = NULL; 4332168404Spjd arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 4333168404Spjd buf = arc_buf_alloc(spa, size, private, type); 4334168404Spjd hdr = buf->b_hdr; 4335268075Sdelphij if (!BP_IS_EMBEDDED(bp)) { 4336268075Sdelphij hdr->b_dva = *BP_IDENTITY(bp); 4337268075Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 4338268075Sdelphij exists = buf_hash_insert(hdr, &hash_lock); 4339268075Sdelphij } 4340268075Sdelphij if (exists != NULL) { 4341168404Spjd /* somebody beat us to the hash insert */ 4342168404Spjd mutex_exit(hash_lock); 4343219089Spjd buf_discard_identity(hdr); 4344168404Spjd (void) arc_buf_remove_ref(buf, private); 4345168404Spjd goto top; /* restart the IO request */ 4346168404Spjd } 4347275811Sdelphij 4348168404Spjd /* if this is a prefetch, we don't have a reference */ 4349275811Sdelphij if (*arc_flags & ARC_FLAG_PREFETCH) { 4350168404Spjd (void) remove_reference(hdr, hash_lock, 4351168404Spjd private); 4352275811Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 4353168404Spjd } 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 if (BP_GET_LEVEL(bp) > 0) 4359275811Sdelphij hdr->b_flags |= ARC_FLAG_INDIRECT; 4360168404Spjd } else { 4361286570Smav /* 4362286570Smav * This block is in the ghost cache. If it was L2-only 4363286570Smav * (and thus didn't have an L1 hdr), we realloc the 4364286570Smav * header to add an L1 hdr. 4365286570Smav */ 4366286570Smav if (!HDR_HAS_L1HDR(hdr)) { 4367286570Smav hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, 4368286570Smav hdr_full_cache); 4369286570Smav } 4370286570Smav 4371286570Smav ASSERT(GHOST_STATE(hdr->b_l1hdr.b_state)); 4372168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4373286570Smav ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4374286763Smav ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 4375168404Spjd 4376168404Spjd /* if this is a prefetch, we don't have a reference */ 4377275811Sdelphij if (*arc_flags & ARC_FLAG_PREFETCH) 4378275811Sdelphij hdr->b_flags |= ARC_FLAG_PREFETCH; 4379168404Spjd else 4380168404Spjd add_reference(hdr, hash_lock, private); 4381275811Sdelphij if (*arc_flags & ARC_FLAG_L2CACHE) 4382275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 4383275811Sdelphij if (*arc_flags & ARC_FLAG_L2COMPRESS) 4384275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4385185029Spjd buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 4386168404Spjd buf->b_hdr = hdr; 4387168404Spjd buf->b_data = NULL; 4388168404Spjd buf->b_efunc = NULL; 4389168404Spjd buf->b_private = NULL; 4390168404Spjd buf->b_next = NULL; 4391286570Smav hdr->b_l1hdr.b_buf = buf; 4392286570Smav ASSERT0(hdr->b_l1hdr.b_datacnt); 4393286570Smav hdr->b_l1hdr.b_datacnt = 1; 4394219089Spjd arc_get_data_buf(buf); 4395219089Spjd arc_access(hdr, hash_lock); 4396168404Spjd } 4397168404Spjd 4398286570Smav ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); 4399219089Spjd 4400168404Spjd acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 4401168404Spjd acb->acb_done = done; 4402168404Spjd acb->acb_private = private; 4403168404Spjd 4404286570Smav ASSERT(hdr->b_l1hdr.b_acb == NULL); 4405286570Smav hdr->b_l1hdr.b_acb = acb; 4406275811Sdelphij hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; 4407168404Spjd 4408286570Smav if (HDR_HAS_L2HDR(hdr) && 4409286570Smav (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { 4410286570Smav devw = hdr->b_l2hdr.b_dev->l2ad_writing; 4411286570Smav addr = hdr->b_l2hdr.b_daddr; 4412286570Smav b_compress = HDR_GET_COMPRESS(hdr); 4413286570Smav b_asize = hdr->b_l2hdr.b_asize; 4414185029Spjd /* 4415185029Spjd * Lock out device removal. 4416185029Spjd */ 4417185029Spjd if (vdev_is_dead(vd) || 4418185029Spjd !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 4419185029Spjd vd = NULL; 4420185029Spjd } 4421185029Spjd 4422268075Sdelphij if (hash_lock != NULL) 4423268075Sdelphij mutex_exit(hash_lock); 4424168404Spjd 4425251629Sdelphij /* 4426251629Sdelphij * At this point, we have a level 1 cache miss. Try again in 4427251629Sdelphij * L2ARC if possible. 4428251629Sdelphij */ 4429168404Spjd ASSERT3U(hdr->b_size, ==, size); 4430219089Spjd DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, 4431268123Sdelphij uint64_t, size, zbookmark_phys_t *, zb); 4432168404Spjd ARCSTAT_BUMP(arcstat_misses); 4433286570Smav ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), 4434286570Smav demand, prefetch, !HDR_ISTYPE_METADATA(hdr), 4435168404Spjd data, metadata, misses); 4436228392Spjd#ifdef _KERNEL 4437228392Spjd curthread->td_ru.ru_inblock++; 4438228392Spjd#endif 4439168404Spjd 4440208373Smm if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { 4441185029Spjd /* 4442185029Spjd * Read from the L2ARC if the following are true: 4443185029Spjd * 1. The L2ARC vdev was previously cached. 4444185029Spjd * 2. This buffer still has L2ARC metadata. 4445185029Spjd * 3. This buffer isn't currently writing to the L2ARC. 4446185029Spjd * 4. The L2ARC entry wasn't evicted, which may 4447185029Spjd * also have invalidated the vdev. 4448208373Smm * 5. This isn't prefetch and l2arc_noprefetch is set. 4449185029Spjd */ 4450286570Smav if (HDR_HAS_L2HDR(hdr) && 4451208373Smm !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 4452208373Smm !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { 4453185029Spjd l2arc_read_callback_t *cb; 4454185029Spjd 4455185029Spjd DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 4456185029Spjd ARCSTAT_BUMP(arcstat_l2_hits); 4457185029Spjd 4458185029Spjd cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 4459185029Spjd KM_SLEEP); 4460185029Spjd cb->l2rcb_buf = buf; 4461185029Spjd cb->l2rcb_spa = spa; 4462185029Spjd cb->l2rcb_bp = *bp; 4463185029Spjd cb->l2rcb_zb = *zb; 4464185029Spjd cb->l2rcb_flags = zio_flags; 4465258389Savg cb->l2rcb_compress = b_compress; 4466185029Spjd 4467247187Smm ASSERT(addr >= VDEV_LABEL_START_SIZE && 4468247187Smm addr + size < vd->vdev_psize - 4469247187Smm VDEV_LABEL_END_SIZE); 4470247187Smm 4471185029Spjd /* 4472185029Spjd * l2arc read. The SCL_L2ARC lock will be 4473185029Spjd * released by l2arc_read_done(). 4474251478Sdelphij * Issue a null zio if the underlying buffer 4475251478Sdelphij * was squashed to zero size by compression. 4476185029Spjd */ 4477258389Savg if (b_compress == ZIO_COMPRESS_EMPTY) { 4478251478Sdelphij rzio = zio_null(pio, spa, vd, 4479251478Sdelphij l2arc_read_done, cb, 4480251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 4481251478Sdelphij ZIO_FLAG_CANFAIL | 4482251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 4483251478Sdelphij ZIO_FLAG_DONT_RETRY); 4484251478Sdelphij } else { 4485251478Sdelphij rzio = zio_read_phys(pio, vd, addr, 4486258389Savg b_asize, buf->b_data, 4487258389Savg ZIO_CHECKSUM_OFF, 4488251478Sdelphij l2arc_read_done, cb, priority, 4489251478Sdelphij zio_flags | ZIO_FLAG_DONT_CACHE | 4490251478Sdelphij ZIO_FLAG_CANFAIL | 4491251478Sdelphij ZIO_FLAG_DONT_PROPAGATE | 4492251478Sdelphij ZIO_FLAG_DONT_RETRY, B_FALSE); 4493251478Sdelphij } 4494185029Spjd DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 4495185029Spjd zio_t *, rzio); 4496258389Savg ARCSTAT_INCR(arcstat_l2_read_bytes, b_asize); 4497185029Spjd 4498275811Sdelphij if (*arc_flags & ARC_FLAG_NOWAIT) { 4499185029Spjd zio_nowait(rzio); 4500185029Spjd return (0); 4501185029Spjd } 4502185029Spjd 4503275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_WAIT); 4504185029Spjd if (zio_wait(rzio) == 0) 4505185029Spjd return (0); 4506185029Spjd 4507185029Spjd /* l2arc read error; goto zio_read() */ 4508185029Spjd } else { 4509185029Spjd DTRACE_PROBE1(l2arc__miss, 4510185029Spjd arc_buf_hdr_t *, hdr); 4511185029Spjd ARCSTAT_BUMP(arcstat_l2_misses); 4512185029Spjd if (HDR_L2_WRITING(hdr)) 4513185029Spjd ARCSTAT_BUMP(arcstat_l2_rw_clash); 4514185029Spjd spa_config_exit(spa, SCL_L2ARC, vd); 4515185029Spjd } 4516208373Smm } else { 4517208373Smm if (vd != NULL) 4518208373Smm spa_config_exit(spa, SCL_L2ARC, vd); 4519208373Smm if (l2arc_ndev != 0) { 4520208373Smm DTRACE_PROBE1(l2arc__miss, 4521208373Smm arc_buf_hdr_t *, hdr); 4522208373Smm ARCSTAT_BUMP(arcstat_l2_misses); 4523208373Smm } 4524185029Spjd } 4525185029Spjd 4526168404Spjd rzio = zio_read(pio, spa, bp, buf->b_data, size, 4527185029Spjd arc_read_done, buf, priority, zio_flags, zb); 4528168404Spjd 4529275811Sdelphij if (*arc_flags & ARC_FLAG_WAIT) 4530168404Spjd return (zio_wait(rzio)); 4531168404Spjd 4532275811Sdelphij ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 4533168404Spjd zio_nowait(rzio); 4534168404Spjd } 4535168404Spjd return (0); 4536168404Spjd} 4537168404Spjd 4538168404Spjdvoid 4539168404Spjdarc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 4540168404Spjd{ 4541168404Spjd ASSERT(buf->b_hdr != NULL); 4542286570Smav ASSERT(buf->b_hdr->b_l1hdr.b_state != arc_anon); 4543286570Smav ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt) || 4544286570Smav func == NULL); 4545219089Spjd ASSERT(buf->b_efunc == NULL); 4546219089Spjd ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); 4547219089Spjd 4548168404Spjd buf->b_efunc = func; 4549168404Spjd buf->b_private = private; 4550168404Spjd} 4551168404Spjd 4552168404Spjd/* 4553251520Sdelphij * Notify the arc that a block was freed, and thus will never be used again. 4554251520Sdelphij */ 4555251520Sdelphijvoid 4556251520Sdelphijarc_freed(spa_t *spa, const blkptr_t *bp) 4557251520Sdelphij{ 4558251520Sdelphij arc_buf_hdr_t *hdr; 4559251520Sdelphij kmutex_t *hash_lock; 4560251520Sdelphij uint64_t guid = spa_load_guid(spa); 4561251520Sdelphij 4562268075Sdelphij ASSERT(!BP_IS_EMBEDDED(bp)); 4563268075Sdelphij 4564268075Sdelphij hdr = buf_hash_find(guid, bp, &hash_lock); 4565251520Sdelphij if (hdr == NULL) 4566251520Sdelphij return; 4567251520Sdelphij if (HDR_BUF_AVAILABLE(hdr)) { 4568286570Smav arc_buf_t *buf = hdr->b_l1hdr.b_buf; 4569251520Sdelphij add_reference(hdr, hash_lock, FTAG); 4570275811Sdelphij hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; 4571251520Sdelphij mutex_exit(hash_lock); 4572251520Sdelphij 4573251520Sdelphij arc_release(buf, FTAG); 4574251520Sdelphij (void) arc_buf_remove_ref(buf, FTAG); 4575251520Sdelphij } else { 4576251520Sdelphij mutex_exit(hash_lock); 4577251520Sdelphij } 4578251520Sdelphij 4579251520Sdelphij} 4580251520Sdelphij 4581251520Sdelphij/* 4582268858Sdelphij * Clear the user eviction callback set by arc_set_callback(), first calling 4583268858Sdelphij * it if it exists. Because the presence of a callback keeps an arc_buf cached 4584268858Sdelphij * clearing the callback may result in the arc_buf being destroyed. However, 4585268858Sdelphij * it will not result in the *last* arc_buf being destroyed, hence the data 4586268858Sdelphij * will remain cached in the ARC. We make a copy of the arc buffer here so 4587268858Sdelphij * that we can process the callback without holding any locks. 4588268858Sdelphij * 4589268858Sdelphij * It's possible that the callback is already in the process of being cleared 4590268858Sdelphij * by another thread. In this case we can not clear the callback. 4591268858Sdelphij * 4592268858Sdelphij * Returns B_TRUE if the callback was successfully called and cleared. 4593168404Spjd */ 4594268858Sdelphijboolean_t 4595268858Sdelphijarc_clear_callback(arc_buf_t *buf) 4596168404Spjd{ 4597168404Spjd arc_buf_hdr_t *hdr; 4598168404Spjd kmutex_t *hash_lock; 4599268858Sdelphij arc_evict_func_t *efunc = buf->b_efunc; 4600268858Sdelphij void *private = buf->b_private; 4601206796Spjd 4602219089Spjd mutex_enter(&buf->b_evict_lock); 4603168404Spjd hdr = buf->b_hdr; 4604168404Spjd if (hdr == NULL) { 4605168404Spjd /* 4606168404Spjd * We are in arc_do_user_evicts(). 4607168404Spjd */ 4608168404Spjd ASSERT(buf->b_data == NULL); 4609219089Spjd mutex_exit(&buf->b_evict_lock); 4610268858Sdelphij return (B_FALSE); 4611185029Spjd } else if (buf->b_data == NULL) { 4612185029Spjd /* 4613185029Spjd * We are on the eviction list; process this buffer now 4614185029Spjd * but let arc_do_user_evicts() do the reaping. 4615185029Spjd */ 4616185029Spjd buf->b_efunc = NULL; 4617219089Spjd mutex_exit(&buf->b_evict_lock); 4618268858Sdelphij VERIFY0(efunc(private)); 4619268858Sdelphij return (B_TRUE); 4620168404Spjd } 4621168404Spjd hash_lock = HDR_LOCK(hdr); 4622168404Spjd mutex_enter(hash_lock); 4623219089Spjd hdr = buf->b_hdr; 4624219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4625168404Spjd 4626286570Smav ASSERT3U(refcount_count(&hdr->b_l1hdr.b_refcnt), <, 4627286570Smav hdr->b_l1hdr.b_datacnt); 4628286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_mru || 4629286570Smav hdr->b_l1hdr.b_state == arc_mfu); 4630168404Spjd 4631268858Sdelphij buf->b_efunc = NULL; 4632268858Sdelphij buf->b_private = NULL; 4633168404Spjd 4634286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 4635268858Sdelphij mutex_exit(&buf->b_evict_lock); 4636286763Smav arc_buf_destroy(buf, TRUE); 4637268858Sdelphij } else { 4638286570Smav ASSERT(buf == hdr->b_l1hdr.b_buf); 4639275811Sdelphij hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; 4640268858Sdelphij mutex_exit(&buf->b_evict_lock); 4641268858Sdelphij } 4642168404Spjd 4643168404Spjd mutex_exit(hash_lock); 4644268858Sdelphij VERIFY0(efunc(private)); 4645268858Sdelphij return (B_TRUE); 4646168404Spjd} 4647168404Spjd 4648168404Spjd/* 4649251629Sdelphij * Release this buffer from the cache, making it an anonymous buffer. This 4650251629Sdelphij * must be done after a read and prior to modifying the buffer contents. 4651168404Spjd * If the buffer has more than one reference, we must make 4652185029Spjd * a new hdr for the buffer. 4653168404Spjd */ 4654168404Spjdvoid 4655168404Spjdarc_release(arc_buf_t *buf, void *tag) 4656168404Spjd{ 4657286570Smav arc_buf_hdr_t *hdr = buf->b_hdr; 4658168404Spjd 4659219089Spjd /* 4660219089Spjd * It would be nice to assert that if it's DMU metadata (level > 4661219089Spjd * 0 || it's the dnode file), then it must be syncing context. 4662219089Spjd * But we don't know that information at this level. 4663219089Spjd */ 4664219089Spjd 4665219089Spjd mutex_enter(&buf->b_evict_lock); 4666286776Smav 4667286776Smav ASSERT(HDR_HAS_L1HDR(hdr)); 4668286776Smav 4669286570Smav /* 4670286570Smav * We don't grab the hash lock prior to this check, because if 4671286570Smav * the buffer's header is in the arc_anon state, it won't be 4672286570Smav * linked into the hash table. 4673286570Smav */ 4674286570Smav if (hdr->b_l1hdr.b_state == arc_anon) { 4675286570Smav mutex_exit(&buf->b_evict_lock); 4676286570Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4677286570Smav ASSERT(!HDR_IN_HASH_TABLE(hdr)); 4678286570Smav ASSERT(!HDR_HAS_L2HDR(hdr)); 4679286570Smav ASSERT(BUF_EMPTY(hdr)); 4680286570Smav ASSERT3U(hdr->b_l1hdr.b_datacnt, ==, 1); 4681286570Smav ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); 4682286570Smav ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); 4683185029Spjd 4684286570Smav ASSERT3P(buf->b_efunc, ==, NULL); 4685286570Smav ASSERT3P(buf->b_private, ==, NULL); 4686168404Spjd 4687286570Smav hdr->b_l1hdr.b_arc_access = 0; 4688286570Smav arc_buf_thaw(buf); 4689286570Smav 4690286570Smav return; 4691168404Spjd } 4692168404Spjd 4693286570Smav kmutex_t *hash_lock = HDR_LOCK(hdr); 4694286570Smav mutex_enter(hash_lock); 4695286570Smav 4696286570Smav /* 4697286570Smav * This assignment is only valid as long as the hash_lock is 4698286570Smav * held, we must be careful not to reference state or the 4699286570Smav * b_state field after dropping the lock. 4700286570Smav */ 4701286570Smav arc_state_t *state = hdr->b_l1hdr.b_state; 4702286570Smav ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 4703286570Smav ASSERT3P(state, !=, arc_anon); 4704286570Smav 4705286570Smav /* this buffer is not on any list */ 4706286570Smav ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) > 0); 4707286570Smav 4708286570Smav if (HDR_HAS_L2HDR(hdr)) { 4709286570Smav mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); 4710286570Smav 4711286570Smav /* 4712286598Smav * We have to recheck this conditional again now that 4713286598Smav * we're holding the l2ad_mtx to prevent a race with 4714286598Smav * another thread which might be concurrently calling 4715286598Smav * l2arc_evict(). In that case, l2arc_evict() might have 4716286598Smav * destroyed the header's L2 portion as we were waiting 4717286598Smav * to acquire the l2ad_mtx. 4718286570Smav */ 4719286598Smav if (HDR_HAS_L2HDR(hdr)) { 4720286647Smav if (hdr->b_l2hdr.b_daddr != L2ARC_ADDR_UNSET) 4721286647Smav trim_map_free(hdr->b_l2hdr.b_dev->l2ad_vdev, 4722286647Smav hdr->b_l2hdr.b_daddr, 4723286647Smav hdr->b_l2hdr.b_asize, 0); 4724286598Smav arc_hdr_l2hdr_destroy(hdr); 4725286598Smav } 4726286570Smav 4727286570Smav mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); 4728185029Spjd } 4729185029Spjd 4730168404Spjd /* 4731168404Spjd * Do we have more than one buf? 4732168404Spjd */ 4733286570Smav if (hdr->b_l1hdr.b_datacnt > 1) { 4734168404Spjd arc_buf_hdr_t *nhdr; 4735168404Spjd arc_buf_t **bufp; 4736168404Spjd uint64_t blksz = hdr->b_size; 4737209962Smm uint64_t spa = hdr->b_spa; 4738286570Smav arc_buf_contents_t type = arc_buf_type(hdr); 4739185029Spjd uint32_t flags = hdr->b_flags; 4740168404Spjd 4741286570Smav ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); 4742168404Spjd /* 4743219089Spjd * Pull the data off of this hdr and attach it to 4744219089Spjd * a new anonymous hdr. 4745168404Spjd */ 4746168404Spjd (void) remove_reference(hdr, hash_lock, tag); 4747286570Smav bufp = &hdr->b_l1hdr.b_buf; 4748168404Spjd while (*bufp != buf) 4749168404Spjd bufp = &(*bufp)->b_next; 4750219089Spjd *bufp = buf->b_next; 4751168404Spjd buf->b_next = NULL; 4752168404Spjd 4753286570Smav ASSERT3P(state, !=, arc_l2c_only); 4754286766Smav 4755286766Smav (void) refcount_remove_many( 4756286766Smav &state->arcs_size, hdr->b_size, buf); 4757286766Smav 4758286570Smav if (refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { 4759286570Smav ASSERT3P(state, !=, arc_l2c_only); 4760286570Smav uint64_t *size = &state->arcs_lsize[type]; 4761185029Spjd ASSERT3U(*size, >=, hdr->b_size); 4762185029Spjd atomic_add_64(size, -hdr->b_size); 4763168404Spjd } 4764242845Sdelphij 4765242845Sdelphij /* 4766242845Sdelphij * We're releasing a duplicate user data buffer, update 4767242845Sdelphij * our statistics accordingly. 4768242845Sdelphij */ 4769286570Smav if (HDR_ISTYPE_DATA(hdr)) { 4770242845Sdelphij ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); 4771242845Sdelphij ARCSTAT_INCR(arcstat_duplicate_buffers_size, 4772242845Sdelphij -hdr->b_size); 4773242845Sdelphij } 4774286570Smav hdr->b_l1hdr.b_datacnt -= 1; 4775168404Spjd arc_cksum_verify(buf); 4776240133Smm#ifdef illumos 4777240133Smm arc_buf_unwatch(buf); 4778277300Ssmh#endif 4779168404Spjd 4780168404Spjd mutex_exit(hash_lock); 4781168404Spjd 4782286570Smav nhdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 4783168404Spjd nhdr->b_size = blksz; 4784168404Spjd nhdr->b_spa = spa; 4785286570Smav 4786275811Sdelphij nhdr->b_flags = flags & ARC_FLAG_L2_WRITING; 4787286570Smav nhdr->b_flags |= arc_bufc_to_flags(type); 4788286570Smav nhdr->b_flags |= ARC_FLAG_HAS_L1HDR; 4789286570Smav 4790286570Smav nhdr->b_l1hdr.b_buf = buf; 4791286570Smav nhdr->b_l1hdr.b_datacnt = 1; 4792286570Smav nhdr->b_l1hdr.b_state = arc_anon; 4793286570Smav nhdr->b_l1hdr.b_arc_access = 0; 4794286763Smav nhdr->b_l1hdr.b_tmp_cdata = NULL; 4795168404Spjd nhdr->b_freeze_cksum = NULL; 4796286570Smav 4797286570Smav (void) refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); 4798168404Spjd buf->b_hdr = nhdr; 4799219089Spjd mutex_exit(&buf->b_evict_lock); 4800286766Smav (void) refcount_add_many(&arc_anon->arcs_size, blksz, buf); 4801168404Spjd } else { 4802219089Spjd mutex_exit(&buf->b_evict_lock); 4803286570Smav ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); 4804286763Smav /* protected by hash lock, or hdr is on arc_anon */ 4805286763Smav ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 4806168404Spjd ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4807286570Smav arc_change_state(arc_anon, hdr, hash_lock); 4808286570Smav hdr->b_l1hdr.b_arc_access = 0; 4809286570Smav mutex_exit(hash_lock); 4810185029Spjd 4811219089Spjd buf_discard_identity(hdr); 4812168404Spjd arc_buf_thaw(buf); 4813168404Spjd } 4814168404Spjd buf->b_efunc = NULL; 4815168404Spjd buf->b_private = NULL; 4816168404Spjd} 4817168404Spjd 4818168404Spjdint 4819168404Spjdarc_released(arc_buf_t *buf) 4820168404Spjd{ 4821185029Spjd int released; 4822185029Spjd 4823219089Spjd mutex_enter(&buf->b_evict_lock); 4824286570Smav released = (buf->b_data != NULL && 4825286570Smav buf->b_hdr->b_l1hdr.b_state == arc_anon); 4826219089Spjd mutex_exit(&buf->b_evict_lock); 4827185029Spjd return (released); 4828168404Spjd} 4829168404Spjd 4830168404Spjd#ifdef ZFS_DEBUG 4831168404Spjdint 4832168404Spjdarc_referenced(arc_buf_t *buf) 4833168404Spjd{ 4834185029Spjd int referenced; 4835185029Spjd 4836219089Spjd mutex_enter(&buf->b_evict_lock); 4837286570Smav referenced = (refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); 4838219089Spjd mutex_exit(&buf->b_evict_lock); 4839185029Spjd return (referenced); 4840168404Spjd} 4841168404Spjd#endif 4842168404Spjd 4843168404Spjdstatic void 4844168404Spjdarc_write_ready(zio_t *zio) 4845168404Spjd{ 4846168404Spjd arc_write_callback_t *callback = zio->io_private; 4847168404Spjd arc_buf_t *buf = callback->awcb_buf; 4848185029Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 4849168404Spjd 4850286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 4851286570Smav ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); 4852286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 4853185029Spjd callback->awcb_ready(zio, buf, callback->awcb_private); 4854185029Spjd 4855185029Spjd /* 4856185029Spjd * If the IO is already in progress, then this is a re-write 4857185029Spjd * attempt, so we need to thaw and re-compute the cksum. 4858185029Spjd * It is the responsibility of the callback to handle the 4859185029Spjd * accounting for any re-write attempt. 4860185029Spjd */ 4861185029Spjd if (HDR_IO_IN_PROGRESS(hdr)) { 4862286570Smav mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 4863185029Spjd if (hdr->b_freeze_cksum != NULL) { 4864185029Spjd kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); 4865185029Spjd hdr->b_freeze_cksum = NULL; 4866185029Spjd } 4867286570Smav mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 4868168404Spjd } 4869185029Spjd arc_cksum_compute(buf, B_FALSE); 4870275811Sdelphij hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; 4871168404Spjd} 4872168404Spjd 4873258632Savg/* 4874258632Savg * The SPA calls this callback for each physical write that happens on behalf 4875258632Savg * of a logical write. See the comment in dbuf_write_physdone() for details. 4876258632Savg */ 4877168404Spjdstatic void 4878258632Savgarc_write_physdone(zio_t *zio) 4879258632Savg{ 4880258632Savg arc_write_callback_t *cb = zio->io_private; 4881258632Savg if (cb->awcb_physdone != NULL) 4882258632Savg cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 4883258632Savg} 4884258632Savg 4885258632Savgstatic void 4886168404Spjdarc_write_done(zio_t *zio) 4887168404Spjd{ 4888168404Spjd arc_write_callback_t *callback = zio->io_private; 4889168404Spjd arc_buf_t *buf = callback->awcb_buf; 4890168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 4891168404Spjd 4892286570Smav ASSERT(hdr->b_l1hdr.b_acb == NULL); 4893168404Spjd 4894219089Spjd if (zio->io_error == 0) { 4895268075Sdelphij if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 4896260150Sdelphij buf_discard_identity(hdr); 4897260150Sdelphij } else { 4898260150Sdelphij hdr->b_dva = *BP_IDENTITY(zio->io_bp); 4899260150Sdelphij hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 4900260150Sdelphij } 4901219089Spjd } else { 4902219089Spjd ASSERT(BUF_EMPTY(hdr)); 4903219089Spjd } 4904219089Spjd 4905168404Spjd /* 4906268075Sdelphij * If the block to be written was all-zero or compressed enough to be 4907268075Sdelphij * embedded in the BP, no write was performed so there will be no 4908268075Sdelphij * dva/birth/checksum. The buffer must therefore remain anonymous 4909268075Sdelphij * (and uncached). 4910168404Spjd */ 4911168404Spjd if (!BUF_EMPTY(hdr)) { 4912168404Spjd arc_buf_hdr_t *exists; 4913168404Spjd kmutex_t *hash_lock; 4914168404Spjd 4915219089Spjd ASSERT(zio->io_error == 0); 4916219089Spjd 4917168404Spjd arc_cksum_verify(buf); 4918168404Spjd 4919168404Spjd exists = buf_hash_insert(hdr, &hash_lock); 4920286570Smav if (exists != NULL) { 4921168404Spjd /* 4922168404Spjd * This can only happen if we overwrite for 4923168404Spjd * sync-to-convergence, because we remove 4924168404Spjd * buffers from the hash table when we arc_free(). 4925168404Spjd */ 4926219089Spjd if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 4927219089Spjd if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 4928219089Spjd panic("bad overwrite, hdr=%p exists=%p", 4929219089Spjd (void *)hdr, (void *)exists); 4930286570Smav ASSERT(refcount_is_zero( 4931286570Smav &exists->b_l1hdr.b_refcnt)); 4932219089Spjd arc_change_state(arc_anon, exists, hash_lock); 4933219089Spjd mutex_exit(hash_lock); 4934219089Spjd arc_hdr_destroy(exists); 4935219089Spjd exists = buf_hash_insert(hdr, &hash_lock); 4936219089Spjd ASSERT3P(exists, ==, NULL); 4937243524Smm } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 4938243524Smm /* nopwrite */ 4939243524Smm ASSERT(zio->io_prop.zp_nopwrite); 4940243524Smm if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 4941243524Smm panic("bad nopwrite, hdr=%p exists=%p", 4942243524Smm (void *)hdr, (void *)exists); 4943219089Spjd } else { 4944219089Spjd /* Dedup */ 4945286570Smav ASSERT(hdr->b_l1hdr.b_datacnt == 1); 4946286570Smav ASSERT(hdr->b_l1hdr.b_state == arc_anon); 4947219089Spjd ASSERT(BP_GET_DEDUP(zio->io_bp)); 4948219089Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 4949219089Spjd } 4950168404Spjd } 4951275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 4952185029Spjd /* if it's not anon, we are doing a scrub */ 4953286570Smav if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) 4954185029Spjd arc_access(hdr, hash_lock); 4955168404Spjd mutex_exit(hash_lock); 4956168404Spjd } else { 4957275811Sdelphij hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; 4958168404Spjd } 4959168404Spjd 4960286570Smav ASSERT(!refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 4961219089Spjd callback->awcb_done(zio, buf, callback->awcb_private); 4962168404Spjd 4963168404Spjd kmem_free(callback, sizeof (arc_write_callback_t)); 4964168404Spjd} 4965168404Spjd 4966168404Spjdzio_t * 4967219089Spjdarc_write(zio_t *pio, spa_t *spa, uint64_t txg, 4968251478Sdelphij blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress, 4969258632Savg const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone, 4970258632Savg arc_done_func_t *done, void *private, zio_priority_t priority, 4971268123Sdelphij int zio_flags, const zbookmark_phys_t *zb) 4972168404Spjd{ 4973168404Spjd arc_buf_hdr_t *hdr = buf->b_hdr; 4974168404Spjd arc_write_callback_t *callback; 4975185029Spjd zio_t *zio; 4976168404Spjd 4977185029Spjd ASSERT(ready != NULL); 4978219089Spjd ASSERT(done != NULL); 4979168404Spjd ASSERT(!HDR_IO_ERROR(hdr)); 4980286570Smav ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 4981286570Smav ASSERT(hdr->b_l1hdr.b_acb == NULL); 4982286570Smav ASSERT(hdr->b_l1hdr.b_datacnt > 0); 4983185029Spjd if (l2arc) 4984275811Sdelphij hdr->b_flags |= ARC_FLAG_L2CACHE; 4985251478Sdelphij if (l2arc_compress) 4986275811Sdelphij hdr->b_flags |= ARC_FLAG_L2COMPRESS; 4987168404Spjd callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 4988168404Spjd callback->awcb_ready = ready; 4989258632Savg callback->awcb_physdone = physdone; 4990168404Spjd callback->awcb_done = done; 4991168404Spjd callback->awcb_private = private; 4992168404Spjd callback->awcb_buf = buf; 4993168404Spjd 4994219089Spjd zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, 4995258632Savg arc_write_ready, arc_write_physdone, arc_write_done, callback, 4996258632Savg priority, zio_flags, zb); 4997185029Spjd 4998168404Spjd return (zio); 4999168404Spjd} 5000168404Spjd 5001185029Spjdstatic int 5002258632Savgarc_memory_throttle(uint64_t reserve, uint64_t txg) 5003185029Spjd{ 5004185029Spjd#ifdef _KERNEL 5005272483Ssmh uint64_t available_memory = ptob(freemem); 5006185029Spjd static uint64_t page_load = 0; 5007185029Spjd static uint64_t last_txg = 0; 5008185029Spjd 5009272483Ssmh#if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC) 5010185029Spjd available_memory = 5011272483Ssmh MIN(available_memory, ptob(vmem_size(heap_arena, VMEM_FREE))); 5012185029Spjd#endif 5013258632Savg 5014272483Ssmh if (freemem > (uint64_t)physmem * arc_lotsfree_percent / 100) 5015185029Spjd return (0); 5016185029Spjd 5017185029Spjd if (txg > last_txg) { 5018185029Spjd last_txg = txg; 5019185029Spjd page_load = 0; 5020185029Spjd } 5021185029Spjd /* 5022185029Spjd * If we are in pageout, we know that memory is already tight, 5023185029Spjd * the arc is already going to be evicting, so we just want to 5024185029Spjd * continue to let page writes occur as quickly as possible. 5025185029Spjd */ 5026185029Spjd if (curproc == pageproc) { 5027272483Ssmh if (page_load > MAX(ptob(minfree), available_memory) / 4) 5028249195Smm return (SET_ERROR(ERESTART)); 5029185029Spjd /* Note: reserve is inflated, so we deflate */ 5030185029Spjd page_load += reserve / 8; 5031185029Spjd return (0); 5032185029Spjd } else if (page_load > 0 && arc_reclaim_needed()) { 5033185029Spjd /* memory is low, delay before restarting */ 5034185029Spjd ARCSTAT_INCR(arcstat_memory_throttle_count, 1); 5035249195Smm return (SET_ERROR(EAGAIN)); 5036185029Spjd } 5037185029Spjd page_load = 0; 5038185029Spjd#endif 5039185029Spjd return (0); 5040185029Spjd} 5041185029Spjd 5042168404Spjdvoid 5043185029Spjdarc_tempreserve_clear(uint64_t reserve) 5044168404Spjd{ 5045185029Spjd atomic_add_64(&arc_tempreserve, -reserve); 5046168404Spjd ASSERT((int64_t)arc_tempreserve >= 0); 5047168404Spjd} 5048168404Spjd 5049168404Spjdint 5050185029Spjdarc_tempreserve_space(uint64_t reserve, uint64_t txg) 5051168404Spjd{ 5052185029Spjd int error; 5053209962Smm uint64_t anon_size; 5054185029Spjd 5055272483Ssmh if (reserve > arc_c/4 && !arc_no_grow) { 5056185029Spjd arc_c = MIN(arc_c_max, reserve * 4); 5057272483Ssmh DTRACE_PROBE1(arc__set_reserve, uint64_t, arc_c); 5058272483Ssmh } 5059185029Spjd if (reserve > arc_c) 5060249195Smm return (SET_ERROR(ENOMEM)); 5061168404Spjd 5062168404Spjd /* 5063209962Smm * Don't count loaned bufs as in flight dirty data to prevent long 5064209962Smm * network delays from blocking transactions that are ready to be 5065209962Smm * assigned to a txg. 5066209962Smm */ 5067286766Smav anon_size = MAX((int64_t)(refcount_count(&arc_anon->arcs_size) - 5068286766Smav arc_loaned_bytes), 0); 5069209962Smm 5070209962Smm /* 5071185029Spjd * Writes will, almost always, require additional memory allocations 5072251631Sdelphij * in order to compress/encrypt/etc the data. We therefore need to 5073185029Spjd * make sure that there is sufficient available memory for this. 5074185029Spjd */ 5075258632Savg error = arc_memory_throttle(reserve, txg); 5076258632Savg if (error != 0) 5077185029Spjd return (error); 5078185029Spjd 5079185029Spjd /* 5080168404Spjd * Throttle writes when the amount of dirty data in the cache 5081168404Spjd * gets too large. We try to keep the cache less than half full 5082168404Spjd * of dirty blocks so that our sync times don't grow too large. 5083168404Spjd * Note: if two requests come in concurrently, we might let them 5084168404Spjd * both succeed, when one of them should fail. Not a huge deal. 5085168404Spjd */ 5086209962Smm 5087209962Smm if (reserve + arc_tempreserve + anon_size > arc_c / 2 && 5088209962Smm anon_size > arc_c / 4) { 5089185029Spjd dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 5090185029Spjd "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", 5091185029Spjd arc_tempreserve>>10, 5092185029Spjd arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, 5093185029Spjd arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, 5094185029Spjd reserve>>10, arc_c>>10); 5095249195Smm return (SET_ERROR(ERESTART)); 5096168404Spjd } 5097185029Spjd atomic_add_64(&arc_tempreserve, reserve); 5098168404Spjd return (0); 5099168404Spjd} 5100168404Spjd 5101286626Smavstatic void 5102286626Smavarc_kstat_update_state(arc_state_t *state, kstat_named_t *size, 5103286626Smav kstat_named_t *evict_data, kstat_named_t *evict_metadata) 5104286626Smav{ 5105286766Smav size->value.ui64 = refcount_count(&state->arcs_size); 5106286626Smav evict_data->value.ui64 = state->arcs_lsize[ARC_BUFC_DATA]; 5107286626Smav evict_metadata->value.ui64 = state->arcs_lsize[ARC_BUFC_METADATA]; 5108286626Smav} 5109286626Smav 5110286626Smavstatic int 5111286626Smavarc_kstat_update(kstat_t *ksp, int rw) 5112286626Smav{ 5113286626Smav arc_stats_t *as = ksp->ks_data; 5114286626Smav 5115286626Smav if (rw == KSTAT_WRITE) { 5116286626Smav return (EACCES); 5117286626Smav } else { 5118286626Smav arc_kstat_update_state(arc_anon, 5119286626Smav &as->arcstat_anon_size, 5120286626Smav &as->arcstat_anon_evictable_data, 5121286626Smav &as->arcstat_anon_evictable_metadata); 5122286626Smav arc_kstat_update_state(arc_mru, 5123286626Smav &as->arcstat_mru_size, 5124286626Smav &as->arcstat_mru_evictable_data, 5125286626Smav &as->arcstat_mru_evictable_metadata); 5126286626Smav arc_kstat_update_state(arc_mru_ghost, 5127286626Smav &as->arcstat_mru_ghost_size, 5128286626Smav &as->arcstat_mru_ghost_evictable_data, 5129286626Smav &as->arcstat_mru_ghost_evictable_metadata); 5130286626Smav arc_kstat_update_state(arc_mfu, 5131286626Smav &as->arcstat_mfu_size, 5132286626Smav &as->arcstat_mfu_evictable_data, 5133286626Smav &as->arcstat_mfu_evictable_metadata); 5134286626Smav arc_kstat_update_state(arc_mfu_ghost, 5135286626Smav &as->arcstat_mfu_ghost_size, 5136286626Smav &as->arcstat_mfu_ghost_evictable_data, 5137286626Smav &as->arcstat_mfu_ghost_evictable_metadata); 5138286626Smav } 5139286626Smav 5140286626Smav return (0); 5141286626Smav} 5142286626Smav 5143286763Smav/* 5144286763Smav * This function *must* return indices evenly distributed between all 5145286763Smav * sublists of the multilist. This is needed due to how the ARC eviction 5146286763Smav * code is laid out; arc_evict_state() assumes ARC buffers are evenly 5147286763Smav * distributed between all sublists and uses this assumption when 5148286763Smav * deciding which sublist to evict from and how much to evict from it. 5149286763Smav */ 5150286763Smavunsigned int 5151286763Smavarc_state_multilist_index_func(multilist_t *ml, void *obj) 5152286763Smav{ 5153286763Smav arc_buf_hdr_t *hdr = obj; 5154286763Smav 5155286763Smav /* 5156286763Smav * We rely on b_dva to generate evenly distributed index 5157286763Smav * numbers using buf_hash below. So, as an added precaution, 5158286763Smav * let's make sure we never add empty buffers to the arc lists. 5159286763Smav */ 5160286763Smav ASSERT(!BUF_EMPTY(hdr)); 5161286763Smav 5162286763Smav /* 5163286763Smav * The assumption here, is the hash value for a given 5164286763Smav * arc_buf_hdr_t will remain constant throughout it's lifetime 5165286763Smav * (i.e. it's b_spa, b_dva, and b_birth fields don't change). 5166286763Smav * Thus, we don't need to store the header's sublist index 5167286763Smav * on insertion, as this index can be recalculated on removal. 5168286763Smav * 5169286763Smav * Also, the low order bits of the hash value are thought to be 5170286763Smav * distributed evenly. Otherwise, in the case that the multilist 5171286763Smav * has a power of two number of sublists, each sublists' usage 5172286763Smav * would not be evenly distributed. 5173286763Smav */ 5174286763Smav return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % 5175286763Smav multilist_get_num_sublists(ml)); 5176286763Smav} 5177286763Smav 5178168404Spjd#ifdef _KERNEL 5179168566Spjdstatic eventhandler_tag arc_event_lowmem = NULL; 5180168404Spjd 5181168404Spjdstatic void 5182168566Spjdarc_lowmem(void *arg __unused, int howto __unused) 5183168404Spjd{ 5184168404Spjd 5185286763Smav mutex_enter(&arc_reclaim_lock); 5186286625Smav /* XXX: Memory deficit should be passed as argument. */ 5187286625Smav needfree = btoc(arc_c >> arc_shrink_shift); 5188272483Ssmh DTRACE_PROBE(arc__needfree); 5189286763Smav cv_signal(&arc_reclaim_thread_cv); 5190241773Savg 5191241773Savg /* 5192241773Savg * It is unsafe to block here in arbitrary threads, because we can come 5193241773Savg * here from ARC itself and may hold ARC locks and thus risk a deadlock 5194241773Savg * with ARC reclaim thread. 5195241773Savg */ 5196286623Smav if (curproc == pageproc) 5197286763Smav (void) cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); 5198286763Smav mutex_exit(&arc_reclaim_lock); 5199168404Spjd} 5200168404Spjd#endif 5201168404Spjd 5202168404Spjdvoid 5203168404Spjdarc_init(void) 5204168404Spjd{ 5205219089Spjd int i, prefetch_tunable_set = 0; 5206205231Skmacy 5207286763Smav mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL); 5208286763Smav cv_init(&arc_reclaim_thread_cv, NULL, CV_DEFAULT, NULL); 5209286763Smav cv_init(&arc_reclaim_waiters_cv, NULL, CV_DEFAULT, NULL); 5210168404Spjd 5211286763Smav mutex_init(&arc_user_evicts_lock, NULL, MUTEX_DEFAULT, NULL); 5212286763Smav cv_init(&arc_user_evicts_cv, NULL, CV_DEFAULT, NULL); 5213286763Smav 5214168404Spjd /* Convert seconds to clock ticks */ 5215168404Spjd arc_min_prefetch_lifespan = 1 * hz; 5216168404Spjd 5217168404Spjd /* Start out with 1/8 of all memory */ 5218168566Spjd arc_c = kmem_size() / 8; 5219219089Spjd 5220277300Ssmh#ifdef illumos 5221192360Skmacy#ifdef _KERNEL 5222192360Skmacy /* 5223192360Skmacy * On architectures where the physical memory can be larger 5224192360Skmacy * than the addressable space (intel in 32-bit mode), we may 5225192360Skmacy * need to limit the cache to 1/8 of VM size. 5226192360Skmacy */ 5227192360Skmacy arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 5228192360Skmacy#endif 5229277300Ssmh#endif /* illumos */ 5230168566Spjd /* set min cache to 1/32 of all memory, or 16MB, whichever is more */ 5231280822Smav arc_c_min = MAX(arc_c / 4, 16 << 20); 5232168566Spjd /* set max to 1/2 of all memory, or all but 1GB, whichever is more */ 5233280822Smav if (arc_c * 8 >= 1 << 30) 5234280822Smav arc_c_max = (arc_c * 8) - (1 << 30); 5235168404Spjd else 5236168404Spjd arc_c_max = arc_c_min; 5237175633Spjd arc_c_max = MAX(arc_c * 5, arc_c_max); 5238219089Spjd 5239168481Spjd#ifdef _KERNEL 5240168404Spjd /* 5241168404Spjd * Allow the tunables to override our calculations if they are 5242168566Spjd * reasonable (ie. over 16MB) 5243168404Spjd */ 5244280822Smav if (zfs_arc_max > 16 << 20 && zfs_arc_max < kmem_size()) 5245168404Spjd arc_c_max = zfs_arc_max; 5246280822Smav if (zfs_arc_min > 16 << 20 && zfs_arc_min <= arc_c_max) 5247168404Spjd arc_c_min = zfs_arc_min; 5248168481Spjd#endif 5249219089Spjd 5250168404Spjd arc_c = arc_c_max; 5251168404Spjd arc_p = (arc_c >> 1); 5252168404Spjd 5253185029Spjd /* limit meta-data to 1/4 of the arc capacity */ 5254185029Spjd arc_meta_limit = arc_c_max / 4; 5255185029Spjd 5256185029Spjd /* Allow the tunable to override if it is reasonable */ 5257185029Spjd if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max) 5258185029Spjd arc_meta_limit = zfs_arc_meta_limit; 5259185029Spjd 5260185029Spjd if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0) 5261185029Spjd arc_c_min = arc_meta_limit / 2; 5262185029Spjd 5263275780Sdelphij if (zfs_arc_meta_min > 0) { 5264275780Sdelphij arc_meta_min = zfs_arc_meta_min; 5265275780Sdelphij } else { 5266275780Sdelphij arc_meta_min = arc_c_min / 2; 5267275780Sdelphij } 5268275780Sdelphij 5269208373Smm if (zfs_arc_grow_retry > 0) 5270208373Smm arc_grow_retry = zfs_arc_grow_retry; 5271208373Smm 5272208373Smm if (zfs_arc_shrink_shift > 0) 5273208373Smm arc_shrink_shift = zfs_arc_shrink_shift; 5274208373Smm 5275286625Smav /* 5276286625Smav * Ensure that arc_no_grow_shift is less than arc_shrink_shift. 5277286625Smav */ 5278286625Smav if (arc_no_grow_shift >= arc_shrink_shift) 5279286625Smav arc_no_grow_shift = arc_shrink_shift - 1; 5280286625Smav 5281208373Smm if (zfs_arc_p_min_shift > 0) 5282208373Smm arc_p_min_shift = zfs_arc_p_min_shift; 5283208373Smm 5284286763Smav if (zfs_arc_num_sublists_per_state < 1) 5285286763Smav zfs_arc_num_sublists_per_state = MAX(max_ncpus, 1); 5286286763Smav 5287168404Spjd /* if kmem_flags are set, lets try to use less memory */ 5288168404Spjd if (kmem_debugging()) 5289168404Spjd arc_c = arc_c / 2; 5290168404Spjd if (arc_c < arc_c_min) 5291168404Spjd arc_c = arc_c_min; 5292168404Spjd 5293168473Spjd zfs_arc_min = arc_c_min; 5294168473Spjd zfs_arc_max = arc_c_max; 5295168473Spjd 5296168404Spjd arc_anon = &ARC_anon; 5297168404Spjd arc_mru = &ARC_mru; 5298168404Spjd arc_mru_ghost = &ARC_mru_ghost; 5299168404Spjd arc_mfu = &ARC_mfu; 5300168404Spjd arc_mfu_ghost = &ARC_mfu_ghost; 5301185029Spjd arc_l2c_only = &ARC_l2c_only; 5302168404Spjd arc_size = 0; 5303168404Spjd 5304286763Smav multilist_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], 5305286762Smav sizeof (arc_buf_hdr_t), 5306286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5307286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5308286763Smav multilist_create(&arc_mru->arcs_list[ARC_BUFC_DATA], 5309286762Smav sizeof (arc_buf_hdr_t), 5310286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5311286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5312286763Smav multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 5313286762Smav sizeof (arc_buf_hdr_t), 5314286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5315286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5316286763Smav multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 5317286762Smav sizeof (arc_buf_hdr_t), 5318286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5319286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5320286763Smav multilist_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 5321286762Smav sizeof (arc_buf_hdr_t), 5322286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5323286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5324286763Smav multilist_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], 5325286762Smav sizeof (arc_buf_hdr_t), 5326286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5327286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5328286763Smav multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 5329286762Smav sizeof (arc_buf_hdr_t), 5330286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5331286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5332286763Smav multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 5333286762Smav sizeof (arc_buf_hdr_t), 5334286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5335286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5336286763Smav multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 5337286762Smav sizeof (arc_buf_hdr_t), 5338286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5339286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5340286763Smav multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 5341286762Smav sizeof (arc_buf_hdr_t), 5342286763Smav offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), 5343286763Smav zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); 5344168404Spjd 5345286766Smav refcount_create(&arc_anon->arcs_size); 5346286766Smav refcount_create(&arc_mru->arcs_size); 5347286766Smav refcount_create(&arc_mru_ghost->arcs_size); 5348286766Smav refcount_create(&arc_mfu->arcs_size); 5349286766Smav refcount_create(&arc_mfu_ghost->arcs_size); 5350286766Smav refcount_create(&arc_l2c_only->arcs_size); 5351286766Smav 5352168404Spjd buf_init(); 5353168404Spjd 5354286763Smav arc_reclaim_thread_exit = FALSE; 5355286763Smav arc_user_evicts_thread_exit = FALSE; 5356168404Spjd arc_eviction_list = NULL; 5357168404Spjd bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); 5358168404Spjd 5359168404Spjd arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 5360168404Spjd sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 5361168404Spjd 5362168404Spjd if (arc_ksp != NULL) { 5363168404Spjd arc_ksp->ks_data = &arc_stats; 5364286574Smav arc_ksp->ks_update = arc_kstat_update; 5365168404Spjd kstat_install(arc_ksp); 5366168404Spjd } 5367168404Spjd 5368168404Spjd (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 5369168404Spjd TS_RUN, minclsyspri); 5370168404Spjd 5371168404Spjd#ifdef _KERNEL 5372168566Spjd arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL, 5373168404Spjd EVENTHANDLER_PRI_FIRST); 5374168404Spjd#endif 5375168404Spjd 5376286763Smav (void) thread_create(NULL, 0, arc_user_evicts_thread, NULL, 0, &p0, 5377286763Smav TS_RUN, minclsyspri); 5378286763Smav 5379168404Spjd arc_dead = FALSE; 5380185029Spjd arc_warm = B_FALSE; 5381168566Spjd 5382258632Savg /* 5383258632Savg * Calculate maximum amount of dirty data per pool. 5384258632Savg * 5385258632Savg * If it has been set by /etc/system, take that. 5386258632Savg * Otherwise, use a percentage of physical memory defined by 5387258632Savg * zfs_dirty_data_max_percent (default 10%) with a cap at 5388258632Savg * zfs_dirty_data_max_max (default 4GB). 5389258632Savg */ 5390258632Savg if (zfs_dirty_data_max == 0) { 5391258632Savg zfs_dirty_data_max = ptob(physmem) * 5392258632Savg zfs_dirty_data_max_percent / 100; 5393258632Savg zfs_dirty_data_max = MIN(zfs_dirty_data_max, 5394258632Savg zfs_dirty_data_max_max); 5395258632Savg } 5396185029Spjd 5397168566Spjd#ifdef _KERNEL 5398194043Skmacy if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable)) 5399193953Skmacy prefetch_tunable_set = 1; 5400206796Spjd 5401193878Skmacy#ifdef __i386__ 5402193953Skmacy if (prefetch_tunable_set == 0) { 5403196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default on i386 " 5404196863Strasz "-- to enable,\n"); 5405196863Strasz printf(" add \"vfs.zfs.prefetch_disable=0\" " 5406196863Strasz "to /boot/loader.conf.\n"); 5407219089Spjd zfs_prefetch_disable = 1; 5408193878Skmacy } 5409206796Spjd#else 5410193878Skmacy if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) && 5411193953Skmacy prefetch_tunable_set == 0) { 5412196863Strasz printf("ZFS NOTICE: Prefetch is disabled by default if less " 5413196941Strasz "than 4GB of RAM is present;\n" 5414196863Strasz " to enable, add \"vfs.zfs.prefetch_disable=0\" " 5415196863Strasz "to /boot/loader.conf.\n"); 5416219089Spjd zfs_prefetch_disable = 1; 5417193878Skmacy } 5418206796Spjd#endif 5419175633Spjd /* Warn about ZFS memory and address space requirements. */ 5420168696Spjd if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) { 5421168987Sbmah printf("ZFS WARNING: Recommended minimum RAM size is 512MB; " 5422168987Sbmah "expect unstable behavior.\n"); 5423175633Spjd } 5424175633Spjd if (kmem_size() < 512 * (1 << 20)) { 5425173419Spjd printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; " 5426168987Sbmah "expect unstable behavior.\n"); 5427185029Spjd printf(" Consider tuning vm.kmem_size and " 5428173419Spjd "vm.kmem_size_max\n"); 5429185029Spjd printf(" in /boot/loader.conf.\n"); 5430168566Spjd } 5431168566Spjd#endif 5432168404Spjd} 5433168404Spjd 5434168404Spjdvoid 5435168404Spjdarc_fini(void) 5436168404Spjd{ 5437286763Smav mutex_enter(&arc_reclaim_lock); 5438286763Smav arc_reclaim_thread_exit = TRUE; 5439286763Smav /* 5440286763Smav * The reclaim thread will set arc_reclaim_thread_exit back to 5441286763Smav * FALSE when it is finished exiting; we're waiting for that. 5442286763Smav */ 5443286763Smav while (arc_reclaim_thread_exit) { 5444286763Smav cv_signal(&arc_reclaim_thread_cv); 5445286763Smav cv_wait(&arc_reclaim_thread_cv, &arc_reclaim_lock); 5446286763Smav } 5447286763Smav mutex_exit(&arc_reclaim_lock); 5448168404Spjd 5449286763Smav mutex_enter(&arc_user_evicts_lock); 5450286763Smav arc_user_evicts_thread_exit = TRUE; 5451286763Smav /* 5452286763Smav * The user evicts thread will set arc_user_evicts_thread_exit 5453286763Smav * to FALSE when it is finished exiting; we're waiting for that. 5454286763Smav */ 5455286763Smav while (arc_user_evicts_thread_exit) { 5456286763Smav cv_signal(&arc_user_evicts_cv); 5457286763Smav cv_wait(&arc_user_evicts_cv, &arc_user_evicts_lock); 5458286763Smav } 5459286763Smav mutex_exit(&arc_user_evicts_lock); 5460168404Spjd 5461286763Smav /* Use TRUE to ensure *all* buffers are evicted */ 5462286763Smav arc_flush(NULL, TRUE); 5463286763Smav 5464168404Spjd arc_dead = TRUE; 5465168404Spjd 5466168404Spjd if (arc_ksp != NULL) { 5467168404Spjd kstat_delete(arc_ksp); 5468168404Spjd arc_ksp = NULL; 5469168404Spjd } 5470168404Spjd 5471286763Smav mutex_destroy(&arc_reclaim_lock); 5472286763Smav cv_destroy(&arc_reclaim_thread_cv); 5473286763Smav cv_destroy(&arc_reclaim_waiters_cv); 5474168404Spjd 5475286763Smav mutex_destroy(&arc_user_evicts_lock); 5476286763Smav cv_destroy(&arc_user_evicts_cv); 5477168404Spjd 5478286766Smav refcount_destroy(&arc_anon->arcs_size); 5479286766Smav refcount_destroy(&arc_mru->arcs_size); 5480286766Smav refcount_destroy(&arc_mru_ghost->arcs_size); 5481286766Smav refcount_destroy(&arc_mfu->arcs_size); 5482286766Smav refcount_destroy(&arc_mfu_ghost->arcs_size); 5483286766Smav refcount_destroy(&arc_l2c_only->arcs_size); 5484286766Smav 5485286763Smav multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 5486286763Smav multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 5487286763Smav multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 5488286763Smav multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 5489286763Smav multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 5490286763Smav multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 5491286763Smav multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 5492286763Smav multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 5493206796Spjd 5494168404Spjd buf_fini(); 5495168404Spjd 5496286570Smav ASSERT0(arc_loaned_bytes); 5497209962Smm 5498168404Spjd#ifdef _KERNEL 5499168566Spjd if (arc_event_lowmem != NULL) 5500168566Spjd EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem); 5501168404Spjd#endif 5502168404Spjd} 5503185029Spjd 5504185029Spjd/* 5505185029Spjd * Level 2 ARC 5506185029Spjd * 5507185029Spjd * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 5508185029Spjd * It uses dedicated storage devices to hold cached data, which are populated 5509185029Spjd * using large infrequent writes. The main role of this cache is to boost 5510185029Spjd * the performance of random read workloads. The intended L2ARC devices 5511185029Spjd * include short-stroked disks, solid state disks, and other media with 5512185029Spjd * substantially faster read latency than disk. 5513185029Spjd * 5514185029Spjd * +-----------------------+ 5515185029Spjd * | ARC | 5516185029Spjd * +-----------------------+ 5517185029Spjd * | ^ ^ 5518185029Spjd * | | | 5519185029Spjd * l2arc_feed_thread() arc_read() 5520185029Spjd * | | | 5521185029Spjd * | l2arc read | 5522185029Spjd * V | | 5523185029Spjd * +---------------+ | 5524185029Spjd * | L2ARC | | 5525185029Spjd * +---------------+ | 5526185029Spjd * | ^ | 5527185029Spjd * l2arc_write() | | 5528185029Spjd * | | | 5529185029Spjd * V | | 5530185029Spjd * +-------+ +-------+ 5531185029Spjd * | vdev | | vdev | 5532185029Spjd * | cache | | cache | 5533185029Spjd * +-------+ +-------+ 5534185029Spjd * +=========+ .-----. 5535185029Spjd * : L2ARC : |-_____-| 5536185029Spjd * : devices : | Disks | 5537185029Spjd * +=========+ `-_____-' 5538185029Spjd * 5539185029Spjd * Read requests are satisfied from the following sources, in order: 5540185029Spjd * 5541185029Spjd * 1) ARC 5542185029Spjd * 2) vdev cache of L2ARC devices 5543185029Spjd * 3) L2ARC devices 5544185029Spjd * 4) vdev cache of disks 5545185029Spjd * 5) disks 5546185029Spjd * 5547185029Spjd * Some L2ARC device types exhibit extremely slow write performance. 5548185029Spjd * To accommodate for this there are some significant differences between 5549185029Spjd * the L2ARC and traditional cache design: 5550185029Spjd * 5551185029Spjd * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 5552185029Spjd * the ARC behave as usual, freeing buffers and placing headers on ghost 5553185029Spjd * lists. The ARC does not send buffers to the L2ARC during eviction as 5554185029Spjd * this would add inflated write latencies for all ARC memory pressure. 5555185029Spjd * 5556185029Spjd * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 5557185029Spjd * It does this by periodically scanning buffers from the eviction-end of 5558185029Spjd * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 5559251478Sdelphij * not already there. It scans until a headroom of buffers is satisfied, 5560251478Sdelphij * which itself is a buffer for ARC eviction. If a compressible buffer is 5561251478Sdelphij * found during scanning and selected for writing to an L2ARC device, we 5562251478Sdelphij * temporarily boost scanning headroom during the next scan cycle to make 5563251478Sdelphij * sure we adapt to compression effects (which might significantly reduce 5564251478Sdelphij * the data volume we write to L2ARC). The thread that does this is 5565185029Spjd * l2arc_feed_thread(), illustrated below; example sizes are included to 5566185029Spjd * provide a better sense of ratio than this diagram: 5567185029Spjd * 5568185029Spjd * head --> tail 5569185029Spjd * +---------------------+----------+ 5570185029Spjd * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 5571185029Spjd * +---------------------+----------+ | o L2ARC eligible 5572185029Spjd * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 5573185029Spjd * +---------------------+----------+ | 5574185029Spjd * 15.9 Gbytes ^ 32 Mbytes | 5575185029Spjd * headroom | 5576185029Spjd * l2arc_feed_thread() 5577185029Spjd * | 5578185029Spjd * l2arc write hand <--[oooo]--' 5579185029Spjd * | 8 Mbyte 5580185029Spjd * | write max 5581185029Spjd * V 5582185029Spjd * +==============================+ 5583185029Spjd * L2ARC dev |####|#|###|###| |####| ... | 5584185029Spjd * +==============================+ 5585185029Spjd * 32 Gbytes 5586185029Spjd * 5587185029Spjd * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 5588185029Spjd * evicted, then the L2ARC has cached a buffer much sooner than it probably 5589185029Spjd * needed to, potentially wasting L2ARC device bandwidth and storage. It is 5590185029Spjd * safe to say that this is an uncommon case, since buffers at the end of 5591185029Spjd * the ARC lists have moved there due to inactivity. 5592185029Spjd * 5593185029Spjd * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 5594185029Spjd * then the L2ARC simply misses copying some buffers. This serves as a 5595185029Spjd * pressure valve to prevent heavy read workloads from both stalling the ARC 5596185029Spjd * with waits and clogging the L2ARC with writes. This also helps prevent 5597185029Spjd * the potential for the L2ARC to churn if it attempts to cache content too 5598185029Spjd * quickly, such as during backups of the entire pool. 5599185029Spjd * 5600185029Spjd * 5. After system boot and before the ARC has filled main memory, there are 5601185029Spjd * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 5602185029Spjd * lists can remain mostly static. Instead of searching from tail of these 5603185029Spjd * lists as pictured, the l2arc_feed_thread() will search from the list heads 5604185029Spjd * for eligible buffers, greatly increasing its chance of finding them. 5605185029Spjd * 5606185029Spjd * The L2ARC device write speed is also boosted during this time so that 5607185029Spjd * the L2ARC warms up faster. Since there have been no ARC evictions yet, 5608185029Spjd * there are no L2ARC reads, and no fear of degrading read performance 5609185029Spjd * through increased writes. 5610185029Spjd * 5611185029Spjd * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 5612185029Spjd * the vdev queue can aggregate them into larger and fewer writes. Each 5613185029Spjd * device is written to in a rotor fashion, sweeping writes through 5614185029Spjd * available space then repeating. 5615185029Spjd * 5616185029Spjd * 7. The L2ARC does not store dirty content. It never needs to flush 5617185029Spjd * write buffers back to disk based storage. 5618185029Spjd * 5619185029Spjd * 8. If an ARC buffer is written (and dirtied) which also exists in the 5620185029Spjd * L2ARC, the now stale L2ARC buffer is immediately dropped. 5621185029Spjd * 5622185029Spjd * The performance of the L2ARC can be tweaked by a number of tunables, which 5623185029Spjd * may be necessary for different workloads: 5624185029Spjd * 5625185029Spjd * l2arc_write_max max write bytes per interval 5626185029Spjd * l2arc_write_boost extra write bytes during device warmup 5627185029Spjd * l2arc_noprefetch skip caching prefetched buffers 5628185029Spjd * l2arc_headroom number of max device writes to precache 5629251478Sdelphij * l2arc_headroom_boost when we find compressed buffers during ARC 5630251478Sdelphij * scanning, we multiply headroom by this 5631251478Sdelphij * percentage factor for the next scan cycle, 5632251478Sdelphij * since more compressed buffers are likely to 5633251478Sdelphij * be present 5634185029Spjd * l2arc_feed_secs seconds between L2ARC writing 5635185029Spjd * 5636185029Spjd * Tunables may be removed or added as future performance improvements are 5637185029Spjd * integrated, and also may become zpool properties. 5638208373Smm * 5639208373Smm * There are three key functions that control how the L2ARC warms up: 5640208373Smm * 5641208373Smm * l2arc_write_eligible() check if a buffer is eligible to cache 5642208373Smm * l2arc_write_size() calculate how much to write 5643208373Smm * l2arc_write_interval() calculate sleep delay between writes 5644208373Smm * 5645208373Smm * These three functions determine what to write, how much, and how quickly 5646208373Smm * to send writes. 5647185029Spjd */ 5648185029Spjd 5649208373Smmstatic boolean_t 5650275811Sdelphijl2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) 5651208373Smm{ 5652208373Smm /* 5653208373Smm * A buffer is *not* eligible for the L2ARC if it: 5654208373Smm * 1. belongs to a different spa. 5655208373Smm * 2. is already cached on the L2ARC. 5656208373Smm * 3. has an I/O in progress (it may be an incomplete read). 5657208373Smm * 4. is flagged not eligible (zfs property). 5658208373Smm */ 5659275811Sdelphij if (hdr->b_spa != spa_guid) { 5660208373Smm ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch); 5661208373Smm return (B_FALSE); 5662208373Smm } 5663286570Smav if (HDR_HAS_L2HDR(hdr)) { 5664208373Smm ARCSTAT_BUMP(arcstat_l2_write_in_l2); 5665208373Smm return (B_FALSE); 5666208373Smm } 5667275811Sdelphij if (HDR_IO_IN_PROGRESS(hdr)) { 5668208373Smm ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress); 5669208373Smm return (B_FALSE); 5670208373Smm } 5671275811Sdelphij if (!HDR_L2CACHE(hdr)) { 5672208373Smm ARCSTAT_BUMP(arcstat_l2_write_not_cacheable); 5673208373Smm return (B_FALSE); 5674208373Smm } 5675208373Smm 5676208373Smm return (B_TRUE); 5677208373Smm} 5678208373Smm 5679208373Smmstatic uint64_t 5680251478Sdelphijl2arc_write_size(void) 5681208373Smm{ 5682208373Smm uint64_t size; 5683208373Smm 5684251478Sdelphij /* 5685251478Sdelphij * Make sure our globals have meaningful values in case the user 5686251478Sdelphij * altered them. 5687251478Sdelphij */ 5688251478Sdelphij size = l2arc_write_max; 5689251478Sdelphij if (size == 0) { 5690251478Sdelphij cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 5691251478Sdelphij "be greater than zero, resetting it to the default (%d)", 5692251478Sdelphij L2ARC_WRITE_SIZE); 5693251478Sdelphij size = l2arc_write_max = L2ARC_WRITE_SIZE; 5694251478Sdelphij } 5695208373Smm 5696208373Smm if (arc_warm == B_FALSE) 5697251478Sdelphij size += l2arc_write_boost; 5698208373Smm 5699208373Smm return (size); 5700208373Smm 5701208373Smm} 5702208373Smm 5703208373Smmstatic clock_t 5704208373Smml2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 5705208373Smm{ 5706219089Spjd clock_t interval, next, now; 5707208373Smm 5708208373Smm /* 5709208373Smm * If the ARC lists are busy, increase our write rate; if the 5710208373Smm * lists are stale, idle back. This is achieved by checking 5711208373Smm * how much we previously wrote - if it was more than half of 5712208373Smm * what we wanted, schedule the next write much sooner. 5713208373Smm */ 5714208373Smm if (l2arc_feed_again && wrote > (wanted / 2)) 5715208373Smm interval = (hz * l2arc_feed_min_ms) / 1000; 5716208373Smm else 5717208373Smm interval = hz * l2arc_feed_secs; 5718208373Smm 5719219089Spjd now = ddi_get_lbolt(); 5720219089Spjd next = MAX(now, MIN(now + interval, began + interval)); 5721208373Smm 5722208373Smm return (next); 5723208373Smm} 5724208373Smm 5725185029Spjd/* 5726185029Spjd * Cycle through L2ARC devices. This is how L2ARC load balances. 5727185029Spjd * If a device is returned, this also returns holding the spa config lock. 5728185029Spjd */ 5729185029Spjdstatic l2arc_dev_t * 5730185029Spjdl2arc_dev_get_next(void) 5731185029Spjd{ 5732185029Spjd l2arc_dev_t *first, *next = NULL; 5733185029Spjd 5734185029Spjd /* 5735185029Spjd * Lock out the removal of spas (spa_namespace_lock), then removal 5736185029Spjd * of cache devices (l2arc_dev_mtx). Once a device has been selected, 5737185029Spjd * both locks will be dropped and a spa config lock held instead. 5738185029Spjd */ 5739185029Spjd mutex_enter(&spa_namespace_lock); 5740185029Spjd mutex_enter(&l2arc_dev_mtx); 5741185029Spjd 5742185029Spjd /* if there are no vdevs, there is nothing to do */ 5743185029Spjd if (l2arc_ndev == 0) 5744185029Spjd goto out; 5745185029Spjd 5746185029Spjd first = NULL; 5747185029Spjd next = l2arc_dev_last; 5748185029Spjd do { 5749185029Spjd /* loop around the list looking for a non-faulted vdev */ 5750185029Spjd if (next == NULL) { 5751185029Spjd next = list_head(l2arc_dev_list); 5752185029Spjd } else { 5753185029Spjd next = list_next(l2arc_dev_list, next); 5754185029Spjd if (next == NULL) 5755185029Spjd next = list_head(l2arc_dev_list); 5756185029Spjd } 5757185029Spjd 5758185029Spjd /* if we have come back to the start, bail out */ 5759185029Spjd if (first == NULL) 5760185029Spjd first = next; 5761185029Spjd else if (next == first) 5762185029Spjd break; 5763185029Spjd 5764185029Spjd } while (vdev_is_dead(next->l2ad_vdev)); 5765185029Spjd 5766185029Spjd /* if we were unable to find any usable vdevs, return NULL */ 5767185029Spjd if (vdev_is_dead(next->l2ad_vdev)) 5768185029Spjd next = NULL; 5769185029Spjd 5770185029Spjd l2arc_dev_last = next; 5771185029Spjd 5772185029Spjdout: 5773185029Spjd mutex_exit(&l2arc_dev_mtx); 5774185029Spjd 5775185029Spjd /* 5776185029Spjd * Grab the config lock to prevent the 'next' device from being 5777185029Spjd * removed while we are writing to it. 5778185029Spjd */ 5779185029Spjd if (next != NULL) 5780185029Spjd spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 5781185029Spjd mutex_exit(&spa_namespace_lock); 5782185029Spjd 5783185029Spjd return (next); 5784185029Spjd} 5785185029Spjd 5786185029Spjd/* 5787185029Spjd * Free buffers that were tagged for destruction. 5788185029Spjd */ 5789185029Spjdstatic void 5790185029Spjdl2arc_do_free_on_write() 5791185029Spjd{ 5792185029Spjd list_t *buflist; 5793185029Spjd l2arc_data_free_t *df, *df_prev; 5794185029Spjd 5795185029Spjd mutex_enter(&l2arc_free_on_write_mtx); 5796185029Spjd buflist = l2arc_free_on_write; 5797185029Spjd 5798185029Spjd for (df = list_tail(buflist); df; df = df_prev) { 5799185029Spjd df_prev = list_prev(buflist, df); 5800185029Spjd ASSERT(df->l2df_data != NULL); 5801185029Spjd ASSERT(df->l2df_func != NULL); 5802185029Spjd df->l2df_func(df->l2df_data, df->l2df_size); 5803185029Spjd list_remove(buflist, df); 5804185029Spjd kmem_free(df, sizeof (l2arc_data_free_t)); 5805185029Spjd } 5806185029Spjd 5807185029Spjd mutex_exit(&l2arc_free_on_write_mtx); 5808185029Spjd} 5809185029Spjd 5810185029Spjd/* 5811185029Spjd * A write to a cache device has completed. Update all headers to allow 5812185029Spjd * reads from these buffers to begin. 5813185029Spjd */ 5814185029Spjdstatic void 5815185029Spjdl2arc_write_done(zio_t *zio) 5816185029Spjd{ 5817185029Spjd l2arc_write_callback_t *cb; 5818185029Spjd l2arc_dev_t *dev; 5819185029Spjd list_t *buflist; 5820275811Sdelphij arc_buf_hdr_t *head, *hdr, *hdr_prev; 5821185029Spjd kmutex_t *hash_lock; 5822268085Sdelphij int64_t bytes_dropped = 0; 5823185029Spjd 5824185029Spjd cb = zio->io_private; 5825185029Spjd ASSERT(cb != NULL); 5826185029Spjd dev = cb->l2wcb_dev; 5827185029Spjd ASSERT(dev != NULL); 5828185029Spjd head = cb->l2wcb_head; 5829185029Spjd ASSERT(head != NULL); 5830286570Smav buflist = &dev->l2ad_buflist; 5831185029Spjd ASSERT(buflist != NULL); 5832185029Spjd DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 5833185029Spjd l2arc_write_callback_t *, cb); 5834185029Spjd 5835185029Spjd if (zio->io_error != 0) 5836185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_error); 5837185029Spjd 5838185029Spjd /* 5839185029Spjd * All writes completed, or an error was hit. 5840185029Spjd */ 5841286763Smavtop: 5842286763Smav mutex_enter(&dev->l2ad_mtx); 5843275811Sdelphij for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { 5844275811Sdelphij hdr_prev = list_prev(buflist, hdr); 5845185029Spjd 5846275811Sdelphij hash_lock = HDR_LOCK(hdr); 5847286763Smav 5848286763Smav /* 5849286763Smav * We cannot use mutex_enter or else we can deadlock 5850286763Smav * with l2arc_write_buffers (due to swapping the order 5851286763Smav * the hash lock and l2ad_mtx are taken). 5852286763Smav */ 5853185029Spjd if (!mutex_tryenter(hash_lock)) { 5854185029Spjd /* 5855286763Smav * Missed the hash lock. We must retry so we 5856286763Smav * don't leave the ARC_FLAG_L2_WRITING bit set. 5857185029Spjd */ 5858286763Smav ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); 5859286763Smav 5860286763Smav /* 5861286763Smav * We don't want to rescan the headers we've 5862286763Smav * already marked as having been written out, so 5863286763Smav * we reinsert the head node so we can pick up 5864286763Smav * where we left off. 5865286763Smav */ 5866286763Smav list_remove(buflist, head); 5867286763Smav list_insert_after(buflist, hdr, head); 5868286763Smav 5869286763Smav mutex_exit(&dev->l2ad_mtx); 5870286763Smav 5871286763Smav /* 5872286763Smav * We wait for the hash lock to become available 5873286763Smav * to try and prevent busy waiting, and increase 5874286763Smav * the chance we'll be able to acquire the lock 5875286763Smav * the next time around. 5876286763Smav */ 5877286763Smav mutex_enter(hash_lock); 5878286763Smav mutex_exit(hash_lock); 5879286763Smav goto top; 5880185029Spjd } 5881185029Spjd 5882286570Smav /* 5883286763Smav * We could not have been moved into the arc_l2c_only 5884286763Smav * state while in-flight due to our ARC_FLAG_L2_WRITING 5885286763Smav * bit being set. Let's just ensure that's being enforced. 5886286570Smav */ 5887286763Smav ASSERT(HDR_HAS_L1HDR(hdr)); 5888286570Smav 5889286763Smav /* 5890286763Smav * We may have allocated a buffer for L2ARC compression, 5891286763Smav * we must release it to avoid leaking this data. 5892286763Smav */ 5893286763Smav l2arc_release_cdata_buf(hdr); 5894286763Smav 5895185029Spjd if (zio->io_error != 0) { 5896185029Spjd /* 5897185029Spjd * Error - drop L2ARC entry. 5898185029Spjd */ 5899286776Smav list_remove(buflist, hdr); 5900286570Smav trim_map_free(hdr->b_l2hdr.b_dev->l2ad_vdev, 5901286570Smav hdr->b_l2hdr.b_daddr, hdr->b_l2hdr.b_asize, 0); 5902286570Smav hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR; 5903286570Smav 5904286570Smav ARCSTAT_INCR(arcstat_l2_asize, -hdr->b_l2hdr.b_asize); 5905275811Sdelphij ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); 5906286598Smav 5907286598Smav bytes_dropped += hdr->b_l2hdr.b_asize; 5908286598Smav (void) refcount_remove_many(&dev->l2ad_alloc, 5909286598Smav hdr->b_l2hdr.b_asize, hdr); 5910185029Spjd } 5911185029Spjd 5912185029Spjd /* 5913286763Smav * Allow ARC to begin reads and ghost list evictions to 5914286763Smav * this L2ARC entry. 5915185029Spjd */ 5916275811Sdelphij hdr->b_flags &= ~ARC_FLAG_L2_WRITING; 5917185029Spjd 5918185029Spjd mutex_exit(hash_lock); 5919185029Spjd } 5920185029Spjd 5921185029Spjd atomic_inc_64(&l2arc_writes_done); 5922185029Spjd list_remove(buflist, head); 5923286570Smav ASSERT(!HDR_HAS_L1HDR(head)); 5924286570Smav kmem_cache_free(hdr_l2only_cache, head); 5925286570Smav mutex_exit(&dev->l2ad_mtx); 5926185029Spjd 5927268085Sdelphij vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 5928268085Sdelphij 5929185029Spjd l2arc_do_free_on_write(); 5930185029Spjd 5931185029Spjd kmem_free(cb, sizeof (l2arc_write_callback_t)); 5932185029Spjd} 5933185029Spjd 5934185029Spjd/* 5935185029Spjd * A read to a cache device completed. Validate buffer contents before 5936185029Spjd * handing over to the regular ARC routines. 5937185029Spjd */ 5938185029Spjdstatic void 5939185029Spjdl2arc_read_done(zio_t *zio) 5940185029Spjd{ 5941185029Spjd l2arc_read_callback_t *cb; 5942185029Spjd arc_buf_hdr_t *hdr; 5943185029Spjd arc_buf_t *buf; 5944185029Spjd kmutex_t *hash_lock; 5945185029Spjd int equal; 5946185029Spjd 5947185029Spjd ASSERT(zio->io_vd != NULL); 5948185029Spjd ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 5949185029Spjd 5950185029Spjd spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 5951185029Spjd 5952185029Spjd cb = zio->io_private; 5953185029Spjd ASSERT(cb != NULL); 5954185029Spjd buf = cb->l2rcb_buf; 5955185029Spjd ASSERT(buf != NULL); 5956185029Spjd 5957219089Spjd hash_lock = HDR_LOCK(buf->b_hdr); 5958185029Spjd mutex_enter(hash_lock); 5959219089Spjd hdr = buf->b_hdr; 5960219089Spjd ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 5961185029Spjd 5962185029Spjd /* 5963251478Sdelphij * If the buffer was compressed, decompress it first. 5964251478Sdelphij */ 5965251478Sdelphij if (cb->l2rcb_compress != ZIO_COMPRESS_OFF) 5966251478Sdelphij l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress); 5967251478Sdelphij ASSERT(zio->io_data != NULL); 5968251478Sdelphij 5969251478Sdelphij /* 5970185029Spjd * Check this survived the L2ARC journey. 5971185029Spjd */ 5972185029Spjd equal = arc_cksum_equal(buf); 5973185029Spjd if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { 5974185029Spjd mutex_exit(hash_lock); 5975185029Spjd zio->io_private = buf; 5976185029Spjd zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 5977185029Spjd zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 5978185029Spjd arc_read_done(zio); 5979185029Spjd } else { 5980185029Spjd mutex_exit(hash_lock); 5981185029Spjd /* 5982185029Spjd * Buffer didn't survive caching. Increment stats and 5983185029Spjd * reissue to the original storage device. 5984185029Spjd */ 5985185029Spjd if (zio->io_error != 0) { 5986185029Spjd ARCSTAT_BUMP(arcstat_l2_io_error); 5987185029Spjd } else { 5988249195Smm zio->io_error = SET_ERROR(EIO); 5989185029Spjd } 5990185029Spjd if (!equal) 5991185029Spjd ARCSTAT_BUMP(arcstat_l2_cksum_bad); 5992185029Spjd 5993185029Spjd /* 5994185029Spjd * If there's no waiter, issue an async i/o to the primary 5995185029Spjd * storage now. If there *is* a waiter, the caller must 5996185029Spjd * issue the i/o in a context where it's OK to block. 5997185029Spjd */ 5998209962Smm if (zio->io_waiter == NULL) { 5999209962Smm zio_t *pio = zio_unique_parent(zio); 6000209962Smm 6001209962Smm ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 6002209962Smm 6003209962Smm zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, 6004185029Spjd buf->b_data, zio->io_size, arc_read_done, buf, 6005185029Spjd zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); 6006209962Smm } 6007185029Spjd } 6008185029Spjd 6009185029Spjd kmem_free(cb, sizeof (l2arc_read_callback_t)); 6010185029Spjd} 6011185029Spjd 6012185029Spjd/* 6013185029Spjd * This is the list priority from which the L2ARC will search for pages to 6014185029Spjd * cache. This is used within loops (0..3) to cycle through lists in the 6015185029Spjd * desired order. This order can have a significant effect on cache 6016185029Spjd * performance. 6017185029Spjd * 6018185029Spjd * Currently the metadata lists are hit first, MFU then MRU, followed by 6019185029Spjd * the data lists. This function returns a locked list, and also returns 6020185029Spjd * the lock pointer. 6021185029Spjd */ 6022286763Smavstatic multilist_sublist_t * 6023286763Smavl2arc_sublist_lock(int list_num) 6024185029Spjd{ 6025286763Smav multilist_t *ml = NULL; 6026286763Smav unsigned int idx; 6027185029Spjd 6028286762Smav ASSERT(list_num >= 0 && list_num <= 3); 6029206796Spjd 6030286762Smav switch (list_num) { 6031286762Smav case 0: 6032286763Smav ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 6033286762Smav break; 6034286762Smav case 1: 6035286763Smav ml = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 6036286762Smav break; 6037286762Smav case 2: 6038286763Smav ml = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 6039286762Smav break; 6040286762Smav case 3: 6041286763Smav ml = &arc_mru->arcs_list[ARC_BUFC_DATA]; 6042286762Smav break; 6043185029Spjd } 6044185029Spjd 6045286763Smav /* 6046286763Smav * Return a randomly-selected sublist. This is acceptable 6047286763Smav * because the caller feeds only a little bit of data for each 6048286763Smav * call (8MB). Subsequent calls will result in different 6049286763Smav * sublists being selected. 6050286763Smav */ 6051286763Smav idx = multilist_get_random_index(ml); 6052286763Smav return (multilist_sublist_lock(ml, idx)); 6053185029Spjd} 6054185029Spjd 6055185029Spjd/* 6056185029Spjd * Evict buffers from the device write hand to the distance specified in 6057185029Spjd * bytes. This distance may span populated buffers, it may span nothing. 6058185029Spjd * This is clearing a region on the L2ARC device ready for writing. 6059185029Spjd * If the 'all' boolean is set, every buffer is evicted. 6060185029Spjd */ 6061185029Spjdstatic void 6062185029Spjdl2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 6063185029Spjd{ 6064185029Spjd list_t *buflist; 6065275811Sdelphij arc_buf_hdr_t *hdr, *hdr_prev; 6066185029Spjd kmutex_t *hash_lock; 6067185029Spjd uint64_t taddr; 6068185029Spjd 6069286570Smav buflist = &dev->l2ad_buflist; 6070185029Spjd 6071185029Spjd if (!all && dev->l2ad_first) { 6072185029Spjd /* 6073185029Spjd * This is the first sweep through the device. There is 6074185029Spjd * nothing to evict. 6075185029Spjd */ 6076185029Spjd return; 6077185029Spjd } 6078185029Spjd 6079185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { 6080185029Spjd /* 6081185029Spjd * When nearing the end of the device, evict to the end 6082185029Spjd * before the device write hand jumps to the start. 6083185029Spjd */ 6084185029Spjd taddr = dev->l2ad_end; 6085185029Spjd } else { 6086185029Spjd taddr = dev->l2ad_hand + distance; 6087185029Spjd } 6088185029Spjd DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 6089185029Spjd uint64_t, taddr, boolean_t, all); 6090185029Spjd 6091185029Spjdtop: 6092286570Smav mutex_enter(&dev->l2ad_mtx); 6093275811Sdelphij for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { 6094275811Sdelphij hdr_prev = list_prev(buflist, hdr); 6095185029Spjd 6096275811Sdelphij hash_lock = HDR_LOCK(hdr); 6097286763Smav 6098286763Smav /* 6099286763Smav * We cannot use mutex_enter or else we can deadlock 6100286763Smav * with l2arc_write_buffers (due to swapping the order 6101286763Smav * the hash lock and l2ad_mtx are taken). 6102286763Smav */ 6103185029Spjd if (!mutex_tryenter(hash_lock)) { 6104185029Spjd /* 6105185029Spjd * Missed the hash lock. Retry. 6106185029Spjd */ 6107185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 6108286570Smav mutex_exit(&dev->l2ad_mtx); 6109185029Spjd mutex_enter(hash_lock); 6110185029Spjd mutex_exit(hash_lock); 6111185029Spjd goto top; 6112185029Spjd } 6113185029Spjd 6114275811Sdelphij if (HDR_L2_WRITE_HEAD(hdr)) { 6115185029Spjd /* 6116185029Spjd * We hit a write head node. Leave it for 6117185029Spjd * l2arc_write_done(). 6118185029Spjd */ 6119275811Sdelphij list_remove(buflist, hdr); 6120185029Spjd mutex_exit(hash_lock); 6121185029Spjd continue; 6122185029Spjd } 6123185029Spjd 6124286570Smav if (!all && HDR_HAS_L2HDR(hdr) && 6125286570Smav (hdr->b_l2hdr.b_daddr > taddr || 6126286570Smav hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { 6127185029Spjd /* 6128185029Spjd * We've evicted to the target address, 6129185029Spjd * or the end of the device. 6130185029Spjd */ 6131185029Spjd mutex_exit(hash_lock); 6132185029Spjd break; 6133185029Spjd } 6134185029Spjd 6135286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 6136286570Smav if (!HDR_HAS_L1HDR(hdr)) { 6137275811Sdelphij ASSERT(!HDR_L2_READING(hdr)); 6138185029Spjd /* 6139185029Spjd * This doesn't exist in the ARC. Destroy. 6140185029Spjd * arc_hdr_destroy() will call list_remove() 6141185029Spjd * and decrement arcstat_l2_size. 6142185029Spjd */ 6143275811Sdelphij arc_change_state(arc_anon, hdr, hash_lock); 6144275811Sdelphij arc_hdr_destroy(hdr); 6145185029Spjd } else { 6146286570Smav ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); 6147286570Smav ARCSTAT_BUMP(arcstat_l2_evict_l1cached); 6148185029Spjd /* 6149185029Spjd * Invalidate issued or about to be issued 6150185029Spjd * reads, since we may be about to write 6151185029Spjd * over this location. 6152185029Spjd */ 6153275811Sdelphij if (HDR_L2_READING(hdr)) { 6154185029Spjd ARCSTAT_BUMP(arcstat_l2_evict_reading); 6155275811Sdelphij hdr->b_flags |= ARC_FLAG_L2_EVICTED; 6156185029Spjd } 6157185029Spjd 6158286763Smav /* Ensure this header has finished being written */ 6159286763Smav ASSERT(!HDR_L2_WRITING(hdr)); 6160286763Smav ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 6161286763Smav 6162286598Smav arc_hdr_l2hdr_destroy(hdr); 6163185029Spjd } 6164185029Spjd mutex_exit(hash_lock); 6165185029Spjd } 6166286570Smav mutex_exit(&dev->l2ad_mtx); 6167185029Spjd} 6168185029Spjd 6169185029Spjd/* 6170185029Spjd * Find and write ARC buffers to the L2ARC device. 6171185029Spjd * 6172275811Sdelphij * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid 6173185029Spjd * for reading until they have completed writing. 6174251478Sdelphij * The headroom_boost is an in-out parameter used to maintain headroom boost 6175251478Sdelphij * state between calls to this function. 6176251478Sdelphij * 6177251478Sdelphij * Returns the number of bytes actually written (which may be smaller than 6178251478Sdelphij * the delta by which the device hand has changed due to alignment). 6179185029Spjd */ 6180208373Smmstatic uint64_t 6181251478Sdelphijl2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz, 6182251478Sdelphij boolean_t *headroom_boost) 6183185029Spjd{ 6184275811Sdelphij arc_buf_hdr_t *hdr, *hdr_prev, *head; 6185287099Savg uint64_t write_asize, write_sz, headroom, buf_compress_minsz; 6186185029Spjd void *buf_data; 6187251478Sdelphij boolean_t full; 6188185029Spjd l2arc_write_callback_t *cb; 6189185029Spjd zio_t *pio, *wzio; 6190228103Smm uint64_t guid = spa_load_guid(spa); 6191251478Sdelphij const boolean_t do_headroom_boost = *headroom_boost; 6192185029Spjd int try; 6193185029Spjd 6194185029Spjd ASSERT(dev->l2ad_vdev != NULL); 6195185029Spjd 6196251478Sdelphij /* Lower the flag now, we might want to raise it again later. */ 6197251478Sdelphij *headroom_boost = B_FALSE; 6198251478Sdelphij 6199185029Spjd pio = NULL; 6200287099Savg write_sz = write_asize = 0; 6201185029Spjd full = B_FALSE; 6202286570Smav head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); 6203275811Sdelphij head->b_flags |= ARC_FLAG_L2_WRITE_HEAD; 6204286570Smav head->b_flags |= ARC_FLAG_HAS_L2HDR; 6205185029Spjd 6206205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_iter); 6207185029Spjd /* 6208251478Sdelphij * We will want to try to compress buffers that are at least 2x the 6209251478Sdelphij * device sector size. 6210251478Sdelphij */ 6211251478Sdelphij buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift; 6212251478Sdelphij 6213251478Sdelphij /* 6214185029Spjd * Copy buffers for L2ARC writing. 6215185029Spjd */ 6216286762Smav for (try = 0; try <= 3; try++) { 6217286763Smav multilist_sublist_t *mls = l2arc_sublist_lock(try); 6218251478Sdelphij uint64_t passed_sz = 0; 6219251478Sdelphij 6220205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter); 6221185029Spjd 6222185029Spjd /* 6223185029Spjd * L2ARC fast warmup. 6224185029Spjd * 6225185029Spjd * Until the ARC is warm and starts to evict, read from the 6226185029Spjd * head of the ARC lists rather than the tail. 6227185029Spjd */ 6228185029Spjd if (arc_warm == B_FALSE) 6229286763Smav hdr = multilist_sublist_head(mls); 6230185029Spjd else 6231286763Smav hdr = multilist_sublist_tail(mls); 6232275811Sdelphij if (hdr == NULL) 6233205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter); 6234185029Spjd 6235286762Smav headroom = target_sz * l2arc_headroom; 6236251478Sdelphij if (do_headroom_boost) 6237251478Sdelphij headroom = (headroom * l2arc_headroom_boost) / 100; 6238251478Sdelphij 6239275811Sdelphij for (; hdr; hdr = hdr_prev) { 6240251478Sdelphij kmutex_t *hash_lock; 6241251478Sdelphij uint64_t buf_sz; 6242287099Savg uint64_t buf_a_sz; 6243251478Sdelphij 6244185029Spjd if (arc_warm == B_FALSE) 6245286763Smav hdr_prev = multilist_sublist_next(mls, hdr); 6246185029Spjd else 6247286763Smav hdr_prev = multilist_sublist_prev(mls, hdr); 6248275811Sdelphij ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, hdr->b_size); 6249206796Spjd 6250275811Sdelphij hash_lock = HDR_LOCK(hdr); 6251251478Sdelphij if (!mutex_tryenter(hash_lock)) { 6252205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_trylock_fail); 6253185029Spjd /* 6254185029Spjd * Skip this buffer rather than waiting. 6255185029Spjd */ 6256185029Spjd continue; 6257185029Spjd } 6258185029Spjd 6259275811Sdelphij passed_sz += hdr->b_size; 6260185029Spjd if (passed_sz > headroom) { 6261185029Spjd /* 6262185029Spjd * Searched too far. 6263185029Spjd */ 6264185029Spjd mutex_exit(hash_lock); 6265205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_passed_headroom); 6266185029Spjd break; 6267185029Spjd } 6268185029Spjd 6269275811Sdelphij if (!l2arc_write_eligible(guid, hdr)) { 6270185029Spjd mutex_exit(hash_lock); 6271185029Spjd continue; 6272185029Spjd } 6273185029Spjd 6274287099Savg /* 6275287099Savg * Assume that the buffer is not going to be compressed 6276287099Savg * and could take more space on disk because of a larger 6277287099Savg * disk block size. 6278287099Savg */ 6279287099Savg buf_sz = hdr->b_size; 6280287099Savg buf_a_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 6281287099Savg 6282287099Savg if ((write_asize + buf_a_sz) > target_sz) { 6283185029Spjd full = B_TRUE; 6284185029Spjd mutex_exit(hash_lock); 6285205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_full); 6286185029Spjd break; 6287185029Spjd } 6288185029Spjd 6289185029Spjd if (pio == NULL) { 6290185029Spjd /* 6291185029Spjd * Insert a dummy header on the buflist so 6292185029Spjd * l2arc_write_done() can find where the 6293185029Spjd * write buffers begin without searching. 6294185029Spjd */ 6295286763Smav mutex_enter(&dev->l2ad_mtx); 6296286570Smav list_insert_head(&dev->l2ad_buflist, head); 6297286763Smav mutex_exit(&dev->l2ad_mtx); 6298185029Spjd 6299185029Spjd cb = kmem_alloc( 6300185029Spjd sizeof (l2arc_write_callback_t), KM_SLEEP); 6301185029Spjd cb->l2wcb_dev = dev; 6302185029Spjd cb->l2wcb_head = head; 6303185029Spjd pio = zio_root(spa, l2arc_write_done, cb, 6304185029Spjd ZIO_FLAG_CANFAIL); 6305205231Skmacy ARCSTAT_BUMP(arcstat_l2_write_pios); 6306185029Spjd } 6307185029Spjd 6308185029Spjd /* 6309185029Spjd * Create and add a new L2ARC header. 6310185029Spjd */ 6311286570Smav hdr->b_l2hdr.b_dev = dev; 6312275811Sdelphij hdr->b_flags |= ARC_FLAG_L2_WRITING; 6313251478Sdelphij /* 6314251478Sdelphij * Temporarily stash the data buffer in b_tmp_cdata. 6315251478Sdelphij * The subsequent write step will pick it up from 6316286570Smav * there. This is because can't access b_l1hdr.b_buf 6317251478Sdelphij * without holding the hash_lock, which we in turn 6318251478Sdelphij * can't access without holding the ARC list locks 6319251478Sdelphij * (which we want to avoid during compression/writing). 6320251478Sdelphij */ 6321286570Smav HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); 6322286570Smav hdr->b_l2hdr.b_asize = hdr->b_size; 6323286570Smav hdr->b_l1hdr.b_tmp_cdata = hdr->b_l1hdr.b_buf->b_data; 6324251478Sdelphij 6325286598Smav /* 6326286598Smav * Explicitly set the b_daddr field to a known 6327286598Smav * value which means "invalid address". This 6328286598Smav * enables us to differentiate which stage of 6329286598Smav * l2arc_write_buffers() the particular header 6330286598Smav * is in (e.g. this loop, or the one below). 6331286598Smav * ARC_FLAG_L2_WRITING is not enough to make 6332286598Smav * this distinction, and we need to know in 6333286598Smav * order to do proper l2arc vdev accounting in 6334286598Smav * arc_release() and arc_hdr_destroy(). 6335286598Smav * 6336286598Smav * Note, we can't use a new flag to distinguish 6337286598Smav * the two stages because we don't hold the 6338286598Smav * header's hash_lock below, in the second stage 6339286598Smav * of this function. Thus, we can't simply 6340286598Smav * change the b_flags field to denote that the 6341286598Smav * IO has been sent. We can change the b_daddr 6342286598Smav * field of the L2 portion, though, since we'll 6343286598Smav * be holding the l2ad_mtx; which is why we're 6344286598Smav * using it to denote the header's state change. 6345286598Smav */ 6346286598Smav hdr->b_l2hdr.b_daddr = L2ARC_ADDR_UNSET; 6347286570Smav hdr->b_flags |= ARC_FLAG_HAS_L2HDR; 6348185029Spjd 6349286763Smav mutex_enter(&dev->l2ad_mtx); 6350286570Smav list_insert_head(&dev->l2ad_buflist, hdr); 6351286763Smav mutex_exit(&dev->l2ad_mtx); 6352251478Sdelphij 6353185029Spjd /* 6354185029Spjd * Compute and store the buffer cksum before 6355185029Spjd * writing. On debug the cksum is verified first. 6356185029Spjd */ 6357286570Smav arc_cksum_verify(hdr->b_l1hdr.b_buf); 6358286570Smav arc_cksum_compute(hdr->b_l1hdr.b_buf, B_TRUE); 6359185029Spjd 6360185029Spjd mutex_exit(hash_lock); 6361185029Spjd 6362251478Sdelphij write_sz += buf_sz; 6363287099Savg write_asize += buf_a_sz; 6364251478Sdelphij } 6365251478Sdelphij 6366286763Smav multilist_sublist_unlock(mls); 6367251478Sdelphij 6368251478Sdelphij if (full == B_TRUE) 6369251478Sdelphij break; 6370251478Sdelphij } 6371251478Sdelphij 6372251478Sdelphij /* No buffers selected for writing? */ 6373251478Sdelphij if (pio == NULL) { 6374251478Sdelphij ASSERT0(write_sz); 6375286570Smav ASSERT(!HDR_HAS_L1HDR(head)); 6376286570Smav kmem_cache_free(hdr_l2only_cache, head); 6377251478Sdelphij return (0); 6378251478Sdelphij } 6379251478Sdelphij 6380286763Smav mutex_enter(&dev->l2ad_mtx); 6381286763Smav 6382251478Sdelphij /* 6383287099Savg * Note that elsewhere in this file arcstat_l2_asize 6384287099Savg * and the used space on l2ad_vdev are updated using b_asize, 6385287099Savg * which is not necessarily rounded up to the device block size. 6386287099Savg * Too keep accounting consistent we do the same here as well: 6387287099Savg * stats_size accumulates the sum of b_asize of the written buffers, 6388287099Savg * while write_asize accumulates the sum of b_asize rounded up 6389287099Savg * to the device block size. 6390287099Savg * The latter sum is used only to validate the corectness of the code. 6391287099Savg */ 6392287099Savg uint64_t stats_size = 0; 6393287099Savg write_asize = 0; 6394287099Savg 6395287099Savg /* 6396251478Sdelphij * Now start writing the buffers. We're starting at the write head 6397251478Sdelphij * and work backwards, retracing the course of the buffer selector 6398251478Sdelphij * loop above. 6399251478Sdelphij */ 6400286570Smav for (hdr = list_prev(&dev->l2ad_buflist, head); hdr; 6401286570Smav hdr = list_prev(&dev->l2ad_buflist, hdr)) { 6402251478Sdelphij uint64_t buf_sz; 6403251478Sdelphij 6404251478Sdelphij /* 6405286763Smav * We rely on the L1 portion of the header below, so 6406286763Smav * it's invalid for this header to have been evicted out 6407286763Smav * of the ghost cache, prior to being written out. The 6408286763Smav * ARC_FLAG_L2_WRITING bit ensures this won't happen. 6409286763Smav */ 6410286763Smav ASSERT(HDR_HAS_L1HDR(hdr)); 6411286763Smav 6412286763Smav /* 6413251478Sdelphij * We shouldn't need to lock the buffer here, since we flagged 6414275811Sdelphij * it as ARC_FLAG_L2_WRITING in the previous step, but we must 6415275811Sdelphij * take care to only access its L2 cache parameters. In 6416286570Smav * particular, hdr->l1hdr.b_buf may be invalid by now due to 6417275811Sdelphij * ARC eviction. 6418251478Sdelphij */ 6419286570Smav hdr->b_l2hdr.b_daddr = dev->l2ad_hand; 6420251478Sdelphij 6421286570Smav if ((HDR_L2COMPRESS(hdr)) && 6422286570Smav hdr->b_l2hdr.b_asize >= buf_compress_minsz) { 6423286570Smav if (l2arc_compress_buf(hdr)) { 6424251478Sdelphij /* 6425251478Sdelphij * If compression succeeded, enable headroom 6426251478Sdelphij * boost on the next scan cycle. 6427251478Sdelphij */ 6428251478Sdelphij *headroom_boost = B_TRUE; 6429251478Sdelphij } 6430251478Sdelphij } 6431251478Sdelphij 6432251478Sdelphij /* 6433251478Sdelphij * Pick up the buffer data we had previously stashed away 6434251478Sdelphij * (and now potentially also compressed). 6435251478Sdelphij */ 6436286570Smav buf_data = hdr->b_l1hdr.b_tmp_cdata; 6437286570Smav buf_sz = hdr->b_l2hdr.b_asize; 6438251478Sdelphij 6439274172Savg /* 6440286598Smav * We need to do this regardless if buf_sz is zero or 6441286598Smav * not, otherwise, when this l2hdr is evicted we'll 6442286598Smav * remove a reference that was never added. 6443286598Smav */ 6444286598Smav (void) refcount_add_many(&dev->l2ad_alloc, buf_sz, hdr); 6445286598Smav 6446251478Sdelphij /* Compression may have squashed the buffer to zero length. */ 6447251478Sdelphij if (buf_sz != 0) { 6448287099Savg uint64_t buf_a_sz; 6449251478Sdelphij 6450185029Spjd wzio = zio_write_phys(pio, dev->l2ad_vdev, 6451185029Spjd dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, 6452185029Spjd NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, 6453185029Spjd ZIO_FLAG_CANFAIL, B_FALSE); 6454185029Spjd 6455185029Spjd DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 6456185029Spjd zio_t *, wzio); 6457185029Spjd (void) zio_nowait(wzio); 6458185029Spjd 6459287099Savg stats_size += buf_sz; 6460286598Smav 6461185029Spjd /* 6462185029Spjd * Keep the clock hand suitably device-aligned. 6463185029Spjd */ 6464287099Savg buf_a_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); 6465287099Savg write_asize += buf_a_sz; 6466287099Savg dev->l2ad_hand += buf_a_sz; 6467185029Spjd } 6468251478Sdelphij } 6469185029Spjd 6470286570Smav mutex_exit(&dev->l2ad_mtx); 6471185029Spjd 6472251478Sdelphij ASSERT3U(write_asize, <=, target_sz); 6473185029Spjd ARCSTAT_BUMP(arcstat_l2_writes_sent); 6474251478Sdelphij ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize); 6475185029Spjd ARCSTAT_INCR(arcstat_l2_size, write_sz); 6476287099Savg ARCSTAT_INCR(arcstat_l2_asize, stats_size); 6477287099Savg vdev_space_update(dev->l2ad_vdev, stats_size, 0, 0); 6478185029Spjd 6479185029Spjd /* 6480185029Spjd * Bump device hand to the device start if it is approaching the end. 6481185029Spjd * l2arc_evict() will already have evicted ahead for this case. 6482185029Spjd */ 6483185029Spjd if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { 6484185029Spjd dev->l2ad_hand = dev->l2ad_start; 6485185029Spjd dev->l2ad_first = B_FALSE; 6486185029Spjd } 6487185029Spjd 6488208373Smm dev->l2ad_writing = B_TRUE; 6489185029Spjd (void) zio_wait(pio); 6490208373Smm dev->l2ad_writing = B_FALSE; 6491208373Smm 6492251478Sdelphij return (write_asize); 6493185029Spjd} 6494185029Spjd 6495185029Spjd/* 6496251478Sdelphij * Compresses an L2ARC buffer. 6497286570Smav * The data to be compressed must be prefilled in l1hdr.b_tmp_cdata and its 6498251478Sdelphij * size in l2hdr->b_asize. This routine tries to compress the data and 6499251478Sdelphij * depending on the compression result there are three possible outcomes: 6500251478Sdelphij * *) The buffer was incompressible. The original l2hdr contents were left 6501251478Sdelphij * untouched and are ready for writing to an L2 device. 6502251478Sdelphij * *) The buffer was all-zeros, so there is no need to write it to an L2 6503251478Sdelphij * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is 6504251478Sdelphij * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY. 6505251478Sdelphij * *) Compression succeeded and b_tmp_cdata was replaced with a temporary 6506251478Sdelphij * data buffer which holds the compressed data to be written, and b_asize 6507251478Sdelphij * tells us how much data there is. b_compress is set to the appropriate 6508251478Sdelphij * compression algorithm. Once writing is done, invoke 6509251478Sdelphij * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer. 6510251478Sdelphij * 6511251478Sdelphij * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the 6512251478Sdelphij * buffer was incompressible). 6513251478Sdelphij */ 6514251478Sdelphijstatic boolean_t 6515286570Smavl2arc_compress_buf(arc_buf_hdr_t *hdr) 6516251478Sdelphij{ 6517251478Sdelphij void *cdata; 6518268075Sdelphij size_t csize, len, rounded; 6519286570Smav ASSERT(HDR_HAS_L2HDR(hdr)); 6520286570Smav l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 6521251478Sdelphij 6522286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 6523286570Smav ASSERT(HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF); 6524286570Smav ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL); 6525251478Sdelphij 6526251478Sdelphij len = l2hdr->b_asize; 6527251478Sdelphij cdata = zio_data_buf_alloc(len); 6528286570Smav ASSERT3P(cdata, !=, NULL); 6529286570Smav csize = zio_compress_data(ZIO_COMPRESS_LZ4, hdr->b_l1hdr.b_tmp_cdata, 6530269086Sdelphij cdata, l2hdr->b_asize); 6531251478Sdelphij 6532286951Smav rounded = P2ROUNDUP(csize, 6533286951Smav (size_t)1 << l2hdr->b_dev->l2ad_vdev->vdev_ashift); 6534286776Smav if (rounded > csize) { 6535286776Smav bzero((char *)cdata + csize, rounded - csize); 6536286776Smav csize = rounded; 6537286776Smav } 6538286776Smav 6539251478Sdelphij if (csize == 0) { 6540251478Sdelphij /* zero block, indicate that there's nothing to write */ 6541251478Sdelphij zio_data_buf_free(cdata, len); 6542286570Smav HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_EMPTY); 6543251478Sdelphij l2hdr->b_asize = 0; 6544286570Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 6545251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_zeros); 6546251478Sdelphij return (B_TRUE); 6547286776Smav } else if (csize > 0 && csize < len) { 6548251478Sdelphij /* 6549251478Sdelphij * Compression succeeded, we'll keep the cdata around for 6550251478Sdelphij * writing and release it afterwards. 6551251478Sdelphij */ 6552286570Smav HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_LZ4); 6553251478Sdelphij l2hdr->b_asize = csize; 6554286570Smav hdr->b_l1hdr.b_tmp_cdata = cdata; 6555251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_successes); 6556251478Sdelphij return (B_TRUE); 6557251478Sdelphij } else { 6558251478Sdelphij /* 6559251478Sdelphij * Compression failed, release the compressed buffer. 6560251478Sdelphij * l2hdr will be left unmodified. 6561251478Sdelphij */ 6562251478Sdelphij zio_data_buf_free(cdata, len); 6563251478Sdelphij ARCSTAT_BUMP(arcstat_l2_compress_failures); 6564251478Sdelphij return (B_FALSE); 6565251478Sdelphij } 6566251478Sdelphij} 6567251478Sdelphij 6568251478Sdelphij/* 6569251478Sdelphij * Decompresses a zio read back from an l2arc device. On success, the 6570251478Sdelphij * underlying zio's io_data buffer is overwritten by the uncompressed 6571251478Sdelphij * version. On decompression error (corrupt compressed stream), the 6572251478Sdelphij * zio->io_error value is set to signal an I/O error. 6573251478Sdelphij * 6574251478Sdelphij * Please note that the compressed data stream is not checksummed, so 6575251478Sdelphij * if the underlying device is experiencing data corruption, we may feed 6576251478Sdelphij * corrupt data to the decompressor, so the decompressor needs to be 6577251478Sdelphij * able to handle this situation (LZ4 does). 6578251478Sdelphij */ 6579251478Sdelphijstatic void 6580251478Sdelphijl2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c) 6581251478Sdelphij{ 6582251478Sdelphij ASSERT(L2ARC_IS_VALID_COMPRESS(c)); 6583251478Sdelphij 6584251478Sdelphij if (zio->io_error != 0) { 6585251478Sdelphij /* 6586251478Sdelphij * An io error has occured, just restore the original io 6587251478Sdelphij * size in preparation for a main pool read. 6588251478Sdelphij */ 6589251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 6590251478Sdelphij return; 6591251478Sdelphij } 6592251478Sdelphij 6593251478Sdelphij if (c == ZIO_COMPRESS_EMPTY) { 6594251478Sdelphij /* 6595251478Sdelphij * An empty buffer results in a null zio, which means we 6596251478Sdelphij * need to fill its io_data after we're done restoring the 6597251478Sdelphij * buffer's contents. 6598251478Sdelphij */ 6599286570Smav ASSERT(hdr->b_l1hdr.b_buf != NULL); 6600286570Smav bzero(hdr->b_l1hdr.b_buf->b_data, hdr->b_size); 6601286570Smav zio->io_data = zio->io_orig_data = hdr->b_l1hdr.b_buf->b_data; 6602251478Sdelphij } else { 6603251478Sdelphij ASSERT(zio->io_data != NULL); 6604251478Sdelphij /* 6605251478Sdelphij * We copy the compressed data from the start of the arc buffer 6606251478Sdelphij * (the zio_read will have pulled in only what we need, the 6607251478Sdelphij * rest is garbage which we will overwrite at decompression) 6608251478Sdelphij * and then decompress back to the ARC data buffer. This way we 6609251478Sdelphij * can minimize copying by simply decompressing back over the 6610251478Sdelphij * original compressed data (rather than decompressing to an 6611251478Sdelphij * aux buffer and then copying back the uncompressed buffer, 6612251478Sdelphij * which is likely to be much larger). 6613251478Sdelphij */ 6614251478Sdelphij uint64_t csize; 6615251478Sdelphij void *cdata; 6616251478Sdelphij 6617251478Sdelphij csize = zio->io_size; 6618251478Sdelphij cdata = zio_data_buf_alloc(csize); 6619251478Sdelphij bcopy(zio->io_data, cdata, csize); 6620251478Sdelphij if (zio_decompress_data(c, cdata, zio->io_data, csize, 6621251478Sdelphij hdr->b_size) != 0) 6622251478Sdelphij zio->io_error = EIO; 6623251478Sdelphij zio_data_buf_free(cdata, csize); 6624251478Sdelphij } 6625251478Sdelphij 6626251478Sdelphij /* Restore the expected uncompressed IO size. */ 6627251478Sdelphij zio->io_orig_size = zio->io_size = hdr->b_size; 6628251478Sdelphij} 6629251478Sdelphij 6630251478Sdelphij/* 6631251478Sdelphij * Releases the temporary b_tmp_cdata buffer in an l2arc header structure. 6632251478Sdelphij * This buffer serves as a temporary holder of compressed data while 6633251478Sdelphij * the buffer entry is being written to an l2arc device. Once that is 6634251478Sdelphij * done, we can dispose of it. 6635251478Sdelphij */ 6636251478Sdelphijstatic void 6637275811Sdelphijl2arc_release_cdata_buf(arc_buf_hdr_t *hdr) 6638251478Sdelphij{ 6639286763Smav enum zio_compress comp = HDR_GET_COMPRESS(hdr); 6640286763Smav 6641286570Smav ASSERT(HDR_HAS_L1HDR(hdr)); 6642286763Smav ASSERT(comp == ZIO_COMPRESS_OFF || L2ARC_IS_VALID_COMPRESS(comp)); 6643286763Smav 6644286763Smav if (comp == ZIO_COMPRESS_OFF) { 6645251478Sdelphij /* 6646286763Smav * In this case, b_tmp_cdata points to the same buffer 6647286763Smav * as the arc_buf_t's b_data field. We don't want to 6648286763Smav * free it, since the arc_buf_t will handle that. 6649286763Smav */ 6650286763Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 6651286763Smav } else if (comp == ZIO_COMPRESS_EMPTY) { 6652286763Smav /* 6653286763Smav * In this case, b_tmp_cdata was compressed to an empty 6654286763Smav * buffer, thus there's nothing to free and b_tmp_cdata 6655286763Smav * should have been set to NULL in l2arc_write_buffers(). 6656286763Smav */ 6657286763Smav ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); 6658286763Smav } else { 6659286763Smav /* 6660251478Sdelphij * If the data was compressed, then we've allocated a 6661251478Sdelphij * temporary buffer for it, so now we need to release it. 6662251478Sdelphij */ 6663286570Smav ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL); 6664286570Smav zio_data_buf_free(hdr->b_l1hdr.b_tmp_cdata, 6665286570Smav hdr->b_size); 6666286570Smav hdr->b_l1hdr.b_tmp_cdata = NULL; 6667251478Sdelphij } 6668286776Smav 6669251478Sdelphij} 6670251478Sdelphij 6671251478Sdelphij/* 6672185029Spjd * This thread feeds the L2ARC at regular intervals. This is the beating 6673185029Spjd * heart of the L2ARC. 6674185029Spjd */ 6675185029Spjdstatic void 6676185029Spjdl2arc_feed_thread(void *dummy __unused) 6677185029Spjd{ 6678185029Spjd callb_cpr_t cpr; 6679185029Spjd l2arc_dev_t *dev; 6680185029Spjd spa_t *spa; 6681208373Smm uint64_t size, wrote; 6682219089Spjd clock_t begin, next = ddi_get_lbolt(); 6683251478Sdelphij boolean_t headroom_boost = B_FALSE; 6684185029Spjd 6685185029Spjd CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 6686185029Spjd 6687185029Spjd mutex_enter(&l2arc_feed_thr_lock); 6688185029Spjd 6689185029Spjd while (l2arc_thread_exit == 0) { 6690185029Spjd CALLB_CPR_SAFE_BEGIN(&cpr); 6691185029Spjd (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock, 6692219089Spjd next - ddi_get_lbolt()); 6693185029Spjd CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 6694219089Spjd next = ddi_get_lbolt() + hz; 6695185029Spjd 6696185029Spjd /* 6697185029Spjd * Quick check for L2ARC devices. 6698185029Spjd */ 6699185029Spjd mutex_enter(&l2arc_dev_mtx); 6700185029Spjd if (l2arc_ndev == 0) { 6701185029Spjd mutex_exit(&l2arc_dev_mtx); 6702185029Spjd continue; 6703185029Spjd } 6704185029Spjd mutex_exit(&l2arc_dev_mtx); 6705219089Spjd begin = ddi_get_lbolt(); 6706185029Spjd 6707185029Spjd /* 6708185029Spjd * This selects the next l2arc device to write to, and in 6709185029Spjd * doing so the next spa to feed from: dev->l2ad_spa. This 6710185029Spjd * will return NULL if there are now no l2arc devices or if 6711185029Spjd * they are all faulted. 6712185029Spjd * 6713185029Spjd * If a device is returned, its spa's config lock is also 6714185029Spjd * held to prevent device removal. l2arc_dev_get_next() 6715185029Spjd * will grab and release l2arc_dev_mtx. 6716185029Spjd */ 6717185029Spjd if ((dev = l2arc_dev_get_next()) == NULL) 6718185029Spjd continue; 6719185029Spjd 6720185029Spjd spa = dev->l2ad_spa; 6721185029Spjd ASSERT(spa != NULL); 6722185029Spjd 6723185029Spjd /* 6724219089Spjd * If the pool is read-only then force the feed thread to 6725219089Spjd * sleep a little longer. 6726219089Spjd */ 6727219089Spjd if (!spa_writeable(spa)) { 6728219089Spjd next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 6729219089Spjd spa_config_exit(spa, SCL_L2ARC, dev); 6730219089Spjd continue; 6731219089Spjd } 6732219089Spjd 6733219089Spjd /* 6734185029Spjd * Avoid contributing to memory pressure. 6735185029Spjd */ 6736185029Spjd if (arc_reclaim_needed()) { 6737185029Spjd ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 6738185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 6739185029Spjd continue; 6740185029Spjd } 6741185029Spjd 6742185029Spjd ARCSTAT_BUMP(arcstat_l2_feeds); 6743185029Spjd 6744251478Sdelphij size = l2arc_write_size(); 6745185029Spjd 6746185029Spjd /* 6747185029Spjd * Evict L2ARC buffers that will be overwritten. 6748185029Spjd */ 6749185029Spjd l2arc_evict(dev, size, B_FALSE); 6750185029Spjd 6751185029Spjd /* 6752185029Spjd * Write ARC buffers. 6753185029Spjd */ 6754251478Sdelphij wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost); 6755208373Smm 6756208373Smm /* 6757208373Smm * Calculate interval between writes. 6758208373Smm */ 6759208373Smm next = l2arc_write_interval(begin, size, wrote); 6760185029Spjd spa_config_exit(spa, SCL_L2ARC, dev); 6761185029Spjd } 6762185029Spjd 6763185029Spjd l2arc_thread_exit = 0; 6764185029Spjd cv_broadcast(&l2arc_feed_thr_cv); 6765185029Spjd CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 6766185029Spjd thread_exit(); 6767185029Spjd} 6768185029Spjd 6769185029Spjdboolean_t 6770185029Spjdl2arc_vdev_present(vdev_t *vd) 6771185029Spjd{ 6772185029Spjd l2arc_dev_t *dev; 6773185029Spjd 6774185029Spjd mutex_enter(&l2arc_dev_mtx); 6775185029Spjd for (dev = list_head(l2arc_dev_list); dev != NULL; 6776185029Spjd dev = list_next(l2arc_dev_list, dev)) { 6777185029Spjd if (dev->l2ad_vdev == vd) 6778185029Spjd break; 6779185029Spjd } 6780185029Spjd mutex_exit(&l2arc_dev_mtx); 6781185029Spjd 6782185029Spjd return (dev != NULL); 6783185029Spjd} 6784185029Spjd 6785185029Spjd/* 6786185029Spjd * Add a vdev for use by the L2ARC. By this point the spa has already 6787185029Spjd * validated the vdev and opened it. 6788185029Spjd */ 6789185029Spjdvoid 6790219089Spjdl2arc_add_vdev(spa_t *spa, vdev_t *vd) 6791185029Spjd{ 6792185029Spjd l2arc_dev_t *adddev; 6793185029Spjd 6794185029Spjd ASSERT(!l2arc_vdev_present(vd)); 6795185029Spjd 6796255753Sgibbs vdev_ashift_optimize(vd); 6797255753Sgibbs 6798185029Spjd /* 6799185029Spjd * Create a new l2arc device entry. 6800185029Spjd */ 6801185029Spjd adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 6802185029Spjd adddev->l2ad_spa = spa; 6803185029Spjd adddev->l2ad_vdev = vd; 6804219089Spjd adddev->l2ad_start = VDEV_LABEL_START_SIZE; 6805219089Spjd adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 6806185029Spjd adddev->l2ad_hand = adddev->l2ad_start; 6807185029Spjd adddev->l2ad_first = B_TRUE; 6808208373Smm adddev->l2ad_writing = B_FALSE; 6809185029Spjd 6810286570Smav mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); 6811185029Spjd /* 6812185029Spjd * This is a list of all ARC buffers that are still valid on the 6813185029Spjd * device. 6814185029Spjd */ 6815286570Smav list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 6816286570Smav offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); 6817185029Spjd 6818219089Spjd vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 6819286598Smav refcount_create(&adddev->l2ad_alloc); 6820185029Spjd 6821185029Spjd /* 6822185029Spjd * Add device to global list 6823185029Spjd */ 6824185029Spjd mutex_enter(&l2arc_dev_mtx); 6825185029Spjd list_insert_head(l2arc_dev_list, adddev); 6826185029Spjd atomic_inc_64(&l2arc_ndev); 6827185029Spjd mutex_exit(&l2arc_dev_mtx); 6828185029Spjd} 6829185029Spjd 6830185029Spjd/* 6831185029Spjd * Remove a vdev from the L2ARC. 6832185029Spjd */ 6833185029Spjdvoid 6834185029Spjdl2arc_remove_vdev(vdev_t *vd) 6835185029Spjd{ 6836185029Spjd l2arc_dev_t *dev, *nextdev, *remdev = NULL; 6837185029Spjd 6838185029Spjd /* 6839185029Spjd * Find the device by vdev 6840185029Spjd */ 6841185029Spjd mutex_enter(&l2arc_dev_mtx); 6842185029Spjd for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { 6843185029Spjd nextdev = list_next(l2arc_dev_list, dev); 6844185029Spjd if (vd == dev->l2ad_vdev) { 6845185029Spjd remdev = dev; 6846185029Spjd break; 6847185029Spjd } 6848185029Spjd } 6849185029Spjd ASSERT(remdev != NULL); 6850185029Spjd 6851185029Spjd /* 6852185029Spjd * Remove device from global list 6853185029Spjd */ 6854185029Spjd list_remove(l2arc_dev_list, remdev); 6855185029Spjd l2arc_dev_last = NULL; /* may have been invalidated */ 6856185029Spjd atomic_dec_64(&l2arc_ndev); 6857185029Spjd mutex_exit(&l2arc_dev_mtx); 6858185029Spjd 6859185029Spjd /* 6860185029Spjd * Clear all buflists and ARC references. L2ARC device flush. 6861185029Spjd */ 6862185029Spjd l2arc_evict(remdev, 0, B_TRUE); 6863286570Smav list_destroy(&remdev->l2ad_buflist); 6864286570Smav mutex_destroy(&remdev->l2ad_mtx); 6865286598Smav refcount_destroy(&remdev->l2ad_alloc); 6866185029Spjd kmem_free(remdev, sizeof (l2arc_dev_t)); 6867185029Spjd} 6868185029Spjd 6869185029Spjdvoid 6870185029Spjdl2arc_init(void) 6871185029Spjd{ 6872185029Spjd l2arc_thread_exit = 0; 6873185029Spjd l2arc_ndev = 0; 6874185029Spjd l2arc_writes_sent = 0; 6875185029Spjd l2arc_writes_done = 0; 6876185029Spjd 6877185029Spjd mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 6878185029Spjd cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 6879185029Spjd mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 6880185029Spjd mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 6881185029Spjd 6882185029Spjd l2arc_dev_list = &L2ARC_dev_list; 6883185029Spjd l2arc_free_on_write = &L2ARC_free_on_write; 6884185029Spjd list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 6885185029Spjd offsetof(l2arc_dev_t, l2ad_node)); 6886185029Spjd list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 6887185029Spjd offsetof(l2arc_data_free_t, l2df_list_node)); 6888185029Spjd} 6889185029Spjd 6890185029Spjdvoid 6891185029Spjdl2arc_fini(void) 6892185029Spjd{ 6893185029Spjd /* 6894185029Spjd * This is called from dmu_fini(), which is called from spa_fini(); 6895185029Spjd * Because of this, we can assume that all l2arc devices have 6896185029Spjd * already been removed when the pools themselves were removed. 6897185029Spjd */ 6898185029Spjd 6899185029Spjd l2arc_do_free_on_write(); 6900185029Spjd 6901185029Spjd mutex_destroy(&l2arc_feed_thr_lock); 6902185029Spjd cv_destroy(&l2arc_feed_thr_cv); 6903185029Spjd mutex_destroy(&l2arc_dev_mtx); 6904185029Spjd mutex_destroy(&l2arc_free_on_write_mtx); 6905185029Spjd 6906185029Spjd list_destroy(l2arc_dev_list); 6907185029Spjd list_destroy(l2arc_free_on_write); 6908185029Spjd} 6909185029Spjd 6910185029Spjdvoid 6911185029Spjdl2arc_start(void) 6912185029Spjd{ 6913209962Smm if (!(spa_mode_global & FWRITE)) 6914185029Spjd return; 6915185029Spjd 6916185029Spjd (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 6917185029Spjd TS_RUN, minclsyspri); 6918185029Spjd} 6919185029Spjd 6920185029Spjdvoid 6921185029Spjdl2arc_stop(void) 6922185029Spjd{ 6923209962Smm if (!(spa_mode_global & FWRITE)) 6924185029Spjd return; 6925185029Spjd 6926185029Spjd mutex_enter(&l2arc_feed_thr_lock); 6927185029Spjd cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 6928185029Spjd l2arc_thread_exit = 1; 6929185029Spjd while (l2arc_thread_exit != 0) 6930185029Spjd cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 6931185029Spjd mutex_exit(&l2arc_feed_thr_lock); 6932185029Spjd} 6933