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