Print this page
zpool import speedup
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/spa_misc.c
+++ new/usr/src/uts/common/fs/zfs/spa_misc.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 25 */
26 26
27 27 #include <sys/zfs_context.h>
28 28 #include <sys/spa_impl.h>
29 29 #include <sys/spa_boot.h>
30 30 #include <sys/zio.h>
31 31 #include <sys/zio_checksum.h>
32 32 #include <sys/zio_compress.h>
33 33 #include <sys/dmu.h>
34 34 #include <sys/dmu_tx.h>
35 35 #include <sys/zap.h>
36 36 #include <sys/zil.h>
37 37 #include <sys/vdev_impl.h>
38 38 #include <sys/metaslab.h>
39 39 #include <sys/uberblock_impl.h>
40 40 #include <sys/txg.h>
41 41 #include <sys/avl.h>
42 42 #include <sys/unique.h>
43 43 #include <sys/dsl_pool.h>
44 44 #include <sys/dsl_dir.h>
45 45 #include <sys/dsl_prop.h>
46 46 #include <sys/dsl_scan.h>
47 47 #include <sys/fs/zfs.h>
48 48 #include <sys/metaslab_impl.h>
49 49 #include <sys/arc.h>
50 50 #include <sys/ddt.h>
51 51 #include "zfs_prop.h"
52 52 #include "zfeature_common.h"
53 53
54 54 /*
55 55 * SPA locking
56 56 *
57 57 * There are four basic locks for managing spa_t structures:
58 58 *
59 59 * spa_namespace_lock (global mutex)
60 60 *
61 61 * This lock must be acquired to do any of the following:
62 62 *
63 63 * - Lookup a spa_t by name
64 64 * - Add or remove a spa_t from the namespace
65 65 * - Increase spa_refcount from non-zero
66 66 * - Check if spa_refcount is zero
67 67 * - Rename a spa_t
68 68 * - add/remove/attach/detach devices
69 69 * - Held for the duration of create/destroy/import/export
70 70 *
71 71 * It does not need to handle recursion. A create or destroy may
72 72 * reference objects (files or zvols) in other pools, but by
73 73 * definition they must have an existing reference, and will never need
74 74 * to lookup a spa_t by name.
75 75 *
76 76 * spa_refcount (per-spa refcount_t protected by mutex)
77 77 *
78 78 * This reference count keep track of any active users of the spa_t. The
79 79 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
80 80 * the refcount is never really 'zero' - opening a pool implicitly keeps
81 81 * some references in the DMU. Internally we check against spa_minref, but
82 82 * present the image of a zero/non-zero value to consumers.
83 83 *
84 84 * spa_config_lock[] (per-spa array of rwlocks)
85 85 *
86 86 * This protects the spa_t from config changes, and must be held in
87 87 * the following circumstances:
88 88 *
89 89 * - RW_READER to perform I/O to the spa
90 90 * - RW_WRITER to change the vdev config
91 91 *
92 92 * The locking order is fairly straightforward:
93 93 *
94 94 * spa_namespace_lock -> spa_refcount
95 95 *
96 96 * The namespace lock must be acquired to increase the refcount from 0
97 97 * or to check if it is zero.
98 98 *
99 99 * spa_refcount -> spa_config_lock[]
100 100 *
101 101 * There must be at least one valid reference on the spa_t to acquire
102 102 * the config lock.
103 103 *
104 104 * spa_namespace_lock -> spa_config_lock[]
105 105 *
106 106 * The namespace lock must always be taken before the config lock.
107 107 *
108 108 *
109 109 * The spa_namespace_lock can be acquired directly and is globally visible.
110 110 *
111 111 * The namespace is manipulated using the following functions, all of which
112 112 * require the spa_namespace_lock to be held.
113 113 *
114 114 * spa_lookup() Lookup a spa_t by name.
115 115 *
116 116 * spa_add() Create a new spa_t in the namespace.
117 117 *
118 118 * spa_remove() Remove a spa_t from the namespace. This also
119 119 * frees up any memory associated with the spa_t.
120 120 *
121 121 * spa_next() Returns the next spa_t in the system, or the
122 122 * first if NULL is passed.
123 123 *
124 124 * spa_evict_all() Shutdown and remove all spa_t structures in
125 125 * the system.
126 126 *
127 127 * spa_guid_exists() Determine whether a pool/device guid exists.
128 128 *
129 129 * The spa_refcount is manipulated using the following functions:
130 130 *
131 131 * spa_open_ref() Adds a reference to the given spa_t. Must be
132 132 * called with spa_namespace_lock held if the
133 133 * refcount is currently zero.
134 134 *
135 135 * spa_close() Remove a reference from the spa_t. This will
136 136 * not free the spa_t or remove it from the
137 137 * namespace. No locking is required.
138 138 *
139 139 * spa_refcount_zero() Returns true if the refcount is currently
140 140 * zero. Must be called with spa_namespace_lock
141 141 * held.
142 142 *
143 143 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
144 144 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
145 145 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
146 146 *
147 147 * To read the configuration, it suffices to hold one of these locks as reader.
148 148 * To modify the configuration, you must hold all locks as writer. To modify
149 149 * vdev state without altering the vdev tree's topology (e.g. online/offline),
150 150 * you must hold SCL_STATE and SCL_ZIO as writer.
151 151 *
152 152 * We use these distinct config locks to avoid recursive lock entry.
153 153 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
154 154 * block allocations (SCL_ALLOC), which may require reading space maps
155 155 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
156 156 *
157 157 * The spa config locks cannot be normal rwlocks because we need the
158 158 * ability to hand off ownership. For example, SCL_ZIO is acquired
159 159 * by the issuing thread and later released by an interrupt thread.
160 160 * They do, however, obey the usual write-wanted semantics to prevent
161 161 * writer (i.e. system administrator) starvation.
162 162 *
163 163 * The lock acquisition rules are as follows:
164 164 *
165 165 * SCL_CONFIG
166 166 * Protects changes to the vdev tree topology, such as vdev
167 167 * add/remove/attach/detach. Protects the dirty config list
168 168 * (spa_config_dirty_list) and the set of spares and l2arc devices.
169 169 *
170 170 * SCL_STATE
171 171 * Protects changes to pool state and vdev state, such as vdev
172 172 * online/offline/fault/degrade/clear. Protects the dirty state list
173 173 * (spa_state_dirty_list) and global pool state (spa_state).
174 174 *
175 175 * SCL_ALLOC
176 176 * Protects changes to metaslab groups and classes.
177 177 * Held as reader by metaslab_alloc() and metaslab_claim().
178 178 *
179 179 * SCL_ZIO
180 180 * Held by bp-level zios (those which have no io_vd upon entry)
181 181 * to prevent changes to the vdev tree. The bp-level zio implicitly
182 182 * protects all of its vdev child zios, which do not hold SCL_ZIO.
183 183 *
184 184 * SCL_FREE
185 185 * Protects changes to metaslab groups and classes.
186 186 * Held as reader by metaslab_free(). SCL_FREE is distinct from
187 187 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
188 188 * blocks in zio_done() while another i/o that holds either
189 189 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
190 190 *
191 191 * SCL_VDEV
192 192 * Held as reader to prevent changes to the vdev tree during trivial
193 193 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
194 194 * other locks, and lower than all of them, to ensure that it's safe
195 195 * to acquire regardless of caller context.
196 196 *
197 197 * In addition, the following rules apply:
198 198 *
199 199 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
200 200 * The lock ordering is SCL_CONFIG > spa_props_lock.
201 201 *
202 202 * (b) I/O operations on leaf vdevs. For any zio operation that takes
203 203 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
204 204 * or zio_write_phys() -- the caller must ensure that the config cannot
205 205 * cannot change in the interim, and that the vdev cannot be reopened.
206 206 * SCL_STATE as reader suffices for both.
207 207 *
208 208 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
209 209 *
210 210 * spa_vdev_enter() Acquire the namespace lock and the config lock
211 211 * for writing.
212 212 *
213 213 * spa_vdev_exit() Release the config lock, wait for all I/O
214 214 * to complete, sync the updated configs to the
215 215 * cache, and release the namespace lock.
216 216 *
217 217 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
218 218 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
219 219 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
220 220 *
221 221 * spa_rename() is also implemented within this file since it requires
222 222 * manipulation of the namespace.
223 223 */
224 224
225 225 static avl_tree_t spa_namespace_avl;
226 226 kmutex_t spa_namespace_lock;
227 227 static kcondvar_t spa_namespace_cv;
228 228 static int spa_active_count;
229 229 int spa_max_replication_override = SPA_DVAS_PER_BP;
230 230
231 231 static kmutex_t spa_spare_lock;
232 232 static avl_tree_t spa_spare_avl;
233 233 static kmutex_t spa_l2cache_lock;
234 234 static avl_tree_t spa_l2cache_avl;
235 235
236 236 kmem_cache_t *spa_buffer_pool;
237 237 int spa_mode_global;
238 238
239 239 #ifdef ZFS_DEBUG
240 240 /* Everything except dprintf and spa is on by default in debug builds */
241 241 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
242 242 #else
243 243 int zfs_flags = 0;
244 244 #endif
245 245
246 246 /*
247 247 * zfs_recover can be set to nonzero to attempt to recover from
248 248 * otherwise-fatal errors, typically caused by on-disk corruption. When
249 249 * set, calls to zfs_panic_recover() will turn into warning messages.
250 250 * This should only be used as a last resort, as it typically results
251 251 * in leaked space, or worse.
252 252 */
253 253 boolean_t zfs_recover = B_FALSE;
254 254
255 255 /*
256 256 * If destroy encounters an EIO while reading metadata (e.g. indirect
257 257 * blocks), space referenced by the missing metadata can not be freed.
258 258 * Normally this causes the background destroy to become "stalled", as
259 259 * it is unable to make forward progress. While in this stalled state,
260 260 * all remaining space to free from the error-encountering filesystem is
261 261 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
262 262 * permanently leak the space from indirect blocks that can not be read,
263 263 * and continue to free everything else that it can.
264 264 *
265 265 * The default, "stalling" behavior is useful if the storage partially
266 266 * fails (i.e. some but not all i/os fail), and then later recovers. In
267 267 * this case, we will be able to continue pool operations while it is
268 268 * partially failed, and when it recovers, we can continue to free the
269 269 * space, with no leaks. However, note that this case is actually
270 270 * fairly rare.
271 271 *
272 272 * Typically pools either (a) fail completely (but perhaps temporarily,
273 273 * e.g. a top-level vdev going offline), or (b) have localized,
274 274 * permanent errors (e.g. disk returns the wrong data due to bit flip or
275 275 * firmware bug). In case (a), this setting does not matter because the
276 276 * pool will be suspended and the sync thread will not be able to make
277 277 * forward progress regardless. In case (b), because the error is
278 278 * permanent, the best we can do is leak the minimum amount of space,
279 279 * which is what setting this flag will do. Therefore, it is reasonable
280 280 * for this flag to normally be set, but we chose the more conservative
281 281 * approach of not setting it, so that there is no possibility of
282 282 * leaking space in the "partial temporary" failure case.
283 283 */
284 284 boolean_t zfs_free_leak_on_eio = B_FALSE;
285 285
286 286 /*
287 287 * Expiration time in milliseconds. This value has two meanings. First it is
288 288 * used to determine when the spa_deadman() logic should fire. By default the
289 289 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
290 290 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
291 291 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
292 292 * in a system panic.
293 293 */
294 294 uint64_t zfs_deadman_synctime_ms = 1000000ULL;
295 295
296 296 /*
297 297 * Check time in milliseconds. This defines the frequency at which we check
298 298 * for hung I/O.
299 299 */
300 300 uint64_t zfs_deadman_checktime_ms = 5000ULL;
301 301
302 302 /*
303 303 * Override the zfs deadman behavior via /etc/system. By default the
304 304 * deadman is enabled except on VMware and sparc deployments.
305 305 */
306 306 int zfs_deadman_enabled = -1;
307 307
308 308 /*
309 309 * The worst case is single-sector max-parity RAID-Z blocks, in which
310 310 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
311 311 * times the size; so just assume that. Add to this the fact that
312 312 * we can have up to 3 DVAs per bp, and one more factor of 2 because
313 313 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
314 314 * the worst case is:
315 315 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
316 316 */
317 317 int spa_asize_inflation = 24;
318 318
319 319 /*
320 320 * ==========================================================================
321 321 * SPA config locking
322 322 * ==========================================================================
323 323 */
324 324 static void
325 325 spa_config_lock_init(spa_t *spa)
326 326 {
327 327 for (int i = 0; i < SCL_LOCKS; i++) {
328 328 spa_config_lock_t *scl = &spa->spa_config_lock[i];
329 329 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
330 330 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
331 331 refcount_create_untracked(&scl->scl_count);
332 332 scl->scl_writer = NULL;
333 333 scl->scl_write_wanted = 0;
334 334 }
335 335 }
336 336
337 337 static void
338 338 spa_config_lock_destroy(spa_t *spa)
339 339 {
340 340 for (int i = 0; i < SCL_LOCKS; i++) {
341 341 spa_config_lock_t *scl = &spa->spa_config_lock[i];
342 342 mutex_destroy(&scl->scl_lock);
343 343 cv_destroy(&scl->scl_cv);
344 344 refcount_destroy(&scl->scl_count);
345 345 ASSERT(scl->scl_writer == NULL);
346 346 ASSERT(scl->scl_write_wanted == 0);
347 347 }
348 348 }
349 349
350 350 int
351 351 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
352 352 {
353 353 for (int i = 0; i < SCL_LOCKS; i++) {
354 354 spa_config_lock_t *scl = &spa->spa_config_lock[i];
355 355 if (!(locks & (1 << i)))
356 356 continue;
357 357 mutex_enter(&scl->scl_lock);
358 358 if (rw == RW_READER) {
359 359 if (scl->scl_writer || scl->scl_write_wanted) {
360 360 mutex_exit(&scl->scl_lock);
361 361 spa_config_exit(spa, locks ^ (1 << i), tag);
362 362 return (0);
363 363 }
364 364 } else {
365 365 ASSERT(scl->scl_writer != curthread);
366 366 if (!refcount_is_zero(&scl->scl_count)) {
367 367 mutex_exit(&scl->scl_lock);
368 368 spa_config_exit(spa, locks ^ (1 << i), tag);
369 369 return (0);
370 370 }
371 371 scl->scl_writer = curthread;
372 372 }
373 373 (void) refcount_add(&scl->scl_count, tag);
374 374 mutex_exit(&scl->scl_lock);
375 375 }
376 376 return (1);
377 377 }
378 378
379 379 void
380 380 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
381 381 {
382 382 int wlocks_held = 0;
383 383
384 384 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
385 385
386 386 for (int i = 0; i < SCL_LOCKS; i++) {
387 387 spa_config_lock_t *scl = &spa->spa_config_lock[i];
388 388 if (scl->scl_writer == curthread)
389 389 wlocks_held |= (1 << i);
390 390 if (!(locks & (1 << i)))
391 391 continue;
392 392 mutex_enter(&scl->scl_lock);
393 393 if (rw == RW_READER) {
394 394 while (scl->scl_writer || scl->scl_write_wanted) {
395 395 cv_wait(&scl->scl_cv, &scl->scl_lock);
396 396 }
397 397 } else {
398 398 ASSERT(scl->scl_writer != curthread);
399 399 while (!refcount_is_zero(&scl->scl_count)) {
400 400 scl->scl_write_wanted++;
401 401 cv_wait(&scl->scl_cv, &scl->scl_lock);
402 402 scl->scl_write_wanted--;
403 403 }
404 404 scl->scl_writer = curthread;
405 405 }
406 406 (void) refcount_add(&scl->scl_count, tag);
407 407 mutex_exit(&scl->scl_lock);
408 408 }
409 409 ASSERT(wlocks_held <= locks);
410 410 }
411 411
412 412 void
413 413 spa_config_exit(spa_t *spa, int locks, void *tag)
414 414 {
415 415 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
416 416 spa_config_lock_t *scl = &spa->spa_config_lock[i];
417 417 if (!(locks & (1 << i)))
418 418 continue;
419 419 mutex_enter(&scl->scl_lock);
420 420 ASSERT(!refcount_is_zero(&scl->scl_count));
421 421 if (refcount_remove(&scl->scl_count, tag) == 0) {
422 422 ASSERT(scl->scl_writer == NULL ||
423 423 scl->scl_writer == curthread);
424 424 scl->scl_writer = NULL; /* OK in either case */
425 425 cv_broadcast(&scl->scl_cv);
426 426 }
427 427 mutex_exit(&scl->scl_lock);
428 428 }
429 429 }
430 430
431 431 int
432 432 spa_config_held(spa_t *spa, int locks, krw_t rw)
433 433 {
434 434 int locks_held = 0;
435 435
436 436 for (int i = 0; i < SCL_LOCKS; i++) {
437 437 spa_config_lock_t *scl = &spa->spa_config_lock[i];
438 438 if (!(locks & (1 << i)))
439 439 continue;
440 440 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
441 441 (rw == RW_WRITER && scl->scl_writer == curthread))
442 442 locks_held |= 1 << i;
443 443 }
444 444
445 445 return (locks_held);
446 446 }
447 447
448 448 /*
449 449 * ==========================================================================
450 450 * SPA namespace functions
↓ open down ↓ |
450 lines elided |
↑ open up ↑ |
451 451 * ==========================================================================
452 452 */
453 453
454 454 /*
455 455 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
456 456 * Returns NULL if no matching spa_t is found.
457 457 */
458 458 spa_t *
459 459 spa_lookup(const char *name)
460 460 {
461 - static spa_t search; /* spa_t is large; don't allocate on stack */
461 + spa_t *search;
462 462 spa_t *spa;
463 463 avl_index_t where;
464 464 char *cp;
465 465
466 - ASSERT(MUTEX_HELD(&spa_namespace_lock));
466 + search = kmem_alloc(sizeof(*search), KM_SLEEP);
467 467
468 - (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
468 + (void) strlcpy(search->spa_name, name, sizeof (search->spa_name));
469 469
470 470 /*
471 471 * If it's a full dataset name, figure out the pool name and
472 472 * just use that.
473 473 */
474 - cp = strpbrk(search.spa_name, "/@#");
474 + cp = strpbrk(search->spa_name, "/@#");
475 475 if (cp != NULL)
476 476 *cp = '\0';
477 477
478 - spa = avl_find(&spa_namespace_avl, &search, &where);
478 + spa = avl_find(&spa_namespace_avl, search, &where);
479 + kmem_free(search, sizeof(*search));
479 480
480 481 return (spa);
481 482 }
482 483
483 484 /*
484 485 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
485 486 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
486 487 * looking for potentially hung I/Os.
487 488 */
488 489 void
489 490 spa_deadman(void *arg)
490 491 {
491 492 spa_t *spa = arg;
492 493
493 494 /*
494 495 * Disable the deadman timer if the pool is suspended.
495 496 */
496 497 if (spa_suspended(spa)) {
497 498 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
498 499 return;
499 500 }
500 501
501 502 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
502 503 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
503 504 ++spa->spa_deadman_calls);
504 505 if (zfs_deadman_enabled)
505 506 vdev_deadman(spa->spa_root_vdev);
506 507 }
507 508
508 509 /*
509 510 * Create an uninitialized spa_t with the given name. Requires
510 511 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
511 512 * exist by calling spa_lookup() first.
512 513 */
513 514 spa_t *
514 515 spa_add(const char *name, nvlist_t *config, const char *altroot)
515 516 {
516 517 spa_t *spa;
517 518 spa_config_dirent_t *dp;
518 519 cyc_handler_t hdlr;
519 520 cyc_time_t when;
520 521
521 522 ASSERT(MUTEX_HELD(&spa_namespace_lock));
522 523
523 524 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
524 525
525 526 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
526 527 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
527 528 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
528 529 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
529 530 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
530 531 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
531 532 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
532 533 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
533 534 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
534 535 mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
535 536
536 537 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
537 538 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
538 539 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
539 540 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
540 541
541 542 for (int t = 0; t < TXG_SIZE; t++)
542 543 bplist_create(&spa->spa_free_bplist[t]);
543 544
544 545 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
545 546 spa->spa_state = POOL_STATE_UNINITIALIZED;
546 547 spa->spa_freeze_txg = UINT64_MAX;
547 548 spa->spa_final_txg = UINT64_MAX;
548 549 spa->spa_load_max_txg = UINT64_MAX;
549 550 spa->spa_proc = &p0;
550 551 spa->spa_proc_state = SPA_PROC_NONE;
551 552
552 553 hdlr.cyh_func = spa_deadman;
553 554 hdlr.cyh_arg = spa;
554 555 hdlr.cyh_level = CY_LOW_LEVEL;
555 556
556 557 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
557 558
558 559 /*
559 560 * This determines how often we need to check for hung I/Os after
560 561 * the cyclic has already fired. Since checking for hung I/Os is
561 562 * an expensive operation we don't want to check too frequently.
562 563 * Instead wait for 5 seconds before checking again.
563 564 */
564 565 when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
565 566 when.cyt_when = CY_INFINITY;
566 567 mutex_enter(&cpu_lock);
567 568 spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
568 569 mutex_exit(&cpu_lock);
569 570
570 571 refcount_create(&spa->spa_refcount);
571 572 spa_config_lock_init(spa);
572 573
573 574 avl_add(&spa_namespace_avl, spa);
574 575
575 576 /*
576 577 * Set the alternate root, if there is one.
577 578 */
578 579 if (altroot) {
579 580 spa->spa_root = spa_strdup(altroot);
580 581 spa_active_count++;
581 582 }
582 583
583 584 /*
584 585 * Every pool starts with the default cachefile
585 586 */
586 587 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
587 588 offsetof(spa_config_dirent_t, scd_link));
588 589
589 590 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
590 591 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
591 592 list_insert_head(&spa->spa_config_list, dp);
592 593
593 594 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
594 595 KM_SLEEP) == 0);
595 596
596 597 if (config != NULL) {
597 598 nvlist_t *features;
598 599
599 600 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
600 601 &features) == 0) {
601 602 VERIFY(nvlist_dup(features, &spa->spa_label_features,
602 603 0) == 0);
603 604 }
604 605
605 606 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
606 607 }
607 608
608 609 if (spa->spa_label_features == NULL) {
609 610 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
610 611 KM_SLEEP) == 0);
611 612 }
612 613
613 614 spa->spa_iokstat = kstat_create("zfs", 0, name,
614 615 "disk", KSTAT_TYPE_IO, 1, 0);
615 616 if (spa->spa_iokstat) {
616 617 spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
617 618 kstat_install(spa->spa_iokstat);
618 619 }
619 620
620 621 spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
621 622
622 623 /*
623 624 * As a pool is being created, treat all features as disabled by
624 625 * setting SPA_FEATURE_DISABLED for all entries in the feature
625 626 * refcount cache.
626 627 */
627 628 for (int i = 0; i < SPA_FEATURES; i++) {
628 629 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
629 630 }
630 631
631 632 return (spa);
632 633 }
633 634
634 635 /*
635 636 * Removes a spa_t from the namespace, freeing up any memory used. Requires
636 637 * spa_namespace_lock. This is called only after the spa_t has been closed and
637 638 * deactivated.
638 639 */
639 640 void
640 641 spa_remove(spa_t *spa)
641 642 {
642 643 spa_config_dirent_t *dp;
643 644
644 645 ASSERT(MUTEX_HELD(&spa_namespace_lock));
645 646 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
646 647
647 648 nvlist_free(spa->spa_config_splitting);
648 649
649 650 avl_remove(&spa_namespace_avl, spa);
650 651 cv_broadcast(&spa_namespace_cv);
651 652
652 653 if (spa->spa_root) {
653 654 spa_strfree(spa->spa_root);
654 655 spa_active_count--;
655 656 }
656 657
657 658 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
658 659 list_remove(&spa->spa_config_list, dp);
659 660 if (dp->scd_path != NULL)
660 661 spa_strfree(dp->scd_path);
661 662 kmem_free(dp, sizeof (spa_config_dirent_t));
662 663 }
663 664
664 665 list_destroy(&spa->spa_config_list);
665 666
666 667 nvlist_free(spa->spa_label_features);
667 668 nvlist_free(spa->spa_load_info);
668 669 spa_config_set(spa, NULL);
669 670
670 671 mutex_enter(&cpu_lock);
671 672 if (spa->spa_deadman_cycid != CYCLIC_NONE)
672 673 cyclic_remove(spa->spa_deadman_cycid);
673 674 mutex_exit(&cpu_lock);
674 675 spa->spa_deadman_cycid = CYCLIC_NONE;
675 676
676 677 refcount_destroy(&spa->spa_refcount);
677 678
678 679 spa_config_lock_destroy(spa);
679 680
680 681 kstat_delete(spa->spa_iokstat);
681 682 spa->spa_iokstat = NULL;
682 683
683 684 for (int t = 0; t < TXG_SIZE; t++)
684 685 bplist_destroy(&spa->spa_free_bplist[t]);
685 686
686 687 cv_destroy(&spa->spa_async_cv);
687 688 cv_destroy(&spa->spa_proc_cv);
688 689 cv_destroy(&spa->spa_scrub_io_cv);
689 690 cv_destroy(&spa->spa_suspend_cv);
690 691
691 692 mutex_destroy(&spa->spa_async_lock);
692 693 mutex_destroy(&spa->spa_errlist_lock);
693 694 mutex_destroy(&spa->spa_errlog_lock);
694 695 mutex_destroy(&spa->spa_history_lock);
695 696 mutex_destroy(&spa->spa_proc_lock);
696 697 mutex_destroy(&spa->spa_props_lock);
697 698 mutex_destroy(&spa->spa_scrub_lock);
698 699 mutex_destroy(&spa->spa_suspend_lock);
699 700 mutex_destroy(&spa->spa_vdev_top_lock);
700 701 mutex_destroy(&spa->spa_iokstat_lock);
701 702
702 703 kmem_free(spa, sizeof (spa_t));
703 704 }
704 705
705 706 /*
706 707 * Given a pool, return the next pool in the namespace, or NULL if there is
707 708 * none. If 'prev' is NULL, return the first pool.
708 709 */
709 710 spa_t *
710 711 spa_next(spa_t *prev)
711 712 {
712 713 ASSERT(MUTEX_HELD(&spa_namespace_lock));
713 714
714 715 if (prev)
715 716 return (AVL_NEXT(&spa_namespace_avl, prev));
716 717 else
717 718 return (avl_first(&spa_namespace_avl));
718 719 }
719 720
720 721 /*
721 722 * ==========================================================================
722 723 * SPA refcount functions
723 724 * ==========================================================================
724 725 */
725 726
726 727 /*
727 728 * Add a reference to the given spa_t. Must have at least one reference, or
728 729 * have the namespace lock held.
729 730 */
730 731 void
731 732 spa_open_ref(spa_t *spa, void *tag)
732 733 {
733 734 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
734 735 MUTEX_HELD(&spa_namespace_lock));
735 736 (void) refcount_add(&spa->spa_refcount, tag);
736 737 }
737 738
738 739 /*
739 740 * Remove a reference to the given spa_t. Must have at least one reference, or
740 741 * have the namespace lock held.
741 742 */
742 743 void
743 744 spa_close(spa_t *spa, void *tag)
744 745 {
745 746 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
746 747 MUTEX_HELD(&spa_namespace_lock));
747 748 (void) refcount_remove(&spa->spa_refcount, tag);
748 749 }
749 750
750 751 /*
751 752 * Check to see if the spa refcount is zero. Must be called with
752 753 * spa_namespace_lock held. We really compare against spa_minref, which is the
753 754 * number of references acquired when opening a pool
754 755 */
755 756 boolean_t
756 757 spa_refcount_zero(spa_t *spa)
757 758 {
758 759 ASSERT(MUTEX_HELD(&spa_namespace_lock));
759 760
760 761 return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
761 762 }
762 763
763 764 /*
764 765 * ==========================================================================
765 766 * SPA spare and l2cache tracking
766 767 * ==========================================================================
767 768 */
768 769
769 770 /*
770 771 * Hot spares and cache devices are tracked using the same code below,
771 772 * for 'auxiliary' devices.
772 773 */
773 774
774 775 typedef struct spa_aux {
775 776 uint64_t aux_guid;
776 777 uint64_t aux_pool;
777 778 avl_node_t aux_avl;
778 779 int aux_count;
779 780 } spa_aux_t;
780 781
781 782 static int
782 783 spa_aux_compare(const void *a, const void *b)
783 784 {
784 785 const spa_aux_t *sa = a;
785 786 const spa_aux_t *sb = b;
786 787
787 788 if (sa->aux_guid < sb->aux_guid)
788 789 return (-1);
789 790 else if (sa->aux_guid > sb->aux_guid)
790 791 return (1);
791 792 else
792 793 return (0);
793 794 }
794 795
795 796 void
796 797 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
797 798 {
798 799 avl_index_t where;
799 800 spa_aux_t search;
800 801 spa_aux_t *aux;
801 802
802 803 search.aux_guid = vd->vdev_guid;
803 804 if ((aux = avl_find(avl, &search, &where)) != NULL) {
804 805 aux->aux_count++;
805 806 } else {
806 807 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
807 808 aux->aux_guid = vd->vdev_guid;
808 809 aux->aux_count = 1;
809 810 avl_insert(avl, aux, where);
810 811 }
811 812 }
812 813
813 814 void
814 815 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
815 816 {
816 817 spa_aux_t search;
817 818 spa_aux_t *aux;
818 819 avl_index_t where;
819 820
820 821 search.aux_guid = vd->vdev_guid;
821 822 aux = avl_find(avl, &search, &where);
822 823
823 824 ASSERT(aux != NULL);
824 825
825 826 if (--aux->aux_count == 0) {
826 827 avl_remove(avl, aux);
827 828 kmem_free(aux, sizeof (spa_aux_t));
828 829 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
829 830 aux->aux_pool = 0ULL;
830 831 }
831 832 }
832 833
833 834 boolean_t
834 835 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
835 836 {
836 837 spa_aux_t search, *found;
837 838
838 839 search.aux_guid = guid;
839 840 found = avl_find(avl, &search, NULL);
840 841
841 842 if (pool) {
842 843 if (found)
843 844 *pool = found->aux_pool;
844 845 else
845 846 *pool = 0ULL;
846 847 }
847 848
848 849 if (refcnt) {
849 850 if (found)
850 851 *refcnt = found->aux_count;
851 852 else
852 853 *refcnt = 0;
853 854 }
854 855
855 856 return (found != NULL);
856 857 }
857 858
858 859 void
859 860 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
860 861 {
861 862 spa_aux_t search, *found;
862 863 avl_index_t where;
863 864
864 865 search.aux_guid = vd->vdev_guid;
865 866 found = avl_find(avl, &search, &where);
866 867 ASSERT(found != NULL);
867 868 ASSERT(found->aux_pool == 0ULL);
868 869
869 870 found->aux_pool = spa_guid(vd->vdev_spa);
870 871 }
871 872
872 873 /*
873 874 * Spares are tracked globally due to the following constraints:
874 875 *
875 876 * - A spare may be part of multiple pools.
876 877 * - A spare may be added to a pool even if it's actively in use within
877 878 * another pool.
878 879 * - A spare in use in any pool can only be the source of a replacement if
879 880 * the target is a spare in the same pool.
880 881 *
881 882 * We keep track of all spares on the system through the use of a reference
882 883 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
883 884 * spare, then we bump the reference count in the AVL tree. In addition, we set
884 885 * the 'vdev_isspare' member to indicate that the device is a spare (active or
885 886 * inactive). When a spare is made active (used to replace a device in the
886 887 * pool), we also keep track of which pool its been made a part of.
887 888 *
888 889 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
889 890 * called under the spa_namespace lock as part of vdev reconfiguration. The
890 891 * separate spare lock exists for the status query path, which does not need to
891 892 * be completely consistent with respect to other vdev configuration changes.
892 893 */
893 894
894 895 static int
895 896 spa_spare_compare(const void *a, const void *b)
896 897 {
897 898 return (spa_aux_compare(a, b));
898 899 }
899 900
900 901 void
901 902 spa_spare_add(vdev_t *vd)
902 903 {
903 904 mutex_enter(&spa_spare_lock);
904 905 ASSERT(!vd->vdev_isspare);
905 906 spa_aux_add(vd, &spa_spare_avl);
906 907 vd->vdev_isspare = B_TRUE;
907 908 mutex_exit(&spa_spare_lock);
908 909 }
909 910
910 911 void
911 912 spa_spare_remove(vdev_t *vd)
912 913 {
913 914 mutex_enter(&spa_spare_lock);
914 915 ASSERT(vd->vdev_isspare);
915 916 spa_aux_remove(vd, &spa_spare_avl);
916 917 vd->vdev_isspare = B_FALSE;
917 918 mutex_exit(&spa_spare_lock);
918 919 }
919 920
920 921 boolean_t
921 922 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
922 923 {
923 924 boolean_t found;
924 925
925 926 mutex_enter(&spa_spare_lock);
926 927 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
927 928 mutex_exit(&spa_spare_lock);
928 929
929 930 return (found);
930 931 }
931 932
932 933 void
933 934 spa_spare_activate(vdev_t *vd)
934 935 {
935 936 mutex_enter(&spa_spare_lock);
936 937 ASSERT(vd->vdev_isspare);
937 938 spa_aux_activate(vd, &spa_spare_avl);
938 939 mutex_exit(&spa_spare_lock);
939 940 }
940 941
941 942 /*
942 943 * Level 2 ARC devices are tracked globally for the same reasons as spares.
943 944 * Cache devices currently only support one pool per cache device, and so
944 945 * for these devices the aux reference count is currently unused beyond 1.
945 946 */
946 947
947 948 static int
948 949 spa_l2cache_compare(const void *a, const void *b)
949 950 {
950 951 return (spa_aux_compare(a, b));
951 952 }
952 953
953 954 void
954 955 spa_l2cache_add(vdev_t *vd)
955 956 {
956 957 mutex_enter(&spa_l2cache_lock);
957 958 ASSERT(!vd->vdev_isl2cache);
958 959 spa_aux_add(vd, &spa_l2cache_avl);
959 960 vd->vdev_isl2cache = B_TRUE;
960 961 mutex_exit(&spa_l2cache_lock);
961 962 }
962 963
963 964 void
964 965 spa_l2cache_remove(vdev_t *vd)
965 966 {
966 967 mutex_enter(&spa_l2cache_lock);
967 968 ASSERT(vd->vdev_isl2cache);
968 969 spa_aux_remove(vd, &spa_l2cache_avl);
969 970 vd->vdev_isl2cache = B_FALSE;
970 971 mutex_exit(&spa_l2cache_lock);
971 972 }
972 973
973 974 boolean_t
974 975 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
975 976 {
976 977 boolean_t found;
977 978
978 979 mutex_enter(&spa_l2cache_lock);
979 980 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
980 981 mutex_exit(&spa_l2cache_lock);
981 982
982 983 return (found);
983 984 }
984 985
985 986 void
986 987 spa_l2cache_activate(vdev_t *vd)
987 988 {
988 989 mutex_enter(&spa_l2cache_lock);
989 990 ASSERT(vd->vdev_isl2cache);
990 991 spa_aux_activate(vd, &spa_l2cache_avl);
991 992 mutex_exit(&spa_l2cache_lock);
992 993 }
993 994
994 995 /*
995 996 * ==========================================================================
996 997 * SPA vdev locking
997 998 * ==========================================================================
998 999 */
999 1000
1000 1001 /*
1001 1002 * Lock the given spa_t for the purpose of adding or removing a vdev.
1002 1003 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1003 1004 * It returns the next transaction group for the spa_t.
1004 1005 */
1005 1006 uint64_t
1006 1007 spa_vdev_enter(spa_t *spa)
1007 1008 {
1008 1009 mutex_enter(&spa->spa_vdev_top_lock);
1009 1010 mutex_enter(&spa_namespace_lock);
1010 1011 return (spa_vdev_config_enter(spa));
1011 1012 }
1012 1013
1013 1014 /*
1014 1015 * Internal implementation for spa_vdev_enter(). Used when a vdev
1015 1016 * operation requires multiple syncs (i.e. removing a device) while
1016 1017 * keeping the spa_namespace_lock held.
1017 1018 */
1018 1019 uint64_t
1019 1020 spa_vdev_config_enter(spa_t *spa)
1020 1021 {
1021 1022 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1022 1023
1023 1024 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1024 1025
1025 1026 return (spa_last_synced_txg(spa) + 1);
1026 1027 }
1027 1028
1028 1029 /*
1029 1030 * Used in combination with spa_vdev_config_enter() to allow the syncing
1030 1031 * of multiple transactions without releasing the spa_namespace_lock.
1031 1032 */
1032 1033 void
1033 1034 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1034 1035 {
1035 1036 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1036 1037
1037 1038 int config_changed = B_FALSE;
1038 1039
1039 1040 ASSERT(txg > spa_last_synced_txg(spa));
1040 1041
1041 1042 spa->spa_pending_vdev = NULL;
1042 1043
1043 1044 /*
1044 1045 * Reassess the DTLs.
1045 1046 */
1046 1047 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1047 1048
1048 1049 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1049 1050 config_changed = B_TRUE;
1050 1051 spa->spa_config_generation++;
1051 1052 }
1052 1053
1053 1054 /*
1054 1055 * Verify the metaslab classes.
1055 1056 */
1056 1057 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1057 1058 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1058 1059
1059 1060 spa_config_exit(spa, SCL_ALL, spa);
1060 1061
1061 1062 /*
1062 1063 * Panic the system if the specified tag requires it. This
1063 1064 * is useful for ensuring that configurations are updated
1064 1065 * transactionally.
1065 1066 */
1066 1067 if (zio_injection_enabled)
1067 1068 zio_handle_panic_injection(spa, tag, 0);
1068 1069
1069 1070 /*
1070 1071 * Note: this txg_wait_synced() is important because it ensures
1071 1072 * that there won't be more than one config change per txg.
1072 1073 * This allows us to use the txg as the generation number.
1073 1074 */
1074 1075 if (error == 0)
1075 1076 txg_wait_synced(spa->spa_dsl_pool, txg);
1076 1077
1077 1078 if (vd != NULL) {
1078 1079 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1079 1080 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1080 1081 vdev_free(vd);
1081 1082 spa_config_exit(spa, SCL_ALL, spa);
1082 1083 }
1083 1084
1084 1085 /*
1085 1086 * If the config changed, update the config cache.
1086 1087 */
1087 1088 if (config_changed)
1088 1089 spa_config_sync(spa, B_FALSE, B_TRUE);
1089 1090 }
1090 1091
1091 1092 /*
1092 1093 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1093 1094 * locking of spa_vdev_enter(), we also want make sure the transactions have
1094 1095 * synced to disk, and then update the global configuration cache with the new
1095 1096 * information.
1096 1097 */
1097 1098 int
1098 1099 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1099 1100 {
1100 1101 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1101 1102 mutex_exit(&spa_namespace_lock);
1102 1103 mutex_exit(&spa->spa_vdev_top_lock);
1103 1104
1104 1105 return (error);
1105 1106 }
1106 1107
1107 1108 /*
1108 1109 * Lock the given spa_t for the purpose of changing vdev state.
1109 1110 */
1110 1111 void
1111 1112 spa_vdev_state_enter(spa_t *spa, int oplocks)
1112 1113 {
1113 1114 int locks = SCL_STATE_ALL | oplocks;
1114 1115
1115 1116 /*
1116 1117 * Root pools may need to read of the underlying devfs filesystem
1117 1118 * when opening up a vdev. Unfortunately if we're holding the
1118 1119 * SCL_ZIO lock it will result in a deadlock when we try to issue
1119 1120 * the read from the root filesystem. Instead we "prefetch"
1120 1121 * the associated vnodes that we need prior to opening the
1121 1122 * underlying devices and cache them so that we can prevent
1122 1123 * any I/O when we are doing the actual open.
1123 1124 */
1124 1125 if (spa_is_root(spa)) {
1125 1126 int low = locks & ~(SCL_ZIO - 1);
1126 1127 int high = locks & ~low;
1127 1128
1128 1129 spa_config_enter(spa, high, spa, RW_WRITER);
1129 1130 vdev_hold(spa->spa_root_vdev);
1130 1131 spa_config_enter(spa, low, spa, RW_WRITER);
1131 1132 } else {
1132 1133 spa_config_enter(spa, locks, spa, RW_WRITER);
1133 1134 }
1134 1135 spa->spa_vdev_locks = locks;
1135 1136 }
1136 1137
1137 1138 int
1138 1139 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1139 1140 {
1140 1141 boolean_t config_changed = B_FALSE;
1141 1142
1142 1143 if (vd != NULL || error == 0)
1143 1144 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1144 1145 0, 0, B_FALSE);
1145 1146
1146 1147 if (vd != NULL) {
1147 1148 vdev_state_dirty(vd->vdev_top);
1148 1149 config_changed = B_TRUE;
1149 1150 spa->spa_config_generation++;
1150 1151 }
1151 1152
1152 1153 if (spa_is_root(spa))
1153 1154 vdev_rele(spa->spa_root_vdev);
1154 1155
1155 1156 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1156 1157 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1157 1158
1158 1159 /*
1159 1160 * If anything changed, wait for it to sync. This ensures that,
1160 1161 * from the system administrator's perspective, zpool(1M) commands
1161 1162 * are synchronous. This is important for things like zpool offline:
1162 1163 * when the command completes, you expect no further I/O from ZFS.
1163 1164 */
1164 1165 if (vd != NULL)
1165 1166 txg_wait_synced(spa->spa_dsl_pool, 0);
1166 1167
1167 1168 /*
1168 1169 * If the config changed, update the config cache.
1169 1170 */
1170 1171 if (config_changed) {
1171 1172 mutex_enter(&spa_namespace_lock);
1172 1173 spa_config_sync(spa, B_FALSE, B_TRUE);
1173 1174 mutex_exit(&spa_namespace_lock);
1174 1175 }
1175 1176
1176 1177 return (error);
1177 1178 }
1178 1179
1179 1180 /*
1180 1181 * ==========================================================================
1181 1182 * Miscellaneous functions
1182 1183 * ==========================================================================
1183 1184 */
1184 1185
1185 1186 void
1186 1187 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1187 1188 {
1188 1189 if (!nvlist_exists(spa->spa_label_features, feature)) {
1189 1190 fnvlist_add_boolean(spa->spa_label_features, feature);
1190 1191 /*
1191 1192 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1192 1193 * dirty the vdev config because lock SCL_CONFIG is not held.
1193 1194 * Thankfully, in this case we don't need to dirty the config
1194 1195 * because it will be written out anyway when we finish
1195 1196 * creating the pool.
1196 1197 */
1197 1198 if (tx->tx_txg != TXG_INITIAL)
1198 1199 vdev_config_dirty(spa->spa_root_vdev);
1199 1200 }
1200 1201 }
1201 1202
1202 1203 void
1203 1204 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1204 1205 {
1205 1206 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1206 1207 vdev_config_dirty(spa->spa_root_vdev);
1207 1208 }
1208 1209
1209 1210 /*
1210 1211 * Rename a spa_t.
1211 1212 */
1212 1213 int
1213 1214 spa_rename(const char *name, const char *newname)
1214 1215 {
1215 1216 spa_t *spa;
1216 1217 int err;
1217 1218
1218 1219 /*
1219 1220 * Lookup the spa_t and grab the config lock for writing. We need to
1220 1221 * actually open the pool so that we can sync out the necessary labels.
1221 1222 * It's OK to call spa_open() with the namespace lock held because we
1222 1223 * allow recursive calls for other reasons.
1223 1224 */
1224 1225 mutex_enter(&spa_namespace_lock);
1225 1226 if ((err = spa_open(name, &spa, FTAG)) != 0) {
1226 1227 mutex_exit(&spa_namespace_lock);
1227 1228 return (err);
1228 1229 }
1229 1230
1230 1231 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1231 1232
1232 1233 avl_remove(&spa_namespace_avl, spa);
1233 1234 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1234 1235 avl_add(&spa_namespace_avl, spa);
1235 1236
1236 1237 /*
1237 1238 * Sync all labels to disk with the new names by marking the root vdev
1238 1239 * dirty and waiting for it to sync. It will pick up the new pool name
1239 1240 * during the sync.
1240 1241 */
1241 1242 vdev_config_dirty(spa->spa_root_vdev);
1242 1243
1243 1244 spa_config_exit(spa, SCL_ALL, FTAG);
1244 1245
1245 1246 txg_wait_synced(spa->spa_dsl_pool, 0);
1246 1247
1247 1248 /*
1248 1249 * Sync the updated config cache.
1249 1250 */
1250 1251 spa_config_sync(spa, B_FALSE, B_TRUE);
1251 1252
1252 1253 spa_close(spa, FTAG);
1253 1254
1254 1255 mutex_exit(&spa_namespace_lock);
1255 1256
1256 1257 return (0);
1257 1258 }
1258 1259
1259 1260 /*
1260 1261 * Return the spa_t associated with given pool_guid, if it exists. If
1261 1262 * device_guid is non-zero, determine whether the pool exists *and* contains
1262 1263 * a device with the specified device_guid.
1263 1264 */
1264 1265 spa_t *
1265 1266 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1266 1267 {
1267 1268 spa_t *spa;
1268 1269 avl_tree_t *t = &spa_namespace_avl;
1269 1270
1270 1271 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1271 1272
1272 1273 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1273 1274 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1274 1275 continue;
1275 1276 if (spa->spa_root_vdev == NULL)
1276 1277 continue;
1277 1278 if (spa_guid(spa) == pool_guid) {
1278 1279 if (device_guid == 0)
1279 1280 break;
1280 1281
1281 1282 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1282 1283 device_guid) != NULL)
1283 1284 break;
1284 1285
1285 1286 /*
1286 1287 * Check any devices we may be in the process of adding.
1287 1288 */
1288 1289 if (spa->spa_pending_vdev) {
1289 1290 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1290 1291 device_guid) != NULL)
1291 1292 break;
1292 1293 }
1293 1294 }
1294 1295 }
1295 1296
1296 1297 return (spa);
1297 1298 }
1298 1299
1299 1300 /*
1300 1301 * Determine whether a pool with the given pool_guid exists.
1301 1302 */
1302 1303 boolean_t
1303 1304 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1304 1305 {
1305 1306 return (spa_by_guid(pool_guid, device_guid) != NULL);
1306 1307 }
1307 1308
1308 1309 char *
1309 1310 spa_strdup(const char *s)
1310 1311 {
1311 1312 size_t len;
1312 1313 char *new;
1313 1314
1314 1315 len = strlen(s);
1315 1316 new = kmem_alloc(len + 1, KM_SLEEP);
1316 1317 bcopy(s, new, len);
1317 1318 new[len] = '\0';
1318 1319
1319 1320 return (new);
1320 1321 }
1321 1322
1322 1323 void
1323 1324 spa_strfree(char *s)
1324 1325 {
1325 1326 kmem_free(s, strlen(s) + 1);
1326 1327 }
1327 1328
1328 1329 uint64_t
1329 1330 spa_get_random(uint64_t range)
1330 1331 {
1331 1332 uint64_t r;
1332 1333
1333 1334 ASSERT(range != 0);
1334 1335
1335 1336 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1336 1337
1337 1338 return (r % range);
1338 1339 }
1339 1340
1340 1341 uint64_t
1341 1342 spa_generate_guid(spa_t *spa)
1342 1343 {
1343 1344 uint64_t guid = spa_get_random(-1ULL);
1344 1345
1345 1346 if (spa != NULL) {
1346 1347 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1347 1348 guid = spa_get_random(-1ULL);
1348 1349 } else {
1349 1350 while (guid == 0 || spa_guid_exists(guid, 0))
1350 1351 guid = spa_get_random(-1ULL);
1351 1352 }
1352 1353
1353 1354 return (guid);
1354 1355 }
1355 1356
1356 1357 void
1357 1358 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1358 1359 {
1359 1360 char type[256];
1360 1361 char *checksum = NULL;
1361 1362 char *compress = NULL;
1362 1363
1363 1364 if (bp != NULL) {
1364 1365 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1365 1366 dmu_object_byteswap_t bswap =
1366 1367 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1367 1368 (void) snprintf(type, sizeof (type), "bswap %s %s",
1368 1369 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1369 1370 "metadata" : "data",
1370 1371 dmu_ot_byteswap[bswap].ob_name);
1371 1372 } else {
1372 1373 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1373 1374 sizeof (type));
1374 1375 }
1375 1376 if (!BP_IS_EMBEDDED(bp)) {
1376 1377 checksum =
1377 1378 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1378 1379 }
1379 1380 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1380 1381 }
1381 1382
1382 1383 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1383 1384 compress);
1384 1385 }
1385 1386
1386 1387 void
1387 1388 spa_freeze(spa_t *spa)
1388 1389 {
1389 1390 uint64_t freeze_txg = 0;
1390 1391
1391 1392 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1392 1393 if (spa->spa_freeze_txg == UINT64_MAX) {
1393 1394 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1394 1395 spa->spa_freeze_txg = freeze_txg;
1395 1396 }
1396 1397 spa_config_exit(spa, SCL_ALL, FTAG);
1397 1398 if (freeze_txg != 0)
1398 1399 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1399 1400 }
1400 1401
1401 1402 void
1402 1403 zfs_panic_recover(const char *fmt, ...)
1403 1404 {
1404 1405 va_list adx;
1405 1406
1406 1407 va_start(adx, fmt);
1407 1408 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1408 1409 va_end(adx);
1409 1410 }
1410 1411
1411 1412 /*
1412 1413 * This is a stripped-down version of strtoull, suitable only for converting
1413 1414 * lowercase hexadecimal numbers that don't overflow.
1414 1415 */
1415 1416 uint64_t
1416 1417 strtonum(const char *str, char **nptr)
1417 1418 {
1418 1419 uint64_t val = 0;
1419 1420 char c;
1420 1421 int digit;
1421 1422
1422 1423 while ((c = *str) != '\0') {
1423 1424 if (c >= '0' && c <= '9')
1424 1425 digit = c - '0';
1425 1426 else if (c >= 'a' && c <= 'f')
1426 1427 digit = 10 + c - 'a';
1427 1428 else
1428 1429 break;
1429 1430
1430 1431 val *= 16;
1431 1432 val += digit;
1432 1433
1433 1434 str++;
1434 1435 }
1435 1436
1436 1437 if (nptr)
1437 1438 *nptr = (char *)str;
1438 1439
1439 1440 return (val);
1440 1441 }
1441 1442
1442 1443 /*
1443 1444 * ==========================================================================
1444 1445 * Accessor functions
1445 1446 * ==========================================================================
1446 1447 */
1447 1448
1448 1449 boolean_t
1449 1450 spa_shutting_down(spa_t *spa)
1450 1451 {
1451 1452 return (spa->spa_async_suspended);
1452 1453 }
1453 1454
1454 1455 dsl_pool_t *
1455 1456 spa_get_dsl(spa_t *spa)
1456 1457 {
1457 1458 return (spa->spa_dsl_pool);
1458 1459 }
1459 1460
1460 1461 boolean_t
1461 1462 spa_is_initializing(spa_t *spa)
1462 1463 {
1463 1464 return (spa->spa_is_initializing);
1464 1465 }
1465 1466
1466 1467 blkptr_t *
1467 1468 spa_get_rootblkptr(spa_t *spa)
1468 1469 {
1469 1470 return (&spa->spa_ubsync.ub_rootbp);
1470 1471 }
1471 1472
1472 1473 void
1473 1474 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1474 1475 {
1475 1476 spa->spa_uberblock.ub_rootbp = *bp;
1476 1477 }
1477 1478
1478 1479 void
1479 1480 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1480 1481 {
1481 1482 if (spa->spa_root == NULL)
1482 1483 buf[0] = '\0';
1483 1484 else
1484 1485 (void) strncpy(buf, spa->spa_root, buflen);
1485 1486 }
1486 1487
1487 1488 int
1488 1489 spa_sync_pass(spa_t *spa)
1489 1490 {
1490 1491 return (spa->spa_sync_pass);
1491 1492 }
1492 1493
1493 1494 char *
1494 1495 spa_name(spa_t *spa)
1495 1496 {
1496 1497 return (spa->spa_name);
1497 1498 }
1498 1499
1499 1500 uint64_t
1500 1501 spa_guid(spa_t *spa)
1501 1502 {
1502 1503 dsl_pool_t *dp = spa_get_dsl(spa);
1503 1504 uint64_t guid;
1504 1505
1505 1506 /*
1506 1507 * If we fail to parse the config during spa_load(), we can go through
1507 1508 * the error path (which posts an ereport) and end up here with no root
1508 1509 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1509 1510 * this case.
1510 1511 */
1511 1512 if (spa->spa_root_vdev == NULL)
1512 1513 return (spa->spa_config_guid);
1513 1514
1514 1515 guid = spa->spa_last_synced_guid != 0 ?
1515 1516 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1516 1517
1517 1518 /*
1518 1519 * Return the most recently synced out guid unless we're
1519 1520 * in syncing context.
1520 1521 */
1521 1522 if (dp && dsl_pool_sync_context(dp))
1522 1523 return (spa->spa_root_vdev->vdev_guid);
1523 1524 else
1524 1525 return (guid);
1525 1526 }
1526 1527
1527 1528 uint64_t
1528 1529 spa_load_guid(spa_t *spa)
1529 1530 {
1530 1531 /*
1531 1532 * This is a GUID that exists solely as a reference for the
1532 1533 * purposes of the arc. It is generated at load time, and
1533 1534 * is never written to persistent storage.
1534 1535 */
1535 1536 return (spa->spa_load_guid);
1536 1537 }
1537 1538
1538 1539 uint64_t
1539 1540 spa_last_synced_txg(spa_t *spa)
1540 1541 {
1541 1542 return (spa->spa_ubsync.ub_txg);
1542 1543 }
1543 1544
1544 1545 uint64_t
1545 1546 spa_first_txg(spa_t *spa)
1546 1547 {
1547 1548 return (spa->spa_first_txg);
1548 1549 }
1549 1550
1550 1551 uint64_t
1551 1552 spa_syncing_txg(spa_t *spa)
1552 1553 {
1553 1554 return (spa->spa_syncing_txg);
1554 1555 }
1555 1556
1556 1557 pool_state_t
1557 1558 spa_state(spa_t *spa)
1558 1559 {
1559 1560 return (spa->spa_state);
1560 1561 }
1561 1562
1562 1563 spa_load_state_t
1563 1564 spa_load_state(spa_t *spa)
1564 1565 {
1565 1566 return (spa->spa_load_state);
1566 1567 }
1567 1568
1568 1569 uint64_t
1569 1570 spa_freeze_txg(spa_t *spa)
1570 1571 {
1571 1572 return (spa->spa_freeze_txg);
1572 1573 }
1573 1574
1574 1575 /* ARGSUSED */
1575 1576 uint64_t
1576 1577 spa_get_asize(spa_t *spa, uint64_t lsize)
1577 1578 {
1578 1579 return (lsize * spa_asize_inflation);
1579 1580 }
1580 1581
1581 1582 uint64_t
1582 1583 spa_get_dspace(spa_t *spa)
1583 1584 {
1584 1585 return (spa->spa_dspace);
1585 1586 }
1586 1587
1587 1588 void
1588 1589 spa_update_dspace(spa_t *spa)
1589 1590 {
1590 1591 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1591 1592 ddt_get_dedup_dspace(spa);
1592 1593 }
1593 1594
1594 1595 /*
1595 1596 * Return the failure mode that has been set to this pool. The default
1596 1597 * behavior will be to block all I/Os when a complete failure occurs.
1597 1598 */
1598 1599 uint8_t
1599 1600 spa_get_failmode(spa_t *spa)
1600 1601 {
1601 1602 return (spa->spa_failmode);
1602 1603 }
1603 1604
1604 1605 boolean_t
1605 1606 spa_suspended(spa_t *spa)
1606 1607 {
1607 1608 return (spa->spa_suspended);
1608 1609 }
1609 1610
1610 1611 uint64_t
1611 1612 spa_version(spa_t *spa)
1612 1613 {
1613 1614 return (spa->spa_ubsync.ub_version);
1614 1615 }
1615 1616
1616 1617 boolean_t
1617 1618 spa_deflate(spa_t *spa)
1618 1619 {
1619 1620 return (spa->spa_deflate);
1620 1621 }
1621 1622
1622 1623 metaslab_class_t *
1623 1624 spa_normal_class(spa_t *spa)
1624 1625 {
1625 1626 return (spa->spa_normal_class);
1626 1627 }
1627 1628
1628 1629 metaslab_class_t *
1629 1630 spa_log_class(spa_t *spa)
1630 1631 {
1631 1632 return (spa->spa_log_class);
1632 1633 }
1633 1634
1634 1635 int
1635 1636 spa_max_replication(spa_t *spa)
1636 1637 {
1637 1638 /*
1638 1639 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1639 1640 * handle BPs with more than one DVA allocated. Set our max
1640 1641 * replication level accordingly.
1641 1642 */
1642 1643 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1643 1644 return (1);
1644 1645 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1645 1646 }
1646 1647
1647 1648 int
1648 1649 spa_prev_software_version(spa_t *spa)
1649 1650 {
1650 1651 return (spa->spa_prev_software_version);
1651 1652 }
1652 1653
1653 1654 uint64_t
1654 1655 spa_deadman_synctime(spa_t *spa)
1655 1656 {
1656 1657 return (spa->spa_deadman_synctime);
1657 1658 }
1658 1659
1659 1660 uint64_t
1660 1661 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1661 1662 {
1662 1663 uint64_t asize = DVA_GET_ASIZE(dva);
1663 1664 uint64_t dsize = asize;
1664 1665
1665 1666 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1666 1667
1667 1668 if (asize != 0 && spa->spa_deflate) {
1668 1669 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1669 1670 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
1670 1671 }
1671 1672
1672 1673 return (dsize);
1673 1674 }
1674 1675
1675 1676 uint64_t
1676 1677 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1677 1678 {
1678 1679 uint64_t dsize = 0;
1679 1680
1680 1681 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1681 1682 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1682 1683
1683 1684 return (dsize);
1684 1685 }
1685 1686
1686 1687 uint64_t
1687 1688 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1688 1689 {
1689 1690 uint64_t dsize = 0;
1690 1691
1691 1692 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1692 1693
1693 1694 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1694 1695 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1695 1696
1696 1697 spa_config_exit(spa, SCL_VDEV, FTAG);
1697 1698
1698 1699 return (dsize);
1699 1700 }
1700 1701
1701 1702 /*
1702 1703 * ==========================================================================
1703 1704 * Initialization and Termination
1704 1705 * ==========================================================================
1705 1706 */
1706 1707
1707 1708 static int
1708 1709 spa_name_compare(const void *a1, const void *a2)
1709 1710 {
1710 1711 const spa_t *s1 = a1;
1711 1712 const spa_t *s2 = a2;
1712 1713 int s;
1713 1714
1714 1715 s = strcmp(s1->spa_name, s2->spa_name);
1715 1716 if (s > 0)
1716 1717 return (1);
1717 1718 if (s < 0)
1718 1719 return (-1);
1719 1720 return (0);
1720 1721 }
1721 1722
1722 1723 int
1723 1724 spa_busy(void)
1724 1725 {
1725 1726 return (spa_active_count);
1726 1727 }
1727 1728
1728 1729 void
1729 1730 spa_boot_init()
1730 1731 {
1731 1732 spa_config_load();
1732 1733 }
1733 1734
1734 1735 void
1735 1736 spa_init(int mode)
1736 1737 {
1737 1738 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1738 1739 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1739 1740 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1740 1741 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1741 1742
1742 1743 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1743 1744 offsetof(spa_t, spa_avl));
1744 1745
1745 1746 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1746 1747 offsetof(spa_aux_t, aux_avl));
1747 1748
1748 1749 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1749 1750 offsetof(spa_aux_t, aux_avl));
1750 1751
1751 1752 spa_mode_global = mode;
1752 1753
1753 1754 #ifdef _KERNEL
1754 1755 spa_arch_init();
1755 1756 #else
1756 1757 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1757 1758 arc_procfd = open("/proc/self/ctl", O_WRONLY);
1758 1759 if (arc_procfd == -1) {
1759 1760 perror("could not enable watchpoints: "
1760 1761 "opening /proc/self/ctl failed: ");
1761 1762 } else {
1762 1763 arc_watch = B_TRUE;
1763 1764 }
1764 1765 }
1765 1766 #endif
1766 1767
1767 1768 refcount_init();
1768 1769 unique_init();
1769 1770 range_tree_init();
1770 1771 zio_init();
1771 1772 dmu_init();
1772 1773 zil_init();
1773 1774 vdev_cache_stat_init();
1774 1775 zfs_prop_init();
1775 1776 zpool_prop_init();
1776 1777 zpool_feature_init();
1777 1778 spa_config_load();
1778 1779 l2arc_start();
1779 1780 }
1780 1781
1781 1782 void
1782 1783 spa_fini(void)
1783 1784 {
1784 1785 l2arc_stop();
1785 1786
1786 1787 spa_evict_all();
1787 1788
1788 1789 vdev_cache_stat_fini();
1789 1790 zil_fini();
1790 1791 dmu_fini();
1791 1792 zio_fini();
1792 1793 range_tree_fini();
1793 1794 unique_fini();
1794 1795 refcount_fini();
1795 1796
1796 1797 avl_destroy(&spa_namespace_avl);
1797 1798 avl_destroy(&spa_spare_avl);
1798 1799 avl_destroy(&spa_l2cache_avl);
1799 1800
1800 1801 cv_destroy(&spa_namespace_cv);
1801 1802 mutex_destroy(&spa_namespace_lock);
1802 1803 mutex_destroy(&spa_spare_lock);
1803 1804 mutex_destroy(&spa_l2cache_lock);
1804 1805 }
1805 1806
1806 1807 /*
1807 1808 * Return whether this pool has slogs. No locking needed.
1808 1809 * It's not a problem if the wrong answer is returned as it's only for
1809 1810 * performance and not correctness
1810 1811 */
1811 1812 boolean_t
1812 1813 spa_has_slogs(spa_t *spa)
1813 1814 {
1814 1815 return (spa->spa_log_class->mc_rotor != NULL);
1815 1816 }
1816 1817
1817 1818 spa_log_state_t
1818 1819 spa_get_log_state(spa_t *spa)
1819 1820 {
1820 1821 return (spa->spa_log_state);
1821 1822 }
1822 1823
1823 1824 void
1824 1825 spa_set_log_state(spa_t *spa, spa_log_state_t state)
1825 1826 {
1826 1827 spa->spa_log_state = state;
1827 1828 }
1828 1829
1829 1830 boolean_t
1830 1831 spa_is_root(spa_t *spa)
1831 1832 {
1832 1833 return (spa->spa_is_root);
1833 1834 }
1834 1835
1835 1836 boolean_t
1836 1837 spa_writeable(spa_t *spa)
1837 1838 {
1838 1839 return (!!(spa->spa_mode & FWRITE));
1839 1840 }
1840 1841
1841 1842 int
1842 1843 spa_mode(spa_t *spa)
1843 1844 {
1844 1845 return (spa->spa_mode);
1845 1846 }
1846 1847
1847 1848 uint64_t
1848 1849 spa_bootfs(spa_t *spa)
1849 1850 {
1850 1851 return (spa->spa_bootfs);
1851 1852 }
1852 1853
1853 1854 uint64_t
1854 1855 spa_delegation(spa_t *spa)
1855 1856 {
1856 1857 return (spa->spa_delegation);
1857 1858 }
1858 1859
1859 1860 objset_t *
1860 1861 spa_meta_objset(spa_t *spa)
1861 1862 {
1862 1863 return (spa->spa_meta_objset);
1863 1864 }
1864 1865
1865 1866 enum zio_checksum
1866 1867 spa_dedup_checksum(spa_t *spa)
1867 1868 {
1868 1869 return (spa->spa_dedup_checksum);
1869 1870 }
1870 1871
1871 1872 /*
1872 1873 * Reset pool scan stat per scan pass (or reboot).
1873 1874 */
1874 1875 void
1875 1876 spa_scan_stat_init(spa_t *spa)
1876 1877 {
1877 1878 /* data not stored on disk */
1878 1879 spa->spa_scan_pass_start = gethrestime_sec();
1879 1880 spa->spa_scan_pass_exam = 0;
1880 1881 vdev_scan_stat_init(spa->spa_root_vdev);
1881 1882 }
1882 1883
1883 1884 /*
1884 1885 * Get scan stats for zpool status reports
1885 1886 */
1886 1887 int
1887 1888 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
1888 1889 {
1889 1890 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
1890 1891
1891 1892 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
1892 1893 return (SET_ERROR(ENOENT));
1893 1894 bzero(ps, sizeof (pool_scan_stat_t));
1894 1895
1895 1896 /* data stored on disk */
1896 1897 ps->pss_func = scn->scn_phys.scn_func;
1897 1898 ps->pss_start_time = scn->scn_phys.scn_start_time;
1898 1899 ps->pss_end_time = scn->scn_phys.scn_end_time;
1899 1900 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
1900 1901 ps->pss_examined = scn->scn_phys.scn_examined;
1901 1902 ps->pss_to_process = scn->scn_phys.scn_to_process;
1902 1903 ps->pss_processed = scn->scn_phys.scn_processed;
1903 1904 ps->pss_errors = scn->scn_phys.scn_errors;
1904 1905 ps->pss_state = scn->scn_phys.scn_state;
1905 1906
1906 1907 /* data not stored on disk */
1907 1908 ps->pss_pass_start = spa->spa_scan_pass_start;
1908 1909 ps->pss_pass_exam = spa->spa_scan_pass_exam;
1909 1910
1910 1911 return (0);
1911 1912 }
1912 1913
1913 1914 boolean_t
1914 1915 spa_debug_enabled(spa_t *spa)
1915 1916 {
1916 1917 return (spa->spa_debug);
1917 1918 }
↓ open down ↓ |
1429 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX