15cabbc6bSPrashanth Sreenivasa#
25cabbc6bSPrashanth Sreenivasa# CDDL HEADER START
35cabbc6bSPrashanth Sreenivasa#
45cabbc6bSPrashanth Sreenivasa# This file and its contents are supplied under the terms of the
55cabbc6bSPrashanth Sreenivasa# Common Development and Distribution License ("CDDL"), version 1.0.
65cabbc6bSPrashanth Sreenivasa# You may only use this file in accordance with the terms of version
75cabbc6bSPrashanth Sreenivasa# 1.0 of the CDDL.
85cabbc6bSPrashanth Sreenivasa#
95cabbc6bSPrashanth Sreenivasa# A full copy of the text of the CDDL should have accompanied this
105cabbc6bSPrashanth Sreenivasa# source.  A copy of the CDDL is also available via the Internet at
115cabbc6bSPrashanth Sreenivasa# http://www.illumos.org/license/CDDL.
125cabbc6bSPrashanth Sreenivasa#
135cabbc6bSPrashanth Sreenivasa# CDDL HEADER END
145cabbc6bSPrashanth Sreenivasa#
155cabbc6bSPrashanth Sreenivasa
165cabbc6bSPrashanth Sreenivasa#
1786714001SSerapheim Dimitropoulos# Copyright (c) 2014, 2017 by Delphix. All rights reserved.
185cabbc6bSPrashanth Sreenivasa#
195cabbc6bSPrashanth Sreenivasa
205cabbc6bSPrashanth Sreenivasaexport REMOVEDISK=${DISKS%% *}
215cabbc6bSPrashanth Sreenivasaexport NOTREMOVEDISK=${DISKS##* }
225cabbc6bSPrashanth Sreenivasa
235cabbc6bSPrashanth Sreenivasa#
2486714001SSerapheim Dimitropoulos# Waits for the pool to finish a removal.
255cabbc6bSPrashanth Sreenivasa#
2686714001SSerapheim Dimitropoulosfunction wait_for_removal # pool
275cabbc6bSPrashanth Sreenivasa{
285cabbc6bSPrashanth Sreenivasa	typeset pool=$1
295cabbc6bSPrashanth Sreenivasa	typeset callback=$2
305cabbc6bSPrashanth Sreenivasa
315cabbc6bSPrashanth Sreenivasa	while is_pool_removing $pool; do
3286714001SSerapheim Dimitropoulos		sleep 1
335cabbc6bSPrashanth Sreenivasa	done
345cabbc6bSPrashanth Sreenivasa
355cabbc6bSPrashanth Sreenivasa	#
365cabbc6bSPrashanth Sreenivasa	# The pool state changes before the TXG finishes syncing; wait for
375cabbc6bSPrashanth Sreenivasa	# the removal to be completed on disk.
385cabbc6bSPrashanth Sreenivasa	#
395cabbc6bSPrashanth Sreenivasa	sync
405cabbc6bSPrashanth Sreenivasa
415cabbc6bSPrashanth Sreenivasa	log_must is_pool_removed $pool
425cabbc6bSPrashanth Sreenivasa	return 0
435cabbc6bSPrashanth Sreenivasa}
445cabbc6bSPrashanth Sreenivasa
4586714001SSerapheim Dimitropoulos#
4686714001SSerapheim Dimitropoulos# Removes the specified disk from its respective pool and
4786714001SSerapheim Dimitropoulos# runs the callback while the removal is in progress.
4886714001SSerapheim Dimitropoulos#
4986714001SSerapheim Dimitropoulos# This function is mainly used to test how other operations
5086714001SSerapheim Dimitropoulos# interact with device removal. After the callback is done,
5186714001SSerapheim Dimitropoulos# the removal is unpaused and we wait for it to finish.
5286714001SSerapheim Dimitropoulos#
5386714001SSerapheim Dimitropoulos# Example usage:
5486714001SSerapheim Dimitropoulos#
5586714001SSerapheim Dimitropoulos#    attempt_during_removal $TESTPOOL $DISK dd if=/dev/urandom \
5686714001SSerapheim Dimitropoulos#        of=/$TESTPOOL/file count=1
5786714001SSerapheim Dimitropoulos#
5886714001SSerapheim Dimitropoulosfunction attempt_during_removal # pool disk callback [args]
5986714001SSerapheim Dimitropoulos{
6086714001SSerapheim Dimitropoulos	typeset pool=$1
6186714001SSerapheim Dimitropoulos	typeset disk=$2
6286714001SSerapheim Dimitropoulos	typeset callback=$3
6386714001SSerapheim Dimitropoulos
6486714001SSerapheim Dimitropoulos	shift 3
65e4c795beSTom Caputi	mdb_ctf_set_int zfs_removal_suspend_progress 0t1
6686714001SSerapheim Dimitropoulos
6786714001SSerapheim Dimitropoulos	log_must zpool remove $pool $disk
6886714001SSerapheim Dimitropoulos
6986714001SSerapheim Dimitropoulos	#
7086714001SSerapheim Dimitropoulos	# We want to make sure that the removal started
7186714001SSerapheim Dimitropoulos	# before issuing the callback.
7286714001SSerapheim Dimitropoulos	#
73*09fbbb7dSAllan Jude	sync_pool $pool
7486714001SSerapheim Dimitropoulos	log_must is_pool_removing $pool
7586714001SSerapheim Dimitropoulos
7686714001SSerapheim Dimitropoulos	log_must $callback "$@"
7786714001SSerapheim Dimitropoulos
7886714001SSerapheim Dimitropoulos	#
7986714001SSerapheim Dimitropoulos	# Ensure that we still haven't finished the removal
8086714001SSerapheim Dimitropoulos	# as expected.
8186714001SSerapheim Dimitropoulos	#
8286714001SSerapheim Dimitropoulos	log_must is_pool_removing $pool
8386714001SSerapheim Dimitropoulos
84e4c795beSTom Caputi	mdb_ctf_set_int zfs_removal_suspend_progress 0t0
8586714001SSerapheim Dimitropoulos
8686714001SSerapheim Dimitropoulos	log_must wait_for_removal $pool
8786714001SSerapheim Dimitropoulos	log_mustnot vdevs_in_pool $pool $disk
8886714001SSerapheim Dimitropoulos	return 0
8986714001SSerapheim Dimitropoulos}
9086714001SSerapheim Dimitropoulos
915cabbc6bSPrashanth Sreenivasafunction indirect_vdev_mapping_size # pool
925cabbc6bSPrashanth Sreenivasa{
935cabbc6bSPrashanth Sreenivasa	typeset pool=$1
945cabbc6bSPrashanth Sreenivasa	zdb -P $pool | grep 'indirect vdev' | \
955cabbc6bSPrashanth Sreenivasa	    sed -E 's/.*\(([0-9]+) in memory\).*/\1/g'
965cabbc6bSPrashanth Sreenivasa}
975cabbc6bSPrashanth Sreenivasa
985cabbc6bSPrashanth Sreenivasafunction random_write # file write_size
995cabbc6bSPrashanth Sreenivasa{
1005cabbc6bSPrashanth Sreenivasa	typeset file=$1
1015cabbc6bSPrashanth Sreenivasa	typeset block_size=$2
1025cabbc6bSPrashanth Sreenivasa	typeset file_size=$(stat -c%s $file 2>/dev/null)
1035cabbc6bSPrashanth Sreenivasa	typeset nblocks=$((file_size / block_size))
1045cabbc6bSPrashanth Sreenivasa
1055cabbc6bSPrashanth Sreenivasa	[[ -w $file ]] || return 1
1065cabbc6bSPrashanth Sreenivasa
1075cabbc6bSPrashanth Sreenivasa	dd if=/dev/urandom of=$file conv=notrunc \
1085cabbc6bSPrashanth Sreenivasa	    bs=$block_size count=1 seek=$((RANDOM % nblocks)) >/dev/null 2>&1
1095cabbc6bSPrashanth Sreenivasa}
1105cabbc6bSPrashanth Sreenivasa
1115cabbc6bSPrashanth Sreenivasafunction start_random_writer # file
1125cabbc6bSPrashanth Sreenivasa{
1135cabbc6bSPrashanth Sreenivasa	typeset file=$1
1145cabbc6bSPrashanth Sreenivasa	(
1155cabbc6bSPrashanth Sreenivasa		log_note "Starting writer for $file"
1165cabbc6bSPrashanth Sreenivasa		# This will fail when we destroy the pool.
1175cabbc6bSPrashanth Sreenivasa		while random_write $file $((2**12)); do
1185cabbc6bSPrashanth Sreenivasa			:
1195cabbc6bSPrashanth Sreenivasa		done
1205cabbc6bSPrashanth Sreenivasa		log_note "Stopping writer for $file"
1215cabbc6bSPrashanth Sreenivasa	) &
1225cabbc6bSPrashanth Sreenivasa}
1235cabbc6bSPrashanth Sreenivasa
1245cabbc6bSPrashanth Sreenivasafunction set_min_bytes # bytes
1255cabbc6bSPrashanth Sreenivasa{
1265cabbc6bSPrashanth Sreenivasa	typeset bytes=$1
1275cabbc6bSPrashanth Sreenivasa	echo "zfs_condense_min_mapping_bytes/W 0t$bytes" | \
1285cabbc6bSPrashanth Sreenivasa	    mdb -kw
1295cabbc6bSPrashanth Sreenivasa}
1305cabbc6bSPrashanth Sreenivasa
13186714001SSerapheim Dimitropoulosfunction test_removal_with_operation # callback [args]
1325cabbc6bSPrashanth Sreenivasa{
1335cabbc6bSPrashanth Sreenivasa	#
1345cabbc6bSPrashanth Sreenivasa	# To ensure that the removal takes a while, we fragment the pool
1355cabbc6bSPrashanth Sreenivasa	# by writing random blocks and continue to do during the removal.
1365cabbc6bSPrashanth Sreenivasa	#
1375cabbc6bSPrashanth Sreenivasa	log_must mkfile 1g $TESTDIR/$TESTFILE0
1385cabbc6bSPrashanth Sreenivasa	for i in $(seq $((2**10))); do
1395cabbc6bSPrashanth Sreenivasa		random_write $TESTDIR/$TESTFILE0 $((2**12)) || \
1405cabbc6bSPrashanth Sreenivasa		    log_fail "Could not write to $TESTDIR/$TESTFILE0."
1415cabbc6bSPrashanth Sreenivasa	done
1425cabbc6bSPrashanth Sreenivasa	start_random_writer $TESTDIR/$TESTFILE0 1g
1435cabbc6bSPrashanth Sreenivasa	killpid=$!
1445cabbc6bSPrashanth Sreenivasa
14586714001SSerapheim Dimitropoulos	log_must attempt_during_removal $TESTPOOL $REMOVEDISK "$@"
1465cabbc6bSPrashanth Sreenivasa	log_mustnot vdevs_in_pool $TESTPOOL $REMOVEDISK
1475cabbc6bSPrashanth Sreenivasa	log_must zdb -cd $TESTPOOL
1485cabbc6bSPrashanth Sreenivasa
1495cabbc6bSPrashanth Sreenivasa	kill $killpid
1505cabbc6bSPrashanth Sreenivasa	wait
1515cabbc6bSPrashanth Sreenivasa}
152