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