]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | /* | |
23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. | |
24 | * Copyright (c) 2013 by Delphix. All rights reserved. | |
25 | * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved. | |
26 | * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved. | |
27 | * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. | |
28 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. | |
29 | */ | |
30 | ||
31 | /* | |
32 | * SPA: Storage Pool Allocator | |
33 | * | |
34 | * This file contains all the routines used when modifying on-disk SPA state. | |
35 | * This includes opening, importing, destroying, exporting a pool, and syncing a | |
36 | * pool. | |
37 | */ | |
38 | ||
39 | #include <sys/zfs_context.h> | |
40 | #include <sys/fm/fs/zfs.h> | |
41 | #include <sys/spa_impl.h> | |
42 | #include <sys/zio.h> | |
43 | #include <sys/zio_checksum.h> | |
44 | #include <sys/dmu.h> | |
45 | #include <sys/dmu_tx.h> | |
46 | #include <sys/zap.h> | |
47 | #include <sys/zil.h> | |
48 | #include <sys/ddt.h> | |
49 | #include <sys/vdev_impl.h> | |
50 | #include <sys/vdev_disk.h> | |
51 | #include <sys/metaslab.h> | |
52 | #include <sys/metaslab_impl.h> | |
53 | #include <sys/uberblock_impl.h> | |
54 | #include <sys/txg.h> | |
55 | #include <sys/avl.h> | |
56 | #include <sys/dmu_traverse.h> | |
57 | #include <sys/dmu_objset.h> | |
58 | #include <sys/unique.h> | |
59 | #include <sys/dsl_pool.h> | |
60 | #include <sys/dsl_dataset.h> | |
61 | #include <sys/dsl_dir.h> | |
62 | #include <sys/dsl_prop.h> | |
63 | #include <sys/dsl_synctask.h> | |
64 | #include <sys/fs/zfs.h> | |
65 | #include <sys/arc.h> | |
66 | #include <sys/callb.h> | |
67 | #include <sys/systeminfo.h> | |
68 | #include <sys/spa_boot.h> | |
69 | #include <sys/zfs_ioctl.h> | |
70 | #include <sys/dsl_scan.h> | |
71 | #include <sys/zfeature.h> | |
72 | #include <sys/dsl_destroy.h> | |
73 | #include <sys/zvol.h> | |
74 | ||
75 | #ifdef _KERNEL | |
76 | #include <sys/bootprops.h> | |
77 | #include <sys/callb.h> | |
78 | #include <sys/cpupart.h> | |
79 | #include <sys/pool.h> | |
80 | #include <sys/sysdc.h> | |
81 | #include <sys/zone.h> | |
82 | #endif /* _KERNEL */ | |
83 | ||
84 | #include "zfs_prop.h" | |
85 | #include "zfs_comutil.h" | |
86 | ||
87 | /* | |
88 | * The interval, in seconds, at which failed configuration cache file writes | |
89 | * should be retried. | |
90 | */ | |
91 | static int zfs_ccw_retry_interval = 300; | |
92 | ||
93 | typedef enum zti_modes { | |
94 | ZTI_MODE_FIXED, /* value is # of threads (min 1) */ | |
95 | ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ | |
96 | ZTI_MODE_NULL, /* don't create a taskq */ | |
97 | ZTI_NMODES | |
98 | } zti_modes_t; | |
99 | ||
100 | #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } | |
101 | #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 } | |
102 | #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } | |
103 | #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } | |
104 | ||
105 | #define ZTI_N(n) ZTI_P(n, 1) | |
106 | #define ZTI_ONE ZTI_N(1) | |
107 | ||
108 | typedef struct zio_taskq_info { | |
109 | zti_modes_t zti_mode; | |
110 | uint_t zti_value; | |
111 | uint_t zti_count; | |
112 | } zio_taskq_info_t; | |
113 | ||
114 | static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { | |
115 | "iss", "iss_h", "int", "int_h" | |
116 | }; | |
117 | ||
118 | /* | |
119 | * This table defines the taskq settings for each ZFS I/O type. When | |
120 | * initializing a pool, we use this table to create an appropriately sized | |
121 | * taskq. Some operations are low volume and therefore have a small, static | |
122 | * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE | |
123 | * macros. Other operations process a large amount of data; the ZTI_BATCH | |
124 | * macro causes us to create a taskq oriented for throughput. Some operations | |
125 | * are so high frequency and short-lived that the taskq itself can become a a | |
126 | * point of lock contention. The ZTI_P(#, #) macro indicates that we need an | |
127 | * additional degree of parallelism specified by the number of threads per- | |
128 | * taskq and the number of taskqs; when dispatching an event in this case, the | |
129 | * particular taskq is chosen at random. | |
130 | * | |
131 | * The different taskq priorities are to handle the different contexts (issue | |
132 | * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that | |
133 | * need to be handled with minimum delay. | |
134 | */ | |
135 | const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { | |
136 | /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ | |
137 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ | |
138 | { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ | |
139 | { ZTI_BATCH, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */ | |
140 | { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ | |
141 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ | |
142 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ | |
143 | }; | |
144 | ||
145 | static void spa_sync_version(void *arg, dmu_tx_t *tx); | |
146 | static void spa_sync_props(void *arg, dmu_tx_t *tx); | |
147 | static boolean_t spa_has_active_shared_spare(spa_t *spa); | |
148 | static inline int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, | |
149 | spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, | |
150 | char **ereport); | |
151 | static void spa_vdev_resilver_done(spa_t *spa); | |
152 | ||
153 | uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ | |
154 | id_t zio_taskq_psrset_bind = PS_NONE; | |
155 | boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ | |
156 | uint_t zio_taskq_basedc = 80; /* base duty cycle */ | |
157 | ||
158 | boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ | |
159 | ||
160 | /* | |
161 | * This (illegal) pool name is used when temporarily importing a spa_t in order | |
162 | * to get the vdev stats associated with the imported devices. | |
163 | */ | |
164 | #define TRYIMPORT_NAME "$import" | |
165 | ||
166 | /* | |
167 | * ========================================================================== | |
168 | * SPA properties routines | |
169 | * ========================================================================== | |
170 | */ | |
171 | ||
172 | /* | |
173 | * Add a (source=src, propname=propval) list to an nvlist. | |
174 | */ | |
175 | static void | |
176 | spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, | |
177 | uint64_t intval, zprop_source_t src) | |
178 | { | |
179 | const char *propname = zpool_prop_to_name(prop); | |
180 | nvlist_t *propval; | |
181 | ||
182 | VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
183 | VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); | |
184 | ||
185 | if (strval != NULL) | |
186 | VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); | |
187 | else | |
188 | VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); | |
189 | ||
190 | VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); | |
191 | nvlist_free(propval); | |
192 | } | |
193 | ||
194 | /* | |
195 | * Get property values from the spa configuration. | |
196 | */ | |
197 | static void | |
198 | spa_prop_get_config(spa_t *spa, nvlist_t **nvp) | |
199 | { | |
200 | vdev_t *rvd = spa->spa_root_vdev; | |
201 | dsl_pool_t *pool = spa->spa_dsl_pool; | |
202 | uint64_t size, alloc, cap, version; | |
203 | const zprop_source_t src = ZPROP_SRC_NONE; | |
204 | spa_config_dirent_t *dp; | |
205 | metaslab_class_t *mc = spa_normal_class(spa); | |
206 | ||
207 | ASSERT(MUTEX_HELD(&spa->spa_props_lock)); | |
208 | ||
209 | if (rvd != NULL) { | |
210 | alloc = metaslab_class_get_alloc(spa_normal_class(spa)); | |
211 | size = metaslab_class_get_space(spa_normal_class(spa)); | |
212 | spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); | |
213 | spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); | |
214 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); | |
215 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, | |
216 | size - alloc, src); | |
217 | ||
218 | spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, | |
219 | metaslab_class_fragmentation(mc), src); | |
220 | spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, | |
221 | metaslab_class_expandable_space(mc), src); | |
222 | spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, | |
223 | (spa_mode(spa) == FREAD), src); | |
224 | ||
225 | cap = (size == 0) ? 0 : (alloc * 100 / size); | |
226 | spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); | |
227 | ||
228 | spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, | |
229 | ddt_get_pool_dedup_ratio(spa), src); | |
230 | ||
231 | spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, | |
232 | rvd->vdev_state, src); | |
233 | ||
234 | version = spa_version(spa); | |
235 | if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) { | |
236 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
237 | version, ZPROP_SRC_DEFAULT); | |
238 | } else { | |
239 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
240 | version, ZPROP_SRC_LOCAL); | |
241 | } | |
242 | } | |
243 | ||
244 | if (pool != NULL) { | |
245 | /* | |
246 | * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, | |
247 | * when opening pools before this version freedir will be NULL. | |
248 | */ | |
249 | if (pool->dp_free_dir != NULL) { | |
250 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, | |
251 | dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, | |
252 | src); | |
253 | } else { | |
254 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, | |
255 | NULL, 0, src); | |
256 | } | |
257 | ||
258 | if (pool->dp_leak_dir != NULL) { | |
259 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, | |
260 | dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, | |
261 | src); | |
262 | } else { | |
263 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, | |
264 | NULL, 0, src); | |
265 | } | |
266 | } | |
267 | ||
268 | spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); | |
269 | ||
270 | if (spa->spa_comment != NULL) { | |
271 | spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, | |
272 | 0, ZPROP_SRC_LOCAL); | |
273 | } | |
274 | ||
275 | if (spa->spa_root != NULL) | |
276 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, | |
277 | 0, ZPROP_SRC_LOCAL); | |
278 | ||
279 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { | |
280 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
281 | MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); | |
282 | } else { | |
283 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
284 | SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); | |
285 | } | |
286 | ||
287 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) { | |
288 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
289 | DNODE_MAX_SIZE, ZPROP_SRC_NONE); | |
290 | } else { | |
291 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
292 | DNODE_MIN_SIZE, ZPROP_SRC_NONE); | |
293 | } | |
294 | ||
295 | if ((dp = list_head(&spa->spa_config_list)) != NULL) { | |
296 | if (dp->scd_path == NULL) { | |
297 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, | |
298 | "none", 0, ZPROP_SRC_LOCAL); | |
299 | } else if (strcmp(dp->scd_path, spa_config_path) != 0) { | |
300 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, | |
301 | dp->scd_path, 0, ZPROP_SRC_LOCAL); | |
302 | } | |
303 | } | |
304 | } | |
305 | ||
306 | /* | |
307 | * Get zpool property values. | |
308 | */ | |
309 | int | |
310 | spa_prop_get(spa_t *spa, nvlist_t **nvp) | |
311 | { | |
312 | objset_t *mos = spa->spa_meta_objset; | |
313 | zap_cursor_t zc; | |
314 | zap_attribute_t za; | |
315 | int err; | |
316 | ||
317 | err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP); | |
318 | if (err) | |
319 | return (err); | |
320 | ||
321 | mutex_enter(&spa->spa_props_lock); | |
322 | ||
323 | /* | |
324 | * Get properties from the spa config. | |
325 | */ | |
326 | spa_prop_get_config(spa, nvp); | |
327 | ||
328 | /* If no pool property object, no more prop to get. */ | |
329 | if (mos == NULL || spa->spa_pool_props_object == 0) { | |
330 | mutex_exit(&spa->spa_props_lock); | |
331 | goto out; | |
332 | } | |
333 | ||
334 | /* | |
335 | * Get properties from the MOS pool property object. | |
336 | */ | |
337 | for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); | |
338 | (err = zap_cursor_retrieve(&zc, &za)) == 0; | |
339 | zap_cursor_advance(&zc)) { | |
340 | uint64_t intval = 0; | |
341 | char *strval = NULL; | |
342 | zprop_source_t src = ZPROP_SRC_DEFAULT; | |
343 | zpool_prop_t prop; | |
344 | ||
345 | if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) | |
346 | continue; | |
347 | ||
348 | switch (za.za_integer_length) { | |
349 | case 8: | |
350 | /* integer property */ | |
351 | if (za.za_first_integer != | |
352 | zpool_prop_default_numeric(prop)) | |
353 | src = ZPROP_SRC_LOCAL; | |
354 | ||
355 | if (prop == ZPOOL_PROP_BOOTFS) { | |
356 | dsl_pool_t *dp; | |
357 | dsl_dataset_t *ds = NULL; | |
358 | ||
359 | dp = spa_get_dsl(spa); | |
360 | dsl_pool_config_enter(dp, FTAG); | |
361 | if ((err = dsl_dataset_hold_obj(dp, | |
362 | za.za_first_integer, FTAG, &ds))) { | |
363 | dsl_pool_config_exit(dp, FTAG); | |
364 | break; | |
365 | } | |
366 | ||
367 | strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, | |
368 | KM_SLEEP); | |
369 | dsl_dataset_name(ds, strval); | |
370 | dsl_dataset_rele(ds, FTAG); | |
371 | dsl_pool_config_exit(dp, FTAG); | |
372 | } else { | |
373 | strval = NULL; | |
374 | intval = za.za_first_integer; | |
375 | } | |
376 | ||
377 | spa_prop_add_list(*nvp, prop, strval, intval, src); | |
378 | ||
379 | if (strval != NULL) | |
380 | kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); | |
381 | ||
382 | break; | |
383 | ||
384 | case 1: | |
385 | /* string property */ | |
386 | strval = kmem_alloc(za.za_num_integers, KM_SLEEP); | |
387 | err = zap_lookup(mos, spa->spa_pool_props_object, | |
388 | za.za_name, 1, za.za_num_integers, strval); | |
389 | if (err) { | |
390 | kmem_free(strval, za.za_num_integers); | |
391 | break; | |
392 | } | |
393 | spa_prop_add_list(*nvp, prop, strval, 0, src); | |
394 | kmem_free(strval, za.za_num_integers); | |
395 | break; | |
396 | ||
397 | default: | |
398 | break; | |
399 | } | |
400 | } | |
401 | zap_cursor_fini(&zc); | |
402 | mutex_exit(&spa->spa_props_lock); | |
403 | out: | |
404 | if (err && err != ENOENT) { | |
405 | nvlist_free(*nvp); | |
406 | *nvp = NULL; | |
407 | return (err); | |
408 | } | |
409 | ||
410 | return (0); | |
411 | } | |
412 | ||
413 | /* | |
414 | * Validate the given pool properties nvlist and modify the list | |
415 | * for the property values to be set. | |
416 | */ | |
417 | static int | |
418 | spa_prop_validate(spa_t *spa, nvlist_t *props) | |
419 | { | |
420 | nvpair_t *elem; | |
421 | int error = 0, reset_bootfs = 0; | |
422 | uint64_t objnum = 0; | |
423 | boolean_t has_feature = B_FALSE; | |
424 | ||
425 | elem = NULL; | |
426 | while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { | |
427 | uint64_t intval; | |
428 | char *strval, *slash, *check, *fname; | |
429 | const char *propname = nvpair_name(elem); | |
430 | zpool_prop_t prop = zpool_name_to_prop(propname); | |
431 | ||
432 | switch ((int)prop) { | |
433 | case ZPROP_INVAL: | |
434 | if (!zpool_prop_feature(propname)) { | |
435 | error = SET_ERROR(EINVAL); | |
436 | break; | |
437 | } | |
438 | ||
439 | /* | |
440 | * Sanitize the input. | |
441 | */ | |
442 | if (nvpair_type(elem) != DATA_TYPE_UINT64) { | |
443 | error = SET_ERROR(EINVAL); | |
444 | break; | |
445 | } | |
446 | ||
447 | if (nvpair_value_uint64(elem, &intval) != 0) { | |
448 | error = SET_ERROR(EINVAL); | |
449 | break; | |
450 | } | |
451 | ||
452 | if (intval != 0) { | |
453 | error = SET_ERROR(EINVAL); | |
454 | break; | |
455 | } | |
456 | ||
457 | fname = strchr(propname, '@') + 1; | |
458 | if (zfeature_lookup_name(fname, NULL) != 0) { | |
459 | error = SET_ERROR(EINVAL); | |
460 | break; | |
461 | } | |
462 | ||
463 | has_feature = B_TRUE; | |
464 | break; | |
465 | ||
466 | case ZPOOL_PROP_VERSION: | |
467 | error = nvpair_value_uint64(elem, &intval); | |
468 | if (!error && | |
469 | (intval < spa_version(spa) || | |
470 | intval > SPA_VERSION_BEFORE_FEATURES || | |
471 | has_feature)) | |
472 | error = SET_ERROR(EINVAL); | |
473 | break; | |
474 | ||
475 | case ZPOOL_PROP_DELEGATION: | |
476 | case ZPOOL_PROP_AUTOREPLACE: | |
477 | case ZPOOL_PROP_LISTSNAPS: | |
478 | case ZPOOL_PROP_AUTOEXPAND: | |
479 | error = nvpair_value_uint64(elem, &intval); | |
480 | if (!error && intval > 1) | |
481 | error = SET_ERROR(EINVAL); | |
482 | break; | |
483 | ||
484 | case ZPOOL_PROP_BOOTFS: | |
485 | /* | |
486 | * If the pool version is less than SPA_VERSION_BOOTFS, | |
487 | * or the pool is still being created (version == 0), | |
488 | * the bootfs property cannot be set. | |
489 | */ | |
490 | if (spa_version(spa) < SPA_VERSION_BOOTFS) { | |
491 | error = SET_ERROR(ENOTSUP); | |
492 | break; | |
493 | } | |
494 | ||
495 | /* | |
496 | * Make sure the vdev config is bootable | |
497 | */ | |
498 | if (!vdev_is_bootable(spa->spa_root_vdev)) { | |
499 | error = SET_ERROR(ENOTSUP); | |
500 | break; | |
501 | } | |
502 | ||
503 | reset_bootfs = 1; | |
504 | ||
505 | error = nvpair_value_string(elem, &strval); | |
506 | ||
507 | if (!error) { | |
508 | objset_t *os; | |
509 | uint64_t propval; | |
510 | ||
511 | if (strval == NULL || strval[0] == '\0') { | |
512 | objnum = zpool_prop_default_numeric( | |
513 | ZPOOL_PROP_BOOTFS); | |
514 | break; | |
515 | } | |
516 | ||
517 | error = dmu_objset_hold(strval, FTAG, &os); | |
518 | if (error) | |
519 | break; | |
520 | ||
521 | /* | |
522 | * Must be ZPL, and its property settings | |
523 | * must be supported by GRUB (compression | |
524 | * is not gzip, and large blocks or large | |
525 | * dnodes are not used). | |
526 | */ | |
527 | ||
528 | if (dmu_objset_type(os) != DMU_OST_ZFS) { | |
529 | error = SET_ERROR(ENOTSUP); | |
530 | } else if ((error = | |
531 | dsl_prop_get_int_ds(dmu_objset_ds(os), | |
532 | zfs_prop_to_name(ZFS_PROP_COMPRESSION), | |
533 | &propval)) == 0 && | |
534 | !BOOTFS_COMPRESS_VALID(propval)) { | |
535 | error = SET_ERROR(ENOTSUP); | |
536 | } else if ((error = | |
537 | dsl_prop_get_int_ds(dmu_objset_ds(os), | |
538 | zfs_prop_to_name(ZFS_PROP_RECORDSIZE), | |
539 | &propval)) == 0 && | |
540 | propval > SPA_OLD_MAXBLOCKSIZE) { | |
541 | error = SET_ERROR(ENOTSUP); | |
542 | } else if ((error = | |
543 | dsl_prop_get_int_ds(dmu_objset_ds(os), | |
544 | zfs_prop_to_name(ZFS_PROP_DNODESIZE), | |
545 | &propval)) == 0 && | |
546 | propval != ZFS_DNSIZE_LEGACY) { | |
547 | error = SET_ERROR(ENOTSUP); | |
548 | } else { | |
549 | objnum = dmu_objset_id(os); | |
550 | } | |
551 | dmu_objset_rele(os, FTAG); | |
552 | } | |
553 | break; | |
554 | ||
555 | case ZPOOL_PROP_FAILUREMODE: | |
556 | error = nvpair_value_uint64(elem, &intval); | |
557 | if (!error && (intval < ZIO_FAILURE_MODE_WAIT || | |
558 | intval > ZIO_FAILURE_MODE_PANIC)) | |
559 | error = SET_ERROR(EINVAL); | |
560 | ||
561 | /* | |
562 | * This is a special case which only occurs when | |
563 | * the pool has completely failed. This allows | |
564 | * the user to change the in-core failmode property | |
565 | * without syncing it out to disk (I/Os might | |
566 | * currently be blocked). We do this by returning | |
567 | * EIO to the caller (spa_prop_set) to trick it | |
568 | * into thinking we encountered a property validation | |
569 | * error. | |
570 | */ | |
571 | if (!error && spa_suspended(spa)) { | |
572 | spa->spa_failmode = intval; | |
573 | error = SET_ERROR(EIO); | |
574 | } | |
575 | break; | |
576 | ||
577 | case ZPOOL_PROP_CACHEFILE: | |
578 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
579 | break; | |
580 | ||
581 | if (strval[0] == '\0') | |
582 | break; | |
583 | ||
584 | if (strcmp(strval, "none") == 0) | |
585 | break; | |
586 | ||
587 | if (strval[0] != '/') { | |
588 | error = SET_ERROR(EINVAL); | |
589 | break; | |
590 | } | |
591 | ||
592 | slash = strrchr(strval, '/'); | |
593 | ASSERT(slash != NULL); | |
594 | ||
595 | if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || | |
596 | strcmp(slash, "/..") == 0) | |
597 | error = SET_ERROR(EINVAL); | |
598 | break; | |
599 | ||
600 | case ZPOOL_PROP_COMMENT: | |
601 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
602 | break; | |
603 | for (check = strval; *check != '\0'; check++) { | |
604 | if (!isprint(*check)) { | |
605 | error = SET_ERROR(EINVAL); | |
606 | break; | |
607 | } | |
608 | } | |
609 | if (strlen(strval) > ZPROP_MAX_COMMENT) | |
610 | error = SET_ERROR(E2BIG); | |
611 | break; | |
612 | ||
613 | case ZPOOL_PROP_DEDUPDITTO: | |
614 | if (spa_version(spa) < SPA_VERSION_DEDUP) | |
615 | error = SET_ERROR(ENOTSUP); | |
616 | else | |
617 | error = nvpair_value_uint64(elem, &intval); | |
618 | if (error == 0 && | |
619 | intval != 0 && intval < ZIO_DEDUPDITTO_MIN) | |
620 | error = SET_ERROR(EINVAL); | |
621 | break; | |
622 | ||
623 | default: | |
624 | break; | |
625 | } | |
626 | ||
627 | if (error) | |
628 | break; | |
629 | } | |
630 | ||
631 | if (!error && reset_bootfs) { | |
632 | error = nvlist_remove(props, | |
633 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); | |
634 | ||
635 | if (!error) { | |
636 | error = nvlist_add_uint64(props, | |
637 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); | |
638 | } | |
639 | } | |
640 | ||
641 | return (error); | |
642 | } | |
643 | ||
644 | void | |
645 | spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) | |
646 | { | |
647 | char *cachefile; | |
648 | spa_config_dirent_t *dp; | |
649 | ||
650 | if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), | |
651 | &cachefile) != 0) | |
652 | return; | |
653 | ||
654 | dp = kmem_alloc(sizeof (spa_config_dirent_t), | |
655 | KM_SLEEP); | |
656 | ||
657 | if (cachefile[0] == '\0') | |
658 | dp->scd_path = spa_strdup(spa_config_path); | |
659 | else if (strcmp(cachefile, "none") == 0) | |
660 | dp->scd_path = NULL; | |
661 | else | |
662 | dp->scd_path = spa_strdup(cachefile); | |
663 | ||
664 | list_insert_head(&spa->spa_config_list, dp); | |
665 | if (need_sync) | |
666 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
667 | } | |
668 | ||
669 | int | |
670 | spa_prop_set(spa_t *spa, nvlist_t *nvp) | |
671 | { | |
672 | int error; | |
673 | nvpair_t *elem = NULL; | |
674 | boolean_t need_sync = B_FALSE; | |
675 | ||
676 | if ((error = spa_prop_validate(spa, nvp)) != 0) | |
677 | return (error); | |
678 | ||
679 | while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { | |
680 | zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); | |
681 | ||
682 | if (prop == ZPOOL_PROP_CACHEFILE || | |
683 | prop == ZPOOL_PROP_ALTROOT || | |
684 | prop == ZPOOL_PROP_READONLY) | |
685 | continue; | |
686 | ||
687 | if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) { | |
688 | uint64_t ver; | |
689 | ||
690 | if (prop == ZPOOL_PROP_VERSION) { | |
691 | VERIFY(nvpair_value_uint64(elem, &ver) == 0); | |
692 | } else { | |
693 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
694 | ver = SPA_VERSION_FEATURES; | |
695 | need_sync = B_TRUE; | |
696 | } | |
697 | ||
698 | /* Save time if the version is already set. */ | |
699 | if (ver == spa_version(spa)) | |
700 | continue; | |
701 | ||
702 | /* | |
703 | * In addition to the pool directory object, we might | |
704 | * create the pool properties object, the features for | |
705 | * read object, the features for write object, or the | |
706 | * feature descriptions object. | |
707 | */ | |
708 | error = dsl_sync_task(spa->spa_name, NULL, | |
709 | spa_sync_version, &ver, | |
710 | 6, ZFS_SPACE_CHECK_RESERVED); | |
711 | if (error) | |
712 | return (error); | |
713 | continue; | |
714 | } | |
715 | ||
716 | need_sync = B_TRUE; | |
717 | break; | |
718 | } | |
719 | ||
720 | if (need_sync) { | |
721 | return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, | |
722 | nvp, 6, ZFS_SPACE_CHECK_RESERVED)); | |
723 | } | |
724 | ||
725 | return (0); | |
726 | } | |
727 | ||
728 | /* | |
729 | * If the bootfs property value is dsobj, clear it. | |
730 | */ | |
731 | void | |
732 | spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) | |
733 | { | |
734 | if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { | |
735 | VERIFY(zap_remove(spa->spa_meta_objset, | |
736 | spa->spa_pool_props_object, | |
737 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); | |
738 | spa->spa_bootfs = 0; | |
739 | } | |
740 | } | |
741 | ||
742 | /*ARGSUSED*/ | |
743 | static int | |
744 | spa_change_guid_check(void *arg, dmu_tx_t *tx) | |
745 | { | |
746 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
747 | vdev_t *rvd = spa->spa_root_vdev; | |
748 | uint64_t vdev_state; | |
749 | ASSERTV(uint64_t *newguid = arg); | |
750 | ||
751 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
752 | vdev_state = rvd->vdev_state; | |
753 | spa_config_exit(spa, SCL_STATE, FTAG); | |
754 | ||
755 | if (vdev_state != VDEV_STATE_HEALTHY) | |
756 | return (SET_ERROR(ENXIO)); | |
757 | ||
758 | ASSERT3U(spa_guid(spa), !=, *newguid); | |
759 | ||
760 | return (0); | |
761 | } | |
762 | ||
763 | static void | |
764 | spa_change_guid_sync(void *arg, dmu_tx_t *tx) | |
765 | { | |
766 | uint64_t *newguid = arg; | |
767 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
768 | uint64_t oldguid; | |
769 | vdev_t *rvd = spa->spa_root_vdev; | |
770 | ||
771 | oldguid = spa_guid(spa); | |
772 | ||
773 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
774 | rvd->vdev_guid = *newguid; | |
775 | rvd->vdev_guid_sum += (*newguid - oldguid); | |
776 | vdev_config_dirty(rvd); | |
777 | spa_config_exit(spa, SCL_STATE, FTAG); | |
778 | ||
779 | spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", | |
780 | oldguid, *newguid); | |
781 | } | |
782 | ||
783 | /* | |
784 | * Change the GUID for the pool. This is done so that we can later | |
785 | * re-import a pool built from a clone of our own vdevs. We will modify | |
786 | * the root vdev's guid, our own pool guid, and then mark all of our | |
787 | * vdevs dirty. Note that we must make sure that all our vdevs are | |
788 | * online when we do this, or else any vdevs that weren't present | |
789 | * would be orphaned from our pool. We are also going to issue a | |
790 | * sysevent to update any watchers. | |
791 | */ | |
792 | int | |
793 | spa_change_guid(spa_t *spa) | |
794 | { | |
795 | int error; | |
796 | uint64_t guid; | |
797 | ||
798 | mutex_enter(&spa->spa_vdev_top_lock); | |
799 | mutex_enter(&spa_namespace_lock); | |
800 | guid = spa_generate_guid(NULL); | |
801 | ||
802 | error = dsl_sync_task(spa->spa_name, spa_change_guid_check, | |
803 | spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); | |
804 | ||
805 | if (error == 0) { | |
806 | spa_config_sync(spa, B_FALSE, B_TRUE); | |
807 | spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID); | |
808 | } | |
809 | ||
810 | mutex_exit(&spa_namespace_lock); | |
811 | mutex_exit(&spa->spa_vdev_top_lock); | |
812 | ||
813 | return (error); | |
814 | } | |
815 | ||
816 | /* | |
817 | * ========================================================================== | |
818 | * SPA state manipulation (open/create/destroy/import/export) | |
819 | * ========================================================================== | |
820 | */ | |
821 | ||
822 | static int | |
823 | spa_error_entry_compare(const void *a, const void *b) | |
824 | { | |
825 | spa_error_entry_t *sa = (spa_error_entry_t *)a; | |
826 | spa_error_entry_t *sb = (spa_error_entry_t *)b; | |
827 | int ret; | |
828 | ||
829 | ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, | |
830 | sizeof (zbookmark_phys_t)); | |
831 | ||
832 | if (ret < 0) | |
833 | return (-1); | |
834 | else if (ret > 0) | |
835 | return (1); | |
836 | else | |
837 | return (0); | |
838 | } | |
839 | ||
840 | /* | |
841 | * Utility function which retrieves copies of the current logs and | |
842 | * re-initializes them in the process. | |
843 | */ | |
844 | void | |
845 | spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) | |
846 | { | |
847 | ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); | |
848 | ||
849 | bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); | |
850 | bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); | |
851 | ||
852 | avl_create(&spa->spa_errlist_scrub, | |
853 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
854 | offsetof(spa_error_entry_t, se_avl)); | |
855 | avl_create(&spa->spa_errlist_last, | |
856 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
857 | offsetof(spa_error_entry_t, se_avl)); | |
858 | } | |
859 | ||
860 | static void | |
861 | spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
862 | { | |
863 | const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; | |
864 | enum zti_modes mode = ztip->zti_mode; | |
865 | uint_t value = ztip->zti_value; | |
866 | uint_t count = ztip->zti_count; | |
867 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
868 | char name[32]; | |
869 | uint_t i, flags = TASKQ_DYNAMIC; | |
870 | boolean_t batch = B_FALSE; | |
871 | ||
872 | if (mode == ZTI_MODE_NULL) { | |
873 | tqs->stqs_count = 0; | |
874 | tqs->stqs_taskq = NULL; | |
875 | return; | |
876 | } | |
877 | ||
878 | ASSERT3U(count, >, 0); | |
879 | ||
880 | tqs->stqs_count = count; | |
881 | tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); | |
882 | ||
883 | switch (mode) { | |
884 | case ZTI_MODE_FIXED: | |
885 | ASSERT3U(value, >=, 1); | |
886 | value = MAX(value, 1); | |
887 | break; | |
888 | ||
889 | case ZTI_MODE_BATCH: | |
890 | batch = B_TRUE; | |
891 | flags |= TASKQ_THREADS_CPU_PCT; | |
892 | value = MIN(zio_taskq_batch_pct, 100); | |
893 | break; | |
894 | ||
895 | default: | |
896 | panic("unrecognized mode for %s_%s taskq (%u:%u) in " | |
897 | "spa_activate()", | |
898 | zio_type_name[t], zio_taskq_types[q], mode, value); | |
899 | break; | |
900 | } | |
901 | ||
902 | for (i = 0; i < count; i++) { | |
903 | taskq_t *tq; | |
904 | ||
905 | if (count > 1) { | |
906 | (void) snprintf(name, sizeof (name), "%s_%s_%u", | |
907 | zio_type_name[t], zio_taskq_types[q], i); | |
908 | } else { | |
909 | (void) snprintf(name, sizeof (name), "%s_%s", | |
910 | zio_type_name[t], zio_taskq_types[q]); | |
911 | } | |
912 | ||
913 | if (zio_taskq_sysdc && spa->spa_proc != &p0) { | |
914 | if (batch) | |
915 | flags |= TASKQ_DC_BATCH; | |
916 | ||
917 | tq = taskq_create_sysdc(name, value, 50, INT_MAX, | |
918 | spa->spa_proc, zio_taskq_basedc, flags); | |
919 | } else { | |
920 | pri_t pri = maxclsyspri; | |
921 | /* | |
922 | * The write issue taskq can be extremely CPU | |
923 | * intensive. Run it at slightly less important | |
924 | * priority than the other taskqs. Under Linux this | |
925 | * means incrementing the priority value on platforms | |
926 | * like illumos it should be decremented. | |
927 | */ | |
928 | if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) | |
929 | pri++; | |
930 | ||
931 | tq = taskq_create_proc(name, value, pri, 50, | |
932 | INT_MAX, spa->spa_proc, flags); | |
933 | } | |
934 | ||
935 | tqs->stqs_taskq[i] = tq; | |
936 | } | |
937 | } | |
938 | ||
939 | static void | |
940 | spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
941 | { | |
942 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
943 | uint_t i; | |
944 | ||
945 | if (tqs->stqs_taskq == NULL) { | |
946 | ASSERT3U(tqs->stqs_count, ==, 0); | |
947 | return; | |
948 | } | |
949 | ||
950 | for (i = 0; i < tqs->stqs_count; i++) { | |
951 | ASSERT3P(tqs->stqs_taskq[i], !=, NULL); | |
952 | taskq_destroy(tqs->stqs_taskq[i]); | |
953 | } | |
954 | ||
955 | kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); | |
956 | tqs->stqs_taskq = NULL; | |
957 | } | |
958 | ||
959 | /* | |
960 | * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. | |
961 | * Note that a type may have multiple discrete taskqs to avoid lock contention | |
962 | * on the taskq itself. In that case we choose which taskq at random by using | |
963 | * the low bits of gethrtime(). | |
964 | */ | |
965 | void | |
966 | spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
967 | task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) | |
968 | { | |
969 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
970 | taskq_t *tq; | |
971 | ||
972 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
973 | ASSERT3U(tqs->stqs_count, !=, 0); | |
974 | ||
975 | if (tqs->stqs_count == 1) { | |
976 | tq = tqs->stqs_taskq[0]; | |
977 | } else { | |
978 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; | |
979 | } | |
980 | ||
981 | taskq_dispatch_ent(tq, func, arg, flags, ent); | |
982 | } | |
983 | ||
984 | /* | |
985 | * Same as spa_taskq_dispatch_ent() but block on the task until completion. | |
986 | */ | |
987 | void | |
988 | spa_taskq_dispatch_sync(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
989 | task_func_t *func, void *arg, uint_t flags) | |
990 | { | |
991 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
992 | taskq_t *tq; | |
993 | taskqid_t id; | |
994 | ||
995 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
996 | ASSERT3U(tqs->stqs_count, !=, 0); | |
997 | ||
998 | if (tqs->stqs_count == 1) { | |
999 | tq = tqs->stqs_taskq[0]; | |
1000 | } else { | |
1001 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; | |
1002 | } | |
1003 | ||
1004 | id = taskq_dispatch(tq, func, arg, flags); | |
1005 | if (id) | |
1006 | taskq_wait_id(tq, id); | |
1007 | } | |
1008 | ||
1009 | static void | |
1010 | spa_create_zio_taskqs(spa_t *spa) | |
1011 | { | |
1012 | int t, q; | |
1013 | ||
1014 | for (t = 0; t < ZIO_TYPES; t++) { | |
1015 | for (q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
1016 | spa_taskqs_init(spa, t, q); | |
1017 | } | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | #if defined(_KERNEL) && defined(HAVE_SPA_THREAD) | |
1022 | static void | |
1023 | spa_thread(void *arg) | |
1024 | { | |
1025 | callb_cpr_t cprinfo; | |
1026 | ||
1027 | spa_t *spa = arg; | |
1028 | user_t *pu = PTOU(curproc); | |
1029 | ||
1030 | CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, | |
1031 | spa->spa_name); | |
1032 | ||
1033 | ASSERT(curproc != &p0); | |
1034 | (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), | |
1035 | "zpool-%s", spa->spa_name); | |
1036 | (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); | |
1037 | ||
1038 | /* bind this thread to the requested psrset */ | |
1039 | if (zio_taskq_psrset_bind != PS_NONE) { | |
1040 | pool_lock(); | |
1041 | mutex_enter(&cpu_lock); | |
1042 | mutex_enter(&pidlock); | |
1043 | mutex_enter(&curproc->p_lock); | |
1044 | ||
1045 | if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, | |
1046 | 0, NULL, NULL) == 0) { | |
1047 | curthread->t_bind_pset = zio_taskq_psrset_bind; | |
1048 | } else { | |
1049 | cmn_err(CE_WARN, | |
1050 | "Couldn't bind process for zfs pool \"%s\" to " | |
1051 | "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); | |
1052 | } | |
1053 | ||
1054 | mutex_exit(&curproc->p_lock); | |
1055 | mutex_exit(&pidlock); | |
1056 | mutex_exit(&cpu_lock); | |
1057 | pool_unlock(); | |
1058 | } | |
1059 | ||
1060 | if (zio_taskq_sysdc) { | |
1061 | sysdc_thread_enter(curthread, 100, 0); | |
1062 | } | |
1063 | ||
1064 | spa->spa_proc = curproc; | |
1065 | spa->spa_did = curthread->t_did; | |
1066 | ||
1067 | spa_create_zio_taskqs(spa); | |
1068 | ||
1069 | mutex_enter(&spa->spa_proc_lock); | |
1070 | ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); | |
1071 | ||
1072 | spa->spa_proc_state = SPA_PROC_ACTIVE; | |
1073 | cv_broadcast(&spa->spa_proc_cv); | |
1074 | ||
1075 | CALLB_CPR_SAFE_BEGIN(&cprinfo); | |
1076 | while (spa->spa_proc_state == SPA_PROC_ACTIVE) | |
1077 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1078 | CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); | |
1079 | ||
1080 | ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); | |
1081 | spa->spa_proc_state = SPA_PROC_GONE; | |
1082 | spa->spa_proc = &p0; | |
1083 | cv_broadcast(&spa->spa_proc_cv); | |
1084 | CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ | |
1085 | ||
1086 | mutex_enter(&curproc->p_lock); | |
1087 | lwp_exit(); | |
1088 | } | |
1089 | #endif | |
1090 | ||
1091 | /* | |
1092 | * Activate an uninitialized pool. | |
1093 | */ | |
1094 | static void | |
1095 | spa_activate(spa_t *spa, int mode) | |
1096 | { | |
1097 | ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); | |
1098 | ||
1099 | spa->spa_state = POOL_STATE_ACTIVE; | |
1100 | spa->spa_mode = mode; | |
1101 | ||
1102 | spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
1103 | spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
1104 | ||
1105 | /* Try to create a covering process */ | |
1106 | mutex_enter(&spa->spa_proc_lock); | |
1107 | ASSERT(spa->spa_proc_state == SPA_PROC_NONE); | |
1108 | ASSERT(spa->spa_proc == &p0); | |
1109 | spa->spa_did = 0; | |
1110 | ||
1111 | #ifdef HAVE_SPA_THREAD | |
1112 | /* Only create a process if we're going to be around a while. */ | |
1113 | if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { | |
1114 | if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, | |
1115 | NULL, 0) == 0) { | |
1116 | spa->spa_proc_state = SPA_PROC_CREATED; | |
1117 | while (spa->spa_proc_state == SPA_PROC_CREATED) { | |
1118 | cv_wait(&spa->spa_proc_cv, | |
1119 | &spa->spa_proc_lock); | |
1120 | } | |
1121 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); | |
1122 | ASSERT(spa->spa_proc != &p0); | |
1123 | ASSERT(spa->spa_did != 0); | |
1124 | } else { | |
1125 | #ifdef _KERNEL | |
1126 | cmn_err(CE_WARN, | |
1127 | "Couldn't create process for zfs pool \"%s\"\n", | |
1128 | spa->spa_name); | |
1129 | #endif | |
1130 | } | |
1131 | } | |
1132 | #endif /* HAVE_SPA_THREAD */ | |
1133 | mutex_exit(&spa->spa_proc_lock); | |
1134 | ||
1135 | /* If we didn't create a process, we need to create our taskqs. */ | |
1136 | if (spa->spa_proc == &p0) { | |
1137 | spa_create_zio_taskqs(spa); | |
1138 | } | |
1139 | ||
1140 | list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), | |
1141 | offsetof(vdev_t, vdev_config_dirty_node)); | |
1142 | list_create(&spa->spa_evicting_os_list, sizeof (objset_t), | |
1143 | offsetof(objset_t, os_evicting_node)); | |
1144 | list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), | |
1145 | offsetof(vdev_t, vdev_state_dirty_node)); | |
1146 | ||
1147 | txg_list_create(&spa->spa_vdev_txg_list, | |
1148 | offsetof(struct vdev, vdev_txg_node)); | |
1149 | ||
1150 | avl_create(&spa->spa_errlist_scrub, | |
1151 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1152 | offsetof(spa_error_entry_t, se_avl)); | |
1153 | avl_create(&spa->spa_errlist_last, | |
1154 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1155 | offsetof(spa_error_entry_t, se_avl)); | |
1156 | ||
1157 | /* | |
1158 | * This taskq is used to perform zvol-minor-related tasks | |
1159 | * asynchronously. This has several advantages, including easy | |
1160 | * resolution of various deadlocks (zfsonlinux bug #3681). | |
1161 | * | |
1162 | * The taskq must be single threaded to ensure tasks are always | |
1163 | * processed in the order in which they were dispatched. | |
1164 | * | |
1165 | * A taskq per pool allows one to keep the pools independent. | |
1166 | * This way if one pool is suspended, it will not impact another. | |
1167 | * | |
1168 | * The preferred location to dispatch a zvol minor task is a sync | |
1169 | * task. In this context, there is easy access to the spa_t and minimal | |
1170 | * error handling is required because the sync task must succeed. | |
1171 | */ | |
1172 | spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri, | |
1173 | 1, INT_MAX, 0); | |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | * Opposite of spa_activate(). | |
1178 | */ | |
1179 | static void | |
1180 | spa_deactivate(spa_t *spa) | |
1181 | { | |
1182 | int t, q; | |
1183 | ||
1184 | ASSERT(spa->spa_sync_on == B_FALSE); | |
1185 | ASSERT(spa->spa_dsl_pool == NULL); | |
1186 | ASSERT(spa->spa_root_vdev == NULL); | |
1187 | ASSERT(spa->spa_async_zio_root == NULL); | |
1188 | ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); | |
1189 | ||
1190 | spa_evicting_os_wait(spa); | |
1191 | ||
1192 | if (spa->spa_zvol_taskq) { | |
1193 | taskq_destroy(spa->spa_zvol_taskq); | |
1194 | spa->spa_zvol_taskq = NULL; | |
1195 | } | |
1196 | ||
1197 | txg_list_destroy(&spa->spa_vdev_txg_list); | |
1198 | ||
1199 | list_destroy(&spa->spa_config_dirty_list); | |
1200 | list_destroy(&spa->spa_evicting_os_list); | |
1201 | list_destroy(&spa->spa_state_dirty_list); | |
1202 | ||
1203 | taskq_cancel_id(system_taskq, spa->spa_deadman_tqid); | |
1204 | ||
1205 | for (t = 0; t < ZIO_TYPES; t++) { | |
1206 | for (q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
1207 | spa_taskqs_fini(spa, t, q); | |
1208 | } | |
1209 | } | |
1210 | ||
1211 | metaslab_class_destroy(spa->spa_normal_class); | |
1212 | spa->spa_normal_class = NULL; | |
1213 | ||
1214 | metaslab_class_destroy(spa->spa_log_class); | |
1215 | spa->spa_log_class = NULL; | |
1216 | ||
1217 | /* | |
1218 | * If this was part of an import or the open otherwise failed, we may | |
1219 | * still have errors left in the queues. Empty them just in case. | |
1220 | */ | |
1221 | spa_errlog_drain(spa); | |
1222 | ||
1223 | avl_destroy(&spa->spa_errlist_scrub); | |
1224 | avl_destroy(&spa->spa_errlist_last); | |
1225 | ||
1226 | spa->spa_state = POOL_STATE_UNINITIALIZED; | |
1227 | ||
1228 | mutex_enter(&spa->spa_proc_lock); | |
1229 | if (spa->spa_proc_state != SPA_PROC_NONE) { | |
1230 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); | |
1231 | spa->spa_proc_state = SPA_PROC_DEACTIVATE; | |
1232 | cv_broadcast(&spa->spa_proc_cv); | |
1233 | while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { | |
1234 | ASSERT(spa->spa_proc != &p0); | |
1235 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1236 | } | |
1237 | ASSERT(spa->spa_proc_state == SPA_PROC_GONE); | |
1238 | spa->spa_proc_state = SPA_PROC_NONE; | |
1239 | } | |
1240 | ASSERT(spa->spa_proc == &p0); | |
1241 | mutex_exit(&spa->spa_proc_lock); | |
1242 | ||
1243 | /* | |
1244 | * We want to make sure spa_thread() has actually exited the ZFS | |
1245 | * module, so that the module can't be unloaded out from underneath | |
1246 | * it. | |
1247 | */ | |
1248 | if (spa->spa_did != 0) { | |
1249 | thread_join(spa->spa_did); | |
1250 | spa->spa_did = 0; | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | /* | |
1255 | * Verify a pool configuration, and construct the vdev tree appropriately. This | |
1256 | * will create all the necessary vdevs in the appropriate layout, with each vdev | |
1257 | * in the CLOSED state. This will prep the pool before open/creation/import. | |
1258 | * All vdev validation is done by the vdev_alloc() routine. | |
1259 | */ | |
1260 | static int | |
1261 | spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, | |
1262 | uint_t id, int atype) | |
1263 | { | |
1264 | nvlist_t **child; | |
1265 | uint_t children; | |
1266 | int error; | |
1267 | int c; | |
1268 | ||
1269 | if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) | |
1270 | return (error); | |
1271 | ||
1272 | if ((*vdp)->vdev_ops->vdev_op_leaf) | |
1273 | return (0); | |
1274 | ||
1275 | error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
1276 | &child, &children); | |
1277 | ||
1278 | if (error == ENOENT) | |
1279 | return (0); | |
1280 | ||
1281 | if (error) { | |
1282 | vdev_free(*vdp); | |
1283 | *vdp = NULL; | |
1284 | return (SET_ERROR(EINVAL)); | |
1285 | } | |
1286 | ||
1287 | for (c = 0; c < children; c++) { | |
1288 | vdev_t *vd; | |
1289 | if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, | |
1290 | atype)) != 0) { | |
1291 | vdev_free(*vdp); | |
1292 | *vdp = NULL; | |
1293 | return (error); | |
1294 | } | |
1295 | } | |
1296 | ||
1297 | ASSERT(*vdp != NULL); | |
1298 | ||
1299 | return (0); | |
1300 | } | |
1301 | ||
1302 | /* | |
1303 | * Opposite of spa_load(). | |
1304 | */ | |
1305 | static void | |
1306 | spa_unload(spa_t *spa) | |
1307 | { | |
1308 | int i; | |
1309 | ||
1310 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
1311 | ||
1312 | /* | |
1313 | * Stop async tasks. | |
1314 | */ | |
1315 | spa_async_suspend(spa); | |
1316 | ||
1317 | /* | |
1318 | * Stop syncing. | |
1319 | */ | |
1320 | if (spa->spa_sync_on) { | |
1321 | txg_sync_stop(spa->spa_dsl_pool); | |
1322 | spa->spa_sync_on = B_FALSE; | |
1323 | } | |
1324 | ||
1325 | /* | |
1326 | * Wait for any outstanding async I/O to complete. | |
1327 | */ | |
1328 | if (spa->spa_async_zio_root != NULL) { | |
1329 | for (i = 0; i < max_ncpus; i++) | |
1330 | (void) zio_wait(spa->spa_async_zio_root[i]); | |
1331 | kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); | |
1332 | spa->spa_async_zio_root = NULL; | |
1333 | } | |
1334 | ||
1335 | bpobj_close(&spa->spa_deferred_bpobj); | |
1336 | ||
1337 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
1338 | ||
1339 | /* | |
1340 | * Close all vdevs. | |
1341 | */ | |
1342 | if (spa->spa_root_vdev) | |
1343 | vdev_free(spa->spa_root_vdev); | |
1344 | ASSERT(spa->spa_root_vdev == NULL); | |
1345 | ||
1346 | /* | |
1347 | * Close the dsl pool. | |
1348 | */ | |
1349 | if (spa->spa_dsl_pool) { | |
1350 | dsl_pool_close(spa->spa_dsl_pool); | |
1351 | spa->spa_dsl_pool = NULL; | |
1352 | spa->spa_meta_objset = NULL; | |
1353 | } | |
1354 | ||
1355 | ddt_unload(spa); | |
1356 | ||
1357 | ||
1358 | /* | |
1359 | * Drop and purge level 2 cache | |
1360 | */ | |
1361 | spa_l2cache_drop(spa); | |
1362 | ||
1363 | for (i = 0; i < spa->spa_spares.sav_count; i++) | |
1364 | vdev_free(spa->spa_spares.sav_vdevs[i]); | |
1365 | if (spa->spa_spares.sav_vdevs) { | |
1366 | kmem_free(spa->spa_spares.sav_vdevs, | |
1367 | spa->spa_spares.sav_count * sizeof (void *)); | |
1368 | spa->spa_spares.sav_vdevs = NULL; | |
1369 | } | |
1370 | if (spa->spa_spares.sav_config) { | |
1371 | nvlist_free(spa->spa_spares.sav_config); | |
1372 | spa->spa_spares.sav_config = NULL; | |
1373 | } | |
1374 | spa->spa_spares.sav_count = 0; | |
1375 | ||
1376 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { | |
1377 | vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); | |
1378 | vdev_free(spa->spa_l2cache.sav_vdevs[i]); | |
1379 | } | |
1380 | if (spa->spa_l2cache.sav_vdevs) { | |
1381 | kmem_free(spa->spa_l2cache.sav_vdevs, | |
1382 | spa->spa_l2cache.sav_count * sizeof (void *)); | |
1383 | spa->spa_l2cache.sav_vdevs = NULL; | |
1384 | } | |
1385 | if (spa->spa_l2cache.sav_config) { | |
1386 | nvlist_free(spa->spa_l2cache.sav_config); | |
1387 | spa->spa_l2cache.sav_config = NULL; | |
1388 | } | |
1389 | spa->spa_l2cache.sav_count = 0; | |
1390 | ||
1391 | spa->spa_async_suspended = 0; | |
1392 | ||
1393 | if (spa->spa_comment != NULL) { | |
1394 | spa_strfree(spa->spa_comment); | |
1395 | spa->spa_comment = NULL; | |
1396 | } | |
1397 | ||
1398 | spa_config_exit(spa, SCL_ALL, FTAG); | |
1399 | } | |
1400 | ||
1401 | /* | |
1402 | * Load (or re-load) the current list of vdevs describing the active spares for | |
1403 | * this pool. When this is called, we have some form of basic information in | |
1404 | * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and | |
1405 | * then re-generate a more complete list including status information. | |
1406 | */ | |
1407 | static void | |
1408 | spa_load_spares(spa_t *spa) | |
1409 | { | |
1410 | nvlist_t **spares; | |
1411 | uint_t nspares; | |
1412 | int i; | |
1413 | vdev_t *vd, *tvd; | |
1414 | ||
1415 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); | |
1416 | ||
1417 | /* | |
1418 | * First, close and free any existing spare vdevs. | |
1419 | */ | |
1420 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
1421 | vd = spa->spa_spares.sav_vdevs[i]; | |
1422 | ||
1423 | /* Undo the call to spa_activate() below */ | |
1424 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, | |
1425 | B_FALSE)) != NULL && tvd->vdev_isspare) | |
1426 | spa_spare_remove(tvd); | |
1427 | vdev_close(vd); | |
1428 | vdev_free(vd); | |
1429 | } | |
1430 | ||
1431 | if (spa->spa_spares.sav_vdevs) | |
1432 | kmem_free(spa->spa_spares.sav_vdevs, | |
1433 | spa->spa_spares.sav_count * sizeof (void *)); | |
1434 | ||
1435 | if (spa->spa_spares.sav_config == NULL) | |
1436 | nspares = 0; | |
1437 | else | |
1438 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
1439 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
1440 | ||
1441 | spa->spa_spares.sav_count = (int)nspares; | |
1442 | spa->spa_spares.sav_vdevs = NULL; | |
1443 | ||
1444 | if (nspares == 0) | |
1445 | return; | |
1446 | ||
1447 | /* | |
1448 | * Construct the array of vdevs, opening them to get status in the | |
1449 | * process. For each spare, there is potentially two different vdev_t | |
1450 | * structures associated with it: one in the list of spares (used only | |
1451 | * for basic validation purposes) and one in the active vdev | |
1452 | * configuration (if it's spared in). During this phase we open and | |
1453 | * validate each vdev on the spare list. If the vdev also exists in the | |
1454 | * active configuration, then we also mark this vdev as an active spare. | |
1455 | */ | |
1456 | spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *), | |
1457 | KM_SLEEP); | |
1458 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
1459 | VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, | |
1460 | VDEV_ALLOC_SPARE) == 0); | |
1461 | ASSERT(vd != NULL); | |
1462 | ||
1463 | spa->spa_spares.sav_vdevs[i] = vd; | |
1464 | ||
1465 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, | |
1466 | B_FALSE)) != NULL) { | |
1467 | if (!tvd->vdev_isspare) | |
1468 | spa_spare_add(tvd); | |
1469 | ||
1470 | /* | |
1471 | * We only mark the spare active if we were successfully | |
1472 | * able to load the vdev. Otherwise, importing a pool | |
1473 | * with a bad active spare would result in strange | |
1474 | * behavior, because multiple pool would think the spare | |
1475 | * is actively in use. | |
1476 | * | |
1477 | * There is a vulnerability here to an equally bizarre | |
1478 | * circumstance, where a dead active spare is later | |
1479 | * brought back to life (onlined or otherwise). Given | |
1480 | * the rarity of this scenario, and the extra complexity | |
1481 | * it adds, we ignore the possibility. | |
1482 | */ | |
1483 | if (!vdev_is_dead(tvd)) | |
1484 | spa_spare_activate(tvd); | |
1485 | } | |
1486 | ||
1487 | vd->vdev_top = vd; | |
1488 | vd->vdev_aux = &spa->spa_spares; | |
1489 | ||
1490 | if (vdev_open(vd) != 0) | |
1491 | continue; | |
1492 | ||
1493 | if (vdev_validate_aux(vd) == 0) | |
1494 | spa_spare_add(vd); | |
1495 | } | |
1496 | ||
1497 | /* | |
1498 | * Recompute the stashed list of spares, with status information | |
1499 | * this time. | |
1500 | */ | |
1501 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, | |
1502 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1503 | ||
1504 | spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), | |
1505 | KM_SLEEP); | |
1506 | for (i = 0; i < spa->spa_spares.sav_count; i++) | |
1507 | spares[i] = vdev_config_generate(spa, | |
1508 | spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); | |
1509 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, | |
1510 | ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); | |
1511 | for (i = 0; i < spa->spa_spares.sav_count; i++) | |
1512 | nvlist_free(spares[i]); | |
1513 | kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); | |
1514 | } | |
1515 | ||
1516 | /* | |
1517 | * Load (or re-load) the current list of vdevs describing the active l2cache for | |
1518 | * this pool. When this is called, we have some form of basic information in | |
1519 | * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and | |
1520 | * then re-generate a more complete list including status information. | |
1521 | * Devices which are already active have their details maintained, and are | |
1522 | * not re-opened. | |
1523 | */ | |
1524 | static void | |
1525 | spa_load_l2cache(spa_t *spa) | |
1526 | { | |
1527 | nvlist_t **l2cache; | |
1528 | uint_t nl2cache; | |
1529 | int i, j, oldnvdevs; | |
1530 | uint64_t guid; | |
1531 | vdev_t *vd, **oldvdevs, **newvdevs; | |
1532 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
1533 | ||
1534 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); | |
1535 | ||
1536 | if (sav->sav_config != NULL) { | |
1537 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, | |
1538 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
1539 | newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); | |
1540 | } else { | |
1541 | nl2cache = 0; | |
1542 | newvdevs = NULL; | |
1543 | } | |
1544 | ||
1545 | oldvdevs = sav->sav_vdevs; | |
1546 | oldnvdevs = sav->sav_count; | |
1547 | sav->sav_vdevs = NULL; | |
1548 | sav->sav_count = 0; | |
1549 | ||
1550 | /* | |
1551 | * Process new nvlist of vdevs. | |
1552 | */ | |
1553 | for (i = 0; i < nl2cache; i++) { | |
1554 | VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, | |
1555 | &guid) == 0); | |
1556 | ||
1557 | newvdevs[i] = NULL; | |
1558 | for (j = 0; j < oldnvdevs; j++) { | |
1559 | vd = oldvdevs[j]; | |
1560 | if (vd != NULL && guid == vd->vdev_guid) { | |
1561 | /* | |
1562 | * Retain previous vdev for add/remove ops. | |
1563 | */ | |
1564 | newvdevs[i] = vd; | |
1565 | oldvdevs[j] = NULL; | |
1566 | break; | |
1567 | } | |
1568 | } | |
1569 | ||
1570 | if (newvdevs[i] == NULL) { | |
1571 | /* | |
1572 | * Create new vdev | |
1573 | */ | |
1574 | VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, | |
1575 | VDEV_ALLOC_L2CACHE) == 0); | |
1576 | ASSERT(vd != NULL); | |
1577 | newvdevs[i] = vd; | |
1578 | ||
1579 | /* | |
1580 | * Commit this vdev as an l2cache device, | |
1581 | * even if it fails to open. | |
1582 | */ | |
1583 | spa_l2cache_add(vd); | |
1584 | ||
1585 | vd->vdev_top = vd; | |
1586 | vd->vdev_aux = sav; | |
1587 | ||
1588 | spa_l2cache_activate(vd); | |
1589 | ||
1590 | if (vdev_open(vd) != 0) | |
1591 | continue; | |
1592 | ||
1593 | (void) vdev_validate_aux(vd); | |
1594 | ||
1595 | if (!vdev_is_dead(vd)) | |
1596 | l2arc_add_vdev(spa, vd); | |
1597 | } | |
1598 | } | |
1599 | ||
1600 | /* | |
1601 | * Purge vdevs that were dropped | |
1602 | */ | |
1603 | for (i = 0; i < oldnvdevs; i++) { | |
1604 | uint64_t pool; | |
1605 | ||
1606 | vd = oldvdevs[i]; | |
1607 | if (vd != NULL) { | |
1608 | ASSERT(vd->vdev_isl2cache); | |
1609 | ||
1610 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && | |
1611 | pool != 0ULL && l2arc_vdev_present(vd)) | |
1612 | l2arc_remove_vdev(vd); | |
1613 | vdev_clear_stats(vd); | |
1614 | vdev_free(vd); | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | if (oldvdevs) | |
1619 | kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); | |
1620 | ||
1621 | if (sav->sav_config == NULL) | |
1622 | goto out; | |
1623 | ||
1624 | sav->sav_vdevs = newvdevs; | |
1625 | sav->sav_count = (int)nl2cache; | |
1626 | ||
1627 | /* | |
1628 | * Recompute the stashed list of l2cache devices, with status | |
1629 | * information this time. | |
1630 | */ | |
1631 | VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, | |
1632 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1633 | ||
1634 | l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); | |
1635 | for (i = 0; i < sav->sav_count; i++) | |
1636 | l2cache[i] = vdev_config_generate(spa, | |
1637 | sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); | |
1638 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
1639 | ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); | |
1640 | out: | |
1641 | for (i = 0; i < sav->sav_count; i++) | |
1642 | nvlist_free(l2cache[i]); | |
1643 | if (sav->sav_count) | |
1644 | kmem_free(l2cache, sav->sav_count * sizeof (void *)); | |
1645 | } | |
1646 | ||
1647 | static int | |
1648 | load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) | |
1649 | { | |
1650 | dmu_buf_t *db; | |
1651 | char *packed = NULL; | |
1652 | size_t nvsize = 0; | |
1653 | int error; | |
1654 | *value = NULL; | |
1655 | ||
1656 | error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); | |
1657 | if (error) | |
1658 | return (error); | |
1659 | ||
1660 | nvsize = *(uint64_t *)db->db_data; | |
1661 | dmu_buf_rele(db, FTAG); | |
1662 | ||
1663 | packed = vmem_alloc(nvsize, KM_SLEEP); | |
1664 | error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, | |
1665 | DMU_READ_PREFETCH); | |
1666 | if (error == 0) | |
1667 | error = nvlist_unpack(packed, nvsize, value, 0); | |
1668 | vmem_free(packed, nvsize); | |
1669 | ||
1670 | return (error); | |
1671 | } | |
1672 | ||
1673 | /* | |
1674 | * Checks to see if the given vdev could not be opened, in which case we post a | |
1675 | * sysevent to notify the autoreplace code that the device has been removed. | |
1676 | */ | |
1677 | static void | |
1678 | spa_check_removed(vdev_t *vd) | |
1679 | { | |
1680 | int c; | |
1681 | ||
1682 | for (c = 0; c < vd->vdev_children; c++) | |
1683 | spa_check_removed(vd->vdev_child[c]); | |
1684 | ||
1685 | if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && | |
1686 | !vd->vdev_ishole) { | |
1687 | zfs_post_autoreplace(vd->vdev_spa, vd); | |
1688 | spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); | |
1689 | } | |
1690 | } | |
1691 | ||
1692 | static void | |
1693 | spa_config_valid_zaps(vdev_t *vd, vdev_t *mvd) | |
1694 | { | |
1695 | uint64_t i; | |
1696 | ||
1697 | ASSERT3U(vd->vdev_children, ==, mvd->vdev_children); | |
1698 | ||
1699 | vd->vdev_top_zap = mvd->vdev_top_zap; | |
1700 | vd->vdev_leaf_zap = mvd->vdev_leaf_zap; | |
1701 | ||
1702 | for (i = 0; i < vd->vdev_children; i++) { | |
1703 | spa_config_valid_zaps(vd->vdev_child[i], mvd->vdev_child[i]); | |
1704 | } | |
1705 | } | |
1706 | ||
1707 | /* | |
1708 | * Validate the current config against the MOS config | |
1709 | */ | |
1710 | static boolean_t | |
1711 | spa_config_valid(spa_t *spa, nvlist_t *config) | |
1712 | { | |
1713 | vdev_t *mrvd, *rvd = spa->spa_root_vdev; | |
1714 | nvlist_t *nv; | |
1715 | int c, i; | |
1716 | ||
1717 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); | |
1718 | ||
1719 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
1720 | VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); | |
1721 | ||
1722 | ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); | |
1723 | ||
1724 | /* | |
1725 | * If we're doing a normal import, then build up any additional | |
1726 | * diagnostic information about missing devices in this config. | |
1727 | * We'll pass this up to the user for further processing. | |
1728 | */ | |
1729 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { | |
1730 | nvlist_t **child, *nv; | |
1731 | uint64_t idx = 0; | |
1732 | ||
1733 | child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), | |
1734 | KM_SLEEP); | |
1735 | VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
1736 | ||
1737 | for (c = 0; c < rvd->vdev_children; c++) { | |
1738 | vdev_t *tvd = rvd->vdev_child[c]; | |
1739 | vdev_t *mtvd = mrvd->vdev_child[c]; | |
1740 | ||
1741 | if (tvd->vdev_ops == &vdev_missing_ops && | |
1742 | mtvd->vdev_ops != &vdev_missing_ops && | |
1743 | mtvd->vdev_islog) | |
1744 | child[idx++] = vdev_config_generate(spa, mtvd, | |
1745 | B_FALSE, 0); | |
1746 | } | |
1747 | ||
1748 | if (idx) { | |
1749 | VERIFY(nvlist_add_nvlist_array(nv, | |
1750 | ZPOOL_CONFIG_CHILDREN, child, idx) == 0); | |
1751 | VERIFY(nvlist_add_nvlist(spa->spa_load_info, | |
1752 | ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); | |
1753 | ||
1754 | for (i = 0; i < idx; i++) | |
1755 | nvlist_free(child[i]); | |
1756 | } | |
1757 | nvlist_free(nv); | |
1758 | kmem_free(child, rvd->vdev_children * sizeof (char **)); | |
1759 | } | |
1760 | ||
1761 | /* | |
1762 | * Compare the root vdev tree with the information we have | |
1763 | * from the MOS config (mrvd). Check each top-level vdev | |
1764 | * with the corresponding MOS config top-level (mtvd). | |
1765 | */ | |
1766 | for (c = 0; c < rvd->vdev_children; c++) { | |
1767 | vdev_t *tvd = rvd->vdev_child[c]; | |
1768 | vdev_t *mtvd = mrvd->vdev_child[c]; | |
1769 | ||
1770 | /* | |
1771 | * Resolve any "missing" vdevs in the current configuration. | |
1772 | * If we find that the MOS config has more accurate information | |
1773 | * about the top-level vdev then use that vdev instead. | |
1774 | */ | |
1775 | if (tvd->vdev_ops == &vdev_missing_ops && | |
1776 | mtvd->vdev_ops != &vdev_missing_ops) { | |
1777 | ||
1778 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) | |
1779 | continue; | |
1780 | ||
1781 | /* | |
1782 | * Device specific actions. | |
1783 | */ | |
1784 | if (mtvd->vdev_islog) { | |
1785 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
1786 | } else { | |
1787 | /* | |
1788 | * XXX - once we have 'readonly' pool | |
1789 | * support we should be able to handle | |
1790 | * missing data devices by transitioning | |
1791 | * the pool to readonly. | |
1792 | */ | |
1793 | continue; | |
1794 | } | |
1795 | ||
1796 | /* | |
1797 | * Swap the missing vdev with the data we were | |
1798 | * able to obtain from the MOS config. | |
1799 | */ | |
1800 | vdev_remove_child(rvd, tvd); | |
1801 | vdev_remove_child(mrvd, mtvd); | |
1802 | ||
1803 | vdev_add_child(rvd, mtvd); | |
1804 | vdev_add_child(mrvd, tvd); | |
1805 | ||
1806 | spa_config_exit(spa, SCL_ALL, FTAG); | |
1807 | vdev_load(mtvd); | |
1808 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
1809 | ||
1810 | vdev_reopen(rvd); | |
1811 | } else { | |
1812 | if (mtvd->vdev_islog) { | |
1813 | /* | |
1814 | * Load the slog device's state from the MOS | |
1815 | * config since it's possible that the label | |
1816 | * does not contain the most up-to-date | |
1817 | * information. | |
1818 | */ | |
1819 | vdev_load_log_state(tvd, mtvd); | |
1820 | vdev_reopen(tvd); | |
1821 | } | |
1822 | ||
1823 | /* | |
1824 | * Per-vdev ZAP info is stored exclusively in the MOS. | |
1825 | */ | |
1826 | spa_config_valid_zaps(tvd, mtvd); | |
1827 | } | |
1828 | } | |
1829 | ||
1830 | vdev_free(mrvd); | |
1831 | spa_config_exit(spa, SCL_ALL, FTAG); | |
1832 | ||
1833 | /* | |
1834 | * Ensure we were able to validate the config. | |
1835 | */ | |
1836 | return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); | |
1837 | } | |
1838 | ||
1839 | /* | |
1840 | * Check for missing log devices | |
1841 | */ | |
1842 | static boolean_t | |
1843 | spa_check_logs(spa_t *spa) | |
1844 | { | |
1845 | boolean_t rv = B_FALSE; | |
1846 | dsl_pool_t *dp = spa_get_dsl(spa); | |
1847 | ||
1848 | switch (spa->spa_log_state) { | |
1849 | default: | |
1850 | break; | |
1851 | case SPA_LOG_MISSING: | |
1852 | /* need to recheck in case slog has been restored */ | |
1853 | case SPA_LOG_UNKNOWN: | |
1854 | rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, | |
1855 | zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); | |
1856 | if (rv) | |
1857 | spa_set_log_state(spa, SPA_LOG_MISSING); | |
1858 | break; | |
1859 | } | |
1860 | return (rv); | |
1861 | } | |
1862 | ||
1863 | static boolean_t | |
1864 | spa_passivate_log(spa_t *spa) | |
1865 | { | |
1866 | vdev_t *rvd = spa->spa_root_vdev; | |
1867 | boolean_t slog_found = B_FALSE; | |
1868 | int c; | |
1869 | ||
1870 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); | |
1871 | ||
1872 | if (!spa_has_slogs(spa)) | |
1873 | return (B_FALSE); | |
1874 | ||
1875 | for (c = 0; c < rvd->vdev_children; c++) { | |
1876 | vdev_t *tvd = rvd->vdev_child[c]; | |
1877 | metaslab_group_t *mg = tvd->vdev_mg; | |
1878 | ||
1879 | if (tvd->vdev_islog) { | |
1880 | metaslab_group_passivate(mg); | |
1881 | slog_found = B_TRUE; | |
1882 | } | |
1883 | } | |
1884 | ||
1885 | return (slog_found); | |
1886 | } | |
1887 | ||
1888 | static void | |
1889 | spa_activate_log(spa_t *spa) | |
1890 | { | |
1891 | vdev_t *rvd = spa->spa_root_vdev; | |
1892 | int c; | |
1893 | ||
1894 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); | |
1895 | ||
1896 | for (c = 0; c < rvd->vdev_children; c++) { | |
1897 | vdev_t *tvd = rvd->vdev_child[c]; | |
1898 | metaslab_group_t *mg = tvd->vdev_mg; | |
1899 | ||
1900 | if (tvd->vdev_islog) | |
1901 | metaslab_group_activate(mg); | |
1902 | } | |
1903 | } | |
1904 | ||
1905 | int | |
1906 | spa_offline_log(spa_t *spa) | |
1907 | { | |
1908 | int error; | |
1909 | ||
1910 | error = dmu_objset_find(spa_name(spa), zil_vdev_offline, | |
1911 | NULL, DS_FIND_CHILDREN); | |
1912 | if (error == 0) { | |
1913 | /* | |
1914 | * We successfully offlined the log device, sync out the | |
1915 | * current txg so that the "stubby" block can be removed | |
1916 | * by zil_sync(). | |
1917 | */ | |
1918 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
1919 | } | |
1920 | return (error); | |
1921 | } | |
1922 | ||
1923 | static void | |
1924 | spa_aux_check_removed(spa_aux_vdev_t *sav) | |
1925 | { | |
1926 | int i; | |
1927 | ||
1928 | for (i = 0; i < sav->sav_count; i++) | |
1929 | spa_check_removed(sav->sav_vdevs[i]); | |
1930 | } | |
1931 | ||
1932 | void | |
1933 | spa_claim_notify(zio_t *zio) | |
1934 | { | |
1935 | spa_t *spa = zio->io_spa; | |
1936 | ||
1937 | if (zio->io_error) | |
1938 | return; | |
1939 | ||
1940 | mutex_enter(&spa->spa_props_lock); /* any mutex will do */ | |
1941 | if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) | |
1942 | spa->spa_claim_max_txg = zio->io_bp->blk_birth; | |
1943 | mutex_exit(&spa->spa_props_lock); | |
1944 | } | |
1945 | ||
1946 | typedef struct spa_load_error { | |
1947 | uint64_t sle_meta_count; | |
1948 | uint64_t sle_data_count; | |
1949 | } spa_load_error_t; | |
1950 | ||
1951 | static void | |
1952 | spa_load_verify_done(zio_t *zio) | |
1953 | { | |
1954 | blkptr_t *bp = zio->io_bp; | |
1955 | spa_load_error_t *sle = zio->io_private; | |
1956 | dmu_object_type_t type = BP_GET_TYPE(bp); | |
1957 | int error = zio->io_error; | |
1958 | spa_t *spa = zio->io_spa; | |
1959 | ||
1960 | if (error) { | |
1961 | if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && | |
1962 | type != DMU_OT_INTENT_LOG) | |
1963 | atomic_inc_64(&sle->sle_meta_count); | |
1964 | else | |
1965 | atomic_inc_64(&sle->sle_data_count); | |
1966 | } | |
1967 | zio_data_buf_free(zio->io_data, zio->io_size); | |
1968 | ||
1969 | mutex_enter(&spa->spa_scrub_lock); | |
1970 | spa->spa_scrub_inflight--; | |
1971 | cv_broadcast(&spa->spa_scrub_io_cv); | |
1972 | mutex_exit(&spa->spa_scrub_lock); | |
1973 | } | |
1974 | ||
1975 | /* | |
1976 | * Maximum number of concurrent scrub i/os to create while verifying | |
1977 | * a pool while importing it. | |
1978 | */ | |
1979 | int spa_load_verify_maxinflight = 10000; | |
1980 | int spa_load_verify_metadata = B_TRUE; | |
1981 | int spa_load_verify_data = B_TRUE; | |
1982 | ||
1983 | /*ARGSUSED*/ | |
1984 | static int | |
1985 | spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
1986 | const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) | |
1987 | { | |
1988 | zio_t *rio; | |
1989 | size_t size; | |
1990 | void *data; | |
1991 | ||
1992 | if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) | |
1993 | return (0); | |
1994 | /* | |
1995 | * Note: normally this routine will not be called if | |
1996 | * spa_load_verify_metadata is not set. However, it may be useful | |
1997 | * to manually set the flag after the traversal has begun. | |
1998 | */ | |
1999 | if (!spa_load_verify_metadata) | |
2000 | return (0); | |
2001 | if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data) | |
2002 | return (0); | |
2003 | ||
2004 | rio = arg; | |
2005 | size = BP_GET_PSIZE(bp); | |
2006 | data = zio_data_buf_alloc(size); | |
2007 | ||
2008 | mutex_enter(&spa->spa_scrub_lock); | |
2009 | while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight) | |
2010 | cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); | |
2011 | spa->spa_scrub_inflight++; | |
2012 | mutex_exit(&spa->spa_scrub_lock); | |
2013 | ||
2014 | zio_nowait(zio_read(rio, spa, bp, data, size, | |
2015 | spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, | |
2016 | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | | |
2017 | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); | |
2018 | return (0); | |
2019 | } | |
2020 | ||
2021 | /* ARGSUSED */ | |
2022 | int | |
2023 | verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) | |
2024 | { | |
2025 | if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) | |
2026 | return (SET_ERROR(ENAMETOOLONG)); | |
2027 | ||
2028 | return (0); | |
2029 | } | |
2030 | ||
2031 | static int | |
2032 | spa_load_verify(spa_t *spa) | |
2033 | { | |
2034 | zio_t *rio; | |
2035 | spa_load_error_t sle = { 0 }; | |
2036 | zpool_rewind_policy_t policy; | |
2037 | boolean_t verify_ok = B_FALSE; | |
2038 | int error = 0; | |
2039 | ||
2040 | zpool_get_rewind_policy(spa->spa_config, &policy); | |
2041 | ||
2042 | if (policy.zrp_request & ZPOOL_NEVER_REWIND) | |
2043 | return (0); | |
2044 | ||
2045 | dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); | |
2046 | error = dmu_objset_find_dp(spa->spa_dsl_pool, | |
2047 | spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, | |
2048 | DS_FIND_CHILDREN); | |
2049 | dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); | |
2050 | if (error != 0) | |
2051 | return (error); | |
2052 | ||
2053 | rio = zio_root(spa, NULL, &sle, | |
2054 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); | |
2055 | ||
2056 | if (spa_load_verify_metadata) { | |
2057 | error = traverse_pool(spa, spa->spa_verify_min_txg, | |
2058 | TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, | |
2059 | spa_load_verify_cb, rio); | |
2060 | } | |
2061 | ||
2062 | (void) zio_wait(rio); | |
2063 | ||
2064 | spa->spa_load_meta_errors = sle.sle_meta_count; | |
2065 | spa->spa_load_data_errors = sle.sle_data_count; | |
2066 | ||
2067 | if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && | |
2068 | sle.sle_data_count <= policy.zrp_maxdata) { | |
2069 | int64_t loss = 0; | |
2070 | ||
2071 | verify_ok = B_TRUE; | |
2072 | spa->spa_load_txg = spa->spa_uberblock.ub_txg; | |
2073 | spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; | |
2074 | ||
2075 | loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; | |
2076 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
2077 | ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); | |
2078 | VERIFY(nvlist_add_int64(spa->spa_load_info, | |
2079 | ZPOOL_CONFIG_REWIND_TIME, loss) == 0); | |
2080 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
2081 | ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); | |
2082 | } else { | |
2083 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; | |
2084 | } | |
2085 | ||
2086 | if (error) { | |
2087 | if (error != ENXIO && error != EIO) | |
2088 | error = SET_ERROR(EIO); | |
2089 | return (error); | |
2090 | } | |
2091 | ||
2092 | return (verify_ok ? 0 : EIO); | |
2093 | } | |
2094 | ||
2095 | /* | |
2096 | * Find a value in the pool props object. | |
2097 | */ | |
2098 | static void | |
2099 | spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) | |
2100 | { | |
2101 | (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, | |
2102 | zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); | |
2103 | } | |
2104 | ||
2105 | /* | |
2106 | * Find a value in the pool directory object. | |
2107 | */ | |
2108 | static int | |
2109 | spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) | |
2110 | { | |
2111 | return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
2112 | name, sizeof (uint64_t), 1, val)); | |
2113 | } | |
2114 | ||
2115 | static int | |
2116 | spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) | |
2117 | { | |
2118 | vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); | |
2119 | return (err); | |
2120 | } | |
2121 | ||
2122 | /* | |
2123 | * Fix up config after a partly-completed split. This is done with the | |
2124 | * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off | |
2125 | * pool have that entry in their config, but only the splitting one contains | |
2126 | * a list of all the guids of the vdevs that are being split off. | |
2127 | * | |
2128 | * This function determines what to do with that list: either rejoin | |
2129 | * all the disks to the pool, or complete the splitting process. To attempt | |
2130 | * the rejoin, each disk that is offlined is marked online again, and | |
2131 | * we do a reopen() call. If the vdev label for every disk that was | |
2132 | * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) | |
2133 | * then we call vdev_split() on each disk, and complete the split. | |
2134 | * | |
2135 | * Otherwise we leave the config alone, with all the vdevs in place in | |
2136 | * the original pool. | |
2137 | */ | |
2138 | static void | |
2139 | spa_try_repair(spa_t *spa, nvlist_t *config) | |
2140 | { | |
2141 | uint_t extracted; | |
2142 | uint64_t *glist; | |
2143 | uint_t i, gcount; | |
2144 | nvlist_t *nvl; | |
2145 | vdev_t **vd; | |
2146 | boolean_t attempt_reopen; | |
2147 | ||
2148 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) | |
2149 | return; | |
2150 | ||
2151 | /* check that the config is complete */ | |
2152 | if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, | |
2153 | &glist, &gcount) != 0) | |
2154 | return; | |
2155 | ||
2156 | vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); | |
2157 | ||
2158 | /* attempt to online all the vdevs & validate */ | |
2159 | attempt_reopen = B_TRUE; | |
2160 | for (i = 0; i < gcount; i++) { | |
2161 | if (glist[i] == 0) /* vdev is hole */ | |
2162 | continue; | |
2163 | ||
2164 | vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); | |
2165 | if (vd[i] == NULL) { | |
2166 | /* | |
2167 | * Don't bother attempting to reopen the disks; | |
2168 | * just do the split. | |
2169 | */ | |
2170 | attempt_reopen = B_FALSE; | |
2171 | } else { | |
2172 | /* attempt to re-online it */ | |
2173 | vd[i]->vdev_offline = B_FALSE; | |
2174 | } | |
2175 | } | |
2176 | ||
2177 | if (attempt_reopen) { | |
2178 | vdev_reopen(spa->spa_root_vdev); | |
2179 | ||
2180 | /* check each device to see what state it's in */ | |
2181 | for (extracted = 0, i = 0; i < gcount; i++) { | |
2182 | if (vd[i] != NULL && | |
2183 | vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) | |
2184 | break; | |
2185 | ++extracted; | |
2186 | } | |
2187 | } | |
2188 | ||
2189 | /* | |
2190 | * If every disk has been moved to the new pool, or if we never | |
2191 | * even attempted to look at them, then we split them off for | |
2192 | * good. | |
2193 | */ | |
2194 | if (!attempt_reopen || gcount == extracted) { | |
2195 | for (i = 0; i < gcount; i++) | |
2196 | if (vd[i] != NULL) | |
2197 | vdev_split(vd[i]); | |
2198 | vdev_reopen(spa->spa_root_vdev); | |
2199 | } | |
2200 | ||
2201 | kmem_free(vd, gcount * sizeof (vdev_t *)); | |
2202 | } | |
2203 | ||
2204 | static int | |
2205 | spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, | |
2206 | boolean_t mosconfig) | |
2207 | { | |
2208 | nvlist_t *config = spa->spa_config; | |
2209 | char *ereport = FM_EREPORT_ZFS_POOL; | |
2210 | char *comment; | |
2211 | int error; | |
2212 | uint64_t pool_guid; | |
2213 | nvlist_t *nvl; | |
2214 | ||
2215 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) | |
2216 | return (SET_ERROR(EINVAL)); | |
2217 | ||
2218 | ASSERT(spa->spa_comment == NULL); | |
2219 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
2220 | spa->spa_comment = spa_strdup(comment); | |
2221 | ||
2222 | /* | |
2223 | * Versioning wasn't explicitly added to the label until later, so if | |
2224 | * it's not present treat it as the initial version. | |
2225 | */ | |
2226 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
2227 | &spa->spa_ubsync.ub_version) != 0) | |
2228 | spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; | |
2229 | ||
2230 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
2231 | &spa->spa_config_txg); | |
2232 | ||
2233 | if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && | |
2234 | spa_guid_exists(pool_guid, 0)) { | |
2235 | error = SET_ERROR(EEXIST); | |
2236 | } else { | |
2237 | spa->spa_config_guid = pool_guid; | |
2238 | ||
2239 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, | |
2240 | &nvl) == 0) { | |
2241 | VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, | |
2242 | KM_SLEEP) == 0); | |
2243 | } | |
2244 | ||
2245 | nvlist_free(spa->spa_load_info); | |
2246 | spa->spa_load_info = fnvlist_alloc(); | |
2247 | ||
2248 | gethrestime(&spa->spa_loaded_ts); | |
2249 | error = spa_load_impl(spa, pool_guid, config, state, type, | |
2250 | mosconfig, &ereport); | |
2251 | } | |
2252 | ||
2253 | /* | |
2254 | * Don't count references from objsets that are already closed | |
2255 | * and are making their way through the eviction process. | |
2256 | */ | |
2257 | spa_evicting_os_wait(spa); | |
2258 | spa->spa_minref = refcount_count(&spa->spa_refcount); | |
2259 | if (error) { | |
2260 | if (error != EEXIST) { | |
2261 | spa->spa_loaded_ts.tv_sec = 0; | |
2262 | spa->spa_loaded_ts.tv_nsec = 0; | |
2263 | } | |
2264 | if (error != EBADF) { | |
2265 | zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); | |
2266 | } | |
2267 | } | |
2268 | spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; | |
2269 | spa->spa_ena = 0; | |
2270 | ||
2271 | return (error); | |
2272 | } | |
2273 | ||
2274 | #ifdef ZFS_DEBUG | |
2275 | /* | |
2276 | * Count the number of per-vdev ZAPs associated with all of the vdevs in the | |
2277 | * vdev tree rooted in the given vd, and ensure that each ZAP is present in the | |
2278 | * spa's per-vdev ZAP list. | |
2279 | */ | |
2280 | static uint64_t | |
2281 | vdev_count_verify_zaps(vdev_t *vd) | |
2282 | { | |
2283 | spa_t *spa = vd->vdev_spa; | |
2284 | uint64_t total = 0; | |
2285 | uint64_t i; | |
2286 | ||
2287 | if (vd->vdev_top_zap != 0) { | |
2288 | total++; | |
2289 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
2290 | spa->spa_all_vdev_zaps, vd->vdev_top_zap)); | |
2291 | } | |
2292 | if (vd->vdev_leaf_zap != 0) { | |
2293 | total++; | |
2294 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
2295 | spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); | |
2296 | } | |
2297 | ||
2298 | for (i = 0; i < vd->vdev_children; i++) { | |
2299 | total += vdev_count_verify_zaps(vd->vdev_child[i]); | |
2300 | } | |
2301 | ||
2302 | return (total); | |
2303 | } | |
2304 | #endif | |
2305 | ||
2306 | /* | |
2307 | * Load an existing storage pool, using the pool's builtin spa_config as a | |
2308 | * source of configuration information. | |
2309 | */ | |
2310 | __attribute__((always_inline)) | |
2311 | static inline int | |
2312 | spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, | |
2313 | spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, | |
2314 | char **ereport) | |
2315 | { | |
2316 | int error = 0; | |
2317 | nvlist_t *nvroot = NULL; | |
2318 | nvlist_t *label; | |
2319 | vdev_t *rvd; | |
2320 | uberblock_t *ub = &spa->spa_uberblock; | |
2321 | uint64_t children, config_cache_txg = spa->spa_config_txg; | |
2322 | int orig_mode = spa->spa_mode; | |
2323 | int parse, i; | |
2324 | uint64_t obj; | |
2325 | boolean_t missing_feat_write = B_FALSE; | |
2326 | nvlist_t *mos_config; | |
2327 | ||
2328 | /* | |
2329 | * If this is an untrusted config, access the pool in read-only mode. | |
2330 | * This prevents things like resilvering recently removed devices. | |
2331 | */ | |
2332 | if (!mosconfig) | |
2333 | spa->spa_mode = FREAD; | |
2334 | ||
2335 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
2336 | ||
2337 | spa->spa_load_state = state; | |
2338 | ||
2339 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) | |
2340 | return (SET_ERROR(EINVAL)); | |
2341 | ||
2342 | parse = (type == SPA_IMPORT_EXISTING ? | |
2343 | VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); | |
2344 | ||
2345 | /* | |
2346 | * Create "The Godfather" zio to hold all async IOs | |
2347 | */ | |
2348 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), | |
2349 | KM_SLEEP); | |
2350 | for (i = 0; i < max_ncpus; i++) { | |
2351 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, | |
2352 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
2353 | ZIO_FLAG_GODFATHER); | |
2354 | } | |
2355 | ||
2356 | /* | |
2357 | * Parse the configuration into a vdev tree. We explicitly set the | |
2358 | * value that will be returned by spa_version() since parsing the | |
2359 | * configuration requires knowing the version number. | |
2360 | */ | |
2361 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
2362 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); | |
2363 | spa_config_exit(spa, SCL_ALL, FTAG); | |
2364 | ||
2365 | if (error != 0) | |
2366 | return (error); | |
2367 | ||
2368 | ASSERT(spa->spa_root_vdev == rvd); | |
2369 | ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); | |
2370 | ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); | |
2371 | ||
2372 | if (type != SPA_IMPORT_ASSEMBLE) { | |
2373 | ASSERT(spa_guid(spa) == pool_guid); | |
2374 | } | |
2375 | ||
2376 | /* | |
2377 | * Try to open all vdevs, loading each label in the process. | |
2378 | */ | |
2379 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
2380 | error = vdev_open(rvd); | |
2381 | spa_config_exit(spa, SCL_ALL, FTAG); | |
2382 | if (error != 0) | |
2383 | return (error); | |
2384 | ||
2385 | /* | |
2386 | * We need to validate the vdev labels against the configuration that | |
2387 | * we have in hand, which is dependent on the setting of mosconfig. If | |
2388 | * mosconfig is true then we're validating the vdev labels based on | |
2389 | * that config. Otherwise, we're validating against the cached config | |
2390 | * (zpool.cache) that was read when we loaded the zfs module, and then | |
2391 | * later we will recursively call spa_load() and validate against | |
2392 | * the vdev config. | |
2393 | * | |
2394 | * If we're assembling a new pool that's been split off from an | |
2395 | * existing pool, the labels haven't yet been updated so we skip | |
2396 | * validation for now. | |
2397 | */ | |
2398 | if (type != SPA_IMPORT_ASSEMBLE) { | |
2399 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
2400 | error = vdev_validate(rvd, mosconfig); | |
2401 | spa_config_exit(spa, SCL_ALL, FTAG); | |
2402 | ||
2403 | if (error != 0) | |
2404 | return (error); | |
2405 | ||
2406 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) | |
2407 | return (SET_ERROR(ENXIO)); | |
2408 | } | |
2409 | ||
2410 | /* | |
2411 | * Find the best uberblock. | |
2412 | */ | |
2413 | vdev_uberblock_load(rvd, ub, &label); | |
2414 | ||
2415 | /* | |
2416 | * If we weren't able to find a single valid uberblock, return failure. | |
2417 | */ | |
2418 | if (ub->ub_txg == 0) { | |
2419 | nvlist_free(label); | |
2420 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); | |
2421 | } | |
2422 | ||
2423 | /* | |
2424 | * If the pool has an unsupported version we can't open it. | |
2425 | */ | |
2426 | if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { | |
2427 | nvlist_free(label); | |
2428 | return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); | |
2429 | } | |
2430 | ||
2431 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
2432 | nvlist_t *features; | |
2433 | ||
2434 | /* | |
2435 | * If we weren't able to find what's necessary for reading the | |
2436 | * MOS in the label, return failure. | |
2437 | */ | |
2438 | if (label == NULL || nvlist_lookup_nvlist(label, | |
2439 | ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { | |
2440 | nvlist_free(label); | |
2441 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
2442 | ENXIO)); | |
2443 | } | |
2444 | ||
2445 | /* | |
2446 | * Update our in-core representation with the definitive values | |
2447 | * from the label. | |
2448 | */ | |
2449 | nvlist_free(spa->spa_label_features); | |
2450 | VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); | |
2451 | } | |
2452 | ||
2453 | nvlist_free(label); | |
2454 | ||
2455 | /* | |
2456 | * Look through entries in the label nvlist's features_for_read. If | |
2457 | * there is a feature listed there which we don't understand then we | |
2458 | * cannot open a pool. | |
2459 | */ | |
2460 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
2461 | nvlist_t *unsup_feat; | |
2462 | nvpair_t *nvp; | |
2463 | ||
2464 | VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == | |
2465 | 0); | |
2466 | ||
2467 | for (nvp = nvlist_next_nvpair(spa->spa_label_features, NULL); | |
2468 | nvp != NULL; | |
2469 | nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { | |
2470 | if (!zfeature_is_supported(nvpair_name(nvp))) { | |
2471 | VERIFY(nvlist_add_string(unsup_feat, | |
2472 | nvpair_name(nvp), "") == 0); | |
2473 | } | |
2474 | } | |
2475 | ||
2476 | if (!nvlist_empty(unsup_feat)) { | |
2477 | VERIFY(nvlist_add_nvlist(spa->spa_load_info, | |
2478 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); | |
2479 | nvlist_free(unsup_feat); | |
2480 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, | |
2481 | ENOTSUP)); | |
2482 | } | |
2483 | ||
2484 | nvlist_free(unsup_feat); | |
2485 | } | |
2486 | ||
2487 | /* | |
2488 | * If the vdev guid sum doesn't match the uberblock, we have an | |
2489 | * incomplete configuration. We first check to see if the pool | |
2490 | * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). | |
2491 | * If it is, defer the vdev_guid_sum check till later so we | |
2492 | * can handle missing vdevs. | |
2493 | */ | |
2494 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, | |
2495 | &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && | |
2496 | rvd->vdev_guid_sum != ub->ub_guid_sum) | |
2497 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); | |
2498 | ||
2499 | if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { | |
2500 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
2501 | spa_try_repair(spa, config); | |
2502 | spa_config_exit(spa, SCL_ALL, FTAG); | |
2503 | nvlist_free(spa->spa_config_splitting); | |
2504 | spa->spa_config_splitting = NULL; | |
2505 | } | |
2506 | ||
2507 | /* | |
2508 | * Initialize internal SPA structures. | |
2509 | */ | |
2510 | spa->spa_state = POOL_STATE_ACTIVE; | |
2511 | spa->spa_ubsync = spa->spa_uberblock; | |
2512 | spa->spa_verify_min_txg = spa->spa_extreme_rewind ? | |
2513 | TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; | |
2514 | spa->spa_first_txg = spa->spa_last_ubsync_txg ? | |
2515 | spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; | |
2516 | spa->spa_claim_max_txg = spa->spa_first_txg; | |
2517 | spa->spa_prev_software_version = ub->ub_software_version; | |
2518 | ||
2519 | error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); | |
2520 | if (error) | |
2521 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2522 | spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; | |
2523 | ||
2524 | if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) | |
2525 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2526 | ||
2527 | if (spa_version(spa) >= SPA_VERSION_FEATURES) { | |
2528 | boolean_t missing_feat_read = B_FALSE; | |
2529 | nvlist_t *unsup_feat, *enabled_feat; | |
2530 | spa_feature_t i; | |
2531 | ||
2532 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, | |
2533 | &spa->spa_feat_for_read_obj) != 0) { | |
2534 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2535 | } | |
2536 | ||
2537 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, | |
2538 | &spa->spa_feat_for_write_obj) != 0) { | |
2539 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2540 | } | |
2541 | ||
2542 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, | |
2543 | &spa->spa_feat_desc_obj) != 0) { | |
2544 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2545 | } | |
2546 | ||
2547 | enabled_feat = fnvlist_alloc(); | |
2548 | unsup_feat = fnvlist_alloc(); | |
2549 | ||
2550 | if (!spa_features_check(spa, B_FALSE, | |
2551 | unsup_feat, enabled_feat)) | |
2552 | missing_feat_read = B_TRUE; | |
2553 | ||
2554 | if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { | |
2555 | if (!spa_features_check(spa, B_TRUE, | |
2556 | unsup_feat, enabled_feat)) { | |
2557 | missing_feat_write = B_TRUE; | |
2558 | } | |
2559 | } | |
2560 | ||
2561 | fnvlist_add_nvlist(spa->spa_load_info, | |
2562 | ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); | |
2563 | ||
2564 | if (!nvlist_empty(unsup_feat)) { | |
2565 | fnvlist_add_nvlist(spa->spa_load_info, | |
2566 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
2567 | } | |
2568 | ||
2569 | fnvlist_free(enabled_feat); | |
2570 | fnvlist_free(unsup_feat); | |
2571 | ||
2572 | if (!missing_feat_read) { | |
2573 | fnvlist_add_boolean(spa->spa_load_info, | |
2574 | ZPOOL_CONFIG_CAN_RDONLY); | |
2575 | } | |
2576 | ||
2577 | /* | |
2578 | * If the state is SPA_LOAD_TRYIMPORT, our objective is | |
2579 | * twofold: to determine whether the pool is available for | |
2580 | * import in read-write mode and (if it is not) whether the | |
2581 | * pool is available for import in read-only mode. If the pool | |
2582 | * is available for import in read-write mode, it is displayed | |
2583 | * as available in userland; if it is not available for import | |
2584 | * in read-only mode, it is displayed as unavailable in | |
2585 | * userland. If the pool is available for import in read-only | |
2586 | * mode but not read-write mode, it is displayed as unavailable | |
2587 | * in userland with a special note that the pool is actually | |
2588 | * available for open in read-only mode. | |
2589 | * | |
2590 | * As a result, if the state is SPA_LOAD_TRYIMPORT and we are | |
2591 | * missing a feature for write, we must first determine whether | |
2592 | * the pool can be opened read-only before returning to | |
2593 | * userland in order to know whether to display the | |
2594 | * abovementioned note. | |
2595 | */ | |
2596 | if (missing_feat_read || (missing_feat_write && | |
2597 | spa_writeable(spa))) { | |
2598 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, | |
2599 | ENOTSUP)); | |
2600 | } | |
2601 | ||
2602 | /* | |
2603 | * Load refcounts for ZFS features from disk into an in-memory | |
2604 | * cache during SPA initialization. | |
2605 | */ | |
2606 | for (i = 0; i < SPA_FEATURES; i++) { | |
2607 | uint64_t refcount; | |
2608 | ||
2609 | error = feature_get_refcount_from_disk(spa, | |
2610 | &spa_feature_table[i], &refcount); | |
2611 | if (error == 0) { | |
2612 | spa->spa_feat_refcount_cache[i] = refcount; | |
2613 | } else if (error == ENOTSUP) { | |
2614 | spa->spa_feat_refcount_cache[i] = | |
2615 | SPA_FEATURE_DISABLED; | |
2616 | } else { | |
2617 | return (spa_vdev_err(rvd, | |
2618 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
2619 | } | |
2620 | } | |
2621 | } | |
2622 | ||
2623 | if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { | |
2624 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, | |
2625 | &spa->spa_feat_enabled_txg_obj) != 0) | |
2626 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2627 | } | |
2628 | ||
2629 | spa->spa_is_initializing = B_TRUE; | |
2630 | error = dsl_pool_open(spa->spa_dsl_pool); | |
2631 | spa->spa_is_initializing = B_FALSE; | |
2632 | if (error != 0) | |
2633 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2634 | ||
2635 | if (!mosconfig) { | |
2636 | uint64_t hostid; | |
2637 | nvlist_t *policy = NULL, *nvconfig; | |
2638 | ||
2639 | if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) | |
2640 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2641 | ||
2642 | if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, | |
2643 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { | |
2644 | char *hostname; | |
2645 | unsigned long myhostid = 0; | |
2646 | ||
2647 | VERIFY(nvlist_lookup_string(nvconfig, | |
2648 | ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); | |
2649 | ||
2650 | #ifdef _KERNEL | |
2651 | myhostid = zone_get_hostid(NULL); | |
2652 | #else /* _KERNEL */ | |
2653 | /* | |
2654 | * We're emulating the system's hostid in userland, so | |
2655 | * we can't use zone_get_hostid(). | |
2656 | */ | |
2657 | (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); | |
2658 | #endif /* _KERNEL */ | |
2659 | if (hostid != 0 && myhostid != 0 && | |
2660 | hostid != myhostid) { | |
2661 | nvlist_free(nvconfig); | |
2662 | cmn_err(CE_WARN, "pool '%s' could not be " | |
2663 | "loaded as it was last accessed by another " | |
2664 | "system (host: %s hostid: 0x%lx). See: " | |
2665 | "http://zfsonlinux.org/msg/ZFS-8000-EY", | |
2666 | spa_name(spa), hostname, | |
2667 | (unsigned long)hostid); | |
2668 | return (SET_ERROR(EBADF)); | |
2669 | } | |
2670 | } | |
2671 | if (nvlist_lookup_nvlist(spa->spa_config, | |
2672 | ZPOOL_REWIND_POLICY, &policy) == 0) | |
2673 | VERIFY(nvlist_add_nvlist(nvconfig, | |
2674 | ZPOOL_REWIND_POLICY, policy) == 0); | |
2675 | ||
2676 | spa_config_set(spa, nvconfig); | |
2677 | spa_unload(spa); | |
2678 | spa_deactivate(spa); | |
2679 | spa_activate(spa, orig_mode); | |
2680 | ||
2681 | return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); | |
2682 | } | |
2683 | ||
2684 | if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) | |
2685 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2686 | error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); | |
2687 | if (error != 0) | |
2688 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2689 | ||
2690 | /* | |
2691 | * Load the bit that tells us to use the new accounting function | |
2692 | * (raid-z deflation). If we have an older pool, this will not | |
2693 | * be present. | |
2694 | */ | |
2695 | error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); | |
2696 | if (error != 0 && error != ENOENT) | |
2697 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2698 | ||
2699 | error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, | |
2700 | &spa->spa_creation_version); | |
2701 | if (error != 0 && error != ENOENT) | |
2702 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2703 | ||
2704 | /* | |
2705 | * Load the persistent error log. If we have an older pool, this will | |
2706 | * not be present. | |
2707 | */ | |
2708 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); | |
2709 | if (error != 0 && error != ENOENT) | |
2710 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2711 | ||
2712 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, | |
2713 | &spa->spa_errlog_scrub); | |
2714 | if (error != 0 && error != ENOENT) | |
2715 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2716 | ||
2717 | /* | |
2718 | * Load the history object. If we have an older pool, this | |
2719 | * will not be present. | |
2720 | */ | |
2721 | error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); | |
2722 | if (error != 0 && error != ENOENT) | |
2723 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2724 | ||
2725 | /* | |
2726 | * Load the per-vdev ZAP map. If we have an older pool, this will not | |
2727 | * be present; in this case, defer its creation to a later time to | |
2728 | * avoid dirtying the MOS this early / out of sync context. See | |
2729 | * spa_sync_config_object. | |
2730 | */ | |
2731 | ||
2732 | /* The sentinel is only available in the MOS config. */ | |
2733 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) | |
2734 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2735 | ||
2736 | error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, | |
2737 | &spa->spa_all_vdev_zaps); | |
2738 | ||
2739 | if (error != ENOENT && error != 0) { | |
2740 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2741 | } else if (error == 0 && !nvlist_exists(mos_config, | |
2742 | ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { | |
2743 | /* | |
2744 | * An older version of ZFS overwrote the sentinel value, so | |
2745 | * we have orphaned per-vdev ZAPs in the MOS. Defer their | |
2746 | * destruction to later; see spa_sync_config_object. | |
2747 | */ | |
2748 | spa->spa_avz_action = AVZ_ACTION_DESTROY; | |
2749 | /* | |
2750 | * We're assuming that no vdevs have had their ZAPs created | |
2751 | * before this. Better be sure of it. | |
2752 | */ | |
2753 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
2754 | } | |
2755 | nvlist_free(mos_config); | |
2756 | ||
2757 | /* | |
2758 | * If we're assembling the pool from the split-off vdevs of | |
2759 | * an existing pool, we don't want to attach the spares & cache | |
2760 | * devices. | |
2761 | */ | |
2762 | ||
2763 | /* | |
2764 | * Load any hot spares for this pool. | |
2765 | */ | |
2766 | error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); | |
2767 | if (error != 0 && error != ENOENT) | |
2768 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2769 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
2770 | ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); | |
2771 | if (load_nvlist(spa, spa->spa_spares.sav_object, | |
2772 | &spa->spa_spares.sav_config) != 0) | |
2773 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2774 | ||
2775 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
2776 | spa_load_spares(spa); | |
2777 | spa_config_exit(spa, SCL_ALL, FTAG); | |
2778 | } else if (error == 0) { | |
2779 | spa->spa_spares.sav_sync = B_TRUE; | |
2780 | } | |
2781 | ||
2782 | /* | |
2783 | * Load any level 2 ARC devices for this pool. | |
2784 | */ | |
2785 | error = spa_dir_prop(spa, DMU_POOL_L2CACHE, | |
2786 | &spa->spa_l2cache.sav_object); | |
2787 | if (error != 0 && error != ENOENT) | |
2788 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2789 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
2790 | ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); | |
2791 | if (load_nvlist(spa, spa->spa_l2cache.sav_object, | |
2792 | &spa->spa_l2cache.sav_config) != 0) | |
2793 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2794 | ||
2795 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
2796 | spa_load_l2cache(spa); | |
2797 | spa_config_exit(spa, SCL_ALL, FTAG); | |
2798 | } else if (error == 0) { | |
2799 | spa->spa_l2cache.sav_sync = B_TRUE; | |
2800 | } | |
2801 | ||
2802 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
2803 | ||
2804 | error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); | |
2805 | if (error && error != ENOENT) | |
2806 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2807 | ||
2808 | if (error == 0) { | |
2809 | uint64_t autoreplace = 0; | |
2810 | ||
2811 | spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); | |
2812 | spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); | |
2813 | spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); | |
2814 | spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); | |
2815 | spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); | |
2816 | spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, | |
2817 | &spa->spa_dedup_ditto); | |
2818 | ||
2819 | spa->spa_autoreplace = (autoreplace != 0); | |
2820 | } | |
2821 | ||
2822 | /* | |
2823 | * If the 'autoreplace' property is set, then post a resource notifying | |
2824 | * the ZFS DE that it should not issue any faults for unopenable | |
2825 | * devices. We also iterate over the vdevs, and post a sysevent for any | |
2826 | * unopenable vdevs so that the normal autoreplace handler can take | |
2827 | * over. | |
2828 | */ | |
2829 | if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { | |
2830 | spa_check_removed(spa->spa_root_vdev); | |
2831 | /* | |
2832 | * For the import case, this is done in spa_import(), because | |
2833 | * at this point we're using the spare definitions from | |
2834 | * the MOS config, not necessarily from the userland config. | |
2835 | */ | |
2836 | if (state != SPA_LOAD_IMPORT) { | |
2837 | spa_aux_check_removed(&spa->spa_spares); | |
2838 | spa_aux_check_removed(&spa->spa_l2cache); | |
2839 | } | |
2840 | } | |
2841 | ||
2842 | /* | |
2843 | * Load the vdev state for all toplevel vdevs. | |
2844 | */ | |
2845 | vdev_load(rvd); | |
2846 | ||
2847 | /* | |
2848 | * Propagate the leaf DTLs we just loaded all the way up the tree. | |
2849 | */ | |
2850 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
2851 | vdev_dtl_reassess(rvd, 0, 0, B_FALSE); | |
2852 | spa_config_exit(spa, SCL_ALL, FTAG); | |
2853 | ||
2854 | /* | |
2855 | * Load the DDTs (dedup tables). | |
2856 | */ | |
2857 | error = ddt_load(spa); | |
2858 | if (error != 0) | |
2859 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2860 | ||
2861 | spa_update_dspace(spa); | |
2862 | ||
2863 | /* | |
2864 | * Validate the config, using the MOS config to fill in any | |
2865 | * information which might be missing. If we fail to validate | |
2866 | * the config then declare the pool unfit for use. If we're | |
2867 | * assembling a pool from a split, the log is not transferred | |
2868 | * over. | |
2869 | */ | |
2870 | if (type != SPA_IMPORT_ASSEMBLE) { | |
2871 | nvlist_t *nvconfig; | |
2872 | ||
2873 | if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) | |
2874 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2875 | ||
2876 | if (!spa_config_valid(spa, nvconfig)) { | |
2877 | nvlist_free(nvconfig); | |
2878 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, | |
2879 | ENXIO)); | |
2880 | } | |
2881 | nvlist_free(nvconfig); | |
2882 | ||
2883 | /* | |
2884 | * Now that we've validated the config, check the state of the | |
2885 | * root vdev. If it can't be opened, it indicates one or | |
2886 | * more toplevel vdevs are faulted. | |
2887 | */ | |
2888 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) | |
2889 | return (SET_ERROR(ENXIO)); | |
2890 | ||
2891 | if (spa_writeable(spa) && spa_check_logs(spa)) { | |
2892 | *ereport = FM_EREPORT_ZFS_LOG_REPLAY; | |
2893 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); | |
2894 | } | |
2895 | } | |
2896 | ||
2897 | if (missing_feat_write) { | |
2898 | ASSERT(state == SPA_LOAD_TRYIMPORT); | |
2899 | ||
2900 | /* | |
2901 | * At this point, we know that we can open the pool in | |
2902 | * read-only mode but not read-write mode. We now have enough | |
2903 | * information and can return to userland. | |
2904 | */ | |
2905 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); | |
2906 | } | |
2907 | ||
2908 | /* | |
2909 | * We've successfully opened the pool, verify that we're ready | |
2910 | * to start pushing transactions. | |
2911 | */ | |
2912 | if (state != SPA_LOAD_TRYIMPORT) { | |
2913 | if ((error = spa_load_verify(spa))) | |
2914 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
2915 | error)); | |
2916 | } | |
2917 | ||
2918 | if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || | |
2919 | spa->spa_load_max_txg == UINT64_MAX)) { | |
2920 | dmu_tx_t *tx; | |
2921 | int need_update = B_FALSE; | |
2922 | dsl_pool_t *dp = spa_get_dsl(spa); | |
2923 | int c; | |
2924 | ||
2925 | ASSERT(state != SPA_LOAD_TRYIMPORT); | |
2926 | ||
2927 | /* | |
2928 | * Claim log blocks that haven't been committed yet. | |
2929 | * This must all happen in a single txg. | |
2930 | * Note: spa_claim_max_txg is updated by spa_claim_notify(), | |
2931 | * invoked from zil_claim_log_block()'s i/o done callback. | |
2932 | * Price of rollback is that we abandon the log. | |
2933 | */ | |
2934 | spa->spa_claiming = B_TRUE; | |
2935 | ||
2936 | tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); | |
2937 | (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, | |
2938 | zil_claim, tx, DS_FIND_CHILDREN); | |
2939 | dmu_tx_commit(tx); | |
2940 | ||
2941 | spa->spa_claiming = B_FALSE; | |
2942 | ||
2943 | spa_set_log_state(spa, SPA_LOG_GOOD); | |
2944 | spa->spa_sync_on = B_TRUE; | |
2945 | txg_sync_start(spa->spa_dsl_pool); | |
2946 | ||
2947 | /* | |
2948 | * Wait for all claims to sync. We sync up to the highest | |
2949 | * claimed log block birth time so that claimed log blocks | |
2950 | * don't appear to be from the future. spa_claim_max_txg | |
2951 | * will have been set for us by either zil_check_log_chain() | |
2952 | * (invoked from spa_check_logs()) or zil_claim() above. | |
2953 | */ | |
2954 | txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); | |
2955 | ||
2956 | /* | |
2957 | * If the config cache is stale, or we have uninitialized | |
2958 | * metaslabs (see spa_vdev_add()), then update the config. | |
2959 | * | |
2960 | * If this is a verbatim import, trust the current | |
2961 | * in-core spa_config and update the disk labels. | |
2962 | */ | |
2963 | if (config_cache_txg != spa->spa_config_txg || | |
2964 | state == SPA_LOAD_IMPORT || | |
2965 | state == SPA_LOAD_RECOVER || | |
2966 | (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) | |
2967 | need_update = B_TRUE; | |
2968 | ||
2969 | for (c = 0; c < rvd->vdev_children; c++) | |
2970 | if (rvd->vdev_child[c]->vdev_ms_array == 0) | |
2971 | need_update = B_TRUE; | |
2972 | ||
2973 | /* | |
2974 | * Update the config cache asychronously in case we're the | |
2975 | * root pool, in which case the config cache isn't writable yet. | |
2976 | */ | |
2977 | if (need_update) | |
2978 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
2979 | ||
2980 | /* | |
2981 | * Check all DTLs to see if anything needs resilvering. | |
2982 | */ | |
2983 | if (!dsl_scan_resilvering(spa->spa_dsl_pool) && | |
2984 | vdev_resilver_needed(rvd, NULL, NULL)) | |
2985 | spa_async_request(spa, SPA_ASYNC_RESILVER); | |
2986 | ||
2987 | /* | |
2988 | * Log the fact that we booted up (so that we can detect if | |
2989 | * we rebooted in the middle of an operation). | |
2990 | */ | |
2991 | spa_history_log_version(spa, "open"); | |
2992 | ||
2993 | /* | |
2994 | * Delete any inconsistent datasets. | |
2995 | */ | |
2996 | (void) dmu_objset_find(spa_name(spa), | |
2997 | dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); | |
2998 | ||
2999 | /* | |
3000 | * Clean up any stale temporary dataset userrefs. | |
3001 | */ | |
3002 | dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); | |
3003 | } | |
3004 | ||
3005 | return (0); | |
3006 | } | |
3007 | ||
3008 | static int | |
3009 | spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) | |
3010 | { | |
3011 | int mode = spa->spa_mode; | |
3012 | ||
3013 | spa_unload(spa); | |
3014 | spa_deactivate(spa); | |
3015 | ||
3016 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; | |
3017 | ||
3018 | spa_activate(spa, mode); | |
3019 | spa_async_suspend(spa); | |
3020 | ||
3021 | return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); | |
3022 | } | |
3023 | ||
3024 | /* | |
3025 | * If spa_load() fails this function will try loading prior txg's. If | |
3026 | * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool | |
3027 | * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this | |
3028 | * function will not rewind the pool and will return the same error as | |
3029 | * spa_load(). | |
3030 | */ | |
3031 | static int | |
3032 | spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, | |
3033 | uint64_t max_request, int rewind_flags) | |
3034 | { | |
3035 | nvlist_t *loadinfo = NULL; | |
3036 | nvlist_t *config = NULL; | |
3037 | int load_error, rewind_error; | |
3038 | uint64_t safe_rewind_txg; | |
3039 | uint64_t min_txg; | |
3040 | ||
3041 | if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { | |
3042 | spa->spa_load_max_txg = spa->spa_load_txg; | |
3043 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
3044 | } else { | |
3045 | spa->spa_load_max_txg = max_request; | |
3046 | if (max_request != UINT64_MAX) | |
3047 | spa->spa_extreme_rewind = B_TRUE; | |
3048 | } | |
3049 | ||
3050 | load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, | |
3051 | mosconfig); | |
3052 | if (load_error == 0) | |
3053 | return (0); | |
3054 | ||
3055 | if (spa->spa_root_vdev != NULL) | |
3056 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
3057 | ||
3058 | spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; | |
3059 | spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; | |
3060 | ||
3061 | if (rewind_flags & ZPOOL_NEVER_REWIND) { | |
3062 | nvlist_free(config); | |
3063 | return (load_error); | |
3064 | } | |
3065 | ||
3066 | if (state == SPA_LOAD_RECOVER) { | |
3067 | /* Price of rolling back is discarding txgs, including log */ | |
3068 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
3069 | } else { | |
3070 | /* | |
3071 | * If we aren't rolling back save the load info from our first | |
3072 | * import attempt so that we can restore it after attempting | |
3073 | * to rewind. | |
3074 | */ | |
3075 | loadinfo = spa->spa_load_info; | |
3076 | spa->spa_load_info = fnvlist_alloc(); | |
3077 | } | |
3078 | ||
3079 | spa->spa_load_max_txg = spa->spa_last_ubsync_txg; | |
3080 | safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; | |
3081 | min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? | |
3082 | TXG_INITIAL : safe_rewind_txg; | |
3083 | ||
3084 | /* | |
3085 | * Continue as long as we're finding errors, we're still within | |
3086 | * the acceptable rewind range, and we're still finding uberblocks | |
3087 | */ | |
3088 | while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && | |
3089 | spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { | |
3090 | if (spa->spa_load_max_txg < safe_rewind_txg) | |
3091 | spa->spa_extreme_rewind = B_TRUE; | |
3092 | rewind_error = spa_load_retry(spa, state, mosconfig); | |
3093 | } | |
3094 | ||
3095 | spa->spa_extreme_rewind = B_FALSE; | |
3096 | spa->spa_load_max_txg = UINT64_MAX; | |
3097 | ||
3098 | if (config && (rewind_error || state != SPA_LOAD_RECOVER)) | |
3099 | spa_config_set(spa, config); | |
3100 | else | |
3101 | nvlist_free(config); | |
3102 | ||
3103 | if (state == SPA_LOAD_RECOVER) { | |
3104 | ASSERT3P(loadinfo, ==, NULL); | |
3105 | return (rewind_error); | |
3106 | } else { | |
3107 | /* Store the rewind info as part of the initial load info */ | |
3108 | fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, | |
3109 | spa->spa_load_info); | |
3110 | ||
3111 | /* Restore the initial load info */ | |
3112 | fnvlist_free(spa->spa_load_info); | |
3113 | spa->spa_load_info = loadinfo; | |
3114 | ||
3115 | return (load_error); | |
3116 | } | |
3117 | } | |
3118 | ||
3119 | /* | |
3120 | * Pool Open/Import | |
3121 | * | |
3122 | * The import case is identical to an open except that the configuration is sent | |
3123 | * down from userland, instead of grabbed from the configuration cache. For the | |
3124 | * case of an open, the pool configuration will exist in the | |
3125 | * POOL_STATE_UNINITIALIZED state. | |
3126 | * | |
3127 | * The stats information (gen/count/ustats) is used to gather vdev statistics at | |
3128 | * the same time open the pool, without having to keep around the spa_t in some | |
3129 | * ambiguous state. | |
3130 | */ | |
3131 | static int | |
3132 | spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, | |
3133 | nvlist_t **config) | |
3134 | { | |
3135 | spa_t *spa; | |
3136 | spa_load_state_t state = SPA_LOAD_OPEN; | |
3137 | int error; | |
3138 | int locked = B_FALSE; | |
3139 | int firstopen = B_FALSE; | |
3140 | ||
3141 | *spapp = NULL; | |
3142 | ||
3143 | /* | |
3144 | * As disgusting as this is, we need to support recursive calls to this | |
3145 | * function because dsl_dir_open() is called during spa_load(), and ends | |
3146 | * up calling spa_open() again. The real fix is to figure out how to | |
3147 | * avoid dsl_dir_open() calling this in the first place. | |
3148 | */ | |
3149 | if (mutex_owner(&spa_namespace_lock) != curthread) { | |
3150 | mutex_enter(&spa_namespace_lock); | |
3151 | locked = B_TRUE; | |
3152 | } | |
3153 | ||
3154 | if ((spa = spa_lookup(pool)) == NULL) { | |
3155 | if (locked) | |
3156 | mutex_exit(&spa_namespace_lock); | |
3157 | return (SET_ERROR(ENOENT)); | |
3158 | } | |
3159 | ||
3160 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) { | |
3161 | zpool_rewind_policy_t policy; | |
3162 | ||
3163 | firstopen = B_TRUE; | |
3164 | ||
3165 | zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, | |
3166 | &policy); | |
3167 | if (policy.zrp_request & ZPOOL_DO_REWIND) | |
3168 | state = SPA_LOAD_RECOVER; | |
3169 | ||
3170 | spa_activate(spa, spa_mode_global); | |
3171 | ||
3172 | if (state != SPA_LOAD_RECOVER) | |
3173 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
3174 | ||
3175 | error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, | |
3176 | policy.zrp_request); | |
3177 | ||
3178 | if (error == EBADF) { | |
3179 | /* | |
3180 | * If vdev_validate() returns failure (indicated by | |
3181 | * EBADF), it indicates that one of the vdevs indicates | |
3182 | * that the pool has been exported or destroyed. If | |
3183 | * this is the case, the config cache is out of sync and | |
3184 | * we should remove the pool from the namespace. | |
3185 | */ | |
3186 | spa_unload(spa); | |
3187 | spa_deactivate(spa); | |
3188 | spa_config_sync(spa, B_TRUE, B_TRUE); | |
3189 | spa_remove(spa); | |
3190 | if (locked) | |
3191 | mutex_exit(&spa_namespace_lock); | |
3192 | return (SET_ERROR(ENOENT)); | |
3193 | } | |
3194 | ||
3195 | if (error) { | |
3196 | /* | |
3197 | * We can't open the pool, but we still have useful | |
3198 | * information: the state of each vdev after the | |
3199 | * attempted vdev_open(). Return this to the user. | |
3200 | */ | |
3201 | if (config != NULL && spa->spa_config) { | |
3202 | VERIFY(nvlist_dup(spa->spa_config, config, | |
3203 | KM_SLEEP) == 0); | |
3204 | VERIFY(nvlist_add_nvlist(*config, | |
3205 | ZPOOL_CONFIG_LOAD_INFO, | |
3206 | spa->spa_load_info) == 0); | |
3207 | } | |
3208 | spa_unload(spa); | |
3209 | spa_deactivate(spa); | |
3210 | spa->spa_last_open_failed = error; | |
3211 | if (locked) | |
3212 | mutex_exit(&spa_namespace_lock); | |
3213 | *spapp = NULL; | |
3214 | return (error); | |
3215 | } | |
3216 | } | |
3217 | ||
3218 | spa_open_ref(spa, tag); | |
3219 | ||
3220 | if (config != NULL) | |
3221 | *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
3222 | ||
3223 | /* | |
3224 | * If we've recovered the pool, pass back any information we | |
3225 | * gathered while doing the load. | |
3226 | */ | |
3227 | if (state == SPA_LOAD_RECOVER) { | |
3228 | VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, | |
3229 | spa->spa_load_info) == 0); | |
3230 | } | |
3231 | ||
3232 | if (locked) { | |
3233 | spa->spa_last_open_failed = 0; | |
3234 | spa->spa_last_ubsync_txg = 0; | |
3235 | spa->spa_load_txg = 0; | |
3236 | mutex_exit(&spa_namespace_lock); | |
3237 | } | |
3238 | ||
3239 | if (firstopen) | |
3240 | zvol_create_minors(spa, spa_name(spa), B_TRUE); | |
3241 | ||
3242 | *spapp = spa; | |
3243 | ||
3244 | return (0); | |
3245 | } | |
3246 | ||
3247 | int | |
3248 | spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, | |
3249 | nvlist_t **config) | |
3250 | { | |
3251 | return (spa_open_common(name, spapp, tag, policy, config)); | |
3252 | } | |
3253 | ||
3254 | int | |
3255 | spa_open(const char *name, spa_t **spapp, void *tag) | |
3256 | { | |
3257 | return (spa_open_common(name, spapp, tag, NULL, NULL)); | |
3258 | } | |
3259 | ||
3260 | /* | |
3261 | * Lookup the given spa_t, incrementing the inject count in the process, | |
3262 | * preventing it from being exported or destroyed. | |
3263 | */ | |
3264 | spa_t * | |
3265 | spa_inject_addref(char *name) | |
3266 | { | |
3267 | spa_t *spa; | |
3268 | ||
3269 | mutex_enter(&spa_namespace_lock); | |
3270 | if ((spa = spa_lookup(name)) == NULL) { | |
3271 | mutex_exit(&spa_namespace_lock); | |
3272 | return (NULL); | |
3273 | } | |
3274 | spa->spa_inject_ref++; | |
3275 | mutex_exit(&spa_namespace_lock); | |
3276 | ||
3277 | return (spa); | |
3278 | } | |
3279 | ||
3280 | void | |
3281 | spa_inject_delref(spa_t *spa) | |
3282 | { | |
3283 | mutex_enter(&spa_namespace_lock); | |
3284 | spa->spa_inject_ref--; | |
3285 | mutex_exit(&spa_namespace_lock); | |
3286 | } | |
3287 | ||
3288 | /* | |
3289 | * Add spares device information to the nvlist. | |
3290 | */ | |
3291 | static void | |
3292 | spa_add_spares(spa_t *spa, nvlist_t *config) | |
3293 | { | |
3294 | nvlist_t **spares; | |
3295 | uint_t i, nspares; | |
3296 | nvlist_t *nvroot; | |
3297 | uint64_t guid; | |
3298 | vdev_stat_t *vs; | |
3299 | uint_t vsc; | |
3300 | uint64_t pool; | |
3301 | ||
3302 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
3303 | ||
3304 | if (spa->spa_spares.sav_count == 0) | |
3305 | return; | |
3306 | ||
3307 | VERIFY(nvlist_lookup_nvlist(config, | |
3308 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
3309 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
3310 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
3311 | if (nspares != 0) { | |
3312 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
3313 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
3314 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
3315 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
3316 | ||
3317 | /* | |
3318 | * Go through and find any spares which have since been | |
3319 | * repurposed as an active spare. If this is the case, update | |
3320 | * their status appropriately. | |
3321 | */ | |
3322 | for (i = 0; i < nspares; i++) { | |
3323 | VERIFY(nvlist_lookup_uint64(spares[i], | |
3324 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
3325 | if (spa_spare_exists(guid, &pool, NULL) && | |
3326 | pool != 0ULL) { | |
3327 | VERIFY(nvlist_lookup_uint64_array( | |
3328 | spares[i], ZPOOL_CONFIG_VDEV_STATS, | |
3329 | (uint64_t **)&vs, &vsc) == 0); | |
3330 | vs->vs_state = VDEV_STATE_CANT_OPEN; | |
3331 | vs->vs_aux = VDEV_AUX_SPARED; | |
3332 | } | |
3333 | } | |
3334 | } | |
3335 | } | |
3336 | ||
3337 | /* | |
3338 | * Add l2cache device information to the nvlist, including vdev stats. | |
3339 | */ | |
3340 | static void | |
3341 | spa_add_l2cache(spa_t *spa, nvlist_t *config) | |
3342 | { | |
3343 | nvlist_t **l2cache; | |
3344 | uint_t i, j, nl2cache; | |
3345 | nvlist_t *nvroot; | |
3346 | uint64_t guid; | |
3347 | vdev_t *vd; | |
3348 | vdev_stat_t *vs; | |
3349 | uint_t vsc; | |
3350 | ||
3351 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
3352 | ||
3353 | if (spa->spa_l2cache.sav_count == 0) | |
3354 | return; | |
3355 | ||
3356 | VERIFY(nvlist_lookup_nvlist(config, | |
3357 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
3358 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
3359 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
3360 | if (nl2cache != 0) { | |
3361 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
3362 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
3363 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
3364 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
3365 | ||
3366 | /* | |
3367 | * Update level 2 cache device stats. | |
3368 | */ | |
3369 | ||
3370 | for (i = 0; i < nl2cache; i++) { | |
3371 | VERIFY(nvlist_lookup_uint64(l2cache[i], | |
3372 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
3373 | ||
3374 | vd = NULL; | |
3375 | for (j = 0; j < spa->spa_l2cache.sav_count; j++) { | |
3376 | if (guid == | |
3377 | spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { | |
3378 | vd = spa->spa_l2cache.sav_vdevs[j]; | |
3379 | break; | |
3380 | } | |
3381 | } | |
3382 | ASSERT(vd != NULL); | |
3383 | ||
3384 | VERIFY(nvlist_lookup_uint64_array(l2cache[i], | |
3385 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) | |
3386 | == 0); | |
3387 | vdev_get_stats(vd, vs); | |
3388 | vdev_config_generate_stats(vd, l2cache[i]); | |
3389 | ||
3390 | } | |
3391 | } | |
3392 | } | |
3393 | ||
3394 | static void | |
3395 | spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features) | |
3396 | { | |
3397 | zap_cursor_t zc; | |
3398 | zap_attribute_t za; | |
3399 | ||
3400 | if (spa->spa_feat_for_read_obj != 0) { | |
3401 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
3402 | spa->spa_feat_for_read_obj); | |
3403 | zap_cursor_retrieve(&zc, &za) == 0; | |
3404 | zap_cursor_advance(&zc)) { | |
3405 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
3406 | za.za_num_integers == 1); | |
3407 | VERIFY0(nvlist_add_uint64(features, za.za_name, | |
3408 | za.za_first_integer)); | |
3409 | } | |
3410 | zap_cursor_fini(&zc); | |
3411 | } | |
3412 | ||
3413 | if (spa->spa_feat_for_write_obj != 0) { | |
3414 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
3415 | spa->spa_feat_for_write_obj); | |
3416 | zap_cursor_retrieve(&zc, &za) == 0; | |
3417 | zap_cursor_advance(&zc)) { | |
3418 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
3419 | za.za_num_integers == 1); | |
3420 | VERIFY0(nvlist_add_uint64(features, za.za_name, | |
3421 | za.za_first_integer)); | |
3422 | } | |
3423 | zap_cursor_fini(&zc); | |
3424 | } | |
3425 | } | |
3426 | ||
3427 | static void | |
3428 | spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features) | |
3429 | { | |
3430 | int i; | |
3431 | ||
3432 | for (i = 0; i < SPA_FEATURES; i++) { | |
3433 | zfeature_info_t feature = spa_feature_table[i]; | |
3434 | uint64_t refcount; | |
3435 | ||
3436 | if (feature_get_refcount(spa, &feature, &refcount) != 0) | |
3437 | continue; | |
3438 | ||
3439 | VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount)); | |
3440 | } | |
3441 | } | |
3442 | ||
3443 | /* | |
3444 | * Store a list of pool features and their reference counts in the | |
3445 | * config. | |
3446 | * | |
3447 | * The first time this is called on a spa, allocate a new nvlist, fetch | |
3448 | * the pool features and reference counts from disk, then save the list | |
3449 | * in the spa. In subsequent calls on the same spa use the saved nvlist | |
3450 | * and refresh its values from the cached reference counts. This | |
3451 | * ensures we don't block here on I/O on a suspended pool so 'zpool | |
3452 | * clear' can resume the pool. | |
3453 | */ | |
3454 | static void | |
3455 | spa_add_feature_stats(spa_t *spa, nvlist_t *config) | |
3456 | { | |
3457 | nvlist_t *features; | |
3458 | ||
3459 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
3460 | ||
3461 | mutex_enter(&spa->spa_feat_stats_lock); | |
3462 | features = spa->spa_feat_stats; | |
3463 | ||
3464 | if (features != NULL) { | |
3465 | spa_feature_stats_from_cache(spa, features); | |
3466 | } else { | |
3467 | VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP)); | |
3468 | spa->spa_feat_stats = features; | |
3469 | spa_feature_stats_from_disk(spa, features); | |
3470 | } | |
3471 | ||
3472 | VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, | |
3473 | features)); | |
3474 | ||
3475 | mutex_exit(&spa->spa_feat_stats_lock); | |
3476 | } | |
3477 | ||
3478 | int | |
3479 | spa_get_stats(const char *name, nvlist_t **config, | |
3480 | char *altroot, size_t buflen) | |
3481 | { | |
3482 | int error; | |
3483 | spa_t *spa; | |
3484 | ||
3485 | *config = NULL; | |
3486 | error = spa_open_common(name, &spa, FTAG, NULL, config); | |
3487 | ||
3488 | if (spa != NULL) { | |
3489 | /* | |
3490 | * This still leaves a window of inconsistency where the spares | |
3491 | * or l2cache devices could change and the config would be | |
3492 | * self-inconsistent. | |
3493 | */ | |
3494 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
3495 | ||
3496 | if (*config != NULL) { | |
3497 | uint64_t loadtimes[2]; | |
3498 | ||
3499 | loadtimes[0] = spa->spa_loaded_ts.tv_sec; | |
3500 | loadtimes[1] = spa->spa_loaded_ts.tv_nsec; | |
3501 | VERIFY(nvlist_add_uint64_array(*config, | |
3502 | ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); | |
3503 | ||
3504 | VERIFY(nvlist_add_uint64(*config, | |
3505 | ZPOOL_CONFIG_ERRCOUNT, | |
3506 | spa_get_errlog_size(spa)) == 0); | |
3507 | ||
3508 | if (spa_suspended(spa)) | |
3509 | VERIFY(nvlist_add_uint64(*config, | |
3510 | ZPOOL_CONFIG_SUSPENDED, | |
3511 | spa->spa_failmode) == 0); | |
3512 | ||
3513 | spa_add_spares(spa, *config); | |
3514 | spa_add_l2cache(spa, *config); | |
3515 | spa_add_feature_stats(spa, *config); | |
3516 | } | |
3517 | } | |
3518 | ||
3519 | /* | |
3520 | * We want to get the alternate root even for faulted pools, so we cheat | |
3521 | * and call spa_lookup() directly. | |
3522 | */ | |
3523 | if (altroot) { | |
3524 | if (spa == NULL) { | |
3525 | mutex_enter(&spa_namespace_lock); | |
3526 | spa = spa_lookup(name); | |
3527 | if (spa) | |
3528 | spa_altroot(spa, altroot, buflen); | |
3529 | else | |
3530 | altroot[0] = '\0'; | |
3531 | spa = NULL; | |
3532 | mutex_exit(&spa_namespace_lock); | |
3533 | } else { | |
3534 | spa_altroot(spa, altroot, buflen); | |
3535 | } | |
3536 | } | |
3537 | ||
3538 | if (spa != NULL) { | |
3539 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
3540 | spa_close(spa, FTAG); | |
3541 | } | |
3542 | ||
3543 | return (error); | |
3544 | } | |
3545 | ||
3546 | /* | |
3547 | * Validate that the auxiliary device array is well formed. We must have an | |
3548 | * array of nvlists, each which describes a valid leaf vdev. If this is an | |
3549 | * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be | |
3550 | * specified, as long as they are well-formed. | |
3551 | */ | |
3552 | static int | |
3553 | spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, | |
3554 | spa_aux_vdev_t *sav, const char *config, uint64_t version, | |
3555 | vdev_labeltype_t label) | |
3556 | { | |
3557 | nvlist_t **dev; | |
3558 | uint_t i, ndev; | |
3559 | vdev_t *vd; | |
3560 | int error; | |
3561 | ||
3562 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); | |
3563 | ||
3564 | /* | |
3565 | * It's acceptable to have no devs specified. | |
3566 | */ | |
3567 | if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) | |
3568 | return (0); | |
3569 | ||
3570 | if (ndev == 0) | |
3571 | return (SET_ERROR(EINVAL)); | |
3572 | ||
3573 | /* | |
3574 | * Make sure the pool is formatted with a version that supports this | |
3575 | * device type. | |
3576 | */ | |
3577 | if (spa_version(spa) < version) | |
3578 | return (SET_ERROR(ENOTSUP)); | |
3579 | ||
3580 | /* | |
3581 | * Set the pending device list so we correctly handle device in-use | |
3582 | * checking. | |
3583 | */ | |
3584 | sav->sav_pending = dev; | |
3585 | sav->sav_npending = ndev; | |
3586 | ||
3587 | for (i = 0; i < ndev; i++) { | |
3588 | if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, | |
3589 | mode)) != 0) | |
3590 | goto out; | |
3591 | ||
3592 | if (!vd->vdev_ops->vdev_op_leaf) { | |
3593 | vdev_free(vd); | |
3594 | error = SET_ERROR(EINVAL); | |
3595 | goto out; | |
3596 | } | |
3597 | ||
3598 | /* | |
3599 | * The L2ARC currently only supports disk devices in | |
3600 | * kernel context. For user-level testing, we allow it. | |
3601 | */ | |
3602 | #ifdef _KERNEL | |
3603 | if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && | |
3604 | strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { | |
3605 | error = SET_ERROR(ENOTBLK); | |
3606 | vdev_free(vd); | |
3607 | goto out; | |
3608 | } | |
3609 | #endif | |
3610 | vd->vdev_top = vd; | |
3611 | ||
3612 | if ((error = vdev_open(vd)) == 0 && | |
3613 | (error = vdev_label_init(vd, crtxg, label)) == 0) { | |
3614 | VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, | |
3615 | vd->vdev_guid) == 0); | |
3616 | } | |
3617 | ||
3618 | vdev_free(vd); | |
3619 | ||
3620 | if (error && | |
3621 | (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) | |
3622 | goto out; | |
3623 | else | |
3624 | error = 0; | |
3625 | } | |
3626 | ||
3627 | out: | |
3628 | sav->sav_pending = NULL; | |
3629 | sav->sav_npending = 0; | |
3630 | return (error); | |
3631 | } | |
3632 | ||
3633 | static int | |
3634 | spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) | |
3635 | { | |
3636 | int error; | |
3637 | ||
3638 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); | |
3639 | ||
3640 | if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
3641 | &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, | |
3642 | VDEV_LABEL_SPARE)) != 0) { | |
3643 | return (error); | |
3644 | } | |
3645 | ||
3646 | return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
3647 | &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, | |
3648 | VDEV_LABEL_L2CACHE)); | |
3649 | } | |
3650 | ||
3651 | static void | |
3652 | spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, | |
3653 | const char *config) | |
3654 | { | |
3655 | int i; | |
3656 | ||
3657 | if (sav->sav_config != NULL) { | |
3658 | nvlist_t **olddevs; | |
3659 | uint_t oldndevs; | |
3660 | nvlist_t **newdevs; | |
3661 | ||
3662 | /* | |
3663 | * Generate new dev list by concatentating with the | |
3664 | * current dev list. | |
3665 | */ | |
3666 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, | |
3667 | &olddevs, &oldndevs) == 0); | |
3668 | ||
3669 | newdevs = kmem_alloc(sizeof (void *) * | |
3670 | (ndevs + oldndevs), KM_SLEEP); | |
3671 | for (i = 0; i < oldndevs; i++) | |
3672 | VERIFY(nvlist_dup(olddevs[i], &newdevs[i], | |
3673 | KM_SLEEP) == 0); | |
3674 | for (i = 0; i < ndevs; i++) | |
3675 | VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], | |
3676 | KM_SLEEP) == 0); | |
3677 | ||
3678 | VERIFY(nvlist_remove(sav->sav_config, config, | |
3679 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
3680 | ||
3681 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
3682 | config, newdevs, ndevs + oldndevs) == 0); | |
3683 | for (i = 0; i < oldndevs + ndevs; i++) | |
3684 | nvlist_free(newdevs[i]); | |
3685 | kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); | |
3686 | } else { | |
3687 | /* | |
3688 | * Generate a new dev list. | |
3689 | */ | |
3690 | VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, | |
3691 | KM_SLEEP) == 0); | |
3692 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, | |
3693 | devs, ndevs) == 0); | |
3694 | } | |
3695 | } | |
3696 | ||
3697 | /* | |
3698 | * Stop and drop level 2 ARC devices | |
3699 | */ | |
3700 | void | |
3701 | spa_l2cache_drop(spa_t *spa) | |
3702 | { | |
3703 | vdev_t *vd; | |
3704 | int i; | |
3705 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
3706 | ||
3707 | for (i = 0; i < sav->sav_count; i++) { | |
3708 | uint64_t pool; | |
3709 | ||
3710 | vd = sav->sav_vdevs[i]; | |
3711 | ASSERT(vd != NULL); | |
3712 | ||
3713 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && | |
3714 | pool != 0ULL && l2arc_vdev_present(vd)) | |
3715 | l2arc_remove_vdev(vd); | |
3716 | } | |
3717 | } | |
3718 | ||
3719 | /* | |
3720 | * Pool Creation | |
3721 | */ | |
3722 | int | |
3723 | spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, | |
3724 | nvlist_t *zplprops) | |
3725 | { | |
3726 | spa_t *spa; | |
3727 | char *altroot = NULL; | |
3728 | vdev_t *rvd; | |
3729 | dsl_pool_t *dp; | |
3730 | dmu_tx_t *tx; | |
3731 | int error = 0; | |
3732 | uint64_t txg = TXG_INITIAL; | |
3733 | nvlist_t **spares, **l2cache; | |
3734 | uint_t nspares, nl2cache; | |
3735 | uint64_t version, obj; | |
3736 | boolean_t has_features; | |
3737 | nvpair_t *elem; | |
3738 | int c, i; | |
3739 | char *poolname; | |
3740 | nvlist_t *nvl; | |
3741 | ||
3742 | if (nvlist_lookup_string(props, "tname", &poolname) != 0) | |
3743 | poolname = (char *)pool; | |
3744 | ||
3745 | /* | |
3746 | * If this pool already exists, return failure. | |
3747 | */ | |
3748 | mutex_enter(&spa_namespace_lock); | |
3749 | if (spa_lookup(poolname) != NULL) { | |
3750 | mutex_exit(&spa_namespace_lock); | |
3751 | return (SET_ERROR(EEXIST)); | |
3752 | } | |
3753 | ||
3754 | /* | |
3755 | * Allocate a new spa_t structure. | |
3756 | */ | |
3757 | nvl = fnvlist_alloc(); | |
3758 | fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool); | |
3759 | (void) nvlist_lookup_string(props, | |
3760 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
3761 | spa = spa_add(poolname, nvl, altroot); | |
3762 | fnvlist_free(nvl); | |
3763 | spa_activate(spa, spa_mode_global); | |
3764 | ||
3765 | if (props && (error = spa_prop_validate(spa, props))) { | |
3766 | spa_deactivate(spa); | |
3767 | spa_remove(spa); | |
3768 | mutex_exit(&spa_namespace_lock); | |
3769 | return (error); | |
3770 | } | |
3771 | ||
3772 | /* | |
3773 | * Temporary pool names should never be written to disk. | |
3774 | */ | |
3775 | if (poolname != pool) | |
3776 | spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME; | |
3777 | ||
3778 | has_features = B_FALSE; | |
3779 | for (elem = nvlist_next_nvpair(props, NULL); | |
3780 | elem != NULL; elem = nvlist_next_nvpair(props, elem)) { | |
3781 | if (zpool_prop_feature(nvpair_name(elem))) | |
3782 | has_features = B_TRUE; | |
3783 | } | |
3784 | ||
3785 | if (has_features || nvlist_lookup_uint64(props, | |
3786 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { | |
3787 | version = SPA_VERSION; | |
3788 | } | |
3789 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
3790 | ||
3791 | spa->spa_first_txg = txg; | |
3792 | spa->spa_uberblock.ub_txg = txg - 1; | |
3793 | spa->spa_uberblock.ub_version = version; | |
3794 | spa->spa_ubsync = spa->spa_uberblock; | |
3795 | ||
3796 | /* | |
3797 | * Create "The Godfather" zio to hold all async IOs | |
3798 | */ | |
3799 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), | |
3800 | KM_SLEEP); | |
3801 | for (i = 0; i < max_ncpus; i++) { | |
3802 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, | |
3803 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
3804 | ZIO_FLAG_GODFATHER); | |
3805 | } | |
3806 | ||
3807 | /* | |
3808 | * Create the root vdev. | |
3809 | */ | |
3810 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
3811 | ||
3812 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); | |
3813 | ||
3814 | ASSERT(error != 0 || rvd != NULL); | |
3815 | ASSERT(error != 0 || spa->spa_root_vdev == rvd); | |
3816 | ||
3817 | if (error == 0 && !zfs_allocatable_devs(nvroot)) | |
3818 | error = SET_ERROR(EINVAL); | |
3819 | ||
3820 | if (error == 0 && | |
3821 | (error = vdev_create(rvd, txg, B_FALSE)) == 0 && | |
3822 | (error = spa_validate_aux(spa, nvroot, txg, | |
3823 | VDEV_ALLOC_ADD)) == 0) { | |
3824 | for (c = 0; c < rvd->vdev_children; c++) { | |
3825 | vdev_metaslab_set_size(rvd->vdev_child[c]); | |
3826 | vdev_expand(rvd->vdev_child[c], txg); | |
3827 | } | |
3828 | } | |
3829 | ||
3830 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3831 | ||
3832 | if (error != 0) { | |
3833 | spa_unload(spa); | |
3834 | spa_deactivate(spa); | |
3835 | spa_remove(spa); | |
3836 | mutex_exit(&spa_namespace_lock); | |
3837 | return (error); | |
3838 | } | |
3839 | ||
3840 | /* | |
3841 | * Get the list of spares, if specified. | |
3842 | */ | |
3843 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
3844 | &spares, &nspares) == 0) { | |
3845 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, | |
3846 | KM_SLEEP) == 0); | |
3847 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, | |
3848 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
3849 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
3850 | spa_load_spares(spa); | |
3851 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3852 | spa->spa_spares.sav_sync = B_TRUE; | |
3853 | } | |
3854 | ||
3855 | /* | |
3856 | * Get the list of level 2 cache devices, if specified. | |
3857 | */ | |
3858 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
3859 | &l2cache, &nl2cache) == 0) { | |
3860 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
3861 | NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
3862 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, | |
3863 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
3864 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
3865 | spa_load_l2cache(spa); | |
3866 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3867 | spa->spa_l2cache.sav_sync = B_TRUE; | |
3868 | } | |
3869 | ||
3870 | spa->spa_is_initializing = B_TRUE; | |
3871 | spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); | |
3872 | spa->spa_meta_objset = dp->dp_meta_objset; | |
3873 | spa->spa_is_initializing = B_FALSE; | |
3874 | ||
3875 | /* | |
3876 | * Create DDTs (dedup tables). | |
3877 | */ | |
3878 | ddt_create(spa); | |
3879 | ||
3880 | spa_update_dspace(spa); | |
3881 | ||
3882 | tx = dmu_tx_create_assigned(dp, txg); | |
3883 | ||
3884 | /* | |
3885 | * Create the pool config object. | |
3886 | */ | |
3887 | spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, | |
3888 | DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, | |
3889 | DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); | |
3890 | ||
3891 | if (zap_add(spa->spa_meta_objset, | |
3892 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, | |
3893 | sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { | |
3894 | cmn_err(CE_PANIC, "failed to add pool config"); | |
3895 | } | |
3896 | ||
3897 | if (spa_version(spa) >= SPA_VERSION_FEATURES) | |
3898 | spa_feature_create_zap_objects(spa, tx); | |
3899 | ||
3900 | if (zap_add(spa->spa_meta_objset, | |
3901 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, | |
3902 | sizeof (uint64_t), 1, &version, tx) != 0) { | |
3903 | cmn_err(CE_PANIC, "failed to add pool version"); | |
3904 | } | |
3905 | ||
3906 | /* Newly created pools with the right version are always deflated. */ | |
3907 | if (version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
3908 | spa->spa_deflate = TRUE; | |
3909 | if (zap_add(spa->spa_meta_objset, | |
3910 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
3911 | sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { | |
3912 | cmn_err(CE_PANIC, "failed to add deflate"); | |
3913 | } | |
3914 | } | |
3915 | ||
3916 | /* | |
3917 | * Create the deferred-free bpobj. Turn off compression | |
3918 | * because sync-to-convergence takes longer if the blocksize | |
3919 | * keeps changing. | |
3920 | */ | |
3921 | obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); | |
3922 | dmu_object_set_compress(spa->spa_meta_objset, obj, | |
3923 | ZIO_COMPRESS_OFF, tx); | |
3924 | if (zap_add(spa->spa_meta_objset, | |
3925 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, | |
3926 | sizeof (uint64_t), 1, &obj, tx) != 0) { | |
3927 | cmn_err(CE_PANIC, "failed to add bpobj"); | |
3928 | } | |
3929 | VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, | |
3930 | spa->spa_meta_objset, obj)); | |
3931 | ||
3932 | /* | |
3933 | * Create the pool's history object. | |
3934 | */ | |
3935 | if (version >= SPA_VERSION_ZPOOL_HISTORY) | |
3936 | spa_history_create_obj(spa, tx); | |
3937 | ||
3938 | /* | |
3939 | * Set pool properties. | |
3940 | */ | |
3941 | spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); | |
3942 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
3943 | spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); | |
3944 | spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); | |
3945 | ||
3946 | if (props != NULL) { | |
3947 | spa_configfile_set(spa, props, B_FALSE); | |
3948 | spa_sync_props(props, tx); | |
3949 | } | |
3950 | ||
3951 | dmu_tx_commit(tx); | |
3952 | ||
3953 | spa->spa_sync_on = B_TRUE; | |
3954 | txg_sync_start(spa->spa_dsl_pool); | |
3955 | ||
3956 | /* | |
3957 | * We explicitly wait for the first transaction to complete so that our | |
3958 | * bean counters are appropriately updated. | |
3959 | */ | |
3960 | txg_wait_synced(spa->spa_dsl_pool, txg); | |
3961 | ||
3962 | spa_config_sync(spa, B_FALSE, B_TRUE); | |
3963 | spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE); | |
3964 | ||
3965 | spa_history_log_version(spa, "create"); | |
3966 | ||
3967 | /* | |
3968 | * Don't count references from objsets that are already closed | |
3969 | * and are making their way through the eviction process. | |
3970 | */ | |
3971 | spa_evicting_os_wait(spa); | |
3972 | spa->spa_minref = refcount_count(&spa->spa_refcount); | |
3973 | ||
3974 | mutex_exit(&spa_namespace_lock); | |
3975 | ||
3976 | return (0); | |
3977 | } | |
3978 | ||
3979 | /* | |
3980 | * Import a non-root pool into the system. | |
3981 | */ | |
3982 | int | |
3983 | spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) | |
3984 | { | |
3985 | spa_t *spa; | |
3986 | char *altroot = NULL; | |
3987 | spa_load_state_t state = SPA_LOAD_IMPORT; | |
3988 | zpool_rewind_policy_t policy; | |
3989 | uint64_t mode = spa_mode_global; | |
3990 | uint64_t readonly = B_FALSE; | |
3991 | int error; | |
3992 | nvlist_t *nvroot; | |
3993 | nvlist_t **spares, **l2cache; | |
3994 | uint_t nspares, nl2cache; | |
3995 | ||
3996 | /* | |
3997 | * If a pool with this name exists, return failure. | |
3998 | */ | |
3999 | mutex_enter(&spa_namespace_lock); | |
4000 | if (spa_lookup(pool) != NULL) { | |
4001 | mutex_exit(&spa_namespace_lock); | |
4002 | return (SET_ERROR(EEXIST)); | |
4003 | } | |
4004 | ||
4005 | /* | |
4006 | * Create and initialize the spa structure. | |
4007 | */ | |
4008 | (void) nvlist_lookup_string(props, | |
4009 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
4010 | (void) nvlist_lookup_uint64(props, | |
4011 | zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); | |
4012 | if (readonly) | |
4013 | mode = FREAD; | |
4014 | spa = spa_add(pool, config, altroot); | |
4015 | spa->spa_import_flags = flags; | |
4016 | ||
4017 | /* | |
4018 | * Verbatim import - Take a pool and insert it into the namespace | |
4019 | * as if it had been loaded at boot. | |
4020 | */ | |
4021 | if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { | |
4022 | if (props != NULL) | |
4023 | spa_configfile_set(spa, props, B_FALSE); | |
4024 | ||
4025 | spa_config_sync(spa, B_FALSE, B_TRUE); | |
4026 | spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT); | |
4027 | ||
4028 | mutex_exit(&spa_namespace_lock); | |
4029 | return (0); | |
4030 | } | |
4031 | ||
4032 | spa_activate(spa, mode); | |
4033 | ||
4034 | /* | |
4035 | * Don't start async tasks until we know everything is healthy. | |
4036 | */ | |
4037 | spa_async_suspend(spa); | |
4038 | ||
4039 | zpool_get_rewind_policy(config, &policy); | |
4040 | if (policy.zrp_request & ZPOOL_DO_REWIND) | |
4041 | state = SPA_LOAD_RECOVER; | |
4042 | ||
4043 | /* | |
4044 | * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig | |
4045 | * because the user-supplied config is actually the one to trust when | |
4046 | * doing an import. | |
4047 | */ | |
4048 | if (state != SPA_LOAD_RECOVER) | |
4049 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
4050 | ||
4051 | error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, | |
4052 | policy.zrp_request); | |
4053 | ||
4054 | /* | |
4055 | * Propagate anything learned while loading the pool and pass it | |
4056 | * back to caller (i.e. rewind info, missing devices, etc). | |
4057 | */ | |
4058 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, | |
4059 | spa->spa_load_info) == 0); | |
4060 | ||
4061 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4062 | /* | |
4063 | * Toss any existing sparelist, as it doesn't have any validity | |
4064 | * anymore, and conflicts with spa_has_spare(). | |
4065 | */ | |
4066 | if (spa->spa_spares.sav_config) { | |
4067 | nvlist_free(spa->spa_spares.sav_config); | |
4068 | spa->spa_spares.sav_config = NULL; | |
4069 | spa_load_spares(spa); | |
4070 | } | |
4071 | if (spa->spa_l2cache.sav_config) { | |
4072 | nvlist_free(spa->spa_l2cache.sav_config); | |
4073 | spa->spa_l2cache.sav_config = NULL; | |
4074 | spa_load_l2cache(spa); | |
4075 | } | |
4076 | ||
4077 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
4078 | &nvroot) == 0); | |
4079 | if (error == 0) | |
4080 | error = spa_validate_aux(spa, nvroot, -1ULL, | |
4081 | VDEV_ALLOC_SPARE); | |
4082 | if (error == 0) | |
4083 | error = spa_validate_aux(spa, nvroot, -1ULL, | |
4084 | VDEV_ALLOC_L2CACHE); | |
4085 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4086 | ||
4087 | if (props != NULL) | |
4088 | spa_configfile_set(spa, props, B_FALSE); | |
4089 | ||
4090 | if (error != 0 || (props && spa_writeable(spa) && | |
4091 | (error = spa_prop_set(spa, props)))) { | |
4092 | spa_unload(spa); | |
4093 | spa_deactivate(spa); | |
4094 | spa_remove(spa); | |
4095 | mutex_exit(&spa_namespace_lock); | |
4096 | return (error); | |
4097 | } | |
4098 | ||
4099 | spa_async_resume(spa); | |
4100 | ||
4101 | /* | |
4102 | * Override any spares and level 2 cache devices as specified by | |
4103 | * the user, as these may have correct device names/devids, etc. | |
4104 | */ | |
4105 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
4106 | &spares, &nspares) == 0) { | |
4107 | if (spa->spa_spares.sav_config) | |
4108 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, | |
4109 | ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); | |
4110 | else | |
4111 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, | |
4112 | NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
4113 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, | |
4114 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
4115 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4116 | spa_load_spares(spa); | |
4117 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4118 | spa->spa_spares.sav_sync = B_TRUE; | |
4119 | } | |
4120 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
4121 | &l2cache, &nl2cache) == 0) { | |
4122 | if (spa->spa_l2cache.sav_config) | |
4123 | VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, | |
4124 | ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); | |
4125 | else | |
4126 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
4127 | NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
4128 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, | |
4129 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
4130 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4131 | spa_load_l2cache(spa); | |
4132 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4133 | spa->spa_l2cache.sav_sync = B_TRUE; | |
4134 | } | |
4135 | ||
4136 | /* | |
4137 | * Check for any removed devices. | |
4138 | */ | |
4139 | if (spa->spa_autoreplace) { | |
4140 | spa_aux_check_removed(&spa->spa_spares); | |
4141 | spa_aux_check_removed(&spa->spa_l2cache); | |
4142 | } | |
4143 | ||
4144 | if (spa_writeable(spa)) { | |
4145 | /* | |
4146 | * Update the config cache to include the newly-imported pool. | |
4147 | */ | |
4148 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
4149 | } | |
4150 | ||
4151 | /* | |
4152 | * It's possible that the pool was expanded while it was exported. | |
4153 | * We kick off an async task to handle this for us. | |
4154 | */ | |
4155 | spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); | |
4156 | ||
4157 | spa_history_log_version(spa, "import"); | |
4158 | ||
4159 | spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT); | |
4160 | ||
4161 | zvol_create_minors(spa, pool, B_TRUE); | |
4162 | ||
4163 | mutex_exit(&spa_namespace_lock); | |
4164 | ||
4165 | return (0); | |
4166 | } | |
4167 | ||
4168 | nvlist_t * | |
4169 | spa_tryimport(nvlist_t *tryconfig) | |
4170 | { | |
4171 | nvlist_t *config = NULL; | |
4172 | char *poolname; | |
4173 | spa_t *spa; | |
4174 | uint64_t state; | |
4175 | int error; | |
4176 | ||
4177 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) | |
4178 | return (NULL); | |
4179 | ||
4180 | if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) | |
4181 | return (NULL); | |
4182 | ||
4183 | /* | |
4184 | * Create and initialize the spa structure. | |
4185 | */ | |
4186 | mutex_enter(&spa_namespace_lock); | |
4187 | spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); | |
4188 | spa_activate(spa, FREAD); | |
4189 | ||
4190 | /* | |
4191 | * Pass off the heavy lifting to spa_load(). | |
4192 | * Pass TRUE for mosconfig because the user-supplied config | |
4193 | * is actually the one to trust when doing an import. | |
4194 | */ | |
4195 | error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); | |
4196 | ||
4197 | /* | |
4198 | * If 'tryconfig' was at least parsable, return the current config. | |
4199 | */ | |
4200 | if (spa->spa_root_vdev != NULL) { | |
4201 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
4202 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, | |
4203 | poolname) == 0); | |
4204 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
4205 | state) == 0); | |
4206 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
4207 | spa->spa_uberblock.ub_timestamp) == 0); | |
4208 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, | |
4209 | spa->spa_load_info) == 0); | |
4210 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, | |
4211 | spa->spa_errata) == 0); | |
4212 | ||
4213 | /* | |
4214 | * If the bootfs property exists on this pool then we | |
4215 | * copy it out so that external consumers can tell which | |
4216 | * pools are bootable. | |
4217 | */ | |
4218 | if ((!error || error == EEXIST) && spa->spa_bootfs) { | |
4219 | char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); | |
4220 | ||
4221 | /* | |
4222 | * We have to play games with the name since the | |
4223 | * pool was opened as TRYIMPORT_NAME. | |
4224 | */ | |
4225 | if (dsl_dsobj_to_dsname(spa_name(spa), | |
4226 | spa->spa_bootfs, tmpname) == 0) { | |
4227 | char *cp; | |
4228 | char *dsname; | |
4229 | ||
4230 | dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); | |
4231 | ||
4232 | cp = strchr(tmpname, '/'); | |
4233 | if (cp == NULL) { | |
4234 | (void) strlcpy(dsname, tmpname, | |
4235 | MAXPATHLEN); | |
4236 | } else { | |
4237 | (void) snprintf(dsname, MAXPATHLEN, | |
4238 | "%s/%s", poolname, ++cp); | |
4239 | } | |
4240 | VERIFY(nvlist_add_string(config, | |
4241 | ZPOOL_CONFIG_BOOTFS, dsname) == 0); | |
4242 | kmem_free(dsname, MAXPATHLEN); | |
4243 | } | |
4244 | kmem_free(tmpname, MAXPATHLEN); | |
4245 | } | |
4246 | ||
4247 | /* | |
4248 | * Add the list of hot spares and level 2 cache devices. | |
4249 | */ | |
4250 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
4251 | spa_add_spares(spa, config); | |
4252 | spa_add_l2cache(spa, config); | |
4253 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
4254 | } | |
4255 | ||
4256 | spa_unload(spa); | |
4257 | spa_deactivate(spa); | |
4258 | spa_remove(spa); | |
4259 | mutex_exit(&spa_namespace_lock); | |
4260 | ||
4261 | return (config); | |
4262 | } | |
4263 | ||
4264 | /* | |
4265 | * Pool export/destroy | |
4266 | * | |
4267 | * The act of destroying or exporting a pool is very simple. We make sure there | |
4268 | * is no more pending I/O and any references to the pool are gone. Then, we | |
4269 | * update the pool state and sync all the labels to disk, removing the | |
4270 | * configuration from the cache afterwards. If the 'hardforce' flag is set, then | |
4271 | * we don't sync the labels or remove the configuration cache. | |
4272 | */ | |
4273 | static int | |
4274 | spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, | |
4275 | boolean_t force, boolean_t hardforce) | |
4276 | { | |
4277 | spa_t *spa; | |
4278 | ||
4279 | if (oldconfig) | |
4280 | *oldconfig = NULL; | |
4281 | ||
4282 | if (!(spa_mode_global & FWRITE)) | |
4283 | return (SET_ERROR(EROFS)); | |
4284 | ||
4285 | mutex_enter(&spa_namespace_lock); | |
4286 | if ((spa = spa_lookup(pool)) == NULL) { | |
4287 | mutex_exit(&spa_namespace_lock); | |
4288 | return (SET_ERROR(ENOENT)); | |
4289 | } | |
4290 | ||
4291 | /* | |
4292 | * Put a hold on the pool, drop the namespace lock, stop async tasks, | |
4293 | * reacquire the namespace lock, and see if we can export. | |
4294 | */ | |
4295 | spa_open_ref(spa, FTAG); | |
4296 | mutex_exit(&spa_namespace_lock); | |
4297 | spa_async_suspend(spa); | |
4298 | if (spa->spa_zvol_taskq) { | |
4299 | zvol_remove_minors(spa, spa_name(spa), B_TRUE); | |
4300 | taskq_wait(spa->spa_zvol_taskq); | |
4301 | } | |
4302 | mutex_enter(&spa_namespace_lock); | |
4303 | spa_close(spa, FTAG); | |
4304 | ||
4305 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) | |
4306 | goto export_spa; | |
4307 | /* | |
4308 | * The pool will be in core if it's openable, in which case we can | |
4309 | * modify its state. Objsets may be open only because they're dirty, | |
4310 | * so we have to force it to sync before checking spa_refcnt. | |
4311 | */ | |
4312 | if (spa->spa_sync_on) { | |
4313 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
4314 | spa_evicting_os_wait(spa); | |
4315 | } | |
4316 | ||
4317 | /* | |
4318 | * A pool cannot be exported or destroyed if there are active | |
4319 | * references. If we are resetting a pool, allow references by | |
4320 | * fault injection handlers. | |
4321 | */ | |
4322 | if (!spa_refcount_zero(spa) || | |
4323 | (spa->spa_inject_ref != 0 && | |
4324 | new_state != POOL_STATE_UNINITIALIZED)) { | |
4325 | spa_async_resume(spa); | |
4326 | mutex_exit(&spa_namespace_lock); | |
4327 | return (SET_ERROR(EBUSY)); | |
4328 | } | |
4329 | ||
4330 | if (spa->spa_sync_on) { | |
4331 | /* | |
4332 | * A pool cannot be exported if it has an active shared spare. | |
4333 | * This is to prevent other pools stealing the active spare | |
4334 | * from an exported pool. At user's own will, such pool can | |
4335 | * be forcedly exported. | |
4336 | */ | |
4337 | if (!force && new_state == POOL_STATE_EXPORTED && | |
4338 | spa_has_active_shared_spare(spa)) { | |
4339 | spa_async_resume(spa); | |
4340 | mutex_exit(&spa_namespace_lock); | |
4341 | return (SET_ERROR(EXDEV)); | |
4342 | } | |
4343 | ||
4344 | /* | |
4345 | * We want this to be reflected on every label, | |
4346 | * so mark them all dirty. spa_unload() will do the | |
4347 | * final sync that pushes these changes out. | |
4348 | */ | |
4349 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { | |
4350 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4351 | spa->spa_state = new_state; | |
4352 | spa->spa_final_txg = spa_last_synced_txg(spa) + | |
4353 | TXG_DEFER_SIZE + 1; | |
4354 | vdev_config_dirty(spa->spa_root_vdev); | |
4355 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4356 | } | |
4357 | } | |
4358 | ||
4359 | export_spa: | |
4360 | spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); | |
4361 | ||
4362 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
4363 | spa_unload(spa); | |
4364 | spa_deactivate(spa); | |
4365 | } | |
4366 | ||
4367 | if (oldconfig && spa->spa_config) | |
4368 | VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); | |
4369 | ||
4370 | if (new_state != POOL_STATE_UNINITIALIZED) { | |
4371 | if (!hardforce) | |
4372 | spa_config_sync(spa, B_TRUE, B_TRUE); | |
4373 | spa_remove(spa); | |
4374 | } | |
4375 | mutex_exit(&spa_namespace_lock); | |
4376 | ||
4377 | return (0); | |
4378 | } | |
4379 | ||
4380 | /* | |
4381 | * Destroy a storage pool. | |
4382 | */ | |
4383 | int | |
4384 | spa_destroy(char *pool) | |
4385 | { | |
4386 | return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, | |
4387 | B_FALSE, B_FALSE)); | |
4388 | } | |
4389 | ||
4390 | /* | |
4391 | * Export a storage pool. | |
4392 | */ | |
4393 | int | |
4394 | spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, | |
4395 | boolean_t hardforce) | |
4396 | { | |
4397 | return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, | |
4398 | force, hardforce)); | |
4399 | } | |
4400 | ||
4401 | /* | |
4402 | * Similar to spa_export(), this unloads the spa_t without actually removing it | |
4403 | * from the namespace in any way. | |
4404 | */ | |
4405 | int | |
4406 | spa_reset(char *pool) | |
4407 | { | |
4408 | return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, | |
4409 | B_FALSE, B_FALSE)); | |
4410 | } | |
4411 | ||
4412 | /* | |
4413 | * ========================================================================== | |
4414 | * Device manipulation | |
4415 | * ========================================================================== | |
4416 | */ | |
4417 | ||
4418 | /* | |
4419 | * Add a device to a storage pool. | |
4420 | */ | |
4421 | int | |
4422 | spa_vdev_add(spa_t *spa, nvlist_t *nvroot) | |
4423 | { | |
4424 | uint64_t txg, id; | |
4425 | int error; | |
4426 | vdev_t *rvd = spa->spa_root_vdev; | |
4427 | vdev_t *vd, *tvd; | |
4428 | nvlist_t **spares, **l2cache; | |
4429 | uint_t nspares, nl2cache; | |
4430 | int c; | |
4431 | ||
4432 | ASSERT(spa_writeable(spa)); | |
4433 | ||
4434 | txg = spa_vdev_enter(spa); | |
4435 | ||
4436 | if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, | |
4437 | VDEV_ALLOC_ADD)) != 0) | |
4438 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
4439 | ||
4440 | spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ | |
4441 | ||
4442 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, | |
4443 | &nspares) != 0) | |
4444 | nspares = 0; | |
4445 | ||
4446 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, | |
4447 | &nl2cache) != 0) | |
4448 | nl2cache = 0; | |
4449 | ||
4450 | if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) | |
4451 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); | |
4452 | ||
4453 | if (vd->vdev_children != 0 && | |
4454 | (error = vdev_create(vd, txg, B_FALSE)) != 0) | |
4455 | return (spa_vdev_exit(spa, vd, txg, error)); | |
4456 | ||
4457 | /* | |
4458 | * We must validate the spares and l2cache devices after checking the | |
4459 | * children. Otherwise, vdev_inuse() will blindly overwrite the spare. | |
4460 | */ | |
4461 | if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) | |
4462 | return (spa_vdev_exit(spa, vd, txg, error)); | |
4463 | ||
4464 | /* | |
4465 | * Transfer each new top-level vdev from vd to rvd. | |
4466 | */ | |
4467 | for (c = 0; c < vd->vdev_children; c++) { | |
4468 | ||
4469 | /* | |
4470 | * Set the vdev id to the first hole, if one exists. | |
4471 | */ | |
4472 | for (id = 0; id < rvd->vdev_children; id++) { | |
4473 | if (rvd->vdev_child[id]->vdev_ishole) { | |
4474 | vdev_free(rvd->vdev_child[id]); | |
4475 | break; | |
4476 | } | |
4477 | } | |
4478 | tvd = vd->vdev_child[c]; | |
4479 | vdev_remove_child(vd, tvd); | |
4480 | tvd->vdev_id = id; | |
4481 | vdev_add_child(rvd, tvd); | |
4482 | vdev_config_dirty(tvd); | |
4483 | } | |
4484 | ||
4485 | if (nspares != 0) { | |
4486 | spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, | |
4487 | ZPOOL_CONFIG_SPARES); | |
4488 | spa_load_spares(spa); | |
4489 | spa->spa_spares.sav_sync = B_TRUE; | |
4490 | } | |
4491 | ||
4492 | if (nl2cache != 0) { | |
4493 | spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, | |
4494 | ZPOOL_CONFIG_L2CACHE); | |
4495 | spa_load_l2cache(spa); | |
4496 | spa->spa_l2cache.sav_sync = B_TRUE; | |
4497 | } | |
4498 | ||
4499 | /* | |
4500 | * We have to be careful when adding new vdevs to an existing pool. | |
4501 | * If other threads start allocating from these vdevs before we | |
4502 | * sync the config cache, and we lose power, then upon reboot we may | |
4503 | * fail to open the pool because there are DVAs that the config cache | |
4504 | * can't translate. Therefore, we first add the vdevs without | |
4505 | * initializing metaslabs; sync the config cache (via spa_vdev_exit()); | |
4506 | * and then let spa_config_update() initialize the new metaslabs. | |
4507 | * | |
4508 | * spa_load() checks for added-but-not-initialized vdevs, so that | |
4509 | * if we lose power at any point in this sequence, the remaining | |
4510 | * steps will be completed the next time we load the pool. | |
4511 | */ | |
4512 | (void) spa_vdev_exit(spa, vd, txg, 0); | |
4513 | ||
4514 | mutex_enter(&spa_namespace_lock); | |
4515 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
4516 | spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD); | |
4517 | mutex_exit(&spa_namespace_lock); | |
4518 | ||
4519 | return (0); | |
4520 | } | |
4521 | ||
4522 | /* | |
4523 | * Attach a device to a mirror. The arguments are the path to any device | |
4524 | * in the mirror, and the nvroot for the new device. If the path specifies | |
4525 | * a device that is not mirrored, we automatically insert the mirror vdev. | |
4526 | * | |
4527 | * If 'replacing' is specified, the new device is intended to replace the | |
4528 | * existing device; in this case the two devices are made into their own | |
4529 | * mirror using the 'replacing' vdev, which is functionally identical to | |
4530 | * the mirror vdev (it actually reuses all the same ops) but has a few | |
4531 | * extra rules: you can't attach to it after it's been created, and upon | |
4532 | * completion of resilvering, the first disk (the one being replaced) | |
4533 | * is automatically detached. | |
4534 | */ | |
4535 | int | |
4536 | spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) | |
4537 | { | |
4538 | uint64_t txg, dtl_max_txg; | |
4539 | vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; | |
4540 | vdev_ops_t *pvops; | |
4541 | char *oldvdpath, *newvdpath; | |
4542 | int newvd_isspare; | |
4543 | int error; | |
4544 | ASSERTV(vdev_t *rvd = spa->spa_root_vdev); | |
4545 | ||
4546 | ASSERT(spa_writeable(spa)); | |
4547 | ||
4548 | txg = spa_vdev_enter(spa); | |
4549 | ||
4550 | oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
4551 | ||
4552 | if (oldvd == NULL) | |
4553 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
4554 | ||
4555 | if (!oldvd->vdev_ops->vdev_op_leaf) | |
4556 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
4557 | ||
4558 | pvd = oldvd->vdev_parent; | |
4559 | ||
4560 | if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, | |
4561 | VDEV_ALLOC_ATTACH)) != 0) | |
4562 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
4563 | ||
4564 | if (newrootvd->vdev_children != 1) | |
4565 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
4566 | ||
4567 | newvd = newrootvd->vdev_child[0]; | |
4568 | ||
4569 | if (!newvd->vdev_ops->vdev_op_leaf) | |
4570 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
4571 | ||
4572 | if ((error = vdev_create(newrootvd, txg, replacing)) != 0) | |
4573 | return (spa_vdev_exit(spa, newrootvd, txg, error)); | |
4574 | ||
4575 | /* | |
4576 | * Spares can't replace logs | |
4577 | */ | |
4578 | if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) | |
4579 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
4580 | ||
4581 | if (!replacing) { | |
4582 | /* | |
4583 | * For attach, the only allowable parent is a mirror or the root | |
4584 | * vdev. | |
4585 | */ | |
4586 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
4587 | pvd->vdev_ops != &vdev_root_ops) | |
4588 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
4589 | ||
4590 | pvops = &vdev_mirror_ops; | |
4591 | } else { | |
4592 | /* | |
4593 | * Active hot spares can only be replaced by inactive hot | |
4594 | * spares. | |
4595 | */ | |
4596 | if (pvd->vdev_ops == &vdev_spare_ops && | |
4597 | oldvd->vdev_isspare && | |
4598 | !spa_has_spare(spa, newvd->vdev_guid)) | |
4599 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
4600 | ||
4601 | /* | |
4602 | * If the source is a hot spare, and the parent isn't already a | |
4603 | * spare, then we want to create a new hot spare. Otherwise, we | |
4604 | * want to create a replacing vdev. The user is not allowed to | |
4605 | * attach to a spared vdev child unless the 'isspare' state is | |
4606 | * the same (spare replaces spare, non-spare replaces | |
4607 | * non-spare). | |
4608 | */ | |
4609 | if (pvd->vdev_ops == &vdev_replacing_ops && | |
4610 | spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { | |
4611 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
4612 | } else if (pvd->vdev_ops == &vdev_spare_ops && | |
4613 | newvd->vdev_isspare != oldvd->vdev_isspare) { | |
4614 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
4615 | } | |
4616 | ||
4617 | if (newvd->vdev_isspare) | |
4618 | pvops = &vdev_spare_ops; | |
4619 | else | |
4620 | pvops = &vdev_replacing_ops; | |
4621 | } | |
4622 | ||
4623 | /* | |
4624 | * Make sure the new device is big enough. | |
4625 | */ | |
4626 | if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) | |
4627 | return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); | |
4628 | ||
4629 | /* | |
4630 | * The new device cannot have a higher alignment requirement | |
4631 | * than the top-level vdev. | |
4632 | */ | |
4633 | if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) | |
4634 | return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); | |
4635 | ||
4636 | /* | |
4637 | * If this is an in-place replacement, update oldvd's path and devid | |
4638 | * to make it distinguishable from newvd, and unopenable from now on. | |
4639 | */ | |
4640 | if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { | |
4641 | spa_strfree(oldvd->vdev_path); | |
4642 | oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, | |
4643 | KM_SLEEP); | |
4644 | (void) sprintf(oldvd->vdev_path, "%s/%s", | |
4645 | newvd->vdev_path, "old"); | |
4646 | if (oldvd->vdev_devid != NULL) { | |
4647 | spa_strfree(oldvd->vdev_devid); | |
4648 | oldvd->vdev_devid = NULL; | |
4649 | } | |
4650 | } | |
4651 | ||
4652 | /* mark the device being resilvered */ | |
4653 | newvd->vdev_resilver_txg = txg; | |
4654 | ||
4655 | /* | |
4656 | * If the parent is not a mirror, or if we're replacing, insert the new | |
4657 | * mirror/replacing/spare vdev above oldvd. | |
4658 | */ | |
4659 | if (pvd->vdev_ops != pvops) | |
4660 | pvd = vdev_add_parent(oldvd, pvops); | |
4661 | ||
4662 | ASSERT(pvd->vdev_top->vdev_parent == rvd); | |
4663 | ASSERT(pvd->vdev_ops == pvops); | |
4664 | ASSERT(oldvd->vdev_parent == pvd); | |
4665 | ||
4666 | /* | |
4667 | * Extract the new device from its root and add it to pvd. | |
4668 | */ | |
4669 | vdev_remove_child(newrootvd, newvd); | |
4670 | newvd->vdev_id = pvd->vdev_children; | |
4671 | newvd->vdev_crtxg = oldvd->vdev_crtxg; | |
4672 | vdev_add_child(pvd, newvd); | |
4673 | ||
4674 | tvd = newvd->vdev_top; | |
4675 | ASSERT(pvd->vdev_top == tvd); | |
4676 | ASSERT(tvd->vdev_parent == rvd); | |
4677 | ||
4678 | vdev_config_dirty(tvd); | |
4679 | ||
4680 | /* | |
4681 | * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account | |
4682 | * for any dmu_sync-ed blocks. It will propagate upward when | |
4683 | * spa_vdev_exit() calls vdev_dtl_reassess(). | |
4684 | */ | |
4685 | dtl_max_txg = txg + TXG_CONCURRENT_STATES; | |
4686 | ||
4687 | vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, | |
4688 | dtl_max_txg - TXG_INITIAL); | |
4689 | ||
4690 | if (newvd->vdev_isspare) { | |
4691 | spa_spare_activate(newvd); | |
4692 | spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); | |
4693 | } | |
4694 | ||
4695 | oldvdpath = spa_strdup(oldvd->vdev_path); | |
4696 | newvdpath = spa_strdup(newvd->vdev_path); | |
4697 | newvd_isspare = newvd->vdev_isspare; | |
4698 | ||
4699 | /* | |
4700 | * Mark newvd's DTL dirty in this txg. | |
4701 | */ | |
4702 | vdev_dirty(tvd, VDD_DTL, newvd, txg); | |
4703 | ||
4704 | /* | |
4705 | * Schedule the resilver to restart in the future. We do this to | |
4706 | * ensure that dmu_sync-ed blocks have been stitched into the | |
4707 | * respective datasets. | |
4708 | */ | |
4709 | dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); | |
4710 | ||
4711 | if (spa->spa_bootfs) | |
4712 | spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH); | |
4713 | ||
4714 | spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH); | |
4715 | ||
4716 | /* | |
4717 | * Commit the config | |
4718 | */ | |
4719 | (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); | |
4720 | ||
4721 | spa_history_log_internal(spa, "vdev attach", NULL, | |
4722 | "%s vdev=%s %s vdev=%s", | |
4723 | replacing && newvd_isspare ? "spare in" : | |
4724 | replacing ? "replace" : "attach", newvdpath, | |
4725 | replacing ? "for" : "to", oldvdpath); | |
4726 | ||
4727 | spa_strfree(oldvdpath); | |
4728 | spa_strfree(newvdpath); | |
4729 | ||
4730 | return (0); | |
4731 | } | |
4732 | ||
4733 | /* | |
4734 | * Detach a device from a mirror or replacing vdev. | |
4735 | * | |
4736 | * If 'replace_done' is specified, only detach if the parent | |
4737 | * is a replacing vdev. | |
4738 | */ | |
4739 | int | |
4740 | spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) | |
4741 | { | |
4742 | uint64_t txg; | |
4743 | int error; | |
4744 | vdev_t *vd, *pvd, *cvd, *tvd; | |
4745 | boolean_t unspare = B_FALSE; | |
4746 | uint64_t unspare_guid = 0; | |
4747 | char *vdpath; | |
4748 | int c, t; | |
4749 | ASSERTV(vdev_t *rvd = spa->spa_root_vdev); | |
4750 | ASSERT(spa_writeable(spa)); | |
4751 | ||
4752 | txg = spa_vdev_enter(spa); | |
4753 | ||
4754 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
4755 | ||
4756 | if (vd == NULL) | |
4757 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
4758 | ||
4759 | if (!vd->vdev_ops->vdev_op_leaf) | |
4760 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
4761 | ||
4762 | pvd = vd->vdev_parent; | |
4763 | ||
4764 | /* | |
4765 | * If the parent/child relationship is not as expected, don't do it. | |
4766 | * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing | |
4767 | * vdev that's replacing B with C. The user's intent in replacing | |
4768 | * is to go from M(A,B) to M(A,C). If the user decides to cancel | |
4769 | * the replace by detaching C, the expected behavior is to end up | |
4770 | * M(A,B). But suppose that right after deciding to detach C, | |
4771 | * the replacement of B completes. We would have M(A,C), and then | |
4772 | * ask to detach C, which would leave us with just A -- not what | |
4773 | * the user wanted. To prevent this, we make sure that the | |
4774 | * parent/child relationship hasn't changed -- in this example, | |
4775 | * that C's parent is still the replacing vdev R. | |
4776 | */ | |
4777 | if (pvd->vdev_guid != pguid && pguid != 0) | |
4778 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
4779 | ||
4780 | /* | |
4781 | * Only 'replacing' or 'spare' vdevs can be replaced. | |
4782 | */ | |
4783 | if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && | |
4784 | pvd->vdev_ops != &vdev_spare_ops) | |
4785 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
4786 | ||
4787 | ASSERT(pvd->vdev_ops != &vdev_spare_ops || | |
4788 | spa_version(spa) >= SPA_VERSION_SPARES); | |
4789 | ||
4790 | /* | |
4791 | * Only mirror, replacing, and spare vdevs support detach. | |
4792 | */ | |
4793 | if (pvd->vdev_ops != &vdev_replacing_ops && | |
4794 | pvd->vdev_ops != &vdev_mirror_ops && | |
4795 | pvd->vdev_ops != &vdev_spare_ops) | |
4796 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
4797 | ||
4798 | /* | |
4799 | * If this device has the only valid copy of some data, | |
4800 | * we cannot safely detach it. | |
4801 | */ | |
4802 | if (vdev_dtl_required(vd)) | |
4803 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
4804 | ||
4805 | ASSERT(pvd->vdev_children >= 2); | |
4806 | ||
4807 | /* | |
4808 | * If we are detaching the second disk from a replacing vdev, then | |
4809 | * check to see if we changed the original vdev's path to have "/old" | |
4810 | * at the end in spa_vdev_attach(). If so, undo that change now. | |
4811 | */ | |
4812 | if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && | |
4813 | vd->vdev_path != NULL) { | |
4814 | size_t len = strlen(vd->vdev_path); | |
4815 | ||
4816 | for (c = 0; c < pvd->vdev_children; c++) { | |
4817 | cvd = pvd->vdev_child[c]; | |
4818 | ||
4819 | if (cvd == vd || cvd->vdev_path == NULL) | |
4820 | continue; | |
4821 | ||
4822 | if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && | |
4823 | strcmp(cvd->vdev_path + len, "/old") == 0) { | |
4824 | spa_strfree(cvd->vdev_path); | |
4825 | cvd->vdev_path = spa_strdup(vd->vdev_path); | |
4826 | break; | |
4827 | } | |
4828 | } | |
4829 | } | |
4830 | ||
4831 | /* | |
4832 | * If we are detaching the original disk from a spare, then it implies | |
4833 | * that the spare should become a real disk, and be removed from the | |
4834 | * active spare list for the pool. | |
4835 | */ | |
4836 | if (pvd->vdev_ops == &vdev_spare_ops && | |
4837 | vd->vdev_id == 0 && | |
4838 | pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) | |
4839 | unspare = B_TRUE; | |
4840 | ||
4841 | /* | |
4842 | * Erase the disk labels so the disk can be used for other things. | |
4843 | * This must be done after all other error cases are handled, | |
4844 | * but before we disembowel vd (so we can still do I/O to it). | |
4845 | * But if we can't do it, don't treat the error as fatal -- | |
4846 | * it may be that the unwritability of the disk is the reason | |
4847 | * it's being detached! | |
4848 | */ | |
4849 | error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); | |
4850 | ||
4851 | /* | |
4852 | * Remove vd from its parent and compact the parent's children. | |
4853 | */ | |
4854 | vdev_remove_child(pvd, vd); | |
4855 | vdev_compact_children(pvd); | |
4856 | ||
4857 | /* | |
4858 | * Remember one of the remaining children so we can get tvd below. | |
4859 | */ | |
4860 | cvd = pvd->vdev_child[pvd->vdev_children - 1]; | |
4861 | ||
4862 | /* | |
4863 | * If we need to remove the remaining child from the list of hot spares, | |
4864 | * do it now, marking the vdev as no longer a spare in the process. | |
4865 | * We must do this before vdev_remove_parent(), because that can | |
4866 | * change the GUID if it creates a new toplevel GUID. For a similar | |
4867 | * reason, we must remove the spare now, in the same txg as the detach; | |
4868 | * otherwise someone could attach a new sibling, change the GUID, and | |
4869 | * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. | |
4870 | */ | |
4871 | if (unspare) { | |
4872 | ASSERT(cvd->vdev_isspare); | |
4873 | spa_spare_remove(cvd); | |
4874 | unspare_guid = cvd->vdev_guid; | |
4875 | (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); | |
4876 | cvd->vdev_unspare = B_TRUE; | |
4877 | } | |
4878 | ||
4879 | /* | |
4880 | * If the parent mirror/replacing vdev only has one child, | |
4881 | * the parent is no longer needed. Remove it from the tree. | |
4882 | */ | |
4883 | if (pvd->vdev_children == 1) { | |
4884 | if (pvd->vdev_ops == &vdev_spare_ops) | |
4885 | cvd->vdev_unspare = B_FALSE; | |
4886 | vdev_remove_parent(cvd); | |
4887 | } | |
4888 | ||
4889 | ||
4890 | /* | |
4891 | * We don't set tvd until now because the parent we just removed | |
4892 | * may have been the previous top-level vdev. | |
4893 | */ | |
4894 | tvd = cvd->vdev_top; | |
4895 | ASSERT(tvd->vdev_parent == rvd); | |
4896 | ||
4897 | /* | |
4898 | * Reevaluate the parent vdev state. | |
4899 | */ | |
4900 | vdev_propagate_state(cvd); | |
4901 | ||
4902 | /* | |
4903 | * If the 'autoexpand' property is set on the pool then automatically | |
4904 | * try to expand the size of the pool. For example if the device we | |
4905 | * just detached was smaller than the others, it may be possible to | |
4906 | * add metaslabs (i.e. grow the pool). We need to reopen the vdev | |
4907 | * first so that we can obtain the updated sizes of the leaf vdevs. | |
4908 | */ | |
4909 | if (spa->spa_autoexpand) { | |
4910 | vdev_reopen(tvd); | |
4911 | vdev_expand(tvd, txg); | |
4912 | } | |
4913 | ||
4914 | vdev_config_dirty(tvd); | |
4915 | ||
4916 | /* | |
4917 | * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that | |
4918 | * vd->vdev_detached is set and free vd's DTL object in syncing context. | |
4919 | * But first make sure we're not on any *other* txg's DTL list, to | |
4920 | * prevent vd from being accessed after it's freed. | |
4921 | */ | |
4922 | vdpath = spa_strdup(vd->vdev_path); | |
4923 | for (t = 0; t < TXG_SIZE; t++) | |
4924 | (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); | |
4925 | vd->vdev_detached = B_TRUE; | |
4926 | vdev_dirty(tvd, VDD_DTL, vd, txg); | |
4927 | ||
4928 | spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); | |
4929 | ||
4930 | /* hang on to the spa before we release the lock */ | |
4931 | spa_open_ref(spa, FTAG); | |
4932 | ||
4933 | error = spa_vdev_exit(spa, vd, txg, 0); | |
4934 | ||
4935 | spa_history_log_internal(spa, "detach", NULL, | |
4936 | "vdev=%s", vdpath); | |
4937 | spa_strfree(vdpath); | |
4938 | ||
4939 | /* | |
4940 | * If this was the removal of the original device in a hot spare vdev, | |
4941 | * then we want to go through and remove the device from the hot spare | |
4942 | * list of every other pool. | |
4943 | */ | |
4944 | if (unspare) { | |
4945 | spa_t *altspa = NULL; | |
4946 | ||
4947 | mutex_enter(&spa_namespace_lock); | |
4948 | while ((altspa = spa_next(altspa)) != NULL) { | |
4949 | if (altspa->spa_state != POOL_STATE_ACTIVE || | |
4950 | altspa == spa) | |
4951 | continue; | |
4952 | ||
4953 | spa_open_ref(altspa, FTAG); | |
4954 | mutex_exit(&spa_namespace_lock); | |
4955 | (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); | |
4956 | mutex_enter(&spa_namespace_lock); | |
4957 | spa_close(altspa, FTAG); | |
4958 | } | |
4959 | mutex_exit(&spa_namespace_lock); | |
4960 | ||
4961 | /* search the rest of the vdevs for spares to remove */ | |
4962 | spa_vdev_resilver_done(spa); | |
4963 | } | |
4964 | ||
4965 | /* all done with the spa; OK to release */ | |
4966 | mutex_enter(&spa_namespace_lock); | |
4967 | spa_close(spa, FTAG); | |
4968 | mutex_exit(&spa_namespace_lock); | |
4969 | ||
4970 | return (error); | |
4971 | } | |
4972 | ||
4973 | /* | |
4974 | * Split a set of devices from their mirrors, and create a new pool from them. | |
4975 | */ | |
4976 | int | |
4977 | spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, | |
4978 | nvlist_t *props, boolean_t exp) | |
4979 | { | |
4980 | int error = 0; | |
4981 | uint64_t txg, *glist; | |
4982 | spa_t *newspa; | |
4983 | uint_t c, children, lastlog; | |
4984 | nvlist_t **child, *nvl, *tmp; | |
4985 | dmu_tx_t *tx; | |
4986 | char *altroot = NULL; | |
4987 | vdev_t *rvd, **vml = NULL; /* vdev modify list */ | |
4988 | boolean_t activate_slog; | |
4989 | ||
4990 | ASSERT(spa_writeable(spa)); | |
4991 | ||
4992 | txg = spa_vdev_enter(spa); | |
4993 | ||
4994 | /* clear the log and flush everything up to now */ | |
4995 | activate_slog = spa_passivate_log(spa); | |
4996 | (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
4997 | error = spa_offline_log(spa); | |
4998 | txg = spa_vdev_config_enter(spa); | |
4999 | ||
5000 | if (activate_slog) | |
5001 | spa_activate_log(spa); | |
5002 | ||
5003 | if (error != 0) | |
5004 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
5005 | ||
5006 | /* check new spa name before going any further */ | |
5007 | if (spa_lookup(newname) != NULL) | |
5008 | return (spa_vdev_exit(spa, NULL, txg, EEXIST)); | |
5009 | ||
5010 | /* | |
5011 | * scan through all the children to ensure they're all mirrors | |
5012 | */ | |
5013 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || | |
5014 | nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, | |
5015 | &children) != 0) | |
5016 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
5017 | ||
5018 | /* first, check to ensure we've got the right child count */ | |
5019 | rvd = spa->spa_root_vdev; | |
5020 | lastlog = 0; | |
5021 | for (c = 0; c < rvd->vdev_children; c++) { | |
5022 | vdev_t *vd = rvd->vdev_child[c]; | |
5023 | ||
5024 | /* don't count the holes & logs as children */ | |
5025 | if (vd->vdev_islog || vd->vdev_ishole) { | |
5026 | if (lastlog == 0) | |
5027 | lastlog = c; | |
5028 | continue; | |
5029 | } | |
5030 | ||
5031 | lastlog = 0; | |
5032 | } | |
5033 | if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) | |
5034 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
5035 | ||
5036 | /* next, ensure no spare or cache devices are part of the split */ | |
5037 | if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || | |
5038 | nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) | |
5039 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
5040 | ||
5041 | vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); | |
5042 | glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); | |
5043 | ||
5044 | /* then, loop over each vdev and validate it */ | |
5045 | for (c = 0; c < children; c++) { | |
5046 | uint64_t is_hole = 0; | |
5047 | ||
5048 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
5049 | &is_hole); | |
5050 | ||
5051 | if (is_hole != 0) { | |
5052 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || | |
5053 | spa->spa_root_vdev->vdev_child[c]->vdev_islog) { | |
5054 | continue; | |
5055 | } else { | |
5056 | error = SET_ERROR(EINVAL); | |
5057 | break; | |
5058 | } | |
5059 | } | |
5060 | ||
5061 | /* which disk is going to be split? */ | |
5062 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, | |
5063 | &glist[c]) != 0) { | |
5064 | error = SET_ERROR(EINVAL); | |
5065 | break; | |
5066 | } | |
5067 | ||
5068 | /* look it up in the spa */ | |
5069 | vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); | |
5070 | if (vml[c] == NULL) { | |
5071 | error = SET_ERROR(ENODEV); | |
5072 | break; | |
5073 | } | |
5074 | ||
5075 | /* make sure there's nothing stopping the split */ | |
5076 | if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || | |
5077 | vml[c]->vdev_islog || | |
5078 | vml[c]->vdev_ishole || | |
5079 | vml[c]->vdev_isspare || | |
5080 | vml[c]->vdev_isl2cache || | |
5081 | !vdev_writeable(vml[c]) || | |
5082 | vml[c]->vdev_children != 0 || | |
5083 | vml[c]->vdev_state != VDEV_STATE_HEALTHY || | |
5084 | c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { | |
5085 | error = SET_ERROR(EINVAL); | |
5086 | break; | |
5087 | } | |
5088 | ||
5089 | if (vdev_dtl_required(vml[c])) { | |
5090 | error = SET_ERROR(EBUSY); | |
5091 | break; | |
5092 | } | |
5093 | ||
5094 | /* we need certain info from the top level */ | |
5095 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, | |
5096 | vml[c]->vdev_top->vdev_ms_array) == 0); | |
5097 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, | |
5098 | vml[c]->vdev_top->vdev_ms_shift) == 0); | |
5099 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, | |
5100 | vml[c]->vdev_top->vdev_asize) == 0); | |
5101 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, | |
5102 | vml[c]->vdev_top->vdev_ashift) == 0); | |
5103 | ||
5104 | /* transfer per-vdev ZAPs */ | |
5105 | ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); | |
5106 | VERIFY0(nvlist_add_uint64(child[c], | |
5107 | ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); | |
5108 | ||
5109 | ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); | |
5110 | VERIFY0(nvlist_add_uint64(child[c], | |
5111 | ZPOOL_CONFIG_VDEV_TOP_ZAP, | |
5112 | vml[c]->vdev_parent->vdev_top_zap)); | |
5113 | } | |
5114 | ||
5115 | if (error != 0) { | |
5116 | kmem_free(vml, children * sizeof (vdev_t *)); | |
5117 | kmem_free(glist, children * sizeof (uint64_t)); | |
5118 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
5119 | } | |
5120 | ||
5121 | /* stop writers from using the disks */ | |
5122 | for (c = 0; c < children; c++) { | |
5123 | if (vml[c] != NULL) | |
5124 | vml[c]->vdev_offline = B_TRUE; | |
5125 | } | |
5126 | vdev_reopen(spa->spa_root_vdev); | |
5127 | ||
5128 | /* | |
5129 | * Temporarily record the splitting vdevs in the spa config. This | |
5130 | * will disappear once the config is regenerated. | |
5131 | */ | |
5132 | VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
5133 | VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, | |
5134 | glist, children) == 0); | |
5135 | kmem_free(glist, children * sizeof (uint64_t)); | |
5136 | ||
5137 | mutex_enter(&spa->spa_props_lock); | |
5138 | VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, | |
5139 | nvl) == 0); | |
5140 | mutex_exit(&spa->spa_props_lock); | |
5141 | spa->spa_config_splitting = nvl; | |
5142 | vdev_config_dirty(spa->spa_root_vdev); | |
5143 | ||
5144 | /* configure and create the new pool */ | |
5145 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); | |
5146 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
5147 | exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); | |
5148 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
5149 | spa_version(spa)) == 0); | |
5150 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
5151 | spa->spa_config_txg) == 0); | |
5152 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
5153 | spa_generate_guid(NULL)) == 0); | |
5154 | VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); | |
5155 | (void) nvlist_lookup_string(props, | |
5156 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
5157 | ||
5158 | /* add the new pool to the namespace */ | |
5159 | newspa = spa_add(newname, config, altroot); | |
5160 | newspa->spa_avz_action = AVZ_ACTION_REBUILD; | |
5161 | newspa->spa_config_txg = spa->spa_config_txg; | |
5162 | spa_set_log_state(newspa, SPA_LOG_CLEAR); | |
5163 | ||
5164 | /* release the spa config lock, retaining the namespace lock */ | |
5165 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
5166 | ||
5167 | if (zio_injection_enabled) | |
5168 | zio_handle_panic_injection(spa, FTAG, 1); | |
5169 | ||
5170 | spa_activate(newspa, spa_mode_global); | |
5171 | spa_async_suspend(newspa); | |
5172 | ||
5173 | /* create the new pool from the disks of the original pool */ | |
5174 | error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); | |
5175 | if (error) | |
5176 | goto out; | |
5177 | ||
5178 | /* if that worked, generate a real config for the new pool */ | |
5179 | if (newspa->spa_root_vdev != NULL) { | |
5180 | VERIFY(nvlist_alloc(&newspa->spa_config_splitting, | |
5181 | NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
5182 | VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, | |
5183 | ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); | |
5184 | spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, | |
5185 | B_TRUE)); | |
5186 | } | |
5187 | ||
5188 | /* set the props */ | |
5189 | if (props != NULL) { | |
5190 | spa_configfile_set(newspa, props, B_FALSE); | |
5191 | error = spa_prop_set(newspa, props); | |
5192 | if (error) | |
5193 | goto out; | |
5194 | } | |
5195 | ||
5196 | /* flush everything */ | |
5197 | txg = spa_vdev_config_enter(newspa); | |
5198 | vdev_config_dirty(newspa->spa_root_vdev); | |
5199 | (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); | |
5200 | ||
5201 | if (zio_injection_enabled) | |
5202 | zio_handle_panic_injection(spa, FTAG, 2); | |
5203 | ||
5204 | spa_async_resume(newspa); | |
5205 | ||
5206 | /* finally, update the original pool's config */ | |
5207 | txg = spa_vdev_config_enter(spa); | |
5208 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
5209 | error = dmu_tx_assign(tx, TXG_WAIT); | |
5210 | if (error != 0) | |
5211 | dmu_tx_abort(tx); | |
5212 | for (c = 0; c < children; c++) { | |
5213 | if (vml[c] != NULL) { | |
5214 | vdev_split(vml[c]); | |
5215 | if (error == 0) | |
5216 | spa_history_log_internal(spa, "detach", tx, | |
5217 | "vdev=%s", vml[c]->vdev_path); | |
5218 | ||
5219 | vdev_free(vml[c]); | |
5220 | } | |
5221 | } | |
5222 | spa->spa_avz_action = AVZ_ACTION_REBUILD; | |
5223 | vdev_config_dirty(spa->spa_root_vdev); | |
5224 | spa->spa_config_splitting = NULL; | |
5225 | nvlist_free(nvl); | |
5226 | if (error == 0) | |
5227 | dmu_tx_commit(tx); | |
5228 | (void) spa_vdev_exit(spa, NULL, txg, 0); | |
5229 | ||
5230 | if (zio_injection_enabled) | |
5231 | zio_handle_panic_injection(spa, FTAG, 3); | |
5232 | ||
5233 | /* split is complete; log a history record */ | |
5234 | spa_history_log_internal(newspa, "split", NULL, | |
5235 | "from pool %s", spa_name(spa)); | |
5236 | ||
5237 | kmem_free(vml, children * sizeof (vdev_t *)); | |
5238 | ||
5239 | /* if we're not going to mount the filesystems in userland, export */ | |
5240 | if (exp) | |
5241 | error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, | |
5242 | B_FALSE, B_FALSE); | |
5243 | ||
5244 | return (error); | |
5245 | ||
5246 | out: | |
5247 | spa_unload(newspa); | |
5248 | spa_deactivate(newspa); | |
5249 | spa_remove(newspa); | |
5250 | ||
5251 | txg = spa_vdev_config_enter(spa); | |
5252 | ||
5253 | /* re-online all offlined disks */ | |
5254 | for (c = 0; c < children; c++) { | |
5255 | if (vml[c] != NULL) | |
5256 | vml[c]->vdev_offline = B_FALSE; | |
5257 | } | |
5258 | vdev_reopen(spa->spa_root_vdev); | |
5259 | ||
5260 | nvlist_free(spa->spa_config_splitting); | |
5261 | spa->spa_config_splitting = NULL; | |
5262 | (void) spa_vdev_exit(spa, NULL, txg, error); | |
5263 | ||
5264 | kmem_free(vml, children * sizeof (vdev_t *)); | |
5265 | return (error); | |
5266 | } | |
5267 | ||
5268 | static nvlist_t * | |
5269 | spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) | |
5270 | { | |
5271 | int i; | |
5272 | ||
5273 | for (i = 0; i < count; i++) { | |
5274 | uint64_t guid; | |
5275 | ||
5276 | VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, | |
5277 | &guid) == 0); | |
5278 | ||
5279 | if (guid == target_guid) | |
5280 | return (nvpp[i]); | |
5281 | } | |
5282 | ||
5283 | return (NULL); | |
5284 | } | |
5285 | ||
5286 | static void | |
5287 | spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, | |
5288 | nvlist_t *dev_to_remove) | |
5289 | { | |
5290 | nvlist_t **newdev = NULL; | |
5291 | int i, j; | |
5292 | ||
5293 | if (count > 1) | |
5294 | newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); | |
5295 | ||
5296 | for (i = 0, j = 0; i < count; i++) { | |
5297 | if (dev[i] == dev_to_remove) | |
5298 | continue; | |
5299 | VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); | |
5300 | } | |
5301 | ||
5302 | VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); | |
5303 | VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); | |
5304 | ||
5305 | for (i = 0; i < count - 1; i++) | |
5306 | nvlist_free(newdev[i]); | |
5307 | ||
5308 | if (count > 1) | |
5309 | kmem_free(newdev, (count - 1) * sizeof (void *)); | |
5310 | } | |
5311 | ||
5312 | /* | |
5313 | * Evacuate the device. | |
5314 | */ | |
5315 | static int | |
5316 | spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) | |
5317 | { | |
5318 | uint64_t txg; | |
5319 | int error = 0; | |
5320 | ||
5321 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
5322 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
5323 | ASSERT(vd == vd->vdev_top); | |
5324 | ||
5325 | /* | |
5326 | * Evacuate the device. We don't hold the config lock as writer | |
5327 | * since we need to do I/O but we do keep the | |
5328 | * spa_namespace_lock held. Once this completes the device | |
5329 | * should no longer have any blocks allocated on it. | |
5330 | */ | |
5331 | if (vd->vdev_islog) { | |
5332 | if (vd->vdev_stat.vs_alloc != 0) | |
5333 | error = spa_offline_log(spa); | |
5334 | } else { | |
5335 | error = SET_ERROR(ENOTSUP); | |
5336 | } | |
5337 | ||
5338 | if (error) | |
5339 | return (error); | |
5340 | ||
5341 | /* | |
5342 | * The evacuation succeeded. Remove any remaining MOS metadata | |
5343 | * associated with this vdev, and wait for these changes to sync. | |
5344 | */ | |
5345 | ASSERT0(vd->vdev_stat.vs_alloc); | |
5346 | txg = spa_vdev_config_enter(spa); | |
5347 | vd->vdev_removing = B_TRUE; | |
5348 | vdev_dirty_leaves(vd, VDD_DTL, txg); | |
5349 | vdev_config_dirty(vd); | |
5350 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
5351 | ||
5352 | return (0); | |
5353 | } | |
5354 | ||
5355 | /* | |
5356 | * Complete the removal by cleaning up the namespace. | |
5357 | */ | |
5358 | static void | |
5359 | spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) | |
5360 | { | |
5361 | vdev_t *rvd = spa->spa_root_vdev; | |
5362 | uint64_t id = vd->vdev_id; | |
5363 | boolean_t last_vdev = (id == (rvd->vdev_children - 1)); | |
5364 | ||
5365 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
5366 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); | |
5367 | ASSERT(vd == vd->vdev_top); | |
5368 | ||
5369 | /* | |
5370 | * Only remove any devices which are empty. | |
5371 | */ | |
5372 | if (vd->vdev_stat.vs_alloc != 0) | |
5373 | return; | |
5374 | ||
5375 | (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); | |
5376 | ||
5377 | if (list_link_active(&vd->vdev_state_dirty_node)) | |
5378 | vdev_state_clean(vd); | |
5379 | if (list_link_active(&vd->vdev_config_dirty_node)) | |
5380 | vdev_config_clean(vd); | |
5381 | ||
5382 | vdev_free(vd); | |
5383 | ||
5384 | if (last_vdev) { | |
5385 | vdev_compact_children(rvd); | |
5386 | } else { | |
5387 | vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); | |
5388 | vdev_add_child(rvd, vd); | |
5389 | } | |
5390 | vdev_config_dirty(rvd); | |
5391 | ||
5392 | /* | |
5393 | * Reassess the health of our root vdev. | |
5394 | */ | |
5395 | vdev_reopen(rvd); | |
5396 | } | |
5397 | ||
5398 | /* | |
5399 | * Remove a device from the pool - | |
5400 | * | |
5401 | * Removing a device from the vdev namespace requires several steps | |
5402 | * and can take a significant amount of time. As a result we use | |
5403 | * the spa_vdev_config_[enter/exit] functions which allow us to | |
5404 | * grab and release the spa_config_lock while still holding the namespace | |
5405 | * lock. During each step the configuration is synced out. | |
5406 | * | |
5407 | * Currently, this supports removing only hot spares, slogs, and level 2 ARC | |
5408 | * devices. | |
5409 | */ | |
5410 | int | |
5411 | spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) | |
5412 | { | |
5413 | vdev_t *vd; | |
5414 | metaslab_group_t *mg; | |
5415 | nvlist_t **spares, **l2cache, *nv; | |
5416 | uint64_t txg = 0; | |
5417 | uint_t nspares, nl2cache; | |
5418 | int error = 0; | |
5419 | boolean_t locked = MUTEX_HELD(&spa_namespace_lock); | |
5420 | ||
5421 | ASSERT(spa_writeable(spa)); | |
5422 | ||
5423 | if (!locked) | |
5424 | txg = spa_vdev_enter(spa); | |
5425 | ||
5426 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
5427 | ||
5428 | if (spa->spa_spares.sav_vdevs != NULL && | |
5429 | nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
5430 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && | |
5431 | (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { | |
5432 | /* | |
5433 | * Only remove the hot spare if it's not currently in use | |
5434 | * in this pool. | |
5435 | */ | |
5436 | if (vd == NULL || unspare) { | |
5437 | spa_vdev_remove_aux(spa->spa_spares.sav_config, | |
5438 | ZPOOL_CONFIG_SPARES, spares, nspares, nv); | |
5439 | spa_load_spares(spa); | |
5440 | spa->spa_spares.sav_sync = B_TRUE; | |
5441 | } else { | |
5442 | error = SET_ERROR(EBUSY); | |
5443 | } | |
5444 | spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX); | |
5445 | } else if (spa->spa_l2cache.sav_vdevs != NULL && | |
5446 | nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
5447 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && | |
5448 | (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { | |
5449 | /* | |
5450 | * Cache devices can always be removed. | |
5451 | */ | |
5452 | spa_vdev_remove_aux(spa->spa_l2cache.sav_config, | |
5453 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); | |
5454 | spa_load_l2cache(spa); | |
5455 | spa->spa_l2cache.sav_sync = B_TRUE; | |
5456 | spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX); | |
5457 | } else if (vd != NULL && vd->vdev_islog) { | |
5458 | ASSERT(!locked); | |
5459 | ASSERT(vd == vd->vdev_top); | |
5460 | ||
5461 | mg = vd->vdev_mg; | |
5462 | ||
5463 | /* | |
5464 | * Stop allocating from this vdev. | |
5465 | */ | |
5466 | metaslab_group_passivate(mg); | |
5467 | ||
5468 | /* | |
5469 | * Wait for the youngest allocations and frees to sync, | |
5470 | * and then wait for the deferral of those frees to finish. | |
5471 | */ | |
5472 | spa_vdev_config_exit(spa, NULL, | |
5473 | txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); | |
5474 | ||
5475 | /* | |
5476 | * Attempt to evacuate the vdev. | |
5477 | */ | |
5478 | error = spa_vdev_remove_evacuate(spa, vd); | |
5479 | ||
5480 | txg = spa_vdev_config_enter(spa); | |
5481 | ||
5482 | /* | |
5483 | * If we couldn't evacuate the vdev, unwind. | |
5484 | */ | |
5485 | if (error) { | |
5486 | metaslab_group_activate(mg); | |
5487 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
5488 | } | |
5489 | ||
5490 | /* | |
5491 | * Clean up the vdev namespace. | |
5492 | */ | |
5493 | spa_vdev_remove_from_namespace(spa, vd); | |
5494 | ||
5495 | spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE_DEV); | |
5496 | } else if (vd != NULL) { | |
5497 | /* | |
5498 | * Normal vdevs cannot be removed (yet). | |
5499 | */ | |
5500 | error = SET_ERROR(ENOTSUP); | |
5501 | } else { | |
5502 | /* | |
5503 | * There is no vdev of any kind with the specified guid. | |
5504 | */ | |
5505 | error = SET_ERROR(ENOENT); | |
5506 | } | |
5507 | ||
5508 | if (!locked) | |
5509 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
5510 | ||
5511 | return (error); | |
5512 | } | |
5513 | ||
5514 | /* | |
5515 | * Find any device that's done replacing, or a vdev marked 'unspare' that's | |
5516 | * currently spared, so we can detach it. | |
5517 | */ | |
5518 | static vdev_t * | |
5519 | spa_vdev_resilver_done_hunt(vdev_t *vd) | |
5520 | { | |
5521 | vdev_t *newvd, *oldvd; | |
5522 | int c; | |
5523 | ||
5524 | for (c = 0; c < vd->vdev_children; c++) { | |
5525 | oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); | |
5526 | if (oldvd != NULL) | |
5527 | return (oldvd); | |
5528 | } | |
5529 | ||
5530 | /* | |
5531 | * Check for a completed replacement. We always consider the first | |
5532 | * vdev in the list to be the oldest vdev, and the last one to be | |
5533 | * the newest (see spa_vdev_attach() for how that works). In | |
5534 | * the case where the newest vdev is faulted, we will not automatically | |
5535 | * remove it after a resilver completes. This is OK as it will require | |
5536 | * user intervention to determine which disk the admin wishes to keep. | |
5537 | */ | |
5538 | if (vd->vdev_ops == &vdev_replacing_ops) { | |
5539 | ASSERT(vd->vdev_children > 1); | |
5540 | ||
5541 | newvd = vd->vdev_child[vd->vdev_children - 1]; | |
5542 | oldvd = vd->vdev_child[0]; | |
5543 | ||
5544 | if (vdev_dtl_empty(newvd, DTL_MISSING) && | |
5545 | vdev_dtl_empty(newvd, DTL_OUTAGE) && | |
5546 | !vdev_dtl_required(oldvd)) | |
5547 | return (oldvd); | |
5548 | } | |
5549 | ||
5550 | /* | |
5551 | * Check for a completed resilver with the 'unspare' flag set. | |
5552 | */ | |
5553 | if (vd->vdev_ops == &vdev_spare_ops) { | |
5554 | vdev_t *first = vd->vdev_child[0]; | |
5555 | vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; | |
5556 | ||
5557 | if (last->vdev_unspare) { | |
5558 | oldvd = first; | |
5559 | newvd = last; | |
5560 | } else if (first->vdev_unspare) { | |
5561 | oldvd = last; | |
5562 | newvd = first; | |
5563 | } else { | |
5564 | oldvd = NULL; | |
5565 | } | |
5566 | ||
5567 | if (oldvd != NULL && | |
5568 | vdev_dtl_empty(newvd, DTL_MISSING) && | |
5569 | vdev_dtl_empty(newvd, DTL_OUTAGE) && | |
5570 | !vdev_dtl_required(oldvd)) | |
5571 | return (oldvd); | |
5572 | ||
5573 | /* | |
5574 | * If there are more than two spares attached to a disk, | |
5575 | * and those spares are not required, then we want to | |
5576 | * attempt to free them up now so that they can be used | |
5577 | * by other pools. Once we're back down to a single | |
5578 | * disk+spare, we stop removing them. | |
5579 | */ | |
5580 | if (vd->vdev_children > 2) { | |
5581 | newvd = vd->vdev_child[1]; | |
5582 | ||
5583 | if (newvd->vdev_isspare && last->vdev_isspare && | |
5584 | vdev_dtl_empty(last, DTL_MISSING) && | |
5585 | vdev_dtl_empty(last, DTL_OUTAGE) && | |
5586 | !vdev_dtl_required(newvd)) | |
5587 | return (newvd); | |
5588 | } | |
5589 | } | |
5590 | ||
5591 | return (NULL); | |
5592 | } | |
5593 | ||
5594 | static void | |
5595 | spa_vdev_resilver_done(spa_t *spa) | |
5596 | { | |
5597 | vdev_t *vd, *pvd, *ppvd; | |
5598 | uint64_t guid, sguid, pguid, ppguid; | |
5599 | ||
5600 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
5601 | ||
5602 | while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { | |
5603 | pvd = vd->vdev_parent; | |
5604 | ppvd = pvd->vdev_parent; | |
5605 | guid = vd->vdev_guid; | |
5606 | pguid = pvd->vdev_guid; | |
5607 | ppguid = ppvd->vdev_guid; | |
5608 | sguid = 0; | |
5609 | /* | |
5610 | * If we have just finished replacing a hot spared device, then | |
5611 | * we need to detach the parent's first child (the original hot | |
5612 | * spare) as well. | |
5613 | */ | |
5614 | if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && | |
5615 | ppvd->vdev_children == 2) { | |
5616 | ASSERT(pvd->vdev_ops == &vdev_replacing_ops); | |
5617 | sguid = ppvd->vdev_child[1]->vdev_guid; | |
5618 | } | |
5619 | ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); | |
5620 | ||
5621 | spa_config_exit(spa, SCL_ALL, FTAG); | |
5622 | if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) | |
5623 | return; | |
5624 | if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) | |
5625 | return; | |
5626 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
5627 | } | |
5628 | ||
5629 | spa_config_exit(spa, SCL_ALL, FTAG); | |
5630 | } | |
5631 | ||
5632 | /* | |
5633 | * Update the stored path or FRU for this vdev. | |
5634 | */ | |
5635 | int | |
5636 | spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, | |
5637 | boolean_t ispath) | |
5638 | { | |
5639 | vdev_t *vd; | |
5640 | boolean_t sync = B_FALSE; | |
5641 | ||
5642 | ASSERT(spa_writeable(spa)); | |
5643 | ||
5644 | spa_vdev_state_enter(spa, SCL_ALL); | |
5645 | ||
5646 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) | |
5647 | return (spa_vdev_state_exit(spa, NULL, ENOENT)); | |
5648 | ||
5649 | if (!vd->vdev_ops->vdev_op_leaf) | |
5650 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); | |
5651 | ||
5652 | if (ispath) { | |
5653 | if (strcmp(value, vd->vdev_path) != 0) { | |
5654 | spa_strfree(vd->vdev_path); | |
5655 | vd->vdev_path = spa_strdup(value); | |
5656 | sync = B_TRUE; | |
5657 | } | |
5658 | } else { | |
5659 | if (vd->vdev_fru == NULL) { | |
5660 | vd->vdev_fru = spa_strdup(value); | |
5661 | sync = B_TRUE; | |
5662 | } else if (strcmp(value, vd->vdev_fru) != 0) { | |
5663 | spa_strfree(vd->vdev_fru); | |
5664 | vd->vdev_fru = spa_strdup(value); | |
5665 | sync = B_TRUE; | |
5666 | } | |
5667 | } | |
5668 | ||
5669 | return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); | |
5670 | } | |
5671 | ||
5672 | int | |
5673 | spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) | |
5674 | { | |
5675 | return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); | |
5676 | } | |
5677 | ||
5678 | int | |
5679 | spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) | |
5680 | { | |
5681 | return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); | |
5682 | } | |
5683 | ||
5684 | /* | |
5685 | * ========================================================================== | |
5686 | * SPA Scanning | |
5687 | * ========================================================================== | |
5688 | */ | |
5689 | ||
5690 | int | |
5691 | spa_scan_stop(spa_t *spa) | |
5692 | { | |
5693 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
5694 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
5695 | return (SET_ERROR(EBUSY)); | |
5696 | return (dsl_scan_cancel(spa->spa_dsl_pool)); | |
5697 | } | |
5698 | ||
5699 | int | |
5700 | spa_scan(spa_t *spa, pool_scan_func_t func) | |
5701 | { | |
5702 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
5703 | ||
5704 | if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) | |
5705 | return (SET_ERROR(ENOTSUP)); | |
5706 | ||
5707 | /* | |
5708 | * If a resilver was requested, but there is no DTL on a | |
5709 | * writeable leaf device, we have nothing to do. | |
5710 | */ | |
5711 | if (func == POOL_SCAN_RESILVER && | |
5712 | !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { | |
5713 | spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); | |
5714 | return (0); | |
5715 | } | |
5716 | ||
5717 | return (dsl_scan(spa->spa_dsl_pool, func)); | |
5718 | } | |
5719 | ||
5720 | /* | |
5721 | * ========================================================================== | |
5722 | * SPA async task processing | |
5723 | * ========================================================================== | |
5724 | */ | |
5725 | ||
5726 | static void | |
5727 | spa_async_remove(spa_t *spa, vdev_t *vd) | |
5728 | { | |
5729 | int c; | |
5730 | ||
5731 | if (vd->vdev_remove_wanted) { | |
5732 | vd->vdev_remove_wanted = B_FALSE; | |
5733 | vd->vdev_delayed_close = B_FALSE; | |
5734 | vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); | |
5735 | ||
5736 | /* | |
5737 | * We want to clear the stats, but we don't want to do a full | |
5738 | * vdev_clear() as that will cause us to throw away | |
5739 | * degraded/faulted state as well as attempt to reopen the | |
5740 | * device, all of which is a waste. | |
5741 | */ | |
5742 | vd->vdev_stat.vs_read_errors = 0; | |
5743 | vd->vdev_stat.vs_write_errors = 0; | |
5744 | vd->vdev_stat.vs_checksum_errors = 0; | |
5745 | ||
5746 | vdev_state_dirty(vd->vdev_top); | |
5747 | } | |
5748 | ||
5749 | for (c = 0; c < vd->vdev_children; c++) | |
5750 | spa_async_remove(spa, vd->vdev_child[c]); | |
5751 | } | |
5752 | ||
5753 | static void | |
5754 | spa_async_probe(spa_t *spa, vdev_t *vd) | |
5755 | { | |
5756 | int c; | |
5757 | ||
5758 | if (vd->vdev_probe_wanted) { | |
5759 | vd->vdev_probe_wanted = B_FALSE; | |
5760 | vdev_reopen(vd); /* vdev_open() does the actual probe */ | |
5761 | } | |
5762 | ||
5763 | for (c = 0; c < vd->vdev_children; c++) | |
5764 | spa_async_probe(spa, vd->vdev_child[c]); | |
5765 | } | |
5766 | ||
5767 | static void | |
5768 | spa_async_autoexpand(spa_t *spa, vdev_t *vd) | |
5769 | { | |
5770 | int c; | |
5771 | ||
5772 | if (!spa->spa_autoexpand) | |
5773 | return; | |
5774 | ||
5775 | for (c = 0; c < vd->vdev_children; c++) { | |
5776 | vdev_t *cvd = vd->vdev_child[c]; | |
5777 | spa_async_autoexpand(spa, cvd); | |
5778 | } | |
5779 | ||
5780 | if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) | |
5781 | return; | |
5782 | ||
5783 | spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_AUTOEXPAND); | |
5784 | } | |
5785 | ||
5786 | static void | |
5787 | spa_async_thread(spa_t *spa) | |
5788 | { | |
5789 | int tasks, i; | |
5790 | ||
5791 | ASSERT(spa->spa_sync_on); | |
5792 | ||
5793 | mutex_enter(&spa->spa_async_lock); | |
5794 | tasks = spa->spa_async_tasks; | |
5795 | spa->spa_async_tasks = 0; | |
5796 | mutex_exit(&spa->spa_async_lock); | |
5797 | ||
5798 | /* | |
5799 | * See if the config needs to be updated. | |
5800 | */ | |
5801 | if (tasks & SPA_ASYNC_CONFIG_UPDATE) { | |
5802 | uint64_t old_space, new_space; | |
5803 | ||
5804 | mutex_enter(&spa_namespace_lock); | |
5805 | old_space = metaslab_class_get_space(spa_normal_class(spa)); | |
5806 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
5807 | new_space = metaslab_class_get_space(spa_normal_class(spa)); | |
5808 | mutex_exit(&spa_namespace_lock); | |
5809 | ||
5810 | /* | |
5811 | * If the pool grew as a result of the config update, | |
5812 | * then log an internal history event. | |
5813 | */ | |
5814 | if (new_space != old_space) { | |
5815 | spa_history_log_internal(spa, "vdev online", NULL, | |
5816 | "pool '%s' size: %llu(+%llu)", | |
5817 | spa_name(spa), new_space, new_space - old_space); | |
5818 | } | |
5819 | } | |
5820 | ||
5821 | /* | |
5822 | * See if any devices need to be marked REMOVED. | |
5823 | */ | |
5824 | if (tasks & SPA_ASYNC_REMOVE) { | |
5825 | spa_vdev_state_enter(spa, SCL_NONE); | |
5826 | spa_async_remove(spa, spa->spa_root_vdev); | |
5827 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) | |
5828 | spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); | |
5829 | for (i = 0; i < spa->spa_spares.sav_count; i++) | |
5830 | spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); | |
5831 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
5832 | } | |
5833 | ||
5834 | if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { | |
5835 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
5836 | spa_async_autoexpand(spa, spa->spa_root_vdev); | |
5837 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
5838 | } | |
5839 | ||
5840 | /* | |
5841 | * See if any devices need to be probed. | |
5842 | */ | |
5843 | if (tasks & SPA_ASYNC_PROBE) { | |
5844 | spa_vdev_state_enter(spa, SCL_NONE); | |
5845 | spa_async_probe(spa, spa->spa_root_vdev); | |
5846 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
5847 | } | |
5848 | ||
5849 | /* | |
5850 | * If any devices are done replacing, detach them. | |
5851 | */ | |
5852 | if (tasks & SPA_ASYNC_RESILVER_DONE) | |
5853 | spa_vdev_resilver_done(spa); | |
5854 | ||
5855 | /* | |
5856 | * Kick off a resilver. | |
5857 | */ | |
5858 | if (tasks & SPA_ASYNC_RESILVER) | |
5859 | dsl_resilver_restart(spa->spa_dsl_pool, 0); | |
5860 | ||
5861 | /* | |
5862 | * Let the world know that we're done. | |
5863 | */ | |
5864 | mutex_enter(&spa->spa_async_lock); | |
5865 | spa->spa_async_thread = NULL; | |
5866 | cv_broadcast(&spa->spa_async_cv); | |
5867 | mutex_exit(&spa->spa_async_lock); | |
5868 | thread_exit(); | |
5869 | } | |
5870 | ||
5871 | void | |
5872 | spa_async_suspend(spa_t *spa) | |
5873 | { | |
5874 | mutex_enter(&spa->spa_async_lock); | |
5875 | spa->spa_async_suspended++; | |
5876 | while (spa->spa_async_thread != NULL) | |
5877 | cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); | |
5878 | mutex_exit(&spa->spa_async_lock); | |
5879 | } | |
5880 | ||
5881 | void | |
5882 | spa_async_resume(spa_t *spa) | |
5883 | { | |
5884 | mutex_enter(&spa->spa_async_lock); | |
5885 | ASSERT(spa->spa_async_suspended != 0); | |
5886 | spa->spa_async_suspended--; | |
5887 | mutex_exit(&spa->spa_async_lock); | |
5888 | } | |
5889 | ||
5890 | static boolean_t | |
5891 | spa_async_tasks_pending(spa_t *spa) | |
5892 | { | |
5893 | uint_t non_config_tasks; | |
5894 | uint_t config_task; | |
5895 | boolean_t config_task_suspended; | |
5896 | ||
5897 | non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; | |
5898 | config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; | |
5899 | if (spa->spa_ccw_fail_time == 0) { | |
5900 | config_task_suspended = B_FALSE; | |
5901 | } else { | |
5902 | config_task_suspended = | |
5903 | (gethrtime() - spa->spa_ccw_fail_time) < | |
5904 | (zfs_ccw_retry_interval * NANOSEC); | |
5905 | } | |
5906 | ||
5907 | return (non_config_tasks || (config_task && !config_task_suspended)); | |
5908 | } | |
5909 | ||
5910 | static void | |
5911 | spa_async_dispatch(spa_t *spa) | |
5912 | { | |
5913 | mutex_enter(&spa->spa_async_lock); | |
5914 | if (spa_async_tasks_pending(spa) && | |
5915 | !spa->spa_async_suspended && | |
5916 | spa->spa_async_thread == NULL && | |
5917 | rootdir != NULL) | |
5918 | spa->spa_async_thread = thread_create(NULL, 0, | |
5919 | spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); | |
5920 | mutex_exit(&spa->spa_async_lock); | |
5921 | } | |
5922 | ||
5923 | void | |
5924 | spa_async_request(spa_t *spa, int task) | |
5925 | { | |
5926 | zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); | |
5927 | mutex_enter(&spa->spa_async_lock); | |
5928 | spa->spa_async_tasks |= task; | |
5929 | mutex_exit(&spa->spa_async_lock); | |
5930 | } | |
5931 | ||
5932 | /* | |
5933 | * ========================================================================== | |
5934 | * SPA syncing routines | |
5935 | * ========================================================================== | |
5936 | */ | |
5937 | ||
5938 | static int | |
5939 | bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
5940 | { | |
5941 | bpobj_t *bpo = arg; | |
5942 | bpobj_enqueue(bpo, bp, tx); | |
5943 | return (0); | |
5944 | } | |
5945 | ||
5946 | static int | |
5947 | spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
5948 | { | |
5949 | zio_t *zio = arg; | |
5950 | ||
5951 | zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, | |
5952 | zio->io_flags)); | |
5953 | return (0); | |
5954 | } | |
5955 | ||
5956 | /* | |
5957 | * Note: this simple function is not inlined to make it easier to dtrace the | |
5958 | * amount of time spent syncing frees. | |
5959 | */ | |
5960 | static void | |
5961 | spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) | |
5962 | { | |
5963 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
5964 | bplist_iterate(bpl, spa_free_sync_cb, zio, tx); | |
5965 | VERIFY(zio_wait(zio) == 0); | |
5966 | } | |
5967 | ||
5968 | /* | |
5969 | * Note: this simple function is not inlined to make it easier to dtrace the | |
5970 | * amount of time spent syncing deferred frees. | |
5971 | */ | |
5972 | static void | |
5973 | spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) | |
5974 | { | |
5975 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
5976 | VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, | |
5977 | spa_free_sync_cb, zio, tx), ==, 0); | |
5978 | VERIFY0(zio_wait(zio)); | |
5979 | } | |
5980 | ||
5981 | static void | |
5982 | spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) | |
5983 | { | |
5984 | char *packed = NULL; | |
5985 | size_t bufsize; | |
5986 | size_t nvsize = 0; | |
5987 | dmu_buf_t *db; | |
5988 | ||
5989 | VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); | |
5990 | ||
5991 | /* | |
5992 | * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration | |
5993 | * information. This avoids the dmu_buf_will_dirty() path and | |
5994 | * saves us a pre-read to get data we don't actually care about. | |
5995 | */ | |
5996 | bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); | |
5997 | packed = vmem_alloc(bufsize, KM_SLEEP); | |
5998 | ||
5999 | VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, | |
6000 | KM_SLEEP) == 0); | |
6001 | bzero(packed + nvsize, bufsize - nvsize); | |
6002 | ||
6003 | dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); | |
6004 | ||
6005 | vmem_free(packed, bufsize); | |
6006 | ||
6007 | VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); | |
6008 | dmu_buf_will_dirty(db, tx); | |
6009 | *(uint64_t *)db->db_data = nvsize; | |
6010 | dmu_buf_rele(db, FTAG); | |
6011 | } | |
6012 | ||
6013 | static void | |
6014 | spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, | |
6015 | const char *config, const char *entry) | |
6016 | { | |
6017 | nvlist_t *nvroot; | |
6018 | nvlist_t **list; | |
6019 | int i; | |
6020 | ||
6021 | if (!sav->sav_sync) | |
6022 | return; | |
6023 | ||
6024 | /* | |
6025 | * Update the MOS nvlist describing the list of available devices. | |
6026 | * spa_validate_aux() will have already made sure this nvlist is | |
6027 | * valid and the vdevs are labeled appropriately. | |
6028 | */ | |
6029 | if (sav->sav_object == 0) { | |
6030 | sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, | |
6031 | DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, | |
6032 | sizeof (uint64_t), tx); | |
6033 | VERIFY(zap_update(spa->spa_meta_objset, | |
6034 | DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, | |
6035 | &sav->sav_object, tx) == 0); | |
6036 | } | |
6037 | ||
6038 | VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
6039 | if (sav->sav_count == 0) { | |
6040 | VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); | |
6041 | } else { | |
6042 | list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP); | |
6043 | for (i = 0; i < sav->sav_count; i++) | |
6044 | list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], | |
6045 | B_FALSE, VDEV_CONFIG_L2CACHE); | |
6046 | VERIFY(nvlist_add_nvlist_array(nvroot, config, list, | |
6047 | sav->sav_count) == 0); | |
6048 | for (i = 0; i < sav->sav_count; i++) | |
6049 | nvlist_free(list[i]); | |
6050 | kmem_free(list, sav->sav_count * sizeof (void *)); | |
6051 | } | |
6052 | ||
6053 | spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); | |
6054 | nvlist_free(nvroot); | |
6055 | ||
6056 | sav->sav_sync = B_FALSE; | |
6057 | } | |
6058 | ||
6059 | /* | |
6060 | * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. | |
6061 | * The all-vdev ZAP must be empty. | |
6062 | */ | |
6063 | static void | |
6064 | spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) | |
6065 | { | |
6066 | spa_t *spa = vd->vdev_spa; | |
6067 | uint64_t i; | |
6068 | ||
6069 | if (vd->vdev_top_zap != 0) { | |
6070 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
6071 | vd->vdev_top_zap, tx)); | |
6072 | } | |
6073 | if (vd->vdev_leaf_zap != 0) { | |
6074 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
6075 | vd->vdev_leaf_zap, tx)); | |
6076 | } | |
6077 | for (i = 0; i < vd->vdev_children; i++) { | |
6078 | spa_avz_build(vd->vdev_child[i], avz, tx); | |
6079 | } | |
6080 | } | |
6081 | ||
6082 | static void | |
6083 | spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) | |
6084 | { | |
6085 | nvlist_t *config; | |
6086 | ||
6087 | /* | |
6088 | * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, | |
6089 | * its config may not be dirty but we still need to build per-vdev ZAPs. | |
6090 | * Similarly, if the pool is being assembled (e.g. after a split), we | |
6091 | * need to rebuild the AVZ although the config may not be dirty. | |
6092 | */ | |
6093 | if (list_is_empty(&spa->spa_config_dirty_list) && | |
6094 | spa->spa_avz_action == AVZ_ACTION_NONE) | |
6095 | return; | |
6096 | ||
6097 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
6098 | ||
6099 | ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || | |
6100 | spa->spa_all_vdev_zaps != 0); | |
6101 | ||
6102 | if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { | |
6103 | zap_cursor_t zc; | |
6104 | zap_attribute_t za; | |
6105 | ||
6106 | /* Make and build the new AVZ */ | |
6107 | uint64_t new_avz = zap_create(spa->spa_meta_objset, | |
6108 | DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); | |
6109 | spa_avz_build(spa->spa_root_vdev, new_avz, tx); | |
6110 | ||
6111 | /* Diff old AVZ with new one */ | |
6112 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
6113 | spa->spa_all_vdev_zaps); | |
6114 | zap_cursor_retrieve(&zc, &za) == 0; | |
6115 | zap_cursor_advance(&zc)) { | |
6116 | uint64_t vdzap = za.za_first_integer; | |
6117 | if (zap_lookup_int(spa->spa_meta_objset, new_avz, | |
6118 | vdzap) == ENOENT) { | |
6119 | /* | |
6120 | * ZAP is listed in old AVZ but not in new one; | |
6121 | * destroy it | |
6122 | */ | |
6123 | VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, | |
6124 | tx)); | |
6125 | } | |
6126 | } | |
6127 | ||
6128 | zap_cursor_fini(&zc); | |
6129 | ||
6130 | /* Destroy the old AVZ */ | |
6131 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
6132 | spa->spa_all_vdev_zaps, tx)); | |
6133 | ||
6134 | /* Replace the old AVZ in the dir obj with the new one */ | |
6135 | VERIFY0(zap_update(spa->spa_meta_objset, | |
6136 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, | |
6137 | sizeof (new_avz), 1, &new_avz, tx)); | |
6138 | ||
6139 | spa->spa_all_vdev_zaps = new_avz; | |
6140 | } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { | |
6141 | zap_cursor_t zc; | |
6142 | zap_attribute_t za; | |
6143 | ||
6144 | /* Walk through the AVZ and destroy all listed ZAPs */ | |
6145 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
6146 | spa->spa_all_vdev_zaps); | |
6147 | zap_cursor_retrieve(&zc, &za) == 0; | |
6148 | zap_cursor_advance(&zc)) { | |
6149 | uint64_t zap = za.za_first_integer; | |
6150 | VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); | |
6151 | } | |
6152 | ||
6153 | zap_cursor_fini(&zc); | |
6154 | ||
6155 | /* Destroy and unlink the AVZ itself */ | |
6156 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
6157 | spa->spa_all_vdev_zaps, tx)); | |
6158 | VERIFY0(zap_remove(spa->spa_meta_objset, | |
6159 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); | |
6160 | spa->spa_all_vdev_zaps = 0; | |
6161 | } | |
6162 | ||
6163 | if (spa->spa_all_vdev_zaps == 0) { | |
6164 | spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, | |
6165 | DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, | |
6166 | DMU_POOL_VDEV_ZAP_MAP, tx); | |
6167 | } | |
6168 | spa->spa_avz_action = AVZ_ACTION_NONE; | |
6169 | ||
6170 | /* Create ZAPs for vdevs that don't have them. */ | |
6171 | vdev_construct_zaps(spa->spa_root_vdev, tx); | |
6172 | ||
6173 | config = spa_config_generate(spa, spa->spa_root_vdev, | |
6174 | dmu_tx_get_txg(tx), B_FALSE); | |
6175 | ||
6176 | /* | |
6177 | * If we're upgrading the spa version then make sure that | |
6178 | * the config object gets updated with the correct version. | |
6179 | */ | |
6180 | if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) | |
6181 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
6182 | spa->spa_uberblock.ub_version); | |
6183 | ||
6184 | spa_config_exit(spa, SCL_STATE, FTAG); | |
6185 | ||
6186 | nvlist_free(spa->spa_config_syncing); | |
6187 | spa->spa_config_syncing = config; | |
6188 | ||
6189 | spa_sync_nvlist(spa, spa->spa_config_object, config, tx); | |
6190 | } | |
6191 | ||
6192 | static void | |
6193 | spa_sync_version(void *arg, dmu_tx_t *tx) | |
6194 | { | |
6195 | uint64_t *versionp = arg; | |
6196 | uint64_t version = *versionp; | |
6197 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
6198 | ||
6199 | /* | |
6200 | * Setting the version is special cased when first creating the pool. | |
6201 | */ | |
6202 | ASSERT(tx->tx_txg != TXG_INITIAL); | |
6203 | ||
6204 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
6205 | ASSERT(version >= spa_version(spa)); | |
6206 | ||
6207 | spa->spa_uberblock.ub_version = version; | |
6208 | vdev_config_dirty(spa->spa_root_vdev); | |
6209 | spa_history_log_internal(spa, "set", tx, "version=%lld", version); | |
6210 | } | |
6211 | ||
6212 | /* | |
6213 | * Set zpool properties. | |
6214 | */ | |
6215 | static void | |
6216 | spa_sync_props(void *arg, dmu_tx_t *tx) | |
6217 | { | |
6218 | nvlist_t *nvp = arg; | |
6219 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
6220 | objset_t *mos = spa->spa_meta_objset; | |
6221 | nvpair_t *elem = NULL; | |
6222 | ||
6223 | mutex_enter(&spa->spa_props_lock); | |
6224 | ||
6225 | while ((elem = nvlist_next_nvpair(nvp, elem))) { | |
6226 | uint64_t intval; | |
6227 | char *strval, *fname; | |
6228 | zpool_prop_t prop; | |
6229 | const char *propname; | |
6230 | zprop_type_t proptype; | |
6231 | spa_feature_t fid; | |
6232 | ||
6233 | prop = zpool_name_to_prop(nvpair_name(elem)); | |
6234 | switch ((int)prop) { | |
6235 | case ZPROP_INVAL: | |
6236 | /* | |
6237 | * We checked this earlier in spa_prop_validate(). | |
6238 | */ | |
6239 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
6240 | ||
6241 | fname = strchr(nvpair_name(elem), '@') + 1; | |
6242 | VERIFY0(zfeature_lookup_name(fname, &fid)); | |
6243 | ||
6244 | spa_feature_enable(spa, fid, tx); | |
6245 | spa_history_log_internal(spa, "set", tx, | |
6246 | "%s=enabled", nvpair_name(elem)); | |
6247 | break; | |
6248 | ||
6249 | case ZPOOL_PROP_VERSION: | |
6250 | intval = fnvpair_value_uint64(elem); | |
6251 | /* | |
6252 | * The version is synced seperatly before other | |
6253 | * properties and should be correct by now. | |
6254 | */ | |
6255 | ASSERT3U(spa_version(spa), >=, intval); | |
6256 | break; | |
6257 | ||
6258 | case ZPOOL_PROP_ALTROOT: | |
6259 | /* | |
6260 | * 'altroot' is a non-persistent property. It should | |
6261 | * have been set temporarily at creation or import time. | |
6262 | */ | |
6263 | ASSERT(spa->spa_root != NULL); | |
6264 | break; | |
6265 | ||
6266 | case ZPOOL_PROP_READONLY: | |
6267 | case ZPOOL_PROP_CACHEFILE: | |
6268 | /* | |
6269 | * 'readonly' and 'cachefile' are also non-persisitent | |
6270 | * properties. | |
6271 | */ | |
6272 | break; | |
6273 | case ZPOOL_PROP_COMMENT: | |
6274 | strval = fnvpair_value_string(elem); | |
6275 | if (spa->spa_comment != NULL) | |
6276 | spa_strfree(spa->spa_comment); | |
6277 | spa->spa_comment = spa_strdup(strval); | |
6278 | /* | |
6279 | * We need to dirty the configuration on all the vdevs | |
6280 | * so that their labels get updated. It's unnecessary | |
6281 | * to do this for pool creation since the vdev's | |
6282 | * configuratoin has already been dirtied. | |
6283 | */ | |
6284 | if (tx->tx_txg != TXG_INITIAL) | |
6285 | vdev_config_dirty(spa->spa_root_vdev); | |
6286 | spa_history_log_internal(spa, "set", tx, | |
6287 | "%s=%s", nvpair_name(elem), strval); | |
6288 | break; | |
6289 | default: | |
6290 | /* | |
6291 | * Set pool property values in the poolprops mos object. | |
6292 | */ | |
6293 | if (spa->spa_pool_props_object == 0) { | |
6294 | spa->spa_pool_props_object = | |
6295 | zap_create_link(mos, DMU_OT_POOL_PROPS, | |
6296 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, | |
6297 | tx); | |
6298 | } | |
6299 | ||
6300 | /* normalize the property name */ | |
6301 | propname = zpool_prop_to_name(prop); | |
6302 | proptype = zpool_prop_get_type(prop); | |
6303 | ||
6304 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
6305 | ASSERT(proptype == PROP_TYPE_STRING); | |
6306 | strval = fnvpair_value_string(elem); | |
6307 | VERIFY0(zap_update(mos, | |
6308 | spa->spa_pool_props_object, propname, | |
6309 | 1, strlen(strval) + 1, strval, tx)); | |
6310 | spa_history_log_internal(spa, "set", tx, | |
6311 | "%s=%s", nvpair_name(elem), strval); | |
6312 | } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { | |
6313 | intval = fnvpair_value_uint64(elem); | |
6314 | ||
6315 | if (proptype == PROP_TYPE_INDEX) { | |
6316 | const char *unused; | |
6317 | VERIFY0(zpool_prop_index_to_string( | |
6318 | prop, intval, &unused)); | |
6319 | } | |
6320 | VERIFY0(zap_update(mos, | |
6321 | spa->spa_pool_props_object, propname, | |
6322 | 8, 1, &intval, tx)); | |
6323 | spa_history_log_internal(spa, "set", tx, | |
6324 | "%s=%lld", nvpair_name(elem), intval); | |
6325 | } else { | |
6326 | ASSERT(0); /* not allowed */ | |
6327 | } | |
6328 | ||
6329 | switch (prop) { | |
6330 | case ZPOOL_PROP_DELEGATION: | |
6331 | spa->spa_delegation = intval; | |
6332 | break; | |
6333 | case ZPOOL_PROP_BOOTFS: | |
6334 | spa->spa_bootfs = intval; | |
6335 | break; | |
6336 | case ZPOOL_PROP_FAILUREMODE: | |
6337 | spa->spa_failmode = intval; | |
6338 | break; | |
6339 | case ZPOOL_PROP_AUTOEXPAND: | |
6340 | spa->spa_autoexpand = intval; | |
6341 | if (tx->tx_txg != TXG_INITIAL) | |
6342 | spa_async_request(spa, | |
6343 | SPA_ASYNC_AUTOEXPAND); | |
6344 | break; | |
6345 | case ZPOOL_PROP_DEDUPDITTO: | |
6346 | spa->spa_dedup_ditto = intval; | |
6347 | break; | |
6348 | default: | |
6349 | break; | |
6350 | } | |
6351 | } | |
6352 | ||
6353 | } | |
6354 | ||
6355 | mutex_exit(&spa->spa_props_lock); | |
6356 | } | |
6357 | ||
6358 | /* | |
6359 | * Perform one-time upgrade on-disk changes. spa_version() does not | |
6360 | * reflect the new version this txg, so there must be no changes this | |
6361 | * txg to anything that the upgrade code depends on after it executes. | |
6362 | * Therefore this must be called after dsl_pool_sync() does the sync | |
6363 | * tasks. | |
6364 | */ | |
6365 | static void | |
6366 | spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) | |
6367 | { | |
6368 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
6369 | ||
6370 | ASSERT(spa->spa_sync_pass == 1); | |
6371 | ||
6372 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); | |
6373 | ||
6374 | if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && | |
6375 | spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { | |
6376 | dsl_pool_create_origin(dp, tx); | |
6377 | ||
6378 | /* Keeping the origin open increases spa_minref */ | |
6379 | spa->spa_minref += 3; | |
6380 | } | |
6381 | ||
6382 | if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && | |
6383 | spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { | |
6384 | dsl_pool_upgrade_clones(dp, tx); | |
6385 | } | |
6386 | ||
6387 | if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && | |
6388 | spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { | |
6389 | dsl_pool_upgrade_dir_clones(dp, tx); | |
6390 | ||
6391 | /* Keeping the freedir open increases spa_minref */ | |
6392 | spa->spa_minref += 3; | |
6393 | } | |
6394 | ||
6395 | if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && | |
6396 | spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
6397 | spa_feature_create_zap_objects(spa, tx); | |
6398 | } | |
6399 | ||
6400 | /* | |
6401 | * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable | |
6402 | * when possibility to use lz4 compression for metadata was added | |
6403 | * Old pools that have this feature enabled must be upgraded to have | |
6404 | * this feature active | |
6405 | */ | |
6406 | if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
6407 | boolean_t lz4_en = spa_feature_is_enabled(spa, | |
6408 | SPA_FEATURE_LZ4_COMPRESS); | |
6409 | boolean_t lz4_ac = spa_feature_is_active(spa, | |
6410 | SPA_FEATURE_LZ4_COMPRESS); | |
6411 | ||
6412 | if (lz4_en && !lz4_ac) | |
6413 | spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); | |
6414 | } | |
6415 | rrw_exit(&dp->dp_config_rwlock, FTAG); | |
6416 | } | |
6417 | ||
6418 | /* | |
6419 | * Sync the specified transaction group. New blocks may be dirtied as | |
6420 | * part of the process, so we iterate until it converges. | |
6421 | */ | |
6422 | void | |
6423 | spa_sync(spa_t *spa, uint64_t txg) | |
6424 | { | |
6425 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
6426 | objset_t *mos = spa->spa_meta_objset; | |
6427 | bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; | |
6428 | vdev_t *rvd = spa->spa_root_vdev; | |
6429 | vdev_t *vd; | |
6430 | dmu_tx_t *tx; | |
6431 | int error; | |
6432 | int c; | |
6433 | ||
6434 | VERIFY(spa_writeable(spa)); | |
6435 | ||
6436 | /* | |
6437 | * Lock out configuration changes. | |
6438 | */ | |
6439 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
6440 | ||
6441 | spa->spa_syncing_txg = txg; | |
6442 | spa->spa_sync_pass = 0; | |
6443 | ||
6444 | /* | |
6445 | * If there are any pending vdev state changes, convert them | |
6446 | * into config changes that go out with this transaction group. | |
6447 | */ | |
6448 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
6449 | while (list_head(&spa->spa_state_dirty_list) != NULL) { | |
6450 | /* | |
6451 | * We need the write lock here because, for aux vdevs, | |
6452 | * calling vdev_config_dirty() modifies sav_config. | |
6453 | * This is ugly and will become unnecessary when we | |
6454 | * eliminate the aux vdev wart by integrating all vdevs | |
6455 | * into the root vdev tree. | |
6456 | */ | |
6457 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
6458 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); | |
6459 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { | |
6460 | vdev_state_clean(vd); | |
6461 | vdev_config_dirty(vd); | |
6462 | } | |
6463 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
6464 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
6465 | } | |
6466 | spa_config_exit(spa, SCL_STATE, FTAG); | |
6467 | ||
6468 | tx = dmu_tx_create_assigned(dp, txg); | |
6469 | ||
6470 | spa->spa_sync_starttime = gethrtime(); | |
6471 | taskq_cancel_id(system_taskq, spa->spa_deadman_tqid); | |
6472 | spa->spa_deadman_tqid = taskq_dispatch_delay(system_taskq, | |
6473 | spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + | |
6474 | NSEC_TO_TICK(spa->spa_deadman_synctime)); | |
6475 | ||
6476 | /* | |
6477 | * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, | |
6478 | * set spa_deflate if we have no raid-z vdevs. | |
6479 | */ | |
6480 | if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && | |
6481 | spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
6482 | int i; | |
6483 | ||
6484 | for (i = 0; i < rvd->vdev_children; i++) { | |
6485 | vd = rvd->vdev_child[i]; | |
6486 | if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) | |
6487 | break; | |
6488 | } | |
6489 | if (i == rvd->vdev_children) { | |
6490 | spa->spa_deflate = TRUE; | |
6491 | VERIFY(0 == zap_add(spa->spa_meta_objset, | |
6492 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
6493 | sizeof (uint64_t), 1, &spa->spa_deflate, tx)); | |
6494 | } | |
6495 | } | |
6496 | ||
6497 | /* | |
6498 | * Iterate to convergence. | |
6499 | */ | |
6500 | do { | |
6501 | int pass = ++spa->spa_sync_pass; | |
6502 | ||
6503 | spa_sync_config_object(spa, tx); | |
6504 | spa_sync_aux_dev(spa, &spa->spa_spares, tx, | |
6505 | ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); | |
6506 | spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, | |
6507 | ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); | |
6508 | spa_errlog_sync(spa, txg); | |
6509 | dsl_pool_sync(dp, txg); | |
6510 | ||
6511 | if (pass < zfs_sync_pass_deferred_free) { | |
6512 | spa_sync_frees(spa, free_bpl, tx); | |
6513 | } else { | |
6514 | /* | |
6515 | * We can not defer frees in pass 1, because | |
6516 | * we sync the deferred frees later in pass 1. | |
6517 | */ | |
6518 | ASSERT3U(pass, >, 1); | |
6519 | bplist_iterate(free_bpl, bpobj_enqueue_cb, | |
6520 | &spa->spa_deferred_bpobj, tx); | |
6521 | } | |
6522 | ||
6523 | ddt_sync(spa, txg); | |
6524 | dsl_scan_sync(dp, tx); | |
6525 | ||
6526 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))) | |
6527 | vdev_sync(vd, txg); | |
6528 | ||
6529 | if (pass == 1) { | |
6530 | spa_sync_upgrades(spa, tx); | |
6531 | ASSERT3U(txg, >=, | |
6532 | spa->spa_uberblock.ub_rootbp.blk_birth); | |
6533 | /* | |
6534 | * Note: We need to check if the MOS is dirty | |
6535 | * because we could have marked the MOS dirty | |
6536 | * without updating the uberblock (e.g. if we | |
6537 | * have sync tasks but no dirty user data). We | |
6538 | * need to check the uberblock's rootbp because | |
6539 | * it is updated if we have synced out dirty | |
6540 | * data (though in this case the MOS will most | |
6541 | * likely also be dirty due to second order | |
6542 | * effects, we don't want to rely on that here). | |
6543 | */ | |
6544 | if (spa->spa_uberblock.ub_rootbp.blk_birth < txg && | |
6545 | !dmu_objset_is_dirty(mos, txg)) { | |
6546 | /* | |
6547 | * Nothing changed on the first pass, | |
6548 | * therefore this TXG is a no-op. Avoid | |
6549 | * syncing deferred frees, so that we | |
6550 | * can keep this TXG as a no-op. | |
6551 | */ | |
6552 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, | |
6553 | txg)); | |
6554 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
6555 | ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); | |
6556 | break; | |
6557 | } | |
6558 | spa_sync_deferred_frees(spa, tx); | |
6559 | } | |
6560 | ||
6561 | } while (dmu_objset_is_dirty(mos, txg)); | |
6562 | ||
6563 | #ifdef ZFS_DEBUG | |
6564 | if (!list_is_empty(&spa->spa_config_dirty_list)) { | |
6565 | /* | |
6566 | * Make sure that the number of ZAPs for all the vdevs matches | |
6567 | * the number of ZAPs in the per-vdev ZAP list. This only gets | |
6568 | * called if the config is dirty; otherwise there may be | |
6569 | * outstanding AVZ operations that weren't completed in | |
6570 | * spa_sync_config_object. | |
6571 | */ | |
6572 | uint64_t all_vdev_zap_entry_count; | |
6573 | ASSERT0(zap_count(spa->spa_meta_objset, | |
6574 | spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); | |
6575 | ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, | |
6576 | all_vdev_zap_entry_count); | |
6577 | } | |
6578 | #endif | |
6579 | ||
6580 | /* | |
6581 | * Rewrite the vdev configuration (which includes the uberblock) | |
6582 | * to commit the transaction group. | |
6583 | * | |
6584 | * If there are no dirty vdevs, we sync the uberblock to a few | |
6585 | * random top-level vdevs that are known to be visible in the | |
6586 | * config cache (see spa_vdev_add() for a complete description). | |
6587 | * If there *are* dirty vdevs, sync the uberblock to all vdevs. | |
6588 | */ | |
6589 | for (;;) { | |
6590 | /* | |
6591 | * We hold SCL_STATE to prevent vdev open/close/etc. | |
6592 | * while we're attempting to write the vdev labels. | |
6593 | */ | |
6594 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
6595 | ||
6596 | if (list_is_empty(&spa->spa_config_dirty_list)) { | |
6597 | vdev_t *svd[SPA_DVAS_PER_BP]; | |
6598 | int svdcount = 0; | |
6599 | int children = rvd->vdev_children; | |
6600 | int c0 = spa_get_random(children); | |
6601 | ||
6602 | for (c = 0; c < children; c++) { | |
6603 | vd = rvd->vdev_child[(c0 + c) % children]; | |
6604 | if (vd->vdev_ms_array == 0 || vd->vdev_islog) | |
6605 | continue; | |
6606 | svd[svdcount++] = vd; | |
6607 | if (svdcount == SPA_DVAS_PER_BP) | |
6608 | break; | |
6609 | } | |
6610 | error = vdev_config_sync(svd, svdcount, txg); | |
6611 | } else { | |
6612 | error = vdev_config_sync(rvd->vdev_child, | |
6613 | rvd->vdev_children, txg); | |
6614 | } | |
6615 | ||
6616 | if (error == 0) | |
6617 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
6618 | ||
6619 | spa_config_exit(spa, SCL_STATE, FTAG); | |
6620 | ||
6621 | if (error == 0) | |
6622 | break; | |
6623 | zio_suspend(spa, NULL); | |
6624 | zio_resume_wait(spa); | |
6625 | } | |
6626 | dmu_tx_commit(tx); | |
6627 | ||
6628 | taskq_cancel_id(system_taskq, spa->spa_deadman_tqid); | |
6629 | spa->spa_deadman_tqid = 0; | |
6630 | ||
6631 | /* | |
6632 | * Clear the dirty config list. | |
6633 | */ | |
6634 | while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) | |
6635 | vdev_config_clean(vd); | |
6636 | ||
6637 | /* | |
6638 | * Now that the new config has synced transactionally, | |
6639 | * let it become visible to the config cache. | |
6640 | */ | |
6641 | if (spa->spa_config_syncing != NULL) { | |
6642 | spa_config_set(spa, spa->spa_config_syncing); | |
6643 | spa->spa_config_txg = txg; | |
6644 | spa->spa_config_syncing = NULL; | |
6645 | } | |
6646 | ||
6647 | spa->spa_ubsync = spa->spa_uberblock; | |
6648 | ||
6649 | dsl_pool_sync_done(dp, txg); | |
6650 | ||
6651 | /* | |
6652 | * Update usable space statistics. | |
6653 | */ | |
6654 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))) | |
6655 | vdev_sync_done(vd, txg); | |
6656 | ||
6657 | spa_update_dspace(spa); | |
6658 | ||
6659 | /* | |
6660 | * It had better be the case that we didn't dirty anything | |
6661 | * since vdev_config_sync(). | |
6662 | */ | |
6663 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); | |
6664 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
6665 | ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); | |
6666 | ||
6667 | spa->spa_sync_pass = 0; | |
6668 | ||
6669 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
6670 | ||
6671 | spa_handle_ignored_writes(spa); | |
6672 | ||
6673 | /* | |
6674 | * If any async tasks have been requested, kick them off. | |
6675 | */ | |
6676 | spa_async_dispatch(spa); | |
6677 | } | |
6678 | ||
6679 | /* | |
6680 | * Sync all pools. We don't want to hold the namespace lock across these | |
6681 | * operations, so we take a reference on the spa_t and drop the lock during the | |
6682 | * sync. | |
6683 | */ | |
6684 | void | |
6685 | spa_sync_allpools(void) | |
6686 | { | |
6687 | spa_t *spa = NULL; | |
6688 | mutex_enter(&spa_namespace_lock); | |
6689 | while ((spa = spa_next(spa)) != NULL) { | |
6690 | if (spa_state(spa) != POOL_STATE_ACTIVE || | |
6691 | !spa_writeable(spa) || spa_suspended(spa)) | |
6692 | continue; | |
6693 | spa_open_ref(spa, FTAG); | |
6694 | mutex_exit(&spa_namespace_lock); | |
6695 | txg_wait_synced(spa_get_dsl(spa), 0); | |
6696 | mutex_enter(&spa_namespace_lock); | |
6697 | spa_close(spa, FTAG); | |
6698 | } | |
6699 | mutex_exit(&spa_namespace_lock); | |
6700 | } | |
6701 | ||
6702 | /* | |
6703 | * ========================================================================== | |
6704 | * Miscellaneous routines | |
6705 | * ========================================================================== | |
6706 | */ | |
6707 | ||
6708 | /* | |
6709 | * Remove all pools in the system. | |
6710 | */ | |
6711 | void | |
6712 | spa_evict_all(void) | |
6713 | { | |
6714 | spa_t *spa; | |
6715 | ||
6716 | /* | |
6717 | * Remove all cached state. All pools should be closed now, | |
6718 | * so every spa in the AVL tree should be unreferenced. | |
6719 | */ | |
6720 | mutex_enter(&spa_namespace_lock); | |
6721 | while ((spa = spa_next(NULL)) != NULL) { | |
6722 | /* | |
6723 | * Stop async tasks. The async thread may need to detach | |
6724 | * a device that's been replaced, which requires grabbing | |
6725 | * spa_namespace_lock, so we must drop it here. | |
6726 | */ | |
6727 | spa_open_ref(spa, FTAG); | |
6728 | mutex_exit(&spa_namespace_lock); | |
6729 | spa_async_suspend(spa); | |
6730 | mutex_enter(&spa_namespace_lock); | |
6731 | spa_close(spa, FTAG); | |
6732 | ||
6733 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
6734 | spa_unload(spa); | |
6735 | spa_deactivate(spa); | |
6736 | } | |
6737 | spa_remove(spa); | |
6738 | } | |
6739 | mutex_exit(&spa_namespace_lock); | |
6740 | } | |
6741 | ||
6742 | vdev_t * | |
6743 | spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) | |
6744 | { | |
6745 | vdev_t *vd; | |
6746 | int i; | |
6747 | ||
6748 | if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) | |
6749 | return (vd); | |
6750 | ||
6751 | if (aux) { | |
6752 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { | |
6753 | vd = spa->spa_l2cache.sav_vdevs[i]; | |
6754 | if (vd->vdev_guid == guid) | |
6755 | return (vd); | |
6756 | } | |
6757 | ||
6758 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
6759 | vd = spa->spa_spares.sav_vdevs[i]; | |
6760 | if (vd->vdev_guid == guid) | |
6761 | return (vd); | |
6762 | } | |
6763 | } | |
6764 | ||
6765 | return (NULL); | |
6766 | } | |
6767 | ||
6768 | void | |
6769 | spa_upgrade(spa_t *spa, uint64_t version) | |
6770 | { | |
6771 | ASSERT(spa_writeable(spa)); | |
6772 | ||
6773 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6774 | ||
6775 | /* | |
6776 | * This should only be called for a non-faulted pool, and since a | |
6777 | * future version would result in an unopenable pool, this shouldn't be | |
6778 | * possible. | |
6779 | */ | |
6780 | ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); | |
6781 | ASSERT3U(version, >=, spa->spa_uberblock.ub_version); | |
6782 | ||
6783 | spa->spa_uberblock.ub_version = version; | |
6784 | vdev_config_dirty(spa->spa_root_vdev); | |
6785 | ||
6786 | spa_config_exit(spa, SCL_ALL, FTAG); | |
6787 | ||
6788 | txg_wait_synced(spa_get_dsl(spa), 0); | |
6789 | } | |
6790 | ||
6791 | boolean_t | |
6792 | spa_has_spare(spa_t *spa, uint64_t guid) | |
6793 | { | |
6794 | int i; | |
6795 | uint64_t spareguid; | |
6796 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
6797 | ||
6798 | for (i = 0; i < sav->sav_count; i++) | |
6799 | if (sav->sav_vdevs[i]->vdev_guid == guid) | |
6800 | return (B_TRUE); | |
6801 | ||
6802 | for (i = 0; i < sav->sav_npending; i++) { | |
6803 | if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, | |
6804 | &spareguid) == 0 && spareguid == guid) | |
6805 | return (B_TRUE); | |
6806 | } | |
6807 | ||
6808 | return (B_FALSE); | |
6809 | } | |
6810 | ||
6811 | /* | |
6812 | * Check if a pool has an active shared spare device. | |
6813 | * Note: reference count of an active spare is 2, as a spare and as a replace | |
6814 | */ | |
6815 | static boolean_t | |
6816 | spa_has_active_shared_spare(spa_t *spa) | |
6817 | { | |
6818 | int i, refcnt; | |
6819 | uint64_t pool; | |
6820 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
6821 | ||
6822 | for (i = 0; i < sav->sav_count; i++) { | |
6823 | if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, | |
6824 | &refcnt) && pool != 0ULL && pool == spa_guid(spa) && | |
6825 | refcnt > 2) | |
6826 | return (B_TRUE); | |
6827 | } | |
6828 | ||
6829 | return (B_FALSE); | |
6830 | } | |
6831 | ||
6832 | /* | |
6833 | * Post a zevent corresponding to the given sysevent. The 'name' must be one | |
6834 | * of the event definitions in sys/sysevent/eventdefs.h. The payload will be | |
6835 | * filled in from the spa and (optionally) the vdev. This doesn't do anything | |
6836 | * in the userland libzpool, as we don't want consumers to misinterpret ztest | |
6837 | * or zdb as real changes. | |
6838 | */ | |
6839 | void | |
6840 | spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) | |
6841 | { | |
6842 | zfs_post_sysevent(spa, vd, name); | |
6843 | } | |
6844 | ||
6845 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
6846 | /* state manipulation functions */ | |
6847 | EXPORT_SYMBOL(spa_open); | |
6848 | EXPORT_SYMBOL(spa_open_rewind); | |
6849 | EXPORT_SYMBOL(spa_get_stats); | |
6850 | EXPORT_SYMBOL(spa_create); | |
6851 | EXPORT_SYMBOL(spa_import); | |
6852 | EXPORT_SYMBOL(spa_tryimport); | |
6853 | EXPORT_SYMBOL(spa_destroy); | |
6854 | EXPORT_SYMBOL(spa_export); | |
6855 | EXPORT_SYMBOL(spa_reset); | |
6856 | EXPORT_SYMBOL(spa_async_request); | |
6857 | EXPORT_SYMBOL(spa_async_suspend); | |
6858 | EXPORT_SYMBOL(spa_async_resume); | |
6859 | EXPORT_SYMBOL(spa_inject_addref); | |
6860 | EXPORT_SYMBOL(spa_inject_delref); | |
6861 | EXPORT_SYMBOL(spa_scan_stat_init); | |
6862 | EXPORT_SYMBOL(spa_scan_get_stats); | |
6863 | ||
6864 | /* device maniion */ | |
6865 | EXPORT_SYMBOL(spa_vdev_add); | |
6866 | EXPORT_SYMBOL(spa_vdev_attach); | |
6867 | EXPORT_SYMBOL(spa_vdev_detach); | |
6868 | EXPORT_SYMBOL(spa_vdev_remove); | |
6869 | EXPORT_SYMBOL(spa_vdev_setpath); | |
6870 | EXPORT_SYMBOL(spa_vdev_setfru); | |
6871 | EXPORT_SYMBOL(spa_vdev_split_mirror); | |
6872 | ||
6873 | /* spare statech is global across all pools) */ | |
6874 | EXPORT_SYMBOL(spa_spare_add); | |
6875 | EXPORT_SYMBOL(spa_spare_remove); | |
6876 | EXPORT_SYMBOL(spa_spare_exists); | |
6877 | EXPORT_SYMBOL(spa_spare_activate); | |
6878 | ||
6879 | /* L2ARC statech is global across all pools) */ | |
6880 | EXPORT_SYMBOL(spa_l2cache_add); | |
6881 | EXPORT_SYMBOL(spa_l2cache_remove); | |
6882 | EXPORT_SYMBOL(spa_l2cache_exists); | |
6883 | EXPORT_SYMBOL(spa_l2cache_activate); | |
6884 | EXPORT_SYMBOL(spa_l2cache_drop); | |
6885 | ||
6886 | /* scanning */ | |
6887 | EXPORT_SYMBOL(spa_scan); | |
6888 | EXPORT_SYMBOL(spa_scan_stop); | |
6889 | ||
6890 | /* spa syncing */ | |
6891 | EXPORT_SYMBOL(spa_sync); /* only for DMU use */ | |
6892 | EXPORT_SYMBOL(spa_sync_allpools); | |
6893 | ||
6894 | /* properties */ | |
6895 | EXPORT_SYMBOL(spa_prop_set); | |
6896 | EXPORT_SYMBOL(spa_prop_get); | |
6897 | EXPORT_SYMBOL(spa_prop_clear_bootfs); | |
6898 | ||
6899 | /* asynchronous event notification */ | |
6900 | EXPORT_SYMBOL(spa_event_notify); | |
6901 | #endif | |
6902 | ||
6903 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
6904 | module_param(spa_load_verify_maxinflight, int, 0644); | |
6905 | MODULE_PARM_DESC(spa_load_verify_maxinflight, | |
6906 | "Max concurrent traversal I/Os while verifying pool during import -X"); | |
6907 | ||
6908 | module_param(spa_load_verify_metadata, int, 0644); | |
6909 | MODULE_PARM_DESC(spa_load_verify_metadata, | |
6910 | "Set to traverse metadata on pool import"); | |
6911 | ||
6912 | module_param(spa_load_verify_data, int, 0644); | |
6913 | MODULE_PARM_DESC(spa_load_verify_data, | |
6914 | "Set to traverse data on pool import"); | |
6915 | ||
6916 | module_param(zio_taskq_batch_pct, uint, 0444); | |
6917 | MODULE_PARM_DESC(zio_taskq_batch_pct, | |
6918 | "Percentage of CPUs to run an IO worker thread"); | |
6919 | ||
6920 | #endif |