1*7ccd7be6Sriastradh /* $NetBSD: lfs_debug.c,v 1.55 2020/02/23 08:39:09 riastradh Exp $ */
2fccfa11aScgd
31b8f5ea3Sperseant /*-
4b397c875Sperseant * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
51b8f5ea3Sperseant * All rights reserved.
61b8f5ea3Sperseant *
71b8f5ea3Sperseant * This code is derived from software contributed to The NetBSD Foundation
81b8f5ea3Sperseant * by Konrad E. Schroder <perseant@hhhh.org>.
91b8f5ea3Sperseant *
101b8f5ea3Sperseant * Redistribution and use in source and binary forms, with or without
111b8f5ea3Sperseant * modification, are permitted provided that the following conditions
121b8f5ea3Sperseant * are met:
131b8f5ea3Sperseant * 1. Redistributions of source code must retain the above copyright
141b8f5ea3Sperseant * notice, this list of conditions and the following disclaimer.
151b8f5ea3Sperseant * 2. Redistributions in binary form must reproduce the above copyright
161b8f5ea3Sperseant * notice, this list of conditions and the following disclaimer in the
171b8f5ea3Sperseant * documentation and/or other materials provided with the distribution.
181b8f5ea3Sperseant *
191b8f5ea3Sperseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201b8f5ea3Sperseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211b8f5ea3Sperseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221b8f5ea3Sperseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231b8f5ea3Sperseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241b8f5ea3Sperseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251b8f5ea3Sperseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261b8f5ea3Sperseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271b8f5ea3Sperseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281b8f5ea3Sperseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291b8f5ea3Sperseant * POSSIBILITY OF SUCH DAMAGE.
301b8f5ea3Sperseant */
31264b874cSmycroft /*
32264b874cSmycroft * Copyright (c) 1991, 1993
33264b874cSmycroft * The Regents of the University of California. All rights reserved.
34264b874cSmycroft *
35264b874cSmycroft * Redistribution and use in source and binary forms, with or without
36264b874cSmycroft * modification, are permitted provided that the following conditions
37264b874cSmycroft * are met:
38264b874cSmycroft * 1. Redistributions of source code must retain the above copyright
39264b874cSmycroft * notice, this list of conditions and the following disclaimer.
40264b874cSmycroft * 2. Redistributions in binary form must reproduce the above copyright
41264b874cSmycroft * notice, this list of conditions and the following disclaimer in the
42264b874cSmycroft * documentation and/or other materials provided with the distribution.
43aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
44264b874cSmycroft * may be used to endorse or promote products derived from this software
45264b874cSmycroft * without specific prior written permission.
46264b874cSmycroft *
47264b874cSmycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48264b874cSmycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49264b874cSmycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50264b874cSmycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51264b874cSmycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52264b874cSmycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53264b874cSmycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54264b874cSmycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55264b874cSmycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56264b874cSmycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57264b874cSmycroft * SUCH DAMAGE.
58264b874cSmycroft *
59fccfa11aScgd * @(#)lfs_debug.c 8.1 (Berkeley) 6/11/93
60264b874cSmycroft */
61264b874cSmycroft
622b0e4faeSlukem #include <sys/cdefs.h>
63*7ccd7be6Sriastradh __KERNEL_RCSID(0, "$NetBSD: lfs_debug.c,v 1.55 2020/02/23 08:39:09 riastradh Exp $");
642b0e4faeSlukem
65264b874cSmycroft #ifdef DEBUG
66ec624546Slukem
67264b874cSmycroft #include <sys/param.h>
68e2c2c981Schristos #include <sys/systm.h>
69264b874cSmycroft #include <sys/namei.h>
70264b874cSmycroft #include <sys/vnode.h>
71264b874cSmycroft #include <sys/mount.h>
728886b0f4Sperseant #include <sys/buf.h>
73eefd94b8Sperseant #include <sys/syslog.h>
74407114c8Schristos #include <sys/proc.h>
75264b874cSmycroft
7615158895Sdholland #include <ufs/lfs/ulfs_inode.h>
77264b874cSmycroft #include <ufs/lfs/lfs.h>
7834f0d74cSdholland #include <ufs/lfs/lfs_accessors.h>
79264b874cSmycroft #include <ufs/lfs/lfs_extern.h>
80264b874cSmycroft
818886b0f4Sperseant int lfs_lognum;
828886b0f4Sperseant struct lfs_log_entry lfs_log[LFS_LOGLENGTH];
838886b0f4Sperseant
8422ba08b0Sdholland int
lfs_bwrite_log(struct buf * bp,const char * file,int line)8522ba08b0Sdholland lfs_bwrite_log(struct buf *bp, const char *file, int line)
868886b0f4Sperseant {
878886b0f4Sperseant
884a780c9aSad if (!(bp->b_flags & B_GATHERED) && !(bp->b_oflags & BO_DELWRI)) {
891ebfc508Sperseant LFS_ENTER_LOG("write", file, line, bp->b_lblkno, bp->b_flags,
901ebfc508Sperseant curproc->p_pid);
911ebfc508Sperseant }
92*7ccd7be6Sriastradh return VOP_BWRITE(bp->b_vp, bp);
938886b0f4Sperseant }
948886b0f4Sperseant
9522ba08b0Sdholland void
lfs_dumplog(void)9622ba08b0Sdholland lfs_dumplog(void)
978886b0f4Sperseant {
988886b0f4Sperseant int i;
99273df636Schristos const char *cp;
1008886b0f4Sperseant
1011ebfc508Sperseant for (i = lfs_lognum; i != (lfs_lognum - 1) % LFS_LOGLENGTH;
1021ebfc508Sperseant i = (i + 1) % LFS_LOGLENGTH)
1038886b0f4Sperseant if (lfs_log[i].file) {
1041ebfc508Sperseant /* Only print out basename, for readability */
1051ebfc508Sperseant cp = lfs_log[i].file;
1061ebfc508Sperseant while(*cp)
1071ebfc508Sperseant ++cp;
1081ebfc508Sperseant while(*cp != '/' && cp > lfs_log[i].file)
1091ebfc508Sperseant --cp;
1101ebfc508Sperseant
1111ebfc508Sperseant printf("lbn %" PRId64 " %s %lx %d, %d %s\n",
1128886b0f4Sperseant lfs_log[i].block,
1138886b0f4Sperseant lfs_log[i].op,
1148886b0f4Sperseant lfs_log[i].flags,
1151ebfc508Sperseant lfs_log[i].pid,
1168886b0f4Sperseant lfs_log[i].line,
1171ebfc508Sperseant cp);
1188886b0f4Sperseant }
1198886b0f4Sperseant }
1208886b0f4Sperseant
121264b874cSmycroft void
lfs_dump_super(struct lfs * lfsp)1224e3fced9Sperseant lfs_dump_super(struct lfs *lfsp)
123264b874cSmycroft {
124264b874cSmycroft int i;
125264b874cSmycroft
12695a8d28cSdholland printf("%s%x\t%s%x\t%s%ju\t%s%d\n",
1279e5184b8Sdholland "magic ", lfsp->lfs_is64 ?
1289e5184b8Sdholland lfsp->lfs_dlfs_u.u_64.dlfs_magic :
1299e5184b8Sdholland lfsp->lfs_dlfs_u.u_32.dlfs_magic,
130992b9a23Sdholland "version ", lfs_sb_getversion(lfsp),
13195a8d28cSdholland "size ", (uintmax_t)lfs_sb_getsize(lfsp),
1327f29db77Smartin "ssize ", lfs_sb_getssize(lfsp));
13395a8d28cSdholland printf("%s%ju\t%s%d\t%s%d\t%s%d\n",
13495a8d28cSdholland "dsize ", (uintmax_t)lfs_sb_getdsize(lfsp),
1357f29db77Smartin "bsize ", lfs_sb_getbsize(lfsp),
1367f29db77Smartin "fsize ", lfs_sb_getfsize(lfsp),
1377f29db77Smartin "frag ", lfs_sb_getfrag(lfsp));
138264b874cSmycroft
13990c7de09Schristos printf("%s%d\t%s%d\t%s%d\t%s%d\n",
1407f29db77Smartin "minfree ", lfs_sb_getminfree(lfsp),
1417f29db77Smartin "inopb ", lfs_sb_getinopb(lfsp),
1427f29db77Smartin "ifpb ", lfs_sb_getifpb(lfsp),
1437f29db77Smartin "nindir ", lfs_sb_getnindir(lfsp));
144264b874cSmycroft
14590c7de09Schristos printf("%s%d\t%s%d\t%s%d\t%s%d\n",
1467f29db77Smartin "nseg ", lfs_sb_getnseg(lfsp),
1477f29db77Smartin "nspf ", lfs_sb_getnspf(lfsp),
1487f29db77Smartin "cleansz ", lfs_sb_getcleansz(lfsp),
1497f29db77Smartin "segtabsz ", lfs_sb_getsegtabsz(lfsp));
150264b874cSmycroft
151e5bc90f4Sfvdl printf("%s%x\t%s%d\t%s%lx\t%s%d\n",
1527f29db77Smartin "segmask ", lfs_sb_getsegmask(lfsp),
1537f29db77Smartin "segshift ", lfs_sb_getsegshift(lfsp),
1547f29db77Smartin "bmask ", (unsigned long)lfs_sb_getbmask(lfsp),
1557f29db77Smartin "bshift ", lfs_sb_getbshift(lfsp));
156264b874cSmycroft
157e5bc90f4Sfvdl printf("%s%lu\t%s%d\t%s%lx\t%s%u\n",
1587f29db77Smartin "ffmask ", (unsigned long)lfs_sb_getffmask(lfsp),
1597f29db77Smartin "ffshift ", lfs_sb_getffshift(lfsp),
1607f29db77Smartin "fbmask ", (unsigned long)lfs_sb_getfbmask(lfsp),
1617f29db77Smartin "fbshift ", lfs_sb_getfbshift(lfsp));
162264b874cSmycroft
163078ffcb8Sdholland printf("%s%d\t%s%d\t%s%x\t%s%jx\n",
1647f29db77Smartin "sushift ", lfs_sb_getsushift(lfsp),
1657f29db77Smartin "fsbtodb ", lfs_sb_getfsbtodb(lfsp),
1667f29db77Smartin "cksum ", lfs_sb_getcksum(lfsp),
167078ffcb8Sdholland "maxfilesize ", (uintmax_t)lfs_sb_getmaxfilesize(lfsp));
168264b874cSmycroft
16990c7de09Schristos printf("Superblock disk addresses:");
170264b874cSmycroft for (i = 0; i < LFS_MAXNUMSB; i++)
171078ffcb8Sdholland printf(" %jx", (intmax_t)lfs_sb_getsboff(lfsp, i));
17290c7de09Schristos printf("\n");
173264b874cSmycroft
17490c7de09Schristos printf("Checkpoint Info\n");
175eb2560adSdholland printf("%s%ju\t%s%jx\n",
1764b344787Sdholland "freehd ", (uintmax_t)lfs_sb_getfreehd(lfsp),
177eb2560adSdholland "idaddr ", (intmax_t)lfs_sb_getidaddr(lfsp));
1784b344787Sdholland printf("%s%jx\t%s%ju\t%s%jx\t%s%jx\t%s%jx\t%s%jx\n",
17995a8d28cSdholland "bfree ", (intmax_t)lfs_sb_getbfree(lfsp),
1804b344787Sdholland "nfiles ", (uintmax_t)lfs_sb_getnfiles(lfsp),
181078ffcb8Sdholland "lastseg ", (intmax_t)lfs_sb_getlastseg(lfsp),
182078ffcb8Sdholland "nextseg ", (intmax_t)lfs_sb_getnextseg(lfsp),
183078ffcb8Sdholland "curseg ", (intmax_t)lfs_sb_getcurseg(lfsp),
184078ffcb8Sdholland "offset ", (intmax_t)lfs_sb_getoffset(lfsp));
1857f29db77Smartin printf("tstamp %llx\n", (long long)lfs_sb_gettstamp(lfsp));
186eb2560adSdholland
187eb2560adSdholland if (!lfsp->lfs_is64) {
188eb2560adSdholland printf("32-bit only derived or constant fields\n");
189eb2560adSdholland printf("%s%u\n",
190eb2560adSdholland "ifile ", lfs_sb_getifile(lfsp));
191eb2560adSdholland }
192264b874cSmycroft }
193264b874cSmycroft
194264b874cSmycroft void
lfs_dump_dinode(struct lfs * fs,union lfs_dinode * dip)195b1828e0bSdholland lfs_dump_dinode(struct lfs *fs, union lfs_dinode *dip)
196264b874cSmycroft {
197264b874cSmycroft int i;
198264b874cSmycroft
199b1828e0bSdholland printf("%s%u\t%s%d\t%s%u\t%s%u\t%s%ju\t%s%ju\n",
200b1828e0bSdholland "mode ", lfs_dino_getmode(fs, dip),
201b1828e0bSdholland "nlink ", lfs_dino_getnlink(fs, dip),
202b1828e0bSdholland "uid ", lfs_dino_getuid(fs, dip),
203b1828e0bSdholland "gid ", lfs_dino_getgid(fs, dip),
204b1828e0bSdholland "size ", (uintmax_t)lfs_dino_getsize(fs, dip),
205b1828e0bSdholland "blocks ", (uintmax_t)lfs_dino_getblocks(fs, dip));
206b1828e0bSdholland printf("inum %ju\n", (uintmax_t)lfs_dino_getinumber(fs, dip));
20790c7de09Schristos printf("Direct Addresses\n");
20849bebcb4Sdholland for (i = 0; i < ULFS_NDADDR; i++) {
209b1828e0bSdholland printf("\t%jx", (intmax_t)lfs_dino_getdb(fs, dip, i));
210264b874cSmycroft if ((i % 6) == 5)
21190c7de09Schristos printf("\n");
212264b874cSmycroft }
21349bebcb4Sdholland for (i = 0; i < ULFS_NIADDR; i++)
214b1828e0bSdholland printf("\t%jx", (intmax_t)lfs_dino_getib(fs, dip, i));
21590c7de09Schristos printf("\n");
216264b874cSmycroft }
2171b8f5ea3Sperseant
2181b8f5ea3Sperseant void
lfs_check_segsum(struct lfs * fs,struct segment * sp,char * file,int line)2191b8f5ea3Sperseant lfs_check_segsum(struct lfs *fs, struct segment *sp, char *file, int line)
2201b8f5ea3Sperseant {
221a2facef3Ssimonb int actual;
2221b8f5ea3Sperseant #if 0
2231b8f5ea3Sperseant static int offset;
2241b8f5ea3Sperseant #endif
2251b8f5ea3Sperseant
226a2facef3Ssimonb if ((actual = 1) == 1)
2271b8f5ea3Sperseant return; /* XXXX not checking this anymore, really */
2281b8f5ea3Sperseant
229e54b457cSdholland if (sp->sum_bytes_left >= FINFOSIZE(fs)
230e54b457cSdholland && lfs_fi_getnblocks(fs, sp->fip) > 512) {
231e54b457cSdholland printf("%s:%d: fi_nblocks = %d\n", file, line,
232e54b457cSdholland lfs_fi_getnblocks(fs, sp->fip));
2331b8f5ea3Sperseant #ifdef DDB
2341b8f5ea3Sperseant Debugger();
2351b8f5ea3Sperseant #endif
2361b8f5ea3Sperseant }
2371b8f5ea3Sperseant
2381b8f5ea3Sperseant if (sp->sum_bytes_left > 484) {
2391b8f5ea3Sperseant printf("%s:%d: bad value (%d = -%d) for sum_bytes_left\n",
2407f29db77Smartin file, line, sp->sum_bytes_left, lfs_sb_getsumsize(fs)-sp->sum_bytes_left);
2411b8f5ea3Sperseant panic("too many bytes");
2421b8f5ea3Sperseant }
2431b8f5ea3Sperseant
2447f29db77Smartin actual = lfs_sb_getsumsize(fs)
2451b8f5ea3Sperseant /* amount taken up by FINFOs */
246e54b457cSdholland - ((char *)NEXT_FINFO(fs, sp->fip) - (char *)(sp->segsum))
2471b8f5ea3Sperseant /* amount taken up by inode blocks */
248e54b457cSdholland /* XXX should this be INUMSIZE or BLKPTRSIZE? */
249e54b457cSdholland - LFS_INUMSIZE(fs)*((sp->ninodes+LFS_INOPB(fs)-1) / LFS_INOPB(fs));
2501b8f5ea3Sperseant #if 0
2511b8f5ea3Sperseant if (actual - sp->sum_bytes_left < offset)
2521b8f5ea3Sperseant {
2531b8f5ea3Sperseant printf("%s:%d: offset changed %d -> %d\n", file, line,
2541b8f5ea3Sperseant offset, actual-sp->sum_bytes_left);
2551b8f5ea3Sperseant offset = actual - sp->sum_bytes_left;
2561b8f5ea3Sperseant /* panic("byte mismatch"); */
2571b8f5ea3Sperseant }
2581b8f5ea3Sperseant #endif
2591b8f5ea3Sperseant #if 0
2601b8f5ea3Sperseant if (actual != sp->sum_bytes_left)
2611b8f5ea3Sperseant printf("%s:%d: warning: segsum miscalc at %d (-%d => %d)\n",
2621b8f5ea3Sperseant file, line, sp->sum_bytes_left,
2630df0ac53Shannken lfs_sb_getsumsize(fs)-sp->sum_bytes_left,
2641b8f5ea3Sperseant actual);
2651b8f5ea3Sperseant #endif
2661b8f5ea3Sperseant if (sp->sum_bytes_left > 0
2677f29db77Smartin && ((char *)(sp->segsum))[lfs_sb_getsumsize(fs)
268855bb66eSchristos - sizeof(int32_t) * ((sp->ninodes+LFS_INOPB(fs)-1) / LFS_INOPB(fs))
2691b8f5ea3Sperseant - sp->sum_bytes_left] != '\0') {
2701b8f5ea3Sperseant printf("%s:%d: warning: segsum overwrite at %d (-%d => %d)\n",
2711b8f5ea3Sperseant file, line, sp->sum_bytes_left,
2727f29db77Smartin lfs_sb_getsumsize(fs)-sp->sum_bytes_left,
2731b8f5ea3Sperseant actual);
2741b8f5ea3Sperseant #ifdef DDB
2751b8f5ea3Sperseant Debugger();
2761b8f5ea3Sperseant #endif
2771b8f5ea3Sperseant }
2781b8f5ea3Sperseant }
2791b8f5ea3Sperseant
2801b8f5ea3Sperseant void
lfs_check_bpp(struct lfs * fs,struct segment * sp,char * file,int line)2814e3fced9Sperseant lfs_check_bpp(struct lfs *fs, struct segment *sp, char *file, int line)
2821b8f5ea3Sperseant {
2831b8f5ea3Sperseant daddr_t blkno;
2841b8f5ea3Sperseant struct buf **bpp;
2851b8f5ea3Sperseant struct vnode *devvp;
2861b8f5ea3Sperseant
2871b8f5ea3Sperseant devvp = VTOI(fs->lfs_ivnode)->i_devvp;
2881b8f5ea3Sperseant blkno = (*(sp->bpp))->b_blkno;
2891b8f5ea3Sperseant for (bpp = sp->bpp; bpp < sp->cbpp; bpp++) {
2901b8f5ea3Sperseant if ((*bpp)->b_blkno != blkno) {
2911b8f5ea3Sperseant if ((*bpp)->b_vp == devvp) {
2924e0e5333Skleink printf("Oops, would misplace raw block "
2934e0e5333Skleink "0x%" PRIx64 " at 0x%" PRIx64 "\n",
2941b8f5ea3Sperseant (*bpp)->b_blkno,
2951b8f5ea3Sperseant blkno);
2961b8f5ea3Sperseant } else {
29750f8955bSchristos printf("%s:%d: misplace ino %llu lbn %" PRId64
2984e0e5333Skleink " at 0x%" PRIx64 " instead of "
2994e0e5333Skleink "0x%" PRIx64 "\n",
3001b8f5ea3Sperseant file, line,
30150f8955bSchristos (unsigned long long)
30250f8955bSchristos VTOI((*bpp)->b_vp)->i_number,
30350f8955bSchristos (*bpp)->b_lblkno,
3041b8f5ea3Sperseant blkno,
3051b8f5ea3Sperseant (*bpp)->b_blkno);
3061b8f5ea3Sperseant }
3071b8f5ea3Sperseant }
308855bb66eSchristos blkno += LFS_FSBTODB(fs, lfs_btofsb(fs, (*bpp)->b_bcount));
3091b8f5ea3Sperseant }
3101b8f5ea3Sperseant }
311eefd94b8Sperseant
312eefd94b8Sperseant int lfs_debug_log_subsys[DLOG_MAX];
313eefd94b8Sperseant
314eefd94b8Sperseant /*
315eefd94b8Sperseant * Log events from various debugging areas of LFS, depending on what
316eefd94b8Sperseant * the user has enabled.
317eefd94b8Sperseant */
318eefd94b8Sperseant void
lfs_debug_log(int subsys,const char * fmt,...)319eefd94b8Sperseant lfs_debug_log(int subsys, const char *fmt, ...)
320eefd94b8Sperseant {
321eefd94b8Sperseant va_list ap;
322eefd94b8Sperseant
323eefd94b8Sperseant /* If not debugging this subsys, exit */
324eefd94b8Sperseant if (lfs_debug_log_subsys[subsys] == 0)
325eefd94b8Sperseant return;
326eefd94b8Sperseant
327f2b82c7fSchristos va_start(ap, fmt);
328aca59c84Schristos vlog(LOG_DEBUG, fmt, ap);
329eefd94b8Sperseant va_end(ap);
330eefd94b8Sperseant }
331264b874cSmycroft #endif /* DEBUG */
332