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