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, 2019 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 * Copyright (c) 2017, Intel Corporation.
29 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
30 */
31
32 #include <sys/zfs_context.h>
33 #include <sys/spa_impl.h>
34 #include <sys/zio.h>
35 #include <sys/zio_checksum.h>
36 #include <sys/zio_compress.h>
37 #include <sys/dmu.h>
38 #include <sys/dmu_tx.h>
39 #include <sys/zap.h>
40 #include <sys/zil.h>
41 #include <sys/vdev_impl.h>
42 #include <sys/vdev_initialize.h>
43 #include <sys/vdev_trim.h>
44 #include <sys/vdev_file.h>
45 #include <sys/vdev_raidz.h>
46 #include <sys/metaslab.h>
47 #include <sys/uberblock_impl.h>
48 #include <sys/txg.h>
49 #include <sys/avl.h>
50 #include <sys/unique.h>
51 #include <sys/dsl_pool.h>
52 #include <sys/dsl_dir.h>
53 #include <sys/dsl_prop.h>
54 #include <sys/fm/util.h>
55 #include <sys/dsl_scan.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/metaslab_impl.h>
58 #include <sys/arc.h>
59 #include <sys/ddt.h>
60 #include <sys/kstat.h>
61 #include "zfs_prop.h"
62 #include <sys/btree.h>
63 #include <sys/zfeature.h>
64 #include <sys/qat.h>
65 #include <sys/zstd/zstd.h>
66
67 /*
68 * SPA locking
69 *
70 * There are three basic locks for managing spa_t structures:
71 *
72 * spa_namespace_lock (global mutex)
73 *
74 * This lock must be acquired to do any of the following:
75 *
76 * - Lookup a spa_t by name
77 * - Add or remove a spa_t from the namespace
78 * - Increase spa_refcount from non-zero
79 * - Check if spa_refcount is zero
80 * - Rename a spa_t
81 * - add/remove/attach/detach devices
82 * - Held for the duration of create/destroy/import/export
83 *
84 * It does not need to handle recursion. A create or destroy may
85 * reference objects (files or zvols) in other pools, but by
86 * definition they must have an existing reference, and will never need
87 * to lookup a spa_t by name.
88 *
89 * spa_refcount (per-spa zfs_refcount_t protected by mutex)
90 *
91 * This reference count keep track of any active users of the spa_t. The
92 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
93 * the refcount is never really 'zero' - opening a pool implicitly keeps
94 * some references in the DMU. Internally we check against spa_minref, but
95 * present the image of a zero/non-zero value to consumers.
96 *
97 * spa_config_lock[] (per-spa array of rwlocks)
98 *
99 * This protects the spa_t from config changes, and must be held in
100 * the following circumstances:
101 *
102 * - RW_READER to perform I/O to the spa
103 * - RW_WRITER to change the vdev config
104 *
105 * The locking order is fairly straightforward:
106 *
107 * spa_namespace_lock -> spa_refcount
108 *
109 * The namespace lock must be acquired to increase the refcount from 0
110 * or to check if it is zero.
111 *
112 * spa_refcount -> spa_config_lock[]
113 *
114 * There must be at least one valid reference on the spa_t to acquire
115 * the config lock.
116 *
117 * spa_namespace_lock -> spa_config_lock[]
118 *
119 * The namespace lock must always be taken before the config lock.
120 *
121 *
122 * The spa_namespace_lock can be acquired directly and is globally visible.
123 *
124 * The namespace is manipulated using the following functions, all of which
125 * require the spa_namespace_lock to be held.
126 *
127 * spa_lookup() Lookup a spa_t by name.
128 *
129 * spa_add() Create a new spa_t in the namespace.
130 *
131 * spa_remove() Remove a spa_t from the namespace. This also
132 * frees up any memory associated with the spa_t.
133 *
134 * spa_next() Returns the next spa_t in the system, or the
135 * first if NULL is passed.
136 *
137 * spa_evict_all() Shutdown and remove all spa_t structures in
138 * the system.
139 *
140 * spa_guid_exists() Determine whether a pool/device guid exists.
141 *
142 * The spa_refcount is manipulated using the following functions:
143 *
144 * spa_open_ref() Adds a reference to the given spa_t. Must be
145 * called with spa_namespace_lock held if the
146 * refcount is currently zero.
147 *
148 * spa_close() Remove a reference from the spa_t. This will
149 * not free the spa_t or remove it from the
150 * namespace. No locking is required.
151 *
152 * spa_refcount_zero() Returns true if the refcount is currently
153 * zero. Must be called with spa_namespace_lock
154 * held.
155 *
156 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
157 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
158 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
159 *
160 * To read the configuration, it suffices to hold one of these locks as reader.
161 * To modify the configuration, you must hold all locks as writer. To modify
162 * vdev state without altering the vdev tree's topology (e.g. online/offline),
163 * you must hold SCL_STATE and SCL_ZIO as writer.
164 *
165 * We use these distinct config locks to avoid recursive lock entry.
166 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
167 * block allocations (SCL_ALLOC), which may require reading space maps
168 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
169 *
170 * The spa config locks cannot be normal rwlocks because we need the
171 * ability to hand off ownership. For example, SCL_ZIO is acquired
172 * by the issuing thread and later released by an interrupt thread.
173 * They do, however, obey the usual write-wanted semantics to prevent
174 * writer (i.e. system administrator) starvation.
175 *
176 * The lock acquisition rules are as follows:
177 *
178 * SCL_CONFIG
179 * Protects changes to the vdev tree topology, such as vdev
180 * add/remove/attach/detach. Protects the dirty config list
181 * (spa_config_dirty_list) and the set of spares and l2arc devices.
182 *
183 * SCL_STATE
184 * Protects changes to pool state and vdev state, such as vdev
185 * online/offline/fault/degrade/clear. Protects the dirty state list
186 * (spa_state_dirty_list) and global pool state (spa_state).
187 *
188 * SCL_ALLOC
189 * Protects changes to metaslab groups and classes.
190 * Held as reader by metaslab_alloc() and metaslab_claim().
191 *
192 * SCL_ZIO
193 * Held by bp-level zios (those which have no io_vd upon entry)
194 * to prevent changes to the vdev tree. The bp-level zio implicitly
195 * protects all of its vdev child zios, which do not hold SCL_ZIO.
196 *
197 * SCL_FREE
198 * Protects changes to metaslab groups and classes.
199 * Held as reader by metaslab_free(). SCL_FREE is distinct from
200 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
201 * blocks in zio_done() while another i/o that holds either
202 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
203 *
204 * SCL_VDEV
205 * Held as reader to prevent changes to the vdev tree during trivial
206 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
207 * other locks, and lower than all of them, to ensure that it's safe
208 * to acquire regardless of caller context.
209 *
210 * In addition, the following rules apply:
211 *
212 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
213 * The lock ordering is SCL_CONFIG > spa_props_lock.
214 *
215 * (b) I/O operations on leaf vdevs. For any zio operation that takes
216 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
217 * or zio_write_phys() -- the caller must ensure that the config cannot
218 * cannot change in the interim, and that the vdev cannot be reopened.
219 * SCL_STATE as reader suffices for both.
220 *
221 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
222 *
223 * spa_vdev_enter() Acquire the namespace lock and the config lock
224 * for writing.
225 *
226 * spa_vdev_exit() Release the config lock, wait for all I/O
227 * to complete, sync the updated configs to the
228 * cache, and release the namespace lock.
229 *
230 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
231 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
232 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
233 */
234
235 static avl_tree_t spa_namespace_avl;
236 kmutex_t spa_namespace_lock;
237 static kcondvar_t spa_namespace_cv;
238 int spa_max_replication_override = SPA_DVAS_PER_BP;
239
240 static kmutex_t spa_spare_lock;
241 static avl_tree_t spa_spare_avl;
242 static kmutex_t spa_l2cache_lock;
243 static avl_tree_t spa_l2cache_avl;
244
245 kmem_cache_t *spa_buffer_pool;
246 spa_mode_t spa_mode_global = SPA_MODE_UNINIT;
247
248 #ifdef ZFS_DEBUG
249 /*
250 * Everything except dprintf, set_error, spa, and indirect_remap is on
251 * by default in debug builds.
252 */
253 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SET_ERROR |
254 ZFS_DEBUG_INDIRECT_REMAP);
255 #else
256 int zfs_flags = 0;
257 #endif
258
259 /*
260 * zfs_recover can be set to nonzero to attempt to recover from
261 * otherwise-fatal errors, typically caused by on-disk corruption. When
262 * set, calls to zfs_panic_recover() will turn into warning messages.
263 * This should only be used as a last resort, as it typically results
264 * in leaked space, or worse.
265 */
266 int zfs_recover = B_FALSE;
267
268 /*
269 * If destroy encounters an EIO while reading metadata (e.g. indirect
270 * blocks), space referenced by the missing metadata can not be freed.
271 * Normally this causes the background destroy to become "stalled", as
272 * it is unable to make forward progress. While in this stalled state,
273 * all remaining space to free from the error-encountering filesystem is
274 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
275 * permanently leak the space from indirect blocks that can not be read,
276 * and continue to free everything else that it can.
277 *
278 * The default, "stalling" behavior is useful if the storage partially
279 * fails (i.e. some but not all i/os fail), and then later recovers. In
280 * this case, we will be able to continue pool operations while it is
281 * partially failed, and when it recovers, we can continue to free the
282 * space, with no leaks. However, note that this case is actually
283 * fairly rare.
284 *
285 * Typically pools either (a) fail completely (but perhaps temporarily,
286 * e.g. a top-level vdev going offline), or (b) have localized,
287 * permanent errors (e.g. disk returns the wrong data due to bit flip or
288 * firmware bug). In case (a), this setting does not matter because the
289 * pool will be suspended and the sync thread will not be able to make
290 * forward progress regardless. In case (b), because the error is
291 * permanent, the best we can do is leak the minimum amount of space,
292 * which is what setting this flag will do. Therefore, it is reasonable
293 * for this flag to normally be set, but we chose the more conservative
294 * approach of not setting it, so that there is no possibility of
295 * leaking space in the "partial temporary" failure case.
296 */
297 int zfs_free_leak_on_eio = B_FALSE;
298
299 /*
300 * Expiration time in milliseconds. This value has two meanings. First it is
301 * used to determine when the spa_deadman() logic should fire. By default the
302 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds.
303 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
304 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
305 * in one of three behaviors controlled by zfs_deadman_failmode.
306 */
307 unsigned long zfs_deadman_synctime_ms = 600000UL;
308
309 /*
310 * This value controls the maximum amount of time zio_wait() will block for an
311 * outstanding IO. By default this is 300 seconds at which point the "hung"
312 * behavior will be applied as described for zfs_deadman_synctime_ms.
313 */
314 unsigned long zfs_deadman_ziotime_ms = 300000UL;
315
316 /*
317 * Check time in milliseconds. This defines the frequency at which we check
318 * for hung I/O.
319 */
320 unsigned long zfs_deadman_checktime_ms = 60000UL;
321
322 /*
323 * By default the deadman is enabled.
324 */
325 int zfs_deadman_enabled = 1;
326
327 /*
328 * Controls the behavior of the deadman when it detects a "hung" I/O.
329 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
330 *
331 * wait - Wait for the "hung" I/O (default)
332 * continue - Attempt to recover from a "hung" I/O
333 * panic - Panic the system
334 */
335 char *zfs_deadman_failmode = "wait";
336
337 /*
338 * The worst case is single-sector max-parity RAID-Z blocks, in which
339 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
340 * times the size; so just assume that. Add to this the fact that
341 * we can have up to 3 DVAs per bp, and one more factor of 2 because
342 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
343 * the worst case is:
344 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
345 */
346 int spa_asize_inflation = 24;
347
348 /*
349 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
350 * the pool to be consumed (bounded by spa_max_slop). This ensures that we
351 * don't run the pool completely out of space, due to unaccounted changes (e.g.
352 * to the MOS). It also limits the worst-case time to allocate space. If we
353 * have less than this amount of free space, most ZPL operations (e.g. write,
354 * create) will return ENOSPC. The ZIL metaslabs (spa_embedded_log_class) are
355 * also part of this 3.2% of space which can't be consumed by normal writes;
356 * the slop space "proper" (spa_get_slop_space()) is decreased by the embedded
357 * log space.
358 *
359 * Certain operations (e.g. file removal, most administrative actions) can
360 * use half the slop space. They will only return ENOSPC if less than half
361 * the slop space is free. Typically, once the pool has less than the slop
362 * space free, the user will use these operations to free up space in the pool.
363 * These are the operations that call dsl_pool_adjustedsize() with the netfree
364 * argument set to TRUE.
365 *
366 * Operations that are almost guaranteed to free up space in the absence of
367 * a pool checkpoint can use up to three quarters of the slop space
368 * (e.g zfs destroy).
369 *
370 * A very restricted set of operations are always permitted, regardless of
371 * the amount of free space. These are the operations that call
372 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
373 * increase in the amount of space used, it is possible to run the pool
374 * completely out of space, causing it to be permanently read-only.
375 *
376 * Note that on very small pools, the slop space will be larger than
377 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
378 * but we never allow it to be more than half the pool size.
379 *
380 * Further, on very large pools, the slop space will be smaller than
381 * 3.2%, to avoid reserving much more space than we actually need; bounded
382 * by spa_max_slop (128GB).
383 *
384 * See also the comments in zfs_space_check_t.
385 */
386 int spa_slop_shift = 5;
387 uint64_t spa_min_slop = 128ULL * 1024 * 1024;
388 uint64_t spa_max_slop = 128ULL * 1024 * 1024 * 1024;
389 int spa_allocators = 4;
390
391
392 /*PRINTFLIKE2*/
393 void
spa_load_failed(spa_t * spa,const char * fmt,...)394 spa_load_failed(spa_t *spa, const char *fmt, ...)
395 {
396 va_list adx;
397 char buf[256];
398
399 va_start(adx, fmt);
400 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
401 va_end(adx);
402
403 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
404 spa->spa_trust_config ? "trusted" : "untrusted", buf);
405 }
406
407 /*PRINTFLIKE2*/
408 void
spa_load_note(spa_t * spa,const char * fmt,...)409 spa_load_note(spa_t *spa, const char *fmt, ...)
410 {
411 va_list adx;
412 char buf[256];
413
414 va_start(adx, fmt);
415 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
416 va_end(adx);
417
418 zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
419 spa->spa_trust_config ? "trusted" : "untrusted", buf);
420 }
421
422 /*
423 * By default dedup and user data indirects land in the special class
424 */
425 int zfs_ddt_data_is_special = B_TRUE;
426 int zfs_user_indirect_is_special = B_TRUE;
427
428 /*
429 * The percentage of special class final space reserved for metadata only.
430 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
431 * let metadata into the class.
432 */
433 int zfs_special_class_metadata_reserve_pct = 25;
434
435 /*
436 * ==========================================================================
437 * SPA config locking
438 * ==========================================================================
439 */
440 static void
spa_config_lock_init(spa_t * spa)441 spa_config_lock_init(spa_t *spa)
442 {
443 for (int i = 0; i < SCL_LOCKS; i++) {
444 spa_config_lock_t *scl = &spa->spa_config_lock[i];
445 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
446 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
447 scl->scl_writer = NULL;
448 scl->scl_write_wanted = 0;
449 scl->scl_count = 0;
450 }
451 }
452
453 static void
spa_config_lock_destroy(spa_t * spa)454 spa_config_lock_destroy(spa_t *spa)
455 {
456 for (int i = 0; i < SCL_LOCKS; i++) {
457 spa_config_lock_t *scl = &spa->spa_config_lock[i];
458 mutex_destroy(&scl->scl_lock);
459 cv_destroy(&scl->scl_cv);
460 ASSERT(scl->scl_writer == NULL);
461 ASSERT(scl->scl_write_wanted == 0);
462 ASSERT(scl->scl_count == 0);
463 }
464 }
465
466 int
spa_config_tryenter(spa_t * spa,int locks,void * tag,krw_t rw)467 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
468 {
469 for (int i = 0; i < SCL_LOCKS; i++) {
470 spa_config_lock_t *scl = &spa->spa_config_lock[i];
471 if (!(locks & (1 << i)))
472 continue;
473 mutex_enter(&scl->scl_lock);
474 if (rw == RW_READER) {
475 if (scl->scl_writer || scl->scl_write_wanted) {
476 mutex_exit(&scl->scl_lock);
477 spa_config_exit(spa, locks & ((1 << i) - 1),
478 tag);
479 return (0);
480 }
481 } else {
482 ASSERT(scl->scl_writer != curthread);
483 if (scl->scl_count != 0) {
484 mutex_exit(&scl->scl_lock);
485 spa_config_exit(spa, locks & ((1 << i) - 1),
486 tag);
487 return (0);
488 }
489 scl->scl_writer = curthread;
490 }
491 scl->scl_count++;
492 mutex_exit(&scl->scl_lock);
493 }
494 return (1);
495 }
496
497 static void
spa_config_enter_impl(spa_t * spa,int locks,const void * tag,krw_t rw,int mmp_flag)498 spa_config_enter_impl(spa_t *spa, int locks, const void *tag, krw_t rw,
499 int mmp_flag)
500 {
501 (void) tag;
502 int wlocks_held = 0;
503
504 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
505
506 for (int i = 0; i < SCL_LOCKS; i++) {
507 spa_config_lock_t *scl = &spa->spa_config_lock[i];
508 if (scl->scl_writer == curthread)
509 wlocks_held |= (1 << i);
510 if (!(locks & (1 << i)))
511 continue;
512 mutex_enter(&scl->scl_lock);
513 if (rw == RW_READER) {
514 while (scl->scl_writer ||
515 (!mmp_flag && scl->scl_write_wanted)) {
516 cv_wait(&scl->scl_cv, &scl->scl_lock);
517 }
518 } else {
519 ASSERT(scl->scl_writer != curthread);
520 while (scl->scl_count != 0) {
521 scl->scl_write_wanted++;
522 cv_wait(&scl->scl_cv, &scl->scl_lock);
523 scl->scl_write_wanted--;
524 }
525 scl->scl_writer = curthread;
526 }
527 scl->scl_count++;
528 mutex_exit(&scl->scl_lock);
529 }
530 ASSERT3U(wlocks_held, <=, locks);
531 }
532
533 void
spa_config_enter(spa_t * spa,int locks,const void * tag,krw_t rw)534 spa_config_enter(spa_t *spa, int locks, const void *tag, krw_t rw)
535 {
536 spa_config_enter_impl(spa, locks, tag, rw, 0);
537 }
538
539 /*
540 * The spa_config_enter_mmp() allows the mmp thread to cut in front of
541 * outstanding write lock requests. This is needed since the mmp updates are
542 * time sensitive and failure to service them promptly will result in a
543 * suspended pool. This pool suspension has been seen in practice when there is
544 * a single disk in a pool that is responding slowly and presumably about to
545 * fail.
546 */
547
548 void
spa_config_enter_mmp(spa_t * spa,int locks,const void * tag,krw_t rw)549 spa_config_enter_mmp(spa_t *spa, int locks, const void *tag, krw_t rw)
550 {
551 spa_config_enter_impl(spa, locks, tag, rw, 1);
552 }
553
554 void
spa_config_exit(spa_t * spa,int locks,const void * tag)555 spa_config_exit(spa_t *spa, int locks, const void *tag)
556 {
557 (void) tag;
558 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
559 spa_config_lock_t *scl = &spa->spa_config_lock[i];
560 if (!(locks & (1 << i)))
561 continue;
562 mutex_enter(&scl->scl_lock);
563 ASSERT(scl->scl_count > 0);
564 if (--scl->scl_count == 0) {
565 ASSERT(scl->scl_writer == NULL ||
566 scl->scl_writer == curthread);
567 scl->scl_writer = NULL; /* OK in either case */
568 cv_broadcast(&scl->scl_cv);
569 }
570 mutex_exit(&scl->scl_lock);
571 }
572 }
573
574 int
spa_config_held(spa_t * spa,int locks,krw_t rw)575 spa_config_held(spa_t *spa, int locks, krw_t rw)
576 {
577 int locks_held = 0;
578
579 for (int i = 0; i < SCL_LOCKS; i++) {
580 spa_config_lock_t *scl = &spa->spa_config_lock[i];
581 if (!(locks & (1 << i)))
582 continue;
583 if ((rw == RW_READER && scl->scl_count != 0) ||
584 (rw == RW_WRITER && scl->scl_writer == curthread))
585 locks_held |= 1 << i;
586 }
587
588 return (locks_held);
589 }
590
591 /*
592 * ==========================================================================
593 * SPA namespace functions
594 * ==========================================================================
595 */
596
597 /*
598 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
599 * Returns NULL if no matching spa_t is found.
600 */
601 spa_t *
spa_lookup(const char * name)602 spa_lookup(const char *name)
603 {
604 static spa_t search; /* spa_t is large; don't allocate on stack */
605 spa_t *spa;
606 avl_index_t where;
607 char *cp;
608
609 ASSERT(MUTEX_HELD(&spa_namespace_lock));
610
611 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
612
613 /*
614 * If it's a full dataset name, figure out the pool name and
615 * just use that.
616 */
617 cp = strpbrk(search.spa_name, "/@#");
618 if (cp != NULL)
619 *cp = '\0';
620
621 spa = avl_find(&spa_namespace_avl, &search, &where);
622
623 return (spa);
624 }
625
626 /*
627 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
628 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
629 * looking for potentially hung I/Os.
630 */
631 void
spa_deadman(void * arg)632 spa_deadman(void *arg)
633 {
634 spa_t *spa = arg;
635
636 /* Disable the deadman if the pool is suspended. */
637 if (spa_suspended(spa))
638 return;
639
640 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
641 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
642 (u_longlong_t)++spa->spa_deadman_calls);
643 if (zfs_deadman_enabled)
644 vdev_deadman(spa->spa_root_vdev, FTAG);
645
646 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
647 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
648 MSEC_TO_TICK(zfs_deadman_checktime_ms));
649 }
650
651 static int
spa_log_sm_sort_by_txg(const void * va,const void * vb)652 spa_log_sm_sort_by_txg(const void *va, const void *vb)
653 {
654 const spa_log_sm_t *a = va;
655 const spa_log_sm_t *b = vb;
656
657 return (TREE_CMP(a->sls_txg, b->sls_txg));
658 }
659
660 /*
661 * Create an uninitialized spa_t with the given name. Requires
662 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
663 * exist by calling spa_lookup() first.
664 */
665 spa_t *
spa_add(const char * name,nvlist_t * config,const char * altroot)666 spa_add(const char *name, nvlist_t *config, const char *altroot)
667 {
668 spa_t *spa;
669 spa_config_dirent_t *dp;
670
671 ASSERT(MUTEX_HELD(&spa_namespace_lock));
672
673 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
674
675 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
676 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
677 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
678 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
679 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
680 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
681 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
682 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
683 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
684 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
685 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
686 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
687 mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL);
688 mutex_init(&spa->spa_activities_lock, NULL, MUTEX_DEFAULT, NULL);
689
690 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
691 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
692 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
693 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
694 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
695 cv_init(&spa->spa_activities_cv, NULL, CV_DEFAULT, NULL);
696 cv_init(&spa->spa_waiters_cv, NULL, CV_DEFAULT, NULL);
697
698 for (int t = 0; t < TXG_SIZE; t++)
699 bplist_create(&spa->spa_free_bplist[t]);
700
701 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
702 spa->spa_state = POOL_STATE_UNINITIALIZED;
703 spa->spa_freeze_txg = UINT64_MAX;
704 spa->spa_final_txg = UINT64_MAX;
705 spa->spa_load_max_txg = UINT64_MAX;
706 spa->spa_proc = &p0;
707 spa->spa_proc_state = SPA_PROC_NONE;
708 spa->spa_trust_config = B_TRUE;
709 spa->spa_hostid = zone_get_hostid(NULL);
710
711 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
712 spa->spa_deadman_ziotime = MSEC2NSEC(zfs_deadman_ziotime_ms);
713 spa_set_deadman_failmode(spa, zfs_deadman_failmode);
714
715 zfs_refcount_create(&spa->spa_refcount);
716 spa_config_lock_init(spa);
717 spa_stats_init(spa);
718
719 avl_add(&spa_namespace_avl, spa);
720
721 /*
722 * Set the alternate root, if there is one.
723 */
724 if (altroot)
725 spa->spa_root = spa_strdup(altroot);
726
727 spa->spa_alloc_count = spa_allocators;
728 spa->spa_allocs = kmem_zalloc(spa->spa_alloc_count *
729 sizeof (spa_alloc_t), KM_SLEEP);
730 for (int i = 0; i < spa->spa_alloc_count; i++) {
731 mutex_init(&spa->spa_allocs[i].spaa_lock, NULL, MUTEX_DEFAULT,
732 NULL);
733 avl_create(&spa->spa_allocs[i].spaa_tree, zio_bookmark_compare,
734 sizeof (zio_t), offsetof(zio_t, io_alloc_node));
735 }
736 avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed,
737 sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node));
738 avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg,
739 sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node));
740 list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t),
741 offsetof(log_summary_entry_t, lse_node));
742
743 /*
744 * Every pool starts with the default cachefile
745 */
746 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
747 offsetof(spa_config_dirent_t, scd_link));
748
749 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
750 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
751 list_insert_head(&spa->spa_config_list, dp);
752
753 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
754 KM_SLEEP) == 0);
755
756 if (config != NULL) {
757 nvlist_t *features;
758
759 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
760 &features) == 0) {
761 VERIFY(nvlist_dup(features, &spa->spa_label_features,
762 0) == 0);
763 }
764
765 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
766 }
767
768 if (spa->spa_label_features == NULL) {
769 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
770 KM_SLEEP) == 0);
771 }
772
773 spa->spa_min_ashift = INT_MAX;
774 spa->spa_max_ashift = 0;
775 spa->spa_min_alloc = INT_MAX;
776
777 /* Reset cached value */
778 spa->spa_dedup_dspace = ~0ULL;
779
780 /*
781 * As a pool is being created, treat all features as disabled by
782 * setting SPA_FEATURE_DISABLED for all entries in the feature
783 * refcount cache.
784 */
785 for (int i = 0; i < SPA_FEATURES; i++) {
786 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
787 }
788
789 list_create(&spa->spa_leaf_list, sizeof (vdev_t),
790 offsetof(vdev_t, vdev_leaf_node));
791
792 return (spa);
793 }
794
795 /*
796 * Removes a spa_t from the namespace, freeing up any memory used. Requires
797 * spa_namespace_lock. This is called only after the spa_t has been closed and
798 * deactivated.
799 */
800 void
spa_remove(spa_t * spa)801 spa_remove(spa_t *spa)
802 {
803 spa_config_dirent_t *dp;
804
805 ASSERT(MUTEX_HELD(&spa_namespace_lock));
806 ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED);
807 ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
808 ASSERT0(spa->spa_waiters);
809
810 nvlist_free(spa->spa_config_splitting);
811
812 avl_remove(&spa_namespace_avl, spa);
813 cv_broadcast(&spa_namespace_cv);
814
815 if (spa->spa_root)
816 spa_strfree(spa->spa_root);
817
818 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
819 list_remove(&spa->spa_config_list, dp);
820 if (dp->scd_path != NULL)
821 spa_strfree(dp->scd_path);
822 kmem_free(dp, sizeof (spa_config_dirent_t));
823 }
824
825 for (int i = 0; i < spa->spa_alloc_count; i++) {
826 avl_destroy(&spa->spa_allocs[i].spaa_tree);
827 mutex_destroy(&spa->spa_allocs[i].spaa_lock);
828 }
829 kmem_free(spa->spa_allocs, spa->spa_alloc_count *
830 sizeof (spa_alloc_t));
831
832 avl_destroy(&spa->spa_metaslabs_by_flushed);
833 avl_destroy(&spa->spa_sm_logs_by_txg);
834 list_destroy(&spa->spa_log_summary);
835 list_destroy(&spa->spa_config_list);
836 list_destroy(&spa->spa_leaf_list);
837
838 nvlist_free(spa->spa_label_features);
839 nvlist_free(spa->spa_load_info);
840 nvlist_free(spa->spa_feat_stats);
841 spa_config_set(spa, NULL);
842
843 zfs_refcount_destroy(&spa->spa_refcount);
844
845 spa_stats_destroy(spa);
846 spa_config_lock_destroy(spa);
847
848 for (int t = 0; t < TXG_SIZE; t++)
849 bplist_destroy(&spa->spa_free_bplist[t]);
850
851 zio_checksum_templates_free(spa);
852
853 cv_destroy(&spa->spa_async_cv);
854 cv_destroy(&spa->spa_evicting_os_cv);
855 cv_destroy(&spa->spa_proc_cv);
856 cv_destroy(&spa->spa_scrub_io_cv);
857 cv_destroy(&spa->spa_suspend_cv);
858 cv_destroy(&spa->spa_activities_cv);
859 cv_destroy(&spa->spa_waiters_cv);
860
861 mutex_destroy(&spa->spa_flushed_ms_lock);
862 mutex_destroy(&spa->spa_async_lock);
863 mutex_destroy(&spa->spa_errlist_lock);
864 mutex_destroy(&spa->spa_errlog_lock);
865 mutex_destroy(&spa->spa_evicting_os_lock);
866 mutex_destroy(&spa->spa_history_lock);
867 mutex_destroy(&spa->spa_proc_lock);
868 mutex_destroy(&spa->spa_props_lock);
869 mutex_destroy(&spa->spa_cksum_tmpls_lock);
870 mutex_destroy(&spa->spa_scrub_lock);
871 mutex_destroy(&spa->spa_suspend_lock);
872 mutex_destroy(&spa->spa_vdev_top_lock);
873 mutex_destroy(&spa->spa_feat_stats_lock);
874 mutex_destroy(&spa->spa_activities_lock);
875
876 kmem_free(spa, sizeof (spa_t));
877 }
878
879 /*
880 * Given a pool, return the next pool in the namespace, or NULL if there is
881 * none. If 'prev' is NULL, return the first pool.
882 */
883 spa_t *
spa_next(spa_t * prev)884 spa_next(spa_t *prev)
885 {
886 ASSERT(MUTEX_HELD(&spa_namespace_lock));
887
888 if (prev)
889 return (AVL_NEXT(&spa_namespace_avl, prev));
890 else
891 return (avl_first(&spa_namespace_avl));
892 }
893
894 /*
895 * ==========================================================================
896 * SPA refcount functions
897 * ==========================================================================
898 */
899
900 /*
901 * Add a reference to the given spa_t. Must have at least one reference, or
902 * have the namespace lock held.
903 */
904 void
spa_open_ref(spa_t * spa,void * tag)905 spa_open_ref(spa_t *spa, void *tag)
906 {
907 ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
908 MUTEX_HELD(&spa_namespace_lock));
909 (void) zfs_refcount_add(&spa->spa_refcount, tag);
910 }
911
912 /*
913 * Remove a reference to the given spa_t. Must have at least one reference, or
914 * have the namespace lock held.
915 */
916 void
spa_close(spa_t * spa,void * tag)917 spa_close(spa_t *spa, void *tag)
918 {
919 ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref ||
920 MUTEX_HELD(&spa_namespace_lock));
921 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
922 }
923
924 /*
925 * Remove a reference to the given spa_t held by a dsl dir that is
926 * being asynchronously released. Async releases occur from a taskq
927 * performing eviction of dsl datasets and dirs. The namespace lock
928 * isn't held and the hold by the object being evicted may contribute to
929 * spa_minref (e.g. dataset or directory released during pool export),
930 * so the asserts in spa_close() do not apply.
931 */
932 void
spa_async_close(spa_t * spa,void * tag)933 spa_async_close(spa_t *spa, void *tag)
934 {
935 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
936 }
937
938 /*
939 * Check to see if the spa refcount is zero. Must be called with
940 * spa_namespace_lock held. We really compare against spa_minref, which is the
941 * number of references acquired when opening a pool
942 */
943 boolean_t
spa_refcount_zero(spa_t * spa)944 spa_refcount_zero(spa_t *spa)
945 {
946 ASSERT(MUTEX_HELD(&spa_namespace_lock));
947
948 return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref);
949 }
950
951 /*
952 * ==========================================================================
953 * SPA spare and l2cache tracking
954 * ==========================================================================
955 */
956
957 /*
958 * Hot spares and cache devices are tracked using the same code below,
959 * for 'auxiliary' devices.
960 */
961
962 typedef struct spa_aux {
963 uint64_t aux_guid;
964 uint64_t aux_pool;
965 avl_node_t aux_avl;
966 int aux_count;
967 } spa_aux_t;
968
969 static inline int
spa_aux_compare(const void * a,const void * b)970 spa_aux_compare(const void *a, const void *b)
971 {
972 const spa_aux_t *sa = (const spa_aux_t *)a;
973 const spa_aux_t *sb = (const spa_aux_t *)b;
974
975 return (TREE_CMP(sa->aux_guid, sb->aux_guid));
976 }
977
978 static void
spa_aux_add(vdev_t * vd,avl_tree_t * avl)979 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
980 {
981 avl_index_t where;
982 spa_aux_t search;
983 spa_aux_t *aux;
984
985 search.aux_guid = vd->vdev_guid;
986 if ((aux = avl_find(avl, &search, &where)) != NULL) {
987 aux->aux_count++;
988 } else {
989 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
990 aux->aux_guid = vd->vdev_guid;
991 aux->aux_count = 1;
992 avl_insert(avl, aux, where);
993 }
994 }
995
996 static void
spa_aux_remove(vdev_t * vd,avl_tree_t * avl)997 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
998 {
999 spa_aux_t search;
1000 spa_aux_t *aux;
1001 avl_index_t where;
1002
1003 search.aux_guid = vd->vdev_guid;
1004 aux = avl_find(avl, &search, &where);
1005
1006 ASSERT(aux != NULL);
1007
1008 if (--aux->aux_count == 0) {
1009 avl_remove(avl, aux);
1010 kmem_free(aux, sizeof (spa_aux_t));
1011 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
1012 aux->aux_pool = 0ULL;
1013 }
1014 }
1015
1016 static boolean_t
spa_aux_exists(uint64_t guid,uint64_t * pool,int * refcnt,avl_tree_t * avl)1017 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
1018 {
1019 spa_aux_t search, *found;
1020
1021 search.aux_guid = guid;
1022 found = avl_find(avl, &search, NULL);
1023
1024 if (pool) {
1025 if (found)
1026 *pool = found->aux_pool;
1027 else
1028 *pool = 0ULL;
1029 }
1030
1031 if (refcnt) {
1032 if (found)
1033 *refcnt = found->aux_count;
1034 else
1035 *refcnt = 0;
1036 }
1037
1038 return (found != NULL);
1039 }
1040
1041 static void
spa_aux_activate(vdev_t * vd,avl_tree_t * avl)1042 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
1043 {
1044 spa_aux_t search, *found;
1045 avl_index_t where;
1046
1047 search.aux_guid = vd->vdev_guid;
1048 found = avl_find(avl, &search, &where);
1049 ASSERT(found != NULL);
1050 ASSERT(found->aux_pool == 0ULL);
1051
1052 found->aux_pool = spa_guid(vd->vdev_spa);
1053 }
1054
1055 /*
1056 * Spares are tracked globally due to the following constraints:
1057 *
1058 * - A spare may be part of multiple pools.
1059 * - A spare may be added to a pool even if it's actively in use within
1060 * another pool.
1061 * - A spare in use in any pool can only be the source of a replacement if
1062 * the target is a spare in the same pool.
1063 *
1064 * We keep track of all spares on the system through the use of a reference
1065 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1066 * spare, then we bump the reference count in the AVL tree. In addition, we set
1067 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1068 * inactive). When a spare is made active (used to replace a device in the
1069 * pool), we also keep track of which pool its been made a part of.
1070 *
1071 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1072 * called under the spa_namespace lock as part of vdev reconfiguration. The
1073 * separate spare lock exists for the status query path, which does not need to
1074 * be completely consistent with respect to other vdev configuration changes.
1075 */
1076
1077 static int
spa_spare_compare(const void * a,const void * b)1078 spa_spare_compare(const void *a, const void *b)
1079 {
1080 return (spa_aux_compare(a, b));
1081 }
1082
1083 void
spa_spare_add(vdev_t * vd)1084 spa_spare_add(vdev_t *vd)
1085 {
1086 mutex_enter(&spa_spare_lock);
1087 ASSERT(!vd->vdev_isspare);
1088 spa_aux_add(vd, &spa_spare_avl);
1089 vd->vdev_isspare = B_TRUE;
1090 mutex_exit(&spa_spare_lock);
1091 }
1092
1093 void
spa_spare_remove(vdev_t * vd)1094 spa_spare_remove(vdev_t *vd)
1095 {
1096 mutex_enter(&spa_spare_lock);
1097 ASSERT(vd->vdev_isspare);
1098 spa_aux_remove(vd, &spa_spare_avl);
1099 vd->vdev_isspare = B_FALSE;
1100 mutex_exit(&spa_spare_lock);
1101 }
1102
1103 boolean_t
spa_spare_exists(uint64_t guid,uint64_t * pool,int * refcnt)1104 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
1105 {
1106 boolean_t found;
1107
1108 mutex_enter(&spa_spare_lock);
1109 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
1110 mutex_exit(&spa_spare_lock);
1111
1112 return (found);
1113 }
1114
1115 void
spa_spare_activate(vdev_t * vd)1116 spa_spare_activate(vdev_t *vd)
1117 {
1118 mutex_enter(&spa_spare_lock);
1119 ASSERT(vd->vdev_isspare);
1120 spa_aux_activate(vd, &spa_spare_avl);
1121 mutex_exit(&spa_spare_lock);
1122 }
1123
1124 /*
1125 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1126 * Cache devices currently only support one pool per cache device, and so
1127 * for these devices the aux reference count is currently unused beyond 1.
1128 */
1129
1130 static int
spa_l2cache_compare(const void * a,const void * b)1131 spa_l2cache_compare(const void *a, const void *b)
1132 {
1133 return (spa_aux_compare(a, b));
1134 }
1135
1136 void
spa_l2cache_add(vdev_t * vd)1137 spa_l2cache_add(vdev_t *vd)
1138 {
1139 mutex_enter(&spa_l2cache_lock);
1140 ASSERT(!vd->vdev_isl2cache);
1141 spa_aux_add(vd, &spa_l2cache_avl);
1142 vd->vdev_isl2cache = B_TRUE;
1143 mutex_exit(&spa_l2cache_lock);
1144 }
1145
1146 void
spa_l2cache_remove(vdev_t * vd)1147 spa_l2cache_remove(vdev_t *vd)
1148 {
1149 mutex_enter(&spa_l2cache_lock);
1150 ASSERT(vd->vdev_isl2cache);
1151 spa_aux_remove(vd, &spa_l2cache_avl);
1152 vd->vdev_isl2cache = B_FALSE;
1153 mutex_exit(&spa_l2cache_lock);
1154 }
1155
1156 boolean_t
spa_l2cache_exists(uint64_t guid,uint64_t * pool)1157 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1158 {
1159 boolean_t found;
1160
1161 mutex_enter(&spa_l2cache_lock);
1162 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1163 mutex_exit(&spa_l2cache_lock);
1164
1165 return (found);
1166 }
1167
1168 void
spa_l2cache_activate(vdev_t * vd)1169 spa_l2cache_activate(vdev_t *vd)
1170 {
1171 mutex_enter(&spa_l2cache_lock);
1172 ASSERT(vd->vdev_isl2cache);
1173 spa_aux_activate(vd, &spa_l2cache_avl);
1174 mutex_exit(&spa_l2cache_lock);
1175 }
1176
1177 /*
1178 * ==========================================================================
1179 * SPA vdev locking
1180 * ==========================================================================
1181 */
1182
1183 /*
1184 * Lock the given spa_t for the purpose of adding or removing a vdev.
1185 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1186 * It returns the next transaction group for the spa_t.
1187 */
1188 uint64_t
spa_vdev_enter(spa_t * spa)1189 spa_vdev_enter(spa_t *spa)
1190 {
1191 mutex_enter(&spa->spa_vdev_top_lock);
1192 mutex_enter(&spa_namespace_lock);
1193
1194 vdev_autotrim_stop_all(spa);
1195
1196 return (spa_vdev_config_enter(spa));
1197 }
1198
1199 /*
1200 * The same as spa_vdev_enter() above but additionally takes the guid of
1201 * the vdev being detached. When there is a rebuild in process it will be
1202 * suspended while the vdev tree is modified then resumed by spa_vdev_exit().
1203 * The rebuild is canceled if only a single child remains after the detach.
1204 */
1205 uint64_t
spa_vdev_detach_enter(spa_t * spa,uint64_t guid)1206 spa_vdev_detach_enter(spa_t *spa, uint64_t guid)
1207 {
1208 mutex_enter(&spa->spa_vdev_top_lock);
1209 mutex_enter(&spa_namespace_lock);
1210
1211 vdev_autotrim_stop_all(spa);
1212
1213 if (guid != 0) {
1214 vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE);
1215 if (vd) {
1216 vdev_rebuild_stop_wait(vd->vdev_top);
1217 }
1218 }
1219
1220 return (spa_vdev_config_enter(spa));
1221 }
1222
1223 /*
1224 * Internal implementation for spa_vdev_enter(). Used when a vdev
1225 * operation requires multiple syncs (i.e. removing a device) while
1226 * keeping the spa_namespace_lock held.
1227 */
1228 uint64_t
spa_vdev_config_enter(spa_t * spa)1229 spa_vdev_config_enter(spa_t *spa)
1230 {
1231 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1232
1233 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1234
1235 return (spa_last_synced_txg(spa) + 1);
1236 }
1237
1238 /*
1239 * Used in combination with spa_vdev_config_enter() to allow the syncing
1240 * of multiple transactions without releasing the spa_namespace_lock.
1241 */
1242 void
spa_vdev_config_exit(spa_t * spa,vdev_t * vd,uint64_t txg,int error,char * tag)1243 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1244 {
1245 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1246
1247 int config_changed = B_FALSE;
1248
1249 ASSERT(txg > spa_last_synced_txg(spa));
1250
1251 spa->spa_pending_vdev = NULL;
1252
1253 /*
1254 * Reassess the DTLs.
1255 */
1256 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE, B_FALSE);
1257
1258 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1259 config_changed = B_TRUE;
1260 spa->spa_config_generation++;
1261 }
1262
1263 /*
1264 * Verify the metaslab classes.
1265 */
1266 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1267 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1268 ASSERT(metaslab_class_validate(spa_embedded_log_class(spa)) == 0);
1269 ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0);
1270 ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0);
1271
1272 spa_config_exit(spa, SCL_ALL, spa);
1273
1274 /*
1275 * Panic the system if the specified tag requires it. This
1276 * is useful for ensuring that configurations are updated
1277 * transactionally.
1278 */
1279 if (zio_injection_enabled)
1280 zio_handle_panic_injection(spa, tag, 0);
1281
1282 /*
1283 * Note: this txg_wait_synced() is important because it ensures
1284 * that there won't be more than one config change per txg.
1285 * This allows us to use the txg as the generation number.
1286 */
1287 if (error == 0)
1288 txg_wait_synced(spa->spa_dsl_pool, txg);
1289
1290 if (vd != NULL) {
1291 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1292 if (vd->vdev_ops->vdev_op_leaf) {
1293 mutex_enter(&vd->vdev_initialize_lock);
1294 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED,
1295 NULL);
1296 mutex_exit(&vd->vdev_initialize_lock);
1297
1298 mutex_enter(&vd->vdev_trim_lock);
1299 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL);
1300 mutex_exit(&vd->vdev_trim_lock);
1301 }
1302
1303 /*
1304 * The vdev may be both a leaf and top-level device.
1305 */
1306 vdev_autotrim_stop_wait(vd);
1307
1308 spa_config_enter(spa, SCL_STATE_ALL, spa, RW_WRITER);
1309 vdev_free(vd);
1310 spa_config_exit(spa, SCL_STATE_ALL, spa);
1311 }
1312
1313 /*
1314 * If the config changed, update the config cache.
1315 */
1316 if (config_changed)
1317 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE);
1318 }
1319
1320 /*
1321 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1322 * locking of spa_vdev_enter(), we also want make sure the transactions have
1323 * synced to disk, and then update the global configuration cache with the new
1324 * information.
1325 */
1326 int
spa_vdev_exit(spa_t * spa,vdev_t * vd,uint64_t txg,int error)1327 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1328 {
1329 vdev_autotrim_restart(spa);
1330 vdev_rebuild_restart(spa);
1331
1332 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1333 mutex_exit(&spa_namespace_lock);
1334 mutex_exit(&spa->spa_vdev_top_lock);
1335
1336 return (error);
1337 }
1338
1339 /*
1340 * Lock the given spa_t for the purpose of changing vdev state.
1341 */
1342 void
spa_vdev_state_enter(spa_t * spa,int oplocks)1343 spa_vdev_state_enter(spa_t *spa, int oplocks)
1344 {
1345 int locks = SCL_STATE_ALL | oplocks;
1346
1347 /*
1348 * Root pools may need to read of the underlying devfs filesystem
1349 * when opening up a vdev. Unfortunately if we're holding the
1350 * SCL_ZIO lock it will result in a deadlock when we try to issue
1351 * the read from the root filesystem. Instead we "prefetch"
1352 * the associated vnodes that we need prior to opening the
1353 * underlying devices and cache them so that we can prevent
1354 * any I/O when we are doing the actual open.
1355 */
1356 if (spa_is_root(spa)) {
1357 int low = locks & ~(SCL_ZIO - 1);
1358 int high = locks & ~low;
1359
1360 spa_config_enter(spa, high, spa, RW_WRITER);
1361 vdev_hold(spa->spa_root_vdev);
1362 spa_config_enter(spa, low, spa, RW_WRITER);
1363 } else {
1364 spa_config_enter(spa, locks, spa, RW_WRITER);
1365 }
1366 spa->spa_vdev_locks = locks;
1367 }
1368
1369 int
spa_vdev_state_exit(spa_t * spa,vdev_t * vd,int error)1370 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1371 {
1372 boolean_t config_changed = B_FALSE;
1373 vdev_t *vdev_top;
1374
1375 if (vd == NULL || vd == spa->spa_root_vdev) {
1376 vdev_top = spa->spa_root_vdev;
1377 } else {
1378 vdev_top = vd->vdev_top;
1379 }
1380
1381 if (vd != NULL || error == 0)
1382 vdev_dtl_reassess(vdev_top, 0, 0, B_FALSE, B_FALSE);
1383
1384 if (vd != NULL) {
1385 if (vd != spa->spa_root_vdev)
1386 vdev_state_dirty(vdev_top);
1387
1388 config_changed = B_TRUE;
1389 spa->spa_config_generation++;
1390 }
1391
1392 if (spa_is_root(spa))
1393 vdev_rele(spa->spa_root_vdev);
1394
1395 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1396 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1397
1398 /*
1399 * If anything changed, wait for it to sync. This ensures that,
1400 * from the system administrator's perspective, zpool(8) commands
1401 * are synchronous. This is important for things like zpool offline:
1402 * when the command completes, you expect no further I/O from ZFS.
1403 */
1404 if (vd != NULL)
1405 txg_wait_synced(spa->spa_dsl_pool, 0);
1406
1407 /*
1408 * If the config changed, update the config cache.
1409 */
1410 if (config_changed) {
1411 mutex_enter(&spa_namespace_lock);
1412 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE);
1413 mutex_exit(&spa_namespace_lock);
1414 }
1415
1416 return (error);
1417 }
1418
1419 /*
1420 * ==========================================================================
1421 * Miscellaneous functions
1422 * ==========================================================================
1423 */
1424
1425 void
spa_activate_mos_feature(spa_t * spa,const char * feature,dmu_tx_t * tx)1426 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1427 {
1428 if (!nvlist_exists(spa->spa_label_features, feature)) {
1429 fnvlist_add_boolean(spa->spa_label_features, feature);
1430 /*
1431 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1432 * dirty the vdev config because lock SCL_CONFIG is not held.
1433 * Thankfully, in this case we don't need to dirty the config
1434 * because it will be written out anyway when we finish
1435 * creating the pool.
1436 */
1437 if (tx->tx_txg != TXG_INITIAL)
1438 vdev_config_dirty(spa->spa_root_vdev);
1439 }
1440 }
1441
1442 void
spa_deactivate_mos_feature(spa_t * spa,const char * feature)1443 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1444 {
1445 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1446 vdev_config_dirty(spa->spa_root_vdev);
1447 }
1448
1449 /*
1450 * Return the spa_t associated with given pool_guid, if it exists. If
1451 * device_guid is non-zero, determine whether the pool exists *and* contains
1452 * a device with the specified device_guid.
1453 */
1454 spa_t *
spa_by_guid(uint64_t pool_guid,uint64_t device_guid)1455 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1456 {
1457 spa_t *spa;
1458 avl_tree_t *t = &spa_namespace_avl;
1459
1460 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1461
1462 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1463 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1464 continue;
1465 if (spa->spa_root_vdev == NULL)
1466 continue;
1467 if (spa_guid(spa) == pool_guid) {
1468 if (device_guid == 0)
1469 break;
1470
1471 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1472 device_guid) != NULL)
1473 break;
1474
1475 /*
1476 * Check any devices we may be in the process of adding.
1477 */
1478 if (spa->spa_pending_vdev) {
1479 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1480 device_guid) != NULL)
1481 break;
1482 }
1483 }
1484 }
1485
1486 return (spa);
1487 }
1488
1489 /*
1490 * Determine whether a pool with the given pool_guid exists.
1491 */
1492 boolean_t
spa_guid_exists(uint64_t pool_guid,uint64_t device_guid)1493 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1494 {
1495 return (spa_by_guid(pool_guid, device_guid) != NULL);
1496 }
1497
1498 char *
spa_strdup(const char * s)1499 spa_strdup(const char *s)
1500 {
1501 size_t len;
1502 char *new;
1503
1504 len = strlen(s);
1505 new = kmem_alloc(len + 1, KM_SLEEP);
1506 bcopy(s, new, len);
1507 new[len] = '\0';
1508
1509 return (new);
1510 }
1511
1512 void
spa_strfree(char * s)1513 spa_strfree(char *s)
1514 {
1515 kmem_free(s, strlen(s) + 1);
1516 }
1517
1518 uint64_t
spa_generate_guid(spa_t * spa)1519 spa_generate_guid(spa_t *spa)
1520 {
1521 uint64_t guid;
1522
1523 if (spa != NULL) {
1524 do {
1525 (void) random_get_pseudo_bytes((void *)&guid,
1526 sizeof (guid));
1527 } while (guid == 0 || spa_guid_exists(spa_guid(spa), guid));
1528 } else {
1529 do {
1530 (void) random_get_pseudo_bytes((void *)&guid,
1531 sizeof (guid));
1532 } while (guid == 0 || spa_guid_exists(guid, 0));
1533 }
1534
1535 return (guid);
1536 }
1537
1538 void
snprintf_blkptr(char * buf,size_t buflen,const blkptr_t * bp)1539 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1540 {
1541 char type[256];
1542 char *checksum = NULL;
1543 char *compress = NULL;
1544
1545 if (bp != NULL) {
1546 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1547 dmu_object_byteswap_t bswap =
1548 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1549 (void) snprintf(type, sizeof (type), "bswap %s %s",
1550 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1551 "metadata" : "data",
1552 dmu_ot_byteswap[bswap].ob_name);
1553 } else {
1554 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1555 sizeof (type));
1556 }
1557 if (!BP_IS_EMBEDDED(bp)) {
1558 checksum =
1559 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1560 }
1561 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1562 }
1563
1564 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1565 compress);
1566 }
1567
1568 void
spa_freeze(spa_t * spa)1569 spa_freeze(spa_t *spa)
1570 {
1571 uint64_t freeze_txg = 0;
1572
1573 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1574 if (spa->spa_freeze_txg == UINT64_MAX) {
1575 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1576 spa->spa_freeze_txg = freeze_txg;
1577 }
1578 spa_config_exit(spa, SCL_ALL, FTAG);
1579 if (freeze_txg != 0)
1580 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1581 }
1582
1583 void
zfs_panic_recover(const char * fmt,...)1584 zfs_panic_recover(const char *fmt, ...)
1585 {
1586 va_list adx;
1587
1588 va_start(adx, fmt);
1589 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1590 va_end(adx);
1591 }
1592
1593 /*
1594 * This is a stripped-down version of strtoull, suitable only for converting
1595 * lowercase hexadecimal numbers that don't overflow.
1596 */
1597 uint64_t
zfs_strtonum(const char * str,char ** nptr)1598 zfs_strtonum(const char *str, char **nptr)
1599 {
1600 uint64_t val = 0;
1601 char c;
1602 int digit;
1603
1604 while ((c = *str) != '\0') {
1605 if (c >= '0' && c <= '9')
1606 digit = c - '0';
1607 else if (c >= 'a' && c <= 'f')
1608 digit = 10 + c - 'a';
1609 else
1610 break;
1611
1612 val *= 16;
1613 val += digit;
1614
1615 str++;
1616 }
1617
1618 if (nptr)
1619 *nptr = (char *)str;
1620
1621 return (val);
1622 }
1623
1624 void
spa_activate_allocation_classes(spa_t * spa,dmu_tx_t * tx)1625 spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx)
1626 {
1627 /*
1628 * We bump the feature refcount for each special vdev added to the pool
1629 */
1630 ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES));
1631 spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx);
1632 }
1633
1634 /*
1635 * ==========================================================================
1636 * Accessor functions
1637 * ==========================================================================
1638 */
1639
1640 boolean_t
spa_shutting_down(spa_t * spa)1641 spa_shutting_down(spa_t *spa)
1642 {
1643 return (spa->spa_async_suspended);
1644 }
1645
1646 dsl_pool_t *
spa_get_dsl(spa_t * spa)1647 spa_get_dsl(spa_t *spa)
1648 {
1649 return (spa->spa_dsl_pool);
1650 }
1651
1652 boolean_t
spa_is_initializing(spa_t * spa)1653 spa_is_initializing(spa_t *spa)
1654 {
1655 return (spa->spa_is_initializing);
1656 }
1657
1658 boolean_t
spa_indirect_vdevs_loaded(spa_t * spa)1659 spa_indirect_vdevs_loaded(spa_t *spa)
1660 {
1661 return (spa->spa_indirect_vdevs_loaded);
1662 }
1663
1664 blkptr_t *
spa_get_rootblkptr(spa_t * spa)1665 spa_get_rootblkptr(spa_t *spa)
1666 {
1667 return (&spa->spa_ubsync.ub_rootbp);
1668 }
1669
1670 void
spa_set_rootblkptr(spa_t * spa,const blkptr_t * bp)1671 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1672 {
1673 spa->spa_uberblock.ub_rootbp = *bp;
1674 }
1675
1676 void
spa_altroot(spa_t * spa,char * buf,size_t buflen)1677 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1678 {
1679 if (spa->spa_root == NULL)
1680 buf[0] = '\0';
1681 else
1682 (void) strncpy(buf, spa->spa_root, buflen);
1683 }
1684
1685 int
spa_sync_pass(spa_t * spa)1686 spa_sync_pass(spa_t *spa)
1687 {
1688 return (spa->spa_sync_pass);
1689 }
1690
1691 char *
spa_name(spa_t * spa)1692 spa_name(spa_t *spa)
1693 {
1694 return (spa->spa_name);
1695 }
1696
1697 uint64_t
spa_guid(spa_t * spa)1698 spa_guid(spa_t *spa)
1699 {
1700 dsl_pool_t *dp = spa_get_dsl(spa);
1701 uint64_t guid;
1702
1703 /*
1704 * If we fail to parse the config during spa_load(), we can go through
1705 * the error path (which posts an ereport) and end up here with no root
1706 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1707 * this case.
1708 */
1709 if (spa->spa_root_vdev == NULL)
1710 return (spa->spa_config_guid);
1711
1712 guid = spa->spa_last_synced_guid != 0 ?
1713 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1714
1715 /*
1716 * Return the most recently synced out guid unless we're
1717 * in syncing context.
1718 */
1719 if (dp && dsl_pool_sync_context(dp))
1720 return (spa->spa_root_vdev->vdev_guid);
1721 else
1722 return (guid);
1723 }
1724
1725 uint64_t
spa_load_guid(spa_t * spa)1726 spa_load_guid(spa_t *spa)
1727 {
1728 /*
1729 * This is a GUID that exists solely as a reference for the
1730 * purposes of the arc. It is generated at load time, and
1731 * is never written to persistent storage.
1732 */
1733 return (spa->spa_load_guid);
1734 }
1735
1736 uint64_t
spa_last_synced_txg(spa_t * spa)1737 spa_last_synced_txg(spa_t *spa)
1738 {
1739 return (spa->spa_ubsync.ub_txg);
1740 }
1741
1742 uint64_t
spa_first_txg(spa_t * spa)1743 spa_first_txg(spa_t *spa)
1744 {
1745 return (spa->spa_first_txg);
1746 }
1747
1748 uint64_t
spa_syncing_txg(spa_t * spa)1749 spa_syncing_txg(spa_t *spa)
1750 {
1751 return (spa->spa_syncing_txg);
1752 }
1753
1754 /*
1755 * Return the last txg where data can be dirtied. The final txgs
1756 * will be used to just clear out any deferred frees that remain.
1757 */
1758 uint64_t
spa_final_dirty_txg(spa_t * spa)1759 spa_final_dirty_txg(spa_t *spa)
1760 {
1761 return (spa->spa_final_txg - TXG_DEFER_SIZE);
1762 }
1763
1764 pool_state_t
spa_state(spa_t * spa)1765 spa_state(spa_t *spa)
1766 {
1767 return (spa->spa_state);
1768 }
1769
1770 spa_load_state_t
spa_load_state(spa_t * spa)1771 spa_load_state(spa_t *spa)
1772 {
1773 return (spa->spa_load_state);
1774 }
1775
1776 uint64_t
spa_freeze_txg(spa_t * spa)1777 spa_freeze_txg(spa_t *spa)
1778 {
1779 return (spa->spa_freeze_txg);
1780 }
1781
1782 /*
1783 * Return the inflated asize for a logical write in bytes. This is used by the
1784 * DMU to calculate the space a logical write will require on disk.
1785 * If lsize is smaller than the largest physical block size allocatable on this
1786 * pool we use its value instead, since the write will end up using the whole
1787 * block anyway.
1788 */
1789 uint64_t
spa_get_worst_case_asize(spa_t * spa,uint64_t lsize)1790 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1791 {
1792 if (lsize == 0)
1793 return (0); /* No inflation needed */
1794 return (MAX(lsize, 1 << spa->spa_max_ashift) * spa_asize_inflation);
1795 }
1796
1797 /*
1798 * Return the amount of slop space in bytes. It is typically 1/32 of the pool
1799 * (3.2%), minus the embedded log space. On very small pools, it may be
1800 * slightly larger than this. On very large pools, it will be capped to
1801 * the value of spa_max_slop. The embedded log space is not included in
1802 * spa_dspace. By subtracting it, the usable space (per "zfs list") is a
1803 * constant 97% of the total space, regardless of metaslab size (assuming the
1804 * default spa_slop_shift=5 and a non-tiny pool).
1805 *
1806 * See the comment above spa_slop_shift for more details.
1807 */
1808 uint64_t
spa_get_slop_space(spa_t * spa)1809 spa_get_slop_space(spa_t *spa)
1810 {
1811 uint64_t space = 0;
1812 uint64_t slop = 0;
1813
1814 /*
1815 * Make sure spa_dedup_dspace has been set.
1816 */
1817 if (spa->spa_dedup_dspace == ~0ULL)
1818 spa_update_dspace(spa);
1819
1820 /*
1821 * spa_get_dspace() includes the space only logically "used" by
1822 * deduplicated data, so since it's not useful to reserve more
1823 * space with more deduplicated data, we subtract that out here.
1824 */
1825 space = spa_get_dspace(spa) - spa->spa_dedup_dspace;
1826 slop = MIN(space >> spa_slop_shift, spa_max_slop);
1827
1828 /*
1829 * Subtract the embedded log space, but no more than half the (3.2%)
1830 * unusable space. Note, the "no more than half" is only relevant if
1831 * zfs_embedded_slog_min_ms >> spa_slop_shift < 2, which is not true by
1832 * default.
1833 */
1834 uint64_t embedded_log =
1835 metaslab_class_get_dspace(spa_embedded_log_class(spa));
1836 slop -= MIN(embedded_log, slop >> 1);
1837
1838 /*
1839 * Slop space should be at least spa_min_slop, but no more than half
1840 * the entire pool.
1841 */
1842 slop = MAX(slop, MIN(space >> 1, spa_min_slop));
1843 return (slop);
1844 }
1845
1846 uint64_t
spa_get_dspace(spa_t * spa)1847 spa_get_dspace(spa_t *spa)
1848 {
1849 return (spa->spa_dspace);
1850 }
1851
1852 uint64_t
spa_get_checkpoint_space(spa_t * spa)1853 spa_get_checkpoint_space(spa_t *spa)
1854 {
1855 return (spa->spa_checkpoint_info.sci_dspace);
1856 }
1857
1858 void
spa_update_dspace(spa_t * spa)1859 spa_update_dspace(spa_t *spa)
1860 {
1861 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1862 ddt_get_dedup_dspace(spa);
1863 if (spa->spa_vdev_removal != NULL) {
1864 /*
1865 * We can't allocate from the removing device, so subtract
1866 * its size if it was included in dspace (i.e. if this is a
1867 * normal-class vdev, not special/dedup). This prevents the
1868 * DMU/DSL from filling up the (now smaller) pool while we
1869 * are in the middle of removing the device.
1870 *
1871 * Note that the DMU/DSL doesn't actually know or care
1872 * how much space is allocated (it does its own tracking
1873 * of how much space has been logically used). So it
1874 * doesn't matter that the data we are moving may be
1875 * allocated twice (on the old device and the new
1876 * device).
1877 */
1878 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1879 vdev_t *vd =
1880 vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
1881 /*
1882 * If the stars align, we can wind up here after
1883 * vdev_remove_complete() has cleared vd->vdev_mg but before
1884 * spa->spa_vdev_removal gets cleared, so we must check before
1885 * we dereference.
1886 */
1887 if (vd->vdev_mg &&
1888 vd->vdev_mg->mg_class == spa_normal_class(spa)) {
1889 spa->spa_dspace -= spa_deflate(spa) ?
1890 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
1891 }
1892 spa_config_exit(spa, SCL_VDEV, FTAG);
1893 }
1894 }
1895
1896 /*
1897 * Return the failure mode that has been set to this pool. The default
1898 * behavior will be to block all I/Os when a complete failure occurs.
1899 */
1900 uint64_t
spa_get_failmode(spa_t * spa)1901 spa_get_failmode(spa_t *spa)
1902 {
1903 return (spa->spa_failmode);
1904 }
1905
1906 boolean_t
spa_suspended(spa_t * spa)1907 spa_suspended(spa_t *spa)
1908 {
1909 return (spa->spa_suspended != ZIO_SUSPEND_NONE);
1910 }
1911
1912 uint64_t
spa_version(spa_t * spa)1913 spa_version(spa_t *spa)
1914 {
1915 return (spa->spa_ubsync.ub_version);
1916 }
1917
1918 boolean_t
spa_deflate(spa_t * spa)1919 spa_deflate(spa_t *spa)
1920 {
1921 return (spa->spa_deflate);
1922 }
1923
1924 metaslab_class_t *
spa_normal_class(spa_t * spa)1925 spa_normal_class(spa_t *spa)
1926 {
1927 return (spa->spa_normal_class);
1928 }
1929
1930 metaslab_class_t *
spa_log_class(spa_t * spa)1931 spa_log_class(spa_t *spa)
1932 {
1933 return (spa->spa_log_class);
1934 }
1935
1936 metaslab_class_t *
spa_embedded_log_class(spa_t * spa)1937 spa_embedded_log_class(spa_t *spa)
1938 {
1939 return (spa->spa_embedded_log_class);
1940 }
1941
1942 metaslab_class_t *
spa_special_class(spa_t * spa)1943 spa_special_class(spa_t *spa)
1944 {
1945 return (spa->spa_special_class);
1946 }
1947
1948 metaslab_class_t *
spa_dedup_class(spa_t * spa)1949 spa_dedup_class(spa_t *spa)
1950 {
1951 return (spa->spa_dedup_class);
1952 }
1953
1954 /*
1955 * Locate an appropriate allocation class
1956 */
1957 metaslab_class_t *
spa_preferred_class(spa_t * spa,uint64_t size,dmu_object_type_t objtype,uint_t level,uint_t special_smallblk)1958 spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype,
1959 uint_t level, uint_t special_smallblk)
1960 {
1961 /*
1962 * ZIL allocations determine their class in zio_alloc_zil().
1963 */
1964 ASSERT(objtype != DMU_OT_INTENT_LOG);
1965
1966 boolean_t has_special_class = spa->spa_special_class->mc_groups != 0;
1967
1968 if (DMU_OT_IS_DDT(objtype)) {
1969 if (spa->spa_dedup_class->mc_groups != 0)
1970 return (spa_dedup_class(spa));
1971 else if (has_special_class && zfs_ddt_data_is_special)
1972 return (spa_special_class(spa));
1973 else
1974 return (spa_normal_class(spa));
1975 }
1976
1977 /* Indirect blocks for user data can land in special if allowed */
1978 if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) {
1979 if (has_special_class && zfs_user_indirect_is_special)
1980 return (spa_special_class(spa));
1981 else
1982 return (spa_normal_class(spa));
1983 }
1984
1985 if (DMU_OT_IS_METADATA(objtype) || level > 0) {
1986 if (has_special_class)
1987 return (spa_special_class(spa));
1988 else
1989 return (spa_normal_class(spa));
1990 }
1991
1992 /*
1993 * Allow small file blocks in special class in some cases (like
1994 * for the dRAID vdev feature). But always leave a reserve of
1995 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1996 */
1997 if (DMU_OT_IS_FILE(objtype) &&
1998 has_special_class && size <= special_smallblk) {
1999 metaslab_class_t *special = spa_special_class(spa);
2000 uint64_t alloc = metaslab_class_get_alloc(special);
2001 uint64_t space = metaslab_class_get_space(special);
2002 uint64_t limit =
2003 (space * (100 - zfs_special_class_metadata_reserve_pct))
2004 / 100;
2005
2006 if (alloc < limit)
2007 return (special);
2008 }
2009
2010 return (spa_normal_class(spa));
2011 }
2012
2013 void
spa_evicting_os_register(spa_t * spa,objset_t * os)2014 spa_evicting_os_register(spa_t *spa, objset_t *os)
2015 {
2016 mutex_enter(&spa->spa_evicting_os_lock);
2017 list_insert_head(&spa->spa_evicting_os_list, os);
2018 mutex_exit(&spa->spa_evicting_os_lock);
2019 }
2020
2021 void
spa_evicting_os_deregister(spa_t * spa,objset_t * os)2022 spa_evicting_os_deregister(spa_t *spa, objset_t *os)
2023 {
2024 mutex_enter(&spa->spa_evicting_os_lock);
2025 list_remove(&spa->spa_evicting_os_list, os);
2026 cv_broadcast(&spa->spa_evicting_os_cv);
2027 mutex_exit(&spa->spa_evicting_os_lock);
2028 }
2029
2030 void
spa_evicting_os_wait(spa_t * spa)2031 spa_evicting_os_wait(spa_t *spa)
2032 {
2033 mutex_enter(&spa->spa_evicting_os_lock);
2034 while (!list_is_empty(&spa->spa_evicting_os_list))
2035 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
2036 mutex_exit(&spa->spa_evicting_os_lock);
2037
2038 dmu_buf_user_evict_wait();
2039 }
2040
2041 int
spa_max_replication(spa_t * spa)2042 spa_max_replication(spa_t *spa)
2043 {
2044 /*
2045 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
2046 * handle BPs with more than one DVA allocated. Set our max
2047 * replication level accordingly.
2048 */
2049 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
2050 return (1);
2051 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
2052 }
2053
2054 int
spa_prev_software_version(spa_t * spa)2055 spa_prev_software_version(spa_t *spa)
2056 {
2057 return (spa->spa_prev_software_version);
2058 }
2059
2060 uint64_t
spa_deadman_synctime(spa_t * spa)2061 spa_deadman_synctime(spa_t *spa)
2062 {
2063 return (spa->spa_deadman_synctime);
2064 }
2065
2066 spa_autotrim_t
spa_get_autotrim(spa_t * spa)2067 spa_get_autotrim(spa_t *spa)
2068 {
2069 return (spa->spa_autotrim);
2070 }
2071
2072 uint64_t
spa_deadman_ziotime(spa_t * spa)2073 spa_deadman_ziotime(spa_t *spa)
2074 {
2075 return (spa->spa_deadman_ziotime);
2076 }
2077
2078 uint64_t
spa_get_deadman_failmode(spa_t * spa)2079 spa_get_deadman_failmode(spa_t *spa)
2080 {
2081 return (spa->spa_deadman_failmode);
2082 }
2083
2084 void
spa_set_deadman_failmode(spa_t * spa,const char * failmode)2085 spa_set_deadman_failmode(spa_t *spa, const char *failmode)
2086 {
2087 if (strcmp(failmode, "wait") == 0)
2088 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
2089 else if (strcmp(failmode, "continue") == 0)
2090 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_CONTINUE;
2091 else if (strcmp(failmode, "panic") == 0)
2092 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
2093 else
2094 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
2095 }
2096
2097 void
spa_set_deadman_ziotime(hrtime_t ns)2098 spa_set_deadman_ziotime(hrtime_t ns)
2099 {
2100 spa_t *spa = NULL;
2101
2102 if (spa_mode_global != SPA_MODE_UNINIT) {
2103 mutex_enter(&spa_namespace_lock);
2104 while ((spa = spa_next(spa)) != NULL)
2105 spa->spa_deadman_ziotime = ns;
2106 mutex_exit(&spa_namespace_lock);
2107 }
2108 }
2109
2110 void
spa_set_deadman_synctime(hrtime_t ns)2111 spa_set_deadman_synctime(hrtime_t ns)
2112 {
2113 spa_t *spa = NULL;
2114
2115 if (spa_mode_global != SPA_MODE_UNINIT) {
2116 mutex_enter(&spa_namespace_lock);
2117 while ((spa = spa_next(spa)) != NULL)
2118 spa->spa_deadman_synctime = ns;
2119 mutex_exit(&spa_namespace_lock);
2120 }
2121 }
2122
2123 uint64_t
dva_get_dsize_sync(spa_t * spa,const dva_t * dva)2124 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
2125 {
2126 uint64_t asize = DVA_GET_ASIZE(dva);
2127 uint64_t dsize = asize;
2128
2129 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2130
2131 if (asize != 0 && spa->spa_deflate) {
2132 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
2133 if (vd != NULL)
2134 dsize = (asize >> SPA_MINBLOCKSHIFT) *
2135 vd->vdev_deflate_ratio;
2136 }
2137
2138 return (dsize);
2139 }
2140
2141 uint64_t
bp_get_dsize_sync(spa_t * spa,const blkptr_t * bp)2142 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
2143 {
2144 uint64_t dsize = 0;
2145
2146 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2147 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2148
2149 return (dsize);
2150 }
2151
2152 uint64_t
bp_get_dsize(spa_t * spa,const blkptr_t * bp)2153 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
2154 {
2155 uint64_t dsize = 0;
2156
2157 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2158
2159 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2160 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2161
2162 spa_config_exit(spa, SCL_VDEV, FTAG);
2163
2164 return (dsize);
2165 }
2166
2167 uint64_t
spa_dirty_data(spa_t * spa)2168 spa_dirty_data(spa_t *spa)
2169 {
2170 return (spa->spa_dsl_pool->dp_dirty_total);
2171 }
2172
2173 /*
2174 * ==========================================================================
2175 * SPA Import Progress Routines
2176 * ==========================================================================
2177 */
2178
2179 typedef struct spa_import_progress {
2180 uint64_t pool_guid; /* unique id for updates */
2181 char *pool_name;
2182 spa_load_state_t spa_load_state;
2183 uint64_t mmp_sec_remaining; /* MMP activity check */
2184 uint64_t spa_load_max_txg; /* rewind txg */
2185 procfs_list_node_t smh_node;
2186 } spa_import_progress_t;
2187
2188 spa_history_list_t *spa_import_progress_list = NULL;
2189
2190 static int
spa_import_progress_show_header(struct seq_file * f)2191 spa_import_progress_show_header(struct seq_file *f)
2192 {
2193 seq_printf(f, "%-20s %-14s %-14s %-12s %s\n", "pool_guid",
2194 "load_state", "multihost_secs", "max_txg",
2195 "pool_name");
2196 return (0);
2197 }
2198
2199 static int
spa_import_progress_show(struct seq_file * f,void * data)2200 spa_import_progress_show(struct seq_file *f, void *data)
2201 {
2202 spa_import_progress_t *sip = (spa_import_progress_t *)data;
2203
2204 seq_printf(f, "%-20llu %-14llu %-14llu %-12llu %s\n",
2205 (u_longlong_t)sip->pool_guid, (u_longlong_t)sip->spa_load_state,
2206 (u_longlong_t)sip->mmp_sec_remaining,
2207 (u_longlong_t)sip->spa_load_max_txg,
2208 (sip->pool_name ? sip->pool_name : "-"));
2209
2210 return (0);
2211 }
2212
2213 /* Remove oldest elements from list until there are no more than 'size' left */
2214 static void
spa_import_progress_truncate(spa_history_list_t * shl,unsigned int size)2215 spa_import_progress_truncate(spa_history_list_t *shl, unsigned int size)
2216 {
2217 spa_import_progress_t *sip;
2218 while (shl->size > size) {
2219 sip = list_remove_head(&shl->procfs_list.pl_list);
2220 if (sip->pool_name)
2221 spa_strfree(sip->pool_name);
2222 kmem_free(sip, sizeof (spa_import_progress_t));
2223 shl->size--;
2224 }
2225
2226 IMPLY(size == 0, list_is_empty(&shl->procfs_list.pl_list));
2227 }
2228
2229 static void
spa_import_progress_init(void)2230 spa_import_progress_init(void)
2231 {
2232 spa_import_progress_list = kmem_zalloc(sizeof (spa_history_list_t),
2233 KM_SLEEP);
2234
2235 spa_import_progress_list->size = 0;
2236
2237 spa_import_progress_list->procfs_list.pl_private =
2238 spa_import_progress_list;
2239
2240 procfs_list_install("zfs",
2241 NULL,
2242 "import_progress",
2243 0644,
2244 &spa_import_progress_list->procfs_list,
2245 spa_import_progress_show,
2246 spa_import_progress_show_header,
2247 NULL,
2248 offsetof(spa_import_progress_t, smh_node));
2249 }
2250
2251 static void
spa_import_progress_destroy(void)2252 spa_import_progress_destroy(void)
2253 {
2254 spa_history_list_t *shl = spa_import_progress_list;
2255 procfs_list_uninstall(&shl->procfs_list);
2256 spa_import_progress_truncate(shl, 0);
2257 procfs_list_destroy(&shl->procfs_list);
2258 kmem_free(shl, sizeof (spa_history_list_t));
2259 }
2260
2261 int
spa_import_progress_set_state(uint64_t pool_guid,spa_load_state_t load_state)2262 spa_import_progress_set_state(uint64_t pool_guid,
2263 spa_load_state_t load_state)
2264 {
2265 spa_history_list_t *shl = spa_import_progress_list;
2266 spa_import_progress_t *sip;
2267 int error = ENOENT;
2268
2269 if (shl->size == 0)
2270 return (0);
2271
2272 mutex_enter(&shl->procfs_list.pl_lock);
2273 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2274 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2275 if (sip->pool_guid == pool_guid) {
2276 sip->spa_load_state = load_state;
2277 error = 0;
2278 break;
2279 }
2280 }
2281 mutex_exit(&shl->procfs_list.pl_lock);
2282
2283 return (error);
2284 }
2285
2286 int
spa_import_progress_set_max_txg(uint64_t pool_guid,uint64_t load_max_txg)2287 spa_import_progress_set_max_txg(uint64_t pool_guid, uint64_t load_max_txg)
2288 {
2289 spa_history_list_t *shl = spa_import_progress_list;
2290 spa_import_progress_t *sip;
2291 int error = ENOENT;
2292
2293 if (shl->size == 0)
2294 return (0);
2295
2296 mutex_enter(&shl->procfs_list.pl_lock);
2297 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2298 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2299 if (sip->pool_guid == pool_guid) {
2300 sip->spa_load_max_txg = load_max_txg;
2301 error = 0;
2302 break;
2303 }
2304 }
2305 mutex_exit(&shl->procfs_list.pl_lock);
2306
2307 return (error);
2308 }
2309
2310 int
spa_import_progress_set_mmp_check(uint64_t pool_guid,uint64_t mmp_sec_remaining)2311 spa_import_progress_set_mmp_check(uint64_t pool_guid,
2312 uint64_t mmp_sec_remaining)
2313 {
2314 spa_history_list_t *shl = spa_import_progress_list;
2315 spa_import_progress_t *sip;
2316 int error = ENOENT;
2317
2318 if (shl->size == 0)
2319 return (0);
2320
2321 mutex_enter(&shl->procfs_list.pl_lock);
2322 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2323 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2324 if (sip->pool_guid == pool_guid) {
2325 sip->mmp_sec_remaining = mmp_sec_remaining;
2326 error = 0;
2327 break;
2328 }
2329 }
2330 mutex_exit(&shl->procfs_list.pl_lock);
2331
2332 return (error);
2333 }
2334
2335 /*
2336 * A new import is in progress, add an entry.
2337 */
2338 void
spa_import_progress_add(spa_t * spa)2339 spa_import_progress_add(spa_t *spa)
2340 {
2341 spa_history_list_t *shl = spa_import_progress_list;
2342 spa_import_progress_t *sip;
2343 char *poolname = NULL;
2344
2345 sip = kmem_zalloc(sizeof (spa_import_progress_t), KM_SLEEP);
2346 sip->pool_guid = spa_guid(spa);
2347
2348 (void) nvlist_lookup_string(spa->spa_config, ZPOOL_CONFIG_POOL_NAME,
2349 &poolname);
2350 if (poolname == NULL)
2351 poolname = spa_name(spa);
2352 sip->pool_name = spa_strdup(poolname);
2353 sip->spa_load_state = spa_load_state(spa);
2354
2355 mutex_enter(&shl->procfs_list.pl_lock);
2356 procfs_list_add(&shl->procfs_list, sip);
2357 shl->size++;
2358 mutex_exit(&shl->procfs_list.pl_lock);
2359 }
2360
2361 void
spa_import_progress_remove(uint64_t pool_guid)2362 spa_import_progress_remove(uint64_t pool_guid)
2363 {
2364 spa_history_list_t *shl = spa_import_progress_list;
2365 spa_import_progress_t *sip;
2366
2367 mutex_enter(&shl->procfs_list.pl_lock);
2368 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2369 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2370 if (sip->pool_guid == pool_guid) {
2371 if (sip->pool_name)
2372 spa_strfree(sip->pool_name);
2373 list_remove(&shl->procfs_list.pl_list, sip);
2374 shl->size--;
2375 kmem_free(sip, sizeof (spa_import_progress_t));
2376 break;
2377 }
2378 }
2379 mutex_exit(&shl->procfs_list.pl_lock);
2380 }
2381
2382 /*
2383 * ==========================================================================
2384 * Initialization and Termination
2385 * ==========================================================================
2386 */
2387
2388 static int
spa_name_compare(const void * a1,const void * a2)2389 spa_name_compare(const void *a1, const void *a2)
2390 {
2391 const spa_t *s1 = a1;
2392 const spa_t *s2 = a2;
2393 int s;
2394
2395 s = strcmp(s1->spa_name, s2->spa_name);
2396
2397 return (TREE_ISIGN(s));
2398 }
2399
2400 void
spa_boot_init(void)2401 spa_boot_init(void)
2402 {
2403 spa_config_load();
2404 }
2405
2406 void
spa_init(spa_mode_t mode)2407 spa_init(spa_mode_t mode)
2408 {
2409 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
2410 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
2411 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
2412 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
2413
2414 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
2415 offsetof(spa_t, spa_avl));
2416
2417 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
2418 offsetof(spa_aux_t, aux_avl));
2419
2420 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
2421 offsetof(spa_aux_t, aux_avl));
2422
2423 spa_mode_global = mode;
2424
2425 #ifndef _KERNEL
2426 if (spa_mode_global != SPA_MODE_READ && dprintf_find_string("watch")) {
2427 struct sigaction sa;
2428
2429 sa.sa_flags = SA_SIGINFO;
2430 sigemptyset(&sa.sa_mask);
2431 sa.sa_sigaction = arc_buf_sigsegv;
2432
2433 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
2434 perror("could not enable watchpoints: "
2435 "sigaction(SIGSEGV, ...) = ");
2436 } else {
2437 arc_watch = B_TRUE;
2438 }
2439 }
2440 #endif
2441
2442 fm_init();
2443 zfs_refcount_init();
2444 unique_init();
2445 zfs_btree_init();
2446 metaslab_stat_init();
2447 ddt_init();
2448 zio_init();
2449 dmu_init();
2450 zil_init();
2451 vdev_cache_stat_init();
2452 vdev_mirror_stat_init();
2453 vdev_raidz_math_init();
2454 vdev_file_init();
2455 zfs_prop_init();
2456 zpool_prop_init();
2457 zpool_feature_init();
2458 spa_config_load();
2459 l2arc_start();
2460 scan_init();
2461 qat_init();
2462 spa_import_progress_init();
2463 }
2464
2465 void
spa_fini(void)2466 spa_fini(void)
2467 {
2468 l2arc_stop();
2469
2470 spa_evict_all();
2471
2472 vdev_file_fini();
2473 vdev_cache_stat_fini();
2474 vdev_mirror_stat_fini();
2475 vdev_raidz_math_fini();
2476 zil_fini();
2477 dmu_fini();
2478 zio_fini();
2479 ddt_fini();
2480 metaslab_stat_fini();
2481 zfs_btree_fini();
2482 unique_fini();
2483 zfs_refcount_fini();
2484 fm_fini();
2485 scan_fini();
2486 qat_fini();
2487 spa_import_progress_destroy();
2488
2489 avl_destroy(&spa_namespace_avl);
2490 avl_destroy(&spa_spare_avl);
2491 avl_destroy(&spa_l2cache_avl);
2492
2493 cv_destroy(&spa_namespace_cv);
2494 mutex_destroy(&spa_namespace_lock);
2495 mutex_destroy(&spa_spare_lock);
2496 mutex_destroy(&spa_l2cache_lock);
2497 }
2498
2499 /*
2500 * Return whether this pool has a dedicated slog device. No locking needed.
2501 * It's not a problem if the wrong answer is returned as it's only for
2502 * performance and not correctness.
2503 */
2504 boolean_t
spa_has_slogs(spa_t * spa)2505 spa_has_slogs(spa_t *spa)
2506 {
2507 return (spa->spa_log_class->mc_groups != 0);
2508 }
2509
2510 spa_log_state_t
spa_get_log_state(spa_t * spa)2511 spa_get_log_state(spa_t *spa)
2512 {
2513 return (spa->spa_log_state);
2514 }
2515
2516 void
spa_set_log_state(spa_t * spa,spa_log_state_t state)2517 spa_set_log_state(spa_t *spa, spa_log_state_t state)
2518 {
2519 spa->spa_log_state = state;
2520 }
2521
2522 boolean_t
spa_is_root(spa_t * spa)2523 spa_is_root(spa_t *spa)
2524 {
2525 return (spa->spa_is_root);
2526 }
2527
2528 boolean_t
spa_writeable(spa_t * spa)2529 spa_writeable(spa_t *spa)
2530 {
2531 return (!!(spa->spa_mode & SPA_MODE_WRITE) && spa->spa_trust_config);
2532 }
2533
2534 /*
2535 * Returns true if there is a pending sync task in any of the current
2536 * syncing txg, the current quiescing txg, or the current open txg.
2537 */
2538 boolean_t
spa_has_pending_synctask(spa_t * spa)2539 spa_has_pending_synctask(spa_t *spa)
2540 {
2541 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
2542 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
2543 }
2544
2545 spa_mode_t
spa_mode(spa_t * spa)2546 spa_mode(spa_t *spa)
2547 {
2548 return (spa->spa_mode);
2549 }
2550
2551 uint64_t
spa_bootfs(spa_t * spa)2552 spa_bootfs(spa_t *spa)
2553 {
2554 return (spa->spa_bootfs);
2555 }
2556
2557 uint64_t
spa_delegation(spa_t * spa)2558 spa_delegation(spa_t *spa)
2559 {
2560 return (spa->spa_delegation);
2561 }
2562
2563 objset_t *
spa_meta_objset(spa_t * spa)2564 spa_meta_objset(spa_t *spa)
2565 {
2566 return (spa->spa_meta_objset);
2567 }
2568
2569 enum zio_checksum
spa_dedup_checksum(spa_t * spa)2570 spa_dedup_checksum(spa_t *spa)
2571 {
2572 return (spa->spa_dedup_checksum);
2573 }
2574
2575 /*
2576 * Reset pool scan stat per scan pass (or reboot).
2577 */
2578 void
spa_scan_stat_init(spa_t * spa)2579 spa_scan_stat_init(spa_t *spa)
2580 {
2581 /* data not stored on disk */
2582 spa->spa_scan_pass_start = gethrestime_sec();
2583 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
2584 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
2585 else
2586 spa->spa_scan_pass_scrub_pause = 0;
2587 spa->spa_scan_pass_scrub_spent_paused = 0;
2588 spa->spa_scan_pass_exam = 0;
2589 spa->spa_scan_pass_issued = 0;
2590 }
2591
2592 /*
2593 * Get scan stats for zpool status reports
2594 */
2595 int
spa_scan_get_stats(spa_t * spa,pool_scan_stat_t * ps)2596 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2597 {
2598 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2599
2600 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2601 return (SET_ERROR(ENOENT));
2602 bzero(ps, sizeof (pool_scan_stat_t));
2603
2604 /* data stored on disk */
2605 ps->pss_func = scn->scn_phys.scn_func;
2606 ps->pss_state = scn->scn_phys.scn_state;
2607 ps->pss_start_time = scn->scn_phys.scn_start_time;
2608 ps->pss_end_time = scn->scn_phys.scn_end_time;
2609 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
2610 ps->pss_examined = scn->scn_phys.scn_examined;
2611 ps->pss_to_process = scn->scn_phys.scn_to_process;
2612 ps->pss_processed = scn->scn_phys.scn_processed;
2613 ps->pss_errors = scn->scn_phys.scn_errors;
2614
2615 /* data not stored on disk */
2616 ps->pss_pass_exam = spa->spa_scan_pass_exam;
2617 ps->pss_pass_start = spa->spa_scan_pass_start;
2618 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
2619 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
2620 ps->pss_pass_issued = spa->spa_scan_pass_issued;
2621 ps->pss_issued =
2622 scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
2623
2624 return (0);
2625 }
2626
2627 int
spa_maxblocksize(spa_t * spa)2628 spa_maxblocksize(spa_t *spa)
2629 {
2630 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2631 return (SPA_MAXBLOCKSIZE);
2632 else
2633 return (SPA_OLD_MAXBLOCKSIZE);
2634 }
2635
2636
2637 /*
2638 * Returns the txg that the last device removal completed. No indirect mappings
2639 * have been added since this txg.
2640 */
2641 uint64_t
spa_get_last_removal_txg(spa_t * spa)2642 spa_get_last_removal_txg(spa_t *spa)
2643 {
2644 uint64_t vdevid;
2645 uint64_t ret = -1ULL;
2646
2647 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2648 /*
2649 * sr_prev_indirect_vdev is only modified while holding all the
2650 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2651 * examining it.
2652 */
2653 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
2654
2655 while (vdevid != -1ULL) {
2656 vdev_t *vd = vdev_lookup_top(spa, vdevid);
2657 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
2658
2659 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
2660
2661 /*
2662 * If the removal did not remap any data, we don't care.
2663 */
2664 if (vdev_indirect_births_count(vib) != 0) {
2665 ret = vdev_indirect_births_last_entry_txg(vib);
2666 break;
2667 }
2668
2669 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
2670 }
2671 spa_config_exit(spa, SCL_VDEV, FTAG);
2672
2673 IMPLY(ret != -1ULL,
2674 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
2675
2676 return (ret);
2677 }
2678
2679 int
spa_maxdnodesize(spa_t * spa)2680 spa_maxdnodesize(spa_t *spa)
2681 {
2682 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2683 return (DNODE_MAX_SIZE);
2684 else
2685 return (DNODE_MIN_SIZE);
2686 }
2687
2688 boolean_t
spa_multihost(spa_t * spa)2689 spa_multihost(spa_t *spa)
2690 {
2691 return (spa->spa_multihost ? B_TRUE : B_FALSE);
2692 }
2693
2694 uint32_t
spa_get_hostid(spa_t * spa)2695 spa_get_hostid(spa_t *spa)
2696 {
2697 return (spa->spa_hostid);
2698 }
2699
2700 boolean_t
spa_trust_config(spa_t * spa)2701 spa_trust_config(spa_t *spa)
2702 {
2703 return (spa->spa_trust_config);
2704 }
2705
2706 uint64_t
spa_missing_tvds_allowed(spa_t * spa)2707 spa_missing_tvds_allowed(spa_t *spa)
2708 {
2709 return (spa->spa_missing_tvds_allowed);
2710 }
2711
2712 space_map_t *
spa_syncing_log_sm(spa_t * spa)2713 spa_syncing_log_sm(spa_t *spa)
2714 {
2715 return (spa->spa_syncing_log_sm);
2716 }
2717
2718 void
spa_set_missing_tvds(spa_t * spa,uint64_t missing)2719 spa_set_missing_tvds(spa_t *spa, uint64_t missing)
2720 {
2721 spa->spa_missing_tvds = missing;
2722 }
2723
2724 /*
2725 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2726 */
2727 const char *
spa_state_to_name(spa_t * spa)2728 spa_state_to_name(spa_t *spa)
2729 {
2730 ASSERT3P(spa, !=, NULL);
2731
2732 /*
2733 * it is possible for the spa to exist, without root vdev
2734 * as the spa transitions during import/export
2735 */
2736 vdev_t *rvd = spa->spa_root_vdev;
2737 if (rvd == NULL) {
2738 return ("TRANSITIONING");
2739 }
2740 vdev_state_t state = rvd->vdev_state;
2741 vdev_aux_t aux = rvd->vdev_stat.vs_aux;
2742
2743 if (spa_suspended(spa) &&
2744 (spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE))
2745 return ("SUSPENDED");
2746
2747 switch (state) {
2748 case VDEV_STATE_CLOSED:
2749 case VDEV_STATE_OFFLINE:
2750 return ("OFFLINE");
2751 case VDEV_STATE_REMOVED:
2752 return ("REMOVED");
2753 case VDEV_STATE_CANT_OPEN:
2754 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
2755 return ("FAULTED");
2756 else if (aux == VDEV_AUX_SPLIT_POOL)
2757 return ("SPLIT");
2758 else
2759 return ("UNAVAIL");
2760 case VDEV_STATE_FAULTED:
2761 return ("FAULTED");
2762 case VDEV_STATE_DEGRADED:
2763 return ("DEGRADED");
2764 case VDEV_STATE_HEALTHY:
2765 return ("ONLINE");
2766 default:
2767 break;
2768 }
2769
2770 return ("UNKNOWN");
2771 }
2772
2773 boolean_t
spa_top_vdevs_spacemap_addressable(spa_t * spa)2774 spa_top_vdevs_spacemap_addressable(spa_t *spa)
2775 {
2776 vdev_t *rvd = spa->spa_root_vdev;
2777 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2778 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
2779 return (B_FALSE);
2780 }
2781 return (B_TRUE);
2782 }
2783
2784 boolean_t
spa_has_checkpoint(spa_t * spa)2785 spa_has_checkpoint(spa_t *spa)
2786 {
2787 return (spa->spa_checkpoint_txg != 0);
2788 }
2789
2790 boolean_t
spa_importing_readonly_checkpoint(spa_t * spa)2791 spa_importing_readonly_checkpoint(spa_t *spa)
2792 {
2793 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
2794 spa->spa_mode == SPA_MODE_READ);
2795 }
2796
2797 uint64_t
spa_min_claim_txg(spa_t * spa)2798 spa_min_claim_txg(spa_t *spa)
2799 {
2800 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
2801
2802 if (checkpoint_txg != 0)
2803 return (checkpoint_txg + 1);
2804
2805 return (spa->spa_first_txg);
2806 }
2807
2808 /*
2809 * If there is a checkpoint, async destroys may consume more space from
2810 * the pool instead of freeing it. In an attempt to save the pool from
2811 * getting suspended when it is about to run out of space, we stop
2812 * processing async destroys.
2813 */
2814 boolean_t
spa_suspend_async_destroy(spa_t * spa)2815 spa_suspend_async_destroy(spa_t *spa)
2816 {
2817 dsl_pool_t *dp = spa_get_dsl(spa);
2818
2819 uint64_t unreserved = dsl_pool_unreserved_space(dp,
2820 ZFS_SPACE_CHECK_EXTRA_RESERVED);
2821 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
2822 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
2823
2824 if (spa_has_checkpoint(spa) && avail == 0)
2825 return (B_TRUE);
2826
2827 return (B_FALSE);
2828 }
2829
2830 #if defined(_KERNEL)
2831
2832 int
param_set_deadman_failmode_common(const char * val)2833 param_set_deadman_failmode_common(const char *val)
2834 {
2835 spa_t *spa = NULL;
2836 char *p;
2837
2838 if (val == NULL)
2839 return (SET_ERROR(EINVAL));
2840
2841 if ((p = strchr(val, '\n')) != NULL)
2842 *p = '\0';
2843
2844 if (strcmp(val, "wait") != 0 && strcmp(val, "continue") != 0 &&
2845 strcmp(val, "panic"))
2846 return (SET_ERROR(EINVAL));
2847
2848 if (spa_mode_global != SPA_MODE_UNINIT) {
2849 mutex_enter(&spa_namespace_lock);
2850 while ((spa = spa_next(spa)) != NULL)
2851 spa_set_deadman_failmode(spa, val);
2852 mutex_exit(&spa_namespace_lock);
2853 }
2854
2855 return (0);
2856 }
2857 #endif
2858
2859 /* Namespace manipulation */
2860 EXPORT_SYMBOL(spa_lookup);
2861 EXPORT_SYMBOL(spa_add);
2862 EXPORT_SYMBOL(spa_remove);
2863 EXPORT_SYMBOL(spa_next);
2864
2865 /* Refcount functions */
2866 EXPORT_SYMBOL(spa_open_ref);
2867 EXPORT_SYMBOL(spa_close);
2868 EXPORT_SYMBOL(spa_refcount_zero);
2869
2870 /* Pool configuration lock */
2871 EXPORT_SYMBOL(spa_config_tryenter);
2872 EXPORT_SYMBOL(spa_config_enter);
2873 EXPORT_SYMBOL(spa_config_exit);
2874 EXPORT_SYMBOL(spa_config_held);
2875
2876 /* Pool vdev add/remove lock */
2877 EXPORT_SYMBOL(spa_vdev_enter);
2878 EXPORT_SYMBOL(spa_vdev_exit);
2879
2880 /* Pool vdev state change lock */
2881 EXPORT_SYMBOL(spa_vdev_state_enter);
2882 EXPORT_SYMBOL(spa_vdev_state_exit);
2883
2884 /* Accessor functions */
2885 EXPORT_SYMBOL(spa_shutting_down);
2886 EXPORT_SYMBOL(spa_get_dsl);
2887 EXPORT_SYMBOL(spa_get_rootblkptr);
2888 EXPORT_SYMBOL(spa_set_rootblkptr);
2889 EXPORT_SYMBOL(spa_altroot);
2890 EXPORT_SYMBOL(spa_sync_pass);
2891 EXPORT_SYMBOL(spa_name);
2892 EXPORT_SYMBOL(spa_guid);
2893 EXPORT_SYMBOL(spa_last_synced_txg);
2894 EXPORT_SYMBOL(spa_first_txg);
2895 EXPORT_SYMBOL(spa_syncing_txg);
2896 EXPORT_SYMBOL(spa_version);
2897 EXPORT_SYMBOL(spa_state);
2898 EXPORT_SYMBOL(spa_load_state);
2899 EXPORT_SYMBOL(spa_freeze_txg);
2900 EXPORT_SYMBOL(spa_get_dspace);
2901 EXPORT_SYMBOL(spa_update_dspace);
2902 EXPORT_SYMBOL(spa_deflate);
2903 EXPORT_SYMBOL(spa_normal_class);
2904 EXPORT_SYMBOL(spa_log_class);
2905 EXPORT_SYMBOL(spa_special_class);
2906 EXPORT_SYMBOL(spa_preferred_class);
2907 EXPORT_SYMBOL(spa_max_replication);
2908 EXPORT_SYMBOL(spa_prev_software_version);
2909 EXPORT_SYMBOL(spa_get_failmode);
2910 EXPORT_SYMBOL(spa_suspended);
2911 EXPORT_SYMBOL(spa_bootfs);
2912 EXPORT_SYMBOL(spa_delegation);
2913 EXPORT_SYMBOL(spa_meta_objset);
2914 EXPORT_SYMBOL(spa_maxblocksize);
2915 EXPORT_SYMBOL(spa_maxdnodesize);
2916
2917 /* Miscellaneous support routines */
2918 EXPORT_SYMBOL(spa_guid_exists);
2919 EXPORT_SYMBOL(spa_strdup);
2920 EXPORT_SYMBOL(spa_strfree);
2921 EXPORT_SYMBOL(spa_generate_guid);
2922 EXPORT_SYMBOL(snprintf_blkptr);
2923 EXPORT_SYMBOL(spa_freeze);
2924 EXPORT_SYMBOL(spa_upgrade);
2925 EXPORT_SYMBOL(spa_evict_all);
2926 EXPORT_SYMBOL(spa_lookup_by_guid);
2927 EXPORT_SYMBOL(spa_has_spare);
2928 EXPORT_SYMBOL(dva_get_dsize_sync);
2929 EXPORT_SYMBOL(bp_get_dsize_sync);
2930 EXPORT_SYMBOL(bp_get_dsize);
2931 EXPORT_SYMBOL(spa_has_slogs);
2932 EXPORT_SYMBOL(spa_is_root);
2933 EXPORT_SYMBOL(spa_writeable);
2934 EXPORT_SYMBOL(spa_mode);
2935 EXPORT_SYMBOL(spa_namespace_lock);
2936 EXPORT_SYMBOL(spa_trust_config);
2937 EXPORT_SYMBOL(spa_missing_tvds_allowed);
2938 EXPORT_SYMBOL(spa_set_missing_tvds);
2939 EXPORT_SYMBOL(spa_state_to_name);
2940 EXPORT_SYMBOL(spa_importing_readonly_checkpoint);
2941 EXPORT_SYMBOL(spa_min_claim_txg);
2942 EXPORT_SYMBOL(spa_suspend_async_destroy);
2943 EXPORT_SYMBOL(spa_has_checkpoint);
2944 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable);
2945
2946 ZFS_MODULE_PARAM(zfs, zfs_, flags, UINT, ZMOD_RW,
2947 "Set additional debugging flags");
2948
2949 ZFS_MODULE_PARAM(zfs, zfs_, recover, INT, ZMOD_RW,
2950 "Set to attempt to recover from fatal errors");
2951
2952 ZFS_MODULE_PARAM(zfs, zfs_, free_leak_on_eio, INT, ZMOD_RW,
2953 "Set to ignore IO errors during free and permanently leak the space");
2954
2955 ZFS_MODULE_PARAM(zfs_deadman, zfs_deadman_, checktime_ms, ULONG, ZMOD_RW,
2956 "Dead I/O check interval in milliseconds");
2957
2958 ZFS_MODULE_PARAM(zfs_deadman, zfs_deadman_, enabled, INT, ZMOD_RW,
2959 "Enable deadman timer");
2960
2961 ZFS_MODULE_PARAM(zfs_spa, spa_, asize_inflation, INT, ZMOD_RW,
2962 "SPA size estimate multiplication factor");
2963
2964 ZFS_MODULE_PARAM(zfs, zfs_, ddt_data_is_special, INT, ZMOD_RW,
2965 "Place DDT data into the special class");
2966
2967 ZFS_MODULE_PARAM(zfs, zfs_, user_indirect_is_special, INT, ZMOD_RW,
2968 "Place user data indirect blocks into the special class");
2969
2970 /* BEGIN CSTYLED */
2971 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, failmode,
2972 param_set_deadman_failmode, param_get_charp, ZMOD_RW,
2973 "Failmode for deadman timer");
2974
2975 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, synctime_ms,
2976 param_set_deadman_synctime, param_get_ulong, ZMOD_RW,
2977 "Pool sync expiration time in milliseconds");
2978
2979 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, ziotime_ms,
2980 param_set_deadman_ziotime, param_get_ulong, ZMOD_RW,
2981 "IO expiration time in milliseconds");
2982
2983 ZFS_MODULE_PARAM(zfs, zfs_, special_class_metadata_reserve_pct, INT, ZMOD_RW,
2984 "Small file blocks in special vdevs depends on this much "
2985 "free space available");
2986 /* END CSTYLED */
2987
2988 ZFS_MODULE_PARAM_CALL(zfs_spa, spa_, slop_shift, param_set_slop_shift,
2989 param_get_int, ZMOD_RW, "Reserved free space in pool");
2990