]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/spa_misc.c
54299a7e451663891daafd289972dc9ecdfb88fe
[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) 2011, 2017 by Delphix. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26 * Copyright 2013 Saso Kiselkov. All rights reserved.
27 * Copyright (c) 2017 Datto Inc.
28 */
29
30 #include <sys/zfs_context.h>
31 #include <sys/spa_impl.h>
32 #include <sys/zio.h>
33 #include <sys/zio_checksum.h>
34 #include <sys/zio_compress.h>
35 #include <sys/dmu.h>
36 #include <sys/dmu_tx.h>
37 #include <sys/zap.h>
38 #include <sys/zil.h>
39 #include <sys/vdev_impl.h>
40 #include <sys/vdev_file.h>
41 #include <sys/vdev_raidz.h>
42 #include <sys/metaslab.h>
43 #include <sys/uberblock_impl.h>
44 #include <sys/txg.h>
45 #include <sys/avl.h>
46 #include <sys/unique.h>
47 #include <sys/dsl_pool.h>
48 #include <sys/dsl_dir.h>
49 #include <sys/dsl_prop.h>
50 #include <sys/fm/util.h>
51 #include <sys/dsl_scan.h>
52 #include <sys/fs/zfs.h>
53 #include <sys/metaslab_impl.h>
54 #include <sys/arc.h>
55 #include <sys/ddt.h>
56 #include <sys/kstat.h>
57 #include "zfs_prop.h"
58 #include <sys/zfeature.h>
59 #include "qat.h"
60
61 /*
62 * SPA locking
63 *
64 * There are four basic locks for managing spa_t structures:
65 *
66 * spa_namespace_lock (global mutex)
67 *
68 * This lock must be acquired to do any of the following:
69 *
70 * - Lookup a spa_t by name
71 * - Add or remove a spa_t from the namespace
72 * - Increase spa_refcount from non-zero
73 * - Check if spa_refcount is zero
74 * - Rename a spa_t
75 * - add/remove/attach/detach devices
76 * - Held for the duration of create/destroy/import/export
77 *
78 * It does not need to handle recursion. A create or destroy may
79 * reference objects (files or zvols) in other pools, but by
80 * definition they must have an existing reference, and will never need
81 * to lookup a spa_t by name.
82 *
83 * spa_refcount (per-spa refcount_t protected by mutex)
84 *
85 * This reference count keep track of any active users of the spa_t. The
86 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
87 * the refcount is never really 'zero' - opening a pool implicitly keeps
88 * some references in the DMU. Internally we check against spa_minref, but
89 * present the image of a zero/non-zero value to consumers.
90 *
91 * spa_config_lock[] (per-spa array of rwlocks)
92 *
93 * This protects the spa_t from config changes, and must be held in
94 * the following circumstances:
95 *
96 * - RW_READER to perform I/O to the spa
97 * - RW_WRITER to change the vdev config
98 *
99 * The locking order is fairly straightforward:
100 *
101 * spa_namespace_lock -> spa_refcount
102 *
103 * The namespace lock must be acquired to increase the refcount from 0
104 * or to check if it is zero.
105 *
106 * spa_refcount -> spa_config_lock[]
107 *
108 * There must be at least one valid reference on the spa_t to acquire
109 * the config lock.
110 *
111 * spa_namespace_lock -> spa_config_lock[]
112 *
113 * The namespace lock must always be taken before the config lock.
114 *
115 *
116 * The spa_namespace_lock can be acquired directly and is globally visible.
117 *
118 * The namespace is manipulated using the following functions, all of which
119 * require the spa_namespace_lock to be held.
120 *
121 * spa_lookup() Lookup a spa_t by name.
122 *
123 * spa_add() Create a new spa_t in the namespace.
124 *
125 * spa_remove() Remove a spa_t from the namespace. This also
126 * frees up any memory associated with the spa_t.
127 *
128 * spa_next() Returns the next spa_t in the system, or the
129 * first if NULL is passed.
130 *
131 * spa_evict_all() Shutdown and remove all spa_t structures in
132 * the system.
133 *
134 * spa_guid_exists() Determine whether a pool/device guid exists.
135 *
136 * The spa_refcount is manipulated using the following functions:
137 *
138 * spa_open_ref() Adds a reference to the given spa_t. Must be
139 * called with spa_namespace_lock held if the
140 * refcount is currently zero.
141 *
142 * spa_close() Remove a reference from the spa_t. This will
143 * not free the spa_t or remove it from the
144 * namespace. No locking is required.
145 *
146 * spa_refcount_zero() Returns true if the refcount is currently
147 * zero. Must be called with spa_namespace_lock
148 * held.
149 *
150 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
151 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
152 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
153 *
154 * To read the configuration, it suffices to hold one of these locks as reader.
155 * To modify the configuration, you must hold all locks as writer. To modify
156 * vdev state without altering the vdev tree's topology (e.g. online/offline),
157 * you must hold SCL_STATE and SCL_ZIO as writer.
158 *
159 * We use these distinct config locks to avoid recursive lock entry.
160 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
161 * block allocations (SCL_ALLOC), which may require reading space maps
162 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
163 *
164 * The spa config locks cannot be normal rwlocks because we need the
165 * ability to hand off ownership. For example, SCL_ZIO is acquired
166 * by the issuing thread and later released by an interrupt thread.
167 * They do, however, obey the usual write-wanted semantics to prevent
168 * writer (i.e. system administrator) starvation.
169 *
170 * The lock acquisition rules are as follows:
171 *
172 * SCL_CONFIG
173 * Protects changes to the vdev tree topology, such as vdev
174 * add/remove/attach/detach. Protects the dirty config list
175 * (spa_config_dirty_list) and the set of spares and l2arc devices.
176 *
177 * SCL_STATE
178 * Protects changes to pool state and vdev state, such as vdev
179 * online/offline/fault/degrade/clear. Protects the dirty state list
180 * (spa_state_dirty_list) and global pool state (spa_state).
181 *
182 * SCL_ALLOC
183 * Protects changes to metaslab groups and classes.
184 * Held as reader by metaslab_alloc() and metaslab_claim().
185 *
186 * SCL_ZIO
187 * Held by bp-level zios (those which have no io_vd upon entry)
188 * to prevent changes to the vdev tree. The bp-level zio implicitly
189 * protects all of its vdev child zios, which do not hold SCL_ZIO.
190 *
191 * SCL_FREE
192 * Protects changes to metaslab groups and classes.
193 * Held as reader by metaslab_free(). SCL_FREE is distinct from
194 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
195 * blocks in zio_done() while another i/o that holds either
196 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
197 *
198 * SCL_VDEV
199 * Held as reader to prevent changes to the vdev tree during trivial
200 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
201 * other locks, and lower than all of them, to ensure that it's safe
202 * to acquire regardless of caller context.
203 *
204 * In addition, the following rules apply:
205 *
206 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
207 * The lock ordering is SCL_CONFIG > spa_props_lock.
208 *
209 * (b) I/O operations on leaf vdevs. For any zio operation that takes
210 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
211 * or zio_write_phys() -- the caller must ensure that the config cannot
212 * cannot change in the interim, and that the vdev cannot be reopened.
213 * SCL_STATE as reader suffices for both.
214 *
215 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
216 *
217 * spa_vdev_enter() Acquire the namespace lock and the config lock
218 * for writing.
219 *
220 * spa_vdev_exit() Release the config lock, wait for all I/O
221 * to complete, sync the updated configs to the
222 * cache, and release the namespace lock.
223 *
224 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
225 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
226 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
227 *
228 * spa_rename() is also implemented within this file since it requires
229 * manipulation of the namespace.
230 */
231
232 static avl_tree_t spa_namespace_avl;
233 kmutex_t spa_namespace_lock;
234 static kcondvar_t spa_namespace_cv;
235 int spa_max_replication_override = SPA_DVAS_PER_BP;
236
237 static kmutex_t spa_spare_lock;
238 static avl_tree_t spa_spare_avl;
239 static kmutex_t spa_l2cache_lock;
240 static avl_tree_t spa_l2cache_avl;
241
242 kmem_cache_t *spa_buffer_pool;
243 int spa_mode_global;
244
245 #ifdef ZFS_DEBUG
246 /*
247 * Everything except dprintf, set_error, spa, and indirect_remap is on
248 * by default in debug builds.
249 */
250 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SET_ERROR |
251 ZFS_DEBUG_SPA | ZFS_DEBUG_INDIRECT_REMAP);
252 #else
253 int zfs_flags = 0;
254 #endif
255
256 /*
257 * zfs_recover can be set to nonzero to attempt to recover from
258 * otherwise-fatal errors, typically caused by on-disk corruption. When
259 * set, calls to zfs_panic_recover() will turn into warning messages.
260 * This should only be used as a last resort, as it typically results
261 * in leaked space, or worse.
262 */
263 int zfs_recover = B_FALSE;
264
265 /*
266 * If destroy encounters an EIO while reading metadata (e.g. indirect
267 * blocks), space referenced by the missing metadata can not be freed.
268 * Normally this causes the background destroy to become "stalled", as
269 * it is unable to make forward progress. While in this stalled state,
270 * all remaining space to free from the error-encountering filesystem is
271 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
272 * permanently leak the space from indirect blocks that can not be read,
273 * and continue to free everything else that it can.
274 *
275 * The default, "stalling" behavior is useful if the storage partially
276 * fails (i.e. some but not all i/os fail), and then later recovers. In
277 * this case, we will be able to continue pool operations while it is
278 * partially failed, and when it recovers, we can continue to free the
279 * space, with no leaks. However, note that this case is actually
280 * fairly rare.
281 *
282 * Typically pools either (a) fail completely (but perhaps temporarily,
283 * e.g. a top-level vdev going offline), or (b) have localized,
284 * permanent errors (e.g. disk returns the wrong data due to bit flip or
285 * firmware bug). In case (a), this setting does not matter because the
286 * pool will be suspended and the sync thread will not be able to make
287 * forward progress regardless. In case (b), because the error is
288 * permanent, the best we can do is leak the minimum amount of space,
289 * which is what setting this flag will do. Therefore, it is reasonable
290 * for this flag to normally be set, but we chose the more conservative
291 * approach of not setting it, so that there is no possibility of
292 * leaking space in the "partial temporary" failure case.
293 */
294 int zfs_free_leak_on_eio = B_FALSE;
295
296 /*
297 * Expiration time in milliseconds. This value has two meanings. First it is
298 * used to determine when the spa_deadman() logic should fire. By default the
299 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds.
300 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
301 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
302 * in one of three behaviors controlled by zfs_deadman_failmode.
303 */
304 unsigned long zfs_deadman_synctime_ms = 600000ULL;
305
306 /*
307 * This value controls the maximum amount of time zio_wait() will block for an
308 * outstanding IO. By default this is 300 seconds at which point the "hung"
309 * behavior will be applied as described for zfs_deadman_synctime_ms.
310 */
311 unsigned long zfs_deadman_ziotime_ms = 300000ULL;
312
313 /*
314 * Check time in milliseconds. This defines the frequency at which we check
315 * for hung I/O.
316 */
317 unsigned long zfs_deadman_checktime_ms = 60000ULL;
318
319 /*
320 * By default the deadman is enabled.
321 */
322 int zfs_deadman_enabled = 1;
323
324 /*
325 * Controls the behavior of the deadman when it detects a "hung" I/O.
326 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
327 *
328 * wait - Wait for the "hung" I/O (default)
329 * continue - Attempt to recover from a "hung" I/O
330 * panic - Panic the system
331 */
332 char *zfs_deadman_failmode = "wait";
333
334 /*
335 * The worst case is single-sector max-parity RAID-Z blocks, in which
336 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
337 * times the size; so just assume that. Add to this the fact that
338 * we can have up to 3 DVAs per bp, and one more factor of 2 because
339 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
340 * the worst case is:
341 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
342 */
343 int spa_asize_inflation = 24;
344
345 /*
346 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
347 * the pool to be consumed. This ensures that we don't run the pool
348 * completely out of space, due to unaccounted changes (e.g. to the MOS).
349 * It also limits the worst-case time to allocate space. If we have
350 * less than this amount of free space, most ZPL operations (e.g. write,
351 * create) will return ENOSPC.
352 *
353 * Certain operations (e.g. file removal, most administrative actions) can
354 * use half the slop space. They will only return ENOSPC if less than half
355 * the slop space is free. Typically, once the pool has less than the slop
356 * space free, the user will use these operations to free up space in the pool.
357 * These are the operations that call dsl_pool_adjustedsize() with the netfree
358 * argument set to TRUE.
359 *
360 * A very restricted set of operations are always permitted, regardless of
361 * the amount of free space. These are the operations that call
362 * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy". If these
363 * operations result in a net increase in the amount of space used,
364 * it is possible to run the pool completely out of space, causing it to
365 * be permanently read-only.
366 *
367 * Note that on very small pools, the slop space will be larger than
368 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
369 * but we never allow it to be more than half the pool size.
370 *
371 * See also the comments in zfs_space_check_t.
372 */
373 int spa_slop_shift = 5;
374 uint64_t spa_min_slop = 128 * 1024 * 1024;
375
376 /*
377 * ==========================================================================
378 * SPA config locking
379 * ==========================================================================
380 */
381 static void
382 spa_config_lock_init(spa_t *spa)
383 {
384 for (int i = 0; i < SCL_LOCKS; i++) {
385 spa_config_lock_t *scl = &spa->spa_config_lock[i];
386 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
387 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
388 refcount_create_untracked(&scl->scl_count);
389 scl->scl_writer = NULL;
390 scl->scl_write_wanted = 0;
391 }
392 }
393
394 static void
395 spa_config_lock_destroy(spa_t *spa)
396 {
397 for (int i = 0; i < SCL_LOCKS; i++) {
398 spa_config_lock_t *scl = &spa->spa_config_lock[i];
399 mutex_destroy(&scl->scl_lock);
400 cv_destroy(&scl->scl_cv);
401 refcount_destroy(&scl->scl_count);
402 ASSERT(scl->scl_writer == NULL);
403 ASSERT(scl->scl_write_wanted == 0);
404 }
405 }
406
407 int
408 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
409 {
410 for (int i = 0; i < SCL_LOCKS; i++) {
411 spa_config_lock_t *scl = &spa->spa_config_lock[i];
412 if (!(locks & (1 << i)))
413 continue;
414 mutex_enter(&scl->scl_lock);
415 if (rw == RW_READER) {
416 if (scl->scl_writer || scl->scl_write_wanted) {
417 mutex_exit(&scl->scl_lock);
418 spa_config_exit(spa, locks & ((1 << i) - 1),
419 tag);
420 return (0);
421 }
422 } else {
423 ASSERT(scl->scl_writer != curthread);
424 if (!refcount_is_zero(&scl->scl_count)) {
425 mutex_exit(&scl->scl_lock);
426 spa_config_exit(spa, locks & ((1 << i) - 1),
427 tag);
428 return (0);
429 }
430 scl->scl_writer = curthread;
431 }
432 (void) refcount_add(&scl->scl_count, tag);
433 mutex_exit(&scl->scl_lock);
434 }
435 return (1);
436 }
437
438 void
439 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
440 {
441 int wlocks_held = 0;
442
443 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
444
445 for (int i = 0; i < SCL_LOCKS; i++) {
446 spa_config_lock_t *scl = &spa->spa_config_lock[i];
447 if (scl->scl_writer == curthread)
448 wlocks_held |= (1 << i);
449 if (!(locks & (1 << i)))
450 continue;
451 mutex_enter(&scl->scl_lock);
452 if (rw == RW_READER) {
453 while (scl->scl_writer || scl->scl_write_wanted) {
454 cv_wait(&scl->scl_cv, &scl->scl_lock);
455 }
456 } else {
457 ASSERT(scl->scl_writer != curthread);
458 while (!refcount_is_zero(&scl->scl_count)) {
459 scl->scl_write_wanted++;
460 cv_wait(&scl->scl_cv, &scl->scl_lock);
461 scl->scl_write_wanted--;
462 }
463 scl->scl_writer = curthread;
464 }
465 (void) refcount_add(&scl->scl_count, tag);
466 mutex_exit(&scl->scl_lock);
467 }
468 ASSERT3U(wlocks_held, <=, locks);
469 }
470
471 void
472 spa_config_exit(spa_t *spa, int locks, void *tag)
473 {
474 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
475 spa_config_lock_t *scl = &spa->spa_config_lock[i];
476 if (!(locks & (1 << i)))
477 continue;
478 mutex_enter(&scl->scl_lock);
479 ASSERT(!refcount_is_zero(&scl->scl_count));
480 if (refcount_remove(&scl->scl_count, tag) == 0) {
481 ASSERT(scl->scl_writer == NULL ||
482 scl->scl_writer == curthread);
483 scl->scl_writer = NULL; /* OK in either case */
484 cv_broadcast(&scl->scl_cv);
485 }
486 mutex_exit(&scl->scl_lock);
487 }
488 }
489
490 int
491 spa_config_held(spa_t *spa, int locks, krw_t rw)
492 {
493 int locks_held = 0;
494
495 for (int i = 0; i < SCL_LOCKS; i++) {
496 spa_config_lock_t *scl = &spa->spa_config_lock[i];
497 if (!(locks & (1 << i)))
498 continue;
499 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
500 (rw == RW_WRITER && scl->scl_writer == curthread))
501 locks_held |= 1 << i;
502 }
503
504 return (locks_held);
505 }
506
507 /*
508 * ==========================================================================
509 * SPA namespace functions
510 * ==========================================================================
511 */
512
513 /*
514 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
515 * Returns NULL if no matching spa_t is found.
516 */
517 spa_t *
518 spa_lookup(const char *name)
519 {
520 static spa_t search; /* spa_t is large; don't allocate on stack */
521 spa_t *spa;
522 avl_index_t where;
523 char *cp;
524
525 ASSERT(MUTEX_HELD(&spa_namespace_lock));
526
527 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
528
529 /*
530 * If it's a full dataset name, figure out the pool name and
531 * just use that.
532 */
533 cp = strpbrk(search.spa_name, "/@#");
534 if (cp != NULL)
535 *cp = '\0';
536
537 spa = avl_find(&spa_namespace_avl, &search, &where);
538
539 return (spa);
540 }
541
542 /*
543 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
544 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
545 * looking for potentially hung I/Os.
546 */
547 void
548 spa_deadman(void *arg)
549 {
550 spa_t *spa = arg;
551
552 /* Disable the deadman if the pool is suspended. */
553 if (spa_suspended(spa))
554 return;
555
556 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
557 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
558 ++spa->spa_deadman_calls);
559 if (zfs_deadman_enabled)
560 vdev_deadman(spa->spa_root_vdev, FTAG);
561
562 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
563 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
564 MSEC_TO_TICK(zfs_deadman_checktime_ms));
565 }
566
567 /*
568 * Create an uninitialized spa_t with the given name. Requires
569 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
570 * exist by calling spa_lookup() first.
571 */
572 spa_t *
573 spa_add(const char *name, nvlist_t *config, const char *altroot)
574 {
575 spa_t *spa;
576 spa_config_dirent_t *dp;
577
578 ASSERT(MUTEX_HELD(&spa_namespace_lock));
579
580 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
581
582 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
583 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
584 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
585 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
586 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
587 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
588 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
589 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
590 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
591 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
592 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
593 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
594 mutex_init(&spa->spa_alloc_lock, NULL, MUTEX_DEFAULT, NULL);
595
596 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
597 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
598 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
599 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
600 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
601
602 for (int t = 0; t < TXG_SIZE; t++)
603 bplist_create(&spa->spa_free_bplist[t]);
604
605 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
606 spa->spa_state = POOL_STATE_UNINITIALIZED;
607 spa->spa_freeze_txg = UINT64_MAX;
608 spa->spa_final_txg = UINT64_MAX;
609 spa->spa_load_max_txg = UINT64_MAX;
610 spa->spa_proc = &p0;
611 spa->spa_proc_state = SPA_PROC_NONE;
612
613 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
614 spa->spa_deadman_ziotime = MSEC2NSEC(zfs_deadman_ziotime_ms);
615 spa_set_deadman_failmode(spa, zfs_deadman_failmode);
616
617 refcount_create(&spa->spa_refcount);
618 spa_config_lock_init(spa);
619 spa_stats_init(spa);
620
621 avl_add(&spa_namespace_avl, spa);
622
623 /*
624 * Set the alternate root, if there is one.
625 */
626 if (altroot)
627 spa->spa_root = spa_strdup(altroot);
628
629 avl_create(&spa->spa_alloc_tree, zio_bookmark_compare,
630 sizeof (zio_t), offsetof(zio_t, io_alloc_node));
631
632 /*
633 * Every pool starts with the default cachefile
634 */
635 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
636 offsetof(spa_config_dirent_t, scd_link));
637
638 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
639 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
640 list_insert_head(&spa->spa_config_list, dp);
641
642 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
643 KM_SLEEP) == 0);
644
645 if (config != NULL) {
646 nvlist_t *features;
647
648 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
649 &features) == 0) {
650 VERIFY(nvlist_dup(features, &spa->spa_label_features,
651 0) == 0);
652 }
653
654 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
655 }
656
657 if (spa->spa_label_features == NULL) {
658 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
659 KM_SLEEP) == 0);
660 }
661
662 spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
663
664 spa->spa_min_ashift = INT_MAX;
665 spa->spa_max_ashift = 0;
666
667 /* Reset cached value */
668 spa->spa_dedup_dspace = ~0ULL;
669
670 /*
671 * As a pool is being created, treat all features as disabled by
672 * setting SPA_FEATURE_DISABLED for all entries in the feature
673 * refcount cache.
674 */
675 for (int i = 0; i < SPA_FEATURES; i++) {
676 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
677 }
678
679 return (spa);
680 }
681
682 /*
683 * Removes a spa_t from the namespace, freeing up any memory used. Requires
684 * spa_namespace_lock. This is called only after the spa_t has been closed and
685 * deactivated.
686 */
687 void
688 spa_remove(spa_t *spa)
689 {
690 spa_config_dirent_t *dp;
691
692 ASSERT(MUTEX_HELD(&spa_namespace_lock));
693 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
694 ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0);
695
696 nvlist_free(spa->spa_config_splitting);
697
698 avl_remove(&spa_namespace_avl, spa);
699 cv_broadcast(&spa_namespace_cv);
700
701 if (spa->spa_root)
702 spa_strfree(spa->spa_root);
703
704 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
705 list_remove(&spa->spa_config_list, dp);
706 if (dp->scd_path != NULL)
707 spa_strfree(dp->scd_path);
708 kmem_free(dp, sizeof (spa_config_dirent_t));
709 }
710
711 avl_destroy(&spa->spa_alloc_tree);
712 list_destroy(&spa->spa_config_list);
713
714 nvlist_free(spa->spa_label_features);
715 nvlist_free(spa->spa_load_info);
716 nvlist_free(spa->spa_feat_stats);
717 spa_config_set(spa, NULL);
718
719 refcount_destroy(&spa->spa_refcount);
720
721 spa_stats_destroy(spa);
722 spa_config_lock_destroy(spa);
723
724 for (int t = 0; t < TXG_SIZE; t++)
725 bplist_destroy(&spa->spa_free_bplist[t]);
726
727 zio_checksum_templates_free(spa);
728
729 cv_destroy(&spa->spa_async_cv);
730 cv_destroy(&spa->spa_evicting_os_cv);
731 cv_destroy(&spa->spa_proc_cv);
732 cv_destroy(&spa->spa_scrub_io_cv);
733 cv_destroy(&spa->spa_suspend_cv);
734
735 mutex_destroy(&spa->spa_alloc_lock);
736 mutex_destroy(&spa->spa_async_lock);
737 mutex_destroy(&spa->spa_errlist_lock);
738 mutex_destroy(&spa->spa_errlog_lock);
739 mutex_destroy(&spa->spa_evicting_os_lock);
740 mutex_destroy(&spa->spa_history_lock);
741 mutex_destroy(&spa->spa_proc_lock);
742 mutex_destroy(&spa->spa_props_lock);
743 mutex_destroy(&spa->spa_cksum_tmpls_lock);
744 mutex_destroy(&spa->spa_scrub_lock);
745 mutex_destroy(&spa->spa_suspend_lock);
746 mutex_destroy(&spa->spa_vdev_top_lock);
747 mutex_destroy(&spa->spa_feat_stats_lock);
748
749 kmem_free(spa, sizeof (spa_t));
750 }
751
752 /*
753 * Given a pool, return the next pool in the namespace, or NULL if there is
754 * none. If 'prev' is NULL, return the first pool.
755 */
756 spa_t *
757 spa_next(spa_t *prev)
758 {
759 ASSERT(MUTEX_HELD(&spa_namespace_lock));
760
761 if (prev)
762 return (AVL_NEXT(&spa_namespace_avl, prev));
763 else
764 return (avl_first(&spa_namespace_avl));
765 }
766
767 /*
768 * ==========================================================================
769 * SPA refcount functions
770 * ==========================================================================
771 */
772
773 /*
774 * Add a reference to the given spa_t. Must have at least one reference, or
775 * have the namespace lock held.
776 */
777 void
778 spa_open_ref(spa_t *spa, void *tag)
779 {
780 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
781 MUTEX_HELD(&spa_namespace_lock));
782 (void) refcount_add(&spa->spa_refcount, tag);
783 }
784
785 /*
786 * Remove a reference to the given spa_t. Must have at least one reference, or
787 * have the namespace lock held.
788 */
789 void
790 spa_close(spa_t *spa, void *tag)
791 {
792 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
793 MUTEX_HELD(&spa_namespace_lock));
794 (void) refcount_remove(&spa->spa_refcount, tag);
795 }
796
797 /*
798 * Remove a reference to the given spa_t held by a dsl dir that is
799 * being asynchronously released. Async releases occur from a taskq
800 * performing eviction of dsl datasets and dirs. The namespace lock
801 * isn't held and the hold by the object being evicted may contribute to
802 * spa_minref (e.g. dataset or directory released during pool export),
803 * so the asserts in spa_close() do not apply.
804 */
805 void
806 spa_async_close(spa_t *spa, void *tag)
807 {
808 (void) refcount_remove(&spa->spa_refcount, tag);
809 }
810
811 /*
812 * Check to see if the spa refcount is zero. Must be called with
813 * spa_namespace_lock held. We really compare against spa_minref, which is the
814 * number of references acquired when opening a pool
815 */
816 boolean_t
817 spa_refcount_zero(spa_t *spa)
818 {
819 ASSERT(MUTEX_HELD(&spa_namespace_lock));
820
821 return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
822 }
823
824 /*
825 * ==========================================================================
826 * SPA spare and l2cache tracking
827 * ==========================================================================
828 */
829
830 /*
831 * Hot spares and cache devices are tracked using the same code below,
832 * for 'auxiliary' devices.
833 */
834
835 typedef struct spa_aux {
836 uint64_t aux_guid;
837 uint64_t aux_pool;
838 avl_node_t aux_avl;
839 int aux_count;
840 } spa_aux_t;
841
842 static inline int
843 spa_aux_compare(const void *a, const void *b)
844 {
845 const spa_aux_t *sa = (const spa_aux_t *)a;
846 const spa_aux_t *sb = (const spa_aux_t *)b;
847
848 return (AVL_CMP(sa->aux_guid, sb->aux_guid));
849 }
850
851 void
852 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
853 {
854 avl_index_t where;
855 spa_aux_t search;
856 spa_aux_t *aux;
857
858 search.aux_guid = vd->vdev_guid;
859 if ((aux = avl_find(avl, &search, &where)) != NULL) {
860 aux->aux_count++;
861 } else {
862 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
863 aux->aux_guid = vd->vdev_guid;
864 aux->aux_count = 1;
865 avl_insert(avl, aux, where);
866 }
867 }
868
869 void
870 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
871 {
872 spa_aux_t search;
873 spa_aux_t *aux;
874 avl_index_t where;
875
876 search.aux_guid = vd->vdev_guid;
877 aux = avl_find(avl, &search, &where);
878
879 ASSERT(aux != NULL);
880
881 if (--aux->aux_count == 0) {
882 avl_remove(avl, aux);
883 kmem_free(aux, sizeof (spa_aux_t));
884 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
885 aux->aux_pool = 0ULL;
886 }
887 }
888
889 boolean_t
890 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
891 {
892 spa_aux_t search, *found;
893
894 search.aux_guid = guid;
895 found = avl_find(avl, &search, NULL);
896
897 if (pool) {
898 if (found)
899 *pool = found->aux_pool;
900 else
901 *pool = 0ULL;
902 }
903
904 if (refcnt) {
905 if (found)
906 *refcnt = found->aux_count;
907 else
908 *refcnt = 0;
909 }
910
911 return (found != NULL);
912 }
913
914 void
915 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
916 {
917 spa_aux_t search, *found;
918 avl_index_t where;
919
920 search.aux_guid = vd->vdev_guid;
921 found = avl_find(avl, &search, &where);
922 ASSERT(found != NULL);
923 ASSERT(found->aux_pool == 0ULL);
924
925 found->aux_pool = spa_guid(vd->vdev_spa);
926 }
927
928 /*
929 * Spares are tracked globally due to the following constraints:
930 *
931 * - A spare may be part of multiple pools.
932 * - A spare may be added to a pool even if it's actively in use within
933 * another pool.
934 * - A spare in use in any pool can only be the source of a replacement if
935 * the target is a spare in the same pool.
936 *
937 * We keep track of all spares on the system through the use of a reference
938 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
939 * spare, then we bump the reference count in the AVL tree. In addition, we set
940 * the 'vdev_isspare' member to indicate that the device is a spare (active or
941 * inactive). When a spare is made active (used to replace a device in the
942 * pool), we also keep track of which pool its been made a part of.
943 *
944 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
945 * called under the spa_namespace lock as part of vdev reconfiguration. The
946 * separate spare lock exists for the status query path, which does not need to
947 * be completely consistent with respect to other vdev configuration changes.
948 */
949
950 static int
951 spa_spare_compare(const void *a, const void *b)
952 {
953 return (spa_aux_compare(a, b));
954 }
955
956 void
957 spa_spare_add(vdev_t *vd)
958 {
959 mutex_enter(&spa_spare_lock);
960 ASSERT(!vd->vdev_isspare);
961 spa_aux_add(vd, &spa_spare_avl);
962 vd->vdev_isspare = B_TRUE;
963 mutex_exit(&spa_spare_lock);
964 }
965
966 void
967 spa_spare_remove(vdev_t *vd)
968 {
969 mutex_enter(&spa_spare_lock);
970 ASSERT(vd->vdev_isspare);
971 spa_aux_remove(vd, &spa_spare_avl);
972 vd->vdev_isspare = B_FALSE;
973 mutex_exit(&spa_spare_lock);
974 }
975
976 boolean_t
977 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
978 {
979 boolean_t found;
980
981 mutex_enter(&spa_spare_lock);
982 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
983 mutex_exit(&spa_spare_lock);
984
985 return (found);
986 }
987
988 void
989 spa_spare_activate(vdev_t *vd)
990 {
991 mutex_enter(&spa_spare_lock);
992 ASSERT(vd->vdev_isspare);
993 spa_aux_activate(vd, &spa_spare_avl);
994 mutex_exit(&spa_spare_lock);
995 }
996
997 /*
998 * Level 2 ARC devices are tracked globally for the same reasons as spares.
999 * Cache devices currently only support one pool per cache device, and so
1000 * for these devices the aux reference count is currently unused beyond 1.
1001 */
1002
1003 static int
1004 spa_l2cache_compare(const void *a, const void *b)
1005 {
1006 return (spa_aux_compare(a, b));
1007 }
1008
1009 void
1010 spa_l2cache_add(vdev_t *vd)
1011 {
1012 mutex_enter(&spa_l2cache_lock);
1013 ASSERT(!vd->vdev_isl2cache);
1014 spa_aux_add(vd, &spa_l2cache_avl);
1015 vd->vdev_isl2cache = B_TRUE;
1016 mutex_exit(&spa_l2cache_lock);
1017 }
1018
1019 void
1020 spa_l2cache_remove(vdev_t *vd)
1021 {
1022 mutex_enter(&spa_l2cache_lock);
1023 ASSERT(vd->vdev_isl2cache);
1024 spa_aux_remove(vd, &spa_l2cache_avl);
1025 vd->vdev_isl2cache = B_FALSE;
1026 mutex_exit(&spa_l2cache_lock);
1027 }
1028
1029 boolean_t
1030 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1031 {
1032 boolean_t found;
1033
1034 mutex_enter(&spa_l2cache_lock);
1035 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1036 mutex_exit(&spa_l2cache_lock);
1037
1038 return (found);
1039 }
1040
1041 void
1042 spa_l2cache_activate(vdev_t *vd)
1043 {
1044 mutex_enter(&spa_l2cache_lock);
1045 ASSERT(vd->vdev_isl2cache);
1046 spa_aux_activate(vd, &spa_l2cache_avl);
1047 mutex_exit(&spa_l2cache_lock);
1048 }
1049
1050 /*
1051 * ==========================================================================
1052 * SPA vdev locking
1053 * ==========================================================================
1054 */
1055
1056 /*
1057 * Lock the given spa_t for the purpose of adding or removing a vdev.
1058 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1059 * It returns the next transaction group for the spa_t.
1060 */
1061 uint64_t
1062 spa_vdev_enter(spa_t *spa)
1063 {
1064 mutex_enter(&spa->spa_vdev_top_lock);
1065 mutex_enter(&spa_namespace_lock);
1066 return (spa_vdev_config_enter(spa));
1067 }
1068
1069 /*
1070 * Internal implementation for spa_vdev_enter(). Used when a vdev
1071 * operation requires multiple syncs (i.e. removing a device) while
1072 * keeping the spa_namespace_lock held.
1073 */
1074 uint64_t
1075 spa_vdev_config_enter(spa_t *spa)
1076 {
1077 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1078
1079 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1080
1081 return (spa_last_synced_txg(spa) + 1);
1082 }
1083
1084 /*
1085 * Used in combination with spa_vdev_config_enter() to allow the syncing
1086 * of multiple transactions without releasing the spa_namespace_lock.
1087 */
1088 void
1089 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1090 {
1091 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1092
1093 int config_changed = B_FALSE;
1094
1095 ASSERT(txg > spa_last_synced_txg(spa));
1096
1097 spa->spa_pending_vdev = NULL;
1098
1099 /*
1100 * Reassess the DTLs.
1101 */
1102 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1103
1104 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1105 config_changed = B_TRUE;
1106 spa->spa_config_generation++;
1107 }
1108
1109 /*
1110 * Verify the metaslab classes.
1111 */
1112 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1113 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1114
1115 spa_config_exit(spa, SCL_ALL, spa);
1116
1117 /*
1118 * Panic the system if the specified tag requires it. This
1119 * is useful for ensuring that configurations are updated
1120 * transactionally.
1121 */
1122 if (zio_injection_enabled)
1123 zio_handle_panic_injection(spa, tag, 0);
1124
1125 /*
1126 * Note: this txg_wait_synced() is important because it ensures
1127 * that there won't be more than one config change per txg.
1128 * This allows us to use the txg as the generation number.
1129 */
1130 if (error == 0)
1131 txg_wait_synced(spa->spa_dsl_pool, txg);
1132
1133 if (vd != NULL) {
1134 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1135 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1136 vdev_free(vd);
1137 spa_config_exit(spa, SCL_ALL, spa);
1138 }
1139
1140 /*
1141 * If the config changed, update the config cache.
1142 */
1143 if (config_changed)
1144 spa_write_cachefile(spa, B_FALSE, B_TRUE);
1145 }
1146
1147 /*
1148 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1149 * locking of spa_vdev_enter(), we also want make sure the transactions have
1150 * synced to disk, and then update the global configuration cache with the new
1151 * information.
1152 */
1153 int
1154 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1155 {
1156 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1157 mutex_exit(&spa_namespace_lock);
1158 mutex_exit(&spa->spa_vdev_top_lock);
1159
1160 return (error);
1161 }
1162
1163 /*
1164 * Lock the given spa_t for the purpose of changing vdev state.
1165 */
1166 void
1167 spa_vdev_state_enter(spa_t *spa, int oplocks)
1168 {
1169 int locks = SCL_STATE_ALL | oplocks;
1170
1171 /*
1172 * Root pools may need to read of the underlying devfs filesystem
1173 * when opening up a vdev. Unfortunately if we're holding the
1174 * SCL_ZIO lock it will result in a deadlock when we try to issue
1175 * the read from the root filesystem. Instead we "prefetch"
1176 * the associated vnodes that we need prior to opening the
1177 * underlying devices and cache them so that we can prevent
1178 * any I/O when we are doing the actual open.
1179 */
1180 if (spa_is_root(spa)) {
1181 int low = locks & ~(SCL_ZIO - 1);
1182 int high = locks & ~low;
1183
1184 spa_config_enter(spa, high, spa, RW_WRITER);
1185 vdev_hold(spa->spa_root_vdev);
1186 spa_config_enter(spa, low, spa, RW_WRITER);
1187 } else {
1188 spa_config_enter(spa, locks, spa, RW_WRITER);
1189 }
1190 spa->spa_vdev_locks = locks;
1191 }
1192
1193 int
1194 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1195 {
1196 boolean_t config_changed = B_FALSE;
1197 vdev_t *vdev_top;
1198
1199 if (vd == NULL || vd == spa->spa_root_vdev) {
1200 vdev_top = spa->spa_root_vdev;
1201 } else {
1202 vdev_top = vd->vdev_top;
1203 }
1204
1205 if (vd != NULL || error == 0)
1206 vdev_dtl_reassess(vdev_top, 0, 0, B_FALSE);
1207
1208 if (vd != NULL) {
1209 if (vd != spa->spa_root_vdev)
1210 vdev_state_dirty(vdev_top);
1211
1212 config_changed = B_TRUE;
1213 spa->spa_config_generation++;
1214 }
1215
1216 if (spa_is_root(spa))
1217 vdev_rele(spa->spa_root_vdev);
1218
1219 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1220 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1221
1222 /*
1223 * If anything changed, wait for it to sync. This ensures that,
1224 * from the system administrator's perspective, zpool(1M) commands
1225 * are synchronous. This is important for things like zpool offline:
1226 * when the command completes, you expect no further I/O from ZFS.
1227 */
1228 if (vd != NULL)
1229 txg_wait_synced(spa->spa_dsl_pool, 0);
1230
1231 /*
1232 * If the config changed, update the config cache.
1233 */
1234 if (config_changed) {
1235 mutex_enter(&spa_namespace_lock);
1236 spa_write_cachefile(spa, B_FALSE, B_TRUE);
1237 mutex_exit(&spa_namespace_lock);
1238 }
1239
1240 return (error);
1241 }
1242
1243 /*
1244 * ==========================================================================
1245 * Miscellaneous functions
1246 * ==========================================================================
1247 */
1248
1249 void
1250 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1251 {
1252 if (!nvlist_exists(spa->spa_label_features, feature)) {
1253 fnvlist_add_boolean(spa->spa_label_features, feature);
1254 /*
1255 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1256 * dirty the vdev config because lock SCL_CONFIG is not held.
1257 * Thankfully, in this case we don't need to dirty the config
1258 * because it will be written out anyway when we finish
1259 * creating the pool.
1260 */
1261 if (tx->tx_txg != TXG_INITIAL)
1262 vdev_config_dirty(spa->spa_root_vdev);
1263 }
1264 }
1265
1266 void
1267 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1268 {
1269 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1270 vdev_config_dirty(spa->spa_root_vdev);
1271 }
1272
1273 /*
1274 * Rename a spa_t.
1275 */
1276 int
1277 spa_rename(const char *name, const char *newname)
1278 {
1279 spa_t *spa;
1280 int err;
1281
1282 /*
1283 * Lookup the spa_t and grab the config lock for writing. We need to
1284 * actually open the pool so that we can sync out the necessary labels.
1285 * It's OK to call spa_open() with the namespace lock held because we
1286 * allow recursive calls for other reasons.
1287 */
1288 mutex_enter(&spa_namespace_lock);
1289 if ((err = spa_open(name, &spa, FTAG)) != 0) {
1290 mutex_exit(&spa_namespace_lock);
1291 return (err);
1292 }
1293
1294 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1295
1296 avl_remove(&spa_namespace_avl, spa);
1297 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1298 avl_add(&spa_namespace_avl, spa);
1299
1300 /*
1301 * Sync all labels to disk with the new names by marking the root vdev
1302 * dirty and waiting for it to sync. It will pick up the new pool name
1303 * during the sync.
1304 */
1305 vdev_config_dirty(spa->spa_root_vdev);
1306
1307 spa_config_exit(spa, SCL_ALL, FTAG);
1308
1309 txg_wait_synced(spa->spa_dsl_pool, 0);
1310
1311 /*
1312 * Sync the updated config cache.
1313 */
1314 spa_write_cachefile(spa, B_FALSE, B_TRUE);
1315
1316 spa_close(spa, FTAG);
1317
1318 mutex_exit(&spa_namespace_lock);
1319
1320 return (0);
1321 }
1322
1323 /*
1324 * Return the spa_t associated with given pool_guid, if it exists. If
1325 * device_guid is non-zero, determine whether the pool exists *and* contains
1326 * a device with the specified device_guid.
1327 */
1328 spa_t *
1329 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1330 {
1331 spa_t *spa;
1332 avl_tree_t *t = &spa_namespace_avl;
1333
1334 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1335
1336 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1337 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1338 continue;
1339 if (spa->spa_root_vdev == NULL)
1340 continue;
1341 if (spa_guid(spa) == pool_guid) {
1342 if (device_guid == 0)
1343 break;
1344
1345 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1346 device_guid) != NULL)
1347 break;
1348
1349 /*
1350 * Check any devices we may be in the process of adding.
1351 */
1352 if (spa->spa_pending_vdev) {
1353 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1354 device_guid) != NULL)
1355 break;
1356 }
1357 }
1358 }
1359
1360 return (spa);
1361 }
1362
1363 /*
1364 * Determine whether a pool with the given pool_guid exists.
1365 */
1366 boolean_t
1367 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1368 {
1369 return (spa_by_guid(pool_guid, device_guid) != NULL);
1370 }
1371
1372 char *
1373 spa_strdup(const char *s)
1374 {
1375 size_t len;
1376 char *new;
1377
1378 len = strlen(s);
1379 new = kmem_alloc(len + 1, KM_SLEEP);
1380 bcopy(s, new, len);
1381 new[len] = '\0';
1382
1383 return (new);
1384 }
1385
1386 void
1387 spa_strfree(char *s)
1388 {
1389 kmem_free(s, strlen(s) + 1);
1390 }
1391
1392 uint64_t
1393 spa_get_random(uint64_t range)
1394 {
1395 uint64_t r;
1396
1397 ASSERT(range != 0);
1398
1399 if (range == 1)
1400 return (0);
1401
1402 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1403
1404 return (r % range);
1405 }
1406
1407 uint64_t
1408 spa_generate_guid(spa_t *spa)
1409 {
1410 uint64_t guid = spa_get_random(-1ULL);
1411
1412 if (spa != NULL) {
1413 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1414 guid = spa_get_random(-1ULL);
1415 } else {
1416 while (guid == 0 || spa_guid_exists(guid, 0))
1417 guid = spa_get_random(-1ULL);
1418 }
1419
1420 return (guid);
1421 }
1422
1423 void
1424 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1425 {
1426 char type[256];
1427 char *checksum = NULL;
1428 char *compress = NULL;
1429
1430 if (bp != NULL) {
1431 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1432 dmu_object_byteswap_t bswap =
1433 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1434 (void) snprintf(type, sizeof (type), "bswap %s %s",
1435 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1436 "metadata" : "data",
1437 dmu_ot_byteswap[bswap].ob_name);
1438 } else {
1439 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1440 sizeof (type));
1441 }
1442 if (!BP_IS_EMBEDDED(bp)) {
1443 checksum =
1444 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1445 }
1446 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1447 }
1448
1449 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1450 compress);
1451 }
1452
1453 void
1454 spa_freeze(spa_t *spa)
1455 {
1456 uint64_t freeze_txg = 0;
1457
1458 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1459 if (spa->spa_freeze_txg == UINT64_MAX) {
1460 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1461 spa->spa_freeze_txg = freeze_txg;
1462 }
1463 spa_config_exit(spa, SCL_ALL, FTAG);
1464 if (freeze_txg != 0)
1465 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1466 }
1467
1468 void
1469 zfs_panic_recover(const char *fmt, ...)
1470 {
1471 va_list adx;
1472
1473 va_start(adx, fmt);
1474 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1475 va_end(adx);
1476 }
1477
1478 /*
1479 * This is a stripped-down version of strtoull, suitable only for converting
1480 * lowercase hexadecimal numbers that don't overflow.
1481 */
1482 uint64_t
1483 zfs_strtonum(const char *str, char **nptr)
1484 {
1485 uint64_t val = 0;
1486 char c;
1487 int digit;
1488
1489 while ((c = *str) != '\0') {
1490 if (c >= '0' && c <= '9')
1491 digit = c - '0';
1492 else if (c >= 'a' && c <= 'f')
1493 digit = 10 + c - 'a';
1494 else
1495 break;
1496
1497 val *= 16;
1498 val += digit;
1499
1500 str++;
1501 }
1502
1503 if (nptr)
1504 *nptr = (char *)str;
1505
1506 return (val);
1507 }
1508
1509 /*
1510 * ==========================================================================
1511 * Accessor functions
1512 * ==========================================================================
1513 */
1514
1515 boolean_t
1516 spa_shutting_down(spa_t *spa)
1517 {
1518 return (spa->spa_async_suspended);
1519 }
1520
1521 dsl_pool_t *
1522 spa_get_dsl(spa_t *spa)
1523 {
1524 return (spa->spa_dsl_pool);
1525 }
1526
1527 boolean_t
1528 spa_is_initializing(spa_t *spa)
1529 {
1530 return (spa->spa_is_initializing);
1531 }
1532
1533 boolean_t
1534 spa_indirect_vdevs_loaded(spa_t *spa)
1535 {
1536 return (spa->spa_indirect_vdevs_loaded);
1537 }
1538
1539 blkptr_t *
1540 spa_get_rootblkptr(spa_t *spa)
1541 {
1542 return (&spa->spa_ubsync.ub_rootbp);
1543 }
1544
1545 void
1546 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1547 {
1548 spa->spa_uberblock.ub_rootbp = *bp;
1549 }
1550
1551 void
1552 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1553 {
1554 if (spa->spa_root == NULL)
1555 buf[0] = '\0';
1556 else
1557 (void) strncpy(buf, spa->spa_root, buflen);
1558 }
1559
1560 int
1561 spa_sync_pass(spa_t *spa)
1562 {
1563 return (spa->spa_sync_pass);
1564 }
1565
1566 char *
1567 spa_name(spa_t *spa)
1568 {
1569 return (spa->spa_name);
1570 }
1571
1572 uint64_t
1573 spa_guid(spa_t *spa)
1574 {
1575 dsl_pool_t *dp = spa_get_dsl(spa);
1576 uint64_t guid;
1577
1578 /*
1579 * If we fail to parse the config during spa_load(), we can go through
1580 * the error path (which posts an ereport) and end up here with no root
1581 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1582 * this case.
1583 */
1584 if (spa->spa_root_vdev == NULL)
1585 return (spa->spa_config_guid);
1586
1587 guid = spa->spa_last_synced_guid != 0 ?
1588 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1589
1590 /*
1591 * Return the most recently synced out guid unless we're
1592 * in syncing context.
1593 */
1594 if (dp && dsl_pool_sync_context(dp))
1595 return (spa->spa_root_vdev->vdev_guid);
1596 else
1597 return (guid);
1598 }
1599
1600 uint64_t
1601 spa_load_guid(spa_t *spa)
1602 {
1603 /*
1604 * This is a GUID that exists solely as a reference for the
1605 * purposes of the arc. It is generated at load time, and
1606 * is never written to persistent storage.
1607 */
1608 return (spa->spa_load_guid);
1609 }
1610
1611 uint64_t
1612 spa_last_synced_txg(spa_t *spa)
1613 {
1614 return (spa->spa_ubsync.ub_txg);
1615 }
1616
1617 uint64_t
1618 spa_first_txg(spa_t *spa)
1619 {
1620 return (spa->spa_first_txg);
1621 }
1622
1623 uint64_t
1624 spa_syncing_txg(spa_t *spa)
1625 {
1626 return (spa->spa_syncing_txg);
1627 }
1628
1629 /*
1630 * Return the last txg where data can be dirtied. The final txgs
1631 * will be used to just clear out any deferred frees that remain.
1632 */
1633 uint64_t
1634 spa_final_dirty_txg(spa_t *spa)
1635 {
1636 return (spa->spa_final_txg - TXG_DEFER_SIZE);
1637 }
1638
1639 pool_state_t
1640 spa_state(spa_t *spa)
1641 {
1642 return (spa->spa_state);
1643 }
1644
1645 spa_load_state_t
1646 spa_load_state(spa_t *spa)
1647 {
1648 return (spa->spa_load_state);
1649 }
1650
1651 uint64_t
1652 spa_freeze_txg(spa_t *spa)
1653 {
1654 return (spa->spa_freeze_txg);
1655 }
1656
1657 /*
1658 * Return the inflated asize for a logical write in bytes. This is used by the
1659 * DMU to calculate the space a logical write will require on disk.
1660 * If lsize is smaller than the largest physical block size allocatable on this
1661 * pool we use its value instead, since the write will end up using the whole
1662 * block anyway.
1663 */
1664 uint64_t
1665 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1666 {
1667 if (lsize == 0)
1668 return (0); /* No inflation needed */
1669 return (MAX(lsize, 1 << spa->spa_max_ashift) * spa_asize_inflation);
1670 }
1671
1672 /*
1673 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1674 * or at least 128MB, unless that would cause it to be more than half the
1675 * pool size.
1676 *
1677 * See the comment above spa_slop_shift for details.
1678 */
1679 uint64_t
1680 spa_get_slop_space(spa_t *spa)
1681 {
1682 uint64_t space = spa_get_dspace(spa);
1683 return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
1684 }
1685
1686 uint64_t
1687 spa_get_dspace(spa_t *spa)
1688 {
1689 return (spa->spa_dspace);
1690 }
1691
1692 void
1693 spa_update_dspace(spa_t *spa)
1694 {
1695 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1696 ddt_get_dedup_dspace(spa);
1697 if (spa->spa_vdev_removal != NULL) {
1698 /*
1699 * We can't allocate from the removing device, so
1700 * subtract its size. This prevents the DMU/DSL from
1701 * filling up the (now smaller) pool while we are in the
1702 * middle of removing the device.
1703 *
1704 * Note that the DMU/DSL doesn't actually know or care
1705 * how much space is allocated (it does its own tracking
1706 * of how much space has been logically used). So it
1707 * doesn't matter that the data we are moving may be
1708 * allocated twice (on the old device and the new
1709 * device).
1710 */
1711 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1712 vdev_t *vd =
1713 vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
1714 spa->spa_dspace -= spa_deflate(spa) ?
1715 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
1716 spa_config_exit(spa, SCL_VDEV, FTAG);
1717 }
1718 }
1719
1720 /*
1721 * Return the failure mode that has been set to this pool. The default
1722 * behavior will be to block all I/Os when a complete failure occurs.
1723 */
1724 uint64_t
1725 spa_get_failmode(spa_t *spa)
1726 {
1727 return (spa->spa_failmode);
1728 }
1729
1730 boolean_t
1731 spa_suspended(spa_t *spa)
1732 {
1733 return (spa->spa_suspended != ZIO_SUSPEND_NONE);
1734 }
1735
1736 uint64_t
1737 spa_version(spa_t *spa)
1738 {
1739 return (spa->spa_ubsync.ub_version);
1740 }
1741
1742 boolean_t
1743 spa_deflate(spa_t *spa)
1744 {
1745 return (spa->spa_deflate);
1746 }
1747
1748 metaslab_class_t *
1749 spa_normal_class(spa_t *spa)
1750 {
1751 return (spa->spa_normal_class);
1752 }
1753
1754 metaslab_class_t *
1755 spa_log_class(spa_t *spa)
1756 {
1757 return (spa->spa_log_class);
1758 }
1759
1760 void
1761 spa_evicting_os_register(spa_t *spa, objset_t *os)
1762 {
1763 mutex_enter(&spa->spa_evicting_os_lock);
1764 list_insert_head(&spa->spa_evicting_os_list, os);
1765 mutex_exit(&spa->spa_evicting_os_lock);
1766 }
1767
1768 void
1769 spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1770 {
1771 mutex_enter(&spa->spa_evicting_os_lock);
1772 list_remove(&spa->spa_evicting_os_list, os);
1773 cv_broadcast(&spa->spa_evicting_os_cv);
1774 mutex_exit(&spa->spa_evicting_os_lock);
1775 }
1776
1777 void
1778 spa_evicting_os_wait(spa_t *spa)
1779 {
1780 mutex_enter(&spa->spa_evicting_os_lock);
1781 while (!list_is_empty(&spa->spa_evicting_os_list))
1782 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1783 mutex_exit(&spa->spa_evicting_os_lock);
1784
1785 dmu_buf_user_evict_wait();
1786 }
1787
1788 int
1789 spa_max_replication(spa_t *spa)
1790 {
1791 /*
1792 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1793 * handle BPs with more than one DVA allocated. Set our max
1794 * replication level accordingly.
1795 */
1796 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1797 return (1);
1798 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1799 }
1800
1801 int
1802 spa_prev_software_version(spa_t *spa)
1803 {
1804 return (spa->spa_prev_software_version);
1805 }
1806
1807 uint64_t
1808 spa_deadman_synctime(spa_t *spa)
1809 {
1810 return (spa->spa_deadman_synctime);
1811 }
1812
1813 uint64_t
1814 spa_deadman_ziotime(spa_t *spa)
1815 {
1816 return (spa->spa_deadman_ziotime);
1817 }
1818
1819 uint64_t
1820 spa_get_deadman_failmode(spa_t *spa)
1821 {
1822 return (spa->spa_deadman_failmode);
1823 }
1824
1825 void
1826 spa_set_deadman_failmode(spa_t *spa, const char *failmode)
1827 {
1828 if (strcmp(failmode, "wait") == 0)
1829 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
1830 else if (strcmp(failmode, "continue") == 0)
1831 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_CONTINUE;
1832 else if (strcmp(failmode, "panic") == 0)
1833 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
1834 else
1835 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
1836 }
1837
1838 uint64_t
1839 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1840 {
1841 uint64_t asize = DVA_GET_ASIZE(dva);
1842 uint64_t dsize = asize;
1843
1844 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1845
1846 if (asize != 0 && spa->spa_deflate) {
1847 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1848 if (vd != NULL)
1849 dsize = (asize >> SPA_MINBLOCKSHIFT) *
1850 vd->vdev_deflate_ratio;
1851 }
1852
1853 return (dsize);
1854 }
1855
1856 uint64_t
1857 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1858 {
1859 uint64_t dsize = 0;
1860
1861 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1862 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1863
1864 return (dsize);
1865 }
1866
1867 uint64_t
1868 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1869 {
1870 uint64_t dsize = 0;
1871
1872 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1873
1874 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1875 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1876
1877 spa_config_exit(spa, SCL_VDEV, FTAG);
1878
1879 return (dsize);
1880 }
1881
1882 /*
1883 * ==========================================================================
1884 * Initialization and Termination
1885 * ==========================================================================
1886 */
1887
1888 static int
1889 spa_name_compare(const void *a1, const void *a2)
1890 {
1891 const spa_t *s1 = a1;
1892 const spa_t *s2 = a2;
1893 int s;
1894
1895 s = strcmp(s1->spa_name, s2->spa_name);
1896
1897 return (AVL_ISIGN(s));
1898 }
1899
1900 void
1901 spa_boot_init(void)
1902 {
1903 spa_config_load();
1904 }
1905
1906 void
1907 spa_init(int mode)
1908 {
1909 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1910 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1911 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1912 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1913
1914 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1915 offsetof(spa_t, spa_avl));
1916
1917 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1918 offsetof(spa_aux_t, aux_avl));
1919
1920 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1921 offsetof(spa_aux_t, aux_avl));
1922
1923 spa_mode_global = mode;
1924
1925 #ifndef _KERNEL
1926 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1927 struct sigaction sa;
1928
1929 sa.sa_flags = SA_SIGINFO;
1930 sigemptyset(&sa.sa_mask);
1931 sa.sa_sigaction = arc_buf_sigsegv;
1932
1933 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
1934 perror("could not enable watchpoints: "
1935 "sigaction(SIGSEGV, ...) = ");
1936 } else {
1937 arc_watch = B_TRUE;
1938 }
1939 }
1940 #endif
1941
1942 fm_init();
1943 refcount_init();
1944 unique_init();
1945 range_tree_init();
1946 metaslab_alloc_trace_init();
1947 ddt_init();
1948 zio_init();
1949 dmu_init();
1950 zil_init();
1951 vdev_cache_stat_init();
1952 vdev_mirror_stat_init();
1953 vdev_raidz_math_init();
1954 vdev_file_init();
1955 zfs_prop_init();
1956 zpool_prop_init();
1957 zpool_feature_init();
1958 spa_config_load();
1959 l2arc_start();
1960 scan_init();
1961 qat_init();
1962 }
1963
1964 void
1965 spa_fini(void)
1966 {
1967 l2arc_stop();
1968
1969 spa_evict_all();
1970
1971 vdev_file_fini();
1972 vdev_cache_stat_fini();
1973 vdev_mirror_stat_fini();
1974 vdev_raidz_math_fini();
1975 zil_fini();
1976 dmu_fini();
1977 zio_fini();
1978 ddt_fini();
1979 metaslab_alloc_trace_fini();
1980 range_tree_fini();
1981 unique_fini();
1982 refcount_fini();
1983 fm_fini();
1984 scan_fini();
1985 qat_fini();
1986
1987 avl_destroy(&spa_namespace_avl);
1988 avl_destroy(&spa_spare_avl);
1989 avl_destroy(&spa_l2cache_avl);
1990
1991 cv_destroy(&spa_namespace_cv);
1992 mutex_destroy(&spa_namespace_lock);
1993 mutex_destroy(&spa_spare_lock);
1994 mutex_destroy(&spa_l2cache_lock);
1995 }
1996
1997 /*
1998 * Return whether this pool has slogs. No locking needed.
1999 * It's not a problem if the wrong answer is returned as it's only for
2000 * performance and not correctness
2001 */
2002 boolean_t
2003 spa_has_slogs(spa_t *spa)
2004 {
2005 return (spa->spa_log_class->mc_rotor != NULL);
2006 }
2007
2008 spa_log_state_t
2009 spa_get_log_state(spa_t *spa)
2010 {
2011 return (spa->spa_log_state);
2012 }
2013
2014 void
2015 spa_set_log_state(spa_t *spa, spa_log_state_t state)
2016 {
2017 spa->spa_log_state = state;
2018 }
2019
2020 boolean_t
2021 spa_is_root(spa_t *spa)
2022 {
2023 return (spa->spa_is_root);
2024 }
2025
2026 boolean_t
2027 spa_writeable(spa_t *spa)
2028 {
2029 return (!!(spa->spa_mode & FWRITE));
2030 }
2031
2032 /*
2033 * Returns true if there is a pending sync task in any of the current
2034 * syncing txg, the current quiescing txg, or the current open txg.
2035 */
2036 boolean_t
2037 spa_has_pending_synctask(spa_t *spa)
2038 {
2039 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
2040 }
2041
2042 int
2043 spa_mode(spa_t *spa)
2044 {
2045 return (spa->spa_mode);
2046 }
2047
2048 uint64_t
2049 spa_bootfs(spa_t *spa)
2050 {
2051 return (spa->spa_bootfs);
2052 }
2053
2054 uint64_t
2055 spa_delegation(spa_t *spa)
2056 {
2057 return (spa->spa_delegation);
2058 }
2059
2060 objset_t *
2061 spa_meta_objset(spa_t *spa)
2062 {
2063 return (spa->spa_meta_objset);
2064 }
2065
2066 enum zio_checksum
2067 spa_dedup_checksum(spa_t *spa)
2068 {
2069 return (spa->spa_dedup_checksum);
2070 }
2071
2072 /*
2073 * Reset pool scan stat per scan pass (or reboot).
2074 */
2075 void
2076 spa_scan_stat_init(spa_t *spa)
2077 {
2078 /* data not stored on disk */
2079 spa->spa_scan_pass_start = gethrestime_sec();
2080 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
2081 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
2082 else
2083 spa->spa_scan_pass_scrub_pause = 0;
2084 spa->spa_scan_pass_scrub_spent_paused = 0;
2085 spa->spa_scan_pass_exam = 0;
2086 spa->spa_scan_pass_issued = 0;
2087 vdev_scan_stat_init(spa->spa_root_vdev);
2088 }
2089
2090 /*
2091 * Get scan stats for zpool status reports
2092 */
2093 int
2094 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2095 {
2096 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2097
2098 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2099 return (SET_ERROR(ENOENT));
2100 bzero(ps, sizeof (pool_scan_stat_t));
2101
2102 /* data stored on disk */
2103 ps->pss_func = scn->scn_phys.scn_func;
2104 ps->pss_state = scn->scn_phys.scn_state;
2105 ps->pss_start_time = scn->scn_phys.scn_start_time;
2106 ps->pss_end_time = scn->scn_phys.scn_end_time;
2107 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
2108 ps->pss_examined = scn->scn_phys.scn_examined;
2109 ps->pss_to_process = scn->scn_phys.scn_to_process;
2110 ps->pss_processed = scn->scn_phys.scn_processed;
2111 ps->pss_errors = scn->scn_phys.scn_errors;
2112
2113 /* data not stored on disk */
2114 ps->pss_pass_exam = spa->spa_scan_pass_exam;
2115 ps->pss_pass_start = spa->spa_scan_pass_start;
2116 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
2117 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
2118 ps->pss_pass_issued = spa->spa_scan_pass_issued;
2119 ps->pss_issued =
2120 scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
2121
2122 return (0);
2123 }
2124
2125 boolean_t
2126 spa_debug_enabled(spa_t *spa)
2127 {
2128 return (spa->spa_debug);
2129 }
2130
2131 int
2132 spa_maxblocksize(spa_t *spa)
2133 {
2134 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2135 return (SPA_MAXBLOCKSIZE);
2136 else
2137 return (SPA_OLD_MAXBLOCKSIZE);
2138 }
2139
2140
2141 /*
2142 * Returns the txg that the last device removal completed. No indirect mappings
2143 * have been added since this txg.
2144 */
2145 uint64_t
2146 spa_get_last_removal_txg(spa_t *spa)
2147 {
2148 uint64_t vdevid;
2149 uint64_t ret = -1ULL;
2150
2151 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2152 /*
2153 * sr_prev_indirect_vdev is only modified while holding all the
2154 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2155 * examining it.
2156 */
2157 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
2158
2159 while (vdevid != -1ULL) {
2160 vdev_t *vd = vdev_lookup_top(spa, vdevid);
2161 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
2162
2163 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
2164
2165 /*
2166 * If the removal did not remap any data, we don't care.
2167 */
2168 if (vdev_indirect_births_count(vib) != 0) {
2169 ret = vdev_indirect_births_last_entry_txg(vib);
2170 break;
2171 }
2172
2173 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
2174 }
2175 spa_config_exit(spa, SCL_VDEV, FTAG);
2176
2177 IMPLY(ret != -1ULL,
2178 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
2179
2180 return (ret);
2181 }
2182
2183 int
2184 spa_maxdnodesize(spa_t *spa)
2185 {
2186 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2187 return (DNODE_MAX_SIZE);
2188 else
2189 return (DNODE_MIN_SIZE);
2190 }
2191
2192 boolean_t
2193 spa_multihost(spa_t *spa)
2194 {
2195 return (spa->spa_multihost ? B_TRUE : B_FALSE);
2196 }
2197
2198 unsigned long
2199 spa_get_hostid(void)
2200 {
2201 unsigned long myhostid;
2202
2203 #ifdef _KERNEL
2204 myhostid = zone_get_hostid(NULL);
2205 #else /* _KERNEL */
2206 /*
2207 * We're emulating the system's hostid in userland, so
2208 * we can't use zone_get_hostid().
2209 */
2210 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2211 #endif /* _KERNEL */
2212
2213 return (myhostid);
2214 }
2215
2216 #if defined(_KERNEL) && defined(HAVE_SPL)
2217
2218 #include <linux/mod_compat.h>
2219
2220 static int
2221 param_set_deadman_failmode(const char *val, zfs_kernel_param_t *kp)
2222 {
2223 spa_t *spa = NULL;
2224 char *p;
2225
2226 if (val == NULL)
2227 return (SET_ERROR(-EINVAL));
2228
2229 if ((p = strchr(val, '\n')) != NULL)
2230 *p = '\0';
2231
2232 if (strcmp(val, "wait") != 0 && strcmp(val, "continue") != 0 &&
2233 strcmp(val, "panic"))
2234 return (SET_ERROR(-EINVAL));
2235
2236 mutex_enter(&spa_namespace_lock);
2237 while ((spa = spa_next(spa)) != NULL)
2238 spa_set_deadman_failmode(spa, val);
2239 mutex_exit(&spa_namespace_lock);
2240
2241 return (param_set_charp(val, kp));
2242 }
2243
2244 /* Namespace manipulation */
2245 EXPORT_SYMBOL(spa_lookup);
2246 EXPORT_SYMBOL(spa_add);
2247 EXPORT_SYMBOL(spa_remove);
2248 EXPORT_SYMBOL(spa_next);
2249
2250 /* Refcount functions */
2251 EXPORT_SYMBOL(spa_open_ref);
2252 EXPORT_SYMBOL(spa_close);
2253 EXPORT_SYMBOL(spa_refcount_zero);
2254
2255 /* Pool configuration lock */
2256 EXPORT_SYMBOL(spa_config_tryenter);
2257 EXPORT_SYMBOL(spa_config_enter);
2258 EXPORT_SYMBOL(spa_config_exit);
2259 EXPORT_SYMBOL(spa_config_held);
2260
2261 /* Pool vdev add/remove lock */
2262 EXPORT_SYMBOL(spa_vdev_enter);
2263 EXPORT_SYMBOL(spa_vdev_exit);
2264
2265 /* Pool vdev state change lock */
2266 EXPORT_SYMBOL(spa_vdev_state_enter);
2267 EXPORT_SYMBOL(spa_vdev_state_exit);
2268
2269 /* Accessor functions */
2270 EXPORT_SYMBOL(spa_shutting_down);
2271 EXPORT_SYMBOL(spa_get_dsl);
2272 EXPORT_SYMBOL(spa_get_rootblkptr);
2273 EXPORT_SYMBOL(spa_set_rootblkptr);
2274 EXPORT_SYMBOL(spa_altroot);
2275 EXPORT_SYMBOL(spa_sync_pass);
2276 EXPORT_SYMBOL(spa_name);
2277 EXPORT_SYMBOL(spa_guid);
2278 EXPORT_SYMBOL(spa_last_synced_txg);
2279 EXPORT_SYMBOL(spa_first_txg);
2280 EXPORT_SYMBOL(spa_syncing_txg);
2281 EXPORT_SYMBOL(spa_version);
2282 EXPORT_SYMBOL(spa_state);
2283 EXPORT_SYMBOL(spa_load_state);
2284 EXPORT_SYMBOL(spa_freeze_txg);
2285 EXPORT_SYMBOL(spa_get_dspace);
2286 EXPORT_SYMBOL(spa_update_dspace);
2287 EXPORT_SYMBOL(spa_deflate);
2288 EXPORT_SYMBOL(spa_normal_class);
2289 EXPORT_SYMBOL(spa_log_class);
2290 EXPORT_SYMBOL(spa_max_replication);
2291 EXPORT_SYMBOL(spa_prev_software_version);
2292 EXPORT_SYMBOL(spa_get_failmode);
2293 EXPORT_SYMBOL(spa_suspended);
2294 EXPORT_SYMBOL(spa_bootfs);
2295 EXPORT_SYMBOL(spa_delegation);
2296 EXPORT_SYMBOL(spa_meta_objset);
2297 EXPORT_SYMBOL(spa_maxblocksize);
2298 EXPORT_SYMBOL(spa_maxdnodesize);
2299
2300 /* Miscellaneous support routines */
2301 EXPORT_SYMBOL(spa_rename);
2302 EXPORT_SYMBOL(spa_guid_exists);
2303 EXPORT_SYMBOL(spa_strdup);
2304 EXPORT_SYMBOL(spa_strfree);
2305 EXPORT_SYMBOL(spa_get_random);
2306 EXPORT_SYMBOL(spa_generate_guid);
2307 EXPORT_SYMBOL(snprintf_blkptr);
2308 EXPORT_SYMBOL(spa_freeze);
2309 EXPORT_SYMBOL(spa_upgrade);
2310 EXPORT_SYMBOL(spa_evict_all);
2311 EXPORT_SYMBOL(spa_lookup_by_guid);
2312 EXPORT_SYMBOL(spa_has_spare);
2313 EXPORT_SYMBOL(dva_get_dsize_sync);
2314 EXPORT_SYMBOL(bp_get_dsize_sync);
2315 EXPORT_SYMBOL(bp_get_dsize);
2316 EXPORT_SYMBOL(spa_has_slogs);
2317 EXPORT_SYMBOL(spa_is_root);
2318 EXPORT_SYMBOL(spa_writeable);
2319 EXPORT_SYMBOL(spa_mode);
2320 EXPORT_SYMBOL(spa_namespace_lock);
2321
2322 /* BEGIN CSTYLED */
2323 module_param(zfs_flags, uint, 0644);
2324 MODULE_PARM_DESC(zfs_flags, "Set additional debugging flags");
2325
2326 module_param(zfs_recover, int, 0644);
2327 MODULE_PARM_DESC(zfs_recover, "Set to attempt to recover from fatal errors");
2328
2329 module_param(zfs_free_leak_on_eio, int, 0644);
2330 MODULE_PARM_DESC(zfs_free_leak_on_eio,
2331 "Set to ignore IO errors during free and permanently leak the space");
2332
2333 module_param(zfs_deadman_synctime_ms, ulong, 0644);
2334 MODULE_PARM_DESC(zfs_deadman_synctime_ms,
2335 "Pool sync expiration time in milliseconds");
2336
2337 module_param(zfs_deadman_ziotime_ms, ulong, 0644);
2338 MODULE_PARM_DESC(zfs_deadman_ziotime_ms,
2339 "IO expiration time in milliseconds");
2340
2341 module_param(zfs_deadman_checktime_ms, ulong, 0644);
2342 MODULE_PARM_DESC(zfs_deadman_checktime_ms,
2343 "Dead I/O check interval in milliseconds");
2344
2345 module_param(zfs_deadman_enabled, int, 0644);
2346 MODULE_PARM_DESC(zfs_deadman_enabled, "Enable deadman timer");
2347
2348 module_param_call(zfs_deadman_failmode, param_set_deadman_failmode,
2349 param_get_charp, &zfs_deadman_failmode, 0644);
2350 MODULE_PARM_DESC(zfs_deadman_failmode, "Failmode for deadman timer");
2351
2352 module_param(spa_asize_inflation, int, 0644);
2353 MODULE_PARM_DESC(spa_asize_inflation,
2354 "SPA size estimate multiplication factor");
2355
2356 module_param(spa_slop_shift, int, 0644);
2357 MODULE_PARM_DESC(spa_slop_shift, "Reserved free space in pool");
2358 /* END CSTYLED */
2359 #endif