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 static ino_t startinum;
58 static int iblock(struct inodesc *, off_t isize, int type);
61 ckinode(union dinode *dp, struct inodesc *idesc)
63 off_t remsize, sizepb;
68 char pathbuf[MAXPATHLEN + 1];
70 if (idesc->id_fix != IGNORE)
71 idesc->id_fix = DONTKNOW;
73 idesc->id_lballoc = -1;
75 idesc->id_entryno = 0;
76 idesc->id_filesize = DIP(dp, di_size);
77 mode = DIP(dp, di_mode) & IFMT;
78 if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
79 DIP(dp, di_size) < (unsigned)sblock.fs_maxsymlinklen))
81 if (sblock.fs_magic == FS_UFS1_MAGIC)
85 ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize);
86 for (i = 0; i < UFS_NDADDR; i++) {
89 (offset = blkoff(&sblock, DIP(&dino, di_size))) != 0)
91 numfrags(&sblock, fragroundup(&sblock, offset));
93 idesc->id_numfrags = sblock.fs_frag;
94 if (DIP(&dino, di_db[i]) == 0) {
95 if (idesc->id_type == DATA && ndb >= 0) {
96 /* An empty block in a directory XXX */
97 getpathname(pathbuf, idesc->id_number,
99 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
101 if (reply("ADJUST LENGTH") == 1) {
102 dp = ginode(idesc->id_number);
104 i * sblock.fs_bsize);
106 "YOU MUST RERUN FSCK AFTERWARDS\n");
114 idesc->id_blkno = DIP(&dino, di_db[i]);
115 if (idesc->id_type != DATA)
116 ret = (*idesc->id_func)(idesc);
118 ret = dirscan(idesc);
122 idesc->id_numfrags = sblock.fs_frag;
123 remsize = DIP(&dino, di_size) - sblock.fs_bsize * UFS_NDADDR;
124 sizepb = sblock.fs_bsize;
125 for (i = 0; i < UFS_NIADDR; i++) {
126 sizepb *= NINDIR(&sblock);
127 idesc->id_level = i + 1;
128 if (DIP(&dino, di_ib[i])) {
129 idesc->id_blkno = DIP(&dino, di_ib[i]);
130 ret = iblock(idesc, remsize, BT_LEVEL1 + i);
133 } else if (remsize > 0) {
134 idesc->id_lbn += sizepb / sblock.fs_bsize;
135 if (idesc->id_type == DATA) {
136 /* An empty block in a directory XXX */
137 getpathname(pathbuf, idesc->id_number,
139 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
141 if (reply("ADJUST LENGTH") == 1) {
142 dp = ginode(idesc->id_number);
144 DIP(dp, di_size) - remsize);
147 "YOU MUST RERUN FSCK AFTERWARDS\n");
160 iblock(struct inodesc *idesc, off_t isize, int type)
163 int i, n, (*func)(struct inodesc *), nif;
166 char pathbuf[MAXPATHLEN + 1];
169 if (idesc->id_type != DATA) {
170 func = idesc->id_func;
171 if (((n = (*func)(idesc)) & KEEPON) == 0)
175 if (chkrange(idesc->id_blkno, idesc->id_numfrags))
177 bp = getdatablk(idesc->id_blkno, sblock.fs_bsize, type);
179 for (sizepb = sblock.fs_bsize, i = 0; i < idesc->id_level; i++)
180 sizepb *= NINDIR(&sblock);
181 if (howmany(isize, sizepb) > NINDIR(&sblock))
182 nif = NINDIR(&sblock);
184 nif = howmany(isize, sizepb);
185 if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
186 for (i = nif; i < NINDIR(&sblock); i++) {
187 if (IBLK(bp, i) == 0)
189 (void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
190 (u_long)idesc->id_number);
193 } else if (dofix(idesc, buf)) {
198 flush(fswritefd, bp);
200 for (i = 0; i < nif; i++) {
202 idesc->id_blkno = IBLK(bp, i);
203 if (idesc->id_level == 0) {
207 n = iblock(idesc, isize, type);
211 bp->b_flags &= ~B_INUSE;
215 idesc->id_lbn += sizepb / sblock.fs_bsize;
216 if (idesc->id_type == DATA && isize > 0) {
217 /* An empty block in a directory XXX */
218 getpathname(pathbuf, idesc->id_number,
220 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
222 if (reply("ADJUST LENGTH") == 1) {
223 dp = ginode(idesc->id_number);
225 DIP(dp, di_size) - isize);
228 "YOU MUST RERUN FSCK AFTERWARDS\n");
231 bp->b_flags &= ~B_INUSE;
238 bp->b_flags &= ~B_INUSE;
243 * Check that a block in a legal block number.
244 * Return 0 if in range, 1 if out of range.
247 chkrange(ufs2_daddr_t blk, int cnt)
251 if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
252 cnt - 1 > maxfsblock - blk)
254 if (cnt > sblock.fs_frag ||
255 fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
257 printf("bad size: blk %ld, offset %i, size %d\n",
258 (long)blk, (int)fragnum(&sblock, blk), cnt);
261 c = dtog(&sblock, blk);
262 if (blk < cgdmin(&sblock, c)) {
263 if ((blk + cnt) > cgsblock(&sblock, c)) {
265 printf("blk %ld < cgdmin %ld;",
266 (long)blk, (long)cgdmin(&sblock, c));
267 printf(" blk + cnt %ld > cgsbase %ld\n",
269 (long)cgsblock(&sblock, c));
274 if ((blk + cnt) > cgbase(&sblock, c+1)) {
276 printf("blk %ld >= cgdmin %ld;",
277 (long)blk, (long)cgdmin(&sblock, c));
278 printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
279 (long)(blk + cnt), (long)sblock.fs_fpg);
288 * General purpose interface for reading inodes.
291 ginode(ino_t inumber)
296 if (inumber < UFS_ROOTINO || inumber > maxino)
297 errx(EEXIT, "bad inode number %ju to ginode",
299 if (startinum == 0 ||
300 inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
301 iblk = ino_to_fsba(&sblock, inumber);
303 pbp->b_flags &= ~B_INUSE;
304 pbp = getdatablk(iblk, sblock.fs_bsize, BT_INODES);
305 startinum = rounddown(inumber, INOPB(&sblock));
307 if (sblock.fs_magic == FS_UFS1_MAGIC)
308 return ((union dinode *)
309 &pbp->b_un.b_dinode1[inumber % INOPB(&sblock)]);
310 dp = (union dinode *)&pbp->b_un.b_dinode2[inumber % INOPB(&sblock)];
311 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) {
312 pwarn("INODE CHECK-HASH FAILED");
313 prtinode(inumber, dp);
314 if (preen || reply("FIX") != 0) {
316 printf(" (FIXED)\n");
317 ffs_update_dinode_ckhash(&sblock,
318 (struct ufs2_dinode *)dp);
326 * Special purpose version of ginode used to optimize first pass
327 * over all the inodes in numerical order.
329 static ino_t nextino, lastinum, lastvalidinum;
330 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
331 static struct bufarea inobuf;
334 getnextinode(ino_t inumber, int rebuildcg)
339 ufs2_daddr_t ndb, blk;
341 static caddr_t nextinop;
343 if (inumber != nextino++ || inumber > lastvalidinum)
344 errx(EEXIT, "bad inode number %ju to nextinode",
346 if (inumber >= lastinum) {
348 blk = ino_to_fsba(&sblock, lastinum);
349 if (readcount % readpercg == 0) {
351 lastinum += partialcnt;
357 * Flush old contents in case they have been updated.
358 * If getblk encounters an error, it will already have zeroed
359 * out the buffer, so we do not need to do so here.
361 flush(fswritefd, &inobuf);
362 getblk(&inobuf, blk, size);
363 nextinop = inobuf.b_un.b_buf;
365 dp = (union dinode *)nextinop;
366 if (sblock.fs_magic == FS_UFS1_MAGIC)
367 nextinop += sizeof(struct ufs1_dinode);
369 nextinop += sizeof(struct ufs2_dinode);
370 if ((ckhashadd & CK_INODE) != 0) {
371 ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp);
374 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) {
375 pwarn("INODE CHECK-HASH FAILED");
376 prtinode(inumber, dp);
377 if (preen || reply("FIX") != 0) {
379 printf(" (FIXED)\n");
380 ffs_update_dinode_ckhash(&sblock,
381 (struct ufs2_dinode *)dp);
385 if (rebuildcg && (char *)dp == inobuf.b_un.b_buf) {
387 * Try to determine if we have reached the end of the
390 mode = DIP(dp, di_mode) & IFMT;
392 if (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
393 UFS_NDADDR * sizeof(ufs2_daddr_t)) ||
394 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
395 UFS_NIADDR * sizeof(ufs2_daddr_t)) ||
396 dp->dp2.di_mode || dp->dp2.di_size)
402 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
405 if (mode == IFBLK || mode == IFCHR)
409 * Fake ndb value so direct/indirect block checks below
410 * will detect any garbage after symlink string.
412 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
413 ndb = howmany(DIP(dp, di_size),
414 sizeof(ufs2_daddr_t));
415 if (ndb > UFS_NDADDR) {
416 j = ndb - UFS_NDADDR;
417 for (ndb = 1; j > 1; j--)
418 ndb *= NINDIR(&sblock);
423 for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++)
424 if (DIP(dp, di_db[j]) != 0)
426 for (j = 0, ndb -= UFS_NDADDR; ndb > 0; j++)
427 ndb /= NINDIR(&sblock);
428 for (; j < UFS_NIADDR; j++)
429 if (DIP(dp, di_ib[j]) != 0)
436 setinodebuf(ino_t inum)
439 if (inum % sblock.fs_ipg != 0)
440 errx(EEXIT, "bad inode number %ju to setinodebuf",
442 lastvalidinum = inum + sblock.fs_ipg - 1;
447 if (inobuf.b_un.b_buf != NULL)
449 inobufsize = blkroundup(&sblock, INOBUFSIZE);
450 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
451 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
452 readpercg = sblock.fs_ipg / fullcnt;
453 partialcnt = sblock.fs_ipg % fullcnt;
454 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
455 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
456 if (partialcnt != 0) {
459 partialcnt = fullcnt;
460 partialsize = inobufsize;
462 initbarea(&inobuf, BT_INODES);
463 if ((inobuf.b_un.b_buf = Malloc((unsigned)inobufsize)) == NULL)
464 errx(EEXIT, "cannot allocate space for inode buffer");
472 * Flush old contents in case they have been updated.
474 flush(fswritefd, &inobuf);
475 if (inobuf.b_un.b_buf != NULL)
476 free((char *)inobuf.b_un.b_buf);
477 inobuf.b_un.b_buf = NULL;
481 * Routines to maintain information about directory inodes.
482 * This is built during the first pass and used during the
483 * second and third passes.
485 * Enter inodes into the cache.
488 cacheino(union dinode *dp, ino_t inumber)
490 struct inoinfo *inp, **inpp;
493 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > UFS_NDADDR)
494 blks = UFS_NDADDR + UFS_NIADDR;
495 else if (DIP(dp, di_size) > 0)
496 blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
499 inp = (struct inoinfo *)
500 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
502 errx(EEXIT, "cannot increase directory list");
503 inpp = &inphead[inumber % dirhash];
504 inp->i_nexthash = *inpp;
506 inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ino_t)0;
507 inp->i_dotdot = (ino_t)0;
508 inp->i_number = inumber;
509 inp->i_isize = DIP(dp, di_size);
510 inp->i_numblks = blks;
511 for (i = 0; i < MIN(blks, UFS_NDADDR); i++)
512 inp->i_blks[i] = DIP(dp, di_db[i]);
513 if (blks > UFS_NDADDR)
514 for (i = 0; i < UFS_NIADDR; i++)
515 inp->i_blks[UFS_NDADDR + i] = DIP(dp, di_ib[i]);
516 if (inplast == listmax) {
518 inpsort = (struct inoinfo **)reallocarray((char *)inpsort,
519 listmax, sizeof(struct inoinfo *));
521 errx(EEXIT, "cannot increase directory list");
523 inpsort[inplast++] = inp;
527 * Look up an inode cache structure.
530 getinoinfo(ino_t inumber)
534 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
535 if (inp->i_number != inumber)
539 errx(EEXIT, "cannot find inode %ju", (uintmax_t)inumber);
540 return ((struct inoinfo *)0);
544 * Clean up all the inode cache structure.
549 struct inoinfo **inpp;
553 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
554 free((char *)(*inpp));
555 free((char *)inphead);
556 free((char *)inpsort);
557 inphead = inpsort = NULL;
561 inodirty(union dinode *dp)
564 if (sblock.fs_magic == FS_UFS2_MAGIC)
565 ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp);
570 clri(struct inodesc *idesc, const char *type, int flag)
574 dp = ginode(idesc->id_number);
577 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
578 prtinode(idesc->id_number, dp);
581 if (preen || reply("CLEAR") == 1) {
583 printf(" (CLEARED)\n");
585 if (bkgrdflag == 0) {
586 (void)ckinode(dp, idesc);
587 inoinfo(idesc->id_number)->ino_state = USTATE;
591 cmd.value = idesc->id_number;
592 cmd.size = -DIP(dp, di_nlink);
594 printf("adjrefcnt ino %ld amt %lld\n",
595 (long)cmd.value, (long long)cmd.size);
596 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
597 &cmd, sizeof cmd) == -1)
598 rwerror("ADJUST INODE", cmd.value);
604 findname(struct inodesc *idesc)
606 struct direct *dirp = idesc->id_dirp;
608 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
612 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
617 findino(struct inodesc *idesc)
619 struct direct *dirp = idesc->id_dirp;
621 if (dirp->d_ino == 0)
623 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
624 dirp->d_ino >= UFS_ROOTINO && dirp->d_ino <= maxino) {
625 idesc->id_parent = dirp->d_ino;
632 clearentry(struct inodesc *idesc)
634 struct direct *dirp = idesc->id_dirp;
636 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
641 return (STOP|FOUND|ALTERED);
645 prtinode(ino_t ino, union dinode *dp)
651 printf(" I=%lu ", (u_long)ino);
652 if (ino < UFS_ROOTINO || ino > maxino)
655 if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL)
656 printf("%s ", pw->pw_name);
658 printf("%u ", (unsigned)DIP(dp, di_uid));
659 printf("MODE=%o\n", DIP(dp, di_mode));
661 printf("%s: ", cdevname);
662 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
663 t = DIP(dp, di_mtime);
665 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
669 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
672 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
674 switch (inoinfo(ino)->ino_state) {
678 inoinfo(ino)->ino_state = FCLEAR;
683 inoinfo(ino)->ino_state = DCLEAR;
691 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
697 * allocate an unused inode
700 allocino(ino_t request, int type)
704 struct bufarea *cgbp;
709 request = UFS_ROOTINO;
710 else if (inoinfo(request)->ino_state != USTATE)
712 for (ino = request; ino < maxino; ino++)
713 if (inoinfo(ino)->ino_state == USTATE)
717 cg = ino_to_cg(&sblock, ino);
719 cgp = cgbp->b_un.b_cg;
720 if (!check_cgmagic(cg, cgbp))
722 setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
723 cgp->cg_cs.cs_nifree--;
724 switch (type & IFMT) {
726 inoinfo(ino)->ino_state = DSTATE;
727 cgp->cg_cs.cs_ndir++;
731 inoinfo(ino)->ino_state = FSTATE;
738 DIP_SET(dp, di_db[0], allocblk((long)1));
739 if (DIP(dp, di_db[0]) == 0) {
740 inoinfo(ino)->ino_state = USTATE;
743 DIP_SET(dp, di_mode, type);
744 DIP_SET(dp, di_flags, 0);
745 DIP_SET(dp, di_atime, time(NULL));
746 DIP_SET(dp, di_ctime, DIP(dp, di_atime));
747 DIP_SET(dp, di_mtime, DIP(dp, di_ctime));
748 DIP_SET(dp, di_mtimensec, 0);
749 DIP_SET(dp, di_ctimensec, 0);
750 DIP_SET(dp, di_atimensec, 0);
751 DIP_SET(dp, di_size, sblock.fs_fsize);
752 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
755 inoinfo(ino)->ino_type = IFTODT(type);
760 * deallocate an inode
765 struct inodesc idesc;
768 memset(&idesc, 0, sizeof(struct inodesc));
769 idesc.id_type = ADDR;
770 idesc.id_func = pass4check;
771 idesc.id_number = ino;
773 (void)ckinode(dp, &idesc);
776 inoinfo(ino)->ino_state = USTATE;