xref: /netbsd-src/sbin/fsck_ext2fs/inode.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: inode.c,v 1.14 2004/03/22 19:46:53 bouyer 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. 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 /*
33  * Copyright (c) 1997 Manuel Bouyer.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgement:
45  *	This product includes software developed by Manuel Bouyer.
46  * 4. The name of the author may not be used to endorse or promote products
47  *    derived from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 
61 #include <sys/cdefs.h>
62 #ifndef lint
63 #if 0
64 static char sccsid[] = "@(#)inode.c	8.5 (Berkeley) 2/8/95";
65 #else
66 __RCSID("$NetBSD: inode.c,v 1.14 2004/03/22 19:46:53 bouyer Exp $");
67 #endif
68 #endif /* not lint */
69 
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <ufs/ext2fs/ext2fs_dinode.h>
73 #include <ufs/ext2fs/ext2fs_dir.h>
74 #include <ufs/ext2fs/ext2fs.h>
75 
76 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
77 #ifndef SMALL
78 #include <pwd.h>
79 #endif
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #include <time.h>
84 
85 #include "fsck.h"
86 #include "fsutil.h"
87 #include "extern.h"
88 
89 /*
90  * CG is stored in fs byte order in memory, so we can't use ino_to_fsba
91  * here.
92  */
93 
94 #define fsck_ino_to_fsba(fs, x)                      \
95 	(fs2h32((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables) + \
96 	(((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
97 
98 
99 static ino_t startinum;
100 
101 static int iblock __P((struct inodesc *, long, u_int64_t));
102 
103 int
104 ckinode(dp, idesc)
105 	struct ext2fs_dinode *dp;
106 	struct inodesc *idesc;
107 {
108 	u_int32_t *ap;
109 	long ret, n, ndb;
110 	struct ext2fs_dinode dino;
111 	u_int64_t remsize, sizepb;
112 	mode_t mode;
113 	char pathbuf[MAXPATHLEN + 1];
114 
115 	if (idesc->id_fix != IGNORE)
116 		idesc->id_fix = DONTKNOW;
117 	idesc->id_entryno = 0;
118 	idesc->id_filesize = fs2h32(dp->e2di_size);
119 	mode = fs2h16(dp->e2di_mode) & IFMT;
120 	if (mode == IFBLK || mode == IFCHR || mode == IFIFO ||
121 	    (mode == IFLNK && (fs2h32(dp->e2di_size) < EXT2_MAXSYMLINKLEN)))
122 		return (KEEPON);
123 	dino = *dp;
124 	ndb = howmany(fs2h32(dino.e2di_size), sblock.e2fs_bsize);
125 	for (ap = &dino.e2di_blocks[0]; ap < &dino.e2di_blocks[NDADDR];
126 																ap++,ndb--) {
127 		idesc->id_numfrags = 1;
128 		if (*ap == 0) {
129 			if (idesc->id_type == DATA && ndb > 0) {
130 				/* An empty block in a directory XXX */
131 				getpathname(pathbuf, sizeof(pathbuf),
132 				    idesc->id_number, idesc->id_number);
133 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
134 				    pathbuf);
135 				if (reply("ADJUST LENGTH") == 1) {
136 					dp = ginode(idesc->id_number);
137 					dp->e2di_size = h2fs32((ap - &dino.e2di_blocks[0]) *
138 					    sblock.e2fs_bsize);
139 					printf(
140 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
141 					rerun = 1;
142 					inodirty();
143 				}
144 			}
145 			continue;
146 		}
147 		idesc->id_blkno = fs2h32(*ap);
148 		if (idesc->id_type == ADDR)
149 			ret = (*idesc->id_func)(idesc);
150 		else
151 			ret = dirscan(idesc);
152 		if (ret & STOP)
153 			return (ret);
154 	}
155 	idesc->id_numfrags = 1;
156 	remsize = fs2h32(dino.e2di_size) - sblock.e2fs_bsize * NDADDR;
157 	sizepb = sblock.e2fs_bsize;
158 	for (ap = &dino.e2di_blocks[NDADDR], n = 1; n <= NIADDR; ap++, n++) {
159 		if (*ap) {
160 			idesc->id_blkno = fs2h32(*ap);
161 			ret = iblock(idesc, n, remsize);
162 			if (ret & STOP)
163 				return (ret);
164 		} else {
165 			if (idesc->id_type == DATA && remsize > 0) {
166 				/* An empty block in a directory XXX */
167 				getpathname(pathbuf, sizeof(pathbuf),
168 				    idesc->id_number, idesc->id_number);
169 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
170 				    pathbuf);
171 				if (reply("ADJUST LENGTH") == 1) {
172 					dp = ginode(idesc->id_number);
173 					dp->e2di_size = h2fs32(fs2h32(dp->e2di_size) - remsize);
174 					remsize = 0;
175 					printf(
176 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
177 					rerun = 1;
178 					inodirty();
179 					break;
180 				}
181 			}
182 		}
183 		sizepb *= NINDIR(&sblock);
184 		remsize -= sizepb;
185 	}
186 	return (KEEPON);
187 }
188 
189 static int
190 iblock(idesc, ilevel, isize)
191 	struct inodesc *idesc;
192 	long ilevel;
193 	u_int64_t isize;
194 {
195 	/* XXX ondisk32 */
196 	int32_t *ap;
197 	int32_t *aplim;
198 	struct bufarea *bp;
199 	int i, n, (*func) __P((struct inodesc *)), nif;
200 	u_int64_t sizepb;
201 	char buf[BUFSIZ];
202 	char pathbuf[MAXPATHLEN + 1];
203 	struct ext2fs_dinode *dp;
204 
205 	if (idesc->id_type == ADDR) {
206 		func = idesc->id_func;
207 		if (((n = (*func)(idesc)) & KEEPON) == 0)
208 			return (n);
209 	} else
210 		func = dirscan;
211 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
212 		return (SKIP);
213 	bp = getdatablk(idesc->id_blkno, sblock.e2fs_bsize);
214 	ilevel--;
215 	for (sizepb = sblock.e2fs_bsize, i = 0; i < ilevel; i++)
216 		sizepb *= NINDIR(&sblock);
217 	if (isize > sizepb * NINDIR(&sblock))
218 		nif = NINDIR(&sblock);
219 	else
220 		nif = howmany(isize, sizepb);
221 	if (idesc->id_func == pass1check &&
222 		nif < NINDIR(&sblock)) {
223 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
224 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
225 			if (*ap == 0)
226 				continue;
227 			(void)snprintf(buf, sizeof(buf),
228 			    "PARTIALLY TRUNCATED INODE I=%u", idesc->id_number);
229 			if (dofix(idesc, buf)) {
230 				*ap = 0;
231 				dirty(bp);
232 			}
233 		}
234 		flush(fswritefd, bp);
235 	}
236 	aplim = &bp->b_un.b_indir[nif];
237 	for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
238 		if (*ap) {
239 			idesc->id_blkno = fs2h32(*ap);
240 			if (ilevel == 0)
241 				n = (*func)(idesc);
242 			else
243 				n = iblock(idesc, ilevel, isize);
244 			if (n & STOP) {
245 				bp->b_flags &= ~B_INUSE;
246 				return (n);
247 			}
248 		} else {
249 			if (idesc->id_type == DATA && isize > 0) {
250 				/* An empty block in a directory XXX */
251 				getpathname(pathbuf, sizeof(pathbuf),
252 				    idesc->id_number, idesc->id_number);
253 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
254 				    pathbuf);
255 				if (reply("ADJUST LENGTH") == 1) {
256 					dp = ginode(idesc->id_number);
257 					dp->e2di_size = h2fs32(fs2h32(dp->e2di_size) - isize);
258 					isize = 0;
259 					printf(
260 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
261 					rerun = 1;
262 					inodirty();
263 					bp->b_flags &= ~B_INUSE;
264 					return(STOP);
265 				}
266 			}
267 		}
268 		isize -= sizepb;
269 	}
270 	bp->b_flags &= ~B_INUSE;
271 	return (KEEPON);
272 }
273 
274 /*
275  * Check that a block in a legal block number.
276  * Return 0 if in range, 1 if out of range.
277  */
278 int
279 chkrange(blk, cnt)
280 	daddr_t blk;
281 	int cnt;
282 {
283 	int c, overh;
284 
285 	if ((unsigned)(blk + cnt) > maxfsblock)
286 		return (1);
287 	c = dtog(&sblock, blk);
288 	overh = cgoverhead(c);
289 	if (blk < sblock.e2fs.e2fs_bpg * c + overh +
290 	    sblock.e2fs.e2fs_first_dblock) {
291 		if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + overh +
292 		    sblock.e2fs.e2fs_first_dblock) {
293 			if (debug) {
294 				printf("blk %lld < cgdmin %d;",
295 				    (long long)blk,
296 				    sblock.e2fs.e2fs_bpg * c + overh +
297 				    sblock.e2fs.e2fs_first_dblock);
298 				printf(" blk + cnt %lld > cgsbase %d\n",
299 				    (long long)(blk + cnt),
300 				    sblock.e2fs.e2fs_bpg * c +
301 				    overh + sblock.e2fs.e2fs_first_dblock);
302 			}
303 			return (1);
304 		}
305 	} else {
306 		if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + overh +
307 		    sblock.e2fs.e2fs_first_dblock) {
308 			if (debug)  {
309 				printf("blk %lld >= cgdmin %d;",
310 				    (long long)blk,
311 				    sblock.e2fs.e2fs_bpg * c + overh +
312 				    sblock.e2fs.e2fs_first_dblock);
313 				printf(" blk + cnt %lld > cgdmax %d\n",
314 				    (long long)(blk+cnt),
315 				    sblock.e2fs.e2fs_bpg * (c + 1) +
316 				    overh + sblock.e2fs.e2fs_first_dblock);
317 			}
318 			return (1);
319 		}
320 	}
321 	return (0);
322 }
323 
324 /*
325  * General purpose interface for reading inodes.
326  */
327 struct ext2fs_dinode *
328 ginode(inumber)
329 	ino_t inumber;
330 {
331 	daddr_t iblk;
332 
333 	if ((inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
334 		|| inumber > maxino)
335 		errexit("bad inode number %d to ginode\n", inumber);
336 	if (startinum == 0 ||
337 	    inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
338 		iblk = fsck_ino_to_fsba(&sblock, inumber);
339 		if (pbp != 0)
340 			pbp->b_flags &= ~B_INUSE;
341 		pbp = getdatablk(iblk, sblock.e2fs_bsize);
342 		startinum = ((inumber -1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
343 	}
344 	return (&pbp->b_un.b_dinode[(inumber-1) % sblock.e2fs_ipb]);
345 }
346 
347 /*
348  * Special purpose version of ginode used to optimize first pass
349  * over all the inodes in numerical order.
350  */
351 ino_t nextino, lastinum;
352 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
353 struct ext2fs_dinode *inodebuf;
354 
355 struct ext2fs_dinode *
356 getnextinode(inumber)
357 	ino_t inumber;
358 {
359 	long size;
360 	daddr_t dblk;
361 	static struct ext2fs_dinode *dp;
362 
363 	if (inumber != nextino++ || inumber > maxino)
364 		errexit("bad inode number %d to nextinode\n", inumber);
365 	if (inumber >= lastinum) {
366 		readcnt++;
367 		dblk = fsbtodb(&sblock, fsck_ino_to_fsba(&sblock, lastinum));
368 		if (readcnt % readpercg == 0) {
369 			size = partialsize;
370 			lastinum += partialcnt;
371 		} else {
372 			size = inobufsize;
373 			lastinum += fullcnt;
374 		}
375 		(void)bread(fsreadfd, (char *)inodebuf, dblk, size);
376 		dp = inodebuf;
377 	}
378 	return (dp++);
379 }
380 
381 void
382 resetinodebuf()
383 {
384 
385 	startinum = 0;
386 	nextino = 1;
387 	lastinum = 1;
388 	readcnt = 0;
389 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
390 	fullcnt = inobufsize / sizeof(struct ext2fs_dinode);
391 	readpercg = sblock.e2fs.e2fs_ipg / fullcnt;
392 	partialcnt = sblock.e2fs.e2fs_ipg % fullcnt;
393 	partialsize = partialcnt * sizeof(struct ext2fs_dinode);
394 	if (partialcnt != 0) {
395 		readpercg++;
396 	} else {
397 		partialcnt = fullcnt;
398 		partialsize = inobufsize;
399 	}
400 	if (inodebuf == NULL &&
401 	    (inodebuf = (struct ext2fs_dinode *)malloc((unsigned)inobufsize)) ==
402 		NULL)
403 		errexit("Cannot allocate space for inode buffer\n");
404 	while (nextino < EXT2_ROOTINO)
405 		(void)getnextinode(nextino);
406 }
407 
408 void
409 freeinodebuf()
410 {
411 
412 	if (inodebuf != NULL)
413 		free((char *)inodebuf);
414 	inodebuf = NULL;
415 }
416 
417 /*
418  * Routines to maintain information about directory inodes.
419  * This is built during the first pass and used during the
420  * second and third passes.
421  *
422  * Enter inodes into the cache.
423  */
424 void
425 cacheino(dp, inumber)
426 	struct ext2fs_dinode *dp;
427 	ino_t inumber;
428 {
429 	struct inoinfo *inp;
430 	struct inoinfo **inpp;
431 	unsigned int blks;
432 
433 	blks = howmany(fs2h32(dp->e2di_size), sblock.e2fs_bsize);
434 	if (blks > NDADDR)
435 		blks = NDADDR + NIADDR;
436 	/* XXX ondisk32 */
437 	inp = (struct inoinfo *)
438 		malloc(sizeof(*inp) + (blks - 1) * sizeof(int32_t));
439 	if (inp == NULL)
440 		return;
441 	inpp = &inphead[inumber % numdirs];
442 	inp->i_nexthash = *inpp;
443 	*inpp = inp;
444 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
445 	if (inumber == EXT2_ROOTINO)
446 		inp->i_parent = EXT2_ROOTINO;
447 	else
448 		inp->i_parent = (ino_t)0;
449 	inp->i_dotdot = (ino_t)0;
450 	inp->i_number = inumber;
451 	inp->i_isize = fs2h32(dp->e2di_size);
452 	/* XXX ondisk32 */
453 	inp->i_numblks = blks * sizeof(int32_t);
454 	memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
455 	if (inplast == listmax) {
456 		listmax += 100;
457 		inpsort = (struct inoinfo **)realloc((char *)inpsort,
458 		    (unsigned)listmax * sizeof(struct inoinfo *));
459 		if (inpsort == NULL)
460 			errexit("cannot increase directory list\n");
461 	}
462 	inpsort[inplast++] = inp;
463 }
464 
465 /*
466  * Look up an inode cache structure.
467  */
468 struct inoinfo *
469 getinoinfo(inumber)
470 	ino_t inumber;
471 {
472 	struct inoinfo *inp;
473 
474 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
475 		if (inp->i_number != inumber)
476 			continue;
477 		return (inp);
478 	}
479 	errexit("cannot find inode %d\n", inumber);
480 	return ((struct inoinfo *)0);
481 }
482 
483 /*
484  * Clean up all the inode cache structure.
485  */
486 void
487 inocleanup()
488 {
489 	struct inoinfo **inpp;
490 
491 	if (inphead == NULL)
492 		return;
493 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
494 		free((char *)(*inpp));
495 	free((char *)inphead);
496 	free((char *)inpsort);
497 	inphead = inpsort = NULL;
498 }
499 
500 void
501 inodirty()
502 {
503 
504 	dirty(pbp);
505 }
506 
507 void
508 clri(idesc, type, flag)
509 	struct inodesc *idesc;
510 	char *type;
511 	int flag;
512 {
513 	struct ext2fs_dinode *dp;
514 
515 	dp = ginode(idesc->id_number);
516 	if (flag == 1) {
517 		pwarn("%s %s", type,
518 		    (dp->e2di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
519 		pinode(idesc->id_number);
520 	}
521 	if (preen || reply("CLEAR") == 1) {
522 		if (preen)
523 			printf(" (CLEARED)\n");
524 		n_files--;
525 		(void)ckinode(dp, idesc);
526 		clearinode(dp);
527 		statemap[idesc->id_number] = USTATE;
528 		inodirty();
529 	}
530 }
531 
532 int
533 findname(idesc)
534 	struct inodesc *idesc;
535 {
536 	struct ext2fs_direct *dirp = idesc->id_dirp;
537 	u_int16_t namlen = dirp->e2d_namlen;
538 
539 	if (fs2h32(dirp->e2d_ino) != idesc->id_parent)
540 		return (KEEPON);
541 	memcpy(idesc->id_name, dirp->e2d_name, (size_t)namlen);
542 	idesc->id_name[namlen] = '\0';
543 	return (STOP|FOUND);
544 }
545 
546 int
547 findino(idesc)
548 	struct inodesc *idesc;
549 {
550 	struct ext2fs_direct *dirp = idesc->id_dirp;
551 	u_int32_t ino = fs2h32(dirp->e2d_ino);
552 
553 	if (ino == 0)
554 		return (KEEPON);
555 	if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
556 	    (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
557 		&& ino <= maxino) {
558 		idesc->id_parent = ino;
559 		return (STOP|FOUND);
560 	}
561 	return (KEEPON);
562 }
563 
564 void
565 pinode(ino)
566 	ino_t ino;
567 {
568 	struct ext2fs_dinode *dp;
569 	char *p;
570 	struct passwd *pw;
571 	time_t t;
572 
573 	printf(" I=%u ", ino);
574 	if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
575 		return;
576 	dp = ginode(ino);
577 	printf(" OWNER=");
578 #ifndef SMALL
579 	if ((pw = getpwuid((int)dp->e2di_uid)) != 0)
580 		printf("%s ", pw->pw_name);
581 	else
582 #endif
583 		printf("%u ", (unsigned)fs2h16(dp->e2di_uid));
584 	printf("MODE=%o\n", fs2h16(dp->e2di_mode));
585 	if (preen)
586 		printf("%s: ", cdevname());
587 	printf("SIZE=%u ", fs2h32(dp->e2di_size));
588 	t = fs2h32(dp->e2di_mtime);
589 	p = ctime(&t);
590 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
591 }
592 
593 void
594 blkerror(ino, type, blk)
595 	ino_t ino;
596 	char *type;
597 	daddr_t blk;
598 {
599 
600 	pfatal("%lld %s I=%u", (long long)blk, type, ino);
601 	printf("\n");
602 	switch (statemap[ino]) {
603 
604 	case FSTATE:
605 		statemap[ino] = FCLEAR;
606 		return;
607 
608 	case DSTATE:
609 		statemap[ino] = DCLEAR;
610 		return;
611 
612 	case FCLEAR:
613 	case DCLEAR:
614 		return;
615 
616 	default:
617 		errexit("BAD STATE %d TO BLKERR\n", statemap[ino]);
618 		/* NOTREACHED */
619 	}
620 }
621 
622 /*
623  * allocate an unused inode
624  */
625 ino_t
626 allocino(request, type)
627 	ino_t request;
628 	int type;
629 {
630 	ino_t ino;
631 	struct ext2fs_dinode *dp;
632 	time_t t;
633 
634 	if (request == 0)
635 		request = EXT2_ROOTINO;
636 	else if (statemap[request] != USTATE)
637 		return (0);
638 	for (ino = request; ino < maxino; ino++) {
639 		if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
640 			continue;
641 		if (statemap[ino] == USTATE)
642 			break;
643 	}
644 	if (ino == maxino)
645 		return (0);
646 	switch (type & IFMT) {
647 	case IFDIR:
648 		statemap[ino] = DSTATE;
649 		break;
650 	case IFREG:
651 	case IFLNK:
652 		statemap[ino] = FSTATE;
653 		break;
654 	default:
655 		return (0);
656 	}
657 	dp = ginode(ino);
658 	dp->e2di_blocks[0] = h2fs32(allocblk());
659 	if (dp->e2di_blocks[0] == 0) {
660 		statemap[ino] = USTATE;
661 		return (0);
662 	}
663 	dp->e2di_mode = h2fs16(type);
664 	(void)time(&t);
665 	dp->e2di_atime = h2fs32(t);
666 	dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
667 	dp->e2di_dtime = 0;
668 	dp->e2di_size = h2fs32(sblock.e2fs_bsize);
669 	dp->e2di_nblock = h2fs32(btodb(sblock.e2fs_bsize));
670 	n_files++;
671 	inodirty();
672 	typemap[ino] = E2IFTODT(type);
673 	return (ino);
674 }
675 
676 /*
677  * deallocate an inode
678  */
679 void
680 freeino(ino)
681 	ino_t ino;
682 {
683 	struct inodesc idesc;
684 	struct ext2fs_dinode *dp;
685 
686 	memset(&idesc, 0, sizeof(struct inodesc));
687 	idesc.id_type = ADDR;
688 	idesc.id_func = pass4check;
689 	idesc.id_number = ino;
690 	dp = ginode(ino);
691 	(void)ckinode(dp, &idesc);
692 	clearinode(dp);
693 	inodirty();
694 	statemap[ino] = USTATE;
695 	n_files--;
696 }
697