1 /* $NetBSD: t_rfw.c,v 1.7 2025/10/18 22:18:19 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 atf_tc_expect_pass(); 84 85 /* Create filesystem, note superblock locations */ 86 create_lfs(FSSIZE, FSSIZE, width, 1); 87 88 /* 89 * Create initial filesystem state, from which 90 * we will attempt to roll forward. 91 */ 92 93 /* Mount filesystem */ 94 fprintf(stderr, "* Mount fs [1, initial]\n"); 95 memset(&args, 0, sizeof(args)); 96 args.fspec = __UNCONST(FAKEBLK); 97 if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1) 98 atf_tc_fail_errno("rump_sys_mount failed"); 99 100 /* Payload */ 101 fprintf(stderr, "* Initial payload\n"); 102 write_file(UNCHANGED_CONTROL, CHUNKSIZE, 1); 103 write_file(TO_BE_DELETED, CHUNKSIZE, 1); 104 write_file(TO_BE_APPENDED, CHUNKSIZE, 1); 105 rump_sys_sync(); 106 rump_sys_sync(); 107 sleep(1); /* XXX yuck - but we need the superblocks dirty */ 108 109 /* Make copies of superblocks */ 110 for (i = 0; i < 2; i++) { 111 sprintf(buf, "dd if=%s of=%s bs=512 iseek=%lld" 112 " count=16 conv=sync", IMGNAME, sblock[i], sbaddr[i]); 113 system(buf); 114 } 115 116 /* Unmount */ 117 rump_sys_unmount(MP, 0); 118 if (fsck()) 119 atf_tc_fail_errno("fsck found errors after first unmount"); 120 121 /* 122 * Make changes which we will attempt to roll forward. 123 */ 124 125 /* Reconfigure and mount filesystem again */ 126 fprintf(stderr, "* Mount fs [2, after changes]\n"); 127 if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1) 128 atf_tc_fail_errno("rump_sys_mount failed [2]"); 129 130 fprintf(stderr, "* Update payload\n"); 131 132 /* Add new file */ 133 write_file(NEWLY_CREATED, CHUNKSIZE, 1); 134 135 /* Append to existing file */ 136 write_file(TO_BE_APPENDED, CHUNKSIZE, 1); 137 138 /* Delete file */ 139 rump_sys_unlink(TO_BE_DELETED); 140 141 /* Done with payload, unmount fs */ 142 rump_sys_unmount(MP, 0); 143 if (fsck()) 144 atf_tc_fail_errno("fsck found errors after second unmount"); 145 146 #ifndef FORCE_SUCCESS 147 fprintf(stderr, "* Revert superblocks\n"); 148 /* 149 * Copy back old superblocks, reverting FS to old state 150 */ 151 for (i = 0; i < 2; i++) { 152 sprintf(buf, "dd of=%s if=%s bs=512 oseek=%lld count=16" 153 " conv=sync,notrunc", IMGNAME, sblock[i], sbaddr[i]); 154 system(buf); 155 } 156 157 if (fsck()) 158 atf_tc_fail_errno("fsck found errors with old superblocks"); 159 #endif /* FORCE_SUCCESS */ 160 #ifdef USE_DUMPLFS 161 dumplfs(); 162 #endif /* USE_DUMPLFS */ 163 164 /* 165 * Roll forward. 166 */ 167 168 /* Mount filesystem; this will roll forward. */ 169 fprintf(stderr, "* Mount fs [3, to roll forward]\n"); 170 if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1) 171 atf_tc_fail_errno("rump_sys_mount failed [3]"); 172 173 /* Unmount filesystem */ 174 if (rump_sys_unmount(MP, 0) != 0) 175 atf_tc_fail_errno("rump_sys_umount failed after roll-forward"); 176 177 /* 178 * Use fsck_lfs to look for consistency errors. 179 */ 180 181 fprintf(stderr, "* Fsck after roll-forward\n"); 182 if (fsck()) { 183 fprintf(stderr, "*** FAILED FSCK ***\n"); 184 atf_tc_fail("fsck found errors after roll forward"); 185 } 186 #ifdef USE_DUMPLFS 187 dumplfs(); 188 #endif /* USE_DUMPLFS */ 189 190 /* 191 * Check file system contents 192 */ 193 194 /* Mount filesystem one last time */ 195 fprintf(stderr, "* Mount fs [4, after roll forward complete]\n"); 196 if (rump_sys_mount(MOUNT_LFS, MP, 0, &args, sizeof(args)) == -1) 197 atf_tc_fail_errno("rump_sys_mount failed [4]"); 198 199 if (check_file(UNCHANGED_CONTROL, CHUNKSIZE) != 0) 200 atf_tc_fail("Unchanged control file differs(!)"); 201 202 if (rump_sys_access(TO_BE_DELETED, F_OK) == 0) 203 atf_tc_fail("Removed file still present"); 204 else 205 fprintf(stderr, "%s: no problem\n", TO_BE_DELETED); 206 207 if (check_file(TO_BE_APPENDED, 2 * CHUNKSIZE) != 0) 208 atf_tc_fail("Appended file differs"); 209 210 if (rump_sys_access(NEWLY_CREATED, F_OK) != 0) 211 atf_tc_fail("Newly added file missing"); 212 213 if (check_file(NEWLY_CREATED, CHUNKSIZE) != 0) 214 atf_tc_fail("Newly added file differs"); 215 216 /* Umount filesystem */ 217 rump_sys_unmount(MP, 0); 218 219 /* Final fsck to double check */ 220 fprintf(stderr, "* Fsck after final unmount\n"); 221 if (fsck()) { 222 fprintf(stderr, "*** FAILED FSCK ***\n"); 223 atf_tc_fail("fsck found errors after final unmount"); 224 } 225 } 226 227 ATF_TP_ADD_TCS(tp) 228 { 229 230 ATF_TP_ADD_TC(tp, rfw32); 231 ATF_TP_ADD_TC(tp, rfw64); 232 return atf_no_error(); 233 } 234