xref: /freebsd/sbin/fsck_ffs/dir.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 void freedir(ino_t ino, ino_t parent);
67 static struct direct *fsck_readdir(struct inodesc *);
68 static struct bufarea *getdirblk(ufs2_daddr_t blkno, long size);
69 static int lftempname(char *bufp, ino_t ino);
70 static int mkentry(struct inodesc *);
71 
72 /*
73  * Propagate connected state through the tree.
74  */
75 void
76 propagate(void)
77 {
78 	struct inoinfo **inpp, *inp;
79 	struct inoinfo **inpend;
80 	long change;
81 
82 	inpend = &inpsort[inplast];
83 	do {
84 		change = 0;
85 		for (inpp = inpsort; inpp < inpend; inpp++) {
86 			inp = *inpp;
87 			if (inp->i_parent == 0)
88 				continue;
89 			if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
90 			    INO_IS_DUNFOUND(inp->i_number)) {
91 				inoinfo(inp->i_number)->ino_state = DFOUND;
92 				change++;
93 			}
94 		}
95 	} while (change > 0);
96 }
97 
98 /*
99  * Scan each entry in a directory block.
100  */
101 int
102 dirscan(struct inodesc *idesc)
103 {
104 	struct direct *dp;
105 	struct bufarea *bp;
106 	u_int dsize, n;
107 	long blksiz;
108 	char dbuf[DIRBLKSIZ];
109 
110 	if (idesc->id_type != DATA)
111 		errx(EEXIT, "wrong type to dirscan %d", idesc->id_type);
112 	if (idesc->id_entryno == 0 &&
113 	    (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
114 		idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
115 	blksiz = idesc->id_numfrags * sblock.fs_fsize;
116 	if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
117 		idesc->id_filesize -= blksiz;
118 		return (SKIP);
119 	}
120 	idesc->id_loc = 0;
121 	for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
122 		dsize = dp->d_reclen;
123 		if (dsize > sizeof(dbuf))
124 			dsize = sizeof(dbuf);
125 		memmove(dbuf, dp, (size_t)dsize);
126 		idesc->id_dirp = (struct direct *)dbuf;
127 		if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
128 			bp = getdirblk(idesc->id_blkno, blksiz);
129 			if (bp->b_errs != 0)
130 				return (STOP);
131 			memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
132 			    (size_t)dsize);
133 			dirty(bp);
134 			sbdirty();
135 		}
136 		if (n & STOP)
137 			return (n);
138 	}
139 	return (idesc->id_filesize > 0 ? KEEPON : STOP);
140 }
141 
142 /*
143  * Get and verify the next entry in a directory.
144  * We also verify that if there is another entry in the block that it is
145  * valid, so if it is not valid it can be subsumed into the current entry.
146  */
147 static struct direct *
148 fsck_readdir(struct inodesc *idesc)
149 {
150 	struct direct *dp, *ndp;
151 	struct bufarea *bp;
152 	long size, blksiz, subsume_ndp;
153 
154 	subsume_ndp = 0;
155 	blksiz = idesc->id_numfrags * sblock.fs_fsize;
156 	if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
157 		return (NULL);
158 	bp = getdirblk(idesc->id_blkno, blksiz);
159 	if (bp->b_errs != 0)
160 		return (NULL);
161 	dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
162 	/*
163 	 * Only need to check current entry if it is the first in the
164 	 * the block, as later entries will have been checked in the
165 	 * previous call to this function.
166 	 */
167 	if (idesc->id_loc % DIRBLKSIZ != 0 || dircheck(idesc, bp, dp) != 0) {
168 		/*
169 		 * Current entry is good, update to point at next.
170 		 */
171 		idesc->id_loc += dp->d_reclen;
172 		idesc->id_filesize -= dp->d_reclen;
173 		/*
174 		 * If at end of directory block, just return this entry.
175 		 */
176 		if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz ||
177 		    idesc->id_loc % DIRBLKSIZ == 0)
178 			return (dp);
179 		/*
180 		 * If the next entry good, return this entry.
181 		 */
182 		ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
183 		if (dircheck(idesc, bp, ndp) != 0)
184 			return (dp);
185 		/*
186 		 * The next entry is bad, so subsume it and the remainder
187 		 * of this directory block into this entry.
188 		 */
189 		subsume_ndp = 1;
190 	}
191 	/*
192 	 * Current or next entry is bad. Zap current entry or
193 	 * subsume next entry into current entry as appropriate.
194 	 */
195 	size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
196 	idesc->id_loc += size;
197 	idesc->id_filesize -= size;
198 	if (idesc->id_fix == IGNORE)
199 		return (NULL);
200 	if (subsume_ndp) {
201 		memset(ndp, 0, size);
202 		dp->d_reclen += size;
203 	} else {
204 		memset(dp, 0, size);
205 		dp->d_reclen = size;
206 	}
207 	if (dofix(idesc, "DIRECTORY CORRUPTED"))
208 		dirty(bp);
209 	return (dp);
210 }
211 
212 /*
213  * Verify that a directory entry is valid.
214  * This is a superset of the checks made in the kernel.
215  * Also optionally clears padding and unused directory space.
216  *
217  * Returns 0 if the entry is bad, 1 if the entry is good.
218  */
219 static int
220 dircheck(struct inodesc *idesc, struct bufarea *bp, struct direct *dp)
221 {
222 	size_t size;
223 	char *cp;
224 	u_int8_t namlen;
225 	int spaceleft, modified, unused;
226 
227 	spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
228 	size = DIRSIZ(0, dp);
229 	if (dp->d_reclen == 0 ||
230 	    dp->d_reclen > spaceleft ||
231 	    dp->d_reclen < size ||
232 	    idesc->id_filesize < size ||
233 	    (dp->d_reclen & (DIR_ROUNDUP - 1)) != 0)
234 		goto bad;
235 	modified = 0;
236 	if (dp->d_ino == 0) {
237 		if (!zflag || fswritefd < 0)
238 			return (1);
239 		/*
240 		 * Special case of an unused directory entry. Normally only
241 		 * occurs at the beginning of a directory block when the block
242 		 * contains no entries. Other than the first entry in a
243 		 * directory block, the kernel coalesces unused space with
244 		 * the previous entry by extending its d_reclen. However,
245 		 * when cleaning up a directory, fsck may set d_ino to zero
246 		 * in the middle of a directory block. If we're clearing out
247 		 * directory cruft (-z flag), then make sure that all directory
248 		 * space in entries with d_ino == 0 gets fully cleared.
249 		 */
250 		if (dp->d_type != 0) {
251 			dp->d_type = 0;
252 			modified = 1;
253 		}
254 		if (dp->d_namlen != 0) {
255 			dp->d_namlen = 0;
256 			modified = 1;
257 		}
258 		unused = dp->d_reclen - __offsetof(struct direct, d_name);
259 		for (cp = dp->d_name; unused > 0; unused--, cp++) {
260 			if (*cp != '\0') {
261 				*cp = '\0';
262 				modified = 1;
263 			}
264 		}
265 		if (modified)
266 			dirty(bp);
267 		return (1);
268 	}
269 	/*
270 	 * The d_type field should not be tested here. A bad type is an error
271 	 * in the entry itself but is not a corruption of the directory
272 	 * structure itself. So blowing away all the remaining entries in the
273 	 * directory block is inappropriate. Rather the type error should be
274 	 * checked in pass1 and fixed there.
275 	 *
276 	 * The name validation should also be done in pass1 although the
277 	 * check to see if the name is longer than fits in the space
278 	 * allocated for it (i.e., the *cp != '\0' fails after exiting the
279 	 * loop below) then it really is a structural error that requires
280 	 * the stronger action taken here.
281 	 */
282 	namlen = dp->d_namlen;
283 	if (namlen == 0 || dp->d_type > 15)
284 		goto bad;
285 	for (cp = dp->d_name, size = 0; size < namlen; size++) {
286 		if (*cp == '\0' || *cp++ == '/')
287 			goto bad;
288 	}
289 	if (*cp != '\0')
290 		goto bad;
291 	if (zflag && fswritefd >= 0) {
292 		/*
293 		 * Clear unused directory entry space, including the d_name
294 		 * padding.
295 		 */
296 		/* First figure the number of pad bytes. */
297 		unused = roundup2(namlen + 1, DIR_ROUNDUP) - (namlen + 1);
298 
299 		/* Add in the free space to the end of the record. */
300 		unused += dp->d_reclen - DIRSIZ(0, dp);
301 
302 		/*
303 		 * Now clear out the unused space, keeping track if we actually
304 		 * changed anything.
305 		 */
306 		for (cp = &dp->d_name[namlen + 1]; unused > 0; unused--, cp++) {
307 			if (*cp != '\0') {
308 				*cp = '\0';
309 				modified = 1;
310 			}
311 		}
312 
313 		if (modified)
314 			dirty(bp);
315 	}
316 	return (1);
317 
318 bad:
319 	if (debug)
320 		printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n",
321 		    dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type,
322 		    dp->d_name);
323 	return (0);
324 }
325 
326 void
327 direrror(ino_t ino, const char *errmesg)
328 {
329 
330 	fileerror(ino, ino, errmesg);
331 }
332 
333 void
334 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
335 {
336 	struct inode ip;
337 	union dinode *dp;
338 	char pathbuf[MAXPATHLEN + 1];
339 
340 	pwarn("%s ", errmesg);
341 	if (ino < UFS_ROOTINO || ino > maxino) {
342 		pfatal("out-of-range inode number %ju", (uintmax_t)ino);
343 		return;
344 	}
345 	ginode(ino, &ip);
346 	dp = ip.i_dp;
347 	prtinode(&ip);
348 	printf("\n");
349 	getpathname(pathbuf, cwd, ino);
350 	if (ftypeok(dp))
351 		pfatal("%s=%s\n",
352 		    (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
353 		    pathbuf);
354 	else
355 		pfatal("NAME=%s\n", pathbuf);
356 	irelse(&ip);
357 }
358 
359 void
360 adjust(struct inodesc *idesc, int lcnt)
361 {
362 	struct inode ip;
363 	union dinode *dp;
364 	int saveresolved;
365 
366 	ginode(idesc->id_number, &ip);
367 	dp = ip.i_dp;
368 	if (DIP(dp, di_nlink) == lcnt) {
369 		/*
370 		 * If we have not hit any unresolved problems, are running
371 		 * in preen mode, and are on a file system using soft updates,
372 		 * then just toss any partially allocated files.
373 		 */
374 		if (resolved && (preen || bkgrdflag) && usedsoftdep) {
375 			clri(idesc, "UNREF", 1);
376 			irelse(&ip);
377 			return;
378 		} else {
379 			/*
380 			 * The file system can be marked clean even if
381 			 * a file is not linked up, but is cleared.
382 			 * Hence, resolved should not be cleared when
383 			 * linkup is answered no, but clri is answered yes.
384 			 */
385 			saveresolved = resolved;
386 			if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
387 				resolved = saveresolved;
388 				clri(idesc, "UNREF", 0);
389 				irelse(&ip);
390 				return;
391 			}
392 			/*
393 			 * Account for the new reference created by linkup().
394 			 */
395 			lcnt--;
396 		}
397 	}
398 	if (lcnt != 0) {
399 		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
400 			((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
401 		prtinode(&ip);
402 		printf(" COUNT %d SHOULD BE %d",
403 			DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt);
404 		if (preen || usedsoftdep) {
405 			if (lcnt < 0) {
406 				printf("\n");
407 				pfatal("LINK COUNT INCREASING");
408 			}
409 			if (preen)
410 				printf(" (ADJUSTED)\n");
411 		}
412 		if (preen || reply("ADJUST") == 1) {
413 			if (bkgrdflag == 0) {
414 				DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt);
415 				inodirty(&ip);
416 			} else {
417 				cmd.value = idesc->id_number;
418 				cmd.size = -lcnt;
419 				if (debug)
420 					printf("adjrefcnt ino %ld amt %lld\n",
421 					    (long)cmd.value,
422 					    (long long)cmd.size);
423 				if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
424 				    &cmd, sizeof cmd) == -1)
425 					rwerror("ADJUST INODE", cmd.value);
426 			}
427 		}
428 	}
429 	irelse(&ip);
430 }
431 
432 static int
433 mkentry(struct inodesc *idesc)
434 {
435 	struct direct *dirp = idesc->id_dirp;
436 	struct direct newent;
437 	int newlen, oldlen;
438 
439 	newent.d_namlen = strlen(idesc->id_name);
440 	newlen = DIRSIZ(0, &newent);
441 	if (dirp->d_ino != 0)
442 		oldlen = DIRSIZ(0, dirp);
443 	else
444 		oldlen = 0;
445 	if (dirp->d_reclen - oldlen < newlen)
446 		return (KEEPON);
447 	newent.d_reclen = dirp->d_reclen - oldlen;
448 	dirp->d_reclen = oldlen;
449 	dirp = (struct direct *)(((char *)dirp) + oldlen);
450 	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
451 	dirp->d_reclen = newent.d_reclen;
452 	dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
453 	dirp->d_namlen = newent.d_namlen;
454 	memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
455 	return (ALTERED|STOP);
456 }
457 
458 static int
459 chgino(struct inodesc *idesc)
460 {
461 	struct direct *dirp = idesc->id_dirp;
462 
463 	if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
464 		return (KEEPON);
465 	dirp->d_ino = idesc->id_parent;
466 	dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
467 	return (ALTERED|STOP);
468 }
469 
470 int
471 linkup(ino_t orphan, ino_t parentdir, char *name)
472 {
473 	struct inode ip;
474 	union dinode *dp;
475 	int lostdir;
476 	ino_t oldlfdir;
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 						freedir(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 		if (parentdir != (ino_t)-1) {
585 			printf("PARENT WAS I=%lu\n", (u_long)parentdir);
586 			/*
587 			 * The parent directory, because of the ordering
588 			 * guarantees, has had the link count incremented
589 			 * for the child, but no entry was made.  This
590 			 * fixes the parent link count so that fsck does
591 			 * not need to be rerun.
592 			 */
593 			inoinfo(parentdir)->ino_linkcnt++;
594 		}
595 		if (preen == 0)
596 			printf("\n");
597 	}
598 	irelse(&ip);
599 	return (1);
600 }
601 
602 /*
603  * fix an entry in a directory.
604  */
605 int
606 changeino(ino_t dir, const char *name, ino_t newnum)
607 {
608 	struct inodesc idesc;
609 	struct inode ip;
610 	int error;
611 
612 	memset(&idesc, 0, sizeof(struct inodesc));
613 	idesc.id_type = DATA;
614 	idesc.id_func = chgino;
615 	idesc.id_number = dir;
616 	idesc.id_fix = DONTKNOW;
617 	idesc.id_name = strdup(name);
618 	idesc.id_parent = newnum;	/* new value for name */
619 	ginode(dir, &ip);
620 	error = ckinode(ip.i_dp, &idesc);
621 	irelse(&ip);
622 	return (error);
623 }
624 
625 /*
626  * make an entry in a directory
627  */
628 int
629 makeentry(ino_t parent, ino_t ino, const char *name)
630 {
631 	struct inode ip;
632 	union dinode *dp;
633 	struct inodesc idesc;
634 	int retval;
635 	char pathbuf[MAXPATHLEN + 1];
636 
637 	if (parent < UFS_ROOTINO || parent >= maxino ||
638 	    ino < UFS_ROOTINO || ino >= maxino)
639 		return (0);
640 	memset(&idesc, 0, sizeof(struct inodesc));
641 	idesc.id_type = DATA;
642 	idesc.id_func = mkentry;
643 	idesc.id_number = parent;
644 	idesc.id_parent = ino;	/* this is the inode to enter */
645 	idesc.id_fix = DONTKNOW;
646 	idesc.id_name = strdup(name);
647 	ginode(parent, &ip);
648 	dp = ip.i_dp;
649 	if (DIP(dp, di_size) % DIRBLKSIZ) {
650 		DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ));
651 		inodirty(&ip);
652 	}
653 	if ((ckinode(dp, &idesc) & ALTERED) != 0) {
654 		irelse(&ip);
655 		return (1);
656 	}
657 	getpathname(pathbuf, parent, parent);
658 	if (expanddir(&ip, pathbuf) == 0) {
659 		irelse(&ip);
660 		return (0);
661 	}
662 	retval = ckinode(dp, &idesc) & ALTERED;
663 	irelse(&ip);
664 	return (retval);
665 }
666 
667 /*
668  * Attempt to expand the size of a directory
669  */
670 static int
671 expanddir(struct inode *ip, char *name)
672 {
673 	ufs2_daddr_t lastlbn, oldblk, newblk, indirblk;
674 	size_t filesize, lastlbnsize;
675 	struct bufarea *bp, *nbp;
676 	struct inodesc idesc;
677 	union dinode *dp;
678 	int indiralloced;
679 	char *cp;
680 
681 	nbp = NULL;
682 	indiralloced = newblk = indirblk = 0;
683 	pwarn("NO SPACE LEFT IN %s", name);
684 	if (!preen && reply("EXPAND") == 0)
685 		return (0);
686 	dp = ip->i_dp;
687 	filesize = DIP(dp, di_size);
688 	lastlbn = lblkno(&sblock, filesize);
689 	/*
690 	 * We only expand lost+found to a single indirect block.
691 	 */
692 	if ((DIP(dp, di_mode) & IFMT) != IFDIR || filesize == 0 ||
693 	    lastlbn >= UFS_NDADDR + NINDIR(&sblock))
694 		goto bad;
695 	/*
696 	 * If last block is a fragment, expand it to a full size block.
697 	 */
698 	lastlbnsize = sblksize(&sblock, filesize, lastlbn);
699 	if (lastlbnsize > 0 && lastlbnsize < sblock.fs_bsize) {
700 		oldblk = DIP(dp, di_db[lastlbn]);
701 		bp = getdirblk(oldblk, lastlbnsize);
702 		if (bp->b_errs)
703 			goto bad;
704 		if ((newblk = allocblk(sblock.fs_frag)) == 0)
705 			goto bad;
706 		nbp = getdatablk(newblk, sblock.fs_bsize, BT_DIRDATA);
707 		if (nbp->b_errs)
708 			goto bad;
709 		DIP_SET(dp, di_db[lastlbn], newblk);
710 		DIP_SET(dp, di_size, filesize + sblock.fs_bsize - lastlbnsize);
711 		DIP_SET(dp, di_blocks, DIP(dp, di_blocks) +
712 		    btodb(sblock.fs_bsize - lastlbnsize));
713 		inodirty(ip);
714 		memmove(nbp->b_un.b_buf, bp->b_un.b_buf, lastlbnsize);
715 		memset(&nbp->b_un.b_buf[lastlbnsize], 0,
716 		    sblock.fs_bsize - lastlbnsize);
717 		for (cp = &nbp->b_un.b_buf[lastlbnsize];
718 		     cp < &nbp->b_un.b_buf[sblock.fs_bsize];
719 		     cp += DIRBLKSIZ)
720 			memmove(cp, &emptydir, sizeof emptydir);
721 		dirty(nbp);
722 		brelse(nbp);
723 		idesc.id_blkno = oldblk;
724 		idesc.id_numfrags = numfrags(&sblock, lastlbnsize);
725 		(void)freeblock(&idesc);
726 		if (preen)
727 			printf(" (EXPANDED)\n");
728 		return (1);
729 	}
730 	if ((newblk = allocblk(sblock.fs_frag)) == 0)
731 		goto bad;
732 	bp = getdirblk(newblk, sblock.fs_bsize);
733 	if (bp->b_errs)
734 		goto bad;
735 	memset(bp->b_un.b_buf, 0, sblock.fs_bsize);
736 	for (cp = bp->b_un.b_buf;
737 	     cp < &bp->b_un.b_buf[sblock.fs_bsize];
738 	     cp += DIRBLKSIZ)
739 		memmove(cp, &emptydir, sizeof emptydir);
740 	dirty(bp);
741 	if (lastlbn < UFS_NDADDR) {
742 		DIP_SET(dp, di_db[lastlbn], newblk);
743 	} else {
744 		/*
745 		 * Allocate indirect block if needed.
746 		 */
747 		if ((indirblk = DIP(dp, di_ib[0])) == 0) {
748 			if ((indirblk = allocblk(sblock.fs_frag)) == 0)
749 				goto bad;
750 			indiralloced = 1;
751 		}
752 		nbp = getdatablk(indirblk, sblock.fs_bsize, BT_LEVEL1);
753 		if (nbp->b_errs)
754 			goto bad;
755 		if (indiralloced) {
756 			memset(nbp->b_un.b_buf, 0, sblock.fs_bsize);
757 			DIP_SET(dp, di_ib[0], indirblk);
758 			DIP_SET(dp, di_blocks,
759 			    DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
760 		}
761 		IBLK_SET(nbp, lastlbn - UFS_NDADDR, newblk);
762 		dirty(nbp);
763 		brelse(nbp);
764 	}
765 	DIP_SET(dp, di_size, filesize + sblock.fs_bsize);
766 	DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
767 	inodirty(ip);
768 	if (preen)
769 		printf(" (EXPANDED)\n");
770 	return (1);
771 bad:
772 	pfatal(" (EXPANSION FAILED)\n");
773 	if (nbp != NULL)
774 		brelse(nbp);
775 	if (newblk != 0) {
776 		idesc.id_blkno = newblk;
777 		idesc.id_numfrags = sblock.fs_frag;
778 		(void)freeblock(&idesc);
779 	}
780 	if (indiralloced) {
781 		idesc.id_blkno = indirblk;
782 		idesc.id_numfrags = sblock.fs_frag;
783 		(void)freeblock(&idesc);
784 	}
785 	return (0);
786 }
787 
788 /*
789  * allocate a new directory
790  */
791 ino_t
792 allocdir(ino_t parent, ino_t request, int mode)
793 {
794 	ino_t ino;
795 	char *cp;
796 	struct inode ip;
797 	union dinode *dp;
798 	struct bufarea *bp;
799 	struct inoinfo *inp;
800 	struct dirtemplate *dirp;
801 
802 	ino = allocino(request, IFDIR|mode);
803 	if (ino == 0)
804 		return (0);
805 	dirp = &dirhead;
806 	dirp->dot_ino = ino;
807 	dirp->dotdot_ino = parent;
808 	ginode(ino, &ip);
809 	dp = ip.i_dp;
810 	bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
811 	if (bp->b_errs) {
812 		freeino(ino);
813 		irelse(&ip);
814 		return (0);
815 	}
816 	memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
817 	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
818 	     cp < &bp->b_un.b_buf[sblock.fs_fsize];
819 	     cp += DIRBLKSIZ)
820 		memmove(cp, &emptydir, sizeof emptydir);
821 	dirty(bp);
822 	DIP_SET(dp, di_nlink, 2);
823 	inodirty(&ip);
824 	if (ino == UFS_ROOTINO) {
825 		inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
826 		cacheino(dp, ino);
827 		irelse(&ip);
828 		return(ino);
829 	}
830 	if (!INO_IS_DVALID(parent)) {
831 		freeino(ino);
832 		irelse(&ip);
833 		return (0);
834 	}
835 	cacheino(dp, ino);
836 	inp = getinoinfo(ino);
837 	inp->i_parent = parent;
838 	inp->i_dotdot = parent;
839 	inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
840 	if (inoinfo(ino)->ino_state == DSTATE) {
841 		inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
842 		inoinfo(parent)->ino_linkcnt++;
843 	}
844 	irelse(&ip);
845 	ginode(parent, &ip);
846 	dp = ip.i_dp;
847 	DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
848 	inodirty(&ip);
849 	irelse(&ip);
850 	return (ino);
851 }
852 
853 /*
854  * free a directory inode
855  */
856 static void
857 freedir(ino_t ino, ino_t parent)
858 {
859 	struct inode ip;
860 	union dinode *dp;
861 
862 	if (ino != parent) {
863 		ginode(parent, &ip);
864 		dp = ip.i_dp;
865 		DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1);
866 		inodirty(&ip);
867 		irelse(&ip);
868 	}
869 	freeino(ino);
870 }
871 
872 /*
873  * generate a temporary name for the lost+found directory.
874  */
875 static int
876 lftempname(char *bufp, ino_t ino)
877 {
878 	ino_t in;
879 	char *cp;
880 	int namlen;
881 
882 	cp = bufp + 2;
883 	for (in = maxino; in > 0; in /= 10)
884 		cp++;
885 	*--cp = 0;
886 	namlen = cp - bufp;
887 	in = ino;
888 	while (cp > bufp) {
889 		*--cp = (in % 10) + '0';
890 		in /= 10;
891 	}
892 	*cp = '#';
893 	return (namlen);
894 }
895 
896 /*
897  * Get a directory block.
898  * Insure that it is held until another is requested.
899  */
900 static struct bufarea *
901 getdirblk(ufs2_daddr_t blkno, long size)
902 {
903 
904 	if (pdirbp != NULL && pdirbp->b_errs == 0)
905 		brelse(pdirbp);
906 	pdirbp = getdatablk(blkno, size, BT_DIRDATA);
907 	return (pdirbp);
908 }
909