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