t_rfw.c revision 1.4 1 1.4 perseant /* $NetBSD: t_rfw.c,v 1.4 2025/08/27 00:28:20 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/wait.h>
6 1.1 perseant
7 1.1 perseant #include <atf-c.h>
8 1.2 perseant #include <ctype.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.4 perseant /* #define USE_DUMPLFS */ /* Dump the filesystem at certain steps */
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.2 perseant /* Check the file system for consistency */
37 1.2 perseant int fsck(void);
38 1.2 perseant
39 1.4 perseant /* Run dumplfs; for debugging */
40 1.4 perseant void dumplfs(void);
41 1.4 perseant
42 1.4 perseant /* Actually run the test */
43 1.2 perseant void test(int);
44 1.2 perseant
45 1.4 perseant ATF_TC(rfw32);
46 1.4 perseant ATF_TC_HEAD(rfw32, tc)
47 1.4 perseant {
48 1.4 perseant atf_tc_set_md_var(tc, "descr",
49 1.4 perseant "LFS32 roll-forward creates an inconsistent filesystem");
50 1.4 perseant atf_tc_set_md_var(tc, "timeout", "20");
51 1.4 perseant }
52 1.4 perseant
53 1.4 perseant ATF_TC(rfw64);
54 1.4 perseant ATF_TC_HEAD(rfw64, tc)
55 1.1 perseant {
56 1.1 perseant atf_tc_set_md_var(tc, "descr",
57 1.4 perseant "LFS64 roll-forward creates an inconsistent filesystem");
58 1.1 perseant atf_tc_set_md_var(tc, "timeout", "20");
59 1.1 perseant }
60 1.1 perseant
61 1.2 perseant #define MAXLINE 132
62 1.2 perseant #define CHUNKSIZE 300
63 1.2 perseant
64 1.1 perseant #define IMGNAME "disk.img"
65 1.1 perseant #define FAKEBLK "/dev/blk"
66 1.1 perseant #define LOGFILE "newfs.log"
67 1.1 perseant #define SBLOCK0_COPY "sb0.dd"
68 1.1 perseant #define SBLOCK1_COPY "sb1.dd"
69 1.2 perseant
70 1.2 perseant #define MP "/mp"
71 1.2 perseant #define UNCHANGED_CONTROL MP "/3-unchanged-control"
72 1.2 perseant #define TO_BE_DELETED MP "/4-to-be-deleted"
73 1.2 perseant #define TO_BE_APPENDED MP "/5-to-be-appended"
74 1.2 perseant #define NEWLY_CREATED MP "/6-newly-created"
75 1.2 perseant
76 1.2 perseant long long sbaddr[2] = { -1, -1 };
77 1.2 perseant const char *sblock[2] = { SBLOCK0_COPY, SBLOCK1_COPY };
78 1.2 perseant
79 1.4 perseant ATF_TC_BODY(rfw32, tc)
80 1.4 perseant {
81 1.4 perseant test(32);
82 1.4 perseant }
83 1.4 perseant
84 1.4 perseant ATF_TC_BODY(rfw64, tc)
85 1.4 perseant {
86 1.4 perseant test(64);
87 1.4 perseant }
88 1.4 perseant
89 1.4 perseant void test(int width)
90 1.1 perseant {
91 1.1 perseant struct ufs_args args;
92 1.1 perseant FILE *pipe;
93 1.1 perseant char buf[MAXLINE];
94 1.4 perseant char cmd[MAXLINE];
95 1.2 perseant int i;
96 1.2 perseant
97 1.2 perseant setvbuf(stdout, NULL, _IONBF, 0);
98 1.1 perseant
99 1.1 perseant /*
100 1.1 perseant * Initialize.
101 1.1 perseant */
102 1.4 perseant if (width > 32) {
103 1.4 perseant atf_tc_skip("fsck_lfs does not yet understand LFS64");
104 1.4 perseant return;
105 1.4 perseant }
106 1.2 perseant atf_tc_expect_fail("roll-forward not yet implemented");
107 1.1 perseant
108 1.2 perseant /* Create filesystem, note superblock locations */
109 1.1 perseant fprintf(stderr, "* Create file system\n");
110 1.4 perseant sprintf(cmd, "newfs_lfs -D -F -s 10000 -w%d ./%s > %s",
111 1.4 perseant width, IMGNAME, LOGFILE);
112 1.4 perseant if (system(cmd) == -1)
113 1.1 perseant atf_tc_fail_errno("newfs failed");
114 1.1 perseant pipe = fopen(LOGFILE, "r");
115 1.1 perseant if (pipe == NULL)
116 1.1 perseant atf_tc_fail_errno("newfs failed to execute");
117 1.1 perseant while (fgets(buf, MAXLINE, pipe) != NULL) {
118 1.2 perseant if (sscanf(buf, "%lld,%lld", sbaddr, sbaddr + 1) == 2)
119 1.1 perseant break;
120 1.1 perseant }
121 1.1 perseant while (fgets(buf, MAXLINE, pipe) != NULL)
122 1.1 perseant ;
123 1.1 perseant fclose(pipe);
124 1.2 perseant if (sbaddr[0] < 0 || sbaddr[1] < 0)
125 1.1 perseant atf_tc_fail("superblock not found");
126 1.1 perseant fprintf(stderr, "* Superblocks at %lld and %lld\n",
127 1.2 perseant sbaddr[0], sbaddr[1]);
128 1.1 perseant
129 1.1 perseant /* Set up rump */
130 1.1 perseant rump_init();
131 1.2 perseant if (rump_sys_mkdir(MP, 0777) == -1)
132 1.1 perseant atf_tc_fail_errno("cannot create mountpoint");
133 1.1 perseant rump_pub_etfs_register(FAKEBLK, IMGNAME, RUMP_ETFS_BLK);
134 1.1 perseant
135 1.1 perseant /*
136 1.1 perseant * Create initial filesystem state, from which
137 1.1 perseant * we will attempt to roll forward.
138 1.1 perseant */
139 1.1 perseant
140 1.1 perseant /* Mount filesystem */
141 1.2 perseant fprintf(stderr, "* Mount fs [1, initial]\n");
142 1.1 perseant memset(&args, 0, sizeof(args));
143 1.1 perseant args.fspec = __UNCONST(FAKEBLK);
144 1.2 perseant if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1)
145 1.1 perseant atf_tc_fail_errno("rump_sys_mount failed");
146 1.1 perseant
147 1.1 perseant /* Payload */
148 1.1 perseant fprintf(stderr, "* Initial payload\n");
149 1.2 perseant write_file(UNCHANGED_CONTROL, CHUNKSIZE);
150 1.2 perseant write_file(TO_BE_DELETED, CHUNKSIZE);
151 1.2 perseant write_file(TO_BE_APPENDED, CHUNKSIZE);
152 1.2 perseant rump_sys_sync();
153 1.2 perseant rump_sys_sync();
154 1.2 perseant sleep(1); /* XXX yuck - but we need the superblocks dirty */
155 1.2 perseant
156 1.2 perseant /* Make copies of superblocks */
157 1.2 perseant for (i = 0; i < 2; i++) {
158 1.2 perseant sprintf(buf, "dd if=%s of=%s bs=512 iseek=%lld"
159 1.2 perseant " count=16 conv=sync", IMGNAME, sblock[i], sbaddr[i]);
160 1.2 perseant system(buf);
161 1.2 perseant }
162 1.1 perseant
163 1.1 perseant /* Unmount */
164 1.2 perseant rump_sys_unmount(MP, 0);
165 1.4 perseant if (fsck())
166 1.4 perseant atf_tc_fail_errno("fsck found errors after first unmount");
167 1.1 perseant
168 1.1 perseant /*
169 1.1 perseant * Make changes which we will attempt to roll forward.
170 1.1 perseant */
171 1.1 perseant
172 1.1 perseant /* Reconfigure and mount filesystem again */
173 1.2 perseant fprintf(stderr, "* Mount fs [2, after changes]\n");
174 1.2 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 [2]");
176 1.1 perseant
177 1.4 perseant fprintf(stderr, "* Update payload\n");
178 1.4 perseant
179 1.1 perseant /* Add new file */
180 1.2 perseant write_file(NEWLY_CREATED, CHUNKSIZE);
181 1.1 perseant
182 1.1 perseant /* Append to existing file */
183 1.2 perseant write_file(TO_BE_APPENDED, CHUNKSIZE);
184 1.1 perseant
185 1.1 perseant /* Delete file */
186 1.2 perseant rump_sys_unlink(TO_BE_DELETED);
187 1.1 perseant
188 1.1 perseant /* Done with payload, unmount fs */
189 1.2 perseant rump_sys_unmount(MP, 0);
190 1.4 perseant if (fsck())
191 1.4 perseant atf_tc_fail_errno("fsck found errors after second unmount");
192 1.1 perseant
193 1.1 perseant #ifndef FORCE_SUCCESS
194 1.4 perseant fprintf(stderr, "* Revert superblocks\n");
195 1.1 perseant /*
196 1.1 perseant * Copy back old superblocks, reverting FS to old state
197 1.1 perseant */
198 1.2 perseant for (i = 0; i < 2; i++) {
199 1.2 perseant sprintf(buf, "dd of=%s if=%s bs=512 oseek=%lld count=16"
200 1.2 perseant " conv=sync,notrunc", IMGNAME, sblock[i], sbaddr[i]);
201 1.2 perseant system(buf);
202 1.2 perseant }
203 1.1 perseant
204 1.2 perseant if (fsck())
205 1.1 perseant atf_tc_fail_errno("fsck found errors with old superblocks");
206 1.1 perseant #endif
207 1.4 perseant dumplfs();
208 1.1 perseant
209 1.1 perseant /*
210 1.1 perseant * Roll forward.
211 1.1 perseant */
212 1.1 perseant
213 1.1 perseant /* Mount filesystem; this will roll forward. */
214 1.2 perseant fprintf(stderr, "* Mount fs [3, to roll forward]\n");
215 1.2 perseant if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1)
216 1.1 perseant atf_tc_fail_errno("rump_sys_mount failed [3]");
217 1.1 perseant
218 1.1 perseant /* Unmount filesystem */
219 1.2 perseant if (rump_sys_unmount(MP, 0) != 0)
220 1.2 perseant atf_tc_fail_errno("rump_sys_umount failed after roll-forward");
221 1.1 perseant
222 1.1 perseant /*
223 1.2 perseant * Use fsck_lfs to look for consistency errors.
224 1.1 perseant */
225 1.1 perseant
226 1.4 perseant fprintf(stderr, "* Fsck after roll-forward\n");
227 1.2 perseant if (fsck()) {
228 1.2 perseant fprintf(stderr, "*** FAILED FSCK ***\n");
229 1.2 perseant atf_tc_fail("fsck found errors after roll forward");
230 1.2 perseant }
231 1.4 perseant dumplfs();
232 1.1 perseant
233 1.1 perseant /*
234 1.1 perseant * Check file system contents
235 1.1 perseant */
236 1.1 perseant
237 1.1 perseant /* Mount filesystem one last time */
238 1.2 perseant fprintf(stderr, "* Mount fs [4, after roll forward complete]\n");
239 1.2 perseant if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1)
240 1.1 perseant atf_tc_fail_errno("rump_sys_mount failed [4]");
241 1.1 perseant
242 1.2 perseant if (check_file(UNCHANGED_CONTROL, CHUNKSIZE) != 0)
243 1.2 perseant atf_tc_fail("Unchanged control file differs(!)");
244 1.1 perseant
245 1.4 perseant if (rump_sys_access(TO_BE_DELETED, F_OK) == 0)
246 1.4 perseant atf_tc_fail("Removed file still present");
247 1.4 perseant else
248 1.4 perseant fprintf(stderr, "%s: no problem\n", TO_BE_DELETED);
249 1.4 perseant
250 1.2 perseant if (check_file(TO_BE_APPENDED, 2 * CHUNKSIZE) != 0)
251 1.1 perseant atf_tc_fail("Appended file differs");
252 1.1 perseant
253 1.2 perseant if (rump_sys_access(NEWLY_CREATED, F_OK) != 0)
254 1.2 perseant atf_tc_fail("Newly added file missing");
255 1.2 perseant
256 1.2 perseant if (check_file(NEWLY_CREATED, CHUNKSIZE) != 0)
257 1.2 perseant atf_tc_fail("Newly added file differs");
258 1.2 perseant
259 1.2 perseant /* Umount filesystem */
260 1.2 perseant rump_sys_unmount(MP, 0);
261 1.2 perseant
262 1.2 perseant /* Final fsck to double check */
263 1.4 perseant fprintf(stderr, "* Fsck after final unmount\n");
264 1.2 perseant if (fsck()) {
265 1.2 perseant fprintf(stderr, "*** FAILED FSCK ***\n");
266 1.2 perseant atf_tc_fail("fsck found errors after final unmount");
267 1.2 perseant }
268 1.1 perseant }
269 1.1 perseant
270 1.1 perseant ATF_TP_ADD_TCS(tp)
271 1.1 perseant {
272 1.1 perseant
273 1.4 perseant ATF_TP_ADD_TC(tp, rfw32);
274 1.4 perseant ATF_TP_ADD_TC(tp, rfw64);
275 1.1 perseant return atf_no_error();
276 1.1 perseant }
277 1.1 perseant
278 1.1 perseant /* Write some data into a file */
279 1.1 perseant int write_file(const char *filename, int add)
280 1.1 perseant {
281 1.1 perseant int fd, size, i;
282 1.1 perseant struct stat statbuf;
283 1.1 perseant unsigned char b;
284 1.1 perseant int flags = O_CREAT|O_WRONLY;
285 1.1 perseant
286 1.1 perseant if (rump_sys_stat(filename, &statbuf) < 0)
287 1.1 perseant size = 0;
288 1.1 perseant else {
289 1.1 perseant size = statbuf.st_size;
290 1.1 perseant flags |= O_APPEND;
291 1.1 perseant }
292 1.1 perseant
293 1.1 perseant fd = rump_sys_open(filename, flags);
294 1.1 perseant
295 1.1 perseant for (i = 0; i < add; i++) {
296 1.2 perseant b = ((unsigned)(size + i)) & 0xff;
297 1.1 perseant rump_sys_write(fd, &b, 1);
298 1.1 perseant }
299 1.1 perseant rump_sys_close(fd);
300 1.1 perseant
301 1.1 perseant return 0;
302 1.1 perseant }
303 1.1 perseant
304 1.1 perseant /* Check file's existence, size and contents */
305 1.1 perseant int check_file(const char *filename, int size)
306 1.1 perseant {
307 1.1 perseant int fd, i;
308 1.1 perseant struct stat statbuf;
309 1.1 perseant unsigned char b;
310 1.3 riastrad
311 1.1 perseant if (rump_sys_stat(filename, &statbuf) < 0) {
312 1.1 perseant fprintf(stderr, "%s: stat failed\n", filename);
313 1.1 perseant return 1;
314 1.1 perseant }
315 1.1 perseant if (size != statbuf.st_size) {
316 1.1 perseant fprintf(stderr, "%s: expected %d bytes, found %d\n",
317 1.1 perseant filename, size, (int)statbuf.st_size);
318 1.1 perseant return 2;
319 1.1 perseant }
320 1.1 perseant
321 1.1 perseant fd = rump_sys_open(filename, O_RDONLY);
322 1.1 perseant for (i = 0; i < size; i++) {
323 1.1 perseant rump_sys_read(fd, &b, 1);
324 1.2 perseant if (b != (((unsigned)i) & 0xff)) {
325 1.1 perseant fprintf(stderr, "%s: byte %d: expected %x found %x\n",
326 1.2 perseant filename, i, ((unsigned)(i)) & 0xff, b);
327 1.2 perseant rump_sys_close(fd);
328 1.1 perseant return 3;
329 1.1 perseant }
330 1.1 perseant }
331 1.1 perseant rump_sys_close(fd);
332 1.2 perseant fprintf(stderr, "%s: no problem\n", filename);
333 1.2 perseant return 0;
334 1.2 perseant }
335 1.1 perseant
336 1.2 perseant /* Run a file system consistency check */
337 1.2 perseant int fsck(void)
338 1.2 perseant {
339 1.2 perseant char s[MAXLINE];
340 1.2 perseant int i, errors = 0;
341 1.2 perseant FILE *pipe;
342 1.2 perseant char cmd[MAXLINE];
343 1.2 perseant
344 1.2 perseant for (i = 0; i < 2; i++) {
345 1.2 perseant sprintf(cmd, "fsck_lfs -n -b %jd -f " IMGNAME,
346 1.2 perseant (intmax_t)sbaddr[i]);
347 1.2 perseant pipe = popen(cmd, "r");
348 1.2 perseant while (fgets(s, MAXLINE, pipe) != NULL) {
349 1.2 perseant if (isdigit((int)s[0])) /* "5 files ... " */
350 1.2 perseant continue;
351 1.2 perseant if (isspace((int)s[0]) || s[0] == '*')
352 1.2 perseant continue;
353 1.2 perseant if (strncmp(s, "Alternate", 9) == 0)
354 1.2 perseant continue;
355 1.2 perseant if (strncmp(s, "ROLL ", 5) == 0)
356 1.2 perseant continue;
357 1.2 perseant fprintf(stderr, "FSCK[sb@%lld]: %s", sbaddr[i], s);
358 1.2 perseant ++errors;
359 1.2 perseant }
360 1.2 perseant pclose(pipe);
361 1.2 perseant if (errors) {
362 1.2 perseant break;
363 1.2 perseant }
364 1.2 perseant }
365 1.2 perseant
366 1.2 perseant return errors;
367 1.1 perseant }
368 1.4 perseant
369 1.4 perseant /* Run dumplfs */
370 1.4 perseant void dumplfs(void)
371 1.4 perseant {
372 1.4 perseant #ifdef USE_DUMPLFS
373 1.4 perseant char s[MAXLINE];
374 1.4 perseant FILE *pipe;
375 1.4 perseant
376 1.4 perseant pipe = popen("dumplfs -S -s 2 -s 1 -s 0 " IMGNAME, "r");
377 1.4 perseant while (fgets(s, MAXLINE, pipe) != NULL)
378 1.4 perseant fprintf(stderr, "DUMPLFS: %s", s);
379 1.4 perseant pclose(pipe);
380 1.4 perseant #endif /* USE_DUMPLFS */
381 1.4 perseant }
382