1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  24  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
  25  * Copyright (c) 2014 by Saso Kiselkov. All rights reserved.
  26  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
  27  */
  28 
  29 /*
  30  * DVA-based Adjustable Replacement Cache
  31  *
  32  * While much of the theory of operation used here is
  33  * based on the self-tuning, low overhead replacement cache
  34  * presented by Megiddo and Modha at FAST 2003, there are some
  35  * significant differences:
  36  *
  37  * 1. The Megiddo and Modha model assumes any page is evictable.
  38  * Pages in its cache cannot be "locked" into memory.  This makes
  39  * the eviction algorithm simple: evict the last page in the list.
  40  * This also make the performance characteristics easy to reason
  41  * about.  Our cache is not so simple.  At any given moment, some
  42  * subset of the blocks in the cache are un-evictable because we
  43  * have handed out a reference to them.  Blocks are only evictable
  44  * when there are no external references active.  This makes
  45  * eviction far more problematic:  we choose to evict the evictable
  46  * blocks that are the "lowest" in the list.
  47  *
  48  * There are times when it is not possible to evict the requested
  49  * space.  In these circumstances we are unable to adjust the cache
  50  * size.  To prevent the cache growing unbounded at these times we
  51  * implement a "cache throttle" that slows the flow of new data
  52  * into the cache until we can make space available.
  53  *
  54  * 2. The Megiddo and Modha model assumes a fixed cache size.
  55  * Pages are evicted when the cache is full and there is a cache
  56  * miss.  Our model has a variable sized cache.  It grows with
  57  * high use, but also tries to react to memory pressure from the
  58  * operating system: decreasing its size when system memory is
  59  * tight.
  60  *
  61  * 3. The Megiddo and Modha model assumes a fixed page size. All
  62  * elements of the cache are therefore exactly the same size.  So
  63  * when adjusting the cache size following a cache miss, its simply
  64  * a matter of choosing a single page to evict.  In our model, we
  65  * have variable sized cache blocks (rangeing from 512 bytes to
  66  * 128K bytes).  We therefore choose a set of blocks to evict to make
  67  * space for a cache miss that approximates as closely as possible
  68  * the space used by the new block.
  69  *
  70  * See also:  "ARC: A Self-Tuning, Low Overhead Replacement Cache"
  71  * by N. Megiddo & D. Modha, FAST 2003
  72  */
  73 
  74 /*
  75  * The locking model:
  76  *
  77  * A new reference to a cache buffer can be obtained in two
  78  * ways: 1) via a hash table lookup using the DVA as a key,
  79  * or 2) via one of the ARC lists.  The arc_read() interface
  80  * uses method 1, while the internal arc algorithms for
  81  * adjusting the cache use method 2.  We therefore provide two
  82  * types of locks: 1) the hash table lock array, and 2) the
  83  * arc list locks.
  84  *
  85  * Buffers do not have their own mutexes, rather they rely on the
  86  * hash table mutexes for the bulk of their protection (i.e. most
  87  * fields in the arc_buf_hdr_t are protected by these mutexes).
  88  *
  89  * buf_hash_find() returns the appropriate mutex (held) when it
  90  * locates the requested buffer in the hash table.  It returns
  91  * NULL for the mutex if the buffer was not in the table.
  92  *
  93  * buf_hash_remove() expects the appropriate hash mutex to be
  94  * already held before it is invoked.
  95  *
  96  * Each arc state also has a mutex which is used to protect the
  97  * buffer list associated with the state.  When attempting to
  98  * obtain a hash table lock while holding an arc list lock you
  99  * must use: mutex_tryenter() to avoid deadlock.  Also note that
 100  * the active state mutex must be held before the ghost state mutex.
 101  *
 102  * Arc buffers may have an associated eviction callback function.
 103  * This function will be invoked prior to removing the buffer (e.g.
 104  * in arc_do_user_evicts()).  Note however that the data associated
 105  * with the buffer may be evicted prior to the callback.  The callback
 106  * must be made with *no locks held* (to prevent deadlock).  Additionally,
 107  * the users of callbacks must ensure that their private data is
 108  * protected from simultaneous callbacks from arc_clear_callback()
 109  * and arc_do_user_evicts().
 110  *
 111  * Note that the majority of the performance stats are manipulated
 112  * with atomic operations.
 113  *
 114  * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
 115  *
 116  *      - L2ARC buflist creation
 117  *      - L2ARC buflist eviction
 118  *      - L2ARC write completion, which walks L2ARC buflists
 119  *      - ARC header destruction, as it removes from L2ARC buflists
 120  *      - ARC header release, as it removes from L2ARC buflists
 121  */
 122 
 123 #include <sys/spa.h>
 124 #include <sys/zio.h>
 125 #include <sys/zio_compress.h>
 126 #include <sys/zfs_context.h>
 127 #include <sys/arc.h>
 128 #include <sys/refcount.h>
 129 #include <sys/vdev.h>
 130 #include <sys/vdev_impl.h>
 131 #include <sys/dsl_pool.h>
 132 #ifdef _KERNEL
 133 #include <sys/vmsystm.h>
 134 #include <vm/anon.h>
 135 #include <sys/fs/swapnode.h>
 136 #include <sys/dnlc.h>
 137 #endif
 138 #include <sys/callb.h>
 139 #include <sys/kstat.h>
 140 #include <zfs_fletcher.h>
 141 
 142 #ifndef _KERNEL
 143 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
 144 boolean_t arc_watch = B_FALSE;
 145 int arc_procfd;
 146 #endif
 147 
 148 static kmutex_t         arc_reclaim_thr_lock;
 149 static kcondvar_t       arc_reclaim_thr_cv;     /* used to signal reclaim thr */
 150 static uint8_t          arc_thread_exit;
 151 
 152 #define ARC_REDUCE_DNLC_PERCENT 3
 153 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT;
 154 
 155 typedef enum arc_reclaim_strategy {
 156         ARC_RECLAIM_AGGR,               /* Aggressive reclaim strategy */
 157         ARC_RECLAIM_CONS                /* Conservative reclaim strategy */
 158 } arc_reclaim_strategy_t;
 159 
 160 /*
 161  * The number of iterations through arc_evict_*() before we
 162  * drop & reacquire the lock.
 163  */
 164 int arc_evict_iterations = 100;
 165 
 166 /* number of seconds before growing cache again */
 167 static int              arc_grow_retry = 60;
 168 
 169 /* shift of arc_c for calculating both min and max arc_p */
 170 static int              arc_p_min_shift = 4;
 171 
 172 /* log2(fraction of arc to reclaim) */
 173 static int              arc_shrink_shift = 5;
 174 
 175 /*
 176  * minimum lifespan of a prefetch block in clock ticks
 177  * (initialized in arc_init())
 178  */
 179 static int              arc_min_prefetch_lifespan;
 180 
 181 /*
 182  * If this percent of memory is free, don't throttle.
 183  */
 184 int arc_lotsfree_percent = 10;
 185 
 186 static int arc_dead;
 187 
 188 /*
 189  * The arc has filled available memory and has now warmed up.
 190  */
 191 static boolean_t arc_warm;
 192 
 193 /*
 194  * These tunables are for performance analysis.
 195  */
 196 uint64_t zfs_arc_max;
 197 uint64_t zfs_arc_min;
 198 uint64_t zfs_arc_meta_limit = 0;
 199 int zfs_arc_grow_retry = 0;
 200 int zfs_arc_shrink_shift = 0;
 201 int zfs_arc_p_min_shift = 0;
 202 int zfs_disable_dup_eviction = 0;
 203 int zfs_arc_average_blocksize = 8 * 1024; /* 8KB */
 204 
 205 /*
 206  * Note that buffers can be in one of 6 states:
 207  *      ARC_anon        - anonymous (discussed below)
 208  *      ARC_mru         - recently used, currently cached
 209  *      ARC_mru_ghost   - recentely used, no longer in cache
 210  *      ARC_mfu         - frequently used, currently cached
 211  *      ARC_mfu_ghost   - frequently used, no longer in cache
 212  *      ARC_l2c_only    - exists in L2ARC but not other states
 213  * When there are no active references to the buffer, they are
 214  * are linked onto a list in one of these arc states.  These are
 215  * the only buffers that can be evicted or deleted.  Within each
 216  * state there are multiple lists, one for meta-data and one for
 217  * non-meta-data.  Meta-data (indirect blocks, blocks of dnodes,
 218  * etc.) is tracked separately so that it can be managed more
 219  * explicitly: favored over data, limited explicitly.
 220  *
 221  * Anonymous buffers are buffers that are not associated with
 222  * a DVA.  These are buffers that hold dirty block copies
 223  * before they are written to stable storage.  By definition,
 224  * they are "ref'd" and are considered part of arc_mru
 225  * that cannot be freed.  Generally, they will aquire a DVA
 226  * as they are written and migrate onto the arc_mru list.
 227  *
 228  * The ARC_l2c_only state is for buffers that are in the second
 229  * level ARC but no longer in any of the ARC_m* lists.  The second
 230  * level ARC itself may also contain buffers that are in any of
 231  * the ARC_m* states - meaning that a buffer can exist in two
 232  * places.  The reason for the ARC_l2c_only state is to keep the
 233  * buffer header in the hash table, so that reads that hit the
 234  * second level ARC benefit from these fast lookups.
 235  */
 236 
 237 typedef struct arc_state {
 238         list_t  arcs_list[ARC_BUFC_NUMTYPES];   /* list of evictable buffers */
 239         uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */
 240         uint64_t arcs_size;     /* total amount of data in this state */
 241         kmutex_t arcs_mtx;
 242 } arc_state_t;
 243 
 244 /* The 6 states: */
 245 static arc_state_t ARC_anon;
 246 static arc_state_t ARC_mru;
 247 static arc_state_t ARC_mru_ghost;
 248 static arc_state_t ARC_mfu;
 249 static arc_state_t ARC_mfu_ghost;
 250 static arc_state_t ARC_l2c_only;
 251 
 252 typedef struct arc_stats {
 253         kstat_named_t arcstat_hits;
 254         kstat_named_t arcstat_misses;
 255         kstat_named_t arcstat_demand_data_hits;
 256         kstat_named_t arcstat_demand_data_misses;
 257         kstat_named_t arcstat_demand_metadata_hits;
 258         kstat_named_t arcstat_demand_metadata_misses;
 259         kstat_named_t arcstat_prefetch_data_hits;
 260         kstat_named_t arcstat_prefetch_data_misses;
 261         kstat_named_t arcstat_prefetch_metadata_hits;
 262         kstat_named_t arcstat_prefetch_metadata_misses;
 263         kstat_named_t arcstat_mru_hits;
 264         kstat_named_t arcstat_mru_ghost_hits;
 265         kstat_named_t arcstat_mfu_hits;
 266         kstat_named_t arcstat_mfu_ghost_hits;
 267         kstat_named_t arcstat_deleted;
 268         kstat_named_t arcstat_recycle_miss;
 269         /*
 270          * Number of buffers that could not be evicted because the hash lock
 271          * was held by another thread.  The lock may not necessarily be held
 272          * by something using the same buffer, since hash locks are shared
 273          * by multiple buffers.
 274          */
 275         kstat_named_t arcstat_mutex_miss;
 276         /*
 277          * Number of buffers skipped because they have I/O in progress, are
 278          * indrect prefetch buffers that have not lived long enough, or are
 279          * not from the spa we're trying to evict from.
 280          */
 281         kstat_named_t arcstat_evict_skip;
 282         kstat_named_t arcstat_evict_l2_cached;
 283         kstat_named_t arcstat_evict_l2_eligible;
 284         kstat_named_t arcstat_evict_l2_ineligible;
 285         kstat_named_t arcstat_hash_elements;
 286         kstat_named_t arcstat_hash_elements_max;
 287         kstat_named_t arcstat_hash_collisions;
 288         kstat_named_t arcstat_hash_chains;
 289         kstat_named_t arcstat_hash_chain_max;
 290         kstat_named_t arcstat_p;
 291         kstat_named_t arcstat_c;
 292         kstat_named_t arcstat_c_min;
 293         kstat_named_t arcstat_c_max;
 294         kstat_named_t arcstat_size;
 295         kstat_named_t arcstat_hdr_size;
 296         kstat_named_t arcstat_data_size;
 297         kstat_named_t arcstat_other_size;
 298         kstat_named_t arcstat_l2_hits;
 299         kstat_named_t arcstat_l2_misses;
 300         kstat_named_t arcstat_l2_feeds;
 301         kstat_named_t arcstat_l2_rw_clash;
 302         kstat_named_t arcstat_l2_read_bytes;
 303         kstat_named_t arcstat_l2_write_bytes;
 304         kstat_named_t arcstat_l2_writes_sent;
 305         kstat_named_t arcstat_l2_writes_done;
 306         kstat_named_t arcstat_l2_writes_error;
 307         kstat_named_t arcstat_l2_writes_hdr_miss;
 308         kstat_named_t arcstat_l2_evict_lock_retry;
 309         kstat_named_t arcstat_l2_evict_reading;
 310         kstat_named_t arcstat_l2_free_on_write;
 311         kstat_named_t arcstat_l2_abort_lowmem;
 312         kstat_named_t arcstat_l2_cksum_bad;
 313         kstat_named_t arcstat_l2_io_error;
 314         kstat_named_t arcstat_l2_size;
 315         kstat_named_t arcstat_l2_asize;
 316         kstat_named_t arcstat_l2_hdr_size;
 317         kstat_named_t arcstat_l2_compress_successes;
 318         kstat_named_t arcstat_l2_compress_zeros;
 319         kstat_named_t arcstat_l2_compress_failures;
 320         kstat_named_t arcstat_memory_throttle_count;
 321         kstat_named_t arcstat_duplicate_buffers;
 322         kstat_named_t arcstat_duplicate_buffers_size;
 323         kstat_named_t arcstat_duplicate_reads;
 324         kstat_named_t arcstat_meta_used;
 325         kstat_named_t arcstat_meta_limit;
 326         kstat_named_t arcstat_meta_max;
 327 } arc_stats_t;
 328 
 329 static arc_stats_t arc_stats = {
 330         { "hits",                       KSTAT_DATA_UINT64 },
 331         { "misses",                     KSTAT_DATA_UINT64 },
 332         { "demand_data_hits",           KSTAT_DATA_UINT64 },
 333         { "demand_data_misses",         KSTAT_DATA_UINT64 },
 334         { "demand_metadata_hits",       KSTAT_DATA_UINT64 },
 335         { "demand_metadata_misses",     KSTAT_DATA_UINT64 },
 336         { "prefetch_data_hits",         KSTAT_DATA_UINT64 },
 337         { "prefetch_data_misses",       KSTAT_DATA_UINT64 },
 338         { "prefetch_metadata_hits",     KSTAT_DATA_UINT64 },
 339         { "prefetch_metadata_misses",   KSTAT_DATA_UINT64 },
 340         { "mru_hits",                   KSTAT_DATA_UINT64 },
 341         { "mru_ghost_hits",             KSTAT_DATA_UINT64 },
 342         { "mfu_hits",                   KSTAT_DATA_UINT64 },
 343         { "mfu_ghost_hits",             KSTAT_DATA_UINT64 },
 344         { "deleted",                    KSTAT_DATA_UINT64 },
 345         { "recycle_miss",               KSTAT_DATA_UINT64 },
 346         { "mutex_miss",                 KSTAT_DATA_UINT64 },
 347         { "evict_skip",                 KSTAT_DATA_UINT64 },
 348         { "evict_l2_cached",            KSTAT_DATA_UINT64 },
 349         { "evict_l2_eligible",          KSTAT_DATA_UINT64 },
 350         { "evict_l2_ineligible",        KSTAT_DATA_UINT64 },
 351         { "hash_elements",              KSTAT_DATA_UINT64 },
 352         { "hash_elements_max",          KSTAT_DATA_UINT64 },
 353         { "hash_collisions",            KSTAT_DATA_UINT64 },
 354         { "hash_chains",                KSTAT_DATA_UINT64 },
 355         { "hash_chain_max",             KSTAT_DATA_UINT64 },
 356         { "p",                          KSTAT_DATA_UINT64 },
 357         { "c",                          KSTAT_DATA_UINT64 },
 358         { "c_min",                      KSTAT_DATA_UINT64 },
 359         { "c_max",                      KSTAT_DATA_UINT64 },
 360         { "size",                       KSTAT_DATA_UINT64 },
 361         { "hdr_size",                   KSTAT_DATA_UINT64 },
 362         { "data_size",                  KSTAT_DATA_UINT64 },
 363         { "other_size",                 KSTAT_DATA_UINT64 },
 364         { "l2_hits",                    KSTAT_DATA_UINT64 },
 365         { "l2_misses",                  KSTAT_DATA_UINT64 },
 366         { "l2_feeds",                   KSTAT_DATA_UINT64 },
 367         { "l2_rw_clash",                KSTAT_DATA_UINT64 },
 368         { "l2_read_bytes",              KSTAT_DATA_UINT64 },
 369         { "l2_write_bytes",             KSTAT_DATA_UINT64 },
 370         { "l2_writes_sent",             KSTAT_DATA_UINT64 },
 371         { "l2_writes_done",             KSTAT_DATA_UINT64 },
 372         { "l2_writes_error",            KSTAT_DATA_UINT64 },
 373         { "l2_writes_hdr_miss",         KSTAT_DATA_UINT64 },
 374         { "l2_evict_lock_retry",        KSTAT_DATA_UINT64 },
 375         { "l2_evict_reading",           KSTAT_DATA_UINT64 },
 376         { "l2_free_on_write",           KSTAT_DATA_UINT64 },
 377         { "l2_abort_lowmem",            KSTAT_DATA_UINT64 },
 378         { "l2_cksum_bad",               KSTAT_DATA_UINT64 },
 379         { "l2_io_error",                KSTAT_DATA_UINT64 },
 380         { "l2_size",                    KSTAT_DATA_UINT64 },
 381         { "l2_asize",                   KSTAT_DATA_UINT64 },
 382         { "l2_hdr_size",                KSTAT_DATA_UINT64 },
 383         { "l2_compress_successes",      KSTAT_DATA_UINT64 },
 384         { "l2_compress_zeros",          KSTAT_DATA_UINT64 },
 385         { "l2_compress_failures",       KSTAT_DATA_UINT64 },
 386         { "memory_throttle_count",      KSTAT_DATA_UINT64 },
 387         { "duplicate_buffers",          KSTAT_DATA_UINT64 },
 388         { "duplicate_buffers_size",     KSTAT_DATA_UINT64 },
 389         { "duplicate_reads",            KSTAT_DATA_UINT64 },
 390         { "arc_meta_used",              KSTAT_DATA_UINT64 },
 391         { "arc_meta_limit",             KSTAT_DATA_UINT64 },
 392         { "arc_meta_max",               KSTAT_DATA_UINT64 }
 393 };
 394 
 395 #define ARCSTAT(stat)   (arc_stats.stat.value.ui64)
 396 
 397 #define ARCSTAT_INCR(stat, val) \
 398         atomic_add_64(&arc_stats.stat.value.ui64, (val))
 399 
 400 #define ARCSTAT_BUMP(stat)      ARCSTAT_INCR(stat, 1)
 401 #define ARCSTAT_BUMPDOWN(stat)  ARCSTAT_INCR(stat, -1)
 402 
 403 #define ARCSTAT_MAX(stat, val) {                                        \
 404         uint64_t m;                                                     \
 405         while ((val) > (m = arc_stats.stat.value.ui64) &&            \
 406             (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val))))     \
 407                 continue;                                               \
 408 }
 409 
 410 #define ARCSTAT_MAXSTAT(stat) \
 411         ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
 412 
 413 /*
 414  * We define a macro to allow ARC hits/misses to be easily broken down by
 415  * two separate conditions, giving a total of four different subtypes for
 416  * each of hits and misses (so eight statistics total).
 417  */
 418 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
 419         if (cond1) {                                                    \
 420                 if (cond2) {                                            \
 421                         ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
 422                 } else {                                                \
 423                         ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
 424                 }                                                       \
 425         } else {                                                        \
 426                 if (cond2) {                                            \
 427                         ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
 428                 } else {                                                \
 429                         ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
 430                 }                                                       \
 431         }
 432 
 433 kstat_t                 *arc_ksp;
 434 static arc_state_t      *arc_anon;
 435 static arc_state_t      *arc_mru;
 436 static arc_state_t      *arc_mru_ghost;
 437 static arc_state_t      *arc_mfu;
 438 static arc_state_t      *arc_mfu_ghost;
 439 static arc_state_t      *arc_l2c_only;
 440 
 441 /*
 442  * There are several ARC variables that are critical to export as kstats --
 443  * but we don't want to have to grovel around in the kstat whenever we wish to
 444  * manipulate them.  For these variables, we therefore define them to be in
 445  * terms of the statistic variable.  This assures that we are not introducing
 446  * the possibility of inconsistency by having shadow copies of the variables,
 447  * while still allowing the code to be readable.
 448  */
 449 #define arc_size        ARCSTAT(arcstat_size)   /* actual total arc size */
 450 #define arc_p           ARCSTAT(arcstat_p)      /* target size of MRU */
 451 #define arc_c           ARCSTAT(arcstat_c)      /* target size of cache */
 452 #define arc_c_min       ARCSTAT(arcstat_c_min)  /* min target cache size */
 453 #define arc_c_max       ARCSTAT(arcstat_c_max)  /* max target cache size */
 454 #define arc_meta_limit  ARCSTAT(arcstat_meta_limit) /* max size for metadata */
 455 #define arc_meta_used   ARCSTAT(arcstat_meta_used) /* size of metadata */
 456 #define arc_meta_max    ARCSTAT(arcstat_meta_max) /* max size of metadata */
 457 
 458 #define L2ARC_IS_VALID_COMPRESS(_c_) \
 459         ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY)
 460 
 461 static int              arc_no_grow;    /* Don't try to grow cache size */
 462 static uint64_t         arc_tempreserve;
 463 static uint64_t         arc_loaned_bytes;
 464 
 465 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
 466 
 467 typedef struct arc_callback arc_callback_t;
 468 
 469 struct arc_callback {
 470         void                    *acb_private;
 471         arc_done_func_t         *acb_done;
 472         arc_buf_t               *acb_buf;
 473         zio_t                   *acb_zio_dummy;
 474         arc_callback_t          *acb_next;
 475 };
 476 
 477 typedef struct arc_write_callback arc_write_callback_t;
 478 
 479 struct arc_write_callback {
 480         void            *awcb_private;
 481         arc_done_func_t *awcb_ready;
 482         arc_done_func_t *awcb_physdone;
 483         arc_done_func_t *awcb_done;
 484         arc_buf_t       *awcb_buf;
 485 };
 486 
 487 struct arc_buf_hdr {
 488         /* protected by hash lock */
 489         dva_t                   b_dva;
 490         uint64_t                b_birth;
 491         uint64_t                b_cksum0;
 492 
 493         kmutex_t                b_freeze_lock;
 494         zio_cksum_t             *b_freeze_cksum;
 495         void                    *b_thawed;
 496 
 497         arc_buf_hdr_t           *b_hash_next;
 498         arc_buf_t               *b_buf;
 499         uint32_t                b_flags;
 500         uint32_t                b_datacnt;
 501 
 502         arc_callback_t          *b_acb;
 503         kcondvar_t              b_cv;
 504 
 505         /* immutable */
 506         arc_buf_contents_t      b_type;
 507         uint64_t                b_size;
 508         uint64_t                b_spa;
 509 
 510         /* protected by arc state mutex */
 511         arc_state_t             *b_state;
 512         list_node_t             b_arc_node;
 513 
 514         /* updated atomically */
 515         clock_t                 b_arc_access;
 516 
 517         /* self protecting */
 518         refcount_t              b_refcnt;
 519 
 520         l2arc_buf_hdr_t         *b_l2hdr;
 521         list_node_t             b_l2node;
 522 };
 523 
 524 static arc_buf_t *arc_eviction_list;
 525 static kmutex_t arc_eviction_mtx;
 526 static arc_buf_hdr_t arc_eviction_hdr;
 527 static void arc_get_data_buf(arc_buf_t *buf);
 528 static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
 529 static int arc_evict_needed(arc_buf_contents_t type);
 530 static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes);
 531 static void arc_buf_watch(arc_buf_t *buf);
 532 
 533 static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab);
 534 
 535 #define GHOST_STATE(state)      \
 536         ((state) == arc_mru_ghost || (state) == arc_mfu_ghost ||        \
 537         (state) == arc_l2c_only)
 538 
 539 /*
 540  * Private ARC flags.  These flags are private ARC only flags that will show up
 541  * in b_flags in the arc_hdr_buf_t.  Some flags are publicly declared, and can
 542  * be passed in as arc_flags in things like arc_read.  However, these flags
 543  * should never be passed and should only be set by ARC code.  When adding new
 544  * public flags, make sure not to smash the private ones.
 545  */
 546 
 547 #define ARC_IN_HASH_TABLE       (1 << 9)  /* this buffer is hashed */
 548 #define ARC_IO_IN_PROGRESS      (1 << 10) /* I/O in progress for buf */
 549 #define ARC_IO_ERROR            (1 << 11) /* I/O failed for buf */
 550 #define ARC_FREED_IN_READ       (1 << 12) /* buf freed while in read */
 551 #define ARC_BUF_AVAILABLE       (1 << 13) /* block not in active use */
 552 #define ARC_INDIRECT            (1 << 14) /* this is an indirect block */
 553 #define ARC_FREE_IN_PROGRESS    (1 << 15) /* hdr about to be freed */
 554 #define ARC_L2_WRITING          (1 << 16) /* L2ARC write in progress */
 555 #define ARC_L2_EVICTED          (1 << 17) /* evicted during I/O */
 556 #define ARC_L2_WRITE_HEAD       (1 << 18) /* head of write list */
 557 
 558 #define HDR_IN_HASH_TABLE(hdr)  ((hdr)->b_flags & ARC_IN_HASH_TABLE)
 559 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
 560 #define HDR_IO_ERROR(hdr)       ((hdr)->b_flags & ARC_IO_ERROR)
 561 #define HDR_PREFETCH(hdr)       ((hdr)->b_flags & ARC_PREFETCH)
 562 #define HDR_FREED_IN_READ(hdr)  ((hdr)->b_flags & ARC_FREED_IN_READ)
 563 #define HDR_BUF_AVAILABLE(hdr)  ((hdr)->b_flags & ARC_BUF_AVAILABLE)
 564 #define HDR_FREE_IN_PROGRESS(hdr)       ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
 565 #define HDR_L2CACHE(hdr)        ((hdr)->b_flags & ARC_L2CACHE)
 566 #define HDR_L2_READING(hdr)     ((hdr)->b_flags & ARC_IO_IN_PROGRESS &&  \
 567                                     (hdr)->b_l2hdr != NULL)
 568 #define HDR_L2_WRITING(hdr)     ((hdr)->b_flags & ARC_L2_WRITING)
 569 #define HDR_L2_EVICTED(hdr)     ((hdr)->b_flags & ARC_L2_EVICTED)
 570 #define HDR_L2_WRITE_HEAD(hdr)  ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
 571 
 572 /*
 573  * Other sizes
 574  */
 575 
 576 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
 577 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
 578 
 579 /*
 580  * Hash table routines
 581  */
 582 
 583 #define HT_LOCK_PAD     64
 584 
 585 struct ht_lock {
 586         kmutex_t        ht_lock;
 587 #ifdef _KERNEL
 588         unsigned char   pad[(HT_LOCK_PAD - sizeof (kmutex_t))];
 589 #endif
 590 };
 591 
 592 #define BUF_LOCKS 256
 593 typedef struct buf_hash_table {
 594         uint64_t ht_mask;
 595         arc_buf_hdr_t **ht_table;
 596         struct ht_lock ht_locks[BUF_LOCKS];
 597 } buf_hash_table_t;
 598 
 599 static buf_hash_table_t buf_hash_table;
 600 
 601 #define BUF_HASH_INDEX(spa, dva, birth) \
 602         (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
 603 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
 604 #define BUF_HASH_LOCK(idx)      (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
 605 #define HDR_LOCK(hdr) \
 606         (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
 607 
 608 uint64_t zfs_crc64_table[256];
 609 
 610 /*
 611  * Level 2 ARC
 612  */
 613 
 614 #define L2ARC_WRITE_SIZE        (8 * 1024 * 1024)       /* initial write max */
 615 #define L2ARC_HEADROOM          2                       /* num of writes */
 616 /*
 617  * If we discover during ARC scan any buffers to be compressed, we boost
 618  * our headroom for the next scanning cycle by this percentage multiple.
 619  */
 620 #define L2ARC_HEADROOM_BOOST    200
 621 #define L2ARC_FEED_SECS         1               /* caching interval secs */
 622 #define L2ARC_FEED_MIN_MS       200             /* min caching interval ms */
 623 
 624 #define l2arc_writes_sent       ARCSTAT(arcstat_l2_writes_sent)
 625 #define l2arc_writes_done       ARCSTAT(arcstat_l2_writes_done)
 626 
 627 /* L2ARC Performance Tunables */
 628 uint64_t l2arc_write_max = L2ARC_WRITE_SIZE;    /* default max write size */
 629 uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE;  /* extra write during warmup */
 630 uint64_t l2arc_headroom = L2ARC_HEADROOM;       /* number of dev writes */
 631 uint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST;
 632 uint64_t l2arc_feed_secs = L2ARC_FEED_SECS;     /* interval seconds */
 633 uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */
 634 boolean_t l2arc_noprefetch = B_TRUE;            /* don't cache prefetch bufs */
 635 boolean_t l2arc_feed_again = B_TRUE;            /* turbo warmup */
 636 boolean_t l2arc_norw = B_TRUE;                  /* no reads during writes */
 637 
 638 /*
 639  * L2ARC Internals
 640  */
 641 typedef struct l2arc_dev {
 642         vdev_t                  *l2ad_vdev;     /* vdev */
 643         spa_t                   *l2ad_spa;      /* spa */
 644         uint64_t                l2ad_hand;      /* next write location */
 645         uint64_t                l2ad_start;     /* first addr on device */
 646         uint64_t                l2ad_end;       /* last addr on device */
 647         uint64_t                l2ad_evict;     /* last addr eviction reached */
 648         boolean_t               l2ad_first;     /* first sweep through */
 649         boolean_t               l2ad_writing;   /* currently writing */
 650         list_t                  *l2ad_buflist;  /* buffer list */
 651         list_node_t             l2ad_node;      /* device list node */
 652 } l2arc_dev_t;
 653 
 654 static list_t L2ARC_dev_list;                   /* device list */
 655 static list_t *l2arc_dev_list;                  /* device list pointer */
 656 static kmutex_t l2arc_dev_mtx;                  /* device list mutex */
 657 static l2arc_dev_t *l2arc_dev_last;             /* last device used */
 658 static kmutex_t l2arc_buflist_mtx;              /* mutex for all buflists */
 659 static list_t L2ARC_free_on_write;              /* free after write buf list */
 660 static list_t *l2arc_free_on_write;             /* free after write list ptr */
 661 static kmutex_t l2arc_free_on_write_mtx;        /* mutex for list */
 662 static uint64_t l2arc_ndev;                     /* number of devices */
 663 
 664 typedef struct l2arc_read_callback {
 665         arc_buf_t               *l2rcb_buf;             /* read buffer */
 666         spa_t                   *l2rcb_spa;             /* spa */
 667         blkptr_t                l2rcb_bp;               /* original blkptr */
 668         zbookmark_phys_t        l2rcb_zb;               /* original bookmark */
 669         int                     l2rcb_flags;            /* original flags */
 670         enum zio_compress       l2rcb_compress;         /* applied compress */
 671 } l2arc_read_callback_t;
 672 
 673 typedef struct l2arc_write_callback {
 674         l2arc_dev_t     *l2wcb_dev;             /* device info */
 675         arc_buf_hdr_t   *l2wcb_head;            /* head of write buflist */
 676 } l2arc_write_callback_t;
 677 
 678 struct l2arc_buf_hdr {
 679         /* protected by arc_buf_hdr  mutex */
 680         l2arc_dev_t             *b_dev;         /* L2ARC device */
 681         uint64_t                b_daddr;        /* disk address, offset byte */
 682         /* compression applied to buffer data */
 683         enum zio_compress       b_compress;
 684         /* real alloc'd buffer size depending on b_compress applied */
 685         int                     b_asize;
 686         /* temporary buffer holder for in-flight compressed data */
 687         void                    *b_tmp_cdata;
 688 };
 689 
 690 typedef struct l2arc_data_free {
 691         /* protected by l2arc_free_on_write_mtx */
 692         void            *l2df_data;
 693         size_t          l2df_size;
 694         void            (*l2df_func)(void *, size_t);
 695         list_node_t     l2df_list_node;
 696 } l2arc_data_free_t;
 697 
 698 static kmutex_t l2arc_feed_thr_lock;
 699 static kcondvar_t l2arc_feed_thr_cv;
 700 static uint8_t l2arc_thread_exit;
 701 
 702 static void l2arc_read_done(zio_t *zio);
 703 static void l2arc_hdr_stat_add(void);
 704 static void l2arc_hdr_stat_remove(void);
 705 
 706 static boolean_t l2arc_compress_buf(l2arc_buf_hdr_t *l2hdr);
 707 static void l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr,
 708     enum zio_compress c);
 709 static void l2arc_release_cdata_buf(arc_buf_hdr_t *ab);
 710 
 711 static uint64_t
 712 buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
 713 {
 714         uint8_t *vdva = (uint8_t *)dva;
 715         uint64_t crc = -1ULL;
 716         int i;
 717 
 718         ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
 719 
 720         for (i = 0; i < sizeof (dva_t); i++)
 721                 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
 722 
 723         crc ^= (spa>>8) ^ birth;
 724 
 725         return (crc);
 726 }
 727 
 728 #define BUF_EMPTY(buf)                                          \
 729         ((buf)->b_dva.dva_word[0] == 0 &&                    \
 730         (buf)->b_dva.dva_word[1] == 0 &&                     \
 731         (buf)->b_cksum0 == 0)
 732 
 733 #define BUF_EQUAL(spa, dva, birth, buf)                         \
 734         ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) &&       \
 735         ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) &&       \
 736         ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
 737 
 738 static void
 739 buf_discard_identity(arc_buf_hdr_t *hdr)
 740 {
 741         hdr->b_dva.dva_word[0] = 0;
 742         hdr->b_dva.dva_word[1] = 0;
 743         hdr->b_birth = 0;
 744         hdr->b_cksum0 = 0;
 745 }
 746 
 747 static arc_buf_hdr_t *
 748 buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp)
 749 {
 750         const dva_t *dva = BP_IDENTITY(bp);
 751         uint64_t birth = BP_PHYSICAL_BIRTH(bp);
 752         uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
 753         kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
 754         arc_buf_hdr_t *buf;
 755 
 756         mutex_enter(hash_lock);
 757         for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
 758             buf = buf->b_hash_next) {
 759                 if (BUF_EQUAL(spa, dva, birth, buf)) {
 760                         *lockp = hash_lock;
 761                         return (buf);
 762                 }
 763         }
 764         mutex_exit(hash_lock);
 765         *lockp = NULL;
 766         return (NULL);
 767 }
 768 
 769 /*
 770  * Insert an entry into the hash table.  If there is already an element
 771  * equal to elem in the hash table, then the already existing element
 772  * will be returned and the new element will not be inserted.
 773  * Otherwise returns NULL.
 774  */
 775 static arc_buf_hdr_t *
 776 buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
 777 {
 778         uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
 779         kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
 780         arc_buf_hdr_t *fbuf;
 781         uint32_t i;
 782 
 783         ASSERT(!DVA_IS_EMPTY(&buf->b_dva));
 784         ASSERT(buf->b_birth != 0);
 785         ASSERT(!HDR_IN_HASH_TABLE(buf));
 786         *lockp = hash_lock;
 787         mutex_enter(hash_lock);
 788         for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
 789             fbuf = fbuf->b_hash_next, i++) {
 790                 if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
 791                         return (fbuf);
 792         }
 793 
 794         buf->b_hash_next = buf_hash_table.ht_table[idx];
 795         buf_hash_table.ht_table[idx] = buf;
 796         buf->b_flags |= ARC_IN_HASH_TABLE;
 797 
 798         /* collect some hash table performance data */
 799         if (i > 0) {
 800                 ARCSTAT_BUMP(arcstat_hash_collisions);
 801                 if (i == 1)
 802                         ARCSTAT_BUMP(arcstat_hash_chains);
 803 
 804                 ARCSTAT_MAX(arcstat_hash_chain_max, i);
 805         }
 806 
 807         ARCSTAT_BUMP(arcstat_hash_elements);
 808         ARCSTAT_MAXSTAT(arcstat_hash_elements);
 809 
 810         return (NULL);
 811 }
 812 
 813 static void
 814 buf_hash_remove(arc_buf_hdr_t *buf)
 815 {
 816         arc_buf_hdr_t *fbuf, **bufp;
 817         uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
 818 
 819         ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
 820         ASSERT(HDR_IN_HASH_TABLE(buf));
 821 
 822         bufp = &buf_hash_table.ht_table[idx];
 823         while ((fbuf = *bufp) != buf) {
 824                 ASSERT(fbuf != NULL);
 825                 bufp = &fbuf->b_hash_next;
 826         }
 827         *bufp = buf->b_hash_next;
 828         buf->b_hash_next = NULL;
 829         buf->b_flags &= ~ARC_IN_HASH_TABLE;
 830 
 831         /* collect some hash table performance data */
 832         ARCSTAT_BUMPDOWN(arcstat_hash_elements);
 833 
 834         if (buf_hash_table.ht_table[idx] &&
 835             buf_hash_table.ht_table[idx]->b_hash_next == NULL)
 836                 ARCSTAT_BUMPDOWN(arcstat_hash_chains);
 837 }
 838 
 839 /*
 840  * Global data structures and functions for the buf kmem cache.
 841  */
 842 static kmem_cache_t *hdr_cache;
 843 static kmem_cache_t *buf_cache;
 844 
 845 static void
 846 buf_fini(void)
 847 {
 848         int i;
 849 
 850         kmem_free(buf_hash_table.ht_table,
 851             (buf_hash_table.ht_mask + 1) * sizeof (void *));
 852         for (i = 0; i < BUF_LOCKS; i++)
 853                 mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
 854         kmem_cache_destroy(hdr_cache);
 855         kmem_cache_destroy(buf_cache);
 856 }
 857 
 858 /*
 859  * Constructor callback - called when the cache is empty
 860  * and a new buf is requested.
 861  */
 862 /* ARGSUSED */
 863 static int
 864 hdr_cons(void *vbuf, void *unused, int kmflag)
 865 {
 866         arc_buf_hdr_t *buf = vbuf;
 867 
 868         bzero(buf, sizeof (arc_buf_hdr_t));
 869         refcount_create(&buf->b_refcnt);
 870         cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
 871         mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
 872         arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
 873 
 874         return (0);
 875 }
 876 
 877 /* ARGSUSED */
 878 static int
 879 buf_cons(void *vbuf, void *unused, int kmflag)
 880 {
 881         arc_buf_t *buf = vbuf;
 882 
 883         bzero(buf, sizeof (arc_buf_t));
 884         mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
 885         arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
 886 
 887         return (0);
 888 }
 889 
 890 /*
 891  * Destructor callback - called when a cached buf is
 892  * no longer required.
 893  */
 894 /* ARGSUSED */
 895 static void
 896 hdr_dest(void *vbuf, void *unused)
 897 {
 898         arc_buf_hdr_t *buf = vbuf;
 899 
 900         ASSERT(BUF_EMPTY(buf));
 901         refcount_destroy(&buf->b_refcnt);
 902         cv_destroy(&buf->b_cv);
 903         mutex_destroy(&buf->b_freeze_lock);
 904         arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
 905 }
 906 
 907 /* ARGSUSED */
 908 static void
 909 buf_dest(void *vbuf, void *unused)
 910 {
 911         arc_buf_t *buf = vbuf;
 912 
 913         mutex_destroy(&buf->b_evict_lock);
 914         arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
 915 }
 916 
 917 /*
 918  * Reclaim callback -- invoked when memory is low.
 919  */
 920 /* ARGSUSED */
 921 static void
 922 hdr_recl(void *unused)
 923 {
 924         dprintf("hdr_recl called\n");
 925         /*
 926          * umem calls the reclaim func when we destroy the buf cache,
 927          * which is after we do arc_fini().
 928          */
 929         if (!arc_dead)
 930                 cv_signal(&arc_reclaim_thr_cv);
 931 }
 932 
 933 static void
 934 buf_init(void)
 935 {
 936         uint64_t *ct;
 937         uint64_t hsize = 1ULL << 12;
 938         int i, j;
 939 
 940         /*
 941          * The hash table is big enough to fill all of physical memory
 942          * with an average block size of zfs_arc_average_blocksize (default 8K).
 943          * By default, the table will take up
 944          * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
 945          */
 946         while (hsize * zfs_arc_average_blocksize < physmem * PAGESIZE)
 947                 hsize <<= 1;
 948 retry:
 949         buf_hash_table.ht_mask = hsize - 1;
 950         buf_hash_table.ht_table =
 951             kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
 952         if (buf_hash_table.ht_table == NULL) {
 953                 ASSERT(hsize > (1ULL << 8));
 954                 hsize >>= 1;
 955                 goto retry;
 956         }
 957 
 958         hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
 959             0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
 960         buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
 961             0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
 962 
 963         for (i = 0; i < 256; i++)
 964                 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
 965                         *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
 966 
 967         for (i = 0; i < BUF_LOCKS; i++) {
 968                 mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
 969                     NULL, MUTEX_DEFAULT, NULL);
 970         }
 971 }
 972 
 973 #define ARC_MINTIME     (hz>>4) /* 62 ms */
 974 
 975 static void
 976 arc_cksum_verify(arc_buf_t *buf)
 977 {
 978         zio_cksum_t zc;
 979 
 980         if (!(zfs_flags & ZFS_DEBUG_MODIFY))
 981                 return;
 982 
 983         mutex_enter(&buf->b_hdr->b_freeze_lock);
 984         if (buf->b_hdr->b_freeze_cksum == NULL ||
 985             (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
 986                 mutex_exit(&buf->b_hdr->b_freeze_lock);
 987                 return;
 988         }
 989         fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
 990         if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
 991                 panic("buffer modified while frozen!");
 992         mutex_exit(&buf->b_hdr->b_freeze_lock);
 993 }
 994 
 995 static int
 996 arc_cksum_equal(arc_buf_t *buf)
 997 {
 998         zio_cksum_t zc;
 999         int equal;
1000 
1001         mutex_enter(&buf->b_hdr->b_freeze_lock);
1002         fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
1003         equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
1004         mutex_exit(&buf->b_hdr->b_freeze_lock);
1005 
1006         return (equal);
1007 }
1008 
1009 static void
1010 arc_cksum_compute(arc_buf_t *buf, boolean_t force)
1011 {
1012         if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
1013                 return;
1014 
1015         mutex_enter(&buf->b_hdr->b_freeze_lock);
1016         if (buf->b_hdr->b_freeze_cksum != NULL) {
1017                 mutex_exit(&buf->b_hdr->b_freeze_lock);
1018                 return;
1019         }
1020         buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP);
1021         fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
1022             buf->b_hdr->b_freeze_cksum);
1023         mutex_exit(&buf->b_hdr->b_freeze_lock);
1024         arc_buf_watch(buf);
1025 }
1026 
1027 #ifndef _KERNEL
1028 typedef struct procctl {
1029         long cmd;
1030         prwatch_t prwatch;
1031 } procctl_t;
1032 #endif
1033 
1034 /* ARGSUSED */
1035 static void
1036 arc_buf_unwatch(arc_buf_t *buf)
1037 {
1038 #ifndef _KERNEL
1039         if (arc_watch) {
1040                 int result;
1041                 procctl_t ctl;
1042                 ctl.cmd = PCWATCH;
1043                 ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data;
1044                 ctl.prwatch.pr_size = 0;
1045                 ctl.prwatch.pr_wflags = 0;
1046                 result = write(arc_procfd, &ctl, sizeof (ctl));
1047                 ASSERT3U(result, ==, sizeof (ctl));
1048         }
1049 #endif
1050 }
1051 
1052 /* ARGSUSED */
1053 static void
1054 arc_buf_watch(arc_buf_t *buf)
1055 {
1056 #ifndef _KERNEL
1057         if (arc_watch) {
1058                 int result;
1059                 procctl_t ctl;
1060                 ctl.cmd = PCWATCH;
1061                 ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data;
1062                 ctl.prwatch.pr_size = buf->b_hdr->b_size;
1063                 ctl.prwatch.pr_wflags = WA_WRITE;
1064                 result = write(arc_procfd, &ctl, sizeof (ctl));
1065                 ASSERT3U(result, ==, sizeof (ctl));
1066         }
1067 #endif
1068 }
1069 
1070 void
1071 arc_buf_thaw(arc_buf_t *buf)
1072 {
1073         if (zfs_flags & ZFS_DEBUG_MODIFY) {
1074                 if (buf->b_hdr->b_state != arc_anon)
1075                         panic("modifying non-anon buffer!");
1076                 if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
1077                         panic("modifying buffer while i/o in progress!");
1078                 arc_cksum_verify(buf);
1079         }
1080 
1081         mutex_enter(&buf->b_hdr->b_freeze_lock);
1082         if (buf->b_hdr->b_freeze_cksum != NULL) {
1083                 kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1084                 buf->b_hdr->b_freeze_cksum = NULL;
1085         }
1086 
1087         if (zfs_flags & ZFS_DEBUG_MODIFY) {
1088                 if (buf->b_hdr->b_thawed)
1089                         kmem_free(buf->b_hdr->b_thawed, 1);
1090                 buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP);
1091         }
1092 
1093         mutex_exit(&buf->b_hdr->b_freeze_lock);
1094 
1095         arc_buf_unwatch(buf);
1096 }
1097 
1098 void
1099 arc_buf_freeze(arc_buf_t *buf)
1100 {
1101         kmutex_t *hash_lock;
1102 
1103         if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1104                 return;
1105 
1106         hash_lock = HDR_LOCK(buf->b_hdr);
1107         mutex_enter(hash_lock);
1108 
1109         ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
1110             buf->b_hdr->b_state == arc_anon);
1111         arc_cksum_compute(buf, B_FALSE);
1112         mutex_exit(hash_lock);
1113 
1114 }
1115 
1116 static void
1117 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1118 {
1119         ASSERT(MUTEX_HELD(hash_lock));
1120 
1121         if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
1122             (ab->b_state != arc_anon)) {
1123                 uint64_t delta = ab->b_size * ab->b_datacnt;
1124                 list_t *list = &ab->b_state->arcs_list[ab->b_type];
1125                 uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
1126 
1127                 ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
1128                 mutex_enter(&ab->b_state->arcs_mtx);
1129                 ASSERT(list_link_active(&ab->b_arc_node));
1130                 list_remove(list, ab);
1131                 if (GHOST_STATE(ab->b_state)) {
1132                         ASSERT0(ab->b_datacnt);
1133                         ASSERT3P(ab->b_buf, ==, NULL);
1134                         delta = ab->b_size;
1135                 }
1136                 ASSERT(delta > 0);
1137                 ASSERT3U(*size, >=, delta);
1138                 atomic_add_64(size, -delta);
1139                 mutex_exit(&ab->b_state->arcs_mtx);
1140                 /* remove the prefetch flag if we get a reference */
1141                 if (ab->b_flags & ARC_PREFETCH)
1142                         ab->b_flags &= ~ARC_PREFETCH;
1143         }
1144 }
1145 
1146 static int
1147 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1148 {
1149         int cnt;
1150         arc_state_t *state = ab->b_state;
1151 
1152         ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
1153         ASSERT(!GHOST_STATE(state));
1154 
1155         if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
1156             (state != arc_anon)) {
1157                 uint64_t *size = &state->arcs_lsize[ab->b_type];
1158 
1159                 ASSERT(!MUTEX_HELD(&state->arcs_mtx));
1160                 mutex_enter(&state->arcs_mtx);
1161                 ASSERT(!list_link_active(&ab->b_arc_node));
1162                 list_insert_head(&state->arcs_list[ab->b_type], ab);
1163                 ASSERT(ab->b_datacnt > 0);
1164                 atomic_add_64(size, ab->b_size * ab->b_datacnt);
1165                 mutex_exit(&state->arcs_mtx);
1166         }
1167         return (cnt);
1168 }
1169 
1170 /*
1171  * Move the supplied buffer to the indicated state.  The mutex
1172  * for the buffer must be held by the caller.
1173  */
1174 static void
1175 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
1176 {
1177         arc_state_t *old_state = ab->b_state;
1178         int64_t refcnt = refcount_count(&ab->b_refcnt);
1179         uint64_t from_delta, to_delta;
1180 
1181         ASSERT(MUTEX_HELD(hash_lock));
1182         ASSERT3P(new_state, !=, old_state);
1183         ASSERT(refcnt == 0 || ab->b_datacnt > 0);
1184         ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
1185         ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
1186 
1187         from_delta = to_delta = ab->b_datacnt * ab->b_size;
1188 
1189         /*
1190          * If this buffer is evictable, transfer it from the
1191          * old state list to the new state list.
1192          */
1193         if (refcnt == 0) {
1194                 if (old_state != arc_anon) {
1195                         int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
1196                         uint64_t *size = &old_state->arcs_lsize[ab->b_type];
1197 
1198                         if (use_mutex)
1199                                 mutex_enter(&old_state->arcs_mtx);
1200 
1201                         ASSERT(list_link_active(&ab->b_arc_node));
1202                         list_remove(&old_state->arcs_list[ab->b_type], ab);
1203 
1204                         /*
1205                          * If prefetching out of the ghost cache,
1206                          * we will have a non-zero datacnt.
1207                          */
1208                         if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
1209                                 /* ghost elements have a ghost size */
1210                                 ASSERT(ab->b_buf == NULL);
1211                                 from_delta = ab->b_size;
1212                         }
1213                         ASSERT3U(*size, >=, from_delta);
1214                         atomic_add_64(size, -from_delta);
1215 
1216                         if (use_mutex)
1217                                 mutex_exit(&old_state->arcs_mtx);
1218                 }
1219                 if (new_state != arc_anon) {
1220                         int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
1221                         uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1222 
1223                         if (use_mutex)
1224                                 mutex_enter(&new_state->arcs_mtx);
1225 
1226                         list_insert_head(&new_state->arcs_list[ab->b_type], ab);
1227 
1228                         /* ghost elements have a ghost size */
1229                         if (GHOST_STATE(new_state)) {
1230                                 ASSERT(ab->b_datacnt == 0);
1231                                 ASSERT(ab->b_buf == NULL);
1232                                 to_delta = ab->b_size;
1233                         }
1234                         atomic_add_64(size, to_delta);
1235 
1236                         if (use_mutex)
1237                                 mutex_exit(&new_state->arcs_mtx);
1238                 }
1239         }
1240 
1241         ASSERT(!BUF_EMPTY(ab));
1242         if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
1243                 buf_hash_remove(ab);
1244 
1245         /* adjust state sizes */
1246         if (to_delta)
1247                 atomic_add_64(&new_state->arcs_size, to_delta);
1248         if (from_delta) {
1249                 ASSERT3U(old_state->arcs_size, >=, from_delta);
1250                 atomic_add_64(&old_state->arcs_size, -from_delta);
1251         }
1252         ab->b_state = new_state;
1253 
1254         /* adjust l2arc hdr stats */
1255         if (new_state == arc_l2c_only)
1256                 l2arc_hdr_stat_add();
1257         else if (old_state == arc_l2c_only)
1258                 l2arc_hdr_stat_remove();
1259 }
1260 
1261 void
1262 arc_space_consume(uint64_t space, arc_space_type_t type)
1263 {
1264         ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1265 
1266         switch (type) {
1267         case ARC_SPACE_DATA:
1268                 ARCSTAT_INCR(arcstat_data_size, space);
1269                 break;
1270         case ARC_SPACE_OTHER:
1271                 ARCSTAT_INCR(arcstat_other_size, space);
1272                 break;
1273         case ARC_SPACE_HDRS:
1274                 ARCSTAT_INCR(arcstat_hdr_size, space);
1275                 break;
1276         case ARC_SPACE_L2HDRS:
1277                 ARCSTAT_INCR(arcstat_l2_hdr_size, space);
1278                 break;
1279         }
1280 
1281         ARCSTAT_INCR(arcstat_meta_used, space);
1282         atomic_add_64(&arc_size, space);
1283 }
1284 
1285 void
1286 arc_space_return(uint64_t space, arc_space_type_t type)
1287 {
1288         ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1289 
1290         switch (type) {
1291         case ARC_SPACE_DATA:
1292                 ARCSTAT_INCR(arcstat_data_size, -space);
1293                 break;
1294         case ARC_SPACE_OTHER:
1295                 ARCSTAT_INCR(arcstat_other_size, -space);
1296                 break;
1297         case ARC_SPACE_HDRS:
1298                 ARCSTAT_INCR(arcstat_hdr_size, -space);
1299                 break;
1300         case ARC_SPACE_L2HDRS:
1301                 ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
1302                 break;
1303         }
1304 
1305         ASSERT(arc_meta_used >= space);
1306         if (arc_meta_max < arc_meta_used)
1307                 arc_meta_max = arc_meta_used;
1308         ARCSTAT_INCR(arcstat_meta_used, -space);
1309         ASSERT(arc_size >= space);
1310         atomic_add_64(&arc_size, -space);
1311 }
1312 
1313 void *
1314 arc_data_buf_alloc(uint64_t size)
1315 {
1316         if (arc_evict_needed(ARC_BUFC_DATA))
1317                 cv_signal(&arc_reclaim_thr_cv);
1318         atomic_add_64(&arc_size, size);
1319         return (zio_data_buf_alloc(size));
1320 }
1321 
1322 void
1323 arc_data_buf_free(void *buf, uint64_t size)
1324 {
1325         zio_data_buf_free(buf, size);
1326         ASSERT(arc_size >= size);
1327         atomic_add_64(&arc_size, -size);
1328 }
1329 
1330 arc_buf_t *
1331 arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1332 {
1333         arc_buf_hdr_t *hdr;
1334         arc_buf_t *buf;
1335 
1336         ASSERT3U(size, >, 0);
1337         hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
1338         ASSERT(BUF_EMPTY(hdr));
1339         hdr->b_size = size;
1340         hdr->b_type = type;
1341         hdr->b_spa = spa_load_guid(spa);
1342         hdr->b_state = arc_anon;
1343         hdr->b_arc_access = 0;
1344         buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1345         buf->b_hdr = hdr;
1346         buf->b_data = NULL;
1347         buf->b_efunc = NULL;
1348         buf->b_private = NULL;
1349         buf->b_next = NULL;
1350         hdr->b_buf = buf;
1351         arc_get_data_buf(buf);
1352         hdr->b_datacnt = 1;
1353         hdr->b_flags = 0;
1354         ASSERT(refcount_is_zero(&hdr->b_refcnt));
1355         (void) refcount_add(&hdr->b_refcnt, tag);
1356 
1357         return (buf);
1358 }
1359 
1360 static char *arc_onloan_tag = "onloan";
1361 
1362 /*
1363  * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1364  * flight data by arc_tempreserve_space() until they are "returned". Loaned
1365  * buffers must be returned to the arc before they can be used by the DMU or
1366  * freed.
1367  */
1368 arc_buf_t *
1369 arc_loan_buf(spa_t *spa, int size)
1370 {
1371         arc_buf_t *buf;
1372 
1373         buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
1374 
1375         atomic_add_64(&arc_loaned_bytes, size);
1376         return (buf);
1377 }
1378 
1379 /*
1380  * Return a loaned arc buffer to the arc.
1381  */
1382 void
1383 arc_return_buf(arc_buf_t *buf, void *tag)
1384 {
1385         arc_buf_hdr_t *hdr = buf->b_hdr;
1386 
1387         ASSERT(buf->b_data != NULL);
1388         (void) refcount_add(&hdr->b_refcnt, tag);
1389         (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
1390 
1391         atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
1392 }
1393 
1394 /* Detach an arc_buf from a dbuf (tag) */
1395 void
1396 arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
1397 {
1398         arc_buf_hdr_t *hdr;
1399 
1400         ASSERT(buf->b_data != NULL);
1401         hdr = buf->b_hdr;
1402         (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
1403         (void) refcount_remove(&hdr->b_refcnt, tag);
1404         buf->b_efunc = NULL;
1405         buf->b_private = NULL;
1406 
1407         atomic_add_64(&arc_loaned_bytes, hdr->b_size);
1408 }
1409 
1410 static arc_buf_t *
1411 arc_buf_clone(arc_buf_t *from)
1412 {
1413         arc_buf_t *buf;
1414         arc_buf_hdr_t *hdr = from->b_hdr;
1415         uint64_t size = hdr->b_size;
1416 
1417         ASSERT(hdr->b_state != arc_anon);
1418 
1419         buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1420         buf->b_hdr = hdr;
1421         buf->b_data = NULL;
1422         buf->b_efunc = NULL;
1423         buf->b_private = NULL;
1424         buf->b_next = hdr->b_buf;
1425         hdr->b_buf = buf;
1426         arc_get_data_buf(buf);
1427         bcopy(from->b_data, buf->b_data, size);
1428 
1429         /*
1430          * This buffer already exists in the arc so create a duplicate
1431          * copy for the caller.  If the buffer is associated with user data
1432          * then track the size and number of duplicates.  These stats will be
1433          * updated as duplicate buffers are created and destroyed.
1434          */
1435         if (hdr->b_type == ARC_BUFC_DATA) {
1436                 ARCSTAT_BUMP(arcstat_duplicate_buffers);
1437                 ARCSTAT_INCR(arcstat_duplicate_buffers_size, size);
1438         }
1439         hdr->b_datacnt += 1;
1440         return (buf);
1441 }
1442 
1443 void
1444 arc_buf_add_ref(arc_buf_t *buf, void* tag)
1445 {
1446         arc_buf_hdr_t *hdr;
1447         kmutex_t *hash_lock;
1448 
1449         /*
1450          * Check to see if this buffer is evicted.  Callers
1451          * must verify b_data != NULL to know if the add_ref
1452          * was successful.
1453          */
1454         mutex_enter(&buf->b_evict_lock);
1455         if (buf->b_data == NULL) {
1456                 mutex_exit(&buf->b_evict_lock);
1457                 return;
1458         }
1459         hash_lock = HDR_LOCK(buf->b_hdr);
1460         mutex_enter(hash_lock);
1461         hdr = buf->b_hdr;
1462         ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1463         mutex_exit(&buf->b_evict_lock);
1464 
1465         ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1466         add_reference(hdr, hash_lock, tag);
1467         DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1468         arc_access(hdr, hash_lock);
1469         mutex_exit(hash_lock);
1470         ARCSTAT_BUMP(arcstat_hits);
1471         ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
1472             demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
1473             data, metadata, hits);
1474 }
1475 
1476 /*
1477  * Free the arc data buffer.  If it is an l2arc write in progress,
1478  * the buffer is placed on l2arc_free_on_write to be freed later.
1479  */
1480 static void
1481 arc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t))
1482 {
1483         arc_buf_hdr_t *hdr = buf->b_hdr;
1484 
1485         if (HDR_L2_WRITING(hdr)) {
1486                 l2arc_data_free_t *df;
1487                 df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP);
1488                 df->l2df_data = buf->b_data;
1489                 df->l2df_size = hdr->b_size;
1490                 df->l2df_func = free_func;
1491                 mutex_enter(&l2arc_free_on_write_mtx);
1492                 list_insert_head(l2arc_free_on_write, df);
1493                 mutex_exit(&l2arc_free_on_write_mtx);
1494                 ARCSTAT_BUMP(arcstat_l2_free_on_write);
1495         } else {
1496                 free_func(buf->b_data, hdr->b_size);
1497         }
1498 }
1499 
1500 /*
1501  * Free up buf->b_data and if 'remove' is set, then pull the
1502  * arc_buf_t off of the the arc_buf_hdr_t's list and free it.
1503  */
1504 static void
1505 arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t remove)
1506 {
1507         arc_buf_t **bufp;
1508 
1509         /* free up data associated with the buf */
1510         if (buf->b_data) {
1511                 arc_state_t *state = buf->b_hdr->b_state;
1512                 uint64_t size = buf->b_hdr->b_size;
1513                 arc_buf_contents_t type = buf->b_hdr->b_type;
1514 
1515                 arc_cksum_verify(buf);
1516                 arc_buf_unwatch(buf);
1517 
1518                 if (!recycle) {
1519                         if (type == ARC_BUFC_METADATA) {
1520                                 arc_buf_data_free(buf, zio_buf_free);
1521                                 arc_space_return(size, ARC_SPACE_DATA);
1522                         } else {
1523                                 ASSERT(type == ARC_BUFC_DATA);
1524                                 arc_buf_data_free(buf, zio_data_buf_free);
1525                                 ARCSTAT_INCR(arcstat_data_size, -size);
1526                                 atomic_add_64(&arc_size, -size);
1527                         }
1528                 }
1529                 if (list_link_active(&buf->b_hdr->b_arc_node)) {
1530                         uint64_t *cnt = &state->arcs_lsize[type];
1531 
1532                         ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
1533                         ASSERT(state != arc_anon);
1534 
1535                         ASSERT3U(*cnt, >=, size);
1536                         atomic_add_64(cnt, -size);
1537                 }
1538                 ASSERT3U(state->arcs_size, >=, size);
1539                 atomic_add_64(&state->arcs_size, -size);
1540                 buf->b_data = NULL;
1541 
1542                 /*
1543                  * If we're destroying a duplicate buffer make sure
1544                  * that the appropriate statistics are updated.
1545                  */
1546                 if (buf->b_hdr->b_datacnt > 1 &&
1547                     buf->b_hdr->b_type == ARC_BUFC_DATA) {
1548                         ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
1549                         ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size);
1550                 }
1551                 ASSERT(buf->b_hdr->b_datacnt > 0);
1552                 buf->b_hdr->b_datacnt -= 1;
1553         }
1554 
1555         /* only remove the buf if requested */
1556         if (!remove)
1557                 return;
1558 
1559         /* remove the buf from the hdr list */
1560         for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
1561                 continue;
1562         *bufp = buf->b_next;
1563         buf->b_next = NULL;
1564 
1565         ASSERT(buf->b_efunc == NULL);
1566 
1567         /* clean up the buf */
1568         buf->b_hdr = NULL;
1569         kmem_cache_free(buf_cache, buf);
1570 }
1571 
1572 static void
1573 arc_hdr_destroy(arc_buf_hdr_t *hdr)
1574 {
1575         ASSERT(refcount_is_zero(&hdr->b_refcnt));
1576         ASSERT3P(hdr->b_state, ==, arc_anon);
1577         ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1578         l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
1579 
1580         if (l2hdr != NULL) {
1581                 boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
1582                 /*
1583                  * To prevent arc_free() and l2arc_evict() from
1584                  * attempting to free the same buffer at the same time,
1585                  * a FREE_IN_PROGRESS flag is given to arc_free() to
1586                  * give it priority.  l2arc_evict() can't destroy this
1587                  * header while we are waiting on l2arc_buflist_mtx.
1588                  *
1589                  * The hdr may be removed from l2ad_buflist before we
1590                  * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1591                  */
1592                 if (!buflist_held) {
1593                         mutex_enter(&l2arc_buflist_mtx);
1594                         l2hdr = hdr->b_l2hdr;
1595                 }
1596 
1597                 if (l2hdr != NULL) {
1598                         list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
1599                         ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
1600                         ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize);
1601                         vdev_space_update(l2hdr->b_dev->l2ad_vdev,
1602                             -l2hdr->b_asize, 0, 0);
1603                         kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
1604                         if (hdr->b_state == arc_l2c_only)
1605                                 l2arc_hdr_stat_remove();
1606                         hdr->b_l2hdr = NULL;
1607                 }
1608 
1609                 if (!buflist_held)
1610                         mutex_exit(&l2arc_buflist_mtx);
1611         }
1612 
1613         if (!BUF_EMPTY(hdr)) {
1614                 ASSERT(!HDR_IN_HASH_TABLE(hdr));
1615                 buf_discard_identity(hdr);
1616         }
1617         while (hdr->b_buf) {
1618                 arc_buf_t *buf = hdr->b_buf;
1619 
1620                 if (buf->b_efunc) {
1621                         mutex_enter(&arc_eviction_mtx);
1622                         mutex_enter(&buf->b_evict_lock);
1623                         ASSERT(buf->b_hdr != NULL);
1624                         arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
1625                         hdr->b_buf = buf->b_next;
1626                         buf->b_hdr = &arc_eviction_hdr;
1627                         buf->b_next = arc_eviction_list;
1628                         arc_eviction_list = buf;
1629                         mutex_exit(&buf->b_evict_lock);
1630                         mutex_exit(&arc_eviction_mtx);
1631                 } else {
1632                         arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
1633                 }
1634         }
1635         if (hdr->b_freeze_cksum != NULL) {
1636                 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1637                 hdr->b_freeze_cksum = NULL;
1638         }
1639         if (hdr->b_thawed) {
1640                 kmem_free(hdr->b_thawed, 1);
1641                 hdr->b_thawed = NULL;
1642         }
1643 
1644         ASSERT(!list_link_active(&hdr->b_arc_node));
1645         ASSERT3P(hdr->b_hash_next, ==, NULL);
1646         ASSERT3P(hdr->b_acb, ==, NULL);
1647         kmem_cache_free(hdr_cache, hdr);
1648 }
1649 
1650 void
1651 arc_buf_free(arc_buf_t *buf, void *tag)
1652 {
1653         arc_buf_hdr_t *hdr = buf->b_hdr;
1654         int hashed = hdr->b_state != arc_anon;
1655 
1656         ASSERT(buf->b_efunc == NULL);
1657         ASSERT(buf->b_data != NULL);
1658 
1659         if (hashed) {
1660                 kmutex_t *hash_lock = HDR_LOCK(hdr);
1661 
1662                 mutex_enter(hash_lock);
1663                 hdr = buf->b_hdr;
1664                 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1665 
1666                 (void) remove_reference(hdr, hash_lock, tag);
1667                 if (hdr->b_datacnt > 1) {
1668                         arc_buf_destroy(buf, FALSE, TRUE);
1669                 } else {
1670                         ASSERT(buf == hdr->b_buf);
1671                         ASSERT(buf->b_efunc == NULL);
1672                         hdr->b_flags |= ARC_BUF_AVAILABLE;
1673                 }
1674                 mutex_exit(hash_lock);
1675         } else if (HDR_IO_IN_PROGRESS(hdr)) {
1676                 int destroy_hdr;
1677                 /*
1678                  * We are in the middle of an async write.  Don't destroy
1679                  * this buffer unless the write completes before we finish
1680                  * decrementing the reference count.
1681                  */
1682                 mutex_enter(&arc_eviction_mtx);
1683                 (void) remove_reference(hdr, NULL, tag);
1684                 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1685                 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
1686                 mutex_exit(&arc_eviction_mtx);
1687                 if (destroy_hdr)
1688                         arc_hdr_destroy(hdr);
1689         } else {
1690                 if (remove_reference(hdr, NULL, tag) > 0)
1691                         arc_buf_destroy(buf, FALSE, TRUE);
1692                 else
1693                         arc_hdr_destroy(hdr);
1694         }
1695 }
1696 
1697 boolean_t
1698 arc_buf_remove_ref(arc_buf_t *buf, void* tag)
1699 {
1700         arc_buf_hdr_t *hdr = buf->b_hdr;
1701         kmutex_t *hash_lock = HDR_LOCK(hdr);
1702         boolean_t no_callback = (buf->b_efunc == NULL);
1703 
1704         if (hdr->b_state == arc_anon) {
1705                 ASSERT(hdr->b_datacnt == 1);
1706                 arc_buf_free(buf, tag);
1707                 return (no_callback);
1708         }
1709 
1710         mutex_enter(hash_lock);
1711         hdr = buf->b_hdr;
1712         ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1713         ASSERT(hdr->b_state != arc_anon);
1714         ASSERT(buf->b_data != NULL);
1715 
1716         (void) remove_reference(hdr, hash_lock, tag);
1717         if (hdr->b_datacnt > 1) {
1718                 if (no_callback)
1719                         arc_buf_destroy(buf, FALSE, TRUE);
1720         } else if (no_callback) {
1721                 ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
1722                 ASSERT(buf->b_efunc == NULL);
1723                 hdr->b_flags |= ARC_BUF_AVAILABLE;
1724         }
1725         ASSERT(no_callback || hdr->b_datacnt > 1 ||
1726             refcount_is_zero(&hdr->b_refcnt));
1727         mutex_exit(hash_lock);
1728         return (no_callback);
1729 }
1730 
1731 int
1732 arc_buf_size(arc_buf_t *buf)
1733 {
1734         return (buf->b_hdr->b_size);
1735 }
1736 
1737 /*
1738  * Called from the DMU to determine if the current buffer should be
1739  * evicted. In order to ensure proper locking, the eviction must be initiated
1740  * from the DMU. Return true if the buffer is associated with user data and
1741  * duplicate buffers still exist.
1742  */
1743 boolean_t
1744 arc_buf_eviction_needed(arc_buf_t *buf)
1745 {
1746         arc_buf_hdr_t *hdr;
1747         boolean_t evict_needed = B_FALSE;
1748 
1749         if (zfs_disable_dup_eviction)
1750                 return (B_FALSE);
1751 
1752         mutex_enter(&buf->b_evict_lock);
1753         hdr = buf->b_hdr;
1754         if (hdr == NULL) {
1755                 /*
1756                  * We are in arc_do_user_evicts(); let that function
1757                  * perform the eviction.
1758                  */
1759                 ASSERT(buf->b_data == NULL);
1760                 mutex_exit(&buf->b_evict_lock);
1761                 return (B_FALSE);
1762         } else if (buf->b_data == NULL) {
1763                 /*
1764                  * We have already been added to the arc eviction list;
1765                  * recommend eviction.
1766                  */
1767                 ASSERT3P(hdr, ==, &arc_eviction_hdr);
1768                 mutex_exit(&buf->b_evict_lock);
1769                 return (B_TRUE);
1770         }
1771 
1772         if (hdr->b_datacnt > 1 && hdr->b_type == ARC_BUFC_DATA)
1773                 evict_needed = B_TRUE;
1774 
1775         mutex_exit(&buf->b_evict_lock);
1776         return (evict_needed);
1777 }
1778 
1779 /*
1780  * Evict buffers from list until we've removed the specified number of
1781  * bytes.  Move the removed buffers to the appropriate evict state.
1782  * If the recycle flag is set, then attempt to "recycle" a buffer:
1783  * - look for a buffer to evict that is `bytes' long.
1784  * - return the data block from this buffer rather than freeing it.
1785  * This flag is used by callers that are trying to make space for a
1786  * new buffer in a full arc cache.
1787  *
1788  * This function makes a "best effort".  It skips over any buffers
1789  * it can't get a hash_lock on, and so may not catch all candidates.
1790  * It may also return without evicting as much space as requested.
1791  */
1792 static void *
1793 arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
1794     arc_buf_contents_t type)
1795 {
1796         arc_state_t *evicted_state;
1797         uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
1798         arc_buf_hdr_t *ab, *ab_prev = NULL;
1799         list_t *list = &state->arcs_list[type];
1800         kmutex_t *hash_lock;
1801         boolean_t have_lock;
1802         void *stolen = NULL;
1803         arc_buf_hdr_t marker = { 0 };
1804         int count = 0;
1805 
1806         ASSERT(state == arc_mru || state == arc_mfu);
1807 
1808         evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1809 
1810         mutex_enter(&state->arcs_mtx);
1811         mutex_enter(&evicted_state->arcs_mtx);
1812 
1813         for (ab = list_tail(list); ab; ab = ab_prev) {
1814                 ab_prev = list_prev(list, ab);
1815                 /* prefetch buffers have a minimum lifespan */
1816                 if (HDR_IO_IN_PROGRESS(ab) ||
1817                     (spa && ab->b_spa != spa) ||
1818                     (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
1819                     ddi_get_lbolt() - ab->b_arc_access <
1820                     arc_min_prefetch_lifespan)) {
1821                         skipped++;
1822                         continue;
1823                 }
1824                 /* "lookahead" for better eviction candidate */
1825                 if (recycle && ab->b_size != bytes &&
1826                     ab_prev && ab_prev->b_size == bytes)
1827                         continue;
1828 
1829                 /* ignore markers */
1830                 if (ab->b_spa == 0)
1831                         continue;
1832 
1833                 /*
1834                  * It may take a long time to evict all the bufs requested.
1835                  * To avoid blocking all arc activity, periodically drop
1836                  * the arcs_mtx and give other threads a chance to run
1837                  * before reacquiring the lock.
1838                  *
1839                  * If we are looking for a buffer to recycle, we are in
1840                  * the hot code path, so don't sleep.
1841                  */
1842                 if (!recycle && count++ > arc_evict_iterations) {
1843                         list_insert_after(list, ab, &marker);
1844                         mutex_exit(&evicted_state->arcs_mtx);
1845                         mutex_exit(&state->arcs_mtx);
1846                         kpreempt(KPREEMPT_SYNC);
1847                         mutex_enter(&state->arcs_mtx);
1848                         mutex_enter(&evicted_state->arcs_mtx);
1849                         ab_prev = list_prev(list, &marker);
1850                         list_remove(list, &marker);
1851                         count = 0;
1852                         continue;
1853                 }
1854 
1855                 hash_lock = HDR_LOCK(ab);
1856                 have_lock = MUTEX_HELD(hash_lock);
1857                 if (have_lock || mutex_tryenter(hash_lock)) {
1858                         ASSERT0(refcount_count(&ab->b_refcnt));
1859                         ASSERT(ab->b_datacnt > 0);
1860                         while (ab->b_buf) {
1861                                 arc_buf_t *buf = ab->b_buf;
1862                                 if (!mutex_tryenter(&buf->b_evict_lock)) {
1863                                         missed += 1;
1864                                         break;
1865                                 }
1866                                 if (buf->b_data) {
1867                                         bytes_evicted += ab->b_size;
1868                                         if (recycle && ab->b_type == type &&
1869                                             ab->b_size == bytes &&
1870                                             !HDR_L2_WRITING(ab)) {
1871                                                 stolen = buf->b_data;
1872                                                 recycle = FALSE;
1873                                         }
1874                                 }
1875                                 if (buf->b_efunc) {
1876                                         mutex_enter(&arc_eviction_mtx);
1877                                         arc_buf_destroy(buf,
1878                                             buf->b_data == stolen, FALSE);
1879                                         ab->b_buf = buf->b_next;
1880                                         buf->b_hdr = &arc_eviction_hdr;
1881                                         buf->b_next = arc_eviction_list;
1882                                         arc_eviction_list = buf;
1883                                         mutex_exit(&arc_eviction_mtx);
1884                                         mutex_exit(&buf->b_evict_lock);
1885                                 } else {
1886                                         mutex_exit(&buf->b_evict_lock);
1887                                         arc_buf_destroy(buf,
1888                                             buf->b_data == stolen, TRUE);
1889                                 }
1890                         }
1891 
1892                         if (ab->b_l2hdr) {
1893                                 ARCSTAT_INCR(arcstat_evict_l2_cached,
1894                                     ab->b_size);
1895                         } else {
1896                                 if (l2arc_write_eligible(ab->b_spa, ab)) {
1897                                         ARCSTAT_INCR(arcstat_evict_l2_eligible,
1898                                             ab->b_size);
1899                                 } else {
1900                                         ARCSTAT_INCR(
1901                                             arcstat_evict_l2_ineligible,
1902                                             ab->b_size);
1903                                 }
1904                         }
1905 
1906                         if (ab->b_datacnt == 0) {
1907                                 arc_change_state(evicted_state, ab, hash_lock);
1908                                 ASSERT(HDR_IN_HASH_TABLE(ab));
1909                                 ab->b_flags |= ARC_IN_HASH_TABLE;
1910                                 ab->b_flags &= ~ARC_BUF_AVAILABLE;
1911                                 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
1912                         }
1913                         if (!have_lock)
1914                                 mutex_exit(hash_lock);
1915                         if (bytes >= 0 && bytes_evicted >= bytes)
1916                                 break;
1917                 } else {
1918                         missed += 1;
1919                 }
1920         }
1921 
1922         mutex_exit(&evicted_state->arcs_mtx);
1923         mutex_exit(&state->arcs_mtx);
1924 
1925         if (bytes_evicted < bytes)
1926                 dprintf("only evicted %lld bytes from %x",
1927                     (longlong_t)bytes_evicted, state);
1928 
1929         if (skipped)
1930                 ARCSTAT_INCR(arcstat_evict_skip, skipped);
1931 
1932         if (missed)
1933                 ARCSTAT_INCR(arcstat_mutex_miss, missed);
1934 
1935         /*
1936          * Note: we have just evicted some data into the ghost state,
1937          * potentially putting the ghost size over the desired size.  Rather
1938          * that evicting from the ghost list in this hot code path, leave
1939          * this chore to the arc_reclaim_thread().
1940          */
1941 
1942         return (stolen);
1943 }
1944 
1945 /*
1946  * Remove buffers from list until we've removed the specified number of
1947  * bytes.  Destroy the buffers that are removed.
1948  */
1949 static void
1950 arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes)
1951 {
1952         arc_buf_hdr_t *ab, *ab_prev;
1953         arc_buf_hdr_t marker = { 0 };
1954         list_t *list = &state->arcs_list[ARC_BUFC_DATA];
1955         kmutex_t *hash_lock;
1956         uint64_t bytes_deleted = 0;
1957         uint64_t bufs_skipped = 0;
1958         int count = 0;
1959 
1960         ASSERT(GHOST_STATE(state));
1961 top:
1962         mutex_enter(&state->arcs_mtx);
1963         for (ab = list_tail(list); ab; ab = ab_prev) {
1964                 ab_prev = list_prev(list, ab);
1965                 if (ab->b_type > ARC_BUFC_NUMTYPES)
1966                         panic("invalid ab=%p", (void *)ab);
1967                 if (spa && ab->b_spa != spa)
1968                         continue;
1969 
1970                 /* ignore markers */
1971                 if (ab->b_spa == 0)
1972                         continue;
1973 
1974                 hash_lock = HDR_LOCK(ab);
1975                 /* caller may be trying to modify this buffer, skip it */
1976                 if (MUTEX_HELD(hash_lock))
1977                         continue;
1978 
1979                 /*
1980                  * It may take a long time to evict all the bufs requested.
1981                  * To avoid blocking all arc activity, periodically drop
1982                  * the arcs_mtx and give other threads a chance to run
1983                  * before reacquiring the lock.
1984                  */
1985                 if (count++ > arc_evict_iterations) {
1986                         list_insert_after(list, ab, &marker);
1987                         mutex_exit(&state->arcs_mtx);
1988                         kpreempt(KPREEMPT_SYNC);
1989                         mutex_enter(&state->arcs_mtx);
1990                         ab_prev = list_prev(list, &marker);
1991                         list_remove(list, &marker);
1992                         count = 0;
1993                         continue;
1994                 }
1995                 if (mutex_tryenter(hash_lock)) {
1996                         ASSERT(!HDR_IO_IN_PROGRESS(ab));
1997                         ASSERT(ab->b_buf == NULL);
1998                         ARCSTAT_BUMP(arcstat_deleted);
1999                         bytes_deleted += ab->b_size;
2000 
2001                         if (ab->b_l2hdr != NULL) {
2002                                 /*
2003                                  * This buffer is cached on the 2nd Level ARC;
2004                                  * don't destroy the header.
2005                                  */
2006                                 arc_change_state(arc_l2c_only, ab, hash_lock);
2007                                 mutex_exit(hash_lock);
2008                         } else {
2009                                 arc_change_state(arc_anon, ab, hash_lock);
2010                                 mutex_exit(hash_lock);
2011                                 arc_hdr_destroy(ab);
2012                         }
2013 
2014                         DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
2015                         if (bytes >= 0 && bytes_deleted >= bytes)
2016                                 break;
2017                 } else if (bytes < 0) {
2018                         /*
2019                          * Insert a list marker and then wait for the
2020                          * hash lock to become available. Once its
2021                          * available, restart from where we left off.
2022                          */
2023                         list_insert_after(list, ab, &marker);
2024                         mutex_exit(&state->arcs_mtx);
2025                         mutex_enter(hash_lock);
2026                         mutex_exit(hash_lock);
2027                         mutex_enter(&state->arcs_mtx);
2028                         ab_prev = list_prev(list, &marker);
2029                         list_remove(list, &marker);
2030                 } else {
2031                         bufs_skipped += 1;
2032                 }
2033 
2034         }
2035         mutex_exit(&state->arcs_mtx);
2036 
2037         if (list == &state->arcs_list[ARC_BUFC_DATA] &&
2038             (bytes < 0 || bytes_deleted < bytes)) {
2039                 list = &state->arcs_list[ARC_BUFC_METADATA];
2040                 goto top;
2041         }
2042 
2043         if (bufs_skipped) {
2044                 ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
2045                 ASSERT(bytes >= 0);
2046         }
2047 
2048         if (bytes_deleted < bytes)
2049                 dprintf("only deleted %lld bytes from %p",
2050                     (longlong_t)bytes_deleted, state);
2051 }
2052 
2053 static void
2054 arc_adjust(void)
2055 {
2056         int64_t adjustment, delta;
2057 
2058         /*
2059          * Adjust MRU size
2060          */
2061 
2062         adjustment = MIN((int64_t)(arc_size - arc_c),
2063             (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
2064             arc_p));
2065 
2066         if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
2067                 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
2068                 (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA);
2069                 adjustment -= delta;
2070         }
2071 
2072         if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2073                 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2074                 (void) arc_evict(arc_mru, NULL, delta, FALSE,
2075                     ARC_BUFC_METADATA);
2076         }
2077 
2078         /*
2079          * Adjust MFU size
2080          */
2081 
2082         adjustment = arc_size - arc_c;
2083 
2084         if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
2085                 delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
2086                 (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA);
2087                 adjustment -= delta;
2088         }
2089 
2090         if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2091                 int64_t delta = MIN(adjustment,
2092                     arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
2093                 (void) arc_evict(arc_mfu, NULL, delta, FALSE,
2094                     ARC_BUFC_METADATA);
2095         }
2096 
2097         /*
2098          * Adjust ghost lists
2099          */
2100 
2101         adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
2102 
2103         if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
2104                 delta = MIN(arc_mru_ghost->arcs_size, adjustment);
2105                 arc_evict_ghost(arc_mru_ghost, NULL, delta);
2106         }
2107 
2108         adjustment =
2109             arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
2110 
2111         if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
2112                 delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
2113                 arc_evict_ghost(arc_mfu_ghost, NULL, delta);
2114         }
2115 }
2116 
2117 static void
2118 arc_do_user_evicts(void)
2119 {
2120         mutex_enter(&arc_eviction_mtx);
2121         while (arc_eviction_list != NULL) {
2122                 arc_buf_t *buf = arc_eviction_list;
2123                 arc_eviction_list = buf->b_next;
2124                 mutex_enter(&buf->b_evict_lock);
2125                 buf->b_hdr = NULL;
2126                 mutex_exit(&buf->b_evict_lock);
2127                 mutex_exit(&arc_eviction_mtx);
2128 
2129                 if (buf->b_efunc != NULL)
2130                         VERIFY0(buf->b_efunc(buf->b_private));
2131 
2132                 buf->b_efunc = NULL;
2133                 buf->b_private = NULL;
2134                 kmem_cache_free(buf_cache, buf);
2135                 mutex_enter(&arc_eviction_mtx);
2136         }
2137         mutex_exit(&arc_eviction_mtx);
2138 }
2139 
2140 /*
2141  * Flush all *evictable* data from the cache for the given spa.
2142  * NOTE: this will not touch "active" (i.e. referenced) data.
2143  */
2144 void
2145 arc_flush(spa_t *spa)
2146 {
2147         uint64_t guid = 0;
2148 
2149         if (spa)
2150                 guid = spa_load_guid(spa);
2151 
2152         while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
2153                 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
2154                 if (spa)
2155                         break;
2156         }
2157         while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
2158                 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
2159                 if (spa)
2160                         break;
2161         }
2162         while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
2163                 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
2164                 if (spa)
2165                         break;
2166         }
2167         while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
2168                 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
2169                 if (spa)
2170                         break;
2171         }
2172 
2173         arc_evict_ghost(arc_mru_ghost, guid, -1);
2174         arc_evict_ghost(arc_mfu_ghost, guid, -1);
2175 
2176         mutex_enter(&arc_reclaim_thr_lock);
2177         arc_do_user_evicts();
2178         mutex_exit(&arc_reclaim_thr_lock);
2179         ASSERT(spa || arc_eviction_list == NULL);
2180 }
2181 
2182 void
2183 arc_shrink(void)
2184 {
2185         if (arc_c > arc_c_min) {
2186                 uint64_t to_free;
2187 
2188 #ifdef _KERNEL
2189                 to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree));
2190 #else
2191                 to_free = arc_c >> arc_shrink_shift;
2192 #endif
2193                 if (arc_c > arc_c_min + to_free)
2194                         atomic_add_64(&arc_c, -to_free);
2195                 else
2196                         arc_c = arc_c_min;
2197 
2198                 atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
2199                 if (arc_c > arc_size)
2200                         arc_c = MAX(arc_size, arc_c_min);
2201                 if (arc_p > arc_c)
2202                         arc_p = (arc_c >> 1);
2203                 ASSERT(arc_c >= arc_c_min);
2204                 ASSERT((int64_t)arc_p >= 0);
2205         }
2206 
2207         if (arc_size > arc_c)
2208                 arc_adjust();
2209 }
2210 
2211 /*
2212  * Determine if the system is under memory pressure and is asking
2213  * to reclaim memory. A return value of 1 indicates that the system
2214  * is under memory pressure and that the arc should adjust accordingly.
2215  */
2216 static int
2217 arc_reclaim_needed(void)
2218 {
2219         uint64_t extra;
2220 
2221 #ifdef _KERNEL
2222 
2223         if (needfree)
2224                 return (1);
2225 
2226         /*
2227          * take 'desfree' extra pages, so we reclaim sooner, rather than later
2228          */
2229         extra = desfree;
2230 
2231         /*
2232          * check that we're out of range of the pageout scanner.  It starts to
2233          * schedule paging if freemem is less than lotsfree and needfree.
2234          * lotsfree is the high-water mark for pageout, and needfree is the
2235          * number of needed free pages.  We add extra pages here to make sure
2236          * the scanner doesn't start up while we're freeing memory.
2237          */
2238         if (freemem < lotsfree + needfree + extra)
2239                 return (1);
2240 
2241         /*
2242          * check to make sure that swapfs has enough space so that anon
2243          * reservations can still succeed. anon_resvmem() checks that the
2244          * availrmem is greater than swapfs_minfree, and the number of reserved
2245          * swap pages.  We also add a bit of extra here just to prevent
2246          * circumstances from getting really dire.
2247          */
2248         if (availrmem < swapfs_minfree + swapfs_reserve + extra)
2249                 return (1);
2250 
2251         /*
2252          * Check that we have enough availrmem that memory locking (e.g., via
2253          * mlock(3C) or memcntl(2)) can still succeed.  (pages_pp_maximum
2254          * stores the number of pages that cannot be locked; when availrmem
2255          * drops below pages_pp_maximum, page locking mechanisms such as
2256          * page_pp_lock() will fail.)
2257          */
2258         if (availrmem <= pages_pp_maximum)
2259                 return (1);
2260 
2261 #if defined(__i386)
2262         /*
2263          * If we're on an i386 platform, it's possible that we'll exhaust the
2264          * kernel heap space before we ever run out of available physical
2265          * memory.  Most checks of the size of the heap_area compare against
2266          * tune.t_minarmem, which is the minimum available real memory that we
2267          * can have in the system.  However, this is generally fixed at 25 pages
2268          * which is so low that it's useless.  In this comparison, we seek to
2269          * calculate the total heap-size, and reclaim if more than 3/4ths of the
2270          * heap is allocated.  (Or, in the calculation, if less than 1/4th is
2271          * free)
2272          */
2273         if (vmem_size(heap_arena, VMEM_FREE) <
2274             (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2))
2275                 return (1);
2276 #endif
2277 
2278         /*
2279          * If zio data pages are being allocated out of a separate heap segment,
2280          * then enforce that the size of available vmem for this arena remains
2281          * above about 1/16th free.
2282          *
2283          * Note: The 1/16th arena free requirement was put in place
2284          * to aggressively evict memory from the arc in order to avoid
2285          * memory fragmentation issues.
2286          */
2287         if (zio_arena != NULL &&
2288             vmem_size(zio_arena, VMEM_FREE) <
2289             (vmem_size(zio_arena, VMEM_ALLOC) >> 4))
2290                 return (1);
2291 #else
2292         if (spa_get_random(100) == 0)
2293                 return (1);
2294 #endif
2295         return (0);
2296 }
2297 
2298 static void
2299 arc_kmem_reap_now(arc_reclaim_strategy_t strat)
2300 {
2301         size_t                  i;
2302         kmem_cache_t            *prev_cache = NULL;
2303         kmem_cache_t            *prev_data_cache = NULL;
2304         extern kmem_cache_t     *zio_buf_cache[];
2305         extern kmem_cache_t     *zio_data_buf_cache[];
2306         extern kmem_cache_t     *range_seg_cache;
2307 
2308 #ifdef _KERNEL
2309         if (arc_meta_used >= arc_meta_limit) {
2310                 /*
2311                  * We are exceeding our meta-data cache limit.
2312                  * Purge some DNLC entries to release holds on meta-data.
2313                  */
2314                 dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
2315         }
2316 #if defined(__i386)
2317         /*
2318          * Reclaim unused memory from all kmem caches.
2319          */
2320         kmem_reap();
2321 #endif
2322 #endif
2323 
2324         /*
2325          * An aggressive reclamation will shrink the cache size as well as
2326          * reap free buffers from the arc kmem caches.
2327          */
2328         if (strat == ARC_RECLAIM_AGGR)
2329                 arc_shrink();
2330 
2331         for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
2332                 if (zio_buf_cache[i] != prev_cache) {
2333                         prev_cache = zio_buf_cache[i];
2334                         kmem_cache_reap_now(zio_buf_cache[i]);
2335                 }
2336                 if (zio_data_buf_cache[i] != prev_data_cache) {
2337                         prev_data_cache = zio_data_buf_cache[i];
2338                         kmem_cache_reap_now(zio_data_buf_cache[i]);
2339                 }
2340         }
2341         kmem_cache_reap_now(buf_cache);
2342         kmem_cache_reap_now(hdr_cache);
2343         kmem_cache_reap_now(range_seg_cache);
2344 
2345         /*
2346          * Ask the vmem areana to reclaim unused memory from its
2347          * quantum caches.
2348          */
2349         if (zio_arena != NULL && strat == ARC_RECLAIM_AGGR)
2350                 vmem_qcache_reap(zio_arena);
2351 }
2352 
2353 static void
2354 arc_reclaim_thread(void)
2355 {
2356         clock_t                 growtime = 0;
2357         arc_reclaim_strategy_t  last_reclaim = ARC_RECLAIM_CONS;
2358         callb_cpr_t             cpr;
2359 
2360         CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
2361 
2362         mutex_enter(&arc_reclaim_thr_lock);
2363         while (arc_thread_exit == 0) {
2364                 if (arc_reclaim_needed()) {
2365 
2366                         if (arc_no_grow) {
2367                                 if (last_reclaim == ARC_RECLAIM_CONS) {
2368                                         last_reclaim = ARC_RECLAIM_AGGR;
2369                                 } else {
2370                                         last_reclaim = ARC_RECLAIM_CONS;
2371                                 }
2372                         } else {
2373                                 arc_no_grow = TRUE;
2374                                 last_reclaim = ARC_RECLAIM_AGGR;
2375                                 membar_producer();
2376                         }
2377 
2378                         /* reset the growth delay for every reclaim */
2379                         growtime = ddi_get_lbolt() + (arc_grow_retry * hz);
2380 
2381                         arc_kmem_reap_now(last_reclaim);
2382                         arc_warm = B_TRUE;
2383 
2384                 } else if (arc_no_grow && ddi_get_lbolt() >= growtime) {
2385                         arc_no_grow = FALSE;
2386                 }
2387 
2388                 arc_adjust();
2389 
2390                 if (arc_eviction_list != NULL)
2391                         arc_do_user_evicts();
2392 
2393                 /* block until needed, or one second, whichever is shorter */
2394                 CALLB_CPR_SAFE_BEGIN(&cpr);
2395                 (void) cv_timedwait(&arc_reclaim_thr_cv,
2396                     &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz));
2397                 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
2398         }
2399 
2400         arc_thread_exit = 0;
2401         cv_broadcast(&arc_reclaim_thr_cv);
2402         CALLB_CPR_EXIT(&cpr);               /* drops arc_reclaim_thr_lock */
2403         thread_exit();
2404 }
2405 
2406 /*
2407  * Adapt arc info given the number of bytes we are trying to add and
2408  * the state that we are comming from.  This function is only called
2409  * when we are adding new content to the cache.
2410  */
2411 static void
2412 arc_adapt(int bytes, arc_state_t *state)
2413 {
2414         int mult;
2415         uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
2416 
2417         if (state == arc_l2c_only)
2418                 return;
2419 
2420         ASSERT(bytes > 0);
2421         /*
2422          * Adapt the target size of the MRU list:
2423          *      - if we just hit in the MRU ghost list, then increase
2424          *        the target size of the MRU list.
2425          *      - if we just hit in the MFU ghost list, then increase
2426          *        the target size of the MFU list by decreasing the
2427          *        target size of the MRU list.
2428          */
2429         if (state == arc_mru_ghost) {
2430                 mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
2431                     1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
2432                 mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
2433 
2434                 arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
2435         } else if (state == arc_mfu_ghost) {
2436                 uint64_t delta;
2437 
2438                 mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
2439                     1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
2440                 mult = MIN(mult, 10);
2441 
2442                 delta = MIN(bytes * mult, arc_p);
2443                 arc_p = MAX(arc_p_min, arc_p - delta);
2444         }
2445         ASSERT((int64_t)arc_p >= 0);
2446 
2447         if (arc_reclaim_needed()) {
2448                 cv_signal(&arc_reclaim_thr_cv);
2449                 return;
2450         }
2451 
2452         if (arc_no_grow)
2453                 return;
2454 
2455         if (arc_c >= arc_c_max)
2456                 return;
2457 
2458         /*
2459          * If we're within (2 * maxblocksize) bytes of the target
2460          * cache size, increment the target cache size
2461          */
2462         if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
2463                 atomic_add_64(&arc_c, (int64_t)bytes);
2464                 if (arc_c > arc_c_max)
2465                         arc_c = arc_c_max;
2466                 else if (state == arc_anon)
2467                         atomic_add_64(&arc_p, (int64_t)bytes);
2468                 if (arc_p > arc_c)
2469                         arc_p = arc_c;
2470         }
2471         ASSERT((int64_t)arc_p >= 0);
2472 }
2473 
2474 /*
2475  * Check if the cache has reached its limits and eviction is required
2476  * prior to insert.
2477  */
2478 static int
2479 arc_evict_needed(arc_buf_contents_t type)
2480 {
2481         if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
2482                 return (1);
2483 
2484         if (arc_reclaim_needed())
2485                 return (1);
2486 
2487         return (arc_size > arc_c);
2488 }
2489 
2490 /*
2491  * The buffer, supplied as the first argument, needs a data block.
2492  * So, if we are at cache max, determine which cache should be victimized.
2493  * We have the following cases:
2494  *
2495  * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2496  * In this situation if we're out of space, but the resident size of the MFU is
2497  * under the limit, victimize the MFU cache to satisfy this insertion request.
2498  *
2499  * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2500  * Here, we've used up all of the available space for the MRU, so we need to
2501  * evict from our own cache instead.  Evict from the set of resident MRU
2502  * entries.
2503  *
2504  * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2505  * c minus p represents the MFU space in the cache, since p is the size of the
2506  * cache that is dedicated to the MRU.  In this situation there's still space on
2507  * the MFU side, so the MRU side needs to be victimized.
2508  *
2509  * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2510  * MFU's resident set is consuming more space than it has been allotted.  In
2511  * this situation, we must victimize our own cache, the MFU, for this insertion.
2512  */
2513 static void
2514 arc_get_data_buf(arc_buf_t *buf)
2515 {
2516         arc_state_t             *state = buf->b_hdr->b_state;
2517         uint64_t                size = buf->b_hdr->b_size;
2518         arc_buf_contents_t      type = buf->b_hdr->b_type;
2519 
2520         arc_adapt(size, state);
2521 
2522 top:
2523         /*
2524          * We have not yet reached cache maximum size,
2525          * just allocate a new buffer.
2526          */
2527         if (!arc_evict_needed(type)) {
2528                 if (type == ARC_BUFC_METADATA) {
2529                         buf->b_data = zio_buf_alloc_canfail(size);
2530                         if (buf->b_data != NULL) {
2531                                 arc_space_consume(size, ARC_SPACE_DATA);
2532                                 goto out;
2533                         }
2534                 } else {
2535                         ASSERT(type == ARC_BUFC_DATA);
2536                         buf->b_data = zio_data_buf_alloc_canfail(size);
2537                         if (buf->b_data != NULL) {
2538                                 ARCSTAT_INCR(arcstat_data_size, size);
2539                                 atomic_add_64(&arc_size, size);
2540                                 goto out;
2541                         }
2542                 }
2543                 /*
2544                  * Memory allocation failed probably due to excessive
2545                  * fragmentation, we need to evict regardless.
2546                  */
2547         }
2548 
2549         /*
2550          * If we are prefetching from the mfu ghost list, this buffer
2551          * will end up on the mru list; so steal space from there.
2552          */
2553         if (state == arc_mfu_ghost)
2554                 state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
2555         else if (state == arc_mru_ghost)
2556                 state = arc_mru;
2557 
2558         if (state == arc_mru || state == arc_anon) {
2559                 uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
2560                 state = (arc_mfu->arcs_lsize[type] >= size &&
2561                     arc_p > mru_used) ? arc_mfu : arc_mru;
2562         } else {
2563                 /* MFU cases */
2564                 uint64_t mfu_space = arc_c - arc_p;
2565                 state =  (arc_mru->arcs_lsize[type] >= size &&
2566                     mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
2567         }
2568         if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) {
2569                 ARCSTAT_BUMP(arcstat_recycle_miss);
2570                 goto top;
2571         }
2572         ASSERT(buf->b_data != NULL);
2573 out:
2574         /*
2575          * Update the state size.  Note that ghost states have a
2576          * "ghost size" and so don't need to be updated.
2577          */
2578         if (!GHOST_STATE(buf->b_hdr->b_state)) {
2579                 arc_buf_hdr_t *hdr = buf->b_hdr;
2580 
2581                 atomic_add_64(&hdr->b_state->arcs_size, size);
2582                 if (list_link_active(&hdr->b_arc_node)) {
2583                         ASSERT(refcount_is_zero(&hdr->b_refcnt));
2584                         atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2585                 }
2586                 /*
2587                  * If we are growing the cache, and we are adding anonymous
2588                  * data, and we have outgrown arc_p, update arc_p
2589                  */
2590                 if (arc_size < arc_c && hdr->b_state == arc_anon &&
2591                     arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
2592                         arc_p = MIN(arc_c, arc_p + size);
2593         }
2594 }
2595 
2596 /*
2597  * This routine is called whenever a buffer is accessed.
2598  * NOTE: the hash lock is dropped in this function.
2599  */
2600 static void
2601 arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2602 {
2603         clock_t now;
2604 
2605         ASSERT(MUTEX_HELD(hash_lock));
2606 
2607         if (buf->b_state == arc_anon) {
2608                 /*
2609                  * This buffer is not in the cache, and does not
2610                  * appear in our "ghost" list.  Add the new buffer
2611                  * to the MRU state.
2612                  */
2613 
2614                 ASSERT(buf->b_arc_access == 0);
2615                 buf->b_arc_access = ddi_get_lbolt();
2616                 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2617                 arc_change_state(arc_mru, buf, hash_lock);
2618 
2619         } else if (buf->b_state == arc_mru) {
2620                 now = ddi_get_lbolt();
2621 
2622                 /*
2623                  * If this buffer is here because of a prefetch, then either:
2624                  * - clear the flag if this is a "referencing" read
2625                  *   (any subsequent access will bump this into the MFU state).
2626                  * or
2627                  * - move the buffer to the head of the list if this is
2628                  *   another prefetch (to make it less likely to be evicted).
2629                  */
2630                 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2631                         if (refcount_count(&buf->b_refcnt) == 0) {
2632                                 ASSERT(list_link_active(&buf->b_arc_node));
2633                         } else {
2634                                 buf->b_flags &= ~ARC_PREFETCH;
2635                                 ARCSTAT_BUMP(arcstat_mru_hits);
2636                         }
2637                         buf->b_arc_access = now;
2638                         return;
2639                 }
2640 
2641                 /*
2642                  * This buffer has been "accessed" only once so far,
2643                  * but it is still in the cache. Move it to the MFU
2644                  * state.
2645                  */
2646                 if (now > buf->b_arc_access + ARC_MINTIME) {
2647                         /*
2648                          * More than 125ms have passed since we
2649                          * instantiated this buffer.  Move it to the
2650                          * most frequently used state.
2651                          */
2652                         buf->b_arc_access = now;
2653                         DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2654                         arc_change_state(arc_mfu, buf, hash_lock);
2655                 }
2656                 ARCSTAT_BUMP(arcstat_mru_hits);
2657         } else if (buf->b_state == arc_mru_ghost) {
2658                 arc_state_t     *new_state;
2659                 /*
2660                  * This buffer has been "accessed" recently, but
2661                  * was evicted from the cache.  Move it to the
2662                  * MFU state.
2663                  */
2664 
2665                 if (buf->b_flags & ARC_PREFETCH) {
2666                         new_state = arc_mru;
2667                         if (refcount_count(&buf->b_refcnt) > 0)
2668                                 buf->b_flags &= ~ARC_PREFETCH;
2669                         DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2670                 } else {
2671                         new_state = arc_mfu;
2672                         DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2673                 }
2674 
2675                 buf->b_arc_access = ddi_get_lbolt();
2676                 arc_change_state(new_state, buf, hash_lock);
2677 
2678                 ARCSTAT_BUMP(arcstat_mru_ghost_hits);
2679         } else if (buf->b_state == arc_mfu) {
2680                 /*
2681                  * This buffer has been accessed more than once and is
2682                  * still in the cache.  Keep it in the MFU state.
2683                  *
2684                  * NOTE: an add_reference() that occurred when we did
2685                  * the arc_read() will have kicked this off the list.
2686                  * If it was a prefetch, we will explicitly move it to
2687                  * the head of the list now.
2688                  */
2689                 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2690                         ASSERT(refcount_count(&buf->b_refcnt) == 0);
2691                         ASSERT(list_link_active(&buf->b_arc_node));
2692                 }
2693                 ARCSTAT_BUMP(arcstat_mfu_hits);
2694                 buf->b_arc_access = ddi_get_lbolt();
2695         } else if (buf->b_state == arc_mfu_ghost) {
2696                 arc_state_t     *new_state = arc_mfu;
2697                 /*
2698                  * This buffer has been accessed more than once but has
2699                  * been evicted from the cache.  Move it back to the
2700                  * MFU state.
2701                  */
2702 
2703                 if (buf->b_flags & ARC_PREFETCH) {
2704                         /*
2705                          * This is a prefetch access...
2706                          * move this block back to the MRU state.
2707                          */
2708                         ASSERT0(refcount_count(&buf->b_refcnt));
2709                         new_state = arc_mru;
2710                 }
2711 
2712                 buf->b_arc_access = ddi_get_lbolt();
2713                 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2714                 arc_change_state(new_state, buf, hash_lock);
2715 
2716                 ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
2717         } else if (buf->b_state == arc_l2c_only) {
2718                 /*
2719                  * This buffer is on the 2nd Level ARC.
2720                  */
2721 
2722                 buf->b_arc_access = ddi_get_lbolt();
2723                 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2724                 arc_change_state(arc_mfu, buf, hash_lock);
2725         } else {
2726                 ASSERT(!"invalid arc state");
2727         }
2728 }
2729 
2730 /* a generic arc_done_func_t which you can use */
2731 /* ARGSUSED */
2732 void
2733 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2734 {
2735         if (zio == NULL || zio->io_error == 0)
2736                 bcopy(buf->b_data, arg, buf->b_hdr->b_size);
2737         VERIFY(arc_buf_remove_ref(buf, arg));
2738 }
2739 
2740 /* a generic arc_done_func_t */
2741 void
2742 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2743 {
2744         arc_buf_t **bufp = arg;
2745         if (zio && zio->io_error) {
2746                 VERIFY(arc_buf_remove_ref(buf, arg));
2747                 *bufp = NULL;
2748         } else {
2749                 *bufp = buf;
2750                 ASSERT(buf->b_data);
2751         }
2752 }
2753 
2754 static void
2755 arc_read_done(zio_t *zio)
2756 {
2757         arc_buf_hdr_t   *hdr;
2758         arc_buf_t       *buf;
2759         arc_buf_t       *abuf;  /* buffer we're assigning to callback */
2760         kmutex_t        *hash_lock = NULL;
2761         arc_callback_t  *callback_list, *acb;
2762         int             freeable = FALSE;
2763 
2764         buf = zio->io_private;
2765         hdr = buf->b_hdr;
2766 
2767         /*
2768          * The hdr was inserted into hash-table and removed from lists
2769          * prior to starting I/O.  We should find this header, since
2770          * it's in the hash table, and it should be legit since it's
2771          * not possible to evict it during the I/O.  The only possible
2772          * reason for it not to be found is if we were freed during the
2773          * read.
2774          */
2775         if (HDR_IN_HASH_TABLE(hdr)) {
2776                 ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp));
2777                 ASSERT3U(hdr->b_dva.dva_word[0], ==,
2778                     BP_IDENTITY(zio->io_bp)->dva_word[0]);
2779                 ASSERT3U(hdr->b_dva.dva_word[1], ==,
2780                     BP_IDENTITY(zio->io_bp)->dva_word[1]);
2781 
2782                 arc_buf_hdr_t *found = buf_hash_find(hdr->b_spa, zio->io_bp,
2783                     &hash_lock);
2784 
2785                 ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) &&
2786                     hash_lock == NULL) ||
2787                     (found == hdr &&
2788                     DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
2789                     (found == hdr && HDR_L2_READING(hdr)));
2790         }
2791 
2792         hdr->b_flags &= ~ARC_L2_EVICTED;
2793         if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
2794                 hdr->b_flags &= ~ARC_L2CACHE;
2795 
2796         /* byteswap if necessary */
2797         callback_list = hdr->b_acb;
2798         ASSERT(callback_list != NULL);
2799         if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
2800                 dmu_object_byteswap_t bswap =
2801                     DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp));
2802                 arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ?
2803                     byteswap_uint64_array :
2804                     dmu_ot_byteswap[bswap].ob_func;
2805                 func(buf->b_data, hdr->b_size);
2806         }
2807 
2808         arc_cksum_compute(buf, B_FALSE);
2809         arc_buf_watch(buf);
2810 
2811         if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
2812                 /*
2813                  * Only call arc_access on anonymous buffers.  This is because
2814                  * if we've issued an I/O for an evicted buffer, we've already
2815                  * called arc_access (to prevent any simultaneous readers from
2816                  * getting confused).
2817                  */
2818                 arc_access(hdr, hash_lock);
2819         }
2820 
2821         /* create copies of the data buffer for the callers */
2822         abuf = buf;
2823         for (acb = callback_list; acb; acb = acb->acb_next) {
2824                 if (acb->acb_done) {
2825                         if (abuf == NULL) {
2826                                 ARCSTAT_BUMP(arcstat_duplicate_reads);
2827                                 abuf = arc_buf_clone(buf);
2828                         }
2829                         acb->acb_buf = abuf;
2830                         abuf = NULL;
2831                 }
2832         }
2833         hdr->b_acb = NULL;
2834         hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2835         ASSERT(!HDR_BUF_AVAILABLE(hdr));
2836         if (abuf == buf) {
2837                 ASSERT(buf->b_efunc == NULL);
2838                 ASSERT(hdr->b_datacnt == 1);
2839                 hdr->b_flags |= ARC_BUF_AVAILABLE;
2840         }
2841 
2842         ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
2843 
2844         if (zio->io_error != 0) {
2845                 hdr->b_flags |= ARC_IO_ERROR;
2846                 if (hdr->b_state != arc_anon)
2847                         arc_change_state(arc_anon, hdr, hash_lock);
2848                 if (HDR_IN_HASH_TABLE(hdr))
2849                         buf_hash_remove(hdr);
2850                 freeable = refcount_is_zero(&hdr->b_refcnt);
2851         }
2852 
2853         /*
2854          * Broadcast before we drop the hash_lock to avoid the possibility
2855          * that the hdr (and hence the cv) might be freed before we get to
2856          * the cv_broadcast().
2857          */
2858         cv_broadcast(&hdr->b_cv);
2859 
2860         if (hash_lock) {
2861                 mutex_exit(hash_lock);
2862         } else {
2863                 /*
2864                  * This block was freed while we waited for the read to
2865                  * complete.  It has been removed from the hash table and
2866                  * moved to the anonymous state (so that it won't show up
2867                  * in the cache).
2868                  */
2869                 ASSERT3P(hdr->b_state, ==, arc_anon);
2870                 freeable = refcount_is_zero(&hdr->b_refcnt);
2871         }
2872 
2873         /* execute each callback and free its structure */
2874         while ((acb = callback_list) != NULL) {
2875                 if (acb->acb_done)
2876                         acb->acb_done(zio, acb->acb_buf, acb->acb_private);
2877 
2878                 if (acb->acb_zio_dummy != NULL) {
2879                         acb->acb_zio_dummy->io_error = zio->io_error;
2880                         zio_nowait(acb->acb_zio_dummy);
2881                 }
2882 
2883                 callback_list = acb->acb_next;
2884                 kmem_free(acb, sizeof (arc_callback_t));
2885         }
2886 
2887         if (freeable)
2888                 arc_hdr_destroy(hdr);
2889 }
2890 
2891 /*
2892  * "Read" the block at the specified DVA (in bp) via the
2893  * cache.  If the block is found in the cache, invoke the provided
2894  * callback immediately and return.  Note that the `zio' parameter
2895  * in the callback will be NULL in this case, since no IO was
2896  * required.  If the block is not in the cache pass the read request
2897  * on to the spa with a substitute callback function, so that the
2898  * requested block will be added to the cache.
2899  *
2900  * If a read request arrives for a block that has a read in-progress,
2901  * either wait for the in-progress read to complete (and return the
2902  * results); or, if this is a read with a "done" func, add a record
2903  * to the read to invoke the "done" func when the read completes,
2904  * and return; or just return.
2905  *
2906  * arc_read_done() will invoke all the requested "done" functions
2907  * for readers of this block.
2908  */
2909 int
2910 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done,
2911     void *private, zio_priority_t priority, int zio_flags, uint32_t *arc_flags,
2912     const zbookmark_phys_t *zb)
2913 {
2914         arc_buf_hdr_t *hdr = NULL;
2915         arc_buf_t *buf = NULL;
2916         kmutex_t *hash_lock = NULL;
2917         zio_t *rzio;
2918         uint64_t guid = spa_load_guid(spa);
2919 
2920         ASSERT(!BP_IS_EMBEDDED(bp) ||
2921             BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2922 
2923 top:
2924         if (!BP_IS_EMBEDDED(bp)) {
2925                 /*
2926                  * Embedded BP's have no DVA and require no I/O to "read".
2927                  * Create an anonymous arc buf to back it.
2928                  */
2929                 hdr = buf_hash_find(guid, bp, &hash_lock);
2930         }
2931 
2932         if (hdr != NULL && hdr->b_datacnt > 0) {
2933 
2934                 *arc_flags |= ARC_CACHED;
2935 
2936                 if (HDR_IO_IN_PROGRESS(hdr)) {
2937 
2938                         if (*arc_flags & ARC_WAIT) {
2939                                 cv_wait(&hdr->b_cv, hash_lock);
2940                                 mutex_exit(hash_lock);
2941                                 goto top;
2942                         }
2943                         ASSERT(*arc_flags & ARC_NOWAIT);
2944 
2945                         if (done) {
2946                                 arc_callback_t  *acb = NULL;
2947 
2948                                 acb = kmem_zalloc(sizeof (arc_callback_t),
2949                                     KM_SLEEP);
2950                                 acb->acb_done = done;
2951                                 acb->acb_private = private;
2952                                 if (pio != NULL)
2953                                         acb->acb_zio_dummy = zio_null(pio,
2954                                             spa, NULL, NULL, NULL, zio_flags);
2955 
2956                                 ASSERT(acb->acb_done != NULL);
2957                                 acb->acb_next = hdr->b_acb;
2958                                 hdr->b_acb = acb;
2959                                 add_reference(hdr, hash_lock, private);
2960                                 mutex_exit(hash_lock);
2961                                 return (0);
2962                         }
2963                         mutex_exit(hash_lock);
2964                         return (0);
2965                 }
2966 
2967                 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2968 
2969                 if (done) {
2970                         add_reference(hdr, hash_lock, private);
2971                         /*
2972                          * If this block is already in use, create a new
2973                          * copy of the data so that we will be guaranteed
2974                          * that arc_release() will always succeed.
2975                          */
2976                         buf = hdr->b_buf;
2977                         ASSERT(buf);
2978                         ASSERT(buf->b_data);
2979                         if (HDR_BUF_AVAILABLE(hdr)) {
2980                                 ASSERT(buf->b_efunc == NULL);
2981                                 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
2982                         } else {
2983                                 buf = arc_buf_clone(buf);
2984                         }
2985 
2986                 } else if (*arc_flags & ARC_PREFETCH &&
2987                     refcount_count(&hdr->b_refcnt) == 0) {
2988                         hdr->b_flags |= ARC_PREFETCH;
2989                 }
2990                 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
2991                 arc_access(hdr, hash_lock);
2992                 if (*arc_flags & ARC_L2CACHE)
2993                         hdr->b_flags |= ARC_L2CACHE;
2994                 if (*arc_flags & ARC_L2COMPRESS)
2995                         hdr->b_flags |= ARC_L2COMPRESS;
2996                 mutex_exit(hash_lock);
2997                 ARCSTAT_BUMP(arcstat_hits);
2998                 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
2999                     demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
3000                     data, metadata, hits);
3001 
3002                 if (done)
3003                         done(NULL, buf, private);
3004         } else {
3005                 uint64_t size = BP_GET_LSIZE(bp);
3006                 arc_callback_t *acb;
3007                 vdev_t *vd = NULL;
3008                 uint64_t addr = 0;
3009                 boolean_t devw = B_FALSE;
3010                 enum zio_compress b_compress = ZIO_COMPRESS_OFF;
3011                 uint64_t b_asize = 0;
3012 
3013                 if (hdr == NULL) {
3014                         /* this block is not in the cache */
3015                         arc_buf_hdr_t *exists = NULL;
3016                         arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
3017                         buf = arc_buf_alloc(spa, size, private, type);
3018                         hdr = buf->b_hdr;
3019                         if (!BP_IS_EMBEDDED(bp)) {
3020                                 hdr->b_dva = *BP_IDENTITY(bp);
3021                                 hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
3022                                 hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
3023                                 exists = buf_hash_insert(hdr, &hash_lock);
3024                         }
3025                         if (exists != NULL) {
3026                                 /* somebody beat us to the hash insert */
3027                                 mutex_exit(hash_lock);
3028                                 buf_discard_identity(hdr);
3029                                 (void) arc_buf_remove_ref(buf, private);
3030                                 goto top; /* restart the IO request */
3031                         }
3032                         /* if this is a prefetch, we don't have a reference */
3033                         if (*arc_flags & ARC_PREFETCH) {
3034                                 (void) remove_reference(hdr, hash_lock,
3035                                     private);
3036                                 hdr->b_flags |= ARC_PREFETCH;
3037                         }
3038                         if (*arc_flags & ARC_L2CACHE)
3039                                 hdr->b_flags |= ARC_L2CACHE;
3040                         if (*arc_flags & ARC_L2COMPRESS)
3041                                 hdr->b_flags |= ARC_L2COMPRESS;
3042                         if (BP_GET_LEVEL(bp) > 0)
3043                                 hdr->b_flags |= ARC_INDIRECT;
3044                 } else {
3045                         /* this block is in the ghost cache */
3046                         ASSERT(GHOST_STATE(hdr->b_state));
3047                         ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3048                         ASSERT0(refcount_count(&hdr->b_refcnt));
3049                         ASSERT(hdr->b_buf == NULL);
3050 
3051                         /* if this is a prefetch, we don't have a reference */
3052                         if (*arc_flags & ARC_PREFETCH)
3053                                 hdr->b_flags |= ARC_PREFETCH;
3054                         else
3055                                 add_reference(hdr, hash_lock, private);
3056                         if (*arc_flags & ARC_L2CACHE)
3057                                 hdr->b_flags |= ARC_L2CACHE;
3058                         if (*arc_flags & ARC_L2COMPRESS)
3059                                 hdr->b_flags |= ARC_L2COMPRESS;
3060                         buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
3061                         buf->b_hdr = hdr;
3062                         buf->b_data = NULL;
3063                         buf->b_efunc = NULL;
3064                         buf->b_private = NULL;
3065                         buf->b_next = NULL;
3066                         hdr->b_buf = buf;
3067                         ASSERT(hdr->b_datacnt == 0);
3068                         hdr->b_datacnt = 1;
3069                         arc_get_data_buf(buf);
3070                         arc_access(hdr, hash_lock);
3071                 }
3072 
3073                 ASSERT(!GHOST_STATE(hdr->b_state));
3074 
3075                 acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
3076                 acb->acb_done = done;
3077                 acb->acb_private = private;
3078 
3079                 ASSERT(hdr->b_acb == NULL);
3080                 hdr->b_acb = acb;
3081                 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3082 
3083                 if (hdr->b_l2hdr != NULL &&
3084                     (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
3085                         devw = hdr->b_l2hdr->b_dev->l2ad_writing;
3086                         addr = hdr->b_l2hdr->b_daddr;
3087                         b_compress = hdr->b_l2hdr->b_compress;
3088                         b_asize = hdr->b_l2hdr->b_asize;
3089                         /*
3090                          * Lock out device removal.
3091                          */
3092                         if (vdev_is_dead(vd) ||
3093                             !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
3094                                 vd = NULL;
3095                 }
3096 
3097                 if (hash_lock != NULL)
3098                         mutex_exit(hash_lock);
3099 
3100                 /*
3101                  * At this point, we have a level 1 cache miss.  Try again in
3102                  * L2ARC if possible.
3103                  */
3104                 ASSERT3U(hdr->b_size, ==, size);
3105                 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
3106                     uint64_t, size, zbookmark_phys_t *, zb);
3107                 ARCSTAT_BUMP(arcstat_misses);
3108                 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
3109                     demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
3110                     data, metadata, misses);
3111 
3112                 if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
3113                         /*
3114                          * Read from the L2ARC if the following are true:
3115                          * 1. The L2ARC vdev was previously cached.
3116                          * 2. This buffer still has L2ARC metadata.
3117                          * 3. This buffer isn't currently writing to the L2ARC.
3118                          * 4. The L2ARC entry wasn't evicted, which may
3119                          *    also have invalidated the vdev.
3120                          * 5. This isn't prefetch and l2arc_noprefetch is set.
3121                          */
3122                         if (hdr->b_l2hdr != NULL &&
3123                             !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
3124                             !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
3125                                 l2arc_read_callback_t *cb;
3126 
3127                                 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
3128                                 ARCSTAT_BUMP(arcstat_l2_hits);
3129 
3130                                 cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
3131                                     KM_SLEEP);
3132                                 cb->l2rcb_buf = buf;
3133                                 cb->l2rcb_spa = spa;
3134                                 cb->l2rcb_bp = *bp;
3135                                 cb->l2rcb_zb = *zb;
3136                                 cb->l2rcb_flags = zio_flags;
3137                                 cb->l2rcb_compress = b_compress;
3138 
3139                                 ASSERT(addr >= VDEV_LABEL_START_SIZE &&
3140                                     addr + size < vd->vdev_psize -
3141                                     VDEV_LABEL_END_SIZE);
3142 
3143                                 /*
3144                                  * l2arc read.  The SCL_L2ARC lock will be
3145                                  * released by l2arc_read_done().
3146                                  * Issue a null zio if the underlying buffer
3147                                  * was squashed to zero size by compression.
3148                                  */
3149                                 if (b_compress == ZIO_COMPRESS_EMPTY) {
3150                                         rzio = zio_null(pio, spa, vd,
3151                                             l2arc_read_done, cb,
3152                                             zio_flags | ZIO_FLAG_DONT_CACHE |
3153                                             ZIO_FLAG_CANFAIL |
3154                                             ZIO_FLAG_DONT_PROPAGATE |
3155                                             ZIO_FLAG_DONT_RETRY);
3156                                 } else {
3157                                         rzio = zio_read_phys(pio, vd, addr,
3158                                             b_asize, buf->b_data,
3159                                             ZIO_CHECKSUM_OFF,
3160                                             l2arc_read_done, cb, priority,
3161                                             zio_flags | ZIO_FLAG_DONT_CACHE |
3162                                             ZIO_FLAG_CANFAIL |
3163                                             ZIO_FLAG_DONT_PROPAGATE |
3164                                             ZIO_FLAG_DONT_RETRY, B_FALSE);
3165                                 }
3166                                 DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
3167                                     zio_t *, rzio);
3168                                 ARCSTAT_INCR(arcstat_l2_read_bytes, b_asize);
3169 
3170                                 if (*arc_flags & ARC_NOWAIT) {
3171                                         zio_nowait(rzio);
3172                                         return (0);
3173                                 }
3174 
3175                                 ASSERT(*arc_flags & ARC_WAIT);
3176                                 if (zio_wait(rzio) == 0)
3177                                         return (0);
3178 
3179                                 /* l2arc read error; goto zio_read() */
3180                         } else {
3181                                 DTRACE_PROBE1(l2arc__miss,
3182                                     arc_buf_hdr_t *, hdr);
3183                                 ARCSTAT_BUMP(arcstat_l2_misses);
3184                                 if (HDR_L2_WRITING(hdr))
3185                                         ARCSTAT_BUMP(arcstat_l2_rw_clash);
3186                                 spa_config_exit(spa, SCL_L2ARC, vd);
3187                         }
3188                 } else {
3189                         if (vd != NULL)
3190                                 spa_config_exit(spa, SCL_L2ARC, vd);
3191                         if (l2arc_ndev != 0) {
3192                                 DTRACE_PROBE1(l2arc__miss,
3193                                     arc_buf_hdr_t *, hdr);
3194                                 ARCSTAT_BUMP(arcstat_l2_misses);
3195                         }
3196                 }
3197 
3198                 rzio = zio_read(pio, spa, bp, buf->b_data, size,
3199                     arc_read_done, buf, priority, zio_flags, zb);
3200 
3201                 if (*arc_flags & ARC_WAIT)
3202                         return (zio_wait(rzio));
3203 
3204                 ASSERT(*arc_flags & ARC_NOWAIT);
3205                 zio_nowait(rzio);
3206         }
3207         return (0);
3208 }
3209 
3210 void
3211 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
3212 {
3213         ASSERT(buf->b_hdr != NULL);
3214         ASSERT(buf->b_hdr->b_state != arc_anon);
3215         ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
3216         ASSERT(buf->b_efunc == NULL);
3217         ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
3218 
3219         buf->b_efunc = func;
3220         buf->b_private = private;
3221 }
3222 
3223 /*
3224  * Notify the arc that a block was freed, and thus will never be used again.
3225  */
3226 void
3227 arc_freed(spa_t *spa, const blkptr_t *bp)
3228 {
3229         arc_buf_hdr_t *hdr;
3230         kmutex_t *hash_lock;
3231         uint64_t guid = spa_load_guid(spa);
3232 
3233         ASSERT(!BP_IS_EMBEDDED(bp));
3234 
3235         hdr = buf_hash_find(guid, bp, &hash_lock);
3236         if (hdr == NULL)
3237                 return;
3238         if (HDR_BUF_AVAILABLE(hdr)) {
3239                 arc_buf_t *buf = hdr->b_buf;
3240                 add_reference(hdr, hash_lock, FTAG);
3241                 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3242                 mutex_exit(hash_lock);
3243 
3244                 arc_release(buf, FTAG);
3245                 (void) arc_buf_remove_ref(buf, FTAG);
3246         } else {
3247                 mutex_exit(hash_lock);
3248         }
3249 
3250 }
3251 
3252 /*
3253  * Clear the user eviction callback set by arc_set_callback(), first calling
3254  * it if it exists.  Because the presence of a callback keeps an arc_buf cached
3255  * clearing the callback may result in the arc_buf being destroyed.  However,
3256  * it will not result in the *last* arc_buf being destroyed, hence the data
3257  * will remain cached in the ARC. We make a copy of the arc buffer here so
3258  * that we can process the callback without holding any locks.
3259  *
3260  * It's possible that the callback is already in the process of being cleared
3261  * by another thread.  In this case we can not clear the callback.
3262  *
3263  * Returns B_TRUE if the callback was successfully called and cleared.
3264  */
3265 boolean_t
3266 arc_clear_callback(arc_buf_t *buf)
3267 {
3268         arc_buf_hdr_t *hdr;
3269         kmutex_t *hash_lock;
3270         arc_evict_func_t *efunc = buf->b_efunc;
3271         void *private = buf->b_private;
3272 
3273         mutex_enter(&buf->b_evict_lock);
3274         hdr = buf->b_hdr;
3275         if (hdr == NULL) {
3276                 /*
3277                  * We are in arc_do_user_evicts().
3278                  */
3279                 ASSERT(buf->b_data == NULL);
3280                 mutex_exit(&buf->b_evict_lock);
3281                 return (B_FALSE);
3282         } else if (buf->b_data == NULL) {
3283                 /*
3284                  * We are on the eviction list; process this buffer now
3285                  * but let arc_do_user_evicts() do the reaping.
3286                  */
3287                 buf->b_efunc = NULL;
3288                 mutex_exit(&buf->b_evict_lock);
3289                 VERIFY0(efunc(private));
3290                 return (B_TRUE);
3291         }
3292         hash_lock = HDR_LOCK(hdr);
3293         mutex_enter(hash_lock);
3294         hdr = buf->b_hdr;
3295         ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3296 
3297         ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
3298         ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
3299 
3300         buf->b_efunc = NULL;
3301         buf->b_private = NULL;
3302 
3303         if (hdr->b_datacnt > 1) {
3304                 mutex_exit(&buf->b_evict_lock);
3305                 arc_buf_destroy(buf, FALSE, TRUE);
3306         } else {
3307                 ASSERT(buf == hdr->b_buf);
3308                 hdr->b_flags |= ARC_BUF_AVAILABLE;
3309                 mutex_exit(&buf->b_evict_lock);
3310         }
3311 
3312         mutex_exit(hash_lock);
3313         VERIFY0(efunc(private));
3314         return (B_TRUE);
3315 }
3316 
3317 /*
3318  * Release this buffer from the cache, making it an anonymous buffer.  This
3319  * must be done after a read and prior to modifying the buffer contents.
3320  * If the buffer has more than one reference, we must make
3321  * a new hdr for the buffer.
3322  */
3323 void
3324 arc_release(arc_buf_t *buf, void *tag)
3325 {
3326         arc_buf_hdr_t *hdr;
3327         kmutex_t *hash_lock = NULL;
3328         l2arc_buf_hdr_t *l2hdr;
3329         uint64_t buf_size;
3330 
3331         /*
3332          * It would be nice to assert that if it's DMU metadata (level >
3333          * 0 || it's the dnode file), then it must be syncing context.
3334          * But we don't know that information at this level.
3335          */
3336 
3337         mutex_enter(&buf->b_evict_lock);
3338         hdr = buf->b_hdr;
3339 
3340         /* this buffer is not on any list */
3341         ASSERT(refcount_count(&hdr->b_refcnt) > 0);
3342 
3343         if (hdr->b_state == arc_anon) {
3344                 /* this buffer is already released */
3345                 ASSERT(buf->b_efunc == NULL);
3346         } else {
3347                 hash_lock = HDR_LOCK(hdr);
3348                 mutex_enter(hash_lock);
3349                 hdr = buf->b_hdr;
3350                 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3351         }
3352 
3353         l2hdr = hdr->b_l2hdr;
3354         if (l2hdr) {
3355                 mutex_enter(&l2arc_buflist_mtx);
3356                 hdr->b_l2hdr = NULL;
3357                 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
3358         }
3359         buf_size = hdr->b_size;
3360 
3361         /*
3362          * Do we have more than one buf?
3363          */
3364         if (hdr->b_datacnt > 1) {
3365                 arc_buf_hdr_t *nhdr;
3366                 arc_buf_t **bufp;
3367                 uint64_t blksz = hdr->b_size;
3368                 uint64_t spa = hdr->b_spa;
3369                 arc_buf_contents_t type = hdr->b_type;
3370                 uint32_t flags = hdr->b_flags;
3371 
3372                 ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
3373                 /*
3374                  * Pull the data off of this hdr and attach it to
3375                  * a new anonymous hdr.
3376                  */
3377                 (void) remove_reference(hdr, hash_lock, tag);
3378                 bufp = &hdr->b_buf;
3379                 while (*bufp != buf)
3380                         bufp = &(*bufp)->b_next;
3381                 *bufp = buf->b_next;
3382                 buf->b_next = NULL;
3383 
3384                 ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
3385                 atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
3386                 if (refcount_is_zero(&hdr->b_refcnt)) {
3387                         uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
3388                         ASSERT3U(*size, >=, hdr->b_size);
3389                         atomic_add_64(size, -hdr->b_size);
3390                 }
3391 
3392                 /*
3393                  * We're releasing a duplicate user data buffer, update
3394                  * our statistics accordingly.
3395                  */
3396                 if (hdr->b_type == ARC_BUFC_DATA) {
3397                         ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
3398                         ARCSTAT_INCR(arcstat_duplicate_buffers_size,
3399                             -hdr->b_size);
3400                 }
3401                 hdr->b_datacnt -= 1;
3402                 arc_cksum_verify(buf);
3403                 arc_buf_unwatch(buf);
3404 
3405                 mutex_exit(hash_lock);
3406 
3407                 nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
3408                 nhdr->b_size = blksz;
3409                 nhdr->b_spa = spa;
3410                 nhdr->b_type = type;
3411                 nhdr->b_buf = buf;
3412                 nhdr->b_state = arc_anon;
3413                 nhdr->b_arc_access = 0;
3414                 nhdr->b_flags = flags & ARC_L2_WRITING;
3415                 nhdr->b_l2hdr = NULL;
3416                 nhdr->b_datacnt = 1;
3417                 nhdr->b_freeze_cksum = NULL;
3418                 (void) refcount_add(&nhdr->b_refcnt, tag);
3419                 buf->b_hdr = nhdr;
3420                 mutex_exit(&buf->b_evict_lock);
3421                 atomic_add_64(&arc_anon->arcs_size, blksz);
3422         } else {
3423                 mutex_exit(&buf->b_evict_lock);
3424                 ASSERT(refcount_count(&hdr->b_refcnt) == 1);
3425                 ASSERT(!list_link_active(&hdr->b_arc_node));
3426                 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3427                 if (hdr->b_state != arc_anon)
3428                         arc_change_state(arc_anon, hdr, hash_lock);
3429                 hdr->b_arc_access = 0;
3430                 if (hash_lock)
3431                         mutex_exit(hash_lock);
3432 
3433                 buf_discard_identity(hdr);
3434                 arc_buf_thaw(buf);
3435         }
3436         buf->b_efunc = NULL;
3437         buf->b_private = NULL;
3438 
3439         if (l2hdr) {
3440                 ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize);
3441                 vdev_space_update(l2hdr->b_dev->l2ad_vdev,
3442                     -l2hdr->b_asize, 0, 0);
3443                 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
3444                 ARCSTAT_INCR(arcstat_l2_size, -buf_size);
3445                 mutex_exit(&l2arc_buflist_mtx);
3446         }
3447 }
3448 
3449 int
3450 arc_released(arc_buf_t *buf)
3451 {
3452         int released;
3453 
3454         mutex_enter(&buf->b_evict_lock);
3455         released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
3456         mutex_exit(&buf->b_evict_lock);
3457         return (released);
3458 }
3459 
3460 #ifdef ZFS_DEBUG
3461 int
3462 arc_referenced(arc_buf_t *buf)
3463 {
3464         int referenced;
3465 
3466         mutex_enter(&buf->b_evict_lock);
3467         referenced = (refcount_count(&buf->b_hdr->b_refcnt));
3468         mutex_exit(&buf->b_evict_lock);
3469         return (referenced);
3470 }
3471 #endif
3472 
3473 static void
3474 arc_write_ready(zio_t *zio)
3475 {
3476         arc_write_callback_t *callback = zio->io_private;
3477         arc_buf_t *buf = callback->awcb_buf;
3478         arc_buf_hdr_t *hdr = buf->b_hdr;
3479 
3480         ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
3481         callback->awcb_ready(zio, buf, callback->awcb_private);
3482 
3483         /*
3484          * If the IO is already in progress, then this is a re-write
3485          * attempt, so we need to thaw and re-compute the cksum.
3486          * It is the responsibility of the callback to handle the
3487          * accounting for any re-write attempt.
3488          */
3489         if (HDR_IO_IN_PROGRESS(hdr)) {
3490                 mutex_enter(&hdr->b_freeze_lock);
3491                 if (hdr->b_freeze_cksum != NULL) {
3492                         kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
3493                         hdr->b_freeze_cksum = NULL;
3494                 }
3495                 mutex_exit(&hdr->b_freeze_lock);
3496         }
3497         arc_cksum_compute(buf, B_FALSE);
3498         hdr->b_flags |= ARC_IO_IN_PROGRESS;
3499 }
3500 
3501 /*
3502  * The SPA calls this callback for each physical write that happens on behalf
3503  * of a logical write.  See the comment in dbuf_write_physdone() for details.
3504  */
3505 static void
3506 arc_write_physdone(zio_t *zio)
3507 {
3508         arc_write_callback_t *cb = zio->io_private;
3509         if (cb->awcb_physdone != NULL)
3510                 cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private);
3511 }
3512 
3513 static void
3514 arc_write_done(zio_t *zio)
3515 {
3516         arc_write_callback_t *callback = zio->io_private;
3517         arc_buf_t *buf = callback->awcb_buf;
3518         arc_buf_hdr_t *hdr = buf->b_hdr;
3519 
3520         ASSERT(hdr->b_acb == NULL);
3521 
3522         if (zio->io_error == 0) {
3523                 if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) {
3524                         buf_discard_identity(hdr);
3525                 } else {
3526                         hdr->b_dva = *BP_IDENTITY(zio->io_bp);
3527                         hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
3528                         hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
3529                 }
3530         } else {
3531                 ASSERT(BUF_EMPTY(hdr));
3532         }
3533 
3534         /*
3535          * If the block to be written was all-zero or compressed enough to be
3536          * embedded in the BP, no write was performed so there will be no
3537          * dva/birth/checksum.  The buffer must therefore remain anonymous
3538          * (and uncached).
3539          */
3540         if (!BUF_EMPTY(hdr)) {
3541                 arc_buf_hdr_t *exists;
3542                 kmutex_t *hash_lock;
3543 
3544                 ASSERT(zio->io_error == 0);
3545 
3546                 arc_cksum_verify(buf);
3547 
3548                 exists = buf_hash_insert(hdr, &hash_lock);
3549                 if (exists) {
3550                         /*
3551                          * This can only happen if we overwrite for
3552                          * sync-to-convergence, because we remove
3553                          * buffers from the hash table when we arc_free().
3554                          */
3555                         if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
3556                                 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3557                                         panic("bad overwrite, hdr=%p exists=%p",
3558                                             (void *)hdr, (void *)exists);
3559                                 ASSERT(refcount_is_zero(&exists->b_refcnt));
3560                                 arc_change_state(arc_anon, exists, hash_lock);
3561                                 mutex_exit(hash_lock);
3562                                 arc_hdr_destroy(exists);
3563                                 exists = buf_hash_insert(hdr, &hash_lock);
3564                                 ASSERT3P(exists, ==, NULL);
3565                         } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
3566                                 /* nopwrite */
3567                                 ASSERT(zio->io_prop.zp_nopwrite);
3568                                 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3569                                         panic("bad nopwrite, hdr=%p exists=%p",
3570                                             (void *)hdr, (void *)exists);
3571                         } else {
3572                                 /* Dedup */
3573                                 ASSERT(hdr->b_datacnt == 1);
3574                                 ASSERT(hdr->b_state == arc_anon);
3575                                 ASSERT(BP_GET_DEDUP(zio->io_bp));
3576                                 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
3577                         }
3578                 }
3579                 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3580                 /* if it's not anon, we are doing a scrub */
3581                 if (!exists && hdr->b_state == arc_anon)
3582                         arc_access(hdr, hash_lock);
3583                 mutex_exit(hash_lock);
3584         } else {
3585                 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3586         }
3587 
3588         ASSERT(!refcount_is_zero(&hdr->b_refcnt));
3589         callback->awcb_done(zio, buf, callback->awcb_private);
3590 
3591         kmem_free(callback, sizeof (arc_write_callback_t));
3592 }
3593 
3594 zio_t *
3595 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
3596     blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress,
3597     const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone,
3598     arc_done_func_t *done, void *private, zio_priority_t priority,
3599     int zio_flags, const zbookmark_phys_t *zb)
3600 {
3601         arc_buf_hdr_t *hdr = buf->b_hdr;
3602         arc_write_callback_t *callback;
3603         zio_t *zio;
3604 
3605         ASSERT(ready != NULL);
3606         ASSERT(done != NULL);
3607         ASSERT(!HDR_IO_ERROR(hdr));
3608         ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
3609         ASSERT(hdr->b_acb == NULL);
3610         if (l2arc)
3611                 hdr->b_flags |= ARC_L2CACHE;
3612         if (l2arc_compress)
3613                 hdr->b_flags |= ARC_L2COMPRESS;
3614         callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
3615         callback->awcb_ready = ready;
3616         callback->awcb_physdone = physdone;
3617         callback->awcb_done = done;
3618         callback->awcb_private = private;
3619         callback->awcb_buf = buf;
3620 
3621         zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
3622             arc_write_ready, arc_write_physdone, arc_write_done, callback,
3623             priority, zio_flags, zb);
3624 
3625         return (zio);
3626 }
3627 
3628 static int
3629 arc_memory_throttle(uint64_t reserve, uint64_t txg)
3630 {
3631 #ifdef _KERNEL
3632         uint64_t available_memory = ptob(freemem);
3633         static uint64_t page_load = 0;
3634         static uint64_t last_txg = 0;
3635 
3636 #if defined(__i386)
3637         available_memory =
3638             MIN(available_memory, vmem_size(heap_arena, VMEM_FREE));
3639 #endif
3640 
3641         if (freemem > physmem * arc_lotsfree_percent / 100)
3642                 return (0);
3643 
3644         if (txg > last_txg) {
3645                 last_txg = txg;
3646                 page_load = 0;
3647         }
3648         /*
3649          * If we are in pageout, we know that memory is already tight,
3650          * the arc is already going to be evicting, so we just want to
3651          * continue to let page writes occur as quickly as possible.
3652          */
3653         if (curproc == proc_pageout) {
3654                 if (page_load > MAX(ptob(minfree), available_memory) / 4)
3655                         return (SET_ERROR(ERESTART));
3656                 /* Note: reserve is inflated, so we deflate */
3657                 page_load += reserve / 8;
3658                 return (0);
3659         } else if (page_load > 0 && arc_reclaim_needed()) {
3660                 /* memory is low, delay before restarting */
3661                 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3662                 return (SET_ERROR(EAGAIN));
3663         }
3664         page_load = 0;
3665 #endif
3666         return (0);
3667 }
3668 
3669 void
3670 arc_tempreserve_clear(uint64_t reserve)
3671 {
3672         atomic_add_64(&arc_tempreserve, -reserve);
3673         ASSERT((int64_t)arc_tempreserve >= 0);
3674 }
3675 
3676 int
3677 arc_tempreserve_space(uint64_t reserve, uint64_t txg)
3678 {
3679         int error;
3680         uint64_t anon_size;
3681 
3682         if (reserve > arc_c/4 && !arc_no_grow)
3683                 arc_c = MIN(arc_c_max, reserve * 4);
3684         if (reserve > arc_c)
3685                 return (SET_ERROR(ENOMEM));
3686 
3687         /*
3688          * Don't count loaned bufs as in flight dirty data to prevent long
3689          * network delays from blocking transactions that are ready to be
3690          * assigned to a txg.
3691          */
3692         anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
3693 
3694         /*
3695          * Writes will, almost always, require additional memory allocations
3696          * in order to compress/encrypt/etc the data.  We therefore need to
3697          * make sure that there is sufficient available memory for this.
3698          */
3699         error = arc_memory_throttle(reserve, txg);
3700         if (error != 0)
3701                 return (error);
3702 
3703         /*
3704          * Throttle writes when the amount of dirty data in the cache
3705          * gets too large.  We try to keep the cache less than half full
3706          * of dirty blocks so that our sync times don't grow too large.
3707          * Note: if two requests come in concurrently, we might let them
3708          * both succeed, when one of them should fail.  Not a huge deal.
3709          */
3710 
3711         if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
3712             anon_size > arc_c / 4) {
3713                 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3714                     "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3715                     arc_tempreserve>>10,
3716                     arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
3717                     arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
3718                     reserve>>10, arc_c>>10);
3719                 return (SET_ERROR(ERESTART));
3720         }
3721         atomic_add_64(&arc_tempreserve, reserve);
3722         return (0);
3723 }
3724 
3725 void
3726 arc_init(void)
3727 {
3728         mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3729         cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3730 
3731         /* Convert seconds to clock ticks */
3732         arc_min_prefetch_lifespan = 1 * hz;
3733 
3734         /* Start out with 1/8 of all memory */
3735         arc_c = physmem * PAGESIZE / 8;
3736 
3737 #ifdef _KERNEL
3738         /*
3739          * On architectures where the physical memory can be larger
3740          * than the addressable space (intel in 32-bit mode), we may
3741          * need to limit the cache to 1/8 of VM size.
3742          */
3743         arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
3744 #endif
3745 
3746         /* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3747         arc_c_min = MAX(arc_c / 4, 64<<20);
3748         /* set max to 3/4 of all memory, or all but 1GB, whichever is more */
3749         if (arc_c * 8 >= 1<<30)
3750                 arc_c_max = (arc_c * 8) - (1<<30);
3751         else
3752                 arc_c_max = arc_c_min;
3753         arc_c_max = MAX(arc_c * 6, arc_c_max);
3754 
3755         /*
3756          * Allow the tunables to override our calculations if they are
3757          * reasonable (ie. over 64MB)
3758          */
3759         if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
3760                 arc_c_max = zfs_arc_max;
3761         if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
3762                 arc_c_min = zfs_arc_min;
3763 
3764         arc_c = arc_c_max;
3765         arc_p = (arc_c >> 1);
3766 
3767         /* limit meta-data to 1/4 of the arc capacity */
3768         arc_meta_limit = arc_c_max / 4;
3769 
3770         /* Allow the tunable to override if it is reasonable */
3771         if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
3772                 arc_meta_limit = zfs_arc_meta_limit;
3773 
3774         if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
3775                 arc_c_min = arc_meta_limit / 2;
3776 
3777         if (zfs_arc_grow_retry > 0)
3778                 arc_grow_retry = zfs_arc_grow_retry;
3779 
3780         if (zfs_arc_shrink_shift > 0)
3781                 arc_shrink_shift = zfs_arc_shrink_shift;
3782 
3783         if (zfs_arc_p_min_shift > 0)
3784                 arc_p_min_shift = zfs_arc_p_min_shift;
3785 
3786         /* if kmem_flags are set, lets try to use less memory */
3787         if (kmem_debugging())
3788                 arc_c = arc_c / 2;
3789         if (arc_c < arc_c_min)
3790                 arc_c = arc_c_min;
3791 
3792         arc_anon = &ARC_anon;
3793         arc_mru = &ARC_mru;
3794         arc_mru_ghost = &ARC_mru_ghost;
3795         arc_mfu = &ARC_mfu;
3796         arc_mfu_ghost = &ARC_mfu_ghost;
3797         arc_l2c_only = &ARC_l2c_only;
3798         arc_size = 0;
3799 
3800         mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3801         mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3802         mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3803         mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3804         mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3805         mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3806 
3807         list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
3808             sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3809         list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
3810             sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3811         list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
3812             sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3813         list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
3814             sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3815         list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
3816             sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3817         list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
3818             sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3819         list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
3820             sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3821         list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
3822             sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3823         list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
3824             sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3825         list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
3826             sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3827 
3828         buf_init();
3829 
3830         arc_thread_exit = 0;
3831         arc_eviction_list = NULL;
3832         mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
3833         bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
3834 
3835         arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
3836             sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
3837 
3838         if (arc_ksp != NULL) {
3839                 arc_ksp->ks_data = &arc_stats;
3840                 kstat_install(arc_ksp);
3841         }
3842 
3843         (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
3844             TS_RUN, minclsyspri);
3845 
3846         arc_dead = FALSE;
3847         arc_warm = B_FALSE;
3848 
3849         /*
3850          * Calculate maximum amount of dirty data per pool.
3851          *
3852          * If it has been set by /etc/system, take that.
3853          * Otherwise, use a percentage of physical memory defined by
3854          * zfs_dirty_data_max_percent (default 10%) with a cap at
3855          * zfs_dirty_data_max_max (default 4GB).
3856          */
3857         if (zfs_dirty_data_max == 0) {
3858                 zfs_dirty_data_max = physmem * PAGESIZE *
3859                     zfs_dirty_data_max_percent / 100;
3860                 zfs_dirty_data_max = MIN(zfs_dirty_data_max,
3861                     zfs_dirty_data_max_max);
3862         }
3863 }
3864 
3865 void
3866 arc_fini(void)
3867 {
3868         mutex_enter(&arc_reclaim_thr_lock);
3869         arc_thread_exit = 1;
3870         while (arc_thread_exit != 0)
3871                 cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
3872         mutex_exit(&arc_reclaim_thr_lock);
3873 
3874         arc_flush(NULL);
3875 
3876         arc_dead = TRUE;
3877 
3878         if (arc_ksp != NULL) {
3879                 kstat_delete(arc_ksp);
3880                 arc_ksp = NULL;
3881         }
3882 
3883         mutex_destroy(&arc_eviction_mtx);
3884         mutex_destroy(&arc_reclaim_thr_lock);
3885         cv_destroy(&arc_reclaim_thr_cv);
3886 
3887         list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
3888         list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
3889         list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
3890         list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
3891         list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
3892         list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
3893         list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
3894         list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
3895 
3896         mutex_destroy(&arc_anon->arcs_mtx);
3897         mutex_destroy(&arc_mru->arcs_mtx);
3898         mutex_destroy(&arc_mru_ghost->arcs_mtx);
3899         mutex_destroy(&arc_mfu->arcs_mtx);
3900         mutex_destroy(&arc_mfu_ghost->arcs_mtx);
3901         mutex_destroy(&arc_l2c_only->arcs_mtx);
3902 
3903         buf_fini();
3904 
3905         ASSERT(arc_loaned_bytes == 0);
3906 }
3907 
3908 /*
3909  * Level 2 ARC
3910  *
3911  * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
3912  * It uses dedicated storage devices to hold cached data, which are populated
3913  * using large infrequent writes.  The main role of this cache is to boost
3914  * the performance of random read workloads.  The intended L2ARC devices
3915  * include short-stroked disks, solid state disks, and other media with
3916  * substantially faster read latency than disk.
3917  *
3918  *                 +-----------------------+
3919  *                 |         ARC           |
3920  *                 +-----------------------+
3921  *                    |         ^     ^
3922  *                    |         |     |
3923  *      l2arc_feed_thread()    arc_read()
3924  *                    |         |     |
3925  *                    |  l2arc read   |
3926  *                    V         |     |
3927  *               +---------------+    |
3928  *               |     L2ARC     |    |
3929  *               +---------------+    |
3930  *                   |    ^           |
3931  *          l2arc_write() |           |
3932  *                   |    |           |
3933  *                   V    |           |
3934  *                 +-------+      +-------+
3935  *                 | vdev  |      | vdev  |
3936  *                 | cache |      | cache |
3937  *                 +-------+      +-------+
3938  *                 +=========+     .-----.
3939  *                 :  L2ARC  :    |-_____-|
3940  *                 : devices :    | Disks |
3941  *                 +=========+    `-_____-'
3942  *
3943  * Read requests are satisfied from the following sources, in order:
3944  *
3945  *      1) ARC
3946  *      2) vdev cache of L2ARC devices
3947  *      3) L2ARC devices
3948  *      4) vdev cache of disks
3949  *      5) disks
3950  *
3951  * Some L2ARC device types exhibit extremely slow write performance.
3952  * To accommodate for this there are some significant differences between
3953  * the L2ARC and traditional cache design:
3954  *
3955  * 1. There is no eviction path from the ARC to the L2ARC.  Evictions from
3956  * the ARC behave as usual, freeing buffers and placing headers on ghost
3957  * lists.  The ARC does not send buffers to the L2ARC during eviction as
3958  * this would add inflated write latencies for all ARC memory pressure.
3959  *
3960  * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
3961  * It does this by periodically scanning buffers from the eviction-end of
3962  * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
3963  * not already there. It scans until a headroom of buffers is satisfied,
3964  * which itself is a buffer for ARC eviction. If a compressible buffer is
3965  * found during scanning and selected for writing to an L2ARC device, we
3966  * temporarily boost scanning headroom during the next scan cycle to make
3967  * sure we adapt to compression effects (which might significantly reduce
3968  * the data volume we write to L2ARC). The thread that does this is
3969  * l2arc_feed_thread(), illustrated below; example sizes are included to
3970  * provide a better sense of ratio than this diagram:
3971  *
3972  *             head -->                        tail
3973  *              +---------------------+----------+
3974  *      ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->.   # already on L2ARC
3975  *              +---------------------+----------+   |   o L2ARC eligible
3976  *      ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->|   : ARC buffer
3977  *              +---------------------+----------+   |
3978  *                   15.9 Gbytes      ^ 32 Mbytes    |
3979  *                                 headroom          |
3980  *                                            l2arc_feed_thread()
3981  *                                                   |
3982  *                       l2arc write hand <--[oooo]--'
3983  *                               |           8 Mbyte
3984  *                               |          write max
3985  *                               V
3986  *                +==============================+
3987  *      L2ARC dev |####|#|###|###|    |####| ... |
3988  *                +==============================+
3989  *                           32 Gbytes
3990  *
3991  * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
3992  * evicted, then the L2ARC has cached a buffer much sooner than it probably
3993  * needed to, potentially wasting L2ARC device bandwidth and storage.  It is
3994  * safe to say that this is an uncommon case, since buffers at the end of
3995  * the ARC lists have moved there due to inactivity.
3996  *
3997  * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
3998  * then the L2ARC simply misses copying some buffers.  This serves as a
3999  * pressure valve to prevent heavy read workloads from both stalling the ARC
4000  * with waits and clogging the L2ARC with writes.  This also helps prevent
4001  * the potential for the L2ARC to churn if it attempts to cache content too
4002  * quickly, such as during backups of the entire pool.
4003  *
4004  * 5. After system boot and before the ARC has filled main memory, there are
4005  * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
4006  * lists can remain mostly static.  Instead of searching from tail of these
4007  * lists as pictured, the l2arc_feed_thread() will search from the list heads
4008  * for eligible buffers, greatly increasing its chance of finding them.
4009  *
4010  * The L2ARC device write speed is also boosted during this time so that
4011  * the L2ARC warms up faster.  Since there have been no ARC evictions yet,
4012  * there are no L2ARC reads, and no fear of degrading read performance
4013  * through increased writes.
4014  *
4015  * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
4016  * the vdev queue can aggregate them into larger and fewer writes.  Each
4017  * device is written to in a rotor fashion, sweeping writes through
4018  * available space then repeating.
4019  *
4020  * 7. The L2ARC does not store dirty content.  It never needs to flush
4021  * write buffers back to disk based storage.
4022  *
4023  * 8. If an ARC buffer is written (and dirtied) which also exists in the
4024  * L2ARC, the now stale L2ARC buffer is immediately dropped.
4025  *
4026  * The performance of the L2ARC can be tweaked by a number of tunables, which
4027  * may be necessary for different workloads:
4028  *
4029  *      l2arc_write_max         max write bytes per interval
4030  *      l2arc_write_boost       extra write bytes during device warmup
4031  *      l2arc_noprefetch        skip caching prefetched buffers
4032  *      l2arc_headroom          number of max device writes to precache
4033  *      l2arc_headroom_boost    when we find compressed buffers during ARC
4034  *                              scanning, we multiply headroom by this
4035  *                              percentage factor for the next scan cycle,
4036  *                              since more compressed buffers are likely to
4037  *                              be present
4038  *      l2arc_feed_secs         seconds between L2ARC writing
4039  *
4040  * Tunables may be removed or added as future performance improvements are
4041  * integrated, and also may become zpool properties.
4042  *
4043  * There are three key functions that control how the L2ARC warms up:
4044  *
4045  *      l2arc_write_eligible()  check if a buffer is eligible to cache
4046  *      l2arc_write_size()      calculate how much to write
4047  *      l2arc_write_interval()  calculate sleep delay between writes
4048  *
4049  * These three functions determine what to write, how much, and how quickly
4050  * to send writes.
4051  */
4052 
4053 static boolean_t
4054 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
4055 {
4056         /*
4057          * A buffer is *not* eligible for the L2ARC if it:
4058          * 1. belongs to a different spa.
4059          * 2. is already cached on the L2ARC.
4060          * 3. has an I/O in progress (it may be an incomplete read).
4061          * 4. is flagged not eligible (zfs property).
4062          */
4063         if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL ||
4064             HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab))
4065                 return (B_FALSE);
4066 
4067         return (B_TRUE);
4068 }
4069 
4070 static uint64_t
4071 l2arc_write_size(void)
4072 {
4073         uint64_t size;
4074 
4075         /*
4076          * Make sure our globals have meaningful values in case the user
4077          * altered them.
4078          */
4079         size = l2arc_write_max;
4080         if (size == 0) {
4081                 cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must "
4082                     "be greater than zero, resetting it to the default (%d)",
4083                     L2ARC_WRITE_SIZE);
4084                 size = l2arc_write_max = L2ARC_WRITE_SIZE;
4085         }
4086 
4087         if (arc_warm == B_FALSE)
4088                 size += l2arc_write_boost;
4089 
4090         return (size);
4091 
4092 }
4093 
4094 static clock_t
4095 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
4096 {
4097         clock_t interval, next, now;
4098 
4099         /*
4100          * If the ARC lists are busy, increase our write rate; if the
4101          * lists are stale, idle back.  This is achieved by checking
4102          * how much we previously wrote - if it was more than half of
4103          * what we wanted, schedule the next write much sooner.
4104          */
4105         if (l2arc_feed_again && wrote > (wanted / 2))
4106                 interval = (hz * l2arc_feed_min_ms) / 1000;
4107         else
4108                 interval = hz * l2arc_feed_secs;
4109 
4110         now = ddi_get_lbolt();
4111         next = MAX(now, MIN(now + interval, began + interval));
4112 
4113         return (next);
4114 }
4115 
4116 static void
4117 l2arc_hdr_stat_add(void)
4118 {
4119         ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
4120         ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
4121 }
4122 
4123 static void
4124 l2arc_hdr_stat_remove(void)
4125 {
4126         ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
4127         ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
4128 }
4129 
4130 /*
4131  * Cycle through L2ARC devices.  This is how L2ARC load balances.
4132  * If a device is returned, this also returns holding the spa config lock.
4133  */
4134 static l2arc_dev_t *
4135 l2arc_dev_get_next(void)
4136 {
4137         l2arc_dev_t *first, *next = NULL;
4138 
4139         /*
4140          * Lock out the removal of spas (spa_namespace_lock), then removal
4141          * of cache devices (l2arc_dev_mtx).  Once a device has been selected,
4142          * both locks will be dropped and a spa config lock held instead.
4143          */
4144         mutex_enter(&spa_namespace_lock);
4145         mutex_enter(&l2arc_dev_mtx);
4146 
4147         /* if there are no vdevs, there is nothing to do */
4148         if (l2arc_ndev == 0)
4149                 goto out;
4150 
4151         first = NULL;
4152         next = l2arc_dev_last;
4153         do {
4154                 /* loop around the list looking for a non-faulted vdev */
4155                 if (next == NULL) {
4156                         next = list_head(l2arc_dev_list);
4157                 } else {
4158                         next = list_next(l2arc_dev_list, next);
4159                         if (next == NULL)
4160                                 next = list_head(l2arc_dev_list);
4161                 }
4162 
4163                 /* if we have come back to the start, bail out */
4164                 if (first == NULL)
4165                         first = next;
4166                 else if (next == first)
4167                         break;
4168 
4169         } while (vdev_is_dead(next->l2ad_vdev));
4170 
4171         /* if we were unable to find any usable vdevs, return NULL */
4172         if (vdev_is_dead(next->l2ad_vdev))
4173                 next = NULL;
4174 
4175         l2arc_dev_last = next;
4176 
4177 out:
4178         mutex_exit(&l2arc_dev_mtx);
4179 
4180         /*
4181          * Grab the config lock to prevent the 'next' device from being
4182          * removed while we are writing to it.
4183          */
4184         if (next != NULL)
4185                 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
4186         mutex_exit(&spa_namespace_lock);
4187 
4188         return (next);
4189 }
4190 
4191 /*
4192  * Free buffers that were tagged for destruction.
4193  */
4194 static void
4195 l2arc_do_free_on_write()
4196 {
4197         list_t *buflist;
4198         l2arc_data_free_t *df, *df_prev;
4199 
4200         mutex_enter(&l2arc_free_on_write_mtx);
4201         buflist = l2arc_free_on_write;
4202 
4203         for (df = list_tail(buflist); df; df = df_prev) {
4204                 df_prev = list_prev(buflist, df);
4205                 ASSERT(df->l2df_data != NULL);
4206                 ASSERT(df->l2df_func != NULL);
4207                 df->l2df_func(df->l2df_data, df->l2df_size);
4208                 list_remove(buflist, df);
4209                 kmem_free(df, sizeof (l2arc_data_free_t));
4210         }
4211 
4212         mutex_exit(&l2arc_free_on_write_mtx);
4213 }
4214 
4215 /*
4216  * A write to a cache device has completed.  Update all headers to allow
4217  * reads from these buffers to begin.
4218  */
4219 static void
4220 l2arc_write_done(zio_t *zio)
4221 {
4222         l2arc_write_callback_t *cb;
4223         l2arc_dev_t *dev;
4224         list_t *buflist;
4225         arc_buf_hdr_t *head, *ab, *ab_prev;
4226         l2arc_buf_hdr_t *abl2;
4227         kmutex_t *hash_lock;
4228         int64_t bytes_dropped = 0;
4229 
4230         cb = zio->io_private;
4231         ASSERT(cb != NULL);
4232         dev = cb->l2wcb_dev;
4233         ASSERT(dev != NULL);
4234         head = cb->l2wcb_head;
4235         ASSERT(head != NULL);
4236         buflist = dev->l2ad_buflist;
4237         ASSERT(buflist != NULL);
4238         DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
4239             l2arc_write_callback_t *, cb);
4240 
4241         if (zio->io_error != 0)
4242                 ARCSTAT_BUMP(arcstat_l2_writes_error);
4243 
4244         mutex_enter(&l2arc_buflist_mtx);
4245 
4246         /*
4247          * All writes completed, or an error was hit.
4248          */
4249         for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
4250                 ab_prev = list_prev(buflist, ab);
4251                 abl2 = ab->b_l2hdr;
4252 
4253                 /*
4254                  * Release the temporary compressed buffer as soon as possible.
4255                  */
4256                 if (abl2->b_compress != ZIO_COMPRESS_OFF)
4257                         l2arc_release_cdata_buf(ab);
4258 
4259                 hash_lock = HDR_LOCK(ab);
4260                 if (!mutex_tryenter(hash_lock)) {
4261                         /*
4262                          * This buffer misses out.  It may be in a stage
4263                          * of eviction.  Its ARC_L2_WRITING flag will be
4264                          * left set, denying reads to this buffer.
4265                          */
4266                         ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
4267                         continue;
4268                 }
4269 
4270                 if (zio->io_error != 0) {
4271                         /*
4272                          * Error - drop L2ARC entry.
4273                          */
4274                         list_remove(buflist, ab);
4275                         ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize);
4276                         bytes_dropped += abl2->b_asize;
4277                         ab->b_l2hdr = NULL;
4278                         kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4279                         ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4280                 }
4281 
4282                 /*
4283                  * Allow ARC to begin reads to this L2ARC entry.
4284                  */
4285                 ab->b_flags &= ~ARC_L2_WRITING;
4286 
4287                 mutex_exit(hash_lock);
4288         }
4289 
4290         atomic_inc_64(&l2arc_writes_done);
4291         list_remove(buflist, head);
4292         kmem_cache_free(hdr_cache, head);
4293         mutex_exit(&l2arc_buflist_mtx);
4294 
4295         vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0);
4296 
4297         l2arc_do_free_on_write();
4298 
4299         kmem_free(cb, sizeof (l2arc_write_callback_t));
4300 }
4301 
4302 /*
4303  * A read to a cache device completed.  Validate buffer contents before
4304  * handing over to the regular ARC routines.
4305  */
4306 static void
4307 l2arc_read_done(zio_t *zio)
4308 {
4309         l2arc_read_callback_t *cb;
4310         arc_buf_hdr_t *hdr;
4311         arc_buf_t *buf;
4312         kmutex_t *hash_lock;
4313         int equal;
4314 
4315         ASSERT(zio->io_vd != NULL);
4316         ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
4317 
4318         spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
4319 
4320         cb = zio->io_private;
4321         ASSERT(cb != NULL);
4322         buf = cb->l2rcb_buf;
4323         ASSERT(buf != NULL);
4324 
4325         hash_lock = HDR_LOCK(buf->b_hdr);
4326         mutex_enter(hash_lock);
4327         hdr = buf->b_hdr;
4328         ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
4329 
4330         /*
4331          * If the buffer was compressed, decompress it first.
4332          */
4333         if (cb->l2rcb_compress != ZIO_COMPRESS_OFF)
4334                 l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress);
4335         ASSERT(zio->io_data != NULL);
4336 
4337         /*
4338          * Check this survived the L2ARC journey.
4339          */
4340         equal = arc_cksum_equal(buf);
4341         if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
4342                 mutex_exit(hash_lock);
4343                 zio->io_private = buf;
4344                 zio->io_bp_copy = cb->l2rcb_bp;   /* XXX fix in L2ARC 2.0 */
4345                 zio->io_bp = &zio->io_bp_copy;        /* XXX fix in L2ARC 2.0 */
4346                 arc_read_done(zio);
4347         } else {
4348                 mutex_exit(hash_lock);
4349                 /*
4350                  * Buffer didn't survive caching.  Increment stats and
4351                  * reissue to the original storage device.
4352                  */
4353                 if (zio->io_error != 0) {
4354                         ARCSTAT_BUMP(arcstat_l2_io_error);
4355                 } else {
4356                         zio->io_error = SET_ERROR(EIO);
4357                 }
4358                 if (!equal)
4359                         ARCSTAT_BUMP(arcstat_l2_cksum_bad);
4360 
4361                 /*
4362                  * If there's no waiter, issue an async i/o to the primary
4363                  * storage now.  If there *is* a waiter, the caller must
4364                  * issue the i/o in a context where it's OK to block.
4365                  */
4366                 if (zio->io_waiter == NULL) {
4367                         zio_t *pio = zio_unique_parent(zio);
4368 
4369                         ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
4370 
4371                         zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
4372                             buf->b_data, zio->io_size, arc_read_done, buf,
4373                             zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
4374                 }
4375         }
4376 
4377         kmem_free(cb, sizeof (l2arc_read_callback_t));
4378 }
4379 
4380 /*
4381  * This is the list priority from which the L2ARC will search for pages to
4382  * cache.  This is used within loops (0..3) to cycle through lists in the
4383  * desired order.  This order can have a significant effect on cache
4384  * performance.
4385  *
4386  * Currently the metadata lists are hit first, MFU then MRU, followed by
4387  * the data lists.  This function returns a locked list, and also returns
4388  * the lock pointer.
4389  */
4390 static list_t *
4391 l2arc_list_locked(int list_num, kmutex_t **lock)
4392 {
4393         list_t *list = NULL;
4394 
4395         ASSERT(list_num >= 0 && list_num <= 3);
4396 
4397         switch (list_num) {
4398         case 0:
4399                 list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
4400                 *lock = &arc_mfu->arcs_mtx;
4401                 break;
4402         case 1:
4403                 list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
4404                 *lock = &arc_mru->arcs_mtx;
4405                 break;
4406         case 2:
4407                 list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
4408                 *lock = &arc_mfu->arcs_mtx;
4409                 break;
4410         case 3:
4411                 list = &arc_mru->arcs_list[ARC_BUFC_DATA];
4412                 *lock = &arc_mru->arcs_mtx;
4413                 break;
4414         }
4415 
4416         ASSERT(!(MUTEX_HELD(*lock)));
4417         mutex_enter(*lock);
4418         return (list);
4419 }
4420 
4421 /*
4422  * Evict buffers from the device write hand to the distance specified in
4423  * bytes.  This distance may span populated buffers, it may span nothing.
4424  * This is clearing a region on the L2ARC device ready for writing.
4425  * If the 'all' boolean is set, every buffer is evicted.
4426  */
4427 static void
4428 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
4429 {
4430         list_t *buflist;
4431         l2arc_buf_hdr_t *abl2;
4432         arc_buf_hdr_t *ab, *ab_prev;
4433         kmutex_t *hash_lock;
4434         uint64_t taddr;
4435         int64_t bytes_evicted = 0;
4436 
4437         buflist = dev->l2ad_buflist;
4438 
4439         if (buflist == NULL)
4440                 return;
4441 
4442         if (!all && dev->l2ad_first) {
4443                 /*
4444                  * This is the first sweep through the device.  There is
4445                  * nothing to evict.
4446                  */
4447                 return;
4448         }
4449 
4450         if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
4451                 /*
4452                  * When nearing the end of the device, evict to the end
4453                  * before the device write hand jumps to the start.
4454                  */
4455                 taddr = dev->l2ad_end;
4456         } else {
4457                 taddr = dev->l2ad_hand + distance;
4458         }
4459         DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
4460             uint64_t, taddr, boolean_t, all);
4461 
4462 top:
4463         mutex_enter(&l2arc_buflist_mtx);
4464         for (ab = list_tail(buflist); ab; ab = ab_prev) {
4465                 ab_prev = list_prev(buflist, ab);
4466 
4467                 hash_lock = HDR_LOCK(ab);
4468                 if (!mutex_tryenter(hash_lock)) {
4469                         /*
4470                          * Missed the hash lock.  Retry.
4471                          */
4472                         ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
4473                         mutex_exit(&l2arc_buflist_mtx);
4474                         mutex_enter(hash_lock);
4475                         mutex_exit(hash_lock);
4476                         goto top;
4477                 }
4478 
4479                 if (HDR_L2_WRITE_HEAD(ab)) {
4480                         /*
4481                          * We hit a write head node.  Leave it for
4482                          * l2arc_write_done().
4483                          */
4484                         list_remove(buflist, ab);
4485                         mutex_exit(hash_lock);
4486                         continue;
4487                 }
4488 
4489                 if (!all && ab->b_l2hdr != NULL &&
4490                     (ab->b_l2hdr->b_daddr > taddr ||
4491                     ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
4492                         /*
4493                          * We've evicted to the target address,
4494                          * or the end of the device.
4495                          */
4496                         mutex_exit(hash_lock);
4497                         break;
4498                 }
4499 
4500                 if (HDR_FREE_IN_PROGRESS(ab)) {
4501                         /*
4502                          * Already on the path to destruction.
4503                          */
4504                         mutex_exit(hash_lock);
4505                         continue;
4506                 }
4507 
4508                 if (ab->b_state == arc_l2c_only) {
4509                         ASSERT(!HDR_L2_READING(ab));
4510                         /*
4511                          * This doesn't exist in the ARC.  Destroy.
4512                          * arc_hdr_destroy() will call list_remove()
4513                          * and decrement arcstat_l2_size.
4514                          */
4515                         arc_change_state(arc_anon, ab, hash_lock);
4516                         arc_hdr_destroy(ab);
4517                 } else {
4518                         /*
4519                          * Invalidate issued or about to be issued
4520                          * reads, since we may be about to write
4521                          * over this location.
4522                          */
4523                         if (HDR_L2_READING(ab)) {
4524                                 ARCSTAT_BUMP(arcstat_l2_evict_reading);
4525                                 ab->b_flags |= ARC_L2_EVICTED;
4526                         }
4527 
4528                         /*
4529                          * Tell ARC this no longer exists in L2ARC.
4530                          */
4531                         if (ab->b_l2hdr != NULL) {
4532                                 abl2 = ab->b_l2hdr;
4533                                 ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize);
4534                                 bytes_evicted += abl2->b_asize;
4535                                 ab->b_l2hdr = NULL;
4536                                 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4537                                 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4538                         }
4539                         list_remove(buflist, ab);
4540 
4541                         /*
4542                          * This may have been leftover after a
4543                          * failed write.
4544                          */
4545                         ab->b_flags &= ~ARC_L2_WRITING;
4546                 }
4547                 mutex_exit(hash_lock);
4548         }
4549         mutex_exit(&l2arc_buflist_mtx);
4550 
4551         vdev_space_update(dev->l2ad_vdev, -bytes_evicted, 0, 0);
4552         dev->l2ad_evict = taddr;
4553 }
4554 
4555 /*
4556  * Find and write ARC buffers to the L2ARC device.
4557  *
4558  * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4559  * for reading until they have completed writing.
4560  * The headroom_boost is an in-out parameter used to maintain headroom boost
4561  * state between calls to this function.
4562  *
4563  * Returns the number of bytes actually written (which may be smaller than
4564  * the delta by which the device hand has changed due to alignment).
4565  */
4566 static uint64_t
4567 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz,
4568     boolean_t *headroom_boost)
4569 {
4570         arc_buf_hdr_t *ab, *ab_prev, *head;
4571         list_t *list;
4572         uint64_t write_asize, write_psize, write_sz, headroom,
4573             buf_compress_minsz;
4574         void *buf_data;
4575         kmutex_t *list_lock;
4576         boolean_t full;
4577         l2arc_write_callback_t *cb;
4578         zio_t *pio, *wzio;
4579         uint64_t guid = spa_load_guid(spa);
4580         const boolean_t do_headroom_boost = *headroom_boost;
4581 
4582         ASSERT(dev->l2ad_vdev != NULL);
4583 
4584         /* Lower the flag now, we might want to raise it again later. */
4585         *headroom_boost = B_FALSE;
4586 
4587         pio = NULL;
4588         write_sz = write_asize = write_psize = 0;
4589         full = B_FALSE;
4590         head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
4591         head->b_flags |= ARC_L2_WRITE_HEAD;
4592 
4593         /*
4594          * We will want to try to compress buffers that are at least 2x the
4595          * device sector size.
4596          */
4597         buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift;
4598 
4599         /*
4600          * Copy buffers for L2ARC writing.
4601          */
4602         mutex_enter(&l2arc_buflist_mtx);
4603         for (int try = 0; try <= 3; try++) {
4604                 uint64_t passed_sz = 0;
4605 
4606                 list = l2arc_list_locked(try, &list_lock);
4607 
4608                 /*
4609                  * L2ARC fast warmup.
4610                  *
4611                  * Until the ARC is warm and starts to evict, read from the
4612                  * head of the ARC lists rather than the tail.
4613                  */
4614                 if (arc_warm == B_FALSE)
4615                         ab = list_head(list);
4616                 else
4617                         ab = list_tail(list);
4618 
4619                 headroom = target_sz * l2arc_headroom;
4620                 if (do_headroom_boost)
4621                         headroom = (headroom * l2arc_headroom_boost) / 100;
4622 
4623                 for (; ab; ab = ab_prev) {
4624                         l2arc_buf_hdr_t *l2hdr;
4625                         kmutex_t *hash_lock;
4626                         uint64_t buf_sz;
4627 
4628                         if (arc_warm == B_FALSE)
4629                                 ab_prev = list_next(list, ab);
4630                         else
4631                                 ab_prev = list_prev(list, ab);
4632 
4633                         hash_lock = HDR_LOCK(ab);
4634                         if (!mutex_tryenter(hash_lock)) {
4635                                 /*
4636                                  * Skip this buffer rather than waiting.
4637                                  */
4638                                 continue;
4639                         }
4640 
4641                         passed_sz += ab->b_size;
4642                         if (passed_sz > headroom) {
4643                                 /*
4644                                  * Searched too far.
4645                                  */
4646                                 mutex_exit(hash_lock);
4647                                 break;
4648                         }
4649 
4650                         if (!l2arc_write_eligible(guid, ab)) {
4651                                 mutex_exit(hash_lock);
4652                                 continue;
4653                         }
4654 
4655                         if ((write_sz + ab->b_size) > target_sz) {
4656                                 full = B_TRUE;
4657                                 mutex_exit(hash_lock);
4658                                 break;
4659                         }
4660 
4661                         if (pio == NULL) {
4662                                 /*
4663                                  * Insert a dummy header on the buflist so
4664                                  * l2arc_write_done() can find where the
4665                                  * write buffers begin without searching.
4666                                  */
4667                                 list_insert_head(dev->l2ad_buflist, head);
4668 
4669                                 cb = kmem_alloc(
4670                                     sizeof (l2arc_write_callback_t), KM_SLEEP);
4671                                 cb->l2wcb_dev = dev;
4672                                 cb->l2wcb_head = head;
4673                                 pio = zio_root(spa, l2arc_write_done, cb,
4674                                     ZIO_FLAG_CANFAIL);
4675                         }
4676 
4677                         /*
4678                          * Create and add a new L2ARC header.
4679                          */
4680                         l2hdr = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP);
4681                         l2hdr->b_dev = dev;
4682                         ab->b_flags |= ARC_L2_WRITING;
4683 
4684                         /*
4685                          * Temporarily stash the data buffer in b_tmp_cdata.
4686                          * The subsequent write step will pick it up from
4687                          * there. This is because can't access ab->b_buf
4688                          * without holding the hash_lock, which we in turn
4689                          * can't access without holding the ARC list locks
4690                          * (which we want to avoid during compression/writing).
4691                          */
4692                         l2hdr->b_compress = ZIO_COMPRESS_OFF;
4693                         l2hdr->b_asize = ab->b_size;
4694                         l2hdr->b_tmp_cdata = ab->b_buf->b_data;
4695 
4696                         buf_sz = ab->b_size;
4697                         ab->b_l2hdr = l2hdr;
4698 
4699                         list_insert_head(dev->l2ad_buflist, ab);
4700 
4701                         /*
4702                          * Compute and store the buffer cksum before
4703                          * writing.  On debug the cksum is verified first.
4704                          */
4705                         arc_cksum_verify(ab->b_buf);
4706                         arc_cksum_compute(ab->b_buf, B_TRUE);
4707 
4708                         mutex_exit(hash_lock);
4709 
4710                         write_sz += buf_sz;
4711                 }
4712 
4713                 mutex_exit(list_lock);
4714 
4715                 if (full == B_TRUE)
4716                         break;
4717         }
4718 
4719         /* No buffers selected for writing? */
4720         if (pio == NULL) {
4721                 ASSERT0(write_sz);
4722                 mutex_exit(&l2arc_buflist_mtx);
4723                 kmem_cache_free(hdr_cache, head);
4724                 return (0);
4725         }
4726 
4727         /*
4728          * Now start writing the buffers. We're starting at the write head
4729          * and work backwards, retracing the course of the buffer selector
4730          * loop above.
4731          */
4732         for (ab = list_prev(dev->l2ad_buflist, head); ab;
4733             ab = list_prev(dev->l2ad_buflist, ab)) {
4734                 l2arc_buf_hdr_t *l2hdr;
4735                 uint64_t buf_sz;
4736 
4737                 /*
4738                  * We shouldn't need to lock the buffer here, since we flagged
4739                  * it as ARC_L2_WRITING in the previous step, but we must take
4740                  * care to only access its L2 cache parameters. In particular,
4741                  * ab->b_buf may be invalid by now due to ARC eviction.
4742                  */
4743                 l2hdr = ab->b_l2hdr;
4744                 l2hdr->b_daddr = dev->l2ad_hand;
4745 
4746                 if ((ab->b_flags & ARC_L2COMPRESS) &&
4747                     l2hdr->b_asize >= buf_compress_minsz) {
4748                         if (l2arc_compress_buf(l2hdr)) {
4749                                 /*
4750                                  * If compression succeeded, enable headroom
4751                                  * boost on the next scan cycle.
4752                                  */
4753                                 *headroom_boost = B_TRUE;
4754                         }
4755                 }
4756 
4757                 /*
4758                  * Pick up the buffer data we had previously stashed away
4759                  * (and now potentially also compressed).
4760                  */
4761                 buf_data = l2hdr->b_tmp_cdata;
4762                 buf_sz = l2hdr->b_asize;
4763 
4764                 /* Compression may have squashed the buffer to zero length. */
4765                 if (buf_sz != 0) {
4766                         uint64_t buf_p_sz;
4767 
4768                         wzio = zio_write_phys(pio, dev->l2ad_vdev,
4769                             dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
4770                             NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
4771                             ZIO_FLAG_CANFAIL, B_FALSE);
4772 
4773                         DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
4774                             zio_t *, wzio);
4775                         (void) zio_nowait(wzio);
4776 
4777                         write_asize += buf_sz;
4778                         /*
4779                          * Keep the clock hand suitably device-aligned.
4780                          */
4781                         buf_p_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
4782                         write_psize += buf_p_sz;
4783                         dev->l2ad_hand += buf_p_sz;
4784                 }
4785         }
4786 
4787         mutex_exit(&l2arc_buflist_mtx);
4788 
4789         ASSERT3U(write_asize, <=, target_sz);
4790         ARCSTAT_BUMP(arcstat_l2_writes_sent);
4791         ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize);
4792         ARCSTAT_INCR(arcstat_l2_size, write_sz);
4793         ARCSTAT_INCR(arcstat_l2_asize, write_asize);
4794         vdev_space_update(dev->l2ad_vdev, write_asize, 0, 0);
4795 
4796         /*
4797          * Bump device hand to the device start if it is approaching the end.
4798          * l2arc_evict() will already have evicted ahead for this case.
4799          */
4800         if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
4801                 dev->l2ad_hand = dev->l2ad_start;
4802                 dev->l2ad_evict = dev->l2ad_start;
4803                 dev->l2ad_first = B_FALSE;
4804         }
4805 
4806         dev->l2ad_writing = B_TRUE;
4807         (void) zio_wait(pio);
4808         dev->l2ad_writing = B_FALSE;
4809 
4810         return (write_asize);
4811 }
4812 
4813 /*
4814  * Compresses an L2ARC buffer.
4815  * The data to be compressed must be prefilled in l2hdr->b_tmp_cdata and its
4816  * size in l2hdr->b_asize. This routine tries to compress the data and
4817  * depending on the compression result there are three possible outcomes:
4818  * *) The buffer was incompressible. The original l2hdr contents were left
4819  *    untouched and are ready for writing to an L2 device.
4820  * *) The buffer was all-zeros, so there is no need to write it to an L2
4821  *    device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is
4822  *    set to zero and b_compress is set to ZIO_COMPRESS_EMPTY.
4823  * *) Compression succeeded and b_tmp_cdata was replaced with a temporary
4824  *    data buffer which holds the compressed data to be written, and b_asize
4825  *    tells us how much data there is. b_compress is set to the appropriate
4826  *    compression algorithm. Once writing is done, invoke
4827  *    l2arc_release_cdata_buf on this l2hdr to free this temporary buffer.
4828  *
4829  * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the
4830  * buffer was incompressible).
4831  */
4832 static boolean_t
4833 l2arc_compress_buf(l2arc_buf_hdr_t *l2hdr)
4834 {
4835         void *cdata;
4836         size_t csize, len, rounded;
4837 
4838         ASSERT(l2hdr->b_compress == ZIO_COMPRESS_OFF);
4839         ASSERT(l2hdr->b_tmp_cdata != NULL);
4840 
4841         len = l2hdr->b_asize;
4842         cdata = zio_data_buf_alloc(len);
4843         csize = zio_compress_data(ZIO_COMPRESS_LZ4, l2hdr->b_tmp_cdata,
4844             cdata, l2hdr->b_asize);
4845 
4846         rounded = P2ROUNDUP(csize, (size_t)SPA_MINBLOCKSIZE);
4847         if (rounded > csize) {
4848                 bzero((char *)cdata + csize, rounded - csize);
4849                 csize = rounded;
4850         }
4851 
4852         if (csize == 0) {
4853                 /* zero block, indicate that there's nothing to write */
4854                 zio_data_buf_free(cdata, len);
4855                 l2hdr->b_compress = ZIO_COMPRESS_EMPTY;
4856                 l2hdr->b_asize = 0;
4857                 l2hdr->b_tmp_cdata = NULL;
4858                 ARCSTAT_BUMP(arcstat_l2_compress_zeros);
4859                 return (B_TRUE);
4860         } else if (csize > 0 && csize < len) {
4861                 /*
4862                  * Compression succeeded, we'll keep the cdata around for
4863                  * writing and release it afterwards.
4864                  */
4865                 l2hdr->b_compress = ZIO_COMPRESS_LZ4;
4866                 l2hdr->b_asize = csize;
4867                 l2hdr->b_tmp_cdata = cdata;
4868                 ARCSTAT_BUMP(arcstat_l2_compress_successes);
4869                 return (B_TRUE);
4870         } else {
4871                 /*
4872                  * Compression failed, release the compressed buffer.
4873                  * l2hdr will be left unmodified.
4874                  */
4875                 zio_data_buf_free(cdata, len);
4876                 ARCSTAT_BUMP(arcstat_l2_compress_failures);
4877                 return (B_FALSE);
4878         }
4879 }
4880 
4881 /*
4882  * Decompresses a zio read back from an l2arc device. On success, the
4883  * underlying zio's io_data buffer is overwritten by the uncompressed
4884  * version. On decompression error (corrupt compressed stream), the
4885  * zio->io_error value is set to signal an I/O error.
4886  *
4887  * Please note that the compressed data stream is not checksummed, so
4888  * if the underlying device is experiencing data corruption, we may feed
4889  * corrupt data to the decompressor, so the decompressor needs to be
4890  * able to handle this situation (LZ4 does).
4891  */
4892 static void
4893 l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c)
4894 {
4895         ASSERT(L2ARC_IS_VALID_COMPRESS(c));
4896 
4897         if (zio->io_error != 0) {
4898                 /*
4899                  * An io error has occured, just restore the original io
4900                  * size in preparation for a main pool read.
4901                  */
4902                 zio->io_orig_size = zio->io_size = hdr->b_size;
4903                 return;
4904         }
4905 
4906         if (c == ZIO_COMPRESS_EMPTY) {
4907                 /*
4908                  * An empty buffer results in a null zio, which means we
4909                  * need to fill its io_data after we're done restoring the
4910                  * buffer's contents.
4911                  */
4912                 ASSERT(hdr->b_buf != NULL);
4913                 bzero(hdr->b_buf->b_data, hdr->b_size);
4914                 zio->io_data = zio->io_orig_data = hdr->b_buf->b_data;
4915         } else {
4916                 ASSERT(zio->io_data != NULL);
4917                 /*
4918                  * We copy the compressed data from the start of the arc buffer
4919                  * (the zio_read will have pulled in only what we need, the
4920                  * rest is garbage which we will overwrite at decompression)
4921                  * and then decompress back to the ARC data buffer. This way we
4922                  * can minimize copying by simply decompressing back over the
4923                  * original compressed data (rather than decompressing to an
4924                  * aux buffer and then copying back the uncompressed buffer,
4925                  * which is likely to be much larger).
4926                  */
4927                 uint64_t csize;
4928                 void *cdata;
4929 
4930                 csize = zio->io_size;
4931                 cdata = zio_data_buf_alloc(csize);
4932                 bcopy(zio->io_data, cdata, csize);
4933                 if (zio_decompress_data(c, cdata, zio->io_data, csize,
4934                     hdr->b_size) != 0)
4935                         zio->io_error = EIO;
4936                 zio_data_buf_free(cdata, csize);
4937         }
4938 
4939         /* Restore the expected uncompressed IO size. */
4940         zio->io_orig_size = zio->io_size = hdr->b_size;
4941 }
4942 
4943 /*
4944  * Releases the temporary b_tmp_cdata buffer in an l2arc header structure.
4945  * This buffer serves as a temporary holder of compressed data while
4946  * the buffer entry is being written to an l2arc device. Once that is
4947  * done, we can dispose of it.
4948  */
4949 static void
4950 l2arc_release_cdata_buf(arc_buf_hdr_t *ab)
4951 {
4952         l2arc_buf_hdr_t *l2hdr = ab->b_l2hdr;
4953 
4954         if (l2hdr->b_compress == ZIO_COMPRESS_LZ4) {
4955                 /*
4956                  * If the data was compressed, then we've allocated a
4957                  * temporary buffer for it, so now we need to release it.
4958                  */
4959                 ASSERT(l2hdr->b_tmp_cdata != NULL);
4960                 zio_data_buf_free(l2hdr->b_tmp_cdata, ab->b_size);
4961         }
4962         l2hdr->b_tmp_cdata = NULL;
4963 }
4964 
4965 /*
4966  * This thread feeds the L2ARC at regular intervals.  This is the beating
4967  * heart of the L2ARC.
4968  */
4969 static void
4970 l2arc_feed_thread(void)
4971 {
4972         callb_cpr_t cpr;
4973         l2arc_dev_t *dev;
4974         spa_t *spa;
4975         uint64_t size, wrote;
4976         clock_t begin, next = ddi_get_lbolt();
4977         boolean_t headroom_boost = B_FALSE;
4978 
4979         CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
4980 
4981         mutex_enter(&l2arc_feed_thr_lock);
4982 
4983         while (l2arc_thread_exit == 0) {
4984                 CALLB_CPR_SAFE_BEGIN(&cpr);
4985                 (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock,
4986                     next);
4987                 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
4988                 next = ddi_get_lbolt() + hz;
4989 
4990                 /*
4991                  * Quick check for L2ARC devices.
4992                  */
4993                 mutex_enter(&l2arc_dev_mtx);
4994                 if (l2arc_ndev == 0) {
4995                         mutex_exit(&l2arc_dev_mtx);
4996                         continue;
4997                 }
4998                 mutex_exit(&l2arc_dev_mtx);
4999                 begin = ddi_get_lbolt();
5000 
5001                 /*
5002                  * This selects the next l2arc device to write to, and in
5003                  * doing so the next spa to feed from: dev->l2ad_spa.   This
5004                  * will return NULL if there are now no l2arc devices or if
5005                  * they are all faulted.
5006                  *
5007                  * If a device is returned, its spa's config lock is also
5008                  * held to prevent device removal.  l2arc_dev_get_next()
5009                  * will grab and release l2arc_dev_mtx.
5010                  */
5011                 if ((dev = l2arc_dev_get_next()) == NULL)
5012                         continue;
5013 
5014                 spa = dev->l2ad_spa;
5015                 ASSERT(spa != NULL);
5016 
5017                 /*
5018                  * If the pool is read-only then force the feed thread to
5019                  * sleep a little longer.
5020                  */
5021                 if (!spa_writeable(spa)) {
5022                         next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
5023                         spa_config_exit(spa, SCL_L2ARC, dev);
5024                         continue;
5025                 }
5026 
5027                 /*
5028                  * Avoid contributing to memory pressure.
5029                  */
5030                 if (arc_reclaim_needed()) {
5031                         ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
5032                         spa_config_exit(spa, SCL_L2ARC, dev);
5033                         continue;
5034                 }
5035 
5036                 ARCSTAT_BUMP(arcstat_l2_feeds);
5037 
5038                 size = l2arc_write_size();
5039 
5040                 /*
5041                  * Evict L2ARC buffers that will be overwritten.
5042                  */
5043                 l2arc_evict(dev, size, B_FALSE);
5044 
5045                 /*
5046                  * Write ARC buffers.
5047                  */
5048                 wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost);
5049 
5050                 /*
5051                  * Calculate interval between writes.
5052                  */
5053                 next = l2arc_write_interval(begin, size, wrote);
5054                 spa_config_exit(spa, SCL_L2ARC, dev);
5055         }
5056 
5057         l2arc_thread_exit = 0;
5058         cv_broadcast(&l2arc_feed_thr_cv);
5059         CALLB_CPR_EXIT(&cpr);               /* drops l2arc_feed_thr_lock */
5060         thread_exit();
5061 }
5062 
5063 boolean_t
5064 l2arc_vdev_present(vdev_t *vd)
5065 {
5066         l2arc_dev_t *dev;
5067 
5068         mutex_enter(&l2arc_dev_mtx);
5069         for (dev = list_head(l2arc_dev_list); dev != NULL;
5070             dev = list_next(l2arc_dev_list, dev)) {
5071                 if (dev->l2ad_vdev == vd)
5072                         break;
5073         }
5074         mutex_exit(&l2arc_dev_mtx);
5075 
5076         return (dev != NULL);
5077 }
5078 
5079 /*
5080  * Add a vdev for use by the L2ARC.  By this point the spa has already
5081  * validated the vdev and opened it.
5082  */
5083 void
5084 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
5085 {
5086         l2arc_dev_t *adddev;
5087 
5088         ASSERT(!l2arc_vdev_present(vd));
5089 
5090         /*
5091          * Create a new l2arc device entry.
5092          */
5093         adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
5094         adddev->l2ad_spa = spa;
5095         adddev->l2ad_vdev = vd;
5096         adddev->l2ad_start = VDEV_LABEL_START_SIZE;
5097         adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
5098         adddev->l2ad_hand = adddev->l2ad_start;
5099         adddev->l2ad_evict = adddev->l2ad_start;
5100         adddev->l2ad_first = B_TRUE;
5101         adddev->l2ad_writing = B_FALSE;
5102 
5103         /*
5104          * This is a list of all ARC buffers that are still valid on the
5105          * device.
5106          */
5107         adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
5108         list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
5109             offsetof(arc_buf_hdr_t, b_l2node));
5110 
5111         vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
5112 
5113         /*
5114          * Add device to global list
5115          */
5116         mutex_enter(&l2arc_dev_mtx);
5117         list_insert_head(l2arc_dev_list, adddev);
5118         atomic_inc_64(&l2arc_ndev);
5119         mutex_exit(&l2arc_dev_mtx);
5120 }
5121 
5122 /*
5123  * Remove a vdev from the L2ARC.
5124  */
5125 void
5126 l2arc_remove_vdev(vdev_t *vd)
5127 {
5128         l2arc_dev_t *dev, *nextdev, *remdev = NULL;
5129 
5130         /*
5131          * Find the device by vdev
5132          */
5133         mutex_enter(&l2arc_dev_mtx);
5134         for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
5135                 nextdev = list_next(l2arc_dev_list, dev);
5136                 if (vd == dev->l2ad_vdev) {
5137                         remdev = dev;
5138                         break;
5139                 }
5140         }
5141         ASSERT(remdev != NULL);
5142 
5143         /*
5144          * Remove device from global list
5145          */
5146         list_remove(l2arc_dev_list, remdev);
5147         l2arc_dev_last = NULL;          /* may have been invalidated */
5148         atomic_dec_64(&l2arc_ndev);
5149         mutex_exit(&l2arc_dev_mtx);
5150 
5151         /*
5152          * Clear all buflists and ARC references.  L2ARC device flush.
5153          */
5154         l2arc_evict(remdev, 0, B_TRUE);
5155         list_destroy(remdev->l2ad_buflist);
5156         kmem_free(remdev->l2ad_buflist, sizeof (list_t));
5157         kmem_free(remdev, sizeof (l2arc_dev_t));
5158 }
5159 
5160 void
5161 l2arc_init(void)
5162 {
5163         l2arc_thread_exit = 0;
5164         l2arc_ndev = 0;
5165         l2arc_writes_sent = 0;
5166         l2arc_writes_done = 0;
5167 
5168         mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
5169         cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
5170         mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
5171         mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
5172         mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
5173 
5174         l2arc_dev_list = &L2ARC_dev_list;
5175         l2arc_free_on_write = &L2ARC_free_on_write;
5176         list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
5177             offsetof(l2arc_dev_t, l2ad_node));
5178         list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
5179             offsetof(l2arc_data_free_t, l2df_list_node));
5180 }
5181 
5182 void
5183 l2arc_fini(void)
5184 {
5185         /*
5186          * This is called from dmu_fini(), which is called from spa_fini();
5187          * Because of this, we can assume that all l2arc devices have
5188          * already been removed when the pools themselves were removed.
5189          */
5190 
5191         l2arc_do_free_on_write();
5192 
5193         mutex_destroy(&l2arc_feed_thr_lock);
5194         cv_destroy(&l2arc_feed_thr_cv);
5195         mutex_destroy(&l2arc_dev_mtx);
5196         mutex_destroy(&l2arc_buflist_mtx);
5197         mutex_destroy(&l2arc_free_on_write_mtx);
5198 
5199         list_destroy(l2arc_dev_list);
5200         list_destroy(l2arc_free_on_write);
5201 }
5202 
5203 void
5204 l2arc_start(void)
5205 {
5206         if (!(spa_mode_global & FWRITE))
5207                 return;
5208 
5209         (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
5210             TS_RUN, minclsyspri);
5211 }
5212 
5213 void
5214 l2arc_stop(void)
5215 {
5216         if (!(spa_mode_global & FWRITE))
5217                 return;
5218 
5219         mutex_enter(&l2arc_feed_thr_lock);
5220         cv_signal(&l2arc_feed_thr_cv);      /* kick thread out of startup */
5221         l2arc_thread_exit = 1;
5222         while (l2arc_thread_exit != 0)
5223                 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
5224         mutex_exit(&l2arc_feed_thr_lock);
5225 }