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