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