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