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 *, long ilevel, 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_entryno = 0;
71 idesc->id_filesize = DIP(dp, di_size);
72 mode = DIP(dp, di_mode) & IFMT;
73 if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
74 DIP(dp, di_size) < (unsigned)sblock.fs_maxsymlinklen))
76 if (sblock.fs_magic == FS_UFS1_MAGIC)
80 ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize);
81 for (i = 0; i < NDADDR; i++) {
84 (offset = blkoff(&sblock, DIP(&dino, di_size))) != 0)
86 numfrags(&sblock, fragroundup(&sblock, offset));
88 idesc->id_numfrags = sblock.fs_frag;
89 if (DIP(&dino, di_db[i]) == 0) {
90 if (idesc->id_type == DATA && ndb >= 0) {
91 /* An empty block in a directory XXX */
92 getpathname(pathbuf, idesc->id_number,
94 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
96 if (reply("ADJUST LENGTH") == 1) {
97 dp = ginode(idesc->id_number);
101 "YOU MUST RERUN FSCK AFTERWARDS\n");
109 idesc->id_blkno = DIP(&dino, di_db[i]);
110 if (idesc->id_type != DATA)
111 ret = (*idesc->id_func)(idesc);
113 ret = dirscan(idesc);
117 idesc->id_numfrags = sblock.fs_frag;
118 remsize = DIP(&dino, di_size) - sblock.fs_bsize * NDADDR;
119 sizepb = sblock.fs_bsize;
120 for (i = 0; i < NIADDR; i++) {
121 sizepb *= NINDIR(&sblock);
122 if (DIP(&dino, di_ib[i])) {
123 idesc->id_blkno = DIP(&dino, di_ib[i]);
124 ret = iblock(idesc, i + 1, remsize, BT_LEVEL1 + i);
128 idesc->id_lbn += sizepb / sblock.fs_bsize;
129 if (idesc->id_type == DATA && remsize > 0) {
130 /* An empty block in a directory XXX */
131 getpathname(pathbuf, idesc->id_number,
133 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
135 if (reply("ADJUST LENGTH") == 1) {
136 dp = ginode(idesc->id_number);
138 DIP(dp, di_size) - remsize);
141 "YOU MUST RERUN FSCK AFTERWARDS\n");
154 iblock(struct inodesc *idesc, long ilevel, off_t isize, int type)
157 int i, n, (*func)(struct inodesc *), nif;
160 char pathbuf[MAXPATHLEN + 1];
163 if (idesc->id_type != DATA) {
164 func = idesc->id_func;
165 if (((n = (*func)(idesc)) & KEEPON) == 0)
169 if (chkrange(idesc->id_blkno, idesc->id_numfrags))
171 bp = getdatablk(idesc->id_blkno, sblock.fs_bsize, type);
173 for (sizepb = sblock.fs_bsize, i = 0; i < ilevel; i++)
174 sizepb *= NINDIR(&sblock);
175 if (howmany(isize, sizepb) > NINDIR(&sblock))
176 nif = NINDIR(&sblock);
178 nif = howmany(isize, sizepb);
179 if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
180 for (i = nif; i < NINDIR(&sblock); i++) {
181 if (IBLK(bp, i) == 0)
183 (void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
184 (u_long)idesc->id_number);
187 } else if (dofix(idesc, buf)) {
192 flush(fswritefd, bp);
194 for (i = 0; i < nif; i++) {
198 idesc->id_blkno = IBLK(bp, i);
202 n = iblock(idesc, ilevel, isize, type);
204 bp->b_flags &= ~B_INUSE;
208 if (idesc->id_type == DATA && isize > 0) {
209 /* An empty block in a directory XXX */
210 getpathname(pathbuf, idesc->id_number,
212 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
214 if (reply("ADJUST LENGTH") == 1) {
215 dp = ginode(idesc->id_number);
217 DIP(dp, di_size) - isize);
220 "YOU MUST RERUN FSCK AFTERWARDS\n");
223 bp->b_flags &= ~B_INUSE;
230 bp->b_flags &= ~B_INUSE;
235 * Check that a block in a legal block number.
236 * Return 0 if in range, 1 if out of range.
239 chkrange(ufs2_daddr_t blk, int cnt)
243 if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
244 cnt - 1 > maxfsblock - blk)
246 if (cnt > sblock.fs_frag ||
247 fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
249 printf("bad size: blk %ld, offset %i, size %d\n",
250 (long)blk, (int)fragnum(&sblock, blk), cnt);
253 c = dtog(&sblock, blk);
254 if (blk < cgdmin(&sblock, c)) {
255 if ((blk + cnt) > cgsblock(&sblock, c)) {
257 printf("blk %ld < cgdmin %ld;",
258 (long)blk, (long)cgdmin(&sblock, c));
259 printf(" blk + cnt %ld > cgsbase %ld\n",
261 (long)cgsblock(&sblock, c));
266 if ((blk + cnt) > cgbase(&sblock, c+1)) {
268 printf("blk %ld >= cgdmin %ld;",
269 (long)blk, (long)cgdmin(&sblock, c));
270 printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
271 (long)(blk + cnt), (long)sblock.fs_fpg);
280 * General purpose interface for reading inodes.
283 ginode(ino_t inumber)
287 if (inumber < ROOTINO || inumber > maxino)
288 errx(EEXIT, "bad inode number %d to ginode", inumber);
289 if (startinum == 0 ||
290 inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
291 iblk = ino_to_fsba(&sblock, inumber);
293 pbp->b_flags &= ~B_INUSE;
294 pbp = getdatablk(iblk, sblock.fs_bsize, BT_INODES);
295 startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
297 if (sblock.fs_magic == FS_UFS1_MAGIC)
298 return ((union dinode *)
299 &pbp->b_un.b_dinode1[inumber % INOPB(&sblock)]);
300 return ((union dinode *)&pbp->b_un.b_dinode2[inumber % INOPB(&sblock)]);
304 * Special purpose version of ginode used to optimize first pass
305 * over all the inodes in numerical order.
307 static ino_t nextino, lastinum, lastvalidinum;
308 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
309 static struct bufarea inobuf;
312 getnextinode(ino_t inumber, int rebuildcg)
317 ufs2_daddr_t ndb, blk;
319 static caddr_t nextinop;
321 if (inumber != nextino++ || inumber > lastvalidinum)
322 errx(EEXIT, "bad inode number %d to nextinode", inumber);
323 if (inumber >= lastinum) {
325 blk = ino_to_fsba(&sblock, lastinum);
326 if (readcount % readpercg == 0) {
328 lastinum += partialcnt;
334 * If getblk encounters an error, it will already have zeroed
335 * out the buffer, so we do not need to do so here.
337 getblk(&inobuf, blk, size);
338 nextinop = inobuf.b_un.b_buf;
340 dp = (union dinode *)nextinop;
341 if (rebuildcg && nextinop == inobuf.b_un.b_buf) {
343 * Try to determine if we have reached the end of the
346 mode = DIP(dp, di_mode) & IFMT;
348 if (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
349 NDADDR * sizeof(ufs2_daddr_t)) ||
350 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
351 NIADDR * sizeof(ufs2_daddr_t)) ||
352 dp->dp2.di_mode || dp->dp2.di_size)
358 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
361 if (mode == IFBLK || mode == IFCHR)
365 * Fake ndb value so direct/indirect block checks below
366 * will detect any garbage after symlink string.
368 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
369 ndb = howmany(DIP(dp, di_size),
370 sizeof(ufs2_daddr_t));
373 for (ndb = 1; j > 1; j--)
374 ndb *= NINDIR(&sblock);
379 for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
380 if (DIP(dp, di_db[j]) != 0)
382 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
383 ndb /= NINDIR(&sblock);
384 for (; j < NIADDR; j++)
385 if (DIP(dp, di_ib[j]) != 0)
389 if (sblock.fs_magic == FS_UFS1_MAGIC)
390 nextinop += sizeof(struct ufs1_dinode);
392 nextinop += sizeof(struct ufs2_dinode);
397 setinodebuf(ino_t inum)
400 if (inum % sblock.fs_ipg != 0)
401 errx(EEXIT, "bad inode number %d to setinodebuf", inum);
402 lastvalidinum = inum + sblock.fs_ipg - 1;
407 if (inobuf.b_un.b_buf != NULL)
409 inobufsize = blkroundup(&sblock, INOBUFSIZE);
410 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
411 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
412 readpercg = sblock.fs_ipg / fullcnt;
413 partialcnt = sblock.fs_ipg % fullcnt;
414 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
415 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
416 if (partialcnt != 0) {
419 partialcnt = fullcnt;
420 partialsize = inobufsize;
422 initbarea(&inobuf, BT_INODES);
423 if ((inobuf.b_un.b_buf = Malloc((unsigned)inobufsize)) == NULL)
424 errx(EEXIT, "cannot allocate space for inode buffer");
431 if (inobuf.b_un.b_buf != NULL)
432 free((char *)inobuf.b_un.b_buf);
433 inobuf.b_un.b_buf = NULL;
437 * Routines to maintain information about directory inodes.
438 * This is built during the first pass and used during the
439 * second and third passes.
441 * Enter inodes into the cache.
444 cacheino(union dinode *dp, ino_t inumber)
446 struct inoinfo *inp, **inpp;
449 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > NDADDR)
450 blks = NDADDR + NIADDR;
452 blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
453 inp = (struct inoinfo *)
454 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
456 errx(EEXIT, "cannot increase directory list");
457 inpp = &inphead[inumber % dirhash];
458 inp->i_nexthash = *inpp;
460 inp->i_parent = inumber == ROOTINO ? ROOTINO : (ino_t)0;
461 inp->i_dotdot = (ino_t)0;
462 inp->i_number = inumber;
463 inp->i_isize = DIP(dp, di_size);
464 inp->i_numblks = blks;
465 for (i = 0; i < (blks < NDADDR ? blks : NDADDR); i++)
466 inp->i_blks[i] = DIP(dp, di_db[i]);
468 for (i = 0; i < NIADDR; i++)
469 inp->i_blks[NDADDR + i] = DIP(dp, di_ib[i]);
470 if (inplast == listmax) {
472 inpsort = (struct inoinfo **)realloc((char *)inpsort,
473 (unsigned)listmax * sizeof(struct inoinfo *));
475 errx(EEXIT, "cannot increase directory list");
477 inpsort[inplast++] = inp;
481 * Look up an inode cache structure.
484 getinoinfo(ino_t inumber)
488 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
489 if (inp->i_number != inumber)
493 errx(EEXIT, "cannot find inode %d", inumber);
494 return ((struct inoinfo *)0);
498 * Clean up all the inode cache structure.
503 struct inoinfo **inpp;
507 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
508 free((char *)(*inpp));
509 free((char *)inphead);
510 free((char *)inpsort);
511 inphead = inpsort = NULL;
522 clri(struct inodesc *idesc, const char *type, int flag)
526 dp = ginode(idesc->id_number);
529 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
530 pinode(idesc->id_number);
532 if (preen || reply("CLEAR") == 1) {
534 printf(" (CLEARED)\n");
536 if (bkgrdflag == 0) {
537 (void)ckinode(dp, idesc);
538 inoinfo(idesc->id_number)->ino_state = USTATE;
542 cmd.value = idesc->id_number;
543 cmd.size = -DIP(dp, di_nlink);
545 printf("adjrefcnt ino %ld amt %lld\n",
546 (long)cmd.value, (long long)cmd.size);
547 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
548 &cmd, sizeof cmd) == -1)
549 rwerror("ADJUST INODE", cmd.value);
555 findname(struct inodesc *idesc)
557 struct direct *dirp = idesc->id_dirp;
559 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
563 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
568 findino(struct inodesc *idesc)
570 struct direct *dirp = idesc->id_dirp;
572 if (dirp->d_ino == 0)
574 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
575 dirp->d_ino >= ROOTINO && dirp->d_ino <= maxino) {
576 idesc->id_parent = dirp->d_ino;
583 clearentry(struct inodesc *idesc)
585 struct direct *dirp = idesc->id_dirp;
587 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
592 return (STOP|FOUND|ALTERED);
603 printf(" I=%lu ", (u_long)ino);
604 if (ino < ROOTINO || ino > maxino)
608 if ((pw = getpwuid((int)DIP(dp, di_uid))) != 0)
609 printf("%s ", pw->pw_name);
611 printf("%u ", (unsigned)DIP(dp, di_uid));
612 printf("MODE=%o\n", DIP(dp, di_mode));
614 printf("%s: ", cdevname);
615 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
616 t = DIP(dp, di_mtime);
618 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
622 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
625 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
627 switch (inoinfo(ino)->ino_state) {
631 inoinfo(ino)->ino_state = FCLEAR;
636 inoinfo(ino)->ino_state = DCLEAR;
644 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
650 * allocate an unused inode
653 allocino(ino_t request, int type)
657 struct bufarea *cgbp;
663 else if (inoinfo(request)->ino_state != USTATE)
665 for (ino = request; ino < maxino; ino++)
666 if (inoinfo(ino)->ino_state == USTATE)
670 cg = ino_to_cg(&sblock, ino);
672 cgp = cgbp->b_un.b_cg;
673 if (!check_cgmagic(cg, cgbp))
675 setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
676 cgp->cg_cs.cs_nifree--;
677 switch (type & IFMT) {
679 inoinfo(ino)->ino_state = DSTATE;
680 cgp->cg_cs.cs_ndir++;
684 inoinfo(ino)->ino_state = FSTATE;
691 DIP_SET(dp, di_db[0], allocblk((long)1));
692 if (DIP(dp, di_db[0]) == 0) {
693 inoinfo(ino)->ino_state = USTATE;
696 DIP_SET(dp, di_mode, type);
697 DIP_SET(dp, di_flags, 0);
698 DIP_SET(dp, di_atime, time(NULL));
699 DIP_SET(dp, di_ctime, DIP(dp, di_atime));
700 DIP_SET(dp, di_mtime, DIP(dp, di_ctime));
701 DIP_SET(dp, di_mtimensec, 0);
702 DIP_SET(dp, di_ctimensec, 0);
703 DIP_SET(dp, di_atimensec, 0);
704 DIP_SET(dp, di_size, sblock.fs_fsize);
705 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
708 inoinfo(ino)->ino_type = IFTODT(type);
713 * deallocate an inode
718 struct inodesc idesc;
721 memset(&idesc, 0, sizeof(struct inodesc));
722 idesc.id_type = ADDR;
723 idesc.id_func = pass4check;
724 idesc.id_number = ino;
726 (void)ckinode(dp, &idesc);
729 inoinfo(ino)->ino_state = USTATE;