xref: /freebsd/sbin/fsck_ffs/dir.c (revision 00e84f52f0985e6b2fd73694aa5f4b50a5f957af)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static const char sccsid[] = "@(#)dir.c	8.8 (Berkeley) 4/28/95";
35 #endif /* not lint */
36 #endif
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sys/sysctl.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 static struct	dirtemplate emptydir = {
55 	0, DIRBLKSIZ, DT_UNKNOWN, 0, "",
56 	0, 0, DT_UNKNOWN, 0, ""
57 };
58 static struct	dirtemplate dirhead = {
59 	0, 12, DT_DIR, 1, ".",
60 	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
61 };
62 
63 static int chgino(struct inodesc *);
64 static int dircheck(struct inodesc *, struct bufarea *, struct direct *);
65 static int expanddir(struct inode *ip, char *name);
66 static struct direct *fsck_readdir(struct inodesc *);
67 static struct bufarea *getdirblk(ufs2_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 	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 			    INO_IS_DUNFOUND(inp->i_number)) {
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(struct inodesc *idesc)
102 {
103 	struct direct *dp;
104 	struct bufarea *bp;
105 	u_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 		idesc->id_dirp = (struct direct *)dbuf;
126 		if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
127 			bp = getdirblk(idesc->id_blkno, blksiz);
128 			if (bp->b_errs != 0)
129 				return (STOP);
130 			memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
131 			    (size_t)dsize);
132 			dirty(bp);
133 			sbdirty();
134 		}
135 		if (n & STOP)
136 			return (n);
137 	}
138 	return (idesc->id_filesize > 0 ? KEEPON : STOP);
139 }
140 
141 /*
142  * Get and verify the next entry in a directory.
143  * We also verify that if there is another entry in the block that it is
144  * valid, so if it is not valid it can be subsumed into the current entry.
145  */
146 static struct direct *
147 fsck_readdir(struct inodesc *idesc)
148 {
149 	struct direct *dp, *ndp;
150 	struct bufarea *bp;
151 	long size, blksiz, subsume_ndp;
152 
153 	subsume_ndp = 0;
154 	blksiz = idesc->id_numfrags * sblock.fs_fsize;
155 	if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
156 		return (NULL);
157 	bp = getdirblk(idesc->id_blkno, blksiz);
158 	if (bp->b_errs != 0)
159 		return (NULL);
160 	dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
161 	/*
162 	 * Only need to check current entry if it is the first in the
163 	 * the block, as later entries will have been checked in the
164 	 * previous call to this function.
165 	 */
166 	if (idesc->id_loc % DIRBLKSIZ != 0 || dircheck(idesc, bp, dp) != 0) {
167 		/*
168 		 * Current entry is good, update to point at next.
169 		 */
170 		idesc->id_loc += dp->d_reclen;
171 		idesc->id_filesize -= dp->d_reclen;
172 		/*
173 		 * If at end of directory block, just return this entry.
174 		 */
175 		if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz ||
176 		    idesc->id_loc % DIRBLKSIZ == 0)
177 			return (dp);
178 		/*
179 		 * If the next entry good, return this entry.
180 		 */
181 		ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
182 		if (dircheck(idesc, bp, ndp) != 0)
183 			return (dp);
184 		/*
185 		 * The next entry is bad, so subsume it and the remainder
186 		 * of this directory block into this entry.
187 		 */
188 		subsume_ndp = 1;
189 	}
190 	/*
191 	 * Current or next entry is bad. Zap current entry or
192 	 * subsume next entry into current entry as appropriate.
193 	 */
194 	size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
195 	idesc->id_loc += size;
196 	idesc->id_filesize -= size;
197 	if (idesc->id_fix == IGNORE)
198 		return (NULL);
199 	if (subsume_ndp) {
200 		memset(ndp, 0, size);
201 		dp->d_reclen += size;
202 	} else {
203 		memset(dp, 0, size);
204 		dp->d_reclen = size;
205 	}
206 	if (dofix(idesc, "DIRECTORY CORRUPTED"))
207 		dirty(bp);
208 	return (dp);
209 }
210 
211 /*
212  * Verify that a directory entry is valid.
213  * This is a superset of the checks made in the kernel.
214  * Also optionally clears padding and unused directory space.
215  *
216  * Returns 0 if the entry is bad, 1 if the entry is good.
217  */
218 static int
219 dircheck(struct inodesc *idesc, struct bufarea *bp, struct direct *dp)
220 {
221 	size_t size;
222 	char *cp;
223 	u_int8_t namlen;
224 	int spaceleft, modified, unused;
225 
226 	spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
227 	size = DIRSIZ(0, dp);
228 	if (dp->d_reclen == 0 ||
229 	    dp->d_reclen > spaceleft ||
230 	    dp->d_reclen < size ||
231 	    idesc->id_filesize < size ||
232 	    (dp->d_reclen & (DIR_ROUNDUP - 1)) != 0)
233 		goto bad;
234 	modified = 0;
235 	if (dp->d_ino == 0) {
236 		if (!zflag || fswritefd < 0)
237 			return (1);
238 		/*
239 		 * Special case of an unused directory entry. Normally only
240 		 * occurs at the beginning of a directory block when the block
241 		 * contains no entries. Other than the first entry in a
242 		 * directory block, the kernel coalesces unused space with
243 		 * the previous entry by extending its d_reclen. However,
244 		 * when cleaning up a directory, fsck may set d_ino to zero
245 		 * in the middle of a directory block. If we're clearing out
246 		 * directory cruft (-z flag), then make sure that all directory
247 		 * space in entries with d_ino == 0 gets fully cleared.
248 		 */
249 		if (dp->d_type != 0) {
250 			dp->d_type = 0;
251 			modified = 1;
252 		}
253 		if (dp->d_namlen != 0) {
254 			dp->d_namlen = 0;
255 			modified = 1;
256 		}
257 		unused = dp->d_reclen - __offsetof(struct direct, d_name);
258 		for (cp = dp->d_name; unused > 0; unused--, cp++) {
259 			if (*cp != '\0') {
260 				*cp = '\0';
261 				modified = 1;
262 			}
263 		}
264 		if (modified)
265 			dirty(bp);
266 		return (1);
267 	}
268 	/*
269 	 * The d_type field should not be tested here. A bad type is an error
270 	 * in the entry itself but is not a corruption of the directory
271 	 * structure itself. So blowing away all the remaining entries in the
272 	 * directory block is inappropriate. Rather the type error should be
273 	 * checked in pass1 and fixed there.
274 	 *
275 	 * The name validation should also be done in pass1 although the
276 	 * check to see if the name is longer than fits in the space
277 	 * allocated for it (i.e., the *cp != '\0' fails after exiting the
278 	 * loop below) then it really is a structural error that requires
279 	 * the stronger action taken here.
280 	 */
281 	namlen = dp->d_namlen;
282 	if (namlen == 0 || dp->d_type > 15)
283 		goto bad;
284 	for (cp = dp->d_name, size = 0; size < namlen; size++) {
285 		if (*cp == '\0' || *cp++ == '/')
286 			goto bad;
287 	}
288 	if (*cp != '\0')
289 		goto bad;
290 	if (zflag && fswritefd >= 0) {
291 		/*
292 		 * Clear unused directory entry space, including the d_name
293 		 * padding.
294 		 */
295 		/* First figure the number of pad bytes. */
296 		unused = roundup2(namlen + 1, DIR_ROUNDUP) - (namlen + 1);
297 
298 		/* Add in the free space to the end of the record. */
299 		unused += dp->d_reclen - DIRSIZ(0, dp);
300 
301 		/*
302 		 * Now clear out the unused space, keeping track if we actually
303 		 * changed anything.
304 		 */
305 		for (cp = &dp->d_name[namlen + 1]; unused > 0; unused--, cp++) {
306 			if (*cp != '\0') {
307 				*cp = '\0';
308 				modified = 1;
309 			}
310 		}
311 
312 		if (modified)
313 			dirty(bp);
314 	}
315 	return (1);
316 
317 bad:
318 	if (debug)
319 		printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n",
320 		    dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type,
321 		    dp->d_name);
322 	return (0);
323 }
324 
325 void
326 direrror(ino_t ino, const char *errmesg)
327 {
328 
329 	fileerror(ino, ino, errmesg);
330 }
331 
332 void
333 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
334 {
335 	struct inode ip;
336 	union dinode *dp;
337 	char pathbuf[MAXPATHLEN + 1];
338 
339 	pwarn("%s ", errmesg);
340 	if (ino < UFS_ROOTINO || ino > maxino) {
341 		pfatal("out-of-range inode number %ju", (uintmax_t)ino);
342 		return;
343 	}
344 	ginode(ino, &ip);
345 	dp = ip.i_dp;
346 	prtinode(&ip);
347 	printf("\n");
348 	getpathname(pathbuf, cwd, ino);
349 	if (ftypeok(dp))
350 		pfatal("%s=%s\n",
351 		    (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
352 		    pathbuf);
353 	else
354 		pfatal("NAME=%s\n", pathbuf);
355 	irelse(&ip);
356 }
357 
358 void
359 adjust(struct inodesc *idesc, int lcnt)
360 {
361 	struct inode ip;
362 	union dinode *dp;
363 	int saveresolved;
364 
365 	ginode(idesc->id_number, &ip);
366 	dp = ip.i_dp;
367 	if (DIP(dp, di_nlink) == lcnt) {
368 		/*
369 		 * If we have not hit any unresolved problems, are running
370 		 * in preen mode, and are on a file system using soft updates,
371 		 * then just toss any partially allocated files.
372 		 */
373 		if (resolved && (preen || bkgrdflag) && usedsoftdep) {
374 			clri(idesc, "UNREF", 1);
375 			irelse(&ip);
376 			return;
377 		} else {
378 			/*
379 			 * The file system can be marked clean even if
380 			 * a file is not linked up, but is cleared.
381 			 * Hence, resolved should not be cleared when
382 			 * linkup is answered no, but clri is answered yes.
383 			 */
384 			saveresolved = resolved;
385 			if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
386 				resolved = saveresolved;
387 				clri(idesc, "UNREF", 0);
388 				irelse(&ip);
389 				return;
390 			}
391 			/*
392 			 * Account for the new reference created by linkup().
393 			 */
394 			lcnt--;
395 		}
396 	}
397 	if (lcnt != 0) {
398 		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
399 			((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
400 		prtinode(&ip);
401 		printf(" COUNT %d SHOULD BE %d",
402 			DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt);
403 		if (preen || usedsoftdep) {
404 			if (lcnt < 0) {
405 				printf("\n");
406 				pfatal("LINK COUNT INCREASING");
407 			}
408 			if (preen)
409 				printf(" (ADJUSTED)\n");
410 		}
411 		if (preen || reply("ADJUST") == 1) {
412 			if (bkgrdflag == 0) {
413 				DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt);
414 				inodirty(&ip);
415 			} else {
416 				cmd.value = idesc->id_number;
417 				cmd.size = -lcnt;
418 				if (debug)
419 					printf("adjrefcnt ino %ld amt %lld\n",
420 					    (long)cmd.value,
421 					    (long long)cmd.size);
422 				if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
423 				    &cmd, sizeof cmd) == -1)
424 					rwerror("ADJUST INODE", cmd.value);
425 			}
426 		}
427 	}
428 	irelse(&ip);
429 }
430 
431 static int
432 mkentry(struct inodesc *idesc)
433 {
434 	struct direct *dirp = idesc->id_dirp;
435 	struct direct newent;
436 	int newlen, oldlen;
437 
438 	newent.d_namlen = strlen(idesc->id_name);
439 	newlen = DIRSIZ(0, &newent);
440 	if (dirp->d_ino != 0)
441 		oldlen = DIRSIZ(0, dirp);
442 	else
443 		oldlen = 0;
444 	if (dirp->d_reclen - oldlen < newlen)
445 		return (KEEPON);
446 	newent.d_reclen = dirp->d_reclen - oldlen;
447 	dirp->d_reclen = oldlen;
448 	dirp = (struct direct *)(((char *)dirp) + oldlen);
449 	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
450 	dirp->d_reclen = newent.d_reclen;
451 	dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
452 	dirp->d_namlen = newent.d_namlen;
453 	memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
454 	return (ALTERED|STOP);
455 }
456 
457 static int
458 chgino(struct inodesc *idesc)
459 {
460 	struct direct *dirp = idesc->id_dirp;
461 
462 	if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
463 		return (KEEPON);
464 	dirp->d_ino = idesc->id_parent;
465 	dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
466 	return (ALTERED|STOP);
467 }
468 
469 int
470 linkup(ino_t orphan, ino_t parentdir, char *name)
471 {
472 	struct inode ip;
473 	union dinode *dp;
474 	int lostdir;
475 	ino_t oldlfdir;
476 	struct inoinfo *inp;
477 	struct inodesc idesc;
478 	char tempname[BUFSIZ];
479 
480 	memset(&idesc, 0, sizeof(struct inodesc));
481 	ginode(orphan, &ip);
482 	dp = ip.i_dp;
483 	lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR;
484 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
485 	prtinode(&ip);
486 	printf("\n");
487 	if (preen && DIP(dp, di_size) == 0) {
488 		irelse(&ip);
489 		return (0);
490 	}
491 	irelse(&ip);
492 	if (cursnapshot != 0) {
493 		pfatal("FILE LINKUP IN SNAPSHOT");
494 		return (0);
495 	}
496 	if (preen)
497 		printf(" (RECONNECTED)\n");
498 	else if (reply("RECONNECT") == 0)
499 		return (0);
500 	if (lfdir == 0) {
501 		ginode(UFS_ROOTINO, &ip);
502 		idesc.id_name = strdup(lfname);
503 		idesc.id_type = DATA;
504 		idesc.id_func = findino;
505 		idesc.id_number = UFS_ROOTINO;
506 		if ((ckinode(ip.i_dp, &idesc) & FOUND) != 0) {
507 			lfdir = idesc.id_parent;
508 		} else {
509 			pwarn("NO lost+found DIRECTORY");
510 			if (preen || reply("CREATE")) {
511 				lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode);
512 				if (lfdir != 0) {
513 					if (makeentry(UFS_ROOTINO, lfdir,
514 					    lfname) != 0) {
515 						numdirs++;
516 						if (preen)
517 							printf(" (CREATED)\n");
518 					} else {
519 						freedirino(lfdir, UFS_ROOTINO);
520 						lfdir = 0;
521 						if (preen)
522 							printf("\n");
523 					}
524 				}
525 			}
526 		}
527 		irelse(&ip);
528 		if (lfdir == 0) {
529 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
530 			printf("\n\n");
531 			return (0);
532 		}
533 	}
534 	ginode(lfdir, &ip);
535 	dp = ip.i_dp;
536 	if ((DIP(dp, di_mode) & IFMT) != IFDIR) {
537 		pfatal("lost+found IS NOT A DIRECTORY");
538 		if (reply("REALLOCATE") == 0) {
539 			irelse(&ip);
540 			return (0);
541 		}
542 		oldlfdir = lfdir;
543 		if ((lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode)) == 0) {
544 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
545 			irelse(&ip);
546 			return (0);
547 		}
548 		if ((changeino(UFS_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
549 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
550 			irelse(&ip);
551 			return (0);
552 		}
553 		idesc.id_type = inoinfo(oldlfdir)->ino_idtype;
554 		idesc.id_func = freeblock;
555 		idesc.id_number = oldlfdir;
556 		adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
557 		inoinfo(oldlfdir)->ino_linkcnt = 0;
558 		inodirty(&ip);
559 		irelse(&ip);
560 		ginode(lfdir, &ip);
561 		dp = ip.i_dp;
562 	}
563 	if (inoinfo(lfdir)->ino_state != DFOUND) {
564 		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
565 		irelse(&ip);
566 		return (0);
567 	}
568 	(void)lftempname(tempname, orphan);
569 	if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
570 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
571 		printf("\n\n");
572 		irelse(&ip);
573 		return (0);
574 	}
575 	inoinfo(orphan)->ino_linkcnt--;
576 	if (lostdir) {
577 		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
578 		    parentdir != (ino_t)-1)
579 			(void)makeentry(orphan, lfdir, "..");
580 		DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
581 		inodirty(&ip);
582 		inoinfo(lfdir)->ino_linkcnt++;
583 		pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan);
584 		inp = getinoinfo(parentdir);
585 		if (parentdir != (ino_t)-1 && inp != NULL) {
586 			printf("PARENT WAS I=%lu\n", (u_long)parentdir);
587 			/*
588 			 * If the parent directory did not have to
589 			 * be replaced then because of the ordering
590 			 * guarantees, has had the link count incremented
591 			 * for the child, but no entry was made.  This
592 			 * fixes the parent link count so that fsck does
593 			 * not need to be rerun.
594 			 */
595 			if ((inp->i_flags & INFO_NEW) != 0)
596 				inoinfo(parentdir)->ino_linkcnt++;
597 		}
598 		if (preen == 0)
599 			printf("\n");
600 	}
601 	irelse(&ip);
602 	return (1);
603 }
604 
605 /*
606  * fix an entry in a directory.
607  */
608 int
609 changeino(ino_t dir, const char *name, ino_t newnum)
610 {
611 	struct inodesc idesc;
612 	struct inode ip;
613 	int error;
614 
615 	memset(&idesc, 0, sizeof(struct inodesc));
616 	idesc.id_type = DATA;
617 	idesc.id_func = chgino;
618 	idesc.id_number = dir;
619 	idesc.id_fix = DONTKNOW;
620 	idesc.id_name = strdup(name);
621 	idesc.id_parent = newnum;	/* new value for name */
622 	ginode(dir, &ip);
623 	error = ckinode(ip.i_dp, &idesc);
624 	irelse(&ip);
625 	return (error);
626 }
627 
628 /*
629  * make an entry in a directory
630  */
631 int
632 makeentry(ino_t parent, ino_t ino, const char *name)
633 {
634 	struct inode ip;
635 	union dinode *dp;
636 	struct inodesc idesc;
637 	int retval;
638 	char pathbuf[MAXPATHLEN + 1];
639 
640 	if (parent < UFS_ROOTINO || parent >= maxino ||
641 	    ino < UFS_ROOTINO || ino >= maxino)
642 		return (0);
643 	memset(&idesc, 0, sizeof(struct inodesc));
644 	idesc.id_type = DATA;
645 	idesc.id_func = mkentry;
646 	idesc.id_number = parent;
647 	idesc.id_parent = ino;	/* this is the inode to enter */
648 	idesc.id_fix = DONTKNOW;
649 	idesc.id_name = strdup(name);
650 	ginode(parent, &ip);
651 	dp = ip.i_dp;
652 	if (DIP(dp, di_size) % DIRBLKSIZ) {
653 		DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ));
654 		inodirty(&ip);
655 	}
656 	if ((ckinode(dp, &idesc) & ALTERED) != 0) {
657 		irelse(&ip);
658 		return (1);
659 	}
660 	getpathname(pathbuf, parent, parent);
661 	if (expanddir(&ip, pathbuf) == 0) {
662 		irelse(&ip);
663 		return (0);
664 	}
665 	retval = ckinode(dp, &idesc) & ALTERED;
666 	irelse(&ip);
667 	return (retval);
668 }
669 
670 /*
671  * Attempt to expand the size of a directory
672  */
673 static int
674 expanddir(struct inode *ip, char *name)
675 {
676 	ufs2_daddr_t lastlbn, oldblk, newblk, indirblk;
677 	size_t filesize, lastlbnsize;
678 	struct bufarea *bp, *nbp;
679 	struct inodesc idesc;
680 	union dinode *dp;
681 	long cg, indiralloced;
682 	char *cp;
683 
684 	nbp = NULL;
685 	indiralloced = newblk = indirblk = 0;
686 	memset(&idesc, 0, sizeof(struct inodesc));
687 	idesc.id_type = ADDR;
688 	pwarn("NO SPACE LEFT IN %s", name);
689 	if (!preen && reply("EXPAND") == 0)
690 		return (0);
691 	cg = ino_to_cg(&sblock, ip->i_number);
692 	dp = ip->i_dp;
693 	filesize = DIP(dp, di_size);
694 	lastlbn = lblkno(&sblock, filesize);
695 	/*
696 	 * We only expand lost+found to a single indirect block.
697 	 */
698 	if ((DIP(dp, di_mode) & IFMT) != IFDIR || filesize == 0 ||
699 	    lastlbn >= UFS_NDADDR + NINDIR(&sblock))
700 		goto bad;
701 	/*
702 	 * If last block is a fragment, expand it to a full size block.
703 	 */
704 	lastlbnsize = sblksize(&sblock, filesize, lastlbn);
705 	if (lastlbnsize > 0 && lastlbnsize < sblock.fs_bsize) {
706 		oldblk = DIP(dp, di_db[lastlbn]);
707 		bp = getdirblk(oldblk, lastlbnsize);
708 		if (bp->b_errs)
709 			goto bad;
710 		newblk = allocblk(cg, sblock.fs_frag, std_checkblkavail);
711 		if (newblk == 0)
712 			goto bad;
713 		nbp = getdatablk(newblk, sblock.fs_bsize, BT_DIRDATA);
714 		if (nbp->b_errs)
715 			goto bad;
716 		DIP_SET(dp, di_db[lastlbn], newblk);
717 		DIP_SET(dp, di_size, filesize + sblock.fs_bsize - lastlbnsize);
718 		DIP_SET(dp, di_blocks, DIP(dp, di_blocks) +
719 		    btodb(sblock.fs_bsize - lastlbnsize));
720 		inodirty(ip);
721 		memmove(nbp->b_un.b_buf, bp->b_un.b_buf, lastlbnsize);
722 		memset(&nbp->b_un.b_buf[lastlbnsize], 0,
723 		    sblock.fs_bsize - lastlbnsize);
724 		for (cp = &nbp->b_un.b_buf[lastlbnsize];
725 		     cp < &nbp->b_un.b_buf[sblock.fs_bsize];
726 		     cp += DIRBLKSIZ)
727 			memmove(cp, &emptydir, sizeof emptydir);
728 		dirty(nbp);
729 		brelse(nbp);
730 		binval(bp);
731 		idesc.id_blkno = oldblk;
732 		idesc.id_numfrags = numfrags(&sblock, lastlbnsize);
733 		(void)freeblock(&idesc);
734 		if (preen)
735 			printf(" (EXPANDED)\n");
736 		return (1);
737 	}
738 	if ((newblk = allocblk(cg, sblock.fs_frag, std_checkblkavail)) == 0)
739 		goto bad;
740 	bp = getdirblk(newblk, sblock.fs_bsize);
741 	if (bp->b_errs)
742 		goto bad;
743 	memset(bp->b_un.b_buf, 0, sblock.fs_bsize);
744 	for (cp = bp->b_un.b_buf;
745 	     cp < &bp->b_un.b_buf[sblock.fs_bsize];
746 	     cp += DIRBLKSIZ)
747 		memmove(cp, &emptydir, sizeof emptydir);
748 	dirty(bp);
749 	if (lastlbn < UFS_NDADDR) {
750 		DIP_SET(dp, di_db[lastlbn], newblk);
751 	} else {
752 		/*
753 		 * Allocate indirect block if needed.
754 		 */
755 		if ((indirblk = DIP(dp, di_ib[0])) == 0) {
756 			indirblk = allocblk(cg, sblock.fs_frag,
757 			    std_checkblkavail);
758 			if (indirblk == 0) {
759 				binval(bp);
760 				goto bad;
761 			}
762 			indiralloced = 1;
763 		}
764 		nbp = getdatablk(indirblk, sblock.fs_bsize, BT_LEVEL1);
765 		if (nbp->b_errs)
766 			goto bad;
767 		if (indiralloced) {
768 			memset(nbp->b_un.b_buf, 0, sblock.fs_bsize);
769 			DIP_SET(dp, di_ib[0], indirblk);
770 			DIP_SET(dp, di_blocks,
771 			    DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
772 		}
773 		IBLK_SET(nbp, lastlbn - UFS_NDADDR, newblk);
774 		dirty(nbp);
775 		brelse(nbp);
776 	}
777 	DIP_SET(dp, di_size, filesize + sblock.fs_bsize);
778 	DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
779 	inodirty(ip);
780 	if (preen)
781 		printf(" (EXPANDED)\n");
782 	return (1);
783 bad:
784 	pfatal(" (EXPANSION FAILED)\n");
785 	if (nbp != NULL) {
786 		binval(bp);
787 		brelse(nbp);
788 	}
789 	if (newblk != 0) {
790 		idesc.id_blkno = newblk;
791 		idesc.id_numfrags = sblock.fs_frag;
792 		(void)freeblock(&idesc);
793 	}
794 	if (indiralloced) {
795 		idesc.id_blkno = indirblk;
796 		idesc.id_numfrags = sblock.fs_frag;
797 		(void)freeblock(&idesc);
798 	}
799 	return (0);
800 }
801 
802 /*
803  * allocate a new directory
804  */
805 ino_t
806 allocdir(ino_t parent, ino_t request, int mode)
807 {
808 	ino_t ino;
809 	char *cp;
810 	struct inode ip;
811 	union dinode *dp;
812 	struct bufarea *bp;
813 	struct inoinfo *inp;
814 	struct dirtemplate *dirp;
815 
816 	ino = allocino(request, IFDIR|mode);
817 	if (ino == 0)
818 		return (0);
819 	dirp = &dirhead;
820 	dirp->dot_ino = ino;
821 	dirp->dotdot_ino = parent;
822 	ginode(ino, &ip);
823 	dp = ip.i_dp;
824 	bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
825 	if (bp->b_errs) {
826 		freeino(ino);
827 		irelse(&ip);
828 		return (0);
829 	}
830 	memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
831 	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
832 	     cp < &bp->b_un.b_buf[sblock.fs_fsize];
833 	     cp += DIRBLKSIZ)
834 		memmove(cp, &emptydir, sizeof emptydir);
835 	dirty(bp);
836 	DIP_SET(dp, di_nlink, 2);
837 	inodirty(&ip);
838 	if (ino == UFS_ROOTINO) {
839 		inp = cacheino(dp, ino);
840 		inp->i_parent = parent;
841 		inp->i_dotdot = parent;
842 		inp->i_flags |= INFO_NEW;
843 		inoinfo(ino)->ino_type = DT_DIR;
844 		inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
845 		irelse(&ip);
846 		return(ino);
847 	}
848 	if (!INO_IS_DVALID(parent)) {
849 		freeino(ino);
850 		irelse(&ip);
851 		return (0);
852 	}
853 	inp = cacheino(dp, ino);
854 	inp->i_parent = parent;
855 	inp->i_dotdot = parent;
856 	inp->i_flags |= INFO_NEW;
857 	inoinfo(ino)->ino_type = DT_DIR;
858 	inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
859 	if (inoinfo(ino)->ino_state == DSTATE) {
860 		inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
861 		inoinfo(parent)->ino_linkcnt++;
862 	}
863 	irelse(&ip);
864 	ginode(parent, &ip);
865 	dp = ip.i_dp;
866 	DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
867 	inodirty(&ip);
868 	irelse(&ip);
869 	return (ino);
870 }
871 
872 /*
873  * free a directory inode
874  */
875 void
876 freedirino(ino_t ino, ino_t parent)
877 {
878 	struct inode ip;
879 	union dinode *dp;
880 
881 	if (ino != parent) {
882 		ginode(parent, &ip);
883 		dp = ip.i_dp;
884 		DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1);
885 		inodirty(&ip);
886 		irelse(&ip);
887 	}
888 	removecachedino(ino);
889 	freeino(ino);
890 }
891 
892 /*
893  * generate a temporary name for the lost+found directory.
894  */
895 static int
896 lftempname(char *bufp, ino_t ino)
897 {
898 	ino_t in;
899 	char *cp;
900 	int namlen;
901 
902 	cp = bufp + 2;
903 	for (in = maxino; in > 0; in /= 10)
904 		cp++;
905 	*--cp = 0;
906 	namlen = cp - bufp;
907 	in = ino;
908 	while (cp > bufp) {
909 		*--cp = (in % 10) + '0';
910 		in /= 10;
911 	}
912 	*cp = '#';
913 	return (namlen);
914 }
915 
916 /*
917  * Get a directory block.
918  * Insure that it is held until another is requested.
919  */
920 static struct bufarea *
921 getdirblk(ufs2_daddr_t blkno, long size)
922 {
923 
924 	if (pdirbp != NULL && pdirbp->b_errs == 0)
925 		brelse(pdirbp);
926 	pdirbp = getdatablk(blkno, size, BT_DIRDATA);
927 	return (pdirbp);
928 }
929