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