1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
30# Copyright 2016 Nexenta Systems, Inc.
31#
32
33. $STF_SUITE/include/libtest.shlib
34
35#
36# DESCRIPTION:
37# Verify that ZFS volume snapshot could be fscked
38#
39# STRATEGY:
40# 1. Create a ZFS volume
41# 2. Copy some files and create snapshot
42# 3. Verify fsck on the snapshot is OK
43#
44
45verify_runnable "global"
46
47volsize=$(zfs get -H -o value volsize $TESTPOOL/$TESTVOL)
48
49function cleanup
50{
51	snapexists $TESTPOOL/$TESTVOL@snap && \
52		destroy_dataset $TESTPOOL/$TESTVOL@snap
53
54	ismounted $TESTDIR $NEWFS_DEFAULT_FS
55	(( $? == 0 )) && log_must umount $TESTDIR
56
57	zfs set volsize=$volsize $TESTPOOL/$TESTVOL
58}
59
60log_assert "Verify that ZFS volume snapshot could be fscked"
61log_onexit cleanup
62
63TESTVOL='testvol'
64BLOCKSZ=$(( 1024 * 1024 ))
65NUM_WRITES=40
66
67log_must zfs set volsize=128m $TESTPOOL/$TESTVOL
68
69log_must new_fs ${ZVOL_RDEVDIR}/$TESTPOOL/$TESTVOL
70
71log_must mount ${ZVOL_DEVDIR}/$TESTPOOL/$TESTVOL $TESTDIR
72
73typeset -i fn=0
74typeset -i retval=0
75
76while (( 1 )); do
77	file_write -o create -f $TESTDIR/testfile$$.$fn \
78	    -b $BLOCKSZ -c $NUM_WRITES
79	retval=$?
80	if (( $retval != 0 )); then
81		break
82	fi
83	(( fn = fn + 1 ))
84done
85
86if is_linux || is_freebsd ; then
87	log_must sync
88else
89	log_must lockfs -f $TESTDIR
90fi
91
92log_must zfs set snapdev=visible $TESTPOOL/$TESTVOL
93log_must zfs snapshot $TESTPOOL/$TESTVOL@snap
94block_device_wait
95
96fsck -n ${ZVOL_RDEVDIR}/$TESTPOOL/$TESTVOL@snap >/dev/null 2>&1
97retval=$?
98
99if [ $retval -ne 0 ] ; then
100	if is_linux ; then
101		# Linux's fsck returns a different code for this test depending
102		# on the version:
103		#
104		# e2fsprogs-1.43.3 (Fedora 25 and older): returns 4
105		# e2fsprogs-1.43.4 (Fedora 26): returns 8
106		#
107		if [ $retval -ne 4 -a $retval -ne 8 ] ; then
108			log_fail "fsck exited with wrong value $retval"
109		fi
110	else
111		if [ $retval -ne 39 ] ; then
112			log_fail "fsck exited with wrong value $retval"
113		fi
114	fi
115fi
116
117log_pass "Verify that ZFS volume snapshot could be fscked"
118