Commit | Line | Data |
---|---|---|
da158ee3 DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
a7b24fe9 | 7 | #ifndef lint |
da158ee3 DF |
8 | char copyright[] = |
9 | "@(#) Copyright (c) 1980 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
13 | #ifndef lint | |
a66ab591 | 14 | static char sccsid[] = "@(#)dcheck.c 5.2 (Berkeley) %G%"; |
da158ee3 DF |
15 | #endif not lint |
16 | ||
e19a5dd5 KM |
17 | /* |
18 | * dcheck - check directory consistency | |
19 | */ | |
20 | #define NB 10 | |
c312eebd | 21 | #define MAXNINDIR (MAXBSIZE / sizeof (daddr_t)) |
e19a5dd5 | 22 | |
45eb394d KM |
23 | #include <sys/param.h> |
24 | #include <sys/inode.h> | |
25 | #include <sys/fs.h> | |
a7b24fe9 | 26 | #include <sys/dir.h> |
5086d197 | 27 | #include <stdio.h> |
e19a5dd5 KM |
28 | |
29 | union { | |
30 | struct fs fs; | |
b6407c9d | 31 | char pad[MAXBSIZE]; |
e19a5dd5 KM |
32 | } fsun; |
33 | #define sblock fsun.fs | |
34 | ||
5086d197 KM |
35 | struct dirstuff { |
36 | int loc; | |
37 | struct dinode *ip; | |
38 | char dbuf[MAXBSIZE]; | |
39 | }; | |
40 | ||
e19a5dd5 | 41 | struct dinode itab[MAXIPG]; |
56d45dcd | 42 | struct dinode *gip; |
e19a5dd5 KM |
43 | ino_t ilist[NB]; |
44 | ||
45 | int fi; | |
46 | ino_t ino; | |
47 | ino_t *ecount; | |
48 | int headpr; | |
49 | int nfiles; | |
a66ab591 | 50 | long dev_bsize = 1; |
e19a5dd5 KM |
51 | |
52 | int nerror; | |
53 | daddr_t bmap(); | |
54 | long atol(); | |
55 | char *malloc(); | |
56 | ||
57 | main(argc, argv) | |
58 | char *argv[]; | |
59 | { | |
60 | register i; | |
61 | long n; | |
62 | ||
63 | while (--argc) { | |
64 | argv++; | |
65 | if (**argv=='-') | |
66 | switch ((*argv)[1]) { | |
67 | ||
68 | case 'i': | |
69 | for(i=0; i<NB; i++) { | |
70 | n = atol(argv[1]); | |
71 | if(n == 0) | |
72 | break; | |
73 | ilist[i] = n; | |
74 | argv++; | |
75 | argc--; | |
76 | } | |
77 | ilist[i] = 0; | |
78 | continue; | |
79 | ||
80 | default: | |
81 | printf("Bad flag %c\n", (*argv)[1]); | |
82 | nerror++; | |
83 | } | |
84 | check(*argv); | |
85 | } | |
86 | return(nerror); | |
87 | } | |
88 | ||
89 | check(file) | |
90 | char *file; | |
91 | { | |
92 | register i, j, c; | |
93 | ||
94 | fi = open(file, 0); | |
95 | if(fi < 0) { | |
96 | printf("cannot open %s\n", file); | |
97 | nerror++; | |
98 | return; | |
99 | } | |
100 | headpr = 0; | |
101 | printf("%s:\n", file); | |
102 | sync(); | |
a66ab591 | 103 | bread(SBOFF, (char *)&sblock, SBSIZE); |
e19a5dd5 KM |
104 | if (sblock.fs_magic != FS_MAGIC) { |
105 | printf("%s: not a file system\n", file); | |
106 | nerror++; | |
107 | return; | |
108 | } | |
a66ab591 | 109 | dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); |
e19a5dd5 | 110 | nfiles = sblock.fs_ipg * sblock.fs_ncg; |
e19a5dd5 KM |
111 | ecount = (ino_t *)malloc((nfiles+1) * sizeof (*ecount)); |
112 | if (ecount == 0) { | |
113 | printf("%s: not enough core for %d files\n", file, nfiles); | |
114 | exit(04); | |
115 | } | |
116 | for (i = 0; i<=nfiles; i++) | |
117 | ecount[i] = 0; | |
118 | ino = 0; | |
119 | for (c = 0; c < sblock.fs_ncg; c++) { | |
6994bf5d | 120 | bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab, |
e19a5dd5 KM |
121 | sblock.fs_ipg * sizeof (struct dinode)); |
122 | for (j = 0; j < sblock.fs_ipg; j++) { | |
123 | pass1(&itab[j]); | |
124 | ino++; | |
125 | } | |
126 | } | |
127 | ino = 0; | |
128 | for (c = 0; c < sblock.fs_ncg; c++) { | |
6994bf5d | 129 | bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab, |
e19a5dd5 KM |
130 | sblock.fs_ipg * sizeof (struct dinode)); |
131 | for (j = 0; j < sblock.fs_ipg; j++) { | |
132 | pass2(&itab[j]); | |
133 | ino++; | |
134 | } | |
135 | } | |
136 | free(ecount); | |
137 | } | |
138 | ||
139 | pass1(ip) | |
5086d197 | 140 | register struct dinode *ip; |
e19a5dd5 | 141 | { |
5086d197 KM |
142 | register struct direct *dp; |
143 | struct dirstuff dirp; | |
e19a5dd5 | 144 | int k; |
e19a5dd5 KM |
145 | |
146 | if((ip->di_mode&IFMT) != IFDIR) | |
147 | return; | |
5086d197 KM |
148 | dirp.loc = 0; |
149 | dirp.ip = ip; | |
56d45dcd | 150 | gip = ip; |
5086d197 KM |
151 | for (dp = readdir(&dirp); dp != NULL; dp = readdir(&dirp)) { |
152 | if(dp->d_ino == 0) | |
153 | continue; | |
154 | if(dp->d_ino > nfiles || dp->d_ino < ROOTINO) { | |
155 | printf("%d bad; %d/%s\n", | |
156 | dp->d_ino, ino, dp->d_name); | |
157 | nerror++; | |
158 | continue; | |
159 | } | |
160 | for (k = 0; ilist[k] != 0; k++) | |
161 | if (ilist[k] == dp->d_ino) { | |
162 | printf("%d arg; %d/%s\n", | |
163 | dp->d_ino, ino, dp->d_name); | |
e19a5dd5 | 164 | nerror++; |
e19a5dd5 | 165 | } |
5086d197 | 166 | ecount[dp->d_ino]++; |
e19a5dd5 KM |
167 | } |
168 | } | |
169 | ||
170 | pass2(ip) | |
171 | register struct dinode *ip; | |
172 | { | |
173 | register i; | |
174 | ||
175 | i = ino; | |
176 | if ((ip->di_mode&IFMT)==0 && ecount[i]==0) | |
177 | return; | |
178 | if (ip->di_nlink==ecount[i] && ip->di_nlink!=0) | |
179 | return; | |
180 | if (headpr==0) { | |
181 | printf(" entries link cnt\n"); | |
182 | headpr++; | |
183 | } | |
184 | printf("%u\t%d\t%d\n", ino, | |
185 | ecount[i], ip->di_nlink); | |
186 | } | |
187 | ||
5086d197 KM |
188 | /* |
189 | * get next entry in a directory. | |
190 | */ | |
191 | struct direct * | |
192 | readdir(dirp) | |
193 | register struct dirstuff *dirp; | |
194 | { | |
195 | register struct direct *dp; | |
196 | daddr_t lbn, d; | |
197 | ||
198 | for(;;) { | |
199 | if (dirp->loc >= dirp->ip->di_size) | |
200 | return NULL; | |
3352e84a | 201 | if ((lbn = lblkno(&sblock, dirp->loc)) == 0) { |
5086d197 KM |
202 | d = bmap(lbn); |
203 | if(d == 0) | |
204 | return NULL; | |
205 | bread(fsbtodb(&sblock, d), dirp->dbuf, | |
206 | dblksize(&sblock, dirp->ip, lbn)); | |
207 | } | |
208 | dp = (struct direct *) | |
3352e84a | 209 | (dirp->dbuf + blkoff(&sblock, dirp->loc)); |
5086d197 KM |
210 | dirp->loc += dp->d_reclen; |
211 | if (dp->d_ino == 0) | |
212 | continue; | |
213 | return (dp); | |
214 | } | |
215 | } | |
216 | ||
e19a5dd5 KM |
217 | bread(bno, buf, cnt) |
218 | daddr_t bno; | |
219 | char *buf; | |
220 | { | |
221 | register i; | |
222 | ||
a66ab591 | 223 | lseek(fi, bno * dev_bsize, 0); |
e19a5dd5 KM |
224 | if (read(fi, buf, cnt) != cnt) { |
225 | printf("read error %d\n", bno); | |
b6407c9d | 226 | for(i=0; i < cnt; i++) |
e19a5dd5 KM |
227 | buf[i] = 0; |
228 | } | |
229 | } | |
230 | ||
231 | daddr_t | |
232 | bmap(i) | |
233 | { | |
b6407c9d | 234 | daddr_t ibuf[MAXNINDIR]; |
e19a5dd5 KM |
235 | |
236 | if(i < NDADDR) | |
56d45dcd | 237 | return(gip->di_db[i]); |
e19a5dd5 | 238 | i -= NDADDR; |
b6407c9d | 239 | if(i > NINDIR(&sblock)) { |
e19a5dd5 KM |
240 | printf("%u - huge directory\n", ino); |
241 | return((daddr_t)0); | |
242 | } | |
b6407c9d | 243 | bread(fsbtodb(&sblock, gip->di_ib[0]), (char *)ibuf, sizeof(ibuf)); |
e19a5dd5 KM |
244 | return(ibuf[i]); |
245 | } |