1 /* 2 * Copyright (c) 2002 Networks Associates Technology, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project by Marshall 6 * Kirk McKusick and Network Associates Laboratories, the Security 7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 9 * research program. 10 * 11 * Copyright (c) 1983, 1992, 1993 12 * The Regents of the University of California. All rights reserved. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #ifndef lint 40 static const char copyright[] = 41 "@(#) Copyright (c) 1983, 1992, 1993\n\ 42 The Regents of the University of California. All rights reserved.\n"; 43 #endif /* not lint */ 44 45 #ifndef lint 46 #if 0 47 static char sccsid[] = "@(#)dumpfs.c 8.5 (Berkeley) 4/29/95"; 48 #endif 49 static const char rcsid[] = 50 "$FreeBSD$"; 51 #endif /* not lint */ 52 53 #include <sys/param.h> 54 #include <sys/time.h> 55 #include <sys/disklabel.h> 56 57 #include <ufs/ufs/dinode.h> 58 #include <ufs/ffs/fs.h> 59 60 #include <err.h> 61 #include <errno.h> 62 #include <fcntl.h> 63 #include <fstab.h> 64 #include <libufs.h> 65 #include <stdint.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <unistd.h> 69 70 #define afs disk.d_fs 71 #define acg disk.d_cg 72 73 struct uufsd disk; 74 75 int dumpfs(const char *); 76 int dumpcg(void); 77 int marshal(const char *); 78 void pbits(void *, int); 79 void ufserr(const char *); 80 void usage(void) __dead2; 81 82 int 83 main(int argc, char *argv[]) 84 { 85 const char *name; 86 int ch, domarshal, eval; 87 88 domarshal = eval = 0; 89 90 while ((ch = getopt(argc, argv, "m")) != -1) { 91 switch (ch) { 92 case 'm': 93 domarshal = 1; 94 break; 95 case '?': 96 default: 97 usage(); 98 } 99 } 100 argc -= optind; 101 argv += optind; 102 103 if (argc < 1) 104 usage(); 105 106 while ((name = *argv++) != NULL) { 107 if (ufs_disk_fillout(&disk, name) == -1) { 108 ufserr(name); 109 eval |= 1; 110 continue; 111 } 112 if (domarshal) 113 eval |= marshal(name); 114 else 115 eval |= dumpfs(name); 116 ufs_disk_close(&disk); 117 } 118 exit(eval); 119 } 120 121 int 122 dumpfs(const char *name) 123 { 124 time_t fstime; 125 int64_t fssize; 126 int32_t fsflags; 127 int i; 128 129 switch (disk.d_ufs) { 130 case 2: 131 fssize = afs.fs_size; 132 fstime = afs.fs_time; 133 printf("magic\t%x (UFS2)\ttime\t%s", 134 afs.fs_magic, ctime(&fstime)); 135 printf("superblock location\t%jd\tid\t[ %x %x ]\n", 136 (intmax_t)afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]); 137 printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n", 138 afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize); 139 break; 140 case 1: 141 fssize = afs.fs_old_size; 142 fstime = afs.fs_old_time; 143 printf("magic\t%x (UFS1)\ttime\t%s", 144 afs.fs_magic, ctime(&fstime)); 145 printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]); 146 printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n", 147 afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize); 148 break; 149 default: 150 goto err; 151 } 152 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n", 153 afs.fs_bsize, afs.fs_bshift, afs.fs_bmask); 154 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n", 155 afs.fs_fsize, afs.fs_fshift, afs.fs_fmask); 156 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n", 157 afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb); 158 printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n", 159 afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time", 160 afs.fs_maxsymlinklen); 161 switch (disk.d_ufs) { 162 case 2: 163 printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n", 164 "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg, 165 afs.fs_maxcontig, afs.fs_contigsumsize); 166 printf("nbfree\t%jd\tndir\t%jd\tnifree\t%jd\tnffree\t%jd\n", 167 (intmax_t)afs.fs_cstotal.cs_nbfree, 168 (intmax_t)afs.fs_cstotal.cs_ndir, 169 (intmax_t)afs.fs_cstotal.cs_nifree, 170 (intmax_t)afs.fs_cstotal.cs_nffree); 171 printf("bpg\t%d\tfpg\t%d\tipg\t%d\tunrefs\t%jd\n", 172 afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg, 173 (intmax_t)afs.fs_unrefs); 174 printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%ju\n", 175 afs.fs_nindir, afs.fs_inopb, 176 (uintmax_t)afs.fs_maxfilesize); 177 printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n", 178 afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr, 179 afs.fs_cssize); 180 break; 181 case 1: 182 printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n", 183 afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize); 184 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 185 afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir, 186 afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree); 187 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n", 188 afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg, 189 afs.fs_ipg); 190 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%ju\n", 191 afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf, 192 (uintmax_t)afs.fs_maxfilesize); 193 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n", 194 afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset, 195 afs.fs_old_cgmask); 196 printf("csaddr\t%d\tcssize\t%d\n", 197 afs.fs_old_csaddr, afs.fs_cssize); 198 printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n", 199 afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew, 200 afs.fs_old_interleave); 201 printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n", 202 afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc); 203 break; 204 default: 205 goto err; 206 } 207 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n", 208 afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno); 209 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n", 210 afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean); 211 printf("avgfpdir %d\tavgfilesize %d\n", 212 afs.fs_avgfpdir, afs.fs_avgfilesize); 213 printf("flags\t"); 214 if (afs.fs_old_flags & FS_FLAGS_UPDATED) 215 fsflags = afs.fs_flags; 216 else 217 fsflags = afs.fs_old_flags; 218 if (fsflags == 0) 219 printf("none"); 220 if (fsflags & FS_UNCLEAN) 221 printf("unclean "); 222 if (fsflags & FS_DOSOFTDEP) 223 printf("soft-updates "); 224 if (fsflags & FS_NEEDSFSCK) 225 printf("needs fsck run "); 226 if (fsflags & FS_INDEXDIRS) 227 printf("indexed directories "); 228 if (fsflags & FS_ACLS) 229 printf("acls "); 230 if (fsflags & FS_MULTILABEL) 231 printf("multilabel "); 232 if (fsflags & FS_GJOURNAL) 233 printf("gjournal "); 234 if (fsflags & FS_FLAGS_UPDATED) 235 printf("fs_flags expanded "); 236 fsflags &= ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS | 237 FS_ACLS | FS_MULTILABEL | FS_GJOURNAL | FS_FLAGS_UPDATED); 238 if (fsflags != 0) 239 printf("unknown flags (%#x)", fsflags); 240 putchar('\n'); 241 printf("fsmnt\t%s\n", afs.fs_fsmnt); 242 printf("volname\t%s\tswuid\t%ju\n", 243 afs.fs_volname, (uintmax_t)afs.fs_swuid); 244 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t"); 245 afs.fs_csp = calloc(1, afs.fs_cssize); 246 if (bread(&disk, fsbtodb(&afs, afs.fs_csaddr), afs.fs_csp, afs.fs_cssize) == -1) 247 goto err; 248 for (i = 0; i < afs.fs_ncg; i++) { 249 struct csum *cs = &afs.fs_cs(&afs, i); 250 if (i && i % 4 == 0) 251 printf("\n\t"); 252 printf("(%d,%d,%d,%d) ", 253 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree); 254 } 255 printf("\n"); 256 if (fssize % afs.fs_fpg) { 257 if (disk.d_ufs == 1) 258 printf("cylinders in last group %d\n", 259 howmany(afs.fs_old_size % afs.fs_fpg, 260 afs.fs_old_spc / afs.fs_old_nspf)); 261 printf("blocks in last group %ld\n\n", 262 (long)((fssize % afs.fs_fpg) / afs.fs_frag)); 263 } 264 while ((i = cgread(&disk)) != 0) { 265 if (i == -1 || dumpcg()) 266 goto err; 267 } 268 return (0); 269 270 err: ufserr(name); 271 return (1); 272 } 273 274 int 275 dumpcg(void) 276 { 277 time_t cgtime; 278 off_t cur; 279 int i, j; 280 281 printf("\ncg %d:\n", disk.d_lcg); 282 cur = fsbtodb(&afs, cgtod(&afs, disk.d_lcg)) * disk.d_bsize; 283 switch (disk.d_ufs) { 284 case 2: 285 cgtime = acg.cg_time; 286 printf("magic\t%x\ttell\t%jx\ttime\t%s", 287 acg.cg_magic, (intmax_t)cur, ctime(&cgtime)); 288 printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\tunrefs %d\n", 289 acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk, 290 acg.cg_unrefs); 291 break; 292 case 1: 293 cgtime = acg.cg_old_time; 294 printf("magic\t%x\ttell\t%jx\ttime\t%s", 295 acg.cg_magic, (intmax_t)cur, ctime(&cgtime)); 296 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n", 297 acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk, 298 acg.cg_ndblk); 299 break; 300 default: 301 break; 302 } 303 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n", 304 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir, 305 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree); 306 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum", 307 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor); 308 for (i = 1, j = 0; i < afs.fs_frag; i++) { 309 printf("\t%d", acg.cg_frsum[i]); 310 j += i * acg.cg_frsum[i]; 311 } 312 printf("\nsum of frsum: %d", j); 313 if (afs.fs_contigsumsize > 0) { 314 for (i = 1; i < afs.fs_contigsumsize; i++) { 315 if ((i - 1) % 8 == 0) 316 printf("\nclusters %d-%d:", i, 317 afs.fs_contigsumsize - 1 < i + 7 ? 318 afs.fs_contigsumsize - 1 : i + 7); 319 printf("\t%d", cg_clustersum(&acg)[i]); 320 } 321 printf("\nclusters size %d and over: %d\n", 322 afs.fs_contigsumsize, 323 cg_clustersum(&acg)[afs.fs_contigsumsize]); 324 printf("clusters free:\t"); 325 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks); 326 } else 327 printf("\n"); 328 printf("inodes used:\t"); 329 pbits(cg_inosused(&acg), afs.fs_ipg); 330 printf("blks free:\t"); 331 pbits(cg_blksfree(&acg), afs.fs_fpg); 332 return (0); 333 } 334 335 int 336 marshal(const char *name) 337 { 338 struct fs *fs; 339 340 fs = &disk.d_fs; 341 342 printf("# newfs command for %s (%s)\n", name, disk.d_name); 343 printf("newfs "); 344 if (fs->fs_volname[0] != '\0') 345 printf("-L %s ", fs->fs_volname); 346 printf("-O %d ", disk.d_ufs); 347 if (fs->fs_flags & FS_DOSOFTDEP) 348 printf("-U "); 349 printf("-a %d ", fs->fs_maxcontig); 350 printf("-b %d ", fs->fs_bsize); 351 /* -c is dumb */ 352 printf("-d %d ", fs->fs_maxbsize); 353 printf("-e %d ", fs->fs_maxbpg); 354 printf("-f %d ", fs->fs_fsize); 355 printf("-g %d ", fs->fs_avgfilesize); 356 printf("-h %d ", fs->fs_avgfpdir); 357 /* -i is dumb */ 358 /* -j..l unimplemented */ 359 printf("-m %d ", fs->fs_minfree); 360 /* -n unimplemented */ 361 printf("-o "); 362 switch (fs->fs_optim) { 363 case FS_OPTSPACE: 364 printf("space "); 365 break; 366 case FS_OPTTIME: 367 printf("time "); 368 break; 369 default: 370 printf("unknown "); 371 break; 372 } 373 /* -p..r unimplemented */ 374 printf("-s %jd ", (intmax_t)fs->fs_size); 375 printf("%s ", disk.d_name); 376 printf("\n"); 377 378 return 0; 379 } 380 381 void 382 pbits(void *vp, int max) 383 { 384 int i; 385 char *p; 386 int count, j; 387 388 for (count = i = 0, p = vp; i < max; i++) 389 if (isset(p, i)) { 390 if (count) 391 printf(",%s", count % 6 ? " " : "\n\t"); 392 count++; 393 printf("%d", i); 394 j = i; 395 while ((i+1)<max && isset(p, i+1)) 396 i++; 397 if (i != j) 398 printf("-%d", i); 399 } 400 printf("\n"); 401 } 402 403 void 404 ufserr(const char *name) 405 { 406 if (disk.d_error != NULL) 407 warnx("%s: %s", name, disk.d_error); 408 else if (errno) 409 warn("%s", name); 410 } 411 412 void 413 usage(void) 414 { 415 (void)fprintf(stderr, "usage: dumpfs [-m] filesys | device\n"); 416 exit(1); 417 } 418