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