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