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