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