1 /* $NetBSD: snapshot.c,v 1.7 2013/02/06 09:05:01 hannken Exp $ */
7 #include <dev/fssvar.h>
17 ATF_TC_WITH_CLEANUP(snapshot);
18 ATF_TC_HEAD(snapshot, tc)
21 atf_tc_set_md_var(tc, "descr", "basic snapshot features");
25 makefile(const char *path)
29 fd = rump_sys_open(path, O_CREAT | O_RDWR, 0777);
31 atf_tc_fail_errno("create %s", path);
35 ATF_TC_BODY(snapshot, tc)
42 if (system(NEWFS) == -1)
43 atf_tc_fail_errno("cannot create file system");
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");
53 rump_pub_etfs_register("/diskdev", IMGNAME, RUMP_ETFS_BLK);
55 mount_diskfs("/diskdev", "/mnt");
57 #define TESTSTR1 "huihai\n"
58 #define TESTSZ1 (sizeof(TESTSTR1)-1)
59 #define TESTSTR2 "baana liten\n"
60 #define TESTSZ2 (sizeof(TESTSTR2)-1)
62 fd = rump_sys_open("/mnt/myfile", O_RDWR | O_CREAT, 0777);
64 atf_tc_fail_errno("create file");
65 if (rump_sys_write(fd, TESTSTR1, TESTSZ1) != TESTSZ1)
66 atf_tc_fail_errno("write fail");
68 fssfd = rump_sys_open("/dev/rfss0", O_RDWR);
70 atf_tc_fail_errno("cannot open fss");
72 memset(&fss, 0, sizeof(fss));
73 fss.fss_mount = __UNCONST("/mnt");
74 fss.fss_bstore = __UNCONST(BAKNAME);
76 if (rump_sys_ioctl(fssfd, FSSIOCSET, &fss) == -1)
77 atf_tc_fail_errno("create snapshot");
79 for (i = 0; i < 10000; i++) {
80 if (rump_sys_write(fd, TESTSTR2, TESTSZ2) != TESTSZ2)
81 atf_tc_fail_errno("write fail");
85 /* technically we should fsck it first? */
86 mount_diskfs("/dev/fss0", "/snap");
88 /* check for old contents */
89 fd2 = rump_sys_open("/snap/myfile", O_RDONLY);
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);
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");
102 atf_tc_fail_errno("newfile open should fail with ENOENT");
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");
112 ATF_TC_CLEANUP(snapshot, tc)
118 ATF_TC_WITH_CLEANUP(snapshotstress);
119 ATF_TC_HEAD(snapshotstress, tc)
122 atf_tc_set_md_var(tc, "descr", "snapshot on active file system");
127 static bool activity_stop = false;
131 fs_activity(void *arg)
134 char *prefix = arg, path[128];
136 rump_pub_lwproc_newlwp(wrkpid);
138 RL(rump_sys_mkdir(prefix, 0777));
139 while (! activity_stop) {
140 for (di = 0; di < 5; di++) {
141 snprintf(path, sizeof(path), "%s/d%d", prefix, di);
142 RL(rump_sys_mkdir(path, 0777));
143 for (fi = 0; fi < 5; fi++) {
144 snprintf(path, sizeof(path), "%s/d%d/f%d",
149 for (di = 0; di < 5; di++) {
150 for (fi = 0; fi < 5; fi++) {
151 snprintf(path, sizeof(path), "%s/d%d/f%d",
153 RL(rump_sys_unlink(path));
155 snprintf(path, sizeof(path), "%s/d%d", prefix, di);
156 RL(rump_sys_rmdir(path));
159 RL(rump_sys_rmdir(prefix));
161 rump_pub_lwproc_releaselwp();
166 ATF_TC_BODY(snapshotstress, tc)
168 pthread_t at[NACTIVITY];
170 char prefix[NACTIVITY][128];
173 if (system(NEWFS) == -1)
174 atf_tc_fail_errno("cannot create file system");
175 /* Force SMP so the stress makes sense. */
176 RL(setenv("RUMP_NCPU", "4", 1));
178 /* Prepare for fsck to use the RUMP /dev/fss0. */
179 RL(rump_init_server("unix://commsock"));
180 RL(setenv("LD_PRELOAD", "/usr/lib/librumphijack.so", 1));
181 RL(setenv("RUMP_SERVER", "unix://commsock", 1));
182 RL(setenv("RUMPHIJACK", "blanket=/dev/rfss0", 1));
185 RL(rump_sys_mkdir("/mnt", 0777));
187 rump_pub_etfs_register("/diskdev", IMGNAME, RUMP_ETFS_BLK);
189 mount_diskfs("/diskdev", "/mnt");
191 /* Start file system activity. */
192 RL(wrkpid = rump_sys_getpid());
193 for (i = 0; i < NACTIVITY; i++) {
194 snprintf(prefix[i], sizeof(prefix[i]), "/mnt/a%d", i);
195 RL(pthread_create(&at[i], NULL, fs_activity, prefix[i]));
199 fssfd = rump_sys_open("/dev/rfss0", O_RDWR);
201 atf_tc_fail_errno("cannot open fss");
203 memset(&fss, 0, sizeof(fss));
204 fss.fss_mount = __UNCONST("/mnt");
205 fss.fss_bstore = __UNCONST(BAKNAME);
207 if (rump_sys_ioctl(fssfd, FSSIOCSET, &fss) == -1)
208 atf_tc_fail_errno("create snapshot");
210 activity_stop = true;
211 for (i = 0; i < NACTIVITY; i++)
212 RL(pthread_join(at[i], NULL));
214 RL(system(FSCK " /dev/rfss0"));
217 ATF_TC_CLEANUP(snapshotstress, tc)
225 ATF_TP_ADD_TC(tp, snapshot);
226 ATF_TP_ADD_TC(tp, snapshotstress);