]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
428870ff | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
3ec34e55 | 23 | * Copyright (c) 2018, Joyent, Inc. |
4f072827 | 24 | * Copyright (c) 2011, 2020, Delphix. All rights reserved. |
77f6826b GA |
25 | * Copyright (c) 2014, Saso Kiselkov. All rights reserved. |
26 | * Copyright (c) 2017, Nexenta Systems, Inc. All rights reserved. | |
e3570464 | 27 | * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. |
77f6826b | 28 | * Copyright (c) 2020, George Amanakis. All rights reserved. |
10b3c7f5 MN |
29 | * Copyright (c) 2019, Klara Inc. |
30 | * Copyright (c) 2019, Allan Jude | |
fc34dfba AJ |
31 | * Copyright (c) 2020, The FreeBSD Foundation [1] |
32 | * | |
33 | * [1] Portions of this software were developed by Allan Jude | |
34 | * under sponsorship from the FreeBSD Foundation. | |
34dc7c2f BB |
35 | */ |
36 | ||
34dc7c2f BB |
37 | /* |
38 | * DVA-based Adjustable Replacement Cache | |
39 | * | |
40 | * While much of the theory of operation used here is | |
41 | * based on the self-tuning, low overhead replacement cache | |
42 | * presented by Megiddo and Modha at FAST 2003, there are some | |
43 | * significant differences: | |
44 | * | |
45 | * 1. The Megiddo and Modha model assumes any page is evictable. | |
46 | * Pages in its cache cannot be "locked" into memory. This makes | |
47 | * the eviction algorithm simple: evict the last page in the list. | |
48 | * This also make the performance characteristics easy to reason | |
49 | * about. Our cache is not so simple. At any given moment, some | |
50 | * subset of the blocks in the cache are un-evictable because we | |
51 | * have handed out a reference to them. Blocks are only evictable | |
52 | * when there are no external references active. This makes | |
53 | * eviction far more problematic: we choose to evict the evictable | |
54 | * blocks that are the "lowest" in the list. | |
55 | * | |
56 | * There are times when it is not possible to evict the requested | |
57 | * space. In these circumstances we are unable to adjust the cache | |
58 | * size. To prevent the cache growing unbounded at these times we | |
59 | * implement a "cache throttle" that slows the flow of new data | |
60 | * into the cache until we can make space available. | |
61 | * | |
62 | * 2. The Megiddo and Modha model assumes a fixed cache size. | |
63 | * Pages are evicted when the cache is full and there is a cache | |
64 | * miss. Our model has a variable sized cache. It grows with | |
65 | * high use, but also tries to react to memory pressure from the | |
66 | * operating system: decreasing its size when system memory is | |
67 | * tight. | |
68 | * | |
69 | * 3. The Megiddo and Modha model assumes a fixed page size. All | |
d3cc8b15 | 70 | * elements of the cache are therefore exactly the same size. So |
34dc7c2f BB |
71 | * when adjusting the cache size following a cache miss, its simply |
72 | * a matter of choosing a single page to evict. In our model, we | |
e1cfd73f | 73 | * have variable sized cache blocks (ranging from 512 bytes to |
d3cc8b15 | 74 | * 128K bytes). We therefore choose a set of blocks to evict to make |
34dc7c2f BB |
75 | * space for a cache miss that approximates as closely as possible |
76 | * the space used by the new block. | |
77 | * | |
78 | * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" | |
79 | * by N. Megiddo & D. Modha, FAST 2003 | |
80 | */ | |
81 | ||
82 | /* | |
83 | * The locking model: | |
84 | * | |
85 | * A new reference to a cache buffer can be obtained in two | |
86 | * ways: 1) via a hash table lookup using the DVA as a key, | |
87 | * or 2) via one of the ARC lists. The arc_read() interface | |
2aa34383 | 88 | * uses method 1, while the internal ARC algorithms for |
d3cc8b15 | 89 | * adjusting the cache use method 2. We therefore provide two |
34dc7c2f | 90 | * types of locks: 1) the hash table lock array, and 2) the |
2aa34383 | 91 | * ARC list locks. |
34dc7c2f | 92 | * |
5c839890 BC |
93 | * Buffers do not have their own mutexes, rather they rely on the |
94 | * hash table mutexes for the bulk of their protection (i.e. most | |
95 | * fields in the arc_buf_hdr_t are protected by these mutexes). | |
34dc7c2f BB |
96 | * |
97 | * buf_hash_find() returns the appropriate mutex (held) when it | |
98 | * locates the requested buffer in the hash table. It returns | |
99 | * NULL for the mutex if the buffer was not in the table. | |
100 | * | |
101 | * buf_hash_remove() expects the appropriate hash mutex to be | |
102 | * already held before it is invoked. | |
103 | * | |
2aa34383 | 104 | * Each ARC state also has a mutex which is used to protect the |
34dc7c2f | 105 | * buffer list associated with the state. When attempting to |
2aa34383 | 106 | * obtain a hash table lock while holding an ARC list lock you |
34dc7c2f BB |
107 | * must use: mutex_tryenter() to avoid deadlock. Also note that |
108 | * the active state mutex must be held before the ghost state mutex. | |
109 | * | |
ab26409d BB |
110 | * It as also possible to register a callback which is run when the |
111 | * arc_meta_limit is reached and no buffers can be safely evicted. In | |
112 | * this case the arc user should drop a reference on some arc buffers so | |
113 | * they can be reclaimed and the arc_meta_limit honored. For example, | |
114 | * when using the ZPL each dentry holds a references on a znode. These | |
115 | * dentries must be pruned before the arc buffer holding the znode can | |
116 | * be safely evicted. | |
117 | * | |
34dc7c2f BB |
118 | * Note that the majority of the performance stats are manipulated |
119 | * with atomic operations. | |
120 | * | |
b9541d6b | 121 | * The L2ARC uses the l2ad_mtx on each vdev for the following: |
34dc7c2f BB |
122 | * |
123 | * - L2ARC buflist creation | |
124 | * - L2ARC buflist eviction | |
125 | * - L2ARC write completion, which walks L2ARC buflists | |
126 | * - ARC header destruction, as it removes from L2ARC buflists | |
127 | * - ARC header release, as it removes from L2ARC buflists | |
128 | */ | |
129 | ||
d3c2ae1c GW |
130 | /* |
131 | * ARC operation: | |
132 | * | |
133 | * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure. | |
134 | * This structure can point either to a block that is still in the cache or to | |
135 | * one that is only accessible in an L2 ARC device, or it can provide | |
136 | * information about a block that was recently evicted. If a block is | |
137 | * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough | |
138 | * information to retrieve it from the L2ARC device. This information is | |
139 | * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block | |
140 | * that is in this state cannot access the data directly. | |
141 | * | |
142 | * Blocks that are actively being referenced or have not been evicted | |
143 | * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within | |
144 | * the arc_buf_hdr_t that will point to the data block in memory. A block can | |
145 | * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC | |
2aa34383 | 146 | * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and |
a6255b7f | 147 | * also in the arc_buf_hdr_t's private physical data block pointer (b_pabd). |
2aa34383 DK |
148 | * |
149 | * The L1ARC's data pointer may or may not be uncompressed. The ARC has the | |
a6255b7f DQ |
150 | * ability to store the physical data (b_pabd) associated with the DVA of the |
151 | * arc_buf_hdr_t. Since the b_pabd is a copy of the on-disk physical block, | |
2aa34383 DK |
152 | * it will match its on-disk compression characteristics. This behavior can be |
153 | * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the | |
a6255b7f | 154 | * compressed ARC functionality is disabled, the b_pabd will point to an |
2aa34383 DK |
155 | * uncompressed version of the on-disk data. |
156 | * | |
157 | * Data in the L1ARC is not accessed by consumers of the ARC directly. Each | |
158 | * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it. | |
159 | * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC | |
160 | * consumer. The ARC will provide references to this data and will keep it | |
161 | * cached until it is no longer in use. The ARC caches only the L1ARC's physical | |
162 | * data block and will evict any arc_buf_t that is no longer referenced. The | |
163 | * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the | |
d3c2ae1c GW |
164 | * "overhead_size" kstat. |
165 | * | |
2aa34383 DK |
166 | * Depending on the consumer, an arc_buf_t can be requested in uncompressed or |
167 | * compressed form. The typical case is that consumers will want uncompressed | |
168 | * data, and when that happens a new data buffer is allocated where the data is | |
169 | * decompressed for them to use. Currently the only consumer who wants | |
170 | * compressed arc_buf_t's is "zfs send", when it streams data exactly as it | |
171 | * exists on disk. When this happens, the arc_buf_t's data buffer is shared | |
172 | * with the arc_buf_hdr_t. | |
d3c2ae1c | 173 | * |
2aa34383 DK |
174 | * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The |
175 | * first one is owned by a compressed send consumer (and therefore references | |
176 | * the same compressed data buffer as the arc_buf_hdr_t) and the second could be | |
177 | * used by any other consumer (and has its own uncompressed copy of the data | |
178 | * buffer). | |
d3c2ae1c | 179 | * |
2aa34383 DK |
180 | * arc_buf_hdr_t |
181 | * +-----------+ | |
182 | * | fields | | |
183 | * | common to | | |
184 | * | L1- and | | |
185 | * | L2ARC | | |
186 | * +-----------+ | |
187 | * | l2arc_buf_hdr_t | |
188 | * | | | |
189 | * +-----------+ | |
190 | * | l1arc_buf_hdr_t | |
191 | * | | arc_buf_t | |
192 | * | b_buf +------------>+-----------+ arc_buf_t | |
a6255b7f | 193 | * | b_pabd +-+ |b_next +---->+-----------+ |
2aa34383 DK |
194 | * +-----------+ | |-----------| |b_next +-->NULL |
195 | * | |b_comp = T | +-----------+ | |
196 | * | |b_data +-+ |b_comp = F | | |
197 | * | +-----------+ | |b_data +-+ | |
198 | * +->+------+ | +-----------+ | | |
199 | * compressed | | | | | |
200 | * data | |<--------------+ | uncompressed | |
201 | * +------+ compressed, | data | |
202 | * shared +-->+------+ | |
203 | * data | | | |
204 | * | | | |
205 | * +------+ | |
d3c2ae1c GW |
206 | * |
207 | * When a consumer reads a block, the ARC must first look to see if the | |
2aa34383 DK |
208 | * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new |
209 | * arc_buf_t and either copies uncompressed data into a new data buffer from an | |
a6255b7f DQ |
210 | * existing uncompressed arc_buf_t, decompresses the hdr's b_pabd buffer into a |
211 | * new data buffer, or shares the hdr's b_pabd buffer, depending on whether the | |
2aa34383 DK |
212 | * hdr is compressed and the desired compression characteristics of the |
213 | * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the | |
214 | * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be | |
215 | * the last buffer in the hdr's b_buf list, however a shared compressed buf can | |
216 | * be anywhere in the hdr's list. | |
d3c2ae1c GW |
217 | * |
218 | * The diagram below shows an example of an uncompressed ARC hdr that is | |
2aa34383 DK |
219 | * sharing its data with an arc_buf_t (note that the shared uncompressed buf is |
220 | * the last element in the buf list): | |
d3c2ae1c GW |
221 | * |
222 | * arc_buf_hdr_t | |
223 | * +-----------+ | |
224 | * | | | |
225 | * | | | |
226 | * | | | |
227 | * +-----------+ | |
228 | * l2arc_buf_hdr_t| | | |
229 | * | | | |
230 | * +-----------+ | |
231 | * l1arc_buf_hdr_t| | | |
232 | * | | arc_buf_t (shared) | |
233 | * | b_buf +------------>+---------+ arc_buf_t | |
234 | * | | |b_next +---->+---------+ | |
a6255b7f | 235 | * | b_pabd +-+ |---------| |b_next +-->NULL |
d3c2ae1c GW |
236 | * +-----------+ | | | +---------+ |
237 | * | |b_data +-+ | | | |
238 | * | +---------+ | |b_data +-+ | |
239 | * +->+------+ | +---------+ | | |
240 | * | | | | | |
241 | * uncompressed | | | | | |
242 | * data +------+ | | | |
243 | * ^ +->+------+ | | |
244 | * | uncompressed | | | | |
245 | * | data | | | | |
246 | * | +------+ | | |
247 | * +---------------------------------+ | |
248 | * | |
a6255b7f | 249 | * Writing to the ARC requires that the ARC first discard the hdr's b_pabd |
d3c2ae1c | 250 | * since the physical block is about to be rewritten. The new data contents |
2aa34383 DK |
251 | * will be contained in the arc_buf_t. As the I/O pipeline performs the write, |
252 | * it may compress the data before writing it to disk. The ARC will be called | |
253 | * with the transformed data and will bcopy the transformed on-disk block into | |
a6255b7f | 254 | * a newly allocated b_pabd. Writes are always done into buffers which have |
2aa34383 DK |
255 | * either been loaned (and hence are new and don't have other readers) or |
256 | * buffers which have been released (and hence have their own hdr, if there | |
257 | * were originally other readers of the buf's original hdr). This ensures that | |
258 | * the ARC only needs to update a single buf and its hdr after a write occurs. | |
d3c2ae1c | 259 | * |
a6255b7f DQ |
260 | * When the L2ARC is in use, it will also take advantage of the b_pabd. The |
261 | * L2ARC will always write the contents of b_pabd to the L2ARC. This means | |
2aa34383 | 262 | * that when compressed ARC is enabled that the L2ARC blocks are identical |
d3c2ae1c GW |
263 | * to the on-disk block in the main data pool. This provides a significant |
264 | * advantage since the ARC can leverage the bp's checksum when reading from the | |
265 | * L2ARC to determine if the contents are valid. However, if the compressed | |
2aa34383 | 266 | * ARC is disabled, then the L2ARC's block must be transformed to look |
d3c2ae1c GW |
267 | * like the physical block in the main data pool before comparing the |
268 | * checksum and determining its validity. | |
b5256303 TC |
269 | * |
270 | * The L1ARC has a slightly different system for storing encrypted data. | |
271 | * Raw (encrypted + possibly compressed) data has a few subtle differences from | |
272 | * data that is just compressed. The biggest difference is that it is not | |
e1cfd73f | 273 | * possible to decrypt encrypted data (or vice-versa) if the keys aren't loaded. |
b5256303 TC |
274 | * The other difference is that encryption cannot be treated as a suggestion. |
275 | * If a caller would prefer compressed data, but they actually wind up with | |
276 | * uncompressed data the worst thing that could happen is there might be a | |
277 | * performance hit. If the caller requests encrypted data, however, we must be | |
278 | * sure they actually get it or else secret information could be leaked. Raw | |
279 | * data is stored in hdr->b_crypt_hdr.b_rabd. An encrypted header, therefore, | |
280 | * may have both an encrypted version and a decrypted version of its data at | |
281 | * once. When a caller needs a raw arc_buf_t, it is allocated and the data is | |
282 | * copied out of this header. To avoid complications with b_pabd, raw buffers | |
283 | * cannot be shared. | |
d3c2ae1c GW |
284 | */ |
285 | ||
34dc7c2f BB |
286 | #include <sys/spa.h> |
287 | #include <sys/zio.h> | |
d3c2ae1c | 288 | #include <sys/spa_impl.h> |
3a17a7a9 | 289 | #include <sys/zio_compress.h> |
d3c2ae1c | 290 | #include <sys/zio_checksum.h> |
34dc7c2f BB |
291 | #include <sys/zfs_context.h> |
292 | #include <sys/arc.h> | |
27d96d22 | 293 | #include <sys/zfs_refcount.h> |
b128c09f | 294 | #include <sys/vdev.h> |
9babb374 | 295 | #include <sys/vdev_impl.h> |
e8b96c60 | 296 | #include <sys/dsl_pool.h> |
ca0bf58d | 297 | #include <sys/multilist.h> |
a6255b7f | 298 | #include <sys/abd.h> |
b5256303 TC |
299 | #include <sys/zil.h> |
300 | #include <sys/fm/fs/zfs.h> | |
34dc7c2f BB |
301 | #include <sys/callb.h> |
302 | #include <sys/kstat.h> | |
3ec34e55 | 303 | #include <sys/zthr.h> |
428870ff | 304 | #include <zfs_fletcher.h> |
59ec819a | 305 | #include <sys/arc_impl.h> |
e5d1c27e | 306 | #include <sys/trace_zfs.h> |
37fb3e43 | 307 | #include <sys/aggsum.h> |
86706441 | 308 | #include <sys/wmsum.h> |
3f387973 | 309 | #include <cityhash.h> |
b7654bd7 | 310 | #include <sys/vdev_trim.h> |
64e0fe14 | 311 | #include <sys/zfs_racct.h> |
8a171ccd | 312 | #include <sys/zstd/zstd.h> |
34dc7c2f | 313 | |
498877ba MA |
314 | #ifndef _KERNEL |
315 | /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ | |
316 | boolean_t arc_watch = B_FALSE; | |
317 | #endif | |
318 | ||
3ec34e55 BL |
319 | /* |
320 | * This thread's job is to keep enough free memory in the system, by | |
321 | * calling arc_kmem_reap_soon() plus arc_reduce_target_size(), which improves | |
322 | * arc_available_memory(). | |
323 | */ | |
3442c2a0 | 324 | static zthr_t *arc_reap_zthr; |
3ec34e55 BL |
325 | |
326 | /* | |
327 | * This thread's job is to keep arc_size under arc_c, by calling | |
5dd92909 | 328 | * arc_evict(), which improves arc_is_overflowing(). |
3ec34e55 | 329 | */ |
3442c2a0 | 330 | static zthr_t *arc_evict_zthr; |
3ec34e55 | 331 | |
3442c2a0 MA |
332 | static kmutex_t arc_evict_lock; |
333 | static boolean_t arc_evict_needed = B_FALSE; | |
334 | ||
335 | /* | |
336 | * Count of bytes evicted since boot. | |
337 | */ | |
338 | static uint64_t arc_evict_count; | |
339 | ||
340 | /* | |
341 | * List of arc_evict_waiter_t's, representing threads waiting for the | |
342 | * arc_evict_count to reach specific values. | |
343 | */ | |
344 | static list_t arc_evict_waiters; | |
345 | ||
346 | /* | |
347 | * When arc_is_overflowing(), arc_get_data_impl() waits for this percent of | |
348 | * the requested amount of data to be evicted. For example, by default for | |
349 | * every 2KB that's evicted, 1KB of it may be "reused" by a new allocation. | |
350 | * Since this is above 100%, it ensures that progress is made towards getting | |
351 | * arc_size under arc_c. Since this is finite, it ensures that allocations | |
352 | * can still happen, even during the potentially long time that arc_size is | |
353 | * more than arc_c. | |
354 | */ | |
18168da7 | 355 | static int zfs_arc_eviction_pct = 200; |
ca0bf58d | 356 | |
e8b96c60 | 357 | /* |
ca0bf58d PS |
358 | * The number of headers to evict in arc_evict_state_impl() before |
359 | * dropping the sublist lock and evicting from another sublist. A lower | |
360 | * value means we're more likely to evict the "correct" header (i.e. the | |
361 | * oldest header in the arc state), but comes with higher overhead | |
362 | * (i.e. more invocations of arc_evict_state_impl()). | |
363 | */ | |
18168da7 | 364 | static int zfs_arc_evict_batch_limit = 10; |
ca0bf58d | 365 | |
34dc7c2f | 366 | /* number of seconds before growing cache again */ |
c9c9c1e2 | 367 | int arc_grow_retry = 5; |
3ec34e55 BL |
368 | |
369 | /* | |
370 | * Minimum time between calls to arc_kmem_reap_soon(). | |
371 | */ | |
18168da7 | 372 | static const int arc_kmem_cache_reap_retry_ms = 1000; |
34dc7c2f | 373 | |
a6255b7f | 374 | /* shift of arc_c for calculating overflow limit in arc_get_data_impl */ |
18168da7 | 375 | static int zfs_arc_overflow_shift = 8; |
62422785 | 376 | |
728d6ae9 | 377 | /* shift of arc_c for calculating both min and max arc_p */ |
18168da7 | 378 | static int arc_p_min_shift = 4; |
728d6ae9 | 379 | |
d164b209 | 380 | /* log2(fraction of arc to reclaim) */ |
c9c9c1e2 | 381 | int arc_shrink_shift = 7; |
d164b209 | 382 | |
03b60eee DB |
383 | /* percent of pagecache to reclaim arc to */ |
384 | #ifdef _KERNEL | |
c9c9c1e2 | 385 | uint_t zfs_arc_pc_percent = 0; |
03b60eee DB |
386 | #endif |
387 | ||
34dc7c2f | 388 | /* |
ca67b33a MA |
389 | * log2(fraction of ARC which must be free to allow growing). |
390 | * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, | |
391 | * when reading a new block into the ARC, we will evict an equal-sized block | |
392 | * from the ARC. | |
393 | * | |
394 | * This must be less than arc_shrink_shift, so that when we shrink the ARC, | |
395 | * we will still not allow it to grow. | |
34dc7c2f | 396 | */ |
ca67b33a | 397 | int arc_no_grow_shift = 5; |
bce45ec9 | 398 | |
49ddb315 | 399 | |
ca0bf58d PS |
400 | /* |
401 | * minimum lifespan of a prefetch block in clock ticks | |
402 | * (initialized in arc_init()) | |
403 | */ | |
d4a72f23 TC |
404 | static int arc_min_prefetch_ms; |
405 | static int arc_min_prescient_prefetch_ms; | |
ca0bf58d | 406 | |
e8b96c60 MA |
407 | /* |
408 | * If this percent of memory is free, don't throttle. | |
409 | */ | |
410 | int arc_lotsfree_percent = 10; | |
411 | ||
b128c09f BB |
412 | /* |
413 | * The arc has filled available memory and has now warmed up. | |
414 | */ | |
c9c9c1e2 | 415 | boolean_t arc_warm; |
b128c09f | 416 | |
34dc7c2f BB |
417 | /* |
418 | * These tunables are for performance analysis. | |
419 | */ | |
c28b2279 BB |
420 | unsigned long zfs_arc_max = 0; |
421 | unsigned long zfs_arc_min = 0; | |
422 | unsigned long zfs_arc_meta_limit = 0; | |
ca0bf58d | 423 | unsigned long zfs_arc_meta_min = 0; |
18168da7 AZ |
424 | static unsigned long zfs_arc_dnode_limit = 0; |
425 | static unsigned long zfs_arc_dnode_reduce_percent = 10; | |
426 | static int zfs_arc_grow_retry = 0; | |
427 | static int zfs_arc_shrink_shift = 0; | |
428 | static int zfs_arc_p_min_shift = 0; | |
ca67b33a | 429 | int zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ |
34dc7c2f | 430 | |
dae3e9ea | 431 | /* |
18168da7 AZ |
432 | * ARC dirty data constraints for arc_tempreserve_space() throttle: |
433 | * * total dirty data limit | |
434 | * * anon block dirty limit | |
435 | * * each pool's anon allowance | |
dae3e9ea | 436 | */ |
18168da7 AZ |
437 | static const unsigned long zfs_arc_dirty_limit_percent = 50; |
438 | static const unsigned long zfs_arc_anon_limit_percent = 25; | |
439 | static const unsigned long zfs_arc_pool_dirty_percent = 20; | |
dae3e9ea DB |
440 | |
441 | /* | |
442 | * Enable or disable compressed arc buffers. | |
443 | */ | |
d3c2ae1c GW |
444 | int zfs_compressed_arc_enabled = B_TRUE; |
445 | ||
9907cc1c G |
446 | /* |
447 | * ARC will evict meta buffers that exceed arc_meta_limit. This | |
448 | * tunable make arc_meta_limit adjustable for different workloads. | |
449 | */ | |
18168da7 | 450 | static unsigned long zfs_arc_meta_limit_percent = 75; |
9907cc1c G |
451 | |
452 | /* | |
453 | * Percentage that can be consumed by dnodes of ARC meta buffers. | |
454 | */ | |
18168da7 | 455 | static unsigned long zfs_arc_dnode_limit_percent = 10; |
9907cc1c | 456 | |
bc888666 | 457 | /* |
18168da7 | 458 | * These tunables are Linux-specific |
bc888666 | 459 | */ |
18168da7 AZ |
460 | static unsigned long zfs_arc_sys_free = 0; |
461 | static int zfs_arc_min_prefetch_ms = 0; | |
462 | static int zfs_arc_min_prescient_prefetch_ms = 0; | |
463 | static int zfs_arc_p_dampener_disable = 1; | |
464 | static int zfs_arc_meta_prune = 10000; | |
465 | static int zfs_arc_meta_strategy = ARC_STRATEGY_META_BALANCED; | |
466 | static int zfs_arc_meta_adjust_restarts = 4096; | |
467 | static int zfs_arc_lotsfree_percent = 10; | |
bc888666 | 468 | |
462217d1 AM |
469 | /* |
470 | * Number of arc_prune threads | |
471 | */ | |
472 | static int zfs_arc_prune_task_threads = 1; | |
473 | ||
34dc7c2f | 474 | /* The 6 states: */ |
13a4027a MM |
475 | arc_state_t ARC_anon; |
476 | arc_state_t ARC_mru; | |
477 | arc_state_t ARC_mru_ghost; | |
478 | arc_state_t ARC_mfu; | |
479 | arc_state_t ARC_mfu_ghost; | |
480 | arc_state_t ARC_l2c_only; | |
34dc7c2f | 481 | |
c9c9c1e2 | 482 | arc_stats_t arc_stats = { |
34dc7c2f BB |
483 | { "hits", KSTAT_DATA_UINT64 }, |
484 | { "misses", KSTAT_DATA_UINT64 }, | |
485 | { "demand_data_hits", KSTAT_DATA_UINT64 }, | |
486 | { "demand_data_misses", KSTAT_DATA_UINT64 }, | |
487 | { "demand_metadata_hits", KSTAT_DATA_UINT64 }, | |
488 | { "demand_metadata_misses", KSTAT_DATA_UINT64 }, | |
489 | { "prefetch_data_hits", KSTAT_DATA_UINT64 }, | |
490 | { "prefetch_data_misses", KSTAT_DATA_UINT64 }, | |
491 | { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, | |
492 | { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, | |
493 | { "mru_hits", KSTAT_DATA_UINT64 }, | |
494 | { "mru_ghost_hits", KSTAT_DATA_UINT64 }, | |
495 | { "mfu_hits", KSTAT_DATA_UINT64 }, | |
496 | { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, | |
497 | { "deleted", KSTAT_DATA_UINT64 }, | |
34dc7c2f | 498 | { "mutex_miss", KSTAT_DATA_UINT64 }, |
0873bb63 | 499 | { "access_skip", KSTAT_DATA_UINT64 }, |
34dc7c2f | 500 | { "evict_skip", KSTAT_DATA_UINT64 }, |
ca0bf58d | 501 | { "evict_not_enough", KSTAT_DATA_UINT64 }, |
428870ff BB |
502 | { "evict_l2_cached", KSTAT_DATA_UINT64 }, |
503 | { "evict_l2_eligible", KSTAT_DATA_UINT64 }, | |
08532162 GA |
504 | { "evict_l2_eligible_mfu", KSTAT_DATA_UINT64 }, |
505 | { "evict_l2_eligible_mru", KSTAT_DATA_UINT64 }, | |
428870ff | 506 | { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, |
ca0bf58d | 507 | { "evict_l2_skip", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
508 | { "hash_elements", KSTAT_DATA_UINT64 }, |
509 | { "hash_elements_max", KSTAT_DATA_UINT64 }, | |
510 | { "hash_collisions", KSTAT_DATA_UINT64 }, | |
511 | { "hash_chains", KSTAT_DATA_UINT64 }, | |
512 | { "hash_chain_max", KSTAT_DATA_UINT64 }, | |
513 | { "p", KSTAT_DATA_UINT64 }, | |
514 | { "c", KSTAT_DATA_UINT64 }, | |
515 | { "c_min", KSTAT_DATA_UINT64 }, | |
516 | { "c_max", KSTAT_DATA_UINT64 }, | |
517 | { "size", KSTAT_DATA_UINT64 }, | |
d3c2ae1c GW |
518 | { "compressed_size", KSTAT_DATA_UINT64 }, |
519 | { "uncompressed_size", KSTAT_DATA_UINT64 }, | |
520 | { "overhead_size", KSTAT_DATA_UINT64 }, | |
34dc7c2f | 521 | { "hdr_size", KSTAT_DATA_UINT64 }, |
d164b209 | 522 | { "data_size", KSTAT_DATA_UINT64 }, |
500445c0 | 523 | { "metadata_size", KSTAT_DATA_UINT64 }, |
25458cbe TC |
524 | { "dbuf_size", KSTAT_DATA_UINT64 }, |
525 | { "dnode_size", KSTAT_DATA_UINT64 }, | |
526 | { "bonus_size", KSTAT_DATA_UINT64 }, | |
1c2725a1 MM |
527 | #if defined(COMPAT_FREEBSD11) |
528 | { "other_size", KSTAT_DATA_UINT64 }, | |
529 | #endif | |
13be560d | 530 | { "anon_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
531 | { "anon_evictable_data", KSTAT_DATA_UINT64 }, |
532 | { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 533 | { "mru_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
534 | { "mru_evictable_data", KSTAT_DATA_UINT64 }, |
535 | { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 536 | { "mru_ghost_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
537 | { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, |
538 | { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 539 | { "mfu_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
540 | { "mfu_evictable_data", KSTAT_DATA_UINT64 }, |
541 | { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 542 | { "mfu_ghost_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
543 | { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, |
544 | { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
545 | { "l2_hits", KSTAT_DATA_UINT64 }, |
546 | { "l2_misses", KSTAT_DATA_UINT64 }, | |
08532162 GA |
547 | { "l2_prefetch_asize", KSTAT_DATA_UINT64 }, |
548 | { "l2_mru_asize", KSTAT_DATA_UINT64 }, | |
549 | { "l2_mfu_asize", KSTAT_DATA_UINT64 }, | |
550 | { "l2_bufc_data_asize", KSTAT_DATA_UINT64 }, | |
551 | { "l2_bufc_metadata_asize", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
552 | { "l2_feeds", KSTAT_DATA_UINT64 }, |
553 | { "l2_rw_clash", KSTAT_DATA_UINT64 }, | |
d164b209 BB |
554 | { "l2_read_bytes", KSTAT_DATA_UINT64 }, |
555 | { "l2_write_bytes", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
556 | { "l2_writes_sent", KSTAT_DATA_UINT64 }, |
557 | { "l2_writes_done", KSTAT_DATA_UINT64 }, | |
558 | { "l2_writes_error", KSTAT_DATA_UINT64 }, | |
ca0bf58d | 559 | { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
560 | { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, |
561 | { "l2_evict_reading", KSTAT_DATA_UINT64 }, | |
b9541d6b | 562 | { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
563 | { "l2_free_on_write", KSTAT_DATA_UINT64 }, |
564 | { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, | |
565 | { "l2_cksum_bad", KSTAT_DATA_UINT64 }, | |
566 | { "l2_io_error", KSTAT_DATA_UINT64 }, | |
567 | { "l2_size", KSTAT_DATA_UINT64 }, | |
3a17a7a9 | 568 | { "l2_asize", KSTAT_DATA_UINT64 }, |
34dc7c2f | 569 | { "l2_hdr_size", KSTAT_DATA_UINT64 }, |
77f6826b | 570 | { "l2_log_blk_writes", KSTAT_DATA_UINT64 }, |
657fd33b GA |
571 | { "l2_log_blk_avg_asize", KSTAT_DATA_UINT64 }, |
572 | { "l2_log_blk_asize", KSTAT_DATA_UINT64 }, | |
573 | { "l2_log_blk_count", KSTAT_DATA_UINT64 }, | |
77f6826b GA |
574 | { "l2_data_to_meta_ratio", KSTAT_DATA_UINT64 }, |
575 | { "l2_rebuild_success", KSTAT_DATA_UINT64 }, | |
576 | { "l2_rebuild_unsupported", KSTAT_DATA_UINT64 }, | |
577 | { "l2_rebuild_io_errors", KSTAT_DATA_UINT64 }, | |
578 | { "l2_rebuild_dh_errors", KSTAT_DATA_UINT64 }, | |
579 | { "l2_rebuild_cksum_lb_errors", KSTAT_DATA_UINT64 }, | |
580 | { "l2_rebuild_lowmem", KSTAT_DATA_UINT64 }, | |
581 | { "l2_rebuild_size", KSTAT_DATA_UINT64 }, | |
657fd33b | 582 | { "l2_rebuild_asize", KSTAT_DATA_UINT64 }, |
77f6826b GA |
583 | { "l2_rebuild_bufs", KSTAT_DATA_UINT64 }, |
584 | { "l2_rebuild_bufs_precached", KSTAT_DATA_UINT64 }, | |
77f6826b | 585 | { "l2_rebuild_log_blks", KSTAT_DATA_UINT64 }, |
1834f2d8 | 586 | { "memory_throttle_count", KSTAT_DATA_UINT64 }, |
7cb67b45 BB |
587 | { "memory_direct_count", KSTAT_DATA_UINT64 }, |
588 | { "memory_indirect_count", KSTAT_DATA_UINT64 }, | |
70f02287 BB |
589 | { "memory_all_bytes", KSTAT_DATA_UINT64 }, |
590 | { "memory_free_bytes", KSTAT_DATA_UINT64 }, | |
591 | { "memory_available_bytes", KSTAT_DATA_INT64 }, | |
1834f2d8 BB |
592 | { "arc_no_grow", KSTAT_DATA_UINT64 }, |
593 | { "arc_tempreserve", KSTAT_DATA_UINT64 }, | |
594 | { "arc_loaned_bytes", KSTAT_DATA_UINT64 }, | |
ab26409d | 595 | { "arc_prune", KSTAT_DATA_UINT64 }, |
1834f2d8 BB |
596 | { "arc_meta_used", KSTAT_DATA_UINT64 }, |
597 | { "arc_meta_limit", KSTAT_DATA_UINT64 }, | |
25458cbe | 598 | { "arc_dnode_limit", KSTAT_DATA_UINT64 }, |
1834f2d8 | 599 | { "arc_meta_max", KSTAT_DATA_UINT64 }, |
11f552fa | 600 | { "arc_meta_min", KSTAT_DATA_UINT64 }, |
a8b2e306 | 601 | { "async_upgrade_sync", KSTAT_DATA_UINT64 }, |
7f60329a | 602 | { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 }, |
d4a72f23 | 603 | { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64 }, |
11f552fa | 604 | { "arc_need_free", KSTAT_DATA_UINT64 }, |
b5256303 | 605 | { "arc_sys_free", KSTAT_DATA_UINT64 }, |
1dc32a67 MA |
606 | { "arc_raw_size", KSTAT_DATA_UINT64 }, |
607 | { "cached_only_in_progress", KSTAT_DATA_UINT64 }, | |
85ec5cba | 608 | { "abd_chunk_waste_size", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
609 | }; |
610 | ||
c4c162c1 AM |
611 | arc_sums_t arc_sums; |
612 | ||
34dc7c2f BB |
613 | #define ARCSTAT_MAX(stat, val) { \ |
614 | uint64_t m; \ | |
615 | while ((val) > (m = arc_stats.stat.value.ui64) && \ | |
616 | (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ | |
617 | continue; \ | |
618 | } | |
619 | ||
34dc7c2f BB |
620 | /* |
621 | * We define a macro to allow ARC hits/misses to be easily broken down by | |
622 | * two separate conditions, giving a total of four different subtypes for | |
623 | * each of hits and misses (so eight statistics total). | |
624 | */ | |
625 | #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ | |
626 | if (cond1) { \ | |
627 | if (cond2) { \ | |
628 | ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ | |
629 | } else { \ | |
630 | ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ | |
631 | } \ | |
632 | } else { \ | |
633 | if (cond2) { \ | |
634 | ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ | |
635 | } else { \ | |
636 | ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ | |
637 | } \ | |
638 | } | |
639 | ||
77f6826b GA |
640 | /* |
641 | * This macro allows us to use kstats as floating averages. Each time we | |
642 | * update this kstat, we first factor it and the update value by | |
643 | * ARCSTAT_AVG_FACTOR to shrink the new value's contribution to the overall | |
644 | * average. This macro assumes that integer loads and stores are atomic, but | |
645 | * is not safe for multiple writers updating the kstat in parallel (only the | |
646 | * last writer's update will remain). | |
647 | */ | |
648 | #define ARCSTAT_F_AVG_FACTOR 3 | |
649 | #define ARCSTAT_F_AVG(stat, value) \ | |
650 | do { \ | |
651 | uint64_t x = ARCSTAT(stat); \ | |
652 | x = x - x / ARCSTAT_F_AVG_FACTOR + \ | |
653 | (value) / ARCSTAT_F_AVG_FACTOR; \ | |
654 | ARCSTAT(stat) = x; \ | |
77f6826b GA |
655 | } while (0) |
656 | ||
18168da7 | 657 | static kstat_t *arc_ksp; |
c9c9c1e2 | 658 | |
34dc7c2f BB |
659 | /* |
660 | * There are several ARC variables that are critical to export as kstats -- | |
661 | * but we don't want to have to grovel around in the kstat whenever we wish to | |
662 | * manipulate them. For these variables, we therefore define them to be in | |
663 | * terms of the statistic variable. This assures that we are not introducing | |
664 | * the possibility of inconsistency by having shadow copies of the variables, | |
665 | * while still allowing the code to be readable. | |
666 | */ | |
1834f2d8 BB |
667 | #define arc_tempreserve ARCSTAT(arcstat_tempreserve) |
668 | #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes) | |
23c0a133 | 669 | #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ |
03fdcb9a MM |
670 | /* max size for dnodes */ |
671 | #define arc_dnode_size_limit ARCSTAT(arcstat_dnode_limit) | |
ca0bf58d | 672 | #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ |
3442c2a0 | 673 | #define arc_need_free ARCSTAT(arcstat_need_free) /* waiting to be evicted */ |
34dc7c2f | 674 | |
c9c9c1e2 MM |
675 | hrtime_t arc_growtime; |
676 | list_t arc_prune_list; | |
677 | kmutex_t arc_prune_mtx; | |
678 | taskq_t *arc_prune_taskq; | |
428870ff | 679 | |
34dc7c2f BB |
680 | #define GHOST_STATE(state) \ |
681 | ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ | |
682 | (state) == arc_l2c_only) | |
683 | ||
2a432414 GW |
684 | #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) |
685 | #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) | |
686 | #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) | |
687 | #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) | |
d4a72f23 TC |
688 | #define HDR_PRESCIENT_PREFETCH(hdr) \ |
689 | ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH) | |
d3c2ae1c GW |
690 | #define HDR_COMPRESSION_ENABLED(hdr) \ |
691 | ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC) | |
b9541d6b | 692 | |
2a432414 GW |
693 | #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) |
694 | #define HDR_L2_READING(hdr) \ | |
d3c2ae1c GW |
695 | (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ |
696 | ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) | |
2a432414 GW |
697 | #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) |
698 | #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) | |
699 | #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) | |
b5256303 TC |
700 | #define HDR_PROTECTED(hdr) ((hdr)->b_flags & ARC_FLAG_PROTECTED) |
701 | #define HDR_NOAUTH(hdr) ((hdr)->b_flags & ARC_FLAG_NOAUTH) | |
d3c2ae1c | 702 | #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA) |
34dc7c2f | 703 | |
b9541d6b | 704 | #define HDR_ISTYPE_METADATA(hdr) \ |
d3c2ae1c | 705 | ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) |
b9541d6b CW |
706 | #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) |
707 | ||
708 | #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) | |
709 | #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) | |
b5256303 TC |
710 | #define HDR_HAS_RABD(hdr) \ |
711 | (HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) && \ | |
712 | (hdr)->b_crypt_hdr.b_rabd != NULL) | |
713 | #define HDR_ENCRYPTED(hdr) \ | |
714 | (HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot)) | |
715 | #define HDR_AUTHENTICATED(hdr) \ | |
716 | (HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot)) | |
b9541d6b | 717 | |
d3c2ae1c GW |
718 | /* For storing compression mode in b_flags */ |
719 | #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1) | |
720 | ||
721 | #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \ | |
722 | HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS)) | |
723 | #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \ | |
724 | HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp)); | |
725 | ||
726 | #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL) | |
524b4217 DK |
727 | #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED) |
728 | #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED) | |
b5256303 | 729 | #define ARC_BUF_ENCRYPTED(buf) ((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED) |
d3c2ae1c | 730 | |
34dc7c2f BB |
731 | /* |
732 | * Other sizes | |
733 | */ | |
734 | ||
b5256303 TC |
735 | #define HDR_FULL_CRYPT_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) |
736 | #define HDR_FULL_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_crypt_hdr)) | |
b9541d6b | 737 | #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) |
34dc7c2f BB |
738 | |
739 | /* | |
740 | * Hash table routines | |
741 | */ | |
742 | ||
490c845e | 743 | #define BUF_LOCKS 2048 |
34dc7c2f BB |
744 | typedef struct buf_hash_table { |
745 | uint64_t ht_mask; | |
746 | arc_buf_hdr_t **ht_table; | |
490c845e | 747 | kmutex_t ht_locks[BUF_LOCKS] ____cacheline_aligned; |
34dc7c2f BB |
748 | } buf_hash_table_t; |
749 | ||
750 | static buf_hash_table_t buf_hash_table; | |
751 | ||
752 | #define BUF_HASH_INDEX(spa, dva, birth) \ | |
753 | (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) | |
490c845e | 754 | #define BUF_HASH_LOCK(idx) (&buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) |
428870ff BB |
755 | #define HDR_LOCK(hdr) \ |
756 | (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) | |
34dc7c2f BB |
757 | |
758 | uint64_t zfs_crc64_table[256]; | |
759 | ||
760 | /* | |
761 | * Level 2 ARC | |
762 | */ | |
763 | ||
764 | #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ | |
3a17a7a9 | 765 | #define L2ARC_HEADROOM 2 /* num of writes */ |
8a09d5fd | 766 | |
3a17a7a9 SK |
767 | /* |
768 | * If we discover during ARC scan any buffers to be compressed, we boost | |
769 | * our headroom for the next scanning cycle by this percentage multiple. | |
770 | */ | |
771 | #define L2ARC_HEADROOM_BOOST 200 | |
d164b209 BB |
772 | #define L2ARC_FEED_SECS 1 /* caching interval secs */ |
773 | #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ | |
34dc7c2f | 774 | |
4aafab91 G |
775 | /* |
776 | * We can feed L2ARC from two states of ARC buffers, mru and mfu, | |
777 | * and each of the state has two types: data and metadata. | |
778 | */ | |
779 | #define L2ARC_FEED_TYPES 4 | |
780 | ||
d3cc8b15 | 781 | /* L2ARC Performance Tunables */ |
abd8610c BB |
782 | unsigned long l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */ |
783 | unsigned long l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */ | |
784 | unsigned long l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */ | |
3a17a7a9 | 785 | unsigned long l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; |
abd8610c BB |
786 | unsigned long l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ |
787 | unsigned long l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */ | |
788 | int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ | |
789 | int l2arc_feed_again = B_TRUE; /* turbo warmup */ | |
c93504f0 | 790 | int l2arc_norw = B_FALSE; /* no reads during writes */ |
18168da7 | 791 | static int l2arc_meta_percent = 33; /* limit on headers size */ |
34dc7c2f BB |
792 | |
793 | /* | |
794 | * L2ARC Internals | |
795 | */ | |
34dc7c2f BB |
796 | static list_t L2ARC_dev_list; /* device list */ |
797 | static list_t *l2arc_dev_list; /* device list pointer */ | |
798 | static kmutex_t l2arc_dev_mtx; /* device list mutex */ | |
799 | static l2arc_dev_t *l2arc_dev_last; /* last device used */ | |
34dc7c2f BB |
800 | static list_t L2ARC_free_on_write; /* free after write buf list */ |
801 | static list_t *l2arc_free_on_write; /* free after write list ptr */ | |
802 | static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ | |
803 | static uint64_t l2arc_ndev; /* number of devices */ | |
804 | ||
805 | typedef struct l2arc_read_callback { | |
2aa34383 | 806 | arc_buf_hdr_t *l2rcb_hdr; /* read header */ |
3a17a7a9 | 807 | blkptr_t l2rcb_bp; /* original blkptr */ |
5dbd68a3 | 808 | zbookmark_phys_t l2rcb_zb; /* original bookmark */ |
3a17a7a9 | 809 | int l2rcb_flags; /* original flags */ |
82710e99 | 810 | abd_t *l2rcb_abd; /* temporary buffer */ |
34dc7c2f BB |
811 | } l2arc_read_callback_t; |
812 | ||
34dc7c2f BB |
813 | typedef struct l2arc_data_free { |
814 | /* protected by l2arc_free_on_write_mtx */ | |
a6255b7f | 815 | abd_t *l2df_abd; |
34dc7c2f | 816 | size_t l2df_size; |
d3c2ae1c | 817 | arc_buf_contents_t l2df_type; |
34dc7c2f BB |
818 | list_node_t l2df_list_node; |
819 | } l2arc_data_free_t; | |
820 | ||
b5256303 TC |
821 | typedef enum arc_fill_flags { |
822 | ARC_FILL_LOCKED = 1 << 0, /* hdr lock is held */ | |
823 | ARC_FILL_COMPRESSED = 1 << 1, /* fill with compressed data */ | |
824 | ARC_FILL_ENCRYPTED = 1 << 2, /* fill with encrypted data */ | |
825 | ARC_FILL_NOAUTH = 1 << 3, /* don't attempt to authenticate */ | |
826 | ARC_FILL_IN_PLACE = 1 << 4 /* fill in place (special case) */ | |
827 | } arc_fill_flags_t; | |
828 | ||
f7de776d AM |
829 | typedef enum arc_ovf_level { |
830 | ARC_OVF_NONE, /* ARC within target size. */ | |
831 | ARC_OVF_SOME, /* ARC is slightly overflowed. */ | |
832 | ARC_OVF_SEVERE /* ARC is severely overflowed. */ | |
833 | } arc_ovf_level_t; | |
834 | ||
34dc7c2f BB |
835 | static kmutex_t l2arc_feed_thr_lock; |
836 | static kcondvar_t l2arc_feed_thr_cv; | |
837 | static uint8_t l2arc_thread_exit; | |
838 | ||
77f6826b GA |
839 | static kmutex_t l2arc_rebuild_thr_lock; |
840 | static kcondvar_t l2arc_rebuild_thr_cv; | |
841 | ||
e111c802 MM |
842 | enum arc_hdr_alloc_flags { |
843 | ARC_HDR_ALLOC_RDATA = 0x1, | |
844 | ARC_HDR_DO_ADAPT = 0x2, | |
6b88b4b5 | 845 | ARC_HDR_USE_RESERVE = 0x4, |
e111c802 MM |
846 | }; |
847 | ||
848 | ||
6b88b4b5 | 849 | static abd_t *arc_get_data_abd(arc_buf_hdr_t *, uint64_t, void *, int); |
d3c2ae1c | 850 | static void *arc_get_data_buf(arc_buf_hdr_t *, uint64_t, void *); |
6b88b4b5 | 851 | static void arc_get_data_impl(arc_buf_hdr_t *, uint64_t, void *, int); |
a6255b7f | 852 | static void arc_free_data_abd(arc_buf_hdr_t *, abd_t *, uint64_t, void *); |
d3c2ae1c | 853 | static void arc_free_data_buf(arc_buf_hdr_t *, void *, uint64_t, void *); |
a6255b7f | 854 | static void arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag); |
b5256303 | 855 | static void arc_hdr_free_abd(arc_buf_hdr_t *, boolean_t); |
e111c802 | 856 | static void arc_hdr_alloc_abd(arc_buf_hdr_t *, int); |
2a432414 | 857 | static void arc_access(arc_buf_hdr_t *, kmutex_t *); |
2a432414 GW |
858 | static void arc_buf_watch(arc_buf_t *); |
859 | ||
b9541d6b CW |
860 | static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); |
861 | static uint32_t arc_bufc_to_flags(arc_buf_contents_t); | |
d3c2ae1c GW |
862 | static inline void arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); |
863 | static inline void arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); | |
b9541d6b | 864 | |
2a432414 GW |
865 | static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); |
866 | static void l2arc_read_done(zio_t *); | |
cfd59f90 | 867 | static void l2arc_do_free_on_write(void); |
08532162 GA |
868 | static void l2arc_hdr_arcstats_update(arc_buf_hdr_t *hdr, boolean_t incr, |
869 | boolean_t state_only); | |
870 | ||
871 | #define l2arc_hdr_arcstats_increment(hdr) \ | |
872 | l2arc_hdr_arcstats_update((hdr), B_TRUE, B_FALSE) | |
873 | #define l2arc_hdr_arcstats_decrement(hdr) \ | |
874 | l2arc_hdr_arcstats_update((hdr), B_FALSE, B_FALSE) | |
875 | #define l2arc_hdr_arcstats_increment_state(hdr) \ | |
876 | l2arc_hdr_arcstats_update((hdr), B_TRUE, B_TRUE) | |
877 | #define l2arc_hdr_arcstats_decrement_state(hdr) \ | |
878 | l2arc_hdr_arcstats_update((hdr), B_FALSE, B_TRUE) | |
34dc7c2f | 879 | |
c9d62d13 GA |
880 | /* |
881 | * l2arc_exclude_special : A zfs module parameter that controls whether buffers | |
882 | * present on special vdevs are eligibile for caching in L2ARC. If | |
883 | * set to 1, exclude dbufs on special vdevs from being cached to | |
884 | * L2ARC. | |
885 | */ | |
886 | int l2arc_exclude_special = 0; | |
887 | ||
feb3a7ee GA |
888 | /* |
889 | * l2arc_mfuonly : A ZFS module parameter that controls whether only MFU | |
890 | * metadata and data are cached from ARC into L2ARC. | |
891 | */ | |
18168da7 | 892 | static int l2arc_mfuonly = 0; |
feb3a7ee | 893 | |
b7654bd7 GA |
894 | /* |
895 | * L2ARC TRIM | |
896 | * l2arc_trim_ahead : A ZFS module parameter that controls how much ahead of | |
897 | * the current write size (l2arc_write_max) we should TRIM if we | |
898 | * have filled the device. It is defined as a percentage of the | |
899 | * write size. If set to 100 we trim twice the space required to | |
900 | * accommodate upcoming writes. A minimum of 64MB will be trimmed. | |
901 | * It also enables TRIM of the whole L2ARC device upon creation or | |
902 | * addition to an existing pool or if the header of the device is | |
903 | * invalid upon importing a pool or onlining a cache device. The | |
904 | * default is 0, which disables TRIM on L2ARC altogether as it can | |
905 | * put significant stress on the underlying storage devices. This | |
906 | * will vary depending of how well the specific device handles | |
907 | * these commands. | |
908 | */ | |
18168da7 | 909 | static unsigned long l2arc_trim_ahead = 0; |
b7654bd7 | 910 | |
77f6826b GA |
911 | /* |
912 | * Performance tuning of L2ARC persistence: | |
913 | * | |
914 | * l2arc_rebuild_enabled : A ZFS module parameter that controls whether adding | |
915 | * an L2ARC device (either at pool import or later) will attempt | |
916 | * to rebuild L2ARC buffer contents. | |
917 | * l2arc_rebuild_blocks_min_l2size : A ZFS module parameter that controls | |
918 | * whether log blocks are written to the L2ARC device. If the L2ARC | |
919 | * device is less than 1GB, the amount of data l2arc_evict() | |
920 | * evicts is significant compared to the amount of restored L2ARC | |
921 | * data. In this case do not write log blocks in L2ARC in order | |
922 | * not to waste space. | |
923 | */ | |
18168da7 AZ |
924 | static int l2arc_rebuild_enabled = B_TRUE; |
925 | static unsigned long l2arc_rebuild_blocks_min_l2size = 1024 * 1024 * 1024; | |
77f6826b GA |
926 | |
927 | /* L2ARC persistence rebuild control routines. */ | |
928 | void l2arc_rebuild_vdev(vdev_t *vd, boolean_t reopen); | |
3eaf76a8 | 929 | static void l2arc_dev_rebuild_thread(void *arg); |
77f6826b GA |
930 | static int l2arc_rebuild(l2arc_dev_t *dev); |
931 | ||
932 | /* L2ARC persistence read I/O routines. */ | |
933 | static int l2arc_dev_hdr_read(l2arc_dev_t *dev); | |
934 | static int l2arc_log_blk_read(l2arc_dev_t *dev, | |
935 | const l2arc_log_blkptr_t *this_lp, const l2arc_log_blkptr_t *next_lp, | |
936 | l2arc_log_blk_phys_t *this_lb, l2arc_log_blk_phys_t *next_lb, | |
937 | zio_t *this_io, zio_t **next_io); | |
938 | static zio_t *l2arc_log_blk_fetch(vdev_t *vd, | |
939 | const l2arc_log_blkptr_t *lp, l2arc_log_blk_phys_t *lb); | |
940 | static void l2arc_log_blk_fetch_abort(zio_t *zio); | |
941 | ||
942 | /* L2ARC persistence block restoration routines. */ | |
943 | static void l2arc_log_blk_restore(l2arc_dev_t *dev, | |
a76e4e67 | 944 | const l2arc_log_blk_phys_t *lb, uint64_t lb_asize); |
77f6826b GA |
945 | static void l2arc_hdr_restore(const l2arc_log_ent_phys_t *le, |
946 | l2arc_dev_t *dev); | |
947 | ||
948 | /* L2ARC persistence write I/O routines. */ | |
77f6826b GA |
949 | static void l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio, |
950 | l2arc_write_callback_t *cb); | |
951 | ||
dd4bc569 | 952 | /* L2ARC persistence auxiliary routines. */ |
77f6826b GA |
953 | boolean_t l2arc_log_blkptr_valid(l2arc_dev_t *dev, |
954 | const l2arc_log_blkptr_t *lbp); | |
955 | static boolean_t l2arc_log_blk_insert(l2arc_dev_t *dev, | |
956 | const arc_buf_hdr_t *ab); | |
957 | boolean_t l2arc_range_check_overlap(uint64_t bottom, | |
958 | uint64_t top, uint64_t check); | |
959 | static void l2arc_blk_fetch_done(zio_t *zio); | |
960 | static inline uint64_t | |
961 | l2arc_log_blk_overhead(uint64_t write_sz, l2arc_dev_t *dev); | |
37fb3e43 PD |
962 | |
963 | /* | |
964 | * We use Cityhash for this. It's fast, and has good hash properties without | |
965 | * requiring any large static buffers. | |
966 | */ | |
34dc7c2f | 967 | static uint64_t |
d164b209 | 968 | buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) |
34dc7c2f | 969 | { |
37fb3e43 | 970 | return (cityhash4(spa, dva->dva_word[0], dva->dva_word[1], birth)); |
34dc7c2f BB |
971 | } |
972 | ||
d3c2ae1c GW |
973 | #define HDR_EMPTY(hdr) \ |
974 | ((hdr)->b_dva.dva_word[0] == 0 && \ | |
975 | (hdr)->b_dva.dva_word[1] == 0) | |
34dc7c2f | 976 | |
ca6c7a94 BB |
977 | #define HDR_EMPTY_OR_LOCKED(hdr) \ |
978 | (HDR_EMPTY(hdr) || MUTEX_HELD(HDR_LOCK(hdr))) | |
979 | ||
d3c2ae1c GW |
980 | #define HDR_EQUAL(spa, dva, birth, hdr) \ |
981 | ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ | |
982 | ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ | |
983 | ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa) | |
34dc7c2f | 984 | |
428870ff BB |
985 | static void |
986 | buf_discard_identity(arc_buf_hdr_t *hdr) | |
987 | { | |
988 | hdr->b_dva.dva_word[0] = 0; | |
989 | hdr->b_dva.dva_word[1] = 0; | |
990 | hdr->b_birth = 0; | |
428870ff BB |
991 | } |
992 | ||
34dc7c2f | 993 | static arc_buf_hdr_t * |
9b67f605 | 994 | buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) |
34dc7c2f | 995 | { |
9b67f605 MA |
996 | const dva_t *dva = BP_IDENTITY(bp); |
997 | uint64_t birth = BP_PHYSICAL_BIRTH(bp); | |
34dc7c2f BB |
998 | uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); |
999 | kmutex_t *hash_lock = BUF_HASH_LOCK(idx); | |
2a432414 | 1000 | arc_buf_hdr_t *hdr; |
34dc7c2f BB |
1001 | |
1002 | mutex_enter(hash_lock); | |
2a432414 GW |
1003 | for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; |
1004 | hdr = hdr->b_hash_next) { | |
d3c2ae1c | 1005 | if (HDR_EQUAL(spa, dva, birth, hdr)) { |
34dc7c2f | 1006 | *lockp = hash_lock; |
2a432414 | 1007 | return (hdr); |
34dc7c2f BB |
1008 | } |
1009 | } | |
1010 | mutex_exit(hash_lock); | |
1011 | *lockp = NULL; | |
1012 | return (NULL); | |
1013 | } | |
1014 | ||
1015 | /* | |
1016 | * Insert an entry into the hash table. If there is already an element | |
1017 | * equal to elem in the hash table, then the already existing element | |
1018 | * will be returned and the new element will not be inserted. | |
1019 | * Otherwise returns NULL. | |
b9541d6b | 1020 | * If lockp == NULL, the caller is assumed to already hold the hash lock. |
34dc7c2f BB |
1021 | */ |
1022 | static arc_buf_hdr_t * | |
2a432414 | 1023 | buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) |
34dc7c2f | 1024 | { |
2a432414 | 1025 | uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); |
34dc7c2f | 1026 | kmutex_t *hash_lock = BUF_HASH_LOCK(idx); |
2a432414 | 1027 | arc_buf_hdr_t *fhdr; |
34dc7c2f BB |
1028 | uint32_t i; |
1029 | ||
2a432414 GW |
1030 | ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); |
1031 | ASSERT(hdr->b_birth != 0); | |
1032 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); | |
b9541d6b CW |
1033 | |
1034 | if (lockp != NULL) { | |
1035 | *lockp = hash_lock; | |
1036 | mutex_enter(hash_lock); | |
1037 | } else { | |
1038 | ASSERT(MUTEX_HELD(hash_lock)); | |
1039 | } | |
1040 | ||
2a432414 GW |
1041 | for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; |
1042 | fhdr = fhdr->b_hash_next, i++) { | |
d3c2ae1c | 1043 | if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) |
2a432414 | 1044 | return (fhdr); |
34dc7c2f BB |
1045 | } |
1046 | ||
2a432414 GW |
1047 | hdr->b_hash_next = buf_hash_table.ht_table[idx]; |
1048 | buf_hash_table.ht_table[idx] = hdr; | |
d3c2ae1c | 1049 | arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
34dc7c2f BB |
1050 | |
1051 | /* collect some hash table performance data */ | |
1052 | if (i > 0) { | |
1053 | ARCSTAT_BUMP(arcstat_hash_collisions); | |
1054 | if (i == 1) | |
1055 | ARCSTAT_BUMP(arcstat_hash_chains); | |
1056 | ||
1057 | ARCSTAT_MAX(arcstat_hash_chain_max, i); | |
1058 | } | |
c4c162c1 AM |
1059 | uint64_t he = atomic_inc_64_nv( |
1060 | &arc_stats.arcstat_hash_elements.value.ui64); | |
1061 | ARCSTAT_MAX(arcstat_hash_elements_max, he); | |
34dc7c2f BB |
1062 | |
1063 | return (NULL); | |
1064 | } | |
1065 | ||
1066 | static void | |
2a432414 | 1067 | buf_hash_remove(arc_buf_hdr_t *hdr) |
34dc7c2f | 1068 | { |
2a432414 GW |
1069 | arc_buf_hdr_t *fhdr, **hdrp; |
1070 | uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); | |
34dc7c2f BB |
1071 | |
1072 | ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); | |
2a432414 | 1073 | ASSERT(HDR_IN_HASH_TABLE(hdr)); |
34dc7c2f | 1074 | |
2a432414 GW |
1075 | hdrp = &buf_hash_table.ht_table[idx]; |
1076 | while ((fhdr = *hdrp) != hdr) { | |
d3c2ae1c | 1077 | ASSERT3P(fhdr, !=, NULL); |
2a432414 | 1078 | hdrp = &fhdr->b_hash_next; |
34dc7c2f | 1079 | } |
2a432414 GW |
1080 | *hdrp = hdr->b_hash_next; |
1081 | hdr->b_hash_next = NULL; | |
d3c2ae1c | 1082 | arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
34dc7c2f BB |
1083 | |
1084 | /* collect some hash table performance data */ | |
c4c162c1 | 1085 | atomic_dec_64(&arc_stats.arcstat_hash_elements.value.ui64); |
34dc7c2f BB |
1086 | |
1087 | if (buf_hash_table.ht_table[idx] && | |
1088 | buf_hash_table.ht_table[idx]->b_hash_next == NULL) | |
1089 | ARCSTAT_BUMPDOWN(arcstat_hash_chains); | |
1090 | } | |
1091 | ||
1092 | /* | |
1093 | * Global data structures and functions for the buf kmem cache. | |
1094 | */ | |
b5256303 | 1095 | |
b9541d6b | 1096 | static kmem_cache_t *hdr_full_cache; |
b5256303 | 1097 | static kmem_cache_t *hdr_full_crypt_cache; |
b9541d6b | 1098 | static kmem_cache_t *hdr_l2only_cache; |
34dc7c2f BB |
1099 | static kmem_cache_t *buf_cache; |
1100 | ||
1101 | static void | |
1102 | buf_fini(void) | |
1103 | { | |
93ce2b4c | 1104 | #if defined(_KERNEL) |
d1d7e268 MK |
1105 | /* |
1106 | * Large allocations which do not require contiguous pages | |
1107 | * should be using vmem_free() in the linux kernel\ | |
1108 | */ | |
00b46022 BB |
1109 | vmem_free(buf_hash_table.ht_table, |
1110 | (buf_hash_table.ht_mask + 1) * sizeof (void *)); | |
1111 | #else | |
34dc7c2f BB |
1112 | kmem_free(buf_hash_table.ht_table, |
1113 | (buf_hash_table.ht_mask + 1) * sizeof (void *)); | |
00b46022 | 1114 | #endif |
14e4e3cb | 1115 | for (int i = 0; i < BUF_LOCKS; i++) |
490c845e | 1116 | mutex_destroy(BUF_HASH_LOCK(i)); |
b9541d6b | 1117 | kmem_cache_destroy(hdr_full_cache); |
b5256303 | 1118 | kmem_cache_destroy(hdr_full_crypt_cache); |
b9541d6b | 1119 | kmem_cache_destroy(hdr_l2only_cache); |
34dc7c2f BB |
1120 | kmem_cache_destroy(buf_cache); |
1121 | } | |
1122 | ||
1123 | /* | |
1124 | * Constructor callback - called when the cache is empty | |
1125 | * and a new buf is requested. | |
1126 | */ | |
34dc7c2f | 1127 | static int |
b9541d6b CW |
1128 | hdr_full_cons(void *vbuf, void *unused, int kmflag) |
1129 | { | |
14e4e3cb | 1130 | (void) unused, (void) kmflag; |
b9541d6b CW |
1131 | arc_buf_hdr_t *hdr = vbuf; |
1132 | ||
1133 | bzero(hdr, HDR_FULL_SIZE); | |
ae76f45c | 1134 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; |
b9541d6b | 1135 | cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); |
424fd7c3 | 1136 | zfs_refcount_create(&hdr->b_l1hdr.b_refcnt); |
b9541d6b CW |
1137 | mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); |
1138 | list_link_init(&hdr->b_l1hdr.b_arc_node); | |
1139 | list_link_init(&hdr->b_l2hdr.b_l2node); | |
ca0bf58d | 1140 | multilist_link_init(&hdr->b_l1hdr.b_arc_node); |
b9541d6b CW |
1141 | arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); |
1142 | ||
1143 | return (0); | |
1144 | } | |
1145 | ||
b5256303 TC |
1146 | static int |
1147 | hdr_full_crypt_cons(void *vbuf, void *unused, int kmflag) | |
1148 | { | |
14e4e3cb | 1149 | (void) unused; |
b5256303 TC |
1150 | arc_buf_hdr_t *hdr = vbuf; |
1151 | ||
1152 | hdr_full_cons(vbuf, unused, kmflag); | |
1153 | bzero(&hdr->b_crypt_hdr, sizeof (hdr->b_crypt_hdr)); | |
1154 | arc_space_consume(sizeof (hdr->b_crypt_hdr), ARC_SPACE_HDRS); | |
1155 | ||
1156 | return (0); | |
1157 | } | |
1158 | ||
b9541d6b CW |
1159 | static int |
1160 | hdr_l2only_cons(void *vbuf, void *unused, int kmflag) | |
34dc7c2f | 1161 | { |
14e4e3cb | 1162 | (void) unused, (void) kmflag; |
2a432414 GW |
1163 | arc_buf_hdr_t *hdr = vbuf; |
1164 | ||
b9541d6b CW |
1165 | bzero(hdr, HDR_L2ONLY_SIZE); |
1166 | arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); | |
34dc7c2f | 1167 | |
34dc7c2f BB |
1168 | return (0); |
1169 | } | |
1170 | ||
b128c09f BB |
1171 | static int |
1172 | buf_cons(void *vbuf, void *unused, int kmflag) | |
1173 | { | |
14e4e3cb | 1174 | (void) unused, (void) kmflag; |
b128c09f BB |
1175 | arc_buf_t *buf = vbuf; |
1176 | ||
1177 | bzero(buf, sizeof (arc_buf_t)); | |
428870ff | 1178 | mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); |
d164b209 BB |
1179 | arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); |
1180 | ||
b128c09f BB |
1181 | return (0); |
1182 | } | |
1183 | ||
34dc7c2f BB |
1184 | /* |
1185 | * Destructor callback - called when a cached buf is | |
1186 | * no longer required. | |
1187 | */ | |
34dc7c2f | 1188 | static void |
b9541d6b | 1189 | hdr_full_dest(void *vbuf, void *unused) |
34dc7c2f | 1190 | { |
14e4e3cb | 1191 | (void) unused; |
2a432414 | 1192 | arc_buf_hdr_t *hdr = vbuf; |
34dc7c2f | 1193 | |
d3c2ae1c | 1194 | ASSERT(HDR_EMPTY(hdr)); |
b9541d6b | 1195 | cv_destroy(&hdr->b_l1hdr.b_cv); |
424fd7c3 | 1196 | zfs_refcount_destroy(&hdr->b_l1hdr.b_refcnt); |
b9541d6b | 1197 | mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); |
ca0bf58d | 1198 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b CW |
1199 | arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); |
1200 | } | |
1201 | ||
b5256303 TC |
1202 | static void |
1203 | hdr_full_crypt_dest(void *vbuf, void *unused) | |
1204 | { | |
14e4e3cb | 1205 | (void) unused; |
b5256303 TC |
1206 | arc_buf_hdr_t *hdr = vbuf; |
1207 | ||
1208 | hdr_full_dest(vbuf, unused); | |
1209 | arc_space_return(sizeof (hdr->b_crypt_hdr), ARC_SPACE_HDRS); | |
1210 | } | |
1211 | ||
b9541d6b CW |
1212 | static void |
1213 | hdr_l2only_dest(void *vbuf, void *unused) | |
1214 | { | |
14e4e3cb AZ |
1215 | (void) unused; |
1216 | arc_buf_hdr_t *hdr = vbuf; | |
b9541d6b | 1217 | |
d3c2ae1c | 1218 | ASSERT(HDR_EMPTY(hdr)); |
b9541d6b | 1219 | arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); |
34dc7c2f BB |
1220 | } |
1221 | ||
b128c09f BB |
1222 | static void |
1223 | buf_dest(void *vbuf, void *unused) | |
1224 | { | |
14e4e3cb | 1225 | (void) unused; |
b128c09f BB |
1226 | arc_buf_t *buf = vbuf; |
1227 | ||
428870ff | 1228 | mutex_destroy(&buf->b_evict_lock); |
d164b209 | 1229 | arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); |
b128c09f BB |
1230 | } |
1231 | ||
34dc7c2f BB |
1232 | static void |
1233 | buf_init(void) | |
1234 | { | |
2db28197 | 1235 | uint64_t *ct = NULL; |
34dc7c2f BB |
1236 | uint64_t hsize = 1ULL << 12; |
1237 | int i, j; | |
1238 | ||
1239 | /* | |
1240 | * The hash table is big enough to fill all of physical memory | |
49ddb315 MA |
1241 | * with an average block size of zfs_arc_average_blocksize (default 8K). |
1242 | * By default, the table will take up | |
1243 | * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). | |
34dc7c2f | 1244 | */ |
9edb3695 | 1245 | while (hsize * zfs_arc_average_blocksize < arc_all_memory()) |
34dc7c2f BB |
1246 | hsize <<= 1; |
1247 | retry: | |
1248 | buf_hash_table.ht_mask = hsize - 1; | |
93ce2b4c | 1249 | #if defined(_KERNEL) |
d1d7e268 MK |
1250 | /* |
1251 | * Large allocations which do not require contiguous pages | |
1252 | * should be using vmem_alloc() in the linux kernel | |
1253 | */ | |
00b46022 BB |
1254 | buf_hash_table.ht_table = |
1255 | vmem_zalloc(hsize * sizeof (void*), KM_SLEEP); | |
1256 | #else | |
34dc7c2f BB |
1257 | buf_hash_table.ht_table = |
1258 | kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); | |
00b46022 | 1259 | #endif |
34dc7c2f BB |
1260 | if (buf_hash_table.ht_table == NULL) { |
1261 | ASSERT(hsize > (1ULL << 8)); | |
1262 | hsize >>= 1; | |
1263 | goto retry; | |
1264 | } | |
1265 | ||
b9541d6b | 1266 | hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, |
026e529c | 1267 | 0, hdr_full_cons, hdr_full_dest, NULL, NULL, NULL, 0); |
b5256303 TC |
1268 | hdr_full_crypt_cache = kmem_cache_create("arc_buf_hdr_t_full_crypt", |
1269 | HDR_FULL_CRYPT_SIZE, 0, hdr_full_crypt_cons, hdr_full_crypt_dest, | |
026e529c | 1270 | NULL, NULL, NULL, 0); |
b9541d6b | 1271 | hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", |
026e529c | 1272 | HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, NULL, |
b9541d6b | 1273 | NULL, NULL, 0); |
34dc7c2f | 1274 | buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), |
b128c09f | 1275 | 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); |
34dc7c2f BB |
1276 | |
1277 | for (i = 0; i < 256; i++) | |
1278 | for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) | |
1279 | *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); | |
1280 | ||
490c845e AM |
1281 | for (i = 0; i < BUF_LOCKS; i++) |
1282 | mutex_init(BUF_HASH_LOCK(i), NULL, MUTEX_DEFAULT, NULL); | |
34dc7c2f BB |
1283 | } |
1284 | ||
d3c2ae1c | 1285 | #define ARC_MINTIME (hz>>4) /* 62 ms */ |
ca0bf58d | 1286 | |
2aa34383 DK |
1287 | /* |
1288 | * This is the size that the buf occupies in memory. If the buf is compressed, | |
1289 | * it will correspond to the compressed size. You should use this method of | |
1290 | * getting the buf size unless you explicitly need the logical size. | |
1291 | */ | |
1292 | uint64_t | |
1293 | arc_buf_size(arc_buf_t *buf) | |
1294 | { | |
1295 | return (ARC_BUF_COMPRESSED(buf) ? | |
1296 | HDR_GET_PSIZE(buf->b_hdr) : HDR_GET_LSIZE(buf->b_hdr)); | |
1297 | } | |
1298 | ||
1299 | uint64_t | |
1300 | arc_buf_lsize(arc_buf_t *buf) | |
1301 | { | |
1302 | return (HDR_GET_LSIZE(buf->b_hdr)); | |
1303 | } | |
1304 | ||
b5256303 TC |
1305 | /* |
1306 | * This function will return B_TRUE if the buffer is encrypted in memory. | |
1307 | * This buffer can be decrypted by calling arc_untransform(). | |
1308 | */ | |
1309 | boolean_t | |
1310 | arc_is_encrypted(arc_buf_t *buf) | |
1311 | { | |
1312 | return (ARC_BUF_ENCRYPTED(buf) != 0); | |
1313 | } | |
1314 | ||
1315 | /* | |
1316 | * Returns B_TRUE if the buffer represents data that has not had its MAC | |
1317 | * verified yet. | |
1318 | */ | |
1319 | boolean_t | |
1320 | arc_is_unauthenticated(arc_buf_t *buf) | |
1321 | { | |
1322 | return (HDR_NOAUTH(buf->b_hdr) != 0); | |
1323 | } | |
1324 | ||
1325 | void | |
1326 | arc_get_raw_params(arc_buf_t *buf, boolean_t *byteorder, uint8_t *salt, | |
1327 | uint8_t *iv, uint8_t *mac) | |
1328 | { | |
1329 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
1330 | ||
1331 | ASSERT(HDR_PROTECTED(hdr)); | |
1332 | ||
1333 | bcopy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN); | |
1334 | bcopy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN); | |
1335 | bcopy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN); | |
1336 | *byteorder = (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ? | |
1337 | ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER; | |
1338 | } | |
1339 | ||
1340 | /* | |
1341 | * Indicates how this buffer is compressed in memory. If it is not compressed | |
1342 | * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with | |
1343 | * arc_untransform() as long as it is also unencrypted. | |
1344 | */ | |
2aa34383 DK |
1345 | enum zio_compress |
1346 | arc_get_compression(arc_buf_t *buf) | |
1347 | { | |
1348 | return (ARC_BUF_COMPRESSED(buf) ? | |
1349 | HDR_GET_COMPRESS(buf->b_hdr) : ZIO_COMPRESS_OFF); | |
1350 | } | |
1351 | ||
b5256303 TC |
1352 | /* |
1353 | * Return the compression algorithm used to store this data in the ARC. If ARC | |
1354 | * compression is enabled or this is an encrypted block, this will be the same | |
1355 | * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF. | |
1356 | */ | |
1357 | static inline enum zio_compress | |
1358 | arc_hdr_get_compress(arc_buf_hdr_t *hdr) | |
1359 | { | |
1360 | return (HDR_COMPRESSION_ENABLED(hdr) ? | |
1361 | HDR_GET_COMPRESS(hdr) : ZIO_COMPRESS_OFF); | |
1362 | } | |
1363 | ||
10b3c7f5 MN |
1364 | uint8_t |
1365 | arc_get_complevel(arc_buf_t *buf) | |
1366 | { | |
1367 | return (buf->b_hdr->b_complevel); | |
1368 | } | |
1369 | ||
d3c2ae1c GW |
1370 | static inline boolean_t |
1371 | arc_buf_is_shared(arc_buf_t *buf) | |
1372 | { | |
1373 | boolean_t shared = (buf->b_data != NULL && | |
a6255b7f DQ |
1374 | buf->b_hdr->b_l1hdr.b_pabd != NULL && |
1375 | abd_is_linear(buf->b_hdr->b_l1hdr.b_pabd) && | |
1376 | buf->b_data == abd_to_buf(buf->b_hdr->b_l1hdr.b_pabd)); | |
d3c2ae1c | 1377 | IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr)); |
2aa34383 DK |
1378 | IMPLY(shared, ARC_BUF_SHARED(buf)); |
1379 | IMPLY(shared, ARC_BUF_COMPRESSED(buf) || ARC_BUF_LAST(buf)); | |
524b4217 DK |
1380 | |
1381 | /* | |
1382 | * It would be nice to assert arc_can_share() too, but the "hdr isn't | |
1383 | * already being shared" requirement prevents us from doing that. | |
1384 | */ | |
1385 | ||
d3c2ae1c GW |
1386 | return (shared); |
1387 | } | |
ca0bf58d | 1388 | |
a7004725 DK |
1389 | /* |
1390 | * Free the checksum associated with this header. If there is no checksum, this | |
1391 | * is a no-op. | |
1392 | */ | |
d3c2ae1c GW |
1393 | static inline void |
1394 | arc_cksum_free(arc_buf_hdr_t *hdr) | |
1395 | { | |
1396 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
b5256303 | 1397 | |
d3c2ae1c GW |
1398 | mutex_enter(&hdr->b_l1hdr.b_freeze_lock); |
1399 | if (hdr->b_l1hdr.b_freeze_cksum != NULL) { | |
1400 | kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t)); | |
1401 | hdr->b_l1hdr.b_freeze_cksum = NULL; | |
b9541d6b | 1402 | } |
d3c2ae1c | 1403 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
b9541d6b CW |
1404 | } |
1405 | ||
a7004725 DK |
1406 | /* |
1407 | * Return true iff at least one of the bufs on hdr is not compressed. | |
b5256303 | 1408 | * Encrypted buffers count as compressed. |
a7004725 DK |
1409 | */ |
1410 | static boolean_t | |
1411 | arc_hdr_has_uncompressed_buf(arc_buf_hdr_t *hdr) | |
1412 | { | |
ca6c7a94 | 1413 | ASSERT(hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY_OR_LOCKED(hdr)); |
149ce888 | 1414 | |
a7004725 DK |
1415 | for (arc_buf_t *b = hdr->b_l1hdr.b_buf; b != NULL; b = b->b_next) { |
1416 | if (!ARC_BUF_COMPRESSED(b)) { | |
1417 | return (B_TRUE); | |
1418 | } | |
1419 | } | |
1420 | return (B_FALSE); | |
1421 | } | |
1422 | ||
1423 | ||
524b4217 DK |
1424 | /* |
1425 | * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data | |
1426 | * matches the checksum that is stored in the hdr. If there is no checksum, | |
1427 | * or if the buf is compressed, this is a no-op. | |
1428 | */ | |
34dc7c2f BB |
1429 | static void |
1430 | arc_cksum_verify(arc_buf_t *buf) | |
1431 | { | |
d3c2ae1c | 1432 | arc_buf_hdr_t *hdr = buf->b_hdr; |
34dc7c2f BB |
1433 | zio_cksum_t zc; |
1434 | ||
1435 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
1436 | return; | |
1437 | ||
149ce888 | 1438 | if (ARC_BUF_COMPRESSED(buf)) |
524b4217 | 1439 | return; |
524b4217 | 1440 | |
d3c2ae1c GW |
1441 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1442 | ||
1443 | mutex_enter(&hdr->b_l1hdr.b_freeze_lock); | |
149ce888 | 1444 | |
d3c2ae1c GW |
1445 | if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) { |
1446 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); | |
34dc7c2f BB |
1447 | return; |
1448 | } | |
2aa34383 | 1449 | |
3c67d83a | 1450 | fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, &zc); |
d3c2ae1c | 1451 | if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc)) |
34dc7c2f | 1452 | panic("buffer modified while frozen!"); |
d3c2ae1c | 1453 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1454 | } |
1455 | ||
b5256303 TC |
1456 | /* |
1457 | * This function makes the assumption that data stored in the L2ARC | |
1458 | * will be transformed exactly as it is in the main pool. Because of | |
1459 | * this we can verify the checksum against the reading process's bp. | |
1460 | */ | |
d3c2ae1c GW |
1461 | static boolean_t |
1462 | arc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio) | |
34dc7c2f | 1463 | { |
d3c2ae1c GW |
1464 | ASSERT(!BP_IS_EMBEDDED(zio->io_bp)); |
1465 | VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr)); | |
34dc7c2f | 1466 | |
d3c2ae1c GW |
1467 | /* |
1468 | * Block pointers always store the checksum for the logical data. | |
1469 | * If the block pointer has the gang bit set, then the checksum | |
1470 | * it represents is for the reconstituted data and not for an | |
1471 | * individual gang member. The zio pipeline, however, must be able to | |
1472 | * determine the checksum of each of the gang constituents so it | |
1473 | * treats the checksum comparison differently than what we need | |
1474 | * for l2arc blocks. This prevents us from using the | |
1475 | * zio_checksum_error() interface directly. Instead we must call the | |
1476 | * zio_checksum_error_impl() so that we can ensure the checksum is | |
1477 | * generated using the correct checksum algorithm and accounts for the | |
1478 | * logical I/O size and not just a gang fragment. | |
1479 | */ | |
b5256303 | 1480 | return (zio_checksum_error_impl(zio->io_spa, zio->io_bp, |
a6255b7f | 1481 | BP_GET_CHECKSUM(zio->io_bp), zio->io_abd, zio->io_size, |
d3c2ae1c | 1482 | zio->io_offset, NULL) == 0); |
34dc7c2f BB |
1483 | } |
1484 | ||
524b4217 DK |
1485 | /* |
1486 | * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a | |
1487 | * checksum and attaches it to the buf's hdr so that we can ensure that the buf | |
1488 | * isn't modified later on. If buf is compressed or there is already a checksum | |
1489 | * on the hdr, this is a no-op (we only checksum uncompressed bufs). | |
1490 | */ | |
34dc7c2f | 1491 | static void |
d3c2ae1c | 1492 | arc_cksum_compute(arc_buf_t *buf) |
34dc7c2f | 1493 | { |
d3c2ae1c GW |
1494 | arc_buf_hdr_t *hdr = buf->b_hdr; |
1495 | ||
1496 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
34dc7c2f BB |
1497 | return; |
1498 | ||
d3c2ae1c | 1499 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2aa34383 | 1500 | |
b9541d6b | 1501 | mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
149ce888 | 1502 | if (hdr->b_l1hdr.b_freeze_cksum != NULL || ARC_BUF_COMPRESSED(buf)) { |
d3c2ae1c | 1503 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1504 | return; |
1505 | } | |
2aa34383 | 1506 | |
b5256303 | 1507 | ASSERT(!ARC_BUF_ENCRYPTED(buf)); |
2aa34383 | 1508 | ASSERT(!ARC_BUF_COMPRESSED(buf)); |
d3c2ae1c GW |
1509 | hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), |
1510 | KM_SLEEP); | |
3c67d83a | 1511 | fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, |
d3c2ae1c GW |
1512 | hdr->b_l1hdr.b_freeze_cksum); |
1513 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); | |
498877ba MA |
1514 | arc_buf_watch(buf); |
1515 | } | |
1516 | ||
1517 | #ifndef _KERNEL | |
1518 | void | |
1519 | arc_buf_sigsegv(int sig, siginfo_t *si, void *unused) | |
1520 | { | |
14e4e3cb | 1521 | (void) sig, (void) unused; |
02730c33 | 1522 | panic("Got SIGSEGV at address: 0x%lx\n", (long)si->si_addr); |
498877ba MA |
1523 | } |
1524 | #endif | |
1525 | ||
498877ba MA |
1526 | static void |
1527 | arc_buf_unwatch(arc_buf_t *buf) | |
1528 | { | |
1529 | #ifndef _KERNEL | |
1530 | if (arc_watch) { | |
a7004725 | 1531 | ASSERT0(mprotect(buf->b_data, arc_buf_size(buf), |
498877ba MA |
1532 | PROT_READ | PROT_WRITE)); |
1533 | } | |
14e4e3cb AZ |
1534 | #else |
1535 | (void) buf; | |
498877ba MA |
1536 | #endif |
1537 | } | |
1538 | ||
498877ba MA |
1539 | static void |
1540 | arc_buf_watch(arc_buf_t *buf) | |
1541 | { | |
1542 | #ifndef _KERNEL | |
1543 | if (arc_watch) | |
2aa34383 | 1544 | ASSERT0(mprotect(buf->b_data, arc_buf_size(buf), |
d3c2ae1c | 1545 | PROT_READ)); |
14e4e3cb AZ |
1546 | #else |
1547 | (void) buf; | |
498877ba | 1548 | #endif |
34dc7c2f BB |
1549 | } |
1550 | ||
b9541d6b CW |
1551 | static arc_buf_contents_t |
1552 | arc_buf_type(arc_buf_hdr_t *hdr) | |
1553 | { | |
d3c2ae1c | 1554 | arc_buf_contents_t type; |
b9541d6b | 1555 | if (HDR_ISTYPE_METADATA(hdr)) { |
d3c2ae1c | 1556 | type = ARC_BUFC_METADATA; |
b9541d6b | 1557 | } else { |
d3c2ae1c | 1558 | type = ARC_BUFC_DATA; |
b9541d6b | 1559 | } |
d3c2ae1c GW |
1560 | VERIFY3U(hdr->b_type, ==, type); |
1561 | return (type); | |
b9541d6b CW |
1562 | } |
1563 | ||
2aa34383 DK |
1564 | boolean_t |
1565 | arc_is_metadata(arc_buf_t *buf) | |
1566 | { | |
1567 | return (HDR_ISTYPE_METADATA(buf->b_hdr) != 0); | |
1568 | } | |
1569 | ||
b9541d6b CW |
1570 | static uint32_t |
1571 | arc_bufc_to_flags(arc_buf_contents_t type) | |
1572 | { | |
1573 | switch (type) { | |
1574 | case ARC_BUFC_DATA: | |
1575 | /* metadata field is 0 if buffer contains normal data */ | |
1576 | return (0); | |
1577 | case ARC_BUFC_METADATA: | |
1578 | return (ARC_FLAG_BUFC_METADATA); | |
1579 | default: | |
1580 | break; | |
1581 | } | |
1582 | panic("undefined ARC buffer type!"); | |
1583 | return ((uint32_t)-1); | |
1584 | } | |
1585 | ||
34dc7c2f BB |
1586 | void |
1587 | arc_buf_thaw(arc_buf_t *buf) | |
1588 | { | |
d3c2ae1c GW |
1589 | arc_buf_hdr_t *hdr = buf->b_hdr; |
1590 | ||
2aa34383 DK |
1591 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
1592 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
1593 | ||
524b4217 | 1594 | arc_cksum_verify(buf); |
34dc7c2f | 1595 | |
2aa34383 | 1596 | /* |
149ce888 | 1597 | * Compressed buffers do not manipulate the b_freeze_cksum. |
2aa34383 | 1598 | */ |
149ce888 | 1599 | if (ARC_BUF_COMPRESSED(buf)) |
2aa34383 | 1600 | return; |
2aa34383 | 1601 | |
d3c2ae1c GW |
1602 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1603 | arc_cksum_free(hdr); | |
498877ba | 1604 | arc_buf_unwatch(buf); |
34dc7c2f BB |
1605 | } |
1606 | ||
1607 | void | |
1608 | arc_buf_freeze(arc_buf_t *buf) | |
1609 | { | |
1610 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
1611 | return; | |
1612 | ||
149ce888 | 1613 | if (ARC_BUF_COMPRESSED(buf)) |
2aa34383 | 1614 | return; |
428870ff | 1615 | |
149ce888 | 1616 | ASSERT(HDR_HAS_L1HDR(buf->b_hdr)); |
d3c2ae1c | 1617 | arc_cksum_compute(buf); |
34dc7c2f BB |
1618 | } |
1619 | ||
d3c2ae1c GW |
1620 | /* |
1621 | * The arc_buf_hdr_t's b_flags should never be modified directly. Instead, | |
1622 | * the following functions should be used to ensure that the flags are | |
1623 | * updated in a thread-safe way. When manipulating the flags either | |
1624 | * the hash_lock must be held or the hdr must be undiscoverable. This | |
1625 | * ensures that we're not racing with any other threads when updating | |
1626 | * the flags. | |
1627 | */ | |
1628 | static inline void | |
1629 | arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) | |
1630 | { | |
ca6c7a94 | 1631 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
1632 | hdr->b_flags |= flags; |
1633 | } | |
1634 | ||
1635 | static inline void | |
1636 | arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) | |
1637 | { | |
ca6c7a94 | 1638 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
1639 | hdr->b_flags &= ~flags; |
1640 | } | |
1641 | ||
1642 | /* | |
1643 | * Setting the compression bits in the arc_buf_hdr_t's b_flags is | |
1644 | * done in a special way since we have to clear and set bits | |
1645 | * at the same time. Consumers that wish to set the compression bits | |
1646 | * must use this function to ensure that the flags are updated in | |
1647 | * thread-safe manner. | |
1648 | */ | |
1649 | static void | |
1650 | arc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp) | |
1651 | { | |
ca6c7a94 | 1652 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
1653 | |
1654 | /* | |
1655 | * Holes and embedded blocks will always have a psize = 0 so | |
1656 | * we ignore the compression of the blkptr and set the | |
d3c2ae1c GW |
1657 | * want to uncompress them. Mark them as uncompressed. |
1658 | */ | |
1659 | if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) { | |
1660 | arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC); | |
d3c2ae1c | 1661 | ASSERT(!HDR_COMPRESSION_ENABLED(hdr)); |
d3c2ae1c GW |
1662 | } else { |
1663 | arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC); | |
d3c2ae1c GW |
1664 | ASSERT(HDR_COMPRESSION_ENABLED(hdr)); |
1665 | } | |
b5256303 TC |
1666 | |
1667 | HDR_SET_COMPRESS(hdr, cmp); | |
1668 | ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp); | |
d3c2ae1c GW |
1669 | } |
1670 | ||
524b4217 DK |
1671 | /* |
1672 | * Looks for another buf on the same hdr which has the data decompressed, copies | |
1673 | * from it, and returns true. If no such buf exists, returns false. | |
1674 | */ | |
1675 | static boolean_t | |
1676 | arc_buf_try_copy_decompressed_data(arc_buf_t *buf) | |
1677 | { | |
1678 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
524b4217 DK |
1679 | boolean_t copied = B_FALSE; |
1680 | ||
1681 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
1682 | ASSERT3P(buf->b_data, !=, NULL); | |
1683 | ASSERT(!ARC_BUF_COMPRESSED(buf)); | |
1684 | ||
a7004725 | 1685 | for (arc_buf_t *from = hdr->b_l1hdr.b_buf; from != NULL; |
524b4217 DK |
1686 | from = from->b_next) { |
1687 | /* can't use our own data buffer */ | |
1688 | if (from == buf) { | |
1689 | continue; | |
1690 | } | |
1691 | ||
1692 | if (!ARC_BUF_COMPRESSED(from)) { | |
1693 | bcopy(from->b_data, buf->b_data, arc_buf_size(buf)); | |
1694 | copied = B_TRUE; | |
1695 | break; | |
1696 | } | |
1697 | } | |
1698 | ||
1699 | /* | |
1700 | * There were no decompressed bufs, so there should not be a | |
1701 | * checksum on the hdr either. | |
1702 | */ | |
46db9d61 BB |
1703 | if (zfs_flags & ZFS_DEBUG_MODIFY) |
1704 | EQUIV(!copied, hdr->b_l1hdr.b_freeze_cksum == NULL); | |
524b4217 DK |
1705 | |
1706 | return (copied); | |
1707 | } | |
1708 | ||
77f6826b GA |
1709 | /* |
1710 | * Allocates an ARC buf header that's in an evicted & L2-cached state. | |
1711 | * This is used during l2arc reconstruction to make empty ARC buffers | |
1712 | * which circumvent the regular disk->arc->l2arc path and instead come | |
1713 | * into being in the reverse order, i.e. l2arc->arc. | |
1714 | */ | |
65c7cc49 | 1715 | static arc_buf_hdr_t * |
77f6826b GA |
1716 | arc_buf_alloc_l2only(size_t size, arc_buf_contents_t type, l2arc_dev_t *dev, |
1717 | dva_t dva, uint64_t daddr, int32_t psize, uint64_t birth, | |
10b3c7f5 | 1718 | enum zio_compress compress, uint8_t complevel, boolean_t protected, |
08532162 | 1719 | boolean_t prefetch, arc_state_type_t arcs_state) |
77f6826b GA |
1720 | { |
1721 | arc_buf_hdr_t *hdr; | |
1722 | ||
1723 | ASSERT(size != 0); | |
1724 | hdr = kmem_cache_alloc(hdr_l2only_cache, KM_SLEEP); | |
1725 | hdr->b_birth = birth; | |
1726 | hdr->b_type = type; | |
1727 | hdr->b_flags = 0; | |
1728 | arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L2HDR); | |
1729 | HDR_SET_LSIZE(hdr, size); | |
1730 | HDR_SET_PSIZE(hdr, psize); | |
1731 | arc_hdr_set_compress(hdr, compress); | |
10b3c7f5 | 1732 | hdr->b_complevel = complevel; |
77f6826b GA |
1733 | if (protected) |
1734 | arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED); | |
1735 | if (prefetch) | |
1736 | arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); | |
1737 | hdr->b_spa = spa_load_guid(dev->l2ad_vdev->vdev_spa); | |
1738 | ||
1739 | hdr->b_dva = dva; | |
1740 | ||
1741 | hdr->b_l2hdr.b_dev = dev; | |
1742 | hdr->b_l2hdr.b_daddr = daddr; | |
08532162 | 1743 | hdr->b_l2hdr.b_arcs_state = arcs_state; |
77f6826b GA |
1744 | |
1745 | return (hdr); | |
1746 | } | |
1747 | ||
b5256303 TC |
1748 | /* |
1749 | * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t. | |
1750 | */ | |
1751 | static uint64_t | |
1752 | arc_hdr_size(arc_buf_hdr_t *hdr) | |
1753 | { | |
1754 | uint64_t size; | |
1755 | ||
1756 | if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF && | |
1757 | HDR_GET_PSIZE(hdr) > 0) { | |
1758 | size = HDR_GET_PSIZE(hdr); | |
1759 | } else { | |
1760 | ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0); | |
1761 | size = HDR_GET_LSIZE(hdr); | |
1762 | } | |
1763 | return (size); | |
1764 | } | |
1765 | ||
1766 | static int | |
1767 | arc_hdr_authenticate(arc_buf_hdr_t *hdr, spa_t *spa, uint64_t dsobj) | |
1768 | { | |
1769 | int ret; | |
1770 | uint64_t csize; | |
1771 | uint64_t lsize = HDR_GET_LSIZE(hdr); | |
1772 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
1773 | void *tmpbuf = NULL; | |
1774 | abd_t *abd = hdr->b_l1hdr.b_pabd; | |
1775 | ||
ca6c7a94 | 1776 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
b5256303 TC |
1777 | ASSERT(HDR_AUTHENTICATED(hdr)); |
1778 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
1779 | ||
1780 | /* | |
1781 | * The MAC is calculated on the compressed data that is stored on disk. | |
1782 | * However, if compressed arc is disabled we will only have the | |
1783 | * decompressed data available to us now. Compress it into a temporary | |
1784 | * abd so we can verify the MAC. The performance overhead of this will | |
1785 | * be relatively low, since most objects in an encrypted objset will | |
1786 | * be encrypted (instead of authenticated) anyway. | |
1787 | */ | |
1788 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
1789 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
1790 | tmpbuf = zio_buf_alloc(lsize); | |
1791 | abd = abd_get_from_buf(tmpbuf, lsize); | |
1792 | abd_take_ownership_of_buf(abd, B_TRUE); | |
b5256303 | 1793 | csize = zio_compress_data(HDR_GET_COMPRESS(hdr), |
10b3c7f5 | 1794 | hdr->b_l1hdr.b_pabd, tmpbuf, lsize, hdr->b_complevel); |
b5256303 TC |
1795 | ASSERT3U(csize, <=, psize); |
1796 | abd_zero_off(abd, csize, psize - csize); | |
1797 | } | |
1798 | ||
1799 | /* | |
1800 | * Authentication is best effort. We authenticate whenever the key is | |
1801 | * available. If we succeed we clear ARC_FLAG_NOAUTH. | |
1802 | */ | |
1803 | if (hdr->b_crypt_hdr.b_ot == DMU_OT_OBJSET) { | |
1804 | ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); | |
1805 | ASSERT3U(lsize, ==, psize); | |
1806 | ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa, dsobj, abd, | |
1807 | psize, hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
1808 | } else { | |
1809 | ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj, abd, psize, | |
1810 | hdr->b_crypt_hdr.b_mac); | |
1811 | } | |
1812 | ||
1813 | if (ret == 0) | |
1814 | arc_hdr_clear_flags(hdr, ARC_FLAG_NOAUTH); | |
1815 | else if (ret != ENOENT) | |
1816 | goto error; | |
1817 | ||
1818 | if (tmpbuf != NULL) | |
1819 | abd_free(abd); | |
1820 | ||
1821 | return (0); | |
1822 | ||
1823 | error: | |
1824 | if (tmpbuf != NULL) | |
1825 | abd_free(abd); | |
1826 | ||
1827 | return (ret); | |
1828 | } | |
1829 | ||
1830 | /* | |
1831 | * This function will take a header that only has raw encrypted data in | |
1832 | * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in | |
1833 | * b_l1hdr.b_pabd. If designated in the header flags, this function will | |
1834 | * also decompress the data. | |
1835 | */ | |
1836 | static int | |
be9a5c35 | 1837 | arc_hdr_decrypt(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb) |
b5256303 TC |
1838 | { |
1839 | int ret; | |
b5256303 TC |
1840 | abd_t *cabd = NULL; |
1841 | void *tmp = NULL; | |
1842 | boolean_t no_crypt = B_FALSE; | |
1843 | boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
1844 | ||
ca6c7a94 | 1845 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
b5256303 TC |
1846 | ASSERT(HDR_ENCRYPTED(hdr)); |
1847 | ||
e111c802 | 1848 | arc_hdr_alloc_abd(hdr, ARC_HDR_DO_ADAPT); |
b5256303 | 1849 | |
be9a5c35 TC |
1850 | ret = spa_do_crypt_abd(B_FALSE, spa, zb, hdr->b_crypt_hdr.b_ot, |
1851 | B_FALSE, bswap, hdr->b_crypt_hdr.b_salt, hdr->b_crypt_hdr.b_iv, | |
1852 | hdr->b_crypt_hdr.b_mac, HDR_GET_PSIZE(hdr), hdr->b_l1hdr.b_pabd, | |
b5256303 TC |
1853 | hdr->b_crypt_hdr.b_rabd, &no_crypt); |
1854 | if (ret != 0) | |
1855 | goto error; | |
1856 | ||
1857 | if (no_crypt) { | |
1858 | abd_copy(hdr->b_l1hdr.b_pabd, hdr->b_crypt_hdr.b_rabd, | |
1859 | HDR_GET_PSIZE(hdr)); | |
1860 | } | |
1861 | ||
1862 | /* | |
1863 | * If this header has disabled arc compression but the b_pabd is | |
1864 | * compressed after decrypting it, we need to decompress the newly | |
1865 | * decrypted data. | |
1866 | */ | |
1867 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
1868 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
1869 | /* | |
1870 | * We want to make sure that we are correctly honoring the | |
1871 | * zfs_abd_scatter_enabled setting, so we allocate an abd here | |
1872 | * and then loan a buffer from it, rather than allocating a | |
1873 | * linear buffer and wrapping it in an abd later. | |
1874 | */ | |
6b88b4b5 AM |
1875 | cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, |
1876 | ARC_HDR_DO_ADAPT); | |
b5256303 TC |
1877 | tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr)); |
1878 | ||
1879 | ret = zio_decompress_data(HDR_GET_COMPRESS(hdr), | |
1880 | hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr), | |
10b3c7f5 | 1881 | HDR_GET_LSIZE(hdr), &hdr->b_complevel); |
b5256303 TC |
1882 | if (ret != 0) { |
1883 | abd_return_buf(cabd, tmp, arc_hdr_size(hdr)); | |
1884 | goto error; | |
1885 | } | |
1886 | ||
1887 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
1888 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
1889 | arc_hdr_size(hdr), hdr); | |
1890 | hdr->b_l1hdr.b_pabd = cabd; | |
1891 | } | |
1892 | ||
b5256303 TC |
1893 | return (0); |
1894 | ||
1895 | error: | |
1896 | arc_hdr_free_abd(hdr, B_FALSE); | |
b5256303 TC |
1897 | if (cabd != NULL) |
1898 | arc_free_data_buf(hdr, cabd, arc_hdr_size(hdr), hdr); | |
1899 | ||
1900 | return (ret); | |
1901 | } | |
1902 | ||
1903 | /* | |
1904 | * This function is called during arc_buf_fill() to prepare the header's | |
1905 | * abd plaintext pointer for use. This involves authenticated protected | |
1906 | * data and decrypting encrypted data into the plaintext abd. | |
1907 | */ | |
1908 | static int | |
1909 | arc_fill_hdr_crypt(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, spa_t *spa, | |
be9a5c35 | 1910 | const zbookmark_phys_t *zb, boolean_t noauth) |
b5256303 TC |
1911 | { |
1912 | int ret; | |
1913 | ||
1914 | ASSERT(HDR_PROTECTED(hdr)); | |
1915 | ||
1916 | if (hash_lock != NULL) | |
1917 | mutex_enter(hash_lock); | |
1918 | ||
1919 | if (HDR_NOAUTH(hdr) && !noauth) { | |
1920 | /* | |
1921 | * The caller requested authenticated data but our data has | |
1922 | * not been authenticated yet. Verify the MAC now if we can. | |
1923 | */ | |
be9a5c35 | 1924 | ret = arc_hdr_authenticate(hdr, spa, zb->zb_objset); |
b5256303 TC |
1925 | if (ret != 0) |
1926 | goto error; | |
1927 | } else if (HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd == NULL) { | |
1928 | /* | |
1929 | * If we only have the encrypted version of the data, but the | |
1930 | * unencrypted version was requested we take this opportunity | |
1931 | * to store the decrypted version in the header for future use. | |
1932 | */ | |
be9a5c35 | 1933 | ret = arc_hdr_decrypt(hdr, spa, zb); |
b5256303 TC |
1934 | if (ret != 0) |
1935 | goto error; | |
1936 | } | |
1937 | ||
1938 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
1939 | ||
1940 | if (hash_lock != NULL) | |
1941 | mutex_exit(hash_lock); | |
1942 | ||
1943 | return (0); | |
1944 | ||
1945 | error: | |
1946 | if (hash_lock != NULL) | |
1947 | mutex_exit(hash_lock); | |
1948 | ||
1949 | return (ret); | |
1950 | } | |
1951 | ||
1952 | /* | |
1953 | * This function is used by the dbuf code to decrypt bonus buffers in place. | |
1954 | * The dbuf code itself doesn't have any locking for decrypting a shared dnode | |
1955 | * block, so we use the hash lock here to protect against concurrent calls to | |
1956 | * arc_buf_fill(). | |
1957 | */ | |
1958 | static void | |
14e4e3cb | 1959 | arc_buf_untransform_in_place(arc_buf_t *buf) |
b5256303 TC |
1960 | { |
1961 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
1962 | ||
1963 | ASSERT(HDR_ENCRYPTED(hdr)); | |
1964 | ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE); | |
ca6c7a94 | 1965 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
b5256303 TC |
1966 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
1967 | ||
1968 | zio_crypt_copy_dnode_bonus(hdr->b_l1hdr.b_pabd, buf->b_data, | |
1969 | arc_buf_size(buf)); | |
1970 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
1971 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
1972 | hdr->b_crypt_hdr.b_ebufcnt -= 1; | |
1973 | } | |
1974 | ||
524b4217 DK |
1975 | /* |
1976 | * Given a buf that has a data buffer attached to it, this function will | |
1977 | * efficiently fill the buf with data of the specified compression setting from | |
1978 | * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr | |
1979 | * are already sharing a data buf, no copy is performed. | |
1980 | * | |
1981 | * If the buf is marked as compressed but uncompressed data was requested, this | |
1982 | * will allocate a new data buffer for the buf, remove that flag, and fill the | |
1983 | * buf with uncompressed data. You can't request a compressed buf on a hdr with | |
1984 | * uncompressed data, and (since we haven't added support for it yet) if you | |
1985 | * want compressed data your buf must already be marked as compressed and have | |
1986 | * the correct-sized data buffer. | |
1987 | */ | |
1988 | static int | |
be9a5c35 TC |
1989 | arc_buf_fill(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb, |
1990 | arc_fill_flags_t flags) | |
d3c2ae1c | 1991 | { |
b5256303 | 1992 | int error = 0; |
d3c2ae1c | 1993 | arc_buf_hdr_t *hdr = buf->b_hdr; |
b5256303 TC |
1994 | boolean_t hdr_compressed = |
1995 | (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); | |
1996 | boolean_t compressed = (flags & ARC_FILL_COMPRESSED) != 0; | |
1997 | boolean_t encrypted = (flags & ARC_FILL_ENCRYPTED) != 0; | |
d3c2ae1c | 1998 | dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap; |
b5256303 | 1999 | kmutex_t *hash_lock = (flags & ARC_FILL_LOCKED) ? NULL : HDR_LOCK(hdr); |
d3c2ae1c | 2000 | |
524b4217 | 2001 | ASSERT3P(buf->b_data, !=, NULL); |
b5256303 | 2002 | IMPLY(compressed, hdr_compressed || ARC_BUF_ENCRYPTED(buf)); |
524b4217 | 2003 | IMPLY(compressed, ARC_BUF_COMPRESSED(buf)); |
b5256303 TC |
2004 | IMPLY(encrypted, HDR_ENCRYPTED(hdr)); |
2005 | IMPLY(encrypted, ARC_BUF_ENCRYPTED(buf)); | |
2006 | IMPLY(encrypted, ARC_BUF_COMPRESSED(buf)); | |
2007 | IMPLY(encrypted, !ARC_BUF_SHARED(buf)); | |
2008 | ||
2009 | /* | |
2010 | * If the caller wanted encrypted data we just need to copy it from | |
2011 | * b_rabd and potentially byteswap it. We won't be able to do any | |
2012 | * further transforms on it. | |
2013 | */ | |
2014 | if (encrypted) { | |
2015 | ASSERT(HDR_HAS_RABD(hdr)); | |
2016 | abd_copy_to_buf(buf->b_data, hdr->b_crypt_hdr.b_rabd, | |
2017 | HDR_GET_PSIZE(hdr)); | |
2018 | goto byteswap; | |
2019 | } | |
2020 | ||
2021 | /* | |
e1cfd73f | 2022 | * Adjust encrypted and authenticated headers to accommodate |
69830602 TC |
2023 | * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are |
2024 | * allowed to fail decryption due to keys not being loaded | |
2025 | * without being marked as an IO error. | |
b5256303 TC |
2026 | */ |
2027 | if (HDR_PROTECTED(hdr)) { | |
2028 | error = arc_fill_hdr_crypt(hdr, hash_lock, spa, | |
be9a5c35 | 2029 | zb, !!(flags & ARC_FILL_NOAUTH)); |
69830602 TC |
2030 | if (error == EACCES && (flags & ARC_FILL_IN_PLACE) != 0) { |
2031 | return (error); | |
2032 | } else if (error != 0) { | |
e7504d7a TC |
2033 | if (hash_lock != NULL) |
2034 | mutex_enter(hash_lock); | |
2c24b5b1 | 2035 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); |
e7504d7a TC |
2036 | if (hash_lock != NULL) |
2037 | mutex_exit(hash_lock); | |
b5256303 | 2038 | return (error); |
2c24b5b1 | 2039 | } |
b5256303 TC |
2040 | } |
2041 | ||
2042 | /* | |
2043 | * There is a special case here for dnode blocks which are | |
2044 | * decrypting their bonus buffers. These blocks may request to | |
2045 | * be decrypted in-place. This is necessary because there may | |
2046 | * be many dnodes pointing into this buffer and there is | |
2047 | * currently no method to synchronize replacing the backing | |
2048 | * b_data buffer and updating all of the pointers. Here we use | |
2049 | * the hash lock to ensure there are no races. If the need | |
2050 | * arises for other types to be decrypted in-place, they must | |
2051 | * add handling here as well. | |
2052 | */ | |
2053 | if ((flags & ARC_FILL_IN_PLACE) != 0) { | |
2054 | ASSERT(!hdr_compressed); | |
2055 | ASSERT(!compressed); | |
2056 | ASSERT(!encrypted); | |
2057 | ||
2058 | if (HDR_ENCRYPTED(hdr) && ARC_BUF_ENCRYPTED(buf)) { | |
2059 | ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE); | |
2060 | ||
2061 | if (hash_lock != NULL) | |
2062 | mutex_enter(hash_lock); | |
14e4e3cb | 2063 | arc_buf_untransform_in_place(buf); |
b5256303 TC |
2064 | if (hash_lock != NULL) |
2065 | mutex_exit(hash_lock); | |
2066 | ||
2067 | /* Compute the hdr's checksum if necessary */ | |
2068 | arc_cksum_compute(buf); | |
2069 | } | |
2070 | ||
2071 | return (0); | |
2072 | } | |
524b4217 DK |
2073 | |
2074 | if (hdr_compressed == compressed) { | |
2aa34383 | 2075 | if (!arc_buf_is_shared(buf)) { |
a6255b7f | 2076 | abd_copy_to_buf(buf->b_data, hdr->b_l1hdr.b_pabd, |
524b4217 | 2077 | arc_buf_size(buf)); |
2aa34383 | 2078 | } |
d3c2ae1c | 2079 | } else { |
524b4217 DK |
2080 | ASSERT(hdr_compressed); |
2081 | ASSERT(!compressed); | |
2aa34383 DK |
2082 | |
2083 | /* | |
524b4217 DK |
2084 | * If the buf is sharing its data with the hdr, unlink it and |
2085 | * allocate a new data buffer for the buf. | |
2aa34383 | 2086 | */ |
524b4217 DK |
2087 | if (arc_buf_is_shared(buf)) { |
2088 | ASSERT(ARC_BUF_COMPRESSED(buf)); | |
2089 | ||
e1cfd73f | 2090 | /* We need to give the buf its own b_data */ |
524b4217 | 2091 | buf->b_flags &= ~ARC_BUF_FLAG_SHARED; |
2aa34383 DK |
2092 | buf->b_data = |
2093 | arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); | |
2094 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); | |
2095 | ||
524b4217 | 2096 | /* Previously overhead was 0; just add new overhead */ |
2aa34383 | 2097 | ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); |
524b4217 DK |
2098 | } else if (ARC_BUF_COMPRESSED(buf)) { |
2099 | /* We need to reallocate the buf's b_data */ | |
2100 | arc_free_data_buf(hdr, buf->b_data, HDR_GET_PSIZE(hdr), | |
2101 | buf); | |
2102 | buf->b_data = | |
2103 | arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); | |
2104 | ||
2105 | /* We increased the size of b_data; update overhead */ | |
2106 | ARCSTAT_INCR(arcstat_overhead_size, | |
2107 | HDR_GET_LSIZE(hdr) - HDR_GET_PSIZE(hdr)); | |
2aa34383 DK |
2108 | } |
2109 | ||
524b4217 DK |
2110 | /* |
2111 | * Regardless of the buf's previous compression settings, it | |
2112 | * should not be compressed at the end of this function. | |
2113 | */ | |
2114 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
2115 | ||
2116 | /* | |
2117 | * Try copying the data from another buf which already has a | |
2118 | * decompressed version. If that's not possible, it's time to | |
2119 | * bite the bullet and decompress the data from the hdr. | |
2120 | */ | |
2121 | if (arc_buf_try_copy_decompressed_data(buf)) { | |
2122 | /* Skip byteswapping and checksumming (already done) */ | |
524b4217 DK |
2123 | return (0); |
2124 | } else { | |
b5256303 | 2125 | error = zio_decompress_data(HDR_GET_COMPRESS(hdr), |
a6255b7f | 2126 | hdr->b_l1hdr.b_pabd, buf->b_data, |
10b3c7f5 MN |
2127 | HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr), |
2128 | &hdr->b_complevel); | |
524b4217 DK |
2129 | |
2130 | /* | |
2131 | * Absent hardware errors or software bugs, this should | |
2132 | * be impossible, but log it anyway so we can debug it. | |
2133 | */ | |
2134 | if (error != 0) { | |
2135 | zfs_dbgmsg( | |
a887d653 | 2136 | "hdr %px, compress %d, psize %d, lsize %d", |
b5256303 | 2137 | hdr, arc_hdr_get_compress(hdr), |
524b4217 | 2138 | HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); |
e7504d7a TC |
2139 | if (hash_lock != NULL) |
2140 | mutex_enter(hash_lock); | |
2c24b5b1 | 2141 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); |
e7504d7a TC |
2142 | if (hash_lock != NULL) |
2143 | mutex_exit(hash_lock); | |
524b4217 DK |
2144 | return (SET_ERROR(EIO)); |
2145 | } | |
d3c2ae1c GW |
2146 | } |
2147 | } | |
524b4217 | 2148 | |
b5256303 | 2149 | byteswap: |
524b4217 | 2150 | /* Byteswap the buf's data if necessary */ |
d3c2ae1c GW |
2151 | if (bswap != DMU_BSWAP_NUMFUNCS) { |
2152 | ASSERT(!HDR_SHARED_DATA(hdr)); | |
2153 | ASSERT3U(bswap, <, DMU_BSWAP_NUMFUNCS); | |
2154 | dmu_ot_byteswap[bswap].ob_func(buf->b_data, HDR_GET_LSIZE(hdr)); | |
2155 | } | |
524b4217 DK |
2156 | |
2157 | /* Compute the hdr's checksum if necessary */ | |
d3c2ae1c | 2158 | arc_cksum_compute(buf); |
524b4217 | 2159 | |
d3c2ae1c GW |
2160 | return (0); |
2161 | } | |
2162 | ||
2163 | /* | |
b5256303 TC |
2164 | * If this function is being called to decrypt an encrypted buffer or verify an |
2165 | * authenticated one, the key must be loaded and a mapping must be made | |
2166 | * available in the keystore via spa_keystore_create_mapping() or one of its | |
2167 | * callers. | |
d3c2ae1c | 2168 | */ |
b5256303 | 2169 | int |
a2c2ed1b TC |
2170 | arc_untransform(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb, |
2171 | boolean_t in_place) | |
d3c2ae1c | 2172 | { |
a2c2ed1b | 2173 | int ret; |
b5256303 | 2174 | arc_fill_flags_t flags = 0; |
d3c2ae1c | 2175 | |
b5256303 TC |
2176 | if (in_place) |
2177 | flags |= ARC_FILL_IN_PLACE; | |
2178 | ||
be9a5c35 | 2179 | ret = arc_buf_fill(buf, spa, zb, flags); |
a2c2ed1b TC |
2180 | if (ret == ECKSUM) { |
2181 | /* | |
2182 | * Convert authentication and decryption errors to EIO | |
2183 | * (and generate an ereport) before leaving the ARC. | |
2184 | */ | |
2185 | ret = SET_ERROR(EIO); | |
be9a5c35 | 2186 | spa_log_error(spa, zb); |
1144586b | 2187 | (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION, |
4f072827 | 2188 | spa, NULL, zb, NULL, 0); |
a2c2ed1b TC |
2189 | } |
2190 | ||
2191 | return (ret); | |
d3c2ae1c GW |
2192 | } |
2193 | ||
2194 | /* | |
2195 | * Increment the amount of evictable space in the arc_state_t's refcount. | |
2196 | * We account for the space used by the hdr and the arc buf individually | |
2197 | * so that we can add and remove them from the refcount individually. | |
2198 | */ | |
34dc7c2f | 2199 | static void |
d3c2ae1c GW |
2200 | arc_evictable_space_increment(arc_buf_hdr_t *hdr, arc_state_t *state) |
2201 | { | |
2202 | arc_buf_contents_t type = arc_buf_type(hdr); | |
d3c2ae1c GW |
2203 | |
2204 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
2205 | ||
2206 | if (GHOST_STATE(state)) { | |
2207 | ASSERT0(hdr->b_l1hdr.b_bufcnt); | |
2208 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
a6255b7f | 2209 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2210 | ASSERT(!HDR_HAS_RABD(hdr)); |
424fd7c3 | 2211 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
2aa34383 | 2212 | HDR_GET_LSIZE(hdr), hdr); |
d3c2ae1c GW |
2213 | return; |
2214 | } | |
2215 | ||
a6255b7f | 2216 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 | 2217 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
d3c2ae1c GW |
2218 | arc_hdr_size(hdr), hdr); |
2219 | } | |
b5256303 | 2220 | if (HDR_HAS_RABD(hdr)) { |
424fd7c3 | 2221 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
b5256303 TC |
2222 | HDR_GET_PSIZE(hdr), hdr); |
2223 | } | |
2224 | ||
1c27024e DB |
2225 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
2226 | buf = buf->b_next) { | |
2aa34383 | 2227 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2228 | continue; |
424fd7c3 | 2229 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
2aa34383 | 2230 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2231 | } |
2232 | } | |
2233 | ||
2234 | /* | |
2235 | * Decrement the amount of evictable space in the arc_state_t's refcount. | |
2236 | * We account for the space used by the hdr and the arc buf individually | |
2237 | * so that we can add and remove them from the refcount individually. | |
2238 | */ | |
2239 | static void | |
2aa34383 | 2240 | arc_evictable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state) |
d3c2ae1c GW |
2241 | { |
2242 | arc_buf_contents_t type = arc_buf_type(hdr); | |
d3c2ae1c GW |
2243 | |
2244 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
2245 | ||
2246 | if (GHOST_STATE(state)) { | |
2247 | ASSERT0(hdr->b_l1hdr.b_bufcnt); | |
2248 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
a6255b7f | 2249 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2250 | ASSERT(!HDR_HAS_RABD(hdr)); |
424fd7c3 | 2251 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
2aa34383 | 2252 | HDR_GET_LSIZE(hdr), hdr); |
d3c2ae1c GW |
2253 | return; |
2254 | } | |
2255 | ||
a6255b7f | 2256 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 | 2257 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c GW |
2258 | arc_hdr_size(hdr), hdr); |
2259 | } | |
b5256303 | 2260 | if (HDR_HAS_RABD(hdr)) { |
424fd7c3 | 2261 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
b5256303 TC |
2262 | HDR_GET_PSIZE(hdr), hdr); |
2263 | } | |
2264 | ||
1c27024e DB |
2265 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
2266 | buf = buf->b_next) { | |
2aa34383 | 2267 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2268 | continue; |
424fd7c3 | 2269 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
2aa34383 | 2270 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2271 | } |
2272 | } | |
2273 | ||
2274 | /* | |
2275 | * Add a reference to this hdr indicating that someone is actively | |
2276 | * referencing that memory. When the refcount transitions from 0 to 1, | |
2277 | * we remove it from the respective arc_state_t list to indicate that | |
2278 | * it is not evictable. | |
2279 | */ | |
2280 | static void | |
2281 | add_reference(arc_buf_hdr_t *hdr, void *tag) | |
34dc7c2f | 2282 | { |
b9541d6b CW |
2283 | arc_state_t *state; |
2284 | ||
2285 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
ca6c7a94 | 2286 | if (!HDR_EMPTY(hdr) && !MUTEX_HELD(HDR_LOCK(hdr))) { |
d3c2ae1c | 2287 | ASSERT(hdr->b_l1hdr.b_state == arc_anon); |
424fd7c3 | 2288 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
2289 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
2290 | } | |
34dc7c2f | 2291 | |
b9541d6b CW |
2292 | state = hdr->b_l1hdr.b_state; |
2293 | ||
c13060e4 | 2294 | if ((zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && |
b9541d6b CW |
2295 | (state != arc_anon)) { |
2296 | /* We don't use the L2-only state list. */ | |
2297 | if (state != arc_l2c_only) { | |
ffdf019c | 2298 | multilist_remove(&state->arcs_list[arc_buf_type(hdr)], |
d3c2ae1c | 2299 | hdr); |
2aa34383 | 2300 | arc_evictable_space_decrement(hdr, state); |
34dc7c2f | 2301 | } |
b128c09f | 2302 | /* remove the prefetch flag if we get a reference */ |
08532162 GA |
2303 | if (HDR_HAS_L2HDR(hdr)) |
2304 | l2arc_hdr_arcstats_decrement_state(hdr); | |
d3c2ae1c | 2305 | arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); |
08532162 GA |
2306 | if (HDR_HAS_L2HDR(hdr)) |
2307 | l2arc_hdr_arcstats_increment_state(hdr); | |
34dc7c2f BB |
2308 | } |
2309 | } | |
2310 | ||
d3c2ae1c GW |
2311 | /* |
2312 | * Remove a reference from this hdr. When the reference transitions from | |
2313 | * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's | |
2314 | * list making it eligible for eviction. | |
2315 | */ | |
34dc7c2f | 2316 | static int |
2a432414 | 2317 | remove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) |
34dc7c2f BB |
2318 | { |
2319 | int cnt; | |
b9541d6b | 2320 | arc_state_t *state = hdr->b_l1hdr.b_state; |
34dc7c2f | 2321 | |
b9541d6b | 2322 | ASSERT(HDR_HAS_L1HDR(hdr)); |
34dc7c2f BB |
2323 | ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); |
2324 | ASSERT(!GHOST_STATE(state)); | |
2325 | ||
b9541d6b CW |
2326 | /* |
2327 | * arc_l2c_only counts as a ghost state so we don't need to explicitly | |
2328 | * check to prevent usage of the arc_l2c_only list. | |
2329 | */ | |
424fd7c3 | 2330 | if (((cnt = zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && |
34dc7c2f | 2331 | (state != arc_anon)) { |
ffdf019c | 2332 | multilist_insert(&state->arcs_list[arc_buf_type(hdr)], hdr); |
d3c2ae1c GW |
2333 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); |
2334 | arc_evictable_space_increment(hdr, state); | |
34dc7c2f BB |
2335 | } |
2336 | return (cnt); | |
2337 | } | |
2338 | ||
e0b0ca98 BB |
2339 | /* |
2340 | * Returns detailed information about a specific arc buffer. When the | |
2341 | * state_index argument is set the function will calculate the arc header | |
2342 | * list position for its arc state. Since this requires a linear traversal | |
2343 | * callers are strongly encourage not to do this. However, it can be helpful | |
2344 | * for targeted analysis so the functionality is provided. | |
2345 | */ | |
2346 | void | |
2347 | arc_buf_info(arc_buf_t *ab, arc_buf_info_t *abi, int state_index) | |
2348 | { | |
14e4e3cb | 2349 | (void) state_index; |
e0b0ca98 | 2350 | arc_buf_hdr_t *hdr = ab->b_hdr; |
b9541d6b CW |
2351 | l1arc_buf_hdr_t *l1hdr = NULL; |
2352 | l2arc_buf_hdr_t *l2hdr = NULL; | |
2353 | arc_state_t *state = NULL; | |
2354 | ||
8887c7d7 TC |
2355 | memset(abi, 0, sizeof (arc_buf_info_t)); |
2356 | ||
2357 | if (hdr == NULL) | |
2358 | return; | |
2359 | ||
2360 | abi->abi_flags = hdr->b_flags; | |
2361 | ||
b9541d6b CW |
2362 | if (HDR_HAS_L1HDR(hdr)) { |
2363 | l1hdr = &hdr->b_l1hdr; | |
2364 | state = l1hdr->b_state; | |
2365 | } | |
2366 | if (HDR_HAS_L2HDR(hdr)) | |
2367 | l2hdr = &hdr->b_l2hdr; | |
e0b0ca98 | 2368 | |
b9541d6b | 2369 | if (l1hdr) { |
d3c2ae1c | 2370 | abi->abi_bufcnt = l1hdr->b_bufcnt; |
b9541d6b CW |
2371 | abi->abi_access = l1hdr->b_arc_access; |
2372 | abi->abi_mru_hits = l1hdr->b_mru_hits; | |
2373 | abi->abi_mru_ghost_hits = l1hdr->b_mru_ghost_hits; | |
2374 | abi->abi_mfu_hits = l1hdr->b_mfu_hits; | |
2375 | abi->abi_mfu_ghost_hits = l1hdr->b_mfu_ghost_hits; | |
424fd7c3 | 2376 | abi->abi_holds = zfs_refcount_count(&l1hdr->b_refcnt); |
b9541d6b CW |
2377 | } |
2378 | ||
2379 | if (l2hdr) { | |
2380 | abi->abi_l2arc_dattr = l2hdr->b_daddr; | |
b9541d6b CW |
2381 | abi->abi_l2arc_hits = l2hdr->b_hits; |
2382 | } | |
2383 | ||
e0b0ca98 | 2384 | abi->abi_state_type = state ? state->arcs_state : ARC_STATE_ANON; |
b9541d6b | 2385 | abi->abi_state_contents = arc_buf_type(hdr); |
d3c2ae1c | 2386 | abi->abi_size = arc_hdr_size(hdr); |
e0b0ca98 BB |
2387 | } |
2388 | ||
34dc7c2f | 2389 | /* |
ca0bf58d | 2390 | * Move the supplied buffer to the indicated state. The hash lock |
34dc7c2f BB |
2391 | * for the buffer must be held by the caller. |
2392 | */ | |
2393 | static void | |
2a432414 GW |
2394 | arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, |
2395 | kmutex_t *hash_lock) | |
34dc7c2f | 2396 | { |
b9541d6b CW |
2397 | arc_state_t *old_state; |
2398 | int64_t refcnt; | |
d3c2ae1c GW |
2399 | uint32_t bufcnt; |
2400 | boolean_t update_old, update_new; | |
b9541d6b CW |
2401 | arc_buf_contents_t buftype = arc_buf_type(hdr); |
2402 | ||
2403 | /* | |
2404 | * We almost always have an L1 hdr here, since we call arc_hdr_realloc() | |
2405 | * in arc_read() when bringing a buffer out of the L2ARC. However, the | |
2406 | * L1 hdr doesn't always exist when we change state to arc_anon before | |
2407 | * destroying a header, in which case reallocating to add the L1 hdr is | |
2408 | * pointless. | |
2409 | */ | |
2410 | if (HDR_HAS_L1HDR(hdr)) { | |
2411 | old_state = hdr->b_l1hdr.b_state; | |
424fd7c3 | 2412 | refcnt = zfs_refcount_count(&hdr->b_l1hdr.b_refcnt); |
d3c2ae1c | 2413 | bufcnt = hdr->b_l1hdr.b_bufcnt; |
b5256303 TC |
2414 | update_old = (bufcnt > 0 || hdr->b_l1hdr.b_pabd != NULL || |
2415 | HDR_HAS_RABD(hdr)); | |
b9541d6b CW |
2416 | } else { |
2417 | old_state = arc_l2c_only; | |
2418 | refcnt = 0; | |
d3c2ae1c GW |
2419 | bufcnt = 0; |
2420 | update_old = B_FALSE; | |
b9541d6b | 2421 | } |
d3c2ae1c | 2422 | update_new = update_old; |
34dc7c2f BB |
2423 | |
2424 | ASSERT(MUTEX_HELD(hash_lock)); | |
e8b96c60 | 2425 | ASSERT3P(new_state, !=, old_state); |
d3c2ae1c GW |
2426 | ASSERT(!GHOST_STATE(new_state) || bufcnt == 0); |
2427 | ASSERT(old_state != arc_anon || bufcnt <= 1); | |
34dc7c2f BB |
2428 | |
2429 | /* | |
2430 | * If this buffer is evictable, transfer it from the | |
2431 | * old state list to the new state list. | |
2432 | */ | |
2433 | if (refcnt == 0) { | |
b9541d6b | 2434 | if (old_state != arc_anon && old_state != arc_l2c_only) { |
b9541d6b | 2435 | ASSERT(HDR_HAS_L1HDR(hdr)); |
ffdf019c | 2436 | multilist_remove(&old_state->arcs_list[buftype], hdr); |
34dc7c2f | 2437 | |
d3c2ae1c GW |
2438 | if (GHOST_STATE(old_state)) { |
2439 | ASSERT0(bufcnt); | |
2440 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
2441 | update_old = B_TRUE; | |
34dc7c2f | 2442 | } |
2aa34383 | 2443 | arc_evictable_space_decrement(hdr, old_state); |
34dc7c2f | 2444 | } |
b9541d6b | 2445 | if (new_state != arc_anon && new_state != arc_l2c_only) { |
b9541d6b CW |
2446 | /* |
2447 | * An L1 header always exists here, since if we're | |
2448 | * moving to some L1-cached state (i.e. not l2c_only or | |
2449 | * anonymous), we realloc the header to add an L1hdr | |
2450 | * beforehand. | |
2451 | */ | |
2452 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
ffdf019c | 2453 | multilist_insert(&new_state->arcs_list[buftype], hdr); |
34dc7c2f | 2454 | |
34dc7c2f | 2455 | if (GHOST_STATE(new_state)) { |
d3c2ae1c GW |
2456 | ASSERT0(bufcnt); |
2457 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
2458 | update_new = B_TRUE; | |
34dc7c2f | 2459 | } |
d3c2ae1c | 2460 | arc_evictable_space_increment(hdr, new_state); |
34dc7c2f BB |
2461 | } |
2462 | } | |
2463 | ||
d3c2ae1c | 2464 | ASSERT(!HDR_EMPTY(hdr)); |
2a432414 GW |
2465 | if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) |
2466 | buf_hash_remove(hdr); | |
34dc7c2f | 2467 | |
b9541d6b | 2468 | /* adjust state sizes (ignore arc_l2c_only) */ |
36da08ef | 2469 | |
d3c2ae1c | 2470 | if (update_new && new_state != arc_l2c_only) { |
36da08ef PS |
2471 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2472 | if (GHOST_STATE(new_state)) { | |
d3c2ae1c | 2473 | ASSERT0(bufcnt); |
36da08ef PS |
2474 | |
2475 | /* | |
d3c2ae1c | 2476 | * When moving a header to a ghost state, we first |
36da08ef | 2477 | * remove all arc buffers. Thus, we'll have a |
d3c2ae1c | 2478 | * bufcnt of zero, and no arc buffer to use for |
36da08ef PS |
2479 | * the reference. As a result, we use the arc |
2480 | * header pointer for the reference. | |
2481 | */ | |
424fd7c3 | 2482 | (void) zfs_refcount_add_many(&new_state->arcs_size, |
d3c2ae1c | 2483 | HDR_GET_LSIZE(hdr), hdr); |
a6255b7f | 2484 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2485 | ASSERT(!HDR_HAS_RABD(hdr)); |
36da08ef | 2486 | } else { |
d3c2ae1c | 2487 | uint32_t buffers = 0; |
36da08ef PS |
2488 | |
2489 | /* | |
2490 | * Each individual buffer holds a unique reference, | |
2491 | * thus we must remove each of these references one | |
2492 | * at a time. | |
2493 | */ | |
1c27024e | 2494 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
36da08ef | 2495 | buf = buf->b_next) { |
d3c2ae1c GW |
2496 | ASSERT3U(bufcnt, !=, 0); |
2497 | buffers++; | |
2498 | ||
2499 | /* | |
2500 | * When the arc_buf_t is sharing the data | |
2501 | * block with the hdr, the owner of the | |
2502 | * reference belongs to the hdr. Only | |
2503 | * add to the refcount if the arc_buf_t is | |
2504 | * not shared. | |
2505 | */ | |
2aa34383 | 2506 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2507 | continue; |
d3c2ae1c | 2508 | |
424fd7c3 TS |
2509 | (void) zfs_refcount_add_many( |
2510 | &new_state->arcs_size, | |
2aa34383 | 2511 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2512 | } |
2513 | ASSERT3U(bufcnt, ==, buffers); | |
2514 | ||
a6255b7f | 2515 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 TS |
2516 | (void) zfs_refcount_add_many( |
2517 | &new_state->arcs_size, | |
d3c2ae1c | 2518 | arc_hdr_size(hdr), hdr); |
b5256303 TC |
2519 | } |
2520 | ||
2521 | if (HDR_HAS_RABD(hdr)) { | |
424fd7c3 TS |
2522 | (void) zfs_refcount_add_many( |
2523 | &new_state->arcs_size, | |
b5256303 | 2524 | HDR_GET_PSIZE(hdr), hdr); |
36da08ef PS |
2525 | } |
2526 | } | |
2527 | } | |
2528 | ||
d3c2ae1c | 2529 | if (update_old && old_state != arc_l2c_only) { |
36da08ef PS |
2530 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2531 | if (GHOST_STATE(old_state)) { | |
d3c2ae1c | 2532 | ASSERT0(bufcnt); |
a6255b7f | 2533 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2534 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c | 2535 | |
36da08ef PS |
2536 | /* |
2537 | * When moving a header off of a ghost state, | |
d3c2ae1c GW |
2538 | * the header will not contain any arc buffers. |
2539 | * We use the arc header pointer for the reference | |
2540 | * which is exactly what we did when we put the | |
2541 | * header on the ghost state. | |
36da08ef PS |
2542 | */ |
2543 | ||
424fd7c3 | 2544 | (void) zfs_refcount_remove_many(&old_state->arcs_size, |
d3c2ae1c | 2545 | HDR_GET_LSIZE(hdr), hdr); |
36da08ef | 2546 | } else { |
d3c2ae1c | 2547 | uint32_t buffers = 0; |
36da08ef PS |
2548 | |
2549 | /* | |
2550 | * Each individual buffer holds a unique reference, | |
2551 | * thus we must remove each of these references one | |
2552 | * at a time. | |
2553 | */ | |
1c27024e | 2554 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
36da08ef | 2555 | buf = buf->b_next) { |
d3c2ae1c GW |
2556 | ASSERT3U(bufcnt, !=, 0); |
2557 | buffers++; | |
2558 | ||
2559 | /* | |
2560 | * When the arc_buf_t is sharing the data | |
2561 | * block with the hdr, the owner of the | |
2562 | * reference belongs to the hdr. Only | |
2563 | * add to the refcount if the arc_buf_t is | |
2564 | * not shared. | |
2565 | */ | |
2aa34383 | 2566 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2567 | continue; |
d3c2ae1c | 2568 | |
424fd7c3 | 2569 | (void) zfs_refcount_remove_many( |
2aa34383 | 2570 | &old_state->arcs_size, arc_buf_size(buf), |
d3c2ae1c | 2571 | buf); |
36da08ef | 2572 | } |
d3c2ae1c | 2573 | ASSERT3U(bufcnt, ==, buffers); |
b5256303 TC |
2574 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || |
2575 | HDR_HAS_RABD(hdr)); | |
2576 | ||
2577 | if (hdr->b_l1hdr.b_pabd != NULL) { | |
424fd7c3 | 2578 | (void) zfs_refcount_remove_many( |
b5256303 TC |
2579 | &old_state->arcs_size, arc_hdr_size(hdr), |
2580 | hdr); | |
2581 | } | |
2582 | ||
2583 | if (HDR_HAS_RABD(hdr)) { | |
424fd7c3 | 2584 | (void) zfs_refcount_remove_many( |
b5256303 TC |
2585 | &old_state->arcs_size, HDR_GET_PSIZE(hdr), |
2586 | hdr); | |
2587 | } | |
36da08ef | 2588 | } |
34dc7c2f | 2589 | } |
36da08ef | 2590 | |
08532162 | 2591 | if (HDR_HAS_L1HDR(hdr)) { |
b9541d6b | 2592 | hdr->b_l1hdr.b_state = new_state; |
34dc7c2f | 2593 | |
08532162 GA |
2594 | if (HDR_HAS_L2HDR(hdr) && new_state != arc_l2c_only) { |
2595 | l2arc_hdr_arcstats_decrement_state(hdr); | |
2596 | hdr->b_l2hdr.b_arcs_state = new_state->arcs_state; | |
2597 | l2arc_hdr_arcstats_increment_state(hdr); | |
2598 | } | |
2599 | } | |
34dc7c2f BB |
2600 | } |
2601 | ||
2602 | void | |
d164b209 | 2603 | arc_space_consume(uint64_t space, arc_space_type_t type) |
34dc7c2f | 2604 | { |
d164b209 BB |
2605 | ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); |
2606 | ||
2607 | switch (type) { | |
e75c13c3 BB |
2608 | default: |
2609 | break; | |
d164b209 | 2610 | case ARC_SPACE_DATA: |
c4c162c1 | 2611 | ARCSTAT_INCR(arcstat_data_size, space); |
d164b209 | 2612 | break; |
cc7f677c | 2613 | case ARC_SPACE_META: |
c4c162c1 | 2614 | ARCSTAT_INCR(arcstat_metadata_size, space); |
cc7f677c | 2615 | break; |
25458cbe | 2616 | case ARC_SPACE_BONUS: |
c4c162c1 | 2617 | ARCSTAT_INCR(arcstat_bonus_size, space); |
25458cbe TC |
2618 | break; |
2619 | case ARC_SPACE_DNODE: | |
c4c162c1 | 2620 | aggsum_add(&arc_sums.arcstat_dnode_size, space); |
25458cbe TC |
2621 | break; |
2622 | case ARC_SPACE_DBUF: | |
c4c162c1 | 2623 | ARCSTAT_INCR(arcstat_dbuf_size, space); |
d164b209 BB |
2624 | break; |
2625 | case ARC_SPACE_HDRS: | |
c4c162c1 | 2626 | ARCSTAT_INCR(arcstat_hdr_size, space); |
d164b209 BB |
2627 | break; |
2628 | case ARC_SPACE_L2HDRS: | |
c4c162c1 | 2629 | aggsum_add(&arc_sums.arcstat_l2_hdr_size, space); |
d164b209 | 2630 | break; |
85ec5cba MA |
2631 | case ARC_SPACE_ABD_CHUNK_WASTE: |
2632 | /* | |
2633 | * Note: this includes space wasted by all scatter ABD's, not | |
2634 | * just those allocated by the ARC. But the vast majority of | |
2635 | * scatter ABD's come from the ARC, because other users are | |
2636 | * very short-lived. | |
2637 | */ | |
c4c162c1 | 2638 | ARCSTAT_INCR(arcstat_abd_chunk_waste_size, space); |
85ec5cba | 2639 | break; |
d164b209 BB |
2640 | } |
2641 | ||
85ec5cba | 2642 | if (type != ARC_SPACE_DATA && type != ARC_SPACE_ABD_CHUNK_WASTE) |
c4c162c1 | 2643 | aggsum_add(&arc_sums.arcstat_meta_used, space); |
cc7f677c | 2644 | |
c4c162c1 | 2645 | aggsum_add(&arc_sums.arcstat_size, space); |
34dc7c2f BB |
2646 | } |
2647 | ||
2648 | void | |
d164b209 | 2649 | arc_space_return(uint64_t space, arc_space_type_t type) |
34dc7c2f | 2650 | { |
d164b209 BB |
2651 | ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); |
2652 | ||
2653 | switch (type) { | |
e75c13c3 BB |
2654 | default: |
2655 | break; | |
d164b209 | 2656 | case ARC_SPACE_DATA: |
c4c162c1 | 2657 | ARCSTAT_INCR(arcstat_data_size, -space); |
d164b209 | 2658 | break; |
cc7f677c | 2659 | case ARC_SPACE_META: |
c4c162c1 | 2660 | ARCSTAT_INCR(arcstat_metadata_size, -space); |
cc7f677c | 2661 | break; |
25458cbe | 2662 | case ARC_SPACE_BONUS: |
c4c162c1 | 2663 | ARCSTAT_INCR(arcstat_bonus_size, -space); |
25458cbe TC |
2664 | break; |
2665 | case ARC_SPACE_DNODE: | |
c4c162c1 | 2666 | aggsum_add(&arc_sums.arcstat_dnode_size, -space); |
25458cbe TC |
2667 | break; |
2668 | case ARC_SPACE_DBUF: | |
c4c162c1 | 2669 | ARCSTAT_INCR(arcstat_dbuf_size, -space); |
d164b209 BB |
2670 | break; |
2671 | case ARC_SPACE_HDRS: | |
c4c162c1 | 2672 | ARCSTAT_INCR(arcstat_hdr_size, -space); |
d164b209 BB |
2673 | break; |
2674 | case ARC_SPACE_L2HDRS: | |
c4c162c1 | 2675 | aggsum_add(&arc_sums.arcstat_l2_hdr_size, -space); |
d164b209 | 2676 | break; |
85ec5cba | 2677 | case ARC_SPACE_ABD_CHUNK_WASTE: |
c4c162c1 | 2678 | ARCSTAT_INCR(arcstat_abd_chunk_waste_size, -space); |
85ec5cba | 2679 | break; |
d164b209 BB |
2680 | } |
2681 | ||
85ec5cba | 2682 | if (type != ARC_SPACE_DATA && type != ARC_SPACE_ABD_CHUNK_WASTE) { |
c4c162c1 AM |
2683 | ASSERT(aggsum_compare(&arc_sums.arcstat_meta_used, |
2684 | space) >= 0); | |
2685 | ARCSTAT_MAX(arcstat_meta_max, | |
2686 | aggsum_upper_bound(&arc_sums.arcstat_meta_used)); | |
2687 | aggsum_add(&arc_sums.arcstat_meta_used, -space); | |
cc7f677c PS |
2688 | } |
2689 | ||
c4c162c1 AM |
2690 | ASSERT(aggsum_compare(&arc_sums.arcstat_size, space) >= 0); |
2691 | aggsum_add(&arc_sums.arcstat_size, -space); | |
34dc7c2f BB |
2692 | } |
2693 | ||
d3c2ae1c | 2694 | /* |
524b4217 | 2695 | * Given a hdr and a buf, returns whether that buf can share its b_data buffer |
a6255b7f | 2696 | * with the hdr's b_pabd. |
d3c2ae1c | 2697 | */ |
524b4217 DK |
2698 | static boolean_t |
2699 | arc_can_share(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
2700 | { | |
524b4217 DK |
2701 | /* |
2702 | * The criteria for sharing a hdr's data are: | |
b5256303 TC |
2703 | * 1. the buffer is not encrypted |
2704 | * 2. the hdr's compression matches the buf's compression | |
2705 | * 3. the hdr doesn't need to be byteswapped | |
2706 | * 4. the hdr isn't already being shared | |
2707 | * 5. the buf is either compressed or it is the last buf in the hdr list | |
524b4217 | 2708 | * |
b5256303 | 2709 | * Criterion #5 maintains the invariant that shared uncompressed |
524b4217 DK |
2710 | * bufs must be the final buf in the hdr's b_buf list. Reading this, you |
2711 | * might ask, "if a compressed buf is allocated first, won't that be the | |
2712 | * last thing in the list?", but in that case it's impossible to create | |
2713 | * a shared uncompressed buf anyway (because the hdr must be compressed | |
2714 | * to have the compressed buf). You might also think that #3 is | |
2715 | * sufficient to make this guarantee, however it's possible | |
2716 | * (specifically in the rare L2ARC write race mentioned in | |
2717 | * arc_buf_alloc_impl()) there will be an existing uncompressed buf that | |
e1cfd73f | 2718 | * is shareable, but wasn't at the time of its allocation. Rather than |
524b4217 DK |
2719 | * allow a new shared uncompressed buf to be created and then shuffle |
2720 | * the list around to make it the last element, this simply disallows | |
2721 | * sharing if the new buf isn't the first to be added. | |
2722 | */ | |
2723 | ASSERT3P(buf->b_hdr, ==, hdr); | |
b5256303 TC |
2724 | boolean_t hdr_compressed = |
2725 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF; | |
a7004725 | 2726 | boolean_t buf_compressed = ARC_BUF_COMPRESSED(buf) != 0; |
b5256303 TC |
2727 | return (!ARC_BUF_ENCRYPTED(buf) && |
2728 | buf_compressed == hdr_compressed && | |
524b4217 DK |
2729 | hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS && |
2730 | !HDR_SHARED_DATA(hdr) && | |
2731 | (ARC_BUF_LAST(buf) || ARC_BUF_COMPRESSED(buf))); | |
2732 | } | |
2733 | ||
2734 | /* | |
2735 | * Allocate a buf for this hdr. If you care about the data that's in the hdr, | |
2736 | * or if you want a compressed buffer, pass those flags in. Returns 0 if the | |
2737 | * copy was made successfully, or an error code otherwise. | |
2738 | */ | |
2739 | static int | |
be9a5c35 TC |
2740 | arc_buf_alloc_impl(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb, |
2741 | void *tag, boolean_t encrypted, boolean_t compressed, boolean_t noauth, | |
524b4217 | 2742 | boolean_t fill, arc_buf_t **ret) |
34dc7c2f | 2743 | { |
34dc7c2f | 2744 | arc_buf_t *buf; |
b5256303 | 2745 | arc_fill_flags_t flags = ARC_FILL_LOCKED; |
34dc7c2f | 2746 | |
d3c2ae1c GW |
2747 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2748 | ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); | |
2749 | VERIFY(hdr->b_type == ARC_BUFC_DATA || | |
2750 | hdr->b_type == ARC_BUFC_METADATA); | |
524b4217 DK |
2751 | ASSERT3P(ret, !=, NULL); |
2752 | ASSERT3P(*ret, ==, NULL); | |
b5256303 | 2753 | IMPLY(encrypted, compressed); |
d3c2ae1c | 2754 | |
524b4217 | 2755 | buf = *ret = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); |
34dc7c2f BB |
2756 | buf->b_hdr = hdr; |
2757 | buf->b_data = NULL; | |
2aa34383 | 2758 | buf->b_next = hdr->b_l1hdr.b_buf; |
524b4217 | 2759 | buf->b_flags = 0; |
b9541d6b | 2760 | |
d3c2ae1c GW |
2761 | add_reference(hdr, tag); |
2762 | ||
2763 | /* | |
2764 | * We're about to change the hdr's b_flags. We must either | |
2765 | * hold the hash_lock or be undiscoverable. | |
2766 | */ | |
ca6c7a94 | 2767 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
2768 | |
2769 | /* | |
524b4217 | 2770 | * Only honor requests for compressed bufs if the hdr is actually |
e1cfd73f | 2771 | * compressed. This must be overridden if the buffer is encrypted since |
b5256303 | 2772 | * encrypted buffers cannot be decompressed. |
524b4217 | 2773 | */ |
b5256303 TC |
2774 | if (encrypted) { |
2775 | buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; | |
2776 | buf->b_flags |= ARC_BUF_FLAG_ENCRYPTED; | |
2777 | flags |= ARC_FILL_COMPRESSED | ARC_FILL_ENCRYPTED; | |
2778 | } else if (compressed && | |
2779 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) { | |
524b4217 | 2780 | buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; |
b5256303 TC |
2781 | flags |= ARC_FILL_COMPRESSED; |
2782 | } | |
2783 | ||
2784 | if (noauth) { | |
2785 | ASSERT0(encrypted); | |
2786 | flags |= ARC_FILL_NOAUTH; | |
2787 | } | |
524b4217 | 2788 | |
524b4217 DK |
2789 | /* |
2790 | * If the hdr's data can be shared then we share the data buffer and | |
2791 | * set the appropriate bit in the hdr's b_flags to indicate the hdr is | |
5662fd57 MA |
2792 | * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new |
2793 | * buffer to store the buf's data. | |
524b4217 | 2794 | * |
a6255b7f DQ |
2795 | * There are two additional restrictions here because we're sharing |
2796 | * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be | |
2797 | * actively involved in an L2ARC write, because if this buf is used by | |
2798 | * an arc_write() then the hdr's data buffer will be released when the | |
524b4217 | 2799 | * write completes, even though the L2ARC write might still be using it. |
a6255b7f | 2800 | * Second, the hdr's ABD must be linear so that the buf's user doesn't |
5662fd57 MA |
2801 | * need to be ABD-aware. It must be allocated via |
2802 | * zio_[data_]buf_alloc(), not as a page, because we need to be able | |
2803 | * to abd_release_ownership_of_buf(), which isn't allowed on "linear | |
2804 | * page" buffers because the ABD code needs to handle freeing them | |
2805 | * specially. | |
2806 | */ | |
2807 | boolean_t can_share = arc_can_share(hdr, buf) && | |
2808 | !HDR_L2_WRITING(hdr) && | |
2809 | hdr->b_l1hdr.b_pabd != NULL && | |
2810 | abd_is_linear(hdr->b_l1hdr.b_pabd) && | |
2811 | !abd_is_linear_page(hdr->b_l1hdr.b_pabd); | |
524b4217 DK |
2812 | |
2813 | /* Set up b_data and sharing */ | |
2814 | if (can_share) { | |
a6255b7f | 2815 | buf->b_data = abd_to_buf(hdr->b_l1hdr.b_pabd); |
524b4217 | 2816 | buf->b_flags |= ARC_BUF_FLAG_SHARED; |
d3c2ae1c GW |
2817 | arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); |
2818 | } else { | |
524b4217 DK |
2819 | buf->b_data = |
2820 | arc_get_data_buf(hdr, arc_buf_size(buf), buf); | |
2821 | ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); | |
d3c2ae1c GW |
2822 | } |
2823 | VERIFY3P(buf->b_data, !=, NULL); | |
b9541d6b CW |
2824 | |
2825 | hdr->b_l1hdr.b_buf = buf; | |
d3c2ae1c | 2826 | hdr->b_l1hdr.b_bufcnt += 1; |
b5256303 TC |
2827 | if (encrypted) |
2828 | hdr->b_crypt_hdr.b_ebufcnt += 1; | |
b9541d6b | 2829 | |
524b4217 DK |
2830 | /* |
2831 | * If the user wants the data from the hdr, we need to either copy or | |
2832 | * decompress the data. | |
2833 | */ | |
2834 | if (fill) { | |
be9a5c35 TC |
2835 | ASSERT3P(zb, !=, NULL); |
2836 | return (arc_buf_fill(buf, spa, zb, flags)); | |
524b4217 | 2837 | } |
d3c2ae1c | 2838 | |
524b4217 | 2839 | return (0); |
34dc7c2f BB |
2840 | } |
2841 | ||
9babb374 BB |
2842 | static char *arc_onloan_tag = "onloan"; |
2843 | ||
a7004725 DK |
2844 | static inline void |
2845 | arc_loaned_bytes_update(int64_t delta) | |
2846 | { | |
2847 | atomic_add_64(&arc_loaned_bytes, delta); | |
2848 | ||
2849 | /* assert that it did not wrap around */ | |
2850 | ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); | |
2851 | } | |
2852 | ||
9babb374 BB |
2853 | /* |
2854 | * Loan out an anonymous arc buffer. Loaned buffers are not counted as in | |
2855 | * flight data by arc_tempreserve_space() until they are "returned". Loaned | |
2856 | * buffers must be returned to the arc before they can be used by the DMU or | |
2857 | * freed. | |
2858 | */ | |
2859 | arc_buf_t * | |
2aa34383 | 2860 | arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size) |
9babb374 | 2861 | { |
2aa34383 DK |
2862 | arc_buf_t *buf = arc_alloc_buf(spa, arc_onloan_tag, |
2863 | is_metadata ? ARC_BUFC_METADATA : ARC_BUFC_DATA, size); | |
9babb374 | 2864 | |
5152a740 | 2865 | arc_loaned_bytes_update(arc_buf_size(buf)); |
a7004725 | 2866 | |
9babb374 BB |
2867 | return (buf); |
2868 | } | |
2869 | ||
2aa34383 DK |
2870 | arc_buf_t * |
2871 | arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize, | |
10b3c7f5 | 2872 | enum zio_compress compression_type, uint8_t complevel) |
2aa34383 DK |
2873 | { |
2874 | arc_buf_t *buf = arc_alloc_compressed_buf(spa, arc_onloan_tag, | |
10b3c7f5 | 2875 | psize, lsize, compression_type, complevel); |
2aa34383 | 2876 | |
5152a740 | 2877 | arc_loaned_bytes_update(arc_buf_size(buf)); |
a7004725 | 2878 | |
2aa34383 DK |
2879 | return (buf); |
2880 | } | |
2881 | ||
b5256303 TC |
2882 | arc_buf_t * |
2883 | arc_loan_raw_buf(spa_t *spa, uint64_t dsobj, boolean_t byteorder, | |
2884 | const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, | |
2885 | dmu_object_type_t ot, uint64_t psize, uint64_t lsize, | |
10b3c7f5 | 2886 | enum zio_compress compression_type, uint8_t complevel) |
b5256303 TC |
2887 | { |
2888 | arc_buf_t *buf = arc_alloc_raw_buf(spa, arc_onloan_tag, dsobj, | |
10b3c7f5 MN |
2889 | byteorder, salt, iv, mac, ot, psize, lsize, compression_type, |
2890 | complevel); | |
b5256303 TC |
2891 | |
2892 | atomic_add_64(&arc_loaned_bytes, psize); | |
2893 | return (buf); | |
2894 | } | |
2895 | ||
2aa34383 | 2896 | |
9babb374 BB |
2897 | /* |
2898 | * Return a loaned arc buffer to the arc. | |
2899 | */ | |
2900 | void | |
2901 | arc_return_buf(arc_buf_t *buf, void *tag) | |
2902 | { | |
2903 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
2904 | ||
d3c2ae1c | 2905 | ASSERT3P(buf->b_data, !=, NULL); |
b9541d6b | 2906 | ASSERT(HDR_HAS_L1HDR(hdr)); |
c13060e4 | 2907 | (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag); |
424fd7c3 | 2908 | (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); |
9babb374 | 2909 | |
a7004725 | 2910 | arc_loaned_bytes_update(-arc_buf_size(buf)); |
9babb374 BB |
2911 | } |
2912 | ||
428870ff BB |
2913 | /* Detach an arc_buf from a dbuf (tag) */ |
2914 | void | |
2915 | arc_loan_inuse_buf(arc_buf_t *buf, void *tag) | |
2916 | { | |
b9541d6b | 2917 | arc_buf_hdr_t *hdr = buf->b_hdr; |
428870ff | 2918 | |
d3c2ae1c | 2919 | ASSERT3P(buf->b_data, !=, NULL); |
b9541d6b | 2920 | ASSERT(HDR_HAS_L1HDR(hdr)); |
c13060e4 | 2921 | (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); |
424fd7c3 | 2922 | (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); |
428870ff | 2923 | |
a7004725 | 2924 | arc_loaned_bytes_update(arc_buf_size(buf)); |
428870ff BB |
2925 | } |
2926 | ||
d3c2ae1c | 2927 | static void |
a6255b7f | 2928 | l2arc_free_abd_on_write(abd_t *abd, size_t size, arc_buf_contents_t type) |
34dc7c2f | 2929 | { |
d3c2ae1c | 2930 | l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP); |
34dc7c2f | 2931 | |
a6255b7f | 2932 | df->l2df_abd = abd; |
d3c2ae1c GW |
2933 | df->l2df_size = size; |
2934 | df->l2df_type = type; | |
2935 | mutex_enter(&l2arc_free_on_write_mtx); | |
2936 | list_insert_head(l2arc_free_on_write, df); | |
2937 | mutex_exit(&l2arc_free_on_write_mtx); | |
2938 | } | |
428870ff | 2939 | |
d3c2ae1c | 2940 | static void |
b5256303 | 2941 | arc_hdr_free_on_write(arc_buf_hdr_t *hdr, boolean_t free_rdata) |
d3c2ae1c GW |
2942 | { |
2943 | arc_state_t *state = hdr->b_l1hdr.b_state; | |
2944 | arc_buf_contents_t type = arc_buf_type(hdr); | |
b5256303 | 2945 | uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr); |
1eb5bfa3 | 2946 | |
d3c2ae1c GW |
2947 | /* protected by hash lock, if in the hash table */ |
2948 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 | 2949 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
2950 | ASSERT(state != arc_anon && state != arc_l2c_only); |
2951 | ||
424fd7c3 | 2952 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c | 2953 | size, hdr); |
1eb5bfa3 | 2954 | } |
424fd7c3 | 2955 | (void) zfs_refcount_remove_many(&state->arcs_size, size, hdr); |
423e7b62 AG |
2956 | if (type == ARC_BUFC_METADATA) { |
2957 | arc_space_return(size, ARC_SPACE_META); | |
2958 | } else { | |
2959 | ASSERT(type == ARC_BUFC_DATA); | |
2960 | arc_space_return(size, ARC_SPACE_DATA); | |
2961 | } | |
d3c2ae1c | 2962 | |
b5256303 TC |
2963 | if (free_rdata) { |
2964 | l2arc_free_abd_on_write(hdr->b_crypt_hdr.b_rabd, size, type); | |
2965 | } else { | |
2966 | l2arc_free_abd_on_write(hdr->b_l1hdr.b_pabd, size, type); | |
2967 | } | |
34dc7c2f BB |
2968 | } |
2969 | ||
d3c2ae1c GW |
2970 | /* |
2971 | * Share the arc_buf_t's data with the hdr. Whenever we are sharing the | |
2972 | * data buffer, we transfer the refcount ownership to the hdr and update | |
2973 | * the appropriate kstats. | |
2974 | */ | |
2975 | static void | |
2976 | arc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
34dc7c2f | 2977 | { |
524b4217 | 2978 | ASSERT(arc_can_share(hdr, buf)); |
a6255b7f | 2979 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2980 | ASSERT(!ARC_BUF_ENCRYPTED(buf)); |
ca6c7a94 | 2981 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
34dc7c2f BB |
2982 | |
2983 | /* | |
d3c2ae1c GW |
2984 | * Start sharing the data buffer. We transfer the |
2985 | * refcount ownership to the hdr since it always owns | |
2986 | * the refcount whenever an arc_buf_t is shared. | |
34dc7c2f | 2987 | */ |
d7e4b30a BB |
2988 | zfs_refcount_transfer_ownership_many(&hdr->b_l1hdr.b_state->arcs_size, |
2989 | arc_hdr_size(hdr), buf, hdr); | |
a6255b7f DQ |
2990 | hdr->b_l1hdr.b_pabd = abd_get_from_buf(buf->b_data, arc_buf_size(buf)); |
2991 | abd_take_ownership_of_buf(hdr->b_l1hdr.b_pabd, | |
2992 | HDR_ISTYPE_METADATA(hdr)); | |
d3c2ae1c | 2993 | arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); |
524b4217 | 2994 | buf->b_flags |= ARC_BUF_FLAG_SHARED; |
34dc7c2f | 2995 | |
d3c2ae1c GW |
2996 | /* |
2997 | * Since we've transferred ownership to the hdr we need | |
2998 | * to increment its compressed and uncompressed kstats and | |
2999 | * decrement the overhead size. | |
3000 | */ | |
3001 | ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); | |
3002 | ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); | |
2aa34383 | 3003 | ARCSTAT_INCR(arcstat_overhead_size, -arc_buf_size(buf)); |
34dc7c2f BB |
3004 | } |
3005 | ||
ca0bf58d | 3006 | static void |
d3c2ae1c | 3007 | arc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) |
ca0bf58d | 3008 | { |
d3c2ae1c | 3009 | ASSERT(arc_buf_is_shared(buf)); |
a6255b7f | 3010 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
ca6c7a94 | 3011 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
ca0bf58d | 3012 | |
d3c2ae1c GW |
3013 | /* |
3014 | * We are no longer sharing this buffer so we need | |
3015 | * to transfer its ownership to the rightful owner. | |
3016 | */ | |
d7e4b30a BB |
3017 | zfs_refcount_transfer_ownership_many(&hdr->b_l1hdr.b_state->arcs_size, |
3018 | arc_hdr_size(hdr), hdr, buf); | |
d3c2ae1c | 3019 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); |
a6255b7f | 3020 | abd_release_ownership_of_buf(hdr->b_l1hdr.b_pabd); |
e2af2acc | 3021 | abd_free(hdr->b_l1hdr.b_pabd); |
a6255b7f | 3022 | hdr->b_l1hdr.b_pabd = NULL; |
524b4217 | 3023 | buf->b_flags &= ~ARC_BUF_FLAG_SHARED; |
d3c2ae1c GW |
3024 | |
3025 | /* | |
3026 | * Since the buffer is no longer shared between | |
3027 | * the arc buf and the hdr, count it as overhead. | |
3028 | */ | |
3029 | ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); | |
3030 | ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); | |
2aa34383 | 3031 | ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); |
ca0bf58d PS |
3032 | } |
3033 | ||
34dc7c2f | 3034 | /* |
2aa34383 DK |
3035 | * Remove an arc_buf_t from the hdr's buf list and return the last |
3036 | * arc_buf_t on the list. If no buffers remain on the list then return | |
3037 | * NULL. | |
3038 | */ | |
3039 | static arc_buf_t * | |
3040 | arc_buf_remove(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
3041 | { | |
2aa34383 | 3042 | ASSERT(HDR_HAS_L1HDR(hdr)); |
ca6c7a94 | 3043 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
2aa34383 | 3044 | |
a7004725 DK |
3045 | arc_buf_t **bufp = &hdr->b_l1hdr.b_buf; |
3046 | arc_buf_t *lastbuf = NULL; | |
3047 | ||
2aa34383 DK |
3048 | /* |
3049 | * Remove the buf from the hdr list and locate the last | |
3050 | * remaining buffer on the list. | |
3051 | */ | |
3052 | while (*bufp != NULL) { | |
3053 | if (*bufp == buf) | |
3054 | *bufp = buf->b_next; | |
3055 | ||
3056 | /* | |
3057 | * If we've removed a buffer in the middle of | |
3058 | * the list then update the lastbuf and update | |
3059 | * bufp. | |
3060 | */ | |
3061 | if (*bufp != NULL) { | |
3062 | lastbuf = *bufp; | |
3063 | bufp = &(*bufp)->b_next; | |
3064 | } | |
3065 | } | |
3066 | buf->b_next = NULL; | |
3067 | ASSERT3P(lastbuf, !=, buf); | |
3068 | IMPLY(hdr->b_l1hdr.b_bufcnt > 0, lastbuf != NULL); | |
3069 | IMPLY(hdr->b_l1hdr.b_bufcnt > 0, hdr->b_l1hdr.b_buf != NULL); | |
3070 | IMPLY(lastbuf != NULL, ARC_BUF_LAST(lastbuf)); | |
3071 | ||
3072 | return (lastbuf); | |
3073 | } | |
3074 | ||
3075 | /* | |
e1cfd73f | 3076 | * Free up buf->b_data and pull the arc_buf_t off of the arc_buf_hdr_t's |
2aa34383 | 3077 | * list and free it. |
34dc7c2f BB |
3078 | */ |
3079 | static void | |
2aa34383 | 3080 | arc_buf_destroy_impl(arc_buf_t *buf) |
34dc7c2f | 3081 | { |
498877ba | 3082 | arc_buf_hdr_t *hdr = buf->b_hdr; |
ca0bf58d PS |
3083 | |
3084 | /* | |
524b4217 DK |
3085 | * Free up the data associated with the buf but only if we're not |
3086 | * sharing this with the hdr. If we are sharing it with the hdr, the | |
3087 | * hdr is responsible for doing the free. | |
ca0bf58d | 3088 | */ |
d3c2ae1c GW |
3089 | if (buf->b_data != NULL) { |
3090 | /* | |
3091 | * We're about to change the hdr's b_flags. We must either | |
3092 | * hold the hash_lock or be undiscoverable. | |
3093 | */ | |
ca6c7a94 | 3094 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c | 3095 | |
524b4217 | 3096 | arc_cksum_verify(buf); |
d3c2ae1c GW |
3097 | arc_buf_unwatch(buf); |
3098 | ||
2aa34383 | 3099 | if (arc_buf_is_shared(buf)) { |
d3c2ae1c GW |
3100 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); |
3101 | } else { | |
2aa34383 | 3102 | uint64_t size = arc_buf_size(buf); |
d3c2ae1c GW |
3103 | arc_free_data_buf(hdr, buf->b_data, size, buf); |
3104 | ARCSTAT_INCR(arcstat_overhead_size, -size); | |
3105 | } | |
3106 | buf->b_data = NULL; | |
3107 | ||
3108 | ASSERT(hdr->b_l1hdr.b_bufcnt > 0); | |
3109 | hdr->b_l1hdr.b_bufcnt -= 1; | |
b5256303 | 3110 | |
da5d4697 | 3111 | if (ARC_BUF_ENCRYPTED(buf)) { |
b5256303 TC |
3112 | hdr->b_crypt_hdr.b_ebufcnt -= 1; |
3113 | ||
da5d4697 D |
3114 | /* |
3115 | * If we have no more encrypted buffers and we've | |
3116 | * already gotten a copy of the decrypted data we can | |
3117 | * free b_rabd to save some space. | |
3118 | */ | |
3119 | if (hdr->b_crypt_hdr.b_ebufcnt == 0 && | |
3120 | HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd != NULL && | |
3121 | !HDR_IO_IN_PROGRESS(hdr)) { | |
3122 | arc_hdr_free_abd(hdr, B_TRUE); | |
3123 | } | |
440a3eb9 | 3124 | } |
d3c2ae1c GW |
3125 | } |
3126 | ||
a7004725 | 3127 | arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); |
d3c2ae1c | 3128 | |
524b4217 | 3129 | if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) { |
2aa34383 | 3130 | /* |
524b4217 | 3131 | * If the current arc_buf_t is sharing its data buffer with the |
a6255b7f | 3132 | * hdr, then reassign the hdr's b_pabd to share it with the new |
524b4217 DK |
3133 | * buffer at the end of the list. The shared buffer is always |
3134 | * the last one on the hdr's buffer list. | |
3135 | * | |
3136 | * There is an equivalent case for compressed bufs, but since | |
3137 | * they aren't guaranteed to be the last buf in the list and | |
3138 | * that is an exceedingly rare case, we just allow that space be | |
b5256303 TC |
3139 | * wasted temporarily. We must also be careful not to share |
3140 | * encrypted buffers, since they cannot be shared. | |
2aa34383 | 3141 | */ |
b5256303 | 3142 | if (lastbuf != NULL && !ARC_BUF_ENCRYPTED(lastbuf)) { |
524b4217 | 3143 | /* Only one buf can be shared at once */ |
2aa34383 | 3144 | VERIFY(!arc_buf_is_shared(lastbuf)); |
524b4217 DK |
3145 | /* hdr is uncompressed so can't have compressed buf */ |
3146 | VERIFY(!ARC_BUF_COMPRESSED(lastbuf)); | |
d3c2ae1c | 3147 | |
a6255b7f | 3148 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
b5256303 | 3149 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c | 3150 | |
2aa34383 DK |
3151 | /* |
3152 | * We must setup a new shared block between the | |
3153 | * last buffer and the hdr. The data would have | |
3154 | * been allocated by the arc buf so we need to transfer | |
3155 | * ownership to the hdr since it's now being shared. | |
3156 | */ | |
3157 | arc_share_buf(hdr, lastbuf); | |
3158 | } | |
3159 | } else if (HDR_SHARED_DATA(hdr)) { | |
d3c2ae1c | 3160 | /* |
2aa34383 DK |
3161 | * Uncompressed shared buffers are always at the end |
3162 | * of the list. Compressed buffers don't have the | |
3163 | * same requirements. This makes it hard to | |
3164 | * simply assert that the lastbuf is shared so | |
3165 | * we rely on the hdr's compression flags to determine | |
3166 | * if we have a compressed, shared buffer. | |
d3c2ae1c | 3167 | */ |
2aa34383 DK |
3168 | ASSERT3P(lastbuf, !=, NULL); |
3169 | ASSERT(arc_buf_is_shared(lastbuf) || | |
b5256303 | 3170 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); |
ca0bf58d PS |
3171 | } |
3172 | ||
a7004725 DK |
3173 | /* |
3174 | * Free the checksum if we're removing the last uncompressed buf from | |
3175 | * this hdr. | |
3176 | */ | |
3177 | if (!arc_hdr_has_uncompressed_buf(hdr)) { | |
d3c2ae1c | 3178 | arc_cksum_free(hdr); |
a7004725 | 3179 | } |
d3c2ae1c GW |
3180 | |
3181 | /* clean up the buf */ | |
3182 | buf->b_hdr = NULL; | |
3183 | kmem_cache_free(buf_cache, buf); | |
3184 | } | |
3185 | ||
3186 | static void | |
e111c802 | 3187 | arc_hdr_alloc_abd(arc_buf_hdr_t *hdr, int alloc_flags) |
d3c2ae1c | 3188 | { |
b5256303 | 3189 | uint64_t size; |
e111c802 | 3190 | boolean_t alloc_rdata = ((alloc_flags & ARC_HDR_ALLOC_RDATA) != 0); |
b5256303 | 3191 | |
d3c2ae1c GW |
3192 | ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); |
3193 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
b5256303 TC |
3194 | ASSERT(!HDR_SHARED_DATA(hdr) || alloc_rdata); |
3195 | IMPLY(alloc_rdata, HDR_PROTECTED(hdr)); | |
d3c2ae1c | 3196 | |
b5256303 TC |
3197 | if (alloc_rdata) { |
3198 | size = HDR_GET_PSIZE(hdr); | |
3199 | ASSERT3P(hdr->b_crypt_hdr.b_rabd, ==, NULL); | |
e111c802 | 3200 | hdr->b_crypt_hdr.b_rabd = arc_get_data_abd(hdr, size, hdr, |
6b88b4b5 | 3201 | alloc_flags); |
b5256303 TC |
3202 | ASSERT3P(hdr->b_crypt_hdr.b_rabd, !=, NULL); |
3203 | ARCSTAT_INCR(arcstat_raw_size, size); | |
3204 | } else { | |
3205 | size = arc_hdr_size(hdr); | |
3206 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); | |
e111c802 | 3207 | hdr->b_l1hdr.b_pabd = arc_get_data_abd(hdr, size, hdr, |
6b88b4b5 | 3208 | alloc_flags); |
b5256303 TC |
3209 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
3210 | } | |
3211 | ||
3212 | ARCSTAT_INCR(arcstat_compressed_size, size); | |
d3c2ae1c GW |
3213 | ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); |
3214 | } | |
3215 | ||
3216 | static void | |
b5256303 | 3217 | arc_hdr_free_abd(arc_buf_hdr_t *hdr, boolean_t free_rdata) |
d3c2ae1c | 3218 | { |
b5256303 TC |
3219 | uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr); |
3220 | ||
d3c2ae1c | 3221 | ASSERT(HDR_HAS_L1HDR(hdr)); |
b5256303 TC |
3222 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); |
3223 | IMPLY(free_rdata, HDR_HAS_RABD(hdr)); | |
d3c2ae1c | 3224 | |
ca0bf58d | 3225 | /* |
d3c2ae1c GW |
3226 | * If the hdr is currently being written to the l2arc then |
3227 | * we defer freeing the data by adding it to the l2arc_free_on_write | |
3228 | * list. The l2arc will free the data once it's finished | |
3229 | * writing it to the l2arc device. | |
ca0bf58d | 3230 | */ |
d3c2ae1c | 3231 | if (HDR_L2_WRITING(hdr)) { |
b5256303 | 3232 | arc_hdr_free_on_write(hdr, free_rdata); |
d3c2ae1c | 3233 | ARCSTAT_BUMP(arcstat_l2_free_on_write); |
b5256303 TC |
3234 | } else if (free_rdata) { |
3235 | arc_free_data_abd(hdr, hdr->b_crypt_hdr.b_rabd, size, hdr); | |
d3c2ae1c | 3236 | } else { |
b5256303 | 3237 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, size, hdr); |
ca0bf58d PS |
3238 | } |
3239 | ||
b5256303 TC |
3240 | if (free_rdata) { |
3241 | hdr->b_crypt_hdr.b_rabd = NULL; | |
3242 | ARCSTAT_INCR(arcstat_raw_size, -size); | |
3243 | } else { | |
3244 | hdr->b_l1hdr.b_pabd = NULL; | |
3245 | } | |
3246 | ||
3247 | if (hdr->b_l1hdr.b_pabd == NULL && !HDR_HAS_RABD(hdr)) | |
3248 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
3249 | ||
3250 | ARCSTAT_INCR(arcstat_compressed_size, -size); | |
d3c2ae1c GW |
3251 | ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); |
3252 | } | |
3253 | ||
6b88b4b5 AM |
3254 | /* |
3255 | * Allocate empty anonymous ARC header. The header will get its identity | |
3256 | * assigned and buffers attached later as part of read or write operations. | |
3257 | * | |
3258 | * In case of read arc_read() assigns header its identify (b_dva + b_birth), | |
3259 | * inserts it into ARC hash to become globally visible and allocates physical | |
3260 | * (b_pabd) or raw (b_rabd) ABD buffer to read into from disk. On disk read | |
3261 | * completion arc_read_done() allocates ARC buffer(s) as needed, potentially | |
3262 | * sharing one of them with the physical ABD buffer. | |
3263 | * | |
3264 | * In case of write arc_alloc_buf() allocates ARC buffer to be filled with | |
3265 | * data. Then after compression and/or encryption arc_write_ready() allocates | |
3266 | * and fills (or potentially shares) physical (b_pabd) or raw (b_rabd) ABD | |
3267 | * buffer. On disk write completion arc_write_done() assigns the header its | |
3268 | * new identity (b_dva + b_birth) and inserts into ARC hash. | |
3269 | * | |
3270 | * In case of partial overwrite the old data is read first as described. Then | |
3271 | * arc_release() either allocates new anonymous ARC header and moves the ARC | |
3272 | * buffer to it, or reuses the old ARC header by discarding its identity and | |
3273 | * removing it from ARC hash. After buffer modification normal write process | |
3274 | * follows as described. | |
3275 | */ | |
d3c2ae1c GW |
3276 | static arc_buf_hdr_t * |
3277 | arc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize, | |
10b3c7f5 | 3278 | boolean_t protected, enum zio_compress compression_type, uint8_t complevel, |
6b88b4b5 | 3279 | arc_buf_contents_t type) |
d3c2ae1c GW |
3280 | { |
3281 | arc_buf_hdr_t *hdr; | |
3282 | ||
d3c2ae1c | 3283 | VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA); |
b5256303 TC |
3284 | if (protected) { |
3285 | hdr = kmem_cache_alloc(hdr_full_crypt_cache, KM_PUSHPAGE); | |
3286 | } else { | |
3287 | hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); | |
3288 | } | |
d3c2ae1c | 3289 | |
d3c2ae1c GW |
3290 | ASSERT(HDR_EMPTY(hdr)); |
3291 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
3292 | HDR_SET_PSIZE(hdr, psize); | |
3293 | HDR_SET_LSIZE(hdr, lsize); | |
3294 | hdr->b_spa = spa; | |
3295 | hdr->b_type = type; | |
3296 | hdr->b_flags = 0; | |
3297 | arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR); | |
2aa34383 | 3298 | arc_hdr_set_compress(hdr, compression_type); |
10b3c7f5 | 3299 | hdr->b_complevel = complevel; |
b5256303 TC |
3300 | if (protected) |
3301 | arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED); | |
ca0bf58d | 3302 | |
d3c2ae1c GW |
3303 | hdr->b_l1hdr.b_state = arc_anon; |
3304 | hdr->b_l1hdr.b_arc_access = 0; | |
cfe8e960 AM |
3305 | hdr->b_l1hdr.b_mru_hits = 0; |
3306 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
3307 | hdr->b_l1hdr.b_mfu_hits = 0; | |
3308 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
d3c2ae1c GW |
3309 | hdr->b_l1hdr.b_bufcnt = 0; |
3310 | hdr->b_l1hdr.b_buf = NULL; | |
ca0bf58d | 3311 | |
424fd7c3 | 3312 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
ca0bf58d | 3313 | |
d3c2ae1c | 3314 | return (hdr); |
ca0bf58d PS |
3315 | } |
3316 | ||
bd089c54 | 3317 | /* |
d3c2ae1c GW |
3318 | * Transition between the two allocation states for the arc_buf_hdr struct. |
3319 | * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without | |
3320 | * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller | |
3321 | * version is used when a cache buffer is only in the L2ARC in order to reduce | |
3322 | * memory usage. | |
bd089c54 | 3323 | */ |
d3c2ae1c GW |
3324 | static arc_buf_hdr_t * |
3325 | arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) | |
34dc7c2f | 3326 | { |
1c27024e DB |
3327 | ASSERT(HDR_HAS_L2HDR(hdr)); |
3328 | ||
d3c2ae1c GW |
3329 | arc_buf_hdr_t *nhdr; |
3330 | l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; | |
34dc7c2f | 3331 | |
d3c2ae1c GW |
3332 | ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || |
3333 | (old == hdr_l2only_cache && new == hdr_full_cache)); | |
34dc7c2f | 3334 | |
b5256303 TC |
3335 | /* |
3336 | * if the caller wanted a new full header and the header is to be | |
3337 | * encrypted we will actually allocate the header from the full crypt | |
3338 | * cache instead. The same applies to freeing from the old cache. | |
3339 | */ | |
3340 | if (HDR_PROTECTED(hdr) && new == hdr_full_cache) | |
3341 | new = hdr_full_crypt_cache; | |
3342 | if (HDR_PROTECTED(hdr) && old == hdr_full_cache) | |
3343 | old = hdr_full_crypt_cache; | |
3344 | ||
d3c2ae1c | 3345 | nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); |
428870ff | 3346 | |
d3c2ae1c GW |
3347 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); |
3348 | buf_hash_remove(hdr); | |
ca0bf58d | 3349 | |
d3c2ae1c | 3350 | bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); |
34dc7c2f | 3351 | |
b5256303 | 3352 | if (new == hdr_full_cache || new == hdr_full_crypt_cache) { |
d3c2ae1c GW |
3353 | arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR); |
3354 | /* | |
3355 | * arc_access and arc_change_state need to be aware that a | |
3356 | * header has just come out of L2ARC, so we set its state to | |
3357 | * l2c_only even though it's about to change. | |
3358 | */ | |
3359 | nhdr->b_l1hdr.b_state = arc_l2c_only; | |
34dc7c2f | 3360 | |
d3c2ae1c | 3361 | /* Verify previous threads set to NULL before freeing */ |
a6255b7f | 3362 | ASSERT3P(nhdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 3363 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
3364 | } else { |
3365 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
3366 | ASSERT0(hdr->b_l1hdr.b_bufcnt); | |
3367 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
36da08ef | 3368 | |
d3c2ae1c GW |
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)); | |
1eb5bfa3 GW |
3377 | |
3378 | /* | |
d3c2ae1c GW |
3379 | * A buffer must not be moved into the arc_l2c_only |
3380 | * state if it's not finished being written out to the | |
a6255b7f | 3381 | * l2arc device. Otherwise, the b_l1hdr.b_pabd field |
d3c2ae1c | 3382 | * might try to be accessed, even though it was removed. |
1eb5bfa3 | 3383 | */ |
d3c2ae1c | 3384 | VERIFY(!HDR_L2_WRITING(hdr)); |
a6255b7f | 3385 | VERIFY3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 3386 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
3387 | |
3388 | arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR); | |
34dc7c2f | 3389 | } |
d3c2ae1c GW |
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); | |
34dc7c2f | 3395 | |
d3c2ae1c | 3396 | ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); |
34dc7c2f | 3397 | |
d3c2ae1c GW |
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); | |
34dc7c2f | 3408 | |
d3c2ae1c | 3409 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 3410 | |
d3c2ae1c GW |
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 | ||
424fd7c3 TS |
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); | |
d3c2ae1c GW |
3423 | |
3424 | buf_discard_identity(hdr); | |
3425 | kmem_cache_free(old, hdr); | |
3426 | ||
3427 | return (nhdr); | |
3428 | } | |
3429 | ||
b5256303 TC |
3430 | /* |
3431 | * This function allows an L1 header to be reallocated as a crypt | |
3432 | * header and vice versa. If we are going to a crypt header, the | |
3433 | * new fields will be zeroed out. | |
3434 | */ | |
3435 | static arc_buf_hdr_t * | |
3436 | arc_hdr_realloc_crypt(arc_buf_hdr_t *hdr, boolean_t need_crypt) | |
3437 | { | |
3438 | arc_buf_hdr_t *nhdr; | |
3439 | arc_buf_t *buf; | |
3440 | kmem_cache_t *ncache, *ocache; | |
3441 | ||
b7ddeaef TC |
3442 | /* |
3443 | * This function requires that hdr is in the arc_anon state. | |
3444 | * Therefore it won't have any L2ARC data for us to worry | |
3445 | * about copying. | |
3446 | */ | |
b5256303 | 3447 | ASSERT(HDR_HAS_L1HDR(hdr)); |
b7ddeaef | 3448 | ASSERT(!HDR_HAS_L2HDR(hdr)); |
b5256303 TC |
3449 | ASSERT3U(!!HDR_PROTECTED(hdr), !=, need_crypt); |
3450 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); | |
3451 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
b7ddeaef TC |
3452 | ASSERT(!list_link_active(&hdr->b_l2hdr.b_l2node)); |
3453 | ASSERT3P(hdr->b_hash_next, ==, NULL); | |
b5256303 TC |
3454 | |
3455 | if (need_crypt) { | |
3456 | ncache = hdr_full_crypt_cache; | |
3457 | ocache = hdr_full_cache; | |
3458 | } else { | |
3459 | ncache = hdr_full_cache; | |
3460 | ocache = hdr_full_crypt_cache; | |
3461 | } | |
3462 | ||
3463 | nhdr = kmem_cache_alloc(ncache, KM_PUSHPAGE); | |
b7ddeaef TC |
3464 | |
3465 | /* | |
3466 | * Copy all members that aren't locks or condvars to the new header. | |
3467 | * No lists are pointing to us (as we asserted above), so we don't | |
3468 | * need to worry about the list nodes. | |
3469 | */ | |
3470 | nhdr->b_dva = hdr->b_dva; | |
3471 | nhdr->b_birth = hdr->b_birth; | |
3472 | nhdr->b_type = hdr->b_type; | |
3473 | nhdr->b_flags = hdr->b_flags; | |
3474 | nhdr->b_psize = hdr->b_psize; | |
3475 | nhdr->b_lsize = hdr->b_lsize; | |
3476 | nhdr->b_spa = hdr->b_spa; | |
b5256303 TC |
3477 | nhdr->b_l1hdr.b_freeze_cksum = hdr->b_l1hdr.b_freeze_cksum; |
3478 | nhdr->b_l1hdr.b_bufcnt = hdr->b_l1hdr.b_bufcnt; | |
3479 | nhdr->b_l1hdr.b_byteswap = hdr->b_l1hdr.b_byteswap; | |
3480 | nhdr->b_l1hdr.b_state = hdr->b_l1hdr.b_state; | |
3481 | nhdr->b_l1hdr.b_arc_access = hdr->b_l1hdr.b_arc_access; | |
3482 | nhdr->b_l1hdr.b_mru_hits = hdr->b_l1hdr.b_mru_hits; | |
3483 | nhdr->b_l1hdr.b_mru_ghost_hits = hdr->b_l1hdr.b_mru_ghost_hits; | |
3484 | nhdr->b_l1hdr.b_mfu_hits = hdr->b_l1hdr.b_mfu_hits; | |
3485 | nhdr->b_l1hdr.b_mfu_ghost_hits = hdr->b_l1hdr.b_mfu_ghost_hits; | |
b5256303 TC |
3486 | nhdr->b_l1hdr.b_acb = hdr->b_l1hdr.b_acb; |
3487 | nhdr->b_l1hdr.b_pabd = hdr->b_l1hdr.b_pabd; | |
b5256303 TC |
3488 | |
3489 | /* | |
c13060e4 | 3490 | * This zfs_refcount_add() exists only to ensure that the individual |
b5256303 TC |
3491 | * arc buffers always point to a header that is referenced, avoiding |
3492 | * a small race condition that could trigger ASSERTs. | |
3493 | */ | |
c13060e4 | 3494 | (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, FTAG); |
b7ddeaef | 3495 | nhdr->b_l1hdr.b_buf = hdr->b_l1hdr.b_buf; |
b5256303 TC |
3496 | for (buf = nhdr->b_l1hdr.b_buf; buf != NULL; buf = buf->b_next) { |
3497 | mutex_enter(&buf->b_evict_lock); | |
3498 | buf->b_hdr = nhdr; | |
3499 | mutex_exit(&buf->b_evict_lock); | |
3500 | } | |
3501 | ||
424fd7c3 TS |
3502 | zfs_refcount_transfer(&nhdr->b_l1hdr.b_refcnt, &hdr->b_l1hdr.b_refcnt); |
3503 | (void) zfs_refcount_remove(&nhdr->b_l1hdr.b_refcnt, FTAG); | |
3504 | ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt)); | |
b5256303 TC |
3505 | |
3506 | if (need_crypt) { | |
3507 | arc_hdr_set_flags(nhdr, ARC_FLAG_PROTECTED); | |
3508 | } else { | |
3509 | arc_hdr_clear_flags(nhdr, ARC_FLAG_PROTECTED); | |
3510 | } | |
3511 | ||
b7ddeaef TC |
3512 | /* unset all members of the original hdr */ |
3513 | bzero(&hdr->b_dva, sizeof (dva_t)); | |
3514 | hdr->b_birth = 0; | |
3515 | hdr->b_type = ARC_BUFC_INVALID; | |
3516 | hdr->b_flags = 0; | |
3517 | hdr->b_psize = 0; | |
3518 | hdr->b_lsize = 0; | |
3519 | hdr->b_spa = 0; | |
3520 | hdr->b_l1hdr.b_freeze_cksum = NULL; | |
3521 | hdr->b_l1hdr.b_buf = NULL; | |
3522 | hdr->b_l1hdr.b_bufcnt = 0; | |
3523 | hdr->b_l1hdr.b_byteswap = 0; | |
3524 | hdr->b_l1hdr.b_state = NULL; | |
3525 | hdr->b_l1hdr.b_arc_access = 0; | |
3526 | hdr->b_l1hdr.b_mru_hits = 0; | |
3527 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
3528 | hdr->b_l1hdr.b_mfu_hits = 0; | |
3529 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
b7ddeaef TC |
3530 | hdr->b_l1hdr.b_acb = NULL; |
3531 | hdr->b_l1hdr.b_pabd = NULL; | |
3532 | ||
3533 | if (ocache == hdr_full_crypt_cache) { | |
3534 | ASSERT(!HDR_HAS_RABD(hdr)); | |
3535 | hdr->b_crypt_hdr.b_ot = DMU_OT_NONE; | |
3536 | hdr->b_crypt_hdr.b_ebufcnt = 0; | |
3537 | hdr->b_crypt_hdr.b_dsobj = 0; | |
3538 | bzero(hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); | |
3539 | bzero(hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); | |
3540 | bzero(hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); | |
3541 | } | |
3542 | ||
b5256303 TC |
3543 | buf_discard_identity(hdr); |
3544 | kmem_cache_free(ocache, hdr); | |
3545 | ||
3546 | return (nhdr); | |
3547 | } | |
3548 | ||
3549 | /* | |
3550 | * This function is used by the send / receive code to convert a newly | |
3551 | * allocated arc_buf_t to one that is suitable for a raw encrypted write. It | |
e1cfd73f | 3552 | * is also used to allow the root objset block to be updated without altering |
b5256303 TC |
3553 | * its embedded MACs. Both block types will always be uncompressed so we do not |
3554 | * have to worry about compression type or psize. | |
3555 | */ | |
3556 | void | |
3557 | arc_convert_to_raw(arc_buf_t *buf, uint64_t dsobj, boolean_t byteorder, | |
3558 | dmu_object_type_t ot, const uint8_t *salt, const uint8_t *iv, | |
3559 | const uint8_t *mac) | |
3560 | { | |
3561 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
3562 | ||
3563 | ASSERT(ot == DMU_OT_DNODE || ot == DMU_OT_OBJSET); | |
3564 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
3565 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); | |
3566 | ||
3567 | buf->b_flags |= (ARC_BUF_FLAG_COMPRESSED | ARC_BUF_FLAG_ENCRYPTED); | |
3568 | if (!HDR_PROTECTED(hdr)) | |
3569 | hdr = arc_hdr_realloc_crypt(hdr, B_TRUE); | |
3570 | hdr->b_crypt_hdr.b_dsobj = dsobj; | |
3571 | hdr->b_crypt_hdr.b_ot = ot; | |
3572 | hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ? | |
3573 | DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot); | |
3574 | if (!arc_hdr_has_uncompressed_buf(hdr)) | |
3575 | arc_cksum_free(hdr); | |
3576 | ||
3577 | if (salt != NULL) | |
3578 | bcopy(salt, hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); | |
3579 | if (iv != NULL) | |
3580 | bcopy(iv, hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); | |
3581 | if (mac != NULL) | |
3582 | bcopy(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); | |
3583 | } | |
3584 | ||
d3c2ae1c GW |
3585 | /* |
3586 | * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller. | |
3587 | * The buf is returned thawed since we expect the consumer to modify it. | |
3588 | */ | |
3589 | arc_buf_t * | |
2aa34383 | 3590 | arc_alloc_buf(spa_t *spa, void *tag, arc_buf_contents_t type, int32_t size) |
d3c2ae1c | 3591 | { |
d3c2ae1c | 3592 | arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size, |
6b88b4b5 | 3593 | B_FALSE, ZIO_COMPRESS_OFF, 0, type); |
2aa34383 | 3594 | |
a7004725 | 3595 | arc_buf_t *buf = NULL; |
be9a5c35 | 3596 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, B_FALSE, |
b5256303 | 3597 | B_FALSE, B_FALSE, &buf)); |
d3c2ae1c | 3598 | arc_buf_thaw(buf); |
2aa34383 DK |
3599 | |
3600 | return (buf); | |
3601 | } | |
3602 | ||
3603 | /* | |
3604 | * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this | |
3605 | * for bufs containing metadata. | |
3606 | */ | |
3607 | arc_buf_t * | |
3608 | arc_alloc_compressed_buf(spa_t *spa, void *tag, uint64_t psize, uint64_t lsize, | |
10b3c7f5 | 3609 | enum zio_compress compression_type, uint8_t complevel) |
2aa34383 | 3610 | { |
2aa34383 DK |
3611 | ASSERT3U(lsize, >, 0); |
3612 | ASSERT3U(lsize, >=, psize); | |
b5256303 TC |
3613 | ASSERT3U(compression_type, >, ZIO_COMPRESS_OFF); |
3614 | ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS); | |
2aa34383 | 3615 | |
a7004725 | 3616 | arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, |
6b88b4b5 | 3617 | B_FALSE, compression_type, complevel, ARC_BUFC_DATA); |
2aa34383 | 3618 | |
a7004725 | 3619 | arc_buf_t *buf = NULL; |
be9a5c35 | 3620 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, |
b5256303 | 3621 | B_TRUE, B_FALSE, B_FALSE, &buf)); |
2aa34383 DK |
3622 | arc_buf_thaw(buf); |
3623 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
3624 | ||
6b88b4b5 AM |
3625 | /* |
3626 | * To ensure that the hdr has the correct data in it if we call | |
3627 | * arc_untransform() on this buf before it's been written to disk, | |
3628 | * it's easiest if we just set up sharing between the buf and the hdr. | |
3629 | */ | |
3630 | arc_share_buf(hdr, buf); | |
a6255b7f | 3631 | |
d3c2ae1c | 3632 | return (buf); |
34dc7c2f BB |
3633 | } |
3634 | ||
b5256303 TC |
3635 | arc_buf_t * |
3636 | arc_alloc_raw_buf(spa_t *spa, void *tag, uint64_t dsobj, boolean_t byteorder, | |
3637 | const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, | |
3638 | dmu_object_type_t ot, uint64_t psize, uint64_t lsize, | |
10b3c7f5 | 3639 | enum zio_compress compression_type, uint8_t complevel) |
b5256303 TC |
3640 | { |
3641 | arc_buf_hdr_t *hdr; | |
3642 | arc_buf_t *buf; | |
3643 | arc_buf_contents_t type = DMU_OT_IS_METADATA(ot) ? | |
3644 | ARC_BUFC_METADATA : ARC_BUFC_DATA; | |
3645 | ||
3646 | ASSERT3U(lsize, >, 0); | |
3647 | ASSERT3U(lsize, >=, psize); | |
3648 | ASSERT3U(compression_type, >=, ZIO_COMPRESS_OFF); | |
3649 | ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS); | |
3650 | ||
3651 | hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, B_TRUE, | |
6b88b4b5 | 3652 | compression_type, complevel, type); |
b5256303 TC |
3653 | |
3654 | hdr->b_crypt_hdr.b_dsobj = dsobj; | |
3655 | hdr->b_crypt_hdr.b_ot = ot; | |
3656 | hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ? | |
3657 | DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot); | |
3658 | bcopy(salt, hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); | |
3659 | bcopy(iv, hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); | |
3660 | bcopy(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); | |
3661 | ||
3662 | /* | |
3663 | * This buffer will be considered encrypted even if the ot is not an | |
3664 | * encrypted type. It will become authenticated instead in | |
3665 | * arc_write_ready(). | |
3666 | */ | |
3667 | buf = NULL; | |
be9a5c35 | 3668 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_TRUE, B_TRUE, |
b5256303 TC |
3669 | B_FALSE, B_FALSE, &buf)); |
3670 | arc_buf_thaw(buf); | |
3671 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
3672 | ||
3673 | return (buf); | |
3674 | } | |
3675 | ||
08532162 GA |
3676 | static void |
3677 | l2arc_hdr_arcstats_update(arc_buf_hdr_t *hdr, boolean_t incr, | |
3678 | boolean_t state_only) | |
3679 | { | |
3680 | l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; | |
3681 | l2arc_dev_t *dev = l2hdr->b_dev; | |
3682 | uint64_t lsize = HDR_GET_LSIZE(hdr); | |
3683 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
3684 | uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
3685 | arc_buf_contents_t type = hdr->b_type; | |
3686 | int64_t lsize_s; | |
3687 | int64_t psize_s; | |
3688 | int64_t asize_s; | |
3689 | ||
3690 | if (incr) { | |
3691 | lsize_s = lsize; | |
3692 | psize_s = psize; | |
3693 | asize_s = asize; | |
3694 | } else { | |
3695 | lsize_s = -lsize; | |
3696 | psize_s = -psize; | |
3697 | asize_s = -asize; | |
3698 | } | |
3699 | ||
3700 | /* If the buffer is a prefetch, count it as such. */ | |
3701 | if (HDR_PREFETCH(hdr)) { | |
3702 | ARCSTAT_INCR(arcstat_l2_prefetch_asize, asize_s); | |
3703 | } else { | |
3704 | /* | |
3705 | * We use the value stored in the L2 header upon initial | |
3706 | * caching in L2ARC. This value will be updated in case | |
3707 | * an MRU/MRU_ghost buffer transitions to MFU but the L2ARC | |
3708 | * metadata (log entry) cannot currently be updated. Having | |
3709 | * the ARC state in the L2 header solves the problem of a | |
3710 | * possibly absent L1 header (apparent in buffers restored | |
3711 | * from persistent L2ARC). | |
3712 | */ | |
3713 | switch (hdr->b_l2hdr.b_arcs_state) { | |
3714 | case ARC_STATE_MRU_GHOST: | |
3715 | case ARC_STATE_MRU: | |
3716 | ARCSTAT_INCR(arcstat_l2_mru_asize, asize_s); | |
3717 | break; | |
3718 | case ARC_STATE_MFU_GHOST: | |
3719 | case ARC_STATE_MFU: | |
3720 | ARCSTAT_INCR(arcstat_l2_mfu_asize, asize_s); | |
3721 | break; | |
3722 | default: | |
3723 | break; | |
3724 | } | |
3725 | } | |
3726 | ||
3727 | if (state_only) | |
3728 | return; | |
3729 | ||
3730 | ARCSTAT_INCR(arcstat_l2_psize, psize_s); | |
3731 | ARCSTAT_INCR(arcstat_l2_lsize, lsize_s); | |
3732 | ||
3733 | switch (type) { | |
3734 | case ARC_BUFC_DATA: | |
3735 | ARCSTAT_INCR(arcstat_l2_bufc_data_asize, asize_s); | |
3736 | break; | |
3737 | case ARC_BUFC_METADATA: | |
3738 | ARCSTAT_INCR(arcstat_l2_bufc_metadata_asize, asize_s); | |
3739 | break; | |
3740 | default: | |
3741 | break; | |
3742 | } | |
3743 | } | |
3744 | ||
3745 | ||
d962d5da PS |
3746 | static void |
3747 | arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) | |
3748 | { | |
3749 | l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; | |
3750 | l2arc_dev_t *dev = l2hdr->b_dev; | |
7558997d SD |
3751 | uint64_t psize = HDR_GET_PSIZE(hdr); |
3752 | uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
d962d5da PS |
3753 | |
3754 | ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); | |
3755 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
3756 | ||
3757 | list_remove(&dev->l2ad_buflist, hdr); | |
3758 | ||
08532162 | 3759 | l2arc_hdr_arcstats_decrement(hdr); |
7558997d | 3760 | vdev_space_update(dev->l2ad_vdev, -asize, 0, 0); |
d962d5da | 3761 | |
7558997d SD |
3762 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr), |
3763 | hdr); | |
d3c2ae1c | 3764 | arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); |
d962d5da PS |
3765 | } |
3766 | ||
34dc7c2f BB |
3767 | static void |
3768 | arc_hdr_destroy(arc_buf_hdr_t *hdr) | |
3769 | { | |
b9541d6b CW |
3770 | if (HDR_HAS_L1HDR(hdr)) { |
3771 | ASSERT(hdr->b_l1hdr.b_buf == NULL || | |
d3c2ae1c | 3772 | hdr->b_l1hdr.b_bufcnt > 0); |
424fd7c3 | 3773 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
b9541d6b CW |
3774 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
3775 | } | |
34dc7c2f | 3776 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b CW |
3777 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); |
3778 | ||
3779 | if (HDR_HAS_L2HDR(hdr)) { | |
d962d5da PS |
3780 | l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; |
3781 | boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); | |
428870ff | 3782 | |
d962d5da PS |
3783 | if (!buflist_held) |
3784 | mutex_enter(&dev->l2ad_mtx); | |
b9541d6b | 3785 | |
ca0bf58d | 3786 | /* |
d962d5da PS |
3787 | * Even though we checked this conditional above, we |
3788 | * need to check this again now that we have the | |
3789 | * l2ad_mtx. This is because we could be racing with | |
3790 | * another thread calling l2arc_evict() which might have | |
3791 | * destroyed this header's L2 portion as we were waiting | |
3792 | * to acquire the l2ad_mtx. If that happens, we don't | |
3793 | * want to re-destroy the header's L2 portion. | |
ca0bf58d | 3794 | */ |
2a49ebbb GA |
3795 | if (HDR_HAS_L2HDR(hdr)) { |
3796 | ||
3797 | if (!HDR_EMPTY(hdr)) | |
3798 | buf_discard_identity(hdr); | |
3799 | ||
d962d5da | 3800 | arc_hdr_l2hdr_destroy(hdr); |
2a49ebbb | 3801 | } |
428870ff BB |
3802 | |
3803 | if (!buflist_held) | |
d962d5da | 3804 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
3805 | } |
3806 | ||
ca6c7a94 BB |
3807 | /* |
3808 | * The header's identify can only be safely discarded once it is no | |
3809 | * longer discoverable. This requires removing it from the hash table | |
3810 | * and the l2arc header list. After this point the hash lock can not | |
3811 | * be used to protect the header. | |
3812 | */ | |
3813 | if (!HDR_EMPTY(hdr)) | |
3814 | buf_discard_identity(hdr); | |
3815 | ||
d3c2ae1c GW |
3816 | if (HDR_HAS_L1HDR(hdr)) { |
3817 | arc_cksum_free(hdr); | |
b9541d6b | 3818 | |
d3c2ae1c | 3819 | while (hdr->b_l1hdr.b_buf != NULL) |
2aa34383 | 3820 | arc_buf_destroy_impl(hdr->b_l1hdr.b_buf); |
34dc7c2f | 3821 | |
ca6c7a94 | 3822 | if (hdr->b_l1hdr.b_pabd != NULL) |
b5256303 | 3823 | arc_hdr_free_abd(hdr, B_FALSE); |
b5256303 | 3824 | |
440a3eb9 | 3825 | if (HDR_HAS_RABD(hdr)) |
b5256303 | 3826 | arc_hdr_free_abd(hdr, B_TRUE); |
b9541d6b CW |
3827 | } |
3828 | ||
34dc7c2f | 3829 | ASSERT3P(hdr->b_hash_next, ==, NULL); |
b9541d6b | 3830 | if (HDR_HAS_L1HDR(hdr)) { |
ca0bf58d | 3831 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b | 3832 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
b5256303 TC |
3833 | |
3834 | if (!HDR_PROTECTED(hdr)) { | |
3835 | kmem_cache_free(hdr_full_cache, hdr); | |
3836 | } else { | |
3837 | kmem_cache_free(hdr_full_crypt_cache, hdr); | |
3838 | } | |
b9541d6b CW |
3839 | } else { |
3840 | kmem_cache_free(hdr_l2only_cache, hdr); | |
3841 | } | |
34dc7c2f BB |
3842 | } |
3843 | ||
3844 | void | |
d3c2ae1c | 3845 | arc_buf_destroy(arc_buf_t *buf, void* tag) |
34dc7c2f BB |
3846 | { |
3847 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
34dc7c2f | 3848 | |
b9541d6b | 3849 | if (hdr->b_l1hdr.b_state == arc_anon) { |
d3c2ae1c GW |
3850 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); |
3851 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
3852 | VERIFY0(remove_reference(hdr, NULL, tag)); | |
3853 | arc_hdr_destroy(hdr); | |
3854 | return; | |
34dc7c2f BB |
3855 | } |
3856 | ||
ca6c7a94 | 3857 | kmutex_t *hash_lock = HDR_LOCK(hdr); |
34dc7c2f | 3858 | mutex_enter(hash_lock); |
ca6c7a94 | 3859 | |
d3c2ae1c GW |
3860 | ASSERT3P(hdr, ==, buf->b_hdr); |
3861 | ASSERT(hdr->b_l1hdr.b_bufcnt > 0); | |
428870ff | 3862 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
d3c2ae1c GW |
3863 | ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon); |
3864 | ASSERT3P(buf->b_data, !=, NULL); | |
34dc7c2f BB |
3865 | |
3866 | (void) remove_reference(hdr, hash_lock, tag); | |
2aa34383 | 3867 | arc_buf_destroy_impl(buf); |
34dc7c2f | 3868 | mutex_exit(hash_lock); |
34dc7c2f BB |
3869 | } |
3870 | ||
34dc7c2f | 3871 | /* |
ca0bf58d PS |
3872 | * Evict the arc_buf_hdr that is provided as a parameter. The resultant |
3873 | * state of the header is dependent on its state prior to entering this | |
3874 | * function. The following transitions are possible: | |
34dc7c2f | 3875 | * |
ca0bf58d PS |
3876 | * - arc_mru -> arc_mru_ghost |
3877 | * - arc_mfu -> arc_mfu_ghost | |
3878 | * - arc_mru_ghost -> arc_l2c_only | |
3879 | * - arc_mru_ghost -> deleted | |
3880 | * - arc_mfu_ghost -> arc_l2c_only | |
3881 | * - arc_mfu_ghost -> deleted | |
f7de776d AM |
3882 | * |
3883 | * Return total size of evicted data buffers for eviction progress tracking. | |
3884 | * When evicting from ghost states return logical buffer size to make eviction | |
3885 | * progress at the same (or at least comparable) rate as from non-ghost states. | |
3886 | * | |
3887 | * Return *real_evicted for actual ARC size reduction to wake up threads | |
3888 | * waiting for it. For non-ghost states it includes size of evicted data | |
3889 | * buffers (the headers are not freed there). For ghost states it includes | |
3890 | * only the evicted headers size. | |
34dc7c2f | 3891 | */ |
ca0bf58d | 3892 | static int64_t |
f7de776d | 3893 | arc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, uint64_t *real_evicted) |
34dc7c2f | 3894 | { |
ca0bf58d PS |
3895 | arc_state_t *evicted_state, *state; |
3896 | int64_t bytes_evicted = 0; | |
d4a72f23 TC |
3897 | int min_lifetime = HDR_PRESCIENT_PREFETCH(hdr) ? |
3898 | arc_min_prescient_prefetch_ms : arc_min_prefetch_ms; | |
34dc7c2f | 3899 | |
ca0bf58d PS |
3900 | ASSERT(MUTEX_HELD(hash_lock)); |
3901 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
e8b96c60 | 3902 | |
f7de776d | 3903 | *real_evicted = 0; |
ca0bf58d PS |
3904 | state = hdr->b_l1hdr.b_state; |
3905 | if (GHOST_STATE(state)) { | |
3906 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
d3c2ae1c | 3907 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
e8b96c60 MA |
3908 | |
3909 | /* | |
ca0bf58d | 3910 | * l2arc_write_buffers() relies on a header's L1 portion |
a6255b7f | 3911 | * (i.e. its b_pabd field) during it's write phase. |
ca0bf58d PS |
3912 | * Thus, we cannot push a header onto the arc_l2c_only |
3913 | * state (removing its L1 piece) until the header is | |
3914 | * done being written to the l2arc. | |
e8b96c60 | 3915 | */ |
ca0bf58d PS |
3916 | if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { |
3917 | ARCSTAT_BUMP(arcstat_evict_l2_skip); | |
3918 | return (bytes_evicted); | |
e8b96c60 MA |
3919 | } |
3920 | ||
ca0bf58d | 3921 | ARCSTAT_BUMP(arcstat_deleted); |
d3c2ae1c | 3922 | bytes_evicted += HDR_GET_LSIZE(hdr); |
428870ff | 3923 | |
ca0bf58d | 3924 | DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); |
428870ff | 3925 | |
ca0bf58d | 3926 | if (HDR_HAS_L2HDR(hdr)) { |
a6255b7f | 3927 | ASSERT(hdr->b_l1hdr.b_pabd == NULL); |
b5256303 | 3928 | ASSERT(!HDR_HAS_RABD(hdr)); |
ca0bf58d PS |
3929 | /* |
3930 | * This buffer is cached on the 2nd Level ARC; | |
3931 | * don't destroy the header. | |
3932 | */ | |
3933 | arc_change_state(arc_l2c_only, hdr, hash_lock); | |
3934 | /* | |
3935 | * dropping from L1+L2 cached to L2-only, | |
3936 | * realloc to remove the L1 header. | |
3937 | */ | |
3938 | hdr = arc_hdr_realloc(hdr, hdr_full_cache, | |
3939 | hdr_l2only_cache); | |
f7de776d | 3940 | *real_evicted += HDR_FULL_SIZE - HDR_L2ONLY_SIZE; |
34dc7c2f | 3941 | } else { |
ca0bf58d PS |
3942 | arc_change_state(arc_anon, hdr, hash_lock); |
3943 | arc_hdr_destroy(hdr); | |
f7de776d | 3944 | *real_evicted += HDR_FULL_SIZE; |
34dc7c2f | 3945 | } |
ca0bf58d | 3946 | return (bytes_evicted); |
34dc7c2f BB |
3947 | } |
3948 | ||
ca0bf58d PS |
3949 | ASSERT(state == arc_mru || state == arc_mfu); |
3950 | evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; | |
34dc7c2f | 3951 | |
ca0bf58d PS |
3952 | /* prefetch buffers have a minimum lifespan */ |
3953 | if (HDR_IO_IN_PROGRESS(hdr) || | |
3954 | ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && | |
2b84817f TC |
3955 | ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < |
3956 | MSEC_TO_TICK(min_lifetime))) { | |
ca0bf58d PS |
3957 | ARCSTAT_BUMP(arcstat_evict_skip); |
3958 | return (bytes_evicted); | |
da8ccd0e PS |
3959 | } |
3960 | ||
424fd7c3 | 3961 | ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt)); |
ca0bf58d PS |
3962 | while (hdr->b_l1hdr.b_buf) { |
3963 | arc_buf_t *buf = hdr->b_l1hdr.b_buf; | |
3964 | if (!mutex_tryenter(&buf->b_evict_lock)) { | |
3965 | ARCSTAT_BUMP(arcstat_mutex_miss); | |
3966 | break; | |
3967 | } | |
f7de776d | 3968 | if (buf->b_data != NULL) { |
d3c2ae1c | 3969 | bytes_evicted += HDR_GET_LSIZE(hdr); |
f7de776d AM |
3970 | *real_evicted += HDR_GET_LSIZE(hdr); |
3971 | } | |
d3c2ae1c | 3972 | mutex_exit(&buf->b_evict_lock); |
2aa34383 | 3973 | arc_buf_destroy_impl(buf); |
ca0bf58d | 3974 | } |
34dc7c2f | 3975 | |
ca0bf58d | 3976 | if (HDR_HAS_L2HDR(hdr)) { |
d3c2ae1c | 3977 | ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr)); |
ca0bf58d | 3978 | } else { |
d3c2ae1c GW |
3979 | if (l2arc_write_eligible(hdr->b_spa, hdr)) { |
3980 | ARCSTAT_INCR(arcstat_evict_l2_eligible, | |
3981 | HDR_GET_LSIZE(hdr)); | |
08532162 GA |
3982 | |
3983 | switch (state->arcs_state) { | |
3984 | case ARC_STATE_MRU: | |
3985 | ARCSTAT_INCR( | |
3986 | arcstat_evict_l2_eligible_mru, | |
3987 | HDR_GET_LSIZE(hdr)); | |
3988 | break; | |
3989 | case ARC_STATE_MFU: | |
3990 | ARCSTAT_INCR( | |
3991 | arcstat_evict_l2_eligible_mfu, | |
3992 | HDR_GET_LSIZE(hdr)); | |
3993 | break; | |
3994 | default: | |
3995 | break; | |
3996 | } | |
d3c2ae1c GW |
3997 | } else { |
3998 | ARCSTAT_INCR(arcstat_evict_l2_ineligible, | |
3999 | HDR_GET_LSIZE(hdr)); | |
4000 | } | |
ca0bf58d | 4001 | } |
34dc7c2f | 4002 | |
d3c2ae1c GW |
4003 | if (hdr->b_l1hdr.b_bufcnt == 0) { |
4004 | arc_cksum_free(hdr); | |
4005 | ||
4006 | bytes_evicted += arc_hdr_size(hdr); | |
f7de776d | 4007 | *real_evicted += arc_hdr_size(hdr); |
d3c2ae1c GW |
4008 | |
4009 | /* | |
4010 | * If this hdr is being evicted and has a compressed | |
4011 | * buffer then we discard it here before we change states. | |
4012 | * This ensures that the accounting is updated correctly | |
a6255b7f | 4013 | * in arc_free_data_impl(). |
d3c2ae1c | 4014 | */ |
b5256303 TC |
4015 | if (hdr->b_l1hdr.b_pabd != NULL) |
4016 | arc_hdr_free_abd(hdr, B_FALSE); | |
4017 | ||
4018 | if (HDR_HAS_RABD(hdr)) | |
4019 | arc_hdr_free_abd(hdr, B_TRUE); | |
d3c2ae1c | 4020 | |
ca0bf58d PS |
4021 | arc_change_state(evicted_state, hdr, hash_lock); |
4022 | ASSERT(HDR_IN_HASH_TABLE(hdr)); | |
d3c2ae1c | 4023 | arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
ca0bf58d PS |
4024 | DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); |
4025 | } | |
34dc7c2f | 4026 | |
ca0bf58d | 4027 | return (bytes_evicted); |
34dc7c2f BB |
4028 | } |
4029 | ||
3442c2a0 MA |
4030 | static void |
4031 | arc_set_need_free(void) | |
4032 | { | |
4033 | ASSERT(MUTEX_HELD(&arc_evict_lock)); | |
4034 | int64_t remaining = arc_free_memory() - arc_sys_free / 2; | |
4035 | arc_evict_waiter_t *aw = list_tail(&arc_evict_waiters); | |
4036 | if (aw == NULL) { | |
4037 | arc_need_free = MAX(-remaining, 0); | |
4038 | } else { | |
4039 | arc_need_free = | |
4040 | MAX(-remaining, (int64_t)(aw->aew_count - arc_evict_count)); | |
4041 | } | |
4042 | } | |
4043 | ||
ca0bf58d PS |
4044 | static uint64_t |
4045 | arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, | |
8172df64 | 4046 | uint64_t spa, uint64_t bytes) |
34dc7c2f | 4047 | { |
ca0bf58d | 4048 | multilist_sublist_t *mls; |
f7de776d | 4049 | uint64_t bytes_evicted = 0, real_evicted = 0; |
ca0bf58d | 4050 | arc_buf_hdr_t *hdr; |
34dc7c2f | 4051 | kmutex_t *hash_lock; |
8172df64 | 4052 | int evict_count = zfs_arc_evict_batch_limit; |
34dc7c2f | 4053 | |
ca0bf58d | 4054 | ASSERT3P(marker, !=, NULL); |
ca0bf58d PS |
4055 | |
4056 | mls = multilist_sublist_lock(ml, idx); | |
572e2857 | 4057 | |
8172df64 | 4058 | for (hdr = multilist_sublist_prev(mls, marker); likely(hdr != NULL); |
ca0bf58d | 4059 | hdr = multilist_sublist_prev(mls, marker)) { |
8172df64 | 4060 | if ((evict_count <= 0) || (bytes_evicted >= bytes)) |
ca0bf58d PS |
4061 | break; |
4062 | ||
4063 | /* | |
4064 | * To keep our iteration location, move the marker | |
4065 | * forward. Since we're not holding hdr's hash lock, we | |
4066 | * must be very careful and not remove 'hdr' from the | |
4067 | * sublist. Otherwise, other consumers might mistake the | |
4068 | * 'hdr' as not being on a sublist when they call the | |
4069 | * multilist_link_active() function (they all rely on | |
4070 | * the hash lock protecting concurrent insertions and | |
4071 | * removals). multilist_sublist_move_forward() was | |
4072 | * specifically implemented to ensure this is the case | |
4073 | * (only 'marker' will be removed and re-inserted). | |
4074 | */ | |
4075 | multilist_sublist_move_forward(mls, marker); | |
4076 | ||
4077 | /* | |
4078 | * The only case where the b_spa field should ever be | |
4079 | * zero, is the marker headers inserted by | |
4080 | * arc_evict_state(). It's possible for multiple threads | |
4081 | * to be calling arc_evict_state() concurrently (e.g. | |
4082 | * dsl_pool_close() and zio_inject_fault()), so we must | |
4083 | * skip any markers we see from these other threads. | |
4084 | */ | |
2a432414 | 4085 | if (hdr->b_spa == 0) |
572e2857 BB |
4086 | continue; |
4087 | ||
ca0bf58d PS |
4088 | /* we're only interested in evicting buffers of a certain spa */ |
4089 | if (spa != 0 && hdr->b_spa != spa) { | |
4090 | ARCSTAT_BUMP(arcstat_evict_skip); | |
428870ff | 4091 | continue; |
ca0bf58d PS |
4092 | } |
4093 | ||
4094 | hash_lock = HDR_LOCK(hdr); | |
e8b96c60 MA |
4095 | |
4096 | /* | |
ca0bf58d PS |
4097 | * We aren't calling this function from any code path |
4098 | * that would already be holding a hash lock, so we're | |
4099 | * asserting on this assumption to be defensive in case | |
4100 | * this ever changes. Without this check, it would be | |
4101 | * possible to incorrectly increment arcstat_mutex_miss | |
4102 | * below (e.g. if the code changed such that we called | |
4103 | * this function with a hash lock held). | |
e8b96c60 | 4104 | */ |
ca0bf58d PS |
4105 | ASSERT(!MUTEX_HELD(hash_lock)); |
4106 | ||
34dc7c2f | 4107 | if (mutex_tryenter(hash_lock)) { |
f7de776d AM |
4108 | uint64_t revicted; |
4109 | uint64_t evicted = arc_evict_hdr(hdr, hash_lock, | |
4110 | &revicted); | |
ca0bf58d | 4111 | mutex_exit(hash_lock); |
34dc7c2f | 4112 | |
ca0bf58d | 4113 | bytes_evicted += evicted; |
f7de776d | 4114 | real_evicted += revicted; |
34dc7c2f | 4115 | |
572e2857 | 4116 | /* |
ca0bf58d PS |
4117 | * If evicted is zero, arc_evict_hdr() must have |
4118 | * decided to skip this header, don't increment | |
4119 | * evict_count in this case. | |
572e2857 | 4120 | */ |
ca0bf58d | 4121 | if (evicted != 0) |
8172df64 | 4122 | evict_count--; |
ca0bf58d | 4123 | |
e8b96c60 | 4124 | } else { |
ca0bf58d | 4125 | ARCSTAT_BUMP(arcstat_mutex_miss); |
e8b96c60 | 4126 | } |
34dc7c2f | 4127 | } |
34dc7c2f | 4128 | |
ca0bf58d | 4129 | multilist_sublist_unlock(mls); |
34dc7c2f | 4130 | |
3442c2a0 MA |
4131 | /* |
4132 | * Increment the count of evicted bytes, and wake up any threads that | |
4133 | * are waiting for the count to reach this value. Since the list is | |
4134 | * ordered by ascending aew_count, we pop off the beginning of the | |
4135 | * list until we reach the end, or a waiter that's past the current | |
4136 | * "count". Doing this outside the loop reduces the number of times | |
4137 | * we need to acquire the global arc_evict_lock. | |
4138 | * | |
4139 | * Only wake when there's sufficient free memory in the system | |
4140 | * (specifically, arc_sys_free/2, which by default is a bit more than | |
4141 | * 1/64th of RAM). See the comments in arc_wait_for_eviction(). | |
4142 | */ | |
4143 | mutex_enter(&arc_evict_lock); | |
f7de776d | 4144 | arc_evict_count += real_evicted; |
3442c2a0 | 4145 | |
dc303dcf | 4146 | if (arc_free_memory() > arc_sys_free / 2) { |
3442c2a0 MA |
4147 | arc_evict_waiter_t *aw; |
4148 | while ((aw = list_head(&arc_evict_waiters)) != NULL && | |
4149 | aw->aew_count <= arc_evict_count) { | |
4150 | list_remove(&arc_evict_waiters, aw); | |
4151 | cv_broadcast(&aw->aew_cv); | |
4152 | } | |
4153 | } | |
4154 | arc_set_need_free(); | |
4155 | mutex_exit(&arc_evict_lock); | |
4156 | ||
67c0f0de MA |
4157 | /* |
4158 | * If the ARC size is reduced from arc_c_max to arc_c_min (especially | |
4159 | * if the average cached block is small), eviction can be on-CPU for | |
4160 | * many seconds. To ensure that other threads that may be bound to | |
4161 | * this CPU are able to make progress, make a voluntary preemption | |
4162 | * call here. | |
4163 | */ | |
4164 | cond_resched(); | |
4165 | ||
ca0bf58d | 4166 | return (bytes_evicted); |
34dc7c2f BB |
4167 | } |
4168 | ||
ca0bf58d PS |
4169 | /* |
4170 | * Evict buffers from the given arc state, until we've removed the | |
4171 | * specified number of bytes. Move the removed buffers to the | |
4172 | * appropriate evict state. | |
4173 | * | |
4174 | * This function makes a "best effort". It skips over any buffers | |
4175 | * it can't get a hash_lock on, and so, may not catch all candidates. | |
4176 | * It may also return without evicting as much space as requested. | |
4177 | * | |
4178 | * If bytes is specified using the special value ARC_EVICT_ALL, this | |
4179 | * will evict all available (i.e. unlocked and evictable) buffers from | |
4180 | * the given arc state; which is used by arc_flush(). | |
4181 | */ | |
4182 | static uint64_t | |
8172df64 | 4183 | arc_evict_state(arc_state_t *state, uint64_t spa, uint64_t bytes, |
ca0bf58d | 4184 | arc_buf_contents_t type) |
34dc7c2f | 4185 | { |
ca0bf58d | 4186 | uint64_t total_evicted = 0; |
ffdf019c | 4187 | multilist_t *ml = &state->arcs_list[type]; |
ca0bf58d PS |
4188 | int num_sublists; |
4189 | arc_buf_hdr_t **markers; | |
ca0bf58d | 4190 | |
ca0bf58d | 4191 | num_sublists = multilist_get_num_sublists(ml); |
d164b209 BB |
4192 | |
4193 | /* | |
ca0bf58d PS |
4194 | * If we've tried to evict from each sublist, made some |
4195 | * progress, but still have not hit the target number of bytes | |
4196 | * to evict, we want to keep trying. The markers allow us to | |
4197 | * pick up where we left off for each individual sublist, rather | |
4198 | * than starting from the tail each time. | |
d164b209 | 4199 | */ |
ca0bf58d | 4200 | markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); |
1c27024e | 4201 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d | 4202 | multilist_sublist_t *mls; |
34dc7c2f | 4203 | |
ca0bf58d PS |
4204 | markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); |
4205 | ||
4206 | /* | |
4207 | * A b_spa of 0 is used to indicate that this header is | |
5dd92909 | 4208 | * a marker. This fact is used in arc_evict_type() and |
ca0bf58d PS |
4209 | * arc_evict_state_impl(). |
4210 | */ | |
4211 | markers[i]->b_spa = 0; | |
34dc7c2f | 4212 | |
ca0bf58d PS |
4213 | mls = multilist_sublist_lock(ml, i); |
4214 | multilist_sublist_insert_tail(mls, markers[i]); | |
4215 | multilist_sublist_unlock(mls); | |
34dc7c2f BB |
4216 | } |
4217 | ||
d164b209 | 4218 | /* |
ca0bf58d PS |
4219 | * While we haven't hit our target number of bytes to evict, or |
4220 | * we're evicting all available buffers. | |
d164b209 | 4221 | */ |
8172df64 | 4222 | while (total_evicted < bytes) { |
25458cbe TC |
4223 | int sublist_idx = multilist_get_random_index(ml); |
4224 | uint64_t scan_evicted = 0; | |
4225 | ||
4226 | /* | |
4227 | * Try to reduce pinned dnodes with a floor of arc_dnode_limit. | |
4228 | * Request that 10% of the LRUs be scanned by the superblock | |
4229 | * shrinker. | |
4230 | */ | |
c4c162c1 AM |
4231 | if (type == ARC_BUFC_DATA && aggsum_compare( |
4232 | &arc_sums.arcstat_dnode_size, arc_dnode_size_limit) > 0) { | |
4233 | arc_prune_async((aggsum_upper_bound( | |
4234 | &arc_sums.arcstat_dnode_size) - | |
03fdcb9a | 4235 | arc_dnode_size_limit) / sizeof (dnode_t) / |
37fb3e43 PD |
4236 | zfs_arc_dnode_reduce_percent); |
4237 | } | |
25458cbe | 4238 | |
ca0bf58d PS |
4239 | /* |
4240 | * Start eviction using a randomly selected sublist, | |
4241 | * this is to try and evenly balance eviction across all | |
4242 | * sublists. Always starting at the same sublist | |
4243 | * (e.g. index 0) would cause evictions to favor certain | |
4244 | * sublists over others. | |
4245 | */ | |
1c27024e | 4246 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d PS |
4247 | uint64_t bytes_remaining; |
4248 | uint64_t bytes_evicted; | |
d164b209 | 4249 | |
8172df64 | 4250 | if (total_evicted < bytes) |
ca0bf58d PS |
4251 | bytes_remaining = bytes - total_evicted; |
4252 | else | |
4253 | break; | |
34dc7c2f | 4254 | |
ca0bf58d PS |
4255 | bytes_evicted = arc_evict_state_impl(ml, sublist_idx, |
4256 | markers[sublist_idx], spa, bytes_remaining); | |
4257 | ||
4258 | scan_evicted += bytes_evicted; | |
4259 | total_evicted += bytes_evicted; | |
4260 | ||
4261 | /* we've reached the end, wrap to the beginning */ | |
4262 | if (++sublist_idx >= num_sublists) | |
4263 | sublist_idx = 0; | |
4264 | } | |
4265 | ||
4266 | /* | |
4267 | * If we didn't evict anything during this scan, we have | |
4268 | * no reason to believe we'll evict more during another | |
4269 | * scan, so break the loop. | |
4270 | */ | |
4271 | if (scan_evicted == 0) { | |
4272 | /* This isn't possible, let's make that obvious */ | |
4273 | ASSERT3S(bytes, !=, 0); | |
34dc7c2f | 4274 | |
ca0bf58d PS |
4275 | /* |
4276 | * When bytes is ARC_EVICT_ALL, the only way to | |
4277 | * break the loop is when scan_evicted is zero. | |
4278 | * In that case, we actually have evicted enough, | |
4279 | * so we don't want to increment the kstat. | |
4280 | */ | |
4281 | if (bytes != ARC_EVICT_ALL) { | |
4282 | ASSERT3S(total_evicted, <, bytes); | |
4283 | ARCSTAT_BUMP(arcstat_evict_not_enough); | |
4284 | } | |
d164b209 | 4285 | |
ca0bf58d PS |
4286 | break; |
4287 | } | |
d164b209 | 4288 | } |
34dc7c2f | 4289 | |
1c27024e | 4290 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d PS |
4291 | multilist_sublist_t *mls = multilist_sublist_lock(ml, i); |
4292 | multilist_sublist_remove(mls, markers[i]); | |
4293 | multilist_sublist_unlock(mls); | |
34dc7c2f | 4294 | |
ca0bf58d | 4295 | kmem_cache_free(hdr_full_cache, markers[i]); |
34dc7c2f | 4296 | } |
ca0bf58d PS |
4297 | kmem_free(markers, sizeof (*markers) * num_sublists); |
4298 | ||
4299 | return (total_evicted); | |
4300 | } | |
4301 | ||
4302 | /* | |
4303 | * Flush all "evictable" data of the given type from the arc state | |
4304 | * specified. This will not evict any "active" buffers (i.e. referenced). | |
4305 | * | |
d3c2ae1c | 4306 | * When 'retry' is set to B_FALSE, the function will make a single pass |
ca0bf58d PS |
4307 | * over the state and evict any buffers that it can. Since it doesn't |
4308 | * continually retry the eviction, it might end up leaving some buffers | |
4309 | * in the ARC due to lock misses. | |
4310 | * | |
d3c2ae1c | 4311 | * When 'retry' is set to B_TRUE, the function will continually retry the |
ca0bf58d PS |
4312 | * eviction until *all* evictable buffers have been removed from the |
4313 | * state. As a result, if concurrent insertions into the state are | |
4314 | * allowed (e.g. if the ARC isn't shutting down), this function might | |
4315 | * wind up in an infinite loop, continually trying to evict buffers. | |
4316 | */ | |
4317 | static uint64_t | |
4318 | arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, | |
4319 | boolean_t retry) | |
4320 | { | |
4321 | uint64_t evicted = 0; | |
4322 | ||
424fd7c3 | 4323 | while (zfs_refcount_count(&state->arcs_esize[type]) != 0) { |
ca0bf58d PS |
4324 | evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); |
4325 | ||
4326 | if (!retry) | |
4327 | break; | |
4328 | } | |
4329 | ||
4330 | return (evicted); | |
34dc7c2f BB |
4331 | } |
4332 | ||
ca0bf58d PS |
4333 | /* |
4334 | * Evict the specified number of bytes from the state specified, | |
4335 | * restricting eviction to the spa and type given. This function | |
4336 | * prevents us from trying to evict more from a state's list than | |
4337 | * is "evictable", and to skip evicting altogether when passed a | |
4338 | * negative value for "bytes". In contrast, arc_evict_state() will | |
4339 | * evict everything it can, when passed a negative value for "bytes". | |
4340 | */ | |
4341 | static uint64_t | |
5dd92909 | 4342 | arc_evict_impl(arc_state_t *state, uint64_t spa, int64_t bytes, |
ca0bf58d PS |
4343 | arc_buf_contents_t type) |
4344 | { | |
8172df64 | 4345 | uint64_t delta; |
ca0bf58d | 4346 | |
424fd7c3 TS |
4347 | if (bytes > 0 && zfs_refcount_count(&state->arcs_esize[type]) > 0) { |
4348 | delta = MIN(zfs_refcount_count(&state->arcs_esize[type]), | |
4349 | bytes); | |
ca0bf58d PS |
4350 | return (arc_evict_state(state, spa, delta, type)); |
4351 | } | |
4352 | ||
4353 | return (0); | |
4354 | } | |
4355 | ||
4356 | /* | |
4357 | * The goal of this function is to evict enough meta data buffers from the | |
4358 | * ARC in order to enforce the arc_meta_limit. Achieving this is slightly | |
4359 | * more complicated than it appears because it is common for data buffers | |
4360 | * to have holds on meta data buffers. In addition, dnode meta data buffers | |
4361 | * will be held by the dnodes in the block preventing them from being freed. | |
4362 | * This means we can't simply traverse the ARC and expect to always find | |
4363 | * enough unheld meta data buffer to release. | |
4364 | * | |
4365 | * Therefore, this function has been updated to make alternating passes | |
4366 | * over the ARC releasing data buffers and then newly unheld meta data | |
37fb3e43 | 4367 | * buffers. This ensures forward progress is maintained and meta_used |
ca0bf58d PS |
4368 | * will decrease. Normally this is sufficient, but if required the ARC |
4369 | * will call the registered prune callbacks causing dentry and inodes to | |
4370 | * be dropped from the VFS cache. This will make dnode meta data buffers | |
4371 | * available for reclaim. | |
4372 | */ | |
4373 | static uint64_t | |
5dd92909 | 4374 | arc_evict_meta_balanced(uint64_t meta_used) |
ca0bf58d | 4375 | { |
25e2ab16 TC |
4376 | int64_t delta, prune = 0, adjustmnt; |
4377 | uint64_t total_evicted = 0; | |
ca0bf58d | 4378 | arc_buf_contents_t type = ARC_BUFC_DATA; |
ca67b33a | 4379 | int restarts = MAX(zfs_arc_meta_adjust_restarts, 0); |
ca0bf58d PS |
4380 | |
4381 | restart: | |
4382 | /* | |
4383 | * This slightly differs than the way we evict from the mru in | |
5dd92909 | 4384 | * arc_evict because we don't have a "target" value (i.e. no |
ca0bf58d PS |
4385 | * "meta" arc_p). As a result, I think we can completely |
4386 | * cannibalize the metadata in the MRU before we evict the | |
4387 | * metadata from the MFU. I think we probably need to implement a | |
4388 | * "metadata arc_p" value to do this properly. | |
4389 | */ | |
37fb3e43 | 4390 | adjustmnt = meta_used - arc_meta_limit; |
ca0bf58d | 4391 | |
424fd7c3 TS |
4392 | if (adjustmnt > 0 && |
4393 | zfs_refcount_count(&arc_mru->arcs_esize[type]) > 0) { | |
4394 | delta = MIN(zfs_refcount_count(&arc_mru->arcs_esize[type]), | |
d3c2ae1c | 4395 | adjustmnt); |
5dd92909 | 4396 | total_evicted += arc_evict_impl(arc_mru, 0, delta, type); |
ca0bf58d PS |
4397 | adjustmnt -= delta; |
4398 | } | |
4399 | ||
4400 | /* | |
4401 | * We can't afford to recalculate adjustmnt here. If we do, | |
4402 | * new metadata buffers can sneak into the MRU or ANON lists, | |
4403 | * thus penalize the MFU metadata. Although the fudge factor is | |
4404 | * small, it has been empirically shown to be significant for | |
4405 | * certain workloads (e.g. creating many empty directories). As | |
4406 | * such, we use the original calculation for adjustmnt, and | |
4407 | * simply decrement the amount of data evicted from the MRU. | |
4408 | */ | |
4409 | ||
424fd7c3 TS |
4410 | if (adjustmnt > 0 && |
4411 | zfs_refcount_count(&arc_mfu->arcs_esize[type]) > 0) { | |
4412 | delta = MIN(zfs_refcount_count(&arc_mfu->arcs_esize[type]), | |
d3c2ae1c | 4413 | adjustmnt); |
5dd92909 | 4414 | total_evicted += arc_evict_impl(arc_mfu, 0, delta, type); |
ca0bf58d PS |
4415 | } |
4416 | ||
37fb3e43 | 4417 | adjustmnt = meta_used - arc_meta_limit; |
ca0bf58d | 4418 | |
d3c2ae1c | 4419 | if (adjustmnt > 0 && |
424fd7c3 | 4420 | zfs_refcount_count(&arc_mru_ghost->arcs_esize[type]) > 0) { |
ca0bf58d | 4421 | delta = MIN(adjustmnt, |
424fd7c3 | 4422 | zfs_refcount_count(&arc_mru_ghost->arcs_esize[type])); |
5dd92909 | 4423 | total_evicted += arc_evict_impl(arc_mru_ghost, 0, delta, type); |
ca0bf58d PS |
4424 | adjustmnt -= delta; |
4425 | } | |
4426 | ||
d3c2ae1c | 4427 | if (adjustmnt > 0 && |
424fd7c3 | 4428 | zfs_refcount_count(&arc_mfu_ghost->arcs_esize[type]) > 0) { |
ca0bf58d | 4429 | delta = MIN(adjustmnt, |
424fd7c3 | 4430 | zfs_refcount_count(&arc_mfu_ghost->arcs_esize[type])); |
5dd92909 | 4431 | total_evicted += arc_evict_impl(arc_mfu_ghost, 0, delta, type); |
ca0bf58d PS |
4432 | } |
4433 | ||
4434 | /* | |
4435 | * If after attempting to make the requested adjustment to the ARC | |
4436 | * the meta limit is still being exceeded then request that the | |
4437 | * higher layers drop some cached objects which have holds on ARC | |
4438 | * meta buffers. Requests to the upper layers will be made with | |
4439 | * increasingly large scan sizes until the ARC is below the limit. | |
4440 | */ | |
37fb3e43 | 4441 | if (meta_used > arc_meta_limit) { |
ca0bf58d PS |
4442 | if (type == ARC_BUFC_DATA) { |
4443 | type = ARC_BUFC_METADATA; | |
4444 | } else { | |
4445 | type = ARC_BUFC_DATA; | |
4446 | ||
4447 | if (zfs_arc_meta_prune) { | |
4448 | prune += zfs_arc_meta_prune; | |
f6046738 | 4449 | arc_prune_async(prune); |
ca0bf58d PS |
4450 | } |
4451 | } | |
4452 | ||
4453 | if (restarts > 0) { | |
4454 | restarts--; | |
4455 | goto restart; | |
4456 | } | |
4457 | } | |
4458 | return (total_evicted); | |
4459 | } | |
4460 | ||
f6046738 | 4461 | /* |
c4c162c1 | 4462 | * Evict metadata buffers from the cache, such that arcstat_meta_used is |
f6046738 BB |
4463 | * capped by the arc_meta_limit tunable. |
4464 | */ | |
4465 | static uint64_t | |
5dd92909 | 4466 | arc_evict_meta_only(uint64_t meta_used) |
f6046738 BB |
4467 | { |
4468 | uint64_t total_evicted = 0; | |
4469 | int64_t target; | |
4470 | ||
4471 | /* | |
4472 | * If we're over the meta limit, we want to evict enough | |
4473 | * metadata to get back under the meta limit. We don't want to | |
4474 | * evict so much that we drop the MRU below arc_p, though. If | |
4475 | * we're over the meta limit more than we're over arc_p, we | |
4476 | * evict some from the MRU here, and some from the MFU below. | |
4477 | */ | |
37fb3e43 | 4478 | target = MIN((int64_t)(meta_used - arc_meta_limit), |
424fd7c3 TS |
4479 | (int64_t)(zfs_refcount_count(&arc_anon->arcs_size) + |
4480 | zfs_refcount_count(&arc_mru->arcs_size) - arc_p)); | |
f6046738 | 4481 | |
5dd92909 | 4482 | total_evicted += arc_evict_impl(arc_mru, 0, target, ARC_BUFC_METADATA); |
f6046738 BB |
4483 | |
4484 | /* | |
4485 | * Similar to the above, we want to evict enough bytes to get us | |
4486 | * below the meta limit, but not so much as to drop us below the | |
2aa34383 | 4487 | * space allotted to the MFU (which is defined as arc_c - arc_p). |
f6046738 | 4488 | */ |
37fb3e43 | 4489 | target = MIN((int64_t)(meta_used - arc_meta_limit), |
424fd7c3 | 4490 | (int64_t)(zfs_refcount_count(&arc_mfu->arcs_size) - |
37fb3e43 | 4491 | (arc_c - arc_p))); |
f6046738 | 4492 | |
5dd92909 | 4493 | total_evicted += arc_evict_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); |
f6046738 BB |
4494 | |
4495 | return (total_evicted); | |
4496 | } | |
4497 | ||
4498 | static uint64_t | |
5dd92909 | 4499 | arc_evict_meta(uint64_t meta_used) |
f6046738 BB |
4500 | { |
4501 | if (zfs_arc_meta_strategy == ARC_STRATEGY_META_ONLY) | |
5dd92909 | 4502 | return (arc_evict_meta_only(meta_used)); |
f6046738 | 4503 | else |
5dd92909 | 4504 | return (arc_evict_meta_balanced(meta_used)); |
f6046738 BB |
4505 | } |
4506 | ||
ca0bf58d PS |
4507 | /* |
4508 | * Return the type of the oldest buffer in the given arc state | |
4509 | * | |
4510 | * This function will select a random sublist of type ARC_BUFC_DATA and | |
4511 | * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist | |
4512 | * is compared, and the type which contains the "older" buffer will be | |
4513 | * returned. | |
4514 | */ | |
4515 | static arc_buf_contents_t | |
5dd92909 | 4516 | arc_evict_type(arc_state_t *state) |
ca0bf58d | 4517 | { |
ffdf019c AM |
4518 | multilist_t *data_ml = &state->arcs_list[ARC_BUFC_DATA]; |
4519 | multilist_t *meta_ml = &state->arcs_list[ARC_BUFC_METADATA]; | |
ca0bf58d PS |
4520 | int data_idx = multilist_get_random_index(data_ml); |
4521 | int meta_idx = multilist_get_random_index(meta_ml); | |
4522 | multilist_sublist_t *data_mls; | |
4523 | multilist_sublist_t *meta_mls; | |
4524 | arc_buf_contents_t type; | |
4525 | arc_buf_hdr_t *data_hdr; | |
4526 | arc_buf_hdr_t *meta_hdr; | |
4527 | ||
4528 | /* | |
4529 | * We keep the sublist lock until we're finished, to prevent | |
4530 | * the headers from being destroyed via arc_evict_state(). | |
4531 | */ | |
4532 | data_mls = multilist_sublist_lock(data_ml, data_idx); | |
4533 | meta_mls = multilist_sublist_lock(meta_ml, meta_idx); | |
4534 | ||
4535 | /* | |
4536 | * These two loops are to ensure we skip any markers that | |
4537 | * might be at the tail of the lists due to arc_evict_state(). | |
4538 | */ | |
4539 | ||
4540 | for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; | |
4541 | data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { | |
4542 | if (data_hdr->b_spa != 0) | |
4543 | break; | |
4544 | } | |
4545 | ||
4546 | for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; | |
4547 | meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { | |
4548 | if (meta_hdr->b_spa != 0) | |
4549 | break; | |
4550 | } | |
4551 | ||
4552 | if (data_hdr == NULL && meta_hdr == NULL) { | |
4553 | type = ARC_BUFC_DATA; | |
4554 | } else if (data_hdr == NULL) { | |
4555 | ASSERT3P(meta_hdr, !=, NULL); | |
4556 | type = ARC_BUFC_METADATA; | |
4557 | } else if (meta_hdr == NULL) { | |
4558 | ASSERT3P(data_hdr, !=, NULL); | |
4559 | type = ARC_BUFC_DATA; | |
4560 | } else { | |
4561 | ASSERT3P(data_hdr, !=, NULL); | |
4562 | ASSERT3P(meta_hdr, !=, NULL); | |
4563 | ||
4564 | /* The headers can't be on the sublist without an L1 header */ | |
4565 | ASSERT(HDR_HAS_L1HDR(data_hdr)); | |
4566 | ASSERT(HDR_HAS_L1HDR(meta_hdr)); | |
4567 | ||
4568 | if (data_hdr->b_l1hdr.b_arc_access < | |
4569 | meta_hdr->b_l1hdr.b_arc_access) { | |
4570 | type = ARC_BUFC_DATA; | |
4571 | } else { | |
4572 | type = ARC_BUFC_METADATA; | |
4573 | } | |
4574 | } | |
4575 | ||
4576 | multilist_sublist_unlock(meta_mls); | |
4577 | multilist_sublist_unlock(data_mls); | |
4578 | ||
4579 | return (type); | |
4580 | } | |
4581 | ||
4582 | /* | |
c4c162c1 | 4583 | * Evict buffers from the cache, such that arcstat_size is capped by arc_c. |
ca0bf58d PS |
4584 | */ |
4585 | static uint64_t | |
5dd92909 | 4586 | arc_evict(void) |
ca0bf58d PS |
4587 | { |
4588 | uint64_t total_evicted = 0; | |
4589 | uint64_t bytes; | |
4590 | int64_t target; | |
c4c162c1 AM |
4591 | uint64_t asize = aggsum_value(&arc_sums.arcstat_size); |
4592 | uint64_t ameta = aggsum_value(&arc_sums.arcstat_meta_used); | |
ca0bf58d PS |
4593 | |
4594 | /* | |
4595 | * If we're over arc_meta_limit, we want to correct that before | |
4596 | * potentially evicting data buffers below. | |
4597 | */ | |
5dd92909 | 4598 | total_evicted += arc_evict_meta(ameta); |
ca0bf58d PS |
4599 | |
4600 | /* | |
4601 | * Adjust MRU size | |
4602 | * | |
4603 | * If we're over the target cache size, we want to evict enough | |
4604 | * from the list to get back to our target size. We don't want | |
4605 | * to evict too much from the MRU, such that it drops below | |
4606 | * arc_p. So, if we're over our target cache size more than | |
4607 | * the MRU is over arc_p, we'll evict enough to get back to | |
4608 | * arc_p here, and then evict more from the MFU below. | |
4609 | */ | |
37fb3e43 | 4610 | target = MIN((int64_t)(asize - arc_c), |
424fd7c3 TS |
4611 | (int64_t)(zfs_refcount_count(&arc_anon->arcs_size) + |
4612 | zfs_refcount_count(&arc_mru->arcs_size) + ameta - arc_p)); | |
ca0bf58d PS |
4613 | |
4614 | /* | |
4615 | * If we're below arc_meta_min, always prefer to evict data. | |
4616 | * Otherwise, try to satisfy the requested number of bytes to | |
4617 | * evict from the type which contains older buffers; in an | |
4618 | * effort to keep newer buffers in the cache regardless of their | |
4619 | * type. If we cannot satisfy the number of bytes from this | |
4620 | * type, spill over into the next type. | |
4621 | */ | |
5dd92909 | 4622 | if (arc_evict_type(arc_mru) == ARC_BUFC_METADATA && |
37fb3e43 | 4623 | ameta > arc_meta_min) { |
5dd92909 | 4624 | bytes = arc_evict_impl(arc_mru, 0, target, ARC_BUFC_METADATA); |
ca0bf58d PS |
4625 | total_evicted += bytes; |
4626 | ||
4627 | /* | |
4628 | * If we couldn't evict our target number of bytes from | |
4629 | * metadata, we try to get the rest from data. | |
4630 | */ | |
4631 | target -= bytes; | |
4632 | ||
4633 | total_evicted += | |
5dd92909 | 4634 | arc_evict_impl(arc_mru, 0, target, ARC_BUFC_DATA); |
ca0bf58d | 4635 | } else { |
5dd92909 | 4636 | bytes = arc_evict_impl(arc_mru, 0, target, ARC_BUFC_DATA); |
ca0bf58d PS |
4637 | total_evicted += bytes; |
4638 | ||
4639 | /* | |
4640 | * If we couldn't evict our target number of bytes from | |
4641 | * data, we try to get the rest from metadata. | |
4642 | */ | |
4643 | target -= bytes; | |
4644 | ||
4645 | total_evicted += | |
5dd92909 | 4646 | arc_evict_impl(arc_mru, 0, target, ARC_BUFC_METADATA); |
ca0bf58d PS |
4647 | } |
4648 | ||
0405eeea RE |
4649 | /* |
4650 | * Re-sum ARC stats after the first round of evictions. | |
4651 | */ | |
c4c162c1 AM |
4652 | asize = aggsum_value(&arc_sums.arcstat_size); |
4653 | ameta = aggsum_value(&arc_sums.arcstat_meta_used); | |
0405eeea RE |
4654 | |
4655 | ||
ca0bf58d PS |
4656 | /* |
4657 | * Adjust MFU size | |
4658 | * | |
4659 | * Now that we've tried to evict enough from the MRU to get its | |
4660 | * size back to arc_p, if we're still above the target cache | |
4661 | * size, we evict the rest from the MFU. | |
4662 | */ | |
37fb3e43 | 4663 | target = asize - arc_c; |
ca0bf58d | 4664 | |
5dd92909 | 4665 | if (arc_evict_type(arc_mfu) == ARC_BUFC_METADATA && |
37fb3e43 | 4666 | ameta > arc_meta_min) { |
5dd92909 | 4667 | bytes = arc_evict_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); |
ca0bf58d PS |
4668 | total_evicted += bytes; |
4669 | ||
4670 | /* | |
4671 | * If we couldn't evict our target number of bytes from | |
4672 | * metadata, we try to get the rest from data. | |
4673 | */ | |
4674 | target -= bytes; | |
4675 | ||
4676 | total_evicted += | |
5dd92909 | 4677 | arc_evict_impl(arc_mfu, 0, target, ARC_BUFC_DATA); |
ca0bf58d | 4678 | } else { |
5dd92909 | 4679 | bytes = arc_evict_impl(arc_mfu, 0, target, ARC_BUFC_DATA); |
ca0bf58d PS |
4680 | total_evicted += bytes; |
4681 | ||
4682 | /* | |
4683 | * If we couldn't evict our target number of bytes from | |
4684 | * data, we try to get the rest from data. | |
4685 | */ | |
4686 | target -= bytes; | |
4687 | ||
4688 | total_evicted += | |
5dd92909 | 4689 | arc_evict_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); |
ca0bf58d PS |
4690 | } |
4691 | ||
4692 | /* | |
4693 | * Adjust ghost lists | |
4694 | * | |
4695 | * In addition to the above, the ARC also defines target values | |
4696 | * for the ghost lists. The sum of the mru list and mru ghost | |
4697 | * list should never exceed the target size of the cache, and | |
4698 | * the sum of the mru list, mfu list, mru ghost list, and mfu | |
4699 | * ghost list should never exceed twice the target size of the | |
4700 | * cache. The following logic enforces these limits on the ghost | |
4701 | * caches, and evicts from them as needed. | |
4702 | */ | |
424fd7c3 TS |
4703 | target = zfs_refcount_count(&arc_mru->arcs_size) + |
4704 | zfs_refcount_count(&arc_mru_ghost->arcs_size) - arc_c; | |
ca0bf58d | 4705 | |
5dd92909 | 4706 | bytes = arc_evict_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); |
ca0bf58d PS |
4707 | total_evicted += bytes; |
4708 | ||
4709 | target -= bytes; | |
4710 | ||
4711 | total_evicted += | |
5dd92909 | 4712 | arc_evict_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); |
ca0bf58d PS |
4713 | |
4714 | /* | |
4715 | * We assume the sum of the mru list and mfu list is less than | |
4716 | * or equal to arc_c (we enforced this above), which means we | |
4717 | * can use the simpler of the two equations below: | |
4718 | * | |
4719 | * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c | |
4720 | * mru ghost + mfu ghost <= arc_c | |
4721 | */ | |
424fd7c3 TS |
4722 | target = zfs_refcount_count(&arc_mru_ghost->arcs_size) + |
4723 | zfs_refcount_count(&arc_mfu_ghost->arcs_size) - arc_c; | |
ca0bf58d | 4724 | |
5dd92909 | 4725 | bytes = arc_evict_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); |
ca0bf58d PS |
4726 | total_evicted += bytes; |
4727 | ||
4728 | target -= bytes; | |
4729 | ||
4730 | total_evicted += | |
5dd92909 | 4731 | arc_evict_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); |
ca0bf58d PS |
4732 | |
4733 | return (total_evicted); | |
4734 | } | |
4735 | ||
ca0bf58d PS |
4736 | void |
4737 | arc_flush(spa_t *spa, boolean_t retry) | |
ab26409d | 4738 | { |
ca0bf58d | 4739 | uint64_t guid = 0; |
94520ca4 | 4740 | |
bc888666 | 4741 | /* |
d3c2ae1c | 4742 | * If retry is B_TRUE, a spa must not be specified since we have |
ca0bf58d PS |
4743 | * no good way to determine if all of a spa's buffers have been |
4744 | * evicted from an arc state. | |
bc888666 | 4745 | */ |
ca0bf58d | 4746 | ASSERT(!retry || spa == 0); |
d164b209 | 4747 | |
b9541d6b | 4748 | if (spa != NULL) |
3541dc6d | 4749 | guid = spa_load_guid(spa); |
d164b209 | 4750 | |
ca0bf58d PS |
4751 | (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); |
4752 | (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); | |
4753 | ||
4754 | (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); | |
4755 | (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); | |
4756 | ||
4757 | (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); | |
4758 | (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); | |
34dc7c2f | 4759 | |
ca0bf58d PS |
4760 | (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); |
4761 | (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); | |
34dc7c2f BB |
4762 | } |
4763 | ||
c9c9c1e2 | 4764 | void |
3ec34e55 | 4765 | arc_reduce_target_size(int64_t to_free) |
34dc7c2f | 4766 | { |
c4c162c1 | 4767 | uint64_t asize = aggsum_value(&arc_sums.arcstat_size); |
3442c2a0 MA |
4768 | |
4769 | /* | |
4770 | * All callers want the ARC to actually evict (at least) this much | |
4771 | * memory. Therefore we reduce from the lower of the current size and | |
4772 | * the target size. This way, even if arc_c is much higher than | |
4773 | * arc_size (as can be the case after many calls to arc_freed(), we will | |
4774 | * immediately have arc_c < arc_size and therefore the arc_evict_zthr | |
4775 | * will evict. | |
4776 | */ | |
4777 | uint64_t c = MIN(arc_c, asize); | |
34dc7c2f | 4778 | |
1b8951b3 TC |
4779 | if (c > to_free && c - to_free > arc_c_min) { |
4780 | arc_c = c - to_free; | |
ca67b33a | 4781 | atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); |
34dc7c2f BB |
4782 | if (arc_p > arc_c) |
4783 | arc_p = (arc_c >> 1); | |
4784 | ASSERT(arc_c >= arc_c_min); | |
4785 | ASSERT((int64_t)arc_p >= 0); | |
1b8951b3 TC |
4786 | } else { |
4787 | arc_c = arc_c_min; | |
34dc7c2f BB |
4788 | } |
4789 | ||
3ec34e55 | 4790 | if (asize > arc_c) { |
5dd92909 MA |
4791 | /* See comment in arc_evict_cb_check() on why lock+flag */ |
4792 | mutex_enter(&arc_evict_lock); | |
4793 | arc_evict_needed = B_TRUE; | |
4794 | mutex_exit(&arc_evict_lock); | |
4795 | zthr_wakeup(arc_evict_zthr); | |
3ec34e55 | 4796 | } |
34dc7c2f | 4797 | } |
ca67b33a MA |
4798 | |
4799 | /* | |
4800 | * Determine if the system is under memory pressure and is asking | |
d3c2ae1c | 4801 | * to reclaim memory. A return value of B_TRUE indicates that the system |
ca67b33a MA |
4802 | * is under memory pressure and that the arc should adjust accordingly. |
4803 | */ | |
c9c9c1e2 | 4804 | boolean_t |
ca67b33a MA |
4805 | arc_reclaim_needed(void) |
4806 | { | |
4807 | return (arc_available_memory() < 0); | |
4808 | } | |
4809 | ||
c9c9c1e2 | 4810 | void |
3ec34e55 | 4811 | arc_kmem_reap_soon(void) |
34dc7c2f BB |
4812 | { |
4813 | size_t i; | |
4814 | kmem_cache_t *prev_cache = NULL; | |
4815 | kmem_cache_t *prev_data_cache = NULL; | |
34dc7c2f | 4816 | |
70f02287 | 4817 | #ifdef _KERNEL |
c4c162c1 AM |
4818 | if ((aggsum_compare(&arc_sums.arcstat_meta_used, |
4819 | arc_meta_limit) >= 0) && zfs_arc_meta_prune) { | |
f6046738 BB |
4820 | /* |
4821 | * We are exceeding our meta-data cache limit. | |
4822 | * Prune some entries to release holds on meta-data. | |
4823 | */ | |
ef5b2e10 | 4824 | arc_prune_async(zfs_arc_meta_prune); |
f6046738 | 4825 | } |
70f02287 BB |
4826 | #if defined(_ILP32) |
4827 | /* | |
4828 | * Reclaim unused memory from all kmem caches. | |
4829 | */ | |
4830 | kmem_reap(); | |
4831 | #endif | |
4832 | #endif | |
f6046738 | 4833 | |
34dc7c2f | 4834 | for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { |
70f02287 | 4835 | #if defined(_ILP32) |
d0c614ec | 4836 | /* reach upper limit of cache size on 32-bit */ |
4837 | if (zio_buf_cache[i] == NULL) | |
4838 | break; | |
4839 | #endif | |
34dc7c2f BB |
4840 | if (zio_buf_cache[i] != prev_cache) { |
4841 | prev_cache = zio_buf_cache[i]; | |
4842 | kmem_cache_reap_now(zio_buf_cache[i]); | |
4843 | } | |
4844 | if (zio_data_buf_cache[i] != prev_data_cache) { | |
4845 | prev_data_cache = zio_data_buf_cache[i]; | |
4846 | kmem_cache_reap_now(zio_data_buf_cache[i]); | |
4847 | } | |
4848 | } | |
ca0bf58d | 4849 | kmem_cache_reap_now(buf_cache); |
b9541d6b CW |
4850 | kmem_cache_reap_now(hdr_full_cache); |
4851 | kmem_cache_reap_now(hdr_l2only_cache); | |
ca577779 | 4852 | kmem_cache_reap_now(zfs_btree_leaf_cache); |
7564073e | 4853 | abd_cache_reap_now(); |
34dc7c2f BB |
4854 | } |
4855 | ||
3ec34e55 | 4856 | static boolean_t |
5dd92909 | 4857 | arc_evict_cb_check(void *arg, zthr_t *zthr) |
3ec34e55 | 4858 | { |
14e4e3cb AZ |
4859 | (void) arg, (void) zthr; |
4860 | ||
1531506d | 4861 | #ifdef ZFS_DEBUG |
3ec34e55 BL |
4862 | /* |
4863 | * This is necessary in order to keep the kstat information | |
4864 | * up to date for tools that display kstat data such as the | |
4865 | * mdb ::arc dcmd and the Linux crash utility. These tools | |
4866 | * typically do not call kstat's update function, but simply | |
4867 | * dump out stats from the most recent update. Without | |
4868 | * this call, these commands may show stale stats for the | |
1531506d RM |
4869 | * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even |
4870 | * with this call, the data might be out of date if the | |
4871 | * evict thread hasn't been woken recently; but that should | |
4872 | * suffice. The arc_state_t structures can be queried | |
4873 | * directly if more accurate information is needed. | |
3ec34e55 BL |
4874 | */ |
4875 | if (arc_ksp != NULL) | |
4876 | arc_ksp->ks_update(arc_ksp, KSTAT_READ); | |
1531506d | 4877 | #endif |
3ec34e55 BL |
4878 | |
4879 | /* | |
3442c2a0 MA |
4880 | * We have to rely on arc_wait_for_eviction() to tell us when to |
4881 | * evict, rather than checking if we are overflowing here, so that we | |
4882 | * are sure to not leave arc_wait_for_eviction() waiting on aew_cv. | |
4883 | * If we have become "not overflowing" since arc_wait_for_eviction() | |
4884 | * checked, we need to wake it up. We could broadcast the CV here, | |
4885 | * but arc_wait_for_eviction() may have not yet gone to sleep. We | |
4886 | * would need to use a mutex to ensure that this function doesn't | |
4887 | * broadcast until arc_wait_for_eviction() has gone to sleep (e.g. | |
4888 | * the arc_evict_lock). However, the lock ordering of such a lock | |
4889 | * would necessarily be incorrect with respect to the zthr_lock, | |
4890 | * which is held before this function is called, and is held by | |
4891 | * arc_wait_for_eviction() when it calls zthr_wakeup(). | |
3ec34e55 | 4892 | */ |
5dd92909 | 4893 | return (arc_evict_needed); |
3ec34e55 BL |
4894 | } |
4895 | ||
302f753f | 4896 | /* |
5dd92909 | 4897 | * Keep arc_size under arc_c by running arc_evict which evicts data |
3ec34e55 | 4898 | * from the ARC. |
302f753f | 4899 | */ |
61c3391a | 4900 | static void |
5dd92909 | 4901 | arc_evict_cb(void *arg, zthr_t *zthr) |
34dc7c2f | 4902 | { |
14e4e3cb AZ |
4903 | (void) arg, (void) zthr; |
4904 | ||
3ec34e55 BL |
4905 | uint64_t evicted = 0; |
4906 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
34dc7c2f | 4907 | |
3ec34e55 | 4908 | /* Evict from cache */ |
5dd92909 | 4909 | evicted = arc_evict(); |
34dc7c2f | 4910 | |
3ec34e55 BL |
4911 | /* |
4912 | * If evicted is zero, we couldn't evict anything | |
5dd92909 | 4913 | * via arc_evict(). This could be due to hash lock |
3ec34e55 BL |
4914 | * collisions, but more likely due to the majority of |
4915 | * arc buffers being unevictable. Therefore, even if | |
4916 | * arc_size is above arc_c, another pass is unlikely to | |
4917 | * be helpful and could potentially cause us to enter an | |
4918 | * infinite loop. Additionally, zthr_iscancelled() is | |
4919 | * checked here so that if the arc is shutting down, the | |
5dd92909 | 4920 | * broadcast will wake any remaining arc evict waiters. |
3ec34e55 | 4921 | */ |
5dd92909 MA |
4922 | mutex_enter(&arc_evict_lock); |
4923 | arc_evict_needed = !zthr_iscancelled(arc_evict_zthr) && | |
c4c162c1 | 4924 | evicted > 0 && aggsum_compare(&arc_sums.arcstat_size, arc_c) > 0; |
5dd92909 | 4925 | if (!arc_evict_needed) { |
d3c2ae1c | 4926 | /* |
3ec34e55 BL |
4927 | * We're either no longer overflowing, or we |
4928 | * can't evict anything more, so we should wake | |
4929 | * arc_get_data_impl() sooner. | |
d3c2ae1c | 4930 | */ |
3442c2a0 MA |
4931 | arc_evict_waiter_t *aw; |
4932 | while ((aw = list_remove_head(&arc_evict_waiters)) != NULL) { | |
4933 | cv_broadcast(&aw->aew_cv); | |
4934 | } | |
4935 | arc_set_need_free(); | |
3ec34e55 | 4936 | } |
5dd92909 | 4937 | mutex_exit(&arc_evict_lock); |
3ec34e55 | 4938 | spl_fstrans_unmark(cookie); |
3ec34e55 BL |
4939 | } |
4940 | ||
3ec34e55 BL |
4941 | static boolean_t |
4942 | arc_reap_cb_check(void *arg, zthr_t *zthr) | |
4943 | { | |
14e4e3cb AZ |
4944 | (void) arg, (void) zthr; |
4945 | ||
3ec34e55 | 4946 | int64_t free_memory = arc_available_memory(); |
8a171ccd | 4947 | static int reap_cb_check_counter = 0; |
3ec34e55 BL |
4948 | |
4949 | /* | |
4950 | * If a kmem reap is already active, don't schedule more. We must | |
4951 | * check for this because kmem_cache_reap_soon() won't actually | |
4952 | * block on the cache being reaped (this is to prevent callers from | |
4953 | * becoming implicitly blocked by a system-wide kmem reap -- which, | |
4954 | * on a system with many, many full magazines, can take minutes). | |
4955 | */ | |
4956 | if (!kmem_cache_reap_active() && free_memory < 0) { | |
34dc7c2f | 4957 | |
3ec34e55 BL |
4958 | arc_no_grow = B_TRUE; |
4959 | arc_warm = B_TRUE; | |
0a252dae | 4960 | /* |
3ec34e55 BL |
4961 | * Wait at least zfs_grow_retry (default 5) seconds |
4962 | * before considering growing. | |
0a252dae | 4963 | */ |
3ec34e55 BL |
4964 | arc_growtime = gethrtime() + SEC2NSEC(arc_grow_retry); |
4965 | return (B_TRUE); | |
4966 | } else if (free_memory < arc_c >> arc_no_grow_shift) { | |
4967 | arc_no_grow = B_TRUE; | |
4968 | } else if (gethrtime() >= arc_growtime) { | |
4969 | arc_no_grow = B_FALSE; | |
4970 | } | |
0a252dae | 4971 | |
8a171ccd SG |
4972 | /* |
4973 | * Called unconditionally every 60 seconds to reclaim unused | |
4974 | * zstd compression and decompression context. This is done | |
4975 | * here to avoid the need for an independent thread. | |
4976 | */ | |
4977 | if (!((reap_cb_check_counter++) % 60)) | |
4978 | zfs_zstd_cache_reap_now(); | |
4979 | ||
3ec34e55 BL |
4980 | return (B_FALSE); |
4981 | } | |
34dc7c2f | 4982 | |
3ec34e55 BL |
4983 | /* |
4984 | * Keep enough free memory in the system by reaping the ARC's kmem | |
4985 | * caches. To cause more slabs to be reapable, we may reduce the | |
5dd92909 | 4986 | * target size of the cache (arc_c), causing the arc_evict_cb() |
3ec34e55 BL |
4987 | * to free more buffers. |
4988 | */ | |
61c3391a | 4989 | static void |
3ec34e55 BL |
4990 | arc_reap_cb(void *arg, zthr_t *zthr) |
4991 | { | |
14e4e3cb AZ |
4992 | (void) arg, (void) zthr; |
4993 | ||
3ec34e55 BL |
4994 | int64_t free_memory; |
4995 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
34dc7c2f | 4996 | |
3ec34e55 BL |
4997 | /* |
4998 | * Kick off asynchronous kmem_reap()'s of all our caches. | |
4999 | */ | |
5000 | arc_kmem_reap_soon(); | |
6a8f9b6b | 5001 | |
3ec34e55 BL |
5002 | /* |
5003 | * Wait at least arc_kmem_cache_reap_retry_ms between | |
5004 | * arc_kmem_reap_soon() calls. Without this check it is possible to | |
5005 | * end up in a situation where we spend lots of time reaping | |
5006 | * caches, while we're near arc_c_min. Waiting here also gives the | |
5007 | * subsequent free memory check a chance of finding that the | |
5008 | * asynchronous reap has already freed enough memory, and we don't | |
5009 | * need to call arc_reduce_target_size(). | |
5010 | */ | |
5011 | delay((hz * arc_kmem_cache_reap_retry_ms + 999) / 1000); | |
34dc7c2f | 5012 | |
3ec34e55 BL |
5013 | /* |
5014 | * Reduce the target size as needed to maintain the amount of free | |
5015 | * memory in the system at a fraction of the arc_size (1/128th by | |
5016 | * default). If oversubscribed (free_memory < 0) then reduce the | |
5017 | * target arc_size by the deficit amount plus the fractional | |
bf169e9f | 5018 | * amount. If free memory is positive but less than the fractional |
3ec34e55 BL |
5019 | * amount, reduce by what is needed to hit the fractional amount. |
5020 | */ | |
5021 | free_memory = arc_available_memory(); | |
34dc7c2f | 5022 | |
3ec34e55 BL |
5023 | int64_t to_free = |
5024 | (arc_c >> arc_shrink_shift) - free_memory; | |
5025 | if (to_free > 0) { | |
3ec34e55 | 5026 | arc_reduce_target_size(to_free); |
ca0bf58d | 5027 | } |
ca0bf58d | 5028 | spl_fstrans_unmark(cookie); |
ca0bf58d PS |
5029 | } |
5030 | ||
7cb67b45 BB |
5031 | #ifdef _KERNEL |
5032 | /* | |
302f753f BB |
5033 | * Determine the amount of memory eligible for eviction contained in the |
5034 | * ARC. All clean data reported by the ghost lists can always be safely | |
5035 | * evicted. Due to arc_c_min, the same does not hold for all clean data | |
5036 | * contained by the regular mru and mfu lists. | |
5037 | * | |
5038 | * In the case of the regular mru and mfu lists, we need to report as | |
5039 | * much clean data as possible, such that evicting that same reported | |
5040 | * data will not bring arc_size below arc_c_min. Thus, in certain | |
5041 | * circumstances, the total amount of clean data in the mru and mfu | |
5042 | * lists might not actually be evictable. | |
5043 | * | |
5044 | * The following two distinct cases are accounted for: | |
5045 | * | |
5046 | * 1. The sum of the amount of dirty data contained by both the mru and | |
5047 | * mfu lists, plus the ARC's other accounting (e.g. the anon list), | |
5048 | * is greater than or equal to arc_c_min. | |
5049 | * (i.e. amount of dirty data >= arc_c_min) | |
5050 | * | |
5051 | * This is the easy case; all clean data contained by the mru and mfu | |
5052 | * lists is evictable. Evicting all clean data can only drop arc_size | |
5053 | * to the amount of dirty data, which is greater than arc_c_min. | |
5054 | * | |
5055 | * 2. The sum of the amount of dirty data contained by both the mru and | |
5056 | * mfu lists, plus the ARC's other accounting (e.g. the anon list), | |
5057 | * is less than arc_c_min. | |
5058 | * (i.e. arc_c_min > amount of dirty data) | |
5059 | * | |
5060 | * 2.1. arc_size is greater than or equal arc_c_min. | |
5061 | * (i.e. arc_size >= arc_c_min > amount of dirty data) | |
5062 | * | |
5063 | * In this case, not all clean data from the regular mru and mfu | |
5064 | * lists is actually evictable; we must leave enough clean data | |
5065 | * to keep arc_size above arc_c_min. Thus, the maximum amount of | |
5066 | * evictable data from the two lists combined, is exactly the | |
5067 | * difference between arc_size and arc_c_min. | |
5068 | * | |
5069 | * 2.2. arc_size is less than arc_c_min | |
5070 | * (i.e. arc_c_min > arc_size > amount of dirty data) | |
5071 | * | |
5072 | * In this case, none of the data contained in the mru and mfu | |
5073 | * lists is evictable, even if it's clean. Since arc_size is | |
5074 | * already below arc_c_min, evicting any more would only | |
5075 | * increase this negative difference. | |
7cb67b45 | 5076 | */ |
7cb67b45 | 5077 | |
7cb67b45 BB |
5078 | #endif /* _KERNEL */ |
5079 | ||
34dc7c2f BB |
5080 | /* |
5081 | * Adapt arc info given the number of bytes we are trying to add and | |
4e33ba4c | 5082 | * the state that we are coming from. This function is only called |
34dc7c2f BB |
5083 | * when we are adding new content to the cache. |
5084 | */ | |
5085 | static void | |
5086 | arc_adapt(int bytes, arc_state_t *state) | |
5087 | { | |
5088 | int mult; | |
728d6ae9 | 5089 | uint64_t arc_p_min = (arc_c >> arc_p_min_shift); |
424fd7c3 TS |
5090 | int64_t mrug_size = zfs_refcount_count(&arc_mru_ghost->arcs_size); |
5091 | int64_t mfug_size = zfs_refcount_count(&arc_mfu_ghost->arcs_size); | |
34dc7c2f | 5092 | |
34dc7c2f BB |
5093 | ASSERT(bytes > 0); |
5094 | /* | |
5095 | * Adapt the target size of the MRU list: | |
5096 | * - if we just hit in the MRU ghost list, then increase | |
5097 | * the target size of the MRU list. | |
5098 | * - if we just hit in the MFU ghost list, then increase | |
5099 | * the target size of the MFU list by decreasing the | |
5100 | * target size of the MRU list. | |
5101 | */ | |
5102 | if (state == arc_mru_ghost) { | |
36da08ef | 5103 | mult = (mrug_size >= mfug_size) ? 1 : (mfug_size / mrug_size); |
62422785 PS |
5104 | if (!zfs_arc_p_dampener_disable) |
5105 | mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ | |
34dc7c2f | 5106 | |
728d6ae9 | 5107 | arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); |
34dc7c2f | 5108 | } else if (state == arc_mfu_ghost) { |
d164b209 BB |
5109 | uint64_t delta; |
5110 | ||
36da08ef | 5111 | mult = (mfug_size >= mrug_size) ? 1 : (mrug_size / mfug_size); |
62422785 PS |
5112 | if (!zfs_arc_p_dampener_disable) |
5113 | mult = MIN(mult, 10); | |
34dc7c2f | 5114 | |
d164b209 | 5115 | delta = MIN(bytes * mult, arc_p); |
728d6ae9 | 5116 | arc_p = MAX(arc_p_min, arc_p - delta); |
34dc7c2f BB |
5117 | } |
5118 | ASSERT((int64_t)arc_p >= 0); | |
5119 | ||
3ec34e55 BL |
5120 | /* |
5121 | * Wake reap thread if we do not have any available memory | |
5122 | */ | |
ca67b33a | 5123 | if (arc_reclaim_needed()) { |
3ec34e55 | 5124 | zthr_wakeup(arc_reap_zthr); |
ca67b33a MA |
5125 | return; |
5126 | } | |
5127 | ||
34dc7c2f BB |
5128 | if (arc_no_grow) |
5129 | return; | |
5130 | ||
5131 | if (arc_c >= arc_c_max) | |
5132 | return; | |
5133 | ||
5134 | /* | |
5135 | * If we're within (2 * maxblocksize) bytes of the target | |
5136 | * cache size, increment the target cache size | |
5137 | */ | |
935434ef | 5138 | ASSERT3U(arc_c, >=, 2ULL << SPA_MAXBLOCKSHIFT); |
c4c162c1 | 5139 | if (aggsum_upper_bound(&arc_sums.arcstat_size) >= |
17ca3018 | 5140 | arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { |
34dc7c2f BB |
5141 | atomic_add_64(&arc_c, (int64_t)bytes); |
5142 | if (arc_c > arc_c_max) | |
5143 | arc_c = arc_c_max; | |
5144 | else if (state == arc_anon) | |
5145 | atomic_add_64(&arc_p, (int64_t)bytes); | |
5146 | if (arc_p > arc_c) | |
5147 | arc_p = arc_c; | |
5148 | } | |
5149 | ASSERT((int64_t)arc_p >= 0); | |
5150 | } | |
5151 | ||
5152 | /* | |
ca0bf58d PS |
5153 | * Check if arc_size has grown past our upper threshold, determined by |
5154 | * zfs_arc_overflow_shift. | |
34dc7c2f | 5155 | */ |
f7de776d | 5156 | static arc_ovf_level_t |
6b88b4b5 | 5157 | arc_is_overflowing(boolean_t use_reserve) |
34dc7c2f | 5158 | { |
ca0bf58d | 5159 | /* Always allow at least one block of overflow */ |
5a902f5a | 5160 | int64_t overflow = MAX(SPA_MAXBLOCKSIZE, |
ca0bf58d | 5161 | arc_c >> zfs_arc_overflow_shift); |
34dc7c2f | 5162 | |
37fb3e43 PD |
5163 | /* |
5164 | * We just compare the lower bound here for performance reasons. Our | |
5165 | * primary goals are to make sure that the arc never grows without | |
5166 | * bound, and that it can reach its maximum size. This check | |
5167 | * accomplishes both goals. The maximum amount we could run over by is | |
5168 | * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block | |
5169 | * in the ARC. In practice, that's in the tens of MB, which is low | |
5170 | * enough to be safe. | |
5171 | */ | |
f7de776d AM |
5172 | int64_t over = aggsum_lower_bound(&arc_sums.arcstat_size) - |
5173 | arc_c - overflow / 2; | |
6b88b4b5 AM |
5174 | if (!use_reserve) |
5175 | overflow /= 2; | |
f7de776d AM |
5176 | return (over < 0 ? ARC_OVF_NONE : |
5177 | over < overflow ? ARC_OVF_SOME : ARC_OVF_SEVERE); | |
34dc7c2f BB |
5178 | } |
5179 | ||
a6255b7f | 5180 | static abd_t * |
e111c802 | 5181 | arc_get_data_abd(arc_buf_hdr_t *hdr, uint64_t size, void *tag, |
6b88b4b5 | 5182 | int alloc_flags) |
a6255b7f DQ |
5183 | { |
5184 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5185 | ||
6b88b4b5 | 5186 | arc_get_data_impl(hdr, size, tag, alloc_flags); |
a6255b7f DQ |
5187 | if (type == ARC_BUFC_METADATA) { |
5188 | return (abd_alloc(size, B_TRUE)); | |
5189 | } else { | |
5190 | ASSERT(type == ARC_BUFC_DATA); | |
5191 | return (abd_alloc(size, B_FALSE)); | |
5192 | } | |
5193 | } | |
5194 | ||
5195 | static void * | |
5196 | arc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, void *tag) | |
5197 | { | |
5198 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5199 | ||
6b88b4b5 | 5200 | arc_get_data_impl(hdr, size, tag, ARC_HDR_DO_ADAPT); |
a6255b7f DQ |
5201 | if (type == ARC_BUFC_METADATA) { |
5202 | return (zio_buf_alloc(size)); | |
5203 | } else { | |
5204 | ASSERT(type == ARC_BUFC_DATA); | |
5205 | return (zio_data_buf_alloc(size)); | |
5206 | } | |
5207 | } | |
5208 | ||
3442c2a0 MA |
5209 | /* |
5210 | * Wait for the specified amount of data (in bytes) to be evicted from the | |
5211 | * ARC, and for there to be sufficient free memory in the system. Waiting for | |
5212 | * eviction ensures that the memory used by the ARC decreases. Waiting for | |
5213 | * free memory ensures that the system won't run out of free pages, regardless | |
5214 | * of ARC behavior and settings. See arc_lowmem_init(). | |
5215 | */ | |
5216 | void | |
6b88b4b5 | 5217 | arc_wait_for_eviction(uint64_t amount, boolean_t use_reserve) |
3442c2a0 | 5218 | { |
6b88b4b5 | 5219 | switch (arc_is_overflowing(use_reserve)) { |
f7de776d AM |
5220 | case ARC_OVF_NONE: |
5221 | return; | |
5222 | case ARC_OVF_SOME: | |
5223 | /* | |
5224 | * This is a bit racy without taking arc_evict_lock, but the | |
5225 | * worst that can happen is we either call zthr_wakeup() extra | |
5226 | * time due to race with other thread here, or the set flag | |
5227 | * get cleared by arc_evict_cb(), which is unlikely due to | |
5228 | * big hysteresis, but also not important since at this level | |
5229 | * of overflow the eviction is purely advisory. Same time | |
5230 | * taking the global lock here every time without waiting for | |
5231 | * the actual eviction creates a significant lock contention. | |
5232 | */ | |
5233 | if (!arc_evict_needed) { | |
5234 | arc_evict_needed = B_TRUE; | |
5235 | zthr_wakeup(arc_evict_zthr); | |
5236 | } | |
5237 | return; | |
5238 | case ARC_OVF_SEVERE: | |
5239 | default: | |
5240 | { | |
5241 | arc_evict_waiter_t aw; | |
5242 | list_link_init(&aw.aew_node); | |
5243 | cv_init(&aw.aew_cv, NULL, CV_DEFAULT, NULL); | |
3442c2a0 | 5244 | |
f7de776d AM |
5245 | uint64_t last_count = 0; |
5246 | mutex_enter(&arc_evict_lock); | |
5247 | if (!list_is_empty(&arc_evict_waiters)) { | |
5248 | arc_evict_waiter_t *last = | |
5249 | list_tail(&arc_evict_waiters); | |
5250 | last_count = last->aew_count; | |
5251 | } else if (!arc_evict_needed) { | |
5252 | arc_evict_needed = B_TRUE; | |
5253 | zthr_wakeup(arc_evict_zthr); | |
5254 | } | |
5255 | /* | |
5256 | * Note, the last waiter's count may be less than | |
5257 | * arc_evict_count if we are low on memory in which | |
5258 | * case arc_evict_state_impl() may have deferred | |
5259 | * wakeups (but still incremented arc_evict_count). | |
5260 | */ | |
5261 | aw.aew_count = MAX(last_count, arc_evict_count) + amount; | |
3442c2a0 | 5262 | |
f7de776d | 5263 | list_insert_tail(&arc_evict_waiters, &aw); |
3442c2a0 | 5264 | |
f7de776d | 5265 | arc_set_need_free(); |
3442c2a0 | 5266 | |
f7de776d AM |
5267 | DTRACE_PROBE3(arc__wait__for__eviction, |
5268 | uint64_t, amount, | |
5269 | uint64_t, arc_evict_count, | |
5270 | uint64_t, aw.aew_count); | |
3442c2a0 | 5271 | |
f7de776d AM |
5272 | /* |
5273 | * We will be woken up either when arc_evict_count reaches | |
5274 | * aew_count, or when the ARC is no longer overflowing and | |
5275 | * eviction completes. | |
5276 | * In case of "false" wakeup, we will still be on the list. | |
5277 | */ | |
5278 | do { | |
3442c2a0 | 5279 | cv_wait(&aw.aew_cv, &arc_evict_lock); |
f7de776d AM |
5280 | } while (list_link_active(&aw.aew_node)); |
5281 | mutex_exit(&arc_evict_lock); | |
3442c2a0 | 5282 | |
f7de776d AM |
5283 | cv_destroy(&aw.aew_cv); |
5284 | } | |
3442c2a0 | 5285 | } |
3442c2a0 MA |
5286 | } |
5287 | ||
34dc7c2f | 5288 | /* |
d3c2ae1c GW |
5289 | * Allocate a block and return it to the caller. If we are hitting the |
5290 | * hard limit for the cache size, we must sleep, waiting for the eviction | |
5291 | * thread to catch up. If we're past the target size but below the hard | |
5292 | * limit, we'll only signal the reclaim thread and continue on. | |
34dc7c2f | 5293 | */ |
a6255b7f | 5294 | static void |
e111c802 | 5295 | arc_get_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag, |
6b88b4b5 | 5296 | int alloc_flags) |
34dc7c2f | 5297 | { |
a6255b7f DQ |
5298 | arc_state_t *state = hdr->b_l1hdr.b_state; |
5299 | arc_buf_contents_t type = arc_buf_type(hdr); | |
34dc7c2f | 5300 | |
6b88b4b5 | 5301 | if (alloc_flags & ARC_HDR_DO_ADAPT) |
e111c802 | 5302 | arc_adapt(size, state); |
34dc7c2f BB |
5303 | |
5304 | /* | |
3442c2a0 MA |
5305 | * If arc_size is currently overflowing, we must be adding data |
5306 | * faster than we are evicting. To ensure we don't compound the | |
ca0bf58d | 5307 | * problem by adding more data and forcing arc_size to grow even |
3442c2a0 MA |
5308 | * further past it's target size, we wait for the eviction thread to |
5309 | * make some progress. We also wait for there to be sufficient free | |
5310 | * memory in the system, as measured by arc_free_memory(). | |
5311 | * | |
5312 | * Specifically, we wait for zfs_arc_eviction_pct percent of the | |
5313 | * requested size to be evicted. This should be more than 100%, to | |
5314 | * ensure that that progress is also made towards getting arc_size | |
5315 | * under arc_c. See the comment above zfs_arc_eviction_pct. | |
34dc7c2f | 5316 | */ |
6b88b4b5 AM |
5317 | arc_wait_for_eviction(size * zfs_arc_eviction_pct / 100, |
5318 | alloc_flags & ARC_HDR_USE_RESERVE); | |
ab26409d | 5319 | |
d3c2ae1c | 5320 | VERIFY3U(hdr->b_type, ==, type); |
da8ccd0e | 5321 | if (type == ARC_BUFC_METADATA) { |
ca0bf58d PS |
5322 | arc_space_consume(size, ARC_SPACE_META); |
5323 | } else { | |
ca0bf58d | 5324 | arc_space_consume(size, ARC_SPACE_DATA); |
da8ccd0e PS |
5325 | } |
5326 | ||
34dc7c2f BB |
5327 | /* |
5328 | * Update the state size. Note that ghost states have a | |
5329 | * "ghost size" and so don't need to be updated. | |
5330 | */ | |
d3c2ae1c | 5331 | if (!GHOST_STATE(state)) { |
34dc7c2f | 5332 | |
424fd7c3 | 5333 | (void) zfs_refcount_add_many(&state->arcs_size, size, tag); |
ca0bf58d PS |
5334 | |
5335 | /* | |
5336 | * If this is reached via arc_read, the link is | |
5337 | * protected by the hash lock. If reached via | |
5338 | * arc_buf_alloc, the header should not be accessed by | |
5339 | * any other thread. And, if reached via arc_read_done, | |
5340 | * the hash lock will protect it if it's found in the | |
5341 | * hash table; otherwise no other thread should be | |
5342 | * trying to [add|remove]_reference it. | |
5343 | */ | |
5344 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 TS |
5345 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
5346 | (void) zfs_refcount_add_many(&state->arcs_esize[type], | |
d3c2ae1c | 5347 | size, tag); |
34dc7c2f | 5348 | } |
d3c2ae1c | 5349 | |
34dc7c2f BB |
5350 | /* |
5351 | * If we are growing the cache, and we are adding anonymous | |
5352 | * data, and we have outgrown arc_p, update arc_p | |
5353 | */ | |
c4c162c1 | 5354 | if (aggsum_upper_bound(&arc_sums.arcstat_size) < arc_c && |
37fb3e43 | 5355 | hdr->b_l1hdr.b_state == arc_anon && |
424fd7c3 TS |
5356 | (zfs_refcount_count(&arc_anon->arcs_size) + |
5357 | zfs_refcount_count(&arc_mru->arcs_size) > arc_p)) | |
34dc7c2f BB |
5358 | arc_p = MIN(arc_c, arc_p + size); |
5359 | } | |
a6255b7f DQ |
5360 | } |
5361 | ||
5362 | static void | |
5363 | arc_free_data_abd(arc_buf_hdr_t *hdr, abd_t *abd, uint64_t size, void *tag) | |
5364 | { | |
5365 | arc_free_data_impl(hdr, size, tag); | |
5366 | abd_free(abd); | |
5367 | } | |
5368 | ||
5369 | static void | |
5370 | arc_free_data_buf(arc_buf_hdr_t *hdr, void *buf, uint64_t size, void *tag) | |
5371 | { | |
5372 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5373 | ||
5374 | arc_free_data_impl(hdr, size, tag); | |
5375 | if (type == ARC_BUFC_METADATA) { | |
5376 | zio_buf_free(buf, size); | |
5377 | } else { | |
5378 | ASSERT(type == ARC_BUFC_DATA); | |
5379 | zio_data_buf_free(buf, size); | |
5380 | } | |
d3c2ae1c GW |
5381 | } |
5382 | ||
5383 | /* | |
5384 | * Free the arc data buffer. | |
5385 | */ | |
5386 | static void | |
a6255b7f | 5387 | arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) |
d3c2ae1c GW |
5388 | { |
5389 | arc_state_t *state = hdr->b_l1hdr.b_state; | |
5390 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5391 | ||
5392 | /* protected by hash lock, if in the hash table */ | |
5393 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 | 5394 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
5395 | ASSERT(state != arc_anon && state != arc_l2c_only); |
5396 | ||
424fd7c3 | 5397 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c GW |
5398 | size, tag); |
5399 | } | |
424fd7c3 | 5400 | (void) zfs_refcount_remove_many(&state->arcs_size, size, tag); |
d3c2ae1c GW |
5401 | |
5402 | VERIFY3U(hdr->b_type, ==, type); | |
5403 | if (type == ARC_BUFC_METADATA) { | |
d3c2ae1c GW |
5404 | arc_space_return(size, ARC_SPACE_META); |
5405 | } else { | |
5406 | ASSERT(type == ARC_BUFC_DATA); | |
d3c2ae1c GW |
5407 | arc_space_return(size, ARC_SPACE_DATA); |
5408 | } | |
34dc7c2f BB |
5409 | } |
5410 | ||
5411 | /* | |
5412 | * This routine is called whenever a buffer is accessed. | |
5413 | * NOTE: the hash lock is dropped in this function. | |
5414 | */ | |
5415 | static void | |
2a432414 | 5416 | arc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) |
34dc7c2f | 5417 | { |
428870ff BB |
5418 | clock_t now; |
5419 | ||
34dc7c2f | 5420 | ASSERT(MUTEX_HELD(hash_lock)); |
b9541d6b | 5421 | ASSERT(HDR_HAS_L1HDR(hdr)); |
34dc7c2f | 5422 | |
b9541d6b | 5423 | if (hdr->b_l1hdr.b_state == arc_anon) { |
34dc7c2f BB |
5424 | /* |
5425 | * This buffer is not in the cache, and does not | |
5426 | * appear in our "ghost" list. Add the new buffer | |
5427 | * to the MRU state. | |
5428 | */ | |
5429 | ||
b9541d6b CW |
5430 | ASSERT0(hdr->b_l1hdr.b_arc_access); |
5431 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); | |
2a432414 GW |
5432 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); |
5433 | arc_change_state(arc_mru, hdr, hash_lock); | |
34dc7c2f | 5434 | |
b9541d6b | 5435 | } else if (hdr->b_l1hdr.b_state == arc_mru) { |
428870ff BB |
5436 | now = ddi_get_lbolt(); |
5437 | ||
34dc7c2f BB |
5438 | /* |
5439 | * If this buffer is here because of a prefetch, then either: | |
5440 | * - clear the flag if this is a "referencing" read | |
5441 | * (any subsequent access will bump this into the MFU state). | |
5442 | * or | |
5443 | * - move the buffer to the head of the list if this is | |
5444 | * another prefetch (to make it less likely to be evicted). | |
5445 | */ | |
d4a72f23 | 5446 | if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { |
424fd7c3 | 5447 | if (zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { |
ca0bf58d PS |
5448 | /* link protected by hash lock */ |
5449 | ASSERT(multilist_link_active( | |
b9541d6b | 5450 | &hdr->b_l1hdr.b_arc_node)); |
34dc7c2f | 5451 | } else { |
08532162 GA |
5452 | if (HDR_HAS_L2HDR(hdr)) |
5453 | l2arc_hdr_arcstats_decrement_state(hdr); | |
d4a72f23 TC |
5454 | arc_hdr_clear_flags(hdr, |
5455 | ARC_FLAG_PREFETCH | | |
5456 | ARC_FLAG_PRESCIENT_PREFETCH); | |
cfe8e960 | 5457 | hdr->b_l1hdr.b_mru_hits++; |
34dc7c2f | 5458 | ARCSTAT_BUMP(arcstat_mru_hits); |
08532162 GA |
5459 | if (HDR_HAS_L2HDR(hdr)) |
5460 | l2arc_hdr_arcstats_increment_state(hdr); | |
34dc7c2f | 5461 | } |
b9541d6b | 5462 | hdr->b_l1hdr.b_arc_access = now; |
34dc7c2f BB |
5463 | return; |
5464 | } | |
5465 | ||
5466 | /* | |
5467 | * This buffer has been "accessed" only once so far, | |
5468 | * but it is still in the cache. Move it to the MFU | |
5469 | * state. | |
5470 | */ | |
b9541d6b CW |
5471 | if (ddi_time_after(now, hdr->b_l1hdr.b_arc_access + |
5472 | ARC_MINTIME)) { | |
34dc7c2f BB |
5473 | /* |
5474 | * More than 125ms have passed since we | |
5475 | * instantiated this buffer. Move it to the | |
5476 | * most frequently used state. | |
5477 | */ | |
b9541d6b | 5478 | hdr->b_l1hdr.b_arc_access = now; |
2a432414 GW |
5479 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
5480 | arc_change_state(arc_mfu, hdr, hash_lock); | |
34dc7c2f | 5481 | } |
cfe8e960 | 5482 | hdr->b_l1hdr.b_mru_hits++; |
34dc7c2f | 5483 | ARCSTAT_BUMP(arcstat_mru_hits); |
b9541d6b | 5484 | } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { |
34dc7c2f BB |
5485 | arc_state_t *new_state; |
5486 | /* | |
5487 | * This buffer has been "accessed" recently, but | |
5488 | * was evicted from the cache. Move it to the | |
5489 | * MFU state. | |
5490 | */ | |
d4a72f23 | 5491 | if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { |
34dc7c2f | 5492 | new_state = arc_mru; |
424fd7c3 | 5493 | if (zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) { |
08532162 GA |
5494 | if (HDR_HAS_L2HDR(hdr)) |
5495 | l2arc_hdr_arcstats_decrement_state(hdr); | |
d4a72f23 TC |
5496 | arc_hdr_clear_flags(hdr, |
5497 | ARC_FLAG_PREFETCH | | |
5498 | ARC_FLAG_PRESCIENT_PREFETCH); | |
08532162 GA |
5499 | if (HDR_HAS_L2HDR(hdr)) |
5500 | l2arc_hdr_arcstats_increment_state(hdr); | |
d4a72f23 | 5501 | } |
2a432414 | 5502 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); |
34dc7c2f BB |
5503 | } else { |
5504 | new_state = arc_mfu; | |
2a432414 | 5505 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
34dc7c2f BB |
5506 | } |
5507 | ||
b9541d6b | 5508 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 | 5509 | arc_change_state(new_state, hdr, hash_lock); |
34dc7c2f | 5510 | |
cfe8e960 | 5511 | hdr->b_l1hdr.b_mru_ghost_hits++; |
34dc7c2f | 5512 | ARCSTAT_BUMP(arcstat_mru_ghost_hits); |
b9541d6b | 5513 | } else if (hdr->b_l1hdr.b_state == arc_mfu) { |
34dc7c2f BB |
5514 | /* |
5515 | * This buffer has been accessed more than once and is | |
5516 | * still in the cache. Keep it in the MFU state. | |
5517 | * | |
5518 | * NOTE: an add_reference() that occurred when we did | |
5519 | * the arc_read() will have kicked this off the list. | |
5520 | * If it was a prefetch, we will explicitly move it to | |
5521 | * the head of the list now. | |
5522 | */ | |
d4a72f23 | 5523 | |
cfe8e960 | 5524 | hdr->b_l1hdr.b_mfu_hits++; |
34dc7c2f | 5525 | ARCSTAT_BUMP(arcstat_mfu_hits); |
b9541d6b CW |
5526 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
5527 | } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { | |
34dc7c2f BB |
5528 | arc_state_t *new_state = arc_mfu; |
5529 | /* | |
5530 | * This buffer has been accessed more than once but has | |
5531 | * been evicted from the cache. Move it back to the | |
5532 | * MFU state. | |
5533 | */ | |
5534 | ||
d4a72f23 | 5535 | if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { |
34dc7c2f BB |
5536 | /* |
5537 | * This is a prefetch access... | |
5538 | * move this block back to the MRU state. | |
5539 | */ | |
34dc7c2f BB |
5540 | new_state = arc_mru; |
5541 | } | |
5542 | ||
b9541d6b | 5543 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 GW |
5544 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
5545 | arc_change_state(new_state, hdr, hash_lock); | |
34dc7c2f | 5546 | |
cfe8e960 | 5547 | hdr->b_l1hdr.b_mfu_ghost_hits++; |
34dc7c2f | 5548 | ARCSTAT_BUMP(arcstat_mfu_ghost_hits); |
b9541d6b | 5549 | } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { |
34dc7c2f BB |
5550 | /* |
5551 | * This buffer is on the 2nd Level ARC. | |
5552 | */ | |
5553 | ||
b9541d6b | 5554 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 GW |
5555 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
5556 | arc_change_state(arc_mfu, hdr, hash_lock); | |
34dc7c2f | 5557 | } else { |
b9541d6b CW |
5558 | cmn_err(CE_PANIC, "invalid arc state 0x%p", |
5559 | hdr->b_l1hdr.b_state); | |
34dc7c2f BB |
5560 | } |
5561 | } | |
5562 | ||
0873bb63 BB |
5563 | /* |
5564 | * This routine is called by dbuf_hold() to update the arc_access() state | |
5565 | * which otherwise would be skipped for entries in the dbuf cache. | |
5566 | */ | |
5567 | void | |
5568 | arc_buf_access(arc_buf_t *buf) | |
5569 | { | |
5570 | mutex_enter(&buf->b_evict_lock); | |
5571 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
5572 | ||
5573 | /* | |
5574 | * Avoid taking the hash_lock when possible as an optimization. | |
5575 | * The header must be checked again under the hash_lock in order | |
5576 | * to handle the case where it is concurrently being released. | |
5577 | */ | |
5578 | if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) { | |
5579 | mutex_exit(&buf->b_evict_lock); | |
5580 | return; | |
5581 | } | |
5582 | ||
5583 | kmutex_t *hash_lock = HDR_LOCK(hdr); | |
5584 | mutex_enter(hash_lock); | |
5585 | ||
5586 | if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) { | |
5587 | mutex_exit(hash_lock); | |
5588 | mutex_exit(&buf->b_evict_lock); | |
5589 | ARCSTAT_BUMP(arcstat_access_skip); | |
5590 | return; | |
5591 | } | |
5592 | ||
5593 | mutex_exit(&buf->b_evict_lock); | |
5594 | ||
5595 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || | |
5596 | hdr->b_l1hdr.b_state == arc_mfu); | |
5597 | ||
5598 | DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); | |
5599 | arc_access(hdr, hash_lock); | |
5600 | mutex_exit(hash_lock); | |
5601 | ||
5602 | ARCSTAT_BUMP(arcstat_hits); | |
5603 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr) && !HDR_PRESCIENT_PREFETCH(hdr), | |
5604 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data, metadata, hits); | |
5605 | } | |
5606 | ||
b5256303 | 5607 | /* a generic arc_read_done_func_t which you can use */ |
34dc7c2f | 5608 | void |
d4a72f23 TC |
5609 | arc_bcopy_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, |
5610 | arc_buf_t *buf, void *arg) | |
34dc7c2f | 5611 | { |
14e4e3cb AZ |
5612 | (void) zio, (void) zb, (void) bp; |
5613 | ||
d4a72f23 TC |
5614 | if (buf == NULL) |
5615 | return; | |
5616 | ||
5617 | bcopy(buf->b_data, arg, arc_buf_size(buf)); | |
d3c2ae1c | 5618 | arc_buf_destroy(buf, arg); |
34dc7c2f BB |
5619 | } |
5620 | ||
b5256303 | 5621 | /* a generic arc_read_done_func_t */ |
34dc7c2f | 5622 | void |
d4a72f23 TC |
5623 | arc_getbuf_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, |
5624 | arc_buf_t *buf, void *arg) | |
34dc7c2f | 5625 | { |
14e4e3cb | 5626 | (void) zb, (void) bp; |
34dc7c2f | 5627 | arc_buf_t **bufp = arg; |
d4a72f23 TC |
5628 | |
5629 | if (buf == NULL) { | |
c3bd3fb4 | 5630 | ASSERT(zio == NULL || zio->io_error != 0); |
34dc7c2f BB |
5631 | *bufp = NULL; |
5632 | } else { | |
c3bd3fb4 | 5633 | ASSERT(zio == NULL || zio->io_error == 0); |
34dc7c2f | 5634 | *bufp = buf; |
c3bd3fb4 | 5635 | ASSERT(buf->b_data != NULL); |
34dc7c2f BB |
5636 | } |
5637 | } | |
5638 | ||
d3c2ae1c GW |
5639 | static void |
5640 | arc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp) | |
5641 | { | |
5642 | if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { | |
5643 | ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0); | |
b5256303 | 5644 | ASSERT3U(arc_hdr_get_compress(hdr), ==, ZIO_COMPRESS_OFF); |
d3c2ae1c GW |
5645 | } else { |
5646 | if (HDR_COMPRESSION_ENABLED(hdr)) { | |
b5256303 | 5647 | ASSERT3U(arc_hdr_get_compress(hdr), ==, |
d3c2ae1c GW |
5648 | BP_GET_COMPRESS(bp)); |
5649 | } | |
5650 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); | |
5651 | ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp)); | |
b5256303 | 5652 | ASSERT3U(!!HDR_PROTECTED(hdr), ==, BP_IS_PROTECTED(bp)); |
d3c2ae1c GW |
5653 | } |
5654 | } | |
5655 | ||
34dc7c2f BB |
5656 | static void |
5657 | arc_read_done(zio_t *zio) | |
5658 | { | |
b5256303 | 5659 | blkptr_t *bp = zio->io_bp; |
d3c2ae1c | 5660 | arc_buf_hdr_t *hdr = zio->io_private; |
9b67f605 | 5661 | kmutex_t *hash_lock = NULL; |
524b4217 DK |
5662 | arc_callback_t *callback_list; |
5663 | arc_callback_t *acb; | |
2aa34383 | 5664 | boolean_t freeable = B_FALSE; |
a7004725 | 5665 | |
34dc7c2f BB |
5666 | /* |
5667 | * The hdr was inserted into hash-table and removed from lists | |
5668 | * prior to starting I/O. We should find this header, since | |
5669 | * it's in the hash table, and it should be legit since it's | |
5670 | * not possible to evict it during the I/O. The only possible | |
5671 | * reason for it not to be found is if we were freed during the | |
5672 | * read. | |
5673 | */ | |
9b67f605 | 5674 | if (HDR_IN_HASH_TABLE(hdr)) { |
31df97cd DB |
5675 | arc_buf_hdr_t *found; |
5676 | ||
9b67f605 MA |
5677 | ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); |
5678 | ASSERT3U(hdr->b_dva.dva_word[0], ==, | |
5679 | BP_IDENTITY(zio->io_bp)->dva_word[0]); | |
5680 | ASSERT3U(hdr->b_dva.dva_word[1], ==, | |
5681 | BP_IDENTITY(zio->io_bp)->dva_word[1]); | |
5682 | ||
31df97cd | 5683 | found = buf_hash_find(hdr->b_spa, zio->io_bp, &hash_lock); |
9b67f605 | 5684 | |
d3c2ae1c | 5685 | ASSERT((found == hdr && |
9b67f605 MA |
5686 | DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || |
5687 | (found == hdr && HDR_L2_READING(hdr))); | |
d3c2ae1c GW |
5688 | ASSERT3P(hash_lock, !=, NULL); |
5689 | } | |
5690 | ||
b5256303 TC |
5691 | if (BP_IS_PROTECTED(bp)) { |
5692 | hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp); | |
5693 | hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset; | |
5694 | zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt, | |
5695 | hdr->b_crypt_hdr.b_iv); | |
5696 | ||
df42e20a RE |
5697 | if (zio->io_error == 0) { |
5698 | if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) { | |
5699 | void *tmpbuf; | |
5700 | ||
5701 | tmpbuf = abd_borrow_buf_copy(zio->io_abd, | |
5702 | sizeof (zil_chain_t)); | |
5703 | zio_crypt_decode_mac_zil(tmpbuf, | |
5704 | hdr->b_crypt_hdr.b_mac); | |
5705 | abd_return_buf(zio->io_abd, tmpbuf, | |
5706 | sizeof (zil_chain_t)); | |
5707 | } else { | |
5708 | zio_crypt_decode_mac_bp(bp, | |
5709 | hdr->b_crypt_hdr.b_mac); | |
5710 | } | |
b5256303 TC |
5711 | } |
5712 | } | |
5713 | ||
d4a72f23 | 5714 | if (zio->io_error == 0) { |
d3c2ae1c GW |
5715 | /* byteswap if necessary */ |
5716 | if (BP_SHOULD_BYTESWAP(zio->io_bp)) { | |
5717 | if (BP_GET_LEVEL(zio->io_bp) > 0) { | |
5718 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; | |
5719 | } else { | |
5720 | hdr->b_l1hdr.b_byteswap = | |
5721 | DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); | |
5722 | } | |
5723 | } else { | |
5724 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
5725 | } | |
10b3c7f5 MN |
5726 | if (!HDR_L2_READING(hdr)) { |
5727 | hdr->b_complevel = zio->io_prop.zp_complevel; | |
5728 | } | |
9b67f605 | 5729 | } |
34dc7c2f | 5730 | |
d3c2ae1c | 5731 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED); |
c6f5e9d9 GA |
5732 | if (l2arc_noprefetch && HDR_PREFETCH(hdr)) |
5733 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE); | |
34dc7c2f | 5734 | |
b9541d6b | 5735 | callback_list = hdr->b_l1hdr.b_acb; |
d3c2ae1c | 5736 | ASSERT3P(callback_list, !=, NULL); |
34dc7c2f | 5737 | |
d4a72f23 TC |
5738 | if (hash_lock && zio->io_error == 0 && |
5739 | hdr->b_l1hdr.b_state == arc_anon) { | |
428870ff BB |
5740 | /* |
5741 | * Only call arc_access on anonymous buffers. This is because | |
5742 | * if we've issued an I/O for an evicted buffer, we've already | |
5743 | * called arc_access (to prevent any simultaneous readers from | |
5744 | * getting confused). | |
5745 | */ | |
5746 | arc_access(hdr, hash_lock); | |
5747 | } | |
5748 | ||
524b4217 DK |
5749 | /* |
5750 | * If a read request has a callback (i.e. acb_done is not NULL), then we | |
5751 | * make a buf containing the data according to the parameters which were | |
5752 | * passed in. The implementation of arc_buf_alloc_impl() ensures that we | |
5753 | * aren't needlessly decompressing the data multiple times. | |
5754 | */ | |
a7004725 | 5755 | int callback_cnt = 0; |
2aa34383 | 5756 | for (acb = callback_list; acb != NULL; acb = acb->acb_next) { |
923d7303 | 5757 | if (!acb->acb_done || acb->acb_nobuf) |
2aa34383 DK |
5758 | continue; |
5759 | ||
2aa34383 | 5760 | callback_cnt++; |
524b4217 | 5761 | |
d4a72f23 TC |
5762 | if (zio->io_error != 0) |
5763 | continue; | |
5764 | ||
b5256303 | 5765 | int error = arc_buf_alloc_impl(hdr, zio->io_spa, |
be9a5c35 | 5766 | &acb->acb_zb, acb->acb_private, acb->acb_encrypted, |
d4a72f23 | 5767 | acb->acb_compressed, acb->acb_noauth, B_TRUE, |
440a3eb9 | 5768 | &acb->acb_buf); |
b5256303 TC |
5769 | |
5770 | /* | |
440a3eb9 | 5771 | * Assert non-speculative zios didn't fail because an |
b5256303 TC |
5772 | * encryption key wasn't loaded |
5773 | */ | |
a2c2ed1b | 5774 | ASSERT((zio->io_flags & ZIO_FLAG_SPECULATIVE) || |
be9a5c35 | 5775 | error != EACCES); |
b5256303 TC |
5776 | |
5777 | /* | |
5778 | * If we failed to decrypt, report an error now (as the zio | |
5779 | * layer would have done if it had done the transforms). | |
5780 | */ | |
5781 | if (error == ECKSUM) { | |
5782 | ASSERT(BP_IS_PROTECTED(bp)); | |
5783 | error = SET_ERROR(EIO); | |
b5256303 | 5784 | if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) { |
be9a5c35 | 5785 | spa_log_error(zio->io_spa, &acb->acb_zb); |
1144586b TS |
5786 | (void) zfs_ereport_post( |
5787 | FM_EREPORT_ZFS_AUTHENTICATION, | |
4f072827 | 5788 | zio->io_spa, NULL, &acb->acb_zb, zio, 0); |
b5256303 TC |
5789 | } |
5790 | } | |
5791 | ||
c3bd3fb4 TC |
5792 | if (error != 0) { |
5793 | /* | |
5794 | * Decompression or decryption failed. Set | |
5795 | * io_error so that when we call acb_done | |
5796 | * (below), we will indicate that the read | |
5797 | * failed. Note that in the unusual case | |
5798 | * where one callback is compressed and another | |
5799 | * uncompressed, we will mark all of them | |
5800 | * as failed, even though the uncompressed | |
5801 | * one can't actually fail. In this case, | |
5802 | * the hdr will not be anonymous, because | |
5803 | * if there are multiple callbacks, it's | |
5804 | * because multiple threads found the same | |
5805 | * arc buf in the hash table. | |
5806 | */ | |
524b4217 | 5807 | zio->io_error = error; |
c3bd3fb4 | 5808 | } |
34dc7c2f | 5809 | } |
c3bd3fb4 TC |
5810 | |
5811 | /* | |
5812 | * If there are multiple callbacks, we must have the hash lock, | |
5813 | * because the only way for multiple threads to find this hdr is | |
5814 | * in the hash table. This ensures that if there are multiple | |
5815 | * callbacks, the hdr is not anonymous. If it were anonymous, | |
5816 | * we couldn't use arc_buf_destroy() in the error case below. | |
5817 | */ | |
5818 | ASSERT(callback_cnt < 2 || hash_lock != NULL); | |
5819 | ||
b9541d6b | 5820 | hdr->b_l1hdr.b_acb = NULL; |
d3c2ae1c | 5821 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
440a3eb9 | 5822 | if (callback_cnt == 0) |
b5256303 | 5823 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); |
34dc7c2f | 5824 | |
424fd7c3 | 5825 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || |
b9541d6b | 5826 | callback_list != NULL); |
34dc7c2f | 5827 | |
d4a72f23 | 5828 | if (zio->io_error == 0) { |
d3c2ae1c GW |
5829 | arc_hdr_verify(hdr, zio->io_bp); |
5830 | } else { | |
5831 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); | |
b9541d6b | 5832 | if (hdr->b_l1hdr.b_state != arc_anon) |
34dc7c2f BB |
5833 | arc_change_state(arc_anon, hdr, hash_lock); |
5834 | if (HDR_IN_HASH_TABLE(hdr)) | |
5835 | buf_hash_remove(hdr); | |
424fd7c3 | 5836 | freeable = zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt); |
34dc7c2f BB |
5837 | } |
5838 | ||
5839 | /* | |
5840 | * Broadcast before we drop the hash_lock to avoid the possibility | |
5841 | * that the hdr (and hence the cv) might be freed before we get to | |
5842 | * the cv_broadcast(). | |
5843 | */ | |
b9541d6b | 5844 | cv_broadcast(&hdr->b_l1hdr.b_cv); |
34dc7c2f | 5845 | |
b9541d6b | 5846 | if (hash_lock != NULL) { |
34dc7c2f BB |
5847 | mutex_exit(hash_lock); |
5848 | } else { | |
5849 | /* | |
5850 | * This block was freed while we waited for the read to | |
5851 | * complete. It has been removed from the hash table and | |
5852 | * moved to the anonymous state (so that it won't show up | |
5853 | * in the cache). | |
5854 | */ | |
b9541d6b | 5855 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
424fd7c3 | 5856 | freeable = zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt); |
34dc7c2f BB |
5857 | } |
5858 | ||
5859 | /* execute each callback and free its structure */ | |
5860 | while ((acb = callback_list) != NULL) { | |
c3bd3fb4 TC |
5861 | if (acb->acb_done != NULL) { |
5862 | if (zio->io_error != 0 && acb->acb_buf != NULL) { | |
5863 | /* | |
5864 | * If arc_buf_alloc_impl() fails during | |
5865 | * decompression, the buf will still be | |
5866 | * allocated, and needs to be freed here. | |
5867 | */ | |
5868 | arc_buf_destroy(acb->acb_buf, | |
5869 | acb->acb_private); | |
5870 | acb->acb_buf = NULL; | |
5871 | } | |
d4a72f23 TC |
5872 | acb->acb_done(zio, &zio->io_bookmark, zio->io_bp, |
5873 | acb->acb_buf, acb->acb_private); | |
b5256303 | 5874 | } |
34dc7c2f BB |
5875 | |
5876 | if (acb->acb_zio_dummy != NULL) { | |
5877 | acb->acb_zio_dummy->io_error = zio->io_error; | |
5878 | zio_nowait(acb->acb_zio_dummy); | |
5879 | } | |
5880 | ||
5881 | callback_list = acb->acb_next; | |
5882 | kmem_free(acb, sizeof (arc_callback_t)); | |
5883 | } | |
5884 | ||
5885 | if (freeable) | |
5886 | arc_hdr_destroy(hdr); | |
5887 | } | |
5888 | ||
5889 | /* | |
5c839890 | 5890 | * "Read" the block at the specified DVA (in bp) via the |
34dc7c2f BB |
5891 | * cache. If the block is found in the cache, invoke the provided |
5892 | * callback immediately and return. Note that the `zio' parameter | |
5893 | * in the callback will be NULL in this case, since no IO was | |
5894 | * required. If the block is not in the cache pass the read request | |
5895 | * on to the spa with a substitute callback function, so that the | |
5896 | * requested block will be added to the cache. | |
5897 | * | |
5898 | * If a read request arrives for a block that has a read in-progress, | |
5899 | * either wait for the in-progress read to complete (and return the | |
5900 | * results); or, if this is a read with a "done" func, add a record | |
5901 | * to the read to invoke the "done" func when the read completes, | |
5902 | * and return; or just return. | |
5903 | * | |
5904 | * arc_read_done() will invoke all the requested "done" functions | |
5905 | * for readers of this block. | |
5906 | */ | |
5907 | int | |
b5256303 TC |
5908 | arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, |
5909 | arc_read_done_func_t *done, void *private, zio_priority_t priority, | |
5910 | int zio_flags, arc_flags_t *arc_flags, const zbookmark_phys_t *zb) | |
34dc7c2f | 5911 | { |
9b67f605 | 5912 | arc_buf_hdr_t *hdr = NULL; |
9b67f605 | 5913 | kmutex_t *hash_lock = NULL; |
34dc7c2f | 5914 | zio_t *rzio; |
3541dc6d | 5915 | uint64_t guid = spa_load_guid(spa); |
b5256303 TC |
5916 | boolean_t compressed_read = (zio_flags & ZIO_FLAG_RAW_COMPRESS) != 0; |
5917 | boolean_t encrypted_read = BP_IS_ENCRYPTED(bp) && | |
5918 | (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0; | |
5919 | boolean_t noauth_read = BP_IS_AUTHENTICATED(bp) && | |
5920 | (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0; | |
0902c457 | 5921 | boolean_t embedded_bp = !!BP_IS_EMBEDDED(bp); |
1e4732cb | 5922 | boolean_t no_buf = *arc_flags & ARC_FLAG_NO_BUF; |
1421c891 | 5923 | int rc = 0; |
34dc7c2f | 5924 | |
0902c457 | 5925 | ASSERT(!embedded_bp || |
9b67f605 | 5926 | BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); |
30af21b0 PD |
5927 | ASSERT(!BP_IS_HOLE(bp)); |
5928 | ASSERT(!BP_IS_REDACTED(bp)); | |
9b67f605 | 5929 | |
1e9231ad MR |
5930 | /* |
5931 | * Normally SPL_FSTRANS will already be set since kernel threads which | |
5932 | * expect to call the DMU interfaces will set it when created. System | |
5933 | * calls are similarly handled by setting/cleaning the bit in the | |
5934 | * registered callback (module/os/.../zfs/zpl_*). | |
5935 | * | |
5936 | * External consumers such as Lustre which call the exported DMU | |
5937 | * interfaces may not have set SPL_FSTRANS. To avoid a deadlock | |
5938 | * on the hash_lock always set and clear the bit. | |
5939 | */ | |
5940 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
34dc7c2f | 5941 | top: |
b9ec4a15 BB |
5942 | /* |
5943 | * Verify the block pointer contents are reasonable. This should | |
5944 | * always be the case since the blkptr is protected by a checksum. | |
5945 | * However, if there is damage it's desirable to detect this early | |
5946 | * and treat it as a checksum error. This allows an alternate blkptr | |
5947 | * to be tried when one is available (e.g. ditto blocks). | |
5948 | */ | |
5949 | if (!zfs_blkptr_verify(spa, bp, zio_flags & ZIO_FLAG_CONFIG_WRITER, | |
5950 | BLK_VERIFY_LOG)) { | |
5951 | rc = SET_ERROR(ECKSUM); | |
5952 | goto out; | |
5953 | } | |
5954 | ||
0902c457 | 5955 | if (!embedded_bp) { |
9b67f605 MA |
5956 | /* |
5957 | * Embedded BP's have no DVA and require no I/O to "read". | |
5958 | * Create an anonymous arc buf to back it. | |
5959 | */ | |
5960 | hdr = buf_hash_find(guid, bp, &hash_lock); | |
5961 | } | |
5962 | ||
b5256303 TC |
5963 | /* |
5964 | * Determine if we have an L1 cache hit or a cache miss. For simplicity | |
e1cfd73f | 5965 | * we maintain encrypted data separately from compressed / uncompressed |
b5256303 TC |
5966 | * data. If the user is requesting raw encrypted data and we don't have |
5967 | * that in the header we will read from disk to guarantee that we can | |
5968 | * get it even if the encryption keys aren't loaded. | |
5969 | */ | |
5970 | if (hdr != NULL && HDR_HAS_L1HDR(hdr) && (HDR_HAS_RABD(hdr) || | |
5971 | (hdr->b_l1hdr.b_pabd != NULL && !encrypted_read))) { | |
d3c2ae1c | 5972 | arc_buf_t *buf = NULL; |
2a432414 | 5973 | *arc_flags |= ARC_FLAG_CACHED; |
34dc7c2f BB |
5974 | |
5975 | if (HDR_IO_IN_PROGRESS(hdr)) { | |
a8b2e306 | 5976 | zio_t *head_zio = hdr->b_l1hdr.b_acb->acb_zio_head; |
34dc7c2f | 5977 | |
1dc32a67 MA |
5978 | if (*arc_flags & ARC_FLAG_CACHED_ONLY) { |
5979 | mutex_exit(hash_lock); | |
5980 | ARCSTAT_BUMP(arcstat_cached_only_in_progress); | |
5981 | rc = SET_ERROR(ENOENT); | |
5982 | goto out; | |
5983 | } | |
5984 | ||
a8b2e306 | 5985 | ASSERT3P(head_zio, !=, NULL); |
7f60329a MA |
5986 | if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) && |
5987 | priority == ZIO_PRIORITY_SYNC_READ) { | |
5988 | /* | |
a8b2e306 TC |
5989 | * This is a sync read that needs to wait for |
5990 | * an in-flight async read. Request that the | |
5991 | * zio have its priority upgraded. | |
7f60329a | 5992 | */ |
a8b2e306 TC |
5993 | zio_change_priority(head_zio, priority); |
5994 | DTRACE_PROBE1(arc__async__upgrade__sync, | |
7f60329a | 5995 | arc_buf_hdr_t *, hdr); |
a8b2e306 | 5996 | ARCSTAT_BUMP(arcstat_async_upgrade_sync); |
7f60329a MA |
5997 | } |
5998 | if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { | |
d3c2ae1c GW |
5999 | arc_hdr_clear_flags(hdr, |
6000 | ARC_FLAG_PREDICTIVE_PREFETCH); | |
7f60329a MA |
6001 | } |
6002 | ||
2a432414 | 6003 | if (*arc_flags & ARC_FLAG_WAIT) { |
b9541d6b | 6004 | cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); |
34dc7c2f BB |
6005 | mutex_exit(hash_lock); |
6006 | goto top; | |
6007 | } | |
2a432414 | 6008 | ASSERT(*arc_flags & ARC_FLAG_NOWAIT); |
34dc7c2f | 6009 | |
923d7303 | 6010 | if (done) { |
7f60329a | 6011 | arc_callback_t *acb = NULL; |
34dc7c2f BB |
6012 | |
6013 | acb = kmem_zalloc(sizeof (arc_callback_t), | |
79c76d5b | 6014 | KM_SLEEP); |
34dc7c2f BB |
6015 | acb->acb_done = done; |
6016 | acb->acb_private = private; | |
a7004725 | 6017 | acb->acb_compressed = compressed_read; |
440a3eb9 TC |
6018 | acb->acb_encrypted = encrypted_read; |
6019 | acb->acb_noauth = noauth_read; | |
923d7303 | 6020 | acb->acb_nobuf = no_buf; |
be9a5c35 | 6021 | acb->acb_zb = *zb; |
34dc7c2f BB |
6022 | if (pio != NULL) |
6023 | acb->acb_zio_dummy = zio_null(pio, | |
d164b209 | 6024 | spa, NULL, NULL, NULL, zio_flags); |
34dc7c2f | 6025 | |
d3c2ae1c | 6026 | ASSERT3P(acb->acb_done, !=, NULL); |
a8b2e306 | 6027 | acb->acb_zio_head = head_zio; |
b9541d6b CW |
6028 | acb->acb_next = hdr->b_l1hdr.b_acb; |
6029 | hdr->b_l1hdr.b_acb = acb; | |
34dc7c2f BB |
6030 | } |
6031 | mutex_exit(hash_lock); | |
1421c891 | 6032 | goto out; |
34dc7c2f BB |
6033 | } |
6034 | ||
b9541d6b CW |
6035 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || |
6036 | hdr->b_l1hdr.b_state == arc_mfu); | |
34dc7c2f | 6037 | |
1e4732cb | 6038 | if (done && !no_buf) { |
7f60329a MA |
6039 | if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { |
6040 | /* | |
6041 | * This is a demand read which does not have to | |
6042 | * wait for i/o because we did a predictive | |
6043 | * prefetch i/o for it, which has completed. | |
6044 | */ | |
6045 | DTRACE_PROBE1( | |
6046 | arc__demand__hit__predictive__prefetch, | |
6047 | arc_buf_hdr_t *, hdr); | |
6048 | ARCSTAT_BUMP( | |
6049 | arcstat_demand_hit_predictive_prefetch); | |
d3c2ae1c GW |
6050 | arc_hdr_clear_flags(hdr, |
6051 | ARC_FLAG_PREDICTIVE_PREFETCH); | |
7f60329a | 6052 | } |
d4a72f23 TC |
6053 | |
6054 | if (hdr->b_flags & ARC_FLAG_PRESCIENT_PREFETCH) { | |
6055 | ARCSTAT_BUMP( | |
6056 | arcstat_demand_hit_prescient_prefetch); | |
6057 | arc_hdr_clear_flags(hdr, | |
6058 | ARC_FLAG_PRESCIENT_PREFETCH); | |
6059 | } | |
6060 | ||
0902c457 | 6061 | ASSERT(!embedded_bp || !BP_IS_HOLE(bp)); |
d3c2ae1c | 6062 | |
524b4217 | 6063 | /* Get a buf with the desired data in it. */ |
be9a5c35 TC |
6064 | rc = arc_buf_alloc_impl(hdr, spa, zb, private, |
6065 | encrypted_read, compressed_read, noauth_read, | |
6066 | B_TRUE, &buf); | |
a2c2ed1b TC |
6067 | if (rc == ECKSUM) { |
6068 | /* | |
6069 | * Convert authentication and decryption errors | |
be9a5c35 TC |
6070 | * to EIO (and generate an ereport if needed) |
6071 | * before leaving the ARC. | |
a2c2ed1b TC |
6072 | */ |
6073 | rc = SET_ERROR(EIO); | |
be9a5c35 TC |
6074 | if ((zio_flags & ZIO_FLAG_SPECULATIVE) == 0) { |
6075 | spa_log_error(spa, zb); | |
1144586b | 6076 | (void) zfs_ereport_post( |
be9a5c35 | 6077 | FM_EREPORT_ZFS_AUTHENTICATION, |
4f072827 | 6078 | spa, NULL, zb, NULL, 0); |
be9a5c35 | 6079 | } |
a2c2ed1b | 6080 | } |
d4a72f23 | 6081 | if (rc != 0) { |
2c24b5b1 TC |
6082 | (void) remove_reference(hdr, hash_lock, |
6083 | private); | |
6084 | arc_buf_destroy_impl(buf); | |
d4a72f23 TC |
6085 | buf = NULL; |
6086 | } | |
6087 | ||
a2c2ed1b TC |
6088 | /* assert any errors weren't due to unloaded keys */ |
6089 | ASSERT((zio_flags & ZIO_FLAG_SPECULATIVE) || | |
be9a5c35 | 6090 | rc != EACCES); |
2a432414 | 6091 | } else if (*arc_flags & ARC_FLAG_PREFETCH && |
08532162 GA |
6092 | zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { |
6093 | if (HDR_HAS_L2HDR(hdr)) | |
6094 | l2arc_hdr_arcstats_decrement_state(hdr); | |
d3c2ae1c | 6095 | arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); |
08532162 GA |
6096 | if (HDR_HAS_L2HDR(hdr)) |
6097 | l2arc_hdr_arcstats_increment_state(hdr); | |
34dc7c2f BB |
6098 | } |
6099 | DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); | |
6100 | arc_access(hdr, hash_lock); | |
d4a72f23 TC |
6101 | if (*arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) |
6102 | arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH); | |
2a432414 | 6103 | if (*arc_flags & ARC_FLAG_L2CACHE) |
d3c2ae1c | 6104 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); |
34dc7c2f BB |
6105 | mutex_exit(hash_lock); |
6106 | ARCSTAT_BUMP(arcstat_hits); | |
b9541d6b CW |
6107 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), |
6108 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), | |
34dc7c2f BB |
6109 | data, metadata, hits); |
6110 | ||
6111 | if (done) | |
d4a72f23 | 6112 | done(NULL, zb, bp, buf, private); |
34dc7c2f | 6113 | } else { |
d3c2ae1c GW |
6114 | uint64_t lsize = BP_GET_LSIZE(bp); |
6115 | uint64_t psize = BP_GET_PSIZE(bp); | |
9b67f605 | 6116 | arc_callback_t *acb; |
b128c09f | 6117 | vdev_t *vd = NULL; |
a117a6d6 | 6118 | uint64_t addr = 0; |
d164b209 | 6119 | boolean_t devw = B_FALSE; |
d3c2ae1c | 6120 | uint64_t size; |
440a3eb9 | 6121 | abd_t *hdr_abd; |
e111c802 | 6122 | int alloc_flags = encrypted_read ? ARC_HDR_ALLOC_RDATA : 0; |
34dc7c2f | 6123 | |
1dc32a67 MA |
6124 | if (*arc_flags & ARC_FLAG_CACHED_ONLY) { |
6125 | rc = SET_ERROR(ENOENT); | |
6126 | if (hash_lock != NULL) | |
6127 | mutex_exit(hash_lock); | |
6128 | goto out; | |
6129 | } | |
6130 | ||
34dc7c2f | 6131 | if (hdr == NULL) { |
0902c457 TC |
6132 | /* |
6133 | * This block is not in the cache or it has | |
6134 | * embedded data. | |
6135 | */ | |
9b67f605 | 6136 | arc_buf_hdr_t *exists = NULL; |
34dc7c2f | 6137 | arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); |
d3c2ae1c | 6138 | hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, |
6b88b4b5 | 6139 | BP_IS_PROTECTED(bp), BP_GET_COMPRESS(bp), 0, type); |
d3c2ae1c | 6140 | |
0902c457 | 6141 | if (!embedded_bp) { |
9b67f605 MA |
6142 | hdr->b_dva = *BP_IDENTITY(bp); |
6143 | hdr->b_birth = BP_PHYSICAL_BIRTH(bp); | |
9b67f605 MA |
6144 | exists = buf_hash_insert(hdr, &hash_lock); |
6145 | } | |
6146 | if (exists != NULL) { | |
34dc7c2f BB |
6147 | /* somebody beat us to the hash insert */ |
6148 | mutex_exit(hash_lock); | |
428870ff | 6149 | buf_discard_identity(hdr); |
d3c2ae1c | 6150 | arc_hdr_destroy(hdr); |
34dc7c2f BB |
6151 | goto top; /* restart the IO request */ |
6152 | } | |
6b88b4b5 | 6153 | alloc_flags |= ARC_HDR_DO_ADAPT; |
34dc7c2f | 6154 | } else { |
b9541d6b | 6155 | /* |
b5256303 TC |
6156 | * This block is in the ghost cache or encrypted data |
6157 | * was requested and we didn't have it. If it was | |
6158 | * L2-only (and thus didn't have an L1 hdr), | |
6159 | * we realloc the header to add an L1 hdr. | |
b9541d6b CW |
6160 | */ |
6161 | if (!HDR_HAS_L1HDR(hdr)) { | |
6162 | hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, | |
6163 | hdr_full_cache); | |
6164 | } | |
6165 | ||
b5256303 TC |
6166 | if (GHOST_STATE(hdr->b_l1hdr.b_state)) { |
6167 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); | |
6168 | ASSERT(!HDR_HAS_RABD(hdr)); | |
6169 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
424fd7c3 TS |
6170 | ASSERT0(zfs_refcount_count( |
6171 | &hdr->b_l1hdr.b_refcnt)); | |
b5256303 TC |
6172 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
6173 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
6174 | } else if (HDR_IO_IN_PROGRESS(hdr)) { | |
6175 | /* | |
6176 | * If this header already had an IO in progress | |
6177 | * and we are performing another IO to fetch | |
6178 | * encrypted data we must wait until the first | |
6179 | * IO completes so as not to confuse | |
6180 | * arc_read_done(). This should be very rare | |
6181 | * and so the performance impact shouldn't | |
6182 | * matter. | |
6183 | */ | |
6184 | cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); | |
6185 | mutex_exit(hash_lock); | |
6186 | goto top; | |
6187 | } | |
34dc7c2f | 6188 | |
7f60329a | 6189 | /* |
d3c2ae1c | 6190 | * This is a delicate dance that we play here. |
b5256303 TC |
6191 | * This hdr might be in the ghost list so we access |
6192 | * it to move it out of the ghost list before we | |
d3c2ae1c GW |
6193 | * initiate the read. If it's a prefetch then |
6194 | * it won't have a callback so we'll remove the | |
6195 | * reference that arc_buf_alloc_impl() created. We | |
6196 | * do this after we've called arc_access() to | |
6197 | * avoid hitting an assert in remove_reference(). | |
7f60329a | 6198 | */ |
e111c802 | 6199 | arc_adapt(arc_hdr_size(hdr), hdr->b_l1hdr.b_state); |
428870ff | 6200 | arc_access(hdr, hash_lock); |
d3c2ae1c | 6201 | } |
d3c2ae1c | 6202 | |
6b88b4b5 | 6203 | arc_hdr_alloc_abd(hdr, alloc_flags); |
b5256303 TC |
6204 | if (encrypted_read) { |
6205 | ASSERT(HDR_HAS_RABD(hdr)); | |
6206 | size = HDR_GET_PSIZE(hdr); | |
6207 | hdr_abd = hdr->b_crypt_hdr.b_rabd; | |
d3c2ae1c | 6208 | zio_flags |= ZIO_FLAG_RAW; |
b5256303 TC |
6209 | } else { |
6210 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
6211 | size = arc_hdr_size(hdr); | |
6212 | hdr_abd = hdr->b_l1hdr.b_pabd; | |
6213 | ||
6214 | if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) { | |
6215 | zio_flags |= ZIO_FLAG_RAW_COMPRESS; | |
6216 | } | |
6217 | ||
6218 | /* | |
6219 | * For authenticated bp's, we do not ask the ZIO layer | |
6220 | * to authenticate them since this will cause the entire | |
6221 | * IO to fail if the key isn't loaded. Instead, we | |
6222 | * defer authentication until arc_buf_fill(), which will | |
6223 | * verify the data when the key is available. | |
6224 | */ | |
6225 | if (BP_IS_AUTHENTICATED(bp)) | |
6226 | zio_flags |= ZIO_FLAG_RAW_ENCRYPT; | |
34dc7c2f BB |
6227 | } |
6228 | ||
b5256303 | 6229 | if (*arc_flags & ARC_FLAG_PREFETCH && |
08532162 GA |
6230 | zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { |
6231 | if (HDR_HAS_L2HDR(hdr)) | |
6232 | l2arc_hdr_arcstats_decrement_state(hdr); | |
d3c2ae1c | 6233 | arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); |
08532162 GA |
6234 | if (HDR_HAS_L2HDR(hdr)) |
6235 | l2arc_hdr_arcstats_increment_state(hdr); | |
6236 | } | |
d4a72f23 TC |
6237 | if (*arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) |
6238 | arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH); | |
d3c2ae1c GW |
6239 | if (*arc_flags & ARC_FLAG_L2CACHE) |
6240 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); | |
b5256303 TC |
6241 | if (BP_IS_AUTHENTICATED(bp)) |
6242 | arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH); | |
d3c2ae1c GW |
6243 | if (BP_GET_LEVEL(bp) > 0) |
6244 | arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT); | |
7f60329a | 6245 | if (*arc_flags & ARC_FLAG_PREDICTIVE_PREFETCH) |
d3c2ae1c | 6246 | arc_hdr_set_flags(hdr, ARC_FLAG_PREDICTIVE_PREFETCH); |
b9541d6b | 6247 | ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); |
428870ff | 6248 | |
79c76d5b | 6249 | acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); |
34dc7c2f BB |
6250 | acb->acb_done = done; |
6251 | acb->acb_private = private; | |
2aa34383 | 6252 | acb->acb_compressed = compressed_read; |
b5256303 TC |
6253 | acb->acb_encrypted = encrypted_read; |
6254 | acb->acb_noauth = noauth_read; | |
be9a5c35 | 6255 | acb->acb_zb = *zb; |
34dc7c2f | 6256 | |
d3c2ae1c | 6257 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
b9541d6b | 6258 | hdr->b_l1hdr.b_acb = acb; |
d3c2ae1c | 6259 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
34dc7c2f | 6260 | |
b9541d6b CW |
6261 | if (HDR_HAS_L2HDR(hdr) && |
6262 | (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { | |
6263 | devw = hdr->b_l2hdr.b_dev->l2ad_writing; | |
6264 | addr = hdr->b_l2hdr.b_daddr; | |
b128c09f | 6265 | /* |
a1d477c2 | 6266 | * Lock out L2ARC device removal. |
b128c09f BB |
6267 | */ |
6268 | if (vdev_is_dead(vd) || | |
6269 | !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) | |
6270 | vd = NULL; | |
6271 | } | |
6272 | ||
a8b2e306 TC |
6273 | /* |
6274 | * We count both async reads and scrub IOs as asynchronous so | |
6275 | * that both can be upgraded in the event of a cache hit while | |
6276 | * the read IO is still in-flight. | |
6277 | */ | |
6278 | if (priority == ZIO_PRIORITY_ASYNC_READ || | |
6279 | priority == ZIO_PRIORITY_SCRUB) | |
d3c2ae1c GW |
6280 | arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); |
6281 | else | |
6282 | arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); | |
6283 | ||
e49f1e20 | 6284 | /* |
0902c457 TC |
6285 | * At this point, we have a level 1 cache miss or a blkptr |
6286 | * with embedded data. Try again in L2ARC if possible. | |
e49f1e20 | 6287 | */ |
d3c2ae1c GW |
6288 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize); |
6289 | ||
0902c457 TC |
6290 | /* |
6291 | * Skip ARC stat bump for block pointers with embedded | |
6292 | * data. The data are read from the blkptr itself via | |
6293 | * decode_embedded_bp_compressed(). | |
6294 | */ | |
6295 | if (!embedded_bp) { | |
6296 | DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, | |
6297 | blkptr_t *, bp, uint64_t, lsize, | |
6298 | zbookmark_phys_t *, zb); | |
6299 | ARCSTAT_BUMP(arcstat_misses); | |
6300 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), | |
6301 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data, | |
6302 | metadata, misses); | |
64e0fe14 | 6303 | zfs_racct_read(size, 1); |
0902c457 | 6304 | } |
34dc7c2f | 6305 | |
666aa69f AM |
6306 | /* Check if the spa even has l2 configured */ |
6307 | const boolean_t spa_has_l2 = l2arc_ndev != 0 && | |
6308 | spa->spa_l2cache.sav_count > 0; | |
6309 | ||
6310 | if (vd != NULL && spa_has_l2 && !(l2arc_norw && devw)) { | |
34dc7c2f BB |
6311 | /* |
6312 | * Read from the L2ARC if the following are true: | |
b128c09f BB |
6313 | * 1. The L2ARC vdev was previously cached. |
6314 | * 2. This buffer still has L2ARC metadata. | |
6315 | * 3. This buffer isn't currently writing to the L2ARC. | |
6316 | * 4. The L2ARC entry wasn't evicted, which may | |
6317 | * also have invalidated the vdev. | |
08532162 | 6318 | * 5. This isn't prefetch or l2arc_noprefetch is 0. |
34dc7c2f | 6319 | */ |
b9541d6b | 6320 | if (HDR_HAS_L2HDR(hdr) && |
d164b209 BB |
6321 | !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && |
6322 | !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { | |
34dc7c2f | 6323 | l2arc_read_callback_t *cb; |
82710e99 GDN |
6324 | abd_t *abd; |
6325 | uint64_t asize; | |
34dc7c2f BB |
6326 | |
6327 | DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); | |
6328 | ARCSTAT_BUMP(arcstat_l2_hits); | |
cfe8e960 | 6329 | hdr->b_l2hdr.b_hits++; |
34dc7c2f | 6330 | |
34dc7c2f | 6331 | cb = kmem_zalloc(sizeof (l2arc_read_callback_t), |
79c76d5b | 6332 | KM_SLEEP); |
d3c2ae1c | 6333 | cb->l2rcb_hdr = hdr; |
34dc7c2f BB |
6334 | cb->l2rcb_bp = *bp; |
6335 | cb->l2rcb_zb = *zb; | |
b128c09f | 6336 | cb->l2rcb_flags = zio_flags; |
34dc7c2f | 6337 | |
fc34dfba AJ |
6338 | /* |
6339 | * When Compressed ARC is disabled, but the | |
6340 | * L2ARC block is compressed, arc_hdr_size() | |
6341 | * will have returned LSIZE rather than PSIZE. | |
6342 | */ | |
6343 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
6344 | !HDR_COMPRESSION_ENABLED(hdr) && | |
6345 | HDR_GET_PSIZE(hdr) != 0) { | |
6346 | size = HDR_GET_PSIZE(hdr); | |
6347 | } | |
6348 | ||
82710e99 GDN |
6349 | asize = vdev_psize_to_asize(vd, size); |
6350 | if (asize != size) { | |
6351 | abd = abd_alloc_for_io(asize, | |
6352 | HDR_ISTYPE_METADATA(hdr)); | |
6353 | cb->l2rcb_abd = abd; | |
6354 | } else { | |
b5256303 | 6355 | abd = hdr_abd; |
82710e99 GDN |
6356 | } |
6357 | ||
a117a6d6 | 6358 | ASSERT(addr >= VDEV_LABEL_START_SIZE && |
82710e99 | 6359 | addr + asize <= vd->vdev_psize - |
a117a6d6 GW |
6360 | VDEV_LABEL_END_SIZE); |
6361 | ||
34dc7c2f | 6362 | /* |
b128c09f BB |
6363 | * l2arc read. The SCL_L2ARC lock will be |
6364 | * released by l2arc_read_done(). | |
3a17a7a9 SK |
6365 | * Issue a null zio if the underlying buffer |
6366 | * was squashed to zero size by compression. | |
34dc7c2f | 6367 | */ |
b5256303 | 6368 | ASSERT3U(arc_hdr_get_compress(hdr), !=, |
d3c2ae1c GW |
6369 | ZIO_COMPRESS_EMPTY); |
6370 | rzio = zio_read_phys(pio, vd, addr, | |
82710e99 | 6371 | asize, abd, |
d3c2ae1c GW |
6372 | ZIO_CHECKSUM_OFF, |
6373 | l2arc_read_done, cb, priority, | |
6374 | zio_flags | ZIO_FLAG_DONT_CACHE | | |
6375 | ZIO_FLAG_CANFAIL | | |
6376 | ZIO_FLAG_DONT_PROPAGATE | | |
6377 | ZIO_FLAG_DONT_RETRY, B_FALSE); | |
a8b2e306 TC |
6378 | acb->acb_zio_head = rzio; |
6379 | ||
6380 | if (hash_lock != NULL) | |
6381 | mutex_exit(hash_lock); | |
d3c2ae1c | 6382 | |
34dc7c2f BB |
6383 | DTRACE_PROBE2(l2arc__read, vdev_t *, vd, |
6384 | zio_t *, rzio); | |
b5256303 TC |
6385 | ARCSTAT_INCR(arcstat_l2_read_bytes, |
6386 | HDR_GET_PSIZE(hdr)); | |
34dc7c2f | 6387 | |
2a432414 | 6388 | if (*arc_flags & ARC_FLAG_NOWAIT) { |
b128c09f | 6389 | zio_nowait(rzio); |
1421c891 | 6390 | goto out; |
b128c09f | 6391 | } |
34dc7c2f | 6392 | |
2a432414 | 6393 | ASSERT(*arc_flags & ARC_FLAG_WAIT); |
b128c09f | 6394 | if (zio_wait(rzio) == 0) |
1421c891 | 6395 | goto out; |
b128c09f BB |
6396 | |
6397 | /* l2arc read error; goto zio_read() */ | |
a8b2e306 TC |
6398 | if (hash_lock != NULL) |
6399 | mutex_enter(hash_lock); | |
34dc7c2f BB |
6400 | } else { |
6401 | DTRACE_PROBE1(l2arc__miss, | |
6402 | arc_buf_hdr_t *, hdr); | |
6403 | ARCSTAT_BUMP(arcstat_l2_misses); | |
6404 | if (HDR_L2_WRITING(hdr)) | |
6405 | ARCSTAT_BUMP(arcstat_l2_rw_clash); | |
b128c09f | 6406 | spa_config_exit(spa, SCL_L2ARC, vd); |
34dc7c2f | 6407 | } |
d164b209 BB |
6408 | } else { |
6409 | if (vd != NULL) | |
6410 | spa_config_exit(spa, SCL_L2ARC, vd); | |
666aa69f | 6411 | |
0902c457 | 6412 | /* |
666aa69f AM |
6413 | * Only a spa with l2 should contribute to l2 |
6414 | * miss stats. (Including the case of having a | |
6415 | * faulted cache device - that's also a miss.) | |
0902c457 | 6416 | */ |
666aa69f AM |
6417 | if (spa_has_l2) { |
6418 | /* | |
6419 | * Skip ARC stat bump for block pointers with | |
6420 | * embedded data. The data are read from the | |
6421 | * blkptr itself via | |
6422 | * decode_embedded_bp_compressed(). | |
6423 | */ | |
6424 | if (!embedded_bp) { | |
6425 | DTRACE_PROBE1(l2arc__miss, | |
6426 | arc_buf_hdr_t *, hdr); | |
6427 | ARCSTAT_BUMP(arcstat_l2_misses); | |
6428 | } | |
d164b209 | 6429 | } |
34dc7c2f | 6430 | } |
34dc7c2f | 6431 | |
b5256303 | 6432 | rzio = zio_read(pio, spa, bp, hdr_abd, size, |
d3c2ae1c | 6433 | arc_read_done, hdr, priority, zio_flags, zb); |
a8b2e306 TC |
6434 | acb->acb_zio_head = rzio; |
6435 | ||
6436 | if (hash_lock != NULL) | |
6437 | mutex_exit(hash_lock); | |
34dc7c2f | 6438 | |
2a432414 | 6439 | if (*arc_flags & ARC_FLAG_WAIT) { |
1421c891 PS |
6440 | rc = zio_wait(rzio); |
6441 | goto out; | |
6442 | } | |
34dc7c2f | 6443 | |
2a432414 | 6444 | ASSERT(*arc_flags & ARC_FLAG_NOWAIT); |
34dc7c2f BB |
6445 | zio_nowait(rzio); |
6446 | } | |
1421c891 PS |
6447 | |
6448 | out: | |
157ef7f6 | 6449 | /* embedded bps don't actually go to disk */ |
0902c457 | 6450 | if (!embedded_bp) |
157ef7f6 | 6451 | spa_read_history_add(spa, zb, *arc_flags); |
1e9231ad | 6452 | spl_fstrans_unmark(cookie); |
1421c891 | 6453 | return (rc); |
34dc7c2f BB |
6454 | } |
6455 | ||
ab26409d BB |
6456 | arc_prune_t * |
6457 | arc_add_prune_callback(arc_prune_func_t *func, void *private) | |
6458 | { | |
6459 | arc_prune_t *p; | |
6460 | ||
d1d7e268 | 6461 | p = kmem_alloc(sizeof (*p), KM_SLEEP); |
ab26409d BB |
6462 | p->p_pfunc = func; |
6463 | p->p_private = private; | |
6464 | list_link_init(&p->p_node); | |
424fd7c3 | 6465 | zfs_refcount_create(&p->p_refcnt); |
ab26409d BB |
6466 | |
6467 | mutex_enter(&arc_prune_mtx); | |
c13060e4 | 6468 | zfs_refcount_add(&p->p_refcnt, &arc_prune_list); |
ab26409d BB |
6469 | list_insert_head(&arc_prune_list, p); |
6470 | mutex_exit(&arc_prune_mtx); | |
6471 | ||
6472 | return (p); | |
6473 | } | |
6474 | ||
6475 | void | |
6476 | arc_remove_prune_callback(arc_prune_t *p) | |
6477 | { | |
4442f60d | 6478 | boolean_t wait = B_FALSE; |
ab26409d BB |
6479 | mutex_enter(&arc_prune_mtx); |
6480 | list_remove(&arc_prune_list, p); | |
424fd7c3 | 6481 | if (zfs_refcount_remove(&p->p_refcnt, &arc_prune_list) > 0) |
4442f60d | 6482 | wait = B_TRUE; |
ab26409d | 6483 | mutex_exit(&arc_prune_mtx); |
4442f60d CC |
6484 | |
6485 | /* wait for arc_prune_task to finish */ | |
6486 | if (wait) | |
6487 | taskq_wait_outstanding(arc_prune_taskq, 0); | |
424fd7c3 TS |
6488 | ASSERT0(zfs_refcount_count(&p->p_refcnt)); |
6489 | zfs_refcount_destroy(&p->p_refcnt); | |
4442f60d | 6490 | kmem_free(p, sizeof (*p)); |
ab26409d BB |
6491 | } |
6492 | ||
df4474f9 MA |
6493 | /* |
6494 | * Notify the arc that a block was freed, and thus will never be used again. | |
6495 | */ | |
6496 | void | |
6497 | arc_freed(spa_t *spa, const blkptr_t *bp) | |
6498 | { | |
6499 | arc_buf_hdr_t *hdr; | |
6500 | kmutex_t *hash_lock; | |
6501 | uint64_t guid = spa_load_guid(spa); | |
6502 | ||
9b67f605 MA |
6503 | ASSERT(!BP_IS_EMBEDDED(bp)); |
6504 | ||
6505 | hdr = buf_hash_find(guid, bp, &hash_lock); | |
df4474f9 MA |
6506 | if (hdr == NULL) |
6507 | return; | |
df4474f9 | 6508 | |
d3c2ae1c GW |
6509 | /* |
6510 | * We might be trying to free a block that is still doing I/O | |
6511 | * (i.e. prefetch) or has a reference (i.e. a dedup-ed, | |
6512 | * dmu_sync-ed block). If this block is being prefetched, then it | |
6513 | * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr | |
6514 | * until the I/O completes. A block may also have a reference if it is | |
6515 | * part of a dedup-ed, dmu_synced write. The dmu_sync() function would | |
6516 | * have written the new block to its final resting place on disk but | |
6517 | * without the dedup flag set. This would have left the hdr in the MRU | |
6518 | * state and discoverable. When the txg finally syncs it detects that | |
6519 | * the block was overridden in open context and issues an override I/O. | |
6520 | * Since this is a dedup block, the override I/O will determine if the | |
6521 | * block is already in the DDT. If so, then it will replace the io_bp | |
6522 | * with the bp from the DDT and allow the I/O to finish. When the I/O | |
6523 | * reaches the done callback, dbuf_write_override_done, it will | |
6524 | * check to see if the io_bp and io_bp_override are identical. | |
6525 | * If they are not, then it indicates that the bp was replaced with | |
6526 | * the bp in the DDT and the override bp is freed. This allows | |
6527 | * us to arrive here with a reference on a block that is being | |
6528 | * freed. So if we have an I/O in progress, or a reference to | |
6529 | * this hdr, then we don't destroy the hdr. | |
6530 | */ | |
6531 | if (!HDR_HAS_L1HDR(hdr) || (!HDR_IO_IN_PROGRESS(hdr) && | |
424fd7c3 | 6532 | zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt))) { |
d3c2ae1c GW |
6533 | arc_change_state(arc_anon, hdr, hash_lock); |
6534 | arc_hdr_destroy(hdr); | |
df4474f9 | 6535 | mutex_exit(hash_lock); |
bd089c54 | 6536 | } else { |
d3c2ae1c | 6537 | mutex_exit(hash_lock); |
34dc7c2f | 6538 | } |
34dc7c2f | 6539 | |
34dc7c2f BB |
6540 | } |
6541 | ||
6542 | /* | |
e49f1e20 WA |
6543 | * Release this buffer from the cache, making it an anonymous buffer. This |
6544 | * must be done after a read and prior to modifying the buffer contents. | |
34dc7c2f | 6545 | * If the buffer has more than one reference, we must make |
b128c09f | 6546 | * a new hdr for the buffer. |
34dc7c2f BB |
6547 | */ |
6548 | void | |
6549 | arc_release(arc_buf_t *buf, void *tag) | |
6550 | { | |
b9541d6b | 6551 | arc_buf_hdr_t *hdr = buf->b_hdr; |
34dc7c2f | 6552 | |
428870ff | 6553 | /* |
ca0bf58d | 6554 | * It would be nice to assert that if its DMU metadata (level > |
428870ff BB |
6555 | * 0 || it's the dnode file), then it must be syncing context. |
6556 | * But we don't know that information at this level. | |
6557 | */ | |
6558 | ||
6559 | mutex_enter(&buf->b_evict_lock); | |
b128c09f | 6560 | |
ca0bf58d PS |
6561 | ASSERT(HDR_HAS_L1HDR(hdr)); |
6562 | ||
b9541d6b CW |
6563 | /* |
6564 | * We don't grab the hash lock prior to this check, because if | |
6565 | * the buffer's header is in the arc_anon state, it won't be | |
6566 | * linked into the hash table. | |
6567 | */ | |
6568 | if (hdr->b_l1hdr.b_state == arc_anon) { | |
6569 | mutex_exit(&buf->b_evict_lock); | |
6570 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
6571 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); | |
6572 | ASSERT(!HDR_HAS_L2HDR(hdr)); | |
34dc7c2f | 6573 | |
d3c2ae1c | 6574 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); |
424fd7c3 | 6575 | ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); |
b9541d6b CW |
6576 | ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); |
6577 | ||
b9541d6b | 6578 | hdr->b_l1hdr.b_arc_access = 0; |
d3c2ae1c GW |
6579 | |
6580 | /* | |
6581 | * If the buf is being overridden then it may already | |
6582 | * have a hdr that is not empty. | |
6583 | */ | |
6584 | buf_discard_identity(hdr); | |
b9541d6b CW |
6585 | arc_buf_thaw(buf); |
6586 | ||
6587 | return; | |
34dc7c2f BB |
6588 | } |
6589 | ||
1c27024e | 6590 | kmutex_t *hash_lock = HDR_LOCK(hdr); |
b9541d6b CW |
6591 | mutex_enter(hash_lock); |
6592 | ||
6593 | /* | |
6594 | * This assignment is only valid as long as the hash_lock is | |
6595 | * held, we must be careful not to reference state or the | |
6596 | * b_state field after dropping the lock. | |
6597 | */ | |
1c27024e | 6598 | arc_state_t *state = hdr->b_l1hdr.b_state; |
b9541d6b CW |
6599 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
6600 | ASSERT3P(state, !=, arc_anon); | |
6601 | ||
6602 | /* this buffer is not on any list */ | |
424fd7c3 | 6603 | ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), >, 0); |
b9541d6b CW |
6604 | |
6605 | if (HDR_HAS_L2HDR(hdr)) { | |
b9541d6b | 6606 | mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); |
ca0bf58d PS |
6607 | |
6608 | /* | |
d962d5da PS |
6609 | * We have to recheck this conditional again now that |
6610 | * we're holding the l2ad_mtx to prevent a race with | |
6611 | * another thread which might be concurrently calling | |
6612 | * l2arc_evict(). In that case, l2arc_evict() might have | |
6613 | * destroyed the header's L2 portion as we were waiting | |
6614 | * to acquire the l2ad_mtx. | |
ca0bf58d | 6615 | */ |
d962d5da PS |
6616 | if (HDR_HAS_L2HDR(hdr)) |
6617 | arc_hdr_l2hdr_destroy(hdr); | |
ca0bf58d | 6618 | |
b9541d6b | 6619 | mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); |
b128c09f BB |
6620 | } |
6621 | ||
34dc7c2f BB |
6622 | /* |
6623 | * Do we have more than one buf? | |
6624 | */ | |
d3c2ae1c | 6625 | if (hdr->b_l1hdr.b_bufcnt > 1) { |
34dc7c2f | 6626 | arc_buf_hdr_t *nhdr; |
d164b209 | 6627 | uint64_t spa = hdr->b_spa; |
d3c2ae1c GW |
6628 | uint64_t psize = HDR_GET_PSIZE(hdr); |
6629 | uint64_t lsize = HDR_GET_LSIZE(hdr); | |
b5256303 TC |
6630 | boolean_t protected = HDR_PROTECTED(hdr); |
6631 | enum zio_compress compress = arc_hdr_get_compress(hdr); | |
b9541d6b | 6632 | arc_buf_contents_t type = arc_buf_type(hdr); |
d3c2ae1c | 6633 | VERIFY3U(hdr->b_type, ==, type); |
34dc7c2f | 6634 | |
b9541d6b | 6635 | ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); |
d3c2ae1c GW |
6636 | (void) remove_reference(hdr, hash_lock, tag); |
6637 | ||
524b4217 | 6638 | if (arc_buf_is_shared(buf) && !ARC_BUF_COMPRESSED(buf)) { |
d3c2ae1c | 6639 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); |
524b4217 DK |
6640 | ASSERT(ARC_BUF_LAST(buf)); |
6641 | } | |
d3c2ae1c | 6642 | |
34dc7c2f | 6643 | /* |
428870ff | 6644 | * Pull the data off of this hdr and attach it to |
d3c2ae1c GW |
6645 | * a new anonymous hdr. Also find the last buffer |
6646 | * in the hdr's buffer list. | |
34dc7c2f | 6647 | */ |
a7004725 | 6648 | arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); |
d3c2ae1c | 6649 | ASSERT3P(lastbuf, !=, NULL); |
34dc7c2f | 6650 | |
d3c2ae1c GW |
6651 | /* |
6652 | * If the current arc_buf_t and the hdr are sharing their data | |
524b4217 | 6653 | * buffer, then we must stop sharing that block. |
d3c2ae1c GW |
6654 | */ |
6655 | if (arc_buf_is_shared(buf)) { | |
6656 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); | |
d3c2ae1c GW |
6657 | VERIFY(!arc_buf_is_shared(lastbuf)); |
6658 | ||
6659 | /* | |
6660 | * First, sever the block sharing relationship between | |
a7004725 | 6661 | * buf and the arc_buf_hdr_t. |
d3c2ae1c GW |
6662 | */ |
6663 | arc_unshare_buf(hdr, buf); | |
2aa34383 DK |
6664 | |
6665 | /* | |
a6255b7f | 6666 | * Now we need to recreate the hdr's b_pabd. Since we |
524b4217 | 6667 | * have lastbuf handy, we try to share with it, but if |
a6255b7f | 6668 | * we can't then we allocate a new b_pabd and copy the |
524b4217 | 6669 | * data from buf into it. |
2aa34383 | 6670 | */ |
524b4217 DK |
6671 | if (arc_can_share(hdr, lastbuf)) { |
6672 | arc_share_buf(hdr, lastbuf); | |
6673 | } else { | |
e111c802 | 6674 | arc_hdr_alloc_abd(hdr, ARC_HDR_DO_ADAPT); |
a6255b7f DQ |
6675 | abd_copy_from_buf(hdr->b_l1hdr.b_pabd, |
6676 | buf->b_data, psize); | |
2aa34383 | 6677 | } |
d3c2ae1c GW |
6678 | VERIFY3P(lastbuf->b_data, !=, NULL); |
6679 | } else if (HDR_SHARED_DATA(hdr)) { | |
2aa34383 DK |
6680 | /* |
6681 | * Uncompressed shared buffers are always at the end | |
6682 | * of the list. Compressed buffers don't have the | |
6683 | * same requirements. This makes it hard to | |
6684 | * simply assert that the lastbuf is shared so | |
6685 | * we rely on the hdr's compression flags to determine | |
6686 | * if we have a compressed, shared buffer. | |
6687 | */ | |
6688 | ASSERT(arc_buf_is_shared(lastbuf) || | |
b5256303 | 6689 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); |
2aa34383 | 6690 | ASSERT(!ARC_BUF_SHARED(buf)); |
d3c2ae1c | 6691 | } |
b5256303 TC |
6692 | |
6693 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); | |
b9541d6b | 6694 | ASSERT3P(state, !=, arc_l2c_only); |
36da08ef | 6695 | |
424fd7c3 | 6696 | (void) zfs_refcount_remove_many(&state->arcs_size, |
2aa34383 | 6697 | arc_buf_size(buf), buf); |
36da08ef | 6698 | |
424fd7c3 | 6699 | if (zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { |
b9541d6b | 6700 | ASSERT3P(state, !=, arc_l2c_only); |
424fd7c3 TS |
6701 | (void) zfs_refcount_remove_many( |
6702 | &state->arcs_esize[type], | |
2aa34383 | 6703 | arc_buf_size(buf), buf); |
34dc7c2f | 6704 | } |
1eb5bfa3 | 6705 | |
d3c2ae1c | 6706 | hdr->b_l1hdr.b_bufcnt -= 1; |
b5256303 TC |
6707 | if (ARC_BUF_ENCRYPTED(buf)) |
6708 | hdr->b_crypt_hdr.b_ebufcnt -= 1; | |
6709 | ||
34dc7c2f | 6710 | arc_cksum_verify(buf); |
498877ba | 6711 | arc_buf_unwatch(buf); |
34dc7c2f | 6712 | |
f486f584 TC |
6713 | /* if this is the last uncompressed buf free the checksum */ |
6714 | if (!arc_hdr_has_uncompressed_buf(hdr)) | |
6715 | arc_cksum_free(hdr); | |
6716 | ||
34dc7c2f BB |
6717 | mutex_exit(hash_lock); |
6718 | ||
d3c2ae1c | 6719 | /* |
a6255b7f | 6720 | * Allocate a new hdr. The new hdr will contain a b_pabd |
d3c2ae1c GW |
6721 | * buffer which will be freed in arc_write(). |
6722 | */ | |
b5256303 | 6723 | nhdr = arc_hdr_alloc(spa, psize, lsize, protected, |
6b88b4b5 | 6724 | compress, hdr->b_complevel, type); |
d3c2ae1c GW |
6725 | ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL); |
6726 | ASSERT0(nhdr->b_l1hdr.b_bufcnt); | |
424fd7c3 | 6727 | ASSERT0(zfs_refcount_count(&nhdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
6728 | VERIFY3U(nhdr->b_type, ==, type); |
6729 | ASSERT(!HDR_SHARED_DATA(nhdr)); | |
b9541d6b | 6730 | |
d3c2ae1c GW |
6731 | nhdr->b_l1hdr.b_buf = buf; |
6732 | nhdr->b_l1hdr.b_bufcnt = 1; | |
b5256303 TC |
6733 | if (ARC_BUF_ENCRYPTED(buf)) |
6734 | nhdr->b_crypt_hdr.b_ebufcnt = 1; | |
c13060e4 | 6735 | (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); |
34dc7c2f | 6736 | buf->b_hdr = nhdr; |
d3c2ae1c | 6737 | |
428870ff | 6738 | mutex_exit(&buf->b_evict_lock); |
424fd7c3 | 6739 | (void) zfs_refcount_add_many(&arc_anon->arcs_size, |
5e8ff256 | 6740 | arc_buf_size(buf), buf); |
34dc7c2f | 6741 | } else { |
428870ff | 6742 | mutex_exit(&buf->b_evict_lock); |
424fd7c3 | 6743 | ASSERT(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); |
ca0bf58d PS |
6744 | /* protected by hash lock, or hdr is on arc_anon */ |
6745 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
34dc7c2f | 6746 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b CW |
6747 | hdr->b_l1hdr.b_mru_hits = 0; |
6748 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
6749 | hdr->b_l1hdr.b_mfu_hits = 0; | |
6750 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
b9541d6b CW |
6751 | arc_change_state(arc_anon, hdr, hash_lock); |
6752 | hdr->b_l1hdr.b_arc_access = 0; | |
34dc7c2f | 6753 | |
b5256303 | 6754 | mutex_exit(hash_lock); |
428870ff | 6755 | buf_discard_identity(hdr); |
34dc7c2f BB |
6756 | arc_buf_thaw(buf); |
6757 | } | |
34dc7c2f BB |
6758 | } |
6759 | ||
6760 | int | |
6761 | arc_released(arc_buf_t *buf) | |
6762 | { | |
b128c09f BB |
6763 | int released; |
6764 | ||
428870ff | 6765 | mutex_enter(&buf->b_evict_lock); |
b9541d6b CW |
6766 | released = (buf->b_data != NULL && |
6767 | buf->b_hdr->b_l1hdr.b_state == arc_anon); | |
428870ff | 6768 | mutex_exit(&buf->b_evict_lock); |
b128c09f | 6769 | return (released); |
34dc7c2f BB |
6770 | } |
6771 | ||
34dc7c2f BB |
6772 | #ifdef ZFS_DEBUG |
6773 | int | |
6774 | arc_referenced(arc_buf_t *buf) | |
6775 | { | |
b128c09f BB |
6776 | int referenced; |
6777 | ||
428870ff | 6778 | mutex_enter(&buf->b_evict_lock); |
424fd7c3 | 6779 | referenced = (zfs_refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); |
428870ff | 6780 | mutex_exit(&buf->b_evict_lock); |
b128c09f | 6781 | return (referenced); |
34dc7c2f BB |
6782 | } |
6783 | #endif | |
6784 | ||
6785 | static void | |
6786 | arc_write_ready(zio_t *zio) | |
6787 | { | |
6788 | arc_write_callback_t *callback = zio->io_private; | |
6789 | arc_buf_t *buf = callback->awcb_buf; | |
6790 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
b5256303 TC |
6791 | blkptr_t *bp = zio->io_bp; |
6792 | uint64_t psize = BP_IS_HOLE(bp) ? 0 : BP_GET_PSIZE(bp); | |
a6255b7f | 6793 | fstrans_cookie_t cookie = spl_fstrans_mark(); |
34dc7c2f | 6794 | |
b9541d6b | 6795 | ASSERT(HDR_HAS_L1HDR(hdr)); |
424fd7c3 | 6796 | ASSERT(!zfs_refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c | 6797 | ASSERT(hdr->b_l1hdr.b_bufcnt > 0); |
b128c09f | 6798 | |
34dc7c2f | 6799 | /* |
d3c2ae1c GW |
6800 | * If we're reexecuting this zio because the pool suspended, then |
6801 | * cleanup any state that was previously set the first time the | |
2aa34383 | 6802 | * callback was invoked. |
34dc7c2f | 6803 | */ |
d3c2ae1c GW |
6804 | if (zio->io_flags & ZIO_FLAG_REEXECUTED) { |
6805 | arc_cksum_free(hdr); | |
6806 | arc_buf_unwatch(buf); | |
a6255b7f | 6807 | if (hdr->b_l1hdr.b_pabd != NULL) { |
d3c2ae1c | 6808 | if (arc_buf_is_shared(buf)) { |
d3c2ae1c GW |
6809 | arc_unshare_buf(hdr, buf); |
6810 | } else { | |
b5256303 | 6811 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c | 6812 | } |
34dc7c2f | 6813 | } |
b5256303 TC |
6814 | |
6815 | if (HDR_HAS_RABD(hdr)) | |
6816 | arc_hdr_free_abd(hdr, B_TRUE); | |
34dc7c2f | 6817 | } |
a6255b7f | 6818 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 6819 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
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 | ||
d3c2ae1c GW |
6828 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
6829 | ||
b5256303 TC |
6830 | if (BP_IS_PROTECTED(bp) != !!HDR_PROTECTED(hdr)) |
6831 | hdr = arc_hdr_realloc_crypt(hdr, BP_IS_PROTECTED(bp)); | |
6832 | ||
6833 | if (BP_IS_PROTECTED(bp)) { | |
6834 | /* ZIL blocks are written through zio_rewrite */ | |
6835 | ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG); | |
6836 | ASSERT(HDR_PROTECTED(hdr)); | |
6837 | ||
ae76f45c TC |
6838 | if (BP_SHOULD_BYTESWAP(bp)) { |
6839 | if (BP_GET_LEVEL(bp) > 0) { | |
6840 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; | |
6841 | } else { | |
6842 | hdr->b_l1hdr.b_byteswap = | |
6843 | DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); | |
6844 | } | |
6845 | } else { | |
6846 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
6847 | } | |
6848 | ||
b5256303 TC |
6849 | hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp); |
6850 | hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset; | |
6851 | zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt, | |
6852 | hdr->b_crypt_hdr.b_iv); | |
6853 | zio_crypt_decode_mac_bp(bp, hdr->b_crypt_hdr.b_mac); | |
6854 | } | |
6855 | ||
6856 | /* | |
6857 | * If this block was written for raw encryption but the zio layer | |
6858 | * ended up only authenticating it, adjust the buffer flags now. | |
6859 | */ | |
6860 | if (BP_IS_AUTHENTICATED(bp) && ARC_BUF_ENCRYPTED(buf)) { | |
6861 | arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH); | |
6862 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
6863 | if (BP_GET_COMPRESS(bp) == ZIO_COMPRESS_OFF) | |
6864 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
b1d21733 TC |
6865 | } else if (BP_IS_HOLE(bp) && ARC_BUF_ENCRYPTED(buf)) { |
6866 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
6867 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
b5256303 TC |
6868 | } |
6869 | ||
6870 | /* this must be done after the buffer flags are adjusted */ | |
6871 | arc_cksum_compute(buf); | |
6872 | ||
1c27024e | 6873 | enum zio_compress compress; |
b5256303 | 6874 | if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { |
d3c2ae1c GW |
6875 | compress = ZIO_COMPRESS_OFF; |
6876 | } else { | |
b5256303 TC |
6877 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); |
6878 | compress = BP_GET_COMPRESS(bp); | |
d3c2ae1c GW |
6879 | } |
6880 | HDR_SET_PSIZE(hdr, psize); | |
6881 | arc_hdr_set_compress(hdr, compress); | |
10b3c7f5 | 6882 | hdr->b_complevel = zio->io_prop.zp_complevel; |
d3c2ae1c | 6883 | |
4807c0ba TC |
6884 | if (zio->io_error != 0 || psize == 0) |
6885 | goto out; | |
6886 | ||
d3c2ae1c | 6887 | /* |
b5256303 TC |
6888 | * Fill the hdr with data. If the buffer is encrypted we have no choice |
6889 | * but to copy the data into b_radb. If the hdr is compressed, the data | |
6890 | * we want is available from the zio, otherwise we can take it from | |
6891 | * the buf. | |
a6255b7f DQ |
6892 | * |
6893 | * We might be able to share the buf's data with the hdr here. However, | |
6894 | * doing so would cause the ARC to be full of linear ABDs if we write a | |
6895 | * lot of shareable data. As a compromise, we check whether scattered | |
6896 | * ABDs are allowed, and assume that if they are then the user wants | |
6897 | * the ARC to be primarily filled with them regardless of the data being | |
6898 | * written. Therefore, if they're allowed then we allocate one and copy | |
6899 | * the data into it; otherwise, we share the data directly if we can. | |
d3c2ae1c | 6900 | */ |
b5256303 | 6901 | if (ARC_BUF_ENCRYPTED(buf)) { |
4807c0ba | 6902 | ASSERT3U(psize, >, 0); |
b5256303 | 6903 | ASSERT(ARC_BUF_COMPRESSED(buf)); |
6b88b4b5 AM |
6904 | arc_hdr_alloc_abd(hdr, ARC_HDR_DO_ADAPT | ARC_HDR_ALLOC_RDATA | |
6905 | ARC_HDR_USE_RESERVE); | |
b5256303 | 6906 | abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize); |
7eebcd2b AM |
6907 | } else if (!abd_size_alloc_linear(arc_buf_size(buf)) || |
6908 | !arc_can_share(hdr, buf)) { | |
a6255b7f DQ |
6909 | /* |
6910 | * Ideally, we would always copy the io_abd into b_pabd, but the | |
6911 | * user may have disabled compressed ARC, thus we must check the | |
6912 | * hdr's compression setting rather than the io_bp's. | |
6913 | */ | |
b5256303 | 6914 | if (BP_IS_ENCRYPTED(bp)) { |
a6255b7f | 6915 | ASSERT3U(psize, >, 0); |
6b88b4b5 AM |
6916 | arc_hdr_alloc_abd(hdr, ARC_HDR_DO_ADAPT | |
6917 | ARC_HDR_ALLOC_RDATA | ARC_HDR_USE_RESERVE); | |
b5256303 TC |
6918 | abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize); |
6919 | } else if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF && | |
6920 | !ARC_BUF_COMPRESSED(buf)) { | |
6921 | ASSERT3U(psize, >, 0); | |
6b88b4b5 AM |
6922 | arc_hdr_alloc_abd(hdr, ARC_HDR_DO_ADAPT | |
6923 | ARC_HDR_USE_RESERVE); | |
a6255b7f DQ |
6924 | abd_copy(hdr->b_l1hdr.b_pabd, zio->io_abd, psize); |
6925 | } else { | |
6926 | ASSERT3U(zio->io_orig_size, ==, arc_hdr_size(hdr)); | |
6b88b4b5 AM |
6927 | arc_hdr_alloc_abd(hdr, ARC_HDR_DO_ADAPT | |
6928 | ARC_HDR_USE_RESERVE); | |
a6255b7f DQ |
6929 | abd_copy_from_buf(hdr->b_l1hdr.b_pabd, buf->b_data, |
6930 | arc_buf_size(buf)); | |
6931 | } | |
d3c2ae1c | 6932 | } else { |
a6255b7f | 6933 | ASSERT3P(buf->b_data, ==, abd_to_buf(zio->io_orig_abd)); |
2aa34383 | 6934 | ASSERT3U(zio->io_orig_size, ==, arc_buf_size(buf)); |
d3c2ae1c | 6935 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); |
d3c2ae1c | 6936 | |
d3c2ae1c | 6937 | arc_share_buf(hdr, buf); |
d3c2ae1c | 6938 | } |
a6255b7f | 6939 | |
4807c0ba | 6940 | out: |
b5256303 | 6941 | arc_hdr_verify(hdr, bp); |
a6255b7f | 6942 | spl_fstrans_unmark(cookie); |
34dc7c2f BB |
6943 | } |
6944 | ||
bc77ba73 PD |
6945 | static void |
6946 | arc_write_children_ready(zio_t *zio) | |
6947 | { | |
6948 | arc_write_callback_t *callback = zio->io_private; | |
6949 | arc_buf_t *buf = callback->awcb_buf; | |
6950 | ||
6951 | callback->awcb_children_ready(zio, buf, callback->awcb_private); | |
6952 | } | |
6953 | ||
e8b96c60 MA |
6954 | /* |
6955 | * The SPA calls this callback for each physical write that happens on behalf | |
6956 | * of a logical write. See the comment in dbuf_write_physdone() for details. | |
6957 | */ | |
6958 | static void | |
6959 | arc_write_physdone(zio_t *zio) | |
6960 | { | |
6961 | arc_write_callback_t *cb = zio->io_private; | |
6962 | if (cb->awcb_physdone != NULL) | |
6963 | cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); | |
6964 | } | |
6965 | ||
34dc7c2f BB |
6966 | static void |
6967 | arc_write_done(zio_t *zio) | |
6968 | { | |
6969 | arc_write_callback_t *callback = zio->io_private; | |
6970 | arc_buf_t *buf = callback->awcb_buf; | |
6971 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
6972 | ||
d3c2ae1c | 6973 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
428870ff BB |
6974 | |
6975 | if (zio->io_error == 0) { | |
d3c2ae1c GW |
6976 | arc_hdr_verify(hdr, zio->io_bp); |
6977 | ||
9b67f605 | 6978 | if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { |
b0bc7a84 MG |
6979 | buf_discard_identity(hdr); |
6980 | } else { | |
6981 | hdr->b_dva = *BP_IDENTITY(zio->io_bp); | |
6982 | hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); | |
b0bc7a84 | 6983 | } |
428870ff | 6984 | } else { |
d3c2ae1c | 6985 | ASSERT(HDR_EMPTY(hdr)); |
428870ff | 6986 | } |
34dc7c2f | 6987 | |
34dc7c2f | 6988 | /* |
9b67f605 MA |
6989 | * If the block to be written was all-zero or compressed enough to be |
6990 | * embedded in the BP, no write was performed so there will be no | |
6991 | * dva/birth/checksum. The buffer must therefore remain anonymous | |
6992 | * (and uncached). | |
34dc7c2f | 6993 | */ |
d3c2ae1c | 6994 | if (!HDR_EMPTY(hdr)) { |
34dc7c2f BB |
6995 | arc_buf_hdr_t *exists; |
6996 | kmutex_t *hash_lock; | |
6997 | ||
524b4217 | 6998 | ASSERT3U(zio->io_error, ==, 0); |
428870ff | 6999 | |
34dc7c2f BB |
7000 | arc_cksum_verify(buf); |
7001 | ||
7002 | exists = buf_hash_insert(hdr, &hash_lock); | |
b9541d6b | 7003 | if (exists != NULL) { |
34dc7c2f BB |
7004 | /* |
7005 | * This can only happen if we overwrite for | |
7006 | * sync-to-convergence, because we remove | |
7007 | * buffers from the hash table when we arc_free(). | |
7008 | */ | |
428870ff BB |
7009 | if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { |
7010 | if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) | |
7011 | panic("bad overwrite, hdr=%p exists=%p", | |
7012 | (void *)hdr, (void *)exists); | |
424fd7c3 | 7013 | ASSERT(zfs_refcount_is_zero( |
b9541d6b | 7014 | &exists->b_l1hdr.b_refcnt)); |
428870ff | 7015 | arc_change_state(arc_anon, exists, hash_lock); |
428870ff | 7016 | arc_hdr_destroy(exists); |
ca6c7a94 | 7017 | mutex_exit(hash_lock); |
428870ff BB |
7018 | exists = buf_hash_insert(hdr, &hash_lock); |
7019 | ASSERT3P(exists, ==, NULL); | |
03c6040b GW |
7020 | } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { |
7021 | /* nopwrite */ | |
7022 | ASSERT(zio->io_prop.zp_nopwrite); | |
7023 | if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) | |
7024 | panic("bad nopwrite, hdr=%p exists=%p", | |
7025 | (void *)hdr, (void *)exists); | |
428870ff BB |
7026 | } else { |
7027 | /* Dedup */ | |
d3c2ae1c | 7028 | ASSERT(hdr->b_l1hdr.b_bufcnt == 1); |
b9541d6b | 7029 | ASSERT(hdr->b_l1hdr.b_state == arc_anon); |
428870ff BB |
7030 | ASSERT(BP_GET_DEDUP(zio->io_bp)); |
7031 | ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); | |
7032 | } | |
34dc7c2f | 7033 | } |
d3c2ae1c | 7034 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
b128c09f | 7035 | /* if it's not anon, we are doing a scrub */ |
b9541d6b | 7036 | if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) |
b128c09f | 7037 | arc_access(hdr, hash_lock); |
34dc7c2f | 7038 | mutex_exit(hash_lock); |
34dc7c2f | 7039 | } else { |
d3c2ae1c | 7040 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
34dc7c2f BB |
7041 | } |
7042 | ||
424fd7c3 | 7043 | ASSERT(!zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
428870ff | 7044 | callback->awcb_done(zio, buf, callback->awcb_private); |
34dc7c2f | 7045 | |
e2af2acc | 7046 | abd_free(zio->io_abd); |
34dc7c2f BB |
7047 | kmem_free(callback, sizeof (arc_write_callback_t)); |
7048 | } | |
7049 | ||
7050 | zio_t * | |
428870ff | 7051 | arc_write(zio_t *pio, spa_t *spa, uint64_t txg, |
d3c2ae1c | 7052 | blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, |
b5256303 TC |
7053 | const zio_prop_t *zp, arc_write_done_func_t *ready, |
7054 | arc_write_done_func_t *children_ready, arc_write_done_func_t *physdone, | |
7055 | arc_write_done_func_t *done, void *private, zio_priority_t priority, | |
5dbd68a3 | 7056 | int zio_flags, const zbookmark_phys_t *zb) |
34dc7c2f BB |
7057 | { |
7058 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
7059 | arc_write_callback_t *callback; | |
b128c09f | 7060 | zio_t *zio; |
82644107 | 7061 | zio_prop_t localprop = *zp; |
34dc7c2f | 7062 | |
d3c2ae1c GW |
7063 | ASSERT3P(ready, !=, NULL); |
7064 | ASSERT3P(done, !=, NULL); | |
34dc7c2f | 7065 | ASSERT(!HDR_IO_ERROR(hdr)); |
b9541d6b | 7066 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
d3c2ae1c GW |
7067 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
7068 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); | |
b128c09f | 7069 | if (l2arc) |
d3c2ae1c | 7070 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); |
82644107 | 7071 | |
b5256303 TC |
7072 | if (ARC_BUF_ENCRYPTED(buf)) { |
7073 | ASSERT(ARC_BUF_COMPRESSED(buf)); | |
7074 | localprop.zp_encrypt = B_TRUE; | |
7075 | localprop.zp_compress = HDR_GET_COMPRESS(hdr); | |
10b3c7f5 | 7076 | localprop.zp_complevel = hdr->b_complevel; |
b5256303 TC |
7077 | localprop.zp_byteorder = |
7078 | (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ? | |
7079 | ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER; | |
7080 | bcopy(hdr->b_crypt_hdr.b_salt, localprop.zp_salt, | |
7081 | ZIO_DATA_SALT_LEN); | |
7082 | bcopy(hdr->b_crypt_hdr.b_iv, localprop.zp_iv, | |
7083 | ZIO_DATA_IV_LEN); | |
7084 | bcopy(hdr->b_crypt_hdr.b_mac, localprop.zp_mac, | |
7085 | ZIO_DATA_MAC_LEN); | |
7086 | if (DMU_OT_IS_ENCRYPTED(localprop.zp_type)) { | |
7087 | localprop.zp_nopwrite = B_FALSE; | |
7088 | localprop.zp_copies = | |
7089 | MIN(localprop.zp_copies, SPA_DVAS_PER_BP - 1); | |
7090 | } | |
2aa34383 | 7091 | zio_flags |= ZIO_FLAG_RAW; |
b5256303 TC |
7092 | } else if (ARC_BUF_COMPRESSED(buf)) { |
7093 | ASSERT3U(HDR_GET_LSIZE(hdr), !=, arc_buf_size(buf)); | |
7094 | localprop.zp_compress = HDR_GET_COMPRESS(hdr); | |
10b3c7f5 | 7095 | localprop.zp_complevel = hdr->b_complevel; |
b5256303 | 7096 | zio_flags |= ZIO_FLAG_RAW_COMPRESS; |
2aa34383 | 7097 | } |
79c76d5b | 7098 | callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); |
34dc7c2f | 7099 | callback->awcb_ready = ready; |
bc77ba73 | 7100 | callback->awcb_children_ready = children_ready; |
e8b96c60 | 7101 | callback->awcb_physdone = physdone; |
34dc7c2f BB |
7102 | callback->awcb_done = done; |
7103 | callback->awcb_private = private; | |
7104 | callback->awcb_buf = buf; | |
b128c09f | 7105 | |
d3c2ae1c | 7106 | /* |
a6255b7f | 7107 | * The hdr's b_pabd is now stale, free it now. A new data block |
d3c2ae1c GW |
7108 | * will be allocated when the zio pipeline calls arc_write_ready(). |
7109 | */ | |
a6255b7f | 7110 | if (hdr->b_l1hdr.b_pabd != NULL) { |
d3c2ae1c GW |
7111 | /* |
7112 | * If the buf is currently sharing the data block with | |
7113 | * the hdr then we need to break that relationship here. | |
7114 | * The hdr will remain with a NULL data pointer and the | |
7115 | * buf will take sole ownership of the block. | |
7116 | */ | |
7117 | if (arc_buf_is_shared(buf)) { | |
d3c2ae1c GW |
7118 | arc_unshare_buf(hdr, buf); |
7119 | } else { | |
b5256303 | 7120 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c GW |
7121 | } |
7122 | VERIFY3P(buf->b_data, !=, NULL); | |
d3c2ae1c | 7123 | } |
b5256303 TC |
7124 | |
7125 | if (HDR_HAS_RABD(hdr)) | |
7126 | arc_hdr_free_abd(hdr, B_TRUE); | |
7127 | ||
71a24c3c TC |
7128 | if (!(zio_flags & ZIO_FLAG_RAW)) |
7129 | arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF); | |
b5256303 | 7130 | |
d3c2ae1c | 7131 | ASSERT(!arc_buf_is_shared(buf)); |
a6255b7f | 7132 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
d3c2ae1c | 7133 | |
a6255b7f DQ |
7134 | zio = zio_write(pio, spa, txg, bp, |
7135 | abd_get_from_buf(buf->b_data, HDR_GET_LSIZE(hdr)), | |
82644107 | 7136 | HDR_GET_LSIZE(hdr), arc_buf_size(buf), &localprop, arc_write_ready, |
bc77ba73 PD |
7137 | (children_ready != NULL) ? arc_write_children_ready : NULL, |
7138 | arc_write_physdone, arc_write_done, callback, | |
e8b96c60 | 7139 | priority, zio_flags, zb); |
34dc7c2f BB |
7140 | |
7141 | return (zio); | |
7142 | } | |
7143 | ||
34dc7c2f BB |
7144 | void |
7145 | arc_tempreserve_clear(uint64_t reserve) | |
7146 | { | |
7147 | atomic_add_64(&arc_tempreserve, -reserve); | |
7148 | ASSERT((int64_t)arc_tempreserve >= 0); | |
7149 | } | |
7150 | ||
7151 | int | |
dae3e9ea | 7152 | arc_tempreserve_space(spa_t *spa, uint64_t reserve, uint64_t txg) |
34dc7c2f BB |
7153 | { |
7154 | int error; | |
9babb374 | 7155 | uint64_t anon_size; |
34dc7c2f | 7156 | |
1b8951b3 TC |
7157 | if (!arc_no_grow && |
7158 | reserve > arc_c/4 && | |
7159 | reserve * 4 > (2ULL << SPA_MAXBLOCKSHIFT)) | |
34dc7c2f | 7160 | arc_c = MIN(arc_c_max, reserve * 4); |
12f9a6a3 BB |
7161 | |
7162 | /* | |
7163 | * Throttle when the calculated memory footprint for the TXG | |
7164 | * exceeds the target ARC size. | |
7165 | */ | |
570827e1 BB |
7166 | if (reserve > arc_c) { |
7167 | DMU_TX_STAT_BUMP(dmu_tx_memory_reserve); | |
12f9a6a3 | 7168 | return (SET_ERROR(ERESTART)); |
570827e1 | 7169 | } |
34dc7c2f | 7170 | |
9babb374 BB |
7171 | /* |
7172 | * Don't count loaned bufs as in flight dirty data to prevent long | |
7173 | * network delays from blocking transactions that are ready to be | |
7174 | * assigned to a txg. | |
7175 | */ | |
a7004725 DK |
7176 | |
7177 | /* assert that it has not wrapped around */ | |
7178 | ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); | |
7179 | ||
424fd7c3 | 7180 | anon_size = MAX((int64_t)(zfs_refcount_count(&arc_anon->arcs_size) - |
36da08ef | 7181 | arc_loaned_bytes), 0); |
9babb374 | 7182 | |
34dc7c2f BB |
7183 | /* |
7184 | * Writes will, almost always, require additional memory allocations | |
d3cc8b15 | 7185 | * in order to compress/encrypt/etc the data. We therefore need to |
34dc7c2f BB |
7186 | * make sure that there is sufficient available memory for this. |
7187 | */ | |
dae3e9ea | 7188 | error = arc_memory_throttle(spa, reserve, txg); |
e8b96c60 | 7189 | if (error != 0) |
34dc7c2f BB |
7190 | return (error); |
7191 | ||
7192 | /* | |
7193 | * Throttle writes when the amount of dirty data in the cache | |
7194 | * gets too large. We try to keep the cache less than half full | |
7195 | * of dirty blocks so that our sync times don't grow too large. | |
dae3e9ea DB |
7196 | * |
7197 | * In the case of one pool being built on another pool, we want | |
7198 | * to make sure we don't end up throttling the lower (backing) | |
7199 | * pool when the upper pool is the majority contributor to dirty | |
7200 | * data. To insure we make forward progress during throttling, we | |
7201 | * also check the current pool's net dirty data and only throttle | |
7202 | * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty | |
7203 | * data in the cache. | |
7204 | * | |
34dc7c2f BB |
7205 | * Note: if two requests come in concurrently, we might let them |
7206 | * both succeed, when one of them should fail. Not a huge deal. | |
7207 | */ | |
dae3e9ea DB |
7208 | uint64_t total_dirty = reserve + arc_tempreserve + anon_size; |
7209 | uint64_t spa_dirty_anon = spa_dirty_data(spa); | |
daabddaa AM |
7210 | uint64_t rarc_c = arc_warm ? arc_c : arc_c_max; |
7211 | if (total_dirty > rarc_c * zfs_arc_dirty_limit_percent / 100 && | |
7212 | anon_size > rarc_c * zfs_arc_anon_limit_percent / 100 && | |
dae3e9ea | 7213 | spa_dirty_anon > anon_size * zfs_arc_pool_dirty_percent / 100) { |
2fd92c3d | 7214 | #ifdef ZFS_DEBUG |
424fd7c3 TS |
7215 | uint64_t meta_esize = zfs_refcount_count( |
7216 | &arc_anon->arcs_esize[ARC_BUFC_METADATA]); | |
d3c2ae1c | 7217 | uint64_t data_esize = |
424fd7c3 | 7218 | zfs_refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]); |
34dc7c2f | 7219 | dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " |
daabddaa | 7220 | "anon_data=%lluK tempreserve=%lluK rarc_c=%lluK\n", |
8e739b2c RE |
7221 | (u_longlong_t)arc_tempreserve >> 10, |
7222 | (u_longlong_t)meta_esize >> 10, | |
7223 | (u_longlong_t)data_esize >> 10, | |
7224 | (u_longlong_t)reserve >> 10, | |
7225 | (u_longlong_t)rarc_c >> 10); | |
2fd92c3d | 7226 | #endif |
570827e1 | 7227 | DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle); |
2e528b49 | 7228 | return (SET_ERROR(ERESTART)); |
34dc7c2f BB |
7229 | } |
7230 | atomic_add_64(&arc_tempreserve, reserve); | |
7231 | return (0); | |
7232 | } | |
7233 | ||
13be560d BB |
7234 | static void |
7235 | arc_kstat_update_state(arc_state_t *state, kstat_named_t *size, | |
7236 | kstat_named_t *evict_data, kstat_named_t *evict_metadata) | |
7237 | { | |
424fd7c3 | 7238 | size->value.ui64 = zfs_refcount_count(&state->arcs_size); |
d3c2ae1c | 7239 | evict_data->value.ui64 = |
424fd7c3 | 7240 | zfs_refcount_count(&state->arcs_esize[ARC_BUFC_DATA]); |
d3c2ae1c | 7241 | evict_metadata->value.ui64 = |
424fd7c3 | 7242 | zfs_refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]); |
13be560d BB |
7243 | } |
7244 | ||
7245 | static int | |
7246 | arc_kstat_update(kstat_t *ksp, int rw) | |
7247 | { | |
7248 | arc_stats_t *as = ksp->ks_data; | |
7249 | ||
c4c162c1 | 7250 | if (rw == KSTAT_WRITE) |
ecb2b7dc | 7251 | return (SET_ERROR(EACCES)); |
c4c162c1 AM |
7252 | |
7253 | as->arcstat_hits.value.ui64 = | |
7254 | wmsum_value(&arc_sums.arcstat_hits); | |
7255 | as->arcstat_misses.value.ui64 = | |
7256 | wmsum_value(&arc_sums.arcstat_misses); | |
7257 | as->arcstat_demand_data_hits.value.ui64 = | |
7258 | wmsum_value(&arc_sums.arcstat_demand_data_hits); | |
7259 | as->arcstat_demand_data_misses.value.ui64 = | |
7260 | wmsum_value(&arc_sums.arcstat_demand_data_misses); | |
7261 | as->arcstat_demand_metadata_hits.value.ui64 = | |
7262 | wmsum_value(&arc_sums.arcstat_demand_metadata_hits); | |
7263 | as->arcstat_demand_metadata_misses.value.ui64 = | |
7264 | wmsum_value(&arc_sums.arcstat_demand_metadata_misses); | |
7265 | as->arcstat_prefetch_data_hits.value.ui64 = | |
7266 | wmsum_value(&arc_sums.arcstat_prefetch_data_hits); | |
7267 | as->arcstat_prefetch_data_misses.value.ui64 = | |
7268 | wmsum_value(&arc_sums.arcstat_prefetch_data_misses); | |
7269 | as->arcstat_prefetch_metadata_hits.value.ui64 = | |
7270 | wmsum_value(&arc_sums.arcstat_prefetch_metadata_hits); | |
7271 | as->arcstat_prefetch_metadata_misses.value.ui64 = | |
7272 | wmsum_value(&arc_sums.arcstat_prefetch_metadata_misses); | |
7273 | as->arcstat_mru_hits.value.ui64 = | |
7274 | wmsum_value(&arc_sums.arcstat_mru_hits); | |
7275 | as->arcstat_mru_ghost_hits.value.ui64 = | |
7276 | wmsum_value(&arc_sums.arcstat_mru_ghost_hits); | |
7277 | as->arcstat_mfu_hits.value.ui64 = | |
7278 | wmsum_value(&arc_sums.arcstat_mfu_hits); | |
7279 | as->arcstat_mfu_ghost_hits.value.ui64 = | |
7280 | wmsum_value(&arc_sums.arcstat_mfu_ghost_hits); | |
7281 | as->arcstat_deleted.value.ui64 = | |
7282 | wmsum_value(&arc_sums.arcstat_deleted); | |
7283 | as->arcstat_mutex_miss.value.ui64 = | |
7284 | wmsum_value(&arc_sums.arcstat_mutex_miss); | |
7285 | as->arcstat_access_skip.value.ui64 = | |
7286 | wmsum_value(&arc_sums.arcstat_access_skip); | |
7287 | as->arcstat_evict_skip.value.ui64 = | |
7288 | wmsum_value(&arc_sums.arcstat_evict_skip); | |
7289 | as->arcstat_evict_not_enough.value.ui64 = | |
7290 | wmsum_value(&arc_sums.arcstat_evict_not_enough); | |
7291 | as->arcstat_evict_l2_cached.value.ui64 = | |
7292 | wmsum_value(&arc_sums.arcstat_evict_l2_cached); | |
7293 | as->arcstat_evict_l2_eligible.value.ui64 = | |
7294 | wmsum_value(&arc_sums.arcstat_evict_l2_eligible); | |
7295 | as->arcstat_evict_l2_eligible_mfu.value.ui64 = | |
7296 | wmsum_value(&arc_sums.arcstat_evict_l2_eligible_mfu); | |
7297 | as->arcstat_evict_l2_eligible_mru.value.ui64 = | |
7298 | wmsum_value(&arc_sums.arcstat_evict_l2_eligible_mru); | |
7299 | as->arcstat_evict_l2_ineligible.value.ui64 = | |
7300 | wmsum_value(&arc_sums.arcstat_evict_l2_ineligible); | |
7301 | as->arcstat_evict_l2_skip.value.ui64 = | |
7302 | wmsum_value(&arc_sums.arcstat_evict_l2_skip); | |
7303 | as->arcstat_hash_collisions.value.ui64 = | |
7304 | wmsum_value(&arc_sums.arcstat_hash_collisions); | |
7305 | as->arcstat_hash_chains.value.ui64 = | |
7306 | wmsum_value(&arc_sums.arcstat_hash_chains); | |
7307 | as->arcstat_size.value.ui64 = | |
7308 | aggsum_value(&arc_sums.arcstat_size); | |
7309 | as->arcstat_compressed_size.value.ui64 = | |
7310 | wmsum_value(&arc_sums.arcstat_compressed_size); | |
7311 | as->arcstat_uncompressed_size.value.ui64 = | |
7312 | wmsum_value(&arc_sums.arcstat_uncompressed_size); | |
7313 | as->arcstat_overhead_size.value.ui64 = | |
7314 | wmsum_value(&arc_sums.arcstat_overhead_size); | |
7315 | as->arcstat_hdr_size.value.ui64 = | |
7316 | wmsum_value(&arc_sums.arcstat_hdr_size); | |
7317 | as->arcstat_data_size.value.ui64 = | |
7318 | wmsum_value(&arc_sums.arcstat_data_size); | |
7319 | as->arcstat_metadata_size.value.ui64 = | |
7320 | wmsum_value(&arc_sums.arcstat_metadata_size); | |
7321 | as->arcstat_dbuf_size.value.ui64 = | |
7322 | wmsum_value(&arc_sums.arcstat_dbuf_size); | |
1c2725a1 | 7323 | #if defined(COMPAT_FREEBSD11) |
c4c162c1 AM |
7324 | as->arcstat_other_size.value.ui64 = |
7325 | wmsum_value(&arc_sums.arcstat_bonus_size) + | |
7326 | aggsum_value(&arc_sums.arcstat_dnode_size) + | |
7327 | wmsum_value(&arc_sums.arcstat_dbuf_size); | |
1c2725a1 | 7328 | #endif |
37fb3e43 | 7329 | |
c4c162c1 AM |
7330 | arc_kstat_update_state(arc_anon, |
7331 | &as->arcstat_anon_size, | |
7332 | &as->arcstat_anon_evictable_data, | |
7333 | &as->arcstat_anon_evictable_metadata); | |
7334 | arc_kstat_update_state(arc_mru, | |
7335 | &as->arcstat_mru_size, | |
7336 | &as->arcstat_mru_evictable_data, | |
7337 | &as->arcstat_mru_evictable_metadata); | |
7338 | arc_kstat_update_state(arc_mru_ghost, | |
7339 | &as->arcstat_mru_ghost_size, | |
7340 | &as->arcstat_mru_ghost_evictable_data, | |
7341 | &as->arcstat_mru_ghost_evictable_metadata); | |
7342 | arc_kstat_update_state(arc_mfu, | |
7343 | &as->arcstat_mfu_size, | |
7344 | &as->arcstat_mfu_evictable_data, | |
7345 | &as->arcstat_mfu_evictable_metadata); | |
7346 | arc_kstat_update_state(arc_mfu_ghost, | |
7347 | &as->arcstat_mfu_ghost_size, | |
7348 | &as->arcstat_mfu_ghost_evictable_data, | |
7349 | &as->arcstat_mfu_ghost_evictable_metadata); | |
7350 | ||
7351 | as->arcstat_dnode_size.value.ui64 = | |
7352 | aggsum_value(&arc_sums.arcstat_dnode_size); | |
7353 | as->arcstat_bonus_size.value.ui64 = | |
7354 | wmsum_value(&arc_sums.arcstat_bonus_size); | |
7355 | as->arcstat_l2_hits.value.ui64 = | |
7356 | wmsum_value(&arc_sums.arcstat_l2_hits); | |
7357 | as->arcstat_l2_misses.value.ui64 = | |
7358 | wmsum_value(&arc_sums.arcstat_l2_misses); | |
7359 | as->arcstat_l2_prefetch_asize.value.ui64 = | |
7360 | wmsum_value(&arc_sums.arcstat_l2_prefetch_asize); | |
7361 | as->arcstat_l2_mru_asize.value.ui64 = | |
7362 | wmsum_value(&arc_sums.arcstat_l2_mru_asize); | |
7363 | as->arcstat_l2_mfu_asize.value.ui64 = | |
7364 | wmsum_value(&arc_sums.arcstat_l2_mfu_asize); | |
7365 | as->arcstat_l2_bufc_data_asize.value.ui64 = | |
7366 | wmsum_value(&arc_sums.arcstat_l2_bufc_data_asize); | |
7367 | as->arcstat_l2_bufc_metadata_asize.value.ui64 = | |
7368 | wmsum_value(&arc_sums.arcstat_l2_bufc_metadata_asize); | |
7369 | as->arcstat_l2_feeds.value.ui64 = | |
7370 | wmsum_value(&arc_sums.arcstat_l2_feeds); | |
7371 | as->arcstat_l2_rw_clash.value.ui64 = | |
7372 | wmsum_value(&arc_sums.arcstat_l2_rw_clash); | |
7373 | as->arcstat_l2_read_bytes.value.ui64 = | |
7374 | wmsum_value(&arc_sums.arcstat_l2_read_bytes); | |
7375 | as->arcstat_l2_write_bytes.value.ui64 = | |
7376 | wmsum_value(&arc_sums.arcstat_l2_write_bytes); | |
7377 | as->arcstat_l2_writes_sent.value.ui64 = | |
7378 | wmsum_value(&arc_sums.arcstat_l2_writes_sent); | |
7379 | as->arcstat_l2_writes_done.value.ui64 = | |
7380 | wmsum_value(&arc_sums.arcstat_l2_writes_done); | |
7381 | as->arcstat_l2_writes_error.value.ui64 = | |
7382 | wmsum_value(&arc_sums.arcstat_l2_writes_error); | |
7383 | as->arcstat_l2_writes_lock_retry.value.ui64 = | |
7384 | wmsum_value(&arc_sums.arcstat_l2_writes_lock_retry); | |
7385 | as->arcstat_l2_evict_lock_retry.value.ui64 = | |
7386 | wmsum_value(&arc_sums.arcstat_l2_evict_lock_retry); | |
7387 | as->arcstat_l2_evict_reading.value.ui64 = | |
7388 | wmsum_value(&arc_sums.arcstat_l2_evict_reading); | |
7389 | as->arcstat_l2_evict_l1cached.value.ui64 = | |
7390 | wmsum_value(&arc_sums.arcstat_l2_evict_l1cached); | |
7391 | as->arcstat_l2_free_on_write.value.ui64 = | |
7392 | wmsum_value(&arc_sums.arcstat_l2_free_on_write); | |
7393 | as->arcstat_l2_abort_lowmem.value.ui64 = | |
7394 | wmsum_value(&arc_sums.arcstat_l2_abort_lowmem); | |
7395 | as->arcstat_l2_cksum_bad.value.ui64 = | |
7396 | wmsum_value(&arc_sums.arcstat_l2_cksum_bad); | |
7397 | as->arcstat_l2_io_error.value.ui64 = | |
7398 | wmsum_value(&arc_sums.arcstat_l2_io_error); | |
7399 | as->arcstat_l2_lsize.value.ui64 = | |
7400 | wmsum_value(&arc_sums.arcstat_l2_lsize); | |
7401 | as->arcstat_l2_psize.value.ui64 = | |
7402 | wmsum_value(&arc_sums.arcstat_l2_psize); | |
7403 | as->arcstat_l2_hdr_size.value.ui64 = | |
7404 | aggsum_value(&arc_sums.arcstat_l2_hdr_size); | |
7405 | as->arcstat_l2_log_blk_writes.value.ui64 = | |
7406 | wmsum_value(&arc_sums.arcstat_l2_log_blk_writes); | |
7407 | as->arcstat_l2_log_blk_asize.value.ui64 = | |
7408 | wmsum_value(&arc_sums.arcstat_l2_log_blk_asize); | |
7409 | as->arcstat_l2_log_blk_count.value.ui64 = | |
7410 | wmsum_value(&arc_sums.arcstat_l2_log_blk_count); | |
7411 | as->arcstat_l2_rebuild_success.value.ui64 = | |
7412 | wmsum_value(&arc_sums.arcstat_l2_rebuild_success); | |
7413 | as->arcstat_l2_rebuild_abort_unsupported.value.ui64 = | |
7414 | wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_unsupported); | |
7415 | as->arcstat_l2_rebuild_abort_io_errors.value.ui64 = | |
7416 | wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_io_errors); | |
7417 | as->arcstat_l2_rebuild_abort_dh_errors.value.ui64 = | |
7418 | wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_dh_errors); | |
7419 | as->arcstat_l2_rebuild_abort_cksum_lb_errors.value.ui64 = | |
7420 | wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors); | |
7421 | as->arcstat_l2_rebuild_abort_lowmem.value.ui64 = | |
7422 | wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_lowmem); | |
7423 | as->arcstat_l2_rebuild_size.value.ui64 = | |
7424 | wmsum_value(&arc_sums.arcstat_l2_rebuild_size); | |
7425 | as->arcstat_l2_rebuild_asize.value.ui64 = | |
7426 | wmsum_value(&arc_sums.arcstat_l2_rebuild_asize); | |
7427 | as->arcstat_l2_rebuild_bufs.value.ui64 = | |
7428 | wmsum_value(&arc_sums.arcstat_l2_rebuild_bufs); | |
7429 | as->arcstat_l2_rebuild_bufs_precached.value.ui64 = | |
7430 | wmsum_value(&arc_sums.arcstat_l2_rebuild_bufs_precached); | |
7431 | as->arcstat_l2_rebuild_log_blks.value.ui64 = | |
7432 | wmsum_value(&arc_sums.arcstat_l2_rebuild_log_blks); | |
7433 | as->arcstat_memory_throttle_count.value.ui64 = | |
7434 | wmsum_value(&arc_sums.arcstat_memory_throttle_count); | |
7435 | as->arcstat_memory_direct_count.value.ui64 = | |
7436 | wmsum_value(&arc_sums.arcstat_memory_direct_count); | |
7437 | as->arcstat_memory_indirect_count.value.ui64 = | |
7438 | wmsum_value(&arc_sums.arcstat_memory_indirect_count); | |
7439 | ||
7440 | as->arcstat_memory_all_bytes.value.ui64 = | |
7441 | arc_all_memory(); | |
7442 | as->arcstat_memory_free_bytes.value.ui64 = | |
7443 | arc_free_memory(); | |
7444 | as->arcstat_memory_available_bytes.value.i64 = | |
7445 | arc_available_memory(); | |
7446 | ||
7447 | as->arcstat_prune.value.ui64 = | |
7448 | wmsum_value(&arc_sums.arcstat_prune); | |
7449 | as->arcstat_meta_used.value.ui64 = | |
7450 | aggsum_value(&arc_sums.arcstat_meta_used); | |
7451 | as->arcstat_async_upgrade_sync.value.ui64 = | |
7452 | wmsum_value(&arc_sums.arcstat_async_upgrade_sync); | |
7453 | as->arcstat_demand_hit_predictive_prefetch.value.ui64 = | |
7454 | wmsum_value(&arc_sums.arcstat_demand_hit_predictive_prefetch); | |
7455 | as->arcstat_demand_hit_prescient_prefetch.value.ui64 = | |
7456 | wmsum_value(&arc_sums.arcstat_demand_hit_prescient_prefetch); | |
7457 | as->arcstat_raw_size.value.ui64 = | |
7458 | wmsum_value(&arc_sums.arcstat_raw_size); | |
7459 | as->arcstat_cached_only_in_progress.value.ui64 = | |
7460 | wmsum_value(&arc_sums.arcstat_cached_only_in_progress); | |
7461 | as->arcstat_abd_chunk_waste_size.value.ui64 = | |
7462 | wmsum_value(&arc_sums.arcstat_abd_chunk_waste_size); | |
13be560d BB |
7463 | |
7464 | return (0); | |
7465 | } | |
7466 | ||
ca0bf58d PS |
7467 | /* |
7468 | * This function *must* return indices evenly distributed between all | |
7469 | * sublists of the multilist. This is needed due to how the ARC eviction | |
7470 | * code is laid out; arc_evict_state() assumes ARC buffers are evenly | |
7471 | * distributed between all sublists and uses this assumption when | |
7472 | * deciding which sublist to evict from and how much to evict from it. | |
7473 | */ | |
65c7cc49 | 7474 | static unsigned int |
ca0bf58d PS |
7475 | arc_state_multilist_index_func(multilist_t *ml, void *obj) |
7476 | { | |
7477 | arc_buf_hdr_t *hdr = obj; | |
7478 | ||
7479 | /* | |
7480 | * We rely on b_dva to generate evenly distributed index | |
7481 | * numbers using buf_hash below. So, as an added precaution, | |
7482 | * let's make sure we never add empty buffers to the arc lists. | |
7483 | */ | |
d3c2ae1c | 7484 | ASSERT(!HDR_EMPTY(hdr)); |
ca0bf58d PS |
7485 | |
7486 | /* | |
7487 | * The assumption here, is the hash value for a given | |
7488 | * arc_buf_hdr_t will remain constant throughout its lifetime | |
7489 | * (i.e. its b_spa, b_dva, and b_birth fields don't change). | |
7490 | * Thus, we don't need to store the header's sublist index | |
7491 | * on insertion, as this index can be recalculated on removal. | |
7492 | * | |
7493 | * Also, the low order bits of the hash value are thought to be | |
7494 | * distributed evenly. Otherwise, in the case that the multilist | |
7495 | * has a power of two number of sublists, each sublists' usage | |
5b7053a9 AM |
7496 | * would not be evenly distributed. In this context full 64bit |
7497 | * division would be a waste of time, so limit it to 32 bits. | |
ca0bf58d | 7498 | */ |
5b7053a9 | 7499 | return ((unsigned int)buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % |
ca0bf58d PS |
7500 | multilist_get_num_sublists(ml)); |
7501 | } | |
7502 | ||
bb7ad5d3 AM |
7503 | static unsigned int |
7504 | arc_state_l2c_multilist_index_func(multilist_t *ml, void *obj) | |
7505 | { | |
7506 | panic("Header %p insert into arc_l2c_only %p", obj, ml); | |
7507 | } | |
7508 | ||
36a6e233 RM |
7509 | #define WARN_IF_TUNING_IGNORED(tuning, value, do_warn) do { \ |
7510 | if ((do_warn) && (tuning) && ((tuning) != (value))) { \ | |
7511 | cmn_err(CE_WARN, \ | |
7512 | "ignoring tunable %s (using %llu instead)", \ | |
5dbf6c5a | 7513 | (#tuning), (u_longlong_t)(value)); \ |
36a6e233 RM |
7514 | } \ |
7515 | } while (0) | |
7516 | ||
ca67b33a MA |
7517 | /* |
7518 | * Called during module initialization and periodically thereafter to | |
e3570464 | 7519 | * apply reasonable changes to the exposed performance tunings. Can also be |
7520 | * called explicitly by param_set_arc_*() functions when ARC tunables are | |
7521 | * updated manually. Non-zero zfs_* values which differ from the currently set | |
7522 | * values will be applied. | |
ca67b33a | 7523 | */ |
e3570464 | 7524 | void |
36a6e233 | 7525 | arc_tuning_update(boolean_t verbose) |
ca67b33a | 7526 | { |
b8a97fb1 | 7527 | uint64_t allmem = arc_all_memory(); |
7528 | unsigned long limit; | |
9edb3695 | 7529 | |
36a6e233 RM |
7530 | /* Valid range: 32M - <arc_c_max> */ |
7531 | if ((zfs_arc_min) && (zfs_arc_min != arc_c_min) && | |
7532 | (zfs_arc_min >= 2ULL << SPA_MAXBLOCKSHIFT) && | |
7533 | (zfs_arc_min <= arc_c_max)) { | |
7534 | arc_c_min = zfs_arc_min; | |
7535 | arc_c = MAX(arc_c, arc_c_min); | |
7536 | } | |
7537 | WARN_IF_TUNING_IGNORED(zfs_arc_min, arc_c_min, verbose); | |
7538 | ||
ca67b33a MA |
7539 | /* Valid range: 64M - <all physical memory> */ |
7540 | if ((zfs_arc_max) && (zfs_arc_max != arc_c_max) && | |
e945e8d7 | 7541 | (zfs_arc_max >= MIN_ARC_MAX) && (zfs_arc_max < allmem) && |
ca67b33a MA |
7542 | (zfs_arc_max > arc_c_min)) { |
7543 | arc_c_max = zfs_arc_max; | |
17ca3018 | 7544 | arc_c = MIN(arc_c, arc_c_max); |
ca67b33a | 7545 | arc_p = (arc_c >> 1); |
b8a97fb1 | 7546 | if (arc_meta_limit > arc_c_max) |
7547 | arc_meta_limit = arc_c_max; | |
03fdcb9a MM |
7548 | if (arc_dnode_size_limit > arc_meta_limit) |
7549 | arc_dnode_size_limit = arc_meta_limit; | |
ca67b33a | 7550 | } |
36a6e233 | 7551 | WARN_IF_TUNING_IGNORED(zfs_arc_max, arc_c_max, verbose); |
ca67b33a MA |
7552 | |
7553 | /* Valid range: 16M - <arc_c_max> */ | |
7554 | if ((zfs_arc_meta_min) && (zfs_arc_meta_min != arc_meta_min) && | |
7555 | (zfs_arc_meta_min >= 1ULL << SPA_MAXBLOCKSHIFT) && | |
7556 | (zfs_arc_meta_min <= arc_c_max)) { | |
7557 | arc_meta_min = zfs_arc_meta_min; | |
b8a97fb1 | 7558 | if (arc_meta_limit < arc_meta_min) |
7559 | arc_meta_limit = arc_meta_min; | |
03fdcb9a MM |
7560 | if (arc_dnode_size_limit < arc_meta_min) |
7561 | arc_dnode_size_limit = arc_meta_min; | |
ca67b33a | 7562 | } |
36a6e233 | 7563 | WARN_IF_TUNING_IGNORED(zfs_arc_meta_min, arc_meta_min, verbose); |
ca67b33a MA |
7564 | |
7565 | /* Valid range: <arc_meta_min> - <arc_c_max> */ | |
b8a97fb1 | 7566 | limit = zfs_arc_meta_limit ? zfs_arc_meta_limit : |
7567 | MIN(zfs_arc_meta_limit_percent, 100) * arc_c_max / 100; | |
7568 | if ((limit != arc_meta_limit) && | |
7569 | (limit >= arc_meta_min) && | |
7570 | (limit <= arc_c_max)) | |
7571 | arc_meta_limit = limit; | |
36a6e233 | 7572 | WARN_IF_TUNING_IGNORED(zfs_arc_meta_limit, arc_meta_limit, verbose); |
b8a97fb1 | 7573 | |
7574 | /* Valid range: <arc_meta_min> - <arc_meta_limit> */ | |
7575 | limit = zfs_arc_dnode_limit ? zfs_arc_dnode_limit : | |
7576 | MIN(zfs_arc_dnode_limit_percent, 100) * arc_meta_limit / 100; | |
03fdcb9a | 7577 | if ((limit != arc_dnode_size_limit) && |
b8a97fb1 | 7578 | (limit >= arc_meta_min) && |
7579 | (limit <= arc_meta_limit)) | |
03fdcb9a | 7580 | arc_dnode_size_limit = limit; |
36a6e233 RM |
7581 | WARN_IF_TUNING_IGNORED(zfs_arc_dnode_limit, arc_dnode_size_limit, |
7582 | verbose); | |
25458cbe | 7583 | |
ca67b33a MA |
7584 | /* Valid range: 1 - N */ |
7585 | if (zfs_arc_grow_retry) | |
7586 | arc_grow_retry = zfs_arc_grow_retry; | |
7587 | ||
7588 | /* Valid range: 1 - N */ | |
7589 | if (zfs_arc_shrink_shift) { | |
7590 | arc_shrink_shift = zfs_arc_shrink_shift; | |
7591 | arc_no_grow_shift = MIN(arc_no_grow_shift, arc_shrink_shift -1); | |
7592 | } | |
7593 | ||
728d6ae9 BB |
7594 | /* Valid range: 1 - N */ |
7595 | if (zfs_arc_p_min_shift) | |
7596 | arc_p_min_shift = zfs_arc_p_min_shift; | |
7597 | ||
d4a72f23 TC |
7598 | /* Valid range: 1 - N ms */ |
7599 | if (zfs_arc_min_prefetch_ms) | |
7600 | arc_min_prefetch_ms = zfs_arc_min_prefetch_ms; | |
7601 | ||
7602 | /* Valid range: 1 - N ms */ | |
7603 | if (zfs_arc_min_prescient_prefetch_ms) { | |
7604 | arc_min_prescient_prefetch_ms = | |
7605 | zfs_arc_min_prescient_prefetch_ms; | |
7606 | } | |
11f552fa | 7607 | |
7e8bddd0 BB |
7608 | /* Valid range: 0 - 100 */ |
7609 | if ((zfs_arc_lotsfree_percent >= 0) && | |
7610 | (zfs_arc_lotsfree_percent <= 100)) | |
7611 | arc_lotsfree_percent = zfs_arc_lotsfree_percent; | |
36a6e233 RM |
7612 | WARN_IF_TUNING_IGNORED(zfs_arc_lotsfree_percent, arc_lotsfree_percent, |
7613 | verbose); | |
7e8bddd0 | 7614 | |
11f552fa BB |
7615 | /* Valid range: 0 - <all physical memory> */ |
7616 | if ((zfs_arc_sys_free) && (zfs_arc_sys_free != arc_sys_free)) | |
9edb3695 | 7617 | arc_sys_free = MIN(MAX(zfs_arc_sys_free, 0), allmem); |
36a6e233 | 7618 | WARN_IF_TUNING_IGNORED(zfs_arc_sys_free, arc_sys_free, verbose); |
ca67b33a MA |
7619 | } |
7620 | ||
d3c2ae1c GW |
7621 | static void |
7622 | arc_state_init(void) | |
7623 | { | |
ffdf019c AM |
7624 | multilist_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], |
7625 | sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7626 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7627 | arc_state_multilist_index_func); |
ffdf019c AM |
7628 | multilist_create(&arc_mru->arcs_list[ARC_BUFC_DATA], |
7629 | sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7630 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7631 | arc_state_multilist_index_func); |
ffdf019c AM |
7632 | multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], |
7633 | sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7634 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7635 | arc_state_multilist_index_func); |
ffdf019c AM |
7636 | multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], |
7637 | sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7638 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7639 | arc_state_multilist_index_func); |
ffdf019c AM |
7640 | multilist_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], |
7641 | sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7642 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7643 | arc_state_multilist_index_func); |
ffdf019c AM |
7644 | multilist_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], |
7645 | sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7646 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7647 | arc_state_multilist_index_func); |
ffdf019c AM |
7648 | multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], |
7649 | sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7650 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7651 | arc_state_multilist_index_func); |
ffdf019c AM |
7652 | multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], |
7653 | sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7654 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7655 | arc_state_multilist_index_func); |
bb7ad5d3 AM |
7656 | /* |
7657 | * L2 headers should never be on the L2 state list since they don't | |
7658 | * have L1 headers allocated. Special index function asserts that. | |
7659 | */ | |
ffdf019c AM |
7660 | multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], |
7661 | sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7662 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
bb7ad5d3 | 7663 | arc_state_l2c_multilist_index_func); |
ffdf019c AM |
7664 | multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], |
7665 | sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7666 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
bb7ad5d3 | 7667 | arc_state_l2c_multilist_index_func); |
d3c2ae1c | 7668 | |
424fd7c3 TS |
7669 | zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); |
7670 | zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]); | |
7671 | zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); | |
7672 | zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]); | |
7673 | zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7674 | zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7675 | zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); | |
7676 | zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); | |
7677 | zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7678 | zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7679 | zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); | |
7680 | zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); | |
7681 | ||
7682 | zfs_refcount_create(&arc_anon->arcs_size); | |
7683 | zfs_refcount_create(&arc_mru->arcs_size); | |
7684 | zfs_refcount_create(&arc_mru_ghost->arcs_size); | |
7685 | zfs_refcount_create(&arc_mfu->arcs_size); | |
7686 | zfs_refcount_create(&arc_mfu_ghost->arcs_size); | |
7687 | zfs_refcount_create(&arc_l2c_only->arcs_size); | |
d3c2ae1c | 7688 | |
c4c162c1 AM |
7689 | wmsum_init(&arc_sums.arcstat_hits, 0); |
7690 | wmsum_init(&arc_sums.arcstat_misses, 0); | |
7691 | wmsum_init(&arc_sums.arcstat_demand_data_hits, 0); | |
7692 | wmsum_init(&arc_sums.arcstat_demand_data_misses, 0); | |
7693 | wmsum_init(&arc_sums.arcstat_demand_metadata_hits, 0); | |
7694 | wmsum_init(&arc_sums.arcstat_demand_metadata_misses, 0); | |
7695 | wmsum_init(&arc_sums.arcstat_prefetch_data_hits, 0); | |
7696 | wmsum_init(&arc_sums.arcstat_prefetch_data_misses, 0); | |
7697 | wmsum_init(&arc_sums.arcstat_prefetch_metadata_hits, 0); | |
7698 | wmsum_init(&arc_sums.arcstat_prefetch_metadata_misses, 0); | |
7699 | wmsum_init(&arc_sums.arcstat_mru_hits, 0); | |
7700 | wmsum_init(&arc_sums.arcstat_mru_ghost_hits, 0); | |
7701 | wmsum_init(&arc_sums.arcstat_mfu_hits, 0); | |
7702 | wmsum_init(&arc_sums.arcstat_mfu_ghost_hits, 0); | |
7703 | wmsum_init(&arc_sums.arcstat_deleted, 0); | |
7704 | wmsum_init(&arc_sums.arcstat_mutex_miss, 0); | |
7705 | wmsum_init(&arc_sums.arcstat_access_skip, 0); | |
7706 | wmsum_init(&arc_sums.arcstat_evict_skip, 0); | |
7707 | wmsum_init(&arc_sums.arcstat_evict_not_enough, 0); | |
7708 | wmsum_init(&arc_sums.arcstat_evict_l2_cached, 0); | |
7709 | wmsum_init(&arc_sums.arcstat_evict_l2_eligible, 0); | |
7710 | wmsum_init(&arc_sums.arcstat_evict_l2_eligible_mfu, 0); | |
7711 | wmsum_init(&arc_sums.arcstat_evict_l2_eligible_mru, 0); | |
7712 | wmsum_init(&arc_sums.arcstat_evict_l2_ineligible, 0); | |
7713 | wmsum_init(&arc_sums.arcstat_evict_l2_skip, 0); | |
7714 | wmsum_init(&arc_sums.arcstat_hash_collisions, 0); | |
7715 | wmsum_init(&arc_sums.arcstat_hash_chains, 0); | |
7716 | aggsum_init(&arc_sums.arcstat_size, 0); | |
7717 | wmsum_init(&arc_sums.arcstat_compressed_size, 0); | |
7718 | wmsum_init(&arc_sums.arcstat_uncompressed_size, 0); | |
7719 | wmsum_init(&arc_sums.arcstat_overhead_size, 0); | |
7720 | wmsum_init(&arc_sums.arcstat_hdr_size, 0); | |
7721 | wmsum_init(&arc_sums.arcstat_data_size, 0); | |
7722 | wmsum_init(&arc_sums.arcstat_metadata_size, 0); | |
7723 | wmsum_init(&arc_sums.arcstat_dbuf_size, 0); | |
7724 | aggsum_init(&arc_sums.arcstat_dnode_size, 0); | |
7725 | wmsum_init(&arc_sums.arcstat_bonus_size, 0); | |
7726 | wmsum_init(&arc_sums.arcstat_l2_hits, 0); | |
7727 | wmsum_init(&arc_sums.arcstat_l2_misses, 0); | |
7728 | wmsum_init(&arc_sums.arcstat_l2_prefetch_asize, 0); | |
7729 | wmsum_init(&arc_sums.arcstat_l2_mru_asize, 0); | |
7730 | wmsum_init(&arc_sums.arcstat_l2_mfu_asize, 0); | |
7731 | wmsum_init(&arc_sums.arcstat_l2_bufc_data_asize, 0); | |
7732 | wmsum_init(&arc_sums.arcstat_l2_bufc_metadata_asize, 0); | |
7733 | wmsum_init(&arc_sums.arcstat_l2_feeds, 0); | |
7734 | wmsum_init(&arc_sums.arcstat_l2_rw_clash, 0); | |
7735 | wmsum_init(&arc_sums.arcstat_l2_read_bytes, 0); | |
7736 | wmsum_init(&arc_sums.arcstat_l2_write_bytes, 0); | |
7737 | wmsum_init(&arc_sums.arcstat_l2_writes_sent, 0); | |
7738 | wmsum_init(&arc_sums.arcstat_l2_writes_done, 0); | |
7739 | wmsum_init(&arc_sums.arcstat_l2_writes_error, 0); | |
7740 | wmsum_init(&arc_sums.arcstat_l2_writes_lock_retry, 0); | |
7741 | wmsum_init(&arc_sums.arcstat_l2_evict_lock_retry, 0); | |
7742 | wmsum_init(&arc_sums.arcstat_l2_evict_reading, 0); | |
7743 | wmsum_init(&arc_sums.arcstat_l2_evict_l1cached, 0); | |
7744 | wmsum_init(&arc_sums.arcstat_l2_free_on_write, 0); | |
7745 | wmsum_init(&arc_sums.arcstat_l2_abort_lowmem, 0); | |
7746 | wmsum_init(&arc_sums.arcstat_l2_cksum_bad, 0); | |
7747 | wmsum_init(&arc_sums.arcstat_l2_io_error, 0); | |
7748 | wmsum_init(&arc_sums.arcstat_l2_lsize, 0); | |
7749 | wmsum_init(&arc_sums.arcstat_l2_psize, 0); | |
7750 | aggsum_init(&arc_sums.arcstat_l2_hdr_size, 0); | |
7751 | wmsum_init(&arc_sums.arcstat_l2_log_blk_writes, 0); | |
7752 | wmsum_init(&arc_sums.arcstat_l2_log_blk_asize, 0); | |
7753 | wmsum_init(&arc_sums.arcstat_l2_log_blk_count, 0); | |
7754 | wmsum_init(&arc_sums.arcstat_l2_rebuild_success, 0); | |
7755 | wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_unsupported, 0); | |
7756 | wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_io_errors, 0); | |
7757 | wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_dh_errors, 0); | |
7758 | wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors, 0); | |
7759 | wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_lowmem, 0); | |
7760 | wmsum_init(&arc_sums.arcstat_l2_rebuild_size, 0); | |
7761 | wmsum_init(&arc_sums.arcstat_l2_rebuild_asize, 0); | |
7762 | wmsum_init(&arc_sums.arcstat_l2_rebuild_bufs, 0); | |
7763 | wmsum_init(&arc_sums.arcstat_l2_rebuild_bufs_precached, 0); | |
7764 | wmsum_init(&arc_sums.arcstat_l2_rebuild_log_blks, 0); | |
7765 | wmsum_init(&arc_sums.arcstat_memory_throttle_count, 0); | |
7766 | wmsum_init(&arc_sums.arcstat_memory_direct_count, 0); | |
7767 | wmsum_init(&arc_sums.arcstat_memory_indirect_count, 0); | |
7768 | wmsum_init(&arc_sums.arcstat_prune, 0); | |
7769 | aggsum_init(&arc_sums.arcstat_meta_used, 0); | |
7770 | wmsum_init(&arc_sums.arcstat_async_upgrade_sync, 0); | |
7771 | wmsum_init(&arc_sums.arcstat_demand_hit_predictive_prefetch, 0); | |
7772 | wmsum_init(&arc_sums.arcstat_demand_hit_prescient_prefetch, 0); | |
7773 | wmsum_init(&arc_sums.arcstat_raw_size, 0); | |
7774 | wmsum_init(&arc_sums.arcstat_cached_only_in_progress, 0); | |
7775 | wmsum_init(&arc_sums.arcstat_abd_chunk_waste_size, 0); | |
37fb3e43 | 7776 | |
d3c2ae1c GW |
7777 | arc_anon->arcs_state = ARC_STATE_ANON; |
7778 | arc_mru->arcs_state = ARC_STATE_MRU; | |
7779 | arc_mru_ghost->arcs_state = ARC_STATE_MRU_GHOST; | |
7780 | arc_mfu->arcs_state = ARC_STATE_MFU; | |
7781 | arc_mfu_ghost->arcs_state = ARC_STATE_MFU_GHOST; | |
7782 | arc_l2c_only->arcs_state = ARC_STATE_L2C_ONLY; | |
7783 | } | |
7784 | ||
7785 | static void | |
7786 | arc_state_fini(void) | |
7787 | { | |
424fd7c3 TS |
7788 | zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); |
7789 | zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]); | |
7790 | zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); | |
7791 | zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]); | |
7792 | zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7793 | zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7794 | zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); | |
7795 | zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); | |
7796 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7797 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7798 | zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); | |
7799 | zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); | |
7800 | ||
7801 | zfs_refcount_destroy(&arc_anon->arcs_size); | |
7802 | zfs_refcount_destroy(&arc_mru->arcs_size); | |
7803 | zfs_refcount_destroy(&arc_mru_ghost->arcs_size); | |
7804 | zfs_refcount_destroy(&arc_mfu->arcs_size); | |
7805 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_size); | |
7806 | zfs_refcount_destroy(&arc_l2c_only->arcs_size); | |
d3c2ae1c | 7807 | |
ffdf019c AM |
7808 | multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); |
7809 | multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); | |
7810 | multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); | |
7811 | multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); | |
7812 | multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); | |
7813 | multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); | |
7814 | multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); | |
7815 | multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); | |
7816 | multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA]); | |
7817 | multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]); | |
37fb3e43 | 7818 | |
c4c162c1 AM |
7819 | wmsum_fini(&arc_sums.arcstat_hits); |
7820 | wmsum_fini(&arc_sums.arcstat_misses); | |
7821 | wmsum_fini(&arc_sums.arcstat_demand_data_hits); | |
7822 | wmsum_fini(&arc_sums.arcstat_demand_data_misses); | |
7823 | wmsum_fini(&arc_sums.arcstat_demand_metadata_hits); | |
7824 | wmsum_fini(&arc_sums.arcstat_demand_metadata_misses); | |
7825 | wmsum_fini(&arc_sums.arcstat_prefetch_data_hits); | |
7826 | wmsum_fini(&arc_sums.arcstat_prefetch_data_misses); | |
7827 | wmsum_fini(&arc_sums.arcstat_prefetch_metadata_hits); | |
7828 | wmsum_fini(&arc_sums.arcstat_prefetch_metadata_misses); | |
7829 | wmsum_fini(&arc_sums.arcstat_mru_hits); | |
7830 | wmsum_fini(&arc_sums.arcstat_mru_ghost_hits); | |
7831 | wmsum_fini(&arc_sums.arcstat_mfu_hits); | |
7832 | wmsum_fini(&arc_sums.arcstat_mfu_ghost_hits); | |
7833 | wmsum_fini(&arc_sums.arcstat_deleted); | |
7834 | wmsum_fini(&arc_sums.arcstat_mutex_miss); | |
7835 | wmsum_fini(&arc_sums.arcstat_access_skip); | |
7836 | wmsum_fini(&arc_sums.arcstat_evict_skip); | |
7837 | wmsum_fini(&arc_sums.arcstat_evict_not_enough); | |
7838 | wmsum_fini(&arc_sums.arcstat_evict_l2_cached); | |
7839 | wmsum_fini(&arc_sums.arcstat_evict_l2_eligible); | |
7840 | wmsum_fini(&arc_sums.arcstat_evict_l2_eligible_mfu); | |
7841 | wmsum_fini(&arc_sums.arcstat_evict_l2_eligible_mru); | |
7842 | wmsum_fini(&arc_sums.arcstat_evict_l2_ineligible); | |
7843 | wmsum_fini(&arc_sums.arcstat_evict_l2_skip); | |
7844 | wmsum_fini(&arc_sums.arcstat_hash_collisions); | |
7845 | wmsum_fini(&arc_sums.arcstat_hash_chains); | |
7846 | aggsum_fini(&arc_sums.arcstat_size); | |
7847 | wmsum_fini(&arc_sums.arcstat_compressed_size); | |
7848 | wmsum_fini(&arc_sums.arcstat_uncompressed_size); | |
7849 | wmsum_fini(&arc_sums.arcstat_overhead_size); | |
7850 | wmsum_fini(&arc_sums.arcstat_hdr_size); | |
7851 | wmsum_fini(&arc_sums.arcstat_data_size); | |
7852 | wmsum_fini(&arc_sums.arcstat_metadata_size); | |
7853 | wmsum_fini(&arc_sums.arcstat_dbuf_size); | |
7854 | aggsum_fini(&arc_sums.arcstat_dnode_size); | |
7855 | wmsum_fini(&arc_sums.arcstat_bonus_size); | |
7856 | wmsum_fini(&arc_sums.arcstat_l2_hits); | |
7857 | wmsum_fini(&arc_sums.arcstat_l2_misses); | |
7858 | wmsum_fini(&arc_sums.arcstat_l2_prefetch_asize); | |
7859 | wmsum_fini(&arc_sums.arcstat_l2_mru_asize); | |
7860 | wmsum_fini(&arc_sums.arcstat_l2_mfu_asize); | |
7861 | wmsum_fini(&arc_sums.arcstat_l2_bufc_data_asize); | |
7862 | wmsum_fini(&arc_sums.arcstat_l2_bufc_metadata_asize); | |
7863 | wmsum_fini(&arc_sums.arcstat_l2_feeds); | |
7864 | wmsum_fini(&arc_sums.arcstat_l2_rw_clash); | |
7865 | wmsum_fini(&arc_sums.arcstat_l2_read_bytes); | |
7866 | wmsum_fini(&arc_sums.arcstat_l2_write_bytes); | |
7867 | wmsum_fini(&arc_sums.arcstat_l2_writes_sent); | |
7868 | wmsum_fini(&arc_sums.arcstat_l2_writes_done); | |
7869 | wmsum_fini(&arc_sums.arcstat_l2_writes_error); | |
7870 | wmsum_fini(&arc_sums.arcstat_l2_writes_lock_retry); | |
7871 | wmsum_fini(&arc_sums.arcstat_l2_evict_lock_retry); | |
7872 | wmsum_fini(&arc_sums.arcstat_l2_evict_reading); | |
7873 | wmsum_fini(&arc_sums.arcstat_l2_evict_l1cached); | |
7874 | wmsum_fini(&arc_sums.arcstat_l2_free_on_write); | |
7875 | wmsum_fini(&arc_sums.arcstat_l2_abort_lowmem); | |
7876 | wmsum_fini(&arc_sums.arcstat_l2_cksum_bad); | |
7877 | wmsum_fini(&arc_sums.arcstat_l2_io_error); | |
7878 | wmsum_fini(&arc_sums.arcstat_l2_lsize); | |
7879 | wmsum_fini(&arc_sums.arcstat_l2_psize); | |
7880 | aggsum_fini(&arc_sums.arcstat_l2_hdr_size); | |
7881 | wmsum_fini(&arc_sums.arcstat_l2_log_blk_writes); | |
7882 | wmsum_fini(&arc_sums.arcstat_l2_log_blk_asize); | |
7883 | wmsum_fini(&arc_sums.arcstat_l2_log_blk_count); | |
7884 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_success); | |
7885 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_unsupported); | |
7886 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_io_errors); | |
7887 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_dh_errors); | |
7888 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors); | |
7889 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_lowmem); | |
7890 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_size); | |
7891 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_asize); | |
7892 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_bufs); | |
7893 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_bufs_precached); | |
7894 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_log_blks); | |
7895 | wmsum_fini(&arc_sums.arcstat_memory_throttle_count); | |
7896 | wmsum_fini(&arc_sums.arcstat_memory_direct_count); | |
7897 | wmsum_fini(&arc_sums.arcstat_memory_indirect_count); | |
7898 | wmsum_fini(&arc_sums.arcstat_prune); | |
7899 | aggsum_fini(&arc_sums.arcstat_meta_used); | |
7900 | wmsum_fini(&arc_sums.arcstat_async_upgrade_sync); | |
7901 | wmsum_fini(&arc_sums.arcstat_demand_hit_predictive_prefetch); | |
7902 | wmsum_fini(&arc_sums.arcstat_demand_hit_prescient_prefetch); | |
7903 | wmsum_fini(&arc_sums.arcstat_raw_size); | |
7904 | wmsum_fini(&arc_sums.arcstat_cached_only_in_progress); | |
7905 | wmsum_fini(&arc_sums.arcstat_abd_chunk_waste_size); | |
d3c2ae1c GW |
7906 | } |
7907 | ||
7908 | uint64_t | |
e71cade6 | 7909 | arc_target_bytes(void) |
d3c2ae1c | 7910 | { |
e71cade6 | 7911 | return (arc_c); |
d3c2ae1c GW |
7912 | } |
7913 | ||
60a4c7d2 PD |
7914 | void |
7915 | arc_set_limits(uint64_t allmem) | |
7916 | { | |
7917 | /* Set min cache to 1/32 of all memory, or 32MB, whichever is more. */ | |
7918 | arc_c_min = MAX(allmem / 32, 2ULL << SPA_MAXBLOCKSHIFT); | |
7919 | ||
7920 | /* How to set default max varies by platform. */ | |
7921 | arc_c_max = arc_default_max(arc_c_min, allmem); | |
7922 | } | |
34dc7c2f BB |
7923 | void |
7924 | arc_init(void) | |
7925 | { | |
9edb3695 | 7926 | uint64_t percent, allmem = arc_all_memory(); |
5dd92909 | 7927 | mutex_init(&arc_evict_lock, NULL, MUTEX_DEFAULT, NULL); |
3442c2a0 MA |
7928 | list_create(&arc_evict_waiters, sizeof (arc_evict_waiter_t), |
7929 | offsetof(arc_evict_waiter_t, aew_node)); | |
ca0bf58d | 7930 | |
2b84817f TC |
7931 | arc_min_prefetch_ms = 1000; |
7932 | arc_min_prescient_prefetch_ms = 6000; | |
34dc7c2f | 7933 | |
c9c9c1e2 MM |
7934 | #if defined(_KERNEL) |
7935 | arc_lowmem_init(); | |
34dc7c2f BB |
7936 | #endif |
7937 | ||
60a4c7d2 | 7938 | arc_set_limits(allmem); |
9a51738b | 7939 | |
e945e8d7 AJ |
7940 | #ifdef _KERNEL |
7941 | /* | |
7942 | * If zfs_arc_max is non-zero at init, meaning it was set in the kernel | |
7943 | * environment before the module was loaded, don't block setting the | |
7944 | * maximum because it is less than arc_c_min, instead, reset arc_c_min | |
7945 | * to a lower value. | |
7946 | * zfs_arc_min will be handled by arc_tuning_update(). | |
7947 | */ | |
7948 | if (zfs_arc_max != 0 && zfs_arc_max >= MIN_ARC_MAX && | |
7949 | zfs_arc_max < allmem) { | |
7950 | arc_c_max = zfs_arc_max; | |
7951 | if (arc_c_min >= arc_c_max) { | |
7952 | arc_c_min = MAX(zfs_arc_max / 2, | |
7953 | 2ULL << SPA_MAXBLOCKSHIFT); | |
7954 | } | |
7955 | } | |
7956 | #else | |
ab5cbbd1 BB |
7957 | /* |
7958 | * In userland, there's only the memory pressure that we artificially | |
7959 | * create (see arc_available_memory()). Don't let arc_c get too | |
7960 | * small, because it can cause transactions to be larger than | |
7961 | * arc_c, causing arc_tempreserve_space() to fail. | |
7962 | */ | |
0a1f8cd9 | 7963 | arc_c_min = MAX(arc_c_max / 2, 2ULL << SPA_MAXBLOCKSHIFT); |
ab5cbbd1 BB |
7964 | #endif |
7965 | ||
17ca3018 | 7966 | arc_c = arc_c_min; |
34dc7c2f BB |
7967 | arc_p = (arc_c >> 1); |
7968 | ||
ca67b33a MA |
7969 | /* Set min to 1/2 of arc_c_min */ |
7970 | arc_meta_min = 1ULL << SPA_MAXBLOCKSHIFT; | |
9907cc1c G |
7971 | /* |
7972 | * Set arc_meta_limit to a percent of arc_c_max with a floor of | |
7973 | * arc_meta_min, and a ceiling of arc_c_max. | |
7974 | */ | |
7975 | percent = MIN(zfs_arc_meta_limit_percent, 100); | |
7976 | arc_meta_limit = MAX(arc_meta_min, (percent * arc_c_max) / 100); | |
7977 | percent = MIN(zfs_arc_dnode_limit_percent, 100); | |
03fdcb9a | 7978 | arc_dnode_size_limit = (percent * arc_meta_limit) / 100; |
34dc7c2f | 7979 | |
ca67b33a | 7980 | /* Apply user specified tunings */ |
36a6e233 | 7981 | arc_tuning_update(B_TRUE); |
c52fca13 | 7982 | |
34dc7c2f BB |
7983 | /* if kmem_flags are set, lets try to use less memory */ |
7984 | if (kmem_debugging()) | |
7985 | arc_c = arc_c / 2; | |
7986 | if (arc_c < arc_c_min) | |
7987 | arc_c = arc_c_min; | |
7988 | ||
60a4c7d2 PD |
7989 | arc_register_hotplug(); |
7990 | ||
d3c2ae1c | 7991 | arc_state_init(); |
3ec34e55 | 7992 | |
34dc7c2f BB |
7993 | buf_init(); |
7994 | ||
ab26409d BB |
7995 | list_create(&arc_prune_list, sizeof (arc_prune_t), |
7996 | offsetof(arc_prune_t, p_node)); | |
ab26409d | 7997 | mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f | 7998 | |
462217d1 AM |
7999 | arc_prune_taskq = taskq_create("arc_prune", zfs_arc_prune_task_threads, |
8000 | defclsyspri, 100, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC); | |
f6046738 | 8001 | |
34dc7c2f BB |
8002 | arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, |
8003 | sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); | |
8004 | ||
8005 | if (arc_ksp != NULL) { | |
8006 | arc_ksp->ks_data = &arc_stats; | |
13be560d | 8007 | arc_ksp->ks_update = arc_kstat_update; |
34dc7c2f BB |
8008 | kstat_install(arc_ksp); |
8009 | } | |
8010 | ||
1531506d | 8011 | arc_evict_zthr = zthr_create("arc_evict", |
6bc61d22 | 8012 | arc_evict_cb_check, arc_evict_cb, NULL, defclsyspri); |
843e9ca2 | 8013 | arc_reap_zthr = zthr_create_timer("arc_reap", |
6bc61d22 | 8014 | arc_reap_cb_check, arc_reap_cb, NULL, SEC2NSEC(1), minclsyspri); |
34dc7c2f | 8015 | |
b128c09f | 8016 | arc_warm = B_FALSE; |
34dc7c2f | 8017 | |
e8b96c60 MA |
8018 | /* |
8019 | * Calculate maximum amount of dirty data per pool. | |
8020 | * | |
8021 | * If it has been set by a module parameter, take that. | |
8022 | * Otherwise, use a percentage of physical memory defined by | |
8023 | * zfs_dirty_data_max_percent (default 10%) with a cap at | |
e99932f7 | 8024 | * zfs_dirty_data_max_max (default 4G or 25% of physical memory). |
e8b96c60 | 8025 | */ |
47ed79ff | 8026 | #ifdef __LP64__ |
e8b96c60 | 8027 | if (zfs_dirty_data_max_max == 0) |
e99932f7 BB |
8028 | zfs_dirty_data_max_max = MIN(4ULL * 1024 * 1024 * 1024, |
8029 | allmem * zfs_dirty_data_max_max_percent / 100); | |
47ed79ff MM |
8030 | #else |
8031 | if (zfs_dirty_data_max_max == 0) | |
8032 | zfs_dirty_data_max_max = MIN(1ULL * 1024 * 1024 * 1024, | |
8033 | allmem * zfs_dirty_data_max_max_percent / 100); | |
8034 | #endif | |
e8b96c60 MA |
8035 | |
8036 | if (zfs_dirty_data_max == 0) { | |
9edb3695 | 8037 | zfs_dirty_data_max = allmem * |
e8b96c60 MA |
8038 | zfs_dirty_data_max_percent / 100; |
8039 | zfs_dirty_data_max = MIN(zfs_dirty_data_max, | |
8040 | zfs_dirty_data_max_max); | |
8041 | } | |
a7bd20e3 KJ |
8042 | |
8043 | if (zfs_wrlog_data_max == 0) { | |
8044 | ||
8045 | /* | |
8046 | * dp_wrlog_total is reduced for each txg at the end of | |
8047 | * spa_sync(). However, dp_dirty_total is reduced every time | |
8048 | * a block is written out. Thus under normal operation, | |
8049 | * dp_wrlog_total could grow 2 times as big as | |
8050 | * zfs_dirty_data_max. | |
8051 | */ | |
8052 | zfs_wrlog_data_max = zfs_dirty_data_max * 2; | |
8053 | } | |
34dc7c2f BB |
8054 | } |
8055 | ||
8056 | void | |
8057 | arc_fini(void) | |
8058 | { | |
ab26409d BB |
8059 | arc_prune_t *p; |
8060 | ||
7cb67b45 | 8061 | #ifdef _KERNEL |
c9c9c1e2 | 8062 | arc_lowmem_fini(); |
7cb67b45 BB |
8063 | #endif /* _KERNEL */ |
8064 | ||
d3c2ae1c GW |
8065 | /* Use B_TRUE to ensure *all* buffers are evicted */ |
8066 | arc_flush(NULL, B_TRUE); | |
34dc7c2f | 8067 | |
34dc7c2f BB |
8068 | if (arc_ksp != NULL) { |
8069 | kstat_delete(arc_ksp); | |
8070 | arc_ksp = NULL; | |
8071 | } | |
8072 | ||
f6046738 BB |
8073 | taskq_wait(arc_prune_taskq); |
8074 | taskq_destroy(arc_prune_taskq); | |
8075 | ||
ab26409d BB |
8076 | mutex_enter(&arc_prune_mtx); |
8077 | while ((p = list_head(&arc_prune_list)) != NULL) { | |
8078 | list_remove(&arc_prune_list, p); | |
424fd7c3 TS |
8079 | zfs_refcount_remove(&p->p_refcnt, &arc_prune_list); |
8080 | zfs_refcount_destroy(&p->p_refcnt); | |
ab26409d BB |
8081 | kmem_free(p, sizeof (*p)); |
8082 | } | |
8083 | mutex_exit(&arc_prune_mtx); | |
8084 | ||
8085 | list_destroy(&arc_prune_list); | |
8086 | mutex_destroy(&arc_prune_mtx); | |
3ec34e55 | 8087 | |
5dd92909 | 8088 | (void) zthr_cancel(arc_evict_zthr); |
3ec34e55 | 8089 | (void) zthr_cancel(arc_reap_zthr); |
3ec34e55 | 8090 | |
5dd92909 | 8091 | mutex_destroy(&arc_evict_lock); |
3442c2a0 | 8092 | list_destroy(&arc_evict_waiters); |
ca0bf58d | 8093 | |
cfd59f90 BB |
8094 | /* |
8095 | * Free any buffers that were tagged for destruction. This needs | |
8096 | * to occur before arc_state_fini() runs and destroys the aggsum | |
8097 | * values which are updated when freeing scatter ABDs. | |
8098 | */ | |
8099 | l2arc_do_free_on_write(); | |
8100 | ||
ae3d8491 PD |
8101 | /* |
8102 | * buf_fini() must proceed arc_state_fini() because buf_fin() may | |
8103 | * trigger the release of kmem magazines, which can callback to | |
8104 | * arc_space_return() which accesses aggsums freed in act_state_fini(). | |
8105 | */ | |
34dc7c2f | 8106 | buf_fini(); |
ae3d8491 | 8107 | arc_state_fini(); |
9babb374 | 8108 | |
60a4c7d2 PD |
8109 | arc_unregister_hotplug(); |
8110 | ||
1c44a5c9 SD |
8111 | /* |
8112 | * We destroy the zthrs after all the ARC state has been | |
8113 | * torn down to avoid the case of them receiving any | |
8114 | * wakeup() signals after they are destroyed. | |
8115 | */ | |
5dd92909 | 8116 | zthr_destroy(arc_evict_zthr); |
1c44a5c9 SD |
8117 | zthr_destroy(arc_reap_zthr); |
8118 | ||
b9541d6b | 8119 | ASSERT0(arc_loaned_bytes); |
34dc7c2f BB |
8120 | } |
8121 | ||
8122 | /* | |
8123 | * Level 2 ARC | |
8124 | * | |
8125 | * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. | |
8126 | * It uses dedicated storage devices to hold cached data, which are populated | |
8127 | * using large infrequent writes. The main role of this cache is to boost | |
8128 | * the performance of random read workloads. The intended L2ARC devices | |
8129 | * include short-stroked disks, solid state disks, and other media with | |
8130 | * substantially faster read latency than disk. | |
8131 | * | |
8132 | * +-----------------------+ | |
8133 | * | ARC | | |
8134 | * +-----------------------+ | |
8135 | * | ^ ^ | |
8136 | * | | | | |
8137 | * l2arc_feed_thread() arc_read() | |
8138 | * | | | | |
8139 | * | l2arc read | | |
8140 | * V | | | |
8141 | * +---------------+ | | |
8142 | * | L2ARC | | | |
8143 | * +---------------+ | | |
8144 | * | ^ | | |
8145 | * l2arc_write() | | | |
8146 | * | | | | |
8147 | * V | | | |
8148 | * +-------+ +-------+ | |
8149 | * | vdev | | vdev | | |
8150 | * | cache | | cache | | |
8151 | * +-------+ +-------+ | |
8152 | * +=========+ .-----. | |
8153 | * : L2ARC : |-_____-| | |
8154 | * : devices : | Disks | | |
8155 | * +=========+ `-_____-' | |
8156 | * | |
8157 | * Read requests are satisfied from the following sources, in order: | |
8158 | * | |
8159 | * 1) ARC | |
8160 | * 2) vdev cache of L2ARC devices | |
8161 | * 3) L2ARC devices | |
8162 | * 4) vdev cache of disks | |
8163 | * 5) disks | |
8164 | * | |
8165 | * Some L2ARC device types exhibit extremely slow write performance. | |
8166 | * To accommodate for this there are some significant differences between | |
8167 | * the L2ARC and traditional cache design: | |
8168 | * | |
8169 | * 1. There is no eviction path from the ARC to the L2ARC. Evictions from | |
8170 | * the ARC behave as usual, freeing buffers and placing headers on ghost | |
8171 | * lists. The ARC does not send buffers to the L2ARC during eviction as | |
8172 | * this would add inflated write latencies for all ARC memory pressure. | |
8173 | * | |
8174 | * 2. The L2ARC attempts to cache data from the ARC before it is evicted. | |
8175 | * It does this by periodically scanning buffers from the eviction-end of | |
8176 | * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are | |
3a17a7a9 SK |
8177 | * not already there. It scans until a headroom of buffers is satisfied, |
8178 | * which itself is a buffer for ARC eviction. If a compressible buffer is | |
8179 | * found during scanning and selected for writing to an L2ARC device, we | |
8180 | * temporarily boost scanning headroom during the next scan cycle to make | |
8181 | * sure we adapt to compression effects (which might significantly reduce | |
8182 | * the data volume we write to L2ARC). The thread that does this is | |
34dc7c2f BB |
8183 | * l2arc_feed_thread(), illustrated below; example sizes are included to |
8184 | * provide a better sense of ratio than this diagram: | |
8185 | * | |
8186 | * head --> tail | |
8187 | * +---------------------+----------+ | |
8188 | * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC | |
8189 | * +---------------------+----------+ | o L2ARC eligible | |
8190 | * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer | |
8191 | * +---------------------+----------+ | | |
8192 | * 15.9 Gbytes ^ 32 Mbytes | | |
8193 | * headroom | | |
8194 | * l2arc_feed_thread() | |
8195 | * | | |
8196 | * l2arc write hand <--[oooo]--' | |
8197 | * | 8 Mbyte | |
8198 | * | write max | |
8199 | * V | |
8200 | * +==============================+ | |
8201 | * L2ARC dev |####|#|###|###| |####| ... | | |
8202 | * +==============================+ | |
8203 | * 32 Gbytes | |
8204 | * | |
8205 | * 3. If an ARC buffer is copied to the L2ARC but then hit instead of | |
8206 | * evicted, then the L2ARC has cached a buffer much sooner than it probably | |
8207 | * needed to, potentially wasting L2ARC device bandwidth and storage. It is | |
8208 | * safe to say that this is an uncommon case, since buffers at the end of | |
8209 | * the ARC lists have moved there due to inactivity. | |
8210 | * | |
8211 | * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, | |
8212 | * then the L2ARC simply misses copying some buffers. This serves as a | |
8213 | * pressure valve to prevent heavy read workloads from both stalling the ARC | |
8214 | * with waits and clogging the L2ARC with writes. This also helps prevent | |
8215 | * the potential for the L2ARC to churn if it attempts to cache content too | |
8216 | * quickly, such as during backups of the entire pool. | |
8217 | * | |
b128c09f BB |
8218 | * 5. After system boot and before the ARC has filled main memory, there are |
8219 | * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru | |
8220 | * lists can remain mostly static. Instead of searching from tail of these | |
8221 | * lists as pictured, the l2arc_feed_thread() will search from the list heads | |
8222 | * for eligible buffers, greatly increasing its chance of finding them. | |
8223 | * | |
8224 | * The L2ARC device write speed is also boosted during this time so that | |
8225 | * the L2ARC warms up faster. Since there have been no ARC evictions yet, | |
8226 | * there are no L2ARC reads, and no fear of degrading read performance | |
8227 | * through increased writes. | |
8228 | * | |
8229 | * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that | |
34dc7c2f BB |
8230 | * the vdev queue can aggregate them into larger and fewer writes. Each |
8231 | * device is written to in a rotor fashion, sweeping writes through | |
8232 | * available space then repeating. | |
8233 | * | |
b128c09f | 8234 | * 7. The L2ARC does not store dirty content. It never needs to flush |
34dc7c2f BB |
8235 | * write buffers back to disk based storage. |
8236 | * | |
b128c09f | 8237 | * 8. If an ARC buffer is written (and dirtied) which also exists in the |
34dc7c2f BB |
8238 | * L2ARC, the now stale L2ARC buffer is immediately dropped. |
8239 | * | |
8240 | * The performance of the L2ARC can be tweaked by a number of tunables, which | |
8241 | * may be necessary for different workloads: | |
8242 | * | |
8243 | * l2arc_write_max max write bytes per interval | |
b128c09f | 8244 | * l2arc_write_boost extra write bytes during device warmup |
34dc7c2f BB |
8245 | * l2arc_noprefetch skip caching prefetched buffers |
8246 | * l2arc_headroom number of max device writes to precache | |
3a17a7a9 SK |
8247 | * l2arc_headroom_boost when we find compressed buffers during ARC |
8248 | * scanning, we multiply headroom by this | |
8249 | * percentage factor for the next scan cycle, | |
8250 | * since more compressed buffers are likely to | |
8251 | * be present | |
34dc7c2f BB |
8252 | * l2arc_feed_secs seconds between L2ARC writing |
8253 | * | |
8254 | * Tunables may be removed or added as future performance improvements are | |
8255 | * integrated, and also may become zpool properties. | |
d164b209 BB |
8256 | * |
8257 | * There are three key functions that control how the L2ARC warms up: | |
8258 | * | |
8259 | * l2arc_write_eligible() check if a buffer is eligible to cache | |
8260 | * l2arc_write_size() calculate how much to write | |
8261 | * l2arc_write_interval() calculate sleep delay between writes | |
8262 | * | |
8263 | * These three functions determine what to write, how much, and how quickly | |
8264 | * to send writes. | |
77f6826b GA |
8265 | * |
8266 | * L2ARC persistence: | |
8267 | * | |
8268 | * When writing buffers to L2ARC, we periodically add some metadata to | |
8269 | * make sure we can pick them up after reboot, thus dramatically reducing | |
8270 | * the impact that any downtime has on the performance of storage systems | |
8271 | * with large caches. | |
8272 | * | |
8273 | * The implementation works fairly simply by integrating the following two | |
8274 | * modifications: | |
8275 | * | |
8276 | * *) When writing to the L2ARC, we occasionally write a "l2arc log block", | |
8277 | * which is an additional piece of metadata which describes what's been | |
8278 | * written. This allows us to rebuild the arc_buf_hdr_t structures of the | |
8279 | * main ARC buffers. There are 2 linked-lists of log blocks headed by | |
8280 | * dh_start_lbps[2]. We alternate which chain we append to, so they are | |
8281 | * time-wise and offset-wise interleaved, but that is an optimization rather | |
8282 | * than for correctness. The log block also includes a pointer to the | |
8283 | * previous block in its chain. | |
8284 | * | |
8285 | * *) We reserve SPA_MINBLOCKSIZE of space at the start of each L2ARC device | |
8286 | * for our header bookkeeping purposes. This contains a device header, | |
8287 | * which contains our top-level reference structures. We update it each | |
8288 | * time we write a new log block, so that we're able to locate it in the | |
8289 | * L2ARC device. If this write results in an inconsistent device header | |
8290 | * (e.g. due to power failure), we detect this by verifying the header's | |
8291 | * checksum and simply fail to reconstruct the L2ARC after reboot. | |
8292 | * | |
8293 | * Implementation diagram: | |
8294 | * | |
8295 | * +=== L2ARC device (not to scale) ======================================+ | |
8296 | * | ___two newest log block pointers__.__________ | | |
8297 | * | / \dh_start_lbps[1] | | |
8298 | * | / \ \dh_start_lbps[0]| | |
8299 | * |.___/__. V V | | |
8300 | * ||L2 dev|....|lb |bufs |lb |bufs |lb |bufs |lb |bufs |lb |---(empty)---| | |
8301 | * || hdr| ^ /^ /^ / / | | |
8302 | * |+------+ ...--\-------/ \-----/--\------/ / | | |
8303 | * | \--------------/ \--------------/ | | |
8304 | * +======================================================================+ | |
8305 | * | |
8306 | * As can be seen on the diagram, rather than using a simple linked list, | |
8307 | * we use a pair of linked lists with alternating elements. This is a | |
8308 | * performance enhancement due to the fact that we only find out the | |
8309 | * address of the next log block access once the current block has been | |
8310 | * completely read in. Obviously, this hurts performance, because we'd be | |
8311 | * keeping the device's I/O queue at only a 1 operation deep, thus | |
8312 | * incurring a large amount of I/O round-trip latency. Having two lists | |
8313 | * allows us to fetch two log blocks ahead of where we are currently | |
8314 | * rebuilding L2ARC buffers. | |
8315 | * | |
8316 | * On-device data structures: | |
8317 | * | |
8318 | * L2ARC device header: l2arc_dev_hdr_phys_t | |
8319 | * L2ARC log block: l2arc_log_blk_phys_t | |
8320 | * | |
8321 | * L2ARC reconstruction: | |
8322 | * | |
8323 | * When writing data, we simply write in the standard rotary fashion, | |
8324 | * evicting buffers as we go and simply writing new data over them (writing | |
8325 | * a new log block every now and then). This obviously means that once we | |
8326 | * loop around the end of the device, we will start cutting into an already | |
8327 | * committed log block (and its referenced data buffers), like so: | |
8328 | * | |
8329 | * current write head__ __old tail | |
8330 | * \ / | |
8331 | * V V | |
8332 | * <--|bufs |lb |bufs |lb | |bufs |lb |bufs |lb |--> | |
8333 | * ^ ^^^^^^^^^___________________________________ | |
8334 | * | \ | |
8335 | * <<nextwrite>> may overwrite this blk and/or its bufs --' | |
8336 | * | |
8337 | * When importing the pool, we detect this situation and use it to stop | |
8338 | * our scanning process (see l2arc_rebuild). | |
8339 | * | |
8340 | * There is one significant caveat to consider when rebuilding ARC contents | |
8341 | * from an L2ARC device: what about invalidated buffers? Given the above | |
8342 | * construction, we cannot update blocks which we've already written to amend | |
8343 | * them to remove buffers which were invalidated. Thus, during reconstruction, | |
8344 | * we might be populating the cache with buffers for data that's not on the | |
8345 | * main pool anymore, or may have been overwritten! | |
8346 | * | |
8347 | * As it turns out, this isn't a problem. Every arc_read request includes | |
8348 | * both the DVA and, crucially, the birth TXG of the BP the caller is | |
8349 | * looking for. So even if the cache were populated by completely rotten | |
8350 | * blocks for data that had been long deleted and/or overwritten, we'll | |
8351 | * never actually return bad data from the cache, since the DVA with the | |
8352 | * birth TXG uniquely identify a block in space and time - once created, | |
8353 | * a block is immutable on disk. The worst thing we have done is wasted | |
8354 | * some time and memory at l2arc rebuild to reconstruct outdated ARC | |
8355 | * entries that will get dropped from the l2arc as it is being updated | |
8356 | * with new blocks. | |
8357 | * | |
8358 | * L2ARC buffers that have been evicted by l2arc_evict() ahead of the write | |
8359 | * hand are not restored. This is done by saving the offset (in bytes) | |
8360 | * l2arc_evict() has evicted to in the L2ARC device header and taking it | |
8361 | * into account when restoring buffers. | |
34dc7c2f BB |
8362 | */ |
8363 | ||
d164b209 | 8364 | static boolean_t |
2a432414 | 8365 | l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) |
d164b209 BB |
8366 | { |
8367 | /* | |
8368 | * A buffer is *not* eligible for the L2ARC if it: | |
8369 | * 1. belongs to a different spa. | |
428870ff BB |
8370 | * 2. is already cached on the L2ARC. |
8371 | * 3. has an I/O in progress (it may be an incomplete read). | |
8372 | * 4. is flagged not eligible (zfs property). | |
d164b209 | 8373 | */ |
b9541d6b | 8374 | if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) || |
c6f5e9d9 | 8375 | HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr)) |
d164b209 BB |
8376 | return (B_FALSE); |
8377 | ||
8378 | return (B_TRUE); | |
8379 | } | |
8380 | ||
8381 | static uint64_t | |
37c22948 | 8382 | l2arc_write_size(l2arc_dev_t *dev) |
d164b209 | 8383 | { |
b7654bd7 | 8384 | uint64_t size, dev_size, tsize; |
d164b209 | 8385 | |
3a17a7a9 SK |
8386 | /* |
8387 | * Make sure our globals have meaningful values in case the user | |
8388 | * altered them. | |
8389 | */ | |
8390 | size = l2arc_write_max; | |
8391 | if (size == 0) { | |
8392 | cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " | |
8393 | "be greater than zero, resetting it to the default (%d)", | |
8394 | L2ARC_WRITE_SIZE); | |
8395 | size = l2arc_write_max = L2ARC_WRITE_SIZE; | |
8396 | } | |
d164b209 BB |
8397 | |
8398 | if (arc_warm == B_FALSE) | |
3a17a7a9 | 8399 | size += l2arc_write_boost; |
d164b209 | 8400 | |
37c22948 GA |
8401 | /* |
8402 | * Make sure the write size does not exceed the size of the cache | |
8403 | * device. This is important in l2arc_evict(), otherwise infinite | |
8404 | * iteration can occur. | |
8405 | */ | |
8406 | dev_size = dev->l2ad_end - dev->l2ad_start; | |
b7654bd7 GA |
8407 | tsize = size + l2arc_log_blk_overhead(size, dev); |
8408 | if (dev->l2ad_vdev->vdev_has_trim && l2arc_trim_ahead > 0) | |
8409 | tsize += MAX(64 * 1024 * 1024, | |
8410 | (tsize * l2arc_trim_ahead) / 100); | |
8411 | ||
8412 | if (tsize >= dev_size) { | |
37c22948 | 8413 | cmn_err(CE_NOTE, "l2arc_write_max or l2arc_write_boost " |
77f6826b GA |
8414 | "plus the overhead of log blocks (persistent L2ARC, " |
8415 | "%llu bytes) exceeds the size of the cache device " | |
8416 | "(guid %llu), resetting them to the default (%d)", | |
b72611f0 BB |
8417 | (u_longlong_t)l2arc_log_blk_overhead(size, dev), |
8418 | (u_longlong_t)dev->l2ad_vdev->vdev_guid, L2ARC_WRITE_SIZE); | |
37c22948 GA |
8419 | size = l2arc_write_max = l2arc_write_boost = L2ARC_WRITE_SIZE; |
8420 | ||
8421 | if (arc_warm == B_FALSE) | |
8422 | size += l2arc_write_boost; | |
8423 | } | |
8424 | ||
d164b209 BB |
8425 | return (size); |
8426 | ||
8427 | } | |
8428 | ||
8429 | static clock_t | |
8430 | l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) | |
8431 | { | |
428870ff | 8432 | clock_t interval, next, now; |
d164b209 BB |
8433 | |
8434 | /* | |
8435 | * If the ARC lists are busy, increase our write rate; if the | |
8436 | * lists are stale, idle back. This is achieved by checking | |
8437 | * how much we previously wrote - if it was more than half of | |
8438 | * what we wanted, schedule the next write much sooner. | |
8439 | */ | |
8440 | if (l2arc_feed_again && wrote > (wanted / 2)) | |
8441 | interval = (hz * l2arc_feed_min_ms) / 1000; | |
8442 | else | |
8443 | interval = hz * l2arc_feed_secs; | |
8444 | ||
428870ff BB |
8445 | now = ddi_get_lbolt(); |
8446 | next = MAX(now, MIN(now + interval, began + interval)); | |
d164b209 BB |
8447 | |
8448 | return (next); | |
8449 | } | |
8450 | ||
34dc7c2f BB |
8451 | /* |
8452 | * Cycle through L2ARC devices. This is how L2ARC load balances. | |
b128c09f | 8453 | * If a device is returned, this also returns holding the spa config lock. |
34dc7c2f BB |
8454 | */ |
8455 | static l2arc_dev_t * | |
8456 | l2arc_dev_get_next(void) | |
8457 | { | |
b128c09f | 8458 | l2arc_dev_t *first, *next = NULL; |
34dc7c2f | 8459 | |
b128c09f BB |
8460 | /* |
8461 | * Lock out the removal of spas (spa_namespace_lock), then removal | |
8462 | * of cache devices (l2arc_dev_mtx). Once a device has been selected, | |
8463 | * both locks will be dropped and a spa config lock held instead. | |
8464 | */ | |
8465 | mutex_enter(&spa_namespace_lock); | |
8466 | mutex_enter(&l2arc_dev_mtx); | |
8467 | ||
8468 | /* if there are no vdevs, there is nothing to do */ | |
8469 | if (l2arc_ndev == 0) | |
8470 | goto out; | |
8471 | ||
8472 | first = NULL; | |
8473 | next = l2arc_dev_last; | |
8474 | do { | |
8475 | /* loop around the list looking for a non-faulted vdev */ | |
8476 | if (next == NULL) { | |
34dc7c2f | 8477 | next = list_head(l2arc_dev_list); |
b128c09f BB |
8478 | } else { |
8479 | next = list_next(l2arc_dev_list, next); | |
8480 | if (next == NULL) | |
8481 | next = list_head(l2arc_dev_list); | |
8482 | } | |
8483 | ||
8484 | /* if we have come back to the start, bail out */ | |
8485 | if (first == NULL) | |
8486 | first = next; | |
8487 | else if (next == first) | |
8488 | break; | |
8489 | ||
b7654bd7 GA |
8490 | } while (vdev_is_dead(next->l2ad_vdev) || next->l2ad_rebuild || |
8491 | next->l2ad_trim_all); | |
b128c09f BB |
8492 | |
8493 | /* if we were unable to find any usable vdevs, return NULL */ | |
b7654bd7 GA |
8494 | if (vdev_is_dead(next->l2ad_vdev) || next->l2ad_rebuild || |
8495 | next->l2ad_trim_all) | |
b128c09f | 8496 | next = NULL; |
34dc7c2f BB |
8497 | |
8498 | l2arc_dev_last = next; | |
8499 | ||
b128c09f BB |
8500 | out: |
8501 | mutex_exit(&l2arc_dev_mtx); | |
8502 | ||
8503 | /* | |
8504 | * Grab the config lock to prevent the 'next' device from being | |
8505 | * removed while we are writing to it. | |
8506 | */ | |
8507 | if (next != NULL) | |
8508 | spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); | |
8509 | mutex_exit(&spa_namespace_lock); | |
8510 | ||
34dc7c2f BB |
8511 | return (next); |
8512 | } | |
8513 | ||
b128c09f BB |
8514 | /* |
8515 | * Free buffers that were tagged for destruction. | |
8516 | */ | |
8517 | static void | |
0bc8fd78 | 8518 | l2arc_do_free_on_write(void) |
b128c09f BB |
8519 | { |
8520 | list_t *buflist; | |
8521 | l2arc_data_free_t *df, *df_prev; | |
8522 | ||
8523 | mutex_enter(&l2arc_free_on_write_mtx); | |
8524 | buflist = l2arc_free_on_write; | |
8525 | ||
8526 | for (df = list_tail(buflist); df; df = df_prev) { | |
8527 | df_prev = list_prev(buflist, df); | |
a6255b7f DQ |
8528 | ASSERT3P(df->l2df_abd, !=, NULL); |
8529 | abd_free(df->l2df_abd); | |
b128c09f BB |
8530 | list_remove(buflist, df); |
8531 | kmem_free(df, sizeof (l2arc_data_free_t)); | |
8532 | } | |
8533 | ||
8534 | mutex_exit(&l2arc_free_on_write_mtx); | |
8535 | } | |
8536 | ||
34dc7c2f BB |
8537 | /* |
8538 | * A write to a cache device has completed. Update all headers to allow | |
8539 | * reads from these buffers to begin. | |
8540 | */ | |
8541 | static void | |
8542 | l2arc_write_done(zio_t *zio) | |
8543 | { | |
77f6826b GA |
8544 | l2arc_write_callback_t *cb; |
8545 | l2arc_lb_abd_buf_t *abd_buf; | |
8546 | l2arc_lb_ptr_buf_t *lb_ptr_buf; | |
8547 | l2arc_dev_t *dev; | |
657fd33b | 8548 | l2arc_dev_hdr_phys_t *l2dhdr; |
77f6826b GA |
8549 | list_t *buflist; |
8550 | arc_buf_hdr_t *head, *hdr, *hdr_prev; | |
8551 | kmutex_t *hash_lock; | |
8552 | int64_t bytes_dropped = 0; | |
34dc7c2f BB |
8553 | |
8554 | cb = zio->io_private; | |
d3c2ae1c | 8555 | ASSERT3P(cb, !=, NULL); |
34dc7c2f | 8556 | dev = cb->l2wcb_dev; |
657fd33b | 8557 | l2dhdr = dev->l2ad_dev_hdr; |
d3c2ae1c | 8558 | ASSERT3P(dev, !=, NULL); |
34dc7c2f | 8559 | head = cb->l2wcb_head; |
d3c2ae1c | 8560 | ASSERT3P(head, !=, NULL); |
b9541d6b | 8561 | buflist = &dev->l2ad_buflist; |
d3c2ae1c | 8562 | ASSERT3P(buflist, !=, NULL); |
34dc7c2f BB |
8563 | DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, |
8564 | l2arc_write_callback_t *, cb); | |
8565 | ||
34dc7c2f BB |
8566 | /* |
8567 | * All writes completed, or an error was hit. | |
8568 | */ | |
ca0bf58d PS |
8569 | top: |
8570 | mutex_enter(&dev->l2ad_mtx); | |
2a432414 GW |
8571 | for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { |
8572 | hdr_prev = list_prev(buflist, hdr); | |
34dc7c2f | 8573 | |
2a432414 | 8574 | hash_lock = HDR_LOCK(hdr); |
ca0bf58d PS |
8575 | |
8576 | /* | |
8577 | * We cannot use mutex_enter or else we can deadlock | |
8578 | * with l2arc_write_buffers (due to swapping the order | |
8579 | * the hash lock and l2ad_mtx are taken). | |
8580 | */ | |
34dc7c2f BB |
8581 | if (!mutex_tryenter(hash_lock)) { |
8582 | /* | |
ca0bf58d PS |
8583 | * Missed the hash lock. We must retry so we |
8584 | * don't leave the ARC_FLAG_L2_WRITING bit set. | |
34dc7c2f | 8585 | */ |
ca0bf58d PS |
8586 | ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); |
8587 | ||
8588 | /* | |
8589 | * We don't want to rescan the headers we've | |
8590 | * already marked as having been written out, so | |
8591 | * we reinsert the head node so we can pick up | |
8592 | * where we left off. | |
8593 | */ | |
8594 | list_remove(buflist, head); | |
8595 | list_insert_after(buflist, hdr, head); | |
8596 | ||
8597 | mutex_exit(&dev->l2ad_mtx); | |
8598 | ||
8599 | /* | |
8600 | * We wait for the hash lock to become available | |
8601 | * to try and prevent busy waiting, and increase | |
8602 | * the chance we'll be able to acquire the lock | |
8603 | * the next time around. | |
8604 | */ | |
8605 | mutex_enter(hash_lock); | |
8606 | mutex_exit(hash_lock); | |
8607 | goto top; | |
34dc7c2f BB |
8608 | } |
8609 | ||
b9541d6b | 8610 | /* |
ca0bf58d PS |
8611 | * We could not have been moved into the arc_l2c_only |
8612 | * state while in-flight due to our ARC_FLAG_L2_WRITING | |
8613 | * bit being set. Let's just ensure that's being enforced. | |
8614 | */ | |
8615 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
8616 | ||
8a09d5fd BB |
8617 | /* |
8618 | * Skipped - drop L2ARC entry and mark the header as no | |
8619 | * longer L2 eligibile. | |
8620 | */ | |
d3c2ae1c | 8621 | if (zio->io_error != 0) { |
34dc7c2f | 8622 | /* |
b128c09f | 8623 | * Error - drop L2ARC entry. |
34dc7c2f | 8624 | */ |
2a432414 | 8625 | list_remove(buflist, hdr); |
d3c2ae1c | 8626 | arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); |
b9541d6b | 8627 | |
7558997d | 8628 | uint64_t psize = HDR_GET_PSIZE(hdr); |
08532162 | 8629 | l2arc_hdr_arcstats_decrement(hdr); |
d962d5da | 8630 | |
7558997d SD |
8631 | bytes_dropped += |
8632 | vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
424fd7c3 | 8633 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, |
d3c2ae1c | 8634 | arc_hdr_size(hdr), hdr); |
34dc7c2f BB |
8635 | } |
8636 | ||
8637 | /* | |
ca0bf58d PS |
8638 | * Allow ARC to begin reads and ghost list evictions to |
8639 | * this L2ARC entry. | |
34dc7c2f | 8640 | */ |
d3c2ae1c | 8641 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING); |
34dc7c2f BB |
8642 | |
8643 | mutex_exit(hash_lock); | |
8644 | } | |
8645 | ||
77f6826b GA |
8646 | /* |
8647 | * Free the allocated abd buffers for writing the log blocks. | |
8648 | * If the zio failed reclaim the allocated space and remove the | |
8649 | * pointers to these log blocks from the log block pointer list | |
8650 | * of the L2ARC device. | |
8651 | */ | |
8652 | while ((abd_buf = list_remove_tail(&cb->l2wcb_abd_list)) != NULL) { | |
8653 | abd_free(abd_buf->abd); | |
8654 | zio_buf_free(abd_buf, sizeof (*abd_buf)); | |
8655 | if (zio->io_error != 0) { | |
8656 | lb_ptr_buf = list_remove_head(&dev->l2ad_lbptr_list); | |
657fd33b GA |
8657 | /* |
8658 | * L2BLK_GET_PSIZE returns aligned size for log | |
8659 | * blocks. | |
8660 | */ | |
8661 | uint64_t asize = | |
77f6826b | 8662 | L2BLK_GET_PSIZE((lb_ptr_buf->lb_ptr)->lbp_prop); |
657fd33b GA |
8663 | bytes_dropped += asize; |
8664 | ARCSTAT_INCR(arcstat_l2_log_blk_asize, -asize); | |
8665 | ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count); | |
8666 | zfs_refcount_remove_many(&dev->l2ad_lb_asize, asize, | |
8667 | lb_ptr_buf); | |
8668 | zfs_refcount_remove(&dev->l2ad_lb_count, lb_ptr_buf); | |
77f6826b GA |
8669 | kmem_free(lb_ptr_buf->lb_ptr, |
8670 | sizeof (l2arc_log_blkptr_t)); | |
8671 | kmem_free(lb_ptr_buf, sizeof (l2arc_lb_ptr_buf_t)); | |
8672 | } | |
8673 | } | |
8674 | list_destroy(&cb->l2wcb_abd_list); | |
8675 | ||
657fd33b | 8676 | if (zio->io_error != 0) { |
08532162 GA |
8677 | ARCSTAT_BUMP(arcstat_l2_writes_error); |
8678 | ||
2054f35e GA |
8679 | /* |
8680 | * Restore the lbps array in the header to its previous state. | |
8681 | * If the list of log block pointers is empty, zero out the | |
8682 | * log block pointers in the device header. | |
8683 | */ | |
657fd33b GA |
8684 | lb_ptr_buf = list_head(&dev->l2ad_lbptr_list); |
8685 | for (int i = 0; i < 2; i++) { | |
2054f35e GA |
8686 | if (lb_ptr_buf == NULL) { |
8687 | /* | |
8688 | * If the list is empty zero out the device | |
8689 | * header. Otherwise zero out the second log | |
8690 | * block pointer in the header. | |
8691 | */ | |
8692 | if (i == 0) { | |
8693 | bzero(l2dhdr, dev->l2ad_dev_hdr_asize); | |
8694 | } else { | |
8695 | bzero(&l2dhdr->dh_start_lbps[i], | |
8696 | sizeof (l2arc_log_blkptr_t)); | |
8697 | } | |
8698 | break; | |
8699 | } | |
657fd33b GA |
8700 | bcopy(lb_ptr_buf->lb_ptr, &l2dhdr->dh_start_lbps[i], |
8701 | sizeof (l2arc_log_blkptr_t)); | |
8702 | lb_ptr_buf = list_next(&dev->l2ad_lbptr_list, | |
8703 | lb_ptr_buf); | |
8704 | } | |
8705 | } | |
8706 | ||
c4c162c1 | 8707 | ARCSTAT_BUMP(arcstat_l2_writes_done); |
34dc7c2f | 8708 | list_remove(buflist, head); |
b9541d6b CW |
8709 | ASSERT(!HDR_HAS_L1HDR(head)); |
8710 | kmem_cache_free(hdr_l2only_cache, head); | |
8711 | mutex_exit(&dev->l2ad_mtx); | |
34dc7c2f | 8712 | |
77f6826b | 8713 | ASSERT(dev->l2ad_vdev != NULL); |
3bec585e SK |
8714 | vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); |
8715 | ||
b128c09f | 8716 | l2arc_do_free_on_write(); |
34dc7c2f BB |
8717 | |
8718 | kmem_free(cb, sizeof (l2arc_write_callback_t)); | |
8719 | } | |
8720 | ||
b5256303 TC |
8721 | static int |
8722 | l2arc_untransform(zio_t *zio, l2arc_read_callback_t *cb) | |
8723 | { | |
8724 | int ret; | |
8725 | spa_t *spa = zio->io_spa; | |
8726 | arc_buf_hdr_t *hdr = cb->l2rcb_hdr; | |
8727 | blkptr_t *bp = zio->io_bp; | |
b5256303 TC |
8728 | uint8_t salt[ZIO_DATA_SALT_LEN]; |
8729 | uint8_t iv[ZIO_DATA_IV_LEN]; | |
8730 | uint8_t mac[ZIO_DATA_MAC_LEN]; | |
8731 | boolean_t no_crypt = B_FALSE; | |
8732 | ||
8733 | /* | |
8734 | * ZIL data is never be written to the L2ARC, so we don't need | |
8735 | * special handling for its unique MAC storage. | |
8736 | */ | |
8737 | ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG); | |
8738 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); | |
440a3eb9 | 8739 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
b5256303 | 8740 | |
440a3eb9 TC |
8741 | /* |
8742 | * If the data was encrypted, decrypt it now. Note that | |
8743 | * we must check the bp here and not the hdr, since the | |
8744 | * hdr does not have its encryption parameters updated | |
8745 | * until arc_read_done(). | |
8746 | */ | |
8747 | if (BP_IS_ENCRYPTED(bp)) { | |
e111c802 | 8748 | abd_t *eabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, |
6b88b4b5 | 8749 | ARC_HDR_DO_ADAPT | ARC_HDR_USE_RESERVE); |
b5256303 TC |
8750 | |
8751 | zio_crypt_decode_params_bp(bp, salt, iv); | |
8752 | zio_crypt_decode_mac_bp(bp, mac); | |
8753 | ||
be9a5c35 TC |
8754 | ret = spa_do_crypt_abd(B_FALSE, spa, &cb->l2rcb_zb, |
8755 | BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), | |
8756 | salt, iv, mac, HDR_GET_PSIZE(hdr), eabd, | |
8757 | hdr->b_l1hdr.b_pabd, &no_crypt); | |
b5256303 TC |
8758 | if (ret != 0) { |
8759 | arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr); | |
b5256303 TC |
8760 | goto error; |
8761 | } | |
8762 | ||
b5256303 TC |
8763 | /* |
8764 | * If we actually performed decryption, replace b_pabd | |
8765 | * with the decrypted data. Otherwise we can just throw | |
8766 | * our decryption buffer away. | |
8767 | */ | |
8768 | if (!no_crypt) { | |
8769 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
8770 | arc_hdr_size(hdr), hdr); | |
8771 | hdr->b_l1hdr.b_pabd = eabd; | |
8772 | zio->io_abd = eabd; | |
8773 | } else { | |
8774 | arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr); | |
8775 | } | |
8776 | } | |
8777 | ||
8778 | /* | |
8779 | * If the L2ARC block was compressed, but ARC compression | |
8780 | * is disabled we decompress the data into a new buffer and | |
8781 | * replace the existing data. | |
8782 | */ | |
8783 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
8784 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
e111c802 | 8785 | abd_t *cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, |
6b88b4b5 | 8786 | ARC_HDR_DO_ADAPT | ARC_HDR_USE_RESERVE); |
b5256303 TC |
8787 | void *tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr)); |
8788 | ||
8789 | ret = zio_decompress_data(HDR_GET_COMPRESS(hdr), | |
8790 | hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr), | |
10b3c7f5 | 8791 | HDR_GET_LSIZE(hdr), &hdr->b_complevel); |
b5256303 TC |
8792 | if (ret != 0) { |
8793 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
8794 | arc_free_data_abd(hdr, cabd, arc_hdr_size(hdr), hdr); | |
8795 | goto error; | |
8796 | } | |
8797 | ||
8798 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
8799 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
8800 | arc_hdr_size(hdr), hdr); | |
8801 | hdr->b_l1hdr.b_pabd = cabd; | |
8802 | zio->io_abd = cabd; | |
8803 | zio->io_size = HDR_GET_LSIZE(hdr); | |
8804 | } | |
8805 | ||
8806 | return (0); | |
8807 | ||
8808 | error: | |
8809 | return (ret); | |
8810 | } | |
8811 | ||
8812 | ||
34dc7c2f BB |
8813 | /* |
8814 | * A read to a cache device completed. Validate buffer contents before | |
8815 | * handing over to the regular ARC routines. | |
8816 | */ | |
8817 | static void | |
8818 | l2arc_read_done(zio_t *zio) | |
8819 | { | |
b5256303 | 8820 | int tfm_error = 0; |
b405837a | 8821 | l2arc_read_callback_t *cb = zio->io_private; |
34dc7c2f | 8822 | arc_buf_hdr_t *hdr; |
34dc7c2f | 8823 | kmutex_t *hash_lock; |
b405837a TC |
8824 | boolean_t valid_cksum; |
8825 | boolean_t using_rdata = (BP_IS_ENCRYPTED(&cb->l2rcb_bp) && | |
8826 | (cb->l2rcb_flags & ZIO_FLAG_RAW_ENCRYPT)); | |
b128c09f | 8827 | |
d3c2ae1c | 8828 | ASSERT3P(zio->io_vd, !=, NULL); |
b128c09f BB |
8829 | ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); |
8830 | ||
8831 | spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); | |
34dc7c2f | 8832 | |
d3c2ae1c GW |
8833 | ASSERT3P(cb, !=, NULL); |
8834 | hdr = cb->l2rcb_hdr; | |
8835 | ASSERT3P(hdr, !=, NULL); | |
34dc7c2f | 8836 | |
d3c2ae1c | 8837 | hash_lock = HDR_LOCK(hdr); |
34dc7c2f | 8838 | mutex_enter(hash_lock); |
428870ff | 8839 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
34dc7c2f | 8840 | |
82710e99 GDN |
8841 | /* |
8842 | * If the data was read into a temporary buffer, | |
8843 | * move it and free the buffer. | |
8844 | */ | |
8845 | if (cb->l2rcb_abd != NULL) { | |
8846 | ASSERT3U(arc_hdr_size(hdr), <, zio->io_size); | |
8847 | if (zio->io_error == 0) { | |
b405837a TC |
8848 | if (using_rdata) { |
8849 | abd_copy(hdr->b_crypt_hdr.b_rabd, | |
8850 | cb->l2rcb_abd, arc_hdr_size(hdr)); | |
8851 | } else { | |
8852 | abd_copy(hdr->b_l1hdr.b_pabd, | |
8853 | cb->l2rcb_abd, arc_hdr_size(hdr)); | |
8854 | } | |
82710e99 GDN |
8855 | } |
8856 | ||
8857 | /* | |
8858 | * The following must be done regardless of whether | |
8859 | * there was an error: | |
8860 | * - free the temporary buffer | |
8861 | * - point zio to the real ARC buffer | |
8862 | * - set zio size accordingly | |
8863 | * These are required because zio is either re-used for | |
8864 | * an I/O of the block in the case of the error | |
8865 | * or the zio is passed to arc_read_done() and it | |
8866 | * needs real data. | |
8867 | */ | |
8868 | abd_free(cb->l2rcb_abd); | |
8869 | zio->io_size = zio->io_orig_size = arc_hdr_size(hdr); | |
440a3eb9 | 8870 | |
b405837a | 8871 | if (using_rdata) { |
440a3eb9 TC |
8872 | ASSERT(HDR_HAS_RABD(hdr)); |
8873 | zio->io_abd = zio->io_orig_abd = | |
8874 | hdr->b_crypt_hdr.b_rabd; | |
8875 | } else { | |
8876 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
8877 | zio->io_abd = zio->io_orig_abd = hdr->b_l1hdr.b_pabd; | |
8878 | } | |
82710e99 GDN |
8879 | } |
8880 | ||
a6255b7f | 8881 | ASSERT3P(zio->io_abd, !=, NULL); |
3a17a7a9 | 8882 | |
34dc7c2f BB |
8883 | /* |
8884 | * Check this survived the L2ARC journey. | |
8885 | */ | |
b5256303 TC |
8886 | ASSERT(zio->io_abd == hdr->b_l1hdr.b_pabd || |
8887 | (HDR_HAS_RABD(hdr) && zio->io_abd == hdr->b_crypt_hdr.b_rabd)); | |
d3c2ae1c GW |
8888 | zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ |
8889 | zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ | |
10b3c7f5 | 8890 | zio->io_prop.zp_complevel = hdr->b_complevel; |
d3c2ae1c GW |
8891 | |
8892 | valid_cksum = arc_cksum_is_equal(hdr, zio); | |
b5256303 TC |
8893 | |
8894 | /* | |
8895 | * b_rabd will always match the data as it exists on disk if it is | |
8896 | * being used. Therefore if we are reading into b_rabd we do not | |
8897 | * attempt to untransform the data. | |
8898 | */ | |
8899 | if (valid_cksum && !using_rdata) | |
8900 | tfm_error = l2arc_untransform(zio, cb); | |
8901 | ||
8902 | if (valid_cksum && tfm_error == 0 && zio->io_error == 0 && | |
8903 | !HDR_L2_EVICTED(hdr)) { | |
34dc7c2f | 8904 | mutex_exit(hash_lock); |
d3c2ae1c | 8905 | zio->io_private = hdr; |
34dc7c2f BB |
8906 | arc_read_done(zio); |
8907 | } else { | |
34dc7c2f BB |
8908 | /* |
8909 | * Buffer didn't survive caching. Increment stats and | |
8910 | * reissue to the original storage device. | |
8911 | */ | |
b128c09f | 8912 | if (zio->io_error != 0) { |
34dc7c2f | 8913 | ARCSTAT_BUMP(arcstat_l2_io_error); |
b128c09f | 8914 | } else { |
2e528b49 | 8915 | zio->io_error = SET_ERROR(EIO); |
b128c09f | 8916 | } |
b5256303 | 8917 | if (!valid_cksum || tfm_error != 0) |
34dc7c2f BB |
8918 | ARCSTAT_BUMP(arcstat_l2_cksum_bad); |
8919 | ||
34dc7c2f | 8920 | /* |
b128c09f BB |
8921 | * If there's no waiter, issue an async i/o to the primary |
8922 | * storage now. If there *is* a waiter, the caller must | |
8923 | * issue the i/o in a context where it's OK to block. | |
34dc7c2f | 8924 | */ |
d164b209 BB |
8925 | if (zio->io_waiter == NULL) { |
8926 | zio_t *pio = zio_unique_parent(zio); | |
b5256303 TC |
8927 | void *abd = (using_rdata) ? |
8928 | hdr->b_crypt_hdr.b_rabd : hdr->b_l1hdr.b_pabd; | |
d164b209 BB |
8929 | |
8930 | ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); | |
8931 | ||
5ff2249f | 8932 | zio = zio_read(pio, zio->io_spa, zio->io_bp, |
b5256303 | 8933 | abd, zio->io_size, arc_read_done, |
d3c2ae1c | 8934 | hdr, zio->io_priority, cb->l2rcb_flags, |
5ff2249f AM |
8935 | &cb->l2rcb_zb); |
8936 | ||
8937 | /* | |
8938 | * Original ZIO will be freed, so we need to update | |
8939 | * ARC header with the new ZIO pointer to be used | |
8940 | * by zio_change_priority() in arc_read(). | |
8941 | */ | |
8942 | for (struct arc_callback *acb = hdr->b_l1hdr.b_acb; | |
8943 | acb != NULL; acb = acb->acb_next) | |
8944 | acb->acb_zio_head = zio; | |
8945 | ||
8946 | mutex_exit(hash_lock); | |
8947 | zio_nowait(zio); | |
8948 | } else { | |
8949 | mutex_exit(hash_lock); | |
d164b209 | 8950 | } |
34dc7c2f BB |
8951 | } |
8952 | ||
8953 | kmem_free(cb, sizeof (l2arc_read_callback_t)); | |
8954 | } | |
8955 | ||
8956 | /* | |
8957 | * This is the list priority from which the L2ARC will search for pages to | |
8958 | * cache. This is used within loops (0..3) to cycle through lists in the | |
8959 | * desired order. This order can have a significant effect on cache | |
8960 | * performance. | |
8961 | * | |
8962 | * Currently the metadata lists are hit first, MFU then MRU, followed by | |
8963 | * the data lists. This function returns a locked list, and also returns | |
8964 | * the lock pointer. | |
8965 | */ | |
ca0bf58d PS |
8966 | static multilist_sublist_t * |
8967 | l2arc_sublist_lock(int list_num) | |
34dc7c2f | 8968 | { |
ca0bf58d PS |
8969 | multilist_t *ml = NULL; |
8970 | unsigned int idx; | |
34dc7c2f | 8971 | |
4aafab91 | 8972 | ASSERT(list_num >= 0 && list_num < L2ARC_FEED_TYPES); |
34dc7c2f BB |
8973 | |
8974 | switch (list_num) { | |
8975 | case 0: | |
ffdf019c | 8976 | ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; |
34dc7c2f BB |
8977 | break; |
8978 | case 1: | |
ffdf019c | 8979 | ml = &arc_mru->arcs_list[ARC_BUFC_METADATA]; |
34dc7c2f BB |
8980 | break; |
8981 | case 2: | |
ffdf019c | 8982 | ml = &arc_mfu->arcs_list[ARC_BUFC_DATA]; |
34dc7c2f BB |
8983 | break; |
8984 | case 3: | |
ffdf019c | 8985 | ml = &arc_mru->arcs_list[ARC_BUFC_DATA]; |
34dc7c2f | 8986 | break; |
4aafab91 G |
8987 | default: |
8988 | return (NULL); | |
34dc7c2f BB |
8989 | } |
8990 | ||
ca0bf58d PS |
8991 | /* |
8992 | * Return a randomly-selected sublist. This is acceptable | |
8993 | * because the caller feeds only a little bit of data for each | |
8994 | * call (8MB). Subsequent calls will result in different | |
8995 | * sublists being selected. | |
8996 | */ | |
8997 | idx = multilist_get_random_index(ml); | |
8998 | return (multilist_sublist_lock(ml, idx)); | |
34dc7c2f BB |
8999 | } |
9000 | ||
77f6826b GA |
9001 | /* |
9002 | * Calculates the maximum overhead of L2ARC metadata log blocks for a given | |
657fd33b | 9003 | * L2ARC write size. l2arc_evict and l2arc_write_size need to include this |
77f6826b GA |
9004 | * overhead in processing to make sure there is enough headroom available |
9005 | * when writing buffers. | |
9006 | */ | |
9007 | static inline uint64_t | |
9008 | l2arc_log_blk_overhead(uint64_t write_sz, l2arc_dev_t *dev) | |
9009 | { | |
657fd33b | 9010 | if (dev->l2ad_log_entries == 0) { |
77f6826b GA |
9011 | return (0); |
9012 | } else { | |
9013 | uint64_t log_entries = write_sz >> SPA_MINBLOCKSHIFT; | |
9014 | ||
9015 | uint64_t log_blocks = (log_entries + | |
657fd33b GA |
9016 | dev->l2ad_log_entries - 1) / |
9017 | dev->l2ad_log_entries; | |
77f6826b GA |
9018 | |
9019 | return (vdev_psize_to_asize(dev->l2ad_vdev, | |
9020 | sizeof (l2arc_log_blk_phys_t)) * log_blocks); | |
9021 | } | |
9022 | } | |
9023 | ||
34dc7c2f BB |
9024 | /* |
9025 | * Evict buffers from the device write hand to the distance specified in | |
77f6826b | 9026 | * bytes. This distance may span populated buffers, it may span nothing. |
34dc7c2f BB |
9027 | * This is clearing a region on the L2ARC device ready for writing. |
9028 | * If the 'all' boolean is set, every buffer is evicted. | |
9029 | */ | |
9030 | static void | |
9031 | l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) | |
9032 | { | |
9033 | list_t *buflist; | |
2a432414 | 9034 | arc_buf_hdr_t *hdr, *hdr_prev; |
34dc7c2f BB |
9035 | kmutex_t *hash_lock; |
9036 | uint64_t taddr; | |
77f6826b | 9037 | l2arc_lb_ptr_buf_t *lb_ptr_buf, *lb_ptr_buf_prev; |
b7654bd7 GA |
9038 | vdev_t *vd = dev->l2ad_vdev; |
9039 | boolean_t rerun; | |
34dc7c2f | 9040 | |
b9541d6b | 9041 | buflist = &dev->l2ad_buflist; |
34dc7c2f | 9042 | |
77f6826b GA |
9043 | /* |
9044 | * We need to add in the worst case scenario of log block overhead. | |
9045 | */ | |
9046 | distance += l2arc_log_blk_overhead(distance, dev); | |
b7654bd7 GA |
9047 | if (vd->vdev_has_trim && l2arc_trim_ahead > 0) { |
9048 | /* | |
9049 | * Trim ahead of the write size 64MB or (l2arc_trim_ahead/100) | |
9050 | * times the write size, whichever is greater. | |
9051 | */ | |
9052 | distance += MAX(64 * 1024 * 1024, | |
9053 | (distance * l2arc_trim_ahead) / 100); | |
9054 | } | |
77f6826b | 9055 | |
37c22948 GA |
9056 | top: |
9057 | rerun = B_FALSE; | |
9058 | if (dev->l2ad_hand >= (dev->l2ad_end - distance)) { | |
34dc7c2f | 9059 | /* |
dd4bc569 | 9060 | * When there is no space to accommodate upcoming writes, |
77f6826b GA |
9061 | * evict to the end. Then bump the write and evict hands |
9062 | * to the start and iterate. This iteration does not | |
9063 | * happen indefinitely as we make sure in | |
9064 | * l2arc_write_size() that when the write hand is reset, | |
9065 | * the write size does not exceed the end of the device. | |
34dc7c2f | 9066 | */ |
37c22948 | 9067 | rerun = B_TRUE; |
34dc7c2f BB |
9068 | taddr = dev->l2ad_end; |
9069 | } else { | |
9070 | taddr = dev->l2ad_hand + distance; | |
9071 | } | |
9072 | DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, | |
9073 | uint64_t, taddr, boolean_t, all); | |
9074 | ||
b7654bd7 | 9075 | if (!all) { |
37c22948 | 9076 | /* |
b7654bd7 GA |
9077 | * This check has to be placed after deciding whether to |
9078 | * iterate (rerun). | |
37c22948 | 9079 | */ |
b7654bd7 GA |
9080 | if (dev->l2ad_first) { |
9081 | /* | |
9082 | * This is the first sweep through the device. There is | |
9083 | * nothing to evict. We have already trimmmed the | |
9084 | * whole device. | |
9085 | */ | |
9086 | goto out; | |
9087 | } else { | |
9088 | /* | |
9089 | * Trim the space to be evicted. | |
9090 | */ | |
9091 | if (vd->vdev_has_trim && dev->l2ad_evict < taddr && | |
9092 | l2arc_trim_ahead > 0) { | |
9093 | /* | |
9094 | * We have to drop the spa_config lock because | |
9095 | * vdev_trim_range() will acquire it. | |
9096 | * l2ad_evict already accounts for the label | |
9097 | * size. To prevent vdev_trim_ranges() from | |
9098 | * adding it again, we subtract it from | |
9099 | * l2ad_evict. | |
9100 | */ | |
9101 | spa_config_exit(dev->l2ad_spa, SCL_L2ARC, dev); | |
9102 | vdev_trim_simple(vd, | |
9103 | dev->l2ad_evict - VDEV_LABEL_START_SIZE, | |
9104 | taddr - dev->l2ad_evict); | |
9105 | spa_config_enter(dev->l2ad_spa, SCL_L2ARC, dev, | |
9106 | RW_READER); | |
9107 | } | |
37c22948 | 9108 | |
b7654bd7 GA |
9109 | /* |
9110 | * When rebuilding L2ARC we retrieve the evict hand | |
9111 | * from the header of the device. Of note, l2arc_evict() | |
9112 | * does not actually delete buffers from the cache | |
9113 | * device, but trimming may do so depending on the | |
9114 | * hardware implementation. Thus keeping track of the | |
9115 | * evict hand is useful. | |
9116 | */ | |
9117 | dev->l2ad_evict = MAX(dev->l2ad_evict, taddr); | |
9118 | } | |
9119 | } | |
77f6826b | 9120 | |
37c22948 | 9121 | retry: |
b9541d6b | 9122 | mutex_enter(&dev->l2ad_mtx); |
77f6826b GA |
9123 | /* |
9124 | * We have to account for evicted log blocks. Run vdev_space_update() | |
9125 | * on log blocks whose offset (in bytes) is before the evicted offset | |
9126 | * (in bytes) by searching in the list of pointers to log blocks | |
9127 | * present in the L2ARC device. | |
9128 | */ | |
9129 | for (lb_ptr_buf = list_tail(&dev->l2ad_lbptr_list); lb_ptr_buf; | |
9130 | lb_ptr_buf = lb_ptr_buf_prev) { | |
9131 | ||
9132 | lb_ptr_buf_prev = list_prev(&dev->l2ad_lbptr_list, lb_ptr_buf); | |
9133 | ||
657fd33b GA |
9134 | /* L2BLK_GET_PSIZE returns aligned size for log blocks */ |
9135 | uint64_t asize = L2BLK_GET_PSIZE( | |
9136 | (lb_ptr_buf->lb_ptr)->lbp_prop); | |
9137 | ||
77f6826b GA |
9138 | /* |
9139 | * We don't worry about log blocks left behind (ie | |
657fd33b | 9140 | * lbp_payload_start < l2ad_hand) because l2arc_write_buffers() |
77f6826b GA |
9141 | * will never write more than l2arc_evict() evicts. |
9142 | */ | |
9143 | if (!all && l2arc_log_blkptr_valid(dev, lb_ptr_buf->lb_ptr)) { | |
9144 | break; | |
9145 | } else { | |
b7654bd7 | 9146 | vdev_space_update(vd, -asize, 0, 0); |
657fd33b GA |
9147 | ARCSTAT_INCR(arcstat_l2_log_blk_asize, -asize); |
9148 | ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count); | |
9149 | zfs_refcount_remove_many(&dev->l2ad_lb_asize, asize, | |
9150 | lb_ptr_buf); | |
9151 | zfs_refcount_remove(&dev->l2ad_lb_count, lb_ptr_buf); | |
77f6826b GA |
9152 | list_remove(&dev->l2ad_lbptr_list, lb_ptr_buf); |
9153 | kmem_free(lb_ptr_buf->lb_ptr, | |
9154 | sizeof (l2arc_log_blkptr_t)); | |
9155 | kmem_free(lb_ptr_buf, sizeof (l2arc_lb_ptr_buf_t)); | |
9156 | } | |
9157 | } | |
9158 | ||
2a432414 GW |
9159 | for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { |
9160 | hdr_prev = list_prev(buflist, hdr); | |
34dc7c2f | 9161 | |
ca6c7a94 | 9162 | ASSERT(!HDR_EMPTY(hdr)); |
2a432414 | 9163 | hash_lock = HDR_LOCK(hdr); |
ca0bf58d PS |
9164 | |
9165 | /* | |
9166 | * We cannot use mutex_enter or else we can deadlock | |
9167 | * with l2arc_write_buffers (due to swapping the order | |
9168 | * the hash lock and l2ad_mtx are taken). | |
9169 | */ | |
34dc7c2f BB |
9170 | if (!mutex_tryenter(hash_lock)) { |
9171 | /* | |
9172 | * Missed the hash lock. Retry. | |
9173 | */ | |
9174 | ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); | |
b9541d6b | 9175 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
9176 | mutex_enter(hash_lock); |
9177 | mutex_exit(hash_lock); | |
37c22948 | 9178 | goto retry; |
34dc7c2f BB |
9179 | } |
9180 | ||
f06f53fa AG |
9181 | /* |
9182 | * A header can't be on this list if it doesn't have L2 header. | |
9183 | */ | |
9184 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
34dc7c2f | 9185 | |
f06f53fa AG |
9186 | /* Ensure this header has finished being written. */ |
9187 | ASSERT(!HDR_L2_WRITING(hdr)); | |
9188 | ASSERT(!HDR_L2_WRITE_HEAD(hdr)); | |
9189 | ||
77f6826b | 9190 | if (!all && (hdr->b_l2hdr.b_daddr >= dev->l2ad_evict || |
b9541d6b | 9191 | hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { |
34dc7c2f BB |
9192 | /* |
9193 | * We've evicted to the target address, | |
9194 | * or the end of the device. | |
9195 | */ | |
9196 | mutex_exit(hash_lock); | |
9197 | break; | |
9198 | } | |
9199 | ||
b9541d6b | 9200 | if (!HDR_HAS_L1HDR(hdr)) { |
2a432414 | 9201 | ASSERT(!HDR_L2_READING(hdr)); |
34dc7c2f BB |
9202 | /* |
9203 | * This doesn't exist in the ARC. Destroy. | |
9204 | * arc_hdr_destroy() will call list_remove() | |
01850391 | 9205 | * and decrement arcstat_l2_lsize. |
34dc7c2f | 9206 | */ |
2a432414 GW |
9207 | arc_change_state(arc_anon, hdr, hash_lock); |
9208 | arc_hdr_destroy(hdr); | |
34dc7c2f | 9209 | } else { |
b9541d6b CW |
9210 | ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); |
9211 | ARCSTAT_BUMP(arcstat_l2_evict_l1cached); | |
b128c09f BB |
9212 | /* |
9213 | * Invalidate issued or about to be issued | |
9214 | * reads, since we may be about to write | |
9215 | * over this location. | |
9216 | */ | |
2a432414 | 9217 | if (HDR_L2_READING(hdr)) { |
b128c09f | 9218 | ARCSTAT_BUMP(arcstat_l2_evict_reading); |
d3c2ae1c | 9219 | arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED); |
b128c09f BB |
9220 | } |
9221 | ||
d962d5da | 9222 | arc_hdr_l2hdr_destroy(hdr); |
34dc7c2f BB |
9223 | } |
9224 | mutex_exit(hash_lock); | |
9225 | } | |
b9541d6b | 9226 | mutex_exit(&dev->l2ad_mtx); |
37c22948 GA |
9227 | |
9228 | out: | |
77f6826b GA |
9229 | /* |
9230 | * We need to check if we evict all buffers, otherwise we may iterate | |
9231 | * unnecessarily. | |
9232 | */ | |
9233 | if (!all && rerun) { | |
37c22948 GA |
9234 | /* |
9235 | * Bump device hand to the device start if it is approaching the | |
9236 | * end. l2arc_evict() has already evicted ahead for this case. | |
9237 | */ | |
9238 | dev->l2ad_hand = dev->l2ad_start; | |
77f6826b | 9239 | dev->l2ad_evict = dev->l2ad_start; |
37c22948 GA |
9240 | dev->l2ad_first = B_FALSE; |
9241 | goto top; | |
9242 | } | |
657fd33b | 9243 | |
2c210f68 GA |
9244 | if (!all) { |
9245 | /* | |
9246 | * In case of cache device removal (all) the following | |
9247 | * assertions may be violated without functional consequences | |
9248 | * as the device is about to be removed. | |
9249 | */ | |
9250 | ASSERT3U(dev->l2ad_hand + distance, <, dev->l2ad_end); | |
9251 | if (!dev->l2ad_first) | |
9252 | ASSERT3U(dev->l2ad_hand, <, dev->l2ad_evict); | |
9253 | } | |
34dc7c2f BB |
9254 | } |
9255 | ||
b5256303 TC |
9256 | /* |
9257 | * Handle any abd transforms that might be required for writing to the L2ARC. | |
9258 | * If successful, this function will always return an abd with the data | |
9259 | * transformed as it is on disk in a new abd of asize bytes. | |
9260 | */ | |
9261 | static int | |
9262 | l2arc_apply_transforms(spa_t *spa, arc_buf_hdr_t *hdr, uint64_t asize, | |
9263 | abd_t **abd_out) | |
9264 | { | |
9265 | int ret; | |
9266 | void *tmp = NULL; | |
9267 | abd_t *cabd = NULL, *eabd = NULL, *to_write = hdr->b_l1hdr.b_pabd; | |
9268 | enum zio_compress compress = HDR_GET_COMPRESS(hdr); | |
9269 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
9270 | uint64_t size = arc_hdr_size(hdr); | |
9271 | boolean_t ismd = HDR_ISTYPE_METADATA(hdr); | |
9272 | boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
9273 | dsl_crypto_key_t *dck = NULL; | |
9274 | uint8_t mac[ZIO_DATA_MAC_LEN] = { 0 }; | |
4807c0ba | 9275 | boolean_t no_crypt = B_FALSE; |
b5256303 TC |
9276 | |
9277 | ASSERT((HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
9278 | !HDR_COMPRESSION_ENABLED(hdr)) || | |
9279 | HDR_ENCRYPTED(hdr) || HDR_SHARED_DATA(hdr) || psize != asize); | |
9280 | ASSERT3U(psize, <=, asize); | |
9281 | ||
9282 | /* | |
9283 | * If this data simply needs its own buffer, we simply allocate it | |
b7109a41 | 9284 | * and copy the data. This may be done to eliminate a dependency on a |
b5256303 TC |
9285 | * shared buffer or to reallocate the buffer to match asize. |
9286 | */ | |
4807c0ba | 9287 | if (HDR_HAS_RABD(hdr) && asize != psize) { |
10adee27 | 9288 | ASSERT3U(asize, >=, psize); |
4807c0ba | 9289 | to_write = abd_alloc_for_io(asize, ismd); |
10adee27 TC |
9290 | abd_copy(to_write, hdr->b_crypt_hdr.b_rabd, psize); |
9291 | if (psize != asize) | |
9292 | abd_zero_off(to_write, psize, asize - psize); | |
4807c0ba TC |
9293 | goto out; |
9294 | } | |
9295 | ||
b5256303 TC |
9296 | if ((compress == ZIO_COMPRESS_OFF || HDR_COMPRESSION_ENABLED(hdr)) && |
9297 | !HDR_ENCRYPTED(hdr)) { | |
9298 | ASSERT3U(size, ==, psize); | |
9299 | to_write = abd_alloc_for_io(asize, ismd); | |
9300 | abd_copy(to_write, hdr->b_l1hdr.b_pabd, size); | |
9301 | if (size != asize) | |
9302 | abd_zero_off(to_write, size, asize - size); | |
9303 | goto out; | |
9304 | } | |
9305 | ||
9306 | if (compress != ZIO_COMPRESS_OFF && !HDR_COMPRESSION_ENABLED(hdr)) { | |
9307 | cabd = abd_alloc_for_io(asize, ismd); | |
9308 | tmp = abd_borrow_buf(cabd, asize); | |
9309 | ||
10b3c7f5 MN |
9310 | psize = zio_compress_data(compress, to_write, tmp, size, |
9311 | hdr->b_complevel); | |
9312 | ||
9313 | if (psize >= size) { | |
9314 | abd_return_buf(cabd, tmp, asize); | |
9315 | HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); | |
9316 | to_write = cabd; | |
9317 | abd_copy(to_write, hdr->b_l1hdr.b_pabd, size); | |
9318 | if (size != asize) | |
9319 | abd_zero_off(to_write, size, asize - size); | |
9320 | goto encrypt; | |
9321 | } | |
b5256303 TC |
9322 | ASSERT3U(psize, <=, HDR_GET_PSIZE(hdr)); |
9323 | if (psize < asize) | |
9324 | bzero((char *)tmp + psize, asize - psize); | |
9325 | psize = HDR_GET_PSIZE(hdr); | |
9326 | abd_return_buf_copy(cabd, tmp, asize); | |
9327 | to_write = cabd; | |
9328 | } | |
9329 | ||
10b3c7f5 | 9330 | encrypt: |
b5256303 TC |
9331 | if (HDR_ENCRYPTED(hdr)) { |
9332 | eabd = abd_alloc_for_io(asize, ismd); | |
9333 | ||
9334 | /* | |
9335 | * If the dataset was disowned before the buffer | |
9336 | * made it to this point, the key to re-encrypt | |
9337 | * it won't be available. In this case we simply | |
9338 | * won't write the buffer to the L2ARC. | |
9339 | */ | |
9340 | ret = spa_keystore_lookup_key(spa, hdr->b_crypt_hdr.b_dsobj, | |
9341 | FTAG, &dck); | |
9342 | if (ret != 0) | |
9343 | goto error; | |
9344 | ||
10fa2545 | 9345 | ret = zio_do_crypt_abd(B_TRUE, &dck->dck_key, |
be9a5c35 TC |
9346 | hdr->b_crypt_hdr.b_ot, bswap, hdr->b_crypt_hdr.b_salt, |
9347 | hdr->b_crypt_hdr.b_iv, mac, psize, to_write, eabd, | |
9348 | &no_crypt); | |
b5256303 TC |
9349 | if (ret != 0) |
9350 | goto error; | |
9351 | ||
4807c0ba TC |
9352 | if (no_crypt) |
9353 | abd_copy(eabd, to_write, psize); | |
b5256303 TC |
9354 | |
9355 | if (psize != asize) | |
9356 | abd_zero_off(eabd, psize, asize - psize); | |
9357 | ||
9358 | /* assert that the MAC we got here matches the one we saved */ | |
9359 | ASSERT0(bcmp(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN)); | |
9360 | spa_keystore_dsl_key_rele(spa, dck, FTAG); | |
9361 | ||
9362 | if (to_write == cabd) | |
9363 | abd_free(cabd); | |
9364 | ||
9365 | to_write = eabd; | |
9366 | } | |
9367 | ||
9368 | out: | |
9369 | ASSERT3P(to_write, !=, hdr->b_l1hdr.b_pabd); | |
9370 | *abd_out = to_write; | |
9371 | return (0); | |
9372 | ||
9373 | error: | |
9374 | if (dck != NULL) | |
9375 | spa_keystore_dsl_key_rele(spa, dck, FTAG); | |
9376 | if (cabd != NULL) | |
9377 | abd_free(cabd); | |
9378 | if (eabd != NULL) | |
9379 | abd_free(eabd); | |
9380 | ||
9381 | *abd_out = NULL; | |
9382 | return (ret); | |
9383 | } | |
9384 | ||
77f6826b GA |
9385 | static void |
9386 | l2arc_blk_fetch_done(zio_t *zio) | |
9387 | { | |
9388 | l2arc_read_callback_t *cb; | |
9389 | ||
9390 | cb = zio->io_private; | |
9391 | if (cb->l2rcb_abd != NULL) | |
e2af2acc | 9392 | abd_free(cb->l2rcb_abd); |
77f6826b GA |
9393 | kmem_free(cb, sizeof (l2arc_read_callback_t)); |
9394 | } | |
9395 | ||
34dc7c2f BB |
9396 | /* |
9397 | * Find and write ARC buffers to the L2ARC device. | |
9398 | * | |
2a432414 | 9399 | * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid |
34dc7c2f | 9400 | * for reading until they have completed writing. |
3a17a7a9 SK |
9401 | * The headroom_boost is an in-out parameter used to maintain headroom boost |
9402 | * state between calls to this function. | |
9403 | * | |
9404 | * Returns the number of bytes actually written (which may be smaller than | |
77f6826b GA |
9405 | * the delta by which the device hand has changed due to alignment and the |
9406 | * writing of log blocks). | |
34dc7c2f | 9407 | */ |
d164b209 | 9408 | static uint64_t |
d3c2ae1c | 9409 | l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) |
34dc7c2f | 9410 | { |
77f6826b GA |
9411 | arc_buf_hdr_t *hdr, *hdr_prev, *head; |
9412 | uint64_t write_asize, write_psize, write_lsize, headroom; | |
9413 | boolean_t full; | |
9414 | l2arc_write_callback_t *cb = NULL; | |
9415 | zio_t *pio, *wzio; | |
9416 | uint64_t guid = spa_load_guid(spa); | |
0ae184a6 | 9417 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; |
34dc7c2f | 9418 | |
d3c2ae1c | 9419 | ASSERT3P(dev->l2ad_vdev, !=, NULL); |
3a17a7a9 | 9420 | |
34dc7c2f | 9421 | pio = NULL; |
01850391 | 9422 | write_lsize = write_asize = write_psize = 0; |
34dc7c2f | 9423 | full = B_FALSE; |
b9541d6b | 9424 | head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); |
d3c2ae1c | 9425 | arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR); |
3a17a7a9 | 9426 | |
34dc7c2f BB |
9427 | /* |
9428 | * Copy buffers for L2ARC writing. | |
9429 | */ | |
4a90d4d6 | 9430 | for (int pass = 0; pass < L2ARC_FEED_TYPES; pass++) { |
feb3a7ee | 9431 | /* |
4a90d4d6 | 9432 | * If pass == 1 or 3, we cache MRU metadata and data |
feb3a7ee GA |
9433 | * respectively. |
9434 | */ | |
9435 | if (l2arc_mfuonly) { | |
4a90d4d6 | 9436 | if (pass == 1 || pass == 3) |
feb3a7ee GA |
9437 | continue; |
9438 | } | |
9439 | ||
4a90d4d6 | 9440 | multilist_sublist_t *mls = l2arc_sublist_lock(pass); |
3a17a7a9 SK |
9441 | uint64_t passed_sz = 0; |
9442 | ||
4aafab91 G |
9443 | VERIFY3P(mls, !=, NULL); |
9444 | ||
b128c09f BB |
9445 | /* |
9446 | * L2ARC fast warmup. | |
9447 | * | |
9448 | * Until the ARC is warm and starts to evict, read from the | |
9449 | * head of the ARC lists rather than the tail. | |
9450 | */ | |
b128c09f | 9451 | if (arc_warm == B_FALSE) |
ca0bf58d | 9452 | hdr = multilist_sublist_head(mls); |
b128c09f | 9453 | else |
ca0bf58d | 9454 | hdr = multilist_sublist_tail(mls); |
b128c09f | 9455 | |
3a17a7a9 | 9456 | headroom = target_sz * l2arc_headroom; |
d3c2ae1c | 9457 | if (zfs_compressed_arc_enabled) |
3a17a7a9 SK |
9458 | headroom = (headroom * l2arc_headroom_boost) / 100; |
9459 | ||
2a432414 | 9460 | for (; hdr; hdr = hdr_prev) { |
3a17a7a9 | 9461 | kmutex_t *hash_lock; |
b5256303 | 9462 | abd_t *to_write = NULL; |
3a17a7a9 | 9463 | |
b128c09f | 9464 | if (arc_warm == B_FALSE) |
ca0bf58d | 9465 | hdr_prev = multilist_sublist_next(mls, hdr); |
b128c09f | 9466 | else |
ca0bf58d | 9467 | hdr_prev = multilist_sublist_prev(mls, hdr); |
34dc7c2f | 9468 | |
2a432414 | 9469 | hash_lock = HDR_LOCK(hdr); |
3a17a7a9 | 9470 | if (!mutex_tryenter(hash_lock)) { |
34dc7c2f BB |
9471 | /* |
9472 | * Skip this buffer rather than waiting. | |
9473 | */ | |
9474 | continue; | |
9475 | } | |
9476 | ||
d3c2ae1c | 9477 | passed_sz += HDR_GET_LSIZE(hdr); |
77f6826b | 9478 | if (l2arc_headroom != 0 && passed_sz > headroom) { |
34dc7c2f BB |
9479 | /* |
9480 | * Searched too far. | |
9481 | */ | |
9482 | mutex_exit(hash_lock); | |
9483 | break; | |
9484 | } | |
9485 | ||
2a432414 | 9486 | if (!l2arc_write_eligible(guid, hdr)) { |
34dc7c2f BB |
9487 | mutex_exit(hash_lock); |
9488 | continue; | |
9489 | } | |
9490 | ||
01850391 AG |
9491 | ASSERT(HDR_HAS_L1HDR(hdr)); |
9492 | ||
9493 | ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); | |
01850391 | 9494 | ASSERT3U(arc_hdr_size(hdr), >, 0); |
b5256303 TC |
9495 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || |
9496 | HDR_HAS_RABD(hdr)); | |
9497 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
01850391 AG |
9498 | uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, |
9499 | psize); | |
9500 | ||
9501 | if ((write_asize + asize) > target_sz) { | |
34dc7c2f BB |
9502 | full = B_TRUE; |
9503 | mutex_exit(hash_lock); | |
9504 | break; | |
9505 | } | |
9506 | ||
b5256303 TC |
9507 | /* |
9508 | * We rely on the L1 portion of the header below, so | |
9509 | * it's invalid for this header to have been evicted out | |
9510 | * of the ghost cache, prior to being written out. The | |
9511 | * ARC_FLAG_L2_WRITING bit ensures this won't happen. | |
9512 | */ | |
9513 | arc_hdr_set_flags(hdr, ARC_FLAG_L2_WRITING); | |
b5256303 TC |
9514 | |
9515 | /* | |
9516 | * If this header has b_rabd, we can use this since it | |
9517 | * must always match the data exactly as it exists on | |
9777044f | 9518 | * disk. Otherwise, the L2ARC can normally use the |
b5256303 TC |
9519 | * hdr's data, but if we're sharing data between the |
9520 | * hdr and one of its bufs, L2ARC needs its own copy of | |
9521 | * the data so that the ZIO below can't race with the | |
9522 | * buf consumer. To ensure that this copy will be | |
9523 | * available for the lifetime of the ZIO and be cleaned | |
9524 | * up afterwards, we add it to the l2arc_free_on_write | |
9525 | * queue. If we need to apply any transforms to the | |
9526 | * data (compression, encryption) we will also need the | |
9527 | * extra buffer. | |
9528 | */ | |
9529 | if (HDR_HAS_RABD(hdr) && psize == asize) { | |
9530 | to_write = hdr->b_crypt_hdr.b_rabd; | |
9531 | } else if ((HDR_COMPRESSION_ENABLED(hdr) || | |
9532 | HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) && | |
9533 | !HDR_ENCRYPTED(hdr) && !HDR_SHARED_DATA(hdr) && | |
9534 | psize == asize) { | |
9535 | to_write = hdr->b_l1hdr.b_pabd; | |
9536 | } else { | |
9537 | int ret; | |
9538 | arc_buf_contents_t type = arc_buf_type(hdr); | |
9539 | ||
9540 | ret = l2arc_apply_transforms(spa, hdr, asize, | |
9541 | &to_write); | |
9542 | if (ret != 0) { | |
9543 | arc_hdr_clear_flags(hdr, | |
9544 | ARC_FLAG_L2_WRITING); | |
9545 | mutex_exit(hash_lock); | |
9546 | continue; | |
9547 | } | |
9548 | ||
9549 | l2arc_free_abd_on_write(to_write, asize, type); | |
9550 | } | |
9551 | ||
34dc7c2f BB |
9552 | if (pio == NULL) { |
9553 | /* | |
9554 | * Insert a dummy header on the buflist so | |
9555 | * l2arc_write_done() can find where the | |
9556 | * write buffers begin without searching. | |
9557 | */ | |
ca0bf58d | 9558 | mutex_enter(&dev->l2ad_mtx); |
b9541d6b | 9559 | list_insert_head(&dev->l2ad_buflist, head); |
ca0bf58d | 9560 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 9561 | |
96c080cb BB |
9562 | cb = kmem_alloc( |
9563 | sizeof (l2arc_write_callback_t), KM_SLEEP); | |
34dc7c2f BB |
9564 | cb->l2wcb_dev = dev; |
9565 | cb->l2wcb_head = head; | |
657fd33b GA |
9566 | /* |
9567 | * Create a list to save allocated abd buffers | |
9568 | * for l2arc_log_blk_commit(). | |
9569 | */ | |
77f6826b GA |
9570 | list_create(&cb->l2wcb_abd_list, |
9571 | sizeof (l2arc_lb_abd_buf_t), | |
9572 | offsetof(l2arc_lb_abd_buf_t, node)); | |
34dc7c2f BB |
9573 | pio = zio_root(spa, l2arc_write_done, cb, |
9574 | ZIO_FLAG_CANFAIL); | |
9575 | } | |
9576 | ||
b9541d6b | 9577 | hdr->b_l2hdr.b_dev = dev; |
b9541d6b | 9578 | hdr->b_l2hdr.b_hits = 0; |
3a17a7a9 | 9579 | |
d3c2ae1c | 9580 | hdr->b_l2hdr.b_daddr = dev->l2ad_hand; |
08532162 GA |
9581 | hdr->b_l2hdr.b_arcs_state = |
9582 | hdr->b_l1hdr.b_state->arcs_state; | |
b5256303 | 9583 | arc_hdr_set_flags(hdr, ARC_FLAG_HAS_L2HDR); |
3a17a7a9 | 9584 | |
ca0bf58d | 9585 | mutex_enter(&dev->l2ad_mtx); |
b9541d6b | 9586 | list_insert_head(&dev->l2ad_buflist, hdr); |
ca0bf58d | 9587 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 9588 | |
424fd7c3 | 9589 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, |
b5256303 | 9590 | arc_hdr_size(hdr), hdr); |
3a17a7a9 | 9591 | |
34dc7c2f | 9592 | wzio = zio_write_phys(pio, dev->l2ad_vdev, |
82710e99 | 9593 | hdr->b_l2hdr.b_daddr, asize, to_write, |
d3c2ae1c GW |
9594 | ZIO_CHECKSUM_OFF, NULL, hdr, |
9595 | ZIO_PRIORITY_ASYNC_WRITE, | |
34dc7c2f BB |
9596 | ZIO_FLAG_CANFAIL, B_FALSE); |
9597 | ||
01850391 | 9598 | write_lsize += HDR_GET_LSIZE(hdr); |
34dc7c2f BB |
9599 | DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, |
9600 | zio_t *, wzio); | |
d962d5da | 9601 | |
01850391 AG |
9602 | write_psize += psize; |
9603 | write_asize += asize; | |
d3c2ae1c | 9604 | dev->l2ad_hand += asize; |
08532162 | 9605 | l2arc_hdr_arcstats_increment(hdr); |
7558997d | 9606 | vdev_space_update(dev->l2ad_vdev, asize, 0, 0); |
d3c2ae1c GW |
9607 | |
9608 | mutex_exit(hash_lock); | |
9609 | ||
77f6826b GA |
9610 | /* |
9611 | * Append buf info to current log and commit if full. | |
9612 | * arcstat_l2_{size,asize} kstats are updated | |
9613 | * internally. | |
9614 | */ | |
9615 | if (l2arc_log_blk_insert(dev, hdr)) | |
9616 | l2arc_log_blk_commit(dev, pio, cb); | |
9617 | ||
9cdf7b1f | 9618 | zio_nowait(wzio); |
34dc7c2f | 9619 | } |
d3c2ae1c GW |
9620 | |
9621 | multilist_sublist_unlock(mls); | |
9622 | ||
9623 | if (full == B_TRUE) | |
9624 | break; | |
34dc7c2f | 9625 | } |
34dc7c2f | 9626 | |
d3c2ae1c GW |
9627 | /* No buffers selected for writing? */ |
9628 | if (pio == NULL) { | |
01850391 | 9629 | ASSERT0(write_lsize); |
d3c2ae1c GW |
9630 | ASSERT(!HDR_HAS_L1HDR(head)); |
9631 | kmem_cache_free(hdr_l2only_cache, head); | |
77f6826b GA |
9632 | |
9633 | /* | |
9634 | * Although we did not write any buffers l2ad_evict may | |
9635 | * have advanced. | |
9636 | */ | |
0ae184a6 GA |
9637 | if (dev->l2ad_evict != l2dhdr->dh_evict) |
9638 | l2arc_dev_hdr_update(dev); | |
77f6826b | 9639 | |
d3c2ae1c GW |
9640 | return (0); |
9641 | } | |
34dc7c2f | 9642 | |
657fd33b GA |
9643 | if (!dev->l2ad_first) |
9644 | ASSERT3U(dev->l2ad_hand, <=, dev->l2ad_evict); | |
9645 | ||
3a17a7a9 | 9646 | ASSERT3U(write_asize, <=, target_sz); |
34dc7c2f | 9647 | ARCSTAT_BUMP(arcstat_l2_writes_sent); |
01850391 | 9648 | ARCSTAT_INCR(arcstat_l2_write_bytes, write_psize); |
34dc7c2f | 9649 | |
d164b209 | 9650 | dev->l2ad_writing = B_TRUE; |
34dc7c2f | 9651 | (void) zio_wait(pio); |
d164b209 BB |
9652 | dev->l2ad_writing = B_FALSE; |
9653 | ||
2054f35e GA |
9654 | /* |
9655 | * Update the device header after the zio completes as | |
9656 | * l2arc_write_done() may have updated the memory holding the log block | |
9657 | * pointers in the device header. | |
9658 | */ | |
9659 | l2arc_dev_hdr_update(dev); | |
9660 | ||
3a17a7a9 SK |
9661 | return (write_asize); |
9662 | } | |
9663 | ||
523e1295 AM |
9664 | static boolean_t |
9665 | l2arc_hdr_limit_reached(void) | |
9666 | { | |
c4c162c1 | 9667 | int64_t s = aggsum_upper_bound(&arc_sums.arcstat_l2_hdr_size); |
523e1295 AM |
9668 | |
9669 | return (arc_reclaim_needed() || (s > arc_meta_limit * 3 / 4) || | |
9670 | (s > (arc_warm ? arc_c : arc_c_max) * l2arc_meta_percent / 100)); | |
9671 | } | |
9672 | ||
34dc7c2f BB |
9673 | /* |
9674 | * This thread feeds the L2ARC at regular intervals. This is the beating | |
9675 | * heart of the L2ARC. | |
9676 | */ | |
9677 | static void | |
c25b8f99 | 9678 | l2arc_feed_thread(void *unused) |
34dc7c2f | 9679 | { |
14e4e3cb | 9680 | (void) unused; |
34dc7c2f BB |
9681 | callb_cpr_t cpr; |
9682 | l2arc_dev_t *dev; | |
9683 | spa_t *spa; | |
d164b209 | 9684 | uint64_t size, wrote; |
428870ff | 9685 | clock_t begin, next = ddi_get_lbolt(); |
40d06e3c | 9686 | fstrans_cookie_t cookie; |
34dc7c2f BB |
9687 | |
9688 | CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); | |
9689 | ||
9690 | mutex_enter(&l2arc_feed_thr_lock); | |
9691 | ||
40d06e3c | 9692 | cookie = spl_fstrans_mark(); |
34dc7c2f | 9693 | while (l2arc_thread_exit == 0) { |
34dc7c2f | 9694 | CALLB_CPR_SAFE_BEGIN(&cpr); |
ac6e5fb2 | 9695 | (void) cv_timedwait_idle(&l2arc_feed_thr_cv, |
5b63b3eb | 9696 | &l2arc_feed_thr_lock, next); |
34dc7c2f | 9697 | CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); |
428870ff | 9698 | next = ddi_get_lbolt() + hz; |
34dc7c2f BB |
9699 | |
9700 | /* | |
b128c09f | 9701 | * Quick check for L2ARC devices. |
34dc7c2f BB |
9702 | */ |
9703 | mutex_enter(&l2arc_dev_mtx); | |
9704 | if (l2arc_ndev == 0) { | |
9705 | mutex_exit(&l2arc_dev_mtx); | |
9706 | continue; | |
9707 | } | |
b128c09f | 9708 | mutex_exit(&l2arc_dev_mtx); |
428870ff | 9709 | begin = ddi_get_lbolt(); |
34dc7c2f BB |
9710 | |
9711 | /* | |
b128c09f BB |
9712 | * This selects the next l2arc device to write to, and in |
9713 | * doing so the next spa to feed from: dev->l2ad_spa. This | |
9714 | * will return NULL if there are now no l2arc devices or if | |
9715 | * they are all faulted. | |
9716 | * | |
9717 | * If a device is returned, its spa's config lock is also | |
9718 | * held to prevent device removal. l2arc_dev_get_next() | |
9719 | * will grab and release l2arc_dev_mtx. | |
34dc7c2f | 9720 | */ |
b128c09f | 9721 | if ((dev = l2arc_dev_get_next()) == NULL) |
34dc7c2f | 9722 | continue; |
b128c09f BB |
9723 | |
9724 | spa = dev->l2ad_spa; | |
d3c2ae1c | 9725 | ASSERT3P(spa, !=, NULL); |
34dc7c2f | 9726 | |
572e2857 BB |
9727 | /* |
9728 | * If the pool is read-only then force the feed thread to | |
9729 | * sleep a little longer. | |
9730 | */ | |
9731 | if (!spa_writeable(spa)) { | |
9732 | next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; | |
9733 | spa_config_exit(spa, SCL_L2ARC, dev); | |
9734 | continue; | |
9735 | } | |
9736 | ||
34dc7c2f | 9737 | /* |
b128c09f | 9738 | * Avoid contributing to memory pressure. |
34dc7c2f | 9739 | */ |
523e1295 | 9740 | if (l2arc_hdr_limit_reached()) { |
b128c09f BB |
9741 | ARCSTAT_BUMP(arcstat_l2_abort_lowmem); |
9742 | spa_config_exit(spa, SCL_L2ARC, dev); | |
34dc7c2f BB |
9743 | continue; |
9744 | } | |
b128c09f | 9745 | |
34dc7c2f BB |
9746 | ARCSTAT_BUMP(arcstat_l2_feeds); |
9747 | ||
37c22948 | 9748 | size = l2arc_write_size(dev); |
b128c09f | 9749 | |
34dc7c2f BB |
9750 | /* |
9751 | * Evict L2ARC buffers that will be overwritten. | |
9752 | */ | |
b128c09f | 9753 | l2arc_evict(dev, size, B_FALSE); |
34dc7c2f BB |
9754 | |
9755 | /* | |
9756 | * Write ARC buffers. | |
9757 | */ | |
d3c2ae1c | 9758 | wrote = l2arc_write_buffers(spa, dev, size); |
d164b209 BB |
9759 | |
9760 | /* | |
9761 | * Calculate interval between writes. | |
9762 | */ | |
9763 | next = l2arc_write_interval(begin, size, wrote); | |
b128c09f | 9764 | spa_config_exit(spa, SCL_L2ARC, dev); |
34dc7c2f | 9765 | } |
40d06e3c | 9766 | spl_fstrans_unmark(cookie); |
34dc7c2f BB |
9767 | |
9768 | l2arc_thread_exit = 0; | |
9769 | cv_broadcast(&l2arc_feed_thr_cv); | |
9770 | CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ | |
9771 | thread_exit(); | |
9772 | } | |
9773 | ||
b128c09f BB |
9774 | boolean_t |
9775 | l2arc_vdev_present(vdev_t *vd) | |
9776 | { | |
77f6826b GA |
9777 | return (l2arc_vdev_get(vd) != NULL); |
9778 | } | |
9779 | ||
9780 | /* | |
9781 | * Returns the l2arc_dev_t associated with a particular vdev_t or NULL if | |
9782 | * the vdev_t isn't an L2ARC device. | |
9783 | */ | |
b7654bd7 | 9784 | l2arc_dev_t * |
77f6826b GA |
9785 | l2arc_vdev_get(vdev_t *vd) |
9786 | { | |
9787 | l2arc_dev_t *dev; | |
b128c09f BB |
9788 | |
9789 | mutex_enter(&l2arc_dev_mtx); | |
9790 | for (dev = list_head(l2arc_dev_list); dev != NULL; | |
9791 | dev = list_next(l2arc_dev_list, dev)) { | |
9792 | if (dev->l2ad_vdev == vd) | |
9793 | break; | |
9794 | } | |
9795 | mutex_exit(&l2arc_dev_mtx); | |
9796 | ||
77f6826b | 9797 | return (dev); |
b128c09f BB |
9798 | } |
9799 | ||
ab8a8f07 GA |
9800 | static void |
9801 | l2arc_rebuild_dev(l2arc_dev_t *dev, boolean_t reopen) | |
9802 | { | |
9803 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; | |
9804 | uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize; | |
9805 | spa_t *spa = dev->l2ad_spa; | |
9806 | ||
9807 | /* | |
9808 | * The L2ARC has to hold at least the payload of one log block for | |
9809 | * them to be restored (persistent L2ARC). The payload of a log block | |
9810 | * depends on the amount of its log entries. We always write log blocks | |
9811 | * with 1022 entries. How many of them are committed or restored depends | |
9812 | * on the size of the L2ARC device. Thus the maximum payload of | |
9813 | * one log block is 1022 * SPA_MAXBLOCKSIZE = 16GB. If the L2ARC device | |
9814 | * is less than that, we reduce the amount of committed and restored | |
9815 | * log entries per block so as to enable persistence. | |
9816 | */ | |
9817 | if (dev->l2ad_end < l2arc_rebuild_blocks_min_l2size) { | |
9818 | dev->l2ad_log_entries = 0; | |
9819 | } else { | |
9820 | dev->l2ad_log_entries = MIN((dev->l2ad_end - | |
9821 | dev->l2ad_start) >> SPA_MAXBLOCKSHIFT, | |
9822 | L2ARC_LOG_BLK_MAX_ENTRIES); | |
9823 | } | |
9824 | ||
9825 | /* | |
9826 | * Read the device header, if an error is returned do not rebuild L2ARC. | |
9827 | */ | |
9828 | if (l2arc_dev_hdr_read(dev) == 0 && dev->l2ad_log_entries > 0) { | |
9829 | /* | |
9830 | * If we are onlining a cache device (vdev_reopen) that was | |
9831 | * still present (l2arc_vdev_present()) and rebuild is enabled, | |
9832 | * we should evict all ARC buffers and pointers to log blocks | |
9833 | * and reclaim their space before restoring its contents to | |
9834 | * L2ARC. | |
9835 | */ | |
9836 | if (reopen) { | |
9837 | if (!l2arc_rebuild_enabled) { | |
9838 | return; | |
9839 | } else { | |
9840 | l2arc_evict(dev, 0, B_TRUE); | |
9841 | /* start a new log block */ | |
9842 | dev->l2ad_log_ent_idx = 0; | |
9843 | dev->l2ad_log_blk_payload_asize = 0; | |
9844 | dev->l2ad_log_blk_payload_start = 0; | |
9845 | } | |
9846 | } | |
9847 | /* | |
9848 | * Just mark the device as pending for a rebuild. We won't | |
9849 | * be starting a rebuild in line here as it would block pool | |
9850 | * import. Instead spa_load_impl will hand that off to an | |
9851 | * async task which will call l2arc_spa_rebuild_start. | |
9852 | */ | |
9853 | dev->l2ad_rebuild = B_TRUE; | |
9854 | } else if (spa_writeable(spa)) { | |
9855 | /* | |
9856 | * In this case TRIM the whole device if l2arc_trim_ahead > 0, | |
9857 | * otherwise create a new header. We zero out the memory holding | |
9858 | * the header to reset dh_start_lbps. If we TRIM the whole | |
9859 | * device the new header will be written by | |
9860 | * vdev_trim_l2arc_thread() at the end of the TRIM to update the | |
9861 | * trim_state in the header too. When reading the header, if | |
9862 | * trim_state is not VDEV_TRIM_COMPLETE and l2arc_trim_ahead > 0 | |
9863 | * we opt to TRIM the whole device again. | |
9864 | */ | |
9865 | if (l2arc_trim_ahead > 0) { | |
9866 | dev->l2ad_trim_all = B_TRUE; | |
9867 | } else { | |
9868 | bzero(l2dhdr, l2dhdr_asize); | |
9869 | l2arc_dev_hdr_update(dev); | |
9870 | } | |
9871 | } | |
9872 | } | |
9873 | ||
34dc7c2f BB |
9874 | /* |
9875 | * Add a vdev for use by the L2ARC. By this point the spa has already | |
9876 | * validated the vdev and opened it. | |
9877 | */ | |
9878 | void | |
9babb374 | 9879 | l2arc_add_vdev(spa_t *spa, vdev_t *vd) |
34dc7c2f | 9880 | { |
77f6826b GA |
9881 | l2arc_dev_t *adddev; |
9882 | uint64_t l2dhdr_asize; | |
34dc7c2f | 9883 | |
b128c09f BB |
9884 | ASSERT(!l2arc_vdev_present(vd)); |
9885 | ||
34dc7c2f BB |
9886 | /* |
9887 | * Create a new l2arc device entry. | |
9888 | */ | |
77f6826b | 9889 | adddev = vmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); |
34dc7c2f BB |
9890 | adddev->l2ad_spa = spa; |
9891 | adddev->l2ad_vdev = vd; | |
77f6826b GA |
9892 | /* leave extra size for an l2arc device header */ |
9893 | l2dhdr_asize = adddev->l2ad_dev_hdr_asize = | |
9894 | MAX(sizeof (*adddev->l2ad_dev_hdr), 1 << vd->vdev_ashift); | |
9895 | adddev->l2ad_start = VDEV_LABEL_START_SIZE + l2dhdr_asize; | |
9babb374 | 9896 | adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); |
77f6826b | 9897 | ASSERT3U(adddev->l2ad_start, <, adddev->l2ad_end); |
34dc7c2f | 9898 | adddev->l2ad_hand = adddev->l2ad_start; |
77f6826b | 9899 | adddev->l2ad_evict = adddev->l2ad_start; |
34dc7c2f | 9900 | adddev->l2ad_first = B_TRUE; |
d164b209 | 9901 | adddev->l2ad_writing = B_FALSE; |
b7654bd7 | 9902 | adddev->l2ad_trim_all = B_FALSE; |
98f72a53 | 9903 | list_link_init(&adddev->l2ad_node); |
77f6826b | 9904 | adddev->l2ad_dev_hdr = kmem_zalloc(l2dhdr_asize, KM_SLEEP); |
34dc7c2f | 9905 | |
b9541d6b | 9906 | mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
9907 | /* |
9908 | * This is a list of all ARC buffers that are still valid on the | |
9909 | * device. | |
9910 | */ | |
b9541d6b CW |
9911 | list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), |
9912 | offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); | |
34dc7c2f | 9913 | |
77f6826b GA |
9914 | /* |
9915 | * This is a list of pointers to log blocks that are still present | |
9916 | * on the device. | |
9917 | */ | |
9918 | list_create(&adddev->l2ad_lbptr_list, sizeof (l2arc_lb_ptr_buf_t), | |
9919 | offsetof(l2arc_lb_ptr_buf_t, node)); | |
9920 | ||
428870ff | 9921 | vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); |
424fd7c3 | 9922 | zfs_refcount_create(&adddev->l2ad_alloc); |
657fd33b GA |
9923 | zfs_refcount_create(&adddev->l2ad_lb_asize); |
9924 | zfs_refcount_create(&adddev->l2ad_lb_count); | |
34dc7c2f | 9925 | |
ab8a8f07 GA |
9926 | /* |
9927 | * Decide if dev is eligible for L2ARC rebuild or whole device | |
9928 | * trimming. This has to happen before the device is added in the | |
9929 | * cache device list and l2arc_dev_mtx is released. Otherwise | |
9930 | * l2arc_feed_thread() might already start writing on the | |
9931 | * device. | |
9932 | */ | |
9933 | l2arc_rebuild_dev(adddev, B_FALSE); | |
9934 | ||
34dc7c2f BB |
9935 | /* |
9936 | * Add device to global list | |
9937 | */ | |
9938 | mutex_enter(&l2arc_dev_mtx); | |
9939 | list_insert_head(l2arc_dev_list, adddev); | |
9940 | atomic_inc_64(&l2arc_ndev); | |
9941 | mutex_exit(&l2arc_dev_mtx); | |
77f6826b GA |
9942 | } |
9943 | ||
ab8a8f07 GA |
9944 | /* |
9945 | * Decide if a vdev is eligible for L2ARC rebuild, called from vdev_reopen() | |
9946 | * in case of onlining a cache device. | |
9947 | */ | |
77f6826b GA |
9948 | void |
9949 | l2arc_rebuild_vdev(vdev_t *vd, boolean_t reopen) | |
9950 | { | |
9951 | l2arc_dev_t *dev = NULL; | |
77f6826b GA |
9952 | |
9953 | dev = l2arc_vdev_get(vd); | |
9954 | ASSERT3P(dev, !=, NULL); | |
77f6826b GA |
9955 | |
9956 | /* | |
ab8a8f07 GA |
9957 | * In contrast to l2arc_add_vdev() we do not have to worry about |
9958 | * l2arc_feed_thread() invalidating previous content when onlining a | |
9959 | * cache device. The device parameters (l2ad*) are not cleared when | |
9960 | * offlining the device and writing new buffers will not invalidate | |
9961 | * all previous content. In worst case only buffers that have not had | |
9962 | * their log block written to the device will be lost. | |
9963 | * When onlining the cache device (ie offline->online without exporting | |
9964 | * the pool in between) this happens: | |
9965 | * vdev_reopen() -> vdev_open() -> l2arc_rebuild_vdev() | |
9966 | * | | | |
9967 | * vdev_is_dead() = B_FALSE l2ad_rebuild = B_TRUE | |
9968 | * During the time where vdev_is_dead = B_FALSE and until l2ad_rebuild | |
9969 | * is set to B_TRUE we might write additional buffers to the device. | |
9970 | */ | |
9971 | l2arc_rebuild_dev(dev, reopen); | |
34dc7c2f BB |
9972 | } |
9973 | ||
9974 | /* | |
9975 | * Remove a vdev from the L2ARC. | |
9976 | */ | |
9977 | void | |
9978 | l2arc_remove_vdev(vdev_t *vd) | |
9979 | { | |
77f6826b | 9980 | l2arc_dev_t *remdev = NULL; |
34dc7c2f | 9981 | |
34dc7c2f BB |
9982 | /* |
9983 | * Find the device by vdev | |
9984 | */ | |
77f6826b | 9985 | remdev = l2arc_vdev_get(vd); |
d3c2ae1c | 9986 | ASSERT3P(remdev, !=, NULL); |
34dc7c2f | 9987 | |
77f6826b GA |
9988 | /* |
9989 | * Cancel any ongoing or scheduled rebuild. | |
9990 | */ | |
9991 | mutex_enter(&l2arc_rebuild_thr_lock); | |
9992 | if (remdev->l2ad_rebuild_began == B_TRUE) { | |
9993 | remdev->l2ad_rebuild_cancel = B_TRUE; | |
9994 | while (remdev->l2ad_rebuild == B_TRUE) | |
9995 | cv_wait(&l2arc_rebuild_thr_cv, &l2arc_rebuild_thr_lock); | |
9996 | } | |
9997 | mutex_exit(&l2arc_rebuild_thr_lock); | |
9998 | ||
34dc7c2f BB |
9999 | /* |
10000 | * Remove device from global list | |
10001 | */ | |
77f6826b | 10002 | mutex_enter(&l2arc_dev_mtx); |
34dc7c2f BB |
10003 | list_remove(l2arc_dev_list, remdev); |
10004 | l2arc_dev_last = NULL; /* may have been invalidated */ | |
b128c09f BB |
10005 | atomic_dec_64(&l2arc_ndev); |
10006 | mutex_exit(&l2arc_dev_mtx); | |
34dc7c2f BB |
10007 | |
10008 | /* | |
10009 | * Clear all buflists and ARC references. L2ARC device flush. | |
10010 | */ | |
10011 | l2arc_evict(remdev, 0, B_TRUE); | |
b9541d6b | 10012 | list_destroy(&remdev->l2ad_buflist); |
77f6826b GA |
10013 | ASSERT(list_is_empty(&remdev->l2ad_lbptr_list)); |
10014 | list_destroy(&remdev->l2ad_lbptr_list); | |
b9541d6b | 10015 | mutex_destroy(&remdev->l2ad_mtx); |
424fd7c3 | 10016 | zfs_refcount_destroy(&remdev->l2ad_alloc); |
657fd33b GA |
10017 | zfs_refcount_destroy(&remdev->l2ad_lb_asize); |
10018 | zfs_refcount_destroy(&remdev->l2ad_lb_count); | |
77f6826b GA |
10019 | kmem_free(remdev->l2ad_dev_hdr, remdev->l2ad_dev_hdr_asize); |
10020 | vmem_free(remdev, sizeof (l2arc_dev_t)); | |
34dc7c2f BB |
10021 | } |
10022 | ||
10023 | void | |
b128c09f | 10024 | l2arc_init(void) |
34dc7c2f BB |
10025 | { |
10026 | l2arc_thread_exit = 0; | |
10027 | l2arc_ndev = 0; | |
34dc7c2f BB |
10028 | |
10029 | mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); | |
10030 | cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); | |
77f6826b GA |
10031 | mutex_init(&l2arc_rebuild_thr_lock, NULL, MUTEX_DEFAULT, NULL); |
10032 | cv_init(&l2arc_rebuild_thr_cv, NULL, CV_DEFAULT, NULL); | |
34dc7c2f | 10033 | mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
10034 | mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); |
10035 | ||
10036 | l2arc_dev_list = &L2ARC_dev_list; | |
10037 | l2arc_free_on_write = &L2ARC_free_on_write; | |
10038 | list_create(l2arc_dev_list, sizeof (l2arc_dev_t), | |
10039 | offsetof(l2arc_dev_t, l2ad_node)); | |
10040 | list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), | |
10041 | offsetof(l2arc_data_free_t, l2df_list_node)); | |
34dc7c2f BB |
10042 | } |
10043 | ||
10044 | void | |
b128c09f | 10045 | l2arc_fini(void) |
34dc7c2f | 10046 | { |
34dc7c2f BB |
10047 | mutex_destroy(&l2arc_feed_thr_lock); |
10048 | cv_destroy(&l2arc_feed_thr_cv); | |
77f6826b GA |
10049 | mutex_destroy(&l2arc_rebuild_thr_lock); |
10050 | cv_destroy(&l2arc_rebuild_thr_cv); | |
34dc7c2f | 10051 | mutex_destroy(&l2arc_dev_mtx); |
34dc7c2f BB |
10052 | mutex_destroy(&l2arc_free_on_write_mtx); |
10053 | ||
10054 | list_destroy(l2arc_dev_list); | |
10055 | list_destroy(l2arc_free_on_write); | |
10056 | } | |
b128c09f BB |
10057 | |
10058 | void | |
10059 | l2arc_start(void) | |
10060 | { | |
da92d5cb | 10061 | if (!(spa_mode_global & SPA_MODE_WRITE)) |
b128c09f BB |
10062 | return; |
10063 | ||
10064 | (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, | |
1229323d | 10065 | TS_RUN, defclsyspri); |
b128c09f BB |
10066 | } |
10067 | ||
10068 | void | |
10069 | l2arc_stop(void) | |
10070 | { | |
da92d5cb | 10071 | if (!(spa_mode_global & SPA_MODE_WRITE)) |
b128c09f BB |
10072 | return; |
10073 | ||
10074 | mutex_enter(&l2arc_feed_thr_lock); | |
10075 | cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ | |
10076 | l2arc_thread_exit = 1; | |
10077 | while (l2arc_thread_exit != 0) | |
10078 | cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); | |
10079 | mutex_exit(&l2arc_feed_thr_lock); | |
10080 | } | |
c28b2279 | 10081 | |
77f6826b GA |
10082 | /* |
10083 | * Punches out rebuild threads for the L2ARC devices in a spa. This should | |
10084 | * be called after pool import from the spa async thread, since starting | |
10085 | * these threads directly from spa_import() will make them part of the | |
10086 | * "zpool import" context and delay process exit (and thus pool import). | |
10087 | */ | |
10088 | void | |
10089 | l2arc_spa_rebuild_start(spa_t *spa) | |
10090 | { | |
10091 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
10092 | ||
10093 | /* | |
10094 | * Locate the spa's l2arc devices and kick off rebuild threads. | |
10095 | */ | |
10096 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) { | |
10097 | l2arc_dev_t *dev = | |
10098 | l2arc_vdev_get(spa->spa_l2cache.sav_vdevs[i]); | |
10099 | if (dev == NULL) { | |
10100 | /* Don't attempt a rebuild if the vdev is UNAVAIL */ | |
10101 | continue; | |
10102 | } | |
10103 | mutex_enter(&l2arc_rebuild_thr_lock); | |
10104 | if (dev->l2ad_rebuild && !dev->l2ad_rebuild_cancel) { | |
10105 | dev->l2ad_rebuild_began = B_TRUE; | |
3eaf76a8 | 10106 | (void) thread_create(NULL, 0, l2arc_dev_rebuild_thread, |
77f6826b GA |
10107 | dev, 0, &p0, TS_RUN, minclsyspri); |
10108 | } | |
10109 | mutex_exit(&l2arc_rebuild_thr_lock); | |
10110 | } | |
10111 | } | |
10112 | ||
10113 | /* | |
10114 | * Main entry point for L2ARC rebuilding. | |
10115 | */ | |
10116 | static void | |
3eaf76a8 | 10117 | l2arc_dev_rebuild_thread(void *arg) |
77f6826b | 10118 | { |
3eaf76a8 RM |
10119 | l2arc_dev_t *dev = arg; |
10120 | ||
77f6826b GA |
10121 | VERIFY(!dev->l2ad_rebuild_cancel); |
10122 | VERIFY(dev->l2ad_rebuild); | |
10123 | (void) l2arc_rebuild(dev); | |
10124 | mutex_enter(&l2arc_rebuild_thr_lock); | |
10125 | dev->l2ad_rebuild_began = B_FALSE; | |
10126 | dev->l2ad_rebuild = B_FALSE; | |
10127 | mutex_exit(&l2arc_rebuild_thr_lock); | |
10128 | ||
10129 | thread_exit(); | |
10130 | } | |
10131 | ||
10132 | /* | |
10133 | * This function implements the actual L2ARC metadata rebuild. It: | |
10134 | * starts reading the log block chain and restores each block's contents | |
10135 | * to memory (reconstructing arc_buf_hdr_t's). | |
10136 | * | |
10137 | * Operation stops under any of the following conditions: | |
10138 | * | |
10139 | * 1) We reach the end of the log block chain. | |
10140 | * 2) We encounter *any* error condition (cksum errors, io errors) | |
10141 | */ | |
10142 | static int | |
10143 | l2arc_rebuild(l2arc_dev_t *dev) | |
10144 | { | |
10145 | vdev_t *vd = dev->l2ad_vdev; | |
10146 | spa_t *spa = vd->vdev_spa; | |
657fd33b | 10147 | int err = 0; |
77f6826b GA |
10148 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; |
10149 | l2arc_log_blk_phys_t *this_lb, *next_lb; | |
10150 | zio_t *this_io = NULL, *next_io = NULL; | |
10151 | l2arc_log_blkptr_t lbps[2]; | |
10152 | l2arc_lb_ptr_buf_t *lb_ptr_buf; | |
10153 | boolean_t lock_held; | |
10154 | ||
10155 | this_lb = vmem_zalloc(sizeof (*this_lb), KM_SLEEP); | |
10156 | next_lb = vmem_zalloc(sizeof (*next_lb), KM_SLEEP); | |
10157 | ||
10158 | /* | |
10159 | * We prevent device removal while issuing reads to the device, | |
10160 | * then during the rebuilding phases we drop this lock again so | |
10161 | * that a spa_unload or device remove can be initiated - this is | |
10162 | * safe, because the spa will signal us to stop before removing | |
10163 | * our device and wait for us to stop. | |
10164 | */ | |
10165 | spa_config_enter(spa, SCL_L2ARC, vd, RW_READER); | |
10166 | lock_held = B_TRUE; | |
10167 | ||
10168 | /* | |
10169 | * Retrieve the persistent L2ARC device state. | |
657fd33b | 10170 | * L2BLK_GET_PSIZE returns aligned size for log blocks. |
77f6826b GA |
10171 | */ |
10172 | dev->l2ad_evict = MAX(l2dhdr->dh_evict, dev->l2ad_start); | |
10173 | dev->l2ad_hand = MAX(l2dhdr->dh_start_lbps[0].lbp_daddr + | |
10174 | L2BLK_GET_PSIZE((&l2dhdr->dh_start_lbps[0])->lbp_prop), | |
10175 | dev->l2ad_start); | |
10176 | dev->l2ad_first = !!(l2dhdr->dh_flags & L2ARC_DEV_HDR_EVICT_FIRST); | |
10177 | ||
b7654bd7 GA |
10178 | vd->vdev_trim_action_time = l2dhdr->dh_trim_action_time; |
10179 | vd->vdev_trim_state = l2dhdr->dh_trim_state; | |
10180 | ||
77f6826b GA |
10181 | /* |
10182 | * In case the zfs module parameter l2arc_rebuild_enabled is false | |
10183 | * we do not start the rebuild process. | |
10184 | */ | |
10185 | if (!l2arc_rebuild_enabled) | |
10186 | goto out; | |
10187 | ||
10188 | /* Prepare the rebuild process */ | |
10189 | bcopy(l2dhdr->dh_start_lbps, lbps, sizeof (lbps)); | |
10190 | ||
10191 | /* Start the rebuild process */ | |
10192 | for (;;) { | |
10193 | if (!l2arc_log_blkptr_valid(dev, &lbps[0])) | |
10194 | break; | |
10195 | ||
10196 | if ((err = l2arc_log_blk_read(dev, &lbps[0], &lbps[1], | |
10197 | this_lb, next_lb, this_io, &next_io)) != 0) | |
10198 | goto out; | |
10199 | ||
10200 | /* | |
10201 | * Our memory pressure valve. If the system is running low | |
10202 | * on memory, rather than swamping memory with new ARC buf | |
10203 | * hdrs, we opt not to rebuild the L2ARC. At this point, | |
10204 | * however, we have already set up our L2ARC dev to chain in | |
10205 | * new metadata log blocks, so the user may choose to offline/ | |
10206 | * online the L2ARC dev at a later time (or re-import the pool) | |
10207 | * to reconstruct it (when there's less memory pressure). | |
10208 | */ | |
523e1295 | 10209 | if (l2arc_hdr_limit_reached()) { |
77f6826b GA |
10210 | ARCSTAT_BUMP(arcstat_l2_rebuild_abort_lowmem); |
10211 | cmn_err(CE_NOTE, "System running low on memory, " | |
10212 | "aborting L2ARC rebuild."); | |
10213 | err = SET_ERROR(ENOMEM); | |
10214 | goto out; | |
10215 | } | |
10216 | ||
10217 | spa_config_exit(spa, SCL_L2ARC, vd); | |
10218 | lock_held = B_FALSE; | |
10219 | ||
10220 | /* | |
10221 | * Now that we know that the next_lb checks out alright, we | |
10222 | * can start reconstruction from this log block. | |
657fd33b | 10223 | * L2BLK_GET_PSIZE returns aligned size for log blocks. |
77f6826b | 10224 | */ |
657fd33b | 10225 | uint64_t asize = L2BLK_GET_PSIZE((&lbps[0])->lbp_prop); |
a76e4e67 | 10226 | l2arc_log_blk_restore(dev, this_lb, asize); |
77f6826b GA |
10227 | |
10228 | /* | |
10229 | * log block restored, include its pointer in the list of | |
10230 | * pointers to log blocks present in the L2ARC device. | |
10231 | */ | |
10232 | lb_ptr_buf = kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t), KM_SLEEP); | |
10233 | lb_ptr_buf->lb_ptr = kmem_zalloc(sizeof (l2arc_log_blkptr_t), | |
10234 | KM_SLEEP); | |
10235 | bcopy(&lbps[0], lb_ptr_buf->lb_ptr, | |
10236 | sizeof (l2arc_log_blkptr_t)); | |
10237 | mutex_enter(&dev->l2ad_mtx); | |
10238 | list_insert_tail(&dev->l2ad_lbptr_list, lb_ptr_buf); | |
657fd33b GA |
10239 | ARCSTAT_INCR(arcstat_l2_log_blk_asize, asize); |
10240 | ARCSTAT_BUMP(arcstat_l2_log_blk_count); | |
10241 | zfs_refcount_add_many(&dev->l2ad_lb_asize, asize, lb_ptr_buf); | |
10242 | zfs_refcount_add(&dev->l2ad_lb_count, lb_ptr_buf); | |
77f6826b | 10243 | mutex_exit(&dev->l2ad_mtx); |
657fd33b | 10244 | vdev_space_update(vd, asize, 0, 0); |
77f6826b GA |
10245 | |
10246 | /* | |
10247 | * Protection against loops of log blocks: | |
10248 | * | |
10249 | * l2ad_hand l2ad_evict | |
10250 | * V V | |
10251 | * l2ad_start |=======================================| l2ad_end | |
10252 | * -----|||----|||---|||----||| | |
10253 | * (3) (2) (1) (0) | |
10254 | * ---|||---|||----|||---||| | |
10255 | * (7) (6) (5) (4) | |
10256 | * | |
10257 | * In this situation the pointer of log block (4) passes | |
10258 | * l2arc_log_blkptr_valid() but the log block should not be | |
10259 | * restored as it is overwritten by the payload of log block | |
10260 | * (0). Only log blocks (0)-(3) should be restored. We check | |
657fd33b GA |
10261 | * whether l2ad_evict lies in between the payload starting |
10262 | * offset of the next log block (lbps[1].lbp_payload_start) | |
10263 | * and the payload starting offset of the present log block | |
10264 | * (lbps[0].lbp_payload_start). If true and this isn't the | |
10265 | * first pass, we are looping from the beginning and we should | |
10266 | * stop. | |
77f6826b | 10267 | */ |
657fd33b GA |
10268 | if (l2arc_range_check_overlap(lbps[1].lbp_payload_start, |
10269 | lbps[0].lbp_payload_start, dev->l2ad_evict) && | |
10270 | !dev->l2ad_first) | |
77f6826b GA |
10271 | goto out; |
10272 | ||
1199c3e8 | 10273 | cond_resched(); |
77f6826b GA |
10274 | for (;;) { |
10275 | mutex_enter(&l2arc_rebuild_thr_lock); | |
10276 | if (dev->l2ad_rebuild_cancel) { | |
10277 | dev->l2ad_rebuild = B_FALSE; | |
10278 | cv_signal(&l2arc_rebuild_thr_cv); | |
10279 | mutex_exit(&l2arc_rebuild_thr_lock); | |
10280 | err = SET_ERROR(ECANCELED); | |
10281 | goto out; | |
10282 | } | |
10283 | mutex_exit(&l2arc_rebuild_thr_lock); | |
10284 | if (spa_config_tryenter(spa, SCL_L2ARC, vd, | |
10285 | RW_READER)) { | |
10286 | lock_held = B_TRUE; | |
10287 | break; | |
10288 | } | |
10289 | /* | |
10290 | * L2ARC config lock held by somebody in writer, | |
10291 | * possibly due to them trying to remove us. They'll | |
10292 | * likely to want us to shut down, so after a little | |
10293 | * delay, we check l2ad_rebuild_cancel and retry | |
10294 | * the lock again. | |
10295 | */ | |
10296 | delay(1); | |
10297 | } | |
10298 | ||
10299 | /* | |
10300 | * Continue with the next log block. | |
10301 | */ | |
10302 | lbps[0] = lbps[1]; | |
10303 | lbps[1] = this_lb->lb_prev_lbp; | |
10304 | PTR_SWAP(this_lb, next_lb); | |
10305 | this_io = next_io; | |
10306 | next_io = NULL; | |
a76e4e67 | 10307 | } |
77f6826b GA |
10308 | |
10309 | if (this_io != NULL) | |
10310 | l2arc_log_blk_fetch_abort(this_io); | |
10311 | out: | |
10312 | if (next_io != NULL) | |
10313 | l2arc_log_blk_fetch_abort(next_io); | |
10314 | vmem_free(this_lb, sizeof (*this_lb)); | |
10315 | vmem_free(next_lb, sizeof (*next_lb)); | |
10316 | ||
10317 | if (!l2arc_rebuild_enabled) { | |
657fd33b GA |
10318 | spa_history_log_internal(spa, "L2ARC rebuild", NULL, |
10319 | "disabled"); | |
10320 | } else if (err == 0 && zfs_refcount_count(&dev->l2ad_lb_count) > 0) { | |
77f6826b | 10321 | ARCSTAT_BUMP(arcstat_l2_rebuild_success); |
657fd33b GA |
10322 | spa_history_log_internal(spa, "L2ARC rebuild", NULL, |
10323 | "successful, restored %llu blocks", | |
10324 | (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count)); | |
10325 | } else if (err == 0 && zfs_refcount_count(&dev->l2ad_lb_count) == 0) { | |
10326 | /* | |
10327 | * No error but also nothing restored, meaning the lbps array | |
10328 | * in the device header points to invalid/non-present log | |
10329 | * blocks. Reset the header. | |
10330 | */ | |
10331 | spa_history_log_internal(spa, "L2ARC rebuild", NULL, | |
10332 | "no valid log blocks"); | |
10333 | bzero(l2dhdr, dev->l2ad_dev_hdr_asize); | |
10334 | l2arc_dev_hdr_update(dev); | |
da60484d GA |
10335 | } else if (err == ECANCELED) { |
10336 | /* | |
10337 | * In case the rebuild was canceled do not log to spa history | |
10338 | * log as the pool may be in the process of being removed. | |
10339 | */ | |
10340 | zfs_dbgmsg("L2ARC rebuild aborted, restored %llu blocks", | |
8e739b2c | 10341 | (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count)); |
77f6826b | 10342 | } else if (err != 0) { |
657fd33b GA |
10343 | spa_history_log_internal(spa, "L2ARC rebuild", NULL, |
10344 | "aborted, restored %llu blocks", | |
10345 | (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count)); | |
77f6826b GA |
10346 | } |
10347 | ||
10348 | if (lock_held) | |
10349 | spa_config_exit(spa, SCL_L2ARC, vd); | |
10350 | ||
10351 | return (err); | |
10352 | } | |
10353 | ||
10354 | /* | |
10355 | * Attempts to read the device header on the provided L2ARC device and writes | |
10356 | * it to `hdr'. On success, this function returns 0, otherwise the appropriate | |
10357 | * error code is returned. | |
10358 | */ | |
10359 | static int | |
10360 | l2arc_dev_hdr_read(l2arc_dev_t *dev) | |
10361 | { | |
10362 | int err; | |
10363 | uint64_t guid; | |
10364 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; | |
10365 | const uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize; | |
10366 | abd_t *abd; | |
10367 | ||
10368 | guid = spa_guid(dev->l2ad_vdev->vdev_spa); | |
10369 | ||
10370 | abd = abd_get_from_buf(l2dhdr, l2dhdr_asize); | |
10371 | ||
10372 | err = zio_wait(zio_read_phys(NULL, dev->l2ad_vdev, | |
10373 | VDEV_LABEL_START_SIZE, l2dhdr_asize, abd, | |
a76e4e67 | 10374 | ZIO_CHECKSUM_LABEL, NULL, NULL, ZIO_PRIORITY_SYNC_READ, |
77f6826b GA |
10375 | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | |
10376 | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY | | |
10377 | ZIO_FLAG_SPECULATIVE, B_FALSE)); | |
10378 | ||
e2af2acc | 10379 | abd_free(abd); |
77f6826b GA |
10380 | |
10381 | if (err != 0) { | |
10382 | ARCSTAT_BUMP(arcstat_l2_rebuild_abort_dh_errors); | |
10383 | zfs_dbgmsg("L2ARC IO error (%d) while reading device header, " | |
8e739b2c RE |
10384 | "vdev guid: %llu", err, |
10385 | (u_longlong_t)dev->l2ad_vdev->vdev_guid); | |
77f6826b GA |
10386 | return (err); |
10387 | } | |
10388 | ||
10389 | if (l2dhdr->dh_magic == BSWAP_64(L2ARC_DEV_HDR_MAGIC)) | |
10390 | byteswap_uint64_array(l2dhdr, sizeof (*l2dhdr)); | |
10391 | ||
10392 | if (l2dhdr->dh_magic != L2ARC_DEV_HDR_MAGIC || | |
10393 | l2dhdr->dh_spa_guid != guid || | |
10394 | l2dhdr->dh_vdev_guid != dev->l2ad_vdev->vdev_guid || | |
10395 | l2dhdr->dh_version != L2ARC_PERSISTENT_VERSION || | |
657fd33b | 10396 | l2dhdr->dh_log_entries != dev->l2ad_log_entries || |
77f6826b GA |
10397 | l2dhdr->dh_end != dev->l2ad_end || |
10398 | !l2arc_range_check_overlap(dev->l2ad_start, dev->l2ad_end, | |
b7654bd7 GA |
10399 | l2dhdr->dh_evict) || |
10400 | (l2dhdr->dh_trim_state != VDEV_TRIM_COMPLETE && | |
10401 | l2arc_trim_ahead > 0)) { | |
77f6826b GA |
10402 | /* |
10403 | * Attempt to rebuild a device containing no actual dev hdr | |
10404 | * or containing a header from some other pool or from another | |
10405 | * version of persistent L2ARC. | |
10406 | */ | |
10407 | ARCSTAT_BUMP(arcstat_l2_rebuild_abort_unsupported); | |
10408 | return (SET_ERROR(ENOTSUP)); | |
10409 | } | |
10410 | ||
10411 | return (0); | |
10412 | } | |
10413 | ||
10414 | /* | |
10415 | * Reads L2ARC log blocks from storage and validates their contents. | |
10416 | * | |
10417 | * This function implements a simple fetcher to make sure that while | |
10418 | * we're processing one buffer the L2ARC is already fetching the next | |
10419 | * one in the chain. | |
10420 | * | |
10421 | * The arguments this_lp and next_lp point to the current and next log block | |
10422 | * address in the block chain. Similarly, this_lb and next_lb hold the | |
10423 | * l2arc_log_blk_phys_t's of the current and next L2ARC blk. | |
10424 | * | |
10425 | * The `this_io' and `next_io' arguments are used for block fetching. | |
10426 | * When issuing the first blk IO during rebuild, you should pass NULL for | |
10427 | * `this_io'. This function will then issue a sync IO to read the block and | |
10428 | * also issue an async IO to fetch the next block in the block chain. The | |
10429 | * fetched IO is returned in `next_io'. On subsequent calls to this | |
10430 | * function, pass the value returned in `next_io' from the previous call | |
10431 | * as `this_io' and a fresh `next_io' pointer to hold the next fetch IO. | |
10432 | * Prior to the call, you should initialize your `next_io' pointer to be | |
10433 | * NULL. If no fetch IO was issued, the pointer is left set at NULL. | |
10434 | * | |
10435 | * On success, this function returns 0, otherwise it returns an appropriate | |
10436 | * error code. On error the fetching IO is aborted and cleared before | |
10437 | * returning from this function. Therefore, if we return `success', the | |
10438 | * caller can assume that we have taken care of cleanup of fetch IOs. | |
10439 | */ | |
10440 | static int | |
10441 | l2arc_log_blk_read(l2arc_dev_t *dev, | |
10442 | const l2arc_log_blkptr_t *this_lbp, const l2arc_log_blkptr_t *next_lbp, | |
10443 | l2arc_log_blk_phys_t *this_lb, l2arc_log_blk_phys_t *next_lb, | |
10444 | zio_t *this_io, zio_t **next_io) | |
10445 | { | |
10446 | int err = 0; | |
10447 | zio_cksum_t cksum; | |
10448 | abd_t *abd = NULL; | |
657fd33b | 10449 | uint64_t asize; |
77f6826b GA |
10450 | |
10451 | ASSERT(this_lbp != NULL && next_lbp != NULL); | |
10452 | ASSERT(this_lb != NULL && next_lb != NULL); | |
10453 | ASSERT(next_io != NULL && *next_io == NULL); | |
10454 | ASSERT(l2arc_log_blkptr_valid(dev, this_lbp)); | |
10455 | ||
10456 | /* | |
10457 | * Check to see if we have issued the IO for this log block in a | |
10458 | * previous run. If not, this is the first call, so issue it now. | |
10459 | */ | |
10460 | if (this_io == NULL) { | |
10461 | this_io = l2arc_log_blk_fetch(dev->l2ad_vdev, this_lbp, | |
10462 | this_lb); | |
10463 | } | |
10464 | ||
10465 | /* | |
10466 | * Peek to see if we can start issuing the next IO immediately. | |
10467 | */ | |
10468 | if (l2arc_log_blkptr_valid(dev, next_lbp)) { | |
10469 | /* | |
10470 | * Start issuing IO for the next log block early - this | |
10471 | * should help keep the L2ARC device busy while we | |
10472 | * decompress and restore this log block. | |
10473 | */ | |
10474 | *next_io = l2arc_log_blk_fetch(dev->l2ad_vdev, next_lbp, | |
10475 | next_lb); | |
10476 | } | |
10477 | ||
10478 | /* Wait for the IO to read this log block to complete */ | |
10479 | if ((err = zio_wait(this_io)) != 0) { | |
10480 | ARCSTAT_BUMP(arcstat_l2_rebuild_abort_io_errors); | |
10481 | zfs_dbgmsg("L2ARC IO error (%d) while reading log block, " | |
8e739b2c RE |
10482 | "offset: %llu, vdev guid: %llu", err, |
10483 | (u_longlong_t)this_lbp->lbp_daddr, | |
10484 | (u_longlong_t)dev->l2ad_vdev->vdev_guid); | |
77f6826b GA |
10485 | goto cleanup; |
10486 | } | |
10487 | ||
657fd33b GA |
10488 | /* |
10489 | * Make sure the buffer checks out. | |
10490 | * L2BLK_GET_PSIZE returns aligned size for log blocks. | |
10491 | */ | |
10492 | asize = L2BLK_GET_PSIZE((this_lbp)->lbp_prop); | |
10493 | fletcher_4_native(this_lb, asize, NULL, &cksum); | |
77f6826b GA |
10494 | if (!ZIO_CHECKSUM_EQUAL(cksum, this_lbp->lbp_cksum)) { |
10495 | ARCSTAT_BUMP(arcstat_l2_rebuild_abort_cksum_lb_errors); | |
10496 | zfs_dbgmsg("L2ARC log block cksum failed, offset: %llu, " | |
10497 | "vdev guid: %llu, l2ad_hand: %llu, l2ad_evict: %llu", | |
8e739b2c RE |
10498 | (u_longlong_t)this_lbp->lbp_daddr, |
10499 | (u_longlong_t)dev->l2ad_vdev->vdev_guid, | |
10500 | (u_longlong_t)dev->l2ad_hand, | |
10501 | (u_longlong_t)dev->l2ad_evict); | |
77f6826b GA |
10502 | err = SET_ERROR(ECKSUM); |
10503 | goto cleanup; | |
10504 | } | |
10505 | ||
10506 | /* Now we can take our time decoding this buffer */ | |
10507 | switch (L2BLK_GET_COMPRESS((this_lbp)->lbp_prop)) { | |
10508 | case ZIO_COMPRESS_OFF: | |
10509 | break; | |
10510 | case ZIO_COMPRESS_LZ4: | |
657fd33b GA |
10511 | abd = abd_alloc_for_io(asize, B_TRUE); |
10512 | abd_copy_from_buf_off(abd, this_lb, 0, asize); | |
77f6826b GA |
10513 | if ((err = zio_decompress_data( |
10514 | L2BLK_GET_COMPRESS((this_lbp)->lbp_prop), | |
10b3c7f5 | 10515 | abd, this_lb, asize, sizeof (*this_lb), NULL)) != 0) { |
77f6826b GA |
10516 | err = SET_ERROR(EINVAL); |
10517 | goto cleanup; | |
10518 | } | |
10519 | break; | |
10520 | default: | |
10521 | err = SET_ERROR(EINVAL); | |
10522 | goto cleanup; | |
10523 | } | |
10524 | if (this_lb->lb_magic == BSWAP_64(L2ARC_LOG_BLK_MAGIC)) | |
10525 | byteswap_uint64_array(this_lb, sizeof (*this_lb)); | |
10526 | if (this_lb->lb_magic != L2ARC_LOG_BLK_MAGIC) { | |
10527 | err = SET_ERROR(EINVAL); | |
10528 | goto cleanup; | |
10529 | } | |
10530 | cleanup: | |
10531 | /* Abort an in-flight fetch I/O in case of error */ | |
10532 | if (err != 0 && *next_io != NULL) { | |
10533 | l2arc_log_blk_fetch_abort(*next_io); | |
10534 | *next_io = NULL; | |
10535 | } | |
10536 | if (abd != NULL) | |
10537 | abd_free(abd); | |
10538 | return (err); | |
10539 | } | |
10540 | ||
10541 | /* | |
10542 | * Restores the payload of a log block to ARC. This creates empty ARC hdr | |
10543 | * entries which only contain an l2arc hdr, essentially restoring the | |
10544 | * buffers to their L2ARC evicted state. This function also updates space | |
10545 | * usage on the L2ARC vdev to make sure it tracks restored buffers. | |
10546 | */ | |
10547 | static void | |
10548 | l2arc_log_blk_restore(l2arc_dev_t *dev, const l2arc_log_blk_phys_t *lb, | |
a76e4e67 | 10549 | uint64_t lb_asize) |
77f6826b | 10550 | { |
657fd33b GA |
10551 | uint64_t size = 0, asize = 0; |
10552 | uint64_t log_entries = dev->l2ad_log_entries; | |
77f6826b | 10553 | |
523e1295 AM |
10554 | /* |
10555 | * Usually arc_adapt() is called only for data, not headers, but | |
10556 | * since we may allocate significant amount of memory here, let ARC | |
10557 | * grow its arc_c. | |
10558 | */ | |
10559 | arc_adapt(log_entries * HDR_L2ONLY_SIZE, arc_l2c_only); | |
10560 | ||
77f6826b GA |
10561 | for (int i = log_entries - 1; i >= 0; i--) { |
10562 | /* | |
10563 | * Restore goes in the reverse temporal direction to preserve | |
10564 | * correct temporal ordering of buffers in the l2ad_buflist. | |
10565 | * l2arc_hdr_restore also does a list_insert_tail instead of | |
10566 | * list_insert_head on the l2ad_buflist: | |
10567 | * | |
10568 | * LIST l2ad_buflist LIST | |
10569 | * HEAD <------ (time) ------ TAIL | |
10570 | * direction +-----+-----+-----+-----+-----+ direction | |
10571 | * of l2arc <== | buf | buf | buf | buf | buf | ===> of rebuild | |
10572 | * fill +-----+-----+-----+-----+-----+ | |
10573 | * ^ ^ | |
10574 | * | | | |
10575 | * | | | |
657fd33b GA |
10576 | * l2arc_feed_thread l2arc_rebuild |
10577 | * will place new bufs here restores bufs here | |
77f6826b | 10578 | * |
657fd33b GA |
10579 | * During l2arc_rebuild() the device is not used by |
10580 | * l2arc_feed_thread() as dev->l2ad_rebuild is set to true. | |
77f6826b GA |
10581 | */ |
10582 | size += L2BLK_GET_LSIZE((&lb->lb_entries[i])->le_prop); | |
657fd33b GA |
10583 | asize += vdev_psize_to_asize(dev->l2ad_vdev, |
10584 | L2BLK_GET_PSIZE((&lb->lb_entries[i])->le_prop)); | |
77f6826b GA |
10585 | l2arc_hdr_restore(&lb->lb_entries[i], dev); |
10586 | } | |
10587 | ||
10588 | /* | |
10589 | * Record rebuild stats: | |
10590 | * size Logical size of restored buffers in the L2ARC | |
657fd33b | 10591 | * asize Aligned size of restored buffers in the L2ARC |
77f6826b GA |
10592 | */ |
10593 | ARCSTAT_INCR(arcstat_l2_rebuild_size, size); | |
657fd33b | 10594 | ARCSTAT_INCR(arcstat_l2_rebuild_asize, asize); |
77f6826b | 10595 | ARCSTAT_INCR(arcstat_l2_rebuild_bufs, log_entries); |
657fd33b GA |
10596 | ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize, lb_asize); |
10597 | ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio, asize / lb_asize); | |
77f6826b GA |
10598 | ARCSTAT_BUMP(arcstat_l2_rebuild_log_blks); |
10599 | } | |
10600 | ||
10601 | /* | |
10602 | * Restores a single ARC buf hdr from a log entry. The ARC buffer is put | |
10603 | * into a state indicating that it has been evicted to L2ARC. | |
10604 | */ | |
10605 | static void | |
10606 | l2arc_hdr_restore(const l2arc_log_ent_phys_t *le, l2arc_dev_t *dev) | |
10607 | { | |
10608 | arc_buf_hdr_t *hdr, *exists; | |
10609 | kmutex_t *hash_lock; | |
10610 | arc_buf_contents_t type = L2BLK_GET_TYPE((le)->le_prop); | |
10611 | uint64_t asize; | |
10612 | ||
10613 | /* | |
10614 | * Do all the allocation before grabbing any locks, this lets us | |
10615 | * sleep if memory is full and we don't have to deal with failed | |
10616 | * allocations. | |
10617 | */ | |
10618 | hdr = arc_buf_alloc_l2only(L2BLK_GET_LSIZE((le)->le_prop), type, | |
10619 | dev, le->le_dva, le->le_daddr, | |
10620 | L2BLK_GET_PSIZE((le)->le_prop), le->le_birth, | |
10b3c7f5 | 10621 | L2BLK_GET_COMPRESS((le)->le_prop), le->le_complevel, |
77f6826b | 10622 | L2BLK_GET_PROTECTED((le)->le_prop), |
08532162 GA |
10623 | L2BLK_GET_PREFETCH((le)->le_prop), |
10624 | L2BLK_GET_STATE((le)->le_prop)); | |
77f6826b GA |
10625 | asize = vdev_psize_to_asize(dev->l2ad_vdev, |
10626 | L2BLK_GET_PSIZE((le)->le_prop)); | |
10627 | ||
10628 | /* | |
10629 | * vdev_space_update() has to be called before arc_hdr_destroy() to | |
08532162 | 10630 | * avoid underflow since the latter also calls vdev_space_update(). |
77f6826b | 10631 | */ |
08532162 | 10632 | l2arc_hdr_arcstats_increment(hdr); |
77f6826b GA |
10633 | vdev_space_update(dev->l2ad_vdev, asize, 0, 0); |
10634 | ||
77f6826b GA |
10635 | mutex_enter(&dev->l2ad_mtx); |
10636 | list_insert_tail(&dev->l2ad_buflist, hdr); | |
10637 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, arc_hdr_size(hdr), hdr); | |
10638 | mutex_exit(&dev->l2ad_mtx); | |
10639 | ||
10640 | exists = buf_hash_insert(hdr, &hash_lock); | |
10641 | if (exists) { | |
10642 | /* Buffer was already cached, no need to restore it. */ | |
10643 | arc_hdr_destroy(hdr); | |
10644 | /* | |
10645 | * If the buffer is already cached, check whether it has | |
10646 | * L2ARC metadata. If not, enter them and update the flag. | |
10647 | * This is important is case of onlining a cache device, since | |
10648 | * we previously evicted all L2ARC metadata from ARC. | |
10649 | */ | |
10650 | if (!HDR_HAS_L2HDR(exists)) { | |
10651 | arc_hdr_set_flags(exists, ARC_FLAG_HAS_L2HDR); | |
10652 | exists->b_l2hdr.b_dev = dev; | |
10653 | exists->b_l2hdr.b_daddr = le->le_daddr; | |
08532162 GA |
10654 | exists->b_l2hdr.b_arcs_state = |
10655 | L2BLK_GET_STATE((le)->le_prop); | |
77f6826b GA |
10656 | mutex_enter(&dev->l2ad_mtx); |
10657 | list_insert_tail(&dev->l2ad_buflist, exists); | |
10658 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, | |
10659 | arc_hdr_size(exists), exists); | |
10660 | mutex_exit(&dev->l2ad_mtx); | |
08532162 | 10661 | l2arc_hdr_arcstats_increment(exists); |
77f6826b | 10662 | vdev_space_update(dev->l2ad_vdev, asize, 0, 0); |
77f6826b GA |
10663 | } |
10664 | ARCSTAT_BUMP(arcstat_l2_rebuild_bufs_precached); | |
10665 | } | |
10666 | ||
10667 | mutex_exit(hash_lock); | |
10668 | } | |
10669 | ||
10670 | /* | |
10671 | * Starts an asynchronous read IO to read a log block. This is used in log | |
10672 | * block reconstruction to start reading the next block before we are done | |
10673 | * decoding and reconstructing the current block, to keep the l2arc device | |
10674 | * nice and hot with read IO to process. | |
10675 | * The returned zio will contain a newly allocated memory buffers for the IO | |
10676 | * data which should then be freed by the caller once the zio is no longer | |
10677 | * needed (i.e. due to it having completed). If you wish to abort this | |
10678 | * zio, you should do so using l2arc_log_blk_fetch_abort, which takes | |
10679 | * care of disposing of the allocated buffers correctly. | |
10680 | */ | |
10681 | static zio_t * | |
10682 | l2arc_log_blk_fetch(vdev_t *vd, const l2arc_log_blkptr_t *lbp, | |
10683 | l2arc_log_blk_phys_t *lb) | |
10684 | { | |
657fd33b | 10685 | uint32_t asize; |
77f6826b GA |
10686 | zio_t *pio; |
10687 | l2arc_read_callback_t *cb; | |
10688 | ||
657fd33b GA |
10689 | /* L2BLK_GET_PSIZE returns aligned size for log blocks */ |
10690 | asize = L2BLK_GET_PSIZE((lbp)->lbp_prop); | |
10691 | ASSERT(asize <= sizeof (l2arc_log_blk_phys_t)); | |
10692 | ||
77f6826b | 10693 | cb = kmem_zalloc(sizeof (l2arc_read_callback_t), KM_SLEEP); |
657fd33b | 10694 | cb->l2rcb_abd = abd_get_from_buf(lb, asize); |
77f6826b GA |
10695 | pio = zio_root(vd->vdev_spa, l2arc_blk_fetch_done, cb, |
10696 | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | | |
10697 | ZIO_FLAG_DONT_RETRY); | |
657fd33b | 10698 | (void) zio_nowait(zio_read_phys(pio, vd, lbp->lbp_daddr, asize, |
77f6826b GA |
10699 | cb->l2rcb_abd, ZIO_CHECKSUM_OFF, NULL, NULL, |
10700 | ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL | | |
10701 | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY, B_FALSE)); | |
10702 | ||
10703 | return (pio); | |
10704 | } | |
10705 | ||
10706 | /* | |
10707 | * Aborts a zio returned from l2arc_log_blk_fetch and frees the data | |
10708 | * buffers allocated for it. | |
10709 | */ | |
10710 | static void | |
10711 | l2arc_log_blk_fetch_abort(zio_t *zio) | |
10712 | { | |
10713 | (void) zio_wait(zio); | |
10714 | } | |
10715 | ||
10716 | /* | |
2054f35e | 10717 | * Creates a zio to update the device header on an l2arc device. |
77f6826b | 10718 | */ |
b7654bd7 | 10719 | void |
77f6826b GA |
10720 | l2arc_dev_hdr_update(l2arc_dev_t *dev) |
10721 | { | |
10722 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; | |
10723 | const uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize; | |
10724 | abd_t *abd; | |
10725 | int err; | |
10726 | ||
657fd33b GA |
10727 | VERIFY(spa_config_held(dev->l2ad_spa, SCL_STATE_ALL, RW_READER)); |
10728 | ||
77f6826b GA |
10729 | l2dhdr->dh_magic = L2ARC_DEV_HDR_MAGIC; |
10730 | l2dhdr->dh_version = L2ARC_PERSISTENT_VERSION; | |
10731 | l2dhdr->dh_spa_guid = spa_guid(dev->l2ad_vdev->vdev_spa); | |
10732 | l2dhdr->dh_vdev_guid = dev->l2ad_vdev->vdev_guid; | |
657fd33b | 10733 | l2dhdr->dh_log_entries = dev->l2ad_log_entries; |
77f6826b GA |
10734 | l2dhdr->dh_evict = dev->l2ad_evict; |
10735 | l2dhdr->dh_start = dev->l2ad_start; | |
10736 | l2dhdr->dh_end = dev->l2ad_end; | |
657fd33b GA |
10737 | l2dhdr->dh_lb_asize = zfs_refcount_count(&dev->l2ad_lb_asize); |
10738 | l2dhdr->dh_lb_count = zfs_refcount_count(&dev->l2ad_lb_count); | |
77f6826b | 10739 | l2dhdr->dh_flags = 0; |
b7654bd7 GA |
10740 | l2dhdr->dh_trim_action_time = dev->l2ad_vdev->vdev_trim_action_time; |
10741 | l2dhdr->dh_trim_state = dev->l2ad_vdev->vdev_trim_state; | |
77f6826b GA |
10742 | if (dev->l2ad_first) |
10743 | l2dhdr->dh_flags |= L2ARC_DEV_HDR_EVICT_FIRST; | |
10744 | ||
10745 | abd = abd_get_from_buf(l2dhdr, l2dhdr_asize); | |
10746 | ||
10747 | err = zio_wait(zio_write_phys(NULL, dev->l2ad_vdev, | |
10748 | VDEV_LABEL_START_SIZE, l2dhdr_asize, abd, ZIO_CHECKSUM_LABEL, NULL, | |
10749 | NULL, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_CANFAIL, B_FALSE)); | |
10750 | ||
e2af2acc | 10751 | abd_free(abd); |
77f6826b GA |
10752 | |
10753 | if (err != 0) { | |
10754 | zfs_dbgmsg("L2ARC IO error (%d) while writing device header, " | |
8e739b2c RE |
10755 | "vdev guid: %llu", err, |
10756 | (u_longlong_t)dev->l2ad_vdev->vdev_guid); | |
77f6826b GA |
10757 | } |
10758 | } | |
10759 | ||
10760 | /* | |
10761 | * Commits a log block to the L2ARC device. This routine is invoked from | |
10762 | * l2arc_write_buffers when the log block fills up. | |
10763 | * This function allocates some memory to temporarily hold the serialized | |
10764 | * buffer to be written. This is then released in l2arc_write_done. | |
10765 | */ | |
10766 | static void | |
10767 | l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio, l2arc_write_callback_t *cb) | |
10768 | { | |
10769 | l2arc_log_blk_phys_t *lb = &dev->l2ad_log_blk; | |
10770 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; | |
10771 | uint64_t psize, asize; | |
10772 | zio_t *wzio; | |
10773 | l2arc_lb_abd_buf_t *abd_buf; | |
10774 | uint8_t *tmpbuf; | |
10775 | l2arc_lb_ptr_buf_t *lb_ptr_buf; | |
10776 | ||
657fd33b | 10777 | VERIFY3S(dev->l2ad_log_ent_idx, ==, dev->l2ad_log_entries); |
77f6826b GA |
10778 | |
10779 | tmpbuf = zio_buf_alloc(sizeof (*lb)); | |
10780 | abd_buf = zio_buf_alloc(sizeof (*abd_buf)); | |
10781 | abd_buf->abd = abd_get_from_buf(lb, sizeof (*lb)); | |
10782 | lb_ptr_buf = kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t), KM_SLEEP); | |
10783 | lb_ptr_buf->lb_ptr = kmem_zalloc(sizeof (l2arc_log_blkptr_t), KM_SLEEP); | |
10784 | ||
10785 | /* link the buffer into the block chain */ | |
10786 | lb->lb_prev_lbp = l2dhdr->dh_start_lbps[1]; | |
10787 | lb->lb_magic = L2ARC_LOG_BLK_MAGIC; | |
10788 | ||
657fd33b GA |
10789 | /* |
10790 | * l2arc_log_blk_commit() may be called multiple times during a single | |
10791 | * l2arc_write_buffers() call. Save the allocated abd buffers in a list | |
10792 | * so we can free them in l2arc_write_done() later on. | |
10793 | */ | |
77f6826b | 10794 | list_insert_tail(&cb->l2wcb_abd_list, abd_buf); |
657fd33b GA |
10795 | |
10796 | /* try to compress the buffer */ | |
77f6826b | 10797 | psize = zio_compress_data(ZIO_COMPRESS_LZ4, |
10b3c7f5 | 10798 | abd_buf->abd, tmpbuf, sizeof (*lb), 0); |
77f6826b GA |
10799 | |
10800 | /* a log block is never entirely zero */ | |
10801 | ASSERT(psize != 0); | |
10802 | asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
10803 | ASSERT(asize <= sizeof (*lb)); | |
10804 | ||
10805 | /* | |
10806 | * Update the start log block pointer in the device header to point | |
10807 | * to the log block we're about to write. | |
10808 | */ | |
10809 | l2dhdr->dh_start_lbps[1] = l2dhdr->dh_start_lbps[0]; | |
10810 | l2dhdr->dh_start_lbps[0].lbp_daddr = dev->l2ad_hand; | |
10811 | l2dhdr->dh_start_lbps[0].lbp_payload_asize = | |
10812 | dev->l2ad_log_blk_payload_asize; | |
10813 | l2dhdr->dh_start_lbps[0].lbp_payload_start = | |
10814 | dev->l2ad_log_blk_payload_start; | |
77f6826b GA |
10815 | L2BLK_SET_LSIZE( |
10816 | (&l2dhdr->dh_start_lbps[0])->lbp_prop, sizeof (*lb)); | |
10817 | L2BLK_SET_PSIZE( | |
10818 | (&l2dhdr->dh_start_lbps[0])->lbp_prop, asize); | |
10819 | L2BLK_SET_CHECKSUM( | |
10820 | (&l2dhdr->dh_start_lbps[0])->lbp_prop, | |
10821 | ZIO_CHECKSUM_FLETCHER_4); | |
10822 | if (asize < sizeof (*lb)) { | |
10823 | /* compression succeeded */ | |
10824 | bzero(tmpbuf + psize, asize - psize); | |
10825 | L2BLK_SET_COMPRESS( | |
10826 | (&l2dhdr->dh_start_lbps[0])->lbp_prop, | |
10827 | ZIO_COMPRESS_LZ4); | |
10828 | } else { | |
10829 | /* compression failed */ | |
10830 | bcopy(lb, tmpbuf, sizeof (*lb)); | |
10831 | L2BLK_SET_COMPRESS( | |
10832 | (&l2dhdr->dh_start_lbps[0])->lbp_prop, | |
10833 | ZIO_COMPRESS_OFF); | |
10834 | } | |
10835 | ||
10836 | /* checksum what we're about to write */ | |
10837 | fletcher_4_native(tmpbuf, asize, NULL, | |
10838 | &l2dhdr->dh_start_lbps[0].lbp_cksum); | |
10839 | ||
e2af2acc | 10840 | abd_free(abd_buf->abd); |
77f6826b GA |
10841 | |
10842 | /* perform the write itself */ | |
10843 | abd_buf->abd = abd_get_from_buf(tmpbuf, sizeof (*lb)); | |
10844 | abd_take_ownership_of_buf(abd_buf->abd, B_TRUE); | |
10845 | wzio = zio_write_phys(pio, dev->l2ad_vdev, dev->l2ad_hand, | |
10846 | asize, abd_buf->abd, ZIO_CHECKSUM_OFF, NULL, NULL, | |
10847 | ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_CANFAIL, B_FALSE); | |
10848 | DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, zio_t *, wzio); | |
10849 | (void) zio_nowait(wzio); | |
10850 | ||
10851 | dev->l2ad_hand += asize; | |
10852 | /* | |
10853 | * Include the committed log block's pointer in the list of pointers | |
10854 | * to log blocks present in the L2ARC device. | |
10855 | */ | |
10856 | bcopy(&l2dhdr->dh_start_lbps[0], lb_ptr_buf->lb_ptr, | |
10857 | sizeof (l2arc_log_blkptr_t)); | |
10858 | mutex_enter(&dev->l2ad_mtx); | |
10859 | list_insert_head(&dev->l2ad_lbptr_list, lb_ptr_buf); | |
657fd33b GA |
10860 | ARCSTAT_INCR(arcstat_l2_log_blk_asize, asize); |
10861 | ARCSTAT_BUMP(arcstat_l2_log_blk_count); | |
10862 | zfs_refcount_add_many(&dev->l2ad_lb_asize, asize, lb_ptr_buf); | |
10863 | zfs_refcount_add(&dev->l2ad_lb_count, lb_ptr_buf); | |
77f6826b GA |
10864 | mutex_exit(&dev->l2ad_mtx); |
10865 | vdev_space_update(dev->l2ad_vdev, asize, 0, 0); | |
10866 | ||
10867 | /* bump the kstats */ | |
10868 | ARCSTAT_INCR(arcstat_l2_write_bytes, asize); | |
10869 | ARCSTAT_BUMP(arcstat_l2_log_blk_writes); | |
657fd33b | 10870 | ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize, asize); |
77f6826b GA |
10871 | ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio, |
10872 | dev->l2ad_log_blk_payload_asize / asize); | |
10873 | ||
10874 | /* start a new log block */ | |
10875 | dev->l2ad_log_ent_idx = 0; | |
10876 | dev->l2ad_log_blk_payload_asize = 0; | |
10877 | dev->l2ad_log_blk_payload_start = 0; | |
10878 | } | |
10879 | ||
10880 | /* | |
10881 | * Validates an L2ARC log block address to make sure that it can be read | |
10882 | * from the provided L2ARC device. | |
10883 | */ | |
10884 | boolean_t | |
10885 | l2arc_log_blkptr_valid(l2arc_dev_t *dev, const l2arc_log_blkptr_t *lbp) | |
10886 | { | |
657fd33b GA |
10887 | /* L2BLK_GET_PSIZE returns aligned size for log blocks */ |
10888 | uint64_t asize = L2BLK_GET_PSIZE((lbp)->lbp_prop); | |
10889 | uint64_t end = lbp->lbp_daddr + asize - 1; | |
77f6826b GA |
10890 | uint64_t start = lbp->lbp_payload_start; |
10891 | boolean_t evicted = B_FALSE; | |
10892 | ||
10893 | /* | |
10894 | * A log block is valid if all of the following conditions are true: | |
10895 | * - it fits entirely (including its payload) between l2ad_start and | |
10896 | * l2ad_end | |
10897 | * - it has a valid size | |
10898 | * - neither the log block itself nor part of its payload was evicted | |
10899 | * by l2arc_evict(): | |
10900 | * | |
10901 | * l2ad_hand l2ad_evict | |
10902 | * | | lbp_daddr | |
10903 | * | start | | end | |
10904 | * | | | | | | |
10905 | * V V V V V | |
10906 | * l2ad_start ============================================ l2ad_end | |
10907 | * --------------------------|||| | |
10908 | * ^ ^ | |
10909 | * | log block | |
10910 | * payload | |
10911 | */ | |
10912 | ||
10913 | evicted = | |
10914 | l2arc_range_check_overlap(start, end, dev->l2ad_hand) || | |
10915 | l2arc_range_check_overlap(start, end, dev->l2ad_evict) || | |
10916 | l2arc_range_check_overlap(dev->l2ad_hand, dev->l2ad_evict, start) || | |
10917 | l2arc_range_check_overlap(dev->l2ad_hand, dev->l2ad_evict, end); | |
10918 | ||
10919 | return (start >= dev->l2ad_start && end <= dev->l2ad_end && | |
657fd33b | 10920 | asize > 0 && asize <= sizeof (l2arc_log_blk_phys_t) && |
77f6826b GA |
10921 | (!evicted || dev->l2ad_first)); |
10922 | } | |
10923 | ||
10924 | /* | |
10925 | * Inserts ARC buffer header `hdr' into the current L2ARC log block on | |
10926 | * the device. The buffer being inserted must be present in L2ARC. | |
10927 | * Returns B_TRUE if the L2ARC log block is full and needs to be committed | |
10928 | * to L2ARC, or B_FALSE if it still has room for more ARC buffers. | |
10929 | */ | |
10930 | static boolean_t | |
10931 | l2arc_log_blk_insert(l2arc_dev_t *dev, const arc_buf_hdr_t *hdr) | |
10932 | { | |
10933 | l2arc_log_blk_phys_t *lb = &dev->l2ad_log_blk; | |
10934 | l2arc_log_ent_phys_t *le; | |
77f6826b | 10935 | |
657fd33b | 10936 | if (dev->l2ad_log_entries == 0) |
77f6826b GA |
10937 | return (B_FALSE); |
10938 | ||
10939 | int index = dev->l2ad_log_ent_idx++; | |
10940 | ||
657fd33b | 10941 | ASSERT3S(index, <, dev->l2ad_log_entries); |
77f6826b GA |
10942 | ASSERT(HDR_HAS_L2HDR(hdr)); |
10943 | ||
10944 | le = &lb->lb_entries[index]; | |
10945 | bzero(le, sizeof (*le)); | |
10946 | le->le_dva = hdr->b_dva; | |
10947 | le->le_birth = hdr->b_birth; | |
10948 | le->le_daddr = hdr->b_l2hdr.b_daddr; | |
10949 | if (index == 0) | |
10950 | dev->l2ad_log_blk_payload_start = le->le_daddr; | |
10951 | L2BLK_SET_LSIZE((le)->le_prop, HDR_GET_LSIZE(hdr)); | |
10952 | L2BLK_SET_PSIZE((le)->le_prop, HDR_GET_PSIZE(hdr)); | |
10953 | L2BLK_SET_COMPRESS((le)->le_prop, HDR_GET_COMPRESS(hdr)); | |
10b3c7f5 | 10954 | le->le_complevel = hdr->b_complevel; |
77f6826b GA |
10955 | L2BLK_SET_TYPE((le)->le_prop, hdr->b_type); |
10956 | L2BLK_SET_PROTECTED((le)->le_prop, !!(HDR_PROTECTED(hdr))); | |
10957 | L2BLK_SET_PREFETCH((le)->le_prop, !!(HDR_PREFETCH(hdr))); | |
08532162 | 10958 | L2BLK_SET_STATE((le)->le_prop, hdr->b_l1hdr.b_state->arcs_state); |
77f6826b GA |
10959 | |
10960 | dev->l2ad_log_blk_payload_asize += vdev_psize_to_asize(dev->l2ad_vdev, | |
10961 | HDR_GET_PSIZE(hdr)); | |
10962 | ||
657fd33b | 10963 | return (dev->l2ad_log_ent_idx == dev->l2ad_log_entries); |
77f6826b GA |
10964 | } |
10965 | ||
10966 | /* | |
10967 | * Checks whether a given L2ARC device address sits in a time-sequential | |
10968 | * range. The trick here is that the L2ARC is a rotary buffer, so we can't | |
10969 | * just do a range comparison, we need to handle the situation in which the | |
10970 | * range wraps around the end of the L2ARC device. Arguments: | |
10971 | * bottom -- Lower end of the range to check (written to earlier). | |
10972 | * top -- Upper end of the range to check (written to later). | |
10973 | * check -- The address for which we want to determine if it sits in | |
10974 | * between the top and bottom. | |
10975 | * | |
10976 | * The 3-way conditional below represents the following cases: | |
10977 | * | |
10978 | * bottom < top : Sequentially ordered case: | |
10979 | * <check>--------+-------------------+ | |
10980 | * | (overlap here?) | | |
10981 | * L2ARC dev V V | |
10982 | * |---------------<bottom>============<top>--------------| | |
10983 | * | |
10984 | * bottom > top: Looped-around case: | |
10985 | * <check>--------+------------------+ | |
10986 | * | (overlap here?) | | |
10987 | * L2ARC dev V V | |
10988 | * |===============<top>---------------<bottom>===========| | |
10989 | * ^ ^ | |
10990 | * | (or here?) | | |
10991 | * +---------------+---------<check> | |
10992 | * | |
10993 | * top == bottom : Just a single address comparison. | |
10994 | */ | |
10995 | boolean_t | |
10996 | l2arc_range_check_overlap(uint64_t bottom, uint64_t top, uint64_t check) | |
10997 | { | |
10998 | if (bottom < top) | |
10999 | return (bottom <= check && check <= top); | |
11000 | else if (bottom > top) | |
11001 | return (check <= top || bottom <= check); | |
11002 | else | |
11003 | return (check == top); | |
11004 | } | |
11005 | ||
0f699108 AZ |
11006 | EXPORT_SYMBOL(arc_buf_size); |
11007 | EXPORT_SYMBOL(arc_write); | |
c28b2279 | 11008 | EXPORT_SYMBOL(arc_read); |
e0b0ca98 | 11009 | EXPORT_SYMBOL(arc_buf_info); |
c28b2279 | 11010 | EXPORT_SYMBOL(arc_getbuf_func); |
ab26409d BB |
11011 | EXPORT_SYMBOL(arc_add_prune_callback); |
11012 | EXPORT_SYMBOL(arc_remove_prune_callback); | |
c28b2279 | 11013 | |
02730c33 | 11014 | /* BEGIN CSTYLED */ |
e945e8d7 | 11015 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min, param_set_arc_min, |
e3570464 | 11016 | param_get_long, ZMOD_RW, "Min arc size"); |
c28b2279 | 11017 | |
e945e8d7 | 11018 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, max, param_set_arc_max, |
e3570464 | 11019 | param_get_long, ZMOD_RW, "Max arc size"); |
c28b2279 | 11020 | |
e3570464 | 11021 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, meta_limit, param_set_arc_long, |
11022 | param_get_long, ZMOD_RW, "Metadata limit for arc size"); | |
6a8f9b6b | 11023 | |
e3570464 | 11024 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, meta_limit_percent, |
11025 | param_set_arc_long, param_get_long, ZMOD_RW, | |
9907cc1c G |
11026 | "Percent of arc size for arc meta limit"); |
11027 | ||
e3570464 | 11028 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, meta_min, param_set_arc_long, |
11029 | param_get_long, ZMOD_RW, "Min arc metadata"); | |
ca0bf58d | 11030 | |
03fdcb9a MM |
11031 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, meta_prune, INT, ZMOD_RW, |
11032 | "Meta objects to scan for prune"); | |
c409e464 | 11033 | |
03fdcb9a | 11034 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, meta_adjust_restarts, INT, ZMOD_RW, |
5dd92909 | 11035 | "Limit number of restarts in arc_evict_meta"); |
bc888666 | 11036 | |
03fdcb9a MM |
11037 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, meta_strategy, INT, ZMOD_RW, |
11038 | "Meta reclaim strategy"); | |
f6046738 | 11039 | |
e3570464 | 11040 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, grow_retry, param_set_arc_int, |
11041 | param_get_int, ZMOD_RW, "Seconds before growing arc size"); | |
c409e464 | 11042 | |
03fdcb9a MM |
11043 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, p_dampener_disable, INT, ZMOD_RW, |
11044 | "Disable arc_p adapt dampener"); | |
62422785 | 11045 | |
e3570464 | 11046 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, shrink_shift, param_set_arc_int, |
11047 | param_get_int, ZMOD_RW, "log2(fraction of arc to reclaim)"); | |
c409e464 | 11048 | |
03fdcb9a | 11049 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, pc_percent, UINT, ZMOD_RW, |
03b60eee DB |
11050 | "Percent of pagecache to reclaim arc to"); |
11051 | ||
e3570464 | 11052 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, p_min_shift, param_set_arc_int, |
11053 | param_get_int, ZMOD_RW, "arc_c shift to calc min/max arc_p"); | |
728d6ae9 | 11054 | |
03fdcb9a MM |
11055 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, average_blocksize, INT, ZMOD_RD, |
11056 | "Target average block size"); | |
49ddb315 | 11057 | |
03fdcb9a MM |
11058 | ZFS_MODULE_PARAM(zfs, zfs_, compressed_arc_enabled, INT, ZMOD_RW, |
11059 | "Disable compressed arc buffers"); | |
d3c2ae1c | 11060 | |
e3570464 | 11061 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min_prefetch_ms, param_set_arc_int, |
11062 | param_get_int, ZMOD_RW, "Min life of prefetch block in ms"); | |
d4a72f23 | 11063 | |
e3570464 | 11064 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min_prescient_prefetch_ms, |
11065 | param_set_arc_int, param_get_int, ZMOD_RW, | |
d4a72f23 | 11066 | "Min life of prescient prefetched block in ms"); |
bce45ec9 | 11067 | |
03fdcb9a MM |
11068 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, write_max, ULONG, ZMOD_RW, |
11069 | "Max write bytes per interval"); | |
abd8610c | 11070 | |
03fdcb9a MM |
11071 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, write_boost, ULONG, ZMOD_RW, |
11072 | "Extra write bytes during device warmup"); | |
abd8610c | 11073 | |
03fdcb9a MM |
11074 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, headroom, ULONG, ZMOD_RW, |
11075 | "Number of max device writes to precache"); | |
abd8610c | 11076 | |
03fdcb9a MM |
11077 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, headroom_boost, ULONG, ZMOD_RW, |
11078 | "Compressed l2arc_headroom multiplier"); | |
3a17a7a9 | 11079 | |
b7654bd7 GA |
11080 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, trim_ahead, ULONG, ZMOD_RW, |
11081 | "TRIM ahead L2ARC write size multiplier"); | |
11082 | ||
03fdcb9a MM |
11083 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_secs, ULONG, ZMOD_RW, |
11084 | "Seconds between L2ARC writing"); | |
abd8610c | 11085 | |
03fdcb9a MM |
11086 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_min_ms, ULONG, ZMOD_RW, |
11087 | "Min feed interval in milliseconds"); | |
abd8610c | 11088 | |
03fdcb9a MM |
11089 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, noprefetch, INT, ZMOD_RW, |
11090 | "Skip caching prefetched buffers"); | |
abd8610c | 11091 | |
03fdcb9a MM |
11092 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_again, INT, ZMOD_RW, |
11093 | "Turbo L2ARC warmup"); | |
abd8610c | 11094 | |
03fdcb9a MM |
11095 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, norw, INT, ZMOD_RW, |
11096 | "No reads during writes"); | |
abd8610c | 11097 | |
523e1295 AM |
11098 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, meta_percent, INT, ZMOD_RW, |
11099 | "Percent of ARC size allowed for L2ARC-only headers"); | |
11100 | ||
77f6826b GA |
11101 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, rebuild_enabled, INT, ZMOD_RW, |
11102 | "Rebuild the L2ARC when importing a pool"); | |
11103 | ||
11104 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, rebuild_blocks_min_l2size, ULONG, ZMOD_RW, | |
11105 | "Min size in bytes to write rebuild log blocks in L2ARC"); | |
11106 | ||
feb3a7ee GA |
11107 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, mfuonly, INT, ZMOD_RW, |
11108 | "Cache only MFU data from ARC into L2ARC"); | |
11109 | ||
c9d62d13 GA |
11110 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, exclude_special, INT, ZMOD_RW, |
11111 | "If set to 1 exclude dbufs on special vdevs from being cached to " | |
11112 | "L2ARC."); | |
11113 | ||
e3570464 | 11114 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, lotsfree_percent, param_set_arc_int, |
11115 | param_get_int, ZMOD_RW, "System free memory I/O throttle in bytes"); | |
7e8bddd0 | 11116 | |
e3570464 | 11117 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, sys_free, param_set_arc_long, |
11118 | param_get_long, ZMOD_RW, "System free memory target size in bytes"); | |
11f552fa | 11119 | |
e3570464 | 11120 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, dnode_limit, param_set_arc_long, |
11121 | param_get_long, ZMOD_RW, "Minimum bytes of dnodes in arc"); | |
25458cbe | 11122 | |
e3570464 | 11123 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, dnode_limit_percent, |
11124 | param_set_arc_long, param_get_long, ZMOD_RW, | |
9907cc1c G |
11125 | "Percent of ARC meta buffers for dnodes"); |
11126 | ||
03fdcb9a | 11127 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, dnode_reduce_percent, ULONG, ZMOD_RW, |
25458cbe | 11128 | "Percentage of excess dnodes to try to unpin"); |
3442c2a0 MA |
11129 | |
11130 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, eviction_pct, INT, ZMOD_RW, | |
eb02a4c6 RM |
11131 | "When full, ARC allocation waits for eviction of this % of alloc size"); |
11132 | ||
11133 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, evict_batch_limit, INT, ZMOD_RW, | |
11134 | "The number of headers to evict per sublist before moving to the next"); | |
462217d1 AM |
11135 | |
11136 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, prune_task_threads, INT, ZMOD_RW, | |
11137 | "Number of arc_prune threads"); | |
02730c33 | 11138 | /* END CSTYLED */ |