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