Home | History | Annotate | Line # | Download | only in fsck_lfs
dir.c revision 1.13
      1 /* $NetBSD: dir.c,v 1.13 2003/10/05 17:11:23 jdolecek 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 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/time.h>
     35 #include <sys/buf.h>
     36 #include <sys/mount.h>
     37 
     38 #include <ufs/ufs/inode.h>
     39 #include <ufs/ufs/dir.h>
     40 #include <ufs/ufs/ufsmount.h>
     41 #include <ufs/lfs/lfs.h>
     42 
     43 #include <err.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 
     48 #include "bufcache.h"
     49 #include "vnode.h"
     50 #include "lfs.h"
     51 
     52 #include "fsck.h"
     53 #include "fsutil.h"
     54 #include "extern.h"
     55 
     56 char *lfname = "lost+found";
     57 int lfmode = 01700;
     58 struct dirtemplate emptydir = {0, DIRBLKSIZ};
     59 struct dirtemplate dirhead = {
     60 	0, 12, DT_DIR, 1, ".",
     61 	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
     62 };
     63 struct odirtemplate odirhead = {
     64 	0, 12, 1, ".",
     65 	0, DIRBLKSIZ - 12, 2, ".."
     66 };
     67 
     68 static int expanddir(struct uvnode *, struct ufs1_dinode *, char *);
     69 static void freedir(ino_t, ino_t);
     70 static struct direct *fsck_readdir(struct uvnode *, struct inodesc *);
     71 static int lftempname(char *, ino_t);
     72 static int mkentry(struct inodesc *);
     73 static int chgino(struct inodesc *);
     74 
     75 /*
     76  * Propagate connected state through the tree.
     77  */
     78 void
     79 propagate()
     80 {
     81 	struct inoinfo **inpp, *inp, *pinp;
     82 	struct inoinfo **inpend;
     83 
     84 	/*
     85 	 * Create a list of children for each directory.
     86 	 */
     87 	inpend = &inpsort[inplast];
     88 	for (inpp = inpsort; inpp < inpend; inpp++) {
     89 		inp = *inpp;
     90 		if (inp->i_parent == 0 ||
     91 		    inp->i_number == ROOTINO)
     92 			continue;
     93 		pinp = getinoinfo(inp->i_parent);
     94 		inp->i_parentp = pinp;
     95 		inp->i_sibling = pinp->i_child;
     96 		pinp->i_child = inp;
     97 	}
     98 	inp = getinoinfo(ROOTINO);
     99 	while (inp) {
    100 		statemap[inp->i_number] = DFOUND;
    101 		if (inp->i_child &&
    102 		    statemap[inp->i_child->i_number] == DSTATE)
    103 			inp = inp->i_child;
    104 		else if (inp->i_sibling)
    105 			inp = inp->i_sibling;
    106 		else
    107 			inp = inp->i_parentp;
    108 	}
    109 }
    110 
    111 /*
    112  * Scan each entry in a directory block.
    113  */
    114 int
    115 dirscan(struct inodesc *idesc)
    116 {
    117 	struct direct *dp;
    118 	struct ubuf *bp;
    119 	int dsize, n;
    120 	long blksiz;
    121 	char dbuf[DIRBLKSIZ];
    122 	struct uvnode *vp;
    123 
    124 	if (idesc->id_type != DATA)
    125 		errexit("wrong type to dirscan %d\n", idesc->id_type);
    126 	if (idesc->id_entryno == 0 &&
    127 	    (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
    128 		idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
    129 	blksiz = idesc->id_numfrags * fs->lfs_fsize;
    130 	if (chkrange(idesc->id_blkno, fragstofsb(fs, idesc->id_numfrags))) {
    131 		idesc->id_filesize -= blksiz;
    132 		return (SKIP);
    133 	}
    134 	idesc->id_loc = 0;
    135 
    136 	vp = vget(fs, idesc->id_number);
    137 	for (dp = fsck_readdir(vp, idesc); dp != NULL;
    138 	    dp = fsck_readdir(vp, idesc)) {
    139 		dsize = dp->d_reclen;
    140 		memcpy(dbuf, dp, (size_t) dsize);
    141 		idesc->id_dirp = (struct direct *) dbuf;
    142 		if ((n = (*idesc->id_func) (idesc)) & ALTERED) {
    143 			bread(vp, idesc->id_lblkno, blksiz, NOCRED, &bp);
    144 			memcpy(bp->b_data + idesc->id_loc - dsize, dbuf,
    145 			    (size_t) dsize);
    146 			VOP_BWRITE(bp);
    147 			sbdirty();
    148 		}
    149 		if (n & STOP)
    150 			return (n);
    151 	}
    152 	return (idesc->id_filesize > 0 ? KEEPON : STOP);
    153 }
    154 
    155 /*
    156  * get next entry in a directory.
    157  */
    158 static struct direct *
    159 fsck_readdir(struct uvnode *vp, struct inodesc *idesc)
    160 {
    161 	struct direct *dp, *ndp;
    162 	struct ubuf *bp;
    163 	long size, blksiz, fix, dploc;
    164 
    165 	blksiz = idesc->id_numfrags * fs->lfs_fsize;
    166 	bread(vp, idesc->id_lblkno, blksiz, NOCRED, &bp);
    167 	if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
    168 	    idesc->id_loc < blksiz) {
    169 		dp = (struct direct *) (bp->b_data + idesc->id_loc);
    170 		if (dircheck(idesc, dp))
    171 			goto dpok;
    172 		brelse(bp);
    173 		if (idesc->id_fix == IGNORE)
    174 			return (0);
    175 		fix = dofix(idesc, "DIRECTORY CORRUPTED");
    176 		bread(vp, idesc->id_lblkno, blksiz, NOCRED, &bp);
    177 		dp = (struct direct *) (bp->b_data + idesc->id_loc);
    178 		dp->d_reclen = DIRBLKSIZ;
    179 		dp->d_ino = 0;
    180 		dp->d_type = 0;
    181 		dp->d_namlen = 0;
    182 		dp->d_name[0] = '\0';
    183 		if (fix)
    184 			VOP_BWRITE(bp);
    185 		else
    186 			brelse(bp);
    187 		idesc->id_loc += DIRBLKSIZ;
    188 		idesc->id_filesize -= DIRBLKSIZ;
    189 		return (dp);
    190 	}
    191 dpok:
    192 	if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) {
    193 		brelse(bp);
    194 		return NULL;
    195 	}
    196 	dploc = idesc->id_loc;
    197 	dp = (struct direct *) (bp->b_data + dploc);
    198 	idesc->id_loc += dp->d_reclen;
    199 	idesc->id_filesize -= dp->d_reclen;
    200 	if ((idesc->id_loc % DIRBLKSIZ) == 0) {
    201 		brelse(bp);
    202 		return dp;
    203 	}
    204 	ndp = (struct direct *) (bp->b_data + idesc->id_loc);
    205 	if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
    206 	    dircheck(idesc, ndp) == 0) {
    207 		brelse(bp);
    208 		size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
    209 		idesc->id_loc += size;
    210 		idesc->id_filesize -= size;
    211 		if (idesc->id_fix == IGNORE)
    212 			return 0;
    213 		fix = dofix(idesc, "DIRECTORY CORRUPTED");
    214 		bread(vp, idesc->id_lblkno, blksiz, NOCRED, &bp);
    215 		dp = (struct direct *) (bp->b_data + dploc);
    216 		dp->d_reclen += size;
    217 		if (fix)
    218 			VOP_BWRITE(bp);
    219 		else
    220 			brelse(bp);
    221 	} else
    222 		brelse(bp);
    223 
    224 	return (dp);
    225 }
    226 
    227 /*
    228  * Verify that a directory entry is valid.
    229  * This is a superset of the checks made in the kernel.
    230  */
    231 int
    232 dircheck(struct inodesc *idesc, struct direct *dp)
    233 {
    234 	int size;
    235 	char *cp;
    236 	u_char namlen, type;
    237 	int spaceleft;
    238 
    239 	spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
    240 	if (dp->d_ino >= maxino ||
    241 	    dp->d_reclen == 0 ||
    242 	    dp->d_reclen > spaceleft ||
    243 	    (dp->d_reclen & 0x3) != 0) {
    244 		pwarn("ino too large, reclen=0, reclen>space, or reclen&3!=0\n");
    245 		pwarn("dp->d_ino = 0x%x\tdp->d_reclen = 0x%x\n",
    246 		    dp->d_ino, dp->d_reclen);
    247 		pwarn("maxino = 0x%x\tspaceleft = 0x%x\n", maxino, spaceleft);
    248 		return (0);
    249 	}
    250 	if (dp->d_ino == 0)
    251 		return (1);
    252 	size = DIRSIZ(0, dp, 0);
    253 	namlen = dp->d_namlen;
    254 	type = dp->d_type;
    255 	if (dp->d_reclen < size ||
    256 	    idesc->id_filesize < size ||
    257 	/* namlen > MAXNAMLEN || */
    258 	    type > 15) {
    259 		printf("reclen<size, filesize<size, namlen too large, or type>15\n");
    260 		return (0);
    261 	}
    262 	for (cp = dp->d_name, size = 0; size < namlen; size++)
    263 		if (*cp == '\0' || (*cp++ == '/')) {
    264 			printf("name contains NUL or /\n");
    265 			return (0);
    266 		}
    267 	if (*cp != '\0') {
    268 		printf("name size misstated\n");
    269 		return (0);
    270 	}
    271 	return (1);
    272 }
    273 
    274 void
    275 direrror(ino_t ino, char *errmesg)
    276 {
    277 
    278 	fileerror(ino, ino, errmesg);
    279 }
    280 
    281 void
    282 fileerror(ino_t cwd, ino_t ino, char *errmesg)
    283 {
    284 	char pathbuf[MAXPATHLEN + 1];
    285 	struct uvnode *vp;
    286 
    287 	pwarn("%s ", errmesg);
    288 	pinode(ino);
    289 	printf("\n");
    290 	getpathname(pathbuf, sizeof(pathbuf), cwd, ino);
    291 	if (ino < ROOTINO || ino >= maxino) {
    292 		pfatal("NAME=%s\n", pathbuf);
    293 		return;
    294 	}
    295 	vp = vget(fs, ino);
    296 	if (vp == NULL)
    297 		pfatal("INO is NULL\n");
    298 	else {
    299 		if (ftypeok(VTOD(vp)))
    300 			pfatal("%s=%s\n",
    301 			    (VTOI(vp)->i_ffs1_mode & IFMT) == IFDIR ?
    302 			    "DIR" : "FILE", pathbuf);
    303 		else
    304 			pfatal("NAME=%s\n", pathbuf);
    305 	}
    306 }
    307 
    308 void
    309 adjust(struct inodesc *idesc, short lcnt)
    310 {
    311 	struct uvnode *vp;
    312 	struct ufs1_dinode *dp;
    313 
    314 	vp = vget(fs, idesc->id_number);
    315 	dp = VTOD(vp);
    316 	if (dp->di_nlink == lcnt) {
    317 		if (linkup(idesc->id_number, (ino_t) 0) == 0)
    318 			clri(idesc, "UNREF", 0);
    319 	} else {
    320 		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
    321 		    ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
    322 		pinode(idesc->id_number);
    323 		printf(" COUNT %d SHOULD BE %d",
    324 		    dp->di_nlink, dp->di_nlink - lcnt);
    325 		if (preen) {
    326 			if (lcnt < 0) {
    327 				printf("\n");
    328 				pfatal("LINK COUNT INCREASING");
    329 			}
    330 			printf(" (ADJUSTED)\n");
    331 		}
    332 		if (preen || reply("ADJUST") == 1) {
    333 			dp->di_nlink -= lcnt;
    334 			inodirty(VTOI(vp));
    335 		}
    336 	}
    337 }
    338 
    339 static int
    340 mkentry(struct inodesc *idesc)
    341 {
    342 	struct direct *dirp = idesc->id_dirp;
    343 	struct direct newent;
    344 	int newlen, oldlen;
    345 
    346 	newent.d_namlen = strlen(idesc->id_name);
    347 	newlen = DIRSIZ(0, &newent, 0);
    348 	if (dirp->d_ino != 0)
    349 		oldlen = DIRSIZ(0, dirp, 0);
    350 	else
    351 		oldlen = 0;
    352 	if (dirp->d_reclen - oldlen < newlen)
    353 		return (KEEPON);
    354 	newent.d_reclen = dirp->d_reclen - oldlen;
    355 	dirp->d_reclen = oldlen;
    356 	dirp = (struct direct *) (((char *) dirp) + oldlen);
    357 	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
    358 	dirp->d_reclen = newent.d_reclen;
    359 	dirp->d_type = typemap[idesc->id_parent];
    360 	dirp->d_namlen = newent.d_namlen;
    361 	memcpy(dirp->d_name, idesc->id_name, (size_t) dirp->d_namlen + 1);
    362 	return (ALTERED | STOP);
    363 }
    364 
    365 static int
    366 chgino(struct inodesc *idesc)
    367 {
    368 	struct direct *dirp = idesc->id_dirp;
    369 
    370 	if (memcmp(dirp->d_name, idesc->id_name, (int) dirp->d_namlen + 1))
    371 		return (KEEPON);
    372 	dirp->d_ino = idesc->id_parent;
    373 	dirp->d_type = typemap[idesc->id_parent];
    374 	return (ALTERED | STOP);
    375 }
    376 
    377 int
    378 linkup(ino_t orphan, ino_t parentdir)
    379 {
    380 	struct ufs1_dinode *dp;
    381 	int lostdir;
    382 	ino_t oldlfdir;
    383 	struct inodesc idesc;
    384 	char tempname[BUFSIZ];
    385 	struct uvnode *vp;
    386 
    387 	memset(&idesc, 0, sizeof(struct inodesc));
    388 	vp = vget(fs, orphan);
    389 	dp = VTOD(vp);
    390 	lostdir = (dp->di_mode & IFMT) == IFDIR;
    391 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
    392 	pinode(orphan);
    393 	if (preen && dp->di_size == 0)
    394 		return (0);
    395 	if (preen)
    396 		printf(" (RECONNECTED)\n");
    397 	else if (reply("RECONNECT") == 0)
    398 		return (0);
    399 	if (lfdir == 0) {
    400 		dp = ginode(ROOTINO);
    401 		idesc.id_name = lfname;
    402 		idesc.id_type = DATA;
    403 		idesc.id_func = findino;
    404 		idesc.id_number = ROOTINO;
    405 		if ((ckinode(dp, &idesc) & FOUND) != 0) {
    406 			lfdir = idesc.id_parent;
    407 		} else {
    408 			pwarn("NO lost+found DIRECTORY");
    409 			if (preen || reply("CREATE")) {
    410 				lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode);
    411 				if (lfdir != 0) {
    412 					if (makeentry(ROOTINO, lfdir, lfname) != 0) {
    413 						if (preen)
    414 							printf(" (CREATED)\n");
    415 					} else {
    416 						freedir(lfdir, ROOTINO);
    417 						lfdir = 0;
    418 						if (preen)
    419 							printf("\n");
    420 					}
    421 				}
    422 			}
    423 		}
    424 		if (lfdir == 0) {
    425 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
    426 			printf("\n\n");
    427 			return (0);
    428 		}
    429 	}
    430 	vp = vget(fs, lfdir);
    431 	dp = VTOD(vp);
    432 	if ((dp->di_mode & IFMT) != IFDIR) {
    433 		pfatal("lost+found IS NOT A DIRECTORY");
    434 		if (reply("REALLOCATE") == 0)
    435 			return (0);
    436 		oldlfdir = lfdir;
    437 		if ((lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode)) == 0) {
    438 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
    439 			return (0);
    440 		}
    441 		if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
    442 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
    443 			return (0);
    444 		}
    445 		inodirty(VTOI(vp));
    446 		idesc.id_type = ADDR;
    447 		idesc.id_func = pass4check;
    448 		idesc.id_number = oldlfdir;
    449 		adjust(&idesc, lncntp[oldlfdir] + 1);
    450 		lncntp[oldlfdir] = 0;
    451 		vp = vget(fs, lfdir);
    452 		dp = VTOD(vp);
    453 	}
    454 	if (statemap[lfdir] != DFOUND) {
    455 		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
    456 		return (0);
    457 	}
    458 	(void) lftempname(tempname, orphan);
    459 	if (makeentry(lfdir, orphan, tempname) == 0) {
    460 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
    461 		printf("\n\n");
    462 		return (0);
    463 	}
    464 	lncntp[orphan]--;
    465 	if (lostdir) {
    466 		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
    467 		    parentdir != (ino_t) - 1)
    468 			(void) makeentry(orphan, lfdir, "..");
    469 		vp = vget(fs, lfdir);
    470 		VTOI(vp)->i_ffs1_nlink++;
    471 		inodirty(VTOI(vp));
    472 		lncntp[lfdir]++;
    473 		pwarn("DIR I=%u CONNECTED. ", orphan);
    474 		if (parentdir != (ino_t) - 1)
    475 			printf("PARENT WAS I=%u\n", parentdir);
    476 		if (preen == 0)
    477 			printf("\n");
    478 	}
    479 	return (1);
    480 }
    481 
    482 /*
    483  * fix an entry in a directory.
    484  */
    485 int
    486 changeino(ino_t dir, char *name, ino_t newnum)
    487 {
    488 	struct inodesc idesc;
    489 
    490 	memset(&idesc, 0, sizeof(struct inodesc));
    491 	idesc.id_type = DATA;
    492 	idesc.id_func = chgino;
    493 	idesc.id_number = dir;
    494 	idesc.id_fix = DONTKNOW;
    495 	idesc.id_name = name;
    496 	idesc.id_parent = newnum;	/* new value for name */
    497 
    498 	return (ckinode(ginode(dir), &idesc));
    499 }
    500 
    501 /*
    502  * make an entry in a directory
    503  */
    504 int
    505 makeentry(ino_t parent, ino_t ino, char *name)
    506 {
    507 	struct ufs1_dinode *dp;
    508 	struct inodesc idesc;
    509 	char pathbuf[MAXPATHLEN + 1];
    510 	struct uvnode *vp;
    511 
    512 	if (parent < ROOTINO || parent >= maxino ||
    513 	    ino < ROOTINO || ino >= maxino)
    514 		return (0);
    515 	memset(&idesc, 0, sizeof(struct inodesc));
    516 	idesc.id_type = DATA;
    517 	idesc.id_func = mkentry;
    518 	idesc.id_number = parent;
    519 	idesc.id_parent = ino;	/* this is the inode to enter */
    520 	idesc.id_fix = DONTKNOW;
    521 	idesc.id_name = name;
    522 	vp = vget(fs, parent);
    523 	dp = VTOD(vp);
    524 	if (dp->di_size % DIRBLKSIZ) {
    525 		dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
    526 		inodirty(VTOI(vp));
    527 	}
    528 	if ((ckinode(dp, &idesc) & ALTERED) != 0)
    529 		return (1);
    530 	getpathname(pathbuf, sizeof(pathbuf), parent, parent);
    531 	vp = vget(fs, parent);
    532 	dp = VTOD(vp);
    533 	if (expanddir(vp, dp, pathbuf) == 0)
    534 		return (0);
    535 	return (ckinode(dp, &idesc) & ALTERED);
    536 }
    537 
    538 /*
    539  * Attempt to expand the size of a directory
    540  */
    541 static int
    542 expanddir(struct uvnode *vp, struct ufs1_dinode *dp, char *name)
    543 {
    544 	daddr_t lastbn, newblk;
    545 	struct ubuf *bp;
    546 	char *cp, firstblk[DIRBLKSIZ];
    547 
    548 	lastbn = lblkno(fs, dp->di_size);
    549 	if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
    550 		return (0);
    551 	dp->di_db[lastbn + 1] = dp->di_db[lastbn];
    552 	dp->di_db[lastbn] = 0;
    553 	bp = getblk(vp, lastbn, fs->lfs_bsize);
    554 	VOP_BWRITE(bp);
    555 	dp->di_size += fs->lfs_bsize;
    556 	dp->di_blocks += btofsb(fs, fs->lfs_bsize);
    557 	bread(vp, dp->di_db[lastbn + 1],
    558 	    (long) dblksize(fs, dp, lastbn + 1), NOCRED, &bp);
    559 	if (bp->b_flags & B_ERROR)
    560 		goto bad;
    561 	memcpy(firstblk, bp->b_data, DIRBLKSIZ);
    562 	bread(vp, newblk, fs->lfs_bsize, NOCRED, &bp);
    563 	if (bp->b_flags & B_ERROR)
    564 		goto bad;
    565 	memcpy(bp->b_data, firstblk, DIRBLKSIZ);
    566 	for (cp = &bp->b_data[DIRBLKSIZ];
    567 	    cp < &bp->b_data[fs->lfs_bsize];
    568 	    cp += DIRBLKSIZ)
    569 		memcpy(cp, &emptydir, sizeof emptydir);
    570 	VOP_BWRITE(bp);
    571 	bread(vp, dp->di_db[lastbn + 1],
    572 	    (long) dblksize(fs, dp, lastbn + 1), NOCRED, &bp);
    573 	if (bp->b_flags & B_ERROR)
    574 		goto bad;
    575 	memcpy(bp->b_data, &emptydir, sizeof emptydir);
    576 	pwarn("NO SPACE LEFT IN %s", name);
    577 	if (preen)
    578 		printf(" (EXPANDED)\n");
    579 	else if (reply("EXPAND") == 0)
    580 		goto bad;
    581 	VOP_BWRITE(bp);
    582 	inodirty(VTOI(vp));
    583 	return (1);
    584 bad:
    585 	dp->di_db[lastbn] = dp->di_db[lastbn + 1];
    586 	dp->di_db[lastbn + 1] = 0;
    587 	dp->di_size -= fs->lfs_bsize;
    588 	dp->di_blocks -= btofsb(fs, fs->lfs_bsize);
    589 	freeblk(newblk, fs->lfs_frag);
    590 	return (0);
    591 }
    592 
    593 /*
    594  * allocate a new directory
    595  */
    596 int
    597 allocdir(ino_t parent, ino_t request, int mode)
    598 {
    599 	ino_t ino;
    600 	char *cp;
    601 	struct ufs1_dinode *dp;
    602 	struct ubuf *bp;
    603 	struct dirtemplate *dirp;
    604 	struct uvnode *vp;
    605 
    606 	ino = allocino(request, IFDIR | mode);
    607 	dirp = &dirhead;
    608 	dirp->dot_ino = ino;
    609 	dirp->dotdot_ino = parent;
    610 	vp = vget(fs, ino);
    611 	dp = VTOD(vp);
    612 	bread(vp, dp->di_db[0], fs->lfs_fsize, NOCRED, &bp);
    613 	if (bp->b_flags & B_ERROR) {
    614 		brelse(bp);
    615 		freeino(ino);
    616 		return (0);
    617 	}
    618 	memcpy(bp->b_data, dirp, sizeof(struct dirtemplate));
    619 	for (cp = &bp->b_data[DIRBLKSIZ];
    620 	    cp < &bp->b_data[fs->lfs_fsize];
    621 	    cp += DIRBLKSIZ)
    622 		memcpy(cp, &emptydir, sizeof emptydir);
    623 	VOP_BWRITE(bp);
    624 	dp->di_nlink = 2;
    625 	inodirty(VTOI(vp));
    626 	if (ino == ROOTINO) {
    627 		lncntp[ino] = dp->di_nlink;
    628 		cacheino(dp, ino);
    629 		return (ino);
    630 	}
    631 	if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
    632 		freeino(ino);
    633 		return (0);
    634 	}
    635 	cacheino(dp, ino);
    636 	statemap[ino] = statemap[parent];
    637 	if (statemap[ino] == DSTATE) {
    638 		lncntp[ino] = dp->di_nlink;
    639 		lncntp[parent]++;
    640 	}
    641 	vp = vget(fs, parent);
    642 	dp = VTOD(vp);
    643 	dp->di_nlink++;
    644 	inodirty(VTOI(vp));
    645 	return (ino);
    646 }
    647 
    648 /*
    649  * free a directory inode
    650  */
    651 static void
    652 freedir(ino_t ino, ino_t parent)
    653 {
    654 	struct uvnode *vp;
    655 
    656 	if (ino != parent) {
    657 		vp = vget(fs, parent);
    658 		VTOI(vp)->i_ffs1_nlink--;
    659 		inodirty(VTOI(vp));
    660 	}
    661 	freeino(ino);
    662 }
    663 
    664 /*
    665  * generate a temporary name for the lost+found directory.
    666  */
    667 static int
    668 lftempname(char *bufp, ino_t ino)
    669 {
    670 	ino_t in;
    671 	char *cp;
    672 	int namlen;
    673 
    674 	cp = bufp + 2;
    675 	for (in = maxino; in > 0; in /= 10)
    676 		cp++;
    677 	*--cp = 0;
    678 	namlen = cp - bufp;
    679 	in = ino;
    680 	while (cp > bufp) {
    681 		*--cp = (in % 10) + '0';
    682 		in /= 10;
    683 	}
    684 	*cp = '#';
    685 	return (namlen);
    686 }
    687