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