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 https://opensource.org/licenses/CDDL-1.0.
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		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 file_write -o create -f $TESTDIR/testfile$$.$fn \
77	    -b $BLOCKSZ -c $NUM_WRITES; do
78	(( fn = fn + 1 ))
79done
80
81if is_linux || is_freebsd ; then
82	sync_all_pools
83else
84	log_must lockfs -f $TESTDIR
85fi
86
87log_must zfs set snapdev=visible $TESTPOOL/$TESTVOL
88log_must zfs snapshot $TESTPOOL/$TESTVOL@snap
89block_device_wait
90
91fsck -n ${ZVOL_RDEVDIR}/$TESTPOOL/$TESTVOL@snap >/dev/null 2>&1
92retval=$?
93
94if [ $retval -ne 0 ] ; then
95	if is_linux ; then
96		# Linux's fsck returns a different code for this test depending
97		# on the version:
98		#
99		# e2fsprogs-1.43.3 (Fedora 25 and older): returns 4
100		# e2fsprogs-1.43.4 (Fedora 26): returns 8
101		#
102		if [ $retval -ne 4 -a $retval -ne 8 ] ; then
103			log_fail "fsck exited with wrong value $retval"
104		fi
105	else
106		if [ $retval -ne 39 ] ; then
107			log_fail "fsck exited with wrong value $retval"
108		fi
109	fi
110fi
111
112log_pass "Verify that ZFS volume snapshot could be fscked"
113