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.6 2004/02/04 17:39:59 joerg 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(const void *, const void *);
51 static int pass2check(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 %ld (%d%%)\n", cdevname,
135 inpp - inpsort, inplast, (int)((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.
218 pass2check(struct inodesc *idesc)
220 register struct direct *dirp = idesc->id_dirp;
221 register struct inoinfo *inp;
222 int n, entrysize, ret = 0;
226 char namebuf[MAXPATHLEN + 1];
227 char pathbuf[MAXPATHLEN + 1];
230 * If converting, set directory entry type.
232 if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) {
233 dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
239 if (idesc->id_entryno != 0)
241 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
242 if (dirp->d_ino != idesc->id_number) {
243 direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
244 dirp->d_ino = idesc->id_number;
245 if (reply("FIX") == 1)
248 if (newinofmt && dirp->d_type != DT_DIR) {
249 direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
250 dirp->d_type = DT_DIR;
251 if (reply("FIX") == 1)
256 direrror(idesc->id_number, "MISSING '.'");
257 proto.d_ino = idesc->id_number;
259 proto.d_type = DT_DIR;
263 (void)strcpy(proto.d_name, ".");
264 # if BYTE_ORDER == LITTLE_ENDIAN
269 proto.d_type = proto.d_namlen;
270 proto.d_namlen = tmp;
273 entrysize = DIRSIZ(0, &proto);
274 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
275 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
277 } else if (dirp->d_reclen < entrysize) {
278 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
279 } else if (dirp->d_reclen < 2 * entrysize) {
280 proto.d_reclen = dirp->d_reclen;
281 memmove(dirp, &proto, (size_t)entrysize);
282 if (reply("FIX") == 1)
285 n = dirp->d_reclen - entrysize;
286 proto.d_reclen = entrysize;
287 memmove(dirp, &proto, (size_t)entrysize);
289 inoinfo(dirp->d_ino)->ino_linkcnt--;
290 dirp = (struct direct *)((char *)(dirp) + entrysize);
291 memset(dirp, 0, (size_t)n);
293 if (reply("FIX") == 1)
297 if (idesc->id_entryno > 1)
299 inp = getinoinfo(idesc->id_number);
300 proto.d_ino = inp->i_parent;
302 proto.d_type = DT_DIR;
306 (void)strcpy(proto.d_name, "..");
307 # if BYTE_ORDER == LITTLE_ENDIAN
312 proto.d_type = proto.d_namlen;
313 proto.d_namlen = tmp;
316 entrysize = DIRSIZ(0, &proto);
317 if (idesc->id_entryno == 0) {
319 if (dirp->d_reclen < n + entrysize)
321 proto.d_reclen = dirp->d_reclen - n;
324 inoinfo(dirp->d_ino)->ino_linkcnt--;
325 dirp = (struct direct *)((char *)(dirp) + n);
326 memset(dirp, 0, (size_t)proto.d_reclen);
327 dirp->d_reclen = proto.d_reclen;
329 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
330 inp->i_dotdot = dirp->d_ino;
331 if (newinofmt && dirp->d_type != DT_DIR) {
332 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
333 dirp->d_type = DT_DIR;
334 if (reply("FIX") == 1)
339 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
340 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
341 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
343 inp->i_dotdot = (ino_t)-1;
344 } else if (dirp->d_reclen < entrysize) {
345 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
346 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
347 inp->i_dotdot = (ino_t)-1;
348 } else if (inp->i_parent != 0) {
350 * We know the parent, so fix now.
352 inp->i_dotdot = inp->i_parent;
353 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
354 proto.d_reclen = dirp->d_reclen;
355 memmove(dirp, &proto, (size_t)entrysize);
356 if (reply("FIX") == 1)
360 if (dirp->d_ino != 0)
361 inoinfo(dirp->d_ino)->ino_linkcnt--;
364 if (dirp->d_ino == 0)
366 if (dirp->d_namlen <= 2 &&
367 dirp->d_name[0] == '.' &&
368 idesc->id_entryno >= 2) {
369 if (dirp->d_namlen == 1) {
370 direrror(idesc->id_number, "EXTRA '.' ENTRY");
372 if (reply("FIX") == 1)
374 return (KEEPON | ret);
376 if (dirp->d_name[1] == '.') {
377 direrror(idesc->id_number, "EXTRA '..' ENTRY");
379 if (reply("FIX") == 1)
381 return (KEEPON | ret);
386 if (dirp->d_ino > maxino) {
387 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
389 } else if (newinofmt &&
390 ((dirp->d_ino == WINO && dirp->d_type != DT_WHT) ||
391 (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
392 fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
394 dirp->d_type = DT_WHT;
395 if (reply("FIX") == 1)
399 switch (inoinfo(dirp->d_ino)->ino_state) {
401 if (idesc->id_entryno <= 2)
403 fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
409 if (idesc->id_entryno <= 2)
411 if (inoinfo(dirp->d_ino)->ino_state == FCLEAR)
413 else if (!preen && !usedsoftdep)
414 errmsg = "ZERO LENGTH DIRECTORY";
419 fileerror(idesc->id_number, dirp->d_ino, errmsg);
420 if ((n = reply("REMOVE")) == 1)
422 dp = ginode(dirp->d_ino);
423 inoinfo(dirp->d_ino)->ino_state =
424 (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
425 inoinfo(dirp->d_ino)->ino_linkcnt = dp->di_nlink;
429 if (inoinfo(idesc->id_number)->ino_state == DFOUND)
430 inoinfo(dirp->d_ino)->ino_state = DFOUND;
434 inp = getinoinfo(dirp->d_ino);
435 if (inp->i_parent != 0 && idesc->id_entryno > 2) {
436 getpathname(pathbuf, idesc->id_number,
438 getpathname(namebuf, dirp->d_ino, dirp->d_ino);
439 pwarn("%s%s%s %s %s\n", pathbuf,
440 (strcmp(pathbuf, "/") == 0 ? "" : "/"),
442 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
445 printf(" (REMOVED)\n");
449 if ((n = reply("REMOVE")) == 1)
452 if (idesc->id_entryno > 2)
453 inp->i_parent = idesc->id_number;
458 dirp->d_type != inoinfo(dirp->d_ino)->ino_type) {
459 fileerror(idesc->id_number, dirp->d_ino,
461 dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
462 if (reply("FIX") == 1)
465 inoinfo(dirp->d_ino)->ino_linkcnt--;
469 errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
470 inoinfo(dirp->d_ino)->ino_state, dirp->d_ino);
476 return (ret|KEEPON|ALTERED);
480 * Routine to sort disk blocks.
483 blksort(const void *arg1, const void *arg2)
486 return ((*(struct inoinfo **)arg1)->i_blks[0] -
487 (*(struct inoinfo **)arg2)->i_blks[0]);