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