xref: /netbsd-src/sbin/fsck_ext2fs/inode.c (revision fd5cb0acea84d278e04e640d37ca2398f894991f)
1 /*	$NetBSD: inode.c,v 1.16 2005/02/05 14:26:05 xtraeme 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.16 2005/02/05 14:26:05 xtraeme 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(struct inodesc *, long, u_int64_t);
102 
103 int
104 ckinode(struct ext2fs_dinode *dp, struct inodesc *idesc)
105 {
106 	u_int32_t *ap;
107 	long ret, n, ndb;
108 	struct ext2fs_dinode dino;
109 	u_int64_t remsize, sizepb;
110 	mode_t mode;
111 	char pathbuf[MAXPATHLEN + 1];
112 
113 	if (idesc->id_fix != IGNORE)
114 		idesc->id_fix = DONTKNOW;
115 	idesc->id_entryno = 0;
116 	idesc->id_filesize = fs2h32(dp->e2di_size);
117 	mode = fs2h16(dp->e2di_mode) & IFMT;
118 	if (mode == IFBLK || mode == IFCHR || mode == IFIFO ||
119 	    (mode == IFLNK && (fs2h32(dp->e2di_size) < EXT2_MAXSYMLINKLEN)))
120 		return (KEEPON);
121 	dino = *dp;
122 	ndb = howmany(fs2h32(dino.e2di_size), sblock.e2fs_bsize);
123 	for (ap = &dino.e2di_blocks[0]; ap < &dino.e2di_blocks[NDADDR];
124 																ap++,ndb--) {
125 		idesc->id_numfrags = 1;
126 		if (*ap == 0) {
127 			if (idesc->id_type == DATA && ndb > 0) {
128 				/* An empty block in a directory XXX */
129 				getpathname(pathbuf, sizeof(pathbuf),
130 				    idesc->id_number, idesc->id_number);
131 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
132 				    pathbuf);
133 				if (reply("ADJUST LENGTH") == 1) {
134 					dp = ginode(idesc->id_number);
135 					dp->e2di_size = h2fs32((ap - &dino.e2di_blocks[0]) *
136 					    sblock.e2fs_bsize);
137 					printf(
138 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
139 					rerun = 1;
140 					inodirty();
141 				}
142 			}
143 			continue;
144 		}
145 		idesc->id_blkno = fs2h32(*ap);
146 		if (idesc->id_type == ADDR)
147 			ret = (*idesc->id_func)(idesc);
148 		else
149 			ret = dirscan(idesc);
150 		if (ret & STOP)
151 			return (ret);
152 	}
153 	idesc->id_numfrags = 1;
154 	remsize = fs2h32(dino.e2di_size) - sblock.e2fs_bsize * NDADDR;
155 	sizepb = sblock.e2fs_bsize;
156 	for (ap = &dino.e2di_blocks[NDADDR], n = 1; n <= NIADDR; ap++, n++) {
157 		if (*ap) {
158 			idesc->id_blkno = fs2h32(*ap);
159 			ret = iblock(idesc, n, remsize);
160 			if (ret & STOP)
161 				return (ret);
162 		} else {
163 			if (idesc->id_type == DATA && remsize > 0) {
164 				/* An empty block in a directory XXX */
165 				getpathname(pathbuf, sizeof(pathbuf),
166 				    idesc->id_number, idesc->id_number);
167 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
168 				    pathbuf);
169 				if (reply("ADJUST LENGTH") == 1) {
170 					dp = ginode(idesc->id_number);
171 					dp->e2di_size = h2fs32(fs2h32(dp->e2di_size) - remsize);
172 					remsize = 0;
173 					printf(
174 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
175 					rerun = 1;
176 					inodirty();
177 					break;
178 				}
179 			}
180 		}
181 		sizepb *= NINDIR(&sblock);
182 		remsize -= sizepb;
183 	}
184 	return (KEEPON);
185 }
186 
187 static int
188 iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
189 {
190 	/* XXX ondisk32 */
191 	int32_t *ap;
192 	int32_t *aplim;
193 	struct bufarea *bp;
194 	int i, n, (*func)(struct inodesc *), nif;
195 	u_int64_t sizepb;
196 	char buf[BUFSIZ];
197 	char pathbuf[MAXPATHLEN + 1];
198 	struct ext2fs_dinode *dp;
199 
200 	if (idesc->id_type == ADDR) {
201 		func = idesc->id_func;
202 		if (((n = (*func)(idesc)) & KEEPON) == 0)
203 			return (n);
204 	} else
205 		func = dirscan;
206 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
207 		return (SKIP);
208 	bp = getdatablk(idesc->id_blkno, sblock.e2fs_bsize);
209 	ilevel--;
210 	for (sizepb = sblock.e2fs_bsize, i = 0; i < ilevel; i++)
211 		sizepb *= NINDIR(&sblock);
212 	if (isize > sizepb * NINDIR(&sblock))
213 		nif = NINDIR(&sblock);
214 	else
215 		nif = howmany(isize, sizepb);
216 	if (idesc->id_func == pass1check &&
217 		nif < NINDIR(&sblock)) {
218 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
219 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
220 			if (*ap == 0)
221 				continue;
222 			(void)snprintf(buf, sizeof(buf),
223 			    "PARTIALLY TRUNCATED INODE I=%u", idesc->id_number);
224 			if (dofix(idesc, buf)) {
225 				*ap = 0;
226 				dirty(bp);
227 			}
228 		}
229 		flush(fswritefd, bp);
230 	}
231 	aplim = &bp->b_un.b_indir[nif];
232 	for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
233 		if (*ap) {
234 			idesc->id_blkno = fs2h32(*ap);
235 			if (ilevel == 0)
236 				n = (*func)(idesc);
237 			else
238 				n = iblock(idesc, ilevel, isize);
239 			if (n & STOP) {
240 				bp->b_flags &= ~B_INUSE;
241 				return (n);
242 			}
243 		} else {
244 			if (idesc->id_type == DATA && isize > 0) {
245 				/* An empty block in a directory XXX */
246 				getpathname(pathbuf, sizeof(pathbuf),
247 				    idesc->id_number, idesc->id_number);
248 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
249 				    pathbuf);
250 				if (reply("ADJUST LENGTH") == 1) {
251 					dp = ginode(idesc->id_number);
252 					dp->e2di_size = h2fs32(fs2h32(dp->e2di_size) - isize);
253 					isize = 0;
254 					printf(
255 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
256 					rerun = 1;
257 					inodirty();
258 					bp->b_flags &= ~B_INUSE;
259 					return(STOP);
260 				}
261 			}
262 		}
263 		isize -= sizepb;
264 	}
265 	bp->b_flags &= ~B_INUSE;
266 	return (KEEPON);
267 }
268 
269 /*
270  * Check that a block in a legal block number.
271  * Return 0 if in range, 1 if out of range.
272  */
273 int
274 chkrange(daddr_t blk, int cnt)
275 {
276 	int c, overh;
277 
278 	if ((unsigned)(blk + cnt) > maxfsblock)
279 		return (1);
280 	c = dtog(&sblock, blk);
281 	overh = cgoverhead(c);
282 	if (blk < sblock.e2fs.e2fs_bpg * c + overh +
283 	    sblock.e2fs.e2fs_first_dblock) {
284 		if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + overh +
285 		    sblock.e2fs.e2fs_first_dblock) {
286 			if (debug) {
287 				printf("blk %lld < cgdmin %d;",
288 				    (long long)blk,
289 				    sblock.e2fs.e2fs_bpg * c + overh +
290 				    sblock.e2fs.e2fs_first_dblock);
291 				printf(" blk + cnt %lld > cgsbase %d\n",
292 				    (long long)(blk + cnt),
293 				    sblock.e2fs.e2fs_bpg * c +
294 				    overh + sblock.e2fs.e2fs_first_dblock);
295 			}
296 			return (1);
297 		}
298 	} else {
299 		if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + overh +
300 		    sblock.e2fs.e2fs_first_dblock) {
301 			if (debug)  {
302 				printf("blk %lld >= cgdmin %d;",
303 				    (long long)blk,
304 				    sblock.e2fs.e2fs_bpg * c + overh +
305 				    sblock.e2fs.e2fs_first_dblock);
306 				printf(" blk + cnt %lld > cgdmax %d\n",
307 				    (long long)(blk+cnt),
308 				    sblock.e2fs.e2fs_bpg * (c + 1) +
309 				    overh + sblock.e2fs.e2fs_first_dblock);
310 			}
311 			return (1);
312 		}
313 	}
314 	return (0);
315 }
316 
317 /*
318  * General purpose interface for reading inodes.
319  */
320 struct ext2fs_dinode *
321 ginode(ino_t inumber)
322 {
323 	daddr_t iblk;
324 
325 	if ((inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
326 		|| inumber > maxino)
327 		errexit("bad inode number %d to ginode\n", inumber);
328 	if (startinum == 0 ||
329 	    inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
330 		iblk = fsck_ino_to_fsba(&sblock, inumber);
331 		if (pbp != 0)
332 			pbp->b_flags &= ~B_INUSE;
333 		pbp = getdatablk(iblk, sblock.e2fs_bsize);
334 		startinum = ((inumber -1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
335 	}
336 	return (&pbp->b_un.b_dinode[(inumber-1) % sblock.e2fs_ipb]);
337 }
338 
339 /*
340  * Special purpose version of ginode used to optimize first pass
341  * over all the inodes in numerical order.
342  */
343 ino_t nextino, lastinum;
344 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
345 struct ext2fs_dinode *inodebuf;
346 
347 struct ext2fs_dinode *
348 getnextinode(ino_t inumber)
349 {
350 	long size;
351 	daddr_t dblk;
352 	static struct ext2fs_dinode *dp;
353 
354 	if (inumber != nextino++ || inumber > maxino)
355 		errexit("bad inode number %d to nextinode\n", inumber);
356 	if (inumber >= lastinum) {
357 		readcnt++;
358 		dblk = fsbtodb(&sblock, fsck_ino_to_fsba(&sblock, lastinum));
359 		if (readcnt % readpercg == 0) {
360 			size = partialsize;
361 			lastinum += partialcnt;
362 		} else {
363 			size = inobufsize;
364 			lastinum += fullcnt;
365 		}
366 		(void)bread(fsreadfd, (char *)inodebuf, dblk, size);
367 		dp = inodebuf;
368 	}
369 	return (dp++);
370 }
371 
372 void
373 resetinodebuf(void)
374 {
375 
376 	startinum = 0;
377 	nextino = 1;
378 	lastinum = 1;
379 	readcnt = 0;
380 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
381 	fullcnt = inobufsize / sizeof(struct ext2fs_dinode);
382 	readpercg = sblock.e2fs.e2fs_ipg / fullcnt;
383 	partialcnt = sblock.e2fs.e2fs_ipg % fullcnt;
384 	partialsize = partialcnt * sizeof(struct ext2fs_dinode);
385 	if (partialcnt != 0) {
386 		readpercg++;
387 	} else {
388 		partialcnt = fullcnt;
389 		partialsize = inobufsize;
390 	}
391 	if (inodebuf == NULL &&
392 	    (inodebuf = (struct ext2fs_dinode *)malloc((unsigned)inobufsize)) ==
393 		NULL)
394 		errexit("Cannot allocate space for inode buffer\n");
395 	while (nextino < EXT2_ROOTINO)
396 		(void)getnextinode(nextino);
397 }
398 
399 void
400 freeinodebuf(void)
401 {
402 
403 	if (inodebuf != NULL)
404 		free((char *)inodebuf);
405 	inodebuf = NULL;
406 }
407 
408 /*
409  * Routines to maintain information about directory inodes.
410  * This is built during the first pass and used during the
411  * second and third passes.
412  *
413  * Enter inodes into the cache.
414  */
415 void
416 cacheino(struct ext2fs_dinode *dp, ino_t inumber)
417 {
418 	struct inoinfo *inp;
419 	struct inoinfo **inpp;
420 	unsigned int blks;
421 
422 	blks = howmany(fs2h32(dp->e2di_size), sblock.e2fs_bsize);
423 	if (blks > NDADDR)
424 		blks = NDADDR + NIADDR;
425 	/* XXX ondisk32 */
426 	inp = (struct inoinfo *)
427 		malloc(sizeof(*inp) + (blks - 1) * sizeof(int32_t));
428 	if (inp == NULL)
429 		return;
430 	inpp = &inphead[inumber % numdirs];
431 	inp->i_nexthash = *inpp;
432 	*inpp = inp;
433 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
434 	if (inumber == EXT2_ROOTINO)
435 		inp->i_parent = EXT2_ROOTINO;
436 	else
437 		inp->i_parent = (ino_t)0;
438 	inp->i_dotdot = (ino_t)0;
439 	inp->i_number = inumber;
440 	inp->i_isize = fs2h32(dp->e2di_size);
441 	/* XXX ondisk32 */
442 	inp->i_numblks = blks * sizeof(int32_t);
443 	memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
444 	if (inplast == listmax) {
445 		listmax += 100;
446 		inpsort = (struct inoinfo **)realloc((char *)inpsort,
447 		    (unsigned)listmax * sizeof(struct inoinfo *));
448 		if (inpsort == NULL)
449 			errexit("cannot increase directory list\n");
450 	}
451 	inpsort[inplast++] = inp;
452 }
453 
454 /*
455  * Look up an inode cache structure.
456  */
457 struct inoinfo *
458 getinoinfo(ino_t inumber)
459 {
460 	struct inoinfo *inp;
461 
462 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
463 		if (inp->i_number != inumber)
464 			continue;
465 		return (inp);
466 	}
467 	errexit("cannot find inode %d\n", inumber);
468 	return ((struct inoinfo *)0);
469 }
470 
471 /*
472  * Clean up all the inode cache structure.
473  */
474 void
475 inocleanup(void)
476 {
477 	struct inoinfo **inpp;
478 
479 	if (inphead == NULL)
480 		return;
481 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
482 		free((char *)(*inpp));
483 	free((char *)inphead);
484 	free((char *)inpsort);
485 	inphead = inpsort = NULL;
486 }
487 
488 void
489 inodirty(void)
490 {
491 
492 	dirty(pbp);
493 }
494 
495 void
496 clri(struct inodesc *idesc, char *type, int flag)
497 {
498 	struct ext2fs_dinode *dp;
499 
500 	dp = ginode(idesc->id_number);
501 	if (flag == 1) {
502 		pwarn("%s %s", type,
503 		    (dp->e2di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
504 		pinode(idesc->id_number);
505 	}
506 	if (preen || reply("CLEAR") == 1) {
507 		if (preen)
508 			printf(" (CLEARED)\n");
509 		n_files--;
510 		(void)ckinode(dp, idesc);
511 		clearinode(dp);
512 		statemap[idesc->id_number] = USTATE;
513 		inodirty();
514 	}
515 }
516 
517 int
518 findname(struct inodesc *idesc)
519 {
520 	struct ext2fs_direct *dirp = idesc->id_dirp;
521 	u_int16_t namlen = dirp->e2d_namlen;
522 
523 	if (fs2h32(dirp->e2d_ino) != idesc->id_parent)
524 		return (KEEPON);
525 	memcpy(idesc->id_name, dirp->e2d_name, (size_t)namlen);
526 	idesc->id_name[namlen] = '\0';
527 	return (STOP|FOUND);
528 }
529 
530 int
531 findino(struct inodesc *idesc)
532 {
533 	struct ext2fs_direct *dirp = idesc->id_dirp;
534 	u_int32_t ino = fs2h32(dirp->e2d_ino);
535 
536 	if (ino == 0)
537 		return (KEEPON);
538 	if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
539 	    (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
540 		&& ino <= maxino) {
541 		idesc->id_parent = ino;
542 		return (STOP|FOUND);
543 	}
544 	return (KEEPON);
545 }
546 
547 void
548 pinode(ino_t ino)
549 {
550 	struct ext2fs_dinode *dp;
551 	char *p;
552 	struct passwd *pw;
553 	time_t t;
554 
555 	printf(" I=%u ", ino);
556 	if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
557 		return;
558 	dp = ginode(ino);
559 	printf(" OWNER=");
560 #ifndef SMALL
561 	if ((pw = getpwuid((int)dp->e2di_uid)) != 0)
562 		printf("%s ", pw->pw_name);
563 	else
564 #endif
565 		printf("%u ", (unsigned)fs2h16(dp->e2di_uid));
566 	printf("MODE=%o\n", fs2h16(dp->e2di_mode));
567 	if (preen)
568 		printf("%s: ", cdevname());
569 	printf("SIZE=%u ", fs2h32(dp->e2di_size));
570 	t = fs2h32(dp->e2di_mtime);
571 	p = ctime(&t);
572 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
573 }
574 
575 void
576 blkerror(ino_t ino, char *type, daddr_t blk)
577 {
578 
579 	pfatal("%lld %s I=%u", (long long)blk, type, ino);
580 	printf("\n");
581 	switch (statemap[ino]) {
582 
583 	case FSTATE:
584 		statemap[ino] = FCLEAR;
585 		return;
586 
587 	case DSTATE:
588 		statemap[ino] = DCLEAR;
589 		return;
590 
591 	case FCLEAR:
592 	case DCLEAR:
593 		return;
594 
595 	default:
596 		errexit("BAD STATE %d TO BLKERR\n", statemap[ino]);
597 		/* NOTREACHED */
598 	}
599 }
600 
601 /*
602  * allocate an unused inode
603  */
604 ino_t
605 allocino(ino_t request, int type)
606 {
607 	ino_t ino;
608 	struct ext2fs_dinode *dp;
609 	time_t t;
610 
611 	if (request == 0)
612 		request = EXT2_ROOTINO;
613 	else if (statemap[request] != USTATE)
614 		return (0);
615 	for (ino = request; ino < maxino; ino++) {
616 		if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
617 			continue;
618 		if (statemap[ino] == USTATE)
619 			break;
620 	}
621 	if (ino == maxino)
622 		return (0);
623 	switch (type & IFMT) {
624 	case IFDIR:
625 		statemap[ino] = DSTATE;
626 		break;
627 	case IFREG:
628 	case IFLNK:
629 		statemap[ino] = FSTATE;
630 		break;
631 	default:
632 		return (0);
633 	}
634 	dp = ginode(ino);
635 	dp->e2di_blocks[0] = h2fs32(allocblk());
636 	if (dp->e2di_blocks[0] == 0) {
637 		statemap[ino] = USTATE;
638 		return (0);
639 	}
640 	dp->e2di_mode = h2fs16(type);
641 	(void)time(&t);
642 	dp->e2di_atime = h2fs32(t);
643 	dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
644 	dp->e2di_dtime = 0;
645 	dp->e2di_size = h2fs32(sblock.e2fs_bsize);
646 	dp->e2di_nblock = h2fs32(btodb(sblock.e2fs_bsize));
647 	n_files++;
648 	inodirty();
649 	typemap[ino] = E2IFTODT(type);
650 	return (ino);
651 }
652 
653 /*
654  * deallocate an inode
655  */
656 void
657 freeino(ino_t ino)
658 {
659 	struct inodesc idesc;
660 	struct ext2fs_dinode *dp;
661 
662 	memset(&idesc, 0, sizeof(struct inodesc));
663 	idesc.id_type = ADDR;
664 	idesc.id_func = pass4check;
665 	idesc.id_number = ino;
666 	dp = ginode(ino);
667 	(void)ckinode(dp, &idesc);
668 	clearinode(dp);
669 	inodirty();
670 	statemap[ino] = USTATE;
671 	n_files--;
672 }
673