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