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