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