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