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