2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char sccsid[] = "@(#)inode.c 8.8 (Berkeley) 4/28/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/stdint.h>
40 #include <sys/sysctl.h>
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ufs/dir.h>
44 #include <ufs/ffs/fs.h>
53 static ino_t startinum;
55 static int iblock(struct inodesc *, off_t isize, int type);
58 ckinode(union dinode *dp, struct inodesc *idesc)
60 off_t remsize, sizepb;
65 char pathbuf[MAXPATHLEN + 1];
67 if (idesc->id_fix != IGNORE)
68 idesc->id_fix = DONTKNOW;
70 idesc->id_lballoc = -1;
72 idesc->id_entryno = 0;
73 idesc->id_filesize = DIP(dp, di_size);
74 mode = DIP(dp, di_mode) & IFMT;
75 if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
76 DIP(dp, di_size) < (unsigned)sblock.fs_maxsymlinklen))
78 if (sblock.fs_magic == FS_UFS1_MAGIC)
82 ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize);
83 for (i = 0; i < NDADDR; i++) {
86 (offset = blkoff(&sblock, DIP(&dino, di_size))) != 0)
88 numfrags(&sblock, fragroundup(&sblock, offset));
90 idesc->id_numfrags = sblock.fs_frag;
91 if (DIP(&dino, di_db[i]) == 0) {
92 if (idesc->id_type == DATA && ndb >= 0) {
93 /* An empty block in a directory XXX */
94 getpathname(pathbuf, idesc->id_number,
96 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
98 if (reply("ADJUST LENGTH") == 1) {
99 dp = ginode(idesc->id_number);
101 i * sblock.fs_bsize);
103 "YOU MUST RERUN FSCK AFTERWARDS\n");
111 idesc->id_blkno = DIP(&dino, di_db[i]);
112 if (idesc->id_type != DATA)
113 ret = (*idesc->id_func)(idesc);
115 ret = dirscan(idesc);
119 idesc->id_numfrags = sblock.fs_frag;
120 remsize = DIP(&dino, di_size) - sblock.fs_bsize * NDADDR;
121 sizepb = sblock.fs_bsize;
122 for (i = 0; i < NIADDR; i++) {
123 sizepb *= NINDIR(&sblock);
124 idesc->id_level = i + 1;
125 if (DIP(&dino, di_ib[i])) {
126 idesc->id_blkno = DIP(&dino, di_ib[i]);
127 ret = iblock(idesc, remsize, BT_LEVEL1 + i);
130 } else if (remsize > 0) {
131 idesc->id_lbn += sizepb / sblock.fs_bsize;
132 if (idesc->id_type == DATA) {
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, 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 < idesc->id_level; 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++) {
199 idesc->id_blkno = IBLK(bp, i);
200 if (idesc->id_level == 0) {
204 n = iblock(idesc, isize, type);
208 bp->b_flags &= ~B_INUSE;
212 idesc->id_lbn += sizepb / sblock.fs_bsize;
213 if (idesc->id_type == DATA && isize > 0) {
214 /* An empty block in a directory XXX */
215 getpathname(pathbuf, idesc->id_number,
217 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
219 if (reply("ADJUST LENGTH") == 1) {
220 dp = ginode(idesc->id_number);
222 DIP(dp, di_size) - isize);
225 "YOU MUST RERUN FSCK AFTERWARDS\n");
228 bp->b_flags &= ~B_INUSE;
235 bp->b_flags &= ~B_INUSE;
240 * Check that a block in a legal block number.
241 * Return 0 if in range, 1 if out of range.
244 chkrange(ufs2_daddr_t blk, int cnt)
248 if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
249 cnt - 1 > maxfsblock - blk)
251 if (cnt > sblock.fs_frag ||
252 fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
254 printf("bad size: blk %ld, offset %i, size %d\n",
255 (long)blk, (int)fragnum(&sblock, blk), cnt);
258 c = dtog(&sblock, blk);
259 if (blk < cgdmin(&sblock, c)) {
260 if ((blk + cnt) > cgsblock(&sblock, c)) {
262 printf("blk %ld < cgdmin %ld;",
263 (long)blk, (long)cgdmin(&sblock, c));
264 printf(" blk + cnt %ld > cgsbase %ld\n",
266 (long)cgsblock(&sblock, c));
271 if ((blk + cnt) > cgbase(&sblock, c+1)) {
273 printf("blk %ld >= cgdmin %ld;",
274 (long)blk, (long)cgdmin(&sblock, c));
275 printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
276 (long)(blk + cnt), (long)sblock.fs_fpg);
285 * General purpose interface for reading inodes.
288 ginode(ino_t inumber)
292 if (inumber < ROOTINO || inumber > maxino)
293 errx(EEXIT, "bad inode number %ju to ginode",
295 if (startinum == 0 ||
296 inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
297 iblk = ino_to_fsba(&sblock, inumber);
299 pbp->b_flags &= ~B_INUSE;
300 pbp = getdatablk(iblk, sblock.fs_bsize, BT_INODES);
301 startinum = rounddown(inumber, INOPB(&sblock));
303 if (sblock.fs_magic == FS_UFS1_MAGIC)
304 return ((union dinode *)
305 &pbp->b_un.b_dinode1[inumber % INOPB(&sblock)]);
306 return ((union dinode *)&pbp->b_un.b_dinode2[inumber % INOPB(&sblock)]);
310 * Special purpose version of ginode used to optimize first pass
311 * over all the inodes in numerical order.
313 static ino_t nextino, lastinum, lastvalidinum;
314 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
315 static struct bufarea inobuf;
318 getnextinode(ino_t inumber, int rebuildcg)
323 ufs2_daddr_t ndb, blk;
325 static caddr_t nextinop;
327 if (inumber != nextino++ || inumber > lastvalidinum)
328 errx(EEXIT, "bad inode number %ju to nextinode",
330 if (inumber >= lastinum) {
332 blk = ino_to_fsba(&sblock, lastinum);
333 if (readcount % readpercg == 0) {
335 lastinum += partialcnt;
341 * If getblk encounters an error, it will already have zeroed
342 * out the buffer, so we do not need to do so here.
344 getblk(&inobuf, blk, size);
345 nextinop = inobuf.b_un.b_buf;
347 dp = (union dinode *)nextinop;
348 if (rebuildcg && nextinop == inobuf.b_un.b_buf) {
350 * Try to determine if we have reached the end of the
353 mode = DIP(dp, di_mode) & IFMT;
355 if (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
356 NDADDR * sizeof(ufs2_daddr_t)) ||
357 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
358 NIADDR * sizeof(ufs2_daddr_t)) ||
359 dp->dp2.di_mode || dp->dp2.di_size)
365 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
368 if (mode == IFBLK || mode == IFCHR)
372 * Fake ndb value so direct/indirect block checks below
373 * will detect any garbage after symlink string.
375 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
376 ndb = howmany(DIP(dp, di_size),
377 sizeof(ufs2_daddr_t));
380 for (ndb = 1; j > 1; j--)
381 ndb *= NINDIR(&sblock);
386 for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
387 if (DIP(dp, di_db[j]) != 0)
389 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
390 ndb /= NINDIR(&sblock);
391 for (; j < NIADDR; j++)
392 if (DIP(dp, di_ib[j]) != 0)
396 if (sblock.fs_magic == FS_UFS1_MAGIC)
397 nextinop += sizeof(struct ufs1_dinode);
399 nextinop += sizeof(struct ufs2_dinode);
404 setinodebuf(ino_t inum)
407 if (inum % sblock.fs_ipg != 0)
408 errx(EEXIT, "bad inode number %ju to setinodebuf",
410 lastvalidinum = inum + sblock.fs_ipg - 1;
415 if (inobuf.b_un.b_buf != NULL)
417 inobufsize = blkroundup(&sblock, INOBUFSIZE);
418 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
419 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
420 readpercg = sblock.fs_ipg / fullcnt;
421 partialcnt = sblock.fs_ipg % fullcnt;
422 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
423 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
424 if (partialcnt != 0) {
427 partialcnt = fullcnt;
428 partialsize = inobufsize;
430 initbarea(&inobuf, BT_INODES);
431 if ((inobuf.b_un.b_buf = Malloc((unsigned)inobufsize)) == NULL)
432 errx(EEXIT, "cannot allocate space for inode buffer");
439 if (inobuf.b_un.b_buf != NULL)
440 free((char *)inobuf.b_un.b_buf);
441 inobuf.b_un.b_buf = NULL;
445 * Routines to maintain information about directory inodes.
446 * This is built during the first pass and used during the
447 * second and third passes.
449 * Enter inodes into the cache.
452 cacheino(union dinode *dp, ino_t inumber)
454 struct inoinfo *inp, **inpp;
457 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > NDADDR)
458 blks = NDADDR + NIADDR;
459 else if (DIP(dp, di_size) > 0)
460 blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
463 inp = (struct inoinfo *)
464 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
466 errx(EEXIT, "cannot increase directory list");
467 inpp = &inphead[inumber % dirhash];
468 inp->i_nexthash = *inpp;
470 inp->i_parent = inumber == ROOTINO ? ROOTINO : (ino_t)0;
471 inp->i_dotdot = (ino_t)0;
472 inp->i_number = inumber;
473 inp->i_isize = DIP(dp, di_size);
474 inp->i_numblks = blks;
475 for (i = 0; i < MIN(blks, NDADDR); i++)
476 inp->i_blks[i] = DIP(dp, di_db[i]);
478 for (i = 0; i < NIADDR; i++)
479 inp->i_blks[NDADDR + i] = DIP(dp, di_ib[i]);
480 if (inplast == listmax) {
482 inpsort = (struct inoinfo **)reallocarray((char *)inpsort,
483 listmax, sizeof(struct inoinfo *));
485 errx(EEXIT, "cannot increase directory list");
487 inpsort[inplast++] = inp;
491 * Look up an inode cache structure.
494 getinoinfo(ino_t inumber)
498 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
499 if (inp->i_number != inumber)
503 errx(EEXIT, "cannot find inode %ju", (uintmax_t)inumber);
504 return ((struct inoinfo *)0);
508 * Clean up all the inode cache structure.
513 struct inoinfo **inpp;
517 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
518 free((char *)(*inpp));
519 free((char *)inphead);
520 free((char *)inpsort);
521 inphead = inpsort = NULL;
525 inodirty(union dinode *dp)
532 clri(struct inodesc *idesc, const char *type, int flag)
536 dp = ginode(idesc->id_number);
539 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
540 pinode(idesc->id_number);
542 if (preen || reply("CLEAR") == 1) {
544 printf(" (CLEARED)\n");
546 if (bkgrdflag == 0) {
547 (void)ckinode(dp, idesc);
548 inoinfo(idesc->id_number)->ino_state = USTATE;
552 cmd.value = idesc->id_number;
553 cmd.size = -DIP(dp, di_nlink);
555 printf("adjrefcnt ino %ld amt %lld\n",
556 (long)cmd.value, (long long)cmd.size);
557 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
558 &cmd, sizeof cmd) == -1)
559 rwerror("ADJUST INODE", cmd.value);
565 findname(struct inodesc *idesc)
567 struct direct *dirp = idesc->id_dirp;
569 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
573 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
578 findino(struct inodesc *idesc)
580 struct direct *dirp = idesc->id_dirp;
582 if (dirp->d_ino == 0)
584 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
585 dirp->d_ino >= ROOTINO && dirp->d_ino <= maxino) {
586 idesc->id_parent = dirp->d_ino;
593 clearentry(struct inodesc *idesc)
595 struct direct *dirp = idesc->id_dirp;
597 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
602 return (STOP|FOUND|ALTERED);
613 printf(" I=%lu ", (u_long)ino);
614 if (ino < ROOTINO || ino > maxino)
618 if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL)
619 printf("%s ", pw->pw_name);
621 printf("%u ", (unsigned)DIP(dp, di_uid));
622 printf("MODE=%o\n", DIP(dp, di_mode));
624 printf("%s: ", cdevname);
625 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
626 t = DIP(dp, di_mtime);
628 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
632 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
635 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
637 switch (inoinfo(ino)->ino_state) {
641 inoinfo(ino)->ino_state = FCLEAR;
646 inoinfo(ino)->ino_state = DCLEAR;
654 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
660 * allocate an unused inode
663 allocino(ino_t request, int type)
667 struct bufarea *cgbp;
673 else if (inoinfo(request)->ino_state != USTATE)
675 for (ino = request; ino < maxino; ino++)
676 if (inoinfo(ino)->ino_state == USTATE)
680 cg = ino_to_cg(&sblock, ino);
682 cgp = cgbp->b_un.b_cg;
683 if (!check_cgmagic(cg, cgbp))
685 setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
686 cgp->cg_cs.cs_nifree--;
687 switch (type & IFMT) {
689 inoinfo(ino)->ino_state = DSTATE;
690 cgp->cg_cs.cs_ndir++;
694 inoinfo(ino)->ino_state = FSTATE;
701 DIP_SET(dp, di_db[0], allocblk((long)1));
702 if (DIP(dp, di_db[0]) == 0) {
703 inoinfo(ino)->ino_state = USTATE;
706 DIP_SET(dp, di_mode, type);
707 DIP_SET(dp, di_flags, 0);
708 DIP_SET(dp, di_atime, time(NULL));
709 DIP_SET(dp, di_ctime, DIP(dp, di_atime));
710 DIP_SET(dp, di_mtime, DIP(dp, di_ctime));
711 DIP_SET(dp, di_mtimensec, 0);
712 DIP_SET(dp, di_ctimensec, 0);
713 DIP_SET(dp, di_atimensec, 0);
714 DIP_SET(dp, di_size, sblock.fs_fsize);
715 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
718 inoinfo(ino)->ino_type = IFTODT(type);
723 * deallocate an inode
728 struct inodesc idesc;
731 memset(&idesc, 0, sizeof(struct inodesc));
732 idesc.id_type = ADDR;
733 idesc.id_func = pass4check;
734 idesc.id_number = ino;
736 (void)ckinode(dp, &idesc);
739 inoinfo(ino)->ino_state = USTATE;