Home | History | Annotate | Line # | Download | only in fsck_lfs
dir.c revision 1.12
      1 /* $NetBSD: dir.c,v 1.12 2003/10/03 12:23:22 yamt 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 	struct inode *ip;
    287 
    288 	pwarn("%s ", errmesg);
    289 	pinode(ino);
    290 	printf("\n");
    291 	getpathname(pathbuf, sizeof(pathbuf), cwd, ino);
    292 	if (ino < ROOTINO || ino >= maxino) {
    293 		pfatal("NAME=%s\n", pathbuf);
    294 		return;
    295 	}
    296 	vp = vget(fs, ino);
    297 	ip = VTOI(vp);
    298 	if (vp == NULL)
    299 		pfatal("INO is NULL\n");
    300 	else {
    301 		if (ftypeok(VTOD(vp)))
    302 			pfatal("%s=%s\n",
    303 			    (ip->i_ffs1_mode & IFMT) == IFDIR ?
    304 			    "DIR" : "FILE", pathbuf);
    305 		else
    306 			pfatal("NAME=%s\n", pathbuf);
    307 	}
    308 }
    309 
    310 void
    311 adjust(struct inodesc *idesc, short lcnt)
    312 {
    313 	struct uvnode *vp;
    314 	struct ufs1_dinode *dp;
    315 
    316 	vp = vget(fs, idesc->id_number);
    317 	dp = VTOD(vp);
    318 	if (dp->di_nlink == lcnt) {
    319 		if (linkup(idesc->id_number, (ino_t) 0) == 0)
    320 			clri(idesc, "UNREF", 0);
    321 	} else {
    322 		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
    323 		    ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
    324 		pinode(idesc->id_number);
    325 		printf(" COUNT %d SHOULD BE %d",
    326 		    dp->di_nlink, dp->di_nlink - lcnt);
    327 		if (preen) {
    328 			if (lcnt < 0) {
    329 				printf("\n");
    330 				pfatal("LINK COUNT INCREASING");
    331 			}
    332 			printf(" (ADJUSTED)\n");
    333 		}
    334 		if (preen || reply("ADJUST") == 1) {
    335 			dp->di_nlink -= lcnt;
    336 			inodirty(VTOI(vp));
    337 		}
    338 	}
    339 }
    340 
    341 static int
    342 mkentry(struct inodesc *idesc)
    343 {
    344 	struct direct *dirp = idesc->id_dirp;
    345 	struct direct newent;
    346 	int newlen, oldlen;
    347 
    348 	newent.d_namlen = strlen(idesc->id_name);
    349 	newlen = DIRSIZ(0, &newent, 0);
    350 	if (dirp->d_ino != 0)
    351 		oldlen = DIRSIZ(0, dirp, 0);
    352 	else
    353 		oldlen = 0;
    354 	if (dirp->d_reclen - oldlen < newlen)
    355 		return (KEEPON);
    356 	newent.d_reclen = dirp->d_reclen - oldlen;
    357 	dirp->d_reclen = oldlen;
    358 	dirp = (struct direct *) (((char *) dirp) + oldlen);
    359 	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
    360 	dirp->d_reclen = newent.d_reclen;
    361 	dirp->d_type = typemap[idesc->id_parent];
    362 	dirp->d_namlen = newent.d_namlen;
    363 	memcpy(dirp->d_name, idesc->id_name, (size_t) dirp->d_namlen + 1);
    364 	return (ALTERED | STOP);
    365 }
    366 
    367 static int
    368 chgino(struct inodesc *idesc)
    369 {
    370 	struct direct *dirp = idesc->id_dirp;
    371 
    372 	if (memcmp(dirp->d_name, idesc->id_name, (int) dirp->d_namlen + 1))
    373 		return (KEEPON);
    374 	dirp->d_ino = idesc->id_parent;
    375 	dirp->d_type = typemap[idesc->id_parent];
    376 	return (ALTERED | STOP);
    377 }
    378 
    379 int
    380 linkup(ino_t orphan, ino_t parentdir)
    381 {
    382 	struct ufs1_dinode *dp;
    383 	int lostdir;
    384 	ino_t oldlfdir;
    385 	struct inodesc idesc;
    386 	char tempname[BUFSIZ];
    387 	struct uvnode *vp;
    388 
    389 	memset(&idesc, 0, sizeof(struct inodesc));
    390 	vp = vget(fs, orphan);
    391 	dp = VTOD(vp);
    392 	lostdir = (dp->di_mode & IFMT) == IFDIR;
    393 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
    394 	pinode(orphan);
    395 	if (preen && dp->di_size == 0)
    396 		return (0);
    397 	if (preen)
    398 		printf(" (RECONNECTED)\n");
    399 	else if (reply("RECONNECT") == 0)
    400 		return (0);
    401 	if (lfdir == 0) {
    402 		dp = ginode(ROOTINO);
    403 		idesc.id_name = lfname;
    404 		idesc.id_type = DATA;
    405 		idesc.id_func = findino;
    406 		idesc.id_number = ROOTINO;
    407 		if ((ckinode(dp, &idesc) & FOUND) != 0) {
    408 			lfdir = idesc.id_parent;
    409 		} else {
    410 			pwarn("NO lost+found DIRECTORY");
    411 			if (preen || reply("CREATE")) {
    412 				lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode);
    413 				if (lfdir != 0) {
    414 					if (makeentry(ROOTINO, lfdir, lfname) != 0) {
    415 						if (preen)
    416 							printf(" (CREATED)\n");
    417 					} else {
    418 						freedir(lfdir, ROOTINO);
    419 						lfdir = 0;
    420 						if (preen)
    421 							printf("\n");
    422 					}
    423 				}
    424 			}
    425 		}
    426 		if (lfdir == 0) {
    427 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
    428 			printf("\n\n");
    429 			return (0);
    430 		}
    431 	}
    432 	vp = vget(fs, lfdir);
    433 	dp = VTOD(vp);
    434 	if ((dp->di_mode & IFMT) != IFDIR) {
    435 		pfatal("lost+found IS NOT A DIRECTORY");
    436 		if (reply("REALLOCATE") == 0)
    437 			return (0);
    438 		oldlfdir = lfdir;
    439 		if ((lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode)) == 0) {
    440 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
    441 			return (0);
    442 		}
    443 		if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
    444 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
    445 			return (0);
    446 		}
    447 		inodirty(VTOI(vp));
    448 		idesc.id_type = ADDR;
    449 		idesc.id_func = pass4check;
    450 		idesc.id_number = oldlfdir;
    451 		adjust(&idesc, lncntp[oldlfdir] + 1);
    452 		lncntp[oldlfdir] = 0;
    453 		vp = vget(fs, lfdir);
    454 		dp = VTOD(vp);
    455 	}
    456 	if (statemap[lfdir] != DFOUND) {
    457 		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
    458 		return (0);
    459 	}
    460 	(void) lftempname(tempname, orphan);
    461 	if (makeentry(lfdir, orphan, tempname) == 0) {
    462 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
    463 		printf("\n\n");
    464 		return (0);
    465 	}
    466 	lncntp[orphan]--;
    467 	if (lostdir) {
    468 		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
    469 		    parentdir != (ino_t) - 1)
    470 			(void) makeentry(orphan, lfdir, "..");
    471 		vp = vget(fs, lfdir);
    472 		VTOI(vp)->i_ffs1_nlink++;
    473 		inodirty(VTOI(vp));
    474 		lncntp[lfdir]++;
    475 		pwarn("DIR I=%u CONNECTED. ", orphan);
    476 		if (parentdir != (ino_t) - 1)
    477 			printf("PARENT WAS I=%u\n", parentdir);
    478 		if (preen == 0)
    479 			printf("\n");
    480 	}
    481 	return (1);
    482 }
    483 
    484 /*
    485  * fix an entry in a directory.
    486  */
    487 int
    488 changeino(ino_t dir, char *name, ino_t newnum)
    489 {
    490 	struct inodesc idesc;
    491 
    492 	memset(&idesc, 0, sizeof(struct inodesc));
    493 	idesc.id_type = DATA;
    494 	idesc.id_func = chgino;
    495 	idesc.id_number = dir;
    496 	idesc.id_fix = DONTKNOW;
    497 	idesc.id_name = name;
    498 	idesc.id_parent = newnum;	/* new value for name */
    499 
    500 	return (ckinode(ginode(dir), &idesc));
    501 }
    502 
    503 /*
    504  * make an entry in a directory
    505  */
    506 int
    507 makeentry(ino_t parent, ino_t ino, char *name)
    508 {
    509 	struct ufs1_dinode *dp;
    510 	struct inodesc idesc;
    511 	char pathbuf[MAXPATHLEN + 1];
    512 	struct uvnode *vp;
    513 
    514 	if (parent < ROOTINO || parent >= maxino ||
    515 	    ino < ROOTINO || ino >= maxino)
    516 		return (0);
    517 	memset(&idesc, 0, sizeof(struct inodesc));
    518 	idesc.id_type = DATA;
    519 	idesc.id_func = mkentry;
    520 	idesc.id_number = parent;
    521 	idesc.id_parent = ino;	/* this is the inode to enter */
    522 	idesc.id_fix = DONTKNOW;
    523 	idesc.id_name = name;
    524 	vp = vget(fs, parent);
    525 	dp = VTOD(vp);
    526 	if (dp->di_size % DIRBLKSIZ) {
    527 		dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
    528 		inodirty(VTOI(vp));
    529 	}
    530 	if ((ckinode(dp, &idesc) & ALTERED) != 0)
    531 		return (1);
    532 	getpathname(pathbuf, sizeof(pathbuf), parent, parent);
    533 	vp = vget(fs, parent);
    534 	dp = VTOD(vp);
    535 	if (expanddir(vp, dp, pathbuf) == 0)
    536 		return (0);
    537 	return (ckinode(dp, &idesc) & ALTERED);
    538 }
    539 
    540 /*
    541  * Attempt to expand the size of a directory
    542  */
    543 static int
    544 expanddir(struct uvnode *vp, struct ufs1_dinode *dp, char *name)
    545 {
    546 	daddr_t lastbn, newblk;
    547 	struct ubuf *bp;
    548 	char *cp, firstblk[DIRBLKSIZ];
    549 
    550 	lastbn = lblkno(fs, dp->di_size);
    551 	if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
    552 		return (0);
    553 	dp->di_db[lastbn + 1] = dp->di_db[lastbn];
    554 	dp->di_db[lastbn] = 0;
    555 	bp = getblk(vp, lastbn, fs->lfs_bsize);
    556 	VOP_BWRITE(bp);
    557 	dp->di_size += fs->lfs_bsize;
    558 	dp->di_blocks += btofsb(fs, fs->lfs_bsize);
    559 	bread(vp, dp->di_db[lastbn + 1],
    560 	    (long) dblksize(fs, dp, lastbn + 1), NOCRED, &bp);
    561 	if (bp->b_flags & B_ERROR)
    562 		goto bad;
    563 	memcpy(firstblk, bp->b_data, DIRBLKSIZ);
    564 	bread(vp, newblk, fs->lfs_bsize, NOCRED, &bp);
    565 	if (bp->b_flags & B_ERROR)
    566 		goto bad;
    567 	memcpy(bp->b_data, firstblk, DIRBLKSIZ);
    568 	for (cp = &bp->b_data[DIRBLKSIZ];
    569 	    cp < &bp->b_data[fs->lfs_bsize];
    570 	    cp += DIRBLKSIZ)
    571 		memcpy(cp, &emptydir, sizeof emptydir);
    572 	VOP_BWRITE(bp);
    573 	bread(vp, dp->di_db[lastbn + 1],
    574 	    (long) dblksize(fs, dp, lastbn + 1), NOCRED, &bp);
    575 	if (bp->b_flags & B_ERROR)
    576 		goto bad;
    577 	memcpy(bp->b_data, &emptydir, sizeof emptydir);
    578 	pwarn("NO SPACE LEFT IN %s", name);
    579 	if (preen)
    580 		printf(" (EXPANDED)\n");
    581 	else if (reply("EXPAND") == 0)
    582 		goto bad;
    583 	VOP_BWRITE(bp);
    584 	inodirty(VTOI(vp));
    585 	return (1);
    586 bad:
    587 	dp->di_db[lastbn] = dp->di_db[lastbn + 1];
    588 	dp->di_db[lastbn + 1] = 0;
    589 	dp->di_size -= fs->lfs_bsize;
    590 	dp->di_blocks -= btofsb(fs, fs->lfs_bsize);
    591 	freeblk(newblk, fs->lfs_frag);
    592 	return (0);
    593 }
    594 
    595 /*
    596  * allocate a new directory
    597  */
    598 int
    599 allocdir(ino_t parent, ino_t request, int mode)
    600 {
    601 	ino_t ino;
    602 	char *cp;
    603 	struct ufs1_dinode *dp;
    604 	struct ubuf *bp;
    605 	struct dirtemplate *dirp;
    606 	struct uvnode *vp;
    607 
    608 	ino = allocino(request, IFDIR | mode);
    609 	dirp = &dirhead;
    610 	dirp->dot_ino = ino;
    611 	dirp->dotdot_ino = parent;
    612 	vp = vget(fs, ino);
    613 	dp = VTOD(vp);
    614 	bread(vp, dp->di_db[0], fs->lfs_fsize, NOCRED, &bp);
    615 	if (bp->b_flags & B_ERROR) {
    616 		brelse(bp);
    617 		freeino(ino);
    618 		return (0);
    619 	}
    620 	memcpy(bp->b_data, dirp, sizeof(struct dirtemplate));
    621 	for (cp = &bp->b_data[DIRBLKSIZ];
    622 	    cp < &bp->b_data[fs->lfs_fsize];
    623 	    cp += DIRBLKSIZ)
    624 		memcpy(cp, &emptydir, sizeof emptydir);
    625 	VOP_BWRITE(bp);
    626 	dp->di_nlink = 2;
    627 	inodirty(VTOI(vp));
    628 	if (ino == ROOTINO) {
    629 		lncntp[ino] = dp->di_nlink;
    630 		cacheino(dp, ino);
    631 		return (ino);
    632 	}
    633 	if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
    634 		freeino(ino);
    635 		return (0);
    636 	}
    637 	cacheino(dp, ino);
    638 	statemap[ino] = statemap[parent];
    639 	if (statemap[ino] == DSTATE) {
    640 		lncntp[ino] = dp->di_nlink;
    641 		lncntp[parent]++;
    642 	}
    643 	vp = vget(fs, parent);
    644 	dp = VTOD(vp);
    645 	dp->di_nlink++;
    646 	inodirty(VTOI(vp));
    647 	return (ino);
    648 }
    649 
    650 /*
    651  * free a directory inode
    652  */
    653 static void
    654 freedir(ino_t ino, ino_t parent)
    655 {
    656 	struct uvnode *vp;
    657 
    658 	if (ino != parent) {
    659 		vp = vget(fs, parent);
    660 		VTOI(vp)->i_ffs1_nlink--;
    661 		inodirty(VTOI(vp));
    662 	}
    663 	freeino(ino);
    664 }
    665 
    666 /*
    667  * generate a temporary name for the lost+found directory.
    668  */
    669 static int
    670 lftempname(char *bufp, ino_t ino)
    671 {
    672 	ino_t in;
    673 	char *cp;
    674 	int namlen;
    675 
    676 	cp = bufp + 2;
    677 	for (in = maxino; in > 0; in /= 10)
    678 		cp++;
    679 	*--cp = 0;
    680 	namlen = cp - bufp;
    681 	in = ino;
    682 	while (cp > bufp) {
    683 		*--cp = (in % 10) + '0';
    684 		in /= 10;
    685 	}
    686 	*cp = '#';
    687 	return (namlen);
    688 }
    689