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