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