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