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