1eda14cbcSMatt Macy#!/bin/ksh -p
2eda14cbcSMatt Macy#
3eda14cbcSMatt Macy# CDDL HEADER START
4eda14cbcSMatt Macy#
5eda14cbcSMatt Macy# The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy# Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy# You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy#
9eda14cbcSMatt Macy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska# or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy# See the License for the specific language governing permissions
12eda14cbcSMatt Macy# and limitations under the License.
13eda14cbcSMatt Macy#
14eda14cbcSMatt Macy# When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy# If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy# fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy# information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy#
20eda14cbcSMatt Macy# CDDL HEADER END
21eda14cbcSMatt Macy#
22eda14cbcSMatt Macy
23eda14cbcSMatt Macy#
24eda14cbcSMatt Macy# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25eda14cbcSMatt Macy# Use is subject to license terms.
26eda14cbcSMatt Macy#
27eda14cbcSMatt Macy
28eda14cbcSMatt Macy#
29eda14cbcSMatt Macy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
30eda14cbcSMatt Macy# Copyright (c) 2020 by Lawrence Livermore National Security, LLC.
31eda14cbcSMatt Macy#
32eda14cbcSMatt Macy
33eda14cbcSMatt Macy. $STF_SUITE/include/libtest.shlib
34eda14cbcSMatt Macy. $STF_SUITE/tests/functional/replacement/replacement.cfg
35eda14cbcSMatt Macy
36eda14cbcSMatt Macy#
37eda14cbcSMatt Macy# DESCRIPTION:
38eda14cbcSMatt Macy# 	Replacing disks during I/O should pass for supported pools.
39eda14cbcSMatt Macy#
40eda14cbcSMatt Macy# STRATEGY:
417877fdebSMatt Macy#	1. Create multidisk pools (stripe/mirror/draid) and
42eda14cbcSMatt Macy#	   start some random I/O
43eda14cbcSMatt Macy#	2. Replace a disk in the pool with another disk.
44eda14cbcSMatt Macy#	3. Verify the integrity of the file system and the rebuilding.
45eda14cbcSMatt Macy#
46eda14cbcSMatt Macy# NOTE: Raidz does not support the sequential resilver (-s) option.
47eda14cbcSMatt Macy#
48eda14cbcSMatt Macy
49eda14cbcSMatt Macyverify_runnable "global"
50eda14cbcSMatt Macy
51eda14cbcSMatt Macyfunction cleanup
52eda14cbcSMatt Macy{
53eda14cbcSMatt Macy	if [[ -n "$child_pids" ]]; then
54eda14cbcSMatt Macy		for wait_pid in $child_pids
55eda14cbcSMatt Macy		do
56eda14cbcSMatt Macy			kill $wait_pid
57eda14cbcSMatt Macy		done
58eda14cbcSMatt Macy	fi
59eda14cbcSMatt Macy
60eda14cbcSMatt Macy	if poolexists $TESTPOOL1; then
61eda14cbcSMatt Macy		destroy_pool $TESTPOOL1
62eda14cbcSMatt Macy	fi
63eda14cbcSMatt Macy
64eda14cbcSMatt Macy	[[ -e $TESTDIR ]] && log_must rm -rf $TESTDIR/*
65eda14cbcSMatt Macy}
66eda14cbcSMatt Macy
67eda14cbcSMatt Macylog_assert "Replacing a disk with -r during I/O completes."
68eda14cbcSMatt Macy
69eda14cbcSMatt Macyoptions=""
70eda14cbcSMatt Macyoptions_display="default options"
71eda14cbcSMatt Macy
72eda14cbcSMatt Macylog_onexit cleanup
73eda14cbcSMatt Macy
74eda14cbcSMatt Macy[[ -n "$HOLES_FILESIZE" ]] && options=" $options -f $HOLES_FILESIZE "
75eda14cbcSMatt Macy
76eda14cbcSMatt Macy[[ -n "$HOLES_BLKSIZE" ]] && options="$options -b $HOLES_BLKSIZE "
77eda14cbcSMatt Macy
78eda14cbcSMatt Macy[[ -n "$HOLES_COUNT" ]] && options="$options -c $HOLES_COUNT "
79eda14cbcSMatt Macy
80eda14cbcSMatt Macy[[ -n "$HOLES_SEED" ]] && options="$options -s $HOLES_SEED "
81eda14cbcSMatt Macy
82eda14cbcSMatt Macy[[ -n "$HOLES_FILEOFFSET" ]] && options="$options -o $HOLES_FILEOFFSET "
83eda14cbcSMatt Macy
84eda14cbcSMatt Macyoptions="$options -r "
85eda14cbcSMatt Macy
86eda14cbcSMatt Macy[[ -n "$options" ]] && options_display=$options
87eda14cbcSMatt Macy
88eda14cbcSMatt Macychild_pids=""
89eda14cbcSMatt Macy
90eda14cbcSMatt Macyfunction replace_test
91eda14cbcSMatt Macy{
92eda14cbcSMatt Macy	typeset -i iters=2
93eda14cbcSMatt Macy	typeset -i index=0
94eda14cbcSMatt Macy	typeset opt=$1
95eda14cbcSMatt Macy	typeset disk1=$2
96eda14cbcSMatt Macy	typeset disk2=$3
97eda14cbcSMatt Macy
98eda14cbcSMatt Macy	typeset i=0
99eda14cbcSMatt Macy	while [[ $i -lt $iters ]]; do
100eda14cbcSMatt Macy		log_note "Invoking file_trunc with: $options_display"
101eda14cbcSMatt Macy		file_trunc $options $TESTDIR/$TESTFILE.$i &
102eda14cbcSMatt Macy		typeset pid=$!
103eda14cbcSMatt Macy
104eda14cbcSMatt Macy		sleep 1
105eda14cbcSMatt Macy
106eda14cbcSMatt Macy		child_pids="$child_pids $pid"
107eda14cbcSMatt Macy		((i = i + 1))
108eda14cbcSMatt Macy	done
109eda14cbcSMatt Macy
110eda14cbcSMatt Macy	log_must zpool replace -sw $opt $TESTPOOL1 $disk1 $disk2
111eda14cbcSMatt Macy
112eda14cbcSMatt Macy	for wait_pid in $child_pids
113eda14cbcSMatt Macy	do
114eda14cbcSMatt Macy		kill $wait_pid
115eda14cbcSMatt Macy	done
116eda14cbcSMatt Macy	child_pids=""
117eda14cbcSMatt Macy
118eda14cbcSMatt Macy	log_must zpool export $TESTPOOL1
119eda14cbcSMatt Macy	log_must zpool import -d $TESTDIR $TESTPOOL1
120eda14cbcSMatt Macy	log_must zfs umount $TESTPOOL1/$TESTFS1
121eda14cbcSMatt Macy	log_must zdb -cdui $TESTPOOL1/$TESTFS1
122eda14cbcSMatt Macy	log_must zfs mount $TESTPOOL1/$TESTFS1
123eda14cbcSMatt Macy	verify_pool $TESTPOOL1
124eda14cbcSMatt Macy}
125eda14cbcSMatt Macy
126eda14cbcSMatt Macyspecials_list=""
127eda14cbcSMatt Macyi=0
128eda14cbcSMatt Macywhile [[ $i != 3 ]]; do
129eda14cbcSMatt Macy	log_must truncate -s $MINVDEVSIZE $TESTDIR/$TESTFILE1.$i
130eda14cbcSMatt Macy	specials_list="$specials_list $TESTDIR/$TESTFILE1.$i"
131eda14cbcSMatt Macy
132eda14cbcSMatt Macy	((i = i + 1))
133eda14cbcSMatt Macydone
134eda14cbcSMatt Macy
135eda14cbcSMatt Macy#
136eda14cbcSMatt Macy# Create a replacement disk special file.
137eda14cbcSMatt Macy#
138eda14cbcSMatt Macylog_must truncate -s $MINVDEVSIZE $TESTDIR/$REPLACEFILE
139eda14cbcSMatt Macy
1407877fdebSMatt Macyfor type in "" "mirror" "draid"; do
141eda14cbcSMatt Macy	for op in "" "-f"; do
142eda14cbcSMatt Macy		create_pool $TESTPOOL1 $type $specials_list
143eda14cbcSMatt Macy		log_must zfs create $TESTPOOL1/$TESTFS1
144eda14cbcSMatt Macy		log_must zfs set mountpoint=$TESTDIR1 $TESTPOOL1/$TESTFS1
145eda14cbcSMatt Macy
146eda14cbcSMatt Macy		replace_test "$opt" $TESTDIR/$TESTFILE1.1 $TESTDIR/$REPLACEFILE
147eda14cbcSMatt Macy
148716fd348SMartin Matuska		log_must eval "zpool iostat -v $TESTPOOL1 | grep \"$REPLACEFILE\""
149eda14cbcSMatt Macy
150eda14cbcSMatt Macy		destroy_pool $TESTPOOL1
151eda14cbcSMatt Macy		log_must rm -rf /$TESTPOOL1
152eda14cbcSMatt Macy	done
153eda14cbcSMatt Macydone
154eda14cbcSMatt Macy
155eda14cbcSMatt Macylog_pass
156