1 /*        $NetBSD: lfs_debug.c,v 1.55 2020/02/23 08:39:09 riastradh 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.55 2020/02/23 08:39:09 riastradh 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/lfs/ulfs_inode.h>
77 #include <ufs/lfs/lfs.h>
78 #include <ufs/lfs/lfs_accessors.h>
79 #include <ufs/lfs/lfs_extern.h>
80 
81 int lfs_lognum;
82 struct lfs_log_entry lfs_log[LFS_LOGLENGTH];
83 
84 int
lfs_bwrite_log(struct buf * bp,const char * file,int line)85 lfs_bwrite_log(struct buf *bp, const char *file, int line)
86 {
87 
88           if (!(bp->b_flags & B_GATHERED) && !(bp->b_oflags & BO_DELWRI)) {
89                     LFS_ENTER_LOG("write", file, line, bp->b_lblkno, bp->b_flags,
90                               curproc->p_pid);
91           }
92           return VOP_BWRITE(bp->b_vp, bp);
93 }
94 
95 void
lfs_dumplog(void)96 lfs_dumplog(void)
97 {
98           int i;
99           const char *cp;
100 
101           for (i = lfs_lognum; i != (lfs_lognum - 1) % LFS_LOGLENGTH;
102                i = (i + 1) % LFS_LOGLENGTH)
103                     if (lfs_log[i].file) {
104                               /* Only print out basename, for readability */
105                               cp = lfs_log[i].file;
106                               while(*cp)
107                                         ++cp;
108                               while(*cp != '/' && cp > lfs_log[i].file)
109                                         --cp;
110 
111                               printf("lbn %" PRId64 " %s %lx %d, %d %s\n",
112                                         lfs_log[i].block,
113                                         lfs_log[i].op,
114                                         lfs_log[i].flags,
115                                         lfs_log[i].pid,
116                                         lfs_log[i].line,
117                                         cp);
118                     }
119 }
120 
121 void
lfs_dump_super(struct lfs * lfsp)122 lfs_dump_super(struct lfs *lfsp)
123 {
124           int i;
125 
126           printf("%s%x\t%s%x\t%s%ju\t%s%d\n",
127                  "magic        ", lfsp->lfs_is64 ?
128                               lfsp->lfs_dlfs_u.u_64.dlfs_magic :
129                               lfsp->lfs_dlfs_u.u_32.dlfs_magic,
130                  "version      ", lfs_sb_getversion(lfsp),
131                  "size         ", (uintmax_t)lfs_sb_getsize(lfsp),
132                  "ssize        ", lfs_sb_getssize(lfsp));
133           printf("%s%ju\t%s%d\t%s%d\t%s%d\n",
134                  "dsize        ", (uintmax_t)lfs_sb_getdsize(lfsp),
135                  "bsize        ", lfs_sb_getbsize(lfsp),
136                  "fsize        ", lfs_sb_getfsize(lfsp),
137                  "frag         ", lfs_sb_getfrag(lfsp));
138 
139           printf("%s%d\t%s%d\t%s%d\t%s%d\n",
140                  "minfree      ", lfs_sb_getminfree(lfsp),
141                  "inopb        ", lfs_sb_getinopb(lfsp),
142                  "ifpb         ", lfs_sb_getifpb(lfsp),
143                  "nindir       ", lfs_sb_getnindir(lfsp));
144 
145           printf("%s%d\t%s%d\t%s%d\t%s%d\n",
146                  "nseg         ", lfs_sb_getnseg(lfsp),
147                  "nspf         ", lfs_sb_getnspf(lfsp),
148                  "cleansz      ", lfs_sb_getcleansz(lfsp),
149                  "segtabsz ", lfs_sb_getsegtabsz(lfsp));
150 
151           printf("%s%x\t%s%d\t%s%lx\t%s%d\n",
152                  "segmask      ", lfs_sb_getsegmask(lfsp),
153                  "segshift ", lfs_sb_getsegshift(lfsp),
154                  "bmask        ", (unsigned long)lfs_sb_getbmask(lfsp),
155                  "bshift       ", lfs_sb_getbshift(lfsp));
156 
157           printf("%s%lu\t%s%d\t%s%lx\t%s%u\n",
158                  "ffmask       ", (unsigned long)lfs_sb_getffmask(lfsp),
159                  "ffshift      ", lfs_sb_getffshift(lfsp),
160                  "fbmask       ", (unsigned long)lfs_sb_getfbmask(lfsp),
161                  "fbshift      ", lfs_sb_getfbshift(lfsp));
162 
163           printf("%s%d\t%s%d\t%s%x\t%s%jx\n",
164                  "sushift      ", lfs_sb_getsushift(lfsp),
165                  "fsbtodb      ", lfs_sb_getfsbtodb(lfsp),
166                  "cksum        ", lfs_sb_getcksum(lfsp),
167                  "maxfilesize ", (uintmax_t)lfs_sb_getmaxfilesize(lfsp));
168 
169           printf("Superblock disk addresses:");
170           for (i = 0; i < LFS_MAXNUMSB; i++)
171                     printf(" %jx", (intmax_t)lfs_sb_getsboff(lfsp, i));
172           printf("\n");
173 
174           printf("Checkpoint Info\n");
175           printf("%s%ju\t%s%jx\n",
176                  "freehd       ", (uintmax_t)lfs_sb_getfreehd(lfsp),
177                  "idaddr       ", (intmax_t)lfs_sb_getidaddr(lfsp));
178           printf("%s%jx\t%s%ju\t%s%jx\t%s%jx\t%s%jx\t%s%jx\n",
179                  "bfree        ", (intmax_t)lfs_sb_getbfree(lfsp),
180                  "nfiles       ", (uintmax_t)lfs_sb_getnfiles(lfsp),
181                  "lastseg      ", (intmax_t)lfs_sb_getlastseg(lfsp),
182                  "nextseg      ", (intmax_t)lfs_sb_getnextseg(lfsp),
183                  "curseg       ", (intmax_t)lfs_sb_getcurseg(lfsp),
184                  "offset       ", (intmax_t)lfs_sb_getoffset(lfsp));
185           printf("tstamp       %llx\n", (long long)lfs_sb_gettstamp(lfsp));
186 
187           if (!lfsp->lfs_is64) {
188                     printf("32-bit only derived or constant fields\n");
189                     printf("%s%u\n",
190                            "ifile        ", lfs_sb_getifile(lfsp));
191           }
192 }
193 
194 void
lfs_dump_dinode(struct lfs * fs,union lfs_dinode * dip)195 lfs_dump_dinode(struct lfs *fs, union lfs_dinode *dip)
196 {
197           int i;
198 
199           printf("%s%u\t%s%d\t%s%u\t%s%u\t%s%ju\t%s%ju\n",
200                  "mode   ", lfs_dino_getmode(fs, dip),
201                  "nlink  ", lfs_dino_getnlink(fs, dip),
202                  "uid    ", lfs_dino_getuid(fs, dip),
203                  "gid    ", lfs_dino_getgid(fs, dip),
204                  "size   ", (uintmax_t)lfs_dino_getsize(fs, dip),
205                  "blocks ", (uintmax_t)lfs_dino_getblocks(fs, dip));
206           printf("inum  %ju\n", (uintmax_t)lfs_dino_getinumber(fs, dip));
207           printf("Direct Addresses\n");
208           for (i = 0; i < ULFS_NDADDR; i++) {
209                     printf("\t%jx", (intmax_t)lfs_dino_getdb(fs, dip, i));
210                     if ((i % 6) == 5)
211                               printf("\n");
212           }
213           for (i = 0; i < ULFS_NIADDR; i++)
214                     printf("\t%jx", (intmax_t)lfs_dino_getib(fs, dip, i));
215           printf("\n");
216 }
217 
218 void
lfs_check_segsum(struct lfs * fs,struct segment * sp,char * file,int line)219 lfs_check_segsum(struct lfs *fs, struct segment *sp, char *file, int line)
220 {
221           int actual;
222 #if 0
223           static int offset;
224 #endif
225 
226           if ((actual = 1) == 1)
227                     return; /* XXXX not checking this anymore, really */
228 
229           if (sp->sum_bytes_left >= FINFOSIZE(fs)
230              && lfs_fi_getnblocks(fs, sp->fip) > 512) {
231                     printf("%s:%d: fi_nblocks = %d\n", file, line,
232                            lfs_fi_getnblocks(fs, sp->fip));
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, lfs_sb_getsumsize(fs)-sp->sum_bytes_left);
241                     panic("too many bytes");
242           }
243 
244           actual = lfs_sb_getsumsize(fs)
245                     /* amount taken up by FINFOs */
246                     - ((char *)NEXT_FINFO(fs, sp->fip) - (char *)(sp->segsum))
247                               /* amount taken up by inode blocks */
248                               /* XXX should this be INUMSIZE or BLKPTRSIZE? */
249                               - LFS_INUMSIZE(fs)*((sp->ninodes+LFS_INOPB(fs)-1) / LFS_INOPB(fs));
250 #if 0
251           if (actual - sp->sum_bytes_left < offset)
252           {
253                     printf("%s:%d: offset changed %d -> %d\n", file, line,
254                            offset, actual-sp->sum_bytes_left);
255                     offset = actual - sp->sum_bytes_left;
256                     /* panic("byte mismatch"); */
257           }
258 #endif
259 #if 0
260           if (actual != sp->sum_bytes_left)
261                     printf("%s:%d: warning: segsum miscalc at %d (-%d => %d)\n",
262                            file, line, sp->sum_bytes_left,
263                            lfs_sb_getsumsize(fs)-sp->sum_bytes_left,
264                            actual);
265 #endif
266           if (sp->sum_bytes_left > 0
267              && ((char *)(sp->segsum))[lfs_sb_getsumsize(fs)
268                                              - sizeof(int32_t) * ((sp->ninodes+LFS_INOPB(fs)-1) / LFS_INOPB(fs))
269                                              - sp->sum_bytes_left] != '\0') {
270                     printf("%s:%d: warning: segsum overwrite at %d (-%d => %d)\n",
271                            file, line, sp->sum_bytes_left,
272                            lfs_sb_getsumsize(fs)-sp->sum_bytes_left,
273                            actual);
274 #ifdef DDB
275                     Debugger();
276 #endif
277           }
278 }
279 
280 void
lfs_check_bpp(struct lfs * fs,struct segment * sp,char * file,int line)281 lfs_check_bpp(struct lfs *fs, struct segment *sp, char *file, int line)
282 {
283           daddr_t blkno;
284           struct buf **bpp;
285           struct vnode *devvp;
286 
287           devvp = VTOI(fs->lfs_ivnode)->i_devvp;
288           blkno = (*(sp->bpp))->b_blkno;
289           for (bpp = sp->bpp; bpp < sp->cbpp; bpp++) {
290                     if ((*bpp)->b_blkno != blkno) {
291                               if ((*bpp)->b_vp == devvp) {
292                                         printf("Oops, would misplace raw block "
293                                                "0x%" PRIx64 " at 0x%" PRIx64 "\n",
294                                                (*bpp)->b_blkno,
295                                                blkno);
296                               } else {
297                                         printf("%s:%d: misplace ino %llu lbn %" PRId64
298                                                " at 0x%" PRIx64 " instead of "
299                                                "0x%" PRIx64 "\n",
300                                                file, line,
301                                                (unsigned long long)
302                                                VTOI((*bpp)->b_vp)->i_number,
303                                                (*bpp)->b_lblkno,
304                                                blkno,
305                                                (*bpp)->b_blkno);
306                               }
307                     }
308                     blkno += LFS_FSBTODB(fs, lfs_btofsb(fs, (*bpp)->b_bcount));
309           }
310 }
311 
312 int lfs_debug_log_subsys[DLOG_MAX];
313 
314 /*
315  * Log events from various debugging areas of LFS, depending on what
316  * the user has enabled.
317  */
318 void
lfs_debug_log(int subsys,const char * fmt,...)319 lfs_debug_log(int subsys, const char *fmt, ...)
320 {
321           va_list ap;
322 
323           /* If not debugging this subsys, exit */
324           if (lfs_debug_log_subsys[subsys] == 0)
325                     return;
326 
327           va_start(ap, fmt);
328           vlog(LOG_DEBUG, fmt, ap);
329           va_end(ap);
330 }
331 #endif /* DEBUG */
332