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