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