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