Merge remote-tracking branch 'origin/vendor/LIBARCHIVE'
[dragonfly.git] / sbin / dumpfs / dumpfs.c
1 /*
2  * Copyright (c) 1983, 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1983, 1992, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)dumpfs.c 8.5 (Berkeley) 4/29/95
31  * $FreeBSD: src/sbin/dumpfs/dumpfs.c,v 1.13.2.1 2001/01/22 18:10:11 iedowse Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/time.h>
36
37 #include <vfs/ufs/dinode.h>
38 #include <vfs/ufs/fs.h>
39
40 #include <err.h>
41 #include <fcntl.h>
42 #include <fstab.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 static union {
49         struct fs fs;
50         char pad[MAXBSIZE];
51 } fsun;
52 #define afs     fsun.fs
53
54 static union {
55         struct cg cg;
56         char pad[MAXBSIZE];
57 } cgun;
58 #define acg     cgun.cg
59
60 static long     dev_bsize = 1;
61
62 static int      dumpfs(char *);
63 static int      dumpcg(char *, int, int);
64 static int      marshal(const char*);
65 static void     pbits(void *, int);
66 static void     usage(void) __dead2;
67
68 int
69 main(int argc, char **argv)
70 {
71         struct fstab *fs;
72         int ch, domarshal, eval;
73
74         domarshal = 0;
75
76         while ((ch = getopt(argc, argv, "m")) != -1)
77                 switch(ch) {
78                 case 'm':
79                         domarshal = 1;
80                         break;
81                 case '?':
82                 default:
83                         usage();
84                 }
85         argc -= optind;
86         argv += optind;
87
88         if (argc < 1)
89                 usage();
90
91         for (eval = 0; *argv; ++argv)
92                 if ((fs = getfsfile(*argv)) == NULL)
93                         if (domarshal)
94                                 eval |= marshal(*argv);
95                         else
96                                 eval |= dumpfs(*argv);
97                 else
98                         if (domarshal)
99                                 eval |= marshal(fs->fs_spec);
100                         else
101                                 eval |= dumpfs(fs->fs_spec);
102         exit(eval);
103 }
104
105 static int
106 dumpfs(char *name)
107 {
108         ssize_t n;
109         int fd, c, i, j, k, size;
110         time_t fs_time;
111
112         if ((fd = open(name, O_RDONLY, 0)) < 0)
113                 goto err;
114         if (lseek(fd, (off_t)SBOFF, SEEK_SET) == (off_t)-1)
115                 goto err;
116         if ((n = read(fd, &afs, SBSIZE)) == -1)
117                 goto err;
118
119         if (n != SBSIZE) {
120                 warnx("%s: non-existent or truncated superblock, skipped",
121                     name);
122                 close(fd);
123                 return (1);
124         }
125         if (afs.fs_magic != FS_MAGIC) {
126                 warnx("%s: superblock has bad magic number, skipped", name);
127                 close(fd);
128                 return (1);
129         }
130
131         if (afs.fs_postblformat == FS_42POSTBLFMT)
132                 afs.fs_nrpos = 8;
133         dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
134         fs_time = afs.fs_time;
135         printf("magic\t%x\ttime\t%s", afs.fs_magic, ctime(&fs_time));
136         printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
137         printf("cylgrp\t%s\tinodes\t%s\n",
138             afs.fs_postblformat == FS_42POSTBLFMT ? "static" : "dynamic",
139             afs.fs_inodefmt < FS_44INODEFMT ? "4.2/4.3BSD" : "4.4BSD");
140         printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
141             afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
142             afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
143         printf("ncg\t%d\tncyl\t%d\tsize\t%d\tblocks\t%d\n",
144             afs.fs_ncg, afs.fs_ncyl, afs.fs_size, afs.fs_dsize);
145         printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
146             afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
147         printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
148             afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
149         printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
150             afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
151         printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
152             afs.fs_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
153         printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
154             afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
155             afs.fs_maxcontig, afs.fs_maxbpg);
156         printf("rotdelay %dms\trps\t%d\n",
157             afs.fs_rotdelay, afs.fs_rps);
158         printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
159             afs.fs_ntrak, afs.fs_nsect, afs.fs_npsect, afs.fs_spc);
160         printf("symlinklen %d\ttrackskew %d\tinterleave %d\tcontigsumsize %d\n",
161             afs.fs_maxsymlinklen, afs.fs_trackskew, afs.fs_interleave,
162             afs.fs_contigsumsize);
163         printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%ju\n",
164             afs.fs_nindir, afs.fs_inopb, afs.fs_nspf,
165             (intmax_t)afs.fs_maxfilesize);
166         printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
167             afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
168         printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
169             afs.fs_sbsize, afs.fs_cgsize, afs.fs_cgoffset, afs.fs_cgmask);
170         printf("csaddr\t%d\tcssize\t%d\tshift\t%d\tmask\t0x%08x\n",
171             afs.fs_csaddr, afs.fs_cssize, afs.fs_csshift, afs.fs_csmask);
172         printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
173             afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
174         printf("flags\t");
175         if (afs.fs_flags == 0)
176                 printf("none");
177         if (afs.fs_flags & FS_UNCLEAN)
178                         printf("unclean ");
179         if (afs.fs_flags & FS_DOSOFTDEP)
180                         printf("soft-updates ");
181         if ((afs.fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP)) != 0)
182                         printf("unknown flags (%#x)", 
183                             afs.fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP));
184         putchar('\n');
185         if (afs.fs_cpc != 0)
186                 printf("blocks available in each of %d rotational positions",
187                      afs.fs_nrpos);
188         else
189                 printf("(no rotational position table)\n");
190         for (c = 0; c < afs.fs_cpc; c++) {
191                 printf("\ncylinder number %d:", c);
192                 for (i = 0; i < afs.fs_nrpos; i++) {
193                         if (fs_postbl(&afs, c)[i] == -1)
194                                 continue;
195                         printf("\n   position %d:\t", i);
196                         for (j = fs_postbl(&afs, c)[i], k = 1; ;
197                              j += fs_rotbl(&afs)[j], k++) {
198                                 printf("%5d", j);
199                                 if (k % 12 == 0)
200                                         printf("\n\t\t");
201                                 if (fs_rotbl(&afs)[j] == 0)
202                                         break;
203                         }
204                 }
205         }
206         printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
207         afs.fs_csp = calloc(1, afs.fs_cssize);
208         for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
209                 size = afs.fs_cssize - i < afs.fs_bsize ?
210                     afs.fs_cssize - i : afs.fs_bsize;
211                 if (lseek(fd,
212                     (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) *
213                     (off_t)dev_bsize, SEEK_SET) == (off_t)-1)
214                         goto err;
215                 if (read(fd, (char *)afs.fs_csp + i, size) != size)
216                         goto err;
217         }
218         for (i = 0; i < afs.fs_ncg; i++) {
219                 struct csum *cs = &afs.fs_cs(&afs, i);
220                 if (i && i % 4 == 0)
221                         printf("\n\t");
222                 printf("(%d,%d,%d,%d) ",
223                     cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
224         }
225         printf("\n");
226         if (afs.fs_ncyl % afs.fs_cpg) {
227                 printf("cylinders in last group %d\n",
228                     i = afs.fs_ncyl % afs.fs_cpg);
229                 printf("blocks in last group %d\n",
230                     i * afs.fs_spc / NSPB(&afs));
231         }
232         printf("\n");
233         for (i = 0; i < afs.fs_ncg; i++)
234                 if (dumpcg(name, fd, i))
235                         goto err;
236         close(fd);
237         return (0);
238
239 err:    if (fd != -1)
240                 close(fd);
241         warn("%s", name);
242         return (1);
243 };
244
245 static int
246 dumpcg(char *name, int fd, int c)
247 {
248         off_t cur;
249         int i, j;
250         time_t cg_time;
251
252         printf("\ncg %d:\n", c);
253         if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) *
254             (off_t)dev_bsize, SEEK_SET)) == (off_t)-1)
255                 return (1);
256         if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
257                 warnx("%s: error reading cg", name);
258                 return (1);
259         }
260         cg_time = acg.cg_time;
261         printf("magic\t%x\ttell\t%jx\ttime\t%s",
262             afs.fs_postblformat == FS_42POSTBLFMT ?
263             ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
264             (intmax_t)cur, ctime(&cg_time));
265         printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
266             acg.cg_cgx, acg.cg_ncyl, acg.cg_niblk, acg.cg_ndblk);
267         printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
268             acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
269             acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
270         printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
271             acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
272         for (i = 1, j = 0; i < afs.fs_frag; i++) {
273                 printf("\t%d", acg.cg_frsum[i]);
274                 j += i * acg.cg_frsum[i];
275         }
276         printf("\nsum of frsum: %d", j);
277         if (afs.fs_contigsumsize > 0) {
278                 for (i = 1; i < afs.fs_contigsumsize; i++) {
279                         if ((i - 1) % 8 == 0)
280                                 printf("\nclusters %d-%d:", i,
281                                     afs.fs_contigsumsize - 1 < i + 7 ?
282                                     afs.fs_contigsumsize - 1 : i + 7);
283                         printf("\t%d", cg_clustersum(&acg)[i]);
284                 }
285                 printf("\nclusters size %d and over: %d\n",
286                     afs.fs_contigsumsize,
287                     cg_clustersum(&acg)[afs.fs_contigsumsize]);
288                 printf("clusters free:\t");
289                 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
290         } else
291                 printf("\n");
292         printf("iused:\t");
293         pbits(cg_inosused(&acg), afs.fs_ipg);
294         printf("free:\t");
295         pbits(cg_blksfree(&acg), afs.fs_fpg);
296         printf("b:\n");
297         for (i = 0; i < afs.fs_cpg; i++) {
298                 if (cg_blktot(&acg)[i] == 0)
299                         continue;
300                 printf("   c%d:\t(%d)\t", i, cg_blktot(&acg)[i]);
301                 for (j = 0; j < afs.fs_nrpos; j++) {
302                         if (afs.fs_cpc > 0 &&
303                             fs_postbl(&afs, i % afs.fs_cpc)[j] == -1)
304                                 continue;
305                         printf(" %d", cg_blks(&afs, &acg, i)[j]);
306                 }
307                 printf("\n");
308         }
309         return (0);
310 };
311
312 static int
313 marshal(const char *name)
314 {
315         ssize_t n;
316         int fd;
317         static char realname[PATH_MAX];
318
319         if ((fd = open(name, O_RDONLY, 0)) < 0)
320                 goto err;
321         if (lseek(fd, (off_t)SBOFF, SEEK_SET) == (off_t)-1)
322                 goto err;
323         if ((n = read(fd, &afs, SBSIZE)) == -1)
324                 goto err;
325
326         if (n != SBSIZE) {
327                 warnx("%s: non-existent or truncated superblock, skipped",
328                     name);
329                 close(fd);
330                 return (1);
331         }
332         if (afs.fs_magic != FS_MAGIC) {
333                 warnx("%s: superblock has bad magic number, skipped", name);
334                 close(fd);
335                 return (1);
336         }
337
338         if(strncmp(name, "/dev", 4) == 0) {
339                 snprintf(realname, PATH_MAX, "%s", name);
340         } else {
341                 snprintf(realname, PATH_MAX, "/dev/%s", name);
342         }
343
344         printf("# newfs command for %s (%s)\n", name, realname);
345         printf("newfs ");
346         if (afs.fs_flags & FS_DOSOFTDEP)
347                 printf("-U ");
348         printf("-a %d ", afs.fs_maxcontig);
349         printf("-b %d ", afs.fs_bsize);
350         /* -c calculated */
351         /* -d should be 0 per manpage */
352         printf("-e %d ", afs.fs_maxbpg);
353         printf("-f %d ", afs.fs_fsize);
354         printf("-g %d ", afs.fs_avgfilesize);
355         printf("-h %d ", afs.fs_avgfpdir);
356         /* -i calculated */
357         /* -j..l not implemented */
358         printf("-m %d ", afs.fs_minfree);
359         /* -n not implemented */
360         printf(" -o ");
361         switch (afs.fs_optim) {
362         case FS_OPTSPACE:
363                 printf("space ");
364                 break;
365         case FS_OPTTIME:
366                 printf("time ");
367                 break;
368         default:
369                 printf("unknown ");
370                 break;
371         }
372         /* -p..r not implemented */
373         printf("-s %jd ", (intmax_t)afs.fs_size);
374         printf("%s ", realname);
375         printf("\n");
376
377         return (0);
378
379 err:    if (fd != -1)
380                 close(fd);
381         warn("%s", name);
382         return (1);
383
384 }
385
386 static void
387 pbits(void *vp, int max)
388 {
389         int i;
390         char *p;
391         int count, j;
392
393         for (count = i = 0, p = vp; i < max; i++)
394                 if (isset(p, i)) {
395                         if (count)
396                                 printf(",%s", count % 6 ? " " : "\n\t");
397                         count++;
398                         printf("%d", i);
399                         j = i;
400                         while ((i+1)<max && isset(p, i+1))
401                                 i++;
402                         if (i != j)
403                                 printf("-%d", i);
404                 }
405         printf("\n");
406 }
407
408 static void
409 usage(void)
410 {
411         fprintf(stderr, "usage: dumpfs filesys | device\n");
412         exit(1);
413 }