t_vnops.c revision 1.3 1 /* $NetBSD: t_vnops.c,v 1.3 2010/07/16 19:16:41 pooka Exp $ */
2
3 /*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/stat.h>
30 #include <sys/statvfs.h>
31
32 #include <atf-c.h>
33 #include <fcntl.h>
34 #include <libgen.h>
35 #include <unistd.h>
36
37 #include <rump/rump_syscalls.h>
38 #include <rump/rump.h>
39
40 #include "../common/h_fsmacros.h"
41 #include "../../h_macros.h"
42
43 /* make work when run with 5.0 userland */
44 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < 599000700)
45 #define rump_sys_stat rump_pub_sys___stat30
46 #endif
47
48 #define USES_DIRS \
49 if (FSTYPE_SYSVBFS(tc)) atf_tc_skip("dirs not supported by file system")
50
51 static char *
52 md(char *buf, const char *base, const char *tail)
53 {
54
55 sprintf(buf, "%s/%s", base, tail);
56 return buf;
57 }
58
59 static void
60 lookup_simple(const atf_tc_t *tc, const char *mountpath)
61 {
62 char pb[MAXPATHLEN], final[MAXPATHLEN];
63 struct stat sb1, sb2;
64
65 strcpy(final, mountpath);
66 sprintf(pb, "%s/../%s", mountpath, basename(final));
67 if (rump_sys_stat(pb, &sb1) == -1)
68 atf_tc_fail_errno("stat 1");
69
70 sprintf(pb, "%s/./../%s", mountpath, basename(final));
71 if (rump_sys_stat(pb, &sb2) == -1)
72 atf_tc_fail_errno("stat 2");
73
74 ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
75 }
76
77 static void
78 lookup_complex(const atf_tc_t *tc, const char *mountpath)
79 {
80 char pb[MAXPATHLEN];
81 struct stat sb1, sb2;
82
83 USES_DIRS;
84
85 sprintf(pb, "%s/dir", mountpath);
86 if (rump_sys_mkdir(pb, 0777) == -1)
87 atf_tc_fail_errno("mkdir");
88 if (rump_sys_stat(pb, &sb1) == -1)
89 atf_tc_fail_errno("stat 1");
90
91 sprintf(pb, "%s/./dir/../././dir/.", mountpath);
92 if (rump_sys_stat(pb, &sb2) == -1)
93 atf_tc_fail_errno("stat 2");
94
95 ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
96 }
97
98 static void
99 dir_simple(const atf_tc_t *tc, const char *mountpath)
100 {
101 char pb[MAXPATHLEN];
102 struct stat sb;
103
104 USES_DIRS;
105
106 /* check we can create directories */
107 sprintf(pb, "%s/dir", mountpath);
108 if (rump_sys_mkdir(pb, 0777) == -1)
109 atf_tc_fail_errno("mkdir");
110 if (rump_sys_stat(pb, &sb) == -1)
111 atf_tc_fail_errno("stat new directory");
112
113 /* check we can remove then and that it makes them unreachable */
114 if (rump_sys_rmdir(pb) == -1)
115 atf_tc_fail_errno("rmdir");
116 if (rump_sys_stat(pb, &sb) != -1 || errno != ENOENT)
117 atf_tc_fail("ENOENT expected from stat");
118 }
119
120 static void
121 dir_notempty(const atf_tc_t *tc, const char *mountpath)
122 {
123 char pb[MAXPATHLEN], pb2[MAXPATHLEN];
124 int fd, rv;
125
126 USES_DIRS;
127
128 /* check we can create directories */
129 sprintf(pb, "%s/dir", mountpath);
130 if (rump_sys_mkdir(pb, 0777) == -1)
131 atf_tc_fail_errno("mkdir");
132
133 sprintf(pb2, "%s/dir/file", mountpath);
134 fd = rump_sys_open(pb2, O_RDWR | O_CREAT, 0777);
135 if (fd == -1)
136 atf_tc_fail_errno("create file");
137 rump_sys_close(fd);
138
139 rv = rump_sys_rmdir(pb);
140 if (rv != -1 || errno != ENOTEMPTY)
141 atf_tc_fail("non-empty directory removed succesfully");
142
143 if (rump_sys_unlink(pb2) == -1)
144 atf_tc_fail_errno("cannot remove dir/file");
145
146 if (rump_sys_rmdir(pb) == -1)
147 atf_tc_fail_errno("remove directory");
148 }
149
150 static void
151 checkfile(const char *path, struct stat *refp)
152 {
153 char buf[MAXPATHLEN];
154 struct stat sb;
155 static int n = 1;
156
157 md(buf, path, "file");
158 if (rump_sys_stat(buf, &sb) == -1)
159 atf_tc_fail_errno("cannot stat file %d (%s)", n, buf);
160 if (memcmp(&sb, refp, sizeof(sb)) != 0)
161 atf_tc_fail("stat mismatch %d", n);
162 n++;
163 }
164
165 static void
166 rename_dir(const atf_tc_t *tc, const char *mp)
167 {
168 char pb1[MAXPATHLEN], pb2[MAXPATHLEN], pb3[MAXPATHLEN];
169 struct stat ref;
170
171 if (FSTYPE_MSDOS(tc))
172 atf_tc_skip("test fails in some setups, reason unknown");
173
174 USES_DIRS;
175
176 md(pb1, mp, "dir1");
177 if (rump_sys_mkdir(pb1, 0777) == -1)
178 atf_tc_fail_errno("mkdir 1");
179
180 md(pb2, mp, "dir2");
181 if (rump_sys_mkdir(pb2, 0777) == -1)
182 atf_tc_fail_errno("mkdir 2");
183 md(pb2, mp, "dir2/subdir");
184 if (rump_sys_mkdir(pb2, 0777) == -1)
185 atf_tc_fail_errno("mkdir 3");
186
187 md(pb3, mp, "dir1/file");
188 if (rump_sys_mknod(pb3, S_IFREG | 0777, -1) == -1)
189 atf_tc_fail_errno("create file");
190 if (rump_sys_stat(pb3, &ref) == -1)
191 atf_tc_fail_errno("stat of file");
192
193 /*
194 * First try ops which should succeed.
195 */
196
197 /* rename within directory */
198 md(pb3, mp, "dir3");
199 if (rump_sys_rename(pb1, pb3) == -1)
200 atf_tc_fail_errno("rename 1");
201 checkfile(pb3, &ref);
202
203 /* rename directory onto itself (two ways, should fail) */
204 md(pb1, mp, "dir3/.");
205 if (rump_sys_rename(pb1, pb3) != -1 || errno != EINVAL)
206 atf_tc_fail_errno("rename 2");
207 if (rump_sys_rename(pb3, pb1) != -1 || errno != EISDIR)
208 atf_tc_fail_errno("rename 3");
209
210 checkfile(pb3, &ref);
211
212 /* rename father of directory into directory */
213 md(pb1, mp, "dir2/dir");
214 md(pb2, mp, "dir2");
215 if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
216 atf_tc_fail_errno("rename 4");
217
218 /* same for grandfather */
219 md(pb1, mp, "dir2/subdir/dir2");
220 if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
221 atf_tc_fail("rename 5");
222
223 checkfile(pb3, &ref);
224
225 /* rename directory over a non-empty directory */
226 if (rump_sys_rename(pb2, pb3) != -1 || errno != ENOTEMPTY)
227 atf_tc_fail("rename 6");
228
229 /* cross-directory rename */
230 md(pb1, mp, "dir3");
231 md(pb2, mp, "dir2/somedir");
232 if (rump_sys_rename(pb1, pb2) == -1)
233 atf_tc_fail_errno("rename 7");
234 checkfile(pb2, &ref);
235
236 /* move to parent directory */
237 md(pb1, mp, "dir2/somedir/../../dir3");
238 if (rump_sys_rename(pb2, pb1) == -1)
239 atf_tc_fail_errno("rename 8");
240 md(pb1, mp, "dir2/../dir3");
241 checkfile(pb1, &ref);
242
243 /* finally, atomic cross-directory rename */
244 md(pb3, mp, "dir2/subdir");
245 if (rump_sys_rename(pb1, pb3) == -1)
246 atf_tc_fail_errno("rename 9");
247 checkfile(pb3, &ref);
248 }
249
250 static void
251 rename_dotdot(const atf_tc_t *tc, const char *mp)
252 {
253
254 USES_DIRS;
255
256 if (rump_sys_chdir(mp) == -1)
257 atf_tc_fail_errno("chdir mountpoint");
258
259 if (rump_sys_mkdir("dir1", 0777) == -1)
260 atf_tc_fail_errno("mkdir 1");
261 if (rump_sys_mkdir("dir2", 0777) == -1)
262 atf_tc_fail_errno("mkdir 2");
263
264 /* msdosfs fails both at least currently */
265 if (FSTYPE_MSDOS(tc)) {
266 atf_tc_expect_fail("PR kern/43616");
267 }
268 if (rump_sys_rename("dir1", "dir1/..") != -1 || errno != EINVAL)
269 atf_tc_fail_errno("self-dotdot to");
270
271 if (rump_sys_rename("dir1/..", "sometarget") != -1 || errno != EINVAL)
272 atf_tc_fail_errno("self-dotdot from");
273 atf_tc_expect_pass();
274
275 if (FSTYPE_TMPFS(tc)) {
276 atf_tc_expect_fail("PR kern/43617");
277 }
278 if (rump_sys_rename("dir1", "dir2/..") != -1 || errno != EINVAL)
279 atf_tc_fail("other-dotdot");
280
281 rump_sys_chdir("/");
282 }
283
284 static void
285 rename_reg_nodir(const atf_tc_t *tc, const char *mp)
286 {
287 bool haslinks;
288 struct stat sb;
289 ino_t f1ino, f2ino;
290
291 if (FSTYPE_MSDOS(tc))
292 atf_tc_skip("test fails in some setups, reason unknown");
293
294 if (rump_sys_chdir(mp) == -1)
295 atf_tc_fail_errno("chdir mountpoint");
296
297 if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
298 haslinks = false;
299 else
300 haslinks = true;
301
302 if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
303 atf_tc_fail_errno("create file");
304 if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
305 atf_tc_fail_errno("create file");
306
307 if (rump_sys_stat("file1", &sb) == -1)
308 atf_tc_fail_errno("stat");
309 f1ino = sb.st_ino;
310
311 if (haslinks) {
312 if (rump_sys_link("file1", "file_link") == -1)
313 atf_tc_fail_errno("link");
314 if (rump_sys_stat("file_link", &sb) == -1)
315 atf_tc_fail_errno("stat");
316 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
317 ATF_REQUIRE_EQ(sb.st_nlink, 2);
318 }
319
320 if (rump_sys_stat("file2", &sb) == -1)
321 atf_tc_fail_errno("stat");
322 f2ino = sb.st_ino;
323
324 if (rump_sys_rename("file1", "file3") == -1)
325 atf_tc_fail_errno("rename 1");
326 if (rump_sys_stat("file3", &sb) == -1)
327 atf_tc_fail_errno("stat 1");
328 if (haslinks) {
329 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
330 }
331 if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
332 atf_tc_fail_errno("source 1");
333
334 if (rump_sys_rename("file3", "file2") == -1)
335 atf_tc_fail_errno("rename 2");
336 if (rump_sys_stat("file2", &sb) == -1)
337 atf_tc_fail_errno("stat 2");
338 if (haslinks) {
339 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
340 }
341
342 if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
343 atf_tc_fail_errno("source 2");
344
345 if (haslinks) {
346 if (rump_sys_rename("file2", "file_link") == -1)
347 atf_tc_fail_errno("rename hardlink");
348 if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
349 atf_tc_fail_errno("source 3");
350 if (rump_sys_stat("file_link", &sb) == -1)
351 atf_tc_fail_errno("stat 2");
352 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
353 ATF_REQUIRE_EQ(sb.st_nlink, 1);
354 }
355
356 rump_sys_chdir("/");
357 }
358
359 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
360 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
361 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
362 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
363 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops");
364 ATF_TC_FSAPPLY(rename_dotdot, "rename dir ..");
365 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
366
367 ATF_TP_ADD_TCS(tp)
368 {
369
370 ATF_TP_FSAPPLY(lookup_simple);
371 ATF_TP_FSAPPLY(lookup_complex);
372 ATF_TP_FSAPPLY(dir_simple);
373 ATF_TP_FSAPPLY(dir_notempty);
374 ATF_TP_FSAPPLY(rename_dir);
375 ATF_TP_FSAPPLY(rename_dotdot);
376 ATF_TP_FSAPPLY(rename_reg_nodir);
377
378 return atf_no_error();
379 }
380