]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
428870ff | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
3ec34e55 | 23 | * Copyright (c) 2018, Joyent, Inc. |
c3bd3fb4 | 24 | * Copyright (c) 2011, 2018 by Delphix. All rights reserved. |
36da08ef | 25 | * Copyright (c) 2014 by Saso Kiselkov. All rights reserved. |
3ec34e55 | 26 | * Copyright 2017 Nexenta Systems, Inc. All rights reserved. |
34dc7c2f BB |
27 | */ |
28 | ||
34dc7c2f BB |
29 | /* |
30 | * DVA-based Adjustable Replacement Cache | |
31 | * | |
32 | * While much of the theory of operation used here is | |
33 | * based on the self-tuning, low overhead replacement cache | |
34 | * presented by Megiddo and Modha at FAST 2003, there are some | |
35 | * significant differences: | |
36 | * | |
37 | * 1. The Megiddo and Modha model assumes any page is evictable. | |
38 | * Pages in its cache cannot be "locked" into memory. This makes | |
39 | * the eviction algorithm simple: evict the last page in the list. | |
40 | * This also make the performance characteristics easy to reason | |
41 | * about. Our cache is not so simple. At any given moment, some | |
42 | * subset of the blocks in the cache are un-evictable because we | |
43 | * have handed out a reference to them. Blocks are only evictable | |
44 | * when there are no external references active. This makes | |
45 | * eviction far more problematic: we choose to evict the evictable | |
46 | * blocks that are the "lowest" in the list. | |
47 | * | |
48 | * There are times when it is not possible to evict the requested | |
49 | * space. In these circumstances we are unable to adjust the cache | |
50 | * size. To prevent the cache growing unbounded at these times we | |
51 | * implement a "cache throttle" that slows the flow of new data | |
52 | * into the cache until we can make space available. | |
53 | * | |
54 | * 2. The Megiddo and Modha model assumes a fixed cache size. | |
55 | * Pages are evicted when the cache is full and there is a cache | |
56 | * miss. Our model has a variable sized cache. It grows with | |
57 | * high use, but also tries to react to memory pressure from the | |
58 | * operating system: decreasing its size when system memory is | |
59 | * tight. | |
60 | * | |
61 | * 3. The Megiddo and Modha model assumes a fixed page size. All | |
d3cc8b15 | 62 | * elements of the cache are therefore exactly the same size. So |
34dc7c2f BB |
63 | * when adjusting the cache size following a cache miss, its simply |
64 | * a matter of choosing a single page to evict. In our model, we | |
65 | * have variable sized cache blocks (rangeing from 512 bytes to | |
d3cc8b15 | 66 | * 128K bytes). We therefore choose a set of blocks to evict to make |
34dc7c2f BB |
67 | * space for a cache miss that approximates as closely as possible |
68 | * the space used by the new block. | |
69 | * | |
70 | * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" | |
71 | * by N. Megiddo & D. Modha, FAST 2003 | |
72 | */ | |
73 | ||
74 | /* | |
75 | * The locking model: | |
76 | * | |
77 | * A new reference to a cache buffer can be obtained in two | |
78 | * ways: 1) via a hash table lookup using the DVA as a key, | |
79 | * or 2) via one of the ARC lists. The arc_read() interface | |
2aa34383 | 80 | * uses method 1, while the internal ARC algorithms for |
d3cc8b15 | 81 | * adjusting the cache use method 2. We therefore provide two |
34dc7c2f | 82 | * types of locks: 1) the hash table lock array, and 2) the |
2aa34383 | 83 | * ARC list locks. |
34dc7c2f | 84 | * |
5c839890 BC |
85 | * Buffers do not have their own mutexes, rather they rely on the |
86 | * hash table mutexes for the bulk of their protection (i.e. most | |
87 | * fields in the arc_buf_hdr_t are protected by these mutexes). | |
34dc7c2f BB |
88 | * |
89 | * buf_hash_find() returns the appropriate mutex (held) when it | |
90 | * locates the requested buffer in the hash table. It returns | |
91 | * NULL for the mutex if the buffer was not in the table. | |
92 | * | |
93 | * buf_hash_remove() expects the appropriate hash mutex to be | |
94 | * already held before it is invoked. | |
95 | * | |
2aa34383 | 96 | * Each ARC state also has a mutex which is used to protect the |
34dc7c2f | 97 | * buffer list associated with the state. When attempting to |
2aa34383 | 98 | * obtain a hash table lock while holding an ARC list lock you |
34dc7c2f BB |
99 | * must use: mutex_tryenter() to avoid deadlock. Also note that |
100 | * the active state mutex must be held before the ghost state mutex. | |
101 | * | |
ab26409d BB |
102 | * It as also possible to register a callback which is run when the |
103 | * arc_meta_limit is reached and no buffers can be safely evicted. In | |
104 | * this case the arc user should drop a reference on some arc buffers so | |
105 | * they can be reclaimed and the arc_meta_limit honored. For example, | |
106 | * when using the ZPL each dentry holds a references on a znode. These | |
107 | * dentries must be pruned before the arc buffer holding the znode can | |
108 | * be safely evicted. | |
109 | * | |
34dc7c2f BB |
110 | * Note that the majority of the performance stats are manipulated |
111 | * with atomic operations. | |
112 | * | |
b9541d6b | 113 | * The L2ARC uses the l2ad_mtx on each vdev for the following: |
34dc7c2f BB |
114 | * |
115 | * - L2ARC buflist creation | |
116 | * - L2ARC buflist eviction | |
117 | * - L2ARC write completion, which walks L2ARC buflists | |
118 | * - ARC header destruction, as it removes from L2ARC buflists | |
119 | * - ARC header release, as it removes from L2ARC buflists | |
120 | */ | |
121 | ||
d3c2ae1c GW |
122 | /* |
123 | * ARC operation: | |
124 | * | |
125 | * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure. | |
126 | * This structure can point either to a block that is still in the cache or to | |
127 | * one that is only accessible in an L2 ARC device, or it can provide | |
128 | * information about a block that was recently evicted. If a block is | |
129 | * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough | |
130 | * information to retrieve it from the L2ARC device. This information is | |
131 | * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block | |
132 | * that is in this state cannot access the data directly. | |
133 | * | |
134 | * Blocks that are actively being referenced or have not been evicted | |
135 | * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within | |
136 | * the arc_buf_hdr_t that will point to the data block in memory. A block can | |
137 | * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC | |
2aa34383 | 138 | * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and |
a6255b7f | 139 | * also in the arc_buf_hdr_t's private physical data block pointer (b_pabd). |
2aa34383 DK |
140 | * |
141 | * The L1ARC's data pointer may or may not be uncompressed. The ARC has the | |
a6255b7f DQ |
142 | * ability to store the physical data (b_pabd) associated with the DVA of the |
143 | * arc_buf_hdr_t. Since the b_pabd is a copy of the on-disk physical block, | |
2aa34383 DK |
144 | * it will match its on-disk compression characteristics. This behavior can be |
145 | * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the | |
a6255b7f | 146 | * compressed ARC functionality is disabled, the b_pabd will point to an |
2aa34383 DK |
147 | * uncompressed version of the on-disk data. |
148 | * | |
149 | * Data in the L1ARC is not accessed by consumers of the ARC directly. Each | |
150 | * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it. | |
151 | * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC | |
152 | * consumer. The ARC will provide references to this data and will keep it | |
153 | * cached until it is no longer in use. The ARC caches only the L1ARC's physical | |
154 | * data block and will evict any arc_buf_t that is no longer referenced. The | |
155 | * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the | |
d3c2ae1c GW |
156 | * "overhead_size" kstat. |
157 | * | |
2aa34383 DK |
158 | * Depending on the consumer, an arc_buf_t can be requested in uncompressed or |
159 | * compressed form. The typical case is that consumers will want uncompressed | |
160 | * data, and when that happens a new data buffer is allocated where the data is | |
161 | * decompressed for them to use. Currently the only consumer who wants | |
162 | * compressed arc_buf_t's is "zfs send", when it streams data exactly as it | |
163 | * exists on disk. When this happens, the arc_buf_t's data buffer is shared | |
164 | * with the arc_buf_hdr_t. | |
d3c2ae1c | 165 | * |
2aa34383 DK |
166 | * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The |
167 | * first one is owned by a compressed send consumer (and therefore references | |
168 | * the same compressed data buffer as the arc_buf_hdr_t) and the second could be | |
169 | * used by any other consumer (and has its own uncompressed copy of the data | |
170 | * buffer). | |
d3c2ae1c | 171 | * |
2aa34383 DK |
172 | * arc_buf_hdr_t |
173 | * +-----------+ | |
174 | * | fields | | |
175 | * | common to | | |
176 | * | L1- and | | |
177 | * | L2ARC | | |
178 | * +-----------+ | |
179 | * | l2arc_buf_hdr_t | |
180 | * | | | |
181 | * +-----------+ | |
182 | * | l1arc_buf_hdr_t | |
183 | * | | arc_buf_t | |
184 | * | b_buf +------------>+-----------+ arc_buf_t | |
a6255b7f | 185 | * | b_pabd +-+ |b_next +---->+-----------+ |
2aa34383 DK |
186 | * +-----------+ | |-----------| |b_next +-->NULL |
187 | * | |b_comp = T | +-----------+ | |
188 | * | |b_data +-+ |b_comp = F | | |
189 | * | +-----------+ | |b_data +-+ | |
190 | * +->+------+ | +-----------+ | | |
191 | * compressed | | | | | |
192 | * data | |<--------------+ | uncompressed | |
193 | * +------+ compressed, | data | |
194 | * shared +-->+------+ | |
195 | * data | | | |
196 | * | | | |
197 | * +------+ | |
d3c2ae1c GW |
198 | * |
199 | * When a consumer reads a block, the ARC must first look to see if the | |
2aa34383 DK |
200 | * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new |
201 | * arc_buf_t and either copies uncompressed data into a new data buffer from an | |
a6255b7f DQ |
202 | * existing uncompressed arc_buf_t, decompresses the hdr's b_pabd buffer into a |
203 | * new data buffer, or shares the hdr's b_pabd buffer, depending on whether the | |
2aa34383 DK |
204 | * hdr is compressed and the desired compression characteristics of the |
205 | * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the | |
206 | * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be | |
207 | * the last buffer in the hdr's b_buf list, however a shared compressed buf can | |
208 | * be anywhere in the hdr's list. | |
d3c2ae1c GW |
209 | * |
210 | * The diagram below shows an example of an uncompressed ARC hdr that is | |
2aa34383 DK |
211 | * sharing its data with an arc_buf_t (note that the shared uncompressed buf is |
212 | * the last element in the buf list): | |
d3c2ae1c GW |
213 | * |
214 | * arc_buf_hdr_t | |
215 | * +-----------+ | |
216 | * | | | |
217 | * | | | |
218 | * | | | |
219 | * +-----------+ | |
220 | * l2arc_buf_hdr_t| | | |
221 | * | | | |
222 | * +-----------+ | |
223 | * l1arc_buf_hdr_t| | | |
224 | * | | arc_buf_t (shared) | |
225 | * | b_buf +------------>+---------+ arc_buf_t | |
226 | * | | |b_next +---->+---------+ | |
a6255b7f | 227 | * | b_pabd +-+ |---------| |b_next +-->NULL |
d3c2ae1c GW |
228 | * +-----------+ | | | +---------+ |
229 | * | |b_data +-+ | | | |
230 | * | +---------+ | |b_data +-+ | |
231 | * +->+------+ | +---------+ | | |
232 | * | | | | | |
233 | * uncompressed | | | | | |
234 | * data +------+ | | | |
235 | * ^ +->+------+ | | |
236 | * | uncompressed | | | | |
237 | * | data | | | | |
238 | * | +------+ | | |
239 | * +---------------------------------+ | |
240 | * | |
a6255b7f | 241 | * Writing to the ARC requires that the ARC first discard the hdr's b_pabd |
d3c2ae1c | 242 | * since the physical block is about to be rewritten. The new data contents |
2aa34383 DK |
243 | * will be contained in the arc_buf_t. As the I/O pipeline performs the write, |
244 | * it may compress the data before writing it to disk. The ARC will be called | |
245 | * with the transformed data and will bcopy the transformed on-disk block into | |
a6255b7f | 246 | * a newly allocated b_pabd. Writes are always done into buffers which have |
2aa34383 DK |
247 | * either been loaned (and hence are new and don't have other readers) or |
248 | * buffers which have been released (and hence have their own hdr, if there | |
249 | * were originally other readers of the buf's original hdr). This ensures that | |
250 | * the ARC only needs to update a single buf and its hdr after a write occurs. | |
d3c2ae1c | 251 | * |
a6255b7f DQ |
252 | * When the L2ARC is in use, it will also take advantage of the b_pabd. The |
253 | * L2ARC will always write the contents of b_pabd to the L2ARC. This means | |
2aa34383 | 254 | * that when compressed ARC is enabled that the L2ARC blocks are identical |
d3c2ae1c GW |
255 | * to the on-disk block in the main data pool. This provides a significant |
256 | * advantage since the ARC can leverage the bp's checksum when reading from the | |
257 | * L2ARC to determine if the contents are valid. However, if the compressed | |
2aa34383 | 258 | * ARC is disabled, then the L2ARC's block must be transformed to look |
d3c2ae1c GW |
259 | * like the physical block in the main data pool before comparing the |
260 | * checksum and determining its validity. | |
b5256303 TC |
261 | * |
262 | * The L1ARC has a slightly different system for storing encrypted data. | |
263 | * Raw (encrypted + possibly compressed) data has a few subtle differences from | |
264 | * data that is just compressed. The biggest difference is that it is not | |
265 | * possible to decrypt encrypted data (or visa versa) if the keys aren't loaded. | |
266 | * The other difference is that encryption cannot be treated as a suggestion. | |
267 | * If a caller would prefer compressed data, but they actually wind up with | |
268 | * uncompressed data the worst thing that could happen is there might be a | |
269 | * performance hit. If the caller requests encrypted data, however, we must be | |
270 | * sure they actually get it or else secret information could be leaked. Raw | |
271 | * data is stored in hdr->b_crypt_hdr.b_rabd. An encrypted header, therefore, | |
272 | * may have both an encrypted version and a decrypted version of its data at | |
273 | * once. When a caller needs a raw arc_buf_t, it is allocated and the data is | |
274 | * copied out of this header. To avoid complications with b_pabd, raw buffers | |
275 | * cannot be shared. | |
d3c2ae1c GW |
276 | */ |
277 | ||
34dc7c2f BB |
278 | #include <sys/spa.h> |
279 | #include <sys/zio.h> | |
d3c2ae1c | 280 | #include <sys/spa_impl.h> |
3a17a7a9 | 281 | #include <sys/zio_compress.h> |
d3c2ae1c | 282 | #include <sys/zio_checksum.h> |
34dc7c2f BB |
283 | #include <sys/zfs_context.h> |
284 | #include <sys/arc.h> | |
36da08ef | 285 | #include <sys/refcount.h> |
b128c09f | 286 | #include <sys/vdev.h> |
9babb374 | 287 | #include <sys/vdev_impl.h> |
e8b96c60 | 288 | #include <sys/dsl_pool.h> |
a6255b7f | 289 | #include <sys/zio_checksum.h> |
ca0bf58d | 290 | #include <sys/multilist.h> |
a6255b7f | 291 | #include <sys/abd.h> |
b5256303 TC |
292 | #include <sys/zil.h> |
293 | #include <sys/fm/fs/zfs.h> | |
34dc7c2f | 294 | #ifdef _KERNEL |
93ce2b4c | 295 | #include <sys/shrinker.h> |
34dc7c2f | 296 | #include <sys/vmsystm.h> |
ab26409d | 297 | #include <sys/zpl.h> |
e9a77290 | 298 | #include <linux/page_compat.h> |
34dc7c2f BB |
299 | #endif |
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> |
49ee64e5 | 305 | #include <sys/trace_arc.h> |
37fb3e43 PD |
306 | #include <sys/aggsum.h> |
307 | #include <sys/cityhash.h> | |
34dc7c2f | 308 | |
498877ba MA |
309 | #ifndef _KERNEL |
310 | /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ | |
311 | boolean_t arc_watch = B_FALSE; | |
312 | #endif | |
313 | ||
3ec34e55 BL |
314 | /* |
315 | * This thread's job is to keep enough free memory in the system, by | |
316 | * calling arc_kmem_reap_soon() plus arc_reduce_target_size(), which improves | |
317 | * arc_available_memory(). | |
318 | */ | |
319 | static zthr_t *arc_reap_zthr; | |
320 | ||
321 | /* | |
322 | * This thread's job is to keep arc_size under arc_c, by calling | |
323 | * arc_adjust(), which improves arc_is_overflowing(). | |
324 | */ | |
325 | static zthr_t *arc_adjust_zthr; | |
326 | ||
327 | static kmutex_t arc_adjust_lock; | |
328 | static kcondvar_t arc_adjust_waiters_cv; | |
329 | static boolean_t arc_adjust_needed = B_FALSE; | |
ca0bf58d | 330 | |
e8b96c60 | 331 | /* |
ca0bf58d PS |
332 | * The number of headers to evict in arc_evict_state_impl() before |
333 | * dropping the sublist lock and evicting from another sublist. A lower | |
334 | * value means we're more likely to evict the "correct" header (i.e. the | |
335 | * oldest header in the arc state), but comes with higher overhead | |
336 | * (i.e. more invocations of arc_evict_state_impl()). | |
337 | */ | |
338 | int zfs_arc_evict_batch_limit = 10; | |
339 | ||
34dc7c2f | 340 | /* number of seconds before growing cache again */ |
3ec34e55 BL |
341 | static int arc_grow_retry = 5; |
342 | ||
343 | /* | |
344 | * Minimum time between calls to arc_kmem_reap_soon(). | |
345 | */ | |
346 | int arc_kmem_cache_reap_retry_ms = 1000; | |
34dc7c2f | 347 | |
a6255b7f | 348 | /* shift of arc_c for calculating overflow limit in arc_get_data_impl */ |
3ec34e55 | 349 | int zfs_arc_overflow_shift = 8; |
62422785 | 350 | |
728d6ae9 | 351 | /* shift of arc_c for calculating both min and max arc_p */ |
3ec34e55 | 352 | int arc_p_min_shift = 4; |
728d6ae9 | 353 | |
d164b209 | 354 | /* log2(fraction of arc to reclaim) */ |
3ec34e55 | 355 | static int arc_shrink_shift = 7; |
d164b209 | 356 | |
03b60eee DB |
357 | /* percent of pagecache to reclaim arc to */ |
358 | #ifdef _KERNEL | |
3ec34e55 | 359 | static uint_t zfs_arc_pc_percent = 0; |
03b60eee DB |
360 | #endif |
361 | ||
34dc7c2f | 362 | /* |
ca67b33a MA |
363 | * log2(fraction of ARC which must be free to allow growing). |
364 | * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, | |
365 | * when reading a new block into the ARC, we will evict an equal-sized block | |
366 | * from the ARC. | |
367 | * | |
368 | * This must be less than arc_shrink_shift, so that when we shrink the ARC, | |
369 | * we will still not allow it to grow. | |
34dc7c2f | 370 | */ |
ca67b33a | 371 | int arc_no_grow_shift = 5; |
bce45ec9 | 372 | |
49ddb315 | 373 | |
ca0bf58d PS |
374 | /* |
375 | * minimum lifespan of a prefetch block in clock ticks | |
376 | * (initialized in arc_init()) | |
377 | */ | |
d4a72f23 TC |
378 | static int arc_min_prefetch_ms; |
379 | static int arc_min_prescient_prefetch_ms; | |
ca0bf58d | 380 | |
e8b96c60 MA |
381 | /* |
382 | * If this percent of memory is free, don't throttle. | |
383 | */ | |
384 | int arc_lotsfree_percent = 10; | |
385 | ||
3ec34e55 BL |
386 | /* |
387 | * hdr_recl() uses this to determine if the arc is up and running. | |
388 | */ | |
389 | static boolean_t arc_initialized; | |
34dc7c2f | 390 | |
b128c09f BB |
391 | /* |
392 | * The arc has filled available memory and has now warmed up. | |
393 | */ | |
394 | static boolean_t arc_warm; | |
395 | ||
d3c2ae1c GW |
396 | /* |
397 | * log2 fraction of the zio arena to keep free. | |
398 | */ | |
399 | int arc_zio_arena_free_shift = 2; | |
400 | ||
34dc7c2f BB |
401 | /* |
402 | * These tunables are for performance analysis. | |
403 | */ | |
c28b2279 BB |
404 | unsigned long zfs_arc_max = 0; |
405 | unsigned long zfs_arc_min = 0; | |
406 | unsigned long zfs_arc_meta_limit = 0; | |
ca0bf58d | 407 | unsigned long zfs_arc_meta_min = 0; |
25458cbe TC |
408 | unsigned long zfs_arc_dnode_limit = 0; |
409 | unsigned long zfs_arc_dnode_reduce_percent = 10; | |
ca67b33a MA |
410 | int zfs_arc_grow_retry = 0; |
411 | int zfs_arc_shrink_shift = 0; | |
728d6ae9 | 412 | int zfs_arc_p_min_shift = 0; |
ca67b33a | 413 | int zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ |
34dc7c2f | 414 | |
dae3e9ea DB |
415 | /* |
416 | * ARC dirty data constraints for arc_tempreserve_space() throttle. | |
417 | */ | |
418 | unsigned long zfs_arc_dirty_limit_percent = 50; /* total dirty data limit */ | |
419 | unsigned long zfs_arc_anon_limit_percent = 25; /* anon block dirty limit */ | |
420 | unsigned long zfs_arc_pool_dirty_percent = 20; /* each pool's anon allowance */ | |
421 | ||
422 | /* | |
423 | * Enable or disable compressed arc buffers. | |
424 | */ | |
d3c2ae1c GW |
425 | int zfs_compressed_arc_enabled = B_TRUE; |
426 | ||
9907cc1c G |
427 | /* |
428 | * ARC will evict meta buffers that exceed arc_meta_limit. This | |
429 | * tunable make arc_meta_limit adjustable for different workloads. | |
430 | */ | |
431 | unsigned long zfs_arc_meta_limit_percent = 75; | |
432 | ||
433 | /* | |
434 | * Percentage that can be consumed by dnodes of ARC meta buffers. | |
435 | */ | |
436 | unsigned long zfs_arc_dnode_limit_percent = 10; | |
437 | ||
bc888666 | 438 | /* |
ca67b33a | 439 | * These tunables are Linux specific |
bc888666 | 440 | */ |
11f552fa | 441 | unsigned long zfs_arc_sys_free = 0; |
d4a72f23 TC |
442 | int zfs_arc_min_prefetch_ms = 0; |
443 | int zfs_arc_min_prescient_prefetch_ms = 0; | |
ca67b33a MA |
444 | int zfs_arc_p_dampener_disable = 1; |
445 | int zfs_arc_meta_prune = 10000; | |
446 | int zfs_arc_meta_strategy = ARC_STRATEGY_META_BALANCED; | |
447 | int zfs_arc_meta_adjust_restarts = 4096; | |
7e8bddd0 | 448 | int zfs_arc_lotsfree_percent = 10; |
bc888666 | 449 | |
34dc7c2f BB |
450 | /* The 6 states: */ |
451 | static arc_state_t ARC_anon; | |
452 | static arc_state_t ARC_mru; | |
453 | static arc_state_t ARC_mru_ghost; | |
454 | static arc_state_t ARC_mfu; | |
455 | static arc_state_t ARC_mfu_ghost; | |
456 | static arc_state_t ARC_l2c_only; | |
457 | ||
458 | typedef struct arc_stats { | |
459 | kstat_named_t arcstat_hits; | |
460 | kstat_named_t arcstat_misses; | |
461 | kstat_named_t arcstat_demand_data_hits; | |
462 | kstat_named_t arcstat_demand_data_misses; | |
463 | kstat_named_t arcstat_demand_metadata_hits; | |
464 | kstat_named_t arcstat_demand_metadata_misses; | |
465 | kstat_named_t arcstat_prefetch_data_hits; | |
466 | kstat_named_t arcstat_prefetch_data_misses; | |
467 | kstat_named_t arcstat_prefetch_metadata_hits; | |
468 | kstat_named_t arcstat_prefetch_metadata_misses; | |
469 | kstat_named_t arcstat_mru_hits; | |
470 | kstat_named_t arcstat_mru_ghost_hits; | |
471 | kstat_named_t arcstat_mfu_hits; | |
472 | kstat_named_t arcstat_mfu_ghost_hits; | |
473 | kstat_named_t arcstat_deleted; | |
e49f1e20 WA |
474 | /* |
475 | * Number of buffers that could not be evicted because the hash lock | |
476 | * was held by another thread. The lock may not necessarily be held | |
477 | * by something using the same buffer, since hash locks are shared | |
478 | * by multiple buffers. | |
479 | */ | |
34dc7c2f | 480 | kstat_named_t arcstat_mutex_miss; |
0873bb63 BB |
481 | /* |
482 | * Number of buffers skipped when updating the access state due to the | |
483 | * header having already been released after acquiring the hash lock. | |
484 | */ | |
485 | kstat_named_t arcstat_access_skip; | |
e49f1e20 WA |
486 | /* |
487 | * Number of buffers skipped because they have I/O in progress, are | |
0873bb63 | 488 | * indirect prefetch buffers that have not lived long enough, or are |
e49f1e20 WA |
489 | * not from the spa we're trying to evict from. |
490 | */ | |
34dc7c2f | 491 | kstat_named_t arcstat_evict_skip; |
ca0bf58d PS |
492 | /* |
493 | * Number of times arc_evict_state() was unable to evict enough | |
494 | * buffers to reach its target amount. | |
495 | */ | |
496 | kstat_named_t arcstat_evict_not_enough; | |
428870ff BB |
497 | kstat_named_t arcstat_evict_l2_cached; |
498 | kstat_named_t arcstat_evict_l2_eligible; | |
499 | kstat_named_t arcstat_evict_l2_ineligible; | |
ca0bf58d | 500 | kstat_named_t arcstat_evict_l2_skip; |
34dc7c2f BB |
501 | kstat_named_t arcstat_hash_elements; |
502 | kstat_named_t arcstat_hash_elements_max; | |
503 | kstat_named_t arcstat_hash_collisions; | |
504 | kstat_named_t arcstat_hash_chains; | |
505 | kstat_named_t arcstat_hash_chain_max; | |
506 | kstat_named_t arcstat_p; | |
507 | kstat_named_t arcstat_c; | |
508 | kstat_named_t arcstat_c_min; | |
509 | kstat_named_t arcstat_c_max; | |
37fb3e43 | 510 | /* Not updated directly; only synced in arc_kstat_update. */ |
34dc7c2f | 511 | kstat_named_t arcstat_size; |
d3c2ae1c | 512 | /* |
a6255b7f | 513 | * Number of compressed bytes stored in the arc_buf_hdr_t's b_pabd. |
d3c2ae1c GW |
514 | * Note that the compressed bytes may match the uncompressed bytes |
515 | * if the block is either not compressed or compressed arc is disabled. | |
516 | */ | |
517 | kstat_named_t arcstat_compressed_size; | |
518 | /* | |
a6255b7f | 519 | * Uncompressed size of the data stored in b_pabd. If compressed |
d3c2ae1c GW |
520 | * arc is disabled then this value will be identical to the stat |
521 | * above. | |
522 | */ | |
523 | kstat_named_t arcstat_uncompressed_size; | |
524 | /* | |
525 | * Number of bytes stored in all the arc_buf_t's. This is classified | |
526 | * as "overhead" since this data is typically short-lived and will | |
527 | * be evicted from the arc when it becomes unreferenced unless the | |
528 | * zfs_keep_uncompressed_metadata or zfs_keep_uncompressed_level | |
529 | * values have been set (see comment in dbuf.c for more information). | |
530 | */ | |
531 | kstat_named_t arcstat_overhead_size; | |
500445c0 PS |
532 | /* |
533 | * Number of bytes consumed by internal ARC structures necessary | |
534 | * for tracking purposes; these structures are not actually | |
535 | * backed by ARC buffers. This includes arc_buf_hdr_t structures | |
536 | * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only | |
537 | * caches), and arc_buf_t structures (allocated via arc_buf_t | |
538 | * cache). | |
37fb3e43 | 539 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 | 540 | */ |
34dc7c2f | 541 | kstat_named_t arcstat_hdr_size; |
500445c0 PS |
542 | /* |
543 | * Number of bytes consumed by ARC buffers of type equal to | |
544 | * ARC_BUFC_DATA. This is generally consumed by buffers backing | |
545 | * on disk user data (e.g. plain file contents). | |
37fb3e43 | 546 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 | 547 | */ |
d164b209 | 548 | kstat_named_t arcstat_data_size; |
500445c0 PS |
549 | /* |
550 | * Number of bytes consumed by ARC buffers of type equal to | |
551 | * ARC_BUFC_METADATA. This is generally consumed by buffers | |
552 | * backing on disk data that is used for internal ZFS | |
553 | * structures (e.g. ZAP, dnode, indirect blocks, etc). | |
37fb3e43 | 554 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
555 | */ |
556 | kstat_named_t arcstat_metadata_size; | |
557 | /* | |
25458cbe | 558 | * Number of bytes consumed by dmu_buf_impl_t objects. |
37fb3e43 | 559 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 | 560 | */ |
25458cbe TC |
561 | kstat_named_t arcstat_dbuf_size; |
562 | /* | |
563 | * Number of bytes consumed by dnode_t objects. | |
37fb3e43 | 564 | * Not updated directly; only synced in arc_kstat_update. |
25458cbe TC |
565 | */ |
566 | kstat_named_t arcstat_dnode_size; | |
567 | /* | |
568 | * Number of bytes consumed by bonus buffers. | |
37fb3e43 | 569 | * Not updated directly; only synced in arc_kstat_update. |
25458cbe TC |
570 | */ |
571 | kstat_named_t arcstat_bonus_size; | |
500445c0 PS |
572 | /* |
573 | * Total number of bytes consumed by ARC buffers residing in the | |
574 | * arc_anon state. This includes *all* buffers in the arc_anon | |
575 | * state; e.g. data, metadata, evictable, and unevictable buffers | |
576 | * are all included in this value. | |
37fb3e43 | 577 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 | 578 | */ |
13be560d | 579 | kstat_named_t arcstat_anon_size; |
500445c0 PS |
580 | /* |
581 | * Number of bytes consumed by ARC buffers that meet the | |
582 | * following criteria: backing buffers of type ARC_BUFC_DATA, | |
583 | * residing in the arc_anon state, and are eligible for eviction | |
584 | * (e.g. have no outstanding holds on the buffer). | |
37fb3e43 | 585 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
586 | */ |
587 | kstat_named_t arcstat_anon_evictable_data; | |
588 | /* | |
589 | * Number of bytes consumed by ARC buffers that meet the | |
590 | * following criteria: backing buffers of type ARC_BUFC_METADATA, | |
591 | * residing in the arc_anon state, and are eligible for eviction | |
592 | * (e.g. have no outstanding holds on the buffer). | |
37fb3e43 | 593 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
594 | */ |
595 | kstat_named_t arcstat_anon_evictable_metadata; | |
596 | /* | |
597 | * Total number of bytes consumed by ARC buffers residing in the | |
598 | * arc_mru state. This includes *all* buffers in the arc_mru | |
599 | * state; e.g. data, metadata, evictable, and unevictable buffers | |
600 | * are all included in this value. | |
37fb3e43 | 601 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 | 602 | */ |
13be560d | 603 | kstat_named_t arcstat_mru_size; |
500445c0 PS |
604 | /* |
605 | * Number of bytes consumed by ARC buffers that meet the | |
606 | * following criteria: backing buffers of type ARC_BUFC_DATA, | |
607 | * residing in the arc_mru state, and are eligible for eviction | |
608 | * (e.g. have no outstanding holds on the buffer). | |
37fb3e43 | 609 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
610 | */ |
611 | kstat_named_t arcstat_mru_evictable_data; | |
612 | /* | |
613 | * Number of bytes consumed by ARC buffers that meet the | |
614 | * following criteria: backing buffers of type ARC_BUFC_METADATA, | |
615 | * residing in the arc_mru state, and are eligible for eviction | |
616 | * (e.g. have no outstanding holds on the buffer). | |
37fb3e43 | 617 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
618 | */ |
619 | kstat_named_t arcstat_mru_evictable_metadata; | |
620 | /* | |
621 | * Total number of bytes that *would have been* consumed by ARC | |
622 | * buffers in the arc_mru_ghost state. The key thing to note | |
623 | * here, is the fact that this size doesn't actually indicate | |
624 | * RAM consumption. The ghost lists only consist of headers and | |
625 | * don't actually have ARC buffers linked off of these headers. | |
626 | * Thus, *if* the headers had associated ARC buffers, these | |
627 | * buffers *would have* consumed this number of bytes. | |
37fb3e43 | 628 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 | 629 | */ |
13be560d | 630 | kstat_named_t arcstat_mru_ghost_size; |
500445c0 PS |
631 | /* |
632 | * Number of bytes that *would have been* consumed by ARC | |
633 | * buffers that are eligible for eviction, of type | |
634 | * ARC_BUFC_DATA, and linked off the arc_mru_ghost state. | |
37fb3e43 | 635 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
636 | */ |
637 | kstat_named_t arcstat_mru_ghost_evictable_data; | |
638 | /* | |
639 | * Number of bytes that *would have been* consumed by ARC | |
640 | * buffers that are eligible for eviction, of type | |
641 | * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. | |
37fb3e43 | 642 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
643 | */ |
644 | kstat_named_t arcstat_mru_ghost_evictable_metadata; | |
645 | /* | |
646 | * Total number of bytes consumed by ARC buffers residing in the | |
647 | * arc_mfu state. This includes *all* buffers in the arc_mfu | |
648 | * state; e.g. data, metadata, evictable, and unevictable buffers | |
649 | * are all included in this value. | |
37fb3e43 | 650 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 | 651 | */ |
13be560d | 652 | kstat_named_t arcstat_mfu_size; |
500445c0 PS |
653 | /* |
654 | * Number of bytes consumed by ARC buffers that are eligible for | |
655 | * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu | |
656 | * state. | |
37fb3e43 | 657 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
658 | */ |
659 | kstat_named_t arcstat_mfu_evictable_data; | |
660 | /* | |
661 | * Number of bytes consumed by ARC buffers that are eligible for | |
662 | * eviction, of type ARC_BUFC_METADATA, and reside in the | |
663 | * arc_mfu state. | |
37fb3e43 | 664 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
665 | */ |
666 | kstat_named_t arcstat_mfu_evictable_metadata; | |
667 | /* | |
668 | * Total number of bytes that *would have been* consumed by ARC | |
669 | * buffers in the arc_mfu_ghost state. See the comment above | |
670 | * arcstat_mru_ghost_size for more details. | |
37fb3e43 | 671 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 | 672 | */ |
13be560d | 673 | kstat_named_t arcstat_mfu_ghost_size; |
500445c0 PS |
674 | /* |
675 | * Number of bytes that *would have been* consumed by ARC | |
676 | * buffers that are eligible for eviction, of type | |
677 | * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state. | |
37fb3e43 | 678 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
679 | */ |
680 | kstat_named_t arcstat_mfu_ghost_evictable_data; | |
681 | /* | |
682 | * Number of bytes that *would have been* consumed by ARC | |
683 | * buffers that are eligible for eviction, of type | |
684 | * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. | |
37fb3e43 | 685 | * Not updated directly; only synced in arc_kstat_update. |
500445c0 PS |
686 | */ |
687 | kstat_named_t arcstat_mfu_ghost_evictable_metadata; | |
34dc7c2f BB |
688 | kstat_named_t arcstat_l2_hits; |
689 | kstat_named_t arcstat_l2_misses; | |
690 | kstat_named_t arcstat_l2_feeds; | |
691 | kstat_named_t arcstat_l2_rw_clash; | |
d164b209 BB |
692 | kstat_named_t arcstat_l2_read_bytes; |
693 | kstat_named_t arcstat_l2_write_bytes; | |
34dc7c2f BB |
694 | kstat_named_t arcstat_l2_writes_sent; |
695 | kstat_named_t arcstat_l2_writes_done; | |
696 | kstat_named_t arcstat_l2_writes_error; | |
ca0bf58d | 697 | kstat_named_t arcstat_l2_writes_lock_retry; |
34dc7c2f BB |
698 | kstat_named_t arcstat_l2_evict_lock_retry; |
699 | kstat_named_t arcstat_l2_evict_reading; | |
b9541d6b | 700 | kstat_named_t arcstat_l2_evict_l1cached; |
34dc7c2f BB |
701 | kstat_named_t arcstat_l2_free_on_write; |
702 | kstat_named_t arcstat_l2_abort_lowmem; | |
703 | kstat_named_t arcstat_l2_cksum_bad; | |
704 | kstat_named_t arcstat_l2_io_error; | |
01850391 AG |
705 | kstat_named_t arcstat_l2_lsize; |
706 | kstat_named_t arcstat_l2_psize; | |
37fb3e43 | 707 | /* Not updated directly; only synced in arc_kstat_update. */ |
34dc7c2f BB |
708 | kstat_named_t arcstat_l2_hdr_size; |
709 | kstat_named_t arcstat_memory_throttle_count; | |
7cb67b45 BB |
710 | kstat_named_t arcstat_memory_direct_count; |
711 | kstat_named_t arcstat_memory_indirect_count; | |
70f02287 BB |
712 | kstat_named_t arcstat_memory_all_bytes; |
713 | kstat_named_t arcstat_memory_free_bytes; | |
714 | kstat_named_t arcstat_memory_available_bytes; | |
1834f2d8 BB |
715 | kstat_named_t arcstat_no_grow; |
716 | kstat_named_t arcstat_tempreserve; | |
717 | kstat_named_t arcstat_loaned_bytes; | |
ab26409d | 718 | kstat_named_t arcstat_prune; |
37fb3e43 | 719 | /* Not updated directly; only synced in arc_kstat_update. */ |
1834f2d8 BB |
720 | kstat_named_t arcstat_meta_used; |
721 | kstat_named_t arcstat_meta_limit; | |
25458cbe | 722 | kstat_named_t arcstat_dnode_limit; |
1834f2d8 | 723 | kstat_named_t arcstat_meta_max; |
ca0bf58d | 724 | kstat_named_t arcstat_meta_min; |
a8b2e306 | 725 | kstat_named_t arcstat_async_upgrade_sync; |
7f60329a | 726 | kstat_named_t arcstat_demand_hit_predictive_prefetch; |
d4a72f23 | 727 | kstat_named_t arcstat_demand_hit_prescient_prefetch; |
11f552fa BB |
728 | kstat_named_t arcstat_need_free; |
729 | kstat_named_t arcstat_sys_free; | |
b5256303 | 730 | kstat_named_t arcstat_raw_size; |
34dc7c2f BB |
731 | } arc_stats_t; |
732 | ||
733 | static arc_stats_t arc_stats = { | |
734 | { "hits", KSTAT_DATA_UINT64 }, | |
735 | { "misses", KSTAT_DATA_UINT64 }, | |
736 | { "demand_data_hits", KSTAT_DATA_UINT64 }, | |
737 | { "demand_data_misses", KSTAT_DATA_UINT64 }, | |
738 | { "demand_metadata_hits", KSTAT_DATA_UINT64 }, | |
739 | { "demand_metadata_misses", KSTAT_DATA_UINT64 }, | |
740 | { "prefetch_data_hits", KSTAT_DATA_UINT64 }, | |
741 | { "prefetch_data_misses", KSTAT_DATA_UINT64 }, | |
742 | { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, | |
743 | { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, | |
744 | { "mru_hits", KSTAT_DATA_UINT64 }, | |
745 | { "mru_ghost_hits", KSTAT_DATA_UINT64 }, | |
746 | { "mfu_hits", KSTAT_DATA_UINT64 }, | |
747 | { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, | |
748 | { "deleted", KSTAT_DATA_UINT64 }, | |
34dc7c2f | 749 | { "mutex_miss", KSTAT_DATA_UINT64 }, |
0873bb63 | 750 | { "access_skip", KSTAT_DATA_UINT64 }, |
34dc7c2f | 751 | { "evict_skip", KSTAT_DATA_UINT64 }, |
ca0bf58d | 752 | { "evict_not_enough", KSTAT_DATA_UINT64 }, |
428870ff BB |
753 | { "evict_l2_cached", KSTAT_DATA_UINT64 }, |
754 | { "evict_l2_eligible", KSTAT_DATA_UINT64 }, | |
755 | { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, | |
ca0bf58d | 756 | { "evict_l2_skip", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
757 | { "hash_elements", KSTAT_DATA_UINT64 }, |
758 | { "hash_elements_max", KSTAT_DATA_UINT64 }, | |
759 | { "hash_collisions", KSTAT_DATA_UINT64 }, | |
760 | { "hash_chains", KSTAT_DATA_UINT64 }, | |
761 | { "hash_chain_max", KSTAT_DATA_UINT64 }, | |
762 | { "p", KSTAT_DATA_UINT64 }, | |
763 | { "c", KSTAT_DATA_UINT64 }, | |
764 | { "c_min", KSTAT_DATA_UINT64 }, | |
765 | { "c_max", KSTAT_DATA_UINT64 }, | |
766 | { "size", KSTAT_DATA_UINT64 }, | |
d3c2ae1c GW |
767 | { "compressed_size", KSTAT_DATA_UINT64 }, |
768 | { "uncompressed_size", KSTAT_DATA_UINT64 }, | |
769 | { "overhead_size", KSTAT_DATA_UINT64 }, | |
34dc7c2f | 770 | { "hdr_size", KSTAT_DATA_UINT64 }, |
d164b209 | 771 | { "data_size", KSTAT_DATA_UINT64 }, |
500445c0 | 772 | { "metadata_size", KSTAT_DATA_UINT64 }, |
25458cbe TC |
773 | { "dbuf_size", KSTAT_DATA_UINT64 }, |
774 | { "dnode_size", KSTAT_DATA_UINT64 }, | |
775 | { "bonus_size", KSTAT_DATA_UINT64 }, | |
13be560d | 776 | { "anon_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
777 | { "anon_evictable_data", KSTAT_DATA_UINT64 }, |
778 | { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 779 | { "mru_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
780 | { "mru_evictable_data", KSTAT_DATA_UINT64 }, |
781 | { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 782 | { "mru_ghost_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
783 | { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, |
784 | { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 785 | { "mfu_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
786 | { "mfu_evictable_data", KSTAT_DATA_UINT64 }, |
787 | { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 788 | { "mfu_ghost_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
789 | { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, |
790 | { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
791 | { "l2_hits", KSTAT_DATA_UINT64 }, |
792 | { "l2_misses", KSTAT_DATA_UINT64 }, | |
793 | { "l2_feeds", KSTAT_DATA_UINT64 }, | |
794 | { "l2_rw_clash", KSTAT_DATA_UINT64 }, | |
d164b209 BB |
795 | { "l2_read_bytes", KSTAT_DATA_UINT64 }, |
796 | { "l2_write_bytes", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
797 | { "l2_writes_sent", KSTAT_DATA_UINT64 }, |
798 | { "l2_writes_done", KSTAT_DATA_UINT64 }, | |
799 | { "l2_writes_error", KSTAT_DATA_UINT64 }, | |
ca0bf58d | 800 | { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
801 | { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, |
802 | { "l2_evict_reading", KSTAT_DATA_UINT64 }, | |
b9541d6b | 803 | { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
804 | { "l2_free_on_write", KSTAT_DATA_UINT64 }, |
805 | { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, | |
806 | { "l2_cksum_bad", KSTAT_DATA_UINT64 }, | |
807 | { "l2_io_error", KSTAT_DATA_UINT64 }, | |
808 | { "l2_size", KSTAT_DATA_UINT64 }, | |
3a17a7a9 | 809 | { "l2_asize", KSTAT_DATA_UINT64 }, |
34dc7c2f | 810 | { "l2_hdr_size", KSTAT_DATA_UINT64 }, |
1834f2d8 | 811 | { "memory_throttle_count", KSTAT_DATA_UINT64 }, |
7cb67b45 BB |
812 | { "memory_direct_count", KSTAT_DATA_UINT64 }, |
813 | { "memory_indirect_count", KSTAT_DATA_UINT64 }, | |
70f02287 BB |
814 | { "memory_all_bytes", KSTAT_DATA_UINT64 }, |
815 | { "memory_free_bytes", KSTAT_DATA_UINT64 }, | |
816 | { "memory_available_bytes", KSTAT_DATA_INT64 }, | |
1834f2d8 BB |
817 | { "arc_no_grow", KSTAT_DATA_UINT64 }, |
818 | { "arc_tempreserve", KSTAT_DATA_UINT64 }, | |
819 | { "arc_loaned_bytes", KSTAT_DATA_UINT64 }, | |
ab26409d | 820 | { "arc_prune", KSTAT_DATA_UINT64 }, |
1834f2d8 BB |
821 | { "arc_meta_used", KSTAT_DATA_UINT64 }, |
822 | { "arc_meta_limit", KSTAT_DATA_UINT64 }, | |
25458cbe | 823 | { "arc_dnode_limit", KSTAT_DATA_UINT64 }, |
1834f2d8 | 824 | { "arc_meta_max", KSTAT_DATA_UINT64 }, |
11f552fa | 825 | { "arc_meta_min", KSTAT_DATA_UINT64 }, |
a8b2e306 | 826 | { "async_upgrade_sync", KSTAT_DATA_UINT64 }, |
7f60329a | 827 | { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 }, |
d4a72f23 | 828 | { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64 }, |
11f552fa | 829 | { "arc_need_free", KSTAT_DATA_UINT64 }, |
b5256303 TC |
830 | { "arc_sys_free", KSTAT_DATA_UINT64 }, |
831 | { "arc_raw_size", KSTAT_DATA_UINT64 } | |
34dc7c2f BB |
832 | }; |
833 | ||
834 | #define ARCSTAT(stat) (arc_stats.stat.value.ui64) | |
835 | ||
836 | #define ARCSTAT_INCR(stat, val) \ | |
d3cc8b15 | 837 | atomic_add_64(&arc_stats.stat.value.ui64, (val)) |
34dc7c2f | 838 | |
428870ff | 839 | #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) |
34dc7c2f BB |
840 | #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) |
841 | ||
842 | #define ARCSTAT_MAX(stat, val) { \ | |
843 | uint64_t m; \ | |
844 | while ((val) > (m = arc_stats.stat.value.ui64) && \ | |
845 | (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ | |
846 | continue; \ | |
847 | } | |
848 | ||
849 | #define ARCSTAT_MAXSTAT(stat) \ | |
850 | ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) | |
851 | ||
852 | /* | |
853 | * We define a macro to allow ARC hits/misses to be easily broken down by | |
854 | * two separate conditions, giving a total of four different subtypes for | |
855 | * each of hits and misses (so eight statistics total). | |
856 | */ | |
857 | #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ | |
858 | if (cond1) { \ | |
859 | if (cond2) { \ | |
860 | ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ | |
861 | } else { \ | |
862 | ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ | |
863 | } \ | |
864 | } else { \ | |
865 | if (cond2) { \ | |
866 | ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ | |
867 | } else { \ | |
868 | ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ | |
869 | } \ | |
870 | } | |
871 | ||
872 | kstat_t *arc_ksp; | |
428870ff | 873 | static arc_state_t *arc_anon; |
34dc7c2f BB |
874 | static arc_state_t *arc_mru; |
875 | static arc_state_t *arc_mru_ghost; | |
876 | static arc_state_t *arc_mfu; | |
877 | static arc_state_t *arc_mfu_ghost; | |
878 | static arc_state_t *arc_l2c_only; | |
879 | ||
880 | /* | |
881 | * There are several ARC variables that are critical to export as kstats -- | |
882 | * but we don't want to have to grovel around in the kstat whenever we wish to | |
883 | * manipulate them. For these variables, we therefore define them to be in | |
884 | * terms of the statistic variable. This assures that we are not introducing | |
885 | * the possibility of inconsistency by having shadow copies of the variables, | |
886 | * while still allowing the code to be readable. | |
887 | */ | |
34dc7c2f BB |
888 | #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ |
889 | #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ | |
890 | #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ | |
891 | #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ | |
d3c2ae1c | 892 | #define arc_no_grow ARCSTAT(arcstat_no_grow) /* do not grow cache size */ |
1834f2d8 BB |
893 | #define arc_tempreserve ARCSTAT(arcstat_tempreserve) |
894 | #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes) | |
23c0a133 | 895 | #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ |
25458cbe | 896 | #define arc_dnode_limit ARCSTAT(arcstat_dnode_limit) /* max size for dnodes */ |
ca0bf58d | 897 | #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ |
23c0a133 | 898 | #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ |
11f552fa BB |
899 | #define arc_need_free ARCSTAT(arcstat_need_free) /* bytes to be freed */ |
900 | #define arc_sys_free ARCSTAT(arcstat_sys_free) /* target system free bytes */ | |
34dc7c2f | 901 | |
b5256303 TC |
902 | /* size of all b_rabd's in entire arc */ |
903 | #define arc_raw_size ARCSTAT(arcstat_raw_size) | |
d3c2ae1c GW |
904 | /* compressed size of entire arc */ |
905 | #define arc_compressed_size ARCSTAT(arcstat_compressed_size) | |
906 | /* uncompressed size of entire arc */ | |
907 | #define arc_uncompressed_size ARCSTAT(arcstat_uncompressed_size) | |
908 | /* number of bytes in the arc from arc_buf_t's */ | |
909 | #define arc_overhead_size ARCSTAT(arcstat_overhead_size) | |
3a17a7a9 | 910 | |
37fb3e43 PD |
911 | /* |
912 | * There are also some ARC variables that we want to export, but that are | |
913 | * updated so often that having the canonical representation be the statistic | |
914 | * variable causes a performance bottleneck. We want to use aggsum_t's for these | |
915 | * instead, but still be able to export the kstat in the same way as before. | |
916 | * The solution is to always use the aggsum version, except in the kstat update | |
917 | * callback. | |
918 | */ | |
919 | aggsum_t arc_size; | |
920 | aggsum_t arc_meta_used; | |
921 | aggsum_t astat_data_size; | |
922 | aggsum_t astat_metadata_size; | |
923 | aggsum_t astat_dbuf_size; | |
924 | aggsum_t astat_dnode_size; | |
925 | aggsum_t astat_bonus_size; | |
926 | aggsum_t astat_hdr_size; | |
927 | aggsum_t astat_l2_hdr_size; | |
928 | ||
3ec34e55 | 929 | static hrtime_t arc_growtime; |
ab26409d BB |
930 | static list_t arc_prune_list; |
931 | static kmutex_t arc_prune_mtx; | |
f6046738 | 932 | static taskq_t *arc_prune_taskq; |
428870ff | 933 | |
34dc7c2f BB |
934 | #define GHOST_STATE(state) \ |
935 | ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ | |
936 | (state) == arc_l2c_only) | |
937 | ||
2a432414 GW |
938 | #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) |
939 | #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) | |
940 | #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) | |
941 | #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) | |
d4a72f23 TC |
942 | #define HDR_PRESCIENT_PREFETCH(hdr) \ |
943 | ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH) | |
d3c2ae1c GW |
944 | #define HDR_COMPRESSION_ENABLED(hdr) \ |
945 | ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC) | |
b9541d6b | 946 | |
2a432414 GW |
947 | #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) |
948 | #define HDR_L2_READING(hdr) \ | |
d3c2ae1c GW |
949 | (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ |
950 | ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) | |
2a432414 GW |
951 | #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) |
952 | #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) | |
953 | #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) | |
b5256303 TC |
954 | #define HDR_PROTECTED(hdr) ((hdr)->b_flags & ARC_FLAG_PROTECTED) |
955 | #define HDR_NOAUTH(hdr) ((hdr)->b_flags & ARC_FLAG_NOAUTH) | |
d3c2ae1c | 956 | #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA) |
34dc7c2f | 957 | |
b9541d6b | 958 | #define HDR_ISTYPE_METADATA(hdr) \ |
d3c2ae1c | 959 | ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) |
b9541d6b CW |
960 | #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) |
961 | ||
962 | #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) | |
963 | #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) | |
b5256303 TC |
964 | #define HDR_HAS_RABD(hdr) \ |
965 | (HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) && \ | |
966 | (hdr)->b_crypt_hdr.b_rabd != NULL) | |
967 | #define HDR_ENCRYPTED(hdr) \ | |
968 | (HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot)) | |
969 | #define HDR_AUTHENTICATED(hdr) \ | |
970 | (HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot)) | |
b9541d6b | 971 | |
d3c2ae1c GW |
972 | /* For storing compression mode in b_flags */ |
973 | #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1) | |
974 | ||
975 | #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \ | |
976 | HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS)) | |
977 | #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \ | |
978 | HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp)); | |
979 | ||
980 | #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL) | |
524b4217 DK |
981 | #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED) |
982 | #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED) | |
b5256303 | 983 | #define ARC_BUF_ENCRYPTED(buf) ((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED) |
d3c2ae1c | 984 | |
34dc7c2f BB |
985 | /* |
986 | * Other sizes | |
987 | */ | |
988 | ||
b5256303 TC |
989 | #define HDR_FULL_CRYPT_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) |
990 | #define HDR_FULL_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_crypt_hdr)) | |
b9541d6b | 991 | #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) |
34dc7c2f BB |
992 | |
993 | /* | |
994 | * Hash table routines | |
995 | */ | |
996 | ||
00b46022 BB |
997 | #define HT_LOCK_ALIGN 64 |
998 | #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN))) | |
34dc7c2f BB |
999 | |
1000 | struct ht_lock { | |
1001 | kmutex_t ht_lock; | |
1002 | #ifdef _KERNEL | |
00b46022 | 1003 | unsigned char pad[HT_LOCK_PAD]; |
34dc7c2f BB |
1004 | #endif |
1005 | }; | |
1006 | ||
b31d8ea7 | 1007 | #define BUF_LOCKS 8192 |
34dc7c2f BB |
1008 | typedef struct buf_hash_table { |
1009 | uint64_t ht_mask; | |
1010 | arc_buf_hdr_t **ht_table; | |
1011 | struct ht_lock ht_locks[BUF_LOCKS]; | |
1012 | } buf_hash_table_t; | |
1013 | ||
1014 | static buf_hash_table_t buf_hash_table; | |
1015 | ||
1016 | #define BUF_HASH_INDEX(spa, dva, birth) \ | |
1017 | (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) | |
1018 | #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) | |
1019 | #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) | |
428870ff BB |
1020 | #define HDR_LOCK(hdr) \ |
1021 | (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) | |
34dc7c2f BB |
1022 | |
1023 | uint64_t zfs_crc64_table[256]; | |
1024 | ||
1025 | /* | |
1026 | * Level 2 ARC | |
1027 | */ | |
1028 | ||
1029 | #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ | |
3a17a7a9 | 1030 | #define L2ARC_HEADROOM 2 /* num of writes */ |
8a09d5fd | 1031 | |
3a17a7a9 SK |
1032 | /* |
1033 | * If we discover during ARC scan any buffers to be compressed, we boost | |
1034 | * our headroom for the next scanning cycle by this percentage multiple. | |
1035 | */ | |
1036 | #define L2ARC_HEADROOM_BOOST 200 | |
d164b209 BB |
1037 | #define L2ARC_FEED_SECS 1 /* caching interval secs */ |
1038 | #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ | |
34dc7c2f | 1039 | |
4aafab91 G |
1040 | /* |
1041 | * We can feed L2ARC from two states of ARC buffers, mru and mfu, | |
1042 | * and each of the state has two types: data and metadata. | |
1043 | */ | |
1044 | #define L2ARC_FEED_TYPES 4 | |
1045 | ||
34dc7c2f BB |
1046 | #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) |
1047 | #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) | |
1048 | ||
d3cc8b15 | 1049 | /* L2ARC Performance Tunables */ |
abd8610c BB |
1050 | unsigned long l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */ |
1051 | unsigned long l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */ | |
1052 | unsigned long l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */ | |
3a17a7a9 | 1053 | unsigned long l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; |
abd8610c BB |
1054 | unsigned long l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ |
1055 | unsigned long l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */ | |
1056 | int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ | |
1057 | int l2arc_feed_again = B_TRUE; /* turbo warmup */ | |
c93504f0 | 1058 | int l2arc_norw = B_FALSE; /* no reads during writes */ |
34dc7c2f BB |
1059 | |
1060 | /* | |
1061 | * L2ARC Internals | |
1062 | */ | |
34dc7c2f BB |
1063 | static list_t L2ARC_dev_list; /* device list */ |
1064 | static list_t *l2arc_dev_list; /* device list pointer */ | |
1065 | static kmutex_t l2arc_dev_mtx; /* device list mutex */ | |
1066 | static l2arc_dev_t *l2arc_dev_last; /* last device used */ | |
34dc7c2f BB |
1067 | static list_t L2ARC_free_on_write; /* free after write buf list */ |
1068 | static list_t *l2arc_free_on_write; /* free after write list ptr */ | |
1069 | static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ | |
1070 | static uint64_t l2arc_ndev; /* number of devices */ | |
1071 | ||
1072 | typedef struct l2arc_read_callback { | |
2aa34383 | 1073 | arc_buf_hdr_t *l2rcb_hdr; /* read header */ |
3a17a7a9 | 1074 | blkptr_t l2rcb_bp; /* original blkptr */ |
5dbd68a3 | 1075 | zbookmark_phys_t l2rcb_zb; /* original bookmark */ |
3a17a7a9 | 1076 | int l2rcb_flags; /* original flags */ |
82710e99 | 1077 | abd_t *l2rcb_abd; /* temporary buffer */ |
34dc7c2f BB |
1078 | } l2arc_read_callback_t; |
1079 | ||
34dc7c2f BB |
1080 | typedef struct l2arc_data_free { |
1081 | /* protected by l2arc_free_on_write_mtx */ | |
a6255b7f | 1082 | abd_t *l2df_abd; |
34dc7c2f | 1083 | size_t l2df_size; |
d3c2ae1c | 1084 | arc_buf_contents_t l2df_type; |
34dc7c2f BB |
1085 | list_node_t l2df_list_node; |
1086 | } l2arc_data_free_t; | |
1087 | ||
b5256303 TC |
1088 | typedef enum arc_fill_flags { |
1089 | ARC_FILL_LOCKED = 1 << 0, /* hdr lock is held */ | |
1090 | ARC_FILL_COMPRESSED = 1 << 1, /* fill with compressed data */ | |
1091 | ARC_FILL_ENCRYPTED = 1 << 2, /* fill with encrypted data */ | |
1092 | ARC_FILL_NOAUTH = 1 << 3, /* don't attempt to authenticate */ | |
1093 | ARC_FILL_IN_PLACE = 1 << 4 /* fill in place (special case) */ | |
1094 | } arc_fill_flags_t; | |
1095 | ||
34dc7c2f BB |
1096 | static kmutex_t l2arc_feed_thr_lock; |
1097 | static kcondvar_t l2arc_feed_thr_cv; | |
1098 | static uint8_t l2arc_thread_exit; | |
1099 | ||
a6255b7f | 1100 | static abd_t *arc_get_data_abd(arc_buf_hdr_t *, uint64_t, void *); |
d3c2ae1c | 1101 | static void *arc_get_data_buf(arc_buf_hdr_t *, uint64_t, void *); |
a6255b7f DQ |
1102 | static void arc_get_data_impl(arc_buf_hdr_t *, uint64_t, void *); |
1103 | static void arc_free_data_abd(arc_buf_hdr_t *, abd_t *, uint64_t, void *); | |
d3c2ae1c | 1104 | static void arc_free_data_buf(arc_buf_hdr_t *, void *, uint64_t, void *); |
a6255b7f | 1105 | static void arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag); |
b5256303 TC |
1106 | static void arc_hdr_free_abd(arc_buf_hdr_t *, boolean_t); |
1107 | static void arc_hdr_alloc_abd(arc_buf_hdr_t *, boolean_t); | |
2a432414 | 1108 | static void arc_access(arc_buf_hdr_t *, kmutex_t *); |
ca0bf58d | 1109 | static boolean_t arc_is_overflowing(void); |
2a432414 | 1110 | static void arc_buf_watch(arc_buf_t *); |
ca67b33a | 1111 | static void arc_tuning_update(void); |
25458cbe | 1112 | static void arc_prune_async(int64_t); |
9edb3695 | 1113 | static uint64_t arc_all_memory(void); |
2a432414 | 1114 | |
b9541d6b CW |
1115 | static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); |
1116 | static uint32_t arc_bufc_to_flags(arc_buf_contents_t); | |
d3c2ae1c GW |
1117 | static inline void arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); |
1118 | static inline void arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); | |
b9541d6b | 1119 | |
2a432414 GW |
1120 | static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); |
1121 | static void l2arc_read_done(zio_t *); | |
34dc7c2f | 1122 | |
37fb3e43 PD |
1123 | |
1124 | /* | |
1125 | * We use Cityhash for this. It's fast, and has good hash properties without | |
1126 | * requiring any large static buffers. | |
1127 | */ | |
34dc7c2f | 1128 | static uint64_t |
d164b209 | 1129 | buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) |
34dc7c2f | 1130 | { |
37fb3e43 | 1131 | return (cityhash4(spa, dva->dva_word[0], dva->dva_word[1], birth)); |
34dc7c2f BB |
1132 | } |
1133 | ||
d3c2ae1c GW |
1134 | #define HDR_EMPTY(hdr) \ |
1135 | ((hdr)->b_dva.dva_word[0] == 0 && \ | |
1136 | (hdr)->b_dva.dva_word[1] == 0) | |
34dc7c2f | 1137 | |
d3c2ae1c GW |
1138 | #define HDR_EQUAL(spa, dva, birth, hdr) \ |
1139 | ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ | |
1140 | ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ | |
1141 | ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa) | |
34dc7c2f | 1142 | |
428870ff BB |
1143 | static void |
1144 | buf_discard_identity(arc_buf_hdr_t *hdr) | |
1145 | { | |
1146 | hdr->b_dva.dva_word[0] = 0; | |
1147 | hdr->b_dva.dva_word[1] = 0; | |
1148 | hdr->b_birth = 0; | |
428870ff BB |
1149 | } |
1150 | ||
34dc7c2f | 1151 | static arc_buf_hdr_t * |
9b67f605 | 1152 | buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) |
34dc7c2f | 1153 | { |
9b67f605 MA |
1154 | const dva_t *dva = BP_IDENTITY(bp); |
1155 | uint64_t birth = BP_PHYSICAL_BIRTH(bp); | |
34dc7c2f BB |
1156 | uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); |
1157 | kmutex_t *hash_lock = BUF_HASH_LOCK(idx); | |
2a432414 | 1158 | arc_buf_hdr_t *hdr; |
34dc7c2f BB |
1159 | |
1160 | mutex_enter(hash_lock); | |
2a432414 GW |
1161 | for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; |
1162 | hdr = hdr->b_hash_next) { | |
d3c2ae1c | 1163 | if (HDR_EQUAL(spa, dva, birth, hdr)) { |
34dc7c2f | 1164 | *lockp = hash_lock; |
2a432414 | 1165 | return (hdr); |
34dc7c2f BB |
1166 | } |
1167 | } | |
1168 | mutex_exit(hash_lock); | |
1169 | *lockp = NULL; | |
1170 | return (NULL); | |
1171 | } | |
1172 | ||
1173 | /* | |
1174 | * Insert an entry into the hash table. If there is already an element | |
1175 | * equal to elem in the hash table, then the already existing element | |
1176 | * will be returned and the new element will not be inserted. | |
1177 | * Otherwise returns NULL. | |
b9541d6b | 1178 | * If lockp == NULL, the caller is assumed to already hold the hash lock. |
34dc7c2f BB |
1179 | */ |
1180 | static arc_buf_hdr_t * | |
2a432414 | 1181 | buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) |
34dc7c2f | 1182 | { |
2a432414 | 1183 | uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); |
34dc7c2f | 1184 | kmutex_t *hash_lock = BUF_HASH_LOCK(idx); |
2a432414 | 1185 | arc_buf_hdr_t *fhdr; |
34dc7c2f BB |
1186 | uint32_t i; |
1187 | ||
2a432414 GW |
1188 | ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); |
1189 | ASSERT(hdr->b_birth != 0); | |
1190 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); | |
b9541d6b CW |
1191 | |
1192 | if (lockp != NULL) { | |
1193 | *lockp = hash_lock; | |
1194 | mutex_enter(hash_lock); | |
1195 | } else { | |
1196 | ASSERT(MUTEX_HELD(hash_lock)); | |
1197 | } | |
1198 | ||
2a432414 GW |
1199 | for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; |
1200 | fhdr = fhdr->b_hash_next, i++) { | |
d3c2ae1c | 1201 | if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) |
2a432414 | 1202 | return (fhdr); |
34dc7c2f BB |
1203 | } |
1204 | ||
2a432414 GW |
1205 | hdr->b_hash_next = buf_hash_table.ht_table[idx]; |
1206 | buf_hash_table.ht_table[idx] = hdr; | |
d3c2ae1c | 1207 | arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
34dc7c2f BB |
1208 | |
1209 | /* collect some hash table performance data */ | |
1210 | if (i > 0) { | |
1211 | ARCSTAT_BUMP(arcstat_hash_collisions); | |
1212 | if (i == 1) | |
1213 | ARCSTAT_BUMP(arcstat_hash_chains); | |
1214 | ||
1215 | ARCSTAT_MAX(arcstat_hash_chain_max, i); | |
1216 | } | |
1217 | ||
1218 | ARCSTAT_BUMP(arcstat_hash_elements); | |
1219 | ARCSTAT_MAXSTAT(arcstat_hash_elements); | |
1220 | ||
1221 | return (NULL); | |
1222 | } | |
1223 | ||
1224 | static void | |
2a432414 | 1225 | buf_hash_remove(arc_buf_hdr_t *hdr) |
34dc7c2f | 1226 | { |
2a432414 GW |
1227 | arc_buf_hdr_t *fhdr, **hdrp; |
1228 | uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); | |
34dc7c2f BB |
1229 | |
1230 | ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); | |
2a432414 | 1231 | ASSERT(HDR_IN_HASH_TABLE(hdr)); |
34dc7c2f | 1232 | |
2a432414 GW |
1233 | hdrp = &buf_hash_table.ht_table[idx]; |
1234 | while ((fhdr = *hdrp) != hdr) { | |
d3c2ae1c | 1235 | ASSERT3P(fhdr, !=, NULL); |
2a432414 | 1236 | hdrp = &fhdr->b_hash_next; |
34dc7c2f | 1237 | } |
2a432414 GW |
1238 | *hdrp = hdr->b_hash_next; |
1239 | hdr->b_hash_next = NULL; | |
d3c2ae1c | 1240 | arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
34dc7c2f BB |
1241 | |
1242 | /* collect some hash table performance data */ | |
1243 | ARCSTAT_BUMPDOWN(arcstat_hash_elements); | |
1244 | ||
1245 | if (buf_hash_table.ht_table[idx] && | |
1246 | buf_hash_table.ht_table[idx]->b_hash_next == NULL) | |
1247 | ARCSTAT_BUMPDOWN(arcstat_hash_chains); | |
1248 | } | |
1249 | ||
1250 | /* | |
1251 | * Global data structures and functions for the buf kmem cache. | |
1252 | */ | |
b5256303 | 1253 | |
b9541d6b | 1254 | static kmem_cache_t *hdr_full_cache; |
b5256303 | 1255 | static kmem_cache_t *hdr_full_crypt_cache; |
b9541d6b | 1256 | static kmem_cache_t *hdr_l2only_cache; |
34dc7c2f BB |
1257 | static kmem_cache_t *buf_cache; |
1258 | ||
1259 | static void | |
1260 | buf_fini(void) | |
1261 | { | |
1262 | int i; | |
1263 | ||
93ce2b4c | 1264 | #if defined(_KERNEL) |
d1d7e268 MK |
1265 | /* |
1266 | * Large allocations which do not require contiguous pages | |
1267 | * should be using vmem_free() in the linux kernel\ | |
1268 | */ | |
00b46022 BB |
1269 | vmem_free(buf_hash_table.ht_table, |
1270 | (buf_hash_table.ht_mask + 1) * sizeof (void *)); | |
1271 | #else | |
34dc7c2f BB |
1272 | kmem_free(buf_hash_table.ht_table, |
1273 | (buf_hash_table.ht_mask + 1) * sizeof (void *)); | |
00b46022 | 1274 | #endif |
34dc7c2f BB |
1275 | for (i = 0; i < BUF_LOCKS; i++) |
1276 | mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); | |
b9541d6b | 1277 | kmem_cache_destroy(hdr_full_cache); |
b5256303 | 1278 | kmem_cache_destroy(hdr_full_crypt_cache); |
b9541d6b | 1279 | kmem_cache_destroy(hdr_l2only_cache); |
34dc7c2f BB |
1280 | kmem_cache_destroy(buf_cache); |
1281 | } | |
1282 | ||
1283 | /* | |
1284 | * Constructor callback - called when the cache is empty | |
1285 | * and a new buf is requested. | |
1286 | */ | |
1287 | /* ARGSUSED */ | |
1288 | static int | |
b9541d6b CW |
1289 | hdr_full_cons(void *vbuf, void *unused, int kmflag) |
1290 | { | |
1291 | arc_buf_hdr_t *hdr = vbuf; | |
1292 | ||
1293 | bzero(hdr, HDR_FULL_SIZE); | |
ae76f45c | 1294 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; |
b9541d6b | 1295 | cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); |
424fd7c3 | 1296 | zfs_refcount_create(&hdr->b_l1hdr.b_refcnt); |
b9541d6b CW |
1297 | mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); |
1298 | list_link_init(&hdr->b_l1hdr.b_arc_node); | |
1299 | list_link_init(&hdr->b_l2hdr.b_l2node); | |
ca0bf58d | 1300 | multilist_link_init(&hdr->b_l1hdr.b_arc_node); |
b9541d6b CW |
1301 | arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); |
1302 | ||
1303 | return (0); | |
1304 | } | |
1305 | ||
b5256303 TC |
1306 | /* ARGSUSED */ |
1307 | static int | |
1308 | hdr_full_crypt_cons(void *vbuf, void *unused, int kmflag) | |
1309 | { | |
1310 | arc_buf_hdr_t *hdr = vbuf; | |
1311 | ||
1312 | hdr_full_cons(vbuf, unused, kmflag); | |
1313 | bzero(&hdr->b_crypt_hdr, sizeof (hdr->b_crypt_hdr)); | |
1314 | arc_space_consume(sizeof (hdr->b_crypt_hdr), ARC_SPACE_HDRS); | |
1315 | ||
1316 | return (0); | |
1317 | } | |
1318 | ||
b9541d6b CW |
1319 | /* ARGSUSED */ |
1320 | static int | |
1321 | hdr_l2only_cons(void *vbuf, void *unused, int kmflag) | |
34dc7c2f | 1322 | { |
2a432414 GW |
1323 | arc_buf_hdr_t *hdr = vbuf; |
1324 | ||
b9541d6b CW |
1325 | bzero(hdr, HDR_L2ONLY_SIZE); |
1326 | arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); | |
34dc7c2f | 1327 | |
34dc7c2f BB |
1328 | return (0); |
1329 | } | |
1330 | ||
b128c09f BB |
1331 | /* ARGSUSED */ |
1332 | static int | |
1333 | buf_cons(void *vbuf, void *unused, int kmflag) | |
1334 | { | |
1335 | arc_buf_t *buf = vbuf; | |
1336 | ||
1337 | bzero(buf, sizeof (arc_buf_t)); | |
428870ff | 1338 | mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); |
d164b209 BB |
1339 | arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); |
1340 | ||
b128c09f BB |
1341 | return (0); |
1342 | } | |
1343 | ||
34dc7c2f BB |
1344 | /* |
1345 | * Destructor callback - called when a cached buf is | |
1346 | * no longer required. | |
1347 | */ | |
1348 | /* ARGSUSED */ | |
1349 | static void | |
b9541d6b | 1350 | hdr_full_dest(void *vbuf, void *unused) |
34dc7c2f | 1351 | { |
2a432414 | 1352 | arc_buf_hdr_t *hdr = vbuf; |
34dc7c2f | 1353 | |
d3c2ae1c | 1354 | ASSERT(HDR_EMPTY(hdr)); |
b9541d6b | 1355 | cv_destroy(&hdr->b_l1hdr.b_cv); |
424fd7c3 | 1356 | zfs_refcount_destroy(&hdr->b_l1hdr.b_refcnt); |
b9541d6b | 1357 | mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); |
ca0bf58d | 1358 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b CW |
1359 | arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); |
1360 | } | |
1361 | ||
b5256303 TC |
1362 | /* ARGSUSED */ |
1363 | static void | |
1364 | hdr_full_crypt_dest(void *vbuf, void *unused) | |
1365 | { | |
1366 | arc_buf_hdr_t *hdr = vbuf; | |
1367 | ||
1368 | hdr_full_dest(vbuf, unused); | |
1369 | arc_space_return(sizeof (hdr->b_crypt_hdr), ARC_SPACE_HDRS); | |
1370 | } | |
1371 | ||
b9541d6b CW |
1372 | /* ARGSUSED */ |
1373 | static void | |
1374 | hdr_l2only_dest(void *vbuf, void *unused) | |
1375 | { | |
1376 | ASSERTV(arc_buf_hdr_t *hdr = vbuf); | |
1377 | ||
d3c2ae1c | 1378 | ASSERT(HDR_EMPTY(hdr)); |
b9541d6b | 1379 | arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); |
34dc7c2f BB |
1380 | } |
1381 | ||
b128c09f BB |
1382 | /* ARGSUSED */ |
1383 | static void | |
1384 | buf_dest(void *vbuf, void *unused) | |
1385 | { | |
1386 | arc_buf_t *buf = vbuf; | |
1387 | ||
428870ff | 1388 | mutex_destroy(&buf->b_evict_lock); |
d164b209 | 1389 | arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); |
b128c09f BB |
1390 | } |
1391 | ||
8c8af9d8 BB |
1392 | /* |
1393 | * Reclaim callback -- invoked when memory is low. | |
1394 | */ | |
1395 | /* ARGSUSED */ | |
1396 | static void | |
1397 | hdr_recl(void *unused) | |
1398 | { | |
1399 | dprintf("hdr_recl called\n"); | |
1400 | /* | |
1401 | * umem calls the reclaim func when we destroy the buf cache, | |
1402 | * which is after we do arc_fini(). | |
1403 | */ | |
3ec34e55 BL |
1404 | if (arc_initialized) |
1405 | zthr_wakeup(arc_reap_zthr); | |
8c8af9d8 BB |
1406 | } |
1407 | ||
34dc7c2f BB |
1408 | static void |
1409 | buf_init(void) | |
1410 | { | |
2db28197 | 1411 | uint64_t *ct = NULL; |
34dc7c2f BB |
1412 | uint64_t hsize = 1ULL << 12; |
1413 | int i, j; | |
1414 | ||
1415 | /* | |
1416 | * The hash table is big enough to fill all of physical memory | |
49ddb315 MA |
1417 | * with an average block size of zfs_arc_average_blocksize (default 8K). |
1418 | * By default, the table will take up | |
1419 | * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). | |
34dc7c2f | 1420 | */ |
9edb3695 | 1421 | while (hsize * zfs_arc_average_blocksize < arc_all_memory()) |
34dc7c2f BB |
1422 | hsize <<= 1; |
1423 | retry: | |
1424 | buf_hash_table.ht_mask = hsize - 1; | |
93ce2b4c | 1425 | #if defined(_KERNEL) |
d1d7e268 MK |
1426 | /* |
1427 | * Large allocations which do not require contiguous pages | |
1428 | * should be using vmem_alloc() in the linux kernel | |
1429 | */ | |
00b46022 BB |
1430 | buf_hash_table.ht_table = |
1431 | vmem_zalloc(hsize * sizeof (void*), KM_SLEEP); | |
1432 | #else | |
34dc7c2f BB |
1433 | buf_hash_table.ht_table = |
1434 | kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); | |
00b46022 | 1435 | #endif |
34dc7c2f BB |
1436 | if (buf_hash_table.ht_table == NULL) { |
1437 | ASSERT(hsize > (1ULL << 8)); | |
1438 | hsize >>= 1; | |
1439 | goto retry; | |
1440 | } | |
1441 | ||
b9541d6b | 1442 | hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, |
8c8af9d8 | 1443 | 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0); |
b5256303 TC |
1444 | hdr_full_crypt_cache = kmem_cache_create("arc_buf_hdr_t_full_crypt", |
1445 | HDR_FULL_CRYPT_SIZE, 0, hdr_full_crypt_cons, hdr_full_crypt_dest, | |
1446 | hdr_recl, NULL, NULL, 0); | |
b9541d6b | 1447 | hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", |
8c8af9d8 | 1448 | HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl, |
b9541d6b | 1449 | NULL, NULL, 0); |
34dc7c2f | 1450 | buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), |
b128c09f | 1451 | 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); |
34dc7c2f BB |
1452 | |
1453 | for (i = 0; i < 256; i++) | |
1454 | for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) | |
1455 | *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); | |
1456 | ||
1457 | for (i = 0; i < BUF_LOCKS; i++) { | |
1458 | mutex_init(&buf_hash_table.ht_locks[i].ht_lock, | |
40d06e3c | 1459 | NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
1460 | } |
1461 | } | |
1462 | ||
d3c2ae1c | 1463 | #define ARC_MINTIME (hz>>4) /* 62 ms */ |
ca0bf58d | 1464 | |
2aa34383 DK |
1465 | /* |
1466 | * This is the size that the buf occupies in memory. If the buf is compressed, | |
1467 | * it will correspond to the compressed size. You should use this method of | |
1468 | * getting the buf size unless you explicitly need the logical size. | |
1469 | */ | |
1470 | uint64_t | |
1471 | arc_buf_size(arc_buf_t *buf) | |
1472 | { | |
1473 | return (ARC_BUF_COMPRESSED(buf) ? | |
1474 | HDR_GET_PSIZE(buf->b_hdr) : HDR_GET_LSIZE(buf->b_hdr)); | |
1475 | } | |
1476 | ||
1477 | uint64_t | |
1478 | arc_buf_lsize(arc_buf_t *buf) | |
1479 | { | |
1480 | return (HDR_GET_LSIZE(buf->b_hdr)); | |
1481 | } | |
1482 | ||
b5256303 TC |
1483 | /* |
1484 | * This function will return B_TRUE if the buffer is encrypted in memory. | |
1485 | * This buffer can be decrypted by calling arc_untransform(). | |
1486 | */ | |
1487 | boolean_t | |
1488 | arc_is_encrypted(arc_buf_t *buf) | |
1489 | { | |
1490 | return (ARC_BUF_ENCRYPTED(buf) != 0); | |
1491 | } | |
1492 | ||
1493 | /* | |
1494 | * Returns B_TRUE if the buffer represents data that has not had its MAC | |
1495 | * verified yet. | |
1496 | */ | |
1497 | boolean_t | |
1498 | arc_is_unauthenticated(arc_buf_t *buf) | |
1499 | { | |
1500 | return (HDR_NOAUTH(buf->b_hdr) != 0); | |
1501 | } | |
1502 | ||
1503 | void | |
1504 | arc_get_raw_params(arc_buf_t *buf, boolean_t *byteorder, uint8_t *salt, | |
1505 | uint8_t *iv, uint8_t *mac) | |
1506 | { | |
1507 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
1508 | ||
1509 | ASSERT(HDR_PROTECTED(hdr)); | |
1510 | ||
1511 | bcopy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN); | |
1512 | bcopy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN); | |
1513 | bcopy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN); | |
1514 | *byteorder = (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ? | |
1515 | ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER; | |
1516 | } | |
1517 | ||
1518 | /* | |
1519 | * Indicates how this buffer is compressed in memory. If it is not compressed | |
1520 | * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with | |
1521 | * arc_untransform() as long as it is also unencrypted. | |
1522 | */ | |
2aa34383 DK |
1523 | enum zio_compress |
1524 | arc_get_compression(arc_buf_t *buf) | |
1525 | { | |
1526 | return (ARC_BUF_COMPRESSED(buf) ? | |
1527 | HDR_GET_COMPRESS(buf->b_hdr) : ZIO_COMPRESS_OFF); | |
1528 | } | |
1529 | ||
b5256303 TC |
1530 | /* |
1531 | * Return the compression algorithm used to store this data in the ARC. If ARC | |
1532 | * compression is enabled or this is an encrypted block, this will be the same | |
1533 | * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF. | |
1534 | */ | |
1535 | static inline enum zio_compress | |
1536 | arc_hdr_get_compress(arc_buf_hdr_t *hdr) | |
1537 | { | |
1538 | return (HDR_COMPRESSION_ENABLED(hdr) ? | |
1539 | HDR_GET_COMPRESS(hdr) : ZIO_COMPRESS_OFF); | |
1540 | } | |
1541 | ||
d3c2ae1c GW |
1542 | static inline boolean_t |
1543 | arc_buf_is_shared(arc_buf_t *buf) | |
1544 | { | |
1545 | boolean_t shared = (buf->b_data != NULL && | |
a6255b7f DQ |
1546 | buf->b_hdr->b_l1hdr.b_pabd != NULL && |
1547 | abd_is_linear(buf->b_hdr->b_l1hdr.b_pabd) && | |
1548 | buf->b_data == abd_to_buf(buf->b_hdr->b_l1hdr.b_pabd)); | |
d3c2ae1c | 1549 | IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr)); |
2aa34383 DK |
1550 | IMPLY(shared, ARC_BUF_SHARED(buf)); |
1551 | IMPLY(shared, ARC_BUF_COMPRESSED(buf) || ARC_BUF_LAST(buf)); | |
524b4217 DK |
1552 | |
1553 | /* | |
1554 | * It would be nice to assert arc_can_share() too, but the "hdr isn't | |
1555 | * already being shared" requirement prevents us from doing that. | |
1556 | */ | |
1557 | ||
d3c2ae1c GW |
1558 | return (shared); |
1559 | } | |
ca0bf58d | 1560 | |
a7004725 DK |
1561 | /* |
1562 | * Free the checksum associated with this header. If there is no checksum, this | |
1563 | * is a no-op. | |
1564 | */ | |
d3c2ae1c GW |
1565 | static inline void |
1566 | arc_cksum_free(arc_buf_hdr_t *hdr) | |
1567 | { | |
1568 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
b5256303 | 1569 | |
d3c2ae1c GW |
1570 | mutex_enter(&hdr->b_l1hdr.b_freeze_lock); |
1571 | if (hdr->b_l1hdr.b_freeze_cksum != NULL) { | |
1572 | kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t)); | |
1573 | hdr->b_l1hdr.b_freeze_cksum = NULL; | |
b9541d6b | 1574 | } |
d3c2ae1c | 1575 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
b9541d6b CW |
1576 | } |
1577 | ||
a7004725 DK |
1578 | /* |
1579 | * Return true iff at least one of the bufs on hdr is not compressed. | |
b5256303 | 1580 | * Encrypted buffers count as compressed. |
a7004725 DK |
1581 | */ |
1582 | static boolean_t | |
1583 | arc_hdr_has_uncompressed_buf(arc_buf_hdr_t *hdr) | |
1584 | { | |
149ce888 TC |
1585 | ASSERT(hdr->b_l1hdr.b_state == arc_anon || |
1586 | MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); | |
1587 | ||
a7004725 DK |
1588 | for (arc_buf_t *b = hdr->b_l1hdr.b_buf; b != NULL; b = b->b_next) { |
1589 | if (!ARC_BUF_COMPRESSED(b)) { | |
1590 | return (B_TRUE); | |
1591 | } | |
1592 | } | |
1593 | return (B_FALSE); | |
1594 | } | |
1595 | ||
1596 | ||
524b4217 DK |
1597 | /* |
1598 | * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data | |
1599 | * matches the checksum that is stored in the hdr. If there is no checksum, | |
1600 | * or if the buf is compressed, this is a no-op. | |
1601 | */ | |
34dc7c2f BB |
1602 | static void |
1603 | arc_cksum_verify(arc_buf_t *buf) | |
1604 | { | |
d3c2ae1c | 1605 | arc_buf_hdr_t *hdr = buf->b_hdr; |
34dc7c2f BB |
1606 | zio_cksum_t zc; |
1607 | ||
1608 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
1609 | return; | |
1610 | ||
149ce888 | 1611 | if (ARC_BUF_COMPRESSED(buf)) |
524b4217 | 1612 | return; |
524b4217 | 1613 | |
d3c2ae1c GW |
1614 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1615 | ||
1616 | mutex_enter(&hdr->b_l1hdr.b_freeze_lock); | |
149ce888 | 1617 | |
d3c2ae1c GW |
1618 | if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) { |
1619 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); | |
34dc7c2f BB |
1620 | return; |
1621 | } | |
2aa34383 | 1622 | |
3c67d83a | 1623 | fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, &zc); |
d3c2ae1c | 1624 | if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc)) |
34dc7c2f | 1625 | panic("buffer modified while frozen!"); |
d3c2ae1c | 1626 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1627 | } |
1628 | ||
b5256303 TC |
1629 | /* |
1630 | * This function makes the assumption that data stored in the L2ARC | |
1631 | * will be transformed exactly as it is in the main pool. Because of | |
1632 | * this we can verify the checksum against the reading process's bp. | |
1633 | */ | |
d3c2ae1c GW |
1634 | static boolean_t |
1635 | arc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio) | |
34dc7c2f | 1636 | { |
d3c2ae1c GW |
1637 | ASSERT(!BP_IS_EMBEDDED(zio->io_bp)); |
1638 | VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr)); | |
34dc7c2f | 1639 | |
d3c2ae1c GW |
1640 | /* |
1641 | * Block pointers always store the checksum for the logical data. | |
1642 | * If the block pointer has the gang bit set, then the checksum | |
1643 | * it represents is for the reconstituted data and not for an | |
1644 | * individual gang member. The zio pipeline, however, must be able to | |
1645 | * determine the checksum of each of the gang constituents so it | |
1646 | * treats the checksum comparison differently than what we need | |
1647 | * for l2arc blocks. This prevents us from using the | |
1648 | * zio_checksum_error() interface directly. Instead we must call the | |
1649 | * zio_checksum_error_impl() so that we can ensure the checksum is | |
1650 | * generated using the correct checksum algorithm and accounts for the | |
1651 | * logical I/O size and not just a gang fragment. | |
1652 | */ | |
b5256303 | 1653 | return (zio_checksum_error_impl(zio->io_spa, zio->io_bp, |
a6255b7f | 1654 | BP_GET_CHECKSUM(zio->io_bp), zio->io_abd, zio->io_size, |
d3c2ae1c | 1655 | zio->io_offset, NULL) == 0); |
34dc7c2f BB |
1656 | } |
1657 | ||
524b4217 DK |
1658 | /* |
1659 | * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a | |
1660 | * checksum and attaches it to the buf's hdr so that we can ensure that the buf | |
1661 | * isn't modified later on. If buf is compressed or there is already a checksum | |
1662 | * on the hdr, this is a no-op (we only checksum uncompressed bufs). | |
1663 | */ | |
34dc7c2f | 1664 | static void |
d3c2ae1c | 1665 | arc_cksum_compute(arc_buf_t *buf) |
34dc7c2f | 1666 | { |
d3c2ae1c GW |
1667 | arc_buf_hdr_t *hdr = buf->b_hdr; |
1668 | ||
1669 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
34dc7c2f BB |
1670 | return; |
1671 | ||
d3c2ae1c | 1672 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2aa34383 | 1673 | |
b9541d6b | 1674 | mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
149ce888 | 1675 | if (hdr->b_l1hdr.b_freeze_cksum != NULL || ARC_BUF_COMPRESSED(buf)) { |
d3c2ae1c | 1676 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1677 | return; |
1678 | } | |
2aa34383 | 1679 | |
b5256303 | 1680 | ASSERT(!ARC_BUF_ENCRYPTED(buf)); |
2aa34383 | 1681 | ASSERT(!ARC_BUF_COMPRESSED(buf)); |
d3c2ae1c GW |
1682 | hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), |
1683 | KM_SLEEP); | |
3c67d83a | 1684 | fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, |
d3c2ae1c GW |
1685 | hdr->b_l1hdr.b_freeze_cksum); |
1686 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); | |
498877ba MA |
1687 | arc_buf_watch(buf); |
1688 | } | |
1689 | ||
1690 | #ifndef _KERNEL | |
1691 | void | |
1692 | arc_buf_sigsegv(int sig, siginfo_t *si, void *unused) | |
1693 | { | |
02730c33 | 1694 | panic("Got SIGSEGV at address: 0x%lx\n", (long)si->si_addr); |
498877ba MA |
1695 | } |
1696 | #endif | |
1697 | ||
1698 | /* ARGSUSED */ | |
1699 | static void | |
1700 | arc_buf_unwatch(arc_buf_t *buf) | |
1701 | { | |
1702 | #ifndef _KERNEL | |
1703 | if (arc_watch) { | |
a7004725 | 1704 | ASSERT0(mprotect(buf->b_data, arc_buf_size(buf), |
498877ba MA |
1705 | PROT_READ | PROT_WRITE)); |
1706 | } | |
1707 | #endif | |
1708 | } | |
1709 | ||
1710 | /* ARGSUSED */ | |
1711 | static void | |
1712 | arc_buf_watch(arc_buf_t *buf) | |
1713 | { | |
1714 | #ifndef _KERNEL | |
1715 | if (arc_watch) | |
2aa34383 | 1716 | ASSERT0(mprotect(buf->b_data, arc_buf_size(buf), |
d3c2ae1c | 1717 | PROT_READ)); |
498877ba | 1718 | #endif |
34dc7c2f BB |
1719 | } |
1720 | ||
b9541d6b CW |
1721 | static arc_buf_contents_t |
1722 | arc_buf_type(arc_buf_hdr_t *hdr) | |
1723 | { | |
d3c2ae1c | 1724 | arc_buf_contents_t type; |
b9541d6b | 1725 | if (HDR_ISTYPE_METADATA(hdr)) { |
d3c2ae1c | 1726 | type = ARC_BUFC_METADATA; |
b9541d6b | 1727 | } else { |
d3c2ae1c | 1728 | type = ARC_BUFC_DATA; |
b9541d6b | 1729 | } |
d3c2ae1c GW |
1730 | VERIFY3U(hdr->b_type, ==, type); |
1731 | return (type); | |
b9541d6b CW |
1732 | } |
1733 | ||
2aa34383 DK |
1734 | boolean_t |
1735 | arc_is_metadata(arc_buf_t *buf) | |
1736 | { | |
1737 | return (HDR_ISTYPE_METADATA(buf->b_hdr) != 0); | |
1738 | } | |
1739 | ||
b9541d6b CW |
1740 | static uint32_t |
1741 | arc_bufc_to_flags(arc_buf_contents_t type) | |
1742 | { | |
1743 | switch (type) { | |
1744 | case ARC_BUFC_DATA: | |
1745 | /* metadata field is 0 if buffer contains normal data */ | |
1746 | return (0); | |
1747 | case ARC_BUFC_METADATA: | |
1748 | return (ARC_FLAG_BUFC_METADATA); | |
1749 | default: | |
1750 | break; | |
1751 | } | |
1752 | panic("undefined ARC buffer type!"); | |
1753 | return ((uint32_t)-1); | |
1754 | } | |
1755 | ||
34dc7c2f BB |
1756 | void |
1757 | arc_buf_thaw(arc_buf_t *buf) | |
1758 | { | |
d3c2ae1c GW |
1759 | arc_buf_hdr_t *hdr = buf->b_hdr; |
1760 | ||
2aa34383 DK |
1761 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
1762 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
1763 | ||
524b4217 | 1764 | arc_cksum_verify(buf); |
34dc7c2f | 1765 | |
2aa34383 | 1766 | /* |
149ce888 | 1767 | * Compressed buffers do not manipulate the b_freeze_cksum. |
2aa34383 | 1768 | */ |
149ce888 | 1769 | if (ARC_BUF_COMPRESSED(buf)) |
2aa34383 | 1770 | return; |
2aa34383 | 1771 | |
d3c2ae1c GW |
1772 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1773 | arc_cksum_free(hdr); | |
498877ba | 1774 | arc_buf_unwatch(buf); |
34dc7c2f BB |
1775 | } |
1776 | ||
1777 | void | |
1778 | arc_buf_freeze(arc_buf_t *buf) | |
1779 | { | |
1780 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
1781 | return; | |
1782 | ||
149ce888 | 1783 | if (ARC_BUF_COMPRESSED(buf)) |
2aa34383 | 1784 | return; |
428870ff | 1785 | |
149ce888 | 1786 | ASSERT(HDR_HAS_L1HDR(buf->b_hdr)); |
d3c2ae1c | 1787 | arc_cksum_compute(buf); |
34dc7c2f BB |
1788 | } |
1789 | ||
d3c2ae1c GW |
1790 | /* |
1791 | * The arc_buf_hdr_t's b_flags should never be modified directly. Instead, | |
1792 | * the following functions should be used to ensure that the flags are | |
1793 | * updated in a thread-safe way. When manipulating the flags either | |
1794 | * the hash_lock must be held or the hdr must be undiscoverable. This | |
1795 | * ensures that we're not racing with any other threads when updating | |
1796 | * the flags. | |
1797 | */ | |
1798 | static inline void | |
1799 | arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) | |
1800 | { | |
1801 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); | |
1802 | hdr->b_flags |= flags; | |
1803 | } | |
1804 | ||
1805 | static inline void | |
1806 | arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) | |
1807 | { | |
1808 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); | |
1809 | hdr->b_flags &= ~flags; | |
1810 | } | |
1811 | ||
1812 | /* | |
1813 | * Setting the compression bits in the arc_buf_hdr_t's b_flags is | |
1814 | * done in a special way since we have to clear and set bits | |
1815 | * at the same time. Consumers that wish to set the compression bits | |
1816 | * must use this function to ensure that the flags are updated in | |
1817 | * thread-safe manner. | |
1818 | */ | |
1819 | static void | |
1820 | arc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp) | |
1821 | { | |
1822 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); | |
1823 | ||
1824 | /* | |
1825 | * Holes and embedded blocks will always have a psize = 0 so | |
1826 | * we ignore the compression of the blkptr and set the | |
d3c2ae1c GW |
1827 | * want to uncompress them. Mark them as uncompressed. |
1828 | */ | |
1829 | if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) { | |
1830 | arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC); | |
d3c2ae1c | 1831 | ASSERT(!HDR_COMPRESSION_ENABLED(hdr)); |
d3c2ae1c GW |
1832 | } else { |
1833 | arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC); | |
d3c2ae1c GW |
1834 | ASSERT(HDR_COMPRESSION_ENABLED(hdr)); |
1835 | } | |
b5256303 TC |
1836 | |
1837 | HDR_SET_COMPRESS(hdr, cmp); | |
1838 | ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp); | |
d3c2ae1c GW |
1839 | } |
1840 | ||
524b4217 DK |
1841 | /* |
1842 | * Looks for another buf on the same hdr which has the data decompressed, copies | |
1843 | * from it, and returns true. If no such buf exists, returns false. | |
1844 | */ | |
1845 | static boolean_t | |
1846 | arc_buf_try_copy_decompressed_data(arc_buf_t *buf) | |
1847 | { | |
1848 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
524b4217 DK |
1849 | boolean_t copied = B_FALSE; |
1850 | ||
1851 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
1852 | ASSERT3P(buf->b_data, !=, NULL); | |
1853 | ASSERT(!ARC_BUF_COMPRESSED(buf)); | |
1854 | ||
a7004725 | 1855 | for (arc_buf_t *from = hdr->b_l1hdr.b_buf; from != NULL; |
524b4217 DK |
1856 | from = from->b_next) { |
1857 | /* can't use our own data buffer */ | |
1858 | if (from == buf) { | |
1859 | continue; | |
1860 | } | |
1861 | ||
1862 | if (!ARC_BUF_COMPRESSED(from)) { | |
1863 | bcopy(from->b_data, buf->b_data, arc_buf_size(buf)); | |
1864 | copied = B_TRUE; | |
1865 | break; | |
1866 | } | |
1867 | } | |
1868 | ||
1869 | /* | |
1870 | * There were no decompressed bufs, so there should not be a | |
1871 | * checksum on the hdr either. | |
1872 | */ | |
1873 | EQUIV(!copied, hdr->b_l1hdr.b_freeze_cksum == NULL); | |
1874 | ||
1875 | return (copied); | |
1876 | } | |
1877 | ||
b5256303 TC |
1878 | /* |
1879 | * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t. | |
1880 | */ | |
1881 | static uint64_t | |
1882 | arc_hdr_size(arc_buf_hdr_t *hdr) | |
1883 | { | |
1884 | uint64_t size; | |
1885 | ||
1886 | if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF && | |
1887 | HDR_GET_PSIZE(hdr) > 0) { | |
1888 | size = HDR_GET_PSIZE(hdr); | |
1889 | } else { | |
1890 | ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0); | |
1891 | size = HDR_GET_LSIZE(hdr); | |
1892 | } | |
1893 | return (size); | |
1894 | } | |
1895 | ||
1896 | static int | |
1897 | arc_hdr_authenticate(arc_buf_hdr_t *hdr, spa_t *spa, uint64_t dsobj) | |
1898 | { | |
1899 | int ret; | |
1900 | uint64_t csize; | |
1901 | uint64_t lsize = HDR_GET_LSIZE(hdr); | |
1902 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
1903 | void *tmpbuf = NULL; | |
1904 | abd_t *abd = hdr->b_l1hdr.b_pabd; | |
1905 | ||
149ce888 | 1906 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); |
b5256303 TC |
1907 | ASSERT(HDR_AUTHENTICATED(hdr)); |
1908 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
1909 | ||
1910 | /* | |
1911 | * The MAC is calculated on the compressed data that is stored on disk. | |
1912 | * However, if compressed arc is disabled we will only have the | |
1913 | * decompressed data available to us now. Compress it into a temporary | |
1914 | * abd so we can verify the MAC. The performance overhead of this will | |
1915 | * be relatively low, since most objects in an encrypted objset will | |
1916 | * be encrypted (instead of authenticated) anyway. | |
1917 | */ | |
1918 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
1919 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
1920 | tmpbuf = zio_buf_alloc(lsize); | |
1921 | abd = abd_get_from_buf(tmpbuf, lsize); | |
1922 | abd_take_ownership_of_buf(abd, B_TRUE); | |
1923 | ||
1924 | csize = zio_compress_data(HDR_GET_COMPRESS(hdr), | |
1925 | hdr->b_l1hdr.b_pabd, tmpbuf, lsize); | |
1926 | ASSERT3U(csize, <=, psize); | |
1927 | abd_zero_off(abd, csize, psize - csize); | |
1928 | } | |
1929 | ||
1930 | /* | |
1931 | * Authentication is best effort. We authenticate whenever the key is | |
1932 | * available. If we succeed we clear ARC_FLAG_NOAUTH. | |
1933 | */ | |
1934 | if (hdr->b_crypt_hdr.b_ot == DMU_OT_OBJSET) { | |
1935 | ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); | |
1936 | ASSERT3U(lsize, ==, psize); | |
1937 | ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa, dsobj, abd, | |
1938 | psize, hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
1939 | } else { | |
1940 | ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj, abd, psize, | |
1941 | hdr->b_crypt_hdr.b_mac); | |
1942 | } | |
1943 | ||
1944 | if (ret == 0) | |
1945 | arc_hdr_clear_flags(hdr, ARC_FLAG_NOAUTH); | |
1946 | else if (ret != ENOENT) | |
1947 | goto error; | |
1948 | ||
1949 | if (tmpbuf != NULL) | |
1950 | abd_free(abd); | |
1951 | ||
1952 | return (0); | |
1953 | ||
1954 | error: | |
1955 | if (tmpbuf != NULL) | |
1956 | abd_free(abd); | |
1957 | ||
1958 | return (ret); | |
1959 | } | |
1960 | ||
1961 | /* | |
1962 | * This function will take a header that only has raw encrypted data in | |
1963 | * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in | |
1964 | * b_l1hdr.b_pabd. If designated in the header flags, this function will | |
1965 | * also decompress the data. | |
1966 | */ | |
1967 | static int | |
be9a5c35 | 1968 | arc_hdr_decrypt(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb) |
b5256303 TC |
1969 | { |
1970 | int ret; | |
b5256303 TC |
1971 | abd_t *cabd = NULL; |
1972 | void *tmp = NULL; | |
1973 | boolean_t no_crypt = B_FALSE; | |
1974 | boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
1975 | ||
149ce888 | 1976 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); |
b5256303 TC |
1977 | ASSERT(HDR_ENCRYPTED(hdr)); |
1978 | ||
1979 | arc_hdr_alloc_abd(hdr, B_FALSE); | |
1980 | ||
be9a5c35 TC |
1981 | ret = spa_do_crypt_abd(B_FALSE, spa, zb, hdr->b_crypt_hdr.b_ot, |
1982 | B_FALSE, bswap, hdr->b_crypt_hdr.b_salt, hdr->b_crypt_hdr.b_iv, | |
1983 | hdr->b_crypt_hdr.b_mac, HDR_GET_PSIZE(hdr), hdr->b_l1hdr.b_pabd, | |
b5256303 TC |
1984 | hdr->b_crypt_hdr.b_rabd, &no_crypt); |
1985 | if (ret != 0) | |
1986 | goto error; | |
1987 | ||
1988 | if (no_crypt) { | |
1989 | abd_copy(hdr->b_l1hdr.b_pabd, hdr->b_crypt_hdr.b_rabd, | |
1990 | HDR_GET_PSIZE(hdr)); | |
1991 | } | |
1992 | ||
1993 | /* | |
1994 | * If this header has disabled arc compression but the b_pabd is | |
1995 | * compressed after decrypting it, we need to decompress the newly | |
1996 | * decrypted data. | |
1997 | */ | |
1998 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
1999 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
2000 | /* | |
2001 | * We want to make sure that we are correctly honoring the | |
2002 | * zfs_abd_scatter_enabled setting, so we allocate an abd here | |
2003 | * and then loan a buffer from it, rather than allocating a | |
2004 | * linear buffer and wrapping it in an abd later. | |
2005 | */ | |
2006 | cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr); | |
2007 | tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr)); | |
2008 | ||
2009 | ret = zio_decompress_data(HDR_GET_COMPRESS(hdr), | |
2010 | hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr), | |
2011 | HDR_GET_LSIZE(hdr)); | |
2012 | if (ret != 0) { | |
2013 | abd_return_buf(cabd, tmp, arc_hdr_size(hdr)); | |
2014 | goto error; | |
2015 | } | |
2016 | ||
2017 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
2018 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
2019 | arc_hdr_size(hdr), hdr); | |
2020 | hdr->b_l1hdr.b_pabd = cabd; | |
2021 | } | |
2022 | ||
b5256303 TC |
2023 | return (0); |
2024 | ||
2025 | error: | |
2026 | arc_hdr_free_abd(hdr, B_FALSE); | |
b5256303 TC |
2027 | if (cabd != NULL) |
2028 | arc_free_data_buf(hdr, cabd, arc_hdr_size(hdr), hdr); | |
2029 | ||
2030 | return (ret); | |
2031 | } | |
2032 | ||
2033 | /* | |
2034 | * This function is called during arc_buf_fill() to prepare the header's | |
2035 | * abd plaintext pointer for use. This involves authenticated protected | |
2036 | * data and decrypting encrypted data into the plaintext abd. | |
2037 | */ | |
2038 | static int | |
2039 | arc_fill_hdr_crypt(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, spa_t *spa, | |
be9a5c35 | 2040 | const zbookmark_phys_t *zb, boolean_t noauth) |
b5256303 TC |
2041 | { |
2042 | int ret; | |
2043 | ||
2044 | ASSERT(HDR_PROTECTED(hdr)); | |
2045 | ||
2046 | if (hash_lock != NULL) | |
2047 | mutex_enter(hash_lock); | |
2048 | ||
2049 | if (HDR_NOAUTH(hdr) && !noauth) { | |
2050 | /* | |
2051 | * The caller requested authenticated data but our data has | |
2052 | * not been authenticated yet. Verify the MAC now if we can. | |
2053 | */ | |
be9a5c35 | 2054 | ret = arc_hdr_authenticate(hdr, spa, zb->zb_objset); |
b5256303 TC |
2055 | if (ret != 0) |
2056 | goto error; | |
2057 | } else if (HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd == NULL) { | |
2058 | /* | |
2059 | * If we only have the encrypted version of the data, but the | |
2060 | * unencrypted version was requested we take this opportunity | |
2061 | * to store the decrypted version in the header for future use. | |
2062 | */ | |
be9a5c35 | 2063 | ret = arc_hdr_decrypt(hdr, spa, zb); |
b5256303 TC |
2064 | if (ret != 0) |
2065 | goto error; | |
2066 | } | |
2067 | ||
2068 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
2069 | ||
2070 | if (hash_lock != NULL) | |
2071 | mutex_exit(hash_lock); | |
2072 | ||
2073 | return (0); | |
2074 | ||
2075 | error: | |
2076 | if (hash_lock != NULL) | |
2077 | mutex_exit(hash_lock); | |
2078 | ||
2079 | return (ret); | |
2080 | } | |
2081 | ||
2082 | /* | |
2083 | * This function is used by the dbuf code to decrypt bonus buffers in place. | |
2084 | * The dbuf code itself doesn't have any locking for decrypting a shared dnode | |
2085 | * block, so we use the hash lock here to protect against concurrent calls to | |
2086 | * arc_buf_fill(). | |
2087 | */ | |
2088 | static void | |
2089 | arc_buf_untransform_in_place(arc_buf_t *buf, kmutex_t *hash_lock) | |
2090 | { | |
2091 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
2092 | ||
2093 | ASSERT(HDR_ENCRYPTED(hdr)); | |
2094 | ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE); | |
149ce888 | 2095 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); |
b5256303 TC |
2096 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
2097 | ||
2098 | zio_crypt_copy_dnode_bonus(hdr->b_l1hdr.b_pabd, buf->b_data, | |
2099 | arc_buf_size(buf)); | |
2100 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
2101 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
2102 | hdr->b_crypt_hdr.b_ebufcnt -= 1; | |
2103 | } | |
2104 | ||
524b4217 DK |
2105 | /* |
2106 | * Given a buf that has a data buffer attached to it, this function will | |
2107 | * efficiently fill the buf with data of the specified compression setting from | |
2108 | * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr | |
2109 | * are already sharing a data buf, no copy is performed. | |
2110 | * | |
2111 | * If the buf is marked as compressed but uncompressed data was requested, this | |
2112 | * will allocate a new data buffer for the buf, remove that flag, and fill the | |
2113 | * buf with uncompressed data. You can't request a compressed buf on a hdr with | |
2114 | * uncompressed data, and (since we haven't added support for it yet) if you | |
2115 | * want compressed data your buf must already be marked as compressed and have | |
2116 | * the correct-sized data buffer. | |
2117 | */ | |
2118 | static int | |
be9a5c35 TC |
2119 | arc_buf_fill(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb, |
2120 | arc_fill_flags_t flags) | |
d3c2ae1c | 2121 | { |
b5256303 | 2122 | int error = 0; |
d3c2ae1c | 2123 | arc_buf_hdr_t *hdr = buf->b_hdr; |
b5256303 TC |
2124 | boolean_t hdr_compressed = |
2125 | (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); | |
2126 | boolean_t compressed = (flags & ARC_FILL_COMPRESSED) != 0; | |
2127 | boolean_t encrypted = (flags & ARC_FILL_ENCRYPTED) != 0; | |
d3c2ae1c | 2128 | dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap; |
b5256303 | 2129 | kmutex_t *hash_lock = (flags & ARC_FILL_LOCKED) ? NULL : HDR_LOCK(hdr); |
d3c2ae1c | 2130 | |
524b4217 | 2131 | ASSERT3P(buf->b_data, !=, NULL); |
b5256303 | 2132 | IMPLY(compressed, hdr_compressed || ARC_BUF_ENCRYPTED(buf)); |
524b4217 | 2133 | IMPLY(compressed, ARC_BUF_COMPRESSED(buf)); |
b5256303 TC |
2134 | IMPLY(encrypted, HDR_ENCRYPTED(hdr)); |
2135 | IMPLY(encrypted, ARC_BUF_ENCRYPTED(buf)); | |
2136 | IMPLY(encrypted, ARC_BUF_COMPRESSED(buf)); | |
2137 | IMPLY(encrypted, !ARC_BUF_SHARED(buf)); | |
2138 | ||
2139 | /* | |
2140 | * If the caller wanted encrypted data we just need to copy it from | |
2141 | * b_rabd and potentially byteswap it. We won't be able to do any | |
2142 | * further transforms on it. | |
2143 | */ | |
2144 | if (encrypted) { | |
2145 | ASSERT(HDR_HAS_RABD(hdr)); | |
2146 | abd_copy_to_buf(buf->b_data, hdr->b_crypt_hdr.b_rabd, | |
2147 | HDR_GET_PSIZE(hdr)); | |
2148 | goto byteswap; | |
2149 | } | |
2150 | ||
2151 | /* | |
69830602 TC |
2152 | * Adjust encrypted and authenticated headers to accomodate |
2153 | * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are | |
2154 | * allowed to fail decryption due to keys not being loaded | |
2155 | * without being marked as an IO error. | |
b5256303 TC |
2156 | */ |
2157 | if (HDR_PROTECTED(hdr)) { | |
2158 | error = arc_fill_hdr_crypt(hdr, hash_lock, spa, | |
be9a5c35 | 2159 | zb, !!(flags & ARC_FILL_NOAUTH)); |
69830602 TC |
2160 | if (error == EACCES && (flags & ARC_FILL_IN_PLACE) != 0) { |
2161 | return (error); | |
2162 | } else if (error != 0) { | |
e7504d7a TC |
2163 | if (hash_lock != NULL) |
2164 | mutex_enter(hash_lock); | |
2c24b5b1 | 2165 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); |
e7504d7a TC |
2166 | if (hash_lock != NULL) |
2167 | mutex_exit(hash_lock); | |
b5256303 | 2168 | return (error); |
2c24b5b1 | 2169 | } |
b5256303 TC |
2170 | } |
2171 | ||
2172 | /* | |
2173 | * There is a special case here for dnode blocks which are | |
2174 | * decrypting their bonus buffers. These blocks may request to | |
2175 | * be decrypted in-place. This is necessary because there may | |
2176 | * be many dnodes pointing into this buffer and there is | |
2177 | * currently no method to synchronize replacing the backing | |
2178 | * b_data buffer and updating all of the pointers. Here we use | |
2179 | * the hash lock to ensure there are no races. If the need | |
2180 | * arises for other types to be decrypted in-place, they must | |
2181 | * add handling here as well. | |
2182 | */ | |
2183 | if ((flags & ARC_FILL_IN_PLACE) != 0) { | |
2184 | ASSERT(!hdr_compressed); | |
2185 | ASSERT(!compressed); | |
2186 | ASSERT(!encrypted); | |
2187 | ||
2188 | if (HDR_ENCRYPTED(hdr) && ARC_BUF_ENCRYPTED(buf)) { | |
2189 | ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE); | |
2190 | ||
2191 | if (hash_lock != NULL) | |
2192 | mutex_enter(hash_lock); | |
2193 | arc_buf_untransform_in_place(buf, hash_lock); | |
2194 | if (hash_lock != NULL) | |
2195 | mutex_exit(hash_lock); | |
2196 | ||
2197 | /* Compute the hdr's checksum if necessary */ | |
2198 | arc_cksum_compute(buf); | |
2199 | } | |
2200 | ||
2201 | return (0); | |
2202 | } | |
524b4217 DK |
2203 | |
2204 | if (hdr_compressed == compressed) { | |
2aa34383 | 2205 | if (!arc_buf_is_shared(buf)) { |
a6255b7f | 2206 | abd_copy_to_buf(buf->b_data, hdr->b_l1hdr.b_pabd, |
524b4217 | 2207 | arc_buf_size(buf)); |
2aa34383 | 2208 | } |
d3c2ae1c | 2209 | } else { |
524b4217 DK |
2210 | ASSERT(hdr_compressed); |
2211 | ASSERT(!compressed); | |
d3c2ae1c | 2212 | ASSERT3U(HDR_GET_LSIZE(hdr), !=, HDR_GET_PSIZE(hdr)); |
2aa34383 DK |
2213 | |
2214 | /* | |
524b4217 DK |
2215 | * If the buf is sharing its data with the hdr, unlink it and |
2216 | * allocate a new data buffer for the buf. | |
2aa34383 | 2217 | */ |
524b4217 DK |
2218 | if (arc_buf_is_shared(buf)) { |
2219 | ASSERT(ARC_BUF_COMPRESSED(buf)); | |
2220 | ||
2221 | /* We need to give the buf it's own b_data */ | |
2222 | buf->b_flags &= ~ARC_BUF_FLAG_SHARED; | |
2aa34383 DK |
2223 | buf->b_data = |
2224 | arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); | |
2225 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); | |
2226 | ||
524b4217 | 2227 | /* Previously overhead was 0; just add new overhead */ |
2aa34383 | 2228 | ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); |
524b4217 DK |
2229 | } else if (ARC_BUF_COMPRESSED(buf)) { |
2230 | /* We need to reallocate the buf's b_data */ | |
2231 | arc_free_data_buf(hdr, buf->b_data, HDR_GET_PSIZE(hdr), | |
2232 | buf); | |
2233 | buf->b_data = | |
2234 | arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); | |
2235 | ||
2236 | /* We increased the size of b_data; update overhead */ | |
2237 | ARCSTAT_INCR(arcstat_overhead_size, | |
2238 | HDR_GET_LSIZE(hdr) - HDR_GET_PSIZE(hdr)); | |
2aa34383 DK |
2239 | } |
2240 | ||
524b4217 DK |
2241 | /* |
2242 | * Regardless of the buf's previous compression settings, it | |
2243 | * should not be compressed at the end of this function. | |
2244 | */ | |
2245 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
2246 | ||
2247 | /* | |
2248 | * Try copying the data from another buf which already has a | |
2249 | * decompressed version. If that's not possible, it's time to | |
2250 | * bite the bullet and decompress the data from the hdr. | |
2251 | */ | |
2252 | if (arc_buf_try_copy_decompressed_data(buf)) { | |
2253 | /* Skip byteswapping and checksumming (already done) */ | |
2254 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, !=, NULL); | |
2255 | return (0); | |
2256 | } else { | |
b5256303 | 2257 | error = zio_decompress_data(HDR_GET_COMPRESS(hdr), |
a6255b7f | 2258 | hdr->b_l1hdr.b_pabd, buf->b_data, |
524b4217 DK |
2259 | HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); |
2260 | ||
2261 | /* | |
2262 | * Absent hardware errors or software bugs, this should | |
2263 | * be impossible, but log it anyway so we can debug it. | |
2264 | */ | |
2265 | if (error != 0) { | |
2266 | zfs_dbgmsg( | |
2267 | "hdr %p, compress %d, psize %d, lsize %d", | |
b5256303 | 2268 | hdr, arc_hdr_get_compress(hdr), |
524b4217 | 2269 | HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); |
e7504d7a TC |
2270 | if (hash_lock != NULL) |
2271 | mutex_enter(hash_lock); | |
2c24b5b1 | 2272 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); |
e7504d7a TC |
2273 | if (hash_lock != NULL) |
2274 | mutex_exit(hash_lock); | |
524b4217 DK |
2275 | return (SET_ERROR(EIO)); |
2276 | } | |
d3c2ae1c GW |
2277 | } |
2278 | } | |
524b4217 | 2279 | |
b5256303 | 2280 | byteswap: |
524b4217 | 2281 | /* Byteswap the buf's data if necessary */ |
d3c2ae1c GW |
2282 | if (bswap != DMU_BSWAP_NUMFUNCS) { |
2283 | ASSERT(!HDR_SHARED_DATA(hdr)); | |
2284 | ASSERT3U(bswap, <, DMU_BSWAP_NUMFUNCS); | |
2285 | dmu_ot_byteswap[bswap].ob_func(buf->b_data, HDR_GET_LSIZE(hdr)); | |
2286 | } | |
524b4217 DK |
2287 | |
2288 | /* Compute the hdr's checksum if necessary */ | |
d3c2ae1c | 2289 | arc_cksum_compute(buf); |
524b4217 | 2290 | |
d3c2ae1c GW |
2291 | return (0); |
2292 | } | |
2293 | ||
2294 | /* | |
b5256303 TC |
2295 | * If this function is being called to decrypt an encrypted buffer or verify an |
2296 | * authenticated one, the key must be loaded and a mapping must be made | |
2297 | * available in the keystore via spa_keystore_create_mapping() or one of its | |
2298 | * callers. | |
d3c2ae1c | 2299 | */ |
b5256303 | 2300 | int |
a2c2ed1b TC |
2301 | arc_untransform(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb, |
2302 | boolean_t in_place) | |
d3c2ae1c | 2303 | { |
a2c2ed1b | 2304 | int ret; |
b5256303 | 2305 | arc_fill_flags_t flags = 0; |
d3c2ae1c | 2306 | |
b5256303 TC |
2307 | if (in_place) |
2308 | flags |= ARC_FILL_IN_PLACE; | |
2309 | ||
be9a5c35 | 2310 | ret = arc_buf_fill(buf, spa, zb, flags); |
a2c2ed1b TC |
2311 | if (ret == ECKSUM) { |
2312 | /* | |
2313 | * Convert authentication and decryption errors to EIO | |
2314 | * (and generate an ereport) before leaving the ARC. | |
2315 | */ | |
2316 | ret = SET_ERROR(EIO); | |
be9a5c35 | 2317 | spa_log_error(spa, zb); |
a2c2ed1b TC |
2318 | zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION, |
2319 | spa, NULL, zb, NULL, 0, 0); | |
2320 | } | |
2321 | ||
2322 | return (ret); | |
d3c2ae1c GW |
2323 | } |
2324 | ||
2325 | /* | |
2326 | * Increment the amount of evictable space in the arc_state_t's refcount. | |
2327 | * We account for the space used by the hdr and the arc buf individually | |
2328 | * so that we can add and remove them from the refcount individually. | |
2329 | */ | |
34dc7c2f | 2330 | static void |
d3c2ae1c GW |
2331 | arc_evictable_space_increment(arc_buf_hdr_t *hdr, arc_state_t *state) |
2332 | { | |
2333 | arc_buf_contents_t type = arc_buf_type(hdr); | |
d3c2ae1c GW |
2334 | |
2335 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
2336 | ||
2337 | if (GHOST_STATE(state)) { | |
2338 | ASSERT0(hdr->b_l1hdr.b_bufcnt); | |
2339 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
a6255b7f | 2340 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2341 | ASSERT(!HDR_HAS_RABD(hdr)); |
424fd7c3 | 2342 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
2aa34383 | 2343 | HDR_GET_LSIZE(hdr), hdr); |
d3c2ae1c GW |
2344 | return; |
2345 | } | |
2346 | ||
2347 | ASSERT(!GHOST_STATE(state)); | |
a6255b7f | 2348 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 | 2349 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
d3c2ae1c GW |
2350 | arc_hdr_size(hdr), hdr); |
2351 | } | |
b5256303 | 2352 | if (HDR_HAS_RABD(hdr)) { |
424fd7c3 | 2353 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
b5256303 TC |
2354 | HDR_GET_PSIZE(hdr), hdr); |
2355 | } | |
2356 | ||
1c27024e DB |
2357 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
2358 | buf = buf->b_next) { | |
2aa34383 | 2359 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2360 | continue; |
424fd7c3 | 2361 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
2aa34383 | 2362 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2363 | } |
2364 | } | |
2365 | ||
2366 | /* | |
2367 | * Decrement the amount of evictable space in the arc_state_t's refcount. | |
2368 | * We account for the space used by the hdr and the arc buf individually | |
2369 | * so that we can add and remove them from the refcount individually. | |
2370 | */ | |
2371 | static void | |
2aa34383 | 2372 | arc_evictable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state) |
d3c2ae1c GW |
2373 | { |
2374 | arc_buf_contents_t type = arc_buf_type(hdr); | |
d3c2ae1c GW |
2375 | |
2376 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
2377 | ||
2378 | if (GHOST_STATE(state)) { | |
2379 | ASSERT0(hdr->b_l1hdr.b_bufcnt); | |
2380 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
a6255b7f | 2381 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2382 | ASSERT(!HDR_HAS_RABD(hdr)); |
424fd7c3 | 2383 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
2aa34383 | 2384 | HDR_GET_LSIZE(hdr), hdr); |
d3c2ae1c GW |
2385 | return; |
2386 | } | |
2387 | ||
2388 | ASSERT(!GHOST_STATE(state)); | |
a6255b7f | 2389 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 | 2390 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c GW |
2391 | arc_hdr_size(hdr), hdr); |
2392 | } | |
b5256303 | 2393 | if (HDR_HAS_RABD(hdr)) { |
424fd7c3 | 2394 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
b5256303 TC |
2395 | HDR_GET_PSIZE(hdr), hdr); |
2396 | } | |
2397 | ||
1c27024e DB |
2398 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
2399 | buf = buf->b_next) { | |
2aa34383 | 2400 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2401 | continue; |
424fd7c3 | 2402 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
2aa34383 | 2403 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2404 | } |
2405 | } | |
2406 | ||
2407 | /* | |
2408 | * Add a reference to this hdr indicating that someone is actively | |
2409 | * referencing that memory. When the refcount transitions from 0 to 1, | |
2410 | * we remove it from the respective arc_state_t list to indicate that | |
2411 | * it is not evictable. | |
2412 | */ | |
2413 | static void | |
2414 | add_reference(arc_buf_hdr_t *hdr, void *tag) | |
34dc7c2f | 2415 | { |
b9541d6b CW |
2416 | arc_state_t *state; |
2417 | ||
2418 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
d3c2ae1c GW |
2419 | if (!MUTEX_HELD(HDR_LOCK(hdr))) { |
2420 | ASSERT(hdr->b_l1hdr.b_state == arc_anon); | |
424fd7c3 | 2421 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
2422 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
2423 | } | |
34dc7c2f | 2424 | |
b9541d6b CW |
2425 | state = hdr->b_l1hdr.b_state; |
2426 | ||
c13060e4 | 2427 | if ((zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && |
b9541d6b CW |
2428 | (state != arc_anon)) { |
2429 | /* We don't use the L2-only state list. */ | |
2430 | if (state != arc_l2c_only) { | |
64fc7762 | 2431 | multilist_remove(state->arcs_list[arc_buf_type(hdr)], |
d3c2ae1c | 2432 | hdr); |
2aa34383 | 2433 | arc_evictable_space_decrement(hdr, state); |
34dc7c2f | 2434 | } |
b128c09f | 2435 | /* remove the prefetch flag if we get a reference */ |
d3c2ae1c | 2436 | arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); |
34dc7c2f BB |
2437 | } |
2438 | } | |
2439 | ||
d3c2ae1c GW |
2440 | /* |
2441 | * Remove a reference from this hdr. When the reference transitions from | |
2442 | * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's | |
2443 | * list making it eligible for eviction. | |
2444 | */ | |
34dc7c2f | 2445 | static int |
2a432414 | 2446 | remove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) |
34dc7c2f BB |
2447 | { |
2448 | int cnt; | |
b9541d6b | 2449 | arc_state_t *state = hdr->b_l1hdr.b_state; |
34dc7c2f | 2450 | |
b9541d6b | 2451 | ASSERT(HDR_HAS_L1HDR(hdr)); |
34dc7c2f BB |
2452 | ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); |
2453 | ASSERT(!GHOST_STATE(state)); | |
2454 | ||
b9541d6b CW |
2455 | /* |
2456 | * arc_l2c_only counts as a ghost state so we don't need to explicitly | |
2457 | * check to prevent usage of the arc_l2c_only list. | |
2458 | */ | |
424fd7c3 | 2459 | if (((cnt = zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && |
34dc7c2f | 2460 | (state != arc_anon)) { |
64fc7762 | 2461 | multilist_insert(state->arcs_list[arc_buf_type(hdr)], hdr); |
d3c2ae1c GW |
2462 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); |
2463 | arc_evictable_space_increment(hdr, state); | |
34dc7c2f BB |
2464 | } |
2465 | return (cnt); | |
2466 | } | |
2467 | ||
e0b0ca98 BB |
2468 | /* |
2469 | * Returns detailed information about a specific arc buffer. When the | |
2470 | * state_index argument is set the function will calculate the arc header | |
2471 | * list position for its arc state. Since this requires a linear traversal | |
2472 | * callers are strongly encourage not to do this. However, it can be helpful | |
2473 | * for targeted analysis so the functionality is provided. | |
2474 | */ | |
2475 | void | |
2476 | arc_buf_info(arc_buf_t *ab, arc_buf_info_t *abi, int state_index) | |
2477 | { | |
2478 | arc_buf_hdr_t *hdr = ab->b_hdr; | |
b9541d6b CW |
2479 | l1arc_buf_hdr_t *l1hdr = NULL; |
2480 | l2arc_buf_hdr_t *l2hdr = NULL; | |
2481 | arc_state_t *state = NULL; | |
2482 | ||
8887c7d7 TC |
2483 | memset(abi, 0, sizeof (arc_buf_info_t)); |
2484 | ||
2485 | if (hdr == NULL) | |
2486 | return; | |
2487 | ||
2488 | abi->abi_flags = hdr->b_flags; | |
2489 | ||
b9541d6b CW |
2490 | if (HDR_HAS_L1HDR(hdr)) { |
2491 | l1hdr = &hdr->b_l1hdr; | |
2492 | state = l1hdr->b_state; | |
2493 | } | |
2494 | if (HDR_HAS_L2HDR(hdr)) | |
2495 | l2hdr = &hdr->b_l2hdr; | |
e0b0ca98 | 2496 | |
b9541d6b | 2497 | if (l1hdr) { |
d3c2ae1c | 2498 | abi->abi_bufcnt = l1hdr->b_bufcnt; |
b9541d6b CW |
2499 | abi->abi_access = l1hdr->b_arc_access; |
2500 | abi->abi_mru_hits = l1hdr->b_mru_hits; | |
2501 | abi->abi_mru_ghost_hits = l1hdr->b_mru_ghost_hits; | |
2502 | abi->abi_mfu_hits = l1hdr->b_mfu_hits; | |
2503 | abi->abi_mfu_ghost_hits = l1hdr->b_mfu_ghost_hits; | |
424fd7c3 | 2504 | abi->abi_holds = zfs_refcount_count(&l1hdr->b_refcnt); |
b9541d6b CW |
2505 | } |
2506 | ||
2507 | if (l2hdr) { | |
2508 | abi->abi_l2arc_dattr = l2hdr->b_daddr; | |
b9541d6b CW |
2509 | abi->abi_l2arc_hits = l2hdr->b_hits; |
2510 | } | |
2511 | ||
e0b0ca98 | 2512 | abi->abi_state_type = state ? state->arcs_state : ARC_STATE_ANON; |
b9541d6b | 2513 | abi->abi_state_contents = arc_buf_type(hdr); |
d3c2ae1c | 2514 | abi->abi_size = arc_hdr_size(hdr); |
e0b0ca98 BB |
2515 | } |
2516 | ||
34dc7c2f | 2517 | /* |
ca0bf58d | 2518 | * Move the supplied buffer to the indicated state. The hash lock |
34dc7c2f BB |
2519 | * for the buffer must be held by the caller. |
2520 | */ | |
2521 | static void | |
2a432414 GW |
2522 | arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, |
2523 | kmutex_t *hash_lock) | |
34dc7c2f | 2524 | { |
b9541d6b CW |
2525 | arc_state_t *old_state; |
2526 | int64_t refcnt; | |
d3c2ae1c GW |
2527 | uint32_t bufcnt; |
2528 | boolean_t update_old, update_new; | |
b9541d6b CW |
2529 | arc_buf_contents_t buftype = arc_buf_type(hdr); |
2530 | ||
2531 | /* | |
2532 | * We almost always have an L1 hdr here, since we call arc_hdr_realloc() | |
2533 | * in arc_read() when bringing a buffer out of the L2ARC. However, the | |
2534 | * L1 hdr doesn't always exist when we change state to arc_anon before | |
2535 | * destroying a header, in which case reallocating to add the L1 hdr is | |
2536 | * pointless. | |
2537 | */ | |
2538 | if (HDR_HAS_L1HDR(hdr)) { | |
2539 | old_state = hdr->b_l1hdr.b_state; | |
424fd7c3 | 2540 | refcnt = zfs_refcount_count(&hdr->b_l1hdr.b_refcnt); |
d3c2ae1c | 2541 | bufcnt = hdr->b_l1hdr.b_bufcnt; |
b5256303 TC |
2542 | update_old = (bufcnt > 0 || hdr->b_l1hdr.b_pabd != NULL || |
2543 | HDR_HAS_RABD(hdr)); | |
b9541d6b CW |
2544 | } else { |
2545 | old_state = arc_l2c_only; | |
2546 | refcnt = 0; | |
d3c2ae1c GW |
2547 | bufcnt = 0; |
2548 | update_old = B_FALSE; | |
b9541d6b | 2549 | } |
d3c2ae1c | 2550 | update_new = update_old; |
34dc7c2f BB |
2551 | |
2552 | ASSERT(MUTEX_HELD(hash_lock)); | |
e8b96c60 | 2553 | ASSERT3P(new_state, !=, old_state); |
d3c2ae1c GW |
2554 | ASSERT(!GHOST_STATE(new_state) || bufcnt == 0); |
2555 | ASSERT(old_state != arc_anon || bufcnt <= 1); | |
34dc7c2f BB |
2556 | |
2557 | /* | |
2558 | * If this buffer is evictable, transfer it from the | |
2559 | * old state list to the new state list. | |
2560 | */ | |
2561 | if (refcnt == 0) { | |
b9541d6b | 2562 | if (old_state != arc_anon && old_state != arc_l2c_only) { |
b9541d6b | 2563 | ASSERT(HDR_HAS_L1HDR(hdr)); |
64fc7762 | 2564 | multilist_remove(old_state->arcs_list[buftype], hdr); |
34dc7c2f | 2565 | |
d3c2ae1c GW |
2566 | if (GHOST_STATE(old_state)) { |
2567 | ASSERT0(bufcnt); | |
2568 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
2569 | update_old = B_TRUE; | |
34dc7c2f | 2570 | } |
2aa34383 | 2571 | arc_evictable_space_decrement(hdr, old_state); |
34dc7c2f | 2572 | } |
b9541d6b | 2573 | if (new_state != arc_anon && new_state != arc_l2c_only) { |
b9541d6b CW |
2574 | /* |
2575 | * An L1 header always exists here, since if we're | |
2576 | * moving to some L1-cached state (i.e. not l2c_only or | |
2577 | * anonymous), we realloc the header to add an L1hdr | |
2578 | * beforehand. | |
2579 | */ | |
2580 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
64fc7762 | 2581 | multilist_insert(new_state->arcs_list[buftype], hdr); |
34dc7c2f | 2582 | |
34dc7c2f | 2583 | if (GHOST_STATE(new_state)) { |
d3c2ae1c GW |
2584 | ASSERT0(bufcnt); |
2585 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
2586 | update_new = B_TRUE; | |
34dc7c2f | 2587 | } |
d3c2ae1c | 2588 | arc_evictable_space_increment(hdr, new_state); |
34dc7c2f BB |
2589 | } |
2590 | } | |
2591 | ||
d3c2ae1c | 2592 | ASSERT(!HDR_EMPTY(hdr)); |
2a432414 GW |
2593 | if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) |
2594 | buf_hash_remove(hdr); | |
34dc7c2f | 2595 | |
b9541d6b | 2596 | /* adjust state sizes (ignore arc_l2c_only) */ |
36da08ef | 2597 | |
d3c2ae1c | 2598 | if (update_new && new_state != arc_l2c_only) { |
36da08ef PS |
2599 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2600 | if (GHOST_STATE(new_state)) { | |
d3c2ae1c | 2601 | ASSERT0(bufcnt); |
36da08ef PS |
2602 | |
2603 | /* | |
d3c2ae1c | 2604 | * When moving a header to a ghost state, we first |
36da08ef | 2605 | * remove all arc buffers. Thus, we'll have a |
d3c2ae1c | 2606 | * bufcnt of zero, and no arc buffer to use for |
36da08ef PS |
2607 | * the reference. As a result, we use the arc |
2608 | * header pointer for the reference. | |
2609 | */ | |
424fd7c3 | 2610 | (void) zfs_refcount_add_many(&new_state->arcs_size, |
d3c2ae1c | 2611 | HDR_GET_LSIZE(hdr), hdr); |
a6255b7f | 2612 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2613 | ASSERT(!HDR_HAS_RABD(hdr)); |
36da08ef | 2614 | } else { |
d3c2ae1c | 2615 | uint32_t buffers = 0; |
36da08ef PS |
2616 | |
2617 | /* | |
2618 | * Each individual buffer holds a unique reference, | |
2619 | * thus we must remove each of these references one | |
2620 | * at a time. | |
2621 | */ | |
1c27024e | 2622 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
36da08ef | 2623 | buf = buf->b_next) { |
d3c2ae1c GW |
2624 | ASSERT3U(bufcnt, !=, 0); |
2625 | buffers++; | |
2626 | ||
2627 | /* | |
2628 | * When the arc_buf_t is sharing the data | |
2629 | * block with the hdr, the owner of the | |
2630 | * reference belongs to the hdr. Only | |
2631 | * add to the refcount if the arc_buf_t is | |
2632 | * not shared. | |
2633 | */ | |
2aa34383 | 2634 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2635 | continue; |
d3c2ae1c | 2636 | |
424fd7c3 TS |
2637 | (void) zfs_refcount_add_many( |
2638 | &new_state->arcs_size, | |
2aa34383 | 2639 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2640 | } |
2641 | ASSERT3U(bufcnt, ==, buffers); | |
2642 | ||
a6255b7f | 2643 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 TS |
2644 | (void) zfs_refcount_add_many( |
2645 | &new_state->arcs_size, | |
d3c2ae1c | 2646 | arc_hdr_size(hdr), hdr); |
b5256303 TC |
2647 | } |
2648 | ||
2649 | if (HDR_HAS_RABD(hdr)) { | |
424fd7c3 TS |
2650 | (void) zfs_refcount_add_many( |
2651 | &new_state->arcs_size, | |
b5256303 | 2652 | HDR_GET_PSIZE(hdr), hdr); |
36da08ef PS |
2653 | } |
2654 | } | |
2655 | } | |
2656 | ||
d3c2ae1c | 2657 | if (update_old && old_state != arc_l2c_only) { |
36da08ef PS |
2658 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2659 | if (GHOST_STATE(old_state)) { | |
d3c2ae1c | 2660 | ASSERT0(bufcnt); |
a6255b7f | 2661 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2662 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c | 2663 | |
36da08ef PS |
2664 | /* |
2665 | * When moving a header off of a ghost state, | |
d3c2ae1c GW |
2666 | * the header will not contain any arc buffers. |
2667 | * We use the arc header pointer for the reference | |
2668 | * which is exactly what we did when we put the | |
2669 | * header on the ghost state. | |
36da08ef PS |
2670 | */ |
2671 | ||
424fd7c3 | 2672 | (void) zfs_refcount_remove_many(&old_state->arcs_size, |
d3c2ae1c | 2673 | HDR_GET_LSIZE(hdr), hdr); |
36da08ef | 2674 | } else { |
d3c2ae1c | 2675 | uint32_t buffers = 0; |
36da08ef PS |
2676 | |
2677 | /* | |
2678 | * Each individual buffer holds a unique reference, | |
2679 | * thus we must remove each of these references one | |
2680 | * at a time. | |
2681 | */ | |
1c27024e | 2682 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
36da08ef | 2683 | buf = buf->b_next) { |
d3c2ae1c GW |
2684 | ASSERT3U(bufcnt, !=, 0); |
2685 | buffers++; | |
2686 | ||
2687 | /* | |
2688 | * When the arc_buf_t is sharing the data | |
2689 | * block with the hdr, the owner of the | |
2690 | * reference belongs to the hdr. Only | |
2691 | * add to the refcount if the arc_buf_t is | |
2692 | * not shared. | |
2693 | */ | |
2aa34383 | 2694 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2695 | continue; |
d3c2ae1c | 2696 | |
424fd7c3 | 2697 | (void) zfs_refcount_remove_many( |
2aa34383 | 2698 | &old_state->arcs_size, arc_buf_size(buf), |
d3c2ae1c | 2699 | buf); |
36da08ef | 2700 | } |
d3c2ae1c | 2701 | ASSERT3U(bufcnt, ==, buffers); |
b5256303 TC |
2702 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || |
2703 | HDR_HAS_RABD(hdr)); | |
2704 | ||
2705 | if (hdr->b_l1hdr.b_pabd != NULL) { | |
424fd7c3 | 2706 | (void) zfs_refcount_remove_many( |
b5256303 TC |
2707 | &old_state->arcs_size, arc_hdr_size(hdr), |
2708 | hdr); | |
2709 | } | |
2710 | ||
2711 | if (HDR_HAS_RABD(hdr)) { | |
424fd7c3 | 2712 | (void) zfs_refcount_remove_many( |
b5256303 TC |
2713 | &old_state->arcs_size, HDR_GET_PSIZE(hdr), |
2714 | hdr); | |
2715 | } | |
36da08ef | 2716 | } |
34dc7c2f | 2717 | } |
36da08ef | 2718 | |
b9541d6b CW |
2719 | if (HDR_HAS_L1HDR(hdr)) |
2720 | hdr->b_l1hdr.b_state = new_state; | |
34dc7c2f | 2721 | |
b9541d6b CW |
2722 | /* |
2723 | * L2 headers should never be on the L2 state list since they don't | |
2724 | * have L1 headers allocated. | |
2725 | */ | |
64fc7762 MA |
2726 | ASSERT(multilist_is_empty(arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && |
2727 | multilist_is_empty(arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); | |
34dc7c2f BB |
2728 | } |
2729 | ||
2730 | void | |
d164b209 | 2731 | arc_space_consume(uint64_t space, arc_space_type_t type) |
34dc7c2f | 2732 | { |
d164b209 BB |
2733 | ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); |
2734 | ||
2735 | switch (type) { | |
e75c13c3 BB |
2736 | default: |
2737 | break; | |
d164b209 | 2738 | case ARC_SPACE_DATA: |
37fb3e43 | 2739 | aggsum_add(&astat_data_size, space); |
d164b209 | 2740 | break; |
cc7f677c | 2741 | case ARC_SPACE_META: |
37fb3e43 | 2742 | aggsum_add(&astat_metadata_size, space); |
cc7f677c | 2743 | break; |
25458cbe | 2744 | case ARC_SPACE_BONUS: |
37fb3e43 | 2745 | aggsum_add(&astat_bonus_size, space); |
25458cbe TC |
2746 | break; |
2747 | case ARC_SPACE_DNODE: | |
37fb3e43 | 2748 | aggsum_add(&astat_dnode_size, space); |
25458cbe TC |
2749 | break; |
2750 | case ARC_SPACE_DBUF: | |
37fb3e43 | 2751 | aggsum_add(&astat_dbuf_size, space); |
d164b209 BB |
2752 | break; |
2753 | case ARC_SPACE_HDRS: | |
37fb3e43 | 2754 | aggsum_add(&astat_hdr_size, space); |
d164b209 BB |
2755 | break; |
2756 | case ARC_SPACE_L2HDRS: | |
37fb3e43 | 2757 | aggsum_add(&astat_l2_hdr_size, space); |
d164b209 BB |
2758 | break; |
2759 | } | |
2760 | ||
500445c0 | 2761 | if (type != ARC_SPACE_DATA) |
37fb3e43 | 2762 | aggsum_add(&arc_meta_used, space); |
cc7f677c | 2763 | |
37fb3e43 | 2764 | aggsum_add(&arc_size, space); |
34dc7c2f BB |
2765 | } |
2766 | ||
2767 | void | |
d164b209 | 2768 | arc_space_return(uint64_t space, arc_space_type_t type) |
34dc7c2f | 2769 | { |
d164b209 BB |
2770 | ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); |
2771 | ||
2772 | switch (type) { | |
e75c13c3 BB |
2773 | default: |
2774 | break; | |
d164b209 | 2775 | case ARC_SPACE_DATA: |
37fb3e43 | 2776 | aggsum_add(&astat_data_size, -space); |
d164b209 | 2777 | break; |
cc7f677c | 2778 | case ARC_SPACE_META: |
37fb3e43 | 2779 | aggsum_add(&astat_metadata_size, -space); |
cc7f677c | 2780 | break; |
25458cbe | 2781 | case ARC_SPACE_BONUS: |
37fb3e43 | 2782 | aggsum_add(&astat_bonus_size, -space); |
25458cbe TC |
2783 | break; |
2784 | case ARC_SPACE_DNODE: | |
37fb3e43 | 2785 | aggsum_add(&astat_dnode_size, -space); |
25458cbe TC |
2786 | break; |
2787 | case ARC_SPACE_DBUF: | |
37fb3e43 | 2788 | aggsum_add(&astat_dbuf_size, -space); |
d164b209 BB |
2789 | break; |
2790 | case ARC_SPACE_HDRS: | |
37fb3e43 | 2791 | aggsum_add(&astat_hdr_size, -space); |
d164b209 BB |
2792 | break; |
2793 | case ARC_SPACE_L2HDRS: | |
37fb3e43 | 2794 | aggsum_add(&astat_l2_hdr_size, -space); |
d164b209 BB |
2795 | break; |
2796 | } | |
2797 | ||
cc7f677c | 2798 | if (type != ARC_SPACE_DATA) { |
37fb3e43 PD |
2799 | ASSERT(aggsum_compare(&arc_meta_used, space) >= 0); |
2800 | /* | |
2801 | * We use the upper bound here rather than the precise value | |
2802 | * because the arc_meta_max value doesn't need to be | |
2803 | * precise. It's only consumed by humans via arcstats. | |
2804 | */ | |
2805 | if (arc_meta_max < aggsum_upper_bound(&arc_meta_used)) | |
2806 | arc_meta_max = aggsum_upper_bound(&arc_meta_used); | |
2807 | aggsum_add(&arc_meta_used, -space); | |
cc7f677c PS |
2808 | } |
2809 | ||
37fb3e43 PD |
2810 | ASSERT(aggsum_compare(&arc_size, space) >= 0); |
2811 | aggsum_add(&arc_size, -space); | |
34dc7c2f BB |
2812 | } |
2813 | ||
d3c2ae1c | 2814 | /* |
524b4217 | 2815 | * Given a hdr and a buf, returns whether that buf can share its b_data buffer |
a6255b7f | 2816 | * with the hdr's b_pabd. |
d3c2ae1c | 2817 | */ |
524b4217 DK |
2818 | static boolean_t |
2819 | arc_can_share(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
2820 | { | |
524b4217 DK |
2821 | /* |
2822 | * The criteria for sharing a hdr's data are: | |
b5256303 TC |
2823 | * 1. the buffer is not encrypted |
2824 | * 2. the hdr's compression matches the buf's compression | |
2825 | * 3. the hdr doesn't need to be byteswapped | |
2826 | * 4. the hdr isn't already being shared | |
2827 | * 5. the buf is either compressed or it is the last buf in the hdr list | |
524b4217 | 2828 | * |
b5256303 | 2829 | * Criterion #5 maintains the invariant that shared uncompressed |
524b4217 DK |
2830 | * bufs must be the final buf in the hdr's b_buf list. Reading this, you |
2831 | * might ask, "if a compressed buf is allocated first, won't that be the | |
2832 | * last thing in the list?", but in that case it's impossible to create | |
2833 | * a shared uncompressed buf anyway (because the hdr must be compressed | |
2834 | * to have the compressed buf). You might also think that #3 is | |
2835 | * sufficient to make this guarantee, however it's possible | |
2836 | * (specifically in the rare L2ARC write race mentioned in | |
2837 | * arc_buf_alloc_impl()) there will be an existing uncompressed buf that | |
2838 | * is sharable, but wasn't at the time of its allocation. Rather than | |
2839 | * allow a new shared uncompressed buf to be created and then shuffle | |
2840 | * the list around to make it the last element, this simply disallows | |
2841 | * sharing if the new buf isn't the first to be added. | |
2842 | */ | |
2843 | ASSERT3P(buf->b_hdr, ==, hdr); | |
b5256303 TC |
2844 | boolean_t hdr_compressed = |
2845 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF; | |
a7004725 | 2846 | boolean_t buf_compressed = ARC_BUF_COMPRESSED(buf) != 0; |
b5256303 TC |
2847 | return (!ARC_BUF_ENCRYPTED(buf) && |
2848 | buf_compressed == hdr_compressed && | |
524b4217 DK |
2849 | hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS && |
2850 | !HDR_SHARED_DATA(hdr) && | |
2851 | (ARC_BUF_LAST(buf) || ARC_BUF_COMPRESSED(buf))); | |
2852 | } | |
2853 | ||
2854 | /* | |
2855 | * Allocate a buf for this hdr. If you care about the data that's in the hdr, | |
2856 | * or if you want a compressed buffer, pass those flags in. Returns 0 if the | |
2857 | * copy was made successfully, or an error code otherwise. | |
2858 | */ | |
2859 | static int | |
be9a5c35 TC |
2860 | arc_buf_alloc_impl(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb, |
2861 | void *tag, boolean_t encrypted, boolean_t compressed, boolean_t noauth, | |
524b4217 | 2862 | boolean_t fill, arc_buf_t **ret) |
34dc7c2f | 2863 | { |
34dc7c2f | 2864 | arc_buf_t *buf; |
b5256303 | 2865 | arc_fill_flags_t flags = ARC_FILL_LOCKED; |
34dc7c2f | 2866 | |
d3c2ae1c GW |
2867 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2868 | ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); | |
2869 | VERIFY(hdr->b_type == ARC_BUFC_DATA || | |
2870 | hdr->b_type == ARC_BUFC_METADATA); | |
524b4217 DK |
2871 | ASSERT3P(ret, !=, NULL); |
2872 | ASSERT3P(*ret, ==, NULL); | |
b5256303 | 2873 | IMPLY(encrypted, compressed); |
d3c2ae1c | 2874 | |
b9541d6b CW |
2875 | hdr->b_l1hdr.b_mru_hits = 0; |
2876 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
2877 | hdr->b_l1hdr.b_mfu_hits = 0; | |
2878 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
2879 | hdr->b_l1hdr.b_l2_hits = 0; | |
2880 | ||
524b4217 | 2881 | buf = *ret = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); |
34dc7c2f BB |
2882 | buf->b_hdr = hdr; |
2883 | buf->b_data = NULL; | |
2aa34383 | 2884 | buf->b_next = hdr->b_l1hdr.b_buf; |
524b4217 | 2885 | buf->b_flags = 0; |
b9541d6b | 2886 | |
d3c2ae1c GW |
2887 | add_reference(hdr, tag); |
2888 | ||
2889 | /* | |
2890 | * We're about to change the hdr's b_flags. We must either | |
2891 | * hold the hash_lock or be undiscoverable. | |
2892 | */ | |
2893 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); | |
2894 | ||
2895 | /* | |
524b4217 | 2896 | * Only honor requests for compressed bufs if the hdr is actually |
b5256303 TC |
2897 | * compressed. This must be overriden if the buffer is encrypted since |
2898 | * encrypted buffers cannot be decompressed. | |
524b4217 | 2899 | */ |
b5256303 TC |
2900 | if (encrypted) { |
2901 | buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; | |
2902 | buf->b_flags |= ARC_BUF_FLAG_ENCRYPTED; | |
2903 | flags |= ARC_FILL_COMPRESSED | ARC_FILL_ENCRYPTED; | |
2904 | } else if (compressed && | |
2905 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) { | |
524b4217 | 2906 | buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; |
b5256303 TC |
2907 | flags |= ARC_FILL_COMPRESSED; |
2908 | } | |
2909 | ||
2910 | if (noauth) { | |
2911 | ASSERT0(encrypted); | |
2912 | flags |= ARC_FILL_NOAUTH; | |
2913 | } | |
524b4217 | 2914 | |
524b4217 DK |
2915 | /* |
2916 | * If the hdr's data can be shared then we share the data buffer and | |
2917 | * set the appropriate bit in the hdr's b_flags to indicate the hdr is | |
2aa34383 | 2918 | * allocate a new buffer to store the buf's data. |
524b4217 | 2919 | * |
a6255b7f DQ |
2920 | * There are two additional restrictions here because we're sharing |
2921 | * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be | |
2922 | * actively involved in an L2ARC write, because if this buf is used by | |
2923 | * an arc_write() then the hdr's data buffer will be released when the | |
524b4217 | 2924 | * write completes, even though the L2ARC write might still be using it. |
a6255b7f DQ |
2925 | * Second, the hdr's ABD must be linear so that the buf's user doesn't |
2926 | * need to be ABD-aware. | |
d3c2ae1c | 2927 | */ |
a7004725 | 2928 | boolean_t can_share = arc_can_share(hdr, buf) && !HDR_L2_WRITING(hdr) && |
b5256303 | 2929 | hdr->b_l1hdr.b_pabd != NULL && abd_is_linear(hdr->b_l1hdr.b_pabd); |
524b4217 DK |
2930 | |
2931 | /* Set up b_data and sharing */ | |
2932 | if (can_share) { | |
a6255b7f | 2933 | buf->b_data = abd_to_buf(hdr->b_l1hdr.b_pabd); |
524b4217 | 2934 | buf->b_flags |= ARC_BUF_FLAG_SHARED; |
d3c2ae1c GW |
2935 | arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); |
2936 | } else { | |
524b4217 DK |
2937 | buf->b_data = |
2938 | arc_get_data_buf(hdr, arc_buf_size(buf), buf); | |
2939 | ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); | |
d3c2ae1c GW |
2940 | } |
2941 | VERIFY3P(buf->b_data, !=, NULL); | |
b9541d6b CW |
2942 | |
2943 | hdr->b_l1hdr.b_buf = buf; | |
d3c2ae1c | 2944 | hdr->b_l1hdr.b_bufcnt += 1; |
b5256303 TC |
2945 | if (encrypted) |
2946 | hdr->b_crypt_hdr.b_ebufcnt += 1; | |
b9541d6b | 2947 | |
524b4217 DK |
2948 | /* |
2949 | * If the user wants the data from the hdr, we need to either copy or | |
2950 | * decompress the data. | |
2951 | */ | |
2952 | if (fill) { | |
be9a5c35 TC |
2953 | ASSERT3P(zb, !=, NULL); |
2954 | return (arc_buf_fill(buf, spa, zb, flags)); | |
524b4217 | 2955 | } |
d3c2ae1c | 2956 | |
524b4217 | 2957 | return (0); |
34dc7c2f BB |
2958 | } |
2959 | ||
9babb374 BB |
2960 | static char *arc_onloan_tag = "onloan"; |
2961 | ||
a7004725 DK |
2962 | static inline void |
2963 | arc_loaned_bytes_update(int64_t delta) | |
2964 | { | |
2965 | atomic_add_64(&arc_loaned_bytes, delta); | |
2966 | ||
2967 | /* assert that it did not wrap around */ | |
2968 | ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); | |
2969 | } | |
2970 | ||
9babb374 BB |
2971 | /* |
2972 | * Loan out an anonymous arc buffer. Loaned buffers are not counted as in | |
2973 | * flight data by arc_tempreserve_space() until they are "returned". Loaned | |
2974 | * buffers must be returned to the arc before they can be used by the DMU or | |
2975 | * freed. | |
2976 | */ | |
2977 | arc_buf_t * | |
2aa34383 | 2978 | arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size) |
9babb374 | 2979 | { |
2aa34383 DK |
2980 | arc_buf_t *buf = arc_alloc_buf(spa, arc_onloan_tag, |
2981 | is_metadata ? ARC_BUFC_METADATA : ARC_BUFC_DATA, size); | |
9babb374 | 2982 | |
5152a740 | 2983 | arc_loaned_bytes_update(arc_buf_size(buf)); |
a7004725 | 2984 | |
9babb374 BB |
2985 | return (buf); |
2986 | } | |
2987 | ||
2aa34383 DK |
2988 | arc_buf_t * |
2989 | arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize, | |
2990 | enum zio_compress compression_type) | |
2991 | { | |
2992 | arc_buf_t *buf = arc_alloc_compressed_buf(spa, arc_onloan_tag, | |
2993 | psize, lsize, compression_type); | |
2994 | ||
5152a740 | 2995 | arc_loaned_bytes_update(arc_buf_size(buf)); |
a7004725 | 2996 | |
2aa34383 DK |
2997 | return (buf); |
2998 | } | |
2999 | ||
b5256303 TC |
3000 | arc_buf_t * |
3001 | arc_loan_raw_buf(spa_t *spa, uint64_t dsobj, boolean_t byteorder, | |
3002 | const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, | |
3003 | dmu_object_type_t ot, uint64_t psize, uint64_t lsize, | |
3004 | enum zio_compress compression_type) | |
3005 | { | |
3006 | arc_buf_t *buf = arc_alloc_raw_buf(spa, arc_onloan_tag, dsobj, | |
3007 | byteorder, salt, iv, mac, ot, psize, lsize, compression_type); | |
3008 | ||
3009 | atomic_add_64(&arc_loaned_bytes, psize); | |
3010 | return (buf); | |
3011 | } | |
3012 | ||
2aa34383 | 3013 | |
9babb374 BB |
3014 | /* |
3015 | * Return a loaned arc buffer to the arc. | |
3016 | */ | |
3017 | void | |
3018 | arc_return_buf(arc_buf_t *buf, void *tag) | |
3019 | { | |
3020 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
3021 | ||
d3c2ae1c | 3022 | ASSERT3P(buf->b_data, !=, NULL); |
b9541d6b | 3023 | ASSERT(HDR_HAS_L1HDR(hdr)); |
c13060e4 | 3024 | (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag); |
424fd7c3 | 3025 | (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); |
9babb374 | 3026 | |
a7004725 | 3027 | arc_loaned_bytes_update(-arc_buf_size(buf)); |
9babb374 BB |
3028 | } |
3029 | ||
428870ff BB |
3030 | /* Detach an arc_buf from a dbuf (tag) */ |
3031 | void | |
3032 | arc_loan_inuse_buf(arc_buf_t *buf, void *tag) | |
3033 | { | |
b9541d6b | 3034 | arc_buf_hdr_t *hdr = buf->b_hdr; |
428870ff | 3035 | |
d3c2ae1c | 3036 | ASSERT3P(buf->b_data, !=, NULL); |
b9541d6b | 3037 | ASSERT(HDR_HAS_L1HDR(hdr)); |
c13060e4 | 3038 | (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); |
424fd7c3 | 3039 | (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); |
428870ff | 3040 | |
a7004725 | 3041 | arc_loaned_bytes_update(arc_buf_size(buf)); |
428870ff BB |
3042 | } |
3043 | ||
d3c2ae1c | 3044 | static void |
a6255b7f | 3045 | l2arc_free_abd_on_write(abd_t *abd, size_t size, arc_buf_contents_t type) |
34dc7c2f | 3046 | { |
d3c2ae1c | 3047 | l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP); |
34dc7c2f | 3048 | |
a6255b7f | 3049 | df->l2df_abd = abd; |
d3c2ae1c GW |
3050 | df->l2df_size = size; |
3051 | df->l2df_type = type; | |
3052 | mutex_enter(&l2arc_free_on_write_mtx); | |
3053 | list_insert_head(l2arc_free_on_write, df); | |
3054 | mutex_exit(&l2arc_free_on_write_mtx); | |
3055 | } | |
428870ff | 3056 | |
d3c2ae1c | 3057 | static void |
b5256303 | 3058 | arc_hdr_free_on_write(arc_buf_hdr_t *hdr, boolean_t free_rdata) |
d3c2ae1c GW |
3059 | { |
3060 | arc_state_t *state = hdr->b_l1hdr.b_state; | |
3061 | arc_buf_contents_t type = arc_buf_type(hdr); | |
b5256303 | 3062 | uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr); |
1eb5bfa3 | 3063 | |
d3c2ae1c GW |
3064 | /* protected by hash lock, if in the hash table */ |
3065 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 | 3066 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
3067 | ASSERT(state != arc_anon && state != arc_l2c_only); |
3068 | ||
424fd7c3 | 3069 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c | 3070 | size, hdr); |
1eb5bfa3 | 3071 | } |
424fd7c3 | 3072 | (void) zfs_refcount_remove_many(&state->arcs_size, size, hdr); |
423e7b62 AG |
3073 | if (type == ARC_BUFC_METADATA) { |
3074 | arc_space_return(size, ARC_SPACE_META); | |
3075 | } else { | |
3076 | ASSERT(type == ARC_BUFC_DATA); | |
3077 | arc_space_return(size, ARC_SPACE_DATA); | |
3078 | } | |
d3c2ae1c | 3079 | |
b5256303 TC |
3080 | if (free_rdata) { |
3081 | l2arc_free_abd_on_write(hdr->b_crypt_hdr.b_rabd, size, type); | |
3082 | } else { | |
3083 | l2arc_free_abd_on_write(hdr->b_l1hdr.b_pabd, size, type); | |
3084 | } | |
34dc7c2f BB |
3085 | } |
3086 | ||
d3c2ae1c GW |
3087 | /* |
3088 | * Share the arc_buf_t's data with the hdr. Whenever we are sharing the | |
3089 | * data buffer, we transfer the refcount ownership to the hdr and update | |
3090 | * the appropriate kstats. | |
3091 | */ | |
3092 | static void | |
3093 | arc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
34dc7c2f | 3094 | { |
524b4217 | 3095 | ASSERT(arc_can_share(hdr, buf)); |
a6255b7f | 3096 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 3097 | ASSERT(!ARC_BUF_ENCRYPTED(buf)); |
d3c2ae1c | 3098 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); |
34dc7c2f BB |
3099 | |
3100 | /* | |
d3c2ae1c GW |
3101 | * Start sharing the data buffer. We transfer the |
3102 | * refcount ownership to the hdr since it always owns | |
3103 | * the refcount whenever an arc_buf_t is shared. | |
34dc7c2f | 3104 | */ |
d7e4b30a BB |
3105 | zfs_refcount_transfer_ownership_many(&hdr->b_l1hdr.b_state->arcs_size, |
3106 | arc_hdr_size(hdr), buf, hdr); | |
a6255b7f DQ |
3107 | hdr->b_l1hdr.b_pabd = abd_get_from_buf(buf->b_data, arc_buf_size(buf)); |
3108 | abd_take_ownership_of_buf(hdr->b_l1hdr.b_pabd, | |
3109 | HDR_ISTYPE_METADATA(hdr)); | |
d3c2ae1c | 3110 | arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); |
524b4217 | 3111 | buf->b_flags |= ARC_BUF_FLAG_SHARED; |
34dc7c2f | 3112 | |
d3c2ae1c GW |
3113 | /* |
3114 | * Since we've transferred ownership to the hdr we need | |
3115 | * to increment its compressed and uncompressed kstats and | |
3116 | * decrement the overhead size. | |
3117 | */ | |
3118 | ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); | |
3119 | ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); | |
2aa34383 | 3120 | ARCSTAT_INCR(arcstat_overhead_size, -arc_buf_size(buf)); |
34dc7c2f BB |
3121 | } |
3122 | ||
ca0bf58d | 3123 | static void |
d3c2ae1c | 3124 | arc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) |
ca0bf58d | 3125 | { |
d3c2ae1c | 3126 | ASSERT(arc_buf_is_shared(buf)); |
a6255b7f | 3127 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
d3c2ae1c | 3128 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); |
ca0bf58d | 3129 | |
d3c2ae1c GW |
3130 | /* |
3131 | * We are no longer sharing this buffer so we need | |
3132 | * to transfer its ownership to the rightful owner. | |
3133 | */ | |
d7e4b30a BB |
3134 | zfs_refcount_transfer_ownership_many(&hdr->b_l1hdr.b_state->arcs_size, |
3135 | arc_hdr_size(hdr), hdr, buf); | |
d3c2ae1c | 3136 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); |
a6255b7f DQ |
3137 | abd_release_ownership_of_buf(hdr->b_l1hdr.b_pabd); |
3138 | abd_put(hdr->b_l1hdr.b_pabd); | |
3139 | hdr->b_l1hdr.b_pabd = NULL; | |
524b4217 | 3140 | buf->b_flags &= ~ARC_BUF_FLAG_SHARED; |
d3c2ae1c GW |
3141 | |
3142 | /* | |
3143 | * Since the buffer is no longer shared between | |
3144 | * the arc buf and the hdr, count it as overhead. | |
3145 | */ | |
3146 | ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); | |
3147 | ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); | |
2aa34383 | 3148 | ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); |
ca0bf58d PS |
3149 | } |
3150 | ||
34dc7c2f | 3151 | /* |
2aa34383 DK |
3152 | * Remove an arc_buf_t from the hdr's buf list and return the last |
3153 | * arc_buf_t on the list. If no buffers remain on the list then return | |
3154 | * NULL. | |
3155 | */ | |
3156 | static arc_buf_t * | |
3157 | arc_buf_remove(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
3158 | { | |
2aa34383 DK |
3159 | ASSERT(HDR_HAS_L1HDR(hdr)); |
3160 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); | |
3161 | ||
a7004725 DK |
3162 | arc_buf_t **bufp = &hdr->b_l1hdr.b_buf; |
3163 | arc_buf_t *lastbuf = NULL; | |
3164 | ||
2aa34383 DK |
3165 | /* |
3166 | * Remove the buf from the hdr list and locate the last | |
3167 | * remaining buffer on the list. | |
3168 | */ | |
3169 | while (*bufp != NULL) { | |
3170 | if (*bufp == buf) | |
3171 | *bufp = buf->b_next; | |
3172 | ||
3173 | /* | |
3174 | * If we've removed a buffer in the middle of | |
3175 | * the list then update the lastbuf and update | |
3176 | * bufp. | |
3177 | */ | |
3178 | if (*bufp != NULL) { | |
3179 | lastbuf = *bufp; | |
3180 | bufp = &(*bufp)->b_next; | |
3181 | } | |
3182 | } | |
3183 | buf->b_next = NULL; | |
3184 | ASSERT3P(lastbuf, !=, buf); | |
3185 | IMPLY(hdr->b_l1hdr.b_bufcnt > 0, lastbuf != NULL); | |
3186 | IMPLY(hdr->b_l1hdr.b_bufcnt > 0, hdr->b_l1hdr.b_buf != NULL); | |
3187 | IMPLY(lastbuf != NULL, ARC_BUF_LAST(lastbuf)); | |
3188 | ||
3189 | return (lastbuf); | |
3190 | } | |
3191 | ||
3192 | /* | |
3193 | * Free up buf->b_data and pull the arc_buf_t off of the the arc_buf_hdr_t's | |
3194 | * list and free it. | |
34dc7c2f BB |
3195 | */ |
3196 | static void | |
2aa34383 | 3197 | arc_buf_destroy_impl(arc_buf_t *buf) |
34dc7c2f | 3198 | { |
498877ba | 3199 | arc_buf_hdr_t *hdr = buf->b_hdr; |
ca0bf58d PS |
3200 | |
3201 | /* | |
524b4217 DK |
3202 | * Free up the data associated with the buf but only if we're not |
3203 | * sharing this with the hdr. If we are sharing it with the hdr, the | |
3204 | * hdr is responsible for doing the free. | |
ca0bf58d | 3205 | */ |
d3c2ae1c GW |
3206 | if (buf->b_data != NULL) { |
3207 | /* | |
3208 | * We're about to change the hdr's b_flags. We must either | |
3209 | * hold the hash_lock or be undiscoverable. | |
3210 | */ | |
3211 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr)) || HDR_EMPTY(hdr)); | |
3212 | ||
524b4217 | 3213 | arc_cksum_verify(buf); |
d3c2ae1c GW |
3214 | arc_buf_unwatch(buf); |
3215 | ||
2aa34383 | 3216 | if (arc_buf_is_shared(buf)) { |
d3c2ae1c GW |
3217 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); |
3218 | } else { | |
2aa34383 | 3219 | uint64_t size = arc_buf_size(buf); |
d3c2ae1c GW |
3220 | arc_free_data_buf(hdr, buf->b_data, size, buf); |
3221 | ARCSTAT_INCR(arcstat_overhead_size, -size); | |
3222 | } | |
3223 | buf->b_data = NULL; | |
3224 | ||
3225 | ASSERT(hdr->b_l1hdr.b_bufcnt > 0); | |
3226 | hdr->b_l1hdr.b_bufcnt -= 1; | |
b5256303 | 3227 | |
da5d4697 | 3228 | if (ARC_BUF_ENCRYPTED(buf)) { |
b5256303 TC |
3229 | hdr->b_crypt_hdr.b_ebufcnt -= 1; |
3230 | ||
da5d4697 D |
3231 | /* |
3232 | * If we have no more encrypted buffers and we've | |
3233 | * already gotten a copy of the decrypted data we can | |
3234 | * free b_rabd to save some space. | |
3235 | */ | |
3236 | if (hdr->b_crypt_hdr.b_ebufcnt == 0 && | |
3237 | HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd != NULL && | |
3238 | !HDR_IO_IN_PROGRESS(hdr)) { | |
3239 | arc_hdr_free_abd(hdr, B_TRUE); | |
3240 | } | |
440a3eb9 | 3241 | } |
d3c2ae1c GW |
3242 | } |
3243 | ||
a7004725 | 3244 | arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); |
d3c2ae1c | 3245 | |
524b4217 | 3246 | if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) { |
2aa34383 | 3247 | /* |
524b4217 | 3248 | * If the current arc_buf_t is sharing its data buffer with the |
a6255b7f | 3249 | * hdr, then reassign the hdr's b_pabd to share it with the new |
524b4217 DK |
3250 | * buffer at the end of the list. The shared buffer is always |
3251 | * the last one on the hdr's buffer list. | |
3252 | * | |
3253 | * There is an equivalent case for compressed bufs, but since | |
3254 | * they aren't guaranteed to be the last buf in the list and | |
3255 | * that is an exceedingly rare case, we just allow that space be | |
b5256303 TC |
3256 | * wasted temporarily. We must also be careful not to share |
3257 | * encrypted buffers, since they cannot be shared. | |
2aa34383 | 3258 | */ |
b5256303 | 3259 | if (lastbuf != NULL && !ARC_BUF_ENCRYPTED(lastbuf)) { |
524b4217 | 3260 | /* Only one buf can be shared at once */ |
2aa34383 | 3261 | VERIFY(!arc_buf_is_shared(lastbuf)); |
524b4217 DK |
3262 | /* hdr is uncompressed so can't have compressed buf */ |
3263 | VERIFY(!ARC_BUF_COMPRESSED(lastbuf)); | |
d3c2ae1c | 3264 | |
a6255b7f | 3265 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
b5256303 | 3266 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c | 3267 | |
2aa34383 DK |
3268 | /* |
3269 | * We must setup a new shared block between the | |
3270 | * last buffer and the hdr. The data would have | |
3271 | * been allocated by the arc buf so we need to transfer | |
3272 | * ownership to the hdr since it's now being shared. | |
3273 | */ | |
3274 | arc_share_buf(hdr, lastbuf); | |
3275 | } | |
3276 | } else if (HDR_SHARED_DATA(hdr)) { | |
d3c2ae1c | 3277 | /* |
2aa34383 DK |
3278 | * Uncompressed shared buffers are always at the end |
3279 | * of the list. Compressed buffers don't have the | |
3280 | * same requirements. This makes it hard to | |
3281 | * simply assert that the lastbuf is shared so | |
3282 | * we rely on the hdr's compression flags to determine | |
3283 | * if we have a compressed, shared buffer. | |
d3c2ae1c | 3284 | */ |
2aa34383 DK |
3285 | ASSERT3P(lastbuf, !=, NULL); |
3286 | ASSERT(arc_buf_is_shared(lastbuf) || | |
b5256303 | 3287 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); |
ca0bf58d PS |
3288 | } |
3289 | ||
a7004725 DK |
3290 | /* |
3291 | * Free the checksum if we're removing the last uncompressed buf from | |
3292 | * this hdr. | |
3293 | */ | |
3294 | if (!arc_hdr_has_uncompressed_buf(hdr)) { | |
d3c2ae1c | 3295 | arc_cksum_free(hdr); |
a7004725 | 3296 | } |
d3c2ae1c GW |
3297 | |
3298 | /* clean up the buf */ | |
3299 | buf->b_hdr = NULL; | |
3300 | kmem_cache_free(buf_cache, buf); | |
3301 | } | |
3302 | ||
3303 | static void | |
b5256303 | 3304 | arc_hdr_alloc_abd(arc_buf_hdr_t *hdr, boolean_t alloc_rdata) |
d3c2ae1c | 3305 | { |
b5256303 TC |
3306 | uint64_t size; |
3307 | ||
d3c2ae1c GW |
3308 | ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); |
3309 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
b5256303 TC |
3310 | ASSERT(!HDR_SHARED_DATA(hdr) || alloc_rdata); |
3311 | IMPLY(alloc_rdata, HDR_PROTECTED(hdr)); | |
d3c2ae1c | 3312 | |
b5256303 TC |
3313 | if (alloc_rdata) { |
3314 | size = HDR_GET_PSIZE(hdr); | |
3315 | ASSERT3P(hdr->b_crypt_hdr.b_rabd, ==, NULL); | |
3316 | hdr->b_crypt_hdr.b_rabd = arc_get_data_abd(hdr, size, hdr); | |
3317 | ASSERT3P(hdr->b_crypt_hdr.b_rabd, !=, NULL); | |
3318 | ARCSTAT_INCR(arcstat_raw_size, size); | |
3319 | } else { | |
3320 | size = arc_hdr_size(hdr); | |
3321 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); | |
3322 | hdr->b_l1hdr.b_pabd = arc_get_data_abd(hdr, size, hdr); | |
3323 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
3324 | } | |
3325 | ||
3326 | ARCSTAT_INCR(arcstat_compressed_size, size); | |
d3c2ae1c GW |
3327 | ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); |
3328 | } | |
3329 | ||
3330 | static void | |
b5256303 | 3331 | arc_hdr_free_abd(arc_buf_hdr_t *hdr, boolean_t free_rdata) |
d3c2ae1c | 3332 | { |
b5256303 TC |
3333 | uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr); |
3334 | ||
d3c2ae1c | 3335 | ASSERT(HDR_HAS_L1HDR(hdr)); |
b5256303 TC |
3336 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); |
3337 | IMPLY(free_rdata, HDR_HAS_RABD(hdr)); | |
d3c2ae1c | 3338 | |
ca0bf58d | 3339 | /* |
d3c2ae1c GW |
3340 | * If the hdr is currently being written to the l2arc then |
3341 | * we defer freeing the data by adding it to the l2arc_free_on_write | |
3342 | * list. The l2arc will free the data once it's finished | |
3343 | * writing it to the l2arc device. | |
ca0bf58d | 3344 | */ |
d3c2ae1c | 3345 | if (HDR_L2_WRITING(hdr)) { |
b5256303 | 3346 | arc_hdr_free_on_write(hdr, free_rdata); |
d3c2ae1c | 3347 | ARCSTAT_BUMP(arcstat_l2_free_on_write); |
b5256303 TC |
3348 | } else if (free_rdata) { |
3349 | arc_free_data_abd(hdr, hdr->b_crypt_hdr.b_rabd, size, hdr); | |
d3c2ae1c | 3350 | } else { |
b5256303 | 3351 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, size, hdr); |
ca0bf58d PS |
3352 | } |
3353 | ||
b5256303 TC |
3354 | if (free_rdata) { |
3355 | hdr->b_crypt_hdr.b_rabd = NULL; | |
3356 | ARCSTAT_INCR(arcstat_raw_size, -size); | |
3357 | } else { | |
3358 | hdr->b_l1hdr.b_pabd = NULL; | |
3359 | } | |
3360 | ||
3361 | if (hdr->b_l1hdr.b_pabd == NULL && !HDR_HAS_RABD(hdr)) | |
3362 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
3363 | ||
3364 | ARCSTAT_INCR(arcstat_compressed_size, -size); | |
d3c2ae1c GW |
3365 | ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); |
3366 | } | |
3367 | ||
3368 | static arc_buf_hdr_t * | |
3369 | arc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize, | |
b5256303 TC |
3370 | boolean_t protected, enum zio_compress compression_type, |
3371 | arc_buf_contents_t type, boolean_t alloc_rdata) | |
d3c2ae1c GW |
3372 | { |
3373 | arc_buf_hdr_t *hdr; | |
3374 | ||
d3c2ae1c | 3375 | VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA); |
b5256303 TC |
3376 | if (protected) { |
3377 | hdr = kmem_cache_alloc(hdr_full_crypt_cache, KM_PUSHPAGE); | |
3378 | } else { | |
3379 | hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); | |
3380 | } | |
d3c2ae1c | 3381 | |
d3c2ae1c GW |
3382 | ASSERT(HDR_EMPTY(hdr)); |
3383 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
3384 | HDR_SET_PSIZE(hdr, psize); | |
3385 | HDR_SET_LSIZE(hdr, lsize); | |
3386 | hdr->b_spa = spa; | |
3387 | hdr->b_type = type; | |
3388 | hdr->b_flags = 0; | |
3389 | arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR); | |
2aa34383 | 3390 | arc_hdr_set_compress(hdr, compression_type); |
b5256303 TC |
3391 | if (protected) |
3392 | arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED); | |
ca0bf58d | 3393 | |
d3c2ae1c GW |
3394 | hdr->b_l1hdr.b_state = arc_anon; |
3395 | hdr->b_l1hdr.b_arc_access = 0; | |
3396 | hdr->b_l1hdr.b_bufcnt = 0; | |
3397 | hdr->b_l1hdr.b_buf = NULL; | |
ca0bf58d | 3398 | |
d3c2ae1c GW |
3399 | /* |
3400 | * Allocate the hdr's buffer. This will contain either | |
3401 | * the compressed or uncompressed data depending on the block | |
3402 | * it references and compressed arc enablement. | |
3403 | */ | |
b5256303 | 3404 | arc_hdr_alloc_abd(hdr, alloc_rdata); |
424fd7c3 | 3405 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
ca0bf58d | 3406 | |
d3c2ae1c | 3407 | return (hdr); |
ca0bf58d PS |
3408 | } |
3409 | ||
bd089c54 | 3410 | /* |
d3c2ae1c GW |
3411 | * Transition between the two allocation states for the arc_buf_hdr struct. |
3412 | * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without | |
3413 | * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller | |
3414 | * version is used when a cache buffer is only in the L2ARC in order to reduce | |
3415 | * memory usage. | |
bd089c54 | 3416 | */ |
d3c2ae1c GW |
3417 | static arc_buf_hdr_t * |
3418 | arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) | |
34dc7c2f | 3419 | { |
1c27024e DB |
3420 | ASSERT(HDR_HAS_L2HDR(hdr)); |
3421 | ||
d3c2ae1c GW |
3422 | arc_buf_hdr_t *nhdr; |
3423 | l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; | |
34dc7c2f | 3424 | |
d3c2ae1c GW |
3425 | ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || |
3426 | (old == hdr_l2only_cache && new == hdr_full_cache)); | |
34dc7c2f | 3427 | |
b5256303 TC |
3428 | /* |
3429 | * if the caller wanted a new full header and the header is to be | |
3430 | * encrypted we will actually allocate the header from the full crypt | |
3431 | * cache instead. The same applies to freeing from the old cache. | |
3432 | */ | |
3433 | if (HDR_PROTECTED(hdr) && new == hdr_full_cache) | |
3434 | new = hdr_full_crypt_cache; | |
3435 | if (HDR_PROTECTED(hdr) && old == hdr_full_cache) | |
3436 | old = hdr_full_crypt_cache; | |
3437 | ||
d3c2ae1c | 3438 | nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); |
428870ff | 3439 | |
d3c2ae1c GW |
3440 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); |
3441 | buf_hash_remove(hdr); | |
ca0bf58d | 3442 | |
d3c2ae1c | 3443 | bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); |
34dc7c2f | 3444 | |
b5256303 | 3445 | if (new == hdr_full_cache || new == hdr_full_crypt_cache) { |
d3c2ae1c GW |
3446 | arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR); |
3447 | /* | |
3448 | * arc_access and arc_change_state need to be aware that a | |
3449 | * header has just come out of L2ARC, so we set its state to | |
3450 | * l2c_only even though it's about to change. | |
3451 | */ | |
3452 | nhdr->b_l1hdr.b_state = arc_l2c_only; | |
34dc7c2f | 3453 | |
d3c2ae1c | 3454 | /* Verify previous threads set to NULL before freeing */ |
a6255b7f | 3455 | ASSERT3P(nhdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 3456 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
3457 | } else { |
3458 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
3459 | ASSERT0(hdr->b_l1hdr.b_bufcnt); | |
3460 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
36da08ef | 3461 | |
d3c2ae1c GW |
3462 | /* |
3463 | * If we've reached here, We must have been called from | |
3464 | * arc_evict_hdr(), as such we should have already been | |
3465 | * removed from any ghost list we were previously on | |
3466 | * (which protects us from racing with arc_evict_state), | |
3467 | * thus no locking is needed during this check. | |
3468 | */ | |
3469 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
1eb5bfa3 GW |
3470 | |
3471 | /* | |
d3c2ae1c GW |
3472 | * A buffer must not be moved into the arc_l2c_only |
3473 | * state if it's not finished being written out to the | |
a6255b7f | 3474 | * l2arc device. Otherwise, the b_l1hdr.b_pabd field |
d3c2ae1c | 3475 | * might try to be accessed, even though it was removed. |
1eb5bfa3 | 3476 | */ |
d3c2ae1c | 3477 | VERIFY(!HDR_L2_WRITING(hdr)); |
a6255b7f | 3478 | VERIFY3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 3479 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
3480 | |
3481 | arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR); | |
34dc7c2f | 3482 | } |
d3c2ae1c GW |
3483 | /* |
3484 | * The header has been reallocated so we need to re-insert it into any | |
3485 | * lists it was on. | |
3486 | */ | |
3487 | (void) buf_hash_insert(nhdr, NULL); | |
34dc7c2f | 3488 | |
d3c2ae1c | 3489 | ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); |
34dc7c2f | 3490 | |
d3c2ae1c GW |
3491 | mutex_enter(&dev->l2ad_mtx); |
3492 | ||
3493 | /* | |
3494 | * We must place the realloc'ed header back into the list at | |
3495 | * the same spot. Otherwise, if it's placed earlier in the list, | |
3496 | * l2arc_write_buffers() could find it during the function's | |
3497 | * write phase, and try to write it out to the l2arc. | |
3498 | */ | |
3499 | list_insert_after(&dev->l2ad_buflist, hdr, nhdr); | |
3500 | list_remove(&dev->l2ad_buflist, hdr); | |
34dc7c2f | 3501 | |
d3c2ae1c | 3502 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 3503 | |
d3c2ae1c GW |
3504 | /* |
3505 | * Since we're using the pointer address as the tag when | |
3506 | * incrementing and decrementing the l2ad_alloc refcount, we | |
3507 | * must remove the old pointer (that we're about to destroy) and | |
3508 | * add the new pointer to the refcount. Otherwise we'd remove | |
3509 | * the wrong pointer address when calling arc_hdr_destroy() later. | |
3510 | */ | |
3511 | ||
424fd7c3 TS |
3512 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, |
3513 | arc_hdr_size(hdr), hdr); | |
3514 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, | |
3515 | arc_hdr_size(nhdr), nhdr); | |
d3c2ae1c GW |
3516 | |
3517 | buf_discard_identity(hdr); | |
3518 | kmem_cache_free(old, hdr); | |
3519 | ||
3520 | return (nhdr); | |
3521 | } | |
3522 | ||
b5256303 TC |
3523 | /* |
3524 | * This function allows an L1 header to be reallocated as a crypt | |
3525 | * header and vice versa. If we are going to a crypt header, the | |
3526 | * new fields will be zeroed out. | |
3527 | */ | |
3528 | static arc_buf_hdr_t * | |
3529 | arc_hdr_realloc_crypt(arc_buf_hdr_t *hdr, boolean_t need_crypt) | |
3530 | { | |
3531 | arc_buf_hdr_t *nhdr; | |
3532 | arc_buf_t *buf; | |
3533 | kmem_cache_t *ncache, *ocache; | |
b7ddeaef | 3534 | unsigned nsize, osize; |
b5256303 | 3535 | |
b7ddeaef TC |
3536 | /* |
3537 | * This function requires that hdr is in the arc_anon state. | |
3538 | * Therefore it won't have any L2ARC data for us to worry | |
3539 | * about copying. | |
3540 | */ | |
b5256303 | 3541 | ASSERT(HDR_HAS_L1HDR(hdr)); |
b7ddeaef | 3542 | ASSERT(!HDR_HAS_L2HDR(hdr)); |
b5256303 TC |
3543 | ASSERT3U(!!HDR_PROTECTED(hdr), !=, need_crypt); |
3544 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); | |
3545 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
b7ddeaef TC |
3546 | ASSERT(!list_link_active(&hdr->b_l2hdr.b_l2node)); |
3547 | ASSERT3P(hdr->b_hash_next, ==, NULL); | |
b5256303 TC |
3548 | |
3549 | if (need_crypt) { | |
3550 | ncache = hdr_full_crypt_cache; | |
b7ddeaef | 3551 | nsize = sizeof (hdr->b_crypt_hdr); |
b5256303 | 3552 | ocache = hdr_full_cache; |
b7ddeaef | 3553 | osize = HDR_FULL_SIZE; |
b5256303 TC |
3554 | } else { |
3555 | ncache = hdr_full_cache; | |
b7ddeaef | 3556 | nsize = HDR_FULL_SIZE; |
b5256303 | 3557 | ocache = hdr_full_crypt_cache; |
b7ddeaef | 3558 | osize = sizeof (hdr->b_crypt_hdr); |
b5256303 TC |
3559 | } |
3560 | ||
3561 | nhdr = kmem_cache_alloc(ncache, KM_PUSHPAGE); | |
b7ddeaef TC |
3562 | |
3563 | /* | |
3564 | * Copy all members that aren't locks or condvars to the new header. | |
3565 | * No lists are pointing to us (as we asserted above), so we don't | |
3566 | * need to worry about the list nodes. | |
3567 | */ | |
3568 | nhdr->b_dva = hdr->b_dva; | |
3569 | nhdr->b_birth = hdr->b_birth; | |
3570 | nhdr->b_type = hdr->b_type; | |
3571 | nhdr->b_flags = hdr->b_flags; | |
3572 | nhdr->b_psize = hdr->b_psize; | |
3573 | nhdr->b_lsize = hdr->b_lsize; | |
3574 | nhdr->b_spa = hdr->b_spa; | |
b5256303 TC |
3575 | nhdr->b_l1hdr.b_freeze_cksum = hdr->b_l1hdr.b_freeze_cksum; |
3576 | nhdr->b_l1hdr.b_bufcnt = hdr->b_l1hdr.b_bufcnt; | |
3577 | nhdr->b_l1hdr.b_byteswap = hdr->b_l1hdr.b_byteswap; | |
3578 | nhdr->b_l1hdr.b_state = hdr->b_l1hdr.b_state; | |
3579 | nhdr->b_l1hdr.b_arc_access = hdr->b_l1hdr.b_arc_access; | |
3580 | nhdr->b_l1hdr.b_mru_hits = hdr->b_l1hdr.b_mru_hits; | |
3581 | nhdr->b_l1hdr.b_mru_ghost_hits = hdr->b_l1hdr.b_mru_ghost_hits; | |
3582 | nhdr->b_l1hdr.b_mfu_hits = hdr->b_l1hdr.b_mfu_hits; | |
3583 | nhdr->b_l1hdr.b_mfu_ghost_hits = hdr->b_l1hdr.b_mfu_ghost_hits; | |
3584 | nhdr->b_l1hdr.b_l2_hits = hdr->b_l1hdr.b_l2_hits; | |
3585 | nhdr->b_l1hdr.b_acb = hdr->b_l1hdr.b_acb; | |
3586 | nhdr->b_l1hdr.b_pabd = hdr->b_l1hdr.b_pabd; | |
b5256303 TC |
3587 | |
3588 | /* | |
c13060e4 | 3589 | * This zfs_refcount_add() exists only to ensure that the individual |
b5256303 TC |
3590 | * arc buffers always point to a header that is referenced, avoiding |
3591 | * a small race condition that could trigger ASSERTs. | |
3592 | */ | |
c13060e4 | 3593 | (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, FTAG); |
b7ddeaef | 3594 | nhdr->b_l1hdr.b_buf = hdr->b_l1hdr.b_buf; |
b5256303 TC |
3595 | for (buf = nhdr->b_l1hdr.b_buf; buf != NULL; buf = buf->b_next) { |
3596 | mutex_enter(&buf->b_evict_lock); | |
3597 | buf->b_hdr = nhdr; | |
3598 | mutex_exit(&buf->b_evict_lock); | |
3599 | } | |
3600 | ||
424fd7c3 TS |
3601 | zfs_refcount_transfer(&nhdr->b_l1hdr.b_refcnt, &hdr->b_l1hdr.b_refcnt); |
3602 | (void) zfs_refcount_remove(&nhdr->b_l1hdr.b_refcnt, FTAG); | |
3603 | ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt)); | |
b5256303 TC |
3604 | |
3605 | if (need_crypt) { | |
3606 | arc_hdr_set_flags(nhdr, ARC_FLAG_PROTECTED); | |
3607 | } else { | |
3608 | arc_hdr_clear_flags(nhdr, ARC_FLAG_PROTECTED); | |
3609 | } | |
3610 | ||
b7ddeaef TC |
3611 | /* unset all members of the original hdr */ |
3612 | bzero(&hdr->b_dva, sizeof (dva_t)); | |
3613 | hdr->b_birth = 0; | |
3614 | hdr->b_type = ARC_BUFC_INVALID; | |
3615 | hdr->b_flags = 0; | |
3616 | hdr->b_psize = 0; | |
3617 | hdr->b_lsize = 0; | |
3618 | hdr->b_spa = 0; | |
3619 | hdr->b_l1hdr.b_freeze_cksum = NULL; | |
3620 | hdr->b_l1hdr.b_buf = NULL; | |
3621 | hdr->b_l1hdr.b_bufcnt = 0; | |
3622 | hdr->b_l1hdr.b_byteswap = 0; | |
3623 | hdr->b_l1hdr.b_state = NULL; | |
3624 | hdr->b_l1hdr.b_arc_access = 0; | |
3625 | hdr->b_l1hdr.b_mru_hits = 0; | |
3626 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
3627 | hdr->b_l1hdr.b_mfu_hits = 0; | |
3628 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
3629 | hdr->b_l1hdr.b_l2_hits = 0; | |
3630 | hdr->b_l1hdr.b_acb = NULL; | |
3631 | hdr->b_l1hdr.b_pabd = NULL; | |
3632 | ||
3633 | if (ocache == hdr_full_crypt_cache) { | |
3634 | ASSERT(!HDR_HAS_RABD(hdr)); | |
3635 | hdr->b_crypt_hdr.b_ot = DMU_OT_NONE; | |
3636 | hdr->b_crypt_hdr.b_ebufcnt = 0; | |
3637 | hdr->b_crypt_hdr.b_dsobj = 0; | |
3638 | bzero(hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); | |
3639 | bzero(hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); | |
3640 | bzero(hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); | |
3641 | } | |
3642 | ||
b5256303 TC |
3643 | buf_discard_identity(hdr); |
3644 | kmem_cache_free(ocache, hdr); | |
3645 | ||
3646 | return (nhdr); | |
3647 | } | |
3648 | ||
3649 | /* | |
3650 | * This function is used by the send / receive code to convert a newly | |
3651 | * allocated arc_buf_t to one that is suitable for a raw encrypted write. It | |
3652 | * is also used to allow the root objset block to be uupdated without altering | |
3653 | * its embedded MACs. Both block types will always be uncompressed so we do not | |
3654 | * have to worry about compression type or psize. | |
3655 | */ | |
3656 | void | |
3657 | arc_convert_to_raw(arc_buf_t *buf, uint64_t dsobj, boolean_t byteorder, | |
3658 | dmu_object_type_t ot, const uint8_t *salt, const uint8_t *iv, | |
3659 | const uint8_t *mac) | |
3660 | { | |
3661 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
3662 | ||
3663 | ASSERT(ot == DMU_OT_DNODE || ot == DMU_OT_OBJSET); | |
3664 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
3665 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); | |
3666 | ||
3667 | buf->b_flags |= (ARC_BUF_FLAG_COMPRESSED | ARC_BUF_FLAG_ENCRYPTED); | |
3668 | if (!HDR_PROTECTED(hdr)) | |
3669 | hdr = arc_hdr_realloc_crypt(hdr, B_TRUE); | |
3670 | hdr->b_crypt_hdr.b_dsobj = dsobj; | |
3671 | hdr->b_crypt_hdr.b_ot = ot; | |
3672 | hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ? | |
3673 | DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot); | |
3674 | if (!arc_hdr_has_uncompressed_buf(hdr)) | |
3675 | arc_cksum_free(hdr); | |
3676 | ||
3677 | if (salt != NULL) | |
3678 | bcopy(salt, hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); | |
3679 | if (iv != NULL) | |
3680 | bcopy(iv, hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); | |
3681 | if (mac != NULL) | |
3682 | bcopy(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); | |
3683 | } | |
3684 | ||
d3c2ae1c GW |
3685 | /* |
3686 | * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller. | |
3687 | * The buf is returned thawed since we expect the consumer to modify it. | |
3688 | */ | |
3689 | arc_buf_t * | |
2aa34383 | 3690 | arc_alloc_buf(spa_t *spa, void *tag, arc_buf_contents_t type, int32_t size) |
d3c2ae1c | 3691 | { |
d3c2ae1c | 3692 | arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size, |
b5256303 | 3693 | B_FALSE, ZIO_COMPRESS_OFF, type, B_FALSE); |
d3c2ae1c | 3694 | ASSERT(!MUTEX_HELD(HDR_LOCK(hdr))); |
2aa34383 | 3695 | |
a7004725 | 3696 | arc_buf_t *buf = NULL; |
be9a5c35 | 3697 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, B_FALSE, |
b5256303 | 3698 | B_FALSE, B_FALSE, &buf)); |
d3c2ae1c | 3699 | arc_buf_thaw(buf); |
2aa34383 DK |
3700 | |
3701 | return (buf); | |
3702 | } | |
3703 | ||
3704 | /* | |
3705 | * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this | |
3706 | * for bufs containing metadata. | |
3707 | */ | |
3708 | arc_buf_t * | |
3709 | arc_alloc_compressed_buf(spa_t *spa, void *tag, uint64_t psize, uint64_t lsize, | |
3710 | enum zio_compress compression_type) | |
3711 | { | |
2aa34383 DK |
3712 | ASSERT3U(lsize, >, 0); |
3713 | ASSERT3U(lsize, >=, psize); | |
b5256303 TC |
3714 | ASSERT3U(compression_type, >, ZIO_COMPRESS_OFF); |
3715 | ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS); | |
2aa34383 | 3716 | |
a7004725 | 3717 | arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, |
b5256303 | 3718 | B_FALSE, compression_type, ARC_BUFC_DATA, B_FALSE); |
2aa34383 DK |
3719 | ASSERT(!MUTEX_HELD(HDR_LOCK(hdr))); |
3720 | ||
a7004725 | 3721 | arc_buf_t *buf = NULL; |
be9a5c35 | 3722 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, |
b5256303 | 3723 | B_TRUE, B_FALSE, B_FALSE, &buf)); |
2aa34383 DK |
3724 | arc_buf_thaw(buf); |
3725 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
3726 | ||
a6255b7f DQ |
3727 | if (!arc_buf_is_shared(buf)) { |
3728 | /* | |
3729 | * To ensure that the hdr has the correct data in it if we call | |
b5256303 | 3730 | * arc_untransform() on this buf before it's been written to |
a6255b7f DQ |
3731 | * disk, it's easiest if we just set up sharing between the |
3732 | * buf and the hdr. | |
3733 | */ | |
3734 | ASSERT(!abd_is_linear(hdr->b_l1hdr.b_pabd)); | |
b5256303 | 3735 | arc_hdr_free_abd(hdr, B_FALSE); |
a6255b7f DQ |
3736 | arc_share_buf(hdr, buf); |
3737 | } | |
3738 | ||
d3c2ae1c | 3739 | return (buf); |
34dc7c2f BB |
3740 | } |
3741 | ||
b5256303 TC |
3742 | arc_buf_t * |
3743 | arc_alloc_raw_buf(spa_t *spa, void *tag, uint64_t dsobj, boolean_t byteorder, | |
3744 | const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, | |
3745 | dmu_object_type_t ot, uint64_t psize, uint64_t lsize, | |
3746 | enum zio_compress compression_type) | |
3747 | { | |
3748 | arc_buf_hdr_t *hdr; | |
3749 | arc_buf_t *buf; | |
3750 | arc_buf_contents_t type = DMU_OT_IS_METADATA(ot) ? | |
3751 | ARC_BUFC_METADATA : ARC_BUFC_DATA; | |
3752 | ||
3753 | ASSERT3U(lsize, >, 0); | |
3754 | ASSERT3U(lsize, >=, psize); | |
3755 | ASSERT3U(compression_type, >=, ZIO_COMPRESS_OFF); | |
3756 | ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS); | |
3757 | ||
3758 | hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, B_TRUE, | |
3759 | compression_type, type, B_TRUE); | |
3760 | ASSERT(!MUTEX_HELD(HDR_LOCK(hdr))); | |
3761 | ||
3762 | hdr->b_crypt_hdr.b_dsobj = dsobj; | |
3763 | hdr->b_crypt_hdr.b_ot = ot; | |
3764 | hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ? | |
3765 | DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot); | |
3766 | bcopy(salt, hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); | |
3767 | bcopy(iv, hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); | |
3768 | bcopy(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); | |
3769 | ||
3770 | /* | |
3771 | * This buffer will be considered encrypted even if the ot is not an | |
3772 | * encrypted type. It will become authenticated instead in | |
3773 | * arc_write_ready(). | |
3774 | */ | |
3775 | buf = NULL; | |
be9a5c35 | 3776 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_TRUE, B_TRUE, |
b5256303 TC |
3777 | B_FALSE, B_FALSE, &buf)); |
3778 | arc_buf_thaw(buf); | |
3779 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
3780 | ||
3781 | return (buf); | |
3782 | } | |
3783 | ||
d962d5da PS |
3784 | static void |
3785 | arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) | |
3786 | { | |
3787 | l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; | |
3788 | l2arc_dev_t *dev = l2hdr->b_dev; | |
7558997d SD |
3789 | uint64_t psize = HDR_GET_PSIZE(hdr); |
3790 | uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
d962d5da PS |
3791 | |
3792 | ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); | |
3793 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
3794 | ||
3795 | list_remove(&dev->l2ad_buflist, hdr); | |
3796 | ||
01850391 AG |
3797 | ARCSTAT_INCR(arcstat_l2_psize, -psize); |
3798 | ARCSTAT_INCR(arcstat_l2_lsize, -HDR_GET_LSIZE(hdr)); | |
d962d5da | 3799 | |
7558997d | 3800 | vdev_space_update(dev->l2ad_vdev, -asize, 0, 0); |
d962d5da | 3801 | |
7558997d SD |
3802 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr), |
3803 | hdr); | |
d3c2ae1c | 3804 | arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); |
d962d5da PS |
3805 | } |
3806 | ||
34dc7c2f BB |
3807 | static void |
3808 | arc_hdr_destroy(arc_buf_hdr_t *hdr) | |
3809 | { | |
b9541d6b CW |
3810 | if (HDR_HAS_L1HDR(hdr)) { |
3811 | ASSERT(hdr->b_l1hdr.b_buf == NULL || | |
d3c2ae1c | 3812 | hdr->b_l1hdr.b_bufcnt > 0); |
424fd7c3 | 3813 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
b9541d6b CW |
3814 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
3815 | } | |
34dc7c2f | 3816 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b CW |
3817 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); |
3818 | ||
d3c2ae1c GW |
3819 | if (!HDR_EMPTY(hdr)) |
3820 | buf_discard_identity(hdr); | |
3821 | ||
b9541d6b | 3822 | if (HDR_HAS_L2HDR(hdr)) { |
d962d5da PS |
3823 | l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; |
3824 | boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); | |
428870ff | 3825 | |
d962d5da PS |
3826 | if (!buflist_held) |
3827 | mutex_enter(&dev->l2ad_mtx); | |
b9541d6b | 3828 | |
ca0bf58d | 3829 | /* |
d962d5da PS |
3830 | * Even though we checked this conditional above, we |
3831 | * need to check this again now that we have the | |
3832 | * l2ad_mtx. This is because we could be racing with | |
3833 | * another thread calling l2arc_evict() which might have | |
3834 | * destroyed this header's L2 portion as we were waiting | |
3835 | * to acquire the l2ad_mtx. If that happens, we don't | |
3836 | * want to re-destroy the header's L2 portion. | |
ca0bf58d | 3837 | */ |
d962d5da PS |
3838 | if (HDR_HAS_L2HDR(hdr)) |
3839 | arc_hdr_l2hdr_destroy(hdr); | |
428870ff BB |
3840 | |
3841 | if (!buflist_held) | |
d962d5da | 3842 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
3843 | } |
3844 | ||
d3c2ae1c GW |
3845 | if (HDR_HAS_L1HDR(hdr)) { |
3846 | arc_cksum_free(hdr); | |
b9541d6b | 3847 | |
d3c2ae1c | 3848 | while (hdr->b_l1hdr.b_buf != NULL) |
2aa34383 | 3849 | arc_buf_destroy_impl(hdr->b_l1hdr.b_buf); |
34dc7c2f | 3850 | |
b5256303 TC |
3851 | if (hdr->b_l1hdr.b_pabd != NULL) { |
3852 | arc_hdr_free_abd(hdr, B_FALSE); | |
3853 | } | |
3854 | ||
440a3eb9 | 3855 | if (HDR_HAS_RABD(hdr)) |
b5256303 | 3856 | arc_hdr_free_abd(hdr, B_TRUE); |
b9541d6b CW |
3857 | } |
3858 | ||
34dc7c2f | 3859 | ASSERT3P(hdr->b_hash_next, ==, NULL); |
b9541d6b | 3860 | if (HDR_HAS_L1HDR(hdr)) { |
ca0bf58d | 3861 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b | 3862 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
b5256303 TC |
3863 | |
3864 | if (!HDR_PROTECTED(hdr)) { | |
3865 | kmem_cache_free(hdr_full_cache, hdr); | |
3866 | } else { | |
3867 | kmem_cache_free(hdr_full_crypt_cache, hdr); | |
3868 | } | |
b9541d6b CW |
3869 | } else { |
3870 | kmem_cache_free(hdr_l2only_cache, hdr); | |
3871 | } | |
34dc7c2f BB |
3872 | } |
3873 | ||
3874 | void | |
d3c2ae1c | 3875 | arc_buf_destroy(arc_buf_t *buf, void* tag) |
34dc7c2f BB |
3876 | { |
3877 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
96c080cb | 3878 | kmutex_t *hash_lock = HDR_LOCK(hdr); |
34dc7c2f | 3879 | |
b9541d6b | 3880 | if (hdr->b_l1hdr.b_state == arc_anon) { |
d3c2ae1c GW |
3881 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); |
3882 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
3883 | VERIFY0(remove_reference(hdr, NULL, tag)); | |
3884 | arc_hdr_destroy(hdr); | |
3885 | return; | |
34dc7c2f BB |
3886 | } |
3887 | ||
3888 | mutex_enter(hash_lock); | |
d3c2ae1c GW |
3889 | ASSERT3P(hdr, ==, buf->b_hdr); |
3890 | ASSERT(hdr->b_l1hdr.b_bufcnt > 0); | |
428870ff | 3891 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
d3c2ae1c GW |
3892 | ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon); |
3893 | ASSERT3P(buf->b_data, !=, NULL); | |
34dc7c2f BB |
3894 | |
3895 | (void) remove_reference(hdr, hash_lock, tag); | |
2aa34383 | 3896 | arc_buf_destroy_impl(buf); |
34dc7c2f | 3897 | mutex_exit(hash_lock); |
34dc7c2f BB |
3898 | } |
3899 | ||
34dc7c2f | 3900 | /* |
ca0bf58d PS |
3901 | * Evict the arc_buf_hdr that is provided as a parameter. The resultant |
3902 | * state of the header is dependent on its state prior to entering this | |
3903 | * function. The following transitions are possible: | |
34dc7c2f | 3904 | * |
ca0bf58d PS |
3905 | * - arc_mru -> arc_mru_ghost |
3906 | * - arc_mfu -> arc_mfu_ghost | |
3907 | * - arc_mru_ghost -> arc_l2c_only | |
3908 | * - arc_mru_ghost -> deleted | |
3909 | * - arc_mfu_ghost -> arc_l2c_only | |
3910 | * - arc_mfu_ghost -> deleted | |
34dc7c2f | 3911 | */ |
ca0bf58d PS |
3912 | static int64_t |
3913 | arc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) | |
34dc7c2f | 3914 | { |
ca0bf58d PS |
3915 | arc_state_t *evicted_state, *state; |
3916 | int64_t bytes_evicted = 0; | |
d4a72f23 TC |
3917 | int min_lifetime = HDR_PRESCIENT_PREFETCH(hdr) ? |
3918 | arc_min_prescient_prefetch_ms : arc_min_prefetch_ms; | |
34dc7c2f | 3919 | |
ca0bf58d PS |
3920 | ASSERT(MUTEX_HELD(hash_lock)); |
3921 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
e8b96c60 | 3922 | |
ca0bf58d PS |
3923 | state = hdr->b_l1hdr.b_state; |
3924 | if (GHOST_STATE(state)) { | |
3925 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
d3c2ae1c | 3926 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
e8b96c60 MA |
3927 | |
3928 | /* | |
ca0bf58d | 3929 | * l2arc_write_buffers() relies on a header's L1 portion |
a6255b7f | 3930 | * (i.e. its b_pabd field) during it's write phase. |
ca0bf58d PS |
3931 | * Thus, we cannot push a header onto the arc_l2c_only |
3932 | * state (removing its L1 piece) until the header is | |
3933 | * done being written to the l2arc. | |
e8b96c60 | 3934 | */ |
ca0bf58d PS |
3935 | if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { |
3936 | ARCSTAT_BUMP(arcstat_evict_l2_skip); | |
3937 | return (bytes_evicted); | |
e8b96c60 MA |
3938 | } |
3939 | ||
ca0bf58d | 3940 | ARCSTAT_BUMP(arcstat_deleted); |
d3c2ae1c | 3941 | bytes_evicted += HDR_GET_LSIZE(hdr); |
428870ff | 3942 | |
ca0bf58d | 3943 | DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); |
428870ff | 3944 | |
ca0bf58d | 3945 | if (HDR_HAS_L2HDR(hdr)) { |
a6255b7f | 3946 | ASSERT(hdr->b_l1hdr.b_pabd == NULL); |
b5256303 | 3947 | ASSERT(!HDR_HAS_RABD(hdr)); |
ca0bf58d PS |
3948 | /* |
3949 | * This buffer is cached on the 2nd Level ARC; | |
3950 | * don't destroy the header. | |
3951 | */ | |
3952 | arc_change_state(arc_l2c_only, hdr, hash_lock); | |
3953 | /* | |
3954 | * dropping from L1+L2 cached to L2-only, | |
3955 | * realloc to remove the L1 header. | |
3956 | */ | |
3957 | hdr = arc_hdr_realloc(hdr, hdr_full_cache, | |
3958 | hdr_l2only_cache); | |
34dc7c2f | 3959 | } else { |
ca0bf58d PS |
3960 | arc_change_state(arc_anon, hdr, hash_lock); |
3961 | arc_hdr_destroy(hdr); | |
34dc7c2f | 3962 | } |
ca0bf58d | 3963 | return (bytes_evicted); |
34dc7c2f BB |
3964 | } |
3965 | ||
ca0bf58d PS |
3966 | ASSERT(state == arc_mru || state == arc_mfu); |
3967 | evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; | |
34dc7c2f | 3968 | |
ca0bf58d PS |
3969 | /* prefetch buffers have a minimum lifespan */ |
3970 | if (HDR_IO_IN_PROGRESS(hdr) || | |
3971 | ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && | |
2b84817f TC |
3972 | ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < |
3973 | MSEC_TO_TICK(min_lifetime))) { | |
ca0bf58d PS |
3974 | ARCSTAT_BUMP(arcstat_evict_skip); |
3975 | return (bytes_evicted); | |
da8ccd0e PS |
3976 | } |
3977 | ||
424fd7c3 | 3978 | ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt)); |
ca0bf58d PS |
3979 | while (hdr->b_l1hdr.b_buf) { |
3980 | arc_buf_t *buf = hdr->b_l1hdr.b_buf; | |
3981 | if (!mutex_tryenter(&buf->b_evict_lock)) { | |
3982 | ARCSTAT_BUMP(arcstat_mutex_miss); | |
3983 | break; | |
3984 | } | |
3985 | if (buf->b_data != NULL) | |
d3c2ae1c GW |
3986 | bytes_evicted += HDR_GET_LSIZE(hdr); |
3987 | mutex_exit(&buf->b_evict_lock); | |
2aa34383 | 3988 | arc_buf_destroy_impl(buf); |
ca0bf58d | 3989 | } |
34dc7c2f | 3990 | |
ca0bf58d | 3991 | if (HDR_HAS_L2HDR(hdr)) { |
d3c2ae1c | 3992 | ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr)); |
ca0bf58d | 3993 | } else { |
d3c2ae1c GW |
3994 | if (l2arc_write_eligible(hdr->b_spa, hdr)) { |
3995 | ARCSTAT_INCR(arcstat_evict_l2_eligible, | |
3996 | HDR_GET_LSIZE(hdr)); | |
3997 | } else { | |
3998 | ARCSTAT_INCR(arcstat_evict_l2_ineligible, | |
3999 | HDR_GET_LSIZE(hdr)); | |
4000 | } | |
ca0bf58d | 4001 | } |
34dc7c2f | 4002 | |
d3c2ae1c GW |
4003 | if (hdr->b_l1hdr.b_bufcnt == 0) { |
4004 | arc_cksum_free(hdr); | |
4005 | ||
4006 | bytes_evicted += arc_hdr_size(hdr); | |
4007 | ||
4008 | /* | |
4009 | * If this hdr is being evicted and has a compressed | |
4010 | * buffer then we discard it here before we change states. | |
4011 | * This ensures that the accounting is updated correctly | |
a6255b7f | 4012 | * in arc_free_data_impl(). |
d3c2ae1c | 4013 | */ |
b5256303 TC |
4014 | if (hdr->b_l1hdr.b_pabd != NULL) |
4015 | arc_hdr_free_abd(hdr, B_FALSE); | |
4016 | ||
4017 | if (HDR_HAS_RABD(hdr)) | |
4018 | arc_hdr_free_abd(hdr, B_TRUE); | |
d3c2ae1c | 4019 | |
ca0bf58d PS |
4020 | arc_change_state(evicted_state, hdr, hash_lock); |
4021 | ASSERT(HDR_IN_HASH_TABLE(hdr)); | |
d3c2ae1c | 4022 | arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
ca0bf58d PS |
4023 | DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); |
4024 | } | |
34dc7c2f | 4025 | |
ca0bf58d | 4026 | return (bytes_evicted); |
34dc7c2f BB |
4027 | } |
4028 | ||
ca0bf58d PS |
4029 | static uint64_t |
4030 | arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, | |
4031 | uint64_t spa, int64_t bytes) | |
34dc7c2f | 4032 | { |
ca0bf58d PS |
4033 | multilist_sublist_t *mls; |
4034 | uint64_t bytes_evicted = 0; | |
4035 | arc_buf_hdr_t *hdr; | |
34dc7c2f | 4036 | kmutex_t *hash_lock; |
ca0bf58d | 4037 | int evict_count = 0; |
34dc7c2f | 4038 | |
ca0bf58d | 4039 | ASSERT3P(marker, !=, NULL); |
96c080cb | 4040 | IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); |
ca0bf58d PS |
4041 | |
4042 | mls = multilist_sublist_lock(ml, idx); | |
572e2857 | 4043 | |
ca0bf58d PS |
4044 | for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL; |
4045 | hdr = multilist_sublist_prev(mls, marker)) { | |
4046 | if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) || | |
4047 | (evict_count >= zfs_arc_evict_batch_limit)) | |
4048 | break; | |
4049 | ||
4050 | /* | |
4051 | * To keep our iteration location, move the marker | |
4052 | * forward. Since we're not holding hdr's hash lock, we | |
4053 | * must be very careful and not remove 'hdr' from the | |
4054 | * sublist. Otherwise, other consumers might mistake the | |
4055 | * 'hdr' as not being on a sublist when they call the | |
4056 | * multilist_link_active() function (they all rely on | |
4057 | * the hash lock protecting concurrent insertions and | |
4058 | * removals). multilist_sublist_move_forward() was | |
4059 | * specifically implemented to ensure this is the case | |
4060 | * (only 'marker' will be removed and re-inserted). | |
4061 | */ | |
4062 | multilist_sublist_move_forward(mls, marker); | |
4063 | ||
4064 | /* | |
4065 | * The only case where the b_spa field should ever be | |
4066 | * zero, is the marker headers inserted by | |
4067 | * arc_evict_state(). It's possible for multiple threads | |
4068 | * to be calling arc_evict_state() concurrently (e.g. | |
4069 | * dsl_pool_close() and zio_inject_fault()), so we must | |
4070 | * skip any markers we see from these other threads. | |
4071 | */ | |
2a432414 | 4072 | if (hdr->b_spa == 0) |
572e2857 BB |
4073 | continue; |
4074 | ||
ca0bf58d PS |
4075 | /* we're only interested in evicting buffers of a certain spa */ |
4076 | if (spa != 0 && hdr->b_spa != spa) { | |
4077 | ARCSTAT_BUMP(arcstat_evict_skip); | |
428870ff | 4078 | continue; |
ca0bf58d PS |
4079 | } |
4080 | ||
4081 | hash_lock = HDR_LOCK(hdr); | |
e8b96c60 MA |
4082 | |
4083 | /* | |
ca0bf58d PS |
4084 | * We aren't calling this function from any code path |
4085 | * that would already be holding a hash lock, so we're | |
4086 | * asserting on this assumption to be defensive in case | |
4087 | * this ever changes. Without this check, it would be | |
4088 | * possible to incorrectly increment arcstat_mutex_miss | |
4089 | * below (e.g. if the code changed such that we called | |
4090 | * this function with a hash lock held). | |
e8b96c60 | 4091 | */ |
ca0bf58d PS |
4092 | ASSERT(!MUTEX_HELD(hash_lock)); |
4093 | ||
34dc7c2f | 4094 | if (mutex_tryenter(hash_lock)) { |
ca0bf58d PS |
4095 | uint64_t evicted = arc_evict_hdr(hdr, hash_lock); |
4096 | mutex_exit(hash_lock); | |
34dc7c2f | 4097 | |
ca0bf58d | 4098 | bytes_evicted += evicted; |
34dc7c2f | 4099 | |
572e2857 | 4100 | /* |
ca0bf58d PS |
4101 | * If evicted is zero, arc_evict_hdr() must have |
4102 | * decided to skip this header, don't increment | |
4103 | * evict_count in this case. | |
572e2857 | 4104 | */ |
ca0bf58d PS |
4105 | if (evicted != 0) |
4106 | evict_count++; | |
4107 | ||
4108 | /* | |
4109 | * If arc_size isn't overflowing, signal any | |
4110 | * threads that might happen to be waiting. | |
4111 | * | |
4112 | * For each header evicted, we wake up a single | |
4113 | * thread. If we used cv_broadcast, we could | |
4114 | * wake up "too many" threads causing arc_size | |
4115 | * to significantly overflow arc_c; since | |
a6255b7f | 4116 | * arc_get_data_impl() doesn't check for overflow |
ca0bf58d PS |
4117 | * when it's woken up (it doesn't because it's |
4118 | * possible for the ARC to be overflowing while | |
4119 | * full of un-evictable buffers, and the | |
4120 | * function should proceed in this case). | |
4121 | * | |
4122 | * If threads are left sleeping, due to not | |
3ec34e55 BL |
4123 | * using cv_broadcast here, they will be woken |
4124 | * up via cv_broadcast in arc_adjust_cb() just | |
4125 | * before arc_adjust_zthr sleeps. | |
ca0bf58d | 4126 | */ |
3ec34e55 | 4127 | mutex_enter(&arc_adjust_lock); |
ca0bf58d | 4128 | if (!arc_is_overflowing()) |
3ec34e55 BL |
4129 | cv_signal(&arc_adjust_waiters_cv); |
4130 | mutex_exit(&arc_adjust_lock); | |
e8b96c60 | 4131 | } else { |
ca0bf58d | 4132 | ARCSTAT_BUMP(arcstat_mutex_miss); |
e8b96c60 | 4133 | } |
34dc7c2f | 4134 | } |
34dc7c2f | 4135 | |
ca0bf58d | 4136 | multilist_sublist_unlock(mls); |
34dc7c2f | 4137 | |
ca0bf58d | 4138 | return (bytes_evicted); |
34dc7c2f BB |
4139 | } |
4140 | ||
ca0bf58d PS |
4141 | /* |
4142 | * Evict buffers from the given arc state, until we've removed the | |
4143 | * specified number of bytes. Move the removed buffers to the | |
4144 | * appropriate evict state. | |
4145 | * | |
4146 | * This function makes a "best effort". It skips over any buffers | |
4147 | * it can't get a hash_lock on, and so, may not catch all candidates. | |
4148 | * It may also return without evicting as much space as requested. | |
4149 | * | |
4150 | * If bytes is specified using the special value ARC_EVICT_ALL, this | |
4151 | * will evict all available (i.e. unlocked and evictable) buffers from | |
4152 | * the given arc state; which is used by arc_flush(). | |
4153 | */ | |
4154 | static uint64_t | |
4155 | arc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes, | |
4156 | arc_buf_contents_t type) | |
34dc7c2f | 4157 | { |
ca0bf58d | 4158 | uint64_t total_evicted = 0; |
64fc7762 | 4159 | multilist_t *ml = state->arcs_list[type]; |
ca0bf58d PS |
4160 | int num_sublists; |
4161 | arc_buf_hdr_t **markers; | |
ca0bf58d | 4162 | |
96c080cb | 4163 | IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); |
ca0bf58d PS |
4164 | |
4165 | num_sublists = multilist_get_num_sublists(ml); | |
d164b209 BB |
4166 | |
4167 | /* | |
ca0bf58d PS |
4168 | * If we've tried to evict from each sublist, made some |
4169 | * progress, but still have not hit the target number of bytes | |
4170 | * to evict, we want to keep trying. The markers allow us to | |
4171 | * pick up where we left off for each individual sublist, rather | |
4172 | * than starting from the tail each time. | |
d164b209 | 4173 | */ |
ca0bf58d | 4174 | markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); |
1c27024e | 4175 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d | 4176 | multilist_sublist_t *mls; |
34dc7c2f | 4177 | |
ca0bf58d PS |
4178 | markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); |
4179 | ||
4180 | /* | |
4181 | * A b_spa of 0 is used to indicate that this header is | |
4182 | * a marker. This fact is used in arc_adjust_type() and | |
4183 | * arc_evict_state_impl(). | |
4184 | */ | |
4185 | markers[i]->b_spa = 0; | |
34dc7c2f | 4186 | |
ca0bf58d PS |
4187 | mls = multilist_sublist_lock(ml, i); |
4188 | multilist_sublist_insert_tail(mls, markers[i]); | |
4189 | multilist_sublist_unlock(mls); | |
34dc7c2f BB |
4190 | } |
4191 | ||
d164b209 | 4192 | /* |
ca0bf58d PS |
4193 | * While we haven't hit our target number of bytes to evict, or |
4194 | * we're evicting all available buffers. | |
d164b209 | 4195 | */ |
ca0bf58d | 4196 | while (total_evicted < bytes || bytes == ARC_EVICT_ALL) { |
25458cbe TC |
4197 | int sublist_idx = multilist_get_random_index(ml); |
4198 | uint64_t scan_evicted = 0; | |
4199 | ||
4200 | /* | |
4201 | * Try to reduce pinned dnodes with a floor of arc_dnode_limit. | |
4202 | * Request that 10% of the LRUs be scanned by the superblock | |
4203 | * shrinker. | |
4204 | */ | |
37fb3e43 PD |
4205 | if (type == ARC_BUFC_DATA && aggsum_compare(&astat_dnode_size, |
4206 | arc_dnode_limit) > 0) { | |
4207 | arc_prune_async((aggsum_upper_bound(&astat_dnode_size) - | |
4208 | arc_dnode_limit) / sizeof (dnode_t) / | |
4209 | zfs_arc_dnode_reduce_percent); | |
4210 | } | |
25458cbe | 4211 | |
ca0bf58d PS |
4212 | /* |
4213 | * Start eviction using a randomly selected sublist, | |
4214 | * this is to try and evenly balance eviction across all | |
4215 | * sublists. Always starting at the same sublist | |
4216 | * (e.g. index 0) would cause evictions to favor certain | |
4217 | * sublists over others. | |
4218 | */ | |
1c27024e | 4219 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d PS |
4220 | uint64_t bytes_remaining; |
4221 | uint64_t bytes_evicted; | |
d164b209 | 4222 | |
ca0bf58d PS |
4223 | if (bytes == ARC_EVICT_ALL) |
4224 | bytes_remaining = ARC_EVICT_ALL; | |
4225 | else if (total_evicted < bytes) | |
4226 | bytes_remaining = bytes - total_evicted; | |
4227 | else | |
4228 | break; | |
34dc7c2f | 4229 | |
ca0bf58d PS |
4230 | bytes_evicted = arc_evict_state_impl(ml, sublist_idx, |
4231 | markers[sublist_idx], spa, bytes_remaining); | |
4232 | ||
4233 | scan_evicted += bytes_evicted; | |
4234 | total_evicted += bytes_evicted; | |
4235 | ||
4236 | /* we've reached the end, wrap to the beginning */ | |
4237 | if (++sublist_idx >= num_sublists) | |
4238 | sublist_idx = 0; | |
4239 | } | |
4240 | ||
4241 | /* | |
4242 | * If we didn't evict anything during this scan, we have | |
4243 | * no reason to believe we'll evict more during another | |
4244 | * scan, so break the loop. | |
4245 | */ | |
4246 | if (scan_evicted == 0) { | |
4247 | /* This isn't possible, let's make that obvious */ | |
4248 | ASSERT3S(bytes, !=, 0); | |
34dc7c2f | 4249 | |
ca0bf58d PS |
4250 | /* |
4251 | * When bytes is ARC_EVICT_ALL, the only way to | |
4252 | * break the loop is when scan_evicted is zero. | |
4253 | * In that case, we actually have evicted enough, | |
4254 | * so we don't want to increment the kstat. | |
4255 | */ | |
4256 | if (bytes != ARC_EVICT_ALL) { | |
4257 | ASSERT3S(total_evicted, <, bytes); | |
4258 | ARCSTAT_BUMP(arcstat_evict_not_enough); | |
4259 | } | |
d164b209 | 4260 | |
ca0bf58d PS |
4261 | break; |
4262 | } | |
d164b209 | 4263 | } |
34dc7c2f | 4264 | |
1c27024e | 4265 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d PS |
4266 | multilist_sublist_t *mls = multilist_sublist_lock(ml, i); |
4267 | multilist_sublist_remove(mls, markers[i]); | |
4268 | multilist_sublist_unlock(mls); | |
34dc7c2f | 4269 | |
ca0bf58d | 4270 | kmem_cache_free(hdr_full_cache, markers[i]); |
34dc7c2f | 4271 | } |
ca0bf58d PS |
4272 | kmem_free(markers, sizeof (*markers) * num_sublists); |
4273 | ||
4274 | return (total_evicted); | |
4275 | } | |
4276 | ||
4277 | /* | |
4278 | * Flush all "evictable" data of the given type from the arc state | |
4279 | * specified. This will not evict any "active" buffers (i.e. referenced). | |
4280 | * | |
d3c2ae1c | 4281 | * When 'retry' is set to B_FALSE, the function will make a single pass |
ca0bf58d PS |
4282 | * over the state and evict any buffers that it can. Since it doesn't |
4283 | * continually retry the eviction, it might end up leaving some buffers | |
4284 | * in the ARC due to lock misses. | |
4285 | * | |
d3c2ae1c | 4286 | * When 'retry' is set to B_TRUE, the function will continually retry the |
ca0bf58d PS |
4287 | * eviction until *all* evictable buffers have been removed from the |
4288 | * state. As a result, if concurrent insertions into the state are | |
4289 | * allowed (e.g. if the ARC isn't shutting down), this function might | |
4290 | * wind up in an infinite loop, continually trying to evict buffers. | |
4291 | */ | |
4292 | static uint64_t | |
4293 | arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, | |
4294 | boolean_t retry) | |
4295 | { | |
4296 | uint64_t evicted = 0; | |
4297 | ||
424fd7c3 | 4298 | while (zfs_refcount_count(&state->arcs_esize[type]) != 0) { |
ca0bf58d PS |
4299 | evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); |
4300 | ||
4301 | if (!retry) | |
4302 | break; | |
4303 | } | |
4304 | ||
4305 | return (evicted); | |
34dc7c2f BB |
4306 | } |
4307 | ||
ab26409d | 4308 | /* |
ef5b2e10 BB |
4309 | * Helper function for arc_prune_async() it is responsible for safely |
4310 | * handling the execution of a registered arc_prune_func_t. | |
ab26409d BB |
4311 | */ |
4312 | static void | |
f6046738 | 4313 | arc_prune_task(void *ptr) |
ab26409d | 4314 | { |
f6046738 BB |
4315 | arc_prune_t *ap = (arc_prune_t *)ptr; |
4316 | arc_prune_func_t *func = ap->p_pfunc; | |
ab26409d | 4317 | |
f6046738 BB |
4318 | if (func != NULL) |
4319 | func(ap->p_adjust, ap->p_private); | |
ab26409d | 4320 | |
424fd7c3 | 4321 | zfs_refcount_remove(&ap->p_refcnt, func); |
f6046738 | 4322 | } |
ab26409d | 4323 | |
f6046738 BB |
4324 | /* |
4325 | * Notify registered consumers they must drop holds on a portion of the ARC | |
4326 | * buffered they reference. This provides a mechanism to ensure the ARC can | |
4327 | * honor the arc_meta_limit and reclaim otherwise pinned ARC buffers. This | |
4328 | * is analogous to dnlc_reduce_cache() but more generic. | |
4329 | * | |
ef5b2e10 | 4330 | * This operation is performed asynchronously so it may be safely called |
ca67b33a | 4331 | * in the context of the arc_reclaim_thread(). A reference is taken here |
f6046738 BB |
4332 | * for each registered arc_prune_t and the arc_prune_task() is responsible |
4333 | * for releasing it once the registered arc_prune_func_t has completed. | |
4334 | */ | |
4335 | static void | |
4336 | arc_prune_async(int64_t adjust) | |
4337 | { | |
4338 | arc_prune_t *ap; | |
ab26409d | 4339 | |
f6046738 BB |
4340 | mutex_enter(&arc_prune_mtx); |
4341 | for (ap = list_head(&arc_prune_list); ap != NULL; | |
4342 | ap = list_next(&arc_prune_list, ap)) { | |
ab26409d | 4343 | |
424fd7c3 | 4344 | if (zfs_refcount_count(&ap->p_refcnt) >= 2) |
f6046738 | 4345 | continue; |
ab26409d | 4346 | |
c13060e4 | 4347 | zfs_refcount_add(&ap->p_refcnt, ap->p_pfunc); |
f6046738 | 4348 | ap->p_adjust = adjust; |
b60eac3d | 4349 | if (taskq_dispatch(arc_prune_taskq, arc_prune_task, |
48d3eb40 | 4350 | ap, TQ_SLEEP) == TASKQID_INVALID) { |
424fd7c3 | 4351 | zfs_refcount_remove(&ap->p_refcnt, ap->p_pfunc); |
b60eac3d | 4352 | continue; |
4353 | } | |
f6046738 | 4354 | ARCSTAT_BUMP(arcstat_prune); |
ab26409d | 4355 | } |
ab26409d BB |
4356 | mutex_exit(&arc_prune_mtx); |
4357 | } | |
4358 | ||
ca0bf58d PS |
4359 | /* |
4360 | * Evict the specified number of bytes from the state specified, | |
4361 | * restricting eviction to the spa and type given. This function | |
4362 | * prevents us from trying to evict more from a state's list than | |
4363 | * is "evictable", and to skip evicting altogether when passed a | |
4364 | * negative value for "bytes". In contrast, arc_evict_state() will | |
4365 | * evict everything it can, when passed a negative value for "bytes". | |
4366 | */ | |
4367 | static uint64_t | |
4368 | arc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes, | |
4369 | arc_buf_contents_t type) | |
4370 | { | |
4371 | int64_t delta; | |
4372 | ||
424fd7c3 TS |
4373 | if (bytes > 0 && zfs_refcount_count(&state->arcs_esize[type]) > 0) { |
4374 | delta = MIN(zfs_refcount_count(&state->arcs_esize[type]), | |
4375 | bytes); | |
ca0bf58d PS |
4376 | return (arc_evict_state(state, spa, delta, type)); |
4377 | } | |
4378 | ||
4379 | return (0); | |
4380 | } | |
4381 | ||
4382 | /* | |
4383 | * The goal of this function is to evict enough meta data buffers from the | |
4384 | * ARC in order to enforce the arc_meta_limit. Achieving this is slightly | |
4385 | * more complicated than it appears because it is common for data buffers | |
4386 | * to have holds on meta data buffers. In addition, dnode meta data buffers | |
4387 | * will be held by the dnodes in the block preventing them from being freed. | |
4388 | * This means we can't simply traverse the ARC and expect to always find | |
4389 | * enough unheld meta data buffer to release. | |
4390 | * | |
4391 | * Therefore, this function has been updated to make alternating passes | |
4392 | * over the ARC releasing data buffers and then newly unheld meta data | |
37fb3e43 | 4393 | * buffers. This ensures forward progress is maintained and meta_used |
ca0bf58d PS |
4394 | * will decrease. Normally this is sufficient, but if required the ARC |
4395 | * will call the registered prune callbacks causing dentry and inodes to | |
4396 | * be dropped from the VFS cache. This will make dnode meta data buffers | |
4397 | * available for reclaim. | |
4398 | */ | |
4399 | static uint64_t | |
37fb3e43 | 4400 | arc_adjust_meta_balanced(uint64_t meta_used) |
ca0bf58d | 4401 | { |
25e2ab16 TC |
4402 | int64_t delta, prune = 0, adjustmnt; |
4403 | uint64_t total_evicted = 0; | |
ca0bf58d | 4404 | arc_buf_contents_t type = ARC_BUFC_DATA; |
ca67b33a | 4405 | int restarts = MAX(zfs_arc_meta_adjust_restarts, 0); |
ca0bf58d PS |
4406 | |
4407 | restart: | |
4408 | /* | |
4409 | * This slightly differs than the way we evict from the mru in | |
4410 | * arc_adjust because we don't have a "target" value (i.e. no | |
4411 | * "meta" arc_p). As a result, I think we can completely | |
4412 | * cannibalize the metadata in the MRU before we evict the | |
4413 | * metadata from the MFU. I think we probably need to implement a | |
4414 | * "metadata arc_p" value to do this properly. | |
4415 | */ | |
37fb3e43 | 4416 | adjustmnt = meta_used - arc_meta_limit; |
ca0bf58d | 4417 | |
424fd7c3 TS |
4418 | if (adjustmnt > 0 && |
4419 | zfs_refcount_count(&arc_mru->arcs_esize[type]) > 0) { | |
4420 | delta = MIN(zfs_refcount_count(&arc_mru->arcs_esize[type]), | |
d3c2ae1c | 4421 | adjustmnt); |
ca0bf58d PS |
4422 | total_evicted += arc_adjust_impl(arc_mru, 0, delta, type); |
4423 | adjustmnt -= delta; | |
4424 | } | |
4425 | ||
4426 | /* | |
4427 | * We can't afford to recalculate adjustmnt here. If we do, | |
4428 | * new metadata buffers can sneak into the MRU or ANON lists, | |
4429 | * thus penalize the MFU metadata. Although the fudge factor is | |
4430 | * small, it has been empirically shown to be significant for | |
4431 | * certain workloads (e.g. creating many empty directories). As | |
4432 | * such, we use the original calculation for adjustmnt, and | |
4433 | * simply decrement the amount of data evicted from the MRU. | |
4434 | */ | |
4435 | ||
424fd7c3 TS |
4436 | if (adjustmnt > 0 && |
4437 | zfs_refcount_count(&arc_mfu->arcs_esize[type]) > 0) { | |
4438 | delta = MIN(zfs_refcount_count(&arc_mfu->arcs_esize[type]), | |
d3c2ae1c | 4439 | adjustmnt); |
ca0bf58d PS |
4440 | total_evicted += arc_adjust_impl(arc_mfu, 0, delta, type); |
4441 | } | |
4442 | ||
37fb3e43 | 4443 | adjustmnt = meta_used - arc_meta_limit; |
ca0bf58d | 4444 | |
d3c2ae1c | 4445 | if (adjustmnt > 0 && |
424fd7c3 | 4446 | zfs_refcount_count(&arc_mru_ghost->arcs_esize[type]) > 0) { |
ca0bf58d | 4447 | delta = MIN(adjustmnt, |
424fd7c3 | 4448 | zfs_refcount_count(&arc_mru_ghost->arcs_esize[type])); |
ca0bf58d PS |
4449 | total_evicted += arc_adjust_impl(arc_mru_ghost, 0, delta, type); |
4450 | adjustmnt -= delta; | |
4451 | } | |
4452 | ||
d3c2ae1c | 4453 | if (adjustmnt > 0 && |
424fd7c3 | 4454 | zfs_refcount_count(&arc_mfu_ghost->arcs_esize[type]) > 0) { |
ca0bf58d | 4455 | delta = MIN(adjustmnt, |
424fd7c3 | 4456 | zfs_refcount_count(&arc_mfu_ghost->arcs_esize[type])); |
ca0bf58d PS |
4457 | total_evicted += arc_adjust_impl(arc_mfu_ghost, 0, delta, type); |
4458 | } | |
4459 | ||
4460 | /* | |
4461 | * If after attempting to make the requested adjustment to the ARC | |
4462 | * the meta limit is still being exceeded then request that the | |
4463 | * higher layers drop some cached objects which have holds on ARC | |
4464 | * meta buffers. Requests to the upper layers will be made with | |
4465 | * increasingly large scan sizes until the ARC is below the limit. | |
4466 | */ | |
37fb3e43 | 4467 | if (meta_used > arc_meta_limit) { |
ca0bf58d PS |
4468 | if (type == ARC_BUFC_DATA) { |
4469 | type = ARC_BUFC_METADATA; | |
4470 | } else { | |
4471 | type = ARC_BUFC_DATA; | |
4472 | ||
4473 | if (zfs_arc_meta_prune) { | |
4474 | prune += zfs_arc_meta_prune; | |
f6046738 | 4475 | arc_prune_async(prune); |
ca0bf58d PS |
4476 | } |
4477 | } | |
4478 | ||
4479 | if (restarts > 0) { | |
4480 | restarts--; | |
4481 | goto restart; | |
4482 | } | |
4483 | } | |
4484 | return (total_evicted); | |
4485 | } | |
4486 | ||
f6046738 BB |
4487 | /* |
4488 | * Evict metadata buffers from the cache, such that arc_meta_used is | |
4489 | * capped by the arc_meta_limit tunable. | |
4490 | */ | |
4491 | static uint64_t | |
37fb3e43 | 4492 | arc_adjust_meta_only(uint64_t meta_used) |
f6046738 BB |
4493 | { |
4494 | uint64_t total_evicted = 0; | |
4495 | int64_t target; | |
4496 | ||
4497 | /* | |
4498 | * If we're over the meta limit, we want to evict enough | |
4499 | * metadata to get back under the meta limit. We don't want to | |
4500 | * evict so much that we drop the MRU below arc_p, though. If | |
4501 | * we're over the meta limit more than we're over arc_p, we | |
4502 | * evict some from the MRU here, and some from the MFU below. | |
4503 | */ | |
37fb3e43 | 4504 | target = MIN((int64_t)(meta_used - arc_meta_limit), |
424fd7c3 TS |
4505 | (int64_t)(zfs_refcount_count(&arc_anon->arcs_size) + |
4506 | zfs_refcount_count(&arc_mru->arcs_size) - arc_p)); | |
f6046738 BB |
4507 | |
4508 | total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); | |
4509 | ||
4510 | /* | |
4511 | * Similar to the above, we want to evict enough bytes to get us | |
4512 | * below the meta limit, but not so much as to drop us below the | |
2aa34383 | 4513 | * space allotted to the MFU (which is defined as arc_c - arc_p). |
f6046738 | 4514 | */ |
37fb3e43 | 4515 | target = MIN((int64_t)(meta_used - arc_meta_limit), |
424fd7c3 | 4516 | (int64_t)(zfs_refcount_count(&arc_mfu->arcs_size) - |
37fb3e43 | 4517 | (arc_c - arc_p))); |
f6046738 BB |
4518 | |
4519 | total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); | |
4520 | ||
4521 | return (total_evicted); | |
4522 | } | |
4523 | ||
4524 | static uint64_t | |
37fb3e43 | 4525 | arc_adjust_meta(uint64_t meta_used) |
f6046738 BB |
4526 | { |
4527 | if (zfs_arc_meta_strategy == ARC_STRATEGY_META_ONLY) | |
37fb3e43 | 4528 | return (arc_adjust_meta_only(meta_used)); |
f6046738 | 4529 | else |
37fb3e43 | 4530 | return (arc_adjust_meta_balanced(meta_used)); |
f6046738 BB |
4531 | } |
4532 | ||
ca0bf58d PS |
4533 | /* |
4534 | * Return the type of the oldest buffer in the given arc state | |
4535 | * | |
4536 | * This function will select a random sublist of type ARC_BUFC_DATA and | |
4537 | * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist | |
4538 | * is compared, and the type which contains the "older" buffer will be | |
4539 | * returned. | |
4540 | */ | |
4541 | static arc_buf_contents_t | |
4542 | arc_adjust_type(arc_state_t *state) | |
4543 | { | |
64fc7762 MA |
4544 | multilist_t *data_ml = state->arcs_list[ARC_BUFC_DATA]; |
4545 | multilist_t *meta_ml = state->arcs_list[ARC_BUFC_METADATA]; | |
ca0bf58d PS |
4546 | int data_idx = multilist_get_random_index(data_ml); |
4547 | int meta_idx = multilist_get_random_index(meta_ml); | |
4548 | multilist_sublist_t *data_mls; | |
4549 | multilist_sublist_t *meta_mls; | |
4550 | arc_buf_contents_t type; | |
4551 | arc_buf_hdr_t *data_hdr; | |
4552 | arc_buf_hdr_t *meta_hdr; | |
4553 | ||
4554 | /* | |
4555 | * We keep the sublist lock until we're finished, to prevent | |
4556 | * the headers from being destroyed via arc_evict_state(). | |
4557 | */ | |
4558 | data_mls = multilist_sublist_lock(data_ml, data_idx); | |
4559 | meta_mls = multilist_sublist_lock(meta_ml, meta_idx); | |
4560 | ||
4561 | /* | |
4562 | * These two loops are to ensure we skip any markers that | |
4563 | * might be at the tail of the lists due to arc_evict_state(). | |
4564 | */ | |
4565 | ||
4566 | for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; | |
4567 | data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { | |
4568 | if (data_hdr->b_spa != 0) | |
4569 | break; | |
4570 | } | |
4571 | ||
4572 | for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; | |
4573 | meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { | |
4574 | if (meta_hdr->b_spa != 0) | |
4575 | break; | |
4576 | } | |
4577 | ||
4578 | if (data_hdr == NULL && meta_hdr == NULL) { | |
4579 | type = ARC_BUFC_DATA; | |
4580 | } else if (data_hdr == NULL) { | |
4581 | ASSERT3P(meta_hdr, !=, NULL); | |
4582 | type = ARC_BUFC_METADATA; | |
4583 | } else if (meta_hdr == NULL) { | |
4584 | ASSERT3P(data_hdr, !=, NULL); | |
4585 | type = ARC_BUFC_DATA; | |
4586 | } else { | |
4587 | ASSERT3P(data_hdr, !=, NULL); | |
4588 | ASSERT3P(meta_hdr, !=, NULL); | |
4589 | ||
4590 | /* The headers can't be on the sublist without an L1 header */ | |
4591 | ASSERT(HDR_HAS_L1HDR(data_hdr)); | |
4592 | ASSERT(HDR_HAS_L1HDR(meta_hdr)); | |
4593 | ||
4594 | if (data_hdr->b_l1hdr.b_arc_access < | |
4595 | meta_hdr->b_l1hdr.b_arc_access) { | |
4596 | type = ARC_BUFC_DATA; | |
4597 | } else { | |
4598 | type = ARC_BUFC_METADATA; | |
4599 | } | |
4600 | } | |
4601 | ||
4602 | multilist_sublist_unlock(meta_mls); | |
4603 | multilist_sublist_unlock(data_mls); | |
4604 | ||
4605 | return (type); | |
4606 | } | |
4607 | ||
4608 | /* | |
4609 | * Evict buffers from the cache, such that arc_size is capped by arc_c. | |
4610 | */ | |
4611 | static uint64_t | |
4612 | arc_adjust(void) | |
4613 | { | |
4614 | uint64_t total_evicted = 0; | |
4615 | uint64_t bytes; | |
4616 | int64_t target; | |
37fb3e43 PD |
4617 | uint64_t asize = aggsum_value(&arc_size); |
4618 | uint64_t ameta = aggsum_value(&arc_meta_used); | |
ca0bf58d PS |
4619 | |
4620 | /* | |
4621 | * If we're over arc_meta_limit, we want to correct that before | |
4622 | * potentially evicting data buffers below. | |
4623 | */ | |
37fb3e43 | 4624 | total_evicted += arc_adjust_meta(ameta); |
ca0bf58d PS |
4625 | |
4626 | /* | |
4627 | * Adjust MRU size | |
4628 | * | |
4629 | * If we're over the target cache size, we want to evict enough | |
4630 | * from the list to get back to our target size. We don't want | |
4631 | * to evict too much from the MRU, such that it drops below | |
4632 | * arc_p. So, if we're over our target cache size more than | |
4633 | * the MRU is over arc_p, we'll evict enough to get back to | |
4634 | * arc_p here, and then evict more from the MFU below. | |
4635 | */ | |
37fb3e43 | 4636 | target = MIN((int64_t)(asize - arc_c), |
424fd7c3 TS |
4637 | (int64_t)(zfs_refcount_count(&arc_anon->arcs_size) + |
4638 | zfs_refcount_count(&arc_mru->arcs_size) + ameta - arc_p)); | |
ca0bf58d PS |
4639 | |
4640 | /* | |
4641 | * If we're below arc_meta_min, always prefer to evict data. | |
4642 | * Otherwise, try to satisfy the requested number of bytes to | |
4643 | * evict from the type which contains older buffers; in an | |
4644 | * effort to keep newer buffers in the cache regardless of their | |
4645 | * type. If we cannot satisfy the number of bytes from this | |
4646 | * type, spill over into the next type. | |
4647 | */ | |
4648 | if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA && | |
37fb3e43 | 4649 | ameta > arc_meta_min) { |
ca0bf58d PS |
4650 | bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); |
4651 | total_evicted += bytes; | |
4652 | ||
4653 | /* | |
4654 | * If we couldn't evict our target number of bytes from | |
4655 | * metadata, we try to get the rest from data. | |
4656 | */ | |
4657 | target -= bytes; | |
4658 | ||
4659 | total_evicted += | |
4660 | arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); | |
4661 | } else { | |
4662 | bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); | |
4663 | total_evicted += bytes; | |
4664 | ||
4665 | /* | |
4666 | * If we couldn't evict our target number of bytes from | |
4667 | * data, we try to get the rest from metadata. | |
4668 | */ | |
4669 | target -= bytes; | |
4670 | ||
4671 | total_evicted += | |
4672 | arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); | |
4673 | } | |
4674 | ||
0405eeea RE |
4675 | /* |
4676 | * Re-sum ARC stats after the first round of evictions. | |
4677 | */ | |
4678 | asize = aggsum_value(&arc_size); | |
4679 | ameta = aggsum_value(&arc_meta_used); | |
4680 | ||
4681 | ||
ca0bf58d PS |
4682 | /* |
4683 | * Adjust MFU size | |
4684 | * | |
4685 | * Now that we've tried to evict enough from the MRU to get its | |
4686 | * size back to arc_p, if we're still above the target cache | |
4687 | * size, we evict the rest from the MFU. | |
4688 | */ | |
37fb3e43 | 4689 | target = asize - arc_c; |
ca0bf58d | 4690 | |
a7b10a93 | 4691 | if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA && |
37fb3e43 | 4692 | ameta > arc_meta_min) { |
ca0bf58d PS |
4693 | bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); |
4694 | total_evicted += bytes; | |
4695 | ||
4696 | /* | |
4697 | * If we couldn't evict our target number of bytes from | |
4698 | * metadata, we try to get the rest from data. | |
4699 | */ | |
4700 | target -= bytes; | |
4701 | ||
4702 | total_evicted += | |
4703 | arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); | |
4704 | } else { | |
4705 | bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); | |
4706 | total_evicted += bytes; | |
4707 | ||
4708 | /* | |
4709 | * If we couldn't evict our target number of bytes from | |
4710 | * data, we try to get the rest from data. | |
4711 | */ | |
4712 | target -= bytes; | |
4713 | ||
4714 | total_evicted += | |
4715 | arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); | |
4716 | } | |
4717 | ||
4718 | /* | |
4719 | * Adjust ghost lists | |
4720 | * | |
4721 | * In addition to the above, the ARC also defines target values | |
4722 | * for the ghost lists. The sum of the mru list and mru ghost | |
4723 | * list should never exceed the target size of the cache, and | |
4724 | * the sum of the mru list, mfu list, mru ghost list, and mfu | |
4725 | * ghost list should never exceed twice the target size of the | |
4726 | * cache. The following logic enforces these limits on the ghost | |
4727 | * caches, and evicts from them as needed. | |
4728 | */ | |
424fd7c3 TS |
4729 | target = zfs_refcount_count(&arc_mru->arcs_size) + |
4730 | zfs_refcount_count(&arc_mru_ghost->arcs_size) - arc_c; | |
ca0bf58d PS |
4731 | |
4732 | bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); | |
4733 | total_evicted += bytes; | |
4734 | ||
4735 | target -= bytes; | |
4736 | ||
4737 | total_evicted += | |
4738 | arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); | |
4739 | ||
4740 | /* | |
4741 | * We assume the sum of the mru list and mfu list is less than | |
4742 | * or equal to arc_c (we enforced this above), which means we | |
4743 | * can use the simpler of the two equations below: | |
4744 | * | |
4745 | * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c | |
4746 | * mru ghost + mfu ghost <= arc_c | |
4747 | */ | |
424fd7c3 TS |
4748 | target = zfs_refcount_count(&arc_mru_ghost->arcs_size) + |
4749 | zfs_refcount_count(&arc_mfu_ghost->arcs_size) - arc_c; | |
ca0bf58d PS |
4750 | |
4751 | bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); | |
4752 | total_evicted += bytes; | |
4753 | ||
4754 | target -= bytes; | |
4755 | ||
4756 | total_evicted += | |
4757 | arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); | |
4758 | ||
4759 | return (total_evicted); | |
4760 | } | |
4761 | ||
ca0bf58d PS |
4762 | void |
4763 | arc_flush(spa_t *spa, boolean_t retry) | |
ab26409d | 4764 | { |
ca0bf58d | 4765 | uint64_t guid = 0; |
94520ca4 | 4766 | |
bc888666 | 4767 | /* |
d3c2ae1c | 4768 | * If retry is B_TRUE, a spa must not be specified since we have |
ca0bf58d PS |
4769 | * no good way to determine if all of a spa's buffers have been |
4770 | * evicted from an arc state. | |
bc888666 | 4771 | */ |
ca0bf58d | 4772 | ASSERT(!retry || spa == 0); |
d164b209 | 4773 | |
b9541d6b | 4774 | if (spa != NULL) |
3541dc6d | 4775 | guid = spa_load_guid(spa); |
d164b209 | 4776 | |
ca0bf58d PS |
4777 | (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); |
4778 | (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); | |
4779 | ||
4780 | (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); | |
4781 | (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); | |
4782 | ||
4783 | (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); | |
4784 | (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); | |
34dc7c2f | 4785 | |
ca0bf58d PS |
4786 | (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); |
4787 | (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); | |
34dc7c2f BB |
4788 | } |
4789 | ||
3ec34e55 BL |
4790 | static void |
4791 | arc_reduce_target_size(int64_t to_free) | |
34dc7c2f | 4792 | { |
37fb3e43 | 4793 | uint64_t asize = aggsum_value(&arc_size); |
1b8951b3 | 4794 | uint64_t c = arc_c; |
34dc7c2f | 4795 | |
1b8951b3 TC |
4796 | if (c > to_free && c - to_free > arc_c_min) { |
4797 | arc_c = c - to_free; | |
ca67b33a | 4798 | atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); |
37fb3e43 PD |
4799 | if (asize < arc_c) |
4800 | arc_c = MAX(asize, arc_c_min); | |
34dc7c2f BB |
4801 | if (arc_p > arc_c) |
4802 | arc_p = (arc_c >> 1); | |
4803 | ASSERT(arc_c >= arc_c_min); | |
4804 | ASSERT((int64_t)arc_p >= 0); | |
1b8951b3 TC |
4805 | } else { |
4806 | arc_c = arc_c_min; | |
34dc7c2f BB |
4807 | } |
4808 | ||
3ec34e55 BL |
4809 | if (asize > arc_c) { |
4810 | /* See comment in arc_adjust_cb_check() on why lock+flag */ | |
4811 | mutex_enter(&arc_adjust_lock); | |
4812 | arc_adjust_needed = B_TRUE; | |
4813 | mutex_exit(&arc_adjust_lock); | |
4814 | zthr_wakeup(arc_adjust_zthr); | |
4815 | } | |
34dc7c2f | 4816 | } |
9edb3695 BB |
4817 | /* |
4818 | * Return maximum amount of memory that we could possibly use. Reduced | |
4819 | * to half of all memory in user space which is primarily used for testing. | |
4820 | */ | |
4821 | static uint64_t | |
4822 | arc_all_memory(void) | |
4823 | { | |
4824 | #ifdef _KERNEL | |
70f02287 | 4825 | #ifdef CONFIG_HIGHMEM |
031cea17 | 4826 | return (ptob(zfs_totalram_pages - totalhigh_pages)); |
70f02287 | 4827 | #else |
031cea17 | 4828 | return (ptob(zfs_totalram_pages)); |
70f02287 | 4829 | #endif /* CONFIG_HIGHMEM */ |
9edb3695 BB |
4830 | #else |
4831 | return (ptob(physmem) / 2); | |
70f02287 | 4832 | #endif /* _KERNEL */ |
9edb3695 BB |
4833 | } |
4834 | ||
70f02287 BB |
4835 | /* |
4836 | * Return the amount of memory that is considered free. In user space | |
4837 | * which is primarily used for testing we pretend that free memory ranges | |
4838 | * from 0-20% of all memory. | |
4839 | */ | |
787acae0 GDN |
4840 | static uint64_t |
4841 | arc_free_memory(void) | |
4842 | { | |
70f02287 BB |
4843 | #ifdef _KERNEL |
4844 | #ifdef CONFIG_HIGHMEM | |
4845 | struct sysinfo si; | |
4846 | si_meminfo(&si); | |
4847 | return (ptob(si.freeram - si.freehigh)); | |
4848 | #else | |
70f02287 | 4849 | return (ptob(nr_free_pages() + |
e9a77290 | 4850 | nr_inactive_file_pages() + |
4851 | nr_inactive_anon_pages() + | |
4852 | nr_slab_reclaimable_pages())); | |
4853 | ||
70f02287 BB |
4854 | #endif /* CONFIG_HIGHMEM */ |
4855 | #else | |
4856 | return (spa_get_random(arc_all_memory() * 20 / 100)); | |
4857 | #endif /* _KERNEL */ | |
787acae0 | 4858 | } |
787acae0 | 4859 | |
ca67b33a MA |
4860 | typedef enum free_memory_reason_t { |
4861 | FMR_UNKNOWN, | |
4862 | FMR_NEEDFREE, | |
4863 | FMR_LOTSFREE, | |
4864 | FMR_SWAPFS_MINFREE, | |
4865 | FMR_PAGES_PP_MAXIMUM, | |
4866 | FMR_HEAP_ARENA, | |
4867 | FMR_ZIO_ARENA, | |
4868 | } free_memory_reason_t; | |
4869 | ||
4870 | int64_t last_free_memory; | |
4871 | free_memory_reason_t last_free_reason; | |
4872 | ||
4873 | #ifdef _KERNEL | |
ca67b33a MA |
4874 | /* |
4875 | * Additional reserve of pages for pp_reserve. | |
4876 | */ | |
4877 | int64_t arc_pages_pp_reserve = 64; | |
4878 | ||
4879 | /* | |
4880 | * Additional reserve of pages for swapfs. | |
4881 | */ | |
4882 | int64_t arc_swapfs_reserve = 64; | |
ca67b33a MA |
4883 | #endif /* _KERNEL */ |
4884 | ||
4885 | /* | |
4886 | * Return the amount of memory that can be consumed before reclaim will be | |
4887 | * needed. Positive if there is sufficient free memory, negative indicates | |
4888 | * the amount of memory that needs to be freed up. | |
4889 | */ | |
4890 | static int64_t | |
4891 | arc_available_memory(void) | |
4892 | { | |
4893 | int64_t lowest = INT64_MAX; | |
4894 | free_memory_reason_t r = FMR_UNKNOWN; | |
ca67b33a | 4895 | #ifdef _KERNEL |
ca67b33a | 4896 | int64_t n; |
11f552fa | 4897 | #ifdef __linux__ |
70f02287 BB |
4898 | #ifdef freemem |
4899 | #undef freemem | |
4900 | #endif | |
11f552fa BB |
4901 | pgcnt_t needfree = btop(arc_need_free); |
4902 | pgcnt_t lotsfree = btop(arc_sys_free); | |
4903 | pgcnt_t desfree = 0; | |
70f02287 | 4904 | pgcnt_t freemem = btop(arc_free_memory()); |
9edb3695 BB |
4905 | #endif |
4906 | ||
ca67b33a MA |
4907 | if (needfree > 0) { |
4908 | n = PAGESIZE * (-needfree); | |
4909 | if (n < lowest) { | |
4910 | lowest = n; | |
4911 | r = FMR_NEEDFREE; | |
4912 | } | |
4913 | } | |
4914 | ||
4915 | /* | |
4916 | * check that we're out of range of the pageout scanner. It starts to | |
4917 | * schedule paging if freemem is less than lotsfree and needfree. | |
4918 | * lotsfree is the high-water mark for pageout, and needfree is the | |
4919 | * number of needed free pages. We add extra pages here to make sure | |
4920 | * the scanner doesn't start up while we're freeing memory. | |
4921 | */ | |
70f02287 | 4922 | n = PAGESIZE * (freemem - lotsfree - needfree - desfree); |
ca67b33a MA |
4923 | if (n < lowest) { |
4924 | lowest = n; | |
4925 | r = FMR_LOTSFREE; | |
4926 | } | |
4927 | ||
11f552fa | 4928 | #ifndef __linux__ |
ca67b33a MA |
4929 | /* |
4930 | * check to make sure that swapfs has enough space so that anon | |
4931 | * reservations can still succeed. anon_resvmem() checks that the | |
4932 | * availrmem is greater than swapfs_minfree, and the number of reserved | |
4933 | * swap pages. We also add a bit of extra here just to prevent | |
4934 | * circumstances from getting really dire. | |
4935 | */ | |
4936 | n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve - | |
4937 | desfree - arc_swapfs_reserve); | |
4938 | if (n < lowest) { | |
4939 | lowest = n; | |
4940 | r = FMR_SWAPFS_MINFREE; | |
4941 | } | |
4942 | ||
ca67b33a MA |
4943 | /* |
4944 | * Check that we have enough availrmem that memory locking (e.g., via | |
4945 | * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum | |
4946 | * stores the number of pages that cannot be locked; when availrmem | |
4947 | * drops below pages_pp_maximum, page locking mechanisms such as | |
4948 | * page_pp_lock() will fail.) | |
4949 | */ | |
4950 | n = PAGESIZE * (availrmem - pages_pp_maximum - | |
4951 | arc_pages_pp_reserve); | |
4952 | if (n < lowest) { | |
4953 | lowest = n; | |
4954 | r = FMR_PAGES_PP_MAXIMUM; | |
4955 | } | |
11f552fa | 4956 | #endif |
ca67b33a | 4957 | |
70f02287 | 4958 | #if defined(_ILP32) |
ca67b33a | 4959 | /* |
70f02287 | 4960 | * If we're on a 32-bit platform, it's possible that we'll exhaust the |
ca67b33a MA |
4961 | * kernel heap space before we ever run out of available physical |
4962 | * memory. Most checks of the size of the heap_area compare against | |
4963 | * tune.t_minarmem, which is the minimum available real memory that we | |
4964 | * can have in the system. However, this is generally fixed at 25 pages | |
4965 | * which is so low that it's useless. In this comparison, we seek to | |
4966 | * calculate the total heap-size, and reclaim if more than 3/4ths of the | |
4967 | * heap is allocated. (Or, in the calculation, if less than 1/4th is | |
4968 | * free) | |
4969 | */ | |
4970 | n = vmem_size(heap_arena, VMEM_FREE) - | |
4971 | (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2); | |
4972 | if (n < lowest) { | |
4973 | lowest = n; | |
4974 | r = FMR_HEAP_ARENA; | |
4975 | } | |
4976 | #endif | |
4977 | ||
4978 | /* | |
4979 | * If zio data pages are being allocated out of a separate heap segment, | |
4980 | * then enforce that the size of available vmem for this arena remains | |
d3c2ae1c | 4981 | * above about 1/4th (1/(2^arc_zio_arena_free_shift)) free. |
ca67b33a | 4982 | * |
d3c2ae1c GW |
4983 | * Note that reducing the arc_zio_arena_free_shift keeps more virtual |
4984 | * memory (in the zio_arena) free, which can avoid memory | |
4985 | * fragmentation issues. | |
ca67b33a MA |
4986 | */ |
4987 | if (zio_arena != NULL) { | |
9edb3695 BB |
4988 | n = (int64_t)vmem_size(zio_arena, VMEM_FREE) - |
4989 | (vmem_size(zio_arena, VMEM_ALLOC) >> | |
4990 | arc_zio_arena_free_shift); | |
ca67b33a MA |
4991 | if (n < lowest) { |
4992 | lowest = n; | |
4993 | r = FMR_ZIO_ARENA; | |
4994 | } | |
4995 | } | |
11f552fa | 4996 | #else /* _KERNEL */ |
ca67b33a MA |
4997 | /* Every 100 calls, free a small amount */ |
4998 | if (spa_get_random(100) == 0) | |
4999 | lowest = -1024; | |
11f552fa | 5000 | #endif /* _KERNEL */ |
ca67b33a MA |
5001 | |
5002 | last_free_memory = lowest; | |
5003 | last_free_reason = r; | |
5004 | ||
5005 | return (lowest); | |
5006 | } | |
5007 | ||
5008 | /* | |
5009 | * Determine if the system is under memory pressure and is asking | |
d3c2ae1c | 5010 | * to reclaim memory. A return value of B_TRUE indicates that the system |
ca67b33a MA |
5011 | * is under memory pressure and that the arc should adjust accordingly. |
5012 | */ | |
5013 | static boolean_t | |
5014 | arc_reclaim_needed(void) | |
5015 | { | |
5016 | return (arc_available_memory() < 0); | |
5017 | } | |
5018 | ||
34dc7c2f | 5019 | static void |
3ec34e55 | 5020 | arc_kmem_reap_soon(void) |
34dc7c2f BB |
5021 | { |
5022 | size_t i; | |
5023 | kmem_cache_t *prev_cache = NULL; | |
5024 | kmem_cache_t *prev_data_cache = NULL; | |
5025 | extern kmem_cache_t *zio_buf_cache[]; | |
5026 | extern kmem_cache_t *zio_data_buf_cache[]; | |
669dedb3 | 5027 | extern kmem_cache_t *range_seg_cache; |
34dc7c2f | 5028 | |
70f02287 | 5029 | #ifdef _KERNEL |
37fb3e43 PD |
5030 | if ((aggsum_compare(&arc_meta_used, arc_meta_limit) >= 0) && |
5031 | zfs_arc_meta_prune) { | |
f6046738 BB |
5032 | /* |
5033 | * We are exceeding our meta-data cache limit. | |
5034 | * Prune some entries to release holds on meta-data. | |
5035 | */ | |
ef5b2e10 | 5036 | arc_prune_async(zfs_arc_meta_prune); |
f6046738 | 5037 | } |
70f02287 BB |
5038 | #if defined(_ILP32) |
5039 | /* | |
5040 | * Reclaim unused memory from all kmem caches. | |
5041 | */ | |
5042 | kmem_reap(); | |
5043 | #endif | |
5044 | #endif | |
f6046738 | 5045 | |
34dc7c2f | 5046 | for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { |
70f02287 | 5047 | #if defined(_ILP32) |
d0c614ec | 5048 | /* reach upper limit of cache size on 32-bit */ |
5049 | if (zio_buf_cache[i] == NULL) | |
5050 | break; | |
5051 | #endif | |
34dc7c2f BB |
5052 | if (zio_buf_cache[i] != prev_cache) { |
5053 | prev_cache = zio_buf_cache[i]; | |
5054 | kmem_cache_reap_now(zio_buf_cache[i]); | |
5055 | } | |
5056 | if (zio_data_buf_cache[i] != prev_data_cache) { | |
5057 | prev_data_cache = zio_data_buf_cache[i]; | |
5058 | kmem_cache_reap_now(zio_data_buf_cache[i]); | |
5059 | } | |
5060 | } | |
ca0bf58d | 5061 | kmem_cache_reap_now(buf_cache); |
b9541d6b CW |
5062 | kmem_cache_reap_now(hdr_full_cache); |
5063 | kmem_cache_reap_now(hdr_l2only_cache); | |
669dedb3 | 5064 | kmem_cache_reap_now(range_seg_cache); |
ca67b33a MA |
5065 | |
5066 | if (zio_arena != NULL) { | |
5067 | /* | |
5068 | * Ask the vmem arena to reclaim unused memory from its | |
5069 | * quantum caches. | |
5070 | */ | |
5071 | vmem_qcache_reap(zio_arena); | |
5072 | } | |
34dc7c2f BB |
5073 | } |
5074 | ||
3ec34e55 BL |
5075 | /* ARGSUSED */ |
5076 | static boolean_t | |
5077 | arc_adjust_cb_check(void *arg, zthr_t *zthr) | |
5078 | { | |
5079 | /* | |
5080 | * This is necessary in order to keep the kstat information | |
5081 | * up to date for tools that display kstat data such as the | |
5082 | * mdb ::arc dcmd and the Linux crash utility. These tools | |
5083 | * typically do not call kstat's update function, but simply | |
5084 | * dump out stats from the most recent update. Without | |
5085 | * this call, these commands may show stale stats for the | |
5086 | * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even | |
5087 | * with this change, the data might be up to 1 second | |
5088 | * out of date(the arc_adjust_zthr has a maximum sleep | |
5089 | * time of 1 second); but that should suffice. The | |
5090 | * arc_state_t structures can be queried directly if more | |
5091 | * accurate information is needed. | |
5092 | */ | |
5093 | if (arc_ksp != NULL) | |
5094 | arc_ksp->ks_update(arc_ksp, KSTAT_READ); | |
5095 | ||
5096 | /* | |
5097 | * We have to rely on arc_get_data_impl() to tell us when to adjust, | |
5098 | * rather than checking if we are overflowing here, so that we are | |
5099 | * sure to not leave arc_get_data_impl() waiting on | |
5100 | * arc_adjust_waiters_cv. If we have become "not overflowing" since | |
5101 | * arc_get_data_impl() checked, we need to wake it up. We could | |
5102 | * broadcast the CV here, but arc_get_data_impl() may have not yet | |
5103 | * gone to sleep. We would need to use a mutex to ensure that this | |
5104 | * function doesn't broadcast until arc_get_data_impl() has gone to | |
5105 | * sleep (e.g. the arc_adjust_lock). However, the lock ordering of | |
5106 | * such a lock would necessarily be incorrect with respect to the | |
5107 | * zthr_lock, which is held before this function is called, and is | |
5108 | * held by arc_get_data_impl() when it calls zthr_wakeup(). | |
5109 | */ | |
5110 | return (arc_adjust_needed); | |
5111 | } | |
5112 | ||
302f753f | 5113 | /* |
3ec34e55 BL |
5114 | * Keep arc_size under arc_c by running arc_adjust which evicts data |
5115 | * from the ARC. | |
302f753f | 5116 | */ |
867959b5 | 5117 | /* ARGSUSED */ |
61c3391a | 5118 | static void |
3ec34e55 | 5119 | arc_adjust_cb(void *arg, zthr_t *zthr) |
34dc7c2f | 5120 | { |
3ec34e55 BL |
5121 | uint64_t evicted = 0; |
5122 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
34dc7c2f | 5123 | |
3ec34e55 BL |
5124 | /* Evict from cache */ |
5125 | evicted = arc_adjust(); | |
34dc7c2f | 5126 | |
3ec34e55 BL |
5127 | /* |
5128 | * If evicted is zero, we couldn't evict anything | |
5129 | * via arc_adjust(). This could be due to hash lock | |
5130 | * collisions, but more likely due to the majority of | |
5131 | * arc buffers being unevictable. Therefore, even if | |
5132 | * arc_size is above arc_c, another pass is unlikely to | |
5133 | * be helpful and could potentially cause us to enter an | |
5134 | * infinite loop. Additionally, zthr_iscancelled() is | |
5135 | * checked here so that if the arc is shutting down, the | |
5136 | * broadcast will wake any remaining arc adjust waiters. | |
5137 | */ | |
5138 | mutex_enter(&arc_adjust_lock); | |
5139 | arc_adjust_needed = !zthr_iscancelled(arc_adjust_zthr) && | |
5140 | evicted > 0 && aggsum_compare(&arc_size, arc_c) > 0; | |
5141 | if (!arc_adjust_needed) { | |
d3c2ae1c | 5142 | /* |
3ec34e55 BL |
5143 | * We're either no longer overflowing, or we |
5144 | * can't evict anything more, so we should wake | |
5145 | * arc_get_data_impl() sooner. | |
d3c2ae1c | 5146 | */ |
3ec34e55 BL |
5147 | cv_broadcast(&arc_adjust_waiters_cv); |
5148 | arc_need_free = 0; | |
5149 | } | |
5150 | mutex_exit(&arc_adjust_lock); | |
5151 | spl_fstrans_unmark(cookie); | |
3ec34e55 BL |
5152 | } |
5153 | ||
5154 | /* ARGSUSED */ | |
5155 | static boolean_t | |
5156 | arc_reap_cb_check(void *arg, zthr_t *zthr) | |
5157 | { | |
5158 | int64_t free_memory = arc_available_memory(); | |
5159 | ||
5160 | /* | |
5161 | * If a kmem reap is already active, don't schedule more. We must | |
5162 | * check for this because kmem_cache_reap_soon() won't actually | |
5163 | * block on the cache being reaped (this is to prevent callers from | |
5164 | * becoming implicitly blocked by a system-wide kmem reap -- which, | |
5165 | * on a system with many, many full magazines, can take minutes). | |
5166 | */ | |
5167 | if (!kmem_cache_reap_active() && free_memory < 0) { | |
34dc7c2f | 5168 | |
3ec34e55 BL |
5169 | arc_no_grow = B_TRUE; |
5170 | arc_warm = B_TRUE; | |
0a252dae | 5171 | /* |
3ec34e55 BL |
5172 | * Wait at least zfs_grow_retry (default 5) seconds |
5173 | * before considering growing. | |
0a252dae | 5174 | */ |
3ec34e55 BL |
5175 | arc_growtime = gethrtime() + SEC2NSEC(arc_grow_retry); |
5176 | return (B_TRUE); | |
5177 | } else if (free_memory < arc_c >> arc_no_grow_shift) { | |
5178 | arc_no_grow = B_TRUE; | |
5179 | } else if (gethrtime() >= arc_growtime) { | |
5180 | arc_no_grow = B_FALSE; | |
5181 | } | |
0a252dae | 5182 | |
3ec34e55 BL |
5183 | return (B_FALSE); |
5184 | } | |
34dc7c2f | 5185 | |
3ec34e55 BL |
5186 | /* |
5187 | * Keep enough free memory in the system by reaping the ARC's kmem | |
5188 | * caches. To cause more slabs to be reapable, we may reduce the | |
5189 | * target size of the cache (arc_c), causing the arc_adjust_cb() | |
5190 | * to free more buffers. | |
5191 | */ | |
5192 | /* ARGSUSED */ | |
61c3391a | 5193 | static void |
3ec34e55 BL |
5194 | arc_reap_cb(void *arg, zthr_t *zthr) |
5195 | { | |
5196 | int64_t free_memory; | |
5197 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
34dc7c2f | 5198 | |
3ec34e55 BL |
5199 | /* |
5200 | * Kick off asynchronous kmem_reap()'s of all our caches. | |
5201 | */ | |
5202 | arc_kmem_reap_soon(); | |
6a8f9b6b | 5203 | |
3ec34e55 BL |
5204 | /* |
5205 | * Wait at least arc_kmem_cache_reap_retry_ms between | |
5206 | * arc_kmem_reap_soon() calls. Without this check it is possible to | |
5207 | * end up in a situation where we spend lots of time reaping | |
5208 | * caches, while we're near arc_c_min. Waiting here also gives the | |
5209 | * subsequent free memory check a chance of finding that the | |
5210 | * asynchronous reap has already freed enough memory, and we don't | |
5211 | * need to call arc_reduce_target_size(). | |
5212 | */ | |
5213 | delay((hz * arc_kmem_cache_reap_retry_ms + 999) / 1000); | |
34dc7c2f | 5214 | |
3ec34e55 BL |
5215 | /* |
5216 | * Reduce the target size as needed to maintain the amount of free | |
5217 | * memory in the system at a fraction of the arc_size (1/128th by | |
5218 | * default). If oversubscribed (free_memory < 0) then reduce the | |
5219 | * target arc_size by the deficit amount plus the fractional | |
5220 | * amount. If free memory is positive but less then the fractional | |
5221 | * amount, reduce by what is needed to hit the fractional amount. | |
5222 | */ | |
5223 | free_memory = arc_available_memory(); | |
34dc7c2f | 5224 | |
3ec34e55 BL |
5225 | int64_t to_free = |
5226 | (arc_c >> arc_shrink_shift) - free_memory; | |
5227 | if (to_free > 0) { | |
ca67b33a | 5228 | #ifdef _KERNEL |
3ec34e55 | 5229 | to_free = MAX(to_free, arc_need_free); |
ca67b33a | 5230 | #endif |
3ec34e55 | 5231 | arc_reduce_target_size(to_free); |
ca0bf58d | 5232 | } |
ca0bf58d | 5233 | spl_fstrans_unmark(cookie); |
ca0bf58d PS |
5234 | } |
5235 | ||
7cb67b45 BB |
5236 | #ifdef _KERNEL |
5237 | /* | |
302f753f BB |
5238 | * Determine the amount of memory eligible for eviction contained in the |
5239 | * ARC. All clean data reported by the ghost lists can always be safely | |
5240 | * evicted. Due to arc_c_min, the same does not hold for all clean data | |
5241 | * contained by the regular mru and mfu lists. | |
5242 | * | |
5243 | * In the case of the regular mru and mfu lists, we need to report as | |
5244 | * much clean data as possible, such that evicting that same reported | |
5245 | * data will not bring arc_size below arc_c_min. Thus, in certain | |
5246 | * circumstances, the total amount of clean data in the mru and mfu | |
5247 | * lists might not actually be evictable. | |
5248 | * | |
5249 | * The following two distinct cases are accounted for: | |
5250 | * | |
5251 | * 1. The sum of the amount of dirty data contained by both the mru and | |
5252 | * mfu lists, plus the ARC's other accounting (e.g. the anon list), | |
5253 | * is greater than or equal to arc_c_min. | |
5254 | * (i.e. amount of dirty data >= arc_c_min) | |
5255 | * | |
5256 | * This is the easy case; all clean data contained by the mru and mfu | |
5257 | * lists is evictable. Evicting all clean data can only drop arc_size | |
5258 | * to the amount of dirty data, which is greater than arc_c_min. | |
5259 | * | |
5260 | * 2. The sum of the amount of dirty data contained by both the mru and | |
5261 | * mfu lists, plus the ARC's other accounting (e.g. the anon list), | |
5262 | * is less than arc_c_min. | |
5263 | * (i.e. arc_c_min > amount of dirty data) | |
5264 | * | |
5265 | * 2.1. arc_size is greater than or equal arc_c_min. | |
5266 | * (i.e. arc_size >= arc_c_min > amount of dirty data) | |
5267 | * | |
5268 | * In this case, not all clean data from the regular mru and mfu | |
5269 | * lists is actually evictable; we must leave enough clean data | |
5270 | * to keep arc_size above arc_c_min. Thus, the maximum amount of | |
5271 | * evictable data from the two lists combined, is exactly the | |
5272 | * difference between arc_size and arc_c_min. | |
5273 | * | |
5274 | * 2.2. arc_size is less than arc_c_min | |
5275 | * (i.e. arc_c_min > arc_size > amount of dirty data) | |
5276 | * | |
5277 | * In this case, none of the data contained in the mru and mfu | |
5278 | * lists is evictable, even if it's clean. Since arc_size is | |
5279 | * already below arc_c_min, evicting any more would only | |
5280 | * increase this negative difference. | |
7cb67b45 | 5281 | */ |
302f753f | 5282 | static uint64_t |
4ea3f864 GM |
5283 | arc_evictable_memory(void) |
5284 | { | |
37fb3e43 | 5285 | int64_t asize = aggsum_value(&arc_size); |
302f753f | 5286 | uint64_t arc_clean = |
424fd7c3 TS |
5287 | zfs_refcount_count(&arc_mru->arcs_esize[ARC_BUFC_DATA]) + |
5288 | zfs_refcount_count(&arc_mru->arcs_esize[ARC_BUFC_METADATA]) + | |
5289 | zfs_refcount_count(&arc_mfu->arcs_esize[ARC_BUFC_DATA]) + | |
5290 | zfs_refcount_count(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); | |
37fb3e43 | 5291 | uint64_t arc_dirty = MAX((int64_t)asize - (int64_t)arc_clean, 0); |
302f753f | 5292 | |
03b60eee DB |
5293 | /* |
5294 | * Scale reported evictable memory in proportion to page cache, cap | |
5295 | * at specified min/max. | |
5296 | */ | |
e9a77290 | 5297 | uint64_t min = (ptob(nr_file_pages()) / 100) * zfs_arc_pc_percent; |
03b60eee DB |
5298 | min = MAX(arc_c_min, MIN(arc_c_max, min)); |
5299 | ||
5300 | if (arc_dirty >= min) | |
9b50146d | 5301 | return (arc_clean); |
302f753f | 5302 | |
37fb3e43 | 5303 | return (MAX((int64_t)asize - (int64_t)min, 0)); |
302f753f BB |
5304 | } |
5305 | ||
ed6e9cc2 TC |
5306 | /* |
5307 | * If sc->nr_to_scan is zero, the caller is requesting a query of the | |
5308 | * number of objects which can potentially be freed. If it is nonzero, | |
5309 | * the request is to free that many objects. | |
5310 | * | |
5311 | * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks | |
5312 | * in struct shrinker and also require the shrinker to return the number | |
5313 | * of objects freed. | |
5314 | * | |
5315 | * Older kernels require the shrinker to return the number of freeable | |
5316 | * objects following the freeing of nr_to_free. | |
5317 | */ | |
5318 | static spl_shrinker_t | |
7e7baeca | 5319 | __arc_shrinker_func(struct shrinker *shrink, struct shrink_control *sc) |
7cb67b45 | 5320 | { |
ed6e9cc2 | 5321 | int64_t pages; |
7cb67b45 | 5322 | |
302f753f BB |
5323 | /* The arc is considered warm once reclaim has occurred */ |
5324 | if (unlikely(arc_warm == B_FALSE)) | |
5325 | arc_warm = B_TRUE; | |
7cb67b45 | 5326 | |
302f753f | 5327 | /* Return the potential number of reclaimable pages */ |
ed6e9cc2 | 5328 | pages = btop((int64_t)arc_evictable_memory()); |
302f753f BB |
5329 | if (sc->nr_to_scan == 0) |
5330 | return (pages); | |
3fd70ee6 BB |
5331 | |
5332 | /* Not allowed to perform filesystem reclaim */ | |
7e7baeca | 5333 | if (!(sc->gfp_mask & __GFP_FS)) |
ed6e9cc2 | 5334 | return (SHRINK_STOP); |
3fd70ee6 | 5335 | |
7cb67b45 | 5336 | /* Reclaim in progress */ |
3ec34e55 | 5337 | if (mutex_tryenter(&arc_adjust_lock) == 0) { |
b855550c | 5338 | ARCSTAT_INCR(arcstat_need_free, ptob(sc->nr_to_scan)); |
2e91c2fb | 5339 | return (0); |
b855550c | 5340 | } |
7cb67b45 | 5341 | |
3ec34e55 | 5342 | mutex_exit(&arc_adjust_lock); |
ca0bf58d | 5343 | |
302f753f BB |
5344 | /* |
5345 | * Evict the requested number of pages by shrinking arc_c the | |
44813aef | 5346 | * requested amount. |
302f753f BB |
5347 | */ |
5348 | if (pages > 0) { | |
3ec34e55 | 5349 | arc_reduce_target_size(ptob(sc->nr_to_scan)); |
44813aef | 5350 | if (current_is_kswapd()) |
3ec34e55 | 5351 | arc_kmem_reap_soon(); |
ed6e9cc2 | 5352 | #ifdef HAVE_SPLIT_SHRINKER_CALLBACK |
4149bf49 DB |
5353 | pages = MAX((int64_t)pages - |
5354 | (int64_t)btop(arc_evictable_memory()), 0); | |
ed6e9cc2 | 5355 | #else |
1e3cb67b | 5356 | pages = btop(arc_evictable_memory()); |
ed6e9cc2 | 5357 | #endif |
1a31dcf5 DB |
5358 | /* |
5359 | * We've shrunk what we can, wake up threads. | |
5360 | */ | |
3ec34e55 | 5361 | cv_broadcast(&arc_adjust_waiters_cv); |
44813aef | 5362 | } else |
ed6e9cc2 | 5363 | pages = SHRINK_STOP; |
302f753f BB |
5364 | |
5365 | /* | |
5366 | * When direct reclaim is observed it usually indicates a rapid | |
5367 | * increase in memory pressure. This occurs because the kswapd | |
5368 | * threads were unable to asynchronously keep enough free memory | |
5369 | * available. In this case set arc_no_grow to briefly pause arc | |
5370 | * growth to avoid compounding the memory pressure. | |
5371 | */ | |
7cb67b45 | 5372 | if (current_is_kswapd()) { |
302f753f | 5373 | ARCSTAT_BUMP(arcstat_memory_indirect_count); |
7cb67b45 | 5374 | } else { |
302f753f | 5375 | arc_no_grow = B_TRUE; |
3ec34e55 | 5376 | arc_kmem_reap_soon(); |
302f753f | 5377 | ARCSTAT_BUMP(arcstat_memory_direct_count); |
7cb67b45 BB |
5378 | } |
5379 | ||
1e3cb67b | 5380 | return (pages); |
7cb67b45 | 5381 | } |
7e7baeca | 5382 | SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func); |
7cb67b45 BB |
5383 | |
5384 | SPL_SHRINKER_DECLARE(arc_shrinker, arc_shrinker_func, DEFAULT_SEEKS); | |
5385 | #endif /* _KERNEL */ | |
5386 | ||
34dc7c2f BB |
5387 | /* |
5388 | * Adapt arc info given the number of bytes we are trying to add and | |
4e33ba4c | 5389 | * the state that we are coming from. This function is only called |
34dc7c2f BB |
5390 | * when we are adding new content to the cache. |
5391 | */ | |
5392 | static void | |
5393 | arc_adapt(int bytes, arc_state_t *state) | |
5394 | { | |
5395 | int mult; | |
728d6ae9 | 5396 | uint64_t arc_p_min = (arc_c >> arc_p_min_shift); |
424fd7c3 TS |
5397 | int64_t mrug_size = zfs_refcount_count(&arc_mru_ghost->arcs_size); |
5398 | int64_t mfug_size = zfs_refcount_count(&arc_mfu_ghost->arcs_size); | |
34dc7c2f BB |
5399 | |
5400 | if (state == arc_l2c_only) | |
5401 | return; | |
5402 | ||
5403 | ASSERT(bytes > 0); | |
5404 | /* | |
5405 | * Adapt the target size of the MRU list: | |
5406 | * - if we just hit in the MRU ghost list, then increase | |
5407 | * the target size of the MRU list. | |
5408 | * - if we just hit in the MFU ghost list, then increase | |
5409 | * the target size of the MFU list by decreasing the | |
5410 | * target size of the MRU list. | |
5411 | */ | |
5412 | if (state == arc_mru_ghost) { | |
36da08ef | 5413 | mult = (mrug_size >= mfug_size) ? 1 : (mfug_size / mrug_size); |
62422785 PS |
5414 | if (!zfs_arc_p_dampener_disable) |
5415 | mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ | |
34dc7c2f | 5416 | |
728d6ae9 | 5417 | arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); |
34dc7c2f | 5418 | } else if (state == arc_mfu_ghost) { |
d164b209 BB |
5419 | uint64_t delta; |
5420 | ||
36da08ef | 5421 | mult = (mfug_size >= mrug_size) ? 1 : (mrug_size / mfug_size); |
62422785 PS |
5422 | if (!zfs_arc_p_dampener_disable) |
5423 | mult = MIN(mult, 10); | |
34dc7c2f | 5424 | |
d164b209 | 5425 | delta = MIN(bytes * mult, arc_p); |
728d6ae9 | 5426 | arc_p = MAX(arc_p_min, arc_p - delta); |
34dc7c2f BB |
5427 | } |
5428 | ASSERT((int64_t)arc_p >= 0); | |
5429 | ||
3ec34e55 BL |
5430 | /* |
5431 | * Wake reap thread if we do not have any available memory | |
5432 | */ | |
ca67b33a | 5433 | if (arc_reclaim_needed()) { |
3ec34e55 | 5434 | zthr_wakeup(arc_reap_zthr); |
ca67b33a MA |
5435 | return; |
5436 | } | |
5437 | ||
34dc7c2f BB |
5438 | if (arc_no_grow) |
5439 | return; | |
5440 | ||
5441 | if (arc_c >= arc_c_max) | |
5442 | return; | |
5443 | ||
5444 | /* | |
5445 | * If we're within (2 * maxblocksize) bytes of the target | |
5446 | * cache size, increment the target cache size | |
5447 | */ | |
935434ef | 5448 | ASSERT3U(arc_c, >=, 2ULL << SPA_MAXBLOCKSHIFT); |
37fb3e43 PD |
5449 | if (aggsum_compare(&arc_size, arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) >= |
5450 | 0) { | |
34dc7c2f BB |
5451 | atomic_add_64(&arc_c, (int64_t)bytes); |
5452 | if (arc_c > arc_c_max) | |
5453 | arc_c = arc_c_max; | |
5454 | else if (state == arc_anon) | |
5455 | atomic_add_64(&arc_p, (int64_t)bytes); | |
5456 | if (arc_p > arc_c) | |
5457 | arc_p = arc_c; | |
5458 | } | |
5459 | ASSERT((int64_t)arc_p >= 0); | |
5460 | } | |
5461 | ||
5462 | /* | |
ca0bf58d PS |
5463 | * Check if arc_size has grown past our upper threshold, determined by |
5464 | * zfs_arc_overflow_shift. | |
34dc7c2f | 5465 | */ |
ca0bf58d PS |
5466 | static boolean_t |
5467 | arc_is_overflowing(void) | |
34dc7c2f | 5468 | { |
ca0bf58d PS |
5469 | /* Always allow at least one block of overflow */ |
5470 | uint64_t overflow = MAX(SPA_MAXBLOCKSIZE, | |
5471 | arc_c >> zfs_arc_overflow_shift); | |
34dc7c2f | 5472 | |
37fb3e43 PD |
5473 | /* |
5474 | * We just compare the lower bound here for performance reasons. Our | |
5475 | * primary goals are to make sure that the arc never grows without | |
5476 | * bound, and that it can reach its maximum size. This check | |
5477 | * accomplishes both goals. The maximum amount we could run over by is | |
5478 | * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block | |
5479 | * in the ARC. In practice, that's in the tens of MB, which is low | |
5480 | * enough to be safe. | |
5481 | */ | |
5482 | return (aggsum_lower_bound(&arc_size) >= arc_c + overflow); | |
34dc7c2f BB |
5483 | } |
5484 | ||
a6255b7f DQ |
5485 | static abd_t * |
5486 | arc_get_data_abd(arc_buf_hdr_t *hdr, uint64_t size, void *tag) | |
5487 | { | |
5488 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5489 | ||
5490 | arc_get_data_impl(hdr, size, tag); | |
5491 | if (type == ARC_BUFC_METADATA) { | |
5492 | return (abd_alloc(size, B_TRUE)); | |
5493 | } else { | |
5494 | ASSERT(type == ARC_BUFC_DATA); | |
5495 | return (abd_alloc(size, B_FALSE)); | |
5496 | } | |
5497 | } | |
5498 | ||
5499 | static void * | |
5500 | arc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, void *tag) | |
5501 | { | |
5502 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5503 | ||
5504 | arc_get_data_impl(hdr, size, tag); | |
5505 | if (type == ARC_BUFC_METADATA) { | |
5506 | return (zio_buf_alloc(size)); | |
5507 | } else { | |
5508 | ASSERT(type == ARC_BUFC_DATA); | |
5509 | return (zio_data_buf_alloc(size)); | |
5510 | } | |
5511 | } | |
5512 | ||
34dc7c2f | 5513 | /* |
d3c2ae1c GW |
5514 | * Allocate a block and return it to the caller. If we are hitting the |
5515 | * hard limit for the cache size, we must sleep, waiting for the eviction | |
5516 | * thread to catch up. If we're past the target size but below the hard | |
5517 | * limit, we'll only signal the reclaim thread and continue on. | |
34dc7c2f | 5518 | */ |
a6255b7f DQ |
5519 | static void |
5520 | arc_get_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) | |
34dc7c2f | 5521 | { |
a6255b7f DQ |
5522 | arc_state_t *state = hdr->b_l1hdr.b_state; |
5523 | arc_buf_contents_t type = arc_buf_type(hdr); | |
34dc7c2f BB |
5524 | |
5525 | arc_adapt(size, state); | |
5526 | ||
5527 | /* | |
ca0bf58d PS |
5528 | * If arc_size is currently overflowing, and has grown past our |
5529 | * upper limit, we must be adding data faster than the evict | |
5530 | * thread can evict. Thus, to ensure we don't compound the | |
5531 | * problem by adding more data and forcing arc_size to grow even | |
5532 | * further past it's target size, we halt and wait for the | |
5533 | * eviction thread to catch up. | |
5534 | * | |
5535 | * It's also possible that the reclaim thread is unable to evict | |
5536 | * enough buffers to get arc_size below the overflow limit (e.g. | |
5537 | * due to buffers being un-evictable, or hash lock collisions). | |
5538 | * In this case, we want to proceed regardless if we're | |
5539 | * overflowing; thus we don't use a while loop here. | |
34dc7c2f | 5540 | */ |
ca0bf58d | 5541 | if (arc_is_overflowing()) { |
3ec34e55 | 5542 | mutex_enter(&arc_adjust_lock); |
ca0bf58d PS |
5543 | |
5544 | /* | |
5545 | * Now that we've acquired the lock, we may no longer be | |
5546 | * over the overflow limit, lets check. | |
5547 | * | |
5548 | * We're ignoring the case of spurious wake ups. If that | |
5549 | * were to happen, it'd let this thread consume an ARC | |
5550 | * buffer before it should have (i.e. before we're under | |
5551 | * the overflow limit and were signalled by the reclaim | |
5552 | * thread). As long as that is a rare occurrence, it | |
5553 | * shouldn't cause any harm. | |
5554 | */ | |
5555 | if (arc_is_overflowing()) { | |
3ec34e55 BL |
5556 | arc_adjust_needed = B_TRUE; |
5557 | zthr_wakeup(arc_adjust_zthr); | |
5558 | (void) cv_wait(&arc_adjust_waiters_cv, | |
5559 | &arc_adjust_lock); | |
34dc7c2f | 5560 | } |
3ec34e55 | 5561 | mutex_exit(&arc_adjust_lock); |
34dc7c2f | 5562 | } |
ab26409d | 5563 | |
d3c2ae1c | 5564 | VERIFY3U(hdr->b_type, ==, type); |
da8ccd0e | 5565 | if (type == ARC_BUFC_METADATA) { |
ca0bf58d PS |
5566 | arc_space_consume(size, ARC_SPACE_META); |
5567 | } else { | |
ca0bf58d | 5568 | arc_space_consume(size, ARC_SPACE_DATA); |
da8ccd0e PS |
5569 | } |
5570 | ||
34dc7c2f BB |
5571 | /* |
5572 | * Update the state size. Note that ghost states have a | |
5573 | * "ghost size" and so don't need to be updated. | |
5574 | */ | |
d3c2ae1c | 5575 | if (!GHOST_STATE(state)) { |
34dc7c2f | 5576 | |
424fd7c3 | 5577 | (void) zfs_refcount_add_many(&state->arcs_size, size, tag); |
ca0bf58d PS |
5578 | |
5579 | /* | |
5580 | * If this is reached via arc_read, the link is | |
5581 | * protected by the hash lock. If reached via | |
5582 | * arc_buf_alloc, the header should not be accessed by | |
5583 | * any other thread. And, if reached via arc_read_done, | |
5584 | * the hash lock will protect it if it's found in the | |
5585 | * hash table; otherwise no other thread should be | |
5586 | * trying to [add|remove]_reference it. | |
5587 | */ | |
5588 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 TS |
5589 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
5590 | (void) zfs_refcount_add_many(&state->arcs_esize[type], | |
d3c2ae1c | 5591 | size, tag); |
34dc7c2f | 5592 | } |
d3c2ae1c | 5593 | |
34dc7c2f BB |
5594 | /* |
5595 | * If we are growing the cache, and we are adding anonymous | |
5596 | * data, and we have outgrown arc_p, update arc_p | |
5597 | */ | |
37fb3e43 PD |
5598 | if (aggsum_compare(&arc_size, arc_c) < 0 && |
5599 | hdr->b_l1hdr.b_state == arc_anon && | |
424fd7c3 TS |
5600 | (zfs_refcount_count(&arc_anon->arcs_size) + |
5601 | zfs_refcount_count(&arc_mru->arcs_size) > arc_p)) | |
34dc7c2f BB |
5602 | arc_p = MIN(arc_c, arc_p + size); |
5603 | } | |
a6255b7f DQ |
5604 | } |
5605 | ||
5606 | static void | |
5607 | arc_free_data_abd(arc_buf_hdr_t *hdr, abd_t *abd, uint64_t size, void *tag) | |
5608 | { | |
5609 | arc_free_data_impl(hdr, size, tag); | |
5610 | abd_free(abd); | |
5611 | } | |
5612 | ||
5613 | static void | |
5614 | arc_free_data_buf(arc_buf_hdr_t *hdr, void *buf, uint64_t size, void *tag) | |
5615 | { | |
5616 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5617 | ||
5618 | arc_free_data_impl(hdr, size, tag); | |
5619 | if (type == ARC_BUFC_METADATA) { | |
5620 | zio_buf_free(buf, size); | |
5621 | } else { | |
5622 | ASSERT(type == ARC_BUFC_DATA); | |
5623 | zio_data_buf_free(buf, size); | |
5624 | } | |
d3c2ae1c GW |
5625 | } |
5626 | ||
5627 | /* | |
5628 | * Free the arc data buffer. | |
5629 | */ | |
5630 | static void | |
a6255b7f | 5631 | arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) |
d3c2ae1c GW |
5632 | { |
5633 | arc_state_t *state = hdr->b_l1hdr.b_state; | |
5634 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5635 | ||
5636 | /* protected by hash lock, if in the hash table */ | |
5637 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 | 5638 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
5639 | ASSERT(state != arc_anon && state != arc_l2c_only); |
5640 | ||
424fd7c3 | 5641 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c GW |
5642 | size, tag); |
5643 | } | |
424fd7c3 | 5644 | (void) zfs_refcount_remove_many(&state->arcs_size, size, tag); |
d3c2ae1c GW |
5645 | |
5646 | VERIFY3U(hdr->b_type, ==, type); | |
5647 | if (type == ARC_BUFC_METADATA) { | |
d3c2ae1c GW |
5648 | arc_space_return(size, ARC_SPACE_META); |
5649 | } else { | |
5650 | ASSERT(type == ARC_BUFC_DATA); | |
d3c2ae1c GW |
5651 | arc_space_return(size, ARC_SPACE_DATA); |
5652 | } | |
34dc7c2f BB |
5653 | } |
5654 | ||
5655 | /* | |
5656 | * This routine is called whenever a buffer is accessed. | |
5657 | * NOTE: the hash lock is dropped in this function. | |
5658 | */ | |
5659 | static void | |
2a432414 | 5660 | arc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) |
34dc7c2f | 5661 | { |
428870ff BB |
5662 | clock_t now; |
5663 | ||
34dc7c2f | 5664 | ASSERT(MUTEX_HELD(hash_lock)); |
b9541d6b | 5665 | ASSERT(HDR_HAS_L1HDR(hdr)); |
34dc7c2f | 5666 | |
b9541d6b | 5667 | if (hdr->b_l1hdr.b_state == arc_anon) { |
34dc7c2f BB |
5668 | /* |
5669 | * This buffer is not in the cache, and does not | |
5670 | * appear in our "ghost" list. Add the new buffer | |
5671 | * to the MRU state. | |
5672 | */ | |
5673 | ||
b9541d6b CW |
5674 | ASSERT0(hdr->b_l1hdr.b_arc_access); |
5675 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); | |
2a432414 GW |
5676 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); |
5677 | arc_change_state(arc_mru, hdr, hash_lock); | |
34dc7c2f | 5678 | |
b9541d6b | 5679 | } else if (hdr->b_l1hdr.b_state == arc_mru) { |
428870ff BB |
5680 | now = ddi_get_lbolt(); |
5681 | ||
34dc7c2f BB |
5682 | /* |
5683 | * If this buffer is here because of a prefetch, then either: | |
5684 | * - clear the flag if this is a "referencing" read | |
5685 | * (any subsequent access will bump this into the MFU state). | |
5686 | * or | |
5687 | * - move the buffer to the head of the list if this is | |
5688 | * another prefetch (to make it less likely to be evicted). | |
5689 | */ | |
d4a72f23 | 5690 | if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { |
424fd7c3 | 5691 | if (zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { |
ca0bf58d PS |
5692 | /* link protected by hash lock */ |
5693 | ASSERT(multilist_link_active( | |
b9541d6b | 5694 | &hdr->b_l1hdr.b_arc_node)); |
34dc7c2f | 5695 | } else { |
d4a72f23 TC |
5696 | arc_hdr_clear_flags(hdr, |
5697 | ARC_FLAG_PREFETCH | | |
5698 | ARC_FLAG_PRESCIENT_PREFETCH); | |
b9541d6b | 5699 | atomic_inc_32(&hdr->b_l1hdr.b_mru_hits); |
34dc7c2f BB |
5700 | ARCSTAT_BUMP(arcstat_mru_hits); |
5701 | } | |
b9541d6b | 5702 | hdr->b_l1hdr.b_arc_access = now; |
34dc7c2f BB |
5703 | return; |
5704 | } | |
5705 | ||
5706 | /* | |
5707 | * This buffer has been "accessed" only once so far, | |
5708 | * but it is still in the cache. Move it to the MFU | |
5709 | * state. | |
5710 | */ | |
b9541d6b CW |
5711 | if (ddi_time_after(now, hdr->b_l1hdr.b_arc_access + |
5712 | ARC_MINTIME)) { | |
34dc7c2f BB |
5713 | /* |
5714 | * More than 125ms have passed since we | |
5715 | * instantiated this buffer. Move it to the | |
5716 | * most frequently used state. | |
5717 | */ | |
b9541d6b | 5718 | hdr->b_l1hdr.b_arc_access = now; |
2a432414 GW |
5719 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
5720 | arc_change_state(arc_mfu, hdr, hash_lock); | |
34dc7c2f | 5721 | } |
b9541d6b | 5722 | atomic_inc_32(&hdr->b_l1hdr.b_mru_hits); |
34dc7c2f | 5723 | ARCSTAT_BUMP(arcstat_mru_hits); |
b9541d6b | 5724 | } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { |
34dc7c2f BB |
5725 | arc_state_t *new_state; |
5726 | /* | |
5727 | * This buffer has been "accessed" recently, but | |
5728 | * was evicted from the cache. Move it to the | |
5729 | * MFU state. | |
5730 | */ | |
5731 | ||
d4a72f23 | 5732 | if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { |
34dc7c2f | 5733 | new_state = arc_mru; |
424fd7c3 | 5734 | if (zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) { |
d4a72f23 TC |
5735 | arc_hdr_clear_flags(hdr, |
5736 | ARC_FLAG_PREFETCH | | |
5737 | ARC_FLAG_PRESCIENT_PREFETCH); | |
5738 | } | |
2a432414 | 5739 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); |
34dc7c2f BB |
5740 | } else { |
5741 | new_state = arc_mfu; | |
2a432414 | 5742 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
34dc7c2f BB |
5743 | } |
5744 | ||
b9541d6b | 5745 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 | 5746 | arc_change_state(new_state, hdr, hash_lock); |
34dc7c2f | 5747 | |
b9541d6b | 5748 | atomic_inc_32(&hdr->b_l1hdr.b_mru_ghost_hits); |
34dc7c2f | 5749 | ARCSTAT_BUMP(arcstat_mru_ghost_hits); |
b9541d6b | 5750 | } else if (hdr->b_l1hdr.b_state == arc_mfu) { |
34dc7c2f BB |
5751 | /* |
5752 | * This buffer has been accessed more than once and is | |
5753 | * still in the cache. Keep it in the MFU state. | |
5754 | * | |
5755 | * NOTE: an add_reference() that occurred when we did | |
5756 | * the arc_read() will have kicked this off the list. | |
5757 | * If it was a prefetch, we will explicitly move it to | |
5758 | * the head of the list now. | |
5759 | */ | |
d4a72f23 | 5760 | |
b9541d6b | 5761 | atomic_inc_32(&hdr->b_l1hdr.b_mfu_hits); |
34dc7c2f | 5762 | ARCSTAT_BUMP(arcstat_mfu_hits); |
b9541d6b CW |
5763 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
5764 | } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { | |
34dc7c2f BB |
5765 | arc_state_t *new_state = arc_mfu; |
5766 | /* | |
5767 | * This buffer has been accessed more than once but has | |
5768 | * been evicted from the cache. Move it back to the | |
5769 | * MFU state. | |
5770 | */ | |
5771 | ||
d4a72f23 | 5772 | if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { |
34dc7c2f BB |
5773 | /* |
5774 | * This is a prefetch access... | |
5775 | * move this block back to the MRU state. | |
5776 | */ | |
34dc7c2f BB |
5777 | new_state = arc_mru; |
5778 | } | |
5779 | ||
b9541d6b | 5780 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 GW |
5781 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
5782 | arc_change_state(new_state, hdr, hash_lock); | |
34dc7c2f | 5783 | |
b9541d6b | 5784 | atomic_inc_32(&hdr->b_l1hdr.b_mfu_ghost_hits); |
34dc7c2f | 5785 | ARCSTAT_BUMP(arcstat_mfu_ghost_hits); |
b9541d6b | 5786 | } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { |
34dc7c2f BB |
5787 | /* |
5788 | * This buffer is on the 2nd Level ARC. | |
5789 | */ | |
5790 | ||
b9541d6b | 5791 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 GW |
5792 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
5793 | arc_change_state(arc_mfu, hdr, hash_lock); | |
34dc7c2f | 5794 | } else { |
b9541d6b CW |
5795 | cmn_err(CE_PANIC, "invalid arc state 0x%p", |
5796 | hdr->b_l1hdr.b_state); | |
34dc7c2f BB |
5797 | } |
5798 | } | |
5799 | ||
0873bb63 BB |
5800 | /* |
5801 | * This routine is called by dbuf_hold() to update the arc_access() state | |
5802 | * which otherwise would be skipped for entries in the dbuf cache. | |
5803 | */ | |
5804 | void | |
5805 | arc_buf_access(arc_buf_t *buf) | |
5806 | { | |
5807 | mutex_enter(&buf->b_evict_lock); | |
5808 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
5809 | ||
5810 | /* | |
5811 | * Avoid taking the hash_lock when possible as an optimization. | |
5812 | * The header must be checked again under the hash_lock in order | |
5813 | * to handle the case where it is concurrently being released. | |
5814 | */ | |
5815 | if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) { | |
5816 | mutex_exit(&buf->b_evict_lock); | |
5817 | return; | |
5818 | } | |
5819 | ||
5820 | kmutex_t *hash_lock = HDR_LOCK(hdr); | |
5821 | mutex_enter(hash_lock); | |
5822 | ||
5823 | if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) { | |
5824 | mutex_exit(hash_lock); | |
5825 | mutex_exit(&buf->b_evict_lock); | |
5826 | ARCSTAT_BUMP(arcstat_access_skip); | |
5827 | return; | |
5828 | } | |
5829 | ||
5830 | mutex_exit(&buf->b_evict_lock); | |
5831 | ||
5832 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || | |
5833 | hdr->b_l1hdr.b_state == arc_mfu); | |
5834 | ||
5835 | DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); | |
5836 | arc_access(hdr, hash_lock); | |
5837 | mutex_exit(hash_lock); | |
5838 | ||
5839 | ARCSTAT_BUMP(arcstat_hits); | |
5840 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr) && !HDR_PRESCIENT_PREFETCH(hdr), | |
5841 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data, metadata, hits); | |
5842 | } | |
5843 | ||
b5256303 | 5844 | /* a generic arc_read_done_func_t which you can use */ |
34dc7c2f BB |
5845 | /* ARGSUSED */ |
5846 | void | |
d4a72f23 TC |
5847 | arc_bcopy_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, |
5848 | arc_buf_t *buf, void *arg) | |
34dc7c2f | 5849 | { |
d4a72f23 TC |
5850 | if (buf == NULL) |
5851 | return; | |
5852 | ||
5853 | bcopy(buf->b_data, arg, arc_buf_size(buf)); | |
d3c2ae1c | 5854 | arc_buf_destroy(buf, arg); |
34dc7c2f BB |
5855 | } |
5856 | ||
b5256303 | 5857 | /* a generic arc_read_done_func_t */ |
d4a72f23 | 5858 | /* ARGSUSED */ |
34dc7c2f | 5859 | void |
d4a72f23 TC |
5860 | arc_getbuf_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, |
5861 | arc_buf_t *buf, void *arg) | |
34dc7c2f BB |
5862 | { |
5863 | arc_buf_t **bufp = arg; | |
d4a72f23 TC |
5864 | |
5865 | if (buf == NULL) { | |
c3bd3fb4 | 5866 | ASSERT(zio == NULL || zio->io_error != 0); |
34dc7c2f BB |
5867 | *bufp = NULL; |
5868 | } else { | |
c3bd3fb4 | 5869 | ASSERT(zio == NULL || zio->io_error == 0); |
34dc7c2f | 5870 | *bufp = buf; |
c3bd3fb4 | 5871 | ASSERT(buf->b_data != NULL); |
34dc7c2f BB |
5872 | } |
5873 | } | |
5874 | ||
d3c2ae1c GW |
5875 | static void |
5876 | arc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp) | |
5877 | { | |
5878 | if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { | |
5879 | ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0); | |
b5256303 | 5880 | ASSERT3U(arc_hdr_get_compress(hdr), ==, ZIO_COMPRESS_OFF); |
d3c2ae1c GW |
5881 | } else { |
5882 | if (HDR_COMPRESSION_ENABLED(hdr)) { | |
b5256303 | 5883 | ASSERT3U(arc_hdr_get_compress(hdr), ==, |
d3c2ae1c GW |
5884 | BP_GET_COMPRESS(bp)); |
5885 | } | |
5886 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); | |
5887 | ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp)); | |
b5256303 | 5888 | ASSERT3U(!!HDR_PROTECTED(hdr), ==, BP_IS_PROTECTED(bp)); |
d3c2ae1c GW |
5889 | } |
5890 | } | |
5891 | ||
34dc7c2f BB |
5892 | static void |
5893 | arc_read_done(zio_t *zio) | |
5894 | { | |
b5256303 | 5895 | blkptr_t *bp = zio->io_bp; |
d3c2ae1c | 5896 | arc_buf_hdr_t *hdr = zio->io_private; |
9b67f605 | 5897 | kmutex_t *hash_lock = NULL; |
524b4217 DK |
5898 | arc_callback_t *callback_list; |
5899 | arc_callback_t *acb; | |
2aa34383 | 5900 | boolean_t freeable = B_FALSE; |
a7004725 | 5901 | |
34dc7c2f BB |
5902 | /* |
5903 | * The hdr was inserted into hash-table and removed from lists | |
5904 | * prior to starting I/O. We should find this header, since | |
5905 | * it's in the hash table, and it should be legit since it's | |
5906 | * not possible to evict it during the I/O. The only possible | |
5907 | * reason for it not to be found is if we were freed during the | |
5908 | * read. | |
5909 | */ | |
9b67f605 | 5910 | if (HDR_IN_HASH_TABLE(hdr)) { |
31df97cd DB |
5911 | arc_buf_hdr_t *found; |
5912 | ||
9b67f605 MA |
5913 | ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); |
5914 | ASSERT3U(hdr->b_dva.dva_word[0], ==, | |
5915 | BP_IDENTITY(zio->io_bp)->dva_word[0]); | |
5916 | ASSERT3U(hdr->b_dva.dva_word[1], ==, | |
5917 | BP_IDENTITY(zio->io_bp)->dva_word[1]); | |
5918 | ||
31df97cd | 5919 | found = buf_hash_find(hdr->b_spa, zio->io_bp, &hash_lock); |
9b67f605 | 5920 | |
d3c2ae1c | 5921 | ASSERT((found == hdr && |
9b67f605 MA |
5922 | DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || |
5923 | (found == hdr && HDR_L2_READING(hdr))); | |
d3c2ae1c GW |
5924 | ASSERT3P(hash_lock, !=, NULL); |
5925 | } | |
5926 | ||
b5256303 TC |
5927 | if (BP_IS_PROTECTED(bp)) { |
5928 | hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp); | |
5929 | hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset; | |
5930 | zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt, | |
5931 | hdr->b_crypt_hdr.b_iv); | |
5932 | ||
5933 | if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) { | |
5934 | void *tmpbuf; | |
5935 | ||
5936 | tmpbuf = abd_borrow_buf_copy(zio->io_abd, | |
5937 | sizeof (zil_chain_t)); | |
5938 | zio_crypt_decode_mac_zil(tmpbuf, | |
5939 | hdr->b_crypt_hdr.b_mac); | |
5940 | abd_return_buf(zio->io_abd, tmpbuf, | |
5941 | sizeof (zil_chain_t)); | |
5942 | } else { | |
5943 | zio_crypt_decode_mac_bp(bp, hdr->b_crypt_hdr.b_mac); | |
5944 | } | |
5945 | } | |
5946 | ||
d4a72f23 | 5947 | if (zio->io_error == 0) { |
d3c2ae1c GW |
5948 | /* byteswap if necessary */ |
5949 | if (BP_SHOULD_BYTESWAP(zio->io_bp)) { | |
5950 | if (BP_GET_LEVEL(zio->io_bp) > 0) { | |
5951 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; | |
5952 | } else { | |
5953 | hdr->b_l1hdr.b_byteswap = | |
5954 | DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); | |
5955 | } | |
5956 | } else { | |
5957 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
5958 | } | |
9b67f605 | 5959 | } |
34dc7c2f | 5960 | |
d3c2ae1c | 5961 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED); |
b9541d6b | 5962 | if (l2arc_noprefetch && HDR_PREFETCH(hdr)) |
d3c2ae1c | 5963 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE); |
34dc7c2f | 5964 | |
b9541d6b | 5965 | callback_list = hdr->b_l1hdr.b_acb; |
d3c2ae1c | 5966 | ASSERT3P(callback_list, !=, NULL); |
34dc7c2f | 5967 | |
d4a72f23 TC |
5968 | if (hash_lock && zio->io_error == 0 && |
5969 | hdr->b_l1hdr.b_state == arc_anon) { | |
428870ff BB |
5970 | /* |
5971 | * Only call arc_access on anonymous buffers. This is because | |
5972 | * if we've issued an I/O for an evicted buffer, we've already | |
5973 | * called arc_access (to prevent any simultaneous readers from | |
5974 | * getting confused). | |
5975 | */ | |
5976 | arc_access(hdr, hash_lock); | |
5977 | } | |
5978 | ||
524b4217 DK |
5979 | /* |
5980 | * If a read request has a callback (i.e. acb_done is not NULL), then we | |
5981 | * make a buf containing the data according to the parameters which were | |
5982 | * passed in. The implementation of arc_buf_alloc_impl() ensures that we | |
5983 | * aren't needlessly decompressing the data multiple times. | |
5984 | */ | |
a7004725 | 5985 | int callback_cnt = 0; |
2aa34383 DK |
5986 | for (acb = callback_list; acb != NULL; acb = acb->acb_next) { |
5987 | if (!acb->acb_done) | |
5988 | continue; | |
5989 | ||
2aa34383 | 5990 | callback_cnt++; |
524b4217 | 5991 | |
d4a72f23 TC |
5992 | if (zio->io_error != 0) |
5993 | continue; | |
5994 | ||
b5256303 | 5995 | int error = arc_buf_alloc_impl(hdr, zio->io_spa, |
be9a5c35 | 5996 | &acb->acb_zb, acb->acb_private, acb->acb_encrypted, |
d4a72f23 | 5997 | acb->acb_compressed, acb->acb_noauth, B_TRUE, |
440a3eb9 | 5998 | &acb->acb_buf); |
b5256303 TC |
5999 | |
6000 | /* | |
440a3eb9 | 6001 | * Assert non-speculative zios didn't fail because an |
b5256303 TC |
6002 | * encryption key wasn't loaded |
6003 | */ | |
a2c2ed1b | 6004 | ASSERT((zio->io_flags & ZIO_FLAG_SPECULATIVE) || |
be9a5c35 | 6005 | error != EACCES); |
b5256303 TC |
6006 | |
6007 | /* | |
6008 | * If we failed to decrypt, report an error now (as the zio | |
6009 | * layer would have done if it had done the transforms). | |
6010 | */ | |
6011 | if (error == ECKSUM) { | |
6012 | ASSERT(BP_IS_PROTECTED(bp)); | |
6013 | error = SET_ERROR(EIO); | |
b5256303 | 6014 | if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) { |
be9a5c35 | 6015 | spa_log_error(zio->io_spa, &acb->acb_zb); |
b5256303 | 6016 | zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION, |
be9a5c35 | 6017 | zio->io_spa, NULL, &acb->acb_zb, zio, 0, 0); |
b5256303 TC |
6018 | } |
6019 | } | |
6020 | ||
c3bd3fb4 TC |
6021 | if (error != 0) { |
6022 | /* | |
6023 | * Decompression or decryption failed. Set | |
6024 | * io_error so that when we call acb_done | |
6025 | * (below), we will indicate that the read | |
6026 | * failed. Note that in the unusual case | |
6027 | * where one callback is compressed and another | |
6028 | * uncompressed, we will mark all of them | |
6029 | * as failed, even though the uncompressed | |
6030 | * one can't actually fail. In this case, | |
6031 | * the hdr will not be anonymous, because | |
6032 | * if there are multiple callbacks, it's | |
6033 | * because multiple threads found the same | |
6034 | * arc buf in the hash table. | |
6035 | */ | |
524b4217 | 6036 | zio->io_error = error; |
c3bd3fb4 | 6037 | } |
34dc7c2f | 6038 | } |
c3bd3fb4 TC |
6039 | |
6040 | /* | |
6041 | * If there are multiple callbacks, we must have the hash lock, | |
6042 | * because the only way for multiple threads to find this hdr is | |
6043 | * in the hash table. This ensures that if there are multiple | |
6044 | * callbacks, the hdr is not anonymous. If it were anonymous, | |
6045 | * we couldn't use arc_buf_destroy() in the error case below. | |
6046 | */ | |
6047 | ASSERT(callback_cnt < 2 || hash_lock != NULL); | |
6048 | ||
b9541d6b | 6049 | hdr->b_l1hdr.b_acb = NULL; |
d3c2ae1c | 6050 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
440a3eb9 | 6051 | if (callback_cnt == 0) |
b5256303 | 6052 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); |
34dc7c2f | 6053 | |
424fd7c3 | 6054 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || |
b9541d6b | 6055 | callback_list != NULL); |
34dc7c2f | 6056 | |
d4a72f23 | 6057 | if (zio->io_error == 0) { |
d3c2ae1c GW |
6058 | arc_hdr_verify(hdr, zio->io_bp); |
6059 | } else { | |
6060 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); | |
b9541d6b | 6061 | if (hdr->b_l1hdr.b_state != arc_anon) |
34dc7c2f BB |
6062 | arc_change_state(arc_anon, hdr, hash_lock); |
6063 | if (HDR_IN_HASH_TABLE(hdr)) | |
6064 | buf_hash_remove(hdr); | |
424fd7c3 | 6065 | freeable = zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt); |
34dc7c2f BB |
6066 | } |
6067 | ||
6068 | /* | |
6069 | * Broadcast before we drop the hash_lock to avoid the possibility | |
6070 | * that the hdr (and hence the cv) might be freed before we get to | |
6071 | * the cv_broadcast(). | |
6072 | */ | |
b9541d6b | 6073 | cv_broadcast(&hdr->b_l1hdr.b_cv); |
34dc7c2f | 6074 | |
b9541d6b | 6075 | if (hash_lock != NULL) { |
34dc7c2f BB |
6076 | mutex_exit(hash_lock); |
6077 | } else { | |
6078 | /* | |
6079 | * This block was freed while we waited for the read to | |
6080 | * complete. It has been removed from the hash table and | |
6081 | * moved to the anonymous state (so that it won't show up | |
6082 | * in the cache). | |
6083 | */ | |
b9541d6b | 6084 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
424fd7c3 | 6085 | freeable = zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt); |
34dc7c2f BB |
6086 | } |
6087 | ||
6088 | /* execute each callback and free its structure */ | |
6089 | while ((acb = callback_list) != NULL) { | |
c3bd3fb4 TC |
6090 | if (acb->acb_done != NULL) { |
6091 | if (zio->io_error != 0 && acb->acb_buf != NULL) { | |
6092 | /* | |
6093 | * If arc_buf_alloc_impl() fails during | |
6094 | * decompression, the buf will still be | |
6095 | * allocated, and needs to be freed here. | |
6096 | */ | |
6097 | arc_buf_destroy(acb->acb_buf, | |
6098 | acb->acb_private); | |
6099 | acb->acb_buf = NULL; | |
6100 | } | |
d4a72f23 TC |
6101 | acb->acb_done(zio, &zio->io_bookmark, zio->io_bp, |
6102 | acb->acb_buf, acb->acb_private); | |
b5256303 | 6103 | } |
34dc7c2f BB |
6104 | |
6105 | if (acb->acb_zio_dummy != NULL) { | |
6106 | acb->acb_zio_dummy->io_error = zio->io_error; | |
6107 | zio_nowait(acb->acb_zio_dummy); | |
6108 | } | |
6109 | ||
6110 | callback_list = acb->acb_next; | |
6111 | kmem_free(acb, sizeof (arc_callback_t)); | |
6112 | } | |
6113 | ||
6114 | if (freeable) | |
6115 | arc_hdr_destroy(hdr); | |
6116 | } | |
6117 | ||
6118 | /* | |
5c839890 | 6119 | * "Read" the block at the specified DVA (in bp) via the |
34dc7c2f BB |
6120 | * cache. If the block is found in the cache, invoke the provided |
6121 | * callback immediately and return. Note that the `zio' parameter | |
6122 | * in the callback will be NULL in this case, since no IO was | |
6123 | * required. If the block is not in the cache pass the read request | |
6124 | * on to the spa with a substitute callback function, so that the | |
6125 | * requested block will be added to the cache. | |
6126 | * | |
6127 | * If a read request arrives for a block that has a read in-progress, | |
6128 | * either wait for the in-progress read to complete (and return the | |
6129 | * results); or, if this is a read with a "done" func, add a record | |
6130 | * to the read to invoke the "done" func when the read completes, | |
6131 | * and return; or just return. | |
6132 | * | |
6133 | * arc_read_done() will invoke all the requested "done" functions | |
6134 | * for readers of this block. | |
6135 | */ | |
6136 | int | |
b5256303 TC |
6137 | arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, |
6138 | arc_read_done_func_t *done, void *private, zio_priority_t priority, | |
6139 | int zio_flags, arc_flags_t *arc_flags, const zbookmark_phys_t *zb) | |
34dc7c2f | 6140 | { |
9b67f605 | 6141 | arc_buf_hdr_t *hdr = NULL; |
9b67f605 | 6142 | kmutex_t *hash_lock = NULL; |
34dc7c2f | 6143 | zio_t *rzio; |
3541dc6d | 6144 | uint64_t guid = spa_load_guid(spa); |
b5256303 TC |
6145 | boolean_t compressed_read = (zio_flags & ZIO_FLAG_RAW_COMPRESS) != 0; |
6146 | boolean_t encrypted_read = BP_IS_ENCRYPTED(bp) && | |
6147 | (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0; | |
6148 | boolean_t noauth_read = BP_IS_AUTHENTICATED(bp) && | |
6149 | (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0; | |
0902c457 | 6150 | boolean_t embedded_bp = !!BP_IS_EMBEDDED(bp); |
1421c891 | 6151 | int rc = 0; |
34dc7c2f | 6152 | |
0902c457 | 6153 | ASSERT(!embedded_bp || |
9b67f605 MA |
6154 | BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); |
6155 | ||
34dc7c2f | 6156 | top: |
0902c457 | 6157 | if (!embedded_bp) { |
9b67f605 MA |
6158 | /* |
6159 | * Embedded BP's have no DVA and require no I/O to "read". | |
6160 | * Create an anonymous arc buf to back it. | |
6161 | */ | |
6162 | hdr = buf_hash_find(guid, bp, &hash_lock); | |
6163 | } | |
6164 | ||
b5256303 TC |
6165 | /* |
6166 | * Determine if we have an L1 cache hit or a cache miss. For simplicity | |
6167 | * we maintain encrypted data seperately from compressed / uncompressed | |
6168 | * data. If the user is requesting raw encrypted data and we don't have | |
6169 | * that in the header we will read from disk to guarantee that we can | |
6170 | * get it even if the encryption keys aren't loaded. | |
6171 | */ | |
6172 | if (hdr != NULL && HDR_HAS_L1HDR(hdr) && (HDR_HAS_RABD(hdr) || | |
6173 | (hdr->b_l1hdr.b_pabd != NULL && !encrypted_read))) { | |
d3c2ae1c | 6174 | arc_buf_t *buf = NULL; |
2a432414 | 6175 | *arc_flags |= ARC_FLAG_CACHED; |
34dc7c2f BB |
6176 | |
6177 | if (HDR_IO_IN_PROGRESS(hdr)) { | |
a8b2e306 | 6178 | zio_t *head_zio = hdr->b_l1hdr.b_acb->acb_zio_head; |
34dc7c2f | 6179 | |
a8b2e306 | 6180 | ASSERT3P(head_zio, !=, NULL); |
7f60329a MA |
6181 | if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) && |
6182 | priority == ZIO_PRIORITY_SYNC_READ) { | |
6183 | /* | |
a8b2e306 TC |
6184 | * This is a sync read that needs to wait for |
6185 | * an in-flight async read. Request that the | |
6186 | * zio have its priority upgraded. | |
7f60329a | 6187 | */ |
a8b2e306 TC |
6188 | zio_change_priority(head_zio, priority); |
6189 | DTRACE_PROBE1(arc__async__upgrade__sync, | |
7f60329a | 6190 | arc_buf_hdr_t *, hdr); |
a8b2e306 | 6191 | ARCSTAT_BUMP(arcstat_async_upgrade_sync); |
7f60329a MA |
6192 | } |
6193 | if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { | |
d3c2ae1c GW |
6194 | arc_hdr_clear_flags(hdr, |
6195 | ARC_FLAG_PREDICTIVE_PREFETCH); | |
7f60329a MA |
6196 | } |
6197 | ||
2a432414 | 6198 | if (*arc_flags & ARC_FLAG_WAIT) { |
b9541d6b | 6199 | cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); |
34dc7c2f BB |
6200 | mutex_exit(hash_lock); |
6201 | goto top; | |
6202 | } | |
2a432414 | 6203 | ASSERT(*arc_flags & ARC_FLAG_NOWAIT); |
34dc7c2f BB |
6204 | |
6205 | if (done) { | |
7f60329a | 6206 | arc_callback_t *acb = NULL; |
34dc7c2f BB |
6207 | |
6208 | acb = kmem_zalloc(sizeof (arc_callback_t), | |
79c76d5b | 6209 | KM_SLEEP); |
34dc7c2f BB |
6210 | acb->acb_done = done; |
6211 | acb->acb_private = private; | |
a7004725 | 6212 | acb->acb_compressed = compressed_read; |
440a3eb9 TC |
6213 | acb->acb_encrypted = encrypted_read; |
6214 | acb->acb_noauth = noauth_read; | |
be9a5c35 | 6215 | acb->acb_zb = *zb; |
34dc7c2f BB |
6216 | if (pio != NULL) |
6217 | acb->acb_zio_dummy = zio_null(pio, | |
d164b209 | 6218 | spa, NULL, NULL, NULL, zio_flags); |
34dc7c2f | 6219 | |
d3c2ae1c | 6220 | ASSERT3P(acb->acb_done, !=, NULL); |
a8b2e306 | 6221 | acb->acb_zio_head = head_zio; |
b9541d6b CW |
6222 | acb->acb_next = hdr->b_l1hdr.b_acb; |
6223 | hdr->b_l1hdr.b_acb = acb; | |
34dc7c2f | 6224 | mutex_exit(hash_lock); |
1421c891 | 6225 | goto out; |
34dc7c2f BB |
6226 | } |
6227 | mutex_exit(hash_lock); | |
1421c891 | 6228 | goto out; |
34dc7c2f BB |
6229 | } |
6230 | ||
b9541d6b CW |
6231 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || |
6232 | hdr->b_l1hdr.b_state == arc_mfu); | |
34dc7c2f BB |
6233 | |
6234 | if (done) { | |
7f60329a MA |
6235 | if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { |
6236 | /* | |
6237 | * This is a demand read which does not have to | |
6238 | * wait for i/o because we did a predictive | |
6239 | * prefetch i/o for it, which has completed. | |
6240 | */ | |
6241 | DTRACE_PROBE1( | |
6242 | arc__demand__hit__predictive__prefetch, | |
6243 | arc_buf_hdr_t *, hdr); | |
6244 | ARCSTAT_BUMP( | |
6245 | arcstat_demand_hit_predictive_prefetch); | |
d3c2ae1c GW |
6246 | arc_hdr_clear_flags(hdr, |
6247 | ARC_FLAG_PREDICTIVE_PREFETCH); | |
7f60329a | 6248 | } |
d4a72f23 TC |
6249 | |
6250 | if (hdr->b_flags & ARC_FLAG_PRESCIENT_PREFETCH) { | |
6251 | ARCSTAT_BUMP( | |
6252 | arcstat_demand_hit_prescient_prefetch); | |
6253 | arc_hdr_clear_flags(hdr, | |
6254 | ARC_FLAG_PRESCIENT_PREFETCH); | |
6255 | } | |
6256 | ||
0902c457 | 6257 | ASSERT(!embedded_bp || !BP_IS_HOLE(bp)); |
d3c2ae1c | 6258 | |
524b4217 | 6259 | /* Get a buf with the desired data in it. */ |
be9a5c35 TC |
6260 | rc = arc_buf_alloc_impl(hdr, spa, zb, private, |
6261 | encrypted_read, compressed_read, noauth_read, | |
6262 | B_TRUE, &buf); | |
a2c2ed1b TC |
6263 | if (rc == ECKSUM) { |
6264 | /* | |
6265 | * Convert authentication and decryption errors | |
be9a5c35 TC |
6266 | * to EIO (and generate an ereport if needed) |
6267 | * before leaving the ARC. | |
a2c2ed1b TC |
6268 | */ |
6269 | rc = SET_ERROR(EIO); | |
be9a5c35 TC |
6270 | if ((zio_flags & ZIO_FLAG_SPECULATIVE) == 0) { |
6271 | spa_log_error(spa, zb); | |
6272 | zfs_ereport_post( | |
6273 | FM_EREPORT_ZFS_AUTHENTICATION, | |
6274 | spa, NULL, zb, NULL, 0, 0); | |
6275 | } | |
a2c2ed1b | 6276 | } |
d4a72f23 | 6277 | if (rc != 0) { |
2c24b5b1 TC |
6278 | (void) remove_reference(hdr, hash_lock, |
6279 | private); | |
6280 | arc_buf_destroy_impl(buf); | |
d4a72f23 TC |
6281 | buf = NULL; |
6282 | } | |
6283 | ||
a2c2ed1b TC |
6284 | /* assert any errors weren't due to unloaded keys */ |
6285 | ASSERT((zio_flags & ZIO_FLAG_SPECULATIVE) || | |
be9a5c35 | 6286 | rc != EACCES); |
2a432414 | 6287 | } else if (*arc_flags & ARC_FLAG_PREFETCH && |
424fd7c3 | 6288 | zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { |
d3c2ae1c | 6289 | arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); |
34dc7c2f BB |
6290 | } |
6291 | DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); | |
6292 | arc_access(hdr, hash_lock); | |
d4a72f23 TC |
6293 | if (*arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) |
6294 | arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH); | |
2a432414 | 6295 | if (*arc_flags & ARC_FLAG_L2CACHE) |
d3c2ae1c | 6296 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); |
34dc7c2f BB |
6297 | mutex_exit(hash_lock); |
6298 | ARCSTAT_BUMP(arcstat_hits); | |
b9541d6b CW |
6299 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), |
6300 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), | |
34dc7c2f BB |
6301 | data, metadata, hits); |
6302 | ||
6303 | if (done) | |
d4a72f23 | 6304 | done(NULL, zb, bp, buf, private); |
34dc7c2f | 6305 | } else { |
d3c2ae1c GW |
6306 | uint64_t lsize = BP_GET_LSIZE(bp); |
6307 | uint64_t psize = BP_GET_PSIZE(bp); | |
9b67f605 | 6308 | arc_callback_t *acb; |
b128c09f | 6309 | vdev_t *vd = NULL; |
a117a6d6 | 6310 | uint64_t addr = 0; |
d164b209 | 6311 | boolean_t devw = B_FALSE; |
d3c2ae1c | 6312 | uint64_t size; |
440a3eb9 | 6313 | abd_t *hdr_abd; |
34dc7c2f | 6314 | |
5f6d0b6f BB |
6315 | /* |
6316 | * Gracefully handle a damaged logical block size as a | |
1cdb86cb | 6317 | * checksum error. |
5f6d0b6f | 6318 | */ |
d3c2ae1c | 6319 | if (lsize > spa_maxblocksize(spa)) { |
1cdb86cb | 6320 | rc = SET_ERROR(ECKSUM); |
5f6d0b6f BB |
6321 | goto out; |
6322 | } | |
6323 | ||
34dc7c2f | 6324 | if (hdr == NULL) { |
0902c457 TC |
6325 | /* |
6326 | * This block is not in the cache or it has | |
6327 | * embedded data. | |
6328 | */ | |
9b67f605 | 6329 | arc_buf_hdr_t *exists = NULL; |
34dc7c2f | 6330 | arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); |
d3c2ae1c | 6331 | hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, |
b5256303 TC |
6332 | BP_IS_PROTECTED(bp), BP_GET_COMPRESS(bp), type, |
6333 | encrypted_read); | |
d3c2ae1c | 6334 | |
0902c457 | 6335 | if (!embedded_bp) { |
9b67f605 MA |
6336 | hdr->b_dva = *BP_IDENTITY(bp); |
6337 | hdr->b_birth = BP_PHYSICAL_BIRTH(bp); | |
9b67f605 MA |
6338 | exists = buf_hash_insert(hdr, &hash_lock); |
6339 | } | |
6340 | if (exists != NULL) { | |
34dc7c2f BB |
6341 | /* somebody beat us to the hash insert */ |
6342 | mutex_exit(hash_lock); | |
428870ff | 6343 | buf_discard_identity(hdr); |
d3c2ae1c | 6344 | arc_hdr_destroy(hdr); |
34dc7c2f BB |
6345 | goto top; /* restart the IO request */ |
6346 | } | |
34dc7c2f | 6347 | } else { |
b9541d6b | 6348 | /* |
b5256303 TC |
6349 | * This block is in the ghost cache or encrypted data |
6350 | * was requested and we didn't have it. If it was | |
6351 | * L2-only (and thus didn't have an L1 hdr), | |
6352 | * we realloc the header to add an L1 hdr. | |
b9541d6b CW |
6353 | */ |
6354 | if (!HDR_HAS_L1HDR(hdr)) { | |
6355 | hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, | |
6356 | hdr_full_cache); | |
6357 | } | |
6358 | ||
b5256303 TC |
6359 | if (GHOST_STATE(hdr->b_l1hdr.b_state)) { |
6360 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); | |
6361 | ASSERT(!HDR_HAS_RABD(hdr)); | |
6362 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
424fd7c3 TS |
6363 | ASSERT0(zfs_refcount_count( |
6364 | &hdr->b_l1hdr.b_refcnt)); | |
b5256303 TC |
6365 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
6366 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
6367 | } else if (HDR_IO_IN_PROGRESS(hdr)) { | |
6368 | /* | |
6369 | * If this header already had an IO in progress | |
6370 | * and we are performing another IO to fetch | |
6371 | * encrypted data we must wait until the first | |
6372 | * IO completes so as not to confuse | |
6373 | * arc_read_done(). This should be very rare | |
6374 | * and so the performance impact shouldn't | |
6375 | * matter. | |
6376 | */ | |
6377 | cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); | |
6378 | mutex_exit(hash_lock); | |
6379 | goto top; | |
6380 | } | |
34dc7c2f | 6381 | |
7f60329a | 6382 | /* |
d3c2ae1c | 6383 | * This is a delicate dance that we play here. |
b5256303 TC |
6384 | * This hdr might be in the ghost list so we access |
6385 | * it to move it out of the ghost list before we | |
d3c2ae1c GW |
6386 | * initiate the read. If it's a prefetch then |
6387 | * it won't have a callback so we'll remove the | |
6388 | * reference that arc_buf_alloc_impl() created. We | |
6389 | * do this after we've called arc_access() to | |
6390 | * avoid hitting an assert in remove_reference(). | |
7f60329a | 6391 | */ |
428870ff | 6392 | arc_access(hdr, hash_lock); |
b5256303 | 6393 | arc_hdr_alloc_abd(hdr, encrypted_read); |
d3c2ae1c | 6394 | } |
d3c2ae1c | 6395 | |
b5256303 TC |
6396 | if (encrypted_read) { |
6397 | ASSERT(HDR_HAS_RABD(hdr)); | |
6398 | size = HDR_GET_PSIZE(hdr); | |
6399 | hdr_abd = hdr->b_crypt_hdr.b_rabd; | |
d3c2ae1c | 6400 | zio_flags |= ZIO_FLAG_RAW; |
b5256303 TC |
6401 | } else { |
6402 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
6403 | size = arc_hdr_size(hdr); | |
6404 | hdr_abd = hdr->b_l1hdr.b_pabd; | |
6405 | ||
6406 | if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) { | |
6407 | zio_flags |= ZIO_FLAG_RAW_COMPRESS; | |
6408 | } | |
6409 | ||
6410 | /* | |
6411 | * For authenticated bp's, we do not ask the ZIO layer | |
6412 | * to authenticate them since this will cause the entire | |
6413 | * IO to fail if the key isn't loaded. Instead, we | |
6414 | * defer authentication until arc_buf_fill(), which will | |
6415 | * verify the data when the key is available. | |
6416 | */ | |
6417 | if (BP_IS_AUTHENTICATED(bp)) | |
6418 | zio_flags |= ZIO_FLAG_RAW_ENCRYPT; | |
34dc7c2f BB |
6419 | } |
6420 | ||
b5256303 | 6421 | if (*arc_flags & ARC_FLAG_PREFETCH && |
424fd7c3 | 6422 | zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) |
d3c2ae1c | 6423 | arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); |
d4a72f23 TC |
6424 | if (*arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) |
6425 | arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH); | |
d3c2ae1c GW |
6426 | if (*arc_flags & ARC_FLAG_L2CACHE) |
6427 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); | |
b5256303 TC |
6428 | if (BP_IS_AUTHENTICATED(bp)) |
6429 | arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH); | |
d3c2ae1c GW |
6430 | if (BP_GET_LEVEL(bp) > 0) |
6431 | arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT); | |
7f60329a | 6432 | if (*arc_flags & ARC_FLAG_PREDICTIVE_PREFETCH) |
d3c2ae1c | 6433 | arc_hdr_set_flags(hdr, ARC_FLAG_PREDICTIVE_PREFETCH); |
b9541d6b | 6434 | ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); |
428870ff | 6435 | |
79c76d5b | 6436 | acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); |
34dc7c2f BB |
6437 | acb->acb_done = done; |
6438 | acb->acb_private = private; | |
2aa34383 | 6439 | acb->acb_compressed = compressed_read; |
b5256303 TC |
6440 | acb->acb_encrypted = encrypted_read; |
6441 | acb->acb_noauth = noauth_read; | |
be9a5c35 | 6442 | acb->acb_zb = *zb; |
34dc7c2f | 6443 | |
d3c2ae1c | 6444 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
b9541d6b | 6445 | hdr->b_l1hdr.b_acb = acb; |
d3c2ae1c | 6446 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
34dc7c2f | 6447 | |
b9541d6b CW |
6448 | if (HDR_HAS_L2HDR(hdr) && |
6449 | (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { | |
6450 | devw = hdr->b_l2hdr.b_dev->l2ad_writing; | |
6451 | addr = hdr->b_l2hdr.b_daddr; | |
b128c09f | 6452 | /* |
a1d477c2 | 6453 | * Lock out L2ARC device removal. |
b128c09f BB |
6454 | */ |
6455 | if (vdev_is_dead(vd) || | |
6456 | !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) | |
6457 | vd = NULL; | |
6458 | } | |
6459 | ||
a8b2e306 TC |
6460 | /* |
6461 | * We count both async reads and scrub IOs as asynchronous so | |
6462 | * that both can be upgraded in the event of a cache hit while | |
6463 | * the read IO is still in-flight. | |
6464 | */ | |
6465 | if (priority == ZIO_PRIORITY_ASYNC_READ || | |
6466 | priority == ZIO_PRIORITY_SCRUB) | |
d3c2ae1c GW |
6467 | arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); |
6468 | else | |
6469 | arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); | |
6470 | ||
e49f1e20 | 6471 | /* |
0902c457 TC |
6472 | * At this point, we have a level 1 cache miss or a blkptr |
6473 | * with embedded data. Try again in L2ARC if possible. | |
e49f1e20 | 6474 | */ |
d3c2ae1c GW |
6475 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize); |
6476 | ||
0902c457 TC |
6477 | /* |
6478 | * Skip ARC stat bump for block pointers with embedded | |
6479 | * data. The data are read from the blkptr itself via | |
6480 | * decode_embedded_bp_compressed(). | |
6481 | */ | |
6482 | if (!embedded_bp) { | |
6483 | DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, | |
6484 | blkptr_t *, bp, uint64_t, lsize, | |
6485 | zbookmark_phys_t *, zb); | |
6486 | ARCSTAT_BUMP(arcstat_misses); | |
6487 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), | |
6488 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data, | |
6489 | metadata, misses); | |
6490 | } | |
34dc7c2f | 6491 | |
d164b209 | 6492 | if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { |
34dc7c2f BB |
6493 | /* |
6494 | * Read from the L2ARC if the following are true: | |
b128c09f BB |
6495 | * 1. The L2ARC vdev was previously cached. |
6496 | * 2. This buffer still has L2ARC metadata. | |
6497 | * 3. This buffer isn't currently writing to the L2ARC. | |
6498 | * 4. The L2ARC entry wasn't evicted, which may | |
6499 | * also have invalidated the vdev. | |
d164b209 | 6500 | * 5. This isn't prefetch and l2arc_noprefetch is set. |
34dc7c2f | 6501 | */ |
b9541d6b | 6502 | if (HDR_HAS_L2HDR(hdr) && |
d164b209 BB |
6503 | !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && |
6504 | !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { | |
34dc7c2f | 6505 | l2arc_read_callback_t *cb; |
82710e99 GDN |
6506 | abd_t *abd; |
6507 | uint64_t asize; | |
34dc7c2f BB |
6508 | |
6509 | DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); | |
6510 | ARCSTAT_BUMP(arcstat_l2_hits); | |
b9541d6b | 6511 | atomic_inc_32(&hdr->b_l2hdr.b_hits); |
34dc7c2f | 6512 | |
34dc7c2f | 6513 | cb = kmem_zalloc(sizeof (l2arc_read_callback_t), |
79c76d5b | 6514 | KM_SLEEP); |
d3c2ae1c | 6515 | cb->l2rcb_hdr = hdr; |
34dc7c2f BB |
6516 | cb->l2rcb_bp = *bp; |
6517 | cb->l2rcb_zb = *zb; | |
b128c09f | 6518 | cb->l2rcb_flags = zio_flags; |
34dc7c2f | 6519 | |
82710e99 GDN |
6520 | asize = vdev_psize_to_asize(vd, size); |
6521 | if (asize != size) { | |
6522 | abd = abd_alloc_for_io(asize, | |
6523 | HDR_ISTYPE_METADATA(hdr)); | |
6524 | cb->l2rcb_abd = abd; | |
6525 | } else { | |
b5256303 | 6526 | abd = hdr_abd; |
82710e99 GDN |
6527 | } |
6528 | ||
a117a6d6 | 6529 | ASSERT(addr >= VDEV_LABEL_START_SIZE && |
82710e99 | 6530 | addr + asize <= vd->vdev_psize - |
a117a6d6 GW |
6531 | VDEV_LABEL_END_SIZE); |
6532 | ||
34dc7c2f | 6533 | /* |
b128c09f BB |
6534 | * l2arc read. The SCL_L2ARC lock will be |
6535 | * released by l2arc_read_done(). | |
3a17a7a9 SK |
6536 | * Issue a null zio if the underlying buffer |
6537 | * was squashed to zero size by compression. | |
34dc7c2f | 6538 | */ |
b5256303 | 6539 | ASSERT3U(arc_hdr_get_compress(hdr), !=, |
d3c2ae1c GW |
6540 | ZIO_COMPRESS_EMPTY); |
6541 | rzio = zio_read_phys(pio, vd, addr, | |
82710e99 | 6542 | asize, abd, |
d3c2ae1c GW |
6543 | ZIO_CHECKSUM_OFF, |
6544 | l2arc_read_done, cb, priority, | |
6545 | zio_flags | ZIO_FLAG_DONT_CACHE | | |
6546 | ZIO_FLAG_CANFAIL | | |
6547 | ZIO_FLAG_DONT_PROPAGATE | | |
6548 | ZIO_FLAG_DONT_RETRY, B_FALSE); | |
a8b2e306 TC |
6549 | acb->acb_zio_head = rzio; |
6550 | ||
6551 | if (hash_lock != NULL) | |
6552 | mutex_exit(hash_lock); | |
d3c2ae1c | 6553 | |
34dc7c2f BB |
6554 | DTRACE_PROBE2(l2arc__read, vdev_t *, vd, |
6555 | zio_t *, rzio); | |
b5256303 TC |
6556 | ARCSTAT_INCR(arcstat_l2_read_bytes, |
6557 | HDR_GET_PSIZE(hdr)); | |
34dc7c2f | 6558 | |
2a432414 | 6559 | if (*arc_flags & ARC_FLAG_NOWAIT) { |
b128c09f | 6560 | zio_nowait(rzio); |
1421c891 | 6561 | goto out; |
b128c09f | 6562 | } |
34dc7c2f | 6563 | |
2a432414 | 6564 | ASSERT(*arc_flags & ARC_FLAG_WAIT); |
b128c09f | 6565 | if (zio_wait(rzio) == 0) |
1421c891 | 6566 | goto out; |
b128c09f BB |
6567 | |
6568 | /* l2arc read error; goto zio_read() */ | |
a8b2e306 TC |
6569 | if (hash_lock != NULL) |
6570 | mutex_enter(hash_lock); | |
34dc7c2f BB |
6571 | } else { |
6572 | DTRACE_PROBE1(l2arc__miss, | |
6573 | arc_buf_hdr_t *, hdr); | |
6574 | ARCSTAT_BUMP(arcstat_l2_misses); | |
6575 | if (HDR_L2_WRITING(hdr)) | |
6576 | ARCSTAT_BUMP(arcstat_l2_rw_clash); | |
b128c09f | 6577 | spa_config_exit(spa, SCL_L2ARC, vd); |
34dc7c2f | 6578 | } |
d164b209 BB |
6579 | } else { |
6580 | if (vd != NULL) | |
6581 | spa_config_exit(spa, SCL_L2ARC, vd); | |
0902c457 TC |
6582 | /* |
6583 | * Skip ARC stat bump for block pointers with | |
6584 | * embedded data. The data are read from the blkptr | |
6585 | * itself via decode_embedded_bp_compressed(). | |
6586 | */ | |
6587 | if (l2arc_ndev != 0 && !embedded_bp) { | |
d164b209 BB |
6588 | DTRACE_PROBE1(l2arc__miss, |
6589 | arc_buf_hdr_t *, hdr); | |
6590 | ARCSTAT_BUMP(arcstat_l2_misses); | |
6591 | } | |
34dc7c2f | 6592 | } |
34dc7c2f | 6593 | |
b5256303 | 6594 | rzio = zio_read(pio, spa, bp, hdr_abd, size, |
d3c2ae1c | 6595 | arc_read_done, hdr, priority, zio_flags, zb); |
a8b2e306 TC |
6596 | acb->acb_zio_head = rzio; |
6597 | ||
6598 | if (hash_lock != NULL) | |
6599 | mutex_exit(hash_lock); | |
34dc7c2f | 6600 | |
2a432414 | 6601 | if (*arc_flags & ARC_FLAG_WAIT) { |
1421c891 PS |
6602 | rc = zio_wait(rzio); |
6603 | goto out; | |
6604 | } | |
34dc7c2f | 6605 | |
2a432414 | 6606 | ASSERT(*arc_flags & ARC_FLAG_NOWAIT); |
34dc7c2f BB |
6607 | zio_nowait(rzio); |
6608 | } | |
1421c891 PS |
6609 | |
6610 | out: | |
157ef7f6 | 6611 | /* embedded bps don't actually go to disk */ |
0902c457 | 6612 | if (!embedded_bp) |
157ef7f6 | 6613 | spa_read_history_add(spa, zb, *arc_flags); |
1421c891 | 6614 | return (rc); |
34dc7c2f BB |
6615 | } |
6616 | ||
ab26409d BB |
6617 | arc_prune_t * |
6618 | arc_add_prune_callback(arc_prune_func_t *func, void *private) | |
6619 | { | |
6620 | arc_prune_t *p; | |
6621 | ||
d1d7e268 | 6622 | p = kmem_alloc(sizeof (*p), KM_SLEEP); |
ab26409d BB |
6623 | p->p_pfunc = func; |
6624 | p->p_private = private; | |
6625 | list_link_init(&p->p_node); | |
424fd7c3 | 6626 | zfs_refcount_create(&p->p_refcnt); |
ab26409d BB |
6627 | |
6628 | mutex_enter(&arc_prune_mtx); | |
c13060e4 | 6629 | zfs_refcount_add(&p->p_refcnt, &arc_prune_list); |
ab26409d BB |
6630 | list_insert_head(&arc_prune_list, p); |
6631 | mutex_exit(&arc_prune_mtx); | |
6632 | ||
6633 | return (p); | |
6634 | } | |
6635 | ||
6636 | void | |
6637 | arc_remove_prune_callback(arc_prune_t *p) | |
6638 | { | |
4442f60d | 6639 | boolean_t wait = B_FALSE; |
ab26409d BB |
6640 | mutex_enter(&arc_prune_mtx); |
6641 | list_remove(&arc_prune_list, p); | |
424fd7c3 | 6642 | if (zfs_refcount_remove(&p->p_refcnt, &arc_prune_list) > 0) |
4442f60d | 6643 | wait = B_TRUE; |
ab26409d | 6644 | mutex_exit(&arc_prune_mtx); |
4442f60d CC |
6645 | |
6646 | /* wait for arc_prune_task to finish */ | |
6647 | if (wait) | |
6648 | taskq_wait_outstanding(arc_prune_taskq, 0); | |
424fd7c3 TS |
6649 | ASSERT0(zfs_refcount_count(&p->p_refcnt)); |
6650 | zfs_refcount_destroy(&p->p_refcnt); | |
4442f60d | 6651 | kmem_free(p, sizeof (*p)); |
ab26409d BB |
6652 | } |
6653 | ||
df4474f9 MA |
6654 | /* |
6655 | * Notify the arc that a block was freed, and thus will never be used again. | |
6656 | */ | |
6657 | void | |
6658 | arc_freed(spa_t *spa, const blkptr_t *bp) | |
6659 | { | |
6660 | arc_buf_hdr_t *hdr; | |
6661 | kmutex_t *hash_lock; | |
6662 | uint64_t guid = spa_load_guid(spa); | |
6663 | ||
9b67f605 MA |
6664 | ASSERT(!BP_IS_EMBEDDED(bp)); |
6665 | ||
6666 | hdr = buf_hash_find(guid, bp, &hash_lock); | |
df4474f9 MA |
6667 | if (hdr == NULL) |
6668 | return; | |
df4474f9 | 6669 | |
d3c2ae1c GW |
6670 | /* |
6671 | * We might be trying to free a block that is still doing I/O | |
6672 | * (i.e. prefetch) or has a reference (i.e. a dedup-ed, | |
6673 | * dmu_sync-ed block). If this block is being prefetched, then it | |
6674 | * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr | |
6675 | * until the I/O completes. A block may also have a reference if it is | |
6676 | * part of a dedup-ed, dmu_synced write. The dmu_sync() function would | |
6677 | * have written the new block to its final resting place on disk but | |
6678 | * without the dedup flag set. This would have left the hdr in the MRU | |
6679 | * state and discoverable. When the txg finally syncs it detects that | |
6680 | * the block was overridden in open context and issues an override I/O. | |
6681 | * Since this is a dedup block, the override I/O will determine if the | |
6682 | * block is already in the DDT. If so, then it will replace the io_bp | |
6683 | * with the bp from the DDT and allow the I/O to finish. When the I/O | |
6684 | * reaches the done callback, dbuf_write_override_done, it will | |
6685 | * check to see if the io_bp and io_bp_override are identical. | |
6686 | * If they are not, then it indicates that the bp was replaced with | |
6687 | * the bp in the DDT and the override bp is freed. This allows | |
6688 | * us to arrive here with a reference on a block that is being | |
6689 | * freed. So if we have an I/O in progress, or a reference to | |
6690 | * this hdr, then we don't destroy the hdr. | |
6691 | */ | |
6692 | if (!HDR_HAS_L1HDR(hdr) || (!HDR_IO_IN_PROGRESS(hdr) && | |
424fd7c3 | 6693 | zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt))) { |
d3c2ae1c GW |
6694 | arc_change_state(arc_anon, hdr, hash_lock); |
6695 | arc_hdr_destroy(hdr); | |
df4474f9 | 6696 | mutex_exit(hash_lock); |
bd089c54 | 6697 | } else { |
d3c2ae1c | 6698 | mutex_exit(hash_lock); |
34dc7c2f | 6699 | } |
34dc7c2f | 6700 | |
34dc7c2f BB |
6701 | } |
6702 | ||
6703 | /* | |
e49f1e20 WA |
6704 | * Release this buffer from the cache, making it an anonymous buffer. This |
6705 | * must be done after a read and prior to modifying the buffer contents. | |
34dc7c2f | 6706 | * If the buffer has more than one reference, we must make |
b128c09f | 6707 | * a new hdr for the buffer. |
34dc7c2f BB |
6708 | */ |
6709 | void | |
6710 | arc_release(arc_buf_t *buf, void *tag) | |
6711 | { | |
b9541d6b | 6712 | arc_buf_hdr_t *hdr = buf->b_hdr; |
34dc7c2f | 6713 | |
428870ff | 6714 | /* |
ca0bf58d | 6715 | * It would be nice to assert that if its DMU metadata (level > |
428870ff BB |
6716 | * 0 || it's the dnode file), then it must be syncing context. |
6717 | * But we don't know that information at this level. | |
6718 | */ | |
6719 | ||
6720 | mutex_enter(&buf->b_evict_lock); | |
b128c09f | 6721 | |
ca0bf58d PS |
6722 | ASSERT(HDR_HAS_L1HDR(hdr)); |
6723 | ||
b9541d6b CW |
6724 | /* |
6725 | * We don't grab the hash lock prior to this check, because if | |
6726 | * the buffer's header is in the arc_anon state, it won't be | |
6727 | * linked into the hash table. | |
6728 | */ | |
6729 | if (hdr->b_l1hdr.b_state == arc_anon) { | |
6730 | mutex_exit(&buf->b_evict_lock); | |
6731 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
6732 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); | |
6733 | ASSERT(!HDR_HAS_L2HDR(hdr)); | |
d3c2ae1c | 6734 | ASSERT(HDR_EMPTY(hdr)); |
34dc7c2f | 6735 | |
d3c2ae1c | 6736 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); |
424fd7c3 | 6737 | ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); |
b9541d6b CW |
6738 | ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); |
6739 | ||
b9541d6b | 6740 | hdr->b_l1hdr.b_arc_access = 0; |
d3c2ae1c GW |
6741 | |
6742 | /* | |
6743 | * If the buf is being overridden then it may already | |
6744 | * have a hdr that is not empty. | |
6745 | */ | |
6746 | buf_discard_identity(hdr); | |
b9541d6b CW |
6747 | arc_buf_thaw(buf); |
6748 | ||
6749 | return; | |
34dc7c2f BB |
6750 | } |
6751 | ||
1c27024e | 6752 | kmutex_t *hash_lock = HDR_LOCK(hdr); |
b9541d6b CW |
6753 | mutex_enter(hash_lock); |
6754 | ||
6755 | /* | |
6756 | * This assignment is only valid as long as the hash_lock is | |
6757 | * held, we must be careful not to reference state or the | |
6758 | * b_state field after dropping the lock. | |
6759 | */ | |
1c27024e | 6760 | arc_state_t *state = hdr->b_l1hdr.b_state; |
b9541d6b CW |
6761 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
6762 | ASSERT3P(state, !=, arc_anon); | |
6763 | ||
6764 | /* this buffer is not on any list */ | |
424fd7c3 | 6765 | ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), >, 0); |
b9541d6b CW |
6766 | |
6767 | if (HDR_HAS_L2HDR(hdr)) { | |
b9541d6b | 6768 | mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); |
ca0bf58d PS |
6769 | |
6770 | /* | |
d962d5da PS |
6771 | * We have to recheck this conditional again now that |
6772 | * we're holding the l2ad_mtx to prevent a race with | |
6773 | * another thread which might be concurrently calling | |
6774 | * l2arc_evict(). In that case, l2arc_evict() might have | |
6775 | * destroyed the header's L2 portion as we were waiting | |
6776 | * to acquire the l2ad_mtx. | |
ca0bf58d | 6777 | */ |
d962d5da PS |
6778 | if (HDR_HAS_L2HDR(hdr)) |
6779 | arc_hdr_l2hdr_destroy(hdr); | |
ca0bf58d | 6780 | |
b9541d6b | 6781 | mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); |
b128c09f BB |
6782 | } |
6783 | ||
34dc7c2f BB |
6784 | /* |
6785 | * Do we have more than one buf? | |
6786 | */ | |
d3c2ae1c | 6787 | if (hdr->b_l1hdr.b_bufcnt > 1) { |
34dc7c2f | 6788 | arc_buf_hdr_t *nhdr; |
d164b209 | 6789 | uint64_t spa = hdr->b_spa; |
d3c2ae1c GW |
6790 | uint64_t psize = HDR_GET_PSIZE(hdr); |
6791 | uint64_t lsize = HDR_GET_LSIZE(hdr); | |
b5256303 TC |
6792 | boolean_t protected = HDR_PROTECTED(hdr); |
6793 | enum zio_compress compress = arc_hdr_get_compress(hdr); | |
b9541d6b | 6794 | arc_buf_contents_t type = arc_buf_type(hdr); |
d3c2ae1c | 6795 | VERIFY3U(hdr->b_type, ==, type); |
34dc7c2f | 6796 | |
b9541d6b | 6797 | ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); |
d3c2ae1c GW |
6798 | (void) remove_reference(hdr, hash_lock, tag); |
6799 | ||
524b4217 | 6800 | if (arc_buf_is_shared(buf) && !ARC_BUF_COMPRESSED(buf)) { |
d3c2ae1c | 6801 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); |
524b4217 DK |
6802 | ASSERT(ARC_BUF_LAST(buf)); |
6803 | } | |
d3c2ae1c | 6804 | |
34dc7c2f | 6805 | /* |
428870ff | 6806 | * Pull the data off of this hdr and attach it to |
d3c2ae1c GW |
6807 | * a new anonymous hdr. Also find the last buffer |
6808 | * in the hdr's buffer list. | |
34dc7c2f | 6809 | */ |
a7004725 | 6810 | arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); |
d3c2ae1c | 6811 | ASSERT3P(lastbuf, !=, NULL); |
34dc7c2f | 6812 | |
d3c2ae1c GW |
6813 | /* |
6814 | * If the current arc_buf_t and the hdr are sharing their data | |
524b4217 | 6815 | * buffer, then we must stop sharing that block. |
d3c2ae1c GW |
6816 | */ |
6817 | if (arc_buf_is_shared(buf)) { | |
6818 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); | |
d3c2ae1c GW |
6819 | VERIFY(!arc_buf_is_shared(lastbuf)); |
6820 | ||
6821 | /* | |
6822 | * First, sever the block sharing relationship between | |
a7004725 | 6823 | * buf and the arc_buf_hdr_t. |
d3c2ae1c GW |
6824 | */ |
6825 | arc_unshare_buf(hdr, buf); | |
2aa34383 DK |
6826 | |
6827 | /* | |
a6255b7f | 6828 | * Now we need to recreate the hdr's b_pabd. Since we |
524b4217 | 6829 | * have lastbuf handy, we try to share with it, but if |
a6255b7f | 6830 | * we can't then we allocate a new b_pabd and copy the |
524b4217 | 6831 | * data from buf into it. |
2aa34383 | 6832 | */ |
524b4217 DK |
6833 | if (arc_can_share(hdr, lastbuf)) { |
6834 | arc_share_buf(hdr, lastbuf); | |
6835 | } else { | |
b5256303 | 6836 | arc_hdr_alloc_abd(hdr, B_FALSE); |
a6255b7f DQ |
6837 | abd_copy_from_buf(hdr->b_l1hdr.b_pabd, |
6838 | buf->b_data, psize); | |
2aa34383 | 6839 | } |
d3c2ae1c GW |
6840 | VERIFY3P(lastbuf->b_data, !=, NULL); |
6841 | } else if (HDR_SHARED_DATA(hdr)) { | |
2aa34383 DK |
6842 | /* |
6843 | * Uncompressed shared buffers are always at the end | |
6844 | * of the list. Compressed buffers don't have the | |
6845 | * same requirements. This makes it hard to | |
6846 | * simply assert that the lastbuf is shared so | |
6847 | * we rely on the hdr's compression flags to determine | |
6848 | * if we have a compressed, shared buffer. | |
6849 | */ | |
6850 | ASSERT(arc_buf_is_shared(lastbuf) || | |
b5256303 | 6851 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); |
2aa34383 | 6852 | ASSERT(!ARC_BUF_SHARED(buf)); |
d3c2ae1c | 6853 | } |
b5256303 TC |
6854 | |
6855 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); | |
b9541d6b | 6856 | ASSERT3P(state, !=, arc_l2c_only); |
36da08ef | 6857 | |
424fd7c3 | 6858 | (void) zfs_refcount_remove_many(&state->arcs_size, |
2aa34383 | 6859 | arc_buf_size(buf), buf); |
36da08ef | 6860 | |
424fd7c3 | 6861 | if (zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { |
b9541d6b | 6862 | ASSERT3P(state, !=, arc_l2c_only); |
424fd7c3 TS |
6863 | (void) zfs_refcount_remove_many( |
6864 | &state->arcs_esize[type], | |
2aa34383 | 6865 | arc_buf_size(buf), buf); |
34dc7c2f | 6866 | } |
1eb5bfa3 | 6867 | |
d3c2ae1c | 6868 | hdr->b_l1hdr.b_bufcnt -= 1; |
b5256303 TC |
6869 | if (ARC_BUF_ENCRYPTED(buf)) |
6870 | hdr->b_crypt_hdr.b_ebufcnt -= 1; | |
6871 | ||
34dc7c2f | 6872 | arc_cksum_verify(buf); |
498877ba | 6873 | arc_buf_unwatch(buf); |
34dc7c2f | 6874 | |
f486f584 TC |
6875 | /* if this is the last uncompressed buf free the checksum */ |
6876 | if (!arc_hdr_has_uncompressed_buf(hdr)) | |
6877 | arc_cksum_free(hdr); | |
6878 | ||
34dc7c2f BB |
6879 | mutex_exit(hash_lock); |
6880 | ||
d3c2ae1c | 6881 | /* |
a6255b7f | 6882 | * Allocate a new hdr. The new hdr will contain a b_pabd |
d3c2ae1c GW |
6883 | * buffer which will be freed in arc_write(). |
6884 | */ | |
b5256303 TC |
6885 | nhdr = arc_hdr_alloc(spa, psize, lsize, protected, |
6886 | compress, type, HDR_HAS_RABD(hdr)); | |
d3c2ae1c GW |
6887 | ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL); |
6888 | ASSERT0(nhdr->b_l1hdr.b_bufcnt); | |
424fd7c3 | 6889 | ASSERT0(zfs_refcount_count(&nhdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
6890 | VERIFY3U(nhdr->b_type, ==, type); |
6891 | ASSERT(!HDR_SHARED_DATA(nhdr)); | |
b9541d6b | 6892 | |
d3c2ae1c GW |
6893 | nhdr->b_l1hdr.b_buf = buf; |
6894 | nhdr->b_l1hdr.b_bufcnt = 1; | |
b5256303 TC |
6895 | if (ARC_BUF_ENCRYPTED(buf)) |
6896 | nhdr->b_crypt_hdr.b_ebufcnt = 1; | |
b9541d6b CW |
6897 | nhdr->b_l1hdr.b_mru_hits = 0; |
6898 | nhdr->b_l1hdr.b_mru_ghost_hits = 0; | |
6899 | nhdr->b_l1hdr.b_mfu_hits = 0; | |
6900 | nhdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
6901 | nhdr->b_l1hdr.b_l2_hits = 0; | |
c13060e4 | 6902 | (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); |
34dc7c2f | 6903 | buf->b_hdr = nhdr; |
d3c2ae1c | 6904 | |
428870ff | 6905 | mutex_exit(&buf->b_evict_lock); |
424fd7c3 | 6906 | (void) zfs_refcount_add_many(&arc_anon->arcs_size, |
5e8ff256 | 6907 | arc_buf_size(buf), buf); |
34dc7c2f | 6908 | } else { |
428870ff | 6909 | mutex_exit(&buf->b_evict_lock); |
424fd7c3 | 6910 | ASSERT(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); |
ca0bf58d PS |
6911 | /* protected by hash lock, or hdr is on arc_anon */ |
6912 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
34dc7c2f | 6913 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b CW |
6914 | hdr->b_l1hdr.b_mru_hits = 0; |
6915 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
6916 | hdr->b_l1hdr.b_mfu_hits = 0; | |
6917 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
6918 | hdr->b_l1hdr.b_l2_hits = 0; | |
6919 | arc_change_state(arc_anon, hdr, hash_lock); | |
6920 | hdr->b_l1hdr.b_arc_access = 0; | |
34dc7c2f | 6921 | |
b5256303 | 6922 | mutex_exit(hash_lock); |
428870ff | 6923 | buf_discard_identity(hdr); |
34dc7c2f BB |
6924 | arc_buf_thaw(buf); |
6925 | } | |
34dc7c2f BB |
6926 | } |
6927 | ||
6928 | int | |
6929 | arc_released(arc_buf_t *buf) | |
6930 | { | |
b128c09f BB |
6931 | int released; |
6932 | ||
428870ff | 6933 | mutex_enter(&buf->b_evict_lock); |
b9541d6b CW |
6934 | released = (buf->b_data != NULL && |
6935 | buf->b_hdr->b_l1hdr.b_state == arc_anon); | |
428870ff | 6936 | mutex_exit(&buf->b_evict_lock); |
b128c09f | 6937 | return (released); |
34dc7c2f BB |
6938 | } |
6939 | ||
34dc7c2f BB |
6940 | #ifdef ZFS_DEBUG |
6941 | int | |
6942 | arc_referenced(arc_buf_t *buf) | |
6943 | { | |
b128c09f BB |
6944 | int referenced; |
6945 | ||
428870ff | 6946 | mutex_enter(&buf->b_evict_lock); |
424fd7c3 | 6947 | referenced = (zfs_refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); |
428870ff | 6948 | mutex_exit(&buf->b_evict_lock); |
b128c09f | 6949 | return (referenced); |
34dc7c2f BB |
6950 | } |
6951 | #endif | |
6952 | ||
6953 | static void | |
6954 | arc_write_ready(zio_t *zio) | |
6955 | { | |
6956 | arc_write_callback_t *callback = zio->io_private; | |
6957 | arc_buf_t *buf = callback->awcb_buf; | |
6958 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
b5256303 TC |
6959 | blkptr_t *bp = zio->io_bp; |
6960 | uint64_t psize = BP_IS_HOLE(bp) ? 0 : BP_GET_PSIZE(bp); | |
a6255b7f | 6961 | fstrans_cookie_t cookie = spl_fstrans_mark(); |
34dc7c2f | 6962 | |
b9541d6b | 6963 | ASSERT(HDR_HAS_L1HDR(hdr)); |
424fd7c3 | 6964 | ASSERT(!zfs_refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c | 6965 | ASSERT(hdr->b_l1hdr.b_bufcnt > 0); |
b128c09f | 6966 | |
34dc7c2f | 6967 | /* |
d3c2ae1c GW |
6968 | * If we're reexecuting this zio because the pool suspended, then |
6969 | * cleanup any state that was previously set the first time the | |
2aa34383 | 6970 | * callback was invoked. |
34dc7c2f | 6971 | */ |
d3c2ae1c GW |
6972 | if (zio->io_flags & ZIO_FLAG_REEXECUTED) { |
6973 | arc_cksum_free(hdr); | |
6974 | arc_buf_unwatch(buf); | |
a6255b7f | 6975 | if (hdr->b_l1hdr.b_pabd != NULL) { |
d3c2ae1c | 6976 | if (arc_buf_is_shared(buf)) { |
d3c2ae1c GW |
6977 | arc_unshare_buf(hdr, buf); |
6978 | } else { | |
b5256303 | 6979 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c | 6980 | } |
34dc7c2f | 6981 | } |
b5256303 TC |
6982 | |
6983 | if (HDR_HAS_RABD(hdr)) | |
6984 | arc_hdr_free_abd(hdr, B_TRUE); | |
34dc7c2f | 6985 | } |
a6255b7f | 6986 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 6987 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
6988 | ASSERT(!HDR_SHARED_DATA(hdr)); |
6989 | ASSERT(!arc_buf_is_shared(buf)); | |
6990 | ||
6991 | callback->awcb_ready(zio, buf, callback->awcb_private); | |
6992 | ||
6993 | if (HDR_IO_IN_PROGRESS(hdr)) | |
6994 | ASSERT(zio->io_flags & ZIO_FLAG_REEXECUTED); | |
6995 | ||
d3c2ae1c GW |
6996 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
6997 | ||
b5256303 TC |
6998 | if (BP_IS_PROTECTED(bp) != !!HDR_PROTECTED(hdr)) |
6999 | hdr = arc_hdr_realloc_crypt(hdr, BP_IS_PROTECTED(bp)); | |
7000 | ||
7001 | if (BP_IS_PROTECTED(bp)) { | |
7002 | /* ZIL blocks are written through zio_rewrite */ | |
7003 | ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG); | |
7004 | ASSERT(HDR_PROTECTED(hdr)); | |
7005 | ||
ae76f45c TC |
7006 | if (BP_SHOULD_BYTESWAP(bp)) { |
7007 | if (BP_GET_LEVEL(bp) > 0) { | |
7008 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; | |
7009 | } else { | |
7010 | hdr->b_l1hdr.b_byteswap = | |
7011 | DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); | |
7012 | } | |
7013 | } else { | |
7014 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
7015 | } | |
7016 | ||
b5256303 TC |
7017 | hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp); |
7018 | hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset; | |
7019 | zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt, | |
7020 | hdr->b_crypt_hdr.b_iv); | |
7021 | zio_crypt_decode_mac_bp(bp, hdr->b_crypt_hdr.b_mac); | |
7022 | } | |
7023 | ||
7024 | /* | |
7025 | * If this block was written for raw encryption but the zio layer | |
7026 | * ended up only authenticating it, adjust the buffer flags now. | |
7027 | */ | |
7028 | if (BP_IS_AUTHENTICATED(bp) && ARC_BUF_ENCRYPTED(buf)) { | |
7029 | arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH); | |
7030 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
7031 | if (BP_GET_COMPRESS(bp) == ZIO_COMPRESS_OFF) | |
7032 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
b1d21733 TC |
7033 | } else if (BP_IS_HOLE(bp) && ARC_BUF_ENCRYPTED(buf)) { |
7034 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
7035 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
b5256303 TC |
7036 | } |
7037 | ||
7038 | /* this must be done after the buffer flags are adjusted */ | |
7039 | arc_cksum_compute(buf); | |
7040 | ||
1c27024e | 7041 | enum zio_compress compress; |
b5256303 | 7042 | if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { |
d3c2ae1c GW |
7043 | compress = ZIO_COMPRESS_OFF; |
7044 | } else { | |
b5256303 TC |
7045 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); |
7046 | compress = BP_GET_COMPRESS(bp); | |
d3c2ae1c GW |
7047 | } |
7048 | HDR_SET_PSIZE(hdr, psize); | |
7049 | arc_hdr_set_compress(hdr, compress); | |
7050 | ||
4807c0ba TC |
7051 | if (zio->io_error != 0 || psize == 0) |
7052 | goto out; | |
7053 | ||
d3c2ae1c | 7054 | /* |
b5256303 TC |
7055 | * Fill the hdr with data. If the buffer is encrypted we have no choice |
7056 | * but to copy the data into b_radb. If the hdr is compressed, the data | |
7057 | * we want is available from the zio, otherwise we can take it from | |
7058 | * the buf. | |
a6255b7f DQ |
7059 | * |
7060 | * We might be able to share the buf's data with the hdr here. However, | |
7061 | * doing so would cause the ARC to be full of linear ABDs if we write a | |
7062 | * lot of shareable data. As a compromise, we check whether scattered | |
7063 | * ABDs are allowed, and assume that if they are then the user wants | |
7064 | * the ARC to be primarily filled with them regardless of the data being | |
7065 | * written. Therefore, if they're allowed then we allocate one and copy | |
7066 | * the data into it; otherwise, we share the data directly if we can. | |
d3c2ae1c | 7067 | */ |
b5256303 | 7068 | if (ARC_BUF_ENCRYPTED(buf)) { |
4807c0ba | 7069 | ASSERT3U(psize, >, 0); |
b5256303 TC |
7070 | ASSERT(ARC_BUF_COMPRESSED(buf)); |
7071 | arc_hdr_alloc_abd(hdr, B_TRUE); | |
7072 | abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize); | |
7073 | } else if (zfs_abd_scatter_enabled || !arc_can_share(hdr, buf)) { | |
a6255b7f DQ |
7074 | /* |
7075 | * Ideally, we would always copy the io_abd into b_pabd, but the | |
7076 | * user may have disabled compressed ARC, thus we must check the | |
7077 | * hdr's compression setting rather than the io_bp's. | |
7078 | */ | |
b5256303 | 7079 | if (BP_IS_ENCRYPTED(bp)) { |
a6255b7f | 7080 | ASSERT3U(psize, >, 0); |
b5256303 TC |
7081 | arc_hdr_alloc_abd(hdr, B_TRUE); |
7082 | abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize); | |
7083 | } else if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF && | |
7084 | !ARC_BUF_COMPRESSED(buf)) { | |
7085 | ASSERT3U(psize, >, 0); | |
7086 | arc_hdr_alloc_abd(hdr, B_FALSE); | |
a6255b7f DQ |
7087 | abd_copy(hdr->b_l1hdr.b_pabd, zio->io_abd, psize); |
7088 | } else { | |
7089 | ASSERT3U(zio->io_orig_size, ==, arc_hdr_size(hdr)); | |
b5256303 | 7090 | arc_hdr_alloc_abd(hdr, B_FALSE); |
a6255b7f DQ |
7091 | abd_copy_from_buf(hdr->b_l1hdr.b_pabd, buf->b_data, |
7092 | arc_buf_size(buf)); | |
7093 | } | |
d3c2ae1c | 7094 | } else { |
a6255b7f | 7095 | ASSERT3P(buf->b_data, ==, abd_to_buf(zio->io_orig_abd)); |
2aa34383 | 7096 | ASSERT3U(zio->io_orig_size, ==, arc_buf_size(buf)); |
d3c2ae1c | 7097 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); |
d3c2ae1c | 7098 | |
d3c2ae1c | 7099 | arc_share_buf(hdr, buf); |
d3c2ae1c | 7100 | } |
a6255b7f | 7101 | |
4807c0ba | 7102 | out: |
b5256303 | 7103 | arc_hdr_verify(hdr, bp); |
a6255b7f | 7104 | spl_fstrans_unmark(cookie); |
34dc7c2f BB |
7105 | } |
7106 | ||
bc77ba73 PD |
7107 | static void |
7108 | arc_write_children_ready(zio_t *zio) | |
7109 | { | |
7110 | arc_write_callback_t *callback = zio->io_private; | |
7111 | arc_buf_t *buf = callback->awcb_buf; | |
7112 | ||
7113 | callback->awcb_children_ready(zio, buf, callback->awcb_private); | |
7114 | } | |
7115 | ||
e8b96c60 MA |
7116 | /* |
7117 | * The SPA calls this callback for each physical write that happens on behalf | |
7118 | * of a logical write. See the comment in dbuf_write_physdone() for details. | |
7119 | */ | |
7120 | static void | |
7121 | arc_write_physdone(zio_t *zio) | |
7122 | { | |
7123 | arc_write_callback_t *cb = zio->io_private; | |
7124 | if (cb->awcb_physdone != NULL) | |
7125 | cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); | |
7126 | } | |
7127 | ||
34dc7c2f BB |
7128 | static void |
7129 | arc_write_done(zio_t *zio) | |
7130 | { | |
7131 | arc_write_callback_t *callback = zio->io_private; | |
7132 | arc_buf_t *buf = callback->awcb_buf; | |
7133 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
7134 | ||
d3c2ae1c | 7135 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
428870ff BB |
7136 | |
7137 | if (zio->io_error == 0) { | |
d3c2ae1c GW |
7138 | arc_hdr_verify(hdr, zio->io_bp); |
7139 | ||
9b67f605 | 7140 | if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { |
b0bc7a84 MG |
7141 | buf_discard_identity(hdr); |
7142 | } else { | |
7143 | hdr->b_dva = *BP_IDENTITY(zio->io_bp); | |
7144 | hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); | |
b0bc7a84 | 7145 | } |
428870ff | 7146 | } else { |
d3c2ae1c | 7147 | ASSERT(HDR_EMPTY(hdr)); |
428870ff | 7148 | } |
34dc7c2f | 7149 | |
34dc7c2f | 7150 | /* |
9b67f605 MA |
7151 | * If the block to be written was all-zero or compressed enough to be |
7152 | * embedded in the BP, no write was performed so there will be no | |
7153 | * dva/birth/checksum. The buffer must therefore remain anonymous | |
7154 | * (and uncached). | |
34dc7c2f | 7155 | */ |
d3c2ae1c | 7156 | if (!HDR_EMPTY(hdr)) { |
34dc7c2f BB |
7157 | arc_buf_hdr_t *exists; |
7158 | kmutex_t *hash_lock; | |
7159 | ||
524b4217 | 7160 | ASSERT3U(zio->io_error, ==, 0); |
428870ff | 7161 | |
34dc7c2f BB |
7162 | arc_cksum_verify(buf); |
7163 | ||
7164 | exists = buf_hash_insert(hdr, &hash_lock); | |
b9541d6b | 7165 | if (exists != NULL) { |
34dc7c2f BB |
7166 | /* |
7167 | * This can only happen if we overwrite for | |
7168 | * sync-to-convergence, because we remove | |
7169 | * buffers from the hash table when we arc_free(). | |
7170 | */ | |
428870ff BB |
7171 | if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { |
7172 | if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) | |
7173 | panic("bad overwrite, hdr=%p exists=%p", | |
7174 | (void *)hdr, (void *)exists); | |
424fd7c3 | 7175 | ASSERT(zfs_refcount_is_zero( |
b9541d6b | 7176 | &exists->b_l1hdr.b_refcnt)); |
428870ff BB |
7177 | arc_change_state(arc_anon, exists, hash_lock); |
7178 | mutex_exit(hash_lock); | |
7179 | arc_hdr_destroy(exists); | |
7180 | exists = buf_hash_insert(hdr, &hash_lock); | |
7181 | ASSERT3P(exists, ==, NULL); | |
03c6040b GW |
7182 | } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { |
7183 | /* nopwrite */ | |
7184 | ASSERT(zio->io_prop.zp_nopwrite); | |
7185 | if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) | |
7186 | panic("bad nopwrite, hdr=%p exists=%p", | |
7187 | (void *)hdr, (void *)exists); | |
428870ff BB |
7188 | } else { |
7189 | /* Dedup */ | |
d3c2ae1c | 7190 | ASSERT(hdr->b_l1hdr.b_bufcnt == 1); |
b9541d6b | 7191 | ASSERT(hdr->b_l1hdr.b_state == arc_anon); |
428870ff BB |
7192 | ASSERT(BP_GET_DEDUP(zio->io_bp)); |
7193 | ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); | |
7194 | } | |
34dc7c2f | 7195 | } |
d3c2ae1c | 7196 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
b128c09f | 7197 | /* if it's not anon, we are doing a scrub */ |
b9541d6b | 7198 | if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) |
b128c09f | 7199 | arc_access(hdr, hash_lock); |
34dc7c2f | 7200 | mutex_exit(hash_lock); |
34dc7c2f | 7201 | } else { |
d3c2ae1c | 7202 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
34dc7c2f BB |
7203 | } |
7204 | ||
424fd7c3 | 7205 | ASSERT(!zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
428870ff | 7206 | callback->awcb_done(zio, buf, callback->awcb_private); |
34dc7c2f | 7207 | |
a6255b7f | 7208 | abd_put(zio->io_abd); |
34dc7c2f BB |
7209 | kmem_free(callback, sizeof (arc_write_callback_t)); |
7210 | } | |
7211 | ||
7212 | zio_t * | |
428870ff | 7213 | arc_write(zio_t *pio, spa_t *spa, uint64_t txg, |
d3c2ae1c | 7214 | blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, |
b5256303 TC |
7215 | const zio_prop_t *zp, arc_write_done_func_t *ready, |
7216 | arc_write_done_func_t *children_ready, arc_write_done_func_t *physdone, | |
7217 | arc_write_done_func_t *done, void *private, zio_priority_t priority, | |
5dbd68a3 | 7218 | int zio_flags, const zbookmark_phys_t *zb) |
34dc7c2f BB |
7219 | { |
7220 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
7221 | arc_write_callback_t *callback; | |
b128c09f | 7222 | zio_t *zio; |
82644107 | 7223 | zio_prop_t localprop = *zp; |
34dc7c2f | 7224 | |
d3c2ae1c GW |
7225 | ASSERT3P(ready, !=, NULL); |
7226 | ASSERT3P(done, !=, NULL); | |
34dc7c2f | 7227 | ASSERT(!HDR_IO_ERROR(hdr)); |
b9541d6b | 7228 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
d3c2ae1c GW |
7229 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
7230 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); | |
b128c09f | 7231 | if (l2arc) |
d3c2ae1c | 7232 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); |
82644107 | 7233 | |
b5256303 TC |
7234 | if (ARC_BUF_ENCRYPTED(buf)) { |
7235 | ASSERT(ARC_BUF_COMPRESSED(buf)); | |
7236 | localprop.zp_encrypt = B_TRUE; | |
7237 | localprop.zp_compress = HDR_GET_COMPRESS(hdr); | |
7238 | localprop.zp_byteorder = | |
7239 | (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ? | |
7240 | ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER; | |
7241 | bcopy(hdr->b_crypt_hdr.b_salt, localprop.zp_salt, | |
7242 | ZIO_DATA_SALT_LEN); | |
7243 | bcopy(hdr->b_crypt_hdr.b_iv, localprop.zp_iv, | |
7244 | ZIO_DATA_IV_LEN); | |
7245 | bcopy(hdr->b_crypt_hdr.b_mac, localprop.zp_mac, | |
7246 | ZIO_DATA_MAC_LEN); | |
7247 | if (DMU_OT_IS_ENCRYPTED(localprop.zp_type)) { | |
7248 | localprop.zp_nopwrite = B_FALSE; | |
7249 | localprop.zp_copies = | |
7250 | MIN(localprop.zp_copies, SPA_DVAS_PER_BP - 1); | |
7251 | } | |
2aa34383 | 7252 | zio_flags |= ZIO_FLAG_RAW; |
b5256303 TC |
7253 | } else if (ARC_BUF_COMPRESSED(buf)) { |
7254 | ASSERT3U(HDR_GET_LSIZE(hdr), !=, arc_buf_size(buf)); | |
7255 | localprop.zp_compress = HDR_GET_COMPRESS(hdr); | |
7256 | zio_flags |= ZIO_FLAG_RAW_COMPRESS; | |
2aa34383 | 7257 | } |
79c76d5b | 7258 | callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); |
34dc7c2f | 7259 | callback->awcb_ready = ready; |
bc77ba73 | 7260 | callback->awcb_children_ready = children_ready; |
e8b96c60 | 7261 | callback->awcb_physdone = physdone; |
34dc7c2f BB |
7262 | callback->awcb_done = done; |
7263 | callback->awcb_private = private; | |
7264 | callback->awcb_buf = buf; | |
b128c09f | 7265 | |
d3c2ae1c | 7266 | /* |
a6255b7f | 7267 | * The hdr's b_pabd is now stale, free it now. A new data block |
d3c2ae1c GW |
7268 | * will be allocated when the zio pipeline calls arc_write_ready(). |
7269 | */ | |
a6255b7f | 7270 | if (hdr->b_l1hdr.b_pabd != NULL) { |
d3c2ae1c GW |
7271 | /* |
7272 | * If the buf is currently sharing the data block with | |
7273 | * the hdr then we need to break that relationship here. | |
7274 | * The hdr will remain with a NULL data pointer and the | |
7275 | * buf will take sole ownership of the block. | |
7276 | */ | |
7277 | if (arc_buf_is_shared(buf)) { | |
d3c2ae1c GW |
7278 | arc_unshare_buf(hdr, buf); |
7279 | } else { | |
b5256303 | 7280 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c GW |
7281 | } |
7282 | VERIFY3P(buf->b_data, !=, NULL); | |
d3c2ae1c | 7283 | } |
b5256303 TC |
7284 | |
7285 | if (HDR_HAS_RABD(hdr)) | |
7286 | arc_hdr_free_abd(hdr, B_TRUE); | |
7287 | ||
71a24c3c TC |
7288 | if (!(zio_flags & ZIO_FLAG_RAW)) |
7289 | arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF); | |
b5256303 | 7290 | |
d3c2ae1c | 7291 | ASSERT(!arc_buf_is_shared(buf)); |
a6255b7f | 7292 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
d3c2ae1c | 7293 | |
a6255b7f DQ |
7294 | zio = zio_write(pio, spa, txg, bp, |
7295 | abd_get_from_buf(buf->b_data, HDR_GET_LSIZE(hdr)), | |
82644107 | 7296 | HDR_GET_LSIZE(hdr), arc_buf_size(buf), &localprop, arc_write_ready, |
bc77ba73 PD |
7297 | (children_ready != NULL) ? arc_write_children_ready : NULL, |
7298 | arc_write_physdone, arc_write_done, callback, | |
e8b96c60 | 7299 | priority, zio_flags, zb); |
34dc7c2f BB |
7300 | |
7301 | return (zio); | |
7302 | } | |
7303 | ||
34dc7c2f | 7304 | static int |
dae3e9ea | 7305 | arc_memory_throttle(spa_t *spa, uint64_t reserve, uint64_t txg) |
34dc7c2f BB |
7306 | { |
7307 | #ifdef _KERNEL | |
70f02287 | 7308 | uint64_t available_memory = arc_free_memory(); |
0c5493d4 | 7309 | |
70f02287 | 7310 | #if defined(_ILP32) |
9edb3695 BB |
7311 | available_memory = |
7312 | MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); | |
7313 | #endif | |
7314 | ||
7315 | if (available_memory > arc_all_memory() * arc_lotsfree_percent / 100) | |
ca67b33a MA |
7316 | return (0); |
7317 | ||
dae3e9ea DB |
7318 | if (txg > spa->spa_lowmem_last_txg) { |
7319 | spa->spa_lowmem_last_txg = txg; | |
7320 | spa->spa_lowmem_page_load = 0; | |
7e8bddd0 | 7321 | } |
7e8bddd0 BB |
7322 | /* |
7323 | * If we are in pageout, we know that memory is already tight, | |
7324 | * the arc is already going to be evicting, so we just want to | |
7325 | * continue to let page writes occur as quickly as possible. | |
7326 | */ | |
7327 | if (current_is_kswapd()) { | |
dae3e9ea DB |
7328 | if (spa->spa_lowmem_page_load > |
7329 | MAX(arc_sys_free / 4, available_memory) / 4) { | |
7e8bddd0 BB |
7330 | DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim); |
7331 | return (SET_ERROR(ERESTART)); | |
7332 | } | |
7333 | /* Note: reserve is inflated, so we deflate */ | |
dae3e9ea | 7334 | atomic_add_64(&spa->spa_lowmem_page_load, reserve / 8); |
7e8bddd0 | 7335 | return (0); |
dae3e9ea | 7336 | } else if (spa->spa_lowmem_page_load > 0 && arc_reclaim_needed()) { |
ca67b33a | 7337 | /* memory is low, delay before restarting */ |
34dc7c2f | 7338 | ARCSTAT_INCR(arcstat_memory_throttle_count, 1); |
570827e1 | 7339 | DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim); |
2e528b49 | 7340 | return (SET_ERROR(EAGAIN)); |
34dc7c2f | 7341 | } |
dae3e9ea DB |
7342 | spa->spa_lowmem_page_load = 0; |
7343 | #endif /* _KERNEL */ | |
34dc7c2f BB |
7344 | return (0); |
7345 | } | |
7346 | ||
7347 | void | |
7348 | arc_tempreserve_clear(uint64_t reserve) | |
7349 | { | |
7350 | atomic_add_64(&arc_tempreserve, -reserve); | |
7351 | ASSERT((int64_t)arc_tempreserve >= 0); | |
7352 | } | |
7353 | ||
7354 | int | |
dae3e9ea | 7355 | arc_tempreserve_space(spa_t *spa, uint64_t reserve, uint64_t txg) |
34dc7c2f BB |
7356 | { |
7357 | int error; | |
9babb374 | 7358 | uint64_t anon_size; |
34dc7c2f | 7359 | |
1b8951b3 TC |
7360 | if (!arc_no_grow && |
7361 | reserve > arc_c/4 && | |
7362 | reserve * 4 > (2ULL << SPA_MAXBLOCKSHIFT)) | |
34dc7c2f | 7363 | arc_c = MIN(arc_c_max, reserve * 4); |
12f9a6a3 BB |
7364 | |
7365 | /* | |
7366 | * Throttle when the calculated memory footprint for the TXG | |
7367 | * exceeds the target ARC size. | |
7368 | */ | |
570827e1 BB |
7369 | if (reserve > arc_c) { |
7370 | DMU_TX_STAT_BUMP(dmu_tx_memory_reserve); | |
12f9a6a3 | 7371 | return (SET_ERROR(ERESTART)); |
570827e1 | 7372 | } |
34dc7c2f | 7373 | |
9babb374 BB |
7374 | /* |
7375 | * Don't count loaned bufs as in flight dirty data to prevent long | |
7376 | * network delays from blocking transactions that are ready to be | |
7377 | * assigned to a txg. | |
7378 | */ | |
a7004725 DK |
7379 | |
7380 | /* assert that it has not wrapped around */ | |
7381 | ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); | |
7382 | ||
424fd7c3 | 7383 | anon_size = MAX((int64_t)(zfs_refcount_count(&arc_anon->arcs_size) - |
36da08ef | 7384 | arc_loaned_bytes), 0); |
9babb374 | 7385 | |
34dc7c2f BB |
7386 | /* |
7387 | * Writes will, almost always, require additional memory allocations | |
d3cc8b15 | 7388 | * in order to compress/encrypt/etc the data. We therefore need to |
34dc7c2f BB |
7389 | * make sure that there is sufficient available memory for this. |
7390 | */ | |
dae3e9ea | 7391 | error = arc_memory_throttle(spa, reserve, txg); |
e8b96c60 | 7392 | if (error != 0) |
34dc7c2f BB |
7393 | return (error); |
7394 | ||
7395 | /* | |
7396 | * Throttle writes when the amount of dirty data in the cache | |
7397 | * gets too large. We try to keep the cache less than half full | |
7398 | * of dirty blocks so that our sync times don't grow too large. | |
dae3e9ea DB |
7399 | * |
7400 | * In the case of one pool being built on another pool, we want | |
7401 | * to make sure we don't end up throttling the lower (backing) | |
7402 | * pool when the upper pool is the majority contributor to dirty | |
7403 | * data. To insure we make forward progress during throttling, we | |
7404 | * also check the current pool's net dirty data and only throttle | |
7405 | * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty | |
7406 | * data in the cache. | |
7407 | * | |
34dc7c2f BB |
7408 | * Note: if two requests come in concurrently, we might let them |
7409 | * both succeed, when one of them should fail. Not a huge deal. | |
7410 | */ | |
dae3e9ea DB |
7411 | uint64_t total_dirty = reserve + arc_tempreserve + anon_size; |
7412 | uint64_t spa_dirty_anon = spa_dirty_data(spa); | |
9babb374 | 7413 | |
dae3e9ea DB |
7414 | if (total_dirty > arc_c * zfs_arc_dirty_limit_percent / 100 && |
7415 | anon_size > arc_c * zfs_arc_anon_limit_percent / 100 && | |
7416 | spa_dirty_anon > anon_size * zfs_arc_pool_dirty_percent / 100) { | |
2fd92c3d | 7417 | #ifdef ZFS_DEBUG |
424fd7c3 TS |
7418 | uint64_t meta_esize = zfs_refcount_count( |
7419 | &arc_anon->arcs_esize[ARC_BUFC_METADATA]); | |
d3c2ae1c | 7420 | uint64_t data_esize = |
424fd7c3 | 7421 | zfs_refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]); |
34dc7c2f BB |
7422 | dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " |
7423 | "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", | |
d3c2ae1c GW |
7424 | arc_tempreserve >> 10, meta_esize >> 10, |
7425 | data_esize >> 10, reserve >> 10, arc_c >> 10); | |
2fd92c3d | 7426 | #endif |
570827e1 | 7427 | DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle); |
2e528b49 | 7428 | return (SET_ERROR(ERESTART)); |
34dc7c2f BB |
7429 | } |
7430 | atomic_add_64(&arc_tempreserve, reserve); | |
7431 | return (0); | |
7432 | } | |
7433 | ||
13be560d BB |
7434 | static void |
7435 | arc_kstat_update_state(arc_state_t *state, kstat_named_t *size, | |
7436 | kstat_named_t *evict_data, kstat_named_t *evict_metadata) | |
7437 | { | |
424fd7c3 | 7438 | size->value.ui64 = zfs_refcount_count(&state->arcs_size); |
d3c2ae1c | 7439 | evict_data->value.ui64 = |
424fd7c3 | 7440 | zfs_refcount_count(&state->arcs_esize[ARC_BUFC_DATA]); |
d3c2ae1c | 7441 | evict_metadata->value.ui64 = |
424fd7c3 | 7442 | zfs_refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]); |
13be560d BB |
7443 | } |
7444 | ||
7445 | static int | |
7446 | arc_kstat_update(kstat_t *ksp, int rw) | |
7447 | { | |
7448 | arc_stats_t *as = ksp->ks_data; | |
7449 | ||
7450 | if (rw == KSTAT_WRITE) { | |
ecb2b7dc | 7451 | return (SET_ERROR(EACCES)); |
13be560d BB |
7452 | } else { |
7453 | arc_kstat_update_state(arc_anon, | |
7454 | &as->arcstat_anon_size, | |
500445c0 PS |
7455 | &as->arcstat_anon_evictable_data, |
7456 | &as->arcstat_anon_evictable_metadata); | |
13be560d BB |
7457 | arc_kstat_update_state(arc_mru, |
7458 | &as->arcstat_mru_size, | |
500445c0 PS |
7459 | &as->arcstat_mru_evictable_data, |
7460 | &as->arcstat_mru_evictable_metadata); | |
13be560d BB |
7461 | arc_kstat_update_state(arc_mru_ghost, |
7462 | &as->arcstat_mru_ghost_size, | |
500445c0 PS |
7463 | &as->arcstat_mru_ghost_evictable_data, |
7464 | &as->arcstat_mru_ghost_evictable_metadata); | |
13be560d BB |
7465 | arc_kstat_update_state(arc_mfu, |
7466 | &as->arcstat_mfu_size, | |
500445c0 PS |
7467 | &as->arcstat_mfu_evictable_data, |
7468 | &as->arcstat_mfu_evictable_metadata); | |
fc41c640 | 7469 | arc_kstat_update_state(arc_mfu_ghost, |
13be560d | 7470 | &as->arcstat_mfu_ghost_size, |
500445c0 PS |
7471 | &as->arcstat_mfu_ghost_evictable_data, |
7472 | &as->arcstat_mfu_ghost_evictable_metadata); | |
70f02287 | 7473 | |
37fb3e43 PD |
7474 | ARCSTAT(arcstat_size) = aggsum_value(&arc_size); |
7475 | ARCSTAT(arcstat_meta_used) = aggsum_value(&arc_meta_used); | |
7476 | ARCSTAT(arcstat_data_size) = aggsum_value(&astat_data_size); | |
7477 | ARCSTAT(arcstat_metadata_size) = | |
7478 | aggsum_value(&astat_metadata_size); | |
7479 | ARCSTAT(arcstat_hdr_size) = aggsum_value(&astat_hdr_size); | |
7480 | ARCSTAT(arcstat_l2_hdr_size) = aggsum_value(&astat_l2_hdr_size); | |
7481 | ARCSTAT(arcstat_dbuf_size) = aggsum_value(&astat_dbuf_size); | |
7482 | ARCSTAT(arcstat_dnode_size) = aggsum_value(&astat_dnode_size); | |
7483 | ARCSTAT(arcstat_bonus_size) = aggsum_value(&astat_bonus_size); | |
7484 | ||
70f02287 BB |
7485 | as->arcstat_memory_all_bytes.value.ui64 = |
7486 | arc_all_memory(); | |
7487 | as->arcstat_memory_free_bytes.value.ui64 = | |
7488 | arc_free_memory(); | |
7489 | as->arcstat_memory_available_bytes.value.i64 = | |
7490 | arc_available_memory(); | |
13be560d BB |
7491 | } |
7492 | ||
7493 | return (0); | |
7494 | } | |
7495 | ||
ca0bf58d PS |
7496 | /* |
7497 | * This function *must* return indices evenly distributed between all | |
7498 | * sublists of the multilist. This is needed due to how the ARC eviction | |
7499 | * code is laid out; arc_evict_state() assumes ARC buffers are evenly | |
7500 | * distributed between all sublists and uses this assumption when | |
7501 | * deciding which sublist to evict from and how much to evict from it. | |
7502 | */ | |
7503 | unsigned int | |
7504 | arc_state_multilist_index_func(multilist_t *ml, void *obj) | |
7505 | { | |
7506 | arc_buf_hdr_t *hdr = obj; | |
7507 | ||
7508 | /* | |
7509 | * We rely on b_dva to generate evenly distributed index | |
7510 | * numbers using buf_hash below. So, as an added precaution, | |
7511 | * let's make sure we never add empty buffers to the arc lists. | |
7512 | */ | |
d3c2ae1c | 7513 | ASSERT(!HDR_EMPTY(hdr)); |
ca0bf58d PS |
7514 | |
7515 | /* | |
7516 | * The assumption here, is the hash value for a given | |
7517 | * arc_buf_hdr_t will remain constant throughout its lifetime | |
7518 | * (i.e. its b_spa, b_dva, and b_birth fields don't change). | |
7519 | * Thus, we don't need to store the header's sublist index | |
7520 | * on insertion, as this index can be recalculated on removal. | |
7521 | * | |
7522 | * Also, the low order bits of the hash value are thought to be | |
7523 | * distributed evenly. Otherwise, in the case that the multilist | |
7524 | * has a power of two number of sublists, each sublists' usage | |
7525 | * would not be evenly distributed. | |
7526 | */ | |
7527 | return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % | |
7528 | multilist_get_num_sublists(ml)); | |
7529 | } | |
7530 | ||
ca67b33a MA |
7531 | /* |
7532 | * Called during module initialization and periodically thereafter to | |
7533 | * apply reasonable changes to the exposed performance tunings. Non-zero | |
7534 | * zfs_* values which differ from the currently set values will be applied. | |
7535 | */ | |
7536 | static void | |
7537 | arc_tuning_update(void) | |
7538 | { | |
b8a97fb1 | 7539 | uint64_t allmem = arc_all_memory(); |
7540 | unsigned long limit; | |
9edb3695 | 7541 | |
ca67b33a MA |
7542 | /* Valid range: 64M - <all physical memory> */ |
7543 | if ((zfs_arc_max) && (zfs_arc_max != arc_c_max) && | |
7403d074 | 7544 | (zfs_arc_max >= 64 << 20) && (zfs_arc_max < allmem) && |
ca67b33a MA |
7545 | (zfs_arc_max > arc_c_min)) { |
7546 | arc_c_max = zfs_arc_max; | |
7547 | arc_c = arc_c_max; | |
7548 | arc_p = (arc_c >> 1); | |
b8a97fb1 | 7549 | if (arc_meta_limit > arc_c_max) |
7550 | arc_meta_limit = arc_c_max; | |
7551 | if (arc_dnode_limit > arc_meta_limit) | |
7552 | arc_dnode_limit = arc_meta_limit; | |
ca67b33a MA |
7553 | } |
7554 | ||
7555 | /* Valid range: 32M - <arc_c_max> */ | |
7556 | if ((zfs_arc_min) && (zfs_arc_min != arc_c_min) && | |
7557 | (zfs_arc_min >= 2ULL << SPA_MAXBLOCKSHIFT) && | |
7558 | (zfs_arc_min <= arc_c_max)) { | |
7559 | arc_c_min = zfs_arc_min; | |
7560 | arc_c = MAX(arc_c, arc_c_min); | |
7561 | } | |
7562 | ||
7563 | /* Valid range: 16M - <arc_c_max> */ | |
7564 | if ((zfs_arc_meta_min) && (zfs_arc_meta_min != arc_meta_min) && | |
7565 | (zfs_arc_meta_min >= 1ULL << SPA_MAXBLOCKSHIFT) && | |
7566 | (zfs_arc_meta_min <= arc_c_max)) { | |
7567 | arc_meta_min = zfs_arc_meta_min; | |
b8a97fb1 | 7568 | if (arc_meta_limit < arc_meta_min) |
7569 | arc_meta_limit = arc_meta_min; | |
7570 | if (arc_dnode_limit < arc_meta_min) | |
7571 | arc_dnode_limit = arc_meta_min; | |
ca67b33a MA |
7572 | } |
7573 | ||
7574 | /* Valid range: <arc_meta_min> - <arc_c_max> */ | |
b8a97fb1 | 7575 | limit = zfs_arc_meta_limit ? zfs_arc_meta_limit : |
7576 | MIN(zfs_arc_meta_limit_percent, 100) * arc_c_max / 100; | |
7577 | if ((limit != arc_meta_limit) && | |
7578 | (limit >= arc_meta_min) && | |
7579 | (limit <= arc_c_max)) | |
7580 | arc_meta_limit = limit; | |
7581 | ||
7582 | /* Valid range: <arc_meta_min> - <arc_meta_limit> */ | |
7583 | limit = zfs_arc_dnode_limit ? zfs_arc_dnode_limit : | |
7584 | MIN(zfs_arc_dnode_limit_percent, 100) * arc_meta_limit / 100; | |
7585 | if ((limit != arc_dnode_limit) && | |
7586 | (limit >= arc_meta_min) && | |
7587 | (limit <= arc_meta_limit)) | |
7588 | arc_dnode_limit = limit; | |
25458cbe | 7589 | |
ca67b33a MA |
7590 | /* Valid range: 1 - N */ |
7591 | if (zfs_arc_grow_retry) | |
7592 | arc_grow_retry = zfs_arc_grow_retry; | |
7593 | ||
7594 | /* Valid range: 1 - N */ | |
7595 | if (zfs_arc_shrink_shift) { | |
7596 | arc_shrink_shift = zfs_arc_shrink_shift; | |
7597 | arc_no_grow_shift = MIN(arc_no_grow_shift, arc_shrink_shift -1); | |
7598 | } | |
7599 | ||
728d6ae9 BB |
7600 | /* Valid range: 1 - N */ |
7601 | if (zfs_arc_p_min_shift) | |
7602 | arc_p_min_shift = zfs_arc_p_min_shift; | |
7603 | ||
d4a72f23 TC |
7604 | /* Valid range: 1 - N ms */ |
7605 | if (zfs_arc_min_prefetch_ms) | |
7606 | arc_min_prefetch_ms = zfs_arc_min_prefetch_ms; | |
7607 | ||
7608 | /* Valid range: 1 - N ms */ | |
7609 | if (zfs_arc_min_prescient_prefetch_ms) { | |
7610 | arc_min_prescient_prefetch_ms = | |
7611 | zfs_arc_min_prescient_prefetch_ms; | |
7612 | } | |
11f552fa | 7613 | |
7e8bddd0 BB |
7614 | /* Valid range: 0 - 100 */ |
7615 | if ((zfs_arc_lotsfree_percent >= 0) && | |
7616 | (zfs_arc_lotsfree_percent <= 100)) | |
7617 | arc_lotsfree_percent = zfs_arc_lotsfree_percent; | |
7618 | ||
11f552fa BB |
7619 | /* Valid range: 0 - <all physical memory> */ |
7620 | if ((zfs_arc_sys_free) && (zfs_arc_sys_free != arc_sys_free)) | |
9edb3695 | 7621 | arc_sys_free = MIN(MAX(zfs_arc_sys_free, 0), allmem); |
7e8bddd0 | 7622 | |
ca67b33a MA |
7623 | } |
7624 | ||
d3c2ae1c GW |
7625 | static void |
7626 | arc_state_init(void) | |
7627 | { | |
7628 | arc_anon = &ARC_anon; | |
7629 | arc_mru = &ARC_mru; | |
7630 | arc_mru_ghost = &ARC_mru_ghost; | |
7631 | arc_mfu = &ARC_mfu; | |
7632 | arc_mfu_ghost = &ARC_mfu_ghost; | |
7633 | arc_l2c_only = &ARC_l2c_only; | |
7634 | ||
64fc7762 MA |
7635 | arc_mru->arcs_list[ARC_BUFC_METADATA] = |
7636 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7637 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7638 | arc_state_multilist_index_func); |
64fc7762 MA |
7639 | arc_mru->arcs_list[ARC_BUFC_DATA] = |
7640 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7641 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7642 | arc_state_multilist_index_func); |
64fc7762 MA |
7643 | arc_mru_ghost->arcs_list[ARC_BUFC_METADATA] = |
7644 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7645 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7646 | arc_state_multilist_index_func); |
64fc7762 MA |
7647 | arc_mru_ghost->arcs_list[ARC_BUFC_DATA] = |
7648 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7649 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7650 | arc_state_multilist_index_func); |
64fc7762 MA |
7651 | arc_mfu->arcs_list[ARC_BUFC_METADATA] = |
7652 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7653 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7654 | arc_state_multilist_index_func); |
64fc7762 MA |
7655 | arc_mfu->arcs_list[ARC_BUFC_DATA] = |
7656 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7657 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7658 | arc_state_multilist_index_func); |
64fc7762 MA |
7659 | arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA] = |
7660 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7661 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7662 | arc_state_multilist_index_func); |
64fc7762 MA |
7663 | arc_mfu_ghost->arcs_list[ARC_BUFC_DATA] = |
7664 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7665 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7666 | arc_state_multilist_index_func); |
64fc7762 MA |
7667 | arc_l2c_only->arcs_list[ARC_BUFC_METADATA] = |
7668 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7669 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7670 | arc_state_multilist_index_func); |
64fc7762 MA |
7671 | arc_l2c_only->arcs_list[ARC_BUFC_DATA] = |
7672 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7673 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7674 | arc_state_multilist_index_func); |
d3c2ae1c | 7675 | |
424fd7c3 TS |
7676 | zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); |
7677 | zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]); | |
7678 | zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); | |
7679 | zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]); | |
7680 | zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7681 | zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7682 | zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); | |
7683 | zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); | |
7684 | zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7685 | zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7686 | zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); | |
7687 | zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); | |
7688 | ||
7689 | zfs_refcount_create(&arc_anon->arcs_size); | |
7690 | zfs_refcount_create(&arc_mru->arcs_size); | |
7691 | zfs_refcount_create(&arc_mru_ghost->arcs_size); | |
7692 | zfs_refcount_create(&arc_mfu->arcs_size); | |
7693 | zfs_refcount_create(&arc_mfu_ghost->arcs_size); | |
7694 | zfs_refcount_create(&arc_l2c_only->arcs_size); | |
d3c2ae1c | 7695 | |
37fb3e43 PD |
7696 | aggsum_init(&arc_meta_used, 0); |
7697 | aggsum_init(&arc_size, 0); | |
7698 | aggsum_init(&astat_data_size, 0); | |
7699 | aggsum_init(&astat_metadata_size, 0); | |
7700 | aggsum_init(&astat_hdr_size, 0); | |
7701 | aggsum_init(&astat_l2_hdr_size, 0); | |
7702 | aggsum_init(&astat_bonus_size, 0); | |
7703 | aggsum_init(&astat_dnode_size, 0); | |
7704 | aggsum_init(&astat_dbuf_size, 0); | |
7705 | ||
d3c2ae1c GW |
7706 | arc_anon->arcs_state = ARC_STATE_ANON; |
7707 | arc_mru->arcs_state = ARC_STATE_MRU; | |
7708 | arc_mru_ghost->arcs_state = ARC_STATE_MRU_GHOST; | |
7709 | arc_mfu->arcs_state = ARC_STATE_MFU; | |
7710 | arc_mfu_ghost->arcs_state = ARC_STATE_MFU_GHOST; | |
7711 | arc_l2c_only->arcs_state = ARC_STATE_L2C_ONLY; | |
7712 | } | |
7713 | ||
7714 | static void | |
7715 | arc_state_fini(void) | |
7716 | { | |
424fd7c3 TS |
7717 | zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); |
7718 | zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]); | |
7719 | zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); | |
7720 | zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]); | |
7721 | zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7722 | zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7723 | zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); | |
7724 | zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); | |
7725 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7726 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7727 | zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); | |
7728 | zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); | |
7729 | ||
7730 | zfs_refcount_destroy(&arc_anon->arcs_size); | |
7731 | zfs_refcount_destroy(&arc_mru->arcs_size); | |
7732 | zfs_refcount_destroy(&arc_mru_ghost->arcs_size); | |
7733 | zfs_refcount_destroy(&arc_mfu->arcs_size); | |
7734 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_size); | |
7735 | zfs_refcount_destroy(&arc_l2c_only->arcs_size); | |
d3c2ae1c | 7736 | |
64fc7762 MA |
7737 | multilist_destroy(arc_mru->arcs_list[ARC_BUFC_METADATA]); |
7738 | multilist_destroy(arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); | |
7739 | multilist_destroy(arc_mfu->arcs_list[ARC_BUFC_METADATA]); | |
7740 | multilist_destroy(arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); | |
7741 | multilist_destroy(arc_mru->arcs_list[ARC_BUFC_DATA]); | |
7742 | multilist_destroy(arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); | |
7743 | multilist_destroy(arc_mfu->arcs_list[ARC_BUFC_DATA]); | |
7744 | multilist_destroy(arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); | |
7745 | multilist_destroy(arc_l2c_only->arcs_list[ARC_BUFC_METADATA]); | |
7746 | multilist_destroy(arc_l2c_only->arcs_list[ARC_BUFC_DATA]); | |
37fb3e43 PD |
7747 | |
7748 | aggsum_fini(&arc_meta_used); | |
7749 | aggsum_fini(&arc_size); | |
7750 | aggsum_fini(&astat_data_size); | |
7751 | aggsum_fini(&astat_metadata_size); | |
7752 | aggsum_fini(&astat_hdr_size); | |
7753 | aggsum_fini(&astat_l2_hdr_size); | |
7754 | aggsum_fini(&astat_bonus_size); | |
7755 | aggsum_fini(&astat_dnode_size); | |
7756 | aggsum_fini(&astat_dbuf_size); | |
d3c2ae1c GW |
7757 | } |
7758 | ||
7759 | uint64_t | |
e71cade6 | 7760 | arc_target_bytes(void) |
d3c2ae1c | 7761 | { |
e71cade6 | 7762 | return (arc_c); |
d3c2ae1c GW |
7763 | } |
7764 | ||
34dc7c2f BB |
7765 | void |
7766 | arc_init(void) | |
7767 | { | |
9edb3695 | 7768 | uint64_t percent, allmem = arc_all_memory(); |
3ec34e55 BL |
7769 | mutex_init(&arc_adjust_lock, NULL, MUTEX_DEFAULT, NULL); |
7770 | cv_init(&arc_adjust_waiters_cv, NULL, CV_DEFAULT, NULL); | |
ca0bf58d | 7771 | |
2b84817f TC |
7772 | arc_min_prefetch_ms = 1000; |
7773 | arc_min_prescient_prefetch_ms = 6000; | |
34dc7c2f | 7774 | |
34dc7c2f | 7775 | #ifdef _KERNEL |
7cb67b45 BB |
7776 | /* |
7777 | * Register a shrinker to support synchronous (direct) memory | |
7778 | * reclaim from the arc. This is done to prevent kswapd from | |
7779 | * swapping out pages when it is preferable to shrink the arc. | |
7780 | */ | |
7781 | spl_register_shrinker(&arc_shrinker); | |
11f552fa BB |
7782 | |
7783 | /* Set to 1/64 of all memory or a minimum of 512K */ | |
9edb3695 | 7784 | arc_sys_free = MAX(allmem / 64, (512 * 1024)); |
11f552fa | 7785 | arc_need_free = 0; |
34dc7c2f BB |
7786 | #endif |
7787 | ||
0a1f8cd9 TC |
7788 | /* Set max to 1/2 of all memory */ |
7789 | arc_c_max = allmem / 2; | |
7790 | ||
4ce3c45a BB |
7791 | #ifdef _KERNEL |
7792 | /* Set min cache to 1/32 of all memory, or 32MB, whichever is more */ | |
7793 | arc_c_min = MAX(allmem / 32, 2ULL << SPA_MAXBLOCKSHIFT); | |
7794 | #else | |
ab5cbbd1 BB |
7795 | /* |
7796 | * In userland, there's only the memory pressure that we artificially | |
7797 | * create (see arc_available_memory()). Don't let arc_c get too | |
7798 | * small, because it can cause transactions to be larger than | |
7799 | * arc_c, causing arc_tempreserve_space() to fail. | |
7800 | */ | |
0a1f8cd9 | 7801 | arc_c_min = MAX(arc_c_max / 2, 2ULL << SPA_MAXBLOCKSHIFT); |
ab5cbbd1 BB |
7802 | #endif |
7803 | ||
34dc7c2f BB |
7804 | arc_c = arc_c_max; |
7805 | arc_p = (arc_c >> 1); | |
7806 | ||
ca67b33a MA |
7807 | /* Set min to 1/2 of arc_c_min */ |
7808 | arc_meta_min = 1ULL << SPA_MAXBLOCKSHIFT; | |
7809 | /* Initialize maximum observed usage to zero */ | |
1834f2d8 | 7810 | arc_meta_max = 0; |
9907cc1c G |
7811 | /* |
7812 | * Set arc_meta_limit to a percent of arc_c_max with a floor of | |
7813 | * arc_meta_min, and a ceiling of arc_c_max. | |
7814 | */ | |
7815 | percent = MIN(zfs_arc_meta_limit_percent, 100); | |
7816 | arc_meta_limit = MAX(arc_meta_min, (percent * arc_c_max) / 100); | |
7817 | percent = MIN(zfs_arc_dnode_limit_percent, 100); | |
7818 | arc_dnode_limit = (percent * arc_meta_limit) / 100; | |
34dc7c2f | 7819 | |
ca67b33a MA |
7820 | /* Apply user specified tunings */ |
7821 | arc_tuning_update(); | |
c52fca13 | 7822 | |
34dc7c2f BB |
7823 | /* if kmem_flags are set, lets try to use less memory */ |
7824 | if (kmem_debugging()) | |
7825 | arc_c = arc_c / 2; | |
7826 | if (arc_c < arc_c_min) | |
7827 | arc_c = arc_c_min; | |
7828 | ||
d3c2ae1c | 7829 | arc_state_init(); |
3ec34e55 BL |
7830 | |
7831 | /* | |
7832 | * The arc must be "uninitialized", so that hdr_recl() (which is | |
7833 | * registered by buf_init()) will not access arc_reap_zthr before | |
7834 | * it is created. | |
7835 | */ | |
7836 | ASSERT(!arc_initialized); | |
34dc7c2f BB |
7837 | buf_init(); |
7838 | ||
ab26409d BB |
7839 | list_create(&arc_prune_list, sizeof (arc_prune_t), |
7840 | offsetof(arc_prune_t, p_node)); | |
ab26409d | 7841 | mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f | 7842 | |
1229323d | 7843 | arc_prune_taskq = taskq_create("arc_prune", max_ncpus, defclsyspri, |
aa9af22c | 7844 | max_ncpus, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC); |
f6046738 | 7845 | |
34dc7c2f BB |
7846 | arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, |
7847 | sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); | |
7848 | ||
7849 | if (arc_ksp != NULL) { | |
7850 | arc_ksp->ks_data = &arc_stats; | |
13be560d | 7851 | arc_ksp->ks_update = arc_kstat_update; |
34dc7c2f BB |
7852 | kstat_install(arc_ksp); |
7853 | } | |
7854 | ||
3ec34e55 BL |
7855 | arc_adjust_zthr = zthr_create(arc_adjust_cb_check, |
7856 | arc_adjust_cb, NULL); | |
7857 | arc_reap_zthr = zthr_create_timer(arc_reap_cb_check, | |
7858 | arc_reap_cb, NULL, SEC2NSEC(1)); | |
34dc7c2f | 7859 | |
3ec34e55 | 7860 | arc_initialized = B_TRUE; |
b128c09f | 7861 | arc_warm = B_FALSE; |
34dc7c2f | 7862 | |
e8b96c60 MA |
7863 | /* |
7864 | * Calculate maximum amount of dirty data per pool. | |
7865 | * | |
7866 | * If it has been set by a module parameter, take that. | |
7867 | * Otherwise, use a percentage of physical memory defined by | |
7868 | * zfs_dirty_data_max_percent (default 10%) with a cap at | |
e99932f7 | 7869 | * zfs_dirty_data_max_max (default 4G or 25% of physical memory). |
e8b96c60 MA |
7870 | */ |
7871 | if (zfs_dirty_data_max_max == 0) | |
e99932f7 BB |
7872 | zfs_dirty_data_max_max = MIN(4ULL * 1024 * 1024 * 1024, |
7873 | allmem * zfs_dirty_data_max_max_percent / 100); | |
e8b96c60 MA |
7874 | |
7875 | if (zfs_dirty_data_max == 0) { | |
9edb3695 | 7876 | zfs_dirty_data_max = allmem * |
e8b96c60 MA |
7877 | zfs_dirty_data_max_percent / 100; |
7878 | zfs_dirty_data_max = MIN(zfs_dirty_data_max, | |
7879 | zfs_dirty_data_max_max); | |
7880 | } | |
34dc7c2f BB |
7881 | } |
7882 | ||
7883 | void | |
7884 | arc_fini(void) | |
7885 | { | |
ab26409d BB |
7886 | arc_prune_t *p; |
7887 | ||
7cb67b45 BB |
7888 | #ifdef _KERNEL |
7889 | spl_unregister_shrinker(&arc_shrinker); | |
7890 | #endif /* _KERNEL */ | |
7891 | ||
d3c2ae1c GW |
7892 | /* Use B_TRUE to ensure *all* buffers are evicted */ |
7893 | arc_flush(NULL, B_TRUE); | |
34dc7c2f | 7894 | |
3ec34e55 | 7895 | arc_initialized = B_FALSE; |
34dc7c2f BB |
7896 | |
7897 | if (arc_ksp != NULL) { | |
7898 | kstat_delete(arc_ksp); | |
7899 | arc_ksp = NULL; | |
7900 | } | |
7901 | ||
f6046738 BB |
7902 | taskq_wait(arc_prune_taskq); |
7903 | taskq_destroy(arc_prune_taskq); | |
7904 | ||
ab26409d BB |
7905 | mutex_enter(&arc_prune_mtx); |
7906 | while ((p = list_head(&arc_prune_list)) != NULL) { | |
7907 | list_remove(&arc_prune_list, p); | |
424fd7c3 TS |
7908 | zfs_refcount_remove(&p->p_refcnt, &arc_prune_list); |
7909 | zfs_refcount_destroy(&p->p_refcnt); | |
ab26409d BB |
7910 | kmem_free(p, sizeof (*p)); |
7911 | } | |
7912 | mutex_exit(&arc_prune_mtx); | |
7913 | ||
7914 | list_destroy(&arc_prune_list); | |
7915 | mutex_destroy(&arc_prune_mtx); | |
3ec34e55 BL |
7916 | (void) zthr_cancel(arc_adjust_zthr); |
7917 | zthr_destroy(arc_adjust_zthr); | |
7918 | ||
7919 | (void) zthr_cancel(arc_reap_zthr); | |
7920 | zthr_destroy(arc_reap_zthr); | |
7921 | ||
7922 | mutex_destroy(&arc_adjust_lock); | |
7923 | cv_destroy(&arc_adjust_waiters_cv); | |
ca0bf58d | 7924 | |
ae3d8491 PD |
7925 | /* |
7926 | * buf_fini() must proceed arc_state_fini() because buf_fin() may | |
7927 | * trigger the release of kmem magazines, which can callback to | |
7928 | * arc_space_return() which accesses aggsums freed in act_state_fini(). | |
7929 | */ | |
34dc7c2f | 7930 | buf_fini(); |
ae3d8491 | 7931 | arc_state_fini(); |
9babb374 | 7932 | |
b9541d6b | 7933 | ASSERT0(arc_loaned_bytes); |
34dc7c2f BB |
7934 | } |
7935 | ||
7936 | /* | |
7937 | * Level 2 ARC | |
7938 | * | |
7939 | * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. | |
7940 | * It uses dedicated storage devices to hold cached data, which are populated | |
7941 | * using large infrequent writes. The main role of this cache is to boost | |
7942 | * the performance of random read workloads. The intended L2ARC devices | |
7943 | * include short-stroked disks, solid state disks, and other media with | |
7944 | * substantially faster read latency than disk. | |
7945 | * | |
7946 | * +-----------------------+ | |
7947 | * | ARC | | |
7948 | * +-----------------------+ | |
7949 | * | ^ ^ | |
7950 | * | | | | |
7951 | * l2arc_feed_thread() arc_read() | |
7952 | * | | | | |
7953 | * | l2arc read | | |
7954 | * V | | | |
7955 | * +---------------+ | | |
7956 | * | L2ARC | | | |
7957 | * +---------------+ | | |
7958 | * | ^ | | |
7959 | * l2arc_write() | | | |
7960 | * | | | | |
7961 | * V | | | |
7962 | * +-------+ +-------+ | |
7963 | * | vdev | | vdev | | |
7964 | * | cache | | cache | | |
7965 | * +-------+ +-------+ | |
7966 | * +=========+ .-----. | |
7967 | * : L2ARC : |-_____-| | |
7968 | * : devices : | Disks | | |
7969 | * +=========+ `-_____-' | |
7970 | * | |
7971 | * Read requests are satisfied from the following sources, in order: | |
7972 | * | |
7973 | * 1) ARC | |
7974 | * 2) vdev cache of L2ARC devices | |
7975 | * 3) L2ARC devices | |
7976 | * 4) vdev cache of disks | |
7977 | * 5) disks | |
7978 | * | |
7979 | * Some L2ARC device types exhibit extremely slow write performance. | |
7980 | * To accommodate for this there are some significant differences between | |
7981 | * the L2ARC and traditional cache design: | |
7982 | * | |
7983 | * 1. There is no eviction path from the ARC to the L2ARC. Evictions from | |
7984 | * the ARC behave as usual, freeing buffers and placing headers on ghost | |
7985 | * lists. The ARC does not send buffers to the L2ARC during eviction as | |
7986 | * this would add inflated write latencies for all ARC memory pressure. | |
7987 | * | |
7988 | * 2. The L2ARC attempts to cache data from the ARC before it is evicted. | |
7989 | * It does this by periodically scanning buffers from the eviction-end of | |
7990 | * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are | |
3a17a7a9 SK |
7991 | * not already there. It scans until a headroom of buffers is satisfied, |
7992 | * which itself is a buffer for ARC eviction. If a compressible buffer is | |
7993 | * found during scanning and selected for writing to an L2ARC device, we | |
7994 | * temporarily boost scanning headroom during the next scan cycle to make | |
7995 | * sure we adapt to compression effects (which might significantly reduce | |
7996 | * the data volume we write to L2ARC). The thread that does this is | |
34dc7c2f BB |
7997 | * l2arc_feed_thread(), illustrated below; example sizes are included to |
7998 | * provide a better sense of ratio than this diagram: | |
7999 | * | |
8000 | * head --> tail | |
8001 | * +---------------------+----------+ | |
8002 | * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC | |
8003 | * +---------------------+----------+ | o L2ARC eligible | |
8004 | * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer | |
8005 | * +---------------------+----------+ | | |
8006 | * 15.9 Gbytes ^ 32 Mbytes | | |
8007 | * headroom | | |
8008 | * l2arc_feed_thread() | |
8009 | * | | |
8010 | * l2arc write hand <--[oooo]--' | |
8011 | * | 8 Mbyte | |
8012 | * | write max | |
8013 | * V | |
8014 | * +==============================+ | |
8015 | * L2ARC dev |####|#|###|###| |####| ... | | |
8016 | * +==============================+ | |
8017 | * 32 Gbytes | |
8018 | * | |
8019 | * 3. If an ARC buffer is copied to the L2ARC but then hit instead of | |
8020 | * evicted, then the L2ARC has cached a buffer much sooner than it probably | |
8021 | * needed to, potentially wasting L2ARC device bandwidth and storage. It is | |
8022 | * safe to say that this is an uncommon case, since buffers at the end of | |
8023 | * the ARC lists have moved there due to inactivity. | |
8024 | * | |
8025 | * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, | |
8026 | * then the L2ARC simply misses copying some buffers. This serves as a | |
8027 | * pressure valve to prevent heavy read workloads from both stalling the ARC | |
8028 | * with waits and clogging the L2ARC with writes. This also helps prevent | |
8029 | * the potential for the L2ARC to churn if it attempts to cache content too | |
8030 | * quickly, such as during backups of the entire pool. | |
8031 | * | |
b128c09f BB |
8032 | * 5. After system boot and before the ARC has filled main memory, there are |
8033 | * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru | |
8034 | * lists can remain mostly static. Instead of searching from tail of these | |
8035 | * lists as pictured, the l2arc_feed_thread() will search from the list heads | |
8036 | * for eligible buffers, greatly increasing its chance of finding them. | |
8037 | * | |
8038 | * The L2ARC device write speed is also boosted during this time so that | |
8039 | * the L2ARC warms up faster. Since there have been no ARC evictions yet, | |
8040 | * there are no L2ARC reads, and no fear of degrading read performance | |
8041 | * through increased writes. | |
8042 | * | |
8043 | * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that | |
34dc7c2f BB |
8044 | * the vdev queue can aggregate them into larger and fewer writes. Each |
8045 | * device is written to in a rotor fashion, sweeping writes through | |
8046 | * available space then repeating. | |
8047 | * | |
b128c09f | 8048 | * 7. The L2ARC does not store dirty content. It never needs to flush |
34dc7c2f BB |
8049 | * write buffers back to disk based storage. |
8050 | * | |
b128c09f | 8051 | * 8. If an ARC buffer is written (and dirtied) which also exists in the |
34dc7c2f BB |
8052 | * L2ARC, the now stale L2ARC buffer is immediately dropped. |
8053 | * | |
8054 | * The performance of the L2ARC can be tweaked by a number of tunables, which | |
8055 | * may be necessary for different workloads: | |
8056 | * | |
8057 | * l2arc_write_max max write bytes per interval | |
b128c09f | 8058 | * l2arc_write_boost extra write bytes during device warmup |
34dc7c2f BB |
8059 | * l2arc_noprefetch skip caching prefetched buffers |
8060 | * l2arc_headroom number of max device writes to precache | |
3a17a7a9 SK |
8061 | * l2arc_headroom_boost when we find compressed buffers during ARC |
8062 | * scanning, we multiply headroom by this | |
8063 | * percentage factor for the next scan cycle, | |
8064 | * since more compressed buffers are likely to | |
8065 | * be present | |
34dc7c2f BB |
8066 | * l2arc_feed_secs seconds between L2ARC writing |
8067 | * | |
8068 | * Tunables may be removed or added as future performance improvements are | |
8069 | * integrated, and also may become zpool properties. | |
d164b209 BB |
8070 | * |
8071 | * There are three key functions that control how the L2ARC warms up: | |
8072 | * | |
8073 | * l2arc_write_eligible() check if a buffer is eligible to cache | |
8074 | * l2arc_write_size() calculate how much to write | |
8075 | * l2arc_write_interval() calculate sleep delay between writes | |
8076 | * | |
8077 | * These three functions determine what to write, how much, and how quickly | |
8078 | * to send writes. | |
34dc7c2f BB |
8079 | */ |
8080 | ||
d164b209 | 8081 | static boolean_t |
2a432414 | 8082 | l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) |
d164b209 BB |
8083 | { |
8084 | /* | |
8085 | * A buffer is *not* eligible for the L2ARC if it: | |
8086 | * 1. belongs to a different spa. | |
428870ff BB |
8087 | * 2. is already cached on the L2ARC. |
8088 | * 3. has an I/O in progress (it may be an incomplete read). | |
8089 | * 4. is flagged not eligible (zfs property). | |
d164b209 | 8090 | */ |
b9541d6b | 8091 | if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) || |
2a432414 | 8092 | HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr)) |
d164b209 BB |
8093 | return (B_FALSE); |
8094 | ||
8095 | return (B_TRUE); | |
8096 | } | |
8097 | ||
8098 | static uint64_t | |
3a17a7a9 | 8099 | l2arc_write_size(void) |
d164b209 BB |
8100 | { |
8101 | uint64_t size; | |
8102 | ||
3a17a7a9 SK |
8103 | /* |
8104 | * Make sure our globals have meaningful values in case the user | |
8105 | * altered them. | |
8106 | */ | |
8107 | size = l2arc_write_max; | |
8108 | if (size == 0) { | |
8109 | cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " | |
8110 | "be greater than zero, resetting it to the default (%d)", | |
8111 | L2ARC_WRITE_SIZE); | |
8112 | size = l2arc_write_max = L2ARC_WRITE_SIZE; | |
8113 | } | |
d164b209 BB |
8114 | |
8115 | if (arc_warm == B_FALSE) | |
3a17a7a9 | 8116 | size += l2arc_write_boost; |
d164b209 BB |
8117 | |
8118 | return (size); | |
8119 | ||
8120 | } | |
8121 | ||
8122 | static clock_t | |
8123 | l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) | |
8124 | { | |
428870ff | 8125 | clock_t interval, next, now; |
d164b209 BB |
8126 | |
8127 | /* | |
8128 | * If the ARC lists are busy, increase our write rate; if the | |
8129 | * lists are stale, idle back. This is achieved by checking | |
8130 | * how much we previously wrote - if it was more than half of | |
8131 | * what we wanted, schedule the next write much sooner. | |
8132 | */ | |
8133 | if (l2arc_feed_again && wrote > (wanted / 2)) | |
8134 | interval = (hz * l2arc_feed_min_ms) / 1000; | |
8135 | else | |
8136 | interval = hz * l2arc_feed_secs; | |
8137 | ||
428870ff BB |
8138 | now = ddi_get_lbolt(); |
8139 | next = MAX(now, MIN(now + interval, began + interval)); | |
d164b209 BB |
8140 | |
8141 | return (next); | |
8142 | } | |
8143 | ||
34dc7c2f BB |
8144 | /* |
8145 | * Cycle through L2ARC devices. This is how L2ARC load balances. | |
b128c09f | 8146 | * If a device is returned, this also returns holding the spa config lock. |
34dc7c2f BB |
8147 | */ |
8148 | static l2arc_dev_t * | |
8149 | l2arc_dev_get_next(void) | |
8150 | { | |
b128c09f | 8151 | l2arc_dev_t *first, *next = NULL; |
34dc7c2f | 8152 | |
b128c09f BB |
8153 | /* |
8154 | * Lock out the removal of spas (spa_namespace_lock), then removal | |
8155 | * of cache devices (l2arc_dev_mtx). Once a device has been selected, | |
8156 | * both locks will be dropped and a spa config lock held instead. | |
8157 | */ | |
8158 | mutex_enter(&spa_namespace_lock); | |
8159 | mutex_enter(&l2arc_dev_mtx); | |
8160 | ||
8161 | /* if there are no vdevs, there is nothing to do */ | |
8162 | if (l2arc_ndev == 0) | |
8163 | goto out; | |
8164 | ||
8165 | first = NULL; | |
8166 | next = l2arc_dev_last; | |
8167 | do { | |
8168 | /* loop around the list looking for a non-faulted vdev */ | |
8169 | if (next == NULL) { | |
34dc7c2f | 8170 | next = list_head(l2arc_dev_list); |
b128c09f BB |
8171 | } else { |
8172 | next = list_next(l2arc_dev_list, next); | |
8173 | if (next == NULL) | |
8174 | next = list_head(l2arc_dev_list); | |
8175 | } | |
8176 | ||
8177 | /* if we have come back to the start, bail out */ | |
8178 | if (first == NULL) | |
8179 | first = next; | |
8180 | else if (next == first) | |
8181 | break; | |
8182 | ||
8183 | } while (vdev_is_dead(next->l2ad_vdev)); | |
8184 | ||
8185 | /* if we were unable to find any usable vdevs, return NULL */ | |
8186 | if (vdev_is_dead(next->l2ad_vdev)) | |
8187 | next = NULL; | |
34dc7c2f BB |
8188 | |
8189 | l2arc_dev_last = next; | |
8190 | ||
b128c09f BB |
8191 | out: |
8192 | mutex_exit(&l2arc_dev_mtx); | |
8193 | ||
8194 | /* | |
8195 | * Grab the config lock to prevent the 'next' device from being | |
8196 | * removed while we are writing to it. | |
8197 | */ | |
8198 | if (next != NULL) | |
8199 | spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); | |
8200 | mutex_exit(&spa_namespace_lock); | |
8201 | ||
34dc7c2f BB |
8202 | return (next); |
8203 | } | |
8204 | ||
b128c09f BB |
8205 | /* |
8206 | * Free buffers that were tagged for destruction. | |
8207 | */ | |
8208 | static void | |
0bc8fd78 | 8209 | l2arc_do_free_on_write(void) |
b128c09f BB |
8210 | { |
8211 | list_t *buflist; | |
8212 | l2arc_data_free_t *df, *df_prev; | |
8213 | ||
8214 | mutex_enter(&l2arc_free_on_write_mtx); | |
8215 | buflist = l2arc_free_on_write; | |
8216 | ||
8217 | for (df = list_tail(buflist); df; df = df_prev) { | |
8218 | df_prev = list_prev(buflist, df); | |
a6255b7f DQ |
8219 | ASSERT3P(df->l2df_abd, !=, NULL); |
8220 | abd_free(df->l2df_abd); | |
b128c09f BB |
8221 | list_remove(buflist, df); |
8222 | kmem_free(df, sizeof (l2arc_data_free_t)); | |
8223 | } | |
8224 | ||
8225 | mutex_exit(&l2arc_free_on_write_mtx); | |
8226 | } | |
8227 | ||
34dc7c2f BB |
8228 | /* |
8229 | * A write to a cache device has completed. Update all headers to allow | |
8230 | * reads from these buffers to begin. | |
8231 | */ | |
8232 | static void | |
8233 | l2arc_write_done(zio_t *zio) | |
8234 | { | |
8235 | l2arc_write_callback_t *cb; | |
8236 | l2arc_dev_t *dev; | |
8237 | list_t *buflist; | |
2a432414 | 8238 | arc_buf_hdr_t *head, *hdr, *hdr_prev; |
34dc7c2f | 8239 | kmutex_t *hash_lock; |
3bec585e | 8240 | int64_t bytes_dropped = 0; |
34dc7c2f BB |
8241 | |
8242 | cb = zio->io_private; | |
d3c2ae1c | 8243 | ASSERT3P(cb, !=, NULL); |
34dc7c2f | 8244 | dev = cb->l2wcb_dev; |
d3c2ae1c | 8245 | ASSERT3P(dev, !=, NULL); |
34dc7c2f | 8246 | head = cb->l2wcb_head; |
d3c2ae1c | 8247 | ASSERT3P(head, !=, NULL); |
b9541d6b | 8248 | buflist = &dev->l2ad_buflist; |
d3c2ae1c | 8249 | ASSERT3P(buflist, !=, NULL); |
34dc7c2f BB |
8250 | DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, |
8251 | l2arc_write_callback_t *, cb); | |
8252 | ||
8253 | if (zio->io_error != 0) | |
8254 | ARCSTAT_BUMP(arcstat_l2_writes_error); | |
8255 | ||
34dc7c2f BB |
8256 | /* |
8257 | * All writes completed, or an error was hit. | |
8258 | */ | |
ca0bf58d PS |
8259 | top: |
8260 | mutex_enter(&dev->l2ad_mtx); | |
2a432414 GW |
8261 | for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { |
8262 | hdr_prev = list_prev(buflist, hdr); | |
34dc7c2f | 8263 | |
2a432414 | 8264 | hash_lock = HDR_LOCK(hdr); |
ca0bf58d PS |
8265 | |
8266 | /* | |
8267 | * We cannot use mutex_enter or else we can deadlock | |
8268 | * with l2arc_write_buffers (due to swapping the order | |
8269 | * the hash lock and l2ad_mtx are taken). | |
8270 | */ | |
34dc7c2f BB |
8271 | if (!mutex_tryenter(hash_lock)) { |
8272 | /* | |
ca0bf58d PS |
8273 | * Missed the hash lock. We must retry so we |
8274 | * don't leave the ARC_FLAG_L2_WRITING bit set. | |
34dc7c2f | 8275 | */ |
ca0bf58d PS |
8276 | ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); |
8277 | ||
8278 | /* | |
8279 | * We don't want to rescan the headers we've | |
8280 | * already marked as having been written out, so | |
8281 | * we reinsert the head node so we can pick up | |
8282 | * where we left off. | |
8283 | */ | |
8284 | list_remove(buflist, head); | |
8285 | list_insert_after(buflist, hdr, head); | |
8286 | ||
8287 | mutex_exit(&dev->l2ad_mtx); | |
8288 | ||
8289 | /* | |
8290 | * We wait for the hash lock to become available | |
8291 | * to try and prevent busy waiting, and increase | |
8292 | * the chance we'll be able to acquire the lock | |
8293 | * the next time around. | |
8294 | */ | |
8295 | mutex_enter(hash_lock); | |
8296 | mutex_exit(hash_lock); | |
8297 | goto top; | |
34dc7c2f BB |
8298 | } |
8299 | ||
b9541d6b | 8300 | /* |
ca0bf58d PS |
8301 | * We could not have been moved into the arc_l2c_only |
8302 | * state while in-flight due to our ARC_FLAG_L2_WRITING | |
8303 | * bit being set. Let's just ensure that's being enforced. | |
8304 | */ | |
8305 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
8306 | ||
8a09d5fd BB |
8307 | /* |
8308 | * Skipped - drop L2ARC entry and mark the header as no | |
8309 | * longer L2 eligibile. | |
8310 | */ | |
d3c2ae1c | 8311 | if (zio->io_error != 0) { |
34dc7c2f | 8312 | /* |
b128c09f | 8313 | * Error - drop L2ARC entry. |
34dc7c2f | 8314 | */ |
2a432414 | 8315 | list_remove(buflist, hdr); |
d3c2ae1c | 8316 | arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); |
b9541d6b | 8317 | |
7558997d SD |
8318 | uint64_t psize = HDR_GET_PSIZE(hdr); |
8319 | ARCSTAT_INCR(arcstat_l2_psize, -psize); | |
01850391 | 8320 | ARCSTAT_INCR(arcstat_l2_lsize, -HDR_GET_LSIZE(hdr)); |
d962d5da | 8321 | |
7558997d SD |
8322 | bytes_dropped += |
8323 | vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
424fd7c3 | 8324 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, |
d3c2ae1c | 8325 | arc_hdr_size(hdr), hdr); |
34dc7c2f BB |
8326 | } |
8327 | ||
8328 | /* | |
ca0bf58d PS |
8329 | * Allow ARC to begin reads and ghost list evictions to |
8330 | * this L2ARC entry. | |
34dc7c2f | 8331 | */ |
d3c2ae1c | 8332 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING); |
34dc7c2f BB |
8333 | |
8334 | mutex_exit(hash_lock); | |
8335 | } | |
8336 | ||
8337 | atomic_inc_64(&l2arc_writes_done); | |
8338 | list_remove(buflist, head); | |
b9541d6b CW |
8339 | ASSERT(!HDR_HAS_L1HDR(head)); |
8340 | kmem_cache_free(hdr_l2only_cache, head); | |
8341 | mutex_exit(&dev->l2ad_mtx); | |
34dc7c2f | 8342 | |
3bec585e SK |
8343 | vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); |
8344 | ||
b128c09f | 8345 | l2arc_do_free_on_write(); |
34dc7c2f BB |
8346 | |
8347 | kmem_free(cb, sizeof (l2arc_write_callback_t)); | |
8348 | } | |
8349 | ||
b5256303 TC |
8350 | static int |
8351 | l2arc_untransform(zio_t *zio, l2arc_read_callback_t *cb) | |
8352 | { | |
8353 | int ret; | |
8354 | spa_t *spa = zio->io_spa; | |
8355 | arc_buf_hdr_t *hdr = cb->l2rcb_hdr; | |
8356 | blkptr_t *bp = zio->io_bp; | |
b5256303 TC |
8357 | uint8_t salt[ZIO_DATA_SALT_LEN]; |
8358 | uint8_t iv[ZIO_DATA_IV_LEN]; | |
8359 | uint8_t mac[ZIO_DATA_MAC_LEN]; | |
8360 | boolean_t no_crypt = B_FALSE; | |
8361 | ||
8362 | /* | |
8363 | * ZIL data is never be written to the L2ARC, so we don't need | |
8364 | * special handling for its unique MAC storage. | |
8365 | */ | |
8366 | ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG); | |
8367 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); | |
440a3eb9 | 8368 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
b5256303 | 8369 | |
440a3eb9 TC |
8370 | /* |
8371 | * If the data was encrypted, decrypt it now. Note that | |
8372 | * we must check the bp here and not the hdr, since the | |
8373 | * hdr does not have its encryption parameters updated | |
8374 | * until arc_read_done(). | |
8375 | */ | |
8376 | if (BP_IS_ENCRYPTED(bp)) { | |
be9a5c35 | 8377 | abd_t *eabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr); |
b5256303 TC |
8378 | |
8379 | zio_crypt_decode_params_bp(bp, salt, iv); | |
8380 | zio_crypt_decode_mac_bp(bp, mac); | |
8381 | ||
be9a5c35 TC |
8382 | ret = spa_do_crypt_abd(B_FALSE, spa, &cb->l2rcb_zb, |
8383 | BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), | |
8384 | salt, iv, mac, HDR_GET_PSIZE(hdr), eabd, | |
8385 | hdr->b_l1hdr.b_pabd, &no_crypt); | |
b5256303 TC |
8386 | if (ret != 0) { |
8387 | arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr); | |
b5256303 TC |
8388 | goto error; |
8389 | } | |
8390 | ||
b5256303 TC |
8391 | /* |
8392 | * If we actually performed decryption, replace b_pabd | |
8393 | * with the decrypted data. Otherwise we can just throw | |
8394 | * our decryption buffer away. | |
8395 | */ | |
8396 | if (!no_crypt) { | |
8397 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
8398 | arc_hdr_size(hdr), hdr); | |
8399 | hdr->b_l1hdr.b_pabd = eabd; | |
8400 | zio->io_abd = eabd; | |
8401 | } else { | |
8402 | arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr); | |
8403 | } | |
8404 | } | |
8405 | ||
8406 | /* | |
8407 | * If the L2ARC block was compressed, but ARC compression | |
8408 | * is disabled we decompress the data into a new buffer and | |
8409 | * replace the existing data. | |
8410 | */ | |
8411 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
8412 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
8413 | abd_t *cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr); | |
8414 | void *tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr)); | |
8415 | ||
8416 | ret = zio_decompress_data(HDR_GET_COMPRESS(hdr), | |
8417 | hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr), | |
8418 | HDR_GET_LSIZE(hdr)); | |
8419 | if (ret != 0) { | |
8420 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
8421 | arc_free_data_abd(hdr, cabd, arc_hdr_size(hdr), hdr); | |
8422 | goto error; | |
8423 | } | |
8424 | ||
8425 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
8426 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
8427 | arc_hdr_size(hdr), hdr); | |
8428 | hdr->b_l1hdr.b_pabd = cabd; | |
8429 | zio->io_abd = cabd; | |
8430 | zio->io_size = HDR_GET_LSIZE(hdr); | |
8431 | } | |
8432 | ||
8433 | return (0); | |
8434 | ||
8435 | error: | |
8436 | return (ret); | |
8437 | } | |
8438 | ||
8439 | ||
34dc7c2f BB |
8440 | /* |
8441 | * A read to a cache device completed. Validate buffer contents before | |
8442 | * handing over to the regular ARC routines. | |
8443 | */ | |
8444 | static void | |
8445 | l2arc_read_done(zio_t *zio) | |
8446 | { | |
b5256303 | 8447 | int tfm_error = 0; |
b405837a | 8448 | l2arc_read_callback_t *cb = zio->io_private; |
34dc7c2f | 8449 | arc_buf_hdr_t *hdr; |
34dc7c2f | 8450 | kmutex_t *hash_lock; |
b405837a TC |
8451 | boolean_t valid_cksum; |
8452 | boolean_t using_rdata = (BP_IS_ENCRYPTED(&cb->l2rcb_bp) && | |
8453 | (cb->l2rcb_flags & ZIO_FLAG_RAW_ENCRYPT)); | |
b128c09f | 8454 | |
d3c2ae1c | 8455 | ASSERT3P(zio->io_vd, !=, NULL); |
b128c09f BB |
8456 | ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); |
8457 | ||
8458 | spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); | |
34dc7c2f | 8459 | |
d3c2ae1c GW |
8460 | ASSERT3P(cb, !=, NULL); |
8461 | hdr = cb->l2rcb_hdr; | |
8462 | ASSERT3P(hdr, !=, NULL); | |
34dc7c2f | 8463 | |
d3c2ae1c | 8464 | hash_lock = HDR_LOCK(hdr); |
34dc7c2f | 8465 | mutex_enter(hash_lock); |
428870ff | 8466 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
34dc7c2f | 8467 | |
82710e99 GDN |
8468 | /* |
8469 | * If the data was read into a temporary buffer, | |
8470 | * move it and free the buffer. | |
8471 | */ | |
8472 | if (cb->l2rcb_abd != NULL) { | |
8473 | ASSERT3U(arc_hdr_size(hdr), <, zio->io_size); | |
8474 | if (zio->io_error == 0) { | |
b405837a TC |
8475 | if (using_rdata) { |
8476 | abd_copy(hdr->b_crypt_hdr.b_rabd, | |
8477 | cb->l2rcb_abd, arc_hdr_size(hdr)); | |
8478 | } else { | |
8479 | abd_copy(hdr->b_l1hdr.b_pabd, | |
8480 | cb->l2rcb_abd, arc_hdr_size(hdr)); | |
8481 | } | |
82710e99 GDN |
8482 | } |
8483 | ||
8484 | /* | |
8485 | * The following must be done regardless of whether | |
8486 | * there was an error: | |
8487 | * - free the temporary buffer | |
8488 | * - point zio to the real ARC buffer | |
8489 | * - set zio size accordingly | |
8490 | * These are required because zio is either re-used for | |
8491 | * an I/O of the block in the case of the error | |
8492 | * or the zio is passed to arc_read_done() and it | |
8493 | * needs real data. | |
8494 | */ | |
8495 | abd_free(cb->l2rcb_abd); | |
8496 | zio->io_size = zio->io_orig_size = arc_hdr_size(hdr); | |
440a3eb9 | 8497 | |
b405837a | 8498 | if (using_rdata) { |
440a3eb9 TC |
8499 | ASSERT(HDR_HAS_RABD(hdr)); |
8500 | zio->io_abd = zio->io_orig_abd = | |
8501 | hdr->b_crypt_hdr.b_rabd; | |
8502 | } else { | |
8503 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
8504 | zio->io_abd = zio->io_orig_abd = hdr->b_l1hdr.b_pabd; | |
8505 | } | |
82710e99 GDN |
8506 | } |
8507 | ||
a6255b7f | 8508 | ASSERT3P(zio->io_abd, !=, NULL); |
3a17a7a9 | 8509 | |
34dc7c2f BB |
8510 | /* |
8511 | * Check this survived the L2ARC journey. | |
8512 | */ | |
b5256303 TC |
8513 | ASSERT(zio->io_abd == hdr->b_l1hdr.b_pabd || |
8514 | (HDR_HAS_RABD(hdr) && zio->io_abd == hdr->b_crypt_hdr.b_rabd)); | |
d3c2ae1c GW |
8515 | zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ |
8516 | zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ | |
8517 | ||
8518 | valid_cksum = arc_cksum_is_equal(hdr, zio); | |
b5256303 TC |
8519 | |
8520 | /* | |
8521 | * b_rabd will always match the data as it exists on disk if it is | |
8522 | * being used. Therefore if we are reading into b_rabd we do not | |
8523 | * attempt to untransform the data. | |
8524 | */ | |
8525 | if (valid_cksum && !using_rdata) | |
8526 | tfm_error = l2arc_untransform(zio, cb); | |
8527 | ||
8528 | if (valid_cksum && tfm_error == 0 && zio->io_error == 0 && | |
8529 | !HDR_L2_EVICTED(hdr)) { | |
34dc7c2f | 8530 | mutex_exit(hash_lock); |
d3c2ae1c | 8531 | zio->io_private = hdr; |
34dc7c2f BB |
8532 | arc_read_done(zio); |
8533 | } else { | |
8534 | mutex_exit(hash_lock); | |
8535 | /* | |
8536 | * Buffer didn't survive caching. Increment stats and | |
8537 | * reissue to the original storage device. | |
8538 | */ | |
b128c09f | 8539 | if (zio->io_error != 0) { |
34dc7c2f | 8540 | ARCSTAT_BUMP(arcstat_l2_io_error); |
b128c09f | 8541 | } else { |
2e528b49 | 8542 | zio->io_error = SET_ERROR(EIO); |
b128c09f | 8543 | } |
b5256303 | 8544 | if (!valid_cksum || tfm_error != 0) |
34dc7c2f BB |
8545 | ARCSTAT_BUMP(arcstat_l2_cksum_bad); |
8546 | ||
34dc7c2f | 8547 | /* |
b128c09f BB |
8548 | * If there's no waiter, issue an async i/o to the primary |
8549 | * storage now. If there *is* a waiter, the caller must | |
8550 | * issue the i/o in a context where it's OK to block. | |
34dc7c2f | 8551 | */ |
d164b209 BB |
8552 | if (zio->io_waiter == NULL) { |
8553 | zio_t *pio = zio_unique_parent(zio); | |
b5256303 TC |
8554 | void *abd = (using_rdata) ? |
8555 | hdr->b_crypt_hdr.b_rabd : hdr->b_l1hdr.b_pabd; | |
d164b209 BB |
8556 | |
8557 | ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); | |
8558 | ||
d3c2ae1c | 8559 | zio_nowait(zio_read(pio, zio->io_spa, zio->io_bp, |
b5256303 | 8560 | abd, zio->io_size, arc_read_done, |
d3c2ae1c GW |
8561 | hdr, zio->io_priority, cb->l2rcb_flags, |
8562 | &cb->l2rcb_zb)); | |
d164b209 | 8563 | } |
34dc7c2f BB |
8564 | } |
8565 | ||
8566 | kmem_free(cb, sizeof (l2arc_read_callback_t)); | |
8567 | } | |
8568 | ||
8569 | /* | |
8570 | * This is the list priority from which the L2ARC will search for pages to | |
8571 | * cache. This is used within loops (0..3) to cycle through lists in the | |
8572 | * desired order. This order can have a significant effect on cache | |
8573 | * performance. | |
8574 | * | |
8575 | * Currently the metadata lists are hit first, MFU then MRU, followed by | |
8576 | * the data lists. This function returns a locked list, and also returns | |
8577 | * the lock pointer. | |
8578 | */ | |
ca0bf58d PS |
8579 | static multilist_sublist_t * |
8580 | l2arc_sublist_lock(int list_num) | |
34dc7c2f | 8581 | { |
ca0bf58d PS |
8582 | multilist_t *ml = NULL; |
8583 | unsigned int idx; | |
34dc7c2f | 8584 | |
4aafab91 | 8585 | ASSERT(list_num >= 0 && list_num < L2ARC_FEED_TYPES); |
34dc7c2f BB |
8586 | |
8587 | switch (list_num) { | |
8588 | case 0: | |
64fc7762 | 8589 | ml = arc_mfu->arcs_list[ARC_BUFC_METADATA]; |
34dc7c2f BB |
8590 | break; |
8591 | case 1: | |
64fc7762 | 8592 | ml = arc_mru->arcs_list[ARC_BUFC_METADATA]; |
34dc7c2f BB |
8593 | break; |
8594 | case 2: | |
64fc7762 | 8595 | ml = arc_mfu->arcs_list[ARC_BUFC_DATA]; |
34dc7c2f BB |
8596 | break; |
8597 | case 3: | |
64fc7762 | 8598 | ml = arc_mru->arcs_list[ARC_BUFC_DATA]; |
34dc7c2f | 8599 | break; |
4aafab91 G |
8600 | default: |
8601 | return (NULL); | |
34dc7c2f BB |
8602 | } |
8603 | ||
ca0bf58d PS |
8604 | /* |
8605 | * Return a randomly-selected sublist. This is acceptable | |
8606 | * because the caller feeds only a little bit of data for each | |
8607 | * call (8MB). Subsequent calls will result in different | |
8608 | * sublists being selected. | |
8609 | */ | |
8610 | idx = multilist_get_random_index(ml); | |
8611 | return (multilist_sublist_lock(ml, idx)); | |
34dc7c2f BB |
8612 | } |
8613 | ||
8614 | /* | |
8615 | * Evict buffers from the device write hand to the distance specified in | |
8616 | * bytes. This distance may span populated buffers, it may span nothing. | |
8617 | * This is clearing a region on the L2ARC device ready for writing. | |
8618 | * If the 'all' boolean is set, every buffer is evicted. | |
8619 | */ | |
8620 | static void | |
8621 | l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) | |
8622 | { | |
8623 | list_t *buflist; | |
2a432414 | 8624 | arc_buf_hdr_t *hdr, *hdr_prev; |
34dc7c2f BB |
8625 | kmutex_t *hash_lock; |
8626 | uint64_t taddr; | |
8627 | ||
b9541d6b | 8628 | buflist = &dev->l2ad_buflist; |
34dc7c2f BB |
8629 | |
8630 | if (!all && dev->l2ad_first) { | |
8631 | /* | |
8632 | * This is the first sweep through the device. There is | |
8633 | * nothing to evict. | |
8634 | */ | |
8635 | return; | |
8636 | } | |
8637 | ||
b128c09f | 8638 | if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { |
34dc7c2f BB |
8639 | /* |
8640 | * When nearing the end of the device, evict to the end | |
8641 | * before the device write hand jumps to the start. | |
8642 | */ | |
8643 | taddr = dev->l2ad_end; | |
8644 | } else { | |
8645 | taddr = dev->l2ad_hand + distance; | |
8646 | } | |
8647 | DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, | |
8648 | uint64_t, taddr, boolean_t, all); | |
8649 | ||
8650 | top: | |
b9541d6b | 8651 | mutex_enter(&dev->l2ad_mtx); |
2a432414 GW |
8652 | for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { |
8653 | hdr_prev = list_prev(buflist, hdr); | |
34dc7c2f | 8654 | |
2a432414 | 8655 | hash_lock = HDR_LOCK(hdr); |
ca0bf58d PS |
8656 | |
8657 | /* | |
8658 | * We cannot use mutex_enter or else we can deadlock | |
8659 | * with l2arc_write_buffers (due to swapping the order | |
8660 | * the hash lock and l2ad_mtx are taken). | |
8661 | */ | |
34dc7c2f BB |
8662 | if (!mutex_tryenter(hash_lock)) { |
8663 | /* | |
8664 | * Missed the hash lock. Retry. | |
8665 | */ | |
8666 | ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); | |
b9541d6b | 8667 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
8668 | mutex_enter(hash_lock); |
8669 | mutex_exit(hash_lock); | |
8670 | goto top; | |
8671 | } | |
8672 | ||
f06f53fa AG |
8673 | /* |
8674 | * A header can't be on this list if it doesn't have L2 header. | |
8675 | */ | |
8676 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
34dc7c2f | 8677 | |
f06f53fa AG |
8678 | /* Ensure this header has finished being written. */ |
8679 | ASSERT(!HDR_L2_WRITING(hdr)); | |
8680 | ASSERT(!HDR_L2_WRITE_HEAD(hdr)); | |
8681 | ||
8682 | if (!all && (hdr->b_l2hdr.b_daddr >= taddr || | |
b9541d6b | 8683 | hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { |
34dc7c2f BB |
8684 | /* |
8685 | * We've evicted to the target address, | |
8686 | * or the end of the device. | |
8687 | */ | |
8688 | mutex_exit(hash_lock); | |
8689 | break; | |
8690 | } | |
8691 | ||
b9541d6b | 8692 | if (!HDR_HAS_L1HDR(hdr)) { |
2a432414 | 8693 | ASSERT(!HDR_L2_READING(hdr)); |
34dc7c2f BB |
8694 | /* |
8695 | * This doesn't exist in the ARC. Destroy. | |
8696 | * arc_hdr_destroy() will call list_remove() | |
01850391 | 8697 | * and decrement arcstat_l2_lsize. |
34dc7c2f | 8698 | */ |
2a432414 GW |
8699 | arc_change_state(arc_anon, hdr, hash_lock); |
8700 | arc_hdr_destroy(hdr); | |
34dc7c2f | 8701 | } else { |
b9541d6b CW |
8702 | ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); |
8703 | ARCSTAT_BUMP(arcstat_l2_evict_l1cached); | |
b128c09f BB |
8704 | /* |
8705 | * Invalidate issued or about to be issued | |
8706 | * reads, since we may be about to write | |
8707 | * over this location. | |
8708 | */ | |
2a432414 | 8709 | if (HDR_L2_READING(hdr)) { |
b128c09f | 8710 | ARCSTAT_BUMP(arcstat_l2_evict_reading); |
d3c2ae1c | 8711 | arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED); |
b128c09f BB |
8712 | } |
8713 | ||
d962d5da | 8714 | arc_hdr_l2hdr_destroy(hdr); |
34dc7c2f BB |
8715 | } |
8716 | mutex_exit(hash_lock); | |
8717 | } | |
b9541d6b | 8718 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
8719 | } |
8720 | ||
b5256303 TC |
8721 | /* |
8722 | * Handle any abd transforms that might be required for writing to the L2ARC. | |
8723 | * If successful, this function will always return an abd with the data | |
8724 | * transformed as it is on disk in a new abd of asize bytes. | |
8725 | */ | |
8726 | static int | |
8727 | l2arc_apply_transforms(spa_t *spa, arc_buf_hdr_t *hdr, uint64_t asize, | |
8728 | abd_t **abd_out) | |
8729 | { | |
8730 | int ret; | |
8731 | void *tmp = NULL; | |
8732 | abd_t *cabd = NULL, *eabd = NULL, *to_write = hdr->b_l1hdr.b_pabd; | |
8733 | enum zio_compress compress = HDR_GET_COMPRESS(hdr); | |
8734 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
8735 | uint64_t size = arc_hdr_size(hdr); | |
8736 | boolean_t ismd = HDR_ISTYPE_METADATA(hdr); | |
8737 | boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
8738 | dsl_crypto_key_t *dck = NULL; | |
8739 | uint8_t mac[ZIO_DATA_MAC_LEN] = { 0 }; | |
4807c0ba | 8740 | boolean_t no_crypt = B_FALSE; |
b5256303 TC |
8741 | |
8742 | ASSERT((HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
8743 | !HDR_COMPRESSION_ENABLED(hdr)) || | |
8744 | HDR_ENCRYPTED(hdr) || HDR_SHARED_DATA(hdr) || psize != asize); | |
8745 | ASSERT3U(psize, <=, asize); | |
8746 | ||
8747 | /* | |
8748 | * If this data simply needs its own buffer, we simply allocate it | |
8749 | * and copy the data. This may be done to elimiate a depedency on a | |
8750 | * shared buffer or to reallocate the buffer to match asize. | |
8751 | */ | |
4807c0ba | 8752 | if (HDR_HAS_RABD(hdr) && asize != psize) { |
10adee27 | 8753 | ASSERT3U(asize, >=, psize); |
4807c0ba | 8754 | to_write = abd_alloc_for_io(asize, ismd); |
10adee27 TC |
8755 | abd_copy(to_write, hdr->b_crypt_hdr.b_rabd, psize); |
8756 | if (psize != asize) | |
8757 | abd_zero_off(to_write, psize, asize - psize); | |
4807c0ba TC |
8758 | goto out; |
8759 | } | |
8760 | ||
b5256303 TC |
8761 | if ((compress == ZIO_COMPRESS_OFF || HDR_COMPRESSION_ENABLED(hdr)) && |
8762 | !HDR_ENCRYPTED(hdr)) { | |
8763 | ASSERT3U(size, ==, psize); | |
8764 | to_write = abd_alloc_for_io(asize, ismd); | |
8765 | abd_copy(to_write, hdr->b_l1hdr.b_pabd, size); | |
8766 | if (size != asize) | |
8767 | abd_zero_off(to_write, size, asize - size); | |
8768 | goto out; | |
8769 | } | |
8770 | ||
8771 | if (compress != ZIO_COMPRESS_OFF && !HDR_COMPRESSION_ENABLED(hdr)) { | |
8772 | cabd = abd_alloc_for_io(asize, ismd); | |
8773 | tmp = abd_borrow_buf(cabd, asize); | |
8774 | ||
8775 | psize = zio_compress_data(compress, to_write, tmp, size); | |
8776 | ASSERT3U(psize, <=, HDR_GET_PSIZE(hdr)); | |
8777 | if (psize < asize) | |
8778 | bzero((char *)tmp + psize, asize - psize); | |
8779 | psize = HDR_GET_PSIZE(hdr); | |
8780 | abd_return_buf_copy(cabd, tmp, asize); | |
8781 | to_write = cabd; | |
8782 | } | |
8783 | ||
8784 | if (HDR_ENCRYPTED(hdr)) { | |
8785 | eabd = abd_alloc_for_io(asize, ismd); | |
8786 | ||
8787 | /* | |
8788 | * If the dataset was disowned before the buffer | |
8789 | * made it to this point, the key to re-encrypt | |
8790 | * it won't be available. In this case we simply | |
8791 | * won't write the buffer to the L2ARC. | |
8792 | */ | |
8793 | ret = spa_keystore_lookup_key(spa, hdr->b_crypt_hdr.b_dsobj, | |
8794 | FTAG, &dck); | |
8795 | if (ret != 0) | |
8796 | goto error; | |
8797 | ||
8798 | ret = zio_do_crypt_abd(B_TRUE, &dck->dck_key, | |
be9a5c35 TC |
8799 | hdr->b_crypt_hdr.b_ot, bswap, hdr->b_crypt_hdr.b_salt, |
8800 | hdr->b_crypt_hdr.b_iv, mac, psize, to_write, eabd, | |
8801 | &no_crypt); | |
b5256303 TC |
8802 | if (ret != 0) |
8803 | goto error; | |
8804 | ||
4807c0ba TC |
8805 | if (no_crypt) |
8806 | abd_copy(eabd, to_write, psize); | |
b5256303 TC |
8807 | |
8808 | if (psize != asize) | |
8809 | abd_zero_off(eabd, psize, asize - psize); | |
8810 | ||
8811 | /* assert that the MAC we got here matches the one we saved */ | |
8812 | ASSERT0(bcmp(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN)); | |
8813 | spa_keystore_dsl_key_rele(spa, dck, FTAG); | |
8814 | ||
8815 | if (to_write == cabd) | |
8816 | abd_free(cabd); | |
8817 | ||
8818 | to_write = eabd; | |
8819 | } | |
8820 | ||
8821 | out: | |
8822 | ASSERT3P(to_write, !=, hdr->b_l1hdr.b_pabd); | |
8823 | *abd_out = to_write; | |
8824 | return (0); | |
8825 | ||
8826 | error: | |
8827 | if (dck != NULL) | |
8828 | spa_keystore_dsl_key_rele(spa, dck, FTAG); | |
8829 | if (cabd != NULL) | |
8830 | abd_free(cabd); | |
8831 | if (eabd != NULL) | |
8832 | abd_free(eabd); | |
8833 | ||
8834 | *abd_out = NULL; | |
8835 | return (ret); | |
8836 | } | |
8837 | ||
34dc7c2f BB |
8838 | /* |
8839 | * Find and write ARC buffers to the L2ARC device. | |
8840 | * | |
2a432414 | 8841 | * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid |
34dc7c2f | 8842 | * for reading until they have completed writing. |
3a17a7a9 SK |
8843 | * The headroom_boost is an in-out parameter used to maintain headroom boost |
8844 | * state between calls to this function. | |
8845 | * | |
8846 | * Returns the number of bytes actually written (which may be smaller than | |
8847 | * the delta by which the device hand has changed due to alignment). | |
34dc7c2f | 8848 | */ |
d164b209 | 8849 | static uint64_t |
d3c2ae1c | 8850 | l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) |
34dc7c2f | 8851 | { |
2a432414 | 8852 | arc_buf_hdr_t *hdr, *hdr_prev, *head; |
01850391 | 8853 | uint64_t write_asize, write_psize, write_lsize, headroom; |
3a17a7a9 | 8854 | boolean_t full; |
34dc7c2f BB |
8855 | l2arc_write_callback_t *cb; |
8856 | zio_t *pio, *wzio; | |
3541dc6d | 8857 | uint64_t guid = spa_load_guid(spa); |
34dc7c2f | 8858 | |
d3c2ae1c | 8859 | ASSERT3P(dev->l2ad_vdev, !=, NULL); |
3a17a7a9 | 8860 | |
34dc7c2f | 8861 | pio = NULL; |
01850391 | 8862 | write_lsize = write_asize = write_psize = 0; |
34dc7c2f | 8863 | full = B_FALSE; |
b9541d6b | 8864 | head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); |
d3c2ae1c | 8865 | arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR); |
3a17a7a9 | 8866 | |
34dc7c2f BB |
8867 | /* |
8868 | * Copy buffers for L2ARC writing. | |
8869 | */ | |
1c27024e | 8870 | for (int try = 0; try < L2ARC_FEED_TYPES; try++) { |
ca0bf58d | 8871 | multilist_sublist_t *mls = l2arc_sublist_lock(try); |
3a17a7a9 SK |
8872 | uint64_t passed_sz = 0; |
8873 | ||
4aafab91 G |
8874 | VERIFY3P(mls, !=, NULL); |
8875 | ||
b128c09f BB |
8876 | /* |
8877 | * L2ARC fast warmup. | |
8878 | * | |
8879 | * Until the ARC is warm and starts to evict, read from the | |
8880 | * head of the ARC lists rather than the tail. | |
8881 | */ | |
b128c09f | 8882 | if (arc_warm == B_FALSE) |
ca0bf58d | 8883 | hdr = multilist_sublist_head(mls); |
b128c09f | 8884 | else |
ca0bf58d | 8885 | hdr = multilist_sublist_tail(mls); |
b128c09f | 8886 | |
3a17a7a9 | 8887 | headroom = target_sz * l2arc_headroom; |
d3c2ae1c | 8888 | if (zfs_compressed_arc_enabled) |
3a17a7a9 SK |
8889 | headroom = (headroom * l2arc_headroom_boost) / 100; |
8890 | ||
2a432414 | 8891 | for (; hdr; hdr = hdr_prev) { |
3a17a7a9 | 8892 | kmutex_t *hash_lock; |
b5256303 | 8893 | abd_t *to_write = NULL; |
3a17a7a9 | 8894 | |
b128c09f | 8895 | if (arc_warm == B_FALSE) |
ca0bf58d | 8896 | hdr_prev = multilist_sublist_next(mls, hdr); |
b128c09f | 8897 | else |
ca0bf58d | 8898 | hdr_prev = multilist_sublist_prev(mls, hdr); |
34dc7c2f | 8899 | |
2a432414 | 8900 | hash_lock = HDR_LOCK(hdr); |
3a17a7a9 | 8901 | if (!mutex_tryenter(hash_lock)) { |
34dc7c2f BB |
8902 | /* |
8903 | * Skip this buffer rather than waiting. | |
8904 | */ | |
8905 | continue; | |
8906 | } | |
8907 | ||
d3c2ae1c | 8908 | passed_sz += HDR_GET_LSIZE(hdr); |
34dc7c2f BB |
8909 | if (passed_sz > headroom) { |
8910 | /* | |
8911 | * Searched too far. | |
8912 | */ | |
8913 | mutex_exit(hash_lock); | |
8914 | break; | |
8915 | } | |
8916 | ||
2a432414 | 8917 | if (!l2arc_write_eligible(guid, hdr)) { |
34dc7c2f BB |
8918 | mutex_exit(hash_lock); |
8919 | continue; | |
8920 | } | |
8921 | ||
01850391 AG |
8922 | /* |
8923 | * We rely on the L1 portion of the header below, so | |
8924 | * it's invalid for this header to have been evicted out | |
8925 | * of the ghost cache, prior to being written out. The | |
8926 | * ARC_FLAG_L2_WRITING bit ensures this won't happen. | |
8927 | */ | |
8928 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
8929 | ||
8930 | ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); | |
01850391 | 8931 | ASSERT3U(arc_hdr_size(hdr), >, 0); |
b5256303 TC |
8932 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || |
8933 | HDR_HAS_RABD(hdr)); | |
8934 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
01850391 AG |
8935 | uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, |
8936 | psize); | |
8937 | ||
8938 | if ((write_asize + asize) > target_sz) { | |
34dc7c2f BB |
8939 | full = B_TRUE; |
8940 | mutex_exit(hash_lock); | |
8941 | break; | |
8942 | } | |
8943 | ||
b5256303 TC |
8944 | /* |
8945 | * We rely on the L1 portion of the header below, so | |
8946 | * it's invalid for this header to have been evicted out | |
8947 | * of the ghost cache, prior to being written out. The | |
8948 | * ARC_FLAG_L2_WRITING bit ensures this won't happen. | |
8949 | */ | |
8950 | arc_hdr_set_flags(hdr, ARC_FLAG_L2_WRITING); | |
8951 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
8952 | ||
8953 | ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); | |
8954 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || | |
8955 | HDR_HAS_RABD(hdr)); | |
8956 | ASSERT3U(arc_hdr_size(hdr), >, 0); | |
8957 | ||
8958 | /* | |
8959 | * If this header has b_rabd, we can use this since it | |
8960 | * must always match the data exactly as it exists on | |
8961 | * disk. Otherwise, the L2ARC can normally use the | |
8962 | * hdr's data, but if we're sharing data between the | |
8963 | * hdr and one of its bufs, L2ARC needs its own copy of | |
8964 | * the data so that the ZIO below can't race with the | |
8965 | * buf consumer. To ensure that this copy will be | |
8966 | * available for the lifetime of the ZIO and be cleaned | |
8967 | * up afterwards, we add it to the l2arc_free_on_write | |
8968 | * queue. If we need to apply any transforms to the | |
8969 | * data (compression, encryption) we will also need the | |
8970 | * extra buffer. | |
8971 | */ | |
8972 | if (HDR_HAS_RABD(hdr) && psize == asize) { | |
8973 | to_write = hdr->b_crypt_hdr.b_rabd; | |
8974 | } else if ((HDR_COMPRESSION_ENABLED(hdr) || | |
8975 | HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) && | |
8976 | !HDR_ENCRYPTED(hdr) && !HDR_SHARED_DATA(hdr) && | |
8977 | psize == asize) { | |
8978 | to_write = hdr->b_l1hdr.b_pabd; | |
8979 | } else { | |
8980 | int ret; | |
8981 | arc_buf_contents_t type = arc_buf_type(hdr); | |
8982 | ||
8983 | ret = l2arc_apply_transforms(spa, hdr, asize, | |
8984 | &to_write); | |
8985 | if (ret != 0) { | |
8986 | arc_hdr_clear_flags(hdr, | |
8987 | ARC_FLAG_L2_WRITING); | |
8988 | mutex_exit(hash_lock); | |
8989 | continue; | |
8990 | } | |
8991 | ||
8992 | l2arc_free_abd_on_write(to_write, asize, type); | |
8993 | } | |
8994 | ||
34dc7c2f BB |
8995 | if (pio == NULL) { |
8996 | /* | |
8997 | * Insert a dummy header on the buflist so | |
8998 | * l2arc_write_done() can find where the | |
8999 | * write buffers begin without searching. | |
9000 | */ | |
ca0bf58d | 9001 | mutex_enter(&dev->l2ad_mtx); |
b9541d6b | 9002 | list_insert_head(&dev->l2ad_buflist, head); |
ca0bf58d | 9003 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 9004 | |
96c080cb BB |
9005 | cb = kmem_alloc( |
9006 | sizeof (l2arc_write_callback_t), KM_SLEEP); | |
34dc7c2f BB |
9007 | cb->l2wcb_dev = dev; |
9008 | cb->l2wcb_head = head; | |
9009 | pio = zio_root(spa, l2arc_write_done, cb, | |
9010 | ZIO_FLAG_CANFAIL); | |
9011 | } | |
9012 | ||
b9541d6b | 9013 | hdr->b_l2hdr.b_dev = dev; |
b9541d6b | 9014 | hdr->b_l2hdr.b_hits = 0; |
3a17a7a9 | 9015 | |
d3c2ae1c | 9016 | hdr->b_l2hdr.b_daddr = dev->l2ad_hand; |
b5256303 | 9017 | arc_hdr_set_flags(hdr, ARC_FLAG_HAS_L2HDR); |
3a17a7a9 | 9018 | |
ca0bf58d | 9019 | mutex_enter(&dev->l2ad_mtx); |
b9541d6b | 9020 | list_insert_head(&dev->l2ad_buflist, hdr); |
ca0bf58d | 9021 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 9022 | |
424fd7c3 | 9023 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, |
b5256303 | 9024 | arc_hdr_size(hdr), hdr); |
3a17a7a9 | 9025 | |
34dc7c2f | 9026 | wzio = zio_write_phys(pio, dev->l2ad_vdev, |
82710e99 | 9027 | hdr->b_l2hdr.b_daddr, asize, to_write, |
d3c2ae1c GW |
9028 | ZIO_CHECKSUM_OFF, NULL, hdr, |
9029 | ZIO_PRIORITY_ASYNC_WRITE, | |
34dc7c2f BB |
9030 | ZIO_FLAG_CANFAIL, B_FALSE); |
9031 | ||
01850391 | 9032 | write_lsize += HDR_GET_LSIZE(hdr); |
34dc7c2f BB |
9033 | DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, |
9034 | zio_t *, wzio); | |
d962d5da | 9035 | |
01850391 AG |
9036 | write_psize += psize; |
9037 | write_asize += asize; | |
d3c2ae1c | 9038 | dev->l2ad_hand += asize; |
7558997d | 9039 | vdev_space_update(dev->l2ad_vdev, asize, 0, 0); |
d3c2ae1c GW |
9040 | |
9041 | mutex_exit(hash_lock); | |
9042 | ||
9043 | (void) zio_nowait(wzio); | |
34dc7c2f | 9044 | } |
d3c2ae1c GW |
9045 | |
9046 | multilist_sublist_unlock(mls); | |
9047 | ||
9048 | if (full == B_TRUE) | |
9049 | break; | |
34dc7c2f | 9050 | } |
34dc7c2f | 9051 | |
d3c2ae1c GW |
9052 | /* No buffers selected for writing? */ |
9053 | if (pio == NULL) { | |
01850391 | 9054 | ASSERT0(write_lsize); |
d3c2ae1c GW |
9055 | ASSERT(!HDR_HAS_L1HDR(head)); |
9056 | kmem_cache_free(hdr_l2only_cache, head); | |
9057 | return (0); | |
9058 | } | |
34dc7c2f | 9059 | |
3a17a7a9 | 9060 | ASSERT3U(write_asize, <=, target_sz); |
34dc7c2f | 9061 | ARCSTAT_BUMP(arcstat_l2_writes_sent); |
01850391 AG |
9062 | ARCSTAT_INCR(arcstat_l2_write_bytes, write_psize); |
9063 | ARCSTAT_INCR(arcstat_l2_lsize, write_lsize); | |
9064 | ARCSTAT_INCR(arcstat_l2_psize, write_psize); | |
34dc7c2f BB |
9065 | |
9066 | /* | |
9067 | * Bump device hand to the device start if it is approaching the end. | |
9068 | * l2arc_evict() will already have evicted ahead for this case. | |
9069 | */ | |
b128c09f | 9070 | if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { |
34dc7c2f | 9071 | dev->l2ad_hand = dev->l2ad_start; |
34dc7c2f BB |
9072 | dev->l2ad_first = B_FALSE; |
9073 | } | |
9074 | ||
d164b209 | 9075 | dev->l2ad_writing = B_TRUE; |
34dc7c2f | 9076 | (void) zio_wait(pio); |
d164b209 BB |
9077 | dev->l2ad_writing = B_FALSE; |
9078 | ||
3a17a7a9 SK |
9079 | return (write_asize); |
9080 | } | |
9081 | ||
34dc7c2f BB |
9082 | /* |
9083 | * This thread feeds the L2ARC at regular intervals. This is the beating | |
9084 | * heart of the L2ARC. | |
9085 | */ | |
867959b5 | 9086 | /* ARGSUSED */ |
34dc7c2f | 9087 | static void |
c25b8f99 | 9088 | l2arc_feed_thread(void *unused) |
34dc7c2f BB |
9089 | { |
9090 | callb_cpr_t cpr; | |
9091 | l2arc_dev_t *dev; | |
9092 | spa_t *spa; | |
d164b209 | 9093 | uint64_t size, wrote; |
428870ff | 9094 | clock_t begin, next = ddi_get_lbolt(); |
40d06e3c | 9095 | fstrans_cookie_t cookie; |
34dc7c2f BB |
9096 | |
9097 | CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); | |
9098 | ||
9099 | mutex_enter(&l2arc_feed_thr_lock); | |
9100 | ||
40d06e3c | 9101 | cookie = spl_fstrans_mark(); |
34dc7c2f | 9102 | while (l2arc_thread_exit == 0) { |
34dc7c2f | 9103 | CALLB_CPR_SAFE_BEGIN(&cpr); |
b64ccd6c | 9104 | (void) cv_timedwait_sig(&l2arc_feed_thr_cv, |
5b63b3eb | 9105 | &l2arc_feed_thr_lock, next); |
34dc7c2f | 9106 | CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); |
428870ff | 9107 | next = ddi_get_lbolt() + hz; |
34dc7c2f BB |
9108 | |
9109 | /* | |
b128c09f | 9110 | * Quick check for L2ARC devices. |
34dc7c2f BB |
9111 | */ |
9112 | mutex_enter(&l2arc_dev_mtx); | |
9113 | if (l2arc_ndev == 0) { | |
9114 | mutex_exit(&l2arc_dev_mtx); | |
9115 | continue; | |
9116 | } | |
b128c09f | 9117 | mutex_exit(&l2arc_dev_mtx); |
428870ff | 9118 | begin = ddi_get_lbolt(); |
34dc7c2f BB |
9119 | |
9120 | /* | |
b128c09f BB |
9121 | * This selects the next l2arc device to write to, and in |
9122 | * doing so the next spa to feed from: dev->l2ad_spa. This | |
9123 | * will return NULL if there are now no l2arc devices or if | |
9124 | * they are all faulted. | |
9125 | * | |
9126 | * If a device is returned, its spa's config lock is also | |
9127 | * held to prevent device removal. l2arc_dev_get_next() | |
9128 | * will grab and release l2arc_dev_mtx. | |
34dc7c2f | 9129 | */ |
b128c09f | 9130 | if ((dev = l2arc_dev_get_next()) == NULL) |
34dc7c2f | 9131 | continue; |
b128c09f BB |
9132 | |
9133 | spa = dev->l2ad_spa; | |
d3c2ae1c | 9134 | ASSERT3P(spa, !=, NULL); |
34dc7c2f | 9135 | |
572e2857 BB |
9136 | /* |
9137 | * If the pool is read-only then force the feed thread to | |
9138 | * sleep a little longer. | |
9139 | */ | |
9140 | if (!spa_writeable(spa)) { | |
9141 | next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; | |
9142 | spa_config_exit(spa, SCL_L2ARC, dev); | |
9143 | continue; | |
9144 | } | |
9145 | ||
34dc7c2f | 9146 | /* |
b128c09f | 9147 | * Avoid contributing to memory pressure. |
34dc7c2f | 9148 | */ |
ca67b33a | 9149 | if (arc_reclaim_needed()) { |
b128c09f BB |
9150 | ARCSTAT_BUMP(arcstat_l2_abort_lowmem); |
9151 | spa_config_exit(spa, SCL_L2ARC, dev); | |
34dc7c2f BB |
9152 | continue; |
9153 | } | |
b128c09f | 9154 | |
34dc7c2f BB |
9155 | ARCSTAT_BUMP(arcstat_l2_feeds); |
9156 | ||
3a17a7a9 | 9157 | size = l2arc_write_size(); |
b128c09f | 9158 | |
34dc7c2f BB |
9159 | /* |
9160 | * Evict L2ARC buffers that will be overwritten. | |
9161 | */ | |
b128c09f | 9162 | l2arc_evict(dev, size, B_FALSE); |
34dc7c2f BB |
9163 | |
9164 | /* | |
9165 | * Write ARC buffers. | |
9166 | */ | |
d3c2ae1c | 9167 | wrote = l2arc_write_buffers(spa, dev, size); |
d164b209 BB |
9168 | |
9169 | /* | |
9170 | * Calculate interval between writes. | |
9171 | */ | |
9172 | next = l2arc_write_interval(begin, size, wrote); | |
b128c09f | 9173 | spa_config_exit(spa, SCL_L2ARC, dev); |
34dc7c2f | 9174 | } |
40d06e3c | 9175 | spl_fstrans_unmark(cookie); |
34dc7c2f BB |
9176 | |
9177 | l2arc_thread_exit = 0; | |
9178 | cv_broadcast(&l2arc_feed_thr_cv); | |
9179 | CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ | |
9180 | thread_exit(); | |
9181 | } | |
9182 | ||
b128c09f BB |
9183 | boolean_t |
9184 | l2arc_vdev_present(vdev_t *vd) | |
9185 | { | |
9186 | l2arc_dev_t *dev; | |
9187 | ||
9188 | mutex_enter(&l2arc_dev_mtx); | |
9189 | for (dev = list_head(l2arc_dev_list); dev != NULL; | |
9190 | dev = list_next(l2arc_dev_list, dev)) { | |
9191 | if (dev->l2ad_vdev == vd) | |
9192 | break; | |
9193 | } | |
9194 | mutex_exit(&l2arc_dev_mtx); | |
9195 | ||
9196 | return (dev != NULL); | |
9197 | } | |
9198 | ||
34dc7c2f BB |
9199 | /* |
9200 | * Add a vdev for use by the L2ARC. By this point the spa has already | |
9201 | * validated the vdev and opened it. | |
9202 | */ | |
9203 | void | |
9babb374 | 9204 | l2arc_add_vdev(spa_t *spa, vdev_t *vd) |
34dc7c2f BB |
9205 | { |
9206 | l2arc_dev_t *adddev; | |
9207 | ||
b128c09f BB |
9208 | ASSERT(!l2arc_vdev_present(vd)); |
9209 | ||
34dc7c2f BB |
9210 | /* |
9211 | * Create a new l2arc device entry. | |
9212 | */ | |
9213 | adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); | |
9214 | adddev->l2ad_spa = spa; | |
9215 | adddev->l2ad_vdev = vd; | |
9babb374 BB |
9216 | adddev->l2ad_start = VDEV_LABEL_START_SIZE; |
9217 | adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); | |
34dc7c2f | 9218 | adddev->l2ad_hand = adddev->l2ad_start; |
34dc7c2f | 9219 | adddev->l2ad_first = B_TRUE; |
d164b209 | 9220 | adddev->l2ad_writing = B_FALSE; |
98f72a53 | 9221 | list_link_init(&adddev->l2ad_node); |
34dc7c2f | 9222 | |
b9541d6b | 9223 | mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
9224 | /* |
9225 | * This is a list of all ARC buffers that are still valid on the | |
9226 | * device. | |
9227 | */ | |
b9541d6b CW |
9228 | list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), |
9229 | offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); | |
34dc7c2f | 9230 | |
428870ff | 9231 | vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); |
424fd7c3 | 9232 | zfs_refcount_create(&adddev->l2ad_alloc); |
34dc7c2f BB |
9233 | |
9234 | /* | |
9235 | * Add device to global list | |
9236 | */ | |
9237 | mutex_enter(&l2arc_dev_mtx); | |
9238 | list_insert_head(l2arc_dev_list, adddev); | |
9239 | atomic_inc_64(&l2arc_ndev); | |
9240 | mutex_exit(&l2arc_dev_mtx); | |
9241 | } | |
9242 | ||
9243 | /* | |
9244 | * Remove a vdev from the L2ARC. | |
9245 | */ | |
9246 | void | |
9247 | l2arc_remove_vdev(vdev_t *vd) | |
9248 | { | |
9249 | l2arc_dev_t *dev, *nextdev, *remdev = NULL; | |
9250 | ||
34dc7c2f BB |
9251 | /* |
9252 | * Find the device by vdev | |
9253 | */ | |
9254 | mutex_enter(&l2arc_dev_mtx); | |
9255 | for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { | |
9256 | nextdev = list_next(l2arc_dev_list, dev); | |
9257 | if (vd == dev->l2ad_vdev) { | |
9258 | remdev = dev; | |
9259 | break; | |
9260 | } | |
9261 | } | |
d3c2ae1c | 9262 | ASSERT3P(remdev, !=, NULL); |
34dc7c2f BB |
9263 | |
9264 | /* | |
9265 | * Remove device from global list | |
9266 | */ | |
9267 | list_remove(l2arc_dev_list, remdev); | |
9268 | l2arc_dev_last = NULL; /* may have been invalidated */ | |
b128c09f BB |
9269 | atomic_dec_64(&l2arc_ndev); |
9270 | mutex_exit(&l2arc_dev_mtx); | |
34dc7c2f BB |
9271 | |
9272 | /* | |
9273 | * Clear all buflists and ARC references. L2ARC device flush. | |
9274 | */ | |
9275 | l2arc_evict(remdev, 0, B_TRUE); | |
b9541d6b CW |
9276 | list_destroy(&remdev->l2ad_buflist); |
9277 | mutex_destroy(&remdev->l2ad_mtx); | |
424fd7c3 | 9278 | zfs_refcount_destroy(&remdev->l2ad_alloc); |
34dc7c2f | 9279 | kmem_free(remdev, sizeof (l2arc_dev_t)); |
34dc7c2f BB |
9280 | } |
9281 | ||
9282 | void | |
b128c09f | 9283 | l2arc_init(void) |
34dc7c2f BB |
9284 | { |
9285 | l2arc_thread_exit = 0; | |
9286 | l2arc_ndev = 0; | |
9287 | l2arc_writes_sent = 0; | |
9288 | l2arc_writes_done = 0; | |
9289 | ||
9290 | mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); | |
9291 | cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); | |
9292 | mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); | |
34dc7c2f BB |
9293 | mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); |
9294 | ||
9295 | l2arc_dev_list = &L2ARC_dev_list; | |
9296 | l2arc_free_on_write = &L2ARC_free_on_write; | |
9297 | list_create(l2arc_dev_list, sizeof (l2arc_dev_t), | |
9298 | offsetof(l2arc_dev_t, l2ad_node)); | |
9299 | list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), | |
9300 | offsetof(l2arc_data_free_t, l2df_list_node)); | |
34dc7c2f BB |
9301 | } |
9302 | ||
9303 | void | |
b128c09f | 9304 | l2arc_fini(void) |
34dc7c2f | 9305 | { |
b128c09f BB |
9306 | /* |
9307 | * This is called from dmu_fini(), which is called from spa_fini(); | |
9308 | * Because of this, we can assume that all l2arc devices have | |
9309 | * already been removed when the pools themselves were removed. | |
9310 | */ | |
9311 | ||
9312 | l2arc_do_free_on_write(); | |
34dc7c2f BB |
9313 | |
9314 | mutex_destroy(&l2arc_feed_thr_lock); | |
9315 | cv_destroy(&l2arc_feed_thr_cv); | |
9316 | mutex_destroy(&l2arc_dev_mtx); | |
34dc7c2f BB |
9317 | mutex_destroy(&l2arc_free_on_write_mtx); |
9318 | ||
9319 | list_destroy(l2arc_dev_list); | |
9320 | list_destroy(l2arc_free_on_write); | |
9321 | } | |
b128c09f BB |
9322 | |
9323 | void | |
9324 | l2arc_start(void) | |
9325 | { | |
fb5f0bc8 | 9326 | if (!(spa_mode_global & FWRITE)) |
b128c09f BB |
9327 | return; |
9328 | ||
9329 | (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, | |
1229323d | 9330 | TS_RUN, defclsyspri); |
b128c09f BB |
9331 | } |
9332 | ||
9333 | void | |
9334 | l2arc_stop(void) | |
9335 | { | |
fb5f0bc8 | 9336 | if (!(spa_mode_global & FWRITE)) |
b128c09f BB |
9337 | return; |
9338 | ||
9339 | mutex_enter(&l2arc_feed_thr_lock); | |
9340 | cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ | |
9341 | l2arc_thread_exit = 1; | |
9342 | while (l2arc_thread_exit != 0) | |
9343 | cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); | |
9344 | mutex_exit(&l2arc_feed_thr_lock); | |
9345 | } | |
c28b2279 | 9346 | |
93ce2b4c | 9347 | #if defined(_KERNEL) |
0f699108 AZ |
9348 | EXPORT_SYMBOL(arc_buf_size); |
9349 | EXPORT_SYMBOL(arc_write); | |
c28b2279 | 9350 | EXPORT_SYMBOL(arc_read); |
e0b0ca98 | 9351 | EXPORT_SYMBOL(arc_buf_info); |
c28b2279 | 9352 | EXPORT_SYMBOL(arc_getbuf_func); |
ab26409d BB |
9353 | EXPORT_SYMBOL(arc_add_prune_callback); |
9354 | EXPORT_SYMBOL(arc_remove_prune_callback); | |
c28b2279 | 9355 | |
02730c33 | 9356 | /* BEGIN CSTYLED */ |
bce45ec9 | 9357 | module_param(zfs_arc_min, ulong, 0644); |
c409e464 | 9358 | MODULE_PARM_DESC(zfs_arc_min, "Min arc size"); |
c28b2279 | 9359 | |
bce45ec9 | 9360 | module_param(zfs_arc_max, ulong, 0644); |
c409e464 | 9361 | MODULE_PARM_DESC(zfs_arc_max, "Max arc size"); |
c28b2279 | 9362 | |
bce45ec9 | 9363 | module_param(zfs_arc_meta_limit, ulong, 0644); |
c28b2279 | 9364 | MODULE_PARM_DESC(zfs_arc_meta_limit, "Meta limit for arc size"); |
6a8f9b6b | 9365 | |
9907cc1c G |
9366 | module_param(zfs_arc_meta_limit_percent, ulong, 0644); |
9367 | MODULE_PARM_DESC(zfs_arc_meta_limit_percent, | |
9368 | "Percent of arc size for arc meta limit"); | |
9369 | ||
ca0bf58d PS |
9370 | module_param(zfs_arc_meta_min, ulong, 0644); |
9371 | MODULE_PARM_DESC(zfs_arc_meta_min, "Min arc metadata"); | |
9372 | ||
bce45ec9 | 9373 | module_param(zfs_arc_meta_prune, int, 0644); |
2cbb06b5 | 9374 | MODULE_PARM_DESC(zfs_arc_meta_prune, "Meta objects to scan for prune"); |
c409e464 | 9375 | |
ca67b33a | 9376 | module_param(zfs_arc_meta_adjust_restarts, int, 0644); |
bc888666 BB |
9377 | MODULE_PARM_DESC(zfs_arc_meta_adjust_restarts, |
9378 | "Limit number of restarts in arc_adjust_meta"); | |
9379 | ||
f6046738 BB |
9380 | module_param(zfs_arc_meta_strategy, int, 0644); |
9381 | MODULE_PARM_DESC(zfs_arc_meta_strategy, "Meta reclaim strategy"); | |
9382 | ||
bce45ec9 | 9383 | module_param(zfs_arc_grow_retry, int, 0644); |
c409e464 BB |
9384 | MODULE_PARM_DESC(zfs_arc_grow_retry, "Seconds before growing arc size"); |
9385 | ||
62422785 PS |
9386 | module_param(zfs_arc_p_dampener_disable, int, 0644); |
9387 | MODULE_PARM_DESC(zfs_arc_p_dampener_disable, "disable arc_p adapt dampener"); | |
9388 | ||
bce45ec9 | 9389 | module_param(zfs_arc_shrink_shift, int, 0644); |
c409e464 BB |
9390 | MODULE_PARM_DESC(zfs_arc_shrink_shift, "log2(fraction of arc to reclaim)"); |
9391 | ||
03b60eee DB |
9392 | module_param(zfs_arc_pc_percent, uint, 0644); |
9393 | MODULE_PARM_DESC(zfs_arc_pc_percent, | |
9394 | "Percent of pagecache to reclaim arc to"); | |
9395 | ||
728d6ae9 BB |
9396 | module_param(zfs_arc_p_min_shift, int, 0644); |
9397 | MODULE_PARM_DESC(zfs_arc_p_min_shift, "arc_c shift to calc min/max arc_p"); | |
9398 | ||
49ddb315 MA |
9399 | module_param(zfs_arc_average_blocksize, int, 0444); |
9400 | MODULE_PARM_DESC(zfs_arc_average_blocksize, "Target average block size"); | |
9401 | ||
d3c2ae1c | 9402 | module_param(zfs_compressed_arc_enabled, int, 0644); |
544596c5 | 9403 | MODULE_PARM_DESC(zfs_compressed_arc_enabled, "Disable compressed arc buffers"); |
d3c2ae1c | 9404 | |
d4a72f23 TC |
9405 | module_param(zfs_arc_min_prefetch_ms, int, 0644); |
9406 | MODULE_PARM_DESC(zfs_arc_min_prefetch_ms, "Min life of prefetch block in ms"); | |
9407 | ||
9408 | module_param(zfs_arc_min_prescient_prefetch_ms, int, 0644); | |
9409 | MODULE_PARM_DESC(zfs_arc_min_prescient_prefetch_ms, | |
9410 | "Min life of prescient prefetched block in ms"); | |
bce45ec9 BB |
9411 | |
9412 | module_param(l2arc_write_max, ulong, 0644); | |
abd8610c BB |
9413 | MODULE_PARM_DESC(l2arc_write_max, "Max write bytes per interval"); |
9414 | ||
bce45ec9 | 9415 | module_param(l2arc_write_boost, ulong, 0644); |
abd8610c BB |
9416 | MODULE_PARM_DESC(l2arc_write_boost, "Extra write bytes during device warmup"); |
9417 | ||
bce45ec9 | 9418 | module_param(l2arc_headroom, ulong, 0644); |
abd8610c BB |
9419 | MODULE_PARM_DESC(l2arc_headroom, "Number of max device writes to precache"); |
9420 | ||
3a17a7a9 SK |
9421 | module_param(l2arc_headroom_boost, ulong, 0644); |
9422 | MODULE_PARM_DESC(l2arc_headroom_boost, "Compressed l2arc_headroom multiplier"); | |
9423 | ||
bce45ec9 | 9424 | module_param(l2arc_feed_secs, ulong, 0644); |
abd8610c BB |
9425 | MODULE_PARM_DESC(l2arc_feed_secs, "Seconds between L2ARC writing"); |
9426 | ||
bce45ec9 | 9427 | module_param(l2arc_feed_min_ms, ulong, 0644); |
abd8610c BB |
9428 | MODULE_PARM_DESC(l2arc_feed_min_ms, "Min feed interval in milliseconds"); |
9429 | ||
bce45ec9 | 9430 | module_param(l2arc_noprefetch, int, 0644); |
abd8610c BB |
9431 | MODULE_PARM_DESC(l2arc_noprefetch, "Skip caching prefetched buffers"); |
9432 | ||
bce45ec9 | 9433 | module_param(l2arc_feed_again, int, 0644); |
abd8610c BB |
9434 | MODULE_PARM_DESC(l2arc_feed_again, "Turbo L2ARC warmup"); |
9435 | ||
bce45ec9 | 9436 | module_param(l2arc_norw, int, 0644); |
abd8610c BB |
9437 | MODULE_PARM_DESC(l2arc_norw, "No reads during writes"); |
9438 | ||
7e8bddd0 BB |
9439 | module_param(zfs_arc_lotsfree_percent, int, 0644); |
9440 | MODULE_PARM_DESC(zfs_arc_lotsfree_percent, | |
9441 | "System free memory I/O throttle in bytes"); | |
9442 | ||
11f552fa BB |
9443 | module_param(zfs_arc_sys_free, ulong, 0644); |
9444 | MODULE_PARM_DESC(zfs_arc_sys_free, "System free memory target size in bytes"); | |
9445 | ||
25458cbe TC |
9446 | module_param(zfs_arc_dnode_limit, ulong, 0644); |
9447 | MODULE_PARM_DESC(zfs_arc_dnode_limit, "Minimum bytes of dnodes in arc"); | |
9448 | ||
9907cc1c G |
9449 | module_param(zfs_arc_dnode_limit_percent, ulong, 0644); |
9450 | MODULE_PARM_DESC(zfs_arc_dnode_limit_percent, | |
9451 | "Percent of ARC meta buffers for dnodes"); | |
9452 | ||
25458cbe TC |
9453 | module_param(zfs_arc_dnode_reduce_percent, ulong, 0644); |
9454 | MODULE_PARM_DESC(zfs_arc_dnode_reduce_percent, | |
9455 | "Percentage of excess dnodes to try to unpin"); | |
02730c33 | 9456 | /* END CSTYLED */ |
c28b2279 | 9457 | #endif |