1 /* $NetBSD: util.c,v 1.6 2025/12/19 20:58:08 perseant Exp $ */ 2 3 #include <sys/mount.h> 4 5 #include <ctype.h> 6 #include <fcntl.h> 7 8 #include <rump/rump.h> 9 #include <rump/rump_syscalls.h> 10 11 #include "h_macros.h" 12 #include "util.h" 13 14 long long sbaddr[2] = { -1, -1 }; 15 16 /* Create filesystem, note superblock locations */ 17 void create_lfs(size_t imgsize, size_t fssize, int width, int do_setup) 18 { 19 FILE *pipe; 20 char cmd[MAXLINE]; 21 char buf[MAXLINE]; 22 23 /* Create image file larger than filesystem */ 24 sprintf(cmd, "dd if=/dev/zero of=%s bs=512 count=%zd", 25 IMGNAME, imgsize); 26 if (system(cmd) == -1) 27 atf_tc_fail_errno("create image failed"); 28 29 /* Create filesystem */ 30 fprintf(stderr, "* Create file system\n"); 31 sprintf(cmd, "newfs_lfs -D -F -B %d -s %zd -w%d ./%s > %s", 32 SEGSIZE, fssize, width, IMGNAME, LOGFILE); 33 if (system(cmd) == -1) 34 atf_tc_fail_errno("newfs failed"); 35 pipe = fopen(LOGFILE, "r"); 36 if (pipe == NULL) 37 atf_tc_fail_errno("newfs failed to execute"); 38 while (fgets(buf, MAXLINE, pipe) != NULL) { 39 if (sscanf(buf, "%lld,%lld", sbaddr, sbaddr + 1) == 2) 40 break; 41 } 42 while (fgets(buf, MAXLINE, pipe) != NULL) 43 ; 44 fclose(pipe); 45 if (sbaddr[0] < 0 || sbaddr[1] < 0) 46 atf_tc_fail("superblock not found"); 47 fprintf(stderr, "* Superblocks at %lld and %lld\n", 48 sbaddr[0], sbaddr[1]); 49 50 if (do_setup) { 51 /* Set up rump */ 52 rump_init(); 53 if (rump_sys_mkdir(MP, 0777) == -1) 54 atf_tc_fail_errno("cannot create mountpoint"); 55 rump_pub_etfs_register(FAKEBLK, IMGNAME, RUMP_ETFS_BLK); 56 } 57 } 58 59 /* Write some data into a file */ 60 int write_file(const char *filename, off_t len, int close, unsigned int seed) 61 { 62 int fd; 63 unsigned i, j; 64 struct stat statbuf; 65 int flags = O_CREAT|O_WRONLY; 66 char buf[1024]; 67 off_t size; 68 69 srandom(seed); 70 if (rump_sys_stat(filename, &statbuf) < 0) 71 size = 0; 72 else { 73 size = statbuf.st_size; 74 flags |= O_APPEND; 75 76 /* Reset randomness */ 77 for (i = 0; i < size; i++) 78 random(); 79 } 80 81 fd = rump_sys_open(filename, flags); 82 83 for (i = 0; i < len; i+= sizeof(buf)) { 84 for (j = 0; j < sizeof(buf); j++) 85 buf[j] = ((unsigned)random()) & 0xff; 86 rump_sys_write(fd, buf, MIN(len - i, (off_t)sizeof(buf))); 87 } 88 89 if (close) { 90 rump_sys_close(fd); 91 fd = -1; 92 } 93 94 return fd; 95 } 96 97 /* Check file's existence, size and contents */ 98 int check_file(const char *filename, int size, unsigned int seed) 99 { 100 int fd, i, j, res; 101 struct stat statbuf; 102 unsigned char b, buf[1024]; 103 104 if (rump_sys_stat(filename, &statbuf) < 0) { 105 fprintf(stderr, "%s: stat failed\n", filename); 106 return 1; 107 } 108 if (size != statbuf.st_size) { 109 fprintf(stderr, "%s: expected %d bytes, found %d\n", 110 filename, size, (int)statbuf.st_size); 111 return 2; 112 } 113 114 fd = rump_sys_open(filename, O_RDONLY); 115 116 srandom(seed); 117 for (i = 0; i < size; i += sizeof(buf)) { 118 res = MIN(size - i, (off_t)sizeof(buf)); 119 rump_sys_read(fd, buf, res); 120 for (j = 0; j < res; j++) { 121 b = (((unsigned)random()) & 0xff); 122 if (buf[j] != b) { 123 fprintf(stderr, "%s: byte %d:" 124 " expected %hhx found %hhx\n", 125 filename, i + j, 126 b, buf[j]); 127 rump_sys_close(fd); 128 return 3; 129 } 130 } 131 } 132 rump_sys_close(fd); 133 fprintf(stderr, "%s: no problem\n", filename); 134 return 0; 135 } 136 137 /* Run a file system consistency check */ 138 int fsck(void) 139 { 140 char s[MAXLINE]; 141 int i, errors = 0; 142 FILE *pipe; 143 char cmd[MAXLINE]; 144 145 for (i = 0; i < 2; i++) { 146 sprintf(cmd, "fsck_lfs -n -a -b %jd -f " IMGNAME, 147 (intmax_t)sbaddr[i]); 148 pipe = popen(cmd, "r"); 149 while (fgets(s, MAXLINE, pipe) != NULL) { 150 if (isdigit((int)s[0])) /* "5 files ... " */ 151 continue; 152 if (isspace((int)s[0]) || s[0] == '*') 153 continue; 154 if (strncmp(s, "Alternate", 9) == 0) 155 continue; 156 if (strncmp(s, "ROLL ", 5) == 0) 157 continue; 158 fprintf(stderr, "FSCK[sb@%lld]: %s", sbaddr[i], s); 159 ++errors; 160 } 161 pclose(pipe); 162 if (errors) { 163 break; 164 } 165 } 166 167 return errors; 168 } 169 170 /* Run dumplfs */ 171 void dumplfs() 172 { 173 char s[MAXLINE]; 174 FILE *pipe; 175 176 pipe = popen("dumplfs -S -s 2 -s 1 -s 0 " IMGNAME, "r"); 177 while (fgets(s, MAXLINE, pipe) != NULL) 178 fprintf(stderr, "DUMPLFS: %s", s); 179 pclose(pipe); 180 } 181 182