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@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 <sys/param.h>
68#include <sys/systm.h>
69#include <sys/namei.h>
70#include <sys/vnode.h>
71#include <sys/mount.h>
72#include <sys/buf.h>
73#include <sys/syslog.h>
74#include <sys/proc.h>
75
76#include <ufs/ufs/inode.h>
77#include <ufs/lfs/lfs.h>
78#include <ufs/lfs/lfs_extern.h>
79
80int lfs_lognum;
81struct lfs_log_entry lfs_log[LFS_LOGLENGTH];
82
83int
84lfs_bwrite_log(struct buf *bp, const char *file, int line)
85{
86	struct vop_bwrite_args a;
87
88	a.a_desc = VDESC(vop_bwrite);
89	a.a_bp = bp;
90
91	if (!(bp->b_flags & B_GATHERED) && !(bp->b_oflags & BO_DELWRI)) {
92		LFS_ENTER_LOG("write", file, line, bp->b_lblkno, bp->b_flags,
93			curproc->p_pid);
94	}
95	return (VCALL(bp->b_vp, VOFFSET(vop_bwrite), &a));
96}
97
98void
99lfs_dumplog(void)
100{
101	int i;
102	const char *cp;
103
104	for (i = lfs_lognum; i != (lfs_lognum - 1) % LFS_LOGLENGTH;
105	     i = (i + 1) % LFS_LOGLENGTH)
106		if (lfs_log[i].file) {
107			/* Only print out basename, for readability */
108			cp = lfs_log[i].file;
109			while(*cp)
110				++cp;
111			while(*cp != '/' && cp > lfs_log[i].file)
112				--cp;
113
114			printf("lbn %" PRId64 " %s %lx %d, %d %s\n",
115				lfs_log[i].block,
116				lfs_log[i].op,
117				lfs_log[i].flags,
118				lfs_log[i].pid,
119				lfs_log[i].line,
120				cp);
121		}
122}
123
124void
125lfs_dump_super(struct lfs *lfsp)
126{
127	int i;
128
129	printf("%s%x\t%s%x\t%s%d\t%s%d\n",
130	       "magic	 ", lfsp->lfs_magic,
131	       "version	 ", lfsp->lfs_version,
132	       "size	 ", lfsp->lfs_size,
133	       "ssize	 ", lfsp->lfs_ssize);
134	printf("%s%d\t%s%d\t%s%d\t%s%d\n",
135	       "dsize	 ", lfsp->lfs_dsize,
136	       "bsize	 ", lfsp->lfs_bsize,
137	       "fsize	 ", lfsp->lfs_fsize,
138	       "frag	 ", lfsp->lfs_frag);
139
140	printf("%s%d\t%s%d\t%s%d\t%s%d\n",
141	       "minfree	 ", lfsp->lfs_minfree,
142	       "inopb	 ", lfsp->lfs_inopb,
143	       "ifpb	 ", lfsp->lfs_ifpb,
144	       "nindir	 ", lfsp->lfs_nindir);
145
146	printf("%s%d\t%s%d\t%s%d\t%s%d\n",
147	       "nseg	 ", lfsp->lfs_nseg,
148	       "nspf	 ", lfsp->lfs_nspf,
149	       "cleansz	 ", lfsp->lfs_cleansz,
150	       "segtabsz ", lfsp->lfs_segtabsz);
151
152	printf("%s%x\t%s%d\t%s%lx\t%s%d\n",
153	       "segmask	 ", lfsp->lfs_segmask,
154	       "segshift ", lfsp->lfs_segshift,
155	       "bmask	 ", (unsigned long)lfsp->lfs_bmask,
156	       "bshift	 ", lfsp->lfs_bshift);
157
158	printf("%s%lu\t%s%d\t%s%lx\t%s%u\n",
159	       "ffmask	 ", (unsigned long)lfsp->lfs_ffmask,
160	       "ffshift	 ", lfsp->lfs_ffshift,
161	       "fbmask	 ", (unsigned long)lfsp->lfs_fbmask,
162	       "fbshift	 ", lfsp->lfs_fbshift);
163
164	printf("%s%d\t%s%d\t%s%x\t%s%qx\n",
165	       "sushift	 ", lfsp->lfs_sushift,
166	       "fsbtodb	 ", lfsp->lfs_fsbtodb,
167	       "cksum	 ", lfsp->lfs_cksum,
168	       "maxfilesize ", (long long)lfsp->lfs_maxfilesize);
169
170	printf("Superblock disk addresses:");
171	for (i = 0; i < LFS_MAXNUMSB; i++)
172		printf(" %x", lfsp->lfs_sboffs[i]);
173	printf("\n");
174
175	printf("Checkpoint Info\n");
176	printf("%s%d\t%s%x\t%s%d\n",
177	       "freehd	 ", lfsp->lfs_freehd,
178	       "idaddr	 ", lfsp->lfs_idaddr,
179	       "ifile	 ", lfsp->lfs_ifile);
180	printf("%s%x\t%s%d\t%s%x\t%s%x\t%s%x\t%s%x\n",
181	       "bfree	 ", lfsp->lfs_bfree,
182	       "nfiles	 ", lfsp->lfs_nfiles,
183	       "lastseg	 ", lfsp->lfs_lastseg,
184	       "nextseg	 ", lfsp->lfs_nextseg,
185	       "curseg	 ", lfsp->lfs_curseg,
186	       "offset	 ", lfsp->lfs_offset);
187	printf("tstamp	 %llx\n", (long long)lfsp->lfs_tstamp);
188}
189
190void
191lfs_dump_dinode(struct ufs1_dinode *dip)
192{
193	int i;
194
195	printf("%s%u\t%s%d\t%s%u\t%s%u\t%s%qu\t%s%d\n",
196	       "mode   ", dip->di_mode,
197	       "nlink  ", dip->di_nlink,
198	       "uid    ", dip->di_uid,
199	       "gid    ", dip->di_gid,
200	       "size   ", (long long)dip->di_size,
201	       "blocks ", dip->di_blocks);
202	printf("inum  %d\n", dip->di_inumber);
203	printf("Direct Addresses\n");
204	for (i = 0; i < NDADDR; i++) {
205		printf("\t%x", dip->di_db[i]);
206		if ((i % 6) == 5)
207			printf("\n");
208	}
209	for (i = 0; i < NIADDR; i++)
210		printf("\t%x", dip->di_ib[i]);
211	printf("\n");
212}
213
214void
215lfs_check_segsum(struct lfs *fs, struct segment *sp, char *file, int line)
216{
217	int actual;
218#if 0
219	static int offset;
220#endif
221
222	if ((actual = 1) == 1)
223		return; /* XXXX not checking this anymore, really */
224
225	if (sp->sum_bytes_left >= FINFOSIZE
226	   && sp->fip->fi_nblocks > 512) {
227		printf("%s:%d: fi_nblocks = %d\n",file,line,sp->fip->fi_nblocks);
228#ifdef DDB
229		Debugger();
230#endif
231	}
232
233	if (sp->sum_bytes_left > 484) {
234		printf("%s:%d: bad value (%d = -%d) for sum_bytes_left\n",
235		       file, line, sp->sum_bytes_left, fs->lfs_sumsize-sp->sum_bytes_left);
236		panic("too many bytes");
237	}
238
239	actual = fs->lfs_sumsize
240		/* amount taken up by FINFOs */
241		- ((char *)&(sp->fip->fi_blocks[sp->fip->fi_nblocks]) - (char *)(sp->segsum))
242			/* amount taken up by inode blocks */
243			- sizeof(int32_t)*((sp->ninodes+INOPB(fs)-1) / INOPB(fs));
244#if 0
245	if (actual - sp->sum_bytes_left < offset)
246	{
247		printf("%s:%d: offset changed %d -> %d\n", file, line,
248		       offset, actual-sp->sum_bytes_left);
249		offset = actual - sp->sum_bytes_left;
250		/* panic("byte mismatch"); */
251	}
252#endif
253#if 0
254	if (actual != sp->sum_bytes_left)
255		printf("%s:%d: warning: segsum miscalc at %d (-%d => %d)\n",
256		       file, line, sp->sum_bytes_left,
257		       fs->lfs_sumsize-sp->sum_bytes_left,
258		       actual);
259#endif
260	if (sp->sum_bytes_left > 0
261	   && ((char *)(sp->segsum))[fs->lfs_sumsize
262				     - sizeof(int32_t) * ((sp->ninodes+INOPB(fs)-1) / INOPB(fs))
263				     - sp->sum_bytes_left] != '\0') {
264		printf("%s:%d: warning: segsum overwrite at %d (-%d => %d)\n",
265		       file, line, sp->sum_bytes_left,
266		       fs->lfs_sumsize-sp->sum_bytes_left,
267		       actual);
268#ifdef DDB
269		Debugger();
270#endif
271	}
272}
273
274void
275lfs_check_bpp(struct lfs *fs, struct segment *sp, char *file, int line)
276{
277	daddr_t blkno;
278	struct buf **bpp;
279	struct vnode *devvp;
280
281	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
282	blkno = (*(sp->bpp))->b_blkno;
283	for (bpp = sp->bpp; bpp < sp->cbpp; bpp++) {
284		if ((*bpp)->b_blkno != blkno) {
285			if ((*bpp)->b_vp == devvp) {
286				printf("Oops, would misplace raw block "
287				       "0x%" PRIx64 " at 0x%" PRIx64 "\n",
288				       (*bpp)->b_blkno,
289				       blkno);
290			} else {
291				printf("%s:%d: misplace ino %llu lbn %" PRId64
292				       " at 0x%" PRIx64 " instead of "
293				       "0x%" PRIx64 "\n",
294				       file, line,
295				       (unsigned long long)
296				       VTOI((*bpp)->b_vp)->i_number,
297				       (*bpp)->b_lblkno,
298				       blkno,
299				       (*bpp)->b_blkno);
300			}
301		}
302		blkno += fsbtodb(fs, btofsb(fs, (*bpp)->b_bcount));
303	}
304}
305
306int lfs_debug_log_subsys[DLOG_MAX];
307
308/*
309 * Log events from various debugging areas of LFS, depending on what
310 * the user has enabled.
311 */
312void
313lfs_debug_log(int subsys, const char *fmt, ...)
314{
315	va_list ap;
316
317	/* If not debugging this subsys, exit */
318	if (lfs_debug_log_subsys[subsys] == 0)
319		return;
320
321	va_start(ap, fmt);
322	vlog(LOG_DEBUG, fmt, ap);
323	va_end(ap);
324}
325#endif /* DEBUG */
326