xref: /minix3/tests/fs/common/snapshot.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: snapshot.c,v 1.7 2013/02/06 09:05:01 hannken Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc #include <sys/types.h>
4*11be35a1SLionel Sambuc #include <sys/ioctl.h>
5*11be35a1SLionel Sambuc #include <sys/mount.h>
6*11be35a1SLionel Sambuc 
7*11be35a1SLionel Sambuc #include <dev/fssvar.h>
8*11be35a1SLionel Sambuc 
9*11be35a1SLionel Sambuc #include <atf-c.h>
10*11be35a1SLionel Sambuc #include <fcntl.h>
11*11be35a1SLionel Sambuc #include <pthread.h>
12*11be35a1SLionel Sambuc #include <stdio.h>
13*11be35a1SLionel Sambuc #include <stdlib.h>
14*11be35a1SLionel Sambuc #include <string.h>
15*11be35a1SLionel Sambuc #include <unistd.h>
16*11be35a1SLionel Sambuc 
17*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(snapshot);
ATF_TC_HEAD(snapshot,tc)18*11be35a1SLionel Sambuc ATF_TC_HEAD(snapshot, tc)
19*11be35a1SLionel Sambuc {
20*11be35a1SLionel Sambuc 
21*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "basic snapshot features");
22*11be35a1SLionel Sambuc }
23*11be35a1SLionel Sambuc 
24*11be35a1SLionel Sambuc static void
makefile(const char * path)25*11be35a1SLionel Sambuc makefile(const char *path)
26*11be35a1SLionel Sambuc {
27*11be35a1SLionel Sambuc 	int fd;
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc 	fd = rump_sys_open(path, O_CREAT | O_RDWR, 0777);
30*11be35a1SLionel Sambuc 	if (fd == -1)
31*11be35a1SLionel Sambuc 		atf_tc_fail_errno("create %s", path);
32*11be35a1SLionel Sambuc 	rump_sys_close(fd);
33*11be35a1SLionel Sambuc }
34*11be35a1SLionel Sambuc 
ATF_TC_BODY(snapshot,tc)35*11be35a1SLionel Sambuc ATF_TC_BODY(snapshot, tc)
36*11be35a1SLionel Sambuc {
37*11be35a1SLionel Sambuc 	char buf[1024];
38*11be35a1SLionel Sambuc 	struct fss_set fss;
39*11be35a1SLionel Sambuc 	int fssfd;
40*11be35a1SLionel Sambuc 	int fd, fd2, i;
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc 	if (system(NEWFS) == -1)
43*11be35a1SLionel Sambuc 		atf_tc_fail_errno("cannot create file system");
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc 	rump_init();
46*11be35a1SLionel Sambuc 	begin();
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc 	if (rump_sys_mkdir("/mnt", 0777) == -1)
49*11be35a1SLionel Sambuc 		atf_tc_fail_errno("mount point create");
50*11be35a1SLionel Sambuc 	if (rump_sys_mkdir("/snap", 0777) == -1)
51*11be35a1SLionel Sambuc 		atf_tc_fail_errno("mount point 2 create");
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc 	rump_pub_etfs_register("/diskdev", IMGNAME, RUMP_ETFS_BLK);
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc 	mount_diskfs("/diskdev", "/mnt");
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc #define TESTSTR1 "huihai\n"
58*11be35a1SLionel Sambuc #define TESTSZ1 (sizeof(TESTSTR1)-1)
59*11be35a1SLionel Sambuc #define TESTSTR2 "baana liten\n"
60*11be35a1SLionel Sambuc #define TESTSZ2 (sizeof(TESTSTR2)-1)
61*11be35a1SLionel Sambuc 
62*11be35a1SLionel Sambuc 	fd = rump_sys_open("/mnt/myfile", O_RDWR | O_CREAT, 0777);
63*11be35a1SLionel Sambuc 	if (fd == -1)
64*11be35a1SLionel Sambuc 		atf_tc_fail_errno("create file");
65*11be35a1SLionel Sambuc 	if (rump_sys_write(fd, TESTSTR1, TESTSZ1) != TESTSZ1)
66*11be35a1SLionel Sambuc 		atf_tc_fail_errno("write fail");
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc 	fssfd = rump_sys_open("/dev/rfss0", O_RDWR);
69*11be35a1SLionel Sambuc 	if (fssfd == -1)
70*11be35a1SLionel Sambuc 		atf_tc_fail_errno("cannot open fss");
71*11be35a1SLionel Sambuc 	makefile(BAKNAME);
72*11be35a1SLionel Sambuc 	memset(&fss, 0, sizeof(fss));
73*11be35a1SLionel Sambuc 	fss.fss_mount = __UNCONST("/mnt");
74*11be35a1SLionel Sambuc 	fss.fss_bstore = __UNCONST(BAKNAME);
75*11be35a1SLionel Sambuc 	fss.fss_csize = 0;
76*11be35a1SLionel Sambuc 	if (rump_sys_ioctl(fssfd, FSSIOCSET, &fss) == -1)
77*11be35a1SLionel Sambuc 		atf_tc_fail_errno("create snapshot");
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc 	for (i = 0; i < 10000; i++) {
80*11be35a1SLionel Sambuc 		if (rump_sys_write(fd, TESTSTR2, TESTSZ2) != TESTSZ2)
81*11be35a1SLionel Sambuc 			atf_tc_fail_errno("write fail");
82*11be35a1SLionel Sambuc 	}
83*11be35a1SLionel Sambuc 	rump_sys_sync();
84*11be35a1SLionel Sambuc 
85*11be35a1SLionel Sambuc 	/* technically we should fsck it first? */
86*11be35a1SLionel Sambuc 	mount_diskfs("/dev/fss0", "/snap");
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc 	/* check for old contents */
89*11be35a1SLionel Sambuc 	fd2 = rump_sys_open("/snap/myfile", O_RDONLY);
90*11be35a1SLionel Sambuc 	if (fd2 == -1)
91*11be35a1SLionel Sambuc 		atf_tc_fail_errno("fail");
92*11be35a1SLionel Sambuc 	memset(buf, 0, sizeof(buf));
93*11be35a1SLionel Sambuc 	if (rump_sys_read(fd2, buf, sizeof(buf)) == -1)
94*11be35a1SLionel Sambuc 		atf_tc_fail_errno("read snap");
95*11be35a1SLionel Sambuc 	ATF_CHECK(strcmp(buf, TESTSTR1) == 0);
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc 	/* check that new files are invisible in the snapshot */
98*11be35a1SLionel Sambuc 	makefile("/mnt/newfile");
99*11be35a1SLionel Sambuc 	if (rump_sys_open("/snap/newfile", O_RDONLY) != -1)
100*11be35a1SLionel Sambuc 		atf_tc_fail("newfile exists in snapshot");
101*11be35a1SLionel Sambuc 	if (errno != ENOENT)
102*11be35a1SLionel Sambuc 		atf_tc_fail_errno("newfile open should fail with ENOENT");
103*11be35a1SLionel Sambuc 
104*11be35a1SLionel Sambuc 	/* check that removed files are still visible in the snapshot */
105*11be35a1SLionel Sambuc 	rump_sys_unlink("/mnt/myfile");
106*11be35a1SLionel Sambuc 	if (rump_sys_open("/snap/myfile", O_RDONLY) == -1)
107*11be35a1SLionel Sambuc 		atf_tc_fail_errno("unlinked file no longer in snapshot");
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc 	/* done for now */
110*11be35a1SLionel Sambuc }
111*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(snapshot,tc)112*11be35a1SLionel Sambuc ATF_TC_CLEANUP(snapshot, tc)
113*11be35a1SLionel Sambuc {
114*11be35a1SLionel Sambuc 
115*11be35a1SLionel Sambuc 	unlink(IMGNAME);
116*11be35a1SLionel Sambuc }
117*11be35a1SLionel Sambuc 
118*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(snapshotstress);
ATF_TC_HEAD(snapshotstress,tc)119*11be35a1SLionel Sambuc ATF_TC_HEAD(snapshotstress, tc)
120*11be35a1SLionel Sambuc {
121*11be35a1SLionel Sambuc 
122*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "snapshot on active file system");
123*11be35a1SLionel Sambuc }
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc #define NACTIVITY 4
126*11be35a1SLionel Sambuc 
127*11be35a1SLionel Sambuc static bool activity_stop = false;
128*11be35a1SLionel Sambuc static pid_t wrkpid;
129*11be35a1SLionel Sambuc 
130*11be35a1SLionel Sambuc static void *
fs_activity(void * arg)131*11be35a1SLionel Sambuc fs_activity(void *arg)
132*11be35a1SLionel Sambuc {
133*11be35a1SLionel Sambuc 	int di, fi;
134*11be35a1SLionel Sambuc 	char *prefix = arg, path[128];
135*11be35a1SLionel Sambuc 
136*11be35a1SLionel Sambuc 	rump_pub_lwproc_newlwp(wrkpid);
137*11be35a1SLionel Sambuc 
138*11be35a1SLionel Sambuc 	RL(rump_sys_mkdir(prefix, 0777));
139*11be35a1SLionel Sambuc 	while (! activity_stop) {
140*11be35a1SLionel Sambuc 		for (di = 0; di < 5; di++) {
141*11be35a1SLionel Sambuc 			snprintf(path, sizeof(path), "%s/d%d", prefix, di);
142*11be35a1SLionel Sambuc 			RL(rump_sys_mkdir(path, 0777));
143*11be35a1SLionel Sambuc 			for (fi = 0; fi < 5; fi++) {
144*11be35a1SLionel Sambuc 				snprintf(path, sizeof(path), "%s/d%d/f%d",
145*11be35a1SLionel Sambuc 				    prefix, di, fi);
146*11be35a1SLionel Sambuc 				makefile(path);
147*11be35a1SLionel Sambuc 			}
148*11be35a1SLionel Sambuc 		}
149*11be35a1SLionel Sambuc 		for (di = 0; di < 5; di++) {
150*11be35a1SLionel Sambuc 			for (fi = 0; fi < 5; fi++) {
151*11be35a1SLionel Sambuc 				snprintf(path, sizeof(path), "%s/d%d/f%d",
152*11be35a1SLionel Sambuc 				    prefix, di, fi);
153*11be35a1SLionel Sambuc 				RL(rump_sys_unlink(path));
154*11be35a1SLionel Sambuc 			}
155*11be35a1SLionel Sambuc 			snprintf(path, sizeof(path), "%s/d%d", prefix, di);
156*11be35a1SLionel Sambuc 			RL(rump_sys_rmdir(path));
157*11be35a1SLionel Sambuc 		}
158*11be35a1SLionel Sambuc 	}
159*11be35a1SLionel Sambuc 	RL(rump_sys_rmdir(prefix));
160*11be35a1SLionel Sambuc 
161*11be35a1SLionel Sambuc 	rump_pub_lwproc_releaselwp();
162*11be35a1SLionel Sambuc 
163*11be35a1SLionel Sambuc 	return NULL;
164*11be35a1SLionel Sambuc }
165*11be35a1SLionel Sambuc 
ATF_TC_BODY(snapshotstress,tc)166*11be35a1SLionel Sambuc ATF_TC_BODY(snapshotstress, tc)
167*11be35a1SLionel Sambuc {
168*11be35a1SLionel Sambuc 	pthread_t at[NACTIVITY];
169*11be35a1SLionel Sambuc 	struct fss_set fss;
170*11be35a1SLionel Sambuc 	char prefix[NACTIVITY][128];
171*11be35a1SLionel Sambuc 	int i, fssfd;
172*11be35a1SLionel Sambuc 
173*11be35a1SLionel Sambuc 	if (system(NEWFS) == -1)
174*11be35a1SLionel Sambuc 		atf_tc_fail_errno("cannot create file system");
175*11be35a1SLionel Sambuc 	/* Force SMP so the stress makes sense. */
176*11be35a1SLionel Sambuc 	RL(setenv("RUMP_NCPU", "4", 1));
177*11be35a1SLionel Sambuc 	RZ(rump_init());
178*11be35a1SLionel Sambuc 	/* Prepare for fsck to use the RUMP /dev/fss0. */
179*11be35a1SLionel Sambuc 	RL(rump_init_server("unix://commsock"));
180*11be35a1SLionel Sambuc 	RL(setenv("LD_PRELOAD", "/usr/lib/librumphijack.so", 1));
181*11be35a1SLionel Sambuc 	RL(setenv("RUMP_SERVER", "unix://commsock", 1));
182*11be35a1SLionel Sambuc 	RL(setenv("RUMPHIJACK", "blanket=/dev/rfss0", 1));
183*11be35a1SLionel Sambuc 	begin();
184*11be35a1SLionel Sambuc 
185*11be35a1SLionel Sambuc 	RL(rump_sys_mkdir("/mnt", 0777));
186*11be35a1SLionel Sambuc 
187*11be35a1SLionel Sambuc 	rump_pub_etfs_register("/diskdev", IMGNAME, RUMP_ETFS_BLK);
188*11be35a1SLionel Sambuc 
189*11be35a1SLionel Sambuc 	mount_diskfs("/diskdev", "/mnt");
190*11be35a1SLionel Sambuc 
191*11be35a1SLionel Sambuc 	/* Start file system activity. */
192*11be35a1SLionel Sambuc 	RL(wrkpid = rump_sys_getpid());
193*11be35a1SLionel Sambuc 	for (i = 0; i < NACTIVITY; i++) {
194*11be35a1SLionel Sambuc 		snprintf(prefix[i], sizeof(prefix[i]),  "/mnt/a%d", i);
195*11be35a1SLionel Sambuc 		RL(pthread_create(&at[i], NULL, fs_activity, prefix[i]));
196*11be35a1SLionel Sambuc 		sleep(1);
197*11be35a1SLionel Sambuc 	}
198*11be35a1SLionel Sambuc 
199*11be35a1SLionel Sambuc 	fssfd = rump_sys_open("/dev/rfss0", O_RDWR);
200*11be35a1SLionel Sambuc 	if (fssfd == -1)
201*11be35a1SLionel Sambuc 		atf_tc_fail_errno("cannot open fss");
202*11be35a1SLionel Sambuc 	makefile(BAKNAME);
203*11be35a1SLionel Sambuc 	memset(&fss, 0, sizeof(fss));
204*11be35a1SLionel Sambuc 	fss.fss_mount = __UNCONST("/mnt");
205*11be35a1SLionel Sambuc 	fss.fss_bstore = __UNCONST(BAKNAME);
206*11be35a1SLionel Sambuc 	fss.fss_csize = 0;
207*11be35a1SLionel Sambuc 	if (rump_sys_ioctl(fssfd, FSSIOCSET, &fss) == -1)
208*11be35a1SLionel Sambuc 		atf_tc_fail_errno("create snapshot");
209*11be35a1SLionel Sambuc 
210*11be35a1SLionel Sambuc 	activity_stop = true;
211*11be35a1SLionel Sambuc 	for (i = 0; i < NACTIVITY; i++)
212*11be35a1SLionel Sambuc 		RL(pthread_join(at[i], NULL));
213*11be35a1SLionel Sambuc 
214*11be35a1SLionel Sambuc 	RL(system(FSCK " /dev/rfss0"));
215*11be35a1SLionel Sambuc }
216*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(snapshotstress,tc)217*11be35a1SLionel Sambuc ATF_TC_CLEANUP(snapshotstress, tc)
218*11be35a1SLionel Sambuc {
219*11be35a1SLionel Sambuc 
220*11be35a1SLionel Sambuc 	unlink(IMGNAME);
221*11be35a1SLionel Sambuc }
222*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)223*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
224*11be35a1SLionel Sambuc {
225*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, snapshot);
226*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, snapshotstress);
227*11be35a1SLionel Sambuc 	return 0;
228*11be35a1SLionel Sambuc }
229