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