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 *, long ilevel, 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_entryno = 0;
74 idesc->id_filesize = DIP(dp, di_size);
75 mode = DIP(dp, di_mode) & IFMT;
76 if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
77 DIP(dp, di_size) < (unsigned)sblock.fs_maxsymlinklen))
79 if (sblock.fs_magic == FS_UFS1_MAGIC)
83 ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize);
84 for (i = 0; i < UFS_NDADDR; i++) {
87 (offset = blkoff(&sblock, DIP(&dino, di_size))) != 0)
89 numfrags(&sblock, fragroundup(&sblock, offset));
91 idesc->id_numfrags = sblock.fs_frag;
92 if (DIP(&dino, di_db[i]) == 0) {
93 if (idesc->id_type == DATA && ndb >= 0) {
94 /* An empty block in a directory XXX */
95 getpathname(pathbuf, idesc->id_number,
97 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
99 if (reply("ADJUST LENGTH") == 1) {
100 dp = ginode(idesc->id_number);
102 i * sblock.fs_bsize);
104 "YOU MUST RERUN FSCK AFTERWARDS\n");
112 idesc->id_blkno = DIP(&dino, di_db[i]);
113 if (idesc->id_type != DATA)
114 ret = (*idesc->id_func)(idesc);
116 ret = dirscan(idesc);
120 idesc->id_numfrags = sblock.fs_frag;
121 remsize = DIP(&dino, di_size) - sblock.fs_bsize * UFS_NDADDR;
122 sizepb = sblock.fs_bsize;
123 for (i = 0; i < UFS_NIADDR; i++) {
124 sizepb *= NINDIR(&sblock);
125 if (DIP(&dino, di_ib[i])) {
126 idesc->id_blkno = DIP(&dino, di_ib[i]);
127 ret = iblock(idesc, i + 1, remsize, BT_LEVEL1 + i);
131 idesc->id_lbn += sizepb / sblock.fs_bsize;
132 if (idesc->id_type == DATA && remsize > 0) {
133 /* An empty block in a directory XXX */
134 getpathname(pathbuf, idesc->id_number,
136 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
138 if (reply("ADJUST LENGTH") == 1) {
139 dp = ginode(idesc->id_number);
141 DIP(dp, di_size) - remsize);
144 "YOU MUST RERUN FSCK AFTERWARDS\n");
157 iblock(struct inodesc *idesc, long ilevel, off_t isize, int type)
160 int i, n, (*func)(struct inodesc *), nif;
163 char pathbuf[MAXPATHLEN + 1];
166 if (idesc->id_type != DATA) {
167 func = idesc->id_func;
168 if (((n = (*func)(idesc)) & KEEPON) == 0)
172 if (chkrange(idesc->id_blkno, idesc->id_numfrags))
174 bp = getdatablk(idesc->id_blkno, sblock.fs_bsize, type);
176 for (sizepb = sblock.fs_bsize, i = 0; i < ilevel; i++)
177 sizepb *= NINDIR(&sblock);
178 if (howmany(isize, sizepb) > NINDIR(&sblock))
179 nif = NINDIR(&sblock);
181 nif = howmany(isize, sizepb);
182 if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
183 for (i = nif; i < NINDIR(&sblock); i++) {
184 if (IBLK(bp, i) == 0)
186 (void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
187 (u_long)idesc->id_number);
190 } else if (dofix(idesc, buf)) {
195 flush(fswritefd, bp);
197 for (i = 0; i < nif; i++) {
201 idesc->id_blkno = IBLK(bp, i);
205 n = iblock(idesc, ilevel, isize, type);
207 bp->b_flags &= ~B_INUSE;
211 if (idesc->id_type == DATA && isize > 0) {
212 /* An empty block in a directory XXX */
213 getpathname(pathbuf, idesc->id_number,
215 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
217 if (reply("ADJUST LENGTH") == 1) {
218 dp = ginode(idesc->id_number);
220 DIP(dp, di_size) - isize);
223 "YOU MUST RERUN FSCK AFTERWARDS\n");
226 bp->b_flags &= ~B_INUSE;
233 bp->b_flags &= ~B_INUSE;
238 * Check that a block in a legal block number.
239 * Return 0 if in range, 1 if out of range.
242 chkrange(ufs2_daddr_t blk, int cnt)
246 if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
247 cnt - 1 > maxfsblock - blk)
249 if (cnt > sblock.fs_frag ||
250 fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
252 printf("bad size: blk %ld, offset %i, size %d\n",
253 (long)blk, (int)fragnum(&sblock, blk), cnt);
256 c = dtog(&sblock, blk);
257 if (blk < cgdmin(&sblock, c)) {
258 if ((blk + cnt) > cgsblock(&sblock, c)) {
260 printf("blk %ld < cgdmin %ld;",
261 (long)blk, (long)cgdmin(&sblock, c));
262 printf(" blk + cnt %ld > cgsbase %ld\n",
264 (long)cgsblock(&sblock, c));
269 if ((blk + cnt) > cgbase(&sblock, c+1)) {
271 printf("blk %ld >= cgdmin %ld;",
272 (long)blk, (long)cgdmin(&sblock, c));
273 printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
274 (long)(blk + cnt), (long)sblock.fs_fpg);
283 * General purpose interface for reading inodes.
286 ginode(ino_t inumber)
291 if (inumber < UFS_ROOTINO || inumber > maxino)
292 errx(EEXIT, "bad inode number %ju to ginode",
294 if (startinum == 0 ||
295 inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
296 iblk = ino_to_fsba(&sblock, inumber);
298 pbp->b_flags &= ~B_INUSE;
299 pbp = getdatablk(iblk, sblock.fs_bsize, BT_INODES);
300 startinum = rounddown(inumber, INOPB(&sblock));
302 if (sblock.fs_magic == FS_UFS1_MAGIC)
303 return ((union dinode *)
304 &pbp->b_un.b_dinode1[inumber % INOPB(&sblock)]);
305 dp = (union dinode *)&pbp->b_un.b_dinode2[inumber % INOPB(&sblock)];
306 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) {
307 pwarn("INODE CHECK-HASH FAILED");
308 prtinode(inumber, dp);
309 if (preen || reply("FIX") != 0) {
311 printf(" (FIXED)\n");
312 ffs_update_dinode_ckhash(&sblock,
313 (struct ufs2_dinode *)dp);
321 * Special purpose version of ginode used to optimize first pass
322 * over all the inodes in numerical order.
324 static ino_t nextino, lastinum, lastvalidinum;
325 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
326 static struct bufarea inobuf;
329 getnextinode(ino_t inumber, int rebuildcg)
334 ufs2_daddr_t ndb, blk;
336 static caddr_t nextinop;
338 if (inumber != nextino++ || inumber > lastvalidinum)
339 errx(EEXIT, "bad inode number %ju to nextinode",
341 if (inumber >= lastinum) {
343 blk = ino_to_fsba(&sblock, lastinum);
344 if (readcount % readpercg == 0) {
346 lastinum += partialcnt;
352 * If getblk encounters an error, it will already have zeroed
353 * out the buffer, so we do not need to do so here.
355 getblk(&inobuf, blk, size);
356 nextinop = inobuf.b_un.b_buf;
358 dp = (union dinode *)nextinop;
359 if (sblock.fs_magic == FS_UFS1_MAGIC)
360 nextinop += sizeof(struct ufs1_dinode);
362 nextinop += sizeof(struct ufs2_dinode);
363 if ((ckhashadd & CK_INODE) != 0) {
364 ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp);
367 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) {
368 pwarn("INODE CHECK-HASH FAILED");
369 prtinode(inumber, dp);
370 if (preen || reply("FIX") != 0) {
372 printf(" (FIXED)\n");
373 ffs_update_dinode_ckhash(&sblock,
374 (struct ufs2_dinode *)dp);
378 if (rebuildcg && (char *)dp == inobuf.b_un.b_buf) {
380 * Try to determine if we have reached the end of the
383 mode = DIP(dp, di_mode) & IFMT;
385 if (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
386 UFS_NDADDR * sizeof(ufs2_daddr_t)) ||
387 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
388 UFS_NIADDR * sizeof(ufs2_daddr_t)) ||
389 dp->dp2.di_mode || dp->dp2.di_size)
395 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
398 if (mode == IFBLK || mode == IFCHR)
402 * Fake ndb value so direct/indirect block checks below
403 * will detect any garbage after symlink string.
405 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
406 ndb = howmany(DIP(dp, di_size),
407 sizeof(ufs2_daddr_t));
408 if (ndb > UFS_NDADDR) {
409 j = ndb - UFS_NDADDR;
410 for (ndb = 1; j > 1; j--)
411 ndb *= NINDIR(&sblock);
416 for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++)
417 if (DIP(dp, di_db[j]) != 0)
419 for (j = 0, ndb -= UFS_NDADDR; ndb > 0; j++)
420 ndb /= NINDIR(&sblock);
421 for (; j < UFS_NIADDR; j++)
422 if (DIP(dp, di_ib[j]) != 0)
429 setinodebuf(ino_t inum)
432 if (inum % sblock.fs_ipg != 0)
433 errx(EEXIT, "bad inode number %ju to setinodebuf",
435 lastvalidinum = inum + sblock.fs_ipg - 1;
440 if (inobuf.b_un.b_buf != NULL)
442 inobufsize = blkroundup(&sblock, INOBUFSIZE);
443 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
444 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
445 readpercg = sblock.fs_ipg / fullcnt;
446 partialcnt = sblock.fs_ipg % fullcnt;
447 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
448 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
449 if (partialcnt != 0) {
452 partialcnt = fullcnt;
453 partialsize = inobufsize;
455 initbarea(&inobuf, BT_INODES);
456 if ((inobuf.b_un.b_buf = Malloc((unsigned)inobufsize)) == NULL)
457 errx(EEXIT, "cannot allocate space for inode buffer");
464 if (inobuf.b_un.b_buf != NULL)
465 free((char *)inobuf.b_un.b_buf);
466 inobuf.b_un.b_buf = NULL;
470 * Routines to maintain information about directory inodes.
471 * This is built during the first pass and used during the
472 * second and third passes.
474 * Enter inodes into the cache.
477 cacheino(union dinode *dp, ino_t inumber)
479 struct inoinfo *inp, **inpp;
482 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > UFS_NDADDR)
483 blks = UFS_NDADDR + UFS_NIADDR;
484 else if (DIP(dp, di_size) > 0)
485 blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
488 inp = (struct inoinfo *)
489 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
491 errx(EEXIT, "cannot increase directory list");
492 inpp = &inphead[inumber % dirhash];
493 inp->i_nexthash = *inpp;
495 inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ino_t)0;
496 inp->i_dotdot = (ino_t)0;
497 inp->i_number = inumber;
498 inp->i_isize = DIP(dp, di_size);
499 inp->i_numblks = blks;
500 for (i = 0; i < MIN(blks, UFS_NDADDR); i++)
501 inp->i_blks[i] = DIP(dp, di_db[i]);
502 if (blks > UFS_NDADDR)
503 for (i = 0; i < UFS_NIADDR; i++)
504 inp->i_blks[UFS_NDADDR + i] = DIP(dp, di_ib[i]);
505 if (inplast == listmax) {
507 inpsort = (struct inoinfo **)reallocarray((char *)inpsort,
508 listmax, sizeof(struct inoinfo *));
510 errx(EEXIT, "cannot increase directory list");
512 inpsort[inplast++] = inp;
516 * Look up an inode cache structure.
519 getinoinfo(ino_t inumber)
523 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
524 if (inp->i_number != inumber)
528 errx(EEXIT, "cannot find inode %ju", (uintmax_t)inumber);
529 return ((struct inoinfo *)0);
533 * Clean up all the inode cache structure.
538 struct inoinfo **inpp;
542 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
543 free((char *)(*inpp));
544 free((char *)inphead);
545 free((char *)inpsort);
546 inphead = inpsort = NULL;
550 inodirty(union dinode *dp)
553 if (sblock.fs_magic == FS_UFS2_MAGIC)
554 ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp);
559 clri(struct inodesc *idesc, const char *type, int flag)
563 dp = ginode(idesc->id_number);
566 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
567 prtinode(idesc->id_number, dp);
570 if (preen || reply("CLEAR") == 1) {
572 printf(" (CLEARED)\n");
574 if (bkgrdflag == 0) {
575 (void)ckinode(dp, idesc);
576 inoinfo(idesc->id_number)->ino_state = USTATE;
580 cmd.value = idesc->id_number;
581 cmd.size = -DIP(dp, di_nlink);
583 printf("adjrefcnt ino %ld amt %lld\n",
584 (long)cmd.value, (long long)cmd.size);
585 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
586 &cmd, sizeof cmd) == -1)
587 rwerror("ADJUST INODE", cmd.value);
593 findname(struct inodesc *idesc)
595 struct direct *dirp = idesc->id_dirp;
597 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
601 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
606 findino(struct inodesc *idesc)
608 struct direct *dirp = idesc->id_dirp;
610 if (dirp->d_ino == 0)
612 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
613 dirp->d_ino >= UFS_ROOTINO && dirp->d_ino <= maxino) {
614 idesc->id_parent = dirp->d_ino;
621 clearentry(struct inodesc *idesc)
623 struct direct *dirp = idesc->id_dirp;
625 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
630 return (STOP|FOUND|ALTERED);
634 prtinode(ino_t ino, union dinode *dp)
640 printf(" I=%lu ", (u_long)ino);
641 if (ino < UFS_ROOTINO || ino > maxino)
644 if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL)
645 printf("%s ", pw->pw_name);
647 printf("%u ", (unsigned)DIP(dp, di_uid));
648 printf("MODE=%o\n", DIP(dp, di_mode));
650 printf("%s: ", cdevname);
651 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
652 t = DIP(dp, di_mtime);
654 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
658 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
661 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
663 switch (inoinfo(ino)->ino_state) {
667 inoinfo(ino)->ino_state = FCLEAR;
672 inoinfo(ino)->ino_state = DCLEAR;
680 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
686 * allocate an unused inode
689 allocino(ino_t request, int type)
693 struct bufarea *cgbp;
698 request = UFS_ROOTINO;
699 else if (inoinfo(request)->ino_state != USTATE)
701 for (ino = request; ino < maxino; ino++)
702 if (inoinfo(ino)->ino_state == USTATE)
706 cg = ino_to_cg(&sblock, ino);
708 cgp = cgbp->b_un.b_cg;
709 if (!check_cgmagic(cg, cgbp))
711 setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
712 cgp->cg_cs.cs_nifree--;
713 switch (type & IFMT) {
715 inoinfo(ino)->ino_state = DSTATE;
716 cgp->cg_cs.cs_ndir++;
720 inoinfo(ino)->ino_state = FSTATE;
727 DIP_SET(dp, di_db[0], allocblk((long)1));
728 if (DIP(dp, di_db[0]) == 0) {
729 inoinfo(ino)->ino_state = USTATE;
732 DIP_SET(dp, di_mode, type);
733 DIP_SET(dp, di_flags, 0);
734 DIP_SET(dp, di_atime, time(NULL));
735 DIP_SET(dp, di_ctime, DIP(dp, di_atime));
736 DIP_SET(dp, di_mtime, DIP(dp, di_ctime));
737 DIP_SET(dp, di_mtimensec, 0);
738 DIP_SET(dp, di_ctimensec, 0);
739 DIP_SET(dp, di_atimensec, 0);
740 DIP_SET(dp, di_size, sblock.fs_fsize);
741 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
744 inoinfo(ino)->ino_type = IFTODT(type);
749 * deallocate an inode
754 struct inodesc idesc;
757 memset(&idesc, 0, sizeof(struct inodesc));
758 idesc.id_type = ADDR;
759 idesc.id_func = pass4check;
760 idesc.id_number = ino;
762 (void)ckinode(dp, &idesc);
765 inoinfo(ino)->ino_state = USTATE;