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