2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static const char sccsid[] = "@(#)inode.c 8.8 (Berkeley) 4/28/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/stdint.h>
42 #include <sys/sysctl.h>
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 #include <ufs/ffs/fs.h>
56 struct bufarea *icachebp; /* inode cache buffer */
58 static int iblock(struct inodesc *, off_t isize, int type);
59 static ufs2_daddr_t indir_blkatoff(ufs2_daddr_t, ino_t, ufs_lbn_t, ufs_lbn_t,
63 ckinode(union dinode *dp, struct inodesc *idesc)
65 off_t remsize, sizepb;
71 char pathbuf[MAXPATHLEN + 1];
73 if (idesc->id_fix != IGNORE)
74 idesc->id_fix = DONTKNOW;
77 idesc->id_lballoc = -1;
79 idesc->id_entryno = 0;
80 idesc->id_filesize = DIP(dp, di_size);
81 mode = DIP(dp, di_mode) & IFMT;
82 if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
83 DIP(dp, di_size) < (unsigned)sblock.fs_maxsymlinklen))
85 if (sblock.fs_magic == FS_UFS1_MAGIC)
89 ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize);
90 for (i = 0; i < UFS_NDADDR; i++) {
93 (offset = blkoff(&sblock, DIP(&dino, di_size))) != 0)
95 numfrags(&sblock, fragroundup(&sblock, offset));
97 idesc->id_numfrags = sblock.fs_frag;
98 if (DIP(&dino, di_db[i]) == 0) {
99 if (idesc->id_type == DATA && ndb >= 0) {
100 /* An empty block in a directory XXX */
101 getpathname(pathbuf, idesc->id_number,
103 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
105 if (reply("ADJUST LENGTH") == 1) {
106 ginode(idesc->id_number, &ip);
107 DIP_SET(ip.i_dp, di_size,
108 i * sblock.fs_bsize);
110 "YOU MUST RERUN FSCK AFTERWARDS\n");
118 idesc->id_blkno = DIP(&dino, di_db[i]);
119 if (idesc->id_type != DATA)
120 ret = (*idesc->id_func)(idesc);
122 ret = dirscan(idesc);
126 idesc->id_numfrags = sblock.fs_frag;
127 remsize = DIP(&dino, di_size) - sblock.fs_bsize * UFS_NDADDR;
128 sizepb = sblock.fs_bsize;
129 for (i = 0; i < UFS_NIADDR; i++) {
130 sizepb *= NINDIR(&sblock);
131 idesc->id_level = i + 1;
132 if (DIP(&dino, di_ib[i])) {
133 idesc->id_blkno = DIP(&dino, di_ib[i]);
134 ret = iblock(idesc, remsize, BT_LEVEL1 + i);
137 } else if (remsize > 0) {
138 idesc->id_lbn += sizepb / sblock.fs_bsize;
139 if (idesc->id_type == DATA) {
140 /* An empty block in a directory XXX */
141 getpathname(pathbuf, idesc->id_number,
143 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
145 if (reply("ADJUST LENGTH") == 1) {
146 ginode(idesc->id_number, &ip);
147 DIP_SET(ip.i_dp, di_size,
148 DIP(ip.i_dp, di_size) - remsize);
151 "YOU MUST RERUN FSCK AFTERWARDS\n");
165 iblock(struct inodesc *idesc, off_t isize, int type)
169 int i, n, (*func)(struct inodesc *), nif;
172 char pathbuf[MAXPATHLEN + 1];
174 if (idesc->id_type != DATA) {
175 func = idesc->id_func;
176 if (((n = (*func)(idesc)) & KEEPON) == 0)
180 bp = getdatablk(idesc->id_blkno, sblock.fs_bsize, type);
181 if (bp->b_errs != 0) {
187 for (sizepb = sblock.fs_bsize, i = 0; i < idesc->id_level; i++)
188 sizepb *= NINDIR(&sblock);
189 if (howmany(isize, sizepb) > NINDIR(&sblock))
190 nif = NINDIR(&sblock);
192 nif = howmany(isize, sizepb);
193 if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
194 for (i = nif; i < NINDIR(&sblock); i++) {
195 if (IBLK(bp, i) == 0)
197 (void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
198 (u_long)idesc->id_number);
201 } else if (dofix(idesc, buf)) {
206 flush(fswritefd, bp);
208 for (i = 0; i < nif; i++) {
210 idesc->id_blkno = IBLK(bp, i);
212 if (idesc->id_level == 0) {
216 n = iblock(idesc, isize, type);
224 idesc->id_lbn += sizepb / sblock.fs_bsize;
225 if (idesc->id_type == DATA && isize > 0) {
226 /* An empty block in a directory XXX */
227 getpathname(pathbuf, idesc->id_number,
229 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
231 if (reply("ADJUST LENGTH") == 1) {
232 ginode(idesc->id_number, &ip);
233 DIP_SET(ip.i_dp, di_size,
234 DIP(ip.i_dp, di_size) - isize);
237 "YOU MUST RERUN FSCK AFTERWARDS\n");
252 * Finds the disk block address at the specified lbn within the inode
253 * specified by dp. This follows the whole tree and honors di_size and
254 * di_extsize so it is a true test of reachability. The lbn may be
255 * negative if an extattr or indirect block is requested.
258 ino_blkatoff(union dinode *dp, ino_t ino, ufs_lbn_t lbn, int *frags,
259 struct bufarea **bpp)
270 * Handle extattr blocks first.
272 if (lbn < 0 && lbn >= -UFS_NXADDR) {
274 if (lbn > lblkno(&sblock, dp->dp2.di_extsize - 1))
276 *frags = numfrags(&sblock,
277 sblksize(&sblock, dp->dp2.di_extsize, lbn));
278 return (dp->dp2.di_extb[lbn]);
281 * Now direct and indirect.
283 if (DIP(dp, di_mode) == IFLNK &&
284 DIP(dp, di_size) < sblock.fs_maxsymlinklen)
286 if (lbn >= 0 && lbn < UFS_NDADDR) {
287 *frags = numfrags(&sblock,
288 sblksize(&sblock, DIP(dp, di_size), lbn));
289 return (DIP(dp, di_db[lbn]));
291 *frags = sblock.fs_frag;
293 for (i = 0, tmpval = NINDIR(&sblock), cur = UFS_NDADDR; i < UFS_NIADDR;
294 i++, tmpval *= NINDIR(&sblock), cur = next) {
297 return (DIP(dp, di_ib[i]));
299 * Determine whether the lbn in question is within this tree.
301 if (lbn < 0 && -lbn >= next)
303 if (lbn > 0 && lbn >= next)
305 if (DIP(dp, di_ib[i]) == 0)
307 return (indir_blkatoff(DIP(dp, di_ib[i]), ino, -cur - i, lbn,
310 pfatal("lbn %jd not in ino %ju\n", lbn, (uintmax_t)ino);
315 * Fetch an indirect block to find the block at a given lbn. The lbn
316 * may be negative to fetch a specific indirect block pointer or positive
317 * to fetch a specific block.
320 indir_blkatoff(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t cur, ufs_lbn_t lbn,
321 struct bufarea **bpp)
328 level = lbn_level(cur);
330 pfatal("Invalid indir lbn %jd in ino %ju\n",
331 lbn, (uintmax_t)ino);
332 if (level == 0 && lbn < 0)
333 pfatal("Invalid lbn %jd in ino %ju\n",
334 lbn, (uintmax_t)ino);
336 base = -(cur + level);
337 for (i = level; i > 0; i--)
338 lbnadd *= NINDIR(&sblock);
340 i = (lbn - base) / lbnadd;
342 i = (-lbn - base) / lbnadd;
343 if (i < 0 || i >= NINDIR(&sblock)) {
344 pfatal("Invalid indirect index %d produced by lbn %jd "
345 "in ino %ju\n", i, lbn, (uintmax_t)ino);
349 cur = base + (i * lbnadd);
351 cur = -(base + (i * lbnadd)) - (level - 1);
352 bp = getdatablk(blk, sblock.fs_bsize, BT_LEVEL1 + level);
357 if (cur == lbn || blk == 0) {
366 pfatal("Invalid lbn %jd at level 0 for ino %ju\n", lbn,
368 return (indir_blkatoff(blk, ino, cur, lbn, bpp));
372 * Check that a block in a legal block number.
373 * Return 0 if in range, 1 if out of range.
376 chkrange(ufs2_daddr_t blk, int cnt)
380 if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
381 cnt - 1 > maxfsblock - blk)
383 if (cnt > sblock.fs_frag ||
384 fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
386 printf("bad size: blk %ld, offset %i, size %d\n",
387 (long)blk, (int)fragnum(&sblock, blk), cnt);
390 c = dtog(&sblock, blk);
391 if (blk < cgdmin(&sblock, c)) {
392 if ((blk + cnt) > cgsblock(&sblock, c)) {
394 printf("blk %ld < cgdmin %ld;",
395 (long)blk, (long)cgdmin(&sblock, c));
396 printf(" blk + cnt %ld > cgsbase %ld\n",
398 (long)cgsblock(&sblock, c));
403 if ((blk + cnt) > cgbase(&sblock, c+1)) {
405 printf("blk %ld >= cgdmin %ld;",
406 (long)blk, (long)cgdmin(&sblock, c));
407 printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
408 (long)(blk + cnt), (long)sblock.fs_fpg);
417 * General purpose interface for reading inodes.
420 ginode(ino_t inumber, struct inode *ip)
424 if (inumber < UFS_ROOTINO || inumber > maxino)
425 errx(EEXIT, "bad inode number %ju to ginode",
427 ip->i_number = inumber;
428 if (icachebp != NULL &&
429 inumber >= icachebp->b_index &&
430 inumber < icachebp->b_index + INOPB(&sblock)) {
431 /* take an additional reference for the returned inode */
432 icachebp->b_refcnt++;
434 iblk = ino_to_fsba(&sblock, inumber);
435 /* release our cache-hold reference on old icachebp */
436 if (icachebp != NULL)
438 icachebp = getdatablk(iblk, sblock.fs_bsize, BT_INODES);
439 if (icachebp->b_errs != 0) {
445 /* take a cache-hold reference on new icachebp */
446 icachebp->b_refcnt++;
447 icachebp->b_index = rounddown(inumber, INOPB(&sblock));
450 if (sblock.fs_magic == FS_UFS1_MAGIC) {
451 ip->i_dp = (union dinode *)
452 &icachebp->b_un.b_dinode1[inumber % INOPB(&sblock)];
455 ip->i_dp = (union dinode *)
456 &icachebp->b_un.b_dinode2[inumber % INOPB(&sblock)];
457 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)ip->i_dp)) {
458 pwarn("INODE CHECK-HASH FAILED");
460 if (preen || reply("FIX") != 0) {
462 printf(" (FIXED)\n");
463 ffs_update_dinode_ckhash(&sblock,
464 (struct ufs2_dinode *)ip->i_dp);
471 * Release a held inode.
474 irelse(struct inode *ip)
477 if (ip->i_bp->b_refcnt <= 0)
478 pfatal("irelse: releasing unreferenced ino %ju\n",
479 (uintmax_t) ip->i_number);
484 * Special purpose version of ginode used to optimize first pass
485 * over all the inodes in numerical order.
487 static ino_t nextino, lastinum, lastvalidinum;
488 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
489 static struct bufarea inobuf;
492 getnextinode(ino_t inumber, int rebuildcg)
497 ufs2_daddr_t ndb, blk;
500 static caddr_t nextinop;
502 if (inumber != nextino++ || inumber > lastvalidinum)
503 errx(EEXIT, "bad inode number %ju to nextinode",
505 if (inumber >= lastinum) {
507 blk = ino_to_fsba(&sblock, lastinum);
508 if (readcount % readpercg == 0) {
510 lastinum += partialcnt;
516 * Flush old contents in case they have been updated.
517 * If getblk encounters an error, it will already have zeroed
518 * out the buffer, so we do not need to do so here.
520 flush(fswritefd, &inobuf);
521 getblk(&inobuf, blk, size);
522 nextinop = inobuf.b_un.b_buf;
524 dp = (union dinode *)nextinop;
525 if (sblock.fs_magic == FS_UFS1_MAGIC)
526 nextinop += sizeof(struct ufs1_dinode);
528 nextinop += sizeof(struct ufs2_dinode);
529 if ((ckhashadd & CK_INODE) != 0) {
530 ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp);
533 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) {
534 pwarn("INODE CHECK-HASH FAILED");
537 ip.i_number = inumber;
539 if (preen || reply("FIX") != 0) {
541 printf(" (FIXED)\n");
542 ffs_update_dinode_ckhash(&sblock,
543 (struct ufs2_dinode *)dp);
547 if (rebuildcg && (char *)dp == inobuf.b_un.b_buf) {
549 * Try to determine if we have reached the end of the
552 mode = DIP(dp, di_mode) & IFMT;
554 if (memcmp(dp->dp2.di_db, zino.dp2.di_db,
555 UFS_NDADDR * sizeof(ufs2_daddr_t)) ||
556 memcmp(dp->dp2.di_ib, zino.dp2.di_ib,
557 UFS_NIADDR * sizeof(ufs2_daddr_t)) ||
558 dp->dp2.di_mode || dp->dp2.di_size)
564 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
567 if (mode == IFBLK || mode == IFCHR)
571 * Fake ndb value so direct/indirect block checks below
572 * will detect any garbage after symlink string.
574 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
575 ndb = howmany(DIP(dp, di_size),
576 sizeof(ufs2_daddr_t));
577 if (ndb > UFS_NDADDR) {
578 j = ndb - UFS_NDADDR;
579 for (ndb = 1; j > 1; j--)
580 ndb *= NINDIR(&sblock);
585 for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++)
586 if (DIP(dp, di_db[j]) != 0)
588 for (j = 0, ndb -= UFS_NDADDR; ndb > 0; j++)
589 ndb /= NINDIR(&sblock);
590 for (; j < UFS_NIADDR; j++)
591 if (DIP(dp, di_ib[j]) != 0)
598 setinodebuf(int cg, ino_t inosused)
602 inum = cg * sblock.fs_ipg;
603 lastvalidinum = inum + inosused - 1;
607 /* Flush old contents in case they have been updated */
608 flush(fswritefd, &inobuf);
610 if (inobuf.b_un.b_buf == NULL) {
611 inobufsize = blkroundup(&sblock,
612 MAX(INOBUFSIZE, sblock.fs_bsize));
613 initbarea(&inobuf, BT_INODES);
614 if ((inobuf.b_un.b_buf = Malloc((unsigned)inobufsize)) == NULL)
615 errx(EEXIT, "cannot allocate space for inode buffer");
617 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
618 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
619 readpercg = inosused / fullcnt;
620 partialcnt = inosused % fullcnt;
621 partialsize = fragroundup(&sblock,
622 partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
623 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)));
624 if (partialcnt != 0) {
627 partialcnt = fullcnt;
628 partialsize = inobufsize;
633 freeblock(struct inodesc *idesc)
640 blkno = idesc->id_blkno;
641 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
642 if (chkrange(blkno, 1)) {
644 } else if (testbmap(blkno)) {
645 for (dlp = duplist; dlp; dlp = dlp->next) {
646 if (dlp->dup != blkno)
648 dlp->dup = duplist->dup;
650 duplist = duplist->next;
668 * Flush old contents in case they have been updated.
670 flush(fswritefd, &inobuf);
671 if (inobuf.b_un.b_buf != NULL)
672 free((char *)inobuf.b_un.b_buf);
673 inobuf.b_un.b_buf = NULL;
677 * Routines to maintain information about directory inodes.
678 * This is built during the first pass and used during the
679 * second and third passes.
681 * Enter inodes into the cache.
684 cacheino(union dinode *dp, ino_t inumber)
686 struct inoinfo *inp, **inpp;
689 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > UFS_NDADDR)
690 blks = UFS_NDADDR + UFS_NIADDR;
691 else if (DIP(dp, di_size) > 0)
692 blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
695 inp = (struct inoinfo *)
696 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
698 errx(EEXIT, "cannot increase directory list");
699 inpp = &inphead[inumber % dirhash];
700 inp->i_nexthash = *inpp;
702 inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ino_t)0;
703 inp->i_dotdot = (ino_t)0;
704 inp->i_number = inumber;
705 inp->i_isize = DIP(dp, di_size);
706 inp->i_numblks = blks;
707 for (i = 0; i < MIN(blks, UFS_NDADDR); i++)
708 inp->i_blks[i] = DIP(dp, di_db[i]);
709 if (blks > UFS_NDADDR)
710 for (i = 0; i < UFS_NIADDR; i++)
711 inp->i_blks[UFS_NDADDR + i] = DIP(dp, di_ib[i]);
712 if (inplast == listmax) {
714 inpsort = (struct inoinfo **)reallocarray((char *)inpsort,
715 listmax, sizeof(struct inoinfo *));
717 errx(EEXIT, "cannot increase directory list");
719 inpsort[inplast++] = inp;
723 * Look up an inode cache structure.
726 getinoinfo(ino_t inumber)
730 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
731 if (inp->i_number != inumber)
735 errx(EEXIT, "cannot find inode %ju", (uintmax_t)inumber);
736 return ((struct inoinfo *)0);
740 * Clean up all the inode cache structure.
745 struct inoinfo **inpp;
749 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
750 free((char *)(*inpp));
751 free((char *)inphead);
752 free((char *)inpsort);
753 inphead = inpsort = NULL;
757 inodirty(struct inode *ip)
760 if (sblock.fs_magic == FS_UFS2_MAGIC)
761 ffs_update_dinode_ckhash(&sblock,
762 (struct ufs2_dinode *)ip->i_dp);
767 clri(struct inodesc *idesc, const char *type, int flag)
772 ginode(idesc->id_number, &ip);
776 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
780 if (preen || reply("CLEAR") == 1) {
782 printf(" (CLEARED)\n");
784 if (bkgrdflag == 0) {
785 (void)ckinode(dp, idesc);
786 inoinfo(idesc->id_number)->ino_state = USTATE;
790 cmd.value = idesc->id_number;
791 cmd.size = -DIP(dp, di_nlink);
793 printf("adjrefcnt ino %ld amt %lld\n",
794 (long)cmd.value, (long long)cmd.size);
795 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
796 &cmd, sizeof cmd) == -1)
797 rwerror("ADJUST INODE", cmd.value);
804 findname(struct inodesc *idesc)
806 struct direct *dirp = idesc->id_dirp;
808 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
812 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
817 findino(struct inodesc *idesc)
819 struct direct *dirp = idesc->id_dirp;
821 if (dirp->d_ino == 0)
823 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
824 dirp->d_ino >= UFS_ROOTINO && dirp->d_ino <= maxino) {
825 idesc->id_parent = dirp->d_ino;
832 clearentry(struct inodesc *idesc)
834 struct direct *dirp = idesc->id_dirp;
836 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
841 return (STOP|FOUND|ALTERED);
845 prtinode(struct inode *ip)
853 printf(" I=%lu ", (u_long)ip->i_number);
854 if (ip->i_number < UFS_ROOTINO || ip->i_number > maxino)
857 if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL)
858 printf("%s ", pw->pw_name);
860 printf("%u ", (unsigned)DIP(dp, di_uid));
861 printf("MODE=%o\n", DIP(dp, di_mode));
863 printf("%s: ", cdevname);
864 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
865 t = DIP(dp, di_mtime);
867 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
871 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
874 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
876 switch (inoinfo(ino)->ino_state) {
880 inoinfo(ino)->ino_state = FCLEAR;
885 inoinfo(ino)->ino_state = DCLEAR;
893 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
899 * allocate an unused inode
902 allocino(ino_t request, int type)
907 struct bufarea *cgbp;
913 request = UFS_ROOTINO;
915 } else if (inoinfo(request)->ino_state != USTATE)
918 for (ino = request; ino < maxino; ino++)
919 if (inoinfo(ino)->ino_state == USTATE)
923 cg = ino_to_cg(&sblock, ino);
925 cgp = cgbp->b_un.b_cg;
926 if (!check_cgmagic(cg, cgbp, 0)) {
929 request = (cg + 1) * sblock.fs_ipg;
932 setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
933 cgp->cg_cs.cs_nifree--;
934 switch (type & IFMT) {
936 inoinfo(ino)->ino_state = DSTATE;
937 cgp->cg_cs.cs_ndir++;
941 inoinfo(ino)->ino_state = FSTATE;
949 DIP_SET(dp, di_db[0], allocblk((long)1));
950 if (DIP(dp, di_db[0]) == 0) {
951 inoinfo(ino)->ino_state = USTATE;
955 DIP_SET(dp, di_mode, type);
956 DIP_SET(dp, di_flags, 0);
957 DIP_SET(dp, di_atime, time(NULL));
958 DIP_SET(dp, di_ctime, DIP(dp, di_atime));
959 DIP_SET(dp, di_mtime, DIP(dp, di_ctime));
960 DIP_SET(dp, di_mtimensec, 0);
961 DIP_SET(dp, di_ctimensec, 0);
962 DIP_SET(dp, di_atimensec, 0);
963 DIP_SET(dp, di_size, sblock.fs_fsize);
964 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
968 inoinfo(ino)->ino_type = IFTODT(type);
973 * deallocate an inode
978 struct inodesc idesc;
982 memset(&idesc, 0, sizeof(struct inodesc));
983 idesc.id_type = inoinfo(ino)->ino_idtype;
984 idesc.id_func = freeblock;
985 idesc.id_number = ino;
988 (void)ckinode(dp, &idesc);
992 inoinfo(ino)->ino_state = USTATE;