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