pass2.c revision 1.33 1 1.33 fvdl /* $NetBSD: pass2.c,v 1.33 2003/04/02 10:39:26 fvdl Exp $ */
2 1.12 cgd
3 1.1 cgd /*
4 1.6 mycroft * Copyright (c) 1980, 1986, 1993
5 1.6 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.18 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.12 cgd #if 0
39 1.20 lukem static char sccsid[] = "@(#)pass2.c 8.9 (Berkeley) 4/28/95";
40 1.12 cgd #else
41 1.33 fvdl __RCSID("$NetBSD: pass2.c,v 1.33 2003/04/02 10:39:26 fvdl Exp $");
42 1.12 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.5 cgd #include <sys/time.h>
47 1.20 lukem
48 1.6 mycroft #include <ufs/ufs/dinode.h>
49 1.6 mycroft #include <ufs/ufs/dir.h>
50 1.6 mycroft #include <ufs/ffs/fs.h>
51 1.9 cgd
52 1.20 lukem #include <err.h>
53 1.21 lukem #include <stdio.h>
54 1.21 lukem #include <stdlib.h>
55 1.1 cgd #include <string.h>
56 1.16 christos
57 1.1 cgd #include "fsck.h"
58 1.17 christos #include "fsutil.h"
59 1.9 cgd #include "extern.h"
60 1.1 cgd
61 1.1 cgd #define MINDIRSIZE (sizeof (struct dirtemplate))
62 1.1 cgd
63 1.20 lukem static int blksort __P((const void *, const void *));
64 1.16 christos static int pass2check __P((struct inodesc *));
65 1.1 cgd
66 1.9 cgd void
67 1.1 cgd pass2()
68 1.1 cgd {
69 1.33 fvdl union dinode *dp;
70 1.29 mycroft struct inoinfo **inpp, *inp, *pinp;
71 1.1 cgd struct inoinfo **inpend;
72 1.33 fvdl struct inostat *rinfo, *info;
73 1.1 cgd struct inodesc curino;
74 1.33 fvdl union dinode dino;
75 1.33 fvdl int i, maxblk;
76 1.1 cgd char pathbuf[MAXPATHLEN + 1];
77 1.1 cgd
78 1.33 fvdl rinfo = inoinfo(ROOTINO);
79 1.33 fvdl switch (rinfo->ino_state) {
80 1.1 cgd
81 1.1 cgd case USTATE:
82 1.1 cgd pfatal("ROOT INODE UNALLOCATED");
83 1.26 fvdl if (reply("ALLOCATE") == 0) {
84 1.26 fvdl markclean = 0;
85 1.26 fvdl ckfini();
86 1.20 lukem exit(EEXIT);
87 1.26 fvdl }
88 1.1 cgd if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
89 1.20 lukem errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
90 1.1 cgd break;
91 1.1 cgd
92 1.1 cgd case DCLEAR:
93 1.1 cgd pfatal("DUPS/BAD IN ROOT INODE");
94 1.1 cgd if (reply("REALLOCATE")) {
95 1.1 cgd freeino(ROOTINO);
96 1.1 cgd if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
97 1.20 lukem errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
98 1.1 cgd break;
99 1.1 cgd }
100 1.26 fvdl markclean = 0;
101 1.26 fvdl if (reply("CONTINUE") == 0) {
102 1.26 fvdl ckfini();
103 1.20 lukem exit(EEXIT);
104 1.26 fvdl }
105 1.1 cgd break;
106 1.1 cgd
107 1.1 cgd case FSTATE:
108 1.1 cgd case FCLEAR:
109 1.1 cgd pfatal("ROOT INODE NOT DIRECTORY");
110 1.1 cgd if (reply("REALLOCATE")) {
111 1.1 cgd freeino(ROOTINO);
112 1.1 cgd if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
113 1.20 lukem errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
114 1.1 cgd break;
115 1.1 cgd }
116 1.26 fvdl if (reply("FIX") == 0) {
117 1.26 fvdl markclean = 0;
118 1.26 fvdl ckfini();
119 1.20 lukem exit(EEXIT);
120 1.26 fvdl }
121 1.1 cgd dp = ginode(ROOTINO);
122 1.33 fvdl DIP(dp, mode) =
123 1.33 fvdl iswap16((iswap16(DIP(dp, mode)) & ~IFMT) | IFDIR);
124 1.1 cgd inodirty();
125 1.1 cgd break;
126 1.1 cgd
127 1.1 cgd case DSTATE:
128 1.1 cgd break;
129 1.1 cgd
130 1.1 cgd default:
131 1.33 fvdl errx(EEXIT, "BAD STATE %d FOR ROOT INODE", rinfo->ino_state);
132 1.1 cgd }
133 1.10 mycroft if (newinofmt) {
134 1.33 fvdl info = inoinfo(WINO);
135 1.33 fvdl info->ino_state = FSTATE;
136 1.33 fvdl info->ino_type = DT_WHT;
137 1.10 mycroft }
138 1.1 cgd /*
139 1.1 cgd * Sort the directory list into disk block order.
140 1.1 cgd */
141 1.1 cgd qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
142 1.1 cgd /*
143 1.1 cgd * Check the integrity of each directory.
144 1.1 cgd */
145 1.7 mycroft memset(&curino, 0, sizeof(struct inodesc));
146 1.1 cgd curino.id_type = DATA;
147 1.1 cgd curino.id_func = pass2check;
148 1.1 cgd inpend = &inpsort[inplast];
149 1.1 cgd for (inpp = inpsort; inpp < inpend; inpp++) {
150 1.30 lukem if (got_siginfo) {
151 1.30 lukem fprintf(stderr,
152 1.31 agc "%s: phase 2: dir %ld of %d (%d%%)\n", cdevname(),
153 1.31 agc (long)(inpp - inpsort), (int)inplast,
154 1.30 lukem (int)((inpp - inpsort) * 100 / inplast));
155 1.30 lukem got_siginfo = 0;
156 1.30 lukem }
157 1.1 cgd inp = *inpp;
158 1.1 cgd if (inp->i_isize == 0)
159 1.1 cgd continue;
160 1.1 cgd if (inp->i_isize < MINDIRSIZE) {
161 1.1 cgd direrror(inp->i_number, "DIRECTORY TOO SHORT");
162 1.32 dbj inp->i_isize = roundup(MINDIRSIZE, dirblksiz);
163 1.1 cgd if (reply("FIX") == 1) {
164 1.1 cgd dp = ginode(inp->i_number);
165 1.33 fvdl DIP(dp, size) = iswap64(inp->i_isize);
166 1.1 cgd inodirty();
167 1.24 bouyer } else
168 1.24 bouyer markclean = 0;
169 1.32 dbj } else if ((inp->i_isize & (dirblksiz - 1)) != 0) {
170 1.1 cgd getpathname(pathbuf, inp->i_number, inp->i_number);
171 1.26 fvdl if (usedsoftdep)
172 1.28 lukem pfatal("%s %s: LENGTH %lld NOT MULTIPLE OF %d",
173 1.27 mrg "DIRECTORY", pathbuf,
174 1.32 dbj (long long)inp->i_isize, dirblksiz);
175 1.26 fvdl else
176 1.28 lukem pwarn("%s %s: LENGTH %lld NOT MULTIPLE OF %d",
177 1.27 mrg "DIRECTORY", pathbuf,
178 1.32 dbj (long long)inp->i_isize, dirblksiz);
179 1.1 cgd if (preen)
180 1.1 cgd printf(" (ADJUSTED)\n");
181 1.32 dbj inp->i_isize = roundup(inp->i_isize, dirblksiz);
182 1.1 cgd if (preen || reply("ADJUST") == 1) {
183 1.1 cgd dp = ginode(inp->i_number);
184 1.33 fvdl DIP(dp, size) = iswap64(inp->i_isize);
185 1.1 cgd inodirty();
186 1.24 bouyer } else
187 1.24 bouyer markclean = 0;
188 1.1 cgd }
189 1.33 fvdl memset(&dino, 0, sizeof dino);
190 1.33 fvdl dp = &dino;
191 1.33 fvdl if (!is_ufs2) {
192 1.33 fvdl dp->dp1.di_mode = iswap16(IFDIR);
193 1.33 fvdl dp->dp1.di_size = iswap64(inp->i_isize);
194 1.33 fvdl maxblk = inp->i_numblks < NDADDR ? inp->i_numblks :
195 1.33 fvdl NDADDR;
196 1.33 fvdl for (i = 0; i < maxblk; i++)
197 1.33 fvdl dp->dp1.di_db[i] = inp->i_blks[i];
198 1.33 fvdl } else {
199 1.33 fvdl dp->dp2.di_mode = iswap16(IFDIR);
200 1.33 fvdl dp->dp2.di_size = iswap64(inp->i_isize);
201 1.33 fvdl maxblk = inp->i_numblks < NDADDR ? inp->i_numblks :
202 1.33 fvdl NDADDR;
203 1.33 fvdl for (i = 0; i < maxblk; i++)
204 1.33 fvdl dp->dp2.di_db[i] = inp->i_blks[i];
205 1.33 fvdl }
206 1.24 bouyer curino.id_number = inp->i_number;
207 1.24 bouyer curino.id_parent = inp->i_parent;
208 1.24 bouyer (void)ckinode(&dino, &curino);
209 1.33 fvdl }
210 1.24 bouyer
211 1.33 fvdl /*
212 1.33 fvdl * Byte swapping in directory entries, if needed, has been done.
213 1.24 bouyer * Now rescan dirs for pass2check()
214 1.24 bouyer */
215 1.24 bouyer if (do_dirswap) {
216 1.24 bouyer do_dirswap = 0;
217 1.24 bouyer for (inpp = inpsort; inpp < inpend; inpp++) {
218 1.24 bouyer inp = *inpp;
219 1.24 bouyer if (inp->i_isize == 0)
220 1.24 bouyer continue;
221 1.33 fvdl memset(&dino, 0, sizeof dino);
222 1.33 fvdl if (!is_ufs2) {
223 1.33 fvdl dino.dp1.di_mode = iswap16(IFDIR);
224 1.33 fvdl dino.dp1.di_size = iswap64(inp->i_isize);
225 1.33 fvdl for (i = 0; i < inp->i_numblks; i++)
226 1.33 fvdl dino.dp1.di_db[i] = inp->i_blks[i];
227 1.33 fvdl } else {
228 1.33 fvdl dino.dp2.di_mode = iswap16(IFDIR);
229 1.33 fvdl dino.dp2.di_size = iswap64(inp->i_isize);
230 1.33 fvdl for (i = 0; i < inp->i_numblks; i++)
231 1.33 fvdl dino.dp2.di_db[i] = inp->i_blks[i];
232 1.33 fvdl }
233 1.33 fvdl curino.id_number = inp->i_number;
234 1.33 fvdl curino.id_parent = inp->i_parent;
235 1.33 fvdl (void)ckinode(&dino, &curino);
236 1.33 fvdl }
237 1.24 bouyer }
238 1.24 bouyer
239 1.1 cgd /*
240 1.1 cgd * Now that the parents of all directories have been found,
241 1.1 cgd * make another pass to verify the value of `..'
242 1.1 cgd */
243 1.1 cgd for (inpp = inpsort; inpp < inpend; inpp++) {
244 1.1 cgd inp = *inpp;
245 1.1 cgd if (inp->i_parent == 0 || inp->i_isize == 0)
246 1.1 cgd continue;
247 1.1 cgd if (inp->i_dotdot == inp->i_parent ||
248 1.1 cgd inp->i_dotdot == (ino_t)-1)
249 1.1 cgd continue;
250 1.33 fvdl info = inoinfo(inp->i_parent);
251 1.1 cgd if (inp->i_dotdot == 0) {
252 1.1 cgd inp->i_dotdot = inp->i_parent;
253 1.1 cgd fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
254 1.24 bouyer if (reply("FIX") == 0) {
255 1.24 bouyer markclean = 0;
256 1.1 cgd continue;
257 1.24 bouyer }
258 1.1 cgd (void)makeentry(inp->i_number, inp->i_parent, "..");
259 1.33 fvdl info->ino_linkcnt--;
260 1.1 cgd continue;
261 1.1 cgd }
262 1.1 cgd fileerror(inp->i_parent, inp->i_number,
263 1.6 mycroft "BAD INODE NUMBER FOR '..'");
264 1.24 bouyer if (reply("FIX") == 0) {
265 1.24 bouyer markclean = 0;
266 1.1 cgd continue;
267 1.24 bouyer }
268 1.33 fvdl inoinfo(inp->i_dotdot)->ino_linkcnt++;
269 1.33 fvdl info->ino_linkcnt--;
270 1.1 cgd inp->i_dotdot = inp->i_parent;
271 1.1 cgd (void)changeino(inp->i_number, "..", inp->i_parent);
272 1.1 cgd }
273 1.1 cgd /*
274 1.29 mycroft * Create a list of children for each directory.
275 1.29 mycroft */
276 1.29 mycroft inpend = &inpsort[inplast];
277 1.29 mycroft for (inpp = inpsort; inpp < inpend; inpp++) {
278 1.29 mycroft inp = *inpp;
279 1.33 fvdl info = inoinfo(inp->i_number);
280 1.29 mycroft inp->i_child = inp->i_sibling = inp->i_parentp = 0;
281 1.33 fvdl if (info->ino_state == DFOUND)
282 1.33 fvdl info->ino_state = DSTATE;
283 1.29 mycroft }
284 1.29 mycroft for (inpp = inpsort; inpp < inpend; inpp++) {
285 1.29 mycroft inp = *inpp;
286 1.29 mycroft if (inp->i_parent == 0 ||
287 1.29 mycroft inp->i_number == ROOTINO)
288 1.29 mycroft continue;
289 1.29 mycroft pinp = getinoinfo(inp->i_parent);
290 1.29 mycroft inp->i_parentp = pinp;
291 1.29 mycroft inp->i_sibling = pinp->i_child;
292 1.29 mycroft pinp->i_child = inp;
293 1.29 mycroft }
294 1.29 mycroft /*
295 1.1 cgd * Mark all the directories that can be found from the root.
296 1.1 cgd */
297 1.29 mycroft propagate(ROOTINO);
298 1.1 cgd }
299 1.1 cgd
300 1.16 christos static int
301 1.1 cgd pass2check(idesc)
302 1.1 cgd struct inodesc *idesc;
303 1.1 cgd {
304 1.18 lukem struct direct *dirp = idesc->id_dirp;
305 1.18 lukem struct inoinfo *inp;
306 1.33 fvdl struct inostat *info;
307 1.1 cgd int n, entrysize, ret = 0;
308 1.33 fvdl union dinode *dp;
309 1.1 cgd char *errmsg;
310 1.1 cgd struct direct proto;
311 1.1 cgd char namebuf[MAXPATHLEN + 1];
312 1.1 cgd char pathbuf[MAXPATHLEN + 1];
313 1.1 cgd
314 1.6 mycroft /*
315 1.6 mycroft * If converting, set directory entry type.
316 1.6 mycroft */
317 1.33 fvdl if (!is_ufs2 && doinglevel2 && iswap32(dirp->d_ino) > 0 &&
318 1.33 fvdl iswap32(dirp->d_ino) < maxino) {
319 1.33 fvdl dirp->d_type = inoinfo(iswap32(dirp->d_ino))->ino_type;
320 1.6 mycroft ret |= ALTERED;
321 1.6 mycroft }
322 1.1 cgd /*
323 1.1 cgd * check for "."
324 1.1 cgd */
325 1.1 cgd if (idesc->id_entryno != 0)
326 1.1 cgd goto chk1;
327 1.1 cgd if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
328 1.24 bouyer if (iswap32(dirp->d_ino) != idesc->id_number) {
329 1.1 cgd direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
330 1.24 bouyer dirp->d_ino = iswap32(idesc->id_number);
331 1.1 cgd if (reply("FIX") == 1)
332 1.1 cgd ret |= ALTERED;
333 1.24 bouyer else
334 1.24 bouyer markclean = 0;
335 1.1 cgd }
336 1.6 mycroft if (newinofmt && dirp->d_type != DT_DIR) {
337 1.6 mycroft direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
338 1.6 mycroft dirp->d_type = DT_DIR;
339 1.6 mycroft if (reply("FIX") == 1)
340 1.6 mycroft ret |= ALTERED;
341 1.24 bouyer else
342 1.24 bouyer markclean = 0;
343 1.6 mycroft }
344 1.1 cgd goto chk1;
345 1.1 cgd }
346 1.1 cgd direrror(idesc->id_number, "MISSING '.'");
347 1.24 bouyer proto.d_ino = iswap32(idesc->id_number);
348 1.6 mycroft if (newinofmt)
349 1.6 mycroft proto.d_type = DT_DIR;
350 1.6 mycroft else
351 1.6 mycroft proto.d_type = 0;
352 1.1 cgd proto.d_namlen = 1;
353 1.1 cgd (void)strcpy(proto.d_name, ".");
354 1.10 mycroft # if BYTE_ORDER == LITTLE_ENDIAN
355 1.24 bouyer if (!newinofmt && !needswap) {
356 1.24 bouyer # else
357 1.24 bouyer if (!newinofmt && needswap) {
358 1.24 bouyer # endif
359 1.10 mycroft u_char tmp;
360 1.10 mycroft
361 1.10 mycroft tmp = proto.d_type;
362 1.10 mycroft proto.d_type = proto.d_namlen;
363 1.10 mycroft proto.d_namlen = tmp;
364 1.10 mycroft }
365 1.24 bouyer entrysize = DIRSIZ(0, &proto, 0);
366 1.1 cgd if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
367 1.1 cgd pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
368 1.1 cgd dirp->d_name);
369 1.24 bouyer markclean = 0;
370 1.24 bouyer } else if (iswap16(dirp->d_reclen) < entrysize) {
371 1.1 cgd pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
372 1.24 bouyer markclean = 0;
373 1.24 bouyer } else if (iswap16(dirp->d_reclen) < 2 * entrysize) {
374 1.1 cgd proto.d_reclen = dirp->d_reclen;
375 1.20 lukem memmove(dirp, &proto, (size_t)entrysize);
376 1.1 cgd if (reply("FIX") == 1)
377 1.1 cgd ret |= ALTERED;
378 1.24 bouyer else
379 1.24 bouyer markclean = 0;
380 1.1 cgd } else {
381 1.24 bouyer n = iswap16(dirp->d_reclen) - entrysize;
382 1.24 bouyer proto.d_reclen = iswap16(entrysize);
383 1.20 lukem memmove(dirp, &proto, (size_t)entrysize);
384 1.1 cgd idesc->id_entryno++;
385 1.33 fvdl inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
386 1.1 cgd dirp = (struct direct *)((char *)(dirp) + entrysize);
387 1.7 mycroft memset(dirp, 0, (size_t)n);
388 1.24 bouyer dirp->d_reclen = iswap16(n);
389 1.1 cgd if (reply("FIX") == 1)
390 1.1 cgd ret |= ALTERED;
391 1.24 bouyer else
392 1.24 bouyer markclean = 0;
393 1.1 cgd }
394 1.1 cgd chk1:
395 1.1 cgd if (idesc->id_entryno > 1)
396 1.1 cgd goto chk2;
397 1.1 cgd inp = getinoinfo(idesc->id_number);
398 1.24 bouyer proto.d_ino = iswap32(inp->i_parent);
399 1.6 mycroft if (newinofmt)
400 1.6 mycroft proto.d_type = DT_DIR;
401 1.6 mycroft else
402 1.6 mycroft proto.d_type = 0;
403 1.1 cgd proto.d_namlen = 2;
404 1.1 cgd (void)strcpy(proto.d_name, "..");
405 1.11 mycroft # if BYTE_ORDER == LITTLE_ENDIAN
406 1.24 bouyer if (!newinofmt && !needswap) {
407 1.24 bouyer # else
408 1.24 bouyer if (!newinofmt && needswap) {
409 1.24 bouyer # endif
410 1.11 mycroft u_char tmp;
411 1.11 mycroft
412 1.11 mycroft tmp = proto.d_type;
413 1.11 mycroft proto.d_type = proto.d_namlen;
414 1.11 mycroft proto.d_namlen = tmp;
415 1.11 mycroft }
416 1.24 bouyer entrysize = DIRSIZ(0, &proto, 0);
417 1.1 cgd if (idesc->id_entryno == 0) {
418 1.24 bouyer n = DIRSIZ(0, dirp, 0);
419 1.24 bouyer if (iswap16(dirp->d_reclen) < n + entrysize)
420 1.1 cgd goto chk2;
421 1.24 bouyer proto.d_reclen = iswap16(iswap16(dirp->d_reclen) - n);
422 1.24 bouyer dirp->d_reclen = iswap16(n);
423 1.1 cgd idesc->id_entryno++;
424 1.33 fvdl inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
425 1.1 cgd dirp = (struct direct *)((char *)(dirp) + n);
426 1.24 bouyer memset(dirp, 0, (size_t)iswap16(proto.d_reclen));
427 1.1 cgd dirp->d_reclen = proto.d_reclen;
428 1.1 cgd }
429 1.1 cgd if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
430 1.24 bouyer inp->i_dotdot = iswap32(dirp->d_ino);
431 1.6 mycroft if (newinofmt && dirp->d_type != DT_DIR) {
432 1.6 mycroft direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
433 1.6 mycroft dirp->d_type = DT_DIR;
434 1.6 mycroft if (reply("FIX") == 1)
435 1.6 mycroft ret |= ALTERED;
436 1.24 bouyer else
437 1.24 bouyer markclean = 0;
438 1.6 mycroft }
439 1.1 cgd goto chk2;
440 1.1 cgd }
441 1.24 bouyer if (iswap32(dirp->d_ino) != 0 && strcmp(dirp->d_name, ".") != 0) {
442 1.1 cgd fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
443 1.1 cgd pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
444 1.1 cgd dirp->d_name);
445 1.1 cgd inp->i_dotdot = (ino_t)-1;
446 1.24 bouyer markclean = 0;
447 1.24 bouyer } else if (iswap16(dirp->d_reclen) < entrysize) {
448 1.1 cgd fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
449 1.1 cgd pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
450 1.1 cgd inp->i_dotdot = (ino_t)-1;
451 1.24 bouyer markclean = 0;
452 1.1 cgd } else if (inp->i_parent != 0) {
453 1.1 cgd /*
454 1.1 cgd * We know the parent, so fix now.
455 1.1 cgd */
456 1.1 cgd inp->i_dotdot = inp->i_parent;
457 1.1 cgd fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
458 1.1 cgd proto.d_reclen = dirp->d_reclen;
459 1.20 lukem memmove(dirp, &proto, (size_t)entrysize);
460 1.1 cgd if (reply("FIX") == 1)
461 1.1 cgd ret |= ALTERED;
462 1.24 bouyer else
463 1.24 bouyer markclean = 0;
464 1.1 cgd }
465 1.1 cgd idesc->id_entryno++;
466 1.1 cgd if (dirp->d_ino != 0)
467 1.33 fvdl inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
468 1.1 cgd return (ret|KEEPON);
469 1.1 cgd chk2:
470 1.1 cgd if (dirp->d_ino == 0)
471 1.1 cgd return (ret|KEEPON);
472 1.1 cgd if (dirp->d_namlen <= 2 &&
473 1.1 cgd dirp->d_name[0] == '.' &&
474 1.1 cgd idesc->id_entryno >= 2) {
475 1.1 cgd if (dirp->d_namlen == 1) {
476 1.1 cgd direrror(idesc->id_number, "EXTRA '.' ENTRY");
477 1.1 cgd dirp->d_ino = 0;
478 1.1 cgd if (reply("FIX") == 1)
479 1.1 cgd ret |= ALTERED;
480 1.24 bouyer else
481 1.24 bouyer markclean = 0;
482 1.1 cgd return (KEEPON | ret);
483 1.1 cgd }
484 1.1 cgd if (dirp->d_name[1] == '.') {
485 1.1 cgd direrror(idesc->id_number, "EXTRA '..' ENTRY");
486 1.1 cgd dirp->d_ino = 0;
487 1.1 cgd if (reply("FIX") == 1)
488 1.1 cgd ret |= ALTERED;
489 1.24 bouyer else
490 1.24 bouyer markclean = 0;
491 1.1 cgd return (KEEPON | ret);
492 1.1 cgd }
493 1.1 cgd }
494 1.1 cgd idesc->id_entryno++;
495 1.1 cgd n = 0;
496 1.24 bouyer if (iswap32(dirp->d_ino) > maxino) {
497 1.1 cgd fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
498 1.1 cgd n = reply("REMOVE");
499 1.24 bouyer if (n == 0)
500 1.24 bouyer markclean = 0;
501 1.10 mycroft } else if (newinofmt &&
502 1.24 bouyer ((iswap32(dirp->d_ino) == WINO && dirp->d_type != DT_WHT) ||
503 1.24 bouyer (iswap32(dirp->d_ino) != WINO && dirp->d_type == DT_WHT))) {
504 1.24 bouyer fileerror(idesc->id_number, iswap32(dirp->d_ino), "BAD WHITEOUT ENTRY");
505 1.24 bouyer dirp->d_ino = iswap32(WINO);
506 1.10 mycroft dirp->d_type = DT_WHT;
507 1.10 mycroft if (reply("FIX") == 1)
508 1.10 mycroft ret |= ALTERED;
509 1.24 bouyer else
510 1.24 bouyer markclean = 0;
511 1.1 cgd } else {
512 1.1 cgd again:
513 1.33 fvdl info = inoinfo(iswap32(dirp->d_ino));
514 1.33 fvdl switch (info->ino_state) {
515 1.1 cgd case USTATE:
516 1.1 cgd if (idesc->id_entryno <= 2)
517 1.1 cgd break;
518 1.24 bouyer fileerror(idesc->id_number, iswap32(dirp->d_ino), "UNALLOCATED");
519 1.1 cgd n = reply("REMOVE");
520 1.24 bouyer if (n == 0)
521 1.24 bouyer markclean = 0;
522 1.1 cgd break;
523 1.1 cgd
524 1.1 cgd case DCLEAR:
525 1.1 cgd case FCLEAR:
526 1.1 cgd if (idesc->id_entryno <= 2)
527 1.1 cgd break;
528 1.33 fvdl if (info->ino_state == FCLEAR)
529 1.6 mycroft errmsg = "DUP/BAD";
530 1.26 fvdl else if (!preen && !usedsoftdep)
531 1.1 cgd errmsg = "ZERO LENGTH DIRECTORY";
532 1.6 mycroft else {
533 1.6 mycroft n = 1;
534 1.6 mycroft break;
535 1.6 mycroft }
536 1.24 bouyer fileerror(idesc->id_number, iswap32(dirp->d_ino), errmsg);
537 1.1 cgd if ((n = reply("REMOVE")) == 1)
538 1.1 cgd break;
539 1.24 bouyer dp = ginode(iswap32(dirp->d_ino));
540 1.33 fvdl info->ino_state =
541 1.33 fvdl (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? DSTATE : FSTATE;
542 1.33 fvdl info->ino_linkcnt = iswap16(DIP(dp, nlink));
543 1.1 cgd goto again;
544 1.1 cgd
545 1.1 cgd case DSTATE:
546 1.1 cgd case DFOUND:
547 1.24 bouyer inp = getinoinfo(iswap32(dirp->d_ino));
548 1.1 cgd if (inp->i_parent != 0 && idesc->id_entryno > 2) {
549 1.1 cgd getpathname(pathbuf, idesc->id_number,
550 1.1 cgd idesc->id_number);
551 1.24 bouyer getpathname(namebuf, iswap32(dirp->d_ino),
552 1.24 bouyer iswap32(dirp->d_ino));
553 1.1 cgd pwarn("%s %s %s\n", pathbuf,
554 1.1 cgd "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
555 1.1 cgd namebuf);
556 1.1 cgd if (preen)
557 1.1 cgd printf(" (IGNORED)\n");
558 1.1 cgd else if ((n = reply("REMOVE")) == 1)
559 1.1 cgd break;
560 1.1 cgd }
561 1.1 cgd if (idesc->id_entryno > 2)
562 1.1 cgd inp->i_parent = idesc->id_number;
563 1.1 cgd /* fall through */
564 1.1 cgd
565 1.1 cgd case FSTATE:
566 1.33 fvdl if (newinofmt && dirp->d_type != info->ino_type) {
567 1.24 bouyer fileerror(idesc->id_number, iswap32(dirp->d_ino),
568 1.6 mycroft "BAD TYPE VALUE");
569 1.33 fvdl dirp->d_type = info->ino_type;
570 1.6 mycroft if (reply("FIX") == 1)
571 1.6 mycroft ret |= ALTERED;
572 1.24 bouyer else
573 1.24 bouyer markclean = 0;
574 1.6 mycroft }
575 1.33 fvdl info->ino_linkcnt--;
576 1.1 cgd break;
577 1.1 cgd
578 1.1 cgd default:
579 1.20 lukem errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
580 1.33 fvdl info->ino_state, iswap32(dirp->d_ino));
581 1.1 cgd }
582 1.1 cgd }
583 1.1 cgd if (n == 0)
584 1.1 cgd return (ret|KEEPON);
585 1.1 cgd dirp->d_ino = 0;
586 1.1 cgd return (ret|KEEPON|ALTERED);
587 1.1 cgd }
588 1.1 cgd
589 1.1 cgd /*
590 1.1 cgd * Routine to sort disk blocks.
591 1.1 cgd */
592 1.16 christos static int
593 1.20 lukem blksort(arg1, arg2)
594 1.20 lukem const void *arg1, *arg2;
595 1.1 cgd {
596 1.20 lukem
597 1.20 lukem return ((*(struct inoinfo **)arg1)->i_blks[0] -
598 1.20 lukem (*(struct inoinfo **)arg2)->i_blks[0]);
599 1.1 cgd }
600