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 (c) 2021 by Nutanix. All rights reserved.
25#
26
27. $STF_SUITE/tests/functional/slog/slog.kshlib
28
29#
30# DESCRIPTION:
31#	Verify saxattr logging in to ZIL works
32#
33# STRATEGY:
34#	1. Create an empty file system (TESTFS)
35#	2. Freeze TESTFS
36#	3. Create Xattrs.
37#	4. Unmount filesystem
38#	   <at this stage TESTFS is empty again and unfrozen, and the
39#	   intent log contains a complete set of deltas to replay it>
40#	5. Remount TESTFS <which replays the intent log>
41#	6. Check xattrs.
42#
43
44verify_runnable "global"
45
46function cleanup_testenv
47{
48	cleanup
49	log_must set_tunable32 ZIL_SAXATTR $orig_zil_saxattr
50}
51
52log_assert "Verify saxattr logging in to ZIL works"
53
54orig_zil_saxattr=$(get_tunable ZIL_SAXATTR)
55
56log_onexit cleanup_testenv
57log_must setup
58
59NFILES=10
60function validate_zil_saxattr
61{
62	saxattrzil=$1
63	if [ "$2" == "disabled" ]; then
64		zilsaxattr_feature_disabled=1
65		zpoolcreateflags="-ofeature@zilsaxattr=disabled"
66	else
67		zilsaxattr_feature_disabled=0
68		zpoolcreateflags=""
69	fi
70
71	log_must set_tunable32 ZIL_SAXATTR $saxattrzil
72
73	#
74	# 1. Create an empty file system (TESTFS)
75	#
76	log_must zpool create $zpoolcreateflags $TESTPOOL $VDEV log mirror $LDEV
77	log_must zfs set compression=on $TESTPOOL
78	log_must zfs create -o xattr=sa $TESTPOOL/$TESTFS
79	log_must mkdir -p $TESTDIR
80
81	#
82	# This dd command works around an issue where ZIL records aren't created
83	# after freezing the pool unless a ZIL header already exists. Create a
84	# file synchronously to force ZFS to write one out.
85	#
86	log_must dd if=/dev/zero of=/$TESTPOOL/$TESTFS/sync \
87	    conv=fdatasync,fsync bs=1 count=1
88
89	#
90	# 2. Freeze TESTFS
91	#
92	log_must zpool freeze $TESTPOOL
93
94	rm /$TESTPOOL/$TESTFS/sync
95	#
96	# 3. Create xattrs
97	#
98	for i in $(seq $NFILES); do
99		log_must mkdir /$TESTPOOL/$TESTFS/xattr.d.$i
100		log_must set_xattr test test /$TESTPOOL/$TESTFS/xattr.d.$i
101
102		log_must touch /$TESTPOOL/$TESTFS/xattr.f.$i
103		log_must set_xattr test test /$TESTPOOL/$TESTFS/xattr.f.$i
104	done
105
106	#
107	# 4. Unmount filesystem and export the pool
108	#
109	# At this stage TESTFS is empty again and unfrozen, and the
110	# intent log contains a complete set of deltas to replay it.
111	#
112	log_must zfs unmount /$TESTPOOL/$TESTFS
113
114	log_note "Verify transactions to replay:"
115	log_must zdb -iv $TESTPOOL/$TESTFS
116
117	log_must zpool export $TESTPOOL
118
119	#
120	# 5. Remount TESTFS <which replays the intent log>
121	#
122	# Import the pool to unfreeze it and claim log blocks.  It has to be
123	# `zpool import -f` because we can't write a frozen pool's labels!
124	#
125	log_must zpool import -f -d $VDIR $TESTPOOL
126
127	#
128	# 6. Verify Xattr
129	# If zilsaxattr_feature_disabled=1 or saxattrzil=0, then xattr=sa
130	# logging in ZIL is not enabled, So, xattrs would be lost.
131	# If zilsaxattr_feature_disabled=0 and saxattrzil=1, then xattr=sa
132	# logging in ZIL is enabled, So, xattrs shouldn't be lost.
133	#
134	for i in $(seq $NFILES); do
135		if [ $zilsaxattr_feature_disabled -eq 1 -o \
136		    $saxattrzil -eq 0 ]; then
137			log_mustnot get_xattr test /$TESTPOOL/$TESTFS/xattr.d.$i
138			log_mustnot get_xattr test /$TESTPOOL/$TESTFS/xattr.f.$i
139		else
140			log_must get_xattr test /$TESTPOOL/$TESTFS/xattr.d.$i
141			log_must get_xattr test /$TESTPOOL/$TESTFS/xattr.f.$i
142		fi
143	done
144
145	cleanup
146	log_must setup
147}
148
149
150#Validate zilsaxattr feature enabled.
151validate_zil_saxattr 0
152validate_zil_saxattr 1
153#Validate zilsaxattr feature disabled.
154validate_zil_saxattr 0 disabled
155validate_zil_saxattr 1 disabled
156
157log_pass "Verify saxattr logging in to ZIL works"
158