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