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