1*94715d8eSBen Gras /* $NetBSD: pass2.c,v 1.15 2009/10/19 18:41:08 bouyer Exp $ */
2*94715d8eSBen Gras
3*94715d8eSBen Gras /*
4*94715d8eSBen Gras * Copyright (c) 1980, 1986, 1993
5*94715d8eSBen Gras * The Regents of the University of California. All rights reserved.
6*94715d8eSBen Gras *
7*94715d8eSBen Gras * Redistribution and use in source and binary forms, with or without
8*94715d8eSBen Gras * modification, are permitted provided that the following conditions
9*94715d8eSBen Gras * are met:
10*94715d8eSBen Gras * 1. Redistributions of source code must retain the above copyright
11*94715d8eSBen Gras * notice, this list of conditions and the following disclaimer.
12*94715d8eSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13*94715d8eSBen Gras * notice, this list of conditions and the following disclaimer in the
14*94715d8eSBen Gras * documentation and/or other materials provided with the distribution.
15*94715d8eSBen Gras * 3. Neither the name of the University nor the names of its contributors
16*94715d8eSBen Gras * may be used to endorse or promote products derived from this software
17*94715d8eSBen Gras * without specific prior written permission.
18*94715d8eSBen Gras *
19*94715d8eSBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*94715d8eSBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*94715d8eSBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*94715d8eSBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*94715d8eSBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*94715d8eSBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*94715d8eSBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*94715d8eSBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*94715d8eSBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*94715d8eSBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*94715d8eSBen Gras * SUCH DAMAGE.
30*94715d8eSBen Gras */
31*94715d8eSBen Gras
32*94715d8eSBen Gras /*
33*94715d8eSBen Gras * Copyright (c) 1997 Manuel Bouyer.
34*94715d8eSBen Gras *
35*94715d8eSBen Gras * Redistribution and use in source and binary forms, with or without
36*94715d8eSBen Gras * modification, are permitted provided that the following conditions
37*94715d8eSBen Gras * are met:
38*94715d8eSBen Gras * 1. Redistributions of source code must retain the above copyright
39*94715d8eSBen Gras * notice, this list of conditions and the following disclaimer.
40*94715d8eSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
41*94715d8eSBen Gras * notice, this list of conditions and the following disclaimer in the
42*94715d8eSBen Gras * documentation and/or other materials provided with the distribution.
43*94715d8eSBen Gras *
44*94715d8eSBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45*94715d8eSBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46*94715d8eSBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47*94715d8eSBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48*94715d8eSBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49*94715d8eSBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50*94715d8eSBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51*94715d8eSBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52*94715d8eSBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53*94715d8eSBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54*94715d8eSBen Gras */
55*94715d8eSBen Gras
56*94715d8eSBen Gras #include <sys/cdefs.h>
57*94715d8eSBen Gras #ifndef lint
58*94715d8eSBen Gras #if 0
59*94715d8eSBen Gras static char sccsid[] = "@(#)pass2.c 8.6 (Berkeley) 10/27/94";
60*94715d8eSBen Gras #else
61*94715d8eSBen Gras __RCSID("$NetBSD: pass2.c,v 1.15 2009/10/19 18:41:08 bouyer Exp $");
62*94715d8eSBen Gras #endif
63*94715d8eSBen Gras #endif /* not lint */
64*94715d8eSBen Gras
65*94715d8eSBen Gras #include <sys/param.h>
66*94715d8eSBen Gras #include <sys/time.h>
67*94715d8eSBen Gras #include <ufs/ext2fs/ext2fs_dinode.h>
68*94715d8eSBen Gras #include <ufs/ext2fs/ext2fs_dir.h>
69*94715d8eSBen Gras #include <ufs/ext2fs/ext2fs.h>
70*94715d8eSBen Gras
71*94715d8eSBen Gras #include <ufs/ufs/dinode.h> /* for IFMT & friends */
72*94715d8eSBen Gras
73*94715d8eSBen Gras #include <stdio.h>
74*94715d8eSBen Gras #include <stdlib.h>
75*94715d8eSBen Gras #include <string.h>
76*94715d8eSBen Gras
77*94715d8eSBen Gras #include "fsck.h"
78*94715d8eSBen Gras #include "fsutil.h"
79*94715d8eSBen Gras #include "extern.h"
80*94715d8eSBen Gras #include "exitvalues.h"
81*94715d8eSBen Gras
82*94715d8eSBen Gras #define MINDIRSIZE (sizeof (struct ext2fs_dirtemplate))
83*94715d8eSBen Gras
84*94715d8eSBen Gras static int pass2check(struct inodesc *);
85*94715d8eSBen Gras static int blksort(const void *, const void *);
86*94715d8eSBen Gras
87*94715d8eSBen Gras void
pass2(void)88*94715d8eSBen Gras pass2(void)
89*94715d8eSBen Gras {
90*94715d8eSBen Gras struct ext2fs_dinode *dp;
91*94715d8eSBen Gras struct inoinfo **inpp, *inp;
92*94715d8eSBen Gras struct inoinfo **inpend;
93*94715d8eSBen Gras struct inodesc curino;
94*94715d8eSBen Gras struct ext2fs_dinode dino;
95*94715d8eSBen Gras char pathbuf[MAXPATHLEN + 1];
96*94715d8eSBen Gras
97*94715d8eSBen Gras switch (statemap[EXT2_ROOTINO]) {
98*94715d8eSBen Gras
99*94715d8eSBen Gras case USTATE:
100*94715d8eSBen Gras pfatal("ROOT INODE UNALLOCATED");
101*94715d8eSBen Gras if (reply("ALLOCATE") == 0)
102*94715d8eSBen Gras exit(FSCK_EXIT_CHECK_FAILED);
103*94715d8eSBen Gras if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
104*94715d8eSBen Gras errexit("CANNOT ALLOCATE ROOT INODE");
105*94715d8eSBen Gras break;
106*94715d8eSBen Gras
107*94715d8eSBen Gras case DCLEAR:
108*94715d8eSBen Gras pfatal("DUPS/BAD IN ROOT INODE");
109*94715d8eSBen Gras if (reply("REALLOCATE")) {
110*94715d8eSBen Gras freeino(EXT2_ROOTINO);
111*94715d8eSBen Gras if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
112*94715d8eSBen Gras errexit("CANNOT ALLOCATE ROOT INODE");
113*94715d8eSBen Gras break;
114*94715d8eSBen Gras }
115*94715d8eSBen Gras if (reply("CONTINUE") == 0)
116*94715d8eSBen Gras exit(FSCK_EXIT_CHECK_FAILED);
117*94715d8eSBen Gras break;
118*94715d8eSBen Gras
119*94715d8eSBen Gras case FSTATE:
120*94715d8eSBen Gras case FCLEAR:
121*94715d8eSBen Gras pfatal("ROOT INODE NOT DIRECTORY");
122*94715d8eSBen Gras if (reply("REALLOCATE")) {
123*94715d8eSBen Gras freeino(EXT2_ROOTINO);
124*94715d8eSBen Gras if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
125*94715d8eSBen Gras errexit("CANNOT ALLOCATE ROOT INODE");
126*94715d8eSBen Gras break;
127*94715d8eSBen Gras }
128*94715d8eSBen Gras if (reply("FIX") == 0)
129*94715d8eSBen Gras exit(FSCK_EXIT_CHECK_FAILED);
130*94715d8eSBen Gras dp = ginode(EXT2_ROOTINO);
131*94715d8eSBen Gras dp->e2di_mode = h2fs16((fs2h16(dp->e2di_mode) & ~IFMT) | IFDIR);
132*94715d8eSBen Gras inodirty();
133*94715d8eSBen Gras break;
134*94715d8eSBen Gras
135*94715d8eSBen Gras case DSTATE:
136*94715d8eSBen Gras break;
137*94715d8eSBen Gras
138*94715d8eSBen Gras default:
139*94715d8eSBen Gras errexit("BAD STATE %d FOR ROOT INODE", statemap[EXT2_ROOTINO]);
140*94715d8eSBen Gras }
141*94715d8eSBen Gras
142*94715d8eSBen Gras /*
143*94715d8eSBen Gras * Sort the directory list into disk block order.
144*94715d8eSBen Gras */
145*94715d8eSBen Gras qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
146*94715d8eSBen Gras /*
147*94715d8eSBen Gras * Check the integrity of each directory.
148*94715d8eSBen Gras */
149*94715d8eSBen Gras memset(&curino, 0, sizeof(struct inodesc));
150*94715d8eSBen Gras curino.id_type = DATA;
151*94715d8eSBen Gras curino.id_func = pass2check;
152*94715d8eSBen Gras inpend = &inpsort[inplast];
153*94715d8eSBen Gras for (inpp = inpsort; inpp < inpend; inpp++) {
154*94715d8eSBen Gras inp = *inpp;
155*94715d8eSBen Gras if (inp->i_isize == 0)
156*94715d8eSBen Gras continue;
157*94715d8eSBen Gras if (inp->i_isize < MINDIRSIZE) {
158*94715d8eSBen Gras direrror(inp->i_number, "DIRECTORY TOO SHORT");
159*94715d8eSBen Gras inp->i_isize = roundup(MINDIRSIZE, sblock.e2fs_bsize);
160*94715d8eSBen Gras if (reply("FIX") == 1) {
161*94715d8eSBen Gras dp = ginode(inp->i_number);
162*94715d8eSBen Gras inossize(dp, inp->i_isize);
163*94715d8eSBen Gras inodirty();
164*94715d8eSBen Gras }
165*94715d8eSBen Gras } else if ((inp->i_isize & (sblock.e2fs_bsize - 1)) != 0) {
166*94715d8eSBen Gras getpathname(pathbuf, sizeof(pathbuf), inp->i_number,
167*94715d8eSBen Gras inp->i_number);
168*94715d8eSBen Gras pwarn("DIRECTORY %s: LENGTH %lu NOT MULTIPLE OF %d",
169*94715d8eSBen Gras pathbuf, (u_long)inp->i_isize, sblock.e2fs_bsize);
170*94715d8eSBen Gras if (preen)
171*94715d8eSBen Gras printf(" (ADJUSTED)\n");
172*94715d8eSBen Gras inp->i_isize = roundup(inp->i_isize, sblock.e2fs_bsize);
173*94715d8eSBen Gras if (preen || reply("ADJUST") == 1) {
174*94715d8eSBen Gras dp = ginode(inp->i_number);
175*94715d8eSBen Gras inossize(dp, inp->i_isize);
176*94715d8eSBen Gras inodirty();
177*94715d8eSBen Gras }
178*94715d8eSBen Gras }
179*94715d8eSBen Gras memset(&dino, 0, sizeof(struct ext2fs_dinode));
180*94715d8eSBen Gras dino.e2di_mode = h2fs16(IFDIR);
181*94715d8eSBen Gras inossize(&dino, inp->i_isize);
182*94715d8eSBen Gras memcpy(&dino.e2di_blocks[0], &inp->i_blks[0], (size_t)inp->i_numblks);
183*94715d8eSBen Gras curino.id_number = inp->i_number;
184*94715d8eSBen Gras curino.id_parent = inp->i_parent;
185*94715d8eSBen Gras (void)ckinode(&dino, &curino);
186*94715d8eSBen Gras }
187*94715d8eSBen Gras /*
188*94715d8eSBen Gras * Now that the parents of all directories have been found,
189*94715d8eSBen Gras * make another pass to verify the value of `..'
190*94715d8eSBen Gras */
191*94715d8eSBen Gras for (inpp = inpsort; inpp < inpend; inpp++) {
192*94715d8eSBen Gras inp = *inpp;
193*94715d8eSBen Gras if (inp->i_parent == 0 || inp->i_isize == 0)
194*94715d8eSBen Gras continue;
195*94715d8eSBen Gras if (inp->i_dotdot == inp->i_parent ||
196*94715d8eSBen Gras inp->i_dotdot == (ino_t)-1)
197*94715d8eSBen Gras continue;
198*94715d8eSBen Gras if (inp->i_dotdot == 0) {
199*94715d8eSBen Gras inp->i_dotdot = inp->i_parent;
200*94715d8eSBen Gras fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
201*94715d8eSBen Gras if (reply("FIX") == 0)
202*94715d8eSBen Gras continue;
203*94715d8eSBen Gras (void)makeentry(inp->i_number, inp->i_parent, "..");
204*94715d8eSBen Gras lncntp[inp->i_parent]--;
205*94715d8eSBen Gras continue;
206*94715d8eSBen Gras }
207*94715d8eSBen Gras fileerror(inp->i_parent, inp->i_number,
208*94715d8eSBen Gras "BAD INODE NUMBER FOR '..'");
209*94715d8eSBen Gras if (reply("FIX") == 0)
210*94715d8eSBen Gras continue;
211*94715d8eSBen Gras lncntp[inp->i_dotdot]++;
212*94715d8eSBen Gras lncntp[inp->i_parent]--;
213*94715d8eSBen Gras inp->i_dotdot = inp->i_parent;
214*94715d8eSBen Gras (void)changeino(inp->i_number, "..", inp->i_parent);
215*94715d8eSBen Gras }
216*94715d8eSBen Gras /*
217*94715d8eSBen Gras * Mark all the directories that can be found from the root.
218*94715d8eSBen Gras */
219*94715d8eSBen Gras propagate();
220*94715d8eSBen Gras }
221*94715d8eSBen Gras
222*94715d8eSBen Gras static int
pass2check(struct inodesc * idesc)223*94715d8eSBen Gras pass2check(struct inodesc *idesc)
224*94715d8eSBen Gras {
225*94715d8eSBen Gras struct ext2fs_direct *dirp = idesc->id_dirp;
226*94715d8eSBen Gras struct inoinfo *inp;
227*94715d8eSBen Gras int n, entrysize, ret = 0;
228*94715d8eSBen Gras struct ext2fs_dinode *dp;
229*94715d8eSBen Gras const char *errmsg;
230*94715d8eSBen Gras struct ext2fs_direct proto;
231*94715d8eSBen Gras char namebuf[MAXPATHLEN + 1];
232*94715d8eSBen Gras char pathbuf[MAXPATHLEN + 1];
233*94715d8eSBen Gras
234*94715d8eSBen Gras /*
235*94715d8eSBen Gras * check for "."
236*94715d8eSBen Gras */
237*94715d8eSBen Gras if (idesc->id_entryno != 0)
238*94715d8eSBen Gras goto chk1;
239*94715d8eSBen Gras if (fs2h32(dirp->e2d_ino) != 0 && dirp->e2d_namlen == 1 &&
240*94715d8eSBen Gras dirp->e2d_name[0] == '.') {
241*94715d8eSBen Gras if (fs2h32(dirp->e2d_ino) != idesc->id_number) {
242*94715d8eSBen Gras direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
243*94715d8eSBen Gras dirp->e2d_ino = h2fs32(idesc->id_number);
244*94715d8eSBen Gras if (reply("FIX") == 1)
245*94715d8eSBen Gras ret |= ALTERED;
246*94715d8eSBen Gras }
247*94715d8eSBen Gras if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
248*94715d8eSBen Gras (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)
249*94715d8eSBen Gras && (dirp->e2d_type != EXT2_FT_DIR)) {
250*94715d8eSBen Gras direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
251*94715d8eSBen Gras dirp->e2d_type = EXT2_FT_DIR;
252*94715d8eSBen Gras if (reply("FIX") == 1)
253*94715d8eSBen Gras ret |= ALTERED;
254*94715d8eSBen Gras }
255*94715d8eSBen Gras goto chk1;
256*94715d8eSBen Gras }
257*94715d8eSBen Gras direrror(idesc->id_number, "MISSING '.'");
258*94715d8eSBen Gras proto.e2d_ino = h2fs32(idesc->id_number);
259*94715d8eSBen Gras proto.e2d_namlen = 1;
260*94715d8eSBen Gras if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
261*94715d8eSBen Gras (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
262*94715d8eSBen Gras proto.e2d_type = EXT2_FT_DIR;
263*94715d8eSBen Gras else
264*94715d8eSBen Gras proto.e2d_type = 0;
265*94715d8eSBen Gras (void)strlcpy(proto.e2d_name, ".", sizeof(proto.e2d_name));
266*94715d8eSBen Gras entrysize = EXT2FS_DIRSIZ(proto.e2d_namlen);
267*94715d8eSBen Gras if (fs2h32(dirp->e2d_ino) != 0 && strcmp(dirp->e2d_name, "..") != 0) {
268*94715d8eSBen Gras pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
269*94715d8eSBen Gras dirp->e2d_name);
270*94715d8eSBen Gras } else if (fs2h16(dirp->e2d_reclen) < entrysize) {
271*94715d8eSBen Gras pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
272*94715d8eSBen Gras } else if (fs2h16(dirp->e2d_reclen) < 2 * entrysize) {
273*94715d8eSBen Gras proto.e2d_reclen = dirp->e2d_reclen;
274*94715d8eSBen Gras memcpy(dirp, &proto, (size_t)entrysize);
275*94715d8eSBen Gras if (reply("FIX") == 1)
276*94715d8eSBen Gras ret |= ALTERED;
277*94715d8eSBen Gras } else {
278*94715d8eSBen Gras n = fs2h16(dirp->e2d_reclen) - entrysize;
279*94715d8eSBen Gras proto.e2d_reclen = h2fs16(entrysize);
280*94715d8eSBen Gras memcpy(dirp, &proto, (size_t)entrysize);
281*94715d8eSBen Gras idesc->id_entryno++;
282*94715d8eSBen Gras lncntp[fs2h32(dirp->e2d_ino)]--;
283*94715d8eSBen Gras dirp = (struct ext2fs_direct *)((char *)(dirp) + entrysize);
284*94715d8eSBen Gras memset(dirp, 0, (size_t)n);
285*94715d8eSBen Gras dirp->e2d_reclen = h2fs16(n);
286*94715d8eSBen Gras if (reply("FIX") == 1)
287*94715d8eSBen Gras ret |= ALTERED;
288*94715d8eSBen Gras }
289*94715d8eSBen Gras chk1:
290*94715d8eSBen Gras if (idesc->id_entryno > 1)
291*94715d8eSBen Gras goto chk2;
292*94715d8eSBen Gras inp = getinoinfo(idesc->id_number);
293*94715d8eSBen Gras proto.e2d_ino = h2fs32(inp->i_parent);
294*94715d8eSBen Gras proto.e2d_namlen = 2;
295*94715d8eSBen Gras if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
296*94715d8eSBen Gras (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
297*94715d8eSBen Gras proto.e2d_type = EXT2_FT_DIR;
298*94715d8eSBen Gras else
299*94715d8eSBen Gras proto.e2d_type = 0;
300*94715d8eSBen Gras (void)strlcpy(proto.e2d_name, "..", sizeof(proto.e2d_name));
301*94715d8eSBen Gras entrysize = EXT2FS_DIRSIZ(2);
302*94715d8eSBen Gras if (idesc->id_entryno == 0) {
303*94715d8eSBen Gras n = EXT2FS_DIRSIZ(dirp->e2d_namlen);
304*94715d8eSBen Gras if (fs2h16(dirp->e2d_reclen) < n + entrysize)
305*94715d8eSBen Gras goto chk2;
306*94715d8eSBen Gras proto.e2d_reclen = h2fs16(fs2h16(dirp->e2d_reclen) - n);
307*94715d8eSBen Gras dirp->e2d_reclen = h2fs16(n);
308*94715d8eSBen Gras idesc->id_entryno++;
309*94715d8eSBen Gras lncntp[fs2h32(dirp->e2d_ino)]--;
310*94715d8eSBen Gras dirp = (struct ext2fs_direct *)((char *)(dirp) + n);
311*94715d8eSBen Gras memset(dirp, 0, (size_t)fs2h16(proto.e2d_reclen));
312*94715d8eSBen Gras dirp->e2d_reclen = proto.e2d_reclen;
313*94715d8eSBen Gras }
314*94715d8eSBen Gras if (fs2h32(dirp->e2d_ino) != 0 &&
315*94715d8eSBen Gras dirp->e2d_namlen == 2 &&
316*94715d8eSBen Gras strncmp(dirp->e2d_name, "..", 2) == 0) {
317*94715d8eSBen Gras inp->i_dotdot = fs2h32(dirp->e2d_ino);
318*94715d8eSBen Gras if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
319*94715d8eSBen Gras (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)
320*94715d8eSBen Gras && dirp->e2d_type != EXT2_FT_DIR) {
321*94715d8eSBen Gras direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
322*94715d8eSBen Gras dirp->e2d_type = EXT2_FT_DIR;
323*94715d8eSBen Gras if (reply("FIX") == 1)
324*94715d8eSBen Gras ret |= ALTERED;
325*94715d8eSBen Gras }
326*94715d8eSBen Gras goto chk2;
327*94715d8eSBen Gras }
328*94715d8eSBen Gras if (fs2h32(dirp->e2d_ino) != 0 &&
329*94715d8eSBen Gras dirp->e2d_namlen == 1 &&
330*94715d8eSBen Gras strncmp(dirp->e2d_name, ".", 1) != 0) {
331*94715d8eSBen Gras fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
332*94715d8eSBen Gras pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
333*94715d8eSBen Gras dirp->e2d_name);
334*94715d8eSBen Gras inp->i_dotdot = (ino_t)-1;
335*94715d8eSBen Gras } else if (fs2h16(dirp->e2d_reclen) < entrysize) {
336*94715d8eSBen Gras fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
337*94715d8eSBen Gras pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
338*94715d8eSBen Gras inp->i_dotdot = (ino_t)-1;
339*94715d8eSBen Gras } else if (inp->i_parent != 0) {
340*94715d8eSBen Gras /*
341*94715d8eSBen Gras * We know the parent, so fix now.
342*94715d8eSBen Gras */
343*94715d8eSBen Gras inp->i_dotdot = inp->i_parent;
344*94715d8eSBen Gras fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
345*94715d8eSBen Gras proto.e2d_reclen = dirp->e2d_reclen;
346*94715d8eSBen Gras memcpy(dirp, &proto, (size_t)entrysize);
347*94715d8eSBen Gras if (reply("FIX") == 1)
348*94715d8eSBen Gras ret |= ALTERED;
349*94715d8eSBen Gras }
350*94715d8eSBen Gras idesc->id_entryno++;
351*94715d8eSBen Gras if (fs2h32(dirp->e2d_ino) != 0)
352*94715d8eSBen Gras lncntp[fs2h32(dirp->e2d_ino)]--;
353*94715d8eSBen Gras return (ret|KEEPON);
354*94715d8eSBen Gras chk2:
355*94715d8eSBen Gras if (fs2h32(dirp->e2d_ino) == 0)
356*94715d8eSBen Gras return (ret|KEEPON);
357*94715d8eSBen Gras if (dirp->e2d_namlen <= 2 &&
358*94715d8eSBen Gras dirp->e2d_name[0] == '.' &&
359*94715d8eSBen Gras idesc->id_entryno >= 2) {
360*94715d8eSBen Gras if (dirp->e2d_namlen == 1) {
361*94715d8eSBen Gras direrror(idesc->id_number, "EXTRA '.' ENTRY");
362*94715d8eSBen Gras dirp->e2d_ino = 0;
363*94715d8eSBen Gras if (reply("FIX") == 1)
364*94715d8eSBen Gras ret |= ALTERED;
365*94715d8eSBen Gras return (KEEPON | ret);
366*94715d8eSBen Gras }
367*94715d8eSBen Gras if (dirp->e2d_name[1] == '.') {
368*94715d8eSBen Gras direrror(idesc->id_number, "EXTRA '..' ENTRY");
369*94715d8eSBen Gras dirp->e2d_ino = 0;
370*94715d8eSBen Gras if (reply("FIX") == 1)
371*94715d8eSBen Gras ret |= ALTERED;
372*94715d8eSBen Gras return (KEEPON | ret);
373*94715d8eSBen Gras }
374*94715d8eSBen Gras }
375*94715d8eSBen Gras idesc->id_entryno++;
376*94715d8eSBen Gras n = 0;
377*94715d8eSBen Gras if (fs2h32(dirp->e2d_ino) > maxino ||
378*94715d8eSBen Gras (fs2h32(dirp->e2d_ino) < EXT2_FIRSTINO &&
379*94715d8eSBen Gras fs2h32(dirp->e2d_ino) != EXT2_ROOTINO)) {
380*94715d8eSBen Gras fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), "I OUT OF RANGE");
381*94715d8eSBen Gras n = reply("REMOVE");
382*94715d8eSBen Gras } else {
383*94715d8eSBen Gras again:
384*94715d8eSBen Gras switch (statemap[fs2h32(dirp->e2d_ino)]) {
385*94715d8eSBen Gras case USTATE:
386*94715d8eSBen Gras if (idesc->id_entryno <= 2)
387*94715d8eSBen Gras break;
388*94715d8eSBen Gras fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), "UNALLOCATED");
389*94715d8eSBen Gras n = reply("REMOVE");
390*94715d8eSBen Gras break;
391*94715d8eSBen Gras
392*94715d8eSBen Gras case DCLEAR:
393*94715d8eSBen Gras case FCLEAR:
394*94715d8eSBen Gras if (idesc->id_entryno <= 2)
395*94715d8eSBen Gras break;
396*94715d8eSBen Gras if (statemap[fs2h32(dirp->e2d_ino)] == FCLEAR)
397*94715d8eSBen Gras errmsg = "DUP/BAD";
398*94715d8eSBen Gras else if (!preen)
399*94715d8eSBen Gras errmsg = "ZERO LENGTH DIRECTORY";
400*94715d8eSBen Gras else {
401*94715d8eSBen Gras n = 1;
402*94715d8eSBen Gras break;
403*94715d8eSBen Gras }
404*94715d8eSBen Gras fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), errmsg);
405*94715d8eSBen Gras if ((n = reply("REMOVE")) == 1)
406*94715d8eSBen Gras break;
407*94715d8eSBen Gras dp = ginode(fs2h32(dirp->e2d_ino));
408*94715d8eSBen Gras statemap[fs2h32(dirp->e2d_ino)] =
409*94715d8eSBen Gras (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE;
410*94715d8eSBen Gras lncntp[fs2h32(dirp->e2d_ino)] = fs2h16(dp->e2di_nlink);
411*94715d8eSBen Gras goto again;
412*94715d8eSBen Gras
413*94715d8eSBen Gras case DSTATE:
414*94715d8eSBen Gras case DFOUND:
415*94715d8eSBen Gras inp = getinoinfo(fs2h32(dirp->e2d_ino));
416*94715d8eSBen Gras if (inp->i_parent != 0 && idesc->id_entryno > 2) {
417*94715d8eSBen Gras getpathname(pathbuf, sizeof(pathbuf),
418*94715d8eSBen Gras idesc->id_number, idesc->id_number);
419*94715d8eSBen Gras getpathname(namebuf, sizeof(namebuf),
420*94715d8eSBen Gras fs2h32(dirp->e2d_ino),
421*94715d8eSBen Gras fs2h32(dirp->e2d_ino));
422*94715d8eSBen Gras pwarn("%s %s %s\n", pathbuf,
423*94715d8eSBen Gras "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
424*94715d8eSBen Gras namebuf);
425*94715d8eSBen Gras if (preen)
426*94715d8eSBen Gras printf(" (IGNORED)\n");
427*94715d8eSBen Gras else if ((n = reply("REMOVE")) == 1)
428*94715d8eSBen Gras break;
429*94715d8eSBen Gras }
430*94715d8eSBen Gras if (idesc->id_entryno > 2)
431*94715d8eSBen Gras inp->i_parent = idesc->id_number;
432*94715d8eSBen Gras /* fall through */
433*94715d8eSBen Gras
434*94715d8eSBen Gras case FSTATE:
435*94715d8eSBen Gras if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
436*94715d8eSBen Gras (sblock.e2fs.e2fs_features_incompat &
437*94715d8eSBen Gras EXT2F_INCOMPAT_FTYPE) &&
438*94715d8eSBen Gras dirp->e2d_type !=
439*94715d8eSBen Gras inot2ext2dt(typemap[fs2h32(dirp->e2d_ino)])) {
440*94715d8eSBen Gras dirp->e2d_type =
441*94715d8eSBen Gras inot2ext2dt(typemap[fs2h32(dirp->e2d_ino)]);
442*94715d8eSBen Gras fileerror(idesc->id_number,
443*94715d8eSBen Gras fs2h32(dirp->e2d_ino),
444*94715d8eSBen Gras "BAD TYPE VALUE");
445*94715d8eSBen Gras if (reply("FIX") == 1)
446*94715d8eSBen Gras ret |= ALTERED;
447*94715d8eSBen Gras }
448*94715d8eSBen Gras lncntp[fs2h32(dirp->e2d_ino)]--;
449*94715d8eSBen Gras break;
450*94715d8eSBen Gras
451*94715d8eSBen Gras default:
452*94715d8eSBen Gras errexit("BAD STATE %d FOR INODE I=%d",
453*94715d8eSBen Gras statemap[fs2h32(dirp->e2d_ino)], fs2h32(dirp->e2d_ino));
454*94715d8eSBen Gras }
455*94715d8eSBen Gras }
456*94715d8eSBen Gras if (n == 0)
457*94715d8eSBen Gras return (ret|KEEPON);
458*94715d8eSBen Gras dirp->e2d_ino = 0;
459*94715d8eSBen Gras return (ret|KEEPON|ALTERED);
460*94715d8eSBen Gras }
461*94715d8eSBen Gras
462*94715d8eSBen Gras /*
463*94715d8eSBen Gras * Routine to sort disk blocks.
464*94715d8eSBen Gras */
465*94715d8eSBen Gras static int
blksort(const void * inpp1,const void * inpp2)466*94715d8eSBen Gras blksort(const void *inpp1, const void *inpp2)
467*94715d8eSBen Gras {
468*94715d8eSBen Gras return ((* (const struct inoinfo * const*) inpp1)->i_blks[0] -
469*94715d8eSBen Gras (* (const struct inoinfo * const*) inpp2)->i_blks[0]);
470*94715d8eSBen Gras }
471