]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/netbsd-tests/fs/common/snapshot.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / netbsd-tests / fs / common / snapshot.c
1 /*      $NetBSD: snapshot.c,v 1.7 2013/02/06 09:05:01 hannken 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 <pthread.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16
17 ATF_TC_WITH_CLEANUP(snapshot);
18 ATF_TC_HEAD(snapshot, tc)
19 {
20
21         atf_tc_set_md_var(tc, "descr", "basic snapshot features");
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 (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");
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_TC_WITH_CLEANUP(snapshotstress);
119 ATF_TC_HEAD(snapshotstress, tc)
120 {
121
122         atf_tc_set_md_var(tc, "descr", "snapshot on active file system");
123 }
124
125 #define NACTIVITY 4
126
127 static bool activity_stop = false;
128 static pid_t wrkpid;
129
130 static void *
131 fs_activity(void *arg)
132 {
133         int di, fi;
134         char *prefix = arg, path[128];
135
136         rump_pub_lwproc_newlwp(wrkpid);
137
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);
147                         }
148                 }
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));
154                         }
155                         snprintf(path, sizeof(path), "%s/d%d", prefix, di);
156                         RL(rump_sys_rmdir(path));
157                 }
158         }
159         RL(rump_sys_rmdir(prefix));
160
161         rump_pub_lwproc_releaselwp();
162
163         return NULL;
164 }
165
166 ATF_TC_BODY(snapshotstress, tc)
167 {
168         pthread_t at[NACTIVITY];
169         struct fss_set fss;
170         char prefix[NACTIVITY][128];
171         int i, fssfd;
172
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();
184
185         RL(rump_sys_mkdir("/mnt", 0777));
186
187         rump_pub_etfs_register("/diskdev", IMGNAME, RUMP_ETFS_BLK);
188
189         mount_diskfs("/diskdev", "/mnt");
190
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);
197         }
198
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");
209
210         activity_stop = true;
211         for (i = 0; i < NACTIVITY; i++)
212                 RL(pthread_join(at[i], NULL));
213
214         RL(system(FSCK " /dev/rfss0"));
215 }
216
217 ATF_TC_CLEANUP(snapshotstress, tc)
218 {
219
220         unlink(IMGNAME);
221 }
222
223 ATF_TP_ADD_TCS(tp)
224 {
225         ATF_TP_ADD_TC(tp, snapshot);
226         ATF_TP_ADD_TC(tp, snapshotstress);
227         return 0;
228 }