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