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