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