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