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