xref: /netbsd-src/sbin/fsck_lfs/dir.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /* $NetBSD: dir.c,v 1.19 2005/09/13 04:14:17 christos 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_user.h"
51 
52 #include "fsck.h"
53 #include "fsutil.h"
54 #include "extern.h"
55 
56 const 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(void)
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 = %llu\tspaceleft = 0x%x\n",
248 		    (unsigned long long)maxino, spaceleft);
249 		return (0);
250 	}
251 	if (dp->d_ino == 0)
252 		return (1);
253 	size = DIRSIZ(0, dp, 0);
254 	namlen = dp->d_namlen;
255 	type = dp->d_type;
256 	if (dp->d_reclen < size ||
257 	    idesc->id_filesize < size ||
258 	/* namlen > MAXNAMLEN || */
259 	    type > 15) {
260 		printf("reclen<size, filesize<size, namlen too large, or type>15\n");
261 		return (0);
262 	}
263 	for (cp = dp->d_name, size = 0; size < namlen; size++)
264 		if (*cp == '\0' || (*cp++ == '/')) {
265 			printf("name contains NUL or /\n");
266 			return (0);
267 		}
268 	if (*cp != '\0') {
269 		printf("name size misstated\n");
270 		return (0);
271 	}
272 	return (1);
273 }
274 
275 void
276 direrror(ino_t ino, const char *errmesg)
277 {
278 
279 	fileerror(ino, ino, errmesg);
280 }
281 
282 void
283 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
284 {
285 	char pathbuf[MAXPATHLEN + 1];
286 	struct uvnode *vp;
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 	if (vp == NULL)
298 		pfatal("INO is NULL\n");
299 	else {
300 		if (ftypeok(VTOD(vp)))
301 			pfatal("%s=%s\n",
302 			    (VTOI(vp)->i_ffs1_mode & IFMT) == IFDIR ?
303 			    "DIR" : "FILE", pathbuf);
304 		else
305 			pfatal("NAME=%s\n", pathbuf);
306 	}
307 }
308 
309 void
310 adjust(struct inodesc *idesc, short lcnt)
311 {
312 	struct uvnode *vp;
313 	struct ufs1_dinode *dp;
314 
315 	vp = vget(fs, idesc->id_number);
316 	dp = VTOD(vp);
317 	if (dp->di_nlink == lcnt) {
318 		if (linkup(idesc->id_number, (ino_t) 0) == 0)
319 			clri(idesc, "UNREF", 0);
320 	} else {
321 		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
322 		    ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
323 		pinode(idesc->id_number);
324 		printf(" COUNT %d SHOULD BE %d",
325 		    dp->di_nlink, dp->di_nlink - lcnt);
326 		if (preen) {
327 			if (lcnt < 0) {
328 				printf("\n");
329 				pfatal("LINK COUNT INCREASING");
330 			}
331 			printf(" (ADJUSTED)\n");
332 		}
333 		if (preen || reply("ADJUST") == 1) {
334 			dp->di_nlink -= lcnt;
335 			inodirty(VTOI(vp));
336 		}
337 	}
338 }
339 
340 static int
341 mkentry(struct inodesc *idesc)
342 {
343 	struct direct *dirp = idesc->id_dirp;
344 	struct direct newent;
345 	int newlen, oldlen;
346 
347 	newent.d_namlen = strlen(idesc->id_name);
348 	newlen = DIRSIZ(0, &newent, 0);
349 	if (dirp->d_ino != 0)
350 		oldlen = DIRSIZ(0, dirp, 0);
351 	else
352 		oldlen = 0;
353 	if (dirp->d_reclen - oldlen < newlen)
354 		return (KEEPON);
355 	newent.d_reclen = dirp->d_reclen - oldlen;
356 	dirp->d_reclen = oldlen;
357 	dirp = (struct direct *) (((char *) dirp) + oldlen);
358 	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
359 	dirp->d_reclen = newent.d_reclen;
360 	dirp->d_type = typemap[idesc->id_parent];
361 	dirp->d_namlen = newent.d_namlen;
362 	memcpy(dirp->d_name, idesc->id_name, (size_t) dirp->d_namlen + 1);
363 	return (ALTERED | STOP);
364 }
365 
366 static int
367 chgino(struct inodesc *idesc)
368 {
369 	struct direct *dirp = idesc->id_dirp;
370 
371 	if (memcmp(dirp->d_name, idesc->id_name, (int) dirp->d_namlen + 1))
372 		return (KEEPON);
373 	dirp->d_ino = idesc->id_parent;
374 	dirp->d_type = typemap[idesc->id_parent];
375 	return (ALTERED | STOP);
376 }
377 
378 int
379 linkup(ino_t orphan, ino_t parentdir)
380 {
381 	struct ufs1_dinode *dp;
382 	int lostdir;
383 	ino_t oldlfdir;
384 	struct inodesc idesc;
385 	char tempname[BUFSIZ];
386 	struct uvnode *vp;
387 
388 	memset(&idesc, 0, sizeof(struct inodesc));
389 	vp = vget(fs, orphan);
390 	dp = VTOD(vp);
391 	lostdir = (dp->di_mode & IFMT) == IFDIR;
392 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
393 	pinode(orphan);
394 	if (preen && dp->di_size == 0)
395 		return (0);
396 	if (preen)
397 		printf(" (RECONNECTED)\n");
398 	else if (reply("RECONNECT") == 0)
399 		return (0);
400 	if (lfdir == 0) {
401 		dp = ginode(ROOTINO);
402 		idesc.id_name = lfname;
403 		idesc.id_type = DATA;
404 		idesc.id_func = findino;
405 		idesc.id_number = ROOTINO;
406 		if ((ckinode(dp, &idesc) & FOUND) != 0) {
407 			lfdir = idesc.id_parent;
408 		} else {
409 			pwarn("NO lost+found DIRECTORY");
410 			if (preen || reply("CREATE")) {
411 				lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode);
412 				if (lfdir != 0) {
413 					if (makeentry(ROOTINO, lfdir, lfname) != 0) {
414 						if (preen)
415 							printf(" (CREATED)\n");
416 					} else {
417 						freedir(lfdir, ROOTINO);
418 						lfdir = 0;
419 						if (preen)
420 							printf("\n");
421 					}
422 				}
423 			}
424 		}
425 		if (lfdir == 0) {
426 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
427 			printf("\n\n");
428 			return (0);
429 		}
430 	}
431 	vp = vget(fs, lfdir);
432 	dp = VTOD(vp);
433 	if ((dp->di_mode & IFMT) != IFDIR) {
434 		pfatal("lost+found IS NOT A DIRECTORY");
435 		if (reply("REALLOCATE") == 0)
436 			return (0);
437 		oldlfdir = lfdir;
438 		if ((lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode)) == 0) {
439 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
440 			return (0);
441 		}
442 		if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
443 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
444 			return (0);
445 		}
446 		inodirty(VTOI(vp));
447 		idesc.id_type = ADDR;
448 		idesc.id_func = pass4check;
449 		idesc.id_number = oldlfdir;
450 		adjust(&idesc, lncntp[oldlfdir] + 1);
451 		lncntp[oldlfdir] = 0;
452 		vp = vget(fs, lfdir);
453 		dp = VTOD(vp);
454 	}
455 	if (statemap[lfdir] != DFOUND) {
456 		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
457 		return (0);
458 	}
459 	(void) lftempname(tempname, orphan);
460 	if (makeentry(lfdir, orphan, tempname) == 0) {
461 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
462 		printf("\n\n");
463 		return (0);
464 	}
465 	lncntp[orphan]--;
466 	if (lostdir) {
467 		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
468 		    parentdir != (ino_t) - 1)
469 			(void) makeentry(orphan, lfdir, "..");
470 		vp = vget(fs, lfdir);
471 		VTOI(vp)->i_ffs1_nlink++;
472 		inodirty(VTOI(vp));
473 		lncntp[lfdir]++;
474 		pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan);
475 		if (parentdir != (ino_t) - 1)
476 			printf("PARENT WAS I=%llu\n",
477 			    (unsigned long long)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, const 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, const 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;
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, lastbn, 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 	return (0);
592 }
593 
594 /*
595  * allocate a new directory
596  */
597 int
598 allocdir(ino_t parent, ino_t request, int mode)
599 {
600 	ino_t ino;
601 	char *cp;
602 	struct ufs1_dinode *dp;
603 	struct ubuf *bp;
604 	struct dirtemplate *dirp;
605 	struct uvnode *vp;
606 
607 	ino = allocino(request, IFDIR | mode);
608 	dirp = &dirhead;
609 	dirp->dot_ino = ino;
610 	dirp->dotdot_ino = parent;
611 	vp = vget(fs, ino);
612 	dp = VTOD(vp);
613 	bread(vp, dp->di_db[0], fs->lfs_fsize, NOCRED, &bp);
614 	if (bp->b_flags & B_ERROR) {
615 		brelse(bp);
616 		freeino(ino);
617 		return (0);
618 	}
619 	memcpy(bp->b_data, dirp, sizeof(struct dirtemplate));
620 	for (cp = &bp->b_data[DIRBLKSIZ];
621 	    cp < &bp->b_data[fs->lfs_fsize];
622 	    cp += DIRBLKSIZ)
623 		memcpy(cp, &emptydir, sizeof emptydir);
624 	VOP_BWRITE(bp);
625 	dp->di_nlink = 2;
626 	inodirty(VTOI(vp));
627 	if (ino == ROOTINO) {
628 		lncntp[ino] = dp->di_nlink;
629 		cacheino(dp, ino);
630 		return (ino);
631 	}
632 	if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
633 		freeino(ino);
634 		return (0);
635 	}
636 	cacheino(dp, ino);
637 	statemap[ino] = statemap[parent];
638 	if (statemap[ino] == DSTATE) {
639 		lncntp[ino] = dp->di_nlink;
640 		lncntp[parent]++;
641 	}
642 	vp = vget(fs, parent);
643 	dp = VTOD(vp);
644 	dp->di_nlink++;
645 	inodirty(VTOI(vp));
646 	return (ino);
647 }
648 
649 /*
650  * free a directory inode
651  */
652 static void
653 freedir(ino_t ino, ino_t parent)
654 {
655 	struct uvnode *vp;
656 
657 	if (ino != parent) {
658 		vp = vget(fs, parent);
659 		VTOI(vp)->i_ffs1_nlink--;
660 		inodirty(VTOI(vp));
661 	}
662 	freeino(ino);
663 }
664 
665 /*
666  * generate a temporary name for the lost+found directory.
667  */
668 static int
669 lftempname(char *bufp, ino_t ino)
670 {
671 	ino_t in;
672 	char *cp;
673 	int namlen;
674 
675 	cp = bufp + 2;
676 	for (in = maxino; in > 0; in /= 10)
677 		cp++;
678 	*--cp = 0;
679 	namlen = cp - bufp;
680 	in = ino;
681 	while (cp > bufp) {
682 		*--cp = (in % 10) + '0';
683 		in /= 10;
684 	}
685 	*cp = '#';
686 	return (namlen);
687 }
688