]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/growfs/debug.c
Merge branch 'releng/11.3' into releng-CDN/11.3
[FreeBSD/FreeBSD.git] / sbin / growfs / debug.c
1 /*
2  * Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
3  * Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
4  * All rights reserved.
5  * 
6  * This code is derived from software contributed to Berkeley by
7  * Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgment:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors, as well as Christoph
21  *      Herrmann and Thomas-Henning von Kamptz.
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  * $TSHeader: src/sbin/growfs/debug.c,v 1.3 2000/12/12 19:31:00 tomsoft Exp $
39  *
40  */
41
42 #ifndef lint
43 static const char rcsid[] =
44   "$FreeBSD$";
45 #endif /* not lint */
46
47 #include <sys/param.h>
48
49 #include <limits.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ffs/fs.h>
54
55 #include "debug.h"
56
57 #ifdef FS_DEBUG
58
59 static FILE             *dbg_log = NULL;
60 static unsigned int     indent = 0;
61
62 /*
63  * prototypes not done here, as they come with debug.h
64  */
65
66 /*
67  * Open the filehandle where all debug output has to go.
68  */
69 void
70 dbg_open(const char *fn)
71 {
72
73         if (strcmp(fn, "-") == 0)
74                 dbg_log = fopen("/dev/stdout", "a");
75         else
76                 dbg_log = fopen(fn, "a");
77
78         return;
79 }
80
81 /*
82  * Close the filehandle where all debug output went to.
83  */
84 void
85 dbg_close(void)
86 {
87
88         if (dbg_log) {
89                 fclose(dbg_log);
90                 dbg_log = NULL;
91         }
92
93         return;
94 }
95
96 /*
97  * Dump out a full file system block in hex.
98  */
99 void
100 dbg_dump_hex(struct fs *sb, const char *comment, unsigned char *mem)
101 {
102         int i, j, k;
103
104         if (!dbg_log)
105                 return;
106
107         fprintf(dbg_log, "===== START HEXDUMP =====\n");
108         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)mem, comment);
109         indent++;
110         for (i = 0; i < sb->fs_bsize; i += 24) {
111                 for (j = 0; j < 3; j++) {
112                         for (k = 0; k < 8; k++)
113                                 fprintf(dbg_log, "%02x ", *mem++);
114                         fprintf(dbg_log, "  ");
115                 }
116                 fprintf(dbg_log, "\n");
117         }
118         indent--;
119         fprintf(dbg_log, "===== END HEXDUMP =====\n");
120
121         return;
122 }
123
124 /*
125  * Dump the superblock.
126  */
127 void
128 dbg_dump_fs(struct fs *sb, const char *comment)
129 {
130         int j;
131
132         if (!dbg_log)
133                 return;
134
135         fprintf(dbg_log, "===== START SUPERBLOCK =====\n");
136         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)sb, comment);
137         indent++;
138
139         fprintf(dbg_log, "sblkno            int32_t          0x%08x\n",
140             sb->fs_sblkno);
141         fprintf(dbg_log, "cblkno            int32_t          0x%08x\n",
142             sb->fs_cblkno);
143         fprintf(dbg_log, "iblkno            int32_t          0x%08x\n",
144             sb->fs_iblkno);
145         fprintf(dbg_log, "dblkno            int32_t          0x%08x\n",
146             sb->fs_dblkno);
147
148         fprintf(dbg_log, "old_cgoffset      int32_t          0x%08x\n",
149             sb->fs_old_cgoffset);
150         fprintf(dbg_log, "old_cgmask        int32_t          0x%08x\n",
151             sb->fs_old_cgmask);
152         fprintf(dbg_log, "old_time          int32_t          %10u\n",
153             (unsigned int)sb->fs_old_time);
154         fprintf(dbg_log, "old_size          int32_t          0x%08x\n",
155             sb->fs_old_size);
156         fprintf(dbg_log, "old_dsize         int32_t          0x%08x\n",
157             sb->fs_old_dsize);
158         fprintf(dbg_log, "ncg               int32_t          0x%08x\n",
159             sb->fs_ncg);
160         fprintf(dbg_log, "bsize             int32_t          0x%08x\n",
161             sb->fs_bsize);
162         fprintf(dbg_log, "fsize             int32_t          0x%08x\n",
163             sb->fs_fsize);
164         fprintf(dbg_log, "frag              int32_t          0x%08x\n",
165             sb->fs_frag);
166
167         fprintf(dbg_log, "minfree           int32_t          0x%08x\n",
168             sb->fs_minfree);
169         fprintf(dbg_log, "old_rotdelay      int32_t          0x%08x\n",
170             sb->fs_old_rotdelay);
171         fprintf(dbg_log, "old_rps           int32_t          0x%08x\n",
172             sb->fs_old_rps);
173
174         fprintf(dbg_log, "bmask             int32_t          0x%08x\n",
175             sb->fs_bmask);
176         fprintf(dbg_log, "fmask             int32_t          0x%08x\n",
177             sb->fs_fmask);
178         fprintf(dbg_log, "bshift            int32_t          0x%08x\n",
179             sb->fs_bshift);
180         fprintf(dbg_log, "fshift            int32_t          0x%08x\n",
181             sb->fs_fshift);
182
183         fprintf(dbg_log, "maxcontig         int32_t          0x%08x\n",
184             sb->fs_maxcontig);
185         fprintf(dbg_log, "maxbpg            int32_t          0x%08x\n",
186             sb->fs_maxbpg);
187
188         fprintf(dbg_log, "fragshift         int32_t          0x%08x\n",
189             sb->fs_fragshift);
190         fprintf(dbg_log, "fsbtodb           int32_t          0x%08x\n",
191             sb->fs_fsbtodb);
192         fprintf(dbg_log, "sbsize            int32_t          0x%08x\n",
193             sb->fs_sbsize);
194         fprintf(dbg_log, "spare1            int32_t[2]       0x%08x 0x%08x\n",
195             sb->fs_spare1[0], sb->fs_spare1[1]);
196         fprintf(dbg_log, "nindir            int32_t          0x%08x\n",
197             sb->fs_nindir);
198         fprintf(dbg_log, "inopb             int32_t          0x%08x\n",
199             sb->fs_inopb);
200         fprintf(dbg_log, "old_nspf          int32_t          0x%08x\n",
201             sb->fs_old_nspf);
202
203         fprintf(dbg_log, "optim             int32_t          0x%08x\n",
204             sb->fs_optim);
205
206         fprintf(dbg_log, "old_npsect        int32_t          0x%08x\n",
207             sb->fs_old_npsect);
208         fprintf(dbg_log, "old_interleave    int32_t          0x%08x\n",
209             sb->fs_old_interleave);
210         fprintf(dbg_log, "old_trackskew     int32_t          0x%08x\n",
211             sb->fs_old_trackskew);
212
213         fprintf(dbg_log, "id                int32_t[2]       0x%08x 0x%08x\n",
214             sb->fs_id[0], sb->fs_id[1]);
215
216         fprintf(dbg_log, "old_csaddr        int32_t          0x%08x\n",
217             sb->fs_old_csaddr);
218         fprintf(dbg_log, "cssize            int32_t          0x%08x\n",
219             sb->fs_cssize);
220         fprintf(dbg_log, "cgsize            int32_t          0x%08x\n",
221             sb->fs_cgsize);
222
223         fprintf(dbg_log, "spare2            int32_t          0x%08x\n",
224             sb->fs_spare2);
225         fprintf(dbg_log, "old_nsect         int32_t          0x%08x\n",
226             sb->fs_old_nsect);
227         fprintf(dbg_log, "old_spc           int32_t          0x%08x\n",
228             sb->fs_old_spc);
229
230         fprintf(dbg_log, "old_ncyl          int32_t          0x%08x\n",
231             sb->fs_old_ncyl);
232
233         fprintf(dbg_log, "old_cpg           int32_t          0x%08x\n",
234             sb->fs_old_cpg);
235         fprintf(dbg_log, "ipg               int32_t          0x%08x\n",
236             sb->fs_ipg);
237         fprintf(dbg_log, "fpg               int32_t          0x%08x\n",
238             sb->fs_fpg);
239
240         dbg_dump_csum("internal old_cstotal", &sb->fs_old_cstotal);
241
242         fprintf(dbg_log, "fmod              int8_t           0x%02x\n",
243             sb->fs_fmod);
244         fprintf(dbg_log, "clean             int8_t           0x%02x\n",
245             sb->fs_clean);
246         fprintf(dbg_log, "ronly             int8_t           0x%02x\n",
247             sb->fs_ronly);
248         fprintf(dbg_log, "old_flags         int8_t           0x%02x\n",
249             sb->fs_old_flags);
250         fprintf(dbg_log, "fsmnt             u_char[MAXMNTLEN] \"%s\"\n",
251             sb->fs_fsmnt);
252         fprintf(dbg_log, "volname           u_char[MAXVOLLEN] \"%s\"\n",
253             sb->fs_volname);
254         fprintf(dbg_log, "swuid             u_int64_t        0x%08x%08x\n",
255             ((unsigned int *)&(sb->fs_swuid))[1],
256                 ((unsigned int *)&(sb->fs_swuid))[0]);
257
258         fprintf(dbg_log, "pad               int32_t          0x%08x\n",
259             sb->fs_pad);
260
261         fprintf(dbg_log, "cgrotor           int32_t          0x%08x\n",
262             sb->fs_cgrotor);
263 /*
264  * struct csum[MAXCSBUFS] - is only maintained in memory
265  */
266 /*      fprintf(dbg_log, " int32_t\n", sb->*fs_maxcluster);*/
267         fprintf(dbg_log, "old_cpc           int32_t          0x%08x\n",
268             sb->fs_old_cpc);
269 /*
270  * int16_t fs_opostbl[16][8] - is dumped when used in dbg_dump_sptbl
271  */
272         fprintf(dbg_log, "maxbsize          int32_t          0x%08x\n",
273             sb->fs_maxbsize);
274         fprintf(dbg_log, "unrefs            int64_t          0x%08jx\n",
275             sb->fs_unrefs);
276         fprintf(dbg_log, "sblockloc         int64_t          0x%08x%08x\n",
277                 ((unsigned int *)&(sb->fs_sblockloc))[1],
278                 ((unsigned int *)&(sb->fs_sblockloc))[0]);
279
280         dbg_dump_csum_total("internal cstotal", &sb->fs_cstotal);
281
282         fprintf(dbg_log, "time              ufs_time_t       %10u\n",
283             (unsigned int)sb->fs_time);
284
285         fprintf(dbg_log, "size              int64_t          0x%08x%08x\n",
286                 ((unsigned int *)&(sb->fs_size))[1],
287                 ((unsigned int *)&(sb->fs_size))[0]);
288         fprintf(dbg_log, "dsize             int64_t          0x%08x%08x\n",
289                 ((unsigned int *)&(sb->fs_dsize))[1],
290                 ((unsigned int *)&(sb->fs_dsize))[0]);
291         fprintf(dbg_log, "csaddr            ufs2_daddr_t     0x%08x%08x\n",
292                 ((unsigned int *)&(sb->fs_csaddr))[1],
293                 ((unsigned int *)&(sb->fs_csaddr))[0]);
294         fprintf(dbg_log, "pendingblocks     int64_t          0x%08x%08x\n",
295                 ((unsigned int *)&(sb->fs_pendingblocks))[1],
296                 ((unsigned int *)&(sb->fs_pendingblocks))[0]);
297         fprintf(dbg_log, "pendinginodes     int32_t          0x%08x\n",
298             sb->fs_pendinginodes);
299
300         for (j = 0; j < FSMAXSNAP; j++) {
301                 fprintf(dbg_log, "snapinum          int32_t[%2d]      0x%08x\n",
302                     j, sb->fs_snapinum[j]);
303                 if (!sb->fs_snapinum[j]) { /* list is dense */
304                         break;
305                 }
306         }
307         fprintf(dbg_log, "avgfilesize       int32_t          0x%08x\n",
308             sb->fs_avgfilesize);
309         fprintf(dbg_log, "avgfpdir          int32_t          0x%08x\n",
310             sb->fs_avgfpdir);
311         fprintf(dbg_log, "save_cgsize       int32_t          0x%08x\n",
312             sb->fs_save_cgsize);
313         fprintf(dbg_log, "flags             int32_t          0x%08x\n",
314             sb->fs_flags);
315         fprintf(dbg_log, "contigsumsize     int32_t          0x%08x\n",
316             sb->fs_contigsumsize);
317         fprintf(dbg_log, "maxsymlinklen     int32_t          0x%08x\n",
318             sb->fs_maxsymlinklen);
319         fprintf(dbg_log, "old_inodefmt      int32_t          0x%08x\n",
320             sb->fs_old_inodefmt);
321         fprintf(dbg_log, "maxfilesize       u_int64_t        0x%08x%08x\n",
322             ((unsigned int *)&(sb->fs_maxfilesize))[1],
323             ((unsigned int *)&(sb->fs_maxfilesize))[0]);
324         fprintf(dbg_log, "qbmask            int64_t          0x%08x%08x\n",
325             ((unsigned int *)&(sb->fs_qbmask))[1],
326             ((unsigned int *)&(sb->fs_qbmask))[0]);
327         fprintf(dbg_log, "qfmask            int64_t          0x%08x%08x\n",
328             ((unsigned int *)&(sb->fs_qfmask))[1],
329             ((unsigned int *)&(sb->fs_qfmask))[0]);
330         fprintf(dbg_log, "state             int32_t          0x%08x\n",
331             sb->fs_state);
332         fprintf(dbg_log, "old_postblformat  int32_t          0x%08x\n",
333             sb->fs_old_postblformat);
334         fprintf(dbg_log, "old_nrpos         int32_t          0x%08x\n",
335             sb->fs_old_nrpos);
336         fprintf(dbg_log, "spare5            int32_t[2]       0x%08x 0x%08x\n",
337             sb->fs_spare5[0], sb->fs_spare5[1]);
338         fprintf(dbg_log, "magic             int32_t          0x%08x\n",
339             sb->fs_magic);
340
341         indent--;
342         fprintf(dbg_log, "===== END SUPERBLOCK =====\n");
343
344         return;
345 }
346
347 /*
348  * Dump a cylinder group.
349  */
350 void
351 dbg_dump_cg(const char *comment, struct cg *cgr)
352 {
353         int j;
354
355         if (!dbg_log)
356                 return;
357
358         fprintf(dbg_log, "===== START CYLINDER GROUP =====\n");
359         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)cgr, comment);
360         indent++;
361
362         fprintf(dbg_log, "magic         int32_t    0x%08x\n", cgr->cg_magic);
363         fprintf(dbg_log, "old_time      int32_t    0x%08x\n", cgr->cg_old_time);
364         fprintf(dbg_log, "cgx           int32_t    0x%08x\n", cgr->cg_cgx);
365         fprintf(dbg_log, "old_ncyl      int16_t    0x%04x\n", cgr->cg_old_ncyl);
366         fprintf(dbg_log, "old_niblk     int16_t    0x%04x\n", cgr->cg_old_niblk);
367         fprintf(dbg_log, "ndblk         int32_t    0x%08x\n", cgr->cg_ndblk);
368         dbg_dump_csum("internal cs", &cgr->cg_cs);
369         fprintf(dbg_log, "rotor         int32_t    0x%08x\n", cgr->cg_rotor);
370         fprintf(dbg_log, "frotor        int32_t    0x%08x\n", cgr->cg_frotor);
371         fprintf(dbg_log, "irotor        int32_t    0x%08x\n", cgr->cg_irotor);
372         for (j = 0; j < MAXFRAG; j++) {
373                 fprintf(dbg_log, "frsum         int32_t[%d] 0x%08x\n", j,
374                     cgr->cg_frsum[j]);
375         }
376         fprintf(dbg_log, "old_btotoff   int32_t    0x%08x\n", cgr->cg_old_btotoff);
377         fprintf(dbg_log, "old_boff      int32_t    0x%08x\n", cgr->cg_old_boff);
378         fprintf(dbg_log, "iusedoff      int32_t    0x%08x\n", cgr->cg_iusedoff);
379         fprintf(dbg_log, "freeoff       int32_t    0x%08x\n", cgr->cg_freeoff);
380         fprintf(dbg_log, "nextfreeoff   int32_t    0x%08x\n",
381             cgr->cg_nextfreeoff);
382         fprintf(dbg_log, "clustersumoff int32_t    0x%08x\n",
383             cgr->cg_clustersumoff);
384         fprintf(dbg_log, "clusteroff    int32_t    0x%08x\n",
385             cgr->cg_clusteroff);
386         fprintf(dbg_log, "nclusterblks  int32_t    0x%08x\n",
387             cgr->cg_nclusterblks);
388         fprintf(dbg_log, "niblk         int32_t    0x%08x\n", cgr->cg_niblk);
389         fprintf(dbg_log, "initediblk    int32_t    0x%08x\n", cgr->cg_initediblk);
390         fprintf(dbg_log, "unrefs        int32_t    0x%08x\n", cgr->cg_unrefs);
391         fprintf(dbg_log, "time          ufs_time_t %10u\n", 
392                 (unsigned int)cgr->cg_initediblk);
393
394         indent--;
395         fprintf(dbg_log, "===== END CYLINDER GROUP =====\n");
396
397         return;
398 }
399
400 /*
401  * Dump a cylinder summary.
402  */
403 void
404 dbg_dump_csum(const char *comment, struct csum *cs)
405 {
406
407         if (!dbg_log)
408                 return;
409
410         fprintf(dbg_log, "===== START CYLINDER SUMMARY =====\n");
411         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)cs, comment);
412         indent++;
413
414         fprintf(dbg_log, "ndir   int32_t 0x%08x\n", cs->cs_ndir);
415         fprintf(dbg_log, "nbfree int32_t 0x%08x\n", cs->cs_nbfree);
416         fprintf(dbg_log, "nifree int32_t 0x%08x\n", cs->cs_nifree);
417         fprintf(dbg_log, "nffree int32_t 0x%08x\n", cs->cs_nffree);
418
419         indent--;
420         fprintf(dbg_log, "===== END CYLINDER SUMMARY =====\n");
421
422         return;
423 }
424
425 /*
426  * Dump a cylinder summary.
427  */
428 void
429 dbg_dump_csum_total(const char *comment, struct csum_total *cs)
430 {
431
432         if (!dbg_log)
433                 return;
434
435         fprintf(dbg_log, "===== START CYLINDER SUMMARY TOTAL =====\n");
436         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)cs, comment);
437         indent++;
438
439         fprintf(dbg_log, "ndir        int64_t 0x%08x%08x\n", 
440                 ((unsigned int *)&(cs->cs_ndir))[1],
441                 ((unsigned int *)&(cs->cs_ndir))[0]);
442         fprintf(dbg_log, "nbfree      int64_t 0x%08x%08x\n", 
443                 ((unsigned int *)&(cs->cs_nbfree))[1],
444                 ((unsigned int *)&(cs->cs_nbfree))[0]);
445         fprintf(dbg_log, "nifree      int64_t 0x%08x%08x\n", 
446                 ((unsigned int *)&(cs->cs_nifree))[1],
447                 ((unsigned int *)&(cs->cs_nifree))[0]);
448         fprintf(dbg_log, "nffree      int64_t 0x%08x%08x\n", 
449                 ((unsigned int *)&(cs->cs_nffree))[1],
450                 ((unsigned int *)&(cs->cs_nffree))[0]);
451         fprintf(dbg_log, "numclusters int64_t 0x%08x%08x\n", 
452                 ((unsigned int *)&(cs->cs_numclusters))[1],
453                 ((unsigned int *)&(cs->cs_numclusters))[0]);
454
455         indent--;
456         fprintf(dbg_log, "===== END CYLINDER SUMMARY TOTAL =====\n");
457
458         return;
459 }
460 /*
461  * Dump the inode allocation map in one cylinder group.
462  */
463 void
464 dbg_dump_inmap(struct fs *sb, const char *comment, struct cg *cgr)
465 {
466         int j,k,l,e;
467         unsigned char *cp;
468
469         if (!dbg_log)
470                 return;
471
472         fprintf(dbg_log, "===== START INODE ALLOCATION MAP =====\n");
473         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)cgr, comment);
474         indent++;
475
476         cp = (unsigned char *)cg_inosused(cgr);
477         e = sb->fs_ipg / 8;
478         for (j = 0; j < e; j += 32) {
479                 fprintf(dbg_log, "%08x: ", j);
480                 for (k = 0; k < 32; k += 8) {
481                         if (j + k + 8 < e) {
482                                 fprintf(dbg_log,
483                                     "%02x%02x%02x%02x%02x%02x%02x%02x ", 
484                                     cp[0], cp[1], cp[2], cp[3],
485                                     cp[4], cp[5], cp[6], cp[7]);
486                         } else {
487                                 for (l = 0; (l < 8) && (j + k + l < e); l++) {
488                                         fprintf(dbg_log, "%02x", cp[l]);
489                                 }
490                         }
491                         cp += 8;
492                 }
493                 fprintf(dbg_log, "\n");
494         }
495
496         indent--;
497         fprintf(dbg_log, "===== END INODE ALLOCATION MAP =====\n");
498
499         return;
500 }
501
502
503 /*
504  * Dump the fragment allocation map in one cylinder group.
505  */
506 void
507 dbg_dump_frmap(struct fs *sb, const char *comment, struct cg *cgr)
508 {
509         int j,k,l,e;
510         unsigned char *cp;
511
512         if (!dbg_log)
513                 return;
514
515         fprintf(dbg_log, "===== START FRAGMENT ALLOCATION MAP =====\n");
516         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)cgr, comment);
517         indent++;
518
519         cp = (unsigned char *)cg_blksfree(cgr);
520         if (sb->fs_old_nspf)
521                 e = howmany(sb->fs_old_cpg * sb->fs_old_spc / sb->fs_old_nspf,
522                     CHAR_BIT);
523         else
524                 e = 0;
525         for (j = 0; j < e; j += 32) {
526                 fprintf(dbg_log, "%08x: ", j);
527                 for (k = 0; k < 32; k += 8) {
528                         if (j + k + 8 <e) {
529                                 fprintf(dbg_log,
530                                     "%02x%02x%02x%02x%02x%02x%02x%02x ", 
531                                     cp[0], cp[1], cp[2], cp[3],
532                                     cp[4], cp[5], cp[6], cp[7]);
533                         } else {
534                                 for (l = 0; (l < 8) && (j + k + l < e); l++) {
535                                         fprintf(dbg_log, "%02x", cp[l]);
536                                 }
537                         }
538                         cp += 8;
539                 }
540                 fprintf(dbg_log, "\n");
541         }
542
543         indent--;
544         fprintf(dbg_log, "===== END FRAGMENT ALLOCATION MAP =====\n");
545
546         return;
547 }
548
549 /*
550  * Dump the cluster allocation map in one cylinder group.
551  */
552 void
553 dbg_dump_clmap(struct fs *sb, const char *comment, struct cg *cgr)
554 {
555         int j,k,l,e;
556         unsigned char *cp;
557
558         if (!dbg_log)
559                 return;
560
561         fprintf(dbg_log, "===== START CLUSTER ALLOCATION MAP =====\n");
562         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)cgr, comment);
563         indent++;
564
565         cp = (unsigned char *)cg_clustersfree(cgr);
566         if (sb->fs_old_nspf)
567                 e = howmany(sb->fs_old_cpg * sb->fs_old_spc / (sb->fs_old_nspf << sb->fs_fragshift), CHAR_BIT);
568         else
569                 e = 0;
570         for (j = 0; j < e; j += 32) {
571                 fprintf(dbg_log, "%08x: ", j);
572                 for (k = 0; k < 32; k += 8) {
573                         if (j + k + 8 < e) {
574                                 fprintf(dbg_log,
575                                     "%02x%02x%02x%02x%02x%02x%02x%02x ", 
576                                     cp[0], cp[1], cp[2], cp[3],
577                                     cp[4], cp[5], cp[6], cp[7]);
578                         } else {
579                                 for (l = 0; (l < 8) && (j + k + l <e); l++) {
580                                         fprintf(dbg_log, "%02x", cp[l]);
581                                 }
582                         }
583                         cp += 8;
584                 }
585                 fprintf(dbg_log, "\n");
586         }
587
588         indent--;
589         fprintf(dbg_log, "===== END CLUSTER ALLOCATION MAP =====\n");
590
591         return;
592 }
593
594 /*
595  * Dump the cluster availability summary of one cylinder group.
596  */
597 void
598 dbg_dump_clsum(struct fs *sb, const char *comment, struct cg *cgr)
599 {
600         int j;
601         int *ip;
602
603         if (!dbg_log)
604                 return;
605
606         fprintf(dbg_log, "===== START CLUSTER SUMMARY =====\n");
607         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)cgr, comment);
608         indent++;
609
610         ip = (int *)cg_clustersum(cgr);
611         for (j = 0; j <= sb->fs_contigsumsize; j++) {
612                 fprintf(dbg_log, "%02d: %8d\n", j, *ip++);
613         }
614
615         indent--;
616         fprintf(dbg_log, "===== END CLUSTER SUMMARY =====\n");
617
618         return;
619 }
620
621 #ifdef NOT_CURRENTLY
622 /*
623  * This code dates from before the UFS2 integration, and doesn't compile
624  * post-UFS2 due to the use of cg_blks().  I'm not sure how best to update
625  * this for UFS2, where the rotational bits of UFS no longer apply, so
626  * will leave it disabled for now; it should probably be re-enabled
627  * specifically for UFS1.
628  */
629 /*
630  * Dump the block summary, and the rotational layout table.
631  */
632 void
633 dbg_dump_sptbl(struct fs *sb, const char *comment, struct cg *cgr)
634 {
635         int j,k;
636         int *ip;
637
638         if (!dbg_log)
639                 return;
640
641         fprintf(dbg_log,
642             "===== START BLOCK SUMMARY AND POSITION TABLE =====\n");
643         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)cgr, comment);
644         indent++;
645
646         ip = (int *)cg_blktot(cgr);
647         for (j = 0; j < sb->fs_old_cpg; j++) {
648                 fprintf(dbg_log, "%2d: %5d = ", j, *ip++);
649                 for (k = 0; k < sb->fs_old_nrpos; k++) {
650                         fprintf(dbg_log, "%4d", cg_blks(sb, cgr, j)[k]);
651                         if (k < sb->fs_old_nrpos - 1)
652                                 fprintf(dbg_log, " + ");
653                 }
654                 fprintf(dbg_log, "\n");
655         }
656
657         indent--;
658         fprintf(dbg_log, "===== END BLOCK SUMMARY AND POSITION TABLE =====\n");
659
660         return;
661 }
662 #endif
663
664 /*
665  * Dump a UFS1 inode structure.
666  */
667 void
668 dbg_dump_ufs1_ino(struct fs *sb, const char *comment, struct ufs1_dinode *ino)
669 {
670         int ictr;
671         int remaining_blocks;
672         
673         if (!dbg_log)
674                 return;
675
676         fprintf(dbg_log, "===== START UFS1 INODE DUMP =====\n");
677         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)ino, comment);
678         indent++;
679
680         fprintf(dbg_log, "mode       u_int16_t      0%o\n", ino->di_mode);
681         fprintf(dbg_log, "nlink      int16_t        0x%04x\n", ino->di_nlink);
682         fprintf(dbg_log, "size       u_int64_t      0x%08x%08x\n", 
683             ((unsigned int *)&(ino->di_size))[1],
684             ((unsigned int *)&(ino->di_size))[0]);
685         fprintf(dbg_log, "atime      int32_t        0x%08x\n", ino->di_atime);
686         fprintf(dbg_log, "atimensec  int32_t        0x%08x\n",
687             ino->di_atimensec);
688         fprintf(dbg_log, "mtime      int32_t        0x%08x\n",
689             ino->di_mtime);
690         fprintf(dbg_log, "mtimensec  int32_t        0x%08x\n",
691             ino->di_mtimensec);
692         fprintf(dbg_log, "ctime      int32_t        0x%08x\n", ino->di_ctime);
693         fprintf(dbg_log, "ctimensec  int32_t        0x%08x\n",
694             ino->di_ctimensec);
695
696         remaining_blocks = howmany(ino->di_size, sb->fs_bsize); /* XXX ts - +1? */
697         for (ictr = 0; ictr < MIN(NDADDR, remaining_blocks); ictr++) {
698                 fprintf(dbg_log, "db         ufs_daddr_t[%x] 0x%08x\n", ictr,
699                     ino->di_db[ictr]);
700         }
701         remaining_blocks -= NDADDR;
702         if (remaining_blocks > 0) {
703                 fprintf(dbg_log, "ib         ufs_daddr_t[0] 0x%08x\n",
704                     ino->di_ib[0]);
705         }
706         remaining_blocks -= howmany(sb->fs_bsize, sizeof(ufs1_daddr_t));
707         if (remaining_blocks > 0) {
708                 fprintf(dbg_log, "ib         ufs_daddr_t[1] 0x%08x\n",
709                     ino->di_ib[1]);
710         }
711 #define SQUARE(a) ((a) * (a))
712         remaining_blocks -= SQUARE(howmany(sb->fs_bsize, sizeof(ufs1_daddr_t)));
713 #undef SQUARE
714         if (remaining_blocks > 0) {
715                 fprintf(dbg_log, "ib         ufs_daddr_t[2] 0x%08x\n",
716                     ino->di_ib[2]);
717         }
718
719         fprintf(dbg_log, "flags      u_int32_t      0x%08x\n", ino->di_flags);
720         fprintf(dbg_log, "blocks     int32_t        0x%08x\n", ino->di_blocks);
721         fprintf(dbg_log, "gen        int32_t        0x%08x\n", ino->di_gen);
722         fprintf(dbg_log, "uid        u_int32_t      0x%08x\n", ino->di_uid);
723         fprintf(dbg_log, "gid        u_int32_t      0x%08x\n", ino->di_gid);
724
725         indent--;
726         fprintf(dbg_log, "===== END UFS1 INODE DUMP =====\n");
727
728         return;
729 }
730
731 /*
732  * Dump a UFS2 inode structure.
733  */
734 void
735 dbg_dump_ufs2_ino(struct fs *sb, const char *comment, struct ufs2_dinode *ino)
736 {
737         int ictr;
738         int remaining_blocks;
739         
740         if (!dbg_log)
741                 return;
742
743         fprintf(dbg_log, "===== START UFS2 INODE DUMP =====\n");
744         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)ino, comment);
745         indent++;
746
747         fprintf(dbg_log, "mode       u_int16_t      0%o\n", ino->di_mode);
748         fprintf(dbg_log, "nlink      int16_t        0x%04x\n", ino->di_nlink);
749         fprintf(dbg_log, "uid        u_int32_t      0x%08x\n", ino->di_uid);
750         fprintf(dbg_log, "gid        u_int32_t      0x%08x\n", ino->di_gid);
751         fprintf(dbg_log, "blksize    u_int32_t      0x%08x\n", ino->di_blksize);
752         fprintf(dbg_log, "size       u_int64_t      0x%08x%08x\n", 
753             ((unsigned int *)&(ino->di_size))[1],
754             ((unsigned int *)&(ino->di_size))[0]);
755         fprintf(dbg_log, "blocks     u_int64_t      0x%08x%08x\n", 
756             ((unsigned int *)&(ino->di_blocks))[1],
757             ((unsigned int *)&(ino->di_blocks))[0]);
758         fprintf(dbg_log, "atime      ufs_time_t     %10jd\n", ino->di_atime);
759         fprintf(dbg_log, "mtime      ufs_time_t     %10jd\n", ino->di_mtime);
760         fprintf(dbg_log, "ctime      ufs_time_t     %10jd\n", ino->di_ctime);
761         fprintf(dbg_log, "birthtime  ufs_time_t     %10jd\n", ino->di_birthtime);
762         fprintf(dbg_log, "mtimensec  int32_t        0x%08x\n", ino->di_mtimensec);
763         fprintf(dbg_log, "atimensec  int32_t        0x%08x\n", ino->di_atimensec);
764         fprintf(dbg_log, "ctimensec  int32_t        0x%08x\n", ino->di_ctimensec);
765         fprintf(dbg_log, "birthnsec  int32_t        0x%08x\n", ino->di_birthnsec);
766         fprintf(dbg_log, "gen        int32_t        0x%08x\n", ino->di_gen);
767         fprintf(dbg_log, "kernflags  u_int32_t      0x%08x\n", ino->di_kernflags);
768         fprintf(dbg_log, "flags      u_int32_t      0x%08x\n", ino->di_flags);
769         fprintf(dbg_log, "extsize    u_int32_t      0x%08x\n", ino->di_extsize);
770
771         /* XXX: What do we do with di_extb[NXADDR]? */
772
773         remaining_blocks = howmany(ino->di_size, sb->fs_bsize); /* XXX ts - +1? */
774         for (ictr = 0; ictr < MIN(NDADDR, remaining_blocks); ictr++) {
775                 fprintf(dbg_log, "db         ufs2_daddr_t[%x] 0x%16jx\n", ictr,
776                     ino->di_db[ictr]);
777         }
778         remaining_blocks -= NDADDR;
779         if (remaining_blocks > 0) {
780                 fprintf(dbg_log, "ib         ufs2_daddr_t[0] 0x%16jx\n",
781                     ino->di_ib[0]);
782         }
783         remaining_blocks -= howmany(sb->fs_bsize, sizeof(ufs2_daddr_t));
784         if (remaining_blocks > 0) {
785                 fprintf(dbg_log, "ib         ufs2_daddr_t[1] 0x%16jx\n",
786                     ino->di_ib[1]);
787         }
788 #define SQUARE(a) ((a) * (a))
789         remaining_blocks -= SQUARE(howmany(sb->fs_bsize, sizeof(ufs2_daddr_t)));
790 #undef SQUARE
791         if (remaining_blocks > 0) {
792                 fprintf(dbg_log, "ib         ufs2_daddr_t[2] 0x%16jx\n",
793                     ino->di_ib[2]);
794         }
795
796         indent--;
797         fprintf(dbg_log, "===== END UFS2 INODE DUMP =====\n");
798
799         return;
800 }
801
802 /*
803  * Dump an indirect block. The iteration to dump a full file has to be
804  * written around.
805  */
806 void
807 dbg_dump_iblk(struct fs *sb, const char *comment, char *block, size_t length)
808 {
809         unsigned int *mem, i, j, size;
810
811         if (!dbg_log)
812                 return;
813
814         fprintf(dbg_log, "===== START INDIRECT BLOCK DUMP =====\n");
815         fprintf(dbg_log, "# %d@%lx: %s\n", indent, (unsigned long)block,
816             comment);
817         indent++;
818
819         if (sb->fs_magic == FS_UFS1_MAGIC)
820                 size = sizeof(ufs1_daddr_t);
821         else
822                 size = sizeof(ufs2_daddr_t);
823
824         mem = (unsigned int *)block;
825         for (i = 0; (size_t)i < MIN(howmany(sb->fs_bsize, size), length);
826             i += 8) {
827                 fprintf(dbg_log, "%04x: ", i);
828                 for (j = 0; j < 8; j++) {
829                         if ((size_t)(i + j) < length)
830                                 fprintf(dbg_log, "%08X ", *mem++);
831                 }
832                 fprintf(dbg_log, "\n");
833         }
834
835         indent--;
836         fprintf(dbg_log, "===== END INDIRECT BLOCK DUMP =====\n");
837
838         return;
839 }
840
841 #endif /* FS_DEBUG */
842