t_rfw.c revision 1.1 1 1.1 perseant /* $NetBSD: t_rfw.c,v 1.1 2020/08/18 03:02:50 perseant Exp $ */
2 1.1 perseant
3 1.1 perseant #include <sys/types.h>
4 1.1 perseant #include <sys/mount.h>
5 1.1 perseant #include <sys/sysctl.h>
6 1.1 perseant #include <sys/wait.h>
7 1.1 perseant
8 1.1 perseant #include <atf-c.h>
9 1.1 perseant #include <errno.h>
10 1.1 perseant #include <fcntl.h>
11 1.1 perseant #include <limits.h>
12 1.1 perseant #include <stdio.h>
13 1.1 perseant #include <stdlib.h>
14 1.1 perseant #include <unistd.h>
15 1.1 perseant #include <string.h>
16 1.1 perseant
17 1.1 perseant #include <rump/rump.h>
18 1.1 perseant #include <rump/rump_syscalls.h>
19 1.1 perseant
20 1.1 perseant #include <ufs/ufs/ufsmount.h>
21 1.1 perseant #include <ufs/lfs/lfs.h>
22 1.1 perseant #include <ufs/lfs/lfs_extern.h>
23 1.1 perseant
24 1.1 perseant #include "h_macros.h"
25 1.1 perseant
26 1.1 perseant /* Debugging conditions */
27 1.1 perseant /* #define FORCE_SUCCESS */ /* Don't actually revert, everything worked */
28 1.1 perseant /* #define FORCE_SYSCTL */ /* Don't check - necessary for FORCE_SUCCESS */
29 1.1 perseant
30 1.1 perseant /* Write a well-known byte pattern into a file, appending if it exists */
31 1.1 perseant int write_file(const char *, int);
32 1.1 perseant
33 1.1 perseant /* Check the byte pattern and size of the file */
34 1.1 perseant int check_file(const char *, int);
35 1.1 perseant
36 1.1 perseant ATF_TC(rfw);
37 1.1 perseant ATF_TC_HEAD(rfw, tc)
38 1.1 perseant {
39 1.1 perseant atf_tc_set_md_var(tc, "descr",
40 1.1 perseant "LFS roll-forward creates an inconsistent filesystem");
41 1.1 perseant atf_tc_set_md_var(tc, "timeout", "20");
42 1.1 perseant }
43 1.1 perseant
44 1.1 perseant #define IMGNAME "disk.img"
45 1.1 perseant #define FAKEBLK "/dev/blk"
46 1.1 perseant #define LOGFILE "newfs.log"
47 1.1 perseant #define SBLOCK0_COPY "sb0.dd"
48 1.1 perseant #define SBLOCK1_COPY "sb1.dd"
49 1.1 perseant #define MAXLINE 132
50 1.1 perseant #define CHUNKSIZE 300
51 1.1 perseant ATF_TC_BODY(rfw, tc)
52 1.1 perseant {
53 1.1 perseant struct ufs_args args;
54 1.1 perseant FILE *pipe;
55 1.1 perseant #if (!defined FORCE_SUCCESS && !defined FORCE_SYSCTL)
56 1.1 perseant int o_sysctl_rfw;
57 1.1 perseant int n_sysctl_rfw;
58 1.1 perseant int mib[3];
59 1.1 perseant size_t len;
60 1.1 perseant #endif
61 1.1 perseant char buf[MAXLINE];
62 1.1 perseant long long sb0addr = -1, sb1addr = -1;
63 1.1 perseant
64 1.1 perseant /*
65 1.1 perseant * Initialize.
66 1.1 perseant */
67 1.1 perseant #if (!defined FORCE_SUCCESS && !defined FORCE_SYSCTL)
68 1.1 perseant /* Set sysctl to allow roll-forward */
69 1.1 perseant fprintf(stderr, "* Set sysctl\n");
70 1.1 perseant mib[0] = CTL_VFS;
71 1.1 perseant mib[1] = 5; /* LFS */
72 1.1 perseant mib[2] = LFS_DO_RFW;
73 1.1 perseant len = sizeof(o_sysctl_rfw);
74 1.1 perseant if (sysctl(mib, 3, &o_sysctl_rfw, &len,
75 1.1 perseant &n_sysctl_rfw, sizeof(n_sysctl_rfw)) < 0)
76 1.1 perseant atf_tc_skip("roll-forward not enabled in kernel");
77 1.1 perseant #endif /* !FORCE_SUCCESS && !FORCE_SYSCTL */
78 1.1 perseant
79 1.1 perseant /* Create filesystem */
80 1.1 perseant fprintf(stderr, "* Create file system\n");
81 1.1 perseant if (system("newfs_lfs -D -F -s 10000 ./" IMGNAME " > " LOGFILE) == -1)
82 1.1 perseant atf_tc_fail_errno("newfs failed");
83 1.1 perseant pipe = fopen(LOGFILE, "r");
84 1.1 perseant if (pipe == NULL)
85 1.1 perseant atf_tc_fail_errno("newfs failed to execute");
86 1.1 perseant while (fgets(buf, MAXLINE, pipe) != NULL) {
87 1.1 perseant if (sscanf(buf, "%lld,%lld", &sb0addr, &sb1addr) == 2)
88 1.1 perseant break;
89 1.1 perseant }
90 1.1 perseant while (fgets(buf, MAXLINE, pipe) != NULL)
91 1.1 perseant ;
92 1.1 perseant fclose(pipe);
93 1.1 perseant if (sb0addr < 0 || sb1addr < 0)
94 1.1 perseant atf_tc_fail("superblock not found");
95 1.1 perseant fprintf(stderr, "* Superblocks at %lld and %lld\n",
96 1.1 perseant sb0addr, sb1addr);
97 1.1 perseant
98 1.1 perseant /* Set up rump */
99 1.1 perseant rump_init();
100 1.1 perseant if (rump_sys_mkdir("/mp", 0777) == -1)
101 1.1 perseant atf_tc_fail_errno("cannot create mountpoint");
102 1.1 perseant rump_pub_etfs_register(FAKEBLK, IMGNAME, RUMP_ETFS_BLK);
103 1.1 perseant
104 1.1 perseant /*
105 1.1 perseant * Create initial filesystem state, from which
106 1.1 perseant * we will attempt to roll forward.
107 1.1 perseant */
108 1.1 perseant
109 1.1 perseant /* Mount filesystem */
110 1.1 perseant fprintf(stderr, "* Mount fs [1]\n");
111 1.1 perseant memset(&args, 0, sizeof(args));
112 1.1 perseant args.fspec = __UNCONST(FAKEBLK);
113 1.1 perseant if (rump_sys_mount(MOUNT_LFS, "/mp", 0, &args, sizeof(args)) == -1)
114 1.1 perseant atf_tc_fail_errno("rump_sys_mount failed");
115 1.1 perseant
116 1.1 perseant /* Payload */
117 1.1 perseant fprintf(stderr, "* Initial payload\n");
118 1.1 perseant write_file("/mp/to_be_deleted", CHUNKSIZE);
119 1.1 perseant write_file("/mp/to_be_appended", CHUNKSIZE);
120 1.1 perseant
121 1.1 perseant /* Unmount */
122 1.1 perseant rump_sys_unmount("/mp", 0);
123 1.1 perseant
124 1.1 perseant /* Make copies of superblocks */
125 1.1 perseant sprintf(buf, "dd if=%s of=%s bs=512 iseek=%lld count=16 conv=sync",
126 1.1 perseant IMGNAME, SBLOCK0_COPY, sb0addr);
127 1.1 perseant system(buf);
128 1.1 perseant sprintf(buf, "dd if=%s of=%s bs=512 iseek=%lld count=16 conv=sync",
129 1.1 perseant IMGNAME, SBLOCK1_COPY, sb1addr);
130 1.1 perseant system(buf);
131 1.1 perseant
132 1.1 perseant /*
133 1.1 perseant * Make changes which we will attempt to roll forward.
134 1.1 perseant */
135 1.1 perseant
136 1.1 perseant /* Reconfigure and mount filesystem again */
137 1.1 perseant fprintf(stderr, "* Mount fs [2]\n");
138 1.1 perseant if (rump_sys_mount(MOUNT_LFS, "/mp", 0, &args, sizeof(args)) == -1)
139 1.1 perseant atf_tc_fail_errno("rump_sys_mount failed [2]");
140 1.1 perseant
141 1.1 perseant /* Add new file */
142 1.1 perseant write_file("/mp/newly_created", CHUNKSIZE);
143 1.1 perseant
144 1.1 perseant /* Append to existing file */
145 1.1 perseant write_file("/mp/to_be_appended", CHUNKSIZE);
146 1.1 perseant
147 1.1 perseant /* Delete file */
148 1.1 perseant rump_sys_unlink("/mp/to_be_deleted");
149 1.1 perseant
150 1.1 perseant /* Done with payload, unmount fs */
151 1.1 perseant rump_sys_unmount("/mp", 0);
152 1.1 perseant
153 1.1 perseant #ifndef FORCE_SUCCESS
154 1.1 perseant /*
155 1.1 perseant * Copy back old superblocks, reverting FS to old state
156 1.1 perseant */
157 1.1 perseant sprintf(buf, "dd of=%s if=%s bs=512 oseek=%lld count=16 conv=sync,notrunc",
158 1.1 perseant IMGNAME, SBLOCK0_COPY, sb0addr);
159 1.1 perseant system(buf);
160 1.1 perseant sprintf(buf, "dd of=%s if=%s bs=512 oseek=%lld count=16 conv=sync,notrunc",
161 1.1 perseant IMGNAME, SBLOCK1_COPY, sb1addr);
162 1.1 perseant system(buf);
163 1.1 perseant
164 1.1 perseant if (system("fsck_lfs -n -f " IMGNAME))
165 1.1 perseant atf_tc_fail_errno("fsck found errors with old superblocks");
166 1.1 perseant #endif
167 1.1 perseant
168 1.1 perseant /*
169 1.1 perseant * Roll forward.
170 1.1 perseant */
171 1.1 perseant
172 1.1 perseant /* Mount filesystem; this will roll forward. */
173 1.1 perseant fprintf(stderr, "* Mount fs [3]\n");
174 1.1 perseant if (rump_sys_mount(MOUNT_LFS, "/mp", 0, &args, sizeof(args)) == -1)
175 1.1 perseant atf_tc_fail_errno("rump_sys_mount failed [3]");
176 1.1 perseant
177 1.1 perseant /* Unmount filesystem */
178 1.1 perseant rump_sys_unmount("/mp", 0);
179 1.1 perseant
180 1.1 perseant /*
181 1.1 perseant * Use fsck_lfs to look for consistency errors
182 1.1 perseant */
183 1.1 perseant
184 1.1 perseant if (system("fsck_lfs -n -f " IMGNAME))
185 1.1 perseant atf_tc_fail_errno("fsck found errors");
186 1.1 perseant
187 1.1 perseant /*
188 1.1 perseant * Check file system contents
189 1.1 perseant */
190 1.1 perseant
191 1.1 perseant /* Mount filesystem one last time */
192 1.1 perseant fprintf(stderr, "* Mount fs [4]\n");
193 1.1 perseant if (rump_sys_mount(MOUNT_LFS, "/mp", 0, &args, sizeof(args)) == -1)
194 1.1 perseant atf_tc_fail_errno("rump_sys_mount failed [4]");
195 1.1 perseant
196 1.1 perseant if (check_file("/mp/newly_created", CHUNKSIZE) != 0)
197 1.1 perseant atf_tc_fail("Newly added file differs");
198 1.1 perseant
199 1.1 perseant if (check_file("/mp/to_be_appended", 2 * CHUNKSIZE) != 0)
200 1.1 perseant atf_tc_fail("Appended file differs");
201 1.1 perseant
202 1.1 perseant if (rump_sys_access("/mp/to_be_deleted", F_OK) == 0)
203 1.1 perseant atf_tc_fail("Removed file still present");
204 1.1 perseant
205 1.1 perseant /* Umount filesystem, test completed */
206 1.1 perseant rump_sys_unmount("/mp", 0);
207 1.1 perseant }
208 1.1 perseant
209 1.1 perseant ATF_TP_ADD_TCS(tp)
210 1.1 perseant {
211 1.1 perseant
212 1.1 perseant ATF_TP_ADD_TC(tp, rfw);
213 1.1 perseant return atf_no_error();
214 1.1 perseant }
215 1.1 perseant
216 1.1 perseant /* Write some data into a file */
217 1.1 perseant int write_file(const char *filename, int add)
218 1.1 perseant {
219 1.1 perseant int fd, size, i;
220 1.1 perseant struct stat statbuf;
221 1.1 perseant unsigned char b;
222 1.1 perseant int flags = O_CREAT|O_WRONLY;
223 1.1 perseant
224 1.1 perseant if (rump_sys_stat(filename, &statbuf) < 0)
225 1.1 perseant size = 0;
226 1.1 perseant else {
227 1.1 perseant size = statbuf.st_size;
228 1.1 perseant flags |= O_APPEND;
229 1.1 perseant }
230 1.1 perseant
231 1.1 perseant fd = rump_sys_open(filename, flags);
232 1.1 perseant
233 1.1 perseant for (i = 0; i < add; i++) {
234 1.1 perseant b = (size + i) & 0xff;
235 1.1 perseant rump_sys_write(fd, &b, 1);
236 1.1 perseant }
237 1.1 perseant rump_sys_close(fd);
238 1.1 perseant
239 1.1 perseant return 0;
240 1.1 perseant }
241 1.1 perseant
242 1.1 perseant /* Check file's existence, size and contents */
243 1.1 perseant int check_file(const char *filename, int size)
244 1.1 perseant {
245 1.1 perseant int fd, i;
246 1.1 perseant struct stat statbuf;
247 1.1 perseant unsigned char b;
248 1.1 perseant
249 1.1 perseant if (rump_sys_stat(filename, &statbuf) < 0) {
250 1.1 perseant fprintf(stderr, "%s: stat failed\n", filename);
251 1.1 perseant return 1;
252 1.1 perseant }
253 1.1 perseant if (size != statbuf.st_size) {
254 1.1 perseant fprintf(stderr, "%s: expected %d bytes, found %d\n",
255 1.1 perseant filename, size, (int)statbuf.st_size);
256 1.1 perseant return 2;
257 1.1 perseant }
258 1.1 perseant
259 1.1 perseant fd = rump_sys_open(filename, O_RDONLY);
260 1.1 perseant
261 1.1 perseant for (i = 0; i < size; i++) {
262 1.1 perseant rump_sys_read(fd, &b, 1);
263 1.1 perseant if (b != (i & 0xff)) {
264 1.1 perseant fprintf(stderr, "%s: byte %d: expected %x found %x\n",
265 1.1 perseant filename, i, (unsigned)(i & 0xff), b);
266 1.1 perseant return 3;
267 1.1 perseant }
268 1.1 perseant }
269 1.1 perseant rump_sys_close(fd);
270 1.1 perseant
271 1.1 perseant return 0;
272 1.1 perseant }
273