]>
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 | |
1d3ba0bf | 9 | * or https://opensource.org/licenses/CDDL-1.0. |
34dc7c2f BB |
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 | 110 | * It as also possible to register a callback which is run when the |
a8d83e2a | 111 | * metadata limit is reached and no buffers can be safely evicted. In |
ab26409d | 112 | * this case the arc user should drop a reference on some arc buffers so |
a8d83e2a AM |
113 | * they can be reclaimed. For example, when using the ZPL each dentry |
114 | * holds a references on a znode. These dentries must be pruned before | |
115 | * the arc buffer holding the znode can be safely evicted. | |
ab26409d | 116 | * |
34dc7c2f BB |
117 | * Note that the majority of the performance stats are manipulated |
118 | * with atomic operations. | |
119 | * | |
b9541d6b | 120 | * The L2ARC uses the l2ad_mtx on each vdev for the following: |
34dc7c2f BB |
121 | * |
122 | * - L2ARC buflist creation | |
123 | * - L2ARC buflist eviction | |
124 | * - L2ARC write completion, which walks L2ARC buflists | |
125 | * - ARC header destruction, as it removes from L2ARC buflists | |
126 | * - ARC header release, as it removes from L2ARC buflists | |
127 | */ | |
128 | ||
d3c2ae1c GW |
129 | /* |
130 | * ARC operation: | |
131 | * | |
132 | * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure. | |
133 | * This structure can point either to a block that is still in the cache or to | |
134 | * one that is only accessible in an L2 ARC device, or it can provide | |
135 | * information about a block that was recently evicted. If a block is | |
136 | * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough | |
137 | * information to retrieve it from the L2ARC device. This information is | |
138 | * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block | |
139 | * that is in this state cannot access the data directly. | |
140 | * | |
141 | * Blocks that are actively being referenced or have not been evicted | |
142 | * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within | |
143 | * the arc_buf_hdr_t that will point to the data block in memory. A block can | |
144 | * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC | |
2aa34383 | 145 | * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and |
a6255b7f | 146 | * also in the arc_buf_hdr_t's private physical data block pointer (b_pabd). |
2aa34383 DK |
147 | * |
148 | * The L1ARC's data pointer may or may not be uncompressed. The ARC has the | |
a6255b7f DQ |
149 | * ability to store the physical data (b_pabd) associated with the DVA of the |
150 | * arc_buf_hdr_t. Since the b_pabd is a copy of the on-disk physical block, | |
2aa34383 DK |
151 | * it will match its on-disk compression characteristics. This behavior can be |
152 | * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the | |
a6255b7f | 153 | * compressed ARC functionality is disabled, the b_pabd will point to an |
2aa34383 DK |
154 | * uncompressed version of the on-disk data. |
155 | * | |
156 | * Data in the L1ARC is not accessed by consumers of the ARC directly. Each | |
157 | * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it. | |
158 | * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC | |
159 | * consumer. The ARC will provide references to this data and will keep it | |
160 | * cached until it is no longer in use. The ARC caches only the L1ARC's physical | |
161 | * data block and will evict any arc_buf_t that is no longer referenced. The | |
162 | * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the | |
d3c2ae1c GW |
163 | * "overhead_size" kstat. |
164 | * | |
2aa34383 DK |
165 | * Depending on the consumer, an arc_buf_t can be requested in uncompressed or |
166 | * compressed form. The typical case is that consumers will want uncompressed | |
167 | * data, and when that happens a new data buffer is allocated where the data is | |
168 | * decompressed for them to use. Currently the only consumer who wants | |
169 | * compressed arc_buf_t's is "zfs send", when it streams data exactly as it | |
170 | * exists on disk. When this happens, the arc_buf_t's data buffer is shared | |
171 | * with the arc_buf_hdr_t. | |
d3c2ae1c | 172 | * |
2aa34383 DK |
173 | * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The |
174 | * first one is owned by a compressed send consumer (and therefore references | |
175 | * the same compressed data buffer as the arc_buf_hdr_t) and the second could be | |
176 | * used by any other consumer (and has its own uncompressed copy of the data | |
177 | * buffer). | |
d3c2ae1c | 178 | * |
2aa34383 DK |
179 | * arc_buf_hdr_t |
180 | * +-----------+ | |
181 | * | fields | | |
182 | * | common to | | |
183 | * | L1- and | | |
184 | * | L2ARC | | |
185 | * +-----------+ | |
186 | * | l2arc_buf_hdr_t | |
187 | * | | | |
188 | * +-----------+ | |
189 | * | l1arc_buf_hdr_t | |
190 | * | | arc_buf_t | |
191 | * | b_buf +------------>+-----------+ arc_buf_t | |
a6255b7f | 192 | * | b_pabd +-+ |b_next +---->+-----------+ |
2aa34383 DK |
193 | * +-----------+ | |-----------| |b_next +-->NULL |
194 | * | |b_comp = T | +-----------+ | |
195 | * | |b_data +-+ |b_comp = F | | |
196 | * | +-----------+ | |b_data +-+ | |
197 | * +->+------+ | +-----------+ | | |
198 | * compressed | | | | | |
199 | * data | |<--------------+ | uncompressed | |
200 | * +------+ compressed, | data | |
201 | * shared +-->+------+ | |
202 | * data | | | |
203 | * | | | |
204 | * +------+ | |
d3c2ae1c GW |
205 | * |
206 | * When a consumer reads a block, the ARC must first look to see if the | |
2aa34383 DK |
207 | * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new |
208 | * arc_buf_t and either copies uncompressed data into a new data buffer from an | |
a6255b7f DQ |
209 | * existing uncompressed arc_buf_t, decompresses the hdr's b_pabd buffer into a |
210 | * new data buffer, or shares the hdr's b_pabd buffer, depending on whether the | |
2aa34383 DK |
211 | * hdr is compressed and the desired compression characteristics of the |
212 | * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the | |
213 | * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be | |
214 | * the last buffer in the hdr's b_buf list, however a shared compressed buf can | |
215 | * be anywhere in the hdr's list. | |
d3c2ae1c GW |
216 | * |
217 | * The diagram below shows an example of an uncompressed ARC hdr that is | |
2aa34383 DK |
218 | * sharing its data with an arc_buf_t (note that the shared uncompressed buf is |
219 | * the last element in the buf list): | |
d3c2ae1c GW |
220 | * |
221 | * arc_buf_hdr_t | |
222 | * +-----------+ | |
223 | * | | | |
224 | * | | | |
225 | * | | | |
226 | * +-----------+ | |
227 | * l2arc_buf_hdr_t| | | |
228 | * | | | |
229 | * +-----------+ | |
230 | * l1arc_buf_hdr_t| | | |
231 | * | | arc_buf_t (shared) | |
232 | * | b_buf +------------>+---------+ arc_buf_t | |
233 | * | | |b_next +---->+---------+ | |
a6255b7f | 234 | * | b_pabd +-+ |---------| |b_next +-->NULL |
d3c2ae1c GW |
235 | * +-----------+ | | | +---------+ |
236 | * | |b_data +-+ | | | |
237 | * | +---------+ | |b_data +-+ | |
238 | * +->+------+ | +---------+ | | |
239 | * | | | | | |
240 | * uncompressed | | | | | |
241 | * data +------+ | | | |
242 | * ^ +->+------+ | | |
243 | * | uncompressed | | | | |
244 | * | data | | | | |
245 | * | +------+ | | |
246 | * +---------------------------------+ | |
247 | * | |
a6255b7f | 248 | * Writing to the ARC requires that the ARC first discard the hdr's b_pabd |
d3c2ae1c | 249 | * since the physical block is about to be rewritten. The new data contents |
2aa34383 DK |
250 | * will be contained in the arc_buf_t. As the I/O pipeline performs the write, |
251 | * it may compress the data before writing it to disk. The ARC will be called | |
861166b0 | 252 | * with the transformed data and will memcpy the transformed on-disk block into |
a6255b7f | 253 | * a newly allocated b_pabd. Writes are always done into buffers which have |
2aa34383 DK |
254 | * either been loaned (and hence are new and don't have other readers) or |
255 | * buffers which have been released (and hence have their own hdr, if there | |
256 | * were originally other readers of the buf's original hdr). This ensures that | |
257 | * the ARC only needs to update a single buf and its hdr after a write occurs. | |
d3c2ae1c | 258 | * |
a6255b7f DQ |
259 | * When the L2ARC is in use, it will also take advantage of the b_pabd. The |
260 | * L2ARC will always write the contents of b_pabd to the L2ARC. This means | |
2aa34383 | 261 | * that when compressed ARC is enabled that the L2ARC blocks are identical |
d3c2ae1c GW |
262 | * to the on-disk block in the main data pool. This provides a significant |
263 | * advantage since the ARC can leverage the bp's checksum when reading from the | |
264 | * L2ARC to determine if the contents are valid. However, if the compressed | |
2aa34383 | 265 | * ARC is disabled, then the L2ARC's block must be transformed to look |
d3c2ae1c GW |
266 | * like the physical block in the main data pool before comparing the |
267 | * checksum and determining its validity. | |
b5256303 TC |
268 | * |
269 | * The L1ARC has a slightly different system for storing encrypted data. | |
270 | * Raw (encrypted + possibly compressed) data has a few subtle differences from | |
271 | * data that is just compressed. The biggest difference is that it is not | |
e1cfd73f | 272 | * possible to decrypt encrypted data (or vice-versa) if the keys aren't loaded. |
b5256303 TC |
273 | * The other difference is that encryption cannot be treated as a suggestion. |
274 | * If a caller would prefer compressed data, but they actually wind up with | |
275 | * uncompressed data the worst thing that could happen is there might be a | |
276 | * performance hit. If the caller requests encrypted data, however, we must be | |
277 | * sure they actually get it or else secret information could be leaked. Raw | |
278 | * data is stored in hdr->b_crypt_hdr.b_rabd. An encrypted header, therefore, | |
279 | * may have both an encrypted version and a decrypted version of its data at | |
280 | * once. When a caller needs a raw arc_buf_t, it is allocated and the data is | |
281 | * copied out of this header. To avoid complications with b_pabd, raw buffers | |
282 | * cannot be shared. | |
d3c2ae1c GW |
283 | */ |
284 | ||
34dc7c2f BB |
285 | #include <sys/spa.h> |
286 | #include <sys/zio.h> | |
d3c2ae1c | 287 | #include <sys/spa_impl.h> |
3a17a7a9 | 288 | #include <sys/zio_compress.h> |
d3c2ae1c | 289 | #include <sys/zio_checksum.h> |
34dc7c2f BB |
290 | #include <sys/zfs_context.h> |
291 | #include <sys/arc.h> | |
27d96d22 | 292 | #include <sys/zfs_refcount.h> |
b128c09f | 293 | #include <sys/vdev.h> |
9babb374 | 294 | #include <sys/vdev_impl.h> |
e8b96c60 | 295 | #include <sys/dsl_pool.h> |
ca0bf58d | 296 | #include <sys/multilist.h> |
a6255b7f | 297 | #include <sys/abd.h> |
b5256303 TC |
298 | #include <sys/zil.h> |
299 | #include <sys/fm/fs/zfs.h> | |
34dc7c2f BB |
300 | #include <sys/callb.h> |
301 | #include <sys/kstat.h> | |
3ec34e55 | 302 | #include <sys/zthr.h> |
428870ff | 303 | #include <zfs_fletcher.h> |
59ec819a | 304 | #include <sys/arc_impl.h> |
e5d1c27e | 305 | #include <sys/trace_zfs.h> |
37fb3e43 | 306 | #include <sys/aggsum.h> |
86706441 | 307 | #include <sys/wmsum.h> |
3f387973 | 308 | #include <cityhash.h> |
b7654bd7 | 309 | #include <sys/vdev_trim.h> |
64e0fe14 | 310 | #include <sys/zfs_racct.h> |
8a171ccd | 311 | #include <sys/zstd/zstd.h> |
34dc7c2f | 312 | |
498877ba MA |
313 | #ifndef _KERNEL |
314 | /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ | |
315 | boolean_t arc_watch = B_FALSE; | |
316 | #endif | |
317 | ||
3ec34e55 BL |
318 | /* |
319 | * This thread's job is to keep enough free memory in the system, by | |
320 | * calling arc_kmem_reap_soon() plus arc_reduce_target_size(), which improves | |
321 | * arc_available_memory(). | |
322 | */ | |
3442c2a0 | 323 | static zthr_t *arc_reap_zthr; |
3ec34e55 BL |
324 | |
325 | /* | |
326 | * This thread's job is to keep arc_size under arc_c, by calling | |
5dd92909 | 327 | * arc_evict(), which improves arc_is_overflowing(). |
3ec34e55 | 328 | */ |
3442c2a0 | 329 | static zthr_t *arc_evict_zthr; |
6e2a5918 MJ |
330 | static arc_buf_hdr_t **arc_state_evict_markers; |
331 | static int arc_state_evict_marker_count; | |
3ec34e55 | 332 | |
3442c2a0 MA |
333 | static kmutex_t arc_evict_lock; |
334 | static boolean_t arc_evict_needed = B_FALSE; | |
ed2f7ba0 | 335 | static clock_t arc_last_uncached_flush; |
3442c2a0 MA |
336 | |
337 | /* | |
338 | * Count of bytes evicted since boot. | |
339 | */ | |
340 | static uint64_t arc_evict_count; | |
341 | ||
342 | /* | |
343 | * List of arc_evict_waiter_t's, representing threads waiting for the | |
344 | * arc_evict_count to reach specific values. | |
345 | */ | |
346 | static list_t arc_evict_waiters; | |
347 | ||
348 | /* | |
349 | * When arc_is_overflowing(), arc_get_data_impl() waits for this percent of | |
350 | * the requested amount of data to be evicted. For example, by default for | |
351 | * every 2KB that's evicted, 1KB of it may be "reused" by a new allocation. | |
352 | * Since this is above 100%, it ensures that progress is made towards getting | |
353 | * arc_size under arc_c. Since this is finite, it ensures that allocations | |
354 | * can still happen, even during the potentially long time that arc_size is | |
355 | * more than arc_c. | |
356 | */ | |
fdc2d303 | 357 | static uint_t zfs_arc_eviction_pct = 200; |
ca0bf58d | 358 | |
e8b96c60 | 359 | /* |
ca0bf58d PS |
360 | * The number of headers to evict in arc_evict_state_impl() before |
361 | * dropping the sublist lock and evicting from another sublist. A lower | |
362 | * value means we're more likely to evict the "correct" header (i.e. the | |
363 | * oldest header in the arc state), but comes with higher overhead | |
364 | * (i.e. more invocations of arc_evict_state_impl()). | |
365 | */ | |
fdc2d303 | 366 | static uint_t zfs_arc_evict_batch_limit = 10; |
ca0bf58d | 367 | |
34dc7c2f | 368 | /* number of seconds before growing cache again */ |
fdc2d303 | 369 | uint_t arc_grow_retry = 5; |
3ec34e55 BL |
370 | |
371 | /* | |
372 | * Minimum time between calls to arc_kmem_reap_soon(). | |
373 | */ | |
18168da7 | 374 | static const int arc_kmem_cache_reap_retry_ms = 1000; |
34dc7c2f | 375 | |
a6255b7f | 376 | /* shift of arc_c for calculating overflow limit in arc_get_data_impl */ |
18168da7 | 377 | static int zfs_arc_overflow_shift = 8; |
62422785 | 378 | |
d164b209 | 379 | /* log2(fraction of arc to reclaim) */ |
fdc2d303 | 380 | uint_t arc_shrink_shift = 7; |
d164b209 | 381 | |
03b60eee DB |
382 | /* percent of pagecache to reclaim arc to */ |
383 | #ifdef _KERNEL | |
c9c9c1e2 | 384 | uint_t zfs_arc_pc_percent = 0; |
03b60eee DB |
385 | #endif |
386 | ||
34dc7c2f | 387 | /* |
ca67b33a MA |
388 | * log2(fraction of ARC which must be free to allow growing). |
389 | * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, | |
390 | * when reading a new block into the ARC, we will evict an equal-sized block | |
391 | * from the ARC. | |
392 | * | |
393 | * This must be less than arc_shrink_shift, so that when we shrink the ARC, | |
394 | * we will still not allow it to grow. | |
34dc7c2f | 395 | */ |
fdc2d303 | 396 | uint_t arc_no_grow_shift = 5; |
bce45ec9 | 397 | |
49ddb315 | 398 | |
ca0bf58d PS |
399 | /* |
400 | * minimum lifespan of a prefetch block in clock ticks | |
401 | * (initialized in arc_init()) | |
402 | */ | |
fdc2d303 RY |
403 | static uint_t arc_min_prefetch_ms; |
404 | static uint_t arc_min_prescient_prefetch_ms; | |
ca0bf58d | 405 | |
e8b96c60 MA |
406 | /* |
407 | * If this percent of memory is free, don't throttle. | |
408 | */ | |
fdc2d303 | 409 | uint_t arc_lotsfree_percent = 10; |
e8b96c60 | 410 | |
b128c09f BB |
411 | /* |
412 | * The arc has filled available memory and has now warmed up. | |
413 | */ | |
c9c9c1e2 | 414 | boolean_t arc_warm; |
b128c09f | 415 | |
34dc7c2f BB |
416 | /* |
417 | * These tunables are for performance analysis. | |
418 | */ | |
ab8d9c17 RY |
419 | uint64_t zfs_arc_max = 0; |
420 | uint64_t zfs_arc_min = 0; | |
ab8d9c17 RY |
421 | static uint64_t zfs_arc_dnode_limit = 0; |
422 | static uint_t zfs_arc_dnode_reduce_percent = 10; | |
fdc2d303 RY |
423 | static uint_t zfs_arc_grow_retry = 0; |
424 | static uint_t zfs_arc_shrink_shift = 0; | |
fdc2d303 | 425 | uint_t zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ |
34dc7c2f | 426 | |
dae3e9ea | 427 | /* |
18168da7 AZ |
428 | * ARC dirty data constraints for arc_tempreserve_space() throttle: |
429 | * * total dirty data limit | |
430 | * * anon block dirty limit | |
431 | * * each pool's anon allowance | |
dae3e9ea | 432 | */ |
18168da7 AZ |
433 | static const unsigned long zfs_arc_dirty_limit_percent = 50; |
434 | static const unsigned long zfs_arc_anon_limit_percent = 25; | |
435 | static const unsigned long zfs_arc_pool_dirty_percent = 20; | |
dae3e9ea DB |
436 | |
437 | /* | |
438 | * Enable or disable compressed arc buffers. | |
439 | */ | |
d3c2ae1c GW |
440 | int zfs_compressed_arc_enabled = B_TRUE; |
441 | ||
9907cc1c | 442 | /* |
a8d83e2a AM |
443 | * Balance between metadata and data on ghost hits. Values above 100 |
444 | * increase metadata caching by proportionally reducing effect of ghost | |
445 | * data hits on target data/metadata rate. | |
9907cc1c | 446 | */ |
a8d83e2a | 447 | static uint_t zfs_arc_meta_balance = 500; |
9907cc1c G |
448 | |
449 | /* | |
450 | * Percentage that can be consumed by dnodes of ARC meta buffers. | |
451 | */ | |
ab8d9c17 | 452 | static uint_t zfs_arc_dnode_limit_percent = 10; |
9907cc1c | 453 | |
bc888666 | 454 | /* |
18168da7 | 455 | * These tunables are Linux-specific |
bc888666 | 456 | */ |
ab8d9c17 | 457 | static uint64_t zfs_arc_sys_free = 0; |
fdc2d303 RY |
458 | static uint_t zfs_arc_min_prefetch_ms = 0; |
459 | static uint_t zfs_arc_min_prescient_prefetch_ms = 0; | |
fdc2d303 | 460 | static uint_t zfs_arc_lotsfree_percent = 10; |
bc888666 | 461 | |
462217d1 AM |
462 | /* |
463 | * Number of arc_prune threads | |
464 | */ | |
465 | static int zfs_arc_prune_task_threads = 1; | |
466 | ||
ed2f7ba0 | 467 | /* The 7 states: */ |
13a4027a MM |
468 | arc_state_t ARC_anon; |
469 | arc_state_t ARC_mru; | |
470 | arc_state_t ARC_mru_ghost; | |
471 | arc_state_t ARC_mfu; | |
472 | arc_state_t ARC_mfu_ghost; | |
473 | arc_state_t ARC_l2c_only; | |
ed2f7ba0 | 474 | arc_state_t ARC_uncached; |
34dc7c2f | 475 | |
c9c9c1e2 | 476 | arc_stats_t arc_stats = { |
34dc7c2f | 477 | { "hits", KSTAT_DATA_UINT64 }, |
c935fe2e | 478 | { "iohits", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
479 | { "misses", KSTAT_DATA_UINT64 }, |
480 | { "demand_data_hits", KSTAT_DATA_UINT64 }, | |
c935fe2e | 481 | { "demand_data_iohits", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
482 | { "demand_data_misses", KSTAT_DATA_UINT64 }, |
483 | { "demand_metadata_hits", KSTAT_DATA_UINT64 }, | |
c935fe2e | 484 | { "demand_metadata_iohits", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
485 | { "demand_metadata_misses", KSTAT_DATA_UINT64 }, |
486 | { "prefetch_data_hits", KSTAT_DATA_UINT64 }, | |
c935fe2e | 487 | { "prefetch_data_iohits", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
488 | { "prefetch_data_misses", KSTAT_DATA_UINT64 }, |
489 | { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, | |
c935fe2e | 490 | { "prefetch_metadata_iohits", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
491 | { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, |
492 | { "mru_hits", KSTAT_DATA_UINT64 }, | |
493 | { "mru_ghost_hits", KSTAT_DATA_UINT64 }, | |
494 | { "mfu_hits", KSTAT_DATA_UINT64 }, | |
495 | { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, | |
ed2f7ba0 | 496 | { "uncached_hits", KSTAT_DATA_UINT64 }, |
34dc7c2f | 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 }, | |
a8d83e2a AM |
513 | { "meta", KSTAT_DATA_UINT64 }, |
514 | { "pd", KSTAT_DATA_UINT64 }, | |
515 | { "pm", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
516 | { "c", KSTAT_DATA_UINT64 }, |
517 | { "c_min", KSTAT_DATA_UINT64 }, | |
518 | { "c_max", KSTAT_DATA_UINT64 }, | |
519 | { "size", KSTAT_DATA_UINT64 }, | |
d3c2ae1c GW |
520 | { "compressed_size", KSTAT_DATA_UINT64 }, |
521 | { "uncompressed_size", KSTAT_DATA_UINT64 }, | |
522 | { "overhead_size", KSTAT_DATA_UINT64 }, | |
34dc7c2f | 523 | { "hdr_size", KSTAT_DATA_UINT64 }, |
d164b209 | 524 | { "data_size", KSTAT_DATA_UINT64 }, |
500445c0 | 525 | { "metadata_size", KSTAT_DATA_UINT64 }, |
25458cbe TC |
526 | { "dbuf_size", KSTAT_DATA_UINT64 }, |
527 | { "dnode_size", KSTAT_DATA_UINT64 }, | |
528 | { "bonus_size", KSTAT_DATA_UINT64 }, | |
1c2725a1 MM |
529 | #if defined(COMPAT_FREEBSD11) |
530 | { "other_size", KSTAT_DATA_UINT64 }, | |
531 | #endif | |
13be560d | 532 | { "anon_size", KSTAT_DATA_UINT64 }, |
a8d83e2a AM |
533 | { "anon_data", KSTAT_DATA_UINT64 }, |
534 | { "anon_metadata", KSTAT_DATA_UINT64 }, | |
500445c0 PS |
535 | { "anon_evictable_data", KSTAT_DATA_UINT64 }, |
536 | { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 537 | { "mru_size", KSTAT_DATA_UINT64 }, |
a8d83e2a AM |
538 | { "mru_data", KSTAT_DATA_UINT64 }, |
539 | { "mru_metadata", KSTAT_DATA_UINT64 }, | |
500445c0 PS |
540 | { "mru_evictable_data", KSTAT_DATA_UINT64 }, |
541 | { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 542 | { "mru_ghost_size", KSTAT_DATA_UINT64 }, |
a8d83e2a AM |
543 | { "mru_ghost_data", KSTAT_DATA_UINT64 }, |
544 | { "mru_ghost_metadata", KSTAT_DATA_UINT64 }, | |
500445c0 PS |
545 | { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, |
546 | { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 547 | { "mfu_size", KSTAT_DATA_UINT64 }, |
a8d83e2a AM |
548 | { "mfu_data", KSTAT_DATA_UINT64 }, |
549 | { "mfu_metadata", KSTAT_DATA_UINT64 }, | |
500445c0 PS |
550 | { "mfu_evictable_data", KSTAT_DATA_UINT64 }, |
551 | { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 552 | { "mfu_ghost_size", KSTAT_DATA_UINT64 }, |
a8d83e2a AM |
553 | { "mfu_ghost_data", KSTAT_DATA_UINT64 }, |
554 | { "mfu_ghost_metadata", KSTAT_DATA_UINT64 }, | |
500445c0 PS |
555 | { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, |
556 | { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, | |
ed2f7ba0 | 557 | { "uncached_size", KSTAT_DATA_UINT64 }, |
a8d83e2a AM |
558 | { "uncached_data", KSTAT_DATA_UINT64 }, |
559 | { "uncached_metadata", KSTAT_DATA_UINT64 }, | |
ed2f7ba0 AM |
560 | { "uncached_evictable_data", KSTAT_DATA_UINT64 }, |
561 | { "uncached_evictable_metadata", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
562 | { "l2_hits", KSTAT_DATA_UINT64 }, |
563 | { "l2_misses", KSTAT_DATA_UINT64 }, | |
08532162 GA |
564 | { "l2_prefetch_asize", KSTAT_DATA_UINT64 }, |
565 | { "l2_mru_asize", KSTAT_DATA_UINT64 }, | |
566 | { "l2_mfu_asize", KSTAT_DATA_UINT64 }, | |
567 | { "l2_bufc_data_asize", KSTAT_DATA_UINT64 }, | |
568 | { "l2_bufc_metadata_asize", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
569 | { "l2_feeds", KSTAT_DATA_UINT64 }, |
570 | { "l2_rw_clash", KSTAT_DATA_UINT64 }, | |
d164b209 BB |
571 | { "l2_read_bytes", KSTAT_DATA_UINT64 }, |
572 | { "l2_write_bytes", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
573 | { "l2_writes_sent", KSTAT_DATA_UINT64 }, |
574 | { "l2_writes_done", KSTAT_DATA_UINT64 }, | |
575 | { "l2_writes_error", KSTAT_DATA_UINT64 }, | |
ca0bf58d | 576 | { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
577 | { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, |
578 | { "l2_evict_reading", KSTAT_DATA_UINT64 }, | |
b9541d6b | 579 | { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
580 | { "l2_free_on_write", KSTAT_DATA_UINT64 }, |
581 | { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, | |
582 | { "l2_cksum_bad", KSTAT_DATA_UINT64 }, | |
583 | { "l2_io_error", KSTAT_DATA_UINT64 }, | |
584 | { "l2_size", KSTAT_DATA_UINT64 }, | |
3a17a7a9 | 585 | { "l2_asize", KSTAT_DATA_UINT64 }, |
34dc7c2f | 586 | { "l2_hdr_size", KSTAT_DATA_UINT64 }, |
77f6826b | 587 | { "l2_log_blk_writes", KSTAT_DATA_UINT64 }, |
657fd33b GA |
588 | { "l2_log_blk_avg_asize", KSTAT_DATA_UINT64 }, |
589 | { "l2_log_blk_asize", KSTAT_DATA_UINT64 }, | |
590 | { "l2_log_blk_count", KSTAT_DATA_UINT64 }, | |
77f6826b GA |
591 | { "l2_data_to_meta_ratio", KSTAT_DATA_UINT64 }, |
592 | { "l2_rebuild_success", KSTAT_DATA_UINT64 }, | |
593 | { "l2_rebuild_unsupported", KSTAT_DATA_UINT64 }, | |
594 | { "l2_rebuild_io_errors", KSTAT_DATA_UINT64 }, | |
595 | { "l2_rebuild_dh_errors", KSTAT_DATA_UINT64 }, | |
596 | { "l2_rebuild_cksum_lb_errors", KSTAT_DATA_UINT64 }, | |
597 | { "l2_rebuild_lowmem", KSTAT_DATA_UINT64 }, | |
598 | { "l2_rebuild_size", KSTAT_DATA_UINT64 }, | |
657fd33b | 599 | { "l2_rebuild_asize", KSTAT_DATA_UINT64 }, |
77f6826b GA |
600 | { "l2_rebuild_bufs", KSTAT_DATA_UINT64 }, |
601 | { "l2_rebuild_bufs_precached", KSTAT_DATA_UINT64 }, | |
77f6826b | 602 | { "l2_rebuild_log_blks", KSTAT_DATA_UINT64 }, |
1834f2d8 | 603 | { "memory_throttle_count", KSTAT_DATA_UINT64 }, |
7cb67b45 BB |
604 | { "memory_direct_count", KSTAT_DATA_UINT64 }, |
605 | { "memory_indirect_count", KSTAT_DATA_UINT64 }, | |
70f02287 BB |
606 | { "memory_all_bytes", KSTAT_DATA_UINT64 }, |
607 | { "memory_free_bytes", KSTAT_DATA_UINT64 }, | |
608 | { "memory_available_bytes", KSTAT_DATA_INT64 }, | |
1834f2d8 BB |
609 | { "arc_no_grow", KSTAT_DATA_UINT64 }, |
610 | { "arc_tempreserve", KSTAT_DATA_UINT64 }, | |
611 | { "arc_loaned_bytes", KSTAT_DATA_UINT64 }, | |
ab26409d | 612 | { "arc_prune", KSTAT_DATA_UINT64 }, |
1834f2d8 | 613 | { "arc_meta_used", KSTAT_DATA_UINT64 }, |
25458cbe | 614 | { "arc_dnode_limit", KSTAT_DATA_UINT64 }, |
a8b2e306 | 615 | { "async_upgrade_sync", KSTAT_DATA_UINT64 }, |
c935fe2e | 616 | { "predictive_prefetch", KSTAT_DATA_UINT64 }, |
7f60329a | 617 | { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 }, |
c935fe2e AM |
618 | { "demand_iohit_predictive_prefetch", KSTAT_DATA_UINT64 }, |
619 | { "prescient_prefetch", KSTAT_DATA_UINT64 }, | |
d4a72f23 | 620 | { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64 }, |
c935fe2e | 621 | { "demand_iohit_prescient_prefetch", KSTAT_DATA_UINT64 }, |
11f552fa | 622 | { "arc_need_free", KSTAT_DATA_UINT64 }, |
b5256303 | 623 | { "arc_sys_free", KSTAT_DATA_UINT64 }, |
1dc32a67 MA |
624 | { "arc_raw_size", KSTAT_DATA_UINT64 }, |
625 | { "cached_only_in_progress", KSTAT_DATA_UINT64 }, | |
85ec5cba | 626 | { "abd_chunk_waste_size", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
627 | }; |
628 | ||
c4c162c1 AM |
629 | arc_sums_t arc_sums; |
630 | ||
34dc7c2f BB |
631 | #define ARCSTAT_MAX(stat, val) { \ |
632 | uint64_t m; \ | |
633 | while ((val) > (m = arc_stats.stat.value.ui64) && \ | |
634 | (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ | |
635 | continue; \ | |
636 | } | |
637 | ||
34dc7c2f BB |
638 | /* |
639 | * We define a macro to allow ARC hits/misses to be easily broken down by | |
640 | * two separate conditions, giving a total of four different subtypes for | |
641 | * each of hits and misses (so eight statistics total). | |
642 | */ | |
643 | #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ | |
644 | if (cond1) { \ | |
645 | if (cond2) { \ | |
646 | ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ | |
647 | } else { \ | |
648 | ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ | |
649 | } \ | |
650 | } else { \ | |
651 | if (cond2) { \ | |
652 | ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ | |
653 | } else { \ | |
654 | ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ | |
655 | } \ | |
656 | } | |
657 | ||
77f6826b GA |
658 | /* |
659 | * This macro allows us to use kstats as floating averages. Each time we | |
660 | * update this kstat, we first factor it and the update value by | |
661 | * ARCSTAT_AVG_FACTOR to shrink the new value's contribution to the overall | |
662 | * average. This macro assumes that integer loads and stores are atomic, but | |
663 | * is not safe for multiple writers updating the kstat in parallel (only the | |
664 | * last writer's update will remain). | |
665 | */ | |
666 | #define ARCSTAT_F_AVG_FACTOR 3 | |
667 | #define ARCSTAT_F_AVG(stat, value) \ | |
668 | do { \ | |
669 | uint64_t x = ARCSTAT(stat); \ | |
670 | x = x - x / ARCSTAT_F_AVG_FACTOR + \ | |
671 | (value) / ARCSTAT_F_AVG_FACTOR; \ | |
672 | ARCSTAT(stat) = x; \ | |
77f6826b GA |
673 | } while (0) |
674 | ||
18168da7 | 675 | static kstat_t *arc_ksp; |
c9c9c1e2 | 676 | |
34dc7c2f BB |
677 | /* |
678 | * There are several ARC variables that are critical to export as kstats -- | |
679 | * but we don't want to have to grovel around in the kstat whenever we wish to | |
680 | * manipulate them. For these variables, we therefore define them to be in | |
681 | * terms of the statistic variable. This assures that we are not introducing | |
682 | * the possibility of inconsistency by having shadow copies of the variables, | |
683 | * while still allowing the code to be readable. | |
684 | */ | |
1834f2d8 BB |
685 | #define arc_tempreserve ARCSTAT(arcstat_tempreserve) |
686 | #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes) | |
a8d83e2a | 687 | #define arc_dnode_limit ARCSTAT(arcstat_dnode_limit) /* max size for dnodes */ |
3442c2a0 | 688 | #define arc_need_free ARCSTAT(arcstat_need_free) /* waiting to be evicted */ |
34dc7c2f | 689 | |
c9c9c1e2 MM |
690 | hrtime_t arc_growtime; |
691 | list_t arc_prune_list; | |
692 | kmutex_t arc_prune_mtx; | |
693 | taskq_t *arc_prune_taskq; | |
428870ff | 694 | |
34dc7c2f BB |
695 | #define GHOST_STATE(state) \ |
696 | ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ | |
697 | (state) == arc_l2c_only) | |
698 | ||
2a432414 GW |
699 | #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) |
700 | #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) | |
701 | #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) | |
702 | #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) | |
d4a72f23 TC |
703 | #define HDR_PRESCIENT_PREFETCH(hdr) \ |
704 | ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH) | |
d3c2ae1c GW |
705 | #define HDR_COMPRESSION_ENABLED(hdr) \ |
706 | ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC) | |
b9541d6b | 707 | |
2a432414 | 708 | #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) |
ed2f7ba0 | 709 | #define HDR_UNCACHED(hdr) ((hdr)->b_flags & ARC_FLAG_UNCACHED) |
2a432414 | 710 | #define HDR_L2_READING(hdr) \ |
d3c2ae1c GW |
711 | (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ |
712 | ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) | |
2a432414 GW |
713 | #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) |
714 | #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) | |
715 | #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) | |
b5256303 TC |
716 | #define HDR_PROTECTED(hdr) ((hdr)->b_flags & ARC_FLAG_PROTECTED) |
717 | #define HDR_NOAUTH(hdr) ((hdr)->b_flags & ARC_FLAG_NOAUTH) | |
d3c2ae1c | 718 | #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA) |
34dc7c2f | 719 | |
b9541d6b | 720 | #define HDR_ISTYPE_METADATA(hdr) \ |
d3c2ae1c | 721 | ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) |
b9541d6b CW |
722 | #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) |
723 | ||
724 | #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) | |
725 | #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) | |
b5256303 TC |
726 | #define HDR_HAS_RABD(hdr) \ |
727 | (HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) && \ | |
728 | (hdr)->b_crypt_hdr.b_rabd != NULL) | |
729 | #define HDR_ENCRYPTED(hdr) \ | |
730 | (HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot)) | |
731 | #define HDR_AUTHENTICATED(hdr) \ | |
732 | (HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot)) | |
b9541d6b | 733 | |
d3c2ae1c GW |
734 | /* For storing compression mode in b_flags */ |
735 | #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1) | |
736 | ||
737 | #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \ | |
738 | HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS)) | |
739 | #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \ | |
740 | HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp)); | |
741 | ||
742 | #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL) | |
524b4217 DK |
743 | #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED) |
744 | #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED) | |
b5256303 | 745 | #define ARC_BUF_ENCRYPTED(buf) ((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED) |
d3c2ae1c | 746 | |
34dc7c2f BB |
747 | /* |
748 | * Other sizes | |
749 | */ | |
750 | ||
75a2eb7f | 751 | #define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) |
b9541d6b | 752 | #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) |
34dc7c2f BB |
753 | |
754 | /* | |
755 | * Hash table routines | |
756 | */ | |
757 | ||
490c845e | 758 | #define BUF_LOCKS 2048 |
34dc7c2f BB |
759 | typedef struct buf_hash_table { |
760 | uint64_t ht_mask; | |
761 | arc_buf_hdr_t **ht_table; | |
490c845e | 762 | kmutex_t ht_locks[BUF_LOCKS] ____cacheline_aligned; |
34dc7c2f BB |
763 | } buf_hash_table_t; |
764 | ||
765 | static buf_hash_table_t buf_hash_table; | |
766 | ||
767 | #define BUF_HASH_INDEX(spa, dva, birth) \ | |
768 | (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) | |
490c845e | 769 | #define BUF_HASH_LOCK(idx) (&buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) |
428870ff BB |
770 | #define HDR_LOCK(hdr) \ |
771 | (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) | |
34dc7c2f BB |
772 | |
773 | uint64_t zfs_crc64_table[256]; | |
774 | ||
775 | /* | |
776 | * Level 2 ARC | |
777 | */ | |
778 | ||
887a3c53 GD |
779 | #define L2ARC_WRITE_SIZE (32 * 1024 * 1024) /* initial write max */ |
780 | #define L2ARC_HEADROOM 8 /* num of writes */ | |
8a09d5fd | 781 | |
3a17a7a9 SK |
782 | /* |
783 | * If we discover during ARC scan any buffers to be compressed, we boost | |
784 | * our headroom for the next scanning cycle by this percentage multiple. | |
785 | */ | |
786 | #define L2ARC_HEADROOM_BOOST 200 | |
d164b209 BB |
787 | #define L2ARC_FEED_SECS 1 /* caching interval secs */ |
788 | #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ | |
34dc7c2f | 789 | |
4aafab91 G |
790 | /* |
791 | * We can feed L2ARC from two states of ARC buffers, mru and mfu, | |
792 | * and each of the state has two types: data and metadata. | |
793 | */ | |
794 | #define L2ARC_FEED_TYPES 4 | |
795 | ||
d3cc8b15 | 796 | /* L2ARC Performance Tunables */ |
ab8d9c17 RY |
797 | uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */ |
798 | uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */ | |
799 | uint64_t l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */ | |
800 | uint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; | |
801 | uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ | |
802 | uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */ | |
abd8610c BB |
803 | int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ |
804 | int l2arc_feed_again = B_TRUE; /* turbo warmup */ | |
c93504f0 | 805 | int l2arc_norw = B_FALSE; /* no reads during writes */ |
fdc2d303 | 806 | static uint_t l2arc_meta_percent = 33; /* limit on headers size */ |
34dc7c2f BB |
807 | |
808 | /* | |
809 | * L2ARC Internals | |
810 | */ | |
34dc7c2f BB |
811 | static list_t L2ARC_dev_list; /* device list */ |
812 | static list_t *l2arc_dev_list; /* device list pointer */ | |
813 | static kmutex_t l2arc_dev_mtx; /* device list mutex */ | |
814 | static l2arc_dev_t *l2arc_dev_last; /* last device used */ | |
34dc7c2f BB |
815 | static list_t L2ARC_free_on_write; /* free after write buf list */ |
816 | static list_t *l2arc_free_on_write; /* free after write list ptr */ | |
817 | static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ | |
818 | static uint64_t l2arc_ndev; /* number of devices */ | |
819 | ||
820 | typedef struct l2arc_read_callback { | |
2aa34383 | 821 | arc_buf_hdr_t *l2rcb_hdr; /* read header */ |
3a17a7a9 | 822 | blkptr_t l2rcb_bp; /* original blkptr */ |
5dbd68a3 | 823 | zbookmark_phys_t l2rcb_zb; /* original bookmark */ |
3a17a7a9 | 824 | int l2rcb_flags; /* original flags */ |
82710e99 | 825 | abd_t *l2rcb_abd; /* temporary buffer */ |
34dc7c2f BB |
826 | } l2arc_read_callback_t; |
827 | ||
34dc7c2f BB |
828 | typedef struct l2arc_data_free { |
829 | /* protected by l2arc_free_on_write_mtx */ | |
a6255b7f | 830 | abd_t *l2df_abd; |
34dc7c2f | 831 | size_t l2df_size; |
d3c2ae1c | 832 | arc_buf_contents_t l2df_type; |
34dc7c2f BB |
833 | list_node_t l2df_list_node; |
834 | } l2arc_data_free_t; | |
835 | ||
b5256303 TC |
836 | typedef enum arc_fill_flags { |
837 | ARC_FILL_LOCKED = 1 << 0, /* hdr lock is held */ | |
838 | ARC_FILL_COMPRESSED = 1 << 1, /* fill with compressed data */ | |
839 | ARC_FILL_ENCRYPTED = 1 << 2, /* fill with encrypted data */ | |
840 | ARC_FILL_NOAUTH = 1 << 3, /* don't attempt to authenticate */ | |
841 | ARC_FILL_IN_PLACE = 1 << 4 /* fill in place (special case) */ | |
842 | } arc_fill_flags_t; | |
843 | ||
f7de776d AM |
844 | typedef enum arc_ovf_level { |
845 | ARC_OVF_NONE, /* ARC within target size. */ | |
846 | ARC_OVF_SOME, /* ARC is slightly overflowed. */ | |
847 | ARC_OVF_SEVERE /* ARC is severely overflowed. */ | |
848 | } arc_ovf_level_t; | |
849 | ||
34dc7c2f BB |
850 | static kmutex_t l2arc_feed_thr_lock; |
851 | static kcondvar_t l2arc_feed_thr_cv; | |
852 | static uint8_t l2arc_thread_exit; | |
853 | ||
77f6826b GA |
854 | static kmutex_t l2arc_rebuild_thr_lock; |
855 | static kcondvar_t l2arc_rebuild_thr_cv; | |
856 | ||
e111c802 MM |
857 | enum arc_hdr_alloc_flags { |
858 | ARC_HDR_ALLOC_RDATA = 0x1, | |
6b88b4b5 | 859 | ARC_HDR_USE_RESERVE = 0x4, |
ed2f7ba0 | 860 | ARC_HDR_ALLOC_LINEAR = 0x8, |
e111c802 MM |
861 | }; |
862 | ||
863 | ||
dd66857d AZ |
864 | static abd_t *arc_get_data_abd(arc_buf_hdr_t *, uint64_t, const void *, int); |
865 | static void *arc_get_data_buf(arc_buf_hdr_t *, uint64_t, const void *); | |
866 | static void arc_get_data_impl(arc_buf_hdr_t *, uint64_t, const void *, int); | |
867 | static void arc_free_data_abd(arc_buf_hdr_t *, abd_t *, uint64_t, const void *); | |
868 | static void arc_free_data_buf(arc_buf_hdr_t *, void *, uint64_t, const void *); | |
869 | static void arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, | |
870 | const void *tag); | |
b5256303 | 871 | static void arc_hdr_free_abd(arc_buf_hdr_t *, boolean_t); |
e111c802 | 872 | static void arc_hdr_alloc_abd(arc_buf_hdr_t *, int); |
ed2f7ba0 | 873 | static void arc_hdr_destroy(arc_buf_hdr_t *); |
c935fe2e | 874 | static void arc_access(arc_buf_hdr_t *, arc_flags_t, boolean_t); |
2a432414 | 875 | static void arc_buf_watch(arc_buf_t *); |
ed2f7ba0 | 876 | static void arc_change_state(arc_state_t *, arc_buf_hdr_t *); |
2a432414 | 877 | |
b9541d6b CW |
878 | static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); |
879 | static uint32_t arc_bufc_to_flags(arc_buf_contents_t); | |
d3c2ae1c GW |
880 | static inline void arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); |
881 | static inline void arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); | |
b9541d6b | 882 | |
2a432414 GW |
883 | static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); |
884 | static void l2arc_read_done(zio_t *); | |
cfd59f90 | 885 | static void l2arc_do_free_on_write(void); |
08532162 GA |
886 | static void l2arc_hdr_arcstats_update(arc_buf_hdr_t *hdr, boolean_t incr, |
887 | boolean_t state_only); | |
888 | ||
799e09f7 AM |
889 | static void arc_prune_async(uint64_t adjust); |
890 | ||
08532162 GA |
891 | #define l2arc_hdr_arcstats_increment(hdr) \ |
892 | l2arc_hdr_arcstats_update((hdr), B_TRUE, B_FALSE) | |
893 | #define l2arc_hdr_arcstats_decrement(hdr) \ | |
894 | l2arc_hdr_arcstats_update((hdr), B_FALSE, B_FALSE) | |
895 | #define l2arc_hdr_arcstats_increment_state(hdr) \ | |
896 | l2arc_hdr_arcstats_update((hdr), B_TRUE, B_TRUE) | |
897 | #define l2arc_hdr_arcstats_decrement_state(hdr) \ | |
898 | l2arc_hdr_arcstats_update((hdr), B_FALSE, B_TRUE) | |
34dc7c2f | 899 | |
c9d62d13 GA |
900 | /* |
901 | * l2arc_exclude_special : A zfs module parameter that controls whether buffers | |
902 | * present on special vdevs are eligibile for caching in L2ARC. If | |
903 | * set to 1, exclude dbufs on special vdevs from being cached to | |
904 | * L2ARC. | |
905 | */ | |
906 | int l2arc_exclude_special = 0; | |
907 | ||
feb3a7ee GA |
908 | /* |
909 | * l2arc_mfuonly : A ZFS module parameter that controls whether only MFU | |
910 | * metadata and data are cached from ARC into L2ARC. | |
911 | */ | |
18168da7 | 912 | static int l2arc_mfuonly = 0; |
feb3a7ee | 913 | |
b7654bd7 GA |
914 | /* |
915 | * L2ARC TRIM | |
916 | * l2arc_trim_ahead : A ZFS module parameter that controls how much ahead of | |
917 | * the current write size (l2arc_write_max) we should TRIM if we | |
918 | * have filled the device. It is defined as a percentage of the | |
919 | * write size. If set to 100 we trim twice the space required to | |
920 | * accommodate upcoming writes. A minimum of 64MB will be trimmed. | |
921 | * It also enables TRIM of the whole L2ARC device upon creation or | |
922 | * addition to an existing pool or if the header of the device is | |
923 | * invalid upon importing a pool or onlining a cache device. The | |
924 | * default is 0, which disables TRIM on L2ARC altogether as it can | |
925 | * put significant stress on the underlying storage devices. This | |
926 | * will vary depending of how well the specific device handles | |
927 | * these commands. | |
928 | */ | |
ab8d9c17 | 929 | static uint64_t l2arc_trim_ahead = 0; |
b7654bd7 | 930 | |
77f6826b GA |
931 | /* |
932 | * Performance tuning of L2ARC persistence: | |
933 | * | |
934 | * l2arc_rebuild_enabled : A ZFS module parameter that controls whether adding | |
935 | * an L2ARC device (either at pool import or later) will attempt | |
936 | * to rebuild L2ARC buffer contents. | |
937 | * l2arc_rebuild_blocks_min_l2size : A ZFS module parameter that controls | |
938 | * whether log blocks are written to the L2ARC device. If the L2ARC | |
939 | * device is less than 1GB, the amount of data l2arc_evict() | |
940 | * evicts is significant compared to the amount of restored L2ARC | |
941 | * data. In this case do not write log blocks in L2ARC in order | |
942 | * not to waste space. | |
943 | */ | |
18168da7 | 944 | static int l2arc_rebuild_enabled = B_TRUE; |
ab8d9c17 | 945 | static uint64_t l2arc_rebuild_blocks_min_l2size = 1024 * 1024 * 1024; |
77f6826b GA |
946 | |
947 | /* L2ARC persistence rebuild control routines. */ | |
948 | void l2arc_rebuild_vdev(vdev_t *vd, boolean_t reopen); | |
460748d4 | 949 | static __attribute__((noreturn)) void l2arc_dev_rebuild_thread(void *arg); |
77f6826b GA |
950 | static int l2arc_rebuild(l2arc_dev_t *dev); |
951 | ||
952 | /* L2ARC persistence read I/O routines. */ | |
953 | static int l2arc_dev_hdr_read(l2arc_dev_t *dev); | |
954 | static int l2arc_log_blk_read(l2arc_dev_t *dev, | |
955 | const l2arc_log_blkptr_t *this_lp, const l2arc_log_blkptr_t *next_lp, | |
956 | l2arc_log_blk_phys_t *this_lb, l2arc_log_blk_phys_t *next_lb, | |
957 | zio_t *this_io, zio_t **next_io); | |
958 | static zio_t *l2arc_log_blk_fetch(vdev_t *vd, | |
959 | const l2arc_log_blkptr_t *lp, l2arc_log_blk_phys_t *lb); | |
960 | static void l2arc_log_blk_fetch_abort(zio_t *zio); | |
961 | ||
962 | /* L2ARC persistence block restoration routines. */ | |
963 | static void l2arc_log_blk_restore(l2arc_dev_t *dev, | |
a76e4e67 | 964 | const l2arc_log_blk_phys_t *lb, uint64_t lb_asize); |
77f6826b GA |
965 | static void l2arc_hdr_restore(const l2arc_log_ent_phys_t *le, |
966 | l2arc_dev_t *dev); | |
967 | ||
968 | /* L2ARC persistence write I/O routines. */ | |
bcd53210 | 969 | static uint64_t l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio, |
77f6826b GA |
970 | l2arc_write_callback_t *cb); |
971 | ||
dd4bc569 | 972 | /* L2ARC persistence auxiliary routines. */ |
77f6826b GA |
973 | boolean_t l2arc_log_blkptr_valid(l2arc_dev_t *dev, |
974 | const l2arc_log_blkptr_t *lbp); | |
975 | static boolean_t l2arc_log_blk_insert(l2arc_dev_t *dev, | |
976 | const arc_buf_hdr_t *ab); | |
977 | boolean_t l2arc_range_check_overlap(uint64_t bottom, | |
978 | uint64_t top, uint64_t check); | |
979 | static void l2arc_blk_fetch_done(zio_t *zio); | |
980 | static inline uint64_t | |
981 | l2arc_log_blk_overhead(uint64_t write_sz, l2arc_dev_t *dev); | |
37fb3e43 PD |
982 | |
983 | /* | |
984 | * We use Cityhash for this. It's fast, and has good hash properties without | |
985 | * requiring any large static buffers. | |
986 | */ | |
34dc7c2f | 987 | static uint64_t |
d164b209 | 988 | buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) |
34dc7c2f | 989 | { |
37fb3e43 | 990 | return (cityhash4(spa, dva->dva_word[0], dva->dva_word[1], birth)); |
34dc7c2f BB |
991 | } |
992 | ||
d3c2ae1c GW |
993 | #define HDR_EMPTY(hdr) \ |
994 | ((hdr)->b_dva.dva_word[0] == 0 && \ | |
995 | (hdr)->b_dva.dva_word[1] == 0) | |
34dc7c2f | 996 | |
ca6c7a94 BB |
997 | #define HDR_EMPTY_OR_LOCKED(hdr) \ |
998 | (HDR_EMPTY(hdr) || MUTEX_HELD(HDR_LOCK(hdr))) | |
999 | ||
d3c2ae1c GW |
1000 | #define HDR_EQUAL(spa, dva, birth, hdr) \ |
1001 | ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ | |
1002 | ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ | |
1003 | ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa) | |
34dc7c2f | 1004 | |
428870ff BB |
1005 | static void |
1006 | buf_discard_identity(arc_buf_hdr_t *hdr) | |
1007 | { | |
1008 | hdr->b_dva.dva_word[0] = 0; | |
1009 | hdr->b_dva.dva_word[1] = 0; | |
1010 | hdr->b_birth = 0; | |
428870ff BB |
1011 | } |
1012 | ||
34dc7c2f | 1013 | static arc_buf_hdr_t * |
9b67f605 | 1014 | buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) |
34dc7c2f | 1015 | { |
9b67f605 | 1016 | const dva_t *dva = BP_IDENTITY(bp); |
493fcce9 | 1017 | uint64_t birth = BP_GET_BIRTH(bp); |
34dc7c2f BB |
1018 | uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); |
1019 | kmutex_t *hash_lock = BUF_HASH_LOCK(idx); | |
2a432414 | 1020 | arc_buf_hdr_t *hdr; |
34dc7c2f BB |
1021 | |
1022 | mutex_enter(hash_lock); | |
2a432414 GW |
1023 | for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; |
1024 | hdr = hdr->b_hash_next) { | |
d3c2ae1c | 1025 | if (HDR_EQUAL(spa, dva, birth, hdr)) { |
34dc7c2f | 1026 | *lockp = hash_lock; |
2a432414 | 1027 | return (hdr); |
34dc7c2f BB |
1028 | } |
1029 | } | |
1030 | mutex_exit(hash_lock); | |
1031 | *lockp = NULL; | |
1032 | return (NULL); | |
1033 | } | |
1034 | ||
1035 | /* | |
1036 | * Insert an entry into the hash table. If there is already an element | |
1037 | * equal to elem in the hash table, then the already existing element | |
1038 | * will be returned and the new element will not be inserted. | |
1039 | * Otherwise returns NULL. | |
b9541d6b | 1040 | * If lockp == NULL, the caller is assumed to already hold the hash lock. |
34dc7c2f BB |
1041 | */ |
1042 | static arc_buf_hdr_t * | |
2a432414 | 1043 | buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) |
34dc7c2f | 1044 | { |
2a432414 | 1045 | uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); |
34dc7c2f | 1046 | kmutex_t *hash_lock = BUF_HASH_LOCK(idx); |
2a432414 | 1047 | arc_buf_hdr_t *fhdr; |
34dc7c2f BB |
1048 | uint32_t i; |
1049 | ||
2a432414 GW |
1050 | ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); |
1051 | ASSERT(hdr->b_birth != 0); | |
1052 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); | |
b9541d6b CW |
1053 | |
1054 | if (lockp != NULL) { | |
1055 | *lockp = hash_lock; | |
1056 | mutex_enter(hash_lock); | |
1057 | } else { | |
1058 | ASSERT(MUTEX_HELD(hash_lock)); | |
1059 | } | |
1060 | ||
2a432414 GW |
1061 | for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; |
1062 | fhdr = fhdr->b_hash_next, i++) { | |
d3c2ae1c | 1063 | if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) |
2a432414 | 1064 | return (fhdr); |
34dc7c2f BB |
1065 | } |
1066 | ||
2a432414 GW |
1067 | hdr->b_hash_next = buf_hash_table.ht_table[idx]; |
1068 | buf_hash_table.ht_table[idx] = hdr; | |
d3c2ae1c | 1069 | arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
34dc7c2f BB |
1070 | |
1071 | /* collect some hash table performance data */ | |
1072 | if (i > 0) { | |
1073 | ARCSTAT_BUMP(arcstat_hash_collisions); | |
1074 | if (i == 1) | |
1075 | ARCSTAT_BUMP(arcstat_hash_chains); | |
1076 | ||
1077 | ARCSTAT_MAX(arcstat_hash_chain_max, i); | |
1078 | } | |
c4c162c1 AM |
1079 | uint64_t he = atomic_inc_64_nv( |
1080 | &arc_stats.arcstat_hash_elements.value.ui64); | |
1081 | ARCSTAT_MAX(arcstat_hash_elements_max, he); | |
34dc7c2f BB |
1082 | |
1083 | return (NULL); | |
1084 | } | |
1085 | ||
1086 | static void | |
2a432414 | 1087 | buf_hash_remove(arc_buf_hdr_t *hdr) |
34dc7c2f | 1088 | { |
2a432414 GW |
1089 | arc_buf_hdr_t *fhdr, **hdrp; |
1090 | uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); | |
34dc7c2f BB |
1091 | |
1092 | ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); | |
2a432414 | 1093 | ASSERT(HDR_IN_HASH_TABLE(hdr)); |
34dc7c2f | 1094 | |
2a432414 GW |
1095 | hdrp = &buf_hash_table.ht_table[idx]; |
1096 | while ((fhdr = *hdrp) != hdr) { | |
d3c2ae1c | 1097 | ASSERT3P(fhdr, !=, NULL); |
2a432414 | 1098 | hdrp = &fhdr->b_hash_next; |
34dc7c2f | 1099 | } |
2a432414 GW |
1100 | *hdrp = hdr->b_hash_next; |
1101 | hdr->b_hash_next = NULL; | |
d3c2ae1c | 1102 | arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
34dc7c2f BB |
1103 | |
1104 | /* collect some hash table performance data */ | |
c4c162c1 | 1105 | atomic_dec_64(&arc_stats.arcstat_hash_elements.value.ui64); |
34dc7c2f BB |
1106 | |
1107 | if (buf_hash_table.ht_table[idx] && | |
1108 | buf_hash_table.ht_table[idx]->b_hash_next == NULL) | |
1109 | ARCSTAT_BUMPDOWN(arcstat_hash_chains); | |
1110 | } | |
1111 | ||
1112 | /* | |
1113 | * Global data structures and functions for the buf kmem cache. | |
1114 | */ | |
b5256303 | 1115 | |
b9541d6b CW |
1116 | static kmem_cache_t *hdr_full_cache; |
1117 | static kmem_cache_t *hdr_l2only_cache; | |
34dc7c2f BB |
1118 | static kmem_cache_t *buf_cache; |
1119 | ||
1120 | static void | |
1121 | buf_fini(void) | |
1122 | { | |
93ce2b4c | 1123 | #if defined(_KERNEL) |
d1d7e268 MK |
1124 | /* |
1125 | * Large allocations which do not require contiguous pages | |
1126 | * should be using vmem_free() in the linux kernel\ | |
1127 | */ | |
00b46022 BB |
1128 | vmem_free(buf_hash_table.ht_table, |
1129 | (buf_hash_table.ht_mask + 1) * sizeof (void *)); | |
1130 | #else | |
34dc7c2f BB |
1131 | kmem_free(buf_hash_table.ht_table, |
1132 | (buf_hash_table.ht_mask + 1) * sizeof (void *)); | |
00b46022 | 1133 | #endif |
14e4e3cb | 1134 | for (int i = 0; i < BUF_LOCKS; i++) |
490c845e | 1135 | mutex_destroy(BUF_HASH_LOCK(i)); |
b9541d6b CW |
1136 | kmem_cache_destroy(hdr_full_cache); |
1137 | kmem_cache_destroy(hdr_l2only_cache); | |
34dc7c2f BB |
1138 | kmem_cache_destroy(buf_cache); |
1139 | } | |
1140 | ||
1141 | /* | |
1142 | * Constructor callback - called when the cache is empty | |
1143 | * and a new buf is requested. | |
1144 | */ | |
34dc7c2f | 1145 | static int |
b9541d6b CW |
1146 | hdr_full_cons(void *vbuf, void *unused, int kmflag) |
1147 | { | |
14e4e3cb | 1148 | (void) unused, (void) kmflag; |
b9541d6b CW |
1149 | arc_buf_hdr_t *hdr = vbuf; |
1150 | ||
861166b0 | 1151 | memset(hdr, 0, HDR_FULL_SIZE); |
ae76f45c | 1152 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; |
424fd7c3 | 1153 | zfs_refcount_create(&hdr->b_l1hdr.b_refcnt); |
bacf366f | 1154 | #ifdef ZFS_DEBUG |
b9541d6b | 1155 | mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); |
bacf366f | 1156 | #endif |
ca0bf58d | 1157 | multilist_link_init(&hdr->b_l1hdr.b_arc_node); |
c935fe2e | 1158 | list_link_init(&hdr->b_l2hdr.b_l2node); |
b9541d6b CW |
1159 | arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); |
1160 | ||
1161 | return (0); | |
1162 | } | |
1163 | ||
b9541d6b CW |
1164 | static int |
1165 | hdr_l2only_cons(void *vbuf, void *unused, int kmflag) | |
34dc7c2f | 1166 | { |
14e4e3cb | 1167 | (void) unused, (void) kmflag; |
2a432414 GW |
1168 | arc_buf_hdr_t *hdr = vbuf; |
1169 | ||
861166b0 | 1170 | memset(hdr, 0, HDR_L2ONLY_SIZE); |
b9541d6b | 1171 | arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); |
34dc7c2f | 1172 | |
34dc7c2f BB |
1173 | return (0); |
1174 | } | |
1175 | ||
b128c09f BB |
1176 | static int |
1177 | buf_cons(void *vbuf, void *unused, int kmflag) | |
1178 | { | |
14e4e3cb | 1179 | (void) unused, (void) kmflag; |
b128c09f BB |
1180 | arc_buf_t *buf = vbuf; |
1181 | ||
861166b0 | 1182 | memset(buf, 0, sizeof (arc_buf_t)); |
d164b209 BB |
1183 | arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); |
1184 | ||
b128c09f BB |
1185 | return (0); |
1186 | } | |
1187 | ||
34dc7c2f BB |
1188 | /* |
1189 | * Destructor callback - called when a cached buf is | |
1190 | * no longer required. | |
1191 | */ | |
34dc7c2f | 1192 | static void |
b9541d6b | 1193 | hdr_full_dest(void *vbuf, void *unused) |
34dc7c2f | 1194 | { |
14e4e3cb | 1195 | (void) unused; |
2a432414 | 1196 | arc_buf_hdr_t *hdr = vbuf; |
34dc7c2f | 1197 | |
d3c2ae1c | 1198 | ASSERT(HDR_EMPTY(hdr)); |
424fd7c3 | 1199 | zfs_refcount_destroy(&hdr->b_l1hdr.b_refcnt); |
bacf366f | 1200 | #ifdef ZFS_DEBUG |
b9541d6b | 1201 | mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); |
bacf366f | 1202 | #endif |
ca0bf58d | 1203 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b CW |
1204 | arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); |
1205 | } | |
1206 | ||
b9541d6b CW |
1207 | static void |
1208 | hdr_l2only_dest(void *vbuf, void *unused) | |
1209 | { | |
14e4e3cb AZ |
1210 | (void) unused; |
1211 | arc_buf_hdr_t *hdr = vbuf; | |
b9541d6b | 1212 | |
d3c2ae1c | 1213 | ASSERT(HDR_EMPTY(hdr)); |
b9541d6b | 1214 | arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); |
34dc7c2f BB |
1215 | } |
1216 | ||
b128c09f BB |
1217 | static void |
1218 | buf_dest(void *vbuf, void *unused) | |
1219 | { | |
14e4e3cb | 1220 | (void) unused; |
289f7e6a | 1221 | (void) vbuf; |
b128c09f | 1222 | |
d164b209 | 1223 | arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); |
b128c09f BB |
1224 | } |
1225 | ||
34dc7c2f BB |
1226 | static void |
1227 | buf_init(void) | |
1228 | { | |
2db28197 | 1229 | uint64_t *ct = NULL; |
34dc7c2f BB |
1230 | uint64_t hsize = 1ULL << 12; |
1231 | int i, j; | |
1232 | ||
1233 | /* | |
1234 | * The hash table is big enough to fill all of physical memory | |
49ddb315 MA |
1235 | * with an average block size of zfs_arc_average_blocksize (default 8K). |
1236 | * By default, the table will take up | |
1237 | * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). | |
34dc7c2f | 1238 | */ |
9edb3695 | 1239 | while (hsize * zfs_arc_average_blocksize < arc_all_memory()) |
34dc7c2f BB |
1240 | hsize <<= 1; |
1241 | retry: | |
1242 | buf_hash_table.ht_mask = hsize - 1; | |
93ce2b4c | 1243 | #if defined(_KERNEL) |
d1d7e268 MK |
1244 | /* |
1245 | * Large allocations which do not require contiguous pages | |
1246 | * should be using vmem_alloc() in the linux kernel | |
1247 | */ | |
00b46022 BB |
1248 | buf_hash_table.ht_table = |
1249 | vmem_zalloc(hsize * sizeof (void*), KM_SLEEP); | |
1250 | #else | |
34dc7c2f BB |
1251 | buf_hash_table.ht_table = |
1252 | kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); | |
00b46022 | 1253 | #endif |
34dc7c2f BB |
1254 | if (buf_hash_table.ht_table == NULL) { |
1255 | ASSERT(hsize > (1ULL << 8)); | |
1256 | hsize >>= 1; | |
1257 | goto retry; | |
1258 | } | |
1259 | ||
b9541d6b | 1260 | hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, |
026e529c | 1261 | 0, hdr_full_cons, hdr_full_dest, NULL, NULL, NULL, 0); |
b9541d6b | 1262 | hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", |
026e529c | 1263 | HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, NULL, |
b9541d6b | 1264 | NULL, NULL, 0); |
34dc7c2f | 1265 | buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), |
b128c09f | 1266 | 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); |
34dc7c2f BB |
1267 | |
1268 | for (i = 0; i < 256; i++) | |
1269 | for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) | |
1270 | *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); | |
1271 | ||
490c845e AM |
1272 | for (i = 0; i < BUF_LOCKS; i++) |
1273 | mutex_init(BUF_HASH_LOCK(i), NULL, MUTEX_DEFAULT, NULL); | |
34dc7c2f BB |
1274 | } |
1275 | ||
d3c2ae1c | 1276 | #define ARC_MINTIME (hz>>4) /* 62 ms */ |
ca0bf58d | 1277 | |
2aa34383 DK |
1278 | /* |
1279 | * This is the size that the buf occupies in memory. If the buf is compressed, | |
1280 | * it will correspond to the compressed size. You should use this method of | |
1281 | * getting the buf size unless you explicitly need the logical size. | |
1282 | */ | |
1283 | uint64_t | |
1284 | arc_buf_size(arc_buf_t *buf) | |
1285 | { | |
1286 | return (ARC_BUF_COMPRESSED(buf) ? | |
1287 | HDR_GET_PSIZE(buf->b_hdr) : HDR_GET_LSIZE(buf->b_hdr)); | |
1288 | } | |
1289 | ||
1290 | uint64_t | |
1291 | arc_buf_lsize(arc_buf_t *buf) | |
1292 | { | |
1293 | return (HDR_GET_LSIZE(buf->b_hdr)); | |
1294 | } | |
1295 | ||
b5256303 TC |
1296 | /* |
1297 | * This function will return B_TRUE if the buffer is encrypted in memory. | |
1298 | * This buffer can be decrypted by calling arc_untransform(). | |
1299 | */ | |
1300 | boolean_t | |
1301 | arc_is_encrypted(arc_buf_t *buf) | |
1302 | { | |
1303 | return (ARC_BUF_ENCRYPTED(buf) != 0); | |
1304 | } | |
1305 | ||
1306 | /* | |
1307 | * Returns B_TRUE if the buffer represents data that has not had its MAC | |
1308 | * verified yet. | |
1309 | */ | |
1310 | boolean_t | |
1311 | arc_is_unauthenticated(arc_buf_t *buf) | |
1312 | { | |
1313 | return (HDR_NOAUTH(buf->b_hdr) != 0); | |
1314 | } | |
1315 | ||
1316 | void | |
1317 | arc_get_raw_params(arc_buf_t *buf, boolean_t *byteorder, uint8_t *salt, | |
1318 | uint8_t *iv, uint8_t *mac) | |
1319 | { | |
1320 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
1321 | ||
1322 | ASSERT(HDR_PROTECTED(hdr)); | |
1323 | ||
861166b0 AZ |
1324 | memcpy(salt, hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); |
1325 | memcpy(iv, hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); | |
1326 | memcpy(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); | |
b5256303 TC |
1327 | *byteorder = (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ? |
1328 | ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER; | |
1329 | } | |
1330 | ||
1331 | /* | |
1332 | * Indicates how this buffer is compressed in memory. If it is not compressed | |
1333 | * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with | |
1334 | * arc_untransform() as long as it is also unencrypted. | |
1335 | */ | |
2aa34383 DK |
1336 | enum zio_compress |
1337 | arc_get_compression(arc_buf_t *buf) | |
1338 | { | |
1339 | return (ARC_BUF_COMPRESSED(buf) ? | |
1340 | HDR_GET_COMPRESS(buf->b_hdr) : ZIO_COMPRESS_OFF); | |
1341 | } | |
1342 | ||
b5256303 TC |
1343 | /* |
1344 | * Return the compression algorithm used to store this data in the ARC. If ARC | |
1345 | * compression is enabled or this is an encrypted block, this will be the same | |
1346 | * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF. | |
1347 | */ | |
1348 | static inline enum zio_compress | |
1349 | arc_hdr_get_compress(arc_buf_hdr_t *hdr) | |
1350 | { | |
1351 | return (HDR_COMPRESSION_ENABLED(hdr) ? | |
1352 | HDR_GET_COMPRESS(hdr) : ZIO_COMPRESS_OFF); | |
1353 | } | |
1354 | ||
10b3c7f5 MN |
1355 | uint8_t |
1356 | arc_get_complevel(arc_buf_t *buf) | |
1357 | { | |
1358 | return (buf->b_hdr->b_complevel); | |
1359 | } | |
1360 | ||
d3c2ae1c GW |
1361 | static inline boolean_t |
1362 | arc_buf_is_shared(arc_buf_t *buf) | |
1363 | { | |
1364 | boolean_t shared = (buf->b_data != NULL && | |
a6255b7f DQ |
1365 | buf->b_hdr->b_l1hdr.b_pabd != NULL && |
1366 | abd_is_linear(buf->b_hdr->b_l1hdr.b_pabd) && | |
1367 | buf->b_data == abd_to_buf(buf->b_hdr->b_l1hdr.b_pabd)); | |
d3c2ae1c | 1368 | IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr)); |
57b40985 | 1369 | EQUIV(shared, ARC_BUF_SHARED(buf)); |
2aa34383 | 1370 | IMPLY(shared, ARC_BUF_COMPRESSED(buf) || ARC_BUF_LAST(buf)); |
524b4217 DK |
1371 | |
1372 | /* | |
1373 | * It would be nice to assert arc_can_share() too, but the "hdr isn't | |
1374 | * already being shared" requirement prevents us from doing that. | |
1375 | */ | |
1376 | ||
d3c2ae1c GW |
1377 | return (shared); |
1378 | } | |
ca0bf58d | 1379 | |
a7004725 DK |
1380 | /* |
1381 | * Free the checksum associated with this header. If there is no checksum, this | |
1382 | * is a no-op. | |
1383 | */ | |
d3c2ae1c GW |
1384 | static inline void |
1385 | arc_cksum_free(arc_buf_hdr_t *hdr) | |
1386 | { | |
bacf366f | 1387 | #ifdef ZFS_DEBUG |
d3c2ae1c | 1388 | ASSERT(HDR_HAS_L1HDR(hdr)); |
b5256303 | 1389 | |
d3c2ae1c GW |
1390 | mutex_enter(&hdr->b_l1hdr.b_freeze_lock); |
1391 | if (hdr->b_l1hdr.b_freeze_cksum != NULL) { | |
1392 | kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t)); | |
1393 | hdr->b_l1hdr.b_freeze_cksum = NULL; | |
b9541d6b | 1394 | } |
d3c2ae1c | 1395 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
bacf366f | 1396 | #endif |
b9541d6b CW |
1397 | } |
1398 | ||
a7004725 DK |
1399 | /* |
1400 | * Return true iff at least one of the bufs on hdr is not compressed. | |
b5256303 | 1401 | * Encrypted buffers count as compressed. |
a7004725 DK |
1402 | */ |
1403 | static boolean_t | |
1404 | arc_hdr_has_uncompressed_buf(arc_buf_hdr_t *hdr) | |
1405 | { | |
ca6c7a94 | 1406 | ASSERT(hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY_OR_LOCKED(hdr)); |
149ce888 | 1407 | |
a7004725 DK |
1408 | for (arc_buf_t *b = hdr->b_l1hdr.b_buf; b != NULL; b = b->b_next) { |
1409 | if (!ARC_BUF_COMPRESSED(b)) { | |
1410 | return (B_TRUE); | |
1411 | } | |
1412 | } | |
1413 | return (B_FALSE); | |
1414 | } | |
1415 | ||
1416 | ||
524b4217 DK |
1417 | /* |
1418 | * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data | |
1419 | * matches the checksum that is stored in the hdr. If there is no checksum, | |
1420 | * or if the buf is compressed, this is a no-op. | |
1421 | */ | |
34dc7c2f BB |
1422 | static void |
1423 | arc_cksum_verify(arc_buf_t *buf) | |
1424 | { | |
bacf366f | 1425 | #ifdef ZFS_DEBUG |
d3c2ae1c | 1426 | arc_buf_hdr_t *hdr = buf->b_hdr; |
34dc7c2f BB |
1427 | zio_cksum_t zc; |
1428 | ||
1429 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
1430 | return; | |
1431 | ||
149ce888 | 1432 | if (ARC_BUF_COMPRESSED(buf)) |
524b4217 | 1433 | return; |
524b4217 | 1434 | |
d3c2ae1c GW |
1435 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1436 | ||
1437 | mutex_enter(&hdr->b_l1hdr.b_freeze_lock); | |
149ce888 | 1438 | |
d3c2ae1c GW |
1439 | if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) { |
1440 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); | |
34dc7c2f BB |
1441 | return; |
1442 | } | |
2aa34383 | 1443 | |
3c67d83a | 1444 | fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, &zc); |
d3c2ae1c | 1445 | if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc)) |
34dc7c2f | 1446 | panic("buffer modified while frozen!"); |
d3c2ae1c | 1447 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
bacf366f | 1448 | #endif |
34dc7c2f BB |
1449 | } |
1450 | ||
b5256303 TC |
1451 | /* |
1452 | * This function makes the assumption that data stored in the L2ARC | |
1453 | * will be transformed exactly as it is in the main pool. Because of | |
1454 | * this we can verify the checksum against the reading process's bp. | |
1455 | */ | |
d3c2ae1c GW |
1456 | static boolean_t |
1457 | arc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio) | |
34dc7c2f | 1458 | { |
d3c2ae1c GW |
1459 | ASSERT(!BP_IS_EMBEDDED(zio->io_bp)); |
1460 | VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr)); | |
34dc7c2f | 1461 | |
d3c2ae1c GW |
1462 | /* |
1463 | * Block pointers always store the checksum for the logical data. | |
1464 | * If the block pointer has the gang bit set, then the checksum | |
1465 | * it represents is for the reconstituted data and not for an | |
1466 | * individual gang member. The zio pipeline, however, must be able to | |
1467 | * determine the checksum of each of the gang constituents so it | |
1468 | * treats the checksum comparison differently than what we need | |
1469 | * for l2arc blocks. This prevents us from using the | |
1470 | * zio_checksum_error() interface directly. Instead we must call the | |
1471 | * zio_checksum_error_impl() so that we can ensure the checksum is | |
1472 | * generated using the correct checksum algorithm and accounts for the | |
1473 | * logical I/O size and not just a gang fragment. | |
1474 | */ | |
b5256303 | 1475 | return (zio_checksum_error_impl(zio->io_spa, zio->io_bp, |
a6255b7f | 1476 | BP_GET_CHECKSUM(zio->io_bp), zio->io_abd, zio->io_size, |
d3c2ae1c | 1477 | zio->io_offset, NULL) == 0); |
34dc7c2f BB |
1478 | } |
1479 | ||
524b4217 DK |
1480 | /* |
1481 | * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a | |
1482 | * checksum and attaches it to the buf's hdr so that we can ensure that the buf | |
1483 | * isn't modified later on. If buf is compressed or there is already a checksum | |
1484 | * on the hdr, this is a no-op (we only checksum uncompressed bufs). | |
1485 | */ | |
34dc7c2f | 1486 | static void |
d3c2ae1c | 1487 | arc_cksum_compute(arc_buf_t *buf) |
34dc7c2f | 1488 | { |
d3c2ae1c | 1489 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) |
34dc7c2f BB |
1490 | return; |
1491 | ||
bacf366f AM |
1492 | #ifdef ZFS_DEBUG |
1493 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
d3c2ae1c | 1494 | ASSERT(HDR_HAS_L1HDR(hdr)); |
bacf366f | 1495 | mutex_enter(&hdr->b_l1hdr.b_freeze_lock); |
149ce888 | 1496 | if (hdr->b_l1hdr.b_freeze_cksum != NULL || ARC_BUF_COMPRESSED(buf)) { |
d3c2ae1c | 1497 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1498 | return; |
1499 | } | |
2aa34383 | 1500 | |
b5256303 | 1501 | ASSERT(!ARC_BUF_ENCRYPTED(buf)); |
2aa34383 | 1502 | ASSERT(!ARC_BUF_COMPRESSED(buf)); |
d3c2ae1c GW |
1503 | hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), |
1504 | KM_SLEEP); | |
3c67d83a | 1505 | fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, |
d3c2ae1c GW |
1506 | hdr->b_l1hdr.b_freeze_cksum); |
1507 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); | |
bacf366f | 1508 | #endif |
498877ba MA |
1509 | arc_buf_watch(buf); |
1510 | } | |
1511 | ||
1512 | #ifndef _KERNEL | |
1513 | void | |
1514 | arc_buf_sigsegv(int sig, siginfo_t *si, void *unused) | |
1515 | { | |
14e4e3cb | 1516 | (void) sig, (void) unused; |
02730c33 | 1517 | panic("Got SIGSEGV at address: 0x%lx\n", (long)si->si_addr); |
498877ba MA |
1518 | } |
1519 | #endif | |
1520 | ||
498877ba MA |
1521 | static void |
1522 | arc_buf_unwatch(arc_buf_t *buf) | |
1523 | { | |
1524 | #ifndef _KERNEL | |
1525 | if (arc_watch) { | |
a7004725 | 1526 | ASSERT0(mprotect(buf->b_data, arc_buf_size(buf), |
498877ba MA |
1527 | PROT_READ | PROT_WRITE)); |
1528 | } | |
14e4e3cb AZ |
1529 | #else |
1530 | (void) buf; | |
498877ba MA |
1531 | #endif |
1532 | } | |
1533 | ||
498877ba MA |
1534 | static void |
1535 | arc_buf_watch(arc_buf_t *buf) | |
1536 | { | |
1537 | #ifndef _KERNEL | |
1538 | if (arc_watch) | |
2aa34383 | 1539 | ASSERT0(mprotect(buf->b_data, arc_buf_size(buf), |
d3c2ae1c | 1540 | PROT_READ)); |
14e4e3cb AZ |
1541 | #else |
1542 | (void) buf; | |
498877ba | 1543 | #endif |
34dc7c2f BB |
1544 | } |
1545 | ||
b9541d6b CW |
1546 | static arc_buf_contents_t |
1547 | arc_buf_type(arc_buf_hdr_t *hdr) | |
1548 | { | |
d3c2ae1c | 1549 | arc_buf_contents_t type; |
b9541d6b | 1550 | if (HDR_ISTYPE_METADATA(hdr)) { |
d3c2ae1c | 1551 | type = ARC_BUFC_METADATA; |
b9541d6b | 1552 | } else { |
d3c2ae1c | 1553 | type = ARC_BUFC_DATA; |
b9541d6b | 1554 | } |
d3c2ae1c GW |
1555 | VERIFY3U(hdr->b_type, ==, type); |
1556 | return (type); | |
b9541d6b CW |
1557 | } |
1558 | ||
2aa34383 DK |
1559 | boolean_t |
1560 | arc_is_metadata(arc_buf_t *buf) | |
1561 | { | |
1562 | return (HDR_ISTYPE_METADATA(buf->b_hdr) != 0); | |
1563 | } | |
1564 | ||
b9541d6b CW |
1565 | static uint32_t |
1566 | arc_bufc_to_flags(arc_buf_contents_t type) | |
1567 | { | |
1568 | switch (type) { | |
1569 | case ARC_BUFC_DATA: | |
1570 | /* metadata field is 0 if buffer contains normal data */ | |
1571 | return (0); | |
1572 | case ARC_BUFC_METADATA: | |
1573 | return (ARC_FLAG_BUFC_METADATA); | |
1574 | default: | |
1575 | break; | |
1576 | } | |
1577 | panic("undefined ARC buffer type!"); | |
1578 | return ((uint32_t)-1); | |
1579 | } | |
1580 | ||
34dc7c2f BB |
1581 | void |
1582 | arc_buf_thaw(arc_buf_t *buf) | |
1583 | { | |
d3c2ae1c GW |
1584 | arc_buf_hdr_t *hdr = buf->b_hdr; |
1585 | ||
2aa34383 DK |
1586 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
1587 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
1588 | ||
524b4217 | 1589 | arc_cksum_verify(buf); |
34dc7c2f | 1590 | |
2aa34383 | 1591 | /* |
149ce888 | 1592 | * Compressed buffers do not manipulate the b_freeze_cksum. |
2aa34383 | 1593 | */ |
149ce888 | 1594 | if (ARC_BUF_COMPRESSED(buf)) |
2aa34383 | 1595 | return; |
2aa34383 | 1596 | |
d3c2ae1c GW |
1597 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1598 | arc_cksum_free(hdr); | |
498877ba | 1599 | arc_buf_unwatch(buf); |
34dc7c2f BB |
1600 | } |
1601 | ||
1602 | void | |
1603 | arc_buf_freeze(arc_buf_t *buf) | |
1604 | { | |
1605 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
1606 | return; | |
1607 | ||
149ce888 | 1608 | if (ARC_BUF_COMPRESSED(buf)) |
2aa34383 | 1609 | return; |
428870ff | 1610 | |
149ce888 | 1611 | ASSERT(HDR_HAS_L1HDR(buf->b_hdr)); |
d3c2ae1c | 1612 | arc_cksum_compute(buf); |
34dc7c2f BB |
1613 | } |
1614 | ||
d3c2ae1c GW |
1615 | /* |
1616 | * The arc_buf_hdr_t's b_flags should never be modified directly. Instead, | |
1617 | * the following functions should be used to ensure that the flags are | |
1618 | * updated in a thread-safe way. When manipulating the flags either | |
1619 | * the hash_lock must be held or the hdr must be undiscoverable. This | |
1620 | * ensures that we're not racing with any other threads when updating | |
1621 | * the flags. | |
1622 | */ | |
1623 | static inline void | |
1624 | arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) | |
1625 | { | |
ca6c7a94 | 1626 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
1627 | hdr->b_flags |= flags; |
1628 | } | |
1629 | ||
1630 | static inline void | |
1631 | arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) | |
1632 | { | |
ca6c7a94 | 1633 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
1634 | hdr->b_flags &= ~flags; |
1635 | } | |
1636 | ||
1637 | /* | |
1638 | * Setting the compression bits in the arc_buf_hdr_t's b_flags is | |
1639 | * done in a special way since we have to clear and set bits | |
1640 | * at the same time. Consumers that wish to set the compression bits | |
1641 | * must use this function to ensure that the flags are updated in | |
1642 | * thread-safe manner. | |
1643 | */ | |
1644 | static void | |
1645 | arc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp) | |
1646 | { | |
ca6c7a94 | 1647 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
1648 | |
1649 | /* | |
1650 | * Holes and embedded blocks will always have a psize = 0 so | |
1651 | * we ignore the compression of the blkptr and set the | |
d3c2ae1c GW |
1652 | * want to uncompress them. Mark them as uncompressed. |
1653 | */ | |
1654 | if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) { | |
1655 | arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC); | |
d3c2ae1c | 1656 | ASSERT(!HDR_COMPRESSION_ENABLED(hdr)); |
d3c2ae1c GW |
1657 | } else { |
1658 | arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC); | |
d3c2ae1c GW |
1659 | ASSERT(HDR_COMPRESSION_ENABLED(hdr)); |
1660 | } | |
b5256303 TC |
1661 | |
1662 | HDR_SET_COMPRESS(hdr, cmp); | |
1663 | ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp); | |
d3c2ae1c GW |
1664 | } |
1665 | ||
524b4217 DK |
1666 | /* |
1667 | * Looks for another buf on the same hdr which has the data decompressed, copies | |
1668 | * from it, and returns true. If no such buf exists, returns false. | |
1669 | */ | |
1670 | static boolean_t | |
1671 | arc_buf_try_copy_decompressed_data(arc_buf_t *buf) | |
1672 | { | |
1673 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
524b4217 DK |
1674 | boolean_t copied = B_FALSE; |
1675 | ||
1676 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
1677 | ASSERT3P(buf->b_data, !=, NULL); | |
1678 | ASSERT(!ARC_BUF_COMPRESSED(buf)); | |
1679 | ||
a7004725 | 1680 | for (arc_buf_t *from = hdr->b_l1hdr.b_buf; from != NULL; |
524b4217 DK |
1681 | from = from->b_next) { |
1682 | /* can't use our own data buffer */ | |
1683 | if (from == buf) { | |
1684 | continue; | |
1685 | } | |
1686 | ||
1687 | if (!ARC_BUF_COMPRESSED(from)) { | |
861166b0 | 1688 | memcpy(buf->b_data, from->b_data, arc_buf_size(buf)); |
524b4217 DK |
1689 | copied = B_TRUE; |
1690 | break; | |
1691 | } | |
1692 | } | |
1693 | ||
bacf366f | 1694 | #ifdef ZFS_DEBUG |
524b4217 DK |
1695 | /* |
1696 | * There were no decompressed bufs, so there should not be a | |
1697 | * checksum on the hdr either. | |
1698 | */ | |
46db9d61 BB |
1699 | if (zfs_flags & ZFS_DEBUG_MODIFY) |
1700 | EQUIV(!copied, hdr->b_l1hdr.b_freeze_cksum == NULL); | |
bacf366f | 1701 | #endif |
524b4217 DK |
1702 | |
1703 | return (copied); | |
1704 | } | |
1705 | ||
77f6826b GA |
1706 | /* |
1707 | * Allocates an ARC buf header that's in an evicted & L2-cached state. | |
1708 | * This is used during l2arc reconstruction to make empty ARC buffers | |
1709 | * which circumvent the regular disk->arc->l2arc path and instead come | |
1710 | * into being in the reverse order, i.e. l2arc->arc. | |
1711 | */ | |
65c7cc49 | 1712 | static arc_buf_hdr_t * |
77f6826b GA |
1713 | arc_buf_alloc_l2only(size_t size, arc_buf_contents_t type, l2arc_dev_t *dev, |
1714 | dva_t dva, uint64_t daddr, int32_t psize, uint64_t birth, | |
10b3c7f5 | 1715 | enum zio_compress compress, uint8_t complevel, boolean_t protected, |
08532162 | 1716 | boolean_t prefetch, arc_state_type_t arcs_state) |
77f6826b GA |
1717 | { |
1718 | arc_buf_hdr_t *hdr; | |
1719 | ||
1720 | ASSERT(size != 0); | |
1721 | hdr = kmem_cache_alloc(hdr_l2only_cache, KM_SLEEP); | |
1722 | hdr->b_birth = birth; | |
1723 | hdr->b_type = type; | |
1724 | hdr->b_flags = 0; | |
1725 | arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L2HDR); | |
1726 | HDR_SET_LSIZE(hdr, size); | |
1727 | HDR_SET_PSIZE(hdr, psize); | |
1728 | arc_hdr_set_compress(hdr, compress); | |
10b3c7f5 | 1729 | hdr->b_complevel = complevel; |
77f6826b GA |
1730 | if (protected) |
1731 | arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED); | |
1732 | if (prefetch) | |
1733 | arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); | |
1734 | hdr->b_spa = spa_load_guid(dev->l2ad_vdev->vdev_spa); | |
1735 | ||
1736 | hdr->b_dva = dva; | |
1737 | ||
1738 | hdr->b_l2hdr.b_dev = dev; | |
1739 | hdr->b_l2hdr.b_daddr = daddr; | |
08532162 | 1740 | hdr->b_l2hdr.b_arcs_state = arcs_state; |
77f6826b GA |
1741 | |
1742 | return (hdr); | |
1743 | } | |
1744 | ||
b5256303 TC |
1745 | /* |
1746 | * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t. | |
1747 | */ | |
1748 | static uint64_t | |
1749 | arc_hdr_size(arc_buf_hdr_t *hdr) | |
1750 | { | |
1751 | uint64_t size; | |
1752 | ||
1753 | if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF && | |
1754 | HDR_GET_PSIZE(hdr) > 0) { | |
1755 | size = HDR_GET_PSIZE(hdr); | |
1756 | } else { | |
1757 | ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0); | |
1758 | size = HDR_GET_LSIZE(hdr); | |
1759 | } | |
1760 | return (size); | |
1761 | } | |
1762 | ||
1763 | static int | |
1764 | arc_hdr_authenticate(arc_buf_hdr_t *hdr, spa_t *spa, uint64_t dsobj) | |
1765 | { | |
1766 | int ret; | |
1767 | uint64_t csize; | |
1768 | uint64_t lsize = HDR_GET_LSIZE(hdr); | |
1769 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
1770 | void *tmpbuf = NULL; | |
1771 | abd_t *abd = hdr->b_l1hdr.b_pabd; | |
1772 | ||
ca6c7a94 | 1773 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
b5256303 TC |
1774 | ASSERT(HDR_AUTHENTICATED(hdr)); |
1775 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
1776 | ||
1777 | /* | |
1778 | * The MAC is calculated on the compressed data that is stored on disk. | |
1779 | * However, if compressed arc is disabled we will only have the | |
1780 | * decompressed data available to us now. Compress it into a temporary | |
1781 | * abd so we can verify the MAC. The performance overhead of this will | |
1782 | * be relatively low, since most objects in an encrypted objset will | |
1783 | * be encrypted (instead of authenticated) anyway. | |
1784 | */ | |
1785 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
1786 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
bff26b02 | 1787 | |
b5256303 | 1788 | csize = zio_compress_data(HDR_GET_COMPRESS(hdr), |
bff26b02 RY |
1789 | hdr->b_l1hdr.b_pabd, &tmpbuf, lsize, hdr->b_complevel); |
1790 | ASSERT3P(tmpbuf, !=, NULL); | |
b5256303 | 1791 | ASSERT3U(csize, <=, psize); |
bff26b02 RY |
1792 | abd = abd_get_from_buf(tmpbuf, lsize); |
1793 | abd_take_ownership_of_buf(abd, B_TRUE); | |
b5256303 TC |
1794 | abd_zero_off(abd, csize, psize - csize); |
1795 | } | |
1796 | ||
1797 | /* | |
1798 | * Authentication is best effort. We authenticate whenever the key is | |
1799 | * available. If we succeed we clear ARC_FLAG_NOAUTH. | |
1800 | */ | |
1801 | if (hdr->b_crypt_hdr.b_ot == DMU_OT_OBJSET) { | |
1802 | ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); | |
1803 | ASSERT3U(lsize, ==, psize); | |
1804 | ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa, dsobj, abd, | |
1805 | psize, hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
1806 | } else { | |
1807 | ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj, abd, psize, | |
1808 | hdr->b_crypt_hdr.b_mac); | |
1809 | } | |
1810 | ||
1811 | if (ret == 0) | |
1812 | arc_hdr_clear_flags(hdr, ARC_FLAG_NOAUTH); | |
1813 | else if (ret != ENOENT) | |
1814 | goto error; | |
1815 | ||
1816 | if (tmpbuf != NULL) | |
1817 | abd_free(abd); | |
1818 | ||
1819 | return (0); | |
1820 | ||
1821 | error: | |
1822 | if (tmpbuf != NULL) | |
1823 | abd_free(abd); | |
1824 | ||
1825 | return (ret); | |
1826 | } | |
1827 | ||
1828 | /* | |
1829 | * This function will take a header that only has raw encrypted data in | |
1830 | * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in | |
1831 | * b_l1hdr.b_pabd. If designated in the header flags, this function will | |
1832 | * also decompress the data. | |
1833 | */ | |
1834 | static int | |
be9a5c35 | 1835 | arc_hdr_decrypt(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb) |
b5256303 TC |
1836 | { |
1837 | int ret; | |
b5256303 TC |
1838 | abd_t *cabd = NULL; |
1839 | void *tmp = NULL; | |
1840 | boolean_t no_crypt = B_FALSE; | |
1841 | boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
1842 | ||
ca6c7a94 | 1843 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
b5256303 TC |
1844 | ASSERT(HDR_ENCRYPTED(hdr)); |
1845 | ||
a8d83e2a | 1846 | arc_hdr_alloc_abd(hdr, 0); |
b5256303 | 1847 | |
be9a5c35 TC |
1848 | ret = spa_do_crypt_abd(B_FALSE, spa, zb, hdr->b_crypt_hdr.b_ot, |
1849 | B_FALSE, bswap, hdr->b_crypt_hdr.b_salt, hdr->b_crypt_hdr.b_iv, | |
1850 | hdr->b_crypt_hdr.b_mac, HDR_GET_PSIZE(hdr), hdr->b_l1hdr.b_pabd, | |
b5256303 TC |
1851 | hdr->b_crypt_hdr.b_rabd, &no_crypt); |
1852 | if (ret != 0) | |
1853 | goto error; | |
1854 | ||
1855 | if (no_crypt) { | |
1856 | abd_copy(hdr->b_l1hdr.b_pabd, hdr->b_crypt_hdr.b_rabd, | |
1857 | HDR_GET_PSIZE(hdr)); | |
1858 | } | |
1859 | ||
1860 | /* | |
1861 | * If this header has disabled arc compression but the b_pabd is | |
1862 | * compressed after decrypting it, we need to decompress the newly | |
1863 | * decrypted data. | |
1864 | */ | |
1865 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
1866 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
1867 | /* | |
1868 | * We want to make sure that we are correctly honoring the | |
1869 | * zfs_abd_scatter_enabled setting, so we allocate an abd here | |
1870 | * and then loan a buffer from it, rather than allocating a | |
1871 | * linear buffer and wrapping it in an abd later. | |
1872 | */ | |
a8d83e2a | 1873 | cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, 0); |
b5256303 TC |
1874 | tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr)); |
1875 | ||
1876 | ret = zio_decompress_data(HDR_GET_COMPRESS(hdr), | |
1877 | hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr), | |
10b3c7f5 | 1878 | HDR_GET_LSIZE(hdr), &hdr->b_complevel); |
b5256303 TC |
1879 | if (ret != 0) { |
1880 | abd_return_buf(cabd, tmp, arc_hdr_size(hdr)); | |
1881 | goto error; | |
1882 | } | |
1883 | ||
1884 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
1885 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
1886 | arc_hdr_size(hdr), hdr); | |
1887 | hdr->b_l1hdr.b_pabd = cabd; | |
1888 | } | |
1889 | ||
b5256303 TC |
1890 | return (0); |
1891 | ||
1892 | error: | |
1893 | arc_hdr_free_abd(hdr, B_FALSE); | |
b5256303 TC |
1894 | if (cabd != NULL) |
1895 | arc_free_data_buf(hdr, cabd, arc_hdr_size(hdr), hdr); | |
1896 | ||
1897 | return (ret); | |
1898 | } | |
1899 | ||
1900 | /* | |
1901 | * This function is called during arc_buf_fill() to prepare the header's | |
1902 | * abd plaintext pointer for use. This involves authenticated protected | |
1903 | * data and decrypting encrypted data into the plaintext abd. | |
1904 | */ | |
1905 | static int | |
1906 | arc_fill_hdr_crypt(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, spa_t *spa, | |
be9a5c35 | 1907 | const zbookmark_phys_t *zb, boolean_t noauth) |
b5256303 TC |
1908 | { |
1909 | int ret; | |
1910 | ||
1911 | ASSERT(HDR_PROTECTED(hdr)); | |
1912 | ||
1913 | if (hash_lock != NULL) | |
1914 | mutex_enter(hash_lock); | |
1915 | ||
1916 | if (HDR_NOAUTH(hdr) && !noauth) { | |
1917 | /* | |
1918 | * The caller requested authenticated data but our data has | |
1919 | * not been authenticated yet. Verify the MAC now if we can. | |
1920 | */ | |
be9a5c35 | 1921 | ret = arc_hdr_authenticate(hdr, spa, zb->zb_objset); |
b5256303 TC |
1922 | if (ret != 0) |
1923 | goto error; | |
1924 | } else if (HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd == NULL) { | |
1925 | /* | |
1926 | * If we only have the encrypted version of the data, but the | |
1927 | * unencrypted version was requested we take this opportunity | |
1928 | * to store the decrypted version in the header for future use. | |
1929 | */ | |
be9a5c35 | 1930 | ret = arc_hdr_decrypt(hdr, spa, zb); |
b5256303 TC |
1931 | if (ret != 0) |
1932 | goto error; | |
1933 | } | |
1934 | ||
1935 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
1936 | ||
1937 | if (hash_lock != NULL) | |
1938 | mutex_exit(hash_lock); | |
1939 | ||
1940 | return (0); | |
1941 | ||
1942 | error: | |
1943 | if (hash_lock != NULL) | |
1944 | mutex_exit(hash_lock); | |
1945 | ||
1946 | return (ret); | |
1947 | } | |
1948 | ||
1949 | /* | |
1950 | * This function is used by the dbuf code to decrypt bonus buffers in place. | |
1951 | * The dbuf code itself doesn't have any locking for decrypting a shared dnode | |
1952 | * block, so we use the hash lock here to protect against concurrent calls to | |
1953 | * arc_buf_fill(). | |
1954 | */ | |
1955 | static void | |
14e4e3cb | 1956 | arc_buf_untransform_in_place(arc_buf_t *buf) |
b5256303 TC |
1957 | { |
1958 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
1959 | ||
1960 | ASSERT(HDR_ENCRYPTED(hdr)); | |
1961 | ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE); | |
ca6c7a94 | 1962 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
b5256303 TC |
1963 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
1964 | ||
1965 | zio_crypt_copy_dnode_bonus(hdr->b_l1hdr.b_pabd, buf->b_data, | |
1966 | arc_buf_size(buf)); | |
1967 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
1968 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
b5256303 TC |
1969 | } |
1970 | ||
524b4217 DK |
1971 | /* |
1972 | * Given a buf that has a data buffer attached to it, this function will | |
1973 | * efficiently fill the buf with data of the specified compression setting from | |
1974 | * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr | |
1975 | * are already sharing a data buf, no copy is performed. | |
1976 | * | |
1977 | * If the buf is marked as compressed but uncompressed data was requested, this | |
1978 | * will allocate a new data buffer for the buf, remove that flag, and fill the | |
1979 | * buf with uncompressed data. You can't request a compressed buf on a hdr with | |
1980 | * uncompressed data, and (since we haven't added support for it yet) if you | |
1981 | * want compressed data your buf must already be marked as compressed and have | |
1982 | * the correct-sized data buffer. | |
1983 | */ | |
1984 | static int | |
be9a5c35 TC |
1985 | arc_buf_fill(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb, |
1986 | arc_fill_flags_t flags) | |
d3c2ae1c | 1987 | { |
b5256303 | 1988 | int error = 0; |
d3c2ae1c | 1989 | arc_buf_hdr_t *hdr = buf->b_hdr; |
b5256303 TC |
1990 | boolean_t hdr_compressed = |
1991 | (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); | |
1992 | boolean_t compressed = (flags & ARC_FILL_COMPRESSED) != 0; | |
1993 | boolean_t encrypted = (flags & ARC_FILL_ENCRYPTED) != 0; | |
d3c2ae1c | 1994 | dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap; |
b5256303 | 1995 | kmutex_t *hash_lock = (flags & ARC_FILL_LOCKED) ? NULL : HDR_LOCK(hdr); |
d3c2ae1c | 1996 | |
524b4217 | 1997 | ASSERT3P(buf->b_data, !=, NULL); |
b5256303 | 1998 | IMPLY(compressed, hdr_compressed || ARC_BUF_ENCRYPTED(buf)); |
524b4217 | 1999 | IMPLY(compressed, ARC_BUF_COMPRESSED(buf)); |
b5256303 TC |
2000 | IMPLY(encrypted, HDR_ENCRYPTED(hdr)); |
2001 | IMPLY(encrypted, ARC_BUF_ENCRYPTED(buf)); | |
2002 | IMPLY(encrypted, ARC_BUF_COMPRESSED(buf)); | |
57b40985 | 2003 | IMPLY(encrypted, !arc_buf_is_shared(buf)); |
b5256303 TC |
2004 | |
2005 | /* | |
2006 | * If the caller wanted encrypted data we just need to copy it from | |
2007 | * b_rabd and potentially byteswap it. We won't be able to do any | |
2008 | * further transforms on it. | |
2009 | */ | |
2010 | if (encrypted) { | |
2011 | ASSERT(HDR_HAS_RABD(hdr)); | |
2012 | abd_copy_to_buf(buf->b_data, hdr->b_crypt_hdr.b_rabd, | |
2013 | HDR_GET_PSIZE(hdr)); | |
2014 | goto byteswap; | |
2015 | } | |
2016 | ||
2017 | /* | |
e1cfd73f | 2018 | * Adjust encrypted and authenticated headers to accommodate |
69830602 TC |
2019 | * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are |
2020 | * allowed to fail decryption due to keys not being loaded | |
2021 | * without being marked as an IO error. | |
b5256303 TC |
2022 | */ |
2023 | if (HDR_PROTECTED(hdr)) { | |
2024 | error = arc_fill_hdr_crypt(hdr, hash_lock, spa, | |
be9a5c35 | 2025 | zb, !!(flags & ARC_FILL_NOAUTH)); |
69830602 TC |
2026 | if (error == EACCES && (flags & ARC_FILL_IN_PLACE) != 0) { |
2027 | return (error); | |
2028 | } else if (error != 0) { | |
e7504d7a TC |
2029 | if (hash_lock != NULL) |
2030 | mutex_enter(hash_lock); | |
2c24b5b1 | 2031 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); |
e7504d7a TC |
2032 | if (hash_lock != NULL) |
2033 | mutex_exit(hash_lock); | |
b5256303 | 2034 | return (error); |
2c24b5b1 | 2035 | } |
b5256303 TC |
2036 | } |
2037 | ||
2038 | /* | |
2039 | * There is a special case here for dnode blocks which are | |
2040 | * decrypting their bonus buffers. These blocks may request to | |
2041 | * be decrypted in-place. This is necessary because there may | |
2042 | * be many dnodes pointing into this buffer and there is | |
2043 | * currently no method to synchronize replacing the backing | |
2044 | * b_data buffer and updating all of the pointers. Here we use | |
2045 | * the hash lock to ensure there are no races. If the need | |
2046 | * arises for other types to be decrypted in-place, they must | |
2047 | * add handling here as well. | |
2048 | */ | |
2049 | if ((flags & ARC_FILL_IN_PLACE) != 0) { | |
2050 | ASSERT(!hdr_compressed); | |
2051 | ASSERT(!compressed); | |
2052 | ASSERT(!encrypted); | |
2053 | ||
2054 | if (HDR_ENCRYPTED(hdr) && ARC_BUF_ENCRYPTED(buf)) { | |
2055 | ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE); | |
2056 | ||
2057 | if (hash_lock != NULL) | |
2058 | mutex_enter(hash_lock); | |
14e4e3cb | 2059 | arc_buf_untransform_in_place(buf); |
b5256303 TC |
2060 | if (hash_lock != NULL) |
2061 | mutex_exit(hash_lock); | |
2062 | ||
2063 | /* Compute the hdr's checksum if necessary */ | |
2064 | arc_cksum_compute(buf); | |
2065 | } | |
2066 | ||
2067 | return (0); | |
2068 | } | |
524b4217 DK |
2069 | |
2070 | if (hdr_compressed == compressed) { | |
57b40985 AM |
2071 | if (ARC_BUF_SHARED(buf)) { |
2072 | ASSERT(arc_buf_is_shared(buf)); | |
2073 | } else { | |
a6255b7f | 2074 | abd_copy_to_buf(buf->b_data, hdr->b_l1hdr.b_pabd, |
524b4217 | 2075 | arc_buf_size(buf)); |
2aa34383 | 2076 | } |
d3c2ae1c | 2077 | } else { |
524b4217 DK |
2078 | ASSERT(hdr_compressed); |
2079 | ASSERT(!compressed); | |
2aa34383 DK |
2080 | |
2081 | /* | |
524b4217 DK |
2082 | * If the buf is sharing its data with the hdr, unlink it and |
2083 | * allocate a new data buffer for the buf. | |
2aa34383 | 2084 | */ |
57b40985 | 2085 | if (ARC_BUF_SHARED(buf)) { |
524b4217 DK |
2086 | ASSERT(ARC_BUF_COMPRESSED(buf)); |
2087 | ||
e1cfd73f | 2088 | /* We need to give the buf its own b_data */ |
524b4217 | 2089 | buf->b_flags &= ~ARC_BUF_FLAG_SHARED; |
2aa34383 DK |
2090 | buf->b_data = |
2091 | arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); | |
2092 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); | |
2093 | ||
524b4217 | 2094 | /* Previously overhead was 0; just add new overhead */ |
2aa34383 | 2095 | ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); |
524b4217 | 2096 | } else if (ARC_BUF_COMPRESSED(buf)) { |
57b40985 AM |
2097 | ASSERT(!arc_buf_is_shared(buf)); |
2098 | ||
524b4217 DK |
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); | |
493fcce9 | 2186 | spa_log_error(spa, zb, buf->b_hdr->b_birth); |
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)) { | |
d3c2ae1c | 2207 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
a6255b7f | 2208 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2209 | ASSERT(!HDR_HAS_RABD(hdr)); |
424fd7c3 | 2210 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
2aa34383 | 2211 | HDR_GET_LSIZE(hdr), hdr); |
d3c2ae1c GW |
2212 | return; |
2213 | } | |
2214 | ||
a6255b7f | 2215 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 | 2216 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
d3c2ae1c GW |
2217 | arc_hdr_size(hdr), hdr); |
2218 | } | |
b5256303 | 2219 | if (HDR_HAS_RABD(hdr)) { |
424fd7c3 | 2220 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
b5256303 TC |
2221 | HDR_GET_PSIZE(hdr), hdr); |
2222 | } | |
2223 | ||
1c27024e DB |
2224 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
2225 | buf = buf->b_next) { | |
57b40985 | 2226 | if (ARC_BUF_SHARED(buf)) |
d3c2ae1c | 2227 | continue; |
424fd7c3 | 2228 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
2aa34383 | 2229 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2230 | } |
2231 | } | |
2232 | ||
2233 | /* | |
2234 | * Decrement the amount of evictable space in the arc_state_t's refcount. | |
2235 | * We account for the space used by the hdr and the arc buf individually | |
2236 | * so that we can add and remove them from the refcount individually. | |
2237 | */ | |
2238 | static void | |
2aa34383 | 2239 | arc_evictable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state) |
d3c2ae1c GW |
2240 | { |
2241 | arc_buf_contents_t type = arc_buf_type(hdr); | |
d3c2ae1c GW |
2242 | |
2243 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
2244 | ||
2245 | if (GHOST_STATE(state)) { | |
d3c2ae1c | 2246 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
a6255b7f | 2247 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2248 | ASSERT(!HDR_HAS_RABD(hdr)); |
424fd7c3 | 2249 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
2aa34383 | 2250 | HDR_GET_LSIZE(hdr), hdr); |
d3c2ae1c GW |
2251 | return; |
2252 | } | |
2253 | ||
a6255b7f | 2254 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 | 2255 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c GW |
2256 | arc_hdr_size(hdr), hdr); |
2257 | } | |
b5256303 | 2258 | if (HDR_HAS_RABD(hdr)) { |
424fd7c3 | 2259 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
b5256303 TC |
2260 | HDR_GET_PSIZE(hdr), hdr); |
2261 | } | |
2262 | ||
1c27024e DB |
2263 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
2264 | buf = buf->b_next) { | |
57b40985 | 2265 | if (ARC_BUF_SHARED(buf)) |
d3c2ae1c | 2266 | continue; |
424fd7c3 | 2267 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
2aa34383 | 2268 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2269 | } |
2270 | } | |
2271 | ||
2272 | /* | |
2273 | * Add a reference to this hdr indicating that someone is actively | |
2274 | * referencing that memory. When the refcount transitions from 0 to 1, | |
2275 | * we remove it from the respective arc_state_t list to indicate that | |
2276 | * it is not evictable. | |
2277 | */ | |
2278 | static void | |
a926aab9 | 2279 | add_reference(arc_buf_hdr_t *hdr, const void *tag) |
34dc7c2f | 2280 | { |
c935fe2e | 2281 | arc_state_t *state = hdr->b_l1hdr.b_state; |
b9541d6b CW |
2282 | |
2283 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
ca6c7a94 | 2284 | if (!HDR_EMPTY(hdr) && !MUTEX_HELD(HDR_LOCK(hdr))) { |
c935fe2e | 2285 | ASSERT(state == arc_anon); |
424fd7c3 | 2286 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
2287 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
2288 | } | |
34dc7c2f | 2289 | |
c13060e4 | 2290 | if ((zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && |
c935fe2e | 2291 | state != arc_anon && state != arc_l2c_only) { |
b9541d6b | 2292 | /* We don't use the L2-only state list. */ |
c935fe2e AM |
2293 | multilist_remove(&state->arcs_list[arc_buf_type(hdr)], hdr); |
2294 | arc_evictable_space_decrement(hdr, state); | |
34dc7c2f BB |
2295 | } |
2296 | } | |
2297 | ||
d3c2ae1c GW |
2298 | /* |
2299 | * Remove a reference from this hdr. When the reference transitions from | |
2300 | * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's | |
2301 | * list making it eligible for eviction. | |
2302 | */ | |
34dc7c2f | 2303 | static int |
c935fe2e | 2304 | remove_reference(arc_buf_hdr_t *hdr, const void *tag) |
34dc7c2f BB |
2305 | { |
2306 | int cnt; | |
b9541d6b | 2307 | arc_state_t *state = hdr->b_l1hdr.b_state; |
34dc7c2f | 2308 | |
b9541d6b | 2309 | ASSERT(HDR_HAS_L1HDR(hdr)); |
c935fe2e | 2310 | ASSERT(state == arc_anon || MUTEX_HELD(HDR_LOCK(hdr))); |
ed2f7ba0 | 2311 | ASSERT(!GHOST_STATE(state)); /* arc_l2c_only counts as a ghost. */ |
34dc7c2f | 2312 | |
ed2f7ba0 AM |
2313 | if ((cnt = zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) != 0) |
2314 | return (cnt); | |
2315 | ||
2316 | if (state == arc_anon) { | |
2317 | arc_hdr_destroy(hdr); | |
2318 | return (0); | |
34dc7c2f | 2319 | } |
ed2f7ba0 AM |
2320 | if (state == arc_uncached && !HDR_PREFETCH(hdr)) { |
2321 | arc_change_state(arc_anon, hdr); | |
2322 | arc_hdr_destroy(hdr); | |
2323 | return (0); | |
2324 | } | |
2325 | multilist_insert(&state->arcs_list[arc_buf_type(hdr)], hdr); | |
2326 | arc_evictable_space_increment(hdr, state); | |
2327 | return (0); | |
34dc7c2f BB |
2328 | } |
2329 | ||
e0b0ca98 BB |
2330 | /* |
2331 | * Returns detailed information about a specific arc buffer. When the | |
2332 | * state_index argument is set the function will calculate the arc header | |
2333 | * list position for its arc state. Since this requires a linear traversal | |
2334 | * callers are strongly encourage not to do this. However, it can be helpful | |
2335 | * for targeted analysis so the functionality is provided. | |
2336 | */ | |
2337 | void | |
2338 | arc_buf_info(arc_buf_t *ab, arc_buf_info_t *abi, int state_index) | |
2339 | { | |
14e4e3cb | 2340 | (void) state_index; |
e0b0ca98 | 2341 | arc_buf_hdr_t *hdr = ab->b_hdr; |
b9541d6b CW |
2342 | l1arc_buf_hdr_t *l1hdr = NULL; |
2343 | l2arc_buf_hdr_t *l2hdr = NULL; | |
2344 | arc_state_t *state = NULL; | |
2345 | ||
8887c7d7 TC |
2346 | memset(abi, 0, sizeof (arc_buf_info_t)); |
2347 | ||
2348 | if (hdr == NULL) | |
2349 | return; | |
2350 | ||
2351 | abi->abi_flags = hdr->b_flags; | |
2352 | ||
b9541d6b CW |
2353 | if (HDR_HAS_L1HDR(hdr)) { |
2354 | l1hdr = &hdr->b_l1hdr; | |
2355 | state = l1hdr->b_state; | |
2356 | } | |
2357 | if (HDR_HAS_L2HDR(hdr)) | |
2358 | l2hdr = &hdr->b_l2hdr; | |
e0b0ca98 | 2359 | |
b9541d6b | 2360 | if (l1hdr) { |
96b9cf42 AM |
2361 | abi->abi_bufcnt = 0; |
2362 | for (arc_buf_t *buf = l1hdr->b_buf; buf; buf = buf->b_next) | |
2363 | abi->abi_bufcnt++; | |
b9541d6b CW |
2364 | abi->abi_access = l1hdr->b_arc_access; |
2365 | abi->abi_mru_hits = l1hdr->b_mru_hits; | |
2366 | abi->abi_mru_ghost_hits = l1hdr->b_mru_ghost_hits; | |
2367 | abi->abi_mfu_hits = l1hdr->b_mfu_hits; | |
2368 | abi->abi_mfu_ghost_hits = l1hdr->b_mfu_ghost_hits; | |
424fd7c3 | 2369 | abi->abi_holds = zfs_refcount_count(&l1hdr->b_refcnt); |
b9541d6b CW |
2370 | } |
2371 | ||
2372 | if (l2hdr) { | |
2373 | abi->abi_l2arc_dattr = l2hdr->b_daddr; | |
b9541d6b CW |
2374 | abi->abi_l2arc_hits = l2hdr->b_hits; |
2375 | } | |
2376 | ||
e0b0ca98 | 2377 | abi->abi_state_type = state ? state->arcs_state : ARC_STATE_ANON; |
b9541d6b | 2378 | abi->abi_state_contents = arc_buf_type(hdr); |
d3c2ae1c | 2379 | abi->abi_size = arc_hdr_size(hdr); |
e0b0ca98 BB |
2380 | } |
2381 | ||
34dc7c2f | 2382 | /* |
ca0bf58d | 2383 | * Move the supplied buffer to the indicated state. The hash lock |
34dc7c2f BB |
2384 | * for the buffer must be held by the caller. |
2385 | */ | |
2386 | static void | |
c935fe2e | 2387 | arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr) |
34dc7c2f | 2388 | { |
b9541d6b CW |
2389 | arc_state_t *old_state; |
2390 | int64_t refcnt; | |
d3c2ae1c | 2391 | boolean_t update_old, update_new; |
a8d83e2a | 2392 | arc_buf_contents_t type = arc_buf_type(hdr); |
b9541d6b CW |
2393 | |
2394 | /* | |
2395 | * We almost always have an L1 hdr here, since we call arc_hdr_realloc() | |
2396 | * in arc_read() when bringing a buffer out of the L2ARC. However, the | |
2397 | * L1 hdr doesn't always exist when we change state to arc_anon before | |
2398 | * destroying a header, in which case reallocating to add the L1 hdr is | |
2399 | * pointless. | |
2400 | */ | |
2401 | if (HDR_HAS_L1HDR(hdr)) { | |
2402 | old_state = hdr->b_l1hdr.b_state; | |
424fd7c3 | 2403 | refcnt = zfs_refcount_count(&hdr->b_l1hdr.b_refcnt); |
96b9cf42 AM |
2404 | update_old = (hdr->b_l1hdr.b_buf != NULL || |
2405 | hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); | |
c935fe2e | 2406 | |
c935fe2e AM |
2407 | IMPLY(GHOST_STATE(old_state), hdr->b_l1hdr.b_buf == NULL); |
2408 | IMPLY(GHOST_STATE(new_state), hdr->b_l1hdr.b_buf == NULL); | |
96b9cf42 AM |
2409 | IMPLY(old_state == arc_anon, hdr->b_l1hdr.b_buf == NULL || |
2410 | ARC_BUF_LAST(hdr->b_l1hdr.b_buf)); | |
b9541d6b CW |
2411 | } else { |
2412 | old_state = arc_l2c_only; | |
2413 | refcnt = 0; | |
d3c2ae1c | 2414 | update_old = B_FALSE; |
b9541d6b | 2415 | } |
d3c2ae1c | 2416 | update_new = update_old; |
c935fe2e AM |
2417 | if (GHOST_STATE(old_state)) |
2418 | update_old = B_TRUE; | |
2419 | if (GHOST_STATE(new_state)) | |
2420 | update_new = B_TRUE; | |
34dc7c2f | 2421 | |
c935fe2e | 2422 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); |
e8b96c60 | 2423 | ASSERT3P(new_state, !=, old_state); |
34dc7c2f BB |
2424 | |
2425 | /* | |
2426 | * If this buffer is evictable, transfer it from the | |
2427 | * old state list to the new state list. | |
2428 | */ | |
2429 | if (refcnt == 0) { | |
b9541d6b | 2430 | if (old_state != arc_anon && old_state != arc_l2c_only) { |
b9541d6b | 2431 | ASSERT(HDR_HAS_L1HDR(hdr)); |
ed2f7ba0 AM |
2432 | /* remove_reference() saves on insert. */ |
2433 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
a8d83e2a | 2434 | multilist_remove(&old_state->arcs_list[type], |
ed2f7ba0 AM |
2435 | hdr); |
2436 | arc_evictable_space_decrement(hdr, old_state); | |
2437 | } | |
34dc7c2f | 2438 | } |
b9541d6b | 2439 | if (new_state != arc_anon && new_state != arc_l2c_only) { |
b9541d6b CW |
2440 | /* |
2441 | * An L1 header always exists here, since if we're | |
2442 | * moving to some L1-cached state (i.e. not l2c_only or | |
2443 | * anonymous), we realloc the header to add an L1hdr | |
2444 | * beforehand. | |
2445 | */ | |
2446 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
a8d83e2a | 2447 | multilist_insert(&new_state->arcs_list[type], hdr); |
d3c2ae1c | 2448 | arc_evictable_space_increment(hdr, new_state); |
34dc7c2f BB |
2449 | } |
2450 | } | |
2451 | ||
d3c2ae1c | 2452 | ASSERT(!HDR_EMPTY(hdr)); |
2a432414 GW |
2453 | if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) |
2454 | buf_hash_remove(hdr); | |
34dc7c2f | 2455 | |
b9541d6b | 2456 | /* adjust state sizes (ignore arc_l2c_only) */ |
36da08ef | 2457 | |
d3c2ae1c | 2458 | if (update_new && new_state != arc_l2c_only) { |
36da08ef PS |
2459 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2460 | if (GHOST_STATE(new_state)) { | |
36da08ef PS |
2461 | |
2462 | /* | |
d3c2ae1c | 2463 | * When moving a header to a ghost state, we first |
96b9cf42 AM |
2464 | * remove all arc buffers. Thus, we'll have no arc |
2465 | * buffer to use for the reference. As a result, we | |
2466 | * use the arc header pointer for the reference. | |
36da08ef | 2467 | */ |
a8d83e2a AM |
2468 | (void) zfs_refcount_add_many( |
2469 | &new_state->arcs_size[type], | |
d3c2ae1c | 2470 | HDR_GET_LSIZE(hdr), hdr); |
a6255b7f | 2471 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2472 | ASSERT(!HDR_HAS_RABD(hdr)); |
36da08ef | 2473 | } else { |
36da08ef PS |
2474 | |
2475 | /* | |
2476 | * Each individual buffer holds a unique reference, | |
2477 | * thus we must remove each of these references one | |
2478 | * at a time. | |
2479 | */ | |
1c27024e | 2480 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
36da08ef | 2481 | buf = buf->b_next) { |
d3c2ae1c GW |
2482 | |
2483 | /* | |
2484 | * When the arc_buf_t is sharing the data | |
2485 | * block with the hdr, the owner of the | |
2486 | * reference belongs to the hdr. Only | |
2487 | * add to the refcount if the arc_buf_t is | |
2488 | * not shared. | |
2489 | */ | |
57b40985 | 2490 | if (ARC_BUF_SHARED(buf)) |
d3c2ae1c | 2491 | continue; |
d3c2ae1c | 2492 | |
424fd7c3 | 2493 | (void) zfs_refcount_add_many( |
a8d83e2a | 2494 | &new_state->arcs_size[type], |
2aa34383 | 2495 | arc_buf_size(buf), buf); |
d3c2ae1c | 2496 | } |
d3c2ae1c | 2497 | |
a6255b7f | 2498 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 | 2499 | (void) zfs_refcount_add_many( |
a8d83e2a | 2500 | &new_state->arcs_size[type], |
d3c2ae1c | 2501 | arc_hdr_size(hdr), hdr); |
b5256303 TC |
2502 | } |
2503 | ||
2504 | if (HDR_HAS_RABD(hdr)) { | |
424fd7c3 | 2505 | (void) zfs_refcount_add_many( |
a8d83e2a | 2506 | &new_state->arcs_size[type], |
b5256303 | 2507 | HDR_GET_PSIZE(hdr), hdr); |
36da08ef PS |
2508 | } |
2509 | } | |
2510 | } | |
2511 | ||
d3c2ae1c | 2512 | if (update_old && old_state != arc_l2c_only) { |
36da08ef PS |
2513 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2514 | if (GHOST_STATE(old_state)) { | |
a6255b7f | 2515 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2516 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c | 2517 | |
36da08ef PS |
2518 | /* |
2519 | * When moving a header off of a ghost state, | |
d3c2ae1c GW |
2520 | * the header will not contain any arc buffers. |
2521 | * We use the arc header pointer for the reference | |
2522 | * which is exactly what we did when we put the | |
2523 | * header on the ghost state. | |
36da08ef PS |
2524 | */ |
2525 | ||
a8d83e2a AM |
2526 | (void) zfs_refcount_remove_many( |
2527 | &old_state->arcs_size[type], | |
d3c2ae1c | 2528 | HDR_GET_LSIZE(hdr), hdr); |
36da08ef | 2529 | } else { |
36da08ef PS |
2530 | |
2531 | /* | |
2532 | * Each individual buffer holds a unique reference, | |
2533 | * thus we must remove each of these references one | |
2534 | * at a time. | |
2535 | */ | |
1c27024e | 2536 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
36da08ef | 2537 | buf = buf->b_next) { |
d3c2ae1c GW |
2538 | |
2539 | /* | |
2540 | * When the arc_buf_t is sharing the data | |
2541 | * block with the hdr, the owner of the | |
2542 | * reference belongs to the hdr. Only | |
2543 | * add to the refcount if the arc_buf_t is | |
2544 | * not shared. | |
2545 | */ | |
57b40985 | 2546 | if (ARC_BUF_SHARED(buf)) |
d3c2ae1c | 2547 | continue; |
d3c2ae1c | 2548 | |
424fd7c3 | 2549 | (void) zfs_refcount_remove_many( |
a8d83e2a AM |
2550 | &old_state->arcs_size[type], |
2551 | arc_buf_size(buf), buf); | |
36da08ef | 2552 | } |
b5256303 TC |
2553 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || |
2554 | HDR_HAS_RABD(hdr)); | |
2555 | ||
2556 | if (hdr->b_l1hdr.b_pabd != NULL) { | |
424fd7c3 | 2557 | (void) zfs_refcount_remove_many( |
a8d83e2a AM |
2558 | &old_state->arcs_size[type], |
2559 | arc_hdr_size(hdr), hdr); | |
b5256303 TC |
2560 | } |
2561 | ||
2562 | if (HDR_HAS_RABD(hdr)) { | |
424fd7c3 | 2563 | (void) zfs_refcount_remove_many( |
a8d83e2a AM |
2564 | &old_state->arcs_size[type], |
2565 | HDR_GET_PSIZE(hdr), hdr); | |
b5256303 | 2566 | } |
36da08ef | 2567 | } |
34dc7c2f | 2568 | } |
36da08ef | 2569 | |
08532162 | 2570 | if (HDR_HAS_L1HDR(hdr)) { |
b9541d6b | 2571 | hdr->b_l1hdr.b_state = new_state; |
34dc7c2f | 2572 | |
08532162 GA |
2573 | if (HDR_HAS_L2HDR(hdr) && new_state != arc_l2c_only) { |
2574 | l2arc_hdr_arcstats_decrement_state(hdr); | |
2575 | hdr->b_l2hdr.b_arcs_state = new_state->arcs_state; | |
2576 | l2arc_hdr_arcstats_increment_state(hdr); | |
2577 | } | |
2578 | } | |
34dc7c2f BB |
2579 | } |
2580 | ||
2581 | void | |
d164b209 | 2582 | arc_space_consume(uint64_t space, arc_space_type_t type) |
34dc7c2f | 2583 | { |
d164b209 BB |
2584 | ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); |
2585 | ||
2586 | switch (type) { | |
e75c13c3 BB |
2587 | default: |
2588 | break; | |
d164b209 | 2589 | case ARC_SPACE_DATA: |
c4c162c1 | 2590 | ARCSTAT_INCR(arcstat_data_size, space); |
d164b209 | 2591 | break; |
cc7f677c | 2592 | case ARC_SPACE_META: |
c4c162c1 | 2593 | ARCSTAT_INCR(arcstat_metadata_size, space); |
cc7f677c | 2594 | break; |
25458cbe | 2595 | case ARC_SPACE_BONUS: |
c4c162c1 | 2596 | ARCSTAT_INCR(arcstat_bonus_size, space); |
25458cbe TC |
2597 | break; |
2598 | case ARC_SPACE_DNODE: | |
a8d83e2a | 2599 | ARCSTAT_INCR(arcstat_dnode_size, space); |
25458cbe TC |
2600 | break; |
2601 | case ARC_SPACE_DBUF: | |
c4c162c1 | 2602 | ARCSTAT_INCR(arcstat_dbuf_size, space); |
d164b209 BB |
2603 | break; |
2604 | case ARC_SPACE_HDRS: | |
c4c162c1 | 2605 | ARCSTAT_INCR(arcstat_hdr_size, space); |
d164b209 BB |
2606 | break; |
2607 | case ARC_SPACE_L2HDRS: | |
c4c162c1 | 2608 | aggsum_add(&arc_sums.arcstat_l2_hdr_size, space); |
d164b209 | 2609 | break; |
85ec5cba MA |
2610 | case ARC_SPACE_ABD_CHUNK_WASTE: |
2611 | /* | |
2612 | * Note: this includes space wasted by all scatter ABD's, not | |
2613 | * just those allocated by the ARC. But the vast majority of | |
2614 | * scatter ABD's come from the ARC, because other users are | |
2615 | * very short-lived. | |
2616 | */ | |
c4c162c1 | 2617 | ARCSTAT_INCR(arcstat_abd_chunk_waste_size, space); |
85ec5cba | 2618 | break; |
d164b209 BB |
2619 | } |
2620 | ||
85ec5cba | 2621 | if (type != ARC_SPACE_DATA && type != ARC_SPACE_ABD_CHUNK_WASTE) |
a8d83e2a | 2622 | ARCSTAT_INCR(arcstat_meta_used, space); |
cc7f677c | 2623 | |
c4c162c1 | 2624 | aggsum_add(&arc_sums.arcstat_size, space); |
34dc7c2f BB |
2625 | } |
2626 | ||
2627 | void | |
d164b209 | 2628 | arc_space_return(uint64_t space, arc_space_type_t type) |
34dc7c2f | 2629 | { |
d164b209 BB |
2630 | ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); |
2631 | ||
2632 | switch (type) { | |
e75c13c3 BB |
2633 | default: |
2634 | break; | |
d164b209 | 2635 | case ARC_SPACE_DATA: |
c4c162c1 | 2636 | ARCSTAT_INCR(arcstat_data_size, -space); |
d164b209 | 2637 | break; |
cc7f677c | 2638 | case ARC_SPACE_META: |
c4c162c1 | 2639 | ARCSTAT_INCR(arcstat_metadata_size, -space); |
cc7f677c | 2640 | break; |
25458cbe | 2641 | case ARC_SPACE_BONUS: |
c4c162c1 | 2642 | ARCSTAT_INCR(arcstat_bonus_size, -space); |
25458cbe TC |
2643 | break; |
2644 | case ARC_SPACE_DNODE: | |
a8d83e2a | 2645 | ARCSTAT_INCR(arcstat_dnode_size, -space); |
25458cbe TC |
2646 | break; |
2647 | case ARC_SPACE_DBUF: | |
c4c162c1 | 2648 | ARCSTAT_INCR(arcstat_dbuf_size, -space); |
d164b209 BB |
2649 | break; |
2650 | case ARC_SPACE_HDRS: | |
c4c162c1 | 2651 | ARCSTAT_INCR(arcstat_hdr_size, -space); |
d164b209 BB |
2652 | break; |
2653 | case ARC_SPACE_L2HDRS: | |
c4c162c1 | 2654 | aggsum_add(&arc_sums.arcstat_l2_hdr_size, -space); |
d164b209 | 2655 | break; |
85ec5cba | 2656 | case ARC_SPACE_ABD_CHUNK_WASTE: |
c4c162c1 | 2657 | ARCSTAT_INCR(arcstat_abd_chunk_waste_size, -space); |
85ec5cba | 2658 | break; |
d164b209 BB |
2659 | } |
2660 | ||
a8d83e2a AM |
2661 | if (type != ARC_SPACE_DATA && type != ARC_SPACE_ABD_CHUNK_WASTE) |
2662 | ARCSTAT_INCR(arcstat_meta_used, -space); | |
cc7f677c | 2663 | |
c4c162c1 AM |
2664 | ASSERT(aggsum_compare(&arc_sums.arcstat_size, space) >= 0); |
2665 | aggsum_add(&arc_sums.arcstat_size, -space); | |
34dc7c2f BB |
2666 | } |
2667 | ||
d3c2ae1c | 2668 | /* |
524b4217 | 2669 | * Given a hdr and a buf, returns whether that buf can share its b_data buffer |
a6255b7f | 2670 | * with the hdr's b_pabd. |
d3c2ae1c | 2671 | */ |
524b4217 DK |
2672 | static boolean_t |
2673 | arc_can_share(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
2674 | { | |
524b4217 DK |
2675 | /* |
2676 | * The criteria for sharing a hdr's data are: | |
b5256303 TC |
2677 | * 1. the buffer is not encrypted |
2678 | * 2. the hdr's compression matches the buf's compression | |
2679 | * 3. the hdr doesn't need to be byteswapped | |
2680 | * 4. the hdr isn't already being shared | |
2681 | * 5. the buf is either compressed or it is the last buf in the hdr list | |
524b4217 | 2682 | * |
b5256303 | 2683 | * Criterion #5 maintains the invariant that shared uncompressed |
524b4217 DK |
2684 | * bufs must be the final buf in the hdr's b_buf list. Reading this, you |
2685 | * might ask, "if a compressed buf is allocated first, won't that be the | |
2686 | * last thing in the list?", but in that case it's impossible to create | |
2687 | * a shared uncompressed buf anyway (because the hdr must be compressed | |
2688 | * to have the compressed buf). You might also think that #3 is | |
2689 | * sufficient to make this guarantee, however it's possible | |
2690 | * (specifically in the rare L2ARC write race mentioned in | |
2691 | * arc_buf_alloc_impl()) there will be an existing uncompressed buf that | |
e1cfd73f | 2692 | * is shareable, but wasn't at the time of its allocation. Rather than |
524b4217 DK |
2693 | * allow a new shared uncompressed buf to be created and then shuffle |
2694 | * the list around to make it the last element, this simply disallows | |
2695 | * sharing if the new buf isn't the first to be added. | |
2696 | */ | |
2697 | ASSERT3P(buf->b_hdr, ==, hdr); | |
b5256303 TC |
2698 | boolean_t hdr_compressed = |
2699 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF; | |
a7004725 | 2700 | boolean_t buf_compressed = ARC_BUF_COMPRESSED(buf) != 0; |
b5256303 TC |
2701 | return (!ARC_BUF_ENCRYPTED(buf) && |
2702 | buf_compressed == hdr_compressed && | |
524b4217 DK |
2703 | hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS && |
2704 | !HDR_SHARED_DATA(hdr) && | |
2705 | (ARC_BUF_LAST(buf) || ARC_BUF_COMPRESSED(buf))); | |
2706 | } | |
2707 | ||
2708 | /* | |
2709 | * Allocate a buf for this hdr. If you care about the data that's in the hdr, | |
2710 | * or if you want a compressed buffer, pass those flags in. Returns 0 if the | |
2711 | * copy was made successfully, or an error code otherwise. | |
2712 | */ | |
2713 | static int | |
be9a5c35 | 2714 | arc_buf_alloc_impl(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb, |
a926aab9 AZ |
2715 | const void *tag, boolean_t encrypted, boolean_t compressed, |
2716 | boolean_t noauth, boolean_t fill, arc_buf_t **ret) | |
34dc7c2f | 2717 | { |
34dc7c2f | 2718 | arc_buf_t *buf; |
b5256303 | 2719 | arc_fill_flags_t flags = ARC_FILL_LOCKED; |
34dc7c2f | 2720 | |
d3c2ae1c GW |
2721 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2722 | ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); | |
2723 | VERIFY(hdr->b_type == ARC_BUFC_DATA || | |
2724 | hdr->b_type == ARC_BUFC_METADATA); | |
524b4217 DK |
2725 | ASSERT3P(ret, !=, NULL); |
2726 | ASSERT3P(*ret, ==, NULL); | |
b5256303 | 2727 | IMPLY(encrypted, compressed); |
d3c2ae1c | 2728 | |
524b4217 | 2729 | buf = *ret = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); |
34dc7c2f BB |
2730 | buf->b_hdr = hdr; |
2731 | buf->b_data = NULL; | |
2aa34383 | 2732 | buf->b_next = hdr->b_l1hdr.b_buf; |
524b4217 | 2733 | buf->b_flags = 0; |
b9541d6b | 2734 | |
d3c2ae1c GW |
2735 | add_reference(hdr, tag); |
2736 | ||
2737 | /* | |
2738 | * We're about to change the hdr's b_flags. We must either | |
2739 | * hold the hash_lock or be undiscoverable. | |
2740 | */ | |
ca6c7a94 | 2741 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
2742 | |
2743 | /* | |
524b4217 | 2744 | * Only honor requests for compressed bufs if the hdr is actually |
e1cfd73f | 2745 | * compressed. This must be overridden if the buffer is encrypted since |
b5256303 | 2746 | * encrypted buffers cannot be decompressed. |
524b4217 | 2747 | */ |
b5256303 TC |
2748 | if (encrypted) { |
2749 | buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; | |
2750 | buf->b_flags |= ARC_BUF_FLAG_ENCRYPTED; | |
2751 | flags |= ARC_FILL_COMPRESSED | ARC_FILL_ENCRYPTED; | |
2752 | } else if (compressed && | |
2753 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) { | |
524b4217 | 2754 | buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; |
b5256303 TC |
2755 | flags |= ARC_FILL_COMPRESSED; |
2756 | } | |
2757 | ||
2758 | if (noauth) { | |
2759 | ASSERT0(encrypted); | |
2760 | flags |= ARC_FILL_NOAUTH; | |
2761 | } | |
524b4217 | 2762 | |
524b4217 DK |
2763 | /* |
2764 | * If the hdr's data can be shared then we share the data buffer and | |
2765 | * set the appropriate bit in the hdr's b_flags to indicate the hdr is | |
5662fd57 MA |
2766 | * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new |
2767 | * buffer to store the buf's data. | |
524b4217 | 2768 | * |
a6255b7f DQ |
2769 | * There are two additional restrictions here because we're sharing |
2770 | * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be | |
2771 | * actively involved in an L2ARC write, because if this buf is used by | |
2772 | * an arc_write() then the hdr's data buffer will be released when the | |
524b4217 | 2773 | * write completes, even though the L2ARC write might still be using it. |
a6255b7f | 2774 | * Second, the hdr's ABD must be linear so that the buf's user doesn't |
5662fd57 MA |
2775 | * need to be ABD-aware. It must be allocated via |
2776 | * zio_[data_]buf_alloc(), not as a page, because we need to be able | |
2777 | * to abd_release_ownership_of_buf(), which isn't allowed on "linear | |
2778 | * page" buffers because the ABD code needs to handle freeing them | |
2779 | * specially. | |
2780 | */ | |
2781 | boolean_t can_share = arc_can_share(hdr, buf) && | |
2782 | !HDR_L2_WRITING(hdr) && | |
2783 | hdr->b_l1hdr.b_pabd != NULL && | |
2784 | abd_is_linear(hdr->b_l1hdr.b_pabd) && | |
2785 | !abd_is_linear_page(hdr->b_l1hdr.b_pabd); | |
524b4217 DK |
2786 | |
2787 | /* Set up b_data and sharing */ | |
2788 | if (can_share) { | |
a6255b7f | 2789 | buf->b_data = abd_to_buf(hdr->b_l1hdr.b_pabd); |
524b4217 | 2790 | buf->b_flags |= ARC_BUF_FLAG_SHARED; |
d3c2ae1c GW |
2791 | arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); |
2792 | } else { | |
524b4217 DK |
2793 | buf->b_data = |
2794 | arc_get_data_buf(hdr, arc_buf_size(buf), buf); | |
2795 | ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); | |
d3c2ae1c GW |
2796 | } |
2797 | VERIFY3P(buf->b_data, !=, NULL); | |
b9541d6b CW |
2798 | |
2799 | hdr->b_l1hdr.b_buf = buf; | |
b9541d6b | 2800 | |
524b4217 DK |
2801 | /* |
2802 | * If the user wants the data from the hdr, we need to either copy or | |
2803 | * decompress the data. | |
2804 | */ | |
2805 | if (fill) { | |
be9a5c35 TC |
2806 | ASSERT3P(zb, !=, NULL); |
2807 | return (arc_buf_fill(buf, spa, zb, flags)); | |
524b4217 | 2808 | } |
d3c2ae1c | 2809 | |
524b4217 | 2810 | return (0); |
34dc7c2f BB |
2811 | } |
2812 | ||
a926aab9 | 2813 | static const char *arc_onloan_tag = "onloan"; |
9babb374 | 2814 | |
a7004725 DK |
2815 | static inline void |
2816 | arc_loaned_bytes_update(int64_t delta) | |
2817 | { | |
2818 | atomic_add_64(&arc_loaned_bytes, delta); | |
2819 | ||
2820 | /* assert that it did not wrap around */ | |
2821 | ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); | |
2822 | } | |
2823 | ||
9babb374 BB |
2824 | /* |
2825 | * Loan out an anonymous arc buffer. Loaned buffers are not counted as in | |
2826 | * flight data by arc_tempreserve_space() until they are "returned". Loaned | |
2827 | * buffers must be returned to the arc before they can be used by the DMU or | |
2828 | * freed. | |
2829 | */ | |
2830 | arc_buf_t * | |
2aa34383 | 2831 | arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size) |
9babb374 | 2832 | { |
2aa34383 DK |
2833 | arc_buf_t *buf = arc_alloc_buf(spa, arc_onloan_tag, |
2834 | is_metadata ? ARC_BUFC_METADATA : ARC_BUFC_DATA, size); | |
9babb374 | 2835 | |
5152a740 | 2836 | arc_loaned_bytes_update(arc_buf_size(buf)); |
a7004725 | 2837 | |
9babb374 BB |
2838 | return (buf); |
2839 | } | |
2840 | ||
2aa34383 DK |
2841 | arc_buf_t * |
2842 | arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize, | |
10b3c7f5 | 2843 | enum zio_compress compression_type, uint8_t complevel) |
2aa34383 DK |
2844 | { |
2845 | arc_buf_t *buf = arc_alloc_compressed_buf(spa, arc_onloan_tag, | |
10b3c7f5 | 2846 | psize, lsize, compression_type, complevel); |
2aa34383 | 2847 | |
5152a740 | 2848 | arc_loaned_bytes_update(arc_buf_size(buf)); |
a7004725 | 2849 | |
2aa34383 DK |
2850 | return (buf); |
2851 | } | |
2852 | ||
b5256303 TC |
2853 | arc_buf_t * |
2854 | arc_loan_raw_buf(spa_t *spa, uint64_t dsobj, boolean_t byteorder, | |
2855 | const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, | |
2856 | dmu_object_type_t ot, uint64_t psize, uint64_t lsize, | |
10b3c7f5 | 2857 | enum zio_compress compression_type, uint8_t complevel) |
b5256303 TC |
2858 | { |
2859 | arc_buf_t *buf = arc_alloc_raw_buf(spa, arc_onloan_tag, dsobj, | |
10b3c7f5 MN |
2860 | byteorder, salt, iv, mac, ot, psize, lsize, compression_type, |
2861 | complevel); | |
b5256303 TC |
2862 | |
2863 | atomic_add_64(&arc_loaned_bytes, psize); | |
2864 | return (buf); | |
2865 | } | |
2866 | ||
2aa34383 | 2867 | |
9babb374 BB |
2868 | /* |
2869 | * Return a loaned arc buffer to the arc. | |
2870 | */ | |
2871 | void | |
dd66857d | 2872 | arc_return_buf(arc_buf_t *buf, const void *tag) |
9babb374 BB |
2873 | { |
2874 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
2875 | ||
d3c2ae1c | 2876 | ASSERT3P(buf->b_data, !=, NULL); |
b9541d6b | 2877 | ASSERT(HDR_HAS_L1HDR(hdr)); |
c13060e4 | 2878 | (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag); |
424fd7c3 | 2879 | (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); |
9babb374 | 2880 | |
a7004725 | 2881 | arc_loaned_bytes_update(-arc_buf_size(buf)); |
9babb374 BB |
2882 | } |
2883 | ||
428870ff BB |
2884 | /* Detach an arc_buf from a dbuf (tag) */ |
2885 | void | |
dd66857d | 2886 | arc_loan_inuse_buf(arc_buf_t *buf, const void *tag) |
428870ff | 2887 | { |
b9541d6b | 2888 | arc_buf_hdr_t *hdr = buf->b_hdr; |
428870ff | 2889 | |
d3c2ae1c | 2890 | ASSERT3P(buf->b_data, !=, NULL); |
b9541d6b | 2891 | ASSERT(HDR_HAS_L1HDR(hdr)); |
c13060e4 | 2892 | (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); |
424fd7c3 | 2893 | (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); |
428870ff | 2894 | |
a7004725 | 2895 | arc_loaned_bytes_update(arc_buf_size(buf)); |
428870ff BB |
2896 | } |
2897 | ||
d3c2ae1c | 2898 | static void |
a6255b7f | 2899 | l2arc_free_abd_on_write(abd_t *abd, size_t size, arc_buf_contents_t type) |
34dc7c2f | 2900 | { |
d3c2ae1c | 2901 | l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP); |
34dc7c2f | 2902 | |
a6255b7f | 2903 | df->l2df_abd = abd; |
d3c2ae1c GW |
2904 | df->l2df_size = size; |
2905 | df->l2df_type = type; | |
2906 | mutex_enter(&l2arc_free_on_write_mtx); | |
2907 | list_insert_head(l2arc_free_on_write, df); | |
2908 | mutex_exit(&l2arc_free_on_write_mtx); | |
2909 | } | |
428870ff | 2910 | |
d3c2ae1c | 2911 | static void |
b5256303 | 2912 | arc_hdr_free_on_write(arc_buf_hdr_t *hdr, boolean_t free_rdata) |
d3c2ae1c GW |
2913 | { |
2914 | arc_state_t *state = hdr->b_l1hdr.b_state; | |
2915 | arc_buf_contents_t type = arc_buf_type(hdr); | |
b5256303 | 2916 | uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr); |
1eb5bfa3 | 2917 | |
d3c2ae1c GW |
2918 | /* protected by hash lock, if in the hash table */ |
2919 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 | 2920 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
2921 | ASSERT(state != arc_anon && state != arc_l2c_only); |
2922 | ||
424fd7c3 | 2923 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c | 2924 | size, hdr); |
1eb5bfa3 | 2925 | } |
a8d83e2a | 2926 | (void) zfs_refcount_remove_many(&state->arcs_size[type], size, hdr); |
423e7b62 AG |
2927 | if (type == ARC_BUFC_METADATA) { |
2928 | arc_space_return(size, ARC_SPACE_META); | |
2929 | } else { | |
2930 | ASSERT(type == ARC_BUFC_DATA); | |
2931 | arc_space_return(size, ARC_SPACE_DATA); | |
2932 | } | |
d3c2ae1c | 2933 | |
b5256303 TC |
2934 | if (free_rdata) { |
2935 | l2arc_free_abd_on_write(hdr->b_crypt_hdr.b_rabd, size, type); | |
2936 | } else { | |
2937 | l2arc_free_abd_on_write(hdr->b_l1hdr.b_pabd, size, type); | |
2938 | } | |
34dc7c2f BB |
2939 | } |
2940 | ||
d3c2ae1c GW |
2941 | /* |
2942 | * Share the arc_buf_t's data with the hdr. Whenever we are sharing the | |
2943 | * data buffer, we transfer the refcount ownership to the hdr and update | |
2944 | * the appropriate kstats. | |
2945 | */ | |
2946 | static void | |
2947 | arc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
34dc7c2f | 2948 | { |
524b4217 | 2949 | ASSERT(arc_can_share(hdr, buf)); |
a6255b7f | 2950 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2951 | ASSERT(!ARC_BUF_ENCRYPTED(buf)); |
ca6c7a94 | 2952 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
34dc7c2f BB |
2953 | |
2954 | /* | |
d3c2ae1c GW |
2955 | * Start sharing the data buffer. We transfer the |
2956 | * refcount ownership to the hdr since it always owns | |
2957 | * the refcount whenever an arc_buf_t is shared. | |
34dc7c2f | 2958 | */ |
a8d83e2a AM |
2959 | zfs_refcount_transfer_ownership_many( |
2960 | &hdr->b_l1hdr.b_state->arcs_size[arc_buf_type(hdr)], | |
d7e4b30a | 2961 | arc_hdr_size(hdr), buf, hdr); |
a6255b7f DQ |
2962 | hdr->b_l1hdr.b_pabd = abd_get_from_buf(buf->b_data, arc_buf_size(buf)); |
2963 | abd_take_ownership_of_buf(hdr->b_l1hdr.b_pabd, | |
2964 | HDR_ISTYPE_METADATA(hdr)); | |
d3c2ae1c | 2965 | arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); |
524b4217 | 2966 | buf->b_flags |= ARC_BUF_FLAG_SHARED; |
34dc7c2f | 2967 | |
d3c2ae1c GW |
2968 | /* |
2969 | * Since we've transferred ownership to the hdr we need | |
2970 | * to increment its compressed and uncompressed kstats and | |
2971 | * decrement the overhead size. | |
2972 | */ | |
2973 | ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); | |
2974 | ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); | |
2aa34383 | 2975 | ARCSTAT_INCR(arcstat_overhead_size, -arc_buf_size(buf)); |
34dc7c2f BB |
2976 | } |
2977 | ||
ca0bf58d | 2978 | static void |
d3c2ae1c | 2979 | arc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) |
ca0bf58d | 2980 | { |
d3c2ae1c | 2981 | ASSERT(arc_buf_is_shared(buf)); |
a6255b7f | 2982 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
ca6c7a94 | 2983 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
ca0bf58d | 2984 | |
d3c2ae1c GW |
2985 | /* |
2986 | * We are no longer sharing this buffer so we need | |
2987 | * to transfer its ownership to the rightful owner. | |
2988 | */ | |
a8d83e2a AM |
2989 | zfs_refcount_transfer_ownership_many( |
2990 | &hdr->b_l1hdr.b_state->arcs_size[arc_buf_type(hdr)], | |
d7e4b30a | 2991 | arc_hdr_size(hdr), hdr, buf); |
d3c2ae1c | 2992 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); |
a6255b7f | 2993 | abd_release_ownership_of_buf(hdr->b_l1hdr.b_pabd); |
e2af2acc | 2994 | abd_free(hdr->b_l1hdr.b_pabd); |
a6255b7f | 2995 | hdr->b_l1hdr.b_pabd = NULL; |
524b4217 | 2996 | buf->b_flags &= ~ARC_BUF_FLAG_SHARED; |
d3c2ae1c GW |
2997 | |
2998 | /* | |
2999 | * Since the buffer is no longer shared between | |
3000 | * the arc buf and the hdr, count it as overhead. | |
3001 | */ | |
3002 | ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); | |
3003 | ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); | |
2aa34383 | 3004 | ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); |
ca0bf58d PS |
3005 | } |
3006 | ||
34dc7c2f | 3007 | /* |
2aa34383 DK |
3008 | * Remove an arc_buf_t from the hdr's buf list and return the last |
3009 | * arc_buf_t on the list. If no buffers remain on the list then return | |
3010 | * NULL. | |
3011 | */ | |
3012 | static arc_buf_t * | |
3013 | arc_buf_remove(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
3014 | { | |
2aa34383 | 3015 | ASSERT(HDR_HAS_L1HDR(hdr)); |
ca6c7a94 | 3016 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
2aa34383 | 3017 | |
a7004725 DK |
3018 | arc_buf_t **bufp = &hdr->b_l1hdr.b_buf; |
3019 | arc_buf_t *lastbuf = NULL; | |
3020 | ||
2aa34383 DK |
3021 | /* |
3022 | * Remove the buf from the hdr list and locate the last | |
3023 | * remaining buffer on the list. | |
3024 | */ | |
3025 | while (*bufp != NULL) { | |
3026 | if (*bufp == buf) | |
3027 | *bufp = buf->b_next; | |
3028 | ||
3029 | /* | |
3030 | * If we've removed a buffer in the middle of | |
3031 | * the list then update the lastbuf and update | |
3032 | * bufp. | |
3033 | */ | |
3034 | if (*bufp != NULL) { | |
3035 | lastbuf = *bufp; | |
3036 | bufp = &(*bufp)->b_next; | |
3037 | } | |
3038 | } | |
3039 | buf->b_next = NULL; | |
3040 | ASSERT3P(lastbuf, !=, buf); | |
2aa34383 DK |
3041 | IMPLY(lastbuf != NULL, ARC_BUF_LAST(lastbuf)); |
3042 | ||
3043 | return (lastbuf); | |
3044 | } | |
3045 | ||
3046 | /* | |
e1cfd73f | 3047 | * Free up buf->b_data and pull the arc_buf_t off of the arc_buf_hdr_t's |
2aa34383 | 3048 | * list and free it. |
34dc7c2f BB |
3049 | */ |
3050 | static void | |
2aa34383 | 3051 | arc_buf_destroy_impl(arc_buf_t *buf) |
34dc7c2f | 3052 | { |
498877ba | 3053 | arc_buf_hdr_t *hdr = buf->b_hdr; |
ca0bf58d PS |
3054 | |
3055 | /* | |
524b4217 DK |
3056 | * Free up the data associated with the buf but only if we're not |
3057 | * sharing this with the hdr. If we are sharing it with the hdr, the | |
3058 | * hdr is responsible for doing the free. | |
ca0bf58d | 3059 | */ |
d3c2ae1c GW |
3060 | if (buf->b_data != NULL) { |
3061 | /* | |
3062 | * We're about to change the hdr's b_flags. We must either | |
3063 | * hold the hash_lock or be undiscoverable. | |
3064 | */ | |
ca6c7a94 | 3065 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c | 3066 | |
524b4217 | 3067 | arc_cksum_verify(buf); |
d3c2ae1c GW |
3068 | arc_buf_unwatch(buf); |
3069 | ||
57b40985 | 3070 | if (ARC_BUF_SHARED(buf)) { |
d3c2ae1c GW |
3071 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); |
3072 | } else { | |
57b40985 | 3073 | ASSERT(!arc_buf_is_shared(buf)); |
2aa34383 | 3074 | uint64_t size = arc_buf_size(buf); |
d3c2ae1c GW |
3075 | arc_free_data_buf(hdr, buf->b_data, size, buf); |
3076 | ARCSTAT_INCR(arcstat_overhead_size, -size); | |
3077 | } | |
3078 | buf->b_data = NULL; | |
3079 | ||
96b9cf42 AM |
3080 | /* |
3081 | * If we have no more encrypted buffers and we've already | |
3082 | * gotten a copy of the decrypted data we can free b_rabd | |
3083 | * to save some space. | |
3084 | */ | |
3085 | if (ARC_BUF_ENCRYPTED(buf) && HDR_HAS_RABD(hdr) && | |
3086 | hdr->b_l1hdr.b_pabd != NULL && !HDR_IO_IN_PROGRESS(hdr)) { | |
3087 | arc_buf_t *b; | |
3088 | for (b = hdr->b_l1hdr.b_buf; b; b = b->b_next) { | |
3089 | if (b != buf && ARC_BUF_ENCRYPTED(b)) | |
3090 | break; | |
da5d4697 | 3091 | } |
96b9cf42 AM |
3092 | if (b == NULL) |
3093 | arc_hdr_free_abd(hdr, B_TRUE); | |
440a3eb9 | 3094 | } |
d3c2ae1c GW |
3095 | } |
3096 | ||
a7004725 | 3097 | arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); |
d3c2ae1c | 3098 | |
524b4217 | 3099 | if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) { |
2aa34383 | 3100 | /* |
524b4217 | 3101 | * If the current arc_buf_t is sharing its data buffer with the |
a6255b7f | 3102 | * hdr, then reassign the hdr's b_pabd to share it with the new |
524b4217 DK |
3103 | * buffer at the end of the list. The shared buffer is always |
3104 | * the last one on the hdr's buffer list. | |
3105 | * | |
3106 | * There is an equivalent case for compressed bufs, but since | |
3107 | * they aren't guaranteed to be the last buf in the list and | |
3108 | * that is an exceedingly rare case, we just allow that space be | |
b5256303 TC |
3109 | * wasted temporarily. We must also be careful not to share |
3110 | * encrypted buffers, since they cannot be shared. | |
2aa34383 | 3111 | */ |
b5256303 | 3112 | if (lastbuf != NULL && !ARC_BUF_ENCRYPTED(lastbuf)) { |
524b4217 | 3113 | /* Only one buf can be shared at once */ |
57b40985 | 3114 | ASSERT(!arc_buf_is_shared(lastbuf)); |
524b4217 | 3115 | /* hdr is uncompressed so can't have compressed buf */ |
57b40985 | 3116 | ASSERT(!ARC_BUF_COMPRESSED(lastbuf)); |
d3c2ae1c | 3117 | |
a6255b7f | 3118 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
b5256303 | 3119 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c | 3120 | |
2aa34383 DK |
3121 | /* |
3122 | * We must setup a new shared block between the | |
3123 | * last buffer and the hdr. The data would have | |
3124 | * been allocated by the arc buf so we need to transfer | |
3125 | * ownership to the hdr since it's now being shared. | |
3126 | */ | |
3127 | arc_share_buf(hdr, lastbuf); | |
3128 | } | |
3129 | } else if (HDR_SHARED_DATA(hdr)) { | |
d3c2ae1c | 3130 | /* |
2aa34383 DK |
3131 | * Uncompressed shared buffers are always at the end |
3132 | * of the list. Compressed buffers don't have the | |
3133 | * same requirements. This makes it hard to | |
3134 | * simply assert that the lastbuf is shared so | |
3135 | * we rely on the hdr's compression flags to determine | |
3136 | * if we have a compressed, shared buffer. | |
d3c2ae1c | 3137 | */ |
2aa34383 DK |
3138 | ASSERT3P(lastbuf, !=, NULL); |
3139 | ASSERT(arc_buf_is_shared(lastbuf) || | |
b5256303 | 3140 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); |
ca0bf58d PS |
3141 | } |
3142 | ||
a7004725 DK |
3143 | /* |
3144 | * Free the checksum if we're removing the last uncompressed buf from | |
3145 | * this hdr. | |
3146 | */ | |
3147 | if (!arc_hdr_has_uncompressed_buf(hdr)) { | |
d3c2ae1c | 3148 | arc_cksum_free(hdr); |
a7004725 | 3149 | } |
d3c2ae1c GW |
3150 | |
3151 | /* clean up the buf */ | |
3152 | buf->b_hdr = NULL; | |
3153 | kmem_cache_free(buf_cache, buf); | |
3154 | } | |
3155 | ||
3156 | static void | |
e111c802 | 3157 | arc_hdr_alloc_abd(arc_buf_hdr_t *hdr, int alloc_flags) |
d3c2ae1c | 3158 | { |
b5256303 | 3159 | uint64_t size; |
e111c802 | 3160 | boolean_t alloc_rdata = ((alloc_flags & ARC_HDR_ALLOC_RDATA) != 0); |
b5256303 | 3161 | |
d3c2ae1c GW |
3162 | ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); |
3163 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
b5256303 TC |
3164 | ASSERT(!HDR_SHARED_DATA(hdr) || alloc_rdata); |
3165 | IMPLY(alloc_rdata, HDR_PROTECTED(hdr)); | |
d3c2ae1c | 3166 | |
b5256303 TC |
3167 | if (alloc_rdata) { |
3168 | size = HDR_GET_PSIZE(hdr); | |
3169 | ASSERT3P(hdr->b_crypt_hdr.b_rabd, ==, NULL); | |
e111c802 | 3170 | hdr->b_crypt_hdr.b_rabd = arc_get_data_abd(hdr, size, hdr, |
6b88b4b5 | 3171 | alloc_flags); |
b5256303 TC |
3172 | ASSERT3P(hdr->b_crypt_hdr.b_rabd, !=, NULL); |
3173 | ARCSTAT_INCR(arcstat_raw_size, size); | |
3174 | } else { | |
3175 | size = arc_hdr_size(hdr); | |
3176 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); | |
e111c802 | 3177 | hdr->b_l1hdr.b_pabd = arc_get_data_abd(hdr, size, hdr, |
6b88b4b5 | 3178 | alloc_flags); |
b5256303 TC |
3179 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
3180 | } | |
3181 | ||
3182 | ARCSTAT_INCR(arcstat_compressed_size, size); | |
d3c2ae1c GW |
3183 | ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); |
3184 | } | |
3185 | ||
3186 | static void | |
b5256303 | 3187 | arc_hdr_free_abd(arc_buf_hdr_t *hdr, boolean_t free_rdata) |
d3c2ae1c | 3188 | { |
b5256303 TC |
3189 | uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr); |
3190 | ||
d3c2ae1c | 3191 | ASSERT(HDR_HAS_L1HDR(hdr)); |
b5256303 TC |
3192 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); |
3193 | IMPLY(free_rdata, HDR_HAS_RABD(hdr)); | |
d3c2ae1c | 3194 | |
ca0bf58d | 3195 | /* |
d3c2ae1c GW |
3196 | * If the hdr is currently being written to the l2arc then |
3197 | * we defer freeing the data by adding it to the l2arc_free_on_write | |
3198 | * list. The l2arc will free the data once it's finished | |
3199 | * writing it to the l2arc device. | |
ca0bf58d | 3200 | */ |
d3c2ae1c | 3201 | if (HDR_L2_WRITING(hdr)) { |
b5256303 | 3202 | arc_hdr_free_on_write(hdr, free_rdata); |
d3c2ae1c | 3203 | ARCSTAT_BUMP(arcstat_l2_free_on_write); |
b5256303 TC |
3204 | } else if (free_rdata) { |
3205 | arc_free_data_abd(hdr, hdr->b_crypt_hdr.b_rabd, size, hdr); | |
d3c2ae1c | 3206 | } else { |
b5256303 | 3207 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, size, hdr); |
ca0bf58d PS |
3208 | } |
3209 | ||
b5256303 TC |
3210 | if (free_rdata) { |
3211 | hdr->b_crypt_hdr.b_rabd = NULL; | |
3212 | ARCSTAT_INCR(arcstat_raw_size, -size); | |
3213 | } else { | |
3214 | hdr->b_l1hdr.b_pabd = NULL; | |
3215 | } | |
3216 | ||
3217 | if (hdr->b_l1hdr.b_pabd == NULL && !HDR_HAS_RABD(hdr)) | |
3218 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
3219 | ||
3220 | ARCSTAT_INCR(arcstat_compressed_size, -size); | |
d3c2ae1c GW |
3221 | ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); |
3222 | } | |
3223 | ||
6b88b4b5 AM |
3224 | /* |
3225 | * Allocate empty anonymous ARC header. The header will get its identity | |
3226 | * assigned and buffers attached later as part of read or write operations. | |
3227 | * | |
3228 | * In case of read arc_read() assigns header its identify (b_dva + b_birth), | |
3229 | * inserts it into ARC hash to become globally visible and allocates physical | |
3230 | * (b_pabd) or raw (b_rabd) ABD buffer to read into from disk. On disk read | |
3231 | * completion arc_read_done() allocates ARC buffer(s) as needed, potentially | |
3232 | * sharing one of them with the physical ABD buffer. | |
3233 | * | |
3234 | * In case of write arc_alloc_buf() allocates ARC buffer to be filled with | |
3235 | * data. Then after compression and/or encryption arc_write_ready() allocates | |
3236 | * and fills (or potentially shares) physical (b_pabd) or raw (b_rabd) ABD | |
3237 | * buffer. On disk write completion arc_write_done() assigns the header its | |
3238 | * new identity (b_dva + b_birth) and inserts into ARC hash. | |
3239 | * | |
3240 | * In case of partial overwrite the old data is read first as described. Then | |
3241 | * arc_release() either allocates new anonymous ARC header and moves the ARC | |
3242 | * buffer to it, or reuses the old ARC header by discarding its identity and | |
3243 | * removing it from ARC hash. After buffer modification normal write process | |
3244 | * follows as described. | |
3245 | */ | |
d3c2ae1c GW |
3246 | static arc_buf_hdr_t * |
3247 | arc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize, | |
10b3c7f5 | 3248 | boolean_t protected, enum zio_compress compression_type, uint8_t complevel, |
6b88b4b5 | 3249 | arc_buf_contents_t type) |
d3c2ae1c GW |
3250 | { |
3251 | arc_buf_hdr_t *hdr; | |
3252 | ||
d3c2ae1c | 3253 | VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA); |
75a2eb7f | 3254 | hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); |
d3c2ae1c | 3255 | |
d3c2ae1c | 3256 | ASSERT(HDR_EMPTY(hdr)); |
bacf366f | 3257 | #ifdef ZFS_DEBUG |
d3c2ae1c | 3258 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); |
bacf366f | 3259 | #endif |
d3c2ae1c GW |
3260 | HDR_SET_PSIZE(hdr, psize); |
3261 | HDR_SET_LSIZE(hdr, lsize); | |
3262 | hdr->b_spa = spa; | |
3263 | hdr->b_type = type; | |
3264 | hdr->b_flags = 0; | |
3265 | arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR); | |
2aa34383 | 3266 | arc_hdr_set_compress(hdr, compression_type); |
10b3c7f5 | 3267 | hdr->b_complevel = complevel; |
b5256303 TC |
3268 | if (protected) |
3269 | arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED); | |
ca0bf58d | 3270 | |
d3c2ae1c GW |
3271 | hdr->b_l1hdr.b_state = arc_anon; |
3272 | hdr->b_l1hdr.b_arc_access = 0; | |
cfe8e960 AM |
3273 | hdr->b_l1hdr.b_mru_hits = 0; |
3274 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
3275 | hdr->b_l1hdr.b_mfu_hits = 0; | |
3276 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
d3c2ae1c | 3277 | hdr->b_l1hdr.b_buf = NULL; |
ca0bf58d | 3278 | |
424fd7c3 | 3279 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
ca0bf58d | 3280 | |
d3c2ae1c | 3281 | return (hdr); |
ca0bf58d PS |
3282 | } |
3283 | ||
bd089c54 | 3284 | /* |
d3c2ae1c GW |
3285 | * Transition between the two allocation states for the arc_buf_hdr struct. |
3286 | * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without | |
3287 | * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller | |
3288 | * version is used when a cache buffer is only in the L2ARC in order to reduce | |
3289 | * memory usage. | |
bd089c54 | 3290 | */ |
d3c2ae1c GW |
3291 | static arc_buf_hdr_t * |
3292 | arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) | |
34dc7c2f | 3293 | { |
1c27024e DB |
3294 | ASSERT(HDR_HAS_L2HDR(hdr)); |
3295 | ||
d3c2ae1c GW |
3296 | arc_buf_hdr_t *nhdr; |
3297 | l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; | |
34dc7c2f | 3298 | |
d3c2ae1c GW |
3299 | ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || |
3300 | (old == hdr_l2only_cache && new == hdr_full_cache)); | |
34dc7c2f | 3301 | |
d3c2ae1c | 3302 | nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); |
428870ff | 3303 | |
d3c2ae1c GW |
3304 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); |
3305 | buf_hash_remove(hdr); | |
ca0bf58d | 3306 | |
861166b0 | 3307 | memcpy(nhdr, hdr, HDR_L2ONLY_SIZE); |
34dc7c2f | 3308 | |
75a2eb7f | 3309 | if (new == hdr_full_cache) { |
d3c2ae1c GW |
3310 | arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR); |
3311 | /* | |
3312 | * arc_access and arc_change_state need to be aware that a | |
3313 | * header has just come out of L2ARC, so we set its state to | |
3314 | * l2c_only even though it's about to change. | |
3315 | */ | |
3316 | nhdr->b_l1hdr.b_state = arc_l2c_only; | |
34dc7c2f | 3317 | |
d3c2ae1c | 3318 | /* Verify previous threads set to NULL before freeing */ |
a6255b7f | 3319 | ASSERT3P(nhdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 3320 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
3321 | } else { |
3322 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
bacf366f | 3323 | #ifdef ZFS_DEBUG |
d3c2ae1c | 3324 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); |
bacf366f | 3325 | #endif |
36da08ef | 3326 | |
d3c2ae1c GW |
3327 | /* |
3328 | * If we've reached here, We must have been called from | |
3329 | * arc_evict_hdr(), as such we should have already been | |
3330 | * removed from any ghost list we were previously on | |
3331 | * (which protects us from racing with arc_evict_state), | |
3332 | * thus no locking is needed during this check. | |
3333 | */ | |
3334 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
1eb5bfa3 GW |
3335 | |
3336 | /* | |
d3c2ae1c GW |
3337 | * A buffer must not be moved into the arc_l2c_only |
3338 | * state if it's not finished being written out to the | |
a6255b7f | 3339 | * l2arc device. Otherwise, the b_l1hdr.b_pabd field |
d3c2ae1c | 3340 | * might try to be accessed, even though it was removed. |
1eb5bfa3 | 3341 | */ |
d3c2ae1c | 3342 | VERIFY(!HDR_L2_WRITING(hdr)); |
a6255b7f | 3343 | VERIFY3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 3344 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
3345 | |
3346 | arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR); | |
34dc7c2f | 3347 | } |
d3c2ae1c GW |
3348 | /* |
3349 | * The header has been reallocated so we need to re-insert it into any | |
3350 | * lists it was on. | |
3351 | */ | |
3352 | (void) buf_hash_insert(nhdr, NULL); | |
34dc7c2f | 3353 | |
d3c2ae1c | 3354 | ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); |
34dc7c2f | 3355 | |
d3c2ae1c GW |
3356 | mutex_enter(&dev->l2ad_mtx); |
3357 | ||
3358 | /* | |
3359 | * We must place the realloc'ed header back into the list at | |
3360 | * the same spot. Otherwise, if it's placed earlier in the list, | |
3361 | * l2arc_write_buffers() could find it during the function's | |
3362 | * write phase, and try to write it out to the l2arc. | |
3363 | */ | |
3364 | list_insert_after(&dev->l2ad_buflist, hdr, nhdr); | |
3365 | list_remove(&dev->l2ad_buflist, hdr); | |
34dc7c2f | 3366 | |
d3c2ae1c | 3367 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 3368 | |
d3c2ae1c GW |
3369 | /* |
3370 | * Since we're using the pointer address as the tag when | |
3371 | * incrementing and decrementing the l2ad_alloc refcount, we | |
3372 | * must remove the old pointer (that we're about to destroy) and | |
3373 | * add the new pointer to the refcount. Otherwise we'd remove | |
3374 | * the wrong pointer address when calling arc_hdr_destroy() later. | |
3375 | */ | |
3376 | ||
424fd7c3 TS |
3377 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, |
3378 | arc_hdr_size(hdr), hdr); | |
3379 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, | |
3380 | arc_hdr_size(nhdr), nhdr); | |
d3c2ae1c GW |
3381 | |
3382 | buf_discard_identity(hdr); | |
3383 | kmem_cache_free(old, hdr); | |
3384 | ||
3385 | return (nhdr); | |
3386 | } | |
3387 | ||
b5256303 TC |
3388 | /* |
3389 | * This function is used by the send / receive code to convert a newly | |
3390 | * allocated arc_buf_t to one that is suitable for a raw encrypted write. It | |
e1cfd73f | 3391 | * is also used to allow the root objset block to be updated without altering |
b5256303 TC |
3392 | * its embedded MACs. Both block types will always be uncompressed so we do not |
3393 | * have to worry about compression type or psize. | |
3394 | */ | |
3395 | void | |
3396 | arc_convert_to_raw(arc_buf_t *buf, uint64_t dsobj, boolean_t byteorder, | |
3397 | dmu_object_type_t ot, const uint8_t *salt, const uint8_t *iv, | |
3398 | const uint8_t *mac) | |
3399 | { | |
3400 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
3401 | ||
3402 | ASSERT(ot == DMU_OT_DNODE || ot == DMU_OT_OBJSET); | |
3403 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
3404 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); | |
3405 | ||
3406 | buf->b_flags |= (ARC_BUF_FLAG_COMPRESSED | ARC_BUF_FLAG_ENCRYPTED); | |
75a2eb7f | 3407 | arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED); |
b5256303 TC |
3408 | hdr->b_crypt_hdr.b_dsobj = dsobj; |
3409 | hdr->b_crypt_hdr.b_ot = ot; | |
3410 | hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ? | |
3411 | DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot); | |
3412 | if (!arc_hdr_has_uncompressed_buf(hdr)) | |
3413 | arc_cksum_free(hdr); | |
3414 | ||
3415 | if (salt != NULL) | |
861166b0 | 3416 | memcpy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN); |
b5256303 | 3417 | if (iv != NULL) |
861166b0 | 3418 | memcpy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN); |
b5256303 | 3419 | if (mac != NULL) |
861166b0 | 3420 | memcpy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN); |
b5256303 TC |
3421 | } |
3422 | ||
d3c2ae1c GW |
3423 | /* |
3424 | * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller. | |
3425 | * The buf is returned thawed since we expect the consumer to modify it. | |
3426 | */ | |
3427 | arc_buf_t * | |
a926aab9 AZ |
3428 | arc_alloc_buf(spa_t *spa, const void *tag, arc_buf_contents_t type, |
3429 | int32_t size) | |
d3c2ae1c | 3430 | { |
d3c2ae1c | 3431 | arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size, |
6b88b4b5 | 3432 | B_FALSE, ZIO_COMPRESS_OFF, 0, type); |
2aa34383 | 3433 | |
a7004725 | 3434 | arc_buf_t *buf = NULL; |
be9a5c35 | 3435 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, B_FALSE, |
b5256303 | 3436 | B_FALSE, B_FALSE, &buf)); |
d3c2ae1c | 3437 | arc_buf_thaw(buf); |
2aa34383 DK |
3438 | |
3439 | return (buf); | |
3440 | } | |
3441 | ||
3442 | /* | |
3443 | * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this | |
3444 | * for bufs containing metadata. | |
3445 | */ | |
3446 | arc_buf_t * | |
a926aab9 AZ |
3447 | arc_alloc_compressed_buf(spa_t *spa, const void *tag, uint64_t psize, |
3448 | uint64_t lsize, enum zio_compress compression_type, uint8_t complevel) | |
2aa34383 | 3449 | { |
2aa34383 DK |
3450 | ASSERT3U(lsize, >, 0); |
3451 | ASSERT3U(lsize, >=, psize); | |
b5256303 TC |
3452 | ASSERT3U(compression_type, >, ZIO_COMPRESS_OFF); |
3453 | ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS); | |
2aa34383 | 3454 | |
a7004725 | 3455 | arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, |
6b88b4b5 | 3456 | B_FALSE, compression_type, complevel, ARC_BUFC_DATA); |
2aa34383 | 3457 | |
a7004725 | 3458 | arc_buf_t *buf = NULL; |
be9a5c35 | 3459 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, |
b5256303 | 3460 | B_TRUE, B_FALSE, B_FALSE, &buf)); |
2aa34383 | 3461 | arc_buf_thaw(buf); |
2aa34383 | 3462 | |
6b88b4b5 AM |
3463 | /* |
3464 | * To ensure that the hdr has the correct data in it if we call | |
3465 | * arc_untransform() on this buf before it's been written to disk, | |
3466 | * it's easiest if we just set up sharing between the buf and the hdr. | |
3467 | */ | |
3468 | arc_share_buf(hdr, buf); | |
a6255b7f | 3469 | |
d3c2ae1c | 3470 | return (buf); |
34dc7c2f BB |
3471 | } |
3472 | ||
b5256303 | 3473 | arc_buf_t * |
a926aab9 AZ |
3474 | arc_alloc_raw_buf(spa_t *spa, const void *tag, uint64_t dsobj, |
3475 | boolean_t byteorder, const uint8_t *salt, const uint8_t *iv, | |
3476 | const uint8_t *mac, dmu_object_type_t ot, uint64_t psize, uint64_t lsize, | |
10b3c7f5 | 3477 | enum zio_compress compression_type, uint8_t complevel) |
b5256303 TC |
3478 | { |
3479 | arc_buf_hdr_t *hdr; | |
3480 | arc_buf_t *buf; | |
3481 | arc_buf_contents_t type = DMU_OT_IS_METADATA(ot) ? | |
3482 | ARC_BUFC_METADATA : ARC_BUFC_DATA; | |
3483 | ||
3484 | ASSERT3U(lsize, >, 0); | |
3485 | ASSERT3U(lsize, >=, psize); | |
3486 | ASSERT3U(compression_type, >=, ZIO_COMPRESS_OFF); | |
3487 | ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS); | |
3488 | ||
3489 | hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, B_TRUE, | |
6b88b4b5 | 3490 | compression_type, complevel, type); |
b5256303 TC |
3491 | |
3492 | hdr->b_crypt_hdr.b_dsobj = dsobj; | |
3493 | hdr->b_crypt_hdr.b_ot = ot; | |
3494 | hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ? | |
3495 | DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot); | |
861166b0 AZ |
3496 | memcpy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN); |
3497 | memcpy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN); | |
3498 | memcpy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN); | |
b5256303 TC |
3499 | |
3500 | /* | |
3501 | * This buffer will be considered encrypted even if the ot is not an | |
3502 | * encrypted type. It will become authenticated instead in | |
3503 | * arc_write_ready(). | |
3504 | */ | |
3505 | buf = NULL; | |
be9a5c35 | 3506 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_TRUE, B_TRUE, |
b5256303 TC |
3507 | B_FALSE, B_FALSE, &buf)); |
3508 | arc_buf_thaw(buf); | |
b5256303 TC |
3509 | |
3510 | return (buf); | |
3511 | } | |
3512 | ||
08532162 GA |
3513 | static void |
3514 | l2arc_hdr_arcstats_update(arc_buf_hdr_t *hdr, boolean_t incr, | |
3515 | boolean_t state_only) | |
3516 | { | |
3517 | l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; | |
3518 | l2arc_dev_t *dev = l2hdr->b_dev; | |
3519 | uint64_t lsize = HDR_GET_LSIZE(hdr); | |
3520 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
3521 | uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
3522 | arc_buf_contents_t type = hdr->b_type; | |
3523 | int64_t lsize_s; | |
3524 | int64_t psize_s; | |
3525 | int64_t asize_s; | |
3526 | ||
3527 | if (incr) { | |
3528 | lsize_s = lsize; | |
3529 | psize_s = psize; | |
3530 | asize_s = asize; | |
3531 | } else { | |
3532 | lsize_s = -lsize; | |
3533 | psize_s = -psize; | |
3534 | asize_s = -asize; | |
3535 | } | |
3536 | ||
3537 | /* If the buffer is a prefetch, count it as such. */ | |
3538 | if (HDR_PREFETCH(hdr)) { | |
3539 | ARCSTAT_INCR(arcstat_l2_prefetch_asize, asize_s); | |
3540 | } else { | |
3541 | /* | |
3542 | * We use the value stored in the L2 header upon initial | |
3543 | * caching in L2ARC. This value will be updated in case | |
3544 | * an MRU/MRU_ghost buffer transitions to MFU but the L2ARC | |
3545 | * metadata (log entry) cannot currently be updated. Having | |
3546 | * the ARC state in the L2 header solves the problem of a | |
3547 | * possibly absent L1 header (apparent in buffers restored | |
3548 | * from persistent L2ARC). | |
3549 | */ | |
3550 | switch (hdr->b_l2hdr.b_arcs_state) { | |
3551 | case ARC_STATE_MRU_GHOST: | |
3552 | case ARC_STATE_MRU: | |
3553 | ARCSTAT_INCR(arcstat_l2_mru_asize, asize_s); | |
3554 | break; | |
3555 | case ARC_STATE_MFU_GHOST: | |
3556 | case ARC_STATE_MFU: | |
3557 | ARCSTAT_INCR(arcstat_l2_mfu_asize, asize_s); | |
3558 | break; | |
3559 | default: | |
3560 | break; | |
3561 | } | |
3562 | } | |
3563 | ||
3564 | if (state_only) | |
3565 | return; | |
3566 | ||
3567 | ARCSTAT_INCR(arcstat_l2_psize, psize_s); | |
3568 | ARCSTAT_INCR(arcstat_l2_lsize, lsize_s); | |
3569 | ||
3570 | switch (type) { | |
3571 | case ARC_BUFC_DATA: | |
3572 | ARCSTAT_INCR(arcstat_l2_bufc_data_asize, asize_s); | |
3573 | break; | |
3574 | case ARC_BUFC_METADATA: | |
3575 | ARCSTAT_INCR(arcstat_l2_bufc_metadata_asize, asize_s); | |
3576 | break; | |
3577 | default: | |
3578 | break; | |
3579 | } | |
3580 | } | |
3581 | ||
3582 | ||
d962d5da PS |
3583 | static void |
3584 | arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) | |
3585 | { | |
3586 | l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; | |
3587 | l2arc_dev_t *dev = l2hdr->b_dev; | |
7558997d SD |
3588 | uint64_t psize = HDR_GET_PSIZE(hdr); |
3589 | uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
d962d5da PS |
3590 | |
3591 | ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); | |
3592 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
3593 | ||
3594 | list_remove(&dev->l2ad_buflist, hdr); | |
3595 | ||
08532162 | 3596 | l2arc_hdr_arcstats_decrement(hdr); |
7558997d | 3597 | vdev_space_update(dev->l2ad_vdev, -asize, 0, 0); |
d962d5da | 3598 | |
7558997d SD |
3599 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr), |
3600 | hdr); | |
d3c2ae1c | 3601 | arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); |
d962d5da PS |
3602 | } |
3603 | ||
34dc7c2f BB |
3604 | static void |
3605 | arc_hdr_destroy(arc_buf_hdr_t *hdr) | |
3606 | { | |
b9541d6b | 3607 | if (HDR_HAS_L1HDR(hdr)) { |
424fd7c3 | 3608 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
b9541d6b CW |
3609 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
3610 | } | |
34dc7c2f | 3611 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b CW |
3612 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); |
3613 | ||
3614 | if (HDR_HAS_L2HDR(hdr)) { | |
d962d5da PS |
3615 | l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; |
3616 | boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); | |
428870ff | 3617 | |
d962d5da PS |
3618 | if (!buflist_held) |
3619 | mutex_enter(&dev->l2ad_mtx); | |
b9541d6b | 3620 | |
ca0bf58d | 3621 | /* |
d962d5da PS |
3622 | * Even though we checked this conditional above, we |
3623 | * need to check this again now that we have the | |
3624 | * l2ad_mtx. This is because we could be racing with | |
3625 | * another thread calling l2arc_evict() which might have | |
3626 | * destroyed this header's L2 portion as we were waiting | |
3627 | * to acquire the l2ad_mtx. If that happens, we don't | |
3628 | * want to re-destroy the header's L2 portion. | |
ca0bf58d | 3629 | */ |
2a49ebbb GA |
3630 | if (HDR_HAS_L2HDR(hdr)) { |
3631 | ||
3632 | if (!HDR_EMPTY(hdr)) | |
3633 | buf_discard_identity(hdr); | |
3634 | ||
d962d5da | 3635 | arc_hdr_l2hdr_destroy(hdr); |
2a49ebbb | 3636 | } |
428870ff BB |
3637 | |
3638 | if (!buflist_held) | |
d962d5da | 3639 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
3640 | } |
3641 | ||
ca6c7a94 BB |
3642 | /* |
3643 | * The header's identify can only be safely discarded once it is no | |
3644 | * longer discoverable. This requires removing it from the hash table | |
3645 | * and the l2arc header list. After this point the hash lock can not | |
3646 | * be used to protect the header. | |
3647 | */ | |
3648 | if (!HDR_EMPTY(hdr)) | |
3649 | buf_discard_identity(hdr); | |
3650 | ||
d3c2ae1c GW |
3651 | if (HDR_HAS_L1HDR(hdr)) { |
3652 | arc_cksum_free(hdr); | |
b9541d6b | 3653 | |
d3c2ae1c | 3654 | while (hdr->b_l1hdr.b_buf != NULL) |
2aa34383 | 3655 | arc_buf_destroy_impl(hdr->b_l1hdr.b_buf); |
34dc7c2f | 3656 | |
ca6c7a94 | 3657 | if (hdr->b_l1hdr.b_pabd != NULL) |
b5256303 | 3658 | arc_hdr_free_abd(hdr, B_FALSE); |
b5256303 | 3659 | |
440a3eb9 | 3660 | if (HDR_HAS_RABD(hdr)) |
b5256303 | 3661 | arc_hdr_free_abd(hdr, B_TRUE); |
b9541d6b CW |
3662 | } |
3663 | ||
34dc7c2f | 3664 | ASSERT3P(hdr->b_hash_next, ==, NULL); |
b9541d6b | 3665 | if (HDR_HAS_L1HDR(hdr)) { |
ca0bf58d | 3666 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b | 3667 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
bacf366f AM |
3668 | #ifdef ZFS_DEBUG |
3669 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
3670 | #endif | |
75a2eb7f | 3671 | kmem_cache_free(hdr_full_cache, hdr); |
b9541d6b CW |
3672 | } else { |
3673 | kmem_cache_free(hdr_l2only_cache, hdr); | |
3674 | } | |
34dc7c2f BB |
3675 | } |
3676 | ||
3677 | void | |
dd66857d | 3678 | arc_buf_destroy(arc_buf_t *buf, const void *tag) |
34dc7c2f BB |
3679 | { |
3680 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
34dc7c2f | 3681 | |
b9541d6b | 3682 | if (hdr->b_l1hdr.b_state == arc_anon) { |
96b9cf42 AM |
3683 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, buf); |
3684 | ASSERT(ARC_BUF_LAST(buf)); | |
d3c2ae1c | 3685 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
c935fe2e | 3686 | VERIFY0(remove_reference(hdr, tag)); |
d3c2ae1c | 3687 | return; |
34dc7c2f BB |
3688 | } |
3689 | ||
ca6c7a94 | 3690 | kmutex_t *hash_lock = HDR_LOCK(hdr); |
34dc7c2f | 3691 | mutex_enter(hash_lock); |
ca6c7a94 | 3692 | |
d3c2ae1c | 3693 | ASSERT3P(hdr, ==, buf->b_hdr); |
96b9cf42 | 3694 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL); |
428870ff | 3695 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
d3c2ae1c GW |
3696 | ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon); |
3697 | ASSERT3P(buf->b_data, !=, NULL); | |
34dc7c2f | 3698 | |
2aa34383 | 3699 | arc_buf_destroy_impl(buf); |
ed2f7ba0 | 3700 | (void) remove_reference(hdr, tag); |
34dc7c2f | 3701 | mutex_exit(hash_lock); |
34dc7c2f BB |
3702 | } |
3703 | ||
34dc7c2f | 3704 | /* |
ca0bf58d PS |
3705 | * Evict the arc_buf_hdr that is provided as a parameter. The resultant |
3706 | * state of the header is dependent on its state prior to entering this | |
3707 | * function. The following transitions are possible: | |
34dc7c2f | 3708 | * |
ca0bf58d PS |
3709 | * - arc_mru -> arc_mru_ghost |
3710 | * - arc_mfu -> arc_mfu_ghost | |
3711 | * - arc_mru_ghost -> arc_l2c_only | |
3712 | * - arc_mru_ghost -> deleted | |
3713 | * - arc_mfu_ghost -> arc_l2c_only | |
3714 | * - arc_mfu_ghost -> deleted | |
ed2f7ba0 | 3715 | * - arc_uncached -> deleted |
f7de776d AM |
3716 | * |
3717 | * Return total size of evicted data buffers for eviction progress tracking. | |
3718 | * When evicting from ghost states return logical buffer size to make eviction | |
3719 | * progress at the same (or at least comparable) rate as from non-ghost states. | |
3720 | * | |
3721 | * Return *real_evicted for actual ARC size reduction to wake up threads | |
3722 | * waiting for it. For non-ghost states it includes size of evicted data | |
3723 | * buffers (the headers are not freed there). For ghost states it includes | |
3724 | * only the evicted headers size. | |
34dc7c2f | 3725 | */ |
ca0bf58d | 3726 | static int64_t |
c935fe2e | 3727 | arc_evict_hdr(arc_buf_hdr_t *hdr, uint64_t *real_evicted) |
34dc7c2f | 3728 | { |
ca0bf58d PS |
3729 | arc_state_t *evicted_state, *state; |
3730 | int64_t bytes_evicted = 0; | |
fdc2d303 | 3731 | uint_t min_lifetime = HDR_PRESCIENT_PREFETCH(hdr) ? |
d4a72f23 | 3732 | arc_min_prescient_prefetch_ms : arc_min_prefetch_ms; |
34dc7c2f | 3733 | |
c935fe2e | 3734 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); |
ca0bf58d | 3735 | ASSERT(HDR_HAS_L1HDR(hdr)); |
c935fe2e | 3736 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
289f7e6a AM |
3737 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
3738 | ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt)); | |
e8b96c60 | 3739 | |
f7de776d | 3740 | *real_evicted = 0; |
ca0bf58d PS |
3741 | state = hdr->b_l1hdr.b_state; |
3742 | if (GHOST_STATE(state)) { | |
e8b96c60 MA |
3743 | |
3744 | /* | |
ca0bf58d | 3745 | * l2arc_write_buffers() relies on a header's L1 portion |
a6255b7f | 3746 | * (i.e. its b_pabd field) during it's write phase. |
ca0bf58d PS |
3747 | * Thus, we cannot push a header onto the arc_l2c_only |
3748 | * state (removing its L1 piece) until the header is | |
3749 | * done being written to the l2arc. | |
e8b96c60 | 3750 | */ |
ca0bf58d PS |
3751 | if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { |
3752 | ARCSTAT_BUMP(arcstat_evict_l2_skip); | |
3753 | return (bytes_evicted); | |
e8b96c60 MA |
3754 | } |
3755 | ||
ca0bf58d | 3756 | ARCSTAT_BUMP(arcstat_deleted); |
d3c2ae1c | 3757 | bytes_evicted += HDR_GET_LSIZE(hdr); |
428870ff | 3758 | |
ca0bf58d | 3759 | DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); |
428870ff | 3760 | |
ca0bf58d | 3761 | if (HDR_HAS_L2HDR(hdr)) { |
a6255b7f | 3762 | ASSERT(hdr->b_l1hdr.b_pabd == NULL); |
b5256303 | 3763 | ASSERT(!HDR_HAS_RABD(hdr)); |
ca0bf58d PS |
3764 | /* |
3765 | * This buffer is cached on the 2nd Level ARC; | |
3766 | * don't destroy the header. | |
3767 | */ | |
c935fe2e | 3768 | arc_change_state(arc_l2c_only, hdr); |
ca0bf58d PS |
3769 | /* |
3770 | * dropping from L1+L2 cached to L2-only, | |
3771 | * realloc to remove the L1 header. | |
3772 | */ | |
6a42939f | 3773 | (void) arc_hdr_realloc(hdr, hdr_full_cache, |
ca0bf58d | 3774 | hdr_l2only_cache); |
f7de776d | 3775 | *real_evicted += HDR_FULL_SIZE - HDR_L2ONLY_SIZE; |
34dc7c2f | 3776 | } else { |
c935fe2e | 3777 | arc_change_state(arc_anon, hdr); |
ca0bf58d | 3778 | arc_hdr_destroy(hdr); |
f7de776d | 3779 | *real_evicted += HDR_FULL_SIZE; |
34dc7c2f | 3780 | } |
ca0bf58d | 3781 | return (bytes_evicted); |
34dc7c2f BB |
3782 | } |
3783 | ||
ed2f7ba0 AM |
3784 | ASSERT(state == arc_mru || state == arc_mfu || state == arc_uncached); |
3785 | evicted_state = (state == arc_uncached) ? arc_anon : | |
3786 | ((state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost); | |
34dc7c2f | 3787 | |
ca0bf58d | 3788 | /* prefetch buffers have a minimum lifespan */ |
c935fe2e | 3789 | if ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && |
2b84817f | 3790 | ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < |
c935fe2e | 3791 | MSEC_TO_TICK(min_lifetime)) { |
ca0bf58d PS |
3792 | ARCSTAT_BUMP(arcstat_evict_skip); |
3793 | return (bytes_evicted); | |
da8ccd0e PS |
3794 | } |
3795 | ||
ca0bf58d | 3796 | if (HDR_HAS_L2HDR(hdr)) { |
d3c2ae1c | 3797 | ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr)); |
ca0bf58d | 3798 | } else { |
d3c2ae1c GW |
3799 | if (l2arc_write_eligible(hdr->b_spa, hdr)) { |
3800 | ARCSTAT_INCR(arcstat_evict_l2_eligible, | |
3801 | HDR_GET_LSIZE(hdr)); | |
08532162 GA |
3802 | |
3803 | switch (state->arcs_state) { | |
3804 | case ARC_STATE_MRU: | |
3805 | ARCSTAT_INCR( | |
3806 | arcstat_evict_l2_eligible_mru, | |
3807 | HDR_GET_LSIZE(hdr)); | |
3808 | break; | |
3809 | case ARC_STATE_MFU: | |
3810 | ARCSTAT_INCR( | |
3811 | arcstat_evict_l2_eligible_mfu, | |
3812 | HDR_GET_LSIZE(hdr)); | |
3813 | break; | |
3814 | default: | |
3815 | break; | |
3816 | } | |
d3c2ae1c GW |
3817 | } else { |
3818 | ARCSTAT_INCR(arcstat_evict_l2_ineligible, | |
3819 | HDR_GET_LSIZE(hdr)); | |
3820 | } | |
ca0bf58d | 3821 | } |
34dc7c2f | 3822 | |
289f7e6a AM |
3823 | bytes_evicted += arc_hdr_size(hdr); |
3824 | *real_evicted += arc_hdr_size(hdr); | |
d3c2ae1c | 3825 | |
289f7e6a AM |
3826 | /* |
3827 | * If this hdr is being evicted and has a compressed buffer then we | |
3828 | * discard it here before we change states. This ensures that the | |
3829 | * accounting is updated correctly in arc_free_data_impl(). | |
3830 | */ | |
3831 | if (hdr->b_l1hdr.b_pabd != NULL) | |
3832 | arc_hdr_free_abd(hdr, B_FALSE); | |
b5256303 | 3833 | |
289f7e6a AM |
3834 | if (HDR_HAS_RABD(hdr)) |
3835 | arc_hdr_free_abd(hdr, B_TRUE); | |
d3c2ae1c | 3836 | |
289f7e6a AM |
3837 | arc_change_state(evicted_state, hdr); |
3838 | DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); | |
3839 | if (evicted_state == arc_anon) { | |
3840 | arc_hdr_destroy(hdr); | |
3841 | *real_evicted += HDR_FULL_SIZE; | |
3842 | } else { | |
3843 | ASSERT(HDR_IN_HASH_TABLE(hdr)); | |
ca0bf58d | 3844 | } |
34dc7c2f | 3845 | |
ca0bf58d | 3846 | return (bytes_evicted); |
34dc7c2f BB |
3847 | } |
3848 | ||
3442c2a0 MA |
3849 | static void |
3850 | arc_set_need_free(void) | |
3851 | { | |
3852 | ASSERT(MUTEX_HELD(&arc_evict_lock)); | |
3853 | int64_t remaining = arc_free_memory() - arc_sys_free / 2; | |
3854 | arc_evict_waiter_t *aw = list_tail(&arc_evict_waiters); | |
3855 | if (aw == NULL) { | |
3856 | arc_need_free = MAX(-remaining, 0); | |
3857 | } else { | |
3858 | arc_need_free = | |
3859 | MAX(-remaining, (int64_t)(aw->aew_count - arc_evict_count)); | |
3860 | } | |
3861 | } | |
3862 | ||
ca0bf58d PS |
3863 | static uint64_t |
3864 | arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, | |
8172df64 | 3865 | uint64_t spa, uint64_t bytes) |
34dc7c2f | 3866 | { |
ca0bf58d | 3867 | multilist_sublist_t *mls; |
f7de776d | 3868 | uint64_t bytes_evicted = 0, real_evicted = 0; |
ca0bf58d | 3869 | arc_buf_hdr_t *hdr; |
34dc7c2f | 3870 | kmutex_t *hash_lock; |
fdc2d303 | 3871 | uint_t evict_count = zfs_arc_evict_batch_limit; |
34dc7c2f | 3872 | |
ca0bf58d | 3873 | ASSERT3P(marker, !=, NULL); |
ca0bf58d PS |
3874 | |
3875 | mls = multilist_sublist_lock(ml, idx); | |
572e2857 | 3876 | |
8172df64 | 3877 | for (hdr = multilist_sublist_prev(mls, marker); likely(hdr != NULL); |
ca0bf58d | 3878 | hdr = multilist_sublist_prev(mls, marker)) { |
fdc2d303 | 3879 | if ((evict_count == 0) || (bytes_evicted >= bytes)) |
ca0bf58d PS |
3880 | break; |
3881 | ||
3882 | /* | |
3883 | * To keep our iteration location, move the marker | |
3884 | * forward. Since we're not holding hdr's hash lock, we | |
3885 | * must be very careful and not remove 'hdr' from the | |
3886 | * sublist. Otherwise, other consumers might mistake the | |
3887 | * 'hdr' as not being on a sublist when they call the | |
3888 | * multilist_link_active() function (they all rely on | |
3889 | * the hash lock protecting concurrent insertions and | |
3890 | * removals). multilist_sublist_move_forward() was | |
3891 | * specifically implemented to ensure this is the case | |
3892 | * (only 'marker' will be removed and re-inserted). | |
3893 | */ | |
3894 | multilist_sublist_move_forward(mls, marker); | |
3895 | ||
3896 | /* | |
3897 | * The only case where the b_spa field should ever be | |
3898 | * zero, is the marker headers inserted by | |
3899 | * arc_evict_state(). It's possible for multiple threads | |
3900 | * to be calling arc_evict_state() concurrently (e.g. | |
3901 | * dsl_pool_close() and zio_inject_fault()), so we must | |
3902 | * skip any markers we see from these other threads. | |
3903 | */ | |
2a432414 | 3904 | if (hdr->b_spa == 0) |
572e2857 BB |
3905 | continue; |
3906 | ||
ca0bf58d PS |
3907 | /* we're only interested in evicting buffers of a certain spa */ |
3908 | if (spa != 0 && hdr->b_spa != spa) { | |
3909 | ARCSTAT_BUMP(arcstat_evict_skip); | |
428870ff | 3910 | continue; |
ca0bf58d PS |
3911 | } |
3912 | ||
3913 | hash_lock = HDR_LOCK(hdr); | |
e8b96c60 MA |
3914 | |
3915 | /* | |
ca0bf58d PS |
3916 | * We aren't calling this function from any code path |
3917 | * that would already be holding a hash lock, so we're | |
3918 | * asserting on this assumption to be defensive in case | |
3919 | * this ever changes. Without this check, it would be | |
3920 | * possible to incorrectly increment arcstat_mutex_miss | |
3921 | * below (e.g. if the code changed such that we called | |
3922 | * this function with a hash lock held). | |
e8b96c60 | 3923 | */ |
ca0bf58d PS |
3924 | ASSERT(!MUTEX_HELD(hash_lock)); |
3925 | ||
34dc7c2f | 3926 | if (mutex_tryenter(hash_lock)) { |
f7de776d | 3927 | uint64_t revicted; |
c935fe2e | 3928 | uint64_t evicted = arc_evict_hdr(hdr, &revicted); |
ca0bf58d | 3929 | mutex_exit(hash_lock); |
34dc7c2f | 3930 | |
ca0bf58d | 3931 | bytes_evicted += evicted; |
f7de776d | 3932 | real_evicted += revicted; |
34dc7c2f | 3933 | |
572e2857 | 3934 | /* |
ca0bf58d PS |
3935 | * If evicted is zero, arc_evict_hdr() must have |
3936 | * decided to skip this header, don't increment | |
3937 | * evict_count in this case. | |
572e2857 | 3938 | */ |
ca0bf58d | 3939 | if (evicted != 0) |
8172df64 | 3940 | evict_count--; |
ca0bf58d | 3941 | |
e8b96c60 | 3942 | } else { |
ca0bf58d | 3943 | ARCSTAT_BUMP(arcstat_mutex_miss); |
e8b96c60 | 3944 | } |
34dc7c2f | 3945 | } |
34dc7c2f | 3946 | |
ca0bf58d | 3947 | multilist_sublist_unlock(mls); |
34dc7c2f | 3948 | |
3442c2a0 MA |
3949 | /* |
3950 | * Increment the count of evicted bytes, and wake up any threads that | |
3951 | * are waiting for the count to reach this value. Since the list is | |
3952 | * ordered by ascending aew_count, we pop off the beginning of the | |
3953 | * list until we reach the end, or a waiter that's past the current | |
3954 | * "count". Doing this outside the loop reduces the number of times | |
3955 | * we need to acquire the global arc_evict_lock. | |
3956 | * | |
3957 | * Only wake when there's sufficient free memory in the system | |
3958 | * (specifically, arc_sys_free/2, which by default is a bit more than | |
3959 | * 1/64th of RAM). See the comments in arc_wait_for_eviction(). | |
3960 | */ | |
3961 | mutex_enter(&arc_evict_lock); | |
f7de776d | 3962 | arc_evict_count += real_evicted; |
3442c2a0 | 3963 | |
dc303dcf | 3964 | if (arc_free_memory() > arc_sys_free / 2) { |
3442c2a0 MA |
3965 | arc_evict_waiter_t *aw; |
3966 | while ((aw = list_head(&arc_evict_waiters)) != NULL && | |
3967 | aw->aew_count <= arc_evict_count) { | |
3968 | list_remove(&arc_evict_waiters, aw); | |
3969 | cv_broadcast(&aw->aew_cv); | |
3970 | } | |
3971 | } | |
3972 | arc_set_need_free(); | |
3973 | mutex_exit(&arc_evict_lock); | |
3974 | ||
67c0f0de MA |
3975 | /* |
3976 | * If the ARC size is reduced from arc_c_max to arc_c_min (especially | |
3977 | * if the average cached block is small), eviction can be on-CPU for | |
3978 | * many seconds. To ensure that other threads that may be bound to | |
3979 | * this CPU are able to make progress, make a voluntary preemption | |
3980 | * call here. | |
3981 | */ | |
0e4c830b | 3982 | kpreempt(KPREEMPT_SYNC); |
67c0f0de | 3983 | |
ca0bf58d | 3984 | return (bytes_evicted); |
34dc7c2f BB |
3985 | } |
3986 | ||
6e2a5918 MJ |
3987 | /* |
3988 | * Allocate an array of buffer headers used as placeholders during arc state | |
3989 | * eviction. | |
3990 | */ | |
3991 | static arc_buf_hdr_t ** | |
3992 | arc_state_alloc_markers(int count) | |
3993 | { | |
3994 | arc_buf_hdr_t **markers; | |
3995 | ||
3996 | markers = kmem_zalloc(sizeof (*markers) * count, KM_SLEEP); | |
3997 | for (int i = 0; i < count; i++) { | |
3998 | markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); | |
3999 | ||
4000 | /* | |
4001 | * A b_spa of 0 is used to indicate that this header is | |
a8d83e2a | 4002 | * a marker. This fact is used in arc_evict_state_impl(). |
6e2a5918 MJ |
4003 | */ |
4004 | markers[i]->b_spa = 0; | |
4005 | ||
4006 | } | |
4007 | return (markers); | |
4008 | } | |
4009 | ||
4010 | static void | |
4011 | arc_state_free_markers(arc_buf_hdr_t **markers, int count) | |
4012 | { | |
4013 | for (int i = 0; i < count; i++) | |
4014 | kmem_cache_free(hdr_full_cache, markers[i]); | |
4015 | kmem_free(markers, sizeof (*markers) * count); | |
4016 | } | |
4017 | ||
ca0bf58d PS |
4018 | /* |
4019 | * Evict buffers from the given arc state, until we've removed the | |
4020 | * specified number of bytes. Move the removed buffers to the | |
4021 | * appropriate evict state. | |
4022 | * | |
4023 | * This function makes a "best effort". It skips over any buffers | |
4024 | * it can't get a hash_lock on, and so, may not catch all candidates. | |
4025 | * It may also return without evicting as much space as requested. | |
4026 | * | |
4027 | * If bytes is specified using the special value ARC_EVICT_ALL, this | |
4028 | * will evict all available (i.e. unlocked and evictable) buffers from | |
4029 | * the given arc state; which is used by arc_flush(). | |
4030 | */ | |
4031 | static uint64_t | |
a8d83e2a AM |
4032 | arc_evict_state(arc_state_t *state, arc_buf_contents_t type, uint64_t spa, |
4033 | uint64_t bytes) | |
34dc7c2f | 4034 | { |
ca0bf58d | 4035 | uint64_t total_evicted = 0; |
ffdf019c | 4036 | multilist_t *ml = &state->arcs_list[type]; |
ca0bf58d PS |
4037 | int num_sublists; |
4038 | arc_buf_hdr_t **markers; | |
ca0bf58d | 4039 | |
ca0bf58d | 4040 | num_sublists = multilist_get_num_sublists(ml); |
d164b209 BB |
4041 | |
4042 | /* | |
ca0bf58d PS |
4043 | * If we've tried to evict from each sublist, made some |
4044 | * progress, but still have not hit the target number of bytes | |
4045 | * to evict, we want to keep trying. The markers allow us to | |
4046 | * pick up where we left off for each individual sublist, rather | |
4047 | * than starting from the tail each time. | |
d164b209 | 4048 | */ |
6e2a5918 MJ |
4049 | if (zthr_iscurthread(arc_evict_zthr)) { |
4050 | markers = arc_state_evict_markers; | |
4051 | ASSERT3S(num_sublists, <=, arc_state_evict_marker_count); | |
4052 | } else { | |
4053 | markers = arc_state_alloc_markers(num_sublists); | |
4054 | } | |
1c27024e | 4055 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d | 4056 | multilist_sublist_t *mls; |
34dc7c2f | 4057 | |
ca0bf58d PS |
4058 | mls = multilist_sublist_lock(ml, i); |
4059 | multilist_sublist_insert_tail(mls, markers[i]); | |
4060 | multilist_sublist_unlock(mls); | |
34dc7c2f BB |
4061 | } |
4062 | ||
d164b209 | 4063 | /* |
ca0bf58d PS |
4064 | * While we haven't hit our target number of bytes to evict, or |
4065 | * we're evicting all available buffers. | |
d164b209 | 4066 | */ |
8172df64 | 4067 | while (total_evicted < bytes) { |
25458cbe TC |
4068 | int sublist_idx = multilist_get_random_index(ml); |
4069 | uint64_t scan_evicted = 0; | |
4070 | ||
ca0bf58d PS |
4071 | /* |
4072 | * Start eviction using a randomly selected sublist, | |
4073 | * this is to try and evenly balance eviction across all | |
4074 | * sublists. Always starting at the same sublist | |
4075 | * (e.g. index 0) would cause evictions to favor certain | |
4076 | * sublists over others. | |
4077 | */ | |
1c27024e | 4078 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d PS |
4079 | uint64_t bytes_remaining; |
4080 | uint64_t bytes_evicted; | |
d164b209 | 4081 | |
8172df64 | 4082 | if (total_evicted < bytes) |
ca0bf58d PS |
4083 | bytes_remaining = bytes - total_evicted; |
4084 | else | |
4085 | break; | |
34dc7c2f | 4086 | |
ca0bf58d PS |
4087 | bytes_evicted = arc_evict_state_impl(ml, sublist_idx, |
4088 | markers[sublist_idx], spa, bytes_remaining); | |
4089 | ||
4090 | scan_evicted += bytes_evicted; | |
4091 | total_evicted += bytes_evicted; | |
4092 | ||
4093 | /* we've reached the end, wrap to the beginning */ | |
4094 | if (++sublist_idx >= num_sublists) | |
4095 | sublist_idx = 0; | |
4096 | } | |
4097 | ||
4098 | /* | |
4099 | * If we didn't evict anything during this scan, we have | |
4100 | * no reason to believe we'll evict more during another | |
4101 | * scan, so break the loop. | |
4102 | */ | |
4103 | if (scan_evicted == 0) { | |
4104 | /* This isn't possible, let's make that obvious */ | |
4105 | ASSERT3S(bytes, !=, 0); | |
34dc7c2f | 4106 | |
ca0bf58d PS |
4107 | /* |
4108 | * When bytes is ARC_EVICT_ALL, the only way to | |
4109 | * break the loop is when scan_evicted is zero. | |
4110 | * In that case, we actually have evicted enough, | |
4111 | * so we don't want to increment the kstat. | |
4112 | */ | |
4113 | if (bytes != ARC_EVICT_ALL) { | |
4114 | ASSERT3S(total_evicted, <, bytes); | |
4115 | ARCSTAT_BUMP(arcstat_evict_not_enough); | |
4116 | } | |
d164b209 | 4117 | |
ca0bf58d PS |
4118 | break; |
4119 | } | |
d164b209 | 4120 | } |
34dc7c2f | 4121 | |
1c27024e | 4122 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d PS |
4123 | multilist_sublist_t *mls = multilist_sublist_lock(ml, i); |
4124 | multilist_sublist_remove(mls, markers[i]); | |
4125 | multilist_sublist_unlock(mls); | |
34dc7c2f | 4126 | } |
6e2a5918 MJ |
4127 | if (markers != arc_state_evict_markers) |
4128 | arc_state_free_markers(markers, num_sublists); | |
ca0bf58d PS |
4129 | |
4130 | return (total_evicted); | |
4131 | } | |
4132 | ||
4133 | /* | |
4134 | * Flush all "evictable" data of the given type from the arc state | |
4135 | * specified. This will not evict any "active" buffers (i.e. referenced). | |
4136 | * | |
d3c2ae1c | 4137 | * When 'retry' is set to B_FALSE, the function will make a single pass |
ca0bf58d PS |
4138 | * over the state and evict any buffers that it can. Since it doesn't |
4139 | * continually retry the eviction, it might end up leaving some buffers | |
4140 | * in the ARC due to lock misses. | |
4141 | * | |
d3c2ae1c | 4142 | * When 'retry' is set to B_TRUE, the function will continually retry the |
ca0bf58d PS |
4143 | * eviction until *all* evictable buffers have been removed from the |
4144 | * state. As a result, if concurrent insertions into the state are | |
4145 | * allowed (e.g. if the ARC isn't shutting down), this function might | |
4146 | * wind up in an infinite loop, continually trying to evict buffers. | |
4147 | */ | |
4148 | static uint64_t | |
4149 | arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, | |
4150 | boolean_t retry) | |
4151 | { | |
4152 | uint64_t evicted = 0; | |
4153 | ||
424fd7c3 | 4154 | while (zfs_refcount_count(&state->arcs_esize[type]) != 0) { |
a8d83e2a | 4155 | evicted += arc_evict_state(state, type, spa, ARC_EVICT_ALL); |
ca0bf58d PS |
4156 | |
4157 | if (!retry) | |
4158 | break; | |
4159 | } | |
4160 | ||
4161 | return (evicted); | |
34dc7c2f BB |
4162 | } |
4163 | ||
ca0bf58d | 4164 | /* |
a8d83e2a AM |
4165 | * Evict the specified number of bytes from the state specified. This |
4166 | * function prevents us from trying to evict more from a state's list | |
4167 | * than is "evictable", and to skip evicting altogether when passed a | |
ca0bf58d PS |
4168 | * negative value for "bytes". In contrast, arc_evict_state() will |
4169 | * evict everything it can, when passed a negative value for "bytes". | |
4170 | */ | |
4171 | static uint64_t | |
a8d83e2a | 4172 | arc_evict_impl(arc_state_t *state, arc_buf_contents_t type, int64_t bytes) |
ca0bf58d | 4173 | { |
8172df64 | 4174 | uint64_t delta; |
ca0bf58d | 4175 | |
424fd7c3 TS |
4176 | if (bytes > 0 && zfs_refcount_count(&state->arcs_esize[type]) > 0) { |
4177 | delta = MIN(zfs_refcount_count(&state->arcs_esize[type]), | |
4178 | bytes); | |
a8d83e2a | 4179 | return (arc_evict_state(state, type, 0, delta)); |
ca0bf58d PS |
4180 | } |
4181 | ||
4182 | return (0); | |
4183 | } | |
4184 | ||
4185 | /* | |
a8d83e2a AM |
4186 | * Adjust specified fraction, taking into account initial ghost state(s) size, |
4187 | * ghost hit bytes towards increasing the fraction, ghost hit bytes towards | |
4188 | * decreasing it, plus a balance factor, controlling the decrease rate, used | |
4189 | * to balance metadata vs data. | |
ca0bf58d PS |
4190 | */ |
4191 | static uint64_t | |
a8d83e2a AM |
4192 | arc_evict_adj(uint64_t frac, uint64_t total, uint64_t up, uint64_t down, |
4193 | uint_t balance) | |
ca0bf58d | 4194 | { |
a8d83e2a AM |
4195 | if (total < 8 || up + down == 0) |
4196 | return (frac); | |
ca0bf58d PS |
4197 | |
4198 | /* | |
a8d83e2a AM |
4199 | * We should not have more ghost hits than ghost size, but they |
4200 | * may get close. Restrict maximum adjustment in that case. | |
ca0bf58d | 4201 | */ |
a8d83e2a AM |
4202 | if (up + down >= total / 4) { |
4203 | uint64_t scale = (up + down) / (total / 8); | |
4204 | up /= scale; | |
4205 | down /= scale; | |
ca0bf58d PS |
4206 | } |
4207 | ||
a8d83e2a AM |
4208 | /* Get maximal dynamic range by choosing optimal shifts. */ |
4209 | int s = highbit64(total); | |
4210 | s = MIN(64 - s, 32); | |
ca0bf58d | 4211 | |
a8d83e2a | 4212 | uint64_t ofrac = (1ULL << 32) - frac; |
ca0bf58d | 4213 | |
a8d83e2a AM |
4214 | if (frac >= 4 * ofrac) |
4215 | up /= frac / (2 * ofrac + 1); | |
4216 | up = (up << s) / (total >> (32 - s)); | |
4217 | if (ofrac >= 4 * frac) | |
4218 | down /= ofrac / (2 * frac + 1); | |
4219 | down = (down << s) / (total >> (32 - s)); | |
4220 | down = down * 100 / balance; | |
ca0bf58d | 4221 | |
a8d83e2a | 4222 | return (frac + up - down); |
ca0bf58d PS |
4223 | } |
4224 | ||
4225 | /* | |
c4c162c1 | 4226 | * Evict buffers from the cache, such that arcstat_size is capped by arc_c. |
ca0bf58d PS |
4227 | */ |
4228 | static uint64_t | |
5dd92909 | 4229 | arc_evict(void) |
ca0bf58d | 4230 | { |
a8d83e2a AM |
4231 | uint64_t asize, bytes, total_evicted = 0; |
4232 | int64_t e, mrud, mrum, mfud, mfum, w; | |
4233 | static uint64_t ogrd, ogrm, ogfd, ogfm; | |
4234 | static uint64_t gsrd, gsrm, gsfd, gsfm; | |
4235 | uint64_t ngrd, ngrm, ngfd, ngfm; | |
4236 | ||
4237 | /* Get current size of ARC states we can evict from. */ | |
4238 | mrud = zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_DATA]) + | |
4239 | zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_DATA]); | |
4240 | mrum = zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_METADATA]) + | |
4241 | zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_METADATA]); | |
4242 | mfud = zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_DATA]); | |
4243 | mfum = zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_METADATA]); | |
4244 | uint64_t d = mrud + mfud; | |
4245 | uint64_t m = mrum + mfum; | |
4246 | uint64_t t = d + m; | |
4247 | ||
4248 | /* Get ARC ghost hits since last eviction. */ | |
4249 | ngrd = wmsum_value(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA]); | |
4250 | uint64_t grd = ngrd - ogrd; | |
4251 | ogrd = ngrd; | |
4252 | ngrm = wmsum_value(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA]); | |
4253 | uint64_t grm = ngrm - ogrm; | |
4254 | ogrm = ngrm; | |
4255 | ngfd = wmsum_value(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA]); | |
4256 | uint64_t gfd = ngfd - ogfd; | |
4257 | ogfd = ngfd; | |
4258 | ngfm = wmsum_value(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA]); | |
4259 | uint64_t gfm = ngfm - ogfm; | |
4260 | ogfm = ngfm; | |
4261 | ||
4262 | /* Adjust ARC states balance based on ghost hits. */ | |
4263 | arc_meta = arc_evict_adj(arc_meta, gsrd + gsrm + gsfd + gsfm, | |
4264 | grm + gfm, grd + gfd, zfs_arc_meta_balance); | |
4265 | arc_pd = arc_evict_adj(arc_pd, gsrd + gsfd, grd, gfd, 100); | |
4266 | arc_pm = arc_evict_adj(arc_pm, gsrm + gsfm, grm, gfm, 100); | |
ca0bf58d | 4267 | |
c4c162c1 | 4268 | asize = aggsum_value(&arc_sums.arcstat_size); |
a8d83e2a AM |
4269 | int64_t wt = t - (asize - arc_c); |
4270 | ||
4271 | /* | |
4272 | * Try to reduce pinned dnodes if more than 3/4 of wanted metadata | |
4273 | * target is not evictable or if they go over arc_dnode_limit. | |
4274 | */ | |
4275 | int64_t prune = 0; | |
4276 | int64_t dn = wmsum_value(&arc_sums.arcstat_dnode_size); | |
1038f87c | 4277 | w = wt * (int64_t)(arc_meta >> 16) >> 16; |
a8d83e2a AM |
4278 | if (zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_METADATA]) + |
4279 | zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_METADATA]) - | |
4280 | zfs_refcount_count(&arc_mru->arcs_esize[ARC_BUFC_METADATA]) - | |
4281 | zfs_refcount_count(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]) > | |
4282 | w * 3 / 4) { | |
4283 | prune = dn / sizeof (dnode_t) * | |
4284 | zfs_arc_dnode_reduce_percent / 100; | |
4285 | } else if (dn > arc_dnode_limit) { | |
4286 | prune = (dn - arc_dnode_limit) / sizeof (dnode_t) * | |
4287 | zfs_arc_dnode_reduce_percent / 100; | |
4288 | } | |
4289 | if (prune > 0) | |
4290 | arc_prune_async(prune); | |
4291 | ||
4292 | /* Evict MRU metadata. */ | |
1038f87c | 4293 | w = wt * (int64_t)(arc_meta * arc_pm >> 48) >> 16; |
a8d83e2a AM |
4294 | e = MIN((int64_t)(asize - arc_c), (int64_t)(mrum - w)); |
4295 | bytes = arc_evict_impl(arc_mru, ARC_BUFC_METADATA, e); | |
ca0bf58d | 4296 | total_evicted += bytes; |
a8d83e2a AM |
4297 | mrum -= bytes; |
4298 | asize -= bytes; | |
ca0bf58d | 4299 | |
a8d83e2a | 4300 | /* Evict MFU metadata. */ |
1038f87c | 4301 | w = wt * (int64_t)(arc_meta >> 16) >> 16; |
a8d83e2a AM |
4302 | e = MIN((int64_t)(asize - arc_c), (int64_t)(m - w)); |
4303 | bytes = arc_evict_impl(arc_mfu, ARC_BUFC_METADATA, e); | |
4304 | total_evicted += bytes; | |
4305 | mfum -= bytes; | |
4306 | asize -= bytes; | |
4307 | ||
4308 | /* Evict MRU data. */ | |
4309 | wt -= m - total_evicted; | |
1038f87c | 4310 | w = wt * (int64_t)(arc_pd >> 16) >> 16; |
a8d83e2a AM |
4311 | e = MIN((int64_t)(asize - arc_c), (int64_t)(mrud - w)); |
4312 | bytes = arc_evict_impl(arc_mru, ARC_BUFC_DATA, e); | |
4313 | total_evicted += bytes; | |
4314 | mrud -= bytes; | |
4315 | asize -= bytes; | |
ca0bf58d | 4316 | |
a8d83e2a AM |
4317 | /* Evict MFU data. */ |
4318 | e = asize - arc_c; | |
4319 | bytes = arc_evict_impl(arc_mfu, ARC_BUFC_DATA, e); | |
4320 | mfud -= bytes; | |
4321 | total_evicted += bytes; | |
ca0bf58d PS |
4322 | |
4323 | /* | |
a8d83e2a | 4324 | * Evict ghost lists |
ca0bf58d | 4325 | * |
a8d83e2a AM |
4326 | * Size of each state's ghost list represents how much that state |
4327 | * may grow by shrinking the other states. Would it need to shrink | |
4328 | * other states to zero (that is unlikely), its ghost size would be | |
4329 | * equal to sum of other three state sizes. But excessive ghost | |
4330 | * size may result in false ghost hits (too far back), that may | |
4331 | * never result in real cache hits if several states are competing. | |
4332 | * So choose some arbitraty point of 1/2 of other state sizes. | |
4333 | */ | |
4334 | gsrd = (mrum + mfud + mfum) / 2; | |
4335 | e = zfs_refcount_count(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]) - | |
4336 | gsrd; | |
4337 | (void) arc_evict_impl(arc_mru_ghost, ARC_BUFC_DATA, e); | |
4338 | ||
4339 | gsrm = (mrud + mfud + mfum) / 2; | |
4340 | e = zfs_refcount_count(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]) - | |
4341 | gsrm; | |
4342 | (void) arc_evict_impl(arc_mru_ghost, ARC_BUFC_METADATA, e); | |
4343 | ||
4344 | gsfd = (mrud + mrum + mfum) / 2; | |
4345 | e = zfs_refcount_count(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]) - | |
4346 | gsfd; | |
4347 | (void) arc_evict_impl(arc_mfu_ghost, ARC_BUFC_DATA, e); | |
4348 | ||
4349 | gsfm = (mrud + mrum + mfud) / 2; | |
4350 | e = zfs_refcount_count(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]) - | |
4351 | gsfm; | |
4352 | (void) arc_evict_impl(arc_mfu_ghost, ARC_BUFC_METADATA, e); | |
ca0bf58d PS |
4353 | |
4354 | return (total_evicted); | |
4355 | } | |
4356 | ||
ca0bf58d PS |
4357 | void |
4358 | arc_flush(spa_t *spa, boolean_t retry) | |
ab26409d | 4359 | { |
ca0bf58d | 4360 | uint64_t guid = 0; |
94520ca4 | 4361 | |
bc888666 | 4362 | /* |
d3c2ae1c | 4363 | * If retry is B_TRUE, a spa must not be specified since we have |
ca0bf58d PS |
4364 | * no good way to determine if all of a spa's buffers have been |
4365 | * evicted from an arc state. | |
bc888666 | 4366 | */ |
4ef69de3 | 4367 | ASSERT(!retry || spa == NULL); |
d164b209 | 4368 | |
b9541d6b | 4369 | if (spa != NULL) |
3541dc6d | 4370 | guid = spa_load_guid(spa); |
d164b209 | 4371 | |
ca0bf58d PS |
4372 | (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); |
4373 | (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); | |
4374 | ||
4375 | (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); | |
4376 | (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); | |
4377 | ||
4378 | (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); | |
4379 | (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); | |
34dc7c2f | 4380 | |
ca0bf58d PS |
4381 | (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); |
4382 | (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); | |
ed2f7ba0 AM |
4383 | |
4384 | (void) arc_flush_state(arc_uncached, guid, ARC_BUFC_DATA, retry); | |
4385 | (void) arc_flush_state(arc_uncached, guid, ARC_BUFC_METADATA, retry); | |
34dc7c2f BB |
4386 | } |
4387 | ||
c9c9c1e2 | 4388 | void |
3ec34e55 | 4389 | arc_reduce_target_size(int64_t to_free) |
34dc7c2f | 4390 | { |
a8d83e2a AM |
4391 | uint64_t c = arc_c; |
4392 | ||
4393 | if (c <= arc_c_min) | |
4394 | return; | |
3442c2a0 MA |
4395 | |
4396 | /* | |
4397 | * All callers want the ARC to actually evict (at least) this much | |
4398 | * memory. Therefore we reduce from the lower of the current size and | |
4399 | * the target size. This way, even if arc_c is much higher than | |
4400 | * arc_size (as can be the case after many calls to arc_freed(), we will | |
4401 | * immediately have arc_c < arc_size and therefore the arc_evict_zthr | |
4402 | * will evict. | |
4403 | */ | |
a8d83e2a AM |
4404 | uint64_t asize = aggsum_value(&arc_sums.arcstat_size); |
4405 | if (asize < c) | |
4406 | to_free += c - asize; | |
4407 | arc_c = MAX((int64_t)c - to_free, (int64_t)arc_c_min); | |
34dc7c2f | 4408 | |
a8d83e2a AM |
4409 | /* See comment in arc_evict_cb_check() on why lock+flag */ |
4410 | mutex_enter(&arc_evict_lock); | |
4411 | arc_evict_needed = B_TRUE; | |
4412 | mutex_exit(&arc_evict_lock); | |
4413 | zthr_wakeup(arc_evict_zthr); | |
34dc7c2f | 4414 | } |
ca67b33a MA |
4415 | |
4416 | /* | |
4417 | * Determine if the system is under memory pressure and is asking | |
d3c2ae1c | 4418 | * to reclaim memory. A return value of B_TRUE indicates that the system |
ca67b33a MA |
4419 | * is under memory pressure and that the arc should adjust accordingly. |
4420 | */ | |
c9c9c1e2 | 4421 | boolean_t |
ca67b33a MA |
4422 | arc_reclaim_needed(void) |
4423 | { | |
4424 | return (arc_available_memory() < 0); | |
4425 | } | |
4426 | ||
c9c9c1e2 | 4427 | void |
3ec34e55 | 4428 | arc_kmem_reap_soon(void) |
34dc7c2f BB |
4429 | { |
4430 | size_t i; | |
4431 | kmem_cache_t *prev_cache = NULL; | |
4432 | kmem_cache_t *prev_data_cache = NULL; | |
34dc7c2f | 4433 | |
70f02287 | 4434 | #ifdef _KERNEL |
70f02287 BB |
4435 | #if defined(_ILP32) |
4436 | /* | |
4437 | * Reclaim unused memory from all kmem caches. | |
4438 | */ | |
4439 | kmem_reap(); | |
4440 | #endif | |
4441 | #endif | |
f6046738 | 4442 | |
34dc7c2f | 4443 | for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { |
70f02287 | 4444 | #if defined(_ILP32) |
d0c614ec | 4445 | /* reach upper limit of cache size on 32-bit */ |
4446 | if (zio_buf_cache[i] == NULL) | |
4447 | break; | |
4448 | #endif | |
34dc7c2f BB |
4449 | if (zio_buf_cache[i] != prev_cache) { |
4450 | prev_cache = zio_buf_cache[i]; | |
4451 | kmem_cache_reap_now(zio_buf_cache[i]); | |
4452 | } | |
4453 | if (zio_data_buf_cache[i] != prev_data_cache) { | |
4454 | prev_data_cache = zio_data_buf_cache[i]; | |
4455 | kmem_cache_reap_now(zio_data_buf_cache[i]); | |
4456 | } | |
4457 | } | |
ca0bf58d | 4458 | kmem_cache_reap_now(buf_cache); |
b9541d6b CW |
4459 | kmem_cache_reap_now(hdr_full_cache); |
4460 | kmem_cache_reap_now(hdr_l2only_cache); | |
ca577779 | 4461 | kmem_cache_reap_now(zfs_btree_leaf_cache); |
7564073e | 4462 | abd_cache_reap_now(); |
34dc7c2f BB |
4463 | } |
4464 | ||
3ec34e55 | 4465 | static boolean_t |
5dd92909 | 4466 | arc_evict_cb_check(void *arg, zthr_t *zthr) |
3ec34e55 | 4467 | { |
14e4e3cb AZ |
4468 | (void) arg, (void) zthr; |
4469 | ||
1531506d | 4470 | #ifdef ZFS_DEBUG |
3ec34e55 BL |
4471 | /* |
4472 | * This is necessary in order to keep the kstat information | |
4473 | * up to date for tools that display kstat data such as the | |
4474 | * mdb ::arc dcmd and the Linux crash utility. These tools | |
4475 | * typically do not call kstat's update function, but simply | |
4476 | * dump out stats from the most recent update. Without | |
4477 | * this call, these commands may show stale stats for the | |
1531506d RM |
4478 | * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even |
4479 | * with this call, the data might be out of date if the | |
4480 | * evict thread hasn't been woken recently; but that should | |
4481 | * suffice. The arc_state_t structures can be queried | |
4482 | * directly if more accurate information is needed. | |
3ec34e55 BL |
4483 | */ |
4484 | if (arc_ksp != NULL) | |
4485 | arc_ksp->ks_update(arc_ksp, KSTAT_READ); | |
1531506d | 4486 | #endif |
3ec34e55 BL |
4487 | |
4488 | /* | |
3442c2a0 MA |
4489 | * We have to rely on arc_wait_for_eviction() to tell us when to |
4490 | * evict, rather than checking if we are overflowing here, so that we | |
4491 | * are sure to not leave arc_wait_for_eviction() waiting on aew_cv. | |
4492 | * If we have become "not overflowing" since arc_wait_for_eviction() | |
4493 | * checked, we need to wake it up. We could broadcast the CV here, | |
4494 | * but arc_wait_for_eviction() may have not yet gone to sleep. We | |
4495 | * would need to use a mutex to ensure that this function doesn't | |
4496 | * broadcast until arc_wait_for_eviction() has gone to sleep (e.g. | |
4497 | * the arc_evict_lock). However, the lock ordering of such a lock | |
4498 | * would necessarily be incorrect with respect to the zthr_lock, | |
4499 | * which is held before this function is called, and is held by | |
4500 | * arc_wait_for_eviction() when it calls zthr_wakeup(). | |
3ec34e55 | 4501 | */ |
ed2f7ba0 AM |
4502 | if (arc_evict_needed) |
4503 | return (B_TRUE); | |
4504 | ||
4505 | /* | |
4506 | * If we have buffers in uncached state, evict them periodically. | |
4507 | */ | |
4508 | return ((zfs_refcount_count(&arc_uncached->arcs_esize[ARC_BUFC_DATA]) + | |
4509 | zfs_refcount_count(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]) && | |
4510 | ddi_get_lbolt() - arc_last_uncached_flush > | |
4511 | MSEC_TO_TICK(arc_min_prefetch_ms / 2))); | |
3ec34e55 BL |
4512 | } |
4513 | ||
302f753f | 4514 | /* |
5dd92909 | 4515 | * Keep arc_size under arc_c by running arc_evict which evicts data |
3ec34e55 | 4516 | * from the ARC. |
302f753f | 4517 | */ |
61c3391a | 4518 | static void |
5dd92909 | 4519 | arc_evict_cb(void *arg, zthr_t *zthr) |
34dc7c2f | 4520 | { |
5caeef02 | 4521 | (void) arg; |
14e4e3cb | 4522 | |
3ec34e55 BL |
4523 | uint64_t evicted = 0; |
4524 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
34dc7c2f | 4525 | |
ed2f7ba0 AM |
4526 | /* Always try to evict from uncached state. */ |
4527 | arc_last_uncached_flush = ddi_get_lbolt(); | |
4528 | evicted += arc_flush_state(arc_uncached, 0, ARC_BUFC_DATA, B_FALSE); | |
4529 | evicted += arc_flush_state(arc_uncached, 0, ARC_BUFC_METADATA, B_FALSE); | |
4530 | ||
4531 | /* Evict from other states only if told to. */ | |
4532 | if (arc_evict_needed) | |
4533 | evicted += arc_evict(); | |
34dc7c2f | 4534 | |
3ec34e55 BL |
4535 | /* |
4536 | * If evicted is zero, we couldn't evict anything | |
5dd92909 | 4537 | * via arc_evict(). This could be due to hash lock |
3ec34e55 BL |
4538 | * collisions, but more likely due to the majority of |
4539 | * arc buffers being unevictable. Therefore, even if | |
4540 | * arc_size is above arc_c, another pass is unlikely to | |
4541 | * be helpful and could potentially cause us to enter an | |
4542 | * infinite loop. Additionally, zthr_iscancelled() is | |
4543 | * checked here so that if the arc is shutting down, the | |
5dd92909 | 4544 | * broadcast will wake any remaining arc evict waiters. |
5caeef02 DB |
4545 | * |
4546 | * Note we cancel using zthr instead of arc_evict_zthr | |
4547 | * because the latter may not yet be initializd when the | |
4548 | * callback is first invoked. | |
3ec34e55 | 4549 | */ |
5dd92909 | 4550 | mutex_enter(&arc_evict_lock); |
5caeef02 | 4551 | arc_evict_needed = !zthr_iscancelled(zthr) && |
c4c162c1 | 4552 | evicted > 0 && aggsum_compare(&arc_sums.arcstat_size, arc_c) > 0; |
5dd92909 | 4553 | if (!arc_evict_needed) { |
d3c2ae1c | 4554 | /* |
3ec34e55 BL |
4555 | * We're either no longer overflowing, or we |
4556 | * can't evict anything more, so we should wake | |
4557 | * arc_get_data_impl() sooner. | |
d3c2ae1c | 4558 | */ |
3442c2a0 MA |
4559 | arc_evict_waiter_t *aw; |
4560 | while ((aw = list_remove_head(&arc_evict_waiters)) != NULL) { | |
4561 | cv_broadcast(&aw->aew_cv); | |
4562 | } | |
4563 | arc_set_need_free(); | |
3ec34e55 | 4564 | } |
5dd92909 | 4565 | mutex_exit(&arc_evict_lock); |
3ec34e55 | 4566 | spl_fstrans_unmark(cookie); |
3ec34e55 BL |
4567 | } |
4568 | ||
3ec34e55 BL |
4569 | static boolean_t |
4570 | arc_reap_cb_check(void *arg, zthr_t *zthr) | |
4571 | { | |
14e4e3cb AZ |
4572 | (void) arg, (void) zthr; |
4573 | ||
3ec34e55 | 4574 | int64_t free_memory = arc_available_memory(); |
8a171ccd | 4575 | static int reap_cb_check_counter = 0; |
3ec34e55 BL |
4576 | |
4577 | /* | |
4578 | * If a kmem reap is already active, don't schedule more. We must | |
4579 | * check for this because kmem_cache_reap_soon() won't actually | |
4580 | * block on the cache being reaped (this is to prevent callers from | |
4581 | * becoming implicitly blocked by a system-wide kmem reap -- which, | |
4582 | * on a system with many, many full magazines, can take minutes). | |
4583 | */ | |
4584 | if (!kmem_cache_reap_active() && free_memory < 0) { | |
34dc7c2f | 4585 | |
3ec34e55 BL |
4586 | arc_no_grow = B_TRUE; |
4587 | arc_warm = B_TRUE; | |
0a252dae | 4588 | /* |
3ec34e55 BL |
4589 | * Wait at least zfs_grow_retry (default 5) seconds |
4590 | * before considering growing. | |
0a252dae | 4591 | */ |
3ec34e55 BL |
4592 | arc_growtime = gethrtime() + SEC2NSEC(arc_grow_retry); |
4593 | return (B_TRUE); | |
4594 | } else if (free_memory < arc_c >> arc_no_grow_shift) { | |
4595 | arc_no_grow = B_TRUE; | |
4596 | } else if (gethrtime() >= arc_growtime) { | |
4597 | arc_no_grow = B_FALSE; | |
4598 | } | |
0a252dae | 4599 | |
8a171ccd SG |
4600 | /* |
4601 | * Called unconditionally every 60 seconds to reclaim unused | |
4602 | * zstd compression and decompression context. This is done | |
4603 | * here to avoid the need for an independent thread. | |
4604 | */ | |
4605 | if (!((reap_cb_check_counter++) % 60)) | |
4606 | zfs_zstd_cache_reap_now(); | |
4607 | ||
3ec34e55 BL |
4608 | return (B_FALSE); |
4609 | } | |
34dc7c2f | 4610 | |
3ec34e55 BL |
4611 | /* |
4612 | * Keep enough free memory in the system by reaping the ARC's kmem | |
4613 | * caches. To cause more slabs to be reapable, we may reduce the | |
5dd92909 | 4614 | * target size of the cache (arc_c), causing the arc_evict_cb() |
3ec34e55 BL |
4615 | * to free more buffers. |
4616 | */ | |
61c3391a | 4617 | static void |
3ec34e55 BL |
4618 | arc_reap_cb(void *arg, zthr_t *zthr) |
4619 | { | |
14e4e3cb AZ |
4620 | (void) arg, (void) zthr; |
4621 | ||
3ec34e55 BL |
4622 | int64_t free_memory; |
4623 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
34dc7c2f | 4624 | |
3ec34e55 BL |
4625 | /* |
4626 | * Kick off asynchronous kmem_reap()'s of all our caches. | |
4627 | */ | |
4628 | arc_kmem_reap_soon(); | |
6a8f9b6b | 4629 | |
3ec34e55 BL |
4630 | /* |
4631 | * Wait at least arc_kmem_cache_reap_retry_ms between | |
4632 | * arc_kmem_reap_soon() calls. Without this check it is possible to | |
4633 | * end up in a situation where we spend lots of time reaping | |
4634 | * caches, while we're near arc_c_min. Waiting here also gives the | |
4635 | * subsequent free memory check a chance of finding that the | |
4636 | * asynchronous reap has already freed enough memory, and we don't | |
4637 | * need to call arc_reduce_target_size(). | |
4638 | */ | |
4639 | delay((hz * arc_kmem_cache_reap_retry_ms + 999) / 1000); | |
34dc7c2f | 4640 | |
3ec34e55 BL |
4641 | /* |
4642 | * Reduce the target size as needed to maintain the amount of free | |
4643 | * memory in the system at a fraction of the arc_size (1/128th by | |
4644 | * default). If oversubscribed (free_memory < 0) then reduce the | |
4645 | * target arc_size by the deficit amount plus the fractional | |
bf169e9f | 4646 | * amount. If free memory is positive but less than the fractional |
3ec34e55 BL |
4647 | * amount, reduce by what is needed to hit the fractional amount. |
4648 | */ | |
4649 | free_memory = arc_available_memory(); | |
34dc7c2f | 4650 | |
f933b3fd AM |
4651 | int64_t can_free = arc_c - arc_c_min; |
4652 | if (can_free > 0) { | |
4653 | int64_t to_free = (can_free >> arc_shrink_shift) - free_memory; | |
4654 | if (to_free > 0) | |
4655 | arc_reduce_target_size(to_free); | |
ca0bf58d | 4656 | } |
ca0bf58d | 4657 | spl_fstrans_unmark(cookie); |
ca0bf58d PS |
4658 | } |
4659 | ||
7cb67b45 BB |
4660 | #ifdef _KERNEL |
4661 | /* | |
302f753f BB |
4662 | * Determine the amount of memory eligible for eviction contained in the |
4663 | * ARC. All clean data reported by the ghost lists can always be safely | |
4664 | * evicted. Due to arc_c_min, the same does not hold for all clean data | |
4665 | * contained by the regular mru and mfu lists. | |
4666 | * | |
4667 | * In the case of the regular mru and mfu lists, we need to report as | |
4668 | * much clean data as possible, such that evicting that same reported | |
4669 | * data will not bring arc_size below arc_c_min. Thus, in certain | |
4670 | * circumstances, the total amount of clean data in the mru and mfu | |
4671 | * lists might not actually be evictable. | |
4672 | * | |
4673 | * The following two distinct cases are accounted for: | |
4674 | * | |
4675 | * 1. The sum of the amount of dirty data contained by both the mru and | |
4676 | * mfu lists, plus the ARC's other accounting (e.g. the anon list), | |
4677 | * is greater than or equal to arc_c_min. | |
4678 | * (i.e. amount of dirty data >= arc_c_min) | |
4679 | * | |
4680 | * This is the easy case; all clean data contained by the mru and mfu | |
4681 | * lists is evictable. Evicting all clean data can only drop arc_size | |
4682 | * to the amount of dirty data, which is greater than arc_c_min. | |
4683 | * | |
4684 | * 2. The sum of the amount of dirty data contained by both the mru and | |
4685 | * mfu lists, plus the ARC's other accounting (e.g. the anon list), | |
4686 | * is less than arc_c_min. | |
4687 | * (i.e. arc_c_min > amount of dirty data) | |
4688 | * | |
4689 | * 2.1. arc_size is greater than or equal arc_c_min. | |
4690 | * (i.e. arc_size >= arc_c_min > amount of dirty data) | |
4691 | * | |
4692 | * In this case, not all clean data from the regular mru and mfu | |
4693 | * lists is actually evictable; we must leave enough clean data | |
4694 | * to keep arc_size above arc_c_min. Thus, the maximum amount of | |
4695 | * evictable data from the two lists combined, is exactly the | |
4696 | * difference between arc_size and arc_c_min. | |
4697 | * | |
4698 | * 2.2. arc_size is less than arc_c_min | |
4699 | * (i.e. arc_c_min > arc_size > amount of dirty data) | |
4700 | * | |
4701 | * In this case, none of the data contained in the mru and mfu | |
4702 | * lists is evictable, even if it's clean. Since arc_size is | |
4703 | * already below arc_c_min, evicting any more would only | |
4704 | * increase this negative difference. | |
7cb67b45 | 4705 | */ |
7cb67b45 | 4706 | |
7cb67b45 BB |
4707 | #endif /* _KERNEL */ |
4708 | ||
34dc7c2f BB |
4709 | /* |
4710 | * Adapt arc info given the number of bytes we are trying to add and | |
4e33ba4c | 4711 | * the state that we are coming from. This function is only called |
34dc7c2f BB |
4712 | * when we are adding new content to the cache. |
4713 | */ | |
4714 | static void | |
a8d83e2a | 4715 | arc_adapt(uint64_t bytes) |
34dc7c2f | 4716 | { |
3ec34e55 BL |
4717 | /* |
4718 | * Wake reap thread if we do not have any available memory | |
4719 | */ | |
ca67b33a | 4720 | if (arc_reclaim_needed()) { |
3ec34e55 | 4721 | zthr_wakeup(arc_reap_zthr); |
ca67b33a MA |
4722 | return; |
4723 | } | |
4724 | ||
34dc7c2f BB |
4725 | if (arc_no_grow) |
4726 | return; | |
4727 | ||
4728 | if (arc_c >= arc_c_max) | |
4729 | return; | |
4730 | ||
4731 | /* | |
4732 | * If we're within (2 * maxblocksize) bytes of the target | |
4733 | * cache size, increment the target cache size | |
4734 | */ | |
a8d83e2a AM |
4735 | if (aggsum_upper_bound(&arc_sums.arcstat_size) + |
4736 | 2 * SPA_MAXBLOCKSIZE >= arc_c) { | |
4737 | uint64_t dc = MAX(bytes, SPA_OLD_MAXBLOCKSIZE); | |
4738 | if (atomic_add_64_nv(&arc_c, dc) > arc_c_max) | |
34dc7c2f | 4739 | arc_c = arc_c_max; |
34dc7c2f | 4740 | } |
34dc7c2f BB |
4741 | } |
4742 | ||
4743 | /* | |
ca0bf58d PS |
4744 | * Check if arc_size has grown past our upper threshold, determined by |
4745 | * zfs_arc_overflow_shift. | |
34dc7c2f | 4746 | */ |
f7de776d | 4747 | static arc_ovf_level_t |
6b88b4b5 | 4748 | arc_is_overflowing(boolean_t use_reserve) |
34dc7c2f | 4749 | { |
ca0bf58d | 4750 | /* Always allow at least one block of overflow */ |
5a902f5a | 4751 | int64_t overflow = MAX(SPA_MAXBLOCKSIZE, |
ca0bf58d | 4752 | arc_c >> zfs_arc_overflow_shift); |
34dc7c2f | 4753 | |
37fb3e43 PD |
4754 | /* |
4755 | * We just compare the lower bound here for performance reasons. Our | |
4756 | * primary goals are to make sure that the arc never grows without | |
4757 | * bound, and that it can reach its maximum size. This check | |
4758 | * accomplishes both goals. The maximum amount we could run over by is | |
4759 | * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block | |
4760 | * in the ARC. In practice, that's in the tens of MB, which is low | |
4761 | * enough to be safe. | |
4762 | */ | |
f7de776d AM |
4763 | int64_t over = aggsum_lower_bound(&arc_sums.arcstat_size) - |
4764 | arc_c - overflow / 2; | |
6b88b4b5 AM |
4765 | if (!use_reserve) |
4766 | overflow /= 2; | |
f7de776d AM |
4767 | return (over < 0 ? ARC_OVF_NONE : |
4768 | over < overflow ? ARC_OVF_SOME : ARC_OVF_SEVERE); | |
34dc7c2f BB |
4769 | } |
4770 | ||
a6255b7f | 4771 | static abd_t * |
dd66857d | 4772 | arc_get_data_abd(arc_buf_hdr_t *hdr, uint64_t size, const void *tag, |
6b88b4b5 | 4773 | int alloc_flags) |
a6255b7f DQ |
4774 | { |
4775 | arc_buf_contents_t type = arc_buf_type(hdr); | |
4776 | ||
6b88b4b5 | 4777 | arc_get_data_impl(hdr, size, tag, alloc_flags); |
ed2f7ba0 AM |
4778 | if (alloc_flags & ARC_HDR_ALLOC_LINEAR) |
4779 | return (abd_alloc_linear(size, type == ARC_BUFC_METADATA)); | |
4780 | else | |
4781 | return (abd_alloc(size, type == ARC_BUFC_METADATA)); | |
a6255b7f DQ |
4782 | } |
4783 | ||
4784 | static void * | |
dd66857d | 4785 | arc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, const void *tag) |
a6255b7f DQ |
4786 | { |
4787 | arc_buf_contents_t type = arc_buf_type(hdr); | |
4788 | ||
a8d83e2a | 4789 | arc_get_data_impl(hdr, size, tag, 0); |
a6255b7f DQ |
4790 | if (type == ARC_BUFC_METADATA) { |
4791 | return (zio_buf_alloc(size)); | |
4792 | } else { | |
4793 | ASSERT(type == ARC_BUFC_DATA); | |
4794 | return (zio_data_buf_alloc(size)); | |
4795 | } | |
4796 | } | |
4797 | ||
3442c2a0 MA |
4798 | /* |
4799 | * Wait for the specified amount of data (in bytes) to be evicted from the | |
4800 | * ARC, and for there to be sufficient free memory in the system. Waiting for | |
4801 | * eviction ensures that the memory used by the ARC decreases. Waiting for | |
4802 | * free memory ensures that the system won't run out of free pages, regardless | |
4803 | * of ARC behavior and settings. See arc_lowmem_init(). | |
4804 | */ | |
4805 | void | |
6b88b4b5 | 4806 | arc_wait_for_eviction(uint64_t amount, boolean_t use_reserve) |
3442c2a0 | 4807 | { |
6b88b4b5 | 4808 | switch (arc_is_overflowing(use_reserve)) { |
f7de776d AM |
4809 | case ARC_OVF_NONE: |
4810 | return; | |
4811 | case ARC_OVF_SOME: | |
4812 | /* | |
4813 | * This is a bit racy without taking arc_evict_lock, but the | |
4814 | * worst that can happen is we either call zthr_wakeup() extra | |
4815 | * time due to race with other thread here, or the set flag | |
4816 | * get cleared by arc_evict_cb(), which is unlikely due to | |
4817 | * big hysteresis, but also not important since at this level | |
4818 | * of overflow the eviction is purely advisory. Same time | |
4819 | * taking the global lock here every time without waiting for | |
4820 | * the actual eviction creates a significant lock contention. | |
4821 | */ | |
4822 | if (!arc_evict_needed) { | |
4823 | arc_evict_needed = B_TRUE; | |
4824 | zthr_wakeup(arc_evict_zthr); | |
4825 | } | |
4826 | return; | |
4827 | case ARC_OVF_SEVERE: | |
4828 | default: | |
4829 | { | |
4830 | arc_evict_waiter_t aw; | |
4831 | list_link_init(&aw.aew_node); | |
4832 | cv_init(&aw.aew_cv, NULL, CV_DEFAULT, NULL); | |
3442c2a0 | 4833 | |
f7de776d AM |
4834 | uint64_t last_count = 0; |
4835 | mutex_enter(&arc_evict_lock); | |
4836 | if (!list_is_empty(&arc_evict_waiters)) { | |
4837 | arc_evict_waiter_t *last = | |
4838 | list_tail(&arc_evict_waiters); | |
4839 | last_count = last->aew_count; | |
4840 | } else if (!arc_evict_needed) { | |
4841 | arc_evict_needed = B_TRUE; | |
4842 | zthr_wakeup(arc_evict_zthr); | |
4843 | } | |
4844 | /* | |
4845 | * Note, the last waiter's count may be less than | |
4846 | * arc_evict_count if we are low on memory in which | |
4847 | * case arc_evict_state_impl() may have deferred | |
4848 | * wakeups (but still incremented arc_evict_count). | |
4849 | */ | |
4850 | aw.aew_count = MAX(last_count, arc_evict_count) + amount; | |
3442c2a0 | 4851 | |
f7de776d | 4852 | list_insert_tail(&arc_evict_waiters, &aw); |
3442c2a0 | 4853 | |
f7de776d | 4854 | arc_set_need_free(); |
3442c2a0 | 4855 | |
f7de776d AM |
4856 | DTRACE_PROBE3(arc__wait__for__eviction, |
4857 | uint64_t, amount, | |
4858 | uint64_t, arc_evict_count, | |
4859 | uint64_t, aw.aew_count); | |
3442c2a0 | 4860 | |
f7de776d AM |
4861 | /* |
4862 | * We will be woken up either when arc_evict_count reaches | |
4863 | * aew_count, or when the ARC is no longer overflowing and | |
4864 | * eviction completes. | |
4865 | * In case of "false" wakeup, we will still be on the list. | |
4866 | */ | |
4867 | do { | |
3442c2a0 | 4868 | cv_wait(&aw.aew_cv, &arc_evict_lock); |
f7de776d AM |
4869 | } while (list_link_active(&aw.aew_node)); |
4870 | mutex_exit(&arc_evict_lock); | |
3442c2a0 | 4871 | |
f7de776d AM |
4872 | cv_destroy(&aw.aew_cv); |
4873 | } | |
3442c2a0 | 4874 | } |
3442c2a0 MA |
4875 | } |
4876 | ||
34dc7c2f | 4877 | /* |
d3c2ae1c GW |
4878 | * Allocate a block and return it to the caller. If we are hitting the |
4879 | * hard limit for the cache size, we must sleep, waiting for the eviction | |
4880 | * thread to catch up. If we're past the target size but below the hard | |
4881 | * limit, we'll only signal the reclaim thread and continue on. | |
34dc7c2f | 4882 | */ |
a6255b7f | 4883 | static void |
dd66857d | 4884 | arc_get_data_impl(arc_buf_hdr_t *hdr, uint64_t size, const void *tag, |
6b88b4b5 | 4885 | int alloc_flags) |
34dc7c2f | 4886 | { |
a8d83e2a | 4887 | arc_adapt(size); |
34dc7c2f BB |
4888 | |
4889 | /* | |
3442c2a0 MA |
4890 | * If arc_size is currently overflowing, we must be adding data |
4891 | * faster than we are evicting. To ensure we don't compound the | |
ca0bf58d | 4892 | * problem by adding more data and forcing arc_size to grow even |
3442c2a0 MA |
4893 | * further past it's target size, we wait for the eviction thread to |
4894 | * make some progress. We also wait for there to be sufficient free | |
4895 | * memory in the system, as measured by arc_free_memory(). | |
4896 | * | |
4897 | * Specifically, we wait for zfs_arc_eviction_pct percent of the | |
4898 | * requested size to be evicted. This should be more than 100%, to | |
4899 | * ensure that that progress is also made towards getting arc_size | |
4900 | * under arc_c. See the comment above zfs_arc_eviction_pct. | |
34dc7c2f | 4901 | */ |
6b88b4b5 AM |
4902 | arc_wait_for_eviction(size * zfs_arc_eviction_pct / 100, |
4903 | alloc_flags & ARC_HDR_USE_RESERVE); | |
ab26409d | 4904 | |
a8d83e2a | 4905 | arc_buf_contents_t type = arc_buf_type(hdr); |
da8ccd0e | 4906 | if (type == ARC_BUFC_METADATA) { |
ca0bf58d PS |
4907 | arc_space_consume(size, ARC_SPACE_META); |
4908 | } else { | |
ca0bf58d | 4909 | arc_space_consume(size, ARC_SPACE_DATA); |
da8ccd0e PS |
4910 | } |
4911 | ||
34dc7c2f BB |
4912 | /* |
4913 | * Update the state size. Note that ghost states have a | |
4914 | * "ghost size" and so don't need to be updated. | |
4915 | */ | |
a8d83e2a | 4916 | arc_state_t *state = hdr->b_l1hdr.b_state; |
d3c2ae1c | 4917 | if (!GHOST_STATE(state)) { |
34dc7c2f | 4918 | |
a8d83e2a AM |
4919 | (void) zfs_refcount_add_many(&state->arcs_size[type], size, |
4920 | tag); | |
ca0bf58d PS |
4921 | |
4922 | /* | |
4923 | * If this is reached via arc_read, the link is | |
4924 | * protected by the hash lock. If reached via | |
4925 | * arc_buf_alloc, the header should not be accessed by | |
4926 | * any other thread. And, if reached via arc_read_done, | |
4927 | * the hash lock will protect it if it's found in the | |
4928 | * hash table; otherwise no other thread should be | |
4929 | * trying to [add|remove]_reference it. | |
4930 | */ | |
4931 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 TS |
4932 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
4933 | (void) zfs_refcount_add_many(&state->arcs_esize[type], | |
d3c2ae1c | 4934 | size, tag); |
34dc7c2f | 4935 | } |
34dc7c2f | 4936 | } |
a6255b7f DQ |
4937 | } |
4938 | ||
4939 | static void | |
dd66857d AZ |
4940 | arc_free_data_abd(arc_buf_hdr_t *hdr, abd_t *abd, uint64_t size, |
4941 | const void *tag) | |
a6255b7f DQ |
4942 | { |
4943 | arc_free_data_impl(hdr, size, tag); | |
4944 | abd_free(abd); | |
4945 | } | |
4946 | ||
4947 | static void | |
dd66857d | 4948 | arc_free_data_buf(arc_buf_hdr_t *hdr, void *buf, uint64_t size, const void *tag) |
a6255b7f DQ |
4949 | { |
4950 | arc_buf_contents_t type = arc_buf_type(hdr); | |
4951 | ||
4952 | arc_free_data_impl(hdr, size, tag); | |
4953 | if (type == ARC_BUFC_METADATA) { | |
4954 | zio_buf_free(buf, size); | |
4955 | } else { | |
4956 | ASSERT(type == ARC_BUFC_DATA); | |
4957 | zio_data_buf_free(buf, size); | |
4958 | } | |
d3c2ae1c GW |
4959 | } |
4960 | ||
4961 | /* | |
4962 | * Free the arc data buffer. | |
4963 | */ | |
4964 | static void | |
dd66857d | 4965 | arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, const void *tag) |
d3c2ae1c GW |
4966 | { |
4967 | arc_state_t *state = hdr->b_l1hdr.b_state; | |
4968 | arc_buf_contents_t type = arc_buf_type(hdr); | |
4969 | ||
4970 | /* protected by hash lock, if in the hash table */ | |
4971 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 | 4972 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
4973 | ASSERT(state != arc_anon && state != arc_l2c_only); |
4974 | ||
424fd7c3 | 4975 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c GW |
4976 | size, tag); |
4977 | } | |
a8d83e2a | 4978 | (void) zfs_refcount_remove_many(&state->arcs_size[type], size, tag); |
d3c2ae1c GW |
4979 | |
4980 | VERIFY3U(hdr->b_type, ==, type); | |
4981 | if (type == ARC_BUFC_METADATA) { | |
d3c2ae1c GW |
4982 | arc_space_return(size, ARC_SPACE_META); |
4983 | } else { | |
4984 | ASSERT(type == ARC_BUFC_DATA); | |
d3c2ae1c GW |
4985 | arc_space_return(size, ARC_SPACE_DATA); |
4986 | } | |
34dc7c2f BB |
4987 | } |
4988 | ||
4989 | /* | |
4990 | * This routine is called whenever a buffer is accessed. | |
34dc7c2f BB |
4991 | */ |
4992 | static void | |
c935fe2e | 4993 | arc_access(arc_buf_hdr_t *hdr, arc_flags_t arc_flags, boolean_t hit) |
34dc7c2f | 4994 | { |
c935fe2e | 4995 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); |
b9541d6b | 4996 | ASSERT(HDR_HAS_L1HDR(hdr)); |
34dc7c2f | 4997 | |
c935fe2e AM |
4998 | /* |
4999 | * Update buffer prefetch status. | |
5000 | */ | |
5001 | boolean_t was_prefetch = HDR_PREFETCH(hdr); | |
5002 | boolean_t now_prefetch = arc_flags & ARC_FLAG_PREFETCH; | |
5003 | if (was_prefetch != now_prefetch) { | |
5004 | if (was_prefetch) { | |
5005 | ARCSTAT_CONDSTAT(hit, demand_hit, demand_iohit, | |
5006 | HDR_PRESCIENT_PREFETCH(hdr), prescient, predictive, | |
5007 | prefetch); | |
5008 | } | |
5009 | if (HDR_HAS_L2HDR(hdr)) | |
5010 | l2arc_hdr_arcstats_decrement_state(hdr); | |
5011 | if (was_prefetch) { | |
5012 | arc_hdr_clear_flags(hdr, | |
5013 | ARC_FLAG_PREFETCH | ARC_FLAG_PRESCIENT_PREFETCH); | |
5014 | } else { | |
5015 | arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); | |
5016 | } | |
5017 | if (HDR_HAS_L2HDR(hdr)) | |
5018 | l2arc_hdr_arcstats_increment_state(hdr); | |
5019 | } | |
5020 | if (now_prefetch) { | |
5021 | if (arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) { | |
5022 | arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH); | |
5023 | ARCSTAT_BUMP(arcstat_prescient_prefetch); | |
5024 | } else { | |
5025 | ARCSTAT_BUMP(arcstat_predictive_prefetch); | |
5026 | } | |
5027 | } | |
5028 | if (arc_flags & ARC_FLAG_L2CACHE) | |
5029 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); | |
5030 | ||
5031 | clock_t now = ddi_get_lbolt(); | |
b9541d6b | 5032 | if (hdr->b_l1hdr.b_state == arc_anon) { |
ed2f7ba0 | 5033 | arc_state_t *new_state; |
34dc7c2f | 5034 | /* |
ed2f7ba0 AM |
5035 | * This buffer is not in the cache, and does not appear in |
5036 | * our "ghost" lists. Add it to the MRU or uncached state. | |
34dc7c2f | 5037 | */ |
b9541d6b | 5038 | ASSERT0(hdr->b_l1hdr.b_arc_access); |
c935fe2e | 5039 | hdr->b_l1hdr.b_arc_access = now; |
ed2f7ba0 AM |
5040 | if (HDR_UNCACHED(hdr)) { |
5041 | new_state = arc_uncached; | |
5042 | DTRACE_PROBE1(new_state__uncached, arc_buf_hdr_t *, | |
5043 | hdr); | |
5044 | } else { | |
5045 | new_state = arc_mru; | |
5046 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); | |
5047 | } | |
5048 | arc_change_state(new_state, hdr); | |
b9541d6b | 5049 | } else if (hdr->b_l1hdr.b_state == arc_mru) { |
c935fe2e AM |
5050 | /* |
5051 | * This buffer has been accessed once recently and either | |
5052 | * its read is still in progress or it is in the cache. | |
5053 | */ | |
5054 | if (HDR_IO_IN_PROGRESS(hdr)) { | |
5055 | hdr->b_l1hdr.b_arc_access = now; | |
5056 | return; | |
5057 | } | |
5058 | hdr->b_l1hdr.b_mru_hits++; | |
5059 | ARCSTAT_BUMP(arcstat_mru_hits); | |
428870ff | 5060 | |
34dc7c2f | 5061 | /* |
c935fe2e AM |
5062 | * If the previous access was a prefetch, then it already |
5063 | * handled possible promotion, so nothing more to do for now. | |
34dc7c2f | 5064 | */ |
c935fe2e | 5065 | if (was_prefetch) { |
b9541d6b | 5066 | hdr->b_l1hdr.b_arc_access = now; |
34dc7c2f BB |
5067 | return; |
5068 | } | |
5069 | ||
5070 | /* | |
c935fe2e AM |
5071 | * If more than ARC_MINTIME have passed from the previous |
5072 | * hit, promote the buffer to the MFU state. | |
34dc7c2f | 5073 | */ |
b9541d6b CW |
5074 | if (ddi_time_after(now, hdr->b_l1hdr.b_arc_access + |
5075 | ARC_MINTIME)) { | |
b9541d6b | 5076 | hdr->b_l1hdr.b_arc_access = now; |
2a432414 | 5077 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
c935fe2e | 5078 | arc_change_state(arc_mfu, hdr); |
34dc7c2f | 5079 | } |
b9541d6b | 5080 | } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { |
34dc7c2f BB |
5081 | arc_state_t *new_state; |
5082 | /* | |
c935fe2e AM |
5083 | * This buffer has been accessed once recently, but was |
5084 | * evicted from the cache. Would we have bigger MRU, it | |
5085 | * would be an MRU hit, so handle it the same way, except | |
5086 | * we don't need to check the previous access time. | |
34dc7c2f | 5087 | */ |
c935fe2e AM |
5088 | hdr->b_l1hdr.b_mru_ghost_hits++; |
5089 | ARCSTAT_BUMP(arcstat_mru_ghost_hits); | |
5090 | hdr->b_l1hdr.b_arc_access = now; | |
a8d83e2a AM |
5091 | wmsum_add(&arc_mru_ghost->arcs_hits[arc_buf_type(hdr)], |
5092 | arc_hdr_size(hdr)); | |
c935fe2e | 5093 | if (was_prefetch) { |
34dc7c2f | 5094 | new_state = arc_mru; |
2a432414 | 5095 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); |
34dc7c2f BB |
5096 | } else { |
5097 | new_state = arc_mfu; | |
2a432414 | 5098 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
34dc7c2f | 5099 | } |
c935fe2e | 5100 | arc_change_state(new_state, hdr); |
b9541d6b | 5101 | } else if (hdr->b_l1hdr.b_state == arc_mfu) { |
34dc7c2f | 5102 | /* |
c935fe2e AM |
5103 | * This buffer has been accessed more than once and either |
5104 | * still in the cache or being restored from one of ghosts. | |
34dc7c2f | 5105 | */ |
c935fe2e AM |
5106 | if (!HDR_IO_IN_PROGRESS(hdr)) { |
5107 | hdr->b_l1hdr.b_mfu_hits++; | |
5108 | ARCSTAT_BUMP(arcstat_mfu_hits); | |
5109 | } | |
5110 | hdr->b_l1hdr.b_arc_access = now; | |
b9541d6b | 5111 | } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { |
34dc7c2f | 5112 | /* |
c935fe2e AM |
5113 | * This buffer has been accessed more than once recently, but |
5114 | * has been evicted from the cache. Would we have bigger MFU | |
5115 | * it would stay in cache, so move it back to MFU state. | |
34dc7c2f | 5116 | */ |
cfe8e960 | 5117 | hdr->b_l1hdr.b_mfu_ghost_hits++; |
34dc7c2f | 5118 | ARCSTAT_BUMP(arcstat_mfu_ghost_hits); |
c935fe2e | 5119 | hdr->b_l1hdr.b_arc_access = now; |
a8d83e2a AM |
5120 | wmsum_add(&arc_mfu_ghost->arcs_hits[arc_buf_type(hdr)], |
5121 | arc_hdr_size(hdr)); | |
c935fe2e AM |
5122 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
5123 | arc_change_state(arc_mfu, hdr); | |
ed2f7ba0 AM |
5124 | } else if (hdr->b_l1hdr.b_state == arc_uncached) { |
5125 | /* | |
5126 | * This buffer is uncacheable, but we got a hit. Probably | |
5127 | * a demand read after prefetch. Nothing more to do here. | |
5128 | */ | |
5129 | if (!HDR_IO_IN_PROGRESS(hdr)) | |
5130 | ARCSTAT_BUMP(arcstat_uncached_hits); | |
5131 | hdr->b_l1hdr.b_arc_access = now; | |
b9541d6b | 5132 | } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { |
34dc7c2f | 5133 | /* |
c935fe2e AM |
5134 | * This buffer is on the 2nd Level ARC and was not accessed |
5135 | * for a long time, so treat it as new and put into MRU. | |
34dc7c2f | 5136 | */ |
c935fe2e AM |
5137 | hdr->b_l1hdr.b_arc_access = now; |
5138 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); | |
5139 | arc_change_state(arc_mru, hdr); | |
34dc7c2f | 5140 | } else { |
b9541d6b CW |
5141 | cmn_err(CE_PANIC, "invalid arc state 0x%p", |
5142 | hdr->b_l1hdr.b_state); | |
34dc7c2f BB |
5143 | } |
5144 | } | |
5145 | ||
0873bb63 BB |
5146 | /* |
5147 | * This routine is called by dbuf_hold() to update the arc_access() state | |
5148 | * which otherwise would be skipped for entries in the dbuf cache. | |
5149 | */ | |
5150 | void | |
5151 | arc_buf_access(arc_buf_t *buf) | |
5152 | { | |
0873bb63 BB |
5153 | arc_buf_hdr_t *hdr = buf->b_hdr; |
5154 | ||
5155 | /* | |
5156 | * Avoid taking the hash_lock when possible as an optimization. | |
5157 | * The header must be checked again under the hash_lock in order | |
5158 | * to handle the case where it is concurrently being released. | |
5159 | */ | |
289f7e6a | 5160 | if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) |
0873bb63 | 5161 | return; |
0873bb63 BB |
5162 | |
5163 | kmutex_t *hash_lock = HDR_LOCK(hdr); | |
5164 | mutex_enter(hash_lock); | |
5165 | ||
5166 | if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) { | |
5167 | mutex_exit(hash_lock); | |
0873bb63 BB |
5168 | ARCSTAT_BUMP(arcstat_access_skip); |
5169 | return; | |
5170 | } | |
5171 | ||
0873bb63 | 5172 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || |
ed2f7ba0 AM |
5173 | hdr->b_l1hdr.b_state == arc_mfu || |
5174 | hdr->b_l1hdr.b_state == arc_uncached); | |
0873bb63 BB |
5175 | |
5176 | DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); | |
c935fe2e | 5177 | arc_access(hdr, 0, B_TRUE); |
0873bb63 BB |
5178 | mutex_exit(hash_lock); |
5179 | ||
5180 | ARCSTAT_BUMP(arcstat_hits); | |
c935fe2e AM |
5181 | ARCSTAT_CONDSTAT(B_TRUE /* demand */, demand, prefetch, |
5182 | !HDR_ISTYPE_METADATA(hdr), data, metadata, hits); | |
0873bb63 BB |
5183 | } |
5184 | ||
b5256303 | 5185 | /* a generic arc_read_done_func_t which you can use */ |
34dc7c2f | 5186 | void |
d4a72f23 TC |
5187 | arc_bcopy_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, |
5188 | arc_buf_t *buf, void *arg) | |
34dc7c2f | 5189 | { |
14e4e3cb AZ |
5190 | (void) zio, (void) zb, (void) bp; |
5191 | ||
d4a72f23 TC |
5192 | if (buf == NULL) |
5193 | return; | |
5194 | ||
861166b0 | 5195 | memcpy(arg, buf->b_data, arc_buf_size(buf)); |
d3c2ae1c | 5196 | arc_buf_destroy(buf, arg); |
34dc7c2f BB |
5197 | } |
5198 | ||
b5256303 | 5199 | /* a generic arc_read_done_func_t */ |
34dc7c2f | 5200 | void |
d4a72f23 TC |
5201 | arc_getbuf_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, |
5202 | arc_buf_t *buf, void *arg) | |
34dc7c2f | 5203 | { |
14e4e3cb | 5204 | (void) zb, (void) bp; |
34dc7c2f | 5205 | arc_buf_t **bufp = arg; |
d4a72f23 TC |
5206 | |
5207 | if (buf == NULL) { | |
c3bd3fb4 | 5208 | ASSERT(zio == NULL || zio->io_error != 0); |
34dc7c2f BB |
5209 | *bufp = NULL; |
5210 | } else { | |
c3bd3fb4 | 5211 | ASSERT(zio == NULL || zio->io_error == 0); |
34dc7c2f | 5212 | *bufp = buf; |
c3bd3fb4 | 5213 | ASSERT(buf->b_data != NULL); |
34dc7c2f BB |
5214 | } |
5215 | } | |
5216 | ||
d3c2ae1c GW |
5217 | static void |
5218 | arc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp) | |
5219 | { | |
5220 | if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { | |
5221 | ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0); | |
b5256303 | 5222 | ASSERT3U(arc_hdr_get_compress(hdr), ==, ZIO_COMPRESS_OFF); |
d3c2ae1c GW |
5223 | } else { |
5224 | if (HDR_COMPRESSION_ENABLED(hdr)) { | |
b5256303 | 5225 | ASSERT3U(arc_hdr_get_compress(hdr), ==, |
d3c2ae1c GW |
5226 | BP_GET_COMPRESS(bp)); |
5227 | } | |
5228 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); | |
5229 | ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp)); | |
b5256303 | 5230 | ASSERT3U(!!HDR_PROTECTED(hdr), ==, BP_IS_PROTECTED(bp)); |
d3c2ae1c GW |
5231 | } |
5232 | } | |
5233 | ||
34dc7c2f BB |
5234 | static void |
5235 | arc_read_done(zio_t *zio) | |
5236 | { | |
b5256303 | 5237 | blkptr_t *bp = zio->io_bp; |
d3c2ae1c | 5238 | arc_buf_hdr_t *hdr = zio->io_private; |
9b67f605 | 5239 | kmutex_t *hash_lock = NULL; |
524b4217 DK |
5240 | arc_callback_t *callback_list; |
5241 | arc_callback_t *acb; | |
a7004725 | 5242 | |
34dc7c2f BB |
5243 | /* |
5244 | * The hdr was inserted into hash-table and removed from lists | |
5245 | * prior to starting I/O. We should find this header, since | |
5246 | * it's in the hash table, and it should be legit since it's | |
5247 | * not possible to evict it during the I/O. The only possible | |
5248 | * reason for it not to be found is if we were freed during the | |
5249 | * read. | |
5250 | */ | |
9b67f605 | 5251 | if (HDR_IN_HASH_TABLE(hdr)) { |
31df97cd DB |
5252 | arc_buf_hdr_t *found; |
5253 | ||
493fcce9 | 5254 | ASSERT3U(hdr->b_birth, ==, BP_GET_BIRTH(zio->io_bp)); |
9b67f605 MA |
5255 | ASSERT3U(hdr->b_dva.dva_word[0], ==, |
5256 | BP_IDENTITY(zio->io_bp)->dva_word[0]); | |
5257 | ASSERT3U(hdr->b_dva.dva_word[1], ==, | |
5258 | BP_IDENTITY(zio->io_bp)->dva_word[1]); | |
5259 | ||
31df97cd | 5260 | found = buf_hash_find(hdr->b_spa, zio->io_bp, &hash_lock); |
9b67f605 | 5261 | |
d3c2ae1c | 5262 | ASSERT((found == hdr && |
9b67f605 MA |
5263 | DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || |
5264 | (found == hdr && HDR_L2_READING(hdr))); | |
d3c2ae1c GW |
5265 | ASSERT3P(hash_lock, !=, NULL); |
5266 | } | |
5267 | ||
b5256303 TC |
5268 | if (BP_IS_PROTECTED(bp)) { |
5269 | hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp); | |
5270 | hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset; | |
5271 | zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt, | |
5272 | hdr->b_crypt_hdr.b_iv); | |
5273 | ||
df42e20a RE |
5274 | if (zio->io_error == 0) { |
5275 | if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) { | |
5276 | void *tmpbuf; | |
5277 | ||
5278 | tmpbuf = abd_borrow_buf_copy(zio->io_abd, | |
5279 | sizeof (zil_chain_t)); | |
5280 | zio_crypt_decode_mac_zil(tmpbuf, | |
5281 | hdr->b_crypt_hdr.b_mac); | |
5282 | abd_return_buf(zio->io_abd, tmpbuf, | |
5283 | sizeof (zil_chain_t)); | |
5284 | } else { | |
5285 | zio_crypt_decode_mac_bp(bp, | |
5286 | hdr->b_crypt_hdr.b_mac); | |
5287 | } | |
b5256303 TC |
5288 | } |
5289 | } | |
5290 | ||
d4a72f23 | 5291 | if (zio->io_error == 0) { |
d3c2ae1c GW |
5292 | /* byteswap if necessary */ |
5293 | if (BP_SHOULD_BYTESWAP(zio->io_bp)) { | |
5294 | if (BP_GET_LEVEL(zio->io_bp) > 0) { | |
5295 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; | |
5296 | } else { | |
5297 | hdr->b_l1hdr.b_byteswap = | |
5298 | DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); | |
5299 | } | |
5300 | } else { | |
5301 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
5302 | } | |
10b3c7f5 MN |
5303 | if (!HDR_L2_READING(hdr)) { |
5304 | hdr->b_complevel = zio->io_prop.zp_complevel; | |
5305 | } | |
9b67f605 | 5306 | } |
34dc7c2f | 5307 | |
d3c2ae1c | 5308 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED); |
c6f5e9d9 GA |
5309 | if (l2arc_noprefetch && HDR_PREFETCH(hdr)) |
5310 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE); | |
34dc7c2f | 5311 | |
b9541d6b | 5312 | callback_list = hdr->b_l1hdr.b_acb; |
d3c2ae1c | 5313 | ASSERT3P(callback_list, !=, NULL); |
c935fe2e | 5314 | hdr->b_l1hdr.b_acb = NULL; |
428870ff | 5315 | |
524b4217 DK |
5316 | /* |
5317 | * If a read request has a callback (i.e. acb_done is not NULL), then we | |
5318 | * make a buf containing the data according to the parameters which were | |
5319 | * passed in. The implementation of arc_buf_alloc_impl() ensures that we | |
5320 | * aren't needlessly decompressing the data multiple times. | |
5321 | */ | |
a7004725 | 5322 | int callback_cnt = 0; |
2aa34383 | 5323 | for (acb = callback_list; acb != NULL; acb = acb->acb_next) { |
c935fe2e AM |
5324 | |
5325 | /* We need the last one to call below in original order. */ | |
5326 | callback_list = acb; | |
5327 | ||
923d7303 | 5328 | if (!acb->acb_done || acb->acb_nobuf) |
2aa34383 DK |
5329 | continue; |
5330 | ||
2aa34383 | 5331 | callback_cnt++; |
524b4217 | 5332 | |
d4a72f23 TC |
5333 | if (zio->io_error != 0) |
5334 | continue; | |
5335 | ||
b5256303 | 5336 | int error = arc_buf_alloc_impl(hdr, zio->io_spa, |
be9a5c35 | 5337 | &acb->acb_zb, acb->acb_private, acb->acb_encrypted, |
d4a72f23 | 5338 | acb->acb_compressed, acb->acb_noauth, B_TRUE, |
440a3eb9 | 5339 | &acb->acb_buf); |
b5256303 TC |
5340 | |
5341 | /* | |
440a3eb9 | 5342 | * Assert non-speculative zios didn't fail because an |
b5256303 TC |
5343 | * encryption key wasn't loaded |
5344 | */ | |
a2c2ed1b | 5345 | ASSERT((zio->io_flags & ZIO_FLAG_SPECULATIVE) || |
be9a5c35 | 5346 | error != EACCES); |
b5256303 TC |
5347 | |
5348 | /* | |
5349 | * If we failed to decrypt, report an error now (as the zio | |
5350 | * layer would have done if it had done the transforms). | |
5351 | */ | |
5352 | if (error == ECKSUM) { | |
5353 | ASSERT(BP_IS_PROTECTED(bp)); | |
5354 | error = SET_ERROR(EIO); | |
b5256303 | 5355 | if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) { |
431083f7 | 5356 | spa_log_error(zio->io_spa, &acb->acb_zb, |
493fcce9 | 5357 | BP_GET_LOGICAL_BIRTH(zio->io_bp)); |
1144586b TS |
5358 | (void) zfs_ereport_post( |
5359 | FM_EREPORT_ZFS_AUTHENTICATION, | |
4f072827 | 5360 | zio->io_spa, NULL, &acb->acb_zb, zio, 0); |
b5256303 TC |
5361 | } |
5362 | } | |
5363 | ||
c3bd3fb4 TC |
5364 | if (error != 0) { |
5365 | /* | |
5366 | * Decompression or decryption failed. Set | |
5367 | * io_error so that when we call acb_done | |
5368 | * (below), we will indicate that the read | |
5369 | * failed. Note that in the unusual case | |
5370 | * where one callback is compressed and another | |
5371 | * uncompressed, we will mark all of them | |
5372 | * as failed, even though the uncompressed | |
5373 | * one can't actually fail. In this case, | |
5374 | * the hdr will not be anonymous, because | |
5375 | * if there are multiple callbacks, it's | |
5376 | * because multiple threads found the same | |
5377 | * arc buf in the hash table. | |
5378 | */ | |
524b4217 | 5379 | zio->io_error = error; |
c3bd3fb4 | 5380 | } |
34dc7c2f | 5381 | } |
c3bd3fb4 TC |
5382 | |
5383 | /* | |
5384 | * If there are multiple callbacks, we must have the hash lock, | |
5385 | * because the only way for multiple threads to find this hdr is | |
5386 | * in the hash table. This ensures that if there are multiple | |
5387 | * callbacks, the hdr is not anonymous. If it were anonymous, | |
5388 | * we couldn't use arc_buf_destroy() in the error case below. | |
5389 | */ | |
5390 | ASSERT(callback_cnt < 2 || hash_lock != NULL); | |
5391 | ||
d4a72f23 | 5392 | if (zio->io_error == 0) { |
d3c2ae1c GW |
5393 | arc_hdr_verify(hdr, zio->io_bp); |
5394 | } else { | |
5395 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); | |
b9541d6b | 5396 | if (hdr->b_l1hdr.b_state != arc_anon) |
c935fe2e | 5397 | arc_change_state(arc_anon, hdr); |
34dc7c2f BB |
5398 | if (HDR_IN_HASH_TABLE(hdr)) |
5399 | buf_hash_remove(hdr); | |
34dc7c2f BB |
5400 | } |
5401 | ||
ed2f7ba0 AM |
5402 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
5403 | (void) remove_reference(hdr, hdr); | |
5404 | ||
5405 | if (hash_lock != NULL) | |
34dc7c2f | 5406 | mutex_exit(hash_lock); |
34dc7c2f BB |
5407 | |
5408 | /* execute each callback and free its structure */ | |
5409 | while ((acb = callback_list) != NULL) { | |
c3bd3fb4 TC |
5410 | if (acb->acb_done != NULL) { |
5411 | if (zio->io_error != 0 && acb->acb_buf != NULL) { | |
5412 | /* | |
5413 | * If arc_buf_alloc_impl() fails during | |
5414 | * decompression, the buf will still be | |
5415 | * allocated, and needs to be freed here. | |
5416 | */ | |
5417 | arc_buf_destroy(acb->acb_buf, | |
5418 | acb->acb_private); | |
5419 | acb->acb_buf = NULL; | |
5420 | } | |
d4a72f23 TC |
5421 | acb->acb_done(zio, &zio->io_bookmark, zio->io_bp, |
5422 | acb->acb_buf, acb->acb_private); | |
b5256303 | 5423 | } |
34dc7c2f BB |
5424 | |
5425 | if (acb->acb_zio_dummy != NULL) { | |
5426 | acb->acb_zio_dummy->io_error = zio->io_error; | |
5427 | zio_nowait(acb->acb_zio_dummy); | |
5428 | } | |
5429 | ||
c935fe2e AM |
5430 | callback_list = acb->acb_prev; |
5431 | if (acb->acb_wait) { | |
5432 | mutex_enter(&acb->acb_wait_lock); | |
5433 | acb->acb_wait_error = zio->io_error; | |
5434 | acb->acb_wait = B_FALSE; | |
5435 | cv_signal(&acb->acb_wait_cv); | |
5436 | mutex_exit(&acb->acb_wait_lock); | |
5437 | /* acb will be freed by the waiting thread. */ | |
5438 | } else { | |
5439 | kmem_free(acb, sizeof (arc_callback_t)); | |
5440 | } | |
34dc7c2f | 5441 | } |
34dc7c2f BB |
5442 | } |
5443 | ||
5444 | /* | |
5c839890 | 5445 | * "Read" the block at the specified DVA (in bp) via the |
34dc7c2f BB |
5446 | * cache. If the block is found in the cache, invoke the provided |
5447 | * callback immediately and return. Note that the `zio' parameter | |
5448 | * in the callback will be NULL in this case, since no IO was | |
5449 | * required. If the block is not in the cache pass the read request | |
5450 | * on to the spa with a substitute callback function, so that the | |
5451 | * requested block will be added to the cache. | |
5452 | * | |
5453 | * If a read request arrives for a block that has a read in-progress, | |
5454 | * either wait for the in-progress read to complete (and return the | |
5455 | * results); or, if this is a read with a "done" func, add a record | |
5456 | * to the read to invoke the "done" func when the read completes, | |
5457 | * and return; or just return. | |
5458 | * | |
5459 | * arc_read_done() will invoke all the requested "done" functions | |
5460 | * for readers of this block. | |
5461 | */ | |
5462 | int | |
b5256303 TC |
5463 | arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, |
5464 | arc_read_done_func_t *done, void *private, zio_priority_t priority, | |
5465 | int zio_flags, arc_flags_t *arc_flags, const zbookmark_phys_t *zb) | |
34dc7c2f | 5466 | { |
9b67f605 | 5467 | arc_buf_hdr_t *hdr = NULL; |
9b67f605 | 5468 | kmutex_t *hash_lock = NULL; |
34dc7c2f | 5469 | zio_t *rzio; |
3541dc6d | 5470 | uint64_t guid = spa_load_guid(spa); |
b5256303 TC |
5471 | boolean_t compressed_read = (zio_flags & ZIO_FLAG_RAW_COMPRESS) != 0; |
5472 | boolean_t encrypted_read = BP_IS_ENCRYPTED(bp) && | |
5473 | (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0; | |
5474 | boolean_t noauth_read = BP_IS_AUTHENTICATED(bp) && | |
5475 | (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0; | |
0902c457 | 5476 | boolean_t embedded_bp = !!BP_IS_EMBEDDED(bp); |
1e4732cb | 5477 | boolean_t no_buf = *arc_flags & ARC_FLAG_NO_BUF; |
87a4dfa5 | 5478 | arc_buf_t *buf = NULL; |
1421c891 | 5479 | int rc = 0; |
34dc7c2f | 5480 | |
0902c457 | 5481 | ASSERT(!embedded_bp || |
9b67f605 | 5482 | BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); |
30af21b0 PD |
5483 | ASSERT(!BP_IS_HOLE(bp)); |
5484 | ASSERT(!BP_IS_REDACTED(bp)); | |
9b67f605 | 5485 | |
1e9231ad MR |
5486 | /* |
5487 | * Normally SPL_FSTRANS will already be set since kernel threads which | |
5488 | * expect to call the DMU interfaces will set it when created. System | |
5489 | * calls are similarly handled by setting/cleaning the bit in the | |
5490 | * registered callback (module/os/.../zfs/zpl_*). | |
5491 | * | |
5492 | * External consumers such as Lustre which call the exported DMU | |
5493 | * interfaces may not have set SPL_FSTRANS. To avoid a deadlock | |
5494 | * on the hash_lock always set and clear the bit. | |
5495 | */ | |
5496 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
34dc7c2f | 5497 | top: |
b9ec4a15 BB |
5498 | /* |
5499 | * Verify the block pointer contents are reasonable. This should | |
5500 | * always be the case since the blkptr is protected by a checksum. | |
5501 | * However, if there is damage it's desirable to detect this early | |
5502 | * and treat it as a checksum error. This allows an alternate blkptr | |
5503 | * to be tried when one is available (e.g. ditto blocks). | |
5504 | */ | |
3095ca91 MA |
5505 | if (!zfs_blkptr_verify(spa, bp, (zio_flags & ZIO_FLAG_CONFIG_WRITER) ? |
5506 | BLK_CONFIG_HELD : BLK_CONFIG_NEEDED, BLK_VERIFY_LOG)) { | |
b9ec4a15 | 5507 | rc = SET_ERROR(ECKSUM); |
87a4dfa5 | 5508 | goto done; |
b9ec4a15 BB |
5509 | } |
5510 | ||
0902c457 | 5511 | if (!embedded_bp) { |
9b67f605 MA |
5512 | /* |
5513 | * Embedded BP's have no DVA and require no I/O to "read". | |
5514 | * Create an anonymous arc buf to back it. | |
5515 | */ | |
5516 | hdr = buf_hash_find(guid, bp, &hash_lock); | |
5517 | } | |
5518 | ||
b5256303 TC |
5519 | /* |
5520 | * Determine if we have an L1 cache hit or a cache miss. For simplicity | |
e1cfd73f | 5521 | * we maintain encrypted data separately from compressed / uncompressed |
b5256303 TC |
5522 | * data. If the user is requesting raw encrypted data and we don't have |
5523 | * that in the header we will read from disk to guarantee that we can | |
5524 | * get it even if the encryption keys aren't loaded. | |
5525 | */ | |
5526 | if (hdr != NULL && HDR_HAS_L1HDR(hdr) && (HDR_HAS_RABD(hdr) || | |
5527 | (hdr->b_l1hdr.b_pabd != NULL && !encrypted_read))) { | |
c935fe2e | 5528 | boolean_t is_data = !HDR_ISTYPE_METADATA(hdr); |
34dc7c2f BB |
5529 | |
5530 | if (HDR_IO_IN_PROGRESS(hdr)) { | |
1dc32a67 MA |
5531 | if (*arc_flags & ARC_FLAG_CACHED_ONLY) { |
5532 | mutex_exit(hash_lock); | |
5533 | ARCSTAT_BUMP(arcstat_cached_only_in_progress); | |
5534 | rc = SET_ERROR(ENOENT); | |
87a4dfa5 | 5535 | goto done; |
1dc32a67 MA |
5536 | } |
5537 | ||
c935fe2e | 5538 | zio_t *head_zio = hdr->b_l1hdr.b_acb->acb_zio_head; |
a8b2e306 | 5539 | ASSERT3P(head_zio, !=, NULL); |
7f60329a MA |
5540 | if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) && |
5541 | priority == ZIO_PRIORITY_SYNC_READ) { | |
5542 | /* | |
a8b2e306 TC |
5543 | * This is a sync read that needs to wait for |
5544 | * an in-flight async read. Request that the | |
5545 | * zio have its priority upgraded. | |
7f60329a | 5546 | */ |
a8b2e306 TC |
5547 | zio_change_priority(head_zio, priority); |
5548 | DTRACE_PROBE1(arc__async__upgrade__sync, | |
7f60329a | 5549 | arc_buf_hdr_t *, hdr); |
a8b2e306 | 5550 | ARCSTAT_BUMP(arcstat_async_upgrade_sync); |
7f60329a | 5551 | } |
c935fe2e AM |
5552 | |
5553 | DTRACE_PROBE1(arc__iohit, arc_buf_hdr_t *, hdr); | |
5554 | arc_access(hdr, *arc_flags, B_FALSE); | |
7f60329a | 5555 | |
91983265 CS |
5556 | /* |
5557 | * If there are multiple threads reading the same block | |
5558 | * and that block is not yet in the ARC, then only one | |
5559 | * thread will do the physical I/O and all other | |
5560 | * threads will wait until that I/O completes. | |
c935fe2e AM |
5561 | * Synchronous reads use the acb_wait_cv whereas nowait |
5562 | * reads register a callback. Both are signalled/called | |
5563 | * in arc_read_done. | |
91983265 | 5564 | * |
c935fe2e AM |
5565 | * Errors of the physical I/O may need to be propagated. |
5566 | * Synchronous read errors are returned here from | |
5567 | * arc_read_done via acb_wait_error. Nowait reads | |
91983265 CS |
5568 | * attach the acb_zio_dummy zio to pio and |
5569 | * arc_read_done propagates the physical I/O's io_error | |
5570 | * to acb_zio_dummy, and thereby to pio. | |
5571 | */ | |
c935fe2e AM |
5572 | arc_callback_t *acb = NULL; |
5573 | if (done || pio || *arc_flags & ARC_FLAG_WAIT) { | |
34dc7c2f | 5574 | acb = kmem_zalloc(sizeof (arc_callback_t), |
79c76d5b | 5575 | KM_SLEEP); |
34dc7c2f BB |
5576 | acb->acb_done = done; |
5577 | acb->acb_private = private; | |
a7004725 | 5578 | acb->acb_compressed = compressed_read; |
440a3eb9 TC |
5579 | acb->acb_encrypted = encrypted_read; |
5580 | acb->acb_noauth = noauth_read; | |
923d7303 | 5581 | acb->acb_nobuf = no_buf; |
c935fe2e AM |
5582 | if (*arc_flags & ARC_FLAG_WAIT) { |
5583 | acb->acb_wait = B_TRUE; | |
5584 | mutex_init(&acb->acb_wait_lock, NULL, | |
5585 | MUTEX_DEFAULT, NULL); | |
5586 | cv_init(&acb->acb_wait_cv, NULL, | |
5587 | CV_DEFAULT, NULL); | |
5588 | } | |
be9a5c35 | 5589 | acb->acb_zb = *zb; |
c935fe2e | 5590 | if (pio != NULL) { |
34dc7c2f | 5591 | acb->acb_zio_dummy = zio_null(pio, |
d164b209 | 5592 | spa, NULL, NULL, NULL, zio_flags); |
c935fe2e | 5593 | } |
a8b2e306 | 5594 | acb->acb_zio_head = head_zio; |
b9541d6b | 5595 | acb->acb_next = hdr->b_l1hdr.b_acb; |
2a6c6210 | 5596 | hdr->b_l1hdr.b_acb->acb_prev = acb; |
b9541d6b | 5597 | hdr->b_l1hdr.b_acb = acb; |
34dc7c2f BB |
5598 | } |
5599 | mutex_exit(hash_lock); | |
c935fe2e AM |
5600 | |
5601 | ARCSTAT_BUMP(arcstat_iohits); | |
5602 | ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH), | |
5603 | demand, prefetch, is_data, data, metadata, iohits); | |
5604 | ||
5605 | if (*arc_flags & ARC_FLAG_WAIT) { | |
5606 | mutex_enter(&acb->acb_wait_lock); | |
5607 | while (acb->acb_wait) { | |
5608 | cv_wait(&acb->acb_wait_cv, | |
5609 | &acb->acb_wait_lock); | |
5610 | } | |
5611 | rc = acb->acb_wait_error; | |
5612 | mutex_exit(&acb->acb_wait_lock); | |
5613 | mutex_destroy(&acb->acb_wait_lock); | |
5614 | cv_destroy(&acb->acb_wait_cv); | |
5615 | kmem_free(acb, sizeof (arc_callback_t)); | |
5616 | } | |
1421c891 | 5617 | goto out; |
34dc7c2f BB |
5618 | } |
5619 | ||
b9541d6b | 5620 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || |
ed2f7ba0 AM |
5621 | hdr->b_l1hdr.b_state == arc_mfu || |
5622 | hdr->b_l1hdr.b_state == arc_uncached); | |
34dc7c2f | 5623 | |
c935fe2e AM |
5624 | DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); |
5625 | arc_access(hdr, *arc_flags, B_TRUE); | |
d4a72f23 | 5626 | |
c935fe2e | 5627 | if (done && !no_buf) { |
0902c457 | 5628 | ASSERT(!embedded_bp || !BP_IS_HOLE(bp)); |
d3c2ae1c | 5629 | |
524b4217 | 5630 | /* Get a buf with the desired data in it. */ |
be9a5c35 TC |
5631 | rc = arc_buf_alloc_impl(hdr, spa, zb, private, |
5632 | encrypted_read, compressed_read, noauth_read, | |
5633 | B_TRUE, &buf); | |
a2c2ed1b TC |
5634 | if (rc == ECKSUM) { |
5635 | /* | |
5636 | * Convert authentication and decryption errors | |
be9a5c35 TC |
5637 | * to EIO (and generate an ereport if needed) |
5638 | * before leaving the ARC. | |
a2c2ed1b TC |
5639 | */ |
5640 | rc = SET_ERROR(EIO); | |
be9a5c35 | 5641 | if ((zio_flags & ZIO_FLAG_SPECULATIVE) == 0) { |
493fcce9 | 5642 | spa_log_error(spa, zb, hdr->b_birth); |
1144586b | 5643 | (void) zfs_ereport_post( |
be9a5c35 | 5644 | FM_EREPORT_ZFS_AUTHENTICATION, |
4f072827 | 5645 | spa, NULL, zb, NULL, 0); |
be9a5c35 | 5646 | } |
a2c2ed1b | 5647 | } |
d4a72f23 | 5648 | if (rc != 0) { |
2c24b5b1 | 5649 | arc_buf_destroy_impl(buf); |
d4a72f23 | 5650 | buf = NULL; |
ed2f7ba0 | 5651 | (void) remove_reference(hdr, private); |
d4a72f23 TC |
5652 | } |
5653 | ||
a2c2ed1b TC |
5654 | /* assert any errors weren't due to unloaded keys */ |
5655 | ASSERT((zio_flags & ZIO_FLAG_SPECULATIVE) || | |
be9a5c35 | 5656 | rc != EACCES); |
34dc7c2f | 5657 | } |
34dc7c2f BB |
5658 | mutex_exit(hash_lock); |
5659 | ARCSTAT_BUMP(arcstat_hits); | |
c935fe2e AM |
5660 | ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH), |
5661 | demand, prefetch, is_data, data, metadata, hits); | |
5662 | *arc_flags |= ARC_FLAG_CACHED; | |
87a4dfa5 | 5663 | goto done; |
34dc7c2f | 5664 | } else { |
d3c2ae1c GW |
5665 | uint64_t lsize = BP_GET_LSIZE(bp); |
5666 | uint64_t psize = BP_GET_PSIZE(bp); | |
9b67f605 | 5667 | arc_callback_t *acb; |
b128c09f | 5668 | vdev_t *vd = NULL; |
a117a6d6 | 5669 | uint64_t addr = 0; |
d164b209 | 5670 | boolean_t devw = B_FALSE; |
d3c2ae1c | 5671 | uint64_t size; |
440a3eb9 | 5672 | abd_t *hdr_abd; |
e111c802 | 5673 | int alloc_flags = encrypted_read ? ARC_HDR_ALLOC_RDATA : 0; |
a8d83e2a | 5674 | arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); |
34dc7c2f | 5675 | |
1dc32a67 | 5676 | if (*arc_flags & ARC_FLAG_CACHED_ONLY) { |
1dc32a67 MA |
5677 | if (hash_lock != NULL) |
5678 | mutex_exit(hash_lock); | |
87a4dfa5 AM |
5679 | rc = SET_ERROR(ENOENT); |
5680 | goto done; | |
1dc32a67 MA |
5681 | } |
5682 | ||
34dc7c2f | 5683 | if (hdr == NULL) { |
0902c457 TC |
5684 | /* |
5685 | * This block is not in the cache or it has | |
5686 | * embedded data. | |
5687 | */ | |
9b67f605 | 5688 | arc_buf_hdr_t *exists = NULL; |
493fcce9 | 5689 | hdr = arc_hdr_alloc(guid, psize, lsize, |
6b88b4b5 | 5690 | BP_IS_PROTECTED(bp), BP_GET_COMPRESS(bp), 0, type); |
d3c2ae1c | 5691 | |
0902c457 | 5692 | if (!embedded_bp) { |
9b67f605 | 5693 | hdr->b_dva = *BP_IDENTITY(bp); |
493fcce9 | 5694 | hdr->b_birth = BP_GET_BIRTH(bp); |
9b67f605 MA |
5695 | exists = buf_hash_insert(hdr, &hash_lock); |
5696 | } | |
5697 | if (exists != NULL) { | |
34dc7c2f BB |
5698 | /* somebody beat us to the hash insert */ |
5699 | mutex_exit(hash_lock); | |
428870ff | 5700 | buf_discard_identity(hdr); |
d3c2ae1c | 5701 | arc_hdr_destroy(hdr); |
34dc7c2f BB |
5702 | goto top; /* restart the IO request */ |
5703 | } | |
34dc7c2f | 5704 | } else { |
b9541d6b | 5705 | /* |
b5256303 TC |
5706 | * This block is in the ghost cache or encrypted data |
5707 | * was requested and we didn't have it. If it was | |
5708 | * L2-only (and thus didn't have an L1 hdr), | |
5709 | * we realloc the header to add an L1 hdr. | |
b9541d6b CW |
5710 | */ |
5711 | if (!HDR_HAS_L1HDR(hdr)) { | |
5712 | hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, | |
5713 | hdr_full_cache); | |
5714 | } | |
5715 | ||
b5256303 TC |
5716 | if (GHOST_STATE(hdr->b_l1hdr.b_state)) { |
5717 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); | |
5718 | ASSERT(!HDR_HAS_RABD(hdr)); | |
5719 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
424fd7c3 TS |
5720 | ASSERT0(zfs_refcount_count( |
5721 | &hdr->b_l1hdr.b_refcnt)); | |
b5256303 | 5722 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
bacf366f | 5723 | #ifdef ZFS_DEBUG |
b5256303 | 5724 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); |
bacf366f | 5725 | #endif |
b5256303 TC |
5726 | } else if (HDR_IO_IN_PROGRESS(hdr)) { |
5727 | /* | |
5728 | * If this header already had an IO in progress | |
5729 | * and we are performing another IO to fetch | |
5730 | * encrypted data we must wait until the first | |
5731 | * IO completes so as not to confuse | |
5732 | * arc_read_done(). This should be very rare | |
5733 | * and so the performance impact shouldn't | |
5734 | * matter. | |
5735 | */ | |
2a6c6210 AM |
5736 | arc_callback_t *acb = kmem_zalloc( |
5737 | sizeof (arc_callback_t), KM_SLEEP); | |
5738 | acb->acb_wait = B_TRUE; | |
5739 | mutex_init(&acb->acb_wait_lock, NULL, | |
5740 | MUTEX_DEFAULT, NULL); | |
5741 | cv_init(&acb->acb_wait_cv, NULL, CV_DEFAULT, | |
5742 | NULL); | |
5743 | acb->acb_zio_head = | |
5744 | hdr->b_l1hdr.b_acb->acb_zio_head; | |
5745 | acb->acb_next = hdr->b_l1hdr.b_acb; | |
5746 | hdr->b_l1hdr.b_acb->acb_prev = acb; | |
5747 | hdr->b_l1hdr.b_acb = acb; | |
b5256303 | 5748 | mutex_exit(hash_lock); |
2a6c6210 AM |
5749 | mutex_enter(&acb->acb_wait_lock); |
5750 | while (acb->acb_wait) { | |
5751 | cv_wait(&acb->acb_wait_cv, | |
5752 | &acb->acb_wait_lock); | |
5753 | } | |
5754 | mutex_exit(&acb->acb_wait_lock); | |
5755 | mutex_destroy(&acb->acb_wait_lock); | |
5756 | cv_destroy(&acb->acb_wait_cv); | |
5757 | kmem_free(acb, sizeof (arc_callback_t)); | |
b5256303 TC |
5758 | goto top; |
5759 | } | |
d3c2ae1c | 5760 | } |
ed2f7ba0 AM |
5761 | if (*arc_flags & ARC_FLAG_UNCACHED) { |
5762 | arc_hdr_set_flags(hdr, ARC_FLAG_UNCACHED); | |
5763 | if (!encrypted_read) | |
5764 | alloc_flags |= ARC_HDR_ALLOC_LINEAR; | |
5765 | } | |
d3c2ae1c | 5766 | |
c935fe2e AM |
5767 | /* |
5768 | * Take additional reference for IO_IN_PROGRESS. It stops | |
5769 | * arc_access() from putting this header without any buffers | |
5770 | * and so other references but obviously nonevictable onto | |
5771 | * the evictable list of MRU or MFU state. | |
5772 | */ | |
5773 | add_reference(hdr, hdr); | |
5774 | if (!embedded_bp) | |
5775 | arc_access(hdr, *arc_flags, B_FALSE); | |
5776 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); | |
6b88b4b5 | 5777 | arc_hdr_alloc_abd(hdr, alloc_flags); |
b5256303 TC |
5778 | if (encrypted_read) { |
5779 | ASSERT(HDR_HAS_RABD(hdr)); | |
5780 | size = HDR_GET_PSIZE(hdr); | |
5781 | hdr_abd = hdr->b_crypt_hdr.b_rabd; | |
d3c2ae1c | 5782 | zio_flags |= ZIO_FLAG_RAW; |
b5256303 TC |
5783 | } else { |
5784 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
5785 | size = arc_hdr_size(hdr); | |
5786 | hdr_abd = hdr->b_l1hdr.b_pabd; | |
5787 | ||
5788 | if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) { | |
5789 | zio_flags |= ZIO_FLAG_RAW_COMPRESS; | |
5790 | } | |
5791 | ||
5792 | /* | |
5793 | * For authenticated bp's, we do not ask the ZIO layer | |
5794 | * to authenticate them since this will cause the entire | |
5795 | * IO to fail if the key isn't loaded. Instead, we | |
5796 | * defer authentication until arc_buf_fill(), which will | |
5797 | * verify the data when the key is available. | |
5798 | */ | |
5799 | if (BP_IS_AUTHENTICATED(bp)) | |
5800 | zio_flags |= ZIO_FLAG_RAW_ENCRYPT; | |
34dc7c2f BB |
5801 | } |
5802 | ||
b5256303 TC |
5803 | if (BP_IS_AUTHENTICATED(bp)) |
5804 | arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH); | |
d3c2ae1c GW |
5805 | if (BP_GET_LEVEL(bp) > 0) |
5806 | arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT); | |
b9541d6b | 5807 | ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); |
428870ff | 5808 | |
79c76d5b | 5809 | acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); |
34dc7c2f BB |
5810 | acb->acb_done = done; |
5811 | acb->acb_private = private; | |
2aa34383 | 5812 | acb->acb_compressed = compressed_read; |
b5256303 TC |
5813 | acb->acb_encrypted = encrypted_read; |
5814 | acb->acb_noauth = noauth_read; | |
be9a5c35 | 5815 | acb->acb_zb = *zb; |
34dc7c2f | 5816 | |
d3c2ae1c | 5817 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
b9541d6b | 5818 | hdr->b_l1hdr.b_acb = acb; |
34dc7c2f | 5819 | |
b9541d6b CW |
5820 | if (HDR_HAS_L2HDR(hdr) && |
5821 | (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { | |
5822 | devw = hdr->b_l2hdr.b_dev->l2ad_writing; | |
5823 | addr = hdr->b_l2hdr.b_daddr; | |
b128c09f | 5824 | /* |
a1d477c2 | 5825 | * Lock out L2ARC device removal. |
b128c09f BB |
5826 | */ |
5827 | if (vdev_is_dead(vd) || | |
5828 | !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) | |
5829 | vd = NULL; | |
5830 | } | |
5831 | ||
a8b2e306 TC |
5832 | /* |
5833 | * We count both async reads and scrub IOs as asynchronous so | |
5834 | * that both can be upgraded in the event of a cache hit while | |
5835 | * the read IO is still in-flight. | |
5836 | */ | |
5837 | if (priority == ZIO_PRIORITY_ASYNC_READ || | |
5838 | priority == ZIO_PRIORITY_SCRUB) | |
d3c2ae1c GW |
5839 | arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); |
5840 | else | |
5841 | arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); | |
5842 | ||
e49f1e20 | 5843 | /* |
0902c457 TC |
5844 | * At this point, we have a level 1 cache miss or a blkptr |
5845 | * with embedded data. Try again in L2ARC if possible. | |
e49f1e20 | 5846 | */ |
d3c2ae1c GW |
5847 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize); |
5848 | ||
0902c457 TC |
5849 | /* |
5850 | * Skip ARC stat bump for block pointers with embedded | |
5851 | * data. The data are read from the blkptr itself via | |
5852 | * decode_embedded_bp_compressed(). | |
5853 | */ | |
5854 | if (!embedded_bp) { | |
5855 | DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, | |
5856 | blkptr_t *, bp, uint64_t, lsize, | |
5857 | zbookmark_phys_t *, zb); | |
5858 | ARCSTAT_BUMP(arcstat_misses); | |
c935fe2e | 5859 | ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH), |
0902c457 TC |
5860 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data, |
5861 | metadata, misses); | |
64e0fe14 | 5862 | zfs_racct_read(size, 1); |
0902c457 | 5863 | } |
34dc7c2f | 5864 | |
666aa69f AM |
5865 | /* Check if the spa even has l2 configured */ |
5866 | const boolean_t spa_has_l2 = l2arc_ndev != 0 && | |
5867 | spa->spa_l2cache.sav_count > 0; | |
5868 | ||
5869 | if (vd != NULL && spa_has_l2 && !(l2arc_norw && devw)) { | |
34dc7c2f BB |
5870 | /* |
5871 | * Read from the L2ARC if the following are true: | |
b128c09f BB |
5872 | * 1. The L2ARC vdev was previously cached. |
5873 | * 2. This buffer still has L2ARC metadata. | |
5874 | * 3. This buffer isn't currently writing to the L2ARC. | |
5875 | * 4. The L2ARC entry wasn't evicted, which may | |
5876 | * also have invalidated the vdev. | |
34dc7c2f | 5877 | */ |
b9541d6b | 5878 | if (HDR_HAS_L2HDR(hdr) && |
043c6ee3 | 5879 | !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr)) { |
34dc7c2f | 5880 | l2arc_read_callback_t *cb; |
82710e99 GDN |
5881 | abd_t *abd; |
5882 | uint64_t asize; | |
34dc7c2f BB |
5883 | |
5884 | DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); | |
5885 | ARCSTAT_BUMP(arcstat_l2_hits); | |
cfe8e960 | 5886 | hdr->b_l2hdr.b_hits++; |
34dc7c2f | 5887 | |
34dc7c2f | 5888 | cb = kmem_zalloc(sizeof (l2arc_read_callback_t), |
79c76d5b | 5889 | KM_SLEEP); |
d3c2ae1c | 5890 | cb->l2rcb_hdr = hdr; |
34dc7c2f BB |
5891 | cb->l2rcb_bp = *bp; |
5892 | cb->l2rcb_zb = *zb; | |
b128c09f | 5893 | cb->l2rcb_flags = zio_flags; |
34dc7c2f | 5894 | |
fc34dfba AJ |
5895 | /* |
5896 | * When Compressed ARC is disabled, but the | |
5897 | * L2ARC block is compressed, arc_hdr_size() | |
5898 | * will have returned LSIZE rather than PSIZE. | |
5899 | */ | |
5900 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
5901 | !HDR_COMPRESSION_ENABLED(hdr) && | |
5902 | HDR_GET_PSIZE(hdr) != 0) { | |
5903 | size = HDR_GET_PSIZE(hdr); | |
5904 | } | |
5905 | ||
82710e99 GDN |
5906 | asize = vdev_psize_to_asize(vd, size); |
5907 | if (asize != size) { | |
5908 | abd = abd_alloc_for_io(asize, | |
5909 | HDR_ISTYPE_METADATA(hdr)); | |
5910 | cb->l2rcb_abd = abd; | |
5911 | } else { | |
b5256303 | 5912 | abd = hdr_abd; |
82710e99 GDN |
5913 | } |
5914 | ||
a117a6d6 | 5915 | ASSERT(addr >= VDEV_LABEL_START_SIZE && |
82710e99 | 5916 | addr + asize <= vd->vdev_psize - |
a117a6d6 GW |
5917 | VDEV_LABEL_END_SIZE); |
5918 | ||
34dc7c2f | 5919 | /* |
b128c09f BB |
5920 | * l2arc read. The SCL_L2ARC lock will be |
5921 | * released by l2arc_read_done(). | |
3a17a7a9 SK |
5922 | * Issue a null zio if the underlying buffer |
5923 | * was squashed to zero size by compression. | |
34dc7c2f | 5924 | */ |
b5256303 | 5925 | ASSERT3U(arc_hdr_get_compress(hdr), !=, |
d3c2ae1c GW |
5926 | ZIO_COMPRESS_EMPTY); |
5927 | rzio = zio_read_phys(pio, vd, addr, | |
82710e99 | 5928 | asize, abd, |
d3c2ae1c GW |
5929 | ZIO_CHECKSUM_OFF, |
5930 | l2arc_read_done, cb, priority, | |
70ea484e | 5931 | zio_flags | ZIO_FLAG_CANFAIL | |
d3c2ae1c GW |
5932 | ZIO_FLAG_DONT_PROPAGATE | |
5933 | ZIO_FLAG_DONT_RETRY, B_FALSE); | |
a8b2e306 TC |
5934 | acb->acb_zio_head = rzio; |
5935 | ||
5936 | if (hash_lock != NULL) | |
5937 | mutex_exit(hash_lock); | |
d3c2ae1c | 5938 | |
34dc7c2f BB |
5939 | DTRACE_PROBE2(l2arc__read, vdev_t *, vd, |
5940 | zio_t *, rzio); | |
b5256303 TC |
5941 | ARCSTAT_INCR(arcstat_l2_read_bytes, |
5942 | HDR_GET_PSIZE(hdr)); | |
34dc7c2f | 5943 | |
2a432414 | 5944 | if (*arc_flags & ARC_FLAG_NOWAIT) { |
b128c09f | 5945 | zio_nowait(rzio); |
1421c891 | 5946 | goto out; |
b128c09f | 5947 | } |
34dc7c2f | 5948 | |
2a432414 | 5949 | ASSERT(*arc_flags & ARC_FLAG_WAIT); |
b128c09f | 5950 | if (zio_wait(rzio) == 0) |
1421c891 | 5951 | goto out; |
b128c09f BB |
5952 | |
5953 | /* l2arc read error; goto zio_read() */ | |
a8b2e306 TC |
5954 | if (hash_lock != NULL) |
5955 | mutex_enter(hash_lock); | |
34dc7c2f BB |
5956 | } else { |
5957 | DTRACE_PROBE1(l2arc__miss, | |
5958 | arc_buf_hdr_t *, hdr); | |
5959 | ARCSTAT_BUMP(arcstat_l2_misses); | |
5960 | if (HDR_L2_WRITING(hdr)) | |
5961 | ARCSTAT_BUMP(arcstat_l2_rw_clash); | |
b128c09f | 5962 | spa_config_exit(spa, SCL_L2ARC, vd); |
34dc7c2f | 5963 | } |
d164b209 BB |
5964 | } else { |
5965 | if (vd != NULL) | |
5966 | spa_config_exit(spa, SCL_L2ARC, vd); | |
666aa69f | 5967 | |
0902c457 | 5968 | /* |
666aa69f AM |
5969 | * Only a spa with l2 should contribute to l2 |
5970 | * miss stats. (Including the case of having a | |
5971 | * faulted cache device - that's also a miss.) | |
0902c457 | 5972 | */ |
666aa69f AM |
5973 | if (spa_has_l2) { |
5974 | /* | |
5975 | * Skip ARC stat bump for block pointers with | |
5976 | * embedded data. The data are read from the | |
5977 | * blkptr itself via | |
5978 | * decode_embedded_bp_compressed(). | |
5979 | */ | |
5980 | if (!embedded_bp) { | |
5981 | DTRACE_PROBE1(l2arc__miss, | |
5982 | arc_buf_hdr_t *, hdr); | |
5983 | ARCSTAT_BUMP(arcstat_l2_misses); | |
5984 | } | |
d164b209 | 5985 | } |
34dc7c2f | 5986 | } |
34dc7c2f | 5987 | |
b5256303 | 5988 | rzio = zio_read(pio, spa, bp, hdr_abd, size, |
d3c2ae1c | 5989 | arc_read_done, hdr, priority, zio_flags, zb); |
a8b2e306 TC |
5990 | acb->acb_zio_head = rzio; |
5991 | ||
5992 | if (hash_lock != NULL) | |
5993 | mutex_exit(hash_lock); | |
34dc7c2f | 5994 | |
2a432414 | 5995 | if (*arc_flags & ARC_FLAG_WAIT) { |
1421c891 PS |
5996 | rc = zio_wait(rzio); |
5997 | goto out; | |
5998 | } | |
34dc7c2f | 5999 | |
2a432414 | 6000 | ASSERT(*arc_flags & ARC_FLAG_NOWAIT); |
34dc7c2f BB |
6001 | zio_nowait(rzio); |
6002 | } | |
1421c891 PS |
6003 | |
6004 | out: | |
157ef7f6 | 6005 | /* embedded bps don't actually go to disk */ |
0902c457 | 6006 | if (!embedded_bp) |
157ef7f6 | 6007 | spa_read_history_add(spa, zb, *arc_flags); |
1e9231ad | 6008 | spl_fstrans_unmark(cookie); |
1421c891 | 6009 | return (rc); |
87a4dfa5 AM |
6010 | |
6011 | done: | |
6012 | if (done) | |
6013 | done(NULL, zb, bp, buf, private); | |
6014 | if (pio && rc != 0) { | |
6015 | zio_t *zio = zio_null(pio, spa, NULL, NULL, NULL, zio_flags); | |
6016 | zio->io_error = rc; | |
6017 | zio_nowait(zio); | |
6018 | } | |
6019 | goto out; | |
34dc7c2f BB |
6020 | } |
6021 | ||
ab26409d BB |
6022 | arc_prune_t * |
6023 | arc_add_prune_callback(arc_prune_func_t *func, void *private) | |
6024 | { | |
6025 | arc_prune_t *p; | |
6026 | ||
d1d7e268 | 6027 | p = kmem_alloc(sizeof (*p), KM_SLEEP); |
ab26409d BB |
6028 | p->p_pfunc = func; |
6029 | p->p_private = private; | |
6030 | list_link_init(&p->p_node); | |
424fd7c3 | 6031 | zfs_refcount_create(&p->p_refcnt); |
ab26409d BB |
6032 | |
6033 | mutex_enter(&arc_prune_mtx); | |
c13060e4 | 6034 | zfs_refcount_add(&p->p_refcnt, &arc_prune_list); |
ab26409d BB |
6035 | list_insert_head(&arc_prune_list, p); |
6036 | mutex_exit(&arc_prune_mtx); | |
6037 | ||
6038 | return (p); | |
6039 | } | |
6040 | ||
6041 | void | |
6042 | arc_remove_prune_callback(arc_prune_t *p) | |
6043 | { | |
4442f60d | 6044 | boolean_t wait = B_FALSE; |
ab26409d BB |
6045 | mutex_enter(&arc_prune_mtx); |
6046 | list_remove(&arc_prune_list, p); | |
424fd7c3 | 6047 | if (zfs_refcount_remove(&p->p_refcnt, &arc_prune_list) > 0) |
4442f60d | 6048 | wait = B_TRUE; |
ab26409d | 6049 | mutex_exit(&arc_prune_mtx); |
4442f60d CC |
6050 | |
6051 | /* wait for arc_prune_task to finish */ | |
6052 | if (wait) | |
6053 | taskq_wait_outstanding(arc_prune_taskq, 0); | |
424fd7c3 TS |
6054 | ASSERT0(zfs_refcount_count(&p->p_refcnt)); |
6055 | zfs_refcount_destroy(&p->p_refcnt); | |
4442f60d | 6056 | kmem_free(p, sizeof (*p)); |
ab26409d BB |
6057 | } |
6058 | ||
799e09f7 AM |
6059 | /* |
6060 | * Helper function for arc_prune_async() it is responsible for safely | |
6061 | * handling the execution of a registered arc_prune_func_t. | |
6062 | */ | |
6063 | static void | |
6064 | arc_prune_task(void *ptr) | |
6065 | { | |
6066 | arc_prune_t *ap = (arc_prune_t *)ptr; | |
6067 | arc_prune_func_t *func = ap->p_pfunc; | |
6068 | ||
6069 | if (func != NULL) | |
6070 | func(ap->p_adjust, ap->p_private); | |
6071 | ||
4836d293 | 6072 | (void) zfs_refcount_remove(&ap->p_refcnt, func); |
799e09f7 AM |
6073 | } |
6074 | ||
6075 | /* | |
6076 | * Notify registered consumers they must drop holds on a portion of the ARC | |
6077 | * buffers they reference. This provides a mechanism to ensure the ARC can | |
6078 | * honor the metadata limit and reclaim otherwise pinned ARC buffers. | |
6079 | * | |
6080 | * This operation is performed asynchronously so it may be safely called | |
6081 | * in the context of the arc_reclaim_thread(). A reference is taken here | |
6082 | * for each registered arc_prune_t and the arc_prune_task() is responsible | |
6083 | * for releasing it once the registered arc_prune_func_t has completed. | |
6084 | */ | |
6085 | static void | |
6086 | arc_prune_async(uint64_t adjust) | |
6087 | { | |
6088 | arc_prune_t *ap; | |
6089 | ||
6090 | mutex_enter(&arc_prune_mtx); | |
6091 | for (ap = list_head(&arc_prune_list); ap != NULL; | |
6092 | ap = list_next(&arc_prune_list, ap)) { | |
6093 | ||
6094 | if (zfs_refcount_count(&ap->p_refcnt) >= 2) | |
6095 | continue; | |
6096 | ||
6097 | zfs_refcount_add(&ap->p_refcnt, ap->p_pfunc); | |
6098 | ap->p_adjust = adjust; | |
6099 | if (taskq_dispatch(arc_prune_taskq, arc_prune_task, | |
6100 | ap, TQ_SLEEP) == TASKQID_INVALID) { | |
4836d293 | 6101 | (void) zfs_refcount_remove(&ap->p_refcnt, ap->p_pfunc); |
799e09f7 AM |
6102 | continue; |
6103 | } | |
6104 | ARCSTAT_BUMP(arcstat_prune); | |
6105 | } | |
6106 | mutex_exit(&arc_prune_mtx); | |
6107 | } | |
6108 | ||
df4474f9 MA |
6109 | /* |
6110 | * Notify the arc that a block was freed, and thus will never be used again. | |
6111 | */ | |
6112 | void | |
6113 | arc_freed(spa_t *spa, const blkptr_t *bp) | |
6114 | { | |
6115 | arc_buf_hdr_t *hdr; | |
6116 | kmutex_t *hash_lock; | |
6117 | uint64_t guid = spa_load_guid(spa); | |
6118 | ||
9b67f605 MA |
6119 | ASSERT(!BP_IS_EMBEDDED(bp)); |
6120 | ||
6121 | hdr = buf_hash_find(guid, bp, &hash_lock); | |
df4474f9 MA |
6122 | if (hdr == NULL) |
6123 | return; | |
df4474f9 | 6124 | |
d3c2ae1c GW |
6125 | /* |
6126 | * We might be trying to free a block that is still doing I/O | |
c935fe2e AM |
6127 | * (i.e. prefetch) or has some other reference (i.e. a dedup-ed, |
6128 | * dmu_sync-ed block). A block may also have a reference if it is | |
d3c2ae1c GW |
6129 | * part of a dedup-ed, dmu_synced write. The dmu_sync() function would |
6130 | * have written the new block to its final resting place on disk but | |
6131 | * without the dedup flag set. This would have left the hdr in the MRU | |
6132 | * state and discoverable. When the txg finally syncs it detects that | |
6133 | * the block was overridden in open context and issues an override I/O. | |
6134 | * Since this is a dedup block, the override I/O will determine if the | |
6135 | * block is already in the DDT. If so, then it will replace the io_bp | |
6136 | * with the bp from the DDT and allow the I/O to finish. When the I/O | |
6137 | * reaches the done callback, dbuf_write_override_done, it will | |
6138 | * check to see if the io_bp and io_bp_override are identical. | |
6139 | * If they are not, then it indicates that the bp was replaced with | |
6140 | * the bp in the DDT and the override bp is freed. This allows | |
6141 | * us to arrive here with a reference on a block that is being | |
6142 | * freed. So if we have an I/O in progress, or a reference to | |
6143 | * this hdr, then we don't destroy the hdr. | |
6144 | */ | |
c935fe2e AM |
6145 | if (!HDR_HAS_L1HDR(hdr) || |
6146 | zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { | |
6147 | arc_change_state(arc_anon, hdr); | |
d3c2ae1c | 6148 | arc_hdr_destroy(hdr); |
df4474f9 | 6149 | mutex_exit(hash_lock); |
bd089c54 | 6150 | } else { |
d3c2ae1c | 6151 | mutex_exit(hash_lock); |
34dc7c2f | 6152 | } |
34dc7c2f | 6153 | |
34dc7c2f BB |
6154 | } |
6155 | ||
6156 | /* | |
e49f1e20 WA |
6157 | * Release this buffer from the cache, making it an anonymous buffer. This |
6158 | * must be done after a read and prior to modifying the buffer contents. | |
34dc7c2f | 6159 | * If the buffer has more than one reference, we must make |
b128c09f | 6160 | * a new hdr for the buffer. |
34dc7c2f BB |
6161 | */ |
6162 | void | |
dd66857d | 6163 | arc_release(arc_buf_t *buf, const void *tag) |
34dc7c2f | 6164 | { |
b9541d6b | 6165 | arc_buf_hdr_t *hdr = buf->b_hdr; |
34dc7c2f | 6166 | |
428870ff | 6167 | /* |
ca0bf58d | 6168 | * It would be nice to assert that if its DMU metadata (level > |
428870ff BB |
6169 | * 0 || it's the dnode file), then it must be syncing context. |
6170 | * But we don't know that information at this level. | |
6171 | */ | |
6172 | ||
ca0bf58d PS |
6173 | ASSERT(HDR_HAS_L1HDR(hdr)); |
6174 | ||
b9541d6b CW |
6175 | /* |
6176 | * We don't grab the hash lock prior to this check, because if | |
6177 | * the buffer's header is in the arc_anon state, it won't be | |
6178 | * linked into the hash table. | |
6179 | */ | |
6180 | if (hdr->b_l1hdr.b_state == arc_anon) { | |
b9541d6b CW |
6181 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
6182 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); | |
6183 | ASSERT(!HDR_HAS_L2HDR(hdr)); | |
34dc7c2f | 6184 | |
96b9cf42 AM |
6185 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, buf); |
6186 | ASSERT(ARC_BUF_LAST(buf)); | |
424fd7c3 | 6187 | ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); |
c935fe2e | 6188 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b | 6189 | |
b9541d6b | 6190 | hdr->b_l1hdr.b_arc_access = 0; |
d3c2ae1c GW |
6191 | |
6192 | /* | |
6193 | * If the buf is being overridden then it may already | |
6194 | * have a hdr that is not empty. | |
6195 | */ | |
6196 | buf_discard_identity(hdr); | |
b9541d6b CW |
6197 | arc_buf_thaw(buf); |
6198 | ||
6199 | return; | |
34dc7c2f BB |
6200 | } |
6201 | ||
1c27024e | 6202 | kmutex_t *hash_lock = HDR_LOCK(hdr); |
b9541d6b CW |
6203 | mutex_enter(hash_lock); |
6204 | ||
6205 | /* | |
6206 | * This assignment is only valid as long as the hash_lock is | |
6207 | * held, we must be careful not to reference state or the | |
6208 | * b_state field after dropping the lock. | |
6209 | */ | |
1c27024e | 6210 | arc_state_t *state = hdr->b_l1hdr.b_state; |
b9541d6b CW |
6211 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
6212 | ASSERT3P(state, !=, arc_anon); | |
6213 | ||
6214 | /* this buffer is not on any list */ | |
424fd7c3 | 6215 | ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), >, 0); |
b9541d6b CW |
6216 | |
6217 | if (HDR_HAS_L2HDR(hdr)) { | |
b9541d6b | 6218 | mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); |
ca0bf58d PS |
6219 | |
6220 | /* | |
d962d5da PS |
6221 | * We have to recheck this conditional again now that |
6222 | * we're holding the l2ad_mtx to prevent a race with | |
6223 | * another thread which might be concurrently calling | |
6224 | * l2arc_evict(). In that case, l2arc_evict() might have | |
6225 | * destroyed the header's L2 portion as we were waiting | |
6226 | * to acquire the l2ad_mtx. | |
ca0bf58d | 6227 | */ |
d962d5da PS |
6228 | if (HDR_HAS_L2HDR(hdr)) |
6229 | arc_hdr_l2hdr_destroy(hdr); | |
ca0bf58d | 6230 | |
b9541d6b | 6231 | mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); |
b128c09f BB |
6232 | } |
6233 | ||
34dc7c2f BB |
6234 | /* |
6235 | * Do we have more than one buf? | |
6236 | */ | |
96b9cf42 | 6237 | if (hdr->b_l1hdr.b_buf != buf || !ARC_BUF_LAST(buf)) { |
34dc7c2f | 6238 | arc_buf_hdr_t *nhdr; |
d164b209 | 6239 | uint64_t spa = hdr->b_spa; |
d3c2ae1c GW |
6240 | uint64_t psize = HDR_GET_PSIZE(hdr); |
6241 | uint64_t lsize = HDR_GET_LSIZE(hdr); | |
b5256303 TC |
6242 | boolean_t protected = HDR_PROTECTED(hdr); |
6243 | enum zio_compress compress = arc_hdr_get_compress(hdr); | |
b9541d6b | 6244 | arc_buf_contents_t type = arc_buf_type(hdr); |
d3c2ae1c | 6245 | VERIFY3U(hdr->b_type, ==, type); |
34dc7c2f | 6246 | |
b9541d6b | 6247 | ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); |
ed2f7ba0 | 6248 | VERIFY3S(remove_reference(hdr, tag), >, 0); |
d3c2ae1c | 6249 | |
57b40985 | 6250 | if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) { |
d3c2ae1c | 6251 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); |
524b4217 DK |
6252 | ASSERT(ARC_BUF_LAST(buf)); |
6253 | } | |
d3c2ae1c | 6254 | |
34dc7c2f | 6255 | /* |
428870ff | 6256 | * Pull the data off of this hdr and attach it to |
d3c2ae1c GW |
6257 | * a new anonymous hdr. Also find the last buffer |
6258 | * in the hdr's buffer list. | |
34dc7c2f | 6259 | */ |
a7004725 | 6260 | arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); |
d3c2ae1c | 6261 | ASSERT3P(lastbuf, !=, NULL); |
34dc7c2f | 6262 | |
d3c2ae1c GW |
6263 | /* |
6264 | * If the current arc_buf_t and the hdr are sharing their data | |
524b4217 | 6265 | * buffer, then we must stop sharing that block. |
d3c2ae1c | 6266 | */ |
57b40985 | 6267 | if (ARC_BUF_SHARED(buf)) { |
d3c2ae1c | 6268 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); |
57b40985 | 6269 | ASSERT(!arc_buf_is_shared(lastbuf)); |
d3c2ae1c GW |
6270 | |
6271 | /* | |
6272 | * First, sever the block sharing relationship between | |
a7004725 | 6273 | * buf and the arc_buf_hdr_t. |
d3c2ae1c GW |
6274 | */ |
6275 | arc_unshare_buf(hdr, buf); | |
2aa34383 DK |
6276 | |
6277 | /* | |
a6255b7f | 6278 | * Now we need to recreate the hdr's b_pabd. Since we |
524b4217 | 6279 | * have lastbuf handy, we try to share with it, but if |
a6255b7f | 6280 | * we can't then we allocate a new b_pabd and copy the |
524b4217 | 6281 | * data from buf into it. |
2aa34383 | 6282 | */ |
524b4217 DK |
6283 | if (arc_can_share(hdr, lastbuf)) { |
6284 | arc_share_buf(hdr, lastbuf); | |
6285 | } else { | |
a8d83e2a | 6286 | arc_hdr_alloc_abd(hdr, 0); |
a6255b7f DQ |
6287 | abd_copy_from_buf(hdr->b_l1hdr.b_pabd, |
6288 | buf->b_data, psize); | |
2aa34383 | 6289 | } |
d3c2ae1c GW |
6290 | VERIFY3P(lastbuf->b_data, !=, NULL); |
6291 | } else if (HDR_SHARED_DATA(hdr)) { | |
2aa34383 DK |
6292 | /* |
6293 | * Uncompressed shared buffers are always at the end | |
6294 | * of the list. Compressed buffers don't have the | |
6295 | * same requirements. This makes it hard to | |
6296 | * simply assert that the lastbuf is shared so | |
6297 | * we rely on the hdr's compression flags to determine | |
6298 | * if we have a compressed, shared buffer. | |
6299 | */ | |
6300 | ASSERT(arc_buf_is_shared(lastbuf) || | |
b5256303 | 6301 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); |
57b40985 | 6302 | ASSERT(!arc_buf_is_shared(buf)); |
d3c2ae1c | 6303 | } |
b5256303 TC |
6304 | |
6305 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); | |
b9541d6b | 6306 | ASSERT3P(state, !=, arc_l2c_only); |
36da08ef | 6307 | |
a8d83e2a | 6308 | (void) zfs_refcount_remove_many(&state->arcs_size[type], |
2aa34383 | 6309 | arc_buf_size(buf), buf); |
36da08ef | 6310 | |
424fd7c3 | 6311 | if (zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { |
b9541d6b | 6312 | ASSERT3P(state, !=, arc_l2c_only); |
424fd7c3 TS |
6313 | (void) zfs_refcount_remove_many( |
6314 | &state->arcs_esize[type], | |
2aa34383 | 6315 | arc_buf_size(buf), buf); |
34dc7c2f | 6316 | } |
1eb5bfa3 | 6317 | |
34dc7c2f | 6318 | arc_cksum_verify(buf); |
498877ba | 6319 | arc_buf_unwatch(buf); |
34dc7c2f | 6320 | |
f486f584 TC |
6321 | /* if this is the last uncompressed buf free the checksum */ |
6322 | if (!arc_hdr_has_uncompressed_buf(hdr)) | |
6323 | arc_cksum_free(hdr); | |
6324 | ||
34dc7c2f BB |
6325 | mutex_exit(hash_lock); |
6326 | ||
b5256303 | 6327 | nhdr = arc_hdr_alloc(spa, psize, lsize, protected, |
6b88b4b5 | 6328 | compress, hdr->b_complevel, type); |
d3c2ae1c | 6329 | ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL); |
424fd7c3 | 6330 | ASSERT0(zfs_refcount_count(&nhdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
6331 | VERIFY3U(nhdr->b_type, ==, type); |
6332 | ASSERT(!HDR_SHARED_DATA(nhdr)); | |
b9541d6b | 6333 | |
d3c2ae1c | 6334 | nhdr->b_l1hdr.b_buf = buf; |
c13060e4 | 6335 | (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); |
34dc7c2f | 6336 | buf->b_hdr = nhdr; |
d3c2ae1c | 6337 | |
a8d83e2a | 6338 | (void) zfs_refcount_add_many(&arc_anon->arcs_size[type], |
5e8ff256 | 6339 | arc_buf_size(buf), buf); |
34dc7c2f | 6340 | } else { |
424fd7c3 | 6341 | ASSERT(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); |
ca0bf58d PS |
6342 | /* protected by hash lock, or hdr is on arc_anon */ |
6343 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
34dc7c2f | 6344 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b CW |
6345 | hdr->b_l1hdr.b_mru_hits = 0; |
6346 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
6347 | hdr->b_l1hdr.b_mfu_hits = 0; | |
6348 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
c935fe2e | 6349 | arc_change_state(arc_anon, hdr); |
b9541d6b | 6350 | hdr->b_l1hdr.b_arc_access = 0; |
34dc7c2f | 6351 | |
b5256303 | 6352 | mutex_exit(hash_lock); |
428870ff | 6353 | buf_discard_identity(hdr); |
34dc7c2f BB |
6354 | arc_buf_thaw(buf); |
6355 | } | |
34dc7c2f BB |
6356 | } |
6357 | ||
6358 | int | |
6359 | arc_released(arc_buf_t *buf) | |
6360 | { | |
289f7e6a | 6361 | return (buf->b_data != NULL && |
b9541d6b | 6362 | buf->b_hdr->b_l1hdr.b_state == arc_anon); |
34dc7c2f BB |
6363 | } |
6364 | ||
34dc7c2f BB |
6365 | #ifdef ZFS_DEBUG |
6366 | int | |
6367 | arc_referenced(arc_buf_t *buf) | |
6368 | { | |
289f7e6a | 6369 | return (zfs_refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); |
34dc7c2f BB |
6370 | } |
6371 | #endif | |
6372 | ||
6373 | static void | |
6374 | arc_write_ready(zio_t *zio) | |
6375 | { | |
6376 | arc_write_callback_t *callback = zio->io_private; | |
6377 | arc_buf_t *buf = callback->awcb_buf; | |
6378 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
b5256303 TC |
6379 | blkptr_t *bp = zio->io_bp; |
6380 | uint64_t psize = BP_IS_HOLE(bp) ? 0 : BP_GET_PSIZE(bp); | |
a6255b7f | 6381 | fstrans_cookie_t cookie = spl_fstrans_mark(); |
34dc7c2f | 6382 | |
b9541d6b | 6383 | ASSERT(HDR_HAS_L1HDR(hdr)); |
424fd7c3 | 6384 | ASSERT(!zfs_refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); |
96b9cf42 | 6385 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL); |
b128c09f | 6386 | |
34dc7c2f | 6387 | /* |
d3c2ae1c GW |
6388 | * If we're reexecuting this zio because the pool suspended, then |
6389 | * cleanup any state that was previously set the first time the | |
2aa34383 | 6390 | * callback was invoked. |
34dc7c2f | 6391 | */ |
d3c2ae1c GW |
6392 | if (zio->io_flags & ZIO_FLAG_REEXECUTED) { |
6393 | arc_cksum_free(hdr); | |
6394 | arc_buf_unwatch(buf); | |
a6255b7f | 6395 | if (hdr->b_l1hdr.b_pabd != NULL) { |
57b40985 | 6396 | if (ARC_BUF_SHARED(buf)) { |
d3c2ae1c GW |
6397 | arc_unshare_buf(hdr, buf); |
6398 | } else { | |
57b40985 | 6399 | ASSERT(!arc_buf_is_shared(buf)); |
b5256303 | 6400 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c | 6401 | } |
34dc7c2f | 6402 | } |
b5256303 TC |
6403 | |
6404 | if (HDR_HAS_RABD(hdr)) | |
6405 | arc_hdr_free_abd(hdr, B_TRUE); | |
34dc7c2f | 6406 | } |
a6255b7f | 6407 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 6408 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
6409 | ASSERT(!HDR_SHARED_DATA(hdr)); |
6410 | ASSERT(!arc_buf_is_shared(buf)); | |
6411 | ||
6412 | callback->awcb_ready(zio, buf, callback->awcb_private); | |
6413 | ||
c935fe2e | 6414 | if (HDR_IO_IN_PROGRESS(hdr)) { |
d3c2ae1c | 6415 | ASSERT(zio->io_flags & ZIO_FLAG_REEXECUTED); |
c935fe2e AM |
6416 | } else { |
6417 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); | |
6418 | add_reference(hdr, hdr); /* For IO_IN_PROGRESS. */ | |
6419 | } | |
d3c2ae1c | 6420 | |
b5256303 TC |
6421 | if (BP_IS_PROTECTED(bp)) { |
6422 | /* ZIL blocks are written through zio_rewrite */ | |
6423 | ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG); | |
b5256303 | 6424 | |
ae76f45c TC |
6425 | if (BP_SHOULD_BYTESWAP(bp)) { |
6426 | if (BP_GET_LEVEL(bp) > 0) { | |
6427 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; | |
6428 | } else { | |
6429 | hdr->b_l1hdr.b_byteswap = | |
6430 | DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); | |
6431 | } | |
6432 | } else { | |
6433 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
6434 | } | |
6435 | ||
75a2eb7f | 6436 | arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED); |
b5256303 TC |
6437 | hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp); |
6438 | hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset; | |
6439 | zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt, | |
6440 | hdr->b_crypt_hdr.b_iv); | |
6441 | zio_crypt_decode_mac_bp(bp, hdr->b_crypt_hdr.b_mac); | |
75a2eb7f AM |
6442 | } else { |
6443 | arc_hdr_clear_flags(hdr, ARC_FLAG_PROTECTED); | |
b5256303 TC |
6444 | } |
6445 | ||
6446 | /* | |
6447 | * If this block was written for raw encryption but the zio layer | |
6448 | * ended up only authenticating it, adjust the buffer flags now. | |
6449 | */ | |
6450 | if (BP_IS_AUTHENTICATED(bp) && ARC_BUF_ENCRYPTED(buf)) { | |
6451 | arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH); | |
6452 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
6453 | if (BP_GET_COMPRESS(bp) == ZIO_COMPRESS_OFF) | |
6454 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
b1d21733 TC |
6455 | } else if (BP_IS_HOLE(bp) && ARC_BUF_ENCRYPTED(buf)) { |
6456 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
6457 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
b5256303 TC |
6458 | } |
6459 | ||
6460 | /* this must be done after the buffer flags are adjusted */ | |
6461 | arc_cksum_compute(buf); | |
6462 | ||
1c27024e | 6463 | enum zio_compress compress; |
b5256303 | 6464 | if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { |
d3c2ae1c GW |
6465 | compress = ZIO_COMPRESS_OFF; |
6466 | } else { | |
b5256303 TC |
6467 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); |
6468 | compress = BP_GET_COMPRESS(bp); | |
d3c2ae1c GW |
6469 | } |
6470 | HDR_SET_PSIZE(hdr, psize); | |
6471 | arc_hdr_set_compress(hdr, compress); | |
10b3c7f5 | 6472 | hdr->b_complevel = zio->io_prop.zp_complevel; |
d3c2ae1c | 6473 | |
4807c0ba TC |
6474 | if (zio->io_error != 0 || psize == 0) |
6475 | goto out; | |
6476 | ||
d3c2ae1c | 6477 | /* |
b5256303 TC |
6478 | * Fill the hdr with data. If the buffer is encrypted we have no choice |
6479 | * but to copy the data into b_radb. If the hdr is compressed, the data | |
6480 | * we want is available from the zio, otherwise we can take it from | |
6481 | * the buf. | |
a6255b7f DQ |
6482 | * |
6483 | * We might be able to share the buf's data with the hdr here. However, | |
6484 | * doing so would cause the ARC to be full of linear ABDs if we write a | |
6485 | * lot of shareable data. As a compromise, we check whether scattered | |
6486 | * ABDs are allowed, and assume that if they are then the user wants | |
6487 | * the ARC to be primarily filled with them regardless of the data being | |
6488 | * written. Therefore, if they're allowed then we allocate one and copy | |
6489 | * the data into it; otherwise, we share the data directly if we can. | |
d3c2ae1c | 6490 | */ |
b5256303 | 6491 | if (ARC_BUF_ENCRYPTED(buf)) { |
4807c0ba | 6492 | ASSERT3U(psize, >, 0); |
b5256303 | 6493 | ASSERT(ARC_BUF_COMPRESSED(buf)); |
a8d83e2a | 6494 | arc_hdr_alloc_abd(hdr, ARC_HDR_ALLOC_RDATA | |
6b88b4b5 | 6495 | ARC_HDR_USE_RESERVE); |
b5256303 | 6496 | abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize); |
ed2f7ba0 AM |
6497 | } else if (!(HDR_UNCACHED(hdr) || |
6498 | abd_size_alloc_linear(arc_buf_size(buf))) || | |
7eebcd2b | 6499 | !arc_can_share(hdr, buf)) { |
a6255b7f DQ |
6500 | /* |
6501 | * Ideally, we would always copy the io_abd into b_pabd, but the | |
6502 | * user may have disabled compressed ARC, thus we must check the | |
6503 | * hdr's compression setting rather than the io_bp's. | |
6504 | */ | |
b5256303 | 6505 | if (BP_IS_ENCRYPTED(bp)) { |
a6255b7f | 6506 | ASSERT3U(psize, >, 0); |
a8d83e2a AM |
6507 | arc_hdr_alloc_abd(hdr, ARC_HDR_ALLOC_RDATA | |
6508 | ARC_HDR_USE_RESERVE); | |
b5256303 TC |
6509 | abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize); |
6510 | } else if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF && | |
6511 | !ARC_BUF_COMPRESSED(buf)) { | |
6512 | ASSERT3U(psize, >, 0); | |
a8d83e2a | 6513 | arc_hdr_alloc_abd(hdr, ARC_HDR_USE_RESERVE); |
a6255b7f DQ |
6514 | abd_copy(hdr->b_l1hdr.b_pabd, zio->io_abd, psize); |
6515 | } else { | |
6516 | ASSERT3U(zio->io_orig_size, ==, arc_hdr_size(hdr)); | |
a8d83e2a | 6517 | arc_hdr_alloc_abd(hdr, ARC_HDR_USE_RESERVE); |
a6255b7f DQ |
6518 | abd_copy_from_buf(hdr->b_l1hdr.b_pabd, buf->b_data, |
6519 | arc_buf_size(buf)); | |
6520 | } | |
d3c2ae1c | 6521 | } else { |
a6255b7f | 6522 | ASSERT3P(buf->b_data, ==, abd_to_buf(zio->io_orig_abd)); |
2aa34383 | 6523 | ASSERT3U(zio->io_orig_size, ==, arc_buf_size(buf)); |
96b9cf42 AM |
6524 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, buf); |
6525 | ASSERT(ARC_BUF_LAST(buf)); | |
d3c2ae1c | 6526 | |
d3c2ae1c | 6527 | arc_share_buf(hdr, buf); |
d3c2ae1c | 6528 | } |
a6255b7f | 6529 | |
4807c0ba | 6530 | out: |
b5256303 | 6531 | arc_hdr_verify(hdr, bp); |
a6255b7f | 6532 | spl_fstrans_unmark(cookie); |
34dc7c2f BB |
6533 | } |
6534 | ||
bc77ba73 PD |
6535 | static void |
6536 | arc_write_children_ready(zio_t *zio) | |
6537 | { | |
6538 | arc_write_callback_t *callback = zio->io_private; | |
6539 | arc_buf_t *buf = callback->awcb_buf; | |
6540 | ||
6541 | callback->awcb_children_ready(zio, buf, callback->awcb_private); | |
6542 | } | |
6543 | ||
34dc7c2f BB |
6544 | static void |
6545 | arc_write_done(zio_t *zio) | |
6546 | { | |
6547 | arc_write_callback_t *callback = zio->io_private; | |
6548 | arc_buf_t *buf = callback->awcb_buf; | |
6549 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
6550 | ||
d3c2ae1c | 6551 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
428870ff BB |
6552 | |
6553 | if (zio->io_error == 0) { | |
d3c2ae1c GW |
6554 | arc_hdr_verify(hdr, zio->io_bp); |
6555 | ||
9b67f605 | 6556 | if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { |
b0bc7a84 MG |
6557 | buf_discard_identity(hdr); |
6558 | } else { | |
6559 | hdr->b_dva = *BP_IDENTITY(zio->io_bp); | |
493fcce9 | 6560 | hdr->b_birth = BP_GET_BIRTH(zio->io_bp); |
b0bc7a84 | 6561 | } |
428870ff | 6562 | } else { |
d3c2ae1c | 6563 | ASSERT(HDR_EMPTY(hdr)); |
428870ff | 6564 | } |
34dc7c2f | 6565 | |
34dc7c2f | 6566 | /* |
9b67f605 MA |
6567 | * If the block to be written was all-zero or compressed enough to be |
6568 | * embedded in the BP, no write was performed so there will be no | |
6569 | * dva/birth/checksum. The buffer must therefore remain anonymous | |
6570 | * (and uncached). | |
34dc7c2f | 6571 | */ |
d3c2ae1c | 6572 | if (!HDR_EMPTY(hdr)) { |
34dc7c2f BB |
6573 | arc_buf_hdr_t *exists; |
6574 | kmutex_t *hash_lock; | |
6575 | ||
524b4217 | 6576 | ASSERT3U(zio->io_error, ==, 0); |
428870ff | 6577 | |
34dc7c2f BB |
6578 | arc_cksum_verify(buf); |
6579 | ||
6580 | exists = buf_hash_insert(hdr, &hash_lock); | |
b9541d6b | 6581 | if (exists != NULL) { |
34dc7c2f BB |
6582 | /* |
6583 | * This can only happen if we overwrite for | |
6584 | * sync-to-convergence, because we remove | |
6585 | * buffers from the hash table when we arc_free(). | |
6586 | */ | |
428870ff BB |
6587 | if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { |
6588 | if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) | |
6589 | panic("bad overwrite, hdr=%p exists=%p", | |
6590 | (void *)hdr, (void *)exists); | |
424fd7c3 | 6591 | ASSERT(zfs_refcount_is_zero( |
b9541d6b | 6592 | &exists->b_l1hdr.b_refcnt)); |
c935fe2e | 6593 | arc_change_state(arc_anon, exists); |
428870ff | 6594 | arc_hdr_destroy(exists); |
ca6c7a94 | 6595 | mutex_exit(hash_lock); |
428870ff BB |
6596 | exists = buf_hash_insert(hdr, &hash_lock); |
6597 | ASSERT3P(exists, ==, NULL); | |
03c6040b GW |
6598 | } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { |
6599 | /* nopwrite */ | |
6600 | ASSERT(zio->io_prop.zp_nopwrite); | |
6601 | if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) | |
6602 | panic("bad nopwrite, hdr=%p exists=%p", | |
6603 | (void *)hdr, (void *)exists); | |
428870ff BB |
6604 | } else { |
6605 | /* Dedup */ | |
96b9cf42 AM |
6606 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL); |
6607 | ASSERT(ARC_BUF_LAST(hdr->b_l1hdr.b_buf)); | |
b9541d6b | 6608 | ASSERT(hdr->b_l1hdr.b_state == arc_anon); |
428870ff BB |
6609 | ASSERT(BP_GET_DEDUP(zio->io_bp)); |
6610 | ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); | |
6611 | } | |
34dc7c2f | 6612 | } |
d3c2ae1c | 6613 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
ed2f7ba0 | 6614 | VERIFY3S(remove_reference(hdr, hdr), >, 0); |
b128c09f | 6615 | /* if it's not anon, we are doing a scrub */ |
b9541d6b | 6616 | if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) |
c935fe2e | 6617 | arc_access(hdr, 0, B_FALSE); |
34dc7c2f | 6618 | mutex_exit(hash_lock); |
34dc7c2f | 6619 | } else { |
d3c2ae1c | 6620 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
ed2f7ba0 | 6621 | VERIFY3S(remove_reference(hdr, hdr), >, 0); |
34dc7c2f BB |
6622 | } |
6623 | ||
428870ff | 6624 | callback->awcb_done(zio, buf, callback->awcb_private); |
34dc7c2f | 6625 | |
e2af2acc | 6626 | abd_free(zio->io_abd); |
34dc7c2f BB |
6627 | kmem_free(callback, sizeof (arc_write_callback_t)); |
6628 | } | |
6629 | ||
6630 | zio_t * | |
428870ff | 6631 | arc_write(zio_t *pio, spa_t *spa, uint64_t txg, |
ed2f7ba0 | 6632 | blkptr_t *bp, arc_buf_t *buf, boolean_t uncached, boolean_t l2arc, |
b5256303 | 6633 | const zio_prop_t *zp, arc_write_done_func_t *ready, |
ccec7fbe AM |
6634 | arc_write_done_func_t *children_ready, arc_write_done_func_t *done, |
6635 | void *private, zio_priority_t priority, int zio_flags, | |
6636 | const zbookmark_phys_t *zb) | |
34dc7c2f BB |
6637 | { |
6638 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
6639 | arc_write_callback_t *callback; | |
b128c09f | 6640 | zio_t *zio; |
82644107 | 6641 | zio_prop_t localprop = *zp; |
34dc7c2f | 6642 | |
d3c2ae1c GW |
6643 | ASSERT3P(ready, !=, NULL); |
6644 | ASSERT3P(done, !=, NULL); | |
34dc7c2f | 6645 | ASSERT(!HDR_IO_ERROR(hdr)); |
b9541d6b | 6646 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
d3c2ae1c | 6647 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
96b9cf42 | 6648 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL); |
ed2f7ba0 AM |
6649 | if (uncached) |
6650 | arc_hdr_set_flags(hdr, ARC_FLAG_UNCACHED); | |
6651 | else if (l2arc) | |
d3c2ae1c | 6652 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); |
82644107 | 6653 | |
b5256303 TC |
6654 | if (ARC_BUF_ENCRYPTED(buf)) { |
6655 | ASSERT(ARC_BUF_COMPRESSED(buf)); | |
6656 | localprop.zp_encrypt = B_TRUE; | |
6657 | localprop.zp_compress = HDR_GET_COMPRESS(hdr); | |
10b3c7f5 | 6658 | localprop.zp_complevel = hdr->b_complevel; |
b5256303 TC |
6659 | localprop.zp_byteorder = |
6660 | (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ? | |
6661 | ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER; | |
861166b0 | 6662 | memcpy(localprop.zp_salt, hdr->b_crypt_hdr.b_salt, |
b5256303 | 6663 | ZIO_DATA_SALT_LEN); |
861166b0 | 6664 | memcpy(localprop.zp_iv, hdr->b_crypt_hdr.b_iv, |
b5256303 | 6665 | ZIO_DATA_IV_LEN); |
861166b0 | 6666 | memcpy(localprop.zp_mac, hdr->b_crypt_hdr.b_mac, |
b5256303 TC |
6667 | ZIO_DATA_MAC_LEN); |
6668 | if (DMU_OT_IS_ENCRYPTED(localprop.zp_type)) { | |
6669 | localprop.zp_nopwrite = B_FALSE; | |
6670 | localprop.zp_copies = | |
6671 | MIN(localprop.zp_copies, SPA_DVAS_PER_BP - 1); | |
6672 | } | |
2aa34383 | 6673 | zio_flags |= ZIO_FLAG_RAW; |
b5256303 TC |
6674 | } else if (ARC_BUF_COMPRESSED(buf)) { |
6675 | ASSERT3U(HDR_GET_LSIZE(hdr), !=, arc_buf_size(buf)); | |
6676 | localprop.zp_compress = HDR_GET_COMPRESS(hdr); | |
10b3c7f5 | 6677 | localprop.zp_complevel = hdr->b_complevel; |
b5256303 | 6678 | zio_flags |= ZIO_FLAG_RAW_COMPRESS; |
2aa34383 | 6679 | } |
79c76d5b | 6680 | callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); |
34dc7c2f | 6681 | callback->awcb_ready = ready; |
bc77ba73 | 6682 | callback->awcb_children_ready = children_ready; |
34dc7c2f BB |
6683 | callback->awcb_done = done; |
6684 | callback->awcb_private = private; | |
6685 | callback->awcb_buf = buf; | |
b128c09f | 6686 | |
d3c2ae1c | 6687 | /* |
a6255b7f | 6688 | * The hdr's b_pabd is now stale, free it now. A new data block |
d3c2ae1c GW |
6689 | * will be allocated when the zio pipeline calls arc_write_ready(). |
6690 | */ | |
a6255b7f | 6691 | if (hdr->b_l1hdr.b_pabd != NULL) { |
d3c2ae1c GW |
6692 | /* |
6693 | * If the buf is currently sharing the data block with | |
6694 | * the hdr then we need to break that relationship here. | |
6695 | * The hdr will remain with a NULL data pointer and the | |
6696 | * buf will take sole ownership of the block. | |
6697 | */ | |
57b40985 | 6698 | if (ARC_BUF_SHARED(buf)) { |
d3c2ae1c GW |
6699 | arc_unshare_buf(hdr, buf); |
6700 | } else { | |
57b40985 | 6701 | ASSERT(!arc_buf_is_shared(buf)); |
b5256303 | 6702 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c GW |
6703 | } |
6704 | VERIFY3P(buf->b_data, !=, NULL); | |
d3c2ae1c | 6705 | } |
b5256303 TC |
6706 | |
6707 | if (HDR_HAS_RABD(hdr)) | |
6708 | arc_hdr_free_abd(hdr, B_TRUE); | |
6709 | ||
71a24c3c TC |
6710 | if (!(zio_flags & ZIO_FLAG_RAW)) |
6711 | arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF); | |
b5256303 | 6712 | |
d3c2ae1c | 6713 | ASSERT(!arc_buf_is_shared(buf)); |
a6255b7f | 6714 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
d3c2ae1c | 6715 | |
a6255b7f DQ |
6716 | zio = zio_write(pio, spa, txg, bp, |
6717 | abd_get_from_buf(buf->b_data, HDR_GET_LSIZE(hdr)), | |
82644107 | 6718 | HDR_GET_LSIZE(hdr), arc_buf_size(buf), &localprop, arc_write_ready, |
bc77ba73 | 6719 | (children_ready != NULL) ? arc_write_children_ready : NULL, |
ccec7fbe | 6720 | arc_write_done, callback, priority, zio_flags, zb); |
34dc7c2f BB |
6721 | |
6722 | return (zio); | |
6723 | } | |
6724 | ||
34dc7c2f BB |
6725 | void |
6726 | arc_tempreserve_clear(uint64_t reserve) | |
6727 | { | |
6728 | atomic_add_64(&arc_tempreserve, -reserve); | |
6729 | ASSERT((int64_t)arc_tempreserve >= 0); | |
6730 | } | |
6731 | ||
6732 | int | |
dae3e9ea | 6733 | arc_tempreserve_space(spa_t *spa, uint64_t reserve, uint64_t txg) |
34dc7c2f BB |
6734 | { |
6735 | int error; | |
9babb374 | 6736 | uint64_t anon_size; |
34dc7c2f | 6737 | |
1b8951b3 TC |
6738 | if (!arc_no_grow && |
6739 | reserve > arc_c/4 && | |
6740 | reserve * 4 > (2ULL << SPA_MAXBLOCKSHIFT)) | |
34dc7c2f | 6741 | arc_c = MIN(arc_c_max, reserve * 4); |
12f9a6a3 BB |
6742 | |
6743 | /* | |
6744 | * Throttle when the calculated memory footprint for the TXG | |
6745 | * exceeds the target ARC size. | |
6746 | */ | |
570827e1 BB |
6747 | if (reserve > arc_c) { |
6748 | DMU_TX_STAT_BUMP(dmu_tx_memory_reserve); | |
12f9a6a3 | 6749 | return (SET_ERROR(ERESTART)); |
570827e1 | 6750 | } |
34dc7c2f | 6751 | |
9babb374 BB |
6752 | /* |
6753 | * Don't count loaned bufs as in flight dirty data to prevent long | |
6754 | * network delays from blocking transactions that are ready to be | |
6755 | * assigned to a txg. | |
6756 | */ | |
a7004725 DK |
6757 | |
6758 | /* assert that it has not wrapped around */ | |
6759 | ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); | |
6760 | ||
a8d83e2a AM |
6761 | anon_size = MAX((int64_t) |
6762 | (zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_DATA]) + | |
6763 | zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_METADATA]) - | |
36da08ef | 6764 | arc_loaned_bytes), 0); |
9babb374 | 6765 | |
34dc7c2f BB |
6766 | /* |
6767 | * Writes will, almost always, require additional memory allocations | |
d3cc8b15 | 6768 | * in order to compress/encrypt/etc the data. We therefore need to |
34dc7c2f BB |
6769 | * make sure that there is sufficient available memory for this. |
6770 | */ | |
dae3e9ea | 6771 | error = arc_memory_throttle(spa, reserve, txg); |
e8b96c60 | 6772 | if (error != 0) |
34dc7c2f BB |
6773 | return (error); |
6774 | ||
6775 | /* | |
6776 | * Throttle writes when the amount of dirty data in the cache | |
6777 | * gets too large. We try to keep the cache less than half full | |
6778 | * of dirty blocks so that our sync times don't grow too large. | |
dae3e9ea DB |
6779 | * |
6780 | * In the case of one pool being built on another pool, we want | |
6781 | * to make sure we don't end up throttling the lower (backing) | |
6782 | * pool when the upper pool is the majority contributor to dirty | |
6783 | * data. To insure we make forward progress during throttling, we | |
6784 | * also check the current pool's net dirty data and only throttle | |
6785 | * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty | |
6786 | * data in the cache. | |
6787 | * | |
34dc7c2f BB |
6788 | * Note: if two requests come in concurrently, we might let them |
6789 | * both succeed, when one of them should fail. Not a huge deal. | |
6790 | */ | |
dae3e9ea DB |
6791 | uint64_t total_dirty = reserve + arc_tempreserve + anon_size; |
6792 | uint64_t spa_dirty_anon = spa_dirty_data(spa); | |
daabddaa AM |
6793 | uint64_t rarc_c = arc_warm ? arc_c : arc_c_max; |
6794 | if (total_dirty > rarc_c * zfs_arc_dirty_limit_percent / 100 && | |
6795 | anon_size > rarc_c * zfs_arc_anon_limit_percent / 100 && | |
dae3e9ea | 6796 | spa_dirty_anon > anon_size * zfs_arc_pool_dirty_percent / 100) { |
2fd92c3d | 6797 | #ifdef ZFS_DEBUG |
424fd7c3 TS |
6798 | uint64_t meta_esize = zfs_refcount_count( |
6799 | &arc_anon->arcs_esize[ARC_BUFC_METADATA]); | |
d3c2ae1c | 6800 | uint64_t data_esize = |
424fd7c3 | 6801 | zfs_refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]); |
34dc7c2f | 6802 | dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " |
daabddaa | 6803 | "anon_data=%lluK tempreserve=%lluK rarc_c=%lluK\n", |
8e739b2c RE |
6804 | (u_longlong_t)arc_tempreserve >> 10, |
6805 | (u_longlong_t)meta_esize >> 10, | |
6806 | (u_longlong_t)data_esize >> 10, | |
6807 | (u_longlong_t)reserve >> 10, | |
6808 | (u_longlong_t)rarc_c >> 10); | |
2fd92c3d | 6809 | #endif |
570827e1 | 6810 | DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle); |
2e528b49 | 6811 | return (SET_ERROR(ERESTART)); |
34dc7c2f BB |
6812 | } |
6813 | atomic_add_64(&arc_tempreserve, reserve); | |
6814 | return (0); | |
6815 | } | |
6816 | ||
13be560d BB |
6817 | static void |
6818 | arc_kstat_update_state(arc_state_t *state, kstat_named_t *size, | |
a8d83e2a | 6819 | kstat_named_t *data, kstat_named_t *metadata, |
13be560d BB |
6820 | kstat_named_t *evict_data, kstat_named_t *evict_metadata) |
6821 | { | |
a8d83e2a AM |
6822 | data->value.ui64 = |
6823 | zfs_refcount_count(&state->arcs_size[ARC_BUFC_DATA]); | |
6824 | metadata->value.ui64 = | |
6825 | zfs_refcount_count(&state->arcs_size[ARC_BUFC_METADATA]); | |
6826 | size->value.ui64 = data->value.ui64 + metadata->value.ui64; | |
d3c2ae1c | 6827 | evict_data->value.ui64 = |
424fd7c3 | 6828 | zfs_refcount_count(&state->arcs_esize[ARC_BUFC_DATA]); |
d3c2ae1c | 6829 | evict_metadata->value.ui64 = |
424fd7c3 | 6830 | zfs_refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]); |
13be560d BB |
6831 | } |
6832 | ||
6833 | static int | |
6834 | arc_kstat_update(kstat_t *ksp, int rw) | |
6835 | { | |
6836 | arc_stats_t *as = ksp->ks_data; | |
6837 | ||
c4c162c1 | 6838 | if (rw == KSTAT_WRITE) |
ecb2b7dc | 6839 | return (SET_ERROR(EACCES)); |
c4c162c1 AM |
6840 | |
6841 | as->arcstat_hits.value.ui64 = | |
6842 | wmsum_value(&arc_sums.arcstat_hits); | |
c935fe2e AM |
6843 | as->arcstat_iohits.value.ui64 = |
6844 | wmsum_value(&arc_sums.arcstat_iohits); | |
c4c162c1 AM |
6845 | as->arcstat_misses.value.ui64 = |
6846 | wmsum_value(&arc_sums.arcstat_misses); | |
6847 | as->arcstat_demand_data_hits.value.ui64 = | |
6848 | wmsum_value(&arc_sums.arcstat_demand_data_hits); | |
c935fe2e AM |
6849 | as->arcstat_demand_data_iohits.value.ui64 = |
6850 | wmsum_value(&arc_sums.arcstat_demand_data_iohits); | |
c4c162c1 AM |
6851 | as->arcstat_demand_data_misses.value.ui64 = |
6852 | wmsum_value(&arc_sums.arcstat_demand_data_misses); | |
6853 | as->arcstat_demand_metadata_hits.value.ui64 = | |
6854 | wmsum_value(&arc_sums.arcstat_demand_metadata_hits); | |
c935fe2e AM |
6855 | as->arcstat_demand_metadata_iohits.value.ui64 = |
6856 | wmsum_value(&arc_sums.arcstat_demand_metadata_iohits); | |
c4c162c1 AM |
6857 | as->arcstat_demand_metadata_misses.value.ui64 = |
6858 | wmsum_value(&arc_sums.arcstat_demand_metadata_misses); | |
6859 | as->arcstat_prefetch_data_hits.value.ui64 = | |
6860 | wmsum_value(&arc_sums.arcstat_prefetch_data_hits); | |
c935fe2e AM |
6861 | as->arcstat_prefetch_data_iohits.value.ui64 = |
6862 | wmsum_value(&arc_sums.arcstat_prefetch_data_iohits); | |
c4c162c1 AM |
6863 | as->arcstat_prefetch_data_misses.value.ui64 = |
6864 | wmsum_value(&arc_sums.arcstat_prefetch_data_misses); | |
6865 | as->arcstat_prefetch_metadata_hits.value.ui64 = | |
6866 | wmsum_value(&arc_sums.arcstat_prefetch_metadata_hits); | |
c935fe2e AM |
6867 | as->arcstat_prefetch_metadata_iohits.value.ui64 = |
6868 | wmsum_value(&arc_sums.arcstat_prefetch_metadata_iohits); | |
c4c162c1 AM |
6869 | as->arcstat_prefetch_metadata_misses.value.ui64 = |
6870 | wmsum_value(&arc_sums.arcstat_prefetch_metadata_misses); | |
6871 | as->arcstat_mru_hits.value.ui64 = | |
6872 | wmsum_value(&arc_sums.arcstat_mru_hits); | |
6873 | as->arcstat_mru_ghost_hits.value.ui64 = | |
6874 | wmsum_value(&arc_sums.arcstat_mru_ghost_hits); | |
6875 | as->arcstat_mfu_hits.value.ui64 = | |
6876 | wmsum_value(&arc_sums.arcstat_mfu_hits); | |
6877 | as->arcstat_mfu_ghost_hits.value.ui64 = | |
6878 | wmsum_value(&arc_sums.arcstat_mfu_ghost_hits); | |
ed2f7ba0 AM |
6879 | as->arcstat_uncached_hits.value.ui64 = |
6880 | wmsum_value(&arc_sums.arcstat_uncached_hits); | |
c4c162c1 AM |
6881 | as->arcstat_deleted.value.ui64 = |
6882 | wmsum_value(&arc_sums.arcstat_deleted); | |
6883 | as->arcstat_mutex_miss.value.ui64 = | |
6884 | wmsum_value(&arc_sums.arcstat_mutex_miss); | |
6885 | as->arcstat_access_skip.value.ui64 = | |
6886 | wmsum_value(&arc_sums.arcstat_access_skip); | |
6887 | as->arcstat_evict_skip.value.ui64 = | |
6888 | wmsum_value(&arc_sums.arcstat_evict_skip); | |
6889 | as->arcstat_evict_not_enough.value.ui64 = | |
6890 | wmsum_value(&arc_sums.arcstat_evict_not_enough); | |
6891 | as->arcstat_evict_l2_cached.value.ui64 = | |
6892 | wmsum_value(&arc_sums.arcstat_evict_l2_cached); | |
6893 | as->arcstat_evict_l2_eligible.value.ui64 = | |
6894 | wmsum_value(&arc_sums.arcstat_evict_l2_eligible); | |
6895 | as->arcstat_evict_l2_eligible_mfu.value.ui64 = | |
6896 | wmsum_value(&arc_sums.arcstat_evict_l2_eligible_mfu); | |
6897 | as->arcstat_evict_l2_eligible_mru.value.ui64 = | |
6898 | wmsum_value(&arc_sums.arcstat_evict_l2_eligible_mru); | |
6899 | as->arcstat_evict_l2_ineligible.value.ui64 = | |
6900 | wmsum_value(&arc_sums.arcstat_evict_l2_ineligible); | |
6901 | as->arcstat_evict_l2_skip.value.ui64 = | |
6902 | wmsum_value(&arc_sums.arcstat_evict_l2_skip); | |
6903 | as->arcstat_hash_collisions.value.ui64 = | |
6904 | wmsum_value(&arc_sums.arcstat_hash_collisions); | |
6905 | as->arcstat_hash_chains.value.ui64 = | |
6906 | wmsum_value(&arc_sums.arcstat_hash_chains); | |
6907 | as->arcstat_size.value.ui64 = | |
6908 | aggsum_value(&arc_sums.arcstat_size); | |
6909 | as->arcstat_compressed_size.value.ui64 = | |
6910 | wmsum_value(&arc_sums.arcstat_compressed_size); | |
6911 | as->arcstat_uncompressed_size.value.ui64 = | |
6912 | wmsum_value(&arc_sums.arcstat_uncompressed_size); | |
6913 | as->arcstat_overhead_size.value.ui64 = | |
6914 | wmsum_value(&arc_sums.arcstat_overhead_size); | |
6915 | as->arcstat_hdr_size.value.ui64 = | |
6916 | wmsum_value(&arc_sums.arcstat_hdr_size); | |
6917 | as->arcstat_data_size.value.ui64 = | |
6918 | wmsum_value(&arc_sums.arcstat_data_size); | |
6919 | as->arcstat_metadata_size.value.ui64 = | |
6920 | wmsum_value(&arc_sums.arcstat_metadata_size); | |
6921 | as->arcstat_dbuf_size.value.ui64 = | |
6922 | wmsum_value(&arc_sums.arcstat_dbuf_size); | |
1c2725a1 | 6923 | #if defined(COMPAT_FREEBSD11) |
c4c162c1 AM |
6924 | as->arcstat_other_size.value.ui64 = |
6925 | wmsum_value(&arc_sums.arcstat_bonus_size) + | |
a8d83e2a | 6926 | wmsum_value(&arc_sums.arcstat_dnode_size) + |
c4c162c1 | 6927 | wmsum_value(&arc_sums.arcstat_dbuf_size); |
1c2725a1 | 6928 | #endif |
37fb3e43 | 6929 | |
c4c162c1 AM |
6930 | arc_kstat_update_state(arc_anon, |
6931 | &as->arcstat_anon_size, | |
a8d83e2a AM |
6932 | &as->arcstat_anon_data, |
6933 | &as->arcstat_anon_metadata, | |
c4c162c1 AM |
6934 | &as->arcstat_anon_evictable_data, |
6935 | &as->arcstat_anon_evictable_metadata); | |
6936 | arc_kstat_update_state(arc_mru, | |
6937 | &as->arcstat_mru_size, | |
a8d83e2a AM |
6938 | &as->arcstat_mru_data, |
6939 | &as->arcstat_mru_metadata, | |
c4c162c1 AM |
6940 | &as->arcstat_mru_evictable_data, |
6941 | &as->arcstat_mru_evictable_metadata); | |
6942 | arc_kstat_update_state(arc_mru_ghost, | |
6943 | &as->arcstat_mru_ghost_size, | |
a8d83e2a AM |
6944 | &as->arcstat_mru_ghost_data, |
6945 | &as->arcstat_mru_ghost_metadata, | |
c4c162c1 AM |
6946 | &as->arcstat_mru_ghost_evictable_data, |
6947 | &as->arcstat_mru_ghost_evictable_metadata); | |
6948 | arc_kstat_update_state(arc_mfu, | |
6949 | &as->arcstat_mfu_size, | |
a8d83e2a AM |
6950 | &as->arcstat_mfu_data, |
6951 | &as->arcstat_mfu_metadata, | |
c4c162c1 AM |
6952 | &as->arcstat_mfu_evictable_data, |
6953 | &as->arcstat_mfu_evictable_metadata); | |
6954 | arc_kstat_update_state(arc_mfu_ghost, | |
6955 | &as->arcstat_mfu_ghost_size, | |
a8d83e2a AM |
6956 | &as->arcstat_mfu_ghost_data, |
6957 | &as->arcstat_mfu_ghost_metadata, | |
c4c162c1 AM |
6958 | &as->arcstat_mfu_ghost_evictable_data, |
6959 | &as->arcstat_mfu_ghost_evictable_metadata); | |
ed2f7ba0 AM |
6960 | arc_kstat_update_state(arc_uncached, |
6961 | &as->arcstat_uncached_size, | |
a8d83e2a AM |
6962 | &as->arcstat_uncached_data, |
6963 | &as->arcstat_uncached_metadata, | |
ed2f7ba0 AM |
6964 | &as->arcstat_uncached_evictable_data, |
6965 | &as->arcstat_uncached_evictable_metadata); | |
c4c162c1 AM |
6966 | |
6967 | as->arcstat_dnode_size.value.ui64 = | |
a8d83e2a | 6968 | wmsum_value(&arc_sums.arcstat_dnode_size); |
c4c162c1 AM |
6969 | as->arcstat_bonus_size.value.ui64 = |
6970 | wmsum_value(&arc_sums.arcstat_bonus_size); | |
6971 | as->arcstat_l2_hits.value.ui64 = | |
6972 | wmsum_value(&arc_sums.arcstat_l2_hits); | |
6973 | as->arcstat_l2_misses.value.ui64 = | |
6974 | wmsum_value(&arc_sums.arcstat_l2_misses); | |
6975 | as->arcstat_l2_prefetch_asize.value.ui64 = | |
6976 | wmsum_value(&arc_sums.arcstat_l2_prefetch_asize); | |
6977 | as->arcstat_l2_mru_asize.value.ui64 = | |
6978 | wmsum_value(&arc_sums.arcstat_l2_mru_asize); | |
6979 | as->arcstat_l2_mfu_asize.value.ui64 = | |
6980 | wmsum_value(&arc_sums.arcstat_l2_mfu_asize); | |
6981 | as->arcstat_l2_bufc_data_asize.value.ui64 = | |
6982 | wmsum_value(&arc_sums.arcstat_l2_bufc_data_asize); | |
6983 | as->arcstat_l2_bufc_metadata_asize.value.ui64 = | |
6984 | wmsum_value(&arc_sums.arcstat_l2_bufc_metadata_asize); | |
6985 | as->arcstat_l2_feeds.value.ui64 = | |
6986 | wmsum_value(&arc_sums.arcstat_l2_feeds); | |
6987 | as->arcstat_l2_rw_clash.value.ui64 = | |
6988 | wmsum_value(&arc_sums.arcstat_l2_rw_clash); | |
6989 | as->arcstat_l2_read_bytes.value.ui64 = | |
6990 | wmsum_value(&arc_sums.arcstat_l2_read_bytes); | |
6991 | as->arcstat_l2_write_bytes.value.ui64 = | |
6992 | wmsum_value(&arc_sums.arcstat_l2_write_bytes); | |
6993 | as->arcstat_l2_writes_sent.value.ui64 = | |
6994 | wmsum_value(&arc_sums.arcstat_l2_writes_sent); | |
6995 | as->arcstat_l2_writes_done.value.ui64 = | |
6996 | wmsum_value(&arc_sums.arcstat_l2_writes_done); | |
6997 | as->arcstat_l2_writes_error.value.ui64 = | |
6998 | wmsum_value(&arc_sums.arcstat_l2_writes_error); | |
6999 | as->arcstat_l2_writes_lock_retry.value.ui64 = | |
7000 | wmsum_value(&arc_sums.arcstat_l2_writes_lock_retry); | |
7001 | as->arcstat_l2_evict_lock_retry.value.ui64 = | |
7002 | wmsum_value(&arc_sums.arcstat_l2_evict_lock_retry); | |
7003 | as->arcstat_l2_evict_reading.value.ui64 = | |
7004 | wmsum_value(&arc_sums.arcstat_l2_evict_reading); | |
7005 | as->arcstat_l2_evict_l1cached.value.ui64 = | |
7006 | wmsum_value(&arc_sums.arcstat_l2_evict_l1cached); | |
7007 | as->arcstat_l2_free_on_write.value.ui64 = | |
7008 | wmsum_value(&arc_sums.arcstat_l2_free_on_write); | |
7009 | as->arcstat_l2_abort_lowmem.value.ui64 = | |
7010 | wmsum_value(&arc_sums.arcstat_l2_abort_lowmem); | |
7011 | as->arcstat_l2_cksum_bad.value.ui64 = | |
7012 | wmsum_value(&arc_sums.arcstat_l2_cksum_bad); | |
7013 | as->arcstat_l2_io_error.value.ui64 = | |
7014 | wmsum_value(&arc_sums.arcstat_l2_io_error); | |
7015 | as->arcstat_l2_lsize.value.ui64 = | |
7016 | wmsum_value(&arc_sums.arcstat_l2_lsize); | |
7017 | as->arcstat_l2_psize.value.ui64 = | |
7018 | wmsum_value(&arc_sums.arcstat_l2_psize); | |
7019 | as->arcstat_l2_hdr_size.value.ui64 = | |
7020 | aggsum_value(&arc_sums.arcstat_l2_hdr_size); | |
7021 | as->arcstat_l2_log_blk_writes.value.ui64 = | |
7022 | wmsum_value(&arc_sums.arcstat_l2_log_blk_writes); | |
7023 | as->arcstat_l2_log_blk_asize.value.ui64 = | |
7024 | wmsum_value(&arc_sums.arcstat_l2_log_blk_asize); | |
7025 | as->arcstat_l2_log_blk_count.value.ui64 = | |
7026 | wmsum_value(&arc_sums.arcstat_l2_log_blk_count); | |
7027 | as->arcstat_l2_rebuild_success.value.ui64 = | |
7028 | wmsum_value(&arc_sums.arcstat_l2_rebuild_success); | |
7029 | as->arcstat_l2_rebuild_abort_unsupported.value.ui64 = | |
7030 | wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_unsupported); | |
7031 | as->arcstat_l2_rebuild_abort_io_errors.value.ui64 = | |
7032 | wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_io_errors); | |
7033 | as->arcstat_l2_rebuild_abort_dh_errors.value.ui64 = | |
7034 | wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_dh_errors); | |
7035 | as->arcstat_l2_rebuild_abort_cksum_lb_errors.value.ui64 = | |
7036 | wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors); | |
7037 | as->arcstat_l2_rebuild_abort_lowmem.value.ui64 = | |
7038 | wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_lowmem); | |
7039 | as->arcstat_l2_rebuild_size.value.ui64 = | |
7040 | wmsum_value(&arc_sums.arcstat_l2_rebuild_size); | |
7041 | as->arcstat_l2_rebuild_asize.value.ui64 = | |
7042 | wmsum_value(&arc_sums.arcstat_l2_rebuild_asize); | |
7043 | as->arcstat_l2_rebuild_bufs.value.ui64 = | |
7044 | wmsum_value(&arc_sums.arcstat_l2_rebuild_bufs); | |
7045 | as->arcstat_l2_rebuild_bufs_precached.value.ui64 = | |
7046 | wmsum_value(&arc_sums.arcstat_l2_rebuild_bufs_precached); | |
7047 | as->arcstat_l2_rebuild_log_blks.value.ui64 = | |
7048 | wmsum_value(&arc_sums.arcstat_l2_rebuild_log_blks); | |
7049 | as->arcstat_memory_throttle_count.value.ui64 = | |
7050 | wmsum_value(&arc_sums.arcstat_memory_throttle_count); | |
7051 | as->arcstat_memory_direct_count.value.ui64 = | |
7052 | wmsum_value(&arc_sums.arcstat_memory_direct_count); | |
7053 | as->arcstat_memory_indirect_count.value.ui64 = | |
7054 | wmsum_value(&arc_sums.arcstat_memory_indirect_count); | |
7055 | ||
7056 | as->arcstat_memory_all_bytes.value.ui64 = | |
7057 | arc_all_memory(); | |
7058 | as->arcstat_memory_free_bytes.value.ui64 = | |
7059 | arc_free_memory(); | |
7060 | as->arcstat_memory_available_bytes.value.i64 = | |
7061 | arc_available_memory(); | |
7062 | ||
7063 | as->arcstat_prune.value.ui64 = | |
7064 | wmsum_value(&arc_sums.arcstat_prune); | |
7065 | as->arcstat_meta_used.value.ui64 = | |
a8d83e2a | 7066 | wmsum_value(&arc_sums.arcstat_meta_used); |
c4c162c1 AM |
7067 | as->arcstat_async_upgrade_sync.value.ui64 = |
7068 | wmsum_value(&arc_sums.arcstat_async_upgrade_sync); | |
c935fe2e AM |
7069 | as->arcstat_predictive_prefetch.value.ui64 = |
7070 | wmsum_value(&arc_sums.arcstat_predictive_prefetch); | |
c4c162c1 AM |
7071 | as->arcstat_demand_hit_predictive_prefetch.value.ui64 = |
7072 | wmsum_value(&arc_sums.arcstat_demand_hit_predictive_prefetch); | |
c935fe2e AM |
7073 | as->arcstat_demand_iohit_predictive_prefetch.value.ui64 = |
7074 | wmsum_value(&arc_sums.arcstat_demand_iohit_predictive_prefetch); | |
7075 | as->arcstat_prescient_prefetch.value.ui64 = | |
7076 | wmsum_value(&arc_sums.arcstat_prescient_prefetch); | |
c4c162c1 AM |
7077 | as->arcstat_demand_hit_prescient_prefetch.value.ui64 = |
7078 | wmsum_value(&arc_sums.arcstat_demand_hit_prescient_prefetch); | |
c935fe2e AM |
7079 | as->arcstat_demand_iohit_prescient_prefetch.value.ui64 = |
7080 | wmsum_value(&arc_sums.arcstat_demand_iohit_prescient_prefetch); | |
c4c162c1 AM |
7081 | as->arcstat_raw_size.value.ui64 = |
7082 | wmsum_value(&arc_sums.arcstat_raw_size); | |
7083 | as->arcstat_cached_only_in_progress.value.ui64 = | |
7084 | wmsum_value(&arc_sums.arcstat_cached_only_in_progress); | |
7085 | as->arcstat_abd_chunk_waste_size.value.ui64 = | |
7086 | wmsum_value(&arc_sums.arcstat_abd_chunk_waste_size); | |
13be560d BB |
7087 | |
7088 | return (0); | |
7089 | } | |
7090 | ||
ca0bf58d PS |
7091 | /* |
7092 | * This function *must* return indices evenly distributed between all | |
7093 | * sublists of the multilist. This is needed due to how the ARC eviction | |
7094 | * code is laid out; arc_evict_state() assumes ARC buffers are evenly | |
7095 | * distributed between all sublists and uses this assumption when | |
7096 | * deciding which sublist to evict from and how much to evict from it. | |
7097 | */ | |
65c7cc49 | 7098 | static unsigned int |
ca0bf58d PS |
7099 | arc_state_multilist_index_func(multilist_t *ml, void *obj) |
7100 | { | |
7101 | arc_buf_hdr_t *hdr = obj; | |
7102 | ||
7103 | /* | |
7104 | * We rely on b_dva to generate evenly distributed index | |
7105 | * numbers using buf_hash below. So, as an added precaution, | |
7106 | * let's make sure we never add empty buffers to the arc lists. | |
7107 | */ | |
d3c2ae1c | 7108 | ASSERT(!HDR_EMPTY(hdr)); |
ca0bf58d PS |
7109 | |
7110 | /* | |
7111 | * The assumption here, is the hash value for a given | |
7112 | * arc_buf_hdr_t will remain constant throughout its lifetime | |
7113 | * (i.e. its b_spa, b_dva, and b_birth fields don't change). | |
7114 | * Thus, we don't need to store the header's sublist index | |
7115 | * on insertion, as this index can be recalculated on removal. | |
7116 | * | |
7117 | * Also, the low order bits of the hash value are thought to be | |
7118 | * distributed evenly. Otherwise, in the case that the multilist | |
7119 | * has a power of two number of sublists, each sublists' usage | |
5b7053a9 AM |
7120 | * would not be evenly distributed. In this context full 64bit |
7121 | * division would be a waste of time, so limit it to 32 bits. | |
ca0bf58d | 7122 | */ |
5b7053a9 | 7123 | return ((unsigned int)buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % |
ca0bf58d PS |
7124 | multilist_get_num_sublists(ml)); |
7125 | } | |
7126 | ||
bb7ad5d3 AM |
7127 | static unsigned int |
7128 | arc_state_l2c_multilist_index_func(multilist_t *ml, void *obj) | |
7129 | { | |
7130 | panic("Header %p insert into arc_l2c_only %p", obj, ml); | |
7131 | } | |
7132 | ||
36a6e233 RM |
7133 | #define WARN_IF_TUNING_IGNORED(tuning, value, do_warn) do { \ |
7134 | if ((do_warn) && (tuning) && ((tuning) != (value))) { \ | |
7135 | cmn_err(CE_WARN, \ | |
7136 | "ignoring tunable %s (using %llu instead)", \ | |
5dbf6c5a | 7137 | (#tuning), (u_longlong_t)(value)); \ |
36a6e233 RM |
7138 | } \ |
7139 | } while (0) | |
7140 | ||
ca67b33a MA |
7141 | /* |
7142 | * Called during module initialization and periodically thereafter to | |
e3570464 | 7143 | * apply reasonable changes to the exposed performance tunings. Can also be |
7144 | * called explicitly by param_set_arc_*() functions when ARC tunables are | |
7145 | * updated manually. Non-zero zfs_* values which differ from the currently set | |
7146 | * values will be applied. | |
ca67b33a | 7147 | */ |
e3570464 | 7148 | void |
36a6e233 | 7149 | arc_tuning_update(boolean_t verbose) |
ca67b33a | 7150 | { |
b8a97fb1 | 7151 | uint64_t allmem = arc_all_memory(); |
9edb3695 | 7152 | |
36a6e233 RM |
7153 | /* Valid range: 32M - <arc_c_max> */ |
7154 | if ((zfs_arc_min) && (zfs_arc_min != arc_c_min) && | |
7155 | (zfs_arc_min >= 2ULL << SPA_MAXBLOCKSHIFT) && | |
7156 | (zfs_arc_min <= arc_c_max)) { | |
7157 | arc_c_min = zfs_arc_min; | |
7158 | arc_c = MAX(arc_c, arc_c_min); | |
7159 | } | |
7160 | WARN_IF_TUNING_IGNORED(zfs_arc_min, arc_c_min, verbose); | |
7161 | ||
ca67b33a MA |
7162 | /* Valid range: 64M - <all physical memory> */ |
7163 | if ((zfs_arc_max) && (zfs_arc_max != arc_c_max) && | |
e945e8d7 | 7164 | (zfs_arc_max >= MIN_ARC_MAX) && (zfs_arc_max < allmem) && |
ca67b33a MA |
7165 | (zfs_arc_max > arc_c_min)) { |
7166 | arc_c_max = zfs_arc_max; | |
17ca3018 | 7167 | arc_c = MIN(arc_c, arc_c_max); |
a8d83e2a AM |
7168 | if (arc_dnode_limit > arc_c_max) |
7169 | arc_dnode_limit = arc_c_max; | |
ca67b33a | 7170 | } |
36a6e233 | 7171 | WARN_IF_TUNING_IGNORED(zfs_arc_max, arc_c_max, verbose); |
ca67b33a | 7172 | |
a8d83e2a AM |
7173 | /* Valid range: 0 - <all physical memory> */ |
7174 | arc_dnode_limit = zfs_arc_dnode_limit ? zfs_arc_dnode_limit : | |
7175 | MIN(zfs_arc_dnode_limit_percent, 100) * arc_c_max / 100; | |
7176 | WARN_IF_TUNING_IGNORED(zfs_arc_dnode_limit, arc_dnode_limit, verbose); | |
25458cbe | 7177 | |
ca67b33a MA |
7178 | /* Valid range: 1 - N */ |
7179 | if (zfs_arc_grow_retry) | |
7180 | arc_grow_retry = zfs_arc_grow_retry; | |
7181 | ||
7182 | /* Valid range: 1 - N */ | |
7183 | if (zfs_arc_shrink_shift) { | |
7184 | arc_shrink_shift = zfs_arc_shrink_shift; | |
7185 | arc_no_grow_shift = MIN(arc_no_grow_shift, arc_shrink_shift -1); | |
7186 | } | |
7187 | ||
d4a72f23 TC |
7188 | /* Valid range: 1 - N ms */ |
7189 | if (zfs_arc_min_prefetch_ms) | |
7190 | arc_min_prefetch_ms = zfs_arc_min_prefetch_ms; | |
7191 | ||
7192 | /* Valid range: 1 - N ms */ | |
7193 | if (zfs_arc_min_prescient_prefetch_ms) { | |
7194 | arc_min_prescient_prefetch_ms = | |
7195 | zfs_arc_min_prescient_prefetch_ms; | |
7196 | } | |
11f552fa | 7197 | |
7e8bddd0 | 7198 | /* Valid range: 0 - 100 */ |
fdc2d303 | 7199 | if (zfs_arc_lotsfree_percent <= 100) |
7e8bddd0 | 7200 | arc_lotsfree_percent = zfs_arc_lotsfree_percent; |
36a6e233 RM |
7201 | WARN_IF_TUNING_IGNORED(zfs_arc_lotsfree_percent, arc_lotsfree_percent, |
7202 | verbose); | |
7e8bddd0 | 7203 | |
11f552fa BB |
7204 | /* Valid range: 0 - <all physical memory> */ |
7205 | if ((zfs_arc_sys_free) && (zfs_arc_sys_free != arc_sys_free)) | |
8ef15f93 | 7206 | arc_sys_free = MIN(zfs_arc_sys_free, allmem); |
36a6e233 | 7207 | WARN_IF_TUNING_IGNORED(zfs_arc_sys_free, arc_sys_free, verbose); |
ca67b33a MA |
7208 | } |
7209 | ||
6e2a5918 MJ |
7210 | static void |
7211 | arc_state_multilist_init(multilist_t *ml, | |
7212 | multilist_sublist_index_func_t *index_func, int *maxcountp) | |
7213 | { | |
7214 | multilist_create(ml, sizeof (arc_buf_hdr_t), | |
7215 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), index_func); | |
7216 | *maxcountp = MAX(*maxcountp, multilist_get_num_sublists(ml)); | |
7217 | } | |
7218 | ||
d3c2ae1c GW |
7219 | static void |
7220 | arc_state_init(void) | |
7221 | { | |
6e2a5918 MJ |
7222 | int num_sublists = 0; |
7223 | ||
7224 | arc_state_multilist_init(&arc_mru->arcs_list[ARC_BUFC_METADATA], | |
7225 | arc_state_multilist_index_func, &num_sublists); | |
7226 | arc_state_multilist_init(&arc_mru->arcs_list[ARC_BUFC_DATA], | |
7227 | arc_state_multilist_index_func, &num_sublists); | |
7228 | arc_state_multilist_init(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], | |
7229 | arc_state_multilist_index_func, &num_sublists); | |
7230 | arc_state_multilist_init(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], | |
7231 | arc_state_multilist_index_func, &num_sublists); | |
7232 | arc_state_multilist_init(&arc_mfu->arcs_list[ARC_BUFC_METADATA], | |
7233 | arc_state_multilist_index_func, &num_sublists); | |
7234 | arc_state_multilist_init(&arc_mfu->arcs_list[ARC_BUFC_DATA], | |
7235 | arc_state_multilist_index_func, &num_sublists); | |
7236 | arc_state_multilist_init(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], | |
7237 | arc_state_multilist_index_func, &num_sublists); | |
7238 | arc_state_multilist_init(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], | |
7239 | arc_state_multilist_index_func, &num_sublists); | |
ed2f7ba0 AM |
7240 | arc_state_multilist_init(&arc_uncached->arcs_list[ARC_BUFC_METADATA], |
7241 | arc_state_multilist_index_func, &num_sublists); | |
7242 | arc_state_multilist_init(&arc_uncached->arcs_list[ARC_BUFC_DATA], | |
7243 | arc_state_multilist_index_func, &num_sublists); | |
6e2a5918 | 7244 | |
bb7ad5d3 AM |
7245 | /* |
7246 | * L2 headers should never be on the L2 state list since they don't | |
7247 | * have L1 headers allocated. Special index function asserts that. | |
7248 | */ | |
6e2a5918 MJ |
7249 | arc_state_multilist_init(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], |
7250 | arc_state_l2c_multilist_index_func, &num_sublists); | |
7251 | arc_state_multilist_init(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], | |
7252 | arc_state_l2c_multilist_index_func, &num_sublists); | |
7253 | ||
7254 | /* | |
7255 | * Keep track of the number of markers needed to reclaim buffers from | |
7256 | * any ARC state. The markers will be pre-allocated so as to minimize | |
7257 | * the number of memory allocations performed by the eviction thread. | |
7258 | */ | |
7259 | arc_state_evict_marker_count = num_sublists; | |
d3c2ae1c | 7260 | |
424fd7c3 TS |
7261 | zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); |
7262 | zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]); | |
7263 | zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); | |
7264 | zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]); | |
7265 | zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7266 | zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7267 | zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); | |
7268 | zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); | |
7269 | zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7270 | zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7271 | zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); | |
7272 | zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); | |
ed2f7ba0 AM |
7273 | zfs_refcount_create(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]); |
7274 | zfs_refcount_create(&arc_uncached->arcs_esize[ARC_BUFC_DATA]); | |
424fd7c3 | 7275 | |
a8d83e2a AM |
7276 | zfs_refcount_create(&arc_anon->arcs_size[ARC_BUFC_DATA]); |
7277 | zfs_refcount_create(&arc_anon->arcs_size[ARC_BUFC_METADATA]); | |
7278 | zfs_refcount_create(&arc_mru->arcs_size[ARC_BUFC_DATA]); | |
7279 | zfs_refcount_create(&arc_mru->arcs_size[ARC_BUFC_METADATA]); | |
7280 | zfs_refcount_create(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]); | |
7281 | zfs_refcount_create(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]); | |
7282 | zfs_refcount_create(&arc_mfu->arcs_size[ARC_BUFC_DATA]); | |
7283 | zfs_refcount_create(&arc_mfu->arcs_size[ARC_BUFC_METADATA]); | |
7284 | zfs_refcount_create(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]); | |
7285 | zfs_refcount_create(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]); | |
7286 | zfs_refcount_create(&arc_l2c_only->arcs_size[ARC_BUFC_DATA]); | |
7287 | zfs_refcount_create(&arc_l2c_only->arcs_size[ARC_BUFC_METADATA]); | |
7288 | zfs_refcount_create(&arc_uncached->arcs_size[ARC_BUFC_DATA]); | |
7289 | zfs_refcount_create(&arc_uncached->arcs_size[ARC_BUFC_METADATA]); | |
7290 | ||
7291 | wmsum_init(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA], 0); | |
7292 | wmsum_init(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA], 0); | |
7293 | wmsum_init(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA], 0); | |
7294 | wmsum_init(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA], 0); | |
d3c2ae1c | 7295 | |
c4c162c1 | 7296 | wmsum_init(&arc_sums.arcstat_hits, 0); |
c935fe2e | 7297 | wmsum_init(&arc_sums.arcstat_iohits, 0); |
c4c162c1 AM |
7298 | wmsum_init(&arc_sums.arcstat_misses, 0); |
7299 | wmsum_init(&arc_sums.arcstat_demand_data_hits, 0); | |
c935fe2e | 7300 | wmsum_init(&arc_sums.arcstat_demand_data_iohits, 0); |
c4c162c1 AM |
7301 | wmsum_init(&arc_sums.arcstat_demand_data_misses, 0); |
7302 | wmsum_init(&arc_sums.arcstat_demand_metadata_hits, 0); | |
c935fe2e | 7303 | wmsum_init(&arc_sums.arcstat_demand_metadata_iohits, 0); |
c4c162c1 AM |
7304 | wmsum_init(&arc_sums.arcstat_demand_metadata_misses, 0); |
7305 | wmsum_init(&arc_sums.arcstat_prefetch_data_hits, 0); | |
c935fe2e | 7306 | wmsum_init(&arc_sums.arcstat_prefetch_data_iohits, 0); |
c4c162c1 AM |
7307 | wmsum_init(&arc_sums.arcstat_prefetch_data_misses, 0); |
7308 | wmsum_init(&arc_sums.arcstat_prefetch_metadata_hits, 0); | |
c935fe2e | 7309 | wmsum_init(&arc_sums.arcstat_prefetch_metadata_iohits, 0); |
c4c162c1 AM |
7310 | wmsum_init(&arc_sums.arcstat_prefetch_metadata_misses, 0); |
7311 | wmsum_init(&arc_sums.arcstat_mru_hits, 0); | |
7312 | wmsum_init(&arc_sums.arcstat_mru_ghost_hits, 0); | |
7313 | wmsum_init(&arc_sums.arcstat_mfu_hits, 0); | |
7314 | wmsum_init(&arc_sums.arcstat_mfu_ghost_hits, 0); | |
ed2f7ba0 | 7315 | wmsum_init(&arc_sums.arcstat_uncached_hits, 0); |
c4c162c1 AM |
7316 | wmsum_init(&arc_sums.arcstat_deleted, 0); |
7317 | wmsum_init(&arc_sums.arcstat_mutex_miss, 0); | |
7318 | wmsum_init(&arc_sums.arcstat_access_skip, 0); | |
7319 | wmsum_init(&arc_sums.arcstat_evict_skip, 0); | |
7320 | wmsum_init(&arc_sums.arcstat_evict_not_enough, 0); | |
7321 | wmsum_init(&arc_sums.arcstat_evict_l2_cached, 0); | |
7322 | wmsum_init(&arc_sums.arcstat_evict_l2_eligible, 0); | |
7323 | wmsum_init(&arc_sums.arcstat_evict_l2_eligible_mfu, 0); | |
7324 | wmsum_init(&arc_sums.arcstat_evict_l2_eligible_mru, 0); | |
7325 | wmsum_init(&arc_sums.arcstat_evict_l2_ineligible, 0); | |
7326 | wmsum_init(&arc_sums.arcstat_evict_l2_skip, 0); | |
7327 | wmsum_init(&arc_sums.arcstat_hash_collisions, 0); | |
7328 | wmsum_init(&arc_sums.arcstat_hash_chains, 0); | |
7329 | aggsum_init(&arc_sums.arcstat_size, 0); | |
7330 | wmsum_init(&arc_sums.arcstat_compressed_size, 0); | |
7331 | wmsum_init(&arc_sums.arcstat_uncompressed_size, 0); | |
7332 | wmsum_init(&arc_sums.arcstat_overhead_size, 0); | |
7333 | wmsum_init(&arc_sums.arcstat_hdr_size, 0); | |
7334 | wmsum_init(&arc_sums.arcstat_data_size, 0); | |
7335 | wmsum_init(&arc_sums.arcstat_metadata_size, 0); | |
7336 | wmsum_init(&arc_sums.arcstat_dbuf_size, 0); | |
a8d83e2a | 7337 | wmsum_init(&arc_sums.arcstat_dnode_size, 0); |
c4c162c1 AM |
7338 | wmsum_init(&arc_sums.arcstat_bonus_size, 0); |
7339 | wmsum_init(&arc_sums.arcstat_l2_hits, 0); | |
7340 | wmsum_init(&arc_sums.arcstat_l2_misses, 0); | |
7341 | wmsum_init(&arc_sums.arcstat_l2_prefetch_asize, 0); | |
7342 | wmsum_init(&arc_sums.arcstat_l2_mru_asize, 0); | |
7343 | wmsum_init(&arc_sums.arcstat_l2_mfu_asize, 0); | |
7344 | wmsum_init(&arc_sums.arcstat_l2_bufc_data_asize, 0); | |
7345 | wmsum_init(&arc_sums.arcstat_l2_bufc_metadata_asize, 0); | |
7346 | wmsum_init(&arc_sums.arcstat_l2_feeds, 0); | |
7347 | wmsum_init(&arc_sums.arcstat_l2_rw_clash, 0); | |
7348 | wmsum_init(&arc_sums.arcstat_l2_read_bytes, 0); | |
7349 | wmsum_init(&arc_sums.arcstat_l2_write_bytes, 0); | |
7350 | wmsum_init(&arc_sums.arcstat_l2_writes_sent, 0); | |
7351 | wmsum_init(&arc_sums.arcstat_l2_writes_done, 0); | |
7352 | wmsum_init(&arc_sums.arcstat_l2_writes_error, 0); | |
7353 | wmsum_init(&arc_sums.arcstat_l2_writes_lock_retry, 0); | |
7354 | wmsum_init(&arc_sums.arcstat_l2_evict_lock_retry, 0); | |
7355 | wmsum_init(&arc_sums.arcstat_l2_evict_reading, 0); | |
7356 | wmsum_init(&arc_sums.arcstat_l2_evict_l1cached, 0); | |
7357 | wmsum_init(&arc_sums.arcstat_l2_free_on_write, 0); | |
7358 | wmsum_init(&arc_sums.arcstat_l2_abort_lowmem, 0); | |
7359 | wmsum_init(&arc_sums.arcstat_l2_cksum_bad, 0); | |
7360 | wmsum_init(&arc_sums.arcstat_l2_io_error, 0); | |
7361 | wmsum_init(&arc_sums.arcstat_l2_lsize, 0); | |
7362 | wmsum_init(&arc_sums.arcstat_l2_psize, 0); | |
7363 | aggsum_init(&arc_sums.arcstat_l2_hdr_size, 0); | |
7364 | wmsum_init(&arc_sums.arcstat_l2_log_blk_writes, 0); | |
7365 | wmsum_init(&arc_sums.arcstat_l2_log_blk_asize, 0); | |
7366 | wmsum_init(&arc_sums.arcstat_l2_log_blk_count, 0); | |
7367 | wmsum_init(&arc_sums.arcstat_l2_rebuild_success, 0); | |
7368 | wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_unsupported, 0); | |
7369 | wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_io_errors, 0); | |
7370 | wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_dh_errors, 0); | |
7371 | wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors, 0); | |
7372 | wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_lowmem, 0); | |
7373 | wmsum_init(&arc_sums.arcstat_l2_rebuild_size, 0); | |
7374 | wmsum_init(&arc_sums.arcstat_l2_rebuild_asize, 0); | |
7375 | wmsum_init(&arc_sums.arcstat_l2_rebuild_bufs, 0); | |
7376 | wmsum_init(&arc_sums.arcstat_l2_rebuild_bufs_precached, 0); | |
7377 | wmsum_init(&arc_sums.arcstat_l2_rebuild_log_blks, 0); | |
7378 | wmsum_init(&arc_sums.arcstat_memory_throttle_count, 0); | |
7379 | wmsum_init(&arc_sums.arcstat_memory_direct_count, 0); | |
7380 | wmsum_init(&arc_sums.arcstat_memory_indirect_count, 0); | |
7381 | wmsum_init(&arc_sums.arcstat_prune, 0); | |
a8d83e2a | 7382 | wmsum_init(&arc_sums.arcstat_meta_used, 0); |
c4c162c1 | 7383 | wmsum_init(&arc_sums.arcstat_async_upgrade_sync, 0); |
c935fe2e | 7384 | wmsum_init(&arc_sums.arcstat_predictive_prefetch, 0); |
c4c162c1 | 7385 | wmsum_init(&arc_sums.arcstat_demand_hit_predictive_prefetch, 0); |
c935fe2e AM |
7386 | wmsum_init(&arc_sums.arcstat_demand_iohit_predictive_prefetch, 0); |
7387 | wmsum_init(&arc_sums.arcstat_prescient_prefetch, 0); | |
c4c162c1 | 7388 | wmsum_init(&arc_sums.arcstat_demand_hit_prescient_prefetch, 0); |
c935fe2e | 7389 | wmsum_init(&arc_sums.arcstat_demand_iohit_prescient_prefetch, 0); |
c4c162c1 AM |
7390 | wmsum_init(&arc_sums.arcstat_raw_size, 0); |
7391 | wmsum_init(&arc_sums.arcstat_cached_only_in_progress, 0); | |
7392 | wmsum_init(&arc_sums.arcstat_abd_chunk_waste_size, 0); | |
37fb3e43 | 7393 | |
d3c2ae1c GW |
7394 | arc_anon->arcs_state = ARC_STATE_ANON; |
7395 | arc_mru->arcs_state = ARC_STATE_MRU; | |
7396 | arc_mru_ghost->arcs_state = ARC_STATE_MRU_GHOST; | |
7397 | arc_mfu->arcs_state = ARC_STATE_MFU; | |
7398 | arc_mfu_ghost->arcs_state = ARC_STATE_MFU_GHOST; | |
7399 | arc_l2c_only->arcs_state = ARC_STATE_L2C_ONLY; | |
ed2f7ba0 | 7400 | arc_uncached->arcs_state = ARC_STATE_UNCACHED; |
d3c2ae1c GW |
7401 | } |
7402 | ||
7403 | static void | |
7404 | arc_state_fini(void) | |
7405 | { | |
424fd7c3 TS |
7406 | zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); |
7407 | zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]); | |
7408 | zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); | |
7409 | zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]); | |
7410 | zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7411 | zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7412 | zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); | |
7413 | zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); | |
7414 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7415 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7416 | zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); | |
7417 | zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); | |
ed2f7ba0 AM |
7418 | zfs_refcount_destroy(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]); |
7419 | zfs_refcount_destroy(&arc_uncached->arcs_esize[ARC_BUFC_DATA]); | |
424fd7c3 | 7420 | |
a8d83e2a AM |
7421 | zfs_refcount_destroy(&arc_anon->arcs_size[ARC_BUFC_DATA]); |
7422 | zfs_refcount_destroy(&arc_anon->arcs_size[ARC_BUFC_METADATA]); | |
7423 | zfs_refcount_destroy(&arc_mru->arcs_size[ARC_BUFC_DATA]); | |
7424 | zfs_refcount_destroy(&arc_mru->arcs_size[ARC_BUFC_METADATA]); | |
7425 | zfs_refcount_destroy(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]); | |
7426 | zfs_refcount_destroy(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]); | |
7427 | zfs_refcount_destroy(&arc_mfu->arcs_size[ARC_BUFC_DATA]); | |
7428 | zfs_refcount_destroy(&arc_mfu->arcs_size[ARC_BUFC_METADATA]); | |
7429 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]); | |
7430 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]); | |
7431 | zfs_refcount_destroy(&arc_l2c_only->arcs_size[ARC_BUFC_DATA]); | |
7432 | zfs_refcount_destroy(&arc_l2c_only->arcs_size[ARC_BUFC_METADATA]); | |
7433 | zfs_refcount_destroy(&arc_uncached->arcs_size[ARC_BUFC_DATA]); | |
7434 | zfs_refcount_destroy(&arc_uncached->arcs_size[ARC_BUFC_METADATA]); | |
d3c2ae1c | 7435 | |
ffdf019c AM |
7436 | multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); |
7437 | multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); | |
7438 | multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); | |
7439 | multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); | |
7440 | multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); | |
7441 | multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); | |
7442 | multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); | |
7443 | multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); | |
7444 | multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA]); | |
7445 | multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]); | |
ed2f7ba0 AM |
7446 | multilist_destroy(&arc_uncached->arcs_list[ARC_BUFC_METADATA]); |
7447 | multilist_destroy(&arc_uncached->arcs_list[ARC_BUFC_DATA]); | |
37fb3e43 | 7448 | |
a8d83e2a AM |
7449 | wmsum_fini(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA]); |
7450 | wmsum_fini(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA]); | |
7451 | wmsum_fini(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA]); | |
7452 | wmsum_fini(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA]); | |
7453 | ||
c4c162c1 | 7454 | wmsum_fini(&arc_sums.arcstat_hits); |
c935fe2e | 7455 | wmsum_fini(&arc_sums.arcstat_iohits); |
c4c162c1 AM |
7456 | wmsum_fini(&arc_sums.arcstat_misses); |
7457 | wmsum_fini(&arc_sums.arcstat_demand_data_hits); | |
c935fe2e | 7458 | wmsum_fini(&arc_sums.arcstat_demand_data_iohits); |
c4c162c1 AM |
7459 | wmsum_fini(&arc_sums.arcstat_demand_data_misses); |
7460 | wmsum_fini(&arc_sums.arcstat_demand_metadata_hits); | |
c935fe2e | 7461 | wmsum_fini(&arc_sums.arcstat_demand_metadata_iohits); |
c4c162c1 AM |
7462 | wmsum_fini(&arc_sums.arcstat_demand_metadata_misses); |
7463 | wmsum_fini(&arc_sums.arcstat_prefetch_data_hits); | |
c935fe2e | 7464 | wmsum_fini(&arc_sums.arcstat_prefetch_data_iohits); |
c4c162c1 AM |
7465 | wmsum_fini(&arc_sums.arcstat_prefetch_data_misses); |
7466 | wmsum_fini(&arc_sums.arcstat_prefetch_metadata_hits); | |
c935fe2e | 7467 | wmsum_fini(&arc_sums.arcstat_prefetch_metadata_iohits); |
c4c162c1 AM |
7468 | wmsum_fini(&arc_sums.arcstat_prefetch_metadata_misses); |
7469 | wmsum_fini(&arc_sums.arcstat_mru_hits); | |
7470 | wmsum_fini(&arc_sums.arcstat_mru_ghost_hits); | |
7471 | wmsum_fini(&arc_sums.arcstat_mfu_hits); | |
7472 | wmsum_fini(&arc_sums.arcstat_mfu_ghost_hits); | |
ed2f7ba0 | 7473 | wmsum_fini(&arc_sums.arcstat_uncached_hits); |
c4c162c1 AM |
7474 | wmsum_fini(&arc_sums.arcstat_deleted); |
7475 | wmsum_fini(&arc_sums.arcstat_mutex_miss); | |
7476 | wmsum_fini(&arc_sums.arcstat_access_skip); | |
7477 | wmsum_fini(&arc_sums.arcstat_evict_skip); | |
7478 | wmsum_fini(&arc_sums.arcstat_evict_not_enough); | |
7479 | wmsum_fini(&arc_sums.arcstat_evict_l2_cached); | |
7480 | wmsum_fini(&arc_sums.arcstat_evict_l2_eligible); | |
7481 | wmsum_fini(&arc_sums.arcstat_evict_l2_eligible_mfu); | |
7482 | wmsum_fini(&arc_sums.arcstat_evict_l2_eligible_mru); | |
7483 | wmsum_fini(&arc_sums.arcstat_evict_l2_ineligible); | |
7484 | wmsum_fini(&arc_sums.arcstat_evict_l2_skip); | |
7485 | wmsum_fini(&arc_sums.arcstat_hash_collisions); | |
7486 | wmsum_fini(&arc_sums.arcstat_hash_chains); | |
7487 | aggsum_fini(&arc_sums.arcstat_size); | |
7488 | wmsum_fini(&arc_sums.arcstat_compressed_size); | |
7489 | wmsum_fini(&arc_sums.arcstat_uncompressed_size); | |
7490 | wmsum_fini(&arc_sums.arcstat_overhead_size); | |
7491 | wmsum_fini(&arc_sums.arcstat_hdr_size); | |
7492 | wmsum_fini(&arc_sums.arcstat_data_size); | |
7493 | wmsum_fini(&arc_sums.arcstat_metadata_size); | |
7494 | wmsum_fini(&arc_sums.arcstat_dbuf_size); | |
a8d83e2a | 7495 | wmsum_fini(&arc_sums.arcstat_dnode_size); |
c4c162c1 AM |
7496 | wmsum_fini(&arc_sums.arcstat_bonus_size); |
7497 | wmsum_fini(&arc_sums.arcstat_l2_hits); | |
7498 | wmsum_fini(&arc_sums.arcstat_l2_misses); | |
7499 | wmsum_fini(&arc_sums.arcstat_l2_prefetch_asize); | |
7500 | wmsum_fini(&arc_sums.arcstat_l2_mru_asize); | |
7501 | wmsum_fini(&arc_sums.arcstat_l2_mfu_asize); | |
7502 | wmsum_fini(&arc_sums.arcstat_l2_bufc_data_asize); | |
7503 | wmsum_fini(&arc_sums.arcstat_l2_bufc_metadata_asize); | |
7504 | wmsum_fini(&arc_sums.arcstat_l2_feeds); | |
7505 | wmsum_fini(&arc_sums.arcstat_l2_rw_clash); | |
7506 | wmsum_fini(&arc_sums.arcstat_l2_read_bytes); | |
7507 | wmsum_fini(&arc_sums.arcstat_l2_write_bytes); | |
7508 | wmsum_fini(&arc_sums.arcstat_l2_writes_sent); | |
7509 | wmsum_fini(&arc_sums.arcstat_l2_writes_done); | |
7510 | wmsum_fini(&arc_sums.arcstat_l2_writes_error); | |
7511 | wmsum_fini(&arc_sums.arcstat_l2_writes_lock_retry); | |
7512 | wmsum_fini(&arc_sums.arcstat_l2_evict_lock_retry); | |
7513 | wmsum_fini(&arc_sums.arcstat_l2_evict_reading); | |
7514 | wmsum_fini(&arc_sums.arcstat_l2_evict_l1cached); | |
7515 | wmsum_fini(&arc_sums.arcstat_l2_free_on_write); | |
7516 | wmsum_fini(&arc_sums.arcstat_l2_abort_lowmem); | |
7517 | wmsum_fini(&arc_sums.arcstat_l2_cksum_bad); | |
7518 | wmsum_fini(&arc_sums.arcstat_l2_io_error); | |
7519 | wmsum_fini(&arc_sums.arcstat_l2_lsize); | |
7520 | wmsum_fini(&arc_sums.arcstat_l2_psize); | |
7521 | aggsum_fini(&arc_sums.arcstat_l2_hdr_size); | |
7522 | wmsum_fini(&arc_sums.arcstat_l2_log_blk_writes); | |
7523 | wmsum_fini(&arc_sums.arcstat_l2_log_blk_asize); | |
7524 | wmsum_fini(&arc_sums.arcstat_l2_log_blk_count); | |
7525 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_success); | |
7526 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_unsupported); | |
7527 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_io_errors); | |
7528 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_dh_errors); | |
7529 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors); | |
7530 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_lowmem); | |
7531 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_size); | |
7532 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_asize); | |
7533 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_bufs); | |
7534 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_bufs_precached); | |
7535 | wmsum_fini(&arc_sums.arcstat_l2_rebuild_log_blks); | |
7536 | wmsum_fini(&arc_sums.arcstat_memory_throttle_count); | |
7537 | wmsum_fini(&arc_sums.arcstat_memory_direct_count); | |
7538 | wmsum_fini(&arc_sums.arcstat_memory_indirect_count); | |
7539 | wmsum_fini(&arc_sums.arcstat_prune); | |
a8d83e2a | 7540 | wmsum_fini(&arc_sums.arcstat_meta_used); |
c4c162c1 | 7541 | wmsum_fini(&arc_sums.arcstat_async_upgrade_sync); |
c935fe2e | 7542 | wmsum_fini(&arc_sums.arcstat_predictive_prefetch); |
c4c162c1 | 7543 | wmsum_fini(&arc_sums.arcstat_demand_hit_predictive_prefetch); |
c935fe2e AM |
7544 | wmsum_fini(&arc_sums.arcstat_demand_iohit_predictive_prefetch); |
7545 | wmsum_fini(&arc_sums.arcstat_prescient_prefetch); | |
c4c162c1 | 7546 | wmsum_fini(&arc_sums.arcstat_demand_hit_prescient_prefetch); |
c935fe2e | 7547 | wmsum_fini(&arc_sums.arcstat_demand_iohit_prescient_prefetch); |
c4c162c1 AM |
7548 | wmsum_fini(&arc_sums.arcstat_raw_size); |
7549 | wmsum_fini(&arc_sums.arcstat_cached_only_in_progress); | |
7550 | wmsum_fini(&arc_sums.arcstat_abd_chunk_waste_size); | |
d3c2ae1c GW |
7551 | } |
7552 | ||
7553 | uint64_t | |
e71cade6 | 7554 | arc_target_bytes(void) |
d3c2ae1c | 7555 | { |
e71cade6 | 7556 | return (arc_c); |
d3c2ae1c GW |
7557 | } |
7558 | ||
60a4c7d2 PD |
7559 | void |
7560 | arc_set_limits(uint64_t allmem) | |
7561 | { | |
7562 | /* Set min cache to 1/32 of all memory, or 32MB, whichever is more. */ | |
7563 | arc_c_min = MAX(allmem / 32, 2ULL << SPA_MAXBLOCKSHIFT); | |
7564 | ||
7565 | /* How to set default max varies by platform. */ | |
7566 | arc_c_max = arc_default_max(arc_c_min, allmem); | |
7567 | } | |
34dc7c2f BB |
7568 | void |
7569 | arc_init(void) | |
7570 | { | |
9edb3695 | 7571 | uint64_t percent, allmem = arc_all_memory(); |
5dd92909 | 7572 | mutex_init(&arc_evict_lock, NULL, MUTEX_DEFAULT, NULL); |
3442c2a0 MA |
7573 | list_create(&arc_evict_waiters, sizeof (arc_evict_waiter_t), |
7574 | offsetof(arc_evict_waiter_t, aew_node)); | |
ca0bf58d | 7575 | |
2b84817f TC |
7576 | arc_min_prefetch_ms = 1000; |
7577 | arc_min_prescient_prefetch_ms = 6000; | |
34dc7c2f | 7578 | |
c9c9c1e2 MM |
7579 | #if defined(_KERNEL) |
7580 | arc_lowmem_init(); | |
34dc7c2f BB |
7581 | #endif |
7582 | ||
60a4c7d2 | 7583 | arc_set_limits(allmem); |
9a51738b | 7584 | |
e945e8d7 AJ |
7585 | #ifdef _KERNEL |
7586 | /* | |
7587 | * If zfs_arc_max is non-zero at init, meaning it was set in the kernel | |
7588 | * environment before the module was loaded, don't block setting the | |
7589 | * maximum because it is less than arc_c_min, instead, reset arc_c_min | |
7590 | * to a lower value. | |
7591 | * zfs_arc_min will be handled by arc_tuning_update(). | |
7592 | */ | |
7593 | if (zfs_arc_max != 0 && zfs_arc_max >= MIN_ARC_MAX && | |
7594 | zfs_arc_max < allmem) { | |
7595 | arc_c_max = zfs_arc_max; | |
7596 | if (arc_c_min >= arc_c_max) { | |
7597 | arc_c_min = MAX(zfs_arc_max / 2, | |
7598 | 2ULL << SPA_MAXBLOCKSHIFT); | |
7599 | } | |
7600 | } | |
7601 | #else | |
ab5cbbd1 BB |
7602 | /* |
7603 | * In userland, there's only the memory pressure that we artificially | |
7604 | * create (see arc_available_memory()). Don't let arc_c get too | |
7605 | * small, because it can cause transactions to be larger than | |
7606 | * arc_c, causing arc_tempreserve_space() to fail. | |
7607 | */ | |
0a1f8cd9 | 7608 | arc_c_min = MAX(arc_c_max / 2, 2ULL << SPA_MAXBLOCKSHIFT); |
ab5cbbd1 BB |
7609 | #endif |
7610 | ||
17ca3018 | 7611 | arc_c = arc_c_min; |
9907cc1c | 7612 | /* |
a8d83e2a AM |
7613 | * 32-bit fixed point fractions of metadata from total ARC size, |
7614 | * MRU data from all data and MRU metadata from all metadata. | |
9907cc1c | 7615 | */ |
a8d83e2a AM |
7616 | arc_meta = (1ULL << 32) / 4; /* Metadata is 25% of arc_c. */ |
7617 | arc_pd = (1ULL << 32) / 2; /* Data MRU is 50% of data. */ | |
7618 | arc_pm = (1ULL << 32) / 2; /* Metadata MRU is 50% of metadata. */ | |
7619 | ||
9907cc1c | 7620 | percent = MIN(zfs_arc_dnode_limit_percent, 100); |
a8d83e2a | 7621 | arc_dnode_limit = arc_c_max * percent / 100; |
34dc7c2f | 7622 | |
ca67b33a | 7623 | /* Apply user specified tunings */ |
36a6e233 | 7624 | arc_tuning_update(B_TRUE); |
c52fca13 | 7625 | |
34dc7c2f BB |
7626 | /* if kmem_flags are set, lets try to use less memory */ |
7627 | if (kmem_debugging()) | |
7628 | arc_c = arc_c / 2; | |
7629 | if (arc_c < arc_c_min) | |
7630 | arc_c = arc_c_min; | |
7631 | ||
60a4c7d2 PD |
7632 | arc_register_hotplug(); |
7633 | ||
d3c2ae1c | 7634 | arc_state_init(); |
3ec34e55 | 7635 | |
34dc7c2f BB |
7636 | buf_init(); |
7637 | ||
ab26409d BB |
7638 | list_create(&arc_prune_list, sizeof (arc_prune_t), |
7639 | offsetof(arc_prune_t, p_node)); | |
ab26409d | 7640 | mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f | 7641 | |
462217d1 AM |
7642 | arc_prune_taskq = taskq_create("arc_prune", zfs_arc_prune_task_threads, |
7643 | defclsyspri, 100, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC); | |
f6046738 | 7644 | |
34dc7c2f BB |
7645 | arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, |
7646 | sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); | |
7647 | ||
7648 | if (arc_ksp != NULL) { | |
7649 | arc_ksp->ks_data = &arc_stats; | |
13be560d | 7650 | arc_ksp->ks_update = arc_kstat_update; |
34dc7c2f BB |
7651 | kstat_install(arc_ksp); |
7652 | } | |
7653 | ||
6e2a5918 MJ |
7654 | arc_state_evict_markers = |
7655 | arc_state_alloc_markers(arc_state_evict_marker_count); | |
ed2f7ba0 AM |
7656 | arc_evict_zthr = zthr_create_timer("arc_evict", |
7657 | arc_evict_cb_check, arc_evict_cb, NULL, SEC2NSEC(1), defclsyspri); | |
843e9ca2 | 7658 | arc_reap_zthr = zthr_create_timer("arc_reap", |
6bc61d22 | 7659 | arc_reap_cb_check, arc_reap_cb, NULL, SEC2NSEC(1), minclsyspri); |
34dc7c2f | 7660 | |
b128c09f | 7661 | arc_warm = B_FALSE; |
34dc7c2f | 7662 | |
e8b96c60 MA |
7663 | /* |
7664 | * Calculate maximum amount of dirty data per pool. | |
7665 | * | |
7666 | * If it has been set by a module parameter, take that. | |
7667 | * Otherwise, use a percentage of physical memory defined by | |
7668 | * zfs_dirty_data_max_percent (default 10%) with a cap at | |
e99932f7 | 7669 | * zfs_dirty_data_max_max (default 4G or 25% of physical memory). |
e8b96c60 | 7670 | */ |
47ed79ff | 7671 | #ifdef __LP64__ |
e8b96c60 | 7672 | if (zfs_dirty_data_max_max == 0) |
e99932f7 BB |
7673 | zfs_dirty_data_max_max = MIN(4ULL * 1024 * 1024 * 1024, |
7674 | allmem * zfs_dirty_data_max_max_percent / 100); | |
47ed79ff MM |
7675 | #else |
7676 | if (zfs_dirty_data_max_max == 0) | |
7677 | zfs_dirty_data_max_max = MIN(1ULL * 1024 * 1024 * 1024, | |
7678 | allmem * zfs_dirty_data_max_max_percent / 100); | |
7679 | #endif | |
e8b96c60 MA |
7680 | |
7681 | if (zfs_dirty_data_max == 0) { | |
9edb3695 | 7682 | zfs_dirty_data_max = allmem * |
e8b96c60 MA |
7683 | zfs_dirty_data_max_percent / 100; |
7684 | zfs_dirty_data_max = MIN(zfs_dirty_data_max, | |
7685 | zfs_dirty_data_max_max); | |
7686 | } | |
a7bd20e3 KJ |
7687 | |
7688 | if (zfs_wrlog_data_max == 0) { | |
7689 | ||
7690 | /* | |
7691 | * dp_wrlog_total is reduced for each txg at the end of | |
7692 | * spa_sync(). However, dp_dirty_total is reduced every time | |
7693 | * a block is written out. Thus under normal operation, | |
7694 | * dp_wrlog_total could grow 2 times as big as | |
7695 | * zfs_dirty_data_max. | |
7696 | */ | |
7697 | zfs_wrlog_data_max = zfs_dirty_data_max * 2; | |
7698 | } | |
34dc7c2f BB |
7699 | } |
7700 | ||
7701 | void | |
7702 | arc_fini(void) | |
7703 | { | |
ab26409d BB |
7704 | arc_prune_t *p; |
7705 | ||
7cb67b45 | 7706 | #ifdef _KERNEL |
c9c9c1e2 | 7707 | arc_lowmem_fini(); |
7cb67b45 BB |
7708 | #endif /* _KERNEL */ |
7709 | ||
d3c2ae1c GW |
7710 | /* Use B_TRUE to ensure *all* buffers are evicted */ |
7711 | arc_flush(NULL, B_TRUE); | |
34dc7c2f | 7712 | |
34dc7c2f BB |
7713 | if (arc_ksp != NULL) { |
7714 | kstat_delete(arc_ksp); | |
7715 | arc_ksp = NULL; | |
7716 | } | |
7717 | ||
f6046738 BB |
7718 | taskq_wait(arc_prune_taskq); |
7719 | taskq_destroy(arc_prune_taskq); | |
7720 | ||
ab26409d | 7721 | mutex_enter(&arc_prune_mtx); |
b3ad3f48 | 7722 | while ((p = list_remove_head(&arc_prune_list)) != NULL) { |
4836d293 | 7723 | (void) zfs_refcount_remove(&p->p_refcnt, &arc_prune_list); |
424fd7c3 | 7724 | zfs_refcount_destroy(&p->p_refcnt); |
ab26409d BB |
7725 | kmem_free(p, sizeof (*p)); |
7726 | } | |
7727 | mutex_exit(&arc_prune_mtx); | |
7728 | ||
7729 | list_destroy(&arc_prune_list); | |
7730 | mutex_destroy(&arc_prune_mtx); | |
3ec34e55 | 7731 | |
5dd92909 | 7732 | (void) zthr_cancel(arc_evict_zthr); |
3ec34e55 | 7733 | (void) zthr_cancel(arc_reap_zthr); |
6e2a5918 MJ |
7734 | arc_state_free_markers(arc_state_evict_markers, |
7735 | arc_state_evict_marker_count); | |
3ec34e55 | 7736 | |
5dd92909 | 7737 | mutex_destroy(&arc_evict_lock); |
3442c2a0 | 7738 | list_destroy(&arc_evict_waiters); |
ca0bf58d | 7739 | |
cfd59f90 BB |
7740 | /* |
7741 | * Free any buffers that were tagged for destruction. This needs | |
7742 | * to occur before arc_state_fini() runs and destroys the aggsum | |
7743 | * values which are updated when freeing scatter ABDs. | |
7744 | */ | |
7745 | l2arc_do_free_on_write(); | |
7746 | ||
ae3d8491 PD |
7747 | /* |
7748 | * buf_fini() must proceed arc_state_fini() because buf_fin() may | |
7749 | * trigger the release of kmem magazines, which can callback to | |
7750 | * arc_space_return() which accesses aggsums freed in act_state_fini(). | |
7751 | */ | |
34dc7c2f | 7752 | buf_fini(); |
ae3d8491 | 7753 | arc_state_fini(); |
9babb374 | 7754 | |
60a4c7d2 PD |
7755 | arc_unregister_hotplug(); |
7756 | ||
1c44a5c9 SD |
7757 | /* |
7758 | * We destroy the zthrs after all the ARC state has been | |
7759 | * torn down to avoid the case of them receiving any | |
7760 | * wakeup() signals after they are destroyed. | |
7761 | */ | |
5dd92909 | 7762 | zthr_destroy(arc_evict_zthr); |
1c44a5c9 SD |
7763 | zthr_destroy(arc_reap_zthr); |
7764 | ||
b9541d6b | 7765 | ASSERT0(arc_loaned_bytes); |
34dc7c2f BB |
7766 | } |
7767 | ||
7768 | /* | |
7769 | * Level 2 ARC | |
7770 | * | |
7771 | * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. | |
7772 | * It uses dedicated storage devices to hold cached data, which are populated | |
7773 | * using large infrequent writes. The main role of this cache is to boost | |
7774 | * the performance of random read workloads. The intended L2ARC devices | |
7775 | * include short-stroked disks, solid state disks, and other media with | |
7776 | * substantially faster read latency than disk. | |
7777 | * | |
7778 | * +-----------------------+ | |
7779 | * | ARC | | |
7780 | * +-----------------------+ | |
7781 | * | ^ ^ | |
7782 | * | | | | |
7783 | * l2arc_feed_thread() arc_read() | |
7784 | * | | | | |
7785 | * | l2arc read | | |
7786 | * V | | | |
7787 | * +---------------+ | | |
7788 | * | L2ARC | | | |
7789 | * +---------------+ | | |
7790 | * | ^ | | |
7791 | * l2arc_write() | | | |
7792 | * | | | | |
7793 | * V | | | |
7794 | * +-------+ +-------+ | |
7795 | * | vdev | | vdev | | |
7796 | * | cache | | cache | | |
7797 | * +-------+ +-------+ | |
7798 | * +=========+ .-----. | |
7799 | * : L2ARC : |-_____-| | |
7800 | * : devices : | Disks | | |
7801 | * +=========+ `-_____-' | |
7802 | * | |
7803 | * Read requests are satisfied from the following sources, in order: | |
7804 | * | |
7805 | * 1) ARC | |
7806 | * 2) vdev cache of L2ARC devices | |
7807 | * 3) L2ARC devices | |
7808 | * 4) vdev cache of disks | |
7809 | * 5) disks | |
7810 | * | |
7811 | * Some L2ARC device types exhibit extremely slow write performance. | |
7812 | * To accommodate for this there are some significant differences between | |
7813 | * the L2ARC and traditional cache design: | |
7814 | * | |
7815 | * 1. There is no eviction path from the ARC to the L2ARC. Evictions from | |
7816 | * the ARC behave as usual, freeing buffers and placing headers on ghost | |
7817 | * lists. The ARC does not send buffers to the L2ARC during eviction as | |
7818 | * this would add inflated write latencies for all ARC memory pressure. | |
7819 | * | |
7820 | * 2. The L2ARC attempts to cache data from the ARC before it is evicted. | |
7821 | * It does this by periodically scanning buffers from the eviction-end of | |
7822 | * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are | |
3a17a7a9 SK |
7823 | * not already there. It scans until a headroom of buffers is satisfied, |
7824 | * which itself is a buffer for ARC eviction. If a compressible buffer is | |
7825 | * found during scanning and selected for writing to an L2ARC device, we | |
7826 | * temporarily boost scanning headroom during the next scan cycle to make | |
7827 | * sure we adapt to compression effects (which might significantly reduce | |
7828 | * the data volume we write to L2ARC). The thread that does this is | |
34dc7c2f BB |
7829 | * l2arc_feed_thread(), illustrated below; example sizes are included to |
7830 | * provide a better sense of ratio than this diagram: | |
7831 | * | |
7832 | * head --> tail | |
7833 | * +---------------------+----------+ | |
7834 | * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC | |
7835 | * +---------------------+----------+ | o L2ARC eligible | |
7836 | * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer | |
7837 | * +---------------------+----------+ | | |
7838 | * 15.9 Gbytes ^ 32 Mbytes | | |
7839 | * headroom | | |
7840 | * l2arc_feed_thread() | |
7841 | * | | |
7842 | * l2arc write hand <--[oooo]--' | |
7843 | * | 8 Mbyte | |
7844 | * | write max | |
7845 | * V | |
7846 | * +==============================+ | |
7847 | * L2ARC dev |####|#|###|###| |####| ... | | |
7848 | * +==============================+ | |
7849 | * 32 Gbytes | |
7850 | * | |
7851 | * 3. If an ARC buffer is copied to the L2ARC but then hit instead of | |
7852 | * evicted, then the L2ARC has cached a buffer much sooner than it probably | |
7853 | * needed to, potentially wasting L2ARC device bandwidth and storage. It is | |
7854 | * safe to say that this is an uncommon case, since buffers at the end of | |
7855 | * the ARC lists have moved there due to inactivity. | |
7856 | * | |
7857 | * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, | |
7858 | * then the L2ARC simply misses copying some buffers. This serves as a | |
7859 | * pressure valve to prevent heavy read workloads from both stalling the ARC | |
7860 | * with waits and clogging the L2ARC with writes. This also helps prevent | |
7861 | * the potential for the L2ARC to churn if it attempts to cache content too | |
7862 | * quickly, such as during backups of the entire pool. | |
7863 | * | |
b128c09f BB |
7864 | * 5. After system boot and before the ARC has filled main memory, there are |
7865 | * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru | |
7866 | * lists can remain mostly static. Instead of searching from tail of these | |
7867 | * lists as pictured, the l2arc_feed_thread() will search from the list heads | |
7868 | * for eligible buffers, greatly increasing its chance of finding them. | |
7869 | * | |
7870 | * The L2ARC device write speed is also boosted during this time so that | |
7871 | * the L2ARC warms up faster. Since there have been no ARC evictions yet, | |
7872 | * there are no L2ARC reads, and no fear of degrading read performance | |
7873 | * through increased writes. | |
7874 | * | |
7875 | * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that | |
34dc7c2f BB |
7876 | * the vdev queue can aggregate them into larger and fewer writes. Each |
7877 | * device is written to in a rotor fashion, sweeping writes through | |
7878 | * available space then repeating. | |
7879 | * | |
b128c09f | 7880 | * 7. The L2ARC does not store dirty content. It never needs to flush |
34dc7c2f BB |
7881 | * write buffers back to disk based storage. |
7882 | * | |
b128c09f | 7883 | * 8. If an ARC buffer is written (and dirtied) which also exists in the |
34dc7c2f BB |
7884 | * L2ARC, the now stale L2ARC buffer is immediately dropped. |
7885 | * | |
7886 | * The performance of the L2ARC can be tweaked by a number of tunables, which | |
7887 | * may be necessary for different workloads: | |
7888 | * | |
7889 | * l2arc_write_max max write bytes per interval | |
b128c09f | 7890 | * l2arc_write_boost extra write bytes during device warmup |
34dc7c2f BB |
7891 | * l2arc_noprefetch skip caching prefetched buffers |
7892 | * l2arc_headroom number of max device writes to precache | |
3a17a7a9 SK |
7893 | * l2arc_headroom_boost when we find compressed buffers during ARC |
7894 | * scanning, we multiply headroom by this | |
7895 | * percentage factor for the next scan cycle, | |
7896 | * since more compressed buffers are likely to | |
7897 | * be present | |
34dc7c2f BB |
7898 | * l2arc_feed_secs seconds between L2ARC writing |
7899 | * | |
7900 | * Tunables may be removed or added as future performance improvements are | |
7901 | * integrated, and also may become zpool properties. | |
d164b209 BB |
7902 | * |
7903 | * There are three key functions that control how the L2ARC warms up: | |
7904 | * | |
7905 | * l2arc_write_eligible() check if a buffer is eligible to cache | |
7906 | * l2arc_write_size() calculate how much to write | |
7907 | * l2arc_write_interval() calculate sleep delay between writes | |
7908 | * | |
7909 | * These three functions determine what to write, how much, and how quickly | |
7910 | * to send writes. | |
77f6826b GA |
7911 | * |
7912 | * L2ARC persistence: | |
7913 | * | |
7914 | * When writing buffers to L2ARC, we periodically add some metadata to | |
7915 | * make sure we can pick them up after reboot, thus dramatically reducing | |
7916 | * the impact that any downtime has on the performance of storage systems | |
7917 | * with large caches. | |
7918 | * | |
7919 | * The implementation works fairly simply by integrating the following two | |
7920 | * modifications: | |
7921 | * | |
7922 | * *) When writing to the L2ARC, we occasionally write a "l2arc log block", | |
7923 | * which is an additional piece of metadata which describes what's been | |
7924 | * written. This allows us to rebuild the arc_buf_hdr_t structures of the | |
7925 | * main ARC buffers. There are 2 linked-lists of log blocks headed by | |
7926 | * dh_start_lbps[2]. We alternate which chain we append to, so they are | |
7927 | * time-wise and offset-wise interleaved, but that is an optimization rather | |
7928 | * than for correctness. The log block also includes a pointer to the | |
7929 | * previous block in its chain. | |
7930 | * | |
7931 | * *) We reserve SPA_MINBLOCKSIZE of space at the start of each L2ARC device | |
7932 | * for our header bookkeeping purposes. This contains a device header, | |
7933 | * which contains our top-level reference structures. We update it each | |
7934 | * time we write a new log block, so that we're able to locate it in the | |
7935 | * L2ARC device. If this write results in an inconsistent device header | |
7936 | * (e.g. due to power failure), we detect this by verifying the header's | |
7937 | * checksum and simply fail to reconstruct the L2ARC after reboot. | |
7938 | * | |
7939 | * Implementation diagram: | |
7940 | * | |
7941 | * +=== L2ARC device (not to scale) ======================================+ | |
7942 | * | ___two newest log block pointers__.__________ | | |
7943 | * | / \dh_start_lbps[1] | | |
7944 | * | / \ \dh_start_lbps[0]| | |
7945 | * |.___/__. V V | | |
7946 | * ||L2 dev|....|lb |bufs |lb |bufs |lb |bufs |lb |bufs |lb |---(empty)---| | |
7947 | * || hdr| ^ /^ /^ / / | | |
7948 | * |+------+ ...--\-------/ \-----/--\------/ / | | |
7949 | * | \--------------/ \--------------/ | | |
7950 | * +======================================================================+ | |
7951 | * | |
7952 | * As can be seen on the diagram, rather than using a simple linked list, | |
7953 | * we use a pair of linked lists with alternating elements. This is a | |
7954 | * performance enhancement due to the fact that we only find out the | |
7955 | * address of the next log block access once the current block has been | |
7956 | * completely read in. Obviously, this hurts performance, because we'd be | |
7957 | * keeping the device's I/O queue at only a 1 operation deep, thus | |
7958 | * incurring a large amount of I/O round-trip latency. Having two lists | |
7959 | * allows us to fetch two log blocks ahead of where we are currently | |
7960 | * rebuilding L2ARC buffers. | |
7961 | * | |
7962 | * On-device data structures: | |
7963 | * | |
7964 | * L2ARC device header: l2arc_dev_hdr_phys_t | |
7965 | * L2ARC log block: l2arc_log_blk_phys_t | |
7966 | * | |
7967 | * L2ARC reconstruction: | |
7968 | * | |
7969 | * When writing data, we simply write in the standard rotary fashion, | |
7970 | * evicting buffers as we go and simply writing new data over them (writing | |
7971 | * a new log block every now and then). This obviously means that once we | |
7972 | * loop around the end of the device, we will start cutting into an already | |
7973 | * committed log block (and its referenced data buffers), like so: | |
7974 | * | |
7975 | * current write head__ __old tail | |
7976 | * \ / | |
7977 | * V V | |
7978 | * <--|bufs |lb |bufs |lb | |bufs |lb |bufs |lb |--> | |
7979 | * ^ ^^^^^^^^^___________________________________ | |
7980 | * | \ | |
7981 | * <<nextwrite>> may overwrite this blk and/or its bufs --' | |
7982 | * | |
7983 | * When importing the pool, we detect this situation and use it to stop | |
7984 | * our scanning process (see l2arc_rebuild). | |
7985 | * | |
7986 | * There is one significant caveat to consider when rebuilding ARC contents | |
7987 | * from an L2ARC device: what about invalidated buffers? Given the above | |
7988 | * construction, we cannot update blocks which we've already written to amend | |
7989 | * them to remove buffers which were invalidated. Thus, during reconstruction, | |
7990 | * we might be populating the cache with buffers for data that's not on the | |
7991 | * main pool anymore, or may have been overwritten! | |
7992 | * | |
7993 | * As it turns out, this isn't a problem. Every arc_read request includes | |
7994 | * both the DVA and, crucially, the birth TXG of the BP the caller is | |
7995 | * looking for. So even if the cache were populated by completely rotten | |
7996 | * blocks for data that had been long deleted and/or overwritten, we'll | |
7997 | * never actually return bad data from the cache, since the DVA with the | |
7998 | * birth TXG uniquely identify a block in space and time - once created, | |
7999 | * a block is immutable on disk. The worst thing we have done is wasted | |
8000 | * some time and memory at l2arc rebuild to reconstruct outdated ARC | |
8001 | * entries that will get dropped from the l2arc as it is being updated | |
8002 | * with new blocks. | |
8003 | * | |
8004 | * L2ARC buffers that have been evicted by l2arc_evict() ahead of the write | |
8005 | * hand are not restored. This is done by saving the offset (in bytes) | |
8006 | * l2arc_evict() has evicted to in the L2ARC device header and taking it | |
8007 | * into account when restoring buffers. | |
34dc7c2f BB |
8008 | */ |
8009 | ||
d164b209 | 8010 | static boolean_t |
2a432414 | 8011 | l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) |
d164b209 BB |
8012 | { |
8013 | /* | |
8014 | * A buffer is *not* eligible for the L2ARC if it: | |
8015 | * 1. belongs to a different spa. | |
428870ff BB |
8016 | * 2. is already cached on the L2ARC. |
8017 | * 3. has an I/O in progress (it may be an incomplete read). | |
8018 | * 4. is flagged not eligible (zfs property). | |
d164b209 | 8019 | */ |
b9541d6b | 8020 | if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) || |
c6f5e9d9 | 8021 | HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr)) |
d164b209 BB |
8022 | return (B_FALSE); |
8023 | ||
8024 | return (B_TRUE); | |
8025 | } | |
8026 | ||
8027 | static uint64_t | |
37c22948 | 8028 | l2arc_write_size(l2arc_dev_t *dev) |
d164b209 | 8029 | { |
bcd53210 | 8030 | uint64_t size; |
d164b209 | 8031 | |
3a17a7a9 SK |
8032 | /* |
8033 | * Make sure our globals have meaningful values in case the user | |
8034 | * altered them. | |
8035 | */ | |
8036 | size = l2arc_write_max; | |
8037 | if (size == 0) { | |
35da3451 AM |
8038 | cmn_err(CE_NOTE, "l2arc_write_max must be greater than zero, " |
8039 | "resetting it to the default (%d)", L2ARC_WRITE_SIZE); | |
3a17a7a9 SK |
8040 | size = l2arc_write_max = L2ARC_WRITE_SIZE; |
8041 | } | |
d164b209 BB |
8042 | |
8043 | if (arc_warm == B_FALSE) | |
3a17a7a9 | 8044 | size += l2arc_write_boost; |
d164b209 | 8045 | |
d38c815f | 8046 | /* We need to add in the worst case scenario of log block overhead. */ |
bcd53210 | 8047 | size += l2arc_log_blk_overhead(size, dev); |
d38c815f GA |
8048 | if (dev->l2ad_vdev->vdev_has_trim && l2arc_trim_ahead > 0) { |
8049 | /* | |
8050 | * Trim ahead of the write size 64MB or (l2arc_trim_ahead/100) | |
8051 | * times the writesize, whichever is greater. | |
8052 | */ | |
bcd53210 GA |
8053 | size += MAX(64 * 1024 * 1024, |
8054 | (size * l2arc_trim_ahead) / 100); | |
d38c815f | 8055 | } |
b7654bd7 | 8056 | |
bcd53210 GA |
8057 | /* |
8058 | * Make sure the write size does not exceed the size of the cache | |
8059 | * device. This is important in l2arc_evict(), otherwise infinite | |
8060 | * iteration can occur. | |
8061 | */ | |
35da3451 | 8062 | size = MIN(size, (dev->l2ad_end - dev->l2ad_start) / 4); |
bcd53210 | 8063 | |
35da3451 | 8064 | size = P2ROUNDUP(size, 1ULL << dev->l2ad_vdev->vdev_ashift); |
37c22948 | 8065 | |
d164b209 BB |
8066 | return (size); |
8067 | ||
8068 | } | |
8069 | ||
8070 | static clock_t | |
8071 | l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) | |
8072 | { | |
428870ff | 8073 | clock_t interval, next, now; |
d164b209 BB |
8074 | |
8075 | /* | |
8076 | * If the ARC lists are busy, increase our write rate; if the | |
8077 | * lists are stale, idle back. This is achieved by checking | |
8078 | * how much we previously wrote - if it was more than half of | |
8079 | * what we wanted, schedule the next write much sooner. | |
8080 | */ | |
8081 | if (l2arc_feed_again && wrote > (wanted / 2)) | |
8082 | interval = (hz * l2arc_feed_min_ms) / 1000; | |
8083 | else | |
8084 | interval = hz * l2arc_feed_secs; | |
8085 | ||
428870ff BB |
8086 | now = ddi_get_lbolt(); |
8087 | next = MAX(now, MIN(now + interval, began + interval)); | |
d164b209 BB |
8088 | |
8089 | return (next); | |
8090 | } | |
8091 | ||
34dc7c2f BB |
8092 | /* |
8093 | * Cycle through L2ARC devices. This is how L2ARC load balances. | |
b128c09f | 8094 | * If a device is returned, this also returns holding the spa config lock. |
34dc7c2f BB |
8095 | */ |
8096 | static l2arc_dev_t * | |
8097 | l2arc_dev_get_next(void) | |
8098 | { | |
b128c09f | 8099 | l2arc_dev_t *first, *next = NULL; |
34dc7c2f | 8100 | |
b128c09f BB |
8101 | /* |
8102 | * Lock out the removal of spas (spa_namespace_lock), then removal | |
8103 | * of cache devices (l2arc_dev_mtx). Once a device has been selected, | |
8104 | * both locks will be dropped and a spa config lock held instead. | |
8105 | */ | |
8106 | mutex_enter(&spa_namespace_lock); | |
8107 | mutex_enter(&l2arc_dev_mtx); | |
8108 | ||
8109 | /* if there are no vdevs, there is nothing to do */ | |
8110 | if (l2arc_ndev == 0) | |
8111 | goto out; | |
8112 | ||
8113 | first = NULL; | |
8114 | next = l2arc_dev_last; | |
8115 | do { | |
8116 | /* loop around the list looking for a non-faulted vdev */ | |
8117 | if (next == NULL) { | |
34dc7c2f | 8118 | next = list_head(l2arc_dev_list); |
b128c09f BB |
8119 | } else { |
8120 | next = list_next(l2arc_dev_list, next); | |
8121 | if (next == NULL) | |
8122 | next = list_head(l2arc_dev_list); | |
8123 | } | |
8124 | ||
8125 | /* if we have come back to the start, bail out */ | |
8126 | if (first == NULL) | |
8127 | first = next; | |
8128 | else if (next == first) | |
8129 | break; | |
8130 | ||
a6ccb36b | 8131 | ASSERT3P(next, !=, NULL); |
b7654bd7 GA |
8132 | } while (vdev_is_dead(next->l2ad_vdev) || next->l2ad_rebuild || |
8133 | next->l2ad_trim_all); | |
b128c09f BB |
8134 | |
8135 | /* if we were unable to find any usable vdevs, return NULL */ | |
b7654bd7 GA |
8136 | if (vdev_is_dead(next->l2ad_vdev) || next->l2ad_rebuild || |
8137 | next->l2ad_trim_all) | |
b128c09f | 8138 | next = NULL; |
34dc7c2f BB |
8139 | |
8140 | l2arc_dev_last = next; | |
8141 | ||
b128c09f BB |
8142 | out: |
8143 | mutex_exit(&l2arc_dev_mtx); | |
8144 | ||
8145 | /* | |
8146 | * Grab the config lock to prevent the 'next' device from being | |
8147 | * removed while we are writing to it. | |
8148 | */ | |
8149 | if (next != NULL) | |
8150 | spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); | |
8151 | mutex_exit(&spa_namespace_lock); | |
8152 | ||
34dc7c2f BB |
8153 | return (next); |
8154 | } | |
8155 | ||
b128c09f BB |
8156 | /* |
8157 | * Free buffers that were tagged for destruction. | |
8158 | */ | |
8159 | static void | |
0bc8fd78 | 8160 | l2arc_do_free_on_write(void) |
b128c09f | 8161 | { |
b3ad3f48 | 8162 | l2arc_data_free_t *df; |
b128c09f BB |
8163 | |
8164 | mutex_enter(&l2arc_free_on_write_mtx); | |
b3ad3f48 | 8165 | while ((df = list_remove_head(l2arc_free_on_write)) != NULL) { |
a6255b7f DQ |
8166 | ASSERT3P(df->l2df_abd, !=, NULL); |
8167 | abd_free(df->l2df_abd); | |
b128c09f BB |
8168 | kmem_free(df, sizeof (l2arc_data_free_t)); |
8169 | } | |
b128c09f BB |
8170 | mutex_exit(&l2arc_free_on_write_mtx); |
8171 | } | |
8172 | ||
34dc7c2f BB |
8173 | /* |
8174 | * A write to a cache device has completed. Update all headers to allow | |
8175 | * reads from these buffers to begin. | |
8176 | */ | |
8177 | static void | |
8178 | l2arc_write_done(zio_t *zio) | |
8179 | { | |
77f6826b GA |
8180 | l2arc_write_callback_t *cb; |
8181 | l2arc_lb_abd_buf_t *abd_buf; | |
8182 | l2arc_lb_ptr_buf_t *lb_ptr_buf; | |
8183 | l2arc_dev_t *dev; | |
657fd33b | 8184 | l2arc_dev_hdr_phys_t *l2dhdr; |
77f6826b GA |
8185 | list_t *buflist; |
8186 | arc_buf_hdr_t *head, *hdr, *hdr_prev; | |
8187 | kmutex_t *hash_lock; | |
8188 | int64_t bytes_dropped = 0; | |
34dc7c2f BB |
8189 | |
8190 | cb = zio->io_private; | |
d3c2ae1c | 8191 | ASSERT3P(cb, !=, NULL); |
34dc7c2f | 8192 | dev = cb->l2wcb_dev; |
657fd33b | 8193 | l2dhdr = dev->l2ad_dev_hdr; |
d3c2ae1c | 8194 | ASSERT3P(dev, !=, NULL); |
34dc7c2f | 8195 | head = cb->l2wcb_head; |
d3c2ae1c | 8196 | ASSERT3P(head, !=, NULL); |
b9541d6b | 8197 | buflist = &dev->l2ad_buflist; |
d3c2ae1c | 8198 | ASSERT3P(buflist, !=, NULL); |
34dc7c2f BB |
8199 | DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, |
8200 | l2arc_write_callback_t *, cb); | |
8201 | ||
34dc7c2f BB |
8202 | /* |
8203 | * All writes completed, or an error was hit. | |
8204 | */ | |
ca0bf58d PS |
8205 | top: |
8206 | mutex_enter(&dev->l2ad_mtx); | |
2a432414 GW |
8207 | for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { |
8208 | hdr_prev = list_prev(buflist, hdr); | |
34dc7c2f | 8209 | |
2a432414 | 8210 | hash_lock = HDR_LOCK(hdr); |
ca0bf58d PS |
8211 | |
8212 | /* | |
8213 | * We cannot use mutex_enter or else we can deadlock | |
8214 | * with l2arc_write_buffers (due to swapping the order | |
8215 | * the hash lock and l2ad_mtx are taken). | |
8216 | */ | |
34dc7c2f BB |
8217 | if (!mutex_tryenter(hash_lock)) { |
8218 | /* | |
ca0bf58d PS |
8219 | * Missed the hash lock. We must retry so we |
8220 | * don't leave the ARC_FLAG_L2_WRITING bit set. | |
34dc7c2f | 8221 | */ |
ca0bf58d PS |
8222 | ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); |
8223 | ||
8224 | /* | |
8225 | * We don't want to rescan the headers we've | |
8226 | * already marked as having been written out, so | |
8227 | * we reinsert the head node so we can pick up | |
8228 | * where we left off. | |
8229 | */ | |
8230 | list_remove(buflist, head); | |
8231 | list_insert_after(buflist, hdr, head); | |
8232 | ||
8233 | mutex_exit(&dev->l2ad_mtx); | |
8234 | ||
8235 | /* | |
8236 | * We wait for the hash lock to become available | |
8237 | * to try and prevent busy waiting, and increase | |
8238 | * the chance we'll be able to acquire the lock | |
8239 | * the next time around. | |
8240 | */ | |
8241 | mutex_enter(hash_lock); | |
8242 | mutex_exit(hash_lock); | |
8243 | goto top; | |
34dc7c2f BB |
8244 | } |
8245 | ||
b9541d6b | 8246 | /* |
ca0bf58d PS |
8247 | * We could not have been moved into the arc_l2c_only |
8248 | * state while in-flight due to our ARC_FLAG_L2_WRITING | |
8249 | * bit being set. Let's just ensure that's being enforced. | |
8250 | */ | |
8251 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
8252 | ||
8a09d5fd BB |
8253 | /* |
8254 | * Skipped - drop L2ARC entry and mark the header as no | |
8255 | * longer L2 eligibile. | |
8256 | */ | |
d3c2ae1c | 8257 | if (zio->io_error != 0) { |
34dc7c2f | 8258 | /* |
b128c09f | 8259 | * Error - drop L2ARC entry. |
34dc7c2f | 8260 | */ |
2a432414 | 8261 | list_remove(buflist, hdr); |
d3c2ae1c | 8262 | arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); |
b9541d6b | 8263 | |
7558997d | 8264 | uint64_t psize = HDR_GET_PSIZE(hdr); |
08532162 | 8265 | l2arc_hdr_arcstats_decrement(hdr); |
d962d5da | 8266 | |
7558997d SD |
8267 | bytes_dropped += |
8268 | vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
424fd7c3 | 8269 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, |
d3c2ae1c | 8270 | arc_hdr_size(hdr), hdr); |
34dc7c2f BB |
8271 | } |
8272 | ||
8273 | /* | |
ca0bf58d PS |
8274 | * Allow ARC to begin reads and ghost list evictions to |
8275 | * this L2ARC entry. | |
34dc7c2f | 8276 | */ |
d3c2ae1c | 8277 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING); |
34dc7c2f BB |
8278 | |
8279 | mutex_exit(hash_lock); | |
8280 | } | |
8281 | ||
77f6826b GA |
8282 | /* |
8283 | * Free the allocated abd buffers for writing the log blocks. | |
8284 | * If the zio failed reclaim the allocated space and remove the | |
8285 | * pointers to these log blocks from the log block pointer list | |
8286 | * of the L2ARC device. | |
8287 | */ | |
8288 | while ((abd_buf = list_remove_tail(&cb->l2wcb_abd_list)) != NULL) { | |
8289 | abd_free(abd_buf->abd); | |
8290 | zio_buf_free(abd_buf, sizeof (*abd_buf)); | |
8291 | if (zio->io_error != 0) { | |
8292 | lb_ptr_buf = list_remove_head(&dev->l2ad_lbptr_list); | |
657fd33b GA |
8293 | /* |
8294 | * L2BLK_GET_PSIZE returns aligned size for log | |
8295 | * blocks. | |
8296 | */ | |
8297 | uint64_t asize = | |
77f6826b | 8298 | L2BLK_GET_PSIZE((lb_ptr_buf->lb_ptr)->lbp_prop); |
657fd33b GA |
8299 | bytes_dropped += asize; |
8300 | ARCSTAT_INCR(arcstat_l2_log_blk_asize, -asize); | |
8301 | ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count); | |
8302 | zfs_refcount_remove_many(&dev->l2ad_lb_asize, asize, | |
8303 | lb_ptr_buf); | |
4836d293 RN |
8304 | (void) zfs_refcount_remove(&dev->l2ad_lb_count, |
8305 | lb_ptr_buf); | |
77f6826b GA |
8306 | kmem_free(lb_ptr_buf->lb_ptr, |
8307 | sizeof (l2arc_log_blkptr_t)); | |
8308 | kmem_free(lb_ptr_buf, sizeof (l2arc_lb_ptr_buf_t)); | |
8309 | } | |
8310 | } | |
8311 | list_destroy(&cb->l2wcb_abd_list); | |
8312 | ||
657fd33b | 8313 | if (zio->io_error != 0) { |
08532162 GA |
8314 | ARCSTAT_BUMP(arcstat_l2_writes_error); |
8315 | ||
2054f35e GA |
8316 | /* |
8317 | * Restore the lbps array in the header to its previous state. | |
8318 | * If the list of log block pointers is empty, zero out the | |
8319 | * log block pointers in the device header. | |
8320 | */ | |
657fd33b GA |
8321 | lb_ptr_buf = list_head(&dev->l2ad_lbptr_list); |
8322 | for (int i = 0; i < 2; i++) { | |
2054f35e GA |
8323 | if (lb_ptr_buf == NULL) { |
8324 | /* | |
8325 | * If the list is empty zero out the device | |
8326 | * header. Otherwise zero out the second log | |
8327 | * block pointer in the header. | |
8328 | */ | |
8329 | if (i == 0) { | |
861166b0 AZ |
8330 | memset(l2dhdr, 0, |
8331 | dev->l2ad_dev_hdr_asize); | |
2054f35e | 8332 | } else { |
861166b0 | 8333 | memset(&l2dhdr->dh_start_lbps[i], 0, |
2054f35e GA |
8334 | sizeof (l2arc_log_blkptr_t)); |
8335 | } | |
8336 | break; | |
8337 | } | |
861166b0 | 8338 | memcpy(&l2dhdr->dh_start_lbps[i], lb_ptr_buf->lb_ptr, |
657fd33b GA |
8339 | sizeof (l2arc_log_blkptr_t)); |
8340 | lb_ptr_buf = list_next(&dev->l2ad_lbptr_list, | |
8341 | lb_ptr_buf); | |
8342 | } | |
8343 | } | |
8344 | ||
c4c162c1 | 8345 | ARCSTAT_BUMP(arcstat_l2_writes_done); |
34dc7c2f | 8346 | list_remove(buflist, head); |
b9541d6b CW |
8347 | ASSERT(!HDR_HAS_L1HDR(head)); |
8348 | kmem_cache_free(hdr_l2only_cache, head); | |
8349 | mutex_exit(&dev->l2ad_mtx); | |
34dc7c2f | 8350 | |
77f6826b | 8351 | ASSERT(dev->l2ad_vdev != NULL); |
3bec585e SK |
8352 | vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); |
8353 | ||
b128c09f | 8354 | l2arc_do_free_on_write(); |
34dc7c2f BB |
8355 | |
8356 | kmem_free(cb, sizeof (l2arc_write_callback_t)); | |
8357 | } | |
8358 | ||
b5256303 TC |
8359 | static int |
8360 | l2arc_untransform(zio_t *zio, l2arc_read_callback_t *cb) | |
8361 | { | |
8362 | int ret; | |
8363 | spa_t *spa = zio->io_spa; | |
8364 | arc_buf_hdr_t *hdr = cb->l2rcb_hdr; | |
8365 | blkptr_t *bp = zio->io_bp; | |
b5256303 TC |
8366 | uint8_t salt[ZIO_DATA_SALT_LEN]; |
8367 | uint8_t iv[ZIO_DATA_IV_LEN]; | |
8368 | uint8_t mac[ZIO_DATA_MAC_LEN]; | |
8369 | boolean_t no_crypt = B_FALSE; | |
8370 | ||
8371 | /* | |
8372 | * ZIL data is never be written to the L2ARC, so we don't need | |
8373 | * special handling for its unique MAC storage. | |
8374 | */ | |
8375 | ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG); | |
8376 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); | |
440a3eb9 | 8377 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
b5256303 | 8378 | |
440a3eb9 TC |
8379 | /* |
8380 | * If the data was encrypted, decrypt it now. Note that | |
8381 | * we must check the bp here and not the hdr, since the | |
8382 | * hdr does not have its encryption parameters updated | |
8383 | * until arc_read_done(). | |
8384 | */ | |
8385 | if (BP_IS_ENCRYPTED(bp)) { | |
e111c802 | 8386 | abd_t *eabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, |
a8d83e2a | 8387 | ARC_HDR_USE_RESERVE); |
b5256303 TC |
8388 | |
8389 | zio_crypt_decode_params_bp(bp, salt, iv); | |
8390 | zio_crypt_decode_mac_bp(bp, mac); | |
8391 | ||
be9a5c35 TC |
8392 | ret = spa_do_crypt_abd(B_FALSE, spa, &cb->l2rcb_zb, |
8393 | BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), | |
8394 | salt, iv, mac, HDR_GET_PSIZE(hdr), eabd, | |
8395 | hdr->b_l1hdr.b_pabd, &no_crypt); | |
b5256303 TC |
8396 | if (ret != 0) { |
8397 | arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr); | |
b5256303 TC |
8398 | goto error; |
8399 | } | |
8400 | ||
b5256303 TC |
8401 | /* |
8402 | * If we actually performed decryption, replace b_pabd | |
8403 | * with the decrypted data. Otherwise we can just throw | |
8404 | * our decryption buffer away. | |
8405 | */ | |
8406 | if (!no_crypt) { | |
8407 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
8408 | arc_hdr_size(hdr), hdr); | |
8409 | hdr->b_l1hdr.b_pabd = eabd; | |
8410 | zio->io_abd = eabd; | |
8411 | } else { | |
8412 | arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr); | |
8413 | } | |
8414 | } | |
8415 | ||
8416 | /* | |
8417 | * If the L2ARC block was compressed, but ARC compression | |
8418 | * is disabled we decompress the data into a new buffer and | |
8419 | * replace the existing data. | |
8420 | */ | |
8421 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
8422 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
e111c802 | 8423 | abd_t *cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, |
a8d83e2a | 8424 | ARC_HDR_USE_RESERVE); |
b5256303 TC |
8425 | void *tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr)); |
8426 | ||
8427 | ret = zio_decompress_data(HDR_GET_COMPRESS(hdr), | |
8428 | hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr), | |
10b3c7f5 | 8429 | HDR_GET_LSIZE(hdr), &hdr->b_complevel); |
b5256303 TC |
8430 | if (ret != 0) { |
8431 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
8432 | arc_free_data_abd(hdr, cabd, arc_hdr_size(hdr), hdr); | |
8433 | goto error; | |
8434 | } | |
8435 | ||
8436 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
8437 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
8438 | arc_hdr_size(hdr), hdr); | |
8439 | hdr->b_l1hdr.b_pabd = cabd; | |
8440 | zio->io_abd = cabd; | |
8441 | zio->io_size = HDR_GET_LSIZE(hdr); | |
8442 | } | |
8443 | ||
8444 | return (0); | |
8445 | ||
8446 | error: | |
8447 | return (ret); | |
8448 | } | |
8449 | ||
8450 | ||
34dc7c2f BB |
8451 | /* |
8452 | * A read to a cache device completed. Validate buffer contents before | |
8453 | * handing over to the regular ARC routines. | |
8454 | */ | |
8455 | static void | |
8456 | l2arc_read_done(zio_t *zio) | |
8457 | { | |
b5256303 | 8458 | int tfm_error = 0; |
b405837a | 8459 | l2arc_read_callback_t *cb = zio->io_private; |
34dc7c2f | 8460 | arc_buf_hdr_t *hdr; |
34dc7c2f | 8461 | kmutex_t *hash_lock; |
b405837a TC |
8462 | boolean_t valid_cksum; |
8463 | boolean_t using_rdata = (BP_IS_ENCRYPTED(&cb->l2rcb_bp) && | |
8464 | (cb->l2rcb_flags & ZIO_FLAG_RAW_ENCRYPT)); | |
b128c09f | 8465 | |
d3c2ae1c | 8466 | ASSERT3P(zio->io_vd, !=, NULL); |
b128c09f BB |
8467 | ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); |
8468 | ||
8469 | spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); | |
34dc7c2f | 8470 | |
d3c2ae1c GW |
8471 | ASSERT3P(cb, !=, NULL); |
8472 | hdr = cb->l2rcb_hdr; | |
8473 | ASSERT3P(hdr, !=, NULL); | |
34dc7c2f | 8474 | |
d3c2ae1c | 8475 | hash_lock = HDR_LOCK(hdr); |
34dc7c2f | 8476 | mutex_enter(hash_lock); |
428870ff | 8477 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
34dc7c2f | 8478 | |
82710e99 GDN |
8479 | /* |
8480 | * If the data was read into a temporary buffer, | |
8481 | * move it and free the buffer. | |
8482 | */ | |
8483 | if (cb->l2rcb_abd != NULL) { | |
8484 | ASSERT3U(arc_hdr_size(hdr), <, zio->io_size); | |
8485 | if (zio->io_error == 0) { | |
b405837a TC |
8486 | if (using_rdata) { |
8487 | abd_copy(hdr->b_crypt_hdr.b_rabd, | |
8488 | cb->l2rcb_abd, arc_hdr_size(hdr)); | |
8489 | } else { | |
8490 | abd_copy(hdr->b_l1hdr.b_pabd, | |
8491 | cb->l2rcb_abd, arc_hdr_size(hdr)); | |
8492 | } | |
82710e99 GDN |
8493 | } |
8494 | ||
8495 | /* | |
8496 | * The following must be done regardless of whether | |
8497 | * there was an error: | |
8498 | * - free the temporary buffer | |
8499 | * - point zio to the real ARC buffer | |
8500 | * - set zio size accordingly | |
8501 | * These are required because zio is either re-used for | |
8502 | * an I/O of the block in the case of the error | |
8503 | * or the zio is passed to arc_read_done() and it | |
8504 | * needs real data. | |
8505 | */ | |
8506 | abd_free(cb->l2rcb_abd); | |
8507 | zio->io_size = zio->io_orig_size = arc_hdr_size(hdr); | |
440a3eb9 | 8508 | |
b405837a | 8509 | if (using_rdata) { |
440a3eb9 TC |
8510 | ASSERT(HDR_HAS_RABD(hdr)); |
8511 | zio->io_abd = zio->io_orig_abd = | |
8512 | hdr->b_crypt_hdr.b_rabd; | |
8513 | } else { | |
8514 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
8515 | zio->io_abd = zio->io_orig_abd = hdr->b_l1hdr.b_pabd; | |
8516 | } | |
82710e99 GDN |
8517 | } |
8518 | ||
a6255b7f | 8519 | ASSERT3P(zio->io_abd, !=, NULL); |
3a17a7a9 | 8520 | |
34dc7c2f BB |
8521 | /* |
8522 | * Check this survived the L2ARC journey. | |
8523 | */ | |
b5256303 TC |
8524 | ASSERT(zio->io_abd == hdr->b_l1hdr.b_pabd || |
8525 | (HDR_HAS_RABD(hdr) && zio->io_abd == hdr->b_crypt_hdr.b_rabd)); | |
d3c2ae1c GW |
8526 | zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ |
8527 | zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ | |
10b3c7f5 | 8528 | zio->io_prop.zp_complevel = hdr->b_complevel; |
d3c2ae1c GW |
8529 | |
8530 | valid_cksum = arc_cksum_is_equal(hdr, zio); | |
b5256303 TC |
8531 | |
8532 | /* | |
8533 | * b_rabd will always match the data as it exists on disk if it is | |
8534 | * being used. Therefore if we are reading into b_rabd we do not | |
8535 | * attempt to untransform the data. | |
8536 | */ | |
8537 | if (valid_cksum && !using_rdata) | |
8538 | tfm_error = l2arc_untransform(zio, cb); | |
8539 | ||
8540 | if (valid_cksum && tfm_error == 0 && zio->io_error == 0 && | |
8541 | !HDR_L2_EVICTED(hdr)) { | |
34dc7c2f | 8542 | mutex_exit(hash_lock); |
d3c2ae1c | 8543 | zio->io_private = hdr; |
34dc7c2f BB |
8544 | arc_read_done(zio); |
8545 | } else { | |
34dc7c2f BB |
8546 | /* |
8547 | * Buffer didn't survive caching. Increment stats and | |
8548 | * reissue to the original storage device. | |
8549 | */ | |
b128c09f | 8550 | if (zio->io_error != 0) { |
34dc7c2f | 8551 | ARCSTAT_BUMP(arcstat_l2_io_error); |
b128c09f | 8552 | } else { |
2e528b49 | 8553 | zio->io_error = SET_ERROR(EIO); |
b128c09f | 8554 | } |
b5256303 | 8555 | if (!valid_cksum || tfm_error != 0) |
34dc7c2f BB |
8556 | ARCSTAT_BUMP(arcstat_l2_cksum_bad); |
8557 | ||
34dc7c2f | 8558 | /* |
b128c09f BB |
8559 | * If there's no waiter, issue an async i/o to the primary |
8560 | * storage now. If there *is* a waiter, the caller must | |
8561 | * issue the i/o in a context where it's OK to block. | |
34dc7c2f | 8562 | */ |
d164b209 BB |
8563 | if (zio->io_waiter == NULL) { |
8564 | zio_t *pio = zio_unique_parent(zio); | |
b5256303 TC |
8565 | void *abd = (using_rdata) ? |
8566 | hdr->b_crypt_hdr.b_rabd : hdr->b_l1hdr.b_pabd; | |
d164b209 BB |
8567 | |
8568 | ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); | |
8569 | ||
5ff2249f | 8570 | zio = zio_read(pio, zio->io_spa, zio->io_bp, |
b5256303 | 8571 | abd, zio->io_size, arc_read_done, |
d3c2ae1c | 8572 | hdr, zio->io_priority, cb->l2rcb_flags, |
5ff2249f AM |
8573 | &cb->l2rcb_zb); |
8574 | ||
8575 | /* | |
8576 | * Original ZIO will be freed, so we need to update | |
8577 | * ARC header with the new ZIO pointer to be used | |
8578 | * by zio_change_priority() in arc_read(). | |
8579 | */ | |
8580 | for (struct arc_callback *acb = hdr->b_l1hdr.b_acb; | |
8581 | acb != NULL; acb = acb->acb_next) | |
8582 | acb->acb_zio_head = zio; | |
8583 | ||
8584 | mutex_exit(hash_lock); | |
8585 | zio_nowait(zio); | |
8586 | } else { | |
8587 | mutex_exit(hash_lock); | |
d164b209 | 8588 | } |
34dc7c2f BB |
8589 | } |
8590 | ||
8591 | kmem_free(cb, sizeof (l2arc_read_callback_t)); | |
8592 | } | |
8593 | ||
8594 | /* | |
8595 | * This is the list priority from which the L2ARC will search for pages to | |
8596 | * cache. This is used within loops (0..3) to cycle through lists in the | |
8597 | * desired order. This order can have a significant effect on cache | |
8598 | * performance. | |
8599 | * | |
8600 | * Currently the metadata lists are hit first, MFU then MRU, followed by | |
8601 | * the data lists. This function returns a locked list, and also returns | |
8602 | * the lock pointer. | |
8603 | */ | |
ca0bf58d PS |
8604 | static multilist_sublist_t * |
8605 | l2arc_sublist_lock(int list_num) | |
34dc7c2f | 8606 | { |
ca0bf58d PS |
8607 | multilist_t *ml = NULL; |
8608 | unsigned int idx; | |
34dc7c2f | 8609 | |
4aafab91 | 8610 | ASSERT(list_num >= 0 && list_num < L2ARC_FEED_TYPES); |
34dc7c2f BB |
8611 | |
8612 | switch (list_num) { | |
8613 | case 0: | |
ffdf019c | 8614 | ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; |
34dc7c2f BB |
8615 | break; |
8616 | case 1: | |
ffdf019c | 8617 | ml = &arc_mru->arcs_list[ARC_BUFC_METADATA]; |
34dc7c2f BB |
8618 | break; |
8619 | case 2: | |
ffdf019c | 8620 | ml = &arc_mfu->arcs_list[ARC_BUFC_DATA]; |
34dc7c2f BB |
8621 | break; |
8622 | case 3: | |
ffdf019c | 8623 | ml = &arc_mru->arcs_list[ARC_BUFC_DATA]; |
34dc7c2f | 8624 | break; |
4aafab91 G |
8625 | default: |
8626 | return (NULL); | |
34dc7c2f BB |
8627 | } |
8628 | ||
ca0bf58d PS |
8629 | /* |
8630 | * Return a randomly-selected sublist. This is acceptable | |
8631 | * because the caller feeds only a little bit of data for each | |
8632 | * call (8MB). Subsequent calls will result in different | |
8633 | * sublists being selected. | |
8634 | */ | |
8635 | idx = multilist_get_random_index(ml); | |
8636 | return (multilist_sublist_lock(ml, idx)); | |
34dc7c2f BB |
8637 | } |
8638 | ||
77f6826b GA |
8639 | /* |
8640 | * Calculates the maximum overhead of L2ARC metadata log blocks for a given | |
657fd33b | 8641 | * L2ARC write size. l2arc_evict and l2arc_write_size need to include this |
77f6826b GA |
8642 | * overhead in processing to make sure there is enough headroom available |
8643 | * when writing buffers. | |
8644 | */ | |
8645 | static inline uint64_t | |
8646 | l2arc_log_blk_overhead(uint64_t write_sz, l2arc_dev_t *dev) | |
8647 | { | |
657fd33b | 8648 | if (dev->l2ad_log_entries == 0) { |
77f6826b GA |
8649 | return (0); |
8650 | } else { | |
8651 | uint64_t log_entries = write_sz >> SPA_MINBLOCKSHIFT; | |
8652 | ||
8653 | uint64_t log_blocks = (log_entries + | |
657fd33b GA |
8654 | dev->l2ad_log_entries - 1) / |
8655 | dev->l2ad_log_entries; | |
77f6826b GA |
8656 | |
8657 | return (vdev_psize_to_asize(dev->l2ad_vdev, | |
8658 | sizeof (l2arc_log_blk_phys_t)) * log_blocks); | |
8659 | } | |
8660 | } | |
8661 | ||
34dc7c2f BB |
8662 | /* |
8663 | * Evict buffers from the device write hand to the distance specified in | |
77f6826b | 8664 | * bytes. This distance may span populated buffers, it may span nothing. |
34dc7c2f BB |
8665 | * This is clearing a region on the L2ARC device ready for writing. |
8666 | * If the 'all' boolean is set, every buffer is evicted. | |
8667 | */ | |
8668 | static void | |
8669 | l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) | |
8670 | { | |
8671 | list_t *buflist; | |
2a432414 | 8672 | arc_buf_hdr_t *hdr, *hdr_prev; |
34dc7c2f BB |
8673 | kmutex_t *hash_lock; |
8674 | uint64_t taddr; | |
77f6826b | 8675 | l2arc_lb_ptr_buf_t *lb_ptr_buf, *lb_ptr_buf_prev; |
b7654bd7 GA |
8676 | vdev_t *vd = dev->l2ad_vdev; |
8677 | boolean_t rerun; | |
34dc7c2f | 8678 | |
b9541d6b | 8679 | buflist = &dev->l2ad_buflist; |
34dc7c2f | 8680 | |
37c22948 GA |
8681 | top: |
8682 | rerun = B_FALSE; | |
feff9dfe | 8683 | if (dev->l2ad_hand + distance > dev->l2ad_end) { |
34dc7c2f | 8684 | /* |
dd4bc569 | 8685 | * When there is no space to accommodate upcoming writes, |
77f6826b GA |
8686 | * evict to the end. Then bump the write and evict hands |
8687 | * to the start and iterate. This iteration does not | |
8688 | * happen indefinitely as we make sure in | |
8689 | * l2arc_write_size() that when the write hand is reset, | |
8690 | * the write size does not exceed the end of the device. | |
34dc7c2f | 8691 | */ |
37c22948 | 8692 | rerun = B_TRUE; |
34dc7c2f BB |
8693 | taddr = dev->l2ad_end; |
8694 | } else { | |
8695 | taddr = dev->l2ad_hand + distance; | |
8696 | } | |
8697 | DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, | |
8698 | uint64_t, taddr, boolean_t, all); | |
8699 | ||
b7654bd7 | 8700 | if (!all) { |
37c22948 | 8701 | /* |
b7654bd7 GA |
8702 | * This check has to be placed after deciding whether to |
8703 | * iterate (rerun). | |
37c22948 | 8704 | */ |
b7654bd7 GA |
8705 | if (dev->l2ad_first) { |
8706 | /* | |
8707 | * This is the first sweep through the device. There is | |
8708 | * nothing to evict. We have already trimmmed the | |
8709 | * whole device. | |
8710 | */ | |
8711 | goto out; | |
8712 | } else { | |
8713 | /* | |
8714 | * Trim the space to be evicted. | |
8715 | */ | |
8716 | if (vd->vdev_has_trim && dev->l2ad_evict < taddr && | |
8717 | l2arc_trim_ahead > 0) { | |
8718 | /* | |
8719 | * We have to drop the spa_config lock because | |
8720 | * vdev_trim_range() will acquire it. | |
8721 | * l2ad_evict already accounts for the label | |
8722 | * size. To prevent vdev_trim_ranges() from | |
8723 | * adding it again, we subtract it from | |
8724 | * l2ad_evict. | |
8725 | */ | |
8726 | spa_config_exit(dev->l2ad_spa, SCL_L2ARC, dev); | |
8727 | vdev_trim_simple(vd, | |
8728 | dev->l2ad_evict - VDEV_LABEL_START_SIZE, | |
8729 | taddr - dev->l2ad_evict); | |
8730 | spa_config_enter(dev->l2ad_spa, SCL_L2ARC, dev, | |
8731 | RW_READER); | |
8732 | } | |
37c22948 | 8733 | |
b7654bd7 GA |
8734 | /* |
8735 | * When rebuilding L2ARC we retrieve the evict hand | |
8736 | * from the header of the device. Of note, l2arc_evict() | |
8737 | * does not actually delete buffers from the cache | |
8738 | * device, but trimming may do so depending on the | |
8739 | * hardware implementation. Thus keeping track of the | |
8740 | * evict hand is useful. | |
8741 | */ | |
8742 | dev->l2ad_evict = MAX(dev->l2ad_evict, taddr); | |
8743 | } | |
8744 | } | |
77f6826b | 8745 | |
37c22948 | 8746 | retry: |
b9541d6b | 8747 | mutex_enter(&dev->l2ad_mtx); |
77f6826b GA |
8748 | /* |
8749 | * We have to account for evicted log blocks. Run vdev_space_update() | |
8750 | * on log blocks whose offset (in bytes) is before the evicted offset | |
8751 | * (in bytes) by searching in the list of pointers to log blocks | |
8752 | * present in the L2ARC device. | |
8753 | */ | |
8754 | for (lb_ptr_buf = list_tail(&dev->l2ad_lbptr_list); lb_ptr_buf; | |
8755 | lb_ptr_buf = lb_ptr_buf_prev) { | |
8756 | ||
8757 | lb_ptr_buf_prev = list_prev(&dev->l2ad_lbptr_list, lb_ptr_buf); | |
8758 | ||
657fd33b GA |
8759 | /* L2BLK_GET_PSIZE returns aligned size for log blocks */ |
8760 | uint64_t asize = L2BLK_GET_PSIZE( | |
8761 | (lb_ptr_buf->lb_ptr)->lbp_prop); | |
8762 | ||
77f6826b GA |
8763 | /* |
8764 | * We don't worry about log blocks left behind (ie | |
657fd33b | 8765 | * lbp_payload_start < l2ad_hand) because l2arc_write_buffers() |
77f6826b GA |
8766 | * will never write more than l2arc_evict() evicts. |
8767 | */ | |
8768 | if (!all && l2arc_log_blkptr_valid(dev, lb_ptr_buf->lb_ptr)) { | |
8769 | break; | |
8770 | } else { | |
b7654bd7 | 8771 | vdev_space_update(vd, -asize, 0, 0); |
657fd33b GA |
8772 | ARCSTAT_INCR(arcstat_l2_log_blk_asize, -asize); |
8773 | ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count); | |
8774 | zfs_refcount_remove_many(&dev->l2ad_lb_asize, asize, | |
8775 | lb_ptr_buf); | |
4836d293 RN |
8776 | (void) zfs_refcount_remove(&dev->l2ad_lb_count, |
8777 | lb_ptr_buf); | |
77f6826b GA |
8778 | list_remove(&dev->l2ad_lbptr_list, lb_ptr_buf); |
8779 | kmem_free(lb_ptr_buf->lb_ptr, | |
8780 | sizeof (l2arc_log_blkptr_t)); | |
8781 | kmem_free(lb_ptr_buf, sizeof (l2arc_lb_ptr_buf_t)); | |
8782 | } | |
8783 | } | |
8784 | ||
2a432414 GW |
8785 | for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { |
8786 | hdr_prev = list_prev(buflist, hdr); | |
34dc7c2f | 8787 | |
ca6c7a94 | 8788 | ASSERT(!HDR_EMPTY(hdr)); |
2a432414 | 8789 | hash_lock = HDR_LOCK(hdr); |
ca0bf58d PS |
8790 | |
8791 | /* | |
8792 | * We cannot use mutex_enter or else we can deadlock | |
8793 | * with l2arc_write_buffers (due to swapping the order | |
8794 | * the hash lock and l2ad_mtx are taken). | |
8795 | */ | |
34dc7c2f BB |
8796 | if (!mutex_tryenter(hash_lock)) { |
8797 | /* | |
8798 | * Missed the hash lock. Retry. | |
8799 | */ | |
8800 | ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); | |
b9541d6b | 8801 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
8802 | mutex_enter(hash_lock); |
8803 | mutex_exit(hash_lock); | |
37c22948 | 8804 | goto retry; |
34dc7c2f BB |
8805 | } |
8806 | ||
f06f53fa AG |
8807 | /* |
8808 | * A header can't be on this list if it doesn't have L2 header. | |
8809 | */ | |
8810 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
34dc7c2f | 8811 | |
f06f53fa AG |
8812 | /* Ensure this header has finished being written. */ |
8813 | ASSERT(!HDR_L2_WRITING(hdr)); | |
8814 | ASSERT(!HDR_L2_WRITE_HEAD(hdr)); | |
8815 | ||
77f6826b | 8816 | if (!all && (hdr->b_l2hdr.b_daddr >= dev->l2ad_evict || |
b9541d6b | 8817 | hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { |
34dc7c2f BB |
8818 | /* |
8819 | * We've evicted to the target address, | |
8820 | * or the end of the device. | |
8821 | */ | |
8822 | mutex_exit(hash_lock); | |
8823 | break; | |
8824 | } | |
8825 | ||
b9541d6b | 8826 | if (!HDR_HAS_L1HDR(hdr)) { |
2a432414 | 8827 | ASSERT(!HDR_L2_READING(hdr)); |
34dc7c2f BB |
8828 | /* |
8829 | * This doesn't exist in the ARC. Destroy. | |
8830 | * arc_hdr_destroy() will call list_remove() | |
01850391 | 8831 | * and decrement arcstat_l2_lsize. |
34dc7c2f | 8832 | */ |
c935fe2e | 8833 | arc_change_state(arc_anon, hdr); |
2a432414 | 8834 | arc_hdr_destroy(hdr); |
34dc7c2f | 8835 | } else { |
b9541d6b CW |
8836 | ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); |
8837 | ARCSTAT_BUMP(arcstat_l2_evict_l1cached); | |
b128c09f BB |
8838 | /* |
8839 | * Invalidate issued or about to be issued | |
8840 | * reads, since we may be about to write | |
8841 | * over this location. | |
8842 | */ | |
2a432414 | 8843 | if (HDR_L2_READING(hdr)) { |
b128c09f | 8844 | ARCSTAT_BUMP(arcstat_l2_evict_reading); |
d3c2ae1c | 8845 | arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED); |
b128c09f BB |
8846 | } |
8847 | ||
d962d5da | 8848 | arc_hdr_l2hdr_destroy(hdr); |
34dc7c2f BB |
8849 | } |
8850 | mutex_exit(hash_lock); | |
8851 | } | |
b9541d6b | 8852 | mutex_exit(&dev->l2ad_mtx); |
37c22948 GA |
8853 | |
8854 | out: | |
77f6826b GA |
8855 | /* |
8856 | * We need to check if we evict all buffers, otherwise we may iterate | |
8857 | * unnecessarily. | |
8858 | */ | |
8859 | if (!all && rerun) { | |
37c22948 GA |
8860 | /* |
8861 | * Bump device hand to the device start if it is approaching the | |
8862 | * end. l2arc_evict() has already evicted ahead for this case. | |
8863 | */ | |
8864 | dev->l2ad_hand = dev->l2ad_start; | |
77f6826b | 8865 | dev->l2ad_evict = dev->l2ad_start; |
37c22948 GA |
8866 | dev->l2ad_first = B_FALSE; |
8867 | goto top; | |
8868 | } | |
657fd33b | 8869 | |
2c210f68 GA |
8870 | if (!all) { |
8871 | /* | |
8872 | * In case of cache device removal (all) the following | |
8873 | * assertions may be violated without functional consequences | |
8874 | * as the device is about to be removed. | |
8875 | */ | |
8876 | ASSERT3U(dev->l2ad_hand + distance, <, dev->l2ad_end); | |
8877 | if (!dev->l2ad_first) | |
feff9dfe | 8878 | ASSERT3U(dev->l2ad_hand, <=, dev->l2ad_evict); |
2c210f68 | 8879 | } |
34dc7c2f BB |
8880 | } |
8881 | ||
b5256303 TC |
8882 | /* |
8883 | * Handle any abd transforms that might be required for writing to the L2ARC. | |
8884 | * If successful, this function will always return an abd with the data | |
8885 | * transformed as it is on disk in a new abd of asize bytes. | |
8886 | */ | |
8887 | static int | |
8888 | l2arc_apply_transforms(spa_t *spa, arc_buf_hdr_t *hdr, uint64_t asize, | |
8889 | abd_t **abd_out) | |
8890 | { | |
8891 | int ret; | |
8892 | void *tmp = NULL; | |
8893 | abd_t *cabd = NULL, *eabd = NULL, *to_write = hdr->b_l1hdr.b_pabd; | |
8894 | enum zio_compress compress = HDR_GET_COMPRESS(hdr); | |
8895 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
8896 | uint64_t size = arc_hdr_size(hdr); | |
8897 | boolean_t ismd = HDR_ISTYPE_METADATA(hdr); | |
8898 | boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
8899 | dsl_crypto_key_t *dck = NULL; | |
8900 | uint8_t mac[ZIO_DATA_MAC_LEN] = { 0 }; | |
4807c0ba | 8901 | boolean_t no_crypt = B_FALSE; |
b5256303 TC |
8902 | |
8903 | ASSERT((HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
8904 | !HDR_COMPRESSION_ENABLED(hdr)) || | |
8905 | HDR_ENCRYPTED(hdr) || HDR_SHARED_DATA(hdr) || psize != asize); | |
8906 | ASSERT3U(psize, <=, asize); | |
8907 | ||
8908 | /* | |
8909 | * If this data simply needs its own buffer, we simply allocate it | |
b7109a41 | 8910 | * and copy the data. This may be done to eliminate a dependency on a |
b5256303 TC |
8911 | * shared buffer or to reallocate the buffer to match asize. |
8912 | */ | |
4807c0ba | 8913 | if (HDR_HAS_RABD(hdr) && asize != psize) { |
10adee27 | 8914 | ASSERT3U(asize, >=, psize); |
4807c0ba | 8915 | to_write = abd_alloc_for_io(asize, ismd); |
10adee27 TC |
8916 | abd_copy(to_write, hdr->b_crypt_hdr.b_rabd, psize); |
8917 | if (psize != asize) | |
8918 | abd_zero_off(to_write, psize, asize - psize); | |
4807c0ba TC |
8919 | goto out; |
8920 | } | |
8921 | ||
b5256303 TC |
8922 | if ((compress == ZIO_COMPRESS_OFF || HDR_COMPRESSION_ENABLED(hdr)) && |
8923 | !HDR_ENCRYPTED(hdr)) { | |
8924 | ASSERT3U(size, ==, psize); | |
8925 | to_write = abd_alloc_for_io(asize, ismd); | |
8926 | abd_copy(to_write, hdr->b_l1hdr.b_pabd, size); | |
8927 | if (size != asize) | |
8928 | abd_zero_off(to_write, size, asize - size); | |
8929 | goto out; | |
8930 | } | |
8931 | ||
8932 | if (compress != ZIO_COMPRESS_OFF && !HDR_COMPRESSION_ENABLED(hdr)) { | |
7bf06f72 RE |
8933 | /* |
8934 | * In some cases, we can wind up with size > asize, so | |
8935 | * we need to opt for the larger allocation option here. | |
8936 | * | |
8937 | * (We also need abd_return_buf_copy in all cases because | |
8938 | * it's an ASSERT() to modify the buffer before returning it | |
8939 | * with arc_return_buf(), and all the compressors | |
8940 | * write things before deciding to fail compression in nearly | |
8941 | * every case.) | |
8942 | */ | |
741c215b PD |
8943 | uint64_t bufsize = MAX(size, asize); |
8944 | cabd = abd_alloc_for_io(bufsize, ismd); | |
8945 | tmp = abd_borrow_buf(cabd, bufsize); | |
b5256303 | 8946 | |
bff26b02 | 8947 | psize = zio_compress_data(compress, to_write, &tmp, size, |
10b3c7f5 MN |
8948 | hdr->b_complevel); |
8949 | ||
7bf06f72 RE |
8950 | if (psize >= asize) { |
8951 | psize = HDR_GET_PSIZE(hdr); | |
741c215b | 8952 | abd_return_buf_copy(cabd, tmp, bufsize); |
10b3c7f5 MN |
8953 | HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); |
8954 | to_write = cabd; | |
7bf06f72 RE |
8955 | abd_copy(to_write, hdr->b_l1hdr.b_pabd, psize); |
8956 | if (psize != asize) | |
8957 | abd_zero_off(to_write, psize, asize - psize); | |
10b3c7f5 MN |
8958 | goto encrypt; |
8959 | } | |
b5256303 TC |
8960 | ASSERT3U(psize, <=, HDR_GET_PSIZE(hdr)); |
8961 | if (psize < asize) | |
741c215b | 8962 | memset((char *)tmp + psize, 0, bufsize - psize); |
b5256303 | 8963 | psize = HDR_GET_PSIZE(hdr); |
741c215b | 8964 | abd_return_buf_copy(cabd, tmp, bufsize); |
b5256303 TC |
8965 | to_write = cabd; |
8966 | } | |
8967 | ||
10b3c7f5 | 8968 | encrypt: |
b5256303 TC |
8969 | if (HDR_ENCRYPTED(hdr)) { |
8970 | eabd = abd_alloc_for_io(asize, ismd); | |
8971 | ||
8972 | /* | |
8973 | * If the dataset was disowned before the buffer | |
8974 | * made it to this point, the key to re-encrypt | |
8975 | * it won't be available. In this case we simply | |
8976 | * won't write the buffer to the L2ARC. | |
8977 | */ | |
8978 | ret = spa_keystore_lookup_key(spa, hdr->b_crypt_hdr.b_dsobj, | |
8979 | FTAG, &dck); | |
8980 | if (ret != 0) | |
8981 | goto error; | |
8982 | ||
10fa2545 | 8983 | ret = zio_do_crypt_abd(B_TRUE, &dck->dck_key, |
be9a5c35 TC |
8984 | hdr->b_crypt_hdr.b_ot, bswap, hdr->b_crypt_hdr.b_salt, |
8985 | hdr->b_crypt_hdr.b_iv, mac, psize, to_write, eabd, | |
8986 | &no_crypt); | |
b5256303 TC |
8987 | if (ret != 0) |
8988 | goto error; | |
8989 | ||
4807c0ba TC |
8990 | if (no_crypt) |
8991 | abd_copy(eabd, to_write, psize); | |
b5256303 TC |
8992 | |
8993 | if (psize != asize) | |
8994 | abd_zero_off(eabd, psize, asize - psize); | |
8995 | ||
8996 | /* assert that the MAC we got here matches the one we saved */ | |
861166b0 | 8997 | ASSERT0(memcmp(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN)); |
b5256303 TC |
8998 | spa_keystore_dsl_key_rele(spa, dck, FTAG); |
8999 | ||
9000 | if (to_write == cabd) | |
9001 | abd_free(cabd); | |
9002 | ||
9003 | to_write = eabd; | |
9004 | } | |
9005 | ||
9006 | out: | |
9007 | ASSERT3P(to_write, !=, hdr->b_l1hdr.b_pabd); | |
9008 | *abd_out = to_write; | |
9009 | return (0); | |
9010 | ||
9011 | error: | |
9012 | if (dck != NULL) | |
9013 | spa_keystore_dsl_key_rele(spa, dck, FTAG); | |
9014 | if (cabd != NULL) | |
9015 | abd_free(cabd); | |
9016 | if (eabd != NULL) | |
9017 | abd_free(eabd); | |
9018 | ||
9019 | *abd_out = NULL; | |
9020 | return (ret); | |
9021 | } | |
9022 | ||
77f6826b GA |
9023 | static void |
9024 | l2arc_blk_fetch_done(zio_t *zio) | |
9025 | { | |
9026 | l2arc_read_callback_t *cb; | |
9027 | ||
9028 | cb = zio->io_private; | |
9029 | if (cb->l2rcb_abd != NULL) | |
e2af2acc | 9030 | abd_free(cb->l2rcb_abd); |
77f6826b GA |
9031 | kmem_free(cb, sizeof (l2arc_read_callback_t)); |
9032 | } | |
9033 | ||
34dc7c2f BB |
9034 | /* |
9035 | * Find and write ARC buffers to the L2ARC device. | |
9036 | * | |
2a432414 | 9037 | * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid |
34dc7c2f | 9038 | * for reading until they have completed writing. |
3a17a7a9 SK |
9039 | * The headroom_boost is an in-out parameter used to maintain headroom boost |
9040 | * state between calls to this function. | |
9041 | * | |
9042 | * Returns the number of bytes actually written (which may be smaller than | |
77f6826b GA |
9043 | * the delta by which the device hand has changed due to alignment and the |
9044 | * writing of log blocks). | |
34dc7c2f | 9045 | */ |
d164b209 | 9046 | static uint64_t |
d3c2ae1c | 9047 | l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) |
34dc7c2f | 9048 | { |
77f6826b GA |
9049 | arc_buf_hdr_t *hdr, *hdr_prev, *head; |
9050 | uint64_t write_asize, write_psize, write_lsize, headroom; | |
9051 | boolean_t full; | |
9052 | l2arc_write_callback_t *cb = NULL; | |
9053 | zio_t *pio, *wzio; | |
9054 | uint64_t guid = spa_load_guid(spa); | |
0ae184a6 | 9055 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; |
34dc7c2f | 9056 | |
d3c2ae1c | 9057 | ASSERT3P(dev->l2ad_vdev, !=, NULL); |
3a17a7a9 | 9058 | |
34dc7c2f | 9059 | pio = NULL; |
01850391 | 9060 | write_lsize = write_asize = write_psize = 0; |
34dc7c2f | 9061 | full = B_FALSE; |
b9541d6b | 9062 | head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); |
d3c2ae1c | 9063 | arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR); |
3a17a7a9 | 9064 | |
34dc7c2f BB |
9065 | /* |
9066 | * Copy buffers for L2ARC writing. | |
9067 | */ | |
4a90d4d6 | 9068 | for (int pass = 0; pass < L2ARC_FEED_TYPES; pass++) { |
feb3a7ee | 9069 | /* |
4a90d4d6 | 9070 | * If pass == 1 or 3, we cache MRU metadata and data |
feb3a7ee GA |
9071 | * respectively. |
9072 | */ | |
9073 | if (l2arc_mfuonly) { | |
4a90d4d6 | 9074 | if (pass == 1 || pass == 3) |
feb3a7ee GA |
9075 | continue; |
9076 | } | |
9077 | ||
4a90d4d6 | 9078 | multilist_sublist_t *mls = l2arc_sublist_lock(pass); |
3a17a7a9 SK |
9079 | uint64_t passed_sz = 0; |
9080 | ||
4aafab91 G |
9081 | VERIFY3P(mls, !=, NULL); |
9082 | ||
b128c09f BB |
9083 | /* |
9084 | * L2ARC fast warmup. | |
9085 | * | |
9086 | * Until the ARC is warm and starts to evict, read from the | |
9087 | * head of the ARC lists rather than the tail. | |
9088 | */ | |
b128c09f | 9089 | if (arc_warm == B_FALSE) |
ca0bf58d | 9090 | hdr = multilist_sublist_head(mls); |
b128c09f | 9091 | else |
ca0bf58d | 9092 | hdr = multilist_sublist_tail(mls); |
b128c09f | 9093 | |
3a17a7a9 | 9094 | headroom = target_sz * l2arc_headroom; |
d3c2ae1c | 9095 | if (zfs_compressed_arc_enabled) |
3a17a7a9 SK |
9096 | headroom = (headroom * l2arc_headroom_boost) / 100; |
9097 | ||
2a432414 | 9098 | for (; hdr; hdr = hdr_prev) { |
3a17a7a9 | 9099 | kmutex_t *hash_lock; |
b5256303 | 9100 | abd_t *to_write = NULL; |
3a17a7a9 | 9101 | |
b128c09f | 9102 | if (arc_warm == B_FALSE) |
ca0bf58d | 9103 | hdr_prev = multilist_sublist_next(mls, hdr); |
b128c09f | 9104 | else |
ca0bf58d | 9105 | hdr_prev = multilist_sublist_prev(mls, hdr); |
34dc7c2f | 9106 | |
2a432414 | 9107 | hash_lock = HDR_LOCK(hdr); |
3a17a7a9 | 9108 | if (!mutex_tryenter(hash_lock)) { |
34dc7c2f BB |
9109 | /* |
9110 | * Skip this buffer rather than waiting. | |
9111 | */ | |
9112 | continue; | |
9113 | } | |
9114 | ||
d3c2ae1c | 9115 | passed_sz += HDR_GET_LSIZE(hdr); |
77f6826b | 9116 | if (l2arc_headroom != 0 && passed_sz > headroom) { |
34dc7c2f BB |
9117 | /* |
9118 | * Searched too far. | |
9119 | */ | |
9120 | mutex_exit(hash_lock); | |
9121 | break; | |
9122 | } | |
9123 | ||
2a432414 | 9124 | if (!l2arc_write_eligible(guid, hdr)) { |
34dc7c2f BB |
9125 | mutex_exit(hash_lock); |
9126 | continue; | |
9127 | } | |
9128 | ||
01850391 AG |
9129 | ASSERT(HDR_HAS_L1HDR(hdr)); |
9130 | ||
9131 | ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); | |
01850391 | 9132 | ASSERT3U(arc_hdr_size(hdr), >, 0); |
b5256303 TC |
9133 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || |
9134 | HDR_HAS_RABD(hdr)); | |
9135 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
01850391 AG |
9136 | uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, |
9137 | psize); | |
9138 | ||
feff9dfe GA |
9139 | /* |
9140 | * If the allocated size of this buffer plus the max | |
9141 | * size for the pending log block exceeds the evicted | |
9142 | * target size, terminate writing buffers for this run. | |
9143 | */ | |
9144 | if (write_asize + asize + | |
9145 | sizeof (l2arc_log_blk_phys_t) > target_sz) { | |
34dc7c2f BB |
9146 | full = B_TRUE; |
9147 | mutex_exit(hash_lock); | |
9148 | break; | |
9149 | } | |
9150 | ||
b5256303 TC |
9151 | /* |
9152 | * We rely on the L1 portion of the header below, so | |
9153 | * it's invalid for this header to have been evicted out | |
9154 | * of the ghost cache, prior to being written out. The | |
9155 | * ARC_FLAG_L2_WRITING bit ensures this won't happen. | |
9156 | */ | |
9157 | arc_hdr_set_flags(hdr, ARC_FLAG_L2_WRITING); | |
b5256303 TC |
9158 | |
9159 | /* | |
9160 | * If this header has b_rabd, we can use this since it | |
9161 | * must always match the data exactly as it exists on | |
9777044f | 9162 | * disk. Otherwise, the L2ARC can normally use the |
b5256303 TC |
9163 | * hdr's data, but if we're sharing data between the |
9164 | * hdr and one of its bufs, L2ARC needs its own copy of | |
9165 | * the data so that the ZIO below can't race with the | |
9166 | * buf consumer. To ensure that this copy will be | |
9167 | * available for the lifetime of the ZIO and be cleaned | |
9168 | * up afterwards, we add it to the l2arc_free_on_write | |
9169 | * queue. If we need to apply any transforms to the | |
9170 | * data (compression, encryption) we will also need the | |
9171 | * extra buffer. | |
9172 | */ | |
9173 | if (HDR_HAS_RABD(hdr) && psize == asize) { | |
9174 | to_write = hdr->b_crypt_hdr.b_rabd; | |
9175 | } else if ((HDR_COMPRESSION_ENABLED(hdr) || | |
9176 | HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) && | |
9177 | !HDR_ENCRYPTED(hdr) && !HDR_SHARED_DATA(hdr) && | |
9178 | psize == asize) { | |
9179 | to_write = hdr->b_l1hdr.b_pabd; | |
9180 | } else { | |
9181 | int ret; | |
9182 | arc_buf_contents_t type = arc_buf_type(hdr); | |
9183 | ||
9184 | ret = l2arc_apply_transforms(spa, hdr, asize, | |
9185 | &to_write); | |
9186 | if (ret != 0) { | |
9187 | arc_hdr_clear_flags(hdr, | |
9188 | ARC_FLAG_L2_WRITING); | |
9189 | mutex_exit(hash_lock); | |
9190 | continue; | |
9191 | } | |
9192 | ||
9193 | l2arc_free_abd_on_write(to_write, asize, type); | |
9194 | } | |
9195 | ||
34dc7c2f BB |
9196 | if (pio == NULL) { |
9197 | /* | |
9198 | * Insert a dummy header on the buflist so | |
9199 | * l2arc_write_done() can find where the | |
9200 | * write buffers begin without searching. | |
9201 | */ | |
ca0bf58d | 9202 | mutex_enter(&dev->l2ad_mtx); |
b9541d6b | 9203 | list_insert_head(&dev->l2ad_buflist, head); |
ca0bf58d | 9204 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 9205 | |
96c080cb BB |
9206 | cb = kmem_alloc( |
9207 | sizeof (l2arc_write_callback_t), KM_SLEEP); | |
34dc7c2f BB |
9208 | cb->l2wcb_dev = dev; |
9209 | cb->l2wcb_head = head; | |
657fd33b GA |
9210 | /* |
9211 | * Create a list to save allocated abd buffers | |
9212 | * for l2arc_log_blk_commit(). | |
9213 | */ | |
77f6826b GA |
9214 | list_create(&cb->l2wcb_abd_list, |
9215 | sizeof (l2arc_lb_abd_buf_t), | |
9216 | offsetof(l2arc_lb_abd_buf_t, node)); | |
34dc7c2f BB |
9217 | pio = zio_root(spa, l2arc_write_done, cb, |
9218 | ZIO_FLAG_CANFAIL); | |
9219 | } | |
9220 | ||
b9541d6b | 9221 | hdr->b_l2hdr.b_dev = dev; |
b9541d6b | 9222 | hdr->b_l2hdr.b_hits = 0; |
3a17a7a9 | 9223 | |
d3c2ae1c | 9224 | hdr->b_l2hdr.b_daddr = dev->l2ad_hand; |
08532162 GA |
9225 | hdr->b_l2hdr.b_arcs_state = |
9226 | hdr->b_l1hdr.b_state->arcs_state; | |
b5256303 | 9227 | arc_hdr_set_flags(hdr, ARC_FLAG_HAS_L2HDR); |
3a17a7a9 | 9228 | |
ca0bf58d | 9229 | mutex_enter(&dev->l2ad_mtx); |
b9541d6b | 9230 | list_insert_head(&dev->l2ad_buflist, hdr); |
ca0bf58d | 9231 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 9232 | |
424fd7c3 | 9233 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, |
b5256303 | 9234 | arc_hdr_size(hdr), hdr); |
3a17a7a9 | 9235 | |
34dc7c2f | 9236 | wzio = zio_write_phys(pio, dev->l2ad_vdev, |
82710e99 | 9237 | hdr->b_l2hdr.b_daddr, asize, to_write, |
d3c2ae1c GW |
9238 | ZIO_CHECKSUM_OFF, NULL, hdr, |
9239 | ZIO_PRIORITY_ASYNC_WRITE, | |
34dc7c2f BB |
9240 | ZIO_FLAG_CANFAIL, B_FALSE); |
9241 | ||
01850391 | 9242 | write_lsize += HDR_GET_LSIZE(hdr); |
34dc7c2f BB |
9243 | DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, |
9244 | zio_t *, wzio); | |
d962d5da | 9245 | |
01850391 AG |
9246 | write_psize += psize; |
9247 | write_asize += asize; | |
d3c2ae1c | 9248 | dev->l2ad_hand += asize; |
08532162 | 9249 | l2arc_hdr_arcstats_increment(hdr); |
7558997d | 9250 | vdev_space_update(dev->l2ad_vdev, asize, 0, 0); |
d3c2ae1c GW |
9251 | |
9252 | mutex_exit(hash_lock); | |
9253 | ||
77f6826b GA |
9254 | /* |
9255 | * Append buf info to current log and commit if full. | |
9256 | * arcstat_l2_{size,asize} kstats are updated | |
9257 | * internally. | |
9258 | */ | |
bcd53210 GA |
9259 | if (l2arc_log_blk_insert(dev, hdr)) { |
9260 | /* | |
feff9dfe | 9261 | * l2ad_hand will be adjusted in |
bcd53210 GA |
9262 | * l2arc_log_blk_commit(). |
9263 | */ | |
9264 | write_asize += | |
9265 | l2arc_log_blk_commit(dev, pio, cb); | |
9266 | } | |
77f6826b | 9267 | |
9cdf7b1f | 9268 | zio_nowait(wzio); |
34dc7c2f | 9269 | } |
d3c2ae1c GW |
9270 | |
9271 | multilist_sublist_unlock(mls); | |
9272 | ||
9273 | if (full == B_TRUE) | |
9274 | break; | |
34dc7c2f | 9275 | } |
34dc7c2f | 9276 | |
d3c2ae1c GW |
9277 | /* No buffers selected for writing? */ |
9278 | if (pio == NULL) { | |
01850391 | 9279 | ASSERT0(write_lsize); |
d3c2ae1c GW |
9280 | ASSERT(!HDR_HAS_L1HDR(head)); |
9281 | kmem_cache_free(hdr_l2only_cache, head); | |
77f6826b GA |
9282 | |
9283 | /* | |
9284 | * Although we did not write any buffers l2ad_evict may | |
9285 | * have advanced. | |
9286 | */ | |
0ae184a6 GA |
9287 | if (dev->l2ad_evict != l2dhdr->dh_evict) |
9288 | l2arc_dev_hdr_update(dev); | |
77f6826b | 9289 | |
d3c2ae1c GW |
9290 | return (0); |
9291 | } | |
34dc7c2f | 9292 | |
657fd33b GA |
9293 | if (!dev->l2ad_first) |
9294 | ASSERT3U(dev->l2ad_hand, <=, dev->l2ad_evict); | |
9295 | ||
3a17a7a9 | 9296 | ASSERT3U(write_asize, <=, target_sz); |
34dc7c2f | 9297 | ARCSTAT_BUMP(arcstat_l2_writes_sent); |
01850391 | 9298 | ARCSTAT_INCR(arcstat_l2_write_bytes, write_psize); |
34dc7c2f | 9299 | |
d164b209 | 9300 | dev->l2ad_writing = B_TRUE; |
34dc7c2f | 9301 | (void) zio_wait(pio); |
d164b209 BB |
9302 | dev->l2ad_writing = B_FALSE; |
9303 | ||
2054f35e GA |
9304 | /* |
9305 | * Update the device header after the zio completes as | |
9306 | * l2arc_write_done() may have updated the memory holding the log block | |
9307 | * pointers in the device header. | |
9308 | */ | |
9309 | l2arc_dev_hdr_update(dev); | |
9310 | ||
3a17a7a9 SK |
9311 | return (write_asize); |
9312 | } | |
9313 | ||
523e1295 AM |
9314 | static boolean_t |
9315 | l2arc_hdr_limit_reached(void) | |
9316 | { | |
c4c162c1 | 9317 | int64_t s = aggsum_upper_bound(&arc_sums.arcstat_l2_hdr_size); |
523e1295 | 9318 | |
a8d83e2a | 9319 | return (arc_reclaim_needed() || |
523e1295 AM |
9320 | (s > (arc_warm ? arc_c : arc_c_max) * l2arc_meta_percent / 100)); |
9321 | } | |
9322 | ||
34dc7c2f BB |
9323 | /* |
9324 | * This thread feeds the L2ARC at regular intervals. This is the beating | |
9325 | * heart of the L2ARC. | |
9326 | */ | |
460748d4 | 9327 | static __attribute__((noreturn)) void |
c25b8f99 | 9328 | l2arc_feed_thread(void *unused) |
34dc7c2f | 9329 | { |
14e4e3cb | 9330 | (void) unused; |
34dc7c2f BB |
9331 | callb_cpr_t cpr; |
9332 | l2arc_dev_t *dev; | |
9333 | spa_t *spa; | |
d164b209 | 9334 | uint64_t size, wrote; |
428870ff | 9335 | clock_t begin, next = ddi_get_lbolt(); |
40d06e3c | 9336 | fstrans_cookie_t cookie; |
34dc7c2f BB |
9337 | |
9338 | CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); | |
9339 | ||
9340 | mutex_enter(&l2arc_feed_thr_lock); | |
9341 | ||
40d06e3c | 9342 | cookie = spl_fstrans_mark(); |
34dc7c2f | 9343 | while (l2arc_thread_exit == 0) { |
34dc7c2f | 9344 | CALLB_CPR_SAFE_BEGIN(&cpr); |
ac6e5fb2 | 9345 | (void) cv_timedwait_idle(&l2arc_feed_thr_cv, |
5b63b3eb | 9346 | &l2arc_feed_thr_lock, next); |
34dc7c2f | 9347 | CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); |
428870ff | 9348 | next = ddi_get_lbolt() + hz; |
34dc7c2f BB |
9349 | |
9350 | /* | |
b128c09f | 9351 | * Quick check for L2ARC devices. |
34dc7c2f BB |
9352 | */ |
9353 | mutex_enter(&l2arc_dev_mtx); | |
9354 | if (l2arc_ndev == 0) { | |
9355 | mutex_exit(&l2arc_dev_mtx); | |
9356 | continue; | |
9357 | } | |
b128c09f | 9358 | mutex_exit(&l2arc_dev_mtx); |
428870ff | 9359 | begin = ddi_get_lbolt(); |
34dc7c2f BB |
9360 | |
9361 | /* | |
b128c09f BB |
9362 | * This selects the next l2arc device to write to, and in |
9363 | * doing so the next spa to feed from: dev->l2ad_spa. This | |
9364 | * will return NULL if there are now no l2arc devices or if | |
9365 | * they are all faulted. | |
9366 | * | |
9367 | * If a device is returned, its spa's config lock is also | |
9368 | * held to prevent device removal. l2arc_dev_get_next() | |
9369 | * will grab and release l2arc_dev_mtx. | |
34dc7c2f | 9370 | */ |
b128c09f | 9371 | if ((dev = l2arc_dev_get_next()) == NULL) |
34dc7c2f | 9372 | continue; |
b128c09f BB |
9373 | |
9374 | spa = dev->l2ad_spa; | |
d3c2ae1c | 9375 | ASSERT3P(spa, !=, NULL); |
34dc7c2f | 9376 | |
572e2857 BB |
9377 | /* |
9378 | * If the pool is read-only then force the feed thread to | |
9379 | * sleep a little longer. | |
9380 | */ | |
9381 | if (!spa_writeable(spa)) { | |
9382 | next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; | |
9383 | spa_config_exit(spa, SCL_L2ARC, dev); | |
9384 | continue; | |
9385 | } | |
9386 | ||
34dc7c2f | 9387 | /* |
b128c09f | 9388 | * Avoid contributing to memory pressure. |
34dc7c2f | 9389 | */ |
523e1295 | 9390 | if (l2arc_hdr_limit_reached()) { |
b128c09f BB |
9391 | ARCSTAT_BUMP(arcstat_l2_abort_lowmem); |
9392 | spa_config_exit(spa, SCL_L2ARC, dev); | |
34dc7c2f BB |
9393 | continue; |
9394 | } | |
b128c09f | 9395 | |
34dc7c2f BB |
9396 | ARCSTAT_BUMP(arcstat_l2_feeds); |
9397 | ||
37c22948 | 9398 | size = l2arc_write_size(dev); |
b128c09f | 9399 | |
34dc7c2f BB |
9400 | /* |
9401 | * Evict L2ARC buffers that will be overwritten. | |
9402 | */ | |
b128c09f | 9403 | l2arc_evict(dev, size, B_FALSE); |
34dc7c2f BB |
9404 | |
9405 | /* | |
9406 | * Write ARC buffers. | |
9407 | */ | |
d3c2ae1c | 9408 | wrote = l2arc_write_buffers(spa, dev, size); |
d164b209 BB |
9409 | |
9410 | /* | |
9411 | * Calculate interval between writes. | |
9412 | */ | |
9413 | next = l2arc_write_interval(begin, size, wrote); | |
b128c09f | 9414 | spa_config_exit(spa, SCL_L2ARC, dev); |
34dc7c2f | 9415 | } |
40d06e3c | 9416 | spl_fstrans_unmark(cookie); |
34dc7c2f BB |
9417 | |
9418 | l2arc_thread_exit = 0; | |
9419 | cv_broadcast(&l2arc_feed_thr_cv); | |
9420 | CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ | |
9421 | thread_exit(); | |
9422 | } | |
9423 | ||
b128c09f BB |
9424 | boolean_t |
9425 | l2arc_vdev_present(vdev_t *vd) | |
9426 | { | |
77f6826b GA |
9427 | return (l2arc_vdev_get(vd) != NULL); |
9428 | } | |
9429 | ||
9430 | /* | |
9431 | * Returns the l2arc_dev_t associated with a particular vdev_t or NULL if | |
9432 | * the vdev_t isn't an L2ARC device. | |
9433 | */ | |
b7654bd7 | 9434 | l2arc_dev_t * |
77f6826b GA |
9435 | l2arc_vdev_get(vdev_t *vd) |
9436 | { | |
9437 | l2arc_dev_t *dev; | |
b128c09f BB |
9438 | |
9439 | mutex_enter(&l2arc_dev_mtx); | |
9440 | for (dev = list_head(l2arc_dev_list); dev != NULL; | |
9441 | dev = list_next(l2arc_dev_list, dev)) { | |
9442 | if (dev->l2ad_vdev == vd) | |
9443 | break; | |
9444 | } | |
9445 | mutex_exit(&l2arc_dev_mtx); | |
9446 | ||
77f6826b | 9447 | return (dev); |
b128c09f BB |
9448 | } |
9449 | ||
ab8a8f07 GA |
9450 | static void |
9451 | l2arc_rebuild_dev(l2arc_dev_t *dev, boolean_t reopen) | |
9452 | { | |
9453 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; | |
9454 | uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize; | |
9455 | spa_t *spa = dev->l2ad_spa; | |
9456 | ||
9457 | /* | |
9458 | * The L2ARC has to hold at least the payload of one log block for | |
9459 | * them to be restored (persistent L2ARC). The payload of a log block | |
9460 | * depends on the amount of its log entries. We always write log blocks | |
9461 | * with 1022 entries. How many of them are committed or restored depends | |
9462 | * on the size of the L2ARC device. Thus the maximum payload of | |
9463 | * one log block is 1022 * SPA_MAXBLOCKSIZE = 16GB. If the L2ARC device | |
9464 | * is less than that, we reduce the amount of committed and restored | |
9465 | * log entries per block so as to enable persistence. | |
9466 | */ | |
9467 | if (dev->l2ad_end < l2arc_rebuild_blocks_min_l2size) { | |
9468 | dev->l2ad_log_entries = 0; | |
9469 | } else { | |
9470 | dev->l2ad_log_entries = MIN((dev->l2ad_end - | |
9471 | dev->l2ad_start) >> SPA_MAXBLOCKSHIFT, | |
9472 | L2ARC_LOG_BLK_MAX_ENTRIES); | |
9473 | } | |
9474 | ||
9475 | /* | |
9476 | * Read the device header, if an error is returned do not rebuild L2ARC. | |
9477 | */ | |
9478 | if (l2arc_dev_hdr_read(dev) == 0 && dev->l2ad_log_entries > 0) { | |
9479 | /* | |
9480 | * If we are onlining a cache device (vdev_reopen) that was | |
9481 | * still present (l2arc_vdev_present()) and rebuild is enabled, | |
9482 | * we should evict all ARC buffers and pointers to log blocks | |
9483 | * and reclaim their space before restoring its contents to | |
9484 | * L2ARC. | |
9485 | */ | |
9486 | if (reopen) { | |
9487 | if (!l2arc_rebuild_enabled) { | |
9488 | return; | |
9489 | } else { | |
9490 | l2arc_evict(dev, 0, B_TRUE); | |
9491 | /* start a new log block */ | |
9492 | dev->l2ad_log_ent_idx = 0; | |
9493 | dev->l2ad_log_blk_payload_asize = 0; | |
9494 | dev->l2ad_log_blk_payload_start = 0; | |
9495 | } | |
9496 | } | |
9497 | /* | |
9498 | * Just mark the device as pending for a rebuild. We won't | |
9499 | * be starting a rebuild in line here as it would block pool | |
9500 | * import. Instead spa_load_impl will hand that off to an | |
9501 | * async task which will call l2arc_spa_rebuild_start. | |
9502 | */ | |
9503 | dev->l2ad_rebuild = B_TRUE; | |
9504 | } else if (spa_writeable(spa)) { | |
9505 | /* | |
9506 | * In this case TRIM the whole device if l2arc_trim_ahead > 0, | |
9507 | * otherwise create a new header. We zero out the memory holding | |
9508 | * the header to reset dh_start_lbps. If we TRIM the whole | |
9509 | * device the new header will be written by | |
9510 | * vdev_trim_l2arc_thread() at the end of the TRIM to update the | |
9511 | * trim_state in the header too. When reading the header, if | |
9512 | * trim_state is not VDEV_TRIM_COMPLETE and l2arc_trim_ahead > 0 | |
9513 | * we opt to TRIM the whole device again. | |
9514 | */ | |
9515 | if (l2arc_trim_ahead > 0) { | |
9516 | dev->l2ad_trim_all = B_TRUE; | |
9517 | } else { | |
861166b0 | 9518 | memset(l2dhdr, 0, l2dhdr_asize); |
ab8a8f07 GA |
9519 | l2arc_dev_hdr_update(dev); |
9520 | } | |
9521 | } | |
9522 | } | |
9523 | ||
34dc7c2f BB |
9524 | /* |
9525 | * Add a vdev for use by the L2ARC. By this point the spa has already | |
9526 | * validated the vdev and opened it. | |
9527 | */ | |
9528 | void | |
9babb374 | 9529 | l2arc_add_vdev(spa_t *spa, vdev_t *vd) |
34dc7c2f | 9530 | { |
77f6826b GA |
9531 | l2arc_dev_t *adddev; |
9532 | uint64_t l2dhdr_asize; | |
34dc7c2f | 9533 | |
b128c09f BB |
9534 | ASSERT(!l2arc_vdev_present(vd)); |
9535 | ||
34dc7c2f BB |
9536 | /* |
9537 | * Create a new l2arc device entry. | |
9538 | */ | |
77f6826b | 9539 | adddev = vmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); |
34dc7c2f BB |
9540 | adddev->l2ad_spa = spa; |
9541 | adddev->l2ad_vdev = vd; | |
77f6826b GA |
9542 | /* leave extra size for an l2arc device header */ |
9543 | l2dhdr_asize = adddev->l2ad_dev_hdr_asize = | |
9544 | MAX(sizeof (*adddev->l2ad_dev_hdr), 1 << vd->vdev_ashift); | |
9545 | adddev->l2ad_start = VDEV_LABEL_START_SIZE + l2dhdr_asize; | |
9babb374 | 9546 | adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); |
77f6826b | 9547 | ASSERT3U(adddev->l2ad_start, <, adddev->l2ad_end); |
34dc7c2f | 9548 | adddev->l2ad_hand = adddev->l2ad_start; |
77f6826b | 9549 | adddev->l2ad_evict = adddev->l2ad_start; |
34dc7c2f | 9550 | adddev->l2ad_first = B_TRUE; |
d164b209 | 9551 | adddev->l2ad_writing = B_FALSE; |
b7654bd7 | 9552 | adddev->l2ad_trim_all = B_FALSE; |
98f72a53 | 9553 | list_link_init(&adddev->l2ad_node); |
77f6826b | 9554 | adddev->l2ad_dev_hdr = kmem_zalloc(l2dhdr_asize, KM_SLEEP); |
34dc7c2f | 9555 | |
b9541d6b | 9556 | mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
9557 | /* |
9558 | * This is a list of all ARC buffers that are still valid on the | |
9559 | * device. | |
9560 | */ | |
b9541d6b CW |
9561 | list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), |
9562 | offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); | |
34dc7c2f | 9563 | |
77f6826b GA |
9564 | /* |
9565 | * This is a list of pointers to log blocks that are still present | |
9566 | * on the device. | |
9567 | */ | |
9568 | list_create(&adddev->l2ad_lbptr_list, sizeof (l2arc_lb_ptr_buf_t), | |
9569 | offsetof(l2arc_lb_ptr_buf_t, node)); | |
9570 | ||
428870ff | 9571 | vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); |
424fd7c3 | 9572 | zfs_refcount_create(&adddev->l2ad_alloc); |
657fd33b GA |
9573 | zfs_refcount_create(&adddev->l2ad_lb_asize); |
9574 | zfs_refcount_create(&adddev->l2ad_lb_count); | |
34dc7c2f | 9575 | |
ab8a8f07 GA |
9576 | /* |
9577 | * Decide if dev is eligible for L2ARC rebuild or whole device | |
9578 | * trimming. This has to happen before the device is added in the | |
9579 | * cache device list and l2arc_dev_mtx is released. Otherwise | |
9580 | * l2arc_feed_thread() might already start writing on the | |
9581 | * device. | |
9582 | */ | |
9583 | l2arc_rebuild_dev(adddev, B_FALSE); | |
9584 | ||
34dc7c2f BB |
9585 | /* |
9586 | * Add device to global list | |
9587 | */ | |
9588 | mutex_enter(&l2arc_dev_mtx); | |
9589 | list_insert_head(l2arc_dev_list, adddev); | |
9590 | atomic_inc_64(&l2arc_ndev); | |
9591 | mutex_exit(&l2arc_dev_mtx); | |
77f6826b GA |
9592 | } |
9593 | ||
ab8a8f07 GA |
9594 | /* |
9595 | * Decide if a vdev is eligible for L2ARC rebuild, called from vdev_reopen() | |
9596 | * in case of onlining a cache device. | |
9597 | */ | |
77f6826b GA |
9598 | void |
9599 | l2arc_rebuild_vdev(vdev_t *vd, boolean_t reopen) | |
9600 | { | |
9601 | l2arc_dev_t *dev = NULL; | |
77f6826b GA |
9602 | |
9603 | dev = l2arc_vdev_get(vd); | |
9604 | ASSERT3P(dev, !=, NULL); | |
77f6826b GA |
9605 | |
9606 | /* | |
ab8a8f07 GA |
9607 | * In contrast to l2arc_add_vdev() we do not have to worry about |
9608 | * l2arc_feed_thread() invalidating previous content when onlining a | |
9609 | * cache device. The device parameters (l2ad*) are not cleared when | |
9610 | * offlining the device and writing new buffers will not invalidate | |
9611 | * all previous content. In worst case only buffers that have not had | |
9612 | * their log block written to the device will be lost. | |
9613 | * When onlining the cache device (ie offline->online without exporting | |
9614 | * the pool in between) this happens: | |
9615 | * vdev_reopen() -> vdev_open() -> l2arc_rebuild_vdev() | |
9616 | * | | | |
9617 | * vdev_is_dead() = B_FALSE l2ad_rebuild = B_TRUE | |
9618 | * During the time where vdev_is_dead = B_FALSE and until l2ad_rebuild | |
9619 | * is set to B_TRUE we might write additional buffers to the device. | |
9620 | */ | |
9621 | l2arc_rebuild_dev(dev, reopen); | |
34dc7c2f BB |
9622 | } |
9623 | ||
9624 | /* | |
9625 | * Remove a vdev from the L2ARC. | |
9626 | */ | |
9627 | void | |
9628 | l2arc_remove_vdev(vdev_t *vd) | |
9629 | { | |
77f6826b | 9630 | l2arc_dev_t *remdev = NULL; |
34dc7c2f | 9631 | |
34dc7c2f BB |
9632 | /* |
9633 | * Find the device by vdev | |
9634 | */ | |
77f6826b | 9635 | remdev = l2arc_vdev_get(vd); |
d3c2ae1c | 9636 | ASSERT3P(remdev, !=, NULL); |
34dc7c2f | 9637 | |
77f6826b GA |
9638 | /* |
9639 | * Cancel any ongoing or scheduled rebuild. | |
9640 | */ | |
9641 | mutex_enter(&l2arc_rebuild_thr_lock); | |
9642 | if (remdev->l2ad_rebuild_began == B_TRUE) { | |
9643 | remdev->l2ad_rebuild_cancel = B_TRUE; | |
9644 | while (remdev->l2ad_rebuild == B_TRUE) | |
9645 | cv_wait(&l2arc_rebuild_thr_cv, &l2arc_rebuild_thr_lock); | |
9646 | } | |
9647 | mutex_exit(&l2arc_rebuild_thr_lock); | |
9648 | ||
34dc7c2f BB |
9649 | /* |
9650 | * Remove device from global list | |
9651 | */ | |
77f6826b | 9652 | mutex_enter(&l2arc_dev_mtx); |
34dc7c2f BB |
9653 | list_remove(l2arc_dev_list, remdev); |
9654 | l2arc_dev_last = NULL; /* may have been invalidated */ | |
b128c09f BB |
9655 | atomic_dec_64(&l2arc_ndev); |
9656 | mutex_exit(&l2arc_dev_mtx); | |
34dc7c2f BB |
9657 | |
9658 | /* | |
9659 | * Clear all buflists and ARC references. L2ARC device flush. | |
9660 | */ | |
9661 | l2arc_evict(remdev, 0, B_TRUE); | |
b9541d6b | 9662 | list_destroy(&remdev->l2ad_buflist); |
77f6826b GA |
9663 | ASSERT(list_is_empty(&remdev->l2ad_lbptr_list)); |
9664 | list_destroy(&remdev->l2ad_lbptr_list); | |
b9541d6b | 9665 | mutex_destroy(&remdev->l2ad_mtx); |
424fd7c3 | 9666 | zfs_refcount_destroy(&remdev->l2ad_alloc); |
657fd33b GA |
9667 | zfs_refcount_destroy(&remdev->l2ad_lb_asize); |
9668 | zfs_refcount_destroy(&remdev->l2ad_lb_count); | |
77f6826b GA |
9669 | kmem_free(remdev->l2ad_dev_hdr, remdev->l2ad_dev_hdr_asize); |
9670 | vmem_free(remdev, sizeof (l2arc_dev_t)); | |
34dc7c2f BB |
9671 | } |
9672 | ||
9673 | void | |
b128c09f | 9674 | l2arc_init(void) |
34dc7c2f BB |
9675 | { |
9676 | l2arc_thread_exit = 0; | |
9677 | l2arc_ndev = 0; | |
34dc7c2f BB |
9678 | |
9679 | mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); | |
9680 | cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); | |
77f6826b GA |
9681 | mutex_init(&l2arc_rebuild_thr_lock, NULL, MUTEX_DEFAULT, NULL); |
9682 | cv_init(&l2arc_rebuild_thr_cv, NULL, CV_DEFAULT, NULL); | |
34dc7c2f | 9683 | mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
9684 | mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); |
9685 | ||
9686 | l2arc_dev_list = &L2ARC_dev_list; | |
9687 | l2arc_free_on_write = &L2ARC_free_on_write; | |
9688 | list_create(l2arc_dev_list, sizeof (l2arc_dev_t), | |
9689 | offsetof(l2arc_dev_t, l2ad_node)); | |
9690 | list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), | |
9691 | offsetof(l2arc_data_free_t, l2df_list_node)); | |
34dc7c2f BB |
9692 | } |
9693 | ||
9694 | void | |
b128c09f | 9695 | l2arc_fini(void) |
34dc7c2f | 9696 | { |
34dc7c2f BB |
9697 | mutex_destroy(&l2arc_feed_thr_lock); |
9698 | cv_destroy(&l2arc_feed_thr_cv); | |
77f6826b GA |
9699 | mutex_destroy(&l2arc_rebuild_thr_lock); |
9700 | cv_destroy(&l2arc_rebuild_thr_cv); | |
34dc7c2f | 9701 | mutex_destroy(&l2arc_dev_mtx); |
34dc7c2f BB |
9702 | mutex_destroy(&l2arc_free_on_write_mtx); |
9703 | ||
9704 | list_destroy(l2arc_dev_list); | |
9705 | list_destroy(l2arc_free_on_write); | |
9706 | } | |
b128c09f BB |
9707 | |
9708 | void | |
9709 | l2arc_start(void) | |
9710 | { | |
da92d5cb | 9711 | if (!(spa_mode_global & SPA_MODE_WRITE)) |
b128c09f BB |
9712 | return; |
9713 | ||
9714 | (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, | |
1229323d | 9715 | TS_RUN, defclsyspri); |
b128c09f BB |
9716 | } |
9717 | ||
9718 | void | |
9719 | l2arc_stop(void) | |
9720 | { | |
da92d5cb | 9721 | if (!(spa_mode_global & SPA_MODE_WRITE)) |
b128c09f BB |
9722 | return; |
9723 | ||
9724 | mutex_enter(&l2arc_feed_thr_lock); | |
9725 | cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ | |
9726 | l2arc_thread_exit = 1; | |
9727 | while (l2arc_thread_exit != 0) | |
9728 | cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); | |
9729 | mutex_exit(&l2arc_feed_thr_lock); | |
9730 | } | |
c28b2279 | 9731 | |
77f6826b GA |
9732 | /* |
9733 | * Punches out rebuild threads for the L2ARC devices in a spa. This should | |
9734 | * be called after pool import from the spa async thread, since starting | |
9735 | * these threads directly from spa_import() will make them part of the | |
9736 | * "zpool import" context and delay process exit (and thus pool import). | |
9737 | */ | |
9738 | void | |
9739 | l2arc_spa_rebuild_start(spa_t *spa) | |
9740 | { | |
9741 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
9742 | ||
9743 | /* | |
9744 | * Locate the spa's l2arc devices and kick off rebuild threads. | |
9745 | */ | |
9746 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) { | |
9747 | l2arc_dev_t *dev = | |
9748 | l2arc_vdev_get(spa->spa_l2cache.sav_vdevs[i]); | |
9749 | if (dev == NULL) { | |
9750 | /* Don't attempt a rebuild if the vdev is UNAVAIL */ | |
9751 | continue; | |
9752 | } | |
9753 | mutex_enter(&l2arc_rebuild_thr_lock); | |
9754 | if (dev->l2ad_rebuild && !dev->l2ad_rebuild_cancel) { | |
9755 | dev->l2ad_rebuild_began = B_TRUE; | |
3eaf76a8 | 9756 | (void) thread_create(NULL, 0, l2arc_dev_rebuild_thread, |
77f6826b GA |
9757 | dev, 0, &p0, TS_RUN, minclsyspri); |
9758 | } | |
9759 | mutex_exit(&l2arc_rebuild_thr_lock); | |
9760 | } | |
9761 | } | |
9762 | ||
9763 | /* | |
9764 | * Main entry point for L2ARC rebuilding. | |
9765 | */ | |
460748d4 | 9766 | static __attribute__((noreturn)) void |
3eaf76a8 | 9767 | l2arc_dev_rebuild_thread(void *arg) |
77f6826b | 9768 | { |
3eaf76a8 RM |
9769 | l2arc_dev_t *dev = arg; |
9770 | ||
77f6826b GA |
9771 | VERIFY(!dev->l2ad_rebuild_cancel); |
9772 | VERIFY(dev->l2ad_rebuild); | |
9773 | (void) l2arc_rebuild(dev); | |
9774 | mutex_enter(&l2arc_rebuild_thr_lock); | |
9775 | dev->l2ad_rebuild_began = B_FALSE; | |
9776 | dev->l2ad_rebuild = B_FALSE; | |
9777 | mutex_exit(&l2arc_rebuild_thr_lock); | |
9778 | ||
9779 | thread_exit(); | |
9780 | } | |
9781 | ||
9782 | /* | |
9783 | * This function implements the actual L2ARC metadata rebuild. It: | |
9784 | * starts reading the log block chain and restores each block's contents | |
9785 | * to memory (reconstructing arc_buf_hdr_t's). | |
9786 | * | |
9787 | * Operation stops under any of the following conditions: | |
9788 | * | |
9789 | * 1) We reach the end of the log block chain. | |
9790 | * 2) We encounter *any* error condition (cksum errors, io errors) | |
9791 | */ | |
9792 | static int | |
9793 | l2arc_rebuild(l2arc_dev_t *dev) | |
9794 | { | |
9795 | vdev_t *vd = dev->l2ad_vdev; | |
9796 | spa_t *spa = vd->vdev_spa; | |
657fd33b | 9797 | int err = 0; |
77f6826b GA |
9798 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; |
9799 | l2arc_log_blk_phys_t *this_lb, *next_lb; | |
9800 | zio_t *this_io = NULL, *next_io = NULL; | |
9801 | l2arc_log_blkptr_t lbps[2]; | |
9802 | l2arc_lb_ptr_buf_t *lb_ptr_buf; | |
9803 | boolean_t lock_held; | |
9804 | ||
9805 | this_lb = vmem_zalloc(sizeof (*this_lb), KM_SLEEP); | |
9806 | next_lb = vmem_zalloc(sizeof (*next_lb), KM_SLEEP); | |
9807 | ||
9808 | /* | |
9809 | * We prevent device removal while issuing reads to the device, | |
9810 | * then during the rebuilding phases we drop this lock again so | |
9811 | * that a spa_unload or device remove can be initiated - this is | |
9812 | * safe, because the spa will signal us to stop before removing | |
9813 | * our device and wait for us to stop. | |
9814 | */ | |
9815 | spa_config_enter(spa, SCL_L2ARC, vd, RW_READER); | |
9816 | lock_held = B_TRUE; | |
9817 | ||
9818 | /* | |
9819 | * Retrieve the persistent L2ARC device state. | |
657fd33b | 9820 | * L2BLK_GET_PSIZE returns aligned size for log blocks. |
77f6826b GA |
9821 | */ |
9822 | dev->l2ad_evict = MAX(l2dhdr->dh_evict, dev->l2ad_start); | |
9823 | dev->l2ad_hand = MAX(l2dhdr->dh_start_lbps[0].lbp_daddr + | |
9824 | L2BLK_GET_PSIZE((&l2dhdr->dh_start_lbps[0])->lbp_prop), | |
9825 | dev->l2ad_start); | |
9826 | dev->l2ad_first = !!(l2dhdr->dh_flags & L2ARC_DEV_HDR_EVICT_FIRST); | |
9827 | ||
b7654bd7 GA |
9828 | vd->vdev_trim_action_time = l2dhdr->dh_trim_action_time; |
9829 | vd->vdev_trim_state = l2dhdr->dh_trim_state; | |
9830 | ||
77f6826b GA |
9831 | /* |
9832 | * In case the zfs module parameter l2arc_rebuild_enabled is false | |
9833 | * we do not start the rebuild process. | |
9834 | */ | |
9835 | if (!l2arc_rebuild_enabled) | |
9836 | goto out; | |
9837 | ||
9838 | /* Prepare the rebuild process */ | |
861166b0 | 9839 | memcpy(lbps, l2dhdr->dh_start_lbps, sizeof (lbps)); |
77f6826b GA |
9840 | |
9841 | /* Start the rebuild process */ | |
9842 | for (;;) { | |
9843 | if (!l2arc_log_blkptr_valid(dev, &lbps[0])) | |
9844 | break; | |
9845 | ||
9846 | if ((err = l2arc_log_blk_read(dev, &lbps[0], &lbps[1], | |
9847 | this_lb, next_lb, this_io, &next_io)) != 0) | |
9848 | goto out; | |
9849 | ||
9850 | /* | |
9851 | * Our memory pressure valve. If the system is running low | |
9852 | * on memory, rather than swamping memory with new ARC buf | |
9853 | * hdrs, we opt not to rebuild the L2ARC. At this point, | |
9854 | * however, we have already set up our L2ARC dev to chain in | |
9855 | * new metadata log blocks, so the user may choose to offline/ | |
9856 | * online the L2ARC dev at a later time (or re-import the pool) | |
9857 | * to reconstruct it (when there's less memory pressure). | |
9858 | */ | |
523e1295 | 9859 | if (l2arc_hdr_limit_reached()) { |
77f6826b GA |
9860 | ARCSTAT_BUMP(arcstat_l2_rebuild_abort_lowmem); |
9861 | cmn_err(CE_NOTE, "System running low on memory, " | |
9862 | "aborting L2ARC rebuild."); | |
9863 | err = SET_ERROR(ENOMEM); | |
9864 | goto out; | |
9865 | } | |
9866 | ||
9867 | spa_config_exit(spa, SCL_L2ARC, vd); | |
9868 | lock_held = B_FALSE; | |
9869 | ||
9870 | /* | |
9871 | * Now that we know that the next_lb checks out alright, we | |
9872 | * can start reconstruction from this log block. | |
657fd33b | 9873 | * L2BLK_GET_PSIZE returns aligned size for log blocks. |
77f6826b | 9874 | */ |
657fd33b | 9875 | uint64_t asize = L2BLK_GET_PSIZE((&lbps[0])->lbp_prop); |
a76e4e67 | 9876 | l2arc_log_blk_restore(dev, this_lb, asize); |
77f6826b GA |
9877 | |
9878 | /* | |
9879 | * log block restored, include its pointer in the list of | |
9880 | * pointers to log blocks present in the L2ARC device. | |
9881 | */ | |
9882 | lb_ptr_buf = kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t), KM_SLEEP); | |
9883 | lb_ptr_buf->lb_ptr = kmem_zalloc(sizeof (l2arc_log_blkptr_t), | |
9884 | KM_SLEEP); | |
861166b0 | 9885 | memcpy(lb_ptr_buf->lb_ptr, &lbps[0], |
77f6826b GA |
9886 | sizeof (l2arc_log_blkptr_t)); |
9887 | mutex_enter(&dev->l2ad_mtx); | |
9888 | list_insert_tail(&dev->l2ad_lbptr_list, lb_ptr_buf); | |
657fd33b GA |
9889 | ARCSTAT_INCR(arcstat_l2_log_blk_asize, asize); |
9890 | ARCSTAT_BUMP(arcstat_l2_log_blk_count); | |
9891 | zfs_refcount_add_many(&dev->l2ad_lb_asize, asize, lb_ptr_buf); | |
9892 | zfs_refcount_add(&dev->l2ad_lb_count, lb_ptr_buf); | |
77f6826b | 9893 | mutex_exit(&dev->l2ad_mtx); |
657fd33b | 9894 | vdev_space_update(vd, asize, 0, 0); |
77f6826b GA |
9895 | |
9896 | /* | |
9897 | * Protection against loops of log blocks: | |
9898 | * | |
9899 | * l2ad_hand l2ad_evict | |
9900 | * V V | |
9901 | * l2ad_start |=======================================| l2ad_end | |
9902 | * -----|||----|||---|||----||| | |
9903 | * (3) (2) (1) (0) | |
9904 | * ---|||---|||----|||---||| | |
9905 | * (7) (6) (5) (4) | |
9906 | * | |
9907 | * In this situation the pointer of log block (4) passes | |
9908 | * l2arc_log_blkptr_valid() but the log block should not be | |
9909 | * restored as it is overwritten by the payload of log block | |
9910 | * (0). Only log blocks (0)-(3) should be restored. We check | |
657fd33b GA |
9911 | * whether l2ad_evict lies in between the payload starting |
9912 | * offset of the next log block (lbps[1].lbp_payload_start) | |
9913 | * and the payload starting offset of the present log block | |
9914 | * (lbps[0].lbp_payload_start). If true and this isn't the | |
9915 | * first pass, we are looping from the beginning and we should | |
9916 | * stop. | |
77f6826b | 9917 | */ |
657fd33b GA |
9918 | if (l2arc_range_check_overlap(lbps[1].lbp_payload_start, |
9919 | lbps[0].lbp_payload_start, dev->l2ad_evict) && | |
9920 | !dev->l2ad_first) | |
77f6826b GA |
9921 | goto out; |
9922 | ||
0e4c830b | 9923 | kpreempt(KPREEMPT_SYNC); |
77f6826b GA |
9924 | for (;;) { |
9925 | mutex_enter(&l2arc_rebuild_thr_lock); | |
9926 | if (dev->l2ad_rebuild_cancel) { | |
9927 | dev->l2ad_rebuild = B_FALSE; | |
9928 | cv_signal(&l2arc_rebuild_thr_cv); | |
9929 | mutex_exit(&l2arc_rebuild_thr_lock); | |
9930 | err = SET_ERROR(ECANCELED); | |
9931 | goto out; | |
9932 | } | |
9933 | mutex_exit(&l2arc_rebuild_thr_lock); | |
9934 | if (spa_config_tryenter(spa, SCL_L2ARC, vd, | |
9935 | RW_READER)) { | |
9936 | lock_held = B_TRUE; | |
9937 | break; | |
9938 | } | |
9939 | /* | |
9940 | * L2ARC config lock held by somebody in writer, | |
9941 | * possibly due to them trying to remove us. They'll | |
9942 | * likely to want us to shut down, so after a little | |
9943 | * delay, we check l2ad_rebuild_cancel and retry | |
9944 | * the lock again. | |
9945 | */ | |
9946 | delay(1); | |
9947 | } | |
9948 | ||
9949 | /* | |
9950 | * Continue with the next log block. | |
9951 | */ | |
9952 | lbps[0] = lbps[1]; | |
9953 | lbps[1] = this_lb->lb_prev_lbp; | |
9954 | PTR_SWAP(this_lb, next_lb); | |
9955 | this_io = next_io; | |
9956 | next_io = NULL; | |
a76e4e67 | 9957 | } |
77f6826b GA |
9958 | |
9959 | if (this_io != NULL) | |
9960 | l2arc_log_blk_fetch_abort(this_io); | |
9961 | out: | |
9962 | if (next_io != NULL) | |
9963 | l2arc_log_blk_fetch_abort(next_io); | |
9964 | vmem_free(this_lb, sizeof (*this_lb)); | |
9965 | vmem_free(next_lb, sizeof (*next_lb)); | |
9966 | ||
9967 | if (!l2arc_rebuild_enabled) { | |
657fd33b GA |
9968 | spa_history_log_internal(spa, "L2ARC rebuild", NULL, |
9969 | "disabled"); | |
9970 | } else if (err == 0 && zfs_refcount_count(&dev->l2ad_lb_count) > 0) { | |
77f6826b | 9971 | ARCSTAT_BUMP(arcstat_l2_rebuild_success); |
657fd33b GA |
9972 | spa_history_log_internal(spa, "L2ARC rebuild", NULL, |
9973 | "successful, restored %llu blocks", | |
9974 | (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count)); | |
9975 | } else if (err == 0 && zfs_refcount_count(&dev->l2ad_lb_count) == 0) { | |
9976 | /* | |
9977 | * No error but also nothing restored, meaning the lbps array | |
9978 | * in the device header points to invalid/non-present log | |
9979 | * blocks. Reset the header. | |
9980 | */ | |
9981 | spa_history_log_internal(spa, "L2ARC rebuild", NULL, | |
9982 | "no valid log blocks"); | |
861166b0 | 9983 | memset(l2dhdr, 0, dev->l2ad_dev_hdr_asize); |
657fd33b | 9984 | l2arc_dev_hdr_update(dev); |
da60484d GA |
9985 | } else if (err == ECANCELED) { |
9986 | /* | |
9987 | * In case the rebuild was canceled do not log to spa history | |
9988 | * log as the pool may be in the process of being removed. | |
9989 | */ | |
9990 | zfs_dbgmsg("L2ARC rebuild aborted, restored %llu blocks", | |
8e739b2c | 9991 | (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count)); |
77f6826b | 9992 | } else if (err != 0) { |
657fd33b GA |
9993 | spa_history_log_internal(spa, "L2ARC rebuild", NULL, |
9994 | "aborted, restored %llu blocks", | |
9995 | (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count)); | |
77f6826b GA |
9996 | } |
9997 | ||
9998 | if (lock_held) | |
9999 | spa_config_exit(spa, SCL_L2ARC, vd); | |
10000 | ||
10001 | return (err); | |
10002 | } | |
10003 | ||
10004 | /* | |
10005 | * Attempts to read the device header on the provided L2ARC device and writes | |
10006 | * it to `hdr'. On success, this function returns 0, otherwise the appropriate | |
10007 | * error code is returned. | |
10008 | */ | |
10009 | static int | |
10010 | l2arc_dev_hdr_read(l2arc_dev_t *dev) | |
10011 | { | |
10012 | int err; | |
10013 | uint64_t guid; | |
10014 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; | |
10015 | const uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize; | |
10016 | abd_t *abd; | |
10017 | ||
10018 | guid = spa_guid(dev->l2ad_vdev->vdev_spa); | |
10019 | ||
10020 | abd = abd_get_from_buf(l2dhdr, l2dhdr_asize); | |
10021 | ||
10022 | err = zio_wait(zio_read_phys(NULL, dev->l2ad_vdev, | |
10023 | VDEV_LABEL_START_SIZE, l2dhdr_asize, abd, | |
a76e4e67 | 10024 | ZIO_CHECKSUM_LABEL, NULL, NULL, ZIO_PRIORITY_SYNC_READ, |
70ea484e | 10025 | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY | |
77f6826b GA |
10026 | ZIO_FLAG_SPECULATIVE, B_FALSE)); |
10027 | ||
e2af2acc | 10028 | abd_free(abd); |
77f6826b GA |
10029 | |
10030 | if (err != 0) { | |
10031 | ARCSTAT_BUMP(arcstat_l2_rebuild_abort_dh_errors); | |
10032 | zfs_dbgmsg("L2ARC IO error (%d) while reading device header, " | |
8e739b2c RE |
10033 | "vdev guid: %llu", err, |
10034 | (u_longlong_t)dev->l2ad_vdev->vdev_guid); | |
77f6826b GA |
10035 | return (err); |
10036 | } | |
10037 | ||
10038 | if (l2dhdr->dh_magic == BSWAP_64(L2ARC_DEV_HDR_MAGIC)) | |
10039 | byteswap_uint64_array(l2dhdr, sizeof (*l2dhdr)); | |
10040 | ||
10041 | if (l2dhdr->dh_magic != L2ARC_DEV_HDR_MAGIC || | |
10042 | l2dhdr->dh_spa_guid != guid || | |
10043 | l2dhdr->dh_vdev_guid != dev->l2ad_vdev->vdev_guid || | |
10044 | l2dhdr->dh_version != L2ARC_PERSISTENT_VERSION || | |
657fd33b | 10045 | l2dhdr->dh_log_entries != dev->l2ad_log_entries || |
77f6826b GA |
10046 | l2dhdr->dh_end != dev->l2ad_end || |
10047 | !l2arc_range_check_overlap(dev->l2ad_start, dev->l2ad_end, | |
b7654bd7 GA |
10048 | l2dhdr->dh_evict) || |
10049 | (l2dhdr->dh_trim_state != VDEV_TRIM_COMPLETE && | |
10050 | l2arc_trim_ahead > 0)) { | |
77f6826b GA |
10051 | /* |
10052 | * Attempt to rebuild a device containing no actual dev hdr | |
10053 | * or containing a header from some other pool or from another | |
10054 | * version of persistent L2ARC. | |
10055 | */ | |
10056 | ARCSTAT_BUMP(arcstat_l2_rebuild_abort_unsupported); | |
10057 | return (SET_ERROR(ENOTSUP)); | |
10058 | } | |
10059 | ||
10060 | return (0); | |
10061 | } | |
10062 | ||
10063 | /* | |
10064 | * Reads L2ARC log blocks from storage and validates their contents. | |
10065 | * | |
10066 | * This function implements a simple fetcher to make sure that while | |
10067 | * we're processing one buffer the L2ARC is already fetching the next | |
10068 | * one in the chain. | |
10069 | * | |
10070 | * The arguments this_lp and next_lp point to the current and next log block | |
10071 | * address in the block chain. Similarly, this_lb and next_lb hold the | |
10072 | * l2arc_log_blk_phys_t's of the current and next L2ARC blk. | |
10073 | * | |
10074 | * The `this_io' and `next_io' arguments are used for block fetching. | |
10075 | * When issuing the first blk IO during rebuild, you should pass NULL for | |
10076 | * `this_io'. This function will then issue a sync IO to read the block and | |
10077 | * also issue an async IO to fetch the next block in the block chain. The | |
10078 | * fetched IO is returned in `next_io'. On subsequent calls to this | |
10079 | * function, pass the value returned in `next_io' from the previous call | |
10080 | * as `this_io' and a fresh `next_io' pointer to hold the next fetch IO. | |
10081 | * Prior to the call, you should initialize your `next_io' pointer to be | |
10082 | * NULL. If no fetch IO was issued, the pointer is left set at NULL. | |
10083 | * | |
10084 | * On success, this function returns 0, otherwise it returns an appropriate | |
10085 | * error code. On error the fetching IO is aborted and cleared before | |
10086 | * returning from this function. Therefore, if we return `success', the | |
10087 | * caller can assume that we have taken care of cleanup of fetch IOs. | |
10088 | */ | |
10089 | static int | |
10090 | l2arc_log_blk_read(l2arc_dev_t *dev, | |
10091 | const l2arc_log_blkptr_t *this_lbp, const l2arc_log_blkptr_t *next_lbp, | |
10092 | l2arc_log_blk_phys_t *this_lb, l2arc_log_blk_phys_t *next_lb, | |
10093 | zio_t *this_io, zio_t **next_io) | |
10094 | { | |
10095 | int err = 0; | |
10096 | zio_cksum_t cksum; | |
10097 | abd_t *abd = NULL; | |
657fd33b | 10098 | uint64_t asize; |
77f6826b GA |
10099 | |
10100 | ASSERT(this_lbp != NULL && next_lbp != NULL); | |
10101 | ASSERT(this_lb != NULL && next_lb != NULL); | |
10102 | ASSERT(next_io != NULL && *next_io == NULL); | |
10103 | ASSERT(l2arc_log_blkptr_valid(dev, this_lbp)); | |
10104 | ||
10105 | /* | |
10106 | * Check to see if we have issued the IO for this log block in a | |
10107 | * previous run. If not, this is the first call, so issue it now. | |
10108 | */ | |
10109 | if (this_io == NULL) { | |
10110 | this_io = l2arc_log_blk_fetch(dev->l2ad_vdev, this_lbp, | |
10111 | this_lb); | |
10112 | } | |
10113 | ||
10114 | /* | |
10115 | * Peek to see if we can start issuing the next IO immediately. | |
10116 | */ | |
10117 | if (l2arc_log_blkptr_valid(dev, next_lbp)) { | |
10118 | /* | |
10119 | * Start issuing IO for the next log block early - this | |
10120 | * should help keep the L2ARC device busy while we | |
10121 | * decompress and restore this log block. | |
10122 | */ | |
10123 | *next_io = l2arc_log_blk_fetch(dev->l2ad_vdev, next_lbp, | |
10124 | next_lb); | |
10125 | } | |
10126 | ||
10127 | /* Wait for the IO to read this log block to complete */ | |
10128 | if ((err = zio_wait(this_io)) != 0) { | |
10129 | ARCSTAT_BUMP(arcstat_l2_rebuild_abort_io_errors); | |
10130 | zfs_dbgmsg("L2ARC IO error (%d) while reading log block, " | |
8e739b2c RE |
10131 | "offset: %llu, vdev guid: %llu", err, |
10132 | (u_longlong_t)this_lbp->lbp_daddr, | |
10133 | (u_longlong_t)dev->l2ad_vdev->vdev_guid); | |
77f6826b GA |
10134 | goto cleanup; |
10135 | } | |
10136 | ||
657fd33b GA |
10137 | /* |
10138 | * Make sure the buffer checks out. | |
10139 | * L2BLK_GET_PSIZE returns aligned size for log blocks. | |
10140 | */ | |
10141 | asize = L2BLK_GET_PSIZE((this_lbp)->lbp_prop); | |
10142 | fletcher_4_native(this_lb, asize, NULL, &cksum); | |
77f6826b GA |
10143 | if (!ZIO_CHECKSUM_EQUAL(cksum, this_lbp->lbp_cksum)) { |
10144 | ARCSTAT_BUMP(arcstat_l2_rebuild_abort_cksum_lb_errors); | |
10145 | zfs_dbgmsg("L2ARC log block cksum failed, offset: %llu, " | |
10146 | "vdev guid: %llu, l2ad_hand: %llu, l2ad_evict: %llu", | |
8e739b2c RE |
10147 | (u_longlong_t)this_lbp->lbp_daddr, |
10148 | (u_longlong_t)dev->l2ad_vdev->vdev_guid, | |
10149 | (u_longlong_t)dev->l2ad_hand, | |
10150 | (u_longlong_t)dev->l2ad_evict); | |
77f6826b GA |
10151 | err = SET_ERROR(ECKSUM); |
10152 | goto cleanup; | |
10153 | } | |
10154 | ||
10155 | /* Now we can take our time decoding this buffer */ | |
10156 | switch (L2BLK_GET_COMPRESS((this_lbp)->lbp_prop)) { | |
10157 | case ZIO_COMPRESS_OFF: | |
10158 | break; | |
10159 | case ZIO_COMPRESS_LZ4: | |
657fd33b GA |
10160 | abd = abd_alloc_for_io(asize, B_TRUE); |
10161 | abd_copy_from_buf_off(abd, this_lb, 0, asize); | |
77f6826b GA |
10162 | if ((err = zio_decompress_data( |
10163 | L2BLK_GET_COMPRESS((this_lbp)->lbp_prop), | |
10b3c7f5 | 10164 | abd, this_lb, asize, sizeof (*this_lb), NULL)) != 0) { |
77f6826b GA |
10165 | err = SET_ERROR(EINVAL); |
10166 | goto cleanup; | |
10167 | } | |
10168 | break; | |
10169 | default: | |
10170 | err = SET_ERROR(EINVAL); | |
10171 | goto cleanup; | |
10172 | } | |
10173 | if (this_lb->lb_magic == BSWAP_64(L2ARC_LOG_BLK_MAGIC)) | |
10174 | byteswap_uint64_array(this_lb, sizeof (*this_lb)); | |
10175 | if (this_lb->lb_magic != L2ARC_LOG_BLK_MAGIC) { | |
10176 | err = SET_ERROR(EINVAL); | |
10177 | goto cleanup; | |
10178 | } | |
10179 | cleanup: | |
10180 | /* Abort an in-flight fetch I/O in case of error */ | |
10181 | if (err != 0 && *next_io != NULL) { | |
10182 | l2arc_log_blk_fetch_abort(*next_io); | |
10183 | *next_io = NULL; | |
10184 | } | |
10185 | if (abd != NULL) | |
10186 | abd_free(abd); | |
10187 | return (err); | |
10188 | } | |
10189 | ||
10190 | /* | |
10191 | * Restores the payload of a log block to ARC. This creates empty ARC hdr | |
10192 | * entries which only contain an l2arc hdr, essentially restoring the | |
10193 | * buffers to their L2ARC evicted state. This function also updates space | |
10194 | * usage on the L2ARC vdev to make sure it tracks restored buffers. | |
10195 | */ | |
10196 | static void | |
10197 | l2arc_log_blk_restore(l2arc_dev_t *dev, const l2arc_log_blk_phys_t *lb, | |
a76e4e67 | 10198 | uint64_t lb_asize) |
77f6826b | 10199 | { |
657fd33b GA |
10200 | uint64_t size = 0, asize = 0; |
10201 | uint64_t log_entries = dev->l2ad_log_entries; | |
77f6826b | 10202 | |
523e1295 AM |
10203 | /* |
10204 | * Usually arc_adapt() is called only for data, not headers, but | |
10205 | * since we may allocate significant amount of memory here, let ARC | |
10206 | * grow its arc_c. | |
10207 | */ | |
a8d83e2a | 10208 | arc_adapt(log_entries * HDR_L2ONLY_SIZE); |
523e1295 | 10209 | |
77f6826b GA |
10210 | for (int i = log_entries - 1; i >= 0; i--) { |
10211 | /* | |
10212 | * Restore goes in the reverse temporal direction to preserve | |
10213 | * correct temporal ordering of buffers in the l2ad_buflist. | |
10214 | * l2arc_hdr_restore also does a list_insert_tail instead of | |
10215 | * list_insert_head on the l2ad_buflist: | |
10216 | * | |
10217 | * LIST l2ad_buflist LIST | |
10218 | * HEAD <------ (time) ------ TAIL | |
10219 | * direction +-----+-----+-----+-----+-----+ direction | |
10220 | * of l2arc <== | buf | buf | buf | buf | buf | ===> of rebuild | |
10221 | * fill +-----+-----+-----+-----+-----+ | |
10222 | * ^ ^ | |
10223 | * | | | |
10224 | * | | | |
657fd33b GA |
10225 | * l2arc_feed_thread l2arc_rebuild |
10226 | * will place new bufs here restores bufs here | |
77f6826b | 10227 | * |
657fd33b GA |
10228 | * During l2arc_rebuild() the device is not used by |
10229 | * l2arc_feed_thread() as dev->l2ad_rebuild is set to true. | |
77f6826b GA |
10230 | */ |
10231 | size += L2BLK_GET_LSIZE((&lb->lb_entries[i])->le_prop); | |
657fd33b GA |
10232 | asize += vdev_psize_to_asize(dev->l2ad_vdev, |
10233 | L2BLK_GET_PSIZE((&lb->lb_entries[i])->le_prop)); | |
77f6826b GA |
10234 | l2arc_hdr_restore(&lb->lb_entries[i], dev); |
10235 | } | |
10236 | ||
10237 | /* | |
10238 | * Record rebuild stats: | |
10239 | * size Logical size of restored buffers in the L2ARC | |
657fd33b | 10240 | * asize Aligned size of restored buffers in the L2ARC |
77f6826b GA |
10241 | */ |
10242 | ARCSTAT_INCR(arcstat_l2_rebuild_size, size); | |
657fd33b | 10243 | ARCSTAT_INCR(arcstat_l2_rebuild_asize, asize); |
77f6826b | 10244 | ARCSTAT_INCR(arcstat_l2_rebuild_bufs, log_entries); |
657fd33b GA |
10245 | ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize, lb_asize); |
10246 | ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio, asize / lb_asize); | |
77f6826b GA |
10247 | ARCSTAT_BUMP(arcstat_l2_rebuild_log_blks); |
10248 | } | |
10249 | ||
10250 | /* | |
10251 | * Restores a single ARC buf hdr from a log entry. The ARC buffer is put | |
10252 | * into a state indicating that it has been evicted to L2ARC. | |
10253 | */ | |
10254 | static void | |
10255 | l2arc_hdr_restore(const l2arc_log_ent_phys_t *le, l2arc_dev_t *dev) | |
10256 | { | |
10257 | arc_buf_hdr_t *hdr, *exists; | |
10258 | kmutex_t *hash_lock; | |
10259 | arc_buf_contents_t type = L2BLK_GET_TYPE((le)->le_prop); | |
10260 | uint64_t asize; | |
10261 | ||
10262 | /* | |
10263 | * Do all the allocation before grabbing any locks, this lets us | |
10264 | * sleep if memory is full and we don't have to deal with failed | |
10265 | * allocations. | |
10266 | */ | |
10267 | hdr = arc_buf_alloc_l2only(L2BLK_GET_LSIZE((le)->le_prop), type, | |
10268 | dev, le->le_dva, le->le_daddr, | |
10269 | L2BLK_GET_PSIZE((le)->le_prop), le->le_birth, | |
10b3c7f5 | 10270 | L2BLK_GET_COMPRESS((le)->le_prop), le->le_complevel, |
77f6826b | 10271 | L2BLK_GET_PROTECTED((le)->le_prop), |
08532162 GA |
10272 | L2BLK_GET_PREFETCH((le)->le_prop), |
10273 | L2BLK_GET_STATE((le)->le_prop)); | |
77f6826b GA |
10274 | asize = vdev_psize_to_asize(dev->l2ad_vdev, |
10275 | L2BLK_GET_PSIZE((le)->le_prop)); | |
10276 | ||
10277 | /* | |
10278 | * vdev_space_update() has to be called before arc_hdr_destroy() to | |
08532162 | 10279 | * avoid underflow since the latter also calls vdev_space_update(). |
77f6826b | 10280 | */ |
08532162 | 10281 | l2arc_hdr_arcstats_increment(hdr); |
77f6826b GA |
10282 | vdev_space_update(dev->l2ad_vdev, asize, 0, 0); |
10283 | ||
77f6826b GA |
10284 | mutex_enter(&dev->l2ad_mtx); |
10285 | list_insert_tail(&dev->l2ad_buflist, hdr); | |
10286 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, arc_hdr_size(hdr), hdr); | |
10287 | mutex_exit(&dev->l2ad_mtx); | |
10288 | ||
10289 | exists = buf_hash_insert(hdr, &hash_lock); | |
10290 | if (exists) { | |
10291 | /* Buffer was already cached, no need to restore it. */ | |
10292 | arc_hdr_destroy(hdr); | |
10293 | /* | |
10294 | * If the buffer is already cached, check whether it has | |
10295 | * L2ARC metadata. If not, enter them and update the flag. | |
10296 | * This is important is case of onlining a cache device, since | |
10297 | * we previously evicted all L2ARC metadata from ARC. | |
10298 | */ | |
10299 | if (!HDR_HAS_L2HDR(exists)) { | |
10300 | arc_hdr_set_flags(exists, ARC_FLAG_HAS_L2HDR); | |
10301 | exists->b_l2hdr.b_dev = dev; | |
10302 | exists->b_l2hdr.b_daddr = le->le_daddr; | |
08532162 GA |
10303 | exists->b_l2hdr.b_arcs_state = |
10304 | L2BLK_GET_STATE((le)->le_prop); | |
77f6826b GA |
10305 | mutex_enter(&dev->l2ad_mtx); |
10306 | list_insert_tail(&dev->l2ad_buflist, exists); | |
10307 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, | |
10308 | arc_hdr_size(exists), exists); | |
10309 | mutex_exit(&dev->l2ad_mtx); | |
08532162 | 10310 | l2arc_hdr_arcstats_increment(exists); |
77f6826b | 10311 | vdev_space_update(dev->l2ad_vdev, asize, 0, 0); |
77f6826b GA |
10312 | } |
10313 | ARCSTAT_BUMP(arcstat_l2_rebuild_bufs_precached); | |
10314 | } | |
10315 | ||
10316 | mutex_exit(hash_lock); | |
10317 | } | |
10318 | ||
10319 | /* | |
10320 | * Starts an asynchronous read IO to read a log block. This is used in log | |
10321 | * block reconstruction to start reading the next block before we are done | |
10322 | * decoding and reconstructing the current block, to keep the l2arc device | |
10323 | * nice and hot with read IO to process. | |
10324 | * The returned zio will contain a newly allocated memory buffers for the IO | |
10325 | * data which should then be freed by the caller once the zio is no longer | |
10326 | * needed (i.e. due to it having completed). If you wish to abort this | |
10327 | * zio, you should do so using l2arc_log_blk_fetch_abort, which takes | |
10328 | * care of disposing of the allocated buffers correctly. | |
10329 | */ | |
10330 | static zio_t * | |
10331 | l2arc_log_blk_fetch(vdev_t *vd, const l2arc_log_blkptr_t *lbp, | |
10332 | l2arc_log_blk_phys_t *lb) | |
10333 | { | |
657fd33b | 10334 | uint32_t asize; |
77f6826b GA |
10335 | zio_t *pio; |
10336 | l2arc_read_callback_t *cb; | |
10337 | ||
657fd33b GA |
10338 | /* L2BLK_GET_PSIZE returns aligned size for log blocks */ |
10339 | asize = L2BLK_GET_PSIZE((lbp)->lbp_prop); | |
10340 | ASSERT(asize <= sizeof (l2arc_log_blk_phys_t)); | |
10341 | ||
77f6826b | 10342 | cb = kmem_zalloc(sizeof (l2arc_read_callback_t), KM_SLEEP); |
657fd33b | 10343 | cb->l2rcb_abd = abd_get_from_buf(lb, asize); |
77f6826b | 10344 | pio = zio_root(vd->vdev_spa, l2arc_blk_fetch_done, cb, |
70ea484e | 10345 | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY); |
657fd33b | 10346 | (void) zio_nowait(zio_read_phys(pio, vd, lbp->lbp_daddr, asize, |
77f6826b | 10347 | cb->l2rcb_abd, ZIO_CHECKSUM_OFF, NULL, NULL, |
70ea484e | 10348 | ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL | |
77f6826b GA |
10349 | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY, B_FALSE)); |
10350 | ||
10351 | return (pio); | |
10352 | } | |
10353 | ||
10354 | /* | |
10355 | * Aborts a zio returned from l2arc_log_blk_fetch and frees the data | |
10356 | * buffers allocated for it. | |
10357 | */ | |
10358 | static void | |
10359 | l2arc_log_blk_fetch_abort(zio_t *zio) | |
10360 | { | |
10361 | (void) zio_wait(zio); | |
10362 | } | |
10363 | ||
10364 | /* | |
2054f35e | 10365 | * Creates a zio to update the device header on an l2arc device. |
77f6826b | 10366 | */ |
b7654bd7 | 10367 | void |
77f6826b GA |
10368 | l2arc_dev_hdr_update(l2arc_dev_t *dev) |
10369 | { | |
10370 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; | |
10371 | const uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize; | |
10372 | abd_t *abd; | |
10373 | int err; | |
10374 | ||
657fd33b GA |
10375 | VERIFY(spa_config_held(dev->l2ad_spa, SCL_STATE_ALL, RW_READER)); |
10376 | ||
77f6826b GA |
10377 | l2dhdr->dh_magic = L2ARC_DEV_HDR_MAGIC; |
10378 | l2dhdr->dh_version = L2ARC_PERSISTENT_VERSION; | |
10379 | l2dhdr->dh_spa_guid = spa_guid(dev->l2ad_vdev->vdev_spa); | |
10380 | l2dhdr->dh_vdev_guid = dev->l2ad_vdev->vdev_guid; | |
657fd33b | 10381 | l2dhdr->dh_log_entries = dev->l2ad_log_entries; |
77f6826b GA |
10382 | l2dhdr->dh_evict = dev->l2ad_evict; |
10383 | l2dhdr->dh_start = dev->l2ad_start; | |
10384 | l2dhdr->dh_end = dev->l2ad_end; | |
657fd33b GA |
10385 | l2dhdr->dh_lb_asize = zfs_refcount_count(&dev->l2ad_lb_asize); |
10386 | l2dhdr->dh_lb_count = zfs_refcount_count(&dev->l2ad_lb_count); | |
77f6826b | 10387 | l2dhdr->dh_flags = 0; |
b7654bd7 GA |
10388 | l2dhdr->dh_trim_action_time = dev->l2ad_vdev->vdev_trim_action_time; |
10389 | l2dhdr->dh_trim_state = dev->l2ad_vdev->vdev_trim_state; | |
77f6826b GA |
10390 | if (dev->l2ad_first) |
10391 | l2dhdr->dh_flags |= L2ARC_DEV_HDR_EVICT_FIRST; | |
10392 | ||
10393 | abd = abd_get_from_buf(l2dhdr, l2dhdr_asize); | |
10394 | ||
10395 | err = zio_wait(zio_write_phys(NULL, dev->l2ad_vdev, | |
10396 | VDEV_LABEL_START_SIZE, l2dhdr_asize, abd, ZIO_CHECKSUM_LABEL, NULL, | |
10397 | NULL, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_CANFAIL, B_FALSE)); | |
10398 | ||
e2af2acc | 10399 | abd_free(abd); |
77f6826b GA |
10400 | |
10401 | if (err != 0) { | |
10402 | zfs_dbgmsg("L2ARC IO error (%d) while writing device header, " | |
8e739b2c RE |
10403 | "vdev guid: %llu", err, |
10404 | (u_longlong_t)dev->l2ad_vdev->vdev_guid); | |
77f6826b GA |
10405 | } |
10406 | } | |
10407 | ||
10408 | /* | |
10409 | * Commits a log block to the L2ARC device. This routine is invoked from | |
10410 | * l2arc_write_buffers when the log block fills up. | |
10411 | * This function allocates some memory to temporarily hold the serialized | |
10412 | * buffer to be written. This is then released in l2arc_write_done. | |
10413 | */ | |
bcd53210 | 10414 | static uint64_t |
77f6826b GA |
10415 | l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio, l2arc_write_callback_t *cb) |
10416 | { | |
10417 | l2arc_log_blk_phys_t *lb = &dev->l2ad_log_blk; | |
10418 | l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; | |
10419 | uint64_t psize, asize; | |
10420 | zio_t *wzio; | |
10421 | l2arc_lb_abd_buf_t *abd_buf; | |
bff26b02 | 10422 | uint8_t *tmpbuf = NULL; |
77f6826b GA |
10423 | l2arc_lb_ptr_buf_t *lb_ptr_buf; |
10424 | ||
657fd33b | 10425 | VERIFY3S(dev->l2ad_log_ent_idx, ==, dev->l2ad_log_entries); |
77f6826b | 10426 | |
77f6826b GA |
10427 | abd_buf = zio_buf_alloc(sizeof (*abd_buf)); |
10428 | abd_buf->abd = abd_get_from_buf(lb, sizeof (*lb)); | |
10429 | lb_ptr_buf = kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t), KM_SLEEP); | |
10430 | lb_ptr_buf->lb_ptr = kmem_zalloc(sizeof (l2arc_log_blkptr_t), KM_SLEEP); | |
10431 | ||
10432 | /* link the buffer into the block chain */ | |
10433 | lb->lb_prev_lbp = l2dhdr->dh_start_lbps[1]; | |
10434 | lb->lb_magic = L2ARC_LOG_BLK_MAGIC; | |
10435 | ||
657fd33b GA |
10436 | /* |
10437 | * l2arc_log_blk_commit() may be called multiple times during a single | |
10438 | * l2arc_write_buffers() call. Save the allocated abd buffers in a list | |
10439 | * so we can free them in l2arc_write_done() later on. | |
10440 | */ | |
77f6826b | 10441 | list_insert_tail(&cb->l2wcb_abd_list, abd_buf); |
657fd33b GA |
10442 | |
10443 | /* try to compress the buffer */ | |
77f6826b | 10444 | psize = zio_compress_data(ZIO_COMPRESS_LZ4, |
bff26b02 | 10445 | abd_buf->abd, (void **) &tmpbuf, sizeof (*lb), 0); |
77f6826b GA |
10446 | |
10447 | /* a log block is never entirely zero */ | |
10448 | ASSERT(psize != 0); | |
10449 | asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
10450 | ASSERT(asize <= sizeof (*lb)); | |
10451 | ||
10452 | /* | |
10453 | * Update the start log block pointer in the device header to point | |
10454 | * to the log block we're about to write. | |
10455 | */ | |
10456 | l2dhdr->dh_start_lbps[1] = l2dhdr->dh_start_lbps[0]; | |
10457 | l2dhdr->dh_start_lbps[0].lbp_daddr = dev->l2ad_hand; | |
10458 | l2dhdr->dh_start_lbps[0].lbp_payload_asize = | |
10459 | dev->l2ad_log_blk_payload_asize; | |
10460 | l2dhdr->dh_start_lbps[0].lbp_payload_start = | |
10461 | dev->l2ad_log_blk_payload_start; | |
77f6826b GA |
10462 | L2BLK_SET_LSIZE( |
10463 | (&l2dhdr->dh_start_lbps[0])->lbp_prop, sizeof (*lb)); | |
10464 | L2BLK_SET_PSIZE( | |
10465 | (&l2dhdr->dh_start_lbps[0])->lbp_prop, asize); | |
10466 | L2BLK_SET_CHECKSUM( | |
10467 | (&l2dhdr->dh_start_lbps[0])->lbp_prop, | |
10468 | ZIO_CHECKSUM_FLETCHER_4); | |
10469 | if (asize < sizeof (*lb)) { | |
10470 | /* compression succeeded */ | |
861166b0 | 10471 | memset(tmpbuf + psize, 0, asize - psize); |
77f6826b GA |
10472 | L2BLK_SET_COMPRESS( |
10473 | (&l2dhdr->dh_start_lbps[0])->lbp_prop, | |
10474 | ZIO_COMPRESS_LZ4); | |
10475 | } else { | |
10476 | /* compression failed */ | |
861166b0 | 10477 | memcpy(tmpbuf, lb, sizeof (*lb)); |
77f6826b GA |
10478 | L2BLK_SET_COMPRESS( |
10479 | (&l2dhdr->dh_start_lbps[0])->lbp_prop, | |
10480 | ZIO_COMPRESS_OFF); | |
10481 | } | |
10482 | ||
10483 | /* checksum what we're about to write */ | |
10484 | fletcher_4_native(tmpbuf, asize, NULL, | |
10485 | &l2dhdr->dh_start_lbps[0].lbp_cksum); | |
10486 | ||
e2af2acc | 10487 | abd_free(abd_buf->abd); |
77f6826b GA |
10488 | |
10489 | /* perform the write itself */ | |
10490 | abd_buf->abd = abd_get_from_buf(tmpbuf, sizeof (*lb)); | |
10491 | abd_take_ownership_of_buf(abd_buf->abd, B_TRUE); | |
10492 | wzio = zio_write_phys(pio, dev->l2ad_vdev, dev->l2ad_hand, | |
10493 | asize, abd_buf->abd, ZIO_CHECKSUM_OFF, NULL, NULL, | |
10494 | ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_CANFAIL, B_FALSE); | |
10495 | DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, zio_t *, wzio); | |
10496 | (void) zio_nowait(wzio); | |
10497 | ||
10498 | dev->l2ad_hand += asize; | |
10499 | /* | |
10500 | * Include the committed log block's pointer in the list of pointers | |
10501 | * to log blocks present in the L2ARC device. | |
10502 | */ | |
861166b0 | 10503 | memcpy(lb_ptr_buf->lb_ptr, &l2dhdr->dh_start_lbps[0], |
77f6826b GA |
10504 | sizeof (l2arc_log_blkptr_t)); |
10505 | mutex_enter(&dev->l2ad_mtx); | |
10506 | list_insert_head(&dev->l2ad_lbptr_list, lb_ptr_buf); | |
657fd33b GA |
10507 | ARCSTAT_INCR(arcstat_l2_log_blk_asize, asize); |
10508 | ARCSTAT_BUMP(arcstat_l2_log_blk_count); | |
10509 | zfs_refcount_add_many(&dev->l2ad_lb_asize, asize, lb_ptr_buf); | |
10510 | zfs_refcount_add(&dev->l2ad_lb_count, lb_ptr_buf); | |
77f6826b GA |
10511 | mutex_exit(&dev->l2ad_mtx); |
10512 | vdev_space_update(dev->l2ad_vdev, asize, 0, 0); | |
10513 | ||
10514 | /* bump the kstats */ | |
10515 | ARCSTAT_INCR(arcstat_l2_write_bytes, asize); | |
10516 | ARCSTAT_BUMP(arcstat_l2_log_blk_writes); | |
657fd33b | 10517 | ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize, asize); |
77f6826b GA |
10518 | ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio, |
10519 | dev->l2ad_log_blk_payload_asize / asize); | |
10520 | ||
10521 | /* start a new log block */ | |
10522 | dev->l2ad_log_ent_idx = 0; | |
10523 | dev->l2ad_log_blk_payload_asize = 0; | |
10524 | dev->l2ad_log_blk_payload_start = 0; | |
bcd53210 GA |
10525 | |
10526 | return (asize); | |
77f6826b GA |
10527 | } |
10528 | ||
10529 | /* | |
10530 | * Validates an L2ARC log block address to make sure that it can be read | |
10531 | * from the provided L2ARC device. | |
10532 | */ | |
10533 | boolean_t | |
10534 | l2arc_log_blkptr_valid(l2arc_dev_t *dev, const l2arc_log_blkptr_t *lbp) | |
10535 | { | |
657fd33b GA |
10536 | /* L2BLK_GET_PSIZE returns aligned size for log blocks */ |
10537 | uint64_t asize = L2BLK_GET_PSIZE((lbp)->lbp_prop); | |
10538 | uint64_t end = lbp->lbp_daddr + asize - 1; | |
77f6826b GA |
10539 | uint64_t start = lbp->lbp_payload_start; |
10540 | boolean_t evicted = B_FALSE; | |
10541 | ||
10542 | /* | |
10543 | * A log block is valid if all of the following conditions are true: | |
10544 | * - it fits entirely (including its payload) between l2ad_start and | |
10545 | * l2ad_end | |
10546 | * - it has a valid size | |
10547 | * - neither the log block itself nor part of its payload was evicted | |
10548 | * by l2arc_evict(): | |
10549 | * | |
10550 | * l2ad_hand l2ad_evict | |
10551 | * | | lbp_daddr | |
10552 | * | start | | end | |
10553 | * | | | | | | |
10554 | * V V V V V | |
10555 | * l2ad_start ============================================ l2ad_end | |
10556 | * --------------------------|||| | |
10557 | * ^ ^ | |
10558 | * | log block | |
10559 | * payload | |
10560 | */ | |
10561 | ||
10562 | evicted = | |
10563 | l2arc_range_check_overlap(start, end, dev->l2ad_hand) || | |
10564 | l2arc_range_check_overlap(start, end, dev->l2ad_evict) || | |
10565 | l2arc_range_check_overlap(dev->l2ad_hand, dev->l2ad_evict, start) || | |
10566 | l2arc_range_check_overlap(dev->l2ad_hand, dev->l2ad_evict, end); | |
10567 | ||
10568 | return (start >= dev->l2ad_start && end <= dev->l2ad_end && | |
657fd33b | 10569 | asize > 0 && asize <= sizeof (l2arc_log_blk_phys_t) && |
77f6826b GA |
10570 | (!evicted || dev->l2ad_first)); |
10571 | } | |
10572 | ||
10573 | /* | |
10574 | * Inserts ARC buffer header `hdr' into the current L2ARC log block on | |
10575 | * the device. The buffer being inserted must be present in L2ARC. | |
10576 | * Returns B_TRUE if the L2ARC log block is full and needs to be committed | |
10577 | * to L2ARC, or B_FALSE if it still has room for more ARC buffers. | |
10578 | */ | |
10579 | static boolean_t | |
10580 | l2arc_log_blk_insert(l2arc_dev_t *dev, const arc_buf_hdr_t *hdr) | |
10581 | { | |
10582 | l2arc_log_blk_phys_t *lb = &dev->l2ad_log_blk; | |
10583 | l2arc_log_ent_phys_t *le; | |
77f6826b | 10584 | |
657fd33b | 10585 | if (dev->l2ad_log_entries == 0) |
77f6826b GA |
10586 | return (B_FALSE); |
10587 | ||
10588 | int index = dev->l2ad_log_ent_idx++; | |
10589 | ||
657fd33b | 10590 | ASSERT3S(index, <, dev->l2ad_log_entries); |
77f6826b GA |
10591 | ASSERT(HDR_HAS_L2HDR(hdr)); |
10592 | ||
10593 | le = &lb->lb_entries[index]; | |
861166b0 | 10594 | memset(le, 0, sizeof (*le)); |
77f6826b GA |
10595 | le->le_dva = hdr->b_dva; |
10596 | le->le_birth = hdr->b_birth; | |
10597 | le->le_daddr = hdr->b_l2hdr.b_daddr; | |
10598 | if (index == 0) | |
10599 | dev->l2ad_log_blk_payload_start = le->le_daddr; | |
10600 | L2BLK_SET_LSIZE((le)->le_prop, HDR_GET_LSIZE(hdr)); | |
10601 | L2BLK_SET_PSIZE((le)->le_prop, HDR_GET_PSIZE(hdr)); | |
10602 | L2BLK_SET_COMPRESS((le)->le_prop, HDR_GET_COMPRESS(hdr)); | |
10b3c7f5 | 10603 | le->le_complevel = hdr->b_complevel; |
77f6826b GA |
10604 | L2BLK_SET_TYPE((le)->le_prop, hdr->b_type); |
10605 | L2BLK_SET_PROTECTED((le)->le_prop, !!(HDR_PROTECTED(hdr))); | |
10606 | L2BLK_SET_PREFETCH((le)->le_prop, !!(HDR_PREFETCH(hdr))); | |
08532162 | 10607 | L2BLK_SET_STATE((le)->le_prop, hdr->b_l1hdr.b_state->arcs_state); |
77f6826b GA |
10608 | |
10609 | dev->l2ad_log_blk_payload_asize += vdev_psize_to_asize(dev->l2ad_vdev, | |
10610 | HDR_GET_PSIZE(hdr)); | |
10611 | ||
657fd33b | 10612 | return (dev->l2ad_log_ent_idx == dev->l2ad_log_entries); |
77f6826b GA |
10613 | } |
10614 | ||
10615 | /* | |
10616 | * Checks whether a given L2ARC device address sits in a time-sequential | |
10617 | * range. The trick here is that the L2ARC is a rotary buffer, so we can't | |
10618 | * just do a range comparison, we need to handle the situation in which the | |
10619 | * range wraps around the end of the L2ARC device. Arguments: | |
10620 | * bottom -- Lower end of the range to check (written to earlier). | |
10621 | * top -- Upper end of the range to check (written to later). | |
10622 | * check -- The address for which we want to determine if it sits in | |
10623 | * between the top and bottom. | |
10624 | * | |
10625 | * The 3-way conditional below represents the following cases: | |
10626 | * | |
10627 | * bottom < top : Sequentially ordered case: | |
10628 | * <check>--------+-------------------+ | |
10629 | * | (overlap here?) | | |
10630 | * L2ARC dev V V | |
10631 | * |---------------<bottom>============<top>--------------| | |
10632 | * | |
10633 | * bottom > top: Looped-around case: | |
10634 | * <check>--------+------------------+ | |
10635 | * | (overlap here?) | | |
10636 | * L2ARC dev V V | |
10637 | * |===============<top>---------------<bottom>===========| | |
10638 | * ^ ^ | |
10639 | * | (or here?) | | |
10640 | * +---------------+---------<check> | |
10641 | * | |
10642 | * top == bottom : Just a single address comparison. | |
10643 | */ | |
10644 | boolean_t | |
10645 | l2arc_range_check_overlap(uint64_t bottom, uint64_t top, uint64_t check) | |
10646 | { | |
10647 | if (bottom < top) | |
10648 | return (bottom <= check && check <= top); | |
10649 | else if (bottom > top) | |
10650 | return (check <= top || bottom <= check); | |
10651 | else | |
10652 | return (check == top); | |
10653 | } | |
10654 | ||
0f699108 AZ |
10655 | EXPORT_SYMBOL(arc_buf_size); |
10656 | EXPORT_SYMBOL(arc_write); | |
c28b2279 | 10657 | EXPORT_SYMBOL(arc_read); |
e0b0ca98 | 10658 | EXPORT_SYMBOL(arc_buf_info); |
c28b2279 | 10659 | EXPORT_SYMBOL(arc_getbuf_func); |
ab26409d BB |
10660 | EXPORT_SYMBOL(arc_add_prune_callback); |
10661 | EXPORT_SYMBOL(arc_remove_prune_callback); | |
c28b2279 | 10662 | |
e945e8d7 | 10663 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min, param_set_arc_min, |
ab8d9c17 | 10664 | spl_param_get_u64, ZMOD_RW, "Minimum ARC size in bytes"); |
c28b2279 | 10665 | |
e945e8d7 | 10666 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, max, param_set_arc_max, |
ab8d9c17 | 10667 | spl_param_get_u64, ZMOD_RW, "Maximum ARC size in bytes"); |
c28b2279 | 10668 | |
a8d83e2a AM |
10669 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, meta_balance, UINT, ZMOD_RW, |
10670 | "Balance between metadata and data on ghost hits."); | |
f6046738 | 10671 | |
e3570464 | 10672 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, grow_retry, param_set_arc_int, |
fdc2d303 | 10673 | param_get_uint, ZMOD_RW, "Seconds before growing ARC size"); |
c409e464 | 10674 | |
e3570464 | 10675 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, shrink_shift, param_set_arc_int, |
fdc2d303 | 10676 | param_get_uint, ZMOD_RW, "log2(fraction of ARC to reclaim)"); |
c409e464 | 10677 | |
03fdcb9a | 10678 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, pc_percent, UINT, ZMOD_RW, |
310ab9d2 | 10679 | "Percent of pagecache to reclaim ARC to"); |
03b60eee | 10680 | |
fdc2d303 | 10681 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, average_blocksize, UINT, ZMOD_RD, |
03fdcb9a | 10682 | "Target average block size"); |
49ddb315 | 10683 | |
03fdcb9a | 10684 | ZFS_MODULE_PARAM(zfs, zfs_, compressed_arc_enabled, INT, ZMOD_RW, |
310ab9d2 | 10685 | "Disable compressed ARC buffers"); |
d3c2ae1c | 10686 | |
e3570464 | 10687 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min_prefetch_ms, param_set_arc_int, |
fdc2d303 | 10688 | param_get_uint, ZMOD_RW, "Min life of prefetch block in ms"); |
d4a72f23 | 10689 | |
e3570464 | 10690 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min_prescient_prefetch_ms, |
fdc2d303 | 10691 | param_set_arc_int, param_get_uint, ZMOD_RW, |
d4a72f23 | 10692 | "Min life of prescient prefetched block in ms"); |
bce45ec9 | 10693 | |
ab8d9c17 | 10694 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, write_max, U64, ZMOD_RW, |
03fdcb9a | 10695 | "Max write bytes per interval"); |
abd8610c | 10696 | |
ab8d9c17 | 10697 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, write_boost, U64, ZMOD_RW, |
03fdcb9a | 10698 | "Extra write bytes during device warmup"); |
abd8610c | 10699 | |
ab8d9c17 | 10700 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, headroom, U64, ZMOD_RW, |
03fdcb9a | 10701 | "Number of max device writes to precache"); |
abd8610c | 10702 | |
ab8d9c17 | 10703 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, headroom_boost, U64, ZMOD_RW, |
03fdcb9a | 10704 | "Compressed l2arc_headroom multiplier"); |
3a17a7a9 | 10705 | |
ab8d9c17 | 10706 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, trim_ahead, U64, ZMOD_RW, |
b7654bd7 GA |
10707 | "TRIM ahead L2ARC write size multiplier"); |
10708 | ||
ab8d9c17 | 10709 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_secs, U64, ZMOD_RW, |
03fdcb9a | 10710 | "Seconds between L2ARC writing"); |
abd8610c | 10711 | |
ab8d9c17 | 10712 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_min_ms, U64, ZMOD_RW, |
03fdcb9a | 10713 | "Min feed interval in milliseconds"); |
abd8610c | 10714 | |
03fdcb9a MM |
10715 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, noprefetch, INT, ZMOD_RW, |
10716 | "Skip caching prefetched buffers"); | |
abd8610c | 10717 | |
03fdcb9a MM |
10718 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_again, INT, ZMOD_RW, |
10719 | "Turbo L2ARC warmup"); | |
abd8610c | 10720 | |
03fdcb9a MM |
10721 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, norw, INT, ZMOD_RW, |
10722 | "No reads during writes"); | |
abd8610c | 10723 | |
fdc2d303 | 10724 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, meta_percent, UINT, ZMOD_RW, |
523e1295 AM |
10725 | "Percent of ARC size allowed for L2ARC-only headers"); |
10726 | ||
77f6826b GA |
10727 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, rebuild_enabled, INT, ZMOD_RW, |
10728 | "Rebuild the L2ARC when importing a pool"); | |
10729 | ||
ab8d9c17 | 10730 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, rebuild_blocks_min_l2size, U64, ZMOD_RW, |
77f6826b GA |
10731 | "Min size in bytes to write rebuild log blocks in L2ARC"); |
10732 | ||
feb3a7ee GA |
10733 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, mfuonly, INT, ZMOD_RW, |
10734 | "Cache only MFU data from ARC into L2ARC"); | |
10735 | ||
c9d62d13 | 10736 | ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, exclude_special, INT, ZMOD_RW, |
7ada752a | 10737 | "Exclude dbufs on special vdevs from being cached to L2ARC if set."); |
c9d62d13 | 10738 | |
e3570464 | 10739 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, lotsfree_percent, param_set_arc_int, |
fdc2d303 | 10740 | param_get_uint, ZMOD_RW, "System free memory I/O throttle in bytes"); |
7e8bddd0 | 10741 | |
ab8d9c17 RY |
10742 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, sys_free, param_set_arc_u64, |
10743 | spl_param_get_u64, ZMOD_RW, "System free memory target size in bytes"); | |
11f552fa | 10744 | |
ab8d9c17 RY |
10745 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, dnode_limit, param_set_arc_u64, |
10746 | spl_param_get_u64, ZMOD_RW, "Minimum bytes of dnodes in ARC"); | |
25458cbe | 10747 | |
e3570464 | 10748 | ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, dnode_limit_percent, |
ab8d9c17 | 10749 | param_set_arc_int, param_get_uint, ZMOD_RW, |
9907cc1c G |
10750 | "Percent of ARC meta buffers for dnodes"); |
10751 | ||
ab8d9c17 | 10752 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, dnode_reduce_percent, UINT, ZMOD_RW, |
25458cbe | 10753 | "Percentage of excess dnodes to try to unpin"); |
3442c2a0 | 10754 | |
fdc2d303 | 10755 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, eviction_pct, UINT, ZMOD_RW, |
eb02a4c6 RM |
10756 | "When full, ARC allocation waits for eviction of this % of alloc size"); |
10757 | ||
fdc2d303 | 10758 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, evict_batch_limit, UINT, ZMOD_RW, |
eb02a4c6 | 10759 | "The number of headers to evict per sublist before moving to the next"); |
462217d1 AM |
10760 | |
10761 | ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, prune_task_threads, INT, ZMOD_RW, | |
10762 | "Number of arc_prune threads"); |