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 http://www.opensolaris.org/os/licensing.
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.
40 static unsigned long zfs_initialize_value
= 0xdeadbeefUL
;
42 static unsigned long zfs_initialize_value
= 0xdeadbeefdeadbeeeULL
;
45 /* maximum number of I/Os outstanding per leaf vdev */
46 static const int zfs_initialize_limit
= 1;
48 /* size of initializing writes; default 1MiB, see zfs_remove_max_segment */
49 static unsigned long zfs_initialize_chunk_size
= 1024 * 1024;
52 vdev_initialize_should_stop(vdev_t
*vd
)
54 return (vd
->vdev_initialize_exit_wanted
|| !vdev_writeable(vd
) ||
55 vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
);
59 vdev_initialize_zap_update_sync(void *arg
, dmu_tx_t
*tx
)
62 * We pass in the guid instead of the vdev_t since the vdev may
63 * have been freed prior to the sync task being processed. This
64 * happens when a vdev is detached as we call spa_config_vdev_exit(),
65 * stop the initializing thread, schedule the sync task, and free
66 * the vdev. Later when the scheduled sync task is invoked, it would
67 * find that the vdev has been freed.
69 uint64_t guid
= *(uint64_t *)arg
;
70 uint64_t txg
= dmu_tx_get_txg(tx
);
71 kmem_free(arg
, sizeof (uint64_t));
73 vdev_t
*vd
= spa_lookup_by_guid(tx
->tx_pool
->dp_spa
, guid
, B_FALSE
);
74 if (vd
== NULL
|| vd
->vdev_top
->vdev_removing
|| !vdev_is_concrete(vd
))
77 uint64_t last_offset
= vd
->vdev_initialize_offset
[txg
& TXG_MASK
];
78 vd
->vdev_initialize_offset
[txg
& TXG_MASK
] = 0;
80 VERIFY(vd
->vdev_leaf_zap
!= 0);
82 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
84 if (last_offset
> 0) {
85 vd
->vdev_initialize_last_offset
= last_offset
;
86 VERIFY0(zap_update(mos
, vd
->vdev_leaf_zap
,
87 VDEV_LEAF_ZAP_INITIALIZE_LAST_OFFSET
,
88 sizeof (last_offset
), 1, &last_offset
, tx
));
90 if (vd
->vdev_initialize_action_time
> 0) {
91 uint64_t val
= (uint64_t)vd
->vdev_initialize_action_time
;
92 VERIFY0(zap_update(mos
, vd
->vdev_leaf_zap
,
93 VDEV_LEAF_ZAP_INITIALIZE_ACTION_TIME
, sizeof (val
),
97 uint64_t initialize_state
= vd
->vdev_initialize_state
;
98 VERIFY0(zap_update(mos
, vd
->vdev_leaf_zap
,
99 VDEV_LEAF_ZAP_INITIALIZE_STATE
, sizeof (initialize_state
), 1,
100 &initialize_state
, tx
));
104 vdev_initialize_change_state(vdev_t
*vd
, vdev_initializing_state_t new_state
)
106 ASSERT(MUTEX_HELD(&vd
->vdev_initialize_lock
));
107 spa_t
*spa
= vd
->vdev_spa
;
109 if (new_state
== vd
->vdev_initialize_state
)
113 * Copy the vd's guid, this will be freed by the sync task.
115 uint64_t *guid
= kmem_zalloc(sizeof (uint64_t), KM_SLEEP
);
116 *guid
= vd
->vdev_guid
;
119 * If we're suspending, then preserving the original start time.
121 if (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
) {
122 vd
->vdev_initialize_action_time
= gethrestime_sec();
125 vdev_initializing_state_t old_state
= vd
->vdev_initialize_state
;
126 vd
->vdev_initialize_state
= new_state
;
128 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
129 VERIFY0(dmu_tx_assign(tx
, TXG_WAIT
));
130 dsl_sync_task_nowait(spa_get_dsl(spa
), vdev_initialize_zap_update_sync
,
134 case VDEV_INITIALIZE_ACTIVE
:
135 spa_history_log_internal(spa
, "initialize", tx
,
136 "vdev=%s activated", vd
->vdev_path
);
138 case VDEV_INITIALIZE_SUSPENDED
:
139 spa_history_log_internal(spa
, "initialize", tx
,
140 "vdev=%s suspended", vd
->vdev_path
);
142 case VDEV_INITIALIZE_CANCELED
:
143 if (old_state
== VDEV_INITIALIZE_ACTIVE
||
144 old_state
== VDEV_INITIALIZE_SUSPENDED
)
145 spa_history_log_internal(spa
, "initialize", tx
,
146 "vdev=%s canceled", vd
->vdev_path
);
148 case VDEV_INITIALIZE_COMPLETE
:
149 spa_history_log_internal(spa
, "initialize", tx
,
150 "vdev=%s complete", vd
->vdev_path
);
153 panic("invalid state %llu", (unsigned long long)new_state
);
158 if (new_state
!= VDEV_INITIALIZE_ACTIVE
)
159 spa_notify_waiters(spa
);
163 vdev_initialize_cb(zio_t
*zio
)
165 vdev_t
*vd
= zio
->io_vd
;
166 mutex_enter(&vd
->vdev_initialize_io_lock
);
167 if (zio
->io_error
== ENXIO
&& !vdev_writeable(vd
)) {
169 * The I/O failed because the vdev was unavailable; roll the
170 * last offset back. (This works because spa_sync waits on
171 * spa_txg_zio before it runs sync tasks.)
174 &vd
->vdev_initialize_offset
[zio
->io_txg
& TXG_MASK
];
175 *off
= MIN(*off
, zio
->io_offset
);
178 * Since initializing is best-effort, we ignore I/O errors and
179 * rely on vdev_probe to determine if the errors are more
182 if (zio
->io_error
!= 0)
183 vd
->vdev_stat
.vs_initialize_errors
++;
185 vd
->vdev_initialize_bytes_done
+= zio
->io_orig_size
;
187 ASSERT3U(vd
->vdev_initialize_inflight
, >, 0);
188 vd
->vdev_initialize_inflight
--;
189 cv_broadcast(&vd
->vdev_initialize_io_cv
);
190 mutex_exit(&vd
->vdev_initialize_io_lock
);
192 spa_config_exit(vd
->vdev_spa
, SCL_STATE_ALL
, vd
);
195 /* Takes care of physical writing and limiting # of concurrent ZIOs. */
197 vdev_initialize_write(vdev_t
*vd
, uint64_t start
, uint64_t size
, abd_t
*data
)
199 spa_t
*spa
= vd
->vdev_spa
;
201 /* Limit inflight initializing I/Os */
202 mutex_enter(&vd
->vdev_initialize_io_lock
);
203 while (vd
->vdev_initialize_inflight
>= zfs_initialize_limit
) {
204 cv_wait(&vd
->vdev_initialize_io_cv
,
205 &vd
->vdev_initialize_io_lock
);
207 vd
->vdev_initialize_inflight
++;
208 mutex_exit(&vd
->vdev_initialize_io_lock
);
210 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
211 VERIFY0(dmu_tx_assign(tx
, TXG_WAIT
));
212 uint64_t txg
= dmu_tx_get_txg(tx
);
214 spa_config_enter(spa
, SCL_STATE_ALL
, vd
, RW_READER
);
215 mutex_enter(&vd
->vdev_initialize_lock
);
217 if (vd
->vdev_initialize_offset
[txg
& TXG_MASK
] == 0) {
218 uint64_t *guid
= kmem_zalloc(sizeof (uint64_t), KM_SLEEP
);
219 *guid
= vd
->vdev_guid
;
221 /* This is the first write of this txg. */
222 dsl_sync_task_nowait(spa_get_dsl(spa
),
223 vdev_initialize_zap_update_sync
, guid
, tx
);
227 * We know the vdev struct will still be around since all
228 * consumers of vdev_free must stop the initialization first.
230 if (vdev_initialize_should_stop(vd
)) {
231 mutex_enter(&vd
->vdev_initialize_io_lock
);
232 ASSERT3U(vd
->vdev_initialize_inflight
, >, 0);
233 vd
->vdev_initialize_inflight
--;
234 mutex_exit(&vd
->vdev_initialize_io_lock
);
235 spa_config_exit(vd
->vdev_spa
, SCL_STATE_ALL
, vd
);
236 mutex_exit(&vd
->vdev_initialize_lock
);
238 return (SET_ERROR(EINTR
));
240 mutex_exit(&vd
->vdev_initialize_lock
);
242 vd
->vdev_initialize_offset
[txg
& TXG_MASK
] = start
+ size
;
243 zio_nowait(zio_write_phys(spa
->spa_txg_zio
[txg
& TXG_MASK
], vd
, start
,
244 size
, data
, ZIO_CHECKSUM_OFF
, vdev_initialize_cb
, NULL
,
245 ZIO_PRIORITY_INITIALIZING
, ZIO_FLAG_CANFAIL
, B_FALSE
));
246 /* vdev_initialize_cb releases SCL_STATE_ALL */
254 * Callback to fill each ABD chunk with zfs_initialize_value. len must be
255 * divisible by sizeof (uint64_t), and buf must be 8-byte aligned. The ABD
256 * allocation will guarantee these for us.
259 vdev_initialize_block_fill(void *buf
, size_t len
, void *unused
)
263 ASSERT0(len
% sizeof (uint64_t));
265 for (uint64_t i
= 0; i
< len
; i
+= sizeof (uint32_t)) {
266 *(uint32_t *)((char *)(buf
) + i
) = zfs_initialize_value
;
269 for (uint64_t i
= 0; i
< len
; i
+= sizeof (uint64_t)) {
270 *(uint64_t *)((char *)(buf
) + i
) = zfs_initialize_value
;
277 vdev_initialize_block_alloc(void)
279 /* Allocate ABD for filler data */
280 abd_t
*data
= abd_alloc_for_io(zfs_initialize_chunk_size
, B_FALSE
);
282 ASSERT0(zfs_initialize_chunk_size
% sizeof (uint64_t));
283 (void) abd_iterate_func(data
, 0, zfs_initialize_chunk_size
,
284 vdev_initialize_block_fill
, NULL
);
290 vdev_initialize_block_free(abd_t
*data
)
296 vdev_initialize_ranges(vdev_t
*vd
, abd_t
*data
)
298 range_tree_t
*rt
= vd
->vdev_initialize_tree
;
299 zfs_btree_t
*bt
= &rt
->rt_root
;
300 zfs_btree_index_t where
;
302 for (range_seg_t
*rs
= zfs_btree_first(bt
, &where
); rs
!= NULL
;
303 rs
= zfs_btree_next(bt
, &where
, &where
)) {
304 uint64_t size
= rs_get_end(rs
, rt
) - rs_get_start(rs
, rt
);
306 /* Split range into legally-sized physical chunks */
307 uint64_t writes_required
=
308 ((size
- 1) / zfs_initialize_chunk_size
) + 1;
310 for (uint64_t w
= 0; w
< writes_required
; w
++) {
313 error
= vdev_initialize_write(vd
,
314 VDEV_LABEL_START_SIZE
+ rs_get_start(rs
, rt
) +
315 (w
* zfs_initialize_chunk_size
),
316 MIN(size
- (w
* zfs_initialize_chunk_size
),
317 zfs_initialize_chunk_size
), data
);
326 vdev_initialize_xlate_last_rs_end(void *arg
, range_seg64_t
*physical_rs
)
328 uint64_t *last_rs_end
= (uint64_t *)arg
;
330 if (physical_rs
->rs_end
> *last_rs_end
)
331 *last_rs_end
= physical_rs
->rs_end
;
335 vdev_initialize_xlate_progress(void *arg
, range_seg64_t
*physical_rs
)
337 vdev_t
*vd
= (vdev_t
*)arg
;
339 uint64_t size
= physical_rs
->rs_end
- physical_rs
->rs_start
;
340 vd
->vdev_initialize_bytes_est
+= size
;
342 if (vd
->vdev_initialize_last_offset
> physical_rs
->rs_end
) {
343 vd
->vdev_initialize_bytes_done
+= size
;
344 } else if (vd
->vdev_initialize_last_offset
> physical_rs
->rs_start
&&
345 vd
->vdev_initialize_last_offset
< physical_rs
->rs_end
) {
346 vd
->vdev_initialize_bytes_done
+=
347 vd
->vdev_initialize_last_offset
- physical_rs
->rs_start
;
352 vdev_initialize_calculate_progress(vdev_t
*vd
)
354 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_CONFIG
, RW_READER
) ||
355 spa_config_held(vd
->vdev_spa
, SCL_CONFIG
, RW_WRITER
));
356 ASSERT(vd
->vdev_leaf_zap
!= 0);
358 vd
->vdev_initialize_bytes_est
= 0;
359 vd
->vdev_initialize_bytes_done
= 0;
361 for (uint64_t i
= 0; i
< vd
->vdev_top
->vdev_ms_count
; i
++) {
362 metaslab_t
*msp
= vd
->vdev_top
->vdev_ms
[i
];
363 mutex_enter(&msp
->ms_lock
);
365 uint64_t ms_free
= (msp
->ms_size
-
366 metaslab_allocated_space(msp
)) /
367 vdev_get_ndisks(vd
->vdev_top
);
370 * Convert the metaslab range to a physical range
371 * on our vdev. We use this to determine if we are
372 * in the middle of this metaslab range.
374 range_seg64_t logical_rs
, physical_rs
, remain_rs
;
375 logical_rs
.rs_start
= msp
->ms_start
;
376 logical_rs
.rs_end
= msp
->ms_start
+ msp
->ms_size
;
378 /* Metaslab space after this offset has not been initialized */
379 vdev_xlate(vd
, &logical_rs
, &physical_rs
, &remain_rs
);
380 if (vd
->vdev_initialize_last_offset
<= physical_rs
.rs_start
) {
381 vd
->vdev_initialize_bytes_est
+= ms_free
;
382 mutex_exit(&msp
->ms_lock
);
386 /* Metaslab space before this offset has been initialized */
387 uint64_t last_rs_end
= physical_rs
.rs_end
;
388 if (!vdev_xlate_is_empty(&remain_rs
)) {
389 vdev_xlate_walk(vd
, &remain_rs
,
390 vdev_initialize_xlate_last_rs_end
, &last_rs_end
);
393 if (vd
->vdev_initialize_last_offset
> last_rs_end
) {
394 vd
->vdev_initialize_bytes_done
+= ms_free
;
395 vd
->vdev_initialize_bytes_est
+= ms_free
;
396 mutex_exit(&msp
->ms_lock
);
401 * If we get here, we're in the middle of initializing this
402 * metaslab. Load it and walk the free tree for more accurate
403 * progress estimation.
405 VERIFY0(metaslab_load(msp
));
407 zfs_btree_index_t where
;
408 range_tree_t
*rt
= msp
->ms_allocatable
;
409 for (range_seg_t
*rs
=
410 zfs_btree_first(&rt
->rt_root
, &where
); rs
;
411 rs
= zfs_btree_next(&rt
->rt_root
, &where
,
413 logical_rs
.rs_start
= rs_get_start(rs
, rt
);
414 logical_rs
.rs_end
= rs_get_end(rs
, rt
);
416 vdev_xlate_walk(vd
, &logical_rs
,
417 vdev_initialize_xlate_progress
, vd
);
419 mutex_exit(&msp
->ms_lock
);
424 vdev_initialize_load(vdev_t
*vd
)
427 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_CONFIG
, RW_READER
) ||
428 spa_config_held(vd
->vdev_spa
, SCL_CONFIG
, RW_WRITER
));
429 ASSERT(vd
->vdev_leaf_zap
!= 0);
431 if (vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
432 vd
->vdev_initialize_state
== VDEV_INITIALIZE_SUSPENDED
) {
433 err
= zap_lookup(vd
->vdev_spa
->spa_meta_objset
,
434 vd
->vdev_leaf_zap
, VDEV_LEAF_ZAP_INITIALIZE_LAST_OFFSET
,
435 sizeof (vd
->vdev_initialize_last_offset
), 1,
436 &vd
->vdev_initialize_last_offset
);
438 vd
->vdev_initialize_last_offset
= 0;
443 vdev_initialize_calculate_progress(vd
);
448 vdev_initialize_xlate_range_add(void *arg
, range_seg64_t
*physical_rs
)
452 /* Only add segments that we have not visited yet */
453 if (physical_rs
->rs_end
<= vd
->vdev_initialize_last_offset
)
456 /* Pick up where we left off mid-range. */
457 if (vd
->vdev_initialize_last_offset
> physical_rs
->rs_start
) {
458 zfs_dbgmsg("range write: vd %s changed (%llu, %llu) to "
459 "(%llu, %llu)", vd
->vdev_path
,
460 (u_longlong_t
)physical_rs
->rs_start
,
461 (u_longlong_t
)physical_rs
->rs_end
,
462 (u_longlong_t
)vd
->vdev_initialize_last_offset
,
463 (u_longlong_t
)physical_rs
->rs_end
);
464 ASSERT3U(physical_rs
->rs_end
, >,
465 vd
->vdev_initialize_last_offset
);
466 physical_rs
->rs_start
= vd
->vdev_initialize_last_offset
;
469 ASSERT3U(physical_rs
->rs_end
, >, physical_rs
->rs_start
);
471 range_tree_add(vd
->vdev_initialize_tree
, physical_rs
->rs_start
,
472 physical_rs
->rs_end
- physical_rs
->rs_start
);
476 * Convert the logical range into a physical range and add it to our
480 vdev_initialize_range_add(void *arg
, uint64_t start
, uint64_t size
)
483 range_seg64_t logical_rs
;
484 logical_rs
.rs_start
= start
;
485 logical_rs
.rs_end
= start
+ size
;
487 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
488 vdev_xlate_walk(vd
, &logical_rs
, vdev_initialize_xlate_range_add
, arg
);
491 static __attribute__((noreturn
)) void
492 vdev_initialize_thread(void *arg
)
495 spa_t
*spa
= vd
->vdev_spa
;
497 uint64_t ms_count
= 0;
499 ASSERT(vdev_is_concrete(vd
));
500 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
502 vd
->vdev_initialize_last_offset
= 0;
503 VERIFY0(vdev_initialize_load(vd
));
505 abd_t
*deadbeef
= vdev_initialize_block_alloc();
507 vd
->vdev_initialize_tree
= range_tree_create(NULL
, RANGE_SEG64
, NULL
,
510 for (uint64_t i
= 0; !vd
->vdev_detached
&&
511 i
< vd
->vdev_top
->vdev_ms_count
; i
++) {
512 metaslab_t
*msp
= vd
->vdev_top
->vdev_ms
[i
];
513 boolean_t unload_when_done
= B_FALSE
;
516 * If we've expanded the top-level vdev or it's our
517 * first pass, calculate our progress.
519 if (vd
->vdev_top
->vdev_ms_count
!= ms_count
) {
520 vdev_initialize_calculate_progress(vd
);
521 ms_count
= vd
->vdev_top
->vdev_ms_count
;
524 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
525 metaslab_disable(msp
);
526 mutex_enter(&msp
->ms_lock
);
527 if (!msp
->ms_loaded
&& !msp
->ms_loading
)
528 unload_when_done
= B_TRUE
;
529 VERIFY0(metaslab_load(msp
));
531 range_tree_walk(msp
->ms_allocatable
, vdev_initialize_range_add
,
533 mutex_exit(&msp
->ms_lock
);
535 error
= vdev_initialize_ranges(vd
, deadbeef
);
536 metaslab_enable(msp
, B_TRUE
, unload_when_done
);
537 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
539 range_tree_vacate(vd
->vdev_initialize_tree
, NULL
, NULL
);
544 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
545 mutex_enter(&vd
->vdev_initialize_io_lock
);
546 while (vd
->vdev_initialize_inflight
> 0) {
547 cv_wait(&vd
->vdev_initialize_io_cv
,
548 &vd
->vdev_initialize_io_lock
);
550 mutex_exit(&vd
->vdev_initialize_io_lock
);
552 range_tree_destroy(vd
->vdev_initialize_tree
);
553 vdev_initialize_block_free(deadbeef
);
554 vd
->vdev_initialize_tree
= NULL
;
556 mutex_enter(&vd
->vdev_initialize_lock
);
557 if (!vd
->vdev_initialize_exit_wanted
) {
558 if (vdev_writeable(vd
)) {
559 vdev_initialize_change_state(vd
,
560 VDEV_INITIALIZE_COMPLETE
);
561 } else if (vd
->vdev_faulted
) {
562 vdev_initialize_change_state(vd
,
563 VDEV_INITIALIZE_CANCELED
);
566 ASSERT(vd
->vdev_initialize_thread
!= NULL
||
567 vd
->vdev_initialize_inflight
== 0);
570 * Drop the vdev_initialize_lock while we sync out the
571 * txg since it's possible that a device might be trying to
572 * come online and must check to see if it needs to restart an
573 * initialization. That thread will be holding the spa_config_lock
574 * which would prevent the txg_wait_synced from completing.
576 mutex_exit(&vd
->vdev_initialize_lock
);
577 txg_wait_synced(spa_get_dsl(spa
), 0);
578 mutex_enter(&vd
->vdev_initialize_lock
);
580 vd
->vdev_initialize_thread
= NULL
;
581 cv_broadcast(&vd
->vdev_initialize_cv
);
582 mutex_exit(&vd
->vdev_initialize_lock
);
588 * Initiates a device. Caller must hold vdev_initialize_lock.
589 * Device must be a leaf and not already be initializing.
592 vdev_initialize(vdev_t
*vd
)
594 ASSERT(MUTEX_HELD(&vd
->vdev_initialize_lock
));
595 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
596 ASSERT(vdev_is_concrete(vd
));
597 ASSERT3P(vd
->vdev_initialize_thread
, ==, NULL
);
598 ASSERT(!vd
->vdev_detached
);
599 ASSERT(!vd
->vdev_initialize_exit_wanted
);
600 ASSERT(!vd
->vdev_top
->vdev_removing
);
602 vdev_initialize_change_state(vd
, VDEV_INITIALIZE_ACTIVE
);
603 vd
->vdev_initialize_thread
= thread_create(NULL
, 0,
604 vdev_initialize_thread
, vd
, 0, &p0
, TS_RUN
, maxclsyspri
);
608 * Wait for the initialize thread to be terminated (cancelled or stopped).
611 vdev_initialize_stop_wait_impl(vdev_t
*vd
)
613 ASSERT(MUTEX_HELD(&vd
->vdev_initialize_lock
));
615 while (vd
->vdev_initialize_thread
!= NULL
)
616 cv_wait(&vd
->vdev_initialize_cv
, &vd
->vdev_initialize_lock
);
618 ASSERT3P(vd
->vdev_initialize_thread
, ==, NULL
);
619 vd
->vdev_initialize_exit_wanted
= B_FALSE
;
623 * Wait for vdev initialize threads which were either to cleanly exit.
626 vdev_initialize_stop_wait(spa_t
*spa
, list_t
*vd_list
)
631 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
633 while ((vd
= list_remove_head(vd_list
)) != NULL
) {
634 mutex_enter(&vd
->vdev_initialize_lock
);
635 vdev_initialize_stop_wait_impl(vd
);
636 mutex_exit(&vd
->vdev_initialize_lock
);
641 * Stop initializing a device, with the resultant initializing state being
642 * tgt_state. For blocking behavior pass NULL for vd_list. Otherwise, when
643 * a list_t is provided the stopping vdev is inserted in to the list. Callers
644 * are then required to call vdev_initialize_stop_wait() to block for all the
645 * initialization threads to exit. The caller must hold vdev_initialize_lock
646 * and must not be writing to the spa config, as the initializing thread may
647 * try to enter the config as a reader before exiting.
650 vdev_initialize_stop(vdev_t
*vd
, vdev_initializing_state_t tgt_state
,
653 ASSERT(!spa_config_held(vd
->vdev_spa
, SCL_CONFIG
|SCL_STATE
, RW_WRITER
));
654 ASSERT(MUTEX_HELD(&vd
->vdev_initialize_lock
));
655 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
656 ASSERT(vdev_is_concrete(vd
));
659 * Allow cancel requests to proceed even if the initialize thread
662 if (vd
->vdev_initialize_thread
== NULL
&&
663 tgt_state
!= VDEV_INITIALIZE_CANCELED
) {
667 vdev_initialize_change_state(vd
, tgt_state
);
668 vd
->vdev_initialize_exit_wanted
= B_TRUE
;
670 if (vd_list
== NULL
) {
671 vdev_initialize_stop_wait_impl(vd
);
673 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
674 list_insert_tail(vd_list
, vd
);
679 vdev_initialize_stop_all_impl(vdev_t
*vd
, vdev_initializing_state_t tgt_state
,
682 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_concrete(vd
)) {
683 mutex_enter(&vd
->vdev_initialize_lock
);
684 vdev_initialize_stop(vd
, tgt_state
, vd_list
);
685 mutex_exit(&vd
->vdev_initialize_lock
);
689 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
690 vdev_initialize_stop_all_impl(vd
->vdev_child
[i
], tgt_state
,
696 * Convenience function to stop initializing of a vdev tree and set all
697 * initialize thread pointers to NULL.
700 vdev_initialize_stop_all(vdev_t
*vd
, vdev_initializing_state_t tgt_state
)
702 spa_t
*spa
= vd
->vdev_spa
;
705 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
707 list_create(&vd_list
, sizeof (vdev_t
),
708 offsetof(vdev_t
, vdev_initialize_node
));
710 vdev_initialize_stop_all_impl(vd
, tgt_state
, &vd_list
);
711 vdev_initialize_stop_wait(spa
, &vd_list
);
713 if (vd
->vdev_spa
->spa_sync_on
) {
714 /* Make sure that our state has been synced to disk */
715 txg_wait_synced(spa_get_dsl(vd
->vdev_spa
), 0);
718 list_destroy(&vd_list
);
722 vdev_initialize_restart(vdev_t
*vd
)
724 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
725 ASSERT(!spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
));
727 if (vd
->vdev_leaf_zap
!= 0) {
728 mutex_enter(&vd
->vdev_initialize_lock
);
729 uint64_t initialize_state
= VDEV_INITIALIZE_NONE
;
730 int err
= zap_lookup(vd
->vdev_spa
->spa_meta_objset
,
731 vd
->vdev_leaf_zap
, VDEV_LEAF_ZAP_INITIALIZE_STATE
,
732 sizeof (initialize_state
), 1, &initialize_state
);
733 ASSERT(err
== 0 || err
== ENOENT
);
734 vd
->vdev_initialize_state
= initialize_state
;
736 uint64_t timestamp
= 0;
737 err
= zap_lookup(vd
->vdev_spa
->spa_meta_objset
,
738 vd
->vdev_leaf_zap
, VDEV_LEAF_ZAP_INITIALIZE_ACTION_TIME
,
739 sizeof (timestamp
), 1, ×tamp
);
740 ASSERT(err
== 0 || err
== ENOENT
);
741 vd
->vdev_initialize_action_time
= timestamp
;
743 if (vd
->vdev_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
745 /* load progress for reporting, but don't resume */
746 VERIFY0(vdev_initialize_load(vd
));
747 } else if (vd
->vdev_initialize_state
==
748 VDEV_INITIALIZE_ACTIVE
&& vdev_writeable(vd
) &&
749 !vd
->vdev_top
->vdev_removing
&&
750 vd
->vdev_initialize_thread
== NULL
) {
754 mutex_exit(&vd
->vdev_initialize_lock
);
757 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
758 vdev_initialize_restart(vd
->vdev_child
[i
]);
762 EXPORT_SYMBOL(vdev_initialize
);
763 EXPORT_SYMBOL(vdev_initialize_stop
);
764 EXPORT_SYMBOL(vdev_initialize_stop_all
);
765 EXPORT_SYMBOL(vdev_initialize_stop_wait
);
766 EXPORT_SYMBOL(vdev_initialize_restart
);
768 ZFS_MODULE_PARAM(zfs
, zfs_
, initialize_value
, ULONG
, ZMOD_RW
,
769 "Value written during zpool initialize");
771 ZFS_MODULE_PARAM(zfs
, zfs_
, initialize_chunk_size
, ULONG
, ZMOD_RW
,
772 "Size in bytes of writes by zpool initialize");