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