dir.c revision 1.9 1 1.9 bouyer /* $NetBSD: dir.c,v 1.9 2003/10/05 17:48:49 bouyer Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1980, 1986, 1993
5 1.1 bouyer * The Regents of the University of California. All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.8 agc * 3. Neither the name of the University nor the names of its contributors
16 1.8 agc * may be used to endorse or promote products derived from this software
17 1.8 agc * without specific prior written permission.
18 1.8 agc *
19 1.8 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.8 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.8 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.8 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.8 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.8 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.8 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.8 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.8 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.8 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.8 agc * SUCH DAMAGE.
30 1.8 agc */
31 1.8 agc
32 1.8 agc /*
33 1.8 agc * Copyright (c) 1997 Manuel Bouyer.
34 1.8 agc *
35 1.8 agc * Redistribution and use in source and binary forms, with or without
36 1.8 agc * modification, are permitted provided that the following conditions
37 1.8 agc * are met:
38 1.8 agc * 1. Redistributions of source code must retain the above copyright
39 1.8 agc * notice, this list of conditions and the following disclaimer.
40 1.8 agc * 2. Redistributions in binary form must reproduce the above copyright
41 1.8 agc * notice, this list of conditions and the following disclaimer in the
42 1.8 agc * documentation and/or other materials provided with the distribution.
43 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
44 1.1 bouyer * must display the following acknowledgement:
45 1.9 bouyer * This product includes software developed by Manuel Bouyer.
46 1.1 bouyer * California, Berkeley and its contributors.
47 1.9 bouyer * 4. The name of the author may not be used to endorse or promote products
48 1.9 bouyer * derived from this software without specific prior written permission.
49 1.1 bouyer *
50 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 1.1 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 1.1 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 1.1 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 1.1 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 1.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 1.1 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 1.1 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 1.1 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 1.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 1.1 bouyer * SUCH DAMAGE.
61 1.1 bouyer */
62 1.1 bouyer
63 1.2 lukem #include <sys/cdefs.h>
64 1.1 bouyer #ifndef lint
65 1.1 bouyer #if 0
66 1.1 bouyer static char sccsid[] = "@(#)dir.c 8.5 (Berkeley) 12/8/94";
67 1.1 bouyer #else
68 1.9 bouyer __RCSID("$NetBSD: dir.c,v 1.9 2003/10/05 17:48:49 bouyer Exp $");
69 1.1 bouyer #endif
70 1.1 bouyer #endif /* not lint */
71 1.1 bouyer
72 1.1 bouyer #include <sys/param.h>
73 1.1 bouyer #include <sys/time.h>
74 1.4 bouyer #include <ufs/ufs/dir.h>
75 1.1 bouyer #include <ufs/ext2fs/ext2fs_dinode.h>
76 1.1 bouyer #include <ufs/ext2fs/ext2fs_dir.h>
77 1.1 bouyer #include <ufs/ext2fs/ext2fs.h>
78 1.1 bouyer
79 1.1 bouyer #include <ufs/ufs/dinode.h> /* for IFMT & friends */
80 1.1 bouyer
81 1.1 bouyer #include <stdio.h>
82 1.1 bouyer #include <stdlib.h>
83 1.1 bouyer #include <string.h>
84 1.1 bouyer
85 1.1 bouyer #include "fsck.h"
86 1.1 bouyer #include "fsutil.h"
87 1.1 bouyer #include "extern.h"
88 1.1 bouyer
89 1.1 bouyer char *lfname = "lost+found";
90 1.1 bouyer int lfmode = 01777;
91 1.1 bouyer struct ext2fs_dirtemplate emptydir = { 0, DIRBLKSIZ };
92 1.1 bouyer struct ext2fs_dirtemplate dirhead = {
93 1.5 bouyer 0, 12, 1, EXT2_FT_DIR, ".",
94 1.5 bouyer 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
95 1.1 bouyer };
96 1.1 bouyer #undef DIRBLKSIZ
97 1.1 bouyer
98 1.1 bouyer static int expanddir __P((struct ext2fs_dinode *, char *));
99 1.1 bouyer static void freedir __P((ino_t, ino_t));
100 1.1 bouyer static struct ext2fs_direct *fsck_readdir __P((struct inodesc *));
101 1.1 bouyer static struct bufarea *getdirblk __P((daddr_t, long));
102 1.1 bouyer static int lftempname __P((char *, ino_t));
103 1.1 bouyer static int mkentry __P((struct inodesc *));
104 1.1 bouyer static int chgino __P((struct inodesc *));
105 1.1 bouyer
106 1.1 bouyer /*
107 1.1 bouyer * Propagate connected state through the tree.
108 1.1 bouyer */
109 1.1 bouyer void
110 1.1 bouyer propagate()
111 1.1 bouyer {
112 1.2 lukem struct inoinfo **inpp, *inp, *pinp;
113 1.1 bouyer struct inoinfo **inpend;
114 1.1 bouyer
115 1.1 bouyer /*
116 1.1 bouyer * Create a list of children for each directory.
117 1.1 bouyer */
118 1.1 bouyer inpend = &inpsort[inplast];
119 1.1 bouyer for (inpp = inpsort; inpp < inpend; inpp++) {
120 1.1 bouyer inp = *inpp;
121 1.1 bouyer if (inp->i_parent == 0 ||
122 1.1 bouyer inp->i_number == EXT2_ROOTINO)
123 1.1 bouyer continue;
124 1.1 bouyer pinp = getinoinfo(inp->i_parent);
125 1.1 bouyer inp->i_parentp = pinp;
126 1.1 bouyer inp->i_sibling = pinp->i_child;
127 1.1 bouyer pinp->i_child = inp;
128 1.1 bouyer }
129 1.1 bouyer inp = getinoinfo(EXT2_ROOTINO);
130 1.1 bouyer while (inp) {
131 1.1 bouyer statemap[inp->i_number] = DFOUND;
132 1.1 bouyer if (inp->i_child &&
133 1.1 bouyer statemap[inp->i_child->i_number] == DSTATE)
134 1.1 bouyer inp = inp->i_child;
135 1.1 bouyer else if (inp->i_sibling)
136 1.1 bouyer inp = inp->i_sibling;
137 1.1 bouyer else
138 1.1 bouyer inp = inp->i_parentp;
139 1.1 bouyer }
140 1.1 bouyer }
141 1.1 bouyer
142 1.1 bouyer /*
143 1.1 bouyer * Scan each entry in a directory block.
144 1.1 bouyer */
145 1.1 bouyer int
146 1.1 bouyer dirscan(idesc)
147 1.2 lukem struct inodesc *idesc;
148 1.1 bouyer {
149 1.2 lukem struct ext2fs_direct *dp;
150 1.2 lukem struct bufarea *bp;
151 1.1 bouyer int dsize, n;
152 1.1 bouyer long blksiz;
153 1.1 bouyer char *dbuf = NULL;
154 1.1 bouyer
155 1.1 bouyer if ((dbuf = malloc(sblock.e2fs_bsize)) == NULL) {
156 1.1 bouyer fprintf(stderr, "out of memory");
157 1.1 bouyer exit(8);
158 1.1 bouyer }
159 1.1 bouyer
160 1.1 bouyer if (idesc->id_type != DATA)
161 1.1 bouyer errexit("wrong type to dirscan %d\n", idesc->id_type);
162 1.1 bouyer if (idesc->id_entryno == 0 &&
163 1.1 bouyer (idesc->id_filesize & (sblock.e2fs_bsize - 1)) != 0)
164 1.1 bouyer idesc->id_filesize = roundup(idesc->id_filesize, sblock.e2fs_bsize);
165 1.1 bouyer blksiz = idesc->id_numfrags * sblock.e2fs_bsize;
166 1.1 bouyer if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
167 1.1 bouyer idesc->id_filesize -= blksiz;
168 1.1 bouyer return (SKIP);
169 1.1 bouyer }
170 1.1 bouyer idesc->id_loc = 0;
171 1.1 bouyer for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
172 1.3 bouyer dsize = fs2h16(dp->e2d_reclen);
173 1.1 bouyer memcpy(dbuf, dp, (size_t)dsize);
174 1.1 bouyer idesc->id_dirp = (struct ext2fs_direct *)dbuf;
175 1.1 bouyer if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
176 1.1 bouyer bp = getdirblk(idesc->id_blkno, blksiz);
177 1.1 bouyer memcpy(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
178 1.1 bouyer (size_t)dsize);
179 1.1 bouyer dirty(bp);
180 1.1 bouyer sbdirty();
181 1.1 bouyer }
182 1.1 bouyer if (n & STOP) {
183 1.1 bouyer free(dbuf);
184 1.1 bouyer return (n);
185 1.1 bouyer }
186 1.1 bouyer }
187 1.1 bouyer free(dbuf);
188 1.1 bouyer return (idesc->id_filesize > 0 ? KEEPON : STOP);
189 1.1 bouyer }
190 1.1 bouyer
191 1.1 bouyer /*
192 1.1 bouyer * get next entry in a directory.
193 1.1 bouyer */
194 1.1 bouyer static struct ext2fs_direct *
195 1.1 bouyer fsck_readdir(idesc)
196 1.2 lukem struct inodesc *idesc;
197 1.1 bouyer {
198 1.2 lukem struct ext2fs_direct *dp, *ndp;
199 1.2 lukem struct bufarea *bp;
200 1.1 bouyer long size, blksiz, fix, dploc;
201 1.1 bouyer
202 1.1 bouyer blksiz = idesc->id_numfrags * sblock.e2fs_bsize;
203 1.1 bouyer bp = getdirblk(idesc->id_blkno, blksiz);
204 1.1 bouyer if (idesc->id_loc % sblock.e2fs_bsize == 0 && idesc->id_filesize > 0 &&
205 1.1 bouyer idesc->id_loc < blksiz) {
206 1.1 bouyer dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
207 1.1 bouyer if (dircheck(idesc, dp))
208 1.1 bouyer goto dpok;
209 1.1 bouyer if (idesc->id_fix == IGNORE)
210 1.1 bouyer return (0);
211 1.1 bouyer fix = dofix(idesc, "DIRECTORY CORRUPTED");
212 1.1 bouyer bp = getdirblk(idesc->id_blkno, blksiz);
213 1.1 bouyer dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
214 1.3 bouyer dp->e2d_reclen = h2fs16(sblock.e2fs_bsize);
215 1.1 bouyer dp->e2d_ino = 0;
216 1.1 bouyer dp->e2d_namlen = 0;
217 1.4 bouyer dp->e2d_type = 0;
218 1.1 bouyer dp->e2d_name[0] = '\0';
219 1.1 bouyer if (fix)
220 1.1 bouyer dirty(bp);
221 1.1 bouyer idesc->id_loc += sblock.e2fs_bsize;
222 1.1 bouyer idesc->id_filesize -= sblock.e2fs_bsize;
223 1.1 bouyer return (dp);
224 1.1 bouyer }
225 1.1 bouyer dpok:
226 1.1 bouyer if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
227 1.1 bouyer return NULL;
228 1.1 bouyer dploc = idesc->id_loc;
229 1.1 bouyer dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc);
230 1.3 bouyer idesc->id_loc += fs2h16(dp->e2d_reclen);
231 1.3 bouyer idesc->id_filesize -= fs2h16(dp->e2d_reclen);
232 1.1 bouyer if ((idesc->id_loc % sblock.e2fs_bsize) == 0)
233 1.1 bouyer return (dp);
234 1.1 bouyer ndp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
235 1.1 bouyer if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
236 1.1 bouyer dircheck(idesc, ndp) == 0) {
237 1.1 bouyer size = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize);
238 1.1 bouyer idesc->id_loc += size;
239 1.1 bouyer idesc->id_filesize -= size;
240 1.1 bouyer if (idesc->id_fix == IGNORE)
241 1.1 bouyer return (0);
242 1.1 bouyer fix = dofix(idesc, "DIRECTORY CORRUPTED");
243 1.1 bouyer bp = getdirblk(idesc->id_blkno, blksiz);
244 1.1 bouyer dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc);
245 1.3 bouyer dp->e2d_reclen = h2fs16(fs2h16(dp->e2d_reclen) + size);
246 1.1 bouyer if (fix)
247 1.1 bouyer dirty(bp);
248 1.1 bouyer }
249 1.1 bouyer return (dp);
250 1.1 bouyer }
251 1.1 bouyer
252 1.1 bouyer /*
253 1.1 bouyer * Verify that a directory entry is valid.
254 1.1 bouyer * This is a superset of the checks made in the kernel.
255 1.1 bouyer */
256 1.1 bouyer int
257 1.1 bouyer dircheck(idesc, dp)
258 1.1 bouyer struct inodesc *idesc;
259 1.1 bouyer struct ext2fs_direct *dp;
260 1.1 bouyer {
261 1.1 bouyer int size;
262 1.1 bouyer char *cp;
263 1.1 bouyer int spaceleft;
264 1.3 bouyer u_int16_t reclen = fs2h16(dp->e2d_reclen);
265 1.1 bouyer
266 1.1 bouyer spaceleft = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize);
267 1.3 bouyer if (fs2h32(dp->e2d_ino) > maxino ||
268 1.3 bouyer reclen == 0 ||
269 1.3 bouyer reclen > spaceleft ||
270 1.3 bouyer (reclen & 0x3) != 0)
271 1.1 bouyer return (0);
272 1.1 bouyer if (dp->e2d_ino == 0)
273 1.1 bouyer return (1);
274 1.5 bouyer if (sblock.e2fs.e2fs_rev < E2FS_REV1 ||
275 1.4 bouyer (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) == 0)
276 1.4 bouyer if (dp->e2d_type != 0)
277 1.4 bouyer return (1);
278 1.4 bouyer size = EXT2FS_DIRSIZ(dp->e2d_namlen);
279 1.3 bouyer if (reclen < size ||
280 1.6 simonb idesc->id_filesize < size /* ||
281 1.6 simonb dp->e2d_namlen > EXT2FS_MAXNAMLEN */)
282 1.1 bouyer return (0);
283 1.4 bouyer for (cp = dp->e2d_name, size = 0; size < dp->e2d_namlen; size++)
284 1.1 bouyer if (*cp == '\0' || (*cp++ == '/'))
285 1.1 bouyer return (0);
286 1.1 bouyer return (1);
287 1.1 bouyer }
288 1.1 bouyer
289 1.1 bouyer void
290 1.1 bouyer direrror(ino, errmesg)
291 1.1 bouyer ino_t ino;
292 1.1 bouyer char *errmesg;
293 1.1 bouyer {
294 1.1 bouyer
295 1.1 bouyer fileerror(ino, ino, errmesg);
296 1.1 bouyer }
297 1.1 bouyer
298 1.1 bouyer void
299 1.1 bouyer fileerror(cwd, ino, errmesg)
300 1.1 bouyer ino_t cwd, ino;
301 1.1 bouyer char *errmesg;
302 1.1 bouyer {
303 1.2 lukem struct ext2fs_dinode *dp;
304 1.1 bouyer char pathbuf[MAXPATHLEN + 1];
305 1.1 bouyer
306 1.1 bouyer pwarn("%s ", errmesg);
307 1.1 bouyer pinode(ino);
308 1.1 bouyer printf("\n");
309 1.7 itojun getpathname(pathbuf, sizeof(pathbuf), cwd, ino);
310 1.1 bouyer if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino) {
311 1.1 bouyer pfatal("NAME=%s\n", pathbuf);
312 1.1 bouyer return;
313 1.1 bouyer }
314 1.1 bouyer dp = ginode(ino);
315 1.1 bouyer if (ftypeok(dp))
316 1.1 bouyer pfatal("%s=%s\n",
317 1.3 bouyer (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
318 1.1 bouyer else
319 1.1 bouyer pfatal("NAME=%s\n", pathbuf);
320 1.1 bouyer }
321 1.1 bouyer
322 1.1 bouyer void
323 1.1 bouyer adjust(idesc, lcnt)
324 1.2 lukem struct inodesc *idesc;
325 1.1 bouyer short lcnt;
326 1.1 bouyer {
327 1.2 lukem struct ext2fs_dinode *dp;
328 1.1 bouyer
329 1.1 bouyer dp = ginode(idesc->id_number);
330 1.3 bouyer if (fs2h16(dp->e2di_nlink) == lcnt) {
331 1.1 bouyer if (linkup(idesc->id_number, (ino_t)0) == 0)
332 1.1 bouyer clri(idesc, "UNREF", 0);
333 1.1 bouyer } else {
334 1.1 bouyer pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
335 1.3 bouyer ((fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
336 1.1 bouyer pinode(idesc->id_number);
337 1.1 bouyer printf(" COUNT %d SHOULD BE %d",
338 1.3 bouyer fs2h16(dp->e2di_nlink), fs2h16(dp->e2di_nlink) - lcnt);
339 1.1 bouyer if (preen) {
340 1.1 bouyer if (lcnt < 0) {
341 1.1 bouyer printf("\n");
342 1.1 bouyer pfatal("LINK COUNT INCREASING");
343 1.1 bouyer }
344 1.1 bouyer printf(" (ADJUSTED)\n");
345 1.1 bouyer }
346 1.1 bouyer if (preen || reply("ADJUST") == 1) {
347 1.3 bouyer dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) - lcnt);
348 1.1 bouyer inodirty();
349 1.1 bouyer }
350 1.1 bouyer }
351 1.1 bouyer }
352 1.1 bouyer
353 1.1 bouyer static int
354 1.1 bouyer mkentry(idesc)
355 1.1 bouyer struct inodesc *idesc;
356 1.1 bouyer {
357 1.2 lukem struct ext2fs_direct *dirp = idesc->id_dirp;
358 1.1 bouyer struct ext2fs_direct newent;
359 1.1 bouyer int newlen, oldlen;
360 1.1 bouyer
361 1.4 bouyer newent.e2d_namlen = strlen(idesc->id_name);
362 1.4 bouyer if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
363 1.4 bouyer (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
364 1.5 bouyer newent.e2d_type = inot2ext2dt(typemap[idesc->id_parent]);
365 1.4 bouyer newlen = EXT2FS_DIRSIZ(newent.e2d_namlen);
366 1.1 bouyer if (dirp->e2d_ino != 0)
367 1.4 bouyer oldlen = EXT2FS_DIRSIZ(dirp->e2d_namlen);
368 1.1 bouyer else
369 1.1 bouyer oldlen = 0;
370 1.3 bouyer if (fs2h16(dirp->e2d_reclen) - oldlen < newlen)
371 1.1 bouyer return (KEEPON);
372 1.3 bouyer newent.e2d_reclen = h2fs16(fs2h16(dirp->e2d_reclen) - oldlen);
373 1.3 bouyer dirp->e2d_reclen = h2fs16(oldlen);
374 1.1 bouyer dirp = (struct ext2fs_direct *)(((char *)dirp) + oldlen);
375 1.3 bouyer dirp->e2d_ino = h2fs32(idesc->id_parent); /* ino to be entered is in id_parent */
376 1.1 bouyer dirp->e2d_reclen = newent.e2d_reclen;
377 1.1 bouyer dirp->e2d_namlen = newent.e2d_namlen;
378 1.4 bouyer dirp->e2d_type = newent.e2d_type;
379 1.4 bouyer memcpy(dirp->e2d_name, idesc->id_name, (size_t)(dirp->e2d_namlen));
380 1.1 bouyer return (ALTERED|STOP);
381 1.1 bouyer }
382 1.1 bouyer
383 1.1 bouyer static int
384 1.1 bouyer chgino(idesc)
385 1.1 bouyer struct inodesc *idesc;
386 1.1 bouyer {
387 1.2 lukem struct ext2fs_direct *dirp = idesc->id_dirp;
388 1.4 bouyer u_int16_t namlen = dirp->e2d_namlen;
389 1.1 bouyer
390 1.3 bouyer if (strlen(idesc->id_name) != namlen ||
391 1.3 bouyer strncmp(dirp->e2d_name, idesc->id_name, (int)namlen))
392 1.1 bouyer return (KEEPON);
393 1.3 bouyer dirp->e2d_ino = h2fs32(idesc->id_parent);
394 1.4 bouyer if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
395 1.4 bouyer (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
396 1.5 bouyer dirp->e2d_type = inot2ext2dt(typemap[idesc->id_parent]);
397 1.4 bouyer else
398 1.4 bouyer dirp->e2d_type = 0;
399 1.1 bouyer return (ALTERED|STOP);
400 1.1 bouyer }
401 1.1 bouyer
402 1.1 bouyer int
403 1.1 bouyer linkup(orphan, parentdir)
404 1.1 bouyer ino_t orphan;
405 1.1 bouyer ino_t parentdir;
406 1.1 bouyer {
407 1.2 lukem struct ext2fs_dinode *dp;
408 1.1 bouyer int lostdir;
409 1.1 bouyer ino_t oldlfdir;
410 1.1 bouyer struct inodesc idesc;
411 1.1 bouyer char tempname[BUFSIZ];
412 1.1 bouyer
413 1.1 bouyer memset(&idesc, 0, sizeof(struct inodesc));
414 1.1 bouyer dp = ginode(orphan);
415 1.3 bouyer lostdir = (fs2h16(dp->e2di_mode) & IFMT) == IFDIR;
416 1.1 bouyer pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
417 1.1 bouyer pinode(orphan);
418 1.3 bouyer if (preen && fs2h32(dp->e2di_size) == 0)
419 1.1 bouyer return (0);
420 1.1 bouyer if (preen)
421 1.1 bouyer printf(" (RECONNECTED)\n");
422 1.1 bouyer else
423 1.1 bouyer if (reply("RECONNECT") == 0)
424 1.1 bouyer return (0);
425 1.1 bouyer if (lfdir == 0) {
426 1.1 bouyer dp = ginode(EXT2_ROOTINO);
427 1.1 bouyer idesc.id_name = lfname;
428 1.1 bouyer idesc.id_type = DATA;
429 1.1 bouyer idesc.id_func = findino;
430 1.1 bouyer idesc.id_number = EXT2_ROOTINO;
431 1.1 bouyer if ((ckinode(dp, &idesc) & FOUND) != 0) {
432 1.1 bouyer lfdir = idesc.id_parent;
433 1.1 bouyer } else {
434 1.1 bouyer pwarn("NO lost+found DIRECTORY");
435 1.1 bouyer if (preen || reply("CREATE")) {
436 1.1 bouyer lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode);
437 1.1 bouyer if (lfdir != 0) {
438 1.1 bouyer if (makeentry(EXT2_ROOTINO, lfdir, lfname) != 0) {
439 1.1 bouyer if (preen)
440 1.1 bouyer printf(" (CREATED)\n");
441 1.1 bouyer } else {
442 1.1 bouyer freedir(lfdir, EXT2_ROOTINO);
443 1.1 bouyer lfdir = 0;
444 1.1 bouyer if (preen)
445 1.1 bouyer printf("\n");
446 1.1 bouyer }
447 1.1 bouyer }
448 1.1 bouyer }
449 1.1 bouyer }
450 1.1 bouyer if (lfdir == 0) {
451 1.1 bouyer pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
452 1.1 bouyer printf("\n\n");
453 1.1 bouyer return (0);
454 1.1 bouyer }
455 1.1 bouyer }
456 1.1 bouyer dp = ginode(lfdir);
457 1.3 bouyer if ((fs2h16(dp->e2di_mode) & IFMT) != IFDIR) {
458 1.1 bouyer pfatal("lost+found IS NOT A DIRECTORY");
459 1.1 bouyer if (reply("REALLOCATE") == 0)
460 1.1 bouyer return (0);
461 1.1 bouyer oldlfdir = lfdir;
462 1.1 bouyer if ((lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode)) == 0) {
463 1.1 bouyer pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
464 1.1 bouyer return (0);
465 1.1 bouyer }
466 1.1 bouyer if ((changeino(EXT2_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
467 1.1 bouyer pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
468 1.1 bouyer return (0);
469 1.1 bouyer }
470 1.1 bouyer inodirty();
471 1.1 bouyer idesc.id_type = ADDR;
472 1.1 bouyer idesc.id_func = pass4check;
473 1.1 bouyer idesc.id_number = oldlfdir;
474 1.1 bouyer adjust(&idesc, lncntp[oldlfdir] + 1);
475 1.1 bouyer lncntp[oldlfdir] = 0;
476 1.1 bouyer dp = ginode(lfdir);
477 1.1 bouyer }
478 1.1 bouyer if (statemap[lfdir] != DFOUND) {
479 1.1 bouyer pfatal("SORRY. NO lost+found DIRECTORY\n\n");
480 1.1 bouyer return (0);
481 1.1 bouyer }
482 1.1 bouyer (void)lftempname(tempname, orphan);
483 1.1 bouyer if (makeentry(lfdir, orphan, tempname) == 0) {
484 1.1 bouyer pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
485 1.1 bouyer printf("\n\n");
486 1.1 bouyer return (0);
487 1.1 bouyer }
488 1.1 bouyer lncntp[orphan]--;
489 1.1 bouyer if (lostdir) {
490 1.1 bouyer if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
491 1.1 bouyer parentdir != (ino_t)-1)
492 1.1 bouyer (void)makeentry(orphan, lfdir, "..");
493 1.1 bouyer dp = ginode(lfdir);
494 1.3 bouyer dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) +1);
495 1.1 bouyer inodirty();
496 1.1 bouyer lncntp[lfdir]++;
497 1.1 bouyer pwarn("DIR I=%u CONNECTED. ", orphan);
498 1.1 bouyer if (parentdir != (ino_t)-1)
499 1.1 bouyer printf("PARENT WAS I=%u\n", parentdir);
500 1.1 bouyer if (preen == 0)
501 1.1 bouyer printf("\n");
502 1.1 bouyer }
503 1.1 bouyer return (1);
504 1.1 bouyer }
505 1.1 bouyer
506 1.1 bouyer /*
507 1.1 bouyer * fix an entry in a directory.
508 1.1 bouyer */
509 1.1 bouyer int
510 1.1 bouyer changeino(dir, name, newnum)
511 1.1 bouyer ino_t dir;
512 1.1 bouyer char *name;
513 1.1 bouyer ino_t newnum;
514 1.1 bouyer {
515 1.1 bouyer struct inodesc idesc;
516 1.1 bouyer
517 1.1 bouyer memset(&idesc, 0, sizeof(struct inodesc));
518 1.1 bouyer idesc.id_type = DATA;
519 1.1 bouyer idesc.id_func = chgino;
520 1.1 bouyer idesc.id_number = dir;
521 1.1 bouyer idesc.id_fix = DONTKNOW;
522 1.1 bouyer idesc.id_name = name;
523 1.1 bouyer idesc.id_parent = newnum; /* new value for name */
524 1.1 bouyer return (ckinode(ginode(dir), &idesc));
525 1.1 bouyer }
526 1.1 bouyer
527 1.1 bouyer /*
528 1.1 bouyer * make an entry in a directory
529 1.1 bouyer */
530 1.1 bouyer int
531 1.1 bouyer makeentry(parent, ino, name)
532 1.1 bouyer ino_t parent, ino;
533 1.1 bouyer char *name;
534 1.1 bouyer {
535 1.1 bouyer struct ext2fs_dinode *dp;
536 1.1 bouyer struct inodesc idesc;
537 1.1 bouyer char pathbuf[MAXPATHLEN + 1];
538 1.1 bouyer
539 1.1 bouyer if ((parent < EXT2_FIRSTINO && parent != EXT2_ROOTINO)
540 1.1 bouyer || parent >= maxino ||
541 1.1 bouyer (ino < EXT2_FIRSTINO && ino < EXT2_ROOTINO) || ino >= maxino)
542 1.1 bouyer return (0);
543 1.1 bouyer memset(&idesc, 0, sizeof(struct inodesc));
544 1.1 bouyer idesc.id_type = DATA;
545 1.1 bouyer idesc.id_func = mkentry;
546 1.1 bouyer idesc.id_number = parent;
547 1.1 bouyer idesc.id_parent = ino; /* this is the inode to enter */
548 1.1 bouyer idesc.id_fix = DONTKNOW;
549 1.1 bouyer idesc.id_name = name;
550 1.1 bouyer dp = ginode(parent);
551 1.3 bouyer if (fs2h32(dp->e2di_size) % sblock.e2fs_bsize) {
552 1.3 bouyer dp->e2di_size =
553 1.3 bouyer h2fs32(roundup(fs2h32(dp->e2di_size), sblock.e2fs_bsize));
554 1.1 bouyer inodirty();
555 1.1 bouyer }
556 1.1 bouyer if ((ckinode(dp, &idesc) & ALTERED) != 0)
557 1.1 bouyer return (1);
558 1.7 itojun getpathname(pathbuf, sizeof(pathbuf), parent, parent);
559 1.1 bouyer dp = ginode(parent);
560 1.1 bouyer if (expanddir(dp, pathbuf) == 0)
561 1.1 bouyer return (0);
562 1.1 bouyer return (ckinode(dp, &idesc) & ALTERED);
563 1.1 bouyer }
564 1.1 bouyer
565 1.1 bouyer /*
566 1.1 bouyer * Attempt to expand the size of a directory
567 1.1 bouyer */
568 1.1 bouyer static int
569 1.1 bouyer expanddir(dp, name)
570 1.2 lukem struct ext2fs_dinode *dp;
571 1.1 bouyer char *name;
572 1.1 bouyer {
573 1.1 bouyer daddr_t lastbn, newblk;
574 1.2 lukem struct bufarea *bp;
575 1.3 bouyer char *firstblk;
576 1.1 bouyer
577 1.1 bouyer if ((firstblk = malloc(sblock.e2fs_bsize)) == NULL) {
578 1.1 bouyer fprintf(stderr, "out of memory");
579 1.1 bouyer exit(8);
580 1.1 bouyer }
581 1.1 bouyer
582 1.3 bouyer lastbn = lblkno(&sblock, fs2h32(dp->e2di_size));
583 1.3 bouyer if (lastbn >= NDADDR - 1 || fs2h32(dp->e2di_blocks[lastbn]) == 0 ||
584 1.3 bouyer fs2h32(dp->e2di_size) == 0)
585 1.1 bouyer return (0);
586 1.1 bouyer if ((newblk = allocblk()) == 0)
587 1.1 bouyer return (0);
588 1.1 bouyer dp->e2di_blocks[lastbn + 1] = dp->e2di_blocks[lastbn];
589 1.3 bouyer dp->e2di_blocks[lastbn] = h2fs32(newblk);
590 1.3 bouyer dp->e2di_size = h2fs32(fs2h32(dp->e2di_size) + sblock.e2fs_bsize);
591 1.3 bouyer dp->e2di_nblock = h2fs32(fs2h32(dp->e2di_nblock) + 1);
592 1.3 bouyer bp = getdirblk(fs2h32(dp->e2di_blocks[lastbn + 1]),
593 1.1 bouyer sblock.e2fs_bsize);
594 1.1 bouyer if (bp->b_errs)
595 1.1 bouyer goto bad;
596 1.1 bouyer memcpy(firstblk, bp->b_un.b_buf, sblock.e2fs_bsize);
597 1.1 bouyer bp = getdirblk(newblk, sblock.e2fs_bsize);
598 1.1 bouyer if (bp->b_errs)
599 1.1 bouyer goto bad;
600 1.1 bouyer memcpy(bp->b_un.b_buf, firstblk, sblock.e2fs_bsize);
601 1.1 bouyer dirty(bp);
602 1.3 bouyer bp = getdirblk(fs2h32(dp->e2di_blocks[lastbn + 1]),
603 1.1 bouyer sblock.e2fs_bsize);
604 1.1 bouyer if (bp->b_errs)
605 1.1 bouyer goto bad;
606 1.3 bouyer emptydir.dot_reclen = h2fs16(sblock.e2fs_bsize);
607 1.1 bouyer memcpy(bp->b_un.b_buf, &emptydir, sizeof emptydir);
608 1.1 bouyer pwarn("NO SPACE LEFT IN %s", name);
609 1.1 bouyer if (preen)
610 1.1 bouyer printf(" (EXPANDED)\n");
611 1.1 bouyer else if (reply("EXPAND") == 0)
612 1.1 bouyer goto bad;
613 1.1 bouyer dirty(bp);
614 1.1 bouyer inodirty();
615 1.1 bouyer return (1);
616 1.1 bouyer bad:
617 1.1 bouyer dp->e2di_blocks[lastbn] = dp->e2di_blocks[lastbn + 1];
618 1.1 bouyer dp->e2di_blocks[lastbn + 1] = 0;
619 1.3 bouyer dp->e2di_size = h2fs32(fs2h32(dp->e2di_size) - sblock.e2fs_bsize);
620 1.3 bouyer dp->e2di_nblock = h2fs32(fs2h32(dp->e2di_nblock) - 1);
621 1.1 bouyer freeblk(newblk);
622 1.1 bouyer return (0);
623 1.1 bouyer }
624 1.1 bouyer
625 1.1 bouyer /*
626 1.1 bouyer * allocate a new directory
627 1.1 bouyer */
628 1.1 bouyer int
629 1.1 bouyer allocdir(parent, request, mode)
630 1.1 bouyer ino_t parent, request;
631 1.1 bouyer int mode;
632 1.1 bouyer {
633 1.1 bouyer ino_t ino;
634 1.1 bouyer struct ext2fs_dinode *dp;
635 1.2 lukem struct bufarea *bp;
636 1.1 bouyer struct ext2fs_dirtemplate *dirp;
637 1.1 bouyer
638 1.1 bouyer ino = allocino(request, IFDIR|mode);
639 1.3 bouyer dirhead.dot_reclen = h2fs16(12); /* XXX */
640 1.3 bouyer dirhead.dotdot_reclen = h2fs16(sblock.e2fs_bsize - 12); /* XXX */
641 1.4 bouyer dirhead.dot_namlen = 1;
642 1.4 bouyer if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
643 1.4 bouyer (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
644 1.5 bouyer dirhead.dot_type = EXT2_FT_DIR;
645 1.4 bouyer else
646 1.4 bouyer dirhead.dot_type = 0;
647 1.4 bouyer dirhead.dotdot_namlen = 2;
648 1.4 bouyer if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
649 1.4 bouyer (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
650 1.5 bouyer dirhead.dotdot_type = EXT2_FT_DIR;
651 1.4 bouyer else
652 1.4 bouyer dirhead.dotdot_type = 0;
653 1.1 bouyer dirp = &dirhead;
654 1.3 bouyer dirp->dot_ino = h2fs32(ino);
655 1.3 bouyer dirp->dotdot_ino = h2fs32(parent);
656 1.1 bouyer dp = ginode(ino);
657 1.3 bouyer bp = getdirblk(fs2h32(dp->e2di_blocks[0]), sblock.e2fs_bsize);
658 1.1 bouyer if (bp->b_errs) {
659 1.1 bouyer freeino(ino);
660 1.1 bouyer return (0);
661 1.1 bouyer }
662 1.1 bouyer memcpy(bp->b_un.b_buf, dirp, sizeof(struct ext2fs_dirtemplate));
663 1.1 bouyer dirty(bp);
664 1.3 bouyer dp->e2di_nlink = h2fs16(2);
665 1.1 bouyer inodirty();
666 1.1 bouyer if (ino == EXT2_ROOTINO) {
667 1.3 bouyer lncntp[ino] = fs2h16(dp->e2di_nlink);
668 1.1 bouyer cacheino(dp, ino);
669 1.1 bouyer return(ino);
670 1.1 bouyer }
671 1.1 bouyer if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
672 1.1 bouyer freeino(ino);
673 1.1 bouyer return (0);
674 1.1 bouyer }
675 1.1 bouyer cacheino(dp, ino);
676 1.1 bouyer statemap[ino] = statemap[parent];
677 1.1 bouyer if (statemap[ino] == DSTATE) {
678 1.3 bouyer lncntp[ino] = fs2h16(dp->e2di_nlink);
679 1.1 bouyer lncntp[parent]++;
680 1.1 bouyer }
681 1.1 bouyer dp = ginode(parent);
682 1.3 bouyer dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) + 1);
683 1.1 bouyer inodirty();
684 1.1 bouyer return (ino);
685 1.1 bouyer }
686 1.1 bouyer
687 1.1 bouyer /*
688 1.1 bouyer * free a directory inode
689 1.1 bouyer */
690 1.1 bouyer static void
691 1.1 bouyer freedir(ino, parent)
692 1.1 bouyer ino_t ino, parent;
693 1.1 bouyer {
694 1.1 bouyer struct ext2fs_dinode *dp;
695 1.1 bouyer
696 1.1 bouyer if (ino != parent) {
697 1.1 bouyer dp = ginode(parent);
698 1.3 bouyer dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) - 1);
699 1.1 bouyer inodirty();
700 1.1 bouyer }
701 1.1 bouyer freeino(ino);
702 1.1 bouyer }
703 1.1 bouyer
704 1.1 bouyer /*
705 1.1 bouyer * generate a temporary name for the lost+found directory.
706 1.1 bouyer */
707 1.1 bouyer static int
708 1.1 bouyer lftempname(bufp, ino)
709 1.1 bouyer char *bufp;
710 1.1 bouyer ino_t ino;
711 1.1 bouyer {
712 1.2 lukem ino_t in;
713 1.2 lukem char *cp;
714 1.1 bouyer int namlen;
715 1.1 bouyer
716 1.1 bouyer cp = bufp + 2;
717 1.1 bouyer for (in = maxino; in > 0; in /= 10)
718 1.1 bouyer cp++;
719 1.1 bouyer *--cp = 0;
720 1.1 bouyer namlen = cp - bufp;
721 1.1 bouyer in = ino;
722 1.1 bouyer while (cp > bufp) {
723 1.1 bouyer *--cp = (in % 10) + '0';
724 1.1 bouyer in /= 10;
725 1.1 bouyer }
726 1.1 bouyer *cp = '#';
727 1.1 bouyer return (namlen);
728 1.1 bouyer }
729 1.1 bouyer
730 1.1 bouyer /*
731 1.1 bouyer * Get a directory block.
732 1.1 bouyer * Insure that it is held until another is requested.
733 1.1 bouyer */
734 1.1 bouyer static struct bufarea *
735 1.1 bouyer getdirblk(blkno, size)
736 1.1 bouyer daddr_t blkno;
737 1.1 bouyer long size;
738 1.1 bouyer {
739 1.1 bouyer
740 1.1 bouyer if (pdirbp != 0)
741 1.1 bouyer pdirbp->b_flags &= ~B_INUSE;
742 1.1 bouyer pdirbp = getdatablk(blkno, size);
743 1.1 bouyer return (pdirbp);
744 1.1 bouyer }
745