t_vnops.c revision 1.27 1 /* $NetBSD: t_vnops.c,v 1.27 2011/08/11 10:52:12 uch 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 <assert.h>
33 #include <atf-c.h>
34 #include <fcntl.h>
35 #include <libgen.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #include <rump/rump_syscalls.h>
41 #include <rump/rump.h>
42
43 #include "../common/h_fsmacros.h"
44 #include "../../h_macros.h"
45
46 #define TESTFILE "afile"
47
48 #define USES_DIRS \
49 if (FSTYPE_SYSVBFS(tc)) atf_tc_skip("dirs not supported by file system")
50
51 #define USES_SYMLINKS \
52 if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc)) \
53 atf_tc_skip("symlinks not supported by file system")
54
55 static char *
56 md(char *buf, const char *base, const char *tail)
57 {
58
59 sprintf(buf, "%s/%s", base, tail);
60 return buf;
61 }
62
63 static void
64 lookup_simple(const atf_tc_t *tc, const char *mountpath)
65 {
66 char pb[MAXPATHLEN], final[MAXPATHLEN];
67 struct stat sb1, sb2;
68
69 strcpy(final, mountpath);
70 sprintf(pb, "%s/../%s", mountpath, basename(final));
71 if (rump_sys_stat(pb, &sb1) == -1)
72 atf_tc_fail_errno("stat 1");
73
74 sprintf(pb, "%s/./../%s", mountpath, basename(final));
75 if (rump_sys_stat(pb, &sb2) == -1)
76 atf_tc_fail_errno("stat 2");
77
78 ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
79 }
80
81 static void
82 lookup_complex(const atf_tc_t *tc, const char *mountpath)
83 {
84 char pb[MAXPATHLEN];
85 struct stat sb1, sb2;
86
87 USES_DIRS;
88
89 sprintf(pb, "%s/dir", mountpath);
90 if (rump_sys_mkdir(pb, 0777) == -1)
91 atf_tc_fail_errno("mkdir");
92 if (rump_sys_stat(pb, &sb1) == -1)
93 atf_tc_fail_errno("stat 1");
94
95 sprintf(pb, "%s/./dir/../././dir/.", mountpath);
96 if (rump_sys_stat(pb, &sb2) == -1)
97 atf_tc_fail_errno("stat 2");
98
99 ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
100 }
101
102 static void
103 dir_simple(const atf_tc_t *tc, const char *mountpath)
104 {
105 char pb[MAXPATHLEN];
106 struct stat sb;
107
108 USES_DIRS;
109
110 /* check we can create directories */
111 sprintf(pb, "%s/dir", mountpath);
112 if (rump_sys_mkdir(pb, 0777) == -1)
113 atf_tc_fail_errno("mkdir");
114 if (rump_sys_stat(pb, &sb) == -1)
115 atf_tc_fail_errno("stat new directory");
116
117 /* check we can remove then and that it makes them unreachable */
118 if (rump_sys_rmdir(pb) == -1)
119 atf_tc_fail_errno("rmdir");
120 if (rump_sys_stat(pb, &sb) != -1 || errno != ENOENT)
121 atf_tc_fail("ENOENT expected from stat");
122 }
123
124 static void
125 dir_notempty(const atf_tc_t *tc, const char *mountpath)
126 {
127 char pb[MAXPATHLEN], pb2[MAXPATHLEN];
128 int fd, rv;
129
130 USES_DIRS;
131
132 /* check we can create directories */
133 sprintf(pb, "%s/dir", mountpath);
134 if (rump_sys_mkdir(pb, 0777) == -1)
135 atf_tc_fail_errno("mkdir");
136
137 sprintf(pb2, "%s/dir/file", mountpath);
138 fd = rump_sys_open(pb2, O_RDWR | O_CREAT, 0777);
139 if (fd == -1)
140 atf_tc_fail_errno("create file");
141 rump_sys_close(fd);
142
143 rv = rump_sys_rmdir(pb);
144 if (rv != -1 || errno != ENOTEMPTY)
145 atf_tc_fail("non-empty directory removed succesfully");
146
147 if (rump_sys_unlink(pb2) == -1)
148 atf_tc_fail_errno("cannot remove dir/file");
149
150 if (rump_sys_rmdir(pb) == -1)
151 atf_tc_fail_errno("remove directory");
152 }
153
154 static void
155 dir_rmdirdotdot(const atf_tc_t *tc, const char *mp)
156 {
157 char pb[MAXPATHLEN];
158 int xerrno;
159
160 USES_DIRS;
161
162 FSTEST_ENTER();
163 RL(rump_sys_mkdir("test", 0777));
164 RL(rump_sys_chdir("test"));
165
166 RL(rump_sys_mkdir("subtest", 0777));
167 RL(rump_sys_chdir("subtest"));
168
169 md(pb, mp, "test/subtest");
170 RL(rump_sys_rmdir(pb));
171 md(pb, mp, "test");
172 RL(rump_sys_rmdir(pb));
173
174 if (FSTYPE_NFS(tc))
175 xerrno = ESTALE;
176 else
177 xerrno = ENOENT;
178 /*
179 if (FSTYPE_TMPFS(tc))
180 atf_tc_expect_signal(-1, "PR kern/44657");
181 */
182 ATF_REQUIRE_ERRNO(xerrno, rump_sys_chdir("..") == -1);
183 FSTEST_EXIT();
184 }
185
186 static void
187 checkfile(const char *path, struct stat *refp)
188 {
189 char buf[MAXPATHLEN];
190 struct stat sb;
191 static int n = 1;
192
193 md(buf, path, "file");
194 if (rump_sys_stat(buf, &sb) == -1)
195 atf_tc_fail_errno("cannot stat file %d (%s)", n, buf);
196 if (memcmp(&sb, refp, sizeof(sb)) != 0)
197 atf_tc_fail("stat mismatch %d", n);
198 n++;
199 }
200
201 static void
202 rename_dir(const atf_tc_t *tc, const char *mp)
203 {
204 char pb1[MAXPATHLEN], pb2[MAXPATHLEN], pb3[MAXPATHLEN];
205 struct stat ref, sb;
206
207 if (FSTYPE_RUMPFS(tc))
208 atf_tc_skip("rename not supported by fs");
209
210 USES_DIRS;
211
212 md(pb1, mp, "dir1");
213 if (rump_sys_mkdir(pb1, 0777) == -1)
214 atf_tc_fail_errno("mkdir 1");
215
216 md(pb2, mp, "dir2");
217 if (rump_sys_mkdir(pb2, 0777) == -1)
218 atf_tc_fail_errno("mkdir 2");
219 md(pb2, mp, "dir2/subdir");
220 if (rump_sys_mkdir(pb2, 0777) == -1)
221 atf_tc_fail_errno("mkdir 3");
222
223 md(pb3, mp, "dir1/file");
224 if (rump_sys_mknod(pb3, S_IFREG | 0777, -1) == -1)
225 atf_tc_fail_errno("create file");
226 if (rump_sys_stat(pb3, &ref) == -1)
227 atf_tc_fail_errno("stat of file");
228
229 /*
230 * First try ops which should succeed.
231 */
232
233 /* rename within directory */
234 md(pb3, mp, "dir3");
235 if (rump_sys_rename(pb1, pb3) == -1)
236 atf_tc_fail_errno("rename 1");
237 checkfile(pb3, &ref);
238
239 /* rename directory onto itself (two ways, should fail) */
240 md(pb1, mp, "dir3/.");
241 if (rump_sys_rename(pb1, pb3) != -1 || errno != EINVAL)
242 atf_tc_fail_errno("rename 2");
243 if (rump_sys_rename(pb3, pb1) != -1 || errno != EISDIR)
244 atf_tc_fail_errno("rename 3");
245
246 checkfile(pb3, &ref);
247
248 /* rename father of directory into directory */
249 md(pb1, mp, "dir2/dir");
250 md(pb2, mp, "dir2");
251 if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
252 atf_tc_fail_errno("rename 4");
253
254 /* same for grandfather */
255 md(pb1, mp, "dir2/subdir/dir2");
256 if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
257 atf_tc_fail("rename 5");
258
259 checkfile(pb3, &ref);
260
261 /* rename directory over a non-empty directory */
262 if (rump_sys_rename(pb2, pb3) != -1 || errno != ENOTEMPTY)
263 atf_tc_fail("rename 6");
264
265 /* cross-directory rename */
266 md(pb1, mp, "dir3");
267 md(pb2, mp, "dir2/somedir");
268 if (rump_sys_rename(pb1, pb2) == -1)
269 atf_tc_fail_errno("rename 7");
270 checkfile(pb2, &ref);
271
272 /* move to parent directory */
273 md(pb1, mp, "dir2/somedir/../../dir3");
274 if (rump_sys_rename(pb2, pb1) == -1)
275 atf_tc_fail_errno("rename 8");
276 md(pb1, mp, "dir2/../dir3");
277 checkfile(pb1, &ref);
278
279 /* atomic cross-directory rename */
280 md(pb3, mp, "dir2/subdir");
281 if (rump_sys_rename(pb1, pb3) == -1)
282 atf_tc_fail_errno("rename 9");
283 checkfile(pb3, &ref);
284
285 /* rename directory over an empty directory */
286 md(pb1, mp, "parent");
287 md(pb2, mp, "parent/dir1");
288 md(pb3, mp, "parent/dir2");
289 RL(rump_sys_mkdir(pb1, 0777));
290 RL(rump_sys_mkdir(pb2, 0777));
291 RL(rump_sys_mkdir(pb3, 0777));
292 RL(rump_sys_rename(pb2, pb3));
293
294 RL(rump_sys_stat(pb1, &sb));
295 if (! FSTYPE_MSDOS(tc))
296 ATF_CHECK_EQ(sb.st_nlink, 3);
297 RL(rump_sys_rmdir(pb3));
298 /*
299 if (FSTYPE_TMPFS(tc))
300 atf_tc_expect_signal(-1, "PR kern/44288");
301 */
302 RL(rump_sys_rmdir(pb1));
303 }
304
305 static void
306 rename_dotdot(const atf_tc_t *tc, const char *mp)
307 {
308
309 if (FSTYPE_RUMPFS(tc))
310 atf_tc_skip("rename not supported by fs");
311
312 USES_DIRS;
313
314 if (rump_sys_chdir(mp) == -1)
315 atf_tc_fail_errno("chdir mountpoint");
316
317 if (rump_sys_mkdir("dir1", 0777) == -1)
318 atf_tc_fail_errno("mkdir 1");
319 if (rump_sys_mkdir("dir2", 0777) == -1)
320 atf_tc_fail_errno("mkdir 2");
321
322 if (rump_sys_rename("dir1", "dir1/..") != -1 || errno != EINVAL)
323 atf_tc_fail_errno("self-dotdot to");
324
325 if (rump_sys_rename("dir1/..", "sometarget") != -1 || errno != EINVAL)
326 atf_tc_fail_errno("self-dotdot from");
327 atf_tc_expect_pass();
328
329 /*
330 if (FSTYPE_TMPFS(tc)) {
331 atf_tc_expect_fail("PR kern/43617");
332 }
333 */
334 if (rump_sys_rename("dir1", "dir2/..") != -1 || errno != EINVAL)
335 atf_tc_fail("other-dotdot");
336
337 rump_sys_chdir("/");
338 }
339
340 static void
341 rename_reg_nodir(const atf_tc_t *tc, const char *mp)
342 {
343 bool haslinks;
344 struct stat sb;
345 ino_t f1ino, f2ino;
346
347 if (FSTYPE_RUMPFS(tc))
348 atf_tc_skip("rename not supported by fs");
349
350 if (rump_sys_chdir(mp) == -1)
351 atf_tc_fail_errno("chdir mountpoint");
352
353 if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
354 haslinks = false;
355 else
356 haslinks = true;
357
358 if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
359 atf_tc_fail_errno("create file");
360 if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
361 atf_tc_fail_errno("create file");
362
363 if (rump_sys_stat("file1", &sb) == -1)
364 atf_tc_fail_errno("stat");
365 f1ino = sb.st_ino;
366
367 if (haslinks) {
368 if (rump_sys_link("file1", "file_link") == -1)
369 atf_tc_fail_errno("link");
370 if (rump_sys_stat("file_link", &sb) == -1)
371 atf_tc_fail_errno("stat");
372 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
373 ATF_REQUIRE_EQ(sb.st_nlink, 2);
374 }
375
376 if (rump_sys_stat("file2", &sb) == -1)
377 atf_tc_fail_errno("stat");
378 f2ino = sb.st_ino;
379
380 if (rump_sys_rename("file1", "file3") == -1)
381 atf_tc_fail_errno("rename 1");
382 if (rump_sys_stat("file3", &sb) == -1)
383 atf_tc_fail_errno("stat 1");
384 if (haslinks) {
385 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
386 }
387 if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
388 atf_tc_fail_errno("source 1");
389
390 if (rump_sys_rename("file3", "file2") == -1)
391 atf_tc_fail_errno("rename 2");
392 if (rump_sys_stat("file2", &sb) == -1)
393 atf_tc_fail_errno("stat 2");
394 if (haslinks) {
395 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
396 }
397
398 if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
399 atf_tc_fail_errno("source 2");
400
401 if (haslinks) {
402 if (rump_sys_rename("file2", "file_link") == -1)
403 atf_tc_fail_errno("rename hardlink");
404 if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
405 atf_tc_fail_errno("source 3");
406 if (rump_sys_stat("file_link", &sb) == -1)
407 atf_tc_fail_errno("stat 2");
408 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
409 ATF_REQUIRE_EQ(sb.st_nlink, 1);
410 }
411
412 rump_sys_chdir("/");
413 }
414
415 static void
416 create_nametoolong(const atf_tc_t *tc, const char *mp)
417 {
418 char *name;
419 int fd;
420 long val;
421 size_t len;
422
423 if (rump_sys_chdir(mp) == -1)
424 atf_tc_fail_errno("chdir mountpoint");
425
426 val = rump_sys_pathconf(".", _PC_NAME_MAX);
427 if (val == -1)
428 atf_tc_fail_errno("pathconf");
429
430 len = val + 1;
431 name = malloc(len+1);
432 if (name == NULL)
433 atf_tc_fail_errno("malloc");
434
435 memset(name, 'a', len);
436 *(name+len) = '\0';
437
438 val = rump_sys_pathconf(".", _PC_NO_TRUNC);
439 if (val == -1)
440 atf_tc_fail_errno("pathconf");
441
442 fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666);
443 if (val != 0 && (fd != -1 || errno != ENAMETOOLONG))
444 atf_tc_fail_errno("open");
445
446 if (val == 0 && rump_sys_close(fd) == -1)
447 atf_tc_fail_errno("close");
448 if (val == 0 && rump_sys_unlink(name) == -1)
449 atf_tc_fail_errno("unlink");
450
451 free(name);
452
453 rump_sys_chdir("/");
454 }
455
456 static void
457 create_exist(const atf_tc_t *tc, const char *mp)
458 {
459 const char *name = "hoge";
460 int fd;
461
462 RL(rump_sys_chdir(mp));
463 RL(fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666));
464 RL(rump_sys_close(fd));
465 RL(rump_sys_unlink(name));
466 RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
467 RL(rump_sys_close(fd));
468 RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
469 RL(rump_sys_close(fd));
470 ATF_REQUIRE_ERRNO(EEXIST,
471 (fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666)));
472 RL(rump_sys_unlink(name));
473 RL(rump_sys_chdir("/"));
474 }
475
476 static void
477 rename_nametoolong(const atf_tc_t *tc, const char *mp)
478 {
479 char *name;
480 int res, fd;
481 long val;
482 size_t len;
483
484 if (FSTYPE_RUMPFS(tc))
485 atf_tc_skip("rename not supported by fs");
486
487 if (rump_sys_chdir(mp) == -1)
488 atf_tc_fail_errno("chdir mountpoint");
489
490 val = rump_sys_pathconf(".", _PC_NAME_MAX);
491 if (val == -1)
492 atf_tc_fail_errno("pathconf");
493
494 len = val + 1;
495 name = malloc(len+1);
496 if (name == NULL)
497 atf_tc_fail_errno("malloc");
498
499 memset(name, 'a', len);
500 *(name+len) = '\0';
501
502 fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
503 if (fd == -1)
504 atf_tc_fail_errno("open");
505 if (rump_sys_close(fd) == -1)
506 atf_tc_fail_errno("close");
507
508 val = rump_sys_pathconf(".", _PC_NO_TRUNC);
509 if (val == -1)
510 atf_tc_fail_errno("pathconf");
511
512 res = rump_sys_rename("dummy", name);
513 if (val != 0 && (res != -1 || errno != ENAMETOOLONG))
514 atf_tc_fail_errno("rename");
515
516 if (val == 0 && rump_sys_unlink(name) == -1)
517 atf_tc_fail_errno("unlink");
518
519 free(name);
520
521 rump_sys_chdir("/");
522 }
523
524 static void
525 symlink_zerolen(const atf_tc_t *tc, const char *mp)
526 {
527
528 USES_SYMLINKS;
529
530 RL(rump_sys_chdir(mp));
531
532 RL(rump_sys_symlink("", "afile"));
533 RL(rump_sys_chdir("/"));
534 }
535
536 static void
537 attrs(const atf_tc_t *tc, const char *mp)
538 {
539 struct stat sb, sb2;
540 struct timeval tv[2];
541 int fd;
542
543 FSTEST_ENTER();
544 RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
545 RL(rump_sys_close(fd));
546 RL(rump_sys_stat(TESTFILE, &sb));
547 if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
548 RL(rump_sys_chown(TESTFILE, 1, 2));
549 sb.st_uid = 1;
550 sb.st_gid = 2;
551 RL(rump_sys_chmod(TESTFILE, 0123));
552 sb.st_mode = (sb.st_mode & ~ACCESSPERMS) | 0123;
553 }
554
555 tv[0].tv_sec = 1000000000; /* need something >1980 for msdosfs */
556 tv[0].tv_usec = 1;
557 tv[1].tv_sec = 1000000002; /* need even seconds for msdosfs */
558 tv[1].tv_usec = 3;
559 RL(rump_sys_utimes(TESTFILE, tv));
560 RL(rump_sys_utimes(TESTFILE, tv)); /* XXX: utimes & birthtime */
561 sb.st_atimespec.tv_sec = 1000000000;
562 sb.st_atimespec.tv_nsec = 1000;
563 sb.st_mtimespec.tv_sec = 1000000002;
564 sb.st_mtimespec.tv_nsec = 3000;
565
566 RL(rump_sys_stat(TESTFILE, &sb2));
567 #define CHECK(a) ATF_REQUIRE_EQ(sb.a, sb2.a)
568 if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
569 CHECK(st_uid);
570 CHECK(st_gid);
571 CHECK(st_mode);
572 }
573 if (!FSTYPE_MSDOS(tc)) {
574 /* msdosfs has only access date, not time */
575 CHECK(st_atimespec.tv_sec);
576 }
577 CHECK(st_mtimespec.tv_sec);
578 if (!(FSTYPE_EXT2FS(tc) || FSTYPE_MSDOS(tc) ||
579 FSTYPE_SYSVBFS(tc) || FSTYPE_V7FS(tc))) {
580 CHECK(st_atimespec.tv_nsec);
581 CHECK(st_mtimespec.tv_nsec);
582 }
583 #undef CHECK
584
585 FSTEST_EXIT();
586 }
587
588 static void
589 fcntl_lock(const atf_tc_t *tc, const char *mp)
590 {
591 int fd, fd2;
592 struct flock l;
593 struct lwp *lwp1, *lwp2;
594
595 FSTEST_ENTER();
596 l.l_pid = 0;
597 l.l_start = l.l_len = 1024;
598 l.l_type = F_RDLCK | F_WRLCK;
599 l.l_whence = SEEK_END;
600
601 lwp1 = rump_pub_lwproc_curlwp();
602 RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
603 RL(rump_sys_ftruncate(fd, 8192));
604
605 /* PR kern/43321 */
606 RL(rump_sys_fcntl(fd, F_SETLK, &l));
607
608 /* Next, we fork and try to lock the same area */
609 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
610 lwp2 = rump_pub_lwproc_curlwp();
611 RL(fd2 = rump_sys_open(TESTFILE, O_RDWR, 0));
612 ATF_REQUIRE_ERRNO(EAGAIN, rump_sys_fcntl(fd2, F_SETLK, &l));
613
614 /* Switch back and unlock... */
615 rump_pub_lwproc_switch(lwp1);
616 l.l_type = F_UNLCK;
617 RL(rump_sys_fcntl(fd, F_SETLK, &l));
618
619 /* ... and try to lock again */
620 rump_pub_lwproc_switch(lwp2);
621 l.l_type = F_RDLCK | F_WRLCK;
622 RL(rump_sys_fcntl(fd2, F_SETLK, &l));
623
624 RL(rump_sys_close(fd2));
625 rump_pub_lwproc_releaselwp();
626
627 RL(rump_sys_close(fd));
628
629 FSTEST_EXIT();
630 }
631
632 static int
633 flock_compare(const void *p, const void *q)
634 {
635 int a = ((const struct flock *)p)->l_start;
636 int b = ((const struct flock *)q)->l_start;
637 return a < b ? -1 : (a > b ? 1 : 0);
638 }
639
640 /*
641 * Find all locks set by fcntl_getlock_pids test
642 * using GETLK for a range [start, start+end], and,
643 * if there is a blocking lock, recursively find
644 * all locks to the left (toward the beginning of
645 * a file) and to the right of the lock.
646 * The function also understands "until end of file"
647 * convention when len==0.
648 */
649 static unsigned int
650 fcntl_getlocks(int fildes, off_t start, off_t len,
651 struct flock *lock, struct flock *end)
652 {
653 unsigned int rv = 0;
654 const struct flock l = { start, len, 0, F_RDLCK, SEEK_SET };
655
656 if (lock == end)
657 return rv;
658
659 RL(rump_sys_fcntl(fildes, F_GETLK, &l));
660
661 if (l.l_type == F_UNLCK)
662 return rv;
663
664 *lock++ = l;
665 rv += 1;
666
667 ATF_REQUIRE(l.l_whence == SEEK_SET);
668
669 if (l.l_start > start) {
670 unsigned int n =
671 fcntl_getlocks(fildes, start, l.l_start - start, lock, end);
672 rv += n;
673 lock += n;
674 if (lock == end)
675 return rv;
676 }
677
678 if (l.l_len == 0) /* does l spans until the end? */
679 return rv;
680
681 if (len == 0) /* are we looking for locks until the end? */ {
682 rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
683 } else if (l.l_start + l.l_len < start + len) {
684 len -= l.l_start + l.l_len - start;
685 rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
686 }
687
688 return rv;
689 }
690
691 static void
692 fcntl_getlock_pids(const atf_tc_t *tc, const char *mp)
693 {
694 /* test non-overlaping ranges */
695 struct flock expect[4];
696 const struct flock lock[4] = {
697 { 0, 2, 0, F_WRLCK, SEEK_SET },
698 { 2, 1, 0, F_WRLCK, SEEK_SET },
699 { 7, 5, 0, F_WRLCK, SEEK_SET },
700 { 4, 3, 0, F_WRLCK, SEEK_SET },
701 };
702
703 /* Add extra element to make sure recursion does't stop at array end */
704 struct flock result[5];
705
706 /* Add 5th process */
707 int fd[5];
708 pid_t pid[5];
709 struct lwp *lwp[5];
710
711 unsigned int i, j;
712 const off_t sz = 8192;
713 int omode = 0755;
714 int oflags = O_RDWR | O_CREAT;
715
716 memcpy(expect, lock, sizeof(lock));
717
718 FSTEST_ENTER();
719
720 /*
721 * First, we create 4 processes and let each lock a range of the
722 * file. Note that the third and fourth processes lock in
723 * "reverse" order, i.e. the greater pid locks a range before
724 * the lesser pid.
725 * Then, we create 5th process which doesn't lock anything.
726 */
727 for (i = 0; i < __arraycount(lwp); i++) {
728 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
729
730 lwp[i] = rump_pub_lwproc_curlwp();
731 pid[i] = rump_sys_getpid();
732
733 RL(fd[i] = rump_sys_open(TESTFILE, oflags, omode));
734 oflags = O_RDWR;
735 omode = 0;
736
737 RL(rump_sys_ftruncate(fd[i], sz));
738
739 if (i < __arraycount(lock)) {
740 RL(rump_sys_fcntl(fd[i], F_SETLK, &lock[i]));
741 expect[i].l_pid = pid[i];
742 }
743 }
744
745 qsort(expect, __arraycount(expect), sizeof(expect[0]), &flock_compare);
746
747 /*
748 * In the context of each process, recursively find all locks
749 * that would block the current process. Processes 1-4 don't
750 * see their own lock, we insert it to simplify checks.
751 * Process 5 sees all 4 locks.
752 */
753 for (i = 0; i < __arraycount(lwp); i++) {
754 unsigned int nlocks;
755
756 rump_pub_lwproc_switch(lwp[i]);
757
758 memset(result, 0, sizeof(result));
759 nlocks = fcntl_getlocks(fd[i], 0, sz,
760 result, result + __arraycount(result));
761
762 if (i < __arraycount(lock)) {
763 ATF_REQUIRE(nlocks < __arraycount(result));
764 result[nlocks] = lock[i];
765 result[nlocks].l_pid = pid[i];
766 nlocks++;
767 }
768
769 ATF_CHECK_EQ(nlocks, __arraycount(expect));
770
771 qsort(result, nlocks, sizeof(result[0]), &flock_compare);
772
773 for (j = 0; j < nlocks; j++) {
774 ATF_CHECK_EQ(result[j].l_start, expect[j].l_start );
775 ATF_CHECK_EQ(result[j].l_len, expect[j].l_len );
776 ATF_CHECK_EQ(result[j].l_pid, expect[j].l_pid );
777 ATF_CHECK_EQ(result[j].l_type, expect[j].l_type );
778 ATF_CHECK_EQ(result[j].l_whence, expect[j].l_whence);
779 }
780 }
781
782 /*
783 * Release processes. This also releases the fds and locks
784 * making fs unmount possible
785 */
786 for (i = 0; i < __arraycount(lwp); i++) {
787 rump_pub_lwproc_switch(lwp[i]);
788 rump_pub_lwproc_releaselwp();
789 }
790
791 FSTEST_EXIT();
792 }
793
794 static void
795 access_simple(const atf_tc_t *tc, const char *mp)
796 {
797 int fd;
798 int tmode;
799
800 FSTEST_ENTER();
801 RL(fd = rump_sys_open("tfile", O_CREAT | O_RDWR, 0777));
802 RL(rump_sys_close(fd));
803
804 #define ALLACC (F_OK | X_OK | W_OK | R_OK)
805 if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))
806 tmode = F_OK;
807 else
808 tmode = ALLACC;
809
810 RL(rump_sys_access("tfile", tmode));
811
812 /* PR kern/44648 */
813 ATF_REQUIRE_ERRNO(EINVAL, rump_sys_access("tfile", ALLACC+1) == -1);
814 #undef ALLACC
815 FSTEST_EXIT();
816 }
817
818 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
819 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
820 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
821 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
822 ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out");
823 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops");
824 ATF_TC_FSAPPLY(rename_dotdot, "rename dir ..");
825 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
826 ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
827 ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
828 ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
829 ATF_TC_FSAPPLY(symlink_zerolen, "symlink with 0-len target");
830 ATF_TC_FSAPPLY(attrs, "check setting attributes works");
831 ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
832 ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
833 ATF_TC_FSAPPLY(access_simple, "access(2)");
834
835 ATF_TP_ADD_TCS(tp)
836 {
837
838 ATF_TP_FSAPPLY(lookup_simple);
839 ATF_TP_FSAPPLY(lookup_complex);
840 ATF_TP_FSAPPLY(dir_simple);
841 ATF_TP_FSAPPLY(dir_notempty);
842 ATF_TP_FSAPPLY(dir_rmdirdotdot);
843 ATF_TP_FSAPPLY(rename_dir);
844 ATF_TP_FSAPPLY(rename_dotdot);
845 ATF_TP_FSAPPLY(rename_reg_nodir);
846 ATF_TP_FSAPPLY(create_nametoolong);
847 ATF_TP_FSAPPLY(create_exist);
848 ATF_TP_FSAPPLY(rename_nametoolong);
849 ATF_TP_FSAPPLY(symlink_zerolen);
850 ATF_TP_FSAPPLY(attrs);
851 ATF_TP_FSAPPLY(fcntl_lock);
852 ATF_TP_FSAPPLY(fcntl_getlock_pids);
853 ATF_TP_FSAPPLY(access_simple);
854
855 return atf_no_error();
856 }
857