dir.c revision 23796
1234370Sjasone/*
2234370Sjasone * Copyright (c) 1980, 1986, 1993
3234370Sjasone *	The Regents of the University of California.  All rights reserved.
4234370Sjasone *
5234370Sjasone * Redistribution and use in source and binary forms, with or without
6234370Sjasone * modification, are permitted provided that the following conditions
7234370Sjasone * are met:
8234370Sjasone * 1. Redistributions of source code must retain the above copyright
9234370Sjasone *    notice, this list of conditions and the following disclaimer.
10234370Sjasone * 2. Redistributions in binary form must reproduce the above copyright
11234370Sjasone *    notice, this list of conditions and the following disclaimer in the
12245869Sjasone *    documentation and/or other materials provided with the distribution.
13234370Sjasone * 3. All advertising materials mentioning features or use of this software
14234370Sjasone *    must display the following acknowledgement:
15234370Sjasone *	This product includes software developed by the University of
16234370Sjasone *	California, Berkeley and its contributors.
17234370Sjasone * 4. Neither the name of the University nor the names of its contributors
18234370Sjasone *    may be used to endorse or promote products derived from this software
19234370Sjasone *    without specific prior written permission.
20234370Sjasone *
21234370Sjasone * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22234370Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23234370Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24234370Sjasone * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25234370Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26234370Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27234370Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28234370Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29234370Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30234370Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31234370Sjasone * SUCH DAMAGE.
32234370Sjasone */
33234370Sjasone
34234370Sjasone#ifndef lint
35234370Sjasonestatic const char sccsid[] = "@(#)dir.c	8.8 (Berkeley) 4/28/95";
36234370Sjasone#endif /* not lint */
37234370Sjasone
38234370Sjasone#include <sys/param.h>
39234370Sjasone#include <sys/time.h>
40234370Sjasone
41234370Sjasone#include <ufs/ufs/dinode.h>
42234370Sjasone#include <ufs/ufs/dir.h>
43234370Sjasone#include <ufs/ffs/fs.h>
44234370Sjasone
45234370Sjasone#include <err.h>
46234370Sjasone#include <string.h>
47234370Sjasone
48234370Sjasone#include "fsck.h"
49234370Sjasone
50234370Sjasonechar	*lfname = "lost+found";
51234370Sjasoneint	lfmode = 01777;
52234370Sjasonestruct	dirtemplate emptydir = { 0, DIRBLKSIZ };
53234370Sjasonestruct	dirtemplate dirhead = {
54234370Sjasone	0, 12, DT_DIR, 1, ".",
55234370Sjasone	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
56234370Sjasone};
57234370Sjasonestruct	odirtemplate odirhead = {
58234370Sjasone	0, 12, 1, ".",
59234370Sjasone	0, DIRBLKSIZ - 12, 2, ".."
60234370Sjasone};
61234370Sjasone
62234370Sjasonestatic int chgino __P((struct inodesc *));
63234370Sjasonestatic int dircheck __P((struct inodesc *, struct direct *));
64234370Sjasonestatic int expanddir __P((struct dinode *dp, char *name));
65234370Sjasonestatic void freedir __P((ino_t ino, ino_t parent));
66234370Sjasonestatic struct direct *fsck_readdir __P((struct inodesc *));
67234370Sjasonestatic struct bufarea *getdirblk __P((ufs_daddr_t blkno, long size));
68234370Sjasonestatic int lftempname __P((char *bufp, ino_t ino));
69234370Sjasonestatic int mkentry __P((struct inodesc *));
70234370Sjasone
71234370Sjasone/*
72234370Sjasone * Propagate connected state through the tree.
73234370Sjasone */
74234370Sjasonevoid
75234370Sjasonepropagate()
76234543Sjasone{
77234543Sjasone	register struct inoinfo **inpp, *inp;
78234370Sjasone	struct inoinfo **inpend;
79234370Sjasone	long change;
80234370Sjasone
81234370Sjasone	inpend = &inpsort[inplast];
82234370Sjasone	do {
83234370Sjasone		change = 0;
84234370Sjasone		for (inpp = inpsort; inpp < inpend; inpp++) {
85234370Sjasone			inp = *inpp;
86234370Sjasone			if (inp->i_parent == 0)
87234370Sjasone				continue;
88234370Sjasone			if (statemap[inp->i_parent] == DFOUND &&
89234370Sjasone			    statemap[inp->i_number] == DSTATE) {
90234370Sjasone				statemap[inp->i_number] = DFOUND;
91234370Sjasone				change++;
92234370Sjasone			}
93234370Sjasone		}
94234370Sjasone	} while (change > 0);
95234370Sjasone}
96234370Sjasone
97234370Sjasone/*
98234370Sjasone * Scan each entry in a directory block.
99234370Sjasone */
100234370Sjasoneint
101234370Sjasonedirscan(idesc)
102234370Sjasone	register struct inodesc *idesc;
103234370Sjasone{
104234370Sjasone	register struct direct *dp;
105234370Sjasone	register struct bufarea *bp;
106234370Sjasone	int dsize, n;
107234370Sjasone	long blksiz;
108234370Sjasone	char dbuf[DIRBLKSIZ];
109234370Sjasone
110234370Sjasone	if (idesc->id_type != DATA)
111234370Sjasone		errx(EEXIT, "wrong type to dirscan %d", idesc->id_type);
112234370Sjasone	if (idesc->id_entryno == 0 &&
113234370Sjasone	    (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
114234370Sjasone		idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
115234370Sjasone	blksiz = idesc->id_numfrags * sblock.fs_fsize;
116234370Sjasone	if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
117234370Sjasone		idesc->id_filesize -= blksiz;
118234370Sjasone		return (SKIP);
119234370Sjasone	}
120234370Sjasone	idesc->id_loc = 0;
121234370Sjasone	for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
122234370Sjasone		dsize = dp->d_reclen;
123234370Sjasone		memmove(dbuf, dp, (size_t)dsize);
124234370Sjasone#		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 */
162static struct direct *
163fsck_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	}
193dpok:
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 */
224static int
225dircheck(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
268void
269direrror(ino, errmesg)
270	ino_t ino;
271	char *errmesg;
272{
273
274	fileerror(ino, ino, errmesg);
275}
276
277void
278fileerror(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
301void
302adjust(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
332static int
333mkentry(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
377static int
378chgino(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
393int
394linkup(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 */
510int
511changeino(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 */
531int
532makeentry(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 */
567static int
568expanddir(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);
612bad:
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 */
624ino_t
625allocdir(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 */
680static void
681freedir(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 */
697static int
698lftempname(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 */
724static struct bufarea *
725getdirblk(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