Remove building with NOCRYPTO option
[minix.git] / tests / fs / common / snapshot.c
blob7baf611b6ed60483adbb7e2cc22c480176a39b6f
1 /* $NetBSD: snapshot.c,v 1.7 2013/02/06 09:05:01 hannken Exp $ */
3 #include <sys/types.h>
4 #include <sys/ioctl.h>
5 #include <sys/mount.h>
7 #include <dev/fssvar.h>
9 #include <atf-c.h>
10 #include <fcntl.h>
11 #include <pthread.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
17 ATF_TC_WITH_CLEANUP(snapshot);
18 ATF_TC_HEAD(snapshot, tc)
21 atf_tc_set_md_var(tc, "descr", "basic snapshot features");
24 static void
25 makefile(const char *path)
27 int fd;
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);
35 ATF_TC_BODY(snapshot, tc)
37 char buf[1024];
38 struct fss_set fss;
39 int fssfd;
40 int fd, fd2, i;
42 if (system(NEWFS) == -1)
43 atf_tc_fail_errno("cannot create file system");
45 rump_init();
46 begin();
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);
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");
68 fssfd = rump_sys_open("/dev/rfss0", O_RDWR);
69 if (fssfd == -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");
79 for (i = 0; i < 10000; i++) {
80 if (rump_sys_write(fd, TESTSTR2, TESTSZ2) != TESTSZ2)
81 atf_tc_fail_errno("write fail");
83 rump_sys_sync();
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);
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);
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");
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");
109 /* done for now */
112 ATF_TC_CLEANUP(snapshot, tc)
115 unlink(IMGNAME);
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");
125 #define NACTIVITY 4
127 static bool activity_stop = false;
128 static pid_t wrkpid;
130 static void *
131 fs_activity(void *arg)
133 int di, fi;
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",
145 prefix, di, fi);
146 makefile(path);
149 for (di = 0; di < 5; di++) {
150 for (fi = 0; fi < 5; fi++) {
151 snprintf(path, sizeof(path), "%s/d%d/f%d",
152 prefix, di, fi);
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();
163 return NULL;
166 ATF_TC_BODY(snapshotstress, tc)
168 pthread_t at[NACTIVITY];
169 struct fss_set fss;
170 char prefix[NACTIVITY][128];
171 int i, fssfd;
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));
177 RZ(rump_init());
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));
183 begin();
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]));
196 sleep(1);
199 fssfd = rump_sys_open("/dev/rfss0", O_RDWR);
200 if (fssfd == -1)
201 atf_tc_fail_errno("cannot open fss");
202 makefile(BAKNAME);
203 memset(&fss, 0, sizeof(fss));
204 fss.fss_mount = __UNCONST("/mnt");
205 fss.fss_bstore = __UNCONST(BAKNAME);
206 fss.fss_csize = 0;
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)
220 unlink(IMGNAME);
223 ATF_TP_ADD_TCS(tp)
225 ATF_TP_ADD_TC(tp, snapshot);
226 ATF_TP_ADD_TC(tp, snapshotstress);
227 return 0;