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 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
25#
26
27. $STF_SUITE/include/libtest.shlib
28. $STF_SUITE/tests/functional/cli_root/zfs_mount/zfs_mount.kshlib
29
30#
31# DESCRIPTION:
32# Verify remount functionality, especially on readonly objects.
33#
34# STRATEGY:
35# 1. Prepare a filesystem and a snapshot
36# 2. Verify we can (re)mount the dataset readonly/read-write
37# 3. Verify we can mount the snapshot and it's mounted readonly
38# 4. Verify we can't remount it read-write
39# 5. Verify we can remount a dataset readonly and unmount it with
40#    encryption=on and sync=disabled (issue #7753)
41# 6. Re-import the pool readonly
42# 7. Verify we can't remount its filesystem read-write
43#
44
45verify_runnable "both"
46
47function cleanup
48{
49	log_must_busy zpool export $TESTPOOL
50	log_must zpool import $TESTPOOL
51	snapexists $TESTSNAP && destroy_dataset $TESTSNAP
52	[[ -d $MNTPSNAP ]] && log_must rmdir $MNTPSNAP
53	return 0
54}
55
56if is_freebsd; then
57	typeset RO="-t zfs -ur"
58	typeset RW="-t zfs -uw"
59else
60	typeset RO="-o remount,ro"
61	typeset RW="-o remount,rw"
62fi
63
64#
65# Verify the $filesystem is mounted readonly
66# This is preferred over "log_mustnot touch $fs" because we actually want to
67# verify the error returned is EROFS
68#
69function readonlyfs # filesystem
70{
71	typeset filesystem="$1"
72
73	file_write -o create -f $filesystem/file.dat
74	ret=$?
75	if [[ $ret != 30 ]]; then
76		log_fail "Writing to $filesystem did not return EROFS ($ret)."
77	fi
78}
79
80#
81# Verify $dataset is mounted with $option
82#
83function checkmount # dataset option
84{
85	typeset dataset="$1"
86	typeset option="$2"
87	typeset options=""
88
89	if is_freebsd; then
90		options=$(mount -p | awk -v ds="$dataset" '$1 == ds { print $4 }')
91	else
92		options=$(awk -v ds="$dataset" '$1 == ds { print $4 }' /proc/mounts)
93	fi
94	if [[ "$options" == '' ]]; then
95		log_fail "Dataset $dataset is not mounted"
96	elif [[ ! -z "${options##*$option*}" ]]; then
97		log_fail "Dataset $dataset is not mounted with expected "\
98		    "option $option ($options)"
99	else
100		log_note "Dataset $dataset is mounted with option $option"
101	fi
102}
103
104log_assert "Verify remount functionality on both filesystem and snapshots"
105
106log_onexit cleanup
107
108# 1. Prepare a filesystem and a snapshot
109TESTFS=$TESTPOOL/$TESTFS
110TESTSNAP="$TESTFS@snap"
111datasetexists $TESTFS || log_must zfs create $TESTFS
112snapexists $TESTSNAP || log_must zfs snapshot $TESTSNAP
113log_must zfs set readonly=off $TESTFS
114MNTPFS="$(get_prop mountpoint $TESTFS)"
115MNTPSNAP="$TESTDIR/zfs_snap_mount"
116log_must mkdir -p $MNTPSNAP
117
118# 2. Verify we can (re)mount the dataset readonly/read-write
119log_must touch $MNTPFS/file.dat
120checkmount $TESTFS 'rw'
121log_must mount $RO $TESTFS $MNTPFS
122readonlyfs $MNTPFS
123checkmount $TESTFS 'ro'
124log_must mount $RW $TESTFS $MNTPFS
125log_must touch $MNTPFS/file.dat
126checkmount $TESTFS 'rw'
127
128if is_linux; then
129	# 3. Verify we can (re)mount the snapshot readonly
130	log_must mount -t zfs $TESTSNAP $MNTPSNAP
131	readonlyfs $MNTPSNAP
132	checkmount $TESTSNAP 'ro'
133	log_must mount $RO $TESTSNAP $MNTPSNAP
134	readonlyfs $MNTPSNAP
135	checkmount $TESTSNAP 'ro'
136	log_must umount $MNTPSNAP
137fi
138
139# 4. Verify we can't remount a snapshot read-write
140# The "mount -o rw" command will succeed but the snapshot is mounted readonly.
141# The "mount -o remount,rw" command must fail with an explicit error.
142log_must mount -t zfs -o rw $TESTSNAP $MNTPSNAP
143readonlyfs $MNTPSNAP
144checkmount $TESTSNAP 'ro'
145log_mustnot mount $RW $TESTSNAP $MNTPSNAP
146readonlyfs $MNTPSNAP
147checkmount $TESTSNAP 'ro'
148log_must umount $MNTPSNAP
149
150# 5. Verify we can remount a dataset readonly and unmount it with
151#    encryption=on and sync=disabled (issue #7753)
152log_must eval "echo 'password' | zfs create -o sync=disabled \
153    -o encryption=on -o keyformat=passphrase $TESTFS/crypt"
154CRYPT_MNTPFS="$(get_prop mountpoint $TESTFS/crypt)"
155log_must touch $CRYPT_MNTPFS/file.dat
156log_must mount $RO $TESTFS/crypt $CRYPT_MNTPFS
157log_must umount -f $CRYPT_MNTPFS
158sync_pool $TESTPOOL
159
160# 6. Re-import the pool readonly
161log_must zpool export $TESTPOOL
162log_must zpool import -o readonly=on $TESTPOOL
163
164# 7. Verify we can't remount its filesystem read-write
165readonlyfs $MNTPFS
166checkmount $TESTFS 'ro'
167log_mustnot mount $RW $MNTPFS
168readonlyfs $MNTPFS
169checkmount $TESTFS 'ro'
170
171log_pass "Both filesystem and snapshots can be remounted correctly."
172