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