1 /* $NetBSD: t_rfw.c,v 1.6 2025/10/13 00:50:48 perseant Exp $ */ 2 3 #include <sys/types.h> 4 #include <sys/wait.h> 5 #include <sys/sysctl.h> 6 7 #include <atf-c.h> 8 #include <errno.h> 9 #include <limits.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <unistd.h> 13 #include <string.h> 14 15 #include <rump/rump.h> 16 #include <rump/rump_syscalls.h> 17 18 #include <ufs/ufs/ufsmount.h> 19 #include <ufs/lfs/lfs.h> 20 #include <ufs/lfs/lfs_extern.h> 21 22 #include "h_macros.h" 23 #include "util.h" 24 25 /* Debugging conditions */ 26 /* #define FORCE_SUCCESS */ /* Don't actually revert, everything worked */ 27 /* #define USE_DUMPLFS */ /* Dump the filesystem at certain steps */ 28 29 /* Temporary files to store superblocks */ 30 #define SBLOCK0_COPY "sb0.dd" 31 #define SBLOCK1_COPY "sb1.dd" 32 33 /* Size of the image file, in 512-blocks */ 34 #define FSSIZE 10000 35 36 /* Actually run the test */ 37 void test(int); 38 39 ATF_TC(rfw32); 40 ATF_TC_HEAD(rfw32, tc) 41 { 42 atf_tc_set_md_var(tc, "descr", 43 "LFS32 roll-forward creates an inconsistent filesystem"); 44 atf_tc_set_md_var(tc, "timeout", "20"); 45 } 46 47 ATF_TC(rfw64); 48 ATF_TC_HEAD(rfw64, tc) 49 { 50 atf_tc_set_md_var(tc, "descr", 51 "LFS64 roll-forward creates an inconsistent filesystem"); 52 atf_tc_set_md_var(tc, "timeout", "20"); 53 } 54 55 #define UNCHANGED_CONTROL MP "/3-unchanged-control" 56 #define TO_BE_DELETED MP "/4-to-be-deleted" 57 #define TO_BE_APPENDED MP "/5-to-be-appended" 58 #define NEWLY_CREATED MP "/6-newly-created" 59 60 const char *sblock[2] = { SBLOCK0_COPY, SBLOCK1_COPY }; 61 62 ATF_TC_BODY(rfw32, tc) 63 { 64 test(32); 65 } 66 67 ATF_TC_BODY(rfw64, tc) 68 { 69 test(64); 70 } 71 72 void test(int width) 73 { 74 struct ufs_args args; 75 char buf[MAXLINE]; 76 int i; 77 78 setvbuf(stdout, NULL, _IONBF, 0); 79 80 /* 81 * Initialize. 82 */ 83 if (width > 32) 84 atf_tc_expect_fail("fsck errors are common in the 64-bit case"); 85 else 86 atf_tc_expect_pass(); 87 88 /* Create filesystem, note superblock locations */ 89 create_lfs(FSSIZE, FSSIZE, width, 1); 90 91 /* 92 * Create initial filesystem state, from which 93 * we will attempt to roll forward. 94 */ 95 96 /* Mount filesystem */ 97 fprintf(stderr, "* Mount fs [1, initial]\n"); 98 memset(&args, 0, sizeof(args)); 99 args.fspec = __UNCONST(FAKEBLK); 100 if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1) 101 atf_tc_fail_errno("rump_sys_mount failed"); 102 103 /* Payload */ 104 fprintf(stderr, "* Initial payload\n"); 105 write_file(UNCHANGED_CONTROL, CHUNKSIZE, 1); 106 write_file(TO_BE_DELETED, CHUNKSIZE, 1); 107 write_file(TO_BE_APPENDED, CHUNKSIZE, 1); 108 rump_sys_sync(); 109 rump_sys_sync(); 110 sleep(1); /* XXX yuck - but we need the superblocks dirty */ 111 112 /* Make copies of superblocks */ 113 for (i = 0; i < 2; i++) { 114 sprintf(buf, "dd if=%s of=%s bs=512 iseek=%lld" 115 " count=16 conv=sync", IMGNAME, sblock[i], sbaddr[i]); 116 system(buf); 117 } 118 119 /* Unmount */ 120 rump_sys_unmount(MP, 0); 121 if (fsck()) 122 atf_tc_fail_errno("fsck found errors after first unmount"); 123 124 /* 125 * Make changes which we will attempt to roll forward. 126 */ 127 128 /* Reconfigure and mount filesystem again */ 129 fprintf(stderr, "* Mount fs [2, after changes]\n"); 130 if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1) 131 atf_tc_fail_errno("rump_sys_mount failed [2]"); 132 133 fprintf(stderr, "* Update payload\n"); 134 135 /* Add new file */ 136 write_file(NEWLY_CREATED, CHUNKSIZE, 1); 137 138 /* Append to existing file */ 139 write_file(TO_BE_APPENDED, CHUNKSIZE, 1); 140 141 /* Delete file */ 142 rump_sys_unlink(TO_BE_DELETED); 143 144 /* Done with payload, unmount fs */ 145 rump_sys_unmount(MP, 0); 146 if (fsck()) 147 atf_tc_fail_errno("fsck found errors after second unmount"); 148 149 #ifndef FORCE_SUCCESS 150 fprintf(stderr, "* Revert superblocks\n"); 151 /* 152 * Copy back old superblocks, reverting FS to old state 153 */ 154 for (i = 0; i < 2; i++) { 155 sprintf(buf, "dd of=%s if=%s bs=512 oseek=%lld count=16" 156 " conv=sync,notrunc", IMGNAME, sblock[i], sbaddr[i]); 157 system(buf); 158 } 159 160 if (fsck()) 161 atf_tc_fail_errno("fsck found errors with old superblocks"); 162 #endif /* FORCE_SUCCESS */ 163 #ifdef USE_DUMPLFS 164 dumplfs(); 165 #endif /* USE_DUMPLFS */ 166 167 /* 168 * Roll forward. 169 */ 170 171 /* Mount filesystem; this will roll forward. */ 172 fprintf(stderr, "* Mount fs [3, to roll forward]\n"); 173 if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1) 174 atf_tc_fail_errno("rump_sys_mount failed [3]"); 175 176 /* Unmount filesystem */ 177 if (rump_sys_unmount(MP, 0) != 0) 178 atf_tc_fail_errno("rump_sys_umount failed after roll-forward"); 179 180 /* 181 * Use fsck_lfs to look for consistency errors. 182 */ 183 184 fprintf(stderr, "* Fsck after roll-forward\n"); 185 if (fsck()) { 186 fprintf(stderr, "*** FAILED FSCK ***\n"); 187 atf_tc_fail("fsck found errors after roll forward"); 188 } 189 #ifdef USE_DUMPLFS 190 dumplfs(); 191 #endif /* USE_DUMPLFS */ 192 193 /* 194 * Check file system contents 195 */ 196 197 /* Mount filesystem one last time */ 198 fprintf(stderr, "* Mount fs [4, after roll forward complete]\n"); 199 if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1) 200 atf_tc_fail_errno("rump_sys_mount failed [4]"); 201 202 if (check_file(UNCHANGED_CONTROL, CHUNKSIZE) != 0) 203 atf_tc_fail("Unchanged control file differs(!)"); 204 205 if (rump_sys_access(TO_BE_DELETED, F_OK) == 0) 206 atf_tc_fail("Removed file still present"); 207 else 208 fprintf(stderr, "%s: no problem\n", TO_BE_DELETED); 209 210 if (check_file(TO_BE_APPENDED, 2 * CHUNKSIZE) != 0) 211 atf_tc_fail("Appended file differs"); 212 213 if (rump_sys_access(NEWLY_CREATED, F_OK) != 0) 214 atf_tc_fail("Newly added file missing"); 215 216 if (check_file(NEWLY_CREATED, CHUNKSIZE) != 0) 217 atf_tc_fail("Newly added file differs"); 218 219 /* Umount filesystem */ 220 rump_sys_unmount(MP, 0); 221 222 /* Final fsck to double check */ 223 fprintf(stderr, "* Fsck after final unmount\n"); 224 if (fsck()) { 225 fprintf(stderr, "*** FAILED FSCK ***\n"); 226 atf_tc_fail("fsck found errors after final unmount"); 227 } 228 } 229 230 ATF_TP_ADD_TCS(tp) 231 { 232 233 ATF_TP_ADD_TC(tp, rfw32); 234 ATF_TP_ADD_TC(tp, rfw64); 235 return atf_no_error(); 236 } 237