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#
30# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
31#
32
33. $STF_SUITE/include/libtest.shlib
34. $STF_SUITE/tests/functional/snapshot/snapshot.cfg
35
36#
37# DESCRIPTION:
38#
39# Directory structure of snapshots reflects filesystem structure.
40#
41# STRATEGY:
42#
43# This test makes sure that the directory structure of snapshots is
44# a proper reflection of the filesystem the snapshot was taken of.
45#
46# 1. Create a simple directory structure of files and directories
47# 2. Take a snapshot of the filesystem
48# 3. Modify original filesystem
49# 4. Walk down the snapshot directory structure verifying it
50#    checking with both absolute and relative paths
51#
52
53verify_runnable "both"
54
55function cleanup
56{
57	cd $SAVED_DIR
58
59	datasetexists $TESTPOOL/$TESTFS && \
60		destroy_dataset $TESTPOOL/$TESTFS -Rf
61
62	log_must zfs create $TESTPOOL/$TESTFS
63	log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
64}
65
66function verify_structure {
67
68	# check absolute paths
69	DIR=$PWD
70	verify_file $DIR/file1
71	verify_file $DIR/file2
72	verify_file $DIR/dir1/file3
73	verify_file $DIR/dir1/file4
74	verify_file $DIR/dir1/dir2/file5
75	verify_file $DIR/dir1/dir2/file6
76
77	verify_no_file $DIR/file99
78
79	# check relative paths
80	verify_file ./file1
81	verify_file ./file2
82	verify_file ./dir1/file3
83	verify_file ./dir1/file4
84	verify_file ./dir1/dir2/file5
85	verify_file ./dir1/dir2/file6
86
87	cd dir1
88	verify_file ../file1
89	verify_file ../file2
90	verify_file ./file3
91	verify_file ./file4
92
93	verify_no_file ../file99
94
95	cd dir2
96	verify_file ./file5
97	verify_file ./file6
98	verify_file ../file3
99	verify_file ../file4
100	verify_no_file ../file99
101
102	verify_file ../../file1
103	verify_file ../../file2
104	verify_no_file ../../file99
105}
106
107function verify_file {
108	if [ ! -e $1 ]
109	then
110		log_note "Working dir is $PWD"
111		log_fail "File $1 does not exist!"
112	fi
113}
114
115function verify_no_file {
116	if [ -e $1 ]
117	then
118		log_note "Working dir is $PWD"
119		log_fail "File $1 exists when it should not!"
120	fi
121}
122
123function verify_dir {
124	if [ ! -d $1 ]
125	then
126		log_note "Working dir is $PWD"
127		log_fail "Directory $1 does not exist!"
128	fi
129}
130
131log_assert "Directory structure of snapshots reflects filesystem structure."
132log_onexit cleanup
133
134SAVED_DIR=$PWD
135
136#
137# Create a directory structure with the following files
138#
139# ./file1
140# ./file2
141# ./dir1/file3
142# ./dir1/file4
143# ./dir1/dir2/file5
144# ./dir1/dir2/file6
145
146cd $TESTDIR
147mkfile 10m file1
148mkfile 20m file2
149mkdir dir1
150cd dir1
151mkfile 10m file3
152mkfile 20m file4
153mkdir dir2
154cd dir2
155mkfile 10m file5
156mkfile 20m file6
157
158# Now walk the directory structure verifying it
159cd $TESTDIR
160verify_structure
161
162# Take snapshots
163log_must zfs snapshot $TESTPOOL/$TESTFS@snap_a
164log_must zfs snapshot $TESTPOOL/$TESTFS@snap_b
165
166# Change the filesystem structure by renaming files in the original structure
167# The snapshot file structure should not change
168cd $TESTDIR
169log_must mv file2 file99
170cd dir1
171log_must mv file4 file99
172cd dir2
173log_must mv file6 file99
174
175# verify the top level snapshot directories
176verify_dir $TESTDIR/.zfs
177verify_dir $TESTDIR/.zfs/snapshot
178verify_dir $TESTDIR/.zfs/snapshot/snap_a
179verify_dir $TESTDIR/.zfs/snapshot/snap_b
180
181cd $TESTDIR/.zfs/snapshot/snap_a
182verify_structure
183
184cd $TESTDIR/.zfs/snapshot/snap_b
185verify_structure
186
187cd $TESTDIR/.zfs
188verify_dir snapshot
189cd $TESTDIR/.zfs/snapshot
190verify_dir snap_a
191verify_dir snap_b
192
193cd snap_a
194verify_dir ../snap_a
195verify_dir ../snap_b
196
197cd ..
198verify_dir snap_a
199verify_dir snap_b
200
201log_pass "Directory structure of snapshots reflects filesystem structure."
202