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