]>
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. |
5dbd68a3 | 23 | * Copyright (c) 2011, 2014 by Delphix. All rights reserved. |
3a17a7a9 | 24 | * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. |
3bec585e | 25 | * Copyright 2014 Nexenta Systems, Inc. All rights reserved. |
34dc7c2f BB |
26 | */ |
27 | ||
34dc7c2f BB |
28 | /* |
29 | * DVA-based Adjustable Replacement Cache | |
30 | * | |
31 | * While much of the theory of operation used here is | |
32 | * based on the self-tuning, low overhead replacement cache | |
33 | * presented by Megiddo and Modha at FAST 2003, there are some | |
34 | * significant differences: | |
35 | * | |
36 | * 1. The Megiddo and Modha model assumes any page is evictable. | |
37 | * Pages in its cache cannot be "locked" into memory. This makes | |
38 | * the eviction algorithm simple: evict the last page in the list. | |
39 | * This also make the performance characteristics easy to reason | |
40 | * about. Our cache is not so simple. At any given moment, some | |
41 | * subset of the blocks in the cache are un-evictable because we | |
42 | * have handed out a reference to them. Blocks are only evictable | |
43 | * when there are no external references active. This makes | |
44 | * eviction far more problematic: we choose to evict the evictable | |
45 | * blocks that are the "lowest" in the list. | |
46 | * | |
47 | * There are times when it is not possible to evict the requested | |
48 | * space. In these circumstances we are unable to adjust the cache | |
49 | * size. To prevent the cache growing unbounded at these times we | |
50 | * implement a "cache throttle" that slows the flow of new data | |
51 | * into the cache until we can make space available. | |
52 | * | |
53 | * 2. The Megiddo and Modha model assumes a fixed cache size. | |
54 | * Pages are evicted when the cache is full and there is a cache | |
55 | * miss. Our model has a variable sized cache. It grows with | |
56 | * high use, but also tries to react to memory pressure from the | |
57 | * operating system: decreasing its size when system memory is | |
58 | * tight. | |
59 | * | |
60 | * 3. The Megiddo and Modha model assumes a fixed page size. All | |
d3cc8b15 | 61 | * elements of the cache are therefore exactly the same size. So |
34dc7c2f BB |
62 | * when adjusting the cache size following a cache miss, its simply |
63 | * a matter of choosing a single page to evict. In our model, we | |
64 | * have variable sized cache blocks (rangeing from 512 bytes to | |
d3cc8b15 | 65 | * 128K bytes). We therefore choose a set of blocks to evict to make |
34dc7c2f BB |
66 | * space for a cache miss that approximates as closely as possible |
67 | * the space used by the new block. | |
68 | * | |
69 | * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" | |
70 | * by N. Megiddo & D. Modha, FAST 2003 | |
71 | */ | |
72 | ||
73 | /* | |
74 | * The locking model: | |
75 | * | |
76 | * A new reference to a cache buffer can be obtained in two | |
77 | * ways: 1) via a hash table lookup using the DVA as a key, | |
78 | * or 2) via one of the ARC lists. The arc_read() interface | |
79 | * uses method 1, while the internal arc algorithms for | |
d3cc8b15 | 80 | * adjusting the cache use method 2. We therefore provide two |
34dc7c2f BB |
81 | * types of locks: 1) the hash table lock array, and 2) the |
82 | * arc list locks. | |
83 | * | |
5c839890 BC |
84 | * Buffers do not have their own mutexes, rather they rely on the |
85 | * hash table mutexes for the bulk of their protection (i.e. most | |
86 | * fields in the arc_buf_hdr_t are protected by these mutexes). | |
34dc7c2f BB |
87 | * |
88 | * buf_hash_find() returns the appropriate mutex (held) when it | |
89 | * locates the requested buffer in the hash table. It returns | |
90 | * NULL for the mutex if the buffer was not in the table. | |
91 | * | |
92 | * buf_hash_remove() expects the appropriate hash mutex to be | |
93 | * already held before it is invoked. | |
94 | * | |
95 | * Each arc state also has a mutex which is used to protect the | |
96 | * buffer list associated with the state. When attempting to | |
97 | * obtain a hash table lock while holding an arc list lock you | |
98 | * must use: mutex_tryenter() to avoid deadlock. Also note that | |
99 | * the active state mutex must be held before the ghost state mutex. | |
100 | * | |
101 | * Arc buffers may have an associated eviction callback function. | |
102 | * This function will be invoked prior to removing the buffer (e.g. | |
103 | * in arc_do_user_evicts()). Note however that the data associated | |
104 | * with the buffer may be evicted prior to the callback. The callback | |
105 | * must be made with *no locks held* (to prevent deadlock). Additionally, | |
106 | * the users of callbacks must ensure that their private data is | |
bd089c54 | 107 | * protected from simultaneous callbacks from arc_clear_callback() |
34dc7c2f BB |
108 | * and arc_do_user_evicts(). |
109 | * | |
ab26409d BB |
110 | * It as also possible to register a callback which is run when the |
111 | * arc_meta_limit is reached and no buffers can be safely evicted. In | |
112 | * this case the arc user should drop a reference on some arc buffers so | |
113 | * they can be reclaimed and the arc_meta_limit honored. For example, | |
114 | * when using the ZPL each dentry holds a references on a znode. These | |
115 | * dentries must be pruned before the arc buffer holding the znode can | |
116 | * be safely evicted. | |
117 | * | |
34dc7c2f BB |
118 | * Note that the majority of the performance stats are manipulated |
119 | * with atomic operations. | |
120 | * | |
b9541d6b | 121 | * The L2ARC uses the l2ad_mtx on each vdev for the following: |
34dc7c2f BB |
122 | * |
123 | * - L2ARC buflist creation | |
124 | * - L2ARC buflist eviction | |
125 | * - L2ARC write completion, which walks L2ARC buflists | |
126 | * - ARC header destruction, as it removes from L2ARC buflists | |
127 | * - ARC header release, as it removes from L2ARC buflists | |
128 | */ | |
129 | ||
130 | #include <sys/spa.h> | |
131 | #include <sys/zio.h> | |
3a17a7a9 | 132 | #include <sys/zio_compress.h> |
34dc7c2f BB |
133 | #include <sys/zfs_context.h> |
134 | #include <sys/arc.h> | |
b128c09f | 135 | #include <sys/vdev.h> |
9babb374 | 136 | #include <sys/vdev_impl.h> |
e8b96c60 | 137 | #include <sys/dsl_pool.h> |
ca0bf58d | 138 | #include <sys/multilist.h> |
34dc7c2f BB |
139 | #ifdef _KERNEL |
140 | #include <sys/vmsystm.h> | |
141 | #include <vm/anon.h> | |
142 | #include <sys/fs/swapnode.h> | |
ab26409d | 143 | #include <sys/zpl.h> |
aaed7c40 | 144 | #include <linux/mm_compat.h> |
34dc7c2f BB |
145 | #endif |
146 | #include <sys/callb.h> | |
147 | #include <sys/kstat.h> | |
570827e1 | 148 | #include <sys/dmu_tx.h> |
428870ff | 149 | #include <zfs_fletcher.h> |
59ec819a | 150 | #include <sys/arc_impl.h> |
49ee64e5 | 151 | #include <sys/trace_arc.h> |
34dc7c2f | 152 | |
498877ba MA |
153 | #ifndef _KERNEL |
154 | /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ | |
155 | boolean_t arc_watch = B_FALSE; | |
156 | #endif | |
157 | ||
ca0bf58d PS |
158 | static kmutex_t arc_reclaim_lock; |
159 | static kcondvar_t arc_reclaim_thread_cv; | |
160 | static boolean_t arc_reclaim_thread_exit; | |
161 | static kcondvar_t arc_reclaim_waiters_cv; | |
162 | ||
163 | static kmutex_t arc_user_evicts_lock; | |
164 | static kcondvar_t arc_user_evicts_cv; | |
165 | static boolean_t arc_user_evicts_thread_exit; | |
34dc7c2f | 166 | |
e8b96c60 | 167 | /* |
ca0bf58d PS |
168 | * The number of headers to evict in arc_evict_state_impl() before |
169 | * dropping the sublist lock and evicting from another sublist. A lower | |
170 | * value means we're more likely to evict the "correct" header (i.e. the | |
171 | * oldest header in the arc state), but comes with higher overhead | |
172 | * (i.e. more invocations of arc_evict_state_impl()). | |
173 | */ | |
174 | int zfs_arc_evict_batch_limit = 10; | |
175 | ||
176 | /* | |
177 | * The number of sublists used for each of the arc state lists. If this | |
178 | * is not set to a suitable value by the user, it will be configured to | |
179 | * the number of CPUs on the system in arc_init(). | |
e8b96c60 | 180 | */ |
ca0bf58d | 181 | int zfs_arc_num_sublists_per_state = 0; |
e8b96c60 | 182 | |
34dc7c2f | 183 | /* number of seconds before growing cache again */ |
ca67b33a | 184 | static int arc_grow_retry = 5; |
34dc7c2f | 185 | |
ca0bf58d | 186 | /* shift of arc_c for calculating overflow limit in arc_get_data_buf */ |
ca67b33a | 187 | int zfs_arc_overflow_shift = 8; |
62422785 | 188 | |
d164b209 | 189 | /* log2(fraction of arc to reclaim) */ |
ca67b33a | 190 | static int arc_shrink_shift = 7; |
d164b209 | 191 | |
34dc7c2f | 192 | /* |
ca67b33a MA |
193 | * log2(fraction of ARC which must be free to allow growing). |
194 | * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, | |
195 | * when reading a new block into the ARC, we will evict an equal-sized block | |
196 | * from the ARC. | |
197 | * | |
198 | * This must be less than arc_shrink_shift, so that when we shrink the ARC, | |
199 | * we will still not allow it to grow. | |
34dc7c2f | 200 | */ |
ca67b33a | 201 | int arc_no_grow_shift = 5; |
bce45ec9 | 202 | |
49ddb315 | 203 | |
ca0bf58d PS |
204 | /* |
205 | * minimum lifespan of a prefetch block in clock ticks | |
206 | * (initialized in arc_init()) | |
207 | */ | |
ca67b33a | 208 | static int arc_min_prefetch_lifespan; |
ca0bf58d | 209 | |
e8b96c60 MA |
210 | /* |
211 | * If this percent of memory is free, don't throttle. | |
212 | */ | |
213 | int arc_lotsfree_percent = 10; | |
214 | ||
34dc7c2f BB |
215 | static int arc_dead; |
216 | ||
b128c09f BB |
217 | /* |
218 | * The arc has filled available memory and has now warmed up. | |
219 | */ | |
220 | static boolean_t arc_warm; | |
221 | ||
34dc7c2f BB |
222 | /* |
223 | * These tunables are for performance analysis. | |
224 | */ | |
c28b2279 BB |
225 | unsigned long zfs_arc_max = 0; |
226 | unsigned long zfs_arc_min = 0; | |
227 | unsigned long zfs_arc_meta_limit = 0; | |
ca0bf58d | 228 | unsigned long zfs_arc_meta_min = 0; |
ca67b33a MA |
229 | int zfs_arc_grow_retry = 0; |
230 | int zfs_arc_shrink_shift = 0; | |
231 | int zfs_disable_dup_eviction = 0; | |
232 | int zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ | |
34dc7c2f | 233 | |
bc888666 | 234 | /* |
ca67b33a | 235 | * These tunables are Linux specific |
bc888666 | 236 | */ |
ca67b33a MA |
237 | int zfs_arc_memory_throttle_disable = 1; |
238 | int zfs_arc_min_prefetch_lifespan = 0; | |
239 | int zfs_arc_p_aggressive_disable = 1; | |
240 | int zfs_arc_p_dampener_disable = 1; | |
241 | int zfs_arc_meta_prune = 10000; | |
242 | int zfs_arc_meta_strategy = ARC_STRATEGY_META_BALANCED; | |
243 | int zfs_arc_meta_adjust_restarts = 4096; | |
bc888666 | 244 | |
34dc7c2f BB |
245 | /* The 6 states: */ |
246 | static arc_state_t ARC_anon; | |
247 | static arc_state_t ARC_mru; | |
248 | static arc_state_t ARC_mru_ghost; | |
249 | static arc_state_t ARC_mfu; | |
250 | static arc_state_t ARC_mfu_ghost; | |
251 | static arc_state_t ARC_l2c_only; | |
252 | ||
253 | typedef struct arc_stats { | |
254 | kstat_named_t arcstat_hits; | |
255 | kstat_named_t arcstat_misses; | |
256 | kstat_named_t arcstat_demand_data_hits; | |
257 | kstat_named_t arcstat_demand_data_misses; | |
258 | kstat_named_t arcstat_demand_metadata_hits; | |
259 | kstat_named_t arcstat_demand_metadata_misses; | |
260 | kstat_named_t arcstat_prefetch_data_hits; | |
261 | kstat_named_t arcstat_prefetch_data_misses; | |
262 | kstat_named_t arcstat_prefetch_metadata_hits; | |
263 | kstat_named_t arcstat_prefetch_metadata_misses; | |
264 | kstat_named_t arcstat_mru_hits; | |
265 | kstat_named_t arcstat_mru_ghost_hits; | |
266 | kstat_named_t arcstat_mfu_hits; | |
267 | kstat_named_t arcstat_mfu_ghost_hits; | |
268 | kstat_named_t arcstat_deleted; | |
e49f1e20 WA |
269 | /* |
270 | * Number of buffers that could not be evicted because the hash lock | |
271 | * was held by another thread. The lock may not necessarily be held | |
272 | * by something using the same buffer, since hash locks are shared | |
273 | * by multiple buffers. | |
274 | */ | |
34dc7c2f | 275 | kstat_named_t arcstat_mutex_miss; |
e49f1e20 WA |
276 | /* |
277 | * Number of buffers skipped because they have I/O in progress, are | |
278 | * indrect prefetch buffers that have not lived long enough, or are | |
279 | * not from the spa we're trying to evict from. | |
280 | */ | |
34dc7c2f | 281 | kstat_named_t arcstat_evict_skip; |
ca0bf58d PS |
282 | /* |
283 | * Number of times arc_evict_state() was unable to evict enough | |
284 | * buffers to reach its target amount. | |
285 | */ | |
286 | kstat_named_t arcstat_evict_not_enough; | |
428870ff BB |
287 | kstat_named_t arcstat_evict_l2_cached; |
288 | kstat_named_t arcstat_evict_l2_eligible; | |
289 | kstat_named_t arcstat_evict_l2_ineligible; | |
ca0bf58d | 290 | kstat_named_t arcstat_evict_l2_skip; |
34dc7c2f BB |
291 | kstat_named_t arcstat_hash_elements; |
292 | kstat_named_t arcstat_hash_elements_max; | |
293 | kstat_named_t arcstat_hash_collisions; | |
294 | kstat_named_t arcstat_hash_chains; | |
295 | kstat_named_t arcstat_hash_chain_max; | |
296 | kstat_named_t arcstat_p; | |
297 | kstat_named_t arcstat_c; | |
298 | kstat_named_t arcstat_c_min; | |
299 | kstat_named_t arcstat_c_max; | |
300 | kstat_named_t arcstat_size; | |
500445c0 PS |
301 | /* |
302 | * Number of bytes consumed by internal ARC structures necessary | |
303 | * for tracking purposes; these structures are not actually | |
304 | * backed by ARC buffers. This includes arc_buf_hdr_t structures | |
305 | * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only | |
306 | * caches), and arc_buf_t structures (allocated via arc_buf_t | |
307 | * cache). | |
308 | */ | |
34dc7c2f | 309 | kstat_named_t arcstat_hdr_size; |
500445c0 PS |
310 | /* |
311 | * Number of bytes consumed by ARC buffers of type equal to | |
312 | * ARC_BUFC_DATA. This is generally consumed by buffers backing | |
313 | * on disk user data (e.g. plain file contents). | |
314 | */ | |
d164b209 | 315 | kstat_named_t arcstat_data_size; |
500445c0 PS |
316 | /* |
317 | * Number of bytes consumed by ARC buffers of type equal to | |
318 | * ARC_BUFC_METADATA. This is generally consumed by buffers | |
319 | * backing on disk data that is used for internal ZFS | |
320 | * structures (e.g. ZAP, dnode, indirect blocks, etc). | |
321 | */ | |
322 | kstat_named_t arcstat_metadata_size; | |
323 | /* | |
324 | * Number of bytes consumed by various buffers and structures | |
325 | * not actually backed with ARC buffers. This includes bonus | |
326 | * buffers (allocated directly via zio_buf_* functions), | |
327 | * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t | |
328 | * cache), and dnode_t structures (allocated via dnode_t cache). | |
329 | */ | |
d164b209 | 330 | kstat_named_t arcstat_other_size; |
500445c0 PS |
331 | /* |
332 | * Total number of bytes consumed by ARC buffers residing in the | |
333 | * arc_anon state. This includes *all* buffers in the arc_anon | |
334 | * state; e.g. data, metadata, evictable, and unevictable buffers | |
335 | * are all included in this value. | |
336 | */ | |
13be560d | 337 | kstat_named_t arcstat_anon_size; |
500445c0 PS |
338 | /* |
339 | * Number of bytes consumed by ARC buffers that meet the | |
340 | * following criteria: backing buffers of type ARC_BUFC_DATA, | |
341 | * residing in the arc_anon state, and are eligible for eviction | |
342 | * (e.g. have no outstanding holds on the buffer). | |
343 | */ | |
344 | kstat_named_t arcstat_anon_evictable_data; | |
345 | /* | |
346 | * Number of bytes consumed by ARC buffers that meet the | |
347 | * following criteria: backing buffers of type ARC_BUFC_METADATA, | |
348 | * residing in the arc_anon state, and are eligible for eviction | |
349 | * (e.g. have no outstanding holds on the buffer). | |
350 | */ | |
351 | kstat_named_t arcstat_anon_evictable_metadata; | |
352 | /* | |
353 | * Total number of bytes consumed by ARC buffers residing in the | |
354 | * arc_mru state. This includes *all* buffers in the arc_mru | |
355 | * state; e.g. data, metadata, evictable, and unevictable buffers | |
356 | * are all included in this value. | |
357 | */ | |
13be560d | 358 | kstat_named_t arcstat_mru_size; |
500445c0 PS |
359 | /* |
360 | * Number of bytes consumed by ARC buffers that meet the | |
361 | * following criteria: backing buffers of type ARC_BUFC_DATA, | |
362 | * residing in the arc_mru state, and are eligible for eviction | |
363 | * (e.g. have no outstanding holds on the buffer). | |
364 | */ | |
365 | kstat_named_t arcstat_mru_evictable_data; | |
366 | /* | |
367 | * Number of bytes consumed by ARC buffers that meet the | |
368 | * following criteria: backing buffers of type ARC_BUFC_METADATA, | |
369 | * residing in the arc_mru state, and are eligible for eviction | |
370 | * (e.g. have no outstanding holds on the buffer). | |
371 | */ | |
372 | kstat_named_t arcstat_mru_evictable_metadata; | |
373 | /* | |
374 | * Total number of bytes that *would have been* consumed by ARC | |
375 | * buffers in the arc_mru_ghost state. The key thing to note | |
376 | * here, is the fact that this size doesn't actually indicate | |
377 | * RAM consumption. The ghost lists only consist of headers and | |
378 | * don't actually have ARC buffers linked off of these headers. | |
379 | * Thus, *if* the headers had associated ARC buffers, these | |
380 | * buffers *would have* consumed this number of bytes. | |
381 | */ | |
13be560d | 382 | kstat_named_t arcstat_mru_ghost_size; |
500445c0 PS |
383 | /* |
384 | * Number of bytes that *would have been* consumed by ARC | |
385 | * buffers that are eligible for eviction, of type | |
386 | * ARC_BUFC_DATA, and linked off the arc_mru_ghost state. | |
387 | */ | |
388 | kstat_named_t arcstat_mru_ghost_evictable_data; | |
389 | /* | |
390 | * Number of bytes that *would have been* consumed by ARC | |
391 | * buffers that are eligible for eviction, of type | |
392 | * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. | |
393 | */ | |
394 | kstat_named_t arcstat_mru_ghost_evictable_metadata; | |
395 | /* | |
396 | * Total number of bytes consumed by ARC buffers residing in the | |
397 | * arc_mfu state. This includes *all* buffers in the arc_mfu | |
398 | * state; e.g. data, metadata, evictable, and unevictable buffers | |
399 | * are all included in this value. | |
400 | */ | |
13be560d | 401 | kstat_named_t arcstat_mfu_size; |
500445c0 PS |
402 | /* |
403 | * Number of bytes consumed by ARC buffers that are eligible for | |
404 | * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu | |
405 | * state. | |
406 | */ | |
407 | kstat_named_t arcstat_mfu_evictable_data; | |
408 | /* | |
409 | * Number of bytes consumed by ARC buffers that are eligible for | |
410 | * eviction, of type ARC_BUFC_METADATA, and reside in the | |
411 | * arc_mfu state. | |
412 | */ | |
413 | kstat_named_t arcstat_mfu_evictable_metadata; | |
414 | /* | |
415 | * Total number of bytes that *would have been* consumed by ARC | |
416 | * buffers in the arc_mfu_ghost state. See the comment above | |
417 | * arcstat_mru_ghost_size for more details. | |
418 | */ | |
13be560d | 419 | kstat_named_t arcstat_mfu_ghost_size; |
500445c0 PS |
420 | /* |
421 | * Number of bytes that *would have been* consumed by ARC | |
422 | * buffers that are eligible for eviction, of type | |
423 | * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state. | |
424 | */ | |
425 | kstat_named_t arcstat_mfu_ghost_evictable_data; | |
426 | /* | |
427 | * Number of bytes that *would have been* consumed by ARC | |
428 | * buffers that are eligible for eviction, of type | |
429 | * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state. | |
430 | */ | |
431 | kstat_named_t arcstat_mfu_ghost_evictable_metadata; | |
34dc7c2f BB |
432 | kstat_named_t arcstat_l2_hits; |
433 | kstat_named_t arcstat_l2_misses; | |
434 | kstat_named_t arcstat_l2_feeds; | |
435 | kstat_named_t arcstat_l2_rw_clash; | |
d164b209 BB |
436 | kstat_named_t arcstat_l2_read_bytes; |
437 | kstat_named_t arcstat_l2_write_bytes; | |
34dc7c2f BB |
438 | kstat_named_t arcstat_l2_writes_sent; |
439 | kstat_named_t arcstat_l2_writes_done; | |
440 | kstat_named_t arcstat_l2_writes_error; | |
ca0bf58d | 441 | kstat_named_t arcstat_l2_writes_lock_retry; |
34dc7c2f BB |
442 | kstat_named_t arcstat_l2_evict_lock_retry; |
443 | kstat_named_t arcstat_l2_evict_reading; | |
b9541d6b | 444 | kstat_named_t arcstat_l2_evict_l1cached; |
34dc7c2f | 445 | kstat_named_t arcstat_l2_free_on_write; |
ca0bf58d | 446 | kstat_named_t arcstat_l2_cdata_free_on_write; |
34dc7c2f BB |
447 | kstat_named_t arcstat_l2_abort_lowmem; |
448 | kstat_named_t arcstat_l2_cksum_bad; | |
449 | kstat_named_t arcstat_l2_io_error; | |
450 | kstat_named_t arcstat_l2_size; | |
3a17a7a9 | 451 | kstat_named_t arcstat_l2_asize; |
34dc7c2f | 452 | kstat_named_t arcstat_l2_hdr_size; |
3a17a7a9 SK |
453 | kstat_named_t arcstat_l2_compress_successes; |
454 | kstat_named_t arcstat_l2_compress_zeros; | |
455 | kstat_named_t arcstat_l2_compress_failures; | |
34dc7c2f | 456 | kstat_named_t arcstat_memory_throttle_count; |
1eb5bfa3 GW |
457 | kstat_named_t arcstat_duplicate_buffers; |
458 | kstat_named_t arcstat_duplicate_buffers_size; | |
459 | kstat_named_t arcstat_duplicate_reads; | |
7cb67b45 BB |
460 | kstat_named_t arcstat_memory_direct_count; |
461 | kstat_named_t arcstat_memory_indirect_count; | |
1834f2d8 BB |
462 | kstat_named_t arcstat_no_grow; |
463 | kstat_named_t arcstat_tempreserve; | |
464 | kstat_named_t arcstat_loaned_bytes; | |
ab26409d | 465 | kstat_named_t arcstat_prune; |
1834f2d8 BB |
466 | kstat_named_t arcstat_meta_used; |
467 | kstat_named_t arcstat_meta_limit; | |
468 | kstat_named_t arcstat_meta_max; | |
ca0bf58d | 469 | kstat_named_t arcstat_meta_min; |
34dc7c2f BB |
470 | } arc_stats_t; |
471 | ||
472 | static arc_stats_t arc_stats = { | |
473 | { "hits", KSTAT_DATA_UINT64 }, | |
474 | { "misses", KSTAT_DATA_UINT64 }, | |
475 | { "demand_data_hits", KSTAT_DATA_UINT64 }, | |
476 | { "demand_data_misses", KSTAT_DATA_UINT64 }, | |
477 | { "demand_metadata_hits", KSTAT_DATA_UINT64 }, | |
478 | { "demand_metadata_misses", KSTAT_DATA_UINT64 }, | |
479 | { "prefetch_data_hits", KSTAT_DATA_UINT64 }, | |
480 | { "prefetch_data_misses", KSTAT_DATA_UINT64 }, | |
481 | { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, | |
482 | { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, | |
483 | { "mru_hits", KSTAT_DATA_UINT64 }, | |
484 | { "mru_ghost_hits", KSTAT_DATA_UINT64 }, | |
485 | { "mfu_hits", KSTAT_DATA_UINT64 }, | |
486 | { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, | |
487 | { "deleted", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
488 | { "mutex_miss", KSTAT_DATA_UINT64 }, |
489 | { "evict_skip", KSTAT_DATA_UINT64 }, | |
ca0bf58d | 490 | { "evict_not_enough", KSTAT_DATA_UINT64 }, |
428870ff BB |
491 | { "evict_l2_cached", KSTAT_DATA_UINT64 }, |
492 | { "evict_l2_eligible", KSTAT_DATA_UINT64 }, | |
493 | { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, | |
ca0bf58d | 494 | { "evict_l2_skip", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
495 | { "hash_elements", KSTAT_DATA_UINT64 }, |
496 | { "hash_elements_max", KSTAT_DATA_UINT64 }, | |
497 | { "hash_collisions", KSTAT_DATA_UINT64 }, | |
498 | { "hash_chains", KSTAT_DATA_UINT64 }, | |
499 | { "hash_chain_max", KSTAT_DATA_UINT64 }, | |
500 | { "p", KSTAT_DATA_UINT64 }, | |
501 | { "c", KSTAT_DATA_UINT64 }, | |
502 | { "c_min", KSTAT_DATA_UINT64 }, | |
503 | { "c_max", KSTAT_DATA_UINT64 }, | |
504 | { "size", KSTAT_DATA_UINT64 }, | |
505 | { "hdr_size", KSTAT_DATA_UINT64 }, | |
d164b209 | 506 | { "data_size", KSTAT_DATA_UINT64 }, |
500445c0 | 507 | { "metadata_size", KSTAT_DATA_UINT64 }, |
d164b209 | 508 | { "other_size", KSTAT_DATA_UINT64 }, |
13be560d | 509 | { "anon_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
510 | { "anon_evictable_data", KSTAT_DATA_UINT64 }, |
511 | { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 512 | { "mru_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
513 | { "mru_evictable_data", KSTAT_DATA_UINT64 }, |
514 | { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 515 | { "mru_ghost_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
516 | { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, |
517 | { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 518 | { "mfu_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
519 | { "mfu_evictable_data", KSTAT_DATA_UINT64 }, |
520 | { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, | |
13be560d | 521 | { "mfu_ghost_size", KSTAT_DATA_UINT64 }, |
500445c0 PS |
522 | { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, |
523 | { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
524 | { "l2_hits", KSTAT_DATA_UINT64 }, |
525 | { "l2_misses", KSTAT_DATA_UINT64 }, | |
526 | { "l2_feeds", KSTAT_DATA_UINT64 }, | |
527 | { "l2_rw_clash", KSTAT_DATA_UINT64 }, | |
d164b209 BB |
528 | { "l2_read_bytes", KSTAT_DATA_UINT64 }, |
529 | { "l2_write_bytes", KSTAT_DATA_UINT64 }, | |
34dc7c2f BB |
530 | { "l2_writes_sent", KSTAT_DATA_UINT64 }, |
531 | { "l2_writes_done", KSTAT_DATA_UINT64 }, | |
532 | { "l2_writes_error", KSTAT_DATA_UINT64 }, | |
ca0bf58d | 533 | { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
534 | { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, |
535 | { "l2_evict_reading", KSTAT_DATA_UINT64 }, | |
b9541d6b | 536 | { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, |
34dc7c2f | 537 | { "l2_free_on_write", KSTAT_DATA_UINT64 }, |
ca0bf58d | 538 | { "l2_cdata_free_on_write", KSTAT_DATA_UINT64 }, |
34dc7c2f BB |
539 | { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, |
540 | { "l2_cksum_bad", KSTAT_DATA_UINT64 }, | |
541 | { "l2_io_error", KSTAT_DATA_UINT64 }, | |
542 | { "l2_size", KSTAT_DATA_UINT64 }, | |
3a17a7a9 | 543 | { "l2_asize", KSTAT_DATA_UINT64 }, |
34dc7c2f | 544 | { "l2_hdr_size", KSTAT_DATA_UINT64 }, |
3a17a7a9 SK |
545 | { "l2_compress_successes", KSTAT_DATA_UINT64 }, |
546 | { "l2_compress_zeros", KSTAT_DATA_UINT64 }, | |
547 | { "l2_compress_failures", KSTAT_DATA_UINT64 }, | |
1834f2d8 | 548 | { "memory_throttle_count", KSTAT_DATA_UINT64 }, |
1eb5bfa3 GW |
549 | { "duplicate_buffers", KSTAT_DATA_UINT64 }, |
550 | { "duplicate_buffers_size", KSTAT_DATA_UINT64 }, | |
551 | { "duplicate_reads", KSTAT_DATA_UINT64 }, | |
7cb67b45 BB |
552 | { "memory_direct_count", KSTAT_DATA_UINT64 }, |
553 | { "memory_indirect_count", KSTAT_DATA_UINT64 }, | |
1834f2d8 BB |
554 | { "arc_no_grow", KSTAT_DATA_UINT64 }, |
555 | { "arc_tempreserve", KSTAT_DATA_UINT64 }, | |
556 | { "arc_loaned_bytes", KSTAT_DATA_UINT64 }, | |
ab26409d | 557 | { "arc_prune", KSTAT_DATA_UINT64 }, |
1834f2d8 BB |
558 | { "arc_meta_used", KSTAT_DATA_UINT64 }, |
559 | { "arc_meta_limit", KSTAT_DATA_UINT64 }, | |
560 | { "arc_meta_max", KSTAT_DATA_UINT64 }, | |
500445c0 | 561 | { "arc_meta_min", KSTAT_DATA_UINT64 } |
34dc7c2f BB |
562 | }; |
563 | ||
564 | #define ARCSTAT(stat) (arc_stats.stat.value.ui64) | |
565 | ||
566 | #define ARCSTAT_INCR(stat, val) \ | |
d3cc8b15 | 567 | atomic_add_64(&arc_stats.stat.value.ui64, (val)) |
34dc7c2f | 568 | |
428870ff | 569 | #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1) |
34dc7c2f BB |
570 | #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1) |
571 | ||
572 | #define ARCSTAT_MAX(stat, val) { \ | |
573 | uint64_t m; \ | |
574 | while ((val) > (m = arc_stats.stat.value.ui64) && \ | |
575 | (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ | |
576 | continue; \ | |
577 | } | |
578 | ||
579 | #define ARCSTAT_MAXSTAT(stat) \ | |
580 | ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64) | |
581 | ||
582 | /* | |
583 | * We define a macro to allow ARC hits/misses to be easily broken down by | |
584 | * two separate conditions, giving a total of four different subtypes for | |
585 | * each of hits and misses (so eight statistics total). | |
586 | */ | |
587 | #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ | |
588 | if (cond1) { \ | |
589 | if (cond2) { \ | |
590 | ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ | |
591 | } else { \ | |
592 | ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ | |
593 | } \ | |
594 | } else { \ | |
595 | if (cond2) { \ | |
596 | ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ | |
597 | } else { \ | |
598 | ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ | |
599 | } \ | |
600 | } | |
601 | ||
602 | kstat_t *arc_ksp; | |
428870ff | 603 | static arc_state_t *arc_anon; |
34dc7c2f BB |
604 | static arc_state_t *arc_mru; |
605 | static arc_state_t *arc_mru_ghost; | |
606 | static arc_state_t *arc_mfu; | |
607 | static arc_state_t *arc_mfu_ghost; | |
608 | static arc_state_t *arc_l2c_only; | |
609 | ||
610 | /* | |
611 | * There are several ARC variables that are critical to export as kstats -- | |
612 | * but we don't want to have to grovel around in the kstat whenever we wish to | |
613 | * manipulate them. For these variables, we therefore define them to be in | |
614 | * terms of the statistic variable. This assures that we are not introducing | |
615 | * the possibility of inconsistency by having shadow copies of the variables, | |
616 | * while still allowing the code to be readable. | |
617 | */ | |
618 | #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */ | |
619 | #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */ | |
620 | #define arc_c ARCSTAT(arcstat_c) /* target size of cache */ | |
621 | #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */ | |
622 | #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */ | |
1834f2d8 BB |
623 | #define arc_no_grow ARCSTAT(arcstat_no_grow) |
624 | #define arc_tempreserve ARCSTAT(arcstat_tempreserve) | |
625 | #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes) | |
23c0a133 | 626 | #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */ |
ca0bf58d | 627 | #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */ |
23c0a133 GW |
628 | #define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */ |
629 | #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */ | |
34dc7c2f | 630 | |
3a17a7a9 SK |
631 | #define L2ARC_IS_VALID_COMPRESS(_c_) \ |
632 | ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY) | |
633 | ||
ab26409d BB |
634 | static list_t arc_prune_list; |
635 | static kmutex_t arc_prune_mtx; | |
f6046738 | 636 | static taskq_t *arc_prune_taskq; |
34dc7c2f | 637 | static arc_buf_t *arc_eviction_list; |
34dc7c2f | 638 | static arc_buf_hdr_t arc_eviction_hdr; |
428870ff | 639 | |
34dc7c2f BB |
640 | #define GHOST_STATE(state) \ |
641 | ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ | |
642 | (state) == arc_l2c_only) | |
643 | ||
2a432414 GW |
644 | #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) |
645 | #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) | |
646 | #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) | |
647 | #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) | |
648 | #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FLAG_FREED_IN_READ) | |
649 | #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_FLAG_BUF_AVAILABLE) | |
b9541d6b | 650 | |
2a432414 | 651 | #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) |
b9541d6b | 652 | #define HDR_L2COMPRESS(hdr) ((hdr)->b_flags & ARC_FLAG_L2COMPRESS) |
2a432414 | 653 | #define HDR_L2_READING(hdr) \ |
b9541d6b CW |
654 | (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ |
655 | ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) | |
2a432414 GW |
656 | #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) |
657 | #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) | |
658 | #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) | |
34dc7c2f | 659 | |
b9541d6b CW |
660 | #define HDR_ISTYPE_METADATA(hdr) \ |
661 | ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) | |
662 | #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) | |
663 | ||
664 | #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) | |
665 | #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) | |
666 | ||
667 | /* For storing compression mode in b_flags */ | |
668 | #define HDR_COMPRESS_OFFSET 24 | |
669 | #define HDR_COMPRESS_NBITS 7 | |
670 | ||
671 | #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET(hdr->b_flags, \ | |
672 | HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS)) | |
673 | #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET(hdr->b_flags, \ | |
674 | HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS, (cmp)) | |
675 | ||
34dc7c2f BB |
676 | /* |
677 | * Other sizes | |
678 | */ | |
679 | ||
b9541d6b CW |
680 | #define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) |
681 | #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) | |
34dc7c2f BB |
682 | |
683 | /* | |
684 | * Hash table routines | |
685 | */ | |
686 | ||
00b46022 BB |
687 | #define HT_LOCK_ALIGN 64 |
688 | #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN))) | |
34dc7c2f BB |
689 | |
690 | struct ht_lock { | |
691 | kmutex_t ht_lock; | |
692 | #ifdef _KERNEL | |
00b46022 | 693 | unsigned char pad[HT_LOCK_PAD]; |
34dc7c2f BB |
694 | #endif |
695 | }; | |
696 | ||
b31d8ea7 | 697 | #define BUF_LOCKS 8192 |
34dc7c2f BB |
698 | typedef struct buf_hash_table { |
699 | uint64_t ht_mask; | |
700 | arc_buf_hdr_t **ht_table; | |
701 | struct ht_lock ht_locks[BUF_LOCKS]; | |
702 | } buf_hash_table_t; | |
703 | ||
704 | static buf_hash_table_t buf_hash_table; | |
705 | ||
706 | #define BUF_HASH_INDEX(spa, dva, birth) \ | |
707 | (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) | |
708 | #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) | |
709 | #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) | |
428870ff BB |
710 | #define HDR_LOCK(hdr) \ |
711 | (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) | |
34dc7c2f BB |
712 | |
713 | uint64_t zfs_crc64_table[256]; | |
714 | ||
715 | /* | |
716 | * Level 2 ARC | |
717 | */ | |
718 | ||
719 | #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ | |
3a17a7a9 SK |
720 | #define L2ARC_HEADROOM 2 /* num of writes */ |
721 | /* | |
722 | * If we discover during ARC scan any buffers to be compressed, we boost | |
723 | * our headroom for the next scanning cycle by this percentage multiple. | |
724 | */ | |
725 | #define L2ARC_HEADROOM_BOOST 200 | |
d164b209 BB |
726 | #define L2ARC_FEED_SECS 1 /* caching interval secs */ |
727 | #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ | |
34dc7c2f | 728 | |
d962d5da PS |
729 | /* |
730 | * Used to distinguish headers that are being process by | |
731 | * l2arc_write_buffers(), but have yet to be assigned to a l2arc disk | |
732 | * address. This can happen when the header is added to the l2arc's list | |
733 | * of buffers to write in the first stage of l2arc_write_buffers(), but | |
734 | * has not yet been written out which happens in the second stage of | |
735 | * l2arc_write_buffers(). | |
736 | */ | |
737 | #define L2ARC_ADDR_UNSET ((uint64_t)(-1)) | |
738 | ||
34dc7c2f BB |
739 | #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent) |
740 | #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done) | |
741 | ||
d3cc8b15 | 742 | /* L2ARC Performance Tunables */ |
abd8610c BB |
743 | unsigned long l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */ |
744 | unsigned long l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */ | |
745 | unsigned long l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */ | |
3a17a7a9 | 746 | unsigned long l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; |
abd8610c BB |
747 | unsigned long l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ |
748 | unsigned long l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */ | |
749 | int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ | |
3a17a7a9 | 750 | int l2arc_nocompress = B_FALSE; /* don't compress bufs */ |
abd8610c | 751 | int l2arc_feed_again = B_TRUE; /* turbo warmup */ |
c93504f0 | 752 | int l2arc_norw = B_FALSE; /* no reads during writes */ |
34dc7c2f BB |
753 | |
754 | /* | |
755 | * L2ARC Internals | |
756 | */ | |
34dc7c2f BB |
757 | static list_t L2ARC_dev_list; /* device list */ |
758 | static list_t *l2arc_dev_list; /* device list pointer */ | |
759 | static kmutex_t l2arc_dev_mtx; /* device list mutex */ | |
760 | static l2arc_dev_t *l2arc_dev_last; /* last device used */ | |
34dc7c2f BB |
761 | static list_t L2ARC_free_on_write; /* free after write buf list */ |
762 | static list_t *l2arc_free_on_write; /* free after write list ptr */ | |
763 | static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ | |
764 | static uint64_t l2arc_ndev; /* number of devices */ | |
765 | ||
766 | typedef struct l2arc_read_callback { | |
3a17a7a9 SK |
767 | arc_buf_t *l2rcb_buf; /* read buffer */ |
768 | spa_t *l2rcb_spa; /* spa */ | |
769 | blkptr_t l2rcb_bp; /* original blkptr */ | |
5dbd68a3 | 770 | zbookmark_phys_t l2rcb_zb; /* original bookmark */ |
3a17a7a9 SK |
771 | int l2rcb_flags; /* original flags */ |
772 | enum zio_compress l2rcb_compress; /* applied compress */ | |
34dc7c2f BB |
773 | } l2arc_read_callback_t; |
774 | ||
34dc7c2f BB |
775 | typedef struct l2arc_data_free { |
776 | /* protected by l2arc_free_on_write_mtx */ | |
777 | void *l2df_data; | |
778 | size_t l2df_size; | |
779 | void (*l2df_func)(void *, size_t); | |
780 | list_node_t l2df_list_node; | |
781 | } l2arc_data_free_t; | |
782 | ||
783 | static kmutex_t l2arc_feed_thr_lock; | |
784 | static kcondvar_t l2arc_feed_thr_cv; | |
785 | static uint8_t l2arc_thread_exit; | |
786 | ||
2a432414 GW |
787 | static void arc_get_data_buf(arc_buf_t *); |
788 | static void arc_access(arc_buf_hdr_t *, kmutex_t *); | |
ca0bf58d | 789 | static boolean_t arc_is_overflowing(void); |
2a432414 | 790 | static void arc_buf_watch(arc_buf_t *); |
ca67b33a | 791 | static void arc_tuning_update(void); |
2a432414 | 792 | |
b9541d6b CW |
793 | static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); |
794 | static uint32_t arc_bufc_to_flags(arc_buf_contents_t); | |
795 | ||
2a432414 GW |
796 | static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); |
797 | static void l2arc_read_done(zio_t *); | |
34dc7c2f | 798 | |
b9541d6b | 799 | static boolean_t l2arc_compress_buf(arc_buf_hdr_t *); |
2a432414 GW |
800 | static void l2arc_decompress_zio(zio_t *, arc_buf_hdr_t *, enum zio_compress); |
801 | static void l2arc_release_cdata_buf(arc_buf_hdr_t *); | |
3a17a7a9 | 802 | |
34dc7c2f | 803 | static uint64_t |
d164b209 | 804 | buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) |
34dc7c2f | 805 | { |
34dc7c2f BB |
806 | uint8_t *vdva = (uint8_t *)dva; |
807 | uint64_t crc = -1ULL; | |
808 | int i; | |
809 | ||
810 | ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); | |
811 | ||
812 | for (i = 0; i < sizeof (dva_t); i++) | |
813 | crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; | |
814 | ||
d164b209 | 815 | crc ^= (spa>>8) ^ birth; |
34dc7c2f BB |
816 | |
817 | return (crc); | |
818 | } | |
819 | ||
820 | #define BUF_EMPTY(buf) \ | |
821 | ((buf)->b_dva.dva_word[0] == 0 && \ | |
b9541d6b | 822 | (buf)->b_dva.dva_word[1] == 0) |
34dc7c2f BB |
823 | |
824 | #define BUF_EQUAL(spa, dva, birth, buf) \ | |
825 | ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ | |
826 | ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ | |
827 | ((buf)->b_birth == birth) && ((buf)->b_spa == spa) | |
828 | ||
428870ff BB |
829 | static void |
830 | buf_discard_identity(arc_buf_hdr_t *hdr) | |
831 | { | |
832 | hdr->b_dva.dva_word[0] = 0; | |
833 | hdr->b_dva.dva_word[1] = 0; | |
834 | hdr->b_birth = 0; | |
428870ff BB |
835 | } |
836 | ||
34dc7c2f | 837 | static arc_buf_hdr_t * |
9b67f605 | 838 | buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) |
34dc7c2f | 839 | { |
9b67f605 MA |
840 | const dva_t *dva = BP_IDENTITY(bp); |
841 | uint64_t birth = BP_PHYSICAL_BIRTH(bp); | |
34dc7c2f BB |
842 | uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); |
843 | kmutex_t *hash_lock = BUF_HASH_LOCK(idx); | |
2a432414 | 844 | arc_buf_hdr_t *hdr; |
34dc7c2f BB |
845 | |
846 | mutex_enter(hash_lock); | |
2a432414 GW |
847 | for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; |
848 | hdr = hdr->b_hash_next) { | |
849 | if (BUF_EQUAL(spa, dva, birth, hdr)) { | |
34dc7c2f | 850 | *lockp = hash_lock; |
2a432414 | 851 | return (hdr); |
34dc7c2f BB |
852 | } |
853 | } | |
854 | mutex_exit(hash_lock); | |
855 | *lockp = NULL; | |
856 | return (NULL); | |
857 | } | |
858 | ||
859 | /* | |
860 | * Insert an entry into the hash table. If there is already an element | |
861 | * equal to elem in the hash table, then the already existing element | |
862 | * will be returned and the new element will not be inserted. | |
863 | * Otherwise returns NULL. | |
b9541d6b | 864 | * If lockp == NULL, the caller is assumed to already hold the hash lock. |
34dc7c2f BB |
865 | */ |
866 | static arc_buf_hdr_t * | |
2a432414 | 867 | buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) |
34dc7c2f | 868 | { |
2a432414 | 869 | uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); |
34dc7c2f | 870 | kmutex_t *hash_lock = BUF_HASH_LOCK(idx); |
2a432414 | 871 | arc_buf_hdr_t *fhdr; |
34dc7c2f BB |
872 | uint32_t i; |
873 | ||
2a432414 GW |
874 | ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); |
875 | ASSERT(hdr->b_birth != 0); | |
876 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); | |
b9541d6b CW |
877 | |
878 | if (lockp != NULL) { | |
879 | *lockp = hash_lock; | |
880 | mutex_enter(hash_lock); | |
881 | } else { | |
882 | ASSERT(MUTEX_HELD(hash_lock)); | |
883 | } | |
884 | ||
2a432414 GW |
885 | for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; |
886 | fhdr = fhdr->b_hash_next, i++) { | |
887 | if (BUF_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) | |
888 | return (fhdr); | |
34dc7c2f BB |
889 | } |
890 | ||
2a432414 GW |
891 | hdr->b_hash_next = buf_hash_table.ht_table[idx]; |
892 | buf_hash_table.ht_table[idx] = hdr; | |
893 | hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; | |
34dc7c2f BB |
894 | |
895 | /* collect some hash table performance data */ | |
896 | if (i > 0) { | |
897 | ARCSTAT_BUMP(arcstat_hash_collisions); | |
898 | if (i == 1) | |
899 | ARCSTAT_BUMP(arcstat_hash_chains); | |
900 | ||
901 | ARCSTAT_MAX(arcstat_hash_chain_max, i); | |
902 | } | |
903 | ||
904 | ARCSTAT_BUMP(arcstat_hash_elements); | |
905 | ARCSTAT_MAXSTAT(arcstat_hash_elements); | |
906 | ||
907 | return (NULL); | |
908 | } | |
909 | ||
910 | static void | |
2a432414 | 911 | buf_hash_remove(arc_buf_hdr_t *hdr) |
34dc7c2f | 912 | { |
2a432414 GW |
913 | arc_buf_hdr_t *fhdr, **hdrp; |
914 | uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); | |
34dc7c2f BB |
915 | |
916 | ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); | |
2a432414 | 917 | ASSERT(HDR_IN_HASH_TABLE(hdr)); |
34dc7c2f | 918 | |
2a432414 GW |
919 | hdrp = &buf_hash_table.ht_table[idx]; |
920 | while ((fhdr = *hdrp) != hdr) { | |
921 | ASSERT(fhdr != NULL); | |
922 | hdrp = &fhdr->b_hash_next; | |
34dc7c2f | 923 | } |
2a432414 GW |
924 | *hdrp = hdr->b_hash_next; |
925 | hdr->b_hash_next = NULL; | |
926 | hdr->b_flags &= ~ARC_FLAG_IN_HASH_TABLE; | |
34dc7c2f BB |
927 | |
928 | /* collect some hash table performance data */ | |
929 | ARCSTAT_BUMPDOWN(arcstat_hash_elements); | |
930 | ||
931 | if (buf_hash_table.ht_table[idx] && | |
932 | buf_hash_table.ht_table[idx]->b_hash_next == NULL) | |
933 | ARCSTAT_BUMPDOWN(arcstat_hash_chains); | |
934 | } | |
935 | ||
936 | /* | |
937 | * Global data structures and functions for the buf kmem cache. | |
938 | */ | |
b9541d6b CW |
939 | static kmem_cache_t *hdr_full_cache; |
940 | static kmem_cache_t *hdr_l2only_cache; | |
34dc7c2f BB |
941 | static kmem_cache_t *buf_cache; |
942 | ||
943 | static void | |
944 | buf_fini(void) | |
945 | { | |
946 | int i; | |
947 | ||
00b46022 | 948 | #if defined(_KERNEL) && defined(HAVE_SPL) |
d1d7e268 MK |
949 | /* |
950 | * Large allocations which do not require contiguous pages | |
951 | * should be using vmem_free() in the linux kernel\ | |
952 | */ | |
00b46022 BB |
953 | vmem_free(buf_hash_table.ht_table, |
954 | (buf_hash_table.ht_mask + 1) * sizeof (void *)); | |
955 | #else | |
34dc7c2f BB |
956 | kmem_free(buf_hash_table.ht_table, |
957 | (buf_hash_table.ht_mask + 1) * sizeof (void *)); | |
00b46022 | 958 | #endif |
34dc7c2f BB |
959 | for (i = 0; i < BUF_LOCKS; i++) |
960 | mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); | |
b9541d6b CW |
961 | kmem_cache_destroy(hdr_full_cache); |
962 | kmem_cache_destroy(hdr_l2only_cache); | |
34dc7c2f BB |
963 | kmem_cache_destroy(buf_cache); |
964 | } | |
965 | ||
966 | /* | |
967 | * Constructor callback - called when the cache is empty | |
968 | * and a new buf is requested. | |
969 | */ | |
970 | /* ARGSUSED */ | |
971 | static int | |
b9541d6b CW |
972 | hdr_full_cons(void *vbuf, void *unused, int kmflag) |
973 | { | |
974 | arc_buf_hdr_t *hdr = vbuf; | |
975 | ||
976 | bzero(hdr, HDR_FULL_SIZE); | |
977 | cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); | |
978 | refcount_create(&hdr->b_l1hdr.b_refcnt); | |
979 | mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); | |
980 | list_link_init(&hdr->b_l1hdr.b_arc_node); | |
981 | list_link_init(&hdr->b_l2hdr.b_l2node); | |
ca0bf58d | 982 | multilist_link_init(&hdr->b_l1hdr.b_arc_node); |
b9541d6b CW |
983 | arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); |
984 | ||
985 | return (0); | |
986 | } | |
987 | ||
988 | /* ARGSUSED */ | |
989 | static int | |
990 | hdr_l2only_cons(void *vbuf, void *unused, int kmflag) | |
34dc7c2f | 991 | { |
2a432414 GW |
992 | arc_buf_hdr_t *hdr = vbuf; |
993 | ||
b9541d6b CW |
994 | bzero(hdr, HDR_L2ONLY_SIZE); |
995 | arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); | |
34dc7c2f | 996 | |
34dc7c2f BB |
997 | return (0); |
998 | } | |
999 | ||
b128c09f BB |
1000 | /* ARGSUSED */ |
1001 | static int | |
1002 | buf_cons(void *vbuf, void *unused, int kmflag) | |
1003 | { | |
1004 | arc_buf_t *buf = vbuf; | |
1005 | ||
1006 | bzero(buf, sizeof (arc_buf_t)); | |
428870ff | 1007 | mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL); |
d164b209 BB |
1008 | arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); |
1009 | ||
b128c09f BB |
1010 | return (0); |
1011 | } | |
1012 | ||
34dc7c2f BB |
1013 | /* |
1014 | * Destructor callback - called when a cached buf is | |
1015 | * no longer required. | |
1016 | */ | |
1017 | /* ARGSUSED */ | |
1018 | static void | |
b9541d6b | 1019 | hdr_full_dest(void *vbuf, void *unused) |
34dc7c2f | 1020 | { |
2a432414 | 1021 | arc_buf_hdr_t *hdr = vbuf; |
34dc7c2f | 1022 | |
2a432414 | 1023 | ASSERT(BUF_EMPTY(hdr)); |
b9541d6b CW |
1024 | cv_destroy(&hdr->b_l1hdr.b_cv); |
1025 | refcount_destroy(&hdr->b_l1hdr.b_refcnt); | |
1026 | mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); | |
ca0bf58d | 1027 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b CW |
1028 | arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); |
1029 | } | |
1030 | ||
1031 | /* ARGSUSED */ | |
1032 | static void | |
1033 | hdr_l2only_dest(void *vbuf, void *unused) | |
1034 | { | |
1035 | ASSERTV(arc_buf_hdr_t *hdr = vbuf); | |
1036 | ||
1037 | ASSERT(BUF_EMPTY(hdr)); | |
1038 | arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); | |
34dc7c2f BB |
1039 | } |
1040 | ||
b128c09f BB |
1041 | /* ARGSUSED */ |
1042 | static void | |
1043 | buf_dest(void *vbuf, void *unused) | |
1044 | { | |
1045 | arc_buf_t *buf = vbuf; | |
1046 | ||
428870ff | 1047 | mutex_destroy(&buf->b_evict_lock); |
d164b209 | 1048 | arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); |
b128c09f BB |
1049 | } |
1050 | ||
34dc7c2f BB |
1051 | static void |
1052 | buf_init(void) | |
1053 | { | |
1054 | uint64_t *ct; | |
1055 | uint64_t hsize = 1ULL << 12; | |
1056 | int i, j; | |
1057 | ||
1058 | /* | |
1059 | * The hash table is big enough to fill all of physical memory | |
49ddb315 MA |
1060 | * with an average block size of zfs_arc_average_blocksize (default 8K). |
1061 | * By default, the table will take up | |
1062 | * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). | |
34dc7c2f | 1063 | */ |
49ddb315 | 1064 | while (hsize * zfs_arc_average_blocksize < physmem * PAGESIZE) |
34dc7c2f BB |
1065 | hsize <<= 1; |
1066 | retry: | |
1067 | buf_hash_table.ht_mask = hsize - 1; | |
00b46022 | 1068 | #if defined(_KERNEL) && defined(HAVE_SPL) |
d1d7e268 MK |
1069 | /* |
1070 | * Large allocations which do not require contiguous pages | |
1071 | * should be using vmem_alloc() in the linux kernel | |
1072 | */ | |
00b46022 BB |
1073 | buf_hash_table.ht_table = |
1074 | vmem_zalloc(hsize * sizeof (void*), KM_SLEEP); | |
1075 | #else | |
34dc7c2f BB |
1076 | buf_hash_table.ht_table = |
1077 | kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); | |
00b46022 | 1078 | #endif |
34dc7c2f BB |
1079 | if (buf_hash_table.ht_table == NULL) { |
1080 | ASSERT(hsize > (1ULL << 8)); | |
1081 | hsize >>= 1; | |
1082 | goto retry; | |
1083 | } | |
1084 | ||
b9541d6b CW |
1085 | hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, |
1086 | 0, hdr_full_cons, hdr_full_dest, NULL, NULL, NULL, 0); | |
1087 | hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", | |
1088 | HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, NULL, | |
1089 | NULL, NULL, 0); | |
34dc7c2f | 1090 | buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), |
b128c09f | 1091 | 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); |
34dc7c2f BB |
1092 | |
1093 | for (i = 0; i < 256; i++) | |
1094 | for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) | |
1095 | *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); | |
1096 | ||
1097 | for (i = 0; i < BUF_LOCKS; i++) { | |
1098 | mutex_init(&buf_hash_table.ht_locks[i].ht_lock, | |
40d06e3c | 1099 | NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
1100 | } |
1101 | } | |
1102 | ||
b9541d6b CW |
1103 | /* |
1104 | * Transition between the two allocation states for the arc_buf_hdr struct. | |
1105 | * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without | |
1106 | * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller | |
1107 | * version is used when a cache buffer is only in the L2ARC in order to reduce | |
1108 | * memory usage. | |
1109 | */ | |
1110 | static arc_buf_hdr_t * | |
1111 | arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) | |
1112 | { | |
1113 | arc_buf_hdr_t *nhdr; | |
1114 | l2arc_dev_t *dev; | |
1115 | ||
1116 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
1117 | ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || | |
1118 | (old == hdr_l2only_cache && new == hdr_full_cache)); | |
1119 | ||
1120 | dev = hdr->b_l2hdr.b_dev; | |
1121 | nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); | |
1122 | ||
1123 | ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); | |
1124 | buf_hash_remove(hdr); | |
1125 | ||
1126 | bcopy(hdr, nhdr, HDR_L2ONLY_SIZE); | |
d962d5da | 1127 | |
b9541d6b CW |
1128 | if (new == hdr_full_cache) { |
1129 | nhdr->b_flags |= ARC_FLAG_HAS_L1HDR; | |
1130 | /* | |
1131 | * arc_access and arc_change_state need to be aware that a | |
1132 | * header has just come out of L2ARC, so we set its state to | |
1133 | * l2c_only even though it's about to change. | |
1134 | */ | |
1135 | nhdr->b_l1hdr.b_state = arc_l2c_only; | |
ca0bf58d PS |
1136 | |
1137 | /* Verify previous threads set to NULL before freeing */ | |
1138 | ASSERT3P(nhdr->b_l1hdr.b_tmp_cdata, ==, NULL); | |
b9541d6b CW |
1139 | } else { |
1140 | ASSERT(hdr->b_l1hdr.b_buf == NULL); | |
1141 | ASSERT0(hdr->b_l1hdr.b_datacnt); | |
ca0bf58d PS |
1142 | |
1143 | /* | |
1144 | * If we've reached here, We must have been called from | |
1145 | * arc_evict_hdr(), as such we should have already been | |
1146 | * removed from any ghost list we were previously on | |
1147 | * (which protects us from racing with arc_evict_state), | |
1148 | * thus no locking is needed during this check. | |
1149 | */ | |
1150 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
1151 | ||
b9541d6b | 1152 | /* |
ca0bf58d PS |
1153 | * A buffer must not be moved into the arc_l2c_only |
1154 | * state if it's not finished being written out to the | |
1155 | * l2arc device. Otherwise, the b_l1hdr.b_tmp_cdata field | |
1156 | * might try to be accessed, even though it was removed. | |
b9541d6b | 1157 | */ |
ca0bf58d PS |
1158 | VERIFY(!HDR_L2_WRITING(hdr)); |
1159 | VERIFY3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); | |
1160 | ||
b9541d6b CW |
1161 | nhdr->b_flags &= ~ARC_FLAG_HAS_L1HDR; |
1162 | } | |
1163 | /* | |
1164 | * The header has been reallocated so we need to re-insert it into any | |
1165 | * lists it was on. | |
1166 | */ | |
1167 | (void) buf_hash_insert(nhdr, NULL); | |
1168 | ||
1169 | ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); | |
1170 | ||
1171 | mutex_enter(&dev->l2ad_mtx); | |
1172 | ||
1173 | /* | |
1174 | * We must place the realloc'ed header back into the list at | |
1175 | * the same spot. Otherwise, if it's placed earlier in the list, | |
1176 | * l2arc_write_buffers() could find it during the function's | |
1177 | * write phase, and try to write it out to the l2arc. | |
1178 | */ | |
1179 | list_insert_after(&dev->l2ad_buflist, hdr, nhdr); | |
1180 | list_remove(&dev->l2ad_buflist, hdr); | |
1181 | ||
1182 | mutex_exit(&dev->l2ad_mtx); | |
1183 | ||
d962d5da PS |
1184 | /* |
1185 | * Since we're using the pointer address as the tag when | |
1186 | * incrementing and decrementing the l2ad_alloc refcount, we | |
1187 | * must remove the old pointer (that we're about to destroy) and | |
1188 | * add the new pointer to the refcount. Otherwise we'd remove | |
1189 | * the wrong pointer address when calling arc_hdr_destroy() later. | |
1190 | */ | |
1191 | ||
1192 | (void) refcount_remove_many(&dev->l2ad_alloc, | |
1193 | hdr->b_l2hdr.b_asize, hdr); | |
1194 | ||
1195 | (void) refcount_add_many(&dev->l2ad_alloc, | |
1196 | nhdr->b_l2hdr.b_asize, nhdr); | |
1197 | ||
b9541d6b CW |
1198 | buf_discard_identity(hdr); |
1199 | hdr->b_freeze_cksum = NULL; | |
1200 | kmem_cache_free(old, hdr); | |
1201 | ||
1202 | return (nhdr); | |
1203 | } | |
1204 | ||
1205 | ||
34dc7c2f BB |
1206 | #define ARC_MINTIME (hz>>4) /* 62 ms */ |
1207 | ||
1208 | static void | |
1209 | arc_cksum_verify(arc_buf_t *buf) | |
1210 | { | |
1211 | zio_cksum_t zc; | |
1212 | ||
1213 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) | |
1214 | return; | |
1215 | ||
b9541d6b CW |
1216 | mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
1217 | if (buf->b_hdr->b_freeze_cksum == NULL || HDR_IO_ERROR(buf->b_hdr)) { | |
1218 | mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); | |
34dc7c2f BB |
1219 | return; |
1220 | } | |
1221 | fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); | |
1222 | if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc)) | |
1223 | panic("buffer modified while frozen!"); | |
b9541d6b | 1224 | mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1225 | } |
1226 | ||
1227 | static int | |
1228 | arc_cksum_equal(arc_buf_t *buf) | |
1229 | { | |
1230 | zio_cksum_t zc; | |
1231 | int equal; | |
1232 | ||
b9541d6b | 1233 | mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1234 | fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc); |
1235 | equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc); | |
b9541d6b | 1236 | mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1237 | |
1238 | return (equal); | |
1239 | } | |
1240 | ||
1241 | static void | |
1242 | arc_cksum_compute(arc_buf_t *buf, boolean_t force) | |
1243 | { | |
1244 | if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY)) | |
1245 | return; | |
1246 | ||
b9541d6b | 1247 | mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f | 1248 | if (buf->b_hdr->b_freeze_cksum != NULL) { |
b9541d6b | 1249 | mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1250 | return; |
1251 | } | |
409dc1a5 | 1252 | buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), |
79c76d5b | 1253 | KM_SLEEP); |
34dc7c2f BB |
1254 | fletcher_2_native(buf->b_data, buf->b_hdr->b_size, |
1255 | buf->b_hdr->b_freeze_cksum); | |
b9541d6b | 1256 | mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
498877ba MA |
1257 | arc_buf_watch(buf); |
1258 | } | |
1259 | ||
1260 | #ifndef _KERNEL | |
1261 | void | |
1262 | arc_buf_sigsegv(int sig, siginfo_t *si, void *unused) | |
1263 | { | |
1264 | panic("Got SIGSEGV at address: 0x%lx\n", (long) si->si_addr); | |
1265 | } | |
1266 | #endif | |
1267 | ||
1268 | /* ARGSUSED */ | |
1269 | static void | |
1270 | arc_buf_unwatch(arc_buf_t *buf) | |
1271 | { | |
1272 | #ifndef _KERNEL | |
1273 | if (arc_watch) { | |
1274 | ASSERT0(mprotect(buf->b_data, buf->b_hdr->b_size, | |
1275 | PROT_READ | PROT_WRITE)); | |
1276 | } | |
1277 | #endif | |
1278 | } | |
1279 | ||
1280 | /* ARGSUSED */ | |
1281 | static void | |
1282 | arc_buf_watch(arc_buf_t *buf) | |
1283 | { | |
1284 | #ifndef _KERNEL | |
1285 | if (arc_watch) | |
1286 | ASSERT0(mprotect(buf->b_data, buf->b_hdr->b_size, PROT_READ)); | |
1287 | #endif | |
34dc7c2f BB |
1288 | } |
1289 | ||
b9541d6b CW |
1290 | static arc_buf_contents_t |
1291 | arc_buf_type(arc_buf_hdr_t *hdr) | |
1292 | { | |
1293 | if (HDR_ISTYPE_METADATA(hdr)) { | |
1294 | return (ARC_BUFC_METADATA); | |
1295 | } else { | |
1296 | return (ARC_BUFC_DATA); | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | static uint32_t | |
1301 | arc_bufc_to_flags(arc_buf_contents_t type) | |
1302 | { | |
1303 | switch (type) { | |
1304 | case ARC_BUFC_DATA: | |
1305 | /* metadata field is 0 if buffer contains normal data */ | |
1306 | return (0); | |
1307 | case ARC_BUFC_METADATA: | |
1308 | return (ARC_FLAG_BUFC_METADATA); | |
1309 | default: | |
1310 | break; | |
1311 | } | |
1312 | panic("undefined ARC buffer type!"); | |
1313 | return ((uint32_t)-1); | |
1314 | } | |
1315 | ||
34dc7c2f BB |
1316 | void |
1317 | arc_buf_thaw(arc_buf_t *buf) | |
1318 | { | |
1319 | if (zfs_flags & ZFS_DEBUG_MODIFY) { | |
b9541d6b | 1320 | if (buf->b_hdr->b_l1hdr.b_state != arc_anon) |
34dc7c2f | 1321 | panic("modifying non-anon buffer!"); |
b9541d6b | 1322 | if (HDR_IO_IN_PROGRESS(buf->b_hdr)) |
34dc7c2f BB |
1323 | panic("modifying buffer while i/o in progress!"); |
1324 | arc_cksum_verify(buf); | |
1325 | } | |
1326 | ||
b9541d6b | 1327 | mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
1328 | if (buf->b_hdr->b_freeze_cksum != NULL) { |
1329 | kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t)); | |
1330 | buf->b_hdr->b_freeze_cksum = NULL; | |
1331 | } | |
428870ff | 1332 | |
b9541d6b | 1333 | mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock); |
498877ba MA |
1334 | |
1335 | arc_buf_unwatch(buf); | |
34dc7c2f BB |
1336 | } |
1337 | ||
1338 | void | |
1339 | arc_buf_freeze(arc_buf_t *buf) | |
1340 | { | |
428870ff BB |
1341 | kmutex_t *hash_lock; |
1342 | ||
34dc7c2f BB |
1343 | if (!(zfs_flags & ZFS_DEBUG_MODIFY)) |
1344 | return; | |
1345 | ||
428870ff BB |
1346 | hash_lock = HDR_LOCK(buf->b_hdr); |
1347 | mutex_enter(hash_lock); | |
1348 | ||
34dc7c2f | 1349 | ASSERT(buf->b_hdr->b_freeze_cksum != NULL || |
b9541d6b | 1350 | buf->b_hdr->b_l1hdr.b_state == arc_anon); |
34dc7c2f | 1351 | arc_cksum_compute(buf, B_FALSE); |
428870ff | 1352 | mutex_exit(hash_lock); |
498877ba | 1353 | |
34dc7c2f BB |
1354 | } |
1355 | ||
1356 | static void | |
2a432414 | 1357 | add_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) |
34dc7c2f | 1358 | { |
b9541d6b CW |
1359 | arc_state_t *state; |
1360 | ||
1361 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
34dc7c2f BB |
1362 | ASSERT(MUTEX_HELD(hash_lock)); |
1363 | ||
b9541d6b CW |
1364 | state = hdr->b_l1hdr.b_state; |
1365 | ||
1366 | if ((refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && | |
1367 | (state != arc_anon)) { | |
1368 | /* We don't use the L2-only state list. */ | |
1369 | if (state != arc_l2c_only) { | |
ca0bf58d | 1370 | arc_buf_contents_t type = arc_buf_type(hdr); |
b9541d6b | 1371 | uint64_t delta = hdr->b_size * hdr->b_l1hdr.b_datacnt; |
ca0bf58d PS |
1372 | multilist_t *list = &state->arcs_list[type]; |
1373 | uint64_t *size = &state->arcs_lsize[type]; | |
1374 | ||
1375 | multilist_remove(list, hdr); | |
b9541d6b | 1376 | |
b9541d6b CW |
1377 | if (GHOST_STATE(state)) { |
1378 | ASSERT0(hdr->b_l1hdr.b_datacnt); | |
1379 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); | |
1380 | delta = hdr->b_size; | |
1381 | } | |
1382 | ASSERT(delta > 0); | |
1383 | ASSERT3U(*size, >=, delta); | |
1384 | atomic_add_64(size, -delta); | |
34dc7c2f | 1385 | } |
b128c09f | 1386 | /* remove the prefetch flag if we get a reference */ |
b9541d6b | 1387 | hdr->b_flags &= ~ARC_FLAG_PREFETCH; |
34dc7c2f BB |
1388 | } |
1389 | } | |
1390 | ||
1391 | static int | |
2a432414 | 1392 | remove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag) |
34dc7c2f BB |
1393 | { |
1394 | int cnt; | |
b9541d6b | 1395 | arc_state_t *state = hdr->b_l1hdr.b_state; |
34dc7c2f | 1396 | |
b9541d6b | 1397 | ASSERT(HDR_HAS_L1HDR(hdr)); |
34dc7c2f BB |
1398 | ASSERT(state == arc_anon || MUTEX_HELD(hash_lock)); |
1399 | ASSERT(!GHOST_STATE(state)); | |
1400 | ||
b9541d6b CW |
1401 | /* |
1402 | * arc_l2c_only counts as a ghost state so we don't need to explicitly | |
1403 | * check to prevent usage of the arc_l2c_only list. | |
1404 | */ | |
1405 | if (((cnt = refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) && | |
34dc7c2f | 1406 | (state != arc_anon)) { |
ca0bf58d PS |
1407 | arc_buf_contents_t type = arc_buf_type(hdr); |
1408 | multilist_t *list = &state->arcs_list[type]; | |
1409 | uint64_t *size = &state->arcs_lsize[type]; | |
1410 | ||
1411 | multilist_insert(list, hdr); | |
34dc7c2f | 1412 | |
b9541d6b CW |
1413 | ASSERT(hdr->b_l1hdr.b_datacnt > 0); |
1414 | atomic_add_64(size, hdr->b_size * | |
1415 | hdr->b_l1hdr.b_datacnt); | |
34dc7c2f BB |
1416 | } |
1417 | return (cnt); | |
1418 | } | |
1419 | ||
e0b0ca98 BB |
1420 | /* |
1421 | * Returns detailed information about a specific arc buffer. When the | |
1422 | * state_index argument is set the function will calculate the arc header | |
1423 | * list position for its arc state. Since this requires a linear traversal | |
1424 | * callers are strongly encourage not to do this. However, it can be helpful | |
1425 | * for targeted analysis so the functionality is provided. | |
1426 | */ | |
1427 | void | |
1428 | arc_buf_info(arc_buf_t *ab, arc_buf_info_t *abi, int state_index) | |
1429 | { | |
1430 | arc_buf_hdr_t *hdr = ab->b_hdr; | |
b9541d6b CW |
1431 | l1arc_buf_hdr_t *l1hdr = NULL; |
1432 | l2arc_buf_hdr_t *l2hdr = NULL; | |
1433 | arc_state_t *state = NULL; | |
1434 | ||
1435 | if (HDR_HAS_L1HDR(hdr)) { | |
1436 | l1hdr = &hdr->b_l1hdr; | |
1437 | state = l1hdr->b_state; | |
1438 | } | |
1439 | if (HDR_HAS_L2HDR(hdr)) | |
1440 | l2hdr = &hdr->b_l2hdr; | |
e0b0ca98 | 1441 | |
d1d7e268 | 1442 | memset(abi, 0, sizeof (arc_buf_info_t)); |
e0b0ca98 | 1443 | abi->abi_flags = hdr->b_flags; |
b9541d6b CW |
1444 | |
1445 | if (l1hdr) { | |
1446 | abi->abi_datacnt = l1hdr->b_datacnt; | |
1447 | abi->abi_access = l1hdr->b_arc_access; | |
1448 | abi->abi_mru_hits = l1hdr->b_mru_hits; | |
1449 | abi->abi_mru_ghost_hits = l1hdr->b_mru_ghost_hits; | |
1450 | abi->abi_mfu_hits = l1hdr->b_mfu_hits; | |
1451 | abi->abi_mfu_ghost_hits = l1hdr->b_mfu_ghost_hits; | |
1452 | abi->abi_holds = refcount_count(&l1hdr->b_refcnt); | |
1453 | } | |
1454 | ||
1455 | if (l2hdr) { | |
1456 | abi->abi_l2arc_dattr = l2hdr->b_daddr; | |
1457 | abi->abi_l2arc_asize = l2hdr->b_asize; | |
1458 | abi->abi_l2arc_compress = HDR_GET_COMPRESS(hdr); | |
1459 | abi->abi_l2arc_hits = l2hdr->b_hits; | |
1460 | } | |
1461 | ||
e0b0ca98 | 1462 | abi->abi_state_type = state ? state->arcs_state : ARC_STATE_ANON; |
b9541d6b | 1463 | abi->abi_state_contents = arc_buf_type(hdr); |
e0b0ca98 | 1464 | abi->abi_size = hdr->b_size; |
e0b0ca98 BB |
1465 | } |
1466 | ||
34dc7c2f | 1467 | /* |
ca0bf58d | 1468 | * Move the supplied buffer to the indicated state. The hash lock |
34dc7c2f BB |
1469 | * for the buffer must be held by the caller. |
1470 | */ | |
1471 | static void | |
2a432414 GW |
1472 | arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr, |
1473 | kmutex_t *hash_lock) | |
34dc7c2f | 1474 | { |
b9541d6b CW |
1475 | arc_state_t *old_state; |
1476 | int64_t refcnt; | |
1477 | uint32_t datacnt; | |
34dc7c2f | 1478 | uint64_t from_delta, to_delta; |
b9541d6b CW |
1479 | arc_buf_contents_t buftype = arc_buf_type(hdr); |
1480 | ||
1481 | /* | |
1482 | * We almost always have an L1 hdr here, since we call arc_hdr_realloc() | |
1483 | * in arc_read() when bringing a buffer out of the L2ARC. However, the | |
1484 | * L1 hdr doesn't always exist when we change state to arc_anon before | |
1485 | * destroying a header, in which case reallocating to add the L1 hdr is | |
1486 | * pointless. | |
1487 | */ | |
1488 | if (HDR_HAS_L1HDR(hdr)) { | |
1489 | old_state = hdr->b_l1hdr.b_state; | |
1490 | refcnt = refcount_count(&hdr->b_l1hdr.b_refcnt); | |
1491 | datacnt = hdr->b_l1hdr.b_datacnt; | |
1492 | } else { | |
1493 | old_state = arc_l2c_only; | |
1494 | refcnt = 0; | |
1495 | datacnt = 0; | |
1496 | } | |
34dc7c2f BB |
1497 | |
1498 | ASSERT(MUTEX_HELD(hash_lock)); | |
e8b96c60 | 1499 | ASSERT3P(new_state, !=, old_state); |
b9541d6b CW |
1500 | ASSERT(refcnt == 0 || datacnt > 0); |
1501 | ASSERT(!GHOST_STATE(new_state) || datacnt == 0); | |
1502 | ASSERT(old_state != arc_anon || datacnt <= 1); | |
34dc7c2f | 1503 | |
b9541d6b | 1504 | from_delta = to_delta = datacnt * hdr->b_size; |
34dc7c2f BB |
1505 | |
1506 | /* | |
1507 | * If this buffer is evictable, transfer it from the | |
1508 | * old state list to the new state list. | |
1509 | */ | |
1510 | if (refcnt == 0) { | |
b9541d6b | 1511 | if (old_state != arc_anon && old_state != arc_l2c_only) { |
b9541d6b | 1512 | uint64_t *size = &old_state->arcs_lsize[buftype]; |
34dc7c2f | 1513 | |
b9541d6b | 1514 | ASSERT(HDR_HAS_L1HDR(hdr)); |
ca0bf58d | 1515 | multilist_remove(&old_state->arcs_list[buftype], hdr); |
34dc7c2f BB |
1516 | |
1517 | /* | |
1518 | * If prefetching out of the ghost cache, | |
428870ff | 1519 | * we will have a non-zero datacnt. |
34dc7c2f | 1520 | */ |
b9541d6b | 1521 | if (GHOST_STATE(old_state) && datacnt == 0) { |
34dc7c2f | 1522 | /* ghost elements have a ghost size */ |
b9541d6b | 1523 | ASSERT(hdr->b_l1hdr.b_buf == NULL); |
2a432414 | 1524 | from_delta = hdr->b_size; |
34dc7c2f BB |
1525 | } |
1526 | ASSERT3U(*size, >=, from_delta); | |
1527 | atomic_add_64(size, -from_delta); | |
34dc7c2f | 1528 | } |
b9541d6b | 1529 | if (new_state != arc_anon && new_state != arc_l2c_only) { |
b9541d6b | 1530 | uint64_t *size = &new_state->arcs_lsize[buftype]; |
34dc7c2f | 1531 | |
b9541d6b CW |
1532 | /* |
1533 | * An L1 header always exists here, since if we're | |
1534 | * moving to some L1-cached state (i.e. not l2c_only or | |
1535 | * anonymous), we realloc the header to add an L1hdr | |
1536 | * beforehand. | |
1537 | */ | |
1538 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
ca0bf58d | 1539 | multilist_insert(&new_state->arcs_list[buftype], hdr); |
34dc7c2f BB |
1540 | |
1541 | /* ghost elements have a ghost size */ | |
1542 | if (GHOST_STATE(new_state)) { | |
b9541d6b CW |
1543 | ASSERT0(datacnt); |
1544 | ASSERT(hdr->b_l1hdr.b_buf == NULL); | |
2a432414 | 1545 | to_delta = hdr->b_size; |
34dc7c2f BB |
1546 | } |
1547 | atomic_add_64(size, to_delta); | |
34dc7c2f BB |
1548 | } |
1549 | } | |
1550 | ||
2a432414 GW |
1551 | ASSERT(!BUF_EMPTY(hdr)); |
1552 | if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) | |
1553 | buf_hash_remove(hdr); | |
34dc7c2f | 1554 | |
b9541d6b CW |
1555 | /* adjust state sizes (ignore arc_l2c_only) */ |
1556 | if (to_delta && new_state != arc_l2c_only) | |
34dc7c2f | 1557 | atomic_add_64(&new_state->arcs_size, to_delta); |
b9541d6b | 1558 | if (from_delta && old_state != arc_l2c_only) { |
34dc7c2f BB |
1559 | ASSERT3U(old_state->arcs_size, >=, from_delta); |
1560 | atomic_add_64(&old_state->arcs_size, -from_delta); | |
1561 | } | |
b9541d6b CW |
1562 | if (HDR_HAS_L1HDR(hdr)) |
1563 | hdr->b_l1hdr.b_state = new_state; | |
34dc7c2f | 1564 | |
b9541d6b CW |
1565 | /* |
1566 | * L2 headers should never be on the L2 state list since they don't | |
1567 | * have L1 headers allocated. | |
1568 | */ | |
ca0bf58d PS |
1569 | ASSERT(multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]) && |
1570 | multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA])); | |
34dc7c2f BB |
1571 | } |
1572 | ||
1573 | void | |
d164b209 | 1574 | arc_space_consume(uint64_t space, arc_space_type_t type) |
34dc7c2f | 1575 | { |
d164b209 BB |
1576 | ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); |
1577 | ||
1578 | switch (type) { | |
e75c13c3 BB |
1579 | default: |
1580 | break; | |
d164b209 BB |
1581 | case ARC_SPACE_DATA: |
1582 | ARCSTAT_INCR(arcstat_data_size, space); | |
1583 | break; | |
cc7f677c | 1584 | case ARC_SPACE_META: |
500445c0 | 1585 | ARCSTAT_INCR(arcstat_metadata_size, space); |
cc7f677c | 1586 | break; |
d164b209 BB |
1587 | case ARC_SPACE_OTHER: |
1588 | ARCSTAT_INCR(arcstat_other_size, space); | |
1589 | break; | |
1590 | case ARC_SPACE_HDRS: | |
1591 | ARCSTAT_INCR(arcstat_hdr_size, space); | |
1592 | break; | |
1593 | case ARC_SPACE_L2HDRS: | |
1594 | ARCSTAT_INCR(arcstat_l2_hdr_size, space); | |
1595 | break; | |
1596 | } | |
1597 | ||
500445c0 | 1598 | if (type != ARC_SPACE_DATA) |
cc7f677c PS |
1599 | ARCSTAT_INCR(arcstat_meta_used, space); |
1600 | ||
34dc7c2f BB |
1601 | atomic_add_64(&arc_size, space); |
1602 | } | |
1603 | ||
1604 | void | |
d164b209 | 1605 | arc_space_return(uint64_t space, arc_space_type_t type) |
34dc7c2f | 1606 | { |
d164b209 BB |
1607 | ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); |
1608 | ||
1609 | switch (type) { | |
e75c13c3 BB |
1610 | default: |
1611 | break; | |
d164b209 BB |
1612 | case ARC_SPACE_DATA: |
1613 | ARCSTAT_INCR(arcstat_data_size, -space); | |
1614 | break; | |
cc7f677c | 1615 | case ARC_SPACE_META: |
500445c0 | 1616 | ARCSTAT_INCR(arcstat_metadata_size, -space); |
cc7f677c | 1617 | break; |
d164b209 BB |
1618 | case ARC_SPACE_OTHER: |
1619 | ARCSTAT_INCR(arcstat_other_size, -space); | |
1620 | break; | |
1621 | case ARC_SPACE_HDRS: | |
1622 | ARCSTAT_INCR(arcstat_hdr_size, -space); | |
1623 | break; | |
1624 | case ARC_SPACE_L2HDRS: | |
1625 | ARCSTAT_INCR(arcstat_l2_hdr_size, -space); | |
1626 | break; | |
1627 | } | |
1628 | ||
cc7f677c PS |
1629 | if (type != ARC_SPACE_DATA) { |
1630 | ASSERT(arc_meta_used >= space); | |
500445c0 PS |
1631 | if (arc_meta_max < arc_meta_used) |
1632 | arc_meta_max = arc_meta_used; | |
cc7f677c PS |
1633 | ARCSTAT_INCR(arcstat_meta_used, -space); |
1634 | } | |
1635 | ||
34dc7c2f BB |
1636 | ASSERT(arc_size >= space); |
1637 | atomic_add_64(&arc_size, -space); | |
1638 | } | |
1639 | ||
34dc7c2f | 1640 | arc_buf_t * |
5f6d0b6f | 1641 | arc_buf_alloc(spa_t *spa, uint64_t size, void *tag, arc_buf_contents_t type) |
34dc7c2f BB |
1642 | { |
1643 | arc_buf_hdr_t *hdr; | |
1644 | arc_buf_t *buf; | |
1645 | ||
f1512ee6 | 1646 | VERIFY3U(size, <=, spa_maxblocksize(spa)); |
b9541d6b | 1647 | hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); |
34dc7c2f | 1648 | ASSERT(BUF_EMPTY(hdr)); |
b9541d6b | 1649 | ASSERT3P(hdr->b_freeze_cksum, ==, NULL); |
34dc7c2f | 1650 | hdr->b_size = size; |
3541dc6d | 1651 | hdr->b_spa = spa_load_guid(spa); |
b9541d6b CW |
1652 | hdr->b_l1hdr.b_mru_hits = 0; |
1653 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
1654 | hdr->b_l1hdr.b_mfu_hits = 0; | |
1655 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
1656 | hdr->b_l1hdr.b_l2_hits = 0; | |
1657 | ||
34dc7c2f BB |
1658 | buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); |
1659 | buf->b_hdr = hdr; | |
1660 | buf->b_data = NULL; | |
1661 | buf->b_efunc = NULL; | |
1662 | buf->b_private = NULL; | |
1663 | buf->b_next = NULL; | |
b9541d6b CW |
1664 | |
1665 | hdr->b_flags = arc_bufc_to_flags(type); | |
1666 | hdr->b_flags |= ARC_FLAG_HAS_L1HDR; | |
1667 | ||
1668 | hdr->b_l1hdr.b_buf = buf; | |
1669 | hdr->b_l1hdr.b_state = arc_anon; | |
1670 | hdr->b_l1hdr.b_arc_access = 0; | |
1671 | hdr->b_l1hdr.b_datacnt = 1; | |
ca0bf58d | 1672 | hdr->b_l1hdr.b_tmp_cdata = NULL; |
b9541d6b | 1673 | |
34dc7c2f | 1674 | arc_get_data_buf(buf); |
b9541d6b CW |
1675 | |
1676 | ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); | |
1677 | (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); | |
34dc7c2f BB |
1678 | |
1679 | return (buf); | |
1680 | } | |
1681 | ||
9babb374 BB |
1682 | static char *arc_onloan_tag = "onloan"; |
1683 | ||
1684 | /* | |
1685 | * Loan out an anonymous arc buffer. Loaned buffers are not counted as in | |
1686 | * flight data by arc_tempreserve_space() until they are "returned". Loaned | |
1687 | * buffers must be returned to the arc before they can be used by the DMU or | |
1688 | * freed. | |
1689 | */ | |
1690 | arc_buf_t * | |
5f6d0b6f | 1691 | arc_loan_buf(spa_t *spa, uint64_t size) |
9babb374 BB |
1692 | { |
1693 | arc_buf_t *buf; | |
1694 | ||
1695 | buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA); | |
1696 | ||
1697 | atomic_add_64(&arc_loaned_bytes, size); | |
1698 | return (buf); | |
1699 | } | |
1700 | ||
1701 | /* | |
1702 | * Return a loaned arc buffer to the arc. | |
1703 | */ | |
1704 | void | |
1705 | arc_return_buf(arc_buf_t *buf, void *tag) | |
1706 | { | |
1707 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
1708 | ||
9babb374 | 1709 | ASSERT(buf->b_data != NULL); |
b9541d6b CW |
1710 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1711 | (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag); | |
1712 | (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); | |
9babb374 BB |
1713 | |
1714 | atomic_add_64(&arc_loaned_bytes, -hdr->b_size); | |
1715 | } | |
1716 | ||
428870ff BB |
1717 | /* Detach an arc_buf from a dbuf (tag) */ |
1718 | void | |
1719 | arc_loan_inuse_buf(arc_buf_t *buf, void *tag) | |
1720 | { | |
b9541d6b | 1721 | arc_buf_hdr_t *hdr = buf->b_hdr; |
428870ff BB |
1722 | |
1723 | ASSERT(buf->b_data != NULL); | |
b9541d6b CW |
1724 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1725 | (void) refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); | |
1726 | (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); | |
428870ff BB |
1727 | buf->b_efunc = NULL; |
1728 | buf->b_private = NULL; | |
1729 | ||
1730 | atomic_add_64(&arc_loaned_bytes, hdr->b_size); | |
1731 | } | |
1732 | ||
34dc7c2f BB |
1733 | static arc_buf_t * |
1734 | arc_buf_clone(arc_buf_t *from) | |
1735 | { | |
1736 | arc_buf_t *buf; | |
1737 | arc_buf_hdr_t *hdr = from->b_hdr; | |
1738 | uint64_t size = hdr->b_size; | |
1739 | ||
b9541d6b CW |
1740 | ASSERT(HDR_HAS_L1HDR(hdr)); |
1741 | ASSERT(hdr->b_l1hdr.b_state != arc_anon); | |
428870ff | 1742 | |
34dc7c2f BB |
1743 | buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); |
1744 | buf->b_hdr = hdr; | |
1745 | buf->b_data = NULL; | |
1746 | buf->b_efunc = NULL; | |
1747 | buf->b_private = NULL; | |
b9541d6b CW |
1748 | buf->b_next = hdr->b_l1hdr.b_buf; |
1749 | hdr->b_l1hdr.b_buf = buf; | |
34dc7c2f BB |
1750 | arc_get_data_buf(buf); |
1751 | bcopy(from->b_data, buf->b_data, size); | |
1eb5bfa3 GW |
1752 | |
1753 | /* | |
1754 | * This buffer already exists in the arc so create a duplicate | |
1755 | * copy for the caller. If the buffer is associated with user data | |
1756 | * then track the size and number of duplicates. These stats will be | |
1757 | * updated as duplicate buffers are created and destroyed. | |
1758 | */ | |
b9541d6b | 1759 | if (HDR_ISTYPE_DATA(hdr)) { |
1eb5bfa3 GW |
1760 | ARCSTAT_BUMP(arcstat_duplicate_buffers); |
1761 | ARCSTAT_INCR(arcstat_duplicate_buffers_size, size); | |
1762 | } | |
b9541d6b | 1763 | hdr->b_l1hdr.b_datacnt += 1; |
34dc7c2f BB |
1764 | return (buf); |
1765 | } | |
1766 | ||
1767 | void | |
1768 | arc_buf_add_ref(arc_buf_t *buf, void* tag) | |
1769 | { | |
1770 | arc_buf_hdr_t *hdr; | |
1771 | kmutex_t *hash_lock; | |
1772 | ||
1773 | /* | |
b128c09f BB |
1774 | * Check to see if this buffer is evicted. Callers |
1775 | * must verify b_data != NULL to know if the add_ref | |
1776 | * was successful. | |
34dc7c2f | 1777 | */ |
428870ff | 1778 | mutex_enter(&buf->b_evict_lock); |
b128c09f | 1779 | if (buf->b_data == NULL) { |
428870ff | 1780 | mutex_exit(&buf->b_evict_lock); |
34dc7c2f BB |
1781 | return; |
1782 | } | |
428870ff | 1783 | hash_lock = HDR_LOCK(buf->b_hdr); |
34dc7c2f | 1784 | mutex_enter(hash_lock); |
428870ff | 1785 | hdr = buf->b_hdr; |
b9541d6b | 1786 | ASSERT(HDR_HAS_L1HDR(hdr)); |
428870ff BB |
1787 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
1788 | mutex_exit(&buf->b_evict_lock); | |
34dc7c2f | 1789 | |
b9541d6b CW |
1790 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || |
1791 | hdr->b_l1hdr.b_state == arc_mfu); | |
1792 | ||
34dc7c2f | 1793 | add_reference(hdr, hash_lock, tag); |
d164b209 | 1794 | DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); |
34dc7c2f BB |
1795 | arc_access(hdr, hash_lock); |
1796 | mutex_exit(hash_lock); | |
1797 | ARCSTAT_BUMP(arcstat_hits); | |
b9541d6b CW |
1798 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), |
1799 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), | |
34dc7c2f BB |
1800 | data, metadata, hits); |
1801 | } | |
1802 | ||
ca0bf58d PS |
1803 | static void |
1804 | arc_buf_free_on_write(void *data, size_t size, | |
1805 | void (*free_func)(void *, size_t)) | |
1806 | { | |
1807 | l2arc_data_free_t *df; | |
1808 | ||
1809 | df = kmem_alloc(sizeof (*df), KM_SLEEP); | |
1810 | df->l2df_data = data; | |
1811 | df->l2df_size = size; | |
1812 | df->l2df_func = free_func; | |
1813 | mutex_enter(&l2arc_free_on_write_mtx); | |
1814 | list_insert_head(l2arc_free_on_write, df); | |
1815 | mutex_exit(&l2arc_free_on_write_mtx); | |
1816 | } | |
1817 | ||
34dc7c2f BB |
1818 | /* |
1819 | * Free the arc data buffer. If it is an l2arc write in progress, | |
1820 | * the buffer is placed on l2arc_free_on_write to be freed later. | |
1821 | */ | |
1822 | static void | |
498877ba | 1823 | arc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t)) |
34dc7c2f | 1824 | { |
498877ba MA |
1825 | arc_buf_hdr_t *hdr = buf->b_hdr; |
1826 | ||
34dc7c2f | 1827 | if (HDR_L2_WRITING(hdr)) { |
ca0bf58d | 1828 | arc_buf_free_on_write(buf->b_data, hdr->b_size, free_func); |
34dc7c2f BB |
1829 | ARCSTAT_BUMP(arcstat_l2_free_on_write); |
1830 | } else { | |
498877ba | 1831 | free_func(buf->b_data, hdr->b_size); |
34dc7c2f BB |
1832 | } |
1833 | } | |
1834 | ||
ca0bf58d PS |
1835 | static void |
1836 | arc_buf_l2_cdata_free(arc_buf_hdr_t *hdr) | |
1837 | { | |
1838 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
1839 | ASSERT(MUTEX_HELD(&hdr->b_l2hdr.b_dev->l2ad_mtx)); | |
1840 | ||
1841 | /* | |
1842 | * The b_tmp_cdata field is linked off of the b_l1hdr, so if | |
1843 | * that doesn't exist, the header is in the arc_l2c_only state, | |
1844 | * and there isn't anything to free (it's already been freed). | |
1845 | */ | |
1846 | if (!HDR_HAS_L1HDR(hdr)) | |
1847 | return; | |
1848 | ||
1849 | /* | |
1850 | * The header isn't being written to the l2arc device, thus it | |
1851 | * shouldn't have a b_tmp_cdata to free. | |
1852 | */ | |
1853 | if (!HDR_L2_WRITING(hdr)) { | |
1854 | ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); | |
1855 | return; | |
1856 | } | |
1857 | ||
1858 | /* | |
1859 | * The header does not have compression enabled. This can be due | |
1860 | * to the buffer not being compressible, or because we're | |
1861 | * freeing the buffer before the second phase of | |
1862 | * l2arc_write_buffer() has started (which does the compression | |
1863 | * step). In either case, b_tmp_cdata does not point to a | |
1864 | * separately compressed buffer, so there's nothing to free (it | |
1865 | * points to the same buffer as the arc_buf_t's b_data field). | |
1866 | */ | |
1867 | if (HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) { | |
1868 | hdr->b_l1hdr.b_tmp_cdata = NULL; | |
1869 | return; | |
1870 | } | |
1871 | ||
1872 | /* | |
1873 | * There's nothing to free since the buffer was all zero's and | |
1874 | * compressed to a zero length buffer. | |
1875 | */ | |
1876 | if (HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_EMPTY) { | |
1877 | ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); | |
1878 | return; | |
1879 | } | |
1880 | ||
1881 | ASSERT(L2ARC_IS_VALID_COMPRESS(HDR_GET_COMPRESS(hdr))); | |
1882 | ||
1883 | arc_buf_free_on_write(hdr->b_l1hdr.b_tmp_cdata, | |
1884 | hdr->b_size, zio_data_buf_free); | |
1885 | ||
1886 | ARCSTAT_BUMP(arcstat_l2_cdata_free_on_write); | |
1887 | hdr->b_l1hdr.b_tmp_cdata = NULL; | |
1888 | } | |
1889 | ||
bd089c54 MA |
1890 | /* |
1891 | * Free up buf->b_data and if 'remove' is set, then pull the | |
1892 | * arc_buf_t off of the the arc_buf_hdr_t's list and free it. | |
1893 | */ | |
34dc7c2f | 1894 | static void |
ca0bf58d | 1895 | arc_buf_destroy(arc_buf_t *buf, boolean_t remove) |
34dc7c2f BB |
1896 | { |
1897 | arc_buf_t **bufp; | |
1898 | ||
1899 | /* free up data associated with the buf */ | |
b9541d6b CW |
1900 | if (buf->b_data != NULL) { |
1901 | arc_state_t *state = buf->b_hdr->b_l1hdr.b_state; | |
34dc7c2f | 1902 | uint64_t size = buf->b_hdr->b_size; |
b9541d6b | 1903 | arc_buf_contents_t type = arc_buf_type(buf->b_hdr); |
34dc7c2f BB |
1904 | |
1905 | arc_cksum_verify(buf); | |
498877ba | 1906 | arc_buf_unwatch(buf); |
428870ff | 1907 | |
ca0bf58d PS |
1908 | if (type == ARC_BUFC_METADATA) { |
1909 | arc_buf_data_free(buf, zio_buf_free); | |
1910 | arc_space_return(size, ARC_SPACE_META); | |
1911 | } else { | |
1912 | ASSERT(type == ARC_BUFC_DATA); | |
1913 | arc_buf_data_free(buf, zio_data_buf_free); | |
1914 | arc_space_return(size, ARC_SPACE_DATA); | |
34dc7c2f | 1915 | } |
ca0bf58d PS |
1916 | |
1917 | /* protected by hash lock, if in the hash table */ | |
1918 | if (multilist_link_active(&buf->b_hdr->b_l1hdr.b_arc_node)) { | |
34dc7c2f BB |
1919 | uint64_t *cnt = &state->arcs_lsize[type]; |
1920 | ||
b9541d6b CW |
1921 | ASSERT(refcount_is_zero( |
1922 | &buf->b_hdr->b_l1hdr.b_refcnt)); | |
1923 | ASSERT(state != arc_anon && state != arc_l2c_only); | |
34dc7c2f BB |
1924 | |
1925 | ASSERT3U(*cnt, >=, size); | |
1926 | atomic_add_64(cnt, -size); | |
1927 | } | |
1928 | ASSERT3U(state->arcs_size, >=, size); | |
1929 | atomic_add_64(&state->arcs_size, -size); | |
1930 | buf->b_data = NULL; | |
1eb5bfa3 GW |
1931 | |
1932 | /* | |
1933 | * If we're destroying a duplicate buffer make sure | |
1934 | * that the appropriate statistics are updated. | |
1935 | */ | |
b9541d6b CW |
1936 | if (buf->b_hdr->b_l1hdr.b_datacnt > 1 && |
1937 | HDR_ISTYPE_DATA(buf->b_hdr)) { | |
1eb5bfa3 GW |
1938 | ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); |
1939 | ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size); | |
1940 | } | |
b9541d6b CW |
1941 | ASSERT(buf->b_hdr->b_l1hdr.b_datacnt > 0); |
1942 | buf->b_hdr->b_l1hdr.b_datacnt -= 1; | |
34dc7c2f BB |
1943 | } |
1944 | ||
1945 | /* only remove the buf if requested */ | |
bd089c54 | 1946 | if (!remove) |
34dc7c2f BB |
1947 | return; |
1948 | ||
1949 | /* remove the buf from the hdr list */ | |
b9541d6b CW |
1950 | for (bufp = &buf->b_hdr->b_l1hdr.b_buf; *bufp != buf; |
1951 | bufp = &(*bufp)->b_next) | |
34dc7c2f BB |
1952 | continue; |
1953 | *bufp = buf->b_next; | |
428870ff | 1954 | buf->b_next = NULL; |
34dc7c2f BB |
1955 | |
1956 | ASSERT(buf->b_efunc == NULL); | |
1957 | ||
1958 | /* clean up the buf */ | |
1959 | buf->b_hdr = NULL; | |
1960 | kmem_cache_free(buf_cache, buf); | |
1961 | } | |
1962 | ||
d962d5da PS |
1963 | static void |
1964 | arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) | |
1965 | { | |
1966 | l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; | |
1967 | l2arc_dev_t *dev = l2hdr->b_dev; | |
1968 | ||
1969 | ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); | |
1970 | ASSERT(HDR_HAS_L2HDR(hdr)); | |
1971 | ||
1972 | list_remove(&dev->l2ad_buflist, hdr); | |
1973 | ||
1974 | arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); | |
1975 | ||
1976 | /* | |
1977 | * We don't want to leak the b_tmp_cdata buffer that was | |
1978 | * allocated in l2arc_write_buffers() | |
1979 | */ | |
1980 | arc_buf_l2_cdata_free(hdr); | |
1981 | ||
1982 | /* | |
1983 | * If the l2hdr's b_daddr is equal to L2ARC_ADDR_UNSET, then | |
1984 | * this header is being processed by l2arc_write_buffers() (i.e. | |
1985 | * it's in the first stage of l2arc_write_buffers()). | |
1986 | * Re-affirming that truth here, just to serve as a reminder. If | |
1987 | * b_daddr does not equal L2ARC_ADDR_UNSET, then the header may or | |
1988 | * may not have its HDR_L2_WRITING flag set. (the write may have | |
1989 | * completed, in which case HDR_L2_WRITING will be false and the | |
1990 | * b_daddr field will point to the address of the buffer on disk). | |
1991 | */ | |
1992 | IMPLY(l2hdr->b_daddr == L2ARC_ADDR_UNSET, HDR_L2_WRITING(hdr)); | |
1993 | ||
1994 | /* | |
1995 | * If b_daddr is equal to L2ARC_ADDR_UNSET, we're racing with | |
1996 | * l2arc_write_buffers(). Since we've just removed this header | |
1997 | * from the l2arc buffer list, this header will never reach the | |
1998 | * second stage of l2arc_write_buffers(), which increments the | |
1999 | * accounting stats for this header. Thus, we must be careful | |
2000 | * not to decrement them for this header either. | |
2001 | */ | |
2002 | if (l2hdr->b_daddr != L2ARC_ADDR_UNSET) { | |
2003 | ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize); | |
2004 | ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); | |
2005 | ||
2006 | vdev_space_update(dev->l2ad_vdev, | |
2007 | -l2hdr->b_asize, 0, 0); | |
2008 | ||
2009 | (void) refcount_remove_many(&dev->l2ad_alloc, | |
2010 | l2hdr->b_asize, hdr); | |
2011 | } | |
2012 | ||
2013 | hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR; | |
2014 | } | |
2015 | ||
34dc7c2f BB |
2016 | static void |
2017 | arc_hdr_destroy(arc_buf_hdr_t *hdr) | |
2018 | { | |
b9541d6b CW |
2019 | if (HDR_HAS_L1HDR(hdr)) { |
2020 | ASSERT(hdr->b_l1hdr.b_buf == NULL || | |
2021 | hdr->b_l1hdr.b_datacnt > 0); | |
2022 | ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); | |
2023 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); | |
2024 | } | |
34dc7c2f | 2025 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b CW |
2026 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); |
2027 | ||
2028 | if (HDR_HAS_L2HDR(hdr)) { | |
d962d5da PS |
2029 | l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; |
2030 | boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); | |
428870ff | 2031 | |
d962d5da PS |
2032 | if (!buflist_held) |
2033 | mutex_enter(&dev->l2ad_mtx); | |
b9541d6b | 2034 | |
ca0bf58d | 2035 | /* |
d962d5da PS |
2036 | * Even though we checked this conditional above, we |
2037 | * need to check this again now that we have the | |
2038 | * l2ad_mtx. This is because we could be racing with | |
2039 | * another thread calling l2arc_evict() which might have | |
2040 | * destroyed this header's L2 portion as we were waiting | |
2041 | * to acquire the l2ad_mtx. If that happens, we don't | |
2042 | * want to re-destroy the header's L2 portion. | |
ca0bf58d | 2043 | */ |
d962d5da PS |
2044 | if (HDR_HAS_L2HDR(hdr)) |
2045 | arc_hdr_l2hdr_destroy(hdr); | |
428870ff BB |
2046 | |
2047 | if (!buflist_held) | |
d962d5da | 2048 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
2049 | } |
2050 | ||
b9541d6b | 2051 | if (!BUF_EMPTY(hdr)) |
428870ff | 2052 | buf_discard_identity(hdr); |
b9541d6b | 2053 | |
34dc7c2f BB |
2054 | if (hdr->b_freeze_cksum != NULL) { |
2055 | kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); | |
2056 | hdr->b_freeze_cksum = NULL; | |
2057 | } | |
2058 | ||
b9541d6b CW |
2059 | if (HDR_HAS_L1HDR(hdr)) { |
2060 | while (hdr->b_l1hdr.b_buf) { | |
2061 | arc_buf_t *buf = hdr->b_l1hdr.b_buf; | |
2062 | ||
2063 | if (buf->b_efunc != NULL) { | |
ca0bf58d | 2064 | mutex_enter(&arc_user_evicts_lock); |
b9541d6b CW |
2065 | mutex_enter(&buf->b_evict_lock); |
2066 | ASSERT(buf->b_hdr != NULL); | |
ca0bf58d | 2067 | arc_buf_destroy(hdr->b_l1hdr.b_buf, FALSE); |
b9541d6b CW |
2068 | hdr->b_l1hdr.b_buf = buf->b_next; |
2069 | buf->b_hdr = &arc_eviction_hdr; | |
2070 | buf->b_next = arc_eviction_list; | |
2071 | arc_eviction_list = buf; | |
2072 | mutex_exit(&buf->b_evict_lock); | |
ca0bf58d PS |
2073 | cv_signal(&arc_user_evicts_cv); |
2074 | mutex_exit(&arc_user_evicts_lock); | |
b9541d6b | 2075 | } else { |
ca0bf58d | 2076 | arc_buf_destroy(hdr->b_l1hdr.b_buf, TRUE); |
b9541d6b CW |
2077 | } |
2078 | } | |
2079 | } | |
2080 | ||
34dc7c2f | 2081 | ASSERT3P(hdr->b_hash_next, ==, NULL); |
b9541d6b | 2082 | if (HDR_HAS_L1HDR(hdr)) { |
ca0bf58d | 2083 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); |
b9541d6b CW |
2084 | ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); |
2085 | kmem_cache_free(hdr_full_cache, hdr); | |
2086 | } else { | |
2087 | kmem_cache_free(hdr_l2only_cache, hdr); | |
2088 | } | |
34dc7c2f BB |
2089 | } |
2090 | ||
2091 | void | |
2092 | arc_buf_free(arc_buf_t *buf, void *tag) | |
2093 | { | |
2094 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
b9541d6b | 2095 | int hashed = hdr->b_l1hdr.b_state != arc_anon; |
34dc7c2f BB |
2096 | |
2097 | ASSERT(buf->b_efunc == NULL); | |
2098 | ASSERT(buf->b_data != NULL); | |
2099 | ||
2100 | if (hashed) { | |
2101 | kmutex_t *hash_lock = HDR_LOCK(hdr); | |
2102 | ||
2103 | mutex_enter(hash_lock); | |
428870ff BB |
2104 | hdr = buf->b_hdr; |
2105 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); | |
2106 | ||
34dc7c2f | 2107 | (void) remove_reference(hdr, hash_lock, tag); |
b9541d6b | 2108 | if (hdr->b_l1hdr.b_datacnt > 1) { |
ca0bf58d | 2109 | arc_buf_destroy(buf, TRUE); |
428870ff | 2110 | } else { |
b9541d6b | 2111 | ASSERT(buf == hdr->b_l1hdr.b_buf); |
428870ff | 2112 | ASSERT(buf->b_efunc == NULL); |
2a432414 | 2113 | hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; |
428870ff | 2114 | } |
34dc7c2f BB |
2115 | mutex_exit(hash_lock); |
2116 | } else if (HDR_IO_IN_PROGRESS(hdr)) { | |
2117 | int destroy_hdr; | |
2118 | /* | |
2119 | * We are in the middle of an async write. Don't destroy | |
2120 | * this buffer unless the write completes before we finish | |
2121 | * decrementing the reference count. | |
2122 | */ | |
ca0bf58d | 2123 | mutex_enter(&arc_user_evicts_lock); |
34dc7c2f | 2124 | (void) remove_reference(hdr, NULL, tag); |
b9541d6b | 2125 | ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
34dc7c2f | 2126 | destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); |
ca0bf58d | 2127 | mutex_exit(&arc_user_evicts_lock); |
34dc7c2f BB |
2128 | if (destroy_hdr) |
2129 | arc_hdr_destroy(hdr); | |
2130 | } else { | |
428870ff | 2131 | if (remove_reference(hdr, NULL, tag) > 0) |
ca0bf58d | 2132 | arc_buf_destroy(buf, TRUE); |
428870ff | 2133 | else |
34dc7c2f | 2134 | arc_hdr_destroy(hdr); |
34dc7c2f BB |
2135 | } |
2136 | } | |
2137 | ||
13fe0198 | 2138 | boolean_t |
34dc7c2f BB |
2139 | arc_buf_remove_ref(arc_buf_t *buf, void* tag) |
2140 | { | |
2141 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
b4f7f105 | 2142 | kmutex_t *hash_lock = NULL; |
13fe0198 | 2143 | boolean_t no_callback = (buf->b_efunc == NULL); |
34dc7c2f | 2144 | |
b9541d6b CW |
2145 | if (hdr->b_l1hdr.b_state == arc_anon) { |
2146 | ASSERT(hdr->b_l1hdr.b_datacnt == 1); | |
34dc7c2f BB |
2147 | arc_buf_free(buf, tag); |
2148 | return (no_callback); | |
2149 | } | |
2150 | ||
b4f7f105 | 2151 | hash_lock = HDR_LOCK(hdr); |
34dc7c2f | 2152 | mutex_enter(hash_lock); |
428870ff | 2153 | hdr = buf->b_hdr; |
b9541d6b | 2154 | ASSERT(hdr->b_l1hdr.b_datacnt > 0); |
428870ff | 2155 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); |
b9541d6b | 2156 | ASSERT(hdr->b_l1hdr.b_state != arc_anon); |
34dc7c2f BB |
2157 | ASSERT(buf->b_data != NULL); |
2158 | ||
2159 | (void) remove_reference(hdr, hash_lock, tag); | |
b9541d6b | 2160 | if (hdr->b_l1hdr.b_datacnt > 1) { |
34dc7c2f | 2161 | if (no_callback) |
ca0bf58d | 2162 | arc_buf_destroy(buf, TRUE); |
34dc7c2f | 2163 | } else if (no_callback) { |
b9541d6b | 2164 | ASSERT(hdr->b_l1hdr.b_buf == buf && buf->b_next == NULL); |
428870ff | 2165 | ASSERT(buf->b_efunc == NULL); |
2a432414 | 2166 | hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; |
34dc7c2f | 2167 | } |
b9541d6b CW |
2168 | ASSERT(no_callback || hdr->b_l1hdr.b_datacnt > 1 || |
2169 | refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); | |
34dc7c2f BB |
2170 | mutex_exit(hash_lock); |
2171 | return (no_callback); | |
2172 | } | |
2173 | ||
5f6d0b6f | 2174 | uint64_t |
34dc7c2f BB |
2175 | arc_buf_size(arc_buf_t *buf) |
2176 | { | |
2177 | return (buf->b_hdr->b_size); | |
2178 | } | |
2179 | ||
1eb5bfa3 GW |
2180 | /* |
2181 | * Called from the DMU to determine if the current buffer should be | |
2182 | * evicted. In order to ensure proper locking, the eviction must be initiated | |
2183 | * from the DMU. Return true if the buffer is associated with user data and | |
2184 | * duplicate buffers still exist. | |
2185 | */ | |
2186 | boolean_t | |
2187 | arc_buf_eviction_needed(arc_buf_t *buf) | |
2188 | { | |
2189 | arc_buf_hdr_t *hdr; | |
2190 | boolean_t evict_needed = B_FALSE; | |
2191 | ||
2192 | if (zfs_disable_dup_eviction) | |
2193 | return (B_FALSE); | |
2194 | ||
2195 | mutex_enter(&buf->b_evict_lock); | |
2196 | hdr = buf->b_hdr; | |
2197 | if (hdr == NULL) { | |
2198 | /* | |
2199 | * We are in arc_do_user_evicts(); let that function | |
2200 | * perform the eviction. | |
2201 | */ | |
2202 | ASSERT(buf->b_data == NULL); | |
2203 | mutex_exit(&buf->b_evict_lock); | |
2204 | return (B_FALSE); | |
2205 | } else if (buf->b_data == NULL) { | |
2206 | /* | |
2207 | * We have already been added to the arc eviction list; | |
2208 | * recommend eviction. | |
2209 | */ | |
2210 | ASSERT3P(hdr, ==, &arc_eviction_hdr); | |
2211 | mutex_exit(&buf->b_evict_lock); | |
2212 | return (B_TRUE); | |
2213 | } | |
2214 | ||
b9541d6b | 2215 | if (hdr->b_l1hdr.b_datacnt > 1 && HDR_ISTYPE_DATA(hdr)) |
1eb5bfa3 GW |
2216 | evict_needed = B_TRUE; |
2217 | ||
2218 | mutex_exit(&buf->b_evict_lock); | |
2219 | return (evict_needed); | |
2220 | } | |
2221 | ||
34dc7c2f | 2222 | /* |
ca0bf58d PS |
2223 | * Evict the arc_buf_hdr that is provided as a parameter. The resultant |
2224 | * state of the header is dependent on its state prior to entering this | |
2225 | * function. The following transitions are possible: | |
34dc7c2f | 2226 | * |
ca0bf58d PS |
2227 | * - arc_mru -> arc_mru_ghost |
2228 | * - arc_mfu -> arc_mfu_ghost | |
2229 | * - arc_mru_ghost -> arc_l2c_only | |
2230 | * - arc_mru_ghost -> deleted | |
2231 | * - arc_mfu_ghost -> arc_l2c_only | |
2232 | * - arc_mfu_ghost -> deleted | |
34dc7c2f | 2233 | */ |
ca0bf58d PS |
2234 | static int64_t |
2235 | arc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) | |
34dc7c2f | 2236 | { |
ca0bf58d PS |
2237 | arc_state_t *evicted_state, *state; |
2238 | int64_t bytes_evicted = 0; | |
34dc7c2f | 2239 | |
ca0bf58d PS |
2240 | ASSERT(MUTEX_HELD(hash_lock)); |
2241 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
e8b96c60 | 2242 | |
ca0bf58d PS |
2243 | state = hdr->b_l1hdr.b_state; |
2244 | if (GHOST_STATE(state)) { | |
2245 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
2246 | ASSERT(hdr->b_l1hdr.b_buf == NULL); | |
e8b96c60 MA |
2247 | |
2248 | /* | |
ca0bf58d PS |
2249 | * l2arc_write_buffers() relies on a header's L1 portion |
2250 | * (i.e. its b_tmp_cdata field) during its write phase. | |
2251 | * Thus, we cannot push a header onto the arc_l2c_only | |
2252 | * state (removing its L1 piece) until the header is | |
2253 | * done being written to the l2arc. | |
e8b96c60 | 2254 | */ |
ca0bf58d PS |
2255 | if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { |
2256 | ARCSTAT_BUMP(arcstat_evict_l2_skip); | |
2257 | return (bytes_evicted); | |
e8b96c60 MA |
2258 | } |
2259 | ||
ca0bf58d PS |
2260 | ARCSTAT_BUMP(arcstat_deleted); |
2261 | bytes_evicted += hdr->b_size; | |
428870ff | 2262 | |
ca0bf58d | 2263 | DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); |
428870ff | 2264 | |
ca0bf58d PS |
2265 | if (HDR_HAS_L2HDR(hdr)) { |
2266 | /* | |
2267 | * This buffer is cached on the 2nd Level ARC; | |
2268 | * don't destroy the header. | |
2269 | */ | |
2270 | arc_change_state(arc_l2c_only, hdr, hash_lock); | |
2271 | /* | |
2272 | * dropping from L1+L2 cached to L2-only, | |
2273 | * realloc to remove the L1 header. | |
2274 | */ | |
2275 | hdr = arc_hdr_realloc(hdr, hdr_full_cache, | |
2276 | hdr_l2only_cache); | |
34dc7c2f | 2277 | } else { |
ca0bf58d PS |
2278 | arc_change_state(arc_anon, hdr, hash_lock); |
2279 | arc_hdr_destroy(hdr); | |
34dc7c2f | 2280 | } |
ca0bf58d | 2281 | return (bytes_evicted); |
34dc7c2f BB |
2282 | } |
2283 | ||
ca0bf58d PS |
2284 | ASSERT(state == arc_mru || state == arc_mfu); |
2285 | evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost; | |
34dc7c2f | 2286 | |
ca0bf58d PS |
2287 | /* prefetch buffers have a minimum lifespan */ |
2288 | if (HDR_IO_IN_PROGRESS(hdr) || | |
2289 | ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && | |
2290 | ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < | |
2291 | arc_min_prefetch_lifespan)) { | |
2292 | ARCSTAT_BUMP(arcstat_evict_skip); | |
2293 | return (bytes_evicted); | |
da8ccd0e PS |
2294 | } |
2295 | ||
ca0bf58d PS |
2296 | ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); |
2297 | ASSERT3U(hdr->b_l1hdr.b_datacnt, >, 0); | |
2298 | while (hdr->b_l1hdr.b_buf) { | |
2299 | arc_buf_t *buf = hdr->b_l1hdr.b_buf; | |
2300 | if (!mutex_tryenter(&buf->b_evict_lock)) { | |
2301 | ARCSTAT_BUMP(arcstat_mutex_miss); | |
2302 | break; | |
2303 | } | |
2304 | if (buf->b_data != NULL) | |
2305 | bytes_evicted += hdr->b_size; | |
2306 | if (buf->b_efunc != NULL) { | |
2307 | mutex_enter(&arc_user_evicts_lock); | |
2308 | arc_buf_destroy(buf, FALSE); | |
2309 | hdr->b_l1hdr.b_buf = buf->b_next; | |
2310 | buf->b_hdr = &arc_eviction_hdr; | |
2311 | buf->b_next = arc_eviction_list; | |
2312 | arc_eviction_list = buf; | |
2313 | cv_signal(&arc_user_evicts_cv); | |
2314 | mutex_exit(&arc_user_evicts_lock); | |
2315 | mutex_exit(&buf->b_evict_lock); | |
2316 | } else { | |
2317 | mutex_exit(&buf->b_evict_lock); | |
2318 | arc_buf_destroy(buf, TRUE); | |
2319 | } | |
2320 | } | |
34dc7c2f | 2321 | |
ca0bf58d PS |
2322 | if (HDR_HAS_L2HDR(hdr)) { |
2323 | ARCSTAT_INCR(arcstat_evict_l2_cached, hdr->b_size); | |
2324 | } else { | |
2325 | if (l2arc_write_eligible(hdr->b_spa, hdr)) | |
2326 | ARCSTAT_INCR(arcstat_evict_l2_eligible, hdr->b_size); | |
2327 | else | |
2328 | ARCSTAT_INCR(arcstat_evict_l2_ineligible, hdr->b_size); | |
2329 | } | |
34dc7c2f | 2330 | |
ca0bf58d PS |
2331 | if (hdr->b_l1hdr.b_datacnt == 0) { |
2332 | arc_change_state(evicted_state, hdr, hash_lock); | |
2333 | ASSERT(HDR_IN_HASH_TABLE(hdr)); | |
2334 | hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE; | |
2335 | hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; | |
2336 | DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); | |
2337 | } | |
34dc7c2f | 2338 | |
ca0bf58d | 2339 | return (bytes_evicted); |
34dc7c2f BB |
2340 | } |
2341 | ||
ca0bf58d PS |
2342 | static uint64_t |
2343 | arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, | |
2344 | uint64_t spa, int64_t bytes) | |
34dc7c2f | 2345 | { |
ca0bf58d PS |
2346 | multilist_sublist_t *mls; |
2347 | uint64_t bytes_evicted = 0; | |
2348 | arc_buf_hdr_t *hdr; | |
34dc7c2f | 2349 | kmutex_t *hash_lock; |
ca0bf58d | 2350 | int evict_count = 0; |
34dc7c2f | 2351 | |
ca0bf58d PS |
2352 | ASSERT3P(marker, !=, NULL); |
2353 | ASSERTV(if (bytes < 0) ASSERT(bytes == ARC_EVICT_ALL)); | |
2354 | ||
2355 | mls = multilist_sublist_lock(ml, idx); | |
572e2857 | 2356 | |
ca0bf58d PS |
2357 | for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL; |
2358 | hdr = multilist_sublist_prev(mls, marker)) { | |
2359 | if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) || | |
2360 | (evict_count >= zfs_arc_evict_batch_limit)) | |
2361 | break; | |
2362 | ||
2363 | /* | |
2364 | * To keep our iteration location, move the marker | |
2365 | * forward. Since we're not holding hdr's hash lock, we | |
2366 | * must be very careful and not remove 'hdr' from the | |
2367 | * sublist. Otherwise, other consumers might mistake the | |
2368 | * 'hdr' as not being on a sublist when they call the | |
2369 | * multilist_link_active() function (they all rely on | |
2370 | * the hash lock protecting concurrent insertions and | |
2371 | * removals). multilist_sublist_move_forward() was | |
2372 | * specifically implemented to ensure this is the case | |
2373 | * (only 'marker' will be removed and re-inserted). | |
2374 | */ | |
2375 | multilist_sublist_move_forward(mls, marker); | |
2376 | ||
2377 | /* | |
2378 | * The only case where the b_spa field should ever be | |
2379 | * zero, is the marker headers inserted by | |
2380 | * arc_evict_state(). It's possible for multiple threads | |
2381 | * to be calling arc_evict_state() concurrently (e.g. | |
2382 | * dsl_pool_close() and zio_inject_fault()), so we must | |
2383 | * skip any markers we see from these other threads. | |
2384 | */ | |
2a432414 | 2385 | if (hdr->b_spa == 0) |
572e2857 BB |
2386 | continue; |
2387 | ||
ca0bf58d PS |
2388 | /* we're only interested in evicting buffers of a certain spa */ |
2389 | if (spa != 0 && hdr->b_spa != spa) { | |
2390 | ARCSTAT_BUMP(arcstat_evict_skip); | |
428870ff | 2391 | continue; |
ca0bf58d PS |
2392 | } |
2393 | ||
2394 | hash_lock = HDR_LOCK(hdr); | |
e8b96c60 MA |
2395 | |
2396 | /* | |
ca0bf58d PS |
2397 | * We aren't calling this function from any code path |
2398 | * that would already be holding a hash lock, so we're | |
2399 | * asserting on this assumption to be defensive in case | |
2400 | * this ever changes. Without this check, it would be | |
2401 | * possible to incorrectly increment arcstat_mutex_miss | |
2402 | * below (e.g. if the code changed such that we called | |
2403 | * this function with a hash lock held). | |
e8b96c60 | 2404 | */ |
ca0bf58d PS |
2405 | ASSERT(!MUTEX_HELD(hash_lock)); |
2406 | ||
34dc7c2f | 2407 | if (mutex_tryenter(hash_lock)) { |
ca0bf58d PS |
2408 | uint64_t evicted = arc_evict_hdr(hdr, hash_lock); |
2409 | mutex_exit(hash_lock); | |
34dc7c2f | 2410 | |
ca0bf58d | 2411 | bytes_evicted += evicted; |
34dc7c2f | 2412 | |
572e2857 | 2413 | /* |
ca0bf58d PS |
2414 | * If evicted is zero, arc_evict_hdr() must have |
2415 | * decided to skip this header, don't increment | |
2416 | * evict_count in this case. | |
572e2857 | 2417 | */ |
ca0bf58d PS |
2418 | if (evicted != 0) |
2419 | evict_count++; | |
2420 | ||
2421 | /* | |
2422 | * If arc_size isn't overflowing, signal any | |
2423 | * threads that might happen to be waiting. | |
2424 | * | |
2425 | * For each header evicted, we wake up a single | |
2426 | * thread. If we used cv_broadcast, we could | |
2427 | * wake up "too many" threads causing arc_size | |
2428 | * to significantly overflow arc_c; since | |
2429 | * arc_get_data_buf() doesn't check for overflow | |
2430 | * when it's woken up (it doesn't because it's | |
2431 | * possible for the ARC to be overflowing while | |
2432 | * full of un-evictable buffers, and the | |
2433 | * function should proceed in this case). | |
2434 | * | |
2435 | * If threads are left sleeping, due to not | |
2436 | * using cv_broadcast, they will be woken up | |
2437 | * just before arc_reclaim_thread() sleeps. | |
2438 | */ | |
2439 | mutex_enter(&arc_reclaim_lock); | |
2440 | if (!arc_is_overflowing()) | |
2441 | cv_signal(&arc_reclaim_waiters_cv); | |
2442 | mutex_exit(&arc_reclaim_lock); | |
e8b96c60 | 2443 | } else { |
ca0bf58d | 2444 | ARCSTAT_BUMP(arcstat_mutex_miss); |
e8b96c60 | 2445 | } |
34dc7c2f | 2446 | } |
34dc7c2f | 2447 | |
ca0bf58d | 2448 | multilist_sublist_unlock(mls); |
34dc7c2f | 2449 | |
ca0bf58d | 2450 | return (bytes_evicted); |
34dc7c2f BB |
2451 | } |
2452 | ||
ca0bf58d PS |
2453 | /* |
2454 | * Evict buffers from the given arc state, until we've removed the | |
2455 | * specified number of bytes. Move the removed buffers to the | |
2456 | * appropriate evict state. | |
2457 | * | |
2458 | * This function makes a "best effort". It skips over any buffers | |
2459 | * it can't get a hash_lock on, and so, may not catch all candidates. | |
2460 | * It may also return without evicting as much space as requested. | |
2461 | * | |
2462 | * If bytes is specified using the special value ARC_EVICT_ALL, this | |
2463 | * will evict all available (i.e. unlocked and evictable) buffers from | |
2464 | * the given arc state; which is used by arc_flush(). | |
2465 | */ | |
2466 | static uint64_t | |
2467 | arc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes, | |
2468 | arc_buf_contents_t type) | |
34dc7c2f | 2469 | { |
ca0bf58d PS |
2470 | uint64_t total_evicted = 0; |
2471 | multilist_t *ml = &state->arcs_list[type]; | |
2472 | int num_sublists; | |
2473 | arc_buf_hdr_t **markers; | |
2474 | int i; | |
2475 | ||
2476 | ASSERTV(if (bytes < 0) ASSERT(bytes == ARC_EVICT_ALL)); | |
2477 | ||
2478 | num_sublists = multilist_get_num_sublists(ml); | |
d164b209 BB |
2479 | |
2480 | /* | |
ca0bf58d PS |
2481 | * If we've tried to evict from each sublist, made some |
2482 | * progress, but still have not hit the target number of bytes | |
2483 | * to evict, we want to keep trying. The markers allow us to | |
2484 | * pick up where we left off for each individual sublist, rather | |
2485 | * than starting from the tail each time. | |
d164b209 | 2486 | */ |
ca0bf58d PS |
2487 | markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP); |
2488 | for (i = 0; i < num_sublists; i++) { | |
2489 | multilist_sublist_t *mls; | |
34dc7c2f | 2490 | |
ca0bf58d PS |
2491 | markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); |
2492 | ||
2493 | /* | |
2494 | * A b_spa of 0 is used to indicate that this header is | |
2495 | * a marker. This fact is used in arc_adjust_type() and | |
2496 | * arc_evict_state_impl(). | |
2497 | */ | |
2498 | markers[i]->b_spa = 0; | |
34dc7c2f | 2499 | |
ca0bf58d PS |
2500 | mls = multilist_sublist_lock(ml, i); |
2501 | multilist_sublist_insert_tail(mls, markers[i]); | |
2502 | multilist_sublist_unlock(mls); | |
34dc7c2f BB |
2503 | } |
2504 | ||
d164b209 | 2505 | /* |
ca0bf58d PS |
2506 | * While we haven't hit our target number of bytes to evict, or |
2507 | * we're evicting all available buffers. | |
d164b209 | 2508 | */ |
ca0bf58d PS |
2509 | while (total_evicted < bytes || bytes == ARC_EVICT_ALL) { |
2510 | /* | |
2511 | * Start eviction using a randomly selected sublist, | |
2512 | * this is to try and evenly balance eviction across all | |
2513 | * sublists. Always starting at the same sublist | |
2514 | * (e.g. index 0) would cause evictions to favor certain | |
2515 | * sublists over others. | |
2516 | */ | |
2517 | int sublist_idx = multilist_get_random_index(ml); | |
2518 | uint64_t scan_evicted = 0; | |
34dc7c2f | 2519 | |
ca0bf58d PS |
2520 | for (i = 0; i < num_sublists; i++) { |
2521 | uint64_t bytes_remaining; | |
2522 | uint64_t bytes_evicted; | |
d164b209 | 2523 | |
ca0bf58d PS |
2524 | if (bytes == ARC_EVICT_ALL) |
2525 | bytes_remaining = ARC_EVICT_ALL; | |
2526 | else if (total_evicted < bytes) | |
2527 | bytes_remaining = bytes - total_evicted; | |
2528 | else | |
2529 | break; | |
34dc7c2f | 2530 | |
ca0bf58d PS |
2531 | bytes_evicted = arc_evict_state_impl(ml, sublist_idx, |
2532 | markers[sublist_idx], spa, bytes_remaining); | |
2533 | ||
2534 | scan_evicted += bytes_evicted; | |
2535 | total_evicted += bytes_evicted; | |
2536 | ||
2537 | /* we've reached the end, wrap to the beginning */ | |
2538 | if (++sublist_idx >= num_sublists) | |
2539 | sublist_idx = 0; | |
2540 | } | |
2541 | ||
2542 | /* | |
2543 | * If we didn't evict anything during this scan, we have | |
2544 | * no reason to believe we'll evict more during another | |
2545 | * scan, so break the loop. | |
2546 | */ | |
2547 | if (scan_evicted == 0) { | |
2548 | /* This isn't possible, let's make that obvious */ | |
2549 | ASSERT3S(bytes, !=, 0); | |
34dc7c2f | 2550 | |
ca0bf58d PS |
2551 | /* |
2552 | * When bytes is ARC_EVICT_ALL, the only way to | |
2553 | * break the loop is when scan_evicted is zero. | |
2554 | * In that case, we actually have evicted enough, | |
2555 | * so we don't want to increment the kstat. | |
2556 | */ | |
2557 | if (bytes != ARC_EVICT_ALL) { | |
2558 | ASSERT3S(total_evicted, <, bytes); | |
2559 | ARCSTAT_BUMP(arcstat_evict_not_enough); | |
2560 | } | |
d164b209 | 2561 | |
ca0bf58d PS |
2562 | break; |
2563 | } | |
d164b209 | 2564 | } |
34dc7c2f | 2565 | |
ca0bf58d PS |
2566 | for (i = 0; i < num_sublists; i++) { |
2567 | multilist_sublist_t *mls = multilist_sublist_lock(ml, i); | |
2568 | multilist_sublist_remove(mls, markers[i]); | |
2569 | multilist_sublist_unlock(mls); | |
34dc7c2f | 2570 | |
ca0bf58d | 2571 | kmem_cache_free(hdr_full_cache, markers[i]); |
34dc7c2f | 2572 | } |
ca0bf58d PS |
2573 | kmem_free(markers, sizeof (*markers) * num_sublists); |
2574 | ||
2575 | return (total_evicted); | |
2576 | } | |
2577 | ||
2578 | /* | |
2579 | * Flush all "evictable" data of the given type from the arc state | |
2580 | * specified. This will not evict any "active" buffers (i.e. referenced). | |
2581 | * | |
2582 | * When 'retry' is set to FALSE, the function will make a single pass | |
2583 | * over the state and evict any buffers that it can. Since it doesn't | |
2584 | * continually retry the eviction, it might end up leaving some buffers | |
2585 | * in the ARC due to lock misses. | |
2586 | * | |
2587 | * When 'retry' is set to TRUE, the function will continually retry the | |
2588 | * eviction until *all* evictable buffers have been removed from the | |
2589 | * state. As a result, if concurrent insertions into the state are | |
2590 | * allowed (e.g. if the ARC isn't shutting down), this function might | |
2591 | * wind up in an infinite loop, continually trying to evict buffers. | |
2592 | */ | |
2593 | static uint64_t | |
2594 | arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, | |
2595 | boolean_t retry) | |
2596 | { | |
2597 | uint64_t evicted = 0; | |
2598 | ||
2599 | while (state->arcs_lsize[type] != 0) { | |
2600 | evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type); | |
2601 | ||
2602 | if (!retry) | |
2603 | break; | |
2604 | } | |
2605 | ||
2606 | return (evicted); | |
34dc7c2f BB |
2607 | } |
2608 | ||
ab26409d | 2609 | /* |
f6046738 BB |
2610 | * Helper function for arc_prune() it is responsible for safely handling |
2611 | * the execution of a registered arc_prune_func_t. | |
ab26409d BB |
2612 | */ |
2613 | static void | |
f6046738 | 2614 | arc_prune_task(void *ptr) |
ab26409d | 2615 | { |
f6046738 BB |
2616 | arc_prune_t *ap = (arc_prune_t *)ptr; |
2617 | arc_prune_func_t *func = ap->p_pfunc; | |
ab26409d | 2618 | |
f6046738 BB |
2619 | if (func != NULL) |
2620 | func(ap->p_adjust, ap->p_private); | |
ab26409d | 2621 | |
f6046738 BB |
2622 | /* Callback unregistered concurrently with execution */ |
2623 | if (refcount_remove(&ap->p_refcnt, func) == 0) { | |
2624 | ASSERT(!list_link_active(&ap->p_node)); | |
2625 | refcount_destroy(&ap->p_refcnt); | |
2626 | kmem_free(ap, sizeof (*ap)); | |
2627 | } | |
2628 | } | |
ab26409d | 2629 | |
f6046738 BB |
2630 | /* |
2631 | * Notify registered consumers they must drop holds on a portion of the ARC | |
2632 | * buffered they reference. This provides a mechanism to ensure the ARC can | |
2633 | * honor the arc_meta_limit and reclaim otherwise pinned ARC buffers. This | |
2634 | * is analogous to dnlc_reduce_cache() but more generic. | |
2635 | * | |
2636 | * This operation is performed asyncronously so it may be safely called | |
ca67b33a | 2637 | * in the context of the arc_reclaim_thread(). A reference is taken here |
f6046738 BB |
2638 | * for each registered arc_prune_t and the arc_prune_task() is responsible |
2639 | * for releasing it once the registered arc_prune_func_t has completed. | |
2640 | */ | |
2641 | static void | |
2642 | arc_prune_async(int64_t adjust) | |
2643 | { | |
2644 | arc_prune_t *ap; | |
ab26409d | 2645 | |
f6046738 BB |
2646 | mutex_enter(&arc_prune_mtx); |
2647 | for (ap = list_head(&arc_prune_list); ap != NULL; | |
2648 | ap = list_next(&arc_prune_list, ap)) { | |
ab26409d | 2649 | |
f6046738 BB |
2650 | if (refcount_count(&ap->p_refcnt) >= 2) |
2651 | continue; | |
ab26409d | 2652 | |
f6046738 BB |
2653 | refcount_add(&ap->p_refcnt, ap->p_pfunc); |
2654 | ap->p_adjust = adjust; | |
2655 | taskq_dispatch(arc_prune_taskq, arc_prune_task, ap, TQ_SLEEP); | |
2656 | ARCSTAT_BUMP(arcstat_prune); | |
ab26409d | 2657 | } |
ab26409d BB |
2658 | mutex_exit(&arc_prune_mtx); |
2659 | } | |
2660 | ||
f6046738 BB |
2661 | static void |
2662 | arc_prune(int64_t adjust) | |
2663 | { | |
2664 | arc_prune_async(adjust); | |
2665 | taskq_wait_outstanding(arc_prune_taskq, 0); | |
2666 | } | |
2667 | ||
ca0bf58d PS |
2668 | /* |
2669 | * Evict the specified number of bytes from the state specified, | |
2670 | * restricting eviction to the spa and type given. This function | |
2671 | * prevents us from trying to evict more from a state's list than | |
2672 | * is "evictable", and to skip evicting altogether when passed a | |
2673 | * negative value for "bytes". In contrast, arc_evict_state() will | |
2674 | * evict everything it can, when passed a negative value for "bytes". | |
2675 | */ | |
2676 | static uint64_t | |
2677 | arc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes, | |
2678 | arc_buf_contents_t type) | |
2679 | { | |
2680 | int64_t delta; | |
2681 | ||
2682 | if (bytes > 0 && state->arcs_lsize[type] > 0) { | |
2683 | delta = MIN(state->arcs_lsize[type], bytes); | |
2684 | return (arc_evict_state(state, spa, delta, type)); | |
2685 | } | |
2686 | ||
2687 | return (0); | |
2688 | } | |
2689 | ||
2690 | /* | |
2691 | * The goal of this function is to evict enough meta data buffers from the | |
2692 | * ARC in order to enforce the arc_meta_limit. Achieving this is slightly | |
2693 | * more complicated than it appears because it is common for data buffers | |
2694 | * to have holds on meta data buffers. In addition, dnode meta data buffers | |
2695 | * will be held by the dnodes in the block preventing them from being freed. | |
2696 | * This means we can't simply traverse the ARC and expect to always find | |
2697 | * enough unheld meta data buffer to release. | |
2698 | * | |
2699 | * Therefore, this function has been updated to make alternating passes | |
2700 | * over the ARC releasing data buffers and then newly unheld meta data | |
2701 | * buffers. This ensures forward progress is maintained and arc_meta_used | |
2702 | * will decrease. Normally this is sufficient, but if required the ARC | |
2703 | * will call the registered prune callbacks causing dentry and inodes to | |
2704 | * be dropped from the VFS cache. This will make dnode meta data buffers | |
2705 | * available for reclaim. | |
2706 | */ | |
2707 | static uint64_t | |
f6046738 | 2708 | arc_adjust_meta_balanced(void) |
ca0bf58d PS |
2709 | { |
2710 | int64_t adjustmnt, delta, prune = 0; | |
2711 | uint64_t total_evicted = 0; | |
2712 | arc_buf_contents_t type = ARC_BUFC_DATA; | |
ca67b33a | 2713 | int restarts = MAX(zfs_arc_meta_adjust_restarts, 0); |
ca0bf58d PS |
2714 | |
2715 | restart: | |
2716 | /* | |
2717 | * This slightly differs than the way we evict from the mru in | |
2718 | * arc_adjust because we don't have a "target" value (i.e. no | |
2719 | * "meta" arc_p). As a result, I think we can completely | |
2720 | * cannibalize the metadata in the MRU before we evict the | |
2721 | * metadata from the MFU. I think we probably need to implement a | |
2722 | * "metadata arc_p" value to do this properly. | |
2723 | */ | |
2724 | adjustmnt = arc_meta_used - arc_meta_limit; | |
2725 | ||
2726 | if (adjustmnt > 0 && arc_mru->arcs_lsize[type] > 0) { | |
2727 | delta = MIN(arc_mru->arcs_lsize[type], adjustmnt); | |
2728 | total_evicted += arc_adjust_impl(arc_mru, 0, delta, type); | |
2729 | adjustmnt -= delta; | |
2730 | } | |
2731 | ||
2732 | /* | |
2733 | * We can't afford to recalculate adjustmnt here. If we do, | |
2734 | * new metadata buffers can sneak into the MRU or ANON lists, | |
2735 | * thus penalize the MFU metadata. Although the fudge factor is | |
2736 | * small, it has been empirically shown to be significant for | |
2737 | * certain workloads (e.g. creating many empty directories). As | |
2738 | * such, we use the original calculation for adjustmnt, and | |
2739 | * simply decrement the amount of data evicted from the MRU. | |
2740 | */ | |
2741 | ||
2742 | if (adjustmnt > 0 && arc_mfu->arcs_lsize[type] > 0) { | |
2743 | delta = MIN(arc_mfu->arcs_lsize[type], adjustmnt); | |
2744 | total_evicted += arc_adjust_impl(arc_mfu, 0, delta, type); | |
2745 | } | |
2746 | ||
2747 | adjustmnt = arc_meta_used - arc_meta_limit; | |
2748 | ||
2749 | if (adjustmnt > 0 && arc_mru_ghost->arcs_lsize[type] > 0) { | |
2750 | delta = MIN(adjustmnt, | |
2751 | arc_mru_ghost->arcs_lsize[type]); | |
2752 | total_evicted += arc_adjust_impl(arc_mru_ghost, 0, delta, type); | |
2753 | adjustmnt -= delta; | |
2754 | } | |
2755 | ||
2756 | if (adjustmnt > 0 && arc_mfu_ghost->arcs_lsize[type] > 0) { | |
2757 | delta = MIN(adjustmnt, | |
2758 | arc_mfu_ghost->arcs_lsize[type]); | |
2759 | total_evicted += arc_adjust_impl(arc_mfu_ghost, 0, delta, type); | |
2760 | } | |
2761 | ||
2762 | /* | |
2763 | * If after attempting to make the requested adjustment to the ARC | |
2764 | * the meta limit is still being exceeded then request that the | |
2765 | * higher layers drop some cached objects which have holds on ARC | |
2766 | * meta buffers. Requests to the upper layers will be made with | |
2767 | * increasingly large scan sizes until the ARC is below the limit. | |
2768 | */ | |
2769 | if (arc_meta_used > arc_meta_limit) { | |
2770 | if (type == ARC_BUFC_DATA) { | |
2771 | type = ARC_BUFC_METADATA; | |
2772 | } else { | |
2773 | type = ARC_BUFC_DATA; | |
2774 | ||
2775 | if (zfs_arc_meta_prune) { | |
2776 | prune += zfs_arc_meta_prune; | |
f6046738 | 2777 | arc_prune_async(prune); |
ca0bf58d PS |
2778 | } |
2779 | } | |
2780 | ||
2781 | if (restarts > 0) { | |
2782 | restarts--; | |
2783 | goto restart; | |
2784 | } | |
2785 | } | |
2786 | return (total_evicted); | |
2787 | } | |
2788 | ||
f6046738 BB |
2789 | /* |
2790 | * Evict metadata buffers from the cache, such that arc_meta_used is | |
2791 | * capped by the arc_meta_limit tunable. | |
2792 | */ | |
2793 | static uint64_t | |
2794 | arc_adjust_meta_only(void) | |
2795 | { | |
2796 | uint64_t total_evicted = 0; | |
2797 | int64_t target; | |
2798 | ||
2799 | /* | |
2800 | * If we're over the meta limit, we want to evict enough | |
2801 | * metadata to get back under the meta limit. We don't want to | |
2802 | * evict so much that we drop the MRU below arc_p, though. If | |
2803 | * we're over the meta limit more than we're over arc_p, we | |
2804 | * evict some from the MRU here, and some from the MFU below. | |
2805 | */ | |
2806 | target = MIN((int64_t)(arc_meta_used - arc_meta_limit), | |
2807 | (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size - arc_p)); | |
2808 | ||
2809 | total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); | |
2810 | ||
2811 | /* | |
2812 | * Similar to the above, we want to evict enough bytes to get us | |
2813 | * below the meta limit, but not so much as to drop us below the | |
2814 | * space alloted to the MFU (which is defined as arc_c - arc_p). | |
2815 | */ | |
2816 | target = MIN((int64_t)(arc_meta_used - arc_meta_limit), | |
2817 | (int64_t)(arc_mfu->arcs_size - (arc_c - arc_p))); | |
2818 | ||
2819 | total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); | |
2820 | ||
2821 | return (total_evicted); | |
2822 | } | |
2823 | ||
2824 | static uint64_t | |
2825 | arc_adjust_meta(void) | |
2826 | { | |
2827 | if (zfs_arc_meta_strategy == ARC_STRATEGY_META_ONLY) | |
2828 | return (arc_adjust_meta_only()); | |
2829 | else | |
2830 | return (arc_adjust_meta_balanced()); | |
2831 | } | |
2832 | ||
ca0bf58d PS |
2833 | /* |
2834 | * Return the type of the oldest buffer in the given arc state | |
2835 | * | |
2836 | * This function will select a random sublist of type ARC_BUFC_DATA and | |
2837 | * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist | |
2838 | * is compared, and the type which contains the "older" buffer will be | |
2839 | * returned. | |
2840 | */ | |
2841 | static arc_buf_contents_t | |
2842 | arc_adjust_type(arc_state_t *state) | |
2843 | { | |
2844 | multilist_t *data_ml = &state->arcs_list[ARC_BUFC_DATA]; | |
2845 | multilist_t *meta_ml = &state->arcs_list[ARC_BUFC_METADATA]; | |
2846 | int data_idx = multilist_get_random_index(data_ml); | |
2847 | int meta_idx = multilist_get_random_index(meta_ml); | |
2848 | multilist_sublist_t *data_mls; | |
2849 | multilist_sublist_t *meta_mls; | |
2850 | arc_buf_contents_t type; | |
2851 | arc_buf_hdr_t *data_hdr; | |
2852 | arc_buf_hdr_t *meta_hdr; | |
2853 | ||
2854 | /* | |
2855 | * We keep the sublist lock until we're finished, to prevent | |
2856 | * the headers from being destroyed via arc_evict_state(). | |
2857 | */ | |
2858 | data_mls = multilist_sublist_lock(data_ml, data_idx); | |
2859 | meta_mls = multilist_sublist_lock(meta_ml, meta_idx); | |
2860 | ||
2861 | /* | |
2862 | * These two loops are to ensure we skip any markers that | |
2863 | * might be at the tail of the lists due to arc_evict_state(). | |
2864 | */ | |
2865 | ||
2866 | for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL; | |
2867 | data_hdr = multilist_sublist_prev(data_mls, data_hdr)) { | |
2868 | if (data_hdr->b_spa != 0) | |
2869 | break; | |
2870 | } | |
2871 | ||
2872 | for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL; | |
2873 | meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) { | |
2874 | if (meta_hdr->b_spa != 0) | |
2875 | break; | |
2876 | } | |
2877 | ||
2878 | if (data_hdr == NULL && meta_hdr == NULL) { | |
2879 | type = ARC_BUFC_DATA; | |
2880 | } else if (data_hdr == NULL) { | |
2881 | ASSERT3P(meta_hdr, !=, NULL); | |
2882 | type = ARC_BUFC_METADATA; | |
2883 | } else if (meta_hdr == NULL) { | |
2884 | ASSERT3P(data_hdr, !=, NULL); | |
2885 | type = ARC_BUFC_DATA; | |
2886 | } else { | |
2887 | ASSERT3P(data_hdr, !=, NULL); | |
2888 | ASSERT3P(meta_hdr, !=, NULL); | |
2889 | ||
2890 | /* The headers can't be on the sublist without an L1 header */ | |
2891 | ASSERT(HDR_HAS_L1HDR(data_hdr)); | |
2892 | ASSERT(HDR_HAS_L1HDR(meta_hdr)); | |
2893 | ||
2894 | if (data_hdr->b_l1hdr.b_arc_access < | |
2895 | meta_hdr->b_l1hdr.b_arc_access) { | |
2896 | type = ARC_BUFC_DATA; | |
2897 | } else { | |
2898 | type = ARC_BUFC_METADATA; | |
2899 | } | |
2900 | } | |
2901 | ||
2902 | multilist_sublist_unlock(meta_mls); | |
2903 | multilist_sublist_unlock(data_mls); | |
2904 | ||
2905 | return (type); | |
2906 | } | |
2907 | ||
2908 | /* | |
2909 | * Evict buffers from the cache, such that arc_size is capped by arc_c. | |
2910 | */ | |
2911 | static uint64_t | |
2912 | arc_adjust(void) | |
2913 | { | |
2914 | uint64_t total_evicted = 0; | |
2915 | uint64_t bytes; | |
2916 | int64_t target; | |
2917 | ||
2918 | /* | |
2919 | * If we're over arc_meta_limit, we want to correct that before | |
2920 | * potentially evicting data buffers below. | |
2921 | */ | |
2922 | total_evicted += arc_adjust_meta(); | |
2923 | ||
2924 | /* | |
2925 | * Adjust MRU size | |
2926 | * | |
2927 | * If we're over the target cache size, we want to evict enough | |
2928 | * from the list to get back to our target size. We don't want | |
2929 | * to evict too much from the MRU, such that it drops below | |
2930 | * arc_p. So, if we're over our target cache size more than | |
2931 | * the MRU is over arc_p, we'll evict enough to get back to | |
2932 | * arc_p here, and then evict more from the MFU below. | |
2933 | */ | |
2934 | target = MIN((int64_t)(arc_size - arc_c), | |
2935 | (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used - | |
2936 | arc_p)); | |
2937 | ||
2938 | /* | |
2939 | * If we're below arc_meta_min, always prefer to evict data. | |
2940 | * Otherwise, try to satisfy the requested number of bytes to | |
2941 | * evict from the type which contains older buffers; in an | |
2942 | * effort to keep newer buffers in the cache regardless of their | |
2943 | * type. If we cannot satisfy the number of bytes from this | |
2944 | * type, spill over into the next type. | |
2945 | */ | |
2946 | if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA && | |
2947 | arc_meta_used > arc_meta_min) { | |
2948 | bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); | |
2949 | total_evicted += bytes; | |
2950 | ||
2951 | /* | |
2952 | * If we couldn't evict our target number of bytes from | |
2953 | * metadata, we try to get the rest from data. | |
2954 | */ | |
2955 | target -= bytes; | |
2956 | ||
2957 | total_evicted += | |
2958 | arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); | |
2959 | } else { | |
2960 | bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA); | |
2961 | total_evicted += bytes; | |
2962 | ||
2963 | /* | |
2964 | * If we couldn't evict our target number of bytes from | |
2965 | * data, we try to get the rest from metadata. | |
2966 | */ | |
2967 | target -= bytes; | |
2968 | ||
2969 | total_evicted += | |
2970 | arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA); | |
2971 | } | |
2972 | ||
2973 | /* | |
2974 | * Adjust MFU size | |
2975 | * | |
2976 | * Now that we've tried to evict enough from the MRU to get its | |
2977 | * size back to arc_p, if we're still above the target cache | |
2978 | * size, we evict the rest from the MFU. | |
2979 | */ | |
2980 | target = arc_size - arc_c; | |
2981 | ||
a7b10a93 | 2982 | if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA && |
ca0bf58d PS |
2983 | arc_meta_used > arc_meta_min) { |
2984 | bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); | |
2985 | total_evicted += bytes; | |
2986 | ||
2987 | /* | |
2988 | * If we couldn't evict our target number of bytes from | |
2989 | * metadata, we try to get the rest from data. | |
2990 | */ | |
2991 | target -= bytes; | |
2992 | ||
2993 | total_evicted += | |
2994 | arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); | |
2995 | } else { | |
2996 | bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA); | |
2997 | total_evicted += bytes; | |
2998 | ||
2999 | /* | |
3000 | * If we couldn't evict our target number of bytes from | |
3001 | * data, we try to get the rest from data. | |
3002 | */ | |
3003 | target -= bytes; | |
3004 | ||
3005 | total_evicted += | |
3006 | arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA); | |
3007 | } | |
3008 | ||
3009 | /* | |
3010 | * Adjust ghost lists | |
3011 | * | |
3012 | * In addition to the above, the ARC also defines target values | |
3013 | * for the ghost lists. The sum of the mru list and mru ghost | |
3014 | * list should never exceed the target size of the cache, and | |
3015 | * the sum of the mru list, mfu list, mru ghost list, and mfu | |
3016 | * ghost list should never exceed twice the target size of the | |
3017 | * cache. The following logic enforces these limits on the ghost | |
3018 | * caches, and evicts from them as needed. | |
3019 | */ | |
3020 | target = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c; | |
3021 | ||
3022 | bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA); | |
3023 | total_evicted += bytes; | |
3024 | ||
3025 | target -= bytes; | |
3026 | ||
3027 | total_evicted += | |
3028 | arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA); | |
3029 | ||
3030 | /* | |
3031 | * We assume the sum of the mru list and mfu list is less than | |
3032 | * or equal to arc_c (we enforced this above), which means we | |
3033 | * can use the simpler of the two equations below: | |
3034 | * | |
3035 | * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c | |
3036 | * mru ghost + mfu ghost <= arc_c | |
3037 | */ | |
3038 | target = arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c; | |
3039 | ||
3040 | bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA); | |
3041 | total_evicted += bytes; | |
3042 | ||
3043 | target -= bytes; | |
3044 | ||
3045 | total_evicted += | |
3046 | arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA); | |
3047 | ||
3048 | return (total_evicted); | |
3049 | } | |
3050 | ||
34dc7c2f BB |
3051 | static void |
3052 | arc_do_user_evicts(void) | |
3053 | { | |
ca0bf58d | 3054 | mutex_enter(&arc_user_evicts_lock); |
34dc7c2f BB |
3055 | while (arc_eviction_list != NULL) { |
3056 | arc_buf_t *buf = arc_eviction_list; | |
3057 | arc_eviction_list = buf->b_next; | |
428870ff | 3058 | mutex_enter(&buf->b_evict_lock); |
34dc7c2f | 3059 | buf->b_hdr = NULL; |
428870ff | 3060 | mutex_exit(&buf->b_evict_lock); |
ca0bf58d | 3061 | mutex_exit(&arc_user_evicts_lock); |
34dc7c2f BB |
3062 | |
3063 | if (buf->b_efunc != NULL) | |
bd089c54 | 3064 | VERIFY0(buf->b_efunc(buf->b_private)); |
34dc7c2f BB |
3065 | |
3066 | buf->b_efunc = NULL; | |
3067 | buf->b_private = NULL; | |
3068 | kmem_cache_free(buf_cache, buf); | |
ca0bf58d | 3069 | mutex_enter(&arc_user_evicts_lock); |
34dc7c2f | 3070 | } |
ca0bf58d | 3071 | mutex_exit(&arc_user_evicts_lock); |
34dc7c2f BB |
3072 | } |
3073 | ||
ca0bf58d PS |
3074 | void |
3075 | arc_flush(spa_t *spa, boolean_t retry) | |
ab26409d | 3076 | { |
ca0bf58d | 3077 | uint64_t guid = 0; |
94520ca4 | 3078 | |
bc888666 | 3079 | /* |
ca0bf58d PS |
3080 | * If retry is TRUE, a spa must not be specified since we have |
3081 | * no good way to determine if all of a spa's buffers have been | |
3082 | * evicted from an arc state. | |
bc888666 | 3083 | */ |
ca0bf58d | 3084 | ASSERT(!retry || spa == 0); |
d164b209 | 3085 | |
b9541d6b | 3086 | if (spa != NULL) |
3541dc6d | 3087 | guid = spa_load_guid(spa); |
d164b209 | 3088 | |
ca0bf58d PS |
3089 | (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); |
3090 | (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); | |
3091 | ||
3092 | (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); | |
3093 | (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); | |
3094 | ||
3095 | (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); | |
3096 | (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); | |
34dc7c2f | 3097 | |
ca0bf58d PS |
3098 | (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); |
3099 | (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); | |
34dc7c2f | 3100 | |
34dc7c2f | 3101 | arc_do_user_evicts(); |
34dc7c2f BB |
3102 | ASSERT(spa || arc_eviction_list == NULL); |
3103 | } | |
3104 | ||
34dc7c2f | 3105 | void |
ca67b33a | 3106 | arc_shrink(int64_t to_free) |
34dc7c2f BB |
3107 | { |
3108 | if (arc_c > arc_c_min) { | |
302f753f | 3109 | |
34dc7c2f BB |
3110 | if (arc_c > arc_c_min + to_free) |
3111 | atomic_add_64(&arc_c, -to_free); | |
3112 | else | |
3113 | arc_c = arc_c_min; | |
3114 | ||
ca67b33a | 3115 | atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift)); |
34dc7c2f BB |
3116 | if (arc_c > arc_size) |
3117 | arc_c = MAX(arc_size, arc_c_min); | |
3118 | if (arc_p > arc_c) | |
3119 | arc_p = (arc_c >> 1); | |
3120 | ASSERT(arc_c >= arc_c_min); | |
3121 | ASSERT((int64_t)arc_p >= 0); | |
3122 | } | |
3123 | ||
3124 | if (arc_size > arc_c) | |
ca0bf58d | 3125 | (void) arc_adjust(); |
34dc7c2f BB |
3126 | } |
3127 | ||
ca67b33a MA |
3128 | typedef enum free_memory_reason_t { |
3129 | FMR_UNKNOWN, | |
3130 | FMR_NEEDFREE, | |
3131 | FMR_LOTSFREE, | |
3132 | FMR_SWAPFS_MINFREE, | |
3133 | FMR_PAGES_PP_MAXIMUM, | |
3134 | FMR_HEAP_ARENA, | |
3135 | FMR_ZIO_ARENA, | |
3136 | } free_memory_reason_t; | |
3137 | ||
3138 | int64_t last_free_memory; | |
3139 | free_memory_reason_t last_free_reason; | |
3140 | ||
3141 | #ifdef _KERNEL | |
3142 | #ifdef __linux__ | |
3143 | /* | |
3144 | * expiration time for arc_no_grow set by direct memory reclaim. | |
3145 | */ | |
3146 | static clock_t arc_grow_time = 0; | |
3147 | #else | |
3148 | /* | |
3149 | * Additional reserve of pages for pp_reserve. | |
3150 | */ | |
3151 | int64_t arc_pages_pp_reserve = 64; | |
3152 | ||
3153 | /* | |
3154 | * Additional reserve of pages for swapfs. | |
3155 | */ | |
3156 | int64_t arc_swapfs_reserve = 64; | |
3157 | #endif | |
3158 | #endif /* _KERNEL */ | |
3159 | ||
3160 | /* | |
3161 | * Return the amount of memory that can be consumed before reclaim will be | |
3162 | * needed. Positive if there is sufficient free memory, negative indicates | |
3163 | * the amount of memory that needs to be freed up. | |
3164 | */ | |
3165 | static int64_t | |
3166 | arc_available_memory(void) | |
3167 | { | |
3168 | int64_t lowest = INT64_MAX; | |
3169 | free_memory_reason_t r = FMR_UNKNOWN; | |
3170 | ||
3171 | #ifdef _KERNEL | |
3172 | #ifdef __linux__ | |
3173 | /* | |
3174 | * Under Linux we are not allowed to directly interrogate the global | |
3175 | * memory state. Instead rely on observing that direct reclaim has | |
3176 | * recently occurred therefore the system must be low on memory. The | |
3177 | * exact values returned are not critical but should be small. | |
3178 | */ | |
3179 | if (ddi_time_after_eq(ddi_get_lbolt(), arc_grow_time)) | |
3180 | lowest = PAGE_SIZE; | |
3181 | else | |
3182 | lowest = -PAGE_SIZE; | |
3183 | #else | |
3184 | int64_t n; | |
3185 | ||
3186 | /* | |
3187 | * Platforms like illumos have greater visibility in to the memory | |
3188 | * subsystem and can return a more detailed analysis of memory. | |
3189 | */ | |
3190 | if (needfree > 0) { | |
3191 | n = PAGESIZE * (-needfree); | |
3192 | if (n < lowest) { | |
3193 | lowest = n; | |
3194 | r = FMR_NEEDFREE; | |
3195 | } | |
3196 | } | |
3197 | ||
3198 | /* | |
3199 | * check that we're out of range of the pageout scanner. It starts to | |
3200 | * schedule paging if freemem is less than lotsfree and needfree. | |
3201 | * lotsfree is the high-water mark for pageout, and needfree is the | |
3202 | * number of needed free pages. We add extra pages here to make sure | |
3203 | * the scanner doesn't start up while we're freeing memory. | |
3204 | */ | |
3205 | n = PAGESIZE * (freemem - lotsfree - needfree - desfree); | |
3206 | if (n < lowest) { | |
3207 | lowest = n; | |
3208 | r = FMR_LOTSFREE; | |
3209 | } | |
3210 | ||
3211 | /* | |
3212 | * check to make sure that swapfs has enough space so that anon | |
3213 | * reservations can still succeed. anon_resvmem() checks that the | |
3214 | * availrmem is greater than swapfs_minfree, and the number of reserved | |
3215 | * swap pages. We also add a bit of extra here just to prevent | |
3216 | * circumstances from getting really dire. | |
3217 | */ | |
3218 | n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve - | |
3219 | desfree - arc_swapfs_reserve); | |
3220 | if (n < lowest) { | |
3221 | lowest = n; | |
3222 | r = FMR_SWAPFS_MINFREE; | |
3223 | } | |
3224 | ||
3225 | ||
3226 | /* | |
3227 | * Check that we have enough availrmem that memory locking (e.g., via | |
3228 | * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum | |
3229 | * stores the number of pages that cannot be locked; when availrmem | |
3230 | * drops below pages_pp_maximum, page locking mechanisms such as | |
3231 | * page_pp_lock() will fail.) | |
3232 | */ | |
3233 | n = PAGESIZE * (availrmem - pages_pp_maximum - | |
3234 | arc_pages_pp_reserve); | |
3235 | if (n < lowest) { | |
3236 | lowest = n; | |
3237 | r = FMR_PAGES_PP_MAXIMUM; | |
3238 | } | |
3239 | ||
3240 | #if defined(__i386) | |
3241 | /* | |
3242 | * If we're on an i386 platform, it's possible that we'll exhaust the | |
3243 | * kernel heap space before we ever run out of available physical | |
3244 | * memory. Most checks of the size of the heap_area compare against | |
3245 | * tune.t_minarmem, which is the minimum available real memory that we | |
3246 | * can have in the system. However, this is generally fixed at 25 pages | |
3247 | * which is so low that it's useless. In this comparison, we seek to | |
3248 | * calculate the total heap-size, and reclaim if more than 3/4ths of the | |
3249 | * heap is allocated. (Or, in the calculation, if less than 1/4th is | |
3250 | * free) | |
3251 | */ | |
3252 | n = vmem_size(heap_arena, VMEM_FREE) - | |
3253 | (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2); | |
3254 | if (n < lowest) { | |
3255 | lowest = n; | |
3256 | r = FMR_HEAP_ARENA; | |
3257 | } | |
3258 | #endif | |
3259 | ||
3260 | /* | |
3261 | * If zio data pages are being allocated out of a separate heap segment, | |
3262 | * then enforce that the size of available vmem for this arena remains | |
3263 | * above about 1/16th free. | |
3264 | * | |
3265 | * Note: The 1/16th arena free requirement was put in place | |
3266 | * to aggressively evict memory from the arc in order to avoid | |
3267 | * memory fragmentation issues. | |
3268 | */ | |
3269 | if (zio_arena != NULL) { | |
3270 | n = vmem_size(zio_arena, VMEM_FREE) - | |
3271 | (vmem_size(zio_arena, VMEM_ALLOC) >> 4); | |
3272 | if (n < lowest) { | |
3273 | lowest = n; | |
3274 | r = FMR_ZIO_ARENA; | |
3275 | } | |
3276 | } | |
3277 | #endif /* __linux__ */ | |
3278 | #else | |
3279 | /* Every 100 calls, free a small amount */ | |
3280 | if (spa_get_random(100) == 0) | |
3281 | lowest = -1024; | |
3282 | #endif | |
3283 | ||
3284 | last_free_memory = lowest; | |
3285 | last_free_reason = r; | |
3286 | ||
3287 | return (lowest); | |
3288 | } | |
3289 | ||
3290 | /* | |
3291 | * Determine if the system is under memory pressure and is asking | |
3292 | * to reclaim memory. A return value of TRUE indicates that the system | |
3293 | * is under memory pressure and that the arc should adjust accordingly. | |
3294 | */ | |
3295 | static boolean_t | |
3296 | arc_reclaim_needed(void) | |
3297 | { | |
3298 | return (arc_available_memory() < 0); | |
3299 | } | |
3300 | ||
34dc7c2f | 3301 | static void |
ca67b33a | 3302 | arc_kmem_reap_now(void) |
34dc7c2f BB |
3303 | { |
3304 | size_t i; | |
3305 | kmem_cache_t *prev_cache = NULL; | |
3306 | kmem_cache_t *prev_data_cache = NULL; | |
3307 | extern kmem_cache_t *zio_buf_cache[]; | |
3308 | extern kmem_cache_t *zio_data_buf_cache[]; | |
669dedb3 | 3309 | extern kmem_cache_t *range_seg_cache; |
34dc7c2f | 3310 | |
f6046738 BB |
3311 | if ((arc_meta_used >= arc_meta_limit) && zfs_arc_meta_prune) { |
3312 | /* | |
3313 | * We are exceeding our meta-data cache limit. | |
3314 | * Prune some entries to release holds on meta-data. | |
3315 | */ | |
3316 | arc_prune(zfs_arc_meta_prune); | |
3317 | } | |
3318 | ||
34dc7c2f BB |
3319 | for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { |
3320 | if (zio_buf_cache[i] != prev_cache) { | |
3321 | prev_cache = zio_buf_cache[i]; | |
3322 | kmem_cache_reap_now(zio_buf_cache[i]); | |
3323 | } | |
3324 | if (zio_data_buf_cache[i] != prev_data_cache) { | |
3325 | prev_data_cache = zio_data_buf_cache[i]; | |
3326 | kmem_cache_reap_now(zio_data_buf_cache[i]); | |
3327 | } | |
3328 | } | |
ca0bf58d | 3329 | kmem_cache_reap_now(buf_cache); |
b9541d6b CW |
3330 | kmem_cache_reap_now(hdr_full_cache); |
3331 | kmem_cache_reap_now(hdr_l2only_cache); | |
669dedb3 | 3332 | kmem_cache_reap_now(range_seg_cache); |
ca67b33a MA |
3333 | |
3334 | if (zio_arena != NULL) { | |
3335 | /* | |
3336 | * Ask the vmem arena to reclaim unused memory from its | |
3337 | * quantum caches. | |
3338 | */ | |
3339 | vmem_qcache_reap(zio_arena); | |
3340 | } | |
34dc7c2f BB |
3341 | } |
3342 | ||
302f753f | 3343 | /* |
ca0bf58d PS |
3344 | * Threads can block in arc_get_data_buf() waiting for this thread to evict |
3345 | * enough data and signal them to proceed. When this happens, the threads in | |
3346 | * arc_get_data_buf() are sleeping while holding the hash lock for their | |
3347 | * particular arc header. Thus, we must be careful to never sleep on a | |
3348 | * hash lock in this thread. This is to prevent the following deadlock: | |
3349 | * | |
3350 | * - Thread A sleeps on CV in arc_get_data_buf() holding hash lock "L", | |
3351 | * waiting for the reclaim thread to signal it. | |
3352 | * | |
3353 | * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter, | |
3354 | * fails, and goes to sleep forever. | |
3355 | * | |
3356 | * This possible deadlock is avoided by always acquiring a hash lock | |
3357 | * using mutex_tryenter() from arc_reclaim_thread(). | |
302f753f | 3358 | */ |
34dc7c2f | 3359 | static void |
ca67b33a | 3360 | arc_reclaim_thread(void) |
34dc7c2f | 3361 | { |
ca67b33a MA |
3362 | fstrans_cookie_t cookie = spl_fstrans_mark(); |
3363 | clock_t growtime = 0; | |
34dc7c2f BB |
3364 | callb_cpr_t cpr; |
3365 | ||
ca0bf58d | 3366 | CALLB_CPR_INIT(&cpr, &arc_reclaim_lock, callb_generic_cpr, FTAG); |
34dc7c2f | 3367 | |
ca0bf58d | 3368 | mutex_enter(&arc_reclaim_lock); |
ca67b33a MA |
3369 | while (!arc_reclaim_thread_exit) { |
3370 | int64_t to_free; | |
3371 | int64_t free_memory = arc_available_memory(); | |
3372 | uint64_t evicted = 0; | |
302f753f | 3373 | |
ca67b33a | 3374 | arc_tuning_update(); |
34dc7c2f | 3375 | |
ca67b33a | 3376 | mutex_exit(&arc_reclaim_lock); |
34dc7c2f | 3377 | |
ca67b33a | 3378 | if (free_memory < 0) { |
34dc7c2f | 3379 | |
ca67b33a | 3380 | arc_no_grow = B_TRUE; |
b128c09f | 3381 | arc_warm = B_TRUE; |
34dc7c2f | 3382 | |
ca67b33a MA |
3383 | /* |
3384 | * Wait at least zfs_grow_retry (default 5) seconds | |
3385 | * before considering growing. | |
3386 | */ | |
3387 | growtime = ddi_get_lbolt() + (arc_grow_retry * hz); | |
6a8f9b6b | 3388 | |
ca67b33a | 3389 | arc_kmem_reap_now(); |
34dc7c2f | 3390 | |
ca67b33a MA |
3391 | /* |
3392 | * If we are still low on memory, shrink the ARC | |
3393 | * so that we have arc_shrink_min free space. | |
3394 | */ | |
3395 | free_memory = arc_available_memory(); | |
34dc7c2f | 3396 | |
ca67b33a MA |
3397 | to_free = (arc_c >> arc_shrink_shift) - free_memory; |
3398 | if (to_free > 0) { | |
3399 | #ifdef _KERNEL | |
3400 | to_free = MAX(to_free, ptob(needfree)); | |
3401 | #endif | |
3402 | arc_shrink(to_free); | |
3403 | } | |
3404 | } else if (free_memory < arc_c >> arc_no_grow_shift) { | |
3405 | arc_no_grow = B_TRUE; | |
3406 | } else if (ddi_get_lbolt() >= growtime) { | |
3407 | arc_no_grow = B_FALSE; | |
3408 | } | |
bce45ec9 | 3409 | |
ca67b33a | 3410 | evicted = arc_adjust(); |
bce45ec9 | 3411 | |
ca67b33a | 3412 | mutex_enter(&arc_reclaim_lock); |
bce45ec9 | 3413 | |
ca67b33a MA |
3414 | /* |
3415 | * If evicted is zero, we couldn't evict anything via | |
3416 | * arc_adjust(). This could be due to hash lock | |
3417 | * collisions, but more likely due to the majority of | |
3418 | * arc buffers being unevictable. Therefore, even if | |
3419 | * arc_size is above arc_c, another pass is unlikely to | |
3420 | * be helpful and could potentially cause us to enter an | |
3421 | * infinite loop. | |
3422 | */ | |
3423 | if (arc_size <= arc_c || evicted == 0) { | |
3424 | /* | |
3425 | * We're either no longer overflowing, or we | |
3426 | * can't evict anything more, so we should wake | |
3427 | * up any threads before we go to sleep. | |
3428 | */ | |
3429 | cv_broadcast(&arc_reclaim_waiters_cv); | |
bce45ec9 | 3430 | |
ca67b33a MA |
3431 | /* |
3432 | * Block until signaled, or after one second (we | |
3433 | * might need to perform arc_kmem_reap_now() | |
3434 | * even if we aren't being signalled) | |
3435 | */ | |
3436 | CALLB_CPR_SAFE_BEGIN(&cpr); | |
3437 | (void) cv_timedwait_sig(&arc_reclaim_thread_cv, | |
3438 | &arc_reclaim_lock, ddi_get_lbolt() + hz); | |
3439 | CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_lock); | |
3440 | } | |
ca0bf58d | 3441 | } |
bce45ec9 | 3442 | |
ca67b33a | 3443 | arc_reclaim_thread_exit = FALSE; |
ca0bf58d PS |
3444 | cv_broadcast(&arc_reclaim_thread_cv); |
3445 | CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_lock */ | |
3446 | spl_fstrans_unmark(cookie); | |
3447 | thread_exit(); | |
3448 | } | |
3449 | ||
3450 | static void | |
3451 | arc_user_evicts_thread(void) | |
3452 | { | |
ca67b33a | 3453 | fstrans_cookie_t cookie = spl_fstrans_mark(); |
ca0bf58d | 3454 | callb_cpr_t cpr; |
bce45ec9 | 3455 | |
ca0bf58d | 3456 | CALLB_CPR_INIT(&cpr, &arc_user_evicts_lock, callb_generic_cpr, FTAG); |
bce45ec9 | 3457 | |
ca0bf58d PS |
3458 | mutex_enter(&arc_user_evicts_lock); |
3459 | while (!arc_user_evicts_thread_exit) { | |
3460 | mutex_exit(&arc_user_evicts_lock); | |
3461 | ||
3462 | arc_do_user_evicts(); | |
3463 | ||
3464 | /* | |
3465 | * This is necessary in order for the mdb ::arc dcmd to | |
3466 | * show up to date information. Since the ::arc command | |
3467 | * does not call the kstat's update function, without | |
3468 | * this call, the command may show stale stats for the | |
3469 | * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even | |
3470 | * with this change, the data might be up to 1 second | |
3471 | * out of date; but that should suffice. The arc_state_t | |
3472 | * structures can be queried directly if more accurate | |
3473 | * information is needed. | |
3474 | */ | |
3475 | if (arc_ksp != NULL) | |
3476 | arc_ksp->ks_update(arc_ksp, KSTAT_READ); | |
3477 | ||
3478 | mutex_enter(&arc_user_evicts_lock); | |
3479 | ||
3480 | /* | |
3481 | * Block until signaled, or after one second (we need to | |
3482 | * call the arc's kstat update function regularly). | |
3483 | */ | |
3484 | CALLB_CPR_SAFE_BEGIN(&cpr); | |
b64ccd6c | 3485 | (void) cv_timedwait_sig(&arc_user_evicts_cv, |
ca0bf58d PS |
3486 | &arc_user_evicts_lock, ddi_get_lbolt() + hz); |
3487 | CALLB_CPR_SAFE_END(&cpr, &arc_user_evicts_lock); | |
34dc7c2f BB |
3488 | } |
3489 | ||
ca0bf58d PS |
3490 | arc_user_evicts_thread_exit = FALSE; |
3491 | cv_broadcast(&arc_user_evicts_cv); | |
3492 | CALLB_CPR_EXIT(&cpr); /* drops arc_user_evicts_lock */ | |
40d06e3c | 3493 | spl_fstrans_unmark(cookie); |
34dc7c2f BB |
3494 | thread_exit(); |
3495 | } | |
3496 | ||
7cb67b45 BB |
3497 | #ifdef _KERNEL |
3498 | /* | |
302f753f BB |
3499 | * Determine the amount of memory eligible for eviction contained in the |
3500 | * ARC. All clean data reported by the ghost lists can always be safely | |
3501 | * evicted. Due to arc_c_min, the same does not hold for all clean data | |
3502 | * contained by the regular mru and mfu lists. | |
3503 | * | |
3504 | * In the case of the regular mru and mfu lists, we need to report as | |
3505 | * much clean data as possible, such that evicting that same reported | |
3506 | * data will not bring arc_size below arc_c_min. Thus, in certain | |
3507 | * circumstances, the total amount of clean data in the mru and mfu | |
3508 | * lists might not actually be evictable. | |
3509 | * | |
3510 | * The following two distinct cases are accounted for: | |
3511 | * | |
3512 | * 1. The sum of the amount of dirty data contained by both the mru and | |
3513 | * mfu lists, plus the ARC's other accounting (e.g. the anon list), | |
3514 | * is greater than or equal to arc_c_min. | |
3515 | * (i.e. amount of dirty data >= arc_c_min) | |
3516 | * | |
3517 | * This is the easy case; all clean data contained by the mru and mfu | |
3518 | * lists is evictable. Evicting all clean data can only drop arc_size | |
3519 | * to the amount of dirty data, which is greater than arc_c_min. | |
3520 | * | |
3521 | * 2. The sum of the amount of dirty data contained by both the mru and | |
3522 | * mfu lists, plus the ARC's other accounting (e.g. the anon list), | |
3523 | * is less than arc_c_min. | |
3524 | * (i.e. arc_c_min > amount of dirty data) | |
3525 | * | |
3526 | * 2.1. arc_size is greater than or equal arc_c_min. | |
3527 | * (i.e. arc_size >= arc_c_min > amount of dirty data) | |
3528 | * | |
3529 | * In this case, not all clean data from the regular mru and mfu | |
3530 | * lists is actually evictable; we must leave enough clean data | |
3531 | * to keep arc_size above arc_c_min. Thus, the maximum amount of | |
3532 | * evictable data from the two lists combined, is exactly the | |
3533 | * difference between arc_size and arc_c_min. | |
3534 | * | |
3535 | * 2.2. arc_size is less than arc_c_min | |
3536 | * (i.e. arc_c_min > arc_size > amount of dirty data) | |
3537 | * | |
3538 | * In this case, none of the data contained in the mru and mfu | |
3539 | * lists is evictable, even if it's clean. Since arc_size is | |
3540 | * already below arc_c_min, evicting any more would only | |
3541 | * increase this negative difference. | |
7cb67b45 | 3542 | */ |
302f753f BB |
3543 | static uint64_t |
3544 | arc_evictable_memory(void) { | |
3545 | uint64_t arc_clean = | |
3546 | arc_mru->arcs_lsize[ARC_BUFC_DATA] + | |
3547 | arc_mru->arcs_lsize[ARC_BUFC_METADATA] + | |
3548 | arc_mfu->arcs_lsize[ARC_BUFC_DATA] + | |
3549 | arc_mfu->arcs_lsize[ARC_BUFC_METADATA]; | |
3550 | uint64_t ghost_clean = | |
3551 | arc_mru_ghost->arcs_lsize[ARC_BUFC_DATA] + | |
3552 | arc_mru_ghost->arcs_lsize[ARC_BUFC_METADATA] + | |
3553 | arc_mfu_ghost->arcs_lsize[ARC_BUFC_DATA] + | |
3554 | arc_mfu_ghost->arcs_lsize[ARC_BUFC_METADATA]; | |
3555 | uint64_t arc_dirty = MAX((int64_t)arc_size - (int64_t)arc_clean, 0); | |
3556 | ||
3557 | if (arc_dirty >= arc_c_min) | |
3558 | return (ghost_clean + arc_clean); | |
3559 | ||
3560 | return (ghost_clean + MAX((int64_t)arc_size - (int64_t)arc_c_min, 0)); | |
3561 | } | |
3562 | ||
ed6e9cc2 TC |
3563 | /* |
3564 | * If sc->nr_to_scan is zero, the caller is requesting a query of the | |
3565 | * number of objects which can potentially be freed. If it is nonzero, | |
3566 | * the request is to free that many objects. | |
3567 | * | |
3568 | * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks | |
3569 | * in struct shrinker and also require the shrinker to return the number | |
3570 | * of objects freed. | |
3571 | * | |
3572 | * Older kernels require the shrinker to return the number of freeable | |
3573 | * objects following the freeing of nr_to_free. | |
3574 | */ | |
3575 | static spl_shrinker_t | |
7e7baeca | 3576 | __arc_shrinker_func(struct shrinker *shrink, struct shrink_control *sc) |
7cb67b45 | 3577 | { |
ed6e9cc2 | 3578 | int64_t pages; |
7cb67b45 | 3579 | |
302f753f BB |
3580 | /* The arc is considered warm once reclaim has occurred */ |
3581 | if (unlikely(arc_warm == B_FALSE)) | |
3582 | arc_warm = B_TRUE; | |
7cb67b45 | 3583 | |
302f753f | 3584 | /* Return the potential number of reclaimable pages */ |
ed6e9cc2 | 3585 | pages = btop((int64_t)arc_evictable_memory()); |
302f753f BB |
3586 | if (sc->nr_to_scan == 0) |
3587 | return (pages); | |
3fd70ee6 BB |
3588 | |
3589 | /* Not allowed to perform filesystem reclaim */ | |
7e7baeca | 3590 | if (!(sc->gfp_mask & __GFP_FS)) |
ed6e9cc2 | 3591 | return (SHRINK_STOP); |
3fd70ee6 | 3592 | |
7cb67b45 | 3593 | /* Reclaim in progress */ |
ca0bf58d | 3594 | if (mutex_tryenter(&arc_reclaim_lock) == 0) |
ed6e9cc2 | 3595 | return (SHRINK_STOP); |
7cb67b45 | 3596 | |
ca0bf58d PS |
3597 | mutex_exit(&arc_reclaim_lock); |
3598 | ||
302f753f BB |
3599 | /* |
3600 | * Evict the requested number of pages by shrinking arc_c the | |
3601 | * requested amount. If there is nothing left to evict just | |
3602 | * reap whatever we can from the various arc slabs. | |
3603 | */ | |
3604 | if (pages > 0) { | |
ca67b33a MA |
3605 | arc_shrink(ptob(sc->nr_to_scan)); |
3606 | arc_kmem_reap_now(); | |
ed6e9cc2 TC |
3607 | #ifdef HAVE_SPLIT_SHRINKER_CALLBACK |
3608 | pages = MAX(pages - btop(arc_evictable_memory()), 0); | |
3609 | #else | |
1e3cb67b | 3610 | pages = btop(arc_evictable_memory()); |
ed6e9cc2 | 3611 | #endif |
302f753f | 3612 | } else { |
ca67b33a | 3613 | arc_kmem_reap_now(); |
ed6e9cc2 | 3614 | pages = SHRINK_STOP; |
302f753f BB |
3615 | } |
3616 | ||
ca0bf58d PS |
3617 | /* |
3618 | * We've reaped what we can, wake up threads. | |
3619 | */ | |
3620 | cv_broadcast(&arc_reclaim_waiters_cv); | |
3621 | ||
302f753f BB |
3622 | /* |
3623 | * When direct reclaim is observed it usually indicates a rapid | |
3624 | * increase in memory pressure. This occurs because the kswapd | |
3625 | * threads were unable to asynchronously keep enough free memory | |
3626 | * available. In this case set arc_no_grow to briefly pause arc | |
3627 | * growth to avoid compounding the memory pressure. | |
3628 | */ | |
7cb67b45 | 3629 | if (current_is_kswapd()) { |
302f753f | 3630 | ARCSTAT_BUMP(arcstat_memory_indirect_count); |
7cb67b45 | 3631 | } else { |
302f753f | 3632 | arc_no_grow = B_TRUE; |
bce45ec9 | 3633 | arc_grow_time = ddi_get_lbolt() + (zfs_arc_grow_retry * hz); |
302f753f | 3634 | ARCSTAT_BUMP(arcstat_memory_direct_count); |
7cb67b45 BB |
3635 | } |
3636 | ||
1e3cb67b | 3637 | return (pages); |
7cb67b45 | 3638 | } |
7e7baeca | 3639 | SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func); |
7cb67b45 BB |
3640 | |
3641 | SPL_SHRINKER_DECLARE(arc_shrinker, arc_shrinker_func, DEFAULT_SEEKS); | |
3642 | #endif /* _KERNEL */ | |
3643 | ||
34dc7c2f BB |
3644 | /* |
3645 | * Adapt arc info given the number of bytes we are trying to add and | |
3646 | * the state that we are comming from. This function is only called | |
3647 | * when we are adding new content to the cache. | |
3648 | */ | |
3649 | static void | |
3650 | arc_adapt(int bytes, arc_state_t *state) | |
3651 | { | |
3652 | int mult; | |
3653 | ||
3654 | if (state == arc_l2c_only) | |
3655 | return; | |
3656 | ||
3657 | ASSERT(bytes > 0); | |
3658 | /* | |
3659 | * Adapt the target size of the MRU list: | |
3660 | * - if we just hit in the MRU ghost list, then increase | |
3661 | * the target size of the MRU list. | |
3662 | * - if we just hit in the MFU ghost list, then increase | |
3663 | * the target size of the MFU list by decreasing the | |
3664 | * target size of the MRU list. | |
3665 | */ | |
3666 | if (state == arc_mru_ghost) { | |
3667 | mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ? | |
3668 | 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size)); | |
62422785 PS |
3669 | |
3670 | if (!zfs_arc_p_dampener_disable) | |
3671 | mult = MIN(mult, 10); /* avoid wild arc_p adjustment */ | |
34dc7c2f | 3672 | |
f521ce1b | 3673 | arc_p = MIN(arc_c, arc_p + bytes * mult); |
34dc7c2f | 3674 | } else if (state == arc_mfu_ghost) { |
d164b209 BB |
3675 | uint64_t delta; |
3676 | ||
34dc7c2f BB |
3677 | mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ? |
3678 | 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size)); | |
62422785 PS |
3679 | |
3680 | if (!zfs_arc_p_dampener_disable) | |
3681 | mult = MIN(mult, 10); | |
34dc7c2f | 3682 | |
d164b209 | 3683 | delta = MIN(bytes * mult, arc_p); |
f521ce1b | 3684 | arc_p = MAX(0, arc_p - delta); |
34dc7c2f BB |
3685 | } |
3686 | ASSERT((int64_t)arc_p >= 0); | |
3687 | ||
ca67b33a MA |
3688 | if (arc_reclaim_needed()) { |
3689 | cv_signal(&arc_reclaim_thread_cv); | |
3690 | return; | |
3691 | } | |
3692 | ||
34dc7c2f BB |
3693 | if (arc_no_grow) |
3694 | return; | |
3695 | ||
3696 | if (arc_c >= arc_c_max) | |
3697 | return; | |
3698 | ||
3699 | /* | |
3700 | * If we're within (2 * maxblocksize) bytes of the target | |
3701 | * cache size, increment the target cache size | |
3702 | */ | |
121b3cae TC |
3703 | VERIFY3U(arc_c, >=, 2ULL << SPA_MAXBLOCKSHIFT); |
3704 | if (arc_size >= arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) { | |
34dc7c2f BB |
3705 | atomic_add_64(&arc_c, (int64_t)bytes); |
3706 | if (arc_c > arc_c_max) | |
3707 | arc_c = arc_c_max; | |
3708 | else if (state == arc_anon) | |
3709 | atomic_add_64(&arc_p, (int64_t)bytes); | |
3710 | if (arc_p > arc_c) | |
3711 | arc_p = arc_c; | |
3712 | } | |
3713 | ASSERT((int64_t)arc_p >= 0); | |
3714 | } | |
3715 | ||
3716 | /* | |
ca0bf58d PS |
3717 | * Check if arc_size has grown past our upper threshold, determined by |
3718 | * zfs_arc_overflow_shift. | |
34dc7c2f | 3719 | */ |
ca0bf58d PS |
3720 | static boolean_t |
3721 | arc_is_overflowing(void) | |
34dc7c2f | 3722 | { |
ca0bf58d PS |
3723 | /* Always allow at least one block of overflow */ |
3724 | uint64_t overflow = MAX(SPA_MAXBLOCKSIZE, | |
3725 | arc_c >> zfs_arc_overflow_shift); | |
34dc7c2f | 3726 | |
ca0bf58d | 3727 | return (arc_size >= arc_c + overflow); |
34dc7c2f BB |
3728 | } |
3729 | ||
3730 | /* | |
ca0bf58d PS |
3731 | * The buffer, supplied as the first argument, needs a data block. If we |
3732 | * are hitting the hard limit for the cache size, we must sleep, waiting | |
3733 | * for the eviction thread to catch up. If we're past the target size | |
3734 | * but below the hard limit, we'll only signal the reclaim thread and | |
3735 | * continue on. | |
34dc7c2f BB |
3736 | */ |
3737 | static void | |
3738 | arc_get_data_buf(arc_buf_t *buf) | |
3739 | { | |
b9541d6b | 3740 | arc_state_t *state = buf->b_hdr->b_l1hdr.b_state; |
34dc7c2f | 3741 | uint64_t size = buf->b_hdr->b_size; |
b9541d6b | 3742 | arc_buf_contents_t type = arc_buf_type(buf->b_hdr); |
34dc7c2f BB |
3743 | |
3744 | arc_adapt(size, state); | |
3745 | ||
3746 | /* | |
ca0bf58d PS |
3747 | * If arc_size is currently overflowing, and has grown past our |
3748 | * upper limit, we must be adding data faster than the evict | |
3749 | * thread can evict. Thus, to ensure we don't compound the | |
3750 | * problem by adding more data and forcing arc_size to grow even | |
3751 | * further past it's target size, we halt and wait for the | |
3752 | * eviction thread to catch up. | |
3753 | * | |
3754 | * It's also possible that the reclaim thread is unable to evict | |
3755 | * enough buffers to get arc_size below the overflow limit (e.g. | |
3756 | * due to buffers being un-evictable, or hash lock collisions). | |
3757 | * In this case, we want to proceed regardless if we're | |
3758 | * overflowing; thus we don't use a while loop here. | |
34dc7c2f | 3759 | */ |
ca0bf58d PS |
3760 | if (arc_is_overflowing()) { |
3761 | mutex_enter(&arc_reclaim_lock); | |
3762 | ||
3763 | /* | |
3764 | * Now that we've acquired the lock, we may no longer be | |
3765 | * over the overflow limit, lets check. | |
3766 | * | |
3767 | * We're ignoring the case of spurious wake ups. If that | |
3768 | * were to happen, it'd let this thread consume an ARC | |
3769 | * buffer before it should have (i.e. before we're under | |
3770 | * the overflow limit and were signalled by the reclaim | |
3771 | * thread). As long as that is a rare occurrence, it | |
3772 | * shouldn't cause any harm. | |
3773 | */ | |
3774 | if (arc_is_overflowing()) { | |
3775 | cv_signal(&arc_reclaim_thread_cv); | |
3776 | cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock); | |
34dc7c2f | 3777 | } |
34dc7c2f | 3778 | |
ca0bf58d | 3779 | mutex_exit(&arc_reclaim_lock); |
34dc7c2f | 3780 | } |
ab26409d | 3781 | |
da8ccd0e | 3782 | if (type == ARC_BUFC_METADATA) { |
ca0bf58d PS |
3783 | buf->b_data = zio_buf_alloc(size); |
3784 | arc_space_consume(size, ARC_SPACE_META); | |
3785 | } else { | |
3786 | ASSERT(type == ARC_BUFC_DATA); | |
3787 | buf->b_data = zio_data_buf_alloc(size); | |
3788 | arc_space_consume(size, ARC_SPACE_DATA); | |
da8ccd0e PS |
3789 | } |
3790 | ||
34dc7c2f BB |
3791 | /* |
3792 | * Update the state size. Note that ghost states have a | |
3793 | * "ghost size" and so don't need to be updated. | |
3794 | */ | |
b9541d6b | 3795 | if (!GHOST_STATE(buf->b_hdr->b_l1hdr.b_state)) { |
34dc7c2f BB |
3796 | arc_buf_hdr_t *hdr = buf->b_hdr; |
3797 | ||
b9541d6b | 3798 | atomic_add_64(&hdr->b_l1hdr.b_state->arcs_size, size); |
ca0bf58d PS |
3799 | |
3800 | /* | |
3801 | * If this is reached via arc_read, the link is | |
3802 | * protected by the hash lock. If reached via | |
3803 | * arc_buf_alloc, the header should not be accessed by | |
3804 | * any other thread. And, if reached via arc_read_done, | |
3805 | * the hash lock will protect it if it's found in the | |
3806 | * hash table; otherwise no other thread should be | |
3807 | * trying to [add|remove]_reference it. | |
3808 | */ | |
3809 | if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { | |
b9541d6b CW |
3810 | ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
3811 | atomic_add_64(&hdr->b_l1hdr.b_state->arcs_lsize[type], | |
3812 | size); | |
34dc7c2f BB |
3813 | } |
3814 | /* | |
3815 | * If we are growing the cache, and we are adding anonymous | |
3816 | * data, and we have outgrown arc_p, update arc_p | |
3817 | */ | |
ca0bf58d | 3818 | if (arc_size < arc_c && hdr->b_l1hdr.b_state == arc_anon && |
34dc7c2f BB |
3819 | arc_anon->arcs_size + arc_mru->arcs_size > arc_p) |
3820 | arc_p = MIN(arc_c, arc_p + size); | |
3821 | } | |
3822 | } | |
3823 | ||
3824 | /* | |
3825 | * This routine is called whenever a buffer is accessed. | |
3826 | * NOTE: the hash lock is dropped in this function. | |
3827 | */ | |
3828 | static void | |
2a432414 | 3829 | arc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock) |
34dc7c2f | 3830 | { |
428870ff BB |
3831 | clock_t now; |
3832 | ||
34dc7c2f | 3833 | ASSERT(MUTEX_HELD(hash_lock)); |
b9541d6b | 3834 | ASSERT(HDR_HAS_L1HDR(hdr)); |
34dc7c2f | 3835 | |
b9541d6b | 3836 | if (hdr->b_l1hdr.b_state == arc_anon) { |
34dc7c2f BB |
3837 | /* |
3838 | * This buffer is not in the cache, and does not | |
3839 | * appear in our "ghost" list. Add the new buffer | |
3840 | * to the MRU state. | |
3841 | */ | |
3842 | ||
b9541d6b CW |
3843 | ASSERT0(hdr->b_l1hdr.b_arc_access); |
3844 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); | |
2a432414 GW |
3845 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); |
3846 | arc_change_state(arc_mru, hdr, hash_lock); | |
34dc7c2f | 3847 | |
b9541d6b | 3848 | } else if (hdr->b_l1hdr.b_state == arc_mru) { |
428870ff BB |
3849 | now = ddi_get_lbolt(); |
3850 | ||
34dc7c2f BB |
3851 | /* |
3852 | * If this buffer is here because of a prefetch, then either: | |
3853 | * - clear the flag if this is a "referencing" read | |
3854 | * (any subsequent access will bump this into the MFU state). | |
3855 | * or | |
3856 | * - move the buffer to the head of the list if this is | |
3857 | * another prefetch (to make it less likely to be evicted). | |
3858 | */ | |
b9541d6b CW |
3859 | if (HDR_PREFETCH(hdr)) { |
3860 | if (refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { | |
ca0bf58d PS |
3861 | /* link protected by hash lock */ |
3862 | ASSERT(multilist_link_active( | |
b9541d6b | 3863 | &hdr->b_l1hdr.b_arc_node)); |
34dc7c2f | 3864 | } else { |
2a432414 | 3865 | hdr->b_flags &= ~ARC_FLAG_PREFETCH; |
b9541d6b | 3866 | atomic_inc_32(&hdr->b_l1hdr.b_mru_hits); |
34dc7c2f BB |
3867 | ARCSTAT_BUMP(arcstat_mru_hits); |
3868 | } | |
b9541d6b | 3869 | hdr->b_l1hdr.b_arc_access = now; |
34dc7c2f BB |
3870 | return; |
3871 | } | |
3872 | ||
3873 | /* | |
3874 | * This buffer has been "accessed" only once so far, | |
3875 | * but it is still in the cache. Move it to the MFU | |
3876 | * state. | |
3877 | */ | |
b9541d6b CW |
3878 | if (ddi_time_after(now, hdr->b_l1hdr.b_arc_access + |
3879 | ARC_MINTIME)) { | |
34dc7c2f BB |
3880 | /* |
3881 | * More than 125ms have passed since we | |
3882 | * instantiated this buffer. Move it to the | |
3883 | * most frequently used state. | |
3884 | */ | |
b9541d6b | 3885 | hdr->b_l1hdr.b_arc_access = now; |
2a432414 GW |
3886 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
3887 | arc_change_state(arc_mfu, hdr, hash_lock); | |
34dc7c2f | 3888 | } |
b9541d6b | 3889 | atomic_inc_32(&hdr->b_l1hdr.b_mru_hits); |
34dc7c2f | 3890 | ARCSTAT_BUMP(arcstat_mru_hits); |
b9541d6b | 3891 | } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { |
34dc7c2f BB |
3892 | arc_state_t *new_state; |
3893 | /* | |
3894 | * This buffer has been "accessed" recently, but | |
3895 | * was evicted from the cache. Move it to the | |
3896 | * MFU state. | |
3897 | */ | |
3898 | ||
b9541d6b | 3899 | if (HDR_PREFETCH(hdr)) { |
34dc7c2f | 3900 | new_state = arc_mru; |
b9541d6b | 3901 | if (refcount_count(&hdr->b_l1hdr.b_refcnt) > 0) |
2a432414 GW |
3902 | hdr->b_flags &= ~ARC_FLAG_PREFETCH; |
3903 | DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); | |
34dc7c2f BB |
3904 | } else { |
3905 | new_state = arc_mfu; | |
2a432414 | 3906 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
34dc7c2f BB |
3907 | } |
3908 | ||
b9541d6b | 3909 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 | 3910 | arc_change_state(new_state, hdr, hash_lock); |
34dc7c2f | 3911 | |
b9541d6b | 3912 | atomic_inc_32(&hdr->b_l1hdr.b_mru_ghost_hits); |
34dc7c2f | 3913 | ARCSTAT_BUMP(arcstat_mru_ghost_hits); |
b9541d6b | 3914 | } else if (hdr->b_l1hdr.b_state == arc_mfu) { |
34dc7c2f BB |
3915 | /* |
3916 | * This buffer has been accessed more than once and is | |
3917 | * still in the cache. Keep it in the MFU state. | |
3918 | * | |
3919 | * NOTE: an add_reference() that occurred when we did | |
3920 | * the arc_read() will have kicked this off the list. | |
3921 | * If it was a prefetch, we will explicitly move it to | |
3922 | * the head of the list now. | |
3923 | */ | |
b9541d6b CW |
3924 | if ((HDR_PREFETCH(hdr)) != 0) { |
3925 | ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); | |
ca0bf58d PS |
3926 | /* link protected by hash_lock */ |
3927 | ASSERT(multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
34dc7c2f | 3928 | } |
b9541d6b | 3929 | atomic_inc_32(&hdr->b_l1hdr.b_mfu_hits); |
34dc7c2f | 3930 | ARCSTAT_BUMP(arcstat_mfu_hits); |
b9541d6b CW |
3931 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
3932 | } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { | |
34dc7c2f BB |
3933 | arc_state_t *new_state = arc_mfu; |
3934 | /* | |
3935 | * This buffer has been accessed more than once but has | |
3936 | * been evicted from the cache. Move it back to the | |
3937 | * MFU state. | |
3938 | */ | |
3939 | ||
b9541d6b | 3940 | if (HDR_PREFETCH(hdr)) { |
34dc7c2f BB |
3941 | /* |
3942 | * This is a prefetch access... | |
3943 | * move this block back to the MRU state. | |
3944 | */ | |
b9541d6b | 3945 | ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt)); |
34dc7c2f BB |
3946 | new_state = arc_mru; |
3947 | } | |
3948 | ||
b9541d6b | 3949 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 GW |
3950 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
3951 | arc_change_state(new_state, hdr, hash_lock); | |
34dc7c2f | 3952 | |
b9541d6b | 3953 | atomic_inc_32(&hdr->b_l1hdr.b_mfu_ghost_hits); |
34dc7c2f | 3954 | ARCSTAT_BUMP(arcstat_mfu_ghost_hits); |
b9541d6b | 3955 | } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { |
34dc7c2f BB |
3956 | /* |
3957 | * This buffer is on the 2nd Level ARC. | |
3958 | */ | |
3959 | ||
b9541d6b | 3960 | hdr->b_l1hdr.b_arc_access = ddi_get_lbolt(); |
2a432414 GW |
3961 | DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); |
3962 | arc_change_state(arc_mfu, hdr, hash_lock); | |
34dc7c2f | 3963 | } else { |
b9541d6b CW |
3964 | cmn_err(CE_PANIC, "invalid arc state 0x%p", |
3965 | hdr->b_l1hdr.b_state); | |
34dc7c2f BB |
3966 | } |
3967 | } | |
3968 | ||
3969 | /* a generic arc_done_func_t which you can use */ | |
3970 | /* ARGSUSED */ | |
3971 | void | |
3972 | arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) | |
3973 | { | |
428870ff BB |
3974 | if (zio == NULL || zio->io_error == 0) |
3975 | bcopy(buf->b_data, arg, buf->b_hdr->b_size); | |
13fe0198 | 3976 | VERIFY(arc_buf_remove_ref(buf, arg)); |
34dc7c2f BB |
3977 | } |
3978 | ||
3979 | /* a generic arc_done_func_t */ | |
3980 | void | |
3981 | arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) | |
3982 | { | |
3983 | arc_buf_t **bufp = arg; | |
3984 | if (zio && zio->io_error) { | |
13fe0198 | 3985 | VERIFY(arc_buf_remove_ref(buf, arg)); |
34dc7c2f BB |
3986 | *bufp = NULL; |
3987 | } else { | |
3988 | *bufp = buf; | |
428870ff | 3989 | ASSERT(buf->b_data); |
34dc7c2f BB |
3990 | } |
3991 | } | |
3992 | ||
3993 | static void | |
3994 | arc_read_done(zio_t *zio) | |
3995 | { | |
9b67f605 | 3996 | arc_buf_hdr_t *hdr; |
34dc7c2f BB |
3997 | arc_buf_t *buf; |
3998 | arc_buf_t *abuf; /* buffer we're assigning to callback */ | |
9b67f605 | 3999 | kmutex_t *hash_lock = NULL; |
34dc7c2f BB |
4000 | arc_callback_t *callback_list, *acb; |
4001 | int freeable = FALSE; | |
4002 | ||
4003 | buf = zio->io_private; | |
4004 | hdr = buf->b_hdr; | |
4005 | ||
4006 | /* | |
4007 | * The hdr was inserted into hash-table and removed from lists | |
4008 | * prior to starting I/O. We should find this header, since | |
4009 | * it's in the hash table, and it should be legit since it's | |
4010 | * not possible to evict it during the I/O. The only possible | |
4011 | * reason for it not to be found is if we were freed during the | |
4012 | * read. | |
4013 | */ | |
9b67f605 MA |
4014 | if (HDR_IN_HASH_TABLE(hdr)) { |
4015 | arc_buf_hdr_t *found; | |
4016 | ||
4017 | ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); | |
4018 | ASSERT3U(hdr->b_dva.dva_word[0], ==, | |
4019 | BP_IDENTITY(zio->io_bp)->dva_word[0]); | |
4020 | ASSERT3U(hdr->b_dva.dva_word[1], ==, | |
4021 | BP_IDENTITY(zio->io_bp)->dva_word[1]); | |
4022 | ||
4023 | found = buf_hash_find(hdr->b_spa, zio->io_bp, | |
4024 | &hash_lock); | |
4025 | ||
4026 | ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && | |
4027 | hash_lock == NULL) || | |
4028 | (found == hdr && | |
4029 | DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || | |
4030 | (found == hdr && HDR_L2_READING(hdr))); | |
4031 | } | |
34dc7c2f | 4032 | |
2a432414 | 4033 | hdr->b_flags &= ~ARC_FLAG_L2_EVICTED; |
b9541d6b | 4034 | if (l2arc_noprefetch && HDR_PREFETCH(hdr)) |
2a432414 | 4035 | hdr->b_flags &= ~ARC_FLAG_L2CACHE; |
34dc7c2f BB |
4036 | |
4037 | /* byteswap if necessary */ | |
b9541d6b | 4038 | callback_list = hdr->b_l1hdr.b_acb; |
34dc7c2f | 4039 | ASSERT(callback_list != NULL); |
428870ff | 4040 | if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) { |
9ae529ec CS |
4041 | dmu_object_byteswap_t bswap = |
4042 | DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); | |
b01615d5 RY |
4043 | if (BP_GET_LEVEL(zio->io_bp) > 0) |
4044 | byteswap_uint64_array(buf->b_data, hdr->b_size); | |
4045 | else | |
4046 | dmu_ot_byteswap[bswap].ob_func(buf->b_data, hdr->b_size); | |
b128c09f | 4047 | } |
34dc7c2f BB |
4048 | |
4049 | arc_cksum_compute(buf, B_FALSE); | |
498877ba | 4050 | arc_buf_watch(buf); |
34dc7c2f | 4051 | |
b9541d6b CW |
4052 | if (hash_lock && zio->io_error == 0 && |
4053 | hdr->b_l1hdr.b_state == arc_anon) { | |
428870ff BB |
4054 | /* |
4055 | * Only call arc_access on anonymous buffers. This is because | |
4056 | * if we've issued an I/O for an evicted buffer, we've already | |
4057 | * called arc_access (to prevent any simultaneous readers from | |
4058 | * getting confused). | |
4059 | */ | |
4060 | arc_access(hdr, hash_lock); | |
4061 | } | |
4062 | ||
34dc7c2f BB |
4063 | /* create copies of the data buffer for the callers */ |
4064 | abuf = buf; | |
4065 | for (acb = callback_list; acb; acb = acb->acb_next) { | |
4066 | if (acb->acb_done) { | |
1eb5bfa3 GW |
4067 | if (abuf == NULL) { |
4068 | ARCSTAT_BUMP(arcstat_duplicate_reads); | |
34dc7c2f | 4069 | abuf = arc_buf_clone(buf); |
1eb5bfa3 | 4070 | } |
34dc7c2f BB |
4071 | acb->acb_buf = abuf; |
4072 | abuf = NULL; | |
4073 | } | |
4074 | } | |
b9541d6b | 4075 | hdr->b_l1hdr.b_acb = NULL; |
2a432414 | 4076 | hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; |
34dc7c2f | 4077 | ASSERT(!HDR_BUF_AVAILABLE(hdr)); |
428870ff BB |
4078 | if (abuf == buf) { |
4079 | ASSERT(buf->b_efunc == NULL); | |
b9541d6b | 4080 | ASSERT(hdr->b_l1hdr.b_datacnt == 1); |
2a432414 | 4081 | hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; |
428870ff | 4082 | } |
34dc7c2f | 4083 | |
b9541d6b CW |
4084 | ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt) || |
4085 | callback_list != NULL); | |
34dc7c2f BB |
4086 | |
4087 | if (zio->io_error != 0) { | |
2a432414 | 4088 | hdr->b_flags |= ARC_FLAG_IO_ERROR; |
b9541d6b | 4089 | if (hdr->b_l1hdr.b_state != arc_anon) |
34dc7c2f BB |
4090 | arc_change_state(arc_anon, hdr, hash_lock); |
4091 | if (HDR_IN_HASH_TABLE(hdr)) | |
4092 | buf_hash_remove(hdr); | |
b9541d6b | 4093 | freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); |
34dc7c2f BB |
4094 | } |
4095 | ||
4096 | /* | |
4097 | * Broadcast before we drop the hash_lock to avoid the possibility | |
4098 | * that the hdr (and hence the cv) might be freed before we get to | |
4099 | * the cv_broadcast(). | |
4100 | */ | |
b9541d6b | 4101 | cv_broadcast(&hdr->b_l1hdr.b_cv); |
34dc7c2f | 4102 | |
b9541d6b | 4103 | if (hash_lock != NULL) { |
34dc7c2f BB |
4104 | mutex_exit(hash_lock); |
4105 | } else { | |
4106 | /* | |
4107 | * This block was freed while we waited for the read to | |
4108 | * complete. It has been removed from the hash table and | |
4109 | * moved to the anonymous state (so that it won't show up | |
4110 | * in the cache). | |
4111 | */ | |
b9541d6b CW |
4112 | ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); |
4113 | freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt); | |
34dc7c2f BB |
4114 | } |
4115 | ||
4116 | /* execute each callback and free its structure */ | |
4117 | while ((acb = callback_list) != NULL) { | |
4118 | if (acb->acb_done) | |
4119 | acb->acb_done(zio, acb->acb_buf, acb->acb_private); | |
4120 | ||
4121 | if (acb->acb_zio_dummy != NULL) { | |
4122 | acb->acb_zio_dummy->io_error = zio->io_error; | |
4123 | zio_nowait(acb->acb_zio_dummy); | |
4124 | } | |
4125 | ||
4126 | callback_list = acb->acb_next; | |
4127 | kmem_free(acb, sizeof (arc_callback_t)); | |
4128 | } | |
4129 | ||
4130 | if (freeable) | |
4131 | arc_hdr_destroy(hdr); | |
4132 | } | |
4133 | ||
4134 | /* | |
5c839890 | 4135 | * "Read" the block at the specified DVA (in bp) via the |
34dc7c2f BB |
4136 | * cache. If the block is found in the cache, invoke the provided |
4137 | * callback immediately and return. Note that the `zio' parameter | |
4138 | * in the callback will be NULL in this case, since no IO was | |
4139 | * required. If the block is not in the cache pass the read request | |
4140 | * on to the spa with a substitute callback function, so that the | |
4141 | * requested block will be added to the cache. | |
4142 | * | |
4143 | * If a read request arrives for a block that has a read in-progress, | |
4144 | * either wait for the in-progress read to complete (and return the | |
4145 | * results); or, if this is a read with a "done" func, add a record | |
4146 | * to the read to invoke the "done" func when the read completes, | |
4147 | * and return; or just return. | |
4148 | * | |
4149 | * arc_read_done() will invoke all the requested "done" functions | |
4150 | * for readers of this block. | |
4151 | */ | |
4152 | int | |
294f6806 | 4153 | arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done, |
2a432414 GW |
4154 | void *private, zio_priority_t priority, int zio_flags, |
4155 | arc_flags_t *arc_flags, const zbookmark_phys_t *zb) | |
34dc7c2f | 4156 | { |
9b67f605 | 4157 | arc_buf_hdr_t *hdr = NULL; |
d4ed6673 | 4158 | arc_buf_t *buf = NULL; |
9b67f605 | 4159 | kmutex_t *hash_lock = NULL; |
34dc7c2f | 4160 | zio_t *rzio; |
3541dc6d | 4161 | uint64_t guid = spa_load_guid(spa); |
1421c891 | 4162 | int rc = 0; |
34dc7c2f | 4163 | |
9b67f605 MA |
4164 | ASSERT(!BP_IS_EMBEDDED(bp) || |
4165 | BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); | |
4166 | ||
34dc7c2f | 4167 | top: |
9b67f605 MA |
4168 | if (!BP_IS_EMBEDDED(bp)) { |
4169 | /* | |
4170 | * Embedded BP's have no DVA and require no I/O to "read". | |
4171 | * Create an anonymous arc buf to back it. | |
4172 | */ | |
4173 | hdr = buf_hash_find(guid, bp, &hash_lock); | |
4174 | } | |
4175 | ||
b9541d6b | 4176 | if (hdr != NULL && HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_datacnt > 0) { |
34dc7c2f | 4177 | |
2a432414 | 4178 | *arc_flags |= ARC_FLAG_CACHED; |
34dc7c2f BB |
4179 | |
4180 | if (HDR_IO_IN_PROGRESS(hdr)) { | |
4181 | ||
2a432414 | 4182 | if (*arc_flags & ARC_FLAG_WAIT) { |
b9541d6b | 4183 | cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); |
34dc7c2f BB |
4184 | mutex_exit(hash_lock); |
4185 | goto top; | |
4186 | } | |
2a432414 | 4187 | ASSERT(*arc_flags & ARC_FLAG_NOWAIT); |
34dc7c2f BB |
4188 | |
4189 | if (done) { | |
4190 | arc_callback_t *acb = NULL; | |
4191 | ||
4192 | acb = kmem_zalloc(sizeof (arc_callback_t), | |
79c76d5b | 4193 | KM_SLEEP); |
34dc7c2f BB |
4194 | acb->acb_done = done; |
4195 | acb->acb_private = private; | |
34dc7c2f BB |
4196 | if (pio != NULL) |
4197 | acb->acb_zio_dummy = zio_null(pio, | |
d164b209 | 4198 | spa, NULL, NULL, NULL, zio_flags); |
34dc7c2f BB |
4199 | |
4200 | ASSERT(acb->acb_done != NULL); | |
b9541d6b CW |
4201 | acb->acb_next = hdr->b_l1hdr.b_acb; |
4202 | hdr->b_l1hdr.b_acb = acb; | |
34dc7c2f BB |
4203 | add_reference(hdr, hash_lock, private); |
4204 | mutex_exit(hash_lock); | |
1421c891 | 4205 | goto out; |
34dc7c2f BB |
4206 | } |
4207 | mutex_exit(hash_lock); | |
1421c891 | 4208 | goto out; |
34dc7c2f BB |
4209 | } |
4210 | ||
b9541d6b CW |
4211 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || |
4212 | hdr->b_l1hdr.b_state == arc_mfu); | |
34dc7c2f BB |
4213 | |
4214 | if (done) { | |
4215 | add_reference(hdr, hash_lock, private); | |
4216 | /* | |
4217 | * If this block is already in use, create a new | |
4218 | * copy of the data so that we will be guaranteed | |
4219 | * that arc_release() will always succeed. | |
4220 | */ | |
b9541d6b | 4221 | buf = hdr->b_l1hdr.b_buf; |
34dc7c2f BB |
4222 | ASSERT(buf); |
4223 | ASSERT(buf->b_data); | |
4224 | if (HDR_BUF_AVAILABLE(hdr)) { | |
4225 | ASSERT(buf->b_efunc == NULL); | |
2a432414 | 4226 | hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; |
34dc7c2f BB |
4227 | } else { |
4228 | buf = arc_buf_clone(buf); | |
4229 | } | |
428870ff | 4230 | |
2a432414 | 4231 | } else if (*arc_flags & ARC_FLAG_PREFETCH && |
b9541d6b | 4232 | refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) { |
2a432414 | 4233 | hdr->b_flags |= ARC_FLAG_PREFETCH; |
34dc7c2f BB |
4234 | } |
4235 | DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); | |
4236 | arc_access(hdr, hash_lock); | |
2a432414 GW |
4237 | if (*arc_flags & ARC_FLAG_L2CACHE) |
4238 | hdr->b_flags |= ARC_FLAG_L2CACHE; | |
4239 | if (*arc_flags & ARC_FLAG_L2COMPRESS) | |
4240 | hdr->b_flags |= ARC_FLAG_L2COMPRESS; | |
34dc7c2f BB |
4241 | mutex_exit(hash_lock); |
4242 | ARCSTAT_BUMP(arcstat_hits); | |
b9541d6b CW |
4243 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), |
4244 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), | |
34dc7c2f BB |
4245 | data, metadata, hits); |
4246 | ||
4247 | if (done) | |
4248 | done(NULL, buf, private); | |
4249 | } else { | |
4250 | uint64_t size = BP_GET_LSIZE(bp); | |
9b67f605 | 4251 | arc_callback_t *acb; |
b128c09f | 4252 | vdev_t *vd = NULL; |
a117a6d6 | 4253 | uint64_t addr = 0; |
d164b209 | 4254 | boolean_t devw = B_FALSE; |
0ed212dc | 4255 | enum zio_compress b_compress = ZIO_COMPRESS_OFF; |
b9541d6b | 4256 | int32_t b_asize = 0; |
34dc7c2f | 4257 | |
5f6d0b6f BB |
4258 | /* |
4259 | * Gracefully handle a damaged logical block size as a | |
4260 | * checksum error by passing a dummy zio to the done callback. | |
4261 | */ | |
f1512ee6 | 4262 | if (size > spa_maxblocksize(spa)) { |
5f6d0b6f BB |
4263 | if (done) { |
4264 | rzio = zio_null(pio, spa, NULL, | |
4265 | NULL, NULL, zio_flags); | |
4266 | rzio->io_error = ECKSUM; | |
4267 | done(rzio, buf, private); | |
4268 | zio_nowait(rzio); | |
4269 | } | |
4270 | rc = ECKSUM; | |
4271 | goto out; | |
4272 | } | |
4273 | ||
34dc7c2f BB |
4274 | if (hdr == NULL) { |
4275 | /* this block is not in the cache */ | |
9b67f605 | 4276 | arc_buf_hdr_t *exists = NULL; |
34dc7c2f BB |
4277 | arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); |
4278 | buf = arc_buf_alloc(spa, size, private, type); | |
4279 | hdr = buf->b_hdr; | |
9b67f605 MA |
4280 | if (!BP_IS_EMBEDDED(bp)) { |
4281 | hdr->b_dva = *BP_IDENTITY(bp); | |
4282 | hdr->b_birth = BP_PHYSICAL_BIRTH(bp); | |
9b67f605 MA |
4283 | exists = buf_hash_insert(hdr, &hash_lock); |
4284 | } | |
4285 | if (exists != NULL) { | |
34dc7c2f BB |
4286 | /* somebody beat us to the hash insert */ |
4287 | mutex_exit(hash_lock); | |
428870ff | 4288 | buf_discard_identity(hdr); |
34dc7c2f BB |
4289 | (void) arc_buf_remove_ref(buf, private); |
4290 | goto top; /* restart the IO request */ | |
4291 | } | |
2a432414 | 4292 | |
34dc7c2f | 4293 | /* if this is a prefetch, we don't have a reference */ |
2a432414 | 4294 | if (*arc_flags & ARC_FLAG_PREFETCH) { |
34dc7c2f BB |
4295 | (void) remove_reference(hdr, hash_lock, |
4296 | private); | |
2a432414 | 4297 | hdr->b_flags |= ARC_FLAG_PREFETCH; |
34dc7c2f | 4298 | } |
2a432414 GW |
4299 | if (*arc_flags & ARC_FLAG_L2CACHE) |
4300 | hdr->b_flags |= ARC_FLAG_L2CACHE; | |
4301 | if (*arc_flags & ARC_FLAG_L2COMPRESS) | |
4302 | hdr->b_flags |= ARC_FLAG_L2COMPRESS; | |
34dc7c2f | 4303 | if (BP_GET_LEVEL(bp) > 0) |
2a432414 | 4304 | hdr->b_flags |= ARC_FLAG_INDIRECT; |
34dc7c2f | 4305 | } else { |
b9541d6b CW |
4306 | /* |
4307 | * This block is in the ghost cache. If it was L2-only | |
4308 | * (and thus didn't have an L1 hdr), we realloc the | |
4309 | * header to add an L1 hdr. | |
4310 | */ | |
4311 | if (!HDR_HAS_L1HDR(hdr)) { | |
4312 | hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, | |
4313 | hdr_full_cache); | |
4314 | } | |
4315 | ||
4316 | ASSERT(GHOST_STATE(hdr->b_l1hdr.b_state)); | |
34dc7c2f | 4317 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b | 4318 | ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
ca0bf58d | 4319 | ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); |
34dc7c2f BB |
4320 | |
4321 | /* if this is a prefetch, we don't have a reference */ | |
2a432414 GW |
4322 | if (*arc_flags & ARC_FLAG_PREFETCH) |
4323 | hdr->b_flags |= ARC_FLAG_PREFETCH; | |
34dc7c2f BB |
4324 | else |
4325 | add_reference(hdr, hash_lock, private); | |
2a432414 GW |
4326 | if (*arc_flags & ARC_FLAG_L2CACHE) |
4327 | hdr->b_flags |= ARC_FLAG_L2CACHE; | |
4328 | if (*arc_flags & ARC_FLAG_L2COMPRESS) | |
4329 | hdr->b_flags |= ARC_FLAG_L2COMPRESS; | |
34dc7c2f BB |
4330 | buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); |
4331 | buf->b_hdr = hdr; | |
4332 | buf->b_data = NULL; | |
4333 | buf->b_efunc = NULL; | |
4334 | buf->b_private = NULL; | |
4335 | buf->b_next = NULL; | |
b9541d6b CW |
4336 | hdr->b_l1hdr.b_buf = buf; |
4337 | ASSERT0(hdr->b_l1hdr.b_datacnt); | |
4338 | hdr->b_l1hdr.b_datacnt = 1; | |
428870ff BB |
4339 | arc_get_data_buf(buf); |
4340 | arc_access(hdr, hash_lock); | |
34dc7c2f BB |
4341 | } |
4342 | ||
b9541d6b | 4343 | ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); |
428870ff | 4344 | |
79c76d5b | 4345 | acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); |
34dc7c2f BB |
4346 | acb->acb_done = done; |
4347 | acb->acb_private = private; | |
34dc7c2f | 4348 | |
b9541d6b CW |
4349 | ASSERT(hdr->b_l1hdr.b_acb == NULL); |
4350 | hdr->b_l1hdr.b_acb = acb; | |
2a432414 | 4351 | hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; |
34dc7c2f | 4352 | |
b9541d6b CW |
4353 | if (HDR_HAS_L2HDR(hdr) && |
4354 | (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { | |
4355 | devw = hdr->b_l2hdr.b_dev->l2ad_writing; | |
4356 | addr = hdr->b_l2hdr.b_daddr; | |
4357 | b_compress = HDR_GET_COMPRESS(hdr); | |
4358 | b_asize = hdr->b_l2hdr.b_asize; | |
b128c09f BB |
4359 | /* |
4360 | * Lock out device removal. | |
4361 | */ | |
4362 | if (vdev_is_dead(vd) || | |
4363 | !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) | |
4364 | vd = NULL; | |
4365 | } | |
4366 | ||
9b67f605 MA |
4367 | if (hash_lock != NULL) |
4368 | mutex_exit(hash_lock); | |
b128c09f | 4369 | |
e49f1e20 WA |
4370 | /* |
4371 | * At this point, we have a level 1 cache miss. Try again in | |
4372 | * L2ARC if possible. | |
4373 | */ | |
34dc7c2f | 4374 | ASSERT3U(hdr->b_size, ==, size); |
428870ff | 4375 | DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp, |
5dbd68a3 | 4376 | uint64_t, size, zbookmark_phys_t *, zb); |
34dc7c2f | 4377 | ARCSTAT_BUMP(arcstat_misses); |
b9541d6b CW |
4378 | ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr), |
4379 | demand, prefetch, !HDR_ISTYPE_METADATA(hdr), | |
34dc7c2f BB |
4380 | data, metadata, misses); |
4381 | ||
d164b209 | 4382 | if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) { |
34dc7c2f BB |
4383 | /* |
4384 | * Read from the L2ARC if the following are true: | |
b128c09f BB |
4385 | * 1. The L2ARC vdev was previously cached. |
4386 | * 2. This buffer still has L2ARC metadata. | |
4387 | * 3. This buffer isn't currently writing to the L2ARC. | |
4388 | * 4. The L2ARC entry wasn't evicted, which may | |
4389 | * also have invalidated the vdev. | |
d164b209 | 4390 | * 5. This isn't prefetch and l2arc_noprefetch is set. |
34dc7c2f | 4391 | */ |
b9541d6b | 4392 | if (HDR_HAS_L2HDR(hdr) && |
d164b209 BB |
4393 | !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && |
4394 | !(l2arc_noprefetch && HDR_PREFETCH(hdr))) { | |
34dc7c2f BB |
4395 | l2arc_read_callback_t *cb; |
4396 | ||
4397 | DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); | |
4398 | ARCSTAT_BUMP(arcstat_l2_hits); | |
b9541d6b | 4399 | atomic_inc_32(&hdr->b_l2hdr.b_hits); |
34dc7c2f | 4400 | |
34dc7c2f | 4401 | cb = kmem_zalloc(sizeof (l2arc_read_callback_t), |
79c76d5b | 4402 | KM_SLEEP); |
34dc7c2f BB |
4403 | cb->l2rcb_buf = buf; |
4404 | cb->l2rcb_spa = spa; | |
4405 | cb->l2rcb_bp = *bp; | |
4406 | cb->l2rcb_zb = *zb; | |
b128c09f | 4407 | cb->l2rcb_flags = zio_flags; |
0ed212dc | 4408 | cb->l2rcb_compress = b_compress; |
34dc7c2f | 4409 | |
a117a6d6 GW |
4410 | ASSERT(addr >= VDEV_LABEL_START_SIZE && |
4411 | addr + size < vd->vdev_psize - | |
4412 | VDEV_LABEL_END_SIZE); | |
4413 | ||
34dc7c2f | 4414 | /* |
b128c09f BB |
4415 | * l2arc read. The SCL_L2ARC lock will be |
4416 | * released by l2arc_read_done(). | |
3a17a7a9 SK |
4417 | * Issue a null zio if the underlying buffer |
4418 | * was squashed to zero size by compression. | |
34dc7c2f | 4419 | */ |
0ed212dc | 4420 | if (b_compress == ZIO_COMPRESS_EMPTY) { |
3a17a7a9 SK |
4421 | rzio = zio_null(pio, spa, vd, |
4422 | l2arc_read_done, cb, | |
4423 | zio_flags | ZIO_FLAG_DONT_CACHE | | |
4424 | ZIO_FLAG_CANFAIL | | |
4425 | ZIO_FLAG_DONT_PROPAGATE | | |
4426 | ZIO_FLAG_DONT_RETRY); | |
4427 | } else { | |
4428 | rzio = zio_read_phys(pio, vd, addr, | |
0ed212dc BP |
4429 | b_asize, buf->b_data, |
4430 | ZIO_CHECKSUM_OFF, | |
3a17a7a9 SK |
4431 | l2arc_read_done, cb, priority, |
4432 | zio_flags | ZIO_FLAG_DONT_CACHE | | |
4433 | ZIO_FLAG_CANFAIL | | |
4434 | ZIO_FLAG_DONT_PROPAGATE | | |
4435 | ZIO_FLAG_DONT_RETRY, B_FALSE); | |
4436 | } | |
34dc7c2f BB |
4437 | DTRACE_PROBE2(l2arc__read, vdev_t *, vd, |
4438 | zio_t *, rzio); | |
0ed212dc | 4439 | ARCSTAT_INCR(arcstat_l2_read_bytes, b_asize); |
34dc7c2f | 4440 | |
2a432414 | 4441 | if (*arc_flags & ARC_FLAG_NOWAIT) { |
b128c09f | 4442 | zio_nowait(rzio); |
1421c891 | 4443 | goto out; |
b128c09f | 4444 | } |
34dc7c2f | 4445 | |
2a432414 | 4446 | ASSERT(*arc_flags & ARC_FLAG_WAIT); |
b128c09f | 4447 | if (zio_wait(rzio) == 0) |
1421c891 | 4448 | goto out; |
b128c09f BB |
4449 | |
4450 | /* l2arc read error; goto zio_read() */ | |
34dc7c2f BB |
4451 | } else { |
4452 | DTRACE_PROBE1(l2arc__miss, | |
4453 | arc_buf_hdr_t *, hdr); | |
4454 | ARCSTAT_BUMP(arcstat_l2_misses); | |
4455 | if (HDR_L2_WRITING(hdr)) | |
4456 | ARCSTAT_BUMP(arcstat_l2_rw_clash); | |
b128c09f | 4457 | spa_config_exit(spa, SCL_L2ARC, vd); |
34dc7c2f | 4458 | } |
d164b209 BB |
4459 | } else { |
4460 | if (vd != NULL) | |
4461 | spa_config_exit(spa, SCL_L2ARC, vd); | |
4462 | if (l2arc_ndev != 0) { | |
4463 | DTRACE_PROBE1(l2arc__miss, | |
4464 | arc_buf_hdr_t *, hdr); | |
4465 | ARCSTAT_BUMP(arcstat_l2_misses); | |
4466 | } | |
34dc7c2f | 4467 | } |
34dc7c2f BB |
4468 | |
4469 | rzio = zio_read(pio, spa, bp, buf->b_data, size, | |
b128c09f | 4470 | arc_read_done, buf, priority, zio_flags, zb); |
34dc7c2f | 4471 | |
2a432414 | 4472 | if (*arc_flags & ARC_FLAG_WAIT) { |
1421c891 PS |
4473 | rc = zio_wait(rzio); |
4474 | goto out; | |
4475 | } | |
34dc7c2f | 4476 | |
2a432414 | 4477 | ASSERT(*arc_flags & ARC_FLAG_NOWAIT); |
34dc7c2f BB |
4478 | zio_nowait(rzio); |
4479 | } | |
1421c891 PS |
4480 | |
4481 | out: | |
4482 | spa_read_history_add(spa, zb, *arc_flags); | |
4483 | return (rc); | |
34dc7c2f BB |
4484 | } |
4485 | ||
ab26409d BB |
4486 | arc_prune_t * |
4487 | arc_add_prune_callback(arc_prune_func_t *func, void *private) | |
4488 | { | |
4489 | arc_prune_t *p; | |
4490 | ||
d1d7e268 | 4491 | p = kmem_alloc(sizeof (*p), KM_SLEEP); |
ab26409d BB |
4492 | p->p_pfunc = func; |
4493 | p->p_private = private; | |
4494 | list_link_init(&p->p_node); | |
4495 | refcount_create(&p->p_refcnt); | |
4496 | ||
4497 | mutex_enter(&arc_prune_mtx); | |
4498 | refcount_add(&p->p_refcnt, &arc_prune_list); | |
4499 | list_insert_head(&arc_prune_list, p); | |
4500 | mutex_exit(&arc_prune_mtx); | |
4501 | ||
4502 | return (p); | |
4503 | } | |
4504 | ||
4505 | void | |
4506 | arc_remove_prune_callback(arc_prune_t *p) | |
4507 | { | |
4508 | mutex_enter(&arc_prune_mtx); | |
4509 | list_remove(&arc_prune_list, p); | |
4510 | if (refcount_remove(&p->p_refcnt, &arc_prune_list) == 0) { | |
4511 | refcount_destroy(&p->p_refcnt); | |
4512 | kmem_free(p, sizeof (*p)); | |
4513 | } | |
4514 | mutex_exit(&arc_prune_mtx); | |
4515 | } | |
4516 | ||
34dc7c2f BB |
4517 | void |
4518 | arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) | |
4519 | { | |
4520 | ASSERT(buf->b_hdr != NULL); | |
b9541d6b CW |
4521 | ASSERT(buf->b_hdr->b_l1hdr.b_state != arc_anon); |
4522 | ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt) || | |
4523 | func == NULL); | |
428870ff BB |
4524 | ASSERT(buf->b_efunc == NULL); |
4525 | ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr)); | |
4526 | ||
34dc7c2f BB |
4527 | buf->b_efunc = func; |
4528 | buf->b_private = private; | |
4529 | } | |
4530 | ||
df4474f9 MA |
4531 | /* |
4532 | * Notify the arc that a block was freed, and thus will never be used again. | |
4533 | */ | |
4534 | void | |
4535 | arc_freed(spa_t *spa, const blkptr_t *bp) | |
4536 | { | |
4537 | arc_buf_hdr_t *hdr; | |
4538 | kmutex_t *hash_lock; | |
4539 | uint64_t guid = spa_load_guid(spa); | |
4540 | ||
9b67f605 MA |
4541 | ASSERT(!BP_IS_EMBEDDED(bp)); |
4542 | ||
4543 | hdr = buf_hash_find(guid, bp, &hash_lock); | |
df4474f9 MA |
4544 | if (hdr == NULL) |
4545 | return; | |
4546 | if (HDR_BUF_AVAILABLE(hdr)) { | |
b9541d6b | 4547 | arc_buf_t *buf = hdr->b_l1hdr.b_buf; |
df4474f9 | 4548 | add_reference(hdr, hash_lock, FTAG); |
2a432414 | 4549 | hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE; |
df4474f9 MA |
4550 | mutex_exit(hash_lock); |
4551 | ||
4552 | arc_release(buf, FTAG); | |
4553 | (void) arc_buf_remove_ref(buf, FTAG); | |
4554 | } else { | |
4555 | mutex_exit(hash_lock); | |
4556 | } | |
4557 | ||
4558 | } | |
4559 | ||
34dc7c2f | 4560 | /* |
bd089c54 MA |
4561 | * Clear the user eviction callback set by arc_set_callback(), first calling |
4562 | * it if it exists. Because the presence of a callback keeps an arc_buf cached | |
4563 | * clearing the callback may result in the arc_buf being destroyed. However, | |
4564 | * it will not result in the *last* arc_buf being destroyed, hence the data | |
4565 | * will remain cached in the ARC. We make a copy of the arc buffer here so | |
4566 | * that we can process the callback without holding any locks. | |
4567 | * | |
4568 | * It's possible that the callback is already in the process of being cleared | |
4569 | * by another thread. In this case we can not clear the callback. | |
4570 | * | |
4571 | * Returns B_TRUE if the callback was successfully called and cleared. | |
34dc7c2f | 4572 | */ |
bd089c54 MA |
4573 | boolean_t |
4574 | arc_clear_callback(arc_buf_t *buf) | |
34dc7c2f BB |
4575 | { |
4576 | arc_buf_hdr_t *hdr; | |
4577 | kmutex_t *hash_lock; | |
bd089c54 MA |
4578 | arc_evict_func_t *efunc = buf->b_efunc; |
4579 | void *private = buf->b_private; | |
34dc7c2f | 4580 | |
428870ff | 4581 | mutex_enter(&buf->b_evict_lock); |
34dc7c2f BB |
4582 | hdr = buf->b_hdr; |
4583 | if (hdr == NULL) { | |
4584 | /* | |
4585 | * We are in arc_do_user_evicts(). | |
4586 | */ | |
4587 | ASSERT(buf->b_data == NULL); | |
428870ff | 4588 | mutex_exit(&buf->b_evict_lock); |
bd089c54 | 4589 | return (B_FALSE); |
b128c09f | 4590 | } else if (buf->b_data == NULL) { |
34dc7c2f | 4591 | /* |
b128c09f BB |
4592 | * We are on the eviction list; process this buffer now |
4593 | * but let arc_do_user_evicts() do the reaping. | |
34dc7c2f | 4594 | */ |
b128c09f | 4595 | buf->b_efunc = NULL; |
428870ff | 4596 | mutex_exit(&buf->b_evict_lock); |
bd089c54 MA |
4597 | VERIFY0(efunc(private)); |
4598 | return (B_TRUE); | |
34dc7c2f | 4599 | } |
b128c09f BB |
4600 | hash_lock = HDR_LOCK(hdr); |
4601 | mutex_enter(hash_lock); | |
428870ff BB |
4602 | hdr = buf->b_hdr; |
4603 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); | |
34dc7c2f | 4604 | |
b9541d6b CW |
4605 | ASSERT3U(refcount_count(&hdr->b_l1hdr.b_refcnt), <, |
4606 | hdr->b_l1hdr.b_datacnt); | |
4607 | ASSERT(hdr->b_l1hdr.b_state == arc_mru || | |
4608 | hdr->b_l1hdr.b_state == arc_mfu); | |
34dc7c2f | 4609 | |
bd089c54 MA |
4610 | buf->b_efunc = NULL; |
4611 | buf->b_private = NULL; | |
34dc7c2f | 4612 | |
b9541d6b | 4613 | if (hdr->b_l1hdr.b_datacnt > 1) { |
bd089c54 | 4614 | mutex_exit(&buf->b_evict_lock); |
ca0bf58d | 4615 | arc_buf_destroy(buf, TRUE); |
bd089c54 | 4616 | } else { |
b9541d6b | 4617 | ASSERT(buf == hdr->b_l1hdr.b_buf); |
2a432414 | 4618 | hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE; |
bd089c54 | 4619 | mutex_exit(&buf->b_evict_lock); |
34dc7c2f | 4620 | } |
34dc7c2f | 4621 | |
bd089c54 MA |
4622 | mutex_exit(hash_lock); |
4623 | VERIFY0(efunc(private)); | |
4624 | return (B_TRUE); | |
34dc7c2f BB |
4625 | } |
4626 | ||
4627 | /* | |
e49f1e20 WA |
4628 | * Release this buffer from the cache, making it an anonymous buffer. This |
4629 | * must be done after a read and prior to modifying the buffer contents. | |
34dc7c2f | 4630 | * If the buffer has more than one reference, we must make |
b128c09f | 4631 | * a new hdr for the buffer. |
34dc7c2f BB |
4632 | */ |
4633 | void | |
4634 | arc_release(arc_buf_t *buf, void *tag) | |
4635 | { | |
b9541d6b CW |
4636 | kmutex_t *hash_lock; |
4637 | arc_state_t *state; | |
4638 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
34dc7c2f | 4639 | |
428870ff | 4640 | /* |
ca0bf58d | 4641 | * It would be nice to assert that if its DMU metadata (level > |
428870ff BB |
4642 | * 0 || it's the dnode file), then it must be syncing context. |
4643 | * But we don't know that information at this level. | |
4644 | */ | |
4645 | ||
4646 | mutex_enter(&buf->b_evict_lock); | |
b128c09f | 4647 | |
ca0bf58d PS |
4648 | ASSERT(HDR_HAS_L1HDR(hdr)); |
4649 | ||
b9541d6b CW |
4650 | /* |
4651 | * We don't grab the hash lock prior to this check, because if | |
4652 | * the buffer's header is in the arc_anon state, it won't be | |
4653 | * linked into the hash table. | |
4654 | */ | |
4655 | if (hdr->b_l1hdr.b_state == arc_anon) { | |
4656 | mutex_exit(&buf->b_evict_lock); | |
4657 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); | |
4658 | ASSERT(!HDR_IN_HASH_TABLE(hdr)); | |
4659 | ASSERT(!HDR_HAS_L2HDR(hdr)); | |
4660 | ASSERT(BUF_EMPTY(hdr)); | |
34dc7c2f | 4661 | |
b9541d6b CW |
4662 | ASSERT3U(hdr->b_l1hdr.b_datacnt, ==, 1); |
4663 | ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); | |
4664 | ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node)); | |
4665 | ||
4666 | ASSERT3P(buf->b_efunc, ==, NULL); | |
4667 | ASSERT3P(buf->b_private, ==, NULL); | |
4668 | ||
4669 | hdr->b_l1hdr.b_arc_access = 0; | |
4670 | arc_buf_thaw(buf); | |
4671 | ||
4672 | return; | |
34dc7c2f BB |
4673 | } |
4674 | ||
b9541d6b CW |
4675 | hash_lock = HDR_LOCK(hdr); |
4676 | mutex_enter(hash_lock); | |
4677 | ||
4678 | /* | |
4679 | * This assignment is only valid as long as the hash_lock is | |
4680 | * held, we must be careful not to reference state or the | |
4681 | * b_state field after dropping the lock. | |
4682 | */ | |
4683 | state = hdr->b_l1hdr.b_state; | |
4684 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); | |
4685 | ASSERT3P(state, !=, arc_anon); | |
4686 | ||
4687 | /* this buffer is not on any list */ | |
4688 | ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) > 0); | |
4689 | ||
4690 | if (HDR_HAS_L2HDR(hdr)) { | |
b9541d6b | 4691 | mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); |
ca0bf58d PS |
4692 | |
4693 | /* | |
d962d5da PS |
4694 | * We have to recheck this conditional again now that |
4695 | * we're holding the l2ad_mtx to prevent a race with | |
4696 | * another thread which might be concurrently calling | |
4697 | * l2arc_evict(). In that case, l2arc_evict() might have | |
4698 | * destroyed the header's L2 portion as we were waiting | |
4699 | * to acquire the l2ad_mtx. | |
ca0bf58d | 4700 | */ |
d962d5da PS |
4701 | if (HDR_HAS_L2HDR(hdr)) |
4702 | arc_hdr_l2hdr_destroy(hdr); | |
ca0bf58d | 4703 | |
b9541d6b | 4704 | mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); |
b128c09f BB |
4705 | } |
4706 | ||
34dc7c2f BB |
4707 | /* |
4708 | * Do we have more than one buf? | |
4709 | */ | |
b9541d6b | 4710 | if (hdr->b_l1hdr.b_datacnt > 1) { |
34dc7c2f BB |
4711 | arc_buf_hdr_t *nhdr; |
4712 | arc_buf_t **bufp; | |
4713 | uint64_t blksz = hdr->b_size; | |
d164b209 | 4714 | uint64_t spa = hdr->b_spa; |
b9541d6b | 4715 | arc_buf_contents_t type = arc_buf_type(hdr); |
34dc7c2f BB |
4716 | uint32_t flags = hdr->b_flags; |
4717 | ||
b9541d6b | 4718 | ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); |
34dc7c2f | 4719 | /* |
428870ff BB |
4720 | * Pull the data off of this hdr and attach it to |
4721 | * a new anonymous hdr. | |
34dc7c2f BB |
4722 | */ |
4723 | (void) remove_reference(hdr, hash_lock, tag); | |
b9541d6b | 4724 | bufp = &hdr->b_l1hdr.b_buf; |
34dc7c2f BB |
4725 | while (*bufp != buf) |
4726 | bufp = &(*bufp)->b_next; | |
428870ff | 4727 | *bufp = buf->b_next; |
34dc7c2f BB |
4728 | buf->b_next = NULL; |
4729 | ||
b9541d6b CW |
4730 | ASSERT3P(state, !=, arc_l2c_only); |
4731 | ASSERT3U(state->arcs_size, >=, hdr->b_size); | |
4732 | atomic_add_64(&state->arcs_size, -hdr->b_size); | |
4733 | if (refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { | |
4734 | uint64_t *size; | |
4735 | ||
4736 | ASSERT3P(state, !=, arc_l2c_only); | |
4737 | size = &state->arcs_lsize[type]; | |
34dc7c2f BB |
4738 | ASSERT3U(*size, >=, hdr->b_size); |
4739 | atomic_add_64(size, -hdr->b_size); | |
4740 | } | |
1eb5bfa3 GW |
4741 | |
4742 | /* | |
4743 | * We're releasing a duplicate user data buffer, update | |
4744 | * our statistics accordingly. | |
4745 | */ | |
b9541d6b | 4746 | if (HDR_ISTYPE_DATA(hdr)) { |
1eb5bfa3 GW |
4747 | ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers); |
4748 | ARCSTAT_INCR(arcstat_duplicate_buffers_size, | |
4749 | -hdr->b_size); | |
4750 | } | |
b9541d6b | 4751 | hdr->b_l1hdr.b_datacnt -= 1; |
34dc7c2f | 4752 | arc_cksum_verify(buf); |
498877ba | 4753 | arc_buf_unwatch(buf); |
34dc7c2f BB |
4754 | |
4755 | mutex_exit(hash_lock); | |
4756 | ||
b9541d6b | 4757 | nhdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); |
34dc7c2f BB |
4758 | nhdr->b_size = blksz; |
4759 | nhdr->b_spa = spa; | |
b9541d6b CW |
4760 | |
4761 | nhdr->b_l1hdr.b_mru_hits = 0; | |
4762 | nhdr->b_l1hdr.b_mru_ghost_hits = 0; | |
4763 | nhdr->b_l1hdr.b_mfu_hits = 0; | |
4764 | nhdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
4765 | nhdr->b_l1hdr.b_l2_hits = 0; | |
2a432414 | 4766 | nhdr->b_flags = flags & ARC_FLAG_L2_WRITING; |
b9541d6b CW |
4767 | nhdr->b_flags |= arc_bufc_to_flags(type); |
4768 | nhdr->b_flags |= ARC_FLAG_HAS_L1HDR; | |
4769 | ||
4770 | nhdr->b_l1hdr.b_buf = buf; | |
4771 | nhdr->b_l1hdr.b_datacnt = 1; | |
4772 | nhdr->b_l1hdr.b_state = arc_anon; | |
4773 | nhdr->b_l1hdr.b_arc_access = 0; | |
ca0bf58d | 4774 | nhdr->b_l1hdr.b_tmp_cdata = NULL; |
34dc7c2f | 4775 | nhdr->b_freeze_cksum = NULL; |
b9541d6b CW |
4776 | |
4777 | (void) refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); | |
34dc7c2f | 4778 | buf->b_hdr = nhdr; |
428870ff | 4779 | mutex_exit(&buf->b_evict_lock); |
34dc7c2f BB |
4780 | atomic_add_64(&arc_anon->arcs_size, blksz); |
4781 | } else { | |
428870ff | 4782 | mutex_exit(&buf->b_evict_lock); |
b9541d6b | 4783 | ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); |
ca0bf58d PS |
4784 | /* protected by hash lock, or hdr is on arc_anon */ |
4785 | ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); | |
34dc7c2f | 4786 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
b9541d6b CW |
4787 | hdr->b_l1hdr.b_mru_hits = 0; |
4788 | hdr->b_l1hdr.b_mru_ghost_hits = 0; | |
4789 | hdr->b_l1hdr.b_mfu_hits = 0; | |
4790 | hdr->b_l1hdr.b_mfu_ghost_hits = 0; | |
4791 | hdr->b_l1hdr.b_l2_hits = 0; | |
4792 | arc_change_state(arc_anon, hdr, hash_lock); | |
4793 | hdr->b_l1hdr.b_arc_access = 0; | |
4794 | mutex_exit(hash_lock); | |
34dc7c2f | 4795 | |
428870ff | 4796 | buf_discard_identity(hdr); |
34dc7c2f BB |
4797 | arc_buf_thaw(buf); |
4798 | } | |
4799 | buf->b_efunc = NULL; | |
4800 | buf->b_private = NULL; | |
34dc7c2f BB |
4801 | } |
4802 | ||
4803 | int | |
4804 | arc_released(arc_buf_t *buf) | |
4805 | { | |
b128c09f BB |
4806 | int released; |
4807 | ||
428870ff | 4808 | mutex_enter(&buf->b_evict_lock); |
b9541d6b CW |
4809 | released = (buf->b_data != NULL && |
4810 | buf->b_hdr->b_l1hdr.b_state == arc_anon); | |
428870ff | 4811 | mutex_exit(&buf->b_evict_lock); |
b128c09f | 4812 | return (released); |
34dc7c2f BB |
4813 | } |
4814 | ||
34dc7c2f BB |
4815 | #ifdef ZFS_DEBUG |
4816 | int | |
4817 | arc_referenced(arc_buf_t *buf) | |
4818 | { | |
b128c09f BB |
4819 | int referenced; |
4820 | ||
428870ff | 4821 | mutex_enter(&buf->b_evict_lock); |
b9541d6b | 4822 | referenced = (refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); |
428870ff | 4823 | mutex_exit(&buf->b_evict_lock); |
b128c09f | 4824 | return (referenced); |
34dc7c2f BB |
4825 | } |
4826 | #endif | |
4827 | ||
4828 | static void | |
4829 | arc_write_ready(zio_t *zio) | |
4830 | { | |
4831 | arc_write_callback_t *callback = zio->io_private; | |
4832 | arc_buf_t *buf = callback->awcb_buf; | |
4833 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
4834 | ||
b9541d6b CW |
4835 | ASSERT(HDR_HAS_L1HDR(hdr)); |
4836 | ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); | |
4837 | ASSERT(hdr->b_l1hdr.b_datacnt > 0); | |
b128c09f BB |
4838 | callback->awcb_ready(zio, buf, callback->awcb_private); |
4839 | ||
34dc7c2f BB |
4840 | /* |
4841 | * If the IO is already in progress, then this is a re-write | |
b128c09f BB |
4842 | * attempt, so we need to thaw and re-compute the cksum. |
4843 | * It is the responsibility of the callback to handle the | |
4844 | * accounting for any re-write attempt. | |
34dc7c2f BB |
4845 | */ |
4846 | if (HDR_IO_IN_PROGRESS(hdr)) { | |
b9541d6b | 4847 | mutex_enter(&hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
4848 | if (hdr->b_freeze_cksum != NULL) { |
4849 | kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t)); | |
4850 | hdr->b_freeze_cksum = NULL; | |
4851 | } | |
b9541d6b | 4852 | mutex_exit(&hdr->b_l1hdr.b_freeze_lock); |
34dc7c2f BB |
4853 | } |
4854 | arc_cksum_compute(buf, B_FALSE); | |
2a432414 | 4855 | hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS; |
34dc7c2f BB |
4856 | } |
4857 | ||
e8b96c60 MA |
4858 | /* |
4859 | * The SPA calls this callback for each physical write that happens on behalf | |
4860 | * of a logical write. See the comment in dbuf_write_physdone() for details. | |
4861 | */ | |
4862 | static void | |
4863 | arc_write_physdone(zio_t *zio) | |
4864 | { | |
4865 | arc_write_callback_t *cb = zio->io_private; | |
4866 | if (cb->awcb_physdone != NULL) | |
4867 | cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); | |
4868 | } | |
4869 | ||
34dc7c2f BB |
4870 | static void |
4871 | arc_write_done(zio_t *zio) | |
4872 | { | |
4873 | arc_write_callback_t *callback = zio->io_private; | |
4874 | arc_buf_t *buf = callback->awcb_buf; | |
4875 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
4876 | ||
b9541d6b | 4877 | ASSERT(hdr->b_l1hdr.b_acb == NULL); |
428870ff BB |
4878 | |
4879 | if (zio->io_error == 0) { | |
9b67f605 | 4880 | if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { |
b0bc7a84 MG |
4881 | buf_discard_identity(hdr); |
4882 | } else { | |
4883 | hdr->b_dva = *BP_IDENTITY(zio->io_bp); | |
4884 | hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); | |
b0bc7a84 | 4885 | } |
428870ff BB |
4886 | } else { |
4887 | ASSERT(BUF_EMPTY(hdr)); | |
4888 | } | |
34dc7c2f | 4889 | |
34dc7c2f | 4890 | /* |
9b67f605 MA |
4891 | * If the block to be written was all-zero or compressed enough to be |
4892 | * embedded in the BP, no write was performed so there will be no | |
4893 | * dva/birth/checksum. The buffer must therefore remain anonymous | |
4894 | * (and uncached). | |
34dc7c2f BB |
4895 | */ |
4896 | if (!BUF_EMPTY(hdr)) { | |
4897 | arc_buf_hdr_t *exists; | |
4898 | kmutex_t *hash_lock; | |
4899 | ||
428870ff BB |
4900 | ASSERT(zio->io_error == 0); |
4901 | ||
34dc7c2f BB |
4902 | arc_cksum_verify(buf); |
4903 | ||
4904 | exists = buf_hash_insert(hdr, &hash_lock); | |
b9541d6b | 4905 | if (exists != NULL) { |
34dc7c2f BB |
4906 | /* |
4907 | * This can only happen if we overwrite for | |
4908 | * sync-to-convergence, because we remove | |
4909 | * buffers from the hash table when we arc_free(). | |
4910 | */ | |
428870ff BB |
4911 | if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { |
4912 | if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) | |
4913 | panic("bad overwrite, hdr=%p exists=%p", | |
4914 | (void *)hdr, (void *)exists); | |
b9541d6b CW |
4915 | ASSERT(refcount_is_zero( |
4916 | &exists->b_l1hdr.b_refcnt)); | |
428870ff BB |
4917 | arc_change_state(arc_anon, exists, hash_lock); |
4918 | mutex_exit(hash_lock); | |
4919 | arc_hdr_destroy(exists); | |
4920 | exists = buf_hash_insert(hdr, &hash_lock); | |
4921 | ASSERT3P(exists, ==, NULL); | |
03c6040b GW |
4922 | } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { |
4923 | /* nopwrite */ | |
4924 | ASSERT(zio->io_prop.zp_nopwrite); | |
4925 | if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) | |
4926 | panic("bad nopwrite, hdr=%p exists=%p", | |
4927 | (void *)hdr, (void *)exists); | |
428870ff BB |
4928 | } else { |
4929 | /* Dedup */ | |
b9541d6b CW |
4930 | ASSERT(hdr->b_l1hdr.b_datacnt == 1); |
4931 | ASSERT(hdr->b_l1hdr.b_state == arc_anon); | |
428870ff BB |
4932 | ASSERT(BP_GET_DEDUP(zio->io_bp)); |
4933 | ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); | |
4934 | } | |
34dc7c2f | 4935 | } |
2a432414 | 4936 | hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; |
b128c09f | 4937 | /* if it's not anon, we are doing a scrub */ |
b9541d6b | 4938 | if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) |
b128c09f | 4939 | arc_access(hdr, hash_lock); |
34dc7c2f | 4940 | mutex_exit(hash_lock); |
34dc7c2f | 4941 | } else { |
2a432414 | 4942 | hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS; |
34dc7c2f BB |
4943 | } |
4944 | ||
b9541d6b | 4945 | ASSERT(!refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); |
428870ff | 4946 | callback->awcb_done(zio, buf, callback->awcb_private); |
34dc7c2f BB |
4947 | |
4948 | kmem_free(callback, sizeof (arc_write_callback_t)); | |
4949 | } | |
4950 | ||
4951 | zio_t * | |
428870ff | 4952 | arc_write(zio_t *pio, spa_t *spa, uint64_t txg, |
3a17a7a9 | 4953 | blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress, |
e8b96c60 MA |
4954 | const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone, |
4955 | arc_done_func_t *done, void *private, zio_priority_t priority, | |
5dbd68a3 | 4956 | int zio_flags, const zbookmark_phys_t *zb) |
34dc7c2f BB |
4957 | { |
4958 | arc_buf_hdr_t *hdr = buf->b_hdr; | |
4959 | arc_write_callback_t *callback; | |
b128c09f | 4960 | zio_t *zio; |
34dc7c2f | 4961 | |
b128c09f | 4962 | ASSERT(ready != NULL); |
428870ff | 4963 | ASSERT(done != NULL); |
34dc7c2f | 4964 | ASSERT(!HDR_IO_ERROR(hdr)); |
b9541d6b CW |
4965 | ASSERT(!HDR_IO_IN_PROGRESS(hdr)); |
4966 | ASSERT(hdr->b_l1hdr.b_acb == NULL); | |
4967 | ASSERT(hdr->b_l1hdr.b_datacnt > 0); | |
b128c09f | 4968 | if (l2arc) |
2a432414 | 4969 | hdr->b_flags |= ARC_FLAG_L2CACHE; |
3a17a7a9 | 4970 | if (l2arc_compress) |
2a432414 | 4971 | hdr->b_flags |= ARC_FLAG_L2COMPRESS; |
79c76d5b | 4972 | callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); |
34dc7c2f | 4973 | callback->awcb_ready = ready; |
e8b96c60 | 4974 | callback->awcb_physdone = physdone; |
34dc7c2f BB |
4975 | callback->awcb_done = done; |
4976 | callback->awcb_private = private; | |
4977 | callback->awcb_buf = buf; | |
b128c09f | 4978 | |
428870ff | 4979 | zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp, |
e8b96c60 MA |
4980 | arc_write_ready, arc_write_physdone, arc_write_done, callback, |
4981 | priority, zio_flags, zb); | |
34dc7c2f BB |
4982 | |
4983 | return (zio); | |
4984 | } | |
4985 | ||
34dc7c2f | 4986 | static int |
e8b96c60 | 4987 | arc_memory_throttle(uint64_t reserve, uint64_t txg) |
34dc7c2f BB |
4988 | { |
4989 | #ifdef _KERNEL | |
0c5493d4 BB |
4990 | if (zfs_arc_memory_throttle_disable) |
4991 | return (0); | |
4992 | ||
ca67b33a MA |
4993 | if (freemem > physmem * arc_lotsfree_percent / 100) |
4994 | return (0); | |
4995 | ||
4996 | if (arc_reclaim_needed()) { | |
4997 | /* memory is low, delay before restarting */ | |
34dc7c2f | 4998 | ARCSTAT_INCR(arcstat_memory_throttle_count, 1); |
570827e1 | 4999 | DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim); |
2e528b49 | 5000 | return (SET_ERROR(EAGAIN)); |
34dc7c2f | 5001 | } |
34dc7c2f BB |
5002 | #endif |
5003 | return (0); | |
5004 | } | |
5005 | ||
5006 | void | |
5007 | arc_tempreserve_clear(uint64_t reserve) | |
5008 | { | |
5009 | atomic_add_64(&arc_tempreserve, -reserve); | |
5010 | ASSERT((int64_t)arc_tempreserve >= 0); | |
5011 | } | |
5012 | ||
5013 | int | |
5014 | arc_tempreserve_space(uint64_t reserve, uint64_t txg) | |
5015 | { | |
5016 | int error; | |
9babb374 | 5017 | uint64_t anon_size; |
34dc7c2f | 5018 | |
34dc7c2f BB |
5019 | if (reserve > arc_c/4 && !arc_no_grow) |
5020 | arc_c = MIN(arc_c_max, reserve * 4); | |
12f9a6a3 BB |
5021 | |
5022 | /* | |
5023 | * Throttle when the calculated memory footprint for the TXG | |
5024 | * exceeds the target ARC size. | |
5025 | */ | |
570827e1 BB |
5026 | if (reserve > arc_c) { |
5027 | DMU_TX_STAT_BUMP(dmu_tx_memory_reserve); | |
12f9a6a3 | 5028 | return (SET_ERROR(ERESTART)); |
570827e1 | 5029 | } |
34dc7c2f | 5030 | |
9babb374 BB |
5031 | /* |
5032 | * Don't count loaned bufs as in flight dirty data to prevent long | |
5033 | * network delays from blocking transactions that are ready to be | |
5034 | * assigned to a txg. | |
5035 | */ | |
5036 | anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0); | |
5037 | ||
34dc7c2f BB |
5038 | /* |
5039 | * Writes will, almost always, require additional memory allocations | |
d3cc8b15 | 5040 | * in order to compress/encrypt/etc the data. We therefore need to |
34dc7c2f BB |
5041 | * make sure that there is sufficient available memory for this. |
5042 | */ | |
e8b96c60 MA |
5043 | error = arc_memory_throttle(reserve, txg); |
5044 | if (error != 0) | |
34dc7c2f BB |
5045 | return (error); |
5046 | ||
5047 | /* | |
5048 | * Throttle writes when the amount of dirty data in the cache | |
5049 | * gets too large. We try to keep the cache less than half full | |
5050 | * of dirty blocks so that our sync times don't grow too large. | |
5051 | * Note: if two requests come in concurrently, we might let them | |
5052 | * both succeed, when one of them should fail. Not a huge deal. | |
5053 | */ | |
9babb374 BB |
5054 | |
5055 | if (reserve + arc_tempreserve + anon_size > arc_c / 2 && | |
5056 | anon_size > arc_c / 4) { | |
34dc7c2f BB |
5057 | dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " |
5058 | "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n", | |
5059 | arc_tempreserve>>10, | |
5060 | arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10, | |
5061 | arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10, | |
5062 | reserve>>10, arc_c>>10); | |
570827e1 | 5063 | DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle); |
2e528b49 | 5064 | return (SET_ERROR(ERESTART)); |
34dc7c2f BB |
5065 | } |
5066 | atomic_add_64(&arc_tempreserve, reserve); | |
5067 | return (0); | |
5068 | } | |
5069 | ||
13be560d BB |
5070 | static void |
5071 | arc_kstat_update_state(arc_state_t *state, kstat_named_t *size, | |
5072 | kstat_named_t *evict_data, kstat_named_t *evict_metadata) | |
5073 | { | |
5074 | size->value.ui64 = state->arcs_size; | |
5075 | evict_data->value.ui64 = state->arcs_lsize[ARC_BUFC_DATA]; | |
5076 | evict_metadata->value.ui64 = state->arcs_lsize[ARC_BUFC_METADATA]; | |
5077 | } | |
5078 | ||
5079 | static int | |
5080 | arc_kstat_update(kstat_t *ksp, int rw) | |
5081 | { | |
5082 | arc_stats_t *as = ksp->ks_data; | |
5083 | ||
5084 | if (rw == KSTAT_WRITE) { | |
500445c0 | 5085 | return (EACCES); |
13be560d BB |
5086 | } else { |
5087 | arc_kstat_update_state(arc_anon, | |
5088 | &as->arcstat_anon_size, | |
500445c0 PS |
5089 | &as->arcstat_anon_evictable_data, |
5090 | &as->arcstat_anon_evictable_metadata); | |
13be560d BB |
5091 | arc_kstat_update_state(arc_mru, |
5092 | &as->arcstat_mru_size, | |
500445c0 PS |
5093 | &as->arcstat_mru_evictable_data, |
5094 | &as->arcstat_mru_evictable_metadata); | |
13be560d BB |
5095 | arc_kstat_update_state(arc_mru_ghost, |
5096 | &as->arcstat_mru_ghost_size, | |
500445c0 PS |
5097 | &as->arcstat_mru_ghost_evictable_data, |
5098 | &as->arcstat_mru_ghost_evictable_metadata); | |
13be560d BB |
5099 | arc_kstat_update_state(arc_mfu, |
5100 | &as->arcstat_mfu_size, | |
500445c0 PS |
5101 | &as->arcstat_mfu_evictable_data, |
5102 | &as->arcstat_mfu_evictable_metadata); | |
fc41c640 | 5103 | arc_kstat_update_state(arc_mfu_ghost, |
13be560d | 5104 | &as->arcstat_mfu_ghost_size, |
500445c0 PS |
5105 | &as->arcstat_mfu_ghost_evictable_data, |
5106 | &as->arcstat_mfu_ghost_evictable_metadata); | |
13be560d BB |
5107 | } |
5108 | ||
5109 | return (0); | |
5110 | } | |
5111 | ||
ca0bf58d PS |
5112 | /* |
5113 | * This function *must* return indices evenly distributed between all | |
5114 | * sublists of the multilist. This is needed due to how the ARC eviction | |
5115 | * code is laid out; arc_evict_state() assumes ARC buffers are evenly | |
5116 | * distributed between all sublists and uses this assumption when | |
5117 | * deciding which sublist to evict from and how much to evict from it. | |
5118 | */ | |
5119 | unsigned int | |
5120 | arc_state_multilist_index_func(multilist_t *ml, void *obj) | |
5121 | { | |
5122 | arc_buf_hdr_t *hdr = obj; | |
5123 | ||
5124 | /* | |
5125 | * We rely on b_dva to generate evenly distributed index | |
5126 | * numbers using buf_hash below. So, as an added precaution, | |
5127 | * let's make sure we never add empty buffers to the arc lists. | |
5128 | */ | |
5129 | ASSERT(!BUF_EMPTY(hdr)); | |
5130 | ||
5131 | /* | |
5132 | * The assumption here, is the hash value for a given | |
5133 | * arc_buf_hdr_t will remain constant throughout its lifetime | |
5134 | * (i.e. its b_spa, b_dva, and b_birth fields don't change). | |
5135 | * Thus, we don't need to store the header's sublist index | |
5136 | * on insertion, as this index can be recalculated on removal. | |
5137 | * | |
5138 | * Also, the low order bits of the hash value are thought to be | |
5139 | * distributed evenly. Otherwise, in the case that the multilist | |
5140 | * has a power of two number of sublists, each sublists' usage | |
5141 | * would not be evenly distributed. | |
5142 | */ | |
5143 | return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % | |
5144 | multilist_get_num_sublists(ml)); | |
5145 | } | |
5146 | ||
ca67b33a MA |
5147 | /* |
5148 | * Called during module initialization and periodically thereafter to | |
5149 | * apply reasonable changes to the exposed performance tunings. Non-zero | |
5150 | * zfs_* values which differ from the currently set values will be applied. | |
5151 | */ | |
5152 | static void | |
5153 | arc_tuning_update(void) | |
5154 | { | |
5155 | /* Valid range: 64M - <all physical memory> */ | |
5156 | if ((zfs_arc_max) && (zfs_arc_max != arc_c_max) && | |
5157 | (zfs_arc_max > 64 << 20) && (zfs_arc_max < ptob(physmem)) && | |
5158 | (zfs_arc_max > arc_c_min)) { | |
5159 | arc_c_max = zfs_arc_max; | |
5160 | arc_c = arc_c_max; | |
5161 | arc_p = (arc_c >> 1); | |
5162 | arc_meta_limit = MIN(arc_meta_limit, arc_c_max); | |
5163 | } | |
5164 | ||
5165 | /* Valid range: 32M - <arc_c_max> */ | |
5166 | if ((zfs_arc_min) && (zfs_arc_min != arc_c_min) && | |
5167 | (zfs_arc_min >= 2ULL << SPA_MAXBLOCKSHIFT) && | |
5168 | (zfs_arc_min <= arc_c_max)) { | |
5169 | arc_c_min = zfs_arc_min; | |
5170 | arc_c = MAX(arc_c, arc_c_min); | |
5171 | } | |
5172 | ||
5173 | /* Valid range: 16M - <arc_c_max> */ | |
5174 | if ((zfs_arc_meta_min) && (zfs_arc_meta_min != arc_meta_min) && | |
5175 | (zfs_arc_meta_min >= 1ULL << SPA_MAXBLOCKSHIFT) && | |
5176 | (zfs_arc_meta_min <= arc_c_max)) { | |
5177 | arc_meta_min = zfs_arc_meta_min; | |
5178 | arc_meta_limit = MAX(arc_meta_limit, arc_meta_min); | |
5179 | } | |
5180 | ||
5181 | /* Valid range: <arc_meta_min> - <arc_c_max> */ | |
5182 | if ((zfs_arc_meta_limit) && (zfs_arc_meta_limit != arc_meta_limit) && | |
5183 | (zfs_arc_meta_limit >= zfs_arc_meta_min) && | |
5184 | (zfs_arc_meta_limit <= arc_c_max)) | |
5185 | arc_meta_limit = zfs_arc_meta_limit; | |
5186 | ||
5187 | /* Valid range: 1 - N */ | |
5188 | if (zfs_arc_grow_retry) | |
5189 | arc_grow_retry = zfs_arc_grow_retry; | |
5190 | ||
5191 | /* Valid range: 1 - N */ | |
5192 | if (zfs_arc_shrink_shift) { | |
5193 | arc_shrink_shift = zfs_arc_shrink_shift; | |
5194 | arc_no_grow_shift = MIN(arc_no_grow_shift, arc_shrink_shift -1); | |
5195 | } | |
5196 | ||
5197 | /* Valid range: 1 - N ticks */ | |
5198 | if (zfs_arc_min_prefetch_lifespan) | |
5199 | arc_min_prefetch_lifespan = zfs_arc_min_prefetch_lifespan; | |
5200 | } | |
5201 | ||
34dc7c2f BB |
5202 | void |
5203 | arc_init(void) | |
5204 | { | |
ca67b33a MA |
5205 | /* |
5206 | * allmem is "all memory that we could possibly use". | |
5207 | */ | |
5208 | #ifdef _KERNEL | |
5209 | uint64_t allmem = ptob(physmem); | |
5210 | #else | |
5211 | uint64_t allmem = (physmem * PAGESIZE) / 2; | |
5212 | #endif | |
5213 | ||
ca0bf58d PS |
5214 | mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL); |
5215 | cv_init(&arc_reclaim_thread_cv, NULL, CV_DEFAULT, NULL); | |
5216 | cv_init(&arc_reclaim_waiters_cv, NULL, CV_DEFAULT, NULL); | |
5217 | ||
5218 | mutex_init(&arc_user_evicts_lock, NULL, MUTEX_DEFAULT, NULL); | |
5219 | cv_init(&arc_user_evicts_cv, NULL, CV_DEFAULT, NULL); | |
34dc7c2f BB |
5220 | |
5221 | /* Convert seconds to clock ticks */ | |
ca67b33a | 5222 | arc_min_prefetch_lifespan = 1 * hz; |
34dc7c2f BB |
5223 | |
5224 | /* Start out with 1/8 of all memory */ | |
ca67b33a | 5225 | arc_c = allmem / 8; |
34dc7c2f BB |
5226 | |
5227 | #ifdef _KERNEL | |
5228 | /* | |
5229 | * On architectures where the physical memory can be larger | |
5230 | * than the addressable space (intel in 32-bit mode), we may | |
5231 | * need to limit the cache to 1/8 of VM size. | |
5232 | */ | |
5233 | arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); | |
ca67b33a | 5234 | |
7cb67b45 BB |
5235 | /* |
5236 | * Register a shrinker to support synchronous (direct) memory | |
5237 | * reclaim from the arc. This is done to prevent kswapd from | |
5238 | * swapping out pages when it is preferable to shrink the arc. | |
5239 | */ | |
5240 | spl_register_shrinker(&arc_shrinker); | |
34dc7c2f BB |
5241 | #endif |
5242 | ||
ca67b33a | 5243 | /* Set min cache to allow safe operation of arc_adapt() */ |
121b3cae | 5244 | arc_c_min = 2ULL << SPA_MAXBLOCKSHIFT; |
ca67b33a MA |
5245 | /* Set max to 1/2 of all memory */ |
5246 | arc_c_max = allmem / 2; | |
34dc7c2f BB |
5247 | |
5248 | arc_c = arc_c_max; | |
5249 | arc_p = (arc_c >> 1); | |
5250 | ||
ca67b33a MA |
5251 | /* Set min to 1/2 of arc_c_min */ |
5252 | arc_meta_min = 1ULL << SPA_MAXBLOCKSHIFT; | |
5253 | /* Initialize maximum observed usage to zero */ | |
1834f2d8 | 5254 | arc_meta_max = 0; |
ca67b33a MA |
5255 | /* Set limit to 3/4 of arc_c_max with a floor of arc_meta_min */ |
5256 | arc_meta_limit = MAX((3 * arc_c_max) / 4, arc_meta_min); | |
34dc7c2f | 5257 | |
ca67b33a MA |
5258 | /* Apply user specified tunings */ |
5259 | arc_tuning_update(); | |
c52fca13 | 5260 | |
ca0bf58d | 5261 | if (zfs_arc_num_sublists_per_state < 1) |
ca67b33a | 5262 | zfs_arc_num_sublists_per_state = MAX(boot_ncpus, 1); |
ca0bf58d | 5263 | |
34dc7c2f BB |
5264 | /* if kmem_flags are set, lets try to use less memory */ |
5265 | if (kmem_debugging()) | |
5266 | arc_c = arc_c / 2; | |
5267 | if (arc_c < arc_c_min) | |
5268 | arc_c = arc_c_min; | |
5269 | ||
5270 | arc_anon = &ARC_anon; | |
5271 | arc_mru = &ARC_mru; | |
5272 | arc_mru_ghost = &ARC_mru_ghost; | |
5273 | arc_mfu = &ARC_mfu; | |
5274 | arc_mfu_ghost = &ARC_mfu_ghost; | |
5275 | arc_l2c_only = &ARC_l2c_only; | |
5276 | arc_size = 0; | |
5277 | ||
ca0bf58d | 5278 | multilist_create(&arc_mru->arcs_list[ARC_BUFC_METADATA], |
b9541d6b | 5279 | sizeof (arc_buf_hdr_t), |
ca0bf58d PS |
5280 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
5281 | zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); | |
5282 | multilist_create(&arc_mru->arcs_list[ARC_BUFC_DATA], | |
b9541d6b | 5283 | sizeof (arc_buf_hdr_t), |
ca0bf58d PS |
5284 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
5285 | zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); | |
5286 | multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], | |
b9541d6b | 5287 | sizeof (arc_buf_hdr_t), |
ca0bf58d PS |
5288 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
5289 | zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); | |
5290 | multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], | |
b9541d6b | 5291 | sizeof (arc_buf_hdr_t), |
ca0bf58d PS |
5292 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
5293 | zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); | |
5294 | multilist_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA], | |
b9541d6b | 5295 | sizeof (arc_buf_hdr_t), |
ca0bf58d PS |
5296 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
5297 | zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); | |
5298 | multilist_create(&arc_mfu->arcs_list[ARC_BUFC_DATA], | |
b9541d6b | 5299 | sizeof (arc_buf_hdr_t), |
ca0bf58d PS |
5300 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
5301 | zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); | |
5302 | multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], | |
b9541d6b | 5303 | sizeof (arc_buf_hdr_t), |
ca0bf58d PS |
5304 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
5305 | zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); | |
5306 | multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], | |
b9541d6b | 5307 | sizeof (arc_buf_hdr_t), |
ca0bf58d PS |
5308 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
5309 | zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); | |
5310 | multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], | |
b9541d6b | 5311 | sizeof (arc_buf_hdr_t), |
ca0bf58d PS |
5312 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
5313 | zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); | |
5314 | multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], | |
b9541d6b | 5315 | sizeof (arc_buf_hdr_t), |
ca0bf58d PS |
5316 | offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), |
5317 | zfs_arc_num_sublists_per_state, arc_state_multilist_index_func); | |
34dc7c2f | 5318 | |
e0b0ca98 BB |
5319 | arc_anon->arcs_state = ARC_STATE_ANON; |
5320 | arc_mru->arcs_state = ARC_STATE_MRU; | |
5321 | arc_mru_ghost->arcs_state = ARC_STATE_MRU_GHOST; | |
5322 | arc_mfu->arcs_state = ARC_STATE_MFU; | |
5323 | arc_mfu_ghost->arcs_state = ARC_STATE_MFU_GHOST; | |
5324 | arc_l2c_only->arcs_state = ARC_STATE_L2C_ONLY; | |
5325 | ||
34dc7c2f BB |
5326 | buf_init(); |
5327 | ||
ca0bf58d PS |
5328 | arc_reclaim_thread_exit = FALSE; |
5329 | arc_user_evicts_thread_exit = FALSE; | |
ab26409d BB |
5330 | list_create(&arc_prune_list, sizeof (arc_prune_t), |
5331 | offsetof(arc_prune_t, p_node)); | |
34dc7c2f | 5332 | arc_eviction_list = NULL; |
ab26409d | 5333 | mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
5334 | bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t)); |
5335 | ||
f6046738 | 5336 | arc_prune_taskq = taskq_create("arc_prune", max_ncpus, minclsyspri, |
aa9af22c | 5337 | max_ncpus, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC); |
f6046738 | 5338 | |
34dc7c2f BB |
5339 | arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, |
5340 | sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); | |
5341 | ||
5342 | if (arc_ksp != NULL) { | |
5343 | arc_ksp->ks_data = &arc_stats; | |
13be560d | 5344 | arc_ksp->ks_update = arc_kstat_update; |
34dc7c2f BB |
5345 | kstat_install(arc_ksp); |
5346 | } | |
5347 | ||
ca67b33a | 5348 | (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, |
34dc7c2f BB |
5349 | TS_RUN, minclsyspri); |
5350 | ||
ca0bf58d PS |
5351 | (void) thread_create(NULL, 0, arc_user_evicts_thread, NULL, 0, &p0, |
5352 | TS_RUN, minclsyspri); | |
5353 | ||
34dc7c2f | 5354 | arc_dead = FALSE; |
b128c09f | 5355 | arc_warm = B_FALSE; |
34dc7c2f | 5356 | |
e8b96c60 MA |
5357 | /* |
5358 | * Calculate maximum amount of dirty data per pool. | |
5359 | * | |
5360 | * If it has been set by a module parameter, take that. | |
5361 | * Otherwise, use a percentage of physical memory defined by | |
5362 | * zfs_dirty_data_max_percent (default 10%) with a cap at | |
5363 | * zfs_dirty_data_max_max (default 25% of physical memory). | |
5364 | */ | |
5365 | if (zfs_dirty_data_max_max == 0) | |
5366 | zfs_dirty_data_max_max = physmem * PAGESIZE * | |
5367 | zfs_dirty_data_max_max_percent / 100; | |
5368 | ||
5369 | if (zfs_dirty_data_max == 0) { | |
5370 | zfs_dirty_data_max = physmem * PAGESIZE * | |
5371 | zfs_dirty_data_max_percent / 100; | |
5372 | zfs_dirty_data_max = MIN(zfs_dirty_data_max, | |
5373 | zfs_dirty_data_max_max); | |
5374 | } | |
34dc7c2f BB |
5375 | } |
5376 | ||
5377 | void | |
5378 | arc_fini(void) | |
5379 | { | |
ab26409d BB |
5380 | arc_prune_t *p; |
5381 | ||
7cb67b45 BB |
5382 | #ifdef _KERNEL |
5383 | spl_unregister_shrinker(&arc_shrinker); | |
5384 | #endif /* _KERNEL */ | |
5385 | ||
ca0bf58d PS |
5386 | mutex_enter(&arc_reclaim_lock); |
5387 | arc_reclaim_thread_exit = TRUE; | |
5388 | /* | |
5389 | * The reclaim thread will set arc_reclaim_thread_exit back to | |
5390 | * FALSE when it is finished exiting; we're waiting for that. | |
5391 | */ | |
5392 | while (arc_reclaim_thread_exit) { | |
5393 | cv_signal(&arc_reclaim_thread_cv); | |
5394 | cv_wait(&arc_reclaim_thread_cv, &arc_reclaim_lock); | |
5395 | } | |
5396 | mutex_exit(&arc_reclaim_lock); | |
5397 | ||
5398 | mutex_enter(&arc_user_evicts_lock); | |
5399 | arc_user_evicts_thread_exit = TRUE; | |
5400 | /* | |
5401 | * The user evicts thread will set arc_user_evicts_thread_exit | |
5402 | * to FALSE when it is finished exiting; we're waiting for that. | |
5403 | */ | |
5404 | while (arc_user_evicts_thread_exit) { | |
5405 | cv_signal(&arc_user_evicts_cv); | |
5406 | cv_wait(&arc_user_evicts_cv, &arc_user_evicts_lock); | |
5407 | } | |
5408 | mutex_exit(&arc_user_evicts_lock); | |
34dc7c2f | 5409 | |
ca0bf58d PS |
5410 | /* Use TRUE to ensure *all* buffers are evicted */ |
5411 | arc_flush(NULL, TRUE); | |
34dc7c2f BB |
5412 | |
5413 | arc_dead = TRUE; | |
5414 | ||
5415 | if (arc_ksp != NULL) { | |
5416 | kstat_delete(arc_ksp); | |
5417 | arc_ksp = NULL; | |
5418 | } | |
5419 | ||
f6046738 BB |
5420 | taskq_wait(arc_prune_taskq); |
5421 | taskq_destroy(arc_prune_taskq); | |
5422 | ||
ab26409d BB |
5423 | mutex_enter(&arc_prune_mtx); |
5424 | while ((p = list_head(&arc_prune_list)) != NULL) { | |
5425 | list_remove(&arc_prune_list, p); | |
5426 | refcount_remove(&p->p_refcnt, &arc_prune_list); | |
5427 | refcount_destroy(&p->p_refcnt); | |
5428 | kmem_free(p, sizeof (*p)); | |
5429 | } | |
5430 | mutex_exit(&arc_prune_mtx); | |
5431 | ||
5432 | list_destroy(&arc_prune_list); | |
5433 | mutex_destroy(&arc_prune_mtx); | |
ca0bf58d PS |
5434 | mutex_destroy(&arc_reclaim_lock); |
5435 | cv_destroy(&arc_reclaim_thread_cv); | |
5436 | cv_destroy(&arc_reclaim_waiters_cv); | |
5437 | ||
5438 | mutex_destroy(&arc_user_evicts_lock); | |
5439 | cv_destroy(&arc_user_evicts_cv); | |
5440 | ||
5441 | multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); | |
5442 | multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); | |
5443 | multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); | |
5444 | multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); | |
5445 | multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); | |
5446 | multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); | |
5447 | multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); | |
5448 | multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); | |
5449 | multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA]); | |
5450 | multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]); | |
34dc7c2f BB |
5451 | |
5452 | buf_fini(); | |
9babb374 | 5453 | |
b9541d6b | 5454 | ASSERT0(arc_loaned_bytes); |
34dc7c2f BB |
5455 | } |
5456 | ||
5457 | /* | |
5458 | * Level 2 ARC | |
5459 | * | |
5460 | * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. | |
5461 | * It uses dedicated storage devices to hold cached data, which are populated | |
5462 | * using large infrequent writes. The main role of this cache is to boost | |
5463 | * the performance of random read workloads. The intended L2ARC devices | |
5464 | * include short-stroked disks, solid state disks, and other media with | |
5465 | * substantially faster read latency than disk. | |
5466 | * | |
5467 | * +-----------------------+ | |
5468 | * | ARC | | |
5469 | * +-----------------------+ | |
5470 | * | ^ ^ | |
5471 | * | | | | |
5472 | * l2arc_feed_thread() arc_read() | |
5473 | * | | | | |
5474 | * | l2arc read | | |
5475 | * V | | | |
5476 | * +---------------+ | | |
5477 | * | L2ARC | | | |
5478 | * +---------------+ | | |
5479 | * | ^ | | |
5480 | * l2arc_write() | | | |
5481 | * | | | | |
5482 | * V | | | |
5483 | * +-------+ +-------+ | |
5484 | * | vdev | | vdev | | |
5485 | * | cache | | cache | | |
5486 | * +-------+ +-------+ | |
5487 | * +=========+ .-----. | |
5488 | * : L2ARC : |-_____-| | |
5489 | * : devices : | Disks | | |
5490 | * +=========+ `-_____-' | |
5491 | * | |
5492 | * Read requests are satisfied from the following sources, in order: | |
5493 | * | |
5494 | * 1) ARC | |
5495 | * 2) vdev cache of L2ARC devices | |
5496 | * 3) L2ARC devices | |
5497 | * 4) vdev cache of disks | |
5498 | * 5) disks | |
5499 | * | |
5500 | * Some L2ARC device types exhibit extremely slow write performance. | |
5501 | * To accommodate for this there are some significant differences between | |
5502 | * the L2ARC and traditional cache design: | |
5503 | * | |
5504 | * 1. There is no eviction path from the ARC to the L2ARC. Evictions from | |
5505 | * the ARC behave as usual, freeing buffers and placing headers on ghost | |
5506 | * lists. The ARC does not send buffers to the L2ARC during eviction as | |
5507 | * this would add inflated write latencies for all ARC memory pressure. | |
5508 | * | |
5509 | * 2. The L2ARC attempts to cache data from the ARC before it is evicted. | |
5510 | * It does this by periodically scanning buffers from the eviction-end of | |
5511 | * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are | |
3a17a7a9 SK |
5512 | * not already there. It scans until a headroom of buffers is satisfied, |
5513 | * which itself is a buffer for ARC eviction. If a compressible buffer is | |
5514 | * found during scanning and selected for writing to an L2ARC device, we | |
5515 | * temporarily boost scanning headroom during the next scan cycle to make | |
5516 | * sure we adapt to compression effects (which might significantly reduce | |
5517 | * the data volume we write to L2ARC). The thread that does this is | |
34dc7c2f BB |
5518 | * l2arc_feed_thread(), illustrated below; example sizes are included to |
5519 | * provide a better sense of ratio than this diagram: | |
5520 | * | |
5521 | * head --> tail | |
5522 | * +---------------------+----------+ | |
5523 | * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC | |
5524 | * +---------------------+----------+ | o L2ARC eligible | |
5525 | * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer | |
5526 | * +---------------------+----------+ | | |
5527 | * 15.9 Gbytes ^ 32 Mbytes | | |
5528 | * headroom | | |
5529 | * l2arc_feed_thread() | |
5530 | * | | |
5531 | * l2arc write hand <--[oooo]--' | |
5532 | * | 8 Mbyte | |
5533 | * | write max | |
5534 | * V | |
5535 | * +==============================+ | |
5536 | * L2ARC dev |####|#|###|###| |####| ... | | |
5537 | * +==============================+ | |
5538 | * 32 Gbytes | |
5539 | * | |
5540 | * 3. If an ARC buffer is copied to the L2ARC but then hit instead of | |
5541 | * evicted, then the L2ARC has cached a buffer much sooner than it probably | |
5542 | * needed to, potentially wasting L2ARC device bandwidth and storage. It is | |
5543 | * safe to say that this is an uncommon case, since buffers at the end of | |
5544 | * the ARC lists have moved there due to inactivity. | |
5545 | * | |
5546 | * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, | |
5547 | * then the L2ARC simply misses copying some buffers. This serves as a | |
5548 | * pressure valve to prevent heavy read workloads from both stalling the ARC | |
5549 | * with waits and clogging the L2ARC with writes. This also helps prevent | |
5550 | * the potential for the L2ARC to churn if it attempts to cache content too | |
5551 | * quickly, such as during backups of the entire pool. | |
5552 | * | |
b128c09f BB |
5553 | * 5. After system boot and before the ARC has filled main memory, there are |
5554 | * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru | |
5555 | * lists can remain mostly static. Instead of searching from tail of these | |
5556 | * lists as pictured, the l2arc_feed_thread() will search from the list heads | |
5557 | * for eligible buffers, greatly increasing its chance of finding them. | |
5558 | * | |
5559 | * The L2ARC device write speed is also boosted during this time so that | |
5560 | * the L2ARC warms up faster. Since there have been no ARC evictions yet, | |
5561 | * there are no L2ARC reads, and no fear of degrading read performance | |
5562 | * through increased writes. | |
5563 | * | |
5564 | * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that | |
34dc7c2f BB |
5565 | * the vdev queue can aggregate them into larger and fewer writes. Each |
5566 | * device is written to in a rotor fashion, sweeping writes through | |
5567 | * available space then repeating. | |
5568 | * | |
b128c09f | 5569 | * 7. The L2ARC does not store dirty content. It never needs to flush |
34dc7c2f BB |
5570 | * write buffers back to disk based storage. |
5571 | * | |
b128c09f | 5572 | * 8. If an ARC buffer is written (and dirtied) which also exists in the |
34dc7c2f BB |
5573 | * L2ARC, the now stale L2ARC buffer is immediately dropped. |
5574 | * | |
5575 | * The performance of the L2ARC can be tweaked by a number of tunables, which | |
5576 | * may be necessary for different workloads: | |
5577 | * | |
5578 | * l2arc_write_max max write bytes per interval | |
b128c09f | 5579 | * l2arc_write_boost extra write bytes during device warmup |
34dc7c2f | 5580 | * l2arc_noprefetch skip caching prefetched buffers |
3a17a7a9 | 5581 | * l2arc_nocompress skip compressing buffers |
34dc7c2f | 5582 | * l2arc_headroom number of max device writes to precache |
3a17a7a9 SK |
5583 | * l2arc_headroom_boost when we find compressed buffers during ARC |
5584 | * scanning, we multiply headroom by this | |
5585 | * percentage factor for the next scan cycle, | |
5586 | * since more compressed buffers are likely to | |
5587 | * be present | |
34dc7c2f BB |
5588 | * l2arc_feed_secs seconds between L2ARC writing |
5589 | * | |
5590 | * Tunables may be removed or added as future performance improvements are | |
5591 | * integrated, and also may become zpool properties. | |
d164b209 BB |
5592 | * |
5593 | * There are three key functions that control how the L2ARC warms up: | |
5594 | * | |
5595 | * l2arc_write_eligible() check if a buffer is eligible to cache | |
5596 | * l2arc_write_size() calculate how much to write | |
5597 | * l2arc_write_interval() calculate sleep delay between writes | |
5598 | * | |
5599 | * These three functions determine what to write, how much, and how quickly | |
5600 | * to send writes. | |
34dc7c2f BB |
5601 | */ |
5602 | ||
d164b209 | 5603 | static boolean_t |
2a432414 | 5604 | l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) |
d164b209 BB |
5605 | { |
5606 | /* | |
5607 | * A buffer is *not* eligible for the L2ARC if it: | |
5608 | * 1. belongs to a different spa. | |
428870ff BB |
5609 | * 2. is already cached on the L2ARC. |
5610 | * 3. has an I/O in progress (it may be an incomplete read). | |
5611 | * 4. is flagged not eligible (zfs property). | |
d164b209 | 5612 | */ |
b9541d6b | 5613 | if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) || |
2a432414 | 5614 | HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr)) |
d164b209 BB |
5615 | return (B_FALSE); |
5616 | ||
5617 | return (B_TRUE); | |
5618 | } | |
5619 | ||
5620 | static uint64_t | |
3a17a7a9 | 5621 | l2arc_write_size(void) |
d164b209 BB |
5622 | { |
5623 | uint64_t size; | |
5624 | ||
3a17a7a9 SK |
5625 | /* |
5626 | * Make sure our globals have meaningful values in case the user | |
5627 | * altered them. | |
5628 | */ | |
5629 | size = l2arc_write_max; | |
5630 | if (size == 0) { | |
5631 | cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " | |
5632 | "be greater than zero, resetting it to the default (%d)", | |
5633 | L2ARC_WRITE_SIZE); | |
5634 | size = l2arc_write_max = L2ARC_WRITE_SIZE; | |
5635 | } | |
d164b209 BB |
5636 | |
5637 | if (arc_warm == B_FALSE) | |
3a17a7a9 | 5638 | size += l2arc_write_boost; |
d164b209 BB |
5639 | |
5640 | return (size); | |
5641 | ||
5642 | } | |
5643 | ||
5644 | static clock_t | |
5645 | l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) | |
5646 | { | |
428870ff | 5647 | clock_t interval, next, now; |
d164b209 BB |
5648 | |
5649 | /* | |
5650 | * If the ARC lists are busy, increase our write rate; if the | |
5651 | * lists are stale, idle back. This is achieved by checking | |
5652 | * how much we previously wrote - if it was more than half of | |
5653 | * what we wanted, schedule the next write much sooner. | |
5654 | */ | |
5655 | if (l2arc_feed_again && wrote > (wanted / 2)) | |
5656 | interval = (hz * l2arc_feed_min_ms) / 1000; | |
5657 | else | |
5658 | interval = hz * l2arc_feed_secs; | |
5659 | ||
428870ff BB |
5660 | now = ddi_get_lbolt(); |
5661 | next = MAX(now, MIN(now + interval, began + interval)); | |
d164b209 BB |
5662 | |
5663 | return (next); | |
5664 | } | |
5665 | ||
34dc7c2f BB |
5666 | /* |
5667 | * Cycle through L2ARC devices. This is how L2ARC load balances. | |
b128c09f | 5668 | * If a device is returned, this also returns holding the spa config lock. |
34dc7c2f BB |
5669 | */ |
5670 | static l2arc_dev_t * | |
5671 | l2arc_dev_get_next(void) | |
5672 | { | |
b128c09f | 5673 | l2arc_dev_t *first, *next = NULL; |
34dc7c2f | 5674 | |
b128c09f BB |
5675 | /* |
5676 | * Lock out the removal of spas (spa_namespace_lock), then removal | |
5677 | * of cache devices (l2arc_dev_mtx). Once a device has been selected, | |
5678 | * both locks will be dropped and a spa config lock held instead. | |
5679 | */ | |
5680 | mutex_enter(&spa_namespace_lock); | |
5681 | mutex_enter(&l2arc_dev_mtx); | |
5682 | ||
5683 | /* if there are no vdevs, there is nothing to do */ | |
5684 | if (l2arc_ndev == 0) | |
5685 | goto out; | |
5686 | ||
5687 | first = NULL; | |
5688 | next = l2arc_dev_last; | |
5689 | do { | |
5690 | /* loop around the list looking for a non-faulted vdev */ | |
5691 | if (next == NULL) { | |
34dc7c2f | 5692 | next = list_head(l2arc_dev_list); |
b128c09f BB |
5693 | } else { |
5694 | next = list_next(l2arc_dev_list, next); | |
5695 | if (next == NULL) | |
5696 | next = list_head(l2arc_dev_list); | |
5697 | } | |
5698 | ||
5699 | /* if we have come back to the start, bail out */ | |
5700 | if (first == NULL) | |
5701 | first = next; | |
5702 | else if (next == first) | |
5703 | break; | |
5704 | ||
5705 | } while (vdev_is_dead(next->l2ad_vdev)); | |
5706 | ||
5707 | /* if we were unable to find any usable vdevs, return NULL */ | |
5708 | if (vdev_is_dead(next->l2ad_vdev)) | |
5709 | next = NULL; | |
34dc7c2f BB |
5710 | |
5711 | l2arc_dev_last = next; | |
5712 | ||
b128c09f BB |
5713 | out: |
5714 | mutex_exit(&l2arc_dev_mtx); | |
5715 | ||
5716 | /* | |
5717 | * Grab the config lock to prevent the 'next' device from being | |
5718 | * removed while we are writing to it. | |
5719 | */ | |
5720 | if (next != NULL) | |
5721 | spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); | |
5722 | mutex_exit(&spa_namespace_lock); | |
5723 | ||
34dc7c2f BB |
5724 | return (next); |
5725 | } | |
5726 | ||
b128c09f BB |
5727 | /* |
5728 | * Free buffers that were tagged for destruction. | |
5729 | */ | |
5730 | static void | |
0bc8fd78 | 5731 | l2arc_do_free_on_write(void) |
b128c09f BB |
5732 | { |
5733 | list_t *buflist; | |
5734 | l2arc_data_free_t *df, *df_prev; | |
5735 | ||
5736 | mutex_enter(&l2arc_free_on_write_mtx); | |
5737 | buflist = l2arc_free_on_write; | |
5738 | ||
5739 | for (df = list_tail(buflist); df; df = df_prev) { | |
5740 | df_prev = list_prev(buflist, df); | |
5741 | ASSERT(df->l2df_data != NULL); | |
5742 | ASSERT(df->l2df_func != NULL); | |
5743 | df->l2df_func(df->l2df_data, df->l2df_size); | |
5744 | list_remove(buflist, df); | |
5745 | kmem_free(df, sizeof (l2arc_data_free_t)); | |
5746 | } | |
5747 | ||
5748 | mutex_exit(&l2arc_free_on_write_mtx); | |
5749 | } | |
5750 | ||
34dc7c2f BB |
5751 | /* |
5752 | * A write to a cache device has completed. Update all headers to allow | |
5753 | * reads from these buffers to begin. | |
5754 | */ | |
5755 | static void | |
5756 | l2arc_write_done(zio_t *zio) | |
5757 | { | |
5758 | l2arc_write_callback_t *cb; | |
5759 | l2arc_dev_t *dev; | |
5760 | list_t *buflist; | |
2a432414 | 5761 | arc_buf_hdr_t *head, *hdr, *hdr_prev; |
34dc7c2f | 5762 | kmutex_t *hash_lock; |
3bec585e | 5763 | int64_t bytes_dropped = 0; |
34dc7c2f BB |
5764 | |
5765 | cb = zio->io_private; | |
5766 | ASSERT(cb != NULL); | |
5767 | dev = cb->l2wcb_dev; | |
5768 | ASSERT(dev != NULL); | |
5769 | head = cb->l2wcb_head; | |
5770 | ASSERT(head != NULL); | |
b9541d6b | 5771 | buflist = &dev->l2ad_buflist; |
34dc7c2f BB |
5772 | ASSERT(buflist != NULL); |
5773 | DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, | |
5774 | l2arc_write_callback_t *, cb); | |
5775 | ||
5776 | if (zio->io_error != 0) | |
5777 | ARCSTAT_BUMP(arcstat_l2_writes_error); | |
5778 | ||
34dc7c2f BB |
5779 | /* |
5780 | * All writes completed, or an error was hit. | |
5781 | */ | |
ca0bf58d PS |
5782 | top: |
5783 | mutex_enter(&dev->l2ad_mtx); | |
2a432414 GW |
5784 | for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { |
5785 | hdr_prev = list_prev(buflist, hdr); | |
34dc7c2f | 5786 | |
2a432414 | 5787 | hash_lock = HDR_LOCK(hdr); |
ca0bf58d PS |
5788 | |
5789 | /* | |
5790 | * We cannot use mutex_enter or else we can deadlock | |
5791 | * with l2arc_write_buffers (due to swapping the order | |
5792 | * the hash lock and l2ad_mtx are taken). | |
5793 | */ | |
34dc7c2f BB |
5794 | if (!mutex_tryenter(hash_lock)) { |
5795 | /* | |
ca0bf58d PS |
5796 | * Missed the hash lock. We must retry so we |
5797 | * don't leave the ARC_FLAG_L2_WRITING bit set. | |
34dc7c2f | 5798 | */ |
ca0bf58d PS |
5799 | ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); |
5800 | ||
5801 | /* | |
5802 | * We don't want to rescan the headers we've | |
5803 | * already marked as having been written out, so | |
5804 | * we reinsert the head node so we can pick up | |
5805 | * where we left off. | |
5806 | */ | |
5807 | list_remove(buflist, head); | |
5808 | list_insert_after(buflist, hdr, head); | |
5809 | ||
5810 | mutex_exit(&dev->l2ad_mtx); | |
5811 | ||
5812 | /* | |
5813 | * We wait for the hash lock to become available | |
5814 | * to try and prevent busy waiting, and increase | |
5815 | * the chance we'll be able to acquire the lock | |
5816 | * the next time around. | |
5817 | */ | |
5818 | mutex_enter(hash_lock); | |
5819 | mutex_exit(hash_lock); | |
5820 | goto top; | |
34dc7c2f BB |
5821 | } |
5822 | ||
b9541d6b | 5823 | /* |
ca0bf58d PS |
5824 | * We could not have been moved into the arc_l2c_only |
5825 | * state while in-flight due to our ARC_FLAG_L2_WRITING | |
5826 | * bit being set. Let's just ensure that's being enforced. | |
5827 | */ | |
5828 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
5829 | ||
5830 | /* | |
5831 | * We may have allocated a buffer for L2ARC compression, | |
5832 | * we must release it to avoid leaking this data. | |
b9541d6b | 5833 | */ |
ca0bf58d | 5834 | l2arc_release_cdata_buf(hdr); |
b9541d6b | 5835 | |
34dc7c2f BB |
5836 | if (zio->io_error != 0) { |
5837 | /* | |
b128c09f | 5838 | * Error - drop L2ARC entry. |
34dc7c2f | 5839 | */ |
2a432414 | 5840 | list_remove(buflist, hdr); |
b9541d6b CW |
5841 | hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR; |
5842 | ||
5843 | ARCSTAT_INCR(arcstat_l2_asize, -hdr->b_l2hdr.b_asize); | |
2a432414 | 5844 | ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size); |
d962d5da PS |
5845 | |
5846 | bytes_dropped += hdr->b_l2hdr.b_asize; | |
5847 | (void) refcount_remove_many(&dev->l2ad_alloc, | |
5848 | hdr->b_l2hdr.b_asize, hdr); | |
34dc7c2f BB |
5849 | } |
5850 | ||
5851 | /* | |
ca0bf58d PS |
5852 | * Allow ARC to begin reads and ghost list evictions to |
5853 | * this L2ARC entry. | |
34dc7c2f | 5854 | */ |
2a432414 | 5855 | hdr->b_flags &= ~ARC_FLAG_L2_WRITING; |
34dc7c2f BB |
5856 | |
5857 | mutex_exit(hash_lock); | |
5858 | } | |
5859 | ||
5860 | atomic_inc_64(&l2arc_writes_done); | |
5861 | list_remove(buflist, head); | |
b9541d6b CW |
5862 | ASSERT(!HDR_HAS_L1HDR(head)); |
5863 | kmem_cache_free(hdr_l2only_cache, head); | |
5864 | mutex_exit(&dev->l2ad_mtx); | |
34dc7c2f | 5865 | |
3bec585e SK |
5866 | vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); |
5867 | ||
b128c09f | 5868 | l2arc_do_free_on_write(); |
34dc7c2f BB |
5869 | |
5870 | kmem_free(cb, sizeof (l2arc_write_callback_t)); | |
5871 | } | |
5872 | ||
5873 | /* | |
5874 | * A read to a cache device completed. Validate buffer contents before | |
5875 | * handing over to the regular ARC routines. | |
5876 | */ | |
5877 | static void | |
5878 | l2arc_read_done(zio_t *zio) | |
5879 | { | |
5880 | l2arc_read_callback_t *cb; | |
5881 | arc_buf_hdr_t *hdr; | |
5882 | arc_buf_t *buf; | |
34dc7c2f | 5883 | kmutex_t *hash_lock; |
b128c09f BB |
5884 | int equal; |
5885 | ||
5886 | ASSERT(zio->io_vd != NULL); | |
5887 | ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); | |
5888 | ||
5889 | spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); | |
34dc7c2f BB |
5890 | |
5891 | cb = zio->io_private; | |
5892 | ASSERT(cb != NULL); | |
5893 | buf = cb->l2rcb_buf; | |
5894 | ASSERT(buf != NULL); | |
34dc7c2f | 5895 | |
428870ff | 5896 | hash_lock = HDR_LOCK(buf->b_hdr); |
34dc7c2f | 5897 | mutex_enter(hash_lock); |
428870ff BB |
5898 | hdr = buf->b_hdr; |
5899 | ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); | |
34dc7c2f | 5900 | |
3a17a7a9 SK |
5901 | /* |
5902 | * If the buffer was compressed, decompress it first. | |
5903 | */ | |
5904 | if (cb->l2rcb_compress != ZIO_COMPRESS_OFF) | |
5905 | l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress); | |
5906 | ASSERT(zio->io_data != NULL); | |
5907 | ||
34dc7c2f BB |
5908 | /* |
5909 | * Check this survived the L2ARC journey. | |
5910 | */ | |
5911 | equal = arc_cksum_equal(buf); | |
5912 | if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) { | |
5913 | mutex_exit(hash_lock); | |
5914 | zio->io_private = buf; | |
b128c09f BB |
5915 | zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ |
5916 | zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ | |
34dc7c2f BB |
5917 | arc_read_done(zio); |
5918 | } else { | |
5919 | mutex_exit(hash_lock); | |
5920 | /* | |
5921 | * Buffer didn't survive caching. Increment stats and | |
5922 | * reissue to the original storage device. | |
5923 | */ | |
b128c09f | 5924 | if (zio->io_error != 0) { |
34dc7c2f | 5925 | ARCSTAT_BUMP(arcstat_l2_io_error); |
b128c09f | 5926 | } else { |
2e528b49 | 5927 | zio->io_error = SET_ERROR(EIO); |
b128c09f | 5928 | } |
34dc7c2f BB |
5929 | if (!equal) |
5930 | ARCSTAT_BUMP(arcstat_l2_cksum_bad); | |
5931 | ||
34dc7c2f | 5932 | /* |
b128c09f BB |
5933 | * If there's no waiter, issue an async i/o to the primary |
5934 | * storage now. If there *is* a waiter, the caller must | |
5935 | * issue the i/o in a context where it's OK to block. | |
34dc7c2f | 5936 | */ |
d164b209 BB |
5937 | if (zio->io_waiter == NULL) { |
5938 | zio_t *pio = zio_unique_parent(zio); | |
5939 | ||
5940 | ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); | |
5941 | ||
5942 | zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp, | |
b128c09f BB |
5943 | buf->b_data, zio->io_size, arc_read_done, buf, |
5944 | zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb)); | |
d164b209 | 5945 | } |
34dc7c2f BB |
5946 | } |
5947 | ||
5948 | kmem_free(cb, sizeof (l2arc_read_callback_t)); | |
5949 | } | |
5950 | ||
5951 | /* | |
5952 | * This is the list priority from which the L2ARC will search for pages to | |
5953 | * cache. This is used within loops (0..3) to cycle through lists in the | |
5954 | * desired order. This order can have a significant effect on cache | |
5955 | * performance. | |
5956 | * | |
5957 | * Currently the metadata lists are hit first, MFU then MRU, followed by | |
5958 | * the data lists. This function returns a locked list, and also returns | |
5959 | * the lock pointer. | |
5960 | */ | |
ca0bf58d PS |
5961 | static multilist_sublist_t * |
5962 | l2arc_sublist_lock(int list_num) | |
34dc7c2f | 5963 | { |
ca0bf58d PS |
5964 | multilist_t *ml = NULL; |
5965 | unsigned int idx; | |
34dc7c2f BB |
5966 | |
5967 | ASSERT(list_num >= 0 && list_num <= 3); | |
5968 | ||
5969 | switch (list_num) { | |
5970 | case 0: | |
ca0bf58d | 5971 | ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; |
34dc7c2f BB |
5972 | break; |
5973 | case 1: | |
ca0bf58d | 5974 | ml = &arc_mru->arcs_list[ARC_BUFC_METADATA]; |
34dc7c2f BB |
5975 | break; |
5976 | case 2: | |
ca0bf58d | 5977 | ml = &arc_mfu->arcs_list[ARC_BUFC_DATA]; |
34dc7c2f BB |
5978 | break; |
5979 | case 3: | |
ca0bf58d | 5980 | ml = &arc_mru->arcs_list[ARC_BUFC_DATA]; |
34dc7c2f BB |
5981 | break; |
5982 | } | |
5983 | ||
ca0bf58d PS |
5984 | /* |
5985 | * Return a randomly-selected sublist. This is acceptable | |
5986 | * because the caller feeds only a little bit of data for each | |
5987 | * call (8MB). Subsequent calls will result in different | |
5988 | * sublists being selected. | |
5989 | */ | |
5990 | idx = multilist_get_random_index(ml); | |
5991 | return (multilist_sublist_lock(ml, idx)); | |
34dc7c2f BB |
5992 | } |
5993 | ||
5994 | /* | |
5995 | * Evict buffers from the device write hand to the distance specified in | |
5996 | * bytes. This distance may span populated buffers, it may span nothing. | |
5997 | * This is clearing a region on the L2ARC device ready for writing. | |
5998 | * If the 'all' boolean is set, every buffer is evicted. | |
5999 | */ | |
6000 | static void | |
6001 | l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) | |
6002 | { | |
6003 | list_t *buflist; | |
2a432414 | 6004 | arc_buf_hdr_t *hdr, *hdr_prev; |
34dc7c2f BB |
6005 | kmutex_t *hash_lock; |
6006 | uint64_t taddr; | |
6007 | ||
b9541d6b | 6008 | buflist = &dev->l2ad_buflist; |
34dc7c2f BB |
6009 | |
6010 | if (!all && dev->l2ad_first) { | |
6011 | /* | |
6012 | * This is the first sweep through the device. There is | |
6013 | * nothing to evict. | |
6014 | */ | |
6015 | return; | |
6016 | } | |
6017 | ||
b128c09f | 6018 | if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) { |
34dc7c2f BB |
6019 | /* |
6020 | * When nearing the end of the device, evict to the end | |
6021 | * before the device write hand jumps to the start. | |
6022 | */ | |
6023 | taddr = dev->l2ad_end; | |
6024 | } else { | |
6025 | taddr = dev->l2ad_hand + distance; | |
6026 | } | |
6027 | DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, | |
6028 | uint64_t, taddr, boolean_t, all); | |
6029 | ||
6030 | top: | |
b9541d6b | 6031 | mutex_enter(&dev->l2ad_mtx); |
2a432414 GW |
6032 | for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { |
6033 | hdr_prev = list_prev(buflist, hdr); | |
34dc7c2f | 6034 | |
2a432414 | 6035 | hash_lock = HDR_LOCK(hdr); |
ca0bf58d PS |
6036 | |
6037 | /* | |
6038 | * We cannot use mutex_enter or else we can deadlock | |
6039 | * with l2arc_write_buffers (due to swapping the order | |
6040 | * the hash lock and l2ad_mtx are taken). | |
6041 | */ | |
34dc7c2f BB |
6042 | if (!mutex_tryenter(hash_lock)) { |
6043 | /* | |
6044 | * Missed the hash lock. Retry. | |
6045 | */ | |
6046 | ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); | |
b9541d6b | 6047 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
6048 | mutex_enter(hash_lock); |
6049 | mutex_exit(hash_lock); | |
6050 | goto top; | |
6051 | } | |
6052 | ||
2a432414 | 6053 | if (HDR_L2_WRITE_HEAD(hdr)) { |
34dc7c2f BB |
6054 | /* |
6055 | * We hit a write head node. Leave it for | |
6056 | * l2arc_write_done(). | |
6057 | */ | |
2a432414 | 6058 | list_remove(buflist, hdr); |
34dc7c2f BB |
6059 | mutex_exit(hash_lock); |
6060 | continue; | |
6061 | } | |
6062 | ||
b9541d6b CW |
6063 | if (!all && HDR_HAS_L2HDR(hdr) && |
6064 | (hdr->b_l2hdr.b_daddr > taddr || | |
6065 | hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { | |
34dc7c2f BB |
6066 | /* |
6067 | * We've evicted to the target address, | |
6068 | * or the end of the device. | |
6069 | */ | |
6070 | mutex_exit(hash_lock); | |
6071 | break; | |
6072 | } | |
6073 | ||
b9541d6b CW |
6074 | ASSERT(HDR_HAS_L2HDR(hdr)); |
6075 | if (!HDR_HAS_L1HDR(hdr)) { | |
2a432414 | 6076 | ASSERT(!HDR_L2_READING(hdr)); |
34dc7c2f BB |
6077 | /* |
6078 | * This doesn't exist in the ARC. Destroy. | |
6079 | * arc_hdr_destroy() will call list_remove() | |
6080 | * and decrement arcstat_l2_size. | |
6081 | */ | |
2a432414 GW |
6082 | arc_change_state(arc_anon, hdr, hash_lock); |
6083 | arc_hdr_destroy(hdr); | |
34dc7c2f | 6084 | } else { |
b9541d6b CW |
6085 | ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); |
6086 | ARCSTAT_BUMP(arcstat_l2_evict_l1cached); | |
b128c09f BB |
6087 | /* |
6088 | * Invalidate issued or about to be issued | |
6089 | * reads, since we may be about to write | |
6090 | * over this location. | |
6091 | */ | |
2a432414 | 6092 | if (HDR_L2_READING(hdr)) { |
b128c09f | 6093 | ARCSTAT_BUMP(arcstat_l2_evict_reading); |
2a432414 | 6094 | hdr->b_flags |= ARC_FLAG_L2_EVICTED; |
b128c09f BB |
6095 | } |
6096 | ||
ca0bf58d PS |
6097 | /* Ensure this header has finished being written */ |
6098 | ASSERT(!HDR_L2_WRITING(hdr)); | |
6099 | ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); | |
d962d5da PS |
6100 | |
6101 | arc_hdr_l2hdr_destroy(hdr); | |
34dc7c2f BB |
6102 | } |
6103 | mutex_exit(hash_lock); | |
6104 | } | |
b9541d6b | 6105 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
6106 | } |
6107 | ||
6108 | /* | |
6109 | * Find and write ARC buffers to the L2ARC device. | |
6110 | * | |
2a432414 | 6111 | * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid |
34dc7c2f | 6112 | * for reading until they have completed writing. |
3a17a7a9 SK |
6113 | * The headroom_boost is an in-out parameter used to maintain headroom boost |
6114 | * state between calls to this function. | |
6115 | * | |
6116 | * Returns the number of bytes actually written (which may be smaller than | |
6117 | * the delta by which the device hand has changed due to alignment). | |
34dc7c2f | 6118 | */ |
d164b209 | 6119 | static uint64_t |
3a17a7a9 SK |
6120 | l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz, |
6121 | boolean_t *headroom_boost) | |
34dc7c2f | 6122 | { |
2a432414 | 6123 | arc_buf_hdr_t *hdr, *hdr_prev, *head; |
ef56b078 AG |
6124 | uint64_t write_asize, write_sz, headroom, buf_compress_minsz, |
6125 | stats_size; | |
34dc7c2f | 6126 | void *buf_data; |
3a17a7a9 | 6127 | boolean_t full; |
34dc7c2f BB |
6128 | l2arc_write_callback_t *cb; |
6129 | zio_t *pio, *wzio; | |
3541dc6d | 6130 | uint64_t guid = spa_load_guid(spa); |
d6320ddb | 6131 | int try; |
3a17a7a9 | 6132 | const boolean_t do_headroom_boost = *headroom_boost; |
34dc7c2f | 6133 | |
34dc7c2f BB |
6134 | ASSERT(dev->l2ad_vdev != NULL); |
6135 | ||
3a17a7a9 SK |
6136 | /* Lower the flag now, we might want to raise it again later. */ |
6137 | *headroom_boost = B_FALSE; | |
6138 | ||
34dc7c2f | 6139 | pio = NULL; |
ef56b078 | 6140 | write_sz = write_asize = 0; |
34dc7c2f | 6141 | full = B_FALSE; |
b9541d6b | 6142 | head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); |
2a432414 | 6143 | head->b_flags |= ARC_FLAG_L2_WRITE_HEAD; |
b9541d6b | 6144 | head->b_flags |= ARC_FLAG_HAS_L2HDR; |
34dc7c2f | 6145 | |
3a17a7a9 SK |
6146 | /* |
6147 | * We will want to try to compress buffers that are at least 2x the | |
6148 | * device sector size. | |
6149 | */ | |
6150 | buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift; | |
6151 | ||
34dc7c2f BB |
6152 | /* |
6153 | * Copy buffers for L2ARC writing. | |
6154 | */ | |
d6320ddb | 6155 | for (try = 0; try <= 3; try++) { |
ca0bf58d | 6156 | multilist_sublist_t *mls = l2arc_sublist_lock(try); |
3a17a7a9 SK |
6157 | uint64_t passed_sz = 0; |
6158 | ||
b128c09f BB |
6159 | /* |
6160 | * L2ARC fast warmup. | |
6161 | * | |
6162 | * Until the ARC is warm and starts to evict, read from the | |
6163 | * head of the ARC lists rather than the tail. | |
6164 | */ | |
b128c09f | 6165 | if (arc_warm == B_FALSE) |
ca0bf58d | 6166 | hdr = multilist_sublist_head(mls); |
b128c09f | 6167 | else |
ca0bf58d | 6168 | hdr = multilist_sublist_tail(mls); |
b128c09f | 6169 | |
3a17a7a9 SK |
6170 | headroom = target_sz * l2arc_headroom; |
6171 | if (do_headroom_boost) | |
6172 | headroom = (headroom * l2arc_headroom_boost) / 100; | |
6173 | ||
2a432414 | 6174 | for (; hdr; hdr = hdr_prev) { |
3a17a7a9 SK |
6175 | kmutex_t *hash_lock; |
6176 | uint64_t buf_sz; | |
ef56b078 | 6177 | uint64_t buf_a_sz; |
3a17a7a9 | 6178 | |
b128c09f | 6179 | if (arc_warm == B_FALSE) |
ca0bf58d | 6180 | hdr_prev = multilist_sublist_next(mls, hdr); |
b128c09f | 6181 | else |
ca0bf58d | 6182 | hdr_prev = multilist_sublist_prev(mls, hdr); |
34dc7c2f | 6183 | |
2a432414 | 6184 | hash_lock = HDR_LOCK(hdr); |
3a17a7a9 | 6185 | if (!mutex_tryenter(hash_lock)) { |
34dc7c2f BB |
6186 | /* |
6187 | * Skip this buffer rather than waiting. | |
6188 | */ | |
6189 | continue; | |
6190 | } | |
6191 | ||
2a432414 | 6192 | passed_sz += hdr->b_size; |
34dc7c2f BB |
6193 | if (passed_sz > headroom) { |
6194 | /* | |
6195 | * Searched too far. | |
6196 | */ | |
6197 | mutex_exit(hash_lock); | |
6198 | break; | |
6199 | } | |
6200 | ||
2a432414 | 6201 | if (!l2arc_write_eligible(guid, hdr)) { |
34dc7c2f BB |
6202 | mutex_exit(hash_lock); |
6203 | continue; | |
6204 | } | |
6205 | ||
ef56b078 AG |
6206 | /* |
6207 | * Assume that the buffer is not going to be compressed | |
6208 | * and could take more space on disk because of a larger | |
6209 | * disk block size. | |
6210 | */ | |
6211 | buf_sz = hdr->b_size; | |
6212 | buf_a_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); | |
6213 | ||
6214 | if ((write_asize + buf_a_sz) > target_sz) { | |
34dc7c2f BB |
6215 | full = B_TRUE; |
6216 | mutex_exit(hash_lock); | |
6217 | break; | |
6218 | } | |
6219 | ||
34dc7c2f BB |
6220 | if (pio == NULL) { |
6221 | /* | |
6222 | * Insert a dummy header on the buflist so | |
6223 | * l2arc_write_done() can find where the | |
6224 | * write buffers begin without searching. | |
6225 | */ | |
ca0bf58d | 6226 | mutex_enter(&dev->l2ad_mtx); |
b9541d6b | 6227 | list_insert_head(&dev->l2ad_buflist, head); |
ca0bf58d | 6228 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 6229 | |
409dc1a5 | 6230 | cb = kmem_alloc(sizeof (l2arc_write_callback_t), |
79c76d5b | 6231 | KM_SLEEP); |
34dc7c2f BB |
6232 | cb->l2wcb_dev = dev; |
6233 | cb->l2wcb_head = head; | |
6234 | pio = zio_root(spa, l2arc_write_done, cb, | |
6235 | ZIO_FLAG_CANFAIL); | |
6236 | } | |
6237 | ||
6238 | /* | |
6239 | * Create and add a new L2ARC header. | |
6240 | */ | |
b9541d6b CW |
6241 | hdr->b_l2hdr.b_dev = dev; |
6242 | arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); | |
2a432414 | 6243 | hdr->b_flags |= ARC_FLAG_L2_WRITING; |
3a17a7a9 SK |
6244 | /* |
6245 | * Temporarily stash the data buffer in b_tmp_cdata. | |
6246 | * The subsequent write step will pick it up from | |
b9541d6b | 6247 | * there. This is because can't access b_l1hdr.b_buf |
3a17a7a9 SK |
6248 | * without holding the hash_lock, which we in turn |
6249 | * can't access without holding the ARC list locks | |
6250 | * (which we want to avoid during compression/writing) | |
6251 | */ | |
b9541d6b CW |
6252 | HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); |
6253 | hdr->b_l2hdr.b_asize = hdr->b_size; | |
6254 | hdr->b_l2hdr.b_hits = 0; | |
6255 | hdr->b_l1hdr.b_tmp_cdata = hdr->b_l1hdr.b_buf->b_data; | |
3a17a7a9 | 6256 | |
d962d5da PS |
6257 | /* |
6258 | * Explicitly set the b_daddr field to a known | |
6259 | * value which means "invalid address". This | |
6260 | * enables us to differentiate which stage of | |
6261 | * l2arc_write_buffers() the particular header | |
6262 | * is in (e.g. this loop, or the one below). | |
6263 | * ARC_FLAG_L2_WRITING is not enough to make | |
6264 | * this distinction, and we need to know in | |
6265 | * order to do proper l2arc vdev accounting in | |
6266 | * arc_release() and arc_hdr_destroy(). | |
6267 | * | |
6268 | * Note, we can't use a new flag to distinguish | |
6269 | * the two stages because we don't hold the | |
6270 | * header's hash_lock below, in the second stage | |
6271 | * of this function. Thus, we can't simply | |
6272 | * change the b_flags field to denote that the | |
6273 | * IO has been sent. We can change the b_daddr | |
6274 | * field of the L2 portion, though, since we'll | |
6275 | * be holding the l2ad_mtx; which is why we're | |
6276 | * using it to denote the header's state change. | |
6277 | */ | |
6278 | hdr->b_l2hdr.b_daddr = L2ARC_ADDR_UNSET; | |
b9541d6b | 6279 | hdr->b_flags |= ARC_FLAG_HAS_L2HDR; |
3a17a7a9 | 6280 | |
ca0bf58d | 6281 | mutex_enter(&dev->l2ad_mtx); |
b9541d6b | 6282 | list_insert_head(&dev->l2ad_buflist, hdr); |
ca0bf58d | 6283 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f BB |
6284 | |
6285 | /* | |
6286 | * Compute and store the buffer cksum before | |
6287 | * writing. On debug the cksum is verified first. | |
6288 | */ | |
b9541d6b CW |
6289 | arc_cksum_verify(hdr->b_l1hdr.b_buf); |
6290 | arc_cksum_compute(hdr->b_l1hdr.b_buf, B_TRUE); | |
34dc7c2f BB |
6291 | |
6292 | mutex_exit(hash_lock); | |
6293 | ||
3a17a7a9 | 6294 | write_sz += buf_sz; |
ef56b078 | 6295 | write_asize += buf_a_sz; |
3a17a7a9 SK |
6296 | } |
6297 | ||
ca0bf58d | 6298 | multilist_sublist_unlock(mls); |
3a17a7a9 SK |
6299 | |
6300 | if (full == B_TRUE) | |
6301 | break; | |
6302 | } | |
6303 | ||
6304 | /* No buffers selected for writing? */ | |
6305 | if (pio == NULL) { | |
6306 | ASSERT0(write_sz); | |
b9541d6b CW |
6307 | ASSERT(!HDR_HAS_L1HDR(head)); |
6308 | kmem_cache_free(hdr_l2only_cache, head); | |
3a17a7a9 SK |
6309 | return (0); |
6310 | } | |
6311 | ||
ca0bf58d PS |
6312 | mutex_enter(&dev->l2ad_mtx); |
6313 | ||
ef56b078 AG |
6314 | /* |
6315 | * Note that elsewhere in this file arcstat_l2_asize | |
6316 | * and the used space on l2ad_vdev are updated using b_asize, | |
6317 | * which is not necessarily rounded up to the device block size. | |
6318 | * Too keep accounting consistent we do the same here as well: | |
6319 | * stats_size accumulates the sum of b_asize of the written buffers, | |
6320 | * while write_asize accumulates the sum of b_asize rounded up | |
6321 | * to the device block size. | |
6322 | * The latter sum is used only to validate the corectness of the code. | |
6323 | */ | |
6324 | stats_size = 0; | |
6325 | write_asize = 0; | |
6326 | ||
3a17a7a9 SK |
6327 | /* |
6328 | * Now start writing the buffers. We're starting at the write head | |
6329 | * and work backwards, retracing the course of the buffer selector | |
6330 | * loop above. | |
6331 | */ | |
b9541d6b CW |
6332 | for (hdr = list_prev(&dev->l2ad_buflist, head); hdr; |
6333 | hdr = list_prev(&dev->l2ad_buflist, hdr)) { | |
3a17a7a9 SK |
6334 | uint64_t buf_sz; |
6335 | ||
ca0bf58d PS |
6336 | /* |
6337 | * We rely on the L1 portion of the header below, so | |
6338 | * it's invalid for this header to have been evicted out | |
6339 | * of the ghost cache, prior to being written out. The | |
6340 | * ARC_FLAG_L2_WRITING bit ensures this won't happen. | |
6341 | */ | |
6342 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
6343 | ||
3a17a7a9 SK |
6344 | /* |
6345 | * We shouldn't need to lock the buffer here, since we flagged | |
2a432414 GW |
6346 | * it as ARC_FLAG_L2_WRITING in the previous step, but we must |
6347 | * take care to only access its L2 cache parameters. In | |
b9541d6b | 6348 | * particular, hdr->l1hdr.b_buf may be invalid by now due to |
2a432414 | 6349 | * ARC eviction. |
3a17a7a9 | 6350 | */ |
b9541d6b | 6351 | hdr->b_l2hdr.b_daddr = dev->l2ad_hand; |
3a17a7a9 | 6352 | |
b9541d6b CW |
6353 | if ((!l2arc_nocompress && HDR_L2COMPRESS(hdr)) && |
6354 | hdr->b_l2hdr.b_asize >= buf_compress_minsz) { | |
6355 | if (l2arc_compress_buf(hdr)) { | |
3a17a7a9 SK |
6356 | /* |
6357 | * If compression succeeded, enable headroom | |
6358 | * boost on the next scan cycle. | |
6359 | */ | |
6360 | *headroom_boost = B_TRUE; | |
6361 | } | |
6362 | } | |
6363 | ||
6364 | /* | |
6365 | * Pick up the buffer data we had previously stashed away | |
6366 | * (and now potentially also compressed). | |
6367 | */ | |
b9541d6b CW |
6368 | buf_data = hdr->b_l1hdr.b_tmp_cdata; |
6369 | buf_sz = hdr->b_l2hdr.b_asize; | |
3a17a7a9 | 6370 | |
d962d5da PS |
6371 | /* |
6372 | * We need to do this regardless if buf_sz is zero or | |
6373 | * not, otherwise, when this l2hdr is evicted we'll | |
6374 | * remove a reference that was never added. | |
6375 | */ | |
6376 | (void) refcount_add_many(&dev->l2ad_alloc, buf_sz, hdr); | |
6377 | ||
3a17a7a9 SK |
6378 | /* Compression may have squashed the buffer to zero length. */ |
6379 | if (buf_sz != 0) { | |
ef56b078 | 6380 | uint64_t buf_a_sz; |
3a17a7a9 | 6381 | |
34dc7c2f BB |
6382 | wzio = zio_write_phys(pio, dev->l2ad_vdev, |
6383 | dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF, | |
6384 | NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE, | |
6385 | ZIO_FLAG_CANFAIL, B_FALSE); | |
6386 | ||
6387 | DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, | |
6388 | zio_t *, wzio); | |
6389 | (void) zio_nowait(wzio); | |
6390 | ||
ef56b078 | 6391 | stats_size += buf_sz; |
d962d5da | 6392 | |
b128c09f BB |
6393 | /* |
6394 | * Keep the clock hand suitably device-aligned. | |
6395 | */ | |
ef56b078 AG |
6396 | buf_a_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz); |
6397 | write_asize += buf_a_sz; | |
6398 | dev->l2ad_hand += buf_a_sz; | |
34dc7c2f | 6399 | } |
34dc7c2f | 6400 | } |
34dc7c2f | 6401 | |
b9541d6b | 6402 | mutex_exit(&dev->l2ad_mtx); |
34dc7c2f | 6403 | |
3a17a7a9 | 6404 | ASSERT3U(write_asize, <=, target_sz); |
34dc7c2f | 6405 | ARCSTAT_BUMP(arcstat_l2_writes_sent); |
3a17a7a9 | 6406 | ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize); |
34dc7c2f | 6407 | ARCSTAT_INCR(arcstat_l2_size, write_sz); |
ef56b078 AG |
6408 | ARCSTAT_INCR(arcstat_l2_asize, stats_size); |
6409 | vdev_space_update(dev->l2ad_vdev, stats_size, 0, 0); | |
34dc7c2f BB |
6410 | |
6411 | /* | |
6412 | * Bump device hand to the device start if it is approaching the end. | |
6413 | * l2arc_evict() will already have evicted ahead for this case. | |
6414 | */ | |
b128c09f | 6415 | if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) { |
34dc7c2f | 6416 | dev->l2ad_hand = dev->l2ad_start; |
34dc7c2f BB |
6417 | dev->l2ad_first = B_FALSE; |
6418 | } | |
6419 | ||
d164b209 | 6420 | dev->l2ad_writing = B_TRUE; |
34dc7c2f | 6421 | (void) zio_wait(pio); |
d164b209 BB |
6422 | dev->l2ad_writing = B_FALSE; |
6423 | ||
3a17a7a9 SK |
6424 | return (write_asize); |
6425 | } | |
6426 | ||
6427 | /* | |
6428 | * Compresses an L2ARC buffer. | |
b9541d6b | 6429 | * The data to be compressed must be prefilled in l1hdr.b_tmp_cdata and its |
3a17a7a9 SK |
6430 | * size in l2hdr->b_asize. This routine tries to compress the data and |
6431 | * depending on the compression result there are three possible outcomes: | |
6432 | * *) The buffer was incompressible. The original l2hdr contents were left | |
6433 | * untouched and are ready for writing to an L2 device. | |
6434 | * *) The buffer was all-zeros, so there is no need to write it to an L2 | |
6435 | * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is | |
6436 | * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY. | |
6437 | * *) Compression succeeded and b_tmp_cdata was replaced with a temporary | |
6438 | * data buffer which holds the compressed data to be written, and b_asize | |
6439 | * tells us how much data there is. b_compress is set to the appropriate | |
6440 | * compression algorithm. Once writing is done, invoke | |
6441 | * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer. | |
6442 | * | |
6443 | * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the | |
6444 | * buffer was incompressible). | |
6445 | */ | |
6446 | static boolean_t | |
b9541d6b | 6447 | l2arc_compress_buf(arc_buf_hdr_t *hdr) |
3a17a7a9 SK |
6448 | { |
6449 | void *cdata; | |
9b67f605 | 6450 | size_t csize, len, rounded; |
b9541d6b | 6451 | l2arc_buf_hdr_t *l2hdr; |
3a17a7a9 | 6452 | |
b9541d6b CW |
6453 | ASSERT(HDR_HAS_L2HDR(hdr)); |
6454 | ||
6455 | l2hdr = &hdr->b_l2hdr; | |
6456 | ||
6457 | ASSERT(HDR_HAS_L1HDR(hdr)); | |
6458 | ASSERT(HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF); | |
6459 | ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL); | |
3a17a7a9 SK |
6460 | |
6461 | len = l2hdr->b_asize; | |
6462 | cdata = zio_data_buf_alloc(len); | |
b9541d6b CW |
6463 | ASSERT3P(cdata, !=, NULL); |
6464 | csize = zio_compress_data(ZIO_COMPRESS_LZ4, hdr->b_l1hdr.b_tmp_cdata, | |
3a17a7a9 SK |
6465 | cdata, l2hdr->b_asize); |
6466 | ||
9b67f605 MA |
6467 | rounded = P2ROUNDUP(csize, (size_t)SPA_MINBLOCKSIZE); |
6468 | if (rounded > csize) { | |
6469 | bzero((char *)cdata + csize, rounded - csize); | |
6470 | csize = rounded; | |
6471 | } | |
6472 | ||
3a17a7a9 SK |
6473 | if (csize == 0) { |
6474 | /* zero block, indicate that there's nothing to write */ | |
6475 | zio_data_buf_free(cdata, len); | |
b9541d6b | 6476 | HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_EMPTY); |
3a17a7a9 | 6477 | l2hdr->b_asize = 0; |
b9541d6b | 6478 | hdr->b_l1hdr.b_tmp_cdata = NULL; |
3a17a7a9 SK |
6479 | ARCSTAT_BUMP(arcstat_l2_compress_zeros); |
6480 | return (B_TRUE); | |
6481 | } else if (csize > 0 && csize < len) { | |
6482 | /* | |
6483 | * Compression succeeded, we'll keep the cdata around for | |
6484 | * writing and release it afterwards. | |
6485 | */ | |
b9541d6b | 6486 | HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_LZ4); |
3a17a7a9 | 6487 | l2hdr->b_asize = csize; |
b9541d6b | 6488 | hdr->b_l1hdr.b_tmp_cdata = cdata; |
3a17a7a9 SK |
6489 | ARCSTAT_BUMP(arcstat_l2_compress_successes); |
6490 | return (B_TRUE); | |
6491 | } else { | |
6492 | /* | |
6493 | * Compression failed, release the compressed buffer. | |
6494 | * l2hdr will be left unmodified. | |
6495 | */ | |
6496 | zio_data_buf_free(cdata, len); | |
6497 | ARCSTAT_BUMP(arcstat_l2_compress_failures); | |
6498 | return (B_FALSE); | |
6499 | } | |
6500 | } | |
6501 | ||
6502 | /* | |
6503 | * Decompresses a zio read back from an l2arc device. On success, the | |
6504 | * underlying zio's io_data buffer is overwritten by the uncompressed | |
6505 | * version. On decompression error (corrupt compressed stream), the | |
6506 | * zio->io_error value is set to signal an I/O error. | |
6507 | * | |
6508 | * Please note that the compressed data stream is not checksummed, so | |
6509 | * if the underlying device is experiencing data corruption, we may feed | |
6510 | * corrupt data to the decompressor, so the decompressor needs to be | |
6511 | * able to handle this situation (LZ4 does). | |
6512 | */ | |
6513 | static void | |
6514 | l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c) | |
6515 | { | |
6516 | uint64_t csize; | |
6517 | void *cdata; | |
6518 | ||
6519 | ASSERT(L2ARC_IS_VALID_COMPRESS(c)); | |
6520 | ||
6521 | if (zio->io_error != 0) { | |
6522 | /* | |
6523 | * An io error has occured, just restore the original io | |
6524 | * size in preparation for a main pool read. | |
6525 | */ | |
6526 | zio->io_orig_size = zio->io_size = hdr->b_size; | |
6527 | return; | |
6528 | } | |
6529 | ||
6530 | if (c == ZIO_COMPRESS_EMPTY) { | |
6531 | /* | |
6532 | * An empty buffer results in a null zio, which means we | |
6533 | * need to fill its io_data after we're done restoring the | |
6534 | * buffer's contents. | |
6535 | */ | |
b9541d6b CW |
6536 | ASSERT(hdr->b_l1hdr.b_buf != NULL); |
6537 | bzero(hdr->b_l1hdr.b_buf->b_data, hdr->b_size); | |
6538 | zio->io_data = zio->io_orig_data = hdr->b_l1hdr.b_buf->b_data; | |
3a17a7a9 SK |
6539 | } else { |
6540 | ASSERT(zio->io_data != NULL); | |
6541 | /* | |
6542 | * We copy the compressed data from the start of the arc buffer | |
6543 | * (the zio_read will have pulled in only what we need, the | |
6544 | * rest is garbage which we will overwrite at decompression) | |
6545 | * and then decompress back to the ARC data buffer. This way we | |
6546 | * can minimize copying by simply decompressing back over the | |
6547 | * original compressed data (rather than decompressing to an | |
6548 | * aux buffer and then copying back the uncompressed buffer, | |
6549 | * which is likely to be much larger). | |
6550 | */ | |
6551 | csize = zio->io_size; | |
6552 | cdata = zio_data_buf_alloc(csize); | |
6553 | bcopy(zio->io_data, cdata, csize); | |
6554 | if (zio_decompress_data(c, cdata, zio->io_data, csize, | |
6555 | hdr->b_size) != 0) | |
2e528b49 | 6556 | zio->io_error = SET_ERROR(EIO); |
3a17a7a9 SK |
6557 | zio_data_buf_free(cdata, csize); |
6558 | } | |
6559 | ||
6560 | /* Restore the expected uncompressed IO size. */ | |
6561 | zio->io_orig_size = zio->io_size = hdr->b_size; | |
6562 | } | |
6563 | ||
6564 | /* | |
6565 | * Releases the temporary b_tmp_cdata buffer in an l2arc header structure. | |
6566 | * This buffer serves as a temporary holder of compressed data while | |
6567 | * the buffer entry is being written to an l2arc device. Once that is | |
6568 | * done, we can dispose of it. | |
6569 | */ | |
6570 | static void | |
2a432414 | 6571 | l2arc_release_cdata_buf(arc_buf_hdr_t *hdr) |
3a17a7a9 | 6572 | { |
ca0bf58d PS |
6573 | enum zio_compress comp = HDR_GET_COMPRESS(hdr); |
6574 | ||
b9541d6b | 6575 | ASSERT(HDR_HAS_L1HDR(hdr)); |
ca0bf58d PS |
6576 | ASSERT(comp == ZIO_COMPRESS_OFF || L2ARC_IS_VALID_COMPRESS(comp)); |
6577 | ||
6578 | if (comp == ZIO_COMPRESS_OFF) { | |
6579 | /* | |
6580 | * In this case, b_tmp_cdata points to the same buffer | |
6581 | * as the arc_buf_t's b_data field. We don't want to | |
6582 | * free it, since the arc_buf_t will handle that. | |
6583 | */ | |
6584 | hdr->b_l1hdr.b_tmp_cdata = NULL; | |
6585 | } else if (comp == ZIO_COMPRESS_EMPTY) { | |
6586 | /* | |
6587 | * In this case, b_tmp_cdata was compressed to an empty | |
6588 | * buffer, thus there's nothing to free and b_tmp_cdata | |
6589 | * should have been set to NULL in l2arc_write_buffers(). | |
6590 | */ | |
6591 | ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL); | |
6592 | } else { | |
3a17a7a9 SK |
6593 | /* |
6594 | * If the data was compressed, then we've allocated a | |
6595 | * temporary buffer for it, so now we need to release it. | |
6596 | */ | |
b9541d6b CW |
6597 | ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL); |
6598 | zio_data_buf_free(hdr->b_l1hdr.b_tmp_cdata, | |
6599 | hdr->b_size); | |
ca0bf58d | 6600 | hdr->b_l1hdr.b_tmp_cdata = NULL; |
3a17a7a9 | 6601 | } |
ca0bf58d | 6602 | |
34dc7c2f BB |
6603 | } |
6604 | ||
6605 | /* | |
6606 | * This thread feeds the L2ARC at regular intervals. This is the beating | |
6607 | * heart of the L2ARC. | |
6608 | */ | |
6609 | static void | |
6610 | l2arc_feed_thread(void) | |
6611 | { | |
6612 | callb_cpr_t cpr; | |
6613 | l2arc_dev_t *dev; | |
6614 | spa_t *spa; | |
d164b209 | 6615 | uint64_t size, wrote; |
428870ff | 6616 | clock_t begin, next = ddi_get_lbolt(); |
3a17a7a9 | 6617 | boolean_t headroom_boost = B_FALSE; |
40d06e3c | 6618 | fstrans_cookie_t cookie; |
34dc7c2f BB |
6619 | |
6620 | CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); | |
6621 | ||
6622 | mutex_enter(&l2arc_feed_thr_lock); | |
6623 | ||
40d06e3c | 6624 | cookie = spl_fstrans_mark(); |
34dc7c2f | 6625 | while (l2arc_thread_exit == 0) { |
34dc7c2f | 6626 | CALLB_CPR_SAFE_BEGIN(&cpr); |
b64ccd6c | 6627 | (void) cv_timedwait_sig(&l2arc_feed_thr_cv, |
5b63b3eb | 6628 | &l2arc_feed_thr_lock, next); |
34dc7c2f | 6629 | CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); |
428870ff | 6630 | next = ddi_get_lbolt() + hz; |
34dc7c2f BB |
6631 | |
6632 | /* | |
b128c09f | 6633 | * Quick check for L2ARC devices. |
34dc7c2f BB |
6634 | */ |
6635 | mutex_enter(&l2arc_dev_mtx); | |
6636 | if (l2arc_ndev == 0) { | |
6637 | mutex_exit(&l2arc_dev_mtx); | |
6638 | continue; | |
6639 | } | |
b128c09f | 6640 | mutex_exit(&l2arc_dev_mtx); |
428870ff | 6641 | begin = ddi_get_lbolt(); |
34dc7c2f BB |
6642 | |
6643 | /* | |
b128c09f BB |
6644 | * This selects the next l2arc device to write to, and in |
6645 | * doing so the next spa to feed from: dev->l2ad_spa. This | |
6646 | * will return NULL if there are now no l2arc devices or if | |
6647 | * they are all faulted. | |
6648 | * | |
6649 | * If a device is returned, its spa's config lock is also | |
6650 | * held to prevent device removal. l2arc_dev_get_next() | |
6651 | * will grab and release l2arc_dev_mtx. | |
34dc7c2f | 6652 | */ |
b128c09f | 6653 | if ((dev = l2arc_dev_get_next()) == NULL) |
34dc7c2f | 6654 | continue; |
b128c09f BB |
6655 | |
6656 | spa = dev->l2ad_spa; | |
6657 | ASSERT(spa != NULL); | |
34dc7c2f | 6658 | |
572e2857 BB |
6659 | /* |
6660 | * If the pool is read-only then force the feed thread to | |
6661 | * sleep a little longer. | |
6662 | */ | |
6663 | if (!spa_writeable(spa)) { | |
6664 | next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; | |
6665 | spa_config_exit(spa, SCL_L2ARC, dev); | |
6666 | continue; | |
6667 | } | |
6668 | ||
34dc7c2f | 6669 | /* |
b128c09f | 6670 | * Avoid contributing to memory pressure. |
34dc7c2f | 6671 | */ |
ca67b33a | 6672 | if (arc_reclaim_needed()) { |
b128c09f BB |
6673 | ARCSTAT_BUMP(arcstat_l2_abort_lowmem); |
6674 | spa_config_exit(spa, SCL_L2ARC, dev); | |
34dc7c2f BB |
6675 | continue; |
6676 | } | |
b128c09f | 6677 | |
34dc7c2f BB |
6678 | ARCSTAT_BUMP(arcstat_l2_feeds); |
6679 | ||
3a17a7a9 | 6680 | size = l2arc_write_size(); |
b128c09f | 6681 | |
34dc7c2f BB |
6682 | /* |
6683 | * Evict L2ARC buffers that will be overwritten. | |
6684 | */ | |
b128c09f | 6685 | l2arc_evict(dev, size, B_FALSE); |
34dc7c2f BB |
6686 | |
6687 | /* | |
6688 | * Write ARC buffers. | |
6689 | */ | |
3a17a7a9 | 6690 | wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost); |
d164b209 BB |
6691 | |
6692 | /* | |
6693 | * Calculate interval between writes. | |
6694 | */ | |
6695 | next = l2arc_write_interval(begin, size, wrote); | |
b128c09f | 6696 | spa_config_exit(spa, SCL_L2ARC, dev); |
34dc7c2f | 6697 | } |
40d06e3c | 6698 | spl_fstrans_unmark(cookie); |
34dc7c2f BB |
6699 | |
6700 | l2arc_thread_exit = 0; | |
6701 | cv_broadcast(&l2arc_feed_thr_cv); | |
6702 | CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ | |
6703 | thread_exit(); | |
6704 | } | |
6705 | ||
b128c09f BB |
6706 | boolean_t |
6707 | l2arc_vdev_present(vdev_t *vd) | |
6708 | { | |
6709 | l2arc_dev_t *dev; | |
6710 | ||
6711 | mutex_enter(&l2arc_dev_mtx); | |
6712 | for (dev = list_head(l2arc_dev_list); dev != NULL; | |
6713 | dev = list_next(l2arc_dev_list, dev)) { | |
6714 | if (dev->l2ad_vdev == vd) | |
6715 | break; | |
6716 | } | |
6717 | mutex_exit(&l2arc_dev_mtx); | |
6718 | ||
6719 | return (dev != NULL); | |
6720 | } | |
6721 | ||
34dc7c2f BB |
6722 | /* |
6723 | * Add a vdev for use by the L2ARC. By this point the spa has already | |
6724 | * validated the vdev and opened it. | |
6725 | */ | |
6726 | void | |
9babb374 | 6727 | l2arc_add_vdev(spa_t *spa, vdev_t *vd) |
34dc7c2f BB |
6728 | { |
6729 | l2arc_dev_t *adddev; | |
6730 | ||
b128c09f BB |
6731 | ASSERT(!l2arc_vdev_present(vd)); |
6732 | ||
34dc7c2f BB |
6733 | /* |
6734 | * Create a new l2arc device entry. | |
6735 | */ | |
6736 | adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); | |
6737 | adddev->l2ad_spa = spa; | |
6738 | adddev->l2ad_vdev = vd; | |
9babb374 BB |
6739 | adddev->l2ad_start = VDEV_LABEL_START_SIZE; |
6740 | adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); | |
34dc7c2f | 6741 | adddev->l2ad_hand = adddev->l2ad_start; |
34dc7c2f | 6742 | adddev->l2ad_first = B_TRUE; |
d164b209 | 6743 | adddev->l2ad_writing = B_FALSE; |
98f72a53 | 6744 | list_link_init(&adddev->l2ad_node); |
34dc7c2f | 6745 | |
b9541d6b | 6746 | mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
6747 | /* |
6748 | * This is a list of all ARC buffers that are still valid on the | |
6749 | * device. | |
6750 | */ | |
b9541d6b CW |
6751 | list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), |
6752 | offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); | |
34dc7c2f | 6753 | |
428870ff | 6754 | vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); |
d962d5da | 6755 | refcount_create(&adddev->l2ad_alloc); |
34dc7c2f BB |
6756 | |
6757 | /* | |
6758 | * Add device to global list | |
6759 | */ | |
6760 | mutex_enter(&l2arc_dev_mtx); | |
6761 | list_insert_head(l2arc_dev_list, adddev); | |
6762 | atomic_inc_64(&l2arc_ndev); | |
6763 | mutex_exit(&l2arc_dev_mtx); | |
6764 | } | |
6765 | ||
6766 | /* | |
6767 | * Remove a vdev from the L2ARC. | |
6768 | */ | |
6769 | void | |
6770 | l2arc_remove_vdev(vdev_t *vd) | |
6771 | { | |
6772 | l2arc_dev_t *dev, *nextdev, *remdev = NULL; | |
6773 | ||
34dc7c2f BB |
6774 | /* |
6775 | * Find the device by vdev | |
6776 | */ | |
6777 | mutex_enter(&l2arc_dev_mtx); | |
6778 | for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) { | |
6779 | nextdev = list_next(l2arc_dev_list, dev); | |
6780 | if (vd == dev->l2ad_vdev) { | |
6781 | remdev = dev; | |
6782 | break; | |
6783 | } | |
6784 | } | |
6785 | ASSERT(remdev != NULL); | |
6786 | ||
6787 | /* | |
6788 | * Remove device from global list | |
6789 | */ | |
6790 | list_remove(l2arc_dev_list, remdev); | |
6791 | l2arc_dev_last = NULL; /* may have been invalidated */ | |
b128c09f BB |
6792 | atomic_dec_64(&l2arc_ndev); |
6793 | mutex_exit(&l2arc_dev_mtx); | |
34dc7c2f BB |
6794 | |
6795 | /* | |
6796 | * Clear all buflists and ARC references. L2ARC device flush. | |
6797 | */ | |
6798 | l2arc_evict(remdev, 0, B_TRUE); | |
b9541d6b CW |
6799 | list_destroy(&remdev->l2ad_buflist); |
6800 | mutex_destroy(&remdev->l2ad_mtx); | |
d962d5da | 6801 | refcount_destroy(&remdev->l2ad_alloc); |
34dc7c2f | 6802 | kmem_free(remdev, sizeof (l2arc_dev_t)); |
34dc7c2f BB |
6803 | } |
6804 | ||
6805 | void | |
b128c09f | 6806 | l2arc_init(void) |
34dc7c2f BB |
6807 | { |
6808 | l2arc_thread_exit = 0; | |
6809 | l2arc_ndev = 0; | |
6810 | l2arc_writes_sent = 0; | |
6811 | l2arc_writes_done = 0; | |
6812 | ||
6813 | mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); | |
6814 | cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); | |
6815 | mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); | |
34dc7c2f BB |
6816 | mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); |
6817 | ||
6818 | l2arc_dev_list = &L2ARC_dev_list; | |
6819 | l2arc_free_on_write = &L2ARC_free_on_write; | |
6820 | list_create(l2arc_dev_list, sizeof (l2arc_dev_t), | |
6821 | offsetof(l2arc_dev_t, l2ad_node)); | |
6822 | list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), | |
6823 | offsetof(l2arc_data_free_t, l2df_list_node)); | |
34dc7c2f BB |
6824 | } |
6825 | ||
6826 | void | |
b128c09f | 6827 | l2arc_fini(void) |
34dc7c2f | 6828 | { |
b128c09f BB |
6829 | /* |
6830 | * This is called from dmu_fini(), which is called from spa_fini(); | |
6831 | * Because of this, we can assume that all l2arc devices have | |
6832 | * already been removed when the pools themselves were removed. | |
6833 | */ | |
6834 | ||
6835 | l2arc_do_free_on_write(); | |
34dc7c2f BB |
6836 | |
6837 | mutex_destroy(&l2arc_feed_thr_lock); | |
6838 | cv_destroy(&l2arc_feed_thr_cv); | |
6839 | mutex_destroy(&l2arc_dev_mtx); | |
34dc7c2f BB |
6840 | mutex_destroy(&l2arc_free_on_write_mtx); |
6841 | ||
6842 | list_destroy(l2arc_dev_list); | |
6843 | list_destroy(l2arc_free_on_write); | |
6844 | } | |
b128c09f BB |
6845 | |
6846 | void | |
6847 | l2arc_start(void) | |
6848 | { | |
fb5f0bc8 | 6849 | if (!(spa_mode_global & FWRITE)) |
b128c09f BB |
6850 | return; |
6851 | ||
6852 | (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, | |
6853 | TS_RUN, minclsyspri); | |
6854 | } | |
6855 | ||
6856 | void | |
6857 | l2arc_stop(void) | |
6858 | { | |
fb5f0bc8 | 6859 | if (!(spa_mode_global & FWRITE)) |
b128c09f BB |
6860 | return; |
6861 | ||
6862 | mutex_enter(&l2arc_feed_thr_lock); | |
6863 | cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ | |
6864 | l2arc_thread_exit = 1; | |
6865 | while (l2arc_thread_exit != 0) | |
6866 | cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); | |
6867 | mutex_exit(&l2arc_feed_thr_lock); | |
6868 | } | |
c28b2279 BB |
6869 | |
6870 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
0f699108 AZ |
6871 | EXPORT_SYMBOL(arc_buf_size); |
6872 | EXPORT_SYMBOL(arc_write); | |
c28b2279 BB |
6873 | EXPORT_SYMBOL(arc_read); |
6874 | EXPORT_SYMBOL(arc_buf_remove_ref); | |
e0b0ca98 | 6875 | EXPORT_SYMBOL(arc_buf_info); |
c28b2279 | 6876 | EXPORT_SYMBOL(arc_getbuf_func); |
ab26409d BB |
6877 | EXPORT_SYMBOL(arc_add_prune_callback); |
6878 | EXPORT_SYMBOL(arc_remove_prune_callback); | |
c28b2279 | 6879 | |
bce45ec9 | 6880 | module_param(zfs_arc_min, ulong, 0644); |
c409e464 | 6881 | MODULE_PARM_DESC(zfs_arc_min, "Min arc size"); |
c28b2279 | 6882 | |
bce45ec9 | 6883 | module_param(zfs_arc_max, ulong, 0644); |
c409e464 | 6884 | MODULE_PARM_DESC(zfs_arc_max, "Max arc size"); |
c28b2279 | 6885 | |
bce45ec9 | 6886 | module_param(zfs_arc_meta_limit, ulong, 0644); |
c28b2279 | 6887 | MODULE_PARM_DESC(zfs_arc_meta_limit, "Meta limit for arc size"); |
6a8f9b6b | 6888 | |
ca0bf58d PS |
6889 | module_param(zfs_arc_meta_min, ulong, 0644); |
6890 | MODULE_PARM_DESC(zfs_arc_meta_min, "Min arc metadata"); | |
6891 | ||
bce45ec9 | 6892 | module_param(zfs_arc_meta_prune, int, 0644); |
2cbb06b5 | 6893 | MODULE_PARM_DESC(zfs_arc_meta_prune, "Meta objects to scan for prune"); |
c409e464 | 6894 | |
ca67b33a | 6895 | module_param(zfs_arc_meta_adjust_restarts, int, 0644); |
bc888666 BB |
6896 | MODULE_PARM_DESC(zfs_arc_meta_adjust_restarts, |
6897 | "Limit number of restarts in arc_adjust_meta"); | |
6898 | ||
f6046738 BB |
6899 | module_param(zfs_arc_meta_strategy, int, 0644); |
6900 | MODULE_PARM_DESC(zfs_arc_meta_strategy, "Meta reclaim strategy"); | |
6901 | ||
bce45ec9 | 6902 | module_param(zfs_arc_grow_retry, int, 0644); |
c409e464 BB |
6903 | MODULE_PARM_DESC(zfs_arc_grow_retry, "Seconds before growing arc size"); |
6904 | ||
89c8cac4 PS |
6905 | module_param(zfs_arc_p_aggressive_disable, int, 0644); |
6906 | MODULE_PARM_DESC(zfs_arc_p_aggressive_disable, "disable aggressive arc_p grow"); | |
6907 | ||
62422785 PS |
6908 | module_param(zfs_arc_p_dampener_disable, int, 0644); |
6909 | MODULE_PARM_DESC(zfs_arc_p_dampener_disable, "disable arc_p adapt dampener"); | |
6910 | ||
bce45ec9 | 6911 | module_param(zfs_arc_shrink_shift, int, 0644); |
c409e464 BB |
6912 | MODULE_PARM_DESC(zfs_arc_shrink_shift, "log2(fraction of arc to reclaim)"); |
6913 | ||
1f7c30df BB |
6914 | module_param(zfs_disable_dup_eviction, int, 0644); |
6915 | MODULE_PARM_DESC(zfs_disable_dup_eviction, "disable duplicate buffer eviction"); | |
6916 | ||
49ddb315 MA |
6917 | module_param(zfs_arc_average_blocksize, int, 0444); |
6918 | MODULE_PARM_DESC(zfs_arc_average_blocksize, "Target average block size"); | |
6919 | ||
0c5493d4 BB |
6920 | module_param(zfs_arc_memory_throttle_disable, int, 0644); |
6921 | MODULE_PARM_DESC(zfs_arc_memory_throttle_disable, "disable memory throttle"); | |
6922 | ||
bce45ec9 BB |
6923 | module_param(zfs_arc_min_prefetch_lifespan, int, 0644); |
6924 | MODULE_PARM_DESC(zfs_arc_min_prefetch_lifespan, "Min life of prefetch block"); | |
6925 | ||
ca0bf58d PS |
6926 | module_param(zfs_arc_num_sublists_per_state, int, 0644); |
6927 | MODULE_PARM_DESC(zfs_arc_num_sublists_per_state, | |
6928 | "Number of sublists used in each of the ARC state lists"); | |
6929 | ||
bce45ec9 | 6930 | module_param(l2arc_write_max, ulong, 0644); |
abd8610c BB |
6931 | MODULE_PARM_DESC(l2arc_write_max, "Max write bytes per interval"); |
6932 | ||
bce45ec9 | 6933 | module_param(l2arc_write_boost, ulong, 0644); |
abd8610c BB |
6934 | MODULE_PARM_DESC(l2arc_write_boost, "Extra write bytes during device warmup"); |
6935 | ||
bce45ec9 | 6936 | module_param(l2arc_headroom, ulong, 0644); |
abd8610c BB |
6937 | MODULE_PARM_DESC(l2arc_headroom, "Number of max device writes to precache"); |
6938 | ||
3a17a7a9 SK |
6939 | module_param(l2arc_headroom_boost, ulong, 0644); |
6940 | MODULE_PARM_DESC(l2arc_headroom_boost, "Compressed l2arc_headroom multiplier"); | |
6941 | ||
bce45ec9 | 6942 | module_param(l2arc_feed_secs, ulong, 0644); |
abd8610c BB |
6943 | MODULE_PARM_DESC(l2arc_feed_secs, "Seconds between L2ARC writing"); |
6944 | ||
bce45ec9 | 6945 | module_param(l2arc_feed_min_ms, ulong, 0644); |
abd8610c BB |
6946 | MODULE_PARM_DESC(l2arc_feed_min_ms, "Min feed interval in milliseconds"); |
6947 | ||
bce45ec9 | 6948 | module_param(l2arc_noprefetch, int, 0644); |
abd8610c BB |
6949 | MODULE_PARM_DESC(l2arc_noprefetch, "Skip caching prefetched buffers"); |
6950 | ||
3a17a7a9 SK |
6951 | module_param(l2arc_nocompress, int, 0644); |
6952 | MODULE_PARM_DESC(l2arc_nocompress, "Skip compressing L2ARC buffers"); | |
6953 | ||
bce45ec9 | 6954 | module_param(l2arc_feed_again, int, 0644); |
abd8610c BB |
6955 | MODULE_PARM_DESC(l2arc_feed_again, "Turbo L2ARC warmup"); |
6956 | ||
bce45ec9 | 6957 | module_param(l2arc_norw, int, 0644); |
abd8610c BB |
6958 | MODULE_PARM_DESC(l2arc_norw, "No reads during writes"); |
6959 | ||
c28b2279 | 6960 | #endif |