]>
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 | |
ca6c7a94 BB |
1138 | #define HDR_EMPTY_OR_LOCKED(hdr) \ |
1139 | (HDR_EMPTY(hdr) || MUTEX_HELD(HDR_LOCK(hdr))) | |
1140 | ||
d3c2ae1c GW |
1141 | #define HDR_EQUAL(spa, dva, birth, hdr) \ |
1142 | ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ | |
1143 | ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ | |
1144 | ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa) | |
34dc7c2f | 1145 | |
428870ff BB |
1146 | static void |
1147 | buf_discard_identity(arc_buf_hdr_t *hdr) | |
1148 | { | |
1149 | hdr->b_dva.dva_word[0] = 0; | |
1150 | hdr->b_dva.dva_word[1] = 0; | |
1151 | hdr->b_birth = 0; | |
428870ff BB |
1152 | } |
1153 | ||
34dc7c2f | 1154 | static arc_buf_hdr_t * |
9b67f605 | 1155 | buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) |
34dc7c2f | 1156 | { |
9b67f605 MA |
1157 | const dva_t *dva = BP_IDENTITY(bp); |
1158 | uint64_t birth = BP_PHYSICAL_BIRTH(bp); | |
34dc7c2f BB |
1159 | uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); |
1160 | kmutex_t *hash_lock = BUF_HASH_LOCK(idx); | |
2a432414 | 1161 | arc_buf_hdr_t *hdr; |
34dc7c2f BB |
1162 | |
1163 | mutex_enter(hash_lock); | |
2a432414 GW |
1164 | for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; |
1165 | hdr = hdr->b_hash_next) { | |
d3c2ae1c | 1166 | if (HDR_EQUAL(spa, dva, birth, hdr)) { |
34dc7c2f | 1167 | *lockp = hash_lock; |
2a432414 | 1168 | return (hdr); |
34dc7c2f BB |
1169 | } |
1170 | } | |
1171 | mutex_exit(hash_lock); | |
1172 | *lockp = NULL; | |
1173 | return (NULL); | |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | * Insert an entry into the hash table. If there is already an element | |
1178 | * equal to elem in the hash table, then the already existing element | |
1179 | * will be returned and the new element will not be inserted. | |
1180 | * Otherwise returns NULL. | |
b9541d6b | 1181 | * If lockp == NULL, the caller is assumed to already hold the hash lock. |
34dc7c2f BB |
1182 | */ |
1183 | static arc_buf_hdr_t * | |
2a432414 | 1184 | buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) |
34dc7c2f | 1185 | { |
2a432414 | 1186 | uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); |
34dc7c2f | 1187 | kmutex_t *hash_lock = BUF_HASH_LOCK(idx); |
2a432414 | 1188 | arc_buf_hdr_t *fhdr; |
34dc7c2f BB |
1189 | uint32_t i; |
1190 | ||
2a432414 GW |
1191 | ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); |
1192 | ASSERT(hdr->b_birth != 0); | |
1193 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); | |
b9541d6b CW |
1194 | |
1195 | if (lockp != NULL) { | |
1196 | *lockp = hash_lock; | |
1197 | mutex_enter(hash_lock); | |
1198 | } else { | |
1199 | ASSERT(MUTEX_HELD(hash_lock)); | |
1200 | } | |
1201 | ||
2a432414 GW |
1202 | for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; |
1203 | fhdr = fhdr->b_hash_next, i++) { | |
d3c2ae1c | 1204 | if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) |
2a432414 | 1205 | return (fhdr); |
34dc7c2f BB |
1206 | } |
1207 | ||
2a432414 GW |
1208 | hdr->b_hash_next = buf_hash_table.ht_table[idx]; |
1209 | buf_hash_table.ht_table[idx] = hdr; | |
d3c2ae1c | 1210 | arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
34dc7c2f BB |
1211 | |
1212 | /* collect some hash table performance data */ | |
1213 | if (i > 0) { | |
1214 | ARCSTAT_BUMP(arcstat_hash_collisions); | |
1215 | if (i == 1) | |
1216 | ARCSTAT_BUMP(arcstat_hash_chains); | |
1217 | ||
1218 | ARCSTAT_MAX(arcstat_hash_chain_max, i); | |
1219 | } | |
1220 | ||
1221 | ARCSTAT_BUMP(arcstat_hash_elements); | |
1222 | ARCSTAT_MAXSTAT(arcstat_hash_elements); | |
1223 | ||
1224 | return (NULL); | |
1225 | } | |
1226 | ||
1227 | static void | |
2a432414 | 1228 | buf_hash_remove(arc_buf_hdr_t *hdr) |
34dc7c2f | 1229 | { |
2a432414 GW |
1230 | arc_buf_hdr_t *fhdr, **hdrp; |
1231 | uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); | |
34dc7c2f BB |
1232 | |
1233 | ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); | |
2a432414 | 1234 | ASSERT(HDR_IN_HASH_TABLE(hdr)); |
34dc7c2f | 1235 | |
2a432414 GW |
1236 | hdrp = &buf_hash_table.ht_table[idx]; |
1237 | while ((fhdr = *hdrp) != hdr) { | |
d3c2ae1c | 1238 | ASSERT3P(fhdr, !=, NULL); |
2a432414 | 1239 | hdrp = &fhdr->b_hash_next; |
34dc7c2f | 1240 | } |
2a432414 GW |
1241 | *hdrp = hdr->b_hash_next; |
1242 | hdr->b_hash_next = NULL; | |
d3c2ae1c | 1243 | arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
34dc7c2f BB |
1244 | |
1245 | /* collect some hash table performance data */ | |
1246 | ARCSTAT_BUMPDOWN(arcstat_hash_elements); | |
1247 | ||
1248 | if (buf_hash_table.ht_table[idx] && | |
1249 | buf_hash_table.ht_table[idx]->b_hash_next == NULL) | |
1250 | ARCSTAT_BUMPDOWN(arcstat_hash_chains); | |
1251 | } | |
1252 | ||
1253 | /* | |
1254 | * Global data structures and functions for the buf kmem cache. | |
1255 | */ | |
b5256303 | 1256 | |
b9541d6b | 1257 | static kmem_cache_t *hdr_full_cache; |
b5256303 | 1258 | static kmem_cache_t *hdr_full_crypt_cache; |
b9541d6b | 1259 | static kmem_cache_t *hdr_l2only_cache; |
34dc7c2f BB |
1260 | static kmem_cache_t *buf_cache; |
1261 | ||
1262 | static void | |
1263 | buf_fini(void) | |
1264 | { | |
1265 | int i; | |
1266 | ||
93ce2b4c | 1267 | #if defined(_KERNEL) |
d1d7e268 MK |
1268 | /* |
1269 | * Large allocations which do not require contiguous pages | |
1270 | * should be using vmem_free() in the linux kernel\ | |
1271 | */ | |
00b46022 BB |
1272 | vmem_free(buf_hash_table.ht_table, |
1273 | (buf_hash_table.ht_mask + 1) * sizeof (void *)); | |
1274 | #else | |
34dc7c2f BB |
1275 | kmem_free(buf_hash_table.ht_table, |
1276 | (buf_hash_table.ht_mask + 1) * sizeof (void *)); | |
00b46022 | 1277 | #endif |
34dc7c2f BB |
1278 | for (i = 0; i < BUF_LOCKS; i++) |
1279 | mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); | |
b9541d6b | 1280 | kmem_cache_destroy(hdr_full_cache); |
b5256303 | 1281 | kmem_cache_destroy(hdr_full_crypt_cache); |
b9541d6b | 1282 | kmem_cache_destroy(hdr_l2only_cache); |
34dc7c2f BB |
1283 | kmem_cache_destroy(buf_cache); |
1284 | } | |
1285 | ||
1286 | /* | |
1287 | * Constructor callback - called when the cache is empty | |
1288 | * and a new buf is requested. | |
1289 | */ | |
1290 | /* ARGSUSED */ | |
1291 | static int | |
b9541d6b CW |
1292 | hdr_full_cons(void *vbuf, void *unused, int kmflag) |
1293 | { | |
1294 | arc_buf_hdr_t *hdr = vbuf; | |
1295 | ||
1296 | bzero(hdr, HDR_FULL_SIZE); | |
ae76f45c | 1297 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; |
b9541d6b | 1298 | cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); |
424fd7c3 | 1299 | zfs_refcount_create(&hdr->b_l1hdr.b_refcnt); |
b9541d6b CW |
1300 | mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); |
1301 | list_link_init(&hdr->b_l1hdr.b_arc_node); | |
1302 | list_link_init(&hdr->b_l2hdr.b_l2node); | |
ca0bf58d | 1303 | multilist_link_init(&hdr->b_l1hdr.b_arc_node); |
b9541d6b CW |
1304 | arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); |
1305 | ||
1306 | return (0); | |
1307 | } | |
1308 | ||
b5256303 TC |
1309 | /* ARGSUSED */ |
1310 | static int | |
1311 | hdr_full_crypt_cons(void *vbuf, void *unused, int kmflag) | |
1312 | { | |
1313 | arc_buf_hdr_t *hdr = vbuf; | |
1314 | ||
1315 | hdr_full_cons(vbuf, unused, kmflag); | |
1316 | bzero(&hdr->b_crypt_hdr, sizeof (hdr->b_crypt_hdr)); | |
1317 | arc_space_consume(sizeof (hdr->b_crypt_hdr), ARC_SPACE_HDRS); | |
1318 | ||
1319 | return (0); | |
1320 | } | |
1321 | ||
b9541d6b CW |
1322 | /* ARGSUSED */ |
1323 | static int | |
1324 | hdr_l2only_cons(void *vbuf, void *unused, int kmflag) | |
34dc7c2f | 1325 | { |
2a432414 GW |
1326 | arc_buf_hdr_t *hdr = vbuf; |
1327 | ||
b9541d6b CW |
1328 | bzero(hdr, HDR_L2ONLY_SIZE); |
1329 | arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); | |
34dc7c2f | 1330 | |
34dc7c2f BB |
1331 | return (0); |
1332 | } | |
1333 | ||
b128c09f BB |
1334 | /* ARGSUSED */ |
1335 | static int | |
1336 | buf_cons(void *vbuf, void *unused, int kmflag) | |
1337 | { | |
1338 | arc_buf_t *buf = vbuf; | |
1339 | ||
1340 | bzero(buf, sizeof (arc_buf_t)); | |
428870ff | 1341 | mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); |
d164b209 BB |
1342 | arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); |
1343 | ||
b128c09f BB |
1344 | return (0); |
1345 | } | |
1346 | ||
34dc7c2f BB |
1347 | /* |
1348 | * Destructor callback - called when a cached buf is | |
1349 | * no longer required. | |
1350 | */ | |
1351 | /* ARGSUSED */ | |
1352 | static void | |
b9541d6b | 1353 | hdr_full_dest(void *vbuf, void *unused) |
34dc7c2f | 1354 | { |
2a432414 | 1355 | arc_buf_hdr_t *hdr = vbuf; |
34dc7c2f | 1356 | |
d3c2ae1c | 1357 | ASSERT(HDR_EMPTY(hdr)); |
b9541d6b | 1358 | cv_destroy(&hdr->b_l1hdr.b_cv); |
424fd7c3 | 1359 | zfs_refcount_destroy(&hdr->b_l1hdr.b_refcnt); |
b9541d6b | 1360 | mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); |
ca0bf58d | 1361 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b CW |
1362 | arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); |
1363 | } | |
1364 | ||
b5256303 TC |
1365 | /* ARGSUSED */ |
1366 | static void | |
1367 | hdr_full_crypt_dest(void *vbuf, void *unused) | |
1368 | { | |
1369 | arc_buf_hdr_t *hdr = vbuf; | |
1370 | ||
1371 | hdr_full_dest(vbuf, unused); | |
1372 | arc_space_return(sizeof (hdr->b_crypt_hdr), ARC_SPACE_HDRS); | |
1373 | } | |
1374 | ||
b9541d6b CW |
1375 | /* ARGSUSED */ |
1376 | static void | |
1377 | hdr_l2only_dest(void *vbuf, void *unused) | |
1378 | { | |
1379 | ASSERTV(arc_buf_hdr_t *hdr = vbuf); | |
1380 | ||
d3c2ae1c | 1381 | ASSERT(HDR_EMPTY(hdr)); |
b9541d6b | 1382 | arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); |
34dc7c2f BB |
1383 | } |
1384 | ||
b128c09f BB |
1385 | /* ARGSUSED */ |
1386 | static void | |
1387 | buf_dest(void *vbuf, void *unused) | |
1388 | { | |
1389 | arc_buf_t *buf = vbuf; | |
1390 | ||
428870ff | 1391 | mutex_destroy(&buf->b_evict_lock); |
d164b209 | 1392 | arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); |
b128c09f BB |
1393 | } |
1394 | ||
8c8af9d8 BB |
1395 | /* |
1396 | * Reclaim callback -- invoked when memory is low. | |
1397 | */ | |
1398 | /* ARGSUSED */ | |
1399 | static void | |
1400 | hdr_recl(void *unused) | |
1401 | { | |
1402 | dprintf("hdr_recl called\n"); | |
1403 | /* | |
1404 | * umem calls the reclaim func when we destroy the buf cache, | |
1405 | * which is after we do arc_fini(). | |
1406 | */ | |
3ec34e55 BL |
1407 | if (arc_initialized) |
1408 | zthr_wakeup(arc_reap_zthr); | |
8c8af9d8 BB |
1409 | } |
1410 | ||
34dc7c2f BB |
1411 | static void |
1412 | buf_init(void) | |
1413 | { | |
2db28197 | 1414 | uint64_t *ct = NULL; |
34dc7c2f BB |
1415 | uint64_t hsize = 1ULL << 12; |
1416 | int i, j; | |
1417 | ||
1418 | /* | |
1419 | * The hash table is big enough to fill all of physical memory | |
49ddb315 MA |
1420 | * with an average block size of zfs_arc_average_blocksize (default 8K). |
1421 | * By default, the table will take up | |
1422 | * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). | |
34dc7c2f | 1423 | */ |
9edb3695 | 1424 | while (hsize * zfs_arc_average_blocksize < arc_all_memory()) |
34dc7c2f BB |
1425 | hsize <<= 1; |
1426 | retry: | |
1427 | buf_hash_table.ht_mask = hsize - 1; | |
93ce2b4c | 1428 | #if defined(_KERNEL) |
d1d7e268 MK |
1429 | /* |
1430 | * Large allocations which do not require contiguous pages | |
1431 | * should be using vmem_alloc() in the linux kernel | |
1432 | */ | |
00b46022 BB |
1433 | buf_hash_table.ht_table = |
1434 | vmem_zalloc(hsize * sizeof (void*), KM_SLEEP); | |
1435 | #else | |
34dc7c2f BB |
1436 | buf_hash_table.ht_table = |
1437 | kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); | |
00b46022 | 1438 | #endif |
34dc7c2f BB |
1439 | if (buf_hash_table.ht_table == NULL) { |
1440 | ASSERT(hsize > (1ULL << 8)); | |
1441 | hsize >>= 1; | |
1442 | goto retry; | |
1443 | } | |
1444 | ||
b9541d6b | 1445 | hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, |
8c8af9d8 | 1446 | 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0); |
b5256303 TC |
1447 | hdr_full_crypt_cache = kmem_cache_create("arc_buf_hdr_t_full_crypt", |
1448 | HDR_FULL_CRYPT_SIZE, 0, hdr_full_crypt_cons, hdr_full_crypt_dest, | |
1449 | hdr_recl, NULL, NULL, 0); | |
b9541d6b | 1450 | hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", |
8c8af9d8 | 1451 | HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl, |
b9541d6b | 1452 | NULL, NULL, 0); |
34dc7c2f | 1453 | buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), |
b128c09f | 1454 | 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); |
34dc7c2f BB |
1455 | |
1456 | for (i = 0; i < 256; i++) | |
1457 | for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) | |
1458 | *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); | |
1459 | ||
1460 | for (i = 0; i < BUF_LOCKS; i++) { | |
1461 | mutex_init(&buf_hash_table.ht_locks[i].ht_lock, | |
40d06e3c | 1462 | NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
1463 | } |
1464 | } | |
1465 | ||
d3c2ae1c | 1466 | #define ARC_MINTIME (hz>>4) /* 62 ms */ |
ca0bf58d | 1467 | |
2aa34383 DK |
1468 | /* |
1469 | * This is the size that the buf occupies in memory. If the buf is compressed, | |
1470 | * it will correspond to the compressed size. You should use this method of | |
1471 | * getting the buf size unless you explicitly need the logical size. | |
1472 | */ | |
1473 | uint64_t | |
1474 | arc_buf_size(arc_buf_t *buf) | |
1475 | { | |
1476 | return (ARC_BUF_COMPRESSED(buf) ? | |
1477 | HDR_GET_PSIZE(buf->b_hdr) : HDR_GET_LSIZE(buf->b_hdr)); | |
1478 | } | |
1479 | ||
1480 | uint64_t | |
1481 | arc_buf_lsize(arc_buf_t *buf) | |
1482 | { | |
1483 | return (HDR_GET_LSIZE(buf->b_hdr)); | |
1484 | } | |
1485 | ||
b5256303 TC |
1486 | /* |
1487 | * This function will return B_TRUE if the buffer is encrypted in memory. | |
1488 | * This buffer can be decrypted by calling arc_untransform(). | |
1489 | */ | |
1490 | boolean_t | |
1491 | arc_is_encrypted(arc_buf_t *buf) | |
1492 | { | |
1493 | return (ARC_BUF_ENCRYPTED(buf) != 0); | |
1494 | } | |
1495 | ||
1496 | /* | |
1497 | * Returns B_TRUE if the buffer represents data that has not had its MAC | |
1498 | * verified yet. | |
1499 | */ | |
1500 | boolean_t | |
1501 | arc_is_unauthenticated(arc_buf_t *buf) | |
1502 | { | |
1503 | return (HDR_NOAUTH(buf->b_hdr) != 0); | |
1504 | } | |
1505 | ||
1506 | void | |
1507 | arc_get_raw_params(arc_buf_t *buf, boolean_t *byteorder, uint8_t *salt, | |
1508 | uint8_t *iv, uint8_t *mac) | |
1509 | { | |
1510 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
1511 | ||
1512 | ASSERT(HDR_PROTECTED(hdr)); | |
1513 | ||
1514 | bcopy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN); | |
1515 | bcopy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN); | |
1516 | bcopy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN); | |
1517 | *byteorder = (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ? | |
1518 | ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER; | |
1519 | } | |
1520 | ||
1521 | /* | |
1522 | * Indicates how this buffer is compressed in memory. If it is not compressed | |
1523 | * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with | |
1524 | * arc_untransform() as long as it is also unencrypted. | |
1525 | */ | |
2aa34383 DK |
1526 | enum zio_compress |
1527 | arc_get_compression(arc_buf_t *buf) | |
1528 | { | |
1529 | return (ARC_BUF_COMPRESSED(buf) ? | |
1530 | HDR_GET_COMPRESS(buf->b_hdr) : ZIO_COMPRESS_OFF); | |
1531 | } | |
1532 | ||
b5256303 TC |
1533 | /* |
1534 | * Return the compression algorithm used to store this data in the ARC. If ARC | |
1535 | * compression is enabled or this is an encrypted block, this will be the same | |
1536 | * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF. | |
1537 | */ | |
1538 | static inline enum zio_compress | |
1539 | arc_hdr_get_compress(arc_buf_hdr_t *hdr) | |
1540 | { | |
1541 | return (HDR_COMPRESSION_ENABLED(hdr) ? | |
1542 | HDR_GET_COMPRESS(hdr) : ZIO_COMPRESS_OFF); | |
1543 | } | |
1544 | ||
d3c2ae1c GW |
1545 | static inline boolean_t |
1546 | arc_buf_is_shared(arc_buf_t *buf) | |
1547 | { | |
1548 | boolean_t shared = (buf->b_data != NULL && | |
a6255b7f DQ |
1549 | buf->b_hdr->b_l1hdr.b_pabd != NULL && |
1550 | abd_is_linear(buf->b_hdr->b_l1hdr.b_pabd) && | |
1551 | buf->b_data == abd_to_buf(buf->b_hdr->b_l1hdr.b_pabd)); | |
d3c2ae1c | 1552 | IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr)); |
2aa34383 DK |
1553 | IMPLY(shared, ARC_BUF_SHARED(buf)); |
1554 | IMPLY(shared, ARC_BUF_COMPRESSED(buf) || ARC_BUF_LAST(buf)); | |
524b4217 DK |
1555 | |
1556 | /* | |
1557 | * It would be nice to assert arc_can_share() too, but the "hdr isn't | |
1558 | * already being shared" requirement prevents us from doing that. | |
1559 | */ | |
1560 | ||
d3c2ae1c GW |
1561 | return (shared); |
1562 | } | |
ca0bf58d | 1563 | |
a7004725 DK |
1564 | /* |
1565 | * Free the checksum associated with this header. If there is no checksum, this | |
1566 | * is a no-op. | |
1567 | */ | |
d3c2ae1c GW |
1568 | static inline void |
1569 | arc_cksum_free(arc_buf_hdr_t *hdr) | |
1570 | { | |
1571 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
b5256303 | 1572 | |
d3c2ae1c GW |
1573 | mutex_enter(&hdr->b_l1hdr.b_freeze_lock); |
1574 | if (hdr->b_l1hdr.b_freeze_cksum != NULL) { | |
1575 | kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t)); | |
1576 | hdr->b_l1hdr.b_freeze_cksum = NULL; | |
b9541d6b | 1577 | } |
d3c2ae1c | 1578 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
b9541d6b CW |
1579 | } |
1580 | ||
a7004725 DK |
1581 | /* |
1582 | * Return true iff at least one of the bufs on hdr is not compressed. | |
b5256303 | 1583 | * Encrypted buffers count as compressed. |
a7004725 DK |
1584 | */ |
1585 | static boolean_t | |
1586 | arc_hdr_has_uncompressed_buf(arc_buf_hdr_t *hdr) | |
1587 | { | |
ca6c7a94 | 1588 | ASSERT(hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY_OR_LOCKED(hdr)); |
149ce888 | 1589 | |
a7004725 DK |
1590 | for (arc_buf_t *b = hdr->b_l1hdr.b_buf; b != NULL; b = b->b_next) { |
1591 | if (!ARC_BUF_COMPRESSED(b)) { | |
1592 | return (B_TRUE); | |
1593 | } | |
1594 | } | |
1595 | return (B_FALSE); | |
1596 | } | |
1597 | ||
1598 | ||
524b4217 DK |
1599 | /* |
1600 | * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data | |
1601 | * matches the checksum that is stored in the hdr. If there is no checksum, | |
1602 | * or if the buf is compressed, this is a no-op. | |
1603 | */ | |
34dc7c2f BB |
1604 | static void |
1605 | arc_cksum_verify(arc_buf_t *buf) | |
1606 | { | |
d3c2ae1c | 1607 | arc_buf_hdr_t *hdr = buf->b_hdr; |
34dc7c2f BB |
1608 | zio_cksum_t zc; |
1609 | ||
1610 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
1611 | return; | |
1612 | ||
149ce888 | 1613 | if (ARC_BUF_COMPRESSED(buf)) |
524b4217 | 1614 | return; |
524b4217 | 1615 | |
d3c2ae1c GW |
1616 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1617 | ||
1618 | mutex_enter(&hdr->b_l1hdr.b_freeze_lock); | |
149ce888 | 1619 | |
d3c2ae1c GW |
1620 | if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) { |
1621 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); | |
34dc7c2f BB |
1622 | return; |
1623 | } | |
2aa34383 | 1624 | |
3c67d83a | 1625 | fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, &zc); |
d3c2ae1c | 1626 | if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc)) |
34dc7c2f | 1627 | panic("buffer modified while frozen!"); |
d3c2ae1c | 1628 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1629 | } |
1630 | ||
b5256303 TC |
1631 | /* |
1632 | * This function makes the assumption that data stored in the L2ARC | |
1633 | * will be transformed exactly as it is in the main pool. Because of | |
1634 | * this we can verify the checksum against the reading process's bp. | |
1635 | */ | |
d3c2ae1c GW |
1636 | static boolean_t |
1637 | arc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio) | |
34dc7c2f | 1638 | { |
d3c2ae1c GW |
1639 | ASSERT(!BP_IS_EMBEDDED(zio->io_bp)); |
1640 | VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr)); | |
34dc7c2f | 1641 | |
d3c2ae1c GW |
1642 | /* |
1643 | * Block pointers always store the checksum for the logical data. | |
1644 | * If the block pointer has the gang bit set, then the checksum | |
1645 | * it represents is for the reconstituted data and not for an | |
1646 | * individual gang member. The zio pipeline, however, must be able to | |
1647 | * determine the checksum of each of the gang constituents so it | |
1648 | * treats the checksum comparison differently than what we need | |
1649 | * for l2arc blocks. This prevents us from using the | |
1650 | * zio_checksum_error() interface directly. Instead we must call the | |
1651 | * zio_checksum_error_impl() so that we can ensure the checksum is | |
1652 | * generated using the correct checksum algorithm and accounts for the | |
1653 | * logical I/O size and not just a gang fragment. | |
1654 | */ | |
b5256303 | 1655 | return (zio_checksum_error_impl(zio->io_spa, zio->io_bp, |
a6255b7f | 1656 | BP_GET_CHECKSUM(zio->io_bp), zio->io_abd, zio->io_size, |
d3c2ae1c | 1657 | zio->io_offset, NULL) == 0); |
34dc7c2f BB |
1658 | } |
1659 | ||
524b4217 DK |
1660 | /* |
1661 | * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a | |
1662 | * checksum and attaches it to the buf's hdr so that we can ensure that the buf | |
1663 | * isn't modified later on. If buf is compressed or there is already a checksum | |
1664 | * on the hdr, this is a no-op (we only checksum uncompressed bufs). | |
1665 | */ | |
34dc7c2f | 1666 | static void |
d3c2ae1c | 1667 | arc_cksum_compute(arc_buf_t *buf) |
34dc7c2f | 1668 | { |
d3c2ae1c GW |
1669 | arc_buf_hdr_t *hdr = buf->b_hdr; |
1670 | ||
1671 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
34dc7c2f BB |
1672 | return; |
1673 | ||
d3c2ae1c | 1674 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2aa34383 | 1675 | |
b9541d6b | 1676 | mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
149ce888 | 1677 | if (hdr->b_l1hdr.b_freeze_cksum != NULL || ARC_BUF_COMPRESSED(buf)) { |
d3c2ae1c | 1678 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1679 | return; |
1680 | } | |
2aa34383 | 1681 | |
b5256303 | 1682 | ASSERT(!ARC_BUF_ENCRYPTED(buf)); |
2aa34383 | 1683 | ASSERT(!ARC_BUF_COMPRESSED(buf)); |
d3c2ae1c GW |
1684 | hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), |
1685 | KM_SLEEP); | |
3c67d83a | 1686 | fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, |
d3c2ae1c GW |
1687 | hdr->b_l1hdr.b_freeze_cksum); |
1688 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); | |
498877ba MA |
1689 | arc_buf_watch(buf); |
1690 | } | |
1691 | ||
1692 | #ifndef _KERNEL | |
1693 | void | |
1694 | arc_buf_sigsegv(int sig, siginfo_t *si, void *unused) | |
1695 | { | |
02730c33 | 1696 | panic("Got SIGSEGV at address: 0x%lx\n", (long)si->si_addr); |
498877ba MA |
1697 | } |
1698 | #endif | |
1699 | ||
1700 | /* ARGSUSED */ | |
1701 | static void | |
1702 | arc_buf_unwatch(arc_buf_t *buf) | |
1703 | { | |
1704 | #ifndef _KERNEL | |
1705 | if (arc_watch) { | |
a7004725 | 1706 | ASSERT0(mprotect(buf->b_data, arc_buf_size(buf), |
498877ba MA |
1707 | PROT_READ | PROT_WRITE)); |
1708 | } | |
1709 | #endif | |
1710 | } | |
1711 | ||
1712 | /* ARGSUSED */ | |
1713 | static void | |
1714 | arc_buf_watch(arc_buf_t *buf) | |
1715 | { | |
1716 | #ifndef _KERNEL | |
1717 | if (arc_watch) | |
2aa34383 | 1718 | ASSERT0(mprotect(buf->b_data, arc_buf_size(buf), |
d3c2ae1c | 1719 | PROT_READ)); |
498877ba | 1720 | #endif |
34dc7c2f BB |
1721 | } |
1722 | ||
b9541d6b CW |
1723 | static arc_buf_contents_t |
1724 | arc_buf_type(arc_buf_hdr_t *hdr) | |
1725 | { | |
d3c2ae1c | 1726 | arc_buf_contents_t type; |
b9541d6b | 1727 | if (HDR_ISTYPE_METADATA(hdr)) { |
d3c2ae1c | 1728 | type = ARC_BUFC_METADATA; |
b9541d6b | 1729 | } else { |
d3c2ae1c | 1730 | type = ARC_BUFC_DATA; |
b9541d6b | 1731 | } |
d3c2ae1c GW |
1732 | VERIFY3U(hdr->b_type, ==, type); |
1733 | return (type); | |
b9541d6b CW |
1734 | } |
1735 | ||
2aa34383 DK |
1736 | boolean_t |
1737 | arc_is_metadata(arc_buf_t *buf) | |
1738 | { | |
1739 | return (HDR_ISTYPE_METADATA(buf->b_hdr) != 0); | |
1740 | } | |
1741 | ||
b9541d6b CW |
1742 | static uint32_t |
1743 | arc_bufc_to_flags(arc_buf_contents_t type) | |
1744 | { | |
1745 | switch (type) { | |
1746 | case ARC_BUFC_DATA: | |
1747 | /* metadata field is 0 if buffer contains normal data */ | |
1748 | return (0); | |
1749 | case ARC_BUFC_METADATA: | |
1750 | return (ARC_FLAG_BUFC_METADATA); | |
1751 | default: | |
1752 | break; | |
1753 | } | |
1754 | panic("undefined ARC buffer type!"); | |
1755 | return ((uint32_t)-1); | |
1756 | } | |
1757 | ||
34dc7c2f BB |
1758 | void |
1759 | arc_buf_thaw(arc_buf_t *buf) | |
1760 | { | |
d3c2ae1c GW |
1761 | arc_buf_hdr_t *hdr = buf->b_hdr; |
1762 | ||
2aa34383 DK |
1763 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
1764 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
1765 | ||
524b4217 | 1766 | arc_cksum_verify(buf); |
34dc7c2f | 1767 | |
2aa34383 | 1768 | /* |
149ce888 | 1769 | * Compressed buffers do not manipulate the b_freeze_cksum. |
2aa34383 | 1770 | */ |
149ce888 | 1771 | if (ARC_BUF_COMPRESSED(buf)) |
2aa34383 | 1772 | return; |
2aa34383 | 1773 | |
d3c2ae1c GW |
1774 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1775 | arc_cksum_free(hdr); | |
498877ba | 1776 | arc_buf_unwatch(buf); |
34dc7c2f BB |
1777 | } |
1778 | ||
1779 | void | |
1780 | arc_buf_freeze(arc_buf_t *buf) | |
1781 | { | |
1782 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
1783 | return; | |
1784 | ||
149ce888 | 1785 | if (ARC_BUF_COMPRESSED(buf)) |
2aa34383 | 1786 | return; |
428870ff | 1787 | |
149ce888 | 1788 | ASSERT(HDR_HAS_L1HDR(buf->b_hdr)); |
d3c2ae1c | 1789 | arc_cksum_compute(buf); |
34dc7c2f BB |
1790 | } |
1791 | ||
d3c2ae1c GW |
1792 | /* |
1793 | * The arc_buf_hdr_t's b_flags should never be modified directly. Instead, | |
1794 | * the following functions should be used to ensure that the flags are | |
1795 | * updated in a thread-safe way. When manipulating the flags either | |
1796 | * the hash_lock must be held or the hdr must be undiscoverable. This | |
1797 | * ensures that we're not racing with any other threads when updating | |
1798 | * the flags. | |
1799 | */ | |
1800 | static inline void | |
1801 | arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) | |
1802 | { | |
ca6c7a94 | 1803 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
1804 | hdr->b_flags |= flags; |
1805 | } | |
1806 | ||
1807 | static inline void | |
1808 | arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) | |
1809 | { | |
ca6c7a94 | 1810 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
1811 | hdr->b_flags &= ~flags; |
1812 | } | |
1813 | ||
1814 | /* | |
1815 | * Setting the compression bits in the arc_buf_hdr_t's b_flags is | |
1816 | * done in a special way since we have to clear and set bits | |
1817 | * at the same time. Consumers that wish to set the compression bits | |
1818 | * must use this function to ensure that the flags are updated in | |
1819 | * thread-safe manner. | |
1820 | */ | |
1821 | static void | |
1822 | arc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp) | |
1823 | { | |
ca6c7a94 | 1824 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
1825 | |
1826 | /* | |
1827 | * Holes and embedded blocks will always have a psize = 0 so | |
1828 | * we ignore the compression of the blkptr and set the | |
d3c2ae1c GW |
1829 | * want to uncompress them. Mark them as uncompressed. |
1830 | */ | |
1831 | if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) { | |
1832 | arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC); | |
d3c2ae1c | 1833 | ASSERT(!HDR_COMPRESSION_ENABLED(hdr)); |
d3c2ae1c GW |
1834 | } else { |
1835 | arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC); | |
d3c2ae1c GW |
1836 | ASSERT(HDR_COMPRESSION_ENABLED(hdr)); |
1837 | } | |
b5256303 TC |
1838 | |
1839 | HDR_SET_COMPRESS(hdr, cmp); | |
1840 | ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp); | |
d3c2ae1c GW |
1841 | } |
1842 | ||
524b4217 DK |
1843 | /* |
1844 | * Looks for another buf on the same hdr which has the data decompressed, copies | |
1845 | * from it, and returns true. If no such buf exists, returns false. | |
1846 | */ | |
1847 | static boolean_t | |
1848 | arc_buf_try_copy_decompressed_data(arc_buf_t *buf) | |
1849 | { | |
1850 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
524b4217 DK |
1851 | boolean_t copied = B_FALSE; |
1852 | ||
1853 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
1854 | ASSERT3P(buf->b_data, !=, NULL); | |
1855 | ASSERT(!ARC_BUF_COMPRESSED(buf)); | |
1856 | ||
a7004725 | 1857 | for (arc_buf_t *from = hdr->b_l1hdr.b_buf; from != NULL; |
524b4217 DK |
1858 | from = from->b_next) { |
1859 | /* can't use our own data buffer */ | |
1860 | if (from == buf) { | |
1861 | continue; | |
1862 | } | |
1863 | ||
1864 | if (!ARC_BUF_COMPRESSED(from)) { | |
1865 | bcopy(from->b_data, buf->b_data, arc_buf_size(buf)); | |
1866 | copied = B_TRUE; | |
1867 | break; | |
1868 | } | |
1869 | } | |
1870 | ||
1871 | /* | |
1872 | * There were no decompressed bufs, so there should not be a | |
1873 | * checksum on the hdr either. | |
1874 | */ | |
1875 | EQUIV(!copied, hdr->b_l1hdr.b_freeze_cksum == NULL); | |
1876 | ||
1877 | return (copied); | |
1878 | } | |
1879 | ||
b5256303 TC |
1880 | /* |
1881 | * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t. | |
1882 | */ | |
1883 | static uint64_t | |
1884 | arc_hdr_size(arc_buf_hdr_t *hdr) | |
1885 | { | |
1886 | uint64_t size; | |
1887 | ||
1888 | if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF && | |
1889 | HDR_GET_PSIZE(hdr) > 0) { | |
1890 | size = HDR_GET_PSIZE(hdr); | |
1891 | } else { | |
1892 | ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0); | |
1893 | size = HDR_GET_LSIZE(hdr); | |
1894 | } | |
1895 | return (size); | |
1896 | } | |
1897 | ||
1898 | static int | |
1899 | arc_hdr_authenticate(arc_buf_hdr_t *hdr, spa_t *spa, uint64_t dsobj) | |
1900 | { | |
1901 | int ret; | |
1902 | uint64_t csize; | |
1903 | uint64_t lsize = HDR_GET_LSIZE(hdr); | |
1904 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
1905 | void *tmpbuf = NULL; | |
1906 | abd_t *abd = hdr->b_l1hdr.b_pabd; | |
1907 | ||
ca6c7a94 | 1908 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
b5256303 TC |
1909 | ASSERT(HDR_AUTHENTICATED(hdr)); |
1910 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
1911 | ||
1912 | /* | |
1913 | * The MAC is calculated on the compressed data that is stored on disk. | |
1914 | * However, if compressed arc is disabled we will only have the | |
1915 | * decompressed data available to us now. Compress it into a temporary | |
1916 | * abd so we can verify the MAC. The performance overhead of this will | |
1917 | * be relatively low, since most objects in an encrypted objset will | |
1918 | * be encrypted (instead of authenticated) anyway. | |
1919 | */ | |
1920 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
1921 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
1922 | tmpbuf = zio_buf_alloc(lsize); | |
1923 | abd = abd_get_from_buf(tmpbuf, lsize); | |
1924 | abd_take_ownership_of_buf(abd, B_TRUE); | |
1925 | ||
1926 | csize = zio_compress_data(HDR_GET_COMPRESS(hdr), | |
1927 | hdr->b_l1hdr.b_pabd, tmpbuf, lsize); | |
1928 | ASSERT3U(csize, <=, psize); | |
1929 | abd_zero_off(abd, csize, psize - csize); | |
1930 | } | |
1931 | ||
1932 | /* | |
1933 | * Authentication is best effort. We authenticate whenever the key is | |
1934 | * available. If we succeed we clear ARC_FLAG_NOAUTH. | |
1935 | */ | |
1936 | if (hdr->b_crypt_hdr.b_ot == DMU_OT_OBJSET) { | |
1937 | ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); | |
1938 | ASSERT3U(lsize, ==, psize); | |
1939 | ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa, dsobj, abd, | |
1940 | psize, hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
1941 | } else { | |
1942 | ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj, abd, psize, | |
1943 | hdr->b_crypt_hdr.b_mac); | |
1944 | } | |
1945 | ||
1946 | if (ret == 0) | |
1947 | arc_hdr_clear_flags(hdr, ARC_FLAG_NOAUTH); | |
1948 | else if (ret != ENOENT) | |
1949 | goto error; | |
1950 | ||
1951 | if (tmpbuf != NULL) | |
1952 | abd_free(abd); | |
1953 | ||
1954 | return (0); | |
1955 | ||
1956 | error: | |
1957 | if (tmpbuf != NULL) | |
1958 | abd_free(abd); | |
1959 | ||
1960 | return (ret); | |
1961 | } | |
1962 | ||
1963 | /* | |
1964 | * This function will take a header that only has raw encrypted data in | |
1965 | * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in | |
1966 | * b_l1hdr.b_pabd. If designated in the header flags, this function will | |
1967 | * also decompress the data. | |
1968 | */ | |
1969 | static int | |
be9a5c35 | 1970 | arc_hdr_decrypt(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb) |
b5256303 TC |
1971 | { |
1972 | int ret; | |
b5256303 TC |
1973 | abd_t *cabd = NULL; |
1974 | void *tmp = NULL; | |
1975 | boolean_t no_crypt = B_FALSE; | |
1976 | boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
1977 | ||
ca6c7a94 | 1978 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
b5256303 TC |
1979 | ASSERT(HDR_ENCRYPTED(hdr)); |
1980 | ||
1981 | arc_hdr_alloc_abd(hdr, B_FALSE); | |
1982 | ||
be9a5c35 TC |
1983 | ret = spa_do_crypt_abd(B_FALSE, spa, zb, hdr->b_crypt_hdr.b_ot, |
1984 | B_FALSE, bswap, hdr->b_crypt_hdr.b_salt, hdr->b_crypt_hdr.b_iv, | |
1985 | hdr->b_crypt_hdr.b_mac, HDR_GET_PSIZE(hdr), hdr->b_l1hdr.b_pabd, | |
b5256303 TC |
1986 | hdr->b_crypt_hdr.b_rabd, &no_crypt); |
1987 | if (ret != 0) | |
1988 | goto error; | |
1989 | ||
1990 | if (no_crypt) { | |
1991 | abd_copy(hdr->b_l1hdr.b_pabd, hdr->b_crypt_hdr.b_rabd, | |
1992 | HDR_GET_PSIZE(hdr)); | |
1993 | } | |
1994 | ||
1995 | /* | |
1996 | * If this header has disabled arc compression but the b_pabd is | |
1997 | * compressed after decrypting it, we need to decompress the newly | |
1998 | * decrypted data. | |
1999 | */ | |
2000 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
2001 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
2002 | /* | |
2003 | * We want to make sure that we are correctly honoring the | |
2004 | * zfs_abd_scatter_enabled setting, so we allocate an abd here | |
2005 | * and then loan a buffer from it, rather than allocating a | |
2006 | * linear buffer and wrapping it in an abd later. | |
2007 | */ | |
2008 | cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr); | |
2009 | tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr)); | |
2010 | ||
2011 | ret = zio_decompress_data(HDR_GET_COMPRESS(hdr), | |
2012 | hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr), | |
2013 | HDR_GET_LSIZE(hdr)); | |
2014 | if (ret != 0) { | |
2015 | abd_return_buf(cabd, tmp, arc_hdr_size(hdr)); | |
2016 | goto error; | |
2017 | } | |
2018 | ||
2019 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
2020 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
2021 | arc_hdr_size(hdr), hdr); | |
2022 | hdr->b_l1hdr.b_pabd = cabd; | |
2023 | } | |
2024 | ||
b5256303 TC |
2025 | return (0); |
2026 | ||
2027 | error: | |
2028 | arc_hdr_free_abd(hdr, B_FALSE); | |
b5256303 TC |
2029 | if (cabd != NULL) |
2030 | arc_free_data_buf(hdr, cabd, arc_hdr_size(hdr), hdr); | |
2031 | ||
2032 | return (ret); | |
2033 | } | |
2034 | ||
2035 | /* | |
2036 | * This function is called during arc_buf_fill() to prepare the header's | |
2037 | * abd plaintext pointer for use. This involves authenticated protected | |
2038 | * data and decrypting encrypted data into the plaintext abd. | |
2039 | */ | |
2040 | static int | |
2041 | arc_fill_hdr_crypt(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, spa_t *spa, | |
be9a5c35 | 2042 | const zbookmark_phys_t *zb, boolean_t noauth) |
b5256303 TC |
2043 | { |
2044 | int ret; | |
2045 | ||
2046 | ASSERT(HDR_PROTECTED(hdr)); | |
2047 | ||
2048 | if (hash_lock != NULL) | |
2049 | mutex_enter(hash_lock); | |
2050 | ||
2051 | if (HDR_NOAUTH(hdr) && !noauth) { | |
2052 | /* | |
2053 | * The caller requested authenticated data but our data has | |
2054 | * not been authenticated yet. Verify the MAC now if we can. | |
2055 | */ | |
be9a5c35 | 2056 | ret = arc_hdr_authenticate(hdr, spa, zb->zb_objset); |
b5256303 TC |
2057 | if (ret != 0) |
2058 | goto error; | |
2059 | } else if (HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd == NULL) { | |
2060 | /* | |
2061 | * If we only have the encrypted version of the data, but the | |
2062 | * unencrypted version was requested we take this opportunity | |
2063 | * to store the decrypted version in the header for future use. | |
2064 | */ | |
be9a5c35 | 2065 | ret = arc_hdr_decrypt(hdr, spa, zb); |
b5256303 TC |
2066 | if (ret != 0) |
2067 | goto error; | |
2068 | } | |
2069 | ||
2070 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
2071 | ||
2072 | if (hash_lock != NULL) | |
2073 | mutex_exit(hash_lock); | |
2074 | ||
2075 | return (0); | |
2076 | ||
2077 | error: | |
2078 | if (hash_lock != NULL) | |
2079 | mutex_exit(hash_lock); | |
2080 | ||
2081 | return (ret); | |
2082 | } | |
2083 | ||
2084 | /* | |
2085 | * This function is used by the dbuf code to decrypt bonus buffers in place. | |
2086 | * The dbuf code itself doesn't have any locking for decrypting a shared dnode | |
2087 | * block, so we use the hash lock here to protect against concurrent calls to | |
2088 | * arc_buf_fill(). | |
2089 | */ | |
2090 | static void | |
2091 | arc_buf_untransform_in_place(arc_buf_t *buf, kmutex_t *hash_lock) | |
2092 | { | |
2093 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
2094 | ||
2095 | ASSERT(HDR_ENCRYPTED(hdr)); | |
2096 | ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE); | |
ca6c7a94 | 2097 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
b5256303 TC |
2098 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
2099 | ||
2100 | zio_crypt_copy_dnode_bonus(hdr->b_l1hdr.b_pabd, buf->b_data, | |
2101 | arc_buf_size(buf)); | |
2102 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
2103 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
2104 | hdr->b_crypt_hdr.b_ebufcnt -= 1; | |
2105 | } | |
2106 | ||
524b4217 DK |
2107 | /* |
2108 | * Given a buf that has a data buffer attached to it, this function will | |
2109 | * efficiently fill the buf with data of the specified compression setting from | |
2110 | * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr | |
2111 | * are already sharing a data buf, no copy is performed. | |
2112 | * | |
2113 | * If the buf is marked as compressed but uncompressed data was requested, this | |
2114 | * will allocate a new data buffer for the buf, remove that flag, and fill the | |
2115 | * buf with uncompressed data. You can't request a compressed buf on a hdr with | |
2116 | * uncompressed data, and (since we haven't added support for it yet) if you | |
2117 | * want compressed data your buf must already be marked as compressed and have | |
2118 | * the correct-sized data buffer. | |
2119 | */ | |
2120 | static int | |
be9a5c35 TC |
2121 | arc_buf_fill(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb, |
2122 | arc_fill_flags_t flags) | |
d3c2ae1c | 2123 | { |
b5256303 | 2124 | int error = 0; |
d3c2ae1c | 2125 | arc_buf_hdr_t *hdr = buf->b_hdr; |
b5256303 TC |
2126 | boolean_t hdr_compressed = |
2127 | (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); | |
2128 | boolean_t compressed = (flags & ARC_FILL_COMPRESSED) != 0; | |
2129 | boolean_t encrypted = (flags & ARC_FILL_ENCRYPTED) != 0; | |
d3c2ae1c | 2130 | dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap; |
b5256303 | 2131 | kmutex_t *hash_lock = (flags & ARC_FILL_LOCKED) ? NULL : HDR_LOCK(hdr); |
d3c2ae1c | 2132 | |
524b4217 | 2133 | ASSERT3P(buf->b_data, !=, NULL); |
b5256303 | 2134 | IMPLY(compressed, hdr_compressed || ARC_BUF_ENCRYPTED(buf)); |
524b4217 | 2135 | IMPLY(compressed, ARC_BUF_COMPRESSED(buf)); |
b5256303 TC |
2136 | IMPLY(encrypted, HDR_ENCRYPTED(hdr)); |
2137 | IMPLY(encrypted, ARC_BUF_ENCRYPTED(buf)); | |
2138 | IMPLY(encrypted, ARC_BUF_COMPRESSED(buf)); | |
2139 | IMPLY(encrypted, !ARC_BUF_SHARED(buf)); | |
2140 | ||
2141 | /* | |
2142 | * If the caller wanted encrypted data we just need to copy it from | |
2143 | * b_rabd and potentially byteswap it. We won't be able to do any | |
2144 | * further transforms on it. | |
2145 | */ | |
2146 | if (encrypted) { | |
2147 | ASSERT(HDR_HAS_RABD(hdr)); | |
2148 | abd_copy_to_buf(buf->b_data, hdr->b_crypt_hdr.b_rabd, | |
2149 | HDR_GET_PSIZE(hdr)); | |
2150 | goto byteswap; | |
2151 | } | |
2152 | ||
2153 | /* | |
69830602 TC |
2154 | * Adjust encrypted and authenticated headers to accomodate |
2155 | * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are | |
2156 | * allowed to fail decryption due to keys not being loaded | |
2157 | * without being marked as an IO error. | |
b5256303 TC |
2158 | */ |
2159 | if (HDR_PROTECTED(hdr)) { | |
2160 | error = arc_fill_hdr_crypt(hdr, hash_lock, spa, | |
be9a5c35 | 2161 | zb, !!(flags & ARC_FILL_NOAUTH)); |
69830602 TC |
2162 | if (error == EACCES && (flags & ARC_FILL_IN_PLACE) != 0) { |
2163 | return (error); | |
2164 | } else if (error != 0) { | |
e7504d7a TC |
2165 | if (hash_lock != NULL) |
2166 | mutex_enter(hash_lock); | |
2c24b5b1 | 2167 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); |
e7504d7a TC |
2168 | if (hash_lock != NULL) |
2169 | mutex_exit(hash_lock); | |
b5256303 | 2170 | return (error); |
2c24b5b1 | 2171 | } |
b5256303 TC |
2172 | } |
2173 | ||
2174 | /* | |
2175 | * There is a special case here for dnode blocks which are | |
2176 | * decrypting their bonus buffers. These blocks may request to | |
2177 | * be decrypted in-place. This is necessary because there may | |
2178 | * be many dnodes pointing into this buffer and there is | |
2179 | * currently no method to synchronize replacing the backing | |
2180 | * b_data buffer and updating all of the pointers. Here we use | |
2181 | * the hash lock to ensure there are no races. If the need | |
2182 | * arises for other types to be decrypted in-place, they must | |
2183 | * add handling here as well. | |
2184 | */ | |
2185 | if ((flags & ARC_FILL_IN_PLACE) != 0) { | |
2186 | ASSERT(!hdr_compressed); | |
2187 | ASSERT(!compressed); | |
2188 | ASSERT(!encrypted); | |
2189 | ||
2190 | if (HDR_ENCRYPTED(hdr) && ARC_BUF_ENCRYPTED(buf)) { | |
2191 | ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE); | |
2192 | ||
2193 | if (hash_lock != NULL) | |
2194 | mutex_enter(hash_lock); | |
2195 | arc_buf_untransform_in_place(buf, hash_lock); | |
2196 | if (hash_lock != NULL) | |
2197 | mutex_exit(hash_lock); | |
2198 | ||
2199 | /* Compute the hdr's checksum if necessary */ | |
2200 | arc_cksum_compute(buf); | |
2201 | } | |
2202 | ||
2203 | return (0); | |
2204 | } | |
524b4217 DK |
2205 | |
2206 | if (hdr_compressed == compressed) { | |
2aa34383 | 2207 | if (!arc_buf_is_shared(buf)) { |
a6255b7f | 2208 | abd_copy_to_buf(buf->b_data, hdr->b_l1hdr.b_pabd, |
524b4217 | 2209 | arc_buf_size(buf)); |
2aa34383 | 2210 | } |
d3c2ae1c | 2211 | } else { |
524b4217 DK |
2212 | ASSERT(hdr_compressed); |
2213 | ASSERT(!compressed); | |
d3c2ae1c | 2214 | ASSERT3U(HDR_GET_LSIZE(hdr), !=, HDR_GET_PSIZE(hdr)); |
2aa34383 DK |
2215 | |
2216 | /* | |
524b4217 DK |
2217 | * If the buf is sharing its data with the hdr, unlink it and |
2218 | * allocate a new data buffer for the buf. | |
2aa34383 | 2219 | */ |
524b4217 DK |
2220 | if (arc_buf_is_shared(buf)) { |
2221 | ASSERT(ARC_BUF_COMPRESSED(buf)); | |
2222 | ||
2223 | /* We need to give the buf it's own b_data */ | |
2224 | buf->b_flags &= ~ARC_BUF_FLAG_SHARED; | |
2aa34383 DK |
2225 | buf->b_data = |
2226 | arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); | |
2227 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); | |
2228 | ||
524b4217 | 2229 | /* Previously overhead was 0; just add new overhead */ |
2aa34383 | 2230 | ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); |
524b4217 DK |
2231 | } else if (ARC_BUF_COMPRESSED(buf)) { |
2232 | /* We need to reallocate the buf's b_data */ | |
2233 | arc_free_data_buf(hdr, buf->b_data, HDR_GET_PSIZE(hdr), | |
2234 | buf); | |
2235 | buf->b_data = | |
2236 | arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); | |
2237 | ||
2238 | /* We increased the size of b_data; update overhead */ | |
2239 | ARCSTAT_INCR(arcstat_overhead_size, | |
2240 | HDR_GET_LSIZE(hdr) - HDR_GET_PSIZE(hdr)); | |
2aa34383 DK |
2241 | } |
2242 | ||
524b4217 DK |
2243 | /* |
2244 | * Regardless of the buf's previous compression settings, it | |
2245 | * should not be compressed at the end of this function. | |
2246 | */ | |
2247 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
2248 | ||
2249 | /* | |
2250 | * Try copying the data from another buf which already has a | |
2251 | * decompressed version. If that's not possible, it's time to | |
2252 | * bite the bullet and decompress the data from the hdr. | |
2253 | */ | |
2254 | if (arc_buf_try_copy_decompressed_data(buf)) { | |
2255 | /* Skip byteswapping and checksumming (already done) */ | |
2256 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, !=, NULL); | |
2257 | return (0); | |
2258 | } else { | |
b5256303 | 2259 | error = zio_decompress_data(HDR_GET_COMPRESS(hdr), |
a6255b7f | 2260 | hdr->b_l1hdr.b_pabd, buf->b_data, |
524b4217 DK |
2261 | HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); |
2262 | ||
2263 | /* | |
2264 | * Absent hardware errors or software bugs, this should | |
2265 | * be impossible, but log it anyway so we can debug it. | |
2266 | */ | |
2267 | if (error != 0) { | |
2268 | zfs_dbgmsg( | |
a887d653 | 2269 | "hdr %px, compress %d, psize %d, lsize %d", |
b5256303 | 2270 | hdr, arc_hdr_get_compress(hdr), |
524b4217 | 2271 | HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); |
e7504d7a TC |
2272 | if (hash_lock != NULL) |
2273 | mutex_enter(hash_lock); | |
2c24b5b1 | 2274 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); |
e7504d7a TC |
2275 | if (hash_lock != NULL) |
2276 | mutex_exit(hash_lock); | |
524b4217 DK |
2277 | return (SET_ERROR(EIO)); |
2278 | } | |
d3c2ae1c GW |
2279 | } |
2280 | } | |
524b4217 | 2281 | |
b5256303 | 2282 | byteswap: |
524b4217 | 2283 | /* Byteswap the buf's data if necessary */ |
d3c2ae1c GW |
2284 | if (bswap != DMU_BSWAP_NUMFUNCS) { |
2285 | ASSERT(!HDR_SHARED_DATA(hdr)); | |
2286 | ASSERT3U(bswap, <, DMU_BSWAP_NUMFUNCS); | |
2287 | dmu_ot_byteswap[bswap].ob_func(buf->b_data, HDR_GET_LSIZE(hdr)); | |
2288 | } | |
524b4217 DK |
2289 | |
2290 | /* Compute the hdr's checksum if necessary */ | |
d3c2ae1c | 2291 | arc_cksum_compute(buf); |
524b4217 | 2292 | |
d3c2ae1c GW |
2293 | return (0); |
2294 | } | |
2295 | ||
2296 | /* | |
b5256303 TC |
2297 | * If this function is being called to decrypt an encrypted buffer or verify an |
2298 | * authenticated one, the key must be loaded and a mapping must be made | |
2299 | * available in the keystore via spa_keystore_create_mapping() or one of its | |
2300 | * callers. | |
d3c2ae1c | 2301 | */ |
b5256303 | 2302 | int |
a2c2ed1b TC |
2303 | arc_untransform(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb, |
2304 | boolean_t in_place) | |
d3c2ae1c | 2305 | { |
a2c2ed1b | 2306 | int ret; |
b5256303 | 2307 | arc_fill_flags_t flags = 0; |
d3c2ae1c | 2308 | |
b5256303 TC |
2309 | if (in_place) |
2310 | flags |= ARC_FILL_IN_PLACE; | |
2311 | ||
be9a5c35 | 2312 | ret = arc_buf_fill(buf, spa, zb, flags); |
a2c2ed1b TC |
2313 | if (ret == ECKSUM) { |
2314 | /* | |
2315 | * Convert authentication and decryption errors to EIO | |
2316 | * (and generate an ereport) before leaving the ARC. | |
2317 | */ | |
2318 | ret = SET_ERROR(EIO); | |
be9a5c35 | 2319 | spa_log_error(spa, zb); |
a2c2ed1b TC |
2320 | zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION, |
2321 | spa, NULL, zb, NULL, 0, 0); | |
2322 | } | |
2323 | ||
2324 | return (ret); | |
d3c2ae1c GW |
2325 | } |
2326 | ||
2327 | /* | |
2328 | * Increment the amount of evictable space in the arc_state_t's refcount. | |
2329 | * We account for the space used by the hdr and the arc buf individually | |
2330 | * so that we can add and remove them from the refcount individually. | |
2331 | */ | |
34dc7c2f | 2332 | static void |
d3c2ae1c GW |
2333 | arc_evictable_space_increment(arc_buf_hdr_t *hdr, arc_state_t *state) |
2334 | { | |
2335 | arc_buf_contents_t type = arc_buf_type(hdr); | |
d3c2ae1c GW |
2336 | |
2337 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
2338 | ||
2339 | if (GHOST_STATE(state)) { | |
2340 | ASSERT0(hdr->b_l1hdr.b_bufcnt); | |
2341 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
a6255b7f | 2342 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2343 | ASSERT(!HDR_HAS_RABD(hdr)); |
424fd7c3 | 2344 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
2aa34383 | 2345 | HDR_GET_LSIZE(hdr), hdr); |
d3c2ae1c GW |
2346 | return; |
2347 | } | |
2348 | ||
2349 | ASSERT(!GHOST_STATE(state)); | |
a6255b7f | 2350 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 | 2351 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
d3c2ae1c GW |
2352 | arc_hdr_size(hdr), hdr); |
2353 | } | |
b5256303 | 2354 | if (HDR_HAS_RABD(hdr)) { |
424fd7c3 | 2355 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
b5256303 TC |
2356 | HDR_GET_PSIZE(hdr), hdr); |
2357 | } | |
2358 | ||
1c27024e DB |
2359 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
2360 | buf = buf->b_next) { | |
2aa34383 | 2361 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2362 | continue; |
424fd7c3 | 2363 | (void) zfs_refcount_add_many(&state->arcs_esize[type], |
2aa34383 | 2364 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2365 | } |
2366 | } | |
2367 | ||
2368 | /* | |
2369 | * Decrement the amount of evictable space in the arc_state_t's refcount. | |
2370 | * We account for the space used by the hdr and the arc buf individually | |
2371 | * so that we can add and remove them from the refcount individually. | |
2372 | */ | |
2373 | static void | |
2aa34383 | 2374 | arc_evictable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state) |
d3c2ae1c GW |
2375 | { |
2376 | arc_buf_contents_t type = arc_buf_type(hdr); | |
d3c2ae1c GW |
2377 | |
2378 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
2379 | ||
2380 | if (GHOST_STATE(state)) { | |
2381 | ASSERT0(hdr->b_l1hdr.b_bufcnt); | |
2382 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
a6255b7f | 2383 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2384 | ASSERT(!HDR_HAS_RABD(hdr)); |
424fd7c3 | 2385 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
2aa34383 | 2386 | HDR_GET_LSIZE(hdr), hdr); |
d3c2ae1c GW |
2387 | return; |
2388 | } | |
2389 | ||
2390 | ASSERT(!GHOST_STATE(state)); | |
a6255b7f | 2391 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 | 2392 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c GW |
2393 | arc_hdr_size(hdr), hdr); |
2394 | } | |
b5256303 | 2395 | if (HDR_HAS_RABD(hdr)) { |
424fd7c3 | 2396 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
b5256303 TC |
2397 | HDR_GET_PSIZE(hdr), hdr); |
2398 | } | |
2399 | ||
1c27024e DB |
2400 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
2401 | buf = buf->b_next) { | |
2aa34383 | 2402 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2403 | continue; |
424fd7c3 | 2404 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
2aa34383 | 2405 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2406 | } |
2407 | } | |
2408 | ||
2409 | /* | |
2410 | * Add a reference to this hdr indicating that someone is actively | |
2411 | * referencing that memory. When the refcount transitions from 0 to 1, | |
2412 | * we remove it from the respective arc_state_t list to indicate that | |
2413 | * it is not evictable. | |
2414 | */ | |
2415 | static void | |
2416 | add_reference(arc_buf_hdr_t *hdr, void *tag) | |
34dc7c2f | 2417 | { |
b9541d6b CW |
2418 | arc_state_t *state; |
2419 | ||
2420 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
ca6c7a94 | 2421 | if (!HDR_EMPTY(hdr) && !MUTEX_HELD(HDR_LOCK(hdr))) { |
d3c2ae1c | 2422 | ASSERT(hdr->b_l1hdr.b_state == arc_anon); |
424fd7c3 | 2423 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
2424 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
2425 | } | |
34dc7c2f | 2426 | |
b9541d6b CW |
2427 | state = hdr->b_l1hdr.b_state; |
2428 | ||
c13060e4 | 2429 | if ((zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && |
b9541d6b CW |
2430 | (state != arc_anon)) { |
2431 | /* We don't use the L2-only state list. */ | |
2432 | if (state != arc_l2c_only) { | |
64fc7762 | 2433 | multilist_remove(state->arcs_list[arc_buf_type(hdr)], |
d3c2ae1c | 2434 | hdr); |
2aa34383 | 2435 | arc_evictable_space_decrement(hdr, state); |
34dc7c2f | 2436 | } |
b128c09f | 2437 | /* remove the prefetch flag if we get a reference */ |
d3c2ae1c | 2438 | arc_hdr_clear_flags(hdr, ARC_FLAG_PREFETCH); |
34dc7c2f BB |
2439 | } |
2440 | } | |
2441 | ||
d3c2ae1c GW |
2442 | /* |
2443 | * Remove a reference from this hdr. When the reference transitions from | |
2444 | * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's | |
2445 | * list making it eligible for eviction. | |
2446 | */ | |
34dc7c2f | 2447 | static int |
2a432414 | 2448 | remove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) |
34dc7c2f BB |
2449 | { |
2450 | int cnt; | |
b9541d6b | 2451 | arc_state_t *state = hdr->b_l1hdr.b_state; |
34dc7c2f | 2452 | |
b9541d6b | 2453 | ASSERT(HDR_HAS_L1HDR(hdr)); |
34dc7c2f BB |
2454 | ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); |
2455 | ASSERT(!GHOST_STATE(state)); | |
2456 | ||
b9541d6b CW |
2457 | /* |
2458 | * arc_l2c_only counts as a ghost state so we don't need to explicitly | |
2459 | * check to prevent usage of the arc_l2c_only list. | |
2460 | */ | |
424fd7c3 | 2461 | if (((cnt = zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && |
34dc7c2f | 2462 | (state != arc_anon)) { |
64fc7762 | 2463 | multilist_insert(state->arcs_list[arc_buf_type(hdr)], hdr); |
d3c2ae1c GW |
2464 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); |
2465 | arc_evictable_space_increment(hdr, state); | |
34dc7c2f BB |
2466 | } |
2467 | return (cnt); | |
2468 | } | |
2469 | ||
e0b0ca98 BB |
2470 | /* |
2471 | * Returns detailed information about a specific arc buffer. When the | |
2472 | * state_index argument is set the function will calculate the arc header | |
2473 | * list position for its arc state. Since this requires a linear traversal | |
2474 | * callers are strongly encourage not to do this. However, it can be helpful | |
2475 | * for targeted analysis so the functionality is provided. | |
2476 | */ | |
2477 | void | |
2478 | arc_buf_info(arc_buf_t *ab, arc_buf_info_t *abi, int state_index) | |
2479 | { | |
2480 | arc_buf_hdr_t *hdr = ab->b_hdr; | |
b9541d6b CW |
2481 | l1arc_buf_hdr_t *l1hdr = NULL; |
2482 | l2arc_buf_hdr_t *l2hdr = NULL; | |
2483 | arc_state_t *state = NULL; | |
2484 | ||
8887c7d7 TC |
2485 | memset(abi, 0, sizeof (arc_buf_info_t)); |
2486 | ||
2487 | if (hdr == NULL) | |
2488 | return; | |
2489 | ||
2490 | abi->abi_flags = hdr->b_flags; | |
2491 | ||
b9541d6b CW |
2492 | if (HDR_HAS_L1HDR(hdr)) { |
2493 | l1hdr = &hdr->b_l1hdr; | |
2494 | state = l1hdr->b_state; | |
2495 | } | |
2496 | if (HDR_HAS_L2HDR(hdr)) | |
2497 | l2hdr = &hdr->b_l2hdr; | |
e0b0ca98 | 2498 | |
b9541d6b | 2499 | if (l1hdr) { |
d3c2ae1c | 2500 | abi->abi_bufcnt = l1hdr->b_bufcnt; |
b9541d6b CW |
2501 | abi->abi_access = l1hdr->b_arc_access; |
2502 | abi->abi_mru_hits = l1hdr->b_mru_hits; | |
2503 | abi->abi_mru_ghost_hits = l1hdr->b_mru_ghost_hits; | |
2504 | abi->abi_mfu_hits = l1hdr->b_mfu_hits; | |
2505 | abi->abi_mfu_ghost_hits = l1hdr->b_mfu_ghost_hits; | |
424fd7c3 | 2506 | abi->abi_holds = zfs_refcount_count(&l1hdr->b_refcnt); |
b9541d6b CW |
2507 | } |
2508 | ||
2509 | if (l2hdr) { | |
2510 | abi->abi_l2arc_dattr = l2hdr->b_daddr; | |
b9541d6b CW |
2511 | abi->abi_l2arc_hits = l2hdr->b_hits; |
2512 | } | |
2513 | ||
e0b0ca98 | 2514 | abi->abi_state_type = state ? state->arcs_state : ARC_STATE_ANON; |
b9541d6b | 2515 | abi->abi_state_contents = arc_buf_type(hdr); |
d3c2ae1c | 2516 | abi->abi_size = arc_hdr_size(hdr); |
e0b0ca98 BB |
2517 | } |
2518 | ||
34dc7c2f | 2519 | /* |
ca0bf58d | 2520 | * Move the supplied buffer to the indicated state. The hash lock |
34dc7c2f BB |
2521 | * for the buffer must be held by the caller. |
2522 | */ | |
2523 | static void | |
2a432414 GW |
2524 | arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, |
2525 | kmutex_t *hash_lock) | |
34dc7c2f | 2526 | { |
b9541d6b CW |
2527 | arc_state_t *old_state; |
2528 | int64_t refcnt; | |
d3c2ae1c GW |
2529 | uint32_t bufcnt; |
2530 | boolean_t update_old, update_new; | |
b9541d6b CW |
2531 | arc_buf_contents_t buftype = arc_buf_type(hdr); |
2532 | ||
2533 | /* | |
2534 | * We almost always have an L1 hdr here, since we call arc_hdr_realloc() | |
2535 | * in arc_read() when bringing a buffer out of the L2ARC. However, the | |
2536 | * L1 hdr doesn't always exist when we change state to arc_anon before | |
2537 | * destroying a header, in which case reallocating to add the L1 hdr is | |
2538 | * pointless. | |
2539 | */ | |
2540 | if (HDR_HAS_L1HDR(hdr)) { | |
2541 | old_state = hdr->b_l1hdr.b_state; | |
424fd7c3 | 2542 | refcnt = zfs_refcount_count(&hdr->b_l1hdr.b_refcnt); |
d3c2ae1c | 2543 | bufcnt = hdr->b_l1hdr.b_bufcnt; |
b5256303 TC |
2544 | update_old = (bufcnt > 0 || hdr->b_l1hdr.b_pabd != NULL || |
2545 | HDR_HAS_RABD(hdr)); | |
b9541d6b CW |
2546 | } else { |
2547 | old_state = arc_l2c_only; | |
2548 | refcnt = 0; | |
d3c2ae1c GW |
2549 | bufcnt = 0; |
2550 | update_old = B_FALSE; | |
b9541d6b | 2551 | } |
d3c2ae1c | 2552 | update_new = update_old; |
34dc7c2f BB |
2553 | |
2554 | ASSERT(MUTEX_HELD(hash_lock)); | |
e8b96c60 | 2555 | ASSERT3P(new_state, !=, old_state); |
d3c2ae1c GW |
2556 | ASSERT(!GHOST_STATE(new_state) || bufcnt == 0); |
2557 | ASSERT(old_state != arc_anon || bufcnt <= 1); | |
34dc7c2f BB |
2558 | |
2559 | /* | |
2560 | * If this buffer is evictable, transfer it from the | |
2561 | * old state list to the new state list. | |
2562 | */ | |
2563 | if (refcnt == 0) { | |
b9541d6b | 2564 | if (old_state != arc_anon && old_state != arc_l2c_only) { |
b9541d6b | 2565 | ASSERT(HDR_HAS_L1HDR(hdr)); |
64fc7762 | 2566 | multilist_remove(old_state->arcs_list[buftype], hdr); |
34dc7c2f | 2567 | |
d3c2ae1c GW |
2568 | if (GHOST_STATE(old_state)) { |
2569 | ASSERT0(bufcnt); | |
2570 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
2571 | update_old = B_TRUE; | |
34dc7c2f | 2572 | } |
2aa34383 | 2573 | arc_evictable_space_decrement(hdr, old_state); |
34dc7c2f | 2574 | } |
b9541d6b | 2575 | if (new_state != arc_anon && new_state != arc_l2c_only) { |
b9541d6b CW |
2576 | /* |
2577 | * An L1 header always exists here, since if we're | |
2578 | * moving to some L1-cached state (i.e. not l2c_only or | |
2579 | * anonymous), we realloc the header to add an L1hdr | |
2580 | * beforehand. | |
2581 | */ | |
2582 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
64fc7762 | 2583 | multilist_insert(new_state->arcs_list[buftype], hdr); |
34dc7c2f | 2584 | |
34dc7c2f | 2585 | if (GHOST_STATE(new_state)) { |
d3c2ae1c GW |
2586 | ASSERT0(bufcnt); |
2587 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
2588 | update_new = B_TRUE; | |
34dc7c2f | 2589 | } |
d3c2ae1c | 2590 | arc_evictable_space_increment(hdr, new_state); |
34dc7c2f BB |
2591 | } |
2592 | } | |
2593 | ||
d3c2ae1c | 2594 | ASSERT(!HDR_EMPTY(hdr)); |
2a432414 GW |
2595 | if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) |
2596 | buf_hash_remove(hdr); | |
34dc7c2f | 2597 | |
b9541d6b | 2598 | /* adjust state sizes (ignore arc_l2c_only) */ |
36da08ef | 2599 | |
d3c2ae1c | 2600 | if (update_new && new_state != arc_l2c_only) { |
36da08ef PS |
2601 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2602 | if (GHOST_STATE(new_state)) { | |
d3c2ae1c | 2603 | ASSERT0(bufcnt); |
36da08ef PS |
2604 | |
2605 | /* | |
d3c2ae1c | 2606 | * When moving a header to a ghost state, we first |
36da08ef | 2607 | * remove all arc buffers. Thus, we'll have a |
d3c2ae1c | 2608 | * bufcnt of zero, and no arc buffer to use for |
36da08ef PS |
2609 | * the reference. As a result, we use the arc |
2610 | * header pointer for the reference. | |
2611 | */ | |
424fd7c3 | 2612 | (void) zfs_refcount_add_many(&new_state->arcs_size, |
d3c2ae1c | 2613 | HDR_GET_LSIZE(hdr), hdr); |
a6255b7f | 2614 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2615 | ASSERT(!HDR_HAS_RABD(hdr)); |
36da08ef | 2616 | } else { |
d3c2ae1c | 2617 | uint32_t buffers = 0; |
36da08ef PS |
2618 | |
2619 | /* | |
2620 | * Each individual buffer holds a unique reference, | |
2621 | * thus we must remove each of these references one | |
2622 | * at a time. | |
2623 | */ | |
1c27024e | 2624 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
36da08ef | 2625 | buf = buf->b_next) { |
d3c2ae1c GW |
2626 | ASSERT3U(bufcnt, !=, 0); |
2627 | buffers++; | |
2628 | ||
2629 | /* | |
2630 | * When the arc_buf_t is sharing the data | |
2631 | * block with the hdr, the owner of the | |
2632 | * reference belongs to the hdr. Only | |
2633 | * add to the refcount if the arc_buf_t is | |
2634 | * not shared. | |
2635 | */ | |
2aa34383 | 2636 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2637 | continue; |
d3c2ae1c | 2638 | |
424fd7c3 TS |
2639 | (void) zfs_refcount_add_many( |
2640 | &new_state->arcs_size, | |
2aa34383 | 2641 | arc_buf_size(buf), buf); |
d3c2ae1c GW |
2642 | } |
2643 | ASSERT3U(bufcnt, ==, buffers); | |
2644 | ||
a6255b7f | 2645 | if (hdr->b_l1hdr.b_pabd != NULL) { |
424fd7c3 TS |
2646 | (void) zfs_refcount_add_many( |
2647 | &new_state->arcs_size, | |
d3c2ae1c | 2648 | arc_hdr_size(hdr), hdr); |
b5256303 TC |
2649 | } |
2650 | ||
2651 | if (HDR_HAS_RABD(hdr)) { | |
424fd7c3 TS |
2652 | (void) zfs_refcount_add_many( |
2653 | &new_state->arcs_size, | |
b5256303 | 2654 | HDR_GET_PSIZE(hdr), hdr); |
36da08ef PS |
2655 | } |
2656 | } | |
2657 | } | |
2658 | ||
d3c2ae1c | 2659 | if (update_old && old_state != arc_l2c_only) { |
36da08ef PS |
2660 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2661 | if (GHOST_STATE(old_state)) { | |
d3c2ae1c | 2662 | ASSERT0(bufcnt); |
a6255b7f | 2663 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 2664 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c | 2665 | |
36da08ef PS |
2666 | /* |
2667 | * When moving a header off of a ghost state, | |
d3c2ae1c GW |
2668 | * the header will not contain any arc buffers. |
2669 | * We use the arc header pointer for the reference | |
2670 | * which is exactly what we did when we put the | |
2671 | * header on the ghost state. | |
36da08ef PS |
2672 | */ |
2673 | ||
424fd7c3 | 2674 | (void) zfs_refcount_remove_many(&old_state->arcs_size, |
d3c2ae1c | 2675 | HDR_GET_LSIZE(hdr), hdr); |
36da08ef | 2676 | } else { |
d3c2ae1c | 2677 | uint32_t buffers = 0; |
36da08ef PS |
2678 | |
2679 | /* | |
2680 | * Each individual buffer holds a unique reference, | |
2681 | * thus we must remove each of these references one | |
2682 | * at a time. | |
2683 | */ | |
1c27024e | 2684 | for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; |
36da08ef | 2685 | buf = buf->b_next) { |
d3c2ae1c GW |
2686 | ASSERT3U(bufcnt, !=, 0); |
2687 | buffers++; | |
2688 | ||
2689 | /* | |
2690 | * When the arc_buf_t is sharing the data | |
2691 | * block with the hdr, the owner of the | |
2692 | * reference belongs to the hdr. Only | |
2693 | * add to the refcount if the arc_buf_t is | |
2694 | * not shared. | |
2695 | */ | |
2aa34383 | 2696 | if (arc_buf_is_shared(buf)) |
d3c2ae1c | 2697 | continue; |
d3c2ae1c | 2698 | |
424fd7c3 | 2699 | (void) zfs_refcount_remove_many( |
2aa34383 | 2700 | &old_state->arcs_size, arc_buf_size(buf), |
d3c2ae1c | 2701 | buf); |
36da08ef | 2702 | } |
d3c2ae1c | 2703 | ASSERT3U(bufcnt, ==, buffers); |
b5256303 TC |
2704 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || |
2705 | HDR_HAS_RABD(hdr)); | |
2706 | ||
2707 | if (hdr->b_l1hdr.b_pabd != NULL) { | |
424fd7c3 | 2708 | (void) zfs_refcount_remove_many( |
b5256303 TC |
2709 | &old_state->arcs_size, arc_hdr_size(hdr), |
2710 | hdr); | |
2711 | } | |
2712 | ||
2713 | if (HDR_HAS_RABD(hdr)) { | |
424fd7c3 | 2714 | (void) zfs_refcount_remove_many( |
b5256303 TC |
2715 | &old_state->arcs_size, HDR_GET_PSIZE(hdr), |
2716 | hdr); | |
2717 | } | |
36da08ef | 2718 | } |
34dc7c2f | 2719 | } |
36da08ef | 2720 | |
b9541d6b CW |
2721 | if (HDR_HAS_L1HDR(hdr)) |
2722 | hdr->b_l1hdr.b_state = new_state; | |
34dc7c2f | 2723 | |
b9541d6b CW |
2724 | /* |
2725 | * L2 headers should never be on the L2 state list since they don't | |
2726 | * have L1 headers allocated. | |
2727 | */ | |
64fc7762 MA |
2728 | ASSERT(multilist_is_empty(arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && |
2729 | multilist_is_empty(arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); | |
34dc7c2f BB |
2730 | } |
2731 | ||
2732 | void | |
d164b209 | 2733 | arc_space_consume(uint64_t space, arc_space_type_t type) |
34dc7c2f | 2734 | { |
d164b209 BB |
2735 | ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); |
2736 | ||
2737 | switch (type) { | |
e75c13c3 BB |
2738 | default: |
2739 | break; | |
d164b209 | 2740 | case ARC_SPACE_DATA: |
37fb3e43 | 2741 | aggsum_add(&astat_data_size, space); |
d164b209 | 2742 | break; |
cc7f677c | 2743 | case ARC_SPACE_META: |
37fb3e43 | 2744 | aggsum_add(&astat_metadata_size, space); |
cc7f677c | 2745 | break; |
25458cbe | 2746 | case ARC_SPACE_BONUS: |
37fb3e43 | 2747 | aggsum_add(&astat_bonus_size, space); |
25458cbe TC |
2748 | break; |
2749 | case ARC_SPACE_DNODE: | |
37fb3e43 | 2750 | aggsum_add(&astat_dnode_size, space); |
25458cbe TC |
2751 | break; |
2752 | case ARC_SPACE_DBUF: | |
37fb3e43 | 2753 | aggsum_add(&astat_dbuf_size, space); |
d164b209 BB |
2754 | break; |
2755 | case ARC_SPACE_HDRS: | |
37fb3e43 | 2756 | aggsum_add(&astat_hdr_size, space); |
d164b209 BB |
2757 | break; |
2758 | case ARC_SPACE_L2HDRS: | |
37fb3e43 | 2759 | aggsum_add(&astat_l2_hdr_size, space); |
d164b209 BB |
2760 | break; |
2761 | } | |
2762 | ||
500445c0 | 2763 | if (type != ARC_SPACE_DATA) |
37fb3e43 | 2764 | aggsum_add(&arc_meta_used, space); |
cc7f677c | 2765 | |
37fb3e43 | 2766 | aggsum_add(&arc_size, space); |
34dc7c2f BB |
2767 | } |
2768 | ||
2769 | void | |
d164b209 | 2770 | arc_space_return(uint64_t space, arc_space_type_t type) |
34dc7c2f | 2771 | { |
d164b209 BB |
2772 | ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); |
2773 | ||
2774 | switch (type) { | |
e75c13c3 BB |
2775 | default: |
2776 | break; | |
d164b209 | 2777 | case ARC_SPACE_DATA: |
37fb3e43 | 2778 | aggsum_add(&astat_data_size, -space); |
d164b209 | 2779 | break; |
cc7f677c | 2780 | case ARC_SPACE_META: |
37fb3e43 | 2781 | aggsum_add(&astat_metadata_size, -space); |
cc7f677c | 2782 | break; |
25458cbe | 2783 | case ARC_SPACE_BONUS: |
37fb3e43 | 2784 | aggsum_add(&astat_bonus_size, -space); |
25458cbe TC |
2785 | break; |
2786 | case ARC_SPACE_DNODE: | |
37fb3e43 | 2787 | aggsum_add(&astat_dnode_size, -space); |
25458cbe TC |
2788 | break; |
2789 | case ARC_SPACE_DBUF: | |
37fb3e43 | 2790 | aggsum_add(&astat_dbuf_size, -space); |
d164b209 BB |
2791 | break; |
2792 | case ARC_SPACE_HDRS: | |
37fb3e43 | 2793 | aggsum_add(&astat_hdr_size, -space); |
d164b209 BB |
2794 | break; |
2795 | case ARC_SPACE_L2HDRS: | |
37fb3e43 | 2796 | aggsum_add(&astat_l2_hdr_size, -space); |
d164b209 BB |
2797 | break; |
2798 | } | |
2799 | ||
cc7f677c | 2800 | if (type != ARC_SPACE_DATA) { |
37fb3e43 PD |
2801 | ASSERT(aggsum_compare(&arc_meta_used, space) >= 0); |
2802 | /* | |
2803 | * We use the upper bound here rather than the precise value | |
2804 | * because the arc_meta_max value doesn't need to be | |
2805 | * precise. It's only consumed by humans via arcstats. | |
2806 | */ | |
2807 | if (arc_meta_max < aggsum_upper_bound(&arc_meta_used)) | |
2808 | arc_meta_max = aggsum_upper_bound(&arc_meta_used); | |
2809 | aggsum_add(&arc_meta_used, -space); | |
cc7f677c PS |
2810 | } |
2811 | ||
37fb3e43 PD |
2812 | ASSERT(aggsum_compare(&arc_size, space) >= 0); |
2813 | aggsum_add(&arc_size, -space); | |
34dc7c2f BB |
2814 | } |
2815 | ||
d3c2ae1c | 2816 | /* |
524b4217 | 2817 | * Given a hdr and a buf, returns whether that buf can share its b_data buffer |
a6255b7f | 2818 | * with the hdr's b_pabd. |
d3c2ae1c | 2819 | */ |
524b4217 DK |
2820 | static boolean_t |
2821 | arc_can_share(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
2822 | { | |
524b4217 DK |
2823 | /* |
2824 | * The criteria for sharing a hdr's data are: | |
b5256303 TC |
2825 | * 1. the buffer is not encrypted |
2826 | * 2. the hdr's compression matches the buf's compression | |
2827 | * 3. the hdr doesn't need to be byteswapped | |
2828 | * 4. the hdr isn't already being shared | |
2829 | * 5. the buf is either compressed or it is the last buf in the hdr list | |
524b4217 | 2830 | * |
b5256303 | 2831 | * Criterion #5 maintains the invariant that shared uncompressed |
524b4217 DK |
2832 | * bufs must be the final buf in the hdr's b_buf list. Reading this, you |
2833 | * might ask, "if a compressed buf is allocated first, won't that be the | |
2834 | * last thing in the list?", but in that case it's impossible to create | |
2835 | * a shared uncompressed buf anyway (because the hdr must be compressed | |
2836 | * to have the compressed buf). You might also think that #3 is | |
2837 | * sufficient to make this guarantee, however it's possible | |
2838 | * (specifically in the rare L2ARC write race mentioned in | |
2839 | * arc_buf_alloc_impl()) there will be an existing uncompressed buf that | |
2840 | * is sharable, but wasn't at the time of its allocation. Rather than | |
2841 | * allow a new shared uncompressed buf to be created and then shuffle | |
2842 | * the list around to make it the last element, this simply disallows | |
2843 | * sharing if the new buf isn't the first to be added. | |
2844 | */ | |
2845 | ASSERT3P(buf->b_hdr, ==, hdr); | |
b5256303 TC |
2846 | boolean_t hdr_compressed = |
2847 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF; | |
a7004725 | 2848 | boolean_t buf_compressed = ARC_BUF_COMPRESSED(buf) != 0; |
b5256303 TC |
2849 | return (!ARC_BUF_ENCRYPTED(buf) && |
2850 | buf_compressed == hdr_compressed && | |
524b4217 DK |
2851 | hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS && |
2852 | !HDR_SHARED_DATA(hdr) && | |
2853 | (ARC_BUF_LAST(buf) || ARC_BUF_COMPRESSED(buf))); | |
2854 | } | |
2855 | ||
2856 | /* | |
2857 | * Allocate a buf for this hdr. If you care about the data that's in the hdr, | |
2858 | * or if you want a compressed buffer, pass those flags in. Returns 0 if the | |
2859 | * copy was made successfully, or an error code otherwise. | |
2860 | */ | |
2861 | static int | |
be9a5c35 TC |
2862 | arc_buf_alloc_impl(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb, |
2863 | void *tag, boolean_t encrypted, boolean_t compressed, boolean_t noauth, | |
524b4217 | 2864 | boolean_t fill, arc_buf_t **ret) |
34dc7c2f | 2865 | { |
34dc7c2f | 2866 | arc_buf_t *buf; |
b5256303 | 2867 | arc_fill_flags_t flags = ARC_FILL_LOCKED; |
34dc7c2f | 2868 | |
d3c2ae1c GW |
2869 | ASSERT(HDR_HAS_L1HDR(hdr)); |
2870 | ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); | |
2871 | VERIFY(hdr->b_type == ARC_BUFC_DATA || | |
2872 | hdr->b_type == ARC_BUFC_METADATA); | |
524b4217 DK |
2873 | ASSERT3P(ret, !=, NULL); |
2874 | ASSERT3P(*ret, ==, NULL); | |
b5256303 | 2875 | IMPLY(encrypted, compressed); |
d3c2ae1c | 2876 | |
b9541d6b CW |
2877 | hdr->b_l1hdr.b_mru_hits = 0; |
2878 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
2879 | hdr->b_l1hdr.b_mfu_hits = 0; | |
2880 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
2881 | hdr->b_l1hdr.b_l2_hits = 0; | |
2882 | ||
524b4217 | 2883 | buf = *ret = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); |
34dc7c2f BB |
2884 | buf->b_hdr = hdr; |
2885 | buf->b_data = NULL; | |
2aa34383 | 2886 | buf->b_next = hdr->b_l1hdr.b_buf; |
524b4217 | 2887 | buf->b_flags = 0; |
b9541d6b | 2888 | |
d3c2ae1c GW |
2889 | add_reference(hdr, tag); |
2890 | ||
2891 | /* | |
2892 | * We're about to change the hdr's b_flags. We must either | |
2893 | * hold the hash_lock or be undiscoverable. | |
2894 | */ | |
ca6c7a94 | 2895 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c GW |
2896 | |
2897 | /* | |
524b4217 | 2898 | * Only honor requests for compressed bufs if the hdr is actually |
b5256303 TC |
2899 | * compressed. This must be overriden if the buffer is encrypted since |
2900 | * encrypted buffers cannot be decompressed. | |
524b4217 | 2901 | */ |
b5256303 TC |
2902 | if (encrypted) { |
2903 | buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; | |
2904 | buf->b_flags |= ARC_BUF_FLAG_ENCRYPTED; | |
2905 | flags |= ARC_FILL_COMPRESSED | ARC_FILL_ENCRYPTED; | |
2906 | } else if (compressed && | |
2907 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) { | |
524b4217 | 2908 | buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; |
b5256303 TC |
2909 | flags |= ARC_FILL_COMPRESSED; |
2910 | } | |
2911 | ||
2912 | if (noauth) { | |
2913 | ASSERT0(encrypted); | |
2914 | flags |= ARC_FILL_NOAUTH; | |
2915 | } | |
524b4217 | 2916 | |
524b4217 DK |
2917 | /* |
2918 | * If the hdr's data can be shared then we share the data buffer and | |
2919 | * set the appropriate bit in the hdr's b_flags to indicate the hdr is | |
2aa34383 | 2920 | * allocate a new buffer to store the buf's data. |
524b4217 | 2921 | * |
a6255b7f DQ |
2922 | * There are two additional restrictions here because we're sharing |
2923 | * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be | |
2924 | * actively involved in an L2ARC write, because if this buf is used by | |
2925 | * an arc_write() then the hdr's data buffer will be released when the | |
524b4217 | 2926 | * write completes, even though the L2ARC write might still be using it. |
a6255b7f DQ |
2927 | * Second, the hdr's ABD must be linear so that the buf's user doesn't |
2928 | * need to be ABD-aware. | |
d3c2ae1c | 2929 | */ |
a7004725 | 2930 | boolean_t can_share = arc_can_share(hdr, buf) && !HDR_L2_WRITING(hdr) && |
b5256303 | 2931 | hdr->b_l1hdr.b_pabd != NULL && abd_is_linear(hdr->b_l1hdr.b_pabd); |
524b4217 DK |
2932 | |
2933 | /* Set up b_data and sharing */ | |
2934 | if (can_share) { | |
a6255b7f | 2935 | buf->b_data = abd_to_buf(hdr->b_l1hdr.b_pabd); |
524b4217 | 2936 | buf->b_flags |= ARC_BUF_FLAG_SHARED; |
d3c2ae1c GW |
2937 | arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); |
2938 | } else { | |
524b4217 DK |
2939 | buf->b_data = |
2940 | arc_get_data_buf(hdr, arc_buf_size(buf), buf); | |
2941 | ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); | |
d3c2ae1c GW |
2942 | } |
2943 | VERIFY3P(buf->b_data, !=, NULL); | |
b9541d6b CW |
2944 | |
2945 | hdr->b_l1hdr.b_buf = buf; | |
d3c2ae1c | 2946 | hdr->b_l1hdr.b_bufcnt += 1; |
b5256303 TC |
2947 | if (encrypted) |
2948 | hdr->b_crypt_hdr.b_ebufcnt += 1; | |
b9541d6b | 2949 | |
524b4217 DK |
2950 | /* |
2951 | * If the user wants the data from the hdr, we need to either copy or | |
2952 | * decompress the data. | |
2953 | */ | |
2954 | if (fill) { | |
be9a5c35 TC |
2955 | ASSERT3P(zb, !=, NULL); |
2956 | return (arc_buf_fill(buf, spa, zb, flags)); | |
524b4217 | 2957 | } |
d3c2ae1c | 2958 | |
524b4217 | 2959 | return (0); |
34dc7c2f BB |
2960 | } |
2961 | ||
9babb374 BB |
2962 | static char *arc_onloan_tag = "onloan"; |
2963 | ||
a7004725 DK |
2964 | static inline void |
2965 | arc_loaned_bytes_update(int64_t delta) | |
2966 | { | |
2967 | atomic_add_64(&arc_loaned_bytes, delta); | |
2968 | ||
2969 | /* assert that it did not wrap around */ | |
2970 | ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); | |
2971 | } | |
2972 | ||
9babb374 BB |
2973 | /* |
2974 | * Loan out an anonymous arc buffer. Loaned buffers are not counted as in | |
2975 | * flight data by arc_tempreserve_space() until they are "returned". Loaned | |
2976 | * buffers must be returned to the arc before they can be used by the DMU or | |
2977 | * freed. | |
2978 | */ | |
2979 | arc_buf_t * | |
2aa34383 | 2980 | arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size) |
9babb374 | 2981 | { |
2aa34383 DK |
2982 | arc_buf_t *buf = arc_alloc_buf(spa, arc_onloan_tag, |
2983 | is_metadata ? ARC_BUFC_METADATA : ARC_BUFC_DATA, size); | |
9babb374 | 2984 | |
5152a740 | 2985 | arc_loaned_bytes_update(arc_buf_size(buf)); |
a7004725 | 2986 | |
9babb374 BB |
2987 | return (buf); |
2988 | } | |
2989 | ||
2aa34383 DK |
2990 | arc_buf_t * |
2991 | arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize, | |
2992 | enum zio_compress compression_type) | |
2993 | { | |
2994 | arc_buf_t *buf = arc_alloc_compressed_buf(spa, arc_onloan_tag, | |
2995 | psize, lsize, compression_type); | |
2996 | ||
5152a740 | 2997 | arc_loaned_bytes_update(arc_buf_size(buf)); |
a7004725 | 2998 | |
2aa34383 DK |
2999 | return (buf); |
3000 | } | |
3001 | ||
b5256303 TC |
3002 | arc_buf_t * |
3003 | arc_loan_raw_buf(spa_t *spa, uint64_t dsobj, boolean_t byteorder, | |
3004 | const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, | |
3005 | dmu_object_type_t ot, uint64_t psize, uint64_t lsize, | |
3006 | enum zio_compress compression_type) | |
3007 | { | |
3008 | arc_buf_t *buf = arc_alloc_raw_buf(spa, arc_onloan_tag, dsobj, | |
3009 | byteorder, salt, iv, mac, ot, psize, lsize, compression_type); | |
3010 | ||
3011 | atomic_add_64(&arc_loaned_bytes, psize); | |
3012 | return (buf); | |
3013 | } | |
3014 | ||
2aa34383 | 3015 | |
9babb374 BB |
3016 | /* |
3017 | * Return a loaned arc buffer to the arc. | |
3018 | */ | |
3019 | void | |
3020 | arc_return_buf(arc_buf_t *buf, void *tag) | |
3021 | { | |
3022 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
3023 | ||
d3c2ae1c | 3024 | ASSERT3P(buf->b_data, !=, NULL); |
b9541d6b | 3025 | ASSERT(HDR_HAS_L1HDR(hdr)); |
c13060e4 | 3026 | (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag); |
424fd7c3 | 3027 | (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); |
9babb374 | 3028 | |
a7004725 | 3029 | arc_loaned_bytes_update(-arc_buf_size(buf)); |
9babb374 BB |
3030 | } |
3031 | ||
428870ff BB |
3032 | /* Detach an arc_buf from a dbuf (tag) */ |
3033 | void | |
3034 | arc_loan_inuse_buf(arc_buf_t *buf, void *tag) | |
3035 | { | |
b9541d6b | 3036 | arc_buf_hdr_t *hdr = buf->b_hdr; |
428870ff | 3037 | |
d3c2ae1c | 3038 | ASSERT3P(buf->b_data, !=, NULL); |
b9541d6b | 3039 | ASSERT(HDR_HAS_L1HDR(hdr)); |
c13060e4 | 3040 | (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); |
424fd7c3 | 3041 | (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); |
428870ff | 3042 | |
a7004725 | 3043 | arc_loaned_bytes_update(arc_buf_size(buf)); |
428870ff BB |
3044 | } |
3045 | ||
d3c2ae1c | 3046 | static void |
a6255b7f | 3047 | l2arc_free_abd_on_write(abd_t *abd, size_t size, arc_buf_contents_t type) |
34dc7c2f | 3048 | { |
d3c2ae1c | 3049 | l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP); |
34dc7c2f | 3050 | |
a6255b7f | 3051 | df->l2df_abd = abd; |
d3c2ae1c GW |
3052 | df->l2df_size = size; |
3053 | df->l2df_type = type; | |
3054 | mutex_enter(&l2arc_free_on_write_mtx); | |
3055 | list_insert_head(l2arc_free_on_write, df); | |
3056 | mutex_exit(&l2arc_free_on_write_mtx); | |
3057 | } | |
428870ff | 3058 | |
d3c2ae1c | 3059 | static void |
b5256303 | 3060 | arc_hdr_free_on_write(arc_buf_hdr_t *hdr, boolean_t free_rdata) |
d3c2ae1c GW |
3061 | { |
3062 | arc_state_t *state = hdr->b_l1hdr.b_state; | |
3063 | arc_buf_contents_t type = arc_buf_type(hdr); | |
b5256303 | 3064 | uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr); |
1eb5bfa3 | 3065 | |
d3c2ae1c GW |
3066 | /* protected by hash lock, if in the hash table */ |
3067 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 | 3068 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
3069 | ASSERT(state != arc_anon && state != arc_l2c_only); |
3070 | ||
424fd7c3 | 3071 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c | 3072 | size, hdr); |
1eb5bfa3 | 3073 | } |
424fd7c3 | 3074 | (void) zfs_refcount_remove_many(&state->arcs_size, size, hdr); |
423e7b62 AG |
3075 | if (type == ARC_BUFC_METADATA) { |
3076 | arc_space_return(size, ARC_SPACE_META); | |
3077 | } else { | |
3078 | ASSERT(type == ARC_BUFC_DATA); | |
3079 | arc_space_return(size, ARC_SPACE_DATA); | |
3080 | } | |
d3c2ae1c | 3081 | |
b5256303 TC |
3082 | if (free_rdata) { |
3083 | l2arc_free_abd_on_write(hdr->b_crypt_hdr.b_rabd, size, type); | |
3084 | } else { | |
3085 | l2arc_free_abd_on_write(hdr->b_l1hdr.b_pabd, size, type); | |
3086 | } | |
34dc7c2f BB |
3087 | } |
3088 | ||
d3c2ae1c GW |
3089 | /* |
3090 | * Share the arc_buf_t's data with the hdr. Whenever we are sharing the | |
3091 | * data buffer, we transfer the refcount ownership to the hdr and update | |
3092 | * the appropriate kstats. | |
3093 | */ | |
3094 | static void | |
3095 | arc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
34dc7c2f | 3096 | { |
524b4217 | 3097 | ASSERT(arc_can_share(hdr, buf)); |
a6255b7f | 3098 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 3099 | ASSERT(!ARC_BUF_ENCRYPTED(buf)); |
ca6c7a94 | 3100 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
34dc7c2f BB |
3101 | |
3102 | /* | |
d3c2ae1c GW |
3103 | * Start sharing the data buffer. We transfer the |
3104 | * refcount ownership to the hdr since it always owns | |
3105 | * the refcount whenever an arc_buf_t is shared. | |
34dc7c2f | 3106 | */ |
d7e4b30a BB |
3107 | zfs_refcount_transfer_ownership_many(&hdr->b_l1hdr.b_state->arcs_size, |
3108 | arc_hdr_size(hdr), buf, hdr); | |
a6255b7f DQ |
3109 | hdr->b_l1hdr.b_pabd = abd_get_from_buf(buf->b_data, arc_buf_size(buf)); |
3110 | abd_take_ownership_of_buf(hdr->b_l1hdr.b_pabd, | |
3111 | HDR_ISTYPE_METADATA(hdr)); | |
d3c2ae1c | 3112 | arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); |
524b4217 | 3113 | buf->b_flags |= ARC_BUF_FLAG_SHARED; |
34dc7c2f | 3114 | |
d3c2ae1c GW |
3115 | /* |
3116 | * Since we've transferred ownership to the hdr we need | |
3117 | * to increment its compressed and uncompressed kstats and | |
3118 | * decrement the overhead size. | |
3119 | */ | |
3120 | ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); | |
3121 | ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); | |
2aa34383 | 3122 | ARCSTAT_INCR(arcstat_overhead_size, -arc_buf_size(buf)); |
34dc7c2f BB |
3123 | } |
3124 | ||
ca0bf58d | 3125 | static void |
d3c2ae1c | 3126 | arc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) |
ca0bf58d | 3127 | { |
d3c2ae1c | 3128 | ASSERT(arc_buf_is_shared(buf)); |
a6255b7f | 3129 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
ca6c7a94 | 3130 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
ca0bf58d | 3131 | |
d3c2ae1c GW |
3132 | /* |
3133 | * We are no longer sharing this buffer so we need | |
3134 | * to transfer its ownership to the rightful owner. | |
3135 | */ | |
d7e4b30a BB |
3136 | zfs_refcount_transfer_ownership_many(&hdr->b_l1hdr.b_state->arcs_size, |
3137 | arc_hdr_size(hdr), hdr, buf); | |
d3c2ae1c | 3138 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); |
a6255b7f DQ |
3139 | abd_release_ownership_of_buf(hdr->b_l1hdr.b_pabd); |
3140 | abd_put(hdr->b_l1hdr.b_pabd); | |
3141 | hdr->b_l1hdr.b_pabd = NULL; | |
524b4217 | 3142 | buf->b_flags &= ~ARC_BUF_FLAG_SHARED; |
d3c2ae1c GW |
3143 | |
3144 | /* | |
3145 | * Since the buffer is no longer shared between | |
3146 | * the arc buf and the hdr, count it as overhead. | |
3147 | */ | |
3148 | ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); | |
3149 | ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); | |
2aa34383 | 3150 | ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); |
ca0bf58d PS |
3151 | } |
3152 | ||
34dc7c2f | 3153 | /* |
2aa34383 DK |
3154 | * Remove an arc_buf_t from the hdr's buf list and return the last |
3155 | * arc_buf_t on the list. If no buffers remain on the list then return | |
3156 | * NULL. | |
3157 | */ | |
3158 | static arc_buf_t * | |
3159 | arc_buf_remove(arc_buf_hdr_t *hdr, arc_buf_t *buf) | |
3160 | { | |
2aa34383 | 3161 | ASSERT(HDR_HAS_L1HDR(hdr)); |
ca6c7a94 | 3162 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
2aa34383 | 3163 | |
a7004725 DK |
3164 | arc_buf_t **bufp = &hdr->b_l1hdr.b_buf; |
3165 | arc_buf_t *lastbuf = NULL; | |
3166 | ||
2aa34383 DK |
3167 | /* |
3168 | * Remove the buf from the hdr list and locate the last | |
3169 | * remaining buffer on the list. | |
3170 | */ | |
3171 | while (*bufp != NULL) { | |
3172 | if (*bufp == buf) | |
3173 | *bufp = buf->b_next; | |
3174 | ||
3175 | /* | |
3176 | * If we've removed a buffer in the middle of | |
3177 | * the list then update the lastbuf and update | |
3178 | * bufp. | |
3179 | */ | |
3180 | if (*bufp != NULL) { | |
3181 | lastbuf = *bufp; | |
3182 | bufp = &(*bufp)->b_next; | |
3183 | } | |
3184 | } | |
3185 | buf->b_next = NULL; | |
3186 | ASSERT3P(lastbuf, !=, buf); | |
3187 | IMPLY(hdr->b_l1hdr.b_bufcnt > 0, lastbuf != NULL); | |
3188 | IMPLY(hdr->b_l1hdr.b_bufcnt > 0, hdr->b_l1hdr.b_buf != NULL); | |
3189 | IMPLY(lastbuf != NULL, ARC_BUF_LAST(lastbuf)); | |
3190 | ||
3191 | return (lastbuf); | |
3192 | } | |
3193 | ||
3194 | /* | |
3195 | * Free up buf->b_data and pull the arc_buf_t off of the the arc_buf_hdr_t's | |
3196 | * list and free it. | |
34dc7c2f BB |
3197 | */ |
3198 | static void | |
2aa34383 | 3199 | arc_buf_destroy_impl(arc_buf_t *buf) |
34dc7c2f | 3200 | { |
498877ba | 3201 | arc_buf_hdr_t *hdr = buf->b_hdr; |
ca0bf58d PS |
3202 | |
3203 | /* | |
524b4217 DK |
3204 | * Free up the data associated with the buf but only if we're not |
3205 | * sharing this with the hdr. If we are sharing it with the hdr, the | |
3206 | * hdr is responsible for doing the free. | |
ca0bf58d | 3207 | */ |
d3c2ae1c GW |
3208 | if (buf->b_data != NULL) { |
3209 | /* | |
3210 | * We're about to change the hdr's b_flags. We must either | |
3211 | * hold the hash_lock or be undiscoverable. | |
3212 | */ | |
ca6c7a94 | 3213 | ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); |
d3c2ae1c | 3214 | |
524b4217 | 3215 | arc_cksum_verify(buf); |
d3c2ae1c GW |
3216 | arc_buf_unwatch(buf); |
3217 | ||
2aa34383 | 3218 | if (arc_buf_is_shared(buf)) { |
d3c2ae1c GW |
3219 | arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); |
3220 | } else { | |
2aa34383 | 3221 | uint64_t size = arc_buf_size(buf); |
d3c2ae1c GW |
3222 | arc_free_data_buf(hdr, buf->b_data, size, buf); |
3223 | ARCSTAT_INCR(arcstat_overhead_size, -size); | |
3224 | } | |
3225 | buf->b_data = NULL; | |
3226 | ||
3227 | ASSERT(hdr->b_l1hdr.b_bufcnt > 0); | |
3228 | hdr->b_l1hdr.b_bufcnt -= 1; | |
b5256303 | 3229 | |
da5d4697 | 3230 | if (ARC_BUF_ENCRYPTED(buf)) { |
b5256303 TC |
3231 | hdr->b_crypt_hdr.b_ebufcnt -= 1; |
3232 | ||
da5d4697 D |
3233 | /* |
3234 | * If we have no more encrypted buffers and we've | |
3235 | * already gotten a copy of the decrypted data we can | |
3236 | * free b_rabd to save some space. | |
3237 | */ | |
3238 | if (hdr->b_crypt_hdr.b_ebufcnt == 0 && | |
3239 | HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd != NULL && | |
3240 | !HDR_IO_IN_PROGRESS(hdr)) { | |
3241 | arc_hdr_free_abd(hdr, B_TRUE); | |
3242 | } | |
440a3eb9 | 3243 | } |
d3c2ae1c GW |
3244 | } |
3245 | ||
a7004725 | 3246 | arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); |
d3c2ae1c | 3247 | |
524b4217 | 3248 | if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) { |
2aa34383 | 3249 | /* |
524b4217 | 3250 | * If the current arc_buf_t is sharing its data buffer with the |
a6255b7f | 3251 | * hdr, then reassign the hdr's b_pabd to share it with the new |
524b4217 DK |
3252 | * buffer at the end of the list. The shared buffer is always |
3253 | * the last one on the hdr's buffer list. | |
3254 | * | |
3255 | * There is an equivalent case for compressed bufs, but since | |
3256 | * they aren't guaranteed to be the last buf in the list and | |
3257 | * that is an exceedingly rare case, we just allow that space be | |
b5256303 TC |
3258 | * wasted temporarily. We must also be careful not to share |
3259 | * encrypted buffers, since they cannot be shared. | |
2aa34383 | 3260 | */ |
b5256303 | 3261 | if (lastbuf != NULL && !ARC_BUF_ENCRYPTED(lastbuf)) { |
524b4217 | 3262 | /* Only one buf can be shared at once */ |
2aa34383 | 3263 | VERIFY(!arc_buf_is_shared(lastbuf)); |
524b4217 DK |
3264 | /* hdr is uncompressed so can't have compressed buf */ |
3265 | VERIFY(!ARC_BUF_COMPRESSED(lastbuf)); | |
d3c2ae1c | 3266 | |
a6255b7f | 3267 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
b5256303 | 3268 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c | 3269 | |
2aa34383 DK |
3270 | /* |
3271 | * We must setup a new shared block between the | |
3272 | * last buffer and the hdr. The data would have | |
3273 | * been allocated by the arc buf so we need to transfer | |
3274 | * ownership to the hdr since it's now being shared. | |
3275 | */ | |
3276 | arc_share_buf(hdr, lastbuf); | |
3277 | } | |
3278 | } else if (HDR_SHARED_DATA(hdr)) { | |
d3c2ae1c | 3279 | /* |
2aa34383 DK |
3280 | * Uncompressed shared buffers are always at the end |
3281 | * of the list. Compressed buffers don't have the | |
3282 | * same requirements. This makes it hard to | |
3283 | * simply assert that the lastbuf is shared so | |
3284 | * we rely on the hdr's compression flags to determine | |
3285 | * if we have a compressed, shared buffer. | |
d3c2ae1c | 3286 | */ |
2aa34383 DK |
3287 | ASSERT3P(lastbuf, !=, NULL); |
3288 | ASSERT(arc_buf_is_shared(lastbuf) || | |
b5256303 | 3289 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); |
ca0bf58d PS |
3290 | } |
3291 | ||
a7004725 DK |
3292 | /* |
3293 | * Free the checksum if we're removing the last uncompressed buf from | |
3294 | * this hdr. | |
3295 | */ | |
3296 | if (!arc_hdr_has_uncompressed_buf(hdr)) { | |
d3c2ae1c | 3297 | arc_cksum_free(hdr); |
a7004725 | 3298 | } |
d3c2ae1c GW |
3299 | |
3300 | /* clean up the buf */ | |
3301 | buf->b_hdr = NULL; | |
3302 | kmem_cache_free(buf_cache, buf); | |
3303 | } | |
3304 | ||
3305 | static void | |
b5256303 | 3306 | arc_hdr_alloc_abd(arc_buf_hdr_t *hdr, boolean_t alloc_rdata) |
d3c2ae1c | 3307 | { |
b5256303 TC |
3308 | uint64_t size; |
3309 | ||
d3c2ae1c GW |
3310 | ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); |
3311 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
b5256303 TC |
3312 | ASSERT(!HDR_SHARED_DATA(hdr) || alloc_rdata); |
3313 | IMPLY(alloc_rdata, HDR_PROTECTED(hdr)); | |
d3c2ae1c | 3314 | |
b5256303 TC |
3315 | if (alloc_rdata) { |
3316 | size = HDR_GET_PSIZE(hdr); | |
3317 | ASSERT3P(hdr->b_crypt_hdr.b_rabd, ==, NULL); | |
3318 | hdr->b_crypt_hdr.b_rabd = arc_get_data_abd(hdr, size, hdr); | |
3319 | ASSERT3P(hdr->b_crypt_hdr.b_rabd, !=, NULL); | |
3320 | ARCSTAT_INCR(arcstat_raw_size, size); | |
3321 | } else { | |
3322 | size = arc_hdr_size(hdr); | |
3323 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); | |
3324 | hdr->b_l1hdr.b_pabd = arc_get_data_abd(hdr, size, hdr); | |
3325 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
3326 | } | |
3327 | ||
3328 | ARCSTAT_INCR(arcstat_compressed_size, size); | |
d3c2ae1c GW |
3329 | ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); |
3330 | } | |
3331 | ||
3332 | static void | |
b5256303 | 3333 | arc_hdr_free_abd(arc_buf_hdr_t *hdr, boolean_t free_rdata) |
d3c2ae1c | 3334 | { |
b5256303 TC |
3335 | uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr); |
3336 | ||
d3c2ae1c | 3337 | ASSERT(HDR_HAS_L1HDR(hdr)); |
b5256303 TC |
3338 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); |
3339 | IMPLY(free_rdata, HDR_HAS_RABD(hdr)); | |
d3c2ae1c | 3340 | |
ca0bf58d | 3341 | /* |
d3c2ae1c GW |
3342 | * If the hdr is currently being written to the l2arc then |
3343 | * we defer freeing the data by adding it to the l2arc_free_on_write | |
3344 | * list. The l2arc will free the data once it's finished | |
3345 | * writing it to the l2arc device. | |
ca0bf58d | 3346 | */ |
d3c2ae1c | 3347 | if (HDR_L2_WRITING(hdr)) { |
b5256303 | 3348 | arc_hdr_free_on_write(hdr, free_rdata); |
d3c2ae1c | 3349 | ARCSTAT_BUMP(arcstat_l2_free_on_write); |
b5256303 TC |
3350 | } else if (free_rdata) { |
3351 | arc_free_data_abd(hdr, hdr->b_crypt_hdr.b_rabd, size, hdr); | |
d3c2ae1c | 3352 | } else { |
b5256303 | 3353 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, size, hdr); |
ca0bf58d PS |
3354 | } |
3355 | ||
b5256303 TC |
3356 | if (free_rdata) { |
3357 | hdr->b_crypt_hdr.b_rabd = NULL; | |
3358 | ARCSTAT_INCR(arcstat_raw_size, -size); | |
3359 | } else { | |
3360 | hdr->b_l1hdr.b_pabd = NULL; | |
3361 | } | |
3362 | ||
3363 | if (hdr->b_l1hdr.b_pabd == NULL && !HDR_HAS_RABD(hdr)) | |
3364 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
3365 | ||
3366 | ARCSTAT_INCR(arcstat_compressed_size, -size); | |
d3c2ae1c GW |
3367 | ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); |
3368 | } | |
3369 | ||
3370 | static arc_buf_hdr_t * | |
3371 | arc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize, | |
b5256303 TC |
3372 | boolean_t protected, enum zio_compress compression_type, |
3373 | arc_buf_contents_t type, boolean_t alloc_rdata) | |
d3c2ae1c GW |
3374 | { |
3375 | arc_buf_hdr_t *hdr; | |
3376 | ||
d3c2ae1c | 3377 | VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA); |
b5256303 TC |
3378 | if (protected) { |
3379 | hdr = kmem_cache_alloc(hdr_full_crypt_cache, KM_PUSHPAGE); | |
3380 | } else { | |
3381 | hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); | |
3382 | } | |
d3c2ae1c | 3383 | |
d3c2ae1c GW |
3384 | ASSERT(HDR_EMPTY(hdr)); |
3385 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
3386 | HDR_SET_PSIZE(hdr, psize); | |
3387 | HDR_SET_LSIZE(hdr, lsize); | |
3388 | hdr->b_spa = spa; | |
3389 | hdr->b_type = type; | |
3390 | hdr->b_flags = 0; | |
3391 | arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR); | |
2aa34383 | 3392 | arc_hdr_set_compress(hdr, compression_type); |
b5256303 TC |
3393 | if (protected) |
3394 | arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED); | |
ca0bf58d | 3395 | |
d3c2ae1c GW |
3396 | hdr->b_l1hdr.b_state = arc_anon; |
3397 | hdr->b_l1hdr.b_arc_access = 0; | |
3398 | hdr->b_l1hdr.b_bufcnt = 0; | |
3399 | hdr->b_l1hdr.b_buf = NULL; | |
ca0bf58d | 3400 | |
d3c2ae1c GW |
3401 | /* |
3402 | * Allocate the hdr's buffer. This will contain either | |
3403 | * the compressed or uncompressed data depending on the block | |
3404 | * it references and compressed arc enablement. | |
3405 | */ | |
b5256303 | 3406 | arc_hdr_alloc_abd(hdr, alloc_rdata); |
424fd7c3 | 3407 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
ca0bf58d | 3408 | |
d3c2ae1c | 3409 | return (hdr); |
ca0bf58d PS |
3410 | } |
3411 | ||
bd089c54 | 3412 | /* |
d3c2ae1c GW |
3413 | * Transition between the two allocation states for the arc_buf_hdr struct. |
3414 | * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without | |
3415 | * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller | |
3416 | * version is used when a cache buffer is only in the L2ARC in order to reduce | |
3417 | * memory usage. | |
bd089c54 | 3418 | */ |
d3c2ae1c GW |
3419 | static arc_buf_hdr_t * |
3420 | arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) | |
34dc7c2f | 3421 | { |
1c27024e DB |
3422 | ASSERT(HDR_HAS_L2HDR(hdr)); |
3423 | ||
d3c2ae1c GW |
3424 | arc_buf_hdr_t *nhdr; |
3425 | l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; | |
34dc7c2f | 3426 | |
d3c2ae1c GW |
3427 | ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || |
3428 | (old == hdr_l2only_cache && new == hdr_full_cache)); | |
34dc7c2f | 3429 | |
b5256303 TC |
3430 | /* |
3431 | * if the caller wanted a new full header and the header is to be | |
3432 | * encrypted we will actually allocate the header from the full crypt | |
3433 | * cache instead. The same applies to freeing from the old cache. | |
3434 | */ | |
3435 | if (HDR_PROTECTED(hdr) && new == hdr_full_cache) | |
3436 | new = hdr_full_crypt_cache; | |
3437 | if (HDR_PROTECTED(hdr) && old == hdr_full_cache) | |
3438 | old = hdr_full_crypt_cache; | |
3439 | ||
d3c2ae1c | 3440 | nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); |
428870ff | 3441 | |
d3c2ae1c GW |
3442 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); |
3443 | buf_hash_remove(hdr); | |
ca0bf58d | 3444 | |
d3c2ae1c | 3445 | bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); |
34dc7c2f | 3446 | |
b5256303 | 3447 | if (new == hdr_full_cache || new == hdr_full_crypt_cache) { |
d3c2ae1c GW |
3448 | arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR); |
3449 | /* | |
3450 | * arc_access and arc_change_state need to be aware that a | |
3451 | * header has just come out of L2ARC, so we set its state to | |
3452 | * l2c_only even though it's about to change. | |
3453 | */ | |
3454 | nhdr->b_l1hdr.b_state = arc_l2c_only; | |
34dc7c2f | 3455 | |
d3c2ae1c | 3456 | /* Verify previous threads set to NULL before freeing */ |
a6255b7f | 3457 | ASSERT3P(nhdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 3458 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
3459 | } else { |
3460 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
3461 | ASSERT0(hdr->b_l1hdr.b_bufcnt); | |
3462 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
36da08ef | 3463 | |
d3c2ae1c GW |
3464 | /* |
3465 | * If we've reached here, We must have been called from | |
3466 | * arc_evict_hdr(), as such we should have already been | |
3467 | * removed from any ghost list we were previously on | |
3468 | * (which protects us from racing with arc_evict_state), | |
3469 | * thus no locking is needed during this check. | |
3470 | */ | |
3471 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
1eb5bfa3 GW |
3472 | |
3473 | /* | |
d3c2ae1c GW |
3474 | * A buffer must not be moved into the arc_l2c_only |
3475 | * state if it's not finished being written out to the | |
a6255b7f | 3476 | * l2arc device. Otherwise, the b_l1hdr.b_pabd field |
d3c2ae1c | 3477 | * might try to be accessed, even though it was removed. |
1eb5bfa3 | 3478 | */ |
d3c2ae1c | 3479 | VERIFY(!HDR_L2_WRITING(hdr)); |
a6255b7f | 3480 | VERIFY3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 3481 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
3482 | |
3483 | arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR); | |
34dc7c2f | 3484 | } |
d3c2ae1c GW |
3485 | /* |
3486 | * The header has been reallocated so we need to re-insert it into any | |
3487 | * lists it was on. | |
3488 | */ | |
3489 | (void) buf_hash_insert(nhdr, NULL); | |
34dc7c2f | 3490 | |
d3c2ae1c | 3491 | ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); |
34dc7c2f | 3492 | |
d3c2ae1c GW |
3493 | mutex_enter(&dev->l2ad_mtx); |
3494 | ||
3495 | /* | |
3496 | * We must place the realloc'ed header back into the list at | |
3497 | * the same spot. Otherwise, if it's placed earlier in the list, | |
3498 | * l2arc_write_buffers() could find it during the function's | |
3499 | * write phase, and try to write it out to the l2arc. | |
3500 | */ | |
3501 | list_insert_after(&dev->l2ad_buflist, hdr, nhdr); | |
3502 | list_remove(&dev->l2ad_buflist, hdr); | |
34dc7c2f | 3503 | |
d3c2ae1c | 3504 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 3505 | |
d3c2ae1c GW |
3506 | /* |
3507 | * Since we're using the pointer address as the tag when | |
3508 | * incrementing and decrementing the l2ad_alloc refcount, we | |
3509 | * must remove the old pointer (that we're about to destroy) and | |
3510 | * add the new pointer to the refcount. Otherwise we'd remove | |
3511 | * the wrong pointer address when calling arc_hdr_destroy() later. | |
3512 | */ | |
3513 | ||
424fd7c3 TS |
3514 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, |
3515 | arc_hdr_size(hdr), hdr); | |
3516 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, | |
3517 | arc_hdr_size(nhdr), nhdr); | |
d3c2ae1c GW |
3518 | |
3519 | buf_discard_identity(hdr); | |
3520 | kmem_cache_free(old, hdr); | |
3521 | ||
3522 | return (nhdr); | |
3523 | } | |
3524 | ||
b5256303 TC |
3525 | /* |
3526 | * This function allows an L1 header to be reallocated as a crypt | |
3527 | * header and vice versa. If we are going to a crypt header, the | |
3528 | * new fields will be zeroed out. | |
3529 | */ | |
3530 | static arc_buf_hdr_t * | |
3531 | arc_hdr_realloc_crypt(arc_buf_hdr_t *hdr, boolean_t need_crypt) | |
3532 | { | |
3533 | arc_buf_hdr_t *nhdr; | |
3534 | arc_buf_t *buf; | |
3535 | kmem_cache_t *ncache, *ocache; | |
b7ddeaef | 3536 | unsigned nsize, osize; |
b5256303 | 3537 | |
b7ddeaef TC |
3538 | /* |
3539 | * This function requires that hdr is in the arc_anon state. | |
3540 | * Therefore it won't have any L2ARC data for us to worry | |
3541 | * about copying. | |
3542 | */ | |
b5256303 | 3543 | ASSERT(HDR_HAS_L1HDR(hdr)); |
b7ddeaef | 3544 | ASSERT(!HDR_HAS_L2HDR(hdr)); |
b5256303 TC |
3545 | ASSERT3U(!!HDR_PROTECTED(hdr), !=, need_crypt); |
3546 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); | |
3547 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
b7ddeaef TC |
3548 | ASSERT(!list_link_active(&hdr->b_l2hdr.b_l2node)); |
3549 | ASSERT3P(hdr->b_hash_next, ==, NULL); | |
b5256303 TC |
3550 | |
3551 | if (need_crypt) { | |
3552 | ncache = hdr_full_crypt_cache; | |
b7ddeaef | 3553 | nsize = sizeof (hdr->b_crypt_hdr); |
b5256303 | 3554 | ocache = hdr_full_cache; |
b7ddeaef | 3555 | osize = HDR_FULL_SIZE; |
b5256303 TC |
3556 | } else { |
3557 | ncache = hdr_full_cache; | |
b7ddeaef | 3558 | nsize = HDR_FULL_SIZE; |
b5256303 | 3559 | ocache = hdr_full_crypt_cache; |
b7ddeaef | 3560 | osize = sizeof (hdr->b_crypt_hdr); |
b5256303 TC |
3561 | } |
3562 | ||
3563 | nhdr = kmem_cache_alloc(ncache, KM_PUSHPAGE); | |
b7ddeaef TC |
3564 | |
3565 | /* | |
3566 | * Copy all members that aren't locks or condvars to the new header. | |
3567 | * No lists are pointing to us (as we asserted above), so we don't | |
3568 | * need to worry about the list nodes. | |
3569 | */ | |
3570 | nhdr->b_dva = hdr->b_dva; | |
3571 | nhdr->b_birth = hdr->b_birth; | |
3572 | nhdr->b_type = hdr->b_type; | |
3573 | nhdr->b_flags = hdr->b_flags; | |
3574 | nhdr->b_psize = hdr->b_psize; | |
3575 | nhdr->b_lsize = hdr->b_lsize; | |
3576 | nhdr->b_spa = hdr->b_spa; | |
b5256303 TC |
3577 | nhdr->b_l1hdr.b_freeze_cksum = hdr->b_l1hdr.b_freeze_cksum; |
3578 | nhdr->b_l1hdr.b_bufcnt = hdr->b_l1hdr.b_bufcnt; | |
3579 | nhdr->b_l1hdr.b_byteswap = hdr->b_l1hdr.b_byteswap; | |
3580 | nhdr->b_l1hdr.b_state = hdr->b_l1hdr.b_state; | |
3581 | nhdr->b_l1hdr.b_arc_access = hdr->b_l1hdr.b_arc_access; | |
3582 | nhdr->b_l1hdr.b_mru_hits = hdr->b_l1hdr.b_mru_hits; | |
3583 | nhdr->b_l1hdr.b_mru_ghost_hits = hdr->b_l1hdr.b_mru_ghost_hits; | |
3584 | nhdr->b_l1hdr.b_mfu_hits = hdr->b_l1hdr.b_mfu_hits; | |
3585 | nhdr->b_l1hdr.b_mfu_ghost_hits = hdr->b_l1hdr.b_mfu_ghost_hits; | |
3586 | nhdr->b_l1hdr.b_l2_hits = hdr->b_l1hdr.b_l2_hits; | |
3587 | nhdr->b_l1hdr.b_acb = hdr->b_l1hdr.b_acb; | |
3588 | nhdr->b_l1hdr.b_pabd = hdr->b_l1hdr.b_pabd; | |
b5256303 TC |
3589 | |
3590 | /* | |
c13060e4 | 3591 | * This zfs_refcount_add() exists only to ensure that the individual |
b5256303 TC |
3592 | * arc buffers always point to a header that is referenced, avoiding |
3593 | * a small race condition that could trigger ASSERTs. | |
3594 | */ | |
c13060e4 | 3595 | (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, FTAG); |
b7ddeaef | 3596 | nhdr->b_l1hdr.b_buf = hdr->b_l1hdr.b_buf; |
b5256303 TC |
3597 | for (buf = nhdr->b_l1hdr.b_buf; buf != NULL; buf = buf->b_next) { |
3598 | mutex_enter(&buf->b_evict_lock); | |
3599 | buf->b_hdr = nhdr; | |
3600 | mutex_exit(&buf->b_evict_lock); | |
3601 | } | |
3602 | ||
424fd7c3 TS |
3603 | zfs_refcount_transfer(&nhdr->b_l1hdr.b_refcnt, &hdr->b_l1hdr.b_refcnt); |
3604 | (void) zfs_refcount_remove(&nhdr->b_l1hdr.b_refcnt, FTAG); | |
3605 | ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt)); | |
b5256303 TC |
3606 | |
3607 | if (need_crypt) { | |
3608 | arc_hdr_set_flags(nhdr, ARC_FLAG_PROTECTED); | |
3609 | } else { | |
3610 | arc_hdr_clear_flags(nhdr, ARC_FLAG_PROTECTED); | |
3611 | } | |
3612 | ||
b7ddeaef TC |
3613 | /* unset all members of the original hdr */ |
3614 | bzero(&hdr->b_dva, sizeof (dva_t)); | |
3615 | hdr->b_birth = 0; | |
3616 | hdr->b_type = ARC_BUFC_INVALID; | |
3617 | hdr->b_flags = 0; | |
3618 | hdr->b_psize = 0; | |
3619 | hdr->b_lsize = 0; | |
3620 | hdr->b_spa = 0; | |
3621 | hdr->b_l1hdr.b_freeze_cksum = NULL; | |
3622 | hdr->b_l1hdr.b_buf = NULL; | |
3623 | hdr->b_l1hdr.b_bufcnt = 0; | |
3624 | hdr->b_l1hdr.b_byteswap = 0; | |
3625 | hdr->b_l1hdr.b_state = NULL; | |
3626 | hdr->b_l1hdr.b_arc_access = 0; | |
3627 | hdr->b_l1hdr.b_mru_hits = 0; | |
3628 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
3629 | hdr->b_l1hdr.b_mfu_hits = 0; | |
3630 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
3631 | hdr->b_l1hdr.b_l2_hits = 0; | |
3632 | hdr->b_l1hdr.b_acb = NULL; | |
3633 | hdr->b_l1hdr.b_pabd = NULL; | |
3634 | ||
3635 | if (ocache == hdr_full_crypt_cache) { | |
3636 | ASSERT(!HDR_HAS_RABD(hdr)); | |
3637 | hdr->b_crypt_hdr.b_ot = DMU_OT_NONE; | |
3638 | hdr->b_crypt_hdr.b_ebufcnt = 0; | |
3639 | hdr->b_crypt_hdr.b_dsobj = 0; | |
3640 | bzero(hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); | |
3641 | bzero(hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); | |
3642 | bzero(hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); | |
3643 | } | |
3644 | ||
b5256303 TC |
3645 | buf_discard_identity(hdr); |
3646 | kmem_cache_free(ocache, hdr); | |
3647 | ||
3648 | return (nhdr); | |
3649 | } | |
3650 | ||
3651 | /* | |
3652 | * This function is used by the send / receive code to convert a newly | |
3653 | * allocated arc_buf_t to one that is suitable for a raw encrypted write. It | |
3654 | * is also used to allow the root objset block to be uupdated without altering | |
3655 | * its embedded MACs. Both block types will always be uncompressed so we do not | |
3656 | * have to worry about compression type or psize. | |
3657 | */ | |
3658 | void | |
3659 | arc_convert_to_raw(arc_buf_t *buf, uint64_t dsobj, boolean_t byteorder, | |
3660 | dmu_object_type_t ot, const uint8_t *salt, const uint8_t *iv, | |
3661 | const uint8_t *mac) | |
3662 | { | |
3663 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
3664 | ||
3665 | ASSERT(ot == DMU_OT_DNODE || ot == DMU_OT_OBJSET); | |
3666 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
3667 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); | |
3668 | ||
3669 | buf->b_flags |= (ARC_BUF_FLAG_COMPRESSED | ARC_BUF_FLAG_ENCRYPTED); | |
3670 | if (!HDR_PROTECTED(hdr)) | |
3671 | hdr = arc_hdr_realloc_crypt(hdr, B_TRUE); | |
3672 | hdr->b_crypt_hdr.b_dsobj = dsobj; | |
3673 | hdr->b_crypt_hdr.b_ot = ot; | |
3674 | hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ? | |
3675 | DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot); | |
3676 | if (!arc_hdr_has_uncompressed_buf(hdr)) | |
3677 | arc_cksum_free(hdr); | |
3678 | ||
3679 | if (salt != NULL) | |
3680 | bcopy(salt, hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); | |
3681 | if (iv != NULL) | |
3682 | bcopy(iv, hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); | |
3683 | if (mac != NULL) | |
3684 | bcopy(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); | |
3685 | } | |
3686 | ||
d3c2ae1c GW |
3687 | /* |
3688 | * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller. | |
3689 | * The buf is returned thawed since we expect the consumer to modify it. | |
3690 | */ | |
3691 | arc_buf_t * | |
2aa34383 | 3692 | arc_alloc_buf(spa_t *spa, void *tag, arc_buf_contents_t type, int32_t size) |
d3c2ae1c | 3693 | { |
d3c2ae1c | 3694 | arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size, |
b5256303 | 3695 | B_FALSE, ZIO_COMPRESS_OFF, type, B_FALSE); |
2aa34383 | 3696 | |
a7004725 | 3697 | arc_buf_t *buf = NULL; |
be9a5c35 | 3698 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, B_FALSE, |
b5256303 | 3699 | B_FALSE, B_FALSE, &buf)); |
d3c2ae1c | 3700 | arc_buf_thaw(buf); |
2aa34383 DK |
3701 | |
3702 | return (buf); | |
3703 | } | |
3704 | ||
3705 | /* | |
3706 | * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this | |
3707 | * for bufs containing metadata. | |
3708 | */ | |
3709 | arc_buf_t * | |
3710 | arc_alloc_compressed_buf(spa_t *spa, void *tag, uint64_t psize, uint64_t lsize, | |
3711 | enum zio_compress compression_type) | |
3712 | { | |
2aa34383 DK |
3713 | ASSERT3U(lsize, >, 0); |
3714 | ASSERT3U(lsize, >=, psize); | |
b5256303 TC |
3715 | ASSERT3U(compression_type, >, ZIO_COMPRESS_OFF); |
3716 | ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS); | |
2aa34383 | 3717 | |
a7004725 | 3718 | arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, |
b5256303 | 3719 | B_FALSE, compression_type, ARC_BUFC_DATA, B_FALSE); |
2aa34383 | 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); | |
b5256303 TC |
3760 | |
3761 | hdr->b_crypt_hdr.b_dsobj = dsobj; | |
3762 | hdr->b_crypt_hdr.b_ot = ot; | |
3763 | hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ? | |
3764 | DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot); | |
3765 | bcopy(salt, hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); | |
3766 | bcopy(iv, hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); | |
3767 | bcopy(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); | |
3768 | ||
3769 | /* | |
3770 | * This buffer will be considered encrypted even if the ot is not an | |
3771 | * encrypted type. It will become authenticated instead in | |
3772 | * arc_write_ready(). | |
3773 | */ | |
3774 | buf = NULL; | |
be9a5c35 | 3775 | VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_TRUE, B_TRUE, |
b5256303 TC |
3776 | B_FALSE, B_FALSE, &buf)); |
3777 | arc_buf_thaw(buf); | |
3778 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
3779 | ||
3780 | return (buf); | |
3781 | } | |
3782 | ||
d962d5da PS |
3783 | static void |
3784 | arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) | |
3785 | { | |
3786 | l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; | |
3787 | l2arc_dev_t *dev = l2hdr->b_dev; | |
7558997d SD |
3788 | uint64_t psize = HDR_GET_PSIZE(hdr); |
3789 | uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
d962d5da PS |
3790 | |
3791 | ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); | |
3792 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
3793 | ||
3794 | list_remove(&dev->l2ad_buflist, hdr); | |
3795 | ||
01850391 AG |
3796 | ARCSTAT_INCR(arcstat_l2_psize, -psize); |
3797 | ARCSTAT_INCR(arcstat_l2_lsize, -HDR_GET_LSIZE(hdr)); | |
d962d5da | 3798 | |
7558997d | 3799 | vdev_space_update(dev->l2ad_vdev, -asize, 0, 0); |
d962d5da | 3800 | |
7558997d SD |
3801 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr), |
3802 | hdr); | |
d3c2ae1c | 3803 | arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); |
d962d5da PS |
3804 | } |
3805 | ||
34dc7c2f BB |
3806 | static void |
3807 | arc_hdr_destroy(arc_buf_hdr_t *hdr) | |
3808 | { | |
b9541d6b CW |
3809 | if (HDR_HAS_L1HDR(hdr)) { |
3810 | ASSERT(hdr->b_l1hdr.b_buf == NULL || | |
d3c2ae1c | 3811 | hdr->b_l1hdr.b_bufcnt > 0); |
424fd7c3 | 3812 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
b9541d6b CW |
3813 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
3814 | } | |
34dc7c2f | 3815 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b CW |
3816 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); |
3817 | ||
3818 | if (HDR_HAS_L2HDR(hdr)) { | |
d962d5da PS |
3819 | l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; |
3820 | boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); | |
428870ff | 3821 | |
d962d5da PS |
3822 | if (!buflist_held) |
3823 | mutex_enter(&dev->l2ad_mtx); | |
b9541d6b | 3824 | |
ca0bf58d | 3825 | /* |
d962d5da PS |
3826 | * Even though we checked this conditional above, we |
3827 | * need to check this again now that we have the | |
3828 | * l2ad_mtx. This is because we could be racing with | |
3829 | * another thread calling l2arc_evict() which might have | |
3830 | * destroyed this header's L2 portion as we were waiting | |
3831 | * to acquire the l2ad_mtx. If that happens, we don't | |
3832 | * want to re-destroy the header's L2 portion. | |
ca0bf58d | 3833 | */ |
d962d5da PS |
3834 | if (HDR_HAS_L2HDR(hdr)) |
3835 | arc_hdr_l2hdr_destroy(hdr); | |
428870ff BB |
3836 | |
3837 | if (!buflist_held) | |
d962d5da | 3838 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
3839 | } |
3840 | ||
ca6c7a94 BB |
3841 | /* |
3842 | * The header's identify can only be safely discarded once it is no | |
3843 | * longer discoverable. This requires removing it from the hash table | |
3844 | * and the l2arc header list. After this point the hash lock can not | |
3845 | * be used to protect the header. | |
3846 | */ | |
3847 | if (!HDR_EMPTY(hdr)) | |
3848 | buf_discard_identity(hdr); | |
3849 | ||
d3c2ae1c GW |
3850 | if (HDR_HAS_L1HDR(hdr)) { |
3851 | arc_cksum_free(hdr); | |
b9541d6b | 3852 | |
d3c2ae1c | 3853 | while (hdr->b_l1hdr.b_buf != NULL) |
2aa34383 | 3854 | arc_buf_destroy_impl(hdr->b_l1hdr.b_buf); |
34dc7c2f | 3855 | |
ca6c7a94 | 3856 | if (hdr->b_l1hdr.b_pabd != NULL) |
b5256303 | 3857 | arc_hdr_free_abd(hdr, B_FALSE); |
b5256303 | 3858 | |
440a3eb9 | 3859 | if (HDR_HAS_RABD(hdr)) |
b5256303 | 3860 | arc_hdr_free_abd(hdr, B_TRUE); |
b9541d6b CW |
3861 | } |
3862 | ||
34dc7c2f | 3863 | ASSERT3P(hdr->b_hash_next, ==, NULL); |
b9541d6b | 3864 | if (HDR_HAS_L1HDR(hdr)) { |
ca0bf58d | 3865 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b | 3866 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
b5256303 TC |
3867 | |
3868 | if (!HDR_PROTECTED(hdr)) { | |
3869 | kmem_cache_free(hdr_full_cache, hdr); | |
3870 | } else { | |
3871 | kmem_cache_free(hdr_full_crypt_cache, hdr); | |
3872 | } | |
b9541d6b CW |
3873 | } else { |
3874 | kmem_cache_free(hdr_l2only_cache, hdr); | |
3875 | } | |
34dc7c2f BB |
3876 | } |
3877 | ||
3878 | void | |
d3c2ae1c | 3879 | arc_buf_destroy(arc_buf_t *buf, void* tag) |
34dc7c2f BB |
3880 | { |
3881 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
34dc7c2f | 3882 | |
b9541d6b | 3883 | if (hdr->b_l1hdr.b_state == arc_anon) { |
d3c2ae1c GW |
3884 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); |
3885 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
3886 | VERIFY0(remove_reference(hdr, NULL, tag)); | |
3887 | arc_hdr_destroy(hdr); | |
3888 | return; | |
34dc7c2f BB |
3889 | } |
3890 | ||
ca6c7a94 | 3891 | kmutex_t *hash_lock = HDR_LOCK(hdr); |
34dc7c2f | 3892 | mutex_enter(hash_lock); |
ca6c7a94 | 3893 | |
d3c2ae1c GW |
3894 | ASSERT3P(hdr, ==, buf->b_hdr); |
3895 | ASSERT(hdr->b_l1hdr.b_bufcnt > 0); | |
428870ff | 3896 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
d3c2ae1c GW |
3897 | ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon); |
3898 | ASSERT3P(buf->b_data, !=, NULL); | |
34dc7c2f BB |
3899 | |
3900 | (void) remove_reference(hdr, hash_lock, tag); | |
2aa34383 | 3901 | arc_buf_destroy_impl(buf); |
34dc7c2f | 3902 | mutex_exit(hash_lock); |
34dc7c2f BB |
3903 | } |
3904 | ||
34dc7c2f | 3905 | /* |
ca0bf58d PS |
3906 | * Evict the arc_buf_hdr that is provided as a parameter. The resultant |
3907 | * state of the header is dependent on its state prior to entering this | |
3908 | * function. The following transitions are possible: | |
34dc7c2f | 3909 | * |
ca0bf58d PS |
3910 | * - arc_mru -> arc_mru_ghost |
3911 | * - arc_mfu -> arc_mfu_ghost | |
3912 | * - arc_mru_ghost -> arc_l2c_only | |
3913 | * - arc_mru_ghost -> deleted | |
3914 | * - arc_mfu_ghost -> arc_l2c_only | |
3915 | * - arc_mfu_ghost -> deleted | |
34dc7c2f | 3916 | */ |
ca0bf58d PS |
3917 | static int64_t |
3918 | arc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) | |
34dc7c2f | 3919 | { |
ca0bf58d PS |
3920 | arc_state_t *evicted_state, *state; |
3921 | int64_t bytes_evicted = 0; | |
d4a72f23 TC |
3922 | int min_lifetime = HDR_PRESCIENT_PREFETCH(hdr) ? |
3923 | arc_min_prescient_prefetch_ms : arc_min_prefetch_ms; | |
34dc7c2f | 3924 | |
ca0bf58d PS |
3925 | ASSERT(MUTEX_HELD(hash_lock)); |
3926 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
e8b96c60 | 3927 | |
ca0bf58d PS |
3928 | state = hdr->b_l1hdr.b_state; |
3929 | if (GHOST_STATE(state)) { | |
3930 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
d3c2ae1c | 3931 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
e8b96c60 MA |
3932 | |
3933 | /* | |
ca0bf58d | 3934 | * l2arc_write_buffers() relies on a header's L1 portion |
a6255b7f | 3935 | * (i.e. its b_pabd field) during it's write phase. |
ca0bf58d PS |
3936 | * Thus, we cannot push a header onto the arc_l2c_only |
3937 | * state (removing its L1 piece) until the header is | |
3938 | * done being written to the l2arc. | |
e8b96c60 | 3939 | */ |
ca0bf58d PS |
3940 | if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { |
3941 | ARCSTAT_BUMP(arcstat_evict_l2_skip); | |
3942 | return (bytes_evicted); | |
e8b96c60 MA |
3943 | } |
3944 | ||
ca0bf58d | 3945 | ARCSTAT_BUMP(arcstat_deleted); |
d3c2ae1c | 3946 | bytes_evicted += HDR_GET_LSIZE(hdr); |
428870ff | 3947 | |
ca0bf58d | 3948 | DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); |
428870ff | 3949 | |
ca0bf58d | 3950 | if (HDR_HAS_L2HDR(hdr)) { |
a6255b7f | 3951 | ASSERT(hdr->b_l1hdr.b_pabd == NULL); |
b5256303 | 3952 | ASSERT(!HDR_HAS_RABD(hdr)); |
ca0bf58d PS |
3953 | /* |
3954 | * This buffer is cached on the 2nd Level ARC; | |
3955 | * don't destroy the header. | |
3956 | */ | |
3957 | arc_change_state(arc_l2c_only, hdr, hash_lock); | |
3958 | /* | |
3959 | * dropping from L1+L2 cached to L2-only, | |
3960 | * realloc to remove the L1 header. | |
3961 | */ | |
3962 | hdr = arc_hdr_realloc(hdr, hdr_full_cache, | |
3963 | hdr_l2only_cache); | |
34dc7c2f | 3964 | } else { |
ca0bf58d PS |
3965 | arc_change_state(arc_anon, hdr, hash_lock); |
3966 | arc_hdr_destroy(hdr); | |
34dc7c2f | 3967 | } |
ca0bf58d | 3968 | return (bytes_evicted); |
34dc7c2f BB |
3969 | } |
3970 | ||
ca0bf58d PS |
3971 | ASSERT(state == arc_mru || state == arc_mfu); |
3972 | evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; | |
34dc7c2f | 3973 | |
ca0bf58d PS |
3974 | /* prefetch buffers have a minimum lifespan */ |
3975 | if (HDR_IO_IN_PROGRESS(hdr) || | |
3976 | ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && | |
2b84817f TC |
3977 | ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < |
3978 | MSEC_TO_TICK(min_lifetime))) { | |
ca0bf58d PS |
3979 | ARCSTAT_BUMP(arcstat_evict_skip); |
3980 | return (bytes_evicted); | |
da8ccd0e PS |
3981 | } |
3982 | ||
424fd7c3 | 3983 | ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt)); |
ca0bf58d PS |
3984 | while (hdr->b_l1hdr.b_buf) { |
3985 | arc_buf_t *buf = hdr->b_l1hdr.b_buf; | |
3986 | if (!mutex_tryenter(&buf->b_evict_lock)) { | |
3987 | ARCSTAT_BUMP(arcstat_mutex_miss); | |
3988 | break; | |
3989 | } | |
3990 | if (buf->b_data != NULL) | |
d3c2ae1c GW |
3991 | bytes_evicted += HDR_GET_LSIZE(hdr); |
3992 | mutex_exit(&buf->b_evict_lock); | |
2aa34383 | 3993 | arc_buf_destroy_impl(buf); |
ca0bf58d | 3994 | } |
34dc7c2f | 3995 | |
ca0bf58d | 3996 | if (HDR_HAS_L2HDR(hdr)) { |
d3c2ae1c | 3997 | ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr)); |
ca0bf58d | 3998 | } else { |
d3c2ae1c GW |
3999 | if (l2arc_write_eligible(hdr->b_spa, hdr)) { |
4000 | ARCSTAT_INCR(arcstat_evict_l2_eligible, | |
4001 | HDR_GET_LSIZE(hdr)); | |
4002 | } else { | |
4003 | ARCSTAT_INCR(arcstat_evict_l2_ineligible, | |
4004 | HDR_GET_LSIZE(hdr)); | |
4005 | } | |
ca0bf58d | 4006 | } |
34dc7c2f | 4007 | |
d3c2ae1c GW |
4008 | if (hdr->b_l1hdr.b_bufcnt == 0) { |
4009 | arc_cksum_free(hdr); | |
4010 | ||
4011 | bytes_evicted += arc_hdr_size(hdr); | |
4012 | ||
4013 | /* | |
4014 | * If this hdr is being evicted and has a compressed | |
4015 | * buffer then we discard it here before we change states. | |
4016 | * This ensures that the accounting is updated correctly | |
a6255b7f | 4017 | * in arc_free_data_impl(). |
d3c2ae1c | 4018 | */ |
b5256303 TC |
4019 | if (hdr->b_l1hdr.b_pabd != NULL) |
4020 | arc_hdr_free_abd(hdr, B_FALSE); | |
4021 | ||
4022 | if (HDR_HAS_RABD(hdr)) | |
4023 | arc_hdr_free_abd(hdr, B_TRUE); | |
d3c2ae1c | 4024 | |
ca0bf58d PS |
4025 | arc_change_state(evicted_state, hdr, hash_lock); |
4026 | ASSERT(HDR_IN_HASH_TABLE(hdr)); | |
d3c2ae1c | 4027 | arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); |
ca0bf58d PS |
4028 | DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); |
4029 | } | |
34dc7c2f | 4030 | |
ca0bf58d | 4031 | return (bytes_evicted); |
34dc7c2f BB |
4032 | } |
4033 | ||
ca0bf58d PS |
4034 | static uint64_t |
4035 | arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, | |
4036 | uint64_t spa, int64_t bytes) | |
34dc7c2f | 4037 | { |
ca0bf58d PS |
4038 | multilist_sublist_t *mls; |
4039 | uint64_t bytes_evicted = 0; | |
4040 | arc_buf_hdr_t *hdr; | |
34dc7c2f | 4041 | kmutex_t *hash_lock; |
ca0bf58d | 4042 | int evict_count = 0; |
34dc7c2f | 4043 | |
ca0bf58d | 4044 | ASSERT3P(marker, !=, NULL); |
96c080cb | 4045 | IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); |
ca0bf58d PS |
4046 | |
4047 | mls = multilist_sublist_lock(ml, idx); | |
572e2857 | 4048 | |
ca0bf58d PS |
4049 | for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL; |
4050 | hdr = multilist_sublist_prev(mls, marker)) { | |
4051 | if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) || | |
4052 | (evict_count >= zfs_arc_evict_batch_limit)) | |
4053 | break; | |
4054 | ||
4055 | /* | |
4056 | * To keep our iteration location, move the marker | |
4057 | * forward. Since we're not holding hdr's hash lock, we | |
4058 | * must be very careful and not remove 'hdr' from the | |
4059 | * sublist. Otherwise, other consumers might mistake the | |
4060 | * 'hdr' as not being on a sublist when they call the | |
4061 | * multilist_link_active() function (they all rely on | |
4062 | * the hash lock protecting concurrent insertions and | |
4063 | * removals). multilist_sublist_move_forward() was | |
4064 | * specifically implemented to ensure this is the case | |
4065 | * (only 'marker' will be removed and re-inserted). | |
4066 | */ | |
4067 | multilist_sublist_move_forward(mls, marker); | |
4068 | ||
4069 | /* | |
4070 | * The only case where the b_spa field should ever be | |
4071 | * zero, is the marker headers inserted by | |
4072 | * arc_evict_state(). It's possible for multiple threads | |
4073 | * to be calling arc_evict_state() concurrently (e.g. | |
4074 | * dsl_pool_close() and zio_inject_fault()), so we must | |
4075 | * skip any markers we see from these other threads. | |
4076 | */ | |
2a432414 | 4077 | if (hdr->b_spa == 0) |
572e2857 BB |
4078 | continue; |
4079 | ||
ca0bf58d PS |
4080 | /* we're only interested in evicting buffers of a certain spa */ |
4081 | if (spa != 0 && hdr->b_spa != spa) { | |
4082 | ARCSTAT_BUMP(arcstat_evict_skip); | |
428870ff | 4083 | continue; |
ca0bf58d PS |
4084 | } |
4085 | ||
4086 | hash_lock = HDR_LOCK(hdr); | |
e8b96c60 MA |
4087 | |
4088 | /* | |
ca0bf58d PS |
4089 | * We aren't calling this function from any code path |
4090 | * that would already be holding a hash lock, so we're | |
4091 | * asserting on this assumption to be defensive in case | |
4092 | * this ever changes. Without this check, it would be | |
4093 | * possible to incorrectly increment arcstat_mutex_miss | |
4094 | * below (e.g. if the code changed such that we called | |
4095 | * this function with a hash lock held). | |
e8b96c60 | 4096 | */ |
ca0bf58d PS |
4097 | ASSERT(!MUTEX_HELD(hash_lock)); |
4098 | ||
34dc7c2f | 4099 | if (mutex_tryenter(hash_lock)) { |
ca0bf58d PS |
4100 | uint64_t evicted = arc_evict_hdr(hdr, hash_lock); |
4101 | mutex_exit(hash_lock); | |
34dc7c2f | 4102 | |
ca0bf58d | 4103 | bytes_evicted += evicted; |
34dc7c2f | 4104 | |
572e2857 | 4105 | /* |
ca0bf58d PS |
4106 | * If evicted is zero, arc_evict_hdr() must have |
4107 | * decided to skip this header, don't increment | |
4108 | * evict_count in this case. | |
572e2857 | 4109 | */ |
ca0bf58d PS |
4110 | if (evicted != 0) |
4111 | evict_count++; | |
4112 | ||
4113 | /* | |
4114 | * If arc_size isn't overflowing, signal any | |
4115 | * threads that might happen to be waiting. | |
4116 | * | |
4117 | * For each header evicted, we wake up a single | |
4118 | * thread. If we used cv_broadcast, we could | |
4119 | * wake up "too many" threads causing arc_size | |
4120 | * to significantly overflow arc_c; since | |
a6255b7f | 4121 | * arc_get_data_impl() doesn't check for overflow |
ca0bf58d PS |
4122 | * when it's woken up (it doesn't because it's |
4123 | * possible for the ARC to be overflowing while | |
4124 | * full of un-evictable buffers, and the | |
4125 | * function should proceed in this case). | |
4126 | * | |
4127 | * If threads are left sleeping, due to not | |
3ec34e55 BL |
4128 | * using cv_broadcast here, they will be woken |
4129 | * up via cv_broadcast in arc_adjust_cb() just | |
4130 | * before arc_adjust_zthr sleeps. | |
ca0bf58d | 4131 | */ |
3ec34e55 | 4132 | mutex_enter(&arc_adjust_lock); |
ca0bf58d | 4133 | if (!arc_is_overflowing()) |
3ec34e55 BL |
4134 | cv_signal(&arc_adjust_waiters_cv); |
4135 | mutex_exit(&arc_adjust_lock); | |
e8b96c60 | 4136 | } else { |
ca0bf58d | 4137 | ARCSTAT_BUMP(arcstat_mutex_miss); |
e8b96c60 | 4138 | } |
34dc7c2f | 4139 | } |
34dc7c2f | 4140 | |
ca0bf58d | 4141 | multilist_sublist_unlock(mls); |
34dc7c2f | 4142 | |
ca0bf58d | 4143 | return (bytes_evicted); |
34dc7c2f BB |
4144 | } |
4145 | ||
ca0bf58d PS |
4146 | /* |
4147 | * Evict buffers from the given arc state, until we've removed the | |
4148 | * specified number of bytes. Move the removed buffers to the | |
4149 | * appropriate evict state. | |
4150 | * | |
4151 | * This function makes a "best effort". It skips over any buffers | |
4152 | * it can't get a hash_lock on, and so, may not catch all candidates. | |
4153 | * It may also return without evicting as much space as requested. | |
4154 | * | |
4155 | * If bytes is specified using the special value ARC_EVICT_ALL, this | |
4156 | * will evict all available (i.e. unlocked and evictable) buffers from | |
4157 | * the given arc state; which is used by arc_flush(). | |
4158 | */ | |
4159 | static uint64_t | |
4160 | arc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes, | |
4161 | arc_buf_contents_t type) | |
34dc7c2f | 4162 | { |
ca0bf58d | 4163 | uint64_t total_evicted = 0; |
64fc7762 | 4164 | multilist_t *ml = state->arcs_list[type]; |
ca0bf58d PS |
4165 | int num_sublists; |
4166 | arc_buf_hdr_t **markers; | |
ca0bf58d | 4167 | |
96c080cb | 4168 | IMPLY(bytes < 0, bytes == ARC_EVICT_ALL); |
ca0bf58d PS |
4169 | |
4170 | num_sublists = multilist_get_num_sublists(ml); | |
d164b209 BB |
4171 | |
4172 | /* | |
ca0bf58d PS |
4173 | * If we've tried to evict from each sublist, made some |
4174 | * progress, but still have not hit the target number of bytes | |
4175 | * to evict, we want to keep trying. The markers allow us to | |
4176 | * pick up where we left off for each individual sublist, rather | |
4177 | * than starting from the tail each time. | |
d164b209 | 4178 | */ |
ca0bf58d | 4179 | markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); |
1c27024e | 4180 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d | 4181 | multilist_sublist_t *mls; |
34dc7c2f | 4182 | |
ca0bf58d PS |
4183 | markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); |
4184 | ||
4185 | /* | |
4186 | * A b_spa of 0 is used to indicate that this header is | |
4187 | * a marker. This fact is used in arc_adjust_type() and | |
4188 | * arc_evict_state_impl(). | |
4189 | */ | |
4190 | markers[i]->b_spa = 0; | |
34dc7c2f | 4191 | |
ca0bf58d PS |
4192 | mls = multilist_sublist_lock(ml, i); |
4193 | multilist_sublist_insert_tail(mls, markers[i]); | |
4194 | multilist_sublist_unlock(mls); | |
34dc7c2f BB |
4195 | } |
4196 | ||
d164b209 | 4197 | /* |
ca0bf58d PS |
4198 | * While we haven't hit our target number of bytes to evict, or |
4199 | * we're evicting all available buffers. | |
d164b209 | 4200 | */ |
ca0bf58d | 4201 | while (total_evicted < bytes || bytes == ARC_EVICT_ALL) { |
25458cbe TC |
4202 | int sublist_idx = multilist_get_random_index(ml); |
4203 | uint64_t scan_evicted = 0; | |
4204 | ||
4205 | /* | |
4206 | * Try to reduce pinned dnodes with a floor of arc_dnode_limit. | |
4207 | * Request that 10% of the LRUs be scanned by the superblock | |
4208 | * shrinker. | |
4209 | */ | |
37fb3e43 PD |
4210 | if (type == ARC_BUFC_DATA && aggsum_compare(&astat_dnode_size, |
4211 | arc_dnode_limit) > 0) { | |
4212 | arc_prune_async((aggsum_upper_bound(&astat_dnode_size) - | |
4213 | arc_dnode_limit) / sizeof (dnode_t) / | |
4214 | zfs_arc_dnode_reduce_percent); | |
4215 | } | |
25458cbe | 4216 | |
ca0bf58d PS |
4217 | /* |
4218 | * Start eviction using a randomly selected sublist, | |
4219 | * this is to try and evenly balance eviction across all | |
4220 | * sublists. Always starting at the same sublist | |
4221 | * (e.g. index 0) would cause evictions to favor certain | |
4222 | * sublists over others. | |
4223 | */ | |
1c27024e | 4224 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d PS |
4225 | uint64_t bytes_remaining; |
4226 | uint64_t bytes_evicted; | |
d164b209 | 4227 | |
ca0bf58d PS |
4228 | if (bytes == ARC_EVICT_ALL) |
4229 | bytes_remaining = ARC_EVICT_ALL; | |
4230 | else if (total_evicted < bytes) | |
4231 | bytes_remaining = bytes - total_evicted; | |
4232 | else | |
4233 | break; | |
34dc7c2f | 4234 | |
ca0bf58d PS |
4235 | bytes_evicted = arc_evict_state_impl(ml, sublist_idx, |
4236 | markers[sublist_idx], spa, bytes_remaining); | |
4237 | ||
4238 | scan_evicted += bytes_evicted; | |
4239 | total_evicted += bytes_evicted; | |
4240 | ||
4241 | /* we've reached the end, wrap to the beginning */ | |
4242 | if (++sublist_idx >= num_sublists) | |
4243 | sublist_idx = 0; | |
4244 | } | |
4245 | ||
4246 | /* | |
4247 | * If we didn't evict anything during this scan, we have | |
4248 | * no reason to believe we'll evict more during another | |
4249 | * scan, so break the loop. | |
4250 | */ | |
4251 | if (scan_evicted == 0) { | |
4252 | /* This isn't possible, let's make that obvious */ | |
4253 | ASSERT3S(bytes, !=, 0); | |
34dc7c2f | 4254 | |
ca0bf58d PS |
4255 | /* |
4256 | * When bytes is ARC_EVICT_ALL, the only way to | |
4257 | * break the loop is when scan_evicted is zero. | |
4258 | * In that case, we actually have evicted enough, | |
4259 | * so we don't want to increment the kstat. | |
4260 | */ | |
4261 | if (bytes != ARC_EVICT_ALL) { | |
4262 | ASSERT3S(total_evicted, <, bytes); | |
4263 | ARCSTAT_BUMP(arcstat_evict_not_enough); | |
4264 | } | |
d164b209 | 4265 | |
ca0bf58d PS |
4266 | break; |
4267 | } | |
d164b209 | 4268 | } |
34dc7c2f | 4269 | |
1c27024e | 4270 | for (int i = 0; i < num_sublists; i++) { |
ca0bf58d PS |
4271 | multilist_sublist_t *mls = multilist_sublist_lock(ml, i); |
4272 | multilist_sublist_remove(mls, markers[i]); | |
4273 | multilist_sublist_unlock(mls); | |
34dc7c2f | 4274 | |
ca0bf58d | 4275 | kmem_cache_free(hdr_full_cache, markers[i]); |
34dc7c2f | 4276 | } |
ca0bf58d PS |
4277 | kmem_free(markers, sizeof (*markers) * num_sublists); |
4278 | ||
4279 | return (total_evicted); | |
4280 | } | |
4281 | ||
4282 | /* | |
4283 | * Flush all "evictable" data of the given type from the arc state | |
4284 | * specified. This will not evict any "active" buffers (i.e. referenced). | |
4285 | * | |
d3c2ae1c | 4286 | * When 'retry' is set to B_FALSE, the function will make a single pass |
ca0bf58d PS |
4287 | * over the state and evict any buffers that it can. Since it doesn't |
4288 | * continually retry the eviction, it might end up leaving some buffers | |
4289 | * in the ARC due to lock misses. | |
4290 | * | |
d3c2ae1c | 4291 | * When 'retry' is set to B_TRUE, the function will continually retry the |
ca0bf58d PS |
4292 | * eviction until *all* evictable buffers have been removed from the |
4293 | * state. As a result, if concurrent insertions into the state are | |
4294 | * allowed (e.g. if the ARC isn't shutting down), this function might | |
4295 | * wind up in an infinite loop, continually trying to evict buffers. | |
4296 | */ | |
4297 | static uint64_t | |
4298 | arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, | |
4299 | boolean_t retry) | |
4300 | { | |
4301 | uint64_t evicted = 0; | |
4302 | ||
424fd7c3 | 4303 | while (zfs_refcount_count(&state->arcs_esize[type]) != 0) { |
ca0bf58d PS |
4304 | evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); |
4305 | ||
4306 | if (!retry) | |
4307 | break; | |
4308 | } | |
4309 | ||
4310 | return (evicted); | |
34dc7c2f BB |
4311 | } |
4312 | ||
ab26409d | 4313 | /* |
ef5b2e10 BB |
4314 | * Helper function for arc_prune_async() it is responsible for safely |
4315 | * handling the execution of a registered arc_prune_func_t. | |
ab26409d BB |
4316 | */ |
4317 | static void | |
f6046738 | 4318 | arc_prune_task(void *ptr) |
ab26409d | 4319 | { |
f6046738 BB |
4320 | arc_prune_t *ap = (arc_prune_t *)ptr; |
4321 | arc_prune_func_t *func = ap->p_pfunc; | |
ab26409d | 4322 | |
f6046738 BB |
4323 | if (func != NULL) |
4324 | func(ap->p_adjust, ap->p_private); | |
ab26409d | 4325 | |
424fd7c3 | 4326 | zfs_refcount_remove(&ap->p_refcnt, func); |
f6046738 | 4327 | } |
ab26409d | 4328 | |
f6046738 BB |
4329 | /* |
4330 | * Notify registered consumers they must drop holds on a portion of the ARC | |
4331 | * buffered they reference. This provides a mechanism to ensure the ARC can | |
4332 | * honor the arc_meta_limit and reclaim otherwise pinned ARC buffers. This | |
4333 | * is analogous to dnlc_reduce_cache() but more generic. | |
4334 | * | |
ef5b2e10 | 4335 | * This operation is performed asynchronously so it may be safely called |
ca67b33a | 4336 | * in the context of the arc_reclaim_thread(). A reference is taken here |
f6046738 BB |
4337 | * for each registered arc_prune_t and the arc_prune_task() is responsible |
4338 | * for releasing it once the registered arc_prune_func_t has completed. | |
4339 | */ | |
4340 | static void | |
4341 | arc_prune_async(int64_t adjust) | |
4342 | { | |
4343 | arc_prune_t *ap; | |
ab26409d | 4344 | |
f6046738 BB |
4345 | mutex_enter(&arc_prune_mtx); |
4346 | for (ap = list_head(&arc_prune_list); ap != NULL; | |
4347 | ap = list_next(&arc_prune_list, ap)) { | |
ab26409d | 4348 | |
424fd7c3 | 4349 | if (zfs_refcount_count(&ap->p_refcnt) >= 2) |
f6046738 | 4350 | continue; |
ab26409d | 4351 | |
c13060e4 | 4352 | zfs_refcount_add(&ap->p_refcnt, ap->p_pfunc); |
f6046738 | 4353 | ap->p_adjust = adjust; |
b60eac3d | 4354 | if (taskq_dispatch(arc_prune_taskq, arc_prune_task, |
48d3eb40 | 4355 | ap, TQ_SLEEP) == TASKQID_INVALID) { |
424fd7c3 | 4356 | zfs_refcount_remove(&ap->p_refcnt, ap->p_pfunc); |
b60eac3d | 4357 | continue; |
4358 | } | |
f6046738 | 4359 | ARCSTAT_BUMP(arcstat_prune); |
ab26409d | 4360 | } |
ab26409d BB |
4361 | mutex_exit(&arc_prune_mtx); |
4362 | } | |
4363 | ||
ca0bf58d PS |
4364 | /* |
4365 | * Evict the specified number of bytes from the state specified, | |
4366 | * restricting eviction to the spa and type given. This function | |
4367 | * prevents us from trying to evict more from a state's list than | |
4368 | * is "evictable", and to skip evicting altogether when passed a | |
4369 | * negative value for "bytes". In contrast, arc_evict_state() will | |
4370 | * evict everything it can, when passed a negative value for "bytes". | |
4371 | */ | |
4372 | static uint64_t | |
4373 | arc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes, | |
4374 | arc_buf_contents_t type) | |
4375 | { | |
4376 | int64_t delta; | |
4377 | ||
424fd7c3 TS |
4378 | if (bytes > 0 && zfs_refcount_count(&state->arcs_esize[type]) > 0) { |
4379 | delta = MIN(zfs_refcount_count(&state->arcs_esize[type]), | |
4380 | bytes); | |
ca0bf58d PS |
4381 | return (arc_evict_state(state, spa, delta, type)); |
4382 | } | |
4383 | ||
4384 | return (0); | |
4385 | } | |
4386 | ||
4387 | /* | |
4388 | * The goal of this function is to evict enough meta data buffers from the | |
4389 | * ARC in order to enforce the arc_meta_limit. Achieving this is slightly | |
4390 | * more complicated than it appears because it is common for data buffers | |
4391 | * to have holds on meta data buffers. In addition, dnode meta data buffers | |
4392 | * will be held by the dnodes in the block preventing them from being freed. | |
4393 | * This means we can't simply traverse the ARC and expect to always find | |
4394 | * enough unheld meta data buffer to release. | |
4395 | * | |
4396 | * Therefore, this function has been updated to make alternating passes | |
4397 | * over the ARC releasing data buffers and then newly unheld meta data | |
37fb3e43 | 4398 | * buffers. This ensures forward progress is maintained and meta_used |
ca0bf58d PS |
4399 | * will decrease. Normally this is sufficient, but if required the ARC |
4400 | * will call the registered prune callbacks causing dentry and inodes to | |
4401 | * be dropped from the VFS cache. This will make dnode meta data buffers | |
4402 | * available for reclaim. | |
4403 | */ | |
4404 | static uint64_t | |
37fb3e43 | 4405 | arc_adjust_meta_balanced(uint64_t meta_used) |
ca0bf58d | 4406 | { |
25e2ab16 TC |
4407 | int64_t delta, prune = 0, adjustmnt; |
4408 | uint64_t total_evicted = 0; | |
ca0bf58d | 4409 | arc_buf_contents_t type = ARC_BUFC_DATA; |
ca67b33a | 4410 | int restarts = MAX(zfs_arc_meta_adjust_restarts, 0); |
ca0bf58d PS |
4411 | |
4412 | restart: | |
4413 | /* | |
4414 | * This slightly differs than the way we evict from the mru in | |
4415 | * arc_adjust because we don't have a "target" value (i.e. no | |
4416 | * "meta" arc_p). As a result, I think we can completely | |
4417 | * cannibalize the metadata in the MRU before we evict the | |
4418 | * metadata from the MFU. I think we probably need to implement a | |
4419 | * "metadata arc_p" value to do this properly. | |
4420 | */ | |
37fb3e43 | 4421 | adjustmnt = meta_used - arc_meta_limit; |
ca0bf58d | 4422 | |
424fd7c3 TS |
4423 | if (adjustmnt > 0 && |
4424 | zfs_refcount_count(&arc_mru->arcs_esize[type]) > 0) { | |
4425 | delta = MIN(zfs_refcount_count(&arc_mru->arcs_esize[type]), | |
d3c2ae1c | 4426 | adjustmnt); |
ca0bf58d PS |
4427 | total_evicted += arc_adjust_impl(arc_mru, 0, delta, type); |
4428 | adjustmnt -= delta; | |
4429 | } | |
4430 | ||
4431 | /* | |
4432 | * We can't afford to recalculate adjustmnt here. If we do, | |
4433 | * new metadata buffers can sneak into the MRU or ANON lists, | |
4434 | * thus penalize the MFU metadata. Although the fudge factor is | |
4435 | * small, it has been empirically shown to be significant for | |
4436 | * certain workloads (e.g. creating many empty directories). As | |
4437 | * such, we use the original calculation for adjustmnt, and | |
4438 | * simply decrement the amount of data evicted from the MRU. | |
4439 | */ | |
4440 | ||
424fd7c3 TS |
4441 | if (adjustmnt > 0 && |
4442 | zfs_refcount_count(&arc_mfu->arcs_esize[type]) > 0) { | |
4443 | delta = MIN(zfs_refcount_count(&arc_mfu->arcs_esize[type]), | |
d3c2ae1c | 4444 | adjustmnt); |
ca0bf58d PS |
4445 | total_evicted += arc_adjust_impl(arc_mfu, 0, delta, type); |
4446 | } | |
4447 | ||
37fb3e43 | 4448 | adjustmnt = meta_used - arc_meta_limit; |
ca0bf58d | 4449 | |
d3c2ae1c | 4450 | if (adjustmnt > 0 && |
424fd7c3 | 4451 | zfs_refcount_count(&arc_mru_ghost->arcs_esize[type]) > 0) { |
ca0bf58d | 4452 | delta = MIN(adjustmnt, |
424fd7c3 | 4453 | zfs_refcount_count(&arc_mru_ghost->arcs_esize[type])); |
ca0bf58d PS |
4454 | total_evicted += arc_adjust_impl(arc_mru_ghost, 0, delta, type); |
4455 | adjustmnt -= delta; | |
4456 | } | |
4457 | ||
d3c2ae1c | 4458 | if (adjustmnt > 0 && |
424fd7c3 | 4459 | zfs_refcount_count(&arc_mfu_ghost->arcs_esize[type]) > 0) { |
ca0bf58d | 4460 | delta = MIN(adjustmnt, |
424fd7c3 | 4461 | zfs_refcount_count(&arc_mfu_ghost->arcs_esize[type])); |
ca0bf58d PS |
4462 | total_evicted += arc_adjust_impl(arc_mfu_ghost, 0, delta, type); |
4463 | } | |
4464 | ||
4465 | /* | |
4466 | * If after attempting to make the requested adjustment to the ARC | |
4467 | * the meta limit is still being exceeded then request that the | |
4468 | * higher layers drop some cached objects which have holds on ARC | |
4469 | * meta buffers. Requests to the upper layers will be made with | |
4470 | * increasingly large scan sizes until the ARC is below the limit. | |
4471 | */ | |
37fb3e43 | 4472 | if (meta_used > arc_meta_limit) { |
ca0bf58d PS |
4473 | if (type == ARC_BUFC_DATA) { |
4474 | type = ARC_BUFC_METADATA; | |
4475 | } else { | |
4476 | type = ARC_BUFC_DATA; | |
4477 | ||
4478 | if (zfs_arc_meta_prune) { | |
4479 | prune += zfs_arc_meta_prune; | |
f6046738 | 4480 | arc_prune_async(prune); |
ca0bf58d PS |
4481 | } |
4482 | } | |
4483 | ||
4484 | if (restarts > 0) { | |
4485 | restarts--; | |
4486 | goto restart; | |
4487 | } | |
4488 | } | |
4489 | return (total_evicted); | |
4490 | } | |
4491 | ||
f6046738 BB |
4492 | /* |
4493 | * Evict metadata buffers from the cache, such that arc_meta_used is | |
4494 | * capped by the arc_meta_limit tunable. | |
4495 | */ | |
4496 | static uint64_t | |
37fb3e43 | 4497 | arc_adjust_meta_only(uint64_t meta_used) |
f6046738 BB |
4498 | { |
4499 | uint64_t total_evicted = 0; | |
4500 | int64_t target; | |
4501 | ||
4502 | /* | |
4503 | * If we're over the meta limit, we want to evict enough | |
4504 | * metadata to get back under the meta limit. We don't want to | |
4505 | * evict so much that we drop the MRU below arc_p, though. If | |
4506 | * we're over the meta limit more than we're over arc_p, we | |
4507 | * evict some from the MRU here, and some from the MFU below. | |
4508 | */ | |
37fb3e43 | 4509 | target = MIN((int64_t)(meta_used - arc_meta_limit), |
424fd7c3 TS |
4510 | (int64_t)(zfs_refcount_count(&arc_anon->arcs_size) + |
4511 | zfs_refcount_count(&arc_mru->arcs_size) - arc_p)); | |
f6046738 BB |
4512 | |
4513 | total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); | |
4514 | ||
4515 | /* | |
4516 | * Similar to the above, we want to evict enough bytes to get us | |
4517 | * below the meta limit, but not so much as to drop us below the | |
2aa34383 | 4518 | * space allotted to the MFU (which is defined as arc_c - arc_p). |
f6046738 | 4519 | */ |
37fb3e43 | 4520 | target = MIN((int64_t)(meta_used - arc_meta_limit), |
424fd7c3 | 4521 | (int64_t)(zfs_refcount_count(&arc_mfu->arcs_size) - |
37fb3e43 | 4522 | (arc_c - arc_p))); |
f6046738 BB |
4523 | |
4524 | total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); | |
4525 | ||
4526 | return (total_evicted); | |
4527 | } | |
4528 | ||
4529 | static uint64_t | |
37fb3e43 | 4530 | arc_adjust_meta(uint64_t meta_used) |
f6046738 BB |
4531 | { |
4532 | if (zfs_arc_meta_strategy == ARC_STRATEGY_META_ONLY) | |
37fb3e43 | 4533 | return (arc_adjust_meta_only(meta_used)); |
f6046738 | 4534 | else |
37fb3e43 | 4535 | return (arc_adjust_meta_balanced(meta_used)); |
f6046738 BB |
4536 | } |
4537 | ||
ca0bf58d PS |
4538 | /* |
4539 | * Return the type of the oldest buffer in the given arc state | |
4540 | * | |
4541 | * This function will select a random sublist of type ARC_BUFC_DATA and | |
4542 | * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist | |
4543 | * is compared, and the type which contains the "older" buffer will be | |
4544 | * returned. | |
4545 | */ | |
4546 | static arc_buf_contents_t | |
4547 | arc_adjust_type(arc_state_t *state) | |
4548 | { | |
64fc7762 MA |
4549 | multilist_t *data_ml = state->arcs_list[ARC_BUFC_DATA]; |
4550 | multilist_t *meta_ml = state->arcs_list[ARC_BUFC_METADATA]; | |
ca0bf58d PS |
4551 | int data_idx = multilist_get_random_index(data_ml); |
4552 | int meta_idx = multilist_get_random_index(meta_ml); | |
4553 | multilist_sublist_t *data_mls; | |
4554 | multilist_sublist_t *meta_mls; | |
4555 | arc_buf_contents_t type; | |
4556 | arc_buf_hdr_t *data_hdr; | |
4557 | arc_buf_hdr_t *meta_hdr; | |
4558 | ||
4559 | /* | |
4560 | * We keep the sublist lock until we're finished, to prevent | |
4561 | * the headers from being destroyed via arc_evict_state(). | |
4562 | */ | |
4563 | data_mls = multilist_sublist_lock(data_ml, data_idx); | |
4564 | meta_mls = multilist_sublist_lock(meta_ml, meta_idx); | |
4565 | ||
4566 | /* | |
4567 | * These two loops are to ensure we skip any markers that | |
4568 | * might be at the tail of the lists due to arc_evict_state(). | |
4569 | */ | |
4570 | ||
4571 | for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; | |
4572 | data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { | |
4573 | if (data_hdr->b_spa != 0) | |
4574 | break; | |
4575 | } | |
4576 | ||
4577 | for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; | |
4578 | meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { | |
4579 | if (meta_hdr->b_spa != 0) | |
4580 | break; | |
4581 | } | |
4582 | ||
4583 | if (data_hdr == NULL && meta_hdr == NULL) { | |
4584 | type = ARC_BUFC_DATA; | |
4585 | } else if (data_hdr == NULL) { | |
4586 | ASSERT3P(meta_hdr, !=, NULL); | |
4587 | type = ARC_BUFC_METADATA; | |
4588 | } else if (meta_hdr == NULL) { | |
4589 | ASSERT3P(data_hdr, !=, NULL); | |
4590 | type = ARC_BUFC_DATA; | |
4591 | } else { | |
4592 | ASSERT3P(data_hdr, !=, NULL); | |
4593 | ASSERT3P(meta_hdr, !=, NULL); | |
4594 | ||
4595 | /* The headers can't be on the sublist without an L1 header */ | |
4596 | ASSERT(HDR_HAS_L1HDR(data_hdr)); | |
4597 | ASSERT(HDR_HAS_L1HDR(meta_hdr)); | |
4598 | ||
4599 | if (data_hdr->b_l1hdr.b_arc_access < | |
4600 | meta_hdr->b_l1hdr.b_arc_access) { | |
4601 | type = ARC_BUFC_DATA; | |
4602 | } else { | |
4603 | type = ARC_BUFC_METADATA; | |
4604 | } | |
4605 | } | |
4606 | ||
4607 | multilist_sublist_unlock(meta_mls); | |
4608 | multilist_sublist_unlock(data_mls); | |
4609 | ||
4610 | return (type); | |
4611 | } | |
4612 | ||
4613 | /* | |
4614 | * Evict buffers from the cache, such that arc_size is capped by arc_c. | |
4615 | */ | |
4616 | static uint64_t | |
4617 | arc_adjust(void) | |
4618 | { | |
4619 | uint64_t total_evicted = 0; | |
4620 | uint64_t bytes; | |
4621 | int64_t target; | |
37fb3e43 PD |
4622 | uint64_t asize = aggsum_value(&arc_size); |
4623 | uint64_t ameta = aggsum_value(&arc_meta_used); | |
ca0bf58d PS |
4624 | |
4625 | /* | |
4626 | * If we're over arc_meta_limit, we want to correct that before | |
4627 | * potentially evicting data buffers below. | |
4628 | */ | |
37fb3e43 | 4629 | total_evicted += arc_adjust_meta(ameta); |
ca0bf58d PS |
4630 | |
4631 | /* | |
4632 | * Adjust MRU size | |
4633 | * | |
4634 | * If we're over the target cache size, we want to evict enough | |
4635 | * from the list to get back to our target size. We don't want | |
4636 | * to evict too much from the MRU, such that it drops below | |
4637 | * arc_p. So, if we're over our target cache size more than | |
4638 | * the MRU is over arc_p, we'll evict enough to get back to | |
4639 | * arc_p here, and then evict more from the MFU below. | |
4640 | */ | |
37fb3e43 | 4641 | target = MIN((int64_t)(asize - arc_c), |
424fd7c3 TS |
4642 | (int64_t)(zfs_refcount_count(&arc_anon->arcs_size) + |
4643 | zfs_refcount_count(&arc_mru->arcs_size) + ameta - arc_p)); | |
ca0bf58d PS |
4644 | |
4645 | /* | |
4646 | * If we're below arc_meta_min, always prefer to evict data. | |
4647 | * Otherwise, try to satisfy the requested number of bytes to | |
4648 | * evict from the type which contains older buffers; in an | |
4649 | * effort to keep newer buffers in the cache regardless of their | |
4650 | * type. If we cannot satisfy the number of bytes from this | |
4651 | * type, spill over into the next type. | |
4652 | */ | |
4653 | if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA && | |
37fb3e43 | 4654 | ameta > arc_meta_min) { |
ca0bf58d PS |
4655 | bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); |
4656 | total_evicted += bytes; | |
4657 | ||
4658 | /* | |
4659 | * If we couldn't evict our target number of bytes from | |
4660 | * metadata, we try to get the rest from data. | |
4661 | */ | |
4662 | target -= bytes; | |
4663 | ||
4664 | total_evicted += | |
4665 | arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); | |
4666 | } else { | |
4667 | bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); | |
4668 | total_evicted += bytes; | |
4669 | ||
4670 | /* | |
4671 | * If we couldn't evict our target number of bytes from | |
4672 | * data, we try to get the rest from metadata. | |
4673 | */ | |
4674 | target -= bytes; | |
4675 | ||
4676 | total_evicted += | |
4677 | arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); | |
4678 | } | |
4679 | ||
0405eeea RE |
4680 | /* |
4681 | * Re-sum ARC stats after the first round of evictions. | |
4682 | */ | |
4683 | asize = aggsum_value(&arc_size); | |
4684 | ameta = aggsum_value(&arc_meta_used); | |
4685 | ||
4686 | ||
ca0bf58d PS |
4687 | /* |
4688 | * Adjust MFU size | |
4689 | * | |
4690 | * Now that we've tried to evict enough from the MRU to get its | |
4691 | * size back to arc_p, if we're still above the target cache | |
4692 | * size, we evict the rest from the MFU. | |
4693 | */ | |
37fb3e43 | 4694 | target = asize - arc_c; |
ca0bf58d | 4695 | |
a7b10a93 | 4696 | if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA && |
37fb3e43 | 4697 | ameta > arc_meta_min) { |
ca0bf58d PS |
4698 | bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); |
4699 | total_evicted += bytes; | |
4700 | ||
4701 | /* | |
4702 | * If we couldn't evict our target number of bytes from | |
4703 | * metadata, we try to get the rest from data. | |
4704 | */ | |
4705 | target -= bytes; | |
4706 | ||
4707 | total_evicted += | |
4708 | arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); | |
4709 | } else { | |
4710 | bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); | |
4711 | total_evicted += bytes; | |
4712 | ||
4713 | /* | |
4714 | * If we couldn't evict our target number of bytes from | |
4715 | * data, we try to get the rest from data. | |
4716 | */ | |
4717 | target -= bytes; | |
4718 | ||
4719 | total_evicted += | |
4720 | arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); | |
4721 | } | |
4722 | ||
4723 | /* | |
4724 | * Adjust ghost lists | |
4725 | * | |
4726 | * In addition to the above, the ARC also defines target values | |
4727 | * for the ghost lists. The sum of the mru list and mru ghost | |
4728 | * list should never exceed the target size of the cache, and | |
4729 | * the sum of the mru list, mfu list, mru ghost list, and mfu | |
4730 | * ghost list should never exceed twice the target size of the | |
4731 | * cache. The following logic enforces these limits on the ghost | |
4732 | * caches, and evicts from them as needed. | |
4733 | */ | |
424fd7c3 TS |
4734 | target = zfs_refcount_count(&arc_mru->arcs_size) + |
4735 | zfs_refcount_count(&arc_mru_ghost->arcs_size) - arc_c; | |
ca0bf58d PS |
4736 | |
4737 | bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); | |
4738 | total_evicted += bytes; | |
4739 | ||
4740 | target -= bytes; | |
4741 | ||
4742 | total_evicted += | |
4743 | arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); | |
4744 | ||
4745 | /* | |
4746 | * We assume the sum of the mru list and mfu list is less than | |
4747 | * or equal to arc_c (we enforced this above), which means we | |
4748 | * can use the simpler of the two equations below: | |
4749 | * | |
4750 | * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c | |
4751 | * mru ghost + mfu ghost <= arc_c | |
4752 | */ | |
424fd7c3 TS |
4753 | target = zfs_refcount_count(&arc_mru_ghost->arcs_size) + |
4754 | zfs_refcount_count(&arc_mfu_ghost->arcs_size) - arc_c; | |
ca0bf58d PS |
4755 | |
4756 | bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); | |
4757 | total_evicted += bytes; | |
4758 | ||
4759 | target -= bytes; | |
4760 | ||
4761 | total_evicted += | |
4762 | arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); | |
4763 | ||
4764 | return (total_evicted); | |
4765 | } | |
4766 | ||
ca0bf58d PS |
4767 | void |
4768 | arc_flush(spa_t *spa, boolean_t retry) | |
ab26409d | 4769 | { |
ca0bf58d | 4770 | uint64_t guid = 0; |
94520ca4 | 4771 | |
bc888666 | 4772 | /* |
d3c2ae1c | 4773 | * If retry is B_TRUE, a spa must not be specified since we have |
ca0bf58d PS |
4774 | * no good way to determine if all of a spa's buffers have been |
4775 | * evicted from an arc state. | |
bc888666 | 4776 | */ |
ca0bf58d | 4777 | ASSERT(!retry || spa == 0); |
d164b209 | 4778 | |
b9541d6b | 4779 | if (spa != NULL) |
3541dc6d | 4780 | guid = spa_load_guid(spa); |
d164b209 | 4781 | |
ca0bf58d PS |
4782 | (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); |
4783 | (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); | |
4784 | ||
4785 | (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); | |
4786 | (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); | |
4787 | ||
4788 | (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); | |
4789 | (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); | |
34dc7c2f | 4790 | |
ca0bf58d PS |
4791 | (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); |
4792 | (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); | |
34dc7c2f BB |
4793 | } |
4794 | ||
3ec34e55 BL |
4795 | static void |
4796 | arc_reduce_target_size(int64_t to_free) | |
34dc7c2f | 4797 | { |
37fb3e43 | 4798 | uint64_t asize = aggsum_value(&arc_size); |
1b8951b3 | 4799 | uint64_t c = arc_c; |
34dc7c2f | 4800 | |
1b8951b3 TC |
4801 | if (c > to_free && c - to_free > arc_c_min) { |
4802 | arc_c = c - to_free; | |
ca67b33a | 4803 | atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); |
37fb3e43 PD |
4804 | if (asize < arc_c) |
4805 | arc_c = MAX(asize, arc_c_min); | |
34dc7c2f BB |
4806 | if (arc_p > arc_c) |
4807 | arc_p = (arc_c >> 1); | |
4808 | ASSERT(arc_c >= arc_c_min); | |
4809 | ASSERT((int64_t)arc_p >= 0); | |
1b8951b3 TC |
4810 | } else { |
4811 | arc_c = arc_c_min; | |
34dc7c2f BB |
4812 | } |
4813 | ||
3ec34e55 BL |
4814 | if (asize > arc_c) { |
4815 | /* See comment in arc_adjust_cb_check() on why lock+flag */ | |
4816 | mutex_enter(&arc_adjust_lock); | |
4817 | arc_adjust_needed = B_TRUE; | |
4818 | mutex_exit(&arc_adjust_lock); | |
4819 | zthr_wakeup(arc_adjust_zthr); | |
4820 | } | |
34dc7c2f | 4821 | } |
9edb3695 BB |
4822 | /* |
4823 | * Return maximum amount of memory that we could possibly use. Reduced | |
4824 | * to half of all memory in user space which is primarily used for testing. | |
4825 | */ | |
4826 | static uint64_t | |
4827 | arc_all_memory(void) | |
4828 | { | |
4829 | #ifdef _KERNEL | |
70f02287 | 4830 | #ifdef CONFIG_HIGHMEM |
031cea17 | 4831 | return (ptob(zfs_totalram_pages - totalhigh_pages)); |
70f02287 | 4832 | #else |
031cea17 | 4833 | return (ptob(zfs_totalram_pages)); |
70f02287 | 4834 | #endif /* CONFIG_HIGHMEM */ |
9edb3695 BB |
4835 | #else |
4836 | return (ptob(physmem) / 2); | |
70f02287 | 4837 | #endif /* _KERNEL */ |
9edb3695 BB |
4838 | } |
4839 | ||
70f02287 BB |
4840 | /* |
4841 | * Return the amount of memory that is considered free. In user space | |
4842 | * which is primarily used for testing we pretend that free memory ranges | |
4843 | * from 0-20% of all memory. | |
4844 | */ | |
787acae0 GDN |
4845 | static uint64_t |
4846 | arc_free_memory(void) | |
4847 | { | |
70f02287 BB |
4848 | #ifdef _KERNEL |
4849 | #ifdef CONFIG_HIGHMEM | |
4850 | struct sysinfo si; | |
4851 | si_meminfo(&si); | |
4852 | return (ptob(si.freeram - si.freehigh)); | |
4853 | #else | |
70f02287 | 4854 | return (ptob(nr_free_pages() + |
e9a77290 | 4855 | nr_inactive_file_pages() + |
4856 | nr_inactive_anon_pages() + | |
4857 | nr_slab_reclaimable_pages())); | |
4858 | ||
70f02287 BB |
4859 | #endif /* CONFIG_HIGHMEM */ |
4860 | #else | |
4861 | return (spa_get_random(arc_all_memory() * 20 / 100)); | |
4862 | #endif /* _KERNEL */ | |
787acae0 | 4863 | } |
787acae0 | 4864 | |
ca67b33a MA |
4865 | typedef enum free_memory_reason_t { |
4866 | FMR_UNKNOWN, | |
4867 | FMR_NEEDFREE, | |
4868 | FMR_LOTSFREE, | |
4869 | FMR_SWAPFS_MINFREE, | |
4870 | FMR_PAGES_PP_MAXIMUM, | |
4871 | FMR_HEAP_ARENA, | |
4872 | FMR_ZIO_ARENA, | |
4873 | } free_memory_reason_t; | |
4874 | ||
4875 | int64_t last_free_memory; | |
4876 | free_memory_reason_t last_free_reason; | |
4877 | ||
4878 | #ifdef _KERNEL | |
ca67b33a MA |
4879 | /* |
4880 | * Additional reserve of pages for pp_reserve. | |
4881 | */ | |
4882 | int64_t arc_pages_pp_reserve = 64; | |
4883 | ||
4884 | /* | |
4885 | * Additional reserve of pages for swapfs. | |
4886 | */ | |
4887 | int64_t arc_swapfs_reserve = 64; | |
ca67b33a MA |
4888 | #endif /* _KERNEL */ |
4889 | ||
4890 | /* | |
4891 | * Return the amount of memory that can be consumed before reclaim will be | |
4892 | * needed. Positive if there is sufficient free memory, negative indicates | |
4893 | * the amount of memory that needs to be freed up. | |
4894 | */ | |
4895 | static int64_t | |
4896 | arc_available_memory(void) | |
4897 | { | |
4898 | int64_t lowest = INT64_MAX; | |
4899 | free_memory_reason_t r = FMR_UNKNOWN; | |
ca67b33a | 4900 | #ifdef _KERNEL |
ca67b33a | 4901 | int64_t n; |
11f552fa | 4902 | #ifdef __linux__ |
70f02287 BB |
4903 | #ifdef freemem |
4904 | #undef freemem | |
4905 | #endif | |
11f552fa BB |
4906 | pgcnt_t needfree = btop(arc_need_free); |
4907 | pgcnt_t lotsfree = btop(arc_sys_free); | |
4908 | pgcnt_t desfree = 0; | |
70f02287 | 4909 | pgcnt_t freemem = btop(arc_free_memory()); |
9edb3695 BB |
4910 | #endif |
4911 | ||
ca67b33a MA |
4912 | if (needfree > 0) { |
4913 | n = PAGESIZE * (-needfree); | |
4914 | if (n < lowest) { | |
4915 | lowest = n; | |
4916 | r = FMR_NEEDFREE; | |
4917 | } | |
4918 | } | |
4919 | ||
4920 | /* | |
4921 | * check that we're out of range of the pageout scanner. It starts to | |
4922 | * schedule paging if freemem is less than lotsfree and needfree. | |
4923 | * lotsfree is the high-water mark for pageout, and needfree is the | |
4924 | * number of needed free pages. We add extra pages here to make sure | |
4925 | * the scanner doesn't start up while we're freeing memory. | |
4926 | */ | |
70f02287 | 4927 | n = PAGESIZE * (freemem - lotsfree - needfree - desfree); |
ca67b33a MA |
4928 | if (n < lowest) { |
4929 | lowest = n; | |
4930 | r = FMR_LOTSFREE; | |
4931 | } | |
4932 | ||
11f552fa | 4933 | #ifndef __linux__ |
ca67b33a MA |
4934 | /* |
4935 | * check to make sure that swapfs has enough space so that anon | |
4936 | * reservations can still succeed. anon_resvmem() checks that the | |
4937 | * availrmem is greater than swapfs_minfree, and the number of reserved | |
4938 | * swap pages. We also add a bit of extra here just to prevent | |
4939 | * circumstances from getting really dire. | |
4940 | */ | |
4941 | n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve - | |
4942 | desfree - arc_swapfs_reserve); | |
4943 | if (n < lowest) { | |
4944 | lowest = n; | |
4945 | r = FMR_SWAPFS_MINFREE; | |
4946 | } | |
4947 | ||
ca67b33a MA |
4948 | /* |
4949 | * Check that we have enough availrmem that memory locking (e.g., via | |
4950 | * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum | |
4951 | * stores the number of pages that cannot be locked; when availrmem | |
4952 | * drops below pages_pp_maximum, page locking mechanisms such as | |
4953 | * page_pp_lock() will fail.) | |
4954 | */ | |
4955 | n = PAGESIZE * (availrmem - pages_pp_maximum - | |
4956 | arc_pages_pp_reserve); | |
4957 | if (n < lowest) { | |
4958 | lowest = n; | |
4959 | r = FMR_PAGES_PP_MAXIMUM; | |
4960 | } | |
11f552fa | 4961 | #endif |
ca67b33a | 4962 | |
70f02287 | 4963 | #if defined(_ILP32) |
ca67b33a | 4964 | /* |
70f02287 | 4965 | * If we're on a 32-bit platform, it's possible that we'll exhaust the |
ca67b33a MA |
4966 | * kernel heap space before we ever run out of available physical |
4967 | * memory. Most checks of the size of the heap_area compare against | |
4968 | * tune.t_minarmem, which is the minimum available real memory that we | |
4969 | * can have in the system. However, this is generally fixed at 25 pages | |
4970 | * which is so low that it's useless. In this comparison, we seek to | |
4971 | * calculate the total heap-size, and reclaim if more than 3/4ths of the | |
4972 | * heap is allocated. (Or, in the calculation, if less than 1/4th is | |
4973 | * free) | |
4974 | */ | |
4975 | n = vmem_size(heap_arena, VMEM_FREE) - | |
4976 | (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2); | |
4977 | if (n < lowest) { | |
4978 | lowest = n; | |
4979 | r = FMR_HEAP_ARENA; | |
4980 | } | |
4981 | #endif | |
4982 | ||
4983 | /* | |
4984 | * If zio data pages are being allocated out of a separate heap segment, | |
4985 | * then enforce that the size of available vmem for this arena remains | |
d3c2ae1c | 4986 | * above about 1/4th (1/(2^arc_zio_arena_free_shift)) free. |
ca67b33a | 4987 | * |
d3c2ae1c GW |
4988 | * Note that reducing the arc_zio_arena_free_shift keeps more virtual |
4989 | * memory (in the zio_arena) free, which can avoid memory | |
4990 | * fragmentation issues. | |
ca67b33a MA |
4991 | */ |
4992 | if (zio_arena != NULL) { | |
9edb3695 BB |
4993 | n = (int64_t)vmem_size(zio_arena, VMEM_FREE) - |
4994 | (vmem_size(zio_arena, VMEM_ALLOC) >> | |
4995 | arc_zio_arena_free_shift); | |
ca67b33a MA |
4996 | if (n < lowest) { |
4997 | lowest = n; | |
4998 | r = FMR_ZIO_ARENA; | |
4999 | } | |
5000 | } | |
11f552fa | 5001 | #else /* _KERNEL */ |
ca67b33a MA |
5002 | /* Every 100 calls, free a small amount */ |
5003 | if (spa_get_random(100) == 0) | |
5004 | lowest = -1024; | |
11f552fa | 5005 | #endif /* _KERNEL */ |
ca67b33a MA |
5006 | |
5007 | last_free_memory = lowest; | |
5008 | last_free_reason = r; | |
5009 | ||
5010 | return (lowest); | |
5011 | } | |
5012 | ||
5013 | /* | |
5014 | * Determine if the system is under memory pressure and is asking | |
d3c2ae1c | 5015 | * to reclaim memory. A return value of B_TRUE indicates that the system |
ca67b33a MA |
5016 | * is under memory pressure and that the arc should adjust accordingly. |
5017 | */ | |
5018 | static boolean_t | |
5019 | arc_reclaim_needed(void) | |
5020 | { | |
5021 | return (arc_available_memory() < 0); | |
5022 | } | |
5023 | ||
34dc7c2f | 5024 | static void |
3ec34e55 | 5025 | arc_kmem_reap_soon(void) |
34dc7c2f BB |
5026 | { |
5027 | size_t i; | |
5028 | kmem_cache_t *prev_cache = NULL; | |
5029 | kmem_cache_t *prev_data_cache = NULL; | |
5030 | extern kmem_cache_t *zio_buf_cache[]; | |
5031 | extern kmem_cache_t *zio_data_buf_cache[]; | |
669dedb3 | 5032 | extern kmem_cache_t *range_seg_cache; |
34dc7c2f | 5033 | |
70f02287 | 5034 | #ifdef _KERNEL |
37fb3e43 PD |
5035 | if ((aggsum_compare(&arc_meta_used, arc_meta_limit) >= 0) && |
5036 | zfs_arc_meta_prune) { | |
f6046738 BB |
5037 | /* |
5038 | * We are exceeding our meta-data cache limit. | |
5039 | * Prune some entries to release holds on meta-data. | |
5040 | */ | |
ef5b2e10 | 5041 | arc_prune_async(zfs_arc_meta_prune); |
f6046738 | 5042 | } |
70f02287 BB |
5043 | #if defined(_ILP32) |
5044 | /* | |
5045 | * Reclaim unused memory from all kmem caches. | |
5046 | */ | |
5047 | kmem_reap(); | |
5048 | #endif | |
5049 | #endif | |
f6046738 | 5050 | |
34dc7c2f | 5051 | for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { |
70f02287 | 5052 | #if defined(_ILP32) |
d0c614ec | 5053 | /* reach upper limit of cache size on 32-bit */ |
5054 | if (zio_buf_cache[i] == NULL) | |
5055 | break; | |
5056 | #endif | |
34dc7c2f BB |
5057 | if (zio_buf_cache[i] != prev_cache) { |
5058 | prev_cache = zio_buf_cache[i]; | |
5059 | kmem_cache_reap_now(zio_buf_cache[i]); | |
5060 | } | |
5061 | if (zio_data_buf_cache[i] != prev_data_cache) { | |
5062 | prev_data_cache = zio_data_buf_cache[i]; | |
5063 | kmem_cache_reap_now(zio_data_buf_cache[i]); | |
5064 | } | |
5065 | } | |
ca0bf58d | 5066 | kmem_cache_reap_now(buf_cache); |
b9541d6b CW |
5067 | kmem_cache_reap_now(hdr_full_cache); |
5068 | kmem_cache_reap_now(hdr_l2only_cache); | |
669dedb3 | 5069 | kmem_cache_reap_now(range_seg_cache); |
ca67b33a MA |
5070 | |
5071 | if (zio_arena != NULL) { | |
5072 | /* | |
5073 | * Ask the vmem arena to reclaim unused memory from its | |
5074 | * quantum caches. | |
5075 | */ | |
5076 | vmem_qcache_reap(zio_arena); | |
5077 | } | |
34dc7c2f BB |
5078 | } |
5079 | ||
3ec34e55 BL |
5080 | /* ARGSUSED */ |
5081 | static boolean_t | |
5082 | arc_adjust_cb_check(void *arg, zthr_t *zthr) | |
5083 | { | |
cffa8372 JG |
5084 | /* |
5085 | * This is necessary so that any changes which may have been made to | |
5086 | * many of the zfs_arc_* module parameters will be propagated to | |
5087 | * their actual internal variable counterparts. Without this, | |
5088 | * changing those module params at runtime would have no effect. | |
5089 | */ | |
5090 | arc_tuning_update(); | |
5091 | ||
3ec34e55 BL |
5092 | /* |
5093 | * This is necessary in order to keep the kstat information | |
5094 | * up to date for tools that display kstat data such as the | |
5095 | * mdb ::arc dcmd and the Linux crash utility. These tools | |
5096 | * typically do not call kstat's update function, but simply | |
5097 | * dump out stats from the most recent update. Without | |
5098 | * this call, these commands may show stale stats for the | |
5099 | * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even | |
5100 | * with this change, the data might be up to 1 second | |
5101 | * out of date(the arc_adjust_zthr has a maximum sleep | |
5102 | * time of 1 second); but that should suffice. The | |
5103 | * arc_state_t structures can be queried directly if more | |
5104 | * accurate information is needed. | |
5105 | */ | |
5106 | if (arc_ksp != NULL) | |
5107 | arc_ksp->ks_update(arc_ksp, KSTAT_READ); | |
5108 | ||
5109 | /* | |
5110 | * We have to rely on arc_get_data_impl() to tell us when to adjust, | |
5111 | * rather than checking if we are overflowing here, so that we are | |
5112 | * sure to not leave arc_get_data_impl() waiting on | |
5113 | * arc_adjust_waiters_cv. If we have become "not overflowing" since | |
5114 | * arc_get_data_impl() checked, we need to wake it up. We could | |
5115 | * broadcast the CV here, but arc_get_data_impl() may have not yet | |
5116 | * gone to sleep. We would need to use a mutex to ensure that this | |
5117 | * function doesn't broadcast until arc_get_data_impl() has gone to | |
5118 | * sleep (e.g. the arc_adjust_lock). However, the lock ordering of | |
5119 | * such a lock would necessarily be incorrect with respect to the | |
5120 | * zthr_lock, which is held before this function is called, and is | |
5121 | * held by arc_get_data_impl() when it calls zthr_wakeup(). | |
5122 | */ | |
5123 | return (arc_adjust_needed); | |
5124 | } | |
5125 | ||
302f753f | 5126 | /* |
3ec34e55 BL |
5127 | * Keep arc_size under arc_c by running arc_adjust which evicts data |
5128 | * from the ARC. | |
302f753f | 5129 | */ |
867959b5 | 5130 | /* ARGSUSED */ |
61c3391a | 5131 | static void |
3ec34e55 | 5132 | arc_adjust_cb(void *arg, zthr_t *zthr) |
34dc7c2f | 5133 | { |
3ec34e55 BL |
5134 | uint64_t evicted = 0; |
5135 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
34dc7c2f | 5136 | |
3ec34e55 BL |
5137 | /* Evict from cache */ |
5138 | evicted = arc_adjust(); | |
34dc7c2f | 5139 | |
3ec34e55 BL |
5140 | /* |
5141 | * If evicted is zero, we couldn't evict anything | |
5142 | * via arc_adjust(). This could be due to hash lock | |
5143 | * collisions, but more likely due to the majority of | |
5144 | * arc buffers being unevictable. Therefore, even if | |
5145 | * arc_size is above arc_c, another pass is unlikely to | |
5146 | * be helpful and could potentially cause us to enter an | |
5147 | * infinite loop. Additionally, zthr_iscancelled() is | |
5148 | * checked here so that if the arc is shutting down, the | |
5149 | * broadcast will wake any remaining arc adjust waiters. | |
5150 | */ | |
5151 | mutex_enter(&arc_adjust_lock); | |
5152 | arc_adjust_needed = !zthr_iscancelled(arc_adjust_zthr) && | |
5153 | evicted > 0 && aggsum_compare(&arc_size, arc_c) > 0; | |
5154 | if (!arc_adjust_needed) { | |
d3c2ae1c | 5155 | /* |
3ec34e55 BL |
5156 | * We're either no longer overflowing, or we |
5157 | * can't evict anything more, so we should wake | |
5158 | * arc_get_data_impl() sooner. | |
d3c2ae1c | 5159 | */ |
3ec34e55 BL |
5160 | cv_broadcast(&arc_adjust_waiters_cv); |
5161 | arc_need_free = 0; | |
5162 | } | |
5163 | mutex_exit(&arc_adjust_lock); | |
5164 | spl_fstrans_unmark(cookie); | |
3ec34e55 BL |
5165 | } |
5166 | ||
5167 | /* ARGSUSED */ | |
5168 | static boolean_t | |
5169 | arc_reap_cb_check(void *arg, zthr_t *zthr) | |
5170 | { | |
5171 | int64_t free_memory = arc_available_memory(); | |
5172 | ||
5173 | /* | |
5174 | * If a kmem reap is already active, don't schedule more. We must | |
5175 | * check for this because kmem_cache_reap_soon() won't actually | |
5176 | * block on the cache being reaped (this is to prevent callers from | |
5177 | * becoming implicitly blocked by a system-wide kmem reap -- which, | |
5178 | * on a system with many, many full magazines, can take minutes). | |
5179 | */ | |
5180 | if (!kmem_cache_reap_active() && free_memory < 0) { | |
34dc7c2f | 5181 | |
3ec34e55 BL |
5182 | arc_no_grow = B_TRUE; |
5183 | arc_warm = B_TRUE; | |
0a252dae | 5184 | /* |
3ec34e55 BL |
5185 | * Wait at least zfs_grow_retry (default 5) seconds |
5186 | * before considering growing. | |
0a252dae | 5187 | */ |
3ec34e55 BL |
5188 | arc_growtime = gethrtime() + SEC2NSEC(arc_grow_retry); |
5189 | return (B_TRUE); | |
5190 | } else if (free_memory < arc_c >> arc_no_grow_shift) { | |
5191 | arc_no_grow = B_TRUE; | |
5192 | } else if (gethrtime() >= arc_growtime) { | |
5193 | arc_no_grow = B_FALSE; | |
5194 | } | |
0a252dae | 5195 | |
3ec34e55 BL |
5196 | return (B_FALSE); |
5197 | } | |
34dc7c2f | 5198 | |
3ec34e55 BL |
5199 | /* |
5200 | * Keep enough free memory in the system by reaping the ARC's kmem | |
5201 | * caches. To cause more slabs to be reapable, we may reduce the | |
5202 | * target size of the cache (arc_c), causing the arc_adjust_cb() | |
5203 | * to free more buffers. | |
5204 | */ | |
5205 | /* ARGSUSED */ | |
61c3391a | 5206 | static void |
3ec34e55 BL |
5207 | arc_reap_cb(void *arg, zthr_t *zthr) |
5208 | { | |
5209 | int64_t free_memory; | |
5210 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
34dc7c2f | 5211 | |
3ec34e55 BL |
5212 | /* |
5213 | * Kick off asynchronous kmem_reap()'s of all our caches. | |
5214 | */ | |
5215 | arc_kmem_reap_soon(); | |
6a8f9b6b | 5216 | |
3ec34e55 BL |
5217 | /* |
5218 | * Wait at least arc_kmem_cache_reap_retry_ms between | |
5219 | * arc_kmem_reap_soon() calls. Without this check it is possible to | |
5220 | * end up in a situation where we spend lots of time reaping | |
5221 | * caches, while we're near arc_c_min. Waiting here also gives the | |
5222 | * subsequent free memory check a chance of finding that the | |
5223 | * asynchronous reap has already freed enough memory, and we don't | |
5224 | * need to call arc_reduce_target_size(). | |
5225 | */ | |
5226 | delay((hz * arc_kmem_cache_reap_retry_ms + 999) / 1000); | |
34dc7c2f | 5227 | |
3ec34e55 BL |
5228 | /* |
5229 | * Reduce the target size as needed to maintain the amount of free | |
5230 | * memory in the system at a fraction of the arc_size (1/128th by | |
5231 | * default). If oversubscribed (free_memory < 0) then reduce the | |
5232 | * target arc_size by the deficit amount plus the fractional | |
5233 | * amount. If free memory is positive but less then the fractional | |
5234 | * amount, reduce by what is needed to hit the fractional amount. | |
5235 | */ | |
5236 | free_memory = arc_available_memory(); | |
34dc7c2f | 5237 | |
3ec34e55 BL |
5238 | int64_t to_free = |
5239 | (arc_c >> arc_shrink_shift) - free_memory; | |
5240 | if (to_free > 0) { | |
ca67b33a | 5241 | #ifdef _KERNEL |
3ec34e55 | 5242 | to_free = MAX(to_free, arc_need_free); |
ca67b33a | 5243 | #endif |
3ec34e55 | 5244 | arc_reduce_target_size(to_free); |
ca0bf58d | 5245 | } |
ca0bf58d | 5246 | spl_fstrans_unmark(cookie); |
ca0bf58d PS |
5247 | } |
5248 | ||
7cb67b45 BB |
5249 | #ifdef _KERNEL |
5250 | /* | |
302f753f BB |
5251 | * Determine the amount of memory eligible for eviction contained in the |
5252 | * ARC. All clean data reported by the ghost lists can always be safely | |
5253 | * evicted. Due to arc_c_min, the same does not hold for all clean data | |
5254 | * contained by the regular mru and mfu lists. | |
5255 | * | |
5256 | * In the case of the regular mru and mfu lists, we need to report as | |
5257 | * much clean data as possible, such that evicting that same reported | |
5258 | * data will not bring arc_size below arc_c_min. Thus, in certain | |
5259 | * circumstances, the total amount of clean data in the mru and mfu | |
5260 | * lists might not actually be evictable. | |
5261 | * | |
5262 | * The following two distinct cases are accounted for: | |
5263 | * | |
5264 | * 1. The sum of the amount of dirty data contained by both the mru and | |
5265 | * mfu lists, plus the ARC's other accounting (e.g. the anon list), | |
5266 | * is greater than or equal to arc_c_min. | |
5267 | * (i.e. amount of dirty data >= arc_c_min) | |
5268 | * | |
5269 | * This is the easy case; all clean data contained by the mru and mfu | |
5270 | * lists is evictable. Evicting all clean data can only drop arc_size | |
5271 | * to the amount of dirty data, which is greater than arc_c_min. | |
5272 | * | |
5273 | * 2. The sum of the amount of dirty data contained by both the mru and | |
5274 | * mfu lists, plus the ARC's other accounting (e.g. the anon list), | |
5275 | * is less than arc_c_min. | |
5276 | * (i.e. arc_c_min > amount of dirty data) | |
5277 | * | |
5278 | * 2.1. arc_size is greater than or equal arc_c_min. | |
5279 | * (i.e. arc_size >= arc_c_min > amount of dirty data) | |
5280 | * | |
5281 | * In this case, not all clean data from the regular mru and mfu | |
5282 | * lists is actually evictable; we must leave enough clean data | |
5283 | * to keep arc_size above arc_c_min. Thus, the maximum amount of | |
5284 | * evictable data from the two lists combined, is exactly the | |
5285 | * difference between arc_size and arc_c_min. | |
5286 | * | |
5287 | * 2.2. arc_size is less than arc_c_min | |
5288 | * (i.e. arc_c_min > arc_size > amount of dirty data) | |
5289 | * | |
5290 | * In this case, none of the data contained in the mru and mfu | |
5291 | * lists is evictable, even if it's clean. Since arc_size is | |
5292 | * already below arc_c_min, evicting any more would only | |
5293 | * increase this negative difference. | |
7cb67b45 | 5294 | */ |
302f753f | 5295 | static uint64_t |
4ea3f864 GM |
5296 | arc_evictable_memory(void) |
5297 | { | |
37fb3e43 | 5298 | int64_t asize = aggsum_value(&arc_size); |
302f753f | 5299 | uint64_t arc_clean = |
424fd7c3 TS |
5300 | zfs_refcount_count(&arc_mru->arcs_esize[ARC_BUFC_DATA]) + |
5301 | zfs_refcount_count(&arc_mru->arcs_esize[ARC_BUFC_METADATA]) + | |
5302 | zfs_refcount_count(&arc_mfu->arcs_esize[ARC_BUFC_DATA]) + | |
5303 | zfs_refcount_count(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); | |
37fb3e43 | 5304 | uint64_t arc_dirty = MAX((int64_t)asize - (int64_t)arc_clean, 0); |
302f753f | 5305 | |
03b60eee DB |
5306 | /* |
5307 | * Scale reported evictable memory in proportion to page cache, cap | |
5308 | * at specified min/max. | |
5309 | */ | |
e9a77290 | 5310 | uint64_t min = (ptob(nr_file_pages()) / 100) * zfs_arc_pc_percent; |
03b60eee DB |
5311 | min = MAX(arc_c_min, MIN(arc_c_max, min)); |
5312 | ||
5313 | if (arc_dirty >= min) | |
9b50146d | 5314 | return (arc_clean); |
302f753f | 5315 | |
37fb3e43 | 5316 | return (MAX((int64_t)asize - (int64_t)min, 0)); |
302f753f BB |
5317 | } |
5318 | ||
ed6e9cc2 TC |
5319 | /* |
5320 | * If sc->nr_to_scan is zero, the caller is requesting a query of the | |
5321 | * number of objects which can potentially be freed. If it is nonzero, | |
5322 | * the request is to free that many objects. | |
5323 | * | |
5324 | * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks | |
5325 | * in struct shrinker and also require the shrinker to return the number | |
5326 | * of objects freed. | |
5327 | * | |
5328 | * Older kernels require the shrinker to return the number of freeable | |
5329 | * objects following the freeing of nr_to_free. | |
5330 | */ | |
5331 | static spl_shrinker_t | |
7e7baeca | 5332 | __arc_shrinker_func(struct shrinker *shrink, struct shrink_control *sc) |
7cb67b45 | 5333 | { |
ed6e9cc2 | 5334 | int64_t pages; |
7cb67b45 | 5335 | |
302f753f BB |
5336 | /* The arc is considered warm once reclaim has occurred */ |
5337 | if (unlikely(arc_warm == B_FALSE)) | |
5338 | arc_warm = B_TRUE; | |
7cb67b45 | 5339 | |
302f753f | 5340 | /* Return the potential number of reclaimable pages */ |
ed6e9cc2 | 5341 | pages = btop((int64_t)arc_evictable_memory()); |
302f753f BB |
5342 | if (sc->nr_to_scan == 0) |
5343 | return (pages); | |
3fd70ee6 BB |
5344 | |
5345 | /* Not allowed to perform filesystem reclaim */ | |
7e7baeca | 5346 | if (!(sc->gfp_mask & __GFP_FS)) |
ed6e9cc2 | 5347 | return (SHRINK_STOP); |
3fd70ee6 | 5348 | |
7cb67b45 | 5349 | /* Reclaim in progress */ |
3ec34e55 | 5350 | if (mutex_tryenter(&arc_adjust_lock) == 0) { |
b855550c | 5351 | ARCSTAT_INCR(arcstat_need_free, ptob(sc->nr_to_scan)); |
2e91c2fb | 5352 | return (0); |
b855550c | 5353 | } |
7cb67b45 | 5354 | |
3ec34e55 | 5355 | mutex_exit(&arc_adjust_lock); |
ca0bf58d | 5356 | |
302f753f BB |
5357 | /* |
5358 | * Evict the requested number of pages by shrinking arc_c the | |
44813aef | 5359 | * requested amount. |
302f753f BB |
5360 | */ |
5361 | if (pages > 0) { | |
3ec34e55 | 5362 | arc_reduce_target_size(ptob(sc->nr_to_scan)); |
44813aef | 5363 | if (current_is_kswapd()) |
3ec34e55 | 5364 | arc_kmem_reap_soon(); |
ed6e9cc2 | 5365 | #ifdef HAVE_SPLIT_SHRINKER_CALLBACK |
4149bf49 DB |
5366 | pages = MAX((int64_t)pages - |
5367 | (int64_t)btop(arc_evictable_memory()), 0); | |
ed6e9cc2 | 5368 | #else |
1e3cb67b | 5369 | pages = btop(arc_evictable_memory()); |
ed6e9cc2 | 5370 | #endif |
1a31dcf5 DB |
5371 | /* |
5372 | * We've shrunk what we can, wake up threads. | |
5373 | */ | |
3ec34e55 | 5374 | cv_broadcast(&arc_adjust_waiters_cv); |
44813aef | 5375 | } else |
ed6e9cc2 | 5376 | pages = SHRINK_STOP; |
302f753f BB |
5377 | |
5378 | /* | |
5379 | * When direct reclaim is observed it usually indicates a rapid | |
5380 | * increase in memory pressure. This occurs because the kswapd | |
5381 | * threads were unable to asynchronously keep enough free memory | |
5382 | * available. In this case set arc_no_grow to briefly pause arc | |
5383 | * growth to avoid compounding the memory pressure. | |
5384 | */ | |
7cb67b45 | 5385 | if (current_is_kswapd()) { |
302f753f | 5386 | ARCSTAT_BUMP(arcstat_memory_indirect_count); |
7cb67b45 | 5387 | } else { |
302f753f | 5388 | arc_no_grow = B_TRUE; |
3ec34e55 | 5389 | arc_kmem_reap_soon(); |
302f753f | 5390 | ARCSTAT_BUMP(arcstat_memory_direct_count); |
7cb67b45 BB |
5391 | } |
5392 | ||
1e3cb67b | 5393 | return (pages); |
7cb67b45 | 5394 | } |
7e7baeca | 5395 | SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func); |
7cb67b45 BB |
5396 | |
5397 | SPL_SHRINKER_DECLARE(arc_shrinker, arc_shrinker_func, DEFAULT_SEEKS); | |
5398 | #endif /* _KERNEL */ | |
5399 | ||
34dc7c2f BB |
5400 | /* |
5401 | * Adapt arc info given the number of bytes we are trying to add and | |
4e33ba4c | 5402 | * the state that we are coming from. This function is only called |
34dc7c2f BB |
5403 | * when we are adding new content to the cache. |
5404 | */ | |
5405 | static void | |
5406 | arc_adapt(int bytes, arc_state_t *state) | |
5407 | { | |
5408 | int mult; | |
728d6ae9 | 5409 | uint64_t arc_p_min = (arc_c >> arc_p_min_shift); |
424fd7c3 TS |
5410 | int64_t mrug_size = zfs_refcount_count(&arc_mru_ghost->arcs_size); |
5411 | int64_t mfug_size = zfs_refcount_count(&arc_mfu_ghost->arcs_size); | |
34dc7c2f BB |
5412 | |
5413 | if (state == arc_l2c_only) | |
5414 | return; | |
5415 | ||
5416 | ASSERT(bytes > 0); | |
5417 | /* | |
5418 | * Adapt the target size of the MRU list: | |
5419 | * - if we just hit in the MRU ghost list, then increase | |
5420 | * the target size of the MRU list. | |
5421 | * - if we just hit in the MFU ghost list, then increase | |
5422 | * the target size of the MFU list by decreasing the | |
5423 | * target size of the MRU list. | |
5424 | */ | |
5425 | if (state == arc_mru_ghost) { | |
36da08ef | 5426 | mult = (mrug_size >= mfug_size) ? 1 : (mfug_size / mrug_size); |
62422785 PS |
5427 | if (!zfs_arc_p_dampener_disable) |
5428 | mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ | |
34dc7c2f | 5429 | |
728d6ae9 | 5430 | arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult); |
34dc7c2f | 5431 | } else if (state == arc_mfu_ghost) { |
d164b209 BB |
5432 | uint64_t delta; |
5433 | ||
36da08ef | 5434 | mult = (mfug_size >= mrug_size) ? 1 : (mrug_size / mfug_size); |
62422785 PS |
5435 | if (!zfs_arc_p_dampener_disable) |
5436 | mult = MIN(mult, 10); | |
34dc7c2f | 5437 | |
d164b209 | 5438 | delta = MIN(bytes * mult, arc_p); |
728d6ae9 | 5439 | arc_p = MAX(arc_p_min, arc_p - delta); |
34dc7c2f BB |
5440 | } |
5441 | ASSERT((int64_t)arc_p >= 0); | |
5442 | ||
3ec34e55 BL |
5443 | /* |
5444 | * Wake reap thread if we do not have any available memory | |
5445 | */ | |
ca67b33a | 5446 | if (arc_reclaim_needed()) { |
3ec34e55 | 5447 | zthr_wakeup(arc_reap_zthr); |
ca67b33a MA |
5448 | return; |
5449 | } | |
5450 | ||
34dc7c2f BB |
5451 | if (arc_no_grow) |
5452 | return; | |
5453 | ||
5454 | if (arc_c >= arc_c_max) | |
5455 | return; | |
5456 | ||
5457 | /* | |
5458 | * If we're within (2 * maxblocksize) bytes of the target | |
5459 | * cache size, increment the target cache size | |
5460 | */ | |
935434ef | 5461 | ASSERT3U(arc_c, >=, 2ULL << SPA_MAXBLOCKSHIFT); |
37fb3e43 PD |
5462 | if (aggsum_compare(&arc_size, arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) >= |
5463 | 0) { | |
34dc7c2f BB |
5464 | atomic_add_64(&arc_c, (int64_t)bytes); |
5465 | if (arc_c > arc_c_max) | |
5466 | arc_c = arc_c_max; | |
5467 | else if (state == arc_anon) | |
5468 | atomic_add_64(&arc_p, (int64_t)bytes); | |
5469 | if (arc_p > arc_c) | |
5470 | arc_p = arc_c; | |
5471 | } | |
5472 | ASSERT((int64_t)arc_p >= 0); | |
5473 | } | |
5474 | ||
5475 | /* | |
ca0bf58d PS |
5476 | * Check if arc_size has grown past our upper threshold, determined by |
5477 | * zfs_arc_overflow_shift. | |
34dc7c2f | 5478 | */ |
ca0bf58d PS |
5479 | static boolean_t |
5480 | arc_is_overflowing(void) | |
34dc7c2f | 5481 | { |
ca0bf58d PS |
5482 | /* Always allow at least one block of overflow */ |
5483 | uint64_t overflow = MAX(SPA_MAXBLOCKSIZE, | |
5484 | arc_c >> zfs_arc_overflow_shift); | |
34dc7c2f | 5485 | |
37fb3e43 PD |
5486 | /* |
5487 | * We just compare the lower bound here for performance reasons. Our | |
5488 | * primary goals are to make sure that the arc never grows without | |
5489 | * bound, and that it can reach its maximum size. This check | |
5490 | * accomplishes both goals. The maximum amount we could run over by is | |
5491 | * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block | |
5492 | * in the ARC. In practice, that's in the tens of MB, which is low | |
5493 | * enough to be safe. | |
5494 | */ | |
5495 | return (aggsum_lower_bound(&arc_size) >= arc_c + overflow); | |
34dc7c2f BB |
5496 | } |
5497 | ||
a6255b7f DQ |
5498 | static abd_t * |
5499 | arc_get_data_abd(arc_buf_hdr_t *hdr, uint64_t size, void *tag) | |
5500 | { | |
5501 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5502 | ||
5503 | arc_get_data_impl(hdr, size, tag); | |
5504 | if (type == ARC_BUFC_METADATA) { | |
5505 | return (abd_alloc(size, B_TRUE)); | |
5506 | } else { | |
5507 | ASSERT(type == ARC_BUFC_DATA); | |
5508 | return (abd_alloc(size, B_FALSE)); | |
5509 | } | |
5510 | } | |
5511 | ||
5512 | static void * | |
5513 | arc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, void *tag) | |
5514 | { | |
5515 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5516 | ||
5517 | arc_get_data_impl(hdr, size, tag); | |
5518 | if (type == ARC_BUFC_METADATA) { | |
5519 | return (zio_buf_alloc(size)); | |
5520 | } else { | |
5521 | ASSERT(type == ARC_BUFC_DATA); | |
5522 | return (zio_data_buf_alloc(size)); | |
5523 | } | |
5524 | } | |
5525 | ||
34dc7c2f | 5526 | /* |
d3c2ae1c GW |
5527 | * Allocate a block and return it to the caller. If we are hitting the |
5528 | * hard limit for the cache size, we must sleep, waiting for the eviction | |
5529 | * thread to catch up. If we're past the target size but below the hard | |
5530 | * limit, we'll only signal the reclaim thread and continue on. | |
34dc7c2f | 5531 | */ |
a6255b7f DQ |
5532 | static void |
5533 | arc_get_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) | |
34dc7c2f | 5534 | { |
a6255b7f DQ |
5535 | arc_state_t *state = hdr->b_l1hdr.b_state; |
5536 | arc_buf_contents_t type = arc_buf_type(hdr); | |
34dc7c2f BB |
5537 | |
5538 | arc_adapt(size, state); | |
5539 | ||
5540 | /* | |
ca0bf58d PS |
5541 | * If arc_size is currently overflowing, and has grown past our |
5542 | * upper limit, we must be adding data faster than the evict | |
5543 | * thread can evict. Thus, to ensure we don't compound the | |
5544 | * problem by adding more data and forcing arc_size to grow even | |
5545 | * further past it's target size, we halt and wait for the | |
5546 | * eviction thread to catch up. | |
5547 | * | |
5548 | * It's also possible that the reclaim thread is unable to evict | |
5549 | * enough buffers to get arc_size below the overflow limit (e.g. | |
5550 | * due to buffers being un-evictable, or hash lock collisions). | |
5551 | * In this case, we want to proceed regardless if we're | |
5552 | * overflowing; thus we don't use a while loop here. | |
34dc7c2f | 5553 | */ |
ca0bf58d | 5554 | if (arc_is_overflowing()) { |
3ec34e55 | 5555 | mutex_enter(&arc_adjust_lock); |
ca0bf58d PS |
5556 | |
5557 | /* | |
5558 | * Now that we've acquired the lock, we may no longer be | |
5559 | * over the overflow limit, lets check. | |
5560 | * | |
5561 | * We're ignoring the case of spurious wake ups. If that | |
5562 | * were to happen, it'd let this thread consume an ARC | |
5563 | * buffer before it should have (i.e. before we're under | |
5564 | * the overflow limit and were signalled by the reclaim | |
5565 | * thread). As long as that is a rare occurrence, it | |
5566 | * shouldn't cause any harm. | |
5567 | */ | |
5568 | if (arc_is_overflowing()) { | |
3ec34e55 BL |
5569 | arc_adjust_needed = B_TRUE; |
5570 | zthr_wakeup(arc_adjust_zthr); | |
5571 | (void) cv_wait(&arc_adjust_waiters_cv, | |
5572 | &arc_adjust_lock); | |
34dc7c2f | 5573 | } |
3ec34e55 | 5574 | mutex_exit(&arc_adjust_lock); |
34dc7c2f | 5575 | } |
ab26409d | 5576 | |
d3c2ae1c | 5577 | VERIFY3U(hdr->b_type, ==, type); |
da8ccd0e | 5578 | if (type == ARC_BUFC_METADATA) { |
ca0bf58d PS |
5579 | arc_space_consume(size, ARC_SPACE_META); |
5580 | } else { | |
ca0bf58d | 5581 | arc_space_consume(size, ARC_SPACE_DATA); |
da8ccd0e PS |
5582 | } |
5583 | ||
34dc7c2f BB |
5584 | /* |
5585 | * Update the state size. Note that ghost states have a | |
5586 | * "ghost size" and so don't need to be updated. | |
5587 | */ | |
d3c2ae1c | 5588 | if (!GHOST_STATE(state)) { |
34dc7c2f | 5589 | |
424fd7c3 | 5590 | (void) zfs_refcount_add_many(&state->arcs_size, size, tag); |
ca0bf58d PS |
5591 | |
5592 | /* | |
5593 | * If this is reached via arc_read, the link is | |
5594 | * protected by the hash lock. If reached via | |
5595 | * arc_buf_alloc, the header should not be accessed by | |
5596 | * any other thread. And, if reached via arc_read_done, | |
5597 | * the hash lock will protect it if it's found in the | |
5598 | * hash table; otherwise no other thread should be | |
5599 | * trying to [add|remove]_reference it. | |
5600 | */ | |
5601 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 TS |
5602 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
5603 | (void) zfs_refcount_add_many(&state->arcs_esize[type], | |
d3c2ae1c | 5604 | size, tag); |
34dc7c2f | 5605 | } |
d3c2ae1c | 5606 | |
34dc7c2f BB |
5607 | /* |
5608 | * If we are growing the cache, and we are adding anonymous | |
5609 | * data, and we have outgrown arc_p, update arc_p | |
5610 | */ | |
37fb3e43 PD |
5611 | if (aggsum_compare(&arc_size, arc_c) < 0 && |
5612 | hdr->b_l1hdr.b_state == arc_anon && | |
424fd7c3 TS |
5613 | (zfs_refcount_count(&arc_anon->arcs_size) + |
5614 | zfs_refcount_count(&arc_mru->arcs_size) > arc_p)) | |
34dc7c2f BB |
5615 | arc_p = MIN(arc_c, arc_p + size); |
5616 | } | |
a6255b7f DQ |
5617 | } |
5618 | ||
5619 | static void | |
5620 | arc_free_data_abd(arc_buf_hdr_t *hdr, abd_t *abd, uint64_t size, void *tag) | |
5621 | { | |
5622 | arc_free_data_impl(hdr, size, tag); | |
5623 | abd_free(abd); | |
5624 | } | |
5625 | ||
5626 | static void | |
5627 | arc_free_data_buf(arc_buf_hdr_t *hdr, void *buf, uint64_t size, void *tag) | |
5628 | { | |
5629 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5630 | ||
5631 | arc_free_data_impl(hdr, size, tag); | |
5632 | if (type == ARC_BUFC_METADATA) { | |
5633 | zio_buf_free(buf, size); | |
5634 | } else { | |
5635 | ASSERT(type == ARC_BUFC_DATA); | |
5636 | zio_data_buf_free(buf, size); | |
5637 | } | |
d3c2ae1c GW |
5638 | } |
5639 | ||
5640 | /* | |
5641 | * Free the arc data buffer. | |
5642 | */ | |
5643 | static void | |
a6255b7f | 5644 | arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, void *tag) |
d3c2ae1c GW |
5645 | { |
5646 | arc_state_t *state = hdr->b_l1hdr.b_state; | |
5647 | arc_buf_contents_t type = arc_buf_type(hdr); | |
5648 | ||
5649 | /* protected by hash lock, if in the hash table */ | |
5650 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
424fd7c3 | 5651 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
5652 | ASSERT(state != arc_anon && state != arc_l2c_only); |
5653 | ||
424fd7c3 | 5654 | (void) zfs_refcount_remove_many(&state->arcs_esize[type], |
d3c2ae1c GW |
5655 | size, tag); |
5656 | } | |
424fd7c3 | 5657 | (void) zfs_refcount_remove_many(&state->arcs_size, size, tag); |
d3c2ae1c GW |
5658 | |
5659 | VERIFY3U(hdr->b_type, ==, type); | |
5660 | if (type == ARC_BUFC_METADATA) { | |
d3c2ae1c GW |
5661 | arc_space_return(size, ARC_SPACE_META); |
5662 | } else { | |
5663 | ASSERT(type == ARC_BUFC_DATA); | |
d3c2ae1c GW |
5664 | arc_space_return(size, ARC_SPACE_DATA); |
5665 | } | |
34dc7c2f BB |
5666 | } |
5667 | ||
5668 | /* | |
5669 | * This routine is called whenever a buffer is accessed. | |
5670 | * NOTE: the hash lock is dropped in this function. | |
5671 | */ | |
5672 | static void | |
2a432414 | 5673 | arc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) |
34dc7c2f | 5674 | { |
428870ff BB |
5675 | clock_t now; |
5676 | ||
34dc7c2f | 5677 | ASSERT(MUTEX_HELD(hash_lock)); |
b9541d6b | 5678 | ASSERT(HDR_HAS_L1HDR(hdr)); |
34dc7c2f | 5679 | |
b9541d6b | 5680 | if (hdr->b_l1hdr.b_state == arc_anon) { |
34dc7c2f BB |
5681 | /* |
5682 | * This buffer is not in the cache, and does not | |
5683 | * appear in our "ghost" list. Add the new buffer | |
5684 | * to the MRU state. | |
5685 | */ | |
5686 | ||
b9541d6b CW |
5687 | ASSERT0(hdr->b_l1hdr.b_arc_access); |
5688 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); | |
2a432414 GW |
5689 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); |
5690 | arc_change_state(arc_mru, hdr, hash_lock); | |
34dc7c2f | 5691 | |
b9541d6b | 5692 | } else if (hdr->b_l1hdr.b_state == arc_mru) { |
428870ff BB |
5693 | now = ddi_get_lbolt(); |
5694 | ||
34dc7c2f BB |
5695 | /* |
5696 | * If this buffer is here because of a prefetch, then either: | |
5697 | * - clear the flag if this is a "referencing" read | |
5698 | * (any subsequent access will bump this into the MFU state). | |
5699 | * or | |
5700 | * - move the buffer to the head of the list if this is | |
5701 | * another prefetch (to make it less likely to be evicted). | |
5702 | */ | |
d4a72f23 | 5703 | if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { |
424fd7c3 | 5704 | if (zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { |
ca0bf58d PS |
5705 | /* link protected by hash lock */ |
5706 | ASSERT(multilist_link_active( | |
b9541d6b | 5707 | &hdr->b_l1hdr.b_arc_node)); |
34dc7c2f | 5708 | } else { |
d4a72f23 TC |
5709 | arc_hdr_clear_flags(hdr, |
5710 | ARC_FLAG_PREFETCH | | |
5711 | ARC_FLAG_PRESCIENT_PREFETCH); | |
b9541d6b | 5712 | atomic_inc_32(&hdr->b_l1hdr.b_mru_hits); |
34dc7c2f BB |
5713 | ARCSTAT_BUMP(arcstat_mru_hits); |
5714 | } | |
b9541d6b | 5715 | hdr->b_l1hdr.b_arc_access = now; |
34dc7c2f BB |
5716 | return; |
5717 | } | |
5718 | ||
5719 | /* | |
5720 | * This buffer has been "accessed" only once so far, | |
5721 | * but it is still in the cache. Move it to the MFU | |
5722 | * state. | |
5723 | */ | |
b9541d6b CW |
5724 | if (ddi_time_after(now, hdr->b_l1hdr.b_arc_access + |
5725 | ARC_MINTIME)) { | |
34dc7c2f BB |
5726 | /* |
5727 | * More than 125ms have passed since we | |
5728 | * instantiated this buffer. Move it to the | |
5729 | * most frequently used state. | |
5730 | */ | |
b9541d6b | 5731 | hdr->b_l1hdr.b_arc_access = now; |
2a432414 GW |
5732 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
5733 | arc_change_state(arc_mfu, hdr, hash_lock); | |
34dc7c2f | 5734 | } |
b9541d6b | 5735 | atomic_inc_32(&hdr->b_l1hdr.b_mru_hits); |
34dc7c2f | 5736 | ARCSTAT_BUMP(arcstat_mru_hits); |
b9541d6b | 5737 | } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { |
34dc7c2f BB |
5738 | arc_state_t *new_state; |
5739 | /* | |
5740 | * This buffer has been "accessed" recently, but | |
5741 | * was evicted from the cache. Move it to the | |
5742 | * MFU state. | |
5743 | */ | |
5744 | ||
d4a72f23 | 5745 | if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { |
34dc7c2f | 5746 | new_state = arc_mru; |
424fd7c3 | 5747 | if (zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) { |
d4a72f23 TC |
5748 | arc_hdr_clear_flags(hdr, |
5749 | ARC_FLAG_PREFETCH | | |
5750 | ARC_FLAG_PRESCIENT_PREFETCH); | |
5751 | } | |
2a432414 | 5752 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); |
34dc7c2f BB |
5753 | } else { |
5754 | new_state = arc_mfu; | |
2a432414 | 5755 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
34dc7c2f BB |
5756 | } |
5757 | ||
b9541d6b | 5758 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 | 5759 | arc_change_state(new_state, hdr, hash_lock); |
34dc7c2f | 5760 | |
b9541d6b | 5761 | atomic_inc_32(&hdr->b_l1hdr.b_mru_ghost_hits); |
34dc7c2f | 5762 | ARCSTAT_BUMP(arcstat_mru_ghost_hits); |
b9541d6b | 5763 | } else if (hdr->b_l1hdr.b_state == arc_mfu) { |
34dc7c2f BB |
5764 | /* |
5765 | * This buffer has been accessed more than once and is | |
5766 | * still in the cache. Keep it in the MFU state. | |
5767 | * | |
5768 | * NOTE: an add_reference() that occurred when we did | |
5769 | * the arc_read() will have kicked this off the list. | |
5770 | * If it was a prefetch, we will explicitly move it to | |
5771 | * the head of the list now. | |
5772 | */ | |
d4a72f23 | 5773 | |
b9541d6b | 5774 | atomic_inc_32(&hdr->b_l1hdr.b_mfu_hits); |
34dc7c2f | 5775 | ARCSTAT_BUMP(arcstat_mfu_hits); |
b9541d6b CW |
5776 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
5777 | } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { | |
34dc7c2f BB |
5778 | arc_state_t *new_state = arc_mfu; |
5779 | /* | |
5780 | * This buffer has been accessed more than once but has | |
5781 | * been evicted from the cache. Move it back to the | |
5782 | * MFU state. | |
5783 | */ | |
5784 | ||
d4a72f23 | 5785 | if (HDR_PREFETCH(hdr) || HDR_PRESCIENT_PREFETCH(hdr)) { |
34dc7c2f BB |
5786 | /* |
5787 | * This is a prefetch access... | |
5788 | * move this block back to the MRU state. | |
5789 | */ | |
34dc7c2f BB |
5790 | new_state = arc_mru; |
5791 | } | |
5792 | ||
b9541d6b | 5793 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 GW |
5794 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
5795 | arc_change_state(new_state, hdr, hash_lock); | |
34dc7c2f | 5796 | |
b9541d6b | 5797 | atomic_inc_32(&hdr->b_l1hdr.b_mfu_ghost_hits); |
34dc7c2f | 5798 | ARCSTAT_BUMP(arcstat_mfu_ghost_hits); |
b9541d6b | 5799 | } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { |
34dc7c2f BB |
5800 | /* |
5801 | * This buffer is on the 2nd Level ARC. | |
5802 | */ | |
5803 | ||
b9541d6b | 5804 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 GW |
5805 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
5806 | arc_change_state(arc_mfu, hdr, hash_lock); | |
34dc7c2f | 5807 | } else { |
b9541d6b CW |
5808 | cmn_err(CE_PANIC, "invalid arc state 0x%p", |
5809 | hdr->b_l1hdr.b_state); | |
34dc7c2f BB |
5810 | } |
5811 | } | |
5812 | ||
0873bb63 BB |
5813 | /* |
5814 | * This routine is called by dbuf_hold() to update the arc_access() state | |
5815 | * which otherwise would be skipped for entries in the dbuf cache. | |
5816 | */ | |
5817 | void | |
5818 | arc_buf_access(arc_buf_t *buf) | |
5819 | { | |
5820 | mutex_enter(&buf->b_evict_lock); | |
5821 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
5822 | ||
5823 | /* | |
5824 | * Avoid taking the hash_lock when possible as an optimization. | |
5825 | * The header must be checked again under the hash_lock in order | |
5826 | * to handle the case where it is concurrently being released. | |
5827 | */ | |
5828 | if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) { | |
5829 | mutex_exit(&buf->b_evict_lock); | |
5830 | return; | |
5831 | } | |
5832 | ||
5833 | kmutex_t *hash_lock = HDR_LOCK(hdr); | |
5834 | mutex_enter(hash_lock); | |
5835 | ||
5836 | if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) { | |
5837 | mutex_exit(hash_lock); | |
5838 | mutex_exit(&buf->b_evict_lock); | |
5839 | ARCSTAT_BUMP(arcstat_access_skip); | |
5840 | return; | |
5841 | } | |
5842 | ||
5843 | mutex_exit(&buf->b_evict_lock); | |
5844 | ||
5845 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || | |
5846 | hdr->b_l1hdr.b_state == arc_mfu); | |
5847 | ||
5848 | DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); | |
5849 | arc_access(hdr, hash_lock); | |
5850 | mutex_exit(hash_lock); | |
5851 | ||
5852 | ARCSTAT_BUMP(arcstat_hits); | |
5853 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr) && !HDR_PRESCIENT_PREFETCH(hdr), | |
5854 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data, metadata, hits); | |
5855 | } | |
5856 | ||
b5256303 | 5857 | /* a generic arc_read_done_func_t which you can use */ |
34dc7c2f BB |
5858 | /* ARGSUSED */ |
5859 | void | |
d4a72f23 TC |
5860 | arc_bcopy_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, |
5861 | arc_buf_t *buf, void *arg) | |
34dc7c2f | 5862 | { |
d4a72f23 TC |
5863 | if (buf == NULL) |
5864 | return; | |
5865 | ||
5866 | bcopy(buf->b_data, arg, arc_buf_size(buf)); | |
d3c2ae1c | 5867 | arc_buf_destroy(buf, arg); |
34dc7c2f BB |
5868 | } |
5869 | ||
b5256303 | 5870 | /* a generic arc_read_done_func_t */ |
d4a72f23 | 5871 | /* ARGSUSED */ |
34dc7c2f | 5872 | void |
d4a72f23 TC |
5873 | arc_getbuf_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, |
5874 | arc_buf_t *buf, void *arg) | |
34dc7c2f BB |
5875 | { |
5876 | arc_buf_t **bufp = arg; | |
d4a72f23 TC |
5877 | |
5878 | if (buf == NULL) { | |
c3bd3fb4 | 5879 | ASSERT(zio == NULL || zio->io_error != 0); |
34dc7c2f BB |
5880 | *bufp = NULL; |
5881 | } else { | |
c3bd3fb4 | 5882 | ASSERT(zio == NULL || zio->io_error == 0); |
34dc7c2f | 5883 | *bufp = buf; |
c3bd3fb4 | 5884 | ASSERT(buf->b_data != NULL); |
34dc7c2f BB |
5885 | } |
5886 | } | |
5887 | ||
d3c2ae1c GW |
5888 | static void |
5889 | arc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp) | |
5890 | { | |
5891 | if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { | |
5892 | ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0); | |
b5256303 | 5893 | ASSERT3U(arc_hdr_get_compress(hdr), ==, ZIO_COMPRESS_OFF); |
d3c2ae1c GW |
5894 | } else { |
5895 | if (HDR_COMPRESSION_ENABLED(hdr)) { | |
b5256303 | 5896 | ASSERT3U(arc_hdr_get_compress(hdr), ==, |
d3c2ae1c GW |
5897 | BP_GET_COMPRESS(bp)); |
5898 | } | |
5899 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); | |
5900 | ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp)); | |
b5256303 | 5901 | ASSERT3U(!!HDR_PROTECTED(hdr), ==, BP_IS_PROTECTED(bp)); |
d3c2ae1c GW |
5902 | } |
5903 | } | |
5904 | ||
34dc7c2f BB |
5905 | static void |
5906 | arc_read_done(zio_t *zio) | |
5907 | { | |
b5256303 | 5908 | blkptr_t *bp = zio->io_bp; |
d3c2ae1c | 5909 | arc_buf_hdr_t *hdr = zio->io_private; |
9b67f605 | 5910 | kmutex_t *hash_lock = NULL; |
524b4217 DK |
5911 | arc_callback_t *callback_list; |
5912 | arc_callback_t *acb; | |
2aa34383 | 5913 | boolean_t freeable = B_FALSE; |
a7004725 | 5914 | |
34dc7c2f BB |
5915 | /* |
5916 | * The hdr was inserted into hash-table and removed from lists | |
5917 | * prior to starting I/O. We should find this header, since | |
5918 | * it's in the hash table, and it should be legit since it's | |
5919 | * not possible to evict it during the I/O. The only possible | |
5920 | * reason for it not to be found is if we were freed during the | |
5921 | * read. | |
5922 | */ | |
9b67f605 | 5923 | if (HDR_IN_HASH_TABLE(hdr)) { |
31df97cd DB |
5924 | arc_buf_hdr_t *found; |
5925 | ||
9b67f605 MA |
5926 | ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); |
5927 | ASSERT3U(hdr->b_dva.dva_word[0], ==, | |
5928 | BP_IDENTITY(zio->io_bp)->dva_word[0]); | |
5929 | ASSERT3U(hdr->b_dva.dva_word[1], ==, | |
5930 | BP_IDENTITY(zio->io_bp)->dva_word[1]); | |
5931 | ||
31df97cd | 5932 | found = buf_hash_find(hdr->b_spa, zio->io_bp, &hash_lock); |
9b67f605 | 5933 | |
d3c2ae1c | 5934 | ASSERT((found == hdr && |
9b67f605 MA |
5935 | DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || |
5936 | (found == hdr && HDR_L2_READING(hdr))); | |
d3c2ae1c GW |
5937 | ASSERT3P(hash_lock, !=, NULL); |
5938 | } | |
5939 | ||
b5256303 TC |
5940 | if (BP_IS_PROTECTED(bp)) { |
5941 | hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp); | |
5942 | hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset; | |
5943 | zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt, | |
5944 | hdr->b_crypt_hdr.b_iv); | |
5945 | ||
5946 | if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) { | |
5947 | void *tmpbuf; | |
5948 | ||
5949 | tmpbuf = abd_borrow_buf_copy(zio->io_abd, | |
5950 | sizeof (zil_chain_t)); | |
5951 | zio_crypt_decode_mac_zil(tmpbuf, | |
5952 | hdr->b_crypt_hdr.b_mac); | |
5953 | abd_return_buf(zio->io_abd, tmpbuf, | |
5954 | sizeof (zil_chain_t)); | |
5955 | } else { | |
5956 | zio_crypt_decode_mac_bp(bp, hdr->b_crypt_hdr.b_mac); | |
5957 | } | |
5958 | } | |
5959 | ||
d4a72f23 | 5960 | if (zio->io_error == 0) { |
d3c2ae1c GW |
5961 | /* byteswap if necessary */ |
5962 | if (BP_SHOULD_BYTESWAP(zio->io_bp)) { | |
5963 | if (BP_GET_LEVEL(zio->io_bp) > 0) { | |
5964 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; | |
5965 | } else { | |
5966 | hdr->b_l1hdr.b_byteswap = | |
5967 | DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); | |
5968 | } | |
5969 | } else { | |
5970 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
5971 | } | |
9b67f605 | 5972 | } |
34dc7c2f | 5973 | |
d3c2ae1c | 5974 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED); |
b9541d6b | 5975 | if (l2arc_noprefetch && HDR_PREFETCH(hdr)) |
d3c2ae1c | 5976 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE); |
34dc7c2f | 5977 | |
b9541d6b | 5978 | callback_list = hdr->b_l1hdr.b_acb; |
d3c2ae1c | 5979 | ASSERT3P(callback_list, !=, NULL); |
34dc7c2f | 5980 | |
d4a72f23 TC |
5981 | if (hash_lock && zio->io_error == 0 && |
5982 | hdr->b_l1hdr.b_state == arc_anon) { | |
428870ff BB |
5983 | /* |
5984 | * Only call arc_access on anonymous buffers. This is because | |
5985 | * if we've issued an I/O for an evicted buffer, we've already | |
5986 | * called arc_access (to prevent any simultaneous readers from | |
5987 | * getting confused). | |
5988 | */ | |
5989 | arc_access(hdr, hash_lock); | |
5990 | } | |
5991 | ||
524b4217 DK |
5992 | /* |
5993 | * If a read request has a callback (i.e. acb_done is not NULL), then we | |
5994 | * make a buf containing the data according to the parameters which were | |
5995 | * passed in. The implementation of arc_buf_alloc_impl() ensures that we | |
5996 | * aren't needlessly decompressing the data multiple times. | |
5997 | */ | |
a7004725 | 5998 | int callback_cnt = 0; |
2aa34383 DK |
5999 | for (acb = callback_list; acb != NULL; acb = acb->acb_next) { |
6000 | if (!acb->acb_done) | |
6001 | continue; | |
6002 | ||
2aa34383 | 6003 | callback_cnt++; |
524b4217 | 6004 | |
d4a72f23 TC |
6005 | if (zio->io_error != 0) |
6006 | continue; | |
6007 | ||
b5256303 | 6008 | int error = arc_buf_alloc_impl(hdr, zio->io_spa, |
be9a5c35 | 6009 | &acb->acb_zb, acb->acb_private, acb->acb_encrypted, |
d4a72f23 | 6010 | acb->acb_compressed, acb->acb_noauth, B_TRUE, |
440a3eb9 | 6011 | &acb->acb_buf); |
b5256303 TC |
6012 | |
6013 | /* | |
440a3eb9 | 6014 | * Assert non-speculative zios didn't fail because an |
b5256303 TC |
6015 | * encryption key wasn't loaded |
6016 | */ | |
a2c2ed1b | 6017 | ASSERT((zio->io_flags & ZIO_FLAG_SPECULATIVE) || |
be9a5c35 | 6018 | error != EACCES); |
b5256303 TC |
6019 | |
6020 | /* | |
6021 | * If we failed to decrypt, report an error now (as the zio | |
6022 | * layer would have done if it had done the transforms). | |
6023 | */ | |
6024 | if (error == ECKSUM) { | |
6025 | ASSERT(BP_IS_PROTECTED(bp)); | |
6026 | error = SET_ERROR(EIO); | |
b5256303 | 6027 | if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) { |
be9a5c35 | 6028 | spa_log_error(zio->io_spa, &acb->acb_zb); |
b5256303 | 6029 | zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION, |
be9a5c35 | 6030 | zio->io_spa, NULL, &acb->acb_zb, zio, 0, 0); |
b5256303 TC |
6031 | } |
6032 | } | |
6033 | ||
c3bd3fb4 TC |
6034 | if (error != 0) { |
6035 | /* | |
6036 | * Decompression or decryption failed. Set | |
6037 | * io_error so that when we call acb_done | |
6038 | * (below), we will indicate that the read | |
6039 | * failed. Note that in the unusual case | |
6040 | * where one callback is compressed and another | |
6041 | * uncompressed, we will mark all of them | |
6042 | * as failed, even though the uncompressed | |
6043 | * one can't actually fail. In this case, | |
6044 | * the hdr will not be anonymous, because | |
6045 | * if there are multiple callbacks, it's | |
6046 | * because multiple threads found the same | |
6047 | * arc buf in the hash table. | |
6048 | */ | |
524b4217 | 6049 | zio->io_error = error; |
c3bd3fb4 | 6050 | } |
34dc7c2f | 6051 | } |
c3bd3fb4 TC |
6052 | |
6053 | /* | |
6054 | * If there are multiple callbacks, we must have the hash lock, | |
6055 | * because the only way for multiple threads to find this hdr is | |
6056 | * in the hash table. This ensures that if there are multiple | |
6057 | * callbacks, the hdr is not anonymous. If it were anonymous, | |
6058 | * we couldn't use arc_buf_destroy() in the error case below. | |
6059 | */ | |
6060 | ASSERT(callback_cnt < 2 || hash_lock != NULL); | |
6061 | ||
b9541d6b | 6062 | hdr->b_l1hdr.b_acb = NULL; |
d3c2ae1c | 6063 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
440a3eb9 | 6064 | if (callback_cnt == 0) |
b5256303 | 6065 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); |
34dc7c2f | 6066 | |
424fd7c3 | 6067 | ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || |
b9541d6b | 6068 | callback_list != NULL); |
34dc7c2f | 6069 | |
d4a72f23 | 6070 | if (zio->io_error == 0) { |
d3c2ae1c GW |
6071 | arc_hdr_verify(hdr, zio->io_bp); |
6072 | } else { | |
6073 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); | |
b9541d6b | 6074 | if (hdr->b_l1hdr.b_state != arc_anon) |
34dc7c2f BB |
6075 | arc_change_state(arc_anon, hdr, hash_lock); |
6076 | if (HDR_IN_HASH_TABLE(hdr)) | |
6077 | buf_hash_remove(hdr); | |
424fd7c3 | 6078 | freeable = zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt); |
34dc7c2f BB |
6079 | } |
6080 | ||
6081 | /* | |
6082 | * Broadcast before we drop the hash_lock to avoid the possibility | |
6083 | * that the hdr (and hence the cv) might be freed before we get to | |
6084 | * the cv_broadcast(). | |
6085 | */ | |
b9541d6b | 6086 | cv_broadcast(&hdr->b_l1hdr.b_cv); |
34dc7c2f | 6087 | |
b9541d6b | 6088 | if (hash_lock != NULL) { |
34dc7c2f BB |
6089 | mutex_exit(hash_lock); |
6090 | } else { | |
6091 | /* | |
6092 | * This block was freed while we waited for the read to | |
6093 | * complete. It has been removed from the hash table and | |
6094 | * moved to the anonymous state (so that it won't show up | |
6095 | * in the cache). | |
6096 | */ | |
b9541d6b | 6097 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
424fd7c3 | 6098 | freeable = zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt); |
34dc7c2f BB |
6099 | } |
6100 | ||
6101 | /* execute each callback and free its structure */ | |
6102 | while ((acb = callback_list) != NULL) { | |
c3bd3fb4 TC |
6103 | if (acb->acb_done != NULL) { |
6104 | if (zio->io_error != 0 && acb->acb_buf != NULL) { | |
6105 | /* | |
6106 | * If arc_buf_alloc_impl() fails during | |
6107 | * decompression, the buf will still be | |
6108 | * allocated, and needs to be freed here. | |
6109 | */ | |
6110 | arc_buf_destroy(acb->acb_buf, | |
6111 | acb->acb_private); | |
6112 | acb->acb_buf = NULL; | |
6113 | } | |
d4a72f23 TC |
6114 | acb->acb_done(zio, &zio->io_bookmark, zio->io_bp, |
6115 | acb->acb_buf, acb->acb_private); | |
b5256303 | 6116 | } |
34dc7c2f BB |
6117 | |
6118 | if (acb->acb_zio_dummy != NULL) { | |
6119 | acb->acb_zio_dummy->io_error = zio->io_error; | |
6120 | zio_nowait(acb->acb_zio_dummy); | |
6121 | } | |
6122 | ||
6123 | callback_list = acb->acb_next; | |
6124 | kmem_free(acb, sizeof (arc_callback_t)); | |
6125 | } | |
6126 | ||
6127 | if (freeable) | |
6128 | arc_hdr_destroy(hdr); | |
6129 | } | |
6130 | ||
6131 | /* | |
5c839890 | 6132 | * "Read" the block at the specified DVA (in bp) via the |
34dc7c2f BB |
6133 | * cache. If the block is found in the cache, invoke the provided |
6134 | * callback immediately and return. Note that the `zio' parameter | |
6135 | * in the callback will be NULL in this case, since no IO was | |
6136 | * required. If the block is not in the cache pass the read request | |
6137 | * on to the spa with a substitute callback function, so that the | |
6138 | * requested block will be added to the cache. | |
6139 | * | |
6140 | * If a read request arrives for a block that has a read in-progress, | |
6141 | * either wait for the in-progress read to complete (and return the | |
6142 | * results); or, if this is a read with a "done" func, add a record | |
6143 | * to the read to invoke the "done" func when the read completes, | |
6144 | * and return; or just return. | |
6145 | * | |
6146 | * arc_read_done() will invoke all the requested "done" functions | |
6147 | * for readers of this block. | |
6148 | */ | |
6149 | int | |
b5256303 TC |
6150 | arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, |
6151 | arc_read_done_func_t *done, void *private, zio_priority_t priority, | |
6152 | int zio_flags, arc_flags_t *arc_flags, const zbookmark_phys_t *zb) | |
34dc7c2f | 6153 | { |
9b67f605 | 6154 | arc_buf_hdr_t *hdr = NULL; |
9b67f605 | 6155 | kmutex_t *hash_lock = NULL; |
34dc7c2f | 6156 | zio_t *rzio; |
3541dc6d | 6157 | uint64_t guid = spa_load_guid(spa); |
b5256303 TC |
6158 | boolean_t compressed_read = (zio_flags & ZIO_FLAG_RAW_COMPRESS) != 0; |
6159 | boolean_t encrypted_read = BP_IS_ENCRYPTED(bp) && | |
6160 | (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0; | |
6161 | boolean_t noauth_read = BP_IS_AUTHENTICATED(bp) && | |
6162 | (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0; | |
0902c457 | 6163 | boolean_t embedded_bp = !!BP_IS_EMBEDDED(bp); |
1421c891 | 6164 | int rc = 0; |
34dc7c2f | 6165 | |
0902c457 | 6166 | ASSERT(!embedded_bp || |
9b67f605 MA |
6167 | BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); |
6168 | ||
34dc7c2f | 6169 | top: |
0902c457 | 6170 | if (!embedded_bp) { |
9b67f605 MA |
6171 | /* |
6172 | * Embedded BP's have no DVA and require no I/O to "read". | |
6173 | * Create an anonymous arc buf to back it. | |
6174 | */ | |
6175 | hdr = buf_hash_find(guid, bp, &hash_lock); | |
6176 | } | |
6177 | ||
b5256303 TC |
6178 | /* |
6179 | * Determine if we have an L1 cache hit or a cache miss. For simplicity | |
6180 | * we maintain encrypted data seperately from compressed / uncompressed | |
6181 | * data. If the user is requesting raw encrypted data and we don't have | |
6182 | * that in the header we will read from disk to guarantee that we can | |
6183 | * get it even if the encryption keys aren't loaded. | |
6184 | */ | |
6185 | if (hdr != NULL && HDR_HAS_L1HDR(hdr) && (HDR_HAS_RABD(hdr) || | |
6186 | (hdr->b_l1hdr.b_pabd != NULL && !encrypted_read))) { | |
d3c2ae1c | 6187 | arc_buf_t *buf = NULL; |
2a432414 | 6188 | *arc_flags |= ARC_FLAG_CACHED; |
34dc7c2f BB |
6189 | |
6190 | if (HDR_IO_IN_PROGRESS(hdr)) { | |
a8b2e306 | 6191 | zio_t *head_zio = hdr->b_l1hdr.b_acb->acb_zio_head; |
34dc7c2f | 6192 | |
a8b2e306 | 6193 | ASSERT3P(head_zio, !=, NULL); |
7f60329a MA |
6194 | if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) && |
6195 | priority == ZIO_PRIORITY_SYNC_READ) { | |
6196 | /* | |
a8b2e306 TC |
6197 | * This is a sync read that needs to wait for |
6198 | * an in-flight async read. Request that the | |
6199 | * zio have its priority upgraded. | |
7f60329a | 6200 | */ |
a8b2e306 TC |
6201 | zio_change_priority(head_zio, priority); |
6202 | DTRACE_PROBE1(arc__async__upgrade__sync, | |
7f60329a | 6203 | arc_buf_hdr_t *, hdr); |
a8b2e306 | 6204 | ARCSTAT_BUMP(arcstat_async_upgrade_sync); |
7f60329a MA |
6205 | } |
6206 | if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { | |
d3c2ae1c GW |
6207 | arc_hdr_clear_flags(hdr, |
6208 | ARC_FLAG_PREDICTIVE_PREFETCH); | |
7f60329a MA |
6209 | } |
6210 | ||
2a432414 | 6211 | if (*arc_flags & ARC_FLAG_WAIT) { |
b9541d6b | 6212 | cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); |
34dc7c2f BB |
6213 | mutex_exit(hash_lock); |
6214 | goto top; | |
6215 | } | |
2a432414 | 6216 | ASSERT(*arc_flags & ARC_FLAG_NOWAIT); |
34dc7c2f BB |
6217 | |
6218 | if (done) { | |
7f60329a | 6219 | arc_callback_t *acb = NULL; |
34dc7c2f BB |
6220 | |
6221 | acb = kmem_zalloc(sizeof (arc_callback_t), | |
79c76d5b | 6222 | KM_SLEEP); |
34dc7c2f BB |
6223 | acb->acb_done = done; |
6224 | acb->acb_private = private; | |
a7004725 | 6225 | acb->acb_compressed = compressed_read; |
440a3eb9 TC |
6226 | acb->acb_encrypted = encrypted_read; |
6227 | acb->acb_noauth = noauth_read; | |
be9a5c35 | 6228 | acb->acb_zb = *zb; |
34dc7c2f BB |
6229 | if (pio != NULL) |
6230 | acb->acb_zio_dummy = zio_null(pio, | |
d164b209 | 6231 | spa, NULL, NULL, NULL, zio_flags); |
34dc7c2f | 6232 | |
d3c2ae1c | 6233 | ASSERT3P(acb->acb_done, !=, NULL); |
a8b2e306 | 6234 | acb->acb_zio_head = head_zio; |
b9541d6b CW |
6235 | acb->acb_next = hdr->b_l1hdr.b_acb; |
6236 | hdr->b_l1hdr.b_acb = acb; | |
34dc7c2f | 6237 | mutex_exit(hash_lock); |
1421c891 | 6238 | goto out; |
34dc7c2f BB |
6239 | } |
6240 | mutex_exit(hash_lock); | |
1421c891 | 6241 | goto out; |
34dc7c2f BB |
6242 | } |
6243 | ||
b9541d6b CW |
6244 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || |
6245 | hdr->b_l1hdr.b_state == arc_mfu); | |
34dc7c2f BB |
6246 | |
6247 | if (done) { | |
7f60329a MA |
6248 | if (hdr->b_flags & ARC_FLAG_PREDICTIVE_PREFETCH) { |
6249 | /* | |
6250 | * This is a demand read which does not have to | |
6251 | * wait for i/o because we did a predictive | |
6252 | * prefetch i/o for it, which has completed. | |
6253 | */ | |
6254 | DTRACE_PROBE1( | |
6255 | arc__demand__hit__predictive__prefetch, | |
6256 | arc_buf_hdr_t *, hdr); | |
6257 | ARCSTAT_BUMP( | |
6258 | arcstat_demand_hit_predictive_prefetch); | |
d3c2ae1c GW |
6259 | arc_hdr_clear_flags(hdr, |
6260 | ARC_FLAG_PREDICTIVE_PREFETCH); | |
7f60329a | 6261 | } |
d4a72f23 TC |
6262 | |
6263 | if (hdr->b_flags & ARC_FLAG_PRESCIENT_PREFETCH) { | |
6264 | ARCSTAT_BUMP( | |
6265 | arcstat_demand_hit_prescient_prefetch); | |
6266 | arc_hdr_clear_flags(hdr, | |
6267 | ARC_FLAG_PRESCIENT_PREFETCH); | |
6268 | } | |
6269 | ||
0902c457 | 6270 | ASSERT(!embedded_bp || !BP_IS_HOLE(bp)); |
d3c2ae1c | 6271 | |
524b4217 | 6272 | /* Get a buf with the desired data in it. */ |
be9a5c35 TC |
6273 | rc = arc_buf_alloc_impl(hdr, spa, zb, private, |
6274 | encrypted_read, compressed_read, noauth_read, | |
6275 | B_TRUE, &buf); | |
a2c2ed1b TC |
6276 | if (rc == ECKSUM) { |
6277 | /* | |
6278 | * Convert authentication and decryption errors | |
be9a5c35 TC |
6279 | * to EIO (and generate an ereport if needed) |
6280 | * before leaving the ARC. | |
a2c2ed1b TC |
6281 | */ |
6282 | rc = SET_ERROR(EIO); | |
be9a5c35 TC |
6283 | if ((zio_flags & ZIO_FLAG_SPECULATIVE) == 0) { |
6284 | spa_log_error(spa, zb); | |
6285 | zfs_ereport_post( | |
6286 | FM_EREPORT_ZFS_AUTHENTICATION, | |
6287 | spa, NULL, zb, NULL, 0, 0); | |
6288 | } | |
a2c2ed1b | 6289 | } |
d4a72f23 | 6290 | if (rc != 0) { |
2c24b5b1 TC |
6291 | (void) remove_reference(hdr, hash_lock, |
6292 | private); | |
6293 | arc_buf_destroy_impl(buf); | |
d4a72f23 TC |
6294 | buf = NULL; |
6295 | } | |
6296 | ||
a2c2ed1b TC |
6297 | /* assert any errors weren't due to unloaded keys */ |
6298 | ASSERT((zio_flags & ZIO_FLAG_SPECULATIVE) || | |
be9a5c35 | 6299 | rc != EACCES); |
2a432414 | 6300 | } else if (*arc_flags & ARC_FLAG_PREFETCH && |
424fd7c3 | 6301 | zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { |
d3c2ae1c | 6302 | arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); |
34dc7c2f BB |
6303 | } |
6304 | DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); | |
6305 | arc_access(hdr, hash_lock); | |
d4a72f23 TC |
6306 | if (*arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) |
6307 | arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH); | |
2a432414 | 6308 | if (*arc_flags & ARC_FLAG_L2CACHE) |
d3c2ae1c | 6309 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); |
34dc7c2f BB |
6310 | mutex_exit(hash_lock); |
6311 | ARCSTAT_BUMP(arcstat_hits); | |
b9541d6b CW |
6312 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), |
6313 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), | |
34dc7c2f BB |
6314 | data, metadata, hits); |
6315 | ||
6316 | if (done) | |
d4a72f23 | 6317 | done(NULL, zb, bp, buf, private); |
34dc7c2f | 6318 | } else { |
d3c2ae1c GW |
6319 | uint64_t lsize = BP_GET_LSIZE(bp); |
6320 | uint64_t psize = BP_GET_PSIZE(bp); | |
9b67f605 | 6321 | arc_callback_t *acb; |
b128c09f | 6322 | vdev_t *vd = NULL; |
a117a6d6 | 6323 | uint64_t addr = 0; |
d164b209 | 6324 | boolean_t devw = B_FALSE; |
d3c2ae1c | 6325 | uint64_t size; |
440a3eb9 | 6326 | abd_t *hdr_abd; |
34dc7c2f | 6327 | |
5f6d0b6f BB |
6328 | /* |
6329 | * Gracefully handle a damaged logical block size as a | |
1cdb86cb | 6330 | * checksum error. |
5f6d0b6f | 6331 | */ |
d3c2ae1c | 6332 | if (lsize > spa_maxblocksize(spa)) { |
1cdb86cb | 6333 | rc = SET_ERROR(ECKSUM); |
5f6d0b6f BB |
6334 | goto out; |
6335 | } | |
6336 | ||
34dc7c2f | 6337 | if (hdr == NULL) { |
0902c457 TC |
6338 | /* |
6339 | * This block is not in the cache or it has | |
6340 | * embedded data. | |
6341 | */ | |
9b67f605 | 6342 | arc_buf_hdr_t *exists = NULL; |
34dc7c2f | 6343 | arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); |
d3c2ae1c | 6344 | hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, |
b5256303 TC |
6345 | BP_IS_PROTECTED(bp), BP_GET_COMPRESS(bp), type, |
6346 | encrypted_read); | |
d3c2ae1c | 6347 | |
0902c457 | 6348 | if (!embedded_bp) { |
9b67f605 MA |
6349 | hdr->b_dva = *BP_IDENTITY(bp); |
6350 | hdr->b_birth = BP_PHYSICAL_BIRTH(bp); | |
9b67f605 MA |
6351 | exists = buf_hash_insert(hdr, &hash_lock); |
6352 | } | |
6353 | if (exists != NULL) { | |
34dc7c2f BB |
6354 | /* somebody beat us to the hash insert */ |
6355 | mutex_exit(hash_lock); | |
428870ff | 6356 | buf_discard_identity(hdr); |
d3c2ae1c | 6357 | arc_hdr_destroy(hdr); |
34dc7c2f BB |
6358 | goto top; /* restart the IO request */ |
6359 | } | |
34dc7c2f | 6360 | } else { |
b9541d6b | 6361 | /* |
b5256303 TC |
6362 | * This block is in the ghost cache or encrypted data |
6363 | * was requested and we didn't have it. If it was | |
6364 | * L2-only (and thus didn't have an L1 hdr), | |
6365 | * we realloc the header to add an L1 hdr. | |
b9541d6b CW |
6366 | */ |
6367 | if (!HDR_HAS_L1HDR(hdr)) { | |
6368 | hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, | |
6369 | hdr_full_cache); | |
6370 | } | |
6371 | ||
b5256303 TC |
6372 | if (GHOST_STATE(hdr->b_l1hdr.b_state)) { |
6373 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); | |
6374 | ASSERT(!HDR_HAS_RABD(hdr)); | |
6375 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
424fd7c3 TS |
6376 | ASSERT0(zfs_refcount_count( |
6377 | &hdr->b_l1hdr.b_refcnt)); | |
b5256303 TC |
6378 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
6379 | ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); | |
6380 | } else if (HDR_IO_IN_PROGRESS(hdr)) { | |
6381 | /* | |
6382 | * If this header already had an IO in progress | |
6383 | * and we are performing another IO to fetch | |
6384 | * encrypted data we must wait until the first | |
6385 | * IO completes so as not to confuse | |
6386 | * arc_read_done(). This should be very rare | |
6387 | * and so the performance impact shouldn't | |
6388 | * matter. | |
6389 | */ | |
6390 | cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); | |
6391 | mutex_exit(hash_lock); | |
6392 | goto top; | |
6393 | } | |
34dc7c2f | 6394 | |
7f60329a | 6395 | /* |
d3c2ae1c | 6396 | * This is a delicate dance that we play here. |
b5256303 TC |
6397 | * This hdr might be in the ghost list so we access |
6398 | * it to move it out of the ghost list before we | |
d3c2ae1c GW |
6399 | * initiate the read. If it's a prefetch then |
6400 | * it won't have a callback so we'll remove the | |
6401 | * reference that arc_buf_alloc_impl() created. We | |
6402 | * do this after we've called arc_access() to | |
6403 | * avoid hitting an assert in remove_reference(). | |
7f60329a | 6404 | */ |
428870ff | 6405 | arc_access(hdr, hash_lock); |
b5256303 | 6406 | arc_hdr_alloc_abd(hdr, encrypted_read); |
d3c2ae1c | 6407 | } |
d3c2ae1c | 6408 | |
b5256303 TC |
6409 | if (encrypted_read) { |
6410 | ASSERT(HDR_HAS_RABD(hdr)); | |
6411 | size = HDR_GET_PSIZE(hdr); | |
6412 | hdr_abd = hdr->b_crypt_hdr.b_rabd; | |
d3c2ae1c | 6413 | zio_flags |= ZIO_FLAG_RAW; |
b5256303 TC |
6414 | } else { |
6415 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
6416 | size = arc_hdr_size(hdr); | |
6417 | hdr_abd = hdr->b_l1hdr.b_pabd; | |
6418 | ||
6419 | if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) { | |
6420 | zio_flags |= ZIO_FLAG_RAW_COMPRESS; | |
6421 | } | |
6422 | ||
6423 | /* | |
6424 | * For authenticated bp's, we do not ask the ZIO layer | |
6425 | * to authenticate them since this will cause the entire | |
6426 | * IO to fail if the key isn't loaded. Instead, we | |
6427 | * defer authentication until arc_buf_fill(), which will | |
6428 | * verify the data when the key is available. | |
6429 | */ | |
6430 | if (BP_IS_AUTHENTICATED(bp)) | |
6431 | zio_flags |= ZIO_FLAG_RAW_ENCRYPT; | |
34dc7c2f BB |
6432 | } |
6433 | ||
b5256303 | 6434 | if (*arc_flags & ARC_FLAG_PREFETCH && |
424fd7c3 | 6435 | zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) |
d3c2ae1c | 6436 | arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); |
d4a72f23 TC |
6437 | if (*arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) |
6438 | arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH); | |
d3c2ae1c GW |
6439 | if (*arc_flags & ARC_FLAG_L2CACHE) |
6440 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); | |
b5256303 TC |
6441 | if (BP_IS_AUTHENTICATED(bp)) |
6442 | arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH); | |
d3c2ae1c GW |
6443 | if (BP_GET_LEVEL(bp) > 0) |
6444 | arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT); | |
7f60329a | 6445 | if (*arc_flags & ARC_FLAG_PREDICTIVE_PREFETCH) |
d3c2ae1c | 6446 | arc_hdr_set_flags(hdr, ARC_FLAG_PREDICTIVE_PREFETCH); |
b9541d6b | 6447 | ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); |
428870ff | 6448 | |
79c76d5b | 6449 | acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); |
34dc7c2f BB |
6450 | acb->acb_done = done; |
6451 | acb->acb_private = private; | |
2aa34383 | 6452 | acb->acb_compressed = compressed_read; |
b5256303 TC |
6453 | acb->acb_encrypted = encrypted_read; |
6454 | acb->acb_noauth = noauth_read; | |
be9a5c35 | 6455 | acb->acb_zb = *zb; |
34dc7c2f | 6456 | |
d3c2ae1c | 6457 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
b9541d6b | 6458 | hdr->b_l1hdr.b_acb = acb; |
d3c2ae1c | 6459 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
34dc7c2f | 6460 | |
b9541d6b CW |
6461 | if (HDR_HAS_L2HDR(hdr) && |
6462 | (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { | |
6463 | devw = hdr->b_l2hdr.b_dev->l2ad_writing; | |
6464 | addr = hdr->b_l2hdr.b_daddr; | |
b128c09f | 6465 | /* |
a1d477c2 | 6466 | * Lock out L2ARC device removal. |
b128c09f BB |
6467 | */ |
6468 | if (vdev_is_dead(vd) || | |
6469 | !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) | |
6470 | vd = NULL; | |
6471 | } | |
6472 | ||
a8b2e306 TC |
6473 | /* |
6474 | * We count both async reads and scrub IOs as asynchronous so | |
6475 | * that both can be upgraded in the event of a cache hit while | |
6476 | * the read IO is still in-flight. | |
6477 | */ | |
6478 | if (priority == ZIO_PRIORITY_ASYNC_READ || | |
6479 | priority == ZIO_PRIORITY_SCRUB) | |
d3c2ae1c GW |
6480 | arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); |
6481 | else | |
6482 | arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); | |
6483 | ||
e49f1e20 | 6484 | /* |
0902c457 TC |
6485 | * At this point, we have a level 1 cache miss or a blkptr |
6486 | * with embedded data. Try again in L2ARC if possible. | |
e49f1e20 | 6487 | */ |
d3c2ae1c GW |
6488 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize); |
6489 | ||
0902c457 TC |
6490 | /* |
6491 | * Skip ARC stat bump for block pointers with embedded | |
6492 | * data. The data are read from the blkptr itself via | |
6493 | * decode_embedded_bp_compressed(). | |
6494 | */ | |
6495 | if (!embedded_bp) { | |
6496 | DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, | |
6497 | blkptr_t *, bp, uint64_t, lsize, | |
6498 | zbookmark_phys_t *, zb); | |
6499 | ARCSTAT_BUMP(arcstat_misses); | |
6500 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), | |
6501 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data, | |
6502 | metadata, misses); | |
6503 | } | |
34dc7c2f | 6504 | |
d164b209 | 6505 | if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { |
34dc7c2f BB |
6506 | /* |
6507 | * Read from the L2ARC if the following are true: | |
b128c09f BB |
6508 | * 1. The L2ARC vdev was previously cached. |
6509 | * 2. This buffer still has L2ARC metadata. | |
6510 | * 3. This buffer isn't currently writing to the L2ARC. | |
6511 | * 4. The L2ARC entry wasn't evicted, which may | |
6512 | * also have invalidated the vdev. | |
d164b209 | 6513 | * 5. This isn't prefetch and l2arc_noprefetch is set. |
34dc7c2f | 6514 | */ |
b9541d6b | 6515 | if (HDR_HAS_L2HDR(hdr) && |
d164b209 BB |
6516 | !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && |
6517 | !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { | |
34dc7c2f | 6518 | l2arc_read_callback_t *cb; |
82710e99 GDN |
6519 | abd_t *abd; |
6520 | uint64_t asize; | |
34dc7c2f BB |
6521 | |
6522 | DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); | |
6523 | ARCSTAT_BUMP(arcstat_l2_hits); | |
b9541d6b | 6524 | atomic_inc_32(&hdr->b_l2hdr.b_hits); |
34dc7c2f | 6525 | |
34dc7c2f | 6526 | cb = kmem_zalloc(sizeof (l2arc_read_callback_t), |
79c76d5b | 6527 | KM_SLEEP); |
d3c2ae1c | 6528 | cb->l2rcb_hdr = hdr; |
34dc7c2f BB |
6529 | cb->l2rcb_bp = *bp; |
6530 | cb->l2rcb_zb = *zb; | |
b128c09f | 6531 | cb->l2rcb_flags = zio_flags; |
34dc7c2f | 6532 | |
82710e99 GDN |
6533 | asize = vdev_psize_to_asize(vd, size); |
6534 | if (asize != size) { | |
6535 | abd = abd_alloc_for_io(asize, | |
6536 | HDR_ISTYPE_METADATA(hdr)); | |
6537 | cb->l2rcb_abd = abd; | |
6538 | } else { | |
b5256303 | 6539 | abd = hdr_abd; |
82710e99 GDN |
6540 | } |
6541 | ||
a117a6d6 | 6542 | ASSERT(addr >= VDEV_LABEL_START_SIZE && |
82710e99 | 6543 | addr + asize <= vd->vdev_psize - |
a117a6d6 GW |
6544 | VDEV_LABEL_END_SIZE); |
6545 | ||
34dc7c2f | 6546 | /* |
b128c09f BB |
6547 | * l2arc read. The SCL_L2ARC lock will be |
6548 | * released by l2arc_read_done(). | |
3a17a7a9 SK |
6549 | * Issue a null zio if the underlying buffer |
6550 | * was squashed to zero size by compression. | |
34dc7c2f | 6551 | */ |
b5256303 | 6552 | ASSERT3U(arc_hdr_get_compress(hdr), !=, |
d3c2ae1c GW |
6553 | ZIO_COMPRESS_EMPTY); |
6554 | rzio = zio_read_phys(pio, vd, addr, | |
82710e99 | 6555 | asize, abd, |
d3c2ae1c GW |
6556 | ZIO_CHECKSUM_OFF, |
6557 | l2arc_read_done, cb, priority, | |
6558 | zio_flags | ZIO_FLAG_DONT_CACHE | | |
6559 | ZIO_FLAG_CANFAIL | | |
6560 | ZIO_FLAG_DONT_PROPAGATE | | |
6561 | ZIO_FLAG_DONT_RETRY, B_FALSE); | |
a8b2e306 TC |
6562 | acb->acb_zio_head = rzio; |
6563 | ||
6564 | if (hash_lock != NULL) | |
6565 | mutex_exit(hash_lock); | |
d3c2ae1c | 6566 | |
34dc7c2f BB |
6567 | DTRACE_PROBE2(l2arc__read, vdev_t *, vd, |
6568 | zio_t *, rzio); | |
b5256303 TC |
6569 | ARCSTAT_INCR(arcstat_l2_read_bytes, |
6570 | HDR_GET_PSIZE(hdr)); | |
34dc7c2f | 6571 | |
2a432414 | 6572 | if (*arc_flags & ARC_FLAG_NOWAIT) { |
b128c09f | 6573 | zio_nowait(rzio); |
1421c891 | 6574 | goto out; |
b128c09f | 6575 | } |
34dc7c2f | 6576 | |
2a432414 | 6577 | ASSERT(*arc_flags & ARC_FLAG_WAIT); |
b128c09f | 6578 | if (zio_wait(rzio) == 0) |
1421c891 | 6579 | goto out; |
b128c09f BB |
6580 | |
6581 | /* l2arc read error; goto zio_read() */ | |
a8b2e306 TC |
6582 | if (hash_lock != NULL) |
6583 | mutex_enter(hash_lock); | |
34dc7c2f BB |
6584 | } else { |
6585 | DTRACE_PROBE1(l2arc__miss, | |
6586 | arc_buf_hdr_t *, hdr); | |
6587 | ARCSTAT_BUMP(arcstat_l2_misses); | |
6588 | if (HDR_L2_WRITING(hdr)) | |
6589 | ARCSTAT_BUMP(arcstat_l2_rw_clash); | |
b128c09f | 6590 | spa_config_exit(spa, SCL_L2ARC, vd); |
34dc7c2f | 6591 | } |
d164b209 BB |
6592 | } else { |
6593 | if (vd != NULL) | |
6594 | spa_config_exit(spa, SCL_L2ARC, vd); | |
0902c457 TC |
6595 | /* |
6596 | * Skip ARC stat bump for block pointers with | |
6597 | * embedded data. The data are read from the blkptr | |
6598 | * itself via decode_embedded_bp_compressed(). | |
6599 | */ | |
6600 | if (l2arc_ndev != 0 && !embedded_bp) { | |
d164b209 BB |
6601 | DTRACE_PROBE1(l2arc__miss, |
6602 | arc_buf_hdr_t *, hdr); | |
6603 | ARCSTAT_BUMP(arcstat_l2_misses); | |
6604 | } | |
34dc7c2f | 6605 | } |
34dc7c2f | 6606 | |
b5256303 | 6607 | rzio = zio_read(pio, spa, bp, hdr_abd, size, |
d3c2ae1c | 6608 | arc_read_done, hdr, priority, zio_flags, zb); |
a8b2e306 TC |
6609 | acb->acb_zio_head = rzio; |
6610 | ||
6611 | if (hash_lock != NULL) | |
6612 | mutex_exit(hash_lock); | |
34dc7c2f | 6613 | |
2a432414 | 6614 | if (*arc_flags & ARC_FLAG_WAIT) { |
1421c891 PS |
6615 | rc = zio_wait(rzio); |
6616 | goto out; | |
6617 | } | |
34dc7c2f | 6618 | |
2a432414 | 6619 | ASSERT(*arc_flags & ARC_FLAG_NOWAIT); |
34dc7c2f BB |
6620 | zio_nowait(rzio); |
6621 | } | |
1421c891 PS |
6622 | |
6623 | out: | |
157ef7f6 | 6624 | /* embedded bps don't actually go to disk */ |
0902c457 | 6625 | if (!embedded_bp) |
157ef7f6 | 6626 | spa_read_history_add(spa, zb, *arc_flags); |
1421c891 | 6627 | return (rc); |
34dc7c2f BB |
6628 | } |
6629 | ||
ab26409d BB |
6630 | arc_prune_t * |
6631 | arc_add_prune_callback(arc_prune_func_t *func, void *private) | |
6632 | { | |
6633 | arc_prune_t *p; | |
6634 | ||
d1d7e268 | 6635 | p = kmem_alloc(sizeof (*p), KM_SLEEP); |
ab26409d BB |
6636 | p->p_pfunc = func; |
6637 | p->p_private = private; | |
6638 | list_link_init(&p->p_node); | |
424fd7c3 | 6639 | zfs_refcount_create(&p->p_refcnt); |
ab26409d BB |
6640 | |
6641 | mutex_enter(&arc_prune_mtx); | |
c13060e4 | 6642 | zfs_refcount_add(&p->p_refcnt, &arc_prune_list); |
ab26409d BB |
6643 | list_insert_head(&arc_prune_list, p); |
6644 | mutex_exit(&arc_prune_mtx); | |
6645 | ||
6646 | return (p); | |
6647 | } | |
6648 | ||
6649 | void | |
6650 | arc_remove_prune_callback(arc_prune_t *p) | |
6651 | { | |
4442f60d | 6652 | boolean_t wait = B_FALSE; |
ab26409d BB |
6653 | mutex_enter(&arc_prune_mtx); |
6654 | list_remove(&arc_prune_list, p); | |
424fd7c3 | 6655 | if (zfs_refcount_remove(&p->p_refcnt, &arc_prune_list) > 0) |
4442f60d | 6656 | wait = B_TRUE; |
ab26409d | 6657 | mutex_exit(&arc_prune_mtx); |
4442f60d CC |
6658 | |
6659 | /* wait for arc_prune_task to finish */ | |
6660 | if (wait) | |
6661 | taskq_wait_outstanding(arc_prune_taskq, 0); | |
424fd7c3 TS |
6662 | ASSERT0(zfs_refcount_count(&p->p_refcnt)); |
6663 | zfs_refcount_destroy(&p->p_refcnt); | |
4442f60d | 6664 | kmem_free(p, sizeof (*p)); |
ab26409d BB |
6665 | } |
6666 | ||
df4474f9 MA |
6667 | /* |
6668 | * Notify the arc that a block was freed, and thus will never be used again. | |
6669 | */ | |
6670 | void | |
6671 | arc_freed(spa_t *spa, const blkptr_t *bp) | |
6672 | { | |
6673 | arc_buf_hdr_t *hdr; | |
6674 | kmutex_t *hash_lock; | |
6675 | uint64_t guid = spa_load_guid(spa); | |
6676 | ||
9b67f605 MA |
6677 | ASSERT(!BP_IS_EMBEDDED(bp)); |
6678 | ||
6679 | hdr = buf_hash_find(guid, bp, &hash_lock); | |
df4474f9 MA |
6680 | if (hdr == NULL) |
6681 | return; | |
df4474f9 | 6682 | |
d3c2ae1c GW |
6683 | /* |
6684 | * We might be trying to free a block that is still doing I/O | |
6685 | * (i.e. prefetch) or has a reference (i.e. a dedup-ed, | |
6686 | * dmu_sync-ed block). If this block is being prefetched, then it | |
6687 | * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr | |
6688 | * until the I/O completes. A block may also have a reference if it is | |
6689 | * part of a dedup-ed, dmu_synced write. The dmu_sync() function would | |
6690 | * have written the new block to its final resting place on disk but | |
6691 | * without the dedup flag set. This would have left the hdr in the MRU | |
6692 | * state and discoverable. When the txg finally syncs it detects that | |
6693 | * the block was overridden in open context and issues an override I/O. | |
6694 | * Since this is a dedup block, the override I/O will determine if the | |
6695 | * block is already in the DDT. If so, then it will replace the io_bp | |
6696 | * with the bp from the DDT and allow the I/O to finish. When the I/O | |
6697 | * reaches the done callback, dbuf_write_override_done, it will | |
6698 | * check to see if the io_bp and io_bp_override are identical. | |
6699 | * If they are not, then it indicates that the bp was replaced with | |
6700 | * the bp in the DDT and the override bp is freed. This allows | |
6701 | * us to arrive here with a reference on a block that is being | |
6702 | * freed. So if we have an I/O in progress, or a reference to | |
6703 | * this hdr, then we don't destroy the hdr. | |
6704 | */ | |
6705 | if (!HDR_HAS_L1HDR(hdr) || (!HDR_IO_IN_PROGRESS(hdr) && | |
424fd7c3 | 6706 | zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt))) { |
d3c2ae1c GW |
6707 | arc_change_state(arc_anon, hdr, hash_lock); |
6708 | arc_hdr_destroy(hdr); | |
df4474f9 | 6709 | mutex_exit(hash_lock); |
bd089c54 | 6710 | } else { |
d3c2ae1c | 6711 | mutex_exit(hash_lock); |
34dc7c2f | 6712 | } |
34dc7c2f | 6713 | |
34dc7c2f BB |
6714 | } |
6715 | ||
6716 | /* | |
e49f1e20 WA |
6717 | * Release this buffer from the cache, making it an anonymous buffer. This |
6718 | * must be done after a read and prior to modifying the buffer contents. | |
34dc7c2f | 6719 | * If the buffer has more than one reference, we must make |
b128c09f | 6720 | * a new hdr for the buffer. |
34dc7c2f BB |
6721 | */ |
6722 | void | |
6723 | arc_release(arc_buf_t *buf, void *tag) | |
6724 | { | |
b9541d6b | 6725 | arc_buf_hdr_t *hdr = buf->b_hdr; |
34dc7c2f | 6726 | |
428870ff | 6727 | /* |
ca0bf58d | 6728 | * It would be nice to assert that if its DMU metadata (level > |
428870ff BB |
6729 | * 0 || it's the dnode file), then it must be syncing context. |
6730 | * But we don't know that information at this level. | |
6731 | */ | |
6732 | ||
6733 | mutex_enter(&buf->b_evict_lock); | |
b128c09f | 6734 | |
ca0bf58d PS |
6735 | ASSERT(HDR_HAS_L1HDR(hdr)); |
6736 | ||
b9541d6b CW |
6737 | /* |
6738 | * We don't grab the hash lock prior to this check, because if | |
6739 | * the buffer's header is in the arc_anon state, it won't be | |
6740 | * linked into the hash table. | |
6741 | */ | |
6742 | if (hdr->b_l1hdr.b_state == arc_anon) { | |
6743 | mutex_exit(&buf->b_evict_lock); | |
6744 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
6745 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); | |
6746 | ASSERT(!HDR_HAS_L2HDR(hdr)); | |
d3c2ae1c | 6747 | ASSERT(HDR_EMPTY(hdr)); |
34dc7c2f | 6748 | |
d3c2ae1c | 6749 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); |
424fd7c3 | 6750 | ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); |
b9541d6b CW |
6751 | ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); |
6752 | ||
b9541d6b | 6753 | hdr->b_l1hdr.b_arc_access = 0; |
d3c2ae1c GW |
6754 | |
6755 | /* | |
6756 | * If the buf is being overridden then it may already | |
6757 | * have a hdr that is not empty. | |
6758 | */ | |
6759 | buf_discard_identity(hdr); | |
b9541d6b CW |
6760 | arc_buf_thaw(buf); |
6761 | ||
6762 | return; | |
34dc7c2f BB |
6763 | } |
6764 | ||
1c27024e | 6765 | kmutex_t *hash_lock = HDR_LOCK(hdr); |
b9541d6b CW |
6766 | mutex_enter(hash_lock); |
6767 | ||
6768 | /* | |
6769 | * This assignment is only valid as long as the hash_lock is | |
6770 | * held, we must be careful not to reference state or the | |
6771 | * b_state field after dropping the lock. | |
6772 | */ | |
1c27024e | 6773 | arc_state_t *state = hdr->b_l1hdr.b_state; |
b9541d6b CW |
6774 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
6775 | ASSERT3P(state, !=, arc_anon); | |
6776 | ||
6777 | /* this buffer is not on any list */ | |
424fd7c3 | 6778 | ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), >, 0); |
b9541d6b CW |
6779 | |
6780 | if (HDR_HAS_L2HDR(hdr)) { | |
b9541d6b | 6781 | mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); |
ca0bf58d PS |
6782 | |
6783 | /* | |
d962d5da PS |
6784 | * We have to recheck this conditional again now that |
6785 | * we're holding the l2ad_mtx to prevent a race with | |
6786 | * another thread which might be concurrently calling | |
6787 | * l2arc_evict(). In that case, l2arc_evict() might have | |
6788 | * destroyed the header's L2 portion as we were waiting | |
6789 | * to acquire the l2ad_mtx. | |
ca0bf58d | 6790 | */ |
d962d5da PS |
6791 | if (HDR_HAS_L2HDR(hdr)) |
6792 | arc_hdr_l2hdr_destroy(hdr); | |
ca0bf58d | 6793 | |
b9541d6b | 6794 | mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); |
b128c09f BB |
6795 | } |
6796 | ||
34dc7c2f BB |
6797 | /* |
6798 | * Do we have more than one buf? | |
6799 | */ | |
d3c2ae1c | 6800 | if (hdr->b_l1hdr.b_bufcnt > 1) { |
34dc7c2f | 6801 | arc_buf_hdr_t *nhdr; |
d164b209 | 6802 | uint64_t spa = hdr->b_spa; |
d3c2ae1c GW |
6803 | uint64_t psize = HDR_GET_PSIZE(hdr); |
6804 | uint64_t lsize = HDR_GET_LSIZE(hdr); | |
b5256303 TC |
6805 | boolean_t protected = HDR_PROTECTED(hdr); |
6806 | enum zio_compress compress = arc_hdr_get_compress(hdr); | |
b9541d6b | 6807 | arc_buf_contents_t type = arc_buf_type(hdr); |
d3c2ae1c | 6808 | VERIFY3U(hdr->b_type, ==, type); |
34dc7c2f | 6809 | |
b9541d6b | 6810 | ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); |
d3c2ae1c GW |
6811 | (void) remove_reference(hdr, hash_lock, tag); |
6812 | ||
524b4217 | 6813 | if (arc_buf_is_shared(buf) && !ARC_BUF_COMPRESSED(buf)) { |
d3c2ae1c | 6814 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); |
524b4217 DK |
6815 | ASSERT(ARC_BUF_LAST(buf)); |
6816 | } | |
d3c2ae1c | 6817 | |
34dc7c2f | 6818 | /* |
428870ff | 6819 | * Pull the data off of this hdr and attach it to |
d3c2ae1c GW |
6820 | * a new anonymous hdr. Also find the last buffer |
6821 | * in the hdr's buffer list. | |
34dc7c2f | 6822 | */ |
a7004725 | 6823 | arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); |
d3c2ae1c | 6824 | ASSERT3P(lastbuf, !=, NULL); |
34dc7c2f | 6825 | |
d3c2ae1c GW |
6826 | /* |
6827 | * If the current arc_buf_t and the hdr are sharing their data | |
524b4217 | 6828 | * buffer, then we must stop sharing that block. |
d3c2ae1c GW |
6829 | */ |
6830 | if (arc_buf_is_shared(buf)) { | |
6831 | ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); | |
d3c2ae1c GW |
6832 | VERIFY(!arc_buf_is_shared(lastbuf)); |
6833 | ||
6834 | /* | |
6835 | * First, sever the block sharing relationship between | |
a7004725 | 6836 | * buf and the arc_buf_hdr_t. |
d3c2ae1c GW |
6837 | */ |
6838 | arc_unshare_buf(hdr, buf); | |
2aa34383 DK |
6839 | |
6840 | /* | |
a6255b7f | 6841 | * Now we need to recreate the hdr's b_pabd. Since we |
524b4217 | 6842 | * have lastbuf handy, we try to share with it, but if |
a6255b7f | 6843 | * we can't then we allocate a new b_pabd and copy the |
524b4217 | 6844 | * data from buf into it. |
2aa34383 | 6845 | */ |
524b4217 DK |
6846 | if (arc_can_share(hdr, lastbuf)) { |
6847 | arc_share_buf(hdr, lastbuf); | |
6848 | } else { | |
b5256303 | 6849 | arc_hdr_alloc_abd(hdr, B_FALSE); |
a6255b7f DQ |
6850 | abd_copy_from_buf(hdr->b_l1hdr.b_pabd, |
6851 | buf->b_data, psize); | |
2aa34383 | 6852 | } |
d3c2ae1c GW |
6853 | VERIFY3P(lastbuf->b_data, !=, NULL); |
6854 | } else if (HDR_SHARED_DATA(hdr)) { | |
2aa34383 DK |
6855 | /* |
6856 | * Uncompressed shared buffers are always at the end | |
6857 | * of the list. Compressed buffers don't have the | |
6858 | * same requirements. This makes it hard to | |
6859 | * simply assert that the lastbuf is shared so | |
6860 | * we rely on the hdr's compression flags to determine | |
6861 | * if we have a compressed, shared buffer. | |
6862 | */ | |
6863 | ASSERT(arc_buf_is_shared(lastbuf) || | |
b5256303 | 6864 | arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); |
2aa34383 | 6865 | ASSERT(!ARC_BUF_SHARED(buf)); |
d3c2ae1c | 6866 | } |
b5256303 TC |
6867 | |
6868 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); | |
b9541d6b | 6869 | ASSERT3P(state, !=, arc_l2c_only); |
36da08ef | 6870 | |
424fd7c3 | 6871 | (void) zfs_refcount_remove_many(&state->arcs_size, |
2aa34383 | 6872 | arc_buf_size(buf), buf); |
36da08ef | 6873 | |
424fd7c3 | 6874 | if (zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { |
b9541d6b | 6875 | ASSERT3P(state, !=, arc_l2c_only); |
424fd7c3 TS |
6876 | (void) zfs_refcount_remove_many( |
6877 | &state->arcs_esize[type], | |
2aa34383 | 6878 | arc_buf_size(buf), buf); |
34dc7c2f | 6879 | } |
1eb5bfa3 | 6880 | |
d3c2ae1c | 6881 | hdr->b_l1hdr.b_bufcnt -= 1; |
b5256303 TC |
6882 | if (ARC_BUF_ENCRYPTED(buf)) |
6883 | hdr->b_crypt_hdr.b_ebufcnt -= 1; | |
6884 | ||
34dc7c2f | 6885 | arc_cksum_verify(buf); |
498877ba | 6886 | arc_buf_unwatch(buf); |
34dc7c2f | 6887 | |
f486f584 TC |
6888 | /* if this is the last uncompressed buf free the checksum */ |
6889 | if (!arc_hdr_has_uncompressed_buf(hdr)) | |
6890 | arc_cksum_free(hdr); | |
6891 | ||
34dc7c2f BB |
6892 | mutex_exit(hash_lock); |
6893 | ||
d3c2ae1c | 6894 | /* |
a6255b7f | 6895 | * Allocate a new hdr. The new hdr will contain a b_pabd |
d3c2ae1c GW |
6896 | * buffer which will be freed in arc_write(). |
6897 | */ | |
b5256303 TC |
6898 | nhdr = arc_hdr_alloc(spa, psize, lsize, protected, |
6899 | compress, type, HDR_HAS_RABD(hdr)); | |
d3c2ae1c GW |
6900 | ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL); |
6901 | ASSERT0(nhdr->b_l1hdr.b_bufcnt); | |
424fd7c3 | 6902 | ASSERT0(zfs_refcount_count(&nhdr->b_l1hdr.b_refcnt)); |
d3c2ae1c GW |
6903 | VERIFY3U(nhdr->b_type, ==, type); |
6904 | ASSERT(!HDR_SHARED_DATA(nhdr)); | |
b9541d6b | 6905 | |
d3c2ae1c GW |
6906 | nhdr->b_l1hdr.b_buf = buf; |
6907 | nhdr->b_l1hdr.b_bufcnt = 1; | |
b5256303 TC |
6908 | if (ARC_BUF_ENCRYPTED(buf)) |
6909 | nhdr->b_crypt_hdr.b_ebufcnt = 1; | |
b9541d6b CW |
6910 | nhdr->b_l1hdr.b_mru_hits = 0; |
6911 | nhdr->b_l1hdr.b_mru_ghost_hits = 0; | |
6912 | nhdr->b_l1hdr.b_mfu_hits = 0; | |
6913 | nhdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
6914 | nhdr->b_l1hdr.b_l2_hits = 0; | |
c13060e4 | 6915 | (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); |
34dc7c2f | 6916 | buf->b_hdr = nhdr; |
d3c2ae1c | 6917 | |
428870ff | 6918 | mutex_exit(&buf->b_evict_lock); |
424fd7c3 | 6919 | (void) zfs_refcount_add_many(&arc_anon->arcs_size, |
5e8ff256 | 6920 | arc_buf_size(buf), buf); |
34dc7c2f | 6921 | } else { |
428870ff | 6922 | mutex_exit(&buf->b_evict_lock); |
424fd7c3 | 6923 | ASSERT(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); |
ca0bf58d PS |
6924 | /* protected by hash lock, or hdr is on arc_anon */ |
6925 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
34dc7c2f | 6926 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b CW |
6927 | hdr->b_l1hdr.b_mru_hits = 0; |
6928 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
6929 | hdr->b_l1hdr.b_mfu_hits = 0; | |
6930 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
6931 | hdr->b_l1hdr.b_l2_hits = 0; | |
6932 | arc_change_state(arc_anon, hdr, hash_lock); | |
6933 | hdr->b_l1hdr.b_arc_access = 0; | |
34dc7c2f | 6934 | |
b5256303 | 6935 | mutex_exit(hash_lock); |
428870ff | 6936 | buf_discard_identity(hdr); |
34dc7c2f BB |
6937 | arc_buf_thaw(buf); |
6938 | } | |
34dc7c2f BB |
6939 | } |
6940 | ||
6941 | int | |
6942 | arc_released(arc_buf_t *buf) | |
6943 | { | |
b128c09f BB |
6944 | int released; |
6945 | ||
428870ff | 6946 | mutex_enter(&buf->b_evict_lock); |
b9541d6b CW |
6947 | released = (buf->b_data != NULL && |
6948 | buf->b_hdr->b_l1hdr.b_state == arc_anon); | |
428870ff | 6949 | mutex_exit(&buf->b_evict_lock); |
b128c09f | 6950 | return (released); |
34dc7c2f BB |
6951 | } |
6952 | ||
34dc7c2f BB |
6953 | #ifdef ZFS_DEBUG |
6954 | int | |
6955 | arc_referenced(arc_buf_t *buf) | |
6956 | { | |
b128c09f BB |
6957 | int referenced; |
6958 | ||
428870ff | 6959 | mutex_enter(&buf->b_evict_lock); |
424fd7c3 | 6960 | referenced = (zfs_refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); |
428870ff | 6961 | mutex_exit(&buf->b_evict_lock); |
b128c09f | 6962 | return (referenced); |
34dc7c2f BB |
6963 | } |
6964 | #endif | |
6965 | ||
6966 | static void | |
6967 | arc_write_ready(zio_t *zio) | |
6968 | { | |
6969 | arc_write_callback_t *callback = zio->io_private; | |
6970 | arc_buf_t *buf = callback->awcb_buf; | |
6971 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
b5256303 TC |
6972 | blkptr_t *bp = zio->io_bp; |
6973 | uint64_t psize = BP_IS_HOLE(bp) ? 0 : BP_GET_PSIZE(bp); | |
a6255b7f | 6974 | fstrans_cookie_t cookie = spl_fstrans_mark(); |
34dc7c2f | 6975 | |
b9541d6b | 6976 | ASSERT(HDR_HAS_L1HDR(hdr)); |
424fd7c3 | 6977 | ASSERT(!zfs_refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); |
d3c2ae1c | 6978 | ASSERT(hdr->b_l1hdr.b_bufcnt > 0); |
b128c09f | 6979 | |
34dc7c2f | 6980 | /* |
d3c2ae1c GW |
6981 | * If we're reexecuting this zio because the pool suspended, then |
6982 | * cleanup any state that was previously set the first time the | |
2aa34383 | 6983 | * callback was invoked. |
34dc7c2f | 6984 | */ |
d3c2ae1c GW |
6985 | if (zio->io_flags & ZIO_FLAG_REEXECUTED) { |
6986 | arc_cksum_free(hdr); | |
6987 | arc_buf_unwatch(buf); | |
a6255b7f | 6988 | if (hdr->b_l1hdr.b_pabd != NULL) { |
d3c2ae1c | 6989 | if (arc_buf_is_shared(buf)) { |
d3c2ae1c GW |
6990 | arc_unshare_buf(hdr, buf); |
6991 | } else { | |
b5256303 | 6992 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c | 6993 | } |
34dc7c2f | 6994 | } |
b5256303 TC |
6995 | |
6996 | if (HDR_HAS_RABD(hdr)) | |
6997 | arc_hdr_free_abd(hdr, B_TRUE); | |
34dc7c2f | 6998 | } |
a6255b7f | 6999 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
b5256303 | 7000 | ASSERT(!HDR_HAS_RABD(hdr)); |
d3c2ae1c GW |
7001 | ASSERT(!HDR_SHARED_DATA(hdr)); |
7002 | ASSERT(!arc_buf_is_shared(buf)); | |
7003 | ||
7004 | callback->awcb_ready(zio, buf, callback->awcb_private); | |
7005 | ||
7006 | if (HDR_IO_IN_PROGRESS(hdr)) | |
7007 | ASSERT(zio->io_flags & ZIO_FLAG_REEXECUTED); | |
7008 | ||
d3c2ae1c GW |
7009 | arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
7010 | ||
b5256303 TC |
7011 | if (BP_IS_PROTECTED(bp) != !!HDR_PROTECTED(hdr)) |
7012 | hdr = arc_hdr_realloc_crypt(hdr, BP_IS_PROTECTED(bp)); | |
7013 | ||
7014 | if (BP_IS_PROTECTED(bp)) { | |
7015 | /* ZIL blocks are written through zio_rewrite */ | |
7016 | ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG); | |
7017 | ASSERT(HDR_PROTECTED(hdr)); | |
7018 | ||
ae76f45c TC |
7019 | if (BP_SHOULD_BYTESWAP(bp)) { |
7020 | if (BP_GET_LEVEL(bp) > 0) { | |
7021 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; | |
7022 | } else { | |
7023 | hdr->b_l1hdr.b_byteswap = | |
7024 | DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); | |
7025 | } | |
7026 | } else { | |
7027 | hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; | |
7028 | } | |
7029 | ||
b5256303 TC |
7030 | hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp); |
7031 | hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset; | |
7032 | zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt, | |
7033 | hdr->b_crypt_hdr.b_iv); | |
7034 | zio_crypt_decode_mac_bp(bp, hdr->b_crypt_hdr.b_mac); | |
7035 | } | |
7036 | ||
7037 | /* | |
7038 | * If this block was written for raw encryption but the zio layer | |
7039 | * ended up only authenticating it, adjust the buffer flags now. | |
7040 | */ | |
7041 | if (BP_IS_AUTHENTICATED(bp) && ARC_BUF_ENCRYPTED(buf)) { | |
7042 | arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH); | |
7043 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
7044 | if (BP_GET_COMPRESS(bp) == ZIO_COMPRESS_OFF) | |
7045 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
b1d21733 TC |
7046 | } else if (BP_IS_HOLE(bp) && ARC_BUF_ENCRYPTED(buf)) { |
7047 | buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; | |
7048 | buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; | |
b5256303 TC |
7049 | } |
7050 | ||
7051 | /* this must be done after the buffer flags are adjusted */ | |
7052 | arc_cksum_compute(buf); | |
7053 | ||
1c27024e | 7054 | enum zio_compress compress; |
b5256303 | 7055 | if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { |
d3c2ae1c GW |
7056 | compress = ZIO_COMPRESS_OFF; |
7057 | } else { | |
b5256303 TC |
7058 | ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); |
7059 | compress = BP_GET_COMPRESS(bp); | |
d3c2ae1c GW |
7060 | } |
7061 | HDR_SET_PSIZE(hdr, psize); | |
7062 | arc_hdr_set_compress(hdr, compress); | |
7063 | ||
4807c0ba TC |
7064 | if (zio->io_error != 0 || psize == 0) |
7065 | goto out; | |
7066 | ||
d3c2ae1c | 7067 | /* |
b5256303 TC |
7068 | * Fill the hdr with data. If the buffer is encrypted we have no choice |
7069 | * but to copy the data into b_radb. If the hdr is compressed, the data | |
7070 | * we want is available from the zio, otherwise we can take it from | |
7071 | * the buf. | |
a6255b7f DQ |
7072 | * |
7073 | * We might be able to share the buf's data with the hdr here. However, | |
7074 | * doing so would cause the ARC to be full of linear ABDs if we write a | |
7075 | * lot of shareable data. As a compromise, we check whether scattered | |
7076 | * ABDs are allowed, and assume that if they are then the user wants | |
7077 | * the ARC to be primarily filled with them regardless of the data being | |
7078 | * written. Therefore, if they're allowed then we allocate one and copy | |
7079 | * the data into it; otherwise, we share the data directly if we can. | |
d3c2ae1c | 7080 | */ |
b5256303 | 7081 | if (ARC_BUF_ENCRYPTED(buf)) { |
4807c0ba | 7082 | ASSERT3U(psize, >, 0); |
b5256303 TC |
7083 | ASSERT(ARC_BUF_COMPRESSED(buf)); |
7084 | arc_hdr_alloc_abd(hdr, B_TRUE); | |
7085 | abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize); | |
7086 | } else if (zfs_abd_scatter_enabled || !arc_can_share(hdr, buf)) { | |
a6255b7f DQ |
7087 | /* |
7088 | * Ideally, we would always copy the io_abd into b_pabd, but the | |
7089 | * user may have disabled compressed ARC, thus we must check the | |
7090 | * hdr's compression setting rather than the io_bp's. | |
7091 | */ | |
b5256303 | 7092 | if (BP_IS_ENCRYPTED(bp)) { |
a6255b7f | 7093 | ASSERT3U(psize, >, 0); |
b5256303 TC |
7094 | arc_hdr_alloc_abd(hdr, B_TRUE); |
7095 | abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize); | |
7096 | } else if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF && | |
7097 | !ARC_BUF_COMPRESSED(buf)) { | |
7098 | ASSERT3U(psize, >, 0); | |
7099 | arc_hdr_alloc_abd(hdr, B_FALSE); | |
a6255b7f DQ |
7100 | abd_copy(hdr->b_l1hdr.b_pabd, zio->io_abd, psize); |
7101 | } else { | |
7102 | ASSERT3U(zio->io_orig_size, ==, arc_hdr_size(hdr)); | |
b5256303 | 7103 | arc_hdr_alloc_abd(hdr, B_FALSE); |
a6255b7f DQ |
7104 | abd_copy_from_buf(hdr->b_l1hdr.b_pabd, buf->b_data, |
7105 | arc_buf_size(buf)); | |
7106 | } | |
d3c2ae1c | 7107 | } else { |
a6255b7f | 7108 | ASSERT3P(buf->b_data, ==, abd_to_buf(zio->io_orig_abd)); |
2aa34383 | 7109 | ASSERT3U(zio->io_orig_size, ==, arc_buf_size(buf)); |
d3c2ae1c | 7110 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); |
d3c2ae1c | 7111 | |
d3c2ae1c | 7112 | arc_share_buf(hdr, buf); |
d3c2ae1c | 7113 | } |
a6255b7f | 7114 | |
4807c0ba | 7115 | out: |
b5256303 | 7116 | arc_hdr_verify(hdr, bp); |
a6255b7f | 7117 | spl_fstrans_unmark(cookie); |
34dc7c2f BB |
7118 | } |
7119 | ||
bc77ba73 PD |
7120 | static void |
7121 | arc_write_children_ready(zio_t *zio) | |
7122 | { | |
7123 | arc_write_callback_t *callback = zio->io_private; | |
7124 | arc_buf_t *buf = callback->awcb_buf; | |
7125 | ||
7126 | callback->awcb_children_ready(zio, buf, callback->awcb_private); | |
7127 | } | |
7128 | ||
e8b96c60 MA |
7129 | /* |
7130 | * The SPA calls this callback for each physical write that happens on behalf | |
7131 | * of a logical write. See the comment in dbuf_write_physdone() for details. | |
7132 | */ | |
7133 | static void | |
7134 | arc_write_physdone(zio_t *zio) | |
7135 | { | |
7136 | arc_write_callback_t *cb = zio->io_private; | |
7137 | if (cb->awcb_physdone != NULL) | |
7138 | cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); | |
7139 | } | |
7140 | ||
34dc7c2f BB |
7141 | static void |
7142 | arc_write_done(zio_t *zio) | |
7143 | { | |
7144 | arc_write_callback_t *callback = zio->io_private; | |
7145 | arc_buf_t *buf = callback->awcb_buf; | |
7146 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
7147 | ||
d3c2ae1c | 7148 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
428870ff BB |
7149 | |
7150 | if (zio->io_error == 0) { | |
d3c2ae1c GW |
7151 | arc_hdr_verify(hdr, zio->io_bp); |
7152 | ||
9b67f605 | 7153 | if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { |
b0bc7a84 MG |
7154 | buf_discard_identity(hdr); |
7155 | } else { | |
7156 | hdr->b_dva = *BP_IDENTITY(zio->io_bp); | |
7157 | hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); | |
b0bc7a84 | 7158 | } |
428870ff | 7159 | } else { |
d3c2ae1c | 7160 | ASSERT(HDR_EMPTY(hdr)); |
428870ff | 7161 | } |
34dc7c2f | 7162 | |
34dc7c2f | 7163 | /* |
9b67f605 MA |
7164 | * If the block to be written was all-zero or compressed enough to be |
7165 | * embedded in the BP, no write was performed so there will be no | |
7166 | * dva/birth/checksum. The buffer must therefore remain anonymous | |
7167 | * (and uncached). | |
34dc7c2f | 7168 | */ |
d3c2ae1c | 7169 | if (!HDR_EMPTY(hdr)) { |
34dc7c2f BB |
7170 | arc_buf_hdr_t *exists; |
7171 | kmutex_t *hash_lock; | |
7172 | ||
524b4217 | 7173 | ASSERT3U(zio->io_error, ==, 0); |
428870ff | 7174 | |
34dc7c2f BB |
7175 | arc_cksum_verify(buf); |
7176 | ||
7177 | exists = buf_hash_insert(hdr, &hash_lock); | |
b9541d6b | 7178 | if (exists != NULL) { |
34dc7c2f BB |
7179 | /* |
7180 | * This can only happen if we overwrite for | |
7181 | * sync-to-convergence, because we remove | |
7182 | * buffers from the hash table when we arc_free(). | |
7183 | */ | |
428870ff BB |
7184 | if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { |
7185 | if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) | |
7186 | panic("bad overwrite, hdr=%p exists=%p", | |
7187 | (void *)hdr, (void *)exists); | |
424fd7c3 | 7188 | ASSERT(zfs_refcount_is_zero( |
b9541d6b | 7189 | &exists->b_l1hdr.b_refcnt)); |
428870ff | 7190 | arc_change_state(arc_anon, exists, hash_lock); |
428870ff | 7191 | arc_hdr_destroy(exists); |
ca6c7a94 | 7192 | mutex_exit(hash_lock); |
428870ff BB |
7193 | exists = buf_hash_insert(hdr, &hash_lock); |
7194 | ASSERT3P(exists, ==, NULL); | |
03c6040b GW |
7195 | } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { |
7196 | /* nopwrite */ | |
7197 | ASSERT(zio->io_prop.zp_nopwrite); | |
7198 | if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) | |
7199 | panic("bad nopwrite, hdr=%p exists=%p", | |
7200 | (void *)hdr, (void *)exists); | |
428870ff BB |
7201 | } else { |
7202 | /* Dedup */ | |
d3c2ae1c | 7203 | ASSERT(hdr->b_l1hdr.b_bufcnt == 1); |
b9541d6b | 7204 | ASSERT(hdr->b_l1hdr.b_state == arc_anon); |
428870ff BB |
7205 | ASSERT(BP_GET_DEDUP(zio->io_bp)); |
7206 | ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); | |
7207 | } | |
34dc7c2f | 7208 | } |
d3c2ae1c | 7209 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
b128c09f | 7210 | /* if it's not anon, we are doing a scrub */ |
b9541d6b | 7211 | if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) |
b128c09f | 7212 | arc_access(hdr, hash_lock); |
34dc7c2f | 7213 | mutex_exit(hash_lock); |
34dc7c2f | 7214 | } else { |
d3c2ae1c | 7215 | arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); |
34dc7c2f BB |
7216 | } |
7217 | ||
424fd7c3 | 7218 | ASSERT(!zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
428870ff | 7219 | callback->awcb_done(zio, buf, callback->awcb_private); |
34dc7c2f | 7220 | |
a6255b7f | 7221 | abd_put(zio->io_abd); |
34dc7c2f BB |
7222 | kmem_free(callback, sizeof (arc_write_callback_t)); |
7223 | } | |
7224 | ||
7225 | zio_t * | |
428870ff | 7226 | arc_write(zio_t *pio, spa_t *spa, uint64_t txg, |
d3c2ae1c | 7227 | blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, |
b5256303 TC |
7228 | const zio_prop_t *zp, arc_write_done_func_t *ready, |
7229 | arc_write_done_func_t *children_ready, arc_write_done_func_t *physdone, | |
7230 | arc_write_done_func_t *done, void *private, zio_priority_t priority, | |
5dbd68a3 | 7231 | int zio_flags, const zbookmark_phys_t *zb) |
34dc7c2f BB |
7232 | { |
7233 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
7234 | arc_write_callback_t *callback; | |
b128c09f | 7235 | zio_t *zio; |
82644107 | 7236 | zio_prop_t localprop = *zp; |
34dc7c2f | 7237 | |
d3c2ae1c GW |
7238 | ASSERT3P(ready, !=, NULL); |
7239 | ASSERT3P(done, !=, NULL); | |
34dc7c2f | 7240 | ASSERT(!HDR_IO_ERROR(hdr)); |
b9541d6b | 7241 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
d3c2ae1c GW |
7242 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
7243 | ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); | |
b128c09f | 7244 | if (l2arc) |
d3c2ae1c | 7245 | arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); |
82644107 | 7246 | |
b5256303 TC |
7247 | if (ARC_BUF_ENCRYPTED(buf)) { |
7248 | ASSERT(ARC_BUF_COMPRESSED(buf)); | |
7249 | localprop.zp_encrypt = B_TRUE; | |
7250 | localprop.zp_compress = HDR_GET_COMPRESS(hdr); | |
7251 | localprop.zp_byteorder = | |
7252 | (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ? | |
7253 | ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER; | |
7254 | bcopy(hdr->b_crypt_hdr.b_salt, localprop.zp_salt, | |
7255 | ZIO_DATA_SALT_LEN); | |
7256 | bcopy(hdr->b_crypt_hdr.b_iv, localprop.zp_iv, | |
7257 | ZIO_DATA_IV_LEN); | |
7258 | bcopy(hdr->b_crypt_hdr.b_mac, localprop.zp_mac, | |
7259 | ZIO_DATA_MAC_LEN); | |
7260 | if (DMU_OT_IS_ENCRYPTED(localprop.zp_type)) { | |
7261 | localprop.zp_nopwrite = B_FALSE; | |
7262 | localprop.zp_copies = | |
7263 | MIN(localprop.zp_copies, SPA_DVAS_PER_BP - 1); | |
7264 | } | |
2aa34383 | 7265 | zio_flags |= ZIO_FLAG_RAW; |
b5256303 TC |
7266 | } else if (ARC_BUF_COMPRESSED(buf)) { |
7267 | ASSERT3U(HDR_GET_LSIZE(hdr), !=, arc_buf_size(buf)); | |
7268 | localprop.zp_compress = HDR_GET_COMPRESS(hdr); | |
7269 | zio_flags |= ZIO_FLAG_RAW_COMPRESS; | |
2aa34383 | 7270 | } |
79c76d5b | 7271 | callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); |
34dc7c2f | 7272 | callback->awcb_ready = ready; |
bc77ba73 | 7273 | callback->awcb_children_ready = children_ready; |
e8b96c60 | 7274 | callback->awcb_physdone = physdone; |
34dc7c2f BB |
7275 | callback->awcb_done = done; |
7276 | callback->awcb_private = private; | |
7277 | callback->awcb_buf = buf; | |
b128c09f | 7278 | |
d3c2ae1c | 7279 | /* |
a6255b7f | 7280 | * The hdr's b_pabd is now stale, free it now. A new data block |
d3c2ae1c GW |
7281 | * will be allocated when the zio pipeline calls arc_write_ready(). |
7282 | */ | |
a6255b7f | 7283 | if (hdr->b_l1hdr.b_pabd != NULL) { |
d3c2ae1c GW |
7284 | /* |
7285 | * If the buf is currently sharing the data block with | |
7286 | * the hdr then we need to break that relationship here. | |
7287 | * The hdr will remain with a NULL data pointer and the | |
7288 | * buf will take sole ownership of the block. | |
7289 | */ | |
7290 | if (arc_buf_is_shared(buf)) { | |
d3c2ae1c GW |
7291 | arc_unshare_buf(hdr, buf); |
7292 | } else { | |
b5256303 | 7293 | arc_hdr_free_abd(hdr, B_FALSE); |
d3c2ae1c GW |
7294 | } |
7295 | VERIFY3P(buf->b_data, !=, NULL); | |
d3c2ae1c | 7296 | } |
b5256303 TC |
7297 | |
7298 | if (HDR_HAS_RABD(hdr)) | |
7299 | arc_hdr_free_abd(hdr, B_TRUE); | |
7300 | ||
71a24c3c TC |
7301 | if (!(zio_flags & ZIO_FLAG_RAW)) |
7302 | arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF); | |
b5256303 | 7303 | |
d3c2ae1c | 7304 | ASSERT(!arc_buf_is_shared(buf)); |
a6255b7f | 7305 | ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); |
d3c2ae1c | 7306 | |
a6255b7f DQ |
7307 | zio = zio_write(pio, spa, txg, bp, |
7308 | abd_get_from_buf(buf->b_data, HDR_GET_LSIZE(hdr)), | |
82644107 | 7309 | HDR_GET_LSIZE(hdr), arc_buf_size(buf), &localprop, arc_write_ready, |
bc77ba73 PD |
7310 | (children_ready != NULL) ? arc_write_children_ready : NULL, |
7311 | arc_write_physdone, arc_write_done, callback, | |
e8b96c60 | 7312 | priority, zio_flags, zb); |
34dc7c2f BB |
7313 | |
7314 | return (zio); | |
7315 | } | |
7316 | ||
34dc7c2f | 7317 | static int |
dae3e9ea | 7318 | arc_memory_throttle(spa_t *spa, uint64_t reserve, uint64_t txg) |
34dc7c2f BB |
7319 | { |
7320 | #ifdef _KERNEL | |
70f02287 | 7321 | uint64_t available_memory = arc_free_memory(); |
0c5493d4 | 7322 | |
70f02287 | 7323 | #if defined(_ILP32) |
9edb3695 BB |
7324 | available_memory = |
7325 | MIN(available_memory, vmem_size(heap_arena, VMEM_FREE)); | |
7326 | #endif | |
7327 | ||
7328 | if (available_memory > arc_all_memory() * arc_lotsfree_percent / 100) | |
ca67b33a MA |
7329 | return (0); |
7330 | ||
dae3e9ea DB |
7331 | if (txg > spa->spa_lowmem_last_txg) { |
7332 | spa->spa_lowmem_last_txg = txg; | |
7333 | spa->spa_lowmem_page_load = 0; | |
7e8bddd0 | 7334 | } |
7e8bddd0 BB |
7335 | /* |
7336 | * If we are in pageout, we know that memory is already tight, | |
7337 | * the arc is already going to be evicting, so we just want to | |
7338 | * continue to let page writes occur as quickly as possible. | |
7339 | */ | |
7340 | if (current_is_kswapd()) { | |
dae3e9ea DB |
7341 | if (spa->spa_lowmem_page_load > |
7342 | MAX(arc_sys_free / 4, available_memory) / 4) { | |
7e8bddd0 BB |
7343 | DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim); |
7344 | return (SET_ERROR(ERESTART)); | |
7345 | } | |
7346 | /* Note: reserve is inflated, so we deflate */ | |
dae3e9ea | 7347 | atomic_add_64(&spa->spa_lowmem_page_load, reserve / 8); |
7e8bddd0 | 7348 | return (0); |
dae3e9ea | 7349 | } else if (spa->spa_lowmem_page_load > 0 && arc_reclaim_needed()) { |
ca67b33a | 7350 | /* memory is low, delay before restarting */ |
34dc7c2f | 7351 | ARCSTAT_INCR(arcstat_memory_throttle_count, 1); |
570827e1 | 7352 | DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim); |
2e528b49 | 7353 | return (SET_ERROR(EAGAIN)); |
34dc7c2f | 7354 | } |
dae3e9ea DB |
7355 | spa->spa_lowmem_page_load = 0; |
7356 | #endif /* _KERNEL */ | |
34dc7c2f BB |
7357 | return (0); |
7358 | } | |
7359 | ||
7360 | void | |
7361 | arc_tempreserve_clear(uint64_t reserve) | |
7362 | { | |
7363 | atomic_add_64(&arc_tempreserve, -reserve); | |
7364 | ASSERT((int64_t)arc_tempreserve >= 0); | |
7365 | } | |
7366 | ||
7367 | int | |
dae3e9ea | 7368 | arc_tempreserve_space(spa_t *spa, uint64_t reserve, uint64_t txg) |
34dc7c2f BB |
7369 | { |
7370 | int error; | |
9babb374 | 7371 | uint64_t anon_size; |
34dc7c2f | 7372 | |
1b8951b3 TC |
7373 | if (!arc_no_grow && |
7374 | reserve > arc_c/4 && | |
7375 | reserve * 4 > (2ULL << SPA_MAXBLOCKSHIFT)) | |
34dc7c2f | 7376 | arc_c = MIN(arc_c_max, reserve * 4); |
12f9a6a3 BB |
7377 | |
7378 | /* | |
7379 | * Throttle when the calculated memory footprint for the TXG | |
7380 | * exceeds the target ARC size. | |
7381 | */ | |
570827e1 BB |
7382 | if (reserve > arc_c) { |
7383 | DMU_TX_STAT_BUMP(dmu_tx_memory_reserve); | |
12f9a6a3 | 7384 | return (SET_ERROR(ERESTART)); |
570827e1 | 7385 | } |
34dc7c2f | 7386 | |
9babb374 BB |
7387 | /* |
7388 | * Don't count loaned bufs as in flight dirty data to prevent long | |
7389 | * network delays from blocking transactions that are ready to be | |
7390 | * assigned to a txg. | |
7391 | */ | |
a7004725 DK |
7392 | |
7393 | /* assert that it has not wrapped around */ | |
7394 | ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); | |
7395 | ||
424fd7c3 | 7396 | anon_size = MAX((int64_t)(zfs_refcount_count(&arc_anon->arcs_size) - |
36da08ef | 7397 | arc_loaned_bytes), 0); |
9babb374 | 7398 | |
34dc7c2f BB |
7399 | /* |
7400 | * Writes will, almost always, require additional memory allocations | |
d3cc8b15 | 7401 | * in order to compress/encrypt/etc the data. We therefore need to |
34dc7c2f BB |
7402 | * make sure that there is sufficient available memory for this. |
7403 | */ | |
dae3e9ea | 7404 | error = arc_memory_throttle(spa, reserve, txg); |
e8b96c60 | 7405 | if (error != 0) |
34dc7c2f BB |
7406 | return (error); |
7407 | ||
7408 | /* | |
7409 | * Throttle writes when the amount of dirty data in the cache | |
7410 | * gets too large. We try to keep the cache less than half full | |
7411 | * of dirty blocks so that our sync times don't grow too large. | |
dae3e9ea DB |
7412 | * |
7413 | * In the case of one pool being built on another pool, we want | |
7414 | * to make sure we don't end up throttling the lower (backing) | |
7415 | * pool when the upper pool is the majority contributor to dirty | |
7416 | * data. To insure we make forward progress during throttling, we | |
7417 | * also check the current pool's net dirty data and only throttle | |
7418 | * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty | |
7419 | * data in the cache. | |
7420 | * | |
34dc7c2f BB |
7421 | * Note: if two requests come in concurrently, we might let them |
7422 | * both succeed, when one of them should fail. Not a huge deal. | |
7423 | */ | |
dae3e9ea DB |
7424 | uint64_t total_dirty = reserve + arc_tempreserve + anon_size; |
7425 | uint64_t spa_dirty_anon = spa_dirty_data(spa); | |
9babb374 | 7426 | |
dae3e9ea DB |
7427 | if (total_dirty > arc_c * zfs_arc_dirty_limit_percent / 100 && |
7428 | anon_size > arc_c * zfs_arc_anon_limit_percent / 100 && | |
7429 | spa_dirty_anon > anon_size * zfs_arc_pool_dirty_percent / 100) { | |
2fd92c3d | 7430 | #ifdef ZFS_DEBUG |
424fd7c3 TS |
7431 | uint64_t meta_esize = zfs_refcount_count( |
7432 | &arc_anon->arcs_esize[ARC_BUFC_METADATA]); | |
d3c2ae1c | 7433 | uint64_t data_esize = |
424fd7c3 | 7434 | zfs_refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]); |
34dc7c2f BB |
7435 | dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " |
7436 | "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", | |
d3c2ae1c GW |
7437 | arc_tempreserve >> 10, meta_esize >> 10, |
7438 | data_esize >> 10, reserve >> 10, arc_c >> 10); | |
2fd92c3d | 7439 | #endif |
570827e1 | 7440 | DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle); |
2e528b49 | 7441 | return (SET_ERROR(ERESTART)); |
34dc7c2f BB |
7442 | } |
7443 | atomic_add_64(&arc_tempreserve, reserve); | |
7444 | return (0); | |
7445 | } | |
7446 | ||
13be560d BB |
7447 | static void |
7448 | arc_kstat_update_state(arc_state_t *state, kstat_named_t *size, | |
7449 | kstat_named_t *evict_data, kstat_named_t *evict_metadata) | |
7450 | { | |
424fd7c3 | 7451 | size->value.ui64 = zfs_refcount_count(&state->arcs_size); |
d3c2ae1c | 7452 | evict_data->value.ui64 = |
424fd7c3 | 7453 | zfs_refcount_count(&state->arcs_esize[ARC_BUFC_DATA]); |
d3c2ae1c | 7454 | evict_metadata->value.ui64 = |
424fd7c3 | 7455 | zfs_refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]); |
13be560d BB |
7456 | } |
7457 | ||
7458 | static int | |
7459 | arc_kstat_update(kstat_t *ksp, int rw) | |
7460 | { | |
7461 | arc_stats_t *as = ksp->ks_data; | |
7462 | ||
7463 | if (rw == KSTAT_WRITE) { | |
ecb2b7dc | 7464 | return (SET_ERROR(EACCES)); |
13be560d BB |
7465 | } else { |
7466 | arc_kstat_update_state(arc_anon, | |
7467 | &as->arcstat_anon_size, | |
500445c0 PS |
7468 | &as->arcstat_anon_evictable_data, |
7469 | &as->arcstat_anon_evictable_metadata); | |
13be560d BB |
7470 | arc_kstat_update_state(arc_mru, |
7471 | &as->arcstat_mru_size, | |
500445c0 PS |
7472 | &as->arcstat_mru_evictable_data, |
7473 | &as->arcstat_mru_evictable_metadata); | |
13be560d BB |
7474 | arc_kstat_update_state(arc_mru_ghost, |
7475 | &as->arcstat_mru_ghost_size, | |
500445c0 PS |
7476 | &as->arcstat_mru_ghost_evictable_data, |
7477 | &as->arcstat_mru_ghost_evictable_metadata); | |
13be560d BB |
7478 | arc_kstat_update_state(arc_mfu, |
7479 | &as->arcstat_mfu_size, | |
500445c0 PS |
7480 | &as->arcstat_mfu_evictable_data, |
7481 | &as->arcstat_mfu_evictable_metadata); | |
fc41c640 | 7482 | arc_kstat_update_state(arc_mfu_ghost, |
13be560d | 7483 | &as->arcstat_mfu_ghost_size, |
500445c0 PS |
7484 | &as->arcstat_mfu_ghost_evictable_data, |
7485 | &as->arcstat_mfu_ghost_evictable_metadata); | |
70f02287 | 7486 | |
37fb3e43 PD |
7487 | ARCSTAT(arcstat_size) = aggsum_value(&arc_size); |
7488 | ARCSTAT(arcstat_meta_used) = aggsum_value(&arc_meta_used); | |
7489 | ARCSTAT(arcstat_data_size) = aggsum_value(&astat_data_size); | |
7490 | ARCSTAT(arcstat_metadata_size) = | |
7491 | aggsum_value(&astat_metadata_size); | |
7492 | ARCSTAT(arcstat_hdr_size) = aggsum_value(&astat_hdr_size); | |
7493 | ARCSTAT(arcstat_l2_hdr_size) = aggsum_value(&astat_l2_hdr_size); | |
7494 | ARCSTAT(arcstat_dbuf_size) = aggsum_value(&astat_dbuf_size); | |
7495 | ARCSTAT(arcstat_dnode_size) = aggsum_value(&astat_dnode_size); | |
7496 | ARCSTAT(arcstat_bonus_size) = aggsum_value(&astat_bonus_size); | |
7497 | ||
70f02287 BB |
7498 | as->arcstat_memory_all_bytes.value.ui64 = |
7499 | arc_all_memory(); | |
7500 | as->arcstat_memory_free_bytes.value.ui64 = | |
7501 | arc_free_memory(); | |
7502 | as->arcstat_memory_available_bytes.value.i64 = | |
7503 | arc_available_memory(); | |
13be560d BB |
7504 | } |
7505 | ||
7506 | return (0); | |
7507 | } | |
7508 | ||
ca0bf58d PS |
7509 | /* |
7510 | * This function *must* return indices evenly distributed between all | |
7511 | * sublists of the multilist. This is needed due to how the ARC eviction | |
7512 | * code is laid out; arc_evict_state() assumes ARC buffers are evenly | |
7513 | * distributed between all sublists and uses this assumption when | |
7514 | * deciding which sublist to evict from and how much to evict from it. | |
7515 | */ | |
7516 | unsigned int | |
7517 | arc_state_multilist_index_func(multilist_t *ml, void *obj) | |
7518 | { | |
7519 | arc_buf_hdr_t *hdr = obj; | |
7520 | ||
7521 | /* | |
7522 | * We rely on b_dva to generate evenly distributed index | |
7523 | * numbers using buf_hash below. So, as an added precaution, | |
7524 | * let's make sure we never add empty buffers to the arc lists. | |
7525 | */ | |
d3c2ae1c | 7526 | ASSERT(!HDR_EMPTY(hdr)); |
ca0bf58d PS |
7527 | |
7528 | /* | |
7529 | * The assumption here, is the hash value for a given | |
7530 | * arc_buf_hdr_t will remain constant throughout its lifetime | |
7531 | * (i.e. its b_spa, b_dva, and b_birth fields don't change). | |
7532 | * Thus, we don't need to store the header's sublist index | |
7533 | * on insertion, as this index can be recalculated on removal. | |
7534 | * | |
7535 | * Also, the low order bits of the hash value are thought to be | |
7536 | * distributed evenly. Otherwise, in the case that the multilist | |
7537 | * has a power of two number of sublists, each sublists' usage | |
7538 | * would not be evenly distributed. | |
7539 | */ | |
7540 | return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % | |
7541 | multilist_get_num_sublists(ml)); | |
7542 | } | |
7543 | ||
ca67b33a MA |
7544 | /* |
7545 | * Called during module initialization and periodically thereafter to | |
7546 | * apply reasonable changes to the exposed performance tunings. Non-zero | |
7547 | * zfs_* values which differ from the currently set values will be applied. | |
7548 | */ | |
7549 | static void | |
7550 | arc_tuning_update(void) | |
7551 | { | |
b8a97fb1 | 7552 | uint64_t allmem = arc_all_memory(); |
7553 | unsigned long limit; | |
9edb3695 | 7554 | |
ca67b33a MA |
7555 | /* Valid range: 64M - <all physical memory> */ |
7556 | if ((zfs_arc_max) && (zfs_arc_max != arc_c_max) && | |
7403d074 | 7557 | (zfs_arc_max >= 64 << 20) && (zfs_arc_max < allmem) && |
ca67b33a MA |
7558 | (zfs_arc_max > arc_c_min)) { |
7559 | arc_c_max = zfs_arc_max; | |
7560 | arc_c = arc_c_max; | |
7561 | arc_p = (arc_c >> 1); | |
b8a97fb1 | 7562 | if (arc_meta_limit > arc_c_max) |
7563 | arc_meta_limit = arc_c_max; | |
7564 | if (arc_dnode_limit > arc_meta_limit) | |
7565 | arc_dnode_limit = arc_meta_limit; | |
ca67b33a MA |
7566 | } |
7567 | ||
7568 | /* Valid range: 32M - <arc_c_max> */ | |
7569 | if ((zfs_arc_min) && (zfs_arc_min != arc_c_min) && | |
7570 | (zfs_arc_min >= 2ULL << SPA_MAXBLOCKSHIFT) && | |
7571 | (zfs_arc_min <= arc_c_max)) { | |
7572 | arc_c_min = zfs_arc_min; | |
7573 | arc_c = MAX(arc_c, arc_c_min); | |
7574 | } | |
7575 | ||
7576 | /* Valid range: 16M - <arc_c_max> */ | |
7577 | if ((zfs_arc_meta_min) && (zfs_arc_meta_min != arc_meta_min) && | |
7578 | (zfs_arc_meta_min >= 1ULL << SPA_MAXBLOCKSHIFT) && | |
7579 | (zfs_arc_meta_min <= arc_c_max)) { | |
7580 | arc_meta_min = zfs_arc_meta_min; | |
b8a97fb1 | 7581 | if (arc_meta_limit < arc_meta_min) |
7582 | arc_meta_limit = arc_meta_min; | |
7583 | if (arc_dnode_limit < arc_meta_min) | |
7584 | arc_dnode_limit = arc_meta_min; | |
ca67b33a MA |
7585 | } |
7586 | ||
7587 | /* Valid range: <arc_meta_min> - <arc_c_max> */ | |
b8a97fb1 | 7588 | limit = zfs_arc_meta_limit ? zfs_arc_meta_limit : |
7589 | MIN(zfs_arc_meta_limit_percent, 100) * arc_c_max / 100; | |
7590 | if ((limit != arc_meta_limit) && | |
7591 | (limit >= arc_meta_min) && | |
7592 | (limit <= arc_c_max)) | |
7593 | arc_meta_limit = limit; | |
7594 | ||
7595 | /* Valid range: <arc_meta_min> - <arc_meta_limit> */ | |
7596 | limit = zfs_arc_dnode_limit ? zfs_arc_dnode_limit : | |
7597 | MIN(zfs_arc_dnode_limit_percent, 100) * arc_meta_limit / 100; | |
7598 | if ((limit != arc_dnode_limit) && | |
7599 | (limit >= arc_meta_min) && | |
7600 | (limit <= arc_meta_limit)) | |
7601 | arc_dnode_limit = limit; | |
25458cbe | 7602 | |
ca67b33a MA |
7603 | /* Valid range: 1 - N */ |
7604 | if (zfs_arc_grow_retry) | |
7605 | arc_grow_retry = zfs_arc_grow_retry; | |
7606 | ||
7607 | /* Valid range: 1 - N */ | |
7608 | if (zfs_arc_shrink_shift) { | |
7609 | arc_shrink_shift = zfs_arc_shrink_shift; | |
7610 | arc_no_grow_shift = MIN(arc_no_grow_shift, arc_shrink_shift -1); | |
7611 | } | |
7612 | ||
728d6ae9 BB |
7613 | /* Valid range: 1 - N */ |
7614 | if (zfs_arc_p_min_shift) | |
7615 | arc_p_min_shift = zfs_arc_p_min_shift; | |
7616 | ||
d4a72f23 TC |
7617 | /* Valid range: 1 - N ms */ |
7618 | if (zfs_arc_min_prefetch_ms) | |
7619 | arc_min_prefetch_ms = zfs_arc_min_prefetch_ms; | |
7620 | ||
7621 | /* Valid range: 1 - N ms */ | |
7622 | if (zfs_arc_min_prescient_prefetch_ms) { | |
7623 | arc_min_prescient_prefetch_ms = | |
7624 | zfs_arc_min_prescient_prefetch_ms; | |
7625 | } | |
11f552fa | 7626 | |
7e8bddd0 BB |
7627 | /* Valid range: 0 - 100 */ |
7628 | if ((zfs_arc_lotsfree_percent >= 0) && | |
7629 | (zfs_arc_lotsfree_percent <= 100)) | |
7630 | arc_lotsfree_percent = zfs_arc_lotsfree_percent; | |
7631 | ||
11f552fa BB |
7632 | /* Valid range: 0 - <all physical memory> */ |
7633 | if ((zfs_arc_sys_free) && (zfs_arc_sys_free != arc_sys_free)) | |
9edb3695 | 7634 | arc_sys_free = MIN(MAX(zfs_arc_sys_free, 0), allmem); |
7e8bddd0 | 7635 | |
ca67b33a MA |
7636 | } |
7637 | ||
d3c2ae1c GW |
7638 | static void |
7639 | arc_state_init(void) | |
7640 | { | |
7641 | arc_anon = &ARC_anon; | |
7642 | arc_mru = &ARC_mru; | |
7643 | arc_mru_ghost = &ARC_mru_ghost; | |
7644 | arc_mfu = &ARC_mfu; | |
7645 | arc_mfu_ghost = &ARC_mfu_ghost; | |
7646 | arc_l2c_only = &ARC_l2c_only; | |
7647 | ||
64fc7762 MA |
7648 | arc_mru->arcs_list[ARC_BUFC_METADATA] = |
7649 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7650 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7651 | arc_state_multilist_index_func); |
64fc7762 MA |
7652 | arc_mru->arcs_list[ARC_BUFC_DATA] = |
7653 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7654 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7655 | arc_state_multilist_index_func); |
64fc7762 MA |
7656 | arc_mru_ghost->arcs_list[ARC_BUFC_METADATA] = |
7657 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7658 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7659 | arc_state_multilist_index_func); |
64fc7762 MA |
7660 | arc_mru_ghost->arcs_list[ARC_BUFC_DATA] = |
7661 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7662 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7663 | arc_state_multilist_index_func); |
64fc7762 MA |
7664 | arc_mfu->arcs_list[ARC_BUFC_METADATA] = |
7665 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7666 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7667 | arc_state_multilist_index_func); |
64fc7762 MA |
7668 | arc_mfu->arcs_list[ARC_BUFC_DATA] = |
7669 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7670 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7671 | arc_state_multilist_index_func); |
64fc7762 MA |
7672 | arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA] = |
7673 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7674 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7675 | arc_state_multilist_index_func); |
64fc7762 MA |
7676 | arc_mfu_ghost->arcs_list[ARC_BUFC_DATA] = |
7677 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7678 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7679 | arc_state_multilist_index_func); |
64fc7762 MA |
7680 | arc_l2c_only->arcs_list[ARC_BUFC_METADATA] = |
7681 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7682 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7683 | arc_state_multilist_index_func); |
64fc7762 MA |
7684 | arc_l2c_only->arcs_list[ARC_BUFC_DATA] = |
7685 | multilist_create(sizeof (arc_buf_hdr_t), | |
d3c2ae1c | 7686 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
c30e58c4 | 7687 | arc_state_multilist_index_func); |
d3c2ae1c | 7688 | |
424fd7c3 TS |
7689 | zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); |
7690 | zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]); | |
7691 | zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); | |
7692 | zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]); | |
7693 | zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7694 | zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7695 | zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); | |
7696 | zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); | |
7697 | zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7698 | zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7699 | zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); | |
7700 | zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); | |
7701 | ||
7702 | zfs_refcount_create(&arc_anon->arcs_size); | |
7703 | zfs_refcount_create(&arc_mru->arcs_size); | |
7704 | zfs_refcount_create(&arc_mru_ghost->arcs_size); | |
7705 | zfs_refcount_create(&arc_mfu->arcs_size); | |
7706 | zfs_refcount_create(&arc_mfu_ghost->arcs_size); | |
7707 | zfs_refcount_create(&arc_l2c_only->arcs_size); | |
d3c2ae1c | 7708 | |
37fb3e43 PD |
7709 | aggsum_init(&arc_meta_used, 0); |
7710 | aggsum_init(&arc_size, 0); | |
7711 | aggsum_init(&astat_data_size, 0); | |
7712 | aggsum_init(&astat_metadata_size, 0); | |
7713 | aggsum_init(&astat_hdr_size, 0); | |
7714 | aggsum_init(&astat_l2_hdr_size, 0); | |
7715 | aggsum_init(&astat_bonus_size, 0); | |
7716 | aggsum_init(&astat_dnode_size, 0); | |
7717 | aggsum_init(&astat_dbuf_size, 0); | |
7718 | ||
d3c2ae1c GW |
7719 | arc_anon->arcs_state = ARC_STATE_ANON; |
7720 | arc_mru->arcs_state = ARC_STATE_MRU; | |
7721 | arc_mru_ghost->arcs_state = ARC_STATE_MRU_GHOST; | |
7722 | arc_mfu->arcs_state = ARC_STATE_MFU; | |
7723 | arc_mfu_ghost->arcs_state = ARC_STATE_MFU_GHOST; | |
7724 | arc_l2c_only->arcs_state = ARC_STATE_L2C_ONLY; | |
7725 | } | |
7726 | ||
7727 | static void | |
7728 | arc_state_fini(void) | |
7729 | { | |
424fd7c3 TS |
7730 | zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); |
7731 | zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]); | |
7732 | zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); | |
7733 | zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]); | |
7734 | zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7735 | zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7736 | zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); | |
7737 | zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); | |
7738 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); | |
7739 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); | |
7740 | zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); | |
7741 | zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); | |
7742 | ||
7743 | zfs_refcount_destroy(&arc_anon->arcs_size); | |
7744 | zfs_refcount_destroy(&arc_mru->arcs_size); | |
7745 | zfs_refcount_destroy(&arc_mru_ghost->arcs_size); | |
7746 | zfs_refcount_destroy(&arc_mfu->arcs_size); | |
7747 | zfs_refcount_destroy(&arc_mfu_ghost->arcs_size); | |
7748 | zfs_refcount_destroy(&arc_l2c_only->arcs_size); | |
d3c2ae1c | 7749 | |
64fc7762 MA |
7750 | multilist_destroy(arc_mru->arcs_list[ARC_BUFC_METADATA]); |
7751 | multilist_destroy(arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); | |
7752 | multilist_destroy(arc_mfu->arcs_list[ARC_BUFC_METADATA]); | |
7753 | multilist_destroy(arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); | |
7754 | multilist_destroy(arc_mru->arcs_list[ARC_BUFC_DATA]); | |
7755 | multilist_destroy(arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); | |
7756 | multilist_destroy(arc_mfu->arcs_list[ARC_BUFC_DATA]); | |
7757 | multilist_destroy(arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); | |
7758 | multilist_destroy(arc_l2c_only->arcs_list[ARC_BUFC_METADATA]); | |
7759 | multilist_destroy(arc_l2c_only->arcs_list[ARC_BUFC_DATA]); | |
37fb3e43 PD |
7760 | |
7761 | aggsum_fini(&arc_meta_used); | |
7762 | aggsum_fini(&arc_size); | |
7763 | aggsum_fini(&astat_data_size); | |
7764 | aggsum_fini(&astat_metadata_size); | |
7765 | aggsum_fini(&astat_hdr_size); | |
7766 | aggsum_fini(&astat_l2_hdr_size); | |
7767 | aggsum_fini(&astat_bonus_size); | |
7768 | aggsum_fini(&astat_dnode_size); | |
7769 | aggsum_fini(&astat_dbuf_size); | |
d3c2ae1c GW |
7770 | } |
7771 | ||
7772 | uint64_t | |
e71cade6 | 7773 | arc_target_bytes(void) |
d3c2ae1c | 7774 | { |
e71cade6 | 7775 | return (arc_c); |
d3c2ae1c GW |
7776 | } |
7777 | ||
34dc7c2f BB |
7778 | void |
7779 | arc_init(void) | |
7780 | { | |
9edb3695 | 7781 | uint64_t percent, allmem = arc_all_memory(); |
3ec34e55 BL |
7782 | mutex_init(&arc_adjust_lock, NULL, MUTEX_DEFAULT, NULL); |
7783 | cv_init(&arc_adjust_waiters_cv, NULL, CV_DEFAULT, NULL); | |
ca0bf58d | 7784 | |
2b84817f TC |
7785 | arc_min_prefetch_ms = 1000; |
7786 | arc_min_prescient_prefetch_ms = 6000; | |
34dc7c2f | 7787 | |
34dc7c2f | 7788 | #ifdef _KERNEL |
7cb67b45 BB |
7789 | /* |
7790 | * Register a shrinker to support synchronous (direct) memory | |
7791 | * reclaim from the arc. This is done to prevent kswapd from | |
7792 | * swapping out pages when it is preferable to shrink the arc. | |
7793 | */ | |
7794 | spl_register_shrinker(&arc_shrinker); | |
11f552fa BB |
7795 | |
7796 | /* Set to 1/64 of all memory or a minimum of 512K */ | |
9edb3695 | 7797 | arc_sys_free = MAX(allmem / 64, (512 * 1024)); |
11f552fa | 7798 | arc_need_free = 0; |
34dc7c2f BB |
7799 | #endif |
7800 | ||
0a1f8cd9 TC |
7801 | /* Set max to 1/2 of all memory */ |
7802 | arc_c_max = allmem / 2; | |
7803 | ||
4ce3c45a BB |
7804 | #ifdef _KERNEL |
7805 | /* Set min cache to 1/32 of all memory, or 32MB, whichever is more */ | |
7806 | arc_c_min = MAX(allmem / 32, 2ULL << SPA_MAXBLOCKSHIFT); | |
7807 | #else | |
ab5cbbd1 BB |
7808 | /* |
7809 | * In userland, there's only the memory pressure that we artificially | |
7810 | * create (see arc_available_memory()). Don't let arc_c get too | |
7811 | * small, because it can cause transactions to be larger than | |
7812 | * arc_c, causing arc_tempreserve_space() to fail. | |
7813 | */ | |
0a1f8cd9 | 7814 | arc_c_min = MAX(arc_c_max / 2, 2ULL << SPA_MAXBLOCKSHIFT); |
ab5cbbd1 BB |
7815 | #endif |
7816 | ||
34dc7c2f BB |
7817 | arc_c = arc_c_max; |
7818 | arc_p = (arc_c >> 1); | |
7819 | ||
ca67b33a MA |
7820 | /* Set min to 1/2 of arc_c_min */ |
7821 | arc_meta_min = 1ULL << SPA_MAXBLOCKSHIFT; | |
7822 | /* Initialize maximum observed usage to zero */ | |
1834f2d8 | 7823 | arc_meta_max = 0; |
9907cc1c G |
7824 | /* |
7825 | * Set arc_meta_limit to a percent of arc_c_max with a floor of | |
7826 | * arc_meta_min, and a ceiling of arc_c_max. | |
7827 | */ | |
7828 | percent = MIN(zfs_arc_meta_limit_percent, 100); | |
7829 | arc_meta_limit = MAX(arc_meta_min, (percent * arc_c_max) / 100); | |
7830 | percent = MIN(zfs_arc_dnode_limit_percent, 100); | |
7831 | arc_dnode_limit = (percent * arc_meta_limit) / 100; | |
34dc7c2f | 7832 | |
ca67b33a MA |
7833 | /* Apply user specified tunings */ |
7834 | arc_tuning_update(); | |
c52fca13 | 7835 | |
34dc7c2f BB |
7836 | /* if kmem_flags are set, lets try to use less memory */ |
7837 | if (kmem_debugging()) | |
7838 | arc_c = arc_c / 2; | |
7839 | if (arc_c < arc_c_min) | |
7840 | arc_c = arc_c_min; | |
7841 | ||
d3c2ae1c | 7842 | arc_state_init(); |
3ec34e55 BL |
7843 | |
7844 | /* | |
7845 | * The arc must be "uninitialized", so that hdr_recl() (which is | |
7846 | * registered by buf_init()) will not access arc_reap_zthr before | |
7847 | * it is created. | |
7848 | */ | |
7849 | ASSERT(!arc_initialized); | |
34dc7c2f BB |
7850 | buf_init(); |
7851 | ||
ab26409d BB |
7852 | list_create(&arc_prune_list, sizeof (arc_prune_t), |
7853 | offsetof(arc_prune_t, p_node)); | |
ab26409d | 7854 | mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f | 7855 | |
1229323d | 7856 | arc_prune_taskq = taskq_create("arc_prune", max_ncpus, defclsyspri, |
aa9af22c | 7857 | max_ncpus, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC); |
f6046738 | 7858 | |
34dc7c2f BB |
7859 | arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, |
7860 | sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); | |
7861 | ||
7862 | if (arc_ksp != NULL) { | |
7863 | arc_ksp->ks_data = &arc_stats; | |
13be560d | 7864 | arc_ksp->ks_update = arc_kstat_update; |
34dc7c2f BB |
7865 | kstat_install(arc_ksp); |
7866 | } | |
7867 | ||
3ec34e55 BL |
7868 | arc_adjust_zthr = zthr_create(arc_adjust_cb_check, |
7869 | arc_adjust_cb, NULL); | |
7870 | arc_reap_zthr = zthr_create_timer(arc_reap_cb_check, | |
7871 | arc_reap_cb, NULL, SEC2NSEC(1)); | |
34dc7c2f | 7872 | |
3ec34e55 | 7873 | arc_initialized = B_TRUE; |
b128c09f | 7874 | arc_warm = B_FALSE; |
34dc7c2f | 7875 | |
e8b96c60 MA |
7876 | /* |
7877 | * Calculate maximum amount of dirty data per pool. | |
7878 | * | |
7879 | * If it has been set by a module parameter, take that. | |
7880 | * Otherwise, use a percentage of physical memory defined by | |
7881 | * zfs_dirty_data_max_percent (default 10%) with a cap at | |
e99932f7 | 7882 | * zfs_dirty_data_max_max (default 4G or 25% of physical memory). |
e8b96c60 MA |
7883 | */ |
7884 | if (zfs_dirty_data_max_max == 0) | |
e99932f7 BB |
7885 | zfs_dirty_data_max_max = MIN(4ULL * 1024 * 1024 * 1024, |
7886 | allmem * zfs_dirty_data_max_max_percent / 100); | |
e8b96c60 MA |
7887 | |
7888 | if (zfs_dirty_data_max == 0) { | |
9edb3695 | 7889 | zfs_dirty_data_max = allmem * |
e8b96c60 MA |
7890 | zfs_dirty_data_max_percent / 100; |
7891 | zfs_dirty_data_max = MIN(zfs_dirty_data_max, | |
7892 | zfs_dirty_data_max_max); | |
7893 | } | |
34dc7c2f BB |
7894 | } |
7895 | ||
7896 | void | |
7897 | arc_fini(void) | |
7898 | { | |
ab26409d BB |
7899 | arc_prune_t *p; |
7900 | ||
7cb67b45 BB |
7901 | #ifdef _KERNEL |
7902 | spl_unregister_shrinker(&arc_shrinker); | |
7903 | #endif /* _KERNEL */ | |
7904 | ||
d3c2ae1c GW |
7905 | /* Use B_TRUE to ensure *all* buffers are evicted */ |
7906 | arc_flush(NULL, B_TRUE); | |
34dc7c2f | 7907 | |
3ec34e55 | 7908 | arc_initialized = B_FALSE; |
34dc7c2f BB |
7909 | |
7910 | if (arc_ksp != NULL) { | |
7911 | kstat_delete(arc_ksp); | |
7912 | arc_ksp = NULL; | |
7913 | } | |
7914 | ||
f6046738 BB |
7915 | taskq_wait(arc_prune_taskq); |
7916 | taskq_destroy(arc_prune_taskq); | |
7917 | ||
ab26409d BB |
7918 | mutex_enter(&arc_prune_mtx); |
7919 | while ((p = list_head(&arc_prune_list)) != NULL) { | |
7920 | list_remove(&arc_prune_list, p); | |
424fd7c3 TS |
7921 | zfs_refcount_remove(&p->p_refcnt, &arc_prune_list); |
7922 | zfs_refcount_destroy(&p->p_refcnt); | |
ab26409d BB |
7923 | kmem_free(p, sizeof (*p)); |
7924 | } | |
7925 | mutex_exit(&arc_prune_mtx); | |
7926 | ||
7927 | list_destroy(&arc_prune_list); | |
7928 | mutex_destroy(&arc_prune_mtx); | |
3ec34e55 BL |
7929 | (void) zthr_cancel(arc_adjust_zthr); |
7930 | zthr_destroy(arc_adjust_zthr); | |
7931 | ||
7932 | (void) zthr_cancel(arc_reap_zthr); | |
7933 | zthr_destroy(arc_reap_zthr); | |
7934 | ||
7935 | mutex_destroy(&arc_adjust_lock); | |
7936 | cv_destroy(&arc_adjust_waiters_cv); | |
ca0bf58d | 7937 | |
ae3d8491 PD |
7938 | /* |
7939 | * buf_fini() must proceed arc_state_fini() because buf_fin() may | |
7940 | * trigger the release of kmem magazines, which can callback to | |
7941 | * arc_space_return() which accesses aggsums freed in act_state_fini(). | |
7942 | */ | |
34dc7c2f | 7943 | buf_fini(); |
ae3d8491 | 7944 | arc_state_fini(); |
9babb374 | 7945 | |
b9541d6b | 7946 | ASSERT0(arc_loaned_bytes); |
34dc7c2f BB |
7947 | } |
7948 | ||
7949 | /* | |
7950 | * Level 2 ARC | |
7951 | * | |
7952 | * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. | |
7953 | * It uses dedicated storage devices to hold cached data, which are populated | |
7954 | * using large infrequent writes. The main role of this cache is to boost | |
7955 | * the performance of random read workloads. The intended L2ARC devices | |
7956 | * include short-stroked disks, solid state disks, and other media with | |
7957 | * substantially faster read latency than disk. | |
7958 | * | |
7959 | * +-----------------------+ | |
7960 | * | ARC | | |
7961 | * +-----------------------+ | |
7962 | * | ^ ^ | |
7963 | * | | | | |
7964 | * l2arc_feed_thread() arc_read() | |
7965 | * | | | | |
7966 | * | l2arc read | | |
7967 | * V | | | |
7968 | * +---------------+ | | |
7969 | * | L2ARC | | | |
7970 | * +---------------+ | | |
7971 | * | ^ | | |
7972 | * l2arc_write() | | | |
7973 | * | | | | |
7974 | * V | | | |
7975 | * +-------+ +-------+ | |
7976 | * | vdev | | vdev | | |
7977 | * | cache | | cache | | |
7978 | * +-------+ +-------+ | |
7979 | * +=========+ .-----. | |
7980 | * : L2ARC : |-_____-| | |
7981 | * : devices : | Disks | | |
7982 | * +=========+ `-_____-' | |
7983 | * | |
7984 | * Read requests are satisfied from the following sources, in order: | |
7985 | * | |
7986 | * 1) ARC | |
7987 | * 2) vdev cache of L2ARC devices | |
7988 | * 3) L2ARC devices | |
7989 | * 4) vdev cache of disks | |
7990 | * 5) disks | |
7991 | * | |
7992 | * Some L2ARC device types exhibit extremely slow write performance. | |
7993 | * To accommodate for this there are some significant differences between | |
7994 | * the L2ARC and traditional cache design: | |
7995 | * | |
7996 | * 1. There is no eviction path from the ARC to the L2ARC. Evictions from | |
7997 | * the ARC behave as usual, freeing buffers and placing headers on ghost | |
7998 | * lists. The ARC does not send buffers to the L2ARC during eviction as | |
7999 | * this would add inflated write latencies for all ARC memory pressure. | |
8000 | * | |
8001 | * 2. The L2ARC attempts to cache data from the ARC before it is evicted. | |
8002 | * It does this by periodically scanning buffers from the eviction-end of | |
8003 | * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are | |
3a17a7a9 SK |
8004 | * not already there. It scans until a headroom of buffers is satisfied, |
8005 | * which itself is a buffer for ARC eviction. If a compressible buffer is | |
8006 | * found during scanning and selected for writing to an L2ARC device, we | |
8007 | * temporarily boost scanning headroom during the next scan cycle to make | |
8008 | * sure we adapt to compression effects (which might significantly reduce | |
8009 | * the data volume we write to L2ARC). The thread that does this is | |
34dc7c2f BB |
8010 | * l2arc_feed_thread(), illustrated below; example sizes are included to |
8011 | * provide a better sense of ratio than this diagram: | |
8012 | * | |
8013 | * head --> tail | |
8014 | * +---------------------+----------+ | |
8015 | * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC | |
8016 | * +---------------------+----------+ | o L2ARC eligible | |
8017 | * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer | |
8018 | * +---------------------+----------+ | | |
8019 | * 15.9 Gbytes ^ 32 Mbytes | | |
8020 | * headroom | | |
8021 | * l2arc_feed_thread() | |
8022 | * | | |
8023 | * l2arc write hand <--[oooo]--' | |
8024 | * | 8 Mbyte | |
8025 | * | write max | |
8026 | * V | |
8027 | * +==============================+ | |
8028 | * L2ARC dev |####|#|###|###| |####| ... | | |
8029 | * +==============================+ | |
8030 | * 32 Gbytes | |
8031 | * | |
8032 | * 3. If an ARC buffer is copied to the L2ARC but then hit instead of | |
8033 | * evicted, then the L2ARC has cached a buffer much sooner than it probably | |
8034 | * needed to, potentially wasting L2ARC device bandwidth and storage. It is | |
8035 | * safe to say that this is an uncommon case, since buffers at the end of | |
8036 | * the ARC lists have moved there due to inactivity. | |
8037 | * | |
8038 | * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, | |
8039 | * then the L2ARC simply misses copying some buffers. This serves as a | |
8040 | * pressure valve to prevent heavy read workloads from both stalling the ARC | |
8041 | * with waits and clogging the L2ARC with writes. This also helps prevent | |
8042 | * the potential for the L2ARC to churn if it attempts to cache content too | |
8043 | * quickly, such as during backups of the entire pool. | |
8044 | * | |
b128c09f BB |
8045 | * 5. After system boot and before the ARC has filled main memory, there are |
8046 | * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru | |
8047 | * lists can remain mostly static. Instead of searching from tail of these | |
8048 | * lists as pictured, the l2arc_feed_thread() will search from the list heads | |
8049 | * for eligible buffers, greatly increasing its chance of finding them. | |
8050 | * | |
8051 | * The L2ARC device write speed is also boosted during this time so that | |
8052 | * the L2ARC warms up faster. Since there have been no ARC evictions yet, | |
8053 | * there are no L2ARC reads, and no fear of degrading read performance | |
8054 | * through increased writes. | |
8055 | * | |
8056 | * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that | |
34dc7c2f BB |
8057 | * the vdev queue can aggregate them into larger and fewer writes. Each |
8058 | * device is written to in a rotor fashion, sweeping writes through | |
8059 | * available space then repeating. | |
8060 | * | |
b128c09f | 8061 | * 7. The L2ARC does not store dirty content. It never needs to flush |
34dc7c2f BB |
8062 | * write buffers back to disk based storage. |
8063 | * | |
b128c09f | 8064 | * 8. If an ARC buffer is written (and dirtied) which also exists in the |
34dc7c2f BB |
8065 | * L2ARC, the now stale L2ARC buffer is immediately dropped. |
8066 | * | |
8067 | * The performance of the L2ARC can be tweaked by a number of tunables, which | |
8068 | * may be necessary for different workloads: | |
8069 | * | |
8070 | * l2arc_write_max max write bytes per interval | |
b128c09f | 8071 | * l2arc_write_boost extra write bytes during device warmup |
34dc7c2f BB |
8072 | * l2arc_noprefetch skip caching prefetched buffers |
8073 | * l2arc_headroom number of max device writes to precache | |
3a17a7a9 SK |
8074 | * l2arc_headroom_boost when we find compressed buffers during ARC |
8075 | * scanning, we multiply headroom by this | |
8076 | * percentage factor for the next scan cycle, | |
8077 | * since more compressed buffers are likely to | |
8078 | * be present | |
34dc7c2f BB |
8079 | * l2arc_feed_secs seconds between L2ARC writing |
8080 | * | |
8081 | * Tunables may be removed or added as future performance improvements are | |
8082 | * integrated, and also may become zpool properties. | |
d164b209 BB |
8083 | * |
8084 | * There are three key functions that control how the L2ARC warms up: | |
8085 | * | |
8086 | * l2arc_write_eligible() check if a buffer is eligible to cache | |
8087 | * l2arc_write_size() calculate how much to write | |
8088 | * l2arc_write_interval() calculate sleep delay between writes | |
8089 | * | |
8090 | * These three functions determine what to write, how much, and how quickly | |
8091 | * to send writes. | |
34dc7c2f BB |
8092 | */ |
8093 | ||
d164b209 | 8094 | static boolean_t |
2a432414 | 8095 | l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) |
d164b209 BB |
8096 | { |
8097 | /* | |
8098 | * A buffer is *not* eligible for the L2ARC if it: | |
8099 | * 1. belongs to a different spa. | |
428870ff BB |
8100 | * 2. is already cached on the L2ARC. |
8101 | * 3. has an I/O in progress (it may be an incomplete read). | |
8102 | * 4. is flagged not eligible (zfs property). | |
d164b209 | 8103 | */ |
b9541d6b | 8104 | if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) || |
2a432414 | 8105 | HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr)) |
d164b209 BB |
8106 | return (B_FALSE); |
8107 | ||
8108 | return (B_TRUE); | |
8109 | } | |
8110 | ||
8111 | static uint64_t | |
3a17a7a9 | 8112 | l2arc_write_size(void) |
d164b209 BB |
8113 | { |
8114 | uint64_t size; | |
8115 | ||
3a17a7a9 SK |
8116 | /* |
8117 | * Make sure our globals have meaningful values in case the user | |
8118 | * altered them. | |
8119 | */ | |
8120 | size = l2arc_write_max; | |
8121 | if (size == 0) { | |
8122 | cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " | |
8123 | "be greater than zero, resetting it to the default (%d)", | |
8124 | L2ARC_WRITE_SIZE); | |
8125 | size = l2arc_write_max = L2ARC_WRITE_SIZE; | |
8126 | } | |
d164b209 BB |
8127 | |
8128 | if (arc_warm == B_FALSE) | |
3a17a7a9 | 8129 | size += l2arc_write_boost; |
d164b209 BB |
8130 | |
8131 | return (size); | |
8132 | ||
8133 | } | |
8134 | ||
8135 | static clock_t | |
8136 | l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) | |
8137 | { | |
428870ff | 8138 | clock_t interval, next, now; |
d164b209 BB |
8139 | |
8140 | /* | |
8141 | * If the ARC lists are busy, increase our write rate; if the | |
8142 | * lists are stale, idle back. This is achieved by checking | |
8143 | * how much we previously wrote - if it was more than half of | |
8144 | * what we wanted, schedule the next write much sooner. | |
8145 | */ | |
8146 | if (l2arc_feed_again && wrote > (wanted / 2)) | |
8147 | interval = (hz * l2arc_feed_min_ms) / 1000; | |
8148 | else | |
8149 | interval = hz * l2arc_feed_secs; | |
8150 | ||
428870ff BB |
8151 | now = ddi_get_lbolt(); |
8152 | next = MAX(now, MIN(now + interval, began + interval)); | |
d164b209 BB |
8153 | |
8154 | return (next); | |
8155 | } | |
8156 | ||
34dc7c2f BB |
8157 | /* |
8158 | * Cycle through L2ARC devices. This is how L2ARC load balances. | |
b128c09f | 8159 | * If a device is returned, this also returns holding the spa config lock. |
34dc7c2f BB |
8160 | */ |
8161 | static l2arc_dev_t * | |
8162 | l2arc_dev_get_next(void) | |
8163 | { | |
b128c09f | 8164 | l2arc_dev_t *first, *next = NULL; |
34dc7c2f | 8165 | |
b128c09f BB |
8166 | /* |
8167 | * Lock out the removal of spas (spa_namespace_lock), then removal | |
8168 | * of cache devices (l2arc_dev_mtx). Once a device has been selected, | |
8169 | * both locks will be dropped and a spa config lock held instead. | |
8170 | */ | |
8171 | mutex_enter(&spa_namespace_lock); | |
8172 | mutex_enter(&l2arc_dev_mtx); | |
8173 | ||
8174 | /* if there are no vdevs, there is nothing to do */ | |
8175 | if (l2arc_ndev == 0) | |
8176 | goto out; | |
8177 | ||
8178 | first = NULL; | |
8179 | next = l2arc_dev_last; | |
8180 | do { | |
8181 | /* loop around the list looking for a non-faulted vdev */ | |
8182 | if (next == NULL) { | |
34dc7c2f | 8183 | next = list_head(l2arc_dev_list); |
b128c09f BB |
8184 | } else { |
8185 | next = list_next(l2arc_dev_list, next); | |
8186 | if (next == NULL) | |
8187 | next = list_head(l2arc_dev_list); | |
8188 | } | |
8189 | ||
8190 | /* if we have come back to the start, bail out */ | |
8191 | if (first == NULL) | |
8192 | first = next; | |
8193 | else if (next == first) | |
8194 | break; | |
8195 | ||
8196 | } while (vdev_is_dead(next->l2ad_vdev)); | |
8197 | ||
8198 | /* if we were unable to find any usable vdevs, return NULL */ | |
8199 | if (vdev_is_dead(next->l2ad_vdev)) | |
8200 | next = NULL; | |
34dc7c2f BB |
8201 | |
8202 | l2arc_dev_last = next; | |
8203 | ||
b128c09f BB |
8204 | out: |
8205 | mutex_exit(&l2arc_dev_mtx); | |
8206 | ||
8207 | /* | |
8208 | * Grab the config lock to prevent the 'next' device from being | |
8209 | * removed while we are writing to it. | |
8210 | */ | |
8211 | if (next != NULL) | |
8212 | spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); | |
8213 | mutex_exit(&spa_namespace_lock); | |
8214 | ||
34dc7c2f BB |
8215 | return (next); |
8216 | } | |
8217 | ||
b128c09f BB |
8218 | /* |
8219 | * Free buffers that were tagged for destruction. | |
8220 | */ | |
8221 | static void | |
0bc8fd78 | 8222 | l2arc_do_free_on_write(void) |
b128c09f BB |
8223 | { |
8224 | list_t *buflist; | |
8225 | l2arc_data_free_t *df, *df_prev; | |
8226 | ||
8227 | mutex_enter(&l2arc_free_on_write_mtx); | |
8228 | buflist = l2arc_free_on_write; | |
8229 | ||
8230 | for (df = list_tail(buflist); df; df = df_prev) { | |
8231 | df_prev = list_prev(buflist, df); | |
a6255b7f DQ |
8232 | ASSERT3P(df->l2df_abd, !=, NULL); |
8233 | abd_free(df->l2df_abd); | |
b128c09f BB |
8234 | list_remove(buflist, df); |
8235 | kmem_free(df, sizeof (l2arc_data_free_t)); | |
8236 | } | |
8237 | ||
8238 | mutex_exit(&l2arc_free_on_write_mtx); | |
8239 | } | |
8240 | ||
34dc7c2f BB |
8241 | /* |
8242 | * A write to a cache device has completed. Update all headers to allow | |
8243 | * reads from these buffers to begin. | |
8244 | */ | |
8245 | static void | |
8246 | l2arc_write_done(zio_t *zio) | |
8247 | { | |
8248 | l2arc_write_callback_t *cb; | |
8249 | l2arc_dev_t *dev; | |
8250 | list_t *buflist; | |
2a432414 | 8251 | arc_buf_hdr_t *head, *hdr, *hdr_prev; |
34dc7c2f | 8252 | kmutex_t *hash_lock; |
3bec585e | 8253 | int64_t bytes_dropped = 0; |
34dc7c2f BB |
8254 | |
8255 | cb = zio->io_private; | |
d3c2ae1c | 8256 | ASSERT3P(cb, !=, NULL); |
34dc7c2f | 8257 | dev = cb->l2wcb_dev; |
d3c2ae1c | 8258 | ASSERT3P(dev, !=, NULL); |
34dc7c2f | 8259 | head = cb->l2wcb_head; |
d3c2ae1c | 8260 | ASSERT3P(head, !=, NULL); |
b9541d6b | 8261 | buflist = &dev->l2ad_buflist; |
d3c2ae1c | 8262 | ASSERT3P(buflist, !=, NULL); |
34dc7c2f BB |
8263 | DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, |
8264 | l2arc_write_callback_t *, cb); | |
8265 | ||
8266 | if (zio->io_error != 0) | |
8267 | ARCSTAT_BUMP(arcstat_l2_writes_error); | |
8268 | ||
34dc7c2f BB |
8269 | /* |
8270 | * All writes completed, or an error was hit. | |
8271 | */ | |
ca0bf58d PS |
8272 | top: |
8273 | mutex_enter(&dev->l2ad_mtx); | |
2a432414 GW |
8274 | for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { |
8275 | hdr_prev = list_prev(buflist, hdr); | |
34dc7c2f | 8276 | |
2a432414 | 8277 | hash_lock = HDR_LOCK(hdr); |
ca0bf58d PS |
8278 | |
8279 | /* | |
8280 | * We cannot use mutex_enter or else we can deadlock | |
8281 | * with l2arc_write_buffers (due to swapping the order | |
8282 | * the hash lock and l2ad_mtx are taken). | |
8283 | */ | |
34dc7c2f BB |
8284 | if (!mutex_tryenter(hash_lock)) { |
8285 | /* | |
ca0bf58d PS |
8286 | * Missed the hash lock. We must retry so we |
8287 | * don't leave the ARC_FLAG_L2_WRITING bit set. | |
34dc7c2f | 8288 | */ |
ca0bf58d PS |
8289 | ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); |
8290 | ||
8291 | /* | |
8292 | * We don't want to rescan the headers we've | |
8293 | * already marked as having been written out, so | |
8294 | * we reinsert the head node so we can pick up | |
8295 | * where we left off. | |
8296 | */ | |
8297 | list_remove(buflist, head); | |
8298 | list_insert_after(buflist, hdr, head); | |
8299 | ||
8300 | mutex_exit(&dev->l2ad_mtx); | |
8301 | ||
8302 | /* | |
8303 | * We wait for the hash lock to become available | |
8304 | * to try and prevent busy waiting, and increase | |
8305 | * the chance we'll be able to acquire the lock | |
8306 | * the next time around. | |
8307 | */ | |
8308 | mutex_enter(hash_lock); | |
8309 | mutex_exit(hash_lock); | |
8310 | goto top; | |
34dc7c2f BB |
8311 | } |
8312 | ||
b9541d6b | 8313 | /* |
ca0bf58d PS |
8314 | * We could not have been moved into the arc_l2c_only |
8315 | * state while in-flight due to our ARC_FLAG_L2_WRITING | |
8316 | * bit being set. Let's just ensure that's being enforced. | |
8317 | */ | |
8318 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
8319 | ||
8a09d5fd BB |
8320 | /* |
8321 | * Skipped - drop L2ARC entry and mark the header as no | |
8322 | * longer L2 eligibile. | |
8323 | */ | |
d3c2ae1c | 8324 | if (zio->io_error != 0) { |
34dc7c2f | 8325 | /* |
b128c09f | 8326 | * Error - drop L2ARC entry. |
34dc7c2f | 8327 | */ |
2a432414 | 8328 | list_remove(buflist, hdr); |
d3c2ae1c | 8329 | arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); |
b9541d6b | 8330 | |
7558997d SD |
8331 | uint64_t psize = HDR_GET_PSIZE(hdr); |
8332 | ARCSTAT_INCR(arcstat_l2_psize, -psize); | |
01850391 | 8333 | ARCSTAT_INCR(arcstat_l2_lsize, -HDR_GET_LSIZE(hdr)); |
d962d5da | 8334 | |
7558997d SD |
8335 | bytes_dropped += |
8336 | vdev_psize_to_asize(dev->l2ad_vdev, psize); | |
424fd7c3 | 8337 | (void) zfs_refcount_remove_many(&dev->l2ad_alloc, |
d3c2ae1c | 8338 | arc_hdr_size(hdr), hdr); |
34dc7c2f BB |
8339 | } |
8340 | ||
8341 | /* | |
ca0bf58d PS |
8342 | * Allow ARC to begin reads and ghost list evictions to |
8343 | * this L2ARC entry. | |
34dc7c2f | 8344 | */ |
d3c2ae1c | 8345 | arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING); |
34dc7c2f BB |
8346 | |
8347 | mutex_exit(hash_lock); | |
8348 | } | |
8349 | ||
8350 | atomic_inc_64(&l2arc_writes_done); | |
8351 | list_remove(buflist, head); | |
b9541d6b CW |
8352 | ASSERT(!HDR_HAS_L1HDR(head)); |
8353 | kmem_cache_free(hdr_l2only_cache, head); | |
8354 | mutex_exit(&dev->l2ad_mtx); | |
34dc7c2f | 8355 | |
3bec585e SK |
8356 | vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); |
8357 | ||
b128c09f | 8358 | l2arc_do_free_on_write(); |
34dc7c2f BB |
8359 | |
8360 | kmem_free(cb, sizeof (l2arc_write_callback_t)); | |
8361 | } | |
8362 | ||
b5256303 TC |
8363 | static int |
8364 | l2arc_untransform(zio_t *zio, l2arc_read_callback_t *cb) | |
8365 | { | |
8366 | int ret; | |
8367 | spa_t *spa = zio->io_spa; | |
8368 | arc_buf_hdr_t *hdr = cb->l2rcb_hdr; | |
8369 | blkptr_t *bp = zio->io_bp; | |
b5256303 TC |
8370 | uint8_t salt[ZIO_DATA_SALT_LEN]; |
8371 | uint8_t iv[ZIO_DATA_IV_LEN]; | |
8372 | uint8_t mac[ZIO_DATA_MAC_LEN]; | |
8373 | boolean_t no_crypt = B_FALSE; | |
8374 | ||
8375 | /* | |
8376 | * ZIL data is never be written to the L2ARC, so we don't need | |
8377 | * special handling for its unique MAC storage. | |
8378 | */ | |
8379 | ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG); | |
8380 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); | |
440a3eb9 | 8381 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); |
b5256303 | 8382 | |
440a3eb9 TC |
8383 | /* |
8384 | * If the data was encrypted, decrypt it now. Note that | |
8385 | * we must check the bp here and not the hdr, since the | |
8386 | * hdr does not have its encryption parameters updated | |
8387 | * until arc_read_done(). | |
8388 | */ | |
8389 | if (BP_IS_ENCRYPTED(bp)) { | |
be9a5c35 | 8390 | abd_t *eabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr); |
b5256303 TC |
8391 | |
8392 | zio_crypt_decode_params_bp(bp, salt, iv); | |
8393 | zio_crypt_decode_mac_bp(bp, mac); | |
8394 | ||
be9a5c35 TC |
8395 | ret = spa_do_crypt_abd(B_FALSE, spa, &cb->l2rcb_zb, |
8396 | BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), | |
8397 | salt, iv, mac, HDR_GET_PSIZE(hdr), eabd, | |
8398 | hdr->b_l1hdr.b_pabd, &no_crypt); | |
b5256303 TC |
8399 | if (ret != 0) { |
8400 | arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr); | |
b5256303 TC |
8401 | goto error; |
8402 | } | |
8403 | ||
b5256303 TC |
8404 | /* |
8405 | * If we actually performed decryption, replace b_pabd | |
8406 | * with the decrypted data. Otherwise we can just throw | |
8407 | * our decryption buffer away. | |
8408 | */ | |
8409 | if (!no_crypt) { | |
8410 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
8411 | arc_hdr_size(hdr), hdr); | |
8412 | hdr->b_l1hdr.b_pabd = eabd; | |
8413 | zio->io_abd = eabd; | |
8414 | } else { | |
8415 | arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr); | |
8416 | } | |
8417 | } | |
8418 | ||
8419 | /* | |
8420 | * If the L2ARC block was compressed, but ARC compression | |
8421 | * is disabled we decompress the data into a new buffer and | |
8422 | * replace the existing data. | |
8423 | */ | |
8424 | if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
8425 | !HDR_COMPRESSION_ENABLED(hdr)) { | |
8426 | abd_t *cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr); | |
8427 | void *tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr)); | |
8428 | ||
8429 | ret = zio_decompress_data(HDR_GET_COMPRESS(hdr), | |
8430 | hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr), | |
8431 | HDR_GET_LSIZE(hdr)); | |
8432 | if (ret != 0) { | |
8433 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
8434 | arc_free_data_abd(hdr, cabd, arc_hdr_size(hdr), hdr); | |
8435 | goto error; | |
8436 | } | |
8437 | ||
8438 | abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); | |
8439 | arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, | |
8440 | arc_hdr_size(hdr), hdr); | |
8441 | hdr->b_l1hdr.b_pabd = cabd; | |
8442 | zio->io_abd = cabd; | |
8443 | zio->io_size = HDR_GET_LSIZE(hdr); | |
8444 | } | |
8445 | ||
8446 | return (0); | |
8447 | ||
8448 | error: | |
8449 | return (ret); | |
8450 | } | |
8451 | ||
8452 | ||
34dc7c2f BB |
8453 | /* |
8454 | * A read to a cache device completed. Validate buffer contents before | |
8455 | * handing over to the regular ARC routines. | |
8456 | */ | |
8457 | static void | |
8458 | l2arc_read_done(zio_t *zio) | |
8459 | { | |
b5256303 | 8460 | int tfm_error = 0; |
b405837a | 8461 | l2arc_read_callback_t *cb = zio->io_private; |
34dc7c2f | 8462 | arc_buf_hdr_t *hdr; |
34dc7c2f | 8463 | kmutex_t *hash_lock; |
b405837a TC |
8464 | boolean_t valid_cksum; |
8465 | boolean_t using_rdata = (BP_IS_ENCRYPTED(&cb->l2rcb_bp) && | |
8466 | (cb->l2rcb_flags & ZIO_FLAG_RAW_ENCRYPT)); | |
b128c09f | 8467 | |
d3c2ae1c | 8468 | ASSERT3P(zio->io_vd, !=, NULL); |
b128c09f BB |
8469 | ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); |
8470 | ||
8471 | spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); | |
34dc7c2f | 8472 | |
d3c2ae1c GW |
8473 | ASSERT3P(cb, !=, NULL); |
8474 | hdr = cb->l2rcb_hdr; | |
8475 | ASSERT3P(hdr, !=, NULL); | |
34dc7c2f | 8476 | |
d3c2ae1c | 8477 | hash_lock = HDR_LOCK(hdr); |
34dc7c2f | 8478 | mutex_enter(hash_lock); |
428870ff | 8479 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
34dc7c2f | 8480 | |
82710e99 GDN |
8481 | /* |
8482 | * If the data was read into a temporary buffer, | |
8483 | * move it and free the buffer. | |
8484 | */ | |
8485 | if (cb->l2rcb_abd != NULL) { | |
8486 | ASSERT3U(arc_hdr_size(hdr), <, zio->io_size); | |
8487 | if (zio->io_error == 0) { | |
b405837a TC |
8488 | if (using_rdata) { |
8489 | abd_copy(hdr->b_crypt_hdr.b_rabd, | |
8490 | cb->l2rcb_abd, arc_hdr_size(hdr)); | |
8491 | } else { | |
8492 | abd_copy(hdr->b_l1hdr.b_pabd, | |
8493 | cb->l2rcb_abd, arc_hdr_size(hdr)); | |
8494 | } | |
82710e99 GDN |
8495 | } |
8496 | ||
8497 | /* | |
8498 | * The following must be done regardless of whether | |
8499 | * there was an error: | |
8500 | * - free the temporary buffer | |
8501 | * - point zio to the real ARC buffer | |
8502 | * - set zio size accordingly | |
8503 | * These are required because zio is either re-used for | |
8504 | * an I/O of the block in the case of the error | |
8505 | * or the zio is passed to arc_read_done() and it | |
8506 | * needs real data. | |
8507 | */ | |
8508 | abd_free(cb->l2rcb_abd); | |
8509 | zio->io_size = zio->io_orig_size = arc_hdr_size(hdr); | |
440a3eb9 | 8510 | |
b405837a | 8511 | if (using_rdata) { |
440a3eb9 TC |
8512 | ASSERT(HDR_HAS_RABD(hdr)); |
8513 | zio->io_abd = zio->io_orig_abd = | |
8514 | hdr->b_crypt_hdr.b_rabd; | |
8515 | } else { | |
8516 | ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); | |
8517 | zio->io_abd = zio->io_orig_abd = hdr->b_l1hdr.b_pabd; | |
8518 | } | |
82710e99 GDN |
8519 | } |
8520 | ||
a6255b7f | 8521 | ASSERT3P(zio->io_abd, !=, NULL); |
3a17a7a9 | 8522 | |
34dc7c2f BB |
8523 | /* |
8524 | * Check this survived the L2ARC journey. | |
8525 | */ | |
b5256303 TC |
8526 | ASSERT(zio->io_abd == hdr->b_l1hdr.b_pabd || |
8527 | (HDR_HAS_RABD(hdr) && zio->io_abd == hdr->b_crypt_hdr.b_rabd)); | |
d3c2ae1c GW |
8528 | zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ |
8529 | zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ | |
8530 | ||
8531 | valid_cksum = arc_cksum_is_equal(hdr, zio); | |
b5256303 TC |
8532 | |
8533 | /* | |
8534 | * b_rabd will always match the data as it exists on disk if it is | |
8535 | * being used. Therefore if we are reading into b_rabd we do not | |
8536 | * attempt to untransform the data. | |
8537 | */ | |
8538 | if (valid_cksum && !using_rdata) | |
8539 | tfm_error = l2arc_untransform(zio, cb); | |
8540 | ||
8541 | if (valid_cksum && tfm_error == 0 && zio->io_error == 0 && | |
8542 | !HDR_L2_EVICTED(hdr)) { | |
34dc7c2f | 8543 | mutex_exit(hash_lock); |
d3c2ae1c | 8544 | zio->io_private = hdr; |
34dc7c2f BB |
8545 | arc_read_done(zio); |
8546 | } else { | |
8547 | mutex_exit(hash_lock); | |
8548 | /* | |
8549 | * Buffer didn't survive caching. Increment stats and | |
8550 | * reissue to the original storage device. | |
8551 | */ | |
b128c09f | 8552 | if (zio->io_error != 0) { |
34dc7c2f | 8553 | ARCSTAT_BUMP(arcstat_l2_io_error); |
b128c09f | 8554 | } else { |
2e528b49 | 8555 | zio->io_error = SET_ERROR(EIO); |
b128c09f | 8556 | } |
b5256303 | 8557 | if (!valid_cksum || tfm_error != 0) |
34dc7c2f BB |
8558 | ARCSTAT_BUMP(arcstat_l2_cksum_bad); |
8559 | ||
34dc7c2f | 8560 | /* |
b128c09f BB |
8561 | * If there's no waiter, issue an async i/o to the primary |
8562 | * storage now. If there *is* a waiter, the caller must | |
8563 | * issue the i/o in a context where it's OK to block. | |
34dc7c2f | 8564 | */ |
d164b209 BB |
8565 | if (zio->io_waiter == NULL) { |
8566 | zio_t *pio = zio_unique_parent(zio); | |
b5256303 TC |
8567 | void *abd = (using_rdata) ? |
8568 | hdr->b_crypt_hdr.b_rabd : hdr->b_l1hdr.b_pabd; | |
d164b209 BB |
8569 | |
8570 | ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); | |
8571 | ||
d3c2ae1c | 8572 | zio_nowait(zio_read(pio, zio->io_spa, zio->io_bp, |
b5256303 | 8573 | abd, zio->io_size, arc_read_done, |
d3c2ae1c GW |
8574 | hdr, zio->io_priority, cb->l2rcb_flags, |
8575 | &cb->l2rcb_zb)); | |
d164b209 | 8576 | } |
34dc7c2f BB |
8577 | } |
8578 | ||
8579 | kmem_free(cb, sizeof (l2arc_read_callback_t)); | |
8580 | } | |
8581 | ||
8582 | /* | |
8583 | * This is the list priority from which the L2ARC will search for pages to | |
8584 | * cache. This is used within loops (0..3) to cycle through lists in the | |
8585 | * desired order. This order can have a significant effect on cache | |
8586 | * performance. | |
8587 | * | |
8588 | * Currently the metadata lists are hit first, MFU then MRU, followed by | |
8589 | * the data lists. This function returns a locked list, and also returns | |
8590 | * the lock pointer. | |
8591 | */ | |
ca0bf58d PS |
8592 | static multilist_sublist_t * |
8593 | l2arc_sublist_lock(int list_num) | |
34dc7c2f | 8594 | { |
ca0bf58d PS |
8595 | multilist_t *ml = NULL; |
8596 | unsigned int idx; | |
34dc7c2f | 8597 | |
4aafab91 | 8598 | ASSERT(list_num >= 0 && list_num < L2ARC_FEED_TYPES); |
34dc7c2f BB |
8599 | |
8600 | switch (list_num) { | |
8601 | case 0: | |
64fc7762 | 8602 | ml = arc_mfu->arcs_list[ARC_BUFC_METADATA]; |
34dc7c2f BB |
8603 | break; |
8604 | case 1: | |
64fc7762 | 8605 | ml = arc_mru->arcs_list[ARC_BUFC_METADATA]; |
34dc7c2f BB |
8606 | break; |
8607 | case 2: | |
64fc7762 | 8608 | ml = arc_mfu->arcs_list[ARC_BUFC_DATA]; |
34dc7c2f BB |
8609 | break; |
8610 | case 3: | |
64fc7762 | 8611 | ml = arc_mru->arcs_list[ARC_BUFC_DATA]; |
34dc7c2f | 8612 | break; |
4aafab91 G |
8613 | default: |
8614 | return (NULL); | |
34dc7c2f BB |
8615 | } |
8616 | ||
ca0bf58d PS |
8617 | /* |
8618 | * Return a randomly-selected sublist. This is acceptable | |
8619 | * because the caller feeds only a little bit of data for each | |
8620 | * call (8MB). Subsequent calls will result in different | |
8621 | * sublists being selected. | |
8622 | */ | |
8623 | idx = multilist_get_random_index(ml); | |
8624 | return (multilist_sublist_lock(ml, idx)); | |
34dc7c2f BB |
8625 | } |
8626 | ||
8627 | /* | |
8628 | * Evict buffers from the device write hand to the distance specified in | |
8629 | * bytes. This distance may span populated buffers, it may span nothing. | |
8630 | * This is clearing a region on the L2ARC device ready for writing. | |
8631 | * If the 'all' boolean is set, every buffer is evicted. | |
8632 | */ | |
8633 | static void | |
8634 | l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) | |
8635 | { | |
8636 | list_t *buflist; | |
2a432414 | 8637 | arc_buf_hdr_t *hdr, *hdr_prev; |
34dc7c2f BB |
8638 | kmutex_t *hash_lock; |
8639 | uint64_t taddr; | |
8640 | ||
b9541d6b | 8641 | buflist = &dev->l2ad_buflist; |
34dc7c2f BB |
8642 | |
8643 | if (!all && dev->l2ad_first) { | |
8644 | /* | |
8645 | * This is the first sweep through the device. There is | |
8646 | * nothing to evict. | |
8647 | */ | |
8648 | return; | |
8649 | } | |
8650 | ||
b128c09f | 8651 | if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { |
34dc7c2f BB |
8652 | /* |
8653 | * When nearing the end of the device, evict to the end | |
8654 | * before the device write hand jumps to the start. | |
8655 | */ | |
8656 | taddr = dev->l2ad_end; | |
8657 | } else { | |
8658 | taddr = dev->l2ad_hand + distance; | |
8659 | } | |
8660 | DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, | |
8661 | uint64_t, taddr, boolean_t, all); | |
8662 | ||
8663 | top: | |
b9541d6b | 8664 | mutex_enter(&dev->l2ad_mtx); |
2a432414 GW |
8665 | for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { |
8666 | hdr_prev = list_prev(buflist, hdr); | |
34dc7c2f | 8667 | |
ca6c7a94 | 8668 | ASSERT(!HDR_EMPTY(hdr)); |
2a432414 | 8669 | hash_lock = HDR_LOCK(hdr); |
ca0bf58d PS |
8670 | |
8671 | /* | |
8672 | * We cannot use mutex_enter or else we can deadlock | |
8673 | * with l2arc_write_buffers (due to swapping the order | |
8674 | * the hash lock and l2ad_mtx are taken). | |
8675 | */ | |
34dc7c2f BB |
8676 | if (!mutex_tryenter(hash_lock)) { |
8677 | /* | |
8678 | * Missed the hash lock. Retry. | |
8679 | */ | |
8680 | ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); | |
b9541d6b | 8681 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
8682 | mutex_enter(hash_lock); |
8683 | mutex_exit(hash_lock); | |
8684 | goto top; | |
8685 | } | |
8686 | ||
f06f53fa AG |
8687 | /* |
8688 | * A header can't be on this list if it doesn't have L2 header. | |
8689 | */ | |
8690 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
34dc7c2f | 8691 | |
f06f53fa AG |
8692 | /* Ensure this header has finished being written. */ |
8693 | ASSERT(!HDR_L2_WRITING(hdr)); | |
8694 | ASSERT(!HDR_L2_WRITE_HEAD(hdr)); | |
8695 | ||
8696 | if (!all && (hdr->b_l2hdr.b_daddr >= taddr || | |
b9541d6b | 8697 | hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { |
34dc7c2f BB |
8698 | /* |
8699 | * We've evicted to the target address, | |
8700 | * or the end of the device. | |
8701 | */ | |
8702 | mutex_exit(hash_lock); | |
8703 | break; | |
8704 | } | |
8705 | ||
b9541d6b | 8706 | if (!HDR_HAS_L1HDR(hdr)) { |
2a432414 | 8707 | ASSERT(!HDR_L2_READING(hdr)); |
34dc7c2f BB |
8708 | /* |
8709 | * This doesn't exist in the ARC. Destroy. | |
8710 | * arc_hdr_destroy() will call list_remove() | |
01850391 | 8711 | * and decrement arcstat_l2_lsize. |
34dc7c2f | 8712 | */ |
2a432414 GW |
8713 | arc_change_state(arc_anon, hdr, hash_lock); |
8714 | arc_hdr_destroy(hdr); | |
34dc7c2f | 8715 | } else { |
b9541d6b CW |
8716 | ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); |
8717 | ARCSTAT_BUMP(arcstat_l2_evict_l1cached); | |
b128c09f BB |
8718 | /* |
8719 | * Invalidate issued or about to be issued | |
8720 | * reads, since we may be about to write | |
8721 | * over this location. | |
8722 | */ | |
2a432414 | 8723 | if (HDR_L2_READING(hdr)) { |
b128c09f | 8724 | ARCSTAT_BUMP(arcstat_l2_evict_reading); |
d3c2ae1c | 8725 | arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED); |
b128c09f BB |
8726 | } |
8727 | ||
d962d5da | 8728 | arc_hdr_l2hdr_destroy(hdr); |
34dc7c2f BB |
8729 | } |
8730 | mutex_exit(hash_lock); | |
8731 | } | |
b9541d6b | 8732 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
8733 | } |
8734 | ||
b5256303 TC |
8735 | /* |
8736 | * Handle any abd transforms that might be required for writing to the L2ARC. | |
8737 | * If successful, this function will always return an abd with the data | |
8738 | * transformed as it is on disk in a new abd of asize bytes. | |
8739 | */ | |
8740 | static int | |
8741 | l2arc_apply_transforms(spa_t *spa, arc_buf_hdr_t *hdr, uint64_t asize, | |
8742 | abd_t **abd_out) | |
8743 | { | |
8744 | int ret; | |
8745 | void *tmp = NULL; | |
8746 | abd_t *cabd = NULL, *eabd = NULL, *to_write = hdr->b_l1hdr.b_pabd; | |
8747 | enum zio_compress compress = HDR_GET_COMPRESS(hdr); | |
8748 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
8749 | uint64_t size = arc_hdr_size(hdr); | |
8750 | boolean_t ismd = HDR_ISTYPE_METADATA(hdr); | |
8751 | boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); | |
8752 | dsl_crypto_key_t *dck = NULL; | |
8753 | uint8_t mac[ZIO_DATA_MAC_LEN] = { 0 }; | |
4807c0ba | 8754 | boolean_t no_crypt = B_FALSE; |
b5256303 TC |
8755 | |
8756 | ASSERT((HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && | |
8757 | !HDR_COMPRESSION_ENABLED(hdr)) || | |
8758 | HDR_ENCRYPTED(hdr) || HDR_SHARED_DATA(hdr) || psize != asize); | |
8759 | ASSERT3U(psize, <=, asize); | |
8760 | ||
8761 | /* | |
8762 | * If this data simply needs its own buffer, we simply allocate it | |
8763 | * and copy the data. This may be done to elimiate a depedency on a | |
8764 | * shared buffer or to reallocate the buffer to match asize. | |
8765 | */ | |
4807c0ba | 8766 | if (HDR_HAS_RABD(hdr) && asize != psize) { |
10adee27 | 8767 | ASSERT3U(asize, >=, psize); |
4807c0ba | 8768 | to_write = abd_alloc_for_io(asize, ismd); |
10adee27 TC |
8769 | abd_copy(to_write, hdr->b_crypt_hdr.b_rabd, psize); |
8770 | if (psize != asize) | |
8771 | abd_zero_off(to_write, psize, asize - psize); | |
4807c0ba TC |
8772 | goto out; |
8773 | } | |
8774 | ||
b5256303 TC |
8775 | if ((compress == ZIO_COMPRESS_OFF || HDR_COMPRESSION_ENABLED(hdr)) && |
8776 | !HDR_ENCRYPTED(hdr)) { | |
8777 | ASSERT3U(size, ==, psize); | |
8778 | to_write = abd_alloc_for_io(asize, ismd); | |
8779 | abd_copy(to_write, hdr->b_l1hdr.b_pabd, size); | |
8780 | if (size != asize) | |
8781 | abd_zero_off(to_write, size, asize - size); | |
8782 | goto out; | |
8783 | } | |
8784 | ||
8785 | if (compress != ZIO_COMPRESS_OFF && !HDR_COMPRESSION_ENABLED(hdr)) { | |
8786 | cabd = abd_alloc_for_io(asize, ismd); | |
8787 | tmp = abd_borrow_buf(cabd, asize); | |
8788 | ||
8789 | psize = zio_compress_data(compress, to_write, tmp, size); | |
8790 | ASSERT3U(psize, <=, HDR_GET_PSIZE(hdr)); | |
8791 | if (psize < asize) | |
8792 | bzero((char *)tmp + psize, asize - psize); | |
8793 | psize = HDR_GET_PSIZE(hdr); | |
8794 | abd_return_buf_copy(cabd, tmp, asize); | |
8795 | to_write = cabd; | |
8796 | } | |
8797 | ||
8798 | if (HDR_ENCRYPTED(hdr)) { | |
8799 | eabd = abd_alloc_for_io(asize, ismd); | |
8800 | ||
8801 | /* | |
8802 | * If the dataset was disowned before the buffer | |
8803 | * made it to this point, the key to re-encrypt | |
8804 | * it won't be available. In this case we simply | |
8805 | * won't write the buffer to the L2ARC. | |
8806 | */ | |
8807 | ret = spa_keystore_lookup_key(spa, hdr->b_crypt_hdr.b_dsobj, | |
8808 | FTAG, &dck); | |
8809 | if (ret != 0) | |
8810 | goto error; | |
8811 | ||
8812 | ret = zio_do_crypt_abd(B_TRUE, &dck->dck_key, | |
be9a5c35 TC |
8813 | hdr->b_crypt_hdr.b_ot, bswap, hdr->b_crypt_hdr.b_salt, |
8814 | hdr->b_crypt_hdr.b_iv, mac, psize, to_write, eabd, | |
8815 | &no_crypt); | |
b5256303 TC |
8816 | if (ret != 0) |
8817 | goto error; | |
8818 | ||
4807c0ba TC |
8819 | if (no_crypt) |
8820 | abd_copy(eabd, to_write, psize); | |
b5256303 TC |
8821 | |
8822 | if (psize != asize) | |
8823 | abd_zero_off(eabd, psize, asize - psize); | |
8824 | ||
8825 | /* assert that the MAC we got here matches the one we saved */ | |
8826 | ASSERT0(bcmp(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN)); | |
8827 | spa_keystore_dsl_key_rele(spa, dck, FTAG); | |
8828 | ||
8829 | if (to_write == cabd) | |
8830 | abd_free(cabd); | |
8831 | ||
8832 | to_write = eabd; | |
8833 | } | |
8834 | ||
8835 | out: | |
8836 | ASSERT3P(to_write, !=, hdr->b_l1hdr.b_pabd); | |
8837 | *abd_out = to_write; | |
8838 | return (0); | |
8839 | ||
8840 | error: | |
8841 | if (dck != NULL) | |
8842 | spa_keystore_dsl_key_rele(spa, dck, FTAG); | |
8843 | if (cabd != NULL) | |
8844 | abd_free(cabd); | |
8845 | if (eabd != NULL) | |
8846 | abd_free(eabd); | |
8847 | ||
8848 | *abd_out = NULL; | |
8849 | return (ret); | |
8850 | } | |
8851 | ||
34dc7c2f BB |
8852 | /* |
8853 | * Find and write ARC buffers to the L2ARC device. | |
8854 | * | |
2a432414 | 8855 | * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid |
34dc7c2f | 8856 | * for reading until they have completed writing. |
3a17a7a9 SK |
8857 | * The headroom_boost is an in-out parameter used to maintain headroom boost |
8858 | * state between calls to this function. | |
8859 | * | |
8860 | * Returns the number of bytes actually written (which may be smaller than | |
8861 | * the delta by which the device hand has changed due to alignment). | |
34dc7c2f | 8862 | */ |
d164b209 | 8863 | static uint64_t |
d3c2ae1c | 8864 | l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) |
34dc7c2f | 8865 | { |
2a432414 | 8866 | arc_buf_hdr_t *hdr, *hdr_prev, *head; |
01850391 | 8867 | uint64_t write_asize, write_psize, write_lsize, headroom; |
3a17a7a9 | 8868 | boolean_t full; |
34dc7c2f BB |
8869 | l2arc_write_callback_t *cb; |
8870 | zio_t *pio, *wzio; | |
3541dc6d | 8871 | uint64_t guid = spa_load_guid(spa); |
34dc7c2f | 8872 | |
d3c2ae1c | 8873 | ASSERT3P(dev->l2ad_vdev, !=, NULL); |
3a17a7a9 | 8874 | |
34dc7c2f | 8875 | pio = NULL; |
01850391 | 8876 | write_lsize = write_asize = write_psize = 0; |
34dc7c2f | 8877 | full = B_FALSE; |
b9541d6b | 8878 | head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); |
d3c2ae1c | 8879 | arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR); |
3a17a7a9 | 8880 | |
34dc7c2f BB |
8881 | /* |
8882 | * Copy buffers for L2ARC writing. | |
8883 | */ | |
1c27024e | 8884 | for (int try = 0; try < L2ARC_FEED_TYPES; try++) { |
ca0bf58d | 8885 | multilist_sublist_t *mls = l2arc_sublist_lock(try); |
3a17a7a9 SK |
8886 | uint64_t passed_sz = 0; |
8887 | ||
4aafab91 G |
8888 | VERIFY3P(mls, !=, NULL); |
8889 | ||
b128c09f BB |
8890 | /* |
8891 | * L2ARC fast warmup. | |
8892 | * | |
8893 | * Until the ARC is warm and starts to evict, read from the | |
8894 | * head of the ARC lists rather than the tail. | |
8895 | */ | |
b128c09f | 8896 | if (arc_warm == B_FALSE) |
ca0bf58d | 8897 | hdr = multilist_sublist_head(mls); |
b128c09f | 8898 | else |
ca0bf58d | 8899 | hdr = multilist_sublist_tail(mls); |
b128c09f | 8900 | |
3a17a7a9 | 8901 | headroom = target_sz * l2arc_headroom; |
d3c2ae1c | 8902 | if (zfs_compressed_arc_enabled) |
3a17a7a9 SK |
8903 | headroom = (headroom * l2arc_headroom_boost) / 100; |
8904 | ||
2a432414 | 8905 | for (; hdr; hdr = hdr_prev) { |
3a17a7a9 | 8906 | kmutex_t *hash_lock; |
b5256303 | 8907 | abd_t *to_write = NULL; |
3a17a7a9 | 8908 | |
b128c09f | 8909 | if (arc_warm == B_FALSE) |
ca0bf58d | 8910 | hdr_prev = multilist_sublist_next(mls, hdr); |
b128c09f | 8911 | else |
ca0bf58d | 8912 | hdr_prev = multilist_sublist_prev(mls, hdr); |
34dc7c2f | 8913 | |
2a432414 | 8914 | hash_lock = HDR_LOCK(hdr); |
3a17a7a9 | 8915 | if (!mutex_tryenter(hash_lock)) { |
34dc7c2f BB |
8916 | /* |
8917 | * Skip this buffer rather than waiting. | |
8918 | */ | |
8919 | continue; | |
8920 | } | |
8921 | ||
d3c2ae1c | 8922 | passed_sz += HDR_GET_LSIZE(hdr); |
34dc7c2f BB |
8923 | if (passed_sz > headroom) { |
8924 | /* | |
8925 | * Searched too far. | |
8926 | */ | |
8927 | mutex_exit(hash_lock); | |
8928 | break; | |
8929 | } | |
8930 | ||
2a432414 | 8931 | if (!l2arc_write_eligible(guid, hdr)) { |
34dc7c2f BB |
8932 | mutex_exit(hash_lock); |
8933 | continue; | |
8934 | } | |
8935 | ||
01850391 AG |
8936 | /* |
8937 | * We rely on the L1 portion of the header below, so | |
8938 | * it's invalid for this header to have been evicted out | |
8939 | * of the ghost cache, prior to being written out. The | |
8940 | * ARC_FLAG_L2_WRITING bit ensures this won't happen. | |
8941 | */ | |
8942 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
8943 | ||
8944 | ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); | |
01850391 | 8945 | ASSERT3U(arc_hdr_size(hdr), >, 0); |
b5256303 TC |
8946 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || |
8947 | HDR_HAS_RABD(hdr)); | |
8948 | uint64_t psize = HDR_GET_PSIZE(hdr); | |
01850391 AG |
8949 | uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, |
8950 | psize); | |
8951 | ||
8952 | if ((write_asize + asize) > target_sz) { | |
34dc7c2f BB |
8953 | full = B_TRUE; |
8954 | mutex_exit(hash_lock); | |
8955 | break; | |
8956 | } | |
8957 | ||
b5256303 TC |
8958 | /* |
8959 | * We rely on the L1 portion of the header below, so | |
8960 | * it's invalid for this header to have been evicted out | |
8961 | * of the ghost cache, prior to being written out. The | |
8962 | * ARC_FLAG_L2_WRITING bit ensures this won't happen. | |
8963 | */ | |
8964 | arc_hdr_set_flags(hdr, ARC_FLAG_L2_WRITING); | |
8965 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
8966 | ||
8967 | ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); | |
8968 | ASSERT(hdr->b_l1hdr.b_pabd != NULL || | |
8969 | HDR_HAS_RABD(hdr)); | |
8970 | ASSERT3U(arc_hdr_size(hdr), >, 0); | |
8971 | ||
8972 | /* | |
8973 | * If this header has b_rabd, we can use this since it | |
8974 | * must always match the data exactly as it exists on | |
8975 | * disk. Otherwise, the L2ARC can normally use the | |
8976 | * hdr's data, but if we're sharing data between the | |
8977 | * hdr and one of its bufs, L2ARC needs its own copy of | |
8978 | * the data so that the ZIO below can't race with the | |
8979 | * buf consumer. To ensure that this copy will be | |
8980 | * available for the lifetime of the ZIO and be cleaned | |
8981 | * up afterwards, we add it to the l2arc_free_on_write | |
8982 | * queue. If we need to apply any transforms to the | |
8983 | * data (compression, encryption) we will also need the | |
8984 | * extra buffer. | |
8985 | */ | |
8986 | if (HDR_HAS_RABD(hdr) && psize == asize) { | |
8987 | to_write = hdr->b_crypt_hdr.b_rabd; | |
8988 | } else if ((HDR_COMPRESSION_ENABLED(hdr) || | |
8989 | HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) && | |
8990 | !HDR_ENCRYPTED(hdr) && !HDR_SHARED_DATA(hdr) && | |
8991 | psize == asize) { | |
8992 | to_write = hdr->b_l1hdr.b_pabd; | |
8993 | } else { | |
8994 | int ret; | |
8995 | arc_buf_contents_t type = arc_buf_type(hdr); | |
8996 | ||
8997 | ret = l2arc_apply_transforms(spa, hdr, asize, | |
8998 | &to_write); | |
8999 | if (ret != 0) { | |
9000 | arc_hdr_clear_flags(hdr, | |
9001 | ARC_FLAG_L2_WRITING); | |
9002 | mutex_exit(hash_lock); | |
9003 | continue; | |
9004 | } | |
9005 | ||
9006 | l2arc_free_abd_on_write(to_write, asize, type); | |
9007 | } | |
9008 | ||
34dc7c2f BB |
9009 | if (pio == NULL) { |
9010 | /* | |
9011 | * Insert a dummy header on the buflist so | |
9012 | * l2arc_write_done() can find where the | |
9013 | * write buffers begin without searching. | |
9014 | */ | |
ca0bf58d | 9015 | mutex_enter(&dev->l2ad_mtx); |
b9541d6b | 9016 | list_insert_head(&dev->l2ad_buflist, head); |
ca0bf58d | 9017 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 9018 | |
96c080cb BB |
9019 | cb = kmem_alloc( |
9020 | sizeof (l2arc_write_callback_t), KM_SLEEP); | |
34dc7c2f BB |
9021 | cb->l2wcb_dev = dev; |
9022 | cb->l2wcb_head = head; | |
9023 | pio = zio_root(spa, l2arc_write_done, cb, | |
9024 | ZIO_FLAG_CANFAIL); | |
9025 | } | |
9026 | ||
b9541d6b | 9027 | hdr->b_l2hdr.b_dev = dev; |
b9541d6b | 9028 | hdr->b_l2hdr.b_hits = 0; |
3a17a7a9 | 9029 | |
d3c2ae1c | 9030 | hdr->b_l2hdr.b_daddr = dev->l2ad_hand; |
b5256303 | 9031 | arc_hdr_set_flags(hdr, ARC_FLAG_HAS_L2HDR); |
3a17a7a9 | 9032 | |
ca0bf58d | 9033 | mutex_enter(&dev->l2ad_mtx); |
b9541d6b | 9034 | list_insert_head(&dev->l2ad_buflist, hdr); |
ca0bf58d | 9035 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 9036 | |
424fd7c3 | 9037 | (void) zfs_refcount_add_many(&dev->l2ad_alloc, |
b5256303 | 9038 | arc_hdr_size(hdr), hdr); |
3a17a7a9 | 9039 | |
34dc7c2f | 9040 | wzio = zio_write_phys(pio, dev->l2ad_vdev, |
82710e99 | 9041 | hdr->b_l2hdr.b_daddr, asize, to_write, |
d3c2ae1c GW |
9042 | ZIO_CHECKSUM_OFF, NULL, hdr, |
9043 | ZIO_PRIORITY_ASYNC_WRITE, | |
34dc7c2f BB |
9044 | ZIO_FLAG_CANFAIL, B_FALSE); |
9045 | ||
01850391 | 9046 | write_lsize += HDR_GET_LSIZE(hdr); |
34dc7c2f BB |
9047 | DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, |
9048 | zio_t *, wzio); | |
d962d5da | 9049 | |
01850391 AG |
9050 | write_psize += psize; |
9051 | write_asize += asize; | |
d3c2ae1c | 9052 | dev->l2ad_hand += asize; |
7558997d | 9053 | vdev_space_update(dev->l2ad_vdev, asize, 0, 0); |
d3c2ae1c GW |
9054 | |
9055 | mutex_exit(hash_lock); | |
9056 | ||
9057 | (void) zio_nowait(wzio); | |
34dc7c2f | 9058 | } |
d3c2ae1c GW |
9059 | |
9060 | multilist_sublist_unlock(mls); | |
9061 | ||
9062 | if (full == B_TRUE) | |
9063 | break; | |
34dc7c2f | 9064 | } |
34dc7c2f | 9065 | |
d3c2ae1c GW |
9066 | /* No buffers selected for writing? */ |
9067 | if (pio == NULL) { | |
01850391 | 9068 | ASSERT0(write_lsize); |
d3c2ae1c GW |
9069 | ASSERT(!HDR_HAS_L1HDR(head)); |
9070 | kmem_cache_free(hdr_l2only_cache, head); | |
9071 | return (0); | |
9072 | } | |
34dc7c2f | 9073 | |
3a17a7a9 | 9074 | ASSERT3U(write_asize, <=, target_sz); |
34dc7c2f | 9075 | ARCSTAT_BUMP(arcstat_l2_writes_sent); |
01850391 AG |
9076 | ARCSTAT_INCR(arcstat_l2_write_bytes, write_psize); |
9077 | ARCSTAT_INCR(arcstat_l2_lsize, write_lsize); | |
9078 | ARCSTAT_INCR(arcstat_l2_psize, write_psize); | |
34dc7c2f BB |
9079 | |
9080 | /* | |
9081 | * Bump device hand to the device start if it is approaching the end. | |
9082 | * l2arc_evict() will already have evicted ahead for this case. | |
9083 | */ | |
b128c09f | 9084 | if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { |
34dc7c2f | 9085 | dev->l2ad_hand = dev->l2ad_start; |
34dc7c2f BB |
9086 | dev->l2ad_first = B_FALSE; |
9087 | } | |
9088 | ||
d164b209 | 9089 | dev->l2ad_writing = B_TRUE; |
34dc7c2f | 9090 | (void) zio_wait(pio); |
d164b209 BB |
9091 | dev->l2ad_writing = B_FALSE; |
9092 | ||
3a17a7a9 SK |
9093 | return (write_asize); |
9094 | } | |
9095 | ||
34dc7c2f BB |
9096 | /* |
9097 | * This thread feeds the L2ARC at regular intervals. This is the beating | |
9098 | * heart of the L2ARC. | |
9099 | */ | |
867959b5 | 9100 | /* ARGSUSED */ |
34dc7c2f | 9101 | static void |
c25b8f99 | 9102 | l2arc_feed_thread(void *unused) |
34dc7c2f BB |
9103 | { |
9104 | callb_cpr_t cpr; | |
9105 | l2arc_dev_t *dev; | |
9106 | spa_t *spa; | |
d164b209 | 9107 | uint64_t size, wrote; |
428870ff | 9108 | clock_t begin, next = ddi_get_lbolt(); |
40d06e3c | 9109 | fstrans_cookie_t cookie; |
34dc7c2f BB |
9110 | |
9111 | CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); | |
9112 | ||
9113 | mutex_enter(&l2arc_feed_thr_lock); | |
9114 | ||
40d06e3c | 9115 | cookie = spl_fstrans_mark(); |
34dc7c2f | 9116 | while (l2arc_thread_exit == 0) { |
34dc7c2f | 9117 | CALLB_CPR_SAFE_BEGIN(&cpr); |
b64ccd6c | 9118 | (void) cv_timedwait_sig(&l2arc_feed_thr_cv, |
5b63b3eb | 9119 | &l2arc_feed_thr_lock, next); |
34dc7c2f | 9120 | CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); |
428870ff | 9121 | next = ddi_get_lbolt() + hz; |
34dc7c2f BB |
9122 | |
9123 | /* | |
b128c09f | 9124 | * Quick check for L2ARC devices. |
34dc7c2f BB |
9125 | */ |
9126 | mutex_enter(&l2arc_dev_mtx); | |
9127 | if (l2arc_ndev == 0) { | |
9128 | mutex_exit(&l2arc_dev_mtx); | |
9129 | continue; | |
9130 | } | |
b128c09f | 9131 | mutex_exit(&l2arc_dev_mtx); |
428870ff | 9132 | begin = ddi_get_lbolt(); |
34dc7c2f BB |
9133 | |
9134 | /* | |
b128c09f BB |
9135 | * This selects the next l2arc device to write to, and in |
9136 | * doing so the next spa to feed from: dev->l2ad_spa. This | |
9137 | * will return NULL if there are now no l2arc devices or if | |
9138 | * they are all faulted. | |
9139 | * | |
9140 | * If a device is returned, its spa's config lock is also | |
9141 | * held to prevent device removal. l2arc_dev_get_next() | |
9142 | * will grab and release l2arc_dev_mtx. | |
34dc7c2f | 9143 | */ |
b128c09f | 9144 | if ((dev = l2arc_dev_get_next()) == NULL) |
34dc7c2f | 9145 | continue; |
b128c09f BB |
9146 | |
9147 | spa = dev->l2ad_spa; | |
d3c2ae1c | 9148 | ASSERT3P(spa, !=, NULL); |
34dc7c2f | 9149 | |
572e2857 BB |
9150 | /* |
9151 | * If the pool is read-only then force the feed thread to | |
9152 | * sleep a little longer. | |
9153 | */ | |
9154 | if (!spa_writeable(spa)) { | |
9155 | next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; | |
9156 | spa_config_exit(spa, SCL_L2ARC, dev); | |
9157 | continue; | |
9158 | } | |
9159 | ||
34dc7c2f | 9160 | /* |
b128c09f | 9161 | * Avoid contributing to memory pressure. |
34dc7c2f | 9162 | */ |
ca67b33a | 9163 | if (arc_reclaim_needed()) { |
b128c09f BB |
9164 | ARCSTAT_BUMP(arcstat_l2_abort_lowmem); |
9165 | spa_config_exit(spa, SCL_L2ARC, dev); | |
34dc7c2f BB |
9166 | continue; |
9167 | } | |
b128c09f | 9168 | |
34dc7c2f BB |
9169 | ARCSTAT_BUMP(arcstat_l2_feeds); |
9170 | ||
3a17a7a9 | 9171 | size = l2arc_write_size(); |
b128c09f | 9172 | |
34dc7c2f BB |
9173 | /* |
9174 | * Evict L2ARC buffers that will be overwritten. | |
9175 | */ | |
b128c09f | 9176 | l2arc_evict(dev, size, B_FALSE); |
34dc7c2f BB |
9177 | |
9178 | /* | |
9179 | * Write ARC buffers. | |
9180 | */ | |
d3c2ae1c | 9181 | wrote = l2arc_write_buffers(spa, dev, size); |
d164b209 BB |
9182 | |
9183 | /* | |
9184 | * Calculate interval between writes. | |
9185 | */ | |
9186 | next = l2arc_write_interval(begin, size, wrote); | |
b128c09f | 9187 | spa_config_exit(spa, SCL_L2ARC, dev); |
34dc7c2f | 9188 | } |
40d06e3c | 9189 | spl_fstrans_unmark(cookie); |
34dc7c2f BB |
9190 | |
9191 | l2arc_thread_exit = 0; | |
9192 | cv_broadcast(&l2arc_feed_thr_cv); | |
9193 | CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ | |
9194 | thread_exit(); | |
9195 | } | |
9196 | ||
b128c09f BB |
9197 | boolean_t |
9198 | l2arc_vdev_present(vdev_t *vd) | |
9199 | { | |
9200 | l2arc_dev_t *dev; | |
9201 | ||
9202 | mutex_enter(&l2arc_dev_mtx); | |
9203 | for (dev = list_head(l2arc_dev_list); dev != NULL; | |
9204 | dev = list_next(l2arc_dev_list, dev)) { | |
9205 | if (dev->l2ad_vdev == vd) | |
9206 | break; | |
9207 | } | |
9208 | mutex_exit(&l2arc_dev_mtx); | |
9209 | ||
9210 | return (dev != NULL); | |
9211 | } | |
9212 | ||
34dc7c2f BB |
9213 | /* |
9214 | * Add a vdev for use by the L2ARC. By this point the spa has already | |
9215 | * validated the vdev and opened it. | |
9216 | */ | |
9217 | void | |
9babb374 | 9218 | l2arc_add_vdev(spa_t *spa, vdev_t *vd) |
34dc7c2f BB |
9219 | { |
9220 | l2arc_dev_t *adddev; | |
9221 | ||
b128c09f BB |
9222 | ASSERT(!l2arc_vdev_present(vd)); |
9223 | ||
34dc7c2f BB |
9224 | /* |
9225 | * Create a new l2arc device entry. | |
9226 | */ | |
9227 | adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); | |
9228 | adddev->l2ad_spa = spa; | |
9229 | adddev->l2ad_vdev = vd; | |
9babb374 BB |
9230 | adddev->l2ad_start = VDEV_LABEL_START_SIZE; |
9231 | adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); | |
34dc7c2f | 9232 | adddev->l2ad_hand = adddev->l2ad_start; |
34dc7c2f | 9233 | adddev->l2ad_first = B_TRUE; |
d164b209 | 9234 | adddev->l2ad_writing = B_FALSE; |
98f72a53 | 9235 | list_link_init(&adddev->l2ad_node); |
34dc7c2f | 9236 | |
b9541d6b | 9237 | mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
9238 | /* |
9239 | * This is a list of all ARC buffers that are still valid on the | |
9240 | * device. | |
9241 | */ | |
b9541d6b CW |
9242 | list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), |
9243 | offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); | |
34dc7c2f | 9244 | |
428870ff | 9245 | vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); |
424fd7c3 | 9246 | zfs_refcount_create(&adddev->l2ad_alloc); |
34dc7c2f BB |
9247 | |
9248 | /* | |
9249 | * Add device to global list | |
9250 | */ | |
9251 | mutex_enter(&l2arc_dev_mtx); | |
9252 | list_insert_head(l2arc_dev_list, adddev); | |
9253 | atomic_inc_64(&l2arc_ndev); | |
9254 | mutex_exit(&l2arc_dev_mtx); | |
9255 | } | |
9256 | ||
9257 | /* | |
9258 | * Remove a vdev from the L2ARC. | |
9259 | */ | |
9260 | void | |
9261 | l2arc_remove_vdev(vdev_t *vd) | |
9262 | { | |
9263 | l2arc_dev_t *dev, *nextdev, *remdev = NULL; | |
9264 | ||
34dc7c2f BB |
9265 | /* |
9266 | * Find the device by vdev | |
9267 | */ | |
9268 | mutex_enter(&l2arc_dev_mtx); | |
9269 | for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { | |
9270 | nextdev = list_next(l2arc_dev_list, dev); | |
9271 | if (vd == dev->l2ad_vdev) { | |
9272 | remdev = dev; | |
9273 | break; | |
9274 | } | |
9275 | } | |
d3c2ae1c | 9276 | ASSERT3P(remdev, !=, NULL); |
34dc7c2f BB |
9277 | |
9278 | /* | |
9279 | * Remove device from global list | |
9280 | */ | |
9281 | list_remove(l2arc_dev_list, remdev); | |
9282 | l2arc_dev_last = NULL; /* may have been invalidated */ | |
b128c09f BB |
9283 | atomic_dec_64(&l2arc_ndev); |
9284 | mutex_exit(&l2arc_dev_mtx); | |
34dc7c2f BB |
9285 | |
9286 | /* | |
9287 | * Clear all buflists and ARC references. L2ARC device flush. | |
9288 | */ | |
9289 | l2arc_evict(remdev, 0, B_TRUE); | |
b9541d6b CW |
9290 | list_destroy(&remdev->l2ad_buflist); |
9291 | mutex_destroy(&remdev->l2ad_mtx); | |
424fd7c3 | 9292 | zfs_refcount_destroy(&remdev->l2ad_alloc); |
34dc7c2f | 9293 | kmem_free(remdev, sizeof (l2arc_dev_t)); |
34dc7c2f BB |
9294 | } |
9295 | ||
9296 | void | |
b128c09f | 9297 | l2arc_init(void) |
34dc7c2f BB |
9298 | { |
9299 | l2arc_thread_exit = 0; | |
9300 | l2arc_ndev = 0; | |
9301 | l2arc_writes_sent = 0; | |
9302 | l2arc_writes_done = 0; | |
9303 | ||
9304 | mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); | |
9305 | cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); | |
9306 | mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); | |
34dc7c2f BB |
9307 | mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); |
9308 | ||
9309 | l2arc_dev_list = &L2ARC_dev_list; | |
9310 | l2arc_free_on_write = &L2ARC_free_on_write; | |
9311 | list_create(l2arc_dev_list, sizeof (l2arc_dev_t), | |
9312 | offsetof(l2arc_dev_t, l2ad_node)); | |
9313 | list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), | |
9314 | offsetof(l2arc_data_free_t, l2df_list_node)); | |
34dc7c2f BB |
9315 | } |
9316 | ||
9317 | void | |
b128c09f | 9318 | l2arc_fini(void) |
34dc7c2f | 9319 | { |
b128c09f BB |
9320 | /* |
9321 | * This is called from dmu_fini(), which is called from spa_fini(); | |
9322 | * Because of this, we can assume that all l2arc devices have | |
9323 | * already been removed when the pools themselves were removed. | |
9324 | */ | |
9325 | ||
9326 | l2arc_do_free_on_write(); | |
34dc7c2f BB |
9327 | |
9328 | mutex_destroy(&l2arc_feed_thr_lock); | |
9329 | cv_destroy(&l2arc_feed_thr_cv); | |
9330 | mutex_destroy(&l2arc_dev_mtx); | |
34dc7c2f BB |
9331 | mutex_destroy(&l2arc_free_on_write_mtx); |
9332 | ||
9333 | list_destroy(l2arc_dev_list); | |
9334 | list_destroy(l2arc_free_on_write); | |
9335 | } | |
b128c09f BB |
9336 | |
9337 | void | |
9338 | l2arc_start(void) | |
9339 | { | |
fb5f0bc8 | 9340 | if (!(spa_mode_global & FWRITE)) |
b128c09f BB |
9341 | return; |
9342 | ||
9343 | (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, | |
1229323d | 9344 | TS_RUN, defclsyspri); |
b128c09f BB |
9345 | } |
9346 | ||
9347 | void | |
9348 | l2arc_stop(void) | |
9349 | { | |
fb5f0bc8 | 9350 | if (!(spa_mode_global & FWRITE)) |
b128c09f BB |
9351 | return; |
9352 | ||
9353 | mutex_enter(&l2arc_feed_thr_lock); | |
9354 | cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ | |
9355 | l2arc_thread_exit = 1; | |
9356 | while (l2arc_thread_exit != 0) | |
9357 | cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); | |
9358 | mutex_exit(&l2arc_feed_thr_lock); | |
9359 | } | |
c28b2279 | 9360 | |
93ce2b4c | 9361 | #if defined(_KERNEL) |
0f699108 AZ |
9362 | EXPORT_SYMBOL(arc_buf_size); |
9363 | EXPORT_SYMBOL(arc_write); | |
c28b2279 | 9364 | EXPORT_SYMBOL(arc_read); |
e0b0ca98 | 9365 | EXPORT_SYMBOL(arc_buf_info); |
c28b2279 | 9366 | EXPORT_SYMBOL(arc_getbuf_func); |
ab26409d BB |
9367 | EXPORT_SYMBOL(arc_add_prune_callback); |
9368 | EXPORT_SYMBOL(arc_remove_prune_callback); | |
c28b2279 | 9369 | |
02730c33 | 9370 | /* BEGIN CSTYLED */ |
bce45ec9 | 9371 | module_param(zfs_arc_min, ulong, 0644); |
c409e464 | 9372 | MODULE_PARM_DESC(zfs_arc_min, "Min arc size"); |
c28b2279 | 9373 | |
bce45ec9 | 9374 | module_param(zfs_arc_max, ulong, 0644); |
c409e464 | 9375 | MODULE_PARM_DESC(zfs_arc_max, "Max arc size"); |
c28b2279 | 9376 | |
bce45ec9 | 9377 | module_param(zfs_arc_meta_limit, ulong, 0644); |
c28b2279 | 9378 | MODULE_PARM_DESC(zfs_arc_meta_limit, "Meta limit for arc size"); |
6a8f9b6b | 9379 | |
9907cc1c G |
9380 | module_param(zfs_arc_meta_limit_percent, ulong, 0644); |
9381 | MODULE_PARM_DESC(zfs_arc_meta_limit_percent, | |
9382 | "Percent of arc size for arc meta limit"); | |
9383 | ||
ca0bf58d PS |
9384 | module_param(zfs_arc_meta_min, ulong, 0644); |
9385 | MODULE_PARM_DESC(zfs_arc_meta_min, "Min arc metadata"); | |
9386 | ||
bce45ec9 | 9387 | module_param(zfs_arc_meta_prune, int, 0644); |
2cbb06b5 | 9388 | MODULE_PARM_DESC(zfs_arc_meta_prune, "Meta objects to scan for prune"); |
c409e464 | 9389 | |
ca67b33a | 9390 | module_param(zfs_arc_meta_adjust_restarts, int, 0644); |
bc888666 BB |
9391 | MODULE_PARM_DESC(zfs_arc_meta_adjust_restarts, |
9392 | "Limit number of restarts in arc_adjust_meta"); | |
9393 | ||
f6046738 BB |
9394 | module_param(zfs_arc_meta_strategy, int, 0644); |
9395 | MODULE_PARM_DESC(zfs_arc_meta_strategy, "Meta reclaim strategy"); | |
9396 | ||
bce45ec9 | 9397 | module_param(zfs_arc_grow_retry, int, 0644); |
c409e464 BB |
9398 | MODULE_PARM_DESC(zfs_arc_grow_retry, "Seconds before growing arc size"); |
9399 | ||
62422785 PS |
9400 | module_param(zfs_arc_p_dampener_disable, int, 0644); |
9401 | MODULE_PARM_DESC(zfs_arc_p_dampener_disable, "disable arc_p adapt dampener"); | |
9402 | ||
bce45ec9 | 9403 | module_param(zfs_arc_shrink_shift, int, 0644); |
c409e464 BB |
9404 | MODULE_PARM_DESC(zfs_arc_shrink_shift, "log2(fraction of arc to reclaim)"); |
9405 | ||
03b60eee DB |
9406 | module_param(zfs_arc_pc_percent, uint, 0644); |
9407 | MODULE_PARM_DESC(zfs_arc_pc_percent, | |
9408 | "Percent of pagecache to reclaim arc to"); | |
9409 | ||
728d6ae9 BB |
9410 | module_param(zfs_arc_p_min_shift, int, 0644); |
9411 | MODULE_PARM_DESC(zfs_arc_p_min_shift, "arc_c shift to calc min/max arc_p"); | |
9412 | ||
49ddb315 MA |
9413 | module_param(zfs_arc_average_blocksize, int, 0444); |
9414 | MODULE_PARM_DESC(zfs_arc_average_blocksize, "Target average block size"); | |
9415 | ||
d3c2ae1c | 9416 | module_param(zfs_compressed_arc_enabled, int, 0644); |
544596c5 | 9417 | MODULE_PARM_DESC(zfs_compressed_arc_enabled, "Disable compressed arc buffers"); |
d3c2ae1c | 9418 | |
d4a72f23 TC |
9419 | module_param(zfs_arc_min_prefetch_ms, int, 0644); |
9420 | MODULE_PARM_DESC(zfs_arc_min_prefetch_ms, "Min life of prefetch block in ms"); | |
9421 | ||
9422 | module_param(zfs_arc_min_prescient_prefetch_ms, int, 0644); | |
9423 | MODULE_PARM_DESC(zfs_arc_min_prescient_prefetch_ms, | |
9424 | "Min life of prescient prefetched block in ms"); | |
bce45ec9 BB |
9425 | |
9426 | module_param(l2arc_write_max, ulong, 0644); | |
abd8610c BB |
9427 | MODULE_PARM_DESC(l2arc_write_max, "Max write bytes per interval"); |
9428 | ||
bce45ec9 | 9429 | module_param(l2arc_write_boost, ulong, 0644); |
abd8610c BB |
9430 | MODULE_PARM_DESC(l2arc_write_boost, "Extra write bytes during device warmup"); |
9431 | ||
bce45ec9 | 9432 | module_param(l2arc_headroom, ulong, 0644); |
abd8610c BB |
9433 | MODULE_PARM_DESC(l2arc_headroom, "Number of max device writes to precache"); |
9434 | ||
3a17a7a9 SK |
9435 | module_param(l2arc_headroom_boost, ulong, 0644); |
9436 | MODULE_PARM_DESC(l2arc_headroom_boost, "Compressed l2arc_headroom multiplier"); | |
9437 | ||
bce45ec9 | 9438 | module_param(l2arc_feed_secs, ulong, 0644); |
abd8610c BB |
9439 | MODULE_PARM_DESC(l2arc_feed_secs, "Seconds between L2ARC writing"); |
9440 | ||
bce45ec9 | 9441 | module_param(l2arc_feed_min_ms, ulong, 0644); |
abd8610c BB |
9442 | MODULE_PARM_DESC(l2arc_feed_min_ms, "Min feed interval in milliseconds"); |
9443 | ||
bce45ec9 | 9444 | module_param(l2arc_noprefetch, int, 0644); |
abd8610c BB |
9445 | MODULE_PARM_DESC(l2arc_noprefetch, "Skip caching prefetched buffers"); |
9446 | ||
bce45ec9 | 9447 | module_param(l2arc_feed_again, int, 0644); |
abd8610c BB |
9448 | MODULE_PARM_DESC(l2arc_feed_again, "Turbo L2ARC warmup"); |
9449 | ||
bce45ec9 | 9450 | module_param(l2arc_norw, int, 0644); |
abd8610c BB |
9451 | MODULE_PARM_DESC(l2arc_norw, "No reads during writes"); |
9452 | ||
7e8bddd0 BB |
9453 | module_param(zfs_arc_lotsfree_percent, int, 0644); |
9454 | MODULE_PARM_DESC(zfs_arc_lotsfree_percent, | |
9455 | "System free memory I/O throttle in bytes"); | |
9456 | ||
11f552fa BB |
9457 | module_param(zfs_arc_sys_free, ulong, 0644); |
9458 | MODULE_PARM_DESC(zfs_arc_sys_free, "System free memory target size in bytes"); | |
9459 | ||
25458cbe TC |
9460 | module_param(zfs_arc_dnode_limit, ulong, 0644); |
9461 | MODULE_PARM_DESC(zfs_arc_dnode_limit, "Minimum bytes of dnodes in arc"); | |
9462 | ||
9907cc1c G |
9463 | module_param(zfs_arc_dnode_limit_percent, ulong, 0644); |
9464 | MODULE_PARM_DESC(zfs_arc_dnode_limit_percent, | |
9465 | "Percent of ARC meta buffers for dnodes"); | |
9466 | ||
25458cbe TC |
9467 | module_param(zfs_arc_dnode_reduce_percent, ulong, 0644); |
9468 | MODULE_PARM_DESC(zfs_arc_dnode_reduce_percent, | |
9469 | "Percentage of excess dnodes to try to unpin"); | |
02730c33 | 9470 | /* END CSTYLED */ |
c28b2279 | 9471 | #endif |