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