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