xref: /netbsd-src/tests/sbin/fsck_ffs/t_extattr.sh (revision 5ae58755a45df7b6cfbd5803078e0c84e32f69e0)
1# $NetBSD: t_extattr.sh,v 1.5 2023/09/26 12:15:44 kre Exp $
2#
3#  Copyright (c) 2021 The NetBSD Foundation, Inc.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions
8#  are met:
9#  1. Redistributions of source code must retain the above copyright
10#     notice, this list of conditions and the following disclaimer.
11#  2. Redistributions in binary form must reproduce the above copyright
12#     notice, this list of conditions and the following disclaimer in the
13#     documentation and/or other materials provided with the distribution.
14#
15#  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16#  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17#  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18#  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19#  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25#  POSSIBILITY OF SUCH DAMAGE.
26#
27
28VND=vnd0
29BDEV=/dev/${VND}
30CDEV=/dev/r${VND}
31IMG=fsimage
32MNT=mnt
33
34atf_test_case fsck_extattr_enable cleanup
35atf_test_case fsck_extattr_enable_corrupted cleanup
36atf_test_case fsck_extattr_disable cleanup
37
38cleanup()
39{
40	echo in cleanup
41	umount -f "${MNT}" > /dev/null 2>&1 || true
42	vnconfig -u "${VND}" > /dev/null 2>&1 || true
43}
44
45fsck_extattr_enable_head()
46{
47	atf_set "descr" "Checks fsck_ffs enabling extattrs"
48	atf_set "require.user" "root";
49}
50
51fsck_extattr_enable_body()
52{
53	atf_check mkdir -p "${MNT}"
54
55	atf_check -o ignore newfs -O2 -s 4m -F "${IMG}"
56	atf_check vnconfig "${VND}" "${IMG}"
57
58	# Verify that extattrs are disabled.
59	atf_check -o ignore -e 'match:POSIX1e ACLs not supported by this fs' \
60		tunefs -p enable "${CDEV}"
61	atf_check mount -t ffs "${BDEV}" "${MNT}"
62	atf_check touch "${MNT}/file"
63	atf_check -s exit:1 -e ignore setextattr user name1 value1 "${MNT}/file"
64	atf_check umount "${MNT}"
65
66	# Enable extattrs.
67	atf_check -o 'match:ENABLING EXTATTR SUPPORT' \
68		fsck_ffs -c ea "${CDEV}"
69
70	# Verify that extattrs are now enabled.
71	atf_check -o 'match:POSIX1e ACLs set' -e ignore \
72		tunefs -p enable "${CDEV}"
73	atf_check mount -t ffs "${BDEV}" "${MNT}"
74	atf_check touch "${MNT}/file"
75	atf_check setextattr user testname testvalue "${MNT}/file"
76	atf_check -o 'match:testvalue' getextattr user testname "${MNT}/file"
77	atf_check umount "${MNT}"
78	atf_check vnconfig -u "${VND}"
79}
80
81fsck_extattr_enable_cleanup()
82{
83	cleanup
84}
85
86fsck_extattr_enable_corrupted_head()
87{
88	atf_set "descr" "Checks fsck_ffs enabling extattrs with corruption"
89	atf_set "require.user" "root";
90}
91
92fsck_extattr_enable_corrupted_body()
93{
94	atf_check mkdir -p "${MNT}"
95
96	# Create an fs with extattrs enabled and set an extattr on the test file.
97	atf_check -o ignore newfs -O2ea -b 8k -f 1k -s 4m -F "${IMG}"
98	atf_check vnconfig "${VND}" "${IMG}"
99
100	atf_check mount -t ffs "${BDEV}" "${MNT}"
101	atf_check touch "${MNT}/file"
102	atf_check setextattr user testname testvalue "${MNT}/file"
103	atf_check -o 'match:testvalue' getextattr user testname "${MNT}/file"
104	atf_check umount "${MNT}"
105
106	# Find the location and size of the extattr block.
107	extb0=$(printf 'cd file\niptrs\n' |
108		fsdb -n "$CDEV" |
109		grep 'di_extb 0' |
110		awk '{print $3}')
111	extsize=$(printf 'cd file\n' |
112		fsdb -n "$CDEV" |
113		grep EXTSIZE |
114		tail -n 1 |
115		awk '{print $4}' |
116		sed 's,.*=,,')
117	atf_check [ "$extb0" != 0 ]
118	atf_check [ "$extsize" != 0 ]
119
120	# Recreate the fs with extattrs disabled and set the extattr block
121	# size/location of the new test file to the same values as the old
122	# test file.  This simulates extattrs having been created in a
123	# UFS2-non-ea file system before UFS2ea was invented.
124	atf_check -o ignore newfs -O2 -b 8k -f 1k -s 4m -F "${IMG}"
125	atf_check mount -t ffs "${BDEV}" "${MNT}"
126	atf_check touch "${MNT}/file"
127	atf_check umount "${MNT}"
128	printf "cd file\nchextb 0 $extb0\n" | fsdb -N "$CDEV"
129	printf "cd file\nchextsize $extsize\n" | fsdb -N "$CDEV"
130
131	# Convert to enable extattrs.
132	atf_check -o 'match:CLEAR EXTATTR FIELDS' \
133		  -o 'match:ENABLING EXTATTR SUPPORT' \
134		  fsck_ffs -y -c ea "${CDEV}"
135
136	# Verify that the test file does not have the extattr.
137	atf_check -o ignore fsck_ffs -n "${CDEV}"
138	atf_check mount -t ffs "${BDEV}" "${MNT}"
139	atf_check -s exit:1 -e 'match:Attribute not found' \
140		  getextattr user testname "${MNT}/file"
141	atf_check umount "${MNT}"
142	atf_check vnconfig -u "${VND}"
143}
144
145fsck_extattr_enable_corrupted_cleanup()
146{
147	cleanup
148}
149
150fsck_extattr_disable_head()
151{
152	atf_set "descr" "Checks fsck_ffs disabling extattrs"
153	atf_set "require.user" "root";
154}
155
156fsck_extattr_disable_body()
157{
158	atf_check mkdir -p "${MNT}"
159
160	# Create an fs with extattrs enabled and set an extattr on the test file.
161	atf_check -o ignore newfs -O2ea -b 8k -f 1k -s 4m -F "${IMG}"
162	atf_check vnconfig "${VND}" "${IMG}"
163
164	atf_check mount -t ffs "${BDEV}" "${MNT}"
165	atf_check touch "${MNT}/file"
166	atf_check setextattr user testname testvalue "${MNT}/file"
167	atf_check -o 'match:testvalue' getextattr user testname "${MNT}/file"
168	atf_check umount "${MNT}"
169
170	# Convert to disable extattrs.
171	atf_check -o 'match:CLEAR EXTATTR FIELDS' \
172		  -o 'match:DISABLING EXTATTR SUPPORT' \
173		  fsck_ffs -y -c no-ea "${CDEV}"
174
175	# Verify that the test file does not have the test extattr.
176	atf_check -o ignore fsck_ffs -n "${CDEV}"
177	atf_check mount -t ffs "${BDEV}" "${MNT}"
178	atf_check -s exit:1 \
179	    -e 'match:getextattr: mnt/file: failed: Operation not supported' \
180		  getextattr user testname "${MNT}/file"
181	atf_check umount "${MNT}"
182
183	# Convert to enable extattrs again.
184	atf_check -o 'match:ENABLING EXTATTR SUPPORT' \
185		  fsck_ffs -y -c ea ${CDEV}
186
187	# Verify that the test extattr is still gone.
188	atf_check -o ignore fsck_ffs -n "${CDEV}"
189	atf_check mount -t ffs "${BDEV}" "${MNT}"
190	atf_check -s exit:1 -e 'match:Attribute not found' \
191		  getextattr user testname "${MNT}/file"
192	atf_check umount "${MNT}"
193
194	atf_check vnconfig -u "${VND}"
195}
196
197fsck_extattr_disable_cleanup()
198{
199	cleanup
200}
201
202atf_init_test_cases()
203{
204	atf_add_test_case fsck_extattr_enable
205	atf_add_test_case fsck_extattr_enable_corrupted
206	atf_add_test_case fsck_extattr_disable
207}
208