xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/removal/removal.kshlib (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1# SPDX-License-Identifier: CDDL-1.0
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2014, 2017 by Delphix. All rights reserved.
19#
20
21export REMOVEDISK=${DISKS%% *}
22export NOTREMOVEDISK=${DISKS##* }
23
24#
25# Waits for the pool to finish a removal.
26#
27function wait_for_removal # pool
28{
29	typeset pool=$1
30	typeset callback=$2
31
32	log_must zpool wait -t remove $pool
33
34	#
35	# The pool state changes before the TXG finishes syncing; wait for
36	# the removal to be completed on disk.
37	#
38	sync_pool $pool
39
40	log_must is_pool_removed $pool
41	return 0
42}
43
44#
45# Removes the specified disk from its respective pool and
46# runs the callback while the removal is in progress.
47#
48# This function is mainly used to test how other operations
49# interact with device removal. After the callback is done,
50# the removal is unpaused and we wait for it to finish.
51#
52# Example usage:
53#
54#    attempt_during_removal $TESTPOOL $DISK dd if=/dev/urandom \
55#        of=/$TESTPOOL/file count=1
56#
57function attempt_during_removal # pool disk callback [args]
58{
59	typeset pool=$1
60	typeset disk=$2
61	typeset callback=$3
62
63	shift 3
64	log_onexit_push set_tunable32 REMOVAL_SUSPEND_PROGRESS 0
65	set_tunable32 REMOVAL_SUSPEND_PROGRESS 1
66
67	log_must zpool remove $pool $disk
68
69	#
70	# We want to make sure that the removal started
71	# before issuing the callback.
72	#
73	sync_pool $pool
74	log_must is_pool_removing $pool
75
76	log_must $callback "$@"
77
78	#
79	# Ensure that we still haven't finished the removal
80	# as expected.
81	#
82	log_must is_pool_removing $pool
83
84	set_tunable32 REMOVAL_SUSPEND_PROGRESS 0
85	log_onexit_pop
86
87	log_must wait_for_removal $pool
88	log_mustnot vdevs_in_pool $pool $disk
89	return 0
90}
91
92function random_write # file write_size
93{
94	typeset file=$1
95	typeset block_size=$2
96	typeset file_size=$(stat_size $file 2>/dev/null)
97	typeset nblocks=$((file_size / block_size))
98
99	[[ -w $file ]] || return 1
100
101	dd if=/dev/urandom of=$file conv=notrunc \
102	    bs=$block_size count=1 seek=$((RANDOM % nblocks)) >/dev/null 2>&1
103}
104
105function start_random_writer # file
106{
107	typeset file=$1
108	(
109		log_note "Starting writer for $file"
110		# This will fail when we destroy the pool.
111		while random_write $file $((2**12)); do
112			:
113		done
114		log_note "Stopping writer for $file"
115	) &
116}
117
118function test_removal_with_operation # callback [args]
119{
120	#
121	# To ensure that the removal takes a while, we fragment the pool
122	# by writing random blocks and continue to do during the removal.
123	#
124	log_must mkfile 1g $TESTDIR/$TESTFILE0
125	for i in $(seq $((2**10))); do
126		random_write $TESTDIR/$TESTFILE0 $((2**12)) || \
127		    log_fail "Could not write to $TESTDIR/$TESTFILE0."
128	done
129	start_random_writer $TESTDIR/$TESTFILE0 1g
130	killpid=$!
131
132	log_must attempt_during_removal $TESTPOOL $REMOVEDISK "$@"
133	log_mustnot vdevs_in_pool $TESTPOOL $REMOVEDISK
134	log_must zdb -cd $TESTPOOL
135
136	kill $killpid
137	wait
138
139	verify_pool $TESTPOOL
140}
141
142#
143# Kill the background job use by the test_removal_with_operation function.
144#
145function test_removal_with_operation_kill
146{
147	kill $killpid
148	wait $killpid
149	return 0
150}
151