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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
23 * Copyright (c) 2016, 2019 by Delphix. All rights reserved.
27 #include <sys/spa_impl.h>
29 #include <sys/vdev_impl.h>
30 #include <sys/metaslab_impl.h>
31 #include <sys/dsl_synctask.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/vdev_initialize.h>
37 * Value that is written to disk during initialization.
39 static uint64_t zfs_initialize_value = 0xdeadbeefdeadbeeeULL;
41 /* maximum number of I/Os outstanding per leaf vdev */
42 static const int zfs_initialize_limit = 1;
44 /* size of initializing writes; default 1MiB, see zfs_remove_max_segment */
45 static uint64_t zfs_initialize_chunk_size = 1024 * 1024;
48 vdev_initialize_should_stop(vdev_t *vd)
50 return (vd->vdev_initialize_exit_wanted || !vdev_writeable(vd) ||
51 vd->vdev_detached || vd->vdev_top->vdev_removing);
55 vdev_initialize_zap_update_sync(void *arg, dmu_tx_t *tx)
58 * We pass in the guid instead of the vdev_t since the vdev may
59 * have been freed prior to the sync task being processed. This
60 * happens when a vdev is detached as we call spa_config_vdev_exit(),
61 * stop the initializing thread, schedule the sync task, and free
62 * the vdev. Later when the scheduled sync task is invoked, it would
63 * find that the vdev has been freed.
65 uint64_t guid = *(uint64_t *)arg;
66 uint64_t txg = dmu_tx_get_txg(tx);
67 kmem_free(arg, sizeof (uint64_t));
69 vdev_t *vd = spa_lookup_by_guid(tx->tx_pool->dp_spa, guid, B_FALSE);
70 if (vd == NULL || vd->vdev_top->vdev_removing || !vdev_is_concrete(vd))
73 uint64_t last_offset = vd->vdev_initialize_offset[txg & TXG_MASK];
74 vd->vdev_initialize_offset[txg & TXG_MASK] = 0;
76 VERIFY(vd->vdev_leaf_zap != 0);
78 objset_t *mos = vd->vdev_spa->spa_meta_objset;
80 if (last_offset > 0) {
81 vd->vdev_initialize_last_offset = last_offset;
82 VERIFY0(zap_update(mos, vd->vdev_leaf_zap,
83 VDEV_LEAF_ZAP_INITIALIZE_LAST_OFFSET,
84 sizeof (last_offset), 1, &last_offset, tx));
86 if (vd->vdev_initialize_action_time > 0) {
87 uint64_t val = (uint64_t)vd->vdev_initialize_action_time;
88 VERIFY0(zap_update(mos, vd->vdev_leaf_zap,
89 VDEV_LEAF_ZAP_INITIALIZE_ACTION_TIME, sizeof (val),
93 uint64_t initialize_state = vd->vdev_initialize_state;
94 VERIFY0(zap_update(mos, vd->vdev_leaf_zap,
95 VDEV_LEAF_ZAP_INITIALIZE_STATE, sizeof (initialize_state), 1,
96 &initialize_state, tx));
100 vdev_initialize_change_state(vdev_t *vd, vdev_initializing_state_t new_state)
102 ASSERT(MUTEX_HELD(&vd->vdev_initialize_lock));
103 spa_t *spa = vd->vdev_spa;
105 if (new_state == vd->vdev_initialize_state)
109 * Copy the vd's guid, this will be freed by the sync task.
111 uint64_t *guid = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
112 *guid = vd->vdev_guid;
115 * If we're suspending, then preserving the original start time.
117 if (vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED) {
118 vd->vdev_initialize_action_time = gethrestime_sec();
121 vdev_initializing_state_t old_state = vd->vdev_initialize_state;
122 vd->vdev_initialize_state = new_state;
124 dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
125 VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
126 dsl_sync_task_nowait(spa_get_dsl(spa), vdev_initialize_zap_update_sync,
130 case VDEV_INITIALIZE_ACTIVE:
131 spa_history_log_internal(spa, "initialize", tx,
132 "vdev=%s activated", vd->vdev_path);
134 case VDEV_INITIALIZE_SUSPENDED:
135 spa_history_log_internal(spa, "initialize", tx,
136 "vdev=%s suspended", vd->vdev_path);
138 case VDEV_INITIALIZE_CANCELED:
139 if (old_state == VDEV_INITIALIZE_ACTIVE ||
140 old_state == VDEV_INITIALIZE_SUSPENDED)
141 spa_history_log_internal(spa, "initialize", tx,
142 "vdev=%s canceled", vd->vdev_path);
144 case VDEV_INITIALIZE_COMPLETE:
145 spa_history_log_internal(spa, "initialize", tx,
146 "vdev=%s complete", vd->vdev_path);
149 panic("invalid state %llu", (unsigned long long)new_state);
154 if (new_state != VDEV_INITIALIZE_ACTIVE)
155 spa_notify_waiters(spa);
159 vdev_initialize_cb(zio_t *zio)
161 vdev_t *vd = zio->io_vd;
162 mutex_enter(&vd->vdev_initialize_io_lock);
163 if (zio->io_error == ENXIO && !vdev_writeable(vd)) {
165 * The I/O failed because the vdev was unavailable; roll the
166 * last offset back. (This works because spa_sync waits on
167 * spa_txg_zio before it runs sync tasks.)
170 &vd->vdev_initialize_offset[zio->io_txg & TXG_MASK];
171 *off = MIN(*off, zio->io_offset);
174 * Since initializing is best-effort, we ignore I/O errors and
175 * rely on vdev_probe to determine if the errors are more
178 if (zio->io_error != 0)
179 vd->vdev_stat.vs_initialize_errors++;
181 vd->vdev_initialize_bytes_done += zio->io_orig_size;
183 ASSERT3U(vd->vdev_initialize_inflight, >, 0);
184 vd->vdev_initialize_inflight--;
185 cv_broadcast(&vd->vdev_initialize_io_cv);
186 mutex_exit(&vd->vdev_initialize_io_lock);
188 spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
191 /* Takes care of physical writing and limiting # of concurrent ZIOs. */
193 vdev_initialize_write(vdev_t *vd, uint64_t start, uint64_t size, abd_t *data)
195 spa_t *spa = vd->vdev_spa;
197 /* Limit inflight initializing I/Os */
198 mutex_enter(&vd->vdev_initialize_io_lock);
199 while (vd->vdev_initialize_inflight >= zfs_initialize_limit) {
200 cv_wait(&vd->vdev_initialize_io_cv,
201 &vd->vdev_initialize_io_lock);
203 vd->vdev_initialize_inflight++;
204 mutex_exit(&vd->vdev_initialize_io_lock);
206 dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
207 VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
208 uint64_t txg = dmu_tx_get_txg(tx);
210 spa_config_enter(spa, SCL_STATE_ALL, vd, RW_READER);
211 mutex_enter(&vd->vdev_initialize_lock);
213 if (vd->vdev_initialize_offset[txg & TXG_MASK] == 0) {
214 uint64_t *guid = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
215 *guid = vd->vdev_guid;
217 /* This is the first write of this txg. */
218 dsl_sync_task_nowait(spa_get_dsl(spa),
219 vdev_initialize_zap_update_sync, guid, tx);
223 * We know the vdev struct will still be around since all
224 * consumers of vdev_free must stop the initialization first.
226 if (vdev_initialize_should_stop(vd)) {
227 mutex_enter(&vd->vdev_initialize_io_lock);
228 ASSERT3U(vd->vdev_initialize_inflight, >, 0);
229 vd->vdev_initialize_inflight--;
230 mutex_exit(&vd->vdev_initialize_io_lock);
231 spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
232 mutex_exit(&vd->vdev_initialize_lock);
234 return (SET_ERROR(EINTR));
236 mutex_exit(&vd->vdev_initialize_lock);
238 vd->vdev_initialize_offset[txg & TXG_MASK] = start + size;
239 zio_nowait(zio_write_phys(spa->spa_txg_zio[txg & TXG_MASK], vd, start,
240 size, data, ZIO_CHECKSUM_OFF, vdev_initialize_cb, NULL,
241 ZIO_PRIORITY_INITIALIZING, ZIO_FLAG_CANFAIL, B_FALSE));
242 /* vdev_initialize_cb releases SCL_STATE_ALL */
250 * Callback to fill each ABD chunk with zfs_initialize_value. len must be
251 * divisible by sizeof (uint64_t), and buf must be 8-byte aligned. The ABD
252 * allocation will guarantee these for us.
255 vdev_initialize_block_fill(void *buf, size_t len, void *unused)
259 ASSERT0(len % sizeof (uint64_t));
260 for (uint64_t i = 0; i < len; i += sizeof (uint64_t)) {
261 *(uint64_t *)((char *)(buf) + i) = zfs_initialize_value;
267 vdev_initialize_block_alloc(void)
269 /* Allocate ABD for filler data */
270 abd_t *data = abd_alloc_for_io(zfs_initialize_chunk_size, B_FALSE);
272 ASSERT0(zfs_initialize_chunk_size % sizeof (uint64_t));
273 (void) abd_iterate_func(data, 0, zfs_initialize_chunk_size,
274 vdev_initialize_block_fill, NULL);
280 vdev_initialize_block_free(abd_t *data)
286 vdev_initialize_ranges(vdev_t *vd, abd_t *data)
288 range_tree_t *rt = vd->vdev_initialize_tree;
289 zfs_btree_t *bt = &rt->rt_root;
290 zfs_btree_index_t where;
292 for (range_seg_t *rs = zfs_btree_first(bt, &where); rs != NULL;
293 rs = zfs_btree_next(bt, &where, &where)) {
294 uint64_t size = rs_get_end(rs, rt) - rs_get_start(rs, rt);
296 /* Split range into legally-sized physical chunks */
297 uint64_t writes_required =
298 ((size - 1) / zfs_initialize_chunk_size) + 1;
300 for (uint64_t w = 0; w < writes_required; w++) {
303 error = vdev_initialize_write(vd,
304 VDEV_LABEL_START_SIZE + rs_get_start(rs, rt) +
305 (w * zfs_initialize_chunk_size),
306 MIN(size - (w * zfs_initialize_chunk_size),
307 zfs_initialize_chunk_size), data);
316 vdev_initialize_xlate_last_rs_end(void *arg, range_seg64_t *physical_rs)
318 uint64_t *last_rs_end = (uint64_t *)arg;
320 if (physical_rs->rs_end > *last_rs_end)
321 *last_rs_end = physical_rs->rs_end;
325 vdev_initialize_xlate_progress(void *arg, range_seg64_t *physical_rs)
327 vdev_t *vd = (vdev_t *)arg;
329 uint64_t size = physical_rs->rs_end - physical_rs->rs_start;
330 vd->vdev_initialize_bytes_est += size;
332 if (vd->vdev_initialize_last_offset > physical_rs->rs_end) {
333 vd->vdev_initialize_bytes_done += size;
334 } else if (vd->vdev_initialize_last_offset > physical_rs->rs_start &&
335 vd->vdev_initialize_last_offset < physical_rs->rs_end) {
336 vd->vdev_initialize_bytes_done +=
337 vd->vdev_initialize_last_offset - physical_rs->rs_start;
342 vdev_initialize_calculate_progress(vdev_t *vd)
344 ASSERT(spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_READER) ||
345 spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_WRITER));
346 ASSERT(vd->vdev_leaf_zap != 0);
348 vd->vdev_initialize_bytes_est = 0;
349 vd->vdev_initialize_bytes_done = 0;
351 for (uint64_t i = 0; i < vd->vdev_top->vdev_ms_count; i++) {
352 metaslab_t *msp = vd->vdev_top->vdev_ms[i];
353 mutex_enter(&msp->ms_lock);
355 uint64_t ms_free = (msp->ms_size -
356 metaslab_allocated_space(msp)) /
357 vdev_get_ndisks(vd->vdev_top);
360 * Convert the metaslab range to a physical range
361 * on our vdev. We use this to determine if we are
362 * in the middle of this metaslab range.
364 range_seg64_t logical_rs, physical_rs, remain_rs;
365 logical_rs.rs_start = msp->ms_start;
366 logical_rs.rs_end = msp->ms_start + msp->ms_size;
368 /* Metaslab space after this offset has not been initialized */
369 vdev_xlate(vd, &logical_rs, &physical_rs, &remain_rs);
370 if (vd->vdev_initialize_last_offset <= physical_rs.rs_start) {
371 vd->vdev_initialize_bytes_est += ms_free;
372 mutex_exit(&msp->ms_lock);
376 /* Metaslab space before this offset has been initialized */
377 uint64_t last_rs_end = physical_rs.rs_end;
378 if (!vdev_xlate_is_empty(&remain_rs)) {
379 vdev_xlate_walk(vd, &remain_rs,
380 vdev_initialize_xlate_last_rs_end, &last_rs_end);
383 if (vd->vdev_initialize_last_offset > last_rs_end) {
384 vd->vdev_initialize_bytes_done += ms_free;
385 vd->vdev_initialize_bytes_est += ms_free;
386 mutex_exit(&msp->ms_lock);
391 * If we get here, we're in the middle of initializing this
392 * metaslab. Load it and walk the free tree for more accurate
393 * progress estimation.
395 VERIFY0(metaslab_load(msp));
397 zfs_btree_index_t where;
398 range_tree_t *rt = msp->ms_allocatable;
399 for (range_seg_t *rs =
400 zfs_btree_first(&rt->rt_root, &where); rs;
401 rs = zfs_btree_next(&rt->rt_root, &where,
403 logical_rs.rs_start = rs_get_start(rs, rt);
404 logical_rs.rs_end = rs_get_end(rs, rt);
406 vdev_xlate_walk(vd, &logical_rs,
407 vdev_initialize_xlate_progress, vd);
409 mutex_exit(&msp->ms_lock);
414 vdev_initialize_load(vdev_t *vd)
417 ASSERT(spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_READER) ||
418 spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_WRITER));
419 ASSERT(vd->vdev_leaf_zap != 0);
421 if (vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE ||
422 vd->vdev_initialize_state == VDEV_INITIALIZE_SUSPENDED) {
423 err = zap_lookup(vd->vdev_spa->spa_meta_objset,
424 vd->vdev_leaf_zap, VDEV_LEAF_ZAP_INITIALIZE_LAST_OFFSET,
425 sizeof (vd->vdev_initialize_last_offset), 1,
426 &vd->vdev_initialize_last_offset);
428 vd->vdev_initialize_last_offset = 0;
433 vdev_initialize_calculate_progress(vd);
438 vdev_initialize_xlate_range_add(void *arg, range_seg64_t *physical_rs)
442 /* Only add segments that we have not visited yet */
443 if (physical_rs->rs_end <= vd->vdev_initialize_last_offset)
446 /* Pick up where we left off mid-range. */
447 if (vd->vdev_initialize_last_offset > physical_rs->rs_start) {
448 zfs_dbgmsg("range write: vd %s changed (%llu, %llu) to "
449 "(%llu, %llu)", vd->vdev_path,
450 (u_longlong_t)physical_rs->rs_start,
451 (u_longlong_t)physical_rs->rs_end,
452 (u_longlong_t)vd->vdev_initialize_last_offset,
453 (u_longlong_t)physical_rs->rs_end);
454 ASSERT3U(physical_rs->rs_end, >,
455 vd->vdev_initialize_last_offset);
456 physical_rs->rs_start = vd->vdev_initialize_last_offset;
459 ASSERT3U(physical_rs->rs_end, >, physical_rs->rs_start);
461 range_tree_add(vd->vdev_initialize_tree, physical_rs->rs_start,
462 physical_rs->rs_end - physical_rs->rs_start);
466 * Convert the logical range into a physical range and add it to our
470 vdev_initialize_range_add(void *arg, uint64_t start, uint64_t size)
473 range_seg64_t logical_rs;
474 logical_rs.rs_start = start;
475 logical_rs.rs_end = start + size;
477 ASSERT(vd->vdev_ops->vdev_op_leaf);
478 vdev_xlate_walk(vd, &logical_rs, vdev_initialize_xlate_range_add, arg);
481 static __attribute__((noreturn)) void
482 vdev_initialize_thread(void *arg)
485 spa_t *spa = vd->vdev_spa;
487 uint64_t ms_count = 0;
489 ASSERT(vdev_is_concrete(vd));
490 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
492 vd->vdev_initialize_last_offset = 0;
493 VERIFY0(vdev_initialize_load(vd));
495 abd_t *deadbeef = vdev_initialize_block_alloc();
497 vd->vdev_initialize_tree = range_tree_create(NULL, RANGE_SEG64, NULL,
500 for (uint64_t i = 0; !vd->vdev_detached &&
501 i < vd->vdev_top->vdev_ms_count; i++) {
502 metaslab_t *msp = vd->vdev_top->vdev_ms[i];
503 boolean_t unload_when_done = B_FALSE;
506 * If we've expanded the top-level vdev or it's our
507 * first pass, calculate our progress.
509 if (vd->vdev_top->vdev_ms_count != ms_count) {
510 vdev_initialize_calculate_progress(vd);
511 ms_count = vd->vdev_top->vdev_ms_count;
514 spa_config_exit(spa, SCL_CONFIG, FTAG);
515 metaslab_disable(msp);
516 mutex_enter(&msp->ms_lock);
517 if (!msp->ms_loaded && !msp->ms_loading)
518 unload_when_done = B_TRUE;
519 VERIFY0(metaslab_load(msp));
521 range_tree_walk(msp->ms_allocatable, vdev_initialize_range_add,
523 mutex_exit(&msp->ms_lock);
525 error = vdev_initialize_ranges(vd, deadbeef);
526 metaslab_enable(msp, B_TRUE, unload_when_done);
527 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
529 range_tree_vacate(vd->vdev_initialize_tree, NULL, NULL);
534 spa_config_exit(spa, SCL_CONFIG, FTAG);
535 mutex_enter(&vd->vdev_initialize_io_lock);
536 while (vd->vdev_initialize_inflight > 0) {
537 cv_wait(&vd->vdev_initialize_io_cv,
538 &vd->vdev_initialize_io_lock);
540 mutex_exit(&vd->vdev_initialize_io_lock);
542 range_tree_destroy(vd->vdev_initialize_tree);
543 vdev_initialize_block_free(deadbeef);
544 vd->vdev_initialize_tree = NULL;
546 mutex_enter(&vd->vdev_initialize_lock);
547 if (!vd->vdev_initialize_exit_wanted) {
548 if (vdev_writeable(vd)) {
549 vdev_initialize_change_state(vd,
550 VDEV_INITIALIZE_COMPLETE);
551 } else if (vd->vdev_faulted) {
552 vdev_initialize_change_state(vd,
553 VDEV_INITIALIZE_CANCELED);
556 ASSERT(vd->vdev_initialize_thread != NULL ||
557 vd->vdev_initialize_inflight == 0);
560 * Drop the vdev_initialize_lock while we sync out the
561 * txg since it's possible that a device might be trying to
562 * come online and must check to see if it needs to restart an
563 * initialization. That thread will be holding the spa_config_lock
564 * which would prevent the txg_wait_synced from completing.
566 mutex_exit(&vd->vdev_initialize_lock);
567 txg_wait_synced(spa_get_dsl(spa), 0);
568 mutex_enter(&vd->vdev_initialize_lock);
570 vd->vdev_initialize_thread = NULL;
571 cv_broadcast(&vd->vdev_initialize_cv);
572 mutex_exit(&vd->vdev_initialize_lock);
578 * Initiates a device. Caller must hold vdev_initialize_lock.
579 * Device must be a leaf and not already be initializing.
582 vdev_initialize(vdev_t *vd)
584 ASSERT(MUTEX_HELD(&vd->vdev_initialize_lock));
585 ASSERT(vd->vdev_ops->vdev_op_leaf);
586 ASSERT(vdev_is_concrete(vd));
587 ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
588 ASSERT(!vd->vdev_detached);
589 ASSERT(!vd->vdev_initialize_exit_wanted);
590 ASSERT(!vd->vdev_top->vdev_removing);
592 vdev_initialize_change_state(vd, VDEV_INITIALIZE_ACTIVE);
593 vd->vdev_initialize_thread = thread_create(NULL, 0,
594 vdev_initialize_thread, vd, 0, &p0, TS_RUN, maxclsyspri);
598 * Wait for the initialize thread to be terminated (cancelled or stopped).
601 vdev_initialize_stop_wait_impl(vdev_t *vd)
603 ASSERT(MUTEX_HELD(&vd->vdev_initialize_lock));
605 while (vd->vdev_initialize_thread != NULL)
606 cv_wait(&vd->vdev_initialize_cv, &vd->vdev_initialize_lock);
608 ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
609 vd->vdev_initialize_exit_wanted = B_FALSE;
613 * Wait for vdev initialize threads which were either to cleanly exit.
616 vdev_initialize_stop_wait(spa_t *spa, list_t *vd_list)
621 ASSERT(MUTEX_HELD(&spa_namespace_lock));
623 while ((vd = list_remove_head(vd_list)) != NULL) {
624 mutex_enter(&vd->vdev_initialize_lock);
625 vdev_initialize_stop_wait_impl(vd);
626 mutex_exit(&vd->vdev_initialize_lock);
631 * Stop initializing a device, with the resultant initializing state being
632 * tgt_state. For blocking behavior pass NULL for vd_list. Otherwise, when
633 * a list_t is provided the stopping vdev is inserted in to the list. Callers
634 * are then required to call vdev_initialize_stop_wait() to block for all the
635 * initialization threads to exit. The caller must hold vdev_initialize_lock
636 * and must not be writing to the spa config, as the initializing thread may
637 * try to enter the config as a reader before exiting.
640 vdev_initialize_stop(vdev_t *vd, vdev_initializing_state_t tgt_state,
643 ASSERT(!spa_config_held(vd->vdev_spa, SCL_CONFIG|SCL_STATE, RW_WRITER));
644 ASSERT(MUTEX_HELD(&vd->vdev_initialize_lock));
645 ASSERT(vd->vdev_ops->vdev_op_leaf);
646 ASSERT(vdev_is_concrete(vd));
649 * Allow cancel requests to proceed even if the initialize thread
652 if (vd->vdev_initialize_thread == NULL &&
653 tgt_state != VDEV_INITIALIZE_CANCELED) {
657 vdev_initialize_change_state(vd, tgt_state);
658 vd->vdev_initialize_exit_wanted = B_TRUE;
660 if (vd_list == NULL) {
661 vdev_initialize_stop_wait_impl(vd);
663 ASSERT(MUTEX_HELD(&spa_namespace_lock));
664 list_insert_tail(vd_list, vd);
669 vdev_initialize_stop_all_impl(vdev_t *vd, vdev_initializing_state_t tgt_state,
672 if (vd->vdev_ops->vdev_op_leaf && vdev_is_concrete(vd)) {
673 mutex_enter(&vd->vdev_initialize_lock);
674 vdev_initialize_stop(vd, tgt_state, vd_list);
675 mutex_exit(&vd->vdev_initialize_lock);
679 for (uint64_t i = 0; i < vd->vdev_children; i++) {
680 vdev_initialize_stop_all_impl(vd->vdev_child[i], tgt_state,
686 * Convenience function to stop initializing of a vdev tree and set all
687 * initialize thread pointers to NULL.
690 vdev_initialize_stop_all(vdev_t *vd, vdev_initializing_state_t tgt_state)
692 spa_t *spa = vd->vdev_spa;
695 ASSERT(MUTEX_HELD(&spa_namespace_lock));
697 list_create(&vd_list, sizeof (vdev_t),
698 offsetof(vdev_t, vdev_initialize_node));
700 vdev_initialize_stop_all_impl(vd, tgt_state, &vd_list);
701 vdev_initialize_stop_wait(spa, &vd_list);
703 if (vd->vdev_spa->spa_sync_on) {
704 /* Make sure that our state has been synced to disk */
705 txg_wait_synced(spa_get_dsl(vd->vdev_spa), 0);
708 list_destroy(&vd_list);
712 vdev_initialize_restart(vdev_t *vd)
714 ASSERT(MUTEX_HELD(&spa_namespace_lock));
715 ASSERT(!spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
717 if (vd->vdev_leaf_zap != 0) {
718 mutex_enter(&vd->vdev_initialize_lock);
719 uint64_t initialize_state = VDEV_INITIALIZE_NONE;
720 int err = zap_lookup(vd->vdev_spa->spa_meta_objset,
721 vd->vdev_leaf_zap, VDEV_LEAF_ZAP_INITIALIZE_STATE,
722 sizeof (initialize_state), 1, &initialize_state);
723 ASSERT(err == 0 || err == ENOENT);
724 vd->vdev_initialize_state = initialize_state;
726 uint64_t timestamp = 0;
727 err = zap_lookup(vd->vdev_spa->spa_meta_objset,
728 vd->vdev_leaf_zap, VDEV_LEAF_ZAP_INITIALIZE_ACTION_TIME,
729 sizeof (timestamp), 1, ×tamp);
730 ASSERT(err == 0 || err == ENOENT);
731 vd->vdev_initialize_action_time = timestamp;
733 if (vd->vdev_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
735 /* load progress for reporting, but don't resume */
736 VERIFY0(vdev_initialize_load(vd));
737 } else if (vd->vdev_initialize_state ==
738 VDEV_INITIALIZE_ACTIVE && vdev_writeable(vd) &&
739 !vd->vdev_top->vdev_removing &&
740 vd->vdev_initialize_thread == NULL) {
744 mutex_exit(&vd->vdev_initialize_lock);
747 for (uint64_t i = 0; i < vd->vdev_children; i++) {
748 vdev_initialize_restart(vd->vdev_child[i]);
752 EXPORT_SYMBOL(vdev_initialize);
753 EXPORT_SYMBOL(vdev_initialize_stop);
754 EXPORT_SYMBOL(vdev_initialize_stop_all);
755 EXPORT_SYMBOL(vdev_initialize_stop_wait);
756 EXPORT_SYMBOL(vdev_initialize_restart);
758 ZFS_MODULE_PARAM(zfs, zfs_, initialize_value, U64, ZMOD_RW,
759 "Value written during zpool initialize");
761 ZFS_MODULE_PARAM(zfs, zfs_, initialize_chunk_size, U64, ZMOD_RW,
762 "Size in bytes of writes by zpool initialize");