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