Home | History | Annotate | Line # | Download | only in common
snapshot.c revision 1.6
      1  1.6  bouyer /*	$NetBSD: snapshot.c,v 1.6 2011/02/12 18:13:46 bouyer Exp $	*/
      2  1.1   pooka 
      3  1.1   pooka #include <sys/types.h>
      4  1.1   pooka #include <sys/ioctl.h>
      5  1.1   pooka #include <sys/mount.h>
      6  1.1   pooka 
      7  1.1   pooka #include <dev/fssvar.h>
      8  1.1   pooka 
      9  1.1   pooka #include <atf-c.h>
     10  1.1   pooka #include <fcntl.h>
     11  1.1   pooka #include <stdio.h>
     12  1.1   pooka #include <stdlib.h>
     13  1.1   pooka #include <string.h>
     14  1.1   pooka #include <unistd.h>
     15  1.1   pooka 
     16  1.1   pooka ATF_TC_WITH_CLEANUP(snapshot);
     17  1.1   pooka ATF_TC_HEAD(snapshot, tc)
     18  1.1   pooka {
     19  1.1   pooka 
     20  1.1   pooka 	atf_tc_set_md_var(tc, "descr", "basic snapshot features");
     21  1.1   pooka }
     22  1.1   pooka 
     23  1.1   pooka static void
     24  1.1   pooka makefile(const char *path)
     25  1.1   pooka {
     26  1.1   pooka 	int fd;
     27  1.1   pooka 
     28  1.1   pooka 	fd = rump_sys_open(path, O_CREAT | O_RDWR, 0777);
     29  1.1   pooka 	if (fd == -1)
     30  1.1   pooka 		atf_tc_fail_errno("create %s", path);
     31  1.1   pooka 	rump_sys_close(fd);
     32  1.1   pooka }
     33  1.1   pooka 
     34  1.1   pooka ATF_TC_BODY(snapshot, tc)
     35  1.1   pooka {
     36  1.1   pooka 	char buf[1024];
     37  1.1   pooka 	struct fss_set fss;
     38  1.1   pooka 	int fssfd;
     39  1.1   pooka 	int fd, fd2, i;
     40  1.1   pooka 
     41  1.1   pooka 	if (system(NEWFS) == -1)
     42  1.1   pooka 		atf_tc_fail_errno("cannot create file system");
     43  1.1   pooka 
     44  1.1   pooka 	rump_init();
     45  1.1   pooka 	begin();
     46  1.1   pooka 
     47  1.1   pooka 	if (rump_sys_mkdir("/mnt", 0777) == -1)
     48  1.1   pooka 		atf_tc_fail_errno("mount point create");
     49  1.1   pooka 	if (rump_sys_mkdir("/snap", 0777) == -1)
     50  1.1   pooka 		atf_tc_fail_errno("mount point 2 create");
     51  1.1   pooka 
     52  1.1   pooka 	rump_pub_etfs_register("/diskdev", IMGNAME, RUMP_ETFS_BLK);
     53  1.1   pooka 
     54  1.1   pooka 	mount_diskfs("/diskdev", "/mnt");
     55  1.1   pooka 
     56  1.1   pooka #define TESTSTR1 "huihai\n"
     57  1.1   pooka #define TESTSZ1 (sizeof(TESTSTR1)-1)
     58  1.1   pooka #define TESTSTR2 "baana liten\n"
     59  1.1   pooka #define TESTSZ2 (sizeof(TESTSTR2)-1)
     60  1.1   pooka 
     61  1.1   pooka 	fd = rump_sys_open("/mnt/myfile", O_RDWR | O_CREAT, 0777);
     62  1.1   pooka 	if (fd == -1)
     63  1.1   pooka 		atf_tc_fail_errno("create file");
     64  1.1   pooka 	if (rump_sys_write(fd, TESTSTR1, TESTSZ1) != TESTSZ1)
     65  1.1   pooka 		atf_tc_fail_errno("write fail");
     66  1.1   pooka 
     67  1.1   pooka 	fssfd = rump_sys_open("/dev/rfss0", O_RDWR);
     68  1.6  bouyer 	if (fssfd == -1)
     69  1.1   pooka 		atf_tc_fail_errno("cannot open fss");
     70  1.1   pooka 	makefile(BAKNAME);
     71  1.1   pooka 	memset(&fss, 0, sizeof(fss));
     72  1.1   pooka 	fss.fss_mount = __UNCONST("/mnt");
     73  1.1   pooka 	fss.fss_bstore = __UNCONST(BAKNAME);
     74  1.1   pooka 	fss.fss_csize = 0;
     75  1.1   pooka 	if (rump_sys_ioctl(fssfd, FSSIOCSET, &fss) == -1)
     76  1.1   pooka 		atf_tc_fail_errno("create snapshot");
     77  1.1   pooka 
     78  1.1   pooka 	for (i = 0; i < 10000; i++) {
     79  1.1   pooka 		if (rump_sys_write(fd, TESTSTR2, TESTSZ2) != TESTSZ2)
     80  1.1   pooka 			atf_tc_fail_errno("write fail");
     81  1.1   pooka 	}
     82  1.1   pooka 	rump_sys_sync();
     83  1.1   pooka 
     84  1.1   pooka 	/* technically we should fsck it first? */
     85  1.1   pooka 	mount_diskfs("/dev/fss0", "/snap");
     86  1.1   pooka 
     87  1.1   pooka 	/* check for old contents */
     88  1.1   pooka 	fd2 = rump_sys_open("/snap/myfile", O_RDONLY);
     89  1.1   pooka 	if (fd2 == -1)
     90  1.1   pooka 		atf_tc_fail_errno("fail");
     91  1.1   pooka 	memset(buf, 0, sizeof(buf));
     92  1.1   pooka 	if (rump_sys_read(fd2, buf, sizeof(buf)) == -1)
     93  1.1   pooka 		atf_tc_fail_errno("read snap");
     94  1.1   pooka 	ATF_CHECK(strcmp(buf, TESTSTR1) == 0);
     95  1.1   pooka 
     96  1.1   pooka 	/* check that new files are invisible in the snapshot */
     97  1.1   pooka 	makefile("/mnt/newfile");
     98  1.1   pooka 	if (rump_sys_open("/snap/newfile", O_RDONLY) != -1)
     99  1.1   pooka 		atf_tc_fail("newfile exists in snapshot");
    100  1.1   pooka 	if (errno != ENOENT)
    101  1.1   pooka 		atf_tc_fail_errno("newfile open should fail with ENOENT");
    102  1.1   pooka 
    103  1.1   pooka 	/* check that removed files are still visible in the snapshot */
    104  1.1   pooka 	rump_sys_unlink("/mnt/myfile");
    105  1.1   pooka 	if (rump_sys_open("/snap/myfile", O_RDONLY) == -1)
    106  1.1   pooka 		atf_tc_fail_errno("unlinked file no longer in snapshot");
    107  1.1   pooka 
    108  1.1   pooka 	/* done for now */
    109  1.1   pooka }
    110  1.1   pooka 
    111  1.1   pooka ATF_TC_CLEANUP(snapshot, tc)
    112  1.1   pooka {
    113  1.1   pooka 
    114  1.1   pooka 	unlink(IMGNAME);
    115  1.1   pooka }
    116  1.1   pooka 
    117  1.1   pooka ATF_TP_ADD_TCS(tp)
    118  1.1   pooka {
    119  1.1   pooka 	ATF_TP_ADD_TC(tp, snapshot);
    120  1.1   pooka 	return 0;
    121  1.1   pooka }
    122