]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/arc.c
238eaa12709ce745c11a7969b7c926a173aca13c
[mirror_zfs.git] / module / zfs / arc.c
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 https://opensource.org/licenses/CDDL-1.0.
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) 2018, Joyent, Inc.
24 * Copyright (c) 2011, 2020, Delphix. All rights reserved.
25 * Copyright (c) 2014, Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2017, Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
28 * Copyright (c) 2020, George Amanakis. All rights reserved.
29 * Copyright (c) 2019, Klara Inc.
30 * Copyright (c) 2019, Allan Jude
31 * Copyright (c) 2020, The FreeBSD Foundation [1]
32 *
33 * [1] Portions of this software were developed by Allan Jude
34 * under sponsorship from the FreeBSD Foundation.
35 */
36
37 /*
38 * DVA-based Adjustable Replacement Cache
39 *
40 * While much of the theory of operation used here is
41 * based on the self-tuning, low overhead replacement cache
42 * presented by Megiddo and Modha at FAST 2003, there are some
43 * significant differences:
44 *
45 * 1. The Megiddo and Modha model assumes any page is evictable.
46 * Pages in its cache cannot be "locked" into memory. This makes
47 * the eviction algorithm simple: evict the last page in the list.
48 * This also make the performance characteristics easy to reason
49 * about. Our cache is not so simple. At any given moment, some
50 * subset of the blocks in the cache are un-evictable because we
51 * have handed out a reference to them. Blocks are only evictable
52 * when there are no external references active. This makes
53 * eviction far more problematic: we choose to evict the evictable
54 * blocks that are the "lowest" in the list.
55 *
56 * There are times when it is not possible to evict the requested
57 * space. In these circumstances we are unable to adjust the cache
58 * size. To prevent the cache growing unbounded at these times we
59 * implement a "cache throttle" that slows the flow of new data
60 * into the cache until we can make space available.
61 *
62 * 2. The Megiddo and Modha model assumes a fixed cache size.
63 * Pages are evicted when the cache is full and there is a cache
64 * miss. Our model has a variable sized cache. It grows with
65 * high use, but also tries to react to memory pressure from the
66 * operating system: decreasing its size when system memory is
67 * tight.
68 *
69 * 3. The Megiddo and Modha model assumes a fixed page size. All
70 * elements of the cache are therefore exactly the same size. So
71 * when adjusting the cache size following a cache miss, its simply
72 * a matter of choosing a single page to evict. In our model, we
73 * have variable sized cache blocks (ranging from 512 bytes to
74 * 128K bytes). We therefore choose a set of blocks to evict to make
75 * space for a cache miss that approximates as closely as possible
76 * the space used by the new block.
77 *
78 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
79 * by N. Megiddo & D. Modha, FAST 2003
80 */
81
82 /*
83 * The locking model:
84 *
85 * A new reference to a cache buffer can be obtained in two
86 * ways: 1) via a hash table lookup using the DVA as a key,
87 * or 2) via one of the ARC lists. The arc_read() interface
88 * uses method 1, while the internal ARC algorithms for
89 * adjusting the cache use method 2. We therefore provide two
90 * types of locks: 1) the hash table lock array, and 2) the
91 * ARC list locks.
92 *
93 * Buffers do not have their own mutexes, rather they rely on the
94 * hash table mutexes for the bulk of their protection (i.e. most
95 * fields in the arc_buf_hdr_t are protected by these mutexes).
96 *
97 * buf_hash_find() returns the appropriate mutex (held) when it
98 * locates the requested buffer in the hash table. It returns
99 * NULL for the mutex if the buffer was not in the table.
100 *
101 * buf_hash_remove() expects the appropriate hash mutex to be
102 * already held before it is invoked.
103 *
104 * Each ARC state also has a mutex which is used to protect the
105 * buffer list associated with the state. When attempting to
106 * obtain a hash table lock while holding an ARC list lock you
107 * must use: mutex_tryenter() to avoid deadlock. Also note that
108 * the active state mutex must be held before the ghost state mutex.
109 *
110 * It as also possible to register a callback which is run when the
111 * metadata limit is reached and no buffers can be safely evicted. In
112 * this case the arc user should drop a reference on some arc buffers so
113 * they can be reclaimed. For example, when using the ZPL each dentry
114 * holds a references on a znode. These dentries must be pruned before
115 * the arc buffer holding the znode can be safely evicted.
116 *
117 * Note that the majority of the performance stats are manipulated
118 * with atomic operations.
119 *
120 * The L2ARC uses the l2ad_mtx on each vdev for the following:
121 *
122 * - L2ARC buflist creation
123 * - L2ARC buflist eviction
124 * - L2ARC write completion, which walks L2ARC buflists
125 * - ARC header destruction, as it removes from L2ARC buflists
126 * - ARC header release, as it removes from L2ARC buflists
127 */
128
129 /*
130 * ARC operation:
131 *
132 * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure.
133 * This structure can point either to a block that is still in the cache or to
134 * one that is only accessible in an L2 ARC device, or it can provide
135 * information about a block that was recently evicted. If a block is
136 * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough
137 * information to retrieve it from the L2ARC device. This information is
138 * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block
139 * that is in this state cannot access the data directly.
140 *
141 * Blocks that are actively being referenced or have not been evicted
142 * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within
143 * the arc_buf_hdr_t that will point to the data block in memory. A block can
144 * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC
145 * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and
146 * also in the arc_buf_hdr_t's private physical data block pointer (b_pabd).
147 *
148 * The L1ARC's data pointer may or may not be uncompressed. The ARC has the
149 * ability to store the physical data (b_pabd) associated with the DVA of the
150 * arc_buf_hdr_t. Since the b_pabd is a copy of the on-disk physical block,
151 * it will match its on-disk compression characteristics. This behavior can be
152 * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the
153 * compressed ARC functionality is disabled, the b_pabd will point to an
154 * uncompressed version of the on-disk data.
155 *
156 * Data in the L1ARC is not accessed by consumers of the ARC directly. Each
157 * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it.
158 * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC
159 * consumer. The ARC will provide references to this data and will keep it
160 * cached until it is no longer in use. The ARC caches only the L1ARC's physical
161 * data block and will evict any arc_buf_t that is no longer referenced. The
162 * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the
163 * "overhead_size" kstat.
164 *
165 * Depending on the consumer, an arc_buf_t can be requested in uncompressed or
166 * compressed form. The typical case is that consumers will want uncompressed
167 * data, and when that happens a new data buffer is allocated where the data is
168 * decompressed for them to use. Currently the only consumer who wants
169 * compressed arc_buf_t's is "zfs send", when it streams data exactly as it
170 * exists on disk. When this happens, the arc_buf_t's data buffer is shared
171 * with the arc_buf_hdr_t.
172 *
173 * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The
174 * first one is owned by a compressed send consumer (and therefore references
175 * the same compressed data buffer as the arc_buf_hdr_t) and the second could be
176 * used by any other consumer (and has its own uncompressed copy of the data
177 * buffer).
178 *
179 * arc_buf_hdr_t
180 * +-----------+
181 * | fields |
182 * | common to |
183 * | L1- and |
184 * | L2ARC |
185 * +-----------+
186 * | l2arc_buf_hdr_t
187 * | |
188 * +-----------+
189 * | l1arc_buf_hdr_t
190 * | | arc_buf_t
191 * | b_buf +------------>+-----------+ arc_buf_t
192 * | b_pabd +-+ |b_next +---->+-----------+
193 * +-----------+ | |-----------| |b_next +-->NULL
194 * | |b_comp = T | +-----------+
195 * | |b_data +-+ |b_comp = F |
196 * | +-----------+ | |b_data +-+
197 * +->+------+ | +-----------+ |
198 * compressed | | | |
199 * data | |<--------------+ | uncompressed
200 * +------+ compressed, | data
201 * shared +-->+------+
202 * data | |
203 * | |
204 * +------+
205 *
206 * When a consumer reads a block, the ARC must first look to see if the
207 * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new
208 * arc_buf_t and either copies uncompressed data into a new data buffer from an
209 * existing uncompressed arc_buf_t, decompresses the hdr's b_pabd buffer into a
210 * new data buffer, or shares the hdr's b_pabd buffer, depending on whether the
211 * hdr is compressed and the desired compression characteristics of the
212 * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the
213 * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be
214 * the last buffer in the hdr's b_buf list, however a shared compressed buf can
215 * be anywhere in the hdr's list.
216 *
217 * The diagram below shows an example of an uncompressed ARC hdr that is
218 * sharing its data with an arc_buf_t (note that the shared uncompressed buf is
219 * the last element in the buf list):
220 *
221 * arc_buf_hdr_t
222 * +-----------+
223 * | |
224 * | |
225 * | |
226 * +-----------+
227 * l2arc_buf_hdr_t| |
228 * | |
229 * +-----------+
230 * l1arc_buf_hdr_t| |
231 * | | arc_buf_t (shared)
232 * | b_buf +------------>+---------+ arc_buf_t
233 * | | |b_next +---->+---------+
234 * | b_pabd +-+ |---------| |b_next +-->NULL
235 * +-----------+ | | | +---------+
236 * | |b_data +-+ | |
237 * | +---------+ | |b_data +-+
238 * +->+------+ | +---------+ |
239 * | | | |
240 * uncompressed | | | |
241 * data +------+ | |
242 * ^ +->+------+ |
243 * | uncompressed | | |
244 * | data | | |
245 * | +------+ |
246 * +---------------------------------+
247 *
248 * Writing to the ARC requires that the ARC first discard the hdr's b_pabd
249 * since the physical block is about to be rewritten. The new data contents
250 * will be contained in the arc_buf_t. As the I/O pipeline performs the write,
251 * it may compress the data before writing it to disk. The ARC will be called
252 * with the transformed data and will memcpy the transformed on-disk block into
253 * a newly allocated b_pabd. Writes are always done into buffers which have
254 * either been loaned (and hence are new and don't have other readers) or
255 * buffers which have been released (and hence have their own hdr, if there
256 * were originally other readers of the buf's original hdr). This ensures that
257 * the ARC only needs to update a single buf and its hdr after a write occurs.
258 *
259 * When the L2ARC is in use, it will also take advantage of the b_pabd. The
260 * L2ARC will always write the contents of b_pabd to the L2ARC. This means
261 * that when compressed ARC is enabled that the L2ARC blocks are identical
262 * to the on-disk block in the main data pool. This provides a significant
263 * advantage since the ARC can leverage the bp's checksum when reading from the
264 * L2ARC to determine if the contents are valid. However, if the compressed
265 * ARC is disabled, then the L2ARC's block must be transformed to look
266 * like the physical block in the main data pool before comparing the
267 * checksum and determining its validity.
268 *
269 * The L1ARC has a slightly different system for storing encrypted data.
270 * Raw (encrypted + possibly compressed) data has a few subtle differences from
271 * data that is just compressed. The biggest difference is that it is not
272 * possible to decrypt encrypted data (or vice-versa) if the keys aren't loaded.
273 * The other difference is that encryption cannot be treated as a suggestion.
274 * If a caller would prefer compressed data, but they actually wind up with
275 * uncompressed data the worst thing that could happen is there might be a
276 * performance hit. If the caller requests encrypted data, however, we must be
277 * sure they actually get it or else secret information could be leaked. Raw
278 * data is stored in hdr->b_crypt_hdr.b_rabd. An encrypted header, therefore,
279 * may have both an encrypted version and a decrypted version of its data at
280 * once. When a caller needs a raw arc_buf_t, it is allocated and the data is
281 * copied out of this header. To avoid complications with b_pabd, raw buffers
282 * cannot be shared.
283 */
284
285 #include <sys/spa.h>
286 #include <sys/zio.h>
287 #include <sys/spa_impl.h>
288 #include <sys/zio_compress.h>
289 #include <sys/zio_checksum.h>
290 #include <sys/zfs_context.h>
291 #include <sys/arc.h>
292 #include <sys/zfs_refcount.h>
293 #include <sys/vdev.h>
294 #include <sys/vdev_impl.h>
295 #include <sys/dsl_pool.h>
296 #include <sys/multilist.h>
297 #include <sys/abd.h>
298 #include <sys/zil.h>
299 #include <sys/fm/fs/zfs.h>
300 #include <sys/callb.h>
301 #include <sys/kstat.h>
302 #include <sys/zthr.h>
303 #include <zfs_fletcher.h>
304 #include <sys/arc_impl.h>
305 #include <sys/trace_zfs.h>
306 #include <sys/aggsum.h>
307 #include <sys/wmsum.h>
308 #include <cityhash.h>
309 #include <sys/vdev_trim.h>
310 #include <sys/zfs_racct.h>
311 #include <sys/zstd/zstd.h>
312
313 #ifndef _KERNEL
314 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
315 boolean_t arc_watch = B_FALSE;
316 #endif
317
318 /*
319 * This thread's job is to keep enough free memory in the system, by
320 * calling arc_kmem_reap_soon() plus arc_reduce_target_size(), which improves
321 * arc_available_memory().
322 */
323 static zthr_t *arc_reap_zthr;
324
325 /*
326 * This thread's job is to keep arc_size under arc_c, by calling
327 * arc_evict(), which improves arc_is_overflowing().
328 */
329 static zthr_t *arc_evict_zthr;
330 static arc_buf_hdr_t **arc_state_evict_markers;
331 static int arc_state_evict_marker_count;
332
333 static kmutex_t arc_evict_lock;
334 static boolean_t arc_evict_needed = B_FALSE;
335 static clock_t arc_last_uncached_flush;
336
337 /*
338 * Count of bytes evicted since boot.
339 */
340 static uint64_t arc_evict_count;
341
342 /*
343 * List of arc_evict_waiter_t's, representing threads waiting for the
344 * arc_evict_count to reach specific values.
345 */
346 static list_t arc_evict_waiters;
347
348 /*
349 * When arc_is_overflowing(), arc_get_data_impl() waits for this percent of
350 * the requested amount of data to be evicted. For example, by default for
351 * every 2KB that's evicted, 1KB of it may be "reused" by a new allocation.
352 * Since this is above 100%, it ensures that progress is made towards getting
353 * arc_size under arc_c. Since this is finite, it ensures that allocations
354 * can still happen, even during the potentially long time that arc_size is
355 * more than arc_c.
356 */
357 static uint_t zfs_arc_eviction_pct = 200;
358
359 /*
360 * The number of headers to evict in arc_evict_state_impl() before
361 * dropping the sublist lock and evicting from another sublist. A lower
362 * value means we're more likely to evict the "correct" header (i.e. the
363 * oldest header in the arc state), but comes with higher overhead
364 * (i.e. more invocations of arc_evict_state_impl()).
365 */
366 static uint_t zfs_arc_evict_batch_limit = 10;
367
368 /* number of seconds before growing cache again */
369 uint_t arc_grow_retry = 5;
370
371 /*
372 * Minimum time between calls to arc_kmem_reap_soon().
373 */
374 static const int arc_kmem_cache_reap_retry_ms = 1000;
375
376 /* shift of arc_c for calculating overflow limit in arc_get_data_impl */
377 static int zfs_arc_overflow_shift = 8;
378
379 /* log2(fraction of arc to reclaim) */
380 uint_t arc_shrink_shift = 7;
381
382 /* percent of pagecache to reclaim arc to */
383 #ifdef _KERNEL
384 uint_t zfs_arc_pc_percent = 0;
385 #endif
386
387 /*
388 * log2(fraction of ARC which must be free to allow growing).
389 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory,
390 * when reading a new block into the ARC, we will evict an equal-sized block
391 * from the ARC.
392 *
393 * This must be less than arc_shrink_shift, so that when we shrink the ARC,
394 * we will still not allow it to grow.
395 */
396 uint_t arc_no_grow_shift = 5;
397
398
399 /*
400 * minimum lifespan of a prefetch block in clock ticks
401 * (initialized in arc_init())
402 */
403 static uint_t arc_min_prefetch_ms;
404 static uint_t arc_min_prescient_prefetch_ms;
405
406 /*
407 * If this percent of memory is free, don't throttle.
408 */
409 uint_t arc_lotsfree_percent = 10;
410
411 /*
412 * The arc has filled available memory and has now warmed up.
413 */
414 boolean_t arc_warm;
415
416 /*
417 * These tunables are for performance analysis.
418 */
419 uint64_t zfs_arc_max = 0;
420 uint64_t zfs_arc_min = 0;
421 static uint64_t zfs_arc_dnode_limit = 0;
422 static uint_t zfs_arc_dnode_reduce_percent = 10;
423 static uint_t zfs_arc_grow_retry = 0;
424 static uint_t zfs_arc_shrink_shift = 0;
425 uint_t zfs_arc_average_blocksize = 8 * 1024; /* 8KB */
426
427 /*
428 * ARC dirty data constraints for arc_tempreserve_space() throttle:
429 * * total dirty data limit
430 * * anon block dirty limit
431 * * each pool's anon allowance
432 */
433 static const unsigned long zfs_arc_dirty_limit_percent = 50;
434 static const unsigned long zfs_arc_anon_limit_percent = 25;
435 static const unsigned long zfs_arc_pool_dirty_percent = 20;
436
437 /*
438 * Enable or disable compressed arc buffers.
439 */
440 int zfs_compressed_arc_enabled = B_TRUE;
441
442 /*
443 * Balance between metadata and data on ghost hits. Values above 100
444 * increase metadata caching by proportionally reducing effect of ghost
445 * data hits on target data/metadata rate.
446 */
447 static uint_t zfs_arc_meta_balance = 500;
448
449 /*
450 * Percentage that can be consumed by dnodes of ARC meta buffers.
451 */
452 static uint_t zfs_arc_dnode_limit_percent = 10;
453
454 /*
455 * These tunables are Linux-specific
456 */
457 static uint64_t zfs_arc_sys_free = 0;
458 static uint_t zfs_arc_min_prefetch_ms = 0;
459 static uint_t zfs_arc_min_prescient_prefetch_ms = 0;
460 static uint_t zfs_arc_lotsfree_percent = 10;
461
462 /*
463 * Number of arc_prune threads
464 */
465 static int zfs_arc_prune_task_threads = 1;
466
467 /* The 7 states: */
468 arc_state_t ARC_anon;
469 arc_state_t ARC_mru;
470 arc_state_t ARC_mru_ghost;
471 arc_state_t ARC_mfu;
472 arc_state_t ARC_mfu_ghost;
473 arc_state_t ARC_l2c_only;
474 arc_state_t ARC_uncached;
475
476 arc_stats_t arc_stats = {
477 { "hits", KSTAT_DATA_UINT64 },
478 { "iohits", KSTAT_DATA_UINT64 },
479 { "misses", KSTAT_DATA_UINT64 },
480 { "demand_data_hits", KSTAT_DATA_UINT64 },
481 { "demand_data_iohits", KSTAT_DATA_UINT64 },
482 { "demand_data_misses", KSTAT_DATA_UINT64 },
483 { "demand_metadata_hits", KSTAT_DATA_UINT64 },
484 { "demand_metadata_iohits", KSTAT_DATA_UINT64 },
485 { "demand_metadata_misses", KSTAT_DATA_UINT64 },
486 { "prefetch_data_hits", KSTAT_DATA_UINT64 },
487 { "prefetch_data_iohits", KSTAT_DATA_UINT64 },
488 { "prefetch_data_misses", KSTAT_DATA_UINT64 },
489 { "prefetch_metadata_hits", KSTAT_DATA_UINT64 },
490 { "prefetch_metadata_iohits", KSTAT_DATA_UINT64 },
491 { "prefetch_metadata_misses", KSTAT_DATA_UINT64 },
492 { "mru_hits", KSTAT_DATA_UINT64 },
493 { "mru_ghost_hits", KSTAT_DATA_UINT64 },
494 { "mfu_hits", KSTAT_DATA_UINT64 },
495 { "mfu_ghost_hits", KSTAT_DATA_UINT64 },
496 { "uncached_hits", KSTAT_DATA_UINT64 },
497 { "deleted", KSTAT_DATA_UINT64 },
498 { "mutex_miss", KSTAT_DATA_UINT64 },
499 { "access_skip", KSTAT_DATA_UINT64 },
500 { "evict_skip", KSTAT_DATA_UINT64 },
501 { "evict_not_enough", KSTAT_DATA_UINT64 },
502 { "evict_l2_cached", KSTAT_DATA_UINT64 },
503 { "evict_l2_eligible", KSTAT_DATA_UINT64 },
504 { "evict_l2_eligible_mfu", KSTAT_DATA_UINT64 },
505 { "evict_l2_eligible_mru", KSTAT_DATA_UINT64 },
506 { "evict_l2_ineligible", KSTAT_DATA_UINT64 },
507 { "evict_l2_skip", KSTAT_DATA_UINT64 },
508 { "hash_elements", KSTAT_DATA_UINT64 },
509 { "hash_elements_max", KSTAT_DATA_UINT64 },
510 { "hash_collisions", KSTAT_DATA_UINT64 },
511 { "hash_chains", KSTAT_DATA_UINT64 },
512 { "hash_chain_max", KSTAT_DATA_UINT64 },
513 { "meta", KSTAT_DATA_UINT64 },
514 { "pd", KSTAT_DATA_UINT64 },
515 { "pm", KSTAT_DATA_UINT64 },
516 { "c", KSTAT_DATA_UINT64 },
517 { "c_min", KSTAT_DATA_UINT64 },
518 { "c_max", KSTAT_DATA_UINT64 },
519 { "size", KSTAT_DATA_UINT64 },
520 { "compressed_size", KSTAT_DATA_UINT64 },
521 { "uncompressed_size", KSTAT_DATA_UINT64 },
522 { "overhead_size", KSTAT_DATA_UINT64 },
523 { "hdr_size", KSTAT_DATA_UINT64 },
524 { "data_size", KSTAT_DATA_UINT64 },
525 { "metadata_size", KSTAT_DATA_UINT64 },
526 { "dbuf_size", KSTAT_DATA_UINT64 },
527 { "dnode_size", KSTAT_DATA_UINT64 },
528 { "bonus_size", KSTAT_DATA_UINT64 },
529 #if defined(COMPAT_FREEBSD11)
530 { "other_size", KSTAT_DATA_UINT64 },
531 #endif
532 { "anon_size", KSTAT_DATA_UINT64 },
533 { "anon_data", KSTAT_DATA_UINT64 },
534 { "anon_metadata", KSTAT_DATA_UINT64 },
535 { "anon_evictable_data", KSTAT_DATA_UINT64 },
536 { "anon_evictable_metadata", KSTAT_DATA_UINT64 },
537 { "mru_size", KSTAT_DATA_UINT64 },
538 { "mru_data", KSTAT_DATA_UINT64 },
539 { "mru_metadata", KSTAT_DATA_UINT64 },
540 { "mru_evictable_data", KSTAT_DATA_UINT64 },
541 { "mru_evictable_metadata", KSTAT_DATA_UINT64 },
542 { "mru_ghost_size", KSTAT_DATA_UINT64 },
543 { "mru_ghost_data", KSTAT_DATA_UINT64 },
544 { "mru_ghost_metadata", KSTAT_DATA_UINT64 },
545 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 },
546 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 },
547 { "mfu_size", KSTAT_DATA_UINT64 },
548 { "mfu_data", KSTAT_DATA_UINT64 },
549 { "mfu_metadata", KSTAT_DATA_UINT64 },
550 { "mfu_evictable_data", KSTAT_DATA_UINT64 },
551 { "mfu_evictable_metadata", KSTAT_DATA_UINT64 },
552 { "mfu_ghost_size", KSTAT_DATA_UINT64 },
553 { "mfu_ghost_data", KSTAT_DATA_UINT64 },
554 { "mfu_ghost_metadata", KSTAT_DATA_UINT64 },
555 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 },
556 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 },
557 { "uncached_size", KSTAT_DATA_UINT64 },
558 { "uncached_data", KSTAT_DATA_UINT64 },
559 { "uncached_metadata", KSTAT_DATA_UINT64 },
560 { "uncached_evictable_data", KSTAT_DATA_UINT64 },
561 { "uncached_evictable_metadata", KSTAT_DATA_UINT64 },
562 { "l2_hits", KSTAT_DATA_UINT64 },
563 { "l2_misses", KSTAT_DATA_UINT64 },
564 { "l2_prefetch_asize", KSTAT_DATA_UINT64 },
565 { "l2_mru_asize", KSTAT_DATA_UINT64 },
566 { "l2_mfu_asize", KSTAT_DATA_UINT64 },
567 { "l2_bufc_data_asize", KSTAT_DATA_UINT64 },
568 { "l2_bufc_metadata_asize", KSTAT_DATA_UINT64 },
569 { "l2_feeds", KSTAT_DATA_UINT64 },
570 { "l2_rw_clash", KSTAT_DATA_UINT64 },
571 { "l2_read_bytes", KSTAT_DATA_UINT64 },
572 { "l2_write_bytes", KSTAT_DATA_UINT64 },
573 { "l2_writes_sent", KSTAT_DATA_UINT64 },
574 { "l2_writes_done", KSTAT_DATA_UINT64 },
575 { "l2_writes_error", KSTAT_DATA_UINT64 },
576 { "l2_writes_lock_retry", KSTAT_DATA_UINT64 },
577 { "l2_evict_lock_retry", KSTAT_DATA_UINT64 },
578 { "l2_evict_reading", KSTAT_DATA_UINT64 },
579 { "l2_evict_l1cached", KSTAT_DATA_UINT64 },
580 { "l2_free_on_write", KSTAT_DATA_UINT64 },
581 { "l2_abort_lowmem", KSTAT_DATA_UINT64 },
582 { "l2_cksum_bad", KSTAT_DATA_UINT64 },
583 { "l2_io_error", KSTAT_DATA_UINT64 },
584 { "l2_size", KSTAT_DATA_UINT64 },
585 { "l2_asize", KSTAT_DATA_UINT64 },
586 { "l2_hdr_size", KSTAT_DATA_UINT64 },
587 { "l2_log_blk_writes", KSTAT_DATA_UINT64 },
588 { "l2_log_blk_avg_asize", KSTAT_DATA_UINT64 },
589 { "l2_log_blk_asize", KSTAT_DATA_UINT64 },
590 { "l2_log_blk_count", KSTAT_DATA_UINT64 },
591 { "l2_data_to_meta_ratio", KSTAT_DATA_UINT64 },
592 { "l2_rebuild_success", KSTAT_DATA_UINT64 },
593 { "l2_rebuild_unsupported", KSTAT_DATA_UINT64 },
594 { "l2_rebuild_io_errors", KSTAT_DATA_UINT64 },
595 { "l2_rebuild_dh_errors", KSTAT_DATA_UINT64 },
596 { "l2_rebuild_cksum_lb_errors", KSTAT_DATA_UINT64 },
597 { "l2_rebuild_lowmem", KSTAT_DATA_UINT64 },
598 { "l2_rebuild_size", KSTAT_DATA_UINT64 },
599 { "l2_rebuild_asize", KSTAT_DATA_UINT64 },
600 { "l2_rebuild_bufs", KSTAT_DATA_UINT64 },
601 { "l2_rebuild_bufs_precached", KSTAT_DATA_UINT64 },
602 { "l2_rebuild_log_blks", KSTAT_DATA_UINT64 },
603 { "memory_throttle_count", KSTAT_DATA_UINT64 },
604 { "memory_direct_count", KSTAT_DATA_UINT64 },
605 { "memory_indirect_count", KSTAT_DATA_UINT64 },
606 { "memory_all_bytes", KSTAT_DATA_UINT64 },
607 { "memory_free_bytes", KSTAT_DATA_UINT64 },
608 { "memory_available_bytes", KSTAT_DATA_INT64 },
609 { "arc_no_grow", KSTAT_DATA_UINT64 },
610 { "arc_tempreserve", KSTAT_DATA_UINT64 },
611 { "arc_loaned_bytes", KSTAT_DATA_UINT64 },
612 { "arc_prune", KSTAT_DATA_UINT64 },
613 { "arc_meta_used", KSTAT_DATA_UINT64 },
614 { "arc_dnode_limit", KSTAT_DATA_UINT64 },
615 { "async_upgrade_sync", KSTAT_DATA_UINT64 },
616 { "predictive_prefetch", KSTAT_DATA_UINT64 },
617 { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 },
618 { "demand_iohit_predictive_prefetch", KSTAT_DATA_UINT64 },
619 { "prescient_prefetch", KSTAT_DATA_UINT64 },
620 { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64 },
621 { "demand_iohit_prescient_prefetch", KSTAT_DATA_UINT64 },
622 { "arc_need_free", KSTAT_DATA_UINT64 },
623 { "arc_sys_free", KSTAT_DATA_UINT64 },
624 { "arc_raw_size", KSTAT_DATA_UINT64 },
625 { "cached_only_in_progress", KSTAT_DATA_UINT64 },
626 { "abd_chunk_waste_size", KSTAT_DATA_UINT64 },
627 };
628
629 arc_sums_t arc_sums;
630
631 #define ARCSTAT_MAX(stat, val) { \
632 uint64_t m; \
633 while ((val) > (m = arc_stats.stat.value.ui64) && \
634 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
635 continue; \
636 }
637
638 /*
639 * We define a macro to allow ARC hits/misses to be easily broken down by
640 * two separate conditions, giving a total of four different subtypes for
641 * each of hits and misses (so eight statistics total).
642 */
643 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
644 if (cond1) { \
645 if (cond2) { \
646 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
647 } else { \
648 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
649 } \
650 } else { \
651 if (cond2) { \
652 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
653 } else { \
654 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
655 } \
656 }
657
658 /*
659 * This macro allows us to use kstats as floating averages. Each time we
660 * update this kstat, we first factor it and the update value by
661 * ARCSTAT_AVG_FACTOR to shrink the new value's contribution to the overall
662 * average. This macro assumes that integer loads and stores are atomic, but
663 * is not safe for multiple writers updating the kstat in parallel (only the
664 * last writer's update will remain).
665 */
666 #define ARCSTAT_F_AVG_FACTOR 3
667 #define ARCSTAT_F_AVG(stat, value) \
668 do { \
669 uint64_t x = ARCSTAT(stat); \
670 x = x - x / ARCSTAT_F_AVG_FACTOR + \
671 (value) / ARCSTAT_F_AVG_FACTOR; \
672 ARCSTAT(stat) = x; \
673 } while (0)
674
675 static kstat_t *arc_ksp;
676
677 /*
678 * There are several ARC variables that are critical to export as kstats --
679 * but we don't want to have to grovel around in the kstat whenever we wish to
680 * manipulate them. For these variables, we therefore define them to be in
681 * terms of the statistic variable. This assures that we are not introducing
682 * the possibility of inconsistency by having shadow copies of the variables,
683 * while still allowing the code to be readable.
684 */
685 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
686 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
687 #define arc_dnode_limit ARCSTAT(arcstat_dnode_limit) /* max size for dnodes */
688 #define arc_need_free ARCSTAT(arcstat_need_free) /* waiting to be evicted */
689
690 hrtime_t arc_growtime;
691 list_t arc_prune_list;
692 kmutex_t arc_prune_mtx;
693 taskq_t *arc_prune_taskq;
694
695 #define GHOST_STATE(state) \
696 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
697 (state) == arc_l2c_only)
698
699 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
700 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
701 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR)
702 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH)
703 #define HDR_PRESCIENT_PREFETCH(hdr) \
704 ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH)
705 #define HDR_COMPRESSION_ENABLED(hdr) \
706 ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC)
707
708 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE)
709 #define HDR_UNCACHED(hdr) ((hdr)->b_flags & ARC_FLAG_UNCACHED)
710 #define HDR_L2_READING(hdr) \
711 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \
712 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
713 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING)
714 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
715 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
716 #define HDR_PROTECTED(hdr) ((hdr)->b_flags & ARC_FLAG_PROTECTED)
717 #define HDR_NOAUTH(hdr) ((hdr)->b_flags & ARC_FLAG_NOAUTH)
718 #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA)
719
720 #define HDR_ISTYPE_METADATA(hdr) \
721 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
722 #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr))
723
724 #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
725 #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
726 #define HDR_HAS_RABD(hdr) \
727 (HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) && \
728 (hdr)->b_crypt_hdr.b_rabd != NULL)
729 #define HDR_ENCRYPTED(hdr) \
730 (HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
731 #define HDR_AUTHENTICATED(hdr) \
732 (HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
733
734 /* For storing compression mode in b_flags */
735 #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1)
736
737 #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \
738 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS))
739 #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \
740 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp));
741
742 #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL)
743 #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED)
744 #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED)
745 #define ARC_BUF_ENCRYPTED(buf) ((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED)
746
747 /*
748 * Other sizes
749 */
750
751 #define HDR_FULL_CRYPT_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
752 #define HDR_FULL_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_crypt_hdr))
753 #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
754
755 /*
756 * Hash table routines
757 */
758
759 #define BUF_LOCKS 2048
760 typedef struct buf_hash_table {
761 uint64_t ht_mask;
762 arc_buf_hdr_t **ht_table;
763 kmutex_t ht_locks[BUF_LOCKS] ____cacheline_aligned;
764 } buf_hash_table_t;
765
766 static buf_hash_table_t buf_hash_table;
767
768 #define BUF_HASH_INDEX(spa, dva, birth) \
769 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
770 #define BUF_HASH_LOCK(idx) (&buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
771 #define HDR_LOCK(hdr) \
772 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
773
774 uint64_t zfs_crc64_table[256];
775
776 /*
777 * Level 2 ARC
778 */
779
780 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
781 #define L2ARC_HEADROOM 2 /* num of writes */
782
783 /*
784 * If we discover during ARC scan any buffers to be compressed, we boost
785 * our headroom for the next scanning cycle by this percentage multiple.
786 */
787 #define L2ARC_HEADROOM_BOOST 200
788 #define L2ARC_FEED_SECS 1 /* caching interval secs */
789 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
790
791 /*
792 * We can feed L2ARC from two states of ARC buffers, mru and mfu,
793 * and each of the state has two types: data and metadata.
794 */
795 #define L2ARC_FEED_TYPES 4
796
797 /* L2ARC Performance Tunables */
798 uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */
799 uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */
800 uint64_t l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */
801 uint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST;
802 uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */
803 uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */
804 int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */
805 int l2arc_feed_again = B_TRUE; /* turbo warmup */
806 int l2arc_norw = B_FALSE; /* no reads during writes */
807 static uint_t l2arc_meta_percent = 33; /* limit on headers size */
808
809 /*
810 * L2ARC Internals
811 */
812 static list_t L2ARC_dev_list; /* device list */
813 static list_t *l2arc_dev_list; /* device list pointer */
814 static kmutex_t l2arc_dev_mtx; /* device list mutex */
815 static l2arc_dev_t *l2arc_dev_last; /* last device used */
816 static list_t L2ARC_free_on_write; /* free after write buf list */
817 static list_t *l2arc_free_on_write; /* free after write list ptr */
818 static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */
819 static uint64_t l2arc_ndev; /* number of devices */
820
821 typedef struct l2arc_read_callback {
822 arc_buf_hdr_t *l2rcb_hdr; /* read header */
823 blkptr_t l2rcb_bp; /* original blkptr */
824 zbookmark_phys_t l2rcb_zb; /* original bookmark */
825 int l2rcb_flags; /* original flags */
826 abd_t *l2rcb_abd; /* temporary buffer */
827 } l2arc_read_callback_t;
828
829 typedef struct l2arc_data_free {
830 /* protected by l2arc_free_on_write_mtx */
831 abd_t *l2df_abd;
832 size_t l2df_size;
833 arc_buf_contents_t l2df_type;
834 list_node_t l2df_list_node;
835 } l2arc_data_free_t;
836
837 typedef enum arc_fill_flags {
838 ARC_FILL_LOCKED = 1 << 0, /* hdr lock is held */
839 ARC_FILL_COMPRESSED = 1 << 1, /* fill with compressed data */
840 ARC_FILL_ENCRYPTED = 1 << 2, /* fill with encrypted data */
841 ARC_FILL_NOAUTH = 1 << 3, /* don't attempt to authenticate */
842 ARC_FILL_IN_PLACE = 1 << 4 /* fill in place (special case) */
843 } arc_fill_flags_t;
844
845 typedef enum arc_ovf_level {
846 ARC_OVF_NONE, /* ARC within target size. */
847 ARC_OVF_SOME, /* ARC is slightly overflowed. */
848 ARC_OVF_SEVERE /* ARC is severely overflowed. */
849 } arc_ovf_level_t;
850
851 static kmutex_t l2arc_feed_thr_lock;
852 static kcondvar_t l2arc_feed_thr_cv;
853 static uint8_t l2arc_thread_exit;
854
855 static kmutex_t l2arc_rebuild_thr_lock;
856 static kcondvar_t l2arc_rebuild_thr_cv;
857
858 enum arc_hdr_alloc_flags {
859 ARC_HDR_ALLOC_RDATA = 0x1,
860 ARC_HDR_USE_RESERVE = 0x4,
861 ARC_HDR_ALLOC_LINEAR = 0x8,
862 };
863
864
865 static abd_t *arc_get_data_abd(arc_buf_hdr_t *, uint64_t, const void *, int);
866 static void *arc_get_data_buf(arc_buf_hdr_t *, uint64_t, const void *);
867 static void arc_get_data_impl(arc_buf_hdr_t *, uint64_t, const void *, int);
868 static void arc_free_data_abd(arc_buf_hdr_t *, abd_t *, uint64_t, const void *);
869 static void arc_free_data_buf(arc_buf_hdr_t *, void *, uint64_t, const void *);
870 static void arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size,
871 const void *tag);
872 static void arc_hdr_free_abd(arc_buf_hdr_t *, boolean_t);
873 static void arc_hdr_alloc_abd(arc_buf_hdr_t *, int);
874 static void arc_hdr_destroy(arc_buf_hdr_t *);
875 static void arc_access(arc_buf_hdr_t *, arc_flags_t, boolean_t);
876 static void arc_buf_watch(arc_buf_t *);
877 static void arc_change_state(arc_state_t *, arc_buf_hdr_t *);
878
879 static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *);
880 static uint32_t arc_bufc_to_flags(arc_buf_contents_t);
881 static inline void arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags);
882 static inline void arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags);
883
884 static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *);
885 static void l2arc_read_done(zio_t *);
886 static void l2arc_do_free_on_write(void);
887 static void l2arc_hdr_arcstats_update(arc_buf_hdr_t *hdr, boolean_t incr,
888 boolean_t state_only);
889
890 #define l2arc_hdr_arcstats_increment(hdr) \
891 l2arc_hdr_arcstats_update((hdr), B_TRUE, B_FALSE)
892 #define l2arc_hdr_arcstats_decrement(hdr) \
893 l2arc_hdr_arcstats_update((hdr), B_FALSE, B_FALSE)
894 #define l2arc_hdr_arcstats_increment_state(hdr) \
895 l2arc_hdr_arcstats_update((hdr), B_TRUE, B_TRUE)
896 #define l2arc_hdr_arcstats_decrement_state(hdr) \
897 l2arc_hdr_arcstats_update((hdr), B_FALSE, B_TRUE)
898
899 /*
900 * l2arc_exclude_special : A zfs module parameter that controls whether buffers
901 * present on special vdevs are eligibile for caching in L2ARC. If
902 * set to 1, exclude dbufs on special vdevs from being cached to
903 * L2ARC.
904 */
905 int l2arc_exclude_special = 0;
906
907 /*
908 * l2arc_mfuonly : A ZFS module parameter that controls whether only MFU
909 * metadata and data are cached from ARC into L2ARC.
910 */
911 static int l2arc_mfuonly = 0;
912
913 /*
914 * L2ARC TRIM
915 * l2arc_trim_ahead : A ZFS module parameter that controls how much ahead of
916 * the current write size (l2arc_write_max) we should TRIM if we
917 * have filled the device. It is defined as a percentage of the
918 * write size. If set to 100 we trim twice the space required to
919 * accommodate upcoming writes. A minimum of 64MB will be trimmed.
920 * It also enables TRIM of the whole L2ARC device upon creation or
921 * addition to an existing pool or if the header of the device is
922 * invalid upon importing a pool or onlining a cache device. The
923 * default is 0, which disables TRIM on L2ARC altogether as it can
924 * put significant stress on the underlying storage devices. This
925 * will vary depending of how well the specific device handles
926 * these commands.
927 */
928 static uint64_t l2arc_trim_ahead = 0;
929
930 /*
931 * Performance tuning of L2ARC persistence:
932 *
933 * l2arc_rebuild_enabled : A ZFS module parameter that controls whether adding
934 * an L2ARC device (either at pool import or later) will attempt
935 * to rebuild L2ARC buffer contents.
936 * l2arc_rebuild_blocks_min_l2size : A ZFS module parameter that controls
937 * whether log blocks are written to the L2ARC device. If the L2ARC
938 * device is less than 1GB, the amount of data l2arc_evict()
939 * evicts is significant compared to the amount of restored L2ARC
940 * data. In this case do not write log blocks in L2ARC in order
941 * not to waste space.
942 */
943 static int l2arc_rebuild_enabled = B_TRUE;
944 static uint64_t l2arc_rebuild_blocks_min_l2size = 1024 * 1024 * 1024;
945
946 /* L2ARC persistence rebuild control routines. */
947 void l2arc_rebuild_vdev(vdev_t *vd, boolean_t reopen);
948 static __attribute__((noreturn)) void l2arc_dev_rebuild_thread(void *arg);
949 static int l2arc_rebuild(l2arc_dev_t *dev);
950
951 /* L2ARC persistence read I/O routines. */
952 static int l2arc_dev_hdr_read(l2arc_dev_t *dev);
953 static int l2arc_log_blk_read(l2arc_dev_t *dev,
954 const l2arc_log_blkptr_t *this_lp, const l2arc_log_blkptr_t *next_lp,
955 l2arc_log_blk_phys_t *this_lb, l2arc_log_blk_phys_t *next_lb,
956 zio_t *this_io, zio_t **next_io);
957 static zio_t *l2arc_log_blk_fetch(vdev_t *vd,
958 const l2arc_log_blkptr_t *lp, l2arc_log_blk_phys_t *lb);
959 static void l2arc_log_blk_fetch_abort(zio_t *zio);
960
961 /* L2ARC persistence block restoration routines. */
962 static void l2arc_log_blk_restore(l2arc_dev_t *dev,
963 const l2arc_log_blk_phys_t *lb, uint64_t lb_asize);
964 static void l2arc_hdr_restore(const l2arc_log_ent_phys_t *le,
965 l2arc_dev_t *dev);
966
967 /* L2ARC persistence write I/O routines. */
968 static uint64_t l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio,
969 l2arc_write_callback_t *cb);
970
971 /* L2ARC persistence auxiliary routines. */
972 boolean_t l2arc_log_blkptr_valid(l2arc_dev_t *dev,
973 const l2arc_log_blkptr_t *lbp);
974 static boolean_t l2arc_log_blk_insert(l2arc_dev_t *dev,
975 const arc_buf_hdr_t *ab);
976 boolean_t l2arc_range_check_overlap(uint64_t bottom,
977 uint64_t top, uint64_t check);
978 static void l2arc_blk_fetch_done(zio_t *zio);
979 static inline uint64_t
980 l2arc_log_blk_overhead(uint64_t write_sz, l2arc_dev_t *dev);
981
982 /*
983 * We use Cityhash for this. It's fast, and has good hash properties without
984 * requiring any large static buffers.
985 */
986 static uint64_t
987 buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
988 {
989 return (cityhash4(spa, dva->dva_word[0], dva->dva_word[1], birth));
990 }
991
992 #define HDR_EMPTY(hdr) \
993 ((hdr)->b_dva.dva_word[0] == 0 && \
994 (hdr)->b_dva.dva_word[1] == 0)
995
996 #define HDR_EMPTY_OR_LOCKED(hdr) \
997 (HDR_EMPTY(hdr) || MUTEX_HELD(HDR_LOCK(hdr)))
998
999 #define HDR_EQUAL(spa, dva, birth, hdr) \
1000 ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
1001 ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
1002 ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa)
1003
1004 static void
1005 buf_discard_identity(arc_buf_hdr_t *hdr)
1006 {
1007 hdr->b_dva.dva_word[0] = 0;
1008 hdr->b_dva.dva_word[1] = 0;
1009 hdr->b_birth = 0;
1010 }
1011
1012 static arc_buf_hdr_t *
1013 buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp)
1014 {
1015 const dva_t *dva = BP_IDENTITY(bp);
1016 uint64_t birth = BP_PHYSICAL_BIRTH(bp);
1017 uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
1018 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
1019 arc_buf_hdr_t *hdr;
1020
1021 mutex_enter(hash_lock);
1022 for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL;
1023 hdr = hdr->b_hash_next) {
1024 if (HDR_EQUAL(spa, dva, birth, hdr)) {
1025 *lockp = hash_lock;
1026 return (hdr);
1027 }
1028 }
1029 mutex_exit(hash_lock);
1030 *lockp = NULL;
1031 return (NULL);
1032 }
1033
1034 /*
1035 * Insert an entry into the hash table. If there is already an element
1036 * equal to elem in the hash table, then the already existing element
1037 * will be returned and the new element will not be inserted.
1038 * Otherwise returns NULL.
1039 * If lockp == NULL, the caller is assumed to already hold the hash lock.
1040 */
1041 static arc_buf_hdr_t *
1042 buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp)
1043 {
1044 uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth);
1045 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
1046 arc_buf_hdr_t *fhdr;
1047 uint32_t i;
1048
1049 ASSERT(!DVA_IS_EMPTY(&hdr->b_dva));
1050 ASSERT(hdr->b_birth != 0);
1051 ASSERT(!HDR_IN_HASH_TABLE(hdr));
1052
1053 if (lockp != NULL) {
1054 *lockp = hash_lock;
1055 mutex_enter(hash_lock);
1056 } else {
1057 ASSERT(MUTEX_HELD(hash_lock));
1058 }
1059
1060 for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL;
1061 fhdr = fhdr->b_hash_next, i++) {
1062 if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr))
1063 return (fhdr);
1064 }
1065
1066 hdr->b_hash_next = buf_hash_table.ht_table[idx];
1067 buf_hash_table.ht_table[idx] = hdr;
1068 arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE);
1069
1070 /* collect some hash table performance data */
1071 if (i > 0) {
1072 ARCSTAT_BUMP(arcstat_hash_collisions);
1073 if (i == 1)
1074 ARCSTAT_BUMP(arcstat_hash_chains);
1075
1076 ARCSTAT_MAX(arcstat_hash_chain_max, i);
1077 }
1078 uint64_t he = atomic_inc_64_nv(
1079 &arc_stats.arcstat_hash_elements.value.ui64);
1080 ARCSTAT_MAX(arcstat_hash_elements_max, he);
1081
1082 return (NULL);
1083 }
1084
1085 static void
1086 buf_hash_remove(arc_buf_hdr_t *hdr)
1087 {
1088 arc_buf_hdr_t *fhdr, **hdrp;
1089 uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth);
1090
1091 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
1092 ASSERT(HDR_IN_HASH_TABLE(hdr));
1093
1094 hdrp = &buf_hash_table.ht_table[idx];
1095 while ((fhdr = *hdrp) != hdr) {
1096 ASSERT3P(fhdr, !=, NULL);
1097 hdrp = &fhdr->b_hash_next;
1098 }
1099 *hdrp = hdr->b_hash_next;
1100 hdr->b_hash_next = NULL;
1101 arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE);
1102
1103 /* collect some hash table performance data */
1104 atomic_dec_64(&arc_stats.arcstat_hash_elements.value.ui64);
1105
1106 if (buf_hash_table.ht_table[idx] &&
1107 buf_hash_table.ht_table[idx]->b_hash_next == NULL)
1108 ARCSTAT_BUMPDOWN(arcstat_hash_chains);
1109 }
1110
1111 /*
1112 * Global data structures and functions for the buf kmem cache.
1113 */
1114
1115 static kmem_cache_t *hdr_full_cache;
1116 static kmem_cache_t *hdr_full_crypt_cache;
1117 static kmem_cache_t *hdr_l2only_cache;
1118 static kmem_cache_t *buf_cache;
1119
1120 static void
1121 buf_fini(void)
1122 {
1123 #if defined(_KERNEL)
1124 /*
1125 * Large allocations which do not require contiguous pages
1126 * should be using vmem_free() in the linux kernel\
1127 */
1128 vmem_free(buf_hash_table.ht_table,
1129 (buf_hash_table.ht_mask + 1) * sizeof (void *));
1130 #else
1131 kmem_free(buf_hash_table.ht_table,
1132 (buf_hash_table.ht_mask + 1) * sizeof (void *));
1133 #endif
1134 for (int i = 0; i < BUF_LOCKS; i++)
1135 mutex_destroy(BUF_HASH_LOCK(i));
1136 kmem_cache_destroy(hdr_full_cache);
1137 kmem_cache_destroy(hdr_full_crypt_cache);
1138 kmem_cache_destroy(hdr_l2only_cache);
1139 kmem_cache_destroy(buf_cache);
1140 }
1141
1142 /*
1143 * Constructor callback - called when the cache is empty
1144 * and a new buf is requested.
1145 */
1146 static int
1147 hdr_full_cons(void *vbuf, void *unused, int kmflag)
1148 {
1149 (void) unused, (void) kmflag;
1150 arc_buf_hdr_t *hdr = vbuf;
1151
1152 memset(hdr, 0, HDR_FULL_SIZE);
1153 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS;
1154 zfs_refcount_create(&hdr->b_l1hdr.b_refcnt);
1155 #ifdef ZFS_DEBUG
1156 mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
1157 #endif
1158 multilist_link_init(&hdr->b_l1hdr.b_arc_node);
1159 list_link_init(&hdr->b_l2hdr.b_l2node);
1160 arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS);
1161
1162 return (0);
1163 }
1164
1165 static int
1166 hdr_full_crypt_cons(void *vbuf, void *unused, int kmflag)
1167 {
1168 (void) unused;
1169 arc_buf_hdr_t *hdr = vbuf;
1170
1171 hdr_full_cons(vbuf, unused, kmflag);
1172 memset(&hdr->b_crypt_hdr, 0, sizeof (hdr->b_crypt_hdr));
1173 arc_space_consume(sizeof (hdr->b_crypt_hdr), ARC_SPACE_HDRS);
1174
1175 return (0);
1176 }
1177
1178 static int
1179 hdr_l2only_cons(void *vbuf, void *unused, int kmflag)
1180 {
1181 (void) unused, (void) kmflag;
1182 arc_buf_hdr_t *hdr = vbuf;
1183
1184 memset(hdr, 0, HDR_L2ONLY_SIZE);
1185 arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS);
1186
1187 return (0);
1188 }
1189
1190 static int
1191 buf_cons(void *vbuf, void *unused, int kmflag)
1192 {
1193 (void) unused, (void) kmflag;
1194 arc_buf_t *buf = vbuf;
1195
1196 memset(buf, 0, sizeof (arc_buf_t));
1197 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
1198
1199 return (0);
1200 }
1201
1202 /*
1203 * Destructor callback - called when a cached buf is
1204 * no longer required.
1205 */
1206 static void
1207 hdr_full_dest(void *vbuf, void *unused)
1208 {
1209 (void) unused;
1210 arc_buf_hdr_t *hdr = vbuf;
1211
1212 ASSERT(HDR_EMPTY(hdr));
1213 zfs_refcount_destroy(&hdr->b_l1hdr.b_refcnt);
1214 #ifdef ZFS_DEBUG
1215 mutex_destroy(&hdr->b_l1hdr.b_freeze_lock);
1216 #endif
1217 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
1218 arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS);
1219 }
1220
1221 static void
1222 hdr_full_crypt_dest(void *vbuf, void *unused)
1223 {
1224 (void) vbuf, (void) unused;
1225
1226 hdr_full_dest(vbuf, unused);
1227 arc_space_return(sizeof (((arc_buf_hdr_t *)NULL)->b_crypt_hdr),
1228 ARC_SPACE_HDRS);
1229 }
1230
1231 static void
1232 hdr_l2only_dest(void *vbuf, void *unused)
1233 {
1234 (void) unused;
1235 arc_buf_hdr_t *hdr = vbuf;
1236
1237 ASSERT(HDR_EMPTY(hdr));
1238 arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS);
1239 }
1240
1241 static void
1242 buf_dest(void *vbuf, void *unused)
1243 {
1244 (void) unused;
1245 (void) vbuf;
1246
1247 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
1248 }
1249
1250 static void
1251 buf_init(void)
1252 {
1253 uint64_t *ct = NULL;
1254 uint64_t hsize = 1ULL << 12;
1255 int i, j;
1256
1257 /*
1258 * The hash table is big enough to fill all of physical memory
1259 * with an average block size of zfs_arc_average_blocksize (default 8K).
1260 * By default, the table will take up
1261 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
1262 */
1263 while (hsize * zfs_arc_average_blocksize < arc_all_memory())
1264 hsize <<= 1;
1265 retry:
1266 buf_hash_table.ht_mask = hsize - 1;
1267 #if defined(_KERNEL)
1268 /*
1269 * Large allocations which do not require contiguous pages
1270 * should be using vmem_alloc() in the linux kernel
1271 */
1272 buf_hash_table.ht_table =
1273 vmem_zalloc(hsize * sizeof (void*), KM_SLEEP);
1274 #else
1275 buf_hash_table.ht_table =
1276 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
1277 #endif
1278 if (buf_hash_table.ht_table == NULL) {
1279 ASSERT(hsize > (1ULL << 8));
1280 hsize >>= 1;
1281 goto retry;
1282 }
1283
1284 hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE,
1285 0, hdr_full_cons, hdr_full_dest, NULL, NULL, NULL, 0);
1286 hdr_full_crypt_cache = kmem_cache_create("arc_buf_hdr_t_full_crypt",
1287 HDR_FULL_CRYPT_SIZE, 0, hdr_full_crypt_cons, hdr_full_crypt_dest,
1288 NULL, NULL, NULL, 0);
1289 hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only",
1290 HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, NULL,
1291 NULL, NULL, 0);
1292 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
1293 0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
1294
1295 for (i = 0; i < 256; i++)
1296 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
1297 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
1298
1299 for (i = 0; i < BUF_LOCKS; i++)
1300 mutex_init(BUF_HASH_LOCK(i), NULL, MUTEX_DEFAULT, NULL);
1301 }
1302
1303 #define ARC_MINTIME (hz>>4) /* 62 ms */
1304
1305 /*
1306 * This is the size that the buf occupies in memory. If the buf is compressed,
1307 * it will correspond to the compressed size. You should use this method of
1308 * getting the buf size unless you explicitly need the logical size.
1309 */
1310 uint64_t
1311 arc_buf_size(arc_buf_t *buf)
1312 {
1313 return (ARC_BUF_COMPRESSED(buf) ?
1314 HDR_GET_PSIZE(buf->b_hdr) : HDR_GET_LSIZE(buf->b_hdr));
1315 }
1316
1317 uint64_t
1318 arc_buf_lsize(arc_buf_t *buf)
1319 {
1320 return (HDR_GET_LSIZE(buf->b_hdr));
1321 }
1322
1323 /*
1324 * This function will return B_TRUE if the buffer is encrypted in memory.
1325 * This buffer can be decrypted by calling arc_untransform().
1326 */
1327 boolean_t
1328 arc_is_encrypted(arc_buf_t *buf)
1329 {
1330 return (ARC_BUF_ENCRYPTED(buf) != 0);
1331 }
1332
1333 /*
1334 * Returns B_TRUE if the buffer represents data that has not had its MAC
1335 * verified yet.
1336 */
1337 boolean_t
1338 arc_is_unauthenticated(arc_buf_t *buf)
1339 {
1340 return (HDR_NOAUTH(buf->b_hdr) != 0);
1341 }
1342
1343 void
1344 arc_get_raw_params(arc_buf_t *buf, boolean_t *byteorder, uint8_t *salt,
1345 uint8_t *iv, uint8_t *mac)
1346 {
1347 arc_buf_hdr_t *hdr = buf->b_hdr;
1348
1349 ASSERT(HDR_PROTECTED(hdr));
1350
1351 memcpy(salt, hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN);
1352 memcpy(iv, hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN);
1353 memcpy(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN);
1354 *byteorder = (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ?
1355 ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER;
1356 }
1357
1358 /*
1359 * Indicates how this buffer is compressed in memory. If it is not compressed
1360 * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with
1361 * arc_untransform() as long as it is also unencrypted.
1362 */
1363 enum zio_compress
1364 arc_get_compression(arc_buf_t *buf)
1365 {
1366 return (ARC_BUF_COMPRESSED(buf) ?
1367 HDR_GET_COMPRESS(buf->b_hdr) : ZIO_COMPRESS_OFF);
1368 }
1369
1370 /*
1371 * Return the compression algorithm used to store this data in the ARC. If ARC
1372 * compression is enabled or this is an encrypted block, this will be the same
1373 * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF.
1374 */
1375 static inline enum zio_compress
1376 arc_hdr_get_compress(arc_buf_hdr_t *hdr)
1377 {
1378 return (HDR_COMPRESSION_ENABLED(hdr) ?
1379 HDR_GET_COMPRESS(hdr) : ZIO_COMPRESS_OFF);
1380 }
1381
1382 uint8_t
1383 arc_get_complevel(arc_buf_t *buf)
1384 {
1385 return (buf->b_hdr->b_complevel);
1386 }
1387
1388 static inline boolean_t
1389 arc_buf_is_shared(arc_buf_t *buf)
1390 {
1391 boolean_t shared = (buf->b_data != NULL &&
1392 buf->b_hdr->b_l1hdr.b_pabd != NULL &&
1393 abd_is_linear(buf->b_hdr->b_l1hdr.b_pabd) &&
1394 buf->b_data == abd_to_buf(buf->b_hdr->b_l1hdr.b_pabd));
1395 IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr));
1396 IMPLY(shared, ARC_BUF_SHARED(buf));
1397 IMPLY(shared, ARC_BUF_COMPRESSED(buf) || ARC_BUF_LAST(buf));
1398
1399 /*
1400 * It would be nice to assert arc_can_share() too, but the "hdr isn't
1401 * already being shared" requirement prevents us from doing that.
1402 */
1403
1404 return (shared);
1405 }
1406
1407 /*
1408 * Free the checksum associated with this header. If there is no checksum, this
1409 * is a no-op.
1410 */
1411 static inline void
1412 arc_cksum_free(arc_buf_hdr_t *hdr)
1413 {
1414 #ifdef ZFS_DEBUG
1415 ASSERT(HDR_HAS_L1HDR(hdr));
1416
1417 mutex_enter(&hdr->b_l1hdr.b_freeze_lock);
1418 if (hdr->b_l1hdr.b_freeze_cksum != NULL) {
1419 kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t));
1420 hdr->b_l1hdr.b_freeze_cksum = NULL;
1421 }
1422 mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
1423 #endif
1424 }
1425
1426 /*
1427 * Return true iff at least one of the bufs on hdr is not compressed.
1428 * Encrypted buffers count as compressed.
1429 */
1430 static boolean_t
1431 arc_hdr_has_uncompressed_buf(arc_buf_hdr_t *hdr)
1432 {
1433 ASSERT(hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY_OR_LOCKED(hdr));
1434
1435 for (arc_buf_t *b = hdr->b_l1hdr.b_buf; b != NULL; b = b->b_next) {
1436 if (!ARC_BUF_COMPRESSED(b)) {
1437 return (B_TRUE);
1438 }
1439 }
1440 return (B_FALSE);
1441 }
1442
1443
1444 /*
1445 * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data
1446 * matches the checksum that is stored in the hdr. If there is no checksum,
1447 * or if the buf is compressed, this is a no-op.
1448 */
1449 static void
1450 arc_cksum_verify(arc_buf_t *buf)
1451 {
1452 #ifdef ZFS_DEBUG
1453 arc_buf_hdr_t *hdr = buf->b_hdr;
1454 zio_cksum_t zc;
1455
1456 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1457 return;
1458
1459 if (ARC_BUF_COMPRESSED(buf))
1460 return;
1461
1462 ASSERT(HDR_HAS_L1HDR(hdr));
1463
1464 mutex_enter(&hdr->b_l1hdr.b_freeze_lock);
1465
1466 if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) {
1467 mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
1468 return;
1469 }
1470
1471 fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, &zc);
1472 if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc))
1473 panic("buffer modified while frozen!");
1474 mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
1475 #endif
1476 }
1477
1478 /*
1479 * This function makes the assumption that data stored in the L2ARC
1480 * will be transformed exactly as it is in the main pool. Because of
1481 * this we can verify the checksum against the reading process's bp.
1482 */
1483 static boolean_t
1484 arc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio)
1485 {
1486 ASSERT(!BP_IS_EMBEDDED(zio->io_bp));
1487 VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr));
1488
1489 /*
1490 * Block pointers always store the checksum for the logical data.
1491 * If the block pointer has the gang bit set, then the checksum
1492 * it represents is for the reconstituted data and not for an
1493 * individual gang member. The zio pipeline, however, must be able to
1494 * determine the checksum of each of the gang constituents so it
1495 * treats the checksum comparison differently than what we need
1496 * for l2arc blocks. This prevents us from using the
1497 * zio_checksum_error() interface directly. Instead we must call the
1498 * zio_checksum_error_impl() so that we can ensure the checksum is
1499 * generated using the correct checksum algorithm and accounts for the
1500 * logical I/O size and not just a gang fragment.
1501 */
1502 return (zio_checksum_error_impl(zio->io_spa, zio->io_bp,
1503 BP_GET_CHECKSUM(zio->io_bp), zio->io_abd, zio->io_size,
1504 zio->io_offset, NULL) == 0);
1505 }
1506
1507 /*
1508 * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a
1509 * checksum and attaches it to the buf's hdr so that we can ensure that the buf
1510 * isn't modified later on. If buf is compressed or there is already a checksum
1511 * on the hdr, this is a no-op (we only checksum uncompressed bufs).
1512 */
1513 static void
1514 arc_cksum_compute(arc_buf_t *buf)
1515 {
1516 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1517 return;
1518
1519 #ifdef ZFS_DEBUG
1520 arc_buf_hdr_t *hdr = buf->b_hdr;
1521 ASSERT(HDR_HAS_L1HDR(hdr));
1522 mutex_enter(&hdr->b_l1hdr.b_freeze_lock);
1523 if (hdr->b_l1hdr.b_freeze_cksum != NULL || ARC_BUF_COMPRESSED(buf)) {
1524 mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
1525 return;
1526 }
1527
1528 ASSERT(!ARC_BUF_ENCRYPTED(buf));
1529 ASSERT(!ARC_BUF_COMPRESSED(buf));
1530 hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t),
1531 KM_SLEEP);
1532 fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL,
1533 hdr->b_l1hdr.b_freeze_cksum);
1534 mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
1535 #endif
1536 arc_buf_watch(buf);
1537 }
1538
1539 #ifndef _KERNEL
1540 void
1541 arc_buf_sigsegv(int sig, siginfo_t *si, void *unused)
1542 {
1543 (void) sig, (void) unused;
1544 panic("Got SIGSEGV at address: 0x%lx\n", (long)si->si_addr);
1545 }
1546 #endif
1547
1548 static void
1549 arc_buf_unwatch(arc_buf_t *buf)
1550 {
1551 #ifndef _KERNEL
1552 if (arc_watch) {
1553 ASSERT0(mprotect(buf->b_data, arc_buf_size(buf),
1554 PROT_READ | PROT_WRITE));
1555 }
1556 #else
1557 (void) buf;
1558 #endif
1559 }
1560
1561 static void
1562 arc_buf_watch(arc_buf_t *buf)
1563 {
1564 #ifndef _KERNEL
1565 if (arc_watch)
1566 ASSERT0(mprotect(buf->b_data, arc_buf_size(buf),
1567 PROT_READ));
1568 #else
1569 (void) buf;
1570 #endif
1571 }
1572
1573 static arc_buf_contents_t
1574 arc_buf_type(arc_buf_hdr_t *hdr)
1575 {
1576 arc_buf_contents_t type;
1577 if (HDR_ISTYPE_METADATA(hdr)) {
1578 type = ARC_BUFC_METADATA;
1579 } else {
1580 type = ARC_BUFC_DATA;
1581 }
1582 VERIFY3U(hdr->b_type, ==, type);
1583 return (type);
1584 }
1585
1586 boolean_t
1587 arc_is_metadata(arc_buf_t *buf)
1588 {
1589 return (HDR_ISTYPE_METADATA(buf->b_hdr) != 0);
1590 }
1591
1592 static uint32_t
1593 arc_bufc_to_flags(arc_buf_contents_t type)
1594 {
1595 switch (type) {
1596 case ARC_BUFC_DATA:
1597 /* metadata field is 0 if buffer contains normal data */
1598 return (0);
1599 case ARC_BUFC_METADATA:
1600 return (ARC_FLAG_BUFC_METADATA);
1601 default:
1602 break;
1603 }
1604 panic("undefined ARC buffer type!");
1605 return ((uint32_t)-1);
1606 }
1607
1608 void
1609 arc_buf_thaw(arc_buf_t *buf)
1610 {
1611 arc_buf_hdr_t *hdr = buf->b_hdr;
1612
1613 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon);
1614 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1615
1616 arc_cksum_verify(buf);
1617
1618 /*
1619 * Compressed buffers do not manipulate the b_freeze_cksum.
1620 */
1621 if (ARC_BUF_COMPRESSED(buf))
1622 return;
1623
1624 ASSERT(HDR_HAS_L1HDR(hdr));
1625 arc_cksum_free(hdr);
1626 arc_buf_unwatch(buf);
1627 }
1628
1629 void
1630 arc_buf_freeze(arc_buf_t *buf)
1631 {
1632 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1633 return;
1634
1635 if (ARC_BUF_COMPRESSED(buf))
1636 return;
1637
1638 ASSERT(HDR_HAS_L1HDR(buf->b_hdr));
1639 arc_cksum_compute(buf);
1640 }
1641
1642 /*
1643 * The arc_buf_hdr_t's b_flags should never be modified directly. Instead,
1644 * the following functions should be used to ensure that the flags are
1645 * updated in a thread-safe way. When manipulating the flags either
1646 * the hash_lock must be held or the hdr must be undiscoverable. This
1647 * ensures that we're not racing with any other threads when updating
1648 * the flags.
1649 */
1650 static inline void
1651 arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags)
1652 {
1653 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1654 hdr->b_flags |= flags;
1655 }
1656
1657 static inline void
1658 arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags)
1659 {
1660 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1661 hdr->b_flags &= ~flags;
1662 }
1663
1664 /*
1665 * Setting the compression bits in the arc_buf_hdr_t's b_flags is
1666 * done in a special way since we have to clear and set bits
1667 * at the same time. Consumers that wish to set the compression bits
1668 * must use this function to ensure that the flags are updated in
1669 * thread-safe manner.
1670 */
1671 static void
1672 arc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp)
1673 {
1674 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1675
1676 /*
1677 * Holes and embedded blocks will always have a psize = 0 so
1678 * we ignore the compression of the blkptr and set the
1679 * want to uncompress them. Mark them as uncompressed.
1680 */
1681 if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) {
1682 arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC);
1683 ASSERT(!HDR_COMPRESSION_ENABLED(hdr));
1684 } else {
1685 arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC);
1686 ASSERT(HDR_COMPRESSION_ENABLED(hdr));
1687 }
1688
1689 HDR_SET_COMPRESS(hdr, cmp);
1690 ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp);
1691 }
1692
1693 /*
1694 * Looks for another buf on the same hdr which has the data decompressed, copies
1695 * from it, and returns true. If no such buf exists, returns false.
1696 */
1697 static boolean_t
1698 arc_buf_try_copy_decompressed_data(arc_buf_t *buf)
1699 {
1700 arc_buf_hdr_t *hdr = buf->b_hdr;
1701 boolean_t copied = B_FALSE;
1702
1703 ASSERT(HDR_HAS_L1HDR(hdr));
1704 ASSERT3P(buf->b_data, !=, NULL);
1705 ASSERT(!ARC_BUF_COMPRESSED(buf));
1706
1707 for (arc_buf_t *from = hdr->b_l1hdr.b_buf; from != NULL;
1708 from = from->b_next) {
1709 /* can't use our own data buffer */
1710 if (from == buf) {
1711 continue;
1712 }
1713
1714 if (!ARC_BUF_COMPRESSED(from)) {
1715 memcpy(buf->b_data, from->b_data, arc_buf_size(buf));
1716 copied = B_TRUE;
1717 break;
1718 }
1719 }
1720
1721 #ifdef ZFS_DEBUG
1722 /*
1723 * There were no decompressed bufs, so there should not be a
1724 * checksum on the hdr either.
1725 */
1726 if (zfs_flags & ZFS_DEBUG_MODIFY)
1727 EQUIV(!copied, hdr->b_l1hdr.b_freeze_cksum == NULL);
1728 #endif
1729
1730 return (copied);
1731 }
1732
1733 /*
1734 * Allocates an ARC buf header that's in an evicted & L2-cached state.
1735 * This is used during l2arc reconstruction to make empty ARC buffers
1736 * which circumvent the regular disk->arc->l2arc path and instead come
1737 * into being in the reverse order, i.e. l2arc->arc.
1738 */
1739 static arc_buf_hdr_t *
1740 arc_buf_alloc_l2only(size_t size, arc_buf_contents_t type, l2arc_dev_t *dev,
1741 dva_t dva, uint64_t daddr, int32_t psize, uint64_t birth,
1742 enum zio_compress compress, uint8_t complevel, boolean_t protected,
1743 boolean_t prefetch, arc_state_type_t arcs_state)
1744 {
1745 arc_buf_hdr_t *hdr;
1746
1747 ASSERT(size != 0);
1748 hdr = kmem_cache_alloc(hdr_l2only_cache, KM_SLEEP);
1749 hdr->b_birth = birth;
1750 hdr->b_type = type;
1751 hdr->b_flags = 0;
1752 arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L2HDR);
1753 HDR_SET_LSIZE(hdr, size);
1754 HDR_SET_PSIZE(hdr, psize);
1755 arc_hdr_set_compress(hdr, compress);
1756 hdr->b_complevel = complevel;
1757 if (protected)
1758 arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED);
1759 if (prefetch)
1760 arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH);
1761 hdr->b_spa = spa_load_guid(dev->l2ad_vdev->vdev_spa);
1762
1763 hdr->b_dva = dva;
1764
1765 hdr->b_l2hdr.b_dev = dev;
1766 hdr->b_l2hdr.b_daddr = daddr;
1767 hdr->b_l2hdr.b_arcs_state = arcs_state;
1768
1769 return (hdr);
1770 }
1771
1772 /*
1773 * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t.
1774 */
1775 static uint64_t
1776 arc_hdr_size(arc_buf_hdr_t *hdr)
1777 {
1778 uint64_t size;
1779
1780 if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF &&
1781 HDR_GET_PSIZE(hdr) > 0) {
1782 size = HDR_GET_PSIZE(hdr);
1783 } else {
1784 ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0);
1785 size = HDR_GET_LSIZE(hdr);
1786 }
1787 return (size);
1788 }
1789
1790 static int
1791 arc_hdr_authenticate(arc_buf_hdr_t *hdr, spa_t *spa, uint64_t dsobj)
1792 {
1793 int ret;
1794 uint64_t csize;
1795 uint64_t lsize = HDR_GET_LSIZE(hdr);
1796 uint64_t psize = HDR_GET_PSIZE(hdr);
1797 void *tmpbuf = NULL;
1798 abd_t *abd = hdr->b_l1hdr.b_pabd;
1799
1800 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1801 ASSERT(HDR_AUTHENTICATED(hdr));
1802 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
1803
1804 /*
1805 * The MAC is calculated on the compressed data that is stored on disk.
1806 * However, if compressed arc is disabled we will only have the
1807 * decompressed data available to us now. Compress it into a temporary
1808 * abd so we can verify the MAC. The performance overhead of this will
1809 * be relatively low, since most objects in an encrypted objset will
1810 * be encrypted (instead of authenticated) anyway.
1811 */
1812 if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF &&
1813 !HDR_COMPRESSION_ENABLED(hdr)) {
1814
1815 csize = zio_compress_data(HDR_GET_COMPRESS(hdr),
1816 hdr->b_l1hdr.b_pabd, &tmpbuf, lsize, hdr->b_complevel);
1817 ASSERT3P(tmpbuf, !=, NULL);
1818 ASSERT3U(csize, <=, psize);
1819 abd = abd_get_from_buf(tmpbuf, lsize);
1820 abd_take_ownership_of_buf(abd, B_TRUE);
1821 abd_zero_off(abd, csize, psize - csize);
1822 }
1823
1824 /*
1825 * Authentication is best effort. We authenticate whenever the key is
1826 * available. If we succeed we clear ARC_FLAG_NOAUTH.
1827 */
1828 if (hdr->b_crypt_hdr.b_ot == DMU_OT_OBJSET) {
1829 ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF);
1830 ASSERT3U(lsize, ==, psize);
1831 ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa, dsobj, abd,
1832 psize, hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS);
1833 } else {
1834 ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj, abd, psize,
1835 hdr->b_crypt_hdr.b_mac);
1836 }
1837
1838 if (ret == 0)
1839 arc_hdr_clear_flags(hdr, ARC_FLAG_NOAUTH);
1840 else if (ret != ENOENT)
1841 goto error;
1842
1843 if (tmpbuf != NULL)
1844 abd_free(abd);
1845
1846 return (0);
1847
1848 error:
1849 if (tmpbuf != NULL)
1850 abd_free(abd);
1851
1852 return (ret);
1853 }
1854
1855 /*
1856 * This function will take a header that only has raw encrypted data in
1857 * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in
1858 * b_l1hdr.b_pabd. If designated in the header flags, this function will
1859 * also decompress the data.
1860 */
1861 static int
1862 arc_hdr_decrypt(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb)
1863 {
1864 int ret;
1865 abd_t *cabd = NULL;
1866 void *tmp = NULL;
1867 boolean_t no_crypt = B_FALSE;
1868 boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS);
1869
1870 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1871 ASSERT(HDR_ENCRYPTED(hdr));
1872
1873 arc_hdr_alloc_abd(hdr, 0);
1874
1875 ret = spa_do_crypt_abd(B_FALSE, spa, zb, hdr->b_crypt_hdr.b_ot,
1876 B_FALSE, bswap, hdr->b_crypt_hdr.b_salt, hdr->b_crypt_hdr.b_iv,
1877 hdr->b_crypt_hdr.b_mac, HDR_GET_PSIZE(hdr), hdr->b_l1hdr.b_pabd,
1878 hdr->b_crypt_hdr.b_rabd, &no_crypt);
1879 if (ret != 0)
1880 goto error;
1881
1882 if (no_crypt) {
1883 abd_copy(hdr->b_l1hdr.b_pabd, hdr->b_crypt_hdr.b_rabd,
1884 HDR_GET_PSIZE(hdr));
1885 }
1886
1887 /*
1888 * If this header has disabled arc compression but the b_pabd is
1889 * compressed after decrypting it, we need to decompress the newly
1890 * decrypted data.
1891 */
1892 if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF &&
1893 !HDR_COMPRESSION_ENABLED(hdr)) {
1894 /*
1895 * We want to make sure that we are correctly honoring the
1896 * zfs_abd_scatter_enabled setting, so we allocate an abd here
1897 * and then loan a buffer from it, rather than allocating a
1898 * linear buffer and wrapping it in an abd later.
1899 */
1900 cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, 0);
1901 tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr));
1902
1903 ret = zio_decompress_data(HDR_GET_COMPRESS(hdr),
1904 hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr),
1905 HDR_GET_LSIZE(hdr), &hdr->b_complevel);
1906 if (ret != 0) {
1907 abd_return_buf(cabd, tmp, arc_hdr_size(hdr));
1908 goto error;
1909 }
1910
1911 abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr));
1912 arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd,
1913 arc_hdr_size(hdr), hdr);
1914 hdr->b_l1hdr.b_pabd = cabd;
1915 }
1916
1917 return (0);
1918
1919 error:
1920 arc_hdr_free_abd(hdr, B_FALSE);
1921 if (cabd != NULL)
1922 arc_free_data_buf(hdr, cabd, arc_hdr_size(hdr), hdr);
1923
1924 return (ret);
1925 }
1926
1927 /*
1928 * This function is called during arc_buf_fill() to prepare the header's
1929 * abd plaintext pointer for use. This involves authenticated protected
1930 * data and decrypting encrypted data into the plaintext abd.
1931 */
1932 static int
1933 arc_fill_hdr_crypt(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, spa_t *spa,
1934 const zbookmark_phys_t *zb, boolean_t noauth)
1935 {
1936 int ret;
1937
1938 ASSERT(HDR_PROTECTED(hdr));
1939
1940 if (hash_lock != NULL)
1941 mutex_enter(hash_lock);
1942
1943 if (HDR_NOAUTH(hdr) && !noauth) {
1944 /*
1945 * The caller requested authenticated data but our data has
1946 * not been authenticated yet. Verify the MAC now if we can.
1947 */
1948 ret = arc_hdr_authenticate(hdr, spa, zb->zb_objset);
1949 if (ret != 0)
1950 goto error;
1951 } else if (HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd == NULL) {
1952 /*
1953 * If we only have the encrypted version of the data, but the
1954 * unencrypted version was requested we take this opportunity
1955 * to store the decrypted version in the header for future use.
1956 */
1957 ret = arc_hdr_decrypt(hdr, spa, zb);
1958 if (ret != 0)
1959 goto error;
1960 }
1961
1962 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
1963
1964 if (hash_lock != NULL)
1965 mutex_exit(hash_lock);
1966
1967 return (0);
1968
1969 error:
1970 if (hash_lock != NULL)
1971 mutex_exit(hash_lock);
1972
1973 return (ret);
1974 }
1975
1976 /*
1977 * This function is used by the dbuf code to decrypt bonus buffers in place.
1978 * The dbuf code itself doesn't have any locking for decrypting a shared dnode
1979 * block, so we use the hash lock here to protect against concurrent calls to
1980 * arc_buf_fill().
1981 */
1982 static void
1983 arc_buf_untransform_in_place(arc_buf_t *buf)
1984 {
1985 arc_buf_hdr_t *hdr = buf->b_hdr;
1986
1987 ASSERT(HDR_ENCRYPTED(hdr));
1988 ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE);
1989 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1990 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
1991
1992 zio_crypt_copy_dnode_bonus(hdr->b_l1hdr.b_pabd, buf->b_data,
1993 arc_buf_size(buf));
1994 buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED;
1995 buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED;
1996 }
1997
1998 /*
1999 * Given a buf that has a data buffer attached to it, this function will
2000 * efficiently fill the buf with data of the specified compression setting from
2001 * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr
2002 * are already sharing a data buf, no copy is performed.
2003 *
2004 * If the buf is marked as compressed but uncompressed data was requested, this
2005 * will allocate a new data buffer for the buf, remove that flag, and fill the
2006 * buf with uncompressed data. You can't request a compressed buf on a hdr with
2007 * uncompressed data, and (since we haven't added support for it yet) if you
2008 * want compressed data your buf must already be marked as compressed and have
2009 * the correct-sized data buffer.
2010 */
2011 static int
2012 arc_buf_fill(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb,
2013 arc_fill_flags_t flags)
2014 {
2015 int error = 0;
2016 arc_buf_hdr_t *hdr = buf->b_hdr;
2017 boolean_t hdr_compressed =
2018 (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF);
2019 boolean_t compressed = (flags & ARC_FILL_COMPRESSED) != 0;
2020 boolean_t encrypted = (flags & ARC_FILL_ENCRYPTED) != 0;
2021 dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap;
2022 kmutex_t *hash_lock = (flags & ARC_FILL_LOCKED) ? NULL : HDR_LOCK(hdr);
2023
2024 ASSERT3P(buf->b_data, !=, NULL);
2025 IMPLY(compressed, hdr_compressed || ARC_BUF_ENCRYPTED(buf));
2026 IMPLY(compressed, ARC_BUF_COMPRESSED(buf));
2027 IMPLY(encrypted, HDR_ENCRYPTED(hdr));
2028 IMPLY(encrypted, ARC_BUF_ENCRYPTED(buf));
2029 IMPLY(encrypted, ARC_BUF_COMPRESSED(buf));
2030 IMPLY(encrypted, !ARC_BUF_SHARED(buf));
2031
2032 /*
2033 * If the caller wanted encrypted data we just need to copy it from
2034 * b_rabd and potentially byteswap it. We won't be able to do any
2035 * further transforms on it.
2036 */
2037 if (encrypted) {
2038 ASSERT(HDR_HAS_RABD(hdr));
2039 abd_copy_to_buf(buf->b_data, hdr->b_crypt_hdr.b_rabd,
2040 HDR_GET_PSIZE(hdr));
2041 goto byteswap;
2042 }
2043
2044 /*
2045 * Adjust encrypted and authenticated headers to accommodate
2046 * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are
2047 * allowed to fail decryption due to keys not being loaded
2048 * without being marked as an IO error.
2049 */
2050 if (HDR_PROTECTED(hdr)) {
2051 error = arc_fill_hdr_crypt(hdr, hash_lock, spa,
2052 zb, !!(flags & ARC_FILL_NOAUTH));
2053 if (error == EACCES && (flags & ARC_FILL_IN_PLACE) != 0) {
2054 return (error);
2055 } else if (error != 0) {
2056 if (hash_lock != NULL)
2057 mutex_enter(hash_lock);
2058 arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR);
2059 if (hash_lock != NULL)
2060 mutex_exit(hash_lock);
2061 return (error);
2062 }
2063 }
2064
2065 /*
2066 * There is a special case here for dnode blocks which are
2067 * decrypting their bonus buffers. These blocks may request to
2068 * be decrypted in-place. This is necessary because there may
2069 * be many dnodes pointing into this buffer and there is
2070 * currently no method to synchronize replacing the backing
2071 * b_data buffer and updating all of the pointers. Here we use
2072 * the hash lock to ensure there are no races. If the need
2073 * arises for other types to be decrypted in-place, they must
2074 * add handling here as well.
2075 */
2076 if ((flags & ARC_FILL_IN_PLACE) != 0) {
2077 ASSERT(!hdr_compressed);
2078 ASSERT(!compressed);
2079 ASSERT(!encrypted);
2080
2081 if (HDR_ENCRYPTED(hdr) && ARC_BUF_ENCRYPTED(buf)) {
2082 ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE);
2083
2084 if (hash_lock != NULL)
2085 mutex_enter(hash_lock);
2086 arc_buf_untransform_in_place(buf);
2087 if (hash_lock != NULL)
2088 mutex_exit(hash_lock);
2089
2090 /* Compute the hdr's checksum if necessary */
2091 arc_cksum_compute(buf);
2092 }
2093
2094 return (0);
2095 }
2096
2097 if (hdr_compressed == compressed) {
2098 if (!arc_buf_is_shared(buf)) {
2099 abd_copy_to_buf(buf->b_data, hdr->b_l1hdr.b_pabd,
2100 arc_buf_size(buf));
2101 }
2102 } else {
2103 ASSERT(hdr_compressed);
2104 ASSERT(!compressed);
2105
2106 /*
2107 * If the buf is sharing its data with the hdr, unlink it and
2108 * allocate a new data buffer for the buf.
2109 */
2110 if (arc_buf_is_shared(buf)) {
2111 ASSERT(ARC_BUF_COMPRESSED(buf));
2112
2113 /* We need to give the buf its own b_data */
2114 buf->b_flags &= ~ARC_BUF_FLAG_SHARED;
2115 buf->b_data =
2116 arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf);
2117 arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA);
2118
2119 /* Previously overhead was 0; just add new overhead */
2120 ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr));
2121 } else if (ARC_BUF_COMPRESSED(buf)) {
2122 /* We need to reallocate the buf's b_data */
2123 arc_free_data_buf(hdr, buf->b_data, HDR_GET_PSIZE(hdr),
2124 buf);
2125 buf->b_data =
2126 arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf);
2127
2128 /* We increased the size of b_data; update overhead */
2129 ARCSTAT_INCR(arcstat_overhead_size,
2130 HDR_GET_LSIZE(hdr) - HDR_GET_PSIZE(hdr));
2131 }
2132
2133 /*
2134 * Regardless of the buf's previous compression settings, it
2135 * should not be compressed at the end of this function.
2136 */
2137 buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED;
2138
2139 /*
2140 * Try copying the data from another buf which already has a
2141 * decompressed version. If that's not possible, it's time to
2142 * bite the bullet and decompress the data from the hdr.
2143 */
2144 if (arc_buf_try_copy_decompressed_data(buf)) {
2145 /* Skip byteswapping and checksumming (already done) */
2146 return (0);
2147 } else {
2148 error = zio_decompress_data(HDR_GET_COMPRESS(hdr),
2149 hdr->b_l1hdr.b_pabd, buf->b_data,
2150 HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr),
2151 &hdr->b_complevel);
2152
2153 /*
2154 * Absent hardware errors or software bugs, this should
2155 * be impossible, but log it anyway so we can debug it.
2156 */
2157 if (error != 0) {
2158 zfs_dbgmsg(
2159 "hdr %px, compress %d, psize %d, lsize %d",
2160 hdr, arc_hdr_get_compress(hdr),
2161 HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr));
2162 if (hash_lock != NULL)
2163 mutex_enter(hash_lock);
2164 arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR);
2165 if (hash_lock != NULL)
2166 mutex_exit(hash_lock);
2167 return (SET_ERROR(EIO));
2168 }
2169 }
2170 }
2171
2172 byteswap:
2173 /* Byteswap the buf's data if necessary */
2174 if (bswap != DMU_BSWAP_NUMFUNCS) {
2175 ASSERT(!HDR_SHARED_DATA(hdr));
2176 ASSERT3U(bswap, <, DMU_BSWAP_NUMFUNCS);
2177 dmu_ot_byteswap[bswap].ob_func(buf->b_data, HDR_GET_LSIZE(hdr));
2178 }
2179
2180 /* Compute the hdr's checksum if necessary */
2181 arc_cksum_compute(buf);
2182
2183 return (0);
2184 }
2185
2186 /*
2187 * If this function is being called to decrypt an encrypted buffer or verify an
2188 * authenticated one, the key must be loaded and a mapping must be made
2189 * available in the keystore via spa_keystore_create_mapping() or one of its
2190 * callers.
2191 */
2192 int
2193 arc_untransform(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb,
2194 boolean_t in_place)
2195 {
2196 int ret;
2197 arc_fill_flags_t flags = 0;
2198
2199 if (in_place)
2200 flags |= ARC_FILL_IN_PLACE;
2201
2202 ret = arc_buf_fill(buf, spa, zb, flags);
2203 if (ret == ECKSUM) {
2204 /*
2205 * Convert authentication and decryption errors to EIO
2206 * (and generate an ereport) before leaving the ARC.
2207 */
2208 ret = SET_ERROR(EIO);
2209 spa_log_error(spa, zb, &buf->b_hdr->b_birth);
2210 (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION,
2211 spa, NULL, zb, NULL, 0);
2212 }
2213
2214 return (ret);
2215 }
2216
2217 /*
2218 * Increment the amount of evictable space in the arc_state_t's refcount.
2219 * We account for the space used by the hdr and the arc buf individually
2220 * so that we can add and remove them from the refcount individually.
2221 */
2222 static void
2223 arc_evictable_space_increment(arc_buf_hdr_t *hdr, arc_state_t *state)
2224 {
2225 arc_buf_contents_t type = arc_buf_type(hdr);
2226
2227 ASSERT(HDR_HAS_L1HDR(hdr));
2228
2229 if (GHOST_STATE(state)) {
2230 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
2231 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
2232 ASSERT(!HDR_HAS_RABD(hdr));
2233 (void) zfs_refcount_add_many(&state->arcs_esize[type],
2234 HDR_GET_LSIZE(hdr), hdr);
2235 return;
2236 }
2237
2238 if (hdr->b_l1hdr.b_pabd != NULL) {
2239 (void) zfs_refcount_add_many(&state->arcs_esize[type],
2240 arc_hdr_size(hdr), hdr);
2241 }
2242 if (HDR_HAS_RABD(hdr)) {
2243 (void) zfs_refcount_add_many(&state->arcs_esize[type],
2244 HDR_GET_PSIZE(hdr), hdr);
2245 }
2246
2247 for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL;
2248 buf = buf->b_next) {
2249 if (arc_buf_is_shared(buf))
2250 continue;
2251 (void) zfs_refcount_add_many(&state->arcs_esize[type],
2252 arc_buf_size(buf), buf);
2253 }
2254 }
2255
2256 /*
2257 * Decrement the amount of evictable space in the arc_state_t's refcount.
2258 * We account for the space used by the hdr and the arc buf individually
2259 * so that we can add and remove them from the refcount individually.
2260 */
2261 static void
2262 arc_evictable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state)
2263 {
2264 arc_buf_contents_t type = arc_buf_type(hdr);
2265
2266 ASSERT(HDR_HAS_L1HDR(hdr));
2267
2268 if (GHOST_STATE(state)) {
2269 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
2270 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
2271 ASSERT(!HDR_HAS_RABD(hdr));
2272 (void) zfs_refcount_remove_many(&state->arcs_esize[type],
2273 HDR_GET_LSIZE(hdr), hdr);
2274 return;
2275 }
2276
2277 if (hdr->b_l1hdr.b_pabd != NULL) {
2278 (void) zfs_refcount_remove_many(&state->arcs_esize[type],
2279 arc_hdr_size(hdr), hdr);
2280 }
2281 if (HDR_HAS_RABD(hdr)) {
2282 (void) zfs_refcount_remove_many(&state->arcs_esize[type],
2283 HDR_GET_PSIZE(hdr), hdr);
2284 }
2285
2286 for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL;
2287 buf = buf->b_next) {
2288 if (arc_buf_is_shared(buf))
2289 continue;
2290 (void) zfs_refcount_remove_many(&state->arcs_esize[type],
2291 arc_buf_size(buf), buf);
2292 }
2293 }
2294
2295 /*
2296 * Add a reference to this hdr indicating that someone is actively
2297 * referencing that memory. When the refcount transitions from 0 to 1,
2298 * we remove it from the respective arc_state_t list to indicate that
2299 * it is not evictable.
2300 */
2301 static void
2302 add_reference(arc_buf_hdr_t *hdr, const void *tag)
2303 {
2304 arc_state_t *state = hdr->b_l1hdr.b_state;
2305
2306 ASSERT(HDR_HAS_L1HDR(hdr));
2307 if (!HDR_EMPTY(hdr) && !MUTEX_HELD(HDR_LOCK(hdr))) {
2308 ASSERT(state == arc_anon);
2309 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
2310 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
2311 }
2312
2313 if ((zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) &&
2314 state != arc_anon && state != arc_l2c_only) {
2315 /* We don't use the L2-only state list. */
2316 multilist_remove(&state->arcs_list[arc_buf_type(hdr)], hdr);
2317 arc_evictable_space_decrement(hdr, state);
2318 }
2319 }
2320
2321 /*
2322 * Remove a reference from this hdr. When the reference transitions from
2323 * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's
2324 * list making it eligible for eviction.
2325 */
2326 static int
2327 remove_reference(arc_buf_hdr_t *hdr, const void *tag)
2328 {
2329 int cnt;
2330 arc_state_t *state = hdr->b_l1hdr.b_state;
2331
2332 ASSERT(HDR_HAS_L1HDR(hdr));
2333 ASSERT(state == arc_anon || MUTEX_HELD(HDR_LOCK(hdr)));
2334 ASSERT(!GHOST_STATE(state)); /* arc_l2c_only counts as a ghost. */
2335
2336 if ((cnt = zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) != 0)
2337 return (cnt);
2338
2339 if (state == arc_anon) {
2340 arc_hdr_destroy(hdr);
2341 return (0);
2342 }
2343 if (state == arc_uncached && !HDR_PREFETCH(hdr)) {
2344 arc_change_state(arc_anon, hdr);
2345 arc_hdr_destroy(hdr);
2346 return (0);
2347 }
2348 multilist_insert(&state->arcs_list[arc_buf_type(hdr)], hdr);
2349 arc_evictable_space_increment(hdr, state);
2350 return (0);
2351 }
2352
2353 /*
2354 * Returns detailed information about a specific arc buffer. When the
2355 * state_index argument is set the function will calculate the arc header
2356 * list position for its arc state. Since this requires a linear traversal
2357 * callers are strongly encourage not to do this. However, it can be helpful
2358 * for targeted analysis so the functionality is provided.
2359 */
2360 void
2361 arc_buf_info(arc_buf_t *ab, arc_buf_info_t *abi, int state_index)
2362 {
2363 (void) state_index;
2364 arc_buf_hdr_t *hdr = ab->b_hdr;
2365 l1arc_buf_hdr_t *l1hdr = NULL;
2366 l2arc_buf_hdr_t *l2hdr = NULL;
2367 arc_state_t *state = NULL;
2368
2369 memset(abi, 0, sizeof (arc_buf_info_t));
2370
2371 if (hdr == NULL)
2372 return;
2373
2374 abi->abi_flags = hdr->b_flags;
2375
2376 if (HDR_HAS_L1HDR(hdr)) {
2377 l1hdr = &hdr->b_l1hdr;
2378 state = l1hdr->b_state;
2379 }
2380 if (HDR_HAS_L2HDR(hdr))
2381 l2hdr = &hdr->b_l2hdr;
2382
2383 if (l1hdr) {
2384 abi->abi_bufcnt = 0;
2385 for (arc_buf_t *buf = l1hdr->b_buf; buf; buf = buf->b_next)
2386 abi->abi_bufcnt++;
2387 abi->abi_access = l1hdr->b_arc_access;
2388 abi->abi_mru_hits = l1hdr->b_mru_hits;
2389 abi->abi_mru_ghost_hits = l1hdr->b_mru_ghost_hits;
2390 abi->abi_mfu_hits = l1hdr->b_mfu_hits;
2391 abi->abi_mfu_ghost_hits = l1hdr->b_mfu_ghost_hits;
2392 abi->abi_holds = zfs_refcount_count(&l1hdr->b_refcnt);
2393 }
2394
2395 if (l2hdr) {
2396 abi->abi_l2arc_dattr = l2hdr->b_daddr;
2397 abi->abi_l2arc_hits = l2hdr->b_hits;
2398 }
2399
2400 abi->abi_state_type = state ? state->arcs_state : ARC_STATE_ANON;
2401 abi->abi_state_contents = arc_buf_type(hdr);
2402 abi->abi_size = arc_hdr_size(hdr);
2403 }
2404
2405 /*
2406 * Move the supplied buffer to the indicated state. The hash lock
2407 * for the buffer must be held by the caller.
2408 */
2409 static void
2410 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr)
2411 {
2412 arc_state_t *old_state;
2413 int64_t refcnt;
2414 boolean_t update_old, update_new;
2415 arc_buf_contents_t type = arc_buf_type(hdr);
2416
2417 /*
2418 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
2419 * in arc_read() when bringing a buffer out of the L2ARC. However, the
2420 * L1 hdr doesn't always exist when we change state to arc_anon before
2421 * destroying a header, in which case reallocating to add the L1 hdr is
2422 * pointless.
2423 */
2424 if (HDR_HAS_L1HDR(hdr)) {
2425 old_state = hdr->b_l1hdr.b_state;
2426 refcnt = zfs_refcount_count(&hdr->b_l1hdr.b_refcnt);
2427 update_old = (hdr->b_l1hdr.b_buf != NULL ||
2428 hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr));
2429
2430 IMPLY(GHOST_STATE(old_state), hdr->b_l1hdr.b_buf == NULL);
2431 IMPLY(GHOST_STATE(new_state), hdr->b_l1hdr.b_buf == NULL);
2432 IMPLY(old_state == arc_anon, hdr->b_l1hdr.b_buf == NULL ||
2433 ARC_BUF_LAST(hdr->b_l1hdr.b_buf));
2434 } else {
2435 old_state = arc_l2c_only;
2436 refcnt = 0;
2437 update_old = B_FALSE;
2438 }
2439 update_new = update_old;
2440 if (GHOST_STATE(old_state))
2441 update_old = B_TRUE;
2442 if (GHOST_STATE(new_state))
2443 update_new = B_TRUE;
2444
2445 ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
2446 ASSERT3P(new_state, !=, old_state);
2447
2448 /*
2449 * If this buffer is evictable, transfer it from the
2450 * old state list to the new state list.
2451 */
2452 if (refcnt == 0) {
2453 if (old_state != arc_anon && old_state != arc_l2c_only) {
2454 ASSERT(HDR_HAS_L1HDR(hdr));
2455 /* remove_reference() saves on insert. */
2456 if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) {
2457 multilist_remove(&old_state->arcs_list[type],
2458 hdr);
2459 arc_evictable_space_decrement(hdr, old_state);
2460 }
2461 }
2462 if (new_state != arc_anon && new_state != arc_l2c_only) {
2463 /*
2464 * An L1 header always exists here, since if we're
2465 * moving to some L1-cached state (i.e. not l2c_only or
2466 * anonymous), we realloc the header to add an L1hdr
2467 * beforehand.
2468 */
2469 ASSERT(HDR_HAS_L1HDR(hdr));
2470 multilist_insert(&new_state->arcs_list[type], hdr);
2471 arc_evictable_space_increment(hdr, new_state);
2472 }
2473 }
2474
2475 ASSERT(!HDR_EMPTY(hdr));
2476 if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr))
2477 buf_hash_remove(hdr);
2478
2479 /* adjust state sizes (ignore arc_l2c_only) */
2480
2481 if (update_new && new_state != arc_l2c_only) {
2482 ASSERT(HDR_HAS_L1HDR(hdr));
2483 if (GHOST_STATE(new_state)) {
2484
2485 /*
2486 * When moving a header to a ghost state, we first
2487 * remove all arc buffers. Thus, we'll have no arc
2488 * buffer to use for the reference. As a result, we
2489 * use the arc header pointer for the reference.
2490 */
2491 (void) zfs_refcount_add_many(
2492 &new_state->arcs_size[type],
2493 HDR_GET_LSIZE(hdr), hdr);
2494 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
2495 ASSERT(!HDR_HAS_RABD(hdr));
2496 } else {
2497
2498 /*
2499 * Each individual buffer holds a unique reference,
2500 * thus we must remove each of these references one
2501 * at a time.
2502 */
2503 for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL;
2504 buf = buf->b_next) {
2505
2506 /*
2507 * When the arc_buf_t is sharing the data
2508 * block with the hdr, the owner of the
2509 * reference belongs to the hdr. Only
2510 * add to the refcount if the arc_buf_t is
2511 * not shared.
2512 */
2513 if (arc_buf_is_shared(buf))
2514 continue;
2515
2516 (void) zfs_refcount_add_many(
2517 &new_state->arcs_size[type],
2518 arc_buf_size(buf), buf);
2519 }
2520
2521 if (hdr->b_l1hdr.b_pabd != NULL) {
2522 (void) zfs_refcount_add_many(
2523 &new_state->arcs_size[type],
2524 arc_hdr_size(hdr), hdr);
2525 }
2526
2527 if (HDR_HAS_RABD(hdr)) {
2528 (void) zfs_refcount_add_many(
2529 &new_state->arcs_size[type],
2530 HDR_GET_PSIZE(hdr), hdr);
2531 }
2532 }
2533 }
2534
2535 if (update_old && old_state != arc_l2c_only) {
2536 ASSERT(HDR_HAS_L1HDR(hdr));
2537 if (GHOST_STATE(old_state)) {
2538 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
2539 ASSERT(!HDR_HAS_RABD(hdr));
2540
2541 /*
2542 * When moving a header off of a ghost state,
2543 * the header will not contain any arc buffers.
2544 * We use the arc header pointer for the reference
2545 * which is exactly what we did when we put the
2546 * header on the ghost state.
2547 */
2548
2549 (void) zfs_refcount_remove_many(
2550 &old_state->arcs_size[type],
2551 HDR_GET_LSIZE(hdr), hdr);
2552 } else {
2553
2554 /*
2555 * Each individual buffer holds a unique reference,
2556 * thus we must remove each of these references one
2557 * at a time.
2558 */
2559 for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL;
2560 buf = buf->b_next) {
2561
2562 /*
2563 * When the arc_buf_t is sharing the data
2564 * block with the hdr, the owner of the
2565 * reference belongs to the hdr. Only
2566 * add to the refcount if the arc_buf_t is
2567 * not shared.
2568 */
2569 if (arc_buf_is_shared(buf))
2570 continue;
2571
2572 (void) zfs_refcount_remove_many(
2573 &old_state->arcs_size[type],
2574 arc_buf_size(buf), buf);
2575 }
2576 ASSERT(hdr->b_l1hdr.b_pabd != NULL ||
2577 HDR_HAS_RABD(hdr));
2578
2579 if (hdr->b_l1hdr.b_pabd != NULL) {
2580 (void) zfs_refcount_remove_many(
2581 &old_state->arcs_size[type],
2582 arc_hdr_size(hdr), hdr);
2583 }
2584
2585 if (HDR_HAS_RABD(hdr)) {
2586 (void) zfs_refcount_remove_many(
2587 &old_state->arcs_size[type],
2588 HDR_GET_PSIZE(hdr), hdr);
2589 }
2590 }
2591 }
2592
2593 if (HDR_HAS_L1HDR(hdr)) {
2594 hdr->b_l1hdr.b_state = new_state;
2595
2596 if (HDR_HAS_L2HDR(hdr) && new_state != arc_l2c_only) {
2597 l2arc_hdr_arcstats_decrement_state(hdr);
2598 hdr->b_l2hdr.b_arcs_state = new_state->arcs_state;
2599 l2arc_hdr_arcstats_increment_state(hdr);
2600 }
2601 }
2602 }
2603
2604 void
2605 arc_space_consume(uint64_t space, arc_space_type_t type)
2606 {
2607 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
2608
2609 switch (type) {
2610 default:
2611 break;
2612 case ARC_SPACE_DATA:
2613 ARCSTAT_INCR(arcstat_data_size, space);
2614 break;
2615 case ARC_SPACE_META:
2616 ARCSTAT_INCR(arcstat_metadata_size, space);
2617 break;
2618 case ARC_SPACE_BONUS:
2619 ARCSTAT_INCR(arcstat_bonus_size, space);
2620 break;
2621 case ARC_SPACE_DNODE:
2622 ARCSTAT_INCR(arcstat_dnode_size, space);
2623 break;
2624 case ARC_SPACE_DBUF:
2625 ARCSTAT_INCR(arcstat_dbuf_size, space);
2626 break;
2627 case ARC_SPACE_HDRS:
2628 ARCSTAT_INCR(arcstat_hdr_size, space);
2629 break;
2630 case ARC_SPACE_L2HDRS:
2631 aggsum_add(&arc_sums.arcstat_l2_hdr_size, space);
2632 break;
2633 case ARC_SPACE_ABD_CHUNK_WASTE:
2634 /*
2635 * Note: this includes space wasted by all scatter ABD's, not
2636 * just those allocated by the ARC. But the vast majority of
2637 * scatter ABD's come from the ARC, because other users are
2638 * very short-lived.
2639 */
2640 ARCSTAT_INCR(arcstat_abd_chunk_waste_size, space);
2641 break;
2642 }
2643
2644 if (type != ARC_SPACE_DATA && type != ARC_SPACE_ABD_CHUNK_WASTE)
2645 ARCSTAT_INCR(arcstat_meta_used, space);
2646
2647 aggsum_add(&arc_sums.arcstat_size, space);
2648 }
2649
2650 void
2651 arc_space_return(uint64_t space, arc_space_type_t type)
2652 {
2653 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
2654
2655 switch (type) {
2656 default:
2657 break;
2658 case ARC_SPACE_DATA:
2659 ARCSTAT_INCR(arcstat_data_size, -space);
2660 break;
2661 case ARC_SPACE_META:
2662 ARCSTAT_INCR(arcstat_metadata_size, -space);
2663 break;
2664 case ARC_SPACE_BONUS:
2665 ARCSTAT_INCR(arcstat_bonus_size, -space);
2666 break;
2667 case ARC_SPACE_DNODE:
2668 ARCSTAT_INCR(arcstat_dnode_size, -space);
2669 break;
2670 case ARC_SPACE_DBUF:
2671 ARCSTAT_INCR(arcstat_dbuf_size, -space);
2672 break;
2673 case ARC_SPACE_HDRS:
2674 ARCSTAT_INCR(arcstat_hdr_size, -space);
2675 break;
2676 case ARC_SPACE_L2HDRS:
2677 aggsum_add(&arc_sums.arcstat_l2_hdr_size, -space);
2678 break;
2679 case ARC_SPACE_ABD_CHUNK_WASTE:
2680 ARCSTAT_INCR(arcstat_abd_chunk_waste_size, -space);
2681 break;
2682 }
2683
2684 if (type != ARC_SPACE_DATA && type != ARC_SPACE_ABD_CHUNK_WASTE)
2685 ARCSTAT_INCR(arcstat_meta_used, -space);
2686
2687 ASSERT(aggsum_compare(&arc_sums.arcstat_size, space) >= 0);
2688 aggsum_add(&arc_sums.arcstat_size, -space);
2689 }
2690
2691 /*
2692 * Given a hdr and a buf, returns whether that buf can share its b_data buffer
2693 * with the hdr's b_pabd.
2694 */
2695 static boolean_t
2696 arc_can_share(arc_buf_hdr_t *hdr, arc_buf_t *buf)
2697 {
2698 /*
2699 * The criteria for sharing a hdr's data are:
2700 * 1. the buffer is not encrypted
2701 * 2. the hdr's compression matches the buf's compression
2702 * 3. the hdr doesn't need to be byteswapped
2703 * 4. the hdr isn't already being shared
2704 * 5. the buf is either compressed or it is the last buf in the hdr list
2705 *
2706 * Criterion #5 maintains the invariant that shared uncompressed
2707 * bufs must be the final buf in the hdr's b_buf list. Reading this, you
2708 * might ask, "if a compressed buf is allocated first, won't that be the
2709 * last thing in the list?", but in that case it's impossible to create
2710 * a shared uncompressed buf anyway (because the hdr must be compressed
2711 * to have the compressed buf). You might also think that #3 is
2712 * sufficient to make this guarantee, however it's possible
2713 * (specifically in the rare L2ARC write race mentioned in
2714 * arc_buf_alloc_impl()) there will be an existing uncompressed buf that
2715 * is shareable, but wasn't at the time of its allocation. Rather than
2716 * allow a new shared uncompressed buf to be created and then shuffle
2717 * the list around to make it the last element, this simply disallows
2718 * sharing if the new buf isn't the first to be added.
2719 */
2720 ASSERT3P(buf->b_hdr, ==, hdr);
2721 boolean_t hdr_compressed =
2722 arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF;
2723 boolean_t buf_compressed = ARC_BUF_COMPRESSED(buf) != 0;
2724 return (!ARC_BUF_ENCRYPTED(buf) &&
2725 buf_compressed == hdr_compressed &&
2726 hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS &&
2727 !HDR_SHARED_DATA(hdr) &&
2728 (ARC_BUF_LAST(buf) || ARC_BUF_COMPRESSED(buf)));
2729 }
2730
2731 /*
2732 * Allocate a buf for this hdr. If you care about the data that's in the hdr,
2733 * or if you want a compressed buffer, pass those flags in. Returns 0 if the
2734 * copy was made successfully, or an error code otherwise.
2735 */
2736 static int
2737 arc_buf_alloc_impl(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb,
2738 const void *tag, boolean_t encrypted, boolean_t compressed,
2739 boolean_t noauth, boolean_t fill, arc_buf_t **ret)
2740 {
2741 arc_buf_t *buf;
2742 arc_fill_flags_t flags = ARC_FILL_LOCKED;
2743
2744 ASSERT(HDR_HAS_L1HDR(hdr));
2745 ASSERT3U(HDR_GET_LSIZE(hdr), >, 0);
2746 VERIFY(hdr->b_type == ARC_BUFC_DATA ||
2747 hdr->b_type == ARC_BUFC_METADATA);
2748 ASSERT3P(ret, !=, NULL);
2749 ASSERT3P(*ret, ==, NULL);
2750 IMPLY(encrypted, compressed);
2751
2752 buf = *ret = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
2753 buf->b_hdr = hdr;
2754 buf->b_data = NULL;
2755 buf->b_next = hdr->b_l1hdr.b_buf;
2756 buf->b_flags = 0;
2757
2758 add_reference(hdr, tag);
2759
2760 /*
2761 * We're about to change the hdr's b_flags. We must either
2762 * hold the hash_lock or be undiscoverable.
2763 */
2764 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
2765
2766 /*
2767 * Only honor requests for compressed bufs if the hdr is actually
2768 * compressed. This must be overridden if the buffer is encrypted since
2769 * encrypted buffers cannot be decompressed.
2770 */
2771 if (encrypted) {
2772 buf->b_flags |= ARC_BUF_FLAG_COMPRESSED;
2773 buf->b_flags |= ARC_BUF_FLAG_ENCRYPTED;
2774 flags |= ARC_FILL_COMPRESSED | ARC_FILL_ENCRYPTED;
2775 } else if (compressed &&
2776 arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) {
2777 buf->b_flags |= ARC_BUF_FLAG_COMPRESSED;
2778 flags |= ARC_FILL_COMPRESSED;
2779 }
2780
2781 if (noauth) {
2782 ASSERT0(encrypted);
2783 flags |= ARC_FILL_NOAUTH;
2784 }
2785
2786 /*
2787 * If the hdr's data can be shared then we share the data buffer and
2788 * set the appropriate bit in the hdr's b_flags to indicate the hdr is
2789 * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new
2790 * buffer to store the buf's data.
2791 *
2792 * There are two additional restrictions here because we're sharing
2793 * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be
2794 * actively involved in an L2ARC write, because if this buf is used by
2795 * an arc_write() then the hdr's data buffer will be released when the
2796 * write completes, even though the L2ARC write might still be using it.
2797 * Second, the hdr's ABD must be linear so that the buf's user doesn't
2798 * need to be ABD-aware. It must be allocated via
2799 * zio_[data_]buf_alloc(), not as a page, because we need to be able
2800 * to abd_release_ownership_of_buf(), which isn't allowed on "linear
2801 * page" buffers because the ABD code needs to handle freeing them
2802 * specially.
2803 */
2804 boolean_t can_share = arc_can_share(hdr, buf) &&
2805 !HDR_L2_WRITING(hdr) &&
2806 hdr->b_l1hdr.b_pabd != NULL &&
2807 abd_is_linear(hdr->b_l1hdr.b_pabd) &&
2808 !abd_is_linear_page(hdr->b_l1hdr.b_pabd);
2809
2810 /* Set up b_data and sharing */
2811 if (can_share) {
2812 buf->b_data = abd_to_buf(hdr->b_l1hdr.b_pabd);
2813 buf->b_flags |= ARC_BUF_FLAG_SHARED;
2814 arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA);
2815 } else {
2816 buf->b_data =
2817 arc_get_data_buf(hdr, arc_buf_size(buf), buf);
2818 ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf));
2819 }
2820 VERIFY3P(buf->b_data, !=, NULL);
2821
2822 hdr->b_l1hdr.b_buf = buf;
2823
2824 /*
2825 * If the user wants the data from the hdr, we need to either copy or
2826 * decompress the data.
2827 */
2828 if (fill) {
2829 ASSERT3P(zb, !=, NULL);
2830 return (arc_buf_fill(buf, spa, zb, flags));
2831 }
2832
2833 return (0);
2834 }
2835
2836 static const char *arc_onloan_tag = "onloan";
2837
2838 static inline void
2839 arc_loaned_bytes_update(int64_t delta)
2840 {
2841 atomic_add_64(&arc_loaned_bytes, delta);
2842
2843 /* assert that it did not wrap around */
2844 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0);
2845 }
2846
2847 /*
2848 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
2849 * flight data by arc_tempreserve_space() until they are "returned". Loaned
2850 * buffers must be returned to the arc before they can be used by the DMU or
2851 * freed.
2852 */
2853 arc_buf_t *
2854 arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size)
2855 {
2856 arc_buf_t *buf = arc_alloc_buf(spa, arc_onloan_tag,
2857 is_metadata ? ARC_BUFC_METADATA : ARC_BUFC_DATA, size);
2858
2859 arc_loaned_bytes_update(arc_buf_size(buf));
2860
2861 return (buf);
2862 }
2863
2864 arc_buf_t *
2865 arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize,
2866 enum zio_compress compression_type, uint8_t complevel)
2867 {
2868 arc_buf_t *buf = arc_alloc_compressed_buf(spa, arc_onloan_tag,
2869 psize, lsize, compression_type, complevel);
2870
2871 arc_loaned_bytes_update(arc_buf_size(buf));
2872
2873 return (buf);
2874 }
2875
2876 arc_buf_t *
2877 arc_loan_raw_buf(spa_t *spa, uint64_t dsobj, boolean_t byteorder,
2878 const uint8_t *salt, const uint8_t *iv, const uint8_t *mac,
2879 dmu_object_type_t ot, uint64_t psize, uint64_t lsize,
2880 enum zio_compress compression_type, uint8_t complevel)
2881 {
2882 arc_buf_t *buf = arc_alloc_raw_buf(spa, arc_onloan_tag, dsobj,
2883 byteorder, salt, iv, mac, ot, psize, lsize, compression_type,
2884 complevel);
2885
2886 atomic_add_64(&arc_loaned_bytes, psize);
2887 return (buf);
2888 }
2889
2890
2891 /*
2892 * Return a loaned arc buffer to the arc.
2893 */
2894 void
2895 arc_return_buf(arc_buf_t *buf, const void *tag)
2896 {
2897 arc_buf_hdr_t *hdr = buf->b_hdr;
2898
2899 ASSERT3P(buf->b_data, !=, NULL);
2900 ASSERT(HDR_HAS_L1HDR(hdr));
2901 (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag);
2902 (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag);
2903
2904 arc_loaned_bytes_update(-arc_buf_size(buf));
2905 }
2906
2907 /* Detach an arc_buf from a dbuf (tag) */
2908 void
2909 arc_loan_inuse_buf(arc_buf_t *buf, const void *tag)
2910 {
2911 arc_buf_hdr_t *hdr = buf->b_hdr;
2912
2913 ASSERT3P(buf->b_data, !=, NULL);
2914 ASSERT(HDR_HAS_L1HDR(hdr));
2915 (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag);
2916 (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag);
2917
2918 arc_loaned_bytes_update(arc_buf_size(buf));
2919 }
2920
2921 static void
2922 l2arc_free_abd_on_write(abd_t *abd, size_t size, arc_buf_contents_t type)
2923 {
2924 l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP);
2925
2926 df->l2df_abd = abd;
2927 df->l2df_size = size;
2928 df->l2df_type = type;
2929 mutex_enter(&l2arc_free_on_write_mtx);
2930 list_insert_head(l2arc_free_on_write, df);
2931 mutex_exit(&l2arc_free_on_write_mtx);
2932 }
2933
2934 static void
2935 arc_hdr_free_on_write(arc_buf_hdr_t *hdr, boolean_t free_rdata)
2936 {
2937 arc_state_t *state = hdr->b_l1hdr.b_state;
2938 arc_buf_contents_t type = arc_buf_type(hdr);
2939 uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr);
2940
2941 /* protected by hash lock, if in the hash table */
2942 if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) {
2943 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
2944 ASSERT(state != arc_anon && state != arc_l2c_only);
2945
2946 (void) zfs_refcount_remove_many(&state->arcs_esize[type],
2947 size, hdr);
2948 }
2949 (void) zfs_refcount_remove_many(&state->arcs_size[type], size, hdr);
2950 if (type == ARC_BUFC_METADATA) {
2951 arc_space_return(size, ARC_SPACE_META);
2952 } else {
2953 ASSERT(type == ARC_BUFC_DATA);
2954 arc_space_return(size, ARC_SPACE_DATA);
2955 }
2956
2957 if (free_rdata) {
2958 l2arc_free_abd_on_write(hdr->b_crypt_hdr.b_rabd, size, type);
2959 } else {
2960 l2arc_free_abd_on_write(hdr->b_l1hdr.b_pabd, size, type);
2961 }
2962 }
2963
2964 /*
2965 * Share the arc_buf_t's data with the hdr. Whenever we are sharing the
2966 * data buffer, we transfer the refcount ownership to the hdr and update
2967 * the appropriate kstats.
2968 */
2969 static void
2970 arc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf)
2971 {
2972 ASSERT(arc_can_share(hdr, buf));
2973 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
2974 ASSERT(!ARC_BUF_ENCRYPTED(buf));
2975 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
2976
2977 /*
2978 * Start sharing the data buffer. We transfer the
2979 * refcount ownership to the hdr since it always owns
2980 * the refcount whenever an arc_buf_t is shared.
2981 */
2982 zfs_refcount_transfer_ownership_many(
2983 &hdr->b_l1hdr.b_state->arcs_size[arc_buf_type(hdr)],
2984 arc_hdr_size(hdr), buf, hdr);
2985 hdr->b_l1hdr.b_pabd = abd_get_from_buf(buf->b_data, arc_buf_size(buf));
2986 abd_take_ownership_of_buf(hdr->b_l1hdr.b_pabd,
2987 HDR_ISTYPE_METADATA(hdr));
2988 arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA);
2989 buf->b_flags |= ARC_BUF_FLAG_SHARED;
2990
2991 /*
2992 * Since we've transferred ownership to the hdr we need
2993 * to increment its compressed and uncompressed kstats and
2994 * decrement the overhead size.
2995 */
2996 ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr));
2997 ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr));
2998 ARCSTAT_INCR(arcstat_overhead_size, -arc_buf_size(buf));
2999 }
3000
3001 static void
3002 arc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf)
3003 {
3004 ASSERT(arc_buf_is_shared(buf));
3005 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
3006 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
3007
3008 /*
3009 * We are no longer sharing this buffer so we need
3010 * to transfer its ownership to the rightful owner.
3011 */
3012 zfs_refcount_transfer_ownership_many(
3013 &hdr->b_l1hdr.b_state->arcs_size[arc_buf_type(hdr)],
3014 arc_hdr_size(hdr), hdr, buf);
3015 arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA);
3016 abd_release_ownership_of_buf(hdr->b_l1hdr.b_pabd);
3017 abd_free(hdr->b_l1hdr.b_pabd);
3018 hdr->b_l1hdr.b_pabd = NULL;
3019 buf->b_flags &= ~ARC_BUF_FLAG_SHARED;
3020
3021 /*
3022 * Since the buffer is no longer shared between
3023 * the arc buf and the hdr, count it as overhead.
3024 */
3025 ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr));
3026 ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr));
3027 ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf));
3028 }
3029
3030 /*
3031 * Remove an arc_buf_t from the hdr's buf list and return the last
3032 * arc_buf_t on the list. If no buffers remain on the list then return
3033 * NULL.
3034 */
3035 static arc_buf_t *
3036 arc_buf_remove(arc_buf_hdr_t *hdr, arc_buf_t *buf)
3037 {
3038 ASSERT(HDR_HAS_L1HDR(hdr));
3039 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
3040
3041 arc_buf_t **bufp = &hdr->b_l1hdr.b_buf;
3042 arc_buf_t *lastbuf = NULL;
3043
3044 /*
3045 * Remove the buf from the hdr list and locate the last
3046 * remaining buffer on the list.
3047 */
3048 while (*bufp != NULL) {
3049 if (*bufp == buf)
3050 *bufp = buf->b_next;
3051
3052 /*
3053 * If we've removed a buffer in the middle of
3054 * the list then update the lastbuf and update
3055 * bufp.
3056 */
3057 if (*bufp != NULL) {
3058 lastbuf = *bufp;
3059 bufp = &(*bufp)->b_next;
3060 }
3061 }
3062 buf->b_next = NULL;
3063 ASSERT3P(lastbuf, !=, buf);
3064 IMPLY(lastbuf != NULL, ARC_BUF_LAST(lastbuf));
3065
3066 return (lastbuf);
3067 }
3068
3069 /*
3070 * Free up buf->b_data and pull the arc_buf_t off of the arc_buf_hdr_t's
3071 * list and free it.
3072 */
3073 static void
3074 arc_buf_destroy_impl(arc_buf_t *buf)
3075 {
3076 arc_buf_hdr_t *hdr = buf->b_hdr;
3077
3078 /*
3079 * Free up the data associated with the buf but only if we're not
3080 * sharing this with the hdr. If we are sharing it with the hdr, the
3081 * hdr is responsible for doing the free.
3082 */
3083 if (buf->b_data != NULL) {
3084 /*
3085 * We're about to change the hdr's b_flags. We must either
3086 * hold the hash_lock or be undiscoverable.
3087 */
3088 ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
3089
3090 arc_cksum_verify(buf);
3091 arc_buf_unwatch(buf);
3092
3093 if (arc_buf_is_shared(buf)) {
3094 arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA);
3095 } else {
3096 uint64_t size = arc_buf_size(buf);
3097 arc_free_data_buf(hdr, buf->b_data, size, buf);
3098 ARCSTAT_INCR(arcstat_overhead_size, -size);
3099 }
3100 buf->b_data = NULL;
3101
3102 /*
3103 * If we have no more encrypted buffers and we've already
3104 * gotten a copy of the decrypted data we can free b_rabd
3105 * to save some space.
3106 */
3107 if (ARC_BUF_ENCRYPTED(buf) && HDR_HAS_RABD(hdr) &&
3108 hdr->b_l1hdr.b_pabd != NULL && !HDR_IO_IN_PROGRESS(hdr)) {
3109 arc_buf_t *b;
3110 for (b = hdr->b_l1hdr.b_buf; b; b = b->b_next) {
3111 if (b != buf && ARC_BUF_ENCRYPTED(b))
3112 break;
3113 }
3114 if (b == NULL)
3115 arc_hdr_free_abd(hdr, B_TRUE);
3116 }
3117 }
3118
3119 arc_buf_t *lastbuf = arc_buf_remove(hdr, buf);
3120
3121 if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) {
3122 /*
3123 * If the current arc_buf_t is sharing its data buffer with the
3124 * hdr, then reassign the hdr's b_pabd to share it with the new
3125 * buffer at the end of the list. The shared buffer is always
3126 * the last one on the hdr's buffer list.
3127 *
3128 * There is an equivalent case for compressed bufs, but since
3129 * they aren't guaranteed to be the last buf in the list and
3130 * that is an exceedingly rare case, we just allow that space be
3131 * wasted temporarily. We must also be careful not to share
3132 * encrypted buffers, since they cannot be shared.
3133 */
3134 if (lastbuf != NULL && !ARC_BUF_ENCRYPTED(lastbuf)) {
3135 /* Only one buf can be shared at once */
3136 VERIFY(!arc_buf_is_shared(lastbuf));
3137 /* hdr is uncompressed so can't have compressed buf */
3138 VERIFY(!ARC_BUF_COMPRESSED(lastbuf));
3139
3140 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
3141 arc_hdr_free_abd(hdr, B_FALSE);
3142
3143 /*
3144 * We must setup a new shared block between the
3145 * last buffer and the hdr. The data would have
3146 * been allocated by the arc buf so we need to transfer
3147 * ownership to the hdr since it's now being shared.
3148 */
3149 arc_share_buf(hdr, lastbuf);
3150 }
3151 } else if (HDR_SHARED_DATA(hdr)) {
3152 /*
3153 * Uncompressed shared buffers are always at the end
3154 * of the list. Compressed buffers don't have the
3155 * same requirements. This makes it hard to
3156 * simply assert that the lastbuf is shared so
3157 * we rely on the hdr's compression flags to determine
3158 * if we have a compressed, shared buffer.
3159 */
3160 ASSERT3P(lastbuf, !=, NULL);
3161 ASSERT(arc_buf_is_shared(lastbuf) ||
3162 arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF);
3163 }
3164
3165 /*
3166 * Free the checksum if we're removing the last uncompressed buf from
3167 * this hdr.
3168 */
3169 if (!arc_hdr_has_uncompressed_buf(hdr)) {
3170 arc_cksum_free(hdr);
3171 }
3172
3173 /* clean up the buf */
3174 buf->b_hdr = NULL;
3175 kmem_cache_free(buf_cache, buf);
3176 }
3177
3178 static void
3179 arc_hdr_alloc_abd(arc_buf_hdr_t *hdr, int alloc_flags)
3180 {
3181 uint64_t size;
3182 boolean_t alloc_rdata = ((alloc_flags & ARC_HDR_ALLOC_RDATA) != 0);
3183
3184 ASSERT3U(HDR_GET_LSIZE(hdr), >, 0);
3185 ASSERT(HDR_HAS_L1HDR(hdr));
3186 ASSERT(!HDR_SHARED_DATA(hdr) || alloc_rdata);
3187 IMPLY(alloc_rdata, HDR_PROTECTED(hdr));
3188
3189 if (alloc_rdata) {
3190 size = HDR_GET_PSIZE(hdr);
3191 ASSERT3P(hdr->b_crypt_hdr.b_rabd, ==, NULL);
3192 hdr->b_crypt_hdr.b_rabd = arc_get_data_abd(hdr, size, hdr,
3193 alloc_flags);
3194 ASSERT3P(hdr->b_crypt_hdr.b_rabd, !=, NULL);
3195 ARCSTAT_INCR(arcstat_raw_size, size);
3196 } else {
3197 size = arc_hdr_size(hdr);
3198 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
3199 hdr->b_l1hdr.b_pabd = arc_get_data_abd(hdr, size, hdr,
3200 alloc_flags);
3201 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
3202 }
3203
3204 ARCSTAT_INCR(arcstat_compressed_size, size);
3205 ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr));
3206 }
3207
3208 static void
3209 arc_hdr_free_abd(arc_buf_hdr_t *hdr, boolean_t free_rdata)
3210 {
3211 uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr);
3212
3213 ASSERT(HDR_HAS_L1HDR(hdr));
3214 ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr));
3215 IMPLY(free_rdata, HDR_HAS_RABD(hdr));
3216
3217 /*
3218 * If the hdr is currently being written to the l2arc then
3219 * we defer freeing the data by adding it to the l2arc_free_on_write
3220 * list. The l2arc will free the data once it's finished
3221 * writing it to the l2arc device.
3222 */
3223 if (HDR_L2_WRITING(hdr)) {
3224 arc_hdr_free_on_write(hdr, free_rdata);
3225 ARCSTAT_BUMP(arcstat_l2_free_on_write);
3226 } else if (free_rdata) {
3227 arc_free_data_abd(hdr, hdr->b_crypt_hdr.b_rabd, size, hdr);
3228 } else {
3229 arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, size, hdr);
3230 }
3231
3232 if (free_rdata) {
3233 hdr->b_crypt_hdr.b_rabd = NULL;
3234 ARCSTAT_INCR(arcstat_raw_size, -size);
3235 } else {
3236 hdr->b_l1hdr.b_pabd = NULL;
3237 }
3238
3239 if (hdr->b_l1hdr.b_pabd == NULL && !HDR_HAS_RABD(hdr))
3240 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS;
3241
3242 ARCSTAT_INCR(arcstat_compressed_size, -size);
3243 ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr));
3244 }
3245
3246 /*
3247 * Allocate empty anonymous ARC header. The header will get its identity
3248 * assigned and buffers attached later as part of read or write operations.
3249 *
3250 * In case of read arc_read() assigns header its identify (b_dva + b_birth),
3251 * inserts it into ARC hash to become globally visible and allocates physical
3252 * (b_pabd) or raw (b_rabd) ABD buffer to read into from disk. On disk read
3253 * completion arc_read_done() allocates ARC buffer(s) as needed, potentially
3254 * sharing one of them with the physical ABD buffer.
3255 *
3256 * In case of write arc_alloc_buf() allocates ARC buffer to be filled with
3257 * data. Then after compression and/or encryption arc_write_ready() allocates
3258 * and fills (or potentially shares) physical (b_pabd) or raw (b_rabd) ABD
3259 * buffer. On disk write completion arc_write_done() assigns the header its
3260 * new identity (b_dva + b_birth) and inserts into ARC hash.
3261 *
3262 * In case of partial overwrite the old data is read first as described. Then
3263 * arc_release() either allocates new anonymous ARC header and moves the ARC
3264 * buffer to it, or reuses the old ARC header by discarding its identity and
3265 * removing it from ARC hash. After buffer modification normal write process
3266 * follows as described.
3267 */
3268 static arc_buf_hdr_t *
3269 arc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize,
3270 boolean_t protected, enum zio_compress compression_type, uint8_t complevel,
3271 arc_buf_contents_t type)
3272 {
3273 arc_buf_hdr_t *hdr;
3274
3275 VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA);
3276 if (protected) {
3277 hdr = kmem_cache_alloc(hdr_full_crypt_cache, KM_PUSHPAGE);
3278 } else {
3279 hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE);
3280 }
3281
3282 ASSERT(HDR_EMPTY(hdr));
3283 #ifdef ZFS_DEBUG
3284 ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL);
3285 #endif
3286 HDR_SET_PSIZE(hdr, psize);
3287 HDR_SET_LSIZE(hdr, lsize);
3288 hdr->b_spa = spa;
3289 hdr->b_type = type;
3290 hdr->b_flags = 0;
3291 arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR);
3292 arc_hdr_set_compress(hdr, compression_type);
3293 hdr->b_complevel = complevel;
3294 if (protected)
3295 arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED);
3296
3297 hdr->b_l1hdr.b_state = arc_anon;
3298 hdr->b_l1hdr.b_arc_access = 0;
3299 hdr->b_l1hdr.b_mru_hits = 0;
3300 hdr->b_l1hdr.b_mru_ghost_hits = 0;
3301 hdr->b_l1hdr.b_mfu_hits = 0;
3302 hdr->b_l1hdr.b_mfu_ghost_hits = 0;
3303 hdr->b_l1hdr.b_buf = NULL;
3304
3305 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
3306
3307 return (hdr);
3308 }
3309
3310 /*
3311 * Transition between the two allocation states for the arc_buf_hdr struct.
3312 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
3313 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
3314 * version is used when a cache buffer is only in the L2ARC in order to reduce
3315 * memory usage.
3316 */
3317 static arc_buf_hdr_t *
3318 arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new)
3319 {
3320 ASSERT(HDR_HAS_L2HDR(hdr));
3321
3322 arc_buf_hdr_t *nhdr;
3323 l2arc_dev_t *dev = hdr->b_l2hdr.b_dev;
3324
3325 ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) ||
3326 (old == hdr_l2only_cache && new == hdr_full_cache));
3327
3328 /*
3329 * if the caller wanted a new full header and the header is to be
3330 * encrypted we will actually allocate the header from the full crypt
3331 * cache instead. The same applies to freeing from the old cache.
3332 */
3333 if (HDR_PROTECTED(hdr) && new == hdr_full_cache)
3334 new = hdr_full_crypt_cache;
3335 if (HDR_PROTECTED(hdr) && old == hdr_full_cache)
3336 old = hdr_full_crypt_cache;
3337
3338 nhdr = kmem_cache_alloc(new, KM_PUSHPAGE);
3339
3340 ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
3341 buf_hash_remove(hdr);
3342
3343 memcpy(nhdr, hdr, HDR_L2ONLY_SIZE);
3344
3345 if (new == hdr_full_cache || new == hdr_full_crypt_cache) {
3346 arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR);
3347 /*
3348 * arc_access and arc_change_state need to be aware that a
3349 * header has just come out of L2ARC, so we set its state to
3350 * l2c_only even though it's about to change.
3351 */
3352 nhdr->b_l1hdr.b_state = arc_l2c_only;
3353
3354 /* Verify previous threads set to NULL before freeing */
3355 ASSERT3P(nhdr->b_l1hdr.b_pabd, ==, NULL);
3356 ASSERT(!HDR_HAS_RABD(hdr));
3357 } else {
3358 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
3359 #ifdef ZFS_DEBUG
3360 ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL);
3361 #endif
3362
3363 /*
3364 * If we've reached here, We must have been called from
3365 * arc_evict_hdr(), as such we should have already been
3366 * removed from any ghost list we were previously on
3367 * (which protects us from racing with arc_evict_state),
3368 * thus no locking is needed during this check.
3369 */
3370 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
3371
3372 /*
3373 * A buffer must not be moved into the arc_l2c_only
3374 * state if it's not finished being written out to the
3375 * l2arc device. Otherwise, the b_l1hdr.b_pabd field
3376 * might try to be accessed, even though it was removed.
3377 */
3378 VERIFY(!HDR_L2_WRITING(hdr));
3379 VERIFY3P(hdr->b_l1hdr.b_pabd, ==, NULL);
3380 ASSERT(!HDR_HAS_RABD(hdr));
3381
3382 arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR);
3383 }
3384 /*
3385 * The header has been reallocated so we need to re-insert it into any
3386 * lists it was on.
3387 */
3388 (void) buf_hash_insert(nhdr, NULL);
3389
3390 ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node));
3391
3392 mutex_enter(&dev->l2ad_mtx);
3393
3394 /*
3395 * We must place the realloc'ed header back into the list at
3396 * the same spot. Otherwise, if it's placed earlier in the list,
3397 * l2arc_write_buffers() could find it during the function's
3398 * write phase, and try to write it out to the l2arc.
3399 */
3400 list_insert_after(&dev->l2ad_buflist, hdr, nhdr);
3401 list_remove(&dev->l2ad_buflist, hdr);
3402
3403 mutex_exit(&dev->l2ad_mtx);
3404
3405 /*
3406 * Since we're using the pointer address as the tag when
3407 * incrementing and decrementing the l2ad_alloc refcount, we
3408 * must remove the old pointer (that we're about to destroy) and
3409 * add the new pointer to the refcount. Otherwise we'd remove
3410 * the wrong pointer address when calling arc_hdr_destroy() later.
3411 */
3412
3413 (void) zfs_refcount_remove_many(&dev->l2ad_alloc,
3414 arc_hdr_size(hdr), hdr);
3415 (void) zfs_refcount_add_many(&dev->l2ad_alloc,
3416 arc_hdr_size(nhdr), nhdr);
3417
3418 buf_discard_identity(hdr);
3419 kmem_cache_free(old, hdr);
3420
3421 return (nhdr);
3422 }
3423
3424 /*
3425 * This function allows an L1 header to be reallocated as a crypt
3426 * header and vice versa. If we are going to a crypt header, the
3427 * new fields will be zeroed out.
3428 */
3429 static arc_buf_hdr_t *
3430 arc_hdr_realloc_crypt(arc_buf_hdr_t *hdr, boolean_t need_crypt)
3431 {
3432 arc_buf_hdr_t *nhdr;
3433 arc_buf_t *buf;
3434 kmem_cache_t *ncache, *ocache;
3435
3436 /*
3437 * This function requires that hdr is in the arc_anon state.
3438 * Therefore it won't have any L2ARC data for us to worry
3439 * about copying.
3440 */
3441 ASSERT(HDR_HAS_L1HDR(hdr));
3442 ASSERT(!HDR_HAS_L2HDR(hdr));
3443 ASSERT3U(!!HDR_PROTECTED(hdr), !=, need_crypt);
3444 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon);
3445 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
3446 ASSERT(!list_link_active(&hdr->b_l2hdr.b_l2node));
3447 ASSERT3P(hdr->b_hash_next, ==, NULL);
3448
3449 if (need_crypt) {
3450 ncache = hdr_full_crypt_cache;
3451 ocache = hdr_full_cache;
3452 } else {
3453 ncache = hdr_full_cache;
3454 ocache = hdr_full_crypt_cache;
3455 }
3456
3457 nhdr = kmem_cache_alloc(ncache, KM_PUSHPAGE);
3458
3459 /*
3460 * Copy all members that aren't locks or condvars to the new header.
3461 * No lists are pointing to us (as we asserted above), so we don't
3462 * need to worry about the list nodes.
3463 */
3464 nhdr->b_dva = hdr->b_dva;
3465 nhdr->b_birth = hdr->b_birth;
3466 nhdr->b_type = hdr->b_type;
3467 nhdr->b_flags = hdr->b_flags;
3468 nhdr->b_psize = hdr->b_psize;
3469 nhdr->b_lsize = hdr->b_lsize;
3470 nhdr->b_spa = hdr->b_spa;
3471 #ifdef ZFS_DEBUG
3472 nhdr->b_l1hdr.b_freeze_cksum = hdr->b_l1hdr.b_freeze_cksum;
3473 #endif
3474 nhdr->b_l1hdr.b_byteswap = hdr->b_l1hdr.b_byteswap;
3475 nhdr->b_l1hdr.b_state = hdr->b_l1hdr.b_state;
3476 nhdr->b_l1hdr.b_arc_access = hdr->b_l1hdr.b_arc_access;
3477 nhdr->b_l1hdr.b_mru_hits = hdr->b_l1hdr.b_mru_hits;
3478 nhdr->b_l1hdr.b_mru_ghost_hits = hdr->b_l1hdr.b_mru_ghost_hits;
3479 nhdr->b_l1hdr.b_mfu_hits = hdr->b_l1hdr.b_mfu_hits;
3480 nhdr->b_l1hdr.b_mfu_ghost_hits = hdr->b_l1hdr.b_mfu_ghost_hits;
3481 nhdr->b_l1hdr.b_acb = hdr->b_l1hdr.b_acb;
3482 nhdr->b_l1hdr.b_pabd = hdr->b_l1hdr.b_pabd;
3483
3484 /*
3485 * This zfs_refcount_add() exists only to ensure that the individual
3486 * arc buffers always point to a header that is referenced, avoiding
3487 * a small race condition that could trigger ASSERTs.
3488 */
3489 (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, FTAG);
3490 nhdr->b_l1hdr.b_buf = hdr->b_l1hdr.b_buf;
3491 for (buf = nhdr->b_l1hdr.b_buf; buf != NULL; buf = buf->b_next)
3492 buf->b_hdr = nhdr;
3493
3494 zfs_refcount_transfer(&nhdr->b_l1hdr.b_refcnt, &hdr->b_l1hdr.b_refcnt);
3495 (void) zfs_refcount_remove(&nhdr->b_l1hdr.b_refcnt, FTAG);
3496 ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt));
3497
3498 if (need_crypt) {
3499 arc_hdr_set_flags(nhdr, ARC_FLAG_PROTECTED);
3500 } else {
3501 arc_hdr_clear_flags(nhdr, ARC_FLAG_PROTECTED);
3502 }
3503
3504 /* unset all members of the original hdr */
3505 memset(&hdr->b_dva, 0, sizeof (dva_t));
3506 hdr->b_birth = 0;
3507 hdr->b_type = 0;
3508 hdr->b_flags = 0;
3509 hdr->b_psize = 0;
3510 hdr->b_lsize = 0;
3511 hdr->b_spa = 0;
3512 #ifdef ZFS_DEBUG
3513 hdr->b_l1hdr.b_freeze_cksum = NULL;
3514 #endif
3515 hdr->b_l1hdr.b_buf = NULL;
3516 hdr->b_l1hdr.b_byteswap = 0;
3517 hdr->b_l1hdr.b_state = NULL;
3518 hdr->b_l1hdr.b_arc_access = 0;
3519 hdr->b_l1hdr.b_mru_hits = 0;
3520 hdr->b_l1hdr.b_mru_ghost_hits = 0;
3521 hdr->b_l1hdr.b_mfu_hits = 0;
3522 hdr->b_l1hdr.b_mfu_ghost_hits = 0;
3523 hdr->b_l1hdr.b_acb = NULL;
3524 hdr->b_l1hdr.b_pabd = NULL;
3525
3526 if (ocache == hdr_full_crypt_cache) {
3527 ASSERT(!HDR_HAS_RABD(hdr));
3528 hdr->b_crypt_hdr.b_ot = DMU_OT_NONE;
3529 hdr->b_crypt_hdr.b_dsobj = 0;
3530 memset(hdr->b_crypt_hdr.b_salt, 0, ZIO_DATA_SALT_LEN);
3531 memset(hdr->b_crypt_hdr.b_iv, 0, ZIO_DATA_IV_LEN);
3532 memset(hdr->b_crypt_hdr.b_mac, 0, ZIO_DATA_MAC_LEN);
3533 }
3534
3535 buf_discard_identity(hdr);
3536 kmem_cache_free(ocache, hdr);
3537
3538 return (nhdr);
3539 }
3540
3541 /*
3542 * This function is used by the send / receive code to convert a newly
3543 * allocated arc_buf_t to one that is suitable for a raw encrypted write. It
3544 * is also used to allow the root objset block to be updated without altering
3545 * its embedded MACs. Both block types will always be uncompressed so we do not
3546 * have to worry about compression type or psize.
3547 */
3548 void
3549 arc_convert_to_raw(arc_buf_t *buf, uint64_t dsobj, boolean_t byteorder,
3550 dmu_object_type_t ot, const uint8_t *salt, const uint8_t *iv,
3551 const uint8_t *mac)
3552 {
3553 arc_buf_hdr_t *hdr = buf->b_hdr;
3554
3555 ASSERT(ot == DMU_OT_DNODE || ot == DMU_OT_OBJSET);
3556 ASSERT(HDR_HAS_L1HDR(hdr));
3557 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon);
3558
3559 buf->b_flags |= (ARC_BUF_FLAG_COMPRESSED | ARC_BUF_FLAG_ENCRYPTED);
3560 if (!HDR_PROTECTED(hdr))
3561 hdr = arc_hdr_realloc_crypt(hdr, B_TRUE);
3562 hdr->b_crypt_hdr.b_dsobj = dsobj;
3563 hdr->b_crypt_hdr.b_ot = ot;
3564 hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ?
3565 DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot);
3566 if (!arc_hdr_has_uncompressed_buf(hdr))
3567 arc_cksum_free(hdr);
3568
3569 if (salt != NULL)
3570 memcpy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN);
3571 if (iv != NULL)
3572 memcpy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN);
3573 if (mac != NULL)
3574 memcpy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN);
3575 }
3576
3577 /*
3578 * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller.
3579 * The buf is returned thawed since we expect the consumer to modify it.
3580 */
3581 arc_buf_t *
3582 arc_alloc_buf(spa_t *spa, const void *tag, arc_buf_contents_t type,
3583 int32_t size)
3584 {
3585 arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size,
3586 B_FALSE, ZIO_COMPRESS_OFF, 0, type);
3587
3588 arc_buf_t *buf = NULL;
3589 VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, B_FALSE,
3590 B_FALSE, B_FALSE, &buf));
3591 arc_buf_thaw(buf);
3592
3593 return (buf);
3594 }
3595
3596 /*
3597 * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this
3598 * for bufs containing metadata.
3599 */
3600 arc_buf_t *
3601 arc_alloc_compressed_buf(spa_t *spa, const void *tag, uint64_t psize,
3602 uint64_t lsize, enum zio_compress compression_type, uint8_t complevel)
3603 {
3604 ASSERT3U(lsize, >, 0);
3605 ASSERT3U(lsize, >=, psize);
3606 ASSERT3U(compression_type, >, ZIO_COMPRESS_OFF);
3607 ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS);
3608
3609 arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize,
3610 B_FALSE, compression_type, complevel, ARC_BUFC_DATA);
3611
3612 arc_buf_t *buf = NULL;
3613 VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE,
3614 B_TRUE, B_FALSE, B_FALSE, &buf));
3615 arc_buf_thaw(buf);
3616
3617 /*
3618 * To ensure that the hdr has the correct data in it if we call
3619 * arc_untransform() on this buf before it's been written to disk,
3620 * it's easiest if we just set up sharing between the buf and the hdr.
3621 */
3622 arc_share_buf(hdr, buf);
3623
3624 return (buf);
3625 }
3626
3627 arc_buf_t *
3628 arc_alloc_raw_buf(spa_t *spa, const void *tag, uint64_t dsobj,
3629 boolean_t byteorder, const uint8_t *salt, const uint8_t *iv,
3630 const uint8_t *mac, dmu_object_type_t ot, uint64_t psize, uint64_t lsize,
3631 enum zio_compress compression_type, uint8_t complevel)
3632 {
3633 arc_buf_hdr_t *hdr;
3634 arc_buf_t *buf;
3635 arc_buf_contents_t type = DMU_OT_IS_METADATA(ot) ?
3636 ARC_BUFC_METADATA : ARC_BUFC_DATA;
3637
3638 ASSERT3U(lsize, >, 0);
3639 ASSERT3U(lsize, >=, psize);
3640 ASSERT3U(compression_type, >=, ZIO_COMPRESS_OFF);
3641 ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS);
3642
3643 hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, B_TRUE,
3644 compression_type, complevel, type);
3645
3646 hdr->b_crypt_hdr.b_dsobj = dsobj;
3647 hdr->b_crypt_hdr.b_ot = ot;
3648 hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ?
3649 DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot);
3650 memcpy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN);
3651 memcpy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN);
3652 memcpy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN);
3653
3654 /*
3655 * This buffer will be considered encrypted even if the ot is not an
3656 * encrypted type. It will become authenticated instead in
3657 * arc_write_ready().
3658 */
3659 buf = NULL;
3660 VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_TRUE, B_TRUE,
3661 B_FALSE, B_FALSE, &buf));
3662 arc_buf_thaw(buf);
3663
3664 return (buf);
3665 }
3666
3667 static void
3668 l2arc_hdr_arcstats_update(arc_buf_hdr_t *hdr, boolean_t incr,
3669 boolean_t state_only)
3670 {
3671 l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr;
3672 l2arc_dev_t *dev = l2hdr->b_dev;
3673 uint64_t lsize = HDR_GET_LSIZE(hdr);
3674 uint64_t psize = HDR_GET_PSIZE(hdr);
3675 uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, psize);
3676 arc_buf_contents_t type = hdr->b_type;
3677 int64_t lsize_s;
3678 int64_t psize_s;
3679 int64_t asize_s;
3680
3681 if (incr) {
3682 lsize_s = lsize;
3683 psize_s = psize;
3684 asize_s = asize;
3685 } else {
3686 lsize_s = -lsize;
3687 psize_s = -psize;
3688 asize_s = -asize;
3689 }
3690
3691 /* If the buffer is a prefetch, count it as such. */
3692 if (HDR_PREFETCH(hdr)) {
3693 ARCSTAT_INCR(arcstat_l2_prefetch_asize, asize_s);
3694 } else {
3695 /*
3696 * We use the value stored in the L2 header upon initial
3697 * caching in L2ARC. This value will be updated in case
3698 * an MRU/MRU_ghost buffer transitions to MFU but the L2ARC
3699 * metadata (log entry) cannot currently be updated. Having
3700 * the ARC state in the L2 header solves the problem of a
3701 * possibly absent L1 header (apparent in buffers restored
3702 * from persistent L2ARC).
3703 */
3704 switch (hdr->b_l2hdr.b_arcs_state) {
3705 case ARC_STATE_MRU_GHOST:
3706 case ARC_STATE_MRU:
3707 ARCSTAT_INCR(arcstat_l2_mru_asize, asize_s);
3708 break;
3709 case ARC_STATE_MFU_GHOST:
3710 case ARC_STATE_MFU:
3711 ARCSTAT_INCR(arcstat_l2_mfu_asize, asize_s);
3712 break;
3713 default:
3714 break;
3715 }
3716 }
3717
3718 if (state_only)
3719 return;
3720
3721 ARCSTAT_INCR(arcstat_l2_psize, psize_s);
3722 ARCSTAT_INCR(arcstat_l2_lsize, lsize_s);
3723
3724 switch (type) {
3725 case ARC_BUFC_DATA:
3726 ARCSTAT_INCR(arcstat_l2_bufc_data_asize, asize_s);
3727 break;
3728 case ARC_BUFC_METADATA:
3729 ARCSTAT_INCR(arcstat_l2_bufc_metadata_asize, asize_s);
3730 break;
3731 default:
3732 break;
3733 }
3734 }
3735
3736
3737 static void
3738 arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr)
3739 {
3740 l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr;
3741 l2arc_dev_t *dev = l2hdr->b_dev;
3742 uint64_t psize = HDR_GET_PSIZE(hdr);
3743 uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, psize);
3744
3745 ASSERT(MUTEX_HELD(&dev->l2ad_mtx));
3746 ASSERT(HDR_HAS_L2HDR(hdr));
3747
3748 list_remove(&dev->l2ad_buflist, hdr);
3749
3750 l2arc_hdr_arcstats_decrement(hdr);
3751 vdev_space_update(dev->l2ad_vdev, -asize, 0, 0);
3752
3753 (void) zfs_refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr),
3754 hdr);
3755 arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR);
3756 }
3757
3758 static void
3759 arc_hdr_destroy(arc_buf_hdr_t *hdr)
3760 {
3761 if (HDR_HAS_L1HDR(hdr)) {
3762 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
3763 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon);
3764 }
3765 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3766 ASSERT(!HDR_IN_HASH_TABLE(hdr));
3767
3768 if (HDR_HAS_L2HDR(hdr)) {
3769 l2arc_dev_t *dev = hdr->b_l2hdr.b_dev;
3770 boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx);
3771
3772 if (!buflist_held)
3773 mutex_enter(&dev->l2ad_mtx);
3774
3775 /*
3776 * Even though we checked this conditional above, we
3777 * need to check this again now that we have the
3778 * l2ad_mtx. This is because we could be racing with
3779 * another thread calling l2arc_evict() which might have
3780 * destroyed this header's L2 portion as we were waiting
3781 * to acquire the l2ad_mtx. If that happens, we don't
3782 * want to re-destroy the header's L2 portion.
3783 */
3784 if (HDR_HAS_L2HDR(hdr)) {
3785
3786 if (!HDR_EMPTY(hdr))
3787 buf_discard_identity(hdr);
3788
3789 arc_hdr_l2hdr_destroy(hdr);
3790 }
3791
3792 if (!buflist_held)
3793 mutex_exit(&dev->l2ad_mtx);
3794 }
3795
3796 /*
3797 * The header's identify can only be safely discarded once it is no
3798 * longer discoverable. This requires removing it from the hash table
3799 * and the l2arc header list. After this point the hash lock can not
3800 * be used to protect the header.
3801 */
3802 if (!HDR_EMPTY(hdr))
3803 buf_discard_identity(hdr);
3804
3805 if (HDR_HAS_L1HDR(hdr)) {
3806 arc_cksum_free(hdr);
3807
3808 while (hdr->b_l1hdr.b_buf != NULL)
3809 arc_buf_destroy_impl(hdr->b_l1hdr.b_buf);
3810
3811 if (hdr->b_l1hdr.b_pabd != NULL)
3812 arc_hdr_free_abd(hdr, B_FALSE);
3813
3814 if (HDR_HAS_RABD(hdr))
3815 arc_hdr_free_abd(hdr, B_TRUE);
3816 }
3817
3818 ASSERT3P(hdr->b_hash_next, ==, NULL);
3819 if (HDR_HAS_L1HDR(hdr)) {
3820 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
3821 ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL);
3822 #ifdef ZFS_DEBUG
3823 ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL);
3824 #endif
3825
3826 if (!HDR_PROTECTED(hdr)) {
3827 kmem_cache_free(hdr_full_cache, hdr);
3828 } else {
3829 kmem_cache_free(hdr_full_crypt_cache, hdr);
3830 }
3831 } else {
3832 kmem_cache_free(hdr_l2only_cache, hdr);
3833 }
3834 }
3835
3836 void
3837 arc_buf_destroy(arc_buf_t *buf, const void *tag)
3838 {
3839 arc_buf_hdr_t *hdr = buf->b_hdr;
3840
3841 if (hdr->b_l1hdr.b_state == arc_anon) {
3842 ASSERT3P(hdr->b_l1hdr.b_buf, ==, buf);
3843 ASSERT(ARC_BUF_LAST(buf));
3844 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3845 VERIFY0(remove_reference(hdr, tag));
3846 return;
3847 }
3848
3849 kmutex_t *hash_lock = HDR_LOCK(hdr);
3850 mutex_enter(hash_lock);
3851
3852 ASSERT3P(hdr, ==, buf->b_hdr);
3853 ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL);
3854 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3855 ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon);
3856 ASSERT3P(buf->b_data, !=, NULL);
3857
3858 arc_buf_destroy_impl(buf);
3859 (void) remove_reference(hdr, tag);
3860 mutex_exit(hash_lock);
3861 }
3862
3863 /*
3864 * Evict the arc_buf_hdr that is provided as a parameter. The resultant
3865 * state of the header is dependent on its state prior to entering this
3866 * function. The following transitions are possible:
3867 *
3868 * - arc_mru -> arc_mru_ghost
3869 * - arc_mfu -> arc_mfu_ghost
3870 * - arc_mru_ghost -> arc_l2c_only
3871 * - arc_mru_ghost -> deleted
3872 * - arc_mfu_ghost -> arc_l2c_only
3873 * - arc_mfu_ghost -> deleted
3874 * - arc_uncached -> deleted
3875 *
3876 * Return total size of evicted data buffers for eviction progress tracking.
3877 * When evicting from ghost states return logical buffer size to make eviction
3878 * progress at the same (or at least comparable) rate as from non-ghost states.
3879 *
3880 * Return *real_evicted for actual ARC size reduction to wake up threads
3881 * waiting for it. For non-ghost states it includes size of evicted data
3882 * buffers (the headers are not freed there). For ghost states it includes
3883 * only the evicted headers size.
3884 */
3885 static int64_t
3886 arc_evict_hdr(arc_buf_hdr_t *hdr, uint64_t *real_evicted)
3887 {
3888 arc_state_t *evicted_state, *state;
3889 int64_t bytes_evicted = 0;
3890 uint_t min_lifetime = HDR_PRESCIENT_PREFETCH(hdr) ?
3891 arc_min_prescient_prefetch_ms : arc_min_prefetch_ms;
3892
3893 ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
3894 ASSERT(HDR_HAS_L1HDR(hdr));
3895 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3896 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
3897 ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt));
3898
3899 *real_evicted = 0;
3900 state = hdr->b_l1hdr.b_state;
3901 if (GHOST_STATE(state)) {
3902
3903 /*
3904 * l2arc_write_buffers() relies on a header's L1 portion
3905 * (i.e. its b_pabd field) during it's write phase.
3906 * Thus, we cannot push a header onto the arc_l2c_only
3907 * state (removing its L1 piece) until the header is
3908 * done being written to the l2arc.
3909 */
3910 if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) {
3911 ARCSTAT_BUMP(arcstat_evict_l2_skip);
3912 return (bytes_evicted);
3913 }
3914
3915 ARCSTAT_BUMP(arcstat_deleted);
3916 bytes_evicted += HDR_GET_LSIZE(hdr);
3917
3918 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr);
3919
3920 if (HDR_HAS_L2HDR(hdr)) {
3921 ASSERT(hdr->b_l1hdr.b_pabd == NULL);
3922 ASSERT(!HDR_HAS_RABD(hdr));
3923 /*
3924 * This buffer is cached on the 2nd Level ARC;
3925 * don't destroy the header.
3926 */
3927 arc_change_state(arc_l2c_only, hdr);
3928 /*
3929 * dropping from L1+L2 cached to L2-only,
3930 * realloc to remove the L1 header.
3931 */
3932 (void) arc_hdr_realloc(hdr, hdr_full_cache,
3933 hdr_l2only_cache);
3934 *real_evicted += HDR_FULL_SIZE - HDR_L2ONLY_SIZE;
3935 } else {
3936 arc_change_state(arc_anon, hdr);
3937 arc_hdr_destroy(hdr);
3938 *real_evicted += HDR_FULL_SIZE;
3939 }
3940 return (bytes_evicted);
3941 }
3942
3943 ASSERT(state == arc_mru || state == arc_mfu || state == arc_uncached);
3944 evicted_state = (state == arc_uncached) ? arc_anon :
3945 ((state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost);
3946
3947 /* prefetch buffers have a minimum lifespan */
3948 if ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) &&
3949 ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access <
3950 MSEC_TO_TICK(min_lifetime)) {
3951 ARCSTAT_BUMP(arcstat_evict_skip);
3952 return (bytes_evicted);
3953 }
3954
3955 if (HDR_HAS_L2HDR(hdr)) {
3956 ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr));
3957 } else {
3958 if (l2arc_write_eligible(hdr->b_spa, hdr)) {
3959 ARCSTAT_INCR(arcstat_evict_l2_eligible,
3960 HDR_GET_LSIZE(hdr));
3961
3962 switch (state->arcs_state) {
3963 case ARC_STATE_MRU:
3964 ARCSTAT_INCR(
3965 arcstat_evict_l2_eligible_mru,
3966 HDR_GET_LSIZE(hdr));
3967 break;
3968 case ARC_STATE_MFU:
3969 ARCSTAT_INCR(
3970 arcstat_evict_l2_eligible_mfu,
3971 HDR_GET_LSIZE(hdr));
3972 break;
3973 default:
3974 break;
3975 }
3976 } else {
3977 ARCSTAT_INCR(arcstat_evict_l2_ineligible,
3978 HDR_GET_LSIZE(hdr));
3979 }
3980 }
3981
3982 bytes_evicted += arc_hdr_size(hdr);
3983 *real_evicted += arc_hdr_size(hdr);
3984
3985 /*
3986 * If this hdr is being evicted and has a compressed buffer then we
3987 * discard it here before we change states. This ensures that the
3988 * accounting is updated correctly in arc_free_data_impl().
3989 */
3990 if (hdr->b_l1hdr.b_pabd != NULL)
3991 arc_hdr_free_abd(hdr, B_FALSE);
3992
3993 if (HDR_HAS_RABD(hdr))
3994 arc_hdr_free_abd(hdr, B_TRUE);
3995
3996 arc_change_state(evicted_state, hdr);
3997 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr);
3998 if (evicted_state == arc_anon) {
3999 arc_hdr_destroy(hdr);
4000 *real_evicted += HDR_FULL_SIZE;
4001 } else {
4002 ASSERT(HDR_IN_HASH_TABLE(hdr));
4003 }
4004
4005 return (bytes_evicted);
4006 }
4007
4008 static void
4009 arc_set_need_free(void)
4010 {
4011 ASSERT(MUTEX_HELD(&arc_evict_lock));
4012 int64_t remaining = arc_free_memory() - arc_sys_free / 2;
4013 arc_evict_waiter_t *aw = list_tail(&arc_evict_waiters);
4014 if (aw == NULL) {
4015 arc_need_free = MAX(-remaining, 0);
4016 } else {
4017 arc_need_free =
4018 MAX(-remaining, (int64_t)(aw->aew_count - arc_evict_count));
4019 }
4020 }
4021
4022 static uint64_t
4023 arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker,
4024 uint64_t spa, uint64_t bytes)
4025 {
4026 multilist_sublist_t *mls;
4027 uint64_t bytes_evicted = 0, real_evicted = 0;
4028 arc_buf_hdr_t *hdr;
4029 kmutex_t *hash_lock;
4030 uint_t evict_count = zfs_arc_evict_batch_limit;
4031
4032 ASSERT3P(marker, !=, NULL);
4033
4034 mls = multilist_sublist_lock(ml, idx);
4035
4036 for (hdr = multilist_sublist_prev(mls, marker); likely(hdr != NULL);
4037 hdr = multilist_sublist_prev(mls, marker)) {
4038 if ((evict_count == 0) || (bytes_evicted >= bytes))
4039 break;
4040
4041 /*
4042 * To keep our iteration location, move the marker
4043 * forward. Since we're not holding hdr's hash lock, we
4044 * must be very careful and not remove 'hdr' from the
4045 * sublist. Otherwise, other consumers might mistake the
4046 * 'hdr' as not being on a sublist when they call the
4047 * multilist_link_active() function (they all rely on
4048 * the hash lock protecting concurrent insertions and
4049 * removals). multilist_sublist_move_forward() was
4050 * specifically implemented to ensure this is the case
4051 * (only 'marker' will be removed and re-inserted).
4052 */
4053 multilist_sublist_move_forward(mls, marker);
4054
4055 /*
4056 * The only case where the b_spa field should ever be
4057 * zero, is the marker headers inserted by
4058 * arc_evict_state(). It's possible for multiple threads
4059 * to be calling arc_evict_state() concurrently (e.g.
4060 * dsl_pool_close() and zio_inject_fault()), so we must
4061 * skip any markers we see from these other threads.
4062 */
4063 if (hdr->b_spa == 0)
4064 continue;
4065
4066 /* we're only interested in evicting buffers of a certain spa */
4067 if (spa != 0 && hdr->b_spa != spa) {
4068 ARCSTAT_BUMP(arcstat_evict_skip);
4069 continue;
4070 }
4071
4072 hash_lock = HDR_LOCK(hdr);
4073
4074 /*
4075 * We aren't calling this function from any code path
4076 * that would already be holding a hash lock, so we're
4077 * asserting on this assumption to be defensive in case
4078 * this ever changes. Without this check, it would be
4079 * possible to incorrectly increment arcstat_mutex_miss
4080 * below (e.g. if the code changed such that we called
4081 * this function with a hash lock held).
4082 */
4083 ASSERT(!MUTEX_HELD(hash_lock));
4084
4085 if (mutex_tryenter(hash_lock)) {
4086 uint64_t revicted;
4087 uint64_t evicted = arc_evict_hdr(hdr, &revicted);
4088 mutex_exit(hash_lock);
4089
4090 bytes_evicted += evicted;
4091 real_evicted += revicted;
4092
4093 /*
4094 * If evicted is zero, arc_evict_hdr() must have
4095 * decided to skip this header, don't increment
4096 * evict_count in this case.
4097 */
4098 if (evicted != 0)
4099 evict_count--;
4100
4101 } else {
4102 ARCSTAT_BUMP(arcstat_mutex_miss);
4103 }
4104 }
4105
4106 multilist_sublist_unlock(mls);
4107
4108 /*
4109 * Increment the count of evicted bytes, and wake up any threads that
4110 * are waiting for the count to reach this value. Since the list is
4111 * ordered by ascending aew_count, we pop off the beginning of the
4112 * list until we reach the end, or a waiter that's past the current
4113 * "count". Doing this outside the loop reduces the number of times
4114 * we need to acquire the global arc_evict_lock.
4115 *
4116 * Only wake when there's sufficient free memory in the system
4117 * (specifically, arc_sys_free/2, which by default is a bit more than
4118 * 1/64th of RAM). See the comments in arc_wait_for_eviction().
4119 */
4120 mutex_enter(&arc_evict_lock);
4121 arc_evict_count += real_evicted;
4122
4123 if (arc_free_memory() > arc_sys_free / 2) {
4124 arc_evict_waiter_t *aw;
4125 while ((aw = list_head(&arc_evict_waiters)) != NULL &&
4126 aw->aew_count <= arc_evict_count) {
4127 list_remove(&arc_evict_waiters, aw);
4128 cv_broadcast(&aw->aew_cv);
4129 }
4130 }
4131 arc_set_need_free();
4132 mutex_exit(&arc_evict_lock);
4133
4134 /*
4135 * If the ARC size is reduced from arc_c_max to arc_c_min (especially
4136 * if the average cached block is small), eviction can be on-CPU for
4137 * many seconds. To ensure that other threads that may be bound to
4138 * this CPU are able to make progress, make a voluntary preemption
4139 * call here.
4140 */
4141 kpreempt(KPREEMPT_SYNC);
4142
4143 return (bytes_evicted);
4144 }
4145
4146 /*
4147 * Allocate an array of buffer headers used as placeholders during arc state
4148 * eviction.
4149 */
4150 static arc_buf_hdr_t **
4151 arc_state_alloc_markers(int count)
4152 {
4153 arc_buf_hdr_t **markers;
4154
4155 markers = kmem_zalloc(sizeof (*markers) * count, KM_SLEEP);
4156 for (int i = 0; i < count; i++) {
4157 markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP);
4158
4159 /*
4160 * A b_spa of 0 is used to indicate that this header is
4161 * a marker. This fact is used in arc_evict_state_impl().
4162 */
4163 markers[i]->b_spa = 0;
4164
4165 }
4166 return (markers);
4167 }
4168
4169 static void
4170 arc_state_free_markers(arc_buf_hdr_t **markers, int count)
4171 {
4172 for (int i = 0; i < count; i++)
4173 kmem_cache_free(hdr_full_cache, markers[i]);
4174 kmem_free(markers, sizeof (*markers) * count);
4175 }
4176
4177 /*
4178 * Evict buffers from the given arc state, until we've removed the
4179 * specified number of bytes. Move the removed buffers to the
4180 * appropriate evict state.
4181 *
4182 * This function makes a "best effort". It skips over any buffers
4183 * it can't get a hash_lock on, and so, may not catch all candidates.
4184 * It may also return without evicting as much space as requested.
4185 *
4186 * If bytes is specified using the special value ARC_EVICT_ALL, this
4187 * will evict all available (i.e. unlocked and evictable) buffers from
4188 * the given arc state; which is used by arc_flush().
4189 */
4190 static uint64_t
4191 arc_evict_state(arc_state_t *state, arc_buf_contents_t type, uint64_t spa,
4192 uint64_t bytes)
4193 {
4194 uint64_t total_evicted = 0;
4195 multilist_t *ml = &state->arcs_list[type];
4196 int num_sublists;
4197 arc_buf_hdr_t **markers;
4198
4199 num_sublists = multilist_get_num_sublists(ml);
4200
4201 /*
4202 * If we've tried to evict from each sublist, made some
4203 * progress, but still have not hit the target number of bytes
4204 * to evict, we want to keep trying. The markers allow us to
4205 * pick up where we left off for each individual sublist, rather
4206 * than starting from the tail each time.
4207 */
4208 if (zthr_iscurthread(arc_evict_zthr)) {
4209 markers = arc_state_evict_markers;
4210 ASSERT3S(num_sublists, <=, arc_state_evict_marker_count);
4211 } else {
4212 markers = arc_state_alloc_markers(num_sublists);
4213 }
4214 for (int i = 0; i < num_sublists; i++) {
4215 multilist_sublist_t *mls;
4216
4217 mls = multilist_sublist_lock(ml, i);
4218 multilist_sublist_insert_tail(mls, markers[i]);
4219 multilist_sublist_unlock(mls);
4220 }
4221
4222 /*
4223 * While we haven't hit our target number of bytes to evict, or
4224 * we're evicting all available buffers.
4225 */
4226 while (total_evicted < bytes) {
4227 int sublist_idx = multilist_get_random_index(ml);
4228 uint64_t scan_evicted = 0;
4229
4230 /*
4231 * Start eviction using a randomly selected sublist,
4232 * this is to try and evenly balance eviction across all
4233 * sublists. Always starting at the same sublist
4234 * (e.g. index 0) would cause evictions to favor certain
4235 * sublists over others.
4236 */
4237 for (int i = 0; i < num_sublists; i++) {
4238 uint64_t bytes_remaining;
4239 uint64_t bytes_evicted;
4240
4241 if (total_evicted < bytes)
4242 bytes_remaining = bytes - total_evicted;
4243 else
4244 break;
4245
4246 bytes_evicted = arc_evict_state_impl(ml, sublist_idx,
4247 markers[sublist_idx], spa, bytes_remaining);
4248
4249 scan_evicted += bytes_evicted;
4250 total_evicted += bytes_evicted;
4251
4252 /* we've reached the end, wrap to the beginning */
4253 if (++sublist_idx >= num_sublists)
4254 sublist_idx = 0;
4255 }
4256
4257 /*
4258 * If we didn't evict anything during this scan, we have
4259 * no reason to believe we'll evict more during another
4260 * scan, so break the loop.
4261 */
4262 if (scan_evicted == 0) {
4263 /* This isn't possible, let's make that obvious */
4264 ASSERT3S(bytes, !=, 0);
4265
4266 /*
4267 * When bytes is ARC_EVICT_ALL, the only way to
4268 * break the loop is when scan_evicted is zero.
4269 * In that case, we actually have evicted enough,
4270 * so we don't want to increment the kstat.
4271 */
4272 if (bytes != ARC_EVICT_ALL) {
4273 ASSERT3S(total_evicted, <, bytes);
4274 ARCSTAT_BUMP(arcstat_evict_not_enough);
4275 }
4276
4277 break;
4278 }
4279 }
4280
4281 for (int i = 0; i < num_sublists; i++) {
4282 multilist_sublist_t *mls = multilist_sublist_lock(ml, i);
4283 multilist_sublist_remove(mls, markers[i]);
4284 multilist_sublist_unlock(mls);
4285 }
4286 if (markers != arc_state_evict_markers)
4287 arc_state_free_markers(markers, num_sublists);
4288
4289 return (total_evicted);
4290 }
4291
4292 /*
4293 * Flush all "evictable" data of the given type from the arc state
4294 * specified. This will not evict any "active" buffers (i.e. referenced).
4295 *
4296 * When 'retry' is set to B_FALSE, the function will make a single pass
4297 * over the state and evict any buffers that it can. Since it doesn't
4298 * continually retry the eviction, it might end up leaving some buffers
4299 * in the ARC due to lock misses.
4300 *
4301 * When 'retry' is set to B_TRUE, the function will continually retry the
4302 * eviction until *all* evictable buffers have been removed from the
4303 * state. As a result, if concurrent insertions into the state are
4304 * allowed (e.g. if the ARC isn't shutting down), this function might
4305 * wind up in an infinite loop, continually trying to evict buffers.
4306 */
4307 static uint64_t
4308 arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type,
4309 boolean_t retry)
4310 {
4311 uint64_t evicted = 0;
4312
4313 while (zfs_refcount_count(&state->arcs_esize[type]) != 0) {
4314 evicted += arc_evict_state(state, type, spa, ARC_EVICT_ALL);
4315
4316 if (!retry)
4317 break;
4318 }
4319
4320 return (evicted);
4321 }
4322
4323 /*
4324 * Evict the specified number of bytes from the state specified. This
4325 * function prevents us from trying to evict more from a state's list
4326 * than is "evictable", and to skip evicting altogether when passed a
4327 * negative value for "bytes". In contrast, arc_evict_state() will
4328 * evict everything it can, when passed a negative value for "bytes".
4329 */
4330 static uint64_t
4331 arc_evict_impl(arc_state_t *state, arc_buf_contents_t type, int64_t bytes)
4332 {
4333 uint64_t delta;
4334
4335 if (bytes > 0 && zfs_refcount_count(&state->arcs_esize[type]) > 0) {
4336 delta = MIN(zfs_refcount_count(&state->arcs_esize[type]),
4337 bytes);
4338 return (arc_evict_state(state, type, 0, delta));
4339 }
4340
4341 return (0);
4342 }
4343
4344 /*
4345 * Adjust specified fraction, taking into account initial ghost state(s) size,
4346 * ghost hit bytes towards increasing the fraction, ghost hit bytes towards
4347 * decreasing it, plus a balance factor, controlling the decrease rate, used
4348 * to balance metadata vs data.
4349 */
4350 static uint64_t
4351 arc_evict_adj(uint64_t frac, uint64_t total, uint64_t up, uint64_t down,
4352 uint_t balance)
4353 {
4354 if (total < 8 || up + down == 0)
4355 return (frac);
4356
4357 /*
4358 * We should not have more ghost hits than ghost size, but they
4359 * may get close. Restrict maximum adjustment in that case.
4360 */
4361 if (up + down >= total / 4) {
4362 uint64_t scale = (up + down) / (total / 8);
4363 up /= scale;
4364 down /= scale;
4365 }
4366
4367 /* Get maximal dynamic range by choosing optimal shifts. */
4368 int s = highbit64(total);
4369 s = MIN(64 - s, 32);
4370
4371 uint64_t ofrac = (1ULL << 32) - frac;
4372
4373 if (frac >= 4 * ofrac)
4374 up /= frac / (2 * ofrac + 1);
4375 up = (up << s) / (total >> (32 - s));
4376 if (ofrac >= 4 * frac)
4377 down /= ofrac / (2 * frac + 1);
4378 down = (down << s) / (total >> (32 - s));
4379 down = down * 100 / balance;
4380
4381 return (frac + up - down);
4382 }
4383
4384 /*
4385 * Evict buffers from the cache, such that arcstat_size is capped by arc_c.
4386 */
4387 static uint64_t
4388 arc_evict(void)
4389 {
4390 uint64_t asize, bytes, total_evicted = 0;
4391 int64_t e, mrud, mrum, mfud, mfum, w;
4392 static uint64_t ogrd, ogrm, ogfd, ogfm;
4393 static uint64_t gsrd, gsrm, gsfd, gsfm;
4394 uint64_t ngrd, ngrm, ngfd, ngfm;
4395
4396 /* Get current size of ARC states we can evict from. */
4397 mrud = zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_DATA]) +
4398 zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_DATA]);
4399 mrum = zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_METADATA]) +
4400 zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_METADATA]);
4401 mfud = zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_DATA]);
4402 mfum = zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_METADATA]);
4403 uint64_t d = mrud + mfud;
4404 uint64_t m = mrum + mfum;
4405 uint64_t t = d + m;
4406
4407 /* Get ARC ghost hits since last eviction. */
4408 ngrd = wmsum_value(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA]);
4409 uint64_t grd = ngrd - ogrd;
4410 ogrd = ngrd;
4411 ngrm = wmsum_value(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA]);
4412 uint64_t grm = ngrm - ogrm;
4413 ogrm = ngrm;
4414 ngfd = wmsum_value(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA]);
4415 uint64_t gfd = ngfd - ogfd;
4416 ogfd = ngfd;
4417 ngfm = wmsum_value(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA]);
4418 uint64_t gfm = ngfm - ogfm;
4419 ogfm = ngfm;
4420
4421 /* Adjust ARC states balance based on ghost hits. */
4422 arc_meta = arc_evict_adj(arc_meta, gsrd + gsrm + gsfd + gsfm,
4423 grm + gfm, grd + gfd, zfs_arc_meta_balance);
4424 arc_pd = arc_evict_adj(arc_pd, gsrd + gsfd, grd, gfd, 100);
4425 arc_pm = arc_evict_adj(arc_pm, gsrm + gsfm, grm, gfm, 100);
4426
4427 asize = aggsum_value(&arc_sums.arcstat_size);
4428 int64_t wt = t - (asize - arc_c);
4429
4430 /*
4431 * Try to reduce pinned dnodes if more than 3/4 of wanted metadata
4432 * target is not evictable or if they go over arc_dnode_limit.
4433 */
4434 int64_t prune = 0;
4435 int64_t dn = wmsum_value(&arc_sums.arcstat_dnode_size);
4436 w = wt * (int64_t)(arc_meta >> 16) >> 16;
4437 if (zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_METADATA]) +
4438 zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_METADATA]) -
4439 zfs_refcount_count(&arc_mru->arcs_esize[ARC_BUFC_METADATA]) -
4440 zfs_refcount_count(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]) >
4441 w * 3 / 4) {
4442 prune = dn / sizeof (dnode_t) *
4443 zfs_arc_dnode_reduce_percent / 100;
4444 } else if (dn > arc_dnode_limit) {
4445 prune = (dn - arc_dnode_limit) / sizeof (dnode_t) *
4446 zfs_arc_dnode_reduce_percent / 100;
4447 }
4448 if (prune > 0)
4449 arc_prune_async(prune);
4450
4451 /* Evict MRU metadata. */
4452 w = wt * (int64_t)(arc_meta * arc_pm >> 48) >> 16;
4453 e = MIN((int64_t)(asize - arc_c), (int64_t)(mrum - w));
4454 bytes = arc_evict_impl(arc_mru, ARC_BUFC_METADATA, e);
4455 total_evicted += bytes;
4456 mrum -= bytes;
4457 asize -= bytes;
4458
4459 /* Evict MFU metadata. */
4460 w = wt * (int64_t)(arc_meta >> 16) >> 16;
4461 e = MIN((int64_t)(asize - arc_c), (int64_t)(m - w));
4462 bytes = arc_evict_impl(arc_mfu, ARC_BUFC_METADATA, e);
4463 total_evicted += bytes;
4464 mfum -= bytes;
4465 asize -= bytes;
4466
4467 /* Evict MRU data. */
4468 wt -= m - total_evicted;
4469 w = wt * (int64_t)(arc_pd >> 16) >> 16;
4470 e = MIN((int64_t)(asize - arc_c), (int64_t)(mrud - w));
4471 bytes = arc_evict_impl(arc_mru, ARC_BUFC_DATA, e);
4472 total_evicted += bytes;
4473 mrud -= bytes;
4474 asize -= bytes;
4475
4476 /* Evict MFU data. */
4477 e = asize - arc_c;
4478 bytes = arc_evict_impl(arc_mfu, ARC_BUFC_DATA, e);
4479 mfud -= bytes;
4480 total_evicted += bytes;
4481
4482 /*
4483 * Evict ghost lists
4484 *
4485 * Size of each state's ghost list represents how much that state
4486 * may grow by shrinking the other states. Would it need to shrink
4487 * other states to zero (that is unlikely), its ghost size would be
4488 * equal to sum of other three state sizes. But excessive ghost
4489 * size may result in false ghost hits (too far back), that may
4490 * never result in real cache hits if several states are competing.
4491 * So choose some arbitraty point of 1/2 of other state sizes.
4492 */
4493 gsrd = (mrum + mfud + mfum) / 2;
4494 e = zfs_refcount_count(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]) -
4495 gsrd;
4496 (void) arc_evict_impl(arc_mru_ghost, ARC_BUFC_DATA, e);
4497
4498 gsrm = (mrud + mfud + mfum) / 2;
4499 e = zfs_refcount_count(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]) -
4500 gsrm;
4501 (void) arc_evict_impl(arc_mru_ghost, ARC_BUFC_METADATA, e);
4502
4503 gsfd = (mrud + mrum + mfum) / 2;
4504 e = zfs_refcount_count(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]) -
4505 gsfd;
4506 (void) arc_evict_impl(arc_mfu_ghost, ARC_BUFC_DATA, e);
4507
4508 gsfm = (mrud + mrum + mfud) / 2;
4509 e = zfs_refcount_count(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]) -
4510 gsfm;
4511 (void) arc_evict_impl(arc_mfu_ghost, ARC_BUFC_METADATA, e);
4512
4513 return (total_evicted);
4514 }
4515
4516 void
4517 arc_flush(spa_t *spa, boolean_t retry)
4518 {
4519 uint64_t guid = 0;
4520
4521 /*
4522 * If retry is B_TRUE, a spa must not be specified since we have
4523 * no good way to determine if all of a spa's buffers have been
4524 * evicted from an arc state.
4525 */
4526 ASSERT(!retry || spa == NULL);
4527
4528 if (spa != NULL)
4529 guid = spa_load_guid(spa);
4530
4531 (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry);
4532 (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry);
4533
4534 (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry);
4535 (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry);
4536
4537 (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry);
4538 (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry);
4539
4540 (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry);
4541 (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry);
4542
4543 (void) arc_flush_state(arc_uncached, guid, ARC_BUFC_DATA, retry);
4544 (void) arc_flush_state(arc_uncached, guid, ARC_BUFC_METADATA, retry);
4545 }
4546
4547 void
4548 arc_reduce_target_size(int64_t to_free)
4549 {
4550 uint64_t c = arc_c;
4551
4552 if (c <= arc_c_min)
4553 return;
4554
4555 /*
4556 * All callers want the ARC to actually evict (at least) this much
4557 * memory. Therefore we reduce from the lower of the current size and
4558 * the target size. This way, even if arc_c is much higher than
4559 * arc_size (as can be the case after many calls to arc_freed(), we will
4560 * immediately have arc_c < arc_size and therefore the arc_evict_zthr
4561 * will evict.
4562 */
4563 uint64_t asize = aggsum_value(&arc_sums.arcstat_size);
4564 if (asize < c)
4565 to_free += c - asize;
4566 arc_c = MAX((int64_t)c - to_free, (int64_t)arc_c_min);
4567
4568 /* See comment in arc_evict_cb_check() on why lock+flag */
4569 mutex_enter(&arc_evict_lock);
4570 arc_evict_needed = B_TRUE;
4571 mutex_exit(&arc_evict_lock);
4572 zthr_wakeup(arc_evict_zthr);
4573 }
4574
4575 /*
4576 * Determine if the system is under memory pressure and is asking
4577 * to reclaim memory. A return value of B_TRUE indicates that the system
4578 * is under memory pressure and that the arc should adjust accordingly.
4579 */
4580 boolean_t
4581 arc_reclaim_needed(void)
4582 {
4583 return (arc_available_memory() < 0);
4584 }
4585
4586 void
4587 arc_kmem_reap_soon(void)
4588 {
4589 size_t i;
4590 kmem_cache_t *prev_cache = NULL;
4591 kmem_cache_t *prev_data_cache = NULL;
4592
4593 #ifdef _KERNEL
4594 #if defined(_ILP32)
4595 /*
4596 * Reclaim unused memory from all kmem caches.
4597 */
4598 kmem_reap();
4599 #endif
4600 #endif
4601
4602 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
4603 #if defined(_ILP32)
4604 /* reach upper limit of cache size on 32-bit */
4605 if (zio_buf_cache[i] == NULL)
4606 break;
4607 #endif
4608 if (zio_buf_cache[i] != prev_cache) {
4609 prev_cache = zio_buf_cache[i];
4610 kmem_cache_reap_now(zio_buf_cache[i]);
4611 }
4612 if (zio_data_buf_cache[i] != prev_data_cache) {
4613 prev_data_cache = zio_data_buf_cache[i];
4614 kmem_cache_reap_now(zio_data_buf_cache[i]);
4615 }
4616 }
4617 kmem_cache_reap_now(buf_cache);
4618 kmem_cache_reap_now(hdr_full_cache);
4619 kmem_cache_reap_now(hdr_l2only_cache);
4620 kmem_cache_reap_now(zfs_btree_leaf_cache);
4621 abd_cache_reap_now();
4622 }
4623
4624 static boolean_t
4625 arc_evict_cb_check(void *arg, zthr_t *zthr)
4626 {
4627 (void) arg, (void) zthr;
4628
4629 #ifdef ZFS_DEBUG
4630 /*
4631 * This is necessary in order to keep the kstat information
4632 * up to date for tools that display kstat data such as the
4633 * mdb ::arc dcmd and the Linux crash utility. These tools
4634 * typically do not call kstat's update function, but simply
4635 * dump out stats from the most recent update. Without
4636 * this call, these commands may show stale stats for the
4637 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even
4638 * with this call, the data might be out of date if the
4639 * evict thread hasn't been woken recently; but that should
4640 * suffice. The arc_state_t structures can be queried
4641 * directly if more accurate information is needed.
4642 */
4643 if (arc_ksp != NULL)
4644 arc_ksp->ks_update(arc_ksp, KSTAT_READ);
4645 #endif
4646
4647 /*
4648 * We have to rely on arc_wait_for_eviction() to tell us when to
4649 * evict, rather than checking if we are overflowing here, so that we
4650 * are sure to not leave arc_wait_for_eviction() waiting on aew_cv.
4651 * If we have become "not overflowing" since arc_wait_for_eviction()
4652 * checked, we need to wake it up. We could broadcast the CV here,
4653 * but arc_wait_for_eviction() may have not yet gone to sleep. We
4654 * would need to use a mutex to ensure that this function doesn't
4655 * broadcast until arc_wait_for_eviction() has gone to sleep (e.g.
4656 * the arc_evict_lock). However, the lock ordering of such a lock
4657 * would necessarily be incorrect with respect to the zthr_lock,
4658 * which is held before this function is called, and is held by
4659 * arc_wait_for_eviction() when it calls zthr_wakeup().
4660 */
4661 if (arc_evict_needed)
4662 return (B_TRUE);
4663
4664 /*
4665 * If we have buffers in uncached state, evict them periodically.
4666 */
4667 return ((zfs_refcount_count(&arc_uncached->arcs_esize[ARC_BUFC_DATA]) +
4668 zfs_refcount_count(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]) &&
4669 ddi_get_lbolt() - arc_last_uncached_flush >
4670 MSEC_TO_TICK(arc_min_prefetch_ms / 2)));
4671 }
4672
4673 /*
4674 * Keep arc_size under arc_c by running arc_evict which evicts data
4675 * from the ARC.
4676 */
4677 static void
4678 arc_evict_cb(void *arg, zthr_t *zthr)
4679 {
4680 (void) arg, (void) zthr;
4681
4682 uint64_t evicted = 0;
4683 fstrans_cookie_t cookie = spl_fstrans_mark();
4684
4685 /* Always try to evict from uncached state. */
4686 arc_last_uncached_flush = ddi_get_lbolt();
4687 evicted += arc_flush_state(arc_uncached, 0, ARC_BUFC_DATA, B_FALSE);
4688 evicted += arc_flush_state(arc_uncached, 0, ARC_BUFC_METADATA, B_FALSE);
4689
4690 /* Evict from other states only if told to. */
4691 if (arc_evict_needed)
4692 evicted += arc_evict();
4693
4694 /*
4695 * If evicted is zero, we couldn't evict anything
4696 * via arc_evict(). This could be due to hash lock
4697 * collisions, but more likely due to the majority of
4698 * arc buffers being unevictable. Therefore, even if
4699 * arc_size is above arc_c, another pass is unlikely to
4700 * be helpful and could potentially cause us to enter an
4701 * infinite loop. Additionally, zthr_iscancelled() is
4702 * checked here so that if the arc is shutting down, the
4703 * broadcast will wake any remaining arc evict waiters.
4704 */
4705 mutex_enter(&arc_evict_lock);
4706 arc_evict_needed = !zthr_iscancelled(arc_evict_zthr) &&
4707 evicted > 0 && aggsum_compare(&arc_sums.arcstat_size, arc_c) > 0;
4708 if (!arc_evict_needed) {
4709 /*
4710 * We're either no longer overflowing, or we
4711 * can't evict anything more, so we should wake
4712 * arc_get_data_impl() sooner.
4713 */
4714 arc_evict_waiter_t *aw;
4715 while ((aw = list_remove_head(&arc_evict_waiters)) != NULL) {
4716 cv_broadcast(&aw->aew_cv);
4717 }
4718 arc_set_need_free();
4719 }
4720 mutex_exit(&arc_evict_lock);
4721 spl_fstrans_unmark(cookie);
4722 }
4723
4724 static boolean_t
4725 arc_reap_cb_check(void *arg, zthr_t *zthr)
4726 {
4727 (void) arg, (void) zthr;
4728
4729 int64_t free_memory = arc_available_memory();
4730 static int reap_cb_check_counter = 0;
4731
4732 /*
4733 * If a kmem reap is already active, don't schedule more. We must
4734 * check for this because kmem_cache_reap_soon() won't actually
4735 * block on the cache being reaped (this is to prevent callers from
4736 * becoming implicitly blocked by a system-wide kmem reap -- which,
4737 * on a system with many, many full magazines, can take minutes).
4738 */
4739 if (!kmem_cache_reap_active() && free_memory < 0) {
4740
4741 arc_no_grow = B_TRUE;
4742 arc_warm = B_TRUE;
4743 /*
4744 * Wait at least zfs_grow_retry (default 5) seconds
4745 * before considering growing.
4746 */
4747 arc_growtime = gethrtime() + SEC2NSEC(arc_grow_retry);
4748 return (B_TRUE);
4749 } else if (free_memory < arc_c >> arc_no_grow_shift) {
4750 arc_no_grow = B_TRUE;
4751 } else if (gethrtime() >= arc_growtime) {
4752 arc_no_grow = B_FALSE;
4753 }
4754
4755 /*
4756 * Called unconditionally every 60 seconds to reclaim unused
4757 * zstd compression and decompression context. This is done
4758 * here to avoid the need for an independent thread.
4759 */
4760 if (!((reap_cb_check_counter++) % 60))
4761 zfs_zstd_cache_reap_now();
4762
4763 return (B_FALSE);
4764 }
4765
4766 /*
4767 * Keep enough free memory in the system by reaping the ARC's kmem
4768 * caches. To cause more slabs to be reapable, we may reduce the
4769 * target size of the cache (arc_c), causing the arc_evict_cb()
4770 * to free more buffers.
4771 */
4772 static void
4773 arc_reap_cb(void *arg, zthr_t *zthr)
4774 {
4775 (void) arg, (void) zthr;
4776
4777 int64_t free_memory;
4778 fstrans_cookie_t cookie = spl_fstrans_mark();
4779
4780 /*
4781 * Kick off asynchronous kmem_reap()'s of all our caches.
4782 */
4783 arc_kmem_reap_soon();
4784
4785 /*
4786 * Wait at least arc_kmem_cache_reap_retry_ms between
4787 * arc_kmem_reap_soon() calls. Without this check it is possible to
4788 * end up in a situation where we spend lots of time reaping
4789 * caches, while we're near arc_c_min. Waiting here also gives the
4790 * subsequent free memory check a chance of finding that the
4791 * asynchronous reap has already freed enough memory, and we don't
4792 * need to call arc_reduce_target_size().
4793 */
4794 delay((hz * arc_kmem_cache_reap_retry_ms + 999) / 1000);
4795
4796 /*
4797 * Reduce the target size as needed to maintain the amount of free
4798 * memory in the system at a fraction of the arc_size (1/128th by
4799 * default). If oversubscribed (free_memory < 0) then reduce the
4800 * target arc_size by the deficit amount plus the fractional
4801 * amount. If free memory is positive but less than the fractional
4802 * amount, reduce by what is needed to hit the fractional amount.
4803 */
4804 free_memory = arc_available_memory();
4805
4806 int64_t can_free = arc_c - arc_c_min;
4807 if (can_free > 0) {
4808 int64_t to_free = (can_free >> arc_shrink_shift) - free_memory;
4809 if (to_free > 0)
4810 arc_reduce_target_size(to_free);
4811 }
4812 spl_fstrans_unmark(cookie);
4813 }
4814
4815 #ifdef _KERNEL
4816 /*
4817 * Determine the amount of memory eligible for eviction contained in the
4818 * ARC. All clean data reported by the ghost lists can always be safely
4819 * evicted. Due to arc_c_min, the same does not hold for all clean data
4820 * contained by the regular mru and mfu lists.
4821 *
4822 * In the case of the regular mru and mfu lists, we need to report as
4823 * much clean data as possible, such that evicting that same reported
4824 * data will not bring arc_size below arc_c_min. Thus, in certain
4825 * circumstances, the total amount of clean data in the mru and mfu
4826 * lists might not actually be evictable.
4827 *
4828 * The following two distinct cases are accounted for:
4829 *
4830 * 1. The sum of the amount of dirty data contained by both the mru and
4831 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
4832 * is greater than or equal to arc_c_min.
4833 * (i.e. amount of dirty data >= arc_c_min)
4834 *
4835 * This is the easy case; all clean data contained by the mru and mfu
4836 * lists is evictable. Evicting all clean data can only drop arc_size
4837 * to the amount of dirty data, which is greater than arc_c_min.
4838 *
4839 * 2. The sum of the amount of dirty data contained by both the mru and
4840 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
4841 * is less than arc_c_min.
4842 * (i.e. arc_c_min > amount of dirty data)
4843 *
4844 * 2.1. arc_size is greater than or equal arc_c_min.
4845 * (i.e. arc_size >= arc_c_min > amount of dirty data)
4846 *
4847 * In this case, not all clean data from the regular mru and mfu
4848 * lists is actually evictable; we must leave enough clean data
4849 * to keep arc_size above arc_c_min. Thus, the maximum amount of
4850 * evictable data from the two lists combined, is exactly the
4851 * difference between arc_size and arc_c_min.
4852 *
4853 * 2.2. arc_size is less than arc_c_min
4854 * (i.e. arc_c_min > arc_size > amount of dirty data)
4855 *
4856 * In this case, none of the data contained in the mru and mfu
4857 * lists is evictable, even if it's clean. Since arc_size is
4858 * already below arc_c_min, evicting any more would only
4859 * increase this negative difference.
4860 */
4861
4862 #endif /* _KERNEL */
4863
4864 /*
4865 * Adapt arc info given the number of bytes we are trying to add and
4866 * the state that we are coming from. This function is only called
4867 * when we are adding new content to the cache.
4868 */
4869 static void
4870 arc_adapt(uint64_t bytes)
4871 {
4872 /*
4873 * Wake reap thread if we do not have any available memory
4874 */
4875 if (arc_reclaim_needed()) {
4876 zthr_wakeup(arc_reap_zthr);
4877 return;
4878 }
4879
4880 if (arc_no_grow)
4881 return;
4882
4883 if (arc_c >= arc_c_max)
4884 return;
4885
4886 /*
4887 * If we're within (2 * maxblocksize) bytes of the target
4888 * cache size, increment the target cache size
4889 */
4890 if (aggsum_upper_bound(&arc_sums.arcstat_size) +
4891 2 * SPA_MAXBLOCKSIZE >= arc_c) {
4892 uint64_t dc = MAX(bytes, SPA_OLD_MAXBLOCKSIZE);
4893 if (atomic_add_64_nv(&arc_c, dc) > arc_c_max)
4894 arc_c = arc_c_max;
4895 }
4896 }
4897
4898 /*
4899 * Check if arc_size has grown past our upper threshold, determined by
4900 * zfs_arc_overflow_shift.
4901 */
4902 static arc_ovf_level_t
4903 arc_is_overflowing(boolean_t use_reserve)
4904 {
4905 /* Always allow at least one block of overflow */
4906 int64_t overflow = MAX(SPA_MAXBLOCKSIZE,
4907 arc_c >> zfs_arc_overflow_shift);
4908
4909 /*
4910 * We just compare the lower bound here for performance reasons. Our
4911 * primary goals are to make sure that the arc never grows without
4912 * bound, and that it can reach its maximum size. This check
4913 * accomplishes both goals. The maximum amount we could run over by is
4914 * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block
4915 * in the ARC. In practice, that's in the tens of MB, which is low
4916 * enough to be safe.
4917 */
4918 int64_t over = aggsum_lower_bound(&arc_sums.arcstat_size) -
4919 arc_c - overflow / 2;
4920 if (!use_reserve)
4921 overflow /= 2;
4922 return (over < 0 ? ARC_OVF_NONE :
4923 over < overflow ? ARC_OVF_SOME : ARC_OVF_SEVERE);
4924 }
4925
4926 static abd_t *
4927 arc_get_data_abd(arc_buf_hdr_t *hdr, uint64_t size, const void *tag,
4928 int alloc_flags)
4929 {
4930 arc_buf_contents_t type = arc_buf_type(hdr);
4931
4932 arc_get_data_impl(hdr, size, tag, alloc_flags);
4933 if (alloc_flags & ARC_HDR_ALLOC_LINEAR)
4934 return (abd_alloc_linear(size, type == ARC_BUFC_METADATA));
4935 else
4936 return (abd_alloc(size, type == ARC_BUFC_METADATA));
4937 }
4938
4939 static void *
4940 arc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, const void *tag)
4941 {
4942 arc_buf_contents_t type = arc_buf_type(hdr);
4943
4944 arc_get_data_impl(hdr, size, tag, 0);
4945 if (type == ARC_BUFC_METADATA) {
4946 return (zio_buf_alloc(size));
4947 } else {
4948 ASSERT(type == ARC_BUFC_DATA);
4949 return (zio_data_buf_alloc(size));
4950 }
4951 }
4952
4953 /*
4954 * Wait for the specified amount of data (in bytes) to be evicted from the
4955 * ARC, and for there to be sufficient free memory in the system. Waiting for
4956 * eviction ensures that the memory used by the ARC decreases. Waiting for
4957 * free memory ensures that the system won't run out of free pages, regardless
4958 * of ARC behavior and settings. See arc_lowmem_init().
4959 */
4960 void
4961 arc_wait_for_eviction(uint64_t amount, boolean_t use_reserve)
4962 {
4963 switch (arc_is_overflowing(use_reserve)) {
4964 case ARC_OVF_NONE:
4965 return;
4966 case ARC_OVF_SOME:
4967 /*
4968 * This is a bit racy without taking arc_evict_lock, but the
4969 * worst that can happen is we either call zthr_wakeup() extra
4970 * time due to race with other thread here, or the set flag
4971 * get cleared by arc_evict_cb(), which is unlikely due to
4972 * big hysteresis, but also not important since at this level
4973 * of overflow the eviction is purely advisory. Same time
4974 * taking the global lock here every time without waiting for
4975 * the actual eviction creates a significant lock contention.
4976 */
4977 if (!arc_evict_needed) {
4978 arc_evict_needed = B_TRUE;
4979 zthr_wakeup(arc_evict_zthr);
4980 }
4981 return;
4982 case ARC_OVF_SEVERE:
4983 default:
4984 {
4985 arc_evict_waiter_t aw;
4986 list_link_init(&aw.aew_node);
4987 cv_init(&aw.aew_cv, NULL, CV_DEFAULT, NULL);
4988
4989 uint64_t last_count = 0;
4990 mutex_enter(&arc_evict_lock);
4991 if (!list_is_empty(&arc_evict_waiters)) {
4992 arc_evict_waiter_t *last =
4993 list_tail(&arc_evict_waiters);
4994 last_count = last->aew_count;
4995 } else if (!arc_evict_needed) {
4996 arc_evict_needed = B_TRUE;
4997 zthr_wakeup(arc_evict_zthr);
4998 }
4999 /*
5000 * Note, the last waiter's count may be less than
5001 * arc_evict_count if we are low on memory in which
5002 * case arc_evict_state_impl() may have deferred
5003 * wakeups (but still incremented arc_evict_count).
5004 */
5005 aw.aew_count = MAX(last_count, arc_evict_count) + amount;
5006
5007 list_insert_tail(&arc_evict_waiters, &aw);
5008
5009 arc_set_need_free();
5010
5011 DTRACE_PROBE3(arc__wait__for__eviction,
5012 uint64_t, amount,
5013 uint64_t, arc_evict_count,
5014 uint64_t, aw.aew_count);
5015
5016 /*
5017 * We will be woken up either when arc_evict_count reaches
5018 * aew_count, or when the ARC is no longer overflowing and
5019 * eviction completes.
5020 * In case of "false" wakeup, we will still be on the list.
5021 */
5022 do {
5023 cv_wait(&aw.aew_cv, &arc_evict_lock);
5024 } while (list_link_active(&aw.aew_node));
5025 mutex_exit(&arc_evict_lock);
5026
5027 cv_destroy(&aw.aew_cv);
5028 }
5029 }
5030 }
5031
5032 /*
5033 * Allocate a block and return it to the caller. If we are hitting the
5034 * hard limit for the cache size, we must sleep, waiting for the eviction
5035 * thread to catch up. If we're past the target size but below the hard
5036 * limit, we'll only signal the reclaim thread and continue on.
5037 */
5038 static void
5039 arc_get_data_impl(arc_buf_hdr_t *hdr, uint64_t size, const void *tag,
5040 int alloc_flags)
5041 {
5042 arc_adapt(size);
5043
5044 /*
5045 * If arc_size is currently overflowing, we must be adding data
5046 * faster than we are evicting. To ensure we don't compound the
5047 * problem by adding more data and forcing arc_size to grow even
5048 * further past it's target size, we wait for the eviction thread to
5049 * make some progress. We also wait for there to be sufficient free
5050 * memory in the system, as measured by arc_free_memory().
5051 *
5052 * Specifically, we wait for zfs_arc_eviction_pct percent of the
5053 * requested size to be evicted. This should be more than 100%, to
5054 * ensure that that progress is also made towards getting arc_size
5055 * under arc_c. See the comment above zfs_arc_eviction_pct.
5056 */
5057 arc_wait_for_eviction(size * zfs_arc_eviction_pct / 100,
5058 alloc_flags & ARC_HDR_USE_RESERVE);
5059
5060 arc_buf_contents_t type = arc_buf_type(hdr);
5061 if (type == ARC_BUFC_METADATA) {
5062 arc_space_consume(size, ARC_SPACE_META);
5063 } else {
5064 arc_space_consume(size, ARC_SPACE_DATA);
5065 }
5066
5067 /*
5068 * Update the state size. Note that ghost states have a
5069 * "ghost size" and so don't need to be updated.
5070 */
5071 arc_state_t *state = hdr->b_l1hdr.b_state;
5072 if (!GHOST_STATE(state)) {
5073
5074 (void) zfs_refcount_add_many(&state->arcs_size[type], size,
5075 tag);
5076
5077 /*
5078 * If this is reached via arc_read, the link is
5079 * protected by the hash lock. If reached via
5080 * arc_buf_alloc, the header should not be accessed by
5081 * any other thread. And, if reached via arc_read_done,
5082 * the hash lock will protect it if it's found in the
5083 * hash table; otherwise no other thread should be
5084 * trying to [add|remove]_reference it.
5085 */
5086 if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) {
5087 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
5088 (void) zfs_refcount_add_many(&state->arcs_esize[type],
5089 size, tag);
5090 }
5091 }
5092 }
5093
5094 static void
5095 arc_free_data_abd(arc_buf_hdr_t *hdr, abd_t *abd, uint64_t size,
5096 const void *tag)
5097 {
5098 arc_free_data_impl(hdr, size, tag);
5099 abd_free(abd);
5100 }
5101
5102 static void
5103 arc_free_data_buf(arc_buf_hdr_t *hdr, void *buf, uint64_t size, const void *tag)
5104 {
5105 arc_buf_contents_t type = arc_buf_type(hdr);
5106
5107 arc_free_data_impl(hdr, size, tag);
5108 if (type == ARC_BUFC_METADATA) {
5109 zio_buf_free(buf, size);
5110 } else {
5111 ASSERT(type == ARC_BUFC_DATA);
5112 zio_data_buf_free(buf, size);
5113 }
5114 }
5115
5116 /*
5117 * Free the arc data buffer.
5118 */
5119 static void
5120 arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, const void *tag)
5121 {
5122 arc_state_t *state = hdr->b_l1hdr.b_state;
5123 arc_buf_contents_t type = arc_buf_type(hdr);
5124
5125 /* protected by hash lock, if in the hash table */
5126 if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) {
5127 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
5128 ASSERT(state != arc_anon && state != arc_l2c_only);
5129
5130 (void) zfs_refcount_remove_many(&state->arcs_esize[type],
5131 size, tag);
5132 }
5133 (void) zfs_refcount_remove_many(&state->arcs_size[type], size, tag);
5134
5135 VERIFY3U(hdr->b_type, ==, type);
5136 if (type == ARC_BUFC_METADATA) {
5137 arc_space_return(size, ARC_SPACE_META);
5138 } else {
5139 ASSERT(type == ARC_BUFC_DATA);
5140 arc_space_return(size, ARC_SPACE_DATA);
5141 }
5142 }
5143
5144 /*
5145 * This routine is called whenever a buffer is accessed.
5146 */
5147 static void
5148 arc_access(arc_buf_hdr_t *hdr, arc_flags_t arc_flags, boolean_t hit)
5149 {
5150 ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
5151 ASSERT(HDR_HAS_L1HDR(hdr));
5152
5153 /*
5154 * Update buffer prefetch status.
5155 */
5156 boolean_t was_prefetch = HDR_PREFETCH(hdr);
5157 boolean_t now_prefetch = arc_flags & ARC_FLAG_PREFETCH;
5158 if (was_prefetch != now_prefetch) {
5159 if (was_prefetch) {
5160 ARCSTAT_CONDSTAT(hit, demand_hit, demand_iohit,
5161 HDR_PRESCIENT_PREFETCH(hdr), prescient, predictive,
5162 prefetch);
5163 }
5164 if (HDR_HAS_L2HDR(hdr))
5165 l2arc_hdr_arcstats_decrement_state(hdr);
5166 if (was_prefetch) {
5167 arc_hdr_clear_flags(hdr,
5168 ARC_FLAG_PREFETCH | ARC_FLAG_PRESCIENT_PREFETCH);
5169 } else {
5170 arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH);
5171 }
5172 if (HDR_HAS_L2HDR(hdr))
5173 l2arc_hdr_arcstats_increment_state(hdr);
5174 }
5175 if (now_prefetch) {
5176 if (arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) {
5177 arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH);
5178 ARCSTAT_BUMP(arcstat_prescient_prefetch);
5179 } else {
5180 ARCSTAT_BUMP(arcstat_predictive_prefetch);
5181 }
5182 }
5183 if (arc_flags & ARC_FLAG_L2CACHE)
5184 arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE);
5185
5186 clock_t now = ddi_get_lbolt();
5187 if (hdr->b_l1hdr.b_state == arc_anon) {
5188 arc_state_t *new_state;
5189 /*
5190 * This buffer is not in the cache, and does not appear in
5191 * our "ghost" lists. Add it to the MRU or uncached state.
5192 */
5193 ASSERT0(hdr->b_l1hdr.b_arc_access);
5194 hdr->b_l1hdr.b_arc_access = now;
5195 if (HDR_UNCACHED(hdr)) {
5196 new_state = arc_uncached;
5197 DTRACE_PROBE1(new_state__uncached, arc_buf_hdr_t *,
5198 hdr);
5199 } else {
5200 new_state = arc_mru;
5201 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr);
5202 }
5203 arc_change_state(new_state, hdr);
5204 } else if (hdr->b_l1hdr.b_state == arc_mru) {
5205 /*
5206 * This buffer has been accessed once recently and either
5207 * its read is still in progress or it is in the cache.
5208 */
5209 if (HDR_IO_IN_PROGRESS(hdr)) {
5210 hdr->b_l1hdr.b_arc_access = now;
5211 return;
5212 }
5213 hdr->b_l1hdr.b_mru_hits++;
5214 ARCSTAT_BUMP(arcstat_mru_hits);
5215
5216 /*
5217 * If the previous access was a prefetch, then it already
5218 * handled possible promotion, so nothing more to do for now.
5219 */
5220 if (was_prefetch) {
5221 hdr->b_l1hdr.b_arc_access = now;
5222 return;
5223 }
5224
5225 /*
5226 * If more than ARC_MINTIME have passed from the previous
5227 * hit, promote the buffer to the MFU state.
5228 */
5229 if (ddi_time_after(now, hdr->b_l1hdr.b_arc_access +
5230 ARC_MINTIME)) {
5231 hdr->b_l1hdr.b_arc_access = now;
5232 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
5233 arc_change_state(arc_mfu, hdr);
5234 }
5235 } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) {
5236 arc_state_t *new_state;
5237 /*
5238 * This buffer has been accessed once recently, but was
5239 * evicted from the cache. Would we have bigger MRU, it
5240 * would be an MRU hit, so handle it the same way, except
5241 * we don't need to check the previous access time.
5242 */
5243 hdr->b_l1hdr.b_mru_ghost_hits++;
5244 ARCSTAT_BUMP(arcstat_mru_ghost_hits);
5245 hdr->b_l1hdr.b_arc_access = now;
5246 wmsum_add(&arc_mru_ghost->arcs_hits[arc_buf_type(hdr)],
5247 arc_hdr_size(hdr));
5248 if (was_prefetch) {
5249 new_state = arc_mru;
5250 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr);
5251 } else {
5252 new_state = arc_mfu;
5253 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
5254 }
5255 arc_change_state(new_state, hdr);
5256 } else if (hdr->b_l1hdr.b_state == arc_mfu) {
5257 /*
5258 * This buffer has been accessed more than once and either
5259 * still in the cache or being restored from one of ghosts.
5260 */
5261 if (!HDR_IO_IN_PROGRESS(hdr)) {
5262 hdr->b_l1hdr.b_mfu_hits++;
5263 ARCSTAT_BUMP(arcstat_mfu_hits);
5264 }
5265 hdr->b_l1hdr.b_arc_access = now;
5266 } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) {
5267 /*
5268 * This buffer has been accessed more than once recently, but
5269 * has been evicted from the cache. Would we have bigger MFU
5270 * it would stay in cache, so move it back to MFU state.
5271 */
5272 hdr->b_l1hdr.b_mfu_ghost_hits++;
5273 ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
5274 hdr->b_l1hdr.b_arc_access = now;
5275 wmsum_add(&arc_mfu_ghost->arcs_hits[arc_buf_type(hdr)],
5276 arc_hdr_size(hdr));
5277 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
5278 arc_change_state(arc_mfu, hdr);
5279 } else if (hdr->b_l1hdr.b_state == arc_uncached) {
5280 /*
5281 * This buffer is uncacheable, but we got a hit. Probably
5282 * a demand read after prefetch. Nothing more to do here.
5283 */
5284 if (!HDR_IO_IN_PROGRESS(hdr))
5285 ARCSTAT_BUMP(arcstat_uncached_hits);
5286 hdr->b_l1hdr.b_arc_access = now;
5287 } else if (hdr->b_l1hdr.b_state == arc_l2c_only) {
5288 /*
5289 * This buffer is on the 2nd Level ARC and was not accessed
5290 * for a long time, so treat it as new and put into MRU.
5291 */
5292 hdr->b_l1hdr.b_arc_access = now;
5293 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr);
5294 arc_change_state(arc_mru, hdr);
5295 } else {
5296 cmn_err(CE_PANIC, "invalid arc state 0x%p",
5297 hdr->b_l1hdr.b_state);
5298 }
5299 }
5300
5301 /*
5302 * This routine is called by dbuf_hold() to update the arc_access() state
5303 * which otherwise would be skipped for entries in the dbuf cache.
5304 */
5305 void
5306 arc_buf_access(arc_buf_t *buf)
5307 {
5308 arc_buf_hdr_t *hdr = buf->b_hdr;
5309
5310 /*
5311 * Avoid taking the hash_lock when possible as an optimization.
5312 * The header must be checked again under the hash_lock in order
5313 * to handle the case where it is concurrently being released.
5314 */
5315 if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr))
5316 return;
5317
5318 kmutex_t *hash_lock = HDR_LOCK(hdr);
5319 mutex_enter(hash_lock);
5320
5321 if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) {
5322 mutex_exit(hash_lock);
5323 ARCSTAT_BUMP(arcstat_access_skip);
5324 return;
5325 }
5326
5327 ASSERT(hdr->b_l1hdr.b_state == arc_mru ||
5328 hdr->b_l1hdr.b_state == arc_mfu ||
5329 hdr->b_l1hdr.b_state == arc_uncached);
5330
5331 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
5332 arc_access(hdr, 0, B_TRUE);
5333 mutex_exit(hash_lock);
5334
5335 ARCSTAT_BUMP(arcstat_hits);
5336 ARCSTAT_CONDSTAT(B_TRUE /* demand */, demand, prefetch,
5337 !HDR_ISTYPE_METADATA(hdr), data, metadata, hits);
5338 }
5339
5340 /* a generic arc_read_done_func_t which you can use */
5341 void
5342 arc_bcopy_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp,
5343 arc_buf_t *buf, void *arg)
5344 {
5345 (void) zio, (void) zb, (void) bp;
5346
5347 if (buf == NULL)
5348 return;
5349
5350 memcpy(arg, buf->b_data, arc_buf_size(buf));
5351 arc_buf_destroy(buf, arg);
5352 }
5353
5354 /* a generic arc_read_done_func_t */
5355 void
5356 arc_getbuf_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp,
5357 arc_buf_t *buf, void *arg)
5358 {
5359 (void) zb, (void) bp;
5360 arc_buf_t **bufp = arg;
5361
5362 if (buf == NULL) {
5363 ASSERT(zio == NULL || zio->io_error != 0);
5364 *bufp = NULL;
5365 } else {
5366 ASSERT(zio == NULL || zio->io_error == 0);
5367 *bufp = buf;
5368 ASSERT(buf->b_data != NULL);
5369 }
5370 }
5371
5372 static void
5373 arc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp)
5374 {
5375 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) {
5376 ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0);
5377 ASSERT3U(arc_hdr_get_compress(hdr), ==, ZIO_COMPRESS_OFF);
5378 } else {
5379 if (HDR_COMPRESSION_ENABLED(hdr)) {
5380 ASSERT3U(arc_hdr_get_compress(hdr), ==,
5381 BP_GET_COMPRESS(bp));
5382 }
5383 ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp));
5384 ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp));
5385 ASSERT3U(!!HDR_PROTECTED(hdr), ==, BP_IS_PROTECTED(bp));
5386 }
5387 }
5388
5389 static void
5390 arc_read_done(zio_t *zio)
5391 {
5392 blkptr_t *bp = zio->io_bp;
5393 arc_buf_hdr_t *hdr = zio->io_private;
5394 kmutex_t *hash_lock = NULL;
5395 arc_callback_t *callback_list;
5396 arc_callback_t *acb;
5397
5398 /*
5399 * The hdr was inserted into hash-table and removed from lists
5400 * prior to starting I/O. We should find this header, since
5401 * it's in the hash table, and it should be legit since it's
5402 * not possible to evict it during the I/O. The only possible
5403 * reason for it not to be found is if we were freed during the
5404 * read.
5405 */
5406 if (HDR_IN_HASH_TABLE(hdr)) {
5407 arc_buf_hdr_t *found;
5408
5409 ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp));
5410 ASSERT3U(hdr->b_dva.dva_word[0], ==,
5411 BP_IDENTITY(zio->io_bp)->dva_word[0]);
5412 ASSERT3U(hdr->b_dva.dva_word[1], ==,
5413 BP_IDENTITY(zio->io_bp)->dva_word[1]);
5414
5415 found = buf_hash_find(hdr->b_spa, zio->io_bp, &hash_lock);
5416
5417 ASSERT((found == hdr &&
5418 DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
5419 (found == hdr && HDR_L2_READING(hdr)));
5420 ASSERT3P(hash_lock, !=, NULL);
5421 }
5422
5423 if (BP_IS_PROTECTED(bp)) {
5424 hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp);
5425 hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset;
5426 zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt,
5427 hdr->b_crypt_hdr.b_iv);
5428
5429 if (zio->io_error == 0) {
5430 if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) {
5431 void *tmpbuf;
5432
5433 tmpbuf = abd_borrow_buf_copy(zio->io_abd,
5434 sizeof (zil_chain_t));
5435 zio_crypt_decode_mac_zil(tmpbuf,
5436 hdr->b_crypt_hdr.b_mac);
5437 abd_return_buf(zio->io_abd, tmpbuf,
5438 sizeof (zil_chain_t));
5439 } else {
5440 zio_crypt_decode_mac_bp(bp,
5441 hdr->b_crypt_hdr.b_mac);
5442 }
5443 }
5444 }
5445
5446 if (zio->io_error == 0) {
5447 /* byteswap if necessary */
5448 if (BP_SHOULD_BYTESWAP(zio->io_bp)) {
5449 if (BP_GET_LEVEL(zio->io_bp) > 0) {
5450 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64;
5451 } else {
5452 hdr->b_l1hdr.b_byteswap =
5453 DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp));
5454 }
5455 } else {
5456 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS;
5457 }
5458 if (!HDR_L2_READING(hdr)) {
5459 hdr->b_complevel = zio->io_prop.zp_complevel;
5460 }
5461 }
5462
5463 arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED);
5464 if (l2arc_noprefetch && HDR_PREFETCH(hdr))
5465 arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE);
5466
5467 callback_list = hdr->b_l1hdr.b_acb;
5468 ASSERT3P(callback_list, !=, NULL);
5469 hdr->b_l1hdr.b_acb = NULL;
5470
5471 /*
5472 * If a read request has a callback (i.e. acb_done is not NULL), then we
5473 * make a buf containing the data according to the parameters which were
5474 * passed in. The implementation of arc_buf_alloc_impl() ensures that we
5475 * aren't needlessly decompressing the data multiple times.
5476 */
5477 int callback_cnt = 0;
5478 for (acb = callback_list; acb != NULL; acb = acb->acb_next) {
5479
5480 /* We need the last one to call below in original order. */
5481 callback_list = acb;
5482
5483 if (!acb->acb_done || acb->acb_nobuf)
5484 continue;
5485
5486 callback_cnt++;
5487
5488 if (zio->io_error != 0)
5489 continue;
5490
5491 int error = arc_buf_alloc_impl(hdr, zio->io_spa,
5492 &acb->acb_zb, acb->acb_private, acb->acb_encrypted,
5493 acb->acb_compressed, acb->acb_noauth, B_TRUE,
5494 &acb->acb_buf);
5495
5496 /*
5497 * Assert non-speculative zios didn't fail because an
5498 * encryption key wasn't loaded
5499 */
5500 ASSERT((zio->io_flags & ZIO_FLAG_SPECULATIVE) ||
5501 error != EACCES);
5502
5503 /*
5504 * If we failed to decrypt, report an error now (as the zio
5505 * layer would have done if it had done the transforms).
5506 */
5507 if (error == ECKSUM) {
5508 ASSERT(BP_IS_PROTECTED(bp));
5509 error = SET_ERROR(EIO);
5510 if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) {
5511 spa_log_error(zio->io_spa, &acb->acb_zb,
5512 &zio->io_bp->blk_birth);
5513 (void) zfs_ereport_post(
5514 FM_EREPORT_ZFS_AUTHENTICATION,
5515 zio->io_spa, NULL, &acb->acb_zb, zio, 0);
5516 }
5517 }
5518
5519 if (error != 0) {
5520 /*
5521 * Decompression or decryption failed. Set
5522 * io_error so that when we call acb_done
5523 * (below), we will indicate that the read
5524 * failed. Note that in the unusual case
5525 * where one callback is compressed and another
5526 * uncompressed, we will mark all of them
5527 * as failed, even though the uncompressed
5528 * one can't actually fail. In this case,
5529 * the hdr will not be anonymous, because
5530 * if there are multiple callbacks, it's
5531 * because multiple threads found the same
5532 * arc buf in the hash table.
5533 */
5534 zio->io_error = error;
5535 }
5536 }
5537
5538 /*
5539 * If there are multiple callbacks, we must have the hash lock,
5540 * because the only way for multiple threads to find this hdr is
5541 * in the hash table. This ensures that if there are multiple
5542 * callbacks, the hdr is not anonymous. If it were anonymous,
5543 * we couldn't use arc_buf_destroy() in the error case below.
5544 */
5545 ASSERT(callback_cnt < 2 || hash_lock != NULL);
5546
5547 if (zio->io_error == 0) {
5548 arc_hdr_verify(hdr, zio->io_bp);
5549 } else {
5550 arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR);
5551 if (hdr->b_l1hdr.b_state != arc_anon)
5552 arc_change_state(arc_anon, hdr);
5553 if (HDR_IN_HASH_TABLE(hdr))
5554 buf_hash_remove(hdr);
5555 }
5556
5557 arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS);
5558 (void) remove_reference(hdr, hdr);
5559
5560 if (hash_lock != NULL)
5561 mutex_exit(hash_lock);
5562
5563 /* execute each callback and free its structure */
5564 while ((acb = callback_list) != NULL) {
5565 if (acb->acb_done != NULL) {
5566 if (zio->io_error != 0 && acb->acb_buf != NULL) {
5567 /*
5568 * If arc_buf_alloc_impl() fails during
5569 * decompression, the buf will still be
5570 * allocated, and needs to be freed here.
5571 */
5572 arc_buf_destroy(acb->acb_buf,
5573 acb->acb_private);
5574 acb->acb_buf = NULL;
5575 }
5576 acb->acb_done(zio, &zio->io_bookmark, zio->io_bp,
5577 acb->acb_buf, acb->acb_private);
5578 }
5579
5580 if (acb->acb_zio_dummy != NULL) {
5581 acb->acb_zio_dummy->io_error = zio->io_error;
5582 zio_nowait(acb->acb_zio_dummy);
5583 }
5584
5585 callback_list = acb->acb_prev;
5586 if (acb->acb_wait) {
5587 mutex_enter(&acb->acb_wait_lock);
5588 acb->acb_wait_error = zio->io_error;
5589 acb->acb_wait = B_FALSE;
5590 cv_signal(&acb->acb_wait_cv);
5591 mutex_exit(&acb->acb_wait_lock);
5592 /* acb will be freed by the waiting thread. */
5593 } else {
5594 kmem_free(acb, sizeof (arc_callback_t));
5595 }
5596 }
5597 }
5598
5599 /*
5600 * "Read" the block at the specified DVA (in bp) via the
5601 * cache. If the block is found in the cache, invoke the provided
5602 * callback immediately and return. Note that the `zio' parameter
5603 * in the callback will be NULL in this case, since no IO was
5604 * required. If the block is not in the cache pass the read request
5605 * on to the spa with a substitute callback function, so that the
5606 * requested block will be added to the cache.
5607 *
5608 * If a read request arrives for a block that has a read in-progress,
5609 * either wait for the in-progress read to complete (and return the
5610 * results); or, if this is a read with a "done" func, add a record
5611 * to the read to invoke the "done" func when the read completes,
5612 * and return; or just return.
5613 *
5614 * arc_read_done() will invoke all the requested "done" functions
5615 * for readers of this block.
5616 */
5617 int
5618 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
5619 arc_read_done_func_t *done, void *private, zio_priority_t priority,
5620 int zio_flags, arc_flags_t *arc_flags, const zbookmark_phys_t *zb)
5621 {
5622 arc_buf_hdr_t *hdr = NULL;
5623 kmutex_t *hash_lock = NULL;
5624 zio_t *rzio;
5625 uint64_t guid = spa_load_guid(spa);
5626 boolean_t compressed_read = (zio_flags & ZIO_FLAG_RAW_COMPRESS) != 0;
5627 boolean_t encrypted_read = BP_IS_ENCRYPTED(bp) &&
5628 (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0;
5629 boolean_t noauth_read = BP_IS_AUTHENTICATED(bp) &&
5630 (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0;
5631 boolean_t embedded_bp = !!BP_IS_EMBEDDED(bp);
5632 boolean_t no_buf = *arc_flags & ARC_FLAG_NO_BUF;
5633 arc_buf_t *buf = NULL;
5634 int rc = 0;
5635
5636 ASSERT(!embedded_bp ||
5637 BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
5638 ASSERT(!BP_IS_HOLE(bp));
5639 ASSERT(!BP_IS_REDACTED(bp));
5640
5641 /*
5642 * Normally SPL_FSTRANS will already be set since kernel threads which
5643 * expect to call the DMU interfaces will set it when created. System
5644 * calls are similarly handled by setting/cleaning the bit in the
5645 * registered callback (module/os/.../zfs/zpl_*).
5646 *
5647 * External consumers such as Lustre which call the exported DMU
5648 * interfaces may not have set SPL_FSTRANS. To avoid a deadlock
5649 * on the hash_lock always set and clear the bit.
5650 */
5651 fstrans_cookie_t cookie = spl_fstrans_mark();
5652 top:
5653 /*
5654 * Verify the block pointer contents are reasonable. This should
5655 * always be the case since the blkptr is protected by a checksum.
5656 * However, if there is damage it's desirable to detect this early
5657 * and treat it as a checksum error. This allows an alternate blkptr
5658 * to be tried when one is available (e.g. ditto blocks).
5659 */
5660 if (!zfs_blkptr_verify(spa, bp, (zio_flags & ZIO_FLAG_CONFIG_WRITER) ?
5661 BLK_CONFIG_HELD : BLK_CONFIG_NEEDED, BLK_VERIFY_LOG)) {
5662 rc = SET_ERROR(ECKSUM);
5663 goto done;
5664 }
5665
5666 if (!embedded_bp) {
5667 /*
5668 * Embedded BP's have no DVA and require no I/O to "read".
5669 * Create an anonymous arc buf to back it.
5670 */
5671 hdr = buf_hash_find(guid, bp, &hash_lock);
5672 }
5673
5674 /*
5675 * Determine if we have an L1 cache hit or a cache miss. For simplicity
5676 * we maintain encrypted data separately from compressed / uncompressed
5677 * data. If the user is requesting raw encrypted data and we don't have
5678 * that in the header we will read from disk to guarantee that we can
5679 * get it even if the encryption keys aren't loaded.
5680 */
5681 if (hdr != NULL && HDR_HAS_L1HDR(hdr) && (HDR_HAS_RABD(hdr) ||
5682 (hdr->b_l1hdr.b_pabd != NULL && !encrypted_read))) {
5683 boolean_t is_data = !HDR_ISTYPE_METADATA(hdr);
5684
5685 if (HDR_IO_IN_PROGRESS(hdr)) {
5686 if (*arc_flags & ARC_FLAG_CACHED_ONLY) {
5687 mutex_exit(hash_lock);
5688 ARCSTAT_BUMP(arcstat_cached_only_in_progress);
5689 rc = SET_ERROR(ENOENT);
5690 goto done;
5691 }
5692
5693 zio_t *head_zio = hdr->b_l1hdr.b_acb->acb_zio_head;
5694 ASSERT3P(head_zio, !=, NULL);
5695 if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) &&
5696 priority == ZIO_PRIORITY_SYNC_READ) {
5697 /*
5698 * This is a sync read that needs to wait for
5699 * an in-flight async read. Request that the
5700 * zio have its priority upgraded.
5701 */
5702 zio_change_priority(head_zio, priority);
5703 DTRACE_PROBE1(arc__async__upgrade__sync,
5704 arc_buf_hdr_t *, hdr);
5705 ARCSTAT_BUMP(arcstat_async_upgrade_sync);
5706 }
5707
5708 DTRACE_PROBE1(arc__iohit, arc_buf_hdr_t *, hdr);
5709 arc_access(hdr, *arc_flags, B_FALSE);
5710
5711 /*
5712 * If there are multiple threads reading the same block
5713 * and that block is not yet in the ARC, then only one
5714 * thread will do the physical I/O and all other
5715 * threads will wait until that I/O completes.
5716 * Synchronous reads use the acb_wait_cv whereas nowait
5717 * reads register a callback. Both are signalled/called
5718 * in arc_read_done.
5719 *
5720 * Errors of the physical I/O may need to be propagated.
5721 * Synchronous read errors are returned here from
5722 * arc_read_done via acb_wait_error. Nowait reads
5723 * attach the acb_zio_dummy zio to pio and
5724 * arc_read_done propagates the physical I/O's io_error
5725 * to acb_zio_dummy, and thereby to pio.
5726 */
5727 arc_callback_t *acb = NULL;
5728 if (done || pio || *arc_flags & ARC_FLAG_WAIT) {
5729 acb = kmem_zalloc(sizeof (arc_callback_t),
5730 KM_SLEEP);
5731 acb->acb_done = done;
5732 acb->acb_private = private;
5733 acb->acb_compressed = compressed_read;
5734 acb->acb_encrypted = encrypted_read;
5735 acb->acb_noauth = noauth_read;
5736 acb->acb_nobuf = no_buf;
5737 if (*arc_flags & ARC_FLAG_WAIT) {
5738 acb->acb_wait = B_TRUE;
5739 mutex_init(&acb->acb_wait_lock, NULL,
5740 MUTEX_DEFAULT, NULL);
5741 cv_init(&acb->acb_wait_cv, NULL,
5742 CV_DEFAULT, NULL);
5743 }
5744 acb->acb_zb = *zb;
5745 if (pio != NULL) {
5746 acb->acb_zio_dummy = zio_null(pio,
5747 spa, NULL, NULL, NULL, zio_flags);
5748 }
5749 acb->acb_zio_head = head_zio;
5750 acb->acb_next = hdr->b_l1hdr.b_acb;
5751 hdr->b_l1hdr.b_acb->acb_prev = acb;
5752 hdr->b_l1hdr.b_acb = acb;
5753 }
5754 mutex_exit(hash_lock);
5755
5756 ARCSTAT_BUMP(arcstat_iohits);
5757 ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH),
5758 demand, prefetch, is_data, data, metadata, iohits);
5759
5760 if (*arc_flags & ARC_FLAG_WAIT) {
5761 mutex_enter(&acb->acb_wait_lock);
5762 while (acb->acb_wait) {
5763 cv_wait(&acb->acb_wait_cv,
5764 &acb->acb_wait_lock);
5765 }
5766 rc = acb->acb_wait_error;
5767 mutex_exit(&acb->acb_wait_lock);
5768 mutex_destroy(&acb->acb_wait_lock);
5769 cv_destroy(&acb->acb_wait_cv);
5770 kmem_free(acb, sizeof (arc_callback_t));
5771 }
5772 goto out;
5773 }
5774
5775 ASSERT(hdr->b_l1hdr.b_state == arc_mru ||
5776 hdr->b_l1hdr.b_state == arc_mfu ||
5777 hdr->b_l1hdr.b_state == arc_uncached);
5778
5779 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
5780 arc_access(hdr, *arc_flags, B_TRUE);
5781
5782 if (done && !no_buf) {
5783 ASSERT(!embedded_bp || !BP_IS_HOLE(bp));
5784
5785 /* Get a buf with the desired data in it. */
5786 rc = arc_buf_alloc_impl(hdr, spa, zb, private,
5787 encrypted_read, compressed_read, noauth_read,
5788 B_TRUE, &buf);
5789 if (rc == ECKSUM) {
5790 /*
5791 * Convert authentication and decryption errors
5792 * to EIO (and generate an ereport if needed)
5793 * before leaving the ARC.
5794 */
5795 rc = SET_ERROR(EIO);
5796 if ((zio_flags & ZIO_FLAG_SPECULATIVE) == 0) {
5797 spa_log_error(spa, zb, &hdr->b_birth);
5798 (void) zfs_ereport_post(
5799 FM_EREPORT_ZFS_AUTHENTICATION,
5800 spa, NULL, zb, NULL, 0);
5801 }
5802 }
5803 if (rc != 0) {
5804 arc_buf_destroy_impl(buf);
5805 buf = NULL;
5806 (void) remove_reference(hdr, private);
5807 }
5808
5809 /* assert any errors weren't due to unloaded keys */
5810 ASSERT((zio_flags & ZIO_FLAG_SPECULATIVE) ||
5811 rc != EACCES);
5812 }
5813 mutex_exit(hash_lock);
5814 ARCSTAT_BUMP(arcstat_hits);
5815 ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH),
5816 demand, prefetch, is_data, data, metadata, hits);
5817 *arc_flags |= ARC_FLAG_CACHED;
5818 goto done;
5819 } else {
5820 uint64_t lsize = BP_GET_LSIZE(bp);
5821 uint64_t psize = BP_GET_PSIZE(bp);
5822 arc_callback_t *acb;
5823 vdev_t *vd = NULL;
5824 uint64_t addr = 0;
5825 boolean_t devw = B_FALSE;
5826 uint64_t size;
5827 abd_t *hdr_abd;
5828 int alloc_flags = encrypted_read ? ARC_HDR_ALLOC_RDATA : 0;
5829 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
5830
5831 if (*arc_flags & ARC_FLAG_CACHED_ONLY) {
5832 if (hash_lock != NULL)
5833 mutex_exit(hash_lock);
5834 rc = SET_ERROR(ENOENT);
5835 goto done;
5836 }
5837
5838 if (hdr == NULL) {
5839 /*
5840 * This block is not in the cache or it has
5841 * embedded data.
5842 */
5843 arc_buf_hdr_t *exists = NULL;
5844 hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize,
5845 BP_IS_PROTECTED(bp), BP_GET_COMPRESS(bp), 0, type);
5846
5847 if (!embedded_bp) {
5848 hdr->b_dva = *BP_IDENTITY(bp);
5849 hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
5850 exists = buf_hash_insert(hdr, &hash_lock);
5851 }
5852 if (exists != NULL) {
5853 /* somebody beat us to the hash insert */
5854 mutex_exit(hash_lock);
5855 buf_discard_identity(hdr);
5856 arc_hdr_destroy(hdr);
5857 goto top; /* restart the IO request */
5858 }
5859 } else {
5860 /*
5861 * This block is in the ghost cache or encrypted data
5862 * was requested and we didn't have it. If it was
5863 * L2-only (and thus didn't have an L1 hdr),
5864 * we realloc the header to add an L1 hdr.
5865 */
5866 if (!HDR_HAS_L1HDR(hdr)) {
5867 hdr = arc_hdr_realloc(hdr, hdr_l2only_cache,
5868 hdr_full_cache);
5869 }
5870
5871 if (GHOST_STATE(hdr->b_l1hdr.b_state)) {
5872 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
5873 ASSERT(!HDR_HAS_RABD(hdr));
5874 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
5875 ASSERT0(zfs_refcount_count(
5876 &hdr->b_l1hdr.b_refcnt));
5877 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
5878 #ifdef ZFS_DEBUG
5879 ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL);
5880 #endif
5881 } else if (HDR_IO_IN_PROGRESS(hdr)) {
5882 /*
5883 * If this header already had an IO in progress
5884 * and we are performing another IO to fetch
5885 * encrypted data we must wait until the first
5886 * IO completes so as not to confuse
5887 * arc_read_done(). This should be very rare
5888 * and so the performance impact shouldn't
5889 * matter.
5890 */
5891 arc_callback_t *acb = kmem_zalloc(
5892 sizeof (arc_callback_t), KM_SLEEP);
5893 acb->acb_wait = B_TRUE;
5894 mutex_init(&acb->acb_wait_lock, NULL,
5895 MUTEX_DEFAULT, NULL);
5896 cv_init(&acb->acb_wait_cv, NULL, CV_DEFAULT,
5897 NULL);
5898 acb->acb_zio_head =
5899 hdr->b_l1hdr.b_acb->acb_zio_head;
5900 acb->acb_next = hdr->b_l1hdr.b_acb;
5901 hdr->b_l1hdr.b_acb->acb_prev = acb;
5902 hdr->b_l1hdr.b_acb = acb;
5903 mutex_exit(hash_lock);
5904 mutex_enter(&acb->acb_wait_lock);
5905 while (acb->acb_wait) {
5906 cv_wait(&acb->acb_wait_cv,
5907 &acb->acb_wait_lock);
5908 }
5909 mutex_exit(&acb->acb_wait_lock);
5910 mutex_destroy(&acb->acb_wait_lock);
5911 cv_destroy(&acb->acb_wait_cv);
5912 kmem_free(acb, sizeof (arc_callback_t));
5913 goto top;
5914 }
5915 }
5916 if (*arc_flags & ARC_FLAG_UNCACHED) {
5917 arc_hdr_set_flags(hdr, ARC_FLAG_UNCACHED);
5918 if (!encrypted_read)
5919 alloc_flags |= ARC_HDR_ALLOC_LINEAR;
5920 }
5921
5922 /*
5923 * Take additional reference for IO_IN_PROGRESS. It stops
5924 * arc_access() from putting this header without any buffers
5925 * and so other references but obviously nonevictable onto
5926 * the evictable list of MRU or MFU state.
5927 */
5928 add_reference(hdr, hdr);
5929 if (!embedded_bp)
5930 arc_access(hdr, *arc_flags, B_FALSE);
5931 arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS);
5932 arc_hdr_alloc_abd(hdr, alloc_flags);
5933 if (encrypted_read) {
5934 ASSERT(HDR_HAS_RABD(hdr));
5935 size = HDR_GET_PSIZE(hdr);
5936 hdr_abd = hdr->b_crypt_hdr.b_rabd;
5937 zio_flags |= ZIO_FLAG_RAW;
5938 } else {
5939 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
5940 size = arc_hdr_size(hdr);
5941 hdr_abd = hdr->b_l1hdr.b_pabd;
5942
5943 if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) {
5944 zio_flags |= ZIO_FLAG_RAW_COMPRESS;
5945 }
5946
5947 /*
5948 * For authenticated bp's, we do not ask the ZIO layer
5949 * to authenticate them since this will cause the entire
5950 * IO to fail if the key isn't loaded. Instead, we
5951 * defer authentication until arc_buf_fill(), which will
5952 * verify the data when the key is available.
5953 */
5954 if (BP_IS_AUTHENTICATED(bp))
5955 zio_flags |= ZIO_FLAG_RAW_ENCRYPT;
5956 }
5957
5958 if (BP_IS_AUTHENTICATED(bp))
5959 arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH);
5960 if (BP_GET_LEVEL(bp) > 0)
5961 arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT);
5962 ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state));
5963
5964 acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
5965 acb->acb_done = done;
5966 acb->acb_private = private;
5967 acb->acb_compressed = compressed_read;
5968 acb->acb_encrypted = encrypted_read;
5969 acb->acb_noauth = noauth_read;
5970 acb->acb_zb = *zb;
5971
5972 ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL);
5973 hdr->b_l1hdr.b_acb = acb;
5974
5975 if (HDR_HAS_L2HDR(hdr) &&
5976 (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) {
5977 devw = hdr->b_l2hdr.b_dev->l2ad_writing;
5978 addr = hdr->b_l2hdr.b_daddr;
5979 /*
5980 * Lock out L2ARC device removal.
5981 */
5982 if (vdev_is_dead(vd) ||
5983 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
5984 vd = NULL;
5985 }
5986
5987 /*
5988 * We count both async reads and scrub IOs as asynchronous so
5989 * that both can be upgraded in the event of a cache hit while
5990 * the read IO is still in-flight.
5991 */
5992 if (priority == ZIO_PRIORITY_ASYNC_READ ||
5993 priority == ZIO_PRIORITY_SCRUB)
5994 arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ);
5995 else
5996 arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ);
5997
5998 /*
5999 * At this point, we have a level 1 cache miss or a blkptr
6000 * with embedded data. Try again in L2ARC if possible.
6001 */
6002 ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize);
6003
6004 /*
6005 * Skip ARC stat bump for block pointers with embedded
6006 * data. The data are read from the blkptr itself via
6007 * decode_embedded_bp_compressed().
6008 */
6009 if (!embedded_bp) {
6010 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr,
6011 blkptr_t *, bp, uint64_t, lsize,
6012 zbookmark_phys_t *, zb);
6013 ARCSTAT_BUMP(arcstat_misses);
6014 ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH),
6015 demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data,
6016 metadata, misses);
6017 zfs_racct_read(size, 1);
6018 }
6019
6020 /* Check if the spa even has l2 configured */
6021 const boolean_t spa_has_l2 = l2arc_ndev != 0 &&
6022 spa->spa_l2cache.sav_count > 0;
6023
6024 if (vd != NULL && spa_has_l2 && !(l2arc_norw && devw)) {
6025 /*
6026 * Read from the L2ARC if the following are true:
6027 * 1. The L2ARC vdev was previously cached.
6028 * 2. This buffer still has L2ARC metadata.
6029 * 3. This buffer isn't currently writing to the L2ARC.
6030 * 4. The L2ARC entry wasn't evicted, which may
6031 * also have invalidated the vdev.
6032 * 5. This isn't prefetch or l2arc_noprefetch is 0.
6033 */
6034 if (HDR_HAS_L2HDR(hdr) &&
6035 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
6036 !(l2arc_noprefetch &&
6037 (*arc_flags & ARC_FLAG_PREFETCH))) {
6038 l2arc_read_callback_t *cb;
6039 abd_t *abd;
6040 uint64_t asize;
6041
6042 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
6043 ARCSTAT_BUMP(arcstat_l2_hits);
6044 hdr->b_l2hdr.b_hits++;
6045
6046 cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
6047 KM_SLEEP);
6048 cb->l2rcb_hdr = hdr;
6049 cb->l2rcb_bp = *bp;
6050 cb->l2rcb_zb = *zb;
6051 cb->l2rcb_flags = zio_flags;
6052
6053 /*
6054 * When Compressed ARC is disabled, but the
6055 * L2ARC block is compressed, arc_hdr_size()
6056 * will have returned LSIZE rather than PSIZE.
6057 */
6058 if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF &&
6059 !HDR_COMPRESSION_ENABLED(hdr) &&
6060 HDR_GET_PSIZE(hdr) != 0) {
6061 size = HDR_GET_PSIZE(hdr);
6062 }
6063
6064 asize = vdev_psize_to_asize(vd, size);
6065 if (asize != size) {
6066 abd = abd_alloc_for_io(asize,
6067 HDR_ISTYPE_METADATA(hdr));
6068 cb->l2rcb_abd = abd;
6069 } else {
6070 abd = hdr_abd;
6071 }
6072
6073 ASSERT(addr >= VDEV_LABEL_START_SIZE &&
6074 addr + asize <= vd->vdev_psize -
6075 VDEV_LABEL_END_SIZE);
6076
6077 /*
6078 * l2arc read. The SCL_L2ARC lock will be
6079 * released by l2arc_read_done().
6080 * Issue a null zio if the underlying buffer
6081 * was squashed to zero size by compression.
6082 */
6083 ASSERT3U(arc_hdr_get_compress(hdr), !=,
6084 ZIO_COMPRESS_EMPTY);
6085 rzio = zio_read_phys(pio, vd, addr,
6086 asize, abd,
6087 ZIO_CHECKSUM_OFF,
6088 l2arc_read_done, cb, priority,
6089 zio_flags | ZIO_FLAG_CANFAIL |
6090 ZIO_FLAG_DONT_PROPAGATE |
6091 ZIO_FLAG_DONT_RETRY, B_FALSE);
6092 acb->acb_zio_head = rzio;
6093
6094 if (hash_lock != NULL)
6095 mutex_exit(hash_lock);
6096
6097 DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
6098 zio_t *, rzio);
6099 ARCSTAT_INCR(arcstat_l2_read_bytes,
6100 HDR_GET_PSIZE(hdr));
6101
6102 if (*arc_flags & ARC_FLAG_NOWAIT) {
6103 zio_nowait(rzio);
6104 goto out;
6105 }
6106
6107 ASSERT(*arc_flags & ARC_FLAG_WAIT);
6108 if (zio_wait(rzio) == 0)
6109 goto out;
6110
6111 /* l2arc read error; goto zio_read() */
6112 if (hash_lock != NULL)
6113 mutex_enter(hash_lock);
6114 } else {
6115 DTRACE_PROBE1(l2arc__miss,
6116 arc_buf_hdr_t *, hdr);
6117 ARCSTAT_BUMP(arcstat_l2_misses);
6118 if (HDR_L2_WRITING(hdr))
6119 ARCSTAT_BUMP(arcstat_l2_rw_clash);
6120 spa_config_exit(spa, SCL_L2ARC, vd);
6121 }
6122 } else {
6123 if (vd != NULL)
6124 spa_config_exit(spa, SCL_L2ARC, vd);
6125
6126 /*
6127 * Only a spa with l2 should contribute to l2
6128 * miss stats. (Including the case of having a
6129 * faulted cache device - that's also a miss.)
6130 */
6131 if (spa_has_l2) {
6132 /*
6133 * Skip ARC stat bump for block pointers with
6134 * embedded data. The data are read from the
6135 * blkptr itself via
6136 * decode_embedded_bp_compressed().
6137 */
6138 if (!embedded_bp) {
6139 DTRACE_PROBE1(l2arc__miss,
6140 arc_buf_hdr_t *, hdr);
6141 ARCSTAT_BUMP(arcstat_l2_misses);
6142 }
6143 }
6144 }
6145
6146 rzio = zio_read(pio, spa, bp, hdr_abd, size,
6147 arc_read_done, hdr, priority, zio_flags, zb);
6148 acb->acb_zio_head = rzio;
6149
6150 if (hash_lock != NULL)
6151 mutex_exit(hash_lock);
6152
6153 if (*arc_flags & ARC_FLAG_WAIT) {
6154 rc = zio_wait(rzio);
6155 goto out;
6156 }
6157
6158 ASSERT(*arc_flags & ARC_FLAG_NOWAIT);
6159 zio_nowait(rzio);
6160 }
6161
6162 out:
6163 /* embedded bps don't actually go to disk */
6164 if (!embedded_bp)
6165 spa_read_history_add(spa, zb, *arc_flags);
6166 spl_fstrans_unmark(cookie);
6167 return (rc);
6168
6169 done:
6170 if (done)
6171 done(NULL, zb, bp, buf, private);
6172 if (pio && rc != 0) {
6173 zio_t *zio = zio_null(pio, spa, NULL, NULL, NULL, zio_flags);
6174 zio->io_error = rc;
6175 zio_nowait(zio);
6176 }
6177 goto out;
6178 }
6179
6180 arc_prune_t *
6181 arc_add_prune_callback(arc_prune_func_t *func, void *private)
6182 {
6183 arc_prune_t *p;
6184
6185 p = kmem_alloc(sizeof (*p), KM_SLEEP);
6186 p->p_pfunc = func;
6187 p->p_private = private;
6188 list_link_init(&p->p_node);
6189 zfs_refcount_create(&p->p_refcnt);
6190
6191 mutex_enter(&arc_prune_mtx);
6192 zfs_refcount_add(&p->p_refcnt, &arc_prune_list);
6193 list_insert_head(&arc_prune_list, p);
6194 mutex_exit(&arc_prune_mtx);
6195
6196 return (p);
6197 }
6198
6199 void
6200 arc_remove_prune_callback(arc_prune_t *p)
6201 {
6202 boolean_t wait = B_FALSE;
6203 mutex_enter(&arc_prune_mtx);
6204 list_remove(&arc_prune_list, p);
6205 if (zfs_refcount_remove(&p->p_refcnt, &arc_prune_list) > 0)
6206 wait = B_TRUE;
6207 mutex_exit(&arc_prune_mtx);
6208
6209 /* wait for arc_prune_task to finish */
6210 if (wait)
6211 taskq_wait_outstanding(arc_prune_taskq, 0);
6212 ASSERT0(zfs_refcount_count(&p->p_refcnt));
6213 zfs_refcount_destroy(&p->p_refcnt);
6214 kmem_free(p, sizeof (*p));
6215 }
6216
6217 /*
6218 * Notify the arc that a block was freed, and thus will never be used again.
6219 */
6220 void
6221 arc_freed(spa_t *spa, const blkptr_t *bp)
6222 {
6223 arc_buf_hdr_t *hdr;
6224 kmutex_t *hash_lock;
6225 uint64_t guid = spa_load_guid(spa);
6226
6227 ASSERT(!BP_IS_EMBEDDED(bp));
6228
6229 hdr = buf_hash_find(guid, bp, &hash_lock);
6230 if (hdr == NULL)
6231 return;
6232
6233 /*
6234 * We might be trying to free a block that is still doing I/O
6235 * (i.e. prefetch) or has some other reference (i.e. a dedup-ed,
6236 * dmu_sync-ed block). A block may also have a reference if it is
6237 * part of a dedup-ed, dmu_synced write. The dmu_sync() function would
6238 * have written the new block to its final resting place on disk but
6239 * without the dedup flag set. This would have left the hdr in the MRU
6240 * state and discoverable. When the txg finally syncs it detects that
6241 * the block was overridden in open context and issues an override I/O.
6242 * Since this is a dedup block, the override I/O will determine if the
6243 * block is already in the DDT. If so, then it will replace the io_bp
6244 * with the bp from the DDT and allow the I/O to finish. When the I/O
6245 * reaches the done callback, dbuf_write_override_done, it will
6246 * check to see if the io_bp and io_bp_override are identical.
6247 * If they are not, then it indicates that the bp was replaced with
6248 * the bp in the DDT and the override bp is freed. This allows
6249 * us to arrive here with a reference on a block that is being
6250 * freed. So if we have an I/O in progress, or a reference to
6251 * this hdr, then we don't destroy the hdr.
6252 */
6253 if (!HDR_HAS_L1HDR(hdr) ||
6254 zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) {
6255 arc_change_state(arc_anon, hdr);
6256 arc_hdr_destroy(hdr);
6257 mutex_exit(hash_lock);
6258 } else {
6259 mutex_exit(hash_lock);
6260 }
6261
6262 }
6263
6264 /*
6265 * Release this buffer from the cache, making it an anonymous buffer. This
6266 * must be done after a read and prior to modifying the buffer contents.
6267 * If the buffer has more than one reference, we must make
6268 * a new hdr for the buffer.
6269 */
6270 void
6271 arc_release(arc_buf_t *buf, const void *tag)
6272 {
6273 arc_buf_hdr_t *hdr = buf->b_hdr;
6274
6275 /*
6276 * It would be nice to assert that if its DMU metadata (level >
6277 * 0 || it's the dnode file), then it must be syncing context.
6278 * But we don't know that information at this level.
6279 */
6280
6281 ASSERT(HDR_HAS_L1HDR(hdr));
6282
6283 /*
6284 * We don't grab the hash lock prior to this check, because if
6285 * the buffer's header is in the arc_anon state, it won't be
6286 * linked into the hash table.
6287 */
6288 if (hdr->b_l1hdr.b_state == arc_anon) {
6289 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
6290 ASSERT(!HDR_IN_HASH_TABLE(hdr));
6291 ASSERT(!HDR_HAS_L2HDR(hdr));
6292
6293 ASSERT3P(hdr->b_l1hdr.b_buf, ==, buf);
6294 ASSERT(ARC_BUF_LAST(buf));
6295 ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1);
6296 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
6297
6298 hdr->b_l1hdr.b_arc_access = 0;
6299
6300 /*
6301 * If the buf is being overridden then it may already
6302 * have a hdr that is not empty.
6303 */
6304 buf_discard_identity(hdr);
6305 arc_buf_thaw(buf);
6306
6307 return;
6308 }
6309
6310 kmutex_t *hash_lock = HDR_LOCK(hdr);
6311 mutex_enter(hash_lock);
6312
6313 /*
6314 * This assignment is only valid as long as the hash_lock is
6315 * held, we must be careful not to reference state or the
6316 * b_state field after dropping the lock.
6317 */
6318 arc_state_t *state = hdr->b_l1hdr.b_state;
6319 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
6320 ASSERT3P(state, !=, arc_anon);
6321
6322 /* this buffer is not on any list */
6323 ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), >, 0);
6324
6325 if (HDR_HAS_L2HDR(hdr)) {
6326 mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx);
6327
6328 /*
6329 * We have to recheck this conditional again now that
6330 * we're holding the l2ad_mtx to prevent a race with
6331 * another thread which might be concurrently calling
6332 * l2arc_evict(). In that case, l2arc_evict() might have
6333 * destroyed the header's L2 portion as we were waiting
6334 * to acquire the l2ad_mtx.
6335 */
6336 if (HDR_HAS_L2HDR(hdr))
6337 arc_hdr_l2hdr_destroy(hdr);
6338
6339 mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx);
6340 }
6341
6342 /*
6343 * Do we have more than one buf?
6344 */
6345 if (hdr->b_l1hdr.b_buf != buf || !ARC_BUF_LAST(buf)) {
6346 arc_buf_hdr_t *nhdr;
6347 uint64_t spa = hdr->b_spa;
6348 uint64_t psize = HDR_GET_PSIZE(hdr);
6349 uint64_t lsize = HDR_GET_LSIZE(hdr);
6350 boolean_t protected = HDR_PROTECTED(hdr);
6351 enum zio_compress compress = arc_hdr_get_compress(hdr);
6352 arc_buf_contents_t type = arc_buf_type(hdr);
6353 VERIFY3U(hdr->b_type, ==, type);
6354
6355 ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL);
6356 VERIFY3S(remove_reference(hdr, tag), >, 0);
6357
6358 if (arc_buf_is_shared(buf) && !ARC_BUF_COMPRESSED(buf)) {
6359 ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf);
6360 ASSERT(ARC_BUF_LAST(buf));
6361 }
6362
6363 /*
6364 * Pull the data off of this hdr and attach it to
6365 * a new anonymous hdr. Also find the last buffer
6366 * in the hdr's buffer list.
6367 */
6368 arc_buf_t *lastbuf = arc_buf_remove(hdr, buf);
6369 ASSERT3P(lastbuf, !=, NULL);
6370
6371 /*
6372 * If the current arc_buf_t and the hdr are sharing their data
6373 * buffer, then we must stop sharing that block.
6374 */
6375 if (arc_buf_is_shared(buf)) {
6376 ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf);
6377 VERIFY(!arc_buf_is_shared(lastbuf));
6378
6379 /*
6380 * First, sever the block sharing relationship between
6381 * buf and the arc_buf_hdr_t.
6382 */
6383 arc_unshare_buf(hdr, buf);
6384
6385 /*
6386 * Now we need to recreate the hdr's b_pabd. Since we
6387 * have lastbuf handy, we try to share with it, but if
6388 * we can't then we allocate a new b_pabd and copy the
6389 * data from buf into it.
6390 */
6391 if (arc_can_share(hdr, lastbuf)) {
6392 arc_share_buf(hdr, lastbuf);
6393 } else {
6394 arc_hdr_alloc_abd(hdr, 0);
6395 abd_copy_from_buf(hdr->b_l1hdr.b_pabd,
6396 buf->b_data, psize);
6397 }
6398 VERIFY3P(lastbuf->b_data, !=, NULL);
6399 } else if (HDR_SHARED_DATA(hdr)) {
6400 /*
6401 * Uncompressed shared buffers are always at the end
6402 * of the list. Compressed buffers don't have the
6403 * same requirements. This makes it hard to
6404 * simply assert that the lastbuf is shared so
6405 * we rely on the hdr's compression flags to determine
6406 * if we have a compressed, shared buffer.
6407 */
6408 ASSERT(arc_buf_is_shared(lastbuf) ||
6409 arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF);
6410 ASSERT(!ARC_BUF_SHARED(buf));
6411 }
6412
6413 ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr));
6414 ASSERT3P(state, !=, arc_l2c_only);
6415
6416 (void) zfs_refcount_remove_many(&state->arcs_size[type],
6417 arc_buf_size(buf), buf);
6418
6419 if (zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) {
6420 ASSERT3P(state, !=, arc_l2c_only);
6421 (void) zfs_refcount_remove_many(
6422 &state->arcs_esize[type],
6423 arc_buf_size(buf), buf);
6424 }
6425
6426 arc_cksum_verify(buf);
6427 arc_buf_unwatch(buf);
6428
6429 /* if this is the last uncompressed buf free the checksum */
6430 if (!arc_hdr_has_uncompressed_buf(hdr))
6431 arc_cksum_free(hdr);
6432
6433 mutex_exit(hash_lock);
6434
6435 nhdr = arc_hdr_alloc(spa, psize, lsize, protected,
6436 compress, hdr->b_complevel, type);
6437 ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL);
6438 ASSERT0(zfs_refcount_count(&nhdr->b_l1hdr.b_refcnt));
6439 VERIFY3U(nhdr->b_type, ==, type);
6440 ASSERT(!HDR_SHARED_DATA(nhdr));
6441
6442 nhdr->b_l1hdr.b_buf = buf;
6443 (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, tag);
6444 buf->b_hdr = nhdr;
6445
6446 (void) zfs_refcount_add_many(&arc_anon->arcs_size[type],
6447 arc_buf_size(buf), buf);
6448 } else {
6449 ASSERT(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 1);
6450 /* protected by hash lock, or hdr is on arc_anon */
6451 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
6452 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
6453 hdr->b_l1hdr.b_mru_hits = 0;
6454 hdr->b_l1hdr.b_mru_ghost_hits = 0;
6455 hdr->b_l1hdr.b_mfu_hits = 0;
6456 hdr->b_l1hdr.b_mfu_ghost_hits = 0;
6457 arc_change_state(arc_anon, hdr);
6458 hdr->b_l1hdr.b_arc_access = 0;
6459
6460 mutex_exit(hash_lock);
6461 buf_discard_identity(hdr);
6462 arc_buf_thaw(buf);
6463 }
6464 }
6465
6466 int
6467 arc_released(arc_buf_t *buf)
6468 {
6469 return (buf->b_data != NULL &&
6470 buf->b_hdr->b_l1hdr.b_state == arc_anon);
6471 }
6472
6473 #ifdef ZFS_DEBUG
6474 int
6475 arc_referenced(arc_buf_t *buf)
6476 {
6477 return (zfs_refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt));
6478 }
6479 #endif
6480
6481 static void
6482 arc_write_ready(zio_t *zio)
6483 {
6484 arc_write_callback_t *callback = zio->io_private;
6485 arc_buf_t *buf = callback->awcb_buf;
6486 arc_buf_hdr_t *hdr = buf->b_hdr;
6487 blkptr_t *bp = zio->io_bp;
6488 uint64_t psize = BP_IS_HOLE(bp) ? 0 : BP_GET_PSIZE(bp);
6489 fstrans_cookie_t cookie = spl_fstrans_mark();
6490
6491 ASSERT(HDR_HAS_L1HDR(hdr));
6492 ASSERT(!zfs_refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt));
6493 ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL);
6494
6495 /*
6496 * If we're reexecuting this zio because the pool suspended, then
6497 * cleanup any state that was previously set the first time the
6498 * callback was invoked.
6499 */
6500 if (zio->io_flags & ZIO_FLAG_REEXECUTED) {
6501 arc_cksum_free(hdr);
6502 arc_buf_unwatch(buf);
6503 if (hdr->b_l1hdr.b_pabd != NULL) {
6504 if (arc_buf_is_shared(buf)) {
6505 arc_unshare_buf(hdr, buf);
6506 } else {
6507 arc_hdr_free_abd(hdr, B_FALSE);
6508 }
6509 }
6510
6511 if (HDR_HAS_RABD(hdr))
6512 arc_hdr_free_abd(hdr, B_TRUE);
6513 }
6514 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
6515 ASSERT(!HDR_HAS_RABD(hdr));
6516 ASSERT(!HDR_SHARED_DATA(hdr));
6517 ASSERT(!arc_buf_is_shared(buf));
6518
6519 callback->awcb_ready(zio, buf, callback->awcb_private);
6520
6521 if (HDR_IO_IN_PROGRESS(hdr)) {
6522 ASSERT(zio->io_flags & ZIO_FLAG_REEXECUTED);
6523 } else {
6524 arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS);
6525 add_reference(hdr, hdr); /* For IO_IN_PROGRESS. */
6526 }
6527
6528 if (BP_IS_PROTECTED(bp) != !!HDR_PROTECTED(hdr))
6529 hdr = arc_hdr_realloc_crypt(hdr, BP_IS_PROTECTED(bp));
6530
6531 if (BP_IS_PROTECTED(bp)) {
6532 /* ZIL blocks are written through zio_rewrite */
6533 ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG);
6534 ASSERT(HDR_PROTECTED(hdr));
6535
6536 if (BP_SHOULD_BYTESWAP(bp)) {
6537 if (BP_GET_LEVEL(bp) > 0) {
6538 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64;
6539 } else {
6540 hdr->b_l1hdr.b_byteswap =
6541 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
6542 }
6543 } else {
6544 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS;
6545 }
6546
6547 hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp);
6548 hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset;
6549 zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt,
6550 hdr->b_crypt_hdr.b_iv);
6551 zio_crypt_decode_mac_bp(bp, hdr->b_crypt_hdr.b_mac);
6552 }
6553
6554 /*
6555 * If this block was written for raw encryption but the zio layer
6556 * ended up only authenticating it, adjust the buffer flags now.
6557 */
6558 if (BP_IS_AUTHENTICATED(bp) && ARC_BUF_ENCRYPTED(buf)) {
6559 arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH);
6560 buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED;
6561 if (BP_GET_COMPRESS(bp) == ZIO_COMPRESS_OFF)
6562 buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED;
6563 } else if (BP_IS_HOLE(bp) && ARC_BUF_ENCRYPTED(buf)) {
6564 buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED;
6565 buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED;
6566 }
6567
6568 /* this must be done after the buffer flags are adjusted */
6569 arc_cksum_compute(buf);
6570
6571 enum zio_compress compress;
6572 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) {
6573 compress = ZIO_COMPRESS_OFF;
6574 } else {
6575 ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp));
6576 compress = BP_GET_COMPRESS(bp);
6577 }
6578 HDR_SET_PSIZE(hdr, psize);
6579 arc_hdr_set_compress(hdr, compress);
6580 hdr->b_complevel = zio->io_prop.zp_complevel;
6581
6582 if (zio->io_error != 0 || psize == 0)
6583 goto out;
6584
6585 /*
6586 * Fill the hdr with data. If the buffer is encrypted we have no choice
6587 * but to copy the data into b_radb. If the hdr is compressed, the data
6588 * we want is available from the zio, otherwise we can take it from
6589 * the buf.
6590 *
6591 * We might be able to share the buf's data with the hdr here. However,
6592 * doing so would cause the ARC to be full of linear ABDs if we write a
6593 * lot of shareable data. As a compromise, we check whether scattered
6594 * ABDs are allowed, and assume that if they are then the user wants
6595 * the ARC to be primarily filled with them regardless of the data being
6596 * written. Therefore, if they're allowed then we allocate one and copy
6597 * the data into it; otherwise, we share the data directly if we can.
6598 */
6599 if (ARC_BUF_ENCRYPTED(buf)) {
6600 ASSERT3U(psize, >, 0);
6601 ASSERT(ARC_BUF_COMPRESSED(buf));
6602 arc_hdr_alloc_abd(hdr, ARC_HDR_ALLOC_RDATA |
6603 ARC_HDR_USE_RESERVE);
6604 abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize);
6605 } else if (!(HDR_UNCACHED(hdr) ||
6606 abd_size_alloc_linear(arc_buf_size(buf))) ||
6607 !arc_can_share(hdr, buf)) {
6608 /*
6609 * Ideally, we would always copy the io_abd into b_pabd, but the
6610 * user may have disabled compressed ARC, thus we must check the
6611 * hdr's compression setting rather than the io_bp's.
6612 */
6613 if (BP_IS_ENCRYPTED(bp)) {
6614 ASSERT3U(psize, >, 0);
6615 arc_hdr_alloc_abd(hdr, ARC_HDR_ALLOC_RDATA |
6616 ARC_HDR_USE_RESERVE);
6617 abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize);
6618 } else if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF &&
6619 !ARC_BUF_COMPRESSED(buf)) {
6620 ASSERT3U(psize, >, 0);
6621 arc_hdr_alloc_abd(hdr, ARC_HDR_USE_RESERVE);
6622 abd_copy(hdr->b_l1hdr.b_pabd, zio->io_abd, psize);
6623 } else {
6624 ASSERT3U(zio->io_orig_size, ==, arc_hdr_size(hdr));
6625 arc_hdr_alloc_abd(hdr, ARC_HDR_USE_RESERVE);
6626 abd_copy_from_buf(hdr->b_l1hdr.b_pabd, buf->b_data,
6627 arc_buf_size(buf));
6628 }
6629 } else {
6630 ASSERT3P(buf->b_data, ==, abd_to_buf(zio->io_orig_abd));
6631 ASSERT3U(zio->io_orig_size, ==, arc_buf_size(buf));
6632 ASSERT3P(hdr->b_l1hdr.b_buf, ==, buf);
6633 ASSERT(ARC_BUF_LAST(buf));
6634
6635 arc_share_buf(hdr, buf);
6636 }
6637
6638 out:
6639 arc_hdr_verify(hdr, bp);
6640 spl_fstrans_unmark(cookie);
6641 }
6642
6643 static void
6644 arc_write_children_ready(zio_t *zio)
6645 {
6646 arc_write_callback_t *callback = zio->io_private;
6647 arc_buf_t *buf = callback->awcb_buf;
6648
6649 callback->awcb_children_ready(zio, buf, callback->awcb_private);
6650 }
6651
6652 static void
6653 arc_write_done(zio_t *zio)
6654 {
6655 arc_write_callback_t *callback = zio->io_private;
6656 arc_buf_t *buf = callback->awcb_buf;
6657 arc_buf_hdr_t *hdr = buf->b_hdr;
6658
6659 ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL);
6660
6661 if (zio->io_error == 0) {
6662 arc_hdr_verify(hdr, zio->io_bp);
6663
6664 if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) {
6665 buf_discard_identity(hdr);
6666 } else {
6667 hdr->b_dva = *BP_IDENTITY(zio->io_bp);
6668 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
6669 }
6670 } else {
6671 ASSERT(HDR_EMPTY(hdr));
6672 }
6673
6674 /*
6675 * If the block to be written was all-zero or compressed enough to be
6676 * embedded in the BP, no write was performed so there will be no
6677 * dva/birth/checksum. The buffer must therefore remain anonymous
6678 * (and uncached).
6679 */
6680 if (!HDR_EMPTY(hdr)) {
6681 arc_buf_hdr_t *exists;
6682 kmutex_t *hash_lock;
6683
6684 ASSERT3U(zio->io_error, ==, 0);
6685
6686 arc_cksum_verify(buf);
6687
6688 exists = buf_hash_insert(hdr, &hash_lock);
6689 if (exists != NULL) {
6690 /*
6691 * This can only happen if we overwrite for
6692 * sync-to-convergence, because we remove
6693 * buffers from the hash table when we arc_free().
6694 */
6695 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
6696 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
6697 panic("bad overwrite, hdr=%p exists=%p",
6698 (void *)hdr, (void *)exists);
6699 ASSERT(zfs_refcount_is_zero(
6700 &exists->b_l1hdr.b_refcnt));
6701 arc_change_state(arc_anon, exists);
6702 arc_hdr_destroy(exists);
6703 mutex_exit(hash_lock);
6704 exists = buf_hash_insert(hdr, &hash_lock);
6705 ASSERT3P(exists, ==, NULL);
6706 } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
6707 /* nopwrite */
6708 ASSERT(zio->io_prop.zp_nopwrite);
6709 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
6710 panic("bad nopwrite, hdr=%p exists=%p",
6711 (void *)hdr, (void *)exists);
6712 } else {
6713 /* Dedup */
6714 ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL);
6715 ASSERT(ARC_BUF_LAST(hdr->b_l1hdr.b_buf));
6716 ASSERT(hdr->b_l1hdr.b_state == arc_anon);
6717 ASSERT(BP_GET_DEDUP(zio->io_bp));
6718 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
6719 }
6720 }
6721 arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS);
6722 VERIFY3S(remove_reference(hdr, hdr), >, 0);
6723 /* if it's not anon, we are doing a scrub */
6724 if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon)
6725 arc_access(hdr, 0, B_FALSE);
6726 mutex_exit(hash_lock);
6727 } else {
6728 arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS);
6729 VERIFY3S(remove_reference(hdr, hdr), >, 0);
6730 }
6731
6732 callback->awcb_done(zio, buf, callback->awcb_private);
6733
6734 abd_free(zio->io_abd);
6735 kmem_free(callback, sizeof (arc_write_callback_t));
6736 }
6737
6738 zio_t *
6739 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
6740 blkptr_t *bp, arc_buf_t *buf, boolean_t uncached, boolean_t l2arc,
6741 const zio_prop_t *zp, arc_write_done_func_t *ready,
6742 arc_write_done_func_t *children_ready, arc_write_done_func_t *done,
6743 void *private, zio_priority_t priority, int zio_flags,
6744 const zbookmark_phys_t *zb)
6745 {
6746 arc_buf_hdr_t *hdr = buf->b_hdr;
6747 arc_write_callback_t *callback;
6748 zio_t *zio;
6749 zio_prop_t localprop = *zp;
6750
6751 ASSERT3P(ready, !=, NULL);
6752 ASSERT3P(done, !=, NULL);
6753 ASSERT(!HDR_IO_ERROR(hdr));
6754 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
6755 ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL);
6756 ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL);
6757 if (uncached)
6758 arc_hdr_set_flags(hdr, ARC_FLAG_UNCACHED);
6759 else if (l2arc)
6760 arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE);
6761
6762 if (ARC_BUF_ENCRYPTED(buf)) {
6763 ASSERT(ARC_BUF_COMPRESSED(buf));
6764 localprop.zp_encrypt = B_TRUE;
6765 localprop.zp_compress = HDR_GET_COMPRESS(hdr);
6766 localprop.zp_complevel = hdr->b_complevel;
6767 localprop.zp_byteorder =
6768 (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ?
6769 ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER;
6770 memcpy(localprop.zp_salt, hdr->b_crypt_hdr.b_salt,
6771 ZIO_DATA_SALT_LEN);
6772 memcpy(localprop.zp_iv, hdr->b_crypt_hdr.b_iv,
6773 ZIO_DATA_IV_LEN);
6774 memcpy(localprop.zp_mac, hdr->b_crypt_hdr.b_mac,
6775 ZIO_DATA_MAC_LEN);
6776 if (DMU_OT_IS_ENCRYPTED(localprop.zp_type)) {
6777 localprop.zp_nopwrite = B_FALSE;
6778 localprop.zp_copies =
6779 MIN(localprop.zp_copies, SPA_DVAS_PER_BP - 1);
6780 }
6781 zio_flags |= ZIO_FLAG_RAW;
6782 } else if (ARC_BUF_COMPRESSED(buf)) {
6783 ASSERT3U(HDR_GET_LSIZE(hdr), !=, arc_buf_size(buf));
6784 localprop.zp_compress = HDR_GET_COMPRESS(hdr);
6785 localprop.zp_complevel = hdr->b_complevel;
6786 zio_flags |= ZIO_FLAG_RAW_COMPRESS;
6787 }
6788 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
6789 callback->awcb_ready = ready;
6790 callback->awcb_children_ready = children_ready;
6791 callback->awcb_done = done;
6792 callback->awcb_private = private;
6793 callback->awcb_buf = buf;
6794
6795 /*
6796 * The hdr's b_pabd is now stale, free it now. A new data block
6797 * will be allocated when the zio pipeline calls arc_write_ready().
6798 */
6799 if (hdr->b_l1hdr.b_pabd != NULL) {
6800 /*
6801 * If the buf is currently sharing the data block with
6802 * the hdr then we need to break that relationship here.
6803 * The hdr will remain with a NULL data pointer and the
6804 * buf will take sole ownership of the block.
6805 */
6806 if (arc_buf_is_shared(buf)) {
6807 arc_unshare_buf(hdr, buf);
6808 } else {
6809 arc_hdr_free_abd(hdr, B_FALSE);
6810 }
6811 VERIFY3P(buf->b_data, !=, NULL);
6812 }
6813
6814 if (HDR_HAS_RABD(hdr))
6815 arc_hdr_free_abd(hdr, B_TRUE);
6816
6817 if (!(zio_flags & ZIO_FLAG_RAW))
6818 arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF);
6819
6820 ASSERT(!arc_buf_is_shared(buf));
6821 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
6822
6823 zio = zio_write(pio, spa, txg, bp,
6824 abd_get_from_buf(buf->b_data, HDR_GET_LSIZE(hdr)),
6825 HDR_GET_LSIZE(hdr), arc_buf_size(buf), &localprop, arc_write_ready,
6826 (children_ready != NULL) ? arc_write_children_ready : NULL,
6827 arc_write_done, callback, priority, zio_flags, zb);
6828
6829 return (zio);
6830 }
6831
6832 void
6833 arc_tempreserve_clear(uint64_t reserve)
6834 {
6835 atomic_add_64(&arc_tempreserve, -reserve);
6836 ASSERT((int64_t)arc_tempreserve >= 0);
6837 }
6838
6839 int
6840 arc_tempreserve_space(spa_t *spa, uint64_t reserve, uint64_t txg)
6841 {
6842 int error;
6843 uint64_t anon_size;
6844
6845 if (!arc_no_grow &&
6846 reserve > arc_c/4 &&
6847 reserve * 4 > (2ULL << SPA_MAXBLOCKSHIFT))
6848 arc_c = MIN(arc_c_max, reserve * 4);
6849
6850 /*
6851 * Throttle when the calculated memory footprint for the TXG
6852 * exceeds the target ARC size.
6853 */
6854 if (reserve > arc_c) {
6855 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve);
6856 return (SET_ERROR(ERESTART));
6857 }
6858
6859 /*
6860 * Don't count loaned bufs as in flight dirty data to prevent long
6861 * network delays from blocking transactions that are ready to be
6862 * assigned to a txg.
6863 */
6864
6865 /* assert that it has not wrapped around */
6866 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0);
6867
6868 anon_size = MAX((int64_t)
6869 (zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_DATA]) +
6870 zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_METADATA]) -
6871 arc_loaned_bytes), 0);
6872
6873 /*
6874 * Writes will, almost always, require additional memory allocations
6875 * in order to compress/encrypt/etc the data. We therefore need to
6876 * make sure that there is sufficient available memory for this.
6877 */
6878 error = arc_memory_throttle(spa, reserve, txg);
6879 if (error != 0)
6880 return (error);
6881
6882 /*
6883 * Throttle writes when the amount of dirty data in the cache
6884 * gets too large. We try to keep the cache less than half full
6885 * of dirty blocks so that our sync times don't grow too large.
6886 *
6887 * In the case of one pool being built on another pool, we want
6888 * to make sure we don't end up throttling the lower (backing)
6889 * pool when the upper pool is the majority contributor to dirty
6890 * data. To insure we make forward progress during throttling, we
6891 * also check the current pool's net dirty data and only throttle
6892 * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty
6893 * data in the cache.
6894 *
6895 * Note: if two requests come in concurrently, we might let them
6896 * both succeed, when one of them should fail. Not a huge deal.
6897 */
6898 uint64_t total_dirty = reserve + arc_tempreserve + anon_size;
6899 uint64_t spa_dirty_anon = spa_dirty_data(spa);
6900 uint64_t rarc_c = arc_warm ? arc_c : arc_c_max;
6901 if (total_dirty > rarc_c * zfs_arc_dirty_limit_percent / 100 &&
6902 anon_size > rarc_c * zfs_arc_anon_limit_percent / 100 &&
6903 spa_dirty_anon > anon_size * zfs_arc_pool_dirty_percent / 100) {
6904 #ifdef ZFS_DEBUG
6905 uint64_t meta_esize = zfs_refcount_count(
6906 &arc_anon->arcs_esize[ARC_BUFC_METADATA]);
6907 uint64_t data_esize =
6908 zfs_refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]);
6909 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
6910 "anon_data=%lluK tempreserve=%lluK rarc_c=%lluK\n",
6911 (u_longlong_t)arc_tempreserve >> 10,
6912 (u_longlong_t)meta_esize >> 10,
6913 (u_longlong_t)data_esize >> 10,
6914 (u_longlong_t)reserve >> 10,
6915 (u_longlong_t)rarc_c >> 10);
6916 #endif
6917 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle);
6918 return (SET_ERROR(ERESTART));
6919 }
6920 atomic_add_64(&arc_tempreserve, reserve);
6921 return (0);
6922 }
6923
6924 static void
6925 arc_kstat_update_state(arc_state_t *state, kstat_named_t *size,
6926 kstat_named_t *data, kstat_named_t *metadata,
6927 kstat_named_t *evict_data, kstat_named_t *evict_metadata)
6928 {
6929 data->value.ui64 =
6930 zfs_refcount_count(&state->arcs_size[ARC_BUFC_DATA]);
6931 metadata->value.ui64 =
6932 zfs_refcount_count(&state->arcs_size[ARC_BUFC_METADATA]);
6933 size->value.ui64 = data->value.ui64 + metadata->value.ui64;
6934 evict_data->value.ui64 =
6935 zfs_refcount_count(&state->arcs_esize[ARC_BUFC_DATA]);
6936 evict_metadata->value.ui64 =
6937 zfs_refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]);
6938 }
6939
6940 static int
6941 arc_kstat_update(kstat_t *ksp, int rw)
6942 {
6943 arc_stats_t *as = ksp->ks_data;
6944
6945 if (rw == KSTAT_WRITE)
6946 return (SET_ERROR(EACCES));
6947
6948 as->arcstat_hits.value.ui64 =
6949 wmsum_value(&arc_sums.arcstat_hits);
6950 as->arcstat_iohits.value.ui64 =
6951 wmsum_value(&arc_sums.arcstat_iohits);
6952 as->arcstat_misses.value.ui64 =
6953 wmsum_value(&arc_sums.arcstat_misses);
6954 as->arcstat_demand_data_hits.value.ui64 =
6955 wmsum_value(&arc_sums.arcstat_demand_data_hits);
6956 as->arcstat_demand_data_iohits.value.ui64 =
6957 wmsum_value(&arc_sums.arcstat_demand_data_iohits);
6958 as->arcstat_demand_data_misses.value.ui64 =
6959 wmsum_value(&arc_sums.arcstat_demand_data_misses);
6960 as->arcstat_demand_metadata_hits.value.ui64 =
6961 wmsum_value(&arc_sums.arcstat_demand_metadata_hits);
6962 as->arcstat_demand_metadata_iohits.value.ui64 =
6963 wmsum_value(&arc_sums.arcstat_demand_metadata_iohits);
6964 as->arcstat_demand_metadata_misses.value.ui64 =
6965 wmsum_value(&arc_sums.arcstat_demand_metadata_misses);
6966 as->arcstat_prefetch_data_hits.value.ui64 =
6967 wmsum_value(&arc_sums.arcstat_prefetch_data_hits);
6968 as->arcstat_prefetch_data_iohits.value.ui64 =
6969 wmsum_value(&arc_sums.arcstat_prefetch_data_iohits);
6970 as->arcstat_prefetch_data_misses.value.ui64 =
6971 wmsum_value(&arc_sums.arcstat_prefetch_data_misses);
6972 as->arcstat_prefetch_metadata_hits.value.ui64 =
6973 wmsum_value(&arc_sums.arcstat_prefetch_metadata_hits);
6974 as->arcstat_prefetch_metadata_iohits.value.ui64 =
6975 wmsum_value(&arc_sums.arcstat_prefetch_metadata_iohits);
6976 as->arcstat_prefetch_metadata_misses.value.ui64 =
6977 wmsum_value(&arc_sums.arcstat_prefetch_metadata_misses);
6978 as->arcstat_mru_hits.value.ui64 =
6979 wmsum_value(&arc_sums.arcstat_mru_hits);
6980 as->arcstat_mru_ghost_hits.value.ui64 =
6981 wmsum_value(&arc_sums.arcstat_mru_ghost_hits);
6982 as->arcstat_mfu_hits.value.ui64 =
6983 wmsum_value(&arc_sums.arcstat_mfu_hits);
6984 as->arcstat_mfu_ghost_hits.value.ui64 =
6985 wmsum_value(&arc_sums.arcstat_mfu_ghost_hits);
6986 as->arcstat_uncached_hits.value.ui64 =
6987 wmsum_value(&arc_sums.arcstat_uncached_hits);
6988 as->arcstat_deleted.value.ui64 =
6989 wmsum_value(&arc_sums.arcstat_deleted);
6990 as->arcstat_mutex_miss.value.ui64 =
6991 wmsum_value(&arc_sums.arcstat_mutex_miss);
6992 as->arcstat_access_skip.value.ui64 =
6993 wmsum_value(&arc_sums.arcstat_access_skip);
6994 as->arcstat_evict_skip.value.ui64 =
6995 wmsum_value(&arc_sums.arcstat_evict_skip);
6996 as->arcstat_evict_not_enough.value.ui64 =
6997 wmsum_value(&arc_sums.arcstat_evict_not_enough);
6998 as->arcstat_evict_l2_cached.value.ui64 =
6999 wmsum_value(&arc_sums.arcstat_evict_l2_cached);
7000 as->arcstat_evict_l2_eligible.value.ui64 =
7001 wmsum_value(&arc_sums.arcstat_evict_l2_eligible);
7002 as->arcstat_evict_l2_eligible_mfu.value.ui64 =
7003 wmsum_value(&arc_sums.arcstat_evict_l2_eligible_mfu);
7004 as->arcstat_evict_l2_eligible_mru.value.ui64 =
7005 wmsum_value(&arc_sums.arcstat_evict_l2_eligible_mru);
7006 as->arcstat_evict_l2_ineligible.value.ui64 =
7007 wmsum_value(&arc_sums.arcstat_evict_l2_ineligible);
7008 as->arcstat_evict_l2_skip.value.ui64 =
7009 wmsum_value(&arc_sums.arcstat_evict_l2_skip);
7010 as->arcstat_hash_collisions.value.ui64 =
7011 wmsum_value(&arc_sums.arcstat_hash_collisions);
7012 as->arcstat_hash_chains.value.ui64 =
7013 wmsum_value(&arc_sums.arcstat_hash_chains);
7014 as->arcstat_size.value.ui64 =
7015 aggsum_value(&arc_sums.arcstat_size);
7016 as->arcstat_compressed_size.value.ui64 =
7017 wmsum_value(&arc_sums.arcstat_compressed_size);
7018 as->arcstat_uncompressed_size.value.ui64 =
7019 wmsum_value(&arc_sums.arcstat_uncompressed_size);
7020 as->arcstat_overhead_size.value.ui64 =
7021 wmsum_value(&arc_sums.arcstat_overhead_size);
7022 as->arcstat_hdr_size.value.ui64 =
7023 wmsum_value(&arc_sums.arcstat_hdr_size);
7024 as->arcstat_data_size.value.ui64 =
7025 wmsum_value(&arc_sums.arcstat_data_size);
7026 as->arcstat_metadata_size.value.ui64 =
7027 wmsum_value(&arc_sums.arcstat_metadata_size);
7028 as->arcstat_dbuf_size.value.ui64 =
7029 wmsum_value(&arc_sums.arcstat_dbuf_size);
7030 #if defined(COMPAT_FREEBSD11)
7031 as->arcstat_other_size.value.ui64 =
7032 wmsum_value(&arc_sums.arcstat_bonus_size) +
7033 wmsum_value(&arc_sums.arcstat_dnode_size) +
7034 wmsum_value(&arc_sums.arcstat_dbuf_size);
7035 #endif
7036
7037 arc_kstat_update_state(arc_anon,
7038 &as->arcstat_anon_size,
7039 &as->arcstat_anon_data,
7040 &as->arcstat_anon_metadata,
7041 &as->arcstat_anon_evictable_data,
7042 &as->arcstat_anon_evictable_metadata);
7043 arc_kstat_update_state(arc_mru,
7044 &as->arcstat_mru_size,
7045 &as->arcstat_mru_data,
7046 &as->arcstat_mru_metadata,
7047 &as->arcstat_mru_evictable_data,
7048 &as->arcstat_mru_evictable_metadata);
7049 arc_kstat_update_state(arc_mru_ghost,
7050 &as->arcstat_mru_ghost_size,
7051 &as->arcstat_mru_ghost_data,
7052 &as->arcstat_mru_ghost_metadata,
7053 &as->arcstat_mru_ghost_evictable_data,
7054 &as->arcstat_mru_ghost_evictable_metadata);
7055 arc_kstat_update_state(arc_mfu,
7056 &as->arcstat_mfu_size,
7057 &as->arcstat_mfu_data,
7058 &as->arcstat_mfu_metadata,
7059 &as->arcstat_mfu_evictable_data,
7060 &as->arcstat_mfu_evictable_metadata);
7061 arc_kstat_update_state(arc_mfu_ghost,
7062 &as->arcstat_mfu_ghost_size,
7063 &as->arcstat_mfu_ghost_data,
7064 &as->arcstat_mfu_ghost_metadata,
7065 &as->arcstat_mfu_ghost_evictable_data,
7066 &as->arcstat_mfu_ghost_evictable_metadata);
7067 arc_kstat_update_state(arc_uncached,
7068 &as->arcstat_uncached_size,
7069 &as->arcstat_uncached_data,
7070 &as->arcstat_uncached_metadata,
7071 &as->arcstat_uncached_evictable_data,
7072 &as->arcstat_uncached_evictable_metadata);
7073
7074 as->arcstat_dnode_size.value.ui64 =
7075 wmsum_value(&arc_sums.arcstat_dnode_size);
7076 as->arcstat_bonus_size.value.ui64 =
7077 wmsum_value(&arc_sums.arcstat_bonus_size);
7078 as->arcstat_l2_hits.value.ui64 =
7079 wmsum_value(&arc_sums.arcstat_l2_hits);
7080 as->arcstat_l2_misses.value.ui64 =
7081 wmsum_value(&arc_sums.arcstat_l2_misses);
7082 as->arcstat_l2_prefetch_asize.value.ui64 =
7083 wmsum_value(&arc_sums.arcstat_l2_prefetch_asize);
7084 as->arcstat_l2_mru_asize.value.ui64 =
7085 wmsum_value(&arc_sums.arcstat_l2_mru_asize);
7086 as->arcstat_l2_mfu_asize.value.ui64 =
7087 wmsum_value(&arc_sums.arcstat_l2_mfu_asize);
7088 as->arcstat_l2_bufc_data_asize.value.ui64 =
7089 wmsum_value(&arc_sums.arcstat_l2_bufc_data_asize);
7090 as->arcstat_l2_bufc_metadata_asize.value.ui64 =
7091 wmsum_value(&arc_sums.arcstat_l2_bufc_metadata_asize);
7092 as->arcstat_l2_feeds.value.ui64 =
7093 wmsum_value(&arc_sums.arcstat_l2_feeds);
7094 as->arcstat_l2_rw_clash.value.ui64 =
7095 wmsum_value(&arc_sums.arcstat_l2_rw_clash);
7096 as->arcstat_l2_read_bytes.value.ui64 =
7097 wmsum_value(&arc_sums.arcstat_l2_read_bytes);
7098 as->arcstat_l2_write_bytes.value.ui64 =
7099 wmsum_value(&arc_sums.arcstat_l2_write_bytes);
7100 as->arcstat_l2_writes_sent.value.ui64 =
7101 wmsum_value(&arc_sums.arcstat_l2_writes_sent);
7102 as->arcstat_l2_writes_done.value.ui64 =
7103 wmsum_value(&arc_sums.arcstat_l2_writes_done);
7104 as->arcstat_l2_writes_error.value.ui64 =
7105 wmsum_value(&arc_sums.arcstat_l2_writes_error);
7106 as->arcstat_l2_writes_lock_retry.value.ui64 =
7107 wmsum_value(&arc_sums.arcstat_l2_writes_lock_retry);
7108 as->arcstat_l2_evict_lock_retry.value.ui64 =
7109 wmsum_value(&arc_sums.arcstat_l2_evict_lock_retry);
7110 as->arcstat_l2_evict_reading.value.ui64 =
7111 wmsum_value(&arc_sums.arcstat_l2_evict_reading);
7112 as->arcstat_l2_evict_l1cached.value.ui64 =
7113 wmsum_value(&arc_sums.arcstat_l2_evict_l1cached);
7114 as->arcstat_l2_free_on_write.value.ui64 =
7115 wmsum_value(&arc_sums.arcstat_l2_free_on_write);
7116 as->arcstat_l2_abort_lowmem.value.ui64 =
7117 wmsum_value(&arc_sums.arcstat_l2_abort_lowmem);
7118 as->arcstat_l2_cksum_bad.value.ui64 =
7119 wmsum_value(&arc_sums.arcstat_l2_cksum_bad);
7120 as->arcstat_l2_io_error.value.ui64 =
7121 wmsum_value(&arc_sums.arcstat_l2_io_error);
7122 as->arcstat_l2_lsize.value.ui64 =
7123 wmsum_value(&arc_sums.arcstat_l2_lsize);
7124 as->arcstat_l2_psize.value.ui64 =
7125 wmsum_value(&arc_sums.arcstat_l2_psize);
7126 as->arcstat_l2_hdr_size.value.ui64 =
7127 aggsum_value(&arc_sums.arcstat_l2_hdr_size);
7128 as->arcstat_l2_log_blk_writes.value.ui64 =
7129 wmsum_value(&arc_sums.arcstat_l2_log_blk_writes);
7130 as->arcstat_l2_log_blk_asize.value.ui64 =
7131 wmsum_value(&arc_sums.arcstat_l2_log_blk_asize);
7132 as->arcstat_l2_log_blk_count.value.ui64 =
7133 wmsum_value(&arc_sums.arcstat_l2_log_blk_count);
7134 as->arcstat_l2_rebuild_success.value.ui64 =
7135 wmsum_value(&arc_sums.arcstat_l2_rebuild_success);
7136 as->arcstat_l2_rebuild_abort_unsupported.value.ui64 =
7137 wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_unsupported);
7138 as->arcstat_l2_rebuild_abort_io_errors.value.ui64 =
7139 wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_io_errors);
7140 as->arcstat_l2_rebuild_abort_dh_errors.value.ui64 =
7141 wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_dh_errors);
7142 as->arcstat_l2_rebuild_abort_cksum_lb_errors.value.ui64 =
7143 wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors);
7144 as->arcstat_l2_rebuild_abort_lowmem.value.ui64 =
7145 wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_lowmem);
7146 as->arcstat_l2_rebuild_size.value.ui64 =
7147 wmsum_value(&arc_sums.arcstat_l2_rebuild_size);
7148 as->arcstat_l2_rebuild_asize.value.ui64 =
7149 wmsum_value(&arc_sums.arcstat_l2_rebuild_asize);
7150 as->arcstat_l2_rebuild_bufs.value.ui64 =
7151 wmsum_value(&arc_sums.arcstat_l2_rebuild_bufs);
7152 as->arcstat_l2_rebuild_bufs_precached.value.ui64 =
7153 wmsum_value(&arc_sums.arcstat_l2_rebuild_bufs_precached);
7154 as->arcstat_l2_rebuild_log_blks.value.ui64 =
7155 wmsum_value(&arc_sums.arcstat_l2_rebuild_log_blks);
7156 as->arcstat_memory_throttle_count.value.ui64 =
7157 wmsum_value(&arc_sums.arcstat_memory_throttle_count);
7158 as->arcstat_memory_direct_count.value.ui64 =
7159 wmsum_value(&arc_sums.arcstat_memory_direct_count);
7160 as->arcstat_memory_indirect_count.value.ui64 =
7161 wmsum_value(&arc_sums.arcstat_memory_indirect_count);
7162
7163 as->arcstat_memory_all_bytes.value.ui64 =
7164 arc_all_memory();
7165 as->arcstat_memory_free_bytes.value.ui64 =
7166 arc_free_memory();
7167 as->arcstat_memory_available_bytes.value.i64 =
7168 arc_available_memory();
7169
7170 as->arcstat_prune.value.ui64 =
7171 wmsum_value(&arc_sums.arcstat_prune);
7172 as->arcstat_meta_used.value.ui64 =
7173 wmsum_value(&arc_sums.arcstat_meta_used);
7174 as->arcstat_async_upgrade_sync.value.ui64 =
7175 wmsum_value(&arc_sums.arcstat_async_upgrade_sync);
7176 as->arcstat_predictive_prefetch.value.ui64 =
7177 wmsum_value(&arc_sums.arcstat_predictive_prefetch);
7178 as->arcstat_demand_hit_predictive_prefetch.value.ui64 =
7179 wmsum_value(&arc_sums.arcstat_demand_hit_predictive_prefetch);
7180 as->arcstat_demand_iohit_predictive_prefetch.value.ui64 =
7181 wmsum_value(&arc_sums.arcstat_demand_iohit_predictive_prefetch);
7182 as->arcstat_prescient_prefetch.value.ui64 =
7183 wmsum_value(&arc_sums.arcstat_prescient_prefetch);
7184 as->arcstat_demand_hit_prescient_prefetch.value.ui64 =
7185 wmsum_value(&arc_sums.arcstat_demand_hit_prescient_prefetch);
7186 as->arcstat_demand_iohit_prescient_prefetch.value.ui64 =
7187 wmsum_value(&arc_sums.arcstat_demand_iohit_prescient_prefetch);
7188 as->arcstat_raw_size.value.ui64 =
7189 wmsum_value(&arc_sums.arcstat_raw_size);
7190 as->arcstat_cached_only_in_progress.value.ui64 =
7191 wmsum_value(&arc_sums.arcstat_cached_only_in_progress);
7192 as->arcstat_abd_chunk_waste_size.value.ui64 =
7193 wmsum_value(&arc_sums.arcstat_abd_chunk_waste_size);
7194
7195 return (0);
7196 }
7197
7198 /*
7199 * This function *must* return indices evenly distributed between all
7200 * sublists of the multilist. This is needed due to how the ARC eviction
7201 * code is laid out; arc_evict_state() assumes ARC buffers are evenly
7202 * distributed between all sublists and uses this assumption when
7203 * deciding which sublist to evict from and how much to evict from it.
7204 */
7205 static unsigned int
7206 arc_state_multilist_index_func(multilist_t *ml, void *obj)
7207 {
7208 arc_buf_hdr_t *hdr = obj;
7209
7210 /*
7211 * We rely on b_dva to generate evenly distributed index
7212 * numbers using buf_hash below. So, as an added precaution,
7213 * let's make sure we never add empty buffers to the arc lists.
7214 */
7215 ASSERT(!HDR_EMPTY(hdr));
7216
7217 /*
7218 * The assumption here, is the hash value for a given
7219 * arc_buf_hdr_t will remain constant throughout its lifetime
7220 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
7221 * Thus, we don't need to store the header's sublist index
7222 * on insertion, as this index can be recalculated on removal.
7223 *
7224 * Also, the low order bits of the hash value are thought to be
7225 * distributed evenly. Otherwise, in the case that the multilist
7226 * has a power of two number of sublists, each sublists' usage
7227 * would not be evenly distributed. In this context full 64bit
7228 * division would be a waste of time, so limit it to 32 bits.
7229 */
7230 return ((unsigned int)buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) %
7231 multilist_get_num_sublists(ml));
7232 }
7233
7234 static unsigned int
7235 arc_state_l2c_multilist_index_func(multilist_t *ml, void *obj)
7236 {
7237 panic("Header %p insert into arc_l2c_only %p", obj, ml);
7238 }
7239
7240 #define WARN_IF_TUNING_IGNORED(tuning, value, do_warn) do { \
7241 if ((do_warn) && (tuning) && ((tuning) != (value))) { \
7242 cmn_err(CE_WARN, \
7243 "ignoring tunable %s (using %llu instead)", \
7244 (#tuning), (u_longlong_t)(value)); \
7245 } \
7246 } while (0)
7247
7248 /*
7249 * Called during module initialization and periodically thereafter to
7250 * apply reasonable changes to the exposed performance tunings. Can also be
7251 * called explicitly by param_set_arc_*() functions when ARC tunables are
7252 * updated manually. Non-zero zfs_* values which differ from the currently set
7253 * values will be applied.
7254 */
7255 void
7256 arc_tuning_update(boolean_t verbose)
7257 {
7258 uint64_t allmem = arc_all_memory();
7259
7260 /* Valid range: 32M - <arc_c_max> */
7261 if ((zfs_arc_min) && (zfs_arc_min != arc_c_min) &&
7262 (zfs_arc_min >= 2ULL << SPA_MAXBLOCKSHIFT) &&
7263 (zfs_arc_min <= arc_c_max)) {
7264 arc_c_min = zfs_arc_min;
7265 arc_c = MAX(arc_c, arc_c_min);
7266 }
7267 WARN_IF_TUNING_IGNORED(zfs_arc_min, arc_c_min, verbose);
7268
7269 /* Valid range: 64M - <all physical memory> */
7270 if ((zfs_arc_max) && (zfs_arc_max != arc_c_max) &&
7271 (zfs_arc_max >= MIN_ARC_MAX) && (zfs_arc_max < allmem) &&
7272 (zfs_arc_max > arc_c_min)) {
7273 arc_c_max = zfs_arc_max;
7274 arc_c = MIN(arc_c, arc_c_max);
7275 if (arc_dnode_limit > arc_c_max)
7276 arc_dnode_limit = arc_c_max;
7277 }
7278 WARN_IF_TUNING_IGNORED(zfs_arc_max, arc_c_max, verbose);
7279
7280 /* Valid range: 0 - <all physical memory> */
7281 arc_dnode_limit = zfs_arc_dnode_limit ? zfs_arc_dnode_limit :
7282 MIN(zfs_arc_dnode_limit_percent, 100) * arc_c_max / 100;
7283 WARN_IF_TUNING_IGNORED(zfs_arc_dnode_limit, arc_dnode_limit, verbose);
7284
7285 /* Valid range: 1 - N */
7286 if (zfs_arc_grow_retry)
7287 arc_grow_retry = zfs_arc_grow_retry;
7288
7289 /* Valid range: 1 - N */
7290 if (zfs_arc_shrink_shift) {
7291 arc_shrink_shift = zfs_arc_shrink_shift;
7292 arc_no_grow_shift = MIN(arc_no_grow_shift, arc_shrink_shift -1);
7293 }
7294
7295 /* Valid range: 1 - N ms */
7296 if (zfs_arc_min_prefetch_ms)
7297 arc_min_prefetch_ms = zfs_arc_min_prefetch_ms;
7298
7299 /* Valid range: 1 - N ms */
7300 if (zfs_arc_min_prescient_prefetch_ms) {
7301 arc_min_prescient_prefetch_ms =
7302 zfs_arc_min_prescient_prefetch_ms;
7303 }
7304
7305 /* Valid range: 0 - 100 */
7306 if (zfs_arc_lotsfree_percent <= 100)
7307 arc_lotsfree_percent = zfs_arc_lotsfree_percent;
7308 WARN_IF_TUNING_IGNORED(zfs_arc_lotsfree_percent, arc_lotsfree_percent,
7309 verbose);
7310
7311 /* Valid range: 0 - <all physical memory> */
7312 if ((zfs_arc_sys_free) && (zfs_arc_sys_free != arc_sys_free))
7313 arc_sys_free = MIN(zfs_arc_sys_free, allmem);
7314 WARN_IF_TUNING_IGNORED(zfs_arc_sys_free, arc_sys_free, verbose);
7315 }
7316
7317 static void
7318 arc_state_multilist_init(multilist_t *ml,
7319 multilist_sublist_index_func_t *index_func, int *maxcountp)
7320 {
7321 multilist_create(ml, sizeof (arc_buf_hdr_t),
7322 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), index_func);
7323 *maxcountp = MAX(*maxcountp, multilist_get_num_sublists(ml));
7324 }
7325
7326 static void
7327 arc_state_init(void)
7328 {
7329 int num_sublists = 0;
7330
7331 arc_state_multilist_init(&arc_mru->arcs_list[ARC_BUFC_METADATA],
7332 arc_state_multilist_index_func, &num_sublists);
7333 arc_state_multilist_init(&arc_mru->arcs_list[ARC_BUFC_DATA],
7334 arc_state_multilist_index_func, &num_sublists);
7335 arc_state_multilist_init(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
7336 arc_state_multilist_index_func, &num_sublists);
7337 arc_state_multilist_init(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
7338 arc_state_multilist_index_func, &num_sublists);
7339 arc_state_multilist_init(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
7340 arc_state_multilist_index_func, &num_sublists);
7341 arc_state_multilist_init(&arc_mfu->arcs_list[ARC_BUFC_DATA],
7342 arc_state_multilist_index_func, &num_sublists);
7343 arc_state_multilist_init(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
7344 arc_state_multilist_index_func, &num_sublists);
7345 arc_state_multilist_init(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
7346 arc_state_multilist_index_func, &num_sublists);
7347 arc_state_multilist_init(&arc_uncached->arcs_list[ARC_BUFC_METADATA],
7348 arc_state_multilist_index_func, &num_sublists);
7349 arc_state_multilist_init(&arc_uncached->arcs_list[ARC_BUFC_DATA],
7350 arc_state_multilist_index_func, &num_sublists);
7351
7352 /*
7353 * L2 headers should never be on the L2 state list since they don't
7354 * have L1 headers allocated. Special index function asserts that.
7355 */
7356 arc_state_multilist_init(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
7357 arc_state_l2c_multilist_index_func, &num_sublists);
7358 arc_state_multilist_init(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
7359 arc_state_l2c_multilist_index_func, &num_sublists);
7360
7361 /*
7362 * Keep track of the number of markers needed to reclaim buffers from
7363 * any ARC state. The markers will be pre-allocated so as to minimize
7364 * the number of memory allocations performed by the eviction thread.
7365 */
7366 arc_state_evict_marker_count = num_sublists;
7367
7368 zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]);
7369 zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]);
7370 zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]);
7371 zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]);
7372 zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]);
7373 zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]);
7374 zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]);
7375 zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]);
7376 zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]);
7377 zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]);
7378 zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]);
7379 zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]);
7380 zfs_refcount_create(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]);
7381 zfs_refcount_create(&arc_uncached->arcs_esize[ARC_BUFC_DATA]);
7382
7383 zfs_refcount_create(&arc_anon->arcs_size[ARC_BUFC_DATA]);
7384 zfs_refcount_create(&arc_anon->arcs_size[ARC_BUFC_METADATA]);
7385 zfs_refcount_create(&arc_mru->arcs_size[ARC_BUFC_DATA]);
7386 zfs_refcount_create(&arc_mru->arcs_size[ARC_BUFC_METADATA]);
7387 zfs_refcount_create(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]);
7388 zfs_refcount_create(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]);
7389 zfs_refcount_create(&arc_mfu->arcs_size[ARC_BUFC_DATA]);
7390 zfs_refcount_create(&arc_mfu->arcs_size[ARC_BUFC_METADATA]);
7391 zfs_refcount_create(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]);
7392 zfs_refcount_create(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]);
7393 zfs_refcount_create(&arc_l2c_only->arcs_size[ARC_BUFC_DATA]);
7394 zfs_refcount_create(&arc_l2c_only->arcs_size[ARC_BUFC_METADATA]);
7395 zfs_refcount_create(&arc_uncached->arcs_size[ARC_BUFC_DATA]);
7396 zfs_refcount_create(&arc_uncached->arcs_size[ARC_BUFC_METADATA]);
7397
7398 wmsum_init(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA], 0);
7399 wmsum_init(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA], 0);
7400 wmsum_init(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA], 0);
7401 wmsum_init(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA], 0);
7402
7403 wmsum_init(&arc_sums.arcstat_hits, 0);
7404 wmsum_init(&arc_sums.arcstat_iohits, 0);
7405 wmsum_init(&arc_sums.arcstat_misses, 0);
7406 wmsum_init(&arc_sums.arcstat_demand_data_hits, 0);
7407 wmsum_init(&arc_sums.arcstat_demand_data_iohits, 0);
7408 wmsum_init(&arc_sums.arcstat_demand_data_misses, 0);
7409 wmsum_init(&arc_sums.arcstat_demand_metadata_hits, 0);
7410 wmsum_init(&arc_sums.arcstat_demand_metadata_iohits, 0);
7411 wmsum_init(&arc_sums.arcstat_demand_metadata_misses, 0);
7412 wmsum_init(&arc_sums.arcstat_prefetch_data_hits, 0);
7413 wmsum_init(&arc_sums.arcstat_prefetch_data_iohits, 0);
7414 wmsum_init(&arc_sums.arcstat_prefetch_data_misses, 0);
7415 wmsum_init(&arc_sums.arcstat_prefetch_metadata_hits, 0);
7416 wmsum_init(&arc_sums.arcstat_prefetch_metadata_iohits, 0);
7417 wmsum_init(&arc_sums.arcstat_prefetch_metadata_misses, 0);
7418 wmsum_init(&arc_sums.arcstat_mru_hits, 0);
7419 wmsum_init(&arc_sums.arcstat_mru_ghost_hits, 0);
7420 wmsum_init(&arc_sums.arcstat_mfu_hits, 0);
7421 wmsum_init(&arc_sums.arcstat_mfu_ghost_hits, 0);
7422 wmsum_init(&arc_sums.arcstat_uncached_hits, 0);
7423 wmsum_init(&arc_sums.arcstat_deleted, 0);
7424 wmsum_init(&arc_sums.arcstat_mutex_miss, 0);
7425 wmsum_init(&arc_sums.arcstat_access_skip, 0);
7426 wmsum_init(&arc_sums.arcstat_evict_skip, 0);
7427 wmsum_init(&arc_sums.arcstat_evict_not_enough, 0);
7428 wmsum_init(&arc_sums.arcstat_evict_l2_cached, 0);
7429 wmsum_init(&arc_sums.arcstat_evict_l2_eligible, 0);
7430 wmsum_init(&arc_sums.arcstat_evict_l2_eligible_mfu, 0);
7431 wmsum_init(&arc_sums.arcstat_evict_l2_eligible_mru, 0);
7432 wmsum_init(&arc_sums.arcstat_evict_l2_ineligible, 0);
7433 wmsum_init(&arc_sums.arcstat_evict_l2_skip, 0);
7434 wmsum_init(&arc_sums.arcstat_hash_collisions, 0);
7435 wmsum_init(&arc_sums.arcstat_hash_chains, 0);
7436 aggsum_init(&arc_sums.arcstat_size, 0);
7437 wmsum_init(&arc_sums.arcstat_compressed_size, 0);
7438 wmsum_init(&arc_sums.arcstat_uncompressed_size, 0);
7439 wmsum_init(&arc_sums.arcstat_overhead_size, 0);
7440 wmsum_init(&arc_sums.arcstat_hdr_size, 0);
7441 wmsum_init(&arc_sums.arcstat_data_size, 0);
7442 wmsum_init(&arc_sums.arcstat_metadata_size, 0);
7443 wmsum_init(&arc_sums.arcstat_dbuf_size, 0);
7444 wmsum_init(&arc_sums.arcstat_dnode_size, 0);
7445 wmsum_init(&arc_sums.arcstat_bonus_size, 0);
7446 wmsum_init(&arc_sums.arcstat_l2_hits, 0);
7447 wmsum_init(&arc_sums.arcstat_l2_misses, 0);
7448 wmsum_init(&arc_sums.arcstat_l2_prefetch_asize, 0);
7449 wmsum_init(&arc_sums.arcstat_l2_mru_asize, 0);
7450 wmsum_init(&arc_sums.arcstat_l2_mfu_asize, 0);
7451 wmsum_init(&arc_sums.arcstat_l2_bufc_data_asize, 0);
7452 wmsum_init(&arc_sums.arcstat_l2_bufc_metadata_asize, 0);
7453 wmsum_init(&arc_sums.arcstat_l2_feeds, 0);
7454 wmsum_init(&arc_sums.arcstat_l2_rw_clash, 0);
7455 wmsum_init(&arc_sums.arcstat_l2_read_bytes, 0);
7456 wmsum_init(&arc_sums.arcstat_l2_write_bytes, 0);
7457 wmsum_init(&arc_sums.arcstat_l2_writes_sent, 0);
7458 wmsum_init(&arc_sums.arcstat_l2_writes_done, 0);
7459 wmsum_init(&arc_sums.arcstat_l2_writes_error, 0);
7460 wmsum_init(&arc_sums.arcstat_l2_writes_lock_retry, 0);
7461 wmsum_init(&arc_sums.arcstat_l2_evict_lock_retry, 0);
7462 wmsum_init(&arc_sums.arcstat_l2_evict_reading, 0);
7463 wmsum_init(&arc_sums.arcstat_l2_evict_l1cached, 0);
7464 wmsum_init(&arc_sums.arcstat_l2_free_on_write, 0);
7465 wmsum_init(&arc_sums.arcstat_l2_abort_lowmem, 0);
7466 wmsum_init(&arc_sums.arcstat_l2_cksum_bad, 0);
7467 wmsum_init(&arc_sums.arcstat_l2_io_error, 0);
7468 wmsum_init(&arc_sums.arcstat_l2_lsize, 0);
7469 wmsum_init(&arc_sums.arcstat_l2_psize, 0);
7470 aggsum_init(&arc_sums.arcstat_l2_hdr_size, 0);
7471 wmsum_init(&arc_sums.arcstat_l2_log_blk_writes, 0);
7472 wmsum_init(&arc_sums.arcstat_l2_log_blk_asize, 0);
7473 wmsum_init(&arc_sums.arcstat_l2_log_blk_count, 0);
7474 wmsum_init(&arc_sums.arcstat_l2_rebuild_success, 0);
7475 wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_unsupported, 0);
7476 wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_io_errors, 0);
7477 wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_dh_errors, 0);
7478 wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors, 0);
7479 wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_lowmem, 0);
7480 wmsum_init(&arc_sums.arcstat_l2_rebuild_size, 0);
7481 wmsum_init(&arc_sums.arcstat_l2_rebuild_asize, 0);
7482 wmsum_init(&arc_sums.arcstat_l2_rebuild_bufs, 0);
7483 wmsum_init(&arc_sums.arcstat_l2_rebuild_bufs_precached, 0);
7484 wmsum_init(&arc_sums.arcstat_l2_rebuild_log_blks, 0);
7485 wmsum_init(&arc_sums.arcstat_memory_throttle_count, 0);
7486 wmsum_init(&arc_sums.arcstat_memory_direct_count, 0);
7487 wmsum_init(&arc_sums.arcstat_memory_indirect_count, 0);
7488 wmsum_init(&arc_sums.arcstat_prune, 0);
7489 wmsum_init(&arc_sums.arcstat_meta_used, 0);
7490 wmsum_init(&arc_sums.arcstat_async_upgrade_sync, 0);
7491 wmsum_init(&arc_sums.arcstat_predictive_prefetch, 0);
7492 wmsum_init(&arc_sums.arcstat_demand_hit_predictive_prefetch, 0);
7493 wmsum_init(&arc_sums.arcstat_demand_iohit_predictive_prefetch, 0);
7494 wmsum_init(&arc_sums.arcstat_prescient_prefetch, 0);
7495 wmsum_init(&arc_sums.arcstat_demand_hit_prescient_prefetch, 0);
7496 wmsum_init(&arc_sums.arcstat_demand_iohit_prescient_prefetch, 0);
7497 wmsum_init(&arc_sums.arcstat_raw_size, 0);
7498 wmsum_init(&arc_sums.arcstat_cached_only_in_progress, 0);
7499 wmsum_init(&arc_sums.arcstat_abd_chunk_waste_size, 0);
7500
7501 arc_anon->arcs_state = ARC_STATE_ANON;
7502 arc_mru->arcs_state = ARC_STATE_MRU;
7503 arc_mru_ghost->arcs_state = ARC_STATE_MRU_GHOST;
7504 arc_mfu->arcs_state = ARC_STATE_MFU;
7505 arc_mfu_ghost->arcs_state = ARC_STATE_MFU_GHOST;
7506 arc_l2c_only->arcs_state = ARC_STATE_L2C_ONLY;
7507 arc_uncached->arcs_state = ARC_STATE_UNCACHED;
7508 }
7509
7510 static void
7511 arc_state_fini(void)
7512 {
7513 zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]);
7514 zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]);
7515 zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]);
7516 zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]);
7517 zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]);
7518 zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]);
7519 zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]);
7520 zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]);
7521 zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]);
7522 zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]);
7523 zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]);
7524 zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]);
7525 zfs_refcount_destroy(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]);
7526 zfs_refcount_destroy(&arc_uncached->arcs_esize[ARC_BUFC_DATA]);
7527
7528 zfs_refcount_destroy(&arc_anon->arcs_size[ARC_BUFC_DATA]);
7529 zfs_refcount_destroy(&arc_anon->arcs_size[ARC_BUFC_METADATA]);
7530 zfs_refcount_destroy(&arc_mru->arcs_size[ARC_BUFC_DATA]);
7531 zfs_refcount_destroy(&arc_mru->arcs_size[ARC_BUFC_METADATA]);
7532 zfs_refcount_destroy(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]);
7533 zfs_refcount_destroy(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]);
7534 zfs_refcount_destroy(&arc_mfu->arcs_size[ARC_BUFC_DATA]);
7535 zfs_refcount_destroy(&arc_mfu->arcs_size[ARC_BUFC_METADATA]);
7536 zfs_refcount_destroy(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]);
7537 zfs_refcount_destroy(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]);
7538 zfs_refcount_destroy(&arc_l2c_only->arcs_size[ARC_BUFC_DATA]);
7539 zfs_refcount_destroy(&arc_l2c_only->arcs_size[ARC_BUFC_METADATA]);
7540 zfs_refcount_destroy(&arc_uncached->arcs_size[ARC_BUFC_DATA]);
7541 zfs_refcount_destroy(&arc_uncached->arcs_size[ARC_BUFC_METADATA]);
7542
7543 multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
7544 multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
7545 multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
7546 multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
7547 multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
7548 multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
7549 multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
7550 multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
7551 multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA]);
7552 multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]);
7553 multilist_destroy(&arc_uncached->arcs_list[ARC_BUFC_METADATA]);
7554 multilist_destroy(&arc_uncached->arcs_list[ARC_BUFC_DATA]);
7555
7556 wmsum_fini(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA]);
7557 wmsum_fini(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA]);
7558 wmsum_fini(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA]);
7559 wmsum_fini(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA]);
7560
7561 wmsum_fini(&arc_sums.arcstat_hits);
7562 wmsum_fini(&arc_sums.arcstat_iohits);
7563 wmsum_fini(&arc_sums.arcstat_misses);
7564 wmsum_fini(&arc_sums.arcstat_demand_data_hits);
7565 wmsum_fini(&arc_sums.arcstat_demand_data_iohits);
7566 wmsum_fini(&arc_sums.arcstat_demand_data_misses);
7567 wmsum_fini(&arc_sums.arcstat_demand_metadata_hits);
7568 wmsum_fini(&arc_sums.arcstat_demand_metadata_iohits);
7569 wmsum_fini(&arc_sums.arcstat_demand_metadata_misses);
7570 wmsum_fini(&arc_sums.arcstat_prefetch_data_hits);
7571 wmsum_fini(&arc_sums.arcstat_prefetch_data_iohits);
7572 wmsum_fini(&arc_sums.arcstat_prefetch_data_misses);
7573 wmsum_fini(&arc_sums.arcstat_prefetch_metadata_hits);
7574 wmsum_fini(&arc_sums.arcstat_prefetch_metadata_iohits);
7575 wmsum_fini(&arc_sums.arcstat_prefetch_metadata_misses);
7576 wmsum_fini(&arc_sums.arcstat_mru_hits);
7577 wmsum_fini(&arc_sums.arcstat_mru_ghost_hits);
7578 wmsum_fini(&arc_sums.arcstat_mfu_hits);
7579 wmsum_fini(&arc_sums.arcstat_mfu_ghost_hits);
7580 wmsum_fini(&arc_sums.arcstat_uncached_hits);
7581 wmsum_fini(&arc_sums.arcstat_deleted);
7582 wmsum_fini(&arc_sums.arcstat_mutex_miss);
7583 wmsum_fini(&arc_sums.arcstat_access_skip);
7584 wmsum_fini(&arc_sums.arcstat_evict_skip);
7585 wmsum_fini(&arc_sums.arcstat_evict_not_enough);
7586 wmsum_fini(&arc_sums.arcstat_evict_l2_cached);
7587 wmsum_fini(&arc_sums.arcstat_evict_l2_eligible);
7588 wmsum_fini(&arc_sums.arcstat_evict_l2_eligible_mfu);
7589 wmsum_fini(&arc_sums.arcstat_evict_l2_eligible_mru);
7590 wmsum_fini(&arc_sums.arcstat_evict_l2_ineligible);
7591 wmsum_fini(&arc_sums.arcstat_evict_l2_skip);
7592 wmsum_fini(&arc_sums.arcstat_hash_collisions);
7593 wmsum_fini(&arc_sums.arcstat_hash_chains);
7594 aggsum_fini(&arc_sums.arcstat_size);
7595 wmsum_fini(&arc_sums.arcstat_compressed_size);
7596 wmsum_fini(&arc_sums.arcstat_uncompressed_size);
7597 wmsum_fini(&arc_sums.arcstat_overhead_size);
7598 wmsum_fini(&arc_sums.arcstat_hdr_size);
7599 wmsum_fini(&arc_sums.arcstat_data_size);
7600 wmsum_fini(&arc_sums.arcstat_metadata_size);
7601 wmsum_fini(&arc_sums.arcstat_dbuf_size);
7602 wmsum_fini(&arc_sums.arcstat_dnode_size);
7603 wmsum_fini(&arc_sums.arcstat_bonus_size);
7604 wmsum_fini(&arc_sums.arcstat_l2_hits);
7605 wmsum_fini(&arc_sums.arcstat_l2_misses);
7606 wmsum_fini(&arc_sums.arcstat_l2_prefetch_asize);
7607 wmsum_fini(&arc_sums.arcstat_l2_mru_asize);
7608 wmsum_fini(&arc_sums.arcstat_l2_mfu_asize);
7609 wmsum_fini(&arc_sums.arcstat_l2_bufc_data_asize);
7610 wmsum_fini(&arc_sums.arcstat_l2_bufc_metadata_asize);
7611 wmsum_fini(&arc_sums.arcstat_l2_feeds);
7612 wmsum_fini(&arc_sums.arcstat_l2_rw_clash);
7613 wmsum_fini(&arc_sums.arcstat_l2_read_bytes);
7614 wmsum_fini(&arc_sums.arcstat_l2_write_bytes);
7615 wmsum_fini(&arc_sums.arcstat_l2_writes_sent);
7616 wmsum_fini(&arc_sums.arcstat_l2_writes_done);
7617 wmsum_fini(&arc_sums.arcstat_l2_writes_error);
7618 wmsum_fini(&arc_sums.arcstat_l2_writes_lock_retry);
7619 wmsum_fini(&arc_sums.arcstat_l2_evict_lock_retry);
7620 wmsum_fini(&arc_sums.arcstat_l2_evict_reading);
7621 wmsum_fini(&arc_sums.arcstat_l2_evict_l1cached);
7622 wmsum_fini(&arc_sums.arcstat_l2_free_on_write);
7623 wmsum_fini(&arc_sums.arcstat_l2_abort_lowmem);
7624 wmsum_fini(&arc_sums.arcstat_l2_cksum_bad);
7625 wmsum_fini(&arc_sums.arcstat_l2_io_error);
7626 wmsum_fini(&arc_sums.arcstat_l2_lsize);
7627 wmsum_fini(&arc_sums.arcstat_l2_psize);
7628 aggsum_fini(&arc_sums.arcstat_l2_hdr_size);
7629 wmsum_fini(&arc_sums.arcstat_l2_log_blk_writes);
7630 wmsum_fini(&arc_sums.arcstat_l2_log_blk_asize);
7631 wmsum_fini(&arc_sums.arcstat_l2_log_blk_count);
7632 wmsum_fini(&arc_sums.arcstat_l2_rebuild_success);
7633 wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_unsupported);
7634 wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_io_errors);
7635 wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_dh_errors);
7636 wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors);
7637 wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_lowmem);
7638 wmsum_fini(&arc_sums.arcstat_l2_rebuild_size);
7639 wmsum_fini(&arc_sums.arcstat_l2_rebuild_asize);
7640 wmsum_fini(&arc_sums.arcstat_l2_rebuild_bufs);
7641 wmsum_fini(&arc_sums.arcstat_l2_rebuild_bufs_precached);
7642 wmsum_fini(&arc_sums.arcstat_l2_rebuild_log_blks);
7643 wmsum_fini(&arc_sums.arcstat_memory_throttle_count);
7644 wmsum_fini(&arc_sums.arcstat_memory_direct_count);
7645 wmsum_fini(&arc_sums.arcstat_memory_indirect_count);
7646 wmsum_fini(&arc_sums.arcstat_prune);
7647 wmsum_fini(&arc_sums.arcstat_meta_used);
7648 wmsum_fini(&arc_sums.arcstat_async_upgrade_sync);
7649 wmsum_fini(&arc_sums.arcstat_predictive_prefetch);
7650 wmsum_fini(&arc_sums.arcstat_demand_hit_predictive_prefetch);
7651 wmsum_fini(&arc_sums.arcstat_demand_iohit_predictive_prefetch);
7652 wmsum_fini(&arc_sums.arcstat_prescient_prefetch);
7653 wmsum_fini(&arc_sums.arcstat_demand_hit_prescient_prefetch);
7654 wmsum_fini(&arc_sums.arcstat_demand_iohit_prescient_prefetch);
7655 wmsum_fini(&arc_sums.arcstat_raw_size);
7656 wmsum_fini(&arc_sums.arcstat_cached_only_in_progress);
7657 wmsum_fini(&arc_sums.arcstat_abd_chunk_waste_size);
7658 }
7659
7660 uint64_t
7661 arc_target_bytes(void)
7662 {
7663 return (arc_c);
7664 }
7665
7666 void
7667 arc_set_limits(uint64_t allmem)
7668 {
7669 /* Set min cache to 1/32 of all memory, or 32MB, whichever is more. */
7670 arc_c_min = MAX(allmem / 32, 2ULL << SPA_MAXBLOCKSHIFT);
7671
7672 /* How to set default max varies by platform. */
7673 arc_c_max = arc_default_max(arc_c_min, allmem);
7674 }
7675 void
7676 arc_init(void)
7677 {
7678 uint64_t percent, allmem = arc_all_memory();
7679 mutex_init(&arc_evict_lock, NULL, MUTEX_DEFAULT, NULL);
7680 list_create(&arc_evict_waiters, sizeof (arc_evict_waiter_t),
7681 offsetof(arc_evict_waiter_t, aew_node));
7682
7683 arc_min_prefetch_ms = 1000;
7684 arc_min_prescient_prefetch_ms = 6000;
7685
7686 #if defined(_KERNEL)
7687 arc_lowmem_init();
7688 #endif
7689
7690 arc_set_limits(allmem);
7691
7692 #ifdef _KERNEL
7693 /*
7694 * If zfs_arc_max is non-zero at init, meaning it was set in the kernel
7695 * environment before the module was loaded, don't block setting the
7696 * maximum because it is less than arc_c_min, instead, reset arc_c_min
7697 * to a lower value.
7698 * zfs_arc_min will be handled by arc_tuning_update().
7699 */
7700 if (zfs_arc_max != 0 && zfs_arc_max >= MIN_ARC_MAX &&
7701 zfs_arc_max < allmem) {
7702 arc_c_max = zfs_arc_max;
7703 if (arc_c_min >= arc_c_max) {
7704 arc_c_min = MAX(zfs_arc_max / 2,
7705 2ULL << SPA_MAXBLOCKSHIFT);
7706 }
7707 }
7708 #else
7709 /*
7710 * In userland, there's only the memory pressure that we artificially
7711 * create (see arc_available_memory()). Don't let arc_c get too
7712 * small, because it can cause transactions to be larger than
7713 * arc_c, causing arc_tempreserve_space() to fail.
7714 */
7715 arc_c_min = MAX(arc_c_max / 2, 2ULL << SPA_MAXBLOCKSHIFT);
7716 #endif
7717
7718 arc_c = arc_c_min;
7719 /*
7720 * 32-bit fixed point fractions of metadata from total ARC size,
7721 * MRU data from all data and MRU metadata from all metadata.
7722 */
7723 arc_meta = (1ULL << 32) / 4; /* Metadata is 25% of arc_c. */
7724 arc_pd = (1ULL << 32) / 2; /* Data MRU is 50% of data. */
7725 arc_pm = (1ULL << 32) / 2; /* Metadata MRU is 50% of metadata. */
7726
7727 percent = MIN(zfs_arc_dnode_limit_percent, 100);
7728 arc_dnode_limit = arc_c_max * percent / 100;
7729
7730 /* Apply user specified tunings */
7731 arc_tuning_update(B_TRUE);
7732
7733 /* if kmem_flags are set, lets try to use less memory */
7734 if (kmem_debugging())
7735 arc_c = arc_c / 2;
7736 if (arc_c < arc_c_min)
7737 arc_c = arc_c_min;
7738
7739 arc_register_hotplug();
7740
7741 arc_state_init();
7742
7743 buf_init();
7744
7745 list_create(&arc_prune_list, sizeof (arc_prune_t),
7746 offsetof(arc_prune_t, p_node));
7747 mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL);
7748
7749 arc_prune_taskq = taskq_create("arc_prune", zfs_arc_prune_task_threads,
7750 defclsyspri, 100, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
7751
7752 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
7753 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
7754
7755 if (arc_ksp != NULL) {
7756 arc_ksp->ks_data = &arc_stats;
7757 arc_ksp->ks_update = arc_kstat_update;
7758 kstat_install(arc_ksp);
7759 }
7760
7761 arc_state_evict_markers =
7762 arc_state_alloc_markers(arc_state_evict_marker_count);
7763 arc_evict_zthr = zthr_create_timer("arc_evict",
7764 arc_evict_cb_check, arc_evict_cb, NULL, SEC2NSEC(1), defclsyspri);
7765 arc_reap_zthr = zthr_create_timer("arc_reap",
7766 arc_reap_cb_check, arc_reap_cb, NULL, SEC2NSEC(1), minclsyspri);
7767
7768 arc_warm = B_FALSE;
7769
7770 /*
7771 * Calculate maximum amount of dirty data per pool.
7772 *
7773 * If it has been set by a module parameter, take that.
7774 * Otherwise, use a percentage of physical memory defined by
7775 * zfs_dirty_data_max_percent (default 10%) with a cap at
7776 * zfs_dirty_data_max_max (default 4G or 25% of physical memory).
7777 */
7778 #ifdef __LP64__
7779 if (zfs_dirty_data_max_max == 0)
7780 zfs_dirty_data_max_max = MIN(4ULL * 1024 * 1024 * 1024,
7781 allmem * zfs_dirty_data_max_max_percent / 100);
7782 #else
7783 if (zfs_dirty_data_max_max == 0)
7784 zfs_dirty_data_max_max = MIN(1ULL * 1024 * 1024 * 1024,
7785 allmem * zfs_dirty_data_max_max_percent / 100);
7786 #endif
7787
7788 if (zfs_dirty_data_max == 0) {
7789 zfs_dirty_data_max = allmem *
7790 zfs_dirty_data_max_percent / 100;
7791 zfs_dirty_data_max = MIN(zfs_dirty_data_max,
7792 zfs_dirty_data_max_max);
7793 }
7794
7795 if (zfs_wrlog_data_max == 0) {
7796
7797 /*
7798 * dp_wrlog_total is reduced for each txg at the end of
7799 * spa_sync(). However, dp_dirty_total is reduced every time
7800 * a block is written out. Thus under normal operation,
7801 * dp_wrlog_total could grow 2 times as big as
7802 * zfs_dirty_data_max.
7803 */
7804 zfs_wrlog_data_max = zfs_dirty_data_max * 2;
7805 }
7806 }
7807
7808 void
7809 arc_fini(void)
7810 {
7811 arc_prune_t *p;
7812
7813 #ifdef _KERNEL
7814 arc_lowmem_fini();
7815 #endif /* _KERNEL */
7816
7817 /* Use B_TRUE to ensure *all* buffers are evicted */
7818 arc_flush(NULL, B_TRUE);
7819
7820 if (arc_ksp != NULL) {
7821 kstat_delete(arc_ksp);
7822 arc_ksp = NULL;
7823 }
7824
7825 taskq_wait(arc_prune_taskq);
7826 taskq_destroy(arc_prune_taskq);
7827
7828 mutex_enter(&arc_prune_mtx);
7829 while ((p = list_remove_head(&arc_prune_list)) != NULL) {
7830 zfs_refcount_remove(&p->p_refcnt, &arc_prune_list);
7831 zfs_refcount_destroy(&p->p_refcnt);
7832 kmem_free(p, sizeof (*p));
7833 }
7834 mutex_exit(&arc_prune_mtx);
7835
7836 list_destroy(&arc_prune_list);
7837 mutex_destroy(&arc_prune_mtx);
7838
7839 (void) zthr_cancel(arc_evict_zthr);
7840 (void) zthr_cancel(arc_reap_zthr);
7841 arc_state_free_markers(arc_state_evict_markers,
7842 arc_state_evict_marker_count);
7843
7844 mutex_destroy(&arc_evict_lock);
7845 list_destroy(&arc_evict_waiters);
7846
7847 /*
7848 * Free any buffers that were tagged for destruction. This needs
7849 * to occur before arc_state_fini() runs and destroys the aggsum
7850 * values which are updated when freeing scatter ABDs.
7851 */
7852 l2arc_do_free_on_write();
7853
7854 /*
7855 * buf_fini() must proceed arc_state_fini() because buf_fin() may
7856 * trigger the release of kmem magazines, which can callback to
7857 * arc_space_return() which accesses aggsums freed in act_state_fini().
7858 */
7859 buf_fini();
7860 arc_state_fini();
7861
7862 arc_unregister_hotplug();
7863
7864 /*
7865 * We destroy the zthrs after all the ARC state has been
7866 * torn down to avoid the case of them receiving any
7867 * wakeup() signals after they are destroyed.
7868 */
7869 zthr_destroy(arc_evict_zthr);
7870 zthr_destroy(arc_reap_zthr);
7871
7872 ASSERT0(arc_loaned_bytes);
7873 }
7874
7875 /*
7876 * Level 2 ARC
7877 *
7878 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
7879 * It uses dedicated storage devices to hold cached data, which are populated
7880 * using large infrequent writes. The main role of this cache is to boost
7881 * the performance of random read workloads. The intended L2ARC devices
7882 * include short-stroked disks, solid state disks, and other media with
7883 * substantially faster read latency than disk.
7884 *
7885 * +-----------------------+
7886 * | ARC |
7887 * +-----------------------+
7888 * | ^ ^
7889 * | | |
7890 * l2arc_feed_thread() arc_read()
7891 * | | |
7892 * | l2arc read |
7893 * V | |
7894 * +---------------+ |
7895 * | L2ARC | |
7896 * +---------------+ |
7897 * | ^ |
7898 * l2arc_write() | |
7899 * | | |
7900 * V | |
7901 * +-------+ +-------+
7902 * | vdev | | vdev |
7903 * | cache | | cache |
7904 * +-------+ +-------+
7905 * +=========+ .-----.
7906 * : L2ARC : |-_____-|
7907 * : devices : | Disks |
7908 * +=========+ `-_____-'
7909 *
7910 * Read requests are satisfied from the following sources, in order:
7911 *
7912 * 1) ARC
7913 * 2) vdev cache of L2ARC devices
7914 * 3) L2ARC devices
7915 * 4) vdev cache of disks
7916 * 5) disks
7917 *
7918 * Some L2ARC device types exhibit extremely slow write performance.
7919 * To accommodate for this there are some significant differences between
7920 * the L2ARC and traditional cache design:
7921 *
7922 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
7923 * the ARC behave as usual, freeing buffers and placing headers on ghost
7924 * lists. The ARC does not send buffers to the L2ARC during eviction as
7925 * this would add inflated write latencies for all ARC memory pressure.
7926 *
7927 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
7928 * It does this by periodically scanning buffers from the eviction-end of
7929 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
7930 * not already there. It scans until a headroom of buffers is satisfied,
7931 * which itself is a buffer for ARC eviction. If a compressible buffer is
7932 * found during scanning and selected for writing to an L2ARC device, we
7933 * temporarily boost scanning headroom during the next scan cycle to make
7934 * sure we adapt to compression effects (which might significantly reduce
7935 * the data volume we write to L2ARC). The thread that does this is
7936 * l2arc_feed_thread(), illustrated below; example sizes are included to
7937 * provide a better sense of ratio than this diagram:
7938 *
7939 * head --> tail
7940 * +---------------------+----------+
7941 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
7942 * +---------------------+----------+ | o L2ARC eligible
7943 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
7944 * +---------------------+----------+ |
7945 * 15.9 Gbytes ^ 32 Mbytes |
7946 * headroom |
7947 * l2arc_feed_thread()
7948 * |
7949 * l2arc write hand <--[oooo]--'
7950 * | 8 Mbyte
7951 * | write max
7952 * V
7953 * +==============================+
7954 * L2ARC dev |####|#|###|###| |####| ... |
7955 * +==============================+
7956 * 32 Gbytes
7957 *
7958 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
7959 * evicted, then the L2ARC has cached a buffer much sooner than it probably
7960 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
7961 * safe to say that this is an uncommon case, since buffers at the end of
7962 * the ARC lists have moved there due to inactivity.
7963 *
7964 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
7965 * then the L2ARC simply misses copying some buffers. This serves as a
7966 * pressure valve to prevent heavy read workloads from both stalling the ARC
7967 * with waits and clogging the L2ARC with writes. This also helps prevent
7968 * the potential for the L2ARC to churn if it attempts to cache content too
7969 * quickly, such as during backups of the entire pool.
7970 *
7971 * 5. After system boot and before the ARC has filled main memory, there are
7972 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
7973 * lists can remain mostly static. Instead of searching from tail of these
7974 * lists as pictured, the l2arc_feed_thread() will search from the list heads
7975 * for eligible buffers, greatly increasing its chance of finding them.
7976 *
7977 * The L2ARC device write speed is also boosted during this time so that
7978 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
7979 * there are no L2ARC reads, and no fear of degrading read performance
7980 * through increased writes.
7981 *
7982 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
7983 * the vdev queue can aggregate them into larger and fewer writes. Each
7984 * device is written to in a rotor fashion, sweeping writes through
7985 * available space then repeating.
7986 *
7987 * 7. The L2ARC does not store dirty content. It never needs to flush
7988 * write buffers back to disk based storage.
7989 *
7990 * 8. If an ARC buffer is written (and dirtied) which also exists in the
7991 * L2ARC, the now stale L2ARC buffer is immediately dropped.
7992 *
7993 * The performance of the L2ARC can be tweaked by a number of tunables, which
7994 * may be necessary for different workloads:
7995 *
7996 * l2arc_write_max max write bytes per interval
7997 * l2arc_write_boost extra write bytes during device warmup
7998 * l2arc_noprefetch skip caching prefetched buffers
7999 * l2arc_headroom number of max device writes to precache
8000 * l2arc_headroom_boost when we find compressed buffers during ARC
8001 * scanning, we multiply headroom by this
8002 * percentage factor for the next scan cycle,
8003 * since more compressed buffers are likely to
8004 * be present
8005 * l2arc_feed_secs seconds between L2ARC writing
8006 *
8007 * Tunables may be removed or added as future performance improvements are
8008 * integrated, and also may become zpool properties.
8009 *
8010 * There are three key functions that control how the L2ARC warms up:
8011 *
8012 * l2arc_write_eligible() check if a buffer is eligible to cache
8013 * l2arc_write_size() calculate how much to write
8014 * l2arc_write_interval() calculate sleep delay between writes
8015 *
8016 * These three functions determine what to write, how much, and how quickly
8017 * to send writes.
8018 *
8019 * L2ARC persistence:
8020 *
8021 * When writing buffers to L2ARC, we periodically add some metadata to
8022 * make sure we can pick them up after reboot, thus dramatically reducing
8023 * the impact that any downtime has on the performance of storage systems
8024 * with large caches.
8025 *
8026 * The implementation works fairly simply by integrating the following two
8027 * modifications:
8028 *
8029 * *) When writing to the L2ARC, we occasionally write a "l2arc log block",
8030 * which is an additional piece of metadata which describes what's been
8031 * written. This allows us to rebuild the arc_buf_hdr_t structures of the
8032 * main ARC buffers. There are 2 linked-lists of log blocks headed by
8033 * dh_start_lbps[2]. We alternate which chain we append to, so they are
8034 * time-wise and offset-wise interleaved, but that is an optimization rather
8035 * than for correctness. The log block also includes a pointer to the
8036 * previous block in its chain.
8037 *
8038 * *) We reserve SPA_MINBLOCKSIZE of space at the start of each L2ARC device
8039 * for our header bookkeeping purposes. This contains a device header,
8040 * which contains our top-level reference structures. We update it each
8041 * time we write a new log block, so that we're able to locate it in the
8042 * L2ARC device. If this write results in an inconsistent device header
8043 * (e.g. due to power failure), we detect this by verifying the header's
8044 * checksum and simply fail to reconstruct the L2ARC after reboot.
8045 *
8046 * Implementation diagram:
8047 *
8048 * +=== L2ARC device (not to scale) ======================================+
8049 * | ___two newest log block pointers__.__________ |
8050 * | / \dh_start_lbps[1] |
8051 * | / \ \dh_start_lbps[0]|
8052 * |.___/__. V V |
8053 * ||L2 dev|....|lb |bufs |lb |bufs |lb |bufs |lb |bufs |lb |---(empty)---|
8054 * || hdr| ^ /^ /^ / / |
8055 * |+------+ ...--\-------/ \-----/--\------/ / |
8056 * | \--------------/ \--------------/ |
8057 * +======================================================================+
8058 *
8059 * As can be seen on the diagram, rather than using a simple linked list,
8060 * we use a pair of linked lists with alternating elements. This is a
8061 * performance enhancement due to the fact that we only find out the
8062 * address of the next log block access once the current block has been
8063 * completely read in. Obviously, this hurts performance, because we'd be
8064 * keeping the device's I/O queue at only a 1 operation deep, thus
8065 * incurring a large amount of I/O round-trip latency. Having two lists
8066 * allows us to fetch two log blocks ahead of where we are currently
8067 * rebuilding L2ARC buffers.
8068 *
8069 * On-device data structures:
8070 *
8071 * L2ARC device header: l2arc_dev_hdr_phys_t
8072 * L2ARC log block: l2arc_log_blk_phys_t
8073 *
8074 * L2ARC reconstruction:
8075 *
8076 * When writing data, we simply write in the standard rotary fashion,
8077 * evicting buffers as we go and simply writing new data over them (writing
8078 * a new log block every now and then). This obviously means that once we
8079 * loop around the end of the device, we will start cutting into an already
8080 * committed log block (and its referenced data buffers), like so:
8081 *
8082 * current write head__ __old tail
8083 * \ /
8084 * V V
8085 * <--|bufs |lb |bufs |lb | |bufs |lb |bufs |lb |-->
8086 * ^ ^^^^^^^^^___________________________________
8087 * | \
8088 * <<nextwrite>> may overwrite this blk and/or its bufs --'
8089 *
8090 * When importing the pool, we detect this situation and use it to stop
8091 * our scanning process (see l2arc_rebuild).
8092 *
8093 * There is one significant caveat to consider when rebuilding ARC contents
8094 * from an L2ARC device: what about invalidated buffers? Given the above
8095 * construction, we cannot update blocks which we've already written to amend
8096 * them to remove buffers which were invalidated. Thus, during reconstruction,
8097 * we might be populating the cache with buffers for data that's not on the
8098 * main pool anymore, or may have been overwritten!
8099 *
8100 * As it turns out, this isn't a problem. Every arc_read request includes
8101 * both the DVA and, crucially, the birth TXG of the BP the caller is
8102 * looking for. So even if the cache were populated by completely rotten
8103 * blocks for data that had been long deleted and/or overwritten, we'll
8104 * never actually return bad data from the cache, since the DVA with the
8105 * birth TXG uniquely identify a block in space and time - once created,
8106 * a block is immutable on disk. The worst thing we have done is wasted
8107 * some time and memory at l2arc rebuild to reconstruct outdated ARC
8108 * entries that will get dropped from the l2arc as it is being updated
8109 * with new blocks.
8110 *
8111 * L2ARC buffers that have been evicted by l2arc_evict() ahead of the write
8112 * hand are not restored. This is done by saving the offset (in bytes)
8113 * l2arc_evict() has evicted to in the L2ARC device header and taking it
8114 * into account when restoring buffers.
8115 */
8116
8117 static boolean_t
8118 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr)
8119 {
8120 /*
8121 * A buffer is *not* eligible for the L2ARC if it:
8122 * 1. belongs to a different spa.
8123 * 2. is already cached on the L2ARC.
8124 * 3. has an I/O in progress (it may be an incomplete read).
8125 * 4. is flagged not eligible (zfs property).
8126 */
8127 if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) ||
8128 HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr))
8129 return (B_FALSE);
8130
8131 return (B_TRUE);
8132 }
8133
8134 static uint64_t
8135 l2arc_write_size(l2arc_dev_t *dev)
8136 {
8137 uint64_t size;
8138
8139 /*
8140 * Make sure our globals have meaningful values in case the user
8141 * altered them.
8142 */
8143 size = l2arc_write_max;
8144 if (size == 0) {
8145 cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must "
8146 "be greater than zero, resetting it to the default (%d)",
8147 L2ARC_WRITE_SIZE);
8148 size = l2arc_write_max = L2ARC_WRITE_SIZE;
8149 }
8150
8151 if (arc_warm == B_FALSE)
8152 size += l2arc_write_boost;
8153
8154 /* We need to add in the worst case scenario of log block overhead. */
8155 size += l2arc_log_blk_overhead(size, dev);
8156 if (dev->l2ad_vdev->vdev_has_trim && l2arc_trim_ahead > 0) {
8157 /*
8158 * Trim ahead of the write size 64MB or (l2arc_trim_ahead/100)
8159 * times the writesize, whichever is greater.
8160 */
8161 size += MAX(64 * 1024 * 1024,
8162 (size * l2arc_trim_ahead) / 100);
8163 }
8164
8165 /*
8166 * Make sure the write size does not exceed the size of the cache
8167 * device. This is important in l2arc_evict(), otherwise infinite
8168 * iteration can occur.
8169 */
8170 if (size > dev->l2ad_end - dev->l2ad_start) {
8171 cmn_err(CE_NOTE, "l2arc_write_max or l2arc_write_boost "
8172 "plus the overhead of log blocks (persistent L2ARC, "
8173 "%llu bytes) exceeds the size of the cache device "
8174 "(guid %llu), resetting them to the default (%d)",
8175 (u_longlong_t)l2arc_log_blk_overhead(size, dev),
8176 (u_longlong_t)dev->l2ad_vdev->vdev_guid, L2ARC_WRITE_SIZE);
8177
8178 size = l2arc_write_max = l2arc_write_boost = L2ARC_WRITE_SIZE;
8179
8180 if (l2arc_trim_ahead > 1) {
8181 cmn_err(CE_NOTE, "l2arc_trim_ahead set to 1");
8182 l2arc_trim_ahead = 1;
8183 }
8184
8185 if (arc_warm == B_FALSE)
8186 size += l2arc_write_boost;
8187
8188 size += l2arc_log_blk_overhead(size, dev);
8189 if (dev->l2ad_vdev->vdev_has_trim && l2arc_trim_ahead > 0) {
8190 size += MAX(64 * 1024 * 1024,
8191 (size * l2arc_trim_ahead) / 100);
8192 }
8193 }
8194
8195 return (size);
8196
8197 }
8198
8199 static clock_t
8200 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
8201 {
8202 clock_t interval, next, now;
8203
8204 /*
8205 * If the ARC lists are busy, increase our write rate; if the
8206 * lists are stale, idle back. This is achieved by checking
8207 * how much we previously wrote - if it was more than half of
8208 * what we wanted, schedule the next write much sooner.
8209 */
8210 if (l2arc_feed_again && wrote > (wanted / 2))
8211 interval = (hz * l2arc_feed_min_ms) / 1000;
8212 else
8213 interval = hz * l2arc_feed_secs;
8214
8215 now = ddi_get_lbolt();
8216 next = MAX(now, MIN(now + interval, began + interval));
8217
8218 return (next);
8219 }
8220
8221 /*
8222 * Cycle through L2ARC devices. This is how L2ARC load balances.
8223 * If a device is returned, this also returns holding the spa config lock.
8224 */
8225 static l2arc_dev_t *
8226 l2arc_dev_get_next(void)
8227 {
8228 l2arc_dev_t *first, *next = NULL;
8229
8230 /*
8231 * Lock out the removal of spas (spa_namespace_lock), then removal
8232 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
8233 * both locks will be dropped and a spa config lock held instead.
8234 */
8235 mutex_enter(&spa_namespace_lock);
8236 mutex_enter(&l2arc_dev_mtx);
8237
8238 /* if there are no vdevs, there is nothing to do */
8239 if (l2arc_ndev == 0)
8240 goto out;
8241
8242 first = NULL;
8243 next = l2arc_dev_last;
8244 do {
8245 /* loop around the list looking for a non-faulted vdev */
8246 if (next == NULL) {
8247 next = list_head(l2arc_dev_list);
8248 } else {
8249 next = list_next(l2arc_dev_list, next);
8250 if (next == NULL)
8251 next = list_head(l2arc_dev_list);
8252 }
8253
8254 /* if we have come back to the start, bail out */
8255 if (first == NULL)
8256 first = next;
8257 else if (next == first)
8258 break;
8259
8260 ASSERT3P(next, !=, NULL);
8261 } while (vdev_is_dead(next->l2ad_vdev) || next->l2ad_rebuild ||
8262 next->l2ad_trim_all);
8263
8264 /* if we were unable to find any usable vdevs, return NULL */
8265 if (vdev_is_dead(next->l2ad_vdev) || next->l2ad_rebuild ||
8266 next->l2ad_trim_all)
8267 next = NULL;
8268
8269 l2arc_dev_last = next;
8270
8271 out:
8272 mutex_exit(&l2arc_dev_mtx);
8273
8274 /*
8275 * Grab the config lock to prevent the 'next' device from being
8276 * removed while we are writing to it.
8277 */
8278 if (next != NULL)
8279 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
8280 mutex_exit(&spa_namespace_lock);
8281
8282 return (next);
8283 }
8284
8285 /*
8286 * Free buffers that were tagged for destruction.
8287 */
8288 static void
8289 l2arc_do_free_on_write(void)
8290 {
8291 l2arc_data_free_t *df;
8292
8293 mutex_enter(&l2arc_free_on_write_mtx);
8294 while ((df = list_remove_head(l2arc_free_on_write)) != NULL) {
8295 ASSERT3P(df->l2df_abd, !=, NULL);
8296 abd_free(df->l2df_abd);
8297 kmem_free(df, sizeof (l2arc_data_free_t));
8298 }
8299 mutex_exit(&l2arc_free_on_write_mtx);
8300 }
8301
8302 /*
8303 * A write to a cache device has completed. Update all headers to allow
8304 * reads from these buffers to begin.
8305 */
8306 static void
8307 l2arc_write_done(zio_t *zio)
8308 {
8309 l2arc_write_callback_t *cb;
8310 l2arc_lb_abd_buf_t *abd_buf;
8311 l2arc_lb_ptr_buf_t *lb_ptr_buf;
8312 l2arc_dev_t *dev;
8313 l2arc_dev_hdr_phys_t *l2dhdr;
8314 list_t *buflist;
8315 arc_buf_hdr_t *head, *hdr, *hdr_prev;
8316 kmutex_t *hash_lock;
8317 int64_t bytes_dropped = 0;
8318
8319 cb = zio->io_private;
8320 ASSERT3P(cb, !=, NULL);
8321 dev = cb->l2wcb_dev;
8322 l2dhdr = dev->l2ad_dev_hdr;
8323 ASSERT3P(dev, !=, NULL);
8324 head = cb->l2wcb_head;
8325 ASSERT3P(head, !=, NULL);
8326 buflist = &dev->l2ad_buflist;
8327 ASSERT3P(buflist, !=, NULL);
8328 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
8329 l2arc_write_callback_t *, cb);
8330
8331 /*
8332 * All writes completed, or an error was hit.
8333 */
8334 top:
8335 mutex_enter(&dev->l2ad_mtx);
8336 for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) {
8337 hdr_prev = list_prev(buflist, hdr);
8338
8339 hash_lock = HDR_LOCK(hdr);
8340
8341 /*
8342 * We cannot use mutex_enter or else we can deadlock
8343 * with l2arc_write_buffers (due to swapping the order
8344 * the hash lock and l2ad_mtx are taken).
8345 */
8346 if (!mutex_tryenter(hash_lock)) {
8347 /*
8348 * Missed the hash lock. We must retry so we
8349 * don't leave the ARC_FLAG_L2_WRITING bit set.
8350 */
8351 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry);
8352
8353 /*
8354 * We don't want to rescan the headers we've
8355 * already marked as having been written out, so
8356 * we reinsert the head node so we can pick up
8357 * where we left off.
8358 */
8359 list_remove(buflist, head);
8360 list_insert_after(buflist, hdr, head);
8361
8362 mutex_exit(&dev->l2ad_mtx);
8363
8364 /*
8365 * We wait for the hash lock to become available
8366 * to try and prevent busy waiting, and increase
8367 * the chance we'll be able to acquire the lock
8368 * the next time around.
8369 */
8370 mutex_enter(hash_lock);
8371 mutex_exit(hash_lock);
8372 goto top;
8373 }
8374
8375 /*
8376 * We could not have been moved into the arc_l2c_only
8377 * state while in-flight due to our ARC_FLAG_L2_WRITING
8378 * bit being set. Let's just ensure that's being enforced.
8379 */
8380 ASSERT(HDR_HAS_L1HDR(hdr));
8381
8382 /*
8383 * Skipped - drop L2ARC entry and mark the header as no
8384 * longer L2 eligibile.
8385 */
8386 if (zio->io_error != 0) {
8387 /*
8388 * Error - drop L2ARC entry.
8389 */
8390 list_remove(buflist, hdr);
8391 arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR);
8392
8393 uint64_t psize = HDR_GET_PSIZE(hdr);
8394 l2arc_hdr_arcstats_decrement(hdr);
8395
8396 bytes_dropped +=
8397 vdev_psize_to_asize(dev->l2ad_vdev, psize);
8398 (void) zfs_refcount_remove_many(&dev->l2ad_alloc,
8399 arc_hdr_size(hdr), hdr);
8400 }
8401
8402 /*
8403 * Allow ARC to begin reads and ghost list evictions to
8404 * this L2ARC entry.
8405 */
8406 arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING);
8407
8408 mutex_exit(hash_lock);
8409 }
8410
8411 /*
8412 * Free the allocated abd buffers for writing the log blocks.
8413 * If the zio failed reclaim the allocated space and remove the
8414 * pointers to these log blocks from the log block pointer list
8415 * of the L2ARC device.
8416 */
8417 while ((abd_buf = list_remove_tail(&cb->l2wcb_abd_list)) != NULL) {
8418 abd_free(abd_buf->abd);
8419 zio_buf_free(abd_buf, sizeof (*abd_buf));
8420 if (zio->io_error != 0) {
8421 lb_ptr_buf = list_remove_head(&dev->l2ad_lbptr_list);
8422 /*
8423 * L2BLK_GET_PSIZE returns aligned size for log
8424 * blocks.
8425 */
8426 uint64_t asize =
8427 L2BLK_GET_PSIZE((lb_ptr_buf->lb_ptr)->lbp_prop);
8428 bytes_dropped += asize;
8429 ARCSTAT_INCR(arcstat_l2_log_blk_asize, -asize);
8430 ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count);
8431 zfs_refcount_remove_many(&dev->l2ad_lb_asize, asize,
8432 lb_ptr_buf);
8433 zfs_refcount_remove(&dev->l2ad_lb_count, lb_ptr_buf);
8434 kmem_free(lb_ptr_buf->lb_ptr,
8435 sizeof (l2arc_log_blkptr_t));
8436 kmem_free(lb_ptr_buf, sizeof (l2arc_lb_ptr_buf_t));
8437 }
8438 }
8439 list_destroy(&cb->l2wcb_abd_list);
8440
8441 if (zio->io_error != 0) {
8442 ARCSTAT_BUMP(arcstat_l2_writes_error);
8443
8444 /*
8445 * Restore the lbps array in the header to its previous state.
8446 * If the list of log block pointers is empty, zero out the
8447 * log block pointers in the device header.
8448 */
8449 lb_ptr_buf = list_head(&dev->l2ad_lbptr_list);
8450 for (int i = 0; i < 2; i++) {
8451 if (lb_ptr_buf == NULL) {
8452 /*
8453 * If the list is empty zero out the device
8454 * header. Otherwise zero out the second log
8455 * block pointer in the header.
8456 */
8457 if (i == 0) {
8458 memset(l2dhdr, 0,
8459 dev->l2ad_dev_hdr_asize);
8460 } else {
8461 memset(&l2dhdr->dh_start_lbps[i], 0,
8462 sizeof (l2arc_log_blkptr_t));
8463 }
8464 break;
8465 }
8466 memcpy(&l2dhdr->dh_start_lbps[i], lb_ptr_buf->lb_ptr,
8467 sizeof (l2arc_log_blkptr_t));
8468 lb_ptr_buf = list_next(&dev->l2ad_lbptr_list,
8469 lb_ptr_buf);
8470 }
8471 }
8472
8473 ARCSTAT_BUMP(arcstat_l2_writes_done);
8474 list_remove(buflist, head);
8475 ASSERT(!HDR_HAS_L1HDR(head));
8476 kmem_cache_free(hdr_l2only_cache, head);
8477 mutex_exit(&dev->l2ad_mtx);
8478
8479 ASSERT(dev->l2ad_vdev != NULL);
8480 vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0);
8481
8482 l2arc_do_free_on_write();
8483
8484 kmem_free(cb, sizeof (l2arc_write_callback_t));
8485 }
8486
8487 static int
8488 l2arc_untransform(zio_t *zio, l2arc_read_callback_t *cb)
8489 {
8490 int ret;
8491 spa_t *spa = zio->io_spa;
8492 arc_buf_hdr_t *hdr = cb->l2rcb_hdr;
8493 blkptr_t *bp = zio->io_bp;
8494 uint8_t salt[ZIO_DATA_SALT_LEN];
8495 uint8_t iv[ZIO_DATA_IV_LEN];
8496 uint8_t mac[ZIO_DATA_MAC_LEN];
8497 boolean_t no_crypt = B_FALSE;
8498
8499 /*
8500 * ZIL data is never be written to the L2ARC, so we don't need
8501 * special handling for its unique MAC storage.
8502 */
8503 ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG);
8504 ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
8505 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
8506
8507 /*
8508 * If the data was encrypted, decrypt it now. Note that
8509 * we must check the bp here and not the hdr, since the
8510 * hdr does not have its encryption parameters updated
8511 * until arc_read_done().
8512 */
8513 if (BP_IS_ENCRYPTED(bp)) {
8514 abd_t *eabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr,
8515 ARC_HDR_USE_RESERVE);
8516
8517 zio_crypt_decode_params_bp(bp, salt, iv);
8518 zio_crypt_decode_mac_bp(bp, mac);
8519
8520 ret = spa_do_crypt_abd(B_FALSE, spa, &cb->l2rcb_zb,
8521 BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp),
8522 salt, iv, mac, HDR_GET_PSIZE(hdr), eabd,
8523 hdr->b_l1hdr.b_pabd, &no_crypt);
8524 if (ret != 0) {
8525 arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr);
8526 goto error;
8527 }
8528
8529 /*
8530 * If we actually performed decryption, replace b_pabd
8531 * with the decrypted data. Otherwise we can just throw
8532 * our decryption buffer away.
8533 */
8534 if (!no_crypt) {
8535 arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd,
8536 arc_hdr_size(hdr), hdr);
8537 hdr->b_l1hdr.b_pabd = eabd;
8538 zio->io_abd = eabd;
8539 } else {
8540 arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr);
8541 }
8542 }
8543
8544 /*
8545 * If the L2ARC block was compressed, but ARC compression
8546 * is disabled we decompress the data into a new buffer and
8547 * replace the existing data.
8548 */
8549 if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF &&
8550 !HDR_COMPRESSION_ENABLED(hdr)) {
8551 abd_t *cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr,
8552 ARC_HDR_USE_RESERVE);
8553 void *tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr));
8554
8555 ret = zio_decompress_data(HDR_GET_COMPRESS(hdr),
8556 hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr),
8557 HDR_GET_LSIZE(hdr), &hdr->b_complevel);
8558 if (ret != 0) {
8559 abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr));
8560 arc_free_data_abd(hdr, cabd, arc_hdr_size(hdr), hdr);
8561 goto error;
8562 }
8563
8564 abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr));
8565 arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd,
8566 arc_hdr_size(hdr), hdr);
8567 hdr->b_l1hdr.b_pabd = cabd;
8568 zio->io_abd = cabd;
8569 zio->io_size = HDR_GET_LSIZE(hdr);
8570 }
8571
8572 return (0);
8573
8574 error:
8575 return (ret);
8576 }
8577
8578
8579 /*
8580 * A read to a cache device completed. Validate buffer contents before
8581 * handing over to the regular ARC routines.
8582 */
8583 static void
8584 l2arc_read_done(zio_t *zio)
8585 {
8586 int tfm_error = 0;
8587 l2arc_read_callback_t *cb = zio->io_private;
8588 arc_buf_hdr_t *hdr;
8589 kmutex_t *hash_lock;
8590 boolean_t valid_cksum;
8591 boolean_t using_rdata = (BP_IS_ENCRYPTED(&cb->l2rcb_bp) &&
8592 (cb->l2rcb_flags & ZIO_FLAG_RAW_ENCRYPT));
8593
8594 ASSERT3P(zio->io_vd, !=, NULL);
8595 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
8596
8597 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
8598
8599 ASSERT3P(cb, !=, NULL);
8600 hdr = cb->l2rcb_hdr;
8601 ASSERT3P(hdr, !=, NULL);
8602
8603 hash_lock = HDR_LOCK(hdr);
8604 mutex_enter(hash_lock);
8605 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
8606
8607 /*
8608 * If the data was read into a temporary buffer,
8609 * move it and free the buffer.
8610 */
8611 if (cb->l2rcb_abd != NULL) {
8612 ASSERT3U(arc_hdr_size(hdr), <, zio->io_size);
8613 if (zio->io_error == 0) {
8614 if (using_rdata) {
8615 abd_copy(hdr->b_crypt_hdr.b_rabd,
8616 cb->l2rcb_abd, arc_hdr_size(hdr));
8617 } else {
8618 abd_copy(hdr->b_l1hdr.b_pabd,
8619 cb->l2rcb_abd, arc_hdr_size(hdr));
8620 }
8621 }
8622
8623 /*
8624 * The following must be done regardless of whether
8625 * there was an error:
8626 * - free the temporary buffer
8627 * - point zio to the real ARC buffer
8628 * - set zio size accordingly
8629 * These are required because zio is either re-used for
8630 * an I/O of the block in the case of the error
8631 * or the zio is passed to arc_read_done() and it
8632 * needs real data.
8633 */
8634 abd_free(cb->l2rcb_abd);
8635 zio->io_size = zio->io_orig_size = arc_hdr_size(hdr);
8636
8637 if (using_rdata) {
8638 ASSERT(HDR_HAS_RABD(hdr));
8639 zio->io_abd = zio->io_orig_abd =
8640 hdr->b_crypt_hdr.b_rabd;
8641 } else {
8642 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
8643 zio->io_abd = zio->io_orig_abd = hdr->b_l1hdr.b_pabd;
8644 }
8645 }
8646
8647 ASSERT3P(zio->io_abd, !=, NULL);
8648
8649 /*
8650 * Check this survived the L2ARC journey.
8651 */
8652 ASSERT(zio->io_abd == hdr->b_l1hdr.b_pabd ||
8653 (HDR_HAS_RABD(hdr) && zio->io_abd == hdr->b_crypt_hdr.b_rabd));
8654 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */
8655 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */
8656 zio->io_prop.zp_complevel = hdr->b_complevel;
8657
8658 valid_cksum = arc_cksum_is_equal(hdr, zio);
8659
8660 /*
8661 * b_rabd will always match the data as it exists on disk if it is
8662 * being used. Therefore if we are reading into b_rabd we do not
8663 * attempt to untransform the data.
8664 */
8665 if (valid_cksum && !using_rdata)
8666 tfm_error = l2arc_untransform(zio, cb);
8667
8668 if (valid_cksum && tfm_error == 0 && zio->io_error == 0 &&
8669 !HDR_L2_EVICTED(hdr)) {
8670 mutex_exit(hash_lock);
8671 zio->io_private = hdr;
8672 arc_read_done(zio);
8673 } else {
8674 /*
8675 * Buffer didn't survive caching. Increment stats and
8676 * reissue to the original storage device.
8677 */
8678 if (zio->io_error != 0) {
8679 ARCSTAT_BUMP(arcstat_l2_io_error);
8680 } else {
8681 zio->io_error = SET_ERROR(EIO);
8682 }
8683 if (!valid_cksum || tfm_error != 0)
8684 ARCSTAT_BUMP(arcstat_l2_cksum_bad);
8685
8686 /*
8687 * If there's no waiter, issue an async i/o to the primary
8688 * storage now. If there *is* a waiter, the caller must
8689 * issue the i/o in a context where it's OK to block.
8690 */
8691 if (zio->io_waiter == NULL) {
8692 zio_t *pio = zio_unique_parent(zio);
8693 void *abd = (using_rdata) ?
8694 hdr->b_crypt_hdr.b_rabd : hdr->b_l1hdr.b_pabd;
8695
8696 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
8697
8698 zio = zio_read(pio, zio->io_spa, zio->io_bp,
8699 abd, zio->io_size, arc_read_done,
8700 hdr, zio->io_priority, cb->l2rcb_flags,
8701 &cb->l2rcb_zb);
8702
8703 /*
8704 * Original ZIO will be freed, so we need to update
8705 * ARC header with the new ZIO pointer to be used
8706 * by zio_change_priority() in arc_read().
8707 */
8708 for (struct arc_callback *acb = hdr->b_l1hdr.b_acb;
8709 acb != NULL; acb = acb->acb_next)
8710 acb->acb_zio_head = zio;
8711
8712 mutex_exit(hash_lock);
8713 zio_nowait(zio);
8714 } else {
8715 mutex_exit(hash_lock);
8716 }
8717 }
8718
8719 kmem_free(cb, sizeof (l2arc_read_callback_t));
8720 }
8721
8722 /*
8723 * This is the list priority from which the L2ARC will search for pages to
8724 * cache. This is used within loops (0..3) to cycle through lists in the
8725 * desired order. This order can have a significant effect on cache
8726 * performance.
8727 *
8728 * Currently the metadata lists are hit first, MFU then MRU, followed by
8729 * the data lists. This function returns a locked list, and also returns
8730 * the lock pointer.
8731 */
8732 static multilist_sublist_t *
8733 l2arc_sublist_lock(int list_num)
8734 {
8735 multilist_t *ml = NULL;
8736 unsigned int idx;
8737
8738 ASSERT(list_num >= 0 && list_num < L2ARC_FEED_TYPES);
8739
8740 switch (list_num) {
8741 case 0:
8742 ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
8743 break;
8744 case 1:
8745 ml = &arc_mru->arcs_list[ARC_BUFC_METADATA];
8746 break;
8747 case 2:
8748 ml = &arc_mfu->arcs_list[ARC_BUFC_DATA];
8749 break;
8750 case 3:
8751 ml = &arc_mru->arcs_list[ARC_BUFC_DATA];
8752 break;
8753 default:
8754 return (NULL);
8755 }
8756
8757 /*
8758 * Return a randomly-selected sublist. This is acceptable
8759 * because the caller feeds only a little bit of data for each
8760 * call (8MB). Subsequent calls will result in different
8761 * sublists being selected.
8762 */
8763 idx = multilist_get_random_index(ml);
8764 return (multilist_sublist_lock(ml, idx));
8765 }
8766
8767 /*
8768 * Calculates the maximum overhead of L2ARC metadata log blocks for a given
8769 * L2ARC write size. l2arc_evict and l2arc_write_size need to include this
8770 * overhead in processing to make sure there is enough headroom available
8771 * when writing buffers.
8772 */
8773 static inline uint64_t
8774 l2arc_log_blk_overhead(uint64_t write_sz, l2arc_dev_t *dev)
8775 {
8776 if (dev->l2ad_log_entries == 0) {
8777 return (0);
8778 } else {
8779 uint64_t log_entries = write_sz >> SPA_MINBLOCKSHIFT;
8780
8781 uint64_t log_blocks = (log_entries +
8782 dev->l2ad_log_entries - 1) /
8783 dev->l2ad_log_entries;
8784
8785 return (vdev_psize_to_asize(dev->l2ad_vdev,
8786 sizeof (l2arc_log_blk_phys_t)) * log_blocks);
8787 }
8788 }
8789
8790 /*
8791 * Evict buffers from the device write hand to the distance specified in
8792 * bytes. This distance may span populated buffers, it may span nothing.
8793 * This is clearing a region on the L2ARC device ready for writing.
8794 * If the 'all' boolean is set, every buffer is evicted.
8795 */
8796 static void
8797 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
8798 {
8799 list_t *buflist;
8800 arc_buf_hdr_t *hdr, *hdr_prev;
8801 kmutex_t *hash_lock;
8802 uint64_t taddr;
8803 l2arc_lb_ptr_buf_t *lb_ptr_buf, *lb_ptr_buf_prev;
8804 vdev_t *vd = dev->l2ad_vdev;
8805 boolean_t rerun;
8806
8807 buflist = &dev->l2ad_buflist;
8808
8809 top:
8810 rerun = B_FALSE;
8811 if (dev->l2ad_hand + distance > dev->l2ad_end) {
8812 /*
8813 * When there is no space to accommodate upcoming writes,
8814 * evict to the end. Then bump the write and evict hands
8815 * to the start and iterate. This iteration does not
8816 * happen indefinitely as we make sure in
8817 * l2arc_write_size() that when the write hand is reset,
8818 * the write size does not exceed the end of the device.
8819 */
8820 rerun = B_TRUE;
8821 taddr = dev->l2ad_end;
8822 } else {
8823 taddr = dev->l2ad_hand + distance;
8824 }
8825 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
8826 uint64_t, taddr, boolean_t, all);
8827
8828 if (!all) {
8829 /*
8830 * This check has to be placed after deciding whether to
8831 * iterate (rerun).
8832 */
8833 if (dev->l2ad_first) {
8834 /*
8835 * This is the first sweep through the device. There is
8836 * nothing to evict. We have already trimmmed the
8837 * whole device.
8838 */
8839 goto out;
8840 } else {
8841 /*
8842 * Trim the space to be evicted.
8843 */
8844 if (vd->vdev_has_trim && dev->l2ad_evict < taddr &&
8845 l2arc_trim_ahead > 0) {
8846 /*
8847 * We have to drop the spa_config lock because
8848 * vdev_trim_range() will acquire it.
8849 * l2ad_evict already accounts for the label
8850 * size. To prevent vdev_trim_ranges() from
8851 * adding it again, we subtract it from
8852 * l2ad_evict.
8853 */
8854 spa_config_exit(dev->l2ad_spa, SCL_L2ARC, dev);
8855 vdev_trim_simple(vd,
8856 dev->l2ad_evict - VDEV_LABEL_START_SIZE,
8857 taddr - dev->l2ad_evict);
8858 spa_config_enter(dev->l2ad_spa, SCL_L2ARC, dev,
8859 RW_READER);
8860 }
8861
8862 /*
8863 * When rebuilding L2ARC we retrieve the evict hand
8864 * from the header of the device. Of note, l2arc_evict()
8865 * does not actually delete buffers from the cache
8866 * device, but trimming may do so depending on the
8867 * hardware implementation. Thus keeping track of the
8868 * evict hand is useful.
8869 */
8870 dev->l2ad_evict = MAX(dev->l2ad_evict, taddr);
8871 }
8872 }
8873
8874 retry:
8875 mutex_enter(&dev->l2ad_mtx);
8876 /*
8877 * We have to account for evicted log blocks. Run vdev_space_update()
8878 * on log blocks whose offset (in bytes) is before the evicted offset
8879 * (in bytes) by searching in the list of pointers to log blocks
8880 * present in the L2ARC device.
8881 */
8882 for (lb_ptr_buf = list_tail(&dev->l2ad_lbptr_list); lb_ptr_buf;
8883 lb_ptr_buf = lb_ptr_buf_prev) {
8884
8885 lb_ptr_buf_prev = list_prev(&dev->l2ad_lbptr_list, lb_ptr_buf);
8886
8887 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
8888 uint64_t asize = L2BLK_GET_PSIZE(
8889 (lb_ptr_buf->lb_ptr)->lbp_prop);
8890
8891 /*
8892 * We don't worry about log blocks left behind (ie
8893 * lbp_payload_start < l2ad_hand) because l2arc_write_buffers()
8894 * will never write more than l2arc_evict() evicts.
8895 */
8896 if (!all && l2arc_log_blkptr_valid(dev, lb_ptr_buf->lb_ptr)) {
8897 break;
8898 } else {
8899 vdev_space_update(vd, -asize, 0, 0);
8900 ARCSTAT_INCR(arcstat_l2_log_blk_asize, -asize);
8901 ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count);
8902 zfs_refcount_remove_many(&dev->l2ad_lb_asize, asize,
8903 lb_ptr_buf);
8904 zfs_refcount_remove(&dev->l2ad_lb_count, lb_ptr_buf);
8905 list_remove(&dev->l2ad_lbptr_list, lb_ptr_buf);
8906 kmem_free(lb_ptr_buf->lb_ptr,
8907 sizeof (l2arc_log_blkptr_t));
8908 kmem_free(lb_ptr_buf, sizeof (l2arc_lb_ptr_buf_t));
8909 }
8910 }
8911
8912 for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) {
8913 hdr_prev = list_prev(buflist, hdr);
8914
8915 ASSERT(!HDR_EMPTY(hdr));
8916 hash_lock = HDR_LOCK(hdr);
8917
8918 /*
8919 * We cannot use mutex_enter or else we can deadlock
8920 * with l2arc_write_buffers (due to swapping the order
8921 * the hash lock and l2ad_mtx are taken).
8922 */
8923 if (!mutex_tryenter(hash_lock)) {
8924 /*
8925 * Missed the hash lock. Retry.
8926 */
8927 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
8928 mutex_exit(&dev->l2ad_mtx);
8929 mutex_enter(hash_lock);
8930 mutex_exit(hash_lock);
8931 goto retry;
8932 }
8933
8934 /*
8935 * A header can't be on this list if it doesn't have L2 header.
8936 */
8937 ASSERT(HDR_HAS_L2HDR(hdr));
8938
8939 /* Ensure this header has finished being written. */
8940 ASSERT(!HDR_L2_WRITING(hdr));
8941 ASSERT(!HDR_L2_WRITE_HEAD(hdr));
8942
8943 if (!all && (hdr->b_l2hdr.b_daddr >= dev->l2ad_evict ||
8944 hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) {
8945 /*
8946 * We've evicted to the target address,
8947 * or the end of the device.
8948 */
8949 mutex_exit(hash_lock);
8950 break;
8951 }
8952
8953 if (!HDR_HAS_L1HDR(hdr)) {
8954 ASSERT(!HDR_L2_READING(hdr));
8955 /*
8956 * This doesn't exist in the ARC. Destroy.
8957 * arc_hdr_destroy() will call list_remove()
8958 * and decrement arcstat_l2_lsize.
8959 */
8960 arc_change_state(arc_anon, hdr);
8961 arc_hdr_destroy(hdr);
8962 } else {
8963 ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only);
8964 ARCSTAT_BUMP(arcstat_l2_evict_l1cached);
8965 /*
8966 * Invalidate issued or about to be issued
8967 * reads, since we may be about to write
8968 * over this location.
8969 */
8970 if (HDR_L2_READING(hdr)) {
8971 ARCSTAT_BUMP(arcstat_l2_evict_reading);
8972 arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED);
8973 }
8974
8975 arc_hdr_l2hdr_destroy(hdr);
8976 }
8977 mutex_exit(hash_lock);
8978 }
8979 mutex_exit(&dev->l2ad_mtx);
8980
8981 out:
8982 /*
8983 * We need to check if we evict all buffers, otherwise we may iterate
8984 * unnecessarily.
8985 */
8986 if (!all && rerun) {
8987 /*
8988 * Bump device hand to the device start if it is approaching the
8989 * end. l2arc_evict() has already evicted ahead for this case.
8990 */
8991 dev->l2ad_hand = dev->l2ad_start;
8992 dev->l2ad_evict = dev->l2ad_start;
8993 dev->l2ad_first = B_FALSE;
8994 goto top;
8995 }
8996
8997 if (!all) {
8998 /*
8999 * In case of cache device removal (all) the following
9000 * assertions may be violated without functional consequences
9001 * as the device is about to be removed.
9002 */
9003 ASSERT3U(dev->l2ad_hand + distance, <, dev->l2ad_end);
9004 if (!dev->l2ad_first)
9005 ASSERT3U(dev->l2ad_hand, <=, dev->l2ad_evict);
9006 }
9007 }
9008
9009 /*
9010 * Handle any abd transforms that might be required for writing to the L2ARC.
9011 * If successful, this function will always return an abd with the data
9012 * transformed as it is on disk in a new abd of asize bytes.
9013 */
9014 static int
9015 l2arc_apply_transforms(spa_t *spa, arc_buf_hdr_t *hdr, uint64_t asize,
9016 abd_t **abd_out)
9017 {
9018 int ret;
9019 void *tmp = NULL;
9020 abd_t *cabd = NULL, *eabd = NULL, *to_write = hdr->b_l1hdr.b_pabd;
9021 enum zio_compress compress = HDR_GET_COMPRESS(hdr);
9022 uint64_t psize = HDR_GET_PSIZE(hdr);
9023 uint64_t size = arc_hdr_size(hdr);
9024 boolean_t ismd = HDR_ISTYPE_METADATA(hdr);
9025 boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS);
9026 dsl_crypto_key_t *dck = NULL;
9027 uint8_t mac[ZIO_DATA_MAC_LEN] = { 0 };
9028 boolean_t no_crypt = B_FALSE;
9029
9030 ASSERT((HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF &&
9031 !HDR_COMPRESSION_ENABLED(hdr)) ||
9032 HDR_ENCRYPTED(hdr) || HDR_SHARED_DATA(hdr) || psize != asize);
9033 ASSERT3U(psize, <=, asize);
9034
9035 /*
9036 * If this data simply needs its own buffer, we simply allocate it
9037 * and copy the data. This may be done to eliminate a dependency on a
9038 * shared buffer or to reallocate the buffer to match asize.
9039 */
9040 if (HDR_HAS_RABD(hdr) && asize != psize) {
9041 ASSERT3U(asize, >=, psize);
9042 to_write = abd_alloc_for_io(asize, ismd);
9043 abd_copy(to_write, hdr->b_crypt_hdr.b_rabd, psize);
9044 if (psize != asize)
9045 abd_zero_off(to_write, psize, asize - psize);
9046 goto out;
9047 }
9048
9049 if ((compress == ZIO_COMPRESS_OFF || HDR_COMPRESSION_ENABLED(hdr)) &&
9050 !HDR_ENCRYPTED(hdr)) {
9051 ASSERT3U(size, ==, psize);
9052 to_write = abd_alloc_for_io(asize, ismd);
9053 abd_copy(to_write, hdr->b_l1hdr.b_pabd, size);
9054 if (size != asize)
9055 abd_zero_off(to_write, size, asize - size);
9056 goto out;
9057 }
9058
9059 if (compress != ZIO_COMPRESS_OFF && !HDR_COMPRESSION_ENABLED(hdr)) {
9060 /*
9061 * In some cases, we can wind up with size > asize, so
9062 * we need to opt for the larger allocation option here.
9063 *
9064 * (We also need abd_return_buf_copy in all cases because
9065 * it's an ASSERT() to modify the buffer before returning it
9066 * with arc_return_buf(), and all the compressors
9067 * write things before deciding to fail compression in nearly
9068 * every case.)
9069 */
9070 uint64_t bufsize = MAX(size, asize);
9071 cabd = abd_alloc_for_io(bufsize, ismd);
9072 tmp = abd_borrow_buf(cabd, bufsize);
9073
9074 psize = zio_compress_data(compress, to_write, &tmp, size,
9075 hdr->b_complevel);
9076
9077 if (psize >= asize) {
9078 psize = HDR_GET_PSIZE(hdr);
9079 abd_return_buf_copy(cabd, tmp, bufsize);
9080 HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF);
9081 to_write = cabd;
9082 abd_copy(to_write, hdr->b_l1hdr.b_pabd, psize);
9083 if (psize != asize)
9084 abd_zero_off(to_write, psize, asize - psize);
9085 goto encrypt;
9086 }
9087 ASSERT3U(psize, <=, HDR_GET_PSIZE(hdr));
9088 if (psize < asize)
9089 memset((char *)tmp + psize, 0, bufsize - psize);
9090 psize = HDR_GET_PSIZE(hdr);
9091 abd_return_buf_copy(cabd, tmp, bufsize);
9092 to_write = cabd;
9093 }
9094
9095 encrypt:
9096 if (HDR_ENCRYPTED(hdr)) {
9097 eabd = abd_alloc_for_io(asize, ismd);
9098
9099 /*
9100 * If the dataset was disowned before the buffer
9101 * made it to this point, the key to re-encrypt
9102 * it won't be available. In this case we simply
9103 * won't write the buffer to the L2ARC.
9104 */
9105 ret = spa_keystore_lookup_key(spa, hdr->b_crypt_hdr.b_dsobj,
9106 FTAG, &dck);
9107 if (ret != 0)
9108 goto error;
9109
9110 ret = zio_do_crypt_abd(B_TRUE, &dck->dck_key,
9111 hdr->b_crypt_hdr.b_ot, bswap, hdr->b_crypt_hdr.b_salt,
9112 hdr->b_crypt_hdr.b_iv, mac, psize, to_write, eabd,
9113 &no_crypt);
9114 if (ret != 0)
9115 goto error;
9116
9117 if (no_crypt)
9118 abd_copy(eabd, to_write, psize);
9119
9120 if (psize != asize)
9121 abd_zero_off(eabd, psize, asize - psize);
9122
9123 /* assert that the MAC we got here matches the one we saved */
9124 ASSERT0(memcmp(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN));
9125 spa_keystore_dsl_key_rele(spa, dck, FTAG);
9126
9127 if (to_write == cabd)
9128 abd_free(cabd);
9129
9130 to_write = eabd;
9131 }
9132
9133 out:
9134 ASSERT3P(to_write, !=, hdr->b_l1hdr.b_pabd);
9135 *abd_out = to_write;
9136 return (0);
9137
9138 error:
9139 if (dck != NULL)
9140 spa_keystore_dsl_key_rele(spa, dck, FTAG);
9141 if (cabd != NULL)
9142 abd_free(cabd);
9143 if (eabd != NULL)
9144 abd_free(eabd);
9145
9146 *abd_out = NULL;
9147 return (ret);
9148 }
9149
9150 static void
9151 l2arc_blk_fetch_done(zio_t *zio)
9152 {
9153 l2arc_read_callback_t *cb;
9154
9155 cb = zio->io_private;
9156 if (cb->l2rcb_abd != NULL)
9157 abd_free(cb->l2rcb_abd);
9158 kmem_free(cb, sizeof (l2arc_read_callback_t));
9159 }
9160
9161 /*
9162 * Find and write ARC buffers to the L2ARC device.
9163 *
9164 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
9165 * for reading until they have completed writing.
9166 * The headroom_boost is an in-out parameter used to maintain headroom boost
9167 * state between calls to this function.
9168 *
9169 * Returns the number of bytes actually written (which may be smaller than
9170 * the delta by which the device hand has changed due to alignment and the
9171 * writing of log blocks).
9172 */
9173 static uint64_t
9174 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz)
9175 {
9176 arc_buf_hdr_t *hdr, *hdr_prev, *head;
9177 uint64_t write_asize, write_psize, write_lsize, headroom;
9178 boolean_t full;
9179 l2arc_write_callback_t *cb = NULL;
9180 zio_t *pio, *wzio;
9181 uint64_t guid = spa_load_guid(spa);
9182 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr;
9183
9184 ASSERT3P(dev->l2ad_vdev, !=, NULL);
9185
9186 pio = NULL;
9187 write_lsize = write_asize = write_psize = 0;
9188 full = B_FALSE;
9189 head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE);
9190 arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR);
9191
9192 /*
9193 * Copy buffers for L2ARC writing.
9194 */
9195 for (int pass = 0; pass < L2ARC_FEED_TYPES; pass++) {
9196 /*
9197 * If pass == 1 or 3, we cache MRU metadata and data
9198 * respectively.
9199 */
9200 if (l2arc_mfuonly) {
9201 if (pass == 1 || pass == 3)
9202 continue;
9203 }
9204
9205 multilist_sublist_t *mls = l2arc_sublist_lock(pass);
9206 uint64_t passed_sz = 0;
9207
9208 VERIFY3P(mls, !=, NULL);
9209
9210 /*
9211 * L2ARC fast warmup.
9212 *
9213 * Until the ARC is warm and starts to evict, read from the
9214 * head of the ARC lists rather than the tail.
9215 */
9216 if (arc_warm == B_FALSE)
9217 hdr = multilist_sublist_head(mls);
9218 else
9219 hdr = multilist_sublist_tail(mls);
9220
9221 headroom = target_sz * l2arc_headroom;
9222 if (zfs_compressed_arc_enabled)
9223 headroom = (headroom * l2arc_headroom_boost) / 100;
9224
9225 for (; hdr; hdr = hdr_prev) {
9226 kmutex_t *hash_lock;
9227 abd_t *to_write = NULL;
9228
9229 if (arc_warm == B_FALSE)
9230 hdr_prev = multilist_sublist_next(mls, hdr);
9231 else
9232 hdr_prev = multilist_sublist_prev(mls, hdr);
9233
9234 hash_lock = HDR_LOCK(hdr);
9235 if (!mutex_tryenter(hash_lock)) {
9236 /*
9237 * Skip this buffer rather than waiting.
9238 */
9239 continue;
9240 }
9241
9242 passed_sz += HDR_GET_LSIZE(hdr);
9243 if (l2arc_headroom != 0 && passed_sz > headroom) {
9244 /*
9245 * Searched too far.
9246 */
9247 mutex_exit(hash_lock);
9248 break;
9249 }
9250
9251 if (!l2arc_write_eligible(guid, hdr)) {
9252 mutex_exit(hash_lock);
9253 continue;
9254 }
9255
9256 ASSERT(HDR_HAS_L1HDR(hdr));
9257
9258 ASSERT3U(HDR_GET_PSIZE(hdr), >, 0);
9259 ASSERT3U(arc_hdr_size(hdr), >, 0);
9260 ASSERT(hdr->b_l1hdr.b_pabd != NULL ||
9261 HDR_HAS_RABD(hdr));
9262 uint64_t psize = HDR_GET_PSIZE(hdr);
9263 uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev,
9264 psize);
9265
9266 /*
9267 * If the allocated size of this buffer plus the max
9268 * size for the pending log block exceeds the evicted
9269 * target size, terminate writing buffers for this run.
9270 */
9271 if (write_asize + asize +
9272 sizeof (l2arc_log_blk_phys_t) > target_sz) {
9273 full = B_TRUE;
9274 mutex_exit(hash_lock);
9275 break;
9276 }
9277
9278 /*
9279 * We rely on the L1 portion of the header below, so
9280 * it's invalid for this header to have been evicted out
9281 * of the ghost cache, prior to being written out. The
9282 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
9283 */
9284 arc_hdr_set_flags(hdr, ARC_FLAG_L2_WRITING);
9285
9286 /*
9287 * If this header has b_rabd, we can use this since it
9288 * must always match the data exactly as it exists on
9289 * disk. Otherwise, the L2ARC can normally use the
9290 * hdr's data, but if we're sharing data between the
9291 * hdr and one of its bufs, L2ARC needs its own copy of
9292 * the data so that the ZIO below can't race with the
9293 * buf consumer. To ensure that this copy will be
9294 * available for the lifetime of the ZIO and be cleaned
9295 * up afterwards, we add it to the l2arc_free_on_write
9296 * queue. If we need to apply any transforms to the
9297 * data (compression, encryption) we will also need the
9298 * extra buffer.
9299 */
9300 if (HDR_HAS_RABD(hdr) && psize == asize) {
9301 to_write = hdr->b_crypt_hdr.b_rabd;
9302 } else if ((HDR_COMPRESSION_ENABLED(hdr) ||
9303 HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) &&
9304 !HDR_ENCRYPTED(hdr) && !HDR_SHARED_DATA(hdr) &&
9305 psize == asize) {
9306 to_write = hdr->b_l1hdr.b_pabd;
9307 } else {
9308 int ret;
9309 arc_buf_contents_t type = arc_buf_type(hdr);
9310
9311 ret = l2arc_apply_transforms(spa, hdr, asize,
9312 &to_write);
9313 if (ret != 0) {
9314 arc_hdr_clear_flags(hdr,
9315 ARC_FLAG_L2_WRITING);
9316 mutex_exit(hash_lock);
9317 continue;
9318 }
9319
9320 l2arc_free_abd_on_write(to_write, asize, type);
9321 }
9322
9323 if (pio == NULL) {
9324 /*
9325 * Insert a dummy header on the buflist so
9326 * l2arc_write_done() can find where the
9327 * write buffers begin without searching.
9328 */
9329 mutex_enter(&dev->l2ad_mtx);
9330 list_insert_head(&dev->l2ad_buflist, head);
9331 mutex_exit(&dev->l2ad_mtx);
9332
9333 cb = kmem_alloc(
9334 sizeof (l2arc_write_callback_t), KM_SLEEP);
9335 cb->l2wcb_dev = dev;
9336 cb->l2wcb_head = head;
9337 /*
9338 * Create a list to save allocated abd buffers
9339 * for l2arc_log_blk_commit().
9340 */
9341 list_create(&cb->l2wcb_abd_list,
9342 sizeof (l2arc_lb_abd_buf_t),
9343 offsetof(l2arc_lb_abd_buf_t, node));
9344 pio = zio_root(spa, l2arc_write_done, cb,
9345 ZIO_FLAG_CANFAIL);
9346 }
9347
9348 hdr->b_l2hdr.b_dev = dev;
9349 hdr->b_l2hdr.b_hits = 0;
9350
9351 hdr->b_l2hdr.b_daddr = dev->l2ad_hand;
9352 hdr->b_l2hdr.b_arcs_state =
9353 hdr->b_l1hdr.b_state->arcs_state;
9354 arc_hdr_set_flags(hdr, ARC_FLAG_HAS_L2HDR);
9355
9356 mutex_enter(&dev->l2ad_mtx);
9357 list_insert_head(&dev->l2ad_buflist, hdr);
9358 mutex_exit(&dev->l2ad_mtx);
9359
9360 (void) zfs_refcount_add_many(&dev->l2ad_alloc,
9361 arc_hdr_size(hdr), hdr);
9362
9363 wzio = zio_write_phys(pio, dev->l2ad_vdev,
9364 hdr->b_l2hdr.b_daddr, asize, to_write,
9365 ZIO_CHECKSUM_OFF, NULL, hdr,
9366 ZIO_PRIORITY_ASYNC_WRITE,
9367 ZIO_FLAG_CANFAIL, B_FALSE);
9368
9369 write_lsize += HDR_GET_LSIZE(hdr);
9370 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
9371 zio_t *, wzio);
9372
9373 write_psize += psize;
9374 write_asize += asize;
9375 dev->l2ad_hand += asize;
9376 l2arc_hdr_arcstats_increment(hdr);
9377 vdev_space_update(dev->l2ad_vdev, asize, 0, 0);
9378
9379 mutex_exit(hash_lock);
9380
9381 /*
9382 * Append buf info to current log and commit if full.
9383 * arcstat_l2_{size,asize} kstats are updated
9384 * internally.
9385 */
9386 if (l2arc_log_blk_insert(dev, hdr)) {
9387 /*
9388 * l2ad_hand will be adjusted in
9389 * l2arc_log_blk_commit().
9390 */
9391 write_asize +=
9392 l2arc_log_blk_commit(dev, pio, cb);
9393 }
9394
9395 zio_nowait(wzio);
9396 }
9397
9398 multilist_sublist_unlock(mls);
9399
9400 if (full == B_TRUE)
9401 break;
9402 }
9403
9404 /* No buffers selected for writing? */
9405 if (pio == NULL) {
9406 ASSERT0(write_lsize);
9407 ASSERT(!HDR_HAS_L1HDR(head));
9408 kmem_cache_free(hdr_l2only_cache, head);
9409
9410 /*
9411 * Although we did not write any buffers l2ad_evict may
9412 * have advanced.
9413 */
9414 if (dev->l2ad_evict != l2dhdr->dh_evict)
9415 l2arc_dev_hdr_update(dev);
9416
9417 return (0);
9418 }
9419
9420 if (!dev->l2ad_first)
9421 ASSERT3U(dev->l2ad_hand, <=, dev->l2ad_evict);
9422
9423 ASSERT3U(write_asize, <=, target_sz);
9424 ARCSTAT_BUMP(arcstat_l2_writes_sent);
9425 ARCSTAT_INCR(arcstat_l2_write_bytes, write_psize);
9426
9427 dev->l2ad_writing = B_TRUE;
9428 (void) zio_wait(pio);
9429 dev->l2ad_writing = B_FALSE;
9430
9431 /*
9432 * Update the device header after the zio completes as
9433 * l2arc_write_done() may have updated the memory holding the log block
9434 * pointers in the device header.
9435 */
9436 l2arc_dev_hdr_update(dev);
9437
9438 return (write_asize);
9439 }
9440
9441 static boolean_t
9442 l2arc_hdr_limit_reached(void)
9443 {
9444 int64_t s = aggsum_upper_bound(&arc_sums.arcstat_l2_hdr_size);
9445
9446 return (arc_reclaim_needed() ||
9447 (s > (arc_warm ? arc_c : arc_c_max) * l2arc_meta_percent / 100));
9448 }
9449
9450 /*
9451 * This thread feeds the L2ARC at regular intervals. This is the beating
9452 * heart of the L2ARC.
9453 */
9454 static __attribute__((noreturn)) void
9455 l2arc_feed_thread(void *unused)
9456 {
9457 (void) unused;
9458 callb_cpr_t cpr;
9459 l2arc_dev_t *dev;
9460 spa_t *spa;
9461 uint64_t size, wrote;
9462 clock_t begin, next = ddi_get_lbolt();
9463 fstrans_cookie_t cookie;
9464
9465 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
9466
9467 mutex_enter(&l2arc_feed_thr_lock);
9468
9469 cookie = spl_fstrans_mark();
9470 while (l2arc_thread_exit == 0) {
9471 CALLB_CPR_SAFE_BEGIN(&cpr);
9472 (void) cv_timedwait_idle(&l2arc_feed_thr_cv,
9473 &l2arc_feed_thr_lock, next);
9474 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
9475 next = ddi_get_lbolt() + hz;
9476
9477 /*
9478 * Quick check for L2ARC devices.
9479 */
9480 mutex_enter(&l2arc_dev_mtx);
9481 if (l2arc_ndev == 0) {
9482 mutex_exit(&l2arc_dev_mtx);
9483 continue;
9484 }
9485 mutex_exit(&l2arc_dev_mtx);
9486 begin = ddi_get_lbolt();
9487
9488 /*
9489 * This selects the next l2arc device to write to, and in
9490 * doing so the next spa to feed from: dev->l2ad_spa. This
9491 * will return NULL if there are now no l2arc devices or if
9492 * they are all faulted.
9493 *
9494 * If a device is returned, its spa's config lock is also
9495 * held to prevent device removal. l2arc_dev_get_next()
9496 * will grab and release l2arc_dev_mtx.
9497 */
9498 if ((dev = l2arc_dev_get_next()) == NULL)
9499 continue;
9500
9501 spa = dev->l2ad_spa;
9502 ASSERT3P(spa, !=, NULL);
9503
9504 /*
9505 * If the pool is read-only then force the feed thread to
9506 * sleep a little longer.
9507 */
9508 if (!spa_writeable(spa)) {
9509 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
9510 spa_config_exit(spa, SCL_L2ARC, dev);
9511 continue;
9512 }
9513
9514 /*
9515 * Avoid contributing to memory pressure.
9516 */
9517 if (l2arc_hdr_limit_reached()) {
9518 ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
9519 spa_config_exit(spa, SCL_L2ARC, dev);
9520 continue;
9521 }
9522
9523 ARCSTAT_BUMP(arcstat_l2_feeds);
9524
9525 size = l2arc_write_size(dev);
9526
9527 /*
9528 * Evict L2ARC buffers that will be overwritten.
9529 */
9530 l2arc_evict(dev, size, B_FALSE);
9531
9532 /*
9533 * Write ARC buffers.
9534 */
9535 wrote = l2arc_write_buffers(spa, dev, size);
9536
9537 /*
9538 * Calculate interval between writes.
9539 */
9540 next = l2arc_write_interval(begin, size, wrote);
9541 spa_config_exit(spa, SCL_L2ARC, dev);
9542 }
9543 spl_fstrans_unmark(cookie);
9544
9545 l2arc_thread_exit = 0;
9546 cv_broadcast(&l2arc_feed_thr_cv);
9547 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */
9548 thread_exit();
9549 }
9550
9551 boolean_t
9552 l2arc_vdev_present(vdev_t *vd)
9553 {
9554 return (l2arc_vdev_get(vd) != NULL);
9555 }
9556
9557 /*
9558 * Returns the l2arc_dev_t associated with a particular vdev_t or NULL if
9559 * the vdev_t isn't an L2ARC device.
9560 */
9561 l2arc_dev_t *
9562 l2arc_vdev_get(vdev_t *vd)
9563 {
9564 l2arc_dev_t *dev;
9565
9566 mutex_enter(&l2arc_dev_mtx);
9567 for (dev = list_head(l2arc_dev_list); dev != NULL;
9568 dev = list_next(l2arc_dev_list, dev)) {
9569 if (dev->l2ad_vdev == vd)
9570 break;
9571 }
9572 mutex_exit(&l2arc_dev_mtx);
9573
9574 return (dev);
9575 }
9576
9577 static void
9578 l2arc_rebuild_dev(l2arc_dev_t *dev, boolean_t reopen)
9579 {
9580 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr;
9581 uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize;
9582 spa_t *spa = dev->l2ad_spa;
9583
9584 /*
9585 * The L2ARC has to hold at least the payload of one log block for
9586 * them to be restored (persistent L2ARC). The payload of a log block
9587 * depends on the amount of its log entries. We always write log blocks
9588 * with 1022 entries. How many of them are committed or restored depends
9589 * on the size of the L2ARC device. Thus the maximum payload of
9590 * one log block is 1022 * SPA_MAXBLOCKSIZE = 16GB. If the L2ARC device
9591 * is less than that, we reduce the amount of committed and restored
9592 * log entries per block so as to enable persistence.
9593 */
9594 if (dev->l2ad_end < l2arc_rebuild_blocks_min_l2size) {
9595 dev->l2ad_log_entries = 0;
9596 } else {
9597 dev->l2ad_log_entries = MIN((dev->l2ad_end -
9598 dev->l2ad_start) >> SPA_MAXBLOCKSHIFT,
9599 L2ARC_LOG_BLK_MAX_ENTRIES);
9600 }
9601
9602 /*
9603 * Read the device header, if an error is returned do not rebuild L2ARC.
9604 */
9605 if (l2arc_dev_hdr_read(dev) == 0 && dev->l2ad_log_entries > 0) {
9606 /*
9607 * If we are onlining a cache device (vdev_reopen) that was
9608 * still present (l2arc_vdev_present()) and rebuild is enabled,
9609 * we should evict all ARC buffers and pointers to log blocks
9610 * and reclaim their space before restoring its contents to
9611 * L2ARC.
9612 */
9613 if (reopen) {
9614 if (!l2arc_rebuild_enabled) {
9615 return;
9616 } else {
9617 l2arc_evict(dev, 0, B_TRUE);
9618 /* start a new log block */
9619 dev->l2ad_log_ent_idx = 0;
9620 dev->l2ad_log_blk_payload_asize = 0;
9621 dev->l2ad_log_blk_payload_start = 0;
9622 }
9623 }
9624 /*
9625 * Just mark the device as pending for a rebuild. We won't
9626 * be starting a rebuild in line here as it would block pool
9627 * import. Instead spa_load_impl will hand that off to an
9628 * async task which will call l2arc_spa_rebuild_start.
9629 */
9630 dev->l2ad_rebuild = B_TRUE;
9631 } else if (spa_writeable(spa)) {
9632 /*
9633 * In this case TRIM the whole device if l2arc_trim_ahead > 0,
9634 * otherwise create a new header. We zero out the memory holding
9635 * the header to reset dh_start_lbps. If we TRIM the whole
9636 * device the new header will be written by
9637 * vdev_trim_l2arc_thread() at the end of the TRIM to update the
9638 * trim_state in the header too. When reading the header, if
9639 * trim_state is not VDEV_TRIM_COMPLETE and l2arc_trim_ahead > 0
9640 * we opt to TRIM the whole device again.
9641 */
9642 if (l2arc_trim_ahead > 0) {
9643 dev->l2ad_trim_all = B_TRUE;
9644 } else {
9645 memset(l2dhdr, 0, l2dhdr_asize);
9646 l2arc_dev_hdr_update(dev);
9647 }
9648 }
9649 }
9650
9651 /*
9652 * Add a vdev for use by the L2ARC. By this point the spa has already
9653 * validated the vdev and opened it.
9654 */
9655 void
9656 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
9657 {
9658 l2arc_dev_t *adddev;
9659 uint64_t l2dhdr_asize;
9660
9661 ASSERT(!l2arc_vdev_present(vd));
9662
9663 /*
9664 * Create a new l2arc device entry.
9665 */
9666 adddev = vmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
9667 adddev->l2ad_spa = spa;
9668 adddev->l2ad_vdev = vd;
9669 /* leave extra size for an l2arc device header */
9670 l2dhdr_asize = adddev->l2ad_dev_hdr_asize =
9671 MAX(sizeof (*adddev->l2ad_dev_hdr), 1 << vd->vdev_ashift);
9672 adddev->l2ad_start = VDEV_LABEL_START_SIZE + l2dhdr_asize;
9673 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
9674 ASSERT3U(adddev->l2ad_start, <, adddev->l2ad_end);
9675 adddev->l2ad_hand = adddev->l2ad_start;
9676 adddev->l2ad_evict = adddev->l2ad_start;
9677 adddev->l2ad_first = B_TRUE;
9678 adddev->l2ad_writing = B_FALSE;
9679 adddev->l2ad_trim_all = B_FALSE;
9680 list_link_init(&adddev->l2ad_node);
9681 adddev->l2ad_dev_hdr = kmem_zalloc(l2dhdr_asize, KM_SLEEP);
9682
9683 mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL);
9684 /*
9685 * This is a list of all ARC buffers that are still valid on the
9686 * device.
9687 */
9688 list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
9689 offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node));
9690
9691 /*
9692 * This is a list of pointers to log blocks that are still present
9693 * on the device.
9694 */
9695 list_create(&adddev->l2ad_lbptr_list, sizeof (l2arc_lb_ptr_buf_t),
9696 offsetof(l2arc_lb_ptr_buf_t, node));
9697
9698 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
9699 zfs_refcount_create(&adddev->l2ad_alloc);
9700 zfs_refcount_create(&adddev->l2ad_lb_asize);
9701 zfs_refcount_create(&adddev->l2ad_lb_count);
9702
9703 /*
9704 * Decide if dev is eligible for L2ARC rebuild or whole device
9705 * trimming. This has to happen before the device is added in the
9706 * cache device list and l2arc_dev_mtx is released. Otherwise
9707 * l2arc_feed_thread() might already start writing on the
9708 * device.
9709 */
9710 l2arc_rebuild_dev(adddev, B_FALSE);
9711
9712 /*
9713 * Add device to global list
9714 */
9715 mutex_enter(&l2arc_dev_mtx);
9716 list_insert_head(l2arc_dev_list, adddev);
9717 atomic_inc_64(&l2arc_ndev);
9718 mutex_exit(&l2arc_dev_mtx);
9719 }
9720
9721 /*
9722 * Decide if a vdev is eligible for L2ARC rebuild, called from vdev_reopen()
9723 * in case of onlining a cache device.
9724 */
9725 void
9726 l2arc_rebuild_vdev(vdev_t *vd, boolean_t reopen)
9727 {
9728 l2arc_dev_t *dev = NULL;
9729
9730 dev = l2arc_vdev_get(vd);
9731 ASSERT3P(dev, !=, NULL);
9732
9733 /*
9734 * In contrast to l2arc_add_vdev() we do not have to worry about
9735 * l2arc_feed_thread() invalidating previous content when onlining a
9736 * cache device. The device parameters (l2ad*) are not cleared when
9737 * offlining the device and writing new buffers will not invalidate
9738 * all previous content. In worst case only buffers that have not had
9739 * their log block written to the device will be lost.
9740 * When onlining the cache device (ie offline->online without exporting
9741 * the pool in between) this happens:
9742 * vdev_reopen() -> vdev_open() -> l2arc_rebuild_vdev()
9743 * | |
9744 * vdev_is_dead() = B_FALSE l2ad_rebuild = B_TRUE
9745 * During the time where vdev_is_dead = B_FALSE and until l2ad_rebuild
9746 * is set to B_TRUE we might write additional buffers to the device.
9747 */
9748 l2arc_rebuild_dev(dev, reopen);
9749 }
9750
9751 /*
9752 * Remove a vdev from the L2ARC.
9753 */
9754 void
9755 l2arc_remove_vdev(vdev_t *vd)
9756 {
9757 l2arc_dev_t *remdev = NULL;
9758
9759 /*
9760 * Find the device by vdev
9761 */
9762 remdev = l2arc_vdev_get(vd);
9763 ASSERT3P(remdev, !=, NULL);
9764
9765 /*
9766 * Cancel any ongoing or scheduled rebuild.
9767 */
9768 mutex_enter(&l2arc_rebuild_thr_lock);
9769 if (remdev->l2ad_rebuild_began == B_TRUE) {
9770 remdev->l2ad_rebuild_cancel = B_TRUE;
9771 while (remdev->l2ad_rebuild == B_TRUE)
9772 cv_wait(&l2arc_rebuild_thr_cv, &l2arc_rebuild_thr_lock);
9773 }
9774 mutex_exit(&l2arc_rebuild_thr_lock);
9775
9776 /*
9777 * Remove device from global list
9778 */
9779 mutex_enter(&l2arc_dev_mtx);
9780 list_remove(l2arc_dev_list, remdev);
9781 l2arc_dev_last = NULL; /* may have been invalidated */
9782 atomic_dec_64(&l2arc_ndev);
9783 mutex_exit(&l2arc_dev_mtx);
9784
9785 /*
9786 * Clear all buflists and ARC references. L2ARC device flush.
9787 */
9788 l2arc_evict(remdev, 0, B_TRUE);
9789 list_destroy(&remdev->l2ad_buflist);
9790 ASSERT(list_is_empty(&remdev->l2ad_lbptr_list));
9791 list_destroy(&remdev->l2ad_lbptr_list);
9792 mutex_destroy(&remdev->l2ad_mtx);
9793 zfs_refcount_destroy(&remdev->l2ad_alloc);
9794 zfs_refcount_destroy(&remdev->l2ad_lb_asize);
9795 zfs_refcount_destroy(&remdev->l2ad_lb_count);
9796 kmem_free(remdev->l2ad_dev_hdr, remdev->l2ad_dev_hdr_asize);
9797 vmem_free(remdev, sizeof (l2arc_dev_t));
9798 }
9799
9800 void
9801 l2arc_init(void)
9802 {
9803 l2arc_thread_exit = 0;
9804 l2arc_ndev = 0;
9805
9806 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
9807 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
9808 mutex_init(&l2arc_rebuild_thr_lock, NULL, MUTEX_DEFAULT, NULL);
9809 cv_init(&l2arc_rebuild_thr_cv, NULL, CV_DEFAULT, NULL);
9810 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
9811 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
9812
9813 l2arc_dev_list = &L2ARC_dev_list;
9814 l2arc_free_on_write = &L2ARC_free_on_write;
9815 list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
9816 offsetof(l2arc_dev_t, l2ad_node));
9817 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
9818 offsetof(l2arc_data_free_t, l2df_list_node));
9819 }
9820
9821 void
9822 l2arc_fini(void)
9823 {
9824 mutex_destroy(&l2arc_feed_thr_lock);
9825 cv_destroy(&l2arc_feed_thr_cv);
9826 mutex_destroy(&l2arc_rebuild_thr_lock);
9827 cv_destroy(&l2arc_rebuild_thr_cv);
9828 mutex_destroy(&l2arc_dev_mtx);
9829 mutex_destroy(&l2arc_free_on_write_mtx);
9830
9831 list_destroy(l2arc_dev_list);
9832 list_destroy(l2arc_free_on_write);
9833 }
9834
9835 void
9836 l2arc_start(void)
9837 {
9838 if (!(spa_mode_global & SPA_MODE_WRITE))
9839 return;
9840
9841 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
9842 TS_RUN, defclsyspri);
9843 }
9844
9845 void
9846 l2arc_stop(void)
9847 {
9848 if (!(spa_mode_global & SPA_MODE_WRITE))
9849 return;
9850
9851 mutex_enter(&l2arc_feed_thr_lock);
9852 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */
9853 l2arc_thread_exit = 1;
9854 while (l2arc_thread_exit != 0)
9855 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
9856 mutex_exit(&l2arc_feed_thr_lock);
9857 }
9858
9859 /*
9860 * Punches out rebuild threads for the L2ARC devices in a spa. This should
9861 * be called after pool import from the spa async thread, since starting
9862 * these threads directly from spa_import() will make them part of the
9863 * "zpool import" context and delay process exit (and thus pool import).
9864 */
9865 void
9866 l2arc_spa_rebuild_start(spa_t *spa)
9867 {
9868 ASSERT(MUTEX_HELD(&spa_namespace_lock));
9869
9870 /*
9871 * Locate the spa's l2arc devices and kick off rebuild threads.
9872 */
9873 for (int i = 0; i < spa->spa_l2cache.sav_count; i++) {
9874 l2arc_dev_t *dev =
9875 l2arc_vdev_get(spa->spa_l2cache.sav_vdevs[i]);
9876 if (dev == NULL) {
9877 /* Don't attempt a rebuild if the vdev is UNAVAIL */
9878 continue;
9879 }
9880 mutex_enter(&l2arc_rebuild_thr_lock);
9881 if (dev->l2ad_rebuild && !dev->l2ad_rebuild_cancel) {
9882 dev->l2ad_rebuild_began = B_TRUE;
9883 (void) thread_create(NULL, 0, l2arc_dev_rebuild_thread,
9884 dev, 0, &p0, TS_RUN, minclsyspri);
9885 }
9886 mutex_exit(&l2arc_rebuild_thr_lock);
9887 }
9888 }
9889
9890 /*
9891 * Main entry point for L2ARC rebuilding.
9892 */
9893 static __attribute__((noreturn)) void
9894 l2arc_dev_rebuild_thread(void *arg)
9895 {
9896 l2arc_dev_t *dev = arg;
9897
9898 VERIFY(!dev->l2ad_rebuild_cancel);
9899 VERIFY(dev->l2ad_rebuild);
9900 (void) l2arc_rebuild(dev);
9901 mutex_enter(&l2arc_rebuild_thr_lock);
9902 dev->l2ad_rebuild_began = B_FALSE;
9903 dev->l2ad_rebuild = B_FALSE;
9904 mutex_exit(&l2arc_rebuild_thr_lock);
9905
9906 thread_exit();
9907 }
9908
9909 /*
9910 * This function implements the actual L2ARC metadata rebuild. It:
9911 * starts reading the log block chain and restores each block's contents
9912 * to memory (reconstructing arc_buf_hdr_t's).
9913 *
9914 * Operation stops under any of the following conditions:
9915 *
9916 * 1) We reach the end of the log block chain.
9917 * 2) We encounter *any* error condition (cksum errors, io errors)
9918 */
9919 static int
9920 l2arc_rebuild(l2arc_dev_t *dev)
9921 {
9922 vdev_t *vd = dev->l2ad_vdev;
9923 spa_t *spa = vd->vdev_spa;
9924 int err = 0;
9925 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr;
9926 l2arc_log_blk_phys_t *this_lb, *next_lb;
9927 zio_t *this_io = NULL, *next_io = NULL;
9928 l2arc_log_blkptr_t lbps[2];
9929 l2arc_lb_ptr_buf_t *lb_ptr_buf;
9930 boolean_t lock_held;
9931
9932 this_lb = vmem_zalloc(sizeof (*this_lb), KM_SLEEP);
9933 next_lb = vmem_zalloc(sizeof (*next_lb), KM_SLEEP);
9934
9935 /*
9936 * We prevent device removal while issuing reads to the device,
9937 * then during the rebuilding phases we drop this lock again so
9938 * that a spa_unload or device remove can be initiated - this is
9939 * safe, because the spa will signal us to stop before removing
9940 * our device and wait for us to stop.
9941 */
9942 spa_config_enter(spa, SCL_L2ARC, vd, RW_READER);
9943 lock_held = B_TRUE;
9944
9945 /*
9946 * Retrieve the persistent L2ARC device state.
9947 * L2BLK_GET_PSIZE returns aligned size for log blocks.
9948 */
9949 dev->l2ad_evict = MAX(l2dhdr->dh_evict, dev->l2ad_start);
9950 dev->l2ad_hand = MAX(l2dhdr->dh_start_lbps[0].lbp_daddr +
9951 L2BLK_GET_PSIZE((&l2dhdr->dh_start_lbps[0])->lbp_prop),
9952 dev->l2ad_start);
9953 dev->l2ad_first = !!(l2dhdr->dh_flags & L2ARC_DEV_HDR_EVICT_FIRST);
9954
9955 vd->vdev_trim_action_time = l2dhdr->dh_trim_action_time;
9956 vd->vdev_trim_state = l2dhdr->dh_trim_state;
9957
9958 /*
9959 * In case the zfs module parameter l2arc_rebuild_enabled is false
9960 * we do not start the rebuild process.
9961 */
9962 if (!l2arc_rebuild_enabled)
9963 goto out;
9964
9965 /* Prepare the rebuild process */
9966 memcpy(lbps, l2dhdr->dh_start_lbps, sizeof (lbps));
9967
9968 /* Start the rebuild process */
9969 for (;;) {
9970 if (!l2arc_log_blkptr_valid(dev, &lbps[0]))
9971 break;
9972
9973 if ((err = l2arc_log_blk_read(dev, &lbps[0], &lbps[1],
9974 this_lb, next_lb, this_io, &next_io)) != 0)
9975 goto out;
9976
9977 /*
9978 * Our memory pressure valve. If the system is running low
9979 * on memory, rather than swamping memory with new ARC buf
9980 * hdrs, we opt not to rebuild the L2ARC. At this point,
9981 * however, we have already set up our L2ARC dev to chain in
9982 * new metadata log blocks, so the user may choose to offline/
9983 * online the L2ARC dev at a later time (or re-import the pool)
9984 * to reconstruct it (when there's less memory pressure).
9985 */
9986 if (l2arc_hdr_limit_reached()) {
9987 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_lowmem);
9988 cmn_err(CE_NOTE, "System running low on memory, "
9989 "aborting L2ARC rebuild.");
9990 err = SET_ERROR(ENOMEM);
9991 goto out;
9992 }
9993
9994 spa_config_exit(spa, SCL_L2ARC, vd);
9995 lock_held = B_FALSE;
9996
9997 /*
9998 * Now that we know that the next_lb checks out alright, we
9999 * can start reconstruction from this log block.
10000 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10001 */
10002 uint64_t asize = L2BLK_GET_PSIZE((&lbps[0])->lbp_prop);
10003 l2arc_log_blk_restore(dev, this_lb, asize);
10004
10005 /*
10006 * log block restored, include its pointer in the list of
10007 * pointers to log blocks present in the L2ARC device.
10008 */
10009 lb_ptr_buf = kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t), KM_SLEEP);
10010 lb_ptr_buf->lb_ptr = kmem_zalloc(sizeof (l2arc_log_blkptr_t),
10011 KM_SLEEP);
10012 memcpy(lb_ptr_buf->lb_ptr, &lbps[0],
10013 sizeof (l2arc_log_blkptr_t));
10014 mutex_enter(&dev->l2ad_mtx);
10015 list_insert_tail(&dev->l2ad_lbptr_list, lb_ptr_buf);
10016 ARCSTAT_INCR(arcstat_l2_log_blk_asize, asize);
10017 ARCSTAT_BUMP(arcstat_l2_log_blk_count);
10018 zfs_refcount_add_many(&dev->l2ad_lb_asize, asize, lb_ptr_buf);
10019 zfs_refcount_add(&dev->l2ad_lb_count, lb_ptr_buf);
10020 mutex_exit(&dev->l2ad_mtx);
10021 vdev_space_update(vd, asize, 0, 0);
10022
10023 /*
10024 * Protection against loops of log blocks:
10025 *
10026 * l2ad_hand l2ad_evict
10027 * V V
10028 * l2ad_start |=======================================| l2ad_end
10029 * -----|||----|||---|||----|||
10030 * (3) (2) (1) (0)
10031 * ---|||---|||----|||---|||
10032 * (7) (6) (5) (4)
10033 *
10034 * In this situation the pointer of log block (4) passes
10035 * l2arc_log_blkptr_valid() but the log block should not be
10036 * restored as it is overwritten by the payload of log block
10037 * (0). Only log blocks (0)-(3) should be restored. We check
10038 * whether l2ad_evict lies in between the payload starting
10039 * offset of the next log block (lbps[1].lbp_payload_start)
10040 * and the payload starting offset of the present log block
10041 * (lbps[0].lbp_payload_start). If true and this isn't the
10042 * first pass, we are looping from the beginning and we should
10043 * stop.
10044 */
10045 if (l2arc_range_check_overlap(lbps[1].lbp_payload_start,
10046 lbps[0].lbp_payload_start, dev->l2ad_evict) &&
10047 !dev->l2ad_first)
10048 goto out;
10049
10050 kpreempt(KPREEMPT_SYNC);
10051 for (;;) {
10052 mutex_enter(&l2arc_rebuild_thr_lock);
10053 if (dev->l2ad_rebuild_cancel) {
10054 dev->l2ad_rebuild = B_FALSE;
10055 cv_signal(&l2arc_rebuild_thr_cv);
10056 mutex_exit(&l2arc_rebuild_thr_lock);
10057 err = SET_ERROR(ECANCELED);
10058 goto out;
10059 }
10060 mutex_exit(&l2arc_rebuild_thr_lock);
10061 if (spa_config_tryenter(spa, SCL_L2ARC, vd,
10062 RW_READER)) {
10063 lock_held = B_TRUE;
10064 break;
10065 }
10066 /*
10067 * L2ARC config lock held by somebody in writer,
10068 * possibly due to them trying to remove us. They'll
10069 * likely to want us to shut down, so after a little
10070 * delay, we check l2ad_rebuild_cancel and retry
10071 * the lock again.
10072 */
10073 delay(1);
10074 }
10075
10076 /*
10077 * Continue with the next log block.
10078 */
10079 lbps[0] = lbps[1];
10080 lbps[1] = this_lb->lb_prev_lbp;
10081 PTR_SWAP(this_lb, next_lb);
10082 this_io = next_io;
10083 next_io = NULL;
10084 }
10085
10086 if (this_io != NULL)
10087 l2arc_log_blk_fetch_abort(this_io);
10088 out:
10089 if (next_io != NULL)
10090 l2arc_log_blk_fetch_abort(next_io);
10091 vmem_free(this_lb, sizeof (*this_lb));
10092 vmem_free(next_lb, sizeof (*next_lb));
10093
10094 if (!l2arc_rebuild_enabled) {
10095 spa_history_log_internal(spa, "L2ARC rebuild", NULL,
10096 "disabled");
10097 } else if (err == 0 && zfs_refcount_count(&dev->l2ad_lb_count) > 0) {
10098 ARCSTAT_BUMP(arcstat_l2_rebuild_success);
10099 spa_history_log_internal(spa, "L2ARC rebuild", NULL,
10100 "successful, restored %llu blocks",
10101 (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count));
10102 } else if (err == 0 && zfs_refcount_count(&dev->l2ad_lb_count) == 0) {
10103 /*
10104 * No error but also nothing restored, meaning the lbps array
10105 * in the device header points to invalid/non-present log
10106 * blocks. Reset the header.
10107 */
10108 spa_history_log_internal(spa, "L2ARC rebuild", NULL,
10109 "no valid log blocks");
10110 memset(l2dhdr, 0, dev->l2ad_dev_hdr_asize);
10111 l2arc_dev_hdr_update(dev);
10112 } else if (err == ECANCELED) {
10113 /*
10114 * In case the rebuild was canceled do not log to spa history
10115 * log as the pool may be in the process of being removed.
10116 */
10117 zfs_dbgmsg("L2ARC rebuild aborted, restored %llu blocks",
10118 (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count));
10119 } else if (err != 0) {
10120 spa_history_log_internal(spa, "L2ARC rebuild", NULL,
10121 "aborted, restored %llu blocks",
10122 (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count));
10123 }
10124
10125 if (lock_held)
10126 spa_config_exit(spa, SCL_L2ARC, vd);
10127
10128 return (err);
10129 }
10130
10131 /*
10132 * Attempts to read the device header on the provided L2ARC device and writes
10133 * it to `hdr'. On success, this function returns 0, otherwise the appropriate
10134 * error code is returned.
10135 */
10136 static int
10137 l2arc_dev_hdr_read(l2arc_dev_t *dev)
10138 {
10139 int err;
10140 uint64_t guid;
10141 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr;
10142 const uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize;
10143 abd_t *abd;
10144
10145 guid = spa_guid(dev->l2ad_vdev->vdev_spa);
10146
10147 abd = abd_get_from_buf(l2dhdr, l2dhdr_asize);
10148
10149 err = zio_wait(zio_read_phys(NULL, dev->l2ad_vdev,
10150 VDEV_LABEL_START_SIZE, l2dhdr_asize, abd,
10151 ZIO_CHECKSUM_LABEL, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
10152 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
10153 ZIO_FLAG_SPECULATIVE, B_FALSE));
10154
10155 abd_free(abd);
10156
10157 if (err != 0) {
10158 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_dh_errors);
10159 zfs_dbgmsg("L2ARC IO error (%d) while reading device header, "
10160 "vdev guid: %llu", err,
10161 (u_longlong_t)dev->l2ad_vdev->vdev_guid);
10162 return (err);
10163 }
10164
10165 if (l2dhdr->dh_magic == BSWAP_64(L2ARC_DEV_HDR_MAGIC))
10166 byteswap_uint64_array(l2dhdr, sizeof (*l2dhdr));
10167
10168 if (l2dhdr->dh_magic != L2ARC_DEV_HDR_MAGIC ||
10169 l2dhdr->dh_spa_guid != guid ||
10170 l2dhdr->dh_vdev_guid != dev->l2ad_vdev->vdev_guid ||
10171 l2dhdr->dh_version != L2ARC_PERSISTENT_VERSION ||
10172 l2dhdr->dh_log_entries != dev->l2ad_log_entries ||
10173 l2dhdr->dh_end != dev->l2ad_end ||
10174 !l2arc_range_check_overlap(dev->l2ad_start, dev->l2ad_end,
10175 l2dhdr->dh_evict) ||
10176 (l2dhdr->dh_trim_state != VDEV_TRIM_COMPLETE &&
10177 l2arc_trim_ahead > 0)) {
10178 /*
10179 * Attempt to rebuild a device containing no actual dev hdr
10180 * or containing a header from some other pool or from another
10181 * version of persistent L2ARC.
10182 */
10183 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_unsupported);
10184 return (SET_ERROR(ENOTSUP));
10185 }
10186
10187 return (0);
10188 }
10189
10190 /*
10191 * Reads L2ARC log blocks from storage and validates their contents.
10192 *
10193 * This function implements a simple fetcher to make sure that while
10194 * we're processing one buffer the L2ARC is already fetching the next
10195 * one in the chain.
10196 *
10197 * The arguments this_lp and next_lp point to the current and next log block
10198 * address in the block chain. Similarly, this_lb and next_lb hold the
10199 * l2arc_log_blk_phys_t's of the current and next L2ARC blk.
10200 *
10201 * The `this_io' and `next_io' arguments are used for block fetching.
10202 * When issuing the first blk IO during rebuild, you should pass NULL for
10203 * `this_io'. This function will then issue a sync IO to read the block and
10204 * also issue an async IO to fetch the next block in the block chain. The
10205 * fetched IO is returned in `next_io'. On subsequent calls to this
10206 * function, pass the value returned in `next_io' from the previous call
10207 * as `this_io' and a fresh `next_io' pointer to hold the next fetch IO.
10208 * Prior to the call, you should initialize your `next_io' pointer to be
10209 * NULL. If no fetch IO was issued, the pointer is left set at NULL.
10210 *
10211 * On success, this function returns 0, otherwise it returns an appropriate
10212 * error code. On error the fetching IO is aborted and cleared before
10213 * returning from this function. Therefore, if we return `success', the
10214 * caller can assume that we have taken care of cleanup of fetch IOs.
10215 */
10216 static int
10217 l2arc_log_blk_read(l2arc_dev_t *dev,
10218 const l2arc_log_blkptr_t *this_lbp, const l2arc_log_blkptr_t *next_lbp,
10219 l2arc_log_blk_phys_t *this_lb, l2arc_log_blk_phys_t *next_lb,
10220 zio_t *this_io, zio_t **next_io)
10221 {
10222 int err = 0;
10223 zio_cksum_t cksum;
10224 abd_t *abd = NULL;
10225 uint64_t asize;
10226
10227 ASSERT(this_lbp != NULL && next_lbp != NULL);
10228 ASSERT(this_lb != NULL && next_lb != NULL);
10229 ASSERT(next_io != NULL && *next_io == NULL);
10230 ASSERT(l2arc_log_blkptr_valid(dev, this_lbp));
10231
10232 /*
10233 * Check to see if we have issued the IO for this log block in a
10234 * previous run. If not, this is the first call, so issue it now.
10235 */
10236 if (this_io == NULL) {
10237 this_io = l2arc_log_blk_fetch(dev->l2ad_vdev, this_lbp,
10238 this_lb);
10239 }
10240
10241 /*
10242 * Peek to see if we can start issuing the next IO immediately.
10243 */
10244 if (l2arc_log_blkptr_valid(dev, next_lbp)) {
10245 /*
10246 * Start issuing IO for the next log block early - this
10247 * should help keep the L2ARC device busy while we
10248 * decompress and restore this log block.
10249 */
10250 *next_io = l2arc_log_blk_fetch(dev->l2ad_vdev, next_lbp,
10251 next_lb);
10252 }
10253
10254 /* Wait for the IO to read this log block to complete */
10255 if ((err = zio_wait(this_io)) != 0) {
10256 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_io_errors);
10257 zfs_dbgmsg("L2ARC IO error (%d) while reading log block, "
10258 "offset: %llu, vdev guid: %llu", err,
10259 (u_longlong_t)this_lbp->lbp_daddr,
10260 (u_longlong_t)dev->l2ad_vdev->vdev_guid);
10261 goto cleanup;
10262 }
10263
10264 /*
10265 * Make sure the buffer checks out.
10266 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10267 */
10268 asize = L2BLK_GET_PSIZE((this_lbp)->lbp_prop);
10269 fletcher_4_native(this_lb, asize, NULL, &cksum);
10270 if (!ZIO_CHECKSUM_EQUAL(cksum, this_lbp->lbp_cksum)) {
10271 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_cksum_lb_errors);
10272 zfs_dbgmsg("L2ARC log block cksum failed, offset: %llu, "
10273 "vdev guid: %llu, l2ad_hand: %llu, l2ad_evict: %llu",
10274 (u_longlong_t)this_lbp->lbp_daddr,
10275 (u_longlong_t)dev->l2ad_vdev->vdev_guid,
10276 (u_longlong_t)dev->l2ad_hand,
10277 (u_longlong_t)dev->l2ad_evict);
10278 err = SET_ERROR(ECKSUM);
10279 goto cleanup;
10280 }
10281
10282 /* Now we can take our time decoding this buffer */
10283 switch (L2BLK_GET_COMPRESS((this_lbp)->lbp_prop)) {
10284 case ZIO_COMPRESS_OFF:
10285 break;
10286 case ZIO_COMPRESS_LZ4:
10287 abd = abd_alloc_for_io(asize, B_TRUE);
10288 abd_copy_from_buf_off(abd, this_lb, 0, asize);
10289 if ((err = zio_decompress_data(
10290 L2BLK_GET_COMPRESS((this_lbp)->lbp_prop),
10291 abd, this_lb, asize, sizeof (*this_lb), NULL)) != 0) {
10292 err = SET_ERROR(EINVAL);
10293 goto cleanup;
10294 }
10295 break;
10296 default:
10297 err = SET_ERROR(EINVAL);
10298 goto cleanup;
10299 }
10300 if (this_lb->lb_magic == BSWAP_64(L2ARC_LOG_BLK_MAGIC))
10301 byteswap_uint64_array(this_lb, sizeof (*this_lb));
10302 if (this_lb->lb_magic != L2ARC_LOG_BLK_MAGIC) {
10303 err = SET_ERROR(EINVAL);
10304 goto cleanup;
10305 }
10306 cleanup:
10307 /* Abort an in-flight fetch I/O in case of error */
10308 if (err != 0 && *next_io != NULL) {
10309 l2arc_log_blk_fetch_abort(*next_io);
10310 *next_io = NULL;
10311 }
10312 if (abd != NULL)
10313 abd_free(abd);
10314 return (err);
10315 }
10316
10317 /*
10318 * Restores the payload of a log block to ARC. This creates empty ARC hdr
10319 * entries which only contain an l2arc hdr, essentially restoring the
10320 * buffers to their L2ARC evicted state. This function also updates space
10321 * usage on the L2ARC vdev to make sure it tracks restored buffers.
10322 */
10323 static void
10324 l2arc_log_blk_restore(l2arc_dev_t *dev, const l2arc_log_blk_phys_t *lb,
10325 uint64_t lb_asize)
10326 {
10327 uint64_t size = 0, asize = 0;
10328 uint64_t log_entries = dev->l2ad_log_entries;
10329
10330 /*
10331 * Usually arc_adapt() is called only for data, not headers, but
10332 * since we may allocate significant amount of memory here, let ARC
10333 * grow its arc_c.
10334 */
10335 arc_adapt(log_entries * HDR_L2ONLY_SIZE);
10336
10337 for (int i = log_entries - 1; i >= 0; i--) {
10338 /*
10339 * Restore goes in the reverse temporal direction to preserve
10340 * correct temporal ordering of buffers in the l2ad_buflist.
10341 * l2arc_hdr_restore also does a list_insert_tail instead of
10342 * list_insert_head on the l2ad_buflist:
10343 *
10344 * LIST l2ad_buflist LIST
10345 * HEAD <------ (time) ------ TAIL
10346 * direction +-----+-----+-----+-----+-----+ direction
10347 * of l2arc <== | buf | buf | buf | buf | buf | ===> of rebuild
10348 * fill +-----+-----+-----+-----+-----+
10349 * ^ ^
10350 * | |
10351 * | |
10352 * l2arc_feed_thread l2arc_rebuild
10353 * will place new bufs here restores bufs here
10354 *
10355 * During l2arc_rebuild() the device is not used by
10356 * l2arc_feed_thread() as dev->l2ad_rebuild is set to true.
10357 */
10358 size += L2BLK_GET_LSIZE((&lb->lb_entries[i])->le_prop);
10359 asize += vdev_psize_to_asize(dev->l2ad_vdev,
10360 L2BLK_GET_PSIZE((&lb->lb_entries[i])->le_prop));
10361 l2arc_hdr_restore(&lb->lb_entries[i], dev);
10362 }
10363
10364 /*
10365 * Record rebuild stats:
10366 * size Logical size of restored buffers in the L2ARC
10367 * asize Aligned size of restored buffers in the L2ARC
10368 */
10369 ARCSTAT_INCR(arcstat_l2_rebuild_size, size);
10370 ARCSTAT_INCR(arcstat_l2_rebuild_asize, asize);
10371 ARCSTAT_INCR(arcstat_l2_rebuild_bufs, log_entries);
10372 ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize, lb_asize);
10373 ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio, asize / lb_asize);
10374 ARCSTAT_BUMP(arcstat_l2_rebuild_log_blks);
10375 }
10376
10377 /*
10378 * Restores a single ARC buf hdr from a log entry. The ARC buffer is put
10379 * into a state indicating that it has been evicted to L2ARC.
10380 */
10381 static void
10382 l2arc_hdr_restore(const l2arc_log_ent_phys_t *le, l2arc_dev_t *dev)
10383 {
10384 arc_buf_hdr_t *hdr, *exists;
10385 kmutex_t *hash_lock;
10386 arc_buf_contents_t type = L2BLK_GET_TYPE((le)->le_prop);
10387 uint64_t asize;
10388
10389 /*
10390 * Do all the allocation before grabbing any locks, this lets us
10391 * sleep if memory is full and we don't have to deal with failed
10392 * allocations.
10393 */
10394 hdr = arc_buf_alloc_l2only(L2BLK_GET_LSIZE((le)->le_prop), type,
10395 dev, le->le_dva, le->le_daddr,
10396 L2BLK_GET_PSIZE((le)->le_prop), le->le_birth,
10397 L2BLK_GET_COMPRESS((le)->le_prop), le->le_complevel,
10398 L2BLK_GET_PROTECTED((le)->le_prop),
10399 L2BLK_GET_PREFETCH((le)->le_prop),
10400 L2BLK_GET_STATE((le)->le_prop));
10401 asize = vdev_psize_to_asize(dev->l2ad_vdev,
10402 L2BLK_GET_PSIZE((le)->le_prop));
10403
10404 /*
10405 * vdev_space_update() has to be called before arc_hdr_destroy() to
10406 * avoid underflow since the latter also calls vdev_space_update().
10407 */
10408 l2arc_hdr_arcstats_increment(hdr);
10409 vdev_space_update(dev->l2ad_vdev, asize, 0, 0);
10410
10411 mutex_enter(&dev->l2ad_mtx);
10412 list_insert_tail(&dev->l2ad_buflist, hdr);
10413 (void) zfs_refcount_add_many(&dev->l2ad_alloc, arc_hdr_size(hdr), hdr);
10414 mutex_exit(&dev->l2ad_mtx);
10415
10416 exists = buf_hash_insert(hdr, &hash_lock);
10417 if (exists) {
10418 /* Buffer was already cached, no need to restore it. */
10419 arc_hdr_destroy(hdr);
10420 /*
10421 * If the buffer is already cached, check whether it has
10422 * L2ARC metadata. If not, enter them and update the flag.
10423 * This is important is case of onlining a cache device, since
10424 * we previously evicted all L2ARC metadata from ARC.
10425 */
10426 if (!HDR_HAS_L2HDR(exists)) {
10427 arc_hdr_set_flags(exists, ARC_FLAG_HAS_L2HDR);
10428 exists->b_l2hdr.b_dev = dev;
10429 exists->b_l2hdr.b_daddr = le->le_daddr;
10430 exists->b_l2hdr.b_arcs_state =
10431 L2BLK_GET_STATE((le)->le_prop);
10432 mutex_enter(&dev->l2ad_mtx);
10433 list_insert_tail(&dev->l2ad_buflist, exists);
10434 (void) zfs_refcount_add_many(&dev->l2ad_alloc,
10435 arc_hdr_size(exists), exists);
10436 mutex_exit(&dev->l2ad_mtx);
10437 l2arc_hdr_arcstats_increment(exists);
10438 vdev_space_update(dev->l2ad_vdev, asize, 0, 0);
10439 }
10440 ARCSTAT_BUMP(arcstat_l2_rebuild_bufs_precached);
10441 }
10442
10443 mutex_exit(hash_lock);
10444 }
10445
10446 /*
10447 * Starts an asynchronous read IO to read a log block. This is used in log
10448 * block reconstruction to start reading the next block before we are done
10449 * decoding and reconstructing the current block, to keep the l2arc device
10450 * nice and hot with read IO to process.
10451 * The returned zio will contain a newly allocated memory buffers for the IO
10452 * data which should then be freed by the caller once the zio is no longer
10453 * needed (i.e. due to it having completed). If you wish to abort this
10454 * zio, you should do so using l2arc_log_blk_fetch_abort, which takes
10455 * care of disposing of the allocated buffers correctly.
10456 */
10457 static zio_t *
10458 l2arc_log_blk_fetch(vdev_t *vd, const l2arc_log_blkptr_t *lbp,
10459 l2arc_log_blk_phys_t *lb)
10460 {
10461 uint32_t asize;
10462 zio_t *pio;
10463 l2arc_read_callback_t *cb;
10464
10465 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
10466 asize = L2BLK_GET_PSIZE((lbp)->lbp_prop);
10467 ASSERT(asize <= sizeof (l2arc_log_blk_phys_t));
10468
10469 cb = kmem_zalloc(sizeof (l2arc_read_callback_t), KM_SLEEP);
10470 cb->l2rcb_abd = abd_get_from_buf(lb, asize);
10471 pio = zio_root(vd->vdev_spa, l2arc_blk_fetch_done, cb,
10472 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY);
10473 (void) zio_nowait(zio_read_phys(pio, vd, lbp->lbp_daddr, asize,
10474 cb->l2rcb_abd, ZIO_CHECKSUM_OFF, NULL, NULL,
10475 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL |
10476 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY, B_FALSE));
10477
10478 return (pio);
10479 }
10480
10481 /*
10482 * Aborts a zio returned from l2arc_log_blk_fetch and frees the data
10483 * buffers allocated for it.
10484 */
10485 static void
10486 l2arc_log_blk_fetch_abort(zio_t *zio)
10487 {
10488 (void) zio_wait(zio);
10489 }
10490
10491 /*
10492 * Creates a zio to update the device header on an l2arc device.
10493 */
10494 void
10495 l2arc_dev_hdr_update(l2arc_dev_t *dev)
10496 {
10497 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr;
10498 const uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize;
10499 abd_t *abd;
10500 int err;
10501
10502 VERIFY(spa_config_held(dev->l2ad_spa, SCL_STATE_ALL, RW_READER));
10503
10504 l2dhdr->dh_magic = L2ARC_DEV_HDR_MAGIC;
10505 l2dhdr->dh_version = L2ARC_PERSISTENT_VERSION;
10506 l2dhdr->dh_spa_guid = spa_guid(dev->l2ad_vdev->vdev_spa);
10507 l2dhdr->dh_vdev_guid = dev->l2ad_vdev->vdev_guid;
10508 l2dhdr->dh_log_entries = dev->l2ad_log_entries;
10509 l2dhdr->dh_evict = dev->l2ad_evict;
10510 l2dhdr->dh_start = dev->l2ad_start;
10511 l2dhdr->dh_end = dev->l2ad_end;
10512 l2dhdr->dh_lb_asize = zfs_refcount_count(&dev->l2ad_lb_asize);
10513 l2dhdr->dh_lb_count = zfs_refcount_count(&dev->l2ad_lb_count);
10514 l2dhdr->dh_flags = 0;
10515 l2dhdr->dh_trim_action_time = dev->l2ad_vdev->vdev_trim_action_time;
10516 l2dhdr->dh_trim_state = dev->l2ad_vdev->vdev_trim_state;
10517 if (dev->l2ad_first)
10518 l2dhdr->dh_flags |= L2ARC_DEV_HDR_EVICT_FIRST;
10519
10520 abd = abd_get_from_buf(l2dhdr, l2dhdr_asize);
10521
10522 err = zio_wait(zio_write_phys(NULL, dev->l2ad_vdev,
10523 VDEV_LABEL_START_SIZE, l2dhdr_asize, abd, ZIO_CHECKSUM_LABEL, NULL,
10524 NULL, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_CANFAIL, B_FALSE));
10525
10526 abd_free(abd);
10527
10528 if (err != 0) {
10529 zfs_dbgmsg("L2ARC IO error (%d) while writing device header, "
10530 "vdev guid: %llu", err,
10531 (u_longlong_t)dev->l2ad_vdev->vdev_guid);
10532 }
10533 }
10534
10535 /*
10536 * Commits a log block to the L2ARC device. This routine is invoked from
10537 * l2arc_write_buffers when the log block fills up.
10538 * This function allocates some memory to temporarily hold the serialized
10539 * buffer to be written. This is then released in l2arc_write_done.
10540 */
10541 static uint64_t
10542 l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio, l2arc_write_callback_t *cb)
10543 {
10544 l2arc_log_blk_phys_t *lb = &dev->l2ad_log_blk;
10545 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr;
10546 uint64_t psize, asize;
10547 zio_t *wzio;
10548 l2arc_lb_abd_buf_t *abd_buf;
10549 uint8_t *tmpbuf = NULL;
10550 l2arc_lb_ptr_buf_t *lb_ptr_buf;
10551
10552 VERIFY3S(dev->l2ad_log_ent_idx, ==, dev->l2ad_log_entries);
10553
10554 abd_buf = zio_buf_alloc(sizeof (*abd_buf));
10555 abd_buf->abd = abd_get_from_buf(lb, sizeof (*lb));
10556 lb_ptr_buf = kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t), KM_SLEEP);
10557 lb_ptr_buf->lb_ptr = kmem_zalloc(sizeof (l2arc_log_blkptr_t), KM_SLEEP);
10558
10559 /* link the buffer into the block chain */
10560 lb->lb_prev_lbp = l2dhdr->dh_start_lbps[1];
10561 lb->lb_magic = L2ARC_LOG_BLK_MAGIC;
10562
10563 /*
10564 * l2arc_log_blk_commit() may be called multiple times during a single
10565 * l2arc_write_buffers() call. Save the allocated abd buffers in a list
10566 * so we can free them in l2arc_write_done() later on.
10567 */
10568 list_insert_tail(&cb->l2wcb_abd_list, abd_buf);
10569
10570 /* try to compress the buffer */
10571 psize = zio_compress_data(ZIO_COMPRESS_LZ4,
10572 abd_buf->abd, (void **) &tmpbuf, sizeof (*lb), 0);
10573
10574 /* a log block is never entirely zero */
10575 ASSERT(psize != 0);
10576 asize = vdev_psize_to_asize(dev->l2ad_vdev, psize);
10577 ASSERT(asize <= sizeof (*lb));
10578
10579 /*
10580 * Update the start log block pointer in the device header to point
10581 * to the log block we're about to write.
10582 */
10583 l2dhdr->dh_start_lbps[1] = l2dhdr->dh_start_lbps[0];
10584 l2dhdr->dh_start_lbps[0].lbp_daddr = dev->l2ad_hand;
10585 l2dhdr->dh_start_lbps[0].lbp_payload_asize =
10586 dev->l2ad_log_blk_payload_asize;
10587 l2dhdr->dh_start_lbps[0].lbp_payload_start =
10588 dev->l2ad_log_blk_payload_start;
10589 L2BLK_SET_LSIZE(
10590 (&l2dhdr->dh_start_lbps[0])->lbp_prop, sizeof (*lb));
10591 L2BLK_SET_PSIZE(
10592 (&l2dhdr->dh_start_lbps[0])->lbp_prop, asize);
10593 L2BLK_SET_CHECKSUM(
10594 (&l2dhdr->dh_start_lbps[0])->lbp_prop,
10595 ZIO_CHECKSUM_FLETCHER_4);
10596 if (asize < sizeof (*lb)) {
10597 /* compression succeeded */
10598 memset(tmpbuf + psize, 0, asize - psize);
10599 L2BLK_SET_COMPRESS(
10600 (&l2dhdr->dh_start_lbps[0])->lbp_prop,
10601 ZIO_COMPRESS_LZ4);
10602 } else {
10603 /* compression failed */
10604 memcpy(tmpbuf, lb, sizeof (*lb));
10605 L2BLK_SET_COMPRESS(
10606 (&l2dhdr->dh_start_lbps[0])->lbp_prop,
10607 ZIO_COMPRESS_OFF);
10608 }
10609
10610 /* checksum what we're about to write */
10611 fletcher_4_native(tmpbuf, asize, NULL,
10612 &l2dhdr->dh_start_lbps[0].lbp_cksum);
10613
10614 abd_free(abd_buf->abd);
10615
10616 /* perform the write itself */
10617 abd_buf->abd = abd_get_from_buf(tmpbuf, sizeof (*lb));
10618 abd_take_ownership_of_buf(abd_buf->abd, B_TRUE);
10619 wzio = zio_write_phys(pio, dev->l2ad_vdev, dev->l2ad_hand,
10620 asize, abd_buf->abd, ZIO_CHECKSUM_OFF, NULL, NULL,
10621 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_CANFAIL, B_FALSE);
10622 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, zio_t *, wzio);
10623 (void) zio_nowait(wzio);
10624
10625 dev->l2ad_hand += asize;
10626 /*
10627 * Include the committed log block's pointer in the list of pointers
10628 * to log blocks present in the L2ARC device.
10629 */
10630 memcpy(lb_ptr_buf->lb_ptr, &l2dhdr->dh_start_lbps[0],
10631 sizeof (l2arc_log_blkptr_t));
10632 mutex_enter(&dev->l2ad_mtx);
10633 list_insert_head(&dev->l2ad_lbptr_list, lb_ptr_buf);
10634 ARCSTAT_INCR(arcstat_l2_log_blk_asize, asize);
10635 ARCSTAT_BUMP(arcstat_l2_log_blk_count);
10636 zfs_refcount_add_many(&dev->l2ad_lb_asize, asize, lb_ptr_buf);
10637 zfs_refcount_add(&dev->l2ad_lb_count, lb_ptr_buf);
10638 mutex_exit(&dev->l2ad_mtx);
10639 vdev_space_update(dev->l2ad_vdev, asize, 0, 0);
10640
10641 /* bump the kstats */
10642 ARCSTAT_INCR(arcstat_l2_write_bytes, asize);
10643 ARCSTAT_BUMP(arcstat_l2_log_blk_writes);
10644 ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize, asize);
10645 ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio,
10646 dev->l2ad_log_blk_payload_asize / asize);
10647
10648 /* start a new log block */
10649 dev->l2ad_log_ent_idx = 0;
10650 dev->l2ad_log_blk_payload_asize = 0;
10651 dev->l2ad_log_blk_payload_start = 0;
10652
10653 return (asize);
10654 }
10655
10656 /*
10657 * Validates an L2ARC log block address to make sure that it can be read
10658 * from the provided L2ARC device.
10659 */
10660 boolean_t
10661 l2arc_log_blkptr_valid(l2arc_dev_t *dev, const l2arc_log_blkptr_t *lbp)
10662 {
10663 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
10664 uint64_t asize = L2BLK_GET_PSIZE((lbp)->lbp_prop);
10665 uint64_t end = lbp->lbp_daddr + asize - 1;
10666 uint64_t start = lbp->lbp_payload_start;
10667 boolean_t evicted = B_FALSE;
10668
10669 /*
10670 * A log block is valid if all of the following conditions are true:
10671 * - it fits entirely (including its payload) between l2ad_start and
10672 * l2ad_end
10673 * - it has a valid size
10674 * - neither the log block itself nor part of its payload was evicted
10675 * by l2arc_evict():
10676 *
10677 * l2ad_hand l2ad_evict
10678 * | | lbp_daddr
10679 * | start | | end
10680 * | | | | |
10681 * V V V V V
10682 * l2ad_start ============================================ l2ad_end
10683 * --------------------------||||
10684 * ^ ^
10685 * | log block
10686 * payload
10687 */
10688
10689 evicted =
10690 l2arc_range_check_overlap(start, end, dev->l2ad_hand) ||
10691 l2arc_range_check_overlap(start, end, dev->l2ad_evict) ||
10692 l2arc_range_check_overlap(dev->l2ad_hand, dev->l2ad_evict, start) ||
10693 l2arc_range_check_overlap(dev->l2ad_hand, dev->l2ad_evict, end);
10694
10695 return (start >= dev->l2ad_start && end <= dev->l2ad_end &&
10696 asize > 0 && asize <= sizeof (l2arc_log_blk_phys_t) &&
10697 (!evicted || dev->l2ad_first));
10698 }
10699
10700 /*
10701 * Inserts ARC buffer header `hdr' into the current L2ARC log block on
10702 * the device. The buffer being inserted must be present in L2ARC.
10703 * Returns B_TRUE if the L2ARC log block is full and needs to be committed
10704 * to L2ARC, or B_FALSE if it still has room for more ARC buffers.
10705 */
10706 static boolean_t
10707 l2arc_log_blk_insert(l2arc_dev_t *dev, const arc_buf_hdr_t *hdr)
10708 {
10709 l2arc_log_blk_phys_t *lb = &dev->l2ad_log_blk;
10710 l2arc_log_ent_phys_t *le;
10711
10712 if (dev->l2ad_log_entries == 0)
10713 return (B_FALSE);
10714
10715 int index = dev->l2ad_log_ent_idx++;
10716
10717 ASSERT3S(index, <, dev->l2ad_log_entries);
10718 ASSERT(HDR_HAS_L2HDR(hdr));
10719
10720 le = &lb->lb_entries[index];
10721 memset(le, 0, sizeof (*le));
10722 le->le_dva = hdr->b_dva;
10723 le->le_birth = hdr->b_birth;
10724 le->le_daddr = hdr->b_l2hdr.b_daddr;
10725 if (index == 0)
10726 dev->l2ad_log_blk_payload_start = le->le_daddr;
10727 L2BLK_SET_LSIZE((le)->le_prop, HDR_GET_LSIZE(hdr));
10728 L2BLK_SET_PSIZE((le)->le_prop, HDR_GET_PSIZE(hdr));
10729 L2BLK_SET_COMPRESS((le)->le_prop, HDR_GET_COMPRESS(hdr));
10730 le->le_complevel = hdr->b_complevel;
10731 L2BLK_SET_TYPE((le)->le_prop, hdr->b_type);
10732 L2BLK_SET_PROTECTED((le)->le_prop, !!(HDR_PROTECTED(hdr)));
10733 L2BLK_SET_PREFETCH((le)->le_prop, !!(HDR_PREFETCH(hdr)));
10734 L2BLK_SET_STATE((le)->le_prop, hdr->b_l1hdr.b_state->arcs_state);
10735
10736 dev->l2ad_log_blk_payload_asize += vdev_psize_to_asize(dev->l2ad_vdev,
10737 HDR_GET_PSIZE(hdr));
10738
10739 return (dev->l2ad_log_ent_idx == dev->l2ad_log_entries);
10740 }
10741
10742 /*
10743 * Checks whether a given L2ARC device address sits in a time-sequential
10744 * range. The trick here is that the L2ARC is a rotary buffer, so we can't
10745 * just do a range comparison, we need to handle the situation in which the
10746 * range wraps around the end of the L2ARC device. Arguments:
10747 * bottom -- Lower end of the range to check (written to earlier).
10748 * top -- Upper end of the range to check (written to later).
10749 * check -- The address for which we want to determine if it sits in
10750 * between the top and bottom.
10751 *
10752 * The 3-way conditional below represents the following cases:
10753 *
10754 * bottom < top : Sequentially ordered case:
10755 * <check>--------+-------------------+
10756 * | (overlap here?) |
10757 * L2ARC dev V V
10758 * |---------------<bottom>============<top>--------------|
10759 *
10760 * bottom > top: Looped-around case:
10761 * <check>--------+------------------+
10762 * | (overlap here?) |
10763 * L2ARC dev V V
10764 * |===============<top>---------------<bottom>===========|
10765 * ^ ^
10766 * | (or here?) |
10767 * +---------------+---------<check>
10768 *
10769 * top == bottom : Just a single address comparison.
10770 */
10771 boolean_t
10772 l2arc_range_check_overlap(uint64_t bottom, uint64_t top, uint64_t check)
10773 {
10774 if (bottom < top)
10775 return (bottom <= check && check <= top);
10776 else if (bottom > top)
10777 return (check <= top || bottom <= check);
10778 else
10779 return (check == top);
10780 }
10781
10782 EXPORT_SYMBOL(arc_buf_size);
10783 EXPORT_SYMBOL(arc_write);
10784 EXPORT_SYMBOL(arc_read);
10785 EXPORT_SYMBOL(arc_buf_info);
10786 EXPORT_SYMBOL(arc_getbuf_func);
10787 EXPORT_SYMBOL(arc_add_prune_callback);
10788 EXPORT_SYMBOL(arc_remove_prune_callback);
10789
10790 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min, param_set_arc_min,
10791 spl_param_get_u64, ZMOD_RW, "Minimum ARC size in bytes");
10792
10793 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, max, param_set_arc_max,
10794 spl_param_get_u64, ZMOD_RW, "Maximum ARC size in bytes");
10795
10796 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, meta_balance, UINT, ZMOD_RW,
10797 "Balance between metadata and data on ghost hits.");
10798
10799 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, grow_retry, param_set_arc_int,
10800 param_get_uint, ZMOD_RW, "Seconds before growing ARC size");
10801
10802 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, shrink_shift, param_set_arc_int,
10803 param_get_uint, ZMOD_RW, "log2(fraction of ARC to reclaim)");
10804
10805 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, pc_percent, UINT, ZMOD_RW,
10806 "Percent of pagecache to reclaim ARC to");
10807
10808 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, average_blocksize, UINT, ZMOD_RD,
10809 "Target average block size");
10810
10811 ZFS_MODULE_PARAM(zfs, zfs_, compressed_arc_enabled, INT, ZMOD_RW,
10812 "Disable compressed ARC buffers");
10813
10814 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min_prefetch_ms, param_set_arc_int,
10815 param_get_uint, ZMOD_RW, "Min life of prefetch block in ms");
10816
10817 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min_prescient_prefetch_ms,
10818 param_set_arc_int, param_get_uint, ZMOD_RW,
10819 "Min life of prescient prefetched block in ms");
10820
10821 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, write_max, U64, ZMOD_RW,
10822 "Max write bytes per interval");
10823
10824 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, write_boost, U64, ZMOD_RW,
10825 "Extra write bytes during device warmup");
10826
10827 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, headroom, U64, ZMOD_RW,
10828 "Number of max device writes to precache");
10829
10830 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, headroom_boost, U64, ZMOD_RW,
10831 "Compressed l2arc_headroom multiplier");
10832
10833 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, trim_ahead, U64, ZMOD_RW,
10834 "TRIM ahead L2ARC write size multiplier");
10835
10836 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_secs, U64, ZMOD_RW,
10837 "Seconds between L2ARC writing");
10838
10839 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_min_ms, U64, ZMOD_RW,
10840 "Min feed interval in milliseconds");
10841
10842 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, noprefetch, INT, ZMOD_RW,
10843 "Skip caching prefetched buffers");
10844
10845 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_again, INT, ZMOD_RW,
10846 "Turbo L2ARC warmup");
10847
10848 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, norw, INT, ZMOD_RW,
10849 "No reads during writes");
10850
10851 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, meta_percent, UINT, ZMOD_RW,
10852 "Percent of ARC size allowed for L2ARC-only headers");
10853
10854 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, rebuild_enabled, INT, ZMOD_RW,
10855 "Rebuild the L2ARC when importing a pool");
10856
10857 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, rebuild_blocks_min_l2size, U64, ZMOD_RW,
10858 "Min size in bytes to write rebuild log blocks in L2ARC");
10859
10860 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, mfuonly, INT, ZMOD_RW,
10861 "Cache only MFU data from ARC into L2ARC");
10862
10863 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, exclude_special, INT, ZMOD_RW,
10864 "Exclude dbufs on special vdevs from being cached to L2ARC if set.");
10865
10866 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, lotsfree_percent, param_set_arc_int,
10867 param_get_uint, ZMOD_RW, "System free memory I/O throttle in bytes");
10868
10869 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, sys_free, param_set_arc_u64,
10870 spl_param_get_u64, ZMOD_RW, "System free memory target size in bytes");
10871
10872 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, dnode_limit, param_set_arc_u64,
10873 spl_param_get_u64, ZMOD_RW, "Minimum bytes of dnodes in ARC");
10874
10875 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, dnode_limit_percent,
10876 param_set_arc_int, param_get_uint, ZMOD_RW,
10877 "Percent of ARC meta buffers for dnodes");
10878
10879 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, dnode_reduce_percent, UINT, ZMOD_RW,
10880 "Percentage of excess dnodes to try to unpin");
10881
10882 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, eviction_pct, UINT, ZMOD_RW,
10883 "When full, ARC allocation waits for eviction of this % of alloc size");
10884
10885 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, evict_batch_limit, UINT, ZMOD_RW,
10886 "The number of headers to evict per sublist before moving to the next");
10887
10888 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, prune_task_threads, INT, ZMOD_RW,
10889 "Number of arc_prune threads");