xref: /openbsd-src/sbin/fsck_ffs/inode.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: inode.c,v 1.42 2014/05/21 18:53:05 krw Exp $	*/
2 /*	$NetBSD: inode.c,v 1.23 1996/10/11 20:15:47 thorpej 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>
34 #include <sys/time.h>
35 #include <ufs/ufs/dinode.h>
36 #include <ufs/ufs/dir.h>
37 #include <ufs/ffs/fs.h>
38 #ifndef SMALL
39 #include <pwd.h>
40 #endif
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "fsck.h"
47 #include "fsutil.h"
48 #include "extern.h"
49 
50 static ino_t startinum;
51 
52 static int iblock(struct inodesc *, long, off_t);
53 
54 int
55 ckinode(union dinode *dp, struct inodesc *idesc)
56 {
57 	long ret, ndb, offset;
58 	union dinode dino;
59 	off_t sizepb, remsize;
60 	mode_t mode;
61 	int i;
62 	char pathbuf[MAXPATHLEN + 1];
63 
64 	if (idesc->id_fix != IGNORE)
65 		idesc->id_fix = DONTKNOW;
66 	idesc->id_entryno = 0;
67 	idesc->id_filesize = DIP(dp, di_size);
68 	mode = DIP(dp, di_mode) & IFMT;
69 	if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
70 	    (DIP(dp, di_size) < sblock.fs_maxsymlinklen ||
71 	     (sblock.fs_maxsymlinklen == 0 && DIP(dp, di_blocks) == 0))))
72 		return (KEEPON);
73 	if (sblock.fs_magic == FS_UFS1_MAGIC)
74 		dino.dp1 = dp->dp1;
75 	else
76 		dino.dp2 = dp->dp2;
77 	ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize);
78 	for (i = 0; i < NDADDR; i++) {
79 		if (--ndb == 0 && (offset = blkoff(&sblock,
80 		    DIP(&dino, di_size))) != 0)
81 			idesc->id_numfrags =
82 				numfrags(&sblock, fragroundup(&sblock, offset));
83 		else
84 			idesc->id_numfrags = sblock.fs_frag;
85 		if (DIP(&dino, di_db[i]) == 0) {
86 			if (idesc->id_type == DATA && ndb >= 0) {
87 				/* An empty block in a directory XXX */
88 				getpathname(pathbuf, sizeof pathbuf,
89 				    idesc->id_number, idesc->id_number);
90 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
91 				    pathbuf);
92 				if (reply("ADJUST LENGTH") == 1) {
93 					dp = ginode(idesc->id_number);
94 					DIP_SET(dp, di_size,
95 					    i * sblock.fs_bsize);
96 					printf(
97 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
98 					rerun = 1;
99 					inodirty();
100 				}
101 			}
102 			continue;
103 		}
104 		idesc->id_blkno = DIP(&dino, di_db[i]);
105 		if (idesc->id_type == ADDR)
106 			ret = (*idesc->id_func)(idesc);
107 		else
108 			ret = dirscan(idesc);
109 		if (ret & STOP)
110 			return (ret);
111 	}
112 	idesc->id_numfrags = sblock.fs_frag;
113 	remsize = DIP(&dino, di_size) - sblock.fs_bsize * NDADDR;
114 	sizepb = sblock.fs_bsize;
115 	for (i = 0; i < NIADDR; i++) {
116 		if (DIP(&dino, di_ib[i])) {
117 			idesc->id_blkno = DIP(&dino, di_ib[i]);
118 			ret = iblock(idesc, i + 1, remsize);
119 			if (ret & STOP)
120 				return (ret);
121 		} else {
122 			if (idesc->id_type == DATA && remsize > 0) {
123 				/* An empty block in a directory XXX */
124 				getpathname(pathbuf, sizeof pathbuf,
125 				    idesc->id_number, idesc->id_number);
126 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
127 				    pathbuf);
128 				if (reply("ADJUST LENGTH") == 1) {
129 					dp = ginode(idesc->id_number);
130 					DIP_SET(dp, di_size,
131 					     DIP(dp, di_size) - remsize);
132 					remsize = 0;
133 					printf(
134 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
135 					rerun = 1;
136 					inodirty();
137 					break;
138 				}
139 			}
140 		}
141 		sizepb *= NINDIR(&sblock);
142 		remsize -= sizepb;
143 	}
144 	return (KEEPON);
145 }
146 
147 static int
148 iblock(struct inodesc *idesc, long ilevel, off_t isize)
149 {
150 	struct bufarea *bp;
151 	int i, n, (*func)(struct inodesc *), nif;
152 	off_t sizepb;
153 	char buf[BUFSIZ];
154 	char pathbuf[MAXPATHLEN + 1];
155 	union dinode *dp;
156 
157 	if (idesc->id_type == ADDR) {
158 		func = idesc->id_func;
159 		if (((n = (*func)(idesc)) & KEEPON) == 0)
160 			return (n);
161 	} else
162 		func = dirscan;
163 	if (isize < 0 || chkrange(idesc->id_blkno, idesc->id_numfrags))
164 		return (SKIP);
165 	bp = getdatablk(idesc->id_blkno, sblock.fs_bsize);
166 	ilevel--;
167 	for (sizepb = sblock.fs_bsize, i = 0; i < ilevel; i++)
168 		sizepb *= NINDIR(&sblock);
169 	if (howmany(isize, sizepb) > NINDIR(&sblock))
170 		nif = NINDIR(&sblock);
171 	else
172 		nif = howmany(isize, sizepb);
173 	if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
174 		for (i = nif; i < NINDIR(&sblock); i++) {
175 			if (IBLK(bp, i) == 0)
176 				continue;
177 			(void)snprintf(buf, sizeof buf,
178 			    "PARTIALLY TRUNCATED INODE I=%llu",
179 			    (unsigned long long)idesc->id_number);
180 			if (preen)
181 				pfatal("%s", buf);
182 			else if (dofix(idesc, buf)) {
183 				IBLK_SET(bp, i, 0);
184 				dirty(bp);
185 			}
186 		}
187 		flush(fswritefd, bp);
188 	}
189 	for (i = 0; i < nif; i++) {
190 		if (IBLK(bp, i)) {
191 			idesc->id_blkno = IBLK(bp, i);
192 			if (ilevel == 0)
193 				n = (*func)(idesc);
194 			else
195 				n = iblock(idesc, ilevel, isize);
196 			if (n & STOP) {
197 				bp->b_flags &= ~B_INUSE;
198 				return (n);
199 			}
200 		} else {
201 			if (idesc->id_type == DATA && isize > 0) {
202 				/* An empty block in a directory XXX */
203 				getpathname(pathbuf, sizeof pathbuf,
204 				    idesc->id_number, idesc->id_number);
205 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
206 				    pathbuf);
207 				if (reply("ADJUST LENGTH") == 1) {
208 					dp = ginode(idesc->id_number);
209 					DIP_SET(dp, di_size,
210 					    DIP(dp, di_size) - isize);
211 					isize = 0;
212 					printf(
213 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
214 					rerun = 1;
215 					inodirty();
216 					bp->b_flags &= ~B_INUSE;
217 					return(STOP);
218 				}
219 			}
220 		}
221 		isize -= sizepb;
222 	}
223 	bp->b_flags &= ~B_INUSE;
224 	return (KEEPON);
225 }
226 
227 /*
228  * Check that a block in a legal block number.
229  * Return 0 if in range, 1 if out of range.
230  */
231 int
232 chkrange(daddr_t blk, int cnt)
233 {
234 	int c;
235 
236 	if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
237 	    cnt - 1 > maxfsblock - blk)
238 		return (1);
239 	if (cnt > sblock.fs_frag ||
240 	    fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
241 		if (debug)
242 			printf("bad size: blk %lld, offset %lld, size %d\n",
243 			    (long long)blk, (long long)fragnum(&sblock, blk),
244 			    cnt);
245 		return (1);
246 	}
247 	c = dtog(&sblock, blk);
248 	if (blk < cgdmin(&sblock, c)) {
249 		if ((blk + cnt) > cgsblock(&sblock, c)) {
250 			if (debug) {
251 				printf("blk %lld < cgdmin %lld;",
252 				    (long long)blk,
253 				    (long long)cgdmin(&sblock, c));
254 				printf(" blk + cnt %lld > cgsbase %lld\n",
255 				    (long long)(blk + cnt),
256 				    (long long)cgsblock(&sblock, c));
257 			}
258 			return (1);
259 		}
260 	} else {
261 		if ((blk + cnt) > cgbase(&sblock, c+1)) {
262 			if (debug)  {
263 				printf("blk %lld >= cgdmin %lld;",
264 				    (long long)blk,
265 				    (long long)cgdmin(&sblock, c));
266 				printf(" blk + cnt %lld > sblock.fs_fpg %d\n",
267 				    (long long)(blk+cnt), sblock.fs_fpg);
268 			}
269 			return (1);
270 		}
271 	}
272 	return (0);
273 }
274 
275 /*
276  * General purpose interface for reading inodes.
277  */
278 union dinode *
279 ginode(ino_t inumber)
280 {
281 	daddr_t iblk;
282 
283 	if (inumber < ROOTINO || inumber > maxino)
284 		errexit("bad inode number %llu to ginode\n",
285 		    (unsigned long long)inumber);
286 	if (startinum == 0 ||
287 	    inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
288 		iblk = ino_to_fsba(&sblock, inumber);
289 		if (pbp != 0)
290 			pbp->b_flags &= ~B_INUSE;
291 		pbp = getdatablk(iblk, sblock.fs_bsize);
292 		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
293 	}
294 	if (sblock.fs_magic == FS_UFS1_MAGIC)
295 		return ((union dinode *)
296 		    &pbp->b_un.b_dinode1[inumber % INOPB(&sblock)]);
297 	return ((union dinode *)&pbp->b_un.b_dinode2[inumber % INOPB(&sblock)]);
298 }
299 
300 /*
301  * Special purpose version of ginode used to optimize first pass
302  * over all the inodes in numerical order.
303  */
304 ino_t nextino, lastinum;
305 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
306 static caddr_t inodebuf;
307 
308 union dinode *
309 getnextinode(ino_t inumber)
310 {
311 	long size;
312 	daddr_t dblk;
313 	union dinode *dp;
314 	static caddr_t nextinop;
315 
316 	if (inumber != nextino++ || inumber > maxino)
317 		errexit("bad inode number %llu to nextinode %llu\n",
318 		    (unsigned long long)inumber,
319 		    (unsigned long long)nextino);
320 	if (inumber >= lastinum) {
321 		readcnt++;
322 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
323 		if (readcnt % readpercg == 0) {
324 			size = partialsize;
325 			lastinum += partialcnt;
326 		} else {
327 			size = inobufsize;
328 			lastinum += fullcnt;
329 		}
330 		(void)bread(fsreadfd, inodebuf, dblk, size);
331 		nextinop = inodebuf;
332 	}
333 	dp = (union dinode *)nextinop;
334 	if (sblock.fs_magic == FS_UFS1_MAGIC)
335 		nextinop += sizeof(struct ufs1_dinode);
336 	else
337 		nextinop += sizeof(struct ufs2_dinode);
338 	return (dp);
339 }
340 
341 void
342 setinodebuf(ino_t inum)
343 {
344 
345 	startinum = 0;
346 	nextino = inum;
347 	lastinum = inum;
348 	readcnt = 0;
349 	if (inodebuf != NULL)
350 		return;
351 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
352 	if (sblock.fs_magic == FS_UFS1_MAGIC)
353 		fullcnt = inobufsize / sizeof(struct ufs1_dinode);
354 	else
355 		fullcnt = inobufsize / sizeof(struct ufs2_dinode);
356 	readpercg = sblock.fs_ipg / fullcnt;
357 	partialcnt = sblock.fs_ipg % fullcnt;
358 	if (sblock.fs_magic == FS_UFS1_MAGIC)
359 		partialsize = partialcnt * sizeof(struct ufs1_dinode);
360 	else
361 		partialsize = partialcnt * sizeof(struct ufs2_dinode);
362 	if (partialcnt != 0) {
363 		readpercg++;
364 	} else {
365 		partialcnt = fullcnt;
366 		partialsize = inobufsize;
367 	}
368 	if (inodebuf == NULL &&
369 	    (inodebuf = malloc((unsigned)inobufsize)) == NULL)
370 		errexit("Cannot allocate space for inode buffer\n");
371 }
372 
373 void
374 freeinodebuf(void)
375 {
376 
377 	if (inodebuf != NULL)
378 		free(inodebuf);
379 	inodebuf = NULL;
380 }
381 
382 /*
383  * Routines to maintain information about directory inodes.
384  * This is built during the first pass and used during the
385  * second and third passes.
386  *
387  * Enter inodes into the cache.
388  */
389 void
390 cacheino(union dinode *dp, ino_t inumber)
391 {
392 	struct inoinfo *inp;
393 	struct inoinfo **inpp, **newinpsort;
394 	unsigned int blks;
395 	long newlistmax;
396 	int i;
397 
398 	blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
399 	if (blks > NDADDR)
400 		blks = NDADDR + NIADDR;
401 	inp = malloc(sizeof(*inp) + (blks ? blks - 1 : 0) * sizeof(daddr_t));
402 	if (inp == NULL)
403 		errexit("cannot allocate memory for inode cache\n");
404 	inpp = &inphead[inumber % numdirs];
405 	inp->i_nexthash = *inpp;
406 	*inpp = inp;
407 	inp->i_child = inp->i_sibling = 0;
408 	if (inumber == ROOTINO)
409 		inp->i_parent = ROOTINO;
410 	else
411 		inp->i_parent = 0;
412 	inp->i_dotdot = 0;
413 	inp->i_number = inumber;
414 	inp->i_isize = DIP(dp, di_size);
415 	inp->i_numblks = blks;
416 	for (i = 0; i < (blks < NDADDR ? blks : NDADDR); i++)
417 		inp->i_blks[i] = DIP(dp, di_db[i]);
418 	if (blks > NDADDR)
419 		for (i = 0; i < NIADDR; i++)
420 			inp->i_blks[NDADDR + i] = DIP(dp, di_ib[i]);
421 	if (inplast == listmax) {
422 		newlistmax = listmax + 100;
423 		newinpsort = realloc(inpsort,
424 		    (unsigned)newlistmax * sizeof(struct inoinfo *));
425 		if (newinpsort == NULL)
426 			errexit("cannot increase directory list");
427 		inpsort = newinpsort;
428 		listmax = newlistmax;
429 	}
430 	inpsort[inplast++] = inp;
431 }
432 
433 /*
434  * Look up an inode cache structure.
435  */
436 struct inoinfo *
437 getinoinfo(ino_t inumber)
438 {
439 	struct inoinfo *inp;
440 
441 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
442 		if (inp->i_number != inumber)
443 			continue;
444 		return (inp);
445 	}
446 	errexit("cannot find inode %llu\n", (unsigned long long)inumber);
447 	return (NULL);
448 }
449 
450 /*
451  * Clean up all the inode cache structure.
452  */
453 void
454 inocleanup(void)
455 {
456 	struct inoinfo **inpp;
457 
458 	if (inphead == NULL)
459 		return;
460 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
461 		free(*inpp);
462 	free(inphead);
463 	free(inpsort);
464 	inphead = inpsort = NULL;
465 }
466 
467 void
468 inodirty(void)
469 {
470 	dirty(pbp);
471 }
472 
473 void
474 clri(struct inodesc *idesc, char *type, int flag)
475 {
476 	union dinode *dp;
477 
478 	dp = ginode(idesc->id_number);
479 	if (flag == 1) {
480 		pwarn("%s %s", type,
481 		    (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
482 		pinode(idesc->id_number);
483 	}
484 	if (preen || reply("CLEAR") == 1) {
485 		if (preen)
486 			printf(" (CLEARED)\n");
487 		n_files--;
488 		(void)ckinode(dp, idesc);
489 		clearinode(dp);
490 		SET_ISTATE(idesc->id_number, USTATE);
491 		inodirty();
492 	}
493 }
494 
495 int
496 findname(struct inodesc *idesc)
497 {
498 	struct direct *dirp = idesc->id_dirp;
499 
500 	if (dirp->d_ino != idesc->id_parent)
501 		return (KEEPON);
502 	memcpy(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
503 	return (STOP|FOUND);
504 }
505 
506 int
507 findino(struct inodesc *idesc)
508 {
509 	struct direct *dirp = idesc->id_dirp;
510 
511 	if (dirp->d_ino == 0)
512 		return (KEEPON);
513 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
514 	    dirp->d_ino >= ROOTINO && dirp->d_ino <= maxino) {
515 		idesc->id_parent = dirp->d_ino;
516 		return (STOP|FOUND);
517 	}
518 	return (KEEPON);
519 }
520 
521 void
522 pinode(ino_t ino)
523 {
524 	union dinode *dp;
525 	char *p;
526 	struct passwd *pw;
527 	time_t t;
528 
529 	printf(" I=%llu ", (unsigned long long)ino);
530 	if (ino < ROOTINO || ino > maxino)
531 		return;
532 	dp = ginode(ino);
533 	printf(" OWNER=");
534 #ifndef SMALL
535 	if ((pw = getpwuid(DIP(dp, di_uid))) != 0)
536 		printf("%s ", pw->pw_name);
537 	else
538 #endif
539 		printf("%u ", (unsigned)DIP(dp, di_uid));
540 	printf("MODE=%o\n", DIP(dp, di_mode));
541 	if (preen)
542 		printf("%s: ", cdevname());
543 	printf("SIZE=%llu ", (unsigned long long)DIP(dp, di_size));
544 	t = DIP(dp, di_mtime);
545 	p = ctime(&t);
546 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
547 }
548 
549 void
550 blkerror(ino_t ino, char *type, daddr_t blk)
551 {
552 
553 	pfatal("%lld %s I=%llu", blk, type, (unsigned long long)ino);
554 	printf("\n");
555 	switch (GET_ISTATE(ino)) {
556 
557 	case FSTATE:
558 		SET_ISTATE(ino, FCLEAR);
559 		return;
560 
561 	case DSTATE:
562 		SET_ISTATE(ino, DCLEAR);
563 		return;
564 
565 	case FCLEAR:
566 	case DCLEAR:
567 		return;
568 
569 	default:
570 		errexit("BAD STATE %d TO BLKERR\n", GET_ISTATE(ino));
571 		/* NOTREACHED */
572 	}
573 }
574 
575 /*
576  * allocate an unused inode
577  */
578 ino_t
579 allocino(ino_t request, int type)
580 {
581 	ino_t ino;
582 	union dinode *dp;
583 	struct cg *cgp = &cgrp;
584 	int cg;
585 	time_t t;
586 	struct inostat *info;
587 
588 	if (request == 0)
589 		request = ROOTINO;
590 	else if (GET_ISTATE(request) != USTATE)
591 		return (0);
592 	for (ino = request; ino < maxino; ino++)
593 		if (GET_ISTATE(ino) == USTATE)
594 			break;
595 	if (ino == maxino)
596 		return (0);
597 	cg = ino_to_cg(&sblock, ino);
598 	/* If necessary, extend the inoinfo array. grow exponentially */
599 	if ((ino % sblock.fs_ipg) >= (uint64_t)inostathead[cg].il_numalloced) {
600 		unsigned long newalloced, i;
601 		newalloced = MIN(sblock.fs_ipg,
602 			MAX(2 * inostathead[cg].il_numalloced, 10));
603 		info = calloc(newalloced, sizeof(struct inostat));
604 		if (info == NULL) {
605 			pwarn("cannot alloc %lu bytes to extend inoinfo\n",
606 				sizeof(struct inostat) * newalloced);
607 			return 0;
608 		}
609 		memmove(info, inostathead[cg].il_stat,
610 			inostathead[cg].il_numalloced * sizeof(*info));
611 		for (i = inostathead[cg].il_numalloced; i < newalloced; i++) {
612 			info[i].ino_state = USTATE;
613 		}
614 		if (inostathead[cg].il_numalloced)
615 			free(inostathead[cg].il_stat);
616 		inostathead[cg].il_stat = info;
617 		inostathead[cg].il_numalloced = newalloced;
618 		info = inoinfo(ino);
619 	}
620 	getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
621 	if (!cg_chkmagic(cgp))
622 		pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
623 	setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
624 	cgp->cg_cs.cs_nifree--;
625 
626 	switch (type & IFMT) {
627 	case IFDIR:
628 		SET_ISTATE(ino, DSTATE);
629 		cgp->cg_cs.cs_ndir++;
630 		break;
631 	case IFREG:
632 	case IFLNK:
633 		SET_ISTATE(ino, FSTATE);
634 		break;
635 	default:
636 		return (0);
637 	}
638 	cgdirty();
639 	dp = ginode(ino);
640 	DIP_SET(dp, di_db[0],  allocblk(1));
641 	if (DIP(dp, di_db[0]) == 0) {
642 		SET_ISTATE(ino, USTATE);
643 		return (0);
644 	}
645 	DIP_SET(dp, di_mode, type);
646 	DIP_SET(dp, di_uid, geteuid());
647 	DIP_SET(dp, di_gid, getegid());
648 	DIP_SET(dp, di_flags, 0);
649 	(void)time(&t);
650 	DIP_SET(dp, di_atime, t);
651 	DIP_SET(dp, di_atimensec, 0);
652 	DIP_SET(dp, di_mtime, t);
653 	DIP_SET(dp, di_mtimensec, 0);
654 	DIP_SET(dp, di_ctime, t);
655 	DIP_SET(dp, di_ctimensec, 0);
656 	DIP_SET(dp, di_size, sblock.fs_fsize);
657 	DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
658 	n_files++;
659 	inodirty();
660 	SET_ITYPE(ino, IFTODT(type));
661 	return (ino);
662 }
663 
664 /*
665  * deallocate an inode
666  */
667 void
668 freeino(ino_t ino)
669 {
670 	struct inodesc idesc;
671 	union dinode *dp;
672 
673 	memset(&idesc, 0, sizeof(struct inodesc));
674 	idesc.id_type = ADDR;
675 	idesc.id_func = pass4check;
676 	idesc.id_number = ino;
677 	dp = ginode(ino);
678 	(void)ckinode(dp, &idesc);
679 	clearinode(dp);
680 	inodirty();
681 	SET_ISTATE(ino, USTATE);
682 	n_files--;
683 }
684