2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)pass2.c 8.9 (Berkeley) 4/28/95
34 * $FreeBSD: src/sbin/fsck/pass2.c,v 1.10.2.2 2001/11/24 15:14:59 iedowse Exp $
35 * $DragonFly: src/sbin/fsck/pass2.c,v 1.3 2003/08/08 04:18:37 dillon Exp $
38 #include <sys/param.h>
40 #include <vfs/ufs/dinode.h>
41 #include <vfs/ufs/dir.h>
48 #define MINDIRSIZE (sizeof (struct dirtemplate))
50 static int blksort __P((const void *, const void *));
51 static int pass2check __P((struct inodesc *));
56 register struct dinode *dp;
57 register struct inoinfo **inpp, *inp;
58 struct inoinfo **inpend;
59 struct inodesc curino;
61 char pathbuf[MAXPATHLEN + 1];
63 switch (inoinfo(ROOTINO)->ino_state) {
66 pfatal("ROOT INODE UNALLOCATED");
67 if (reply("ALLOCATE") == 0) {
71 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
72 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
76 pfatal("DUPS/BAD IN ROOT INODE");
77 if (reply("REALLOCATE")) {
79 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
80 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
83 if (reply("CONTINUE") == 0) {
91 pfatal("ROOT INODE NOT DIRECTORY");
92 if (reply("REALLOCATE")) {
94 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
95 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
98 if (reply("FIX") == 0) {
102 dp = ginode(ROOTINO);
103 dp->di_mode &= ~IFMT;
104 dp->di_mode |= IFDIR;
112 errx(EEXIT, "BAD STATE %d FOR ROOT INODE",
113 inoinfo(ROOTINO)->ino_state);
115 inoinfo(ROOTINO)->ino_state = DFOUND;
117 inoinfo(WINO)->ino_state = FSTATE;
118 inoinfo(WINO)->ino_type = DT_WHT;
121 * Sort the directory list into disk block order.
123 qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
125 * Check the integrity of each directory.
127 memset(&curino, 0, sizeof(struct inodesc));
128 curino.id_type = DATA;
129 curino.id_func = pass2check;
131 inpend = &inpsort[inplast];
132 for (inpp = inpsort; inpp < inpend; inpp++) {
134 printf("%s: phase 2: dir %d of %d (%d%%)\n", cdevname,
135 inpp - inpsort, inplast, (inpp - inpsort) * 100 /
140 if (inp->i_isize == 0)
142 if (inp->i_isize < MINDIRSIZE) {
143 direrror(inp->i_number, "DIRECTORY TOO SHORT");
144 inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
145 if (reply("FIX") == 1) {
146 dp = ginode(inp->i_number);
147 dp->di_size = inp->i_isize;
151 } else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
152 getpathname(pathbuf, inp->i_number, inp->i_number);
154 pfatal("%s %s: LENGTH %d NOT MULTIPLE OF %d",
155 "DIRECTORY", pathbuf, inp->i_isize,
158 pwarn("%s %s: LENGTH %d NOT MULTIPLE OF %d",
159 "DIRECTORY", pathbuf, inp->i_isize,
162 printf(" (ADJUSTED)\n");
163 inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
164 if (preen || reply("ADJUST") == 1) {
165 dp = ginode(inp->i_number);
166 dp->di_size = roundup(inp->i_isize, DIRBLKSIZ);
171 memset(&dino, 0, sizeof(struct dinode));
172 dino.di_mode = IFDIR;
173 dp->di_size = inp->i_isize;
174 memmove(&dp->di_db[0], &inp->i_blks[0], (size_t)inp->i_numblks);
175 curino.id_number = inp->i_number;
176 curino.id_parent = inp->i_parent;
177 (void)ckinode(dp, &curino);
180 * Now that the parents of all directories have been found,
181 * make another pass to verify the value of `..'
183 for (inpp = inpsort; inpp < inpend; inpp++) {
185 if (inp->i_parent == 0 || inp->i_isize == 0)
187 if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
188 inoinfo(inp->i_number)->ino_state == DSTATE)
189 inoinfo(inp->i_number)->ino_state = DFOUND;
190 if (inp->i_dotdot == inp->i_parent ||
191 inp->i_dotdot == (ino_t)-1)
193 if (inp->i_dotdot == 0) {
194 inp->i_dotdot = inp->i_parent;
195 fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
196 if (reply("FIX") == 0)
198 (void)makeentry(inp->i_number, inp->i_parent, "..");
199 inoinfo(inp->i_parent)->ino_linkcnt--;
202 fileerror(inp->i_parent, inp->i_number,
203 "BAD INODE NUMBER FOR '..'");
204 if (reply("FIX") == 0)
206 inoinfo(inp->i_dotdot)->ino_linkcnt++;
207 inoinfo(inp->i_parent)->ino_linkcnt--;
208 inp->i_dotdot = inp->i_parent;
209 (void)changeino(inp->i_number, "..", inp->i_parent);
212 * Mark all the directories that can be found from the root.
219 struct inodesc *idesc;
221 register struct direct *dirp = idesc->id_dirp;
222 register struct inoinfo *inp;
223 int n, entrysize, ret = 0;
227 char namebuf[MAXPATHLEN + 1];
228 char pathbuf[MAXPATHLEN + 1];
231 * If converting, set directory entry type.
233 if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) {
234 dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
240 if (idesc->id_entryno != 0)
242 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
243 if (dirp->d_ino != idesc->id_number) {
244 direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
245 dirp->d_ino = idesc->id_number;
246 if (reply("FIX") == 1)
249 if (newinofmt && dirp->d_type != DT_DIR) {
250 direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
251 dirp->d_type = DT_DIR;
252 if (reply("FIX") == 1)
257 direrror(idesc->id_number, "MISSING '.'");
258 proto.d_ino = idesc->id_number;
260 proto.d_type = DT_DIR;
264 (void)strcpy(proto.d_name, ".");
265 # if BYTE_ORDER == LITTLE_ENDIAN
270 proto.d_type = proto.d_namlen;
271 proto.d_namlen = tmp;
274 entrysize = DIRSIZ(0, &proto);
275 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
276 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
278 } else if (dirp->d_reclen < entrysize) {
279 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
280 } else if (dirp->d_reclen < 2 * entrysize) {
281 proto.d_reclen = dirp->d_reclen;
282 memmove(dirp, &proto, (size_t)entrysize);
283 if (reply("FIX") == 1)
286 n = dirp->d_reclen - entrysize;
287 proto.d_reclen = entrysize;
288 memmove(dirp, &proto, (size_t)entrysize);
290 inoinfo(dirp->d_ino)->ino_linkcnt--;
291 dirp = (struct direct *)((char *)(dirp) + entrysize);
292 memset(dirp, 0, (size_t)n);
294 if (reply("FIX") == 1)
298 if (idesc->id_entryno > 1)
300 inp = getinoinfo(idesc->id_number);
301 proto.d_ino = inp->i_parent;
303 proto.d_type = DT_DIR;
307 (void)strcpy(proto.d_name, "..");
308 # if BYTE_ORDER == LITTLE_ENDIAN
313 proto.d_type = proto.d_namlen;
314 proto.d_namlen = tmp;
317 entrysize = DIRSIZ(0, &proto);
318 if (idesc->id_entryno == 0) {
320 if (dirp->d_reclen < n + entrysize)
322 proto.d_reclen = dirp->d_reclen - n;
325 inoinfo(dirp->d_ino)->ino_linkcnt--;
326 dirp = (struct direct *)((char *)(dirp) + n);
327 memset(dirp, 0, (size_t)proto.d_reclen);
328 dirp->d_reclen = proto.d_reclen;
330 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
331 inp->i_dotdot = dirp->d_ino;
332 if (newinofmt && dirp->d_type != DT_DIR) {
333 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
334 dirp->d_type = DT_DIR;
335 if (reply("FIX") == 1)
340 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
341 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
342 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
344 inp->i_dotdot = (ino_t)-1;
345 } else if (dirp->d_reclen < entrysize) {
346 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
347 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
348 inp->i_dotdot = (ino_t)-1;
349 } else if (inp->i_parent != 0) {
351 * We know the parent, so fix now.
353 inp->i_dotdot = inp->i_parent;
354 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
355 proto.d_reclen = dirp->d_reclen;
356 memmove(dirp, &proto, (size_t)entrysize);
357 if (reply("FIX") == 1)
361 if (dirp->d_ino != 0)
362 inoinfo(dirp->d_ino)->ino_linkcnt--;
365 if (dirp->d_ino == 0)
367 if (dirp->d_namlen <= 2 &&
368 dirp->d_name[0] == '.' &&
369 idesc->id_entryno >= 2) {
370 if (dirp->d_namlen == 1) {
371 direrror(idesc->id_number, "EXTRA '.' ENTRY");
373 if (reply("FIX") == 1)
375 return (KEEPON | ret);
377 if (dirp->d_name[1] == '.') {
378 direrror(idesc->id_number, "EXTRA '..' ENTRY");
380 if (reply("FIX") == 1)
382 return (KEEPON | ret);
387 if (dirp->d_ino > maxino) {
388 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
390 } else if (newinofmt &&
391 ((dirp->d_ino == WINO && dirp->d_type != DT_WHT) ||
392 (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
393 fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
395 dirp->d_type = DT_WHT;
396 if (reply("FIX") == 1)
400 switch (inoinfo(dirp->d_ino)->ino_state) {
402 if (idesc->id_entryno <= 2)
404 fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
410 if (idesc->id_entryno <= 2)
412 if (inoinfo(dirp->d_ino)->ino_state == FCLEAR)
414 else if (!preen && !usedsoftdep)
415 errmsg = "ZERO LENGTH DIRECTORY";
420 fileerror(idesc->id_number, dirp->d_ino, errmsg);
421 if ((n = reply("REMOVE")) == 1)
423 dp = ginode(dirp->d_ino);
424 inoinfo(dirp->d_ino)->ino_state =
425 (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
426 inoinfo(dirp->d_ino)->ino_linkcnt = dp->di_nlink;
430 if (inoinfo(idesc->id_number)->ino_state == DFOUND)
431 inoinfo(dirp->d_ino)->ino_state = DFOUND;
435 inp = getinoinfo(dirp->d_ino);
436 if (inp->i_parent != 0 && idesc->id_entryno > 2) {
437 getpathname(pathbuf, idesc->id_number,
439 getpathname(namebuf, dirp->d_ino, dirp->d_ino);
440 pwarn("%s%s%s %s %s\n", pathbuf,
441 (strcmp(pathbuf, "/") == 0 ? "" : "/"),
443 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
446 printf(" (REMOVED)\n");
450 if ((n = reply("REMOVE")) == 1)
453 if (idesc->id_entryno > 2)
454 inp->i_parent = idesc->id_number;
459 dirp->d_type != inoinfo(dirp->d_ino)->ino_type) {
460 fileerror(idesc->id_number, dirp->d_ino,
462 dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
463 if (reply("FIX") == 1)
466 inoinfo(dirp->d_ino)->ino_linkcnt--;
470 errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
471 inoinfo(dirp->d_ino)->ino_state, dirp->d_ino);
477 return (ret|KEEPON|ALTERED);
481 * Routine to sort disk blocks.
485 const void *arg1, *arg2;
488 return ((*(struct inoinfo **)arg1)->i_blks[0] -
489 (*(struct inoinfo **)arg2)->i_blks[0]);