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