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