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