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