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