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