t_vnops.c revision 1.18 1 /* $NetBSD: t_vnops.c,v 1.18 2011/03/01 15:04:47 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 <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 ATF_REQUIRE_ERRNO(xerrno, rump_sys_chdir("..") == -1);
179 FSTEST_EXIT();
180 }
181
182 static void
183 checkfile(const char *path, struct stat *refp)
184 {
185 char buf[MAXPATHLEN];
186 struct stat sb;
187 static int n = 1;
188
189 md(buf, path, "file");
190 if (rump_sys_stat(buf, &sb) == -1)
191 atf_tc_fail_errno("cannot stat file %d (%s)", n, buf);
192 if (memcmp(&sb, refp, sizeof(sb)) != 0)
193 atf_tc_fail("stat mismatch %d", n);
194 n++;
195 }
196
197 static void
198 rename_dir(const atf_tc_t *tc, const char *mp)
199 {
200 char pb1[MAXPATHLEN], pb2[MAXPATHLEN], pb3[MAXPATHLEN];
201 struct stat ref, sb;
202
203 //if (FSTYPE_MSDOS(tc))
204 //atf_tc_skip("test fails in some setups, reason unknown");
205
206 if (FSTYPE_RUMPFS(tc))
207 atf_tc_skip("rename not supported by fs");
208
209 USES_DIRS;
210
211 md(pb1, mp, "dir1");
212 if (rump_sys_mkdir(pb1, 0777) == -1)
213 atf_tc_fail_errno("mkdir 1");
214
215 md(pb2, mp, "dir2");
216 if (rump_sys_mkdir(pb2, 0777) == -1)
217 atf_tc_fail_errno("mkdir 2");
218 md(pb2, mp, "dir2/subdir");
219 if (rump_sys_mkdir(pb2, 0777) == -1)
220 atf_tc_fail_errno("mkdir 3");
221
222 md(pb3, mp, "dir1/file");
223 if (rump_sys_mknod(pb3, S_IFREG | 0777, -1) == -1)
224 atf_tc_fail_errno("create file");
225 if (rump_sys_stat(pb3, &ref) == -1)
226 atf_tc_fail_errno("stat of file");
227
228 /*
229 * First try ops which should succeed.
230 */
231
232 /* rename within directory */
233 md(pb3, mp, "dir3");
234 if (rump_sys_rename(pb1, pb3) == -1)
235 atf_tc_fail_errno("rename 1");
236 checkfile(pb3, &ref);
237
238 /* rename directory onto itself (two ways, should fail) */
239 md(pb1, mp, "dir3/.");
240 if (rump_sys_rename(pb1, pb3) != -1 || errno != EINVAL)
241 atf_tc_fail_errno("rename 2");
242 if (rump_sys_rename(pb3, pb1) != -1 || errno != EISDIR)
243 atf_tc_fail_errno("rename 3");
244
245 checkfile(pb3, &ref);
246
247 /* rename father of directory into directory */
248 md(pb1, mp, "dir2/dir");
249 md(pb2, mp, "dir2");
250 if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
251 atf_tc_fail_errno("rename 4");
252
253 /* same for grandfather */
254 md(pb1, mp, "dir2/subdir/dir2");
255 if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
256 atf_tc_fail("rename 5");
257
258 checkfile(pb3, &ref);
259
260 /* rename directory over a non-empty directory */
261 if (rump_sys_rename(pb2, pb3) != -1 || errno != ENOTEMPTY)
262 atf_tc_fail("rename 6");
263
264 /* cross-directory rename */
265 md(pb1, mp, "dir3");
266 md(pb2, mp, "dir2/somedir");
267 if (rump_sys_rename(pb1, pb2) == -1)
268 atf_tc_fail_errno("rename 7");
269 checkfile(pb2, &ref);
270
271 /* move to parent directory */
272 md(pb1, mp, "dir2/somedir/../../dir3");
273 if (rump_sys_rename(pb2, pb1) == -1)
274 atf_tc_fail_errno("rename 8");
275 md(pb1, mp, "dir2/../dir3");
276 checkfile(pb1, &ref);
277
278 /* atomic cross-directory rename */
279 md(pb3, mp, "dir2/subdir");
280 if (rump_sys_rename(pb1, pb3) == -1)
281 atf_tc_fail_errno("rename 9");
282 checkfile(pb3, &ref);
283
284 /* rename directory over an empty directory */
285 md(pb1, mp, "parent");
286 md(pb2, mp, "parent/dir1");
287 md(pb3, mp, "parent/dir2");
288 RL(rump_sys_mkdir(pb1, 0777));
289 RL(rump_sys_mkdir(pb2, 0777));
290 RL(rump_sys_mkdir(pb3, 0777));
291 RL(rump_sys_rename(pb2, pb3));
292
293 RL(rump_sys_stat(pb1, &sb));
294 ATF_CHECK_EQ(sb.st_nlink, 3);
295 RL(rump_sys_rmdir(pb3));
296 if (FSTYPE_TMPFS(tc))
297 atf_tc_expect_signal(-1, "PR kern/44288");
298 RL(rump_sys_rmdir(pb1));
299 }
300
301 static void
302 rename_dotdot(const atf_tc_t *tc, const char *mp)
303 {
304
305 if (FSTYPE_RUMPFS(tc))
306 atf_tc_skip("rename not supported by fs");
307
308 USES_DIRS;
309
310 if (rump_sys_chdir(mp) == -1)
311 atf_tc_fail_errno("chdir mountpoint");
312
313 if (rump_sys_mkdir("dir1", 0777) == -1)
314 atf_tc_fail_errno("mkdir 1");
315 if (rump_sys_mkdir("dir2", 0777) == -1)
316 atf_tc_fail_errno("mkdir 2");
317
318 if (rump_sys_rename("dir1", "dir1/..") != -1 || errno != EINVAL)
319 atf_tc_fail_errno("self-dotdot to");
320
321 if (rump_sys_rename("dir1/..", "sometarget") != -1 || errno != EINVAL)
322 atf_tc_fail_errno("self-dotdot from");
323 atf_tc_expect_pass();
324
325 if (FSTYPE_TMPFS(tc)) {
326 atf_tc_expect_fail("PR kern/43617");
327 }
328 if (rump_sys_rename("dir1", "dir2/..") != -1 || errno != EINVAL)
329 atf_tc_fail("other-dotdot");
330
331 rump_sys_chdir("/");
332 }
333
334 static void
335 rename_reg_nodir(const atf_tc_t *tc, const char *mp)
336 {
337 bool haslinks;
338 struct stat sb;
339 ino_t f1ino, f2ino;
340
341 if (FSTYPE_RUMPFS(tc))
342 atf_tc_skip("rename not supported by fs");
343
344 if (FSTYPE_MSDOS(tc))
345 atf_tc_skip("test fails in some setups, reason unknown");
346
347 if (rump_sys_chdir(mp) == -1)
348 atf_tc_fail_errno("chdir mountpoint");
349
350 if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
351 haslinks = false;
352 else
353 haslinks = true;
354
355 if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
356 atf_tc_fail_errno("create file");
357 if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
358 atf_tc_fail_errno("create file");
359
360 if (rump_sys_stat("file1", &sb) == -1)
361 atf_tc_fail_errno("stat");
362 f1ino = sb.st_ino;
363
364 if (haslinks) {
365 if (rump_sys_link("file1", "file_link") == -1)
366 atf_tc_fail_errno("link");
367 if (rump_sys_stat("file_link", &sb) == -1)
368 atf_tc_fail_errno("stat");
369 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
370 ATF_REQUIRE_EQ(sb.st_nlink, 2);
371 }
372
373 if (rump_sys_stat("file2", &sb) == -1)
374 atf_tc_fail_errno("stat");
375 f2ino = sb.st_ino;
376
377 if (rump_sys_rename("file1", "file3") == -1)
378 atf_tc_fail_errno("rename 1");
379 if (rump_sys_stat("file3", &sb) == -1)
380 atf_tc_fail_errno("stat 1");
381 if (haslinks) {
382 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
383 }
384 if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
385 atf_tc_fail_errno("source 1");
386
387 if (rump_sys_rename("file3", "file2") == -1)
388 atf_tc_fail_errno("rename 2");
389 if (rump_sys_stat("file2", &sb) == -1)
390 atf_tc_fail_errno("stat 2");
391 if (haslinks) {
392 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
393 }
394
395 if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
396 atf_tc_fail_errno("source 2");
397
398 if (haslinks) {
399 if (rump_sys_rename("file2", "file_link") == -1)
400 atf_tc_fail_errno("rename hardlink");
401 if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
402 atf_tc_fail_errno("source 3");
403 if (rump_sys_stat("file_link", &sb) == -1)
404 atf_tc_fail_errno("stat 2");
405 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
406 ATF_REQUIRE_EQ(sb.st_nlink, 1);
407 }
408
409 rump_sys_chdir("/");
410 }
411
412 static void
413 create_nametoolong(const atf_tc_t *tc, const char *mp)
414 {
415 char *name;
416 int fd;
417 long val;
418 size_t len;
419
420 if (rump_sys_chdir(mp) == -1)
421 atf_tc_fail_errno("chdir mountpoint");
422
423 val = rump_sys_pathconf(".", _PC_NAME_MAX);
424 if (val == -1)
425 atf_tc_fail_errno("pathconf");
426
427 len = val + 1;
428 name = malloc(len+1);
429 if (name == NULL)
430 atf_tc_fail_errno("malloc");
431
432 memset(name, 'a', len);
433 *(name+len) = '\0';
434
435 val = rump_sys_pathconf(".", _PC_NO_TRUNC);
436 if (val == -1)
437 atf_tc_fail_errno("pathconf");
438
439 if (FSTYPE_MSDOS(tc))
440 atf_tc_expect_fail("PR kern/43670");
441 fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666);
442 if (val != 0 && (fd != -1 || errno != ENAMETOOLONG))
443 atf_tc_fail_errno("open");
444
445 if (val == 0 && rump_sys_close(fd) == -1)
446 atf_tc_fail_errno("close");
447 if (val == 0 && rump_sys_unlink(name) == -1)
448 atf_tc_fail_errno("unlink");
449
450 free(name);
451
452 rump_sys_chdir("/");
453 }
454
455 static void
456 create_exist(const atf_tc_t *tc, const char *mp)
457 {
458 const char *name = "hoge";
459 int fd;
460
461 RL(rump_sys_chdir(mp));
462 RL(fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666));
463 RL(rump_sys_close(fd));
464 RL(rump_sys_unlink(name));
465 RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
466 RL(rump_sys_close(fd));
467 RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
468 RL(rump_sys_close(fd));
469 ATF_REQUIRE_ERRNO(EEXIST,
470 (fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666)));
471 RL(rump_sys_unlink(name));
472 RL(rump_sys_chdir("/"));
473 }
474
475 static void
476 rename_nametoolong(const atf_tc_t *tc, const char *mp)
477 {
478 char *name;
479 int res, fd;
480 long val;
481 size_t len;
482
483 if (FSTYPE_RUMPFS(tc))
484 atf_tc_skip("rename not supported by fs");
485
486 if (rump_sys_chdir(mp) == -1)
487 atf_tc_fail_errno("chdir mountpoint");
488
489 val = rump_sys_pathconf(".", _PC_NAME_MAX);
490 if (val == -1)
491 atf_tc_fail_errno("pathconf");
492
493 len = val + 1;
494 name = malloc(len+1);
495 if (name == NULL)
496 atf_tc_fail_errno("malloc");
497
498 memset(name, 'a', len);
499 *(name+len) = '\0';
500
501 fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
502 if (fd == -1)
503 atf_tc_fail_errno("open");
504 if (rump_sys_close(fd) == -1)
505 atf_tc_fail_errno("close");
506
507 val = rump_sys_pathconf(".", _PC_NO_TRUNC);
508 if (val == -1)
509 atf_tc_fail_errno("pathconf");
510
511 if (FSTYPE_MSDOS(tc))
512 atf_tc_expect_fail("PR kern/43670");
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 if (FSTYPE_TMPFS(tc)) {
534 atf_tc_expect_signal(SIGABRT, "PR kern/43843");
535 }
536
537 RL(rump_sys_symlink("", "afile"));
538 RL(rump_sys_chdir("/"));
539 }
540
541 static void
542 attrs(const atf_tc_t *tc, const char *mp)
543 {
544 struct stat sb, sb2;
545 struct timeval tv[2];
546 int fd;
547
548 FSTEST_ENTER();
549 RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
550 RL(rump_sys_close(fd));
551 RL(rump_sys_stat(TESTFILE, &sb));
552 if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
553 RL(rump_sys_chown(TESTFILE, 1, 2));
554 sb.st_uid = 1;
555 sb.st_gid = 2;
556 RL(rump_sys_chmod(TESTFILE, 0123));
557 sb.st_mode = (sb.st_mode & ~ACCESSPERMS) | 0123;
558 }
559
560 tv[0].tv_sec = 1000000000; /* need something >1980 for msdosfs */
561 tv[0].tv_usec = 1;
562 tv[1].tv_sec = 1000000002; /* need even seconds for msdosfs */
563 tv[1].tv_usec = 3;
564 RL(rump_sys_utimes(TESTFILE, tv));
565 RL(rump_sys_utimes(TESTFILE, tv)); /* XXX: utimes & birthtime */
566 sb.st_atimespec.tv_sec = 1000000000;
567 sb.st_atimespec.tv_nsec = 1000;
568 sb.st_mtimespec.tv_sec = 1000000002;
569 sb.st_mtimespec.tv_nsec = 3000;
570
571 RL(rump_sys_stat(TESTFILE, &sb2));
572 #define CHECK(a) ATF_REQUIRE_EQ(sb.a, sb2.a)
573 if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
574 CHECK(st_uid);
575 CHECK(st_gid);
576 CHECK(st_mode);
577 }
578 if (!FSTYPE_MSDOS(tc)) {
579 /* msdosfs has only access date, not time */
580 CHECK(st_atimespec.tv_sec);
581 }
582 CHECK(st_mtimespec.tv_sec);
583 if (!(FSTYPE_EXT2FS(tc) || FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
584 CHECK(st_atimespec.tv_nsec);
585 CHECK(st_mtimespec.tv_nsec);
586 }
587 #undef CHECK
588
589 FSTEST_EXIT();
590 }
591
592 static void
593 fcntl_lock(const atf_tc_t *tc, const char *mp)
594 {
595 int fd, fd2;
596 struct flock l;
597 struct lwp *lwp1, *lwp2;
598
599 FSTEST_ENTER();
600 l.l_pid = 0;
601 l.l_start = l.l_len = 1024;
602 l.l_type = F_RDLCK | F_WRLCK;
603 l.l_whence = SEEK_END;
604
605 lwp1 = rump_pub_lwproc_curlwp();
606 RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
607 RL(rump_sys_ftruncate(fd, 8192));
608
609 /* PR kern/43321 */
610 RL(rump_sys_fcntl(fd, F_SETLK, &l));
611
612 /* Next, we fork and try to lock the same area */
613 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
614 lwp2 = rump_pub_lwproc_curlwp();
615 RL(fd2 = rump_sys_open(TESTFILE, O_RDWR, 0));
616 ATF_REQUIRE_ERRNO(EAGAIN, rump_sys_fcntl(fd2, F_SETLK, &l));
617
618 /* Switch back and unlock... */
619 rump_pub_lwproc_switch(lwp1);
620 l.l_type = F_UNLCK;
621 RL(rump_sys_fcntl(fd, F_SETLK, &l));
622
623 /* ... and try to lock again */
624 rump_pub_lwproc_switch(lwp2);
625 l.l_type = F_RDLCK | F_WRLCK;
626 RL(rump_sys_fcntl(fd2, F_SETLK, &l));
627
628 RL(rump_sys_close(fd2));
629 rump_pub_lwproc_releaselwp();
630
631 RL(rump_sys_close(fd));
632
633 FSTEST_EXIT();
634 }
635
636 static int
637 flock_compare(const void *p, const void *q)
638 {
639 int a = ((const struct flock *)p)->l_start;
640 int b = ((const struct flock *)q)->l_start;
641 return a < b ? -1 : (a > b ? 1 : 0);
642 }
643
644 static void
645 fcntl_getlock_pids(const atf_tc_t *tc, const char *mp)
646 {
647 /* test non-overlaping ranges */
648 struct flock expect[4];
649 const struct flock lock[4] = {
650 { 0, 2, 0, F_WRLCK, SEEK_SET },
651 { 2, 1, 0, F_WRLCK, SEEK_SET },
652 { 7, 5, 0, F_WRLCK, SEEK_SET },
653 { 4, 3, 0, F_WRLCK, SEEK_SET },
654 };
655
656 int fd[4];
657 struct lwp *lwp[4];
658 pid_t prevpid = 0;
659
660 unsigned int i, j;
661 const off_t sz = 8192;
662 int omode = 0755;
663 int oflags = O_RDWR | O_CREAT;
664
665 memcpy(expect, lock, sizeof(lock));
666 qsort(expect, __arraycount(expect), sizeof(expect[0]), &flock_compare);
667
668 FSTEST_ENTER();
669
670 /*
671 * First, we create 4 processes and let each lock a range of the
672 * file. Note that the third and fourth processes lock in
673 * "reverse" order, i.e. the greater pid locks a range before
674 * the lesser pid.
675 */
676 for(i = 0; i < __arraycount(lwp); i++) {
677 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
678
679 lwp[i] = rump_pub_lwproc_curlwp();
680 assert(rump_sys_getpid() > prevpid);
681 prevpid = rump_sys_getpid();
682
683 RL(fd[i] = rump_sys_open(TESTFILE, oflags, omode));
684 oflags = O_RDWR;
685 omode = 0;
686
687 RL(rump_sys_ftruncate(fd[i], sz));
688 RL(rump_sys_fcntl(fd[i], F_SETLK, &lock[i]));
689 }
690
691 atf_tc_expect_fail("PR kern/44494");
692 /*
693 * In the context of each pid , do GETLK for a readlock from
694 * i = [0,__arraycount(locks)]. If we try to lock from the same
695 * start offset as the lock our current process holds, check
696 * that we fail on the offset of the next lock ("else if" branch).
697 * Otherwise, expect to get a lock for the current offset
698 * ("if" branch). The "else" branch is purely for the last
699 * process where we expect no blocking locks.
700 */
701 for(i = 0; i < __arraycount(lwp); i++) {
702 rump_pub_lwproc_switch(lwp[i]);
703
704 for(j = 0; j < __arraycount(lwp); j++) {
705 struct flock l;
706 l = expect[j];
707 l.l_len = sz;
708 l.l_type = F_RDLCK;
709
710 RL(rump_sys_fcntl(fd[i], F_GETLK, &l));
711
712 if(expect[j].l_start != lock[i].l_start) {
713 /*
714 * lock set by another process
715 */
716 ATF_CHECK(l.l_type != F_UNLCK);
717 ATF_CHECK_EQ(l.l_start, expect[j].l_start);
718 ATF_CHECK_EQ(l.l_len, expect[j].l_len);
719 } else if (j != __arraycount(lwp) - 1) {
720 /*
721 * lock set by the current process
722 */
723 ATF_CHECK(l.l_type != F_UNLCK);
724 ATF_CHECK_EQ(l.l_start, expect[j+1].l_start);
725 ATF_CHECK_EQ(l.l_len, expect[j+1].l_len);
726 } else {
727 /*
728 * there are no other locks after the
729 * current process lock
730 */
731 ATF_CHECK_EQ(l.l_type, F_UNLCK);
732 ATF_CHECK_EQ(l.l_start, expect[j].l_start);
733 ATF_CHECK_EQ(l.l_len, sz);
734 ATF_CHECK_EQ(l.l_pid, expect[j].l_pid);
735 ATF_CHECK_EQ(l.l_whence, expect[j].l_whence);
736 }
737 }
738 }
739
740 /*
741 * Release processes. This also releases the fds and locks
742 * making fs unmount possible
743 */
744 for(i = 0; i < __arraycount(lwp); i++) {
745 rump_pub_lwproc_switch(lwp[i]);
746 rump_pub_lwproc_releaselwp();
747 }
748
749 FSTEST_EXIT();
750 }
751
752 static void
753 access_simple(const atf_tc_t *tc, const char *mp)
754 {
755 int fd;
756 int tmode;
757
758 FSTEST_ENTER();
759 RL(fd = rump_sys_open("tfile", O_CREAT | O_RDWR, 0777));
760 RL(rump_sys_close(fd));
761
762 #define ALLACC (F_OK | X_OK | W_OK | R_OK)
763 if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))
764 tmode = F_OK;
765 else
766 tmode = ALLACC;
767
768 RL(rump_sys_access("tfile", tmode));
769
770 /* PR kern/44648 */
771 ATF_REQUIRE_ERRNO(EINVAL, rump_sys_access("tfile", ALLACC+1) == -1);
772 #undef ALLACC
773 FSTEST_EXIT();
774 }
775
776 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
777 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
778 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
779 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
780 ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out");
781 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops");
782 ATF_TC_FSAPPLY(rename_dotdot, "rename dir ..");
783 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
784 ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
785 ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
786 ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
787 ATF_TC_FSAPPLY(symlink_zerolen, "symlink with 0-len target");
788 ATF_TC_FSAPPLY(attrs, "check setting attributes works");
789 ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
790 ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
791 ATF_TC_FSAPPLY(access_simple, "access(2)");
792
793 ATF_TP_ADD_TCS(tp)
794 {
795
796 ATF_TP_FSAPPLY(lookup_simple);
797 ATF_TP_FSAPPLY(lookup_complex);
798 ATF_TP_FSAPPLY(dir_simple);
799 ATF_TP_FSAPPLY(dir_notempty);
800 ATF_TP_FSAPPLY(dir_rmdirdotdot);
801 ATF_TP_FSAPPLY(rename_dir);
802 ATF_TP_FSAPPLY(rename_dotdot);
803 ATF_TP_FSAPPLY(rename_reg_nodir);
804 ATF_TP_FSAPPLY(create_nametoolong);
805 ATF_TP_FSAPPLY(create_exist);
806 ATF_TP_FSAPPLY(rename_nametoolong);
807 ATF_TP_FSAPPLY(symlink_zerolen);
808 ATF_TP_FSAPPLY(attrs);
809 ATF_TP_FSAPPLY(fcntl_lock);
810 ATF_TP_FSAPPLY(fcntl_getlock_pids);
811 ATF_TP_FSAPPLY(access_simple);
812
813 return atf_no_error();
814 }
815