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