t_union.c revision 1.1 1 1.1 pooka /* $NetBSD: t_union.c,v 1.1 2011/01/12 21:13:27 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka #include <sys/types.h>
4 1.1 pooka #include <sys/mount.h>
5 1.1 pooka
6 1.1 pooka #include <atf-c.h>
7 1.1 pooka #include <err.h>
8 1.1 pooka #include <errno.h>
9 1.1 pooka #include <fcntl.h>
10 1.1 pooka #include <stdio.h>
11 1.1 pooka #include <unistd.h>
12 1.1 pooka #include <string.h>
13 1.1 pooka #include <stdlib.h>
14 1.1 pooka
15 1.1 pooka #include <rump/rump.h>
16 1.1 pooka #include <rump/rump_syscalls.h>
17 1.1 pooka
18 1.1 pooka #include <miscfs/union/union.h>
19 1.1 pooka
20 1.1 pooka #include "../../h_macros.h"
21 1.1 pooka #include "../common/h_fsmacros.h"
22 1.1 pooka
23 1.1 pooka #define MSTR "magic bus"
24 1.1 pooka
25 1.1 pooka #define HAS_WHITEOUT (FSTYPE_FFS(tc) || FSTYPE_FFSLOG(tc) || \
26 1.1 pooka FSTYPE_LFS(tc) || FSTYPE_RUMPFS(tc))
27 1.1 pooka
28 1.1 pooka static void
29 1.1 pooka xput_tfile(const char *mp, const char *path)
30 1.1 pooka {
31 1.1 pooka char pathb[MAXPATHLEN];
32 1.1 pooka int fd;
33 1.1 pooka
34 1.1 pooka strcpy(pathb, mp);
35 1.1 pooka strcat(pathb, "/");
36 1.1 pooka strcat(pathb, path);
37 1.1 pooka
38 1.1 pooka RL(fd = rump_sys_open(pathb, O_CREAT | O_RDWR, 0777));
39 1.1 pooka if (rump_sys_write(fd, MSTR, sizeof(MSTR)) != sizeof(MSTR))
40 1.1 pooka atf_tc_fail_errno("write to testfile");
41 1.1 pooka RL(rump_sys_close(fd));
42 1.1 pooka }
43 1.1 pooka
44 1.1 pooka static int
45 1.1 pooka xread_tfile(const char *mp, const char *path)
46 1.1 pooka {
47 1.1 pooka char pathb[MAXPATHLEN];
48 1.1 pooka char buf[128];
49 1.1 pooka int fd;
50 1.1 pooka
51 1.1 pooka strcpy(pathb, mp);
52 1.1 pooka strcat(pathb, "/");
53 1.1 pooka strcat(pathb, path);
54 1.1 pooka
55 1.1 pooka fd = rump_sys_open(pathb, O_RDONLY);
56 1.1 pooka if (fd == -1)
57 1.1 pooka return errno;
58 1.1 pooka if (rump_sys_read(fd, buf, sizeof(buf)) == -1)
59 1.1 pooka atf_tc_fail_errno("read tfile");
60 1.1 pooka RL(rump_sys_close(fd));
61 1.1 pooka if (strcmp(buf, MSTR) == 0)
62 1.1 pooka return 0;
63 1.1 pooka return EPROGMISMATCH;
64 1.1 pooka }
65 1.1 pooka
66 1.1 pooka #define TFILE "tensti"
67 1.1 pooka static void
68 1.1 pooka basic(const atf_tc_t *tc, const char *mp)
69 1.1 pooka {
70 1.1 pooka char lowerpath[MAXPATHLEN];
71 1.1 pooka char dbuf[8192];
72 1.1 pooka struct union_args unionargs;
73 1.1 pooka struct stat sb;
74 1.1 pooka struct dirent *dp;
75 1.1 pooka int error, fd, dsize;
76 1.1 pooka
77 1.1 pooka if (!HAS_WHITEOUT) {
78 1.1 pooka atf_tc_skip("file system does not support VOP_WHITEOUT");
79 1.1 pooka }
80 1.1 pooka
81 1.1 pooka snprintf(lowerpath, sizeof(lowerpath), "%s.lower", mp);
82 1.1 pooka
83 1.1 pooka RL(rump_sys_mkdir(lowerpath, 0777));
84 1.1 pooka
85 1.1 pooka /* create a file in the lower layer */
86 1.1 pooka xput_tfile(lowerpath, TFILE);
87 1.1 pooka
88 1.1 pooka /* mount the union with our testfs as the upper layer */
89 1.1 pooka memset(&unionargs, 0, sizeof(unionargs));
90 1.1 pooka unionargs.target = lowerpath;
91 1.1 pooka unionargs.mntflags = UNMNT_BELOW;
92 1.1 pooka
93 1.1 pooka if (rump_sys_mount(MOUNT_UNION, mp, 0,
94 1.1 pooka &unionargs, sizeof(unionargs)) == -1)
95 1.1 pooka atf_tc_fail_errno("union mount");
96 1.1 pooka
97 1.1 pooka /* first, test we can read the old file from the new namespace */
98 1.1 pooka error = xread_tfile(mp, TFILE);
99 1.1 pooka if (error != 0)
100 1.1 pooka atf_tc_fail("union compare failed: %d (%s)",
101 1.1 pooka error, strerror(error));
102 1.1 pooka
103 1.1 pooka /* then, test upper layer writes don't affect the lower layer */
104 1.1 pooka xput_tfile(mp, "kiekko");
105 1.1 pooka if ((error = xread_tfile(lowerpath, "kiekko")) != ENOENT)
106 1.1 pooka atf_tc_fail("invisibility failed: %d (%s)",
107 1.1 pooka error, strerror(error));
108 1.1 pooka
109 1.1 pooka /* check that we can whiteout stuff in the upper layer */
110 1.1 pooka FSTEST_ENTER();
111 1.1 pooka RL(rump_sys_unlink(TFILE));
112 1.1 pooka ATF_REQUIRE_ERRNO(ENOENT, rump_sys_stat(TFILE, &sb) == -1);
113 1.1 pooka FSTEST_EXIT();
114 1.1 pooka
115 1.1 pooka /* check that the removed node is not in the directory listing */
116 1.1 pooka RL(fd = rump_sys_open(mp, O_RDONLY));
117 1.1 pooka RL(dsize = rump_sys_getdents(fd, dbuf, sizeof(dbuf)));
118 1.1 pooka for (dp = (struct dirent *)dbuf;
119 1.1 pooka (char *)dp < dbuf + dsize;
120 1.1 pooka dp = _DIRENT_NEXT(dp)) {
121 1.1 pooka if (strcmp(dp->d_name, TFILE) == 0 && dp->d_type != DT_WHT)
122 1.1 pooka atf_tc_fail("removed file non-white-outed");
123 1.1 pooka }
124 1.1 pooka RL(rump_sys_close(fd));
125 1.1 pooka
126 1.1 pooka RL(rump_sys_unmount(mp, 0));
127 1.1 pooka }
128 1.1 pooka
129 1.1 pooka ATF_TC_FSAPPLY(basic, "check basic union functionality");
130 1.1 pooka
131 1.1 pooka ATF_TP_ADD_TCS(tp)
132 1.1 pooka {
133 1.1 pooka
134 1.1 pooka ATF_TP_FSAPPLY(basic);
135 1.1 pooka return atf_no_error();
136 1.1 pooka }
137