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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char sccsid[] = "@(#)pass2.c 8.9 (Berkeley) 4/28/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ufs/dir.h>
42 #include <ufs/ffs/fs.h>
50 #define MINDIRSIZE (sizeof (struct dirtemplate))
52 static int blksort(const void *, const void *);
53 static int pass2check(struct inodesc *);
59 struct inoinfo **inpp, *inp;
60 struct inoinfo **inpend;
61 struct inodesc curino;
64 char pathbuf[MAXPATHLEN + 1];
66 switch (inoinfo(ROOTINO)->ino_state) {
69 pfatal("ROOT INODE UNALLOCATED");
70 if (reply("ALLOCATE") == 0) {
74 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
75 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
79 pfatal("DUPS/BAD IN ROOT INODE");
80 if (reply("REALLOCATE")) {
82 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
83 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
86 if (reply("CONTINUE") == 0) {
94 pfatal("ROOT INODE NOT DIRECTORY");
95 if (reply("REALLOCATE")) {
97 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
98 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
101 if (reply("FIX") == 0) {
105 dp = ginode(ROOTINO);
106 DIP(dp, di_mode) &= ~IFMT;
107 DIP(dp, di_mode) |= IFDIR;
115 errx(EEXIT, "BAD STATE %d FOR ROOT INODE",
116 inoinfo(ROOTINO)->ino_state);
118 inoinfo(ROOTINO)->ino_state = DFOUND;
119 inoinfo(WINO)->ino_state = FSTATE;
120 inoinfo(WINO)->ino_type = DT_WHT;
122 * Sort the directory list into disk block order.
124 qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
126 * Check the integrity of each directory.
128 memset(&curino, 0, sizeof(struct inodesc));
129 curino.id_type = DATA;
130 curino.id_func = pass2check;
131 inpend = &inpsort[inplast];
132 for (inpp = inpsort; inpp < inpend; inpp++) {
134 printf("%s: phase 2: dir %td of %d (%d%%)\n", cdevname,
135 inpp - inpsort, (int)inplast,
136 (int)((inpp - inpsort) * 100 / inplast));
140 setproctitle("%s p2 %d%%", cdevname,
141 (int)((inpp - inpsort) * 100 / inplast));
145 if (inp->i_isize == 0)
147 if (inp->i_isize < MINDIRSIZE) {
148 direrror(inp->i_number, "DIRECTORY TOO SHORT");
149 inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
150 if (reply("FIX") == 1) {
151 dp = ginode(inp->i_number);
152 DIP(dp, di_size) = inp->i_isize;
155 } else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
156 getpathname(pathbuf, inp->i_number, inp->i_number);
158 pfatal("%s %s: LENGTH %jd NOT MULTIPLE OF %d",
159 "DIRECTORY", pathbuf,
160 (intmax_t)inp->i_isize, DIRBLKSIZ);
162 pwarn("%s %s: LENGTH %jd NOT MULTIPLE OF %d",
163 "DIRECTORY", pathbuf,
164 (intmax_t)inp->i_isize, DIRBLKSIZ);
166 printf(" (ADJUSTED)\n");
167 inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
168 if (preen || reply("ADJUST") == 1) {
169 dp = ginode(inp->i_number);
171 roundup(inp->i_isize, DIRBLKSIZ);
176 memset(dp, 0, sizeof(struct ufs2_dinode));
177 DIP(dp, di_mode) = IFDIR;
178 DIP(dp, di_size) = inp->i_isize;
180 i < (inp->i_numblks<NDADDR ? inp->i_numblks : NDADDR);
182 DIP(dp, di_db[i]) = inp->i_blks[i];
183 if (inp->i_numblks > NDADDR)
184 for (i = 0; i < NIADDR; i++)
185 DIP(dp, di_ib[i]) = inp->i_blks[NDADDR + i];
186 curino.id_number = inp->i_number;
187 curino.id_parent = inp->i_parent;
188 (void)ckinode(dp, &curino);
191 * Now that the parents of all directories have been found,
192 * make another pass to verify the value of `..'
194 for (inpp = inpsort; inpp < inpend; inpp++) {
196 if (inp->i_parent == 0 || inp->i_isize == 0)
198 if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
199 inoinfo(inp->i_number)->ino_state == DSTATE)
200 inoinfo(inp->i_number)->ino_state = DFOUND;
201 if (inp->i_dotdot == inp->i_parent ||
202 inp->i_dotdot == (ino_t)-1)
204 if (inp->i_dotdot == 0) {
205 inp->i_dotdot = inp->i_parent;
206 fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
207 if (reply("FIX") == 0)
209 (void)makeentry(inp->i_number, inp->i_parent, "..");
210 inoinfo(inp->i_parent)->ino_linkcnt--;
213 fileerror(inp->i_parent, inp->i_number,
214 "BAD INODE NUMBER FOR '..'");
215 if (reply("FIX") == 0)
217 inoinfo(inp->i_dotdot)->ino_linkcnt++;
218 inoinfo(inp->i_parent)->ino_linkcnt--;
219 inp->i_dotdot = inp->i_parent;
220 (void)changeino(inp->i_number, "..", inp->i_parent);
223 * Mark all the directories that can be found from the root.
229 pass2check(struct inodesc *idesc)
231 struct direct *dirp = idesc->id_dirp;
233 int n, entrysize, ret = 0;
237 char namebuf[MAXPATHLEN + 1];
238 char pathbuf[MAXPATHLEN + 1];
243 if (idesc->id_entryno != 0)
245 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
246 if (dirp->d_ino != idesc->id_number) {
247 direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
248 dirp->d_ino = idesc->id_number;
249 if (reply("FIX") == 1)
252 if (dirp->d_type != DT_DIR) {
253 direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
254 dirp->d_type = DT_DIR;
255 if (reply("FIX") == 1)
260 direrror(idesc->id_number, "MISSING '.'");
261 proto.d_ino = idesc->id_number;
262 proto.d_type = DT_DIR;
264 (void)strcpy(proto.d_name, ".");
265 entrysize = DIRSIZ(0, &proto);
266 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
267 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
269 } else if (dirp->d_reclen < entrysize) {
270 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
271 } else if (dirp->d_reclen < 2 * entrysize) {
272 proto.d_reclen = dirp->d_reclen;
273 memmove(dirp, &proto, (size_t)entrysize);
274 if (reply("FIX") == 1)
277 n = dirp->d_reclen - entrysize;
278 proto.d_reclen = entrysize;
279 memmove(dirp, &proto, (size_t)entrysize);
281 inoinfo(dirp->d_ino)->ino_linkcnt--;
282 dirp = (struct direct *)((char *)(dirp) + entrysize);
283 memset(dirp, 0, (size_t)n);
285 if (reply("FIX") == 1)
289 if (idesc->id_entryno > 1)
291 inp = getinoinfo(idesc->id_number);
292 proto.d_ino = inp->i_parent;
293 proto.d_type = DT_DIR;
295 (void)strcpy(proto.d_name, "..");
296 entrysize = DIRSIZ(0, &proto);
297 if (idesc->id_entryno == 0) {
299 if (dirp->d_reclen < n + entrysize)
301 proto.d_reclen = dirp->d_reclen - n;
304 inoinfo(dirp->d_ino)->ino_linkcnt--;
305 dirp = (struct direct *)((char *)(dirp) + n);
306 memset(dirp, 0, (size_t)proto.d_reclen);
307 dirp->d_reclen = proto.d_reclen;
309 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
310 inp->i_dotdot = dirp->d_ino;
311 if (dirp->d_type != DT_DIR) {
312 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
313 dirp->d_type = DT_DIR;
314 if (reply("FIX") == 1)
319 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
320 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
321 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
323 inp->i_dotdot = (ino_t)-1;
324 } else if (dirp->d_reclen < entrysize) {
325 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
326 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
327 inp->i_dotdot = (ino_t)-1;
328 } else if (inp->i_parent != 0) {
330 * We know the parent, so fix now.
332 inp->i_dotdot = inp->i_parent;
333 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
334 proto.d_reclen = dirp->d_reclen;
335 memmove(dirp, &proto, (size_t)entrysize);
336 if (reply("FIX") == 1)
340 if (dirp->d_ino != 0)
341 inoinfo(dirp->d_ino)->ino_linkcnt--;
344 if (dirp->d_ino == 0)
346 if (dirp->d_namlen <= 2 &&
347 dirp->d_name[0] == '.' &&
348 idesc->id_entryno >= 2) {
349 if (dirp->d_namlen == 1) {
350 direrror(idesc->id_number, "EXTRA '.' ENTRY");
352 if (reply("FIX") == 1)
354 return (KEEPON | ret);
356 if (dirp->d_name[1] == '.') {
357 direrror(idesc->id_number, "EXTRA '..' ENTRY");
359 if (reply("FIX") == 1)
361 return (KEEPON | ret);
366 if (dirp->d_ino > maxino) {
367 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
369 } else if (((dirp->d_ino == WINO && dirp->d_type != DT_WHT) ||
370 (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
371 fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
373 dirp->d_type = DT_WHT;
374 if (reply("FIX") == 1)
378 switch (inoinfo(dirp->d_ino)->ino_state) {
380 if (idesc->id_entryno <= 2)
382 fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
388 if (idesc->id_entryno <= 2)
390 if (inoinfo(dirp->d_ino)->ino_state == FCLEAR)
392 else if (!preen && !usedsoftdep)
393 errmsg = "ZERO LENGTH DIRECTORY";
398 fileerror(idesc->id_number, dirp->d_ino, errmsg);
399 if ((n = reply("REMOVE")) == 1)
401 dp = ginode(dirp->d_ino);
402 inoinfo(dirp->d_ino)->ino_state =
403 (DIP(dp, di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE;
404 inoinfo(dirp->d_ino)->ino_linkcnt = DIP(dp, di_nlink);
408 if (inoinfo(idesc->id_number)->ino_state == DFOUND)
409 inoinfo(dirp->d_ino)->ino_state = DFOUND;
413 inp = getinoinfo(dirp->d_ino);
414 if (inp->i_parent != 0 && idesc->id_entryno > 2) {
415 getpathname(pathbuf, idesc->id_number,
417 getpathname(namebuf, dirp->d_ino, dirp->d_ino);
418 pwarn("%s%s%s %s %s\n", pathbuf,
419 (strcmp(pathbuf, "/") == 0 ? "" : "/"),
421 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
423 if (cursnapshot != 0)
426 printf(" (REMOVED)\n");
430 if ((n = reply("REMOVE")) == 1)
433 if (idesc->id_entryno > 2)
434 inp->i_parent = idesc->id_number;
438 if (dirp->d_type != inoinfo(dirp->d_ino)->ino_type) {
439 fileerror(idesc->id_number, dirp->d_ino,
441 dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
442 if (reply("FIX") == 1)
445 inoinfo(dirp->d_ino)->ino_linkcnt--;
449 errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
450 inoinfo(dirp->d_ino)->ino_state, dirp->d_ino);
456 return (ret|KEEPON|ALTERED);
460 * Routine to sort disk blocks.
463 blksort(const void *arg1, const void *arg2)
466 return ((*(struct inoinfo * const *)arg1)->i_blks[0] -
467 (*(struct inoinfo * const *)arg2)->i_blks[0]);