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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/tests/functional/cli_root/zfs_mount/zfs_mount.kshlib
33
34#
35# DESCRIPTION:
36# The following options can be set on a temporary basis using the -o option
37# without affecting the on-disk property. The original on-disk value will be
38# restored when the file system is unmounted and mounted.
39#
40#         PROPERTY		MOUNT OPTION
41#	  atime			atime/noatime
42#	  devices		devices/nodevices
43#	  exec			exec/noexec
44#	  readonly		ro/rw
45#	  setuid		setuid/nosetuid
46#
47# STRATEGY:
48#	1. Create filesystem and get original property value.
49#	2. Using 'zfs mount -o' to set filesystem property.
50#	3. Verify the property was set temporarily.
51#	4. Verify it will not affect the property that is stored on disk.
52#
53
54function cleanup
55{
56	if ! ismounted $TESTPOOL/$TESTFS; then
57		log_must zfs mount $TESTPOOL/$TESTFS
58	fi
59}
60
61log_assert "Verify '-o' will set filesystem property temporarily, " \
62	"without affecting the property that is stored on disk."
63log_onexit cleanup
64
65set -A properties "atime" "exec" "readonly" "setuid"
66if ! is_freebsd; then
67	properties+=("devices")
68fi
69
70#
71# Get the specified filesystem property reverse mount option.
72#
73# $1 filesystem
74# $2 property
75#
76function get_reverse_option
77{
78	typeset fs=$1
79	typeset prop=$2
80
81	# Define property value: "reverse if value=on" "reverse if value=off"
82	if is_linux; then
83		set -A values "noatime"   "atime" \
84			      "noexec"    "exec" \
85			      "rw"        "ro" \
86			      "nosuid"    "suid" \
87			      "nodev"     "dev"
88	elif is_freebsd; then
89		set -A values "noatime"   "atime" \
90			      "noexec"    "exec" \
91			      "rw"        "ro" \
92			      "nosetuid"  "setuid"
93	else
94		set -A values "noatime"   "atime" \
95			      "noexec"    "exec" \
96			      "rw"        "ro" \
97			      "nosetuid"  "setuid" \
98			      "nodevices" "devices"
99	fi
100
101	typeset -i i=0
102	while (( i < ${#properties[@]} )); do
103		if [[ $prop == ${properties[$i]} ]]; then
104			break
105		fi
106
107		(( i += 1 ))
108	done
109	if (( i >= ${#properties[@]} )); then
110		log_fail "Incorrect option: $prop"
111	fi
112
113	typeset val
114	typeset -i ind=0
115	val=$(get_prop $prop $fs)
116	if [[ $val == "on" ]]; then
117		(( ind = i * 2 ))
118	else
119		(( ind = i * 2 + 1 ))
120	fi
121
122	echo ${values[$ind]}
123}
124
125fs=$TESTPOOL/$TESTFS
126cleanup
127
128for property in ${properties[@]}; do
129	orig_val=$(get_prop $property $fs)
130
131	# Set filesystem property temporarily
132	reverse_opt=$(get_reverse_option $fs $property)
133	log_must zfs unmount $fs
134	log_must zfs mount -o $reverse_opt $fs
135
136	cur_val=$(get_prop $property $fs)
137
138	# In LZ, a user with all zone privileges can never with "devices"
139	if ! is_global_zone && [[ $property == devices ]] ; then
140		if [[ $cur_val != off || $orig_val != off ]]; then
141			log_fail "'devices' property shouldn't " \
142				"be enabled in LZ"
143		fi
144	elif [[ $orig_val == $cur_val ]]; then
145		log_fail "zfs mount -o $reverse_opt " \
146			"doesn't change property."
147	fi
148
149	# unmount & mount will revert property to the original value
150	log_must zfs unmount $fs
151	log_must zfs mount $fs
152
153	cur_val=$(get_prop $property $fs)
154	if [[ $orig_val != $cur_val ]]; then
155		log_fail "zfs mount -o $reverse_opt " \
156			"change the property that is stored on disks"
157	fi
158done
159
160log_pass "Verify '-o' set filesystem property temporarily passed."
161