1f38cb554SJohn Wren Kennedy#
2f38cb554SJohn Wren Kennedy# CDDL HEADER START
3f38cb554SJohn Wren Kennedy#
4f38cb554SJohn Wren Kennedy# The contents of this file are subject to the terms of the
5f38cb554SJohn Wren Kennedy# Common Development and Distribution License (the "License").
6f38cb554SJohn Wren Kennedy# You may not use this file except in compliance with the License.
7f38cb554SJohn Wren Kennedy#
8f38cb554SJohn Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f38cb554SJohn Wren Kennedy# or http://www.opensolaris.org/os/licensing.
10f38cb554SJohn Wren Kennedy# See the License for the specific language governing permissions
11f38cb554SJohn Wren Kennedy# and limitations under the License.
12f38cb554SJohn Wren Kennedy#
13f38cb554SJohn Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
14f38cb554SJohn Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f38cb554SJohn Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
16f38cb554SJohn Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
17f38cb554SJohn Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
18f38cb554SJohn Wren Kennedy#
19f38cb554SJohn Wren Kennedy# CDDL HEADER END
20f38cb554SJohn Wren Kennedy#
21f38cb554SJohn Wren Kennedy
22f38cb554SJohn Wren Kennedy#
23f38cb554SJohn Wren Kennedy# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24f38cb554SJohn Wren Kennedy# Use is subject to license terms.
25f38cb554SJohn Wren Kennedy#
26f38cb554SJohn Wren Kennedy
27f38cb554SJohn Wren Kennedy#
281d32ba66SJohn Wren Kennedy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
299c2acf00SAlek Pinchuk# Copyright (c) 2017 Datto Inc.
30*7811b028SKody Kantor# Copyright 2019 Joyent, Inc.
31f38cb554SJohn Wren Kennedy#
32f38cb554SJohn Wren Kennedy
339c2acf00SAlek Pinchuk. $STF_SUITE/include/libtest.shlib
34f38cb554SJohn Wren Kennedy. $STF_SUITE/tests/functional/clean_mirror/default.cfg
35f38cb554SJohn Wren Kennedy
36f38cb554SJohn Wren Kennedy# Most of the code related to the clearing of mirrors is duplicated in all
37f38cb554SJohn Wren Kennedy# the test cases below this directory, barring a few minor changes
38f38cb554SJohn Wren Kennedy# involving the device to be affected and the 'object' to use to mangle
39f38cb554SJohn Wren Kennedy# the contents of the mirror.
40f38cb554SJohn Wren Kennedy# This code is sourced into each of these test cases.
41f38cb554SJohn Wren Kennedy
42f38cb554SJohn Wren Kennedyfunction overwrite_verify_mirror
43f38cb554SJohn Wren Kennedy{
44f38cb554SJohn Wren Kennedy	typeset AFFECTED_DEVICE=$1
45f38cb554SJohn Wren Kennedy	typeset OVERWRITING_DEVICE=$2
46f38cb554SJohn Wren Kennedy
47f38cb554SJohn Wren Kennedy	typeset atfile=0
48f38cb554SJohn Wren Kennedy	set -A files
49f38cb554SJohn Wren Kennedy	set -A cksums
50f38cb554SJohn Wren Kennedy	set -A newcksums
51f38cb554SJohn Wren Kennedy
52*7811b028SKody Kantor	scrub_pool # Make sure we have a clean pool before starting the test.
53*7811b028SKody Kantor
54f38cb554SJohn Wren Kennedy	while (( atfile < FILE_COUNT )); do
55f38cb554SJohn Wren Kennedy		files[$atfile]=$TESTDIR/file.$atfile
561d32ba66SJohn Wren Kennedy		log_must file_write -o create -f $TESTDIR/file.$atfile \
57f38cb554SJohn Wren Kennedy			-b $FILE_SIZE -c 1
581d32ba66SJohn Wren Kennedy		cksums[$atfile]=$(cksum ${files[$atfile]})
59f38cb554SJohn Wren Kennedy		(( atfile = atfile + 1 ))
60f38cb554SJohn Wren Kennedy	done
61f38cb554SJohn Wren Kennedy
62f38cb554SJohn Wren Kennedy	# dd the primary side of the mirror
631d32ba66SJohn Wren Kennedy	log_must dd if=$OVERWRITING_DEVICE of=/dev/dsk/$AFFECTED_DEVICE \
64f38cb554SJohn Wren Kennedy		seek=8 bs=$DD_BLOCK count=$(( DD_COUNT - 8 )) conv=notrunc
65f38cb554SJohn Wren Kennedy
66f38cb554SJohn Wren Kennedy	atfile=0
67f38cb554SJohn Wren Kennedy
68dcbf3bd6SGeorge Wilson	#
69dcbf3bd6SGeorge Wilson	# Flush out the cache so that we ensure we're reading from disk.
70dcbf3bd6SGeorge Wilson	#
711d32ba66SJohn Wren Kennedy	log_must zpool export $TESTPOOL
721d32ba66SJohn Wren Kennedy	log_must zpool import $TESTPOOL
73dcbf3bd6SGeorge Wilson
74f38cb554SJohn Wren Kennedy	typeset -i failedcount=0
75f38cb554SJohn Wren Kennedy	while (( atfile < FILE_COUNT )); do
76f38cb554SJohn Wren Kennedy		files[$atfile]=$TESTDIR/file.$atfile
771d32ba66SJohn Wren Kennedy		newcksum=$(cksum ${files[$atfile]})
78f38cb554SJohn Wren Kennedy		if [[ $newcksum != ${cksums[$atfile]} ]]; then
79f38cb554SJohn Wren Kennedy			(( failedcount = failedcount + 1 ))
80f38cb554SJohn Wren Kennedy		fi
811d32ba66SJohn Wren Kennedy		rm -f ${files[$atfile]}
82f38cb554SJohn Wren Kennedy		(( atfile = atfile + 1 ))
83f38cb554SJohn Wren Kennedy	done
84f38cb554SJohn Wren Kennedy
85f38cb554SJohn Wren Kennedy	if (( $failedcount > 0 )); then
86f38cb554SJohn Wren Kennedy		log_fail "of the $FILE_COUNT files $failedcount did not " \
87f38cb554SJohn Wren Kennedy		    "have the same checksum before and after."
88f38cb554SJohn Wren Kennedy	fi
89dcbf3bd6SGeorge Wilson
90dcbf3bd6SGeorge Wilson	sync_pool $TESTPOOL
91f38cb554SJohn Wren Kennedy}
92*7811b028SKody Kantor
93*7811b028SKody Kantor# Scrub the pool and wait for completion.
94*7811b028SKody Kantorfunction scrub_pool
95*7811b028SKody Kantor{
96*7811b028SKody Kantor        typeset pool=$TESTPOOL
97*7811b028SKody Kantor        log_note "Scrubbing $TESTPOOL"
98*7811b028SKody Kantor
99*7811b028SKody Kantor        # Flush all the pool data.
100*7811b028SKody Kantor        log_must zpool scrub $pool
101*7811b028SKody Kantor
102*7811b028SKody Kantor        while ! is_pool_scrubbed $pool; do
103*7811b028SKody Kantor                if is_pool_resilvered $pool ; then
104*7811b028SKody Kantor                        log_fail "$pool should not have been resilvered"
105*7811b028SKody Kantor                fi
106*7811b028SKody Kantor                log_must sleep 2
107*7811b028SKody Kantor        done
108*7811b028SKody Kantor}
109