Home | History | Annotate | Line # | Download | only in lfs
lfs_debug.c revision 1.38
      1 /*	$NetBSD: lfs_debug.c,v 1.38 2009/07/19 04:16:23 dholland Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 /*
     32  * Copyright (c) 1991, 1993
     33  *	The Regents of the University of California.  All rights reserved.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. Neither the name of the University nor the names of its contributors
     44  *    may be used to endorse or promote products derived from this software
     45  *    without specific prior written permission.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57  * SUCH DAMAGE.
     58  *
     59  *	@(#)lfs_debug.c	8.1 (Berkeley) 6/11/93
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: lfs_debug.c,v 1.38 2009/07/19 04:16:23 dholland Exp $");
     64 
     65 #ifdef DEBUG
     66 
     67 #include <machine/stdarg.h>
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/namei.h>
     72 #include <sys/vnode.h>
     73 #include <sys/mount.h>
     74 #include <sys/buf.h>
     75 #include <sys/syslog.h>
     76 #include <sys/proc.h>
     77 
     78 #include <ufs/ufs/inode.h>
     79 #include <ufs/lfs/lfs.h>
     80 #include <ufs/lfs/lfs_extern.h>
     81 
     82 int lfs_lognum;
     83 struct lfs_log_entry lfs_log[LFS_LOGLENGTH];
     84 
     85 int
     86 lfs_bwrite_log(struct buf *bp, const char *file, int line)
     87 {
     88 	struct vop_bwrite_args a;
     89 
     90 	a.a_desc = VDESC(vop_bwrite);
     91 	a.a_bp = bp;
     92 
     93 	if (!(bp->b_flags & B_GATHERED) && !(bp->b_oflags & BO_DELWRI)) {
     94 		LFS_ENTER_LOG("write", file, line, bp->b_lblkno, bp->b_flags,
     95 			curproc->p_pid);
     96 	}
     97 	return (VCALL(bp->b_vp, VOFFSET(vop_bwrite), &a));
     98 }
     99 
    100 void
    101 lfs_dumplog(void)
    102 {
    103 	int i;
    104 	const char *cp;
    105 
    106 	for (i = lfs_lognum; i != (lfs_lognum - 1) % LFS_LOGLENGTH;
    107 	     i = (i + 1) % LFS_LOGLENGTH)
    108 		if (lfs_log[i].file) {
    109 			/* Only print out basename, for readability */
    110 			cp = lfs_log[i].file;
    111 			while(*cp)
    112 				++cp;
    113 			while(*cp != '/' && cp > lfs_log[i].file)
    114 				--cp;
    115 
    116 			printf("lbn %" PRId64 " %s %lx %d, %d %s\n",
    117 				lfs_log[i].block,
    118 				lfs_log[i].op,
    119 				lfs_log[i].flags,
    120 				lfs_log[i].pid,
    121 				lfs_log[i].line,
    122 				cp);
    123 		}
    124 }
    125 
    126 void
    127 lfs_dump_super(struct lfs *lfsp)
    128 {
    129 	int i;
    130 
    131 	printf("%s%x\t%s%x\t%s%d\t%s%d\n",
    132 	       "magic	 ", lfsp->lfs_magic,
    133 	       "version	 ", lfsp->lfs_version,
    134 	       "size	 ", lfsp->lfs_size,
    135 	       "ssize	 ", lfsp->lfs_ssize);
    136 	printf("%s%d\t%s%d\t%s%d\t%s%d\n",
    137 	       "dsize	 ", lfsp->lfs_dsize,
    138 	       "bsize	 ", lfsp->lfs_bsize,
    139 	       "fsize	 ", lfsp->lfs_fsize,
    140 	       "frag	 ", lfsp->lfs_frag);
    141 
    142 	printf("%s%d\t%s%d\t%s%d\t%s%d\n",
    143 	       "minfree	 ", lfsp->lfs_minfree,
    144 	       "inopb	 ", lfsp->lfs_inopb,
    145 	       "ifpb	 ", lfsp->lfs_ifpb,
    146 	       "nindir	 ", lfsp->lfs_nindir);
    147 
    148 	printf("%s%d\t%s%d\t%s%d\t%s%d\n",
    149 	       "nseg	 ", lfsp->lfs_nseg,
    150 	       "nspf	 ", lfsp->lfs_nspf,
    151 	       "cleansz	 ", lfsp->lfs_cleansz,
    152 	       "segtabsz ", lfsp->lfs_segtabsz);
    153 
    154 	printf("%s%x\t%s%d\t%s%lx\t%s%d\n",
    155 	       "segmask	 ", lfsp->lfs_segmask,
    156 	       "segshift ", lfsp->lfs_segshift,
    157 	       "bmask	 ", (unsigned long)lfsp->lfs_bmask,
    158 	       "bshift	 ", lfsp->lfs_bshift);
    159 
    160 	printf("%s%lu\t%s%d\t%s%lx\t%s%u\n",
    161 	       "ffmask	 ", (unsigned long)lfsp->lfs_ffmask,
    162 	       "ffshift	 ", lfsp->lfs_ffshift,
    163 	       "fbmask	 ", (unsigned long)lfsp->lfs_fbmask,
    164 	       "fbshift	 ", lfsp->lfs_fbshift);
    165 
    166 	printf("%s%d\t%s%d\t%s%x\t%s%qx\n",
    167 	       "sushift	 ", lfsp->lfs_sushift,
    168 	       "fsbtodb	 ", lfsp->lfs_fsbtodb,
    169 	       "cksum	 ", lfsp->lfs_cksum,
    170 	       "maxfilesize ", (long long)lfsp->lfs_maxfilesize);
    171 
    172 	printf("Superblock disk addresses:");
    173 	for (i = 0; i < LFS_MAXNUMSB; i++)
    174 		printf(" %x", lfsp->lfs_sboffs[i]);
    175 	printf("\n");
    176 
    177 	printf("Checkpoint Info\n");
    178 	printf("%s%d\t%s%x\t%s%d\n",
    179 	       "freehd	 ", lfsp->lfs_freehd,
    180 	       "idaddr	 ", lfsp->lfs_idaddr,
    181 	       "ifile	 ", lfsp->lfs_ifile);
    182 	printf("%s%x\t%s%d\t%s%x\t%s%x\t%s%x\t%s%x\n",
    183 	       "bfree	 ", lfsp->lfs_bfree,
    184 	       "nfiles	 ", lfsp->lfs_nfiles,
    185 	       "lastseg	 ", lfsp->lfs_lastseg,
    186 	       "nextseg	 ", lfsp->lfs_nextseg,
    187 	       "curseg	 ", lfsp->lfs_curseg,
    188 	       "offset	 ", lfsp->lfs_offset);
    189 	printf("tstamp	 %llx\n", (long long)lfsp->lfs_tstamp);
    190 }
    191 
    192 void
    193 lfs_dump_dinode(struct ufs1_dinode *dip)
    194 {
    195 	int i;
    196 
    197 	printf("%s%u\t%s%d\t%s%u\t%s%u\t%s%qu\t%s%d\n",
    198 	       "mode   ", dip->di_mode,
    199 	       "nlink  ", dip->di_nlink,
    200 	       "uid    ", dip->di_uid,
    201 	       "gid    ", dip->di_gid,
    202 	       "size   ", (long long)dip->di_size,
    203 	       "blocks ", dip->di_blocks);
    204 	printf("inum  %d\n", dip->di_inumber);
    205 	printf("Direct Addresses\n");
    206 	for (i = 0; i < NDADDR; i++) {
    207 		printf("\t%x", dip->di_db[i]);
    208 		if ((i % 6) == 5)
    209 			printf("\n");
    210 	}
    211 	for (i = 0; i < NIADDR; i++)
    212 		printf("\t%x", dip->di_ib[i]);
    213 	printf("\n");
    214 }
    215 
    216 void
    217 lfs_check_segsum(struct lfs *fs, struct segment *sp, char *file, int line)
    218 {
    219 	int actual;
    220 #if 0
    221 	static int offset;
    222 #endif
    223 
    224 	if ((actual = 1) == 1)
    225 		return; /* XXXX not checking this anymore, really */
    226 
    227 	if (sp->sum_bytes_left >= FINFOSIZE
    228 	   && sp->fip->fi_nblocks > 512) {
    229 		printf("%s:%d: fi_nblocks = %d\n",file,line,sp->fip->fi_nblocks);
    230 #ifdef DDB
    231 		Debugger();
    232 #endif
    233 	}
    234 
    235 	if (sp->sum_bytes_left > 484) {
    236 		printf("%s:%d: bad value (%d = -%d) for sum_bytes_left\n",
    237 		       file, line, sp->sum_bytes_left, fs->lfs_sumsize-sp->sum_bytes_left);
    238 		panic("too many bytes");
    239 	}
    240 
    241 	actual = fs->lfs_sumsize
    242 		/* amount taken up by FINFOs */
    243 		- ((char *)&(sp->fip->fi_blocks[sp->fip->fi_nblocks]) - (char *)(sp->segsum))
    244 			/* amount taken up by inode blocks */
    245 			- sizeof(int32_t)*((sp->ninodes+INOPB(fs)-1) / INOPB(fs));
    246 #if 0
    247 	if (actual - sp->sum_bytes_left < offset)
    248 	{
    249 		printf("%s:%d: offset changed %d -> %d\n", file, line,
    250 		       offset, actual-sp->sum_bytes_left);
    251 		offset = actual - sp->sum_bytes_left;
    252 		/* panic("byte mismatch"); */
    253 	}
    254 #endif
    255 #if 0
    256 	if (actual != sp->sum_bytes_left)
    257 		printf("%s:%d: warning: segsum miscalc at %d (-%d => %d)\n",
    258 		       file, line, sp->sum_bytes_left,
    259 		       fs->lfs_sumsize-sp->sum_bytes_left,
    260 		       actual);
    261 #endif
    262 	if (sp->sum_bytes_left > 0
    263 	   && ((char *)(sp->segsum))[fs->lfs_sumsize
    264 				     - sizeof(int32_t) * ((sp->ninodes+INOPB(fs)-1) / INOPB(fs))
    265 				     - sp->sum_bytes_left] != '\0') {
    266 		printf("%s:%d: warning: segsum overwrite at %d (-%d => %d)\n",
    267 		       file, line, sp->sum_bytes_left,
    268 		       fs->lfs_sumsize-sp->sum_bytes_left,
    269 		       actual);
    270 #ifdef DDB
    271 		Debugger();
    272 #endif
    273 	}
    274 }
    275 
    276 void
    277 lfs_check_bpp(struct lfs *fs, struct segment *sp, char *file, int line)
    278 {
    279 	daddr_t blkno;
    280 	struct buf **bpp;
    281 	struct vnode *devvp;
    282 
    283 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    284 	blkno = (*(sp->bpp))->b_blkno;
    285 	for (bpp = sp->bpp; bpp < sp->cbpp; bpp++) {
    286 		if ((*bpp)->b_blkno != blkno) {
    287 			if ((*bpp)->b_vp == devvp) {
    288 				printf("Oops, would misplace raw block "
    289 				       "0x%" PRIx64 " at 0x%" PRIx64 "\n",
    290 				       (*bpp)->b_blkno,
    291 				       blkno);
    292 			} else {
    293 				printf("%s:%d: misplace ino %llu lbn %" PRId64
    294 				       " at 0x%" PRIx64 " instead of "
    295 				       "0x%" PRIx64 "\n",
    296 				       file, line,
    297 				       (unsigned long long)
    298 				       VTOI((*bpp)->b_vp)->i_number,
    299 				       (*bpp)->b_lblkno,
    300 				       blkno,
    301 				       (*bpp)->b_blkno);
    302 			}
    303 		}
    304 		blkno += fsbtodb(fs, btofsb(fs, (*bpp)->b_bcount));
    305 	}
    306 }
    307 
    308 int lfs_debug_log_subsys[DLOG_MAX];
    309 
    310 /*
    311  * Log events from various debugging areas of LFS, depending on what
    312  * the user has enabled.
    313  */
    314 void
    315 lfs_debug_log(int subsys, const char *fmt, ...)
    316 {
    317 	va_list ap;
    318 
    319 	/* If not debugging this subsys, exit */
    320 	if (lfs_debug_log_subsys[subsys] == 0)
    321 		return;
    322 
    323 	va_start(ap, fmt);
    324 	vlog(LOG_DEBUG, fmt, ap);
    325 	va_end(ap);
    326 }
    327 #endif /* DEBUG */
    328