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