xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_initialize.c (revision 7ec2f6bce5d28e6662c29e63f6ab6b7ef57d98b2)
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) 2016, 2019 by Delphix. All rights reserved.
24  */
25 
26 #include <sys/spa.h>
27 #include <sys/spa_impl.h>
28 #include <sys/txg.h>
29 #include <sys/vdev_impl.h>
30 #include <sys/metaslab_impl.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/zap.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/vdev_initialize.h>
35 
36 /*
37  * Value that is written to disk during initialization.
38  */
39 #ifdef _ILP32
40 unsigned long zfs_initialize_value = 0xdeadbeefUL;
41 #else
42 unsigned long zfs_initialize_value = 0xdeadbeefdeadbeeeULL;
43 #endif
44 
45 /* maximum number of I/Os outstanding per leaf vdev */
46 int zfs_initialize_limit = 1;
47 
48 /* size of initializing writes; default 1MiB, see zfs_remove_max_segment */
49 unsigned long zfs_initialize_chunk_size = 1024 * 1024;
50 
51 static boolean_t
52 vdev_initialize_should_stop(vdev_t *vd)
53 {
54 	return (vd->vdev_initialize_exit_wanted || !vdev_writeable(vd) ||
55 	    vd->vdev_detached || vd->vdev_top->vdev_removing);
56 }
57 
58 static void
59 vdev_initialize_zap_update_sync(void *arg, dmu_tx_t *tx)
60 {
61 	/*
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.
68 	 */
69 	uint64_t guid = *(uint64_t *)arg;
70 	uint64_t txg = dmu_tx_get_txg(tx);
71 	kmem_free(arg, sizeof (uint64_t));
72 
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))
75 		return;
76 
77 	uint64_t last_offset = vd->vdev_initialize_offset[txg & TXG_MASK];
78 	vd->vdev_initialize_offset[txg & TXG_MASK] = 0;
79 
80 	VERIFY(vd->vdev_leaf_zap != 0);
81 
82 	objset_t *mos = vd->vdev_spa->spa_meta_objset;
83 
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));
89 	}
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),
94 		    1, &val, tx));
95 	}
96 
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));
101 }
102 
103 static void
104 vdev_initialize_change_state(vdev_t *vd, vdev_initializing_state_t new_state)
105 {
106 	ASSERT(MUTEX_HELD(&vd->vdev_initialize_lock));
107 	spa_t *spa = vd->vdev_spa;
108 
109 	if (new_state == vd->vdev_initialize_state)
110 		return;
111 
112 	/*
113 	 * Copy the vd's guid, this will be freed by the sync task.
114 	 */
115 	uint64_t *guid = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
116 	*guid = vd->vdev_guid;
117 
118 	/*
119 	 * If we're suspending, then preserving the original start time.
120 	 */
121 	if (vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED) {
122 		vd->vdev_initialize_action_time = gethrestime_sec();
123 	}
124 	vd->vdev_initialize_state = new_state;
125 
126 	dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
127 	VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
128 	dsl_sync_task_nowait(spa_get_dsl(spa), vdev_initialize_zap_update_sync,
129 	    guid, tx);
130 
131 	switch (new_state) {
132 	case VDEV_INITIALIZE_ACTIVE:
133 		spa_history_log_internal(spa, "initialize", tx,
134 		    "vdev=%s activated", vd->vdev_path);
135 		break;
136 	case VDEV_INITIALIZE_SUSPENDED:
137 		spa_history_log_internal(spa, "initialize", tx,
138 		    "vdev=%s suspended", vd->vdev_path);
139 		break;
140 	case VDEV_INITIALIZE_CANCELED:
141 		spa_history_log_internal(spa, "initialize", tx,
142 		    "vdev=%s canceled", vd->vdev_path);
143 		break;
144 	case VDEV_INITIALIZE_COMPLETE:
145 		spa_history_log_internal(spa, "initialize", tx,
146 		    "vdev=%s complete", vd->vdev_path);
147 		break;
148 	default:
149 		panic("invalid state %llu", (unsigned long long)new_state);
150 	}
151 
152 	dmu_tx_commit(tx);
153 
154 	if (new_state != VDEV_INITIALIZE_ACTIVE)
155 		spa_notify_waiters(spa);
156 }
157 
158 static void
159 vdev_initialize_cb(zio_t *zio)
160 {
161 	vdev_t *vd = zio->io_vd;
162 	mutex_enter(&vd->vdev_initialize_io_lock);
163 	if (zio->io_error == ENXIO && !vdev_writeable(vd)) {
164 		/*
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.)
168 		 */
169 		uint64_t *off =
170 		    &vd->vdev_initialize_offset[zio->io_txg & TXG_MASK];
171 		*off = MIN(*off, zio->io_offset);
172 	} else {
173 		/*
174 		 * Since initializing is best-effort, we ignore I/O errors and
175 		 * rely on vdev_probe to determine if the errors are more
176 		 * critical.
177 		 */
178 		if (zio->io_error != 0)
179 			vd->vdev_stat.vs_initialize_errors++;
180 
181 		vd->vdev_initialize_bytes_done += zio->io_orig_size;
182 	}
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);
187 
188 	spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
189 }
190 
191 /* Takes care of physical writing and limiting # of concurrent ZIOs. */
192 static int
193 vdev_initialize_write(vdev_t *vd, uint64_t start, uint64_t size, abd_t *data)
194 {
195 	spa_t *spa = vd->vdev_spa;
196 
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);
202 	}
203 	vd->vdev_initialize_inflight++;
204 	mutex_exit(&vd->vdev_initialize_io_lock);
205 
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);
209 
210 	spa_config_enter(spa, SCL_STATE_ALL, vd, RW_READER);
211 	mutex_enter(&vd->vdev_initialize_lock);
212 
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;
216 
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);
220 	}
221 
222 	/*
223 	 * We know the vdev struct will still be around since all
224 	 * consumers of vdev_free must stop the initialization first.
225 	 */
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);
233 		dmu_tx_commit(tx);
234 		return (SET_ERROR(EINTR));
235 	}
236 	mutex_exit(&vd->vdev_initialize_lock);
237 
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 */
243 
244 	dmu_tx_commit(tx);
245 
246 	return (0);
247 }
248 
249 /*
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.
253  */
254 /* ARGSUSED */
255 static int
256 vdev_initialize_block_fill(void *buf, size_t len, void *unused)
257 {
258 	ASSERT0(len % sizeof (uint64_t));
259 #ifdef _ILP32
260 	for (uint64_t i = 0; i < len; i += sizeof (uint32_t)) {
261 		*(uint32_t *)((char *)(buf) + i) = zfs_initialize_value;
262 	}
263 #else
264 	for (uint64_t i = 0; i < len; i += sizeof (uint64_t)) {
265 		*(uint64_t *)((char *)(buf) + i) = zfs_initialize_value;
266 	}
267 #endif
268 	return (0);
269 }
270 
271 static abd_t *
272 vdev_initialize_block_alloc(void)
273 {
274 	/* Allocate ABD for filler data */
275 	abd_t *data = abd_alloc_for_io(zfs_initialize_chunk_size, B_FALSE);
276 
277 	ASSERT0(zfs_initialize_chunk_size % sizeof (uint64_t));
278 	(void) abd_iterate_func(data, 0, zfs_initialize_chunk_size,
279 	    vdev_initialize_block_fill, NULL);
280 
281 	return (data);
282 }
283 
284 static void
285 vdev_initialize_block_free(abd_t *data)
286 {
287 	abd_free(data);
288 }
289 
290 static int
291 vdev_initialize_ranges(vdev_t *vd, abd_t *data)
292 {
293 	range_tree_t *rt = vd->vdev_initialize_tree;
294 	zfs_btree_t *bt = &rt->rt_root;
295 	zfs_btree_index_t where;
296 
297 	for (range_seg_t *rs = zfs_btree_first(bt, &where); rs != NULL;
298 	    rs = zfs_btree_next(bt, &where, &where)) {
299 		uint64_t size = rs_get_end(rs, rt) - rs_get_start(rs, rt);
300 
301 		/* Split range into legally-sized physical chunks */
302 		uint64_t writes_required =
303 		    ((size - 1) / zfs_initialize_chunk_size) + 1;
304 
305 		for (uint64_t w = 0; w < writes_required; w++) {
306 			int error;
307 
308 			error = vdev_initialize_write(vd,
309 			    VDEV_LABEL_START_SIZE + rs_get_start(rs, rt) +
310 			    (w * zfs_initialize_chunk_size),
311 			    MIN(size - (w * zfs_initialize_chunk_size),
312 			    zfs_initialize_chunk_size), data);
313 			if (error != 0)
314 				return (error);
315 		}
316 	}
317 	return (0);
318 }
319 
320 static void
321 vdev_initialize_calculate_progress(vdev_t *vd)
322 {
323 	ASSERT(spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_READER) ||
324 	    spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_WRITER));
325 	ASSERT(vd->vdev_leaf_zap != 0);
326 
327 	vd->vdev_initialize_bytes_est = 0;
328 	vd->vdev_initialize_bytes_done = 0;
329 
330 	for (uint64_t i = 0; i < vd->vdev_top->vdev_ms_count; i++) {
331 		metaslab_t *msp = vd->vdev_top->vdev_ms[i];
332 		mutex_enter(&msp->ms_lock);
333 
334 		uint64_t ms_free = msp->ms_size -
335 		    metaslab_allocated_space(msp);
336 
337 		if (vd->vdev_top->vdev_ops == &vdev_raidz_ops)
338 			ms_free /= vd->vdev_top->vdev_children;
339 
340 		/*
341 		 * Convert the metaslab range to a physical range
342 		 * on our vdev. We use this to determine if we are
343 		 * in the middle of this metaslab range.
344 		 */
345 		range_seg64_t logical_rs, physical_rs;
346 		logical_rs.rs_start = msp->ms_start;
347 		logical_rs.rs_end = msp->ms_start + msp->ms_size;
348 		vdev_xlate(vd, &logical_rs, &physical_rs);
349 
350 		if (vd->vdev_initialize_last_offset <= physical_rs.rs_start) {
351 			vd->vdev_initialize_bytes_est += ms_free;
352 			mutex_exit(&msp->ms_lock);
353 			continue;
354 		} else if (vd->vdev_initialize_last_offset >
355 		    physical_rs.rs_end) {
356 			vd->vdev_initialize_bytes_done += ms_free;
357 			vd->vdev_initialize_bytes_est += ms_free;
358 			mutex_exit(&msp->ms_lock);
359 			continue;
360 		}
361 
362 		/*
363 		 * If we get here, we're in the middle of initializing this
364 		 * metaslab. Load it and walk the free tree for more accurate
365 		 * progress estimation.
366 		 */
367 		VERIFY0(metaslab_load(msp));
368 
369 		zfs_btree_index_t where;
370 		range_tree_t *rt = msp->ms_allocatable;
371 		for (range_seg_t *rs =
372 		    zfs_btree_first(&rt->rt_root, &where); rs;
373 		    rs = zfs_btree_next(&rt->rt_root, &where,
374 		    &where)) {
375 			logical_rs.rs_start = rs_get_start(rs, rt);
376 			logical_rs.rs_end = rs_get_end(rs, rt);
377 			vdev_xlate(vd, &logical_rs, &physical_rs);
378 
379 			uint64_t size = physical_rs.rs_end -
380 			    physical_rs.rs_start;
381 			vd->vdev_initialize_bytes_est += size;
382 			if (vd->vdev_initialize_last_offset >
383 			    physical_rs.rs_end) {
384 				vd->vdev_initialize_bytes_done += size;
385 			} else if (vd->vdev_initialize_last_offset >
386 			    physical_rs.rs_start &&
387 			    vd->vdev_initialize_last_offset <
388 			    physical_rs.rs_end) {
389 				vd->vdev_initialize_bytes_done +=
390 				    vd->vdev_initialize_last_offset -
391 				    physical_rs.rs_start;
392 			}
393 		}
394 		mutex_exit(&msp->ms_lock);
395 	}
396 }
397 
398 static int
399 vdev_initialize_load(vdev_t *vd)
400 {
401 	int err = 0;
402 	ASSERT(spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_READER) ||
403 	    spa_config_held(vd->vdev_spa, SCL_CONFIG, RW_WRITER));
404 	ASSERT(vd->vdev_leaf_zap != 0);
405 
406 	if (vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE ||
407 	    vd->vdev_initialize_state == VDEV_INITIALIZE_SUSPENDED) {
408 		err = zap_lookup(vd->vdev_spa->spa_meta_objset,
409 		    vd->vdev_leaf_zap, VDEV_LEAF_ZAP_INITIALIZE_LAST_OFFSET,
410 		    sizeof (vd->vdev_initialize_last_offset), 1,
411 		    &vd->vdev_initialize_last_offset);
412 		if (err == ENOENT) {
413 			vd->vdev_initialize_last_offset = 0;
414 			err = 0;
415 		}
416 	}
417 
418 	vdev_initialize_calculate_progress(vd);
419 	return (err);
420 }
421 
422 /*
423  * Convert the logical range into a physical range and add it to our
424  * avl tree.
425  */
426 static void
427 vdev_initialize_range_add(void *arg, uint64_t start, uint64_t size)
428 {
429 	vdev_t *vd = arg;
430 	range_seg64_t logical_rs, physical_rs;
431 	logical_rs.rs_start = start;
432 	logical_rs.rs_end = start + size;
433 
434 	ASSERT(vd->vdev_ops->vdev_op_leaf);
435 	vdev_xlate(vd, &logical_rs, &physical_rs);
436 
437 	IMPLY(vd->vdev_top == vd,
438 	    logical_rs.rs_start == physical_rs.rs_start);
439 	IMPLY(vd->vdev_top == vd,
440 	    logical_rs.rs_end == physical_rs.rs_end);
441 
442 	/* Only add segments that we have not visited yet */
443 	if (physical_rs.rs_end <= vd->vdev_initialize_last_offset)
444 		return;
445 
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;
457 	}
458 	ASSERT3U(physical_rs.rs_end, >=, physical_rs.rs_start);
459 
460 	/*
461 	 * With raidz, it's possible that the logical range does not live on
462 	 * this leaf vdev. We only add the physical range to this vdev's if it
463 	 * has a length greater than 0.
464 	 */
465 	if (physical_rs.rs_end > physical_rs.rs_start) {
466 		range_tree_add(vd->vdev_initialize_tree, physical_rs.rs_start,
467 		    physical_rs.rs_end - physical_rs.rs_start);
468 	} else {
469 		ASSERT3U(physical_rs.rs_end, ==, physical_rs.rs_start);
470 	}
471 }
472 
473 static void
474 vdev_initialize_thread(void *arg)
475 {
476 	vdev_t *vd = arg;
477 	spa_t *spa = vd->vdev_spa;
478 	int error = 0;
479 	uint64_t ms_count = 0;
480 
481 	ASSERT(vdev_is_concrete(vd));
482 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
483 
484 	vd->vdev_initialize_last_offset = 0;
485 	VERIFY0(vdev_initialize_load(vd));
486 
487 	abd_t *deadbeef = vdev_initialize_block_alloc();
488 
489 	vd->vdev_initialize_tree = range_tree_create(NULL, RANGE_SEG64, NULL,
490 	    0, 0);
491 
492 	for (uint64_t i = 0; !vd->vdev_detached &&
493 	    i < vd->vdev_top->vdev_ms_count; i++) {
494 		metaslab_t *msp = vd->vdev_top->vdev_ms[i];
495 		boolean_t unload_when_done = B_FALSE;
496 
497 		/*
498 		 * If we've expanded the top-level vdev or it's our
499 		 * first pass, calculate our progress.
500 		 */
501 		if (vd->vdev_top->vdev_ms_count != ms_count) {
502 			vdev_initialize_calculate_progress(vd);
503 			ms_count = vd->vdev_top->vdev_ms_count;
504 		}
505 
506 		spa_config_exit(spa, SCL_CONFIG, FTAG);
507 		metaslab_disable(msp);
508 		mutex_enter(&msp->ms_lock);
509 		if (!msp->ms_loaded && !msp->ms_loading)
510 			unload_when_done = B_TRUE;
511 		VERIFY0(metaslab_load(msp));
512 
513 		range_tree_walk(msp->ms_allocatable, vdev_initialize_range_add,
514 		    vd);
515 		mutex_exit(&msp->ms_lock);
516 
517 		error = vdev_initialize_ranges(vd, deadbeef);
518 		metaslab_enable(msp, B_TRUE, unload_when_done);
519 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
520 
521 		range_tree_vacate(vd->vdev_initialize_tree, NULL, NULL);
522 		if (error != 0)
523 			break;
524 	}
525 
526 	spa_config_exit(spa, SCL_CONFIG, FTAG);
527 	mutex_enter(&vd->vdev_initialize_io_lock);
528 	while (vd->vdev_initialize_inflight > 0) {
529 		cv_wait(&vd->vdev_initialize_io_cv,
530 		    &vd->vdev_initialize_io_lock);
531 	}
532 	mutex_exit(&vd->vdev_initialize_io_lock);
533 
534 	range_tree_destroy(vd->vdev_initialize_tree);
535 	vdev_initialize_block_free(deadbeef);
536 	vd->vdev_initialize_tree = NULL;
537 
538 	mutex_enter(&vd->vdev_initialize_lock);
539 	if (!vd->vdev_initialize_exit_wanted && vdev_writeable(vd)) {
540 		vdev_initialize_change_state(vd, VDEV_INITIALIZE_COMPLETE);
541 	}
542 	ASSERT(vd->vdev_initialize_thread != NULL ||
543 	    vd->vdev_initialize_inflight == 0);
544 
545 	/*
546 	 * Drop the vdev_initialize_lock while we sync out the
547 	 * txg since it's possible that a device might be trying to
548 	 * come online and must check to see if it needs to restart an
549 	 * initialization. That thread will be holding the spa_config_lock
550 	 * which would prevent the txg_wait_synced from completing.
551 	 */
552 	mutex_exit(&vd->vdev_initialize_lock);
553 	txg_wait_synced(spa_get_dsl(spa), 0);
554 	mutex_enter(&vd->vdev_initialize_lock);
555 
556 	vd->vdev_initialize_thread = NULL;
557 	cv_broadcast(&vd->vdev_initialize_cv);
558 	mutex_exit(&vd->vdev_initialize_lock);
559 
560 	thread_exit();
561 }
562 
563 /*
564  * Initiates a device. Caller must hold vdev_initialize_lock.
565  * Device must be a leaf and not already be initializing.
566  */
567 void
568 vdev_initialize(vdev_t *vd)
569 {
570 	ASSERT(MUTEX_HELD(&vd->vdev_initialize_lock));
571 	ASSERT(vd->vdev_ops->vdev_op_leaf);
572 	ASSERT(vdev_is_concrete(vd));
573 	ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
574 	ASSERT(!vd->vdev_detached);
575 	ASSERT(!vd->vdev_initialize_exit_wanted);
576 	ASSERT(!vd->vdev_top->vdev_removing);
577 
578 	vdev_initialize_change_state(vd, VDEV_INITIALIZE_ACTIVE);
579 	vd->vdev_initialize_thread = thread_create(NULL, 0,
580 	    vdev_initialize_thread, vd, 0, &p0, TS_RUN, maxclsyspri);
581 }
582 
583 /*
584  * Wait for the initialize thread to be terminated (cancelled or stopped).
585  */
586 static void
587 vdev_initialize_stop_wait_impl(vdev_t *vd)
588 {
589 	ASSERT(MUTEX_HELD(&vd->vdev_initialize_lock));
590 
591 	while (vd->vdev_initialize_thread != NULL)
592 		cv_wait(&vd->vdev_initialize_cv, &vd->vdev_initialize_lock);
593 
594 	ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
595 	vd->vdev_initialize_exit_wanted = B_FALSE;
596 }
597 
598 /*
599  * Wait for vdev initialize threads which were either to cleanly exit.
600  */
601 void
602 vdev_initialize_stop_wait(spa_t *spa, list_t *vd_list)
603 {
604 	vdev_t *vd;
605 
606 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
607 
608 	while ((vd = list_remove_head(vd_list)) != NULL) {
609 		mutex_enter(&vd->vdev_initialize_lock);
610 		vdev_initialize_stop_wait_impl(vd);
611 		mutex_exit(&vd->vdev_initialize_lock);
612 	}
613 }
614 
615 /*
616  * Stop initializing a device, with the resultant initializing state being
617  * tgt_state.  For blocking behavior pass NULL for vd_list.  Otherwise, when
618  * a list_t is provided the stopping vdev is inserted in to the list.  Callers
619  * are then required to call vdev_initialize_stop_wait() to block for all the
620  * initialization threads to exit.  The caller must hold vdev_initialize_lock
621  * and must not be writing to the spa config, as the initializing thread may
622  * try to enter the config as a reader before exiting.
623  */
624 void
625 vdev_initialize_stop(vdev_t *vd, vdev_initializing_state_t tgt_state,
626     list_t *vd_list)
627 {
628 	ASSERT(!spa_config_held(vd->vdev_spa, SCL_CONFIG|SCL_STATE, RW_WRITER));
629 	ASSERT(MUTEX_HELD(&vd->vdev_initialize_lock));
630 	ASSERT(vd->vdev_ops->vdev_op_leaf);
631 	ASSERT(vdev_is_concrete(vd));
632 
633 	/*
634 	 * Allow cancel requests to proceed even if the initialize thread
635 	 * has stopped.
636 	 */
637 	if (vd->vdev_initialize_thread == NULL &&
638 	    tgt_state != VDEV_INITIALIZE_CANCELED) {
639 		return;
640 	}
641 
642 	vdev_initialize_change_state(vd, tgt_state);
643 	vd->vdev_initialize_exit_wanted = B_TRUE;
644 
645 	if (vd_list == NULL) {
646 		vdev_initialize_stop_wait_impl(vd);
647 	} else {
648 		ASSERT(MUTEX_HELD(&spa_namespace_lock));
649 		list_insert_tail(vd_list, vd);
650 	}
651 }
652 
653 static void
654 vdev_initialize_stop_all_impl(vdev_t *vd, vdev_initializing_state_t tgt_state,
655     list_t *vd_list)
656 {
657 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_concrete(vd)) {
658 		mutex_enter(&vd->vdev_initialize_lock);
659 		vdev_initialize_stop(vd, tgt_state, vd_list);
660 		mutex_exit(&vd->vdev_initialize_lock);
661 		return;
662 	}
663 
664 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
665 		vdev_initialize_stop_all_impl(vd->vdev_child[i], tgt_state,
666 		    vd_list);
667 	}
668 }
669 
670 /*
671  * Convenience function to stop initializing of a vdev tree and set all
672  * initialize thread pointers to NULL.
673  */
674 void
675 vdev_initialize_stop_all(vdev_t *vd, vdev_initializing_state_t tgt_state)
676 {
677 	spa_t *spa = vd->vdev_spa;
678 	list_t vd_list;
679 
680 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
681 
682 	list_create(&vd_list, sizeof (vdev_t),
683 	    offsetof(vdev_t, vdev_initialize_node));
684 
685 	vdev_initialize_stop_all_impl(vd, tgt_state, &vd_list);
686 	vdev_initialize_stop_wait(spa, &vd_list);
687 
688 	if (vd->vdev_spa->spa_sync_on) {
689 		/* Make sure that our state has been synced to disk */
690 		txg_wait_synced(spa_get_dsl(vd->vdev_spa), 0);
691 	}
692 
693 	list_destroy(&vd_list);
694 }
695 
696 void
697 vdev_initialize_restart(vdev_t *vd)
698 {
699 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
700 	ASSERT(!spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
701 
702 	if (vd->vdev_leaf_zap != 0) {
703 		mutex_enter(&vd->vdev_initialize_lock);
704 		uint64_t initialize_state = VDEV_INITIALIZE_NONE;
705 		int err = zap_lookup(vd->vdev_spa->spa_meta_objset,
706 		    vd->vdev_leaf_zap, VDEV_LEAF_ZAP_INITIALIZE_STATE,
707 		    sizeof (initialize_state), 1, &initialize_state);
708 		ASSERT(err == 0 || err == ENOENT);
709 		vd->vdev_initialize_state = initialize_state;
710 
711 		uint64_t timestamp = 0;
712 		err = zap_lookup(vd->vdev_spa->spa_meta_objset,
713 		    vd->vdev_leaf_zap, VDEV_LEAF_ZAP_INITIALIZE_ACTION_TIME,
714 		    sizeof (timestamp), 1, &timestamp);
715 		ASSERT(err == 0 || err == ENOENT);
716 		vd->vdev_initialize_action_time = timestamp;
717 
718 		if (vd->vdev_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
719 		    vd->vdev_offline) {
720 			/* load progress for reporting, but don't resume */
721 			VERIFY0(vdev_initialize_load(vd));
722 		} else if (vd->vdev_initialize_state ==
723 		    VDEV_INITIALIZE_ACTIVE && vdev_writeable(vd) &&
724 		    !vd->vdev_top->vdev_removing &&
725 		    vd->vdev_initialize_thread == NULL) {
726 			vdev_initialize(vd);
727 		}
728 
729 		mutex_exit(&vd->vdev_initialize_lock);
730 	}
731 
732 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
733 		vdev_initialize_restart(vd->vdev_child[i]);
734 	}
735 }
736 
737 EXPORT_SYMBOL(vdev_initialize);
738 EXPORT_SYMBOL(vdev_initialize_stop);
739 EXPORT_SYMBOL(vdev_initialize_stop_all);
740 EXPORT_SYMBOL(vdev_initialize_stop_wait);
741 EXPORT_SYMBOL(vdev_initialize_restart);
742 
743 /* BEGIN CSTYLED */
744 ZFS_MODULE_PARAM(zfs, zfs_, initialize_value, ULONG, ZMOD_RW,
745 	"Value written during zpool initialize");
746 
747 ZFS_MODULE_PARAM(zfs, zfs_, initialize_chunk_size, ULONG, ZMOD_RW,
748 	"Size in bytes of writes by zpool initialize");
749 /* END CSTYLED */
750