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