xref: /openbsd-src/sbin/fsck_ffs/dir.c (revision 81fb472f136e03be4db5acdf90d9ac9fd7ec035c)
1 /*	$OpenBSD: dir.c,v 1.35 2024/02/03 18:51:57 beck Exp $	*/
2 /*	$NetBSD: dir.c,v 1.20 1996/09/27 22:45:11 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>	/* DEV_BSIZE roundup btodb */
34 #include <sys/time.h>
35 #include <ufs/ufs/dinode.h>
36 #include <ufs/ufs/dir.h>
37 #include <ufs/ffs/fs.h>
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <limits.h>
43 
44 #include "fsck.h"
45 #include "fsutil.h"
46 #include "extern.h"
47 
48 char	*lfname = "lost+found";
49 int	lfmode = 01700;
50 struct	dirtemplate emptydir = { 0, DIRBLKSIZ };
51 struct	dirtemplate dirhead = {
52 	0, 12, DT_DIR, 1, ".",
53 	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
54 };
55 
56 static int expanddir(union dinode *, char *);
57 static void freedir(ino_t, ino_t);
58 static struct direct *fsck_readdir(struct inodesc *);
59 static struct bufarea *getdirblk(daddr_t, long);
60 static int lftempname(char *, ino_t);
61 static int mkentry(struct inodesc *);
62 static int chgino(struct  inodesc *);
63 
64 /*
65  * Propagate connected state through the tree.
66  */
67 void
propagate(ino_t inumber)68 propagate(ino_t inumber)
69 {
70 	struct	inoinfo *inp;
71 	char	state;
72 
73 	inp = getinoinfo(inumber);
74 	state = GET_ISTATE(inp->i_number);
75 	for (;;) {
76 		SET_ISTATE(inp->i_number, state);
77 		if (inp->i_child &&
78 		    GET_ISTATE(inp->i_child->i_number) != state)
79 			inp = inp->i_child;
80 		else if (inp->i_number == inumber)
81 			break;
82 		else if (inp->i_sibling)
83 			inp = inp->i_sibling;
84 		else
85 			inp = getinoinfo(inp->i_parent);
86 	}
87 }
88 
89 /*
90  * Scan each entry in a directory block.
91  */
92 int
dirscan(struct inodesc * idesc)93 dirscan(struct inodesc *idesc)
94 {
95 	struct direct *dp;
96 	struct bufarea *bp;
97 	int dsize, n;
98 	long blksiz;
99 	char dbuf[DIRBLKSIZ];
100 
101 	if (idesc->id_type != DATA)
102 		errexit("wrong type to dirscan %d\n", idesc->id_type);
103 	if (idesc->id_entryno == 0 &&
104 	    (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
105 		idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
106 	blksiz = idesc->id_numfrags * sblock.fs_fsize;
107 	if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
108 		idesc->id_filesize -= blksiz;
109 		return (SKIP);
110 	}
111 	idesc->id_loc = 0;
112 	for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
113 		dsize = dp->d_reclen;
114 		memcpy(dbuf, dp, (size_t)dsize);
115 		idesc->id_dirp = (struct direct *)dbuf;
116 		if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
117 			bp = getdirblk(idesc->id_blkno, blksiz);
118 			memcpy(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
119 			    (size_t)dsize);
120 			dirty(bp);
121 			sbdirty();
122 		}
123 		if (n & STOP)
124 			return (n);
125 	}
126 	return (idesc->id_filesize > 0 ? KEEPON : STOP);
127 }
128 
129 /*
130  * get next entry in a directory.
131  */
132 static struct direct *
fsck_readdir(struct inodesc * idesc)133 fsck_readdir(struct inodesc *idesc)
134 {
135 	struct direct *dp, *ndp;
136 	struct bufarea *bp;
137 	long size, blksiz, fix, dploc;
138 
139 	blksiz = idesc->id_numfrags * sblock.fs_fsize;
140 	bp = getdirblk(idesc->id_blkno, blksiz);
141 	if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
142 	    idesc->id_loc < blksiz) {
143 		dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
144 		if (dircheck(idesc, dp))
145 			goto dpok;
146 		if (idesc->id_fix == IGNORE)
147 			return (0);
148 		fix = dofix(idesc, "DIRECTORY CORRUPTED");
149 		bp = getdirblk(idesc->id_blkno, blksiz);
150 		dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
151 		dp->d_reclen = DIRBLKSIZ;
152 		dp->d_ino = 0;
153 		dp->d_type = 0;
154 		dp->d_namlen = 0;
155 		dp->d_name[0] = '\0';
156 		if (fix)
157 			dirty(bp);
158 		idesc->id_loc += DIRBLKSIZ;
159 		idesc->id_filesize -= DIRBLKSIZ;
160 		return (dp);
161 	}
162 dpok:
163 	if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
164 		return NULL;
165 	dploc = idesc->id_loc;
166 	dp = (struct direct *)(bp->b_un.b_buf + dploc);
167 	idesc->id_loc += dp->d_reclen;
168 	idesc->id_filesize -= dp->d_reclen;
169 	if ((idesc->id_loc % DIRBLKSIZ) == 0)
170 		return (dp);
171 	ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
172 	if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
173 	    dircheck(idesc, ndp) == 0) {
174 		size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
175 		idesc->id_loc += size;
176 		idesc->id_filesize -= size;
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 + dploc);
182 		dp->d_reclen += size;
183 		if (fix)
184 			dirty(bp);
185 	}
186 	return (dp);
187 }
188 
189 /*
190  * Verify that a directory entry is valid.
191  * This is a superset of the checks made in the kernel.
192  */
193 int
dircheck(struct inodesc * idesc,struct direct * dp)194 dircheck(struct inodesc *idesc, struct direct *dp)
195 {
196 	int size;
197 	char *cp;
198 	u_char namlen, type;
199 	int spaceleft;
200 
201 	spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
202 	if (dp->d_ino >= maxino ||
203 	    dp->d_reclen == 0 ||
204 	    dp->d_reclen > spaceleft ||
205 	    (dp->d_reclen & 0x3) != 0)
206 		return (0);
207 	if (dp->d_ino == 0)
208 		return (1);
209 	size = DIRSIZ(dp);
210 	namlen = dp->d_namlen;
211 	type = dp->d_type;
212 	if (dp->d_reclen < size ||
213 	    idesc->id_filesize < size ||
214 	    type > 15)
215 		return (0);
216 	for (cp = dp->d_name, size = 0; size < namlen; size++)
217 		if (*cp == '\0' || (*cp++ == '/'))
218 			return (0);
219 	if (*cp != '\0')
220 		return (0);
221 	return (1);
222 }
223 
224 void
direrror(ino_t ino,char * errmesg)225 direrror(ino_t ino, char *errmesg)
226 {
227 	fileerror(ino, ino, errmesg);
228 }
229 
230 void
fileerror(ino_t cwd,ino_t ino,char * errmesg)231 fileerror(ino_t cwd, ino_t ino, char *errmesg)
232 {
233 	union dinode *dp;
234 	char pathbuf[PATH_MAX + 1];
235 
236 	pwarn("%s ", errmesg);
237 	pinode(ino);
238 	printf("\n");
239 	getpathname(pathbuf, sizeof pathbuf, cwd, ino);
240 	if (ino < ROOTINO || ino > maxino) {
241 		pfatal("NAME=%s\n", pathbuf);
242 		return;
243 	}
244 	dp = ginode(ino);
245 	if (ftypeok(dp))
246 		pfatal("%s=%s\n",
247 		    (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
248 		    pathbuf);
249 	else
250 		pfatal("NAME=%s\n", pathbuf);
251 }
252 
253 void
adjust(struct inodesc * idesc,short lcnt)254 adjust(struct inodesc *idesc, short lcnt)
255 {
256 	union dinode *dp;
257 
258 	dp = ginode(idesc->id_number);
259 	if (DIP(dp, di_nlink) == lcnt) {
260 		if (linkup(idesc->id_number, 0) == 0)
261 			clri(idesc, "UNREF", 0);
262 	} else {
263 		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
264 			((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
265 		pinode(idesc->id_number);
266 		printf(" COUNT %d SHOULD BE %d", DIP(dp, di_nlink),
267 		    DIP(dp, di_nlink) - lcnt);
268 		if (preen) {
269 			if (lcnt < 0) {
270 				printf("\n");
271 				pfatal("LINK COUNT INCREASING");
272 			}
273 			if (preen)
274 				printf(" (ADJUSTED)\n");
275 		}
276 		if (preen || reply("ADJUST") == 1) {
277 			DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt);
278 			inodirty();
279 		}
280 	}
281 }
282 
283 static int
mkentry(struct inodesc * idesc)284 mkentry(struct inodesc *idesc)
285 {
286 	struct direct *dirp = idesc->id_dirp;
287 	struct direct newent;
288 	int newlen, oldlen;
289 
290 	newent.d_namlen = strlen(idesc->id_name);
291 	newlen = DIRSIZ(&newent);
292 	if (dirp->d_ino != 0)
293 		oldlen = DIRSIZ(dirp);
294 	else
295 		oldlen = 0;
296 	if (dirp->d_reclen - oldlen < newlen)
297 		return (KEEPON);
298 	newent.d_reclen = dirp->d_reclen - oldlen;
299 	dirp->d_reclen = oldlen;
300 	dirp = (struct direct *)(((char *)dirp) + oldlen);
301 	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
302 	dirp->d_reclen = newent.d_reclen;
303 	dirp->d_type = GET_ITYPE(idesc->id_parent);
304 	dirp->d_namlen = newent.d_namlen;
305 	memcpy(dirp->d_name, idesc->id_name, (size_t)dirp->d_namlen + 1);
306 	return (ALTERED|STOP);
307 }
308 
309 static int
chgino(struct inodesc * idesc)310 chgino(struct inodesc *idesc)
311 {
312 	struct direct *dirp = idesc->id_dirp;
313 
314 	if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
315 		return (KEEPON);
316 	dirp->d_ino = idesc->id_parent;
317 	dirp->d_type = GET_ITYPE(idesc->id_parent);
318 	return (ALTERED|STOP);
319 }
320 
321 int
linkup(ino_t orphan,ino_t parentdir)322 linkup(ino_t orphan, ino_t parentdir)
323 {
324 	union dinode *dp;
325 	int lostdir;
326 	ino_t oldlfdir;
327 	struct inodesc idesc;
328 	char tempname[BUFSIZ];
329 
330 	memset(&idesc, 0, sizeof(struct inodesc));
331 	dp = ginode(orphan);
332 	lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR;
333 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
334 	pinode(orphan);
335 	if (preen && DIP(dp, di_size) == 0)
336 		return (0);
337 	if (preen)
338 		printf(" (RECONNECTED)\n");
339 	else
340 		if (reply("RECONNECT") == 0)
341 			return (0);
342 	if (lfdir == 0) {
343 		dp = ginode(ROOTINO);
344 		idesc.id_name = lfname;
345 		idesc.id_type = DATA;
346 		idesc.id_func = findino;
347 		idesc.id_number = ROOTINO;
348 		if ((ckinode(dp, &idesc) & FOUND) != 0) {
349 			lfdir = idesc.id_parent;
350 		} else {
351 			pwarn("NO lost+found DIRECTORY");
352 			if (preen || reply("CREATE")) {
353 				lfdir = allocdir(ROOTINO, 0, lfmode);
354 				if (lfdir != 0) {
355 					if (makeentry(ROOTINO, lfdir, lfname) != 0) {
356 						if (preen)
357 							printf(" (CREATED)\n");
358 					} else {
359 						freedir(lfdir, ROOTINO);
360 						lfdir = 0;
361 						if (preen)
362 							printf("\n");
363 					}
364 				}
365 			}
366 		}
367 		if (lfdir == 0) {
368 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
369 			printf("\n\n");
370 			return (0);
371 		}
372 	}
373 	dp = ginode(lfdir);
374 	if ((DIP(dp, di_mode) & IFMT) != IFDIR) {
375 		pfatal("lost+found IS NOT A DIRECTORY");
376 		if (reply("REALLOCATE") == 0)
377 			return (0);
378 		oldlfdir = lfdir;
379 		if ((lfdir = allocdir(ROOTINO, 0, lfmode)) == 0) {
380 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
381 			return (0);
382 		}
383 		if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
384 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
385 			return (0);
386 		}
387 		inodirty();
388 		idesc.id_type = ADDR;
389 		idesc.id_func = pass4check;
390 		idesc.id_number = oldlfdir;
391 		adjust(&idesc, ILNCOUNT(oldlfdir) + 1);
392 		ILNCOUNT(oldlfdir) = 0;
393 		dp = ginode(lfdir);
394 	}
395 	if (GET_ISTATE(lfdir) != DFOUND) {
396 		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
397 		return (0);
398 	}
399 	(void)lftempname(tempname, orphan);
400 	if (makeentry(lfdir, orphan, tempname) == 0) {
401 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
402 		printf("\n\n");
403 		return (0);
404 	}
405 	ILNCOUNT(orphan)--;
406 	if (lostdir) {
407 		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
408 		    parentdir != (ino_t)-1)
409 			(void)makeentry(orphan, lfdir, "..");
410 		dp = ginode(lfdir);
411 		DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
412 		inodirty();
413 		ILNCOUNT(lfdir)++;
414 		pwarn("DIR I=%llu CONNECTED. ",
415 		    (unsigned long long)orphan);
416 		if (parentdir != (ino_t)-1) {
417 			printf("PARENT WAS I=%llu\n",
418 			    (unsigned long long)parentdir);
419 			/*
420 			 * The parent directory, because of the ordering
421 			 * guarantees, has had the link count incremented
422 			 * for the child, but no entry was made.  This
423 			 * fixes the parent link count so that fsck does
424 			 * not need to be rerun.
425 			 */
426 			ILNCOUNT(parentdir)++;
427 		}
428 		if (preen == 0)
429 			printf("\n");
430 	}
431 	return (1);
432 }
433 
434 /*
435  * fix an entry in a directory.
436  */
437 int
changeino(ino_t dir,char * name,ino_t newnum)438 changeino(ino_t dir, char *name, ino_t newnum)
439 {
440 	struct inodesc idesc;
441 
442 	memset(&idesc, 0, sizeof(struct inodesc));
443 	idesc.id_type = DATA;
444 	idesc.id_func = chgino;
445 	idesc.id_number = dir;
446 	idesc.id_fix = DONTKNOW;
447 	idesc.id_name = name;
448 	idesc.id_parent = newnum;	/* new value for name */
449 	return (ckinode(ginode(dir), &idesc));
450 }
451 
452 /*
453  * make an entry in a directory
454  */
455 int
makeentry(ino_t parent,ino_t ino,char * name)456 makeentry(ino_t parent, ino_t ino, char *name)
457 {
458 	union dinode *dp;
459 	struct inodesc idesc;
460 	char pathbuf[PATH_MAX + 1];
461 
462 	if (parent < ROOTINO || parent >= maxino ||
463 	    ino < ROOTINO || ino >= maxino)
464 		return (0);
465 	memset(&idesc, 0, sizeof(struct inodesc));
466 	idesc.id_type = DATA;
467 	idesc.id_func = mkentry;
468 	idesc.id_number = parent;
469 	idesc.id_parent = ino;	/* this is the inode to enter */
470 	idesc.id_fix = DONTKNOW;
471 	idesc.id_name = name;
472 	dp = ginode(parent);
473 	if (DIP(dp, di_size) % DIRBLKSIZ) {
474 		DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ));
475 		inodirty();
476 	}
477 	if ((ckinode(dp, &idesc) & ALTERED) != 0)
478 		return (1);
479 	getpathname(pathbuf, sizeof pathbuf, parent, parent);
480 	dp = ginode(parent);
481 	if (expanddir(dp, pathbuf) == 0)
482 		return (0);
483 	return (ckinode(dp, &idesc) & ALTERED);
484 }
485 
486 /*
487  * Attempt to expand the size of a directory
488  */
489 static int
expanddir(union dinode * dp,char * name)490 expanddir(union dinode *dp, char *name)
491 {
492 	daddr_t lastbn, newblk;
493 	struct bufarea *bp;
494 	char *cp, firstblk[DIRBLKSIZ];
495 	u_int64_t dis;
496 
497 	dis = lblkno(&sblock, DIP(dp, di_size));
498 	if (dis > (u_int64_t)INT_MAX)
499 		return (0);
500 	lastbn = dis;
501 	if (lastbn >= NDADDR - 1 || DIP(dp, di_db[lastbn]) == 0 ||
502 	    DIP(dp, di_size) == 0)
503 		return (0);
504 	if ((newblk = allocblk(sblock.fs_frag)) == 0)
505 		return (0);
506 	DIP_SET(dp, di_db[lastbn + 1], DIP(dp, di_db[lastbn]));
507 	DIP_SET(dp, di_db[lastbn], newblk);
508 	DIP_SET(dp, di_size, DIP(dp, di_size) + sblock.fs_bsize);
509 	DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
510 	bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
511 	    sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
512 	if (bp->b_errs)
513 		goto bad;
514 	memcpy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
515 	bp = getdirblk(newblk, sblock.fs_bsize);
516 	if (bp->b_errs)
517 		goto bad;
518 	memcpy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
519 	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
520 	     cp < &bp->b_un.b_buf[sblock.fs_bsize];
521 	     cp += DIRBLKSIZ)
522 		memcpy(cp, &emptydir, sizeof emptydir);
523 	dirty(bp);
524 	bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
525 	    sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
526 	if (bp->b_errs)
527 		goto bad;
528 	memcpy(bp->b_un.b_buf, &emptydir, sizeof emptydir);
529 	pwarn("NO SPACE LEFT IN %s", name);
530 	if (preen)
531 		printf(" (EXPANDED)\n");
532 	else if (reply("EXPAND") == 0)
533 		goto bad;
534 	dirty(bp);
535 	inodirty();
536 	return (1);
537 bad:
538 	DIP_SET(dp, di_db[lastbn], DIP(dp, di_db[lastbn + 1]));
539 	DIP_SET(dp, di_db[lastbn + 1], 0);
540 	DIP_SET(dp, di_size, DIP(dp, di_size) - sblock.fs_bsize);
541 	DIP_SET(dp, di_blocks, DIP(dp, di_blocks) - btodb(sblock.fs_bsize));
542 	freeblk(newblk, sblock.fs_frag);
543 	return (0);
544 }
545 
546 /*
547  * allocate a new directory
548  */
549 ino_t
allocdir(ino_t parent,ino_t request,int mode)550 allocdir(ino_t parent, ino_t request, int mode)
551 {
552 	ino_t ino;
553 	uid_t uid;
554 	gid_t gid;
555 	char *cp;
556 	union dinode *dp;
557 	struct bufarea *bp;
558 	struct dirtemplate *dirp;
559 	struct inoinfo *inp;
560 
561 	ino = allocino(request, IFDIR|mode);
562 	dirp = &dirhead;
563 	dirp->dot_ino = ino;
564 	dirp->dotdot_ino = parent;
565 	dp = ginode(ino);
566 	bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
567 	if (bp->b_errs) {
568 		freeino(ino);
569 		return (0);
570 	}
571 	memcpy(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
572 	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
573 	     cp < &bp->b_un.b_buf[sblock.fs_fsize];
574 	     cp += DIRBLKSIZ)
575 		memcpy(cp, &emptydir, sizeof emptydir);
576 	dirty(bp);
577 	DIP_SET(dp, di_nlink, 2);
578 	inodirty();
579 	if (ino == ROOTINO) {
580 		ILNCOUNT(ino) = DIP(dp, di_nlink);
581 		cacheino(dp, ino);
582 		return(ino);
583 	}
584 	if (GET_ISTATE(parent) != DSTATE && GET_ISTATE(parent) != DFOUND) {
585 		freeino(ino);
586 		return (0);
587 	}
588 	cacheino(dp, ino);
589 	inp = getinoinfo(ino);
590 	inp->i_parent = parent;
591 	inp->i_dotdot = parent;
592 	SET_ISTATE(ino, GET_ISTATE(parent));
593 	if (GET_ISTATE(ino) == DSTATE) {
594 		ILNCOUNT(ino) = DIP(dp, di_nlink);
595 		ILNCOUNT(parent)++;
596 	}
597 	dp = ginode(parent);
598 	DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
599 	uid = DIP(dp, di_uid);
600 	gid = DIP(dp, di_gid);
601 	inodirty();
602 	dp = ginode(ino);
603 	DIP_SET(dp, di_uid, uid);
604 	DIP_SET(dp, di_gid, gid);
605 	inodirty();
606 	return (ino);
607 }
608 
609 /*
610  * free a directory inode
611  */
612 static void
freedir(ino_t ino,ino_t parent)613 freedir(ino_t ino, ino_t parent)
614 {
615 	union dinode *dp;
616 
617 	if (ino != parent) {
618 		dp = ginode(parent);
619 		DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1);
620 		inodirty();
621 	}
622 	freeino(ino);
623 }
624 
625 /*
626  * generate a temporary name for the lost+found directory.
627  */
628 static int
lftempname(char * bufp,ino_t ino)629 lftempname(char *bufp, ino_t ino)
630 {
631 	ino_t in;
632 	char *cp;
633 	int namlen;
634 
635 	cp = bufp + 2;
636 	for (in = maxino; in > 0; in /= 10)
637 		cp++;
638 	*--cp = 0;
639 	namlen = cp - bufp;
640 	in = ino;
641 	while (cp > bufp) {
642 		*--cp = (in % 10) + '0';
643 		in /= 10;
644 	}
645 	*cp = '#';
646 	return (namlen);
647 }
648 
649 /*
650  * Get a directory block.
651  * Insure that it is held until another is requested.
652  */
653 static struct bufarea *
getdirblk(daddr_t blkno,long size)654 getdirblk(daddr_t blkno, long size)
655 {
656 
657 	if (pdirbp != 0)
658 		pdirbp->b_flags &= ~B_INUSE;
659 	pdirbp = getdatablk(blkno, size);
660 	return (pdirbp);
661 }
662