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