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