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