xref: /openbsd-src/sbin/fsck_ext2fs/inode.c (revision 6f05df2d9be0954bec42d51d943d77bd250fb664)
1 /*	$OpenBSD: inode.c,v 1.24 2014/10/29 06:31:58 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 	(letoh32((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 = letoh32(dp->e2di_size);
89 
90 	if ((letoh16(dp->e2di_mode) & IFMT) == IFREG)
91 		size |= (u_int64_t)letoh32(dp->e2di_size_hi) << 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 ((letoh16(dp->e2di_mode) & IFMT) == IFREG) {
101 		dp->e2di_size_hi = htole32(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, letoh16(dp->e2di_mode) & IFMT);
108 		return;
109 	}
110 	dp->e2di_size = htole32(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 = letoh16(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 = letoh32(*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 = letoh32(*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 = letoh32(*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 = malloc((unsigned)inobufsize)) == NULL)
402 		errexit("Cannot allocate space for inode buffer\n");
403 	while (nextino < EXT2_ROOTINO)
404 		(void)getnextinode(nextino);
405 }
406 
407 void
408 freeinodebuf(void)
409 {
410 
411 	if (inodebuf != NULL)
412 		free((char *)inodebuf);
413 	inodebuf = NULL;
414 }
415 
416 /*
417  * Routines to maintain information about directory inodes.
418  * This is built during the first pass and used during the
419  * second and third passes.
420  *
421  * Enter inodes into the cache.
422  */
423 void
424 cacheino(struct ext2fs_dinode *dp, ino_t inumber)
425 {
426 	struct inoinfo *inp;
427 	struct inoinfo **inpp;
428 	unsigned int blks;
429 
430 	blks = howmany(inosize(dp), sblock.e2fs_bsize);
431 	if (blks > NDADDR)
432 		blks = NDADDR + NIADDR;
433 	inp = malloc(sizeof(*inp) + (blks - 1) * sizeof(daddr32_t));
434 	if (inp == NULL)
435 		return;
436 	inpp = &inphead[inumber % numdirs];
437 	inp->i_nexthash = *inpp;
438 	*inpp = inp;
439 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
440 	if (inumber == EXT2_ROOTINO)
441 		inp->i_parent = EXT2_ROOTINO;
442 	else
443 		inp->i_parent = (ino_t)0;
444 	inp->i_dotdot = (ino_t)0;
445 	inp->i_number = inumber;
446 	inp->i_isize = inosize(dp);
447 	inp->i_numblks = blks * sizeof(daddr32_t);
448 	memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
449 	if (inplast == listmax) {
450 		listmax += 100;
451 		inpsort = reallocarray(inpsort, listmax,
452 		    sizeof(struct inoinfo *));
453 		if (inpsort == NULL)
454 			errexit("cannot increase directory list\n");
455 	}
456 	inpsort[inplast++] = inp;
457 }
458 
459 /*
460  * Look up an inode cache structure.
461  */
462 struct inoinfo *
463 getinoinfo(ino_t inumber)
464 {
465 	struct inoinfo *inp;
466 
467 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
468 		if (inp->i_number != inumber)
469 			continue;
470 		return (inp);
471 	}
472 	errexit("cannot find inode %llu\n", (unsigned long long)inumber);
473 	return (NULL);
474 }
475 
476 /*
477  * Clean up all the inode cache structure.
478  */
479 void
480 inocleanup(void)
481 {
482 	struct inoinfo **inpp;
483 
484 	if (inphead == NULL)
485 		return;
486 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
487 		free((char *)(*inpp));
488 	free((char *)inphead);
489 	free((char *)inpsort);
490 	inphead = inpsort = NULL;
491 }
492 
493 void
494 inodirty(void)
495 {
496 
497 	dirty(pbp);
498 }
499 
500 void
501 clri(struct inodesc *idesc, char *type, int flag)
502 {
503 	struct ext2fs_dinode *dp;
504 
505 	dp = ginode(idesc->id_number);
506 	if (flag == 1) {
507 		pwarn("%s %s", type,
508 		    (dp->e2di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
509 		pinode(idesc->id_number);
510 	}
511 	if (preen || reply("CLEAR") == 1) {
512 		if (preen)
513 			printf(" (CLEARED)\n");
514 		n_files--;
515 		(void)ckinode(dp, idesc);
516 		clearinode(dp);
517 		statemap[idesc->id_number] = USTATE;
518 		inodirty();
519 	}
520 }
521 
522 int
523 findname(struct inodesc *idesc)
524 {
525 	struct ext2fs_direct *dirp = idesc->id_dirp;
526 	u_int16_t namlen = dirp->e2d_namlen;
527 
528 	if (letoh32(dirp->e2d_ino) != idesc->id_parent)
529 		return (KEEPON);
530 	memcpy(idesc->id_name, dirp->e2d_name, (size_t)namlen);
531 	idesc->id_name[namlen] = '\0';
532 	return (STOP|FOUND);
533 }
534 
535 int
536 findino(struct inodesc *idesc)
537 {
538 	struct ext2fs_direct *dirp = idesc->id_dirp;
539 	u_int32_t ino = letoh32(dirp->e2d_ino);
540 
541 	if (ino == 0)
542 		return (KEEPON);
543 	if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
544 	    (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
545 		&& ino <= maxino) {
546 		idesc->id_parent = ino;
547 		return (STOP|FOUND);
548 	}
549 	return (KEEPON);
550 }
551 
552 void
553 pinode(ino_t ino)
554 {
555 	struct ext2fs_dinode *dp;
556 	char *p;
557 	struct passwd *pw;
558 	time_t t;
559 	u_int32_t uid;
560 
561 	printf(" I=%llu ", (unsigned long long)ino);
562 	if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
563 		return;
564 	dp = ginode(ino);
565 	printf(" OWNER=");
566 	uid = letoh16(dp->e2di_uid_low) | (letoh16(dp->e2di_uid_high) << 16);
567 #ifndef SMALL
568 	if ((pw = getpwuid((int)uid)) != 0)
569 		printf("%s ", pw->pw_name);
570 	else
571 #endif
572 		printf("%u ", (unsigned)uid);
573 	printf("MODE=%o\n", letoh16(dp->e2di_mode));
574 	if (preen)
575 		printf("%s: ", cdevname());
576 	printf("SIZE=%llu ", (long long)inosize(dp));
577 	t = (time_t) letoh32(dp->e2di_mtime);
578 	p = ctime(&t);
579 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
580 }
581 
582 void
583 blkerror(ino_t ino, char *type, daddr32_t blk)
584 {
585 
586 	pfatal("%d %s I=%llu", blk, type, (unsigned long long)ino);
587 	printf("\n");
588 	switch (statemap[ino]) {
589 
590 	case FSTATE:
591 		statemap[ino] = FCLEAR;
592 		return;
593 
594 	case DSTATE:
595 		statemap[ino] = DCLEAR;
596 		return;
597 
598 	case FCLEAR:
599 	case DCLEAR:
600 		return;
601 
602 	default:
603 		errexit("BAD STATE %d TO BLKERR\n", statemap[ino]);
604 		/* NOTREACHED */
605 	}
606 }
607 
608 /*
609  * allocate an unused inode
610  */
611 ino_t
612 allocino(ino_t request, int type)
613 {
614 	ino_t ino;
615 	struct ext2fs_dinode *dp;
616 	time_t t;
617 
618 	if (request == 0)
619 		request = EXT2_ROOTINO;
620 	else if (statemap[request] != USTATE)
621 		return (0);
622 	for (ino = request; ino < maxino; ino++) {
623 		if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
624 			continue;
625 		if (statemap[ino] == USTATE)
626 			break;
627 	}
628 	if (ino == maxino)
629 		return (0);
630 	switch (type & IFMT) {
631 	case IFDIR:
632 		statemap[ino] = DSTATE;
633 		break;
634 	case IFREG:
635 	case IFLNK:
636 		statemap[ino] = FSTATE;
637 		break;
638 	default:
639 		return (0);
640 	}
641 	dp = ginode(ino);
642 	dp->e2di_blocks[0] = htole32(allocblk());
643 	if (dp->e2di_blocks[0] == 0) {
644 		statemap[ino] = USTATE;
645 		return (0);
646 	}
647 	dp->e2di_mode = htole16(type);
648 	(void)time(&t);
649 	dp->e2di_atime = (u_int32_t)htole32(t);
650 	dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
651 	dp->e2di_dtime = 0;
652 	inossize(dp, sblock.e2fs_bsize);
653 	dp->e2di_nblock = htole32(btodb(sblock.e2fs_bsize));
654 	n_files++;
655 	inodirty();
656 	typemap[ino] = E2IFTODT(type);
657 	return (ino);
658 }
659 
660 /*
661  * deallocate an inode
662  */
663 void
664 freeino(ino_t ino)
665 {
666 	struct inodesc idesc;
667 	struct ext2fs_dinode *dp;
668 
669 	memset(&idesc, 0, sizeof(struct inodesc));
670 	idesc.id_type = ADDR;
671 	idesc.id_func = pass4check;
672 	idesc.id_number = ino;
673 	dp = ginode(ino);
674 	(void)ckinode(dp, &idesc);
675 	clearinode(dp);
676 	inodirty();
677 	statemap[ino] = USTATE;
678 	n_files--;
679 }
680