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